diff --git a/backend/.env.example b/backend/.env.example index ad027360..88f654b3 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -1,5 +1,33 @@ # PostgreSQL connection string DATABASE_URL=postgres://mmapp:mmapp@127.0.0.1:5432/mmapp +# Optional operator-approved request and CA for runtime PostgreSQL TLS and SQL identity checks. +# DATABASE_RUNTIME_REQUEST_FILE=/run/config/mmapp/approved-request.json +# DATABASE_RUNTIME_REQUEST_SHA256= +# DATABASE_RUNTIME_CA_FILE=/run/config/mmapp/postgresql-ca.pem +# Optional live operator activation directory (mount the directory read-only). +# DATABASE_RUNTIME_ACTIVATION_DIRECTORY=/var/lib/mmapp/source-activation +# DATABASE_RUNTIME_ACTIVATION_SHA256= +# Optional operator-approved logical schema SHA-256 from db:inspect-schema. +# DATABASE_RUNTIME_SCHEMA_SHA256= +# Restore-aware signed writer profile. Requires source activation, schema pin and restore checkpoint. +# DATABASE_RUNTIME_WRITER_APPROVAL_FILE=/run/config/mmapp/writer-approval.json +# DATABASE_RUNTIME_WRITER_APPROVAL_SHA256= +# Protected exports use the approved capture role and require this private password file. +# The PostgreSQL TLS peer must present the approved self-signed non-CA leaf. +# DATABASE_ARCHIVE_PASSWORD_FILE=/run/secrets/mmapp_database_archive_password +# Optional owner connection URI file for restore and its schema migrations. +# DATABASE_RESTORE_URL_FILE=/run/secrets/mmapp_database_restore_url +# Opt in on every backend/CLI after provisioning the restore quarantine in mmapp_control. +# DATABASE_RESTORE_QUARANTINE=true +# Optional operator-approved pair for verifying one archive's restored data. +# Requires DATABASE_RESTORE_QUARANTINE and DATABASE_RESTORE_URL_FILE. +# DATABASE_RESTORE_ARCHIVE_SHA256= +# DATABASE_RESTORE_DATA_SHA256= +# Optional checkpoint in an operator-managed directory outside the PostgreSQL and MCP state backups. +# Mount the whole directory read-only. Restore requires a staged pending checkpoint. +# DATABASE_RESTORE_CHECKPOINT_FILE=/run/config/mmapp-restore/database-restore-checkpoint.json +# Local socket served by the operator-side db:serve-restore-claims process. +# DATABASE_RESTORE_CHECKPOINT_CLAIM_SOCKET=/run/mmapp-restore/claims.sock # SQLite file used only for one-shot boot ETL (empty PG + existing mikrotik.db) DATABASE_PATH=./mikrotik.db @@ -17,5 +45,37 @@ AUTH_REQUIRED=false AUTH_JWT_SECRET=dev-secret-change-me AUTH_ISSUER=https://auth.shnt.top AUTH_PORTAL_URL=http://localhost:5175 +# Optional portal -> MCP revocation webhook. Separate random secret, at least 32 characters. +# Portal must send signed events to /_internal/mcp/portal/revocations; see docs/mcp.md. +# MCP_PORTAL_WEBHOOK_SECRET= +# MCP_PUBLIC_URL=https://manager.example/mcp +# MCP_STATE_DIR=/var/lib/mikrotik-manager/mcp-state +# Optional separate, externally provisioned bandwidth control source. No automatic +# schema/key/identity creation. See docs/mcp-bandwidth-control-source.md. +# Use the restricted control runtime role, never the owner/application role. +# MMAPP_BANDWIDTH_CONTROL_URL=postgresql://bandwidth_runtime:change-me@control-db:5432/bandwidth_control +# MMAPP_BANDWIDTH_CONTROL_ATTESTATION_FILE=/etc/mmapp/control/source.json +# MMAPP_BANDWIDTH_CONTROL_TRUST_KEY_FILE=/etc/mmapp/control/issuer-public.pem +# SHA256 of the Ed25519 public SPKI DER, pinned outside the attestation itself. +# MMAPP_BANDWIDTH_CONTROL_TRUST_KEY_SHA256= +# MMAPP_BANDWIDTH_CONTROL_EVENT_KEY_FILE=/etc/mmapp/control/event.key +# Separate persistent registry issuer key (raw 32 bytes, different from event.key) +# and operator bootstrap signed by the pinned control-source Ed25519 issuer. +# These inputs grant no actor/network permissions and create no registry rows. +# MMAPP_BANDWIDTH_NODE_ISSUER_KEY_FILE=/etc/mmapp/control/node-issuer.key +# MMAPP_BANDWIDTH_NODE_ISSUER_KEY_SHA256= +# MMAPP_BANDWIDTH_NODE_BOOTSTRAP_FILE=/etc/mmapp/control/node-bootstrap.json # Optional: enabled by the NetFlow agent Compose installer. Linux Unix socket only. # FLOW_AGENT_SOCKET=/run/mm-netflow-host/agent.sock + +# Optional UDP NetFlow v9/IPFIX template lifetime in milliseconds (restart required). +# Collector policy defaults to 90 minutes, not a RouterOS default. Set at least +# 3 times the longest configured exporter template refresh interval; data does +# not refresh templates. Missing/expired templates refuse decoding, not zero traffic. +# FLOW_TEMPLATE_LIFETIME_MS=5400000 + +# Optional NetFlow read admission: standard (default) or constrained. +# Constrained admits two responses (UI + AI), limits SQL sources to 2 MiB, +# cumulative sources to 4 MiB and encoded responses to 2 MiB; large reads refuse. +# This is not a complete installation memory limit. See docs/mcp-2gb-resource-acceptance.md. +# MM_FLOW_READ_RESOURCE_PROFILE=standard diff --git a/backend/Dockerfile b/backend/Dockerfile index 95fb213e..b591b870 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -55,7 +55,9 @@ COPY --from=build /app/package.json /app/package-lock.json ./ COPY --from=build /app/node_modules ./node_modules COPY --from=build /app/packages/contracts ./packages/contracts COPY --from=build /app/backend/dist ./backend/dist +COPY --from=build /app/backend/dist-operator ./backend/dist-operator COPY --from=build /app/backend/package.json ./backend/package.json +COPY --from=build /app/backend/mcp-coverage.json ./backend/mcp-coverage.json COPY --from=build /app/backend/drizzle ./backend/drizzle COPY deploy/netflow-agent/install.sh ./deploy/netflow-agent/install.sh # Nested install from lockfile (dotenv etc.) — ESM resolves from /app/backend/dist → ../node_modules diff --git a/backend/mcp-coverage.json b/backend/mcp-coverage.json new file mode 100644 index 00000000..df94ca49 --- /dev/null +++ b/backend/mcp-coverage.json @@ -0,0 +1,2265 @@ +{ + "version": 1, + "routes": [ + { + "method": "DELETE", + "path": "/api/backups/:id", + "status": "mediated", + "operations": [ + "backups.delete" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "DELETE", + "path": "/api/firewall/address-lists", + "status": "mediated", + "operations": [ + "firewall.addressLists.delete" + ], + "reason": "Общий REST/MCP address-list сервис: scoped ID, human plan, консервативные защиты зависимых правил, полный stable proof, отдельная проверка expiry window, shared locks/read-back/revision/no retry." + }, + { + "method": "DELETE", + "path": "/api/firewall/rules", + "status": "mediated", + "operations": [ + "firewall.rules.delete" + ], + "reason": "Общий REST/MCP сервис формы Firewall: человеческий sealed plan, scoped native ID, полный matcher/order proof, статическая цель, shared locks, read-back, отдельный статус ревизии и отсутствие retry." + }, + { + "method": "DELETE", + "path": "/api/gre/tunnels", + "status": "mediated", + "operations": [ + "network.gre.delete" + ], + "reason": "Общий REST/MCP GRE business executor: интерфейс и v4/v6 адреса, staged disabled, opaque native proof/dependencies, PSK purpose reference, protections/budget/locks, per-command read-back, partial/unknown без retry; SQL/HTTP проверены." + }, + { + "method": "DELETE", + "path": "/api/ipsec/certs/:serverId", + "status": "mediated", + "operations": [ + "network.ipsec.certificates.delete" + ], + "reason": "Общий REST/MCP single certificate DELETE любого выбранного сертификата: force только для peer/identity ссылок, CA/CRL guards без обхода, native ID/private proof, intent/read-back, история и exact VERIFIED; SQL/HTTP any-cert/force/alias/lost/revoke/ABA/privacy приняты. Native force-reference поведение принимается отдельно на CHR." + }, + { + "method": "DELETE", + "path": "/api/ipsec/peers/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.ipsec.peers.delete" + ], + "reason": "Общий REST/MCP сервис принят SQL/HTTP: права и human approval, private source fences, bounded transport, точная семантика, partial/unknown без повторов; сетевой forwarding не заявляется." + }, + { + "method": "DELETE", + "path": "/api/ipsec/server/:serverId", + "status": "mediated", + "operations": [ + "network.ipsec.server.delete" + ], + "reason": "Полный общий REST/MCP server removal: точный ordered manifest, зависимости foreign MC/pool и peer/certificate, полный certificate graph и объявленные CA cascade, preservation profiles/proposals, staged ACK/private read-back и история. Human/server/protection/budget guards; unknown не повторяется. SQL/HTTP parity, runtime countdown и dynamic policy exclusions приняты; настоящий CHR cascade отдельно не проверялся." + }, + { + "method": "DELETE", + "path": "/api/ipsec/users/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.ipsec.users.delete" + ], + "reason": "Общий REST/MCP executor полного удаления клиента с необязательным сертификатом, dependency/source proofs, последовательными intent/read-back, managed-only историей и SQL/HTTP-приёмкой без replay." + }, + { + "method": "DELETE", + "path": "/api/mcp/grants/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "DELETE", + "path": "/api/mcp/management-channels/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "DELETE", + "path": "/api/mcp/oauth/clients/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "DELETE", + "path": "/api/mcp/protections/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "DELETE", + "path": "/api/servers/:id", + "status": "mediated", + "operations": [ + "servers.delete" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "DELETE", + "path": "/api/users/:id", + "status": "mediated", + "operations": [ + "users.delete" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "DELETE", + "path": "/api/users/:id/bindings/:bindingId", + "status": "mediated", + "operations": [ + "users.binding_remove" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "DELETE", + "path": "/api/wireguard/interfaces/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.wireguard.interfaces.delete", + "ros.wireguard.delete" + ], + "reason": "Общий REST/MCP каскад с запечатанными зависимостями, отдельным бюджетом каждой команды, read-back и остановкой без повтора; native alias не приобретает каскадных полномочий." + }, + { + "method": "DELETE", + "path": "/api/wireguard/peers/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.wireguard.peers.delete", + "ros.wireguard_peer.delete" + ], + "reason": "Общий REST/MCP сервис удаления peer: подтверждённый parent, привязки пользователей, полный непрозрачный private proof, защита сервера/интерфейса, drift и endpoint ABA, одна команда DELETE, проверка остальных peers, partial/unknown без повтора и отдельный receipt ревизии." + }, + { + "method": "DELETE", + "path": "/mcp", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/.well-known/oauth-authorization-server", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/.well-known/oauth-protected-resource", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/alerts", + "status": "mediated", + "operations": [ + "alerts.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/alerts/outbox", + "status": "mediated", + "operations": [ + "alerts.outbox.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/auth/config", + "status": "excluded", + "operations": [], + "reason": "Bootstrap/health авторизации интерфейса; бизнес-операция MCP отсутствует." + }, + { + "method": "GET", + "path": "/api/backups", + "status": "mediated", + "operations": [ + "backups.list" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/backups/:id/download", + "status": "mediated", + "operations": [ + "backups.download" + ], + "reason": "Общий сервис REST/MCP, scoped источники и защиты, человеческое подтверждение, intent/read-back или безопасный human-only artifact, SQL/HTTP-приёмка и сверка без replay." + }, + { + "method": "GET", + "path": "/api/backups/jobs/:jobId", + "status": "mediated", + "operations": [ + "backups.jobs.read" + ], + "reason": "Общий реестр фактической REST backup-очереди: доступ только к целиком разрешённой задаче, проверка устройств и прогресса, ограниченные страницы результатов, без notes/имён/путей/текстов ошибок. Временные задачи очищаются при закрытии приложения и restore; несовпадающие или неоднозначные ID не угадываются." + }, + { + "method": "GET", + "path": "/api/backups/schedule", + "status": "mediated", + "operations": [ + "backups.schedule.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/backups/storage", + "status": "mediated", + "operations": [ + "backups.storage.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/bfd/sessions", + "status": "mediated", + "operations": [ + "routing.bfd.sessions.read" + ], + "reason": "Общие REST/MCP state-read сервисы и модели; legacy REST сохраняет defaults совместимости, MCP возвращает observed DTO с null/Unknown. Серверная область до опроса, пагинация, явные failures, areas и отдельное чтение BFD." + }, + { + "method": "GET", + "path": "/api/bgp/catalog", + "status": "mediated", + "operations": [ + "routing.bgp.catalog.read" + ], + "reason": "Общие BGP catalog poll/parser; серверы ограничиваются до опроса, пагинация и failures сохраняются явно." + }, + { + "method": "GET", + "path": "/api/bgp/sessions", + "status": "mediated", + "operations": [ + "routing.bgp.sessions.read" + ], + "reason": "Общий readBgpSessionsForServer/parser и pollBgpCatalog; эффективный scope до SQL/native, серверная и строковая пагинация, failures/incomplete сохраняют частичный отказ вместо успешного пустого списка." + }, + { + "method": "GET", + "path": "/api/bgp/sessions/raw/:id", + "status": "mediated", + "operations": [ + "routing.bgp.sessions.raw.read" + ], + "reason": "Общий readRawBgpSessionsForServer, scope/disabled до native GET, bounded raw DTO с метаданными сервера и маскированием ключей до выдачи/аудита." + }, + { + "method": "GET", + "path": "/api/certificates", + "status": "mediated", + "operations": [ + "certificates.state.read" + ], + "reason": "Общий REST/MCP источник сертификатов с проверкой schema и clock offset; refresh только повторяет чтение без записи. Область до опроса, unknown в MCP, native keys исключены; HTTP/SQL parity и malformed failure проверены." + }, + { + "method": "GET", + "path": "/api/certificates/acme-settings", + "status": "mediated", + "operations": [ + "certificates.acme.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/certificates/issue/:jobId", + "status": "mediated", + "operations": [ + "certificates.jobs.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/certificates/renew-settings", + "status": "mediated", + "operations": [ + "certificates.renew.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/certificates/state", + "status": "mediated", + "operations": [ + "certificates.state.read" + ], + "reason": "Общий сервис REST/MCP с ограниченным типизированным DTO; права и серверная область проверяются до SQL/DNS/опроса, выдача ограничена, ошибки и неизвестные значения сохраняются." + }, + { + "method": "GET", + "path": "/api/containers", + "status": "mediated", + "operations": [ + "containers.state.read" + ], + "reason": "Общий REST/MCP collector контейнеров; область до опроса, стабильные страницы, explicit partial/unsupported, env values и cmd скрыты в MCP. Реальные HTTP/SQL тесты сравнивают legacy каталог и observed DTO." + }, + { + "method": "GET", + "path": "/api/containers/state", + "status": "mediated", + "operations": [ + "containers.state.read" + ], + "reason": "Общий сервис REST/MCP с ограниченным типизированным DTO; права и серверная область проверяются до SQL/DNS/опроса, выдача ограничена, ошибки и неизвестные значения сохраняются." + }, + { + "method": "GET", + "path": "/api/events", + "status": "mediated", + "operations": [ + "events.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/evobgp/settings", + "status": "mediated", + "operations": [ + "evobgp.settings.read" + ], + "reason": "Общие readEvoBgpSettings/publicEvoBgpSettings без создания строки и без API-ключа; полный эффективный каталог проверяется до чтения." + }, + { + "method": "GET", + "path": "/api/filters/gre-tunnels", + "status": "mediated", + "operations": [ + "routing.gre.catalog.read" + ], + "reason": "Общие fetchServerFilters/pollGreCatalog с REST; разрешённые серверы отбираются до опроса, выдача ограничена, IPsec secret не передаётся, ошибки устройств сохраняются." + }, + { + "method": "GET", + "path": "/api/filters/revisions", + "status": "mediated", + "operations": [ + "config.filters.revisions.read" + ], + "reason": "Общий listRevisions с REST: только метаданные, максимум 50 записей; серверная область и права проверяются до SQL, payload ревизии не выдаётся." + }, + { + "method": "GET", + "path": "/api/filters/rules", + "status": "mediated", + "operations": [ + "routing.filters.catalog.read" + ], + "reason": "Общий REST/MCP live/cache reader BGP-in, scope до опроса/SQL, native order/unknown flags, scoped stale fallback; MCP observer не обновляет cache/revisions и не подтверждает эффекты parser hints." + }, + { + "method": "GET", + "path": "/api/firewall/all", + "status": "mediated", + "operations": [ + "firewall.state.read" + ], + "reason": "Общий listFirewallAll/fetchFirewallState с областью до HTTP; MCP получает observed flags/counters без legacy defaults, отдельные matcher-поля, пагинацию и failures. Неполные flags/ошибки не сохраняются как observed revision." + }, + { + "method": "GET", + "path": "/api/firewall/revisions", + "status": "mediated", + "operations": [ + "config.firewall.revisions.read" + ], + "reason": "Общий listRevisions с REST: только метаданные, максимум 50 записей; серверная область и права проверяются до SQL, payload ревизии не выдаётся." + }, + { + "method": "GET", + "path": "/api/geoip", + "status": "mediated", + "operations": [ + "geoip.read" + ], + "reason": "Общий buildGeoipStatus: только локальные readers и метаданные; глобальная эффективная область проверяется до чтения." + }, + { + "method": "GET", + "path": "/api/gre/revisions", + "status": "mediated", + "operations": [ + "config.gre.revisions.read" + ], + "reason": "Общий listRevisions с REST: только метаданные, максимум 50 записей; серверная область и права проверяются до SQL, payload ревизии не выдаётся." + }, + { + "method": "GET", + "path": "/api/gre/tunnels", + "status": "mediated", + "operations": [ + "network.gre.state.read" + ], + "reason": "Общий REST/MCP опрос GRE и IPv4/IPv6-адресов; ограничение серверов до чтения, страницы адресов, nullable-наблюдения, происхождение метаданных, PSK скрыт; MCP не сохраняет историю и не подтверждает forwarding." + }, + { + "method": "GET", + "path": "/api/internet-path/latest", + "status": "mediated", + "operations": [ + "routing.internet-path.latest.read" + ], + "reason": "Общий latest-row REST/MCP reader, scoped JSON-проекция в SQL до разбора скрытых записей, проверка server bindings и схемы, вложенная пагинация, известные нули/unknown и secret-free whitelist. Возвращается сохранённая интерпретация с возрастом/stale/clockSkew; она не доказывает текущий маршрут или forwarding." + }, + { + "method": "GET", + "path": "/api/internet-path/settings", + "status": "mediated", + "operations": [ + "internet-path.settings.read" + ], + "reason": "Общий сервис настроек коллектора; полная область серверов, защита зависимостей, подтверждённый план и проверка сохранения." + }, + { + "method": "GET", + "path": "/api/ipsec", + "status": "mediated", + "operations": [ + "network.ipsec.state.read" + ], + "reason": "Общий REST polling/parser по выбранным серверам до HTTP; MCP использует safe observed DTO без key material, nullable flags/counters и явные failures. Пагинация каталогов и вложенных peers/VTEP/certificates, без обновления истории конфигурации или утверждения о forwarding." + }, + { + "method": "GET", + "path": "/api/ipsec/revisions", + "status": "mediated", + "operations": [ + "config.ipsec.revisions.read" + ], + "reason": "Общий listRevisions с REST: только метаданные, максимум 50 записей; серверная область и права проверяются до SQL, payload ревизии не выдаётся." + }, + { + "method": "GET", + "path": "/api/logs", + "status": "mediated", + "operations": [ + "logs.query" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/logs/:id", + "status": "mediated", + "operations": [ + "logs.get" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/logs/:id/context", + "status": "mediated", + "operations": [ + "logs.context" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/logs/export", + "status": "mediated", + "operations": [ + "logs.export" + ], + "reason": "Общий REST/MCP SQL-источник с server scope до бюджета 50000 строк/8 MiB; маскированная зашифрованная выгрузка с атомарной квотой и отдельным logs.export.read для чтения частей с повторной авторизацией." + }, + { + "method": "GET", + "path": "/api/logs/live", + "status": "mediated", + "operations": [ + "logs.follow" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/logs/settings", + "status": "mediated", + "operations": [ + "logs.settings.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/logs/summary", + "status": "mediated", + "operations": [ + "logs.summary" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/mcp/artifacts/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/audit", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/catalog", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/connections", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/connections/:id/grants", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/coverage", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/database/receipts/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/database/receipts/:id/audit-status", + "status": "excluded", + "operations": [], + "reason": "Человеческая административная сверка аудита внешней квитанции восстановления: управление самим MCP, без отдельного AI-инструмента." + }, + { + "method": "GET", + "path": "/api/mcp/devices/:serverId/:resource", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/devices/catalog", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/devices/:serverId/bridge-vlan/configuration", + "status": "mediated", + "operations": ["network.bridge-vlan.read"], + "reason": "Чтение связанной конфигурации bridge/VLAN через общую типизированную модель и проверку доступа MCP." + }, + { + "method": "GET", + "path": "/api/mcp/drift", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/management-channels", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/oauth/clients", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/oauth/requests/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/operations", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/overview", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/plans", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/plans/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/protections", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/requests", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/routing-profiles", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/mcp/settings", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/api/ospf/all", + "status": "mediated", + "operations": [ + "routing.ospf.state.read" + ], + "reason": "Общие REST/MCP state-read сервисы и модели; legacy REST сохраняет defaults совместимости, MCP возвращает observed DTO с null/Unknown. Серверная область до опроса, пагинация, явные failures, areas и отдельное чтение BFD." + }, + { + "method": "GET", + "path": "/api/ospf/catalog", + "status": "mediated", + "operations": [ + "routing.ospf.catalog.read" + ], + "reason": "Общий OSPF/BFD catalog parser; область серверов ограничивается до опроса, коллекции и серверы выдаются страницами с явными failures." + }, + { + "method": "GET", + "path": "/api/ospf/instances", + "status": "mediated", + "operations": [ + "routing.ospf.state.read" + ], + "reason": "Общие REST/MCP state-read сервисы и модели; legacy REST сохраняет defaults совместимости, MCP возвращает observed DTO с null/Unknown. Серверная область до опроса, пагинация, явные failures, areas и отдельное чтение BFD." + }, + { + "method": "GET", + "path": "/api/ospf/interfaces", + "status": "mediated", + "operations": [ + "routing.ospf.state.read" + ], + "reason": "Общие REST/MCP state-read сервисы и модели; legacy REST сохраняет defaults совместимости, MCP возвращает observed DTO с null/Unknown. Серверная область до опроса, пагинация, явные failures, areas и отдельное чтение BFD." + }, + { + "method": "GET", + "path": "/api/ospf/neighbors", + "status": "mediated", + "operations": [ + "routing.ospf.state.read" + ], + "reason": "Общие REST/MCP state-read сервисы и модели; legacy REST сохраняет defaults совместимости, MCP возвращает observed DTO с null/Unknown. Серверная область до опроса, пагинация, явные failures, areas и отдельное чтение BFD." + }, + { + "method": "GET", + "path": "/api/recursive-routes", + "status": "mediated", + "operations": [ + "routing.recursive.routes.read" + ], + "reason": "Общий REST/MCP reader с явным live/stale fallback, строгими effective properties, устойчивыми логическими ID и scoped SQL. MCP observer не меняет кеш/ревизии; malformed source не объявляется успешным пустым каталогом." + }, + { + "method": "GET", + "path": "/api/recursive-routes/catalog", + "status": "mediated", + "operations": [ + "routing.recursive.catalog.read" + ], + "reason": "Общий сервис REST/MCP с ограниченным типизированным DTO; права и серверная область проверяются до SQL/DNS/опроса, выдача ограничена, ошибки и неизвестные значения сохраняются." + }, + { + "method": "GET", + "path": "/api/recursive-routes/gateways", + "status": "mediated", + "operations": [ + "routing.recursive.gateways.read" + ], + "reason": "Общий REST/MCP reader шлюзов приложения с агрегацией up/down/unknown, пагинацией и ограничением сервера до опроса; active не трактуется как подтверждение передачи пакетов." + }, + { + "method": "GET", + "path": "/api/recursive-routes/gateways/catalog", + "status": "mediated", + "operations": [ + "routing.recursive.gateways.catalog.read" + ], + "reason": "Общий сервис REST/MCP с ограниченным типизированным DTO; права и серверная область проверяются до SQL/DNS/опроса, выдача ограничена, ошибки и неизвестные значения сохраняются." + }, + { + "method": "GET", + "path": "/api/recursive-routes/revisions", + "status": "mediated", + "operations": [ + "config.recursive-routes.revisions.read" + ], + "reason": "Общий listRevisions с REST: только метаданные, максимум 50 записей; серверная область и права проверяются до SQL, payload ревизии не выдаётся." + }, + { + "method": "GET", + "path": "/api/scheduler/runs", + "status": "mediated", + "operations": [ + "scheduler.runs" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/scheduler/status", + "status": "mediated", + "operations": [ + "scheduler.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/servers", + "status": "mediated", + "operations": [ + "servers.list" + ], + "reason": "Общий сервис каталога с ограничениями серверов и типизированным MCP-ответом." + }, + { + "method": "GET", + "path": "/api/servers-api-ping/settings", + "status": "mediated", + "operations": [ + "servers.api-ping.settings.read" + ], + "reason": "Общий сервис настроек коллектора; полная область серверов, защита зависимостей, подтверждённый план и проверка сохранения." + }, + { + "method": "GET", + "path": "/api/servers/:id", + "status": "mediated", + "operations": [ + "servers.list" + ], + "reason": "Общий сервис каталога с ограничениями серверов и типизированным MCP-ответом." + }, + { + "method": "GET", + "path": "/api/servers/:id/bgp/sessions", + "status": "mediated", + "operations": [ + "routing.bgp.sessions.read" + ], + "reason": "Явный serverId в общем sessions-адаптере: общий reader/parser и poller, scope до опроса; диагностические ошибки/неизвестные счётчики не подменяются нулём." + }, + { + "method": "GET", + "path": "/api/servers/:id/containers", + "status": "mediated", + "operations": [ + "containers.state.read" + ], + "reason": "Общий REST/MCP collector контейнеров; область до опроса, стабильные страницы, explicit partial/unsupported, env values и cmd скрыты в MCP. Реальные HTTP/SQL тесты сравнивают legacy каталог и observed DTO." + }, + { + "method": "GET", + "path": "/api/servers/:id/ospf", + "status": "mediated", + "operations": [ + "routing.ospf.state.read" + ], + "reason": "Общие REST/MCP state-read сервисы и модели; legacy REST сохраняет defaults совместимости, MCP возвращает observed DTO с null/Unknown. Серверная область до опроса, пагинация, явные failures, areas и отдельное чтение BFD." + }, + { + "method": "GET", + "path": "/api/servers/:id/ros-src-address", + "status": "mediated", + "operations": [ + "servers.source-address.read" + ], + "reason": "Общий сервис REST/MCP с ограниченным типизированным DTO; права и серверная область проверяются до SQL/DNS/опроса, выдача ограничена, ошибки и неизвестные значения сохраняются." + }, + { + "method": "GET", + "path": "/api/servers/:id/snapshots", + "status": "mediated", + "operations": [ + "servers.snapshots.read" + ], + "reason": "Общий сервис REST/MCP с ограниченным типизированным DTO; права и серверная область проверяются до SQL/DNS/опроса, выдача ограничена, ошибки и неизвестные значения сохраняются." + }, + { + "method": "GET", + "path": "/api/servers/:id/vxlan", + "status": "mediated", + "operations": [ + "network.vxlan.state.read" + ], + "reason": "Общий REST polling/parser по выбранным серверам до HTTP; MCP использует safe observed DTO без key material, nullable flags/counters и явные failures. Пагинация каталогов и вложенных peers/VTEP/certificates, без обновления истории конфигурации или утверждения о forwarding." + }, + { + "method": "GET", + "path": "/api/servers/:id/wan-runtime", + "status": "mediated", + "operations": [ + "servers.wan-runtime.read" + ], + "reason": "Общий readWanRuntimeForServer и модель IPv4/main: scope/disabled до native GET, bound DHCP, explicit unknown/ambiguous default и адреса, ошибка источника не выдаётся за пустое состояние; forwarding не подтверждается." + }, + { + "method": "GET", + "path": "/api/sidebar-counts", + "status": "mediated", + "operations": [ + "system.sidebar.counts.read" + ], + "reason": "Общий REST/MCP readSidebarCounts: SQL COUNT без DTO/привязок пользователей, global allow + все права источников, каталог до 100 серверов до native reads, coalescing, null/unknown отличается от zero, source interval и nonatomic snapshot; реальные SQL/permission/no-native-on-denial проверки." + }, + { + "method": "GET", + "path": "/api/statistics", + "status": "mediated", + "operations": [ + "statistics.read" + ], + "reason": "Общий SQL-сервис статистики/pivot; MCP требует выбранный сервер и период до 31 дня, обновляет только его интерфейсы." + }, + { + "method": "GET", + "path": "/api/statistics/pivot", + "status": "mediated", + "operations": [ + "statistics.pivot" + ], + "reason": "Общий SQL-сервис статистики/pivot; MCP требует выбранный сервер и период до 31 дня, обновляет только его интерфейсы." + }, + { + "method": "GET", + "path": "/api/system/database/backup", + "status": "mediated", + "operations": [ + "database.backup" + ], + "reason": "Общий backup service/event: обязательное human approval, полный global/server scope, exclusive drain, bounded real pg_dump, private temporary libpq service file, encrypted signed receipt и download только JWT владельца; SQL/HTTP проверены." + }, + { + "method": "GET", + "path": "/api/traffic/bound-interfaces", + "status": "mediated", + "operations": [ + "traffic.bindings.history.read" + ], + "reason": "Общий расчёт привязок по сохранённым samples: scoped user eligibility до JSON/метрик, user/server/binding pages, скрытые peer keys/comments, known zero/unknown, binding drift proof и отсутствие RouterOS/планов записи." + }, + { + "method": "GET", + "path": "/api/traffic/flow", + "status": "mediated", + "operations": [ + "traffic.flow.talkers.read" + ], + "reason": "Общая REST/MCP проекция разговоров; серверная область применяется до SQL и pending, источник ограничен, глобальные runtime/host-поля не раскрываются." + }, + { + "method": "GET", + "path": "/api/traffic/flow/analytics", + "status": "mediated", + "operations": [ + "traffic.flow.analytics.read" + ], + "reason": "Общий REST analytics engine в scoped режиме: сервер до SQL, topology без скрытых пользователей/ключей, обход общего кэша, bounded read и страницы, source/access/topology fences; retained данные и контекстные оценки скоростей явно разделены." + }, + { + "method": "GET", + "path": "/api/traffic/flow/clients", + "status": "mediated", + "operations": [ + "traffic.flow.clients.read" + ], + "reason": "Общие REST/MCP readers: scope до SQL/агрегации и каталогов, hidden-binding exclusion, bounded pages/counters, наблюдаемые retained rows вместо выдуманных online/wire rates; клиентская attribution и coverage явно ограничены." + }, + { + "method": "GET", + "path": "/api/traffic/flow/exporters", + "status": "mediated", + "operations": [ + "traffic.flow.exporters.read" + ], + "reason": "Общие REST/MCP readers: scope до SQL/агрегации и каталогов, hidden-binding exclusion, bounded pages/counters, наблюдаемые retained rows вместо выдуманных online/wire rates; клиентская attribution и coverage явно ограничены." + }, + { + "method": "GET", + "path": "/api/traffic/flow/host-files", + "status": "mediated", + "operations": [ + "traffic.flow.hostFiles.export" + ], + "reason": "Общий REST builder файлов NetFlow; полный глобальный допуск и человеческий план создают зашифрованный owner-only артефакт. Ключи не возвращаются MCP; скачивание через JWT повторно проверяет credential/права/область/источник/подпись/hash/срок." + }, + { + "method": "GET", + "path": "/api/traffic/flow/host-status", + "status": "mediated", + "operations": [ + "netflow.host.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/traffic/flow/live", + "status": "mediated", + "operations": [ + "traffic.flow.live.read" + ], + "reason": "Ограниченное наблюдение через общий live-сервис REST/MCP: scope до источника, uncached выборки, отмена и отзыв доступа, общий лимит подписок и SQL/HTTP-приёмка." + }, + { + "method": "GET", + "path": "/api/traffic/flow/map-hops", + "status": "mediated", + "operations": [ + "traffic.flow.map.read" + ], + "reason": "Общий REST map engine со scoped SQL/pending/topology, отказом от общего кэша/enrichment, проверками источника/настроек/доступа, лимитами и вложенными страницами; предполагаемые связи не выдаются за traceroute или forwarding proof." + }, + { + "method": "GET", + "path": "/api/traffic/flow/monthly", + "status": "mediated", + "operations": [ + "traffic.flow.monthly.read" + ], + "reason": "Общие REST/MCP readers: scope до SQL/агрегации и каталогов, hidden-binding exclusion, bounded pages/counters, наблюдаемые retained rows вместо выдуманных online/wire rates; клиентская attribution и coverage явно ограничены." + }, + { + "method": "GET", + "path": "/api/traffic/flow/settings", + "status": "mediated", + "operations": [ + "netflow.settings.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/traffic/flows", + "status": "mediated", + "operations": [ + "traffic.flow.talkers.read" + ], + "reason": "Общая REST/MCP проекция разговоров; серверная область применяется до SQL и pending, источник ограничен, глобальные runtime/host-поля не раскрываются." + }, + { + "method": "GET", + "path": "/api/traffic/servers", + "status": "mediated", + "operations": [ + "traffic.servers.history.read" + ], + "reason": "Общий REST/MCP reader сохранённой истории: scoped SQL/context до чтения, enabled каталог и страницы серверов, подтверждённый ноль/unknown masks, freshness, предел строк без частичных итогов, без RouterOS и observer writes." + }, + { + "method": "GET", + "path": "/api/traffic/servers/:id", + "status": "mediated", + "operations": [ + "traffic.server.history.read" + ], + "reason": "Общий REST/MCP расчёт сервера/интерфейса по сохранённым counters; scoped SQL до разбора, disabled/stale/error не становятся текущим нулём, known volume отдельно от coverage, bounded range/rows, без опроса." + }, + { + "method": "GET", + "path": "/api/traffic/servers/:id/interfaces", + "status": "mediated", + "operations": [ + "traffic.server.interfaces.read" + ], + "reason": "Общий REST/MCP интерфейсный reader за 24 часа: aggregate-only, nullable statusKnown/freshness, будущие samples исключены, страницы/предел строк, без current-state и forwarding claims." + }, + { + "method": "GET", + "path": "/api/traffic/servers/:id/live", + "status": "mediated", + "operations": [ + "traffic.live.read" + ], + "reason": "Общий readTrafficLiveSample; MCP возвращает ограниченную серию до трёх срезов вместо бесконечного SSE. Полнота суммы, направление RX/TX, отзыв доступа и смена источника проверяются перед каждым новым native-запросом." + }, + { + "method": "GET", + "path": "/api/traffic/settings", + "status": "mediated", + "operations": [ + "traffic.settings.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/traffic/users", + "status": "mediated", + "operations": [ + "traffic.users.history.read" + ], + "reason": "Общий расчёт REST/MCP: eligibility/scope до SQL JSON, исключение пользователей со скрытыми привязками, страницы пользователей/серверов/привязок, явный subtotal, guarded global unbound, freshness и проверка drift без observer writes." + }, + { + "method": "GET", + "path": "/api/traffic/users/:id", + "status": "mediated", + "operations": [ + "traffic.user.history.read" + ], + "reason": "Общий REST/MCP user-traffic builder с scoped SQL input вместо глобальной гидрации: nullable masks, peer-specific counters без вывода ключа, completeness выбранных источников, guards прав/server/binding proof." + }, + { + "method": "GET", + "path": "/api/uptime/overview", + "status": "mediated", + "operations": [ + "uptime.overview.read" + ], + "reason": "Общий readUptimeOverview; SQL scope до samples/provenance, bounded pages/window, unknown вместо UI zeros, явный historical fallback, повторные source/probe/grant checks, только stored data без сетевых команд и секретов." + }, + { + "method": "GET", + "path": "/api/uptime/probes", + "status": "mediated", + "operations": [ + "uptime.ping.probes.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/uptime/settings", + "status": "mediated", + "operations": [ + "uptime.settings.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/uptime/sources/:id/interfaces", + "status": "mediated", + "operations": [ + "uptime.ping.interfaces.read" + ], + "reason": "Общий reader/model интерфейсов: scope/disabled до native, effective disabled, unknown flag явно непригоден; пагинация и ошибки не подменяются пустым успехом." + }, + { + "method": "GET", + "path": "/api/uptime/speed-probes", + "status": "mediated", + "operations": [ + "uptime.speed.probes.read" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/uptime/speed-test/endpoints/:id/interfaces", + "status": "mediated", + "operations": [ + "uptime.speed.interfaces.read" + ], + "reason": "Общий reader/model с IPv4 address candidates, без выдуманных flags; bounded пагинация и адреса, неизвестное состояние видно отдельно." + }, + { + "method": "GET", + "path": "/api/uptime/speed-test/runs", + "status": "mediated", + "operations": [ + "uptime.speed.runs.read" + ], + "reason": "Общий readSpeedHistoryRuns/mapSpeedHistoryRun; оба serverId ограничены в SQL до LIMIT/OFFSET и parser, стабильный порядок, unknown/zero сохранены." + }, + { + "method": "GET", + "path": "/api/users", + "status": "mediated", + "operations": [ + "users.list" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/users/:id", + "status": "mediated", + "operations": [ + "users.get" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "GET", + "path": "/api/users/interface-catalog", + "status": "mediated", + "operations": [ + "users.interfaces.read" + ], + "reason": "Общий сервис каталога с ограничениями серверов и типизированным MCP-ответом." + }, + { + "method": "GET", + "path": "/api/users/interface-catalog-state", + "status": "mediated", + "operations": [ + "users.interfaces.state" + ], + "reason": "Общий сервис каталога с ограничениями серверов и типизированным MCP-ответом." + }, + { + "method": "GET", + "path": "/api/vxlan", + "status": "mediated", + "operations": [ + "network.vxlan.state.read" + ], + "reason": "Общий REST polling/parser по выбранным серверам до HTTP; MCP использует safe observed DTO без key material, nullable flags/counters и явные failures. Пагинация каталогов и вложенных peers/VTEP/certificates, без обновления истории конфигурации или утверждения о forwarding." + }, + { + "method": "GET", + "path": "/api/wireguard", + "status": "mediated", + "operations": [ + "network.wireguard.state.read" + ], + "reason": "Общий REST polling/parser по выбранным серверам до HTTP; MCP использует safe observed DTO без key material, nullable flags/counters и явные failures. Пагинация каталогов и вложенных peers/VTEP/certificates, без обновления истории конфигурации или утверждения о forwarding." + }, + { + "method": "GET", + "path": "/api/wireguard/revisions", + "status": "mediated", + "operations": [ + "config.wireguard.revisions.read" + ], + "reason": "Общий listRevisions с REST: только метаданные, максимум 50 записей; серверная область и права проверяются до SQL, payload ревизии не выдаётся." + }, + { + "method": "GET", + "path": "/health", + "status": "excluded", + "operations": [], + "reason": "Bootstrap/health авторизации интерфейса; бизнес-операция MCP отсутствует." + }, + { + "method": "GET", + "path": "/mcp", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "GET", + "path": "/oauth/authorize", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "PATCH", + "path": "/api/firewall/address-lists", + "status": "mediated", + "operations": [ + "firewall.addressLists.toggle" + ], + "reason": "Общий REST/MCP address-list сервис: scoped ID, human plan, консервативные защиты зависимых правил, полный stable proof, отдельная проверка expiry window, shared locks/read-back/revision/no retry." + }, + { + "method": "PATCH", + "path": "/api/firewall/rules", + "status": "mediated", + "operations": [ + "firewall.rules.toggle" + ], + "reason": "Общий REST/MCP сервис формы Firewall: человеческий sealed plan, scoped native ID, полный matcher/order proof, статическая цель, shared locks, read-back, отдельный статус ревизии и отсутствие retry." + }, + { + "method": "PATCH", + "path": "/api/gre/tunnels", + "status": "mediated", + "operations": [ + "network.gre.update" + ], + "reason": "Общий REST/MCP GRE business executor: интерфейс и v4/v6 адреса, staged disabled, opaque native proof/dependencies, PSK purpose reference, protections/budget/locks, per-command read-back, partial/unknown без retry; SQL/HTTP проверены." + }, + { + "method": "PATCH", + "path": "/api/ipsec/peers/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.ipsec.peers.update" + ], + "reason": "Общий REST/MCP peer PATCH всех REST-полей, same-value/native empty strings и history-only noop; certificate presence отклоняется. Native alias сохраняет comment и входит в общий private proof/intent/read-back/history. SQL/HTTP lost/ignored/collateral/reference mismatch/grant/protection/reconcile приняты; native rename gate остаётся отдельным." + }, + { + "method": "PATCH", + "path": "/api/ipsec/users/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.ipsec.users.update" + ], + "reason": "Полный общий REST/MCP PATCH клиента: display name, static IPv4/pool reset, PSK reference, Remote ID/disabled, cert-only promotion/ignored fields, чужой mode-config upsert с consumer closure, история, полный private proof и staged ACK/read-back. Human/server/operation/protection guards и SQL/HTTP parity приняты; lost ACK не повторяется." + }, + { + "method": "PATCH", + "path": "/api/uptime/probes/:id", + "status": "mediated", + "operations": [ + "uptime.ping.visibility.update" + ], + "reason": "Общий guarded updateProbeShowOnDashboard: scoped конфигурация, approval, защиты глобального probe ID, проверка drift/read-back без запуска сетевых измерений." + }, + { + "method": "PATCH", + "path": "/api/users/:id", + "status": "mediated", + "operations": [ + "users.update", + "users.permissions.update" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PATCH", + "path": "/api/wireguard/interfaces/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.wireguard.interfaces.update" + ], + "reason": "Общий REST/MCP WireGuard PATCH: все поля, scoped key refs, rename peer/address ссылок, primary/secondary адреса, private dependency proof, actual byte limits, epoch ABA, intent/fences/read-back и exact VERIFIED reconcile без replay. Сторонние routing/firewall/application зависимости требуют отдельного плана; SQL/HTTP проверены, native readiness отдельно не заявляется." + }, + { + "method": "PATCH", + "path": "/api/wireguard/peers/:serverId/:rosId", + "status": "mediated", + "operations": [ + "network.wireguard.peers.update", + "ros.wireguard_peer.update" + ], + "reason": "Общий REST/MCP сервис изменения peer: зависимости, приватный proof, один PATCH, точный read-back и отдельная read-only сверка без повтора." + }, + { + "method": "POST", + "path": "/_internal/mcp/portal/revocations", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/alerts/outbox/:id/retry", + "status": "mediated", + "operations": [ + "alerts.outbox.retry" + ], + "reason": "Общий REST/MCP повтор unknown: human approval, acknowledged duplicate risk, private row source proof under lock, CAS version/status and application protections; подтверждается только постановка в очередь." + }, + { + "method": "POST", + "path": "/api/alerts/telegram/test", + "status": "mediated", + "operations": [ + "alerts.telegram.test" + ], + "reason": "Общий REST/MCP test payload и единственный guarded POST после человеческого подтверждения. Full global/server scope, owner/purpose secretRef, private proof и late fence; ack не delivery, reject/unknown без повтора, bounded response и запрет redirect." + }, + { + "method": "POST", + "path": "/api/backups/:id/restore", + "status": "mediated", + "operations": [ + "backups.restore" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/backups/create", + "status": "mediated", + "operations": [ + "backups.create" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/backups/storage/sync", + "status": "mediated", + "operations": [ + "backups.storage.sync" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/backups/storage/test", + "status": "mediated", + "operations": [ + "backups.storage.test" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/certificates/acme-settings/test", + "status": "mediated", + "operations": [ + "certificates.acme.test" + ], + "reason": "Общий сервис REST/MCP, scoped источники и защиты, человеческое подтверждение, intent/read-back или безопасный human-only artifact, SQL/HTTP-приёмка и сверка без replay." + }, + { + "method": "POST", + "path": "/api/certificates/issue", + "status": "mediated", + "operations": [ + "certificates.issue" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/certificates/refresh", + "status": "mediated", + "operations": [ + "certificates.state.read" + ], + "reason": "Общий REST/MCP источник сертификатов с проверкой schema и clock offset; refresh только повторяет чтение без записи. Область до опроса, unknown в MCP, native keys исключены; HTTP/SQL parity и malformed failure проверены." + }, + { + "method": "POST", + "path": "/api/events/batch", + "status": "mediated", + "operations": [ + "events.batch.append" + ], + "reason": "Общий REST/MCP client batch insert: все поля и payload, server UUID, human approval/full global scope, bounded encrypted plan, atomic insert и exact VERIFIED read-only global/portal reconcile; lost acknowledgement/tamper/late pause/protection не повторяются. Это записи журнала, не доказательства системных действий." + }, + { + "method": "POST", + "path": "/api/evobgp/catalog", + "status": "mediated", + "operations": [ + "evobgp.catalog.read" + ], + "reason": "Общие parser/formatter и distinct server counts, полный эффективный scope до HTTP/SQL, credentials только из БД, bounded GET без redirects, пагинация всех коллекций с fingerprint и сохранением неизвестных счётчиков." + }, + { + "method": "POST", + "path": "/api/evobgp/test", + "status": "mediated", + "operations": [ + "evobgp.connection.check" + ], + "reason": "Общий ограниченный modules GET; MCP принимает сохранённый ключ или owner/destination-bound secret reference вместо открытого секрета. Полная серверная область проверяется до HTTP, результат не отражает upstream payload и не сохраняет настройки." + }, + { + "method": "POST", + "path": "/api/filters/revisions/:id/restore", + "status": "mediated", + "operations": [ + "filters.revision.restore" + ], + "reason": "Общий guarded applyFiltersToServer; scoped filters revision/checksum повторно проверяются после approval, те же зависимости/защиты и native/DB/revision read-back, human only." + }, + { + "method": "POST", + "path": "/api/firewall/address-lists", + "status": "mediated", + "operations": [ + "firewall.addressLists.create" + ], + "reason": "Общий REST/MCP address-list сервис: scoped ID, human plan, консервативные защиты зависимых правил, полный stable proof, отдельная проверка expiry window, shared locks/read-back/revision/no retry." + }, + { + "method": "POST", + "path": "/api/firewall/revisions/:id/restore", + "status": "mediated", + "operations": [ + "firewall.revision.restore" + ], + "reason": "Общий строгий REST/MCP restore: scoped revision fingerprint, full native proof, точный static порядок/дубликаты, staged disabled preparation, per-command read-back, защиты/бюджет/locks и no retry." + }, + { + "method": "POST", + "path": "/api/firewall/rules", + "status": "mediated", + "operations": [ + "firewall.rules.create" + ], + "reason": "Общий REST/MCP сервис формы Firewall: человеческий sealed plan, scoped native ID, полный matcher/order proof, статическая цель, shared locks, read-back, отдельный статус ревизии и отсутствие retry." + }, + { + "method": "POST", + "path": "/api/firewall/rules/move", + "status": "mediated", + "operations": [ + "firewall.rules.move" + ], + "reason": "Общий moveFirewallRule: точные ID/порядок и все matcher-поля без счётчиков, static source/destination, sealed человеческий план, защиты всей таблицы/сервера, fingerprint перед POST, read-back, без retry и явное состояние сохранения ревизии." + }, + { + "method": "POST", + "path": "/api/geoip/update", + "status": "mediated", + "operations": [ + "geoip.update" + ], + "reason": "Общий сервис REST/MCP, scoped источники и защиты, человеческое подтверждение, intent/read-back или безопасный human-only artifact, SQL/HTTP-приёмка и сверка без replay." + }, + { + "method": "POST", + "path": "/api/gre/revisions/:id/restore", + "status": "mediated", + "operations": [ + "network.gre.revision.restore" + ], + "reason": "Общий строгий REST/MCP GRE restore: весь раздел и IPv4/IPv6 адреса, зависимости и защиты, отключение перед адресами, приватные proof/PSK, human approval, поштучный read-back и отказ без повторной записи." + }, + { + "method": "POST", + "path": "/api/gre/tunnels", + "status": "mediated", + "operations": [ + "network.gre.create" + ], + "reason": "Общий REST/MCP GRE business executor: интерфейс и v4/v6 адреса, staged disabled, opaque native proof/dependencies, PSK purpose reference, protections/budget/locks, per-command read-back, partial/unknown без retry; SQL/HTTP проверены." + }, + { + "method": "POST", + "path": "/api/ipsec/certs/:serverId/export", + "status": "mediated", + "operations": [ + "network.ipsec.existing-certificates.export" + ], + "reason": "Общий REST/MCP executor существующего сертификата: оба selector и все роли, явный private/CA intent, human MCP plan, actor/key no-replay, exact owned file/read/cleanup, encrypted owner-only artifact/TTL; синтетическая SQL/HTTP/portal и browser приёмка. Native CHR отдельно." + }, + { + "method": "POST", + "path": "/api/ipsec/revisions/:id/restore", + "status": "mediated", + "operations": [ + "network.ipsec.revisions.restore" + ], + "reason": "Общий REST/MCP revision restore: strict fingerprint/schema, private whole-state proof, PKI/profile/policy/pool dependencies, exact clears, human approval и read-back без replay. Утраченные PKI ключи не генерируются." + }, + { + "method": "POST", + "path": "/api/ipsec/server/init", + "status": "mediated", + "operations": [ + "network.ipsec.server.init" + ], + "reason": "Общий REST/MCP init: полная форма, raw dependency closure, подпись только новых ACK-owned сертификатов, human approval, per-command fences/read-back и обязательная история." + }, + { + "method": "POST", + "path": "/api/ipsec/users", + "status": "mediated", + "operations": [ + "network.ipsec.users.create" + ], + "reason": "Full shared guarded client creation, purpose-bound secret refs, human-approved encrypted PKCS12 artifact and owner portal; private export separately authorized" + }, + { + "method": "POST", + "path": "/api/ipsec/users/:serverId/:rosId/cert", + "status": "mediated", + "operations": [ + "network.ipsec.existing-certificates.export" + ], + "reason": "Общий REST/MCP executor существующего сертификата: оба selector и все роли, явный private/CA intent, human MCP plan, actor/key no-replay, exact owned file/read/cleanup, encrypted owner-only artifact/TTL; синтетическая SQL/HTTP/portal и browser приёмка. Native CHR отдельно." + }, + { + "method": "POST", + "path": "/api/mcp/connections", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/connections/:id/check", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/connections/:id/grants", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/connections/:id/revalidate", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/connections/:id/rotate", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/connections/:id/status", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/database/archives", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/database/plan", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/devices/:serverId/:resource/plan", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/devices/:serverId/bridge-vlan/configuration/plan", + "status": "mediated", + "operations": ["network.bridge-vlan.configure"], + "reason": "Подготовка связанного плана bridge/VLAN через тот же MCP-инструмент; выполнение требует человеческого подтверждения." + }, + { + "method": "POST", + "path": "/api/mcp/management-channels", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/oauth/clients", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/oauth/consent", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/operations/:id/reconcile", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/operations/:id/recover", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/pause", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/plans/:id/apply", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/plans/:id/decision", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/policy/preview", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/protections", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/routing-profiles/:id/check", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/routing-profiles/:id/pause", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/routing-profiles/plan", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/secrets", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/mcp/stop-automation", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/api/network/resolve-hosts", + "status": "mediated", + "operations": [ + "network.hosts.resolve" + ], + "reason": "Общий bounded resolveNetworkHosts: явный global grant и network-write как в REST, IPv4/DNS/IDN validation, IPv6 literal без DNS как unsupported, дедупликация, TTL/cache/pending/deadline caps, stage access/cancel fences и различение manager resolver от RouterOS/path proof." + }, + { + "method": "POST", + "path": "/api/recursive-routes/revisions/:id/restore", + "status": "mediated", + "operations": [ + "routing.recursive.revision.restore" + ], + "reason": "Общий applyRecursiveToServer/executeRecursiveApply с REST: scoped revision/fingerprint, максимум 50 маршрутов, effective properties, зависимости фильтров/таблиц/интерфейсов, полный бюджет create+delete+metadata и read-back native/revision." + }, + { + "method": "POST", + "path": "/api/scheduler/jobs/:jobKey/run-now", + "status": "mediated", + "operations": [ + "scheduler.run" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/scheduler/refresh", + "status": "mediated", + "operations": [ + "scheduler.refresh" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/servers", + "status": "mediated", + "operations": [ + "servers.create" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/servers-api-ping/collect-now", + "status": "mediated", + "operations": [ + "servers.api-ping.collect" + ], + "reason": "Общий полный manual executor REST/MCP: все включённые устройства, samples/partition maintenance/settings и отложенный глобальный alert engine. GLOBAL + весь каталог, человеческий план, явный skipped при busy, audit/idempotency и no replay; SQL/HTTP приёмка пройдена без реальных уведомлений." + }, + { + "method": "POST", + "path": "/api/servers/:id/containers/remove", + "status": "mediated", + "operations": [ + "containers.remove" + ], + "reason": "Общий removeContainer/removeStoppedContainer с REST: fresh stopped-only, идентичность конфигурации/интерфейса, защиты родителя и подтверждение удаления. Native delete alias использует тот же план." + }, + { + "method": "POST", + "path": "/api/servers/:id/containers/restart", + "status": "mediated", + "operations": [ + "containers.restart" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/servers/:id/containers/start", + "status": "mediated", + "operations": [ + "containers.start" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/servers/:id/containers/stop", + "status": "mediated", + "operations": [ + "containers.stop" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/servers/:id/exec", + "status": "mediated", + "operations": [ + "servers.terminal.read" + ], + "reason": "Общий REST/MCP allowlist терминальной диагностики: 15 типизированных сценариев, все aliases и форматирование, прежнее servers:write право, полный scope/epoch до credentials, bounded transport/output и отзыв между запросами. SQL/REST parity подтверждена; произвольные CLI-команды не принимаются." + }, + { + "method": "POST", + "path": "/api/servers/:id/ospf/optimize", + "status": "mediated", + "operations": [ + "routing.ospf.application.optimize" + ], + "reason": "Общий legacy REST ranker/apply; human-only полный каталог, private source/prefix proofs, native dispatch/read-back и отдельное восстановление." + }, + { + "method": "POST", + "path": "/api/servers/:id/ospf/optimize/preview", + "status": "mediated", + "operations": [ + "routing.ospf.application.preview" + ], + "reason": "Общий REST/MCP reader рейтинга приложения; peer scope и probes отсекаются SQL до чтения, latest aggregate traffic требует известной дельты. MCP разделяет modeled/observed, ограничивает страницы/peers, повторно проверяет доступ/источник перед GET/POST и отменяет HTTP при остановке. Preview не создаёт план и не заменяет строгий family/VRF-aware optimizer." + }, + { + "method": "POST", + "path": "/api/servers/:id/poll", + "status": "mediated", + "operations": [ + "servers.poll" + ], + "reason": "Полный human-approved servers.poll: исходный sealed допуск и actual operation ID, общий снимок/обслуживание истории, quota-bound durable журнал, сохранённая квитанция без повтора native. UI REST-обработчик сохранён; retained admission limits MCP применяются отдельно." + }, + { + "method": "POST", + "path": "/api/servers/:id/probes/run", + "status": "mediated", + "operations": [ + "servers.probes.run" + ], + "reason": "Общий v2 service executor для всех6 диагностик: REST JWT/local и MCP human plan, durable stages/receipts, общие квоты, текущие права, отмена и bounded retention; публичная регистрация и SQL/HTTP/DNS приёмка проверены." + }, + { + "method": "POST", + "path": "/api/servers/test-connection", + "status": "mediated", + "operations": [ + "servers.test-connection" + ], + "reason": "Общий REST/MCP сервис принят SQL/HTTP: права и human approval, private source fences, bounded transport, точная семантика, partial/unknown без повторов; сетевой forwarding не заявляется." + }, + { + "method": "POST", + "path": "/api/system/database/restore", + "status": "mediated", + "operations": [ + "database.restore" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/traffic/collect-now", + "status": "mediated", + "operations": [ + "traffic.collect" + ], + "reason": "Полный общий REST/MCP executor: GLOBAL и весь каталог, человеческий план, штатные samples/settings/locks и глобальный alert hook. SQL/HTTP приёмка на одноразовой БД/localhost; busy/partial/unknown без автоматического повтора, квитанция не подтверждает доставку уведомлений." + }, + { + "method": "POST", + "path": "/api/traffic/flow-overlay", + "status": "mediated", + "operations": [ + "traffic.flow.overlay.apply" + ], + "reason": "Общий сервис REST/MCP, scoped источники и защиты, человеческое подтверждение, intent/read-back или безопасный human-only artifact, SQL/HTTP-приёмка и сверка без replay." + }, + { + "method": "POST", + "path": "/api/traffic/flow/host-reconcile", + "status": "mediated", + "operations": [ + "netflow.host.reconcile" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/traffic/flow/overlay", + "status": "mediated", + "operations": [ + "traffic.flow.overlay.apply" + ], + "reason": "Общий сервис REST/MCP, scoped источники и защиты, человеческое подтверждение, intent/read-back или безопасный human-only artifact, SQL/HTTP-приёмка и сверка без replay." + }, + { + "method": "POST", + "path": "/api/traffic/flow/purge", + "status": "mediated", + "operations": [ + "traffic.flow.purge" + ], + "reason": "Полный общий REST/MCP сценарий: human/global scope, acknowledged drain всех retiring workers, транзакционная очистка, сохранение настроек/ключей/peers, утверждённый partition inventory/budget, journal/restart, SQL/HTTP-приёмка и unknown без replay." + }, + { + "method": "POST", + "path": "/api/traffic/flow/rebuild-facts", + "status": "mediated", + "operations": [ + "traffic.flow.facts.rebuild" + ], + "reason": "Общий REST/MCP rebuild: обязательное human/global/full-server подтверждение, строгий drain существующих writers, атомарные кубы/событие/квитанция, source/protection guards и отдельная read-only сверка без replay. Принят на настоящей временной SQL-базе через MCP и JWT REST; живой worker отдельно не запускался." + }, + { + "method": "POST", + "path": "/api/traffic/flow/settings/generate-keys", + "status": "mediated", + "operations": [ + "traffic.flow.settings.generate-keys" + ], + "reason": "Shared full-config CAS, single-use durable receipt, human approval; public key only, no collector or host apply" + }, + { + "method": "POST", + "path": "/api/uptime/collect-now", + "status": "mediated", + "operations": [ + "uptime.collect" + ], + "reason": "Полный общий REST/MCP executor: GLOBAL и весь каталог, человеческий план, штатные samples/settings/locks и глобальный alert hook. SQL/HTTP приёмка на одноразовой БД/localhost; busy/partial/unknown без автоматического повтора, квитанция не подтверждает доставку уведомлений." + }, + { + "method": "POST", + "path": "/api/uptime/probes/collect-group", + "status": "mediated", + "operations": [ + "uptime.ping.group.collect" + ], + "reason": "Полный общий REST/MCP executor: GLOBAL и весь каталог, человеческий план, штатные samples/settings/locks и глобальный alert hook. SQL/HTTP приёмка на одноразовой БД/localhost; busy/partial/unknown без автоматического повтора, квитанция не подтверждает доставку уведомлений." + }, + { + "method": "POST", + "path": "/api/uptime/speed-test", + "status": "mediated", + "operations": [ + "uptime.speed.test" + ], + "reason": "Обычный RouterOS bandwidth-test через общий speed-сервис: MCP проверяет оба server scope, человеческое подтверждение, срок и лимит нагрузки; общие блокировки, аудит и стандартная история. Без отдельного реестра узлов, ключей и Docker-окон; no replay при unknown." + }, + { + "method": "POST", + "path": "/api/users", + "status": "mediated", + "operations": [ + "users.create" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/users/:id/bindings", + "status": "mediated", + "operations": [ + "users.binding_add" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "POST", + "path": "/api/wireguard/export", + "status": "mediated", + "operations": [ + "network.wireguard.export" + ], + "reason": "Общий REST/MCP export для rsc/conf/peer-conf: явный private-key opt-in, human approval, зашифрованный файл на 15 минут только владельцу через JWT; модель получает квитанцию. Полная SQL/HTTP-проверка форматов, обоих ID пира, source/grant/checkpoint и tamper; неоднозначное или невоспроизводимое состояние отклоняется." + }, + { + "method": "POST", + "path": "/api/wireguard/import", + "status": "mediated", + "operations": [ + "network.wireguard.import.preview", + "network.wireguard.import" + ], + "reason": "Общий REST/MCP импорт .rsc/.conf: owner/purpose/server contentRef, скрытые ключи, disabled preparation и selective activation всех peers/address/interface, отдельные object refs и полный command budget, per-command durable intent/fence/read-back, prefix checkpoint, exact VERIFIED reconcile; partial/unknown без автоматического повтора или компенсации." + }, + { + "method": "POST", + "path": "/api/wireguard/interfaces", + "status": "mediated", + "operations": [ + "network.wireguard.interfaces.create" + ], + "reason": "Общий сервис REST/MCP, scoped источники и защиты, человеческое подтверждение, intent/read-back или безопасный human-only artifact, SQL/HTTP-приёмка и сверка без replay." + }, + { + "method": "POST", + "path": "/api/wireguard/peers", + "status": "mediated", + "operations": [ + "network.wireguard.peers.create" + ], + "reason": "Общий REST/MCP peer create и native alias: полная форма, существующий parent/dependencies, secret refs, staged disabled, per-command fences/read-back, exact verified checkpoint, partial/unknown без retry; SQL/HTTP проверены." + }, + { + "method": "POST", + "path": "/api/wireguard/revisions/:id/restore", + "status": "mediated", + "operations": [ + "config.wireguard.revisions.restore" + ], + "reason": "Общий REST/MCP whole-section restore: строгая ревизия, encrypted desired keys, полный dependency inventory, staged disable, exact clears, per-command fences и read-back без replay." + }, + { + "method": "POST", + "path": "/mcp", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/oauth/revoke", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "POST", + "path": "/oauth/token", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "PUT", + "path": "/api/alerts/rules", + "status": "mediated", + "operations": [ + "alerts.replace" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/alerts/telegram", + "status": "mediated", + "operations": [ + "alerts.telegram.update" + ], + "reason": "Общий guarded REST/MCP selective-update сервис; полный global/server scope, человеческий план, owner/purpose secretRef или явное очищение, private source/reference proof, проверки перед записью и read-back. Сообщения не отправляются." + }, + { + "method": "PUT", + "path": "/api/backups/schedule", + "status": "mediated", + "operations": [ + "backups.schedule.update" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/backups/storage", + "status": "mediated", + "operations": [ + "backups.storage.update" + ], + "reason": "Общий REST updateBackupStorageSettings: глобальный человеческий план, полный каталог серверов/защиты, owner/destination-bound secretRef вместо literal secret, keyed credential proof, защита S3-архивов и занятости, полная проверка конфигурации/сохранённого ключа, partial при несовпадении и idempotency. Настройка не объявляется проверкой доступности S3." + }, + { + "method": "PUT", + "path": "/api/certificates/acme-settings", + "status": "mediated", + "operations": [ + "certificates.acme.update" + ], + "reason": "Общий REST/MCP settings executor: owner/purpose secret refs, full global scope, human approval, bounded private SQL/source proof, CAS/read-back, verified exact receipt и no replay; SQL/HTTP проверены." + }, + { + "method": "PUT", + "path": "/api/certificates/renew-settings", + "status": "mediated", + "operations": [ + "certificates.renew.update" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/evobgp/settings", + "status": "mediated", + "operations": [ + "evobgp.settings.update" + ], + "reason": "Общий guarded writeEvoBgpSettings/schema; подтверждение человеком, URL-bound opaque secret вместо ключа, явное удаление ключа, fingerprint всех настроек и полного каталога, read-back включая ключ без его выдачи." + }, + { + "method": "PUT", + "path": "/api/filters/rules", + "status": "mediated", + "operations": [ + "filters.rules.replace" + ], + "reason": "Общий guarded applyFiltersToServer: human approval, совместные filters/network/BGP права, HMAC источника и routing dependencies, server-wide protections, безопасный compiler, native read-back до metadata/revision, без удаления чужих правил и автоматического повтора." + }, + { + "method": "PUT", + "path": "/api/firewall/address-lists", + "status": "mediated", + "operations": [ + "firewall.addressLists.update" + ], + "reason": "Общий REST/MCP address-list сервис: scoped ID, human plan, консервативные защиты зависимых правил, полный stable proof, отдельная проверка expiry window, shared locks/read-back/revision/no retry." + }, + { + "method": "PUT", + "path": "/api/firewall/rules", + "status": "mediated", + "operations": [ + "firewall.rules.update" + ], + "reason": "Общий REST/MCP сервис формы Firewall: человеческий sealed plan, scoped native ID, полный matcher/order proof, статическая цель, shared locks, read-back, отдельный статус ревизии и отсутствие retry." + }, + { + "method": "PUT", + "path": "/api/geoip", + "status": "mediated", + "operations": [ + "geoip.settings.update" + ], + "reason": "Общий schema/updateGeoipSettings и refreshScheduler; защищённый human-approved план, полный каталог зависимостей, fingerprint конфигурации без observer metadata и read-back сохранённых полей." + }, + { + "method": "PUT", + "path": "/api/internet-path/settings", + "status": "mediated", + "operations": [ + "internet-path.settings.update" + ], + "reason": "Общий сервис настроек коллектора; полная область серверов, защита зависимостей, подтверждённый план и проверка сохранения." + }, + { + "method": "PUT", + "path": "/api/logs/settings", + "status": "mediated", + "operations": [ + "logs.settings.update" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/mcp/connections/:id", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "PUT", + "path": "/api/mcp/settings", + "status": "excluded", + "operations": [], + "reason": "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." + }, + { + "method": "PUT", + "path": "/api/recursive-routes", + "status": "mediated", + "operations": [ + "routing.recursive.routes.replace" + ], + "reason": "Общий applyRecursiveToServer для полного apply/copy набора до 50 строк: человеческий план, стоимость create+delete+metadata, sealed native/DB/filter proof, устойчивые ID, проверка зависимостей и каждого нового/удаляемого маршрута, независимый native/cache/revision read-back и отсутствие повторов при partial/unknown." + }, + { + "method": "PUT", + "path": "/api/servers-api-ping/settings", + "status": "mediated", + "operations": [ + "servers.api-ping.settings.update" + ], + "reason": "Общий сервис настроек коллектора; полная область серверов, защита зависимостей, подтверждённый план и проверка сохранения." + }, + { + "method": "PUT", + "path": "/api/servers/:id", + "status": "mediated", + "operations": [ + "servers.update" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/traffic/flow/settings", + "status": "mediated", + "operations": [ + "netflow.settings.update" + ], + "reason": "Full 10-field shared CAS and fenced main/worker lifecycle, global store/RIPE scope, human approval and durable two-phase receipt; readiness separate" + }, + { + "method": "PUT", + "path": "/api/traffic/settings", + "status": "mediated", + "operations": [ + "traffic.settings.update" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/uptime/probes", + "status": "mediated", + "operations": [ + "uptime.ping.probes.replace" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/uptime/settings", + "status": "mediated", + "operations": [ + "uptime.settings.update" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + }, + { + "method": "PUT", + "path": "/api/uptime/speed-probes", + "status": "mediated", + "operations": [ + "uptime.speed.probes.replace" + ], + "reason": "Типизированный MCP-адаптер этого сервисного сценария; применяются собственные scopes, лимиты, проверка зависимостей и подтверждение записи." + } + ] +} diff --git a/backend/package.json b/backend/package.json index 69c92320..8fa26ae5 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,48 +1,190 @@ { "name": "mikrotik-manager-backend", "version": "1.0.0", - "description": "MikroTik Manager backend — Fastify + Drizzle + PostgreSQL", + "description": "MikroTik Manager backend — Fastify + Drizzle + PostgreSQL", "type": "module", "scripts": { - "test:logs": "tsx src/services/router-logs-model.test.ts && tsx src/plugins/auth.logs.test.ts && tsx src/services/mikrotik-logs.test.ts", + "test:mcp:module-completion": "tsx src/services/ipsec-server-init-model.test.ts && tsx src/modules/mcp/ipsec-server-init-actions.test.ts && tsx src/services/wireguard-revision-restore.test.ts && tsx src/modules/mcp/vrf-ticket-runtime.test.ts && tsx src/services/ipsec-restore.test.ts && tsx src/services/ipsec-pool-dependencies.test.ts && tsx src/services/vrf-production-transition.test.ts && tsx src/services/vrf-production-action-binding.test.ts && tsx src/services/ipsec-revision-restore-model.test.ts && tsx src/modules/mcp/ipsec-revision-actions.test.ts && tsx src/modules/mcp/service-native-progress.test.ts && tsx src/services/ipsec-revision-restore.test.ts && tsx src/services/ipsec-revision-secret.test.ts", + "test:mcp:module-completion:pg": "node scripts/test-mcp-postgres.mjs --module-completion --restore", + "test:mcp:pg": "node scripts/test-mcp-postgres.mjs", + "test:mcp:users-bindings:pg": "node scripts/test-mcp-postgres.mjs --users-bindings", + "test:mcp:bridge-vlan:pg": "node scripts/test-mcp-postgres.mjs --bridge-vlan-configuration", + "test:mcp:external-process:pg": "node scripts/mcp-external-process/run-owned.mjs", + "test:mcp:bridge-vlan:sdk": "node scripts/mcp-external-process/run-owned.mjs .. bridge-vlan", + "test:traffic-flow:ownership": "tsx src/services/traffic-flow-batch-proof.test.ts && tsx src/services/traffic-flow-owned-batch.test.ts && tsx src/services/traffic-flow-owned-writer.test.ts && tsx src/services/traffic-flow-write-transaction.test.ts && tsx src/services/traffic-flow-writer-exclusion.test.ts && tsx src/services/traffic-flow-drain-protocol.test.ts && tsx src/services/traffic-flow-facts-rebuild-execution.test.ts && tsx src/services/traffic-flow-facts-rebuild-source.test.ts && tsx src/services/traffic-flow-facts-rebuild-classifier.test.ts && tsx src/services/traffic-flow-facts-rebuild-service.test.ts && tsx src/services/traffic-flow-ripe-prune-owned.test.ts && tsx src/services/traffic-flow-engine-owned.test.ts && tsx src/routes/traffic-flow-rebuild.test.ts && tsx src/services/traffic-flow-settings-execution.test.ts", + "test:traffic-flow:rebuild:pg": "node scripts/test-mcp-postgres.mjs --flow-rebuild", + "test:traffic-flow:pg": "node scripts/test-mcp-postgres.mjs --flow-writes", + "test:traffic-flow:quality:pg": "node scripts/test-mcp-postgres.mjs --flow-quality", + "test:traffic-flow:chr-evidence": "tsx --test src/test/traffic-flow-chr-capture-check.test.ts src/test/traffic-flow-chr-wire.test.ts src/test/traffic-flow-chr-target-harness.test.ts src/test/traffic-flow-chr-owned-sequence.test.ts src/test/traffic-flow-chr-reconcile.test.ts", + "test:mcp:pg:all": "node scripts/test-mcp-postgres.mjs --all", + "mcp:stdio": "node dist/scripts/mcp-stdio.js", + "mcp:stdio:dev": "tsx src/scripts/mcp-stdio.ts", + "test:logs": "tsx src/services/router-logs-export.test.ts && tsx src/services/router-logs-model.test.ts && tsx src/plugins/auth.logs.test.ts && tsx src/services/mikrotik-logs.test.ts", "test:logs:pg": "tsx src/services/router-logs-sql.test.ts", - "test:sidebar-counts": "tsx src/services/complete-count.test.ts && tsx src/routes/sidebar-counts.test.ts", + "test:sidebar-counts": "tsx src/services/sidebar-counts.test.ts && tsx src/services/complete-count.test.ts && tsx src/routes/sidebar-counts.test.ts", "dev": "tsx watch src/index.ts", - "build": "npm run build -w @mmapp/contracts && tsc", + "build": "npm run build -w @mmapp/contracts && tsc && npm run build:database-operator", + "build:database-operator": "node scripts/build-database-operator.mjs", "test:alerts-telegram": "tsx src/services/alerts-outbox-retry.test.ts && tsx src/services/alerts-outbox-overview.test.ts && tsx src/services/alert-engine/outbox-execution.test.ts && tsx src/services/alerts-telegram-delivery.test.ts && tsx src/services/alerts-revision.test.ts && tsx src/services/alerts-overview.test.ts && tsx ../packages/contracts/src/alerts-normalization.test.ts && tsx src/services/alerts-config-execution.test.ts && tsx src/services/alerts-telegram-settings.test.ts && tsx src/services/alerts-telegram-send.test.ts && tsx src/routes/alerts.test.ts && tsx src/plugins/auth.alerts.test.ts", - "test:alert-engine": "tsx src/services/alert-engine/server-signal-identity.test.ts && tsx src/services/alert-engine/collector-source-signals.test.ts && tsx src/services/alert-engine/monitoring-history.test.ts && tsx src/services/alert-engine/groups.test.ts && tsx src/services/alert-engine/evaluate-rule.test.ts && tsx src/services/gre-tunnels-live.test.ts && tsx src/services/bgp-peers-live.test.ts && tsx src/services/gre-bgp-snapshot-collector.test.ts && tsx src/services/gre-bgp-snapshot-history.test.ts && tsx src/services/gre-bgp-snapshot-sql.test.ts && tsx src/services/alert-engine/collector-source-sql.test.ts && tsx src/services/alert-engine/confirm-stability.test.ts", + "test:alert-engine": "tsx src/services/alert-engine/execution-access.test.ts && tsx src/services/alert-engine/server-signal-identity.test.ts && tsx src/services/alert-engine/collector-source-signals.test.ts && tsx src/services/alert-engine/monitoring-history.test.ts && tsx src/services/alert-engine/groups.test.ts && tsx src/services/alert-engine/evaluate-rule.test.ts && tsx src/services/gre-tunnels-live.test.ts && tsx src/services/bgp-peers-live.test.ts && tsx src/services/gre-bgp-snapshot-collector.test.ts && tsx src/services/gre-bgp-snapshot-history.test.ts && tsx src/services/gre-bgp-snapshot-sql.test.ts && tsx src/services/alert-engine/collector-source-sql.test.ts && tsx src/services/alert-engine/confirm-stability.test.ts", "start": "node dist/index.js", "db:generate": "drizzle-kit generate", "db:migrate": "drizzle-kit migrate", + "db:prepare": "tsx src/scripts/prepare-database.ts", + "db:complete-restore": "tsx src/scripts/complete-database-restore.ts", + "db:serve-restore-claims": "tsx src/scripts/serve-restore-checkpoint-claims.ts", + "db:seal-restore-checkpoint": "tsx src/scripts/seal-database-restore-checkpoint.ts", + "db:stage-restore-checkpoint": "tsx src/scripts/stage-database-restore-checkpoint.ts", + "db:inspect-restore-checkpoint": "tsx src/scripts/inspect-database-restore-checkpoint.ts", + "db:inspect-schema": "tsx src/scripts/inspect-database-schema.ts", "db:studio": "drizzle-kit studio", "db:migrate-from-sqlite": "tsx src/scripts/migrate-sqlite-to-pg.ts", "facts:rebuild": "tsx src/scripts/rebuild-flow-facts.ts", - "test:probes": "tsx src/routes/probes.test.ts && tsx src/routes/probes.cancel.test.ts && tsx src/services/ping-measurement.test.ts && tsx src/services/uptime-probe-observations.test.ts && tsx src/services/ping-measurement-sql.test.ts", - "test:exec": "tsx src/services/mikrotik-logs.test.ts && tsx src/routes/exec.test.ts && tsx src/plugins/auth.exec.test.ts", - "test:auth": "tsx src/plugins/auth-path.test.ts && tsx src/lib/permissions.test.ts && tsx src/plugins/auth.smoke.test.ts && tsx src/plugins/auth.ospf-optimizer.test.ts", - "test:wireguard": "npx tsx src/services/wireguard-config.test.ts && npx tsx src/services/wireguard-ipv6.test.ts && npx tsx --test src/services/wireguard-import.test.ts src/services/wireguard-import-apply.test.ts src/services/wireguard-import-revision.test.ts src/services/wireguard-delete.test.ts && npx tsx src/routes/wireguard-import.test.ts", + "test:mcp:vrf-cleanup": "tsx src/test/chr-vrf-companion-cleanup-proof.test.ts && tsx src/test/chr-vrf-companion-disabled-cleanup-proof.test.ts && tsx src/test/chr-vrf-companion-transfer-cleanup-proof.test.ts", + "test:probes": "npm run test:speed-workflow && tsx src/services/manual-data-collection-execution.test.ts && tsx src/routes/manual-data-collection.test.ts && tsx src/services/network-host-resolution.test.ts && tsx src/routes/probes.test.ts && tsx src/routes/probes.cancel.test.ts && tsx src/services/ping-measurement.test.ts && tsx src/services/uptime-probe-observations.test.ts && tsx src/services/ping-measurement-sql.test.ts", + "test:exec": "tsx src/services/mikrotik-logs.test.ts && tsx src/services/terminal-diagnostics.test.ts && tsx src/routes/exec.test.ts && tsx src/plugins/auth.exec.test.ts", + "test:auth": "tsx --test src/modules/mcp/routing-manual-inverse-history.test.ts && tsx src/db/application-writer-profile-registry-structure.test.ts && tsx src/db/application-writer-profile-registry-read.test.ts && tsx src/db/application-writer-profile-registry-read-parity.test.ts && tsx src/db/application-writer-profile-format.test.ts && tsx src/plugins/auth-path.test.ts && tsx src/lib/permissions.test.ts && tsx src/plugins/auth-provenance.test.ts && tsx src/services/bandwidth-rest-speed-original.test.ts && tsx src/plugins/auth.smoke.test.ts && tsx src/plugins/auth.ospf-optimizer.test.ts && npm run test:mcp", + "test:mcp": "node scripts/vrf-production-profile.test.mjs && node src/services/vrf-production-peer-telemetry.test.mjs && node src/services/vrf-execution-diagnostic.test.mjs && node src/services/vrf-transition-refusal.test.mjs && node src/services/vrf-production-gre-cycle.test.mjs && tsx src/services/vrf-production-gre-link-local.test.ts && tsx src/services/vrf-production-bridge-delete.test.ts && tsx src/services/vrf-production-transition.test.ts && tsx --test src/services/routeros-snapshot-read.test.ts src/services/bridge-vlan-configuration.test.ts src/modules/mcp/bridge-vlan-projection.test.ts src/modules/mcp/vrf-preparation-diagnostic.test.ts && tsx --test src/services/server-poll-error-provenance.test.ts && tsx --test src/modules/mcp/plan-expiry.test.ts src/modules/mcp/result-budget.test.ts src/modules/mcp/result-delivery.test.ts src/modules/mcp/wire-admission.test.ts src/modules/mcp/flow-envelope-allowance.test.ts src/modules/mcp/sdk-terminal-budget.test.ts src/modules/mcp/inflight-owner.test.ts && npm run test:database-source-binding && tsx --test src/modules/mcp/integration-fixture-expiry.test.ts && npm run test:mcp:operation-origin && npm run test:mcp:service-context && npm run test:mcp:operation-locks && tsx --test src/modules/mcp/routing-community-public.test.ts src/modules/mcp/management-transport.test.ts src/modules/mcp/routing-community-context-model.test.ts src/modules/mcp/routing-community-context-native.test.ts ../packages/contracts/src/mcp-routing.test.ts && tsx src/modules/mcp/human-artifact-receipts.test.ts && tsx src/modules/mcp/chr-overlay-readiness.test.ts && tsx src/modules/mcp/chr-recovery-source.test.ts && tsx src/modules/mcp/chr-native-failure.test.ts && tsx src/modules/mcp/ospf-owner-graph.test.ts && tsx src/modules/mcp/routing-measurement-address.test.ts && tsx src/modules/mcp/routing-observation-source.test.ts && tsx src/services/traffic-flow-talkers-model.test.ts && tsx src/services/bgp-prefix-model.test.ts && tsx src/services/vrf-members.test.ts && tsx src/services/routing-gateway-reference.test.ts && tsx src/services/bgp-export-model.test.ts && tsx src/services/traffic-history-model.test.ts && tsx src/services/internet-path-snapshot.test.ts && tsx src/services/internet-path-observation.test.ts && tsx src/modules/mcp/coverage.test.ts && tsx src/modules/mcp/policy.test.ts && tsx src/modules/mcp/events-batch-model.test.ts && tsx src/modules/mcp/operation-revisions.test.ts && tsx src/modules/mcp/native-create-identity.test.ts && tsx src/modules/mcp/ros-step-verification.test.ts && tsx src/modules/mcp/ros-prefix.test.ts && tsx src/modules/mcp/native-conservation-http.test.ts && tsx src/modules/mcp/security.test.ts && tsx src/modules/mcp/state-file-publish.test.ts && tsx src/modules/mcp/routing-model.test.ts && tsx src/modules/mcp/routing-observations-model.test.ts && tsx src/modules/mcp/ospf-health.test.ts && tsx src/modules/mcp/ecmp-preflight.test.ts && tsx src/modules/mcp/recursive-recovery.test.ts && tsx src/modules/mcp/control-path-model.test.ts && tsx src/modules/mcp/routing-community-scoped-model.test.ts && tsx src/modules/mcp/routing-community-scoped-native.test.ts && tsx src/modules/mcp/path-verification.test.ts && tsx src/modules/mcp/database-maintenance.test.ts && tsx src/routes/mcp.test.ts && tsx ../lib/auth-mcp.test.ts && tsx --test src/modules/mcp/routing-controller-fence.test.ts && tsx src/test/chr-vrf-companion-proof.test.ts && tsx src/test/chr-vrf-companion-telemetry.test.ts && tsx src/test/chr-vrf-companion-address-space.test.ts && tsx src/modules/mcp/flow-facts-rebuild-actions.test.ts && tsx src/modules/mcp/ipsec-peer-create-dependencies.test.ts && tsx src/services/ipsec-certificate-update-model.test.ts && tsx src/modules/mcp/ipsec-native-dispatch.test.ts && tsx src/modules/mcp/ipsec-user-update-actions.test.ts && tsx src/modules/mcp/ipsec-server-delete-actions.test.ts && tsx src/services/ipsec-proof-telemetry.test.ts && tsx src/services/vrf-production-admission.test.ts && tsx src/modules/mcp/flow-host-keys-actions.test.ts && tsx src/services/vrf-production-read.test.ts && tsx --test src/services/vrf-production-wireguard-alias.test.ts && tsx src/modules/mcp/ipsec-user-create-actions.test.ts && tsx src/modules/mcp/flow-settings-actions.test.ts && tsx src/services/vrf-production-ticket-store.test.ts && npm run test:mcp:routing-baseline", + "test:wireguard": "tsx src/services/mikrotik-response-bounds.test.ts && tsx --test src/services/wireguard-dependency-read.test.ts && tsx src/services/wireguard-interface-update.test.ts && tsx src/services/wireguard-peer-update.test.ts && npx tsx src/services/wireguard-config.test.ts && npx tsx src/services/wireguard-ipv6.test.ts && npx tsx --test src/services/wireguard-import.test.ts src/services/wireguard-import-apply.test.ts src/services/wireguard-import-revision.test.ts src/services/wireguard-delete.test.ts && npx tsx src/routes/wireguard-import.test.ts && tsx src/services/wireguard-interface-create.test.ts && tsx src/services/wireguard-peer-create.test.ts && tsx src/services/wireguard-import-plan.test.ts && tsx src/services/wireguard-import-execution.test.ts && tsx src/modules/mcp/wireguard-import-actions.test.ts && tsx --test src/services/wireguard-export.test.ts", "test:traffic-rate": "tsx src/services/traffic-rate.test.ts && tsx src/services/traffic-observation.test.ts && tsx src/services/traffic-freshness.test.ts && tsx src/services/traffic-live.test.ts && tsx src/services/traffic-observation-sql.test.ts && tsx src/services/traffic-history-sql.test.ts && tsx src/services/traffic-live-sql.test.ts && tsx src/services/traffic-volume-sql.test.ts && tsx src/services/traffic-binding-status.test.ts && tsx src/services/traffic-status-sql.test.ts && tsx src/services/traffic-gap.test.ts && tsx src/services/traffic-identity.test.ts && tsx src/services/traffic-membership.test.ts && tsx src/services/traffic-aggregate-peak.test.ts && tsx src/services/traffic-users-cache.test.ts && tsx src/services/traffic-collection-lock.test.ts && tsx src/services/traffic-concurrency-sql.test.ts", - "test:traffic-flow": "tsx src/routes/traffic-flow-query.test.ts && tsx src/services/traffic-flow-month.test.ts && tsx src/services/traffic-flow-parse.test.ts && tsx src/services/traffic-flow-ipv6-pipeline.test.ts && tsx src/services/traffic-flow-map-exporter.test.ts && tsx src/services/traffic-flow-ifaces.test.ts && tsx src/services/traffic-flow-ifindex.test.ts && tsx src/services/traffic-flow-dedup.test.ts && tsx src/services/traffic-flow-planes.test.ts && tsx src/services/traffic-flow-ip.test.ts && tsx src/services/traffic-flow-dest.test.ts && tsx src/services/traffic-flow-classify.test.ts && tsx src/services/traffic-flow-ripe.test.ts && tsx src/services/traffic-flow-brands.test.ts && tsx src/services/traffic-flow-ingest.test.ts && tsx src/services/traffic-flow-analytics.test.ts && tsx src/services/traffic-flow-map-hops.test.ts && tsx src/services/traffic-flow-purge.test.ts && tsx src/services/geoip-download.test.ts && tsx src/services/traffic-flow-geoip.test.ts && tsx src/services/traffic-flow-facts.test.ts && tsx src/services/traffic-flow-facts-filter.test.ts && tsx src/services/traffic-flow-facts-rebuild.test.ts && tsx src/services/statistics-aggregate.test.ts", - "test:users": "tsx src/modules/users/iface-type.test.ts && tsx src/modules/users/bindings.test.ts && tsx src/modules/users/interface-inventory.test.ts && tsx src/routes/users-catalog.test.ts", + "test:traffic-flow": "tsx --test src/services/traffic-flow-heartbeat.test.ts src/services/traffic-flow-heartbeat-wiring.test.ts src/services/traffic-flow-heartbeat-receiver.test.ts src/services/traffic-flow-ingest-quality.test.ts src/services/traffic-flow-quality-contract.test.ts && tsx src/services/traffic-flow-catalog-read.test.ts && tsx src/routes/traffic-flow-query.test.ts && tsx src/services/traffic-flow-month.test.ts && tsx src/services/traffic-flow-parse.test.ts && tsx src/services/traffic-flow-ipv6-pipeline.test.ts && tsx src/services/traffic-flow-map-exporter.test.ts && tsx src/services/traffic-flow-ifaces.test.ts && tsx src/services/traffic-flow-ifindex.test.ts && tsx src/services/traffic-flow-dedup.test.ts && tsx src/services/traffic-flow-planes.test.ts && tsx src/services/traffic-flow-ip.test.ts && tsx src/services/traffic-flow-dest.test.ts && tsx src/services/traffic-flow-classify.test.ts && tsx src/services/traffic-flow-ripe.test.ts && tsx src/services/traffic-flow-brands.test.ts && tsx src/services/traffic-flow-ingest.test.ts && tsx src/services/traffic-flow-analytics.test.ts && tsx src/services/traffic-flow-map-hops.test.ts && tsx src/services/traffic-flow-purge.test.ts && tsx src/services/geoip-download.test.ts && tsx src/services/traffic-flow-geoip.test.ts && tsx src/services/traffic-flow-fact-accumulator.test.ts && tsx src/services/traffic-flow-facts.test.ts && tsx src/services/traffic-flow-facts-filter.test.ts && tsx src/services/traffic-flow-facts-rebuild.test.ts && tsx src/services/statistics-aggregate.test.ts && tsx src/services/geoip-manual-update.test.ts && tsx src/services/traffic-flow-overlay-model.test.ts && npm run test:traffic-flow:ownership && tsx src/services/traffic-flow-worker-platform.test.ts && tsx src/services/traffic-flow-template-cache.test.ts && tsx --test src/services/traffic-flow-template-lifetime.test.ts && tsx src/services/traffic-flow-protocol-scope.test.ts && npm run test:traffic-flow:read-budget && npm run test:traffic-flow:resource-profile", + "test:users": "tsx ../packages/contracts/src/users.test.ts && tsx src/modules/users/iface-type.test.ts && tsx src/modules/users/binding-target.test.ts && tsx src/modules/users/bindings.test.ts && tsx src/modules/users/interface-inventory.test.ts && tsx src/routes/users-catalog.test.ts", "test:pg": "tsx src/db/pg-pool.test.ts && tsx src/db/sql-bind.test.ts && tsx src/db/sqlite-json.test.ts && tsx src/db/traffic-flags.test.ts && tsx src/db/pg-schema.test.ts && tsx src/db/scheduler-retention-index.test.ts && tsx src/services/config-revisions.test.ts", - "test:recursive-routes": "tsx src/routes/recursive-routes.test.ts && tsx src/services/recursive-route-catalog.test.ts && tsx src/services/recursive-effective-properties.test.ts && tsx src/services/recursive-apply.test.ts", - "test:config-sync": "tsx src/utils/server-id.test.ts && tsx src/services/config-apply-plan.test.ts && tsx src/services/entity-snapshots.test.ts && tsx src/services/gre-native-identity.test.ts && tsx src/services/gre-ipv6.test.ts && tsx src/routes/gre-ipv6-validation.test.ts && tsx src/services/network-disabled-server.test.ts", - "test:ros-file": "tsx src/services/ros-file-contents.test.ts && tsx src/services/mikrotik-response.test.ts", + "test:recursive-routes": "tsx src/services/recursive-route-identity.test.ts && tsx src/routes/recursive-routes.test.ts && tsx src/services/recursive-route-catalog.test.ts && tsx src/services/recursive-effective-properties.test.ts && tsx src/services/recursive-apply.test.ts", + "test:config-sync": "tsx src/services/owned-routing-policy.test.ts && tsx src/services/routing-filter-body.test.ts && tsx src/services/bgp-community-policy-model.test.ts && tsx src/services/bgp-connection-read.test.ts && tsx src/services/bgp-filter-binding-model.test.ts && tsx src/services/filter-rules-write-model.test.ts && tsx src/services/filter-rules-mutation.test.ts && tsx src/utils/server-id.test.ts && tsx src/services/config-apply-plan.test.ts && tsx src/services/entity-snapshots.test.ts && tsx src/services/gre-native-identity.test.ts && tsx src/services/gre-ipv6.test.ts && tsx src/routes/gre-ipv6-validation.test.ts && tsx src/services/network-disabled-server.test.ts", + "test:ros-file": "tsx src/services/mikrotik-get-cancel.test.ts && tsx src/services/ros-file-contents.test.ts && tsx src/services/mikrotik-response.test.ts && tsx src/services/mikrotik-owned-file.test.ts && tsx --test src/services/mikrotik-owned-file-zeroization.test.ts", "test:scheduler": "tsx src/services/scheduler-running.test.ts && tsx src/services/scheduler-recorded-run.test.ts && tsx src/routes/scheduler.test.ts", - "test:backups": "tsx src/services/backup-local-file-state.test.ts && tsx src/services/router-config-export.test.ts && tsx src/services/backup-scheduler-run.test.ts && tsx src/services/backup-restore.test.ts && tsx src/services/backup-deletion.test.ts && tsx src/services/backup-dto.test.ts && tsx src/services/backup-storage-update.test.ts && tsx src/services/backup-job-execution.test.ts && tsx src/routes/backups-operations.test.ts && tsx src/services/s3-backup-client.test.ts", - "test:ipsec": "tsx src/services/ipsec-config.test.ts && tsx src/services/ipsec-state.test.ts && tsx src/routes/ipsec-history.test.ts && tsx src/services/ipsec-restore.test.ts && tsx src/services/ipsec-operation.test.ts && tsx src/routes/ipsec-mutation.test.ts && tsx src/routes/ipsec-nat.test.ts && tsx src/routes/ipsec-group.test.ts", - "test:live-maps": "tsx src/services/gre-keepalive.test.ts && tsx src/services/gre-read.test.ts && tsx src/services/ospf-route-parse.test.ts && tsx src/services/vxlan-live.test.ts && tsx src/routes/vxlan.test.ts && tsx src/services/containers-live.test.ts && tsx src/services/firewall-poll.test.ts && tsx src/services/gre-catalog-poll.test.ts && tsx src/routes/filters-gre-catalog.test.ts && tsx src/routes/bgp-catalog.test.ts && tsx src/services/bgp-catalog-parse.test.ts && tsx src/routes/ospf-catalog.test.ts && tsx src/services/ospf-observed-values.test.ts && tsx src/routes/ospf-legacy.test.ts", + "test:backups": "tsx src/modules/mcp/backup-storage-actions.test.ts && tsx src/services/backup-job-read.test.ts && tsx src/services/backup-local-file-state.test.ts && tsx src/services/router-config-export.test.ts && tsx src/services/backup-scheduler-run.test.ts && tsx src/services/backup-restore.test.ts && tsx src/services/backup-deletion.test.ts && tsx src/services/backup-dto.test.ts && tsx src/services/backup-storage-update.test.ts && tsx src/services/backup-job-execution.test.ts && tsx src/routes/backups-operations.test.ts && tsx src/services/s3-backup-client.test.ts && tsx src/services/backup-content-bounds.test.ts", + "test:ipsec": "tsx src/services/ipsec-config.test.ts && tsx src/services/ipsec-state.test.ts && tsx src/routes/ipsec-history.test.ts && tsx src/services/ipsec-restore.test.ts && tsx src/services/ipsec-operation.test.ts && tsx src/routes/ipsec-mutation.test.ts && tsx src/routes/ipsec-nat.test.ts && tsx src/routes/ipsec-group.test.ts && tsx src/services/ipsec-peer-delete.test.ts && tsx src/modules/mcp/ipsec-peer-delete-actions.test.ts && tsx src/services/ipsec-user-delete.test.ts && tsx src/modules/mcp/ipsec-user-delete-actions.test.ts && tsx src/services/ipsec-certificate-delete.test.ts && tsx src/modules/mcp/ipsec-certificate-delete-actions.test.ts && tsx src/services/ipsec-peer-update.test.ts && tsx src/modules/mcp/ipsec-peer-update-actions.test.ts && tsx src/services/ipsec-user-update-model.test.ts && tsx src/modules/mcp/ipsec-user-update-actions.test.ts && tsx src/services/ipsec-server-delete-model.test.ts && tsx src/modules/mcp/ipsec-server-delete-actions.test.ts && tsx src/services/ipsec-proof-telemetry.test.ts && tsx src/services/ipsec-user-create-model.test.ts", + "test:live-maps": "tsx src/services/gre-revision-restore.test.ts && tsx src/services/gre-native-mutation.test.ts && tsx src/services/gre-mutation.test.ts && tsx src/services/filter-rules-observed.test.ts && tsx src/services/firewall-revision-restore.test.ts && tsx src/services/firewall-address-write.test.ts && tsx src/services/firewall-rule-write.test.ts && tsx src/services/gre-observed-read.test.ts && tsx src/services/network-observed-read.test.ts && tsx src/services/firewall-rule-move.test.ts && tsx src/services/ospf-state-read.test.ts && tsx src/modules/mcp/readiness-http.test.ts && tsx src/services/probe-interface-model.test.ts && tsx src/services/wan-runtime-model.test.ts && tsx src/services/gre-keepalive.test.ts && tsx src/services/gre-read.test.ts && tsx src/services/ospf-route-parse.test.ts && tsx src/services/vxlan-live.test.ts && tsx src/routes/vxlan.test.ts && tsx src/services/containers-live.test.ts && tsx src/services/firewall-poll.test.ts && tsx src/services/gre-catalog-poll.test.ts && tsx src/routes/filters-gre-catalog.test.ts && tsx src/routes/bgp-catalog.test.ts && tsx src/services/bgp-catalog-parse.test.ts && tsx src/routes/ospf-catalog.test.ts && tsx src/services/ospf-observed-values.test.ts && tsx src/routes/ospf-legacy.test.ts", "test": "node scripts/test.mjs", - "test:evolution": "tsx src/services/evobgp-catalog.test.ts && tsx src/routes/evobgp-catalog.test.ts && tsx src/routes/evobgp-settings.test.ts && tsx src/plugins/auth.evolution-settings.test.ts", - "test:netflow-purge": "tsx src/db/operation-lock.test.ts && tsx src/services/traffic-flow-purge-execution.test.ts && tsx src/routes/traffic-flow-purge.test.ts", - "test:netflow-settings": "tsx src/services/traffic-flow-host-file-builders.test.ts && tsx src/services/traffic-flow-key-execution.test.ts && tsx src/routes/traffic-flow-settings.test.ts && tsx src/plugins/auth.netflow-settings.test.ts && tsx src/services/traffic-flow-agent-install.test.ts && tsx src/services/traffic-flow-agent-client.test.ts && tsx src/services/traffic-flow-operation-queue.test.ts && tsx src/routes/traffic-flow-host.test.ts", + "test:evolution": "tsx src/services/evobgp-catalog.test.ts && tsx src/services/evobgp-client.test.ts && tsx src/routes/evobgp-catalog.test.ts && tsx src/routes/evobgp-settings.test.ts && tsx src/plugins/auth.evolution-settings.test.ts", + "test:netflow-purge": "tsx src/db/operation-lock.test.ts && tsx src/services/traffic-flow-purge-execution.test.ts && tsx src/services/flow-purge-partitions.test.ts && tsx --test src/services/traffic-flow-worker-lifecycle.test.ts && tsx src/routes/traffic-flow-purge.test.ts", + "test:netflow-settings": "tsx src/services/traffic-flow-host-file-builders.test.ts && tsx src/services/traffic-flow-key-execution.test.ts && tsx src/routes/traffic-flow-settings.test.ts && tsx src/plugins/auth.netflow-settings.test.ts && tsx src/services/traffic-flow-agent-install.test.ts && tsx src/services/traffic-flow-agent-client.test.ts && tsx src/services/traffic-flow-operation-queue.test.ts && tsx src/routes/traffic-flow-host.test.ts && tsx src/services/traffic-flow-settings-execution.test.ts && tsx src/modules/mcp/flow-host-keys-actions.test.ts && tsx src/services/traffic-flow-lifecycle-fence.test.ts && tsx src/services/traffic-flow-settings-store.test.ts && tsx src/services/traffic-flow-settings-service.test.ts && tsx src/modules/mcp/flow-settings-actions.test.ts", "test:geoip": "tsx src/services/geoip-download.test.ts && tsx src/services/traffic-flow-geoip.test.ts && tsx src/plugins/auth.geoip.test.ts", - "test:ospf-optimizer": "tsx src/services/ospf-optimizer-identity.test.ts && tsx src/services/ospf-optimizer-metrics.test.ts && tsx src/services/ospf-optimizer-plan.test.ts && tsx src/services/ospf-effective-disabled.test.ts && tsx src/routes/ospf-optimizer.test.ts", + "test:ospf-optimizer": "tsx src/services/ospf-cost-optimization.test.ts && tsx src/modules/mcp/ospf-legacy-source.test.ts && tsx src/services/ospf-legacy-optimization.test.ts && tsx src/services/ospf-template-flags.test.ts && tsx src/services/ospf-route-proof.test.ts && tsx src/services/ospf-link-catalog.test.ts && tsx src/services/ospf-identity-catalog.test.ts && tsx src/services/ospf-template-cost-write.test.ts && tsx src/services/ospf-template-cost-http.test.ts && tsx src/services/ospf-optimizer-identity.test.ts && tsx src/services/ospf-optimizer-metrics.test.ts && tsx src/services/ospf-optimizer-plan.test.ts && tsx src/services/ospf-effective-disabled.test.ts && tsx src/routes/ospf-optimizer.test.ts", "test:containers": "tsx src/services/containers-live.test.ts && tsx src/services/containers-state.test.ts && tsx src/services/container-removal.test.ts && tsx src/routes/containers-state.test.ts", - "test:certificates": "tsx src/services/certificate-installation.test.ts && tsx src/services/certificate-renew-collector.test.ts && tsx src/services/certificate-issue-execution.test.ts && tsx src/services/certificate-issue-request.test.ts && tsx src/routes/certificates-issue.test.ts && tsx src/services/certificate-issue-dto.test.ts && tsx src/services/certificate-parse.test.ts && tsx src/services/certificates-state.test.ts && tsx src/routes/certificates-state.test.ts", - "test:system-database": "tsx src/services/system-database-partition-restore.test.ts && tsx src/services/system-database-cache.test.ts && tsx src/services/database-background-work.test.ts && tsx src/services/database-request-gate.test.ts && tsx src/services/traffic-flow-worker-shutdown.test.ts && tsx src/services/system-database-restore-execution.test.ts && tsx src/services/system-database-operation.test.ts && tsx src/routes/system-database.test.ts && tsx src/plugins/auth.system-database.test.ts", - "test:all": "npm run test:logs && npm run test:probes && npm run test:exec && npm run test:alert-engine && npm run test:alerts-telegram && npm run test:auth && npm run test:wireguard && npm run test:ipsec && npm run test:traffic-rate && npm run test:traffic-flow && npm run test:users && npm run test:pg && npm run test:backups && npm run test:scheduler && npm run test:live-maps && npm run test:ospf-optimizer && npm run test:recursive-routes && npm run test:config-sync && npm run test:ros-file && npm run test:containers && npm run test:certificates && npm run test:netflow-settings && npm run test:netflow-purge && npm run test:evolution && npm run test:system-database && npm run test:server-privacy", - "test:server-privacy": "tsx src/routes/server-secrets-sql.test.ts" + "test:certificates": "tsx src/services/certificate-installation.test.ts && tsx src/services/certificate-renew-collector.test.ts && tsx src/services/certificate-issue-execution.test.ts && tsx src/services/certificate-issue-request.test.ts && tsx src/routes/certificates-issue.test.ts && tsx src/services/certificate-issue-dto.test.ts && tsx src/services/certificate-parse.test.ts && tsx src/services/certificates-state.test.ts && tsx src/routes/certificates-state.test.ts && tsx src/services/acme-settings-test.test.ts && tsx src/services/acme-settings-update-model.test.ts && tsx src/modules/mcp/acme-settings-actions.test.ts", + "test:system-database": "tsx src/services/bounded-database-dump.test.ts && tsx src/services/database-dump-connection.test.ts && tsx src/services/system-database-partition-restore.test.ts && tsx src/services/system-database-cache.test.ts && tsx src/services/database-background-work.test.ts && tsx src/services/database-request-gate.test.ts && tsx --test src/services/detached-database-admission.test.ts && tsx src/services/traffic-flow-worker-shutdown.test.ts && tsx src/services/system-database-restore-execution.test.ts && tsx src/services/system-database-operation.test.ts && tsx src/services/system-database-schema-readiness.test.ts && tsx --test src/routes/mcp-oauth-database-admission.test.ts && tsx src/routes/system-database.test.ts && tsx src/plugins/auth.system-database.test.ts && tsx src/services/database-control-archive-boundary.test.ts && tsx src/db/database-schema-control-model.test.ts && tsx src/db/database-schema-control.test.ts && tsx src/db/controlled-migrations.test.ts && tsx src/db/controlled-schema-verification.test.ts && tsx --test src/db/migration-data-inventory.test.ts && tsx --test src/services/database-maintenance-startup.test.ts && tsx --test src/services/database-restore-audit.test.ts && tsx --test src/services/database-maintenance-resolution.test.ts && tsx --test scripts/database-maintenance-resolution.test.mts", + "test:all": "npm run test:logs && npm run test:probes && npm run test:exec && npm run test:alert-engine && npm run test:alerts-telegram && npm run test:auth && npm run test:wireguard && npm run test:ipsec && npm run test:traffic-rate && npm run test:traffic-flow && npm run test:users && npm run test:pg && npm run test:sidebar-counts && npm run test:backups && npm run test:scheduler && npm run test:live-maps && npm run test:ospf-optimizer && npm run test:recursive-routes && npm run test:config-sync && npm run test:ros-file && npm run test:containers && npm run test:certificates && npm run test:netflow-settings && npm run test:netflow-purge && npm run test:evolution && npm run test:system-database && npm run test:server-privacy && npm run test:mcp:module-completion && npm run test:mcp:loopback-agent && npm run test:server-probe-management && npm run test:server-poll-management && npm run test:mcp:routing-authority && npm run test:mcp:native-readers && npm run test:mcp:chr-acquisition && npm run test:mcp:shared-context && npm run test:mcp:telemetry-configuration", + "test:server-privacy": "tsx ../packages/contracts/src/servers.test.ts && tsx src/routes/server-secrets-sql.test.ts && tsx --test src/services/server-connection-test.test.ts", + "test:mcp:flow-rebuild:pg": "node scripts/test-mcp-postgres.mjs --flow-rebuild-mcp", + "test:mcp:flow-host-keys:pg": "node scripts/test-mcp-postgres.mjs --flow-host-keys", + "test:mcp:loopback-agent": "tsx src/test/loopback-http-agent.test.ts", + "test:mcp:pending-foundations": "tsx src/services/server-poll-execution.test.ts && tsx src/services/server-probe-execution.test.ts && tsx src/services/ipsec-existing-certificate-export.test.ts && tsx src/modules/mcp/ipsec-existing-certificate-export-actions.test.ts && tsx src/modules/mcp/ipsec-existing-certificate-export-production.test.ts && tsx src/services/ipsec-existing-certificate-export-rest.test.ts && tsx src/services/ipsec-existing-certificate-export-rest-production.test.ts && tsx src/services/ipsec-existing-certificate-export-rest-journal.test.ts && tsx src/services/ipsec-existing-certificate-export-rest-http.test.ts && tsx src/services/ipsec-existing-certificate-export-artifact.test.ts && tsx src/modules/mcp/ipsec-existing-certificate-export-registration.test.ts", + "test:mcp:ipsec-create:pg": "node scripts/test-mcp-postgres.mjs --module-completion --only=ipsec-create --restore", + "test:mcp:ipsec-export-journal:pg": "node scripts/test-mcp-postgres.mjs --ipsec-export-journal --restore", + "test:mcp:ipsec-export:http:pg": "node scripts/test-mcp-postgres.mjs --ipsec-export-http --restore", + "test:mcp:diagnostic-cancel-host:pg": "node scripts/test-mcp-postgres.mjs --diagnostic-cancel-host --restore", + "test:server-probe-management": "tsx src/services/server-probe-authority.test.ts && tsx src/services/server-probe-public-result.test.ts && tsx src/services/server-probe-execution.test.ts && tsx src/services/server-probe-rest-contract.test.ts && tsx src/services/server-probe-rest-execution.test.ts && tsx src/services/server-probe-journal.test.ts && tsx src/services/server-probe-journal-history.test.ts && tsx src/modules/mcp/server-probe-actions.test.ts && tsx src/modules/mcp/diagnostic-cancellation.test.ts && tsx src/modules/mcp/service-diagnostic-progress.test.ts && tsx src/services/mikrotik-get-admission.test.ts && tsx src/services/server-probe-storage-journal-adapter.test.ts && npm run test:server-probe-history && npm run test:server-probe-storage-models", + "test:server-poll-http:pg": "node scripts/test-server-poll-http-owned.mjs", + "test:server-poll-approval:pg": "node scripts/test-server-poll-approval-owned.mjs", + "test:server-poll-atomic:pg": "node scripts/test-server-poll-atomic-owned.mjs", + "test:source-capture-adopted:pg": "node scripts/test-source-capture-adopted-postgres.mjs", + "test:server-poll-settings-epoch:pg": "node scripts/test-server-poll-settings-epoch-owned.mjs", + "test:server-poll-management": "tsx --test src/services/server-poll-maintenance-planner.test.ts src/services/server-poll-maintenance-groups.test.ts && tsx src/services/server-poll-execution.test.ts && tsx src/services/server-poll-production.test.ts && tsx src/services/server-poll-journal.test.ts && tsx src/services/server-poll-authority.test.ts && tsx src/services/server-poll-rest.test.ts && tsx src/services/server-partition-maintenance-lease.test.ts && npm run test:server-poll-storage && npm run test:partition-writers && tsx --test src/services/server-poll-history-verification.test.ts src/services/server-poll-terminal-source.test.ts src/services/server-poll-terminal-archive.test.ts src/services/server-poll-archive-accounting.test.ts src/services/server-poll-terminal-compaction.test.ts src/services/server-poll-history-reader.test.ts src/modules/mcp/server-poll-actions.test.ts src/modules/mcp/server-poll-production.test.ts src/modules/mcp/server-poll-registration.test.ts && tsx src/modules/mcp/effective-connection-source.test.ts && tsx src/services/server-poll-cost-diagnostics.test.ts && tsx src/services/server-poll-journal-snapshot.test.ts && tsx src/modules/mcp/principal-security-source.test.ts && tsx --test src/modules/mcp/poll-runtime-policy-source.test.ts src/modules/mcp/poll-policy-coordinator.test.ts && tsx src/services/server-poll-http.test.ts && tsx --test src/services/server-poll-approval-context.test.ts", + "test:server-poll-storage": "tsx src/services/server-poll-capacity.test.ts && tsx src/services/server-poll-storage-model.test.ts && tsx src/services/server-poll-storage.test.ts && tsx src/services/server-poll-storage-journal.test.ts && tsx src/services/server-poll-storage-retention.test.ts", + "test:bandwidth-admission:pg": "node scripts/test-mcp-postgres.mjs --bandwidth-admission --restore", + "test:bandwidth-control-source:pg": "node scripts/test-mcp-postgres.mjs --bandwidth-control-source --restore", + "test:bandwidth-control-source": "tsx src/services/bandwidth-control-role.test.ts && tsx src/services/bandwidth-control-catalog.test.ts && tsx --test src/services/bandwidth-control-identity.test.ts", + "bandwidth:control:seal": "tsx src/scripts/seal-bandwidth-control-source.ts", + "test:bandwidth-node-observation": "tsx src/services/bandwidth-node-observation.test.ts", + "test:bandwidth-peers": "tsx src/services/bandwidth-peer-observation.test.ts && tsx src/services/bandwidth-peer-network-observation.test.ts && tsx src/services/bandwidth-peer-direct-observation.test.ts && tsx src/services/bandwidth-operator-enrollment.test.ts && tsx --test --test-concurrency=1 src/services/bandwidth-control-registry-frame.test.ts src/services/bandwidth-control-registry-identity.test.ts src/services/bandwidth-node-identity.test.ts src/services/bandwidth-node-registry-batch.test.ts src/services/bandwidth-operator-docker-stop.test.ts src/services/bandwidth-operator-writer-window.test.ts src/services/bandwidth-operator-writer-composition.test.ts src/services/bandwidth-operator-registry.test.ts src/services/bandwidth-external-node-registry.test.ts src/services/bandwidth-external-node-registry-catalog.test.ts src/services/bandwidth-external-node-registry-role.test.ts src/modules/mcp/routing-bandwidth-original.test.ts src/services/bandwidth-peer-identity.test.ts src/services/bandwidth-peer-resolver.test.ts && node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/bandwidth-node-proof-batch.test.ts src/services/bandwidth-node-key-effect.test.ts src/services/bandwidth-control-file.test.ts src/services/bandwidth-node-bootstrap-v2.test.ts src/services/bandwidth-peer-fence.test.ts src/services/bandwidth-operator-public-files.test.ts src/services/bandwidth-registry-combined.test.ts && npm run test:bandwidth-public-metadata && npm run test:bandwidth-public-registry && npm run test:bandwidth-peer-metadata && npm run test:bandwidth-guarded-peer-sources", + "test:bandwidth-peers:pg": "node scripts/test-mcp-postgres.mjs --bandwidth-external-registry", + "test:bandwidth-rest-origin:pg": "node scripts/test-mcp-postgres.mjs --rest-speed-original", + "test:peer-speed-workflow:pg": "node scripts/test-mcp-postgres.mjs --bandwidth-speed-workflow", + "test:scheduled-speed-original:pg": "node scripts/test-mcp-postgres.mjs --scheduled-speed-original", + "test:operator-docker:pg": "node scripts/test-mcp-postgres.mjs --operator-docker-sql", + "test:bandwidth-public-metadata": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/bandwidth-control-public-metadata.test.ts src/services/bandwidth-node-public-metadata.test.ts", + "test:bandwidth-public-registry:pg": "node scripts/test-mcp-postgres.mjs --public-registry-metadata", + "test:bandwidth-public-registry": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/bandwidth-control-public-registry.test.ts src/services/bandwidth-node-public-registry.test.ts", + "test:bandwidth-peer-metadata": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/bandwidth-peer-metadata.test.ts src/services/bandwidth-peer-metadata-source.test.ts src/services/bandwidth-operator-metadata.test.ts", + "test:operator-peer-metadata:pg": "node scripts/test-mcp-postgres.mjs --operator-peer-metadata", + "test:bandwidth-guarded-peer-sources": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/server-probe-storage-connection.test.ts src/services/bandwidth-operator-peer-source-guard.test.ts src/services/bandwidth-external-peer-source.test.ts src/services/bandwidth-managed-peer-source.test.ts src/services/bandwidth-operator-peer-source-composition.test.ts", + "test:guarded-peer-sources:pg": "node scripts/test-mcp-postgres.mjs --external-peer-metadata", + "test:mcp:operation-origin": "node --experimental-test-module-mocks --import tsx --test src/modules/mcp/operation-execution-origin.test.ts", + "test:mcp:operation-origin:pg": "node scripts/test-mcp-postgres.mjs --mcp-operation-origin", + "test:mcp:speed-original": "node --import tsx --test src/modules/mcp/speed-bandwidth-original.test.ts", + "test:mcp:speed-original:pg": "node scripts/test-mcp-postgres.mjs --mcp-speed-original", + "test:router-configuration-guard:pg": "node scripts/test-mcp-postgres.mjs --router-configuration-guard", + "test:router-configuration-writer": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/router-configuration-writer-context.test.ts src/services/router-configuration-native-transport.test.ts", + "test:scheduled-speed-original": "node --experimental-test-module-mocks --import tsx src/services/bandwidth-scheduled-speed-original.test.ts", + "test:peer-speed-workflow": "node --experimental-test-module-mocks --import tsx --test src/services/bandwidth-speed-run-identity.test.ts src/services/bandwidth-speed-workflow.test.ts", + "test:bandwidth-node-registry:pg": "node scripts/test-mcp-postgres.mjs --bandwidth-node-registry --restore", + "test:mcp:poll-terminal-claim-loss:pg": "node scripts/test-mcp-postgres.mjs --poll-terminal-archive-claim-loss --restore", + "test:mcp:poll-terminal-history-cold:pg": "node scripts/test-mcp-postgres.mjs --poll-terminal-archive-history-cold --restore", + "test:mcp:poll-terminal-history:pg": "node scripts/test-mcp-postgres.mjs --poll-terminal-archive-history --restore", + "test:mcp:poll-terminal-archive-lost-ack:pg": "node scripts/test-mcp-postgres.mjs --poll-terminal-archive-lost-ack --restore", + "test:mcp:poll-terminal-archive:pg": "node scripts/test-mcp-postgres.mjs --poll-terminal-archive --restore", + "test:mcp:poll-archive-schema:pg": "node scripts/test-mcp-postgres.mjs --poll-archive-schema", + "test:mcp:telemetry-configuration:pg": "node scripts/test-mcp-postgres.mjs --telemetry-settings-configuration", + "test:mcp:telemetry-configuration": "tsx --test src/modules/mcp/telemetry-settings-configuration.test.ts", + "test:mcp:routing-schema:pg": "node scripts/test-mcp-postgres.mjs --routing-reservation-schema", + "test:mcp:reserved-step": "tsx --test src/modules/mcp/routing-reserved-step.test.ts", + "test:mcp:routing-forward-rounds:pg": "node scripts/test-mcp-postgres.mjs --routing-network-forward-rounds", + "test:mcp:routing-original-effects:pg": "node scripts/test-mcp-postgres.mjs --routing-original-effects", + "test:flow-active-services:pg": "node scripts/test-mcp-postgres.mjs --flow-active-services", + "test:flow-active-rebuild:pg": "node scripts/test-mcp-postgres.mjs --flow-active-rebuild", + "test:flow-worker-ipc:pg": "node scripts/test-mcp-postgres.mjs --flow-worker-ipc", + "test:manual-uptime:pg": "node scripts/test-mcp-postgres.mjs --manual-uptime", + "test:manual-ping-group:pg": "node scripts/test-mcp-postgres.mjs --manual-ping-group", + "test:manual-traffic:pg": "node scripts/test-mcp-postgres.mjs --manual-traffic", + "test:manual-remaining": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/modules/mcp/manual-uptime-actions.test.ts src/modules/mcp/manual-ping-group-actions.test.ts src/modules/mcp/manual-traffic-actions.test.ts src/services/manual-data-collection-execution.test.ts src/routes/manual-data-collection.test.ts", + "test:manual-api-ping:pg": "node scripts/test-mcp-postgres.mjs --manual-api-ping", + "test:manual-api-ping": "node --experimental-test-module-mocks --import tsx --test src/modules/mcp/manual-api-ping-actions.test.ts src/services/manual-data-collection-execution.test.ts src/routes/manual-data-collection.test.ts", + "test:simple-speed:pg": "node scripts/test-mcp-postgres.mjs --simple-speed", + "test:simple-speed:chr": "node scripts/test-mcp-postgres.mjs --simple-speed-chr", + "test:simple-speed": "node --experimental-test-module-mocks --import tsx --test src/modules/mcp/speed-test-actions.test.ts src/services/uptime-speed-service.test.ts && node --import tsx --test src/routes/speed-test.test.ts src/services/speed-run-persistence.test.ts src/services/btest-node-locks.test.ts src/services/bandwidth-test-status.test.ts src/services/bandwidth-test-rates.test.ts", + "test:bandwidth-node-key-effect": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/bandwidth-node-key-effect.test.ts src/services/bandwidth-node-proof-batch.test.ts", + "test:bandwidth-node-source": "tsx src/services/bandwidth-node-registry-role.test.ts && tsx src/services/bandwidth-node-registry-catalog.test.ts", + "test:bandwidth-node-source:pg": "node scripts/test-mcp-postgres.mjs --bandwidth-node-source --restore", + "test:mcp:service-context": "tsx --test src/services/config-mcp-service-execution.test.ts src/modules/mcp/temporary-grant-origin.test.ts src/modules/mcp/routing-measurement-origin.test.ts", + "test:mcp:service-context:pg": "node scripts/test-mcp-postgres.mjs --service-execution-origin", + "test:mcp:operation-locks": "tsx --test src/services/operation-config-lock-lease.test.ts && npm run test:scheduler-automatic", + "test:mcp:operation-locks:pg": "node scripts/test-mcp-postgres.mjs --operation-locks", + "test:scheduler-automatic": "node --test src/services/scheduler-automatic-root.test.mjs src/services/scheduler-automatic-authority.test.mjs", + "test:speed-workflow": "npm run test:router-configuration-writer && npm run test:mcp:speed-original && npm run test:scheduled-speed-original && npm run test:peer-speed-workflow && npm run test:bandwidth-node-observation && npm run test:bandwidth-native-permit && tsx src/services/bandwidth-admission.test.ts && tsx src/services/btest-node-locks.test.ts && tsx src/services/mikrotik-speed-admission.test.ts && tsx src/services/bandwidth-test-status.test.ts && tsx src/services/bandwidth-test-rates.test.ts && tsx src/services/speed-run-persistence.test.ts && tsx src/routes/speed-test.test.ts && tsx src/routes/speed-probes-write.test.ts", + "test:bandwidth-native-dispatch": "node --experimental-test-module-mocks --import tsx --test --test-concurrency=1 src/services/mikrotik-managed-source.test.ts src/services/bandwidth-native-dispatch-composite.test.ts src/services/bandwidth-peer-execution-host-fence.test.ts src/services/bandwidth-native-dispatch-source.test.ts src/services/bandwidth-native-dispatch.test.ts src/services/bandwidth-native-permit.test.ts src/services/bandwidth-admission.test.ts src/services/mikrotik-speed-admission.test.ts", + "test:bandwidth-native-permit": "tsx src/services/bandwidth-native-permit.test.ts", + "test:bandwidth-native-permit:pg": "node scripts/test-mcp-postgres.mjs --bandwidth-control-source --restore", + "test:server-poll-mcp": "tsx src/services/server-poll-boundary.test.ts && tsx src/modules/mcp/server-poll-actions.test.ts && tsx src/modules/mcp/server-poll-production.test.ts && tsx src/modules/mcp/server-poll-registration.test.ts", + "test:server-poll-storage:pg": "node scripts/test-mcp-postgres.mjs --server-poll-storage --restore", + "test:server-poll-storage-fresh:pg": "node scripts/test-mcp-postgres.mjs --server-poll-storage-fresh --restore", + "test:partition-writers": "tsx src/services/partition-write-transaction.test.ts && tsx src/services/collector-partition-writes.test.ts && tsx src/db/partitions-writer.test.ts && tsx src/db/migrate-partition-admission.test.ts && tsx src/db/migration-manifest.test.ts && tsx src/db/sqlite-import-partition-admission.test.ts && tsx src/services/system-database-partition-restore.test.ts && tsx src/services/traffic-flow-write-transaction.test.ts && tsx src/services/traffic-flow-facts-rebuild-cli.test.ts", + "test:mcp:routing-authority": "tsx --test src/modules/mcp/routing-switch-ledger-contract.test.ts && tsx src/modules/mcp/routing-profile-authority.test.ts && tsx src/modules/mcp/routing-recovery-authority.test.ts && tsx src/modules/mcp/routing-reservation.test.ts && tsx src/modules/mcp/routing-community-reserved-source.test.ts && tsx src/modules/mcp/routing-network-verifier-model.test.ts && tsx --test src/test/committed-scoped-checkpoint-observer.test.ts && node --test src/modules/mcp/routing-forward-transport-charge.test.cjs", + "test:mcp:native-readers": "tsx src/modules/mcp/routing-network-verifier-model.test.ts && tsx src/modules/mcp/routing-network-native-parser.test.ts && tsx src/modules/mcp/routing-network-native-session.test.ts && tsx src/db/migration-schema-inventory.test.ts && tsx src/modules/mcp/routing-network-observation-assembly.test.ts && tsx src/modules/mcp/routing-network-configuration-bridge.test.ts && tsx --test src/services/routing-table-fib.test.ts src/services/routing-topology-observation.test.ts && node --test src/modules/mcp/routing-original-effects.test.mjs src/modules/mcp/routing-original-effects-caller.test.mjs src/modules/mcp/routing-original-effects-controller.test.mjs src/modules/mcp/routing-network-execution-origin.test.mjs", + "test:mcp:native-readers:pg": "node scripts/test-mcp-postgres.mjs --routing-network-native --restore", + "test:mcp:schema-inventory:pg": "node scripts/test-mcp-postgres.mjs --migration-schema-inventory --restore", + "test:mcp:native-tls": "tsx src/modules/mcp/routing-network-native-tls.test.ts", + "test:mcp:schema-control:pg": "node scripts/test-mcp-postgres.mjs --database-schema-control --restore", + "test:mcp:chr-acquisition": "node --test scripts/acceptance/chr-recursive-observation.test.mjs", + "test:mcp:controlled-migrations:pg": "node scripts/test-mcp-postgres.mjs --controlled-migrations --restore", + "test:mcp:shared-context": "tsx --test src/modules/mcp/routing-network-scoped-context.test.ts", + "test:mcp:controlled-reference:pg": "node scripts/test-mcp-postgres.mjs --controlled-schema-reference --restore", + "test:server-probe-storage-fresh:pg": "node scripts/test-mcp-postgres.mjs --probe-storage-fresh --restore", + "test:server-poll-durable:pg": "node scripts/test-mcp-postgres.mjs --server-poll-durable --restore", + "test:server-probe-legacy:pg": "node scripts/test-mcp-postgres.mjs --probe-legacy-orphans", + "test:server-probe-storage:pg": "node scripts/test-mcp-postgres.mjs --probe-storage-journal", + "test:server-probe-history": "tsx --test src/services/server-probe-storage-history.test.ts src/services/server-probe-retired-source.test.ts", + "test:server-probe-history:pg": "node scripts/test-mcp-postgres.mjs --probe-storage-history", + "test:server-probe-retirement:pg": "node scripts/test-mcp-postgres.mjs --probe-operation-retirement", + "test:server-probe-storage-models": "tsx src/services/server-probe-journal-maintenance.test.ts && tsx src/modules/mcp/diagnostic-cancellation-source.test.ts && tsx src/services/server-probe-storage-source.test.ts && tsx src/services/server-probe-storage-connection.test.ts && tsx src/modules/mcp/server-probe-storage-host.test.ts && tsx src/services/server-probe-storage-rest.test.ts && tsx src/services/server-probe-storage-metadata.test.ts && tsx src/services/server-probe-legacy-verifier.test.ts && tsx src/services/server-probe-legacy-snapshot.test.ts && tsx src/services/server-probe-legacy-inspection.test.ts && tsx src/db/database-source-observation.test.ts && tsx src/services/server-probe-retention.test.ts && tsx src/modules/mcp/server-probe-registration.test.ts", + "test:mcp:migration-data:pg": "node scripts/test-mcp-postgres.mjs --migration-data-inventory --restore", + "test:mcp:poll-memory:pg": "npm run build -w @mmapp/contracts && node scripts/acceptance/build-poll-memory.mjs && node scripts/test-mcp-postgres.mjs --poll-memory", + "test:mcp:poll-memory-bootstrap:pg": "npm run build -w @mmapp/contracts && node scripts/acceptance/build-poll-memory.mjs && node scripts/test-mcp-postgres.mjs --poll-memory-bootstrap", + "test:mcp:poll-memory-cost:pg": "npm run build -w @mmapp/contracts && node scripts/acceptance/build-poll-memory.mjs && node scripts/test-mcp-postgres.mjs --poll-memory-cost", + "test:mcp:poll-journal-snapshot:pg": "node scripts/test-mcp-postgres.mjs --poll-journal-snapshot", + "test:mcp:principal-security-source:pg": "node scripts/test-mcp-postgres.mjs --principal-security-source", + "test:traffic-flow:read-budget": "tsx --test src/services/traffic-flow-read-budget.test.ts src/modules/mcp/flow-read-quality.test.ts && node scripts/test-flow-read-adapters.mjs", + "test:traffic-flow:read-http:pg": "node scripts/test-flow-read-http-owned.mjs", + "test:traffic-flow:read-source:pg": "tsx scripts/test-flow-read-sql-owned.mts", + "test:mcp:routing-baseline": "tsx --test src/modules/mcp/routing-controller-active-writer.test.ts src/modules/mcp/routing-controller-baseline.test.ts src/modules/mcp/routing-controller-fence.test.ts src/modules/mcp/routing-community-public.test.ts src/modules/mcp/routing-controller-overlap.test.ts", + "test:mcp:routing-baseline:pg": "node scripts/test-routing-controller-baseline.mjs", + "test:mcp:routing-baseline-inverse:pg": "node scripts/test-routing-controller-baseline.mjs --default-scoped", + "test:sqlite-import-control:pg": "node scripts/test-sqlite-import-control-postgres.mjs", + "test:traffic-flow:resource-profile": "tsx --test src/services/traffic-flow-read-resource-profile.test.ts && node scripts/test-flow-read-profile-startup.mjs", + "test:traffic-flow:settings-read-profiles:pg": "node scripts/test-flow-settings-read-profiles-owned.mjs", + "test:database-source-binding": "node scripts/test-database-source-binding.mjs" }, "dependencies": { "@aws-sdk/client-s3": "^3.888.0", @@ -50,6 +192,10 @@ "@fastify/jwt": "^10.2.2", "@fastify/type-provider-zod": "^1.0.0", "@mmapp/contracts": "1.0.0", + "@modelcontextprotocol/client": "2.3.1", + "@modelcontextprotocol/fastify": "2.0.1", + "@modelcontextprotocol/node": "2.1.1", + "@modelcontextprotocol/server": "2.3.1", "acme-client": "^5.4.0", "better-sqlite3": "^12.9.0", "dotenv": "^16.4.7", @@ -66,6 +212,7 @@ "@types/node": "^22.15.3", "@types/pg": "^8.23.1", "drizzle-kit": "^0.31.10", + "embedded-postgres": "18.4.0-beta.17", "jose": "^6.2.11", "pino-pretty": "^13.1.3", "tsx": "^4.19.3", diff --git a/backend/scripts/acceptance/build-chr-netflow-collector-smoke.mjs b/backend/scripts/acceptance/build-chr-netflow-collector-smoke.mjs new file mode 100644 index 00000000..b4ccefe9 --- /dev/null +++ b/backend/scripts/acceptance/build-chr-netflow-collector-smoke.mjs @@ -0,0 +1,72 @@ +import { createHash } from 'node:crypto'; +import { execFileSync } from 'node:child_process'; +import { cpSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const root = fileURLToPath(new URL('../../../', import.meta.url)); +const baseRevision = 'bf3d50e1ff2ee3b5c9268991a255c3b49ce6378e'; +const sha = bytes => createHash('sha256').update(bytes).digest('hex'); +const require = (value, reason) => { if (!value) throw Error(reason); }; +const git = (...args) => execFileSync('git', args, { cwd: root, stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 4 * 1024 * 1024 }); +const fromRoot = filename => path.join(root, filename); +const readJson = bytes => JSON.parse(bytes.toString('utf8')); +const manifestPath = ['backend/package.json', 'packages/contracts/package.json']; +const runtimeKeys = ['type', 'exports', 'engines', 'dependencies', 'optionalDependencies', 'peerDependencies']; +const normalizeLock = bytes => bytes.toString('utf8').replace(/\r\n/g, '\n'); +const sort = values => [...new Set(values)].sort(); + +function runtimePins() { + const lockFile = readFileSync(fromRoot('package-lock.json')); + require(normalizeLock(lockFile) === normalizeLock(git('show', baseRevision + ':package-lock.json')), 'Runtime lock differs from cached source'); + const lock = readJson(lockFile).packages; + return manifestPath.flatMap(filename => { + const current = readJson(readFileSync(fromRoot(filename))); + const cached = readJson(git('show', baseRevision + ':' + filename)); + for (const key of runtimeKeys) { + require(JSON.stringify(current[key]) === JSON.stringify(cached[key]), 'Runtime package manifest differs from cached source'); + } + const workspace = path.posix.dirname(filename); + return sort(Object.keys(current.dependencies ?? {})).filter(name => name !== '@mmapp/contracts').map(name => { + const nested = workspace + '/node_modules/' + name, shared = 'node_modules/' + name; + const installed = lock[nested] ? nested : shared; + const version = lock[installed]?.version; + require(typeof version === 'string', 'Runtime dependency pin unavailable'); + return { path: installed + '/package.json', name, version }; + }); + }).filter((item, index, all) => all.findIndex(other => other.path === item.path) === index); +} + +function compile(project, outDir) { + execFileSync(process.execPath, [fromRoot('node_modules/typescript/bin/tsc'), '--project', fromRoot(project), '--outDir', outDir, + '--declaration', 'false', '--declarationMap', 'false', '--sourceMap', 'false', '--incremental', 'false'], + { cwd: root, stdio: 'inherit' }); +} + +function files(directory, prefix = '') { + return readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)).flatMap(entry => { + const key = prefix + entry.name, item = path.join(directory, entry.name); + if (entry.isDirectory()) return files(item, key + '/'); + require(entry.isFile(), 'Packet contains a nonregular entry'); + const bytes = readFileSync(item); + return [{ path: key, size: bytes.length, sha256: sha(bytes) }]; + }); +} + +const [runId, ...extra] = process.argv.slice(2); +require(/^[a-f0-9]{8}$/.test(runId ?? '') && !extra.length, 'Expected one eight-digit hexadecimal run ID'); +const directory = fromRoot('tmp/mcp-netflow-smoke-packet-' + runId); +const dependencies = runtimePins(); +mkdirSync(directory); +compile('packages/contracts/tsconfig.json', path.join(directory, 'contracts-dist')); +compile('backend/tsconfig.json', path.join(directory, 'dist')); +require(statSync(path.join(directory, 'dist/test/traffic-flow-chr-collector-bootstrap.js')).isFile(), 'Compiled collector entry unavailable'); +cpSync(fromRoot('backend/drizzle'), path.join(directory, 'drizzle'), { recursive: true, errorOnExist: true }); +const entries = files(directory); +require(entries.length > 0 && entries.length <= 4096 && entries.reduce((sum, entry) => sum + entry.size, 0) <= 128 * 1024 * 1024, + 'Packet budget exceeded'); +const body = Buffer.from(JSON.stringify({ format: 'mmapp-netflow-collector-smoke-v1', cachedSourceRevision: baseRevision, dependencies, + files: entries }, null, 2) + '\n'); +writeFileSync(path.join(directory, 'manifest.json'), body, { flag: 'wx' }); +console.log(JSON.stringify({ directory, manifestSha256: sha(body), files: entries.length, dependencies: dependencies.length, + collectorStarted: false, networkUsed: false })); diff --git a/backend/scripts/acceptance/build-installation-memory.mjs b/backend/scripts/acceptance/build-installation-memory.mjs new file mode 100644 index 00000000..5fba363a --- /dev/null +++ b/backend/scripts/acceptance/build-installation-memory.mjs @@ -0,0 +1,22 @@ +/** Separate output: never replace an in-flight or accepted poll-memory tree. */ +import assert from 'node:assert/strict' +import { mkdir, lstat } from 'node:fs/promises' +import { dirname, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import ts from 'typescript' +assert(process.argv.length===2||process.argv.length===3,'Supply at most one new output name') +const outputName=process.argv[2]??'dist-installation-memory' +assert(/^dist-installation-memory(?:-[a-z0-9-]{3,64})?$/.test(outputName),'Invalid compiled output name') +const backend=resolve(dirname(fileURLToPath(import.meta.url)),'../..'), output=resolve(backend,outputName) +const existing=await lstat(output).catch(error=>{if(error.code!=='ENOENT')throw error}) +assert(!existing,'Use a new output directory; no implicit cleanup or concurrent rebuild') +await mkdir(output) +const configPath=resolve(backend,'tsconfig.json'),config=ts.readConfigFile(configPath,ts.sys.readFile) +assert(!config.error) +const parsed=ts.parseJsonConfigFileContent(config.config,ts.sys,backend,undefined,configPath) +const options={...parsed.options,rootDir:resolve(backend,'src'),outDir:output,noEmit:false,noEmitOnError:true,declaration:false,declarationMap:false,sourceMap:false,incremental:false,composite:false,tsBuildInfoFile:undefined} +const roots=['src/services/installation-co-load.owned.test.ts','src/services/traffic-flow-collector-worker.ts'].map(file=>resolve(backend,file)) +const program=ts.createProgram({rootNames:roots,options}),diagnostics=ts.getPreEmitDiagnostics(program) +if(diagnostics.length)throw Error(ts.formatDiagnosticsWithColorAndContext(diagnostics,{getCurrentDirectory:()=>backend,getCanonicalFileName:file=>file,getNewLine:()=> '\n'})) +assert(!program.emit().emitSkipped) +console.log(JSON.stringify({output,rootEntries:roots.length,actualWorkerIncluded:true,productionModulesSubstituted:false})) diff --git a/backend/scripts/acceptance/build-poll-memory.mjs b/backend/scripts/acceptance/build-poll-memory.mjs new file mode 100644 index 00000000..6a837d2f --- /dev/null +++ b/backend/scripts/acceptance/build-poll-memory.mjs @@ -0,0 +1,36 @@ +/** Emit one owned memory entry plus its actual production import tree. The + * measured child runs plain node, not the TypeScript compiler or tsx. */ +import assert from 'node:assert/strict' +import { lstat, rm } from 'node:fs/promises' +import { dirname, basename, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import ts from 'typescript' + +const backend = resolve(dirname(fileURLToPath(import.meta.url)), '../..') +const output = resolve(backend, 'dist-mcp-memory') +assert.equal(dirname(output), backend) +assert.equal(basename(output), 'dist-mcp-memory') +const existing = await lstat(output).catch(error => { if (error.code !== 'ENOENT') throw error }) +assert(!existing?.isSymbolicLink(), 'Refuse to clean a redirected output directory') +if (existing) await rm(output, { recursive: true }) +const configPath = resolve(backend, 'tsconfig.json') +const config = ts.readConfigFile(configPath, ts.sys.readFile) +const format = diagnostics => ts.formatDiagnosticsWithColorAndContext(diagnostics, { + getCurrentDirectory: () => backend, getCanonicalFileName: name => name, getNewLine: () => '\n', +}) +if (config.error) throw Error(format([config.error])) +const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, backend, undefined, configPath) +if (parsed.errors.length) throw Error(format(parsed.errors)) +const entry = resolve(backend, 'src/services/server-poll-memory.owned.test.ts') +const options = { ...parsed.options, rootDir: resolve(backend, 'src'), outDir: output, + noEmit: false, noEmitOnError: true, declaration: false, declarationMap: false, sourceMap: false, + incremental: false, composite: false, tsBuildInfoFile: undefined } +// Deliberately override tsconfig include/exclude with this single test root. +// TypeScript still follows all normal imports; no module/path/IO substitutes. +const program = ts.createProgram({ rootNames: [entry], options }) +const diagnostics = ts.getPreEmitDiagnostics(program) +if (diagnostics.length) throw Error(format(diagnostics)) +const emitted = program.emit() +if (emitted.emitSkipped || emitted.diagnostics.length) throw Error(format(emitted.diagnostics) || 'Memory entry emit skipped') +console.log(JSON.stringify({ compiledPollMemoryEntry: resolve(output, 'services/server-poll-memory.owned.test.js'), + outputDirectory: output, rootEntries: 1, noEmitOnError: true, measuredChildUsesTsx: false })) diff --git a/backend/scripts/acceptance/chr-netflow-collector-container.py b/backend/scripts/acceptance/chr-netflow-collector-container.py new file mode 100644 index 00000000..97a26206 --- /dev/null +++ b/backend/scripts/acceptance/chr-netflow-collector-container.py @@ -0,0 +1,325 @@ +"""Fixed, opt-in local Docker owner for the disposable NetFlow collector.""" +import argparse +import json +import os +import re +import secrets +import stat +import subprocess +import sys +from pathlib import Path + +PG_IMAGE = 'postgres@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873' +NODE_IMAGE = 'git.shx.one/denozord/mikrotikmanager-backend-mcp-acceptance@sha256:151ab635caca5f2f6b50a9d0c27980a1559d7bd67f0e3a8d2b74cfa3f9ec6945' +CID = re.compile(r'[a-f0-9]{64}\Z') +RUN = re.compile(r'[a-f0-9]{8}\Z') +LABEL = 'io.mmapp.netflow.run' +ENV = {'PATH': '/usr/sbin:/usr/bin:/sbin:/bin', 'LC_ALL': 'C', + 'HOME': '/nonexistent', 'DOCKER_CONFIG': '/nonexistent'} + + +def require(value, code): + if not value: + raise ValueError(code) + + +def root(run): + require(isinstance(run, str) and RUN.fullmatch(run), 'RUN_ID_REFUSED') + return '/tmp/mcp-netflow-' + run + + +def mounts(source, target, readonly=False): + return ['--mount', 'type=bind,src=' + source + ',dst=' + target + (',readonly' if readonly else '')] + + +def build_pg(run, uid, gid): + base = root(run) + require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED') + return [ + 'create', '--pull=never', '--name', 'mm-nf-pg-' + run, + '--label', LABEL + '=' + run, '--label', 'io.mmapp.netflow.role=postgres', + '--network=none', '--user', f'{uid}:{gid}', '--read-only', + '--cap-drop=ALL', '--security-opt=no-new-privileges', + '--memory=192m', '--memory-swap=192m', + '--tmpfs', '/tmp:rw,nosuid,nodev,noexec,mode=1777,size=32m', + '--tmpfs', '/var/run/postgresql:rw,nosuid,nodev,noexec,mode=1777,size=1m', + *mounts(base + '/pg', '/owned-pg'), + *mounts(base + '/pg-socket', base + '/pg-socket'), + *mounts(base + '/pg-password', '/run/mm-netflow-pg-secret', True), + '--env', 'PGDATA=/owned-pg/data', '--env', 'POSTGRES_USER=mcp_test', + '--env', 'POSTGRES_DB=mcp_netflow_' + run, + '--env', 'POSTGRES_PASSWORD_FILE=/run/mm-netflow-pg-secret', + '--env', 'POSTGRES_INITDB_ARGS=--auth-local=scram-sha-256 --auth-host=scram-sha-256', + PG_IMAGE, 'postgres', '-c', 'listen_addresses=', '-c', 'unix_socket_directories=' + base + '/pg-socket', + '-c', 'unix_socket_permissions=0700', + ] + + +def build_node(run, uid, gid, anchor_id, port, namespace, host_namespace, packet_digest): + base = root(run) + require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED') + require(isinstance(anchor_id, str) and CID.fullmatch(anchor_id), 'ANCHOR_ID_REFUSED') + require(isinstance(packet_digest, str) and CID.fullmatch(packet_digest), 'PACKET_PIN_REFUSED') + require(type(port) is int and 49152 <= port <= 65535, 'COLLECTOR_PORT_REFUSED') + require(all(type(pair) in (tuple, list) and len(pair) == 2 and + all(type(value) is int and 0 < value < 2**64 for value in pair) + for pair in (namespace, host_namespace)) and list(namespace) != list(host_namespace), 'NAMESPACE_PINS_REFUSED') + return [ + 'create', '--pull=never', '--name', 'mm-nf-node-' + run, + '--label', LABEL + '=' + run, '--label', 'io.mmapp.netflow.role=collector', + '--network=container:' + anchor_id, '--user', f'{uid}:{gid}', '--read-only', + '--cap-drop=ALL', '--security-opt=no-new-privileges', '--interactive', '--tty', + '--memory=320m', '--memory-swap=320m', + '--tmpfs', '/tmp:rw,nosuid,nodev,noexec,mode=1777,size=32m', + *mounts(base + '/collector', base), + *mounts(base + '/pg-socket', base + '/pg-socket'), + *mounts(base + '/pg-password', '/run/mm-netflow-pg-secret', True), + *mounts(base + '/packet/dist', '/app/backend/dist', True), + *mounts(base + '/packet/drizzle', '/app/backend/drizzle', True), + *mounts(base + '/packet/contracts-dist', '/app/packages/contracts/dist', True), + *mounts(base + '/packet/manifest.json', '/run/mm-netflow-packet.json', True), + '--workdir', base, '--env', 'MMAPP_OWN_CHR_NETFLOW_COLLECTOR=true', + '--env', 'MMAPP_NETFLOW_PACKET_SHA256=' + packet_digest, + '--entrypoint', 'node', NODE_IMAGE, + '--max-old-space-size=160', '--max-semi-space-size=8', + '/app/backend/dist/test/traffic-flow-chr-collector-bootstrap.js', + '--container-collector-cycle', run, str(port), *map(str, namespace), *map(str, host_namespace), + ] + + +def capture_args(run, first, second, port): + root(run) + require((first, second) == ('198.18.241.2', '198.18.241.9') and + type(port) is int and 49152 <= port <= 65535, 'CAPTURE_SCOPE_REFUSED') + return ['nsenter', '--net=/run/netns/nf' + run + 'nc', '--', + 'tcpdump', '-n', '-p', '-U', '-i', 'nf' + run + 'cv', '-y', 'EN10MB', '-s', '4138', + '-c', '64', '-w', '-', 'udp and dst host 198.18.242.3 and dst port ' + str(port) + + ' and (src host ' + first + ' or src host ' + second + ')'] + + +def local_docker(): + require(sys.platform == 'linux' and os.geteuid() == 0 and + os.environ.get('MMAPP_OWN_CHR_NETFLOW_CONTAINER') == 'true', 'DOCKER_OWNER_REFUSED') + socket = Path('/var/run/docker.sock') + require(stat.S_ISSOCK(socket.stat().st_mode), 'LOCAL_DOCKER_SOCKET_UNAVAILABLE') + choices = [Path(folder, 'docker') for folder in ('/usr/bin', '/usr/sbin', '/bin')] + binary = next((path.resolve(strict=True) for path in choices if path.is_file()), None) + require(binary is not None and binary.stat().st_uid == 0 and not binary.stat().st_mode & 0o022, + 'DOCKER_BINARY_UNPINNED') + + def call(*args): + result = subprocess.run([str(binary), '--host', 'unix:///var/run/docker.sock', *args], + env=ENV, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, timeout=30) + require(result.returncode == 0 and len(result.stdout) <= 256 * 1024, 'DOCKER_OUTCOME_UNCONFIRMED') + return result.stdout.decode('utf-8', 'strict').strip() + + def attach(cid): + require(CID.fullmatch(cid) and sys.stdin.isatty() and sys.stdout.isatty(), 'EXACT_TTY_REFUSED') + return subprocess.run([str(binary), '--host', 'unix:///var/run/docker.sock', 'start', '--attach', '--interactive', cid], + env=ENV, timeout=330).returncode + + return call, attach + + +def prepare_private_storage(run, uid, gid): + """Create only new private bind sources. A retained fixture is never reused.""" + require(sys.platform == 'linux' and os.geteuid() == 0 and + os.environ.get('MMAPP_OWN_CHR_NETFLOW_CONTAINER') == 'true', 'DOCKER_OWNER_REFUSED') + require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED') + base = Path(root(run)) + info = base.lstat() + require(base.resolve(strict=True) == base and stat.S_ISDIR(info.st_mode) and info.st_uid == uid and + stat.S_IMODE(info.st_mode) == 0o700, 'FIXTURE_ROOT_REFUSED') + for name in ('pg', 'pg-socket', 'collector'): + directory = base / name + directory.mkdir(mode=0o700) + os.chown(directory, uid, gid, follow_symlinks=False) + fd = os.open(base / 'pg-password', os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o400) + with os.fdopen(fd, 'wb') as output: + output.write((secrets.token_hex(32) + '\n').encode('ascii')) + output.flush() + os.fsync(output.fileno()) + os.fchown(output.fileno(), uid, gid) + + +class CollectorContainerOwner: + """Outer Linux parent retains each acknowledged ID and journals it. + + An unknown create/start/retire acknowledgement requires reconciliation. + It never searches by label, restarts, prunes, or removes a matched name. + """ + def __init__(self, run, docker, checkpoint): + root(run) + self.run, self.docker, self.checkpoint = run, docker, checkpoint + self.ids = {} + self.attempted = set() + self.verified_exits = {} + self.anchor_start = None + self.adopted = None + self.topology_retired = False + + def image(self, reference): + rows = json.loads(self.docker('image', 'inspect', reference)) + require(len(rows) == 1 and reference in rows[0]['RepoDigests'] and rows[0]['Os'] == 'linux' and + re.fullmatch(r'sha256:[a-f0-9]{64}', rows[0]['Id']), 'CACHED_IMAGE_UNPINNED') + return rows[0]['Id'] + + def inspect(self, cid, role, image_id, network): + require(CID.fullmatch(cid), 'CONTAINER_ID_UNPINNED') + rows = json.loads(self.docker('container', 'inspect', cid)) + require(len(rows) == 1, 'CONTAINER_UNCONFIRMED') + row = rows[0] + labels = row['Config']['Labels'] + memory = (192 if role == 'postgres' else 320) * 1024 * 1024 + require(row['Id'] == cid and row['Image'] == image_id and labels[LABEL] == self.run and + labels['io.mmapp.netflow.role'] == role and row['HostConfig']['NetworkMode'] == network and + not row['HostConfig']['Privileged'] and not row['HostConfig']['PortBindings'] and + row['HostConfig']['ReadonlyRootfs'] is True and row['HostConfig']['CapDrop'] == ['ALL'] and + not row['HostConfig']['CapAdd'] and row['HostConfig']['Memory'] == memory and + row['HostConfig']['MemorySwap'] == memory, 'CONTAINER_SCOPE_DRIFT') + return row['State'] + + def _create(self, role, argv, reference, network): + require(role not in self.attempted, 'CONTAINER_REPLAY_REFUSED') + self.attempted.add(role) + image_id = self.image(reference) + self.checkpoint(role, 'create-intent', None) + cid = self.docker(*argv) + require(CID.fullmatch(cid), 'CREATE_ACK_UNCONFIRMED') + self.checkpoint(role, 'create-acknowledged', cid) + state = self.inspect(cid, role, image_id, network) + require(state['Status'] == 'created' and state['Running'] is False, 'CREATED_STATE_UNCONFIRMED') + self.ids[role] = (cid, image_id, network) + self.checkpoint(role, 'create-verified', cid) + return cid + + def create_anchor(self, uid, gid): + return self._create('postgres', build_pg(self.run, uid, gid), PG_IMAGE, 'none') + + def create_collector(self, uid, gid, port, namespace, host_namespace, packet_digest, assert_packet): + anchor_id, image_id, network = self.ids['postgres'] + anchor = self.inspect(anchor_id, 'postgres', image_id, network) + require(anchor['Running'] is True and anchor['Pid'] > 0, 'ANCHOR_PID_UNCONFIRMED') + require(self.adopted is not None, 'C_ADOPTION_UNCONFIRMED') + adopted, read_mount = self.adopted + require(adopted == self.anchor_namespace() and adopted['namespace'] == list(namespace) and + adopted['hostNamespace'] == list(host_namespace) and read_mount() == adopted['namespace'], + 'C_ADOPTION_DRIFT') + assert_packet() + return self._create('collector', build_node(self.run, uid, gid, anchor_id, port, namespace, host_namespace, packet_digest), + NODE_IMAGE, 'container:' + anchor_id) + + def start_anchor(self): + cid, image_id, network = self.ids['postgres'] + require('postgres-start' not in self.attempted, 'CONTAINER_REPLAY_REFUSED') + self.attempted.add('postgres-start') + self.checkpoint('postgres', 'start-intent', cid) + require(self.docker('start', cid) == cid, 'START_ACK_UNCONFIRMED') + state = self.inspect(cid, 'postgres', image_id, network) + require(state['Running'] is True and type(state['Pid']) is int and state['Pid'] > 0 and + re.fullmatch(r'\d{4}-\d{2}-\d{2}T[0-9:.]+Z', state['StartedAt']), 'ANCHOR_PID_UNCONFIRMED') + self.checkpoint('postgres', 'start-verified', cid) + self.anchor_start = (state['Pid'], state['StartedAt']) + return state['Pid'] + + def anchor_namespace(self): + """Return live anchor and host netns pins for the topology owner.""" + require(sys.platform == 'linux' and os.geteuid() == 0, 'LINUX_OWNER_REQUIRED') + cid, image_id, network = self.ids['postgres'] + state = self.inspect(cid, 'postgres', image_id, network) + pid = state['Pid'] + started_at = state['StartedAt'] + require(state['Running'] is True and self.anchor_start == (pid, started_at), 'ANCHOR_PID_UNCONFIRMED') + pin = Path('/proc', str(pid), 'ns/net').stat() + host = Path('/proc/self/ns/net').stat() + namespace, host_namespace = [pin.st_dev, pin.st_ino], [host.st_dev, host.st_ino] + latest = self.inspect(cid, 'postgres', image_id, network) + require(latest['Running'] is True and latest['Pid'] == pid and latest['StartedAt'] == started_at and + namespace != host_namespace, + 'ANCHOR_NAMESPACE_DRIFT') + return {'containerId': cid, 'pid': pid, 'startedAt': started_at, + 'namespace': namespace, 'hostNamespace': host_namespace} + + def create_linux_topology(self, executor, backend, journal, planned, pins): + """Bridge the exact live anchor into the existing guarded Linux owner.""" + name = 'nf' + self.run + 'nc' + require(pins['nonce'][:8] == self.run and planned['names']['C'] == name, 'TOPOLOGY_RUN_DIFFERS') + require('topology-adopt' not in self.attempted, 'C_ADOPTION_REPLAY_REFUSED') + self.attempted.add('topology-adopt') + result = executor(backend, journal, planned, pins, read_collector_anchor=self.anchor_namespace) + adopted = journal.value['collectorAnchor'] + require(adopted == self.anchor_namespace() and backend.namespace(name) == adopted['namespace'], + 'C_ADOPTION_DRIFT') + self.adopted = (adopted, lambda: backend.namespace(name)) + return result + + def assert_linux_anchor_exited(self, recorded): + """Validate the journalled container and its exact exit before C cleanup.""" + cid, image_id, network = self.ids['postgres'] + require(recorded['containerId'] == cid and 'postgres' in self.verified_exits and + ('collector' not in self.ids or 'collector' in self.verified_exits), 'ANCHOR_EXIT_NOT_OWNED') + state = self.inspect(cid, 'postgres', image_id, network) + require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and + state['StartedAt'] == recorded['startedAt'] == self.verified_exits['postgres'], 'ANCHOR_EXIT_UNCONFIRMED') + + def cleanup_linux_topology(self, executor, backend, journal, planned): + require(self.adopted is not None and self.adopted[0] == journal.value.get('collectorAnchor') and + backend.root_baseline() == journal.value.get('rootBaseline'), 'TOPOLOGY_CLEANUP_SCOPE_DRIFT') + executor(backend, journal, planned, assert_collector_anchor_exited=self.assert_linux_anchor_exited) + require(journal.value['state'] == 'retired' and backend.root_baseline() == journal.value['rootBaseline'], + 'TOPOLOGY_CLEANUP_UNCONFIRMED') + self.topology_retired = True + + def start_collector(self, attach): + cid, image_id, network = self.ids['collector'] + require('collector-start' not in self.attempted, 'CONTAINER_REPLAY_REFUSED') + require(self.adopted is not None and self.adopted[0] == self.anchor_namespace() and + self.adopted[1]() == self.adopted[0]['namespace'], 'C_ADOPTION_DRIFT') + self.attempted.add('collector-start') + self.checkpoint('collector', 'start-intent', cid) + status = attach(cid) + state = self.inspect(cid, 'collector', image_id, network) + require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and + state['ExitCode'] == status and re.fullmatch(r'\d{4}-\d{2}-\d{2}T[0-9:.]+Z', state['StartedAt']), + 'COLLECTOR_EXIT_UNCONFIRMED') + self.checkpoint('collector', 'exit-verified', cid) + self.verified_exits['collector'] = state['StartedAt'] + require(status == 0, 'COLLECTOR_RUN_REFUSED') + + def stop_anchor(self, assert_restored): + cid, image_id, network = self.ids['postgres'] + require('postgres-stop' not in self.attempted, 'CONTAINER_REPLAY_REFUSED') + require('collector' not in self.ids or 'collector' in self.verified_exits, 'COLLECTOR_EXIT_UNCONFIRMED') + self.attempted.add('postgres-stop') + assert_restored() + self.checkpoint('postgres', 'stop-intent', cid) + current = self.inspect(cid, 'postgres', image_id, network) + require(current['Running'] is True and self.anchor_start == (current['Pid'], current['StartedAt']), 'ANCHOR_START_DRIFT') + require(self.docker('stop', '--time', '15', cid) == cid, 'STOP_ACK_UNCONFIRMED') + state = self.inspect(cid, 'postgres', image_id, network) + require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and + state['StartedAt'] == self.anchor_start[1] and state['ExitCode'] == 0, 'ANCHOR_EXIT_UNCONFIRMED') + self.checkpoint('postgres', 'exit-verified', cid) + self.verified_exits['postgres'] = state['StartedAt'] + + def retire_exited(self, role): + cid, image_id, network = self.ids[role] + operation = role + '-retire' + require(operation not in self.attempted, 'CONTAINER_REPLAY_REFUSED') + require(role != 'postgres' or self.adopted is None or self.topology_retired, 'C_RETIREMENT_UNCONFIRMED') + self.attempted.add(operation) + state = self.inspect(cid, role, image_id, network) + require(role in self.verified_exits and state['Running'] is False and state['Status'] == 'exited' and + state['Pid'] == 0 and state['StartedAt'] == self.verified_exits[role], 'EXIT_NOT_CONFIRMED') + self.checkpoint(role, 'retire-intent', cid) + require(self.docker('rm', cid) == cid, 'RETIRE_ACK_UNCONFIRMED') + self.checkpoint(role, 'retire-acknowledged', cid) + + +if __name__ == '__main__': + cli = argparse.ArgumentParser() + cli.add_argument('--check-only', required=True, metavar='RUN_ID') + args = cli.parse_args() + print(json.dumps({'fixtureRoot': root(args.check_only), 'postgresImage': PG_IMAGE, 'nodeImage': NODE_IMAGE, + 'requiresOwnedCollectorNamespace': True, 'dockerUsed': False, 'networkUsed': False})) diff --git a/backend/scripts/acceptance/chr-netflow-collector-container.test.py b/backend/scripts/acceptance/chr-netflow-collector-container.test.py new file mode 100644 index 00000000..7dea37fe --- /dev/null +++ b/backend/scripts/acceptance/chr-netflow-collector-container.test.py @@ -0,0 +1,143 @@ +import json +import runpy +import unittest +from pathlib import Path + +owner = runpy.run_path(str(Path(__file__).with_name('chr-netflow-collector-container.py'))) +RUN = 'a012bcde' +PG_ID = 'a' * 64 +NODE_ID = 'b' * 64 +PACKET = 'd' * 64 +IMAGE_ID = 'sha256:' + 'c' * 64 +STARTED = '2026-11-04T12:00:00.000Z' + + +class DockerFixture: + def __init__(self): + self.calls = [] + self.records = {} + self.lose_create = False + + def __call__(self, *args): + self.calls.append(args) + if args[:2] == ('image', 'inspect'): + return json.dumps([{'Id': IMAGE_ID, 'Os': 'linux', 'RepoDigests': [args[2]]}]) + if args[0] == 'create': + if self.lose_create: + raise RuntimeError('lost ACK') + pg = '--network=none' in args + role, cid = ('postgres', PG_ID) if pg else ('collector', NODE_ID) + self.records[cid] = {'Id': cid, 'Image': IMAGE_ID, 'Config': {'Labels': { + owner['LABEL']: RUN, 'io.mmapp.netflow.role': role}}, + 'HostConfig': {'NetworkMode': 'none' if pg else 'container:' + PG_ID, 'Privileged': False, + 'PortBindings': None, 'ReadonlyRootfs': True, 'CapDrop': ['ALL'], 'CapAdd': None, + 'Memory': (192 if pg else 320) * 1024 * 1024, + 'MemorySwap': (192 if pg else 320) * 1024 * 1024}, + 'State': {'Running': False, 'Status': 'created', 'Pid': 0}} + return cid + cid = args[-1] + if args[:2] == ('container', 'inspect'): + return json.dumps([self.records[cid]]) + if args[0] == 'start': + self.records[cid]['State'] = {'Running': True, 'Status': 'running', 'Pid': 42, 'StartedAt': STARTED} + elif args[0] == 'stop': + self.records[cid]['State'] = {'Running': False, 'Status': 'exited', 'Pid': 0, 'StartedAt': STARTED, 'ExitCode': 0} + elif args[0] == 'rm': + del self.records[cid] + else: + raise AssertionError(args) + return cid + + +class TestCollectorLauncher(unittest.TestCase): + def test_exact_images_no_pull_no_privilege_and_fixed_namespace(self): + pg = owner['build_pg'](RUN, 1000, 1000) + node = owner['build_node'](RUN, 1000, 1000, PG_ID, 50100, (4, 5), (4, 6), PACKET) + self.assertIn('--network=none', pg) + self.assertIn('--network=container:' + PG_ID, node) + self.assertIn('listen_addresses=', pg) + for argv in (pg, node): + self.assertIn('--pull=never', argv) + self.assertIn('--cap-drop=ALL', argv) + self.assertNotIn('--privileged', argv) + self.assertNotIn('/var/run/docker.sock', ' '.join(argv)) + self.assertNotIn('POSTGRES_PASSWORD=', ' '.join(pg)) + with self.assertRaisesRegex(ValueError, 'NAMESPACE_PINS'): + owner['build_node'](RUN, 1000, 1000, PG_ID, 50100, (4, 5), (4, 5), PACKET) + self.assertIn('EN10MB', owner['capture_args'](RUN, '198.18.241.2', '198.18.241.9', 50100)) + + def test_one_shot_ack_exact_ids_and_verified_cleanup(self): + docker, journal = DockerFixture(), [] + launcher = owner['CollectorContainerOwner'](RUN, docker, lambda *entry: journal.append(entry)) + self.assertEqual(launcher.create_anchor(1000, 1000), PG_ID) + self.assertEqual(launcher.start_anchor(), 42) + with self.assertRaisesRegex(ValueError, 'ADOPTION_UNCONFIRMED'): + launcher.create_collector(1000, 1000, 50100, (4, 5), (4, 6), PACKET, lambda: None) + anchor = {'containerId': PG_ID, 'pid': 42, 'startedAt': STARTED, 'namespace': [4, 5], 'hostNamespace': [4, 6]} + launcher.anchor_namespace = lambda: anchor + name = 'nf' + RUN + 'nc' + + class Topology: + def namespace(self, target): + return [4, 5] if target == name else None + + def root_baseline(self): + return 'root-baseline' + + class Journal: + value = {} + + def provision(backend, journal, planned, pins, read_collector_anchor): + journal.value.update(collectorAnchor=read_collector_anchor(), rootBaseline='root-baseline', state='created') + return {'ns:' + name: backend.namespace(name)} + + def cleanup(backend, journal, planned, assert_collector_anchor_exited): + assert_collector_anchor_exited(journal.value['collectorAnchor']) + journal.value['state'] = 'retired' + + topology, topology_journal, planned = Topology(), Journal(), {'names': {'C': name}} + launcher.create_linux_topology(provision, topology, topology_journal, planned, {'nonce': RUN + '0' * 24}) + self.assertEqual(launcher.create_collector(1000, 1000, 50100, (4, 5), (4, 6), PACKET, lambda: None), NODE_ID) + with self.assertRaisesRegex(ValueError, 'EXIT_NOT_OWNED'): + launcher.cleanup_linux_topology(cleanup, topology, topology_journal, planned) + + def attach(cid): + self.assertEqual(cid, NODE_ID) + docker.records[cid]['State'] = {'Running': False, 'Status': 'exited', 'Pid': 0, 'ExitCode': 0, 'StartedAt': STARTED} + return 0 + + launcher.start_collector(attach) + launcher.retire_exited('collector') + launcher.stop_anchor(lambda: None) + with self.assertRaisesRegex(ValueError, 'C_RETIREMENT_UNCONFIRMED'): + launcher.retire_exited('postgres') + launcher.cleanup_linux_topology(cleanup, topology, topology_journal, planned) + launcher.retire_exited('postgres') + self.assertEqual(docker.records, {}) + self.assertIn(('collector', 'exit-verified', NODE_ID), journal) + self.assertEqual([call for call in docker.calls if call[0] == 'rm'], [('rm', NODE_ID), ('rm', PG_ID)]) + + def test_lost_create_ack_is_not_replayed_or_cleaned_by_name(self): + docker, journal = DockerFixture(), [] + docker.lose_create = True + launcher = owner['CollectorContainerOwner'](RUN, docker, lambda *entry: journal.append(entry)) + with self.assertRaisesRegex(RuntimeError, 'lost ACK'): + launcher.create_anchor(1000, 1000) + with self.assertRaisesRegex(ValueError, 'REPLAY'): + launcher.create_anchor(1000, 1000) + self.assertEqual(journal, [('postgres', 'create-intent', None)]) + self.assertEqual([call[0] for call in docker.calls], ['image', 'create']) + + def test_anchor_process_replaced_under_same_container_id_is_never_stopped(self): + docker = DockerFixture() + launcher = owner['CollectorContainerOwner'](RUN, docker, lambda *_: None) + launcher.create_anchor(1000, 1000) + launcher.start_anchor() + docker.records[PG_ID]['State']['StartedAt'] = '2026-11-04T13:00:00.000Z' + with self.assertRaisesRegex(ValueError, 'ANCHOR_START_DRIFT'): + launcher.stop_anchor(lambda: None) + self.assertFalse(any(call[0] == 'stop' for call in docker.calls)) + + +if __name__ == '__main__': + unittest.main() diff --git a/backend/scripts/acceptance/chr-netflow-collector-smoke.py b/backend/scripts/acceptance/chr-netflow-collector-smoke.py new file mode 100644 index 00000000..124ea606 --- /dev/null +++ b/backend/scripts/acceptance/chr-netflow-collector-smoke.py @@ -0,0 +1,242 @@ +"""Disposable collector-only cycle. No RouterOS clients or capture are opened.""" +import argparse +import gzip +import hashlib +import io +import json +import os +import re +import runpy +import stat +import subprocess +import sys +import tarfile +import time +from pathlib import Path + +owner = runpy.run_path(str(Path(__file__).with_name('chr-netflow-collector-container.py'))) +require = owner['require'] +MAX_PACKET = 128 * 1024 * 1024 +PACKET_PATH = re.compile(r'(?:dist|drizzle|contracts-dist)/(?:[A-Za-z0-9_.-]+/)*[A-Za-z0-9_.-]+\Z') + + +def verify_packet(directory, digest, require_root=False): + require(re.fullmatch(r'[a-f0-9]{64}', digest), 'PACKET_DIGEST_REFUSED') + require(directory.resolve(strict=True) == directory and directory.is_dir(), 'PACKET_DIRECTORY_REFUSED') + manifest = directory / 'manifest.json' + info = manifest.lstat() + require(stat.S_ISREG(info.st_mode) and 0 < info.st_size <= 1048576, 'PACKET_MANIFEST_REFUSED') + raw = manifest.read_bytes() + require(hashlib.sha256(raw).hexdigest() == digest, 'PACKET_MANIFEST_DRIFT') + value = json.loads(raw) + require(value.get('format') == 'mmapp-netflow-collector-smoke-v1' and + value.get('cachedSourceRevision') == 'bf3d50e1ff2ee3b5c9268991a255c3b49ce6378e' and + type(value.get('files')) is list and 1 <= len(value['files']) <= 4096, 'PACKET_MANIFEST_REFUSED') + total, listed = 0, {'manifest.json'} + for row in value['files']: + relative = row.get('path', '') + require(PACKET_PATH.fullmatch(relative) and + not any(part in ('.', '..') for part in relative.split('/')) and relative not in listed and + type(row.get('size')) is int and 0 < row['size'] <= 2 * 1024 * 1024 and + re.fullmatch(r'[a-f0-9]{64}', row.get('sha256', '')), 'PACKET_FILE_REFUSED') + item = directory / relative + info = item.lstat() + total += row['size'] + require(total <= MAX_PACKET and stat.S_ISREG(info.st_mode) and item.resolve(strict=True) == item and + info.st_size == row['size'] and hashlib.sha256(item.read_bytes()).hexdigest() == row['sha256'], 'PACKET_FILE_DRIFT') + listed.add(relative) + observed = set() + for folder, directories, filenames in os.walk(directory, followlinks=False): + parent = Path(folder) + for entry in [parent, *(parent / name for name in directories + filenames)]: + info = entry.lstat() + require(not stat.S_ISLNK(info.st_mode) and + (not require_root or info.st_uid == 0 and not info.st_mode & 0o022), 'PACKET_OWNERSHIP_REFUSED') + observed.update((parent / filename).relative_to(directory).as_posix() for filename in filenames) + require(observed == listed and 'dist/test/traffic-flow-chr-collector-bootstrap.js' in listed, 'PACKET_FILE_SET_DRIFT') + return {'files': len(listed) - 1, 'bytes': total, 'manifestSha256': digest} + + +def pack(directory, digest): + match = re.fullmatch(r'mcp-netflow-smoke-packet-([a-f0-9]{8})', directory.name) + require(match is not None, 'PACKET_DIRECTORY_REFUSED') + verified = verify_packet(directory, digest) + archive = directory.parent / ('mcp-netflow-collector-smoke-' + match[1] + '.tar.gz') + manifest = json.loads((directory / 'manifest.json').read_bytes()) + script_root = Path(__file__).resolve().parent + scripts = ('chr-netflow-collector-smoke.py', 'chr-netflow-collector-container.py') + with archive.open('xb') as destination, gzip.GzipFile(fileobj=destination, mode='wb', filename='', mtime=0) as compressed: + with tarfile.open(fileobj=compressed, mode='w|', format=tarfile.USTAR_FORMAT) as tar: + def add(name, body): + item = tarfile.TarInfo(name) + item.mode = 0o644 + item.size = len(body) + tar.addfile(item, io.BytesIO(body)) + for name in scripts: + add(name, (script_root / name).read_bytes()) + add('packet/manifest.json', (directory / 'manifest.json').read_bytes()) + for row in manifest['files']: + add('packet/' + row['path'], (directory / row['path']).read_bytes()) + with archive.open('rb') as stream: + archive_sha = hashlib.file_digest(stream, 'sha256').hexdigest() + return {**verified, 'archive': str(archive), 'archiveSha256': archive_sha, 'dockerUsed': False, 'networkUsed': False} + + +class SmokeNamespace: + """Only the C alias and one local dummy address, never the transit topology.""" + def __init__(self, run, ip, namespace, checkpoint): + self.name, self.device = 'nf' + run + 'nc', 'nf' + run + 'cv' + self.ip, self.namespace, self.checkpoint = ip, namespace, checkpoint + self.anchor = None + + def create(self, read_anchor): + require(self.anchor is None and self.namespace(self.name) is None, 'SMOKE_NAMESPACE_PRESENT') + anchor = read_anchor() + require(anchor['namespace'] != anchor['hostNamespace'] and read_anchor() == anchor, 'SMOKE_ANCHOR_DRIFT') + self.checkpoint('smoke', 'attach-intent', anchor) + self.ip('netns', 'attach', self.name, str(anchor['pid'])) + require(self.namespace(self.name) == anchor['namespace'] and read_anchor() == anchor, 'SMOKE_ATTACH_UNCONFIRMED') + self.checkpoint('smoke', 'attach-verified', anchor) + self.anchor = anchor + def current(): + require(self.namespace(self.name) == anchor['namespace'] and read_anchor() == anchor, 'SMOKE_ANCHOR_DRIFT') + links = json.loads(self.ip('-n', self.name, '-j', 'link', 'show')) + require([item['ifname'] for item in links] == ['lo'], 'SMOKE_NAMESPACE_NOT_EMPTY') + self.checkpoint('smoke', 'dummy-intent', self.device) + current() + self.ip('-n', self.name, 'link', 'add', self.device, 'type', 'dummy') + current() + created = json.loads(self.ip('-n', self.name, '-j', '-d', 'link', 'show', 'dev', self.device)) + require(len(created) == 1 and created[0]['ifname'] == self.device and + created[0]['linkinfo']['info_kind'] == 'dummy' and created[0]['ifindex'] > 0, 'SMOKE_DUMMY_UNCONFIRMED') + self.ip('-n', self.name, 'address', 'add', '198.18.242.3/32', 'dev', self.device) + current() + self.ip('-n', self.name, 'link', 'set', self.device, 'up') + rows = json.loads(self.ip('-n', self.name, '-j', '-d', 'address', 'show', 'dev', self.device)) + routes = json.loads(self.ip('-n', self.name, '-j', '-4', 'route', 'show', 'table', 'all')) + require(len(rows) == 1 and rows[0]['ifname'] == self.device and rows[0]['linkinfo']['info_kind'] == 'dummy' and + rows[0]['ifindex'] == created[0]['ifindex'] and + 'UP' in rows[0]['flags'] and any(row.get('local') == '198.18.242.3' and row.get('prefixlen') == 32 + for row in rows[0]['addr_info']) and all(row.get('dst') != 'default' for row in routes) and + self.namespace(self.name) == anchor['namespace'] and read_anchor() == anchor, 'SMOKE_DUMMY_UNCONFIRMED') + self.checkpoint('smoke', 'dummy-verified', rows[0]['ifindex']) + return anchor + + def restore(self, assert_anchor_exited): + require(self.anchor is not None and self.namespace(self.name) == self.anchor['namespace'], + 'SMOKE_NAMESPACE_DRIFT') + assert_anchor_exited(self.anchor) + require(not self.ip('netns', 'pids', self.name).strip(), 'SMOKE_NAMESPACE_BUSY') + self.checkpoint('smoke', 'delete-intent', self.anchor) + self.ip('netns', 'delete', self.name) + require(self.namespace(self.name) is None, 'SMOKE_NAMESPACE_RETIREMENT_UNCONFIRMED') + self.checkpoint('smoke', 'delete-verified', self.anchor) + + +def linux_ip(): + binary = next((item.resolve(strict=True) for item in (Path('/usr/sbin/ip'), Path('/usr/bin/ip'), Path('/sbin/ip')) + if item.is_file()), None) + require(binary is not None and binary.stat().st_uid == 0 and not binary.stat().st_mode & 0o022, 'IP_BINARY_UNPINNED') + + def call(*args): + result = subprocess.run([str(binary), *args], env=owner['ENV'], stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=8) + require(result.returncode == 0 and len(result.stdout) <= 128 * 1024, 'IP_OUTCOME_UNCONFIRMED') + return result.stdout.decode('utf-8', 'strict') + return call + + +def ns_pin(name): + try: + row = Path('/run/netns', name).lstat() + except FileNotFoundError: + return None + require(stat.S_ISREG(row.st_mode) and row.st_uid == 0, 'SMOKE_NAMESPACE_HANDLE_UNSAFE') + return [row.st_dev, row.st_ino] + + +def run_smoke(run, uid, gid, port, digest): + docker, attach = owner['local_docker']() + base = Path(owner['root'](run)) + packet = base / 'packet' + verify_packet(packet, digest, require_root=True) + ip = linux_ip() + require(ns_pin('nf' + run + 'nc') is None, 'SMOKE_NAMESPACE_PRESENT') + journal = os.open(base / 'collector-smoke.jsonl', os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600) + with os.fdopen(journal, 'w', encoding='utf-8') as report: + def checkpoint(role, phase, value): + report.write(json.dumps({'role': role, 'phase': phase, 'value': value}, separators=(',', ':')) + '\n') + report.flush() + os.fsync(report.fileno()) + + checkpoint('smoke', 'packet-verified', digest) + owner['prepare_private_storage'](run, uid, gid) + runner = owner['CollectorContainerOwner'](run, docker, checkpoint) + runner.create_anchor(uid, gid) + runner.start_anchor() + scope = SmokeNamespace(run, ip, ns_pin, checkpoint) + # Reuse the container owner's live-C admission and retirement checks. + class Journal: + value = {} + class Backend: + def namespace(self, name): + return ns_pin(name) + def root_baseline(self): + raw = [ip('-j', '-4', 'route', 'show', 'table', 'all'), ip('-j', '-d', 'link', 'show')] + # Link counters/traffic are outside root configuration; use exact names and ifindex. + links = sorted((row['ifname'], row['ifindex']) for row in json.loads(raw[1])) + return hashlib.sha256(json.dumps([json.loads(raw[0]), links], sort_keys=True).encode()).hexdigest() + def provision(backend, ledger, _plan, _pins, read_collector_anchor): + baseline = backend.root_baseline() + anchor = scope.create(read_collector_anchor) + require(backend.root_baseline() == baseline, 'ROOT_NETWORK_DRIFT') + ledger.value.update(collectorAnchor=anchor, rootBaseline=baseline, state='created') + return anchor + def cleanup(_backend, ledger, _plan, assert_collector_anchor_exited): + scope.restore(assert_collector_anchor_exited) + ledger.value['state'] = 'retired' + backend, ledger, plan = Backend(), Journal(), {'names': {'C': scope.name}} + pin = runner.create_linux_topology(provision, backend, ledger, plan, {'nonce': run + '0' * 24}) + wait_until = time.monotonic() + 20 + socket = base / 'pg-socket' / '.s.PGSQL.5432' + while not socket.exists() and time.monotonic() < wait_until: + time.sleep(.1) + require(stat.S_ISSOCK(socket.lstat().st_mode), 'PRIVATE_PG_NOT_READY') + runner.create_collector(uid, gid, port, pin['namespace'], pin['hostNamespace'], digest, + lambda: verify_packet(packet, digest, require_root=True)) + runner.start_collector(attach) + runner.retire_exited('collector') + runner.stop_anchor(lambda: require(ledger.value['state'] == 'created', 'SMOKE_TOPOLOGY_DRIFT')) + runner.cleanup_linux_topology(cleanup, backend, ledger, plan) + runner.retire_exited('postgres') + checkpoint('smoke', 'complete', None) + return {'collectorSmokeCompleted': True, 'chrMutations': 0, 'capturePerformed': False, 'nativeAcceptance': False} + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + sub = parser.add_subparsers(dest='mode', required=True) + check = sub.add_parser('check') + check.add_argument('packet', type=Path) + check.add_argument('sha256') + package = sub.add_parser('pack') + package.add_argument('packet', type=Path) + package.add_argument('sha256') + run = sub.add_parser('run') + run.add_argument('run_id') + run.add_argument('uid', type=int) + run.add_argument('gid', type=int) + run.add_argument('port', type=int) + run.add_argument('sha256') + args = parser.parse_args() + try: + if args.mode == 'check': + result = {**verify_packet(args.packet.resolve(), args.sha256), 'dockerUsed': False, 'networkUsed': False} + elif args.mode == 'pack': + result = pack(args.packet.resolve(), args.sha256) + else: + result = run_smoke(args.run_id, args.uid, args.gid, args.port, args.sha256) + print(json.dumps(result)) + except (OSError, ValueError, RuntimeError, subprocess.SubprocessError, KeyError) as error: + print(json.dumps({'completed': False, 'failureClass': type(error).__name__, 'automaticReplay': False})) + raise SystemExit(1) from None diff --git a/backend/scripts/acceptance/chr-netflow-collector-smoke.test.py b/backend/scripts/acceptance/chr-netflow-collector-smoke.test.py new file mode 100644 index 00000000..d7b8f6b9 --- /dev/null +++ b/backend/scripts/acceptance/chr-netflow-collector-smoke.test.py @@ -0,0 +1,69 @@ +import hashlib +import json +from pathlib import Path +import runpy +import tempfile +import unittest + +smoke = runpy.run_path(str(Path(__file__).with_name('chr-netflow-collector-smoke.py'))) + + +class SmokeTests(unittest.TestCase): + def test_pin_and_file_set_drift_fail_closed(self): + with tempfile.TemporaryDirectory(dir=Path(__file__).resolve().parents[3] / 'tmp') as temp: + root = Path(temp).resolve() + entry = root / 'dist/test/traffic-flow-chr-collector-bootstrap.js' + entry.parent.mkdir(parents=True) + entry.write_bytes(b'owned fixture\n') + body = json.dumps({'format': 'mmapp-netflow-collector-smoke-v1', + 'cachedSourceRevision': 'bf3d50e1ff2ee3b5c9268991a255c3b49ce6378e', + 'files': [{'path': entry.relative_to(root).as_posix(), 'size': entry.stat().st_size, + 'sha256': hashlib.sha256(entry.read_bytes()).hexdigest()}]}).encode() + (root / 'manifest.json').write_bytes(body) + digest = hashlib.sha256(body).hexdigest() + self.assertEqual(smoke['verify_packet'](root, digest)['files'], 1) + entry.write_bytes(b'foreign data!\n') + with self.assertRaisesRegex(ValueError, 'FILE_DRIFT'): + smoke['verify_packet'](root, digest) + with self.assertRaisesRegex(ValueError, 'MANIFEST_DRIFT'): + smoke['verify_packet'](root, '0' * 64) + + def test_exact_dummy_namespace_and_busy_cleanup(self): + anchor = {'containerId': 'a' * 64, 'pid': 42, 'namespace': [10, 20], 'hostNamespace': [10, 21]} + calls, journal = [], [] + namespace = None + busy = True + name, device = 'nfa012bcdenc', 'nfa012bcdecv' + link = {'ifname': device, 'ifindex': 5, 'linkinfo': {'info_kind': 'dummy'}, 'flags': ['UP'], + 'addr_info': [{'local': '198.18.242.3', 'prefixlen': 32}]} + def ip(*args): + nonlocal namespace + calls.append(args) + if args[:2] == ('netns', 'attach'): + self.assertEqual(args, ('netns', 'attach', name, '42')) + namespace = anchor['namespace'] + elif args[:2] == ('netns', 'pids'): + return '42' if busy else '' + elif args[:2] == ('netns', 'delete'): + self.assertEqual(args, ('netns', 'delete', name)) + namespace = None + elif args == ('-n', name, '-j', 'link', 'show'): + return json.dumps([{'ifname': 'lo'}]) + elif '-d' in args: + return json.dumps([link]) + elif 'route' in args: + return '[]' + return '' + fixture = smoke['SmokeNamespace']('a012bcde', ip, lambda _name: namespace, lambda *row: journal.append(row)) + self.assertEqual(fixture.create(lambda: anchor), anchor) + self.assertIn(('-n', name, 'address', 'add', '198.18.242.3/32', 'dev', device), calls) + with self.assertRaisesRegex(ValueError, 'NAMESPACE_BUSY'): + fixture.restore(lambda pin: self.assertEqual(pin, anchor)) + self.assertNotIn(('netns', 'delete', name), calls) + busy = False + fixture.restore(lambda pin: self.assertEqual(pin, anchor)) + self.assertEqual(journal[-1][1], 'delete-verified') + + +if __name__ == '__main__': + unittest.main() diff --git a/backend/scripts/acceptance/chr-netflow-drain-witness-pg.mjs b/backend/scripts/acceptance/chr-netflow-drain-witness-pg.mjs new file mode 100644 index 00000000..54dbafc0 --- /dev/null +++ b/backend/scripts/acceptance/chr-netflow-drain-witness-pg.mjs @@ -0,0 +1,123 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { randomBytes } from 'node:crypto' +import { lstat, mkdtemp, rm } from 'node:fs/promises' +import { createServer } from 'node:net' +import { tmpdir } from 'node:os' +import { isAbsolute, join, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import EmbeddedPostgres from 'embedded-postgres' +import pg from 'pg' +import { applySqlMigrations } from '../../src/db/migrate.ts' +import { MIGRATION_MANIFEST } from '../../src/db/migration-manifest.ts' + +assert.deepEqual(process.argv.slice(2), ['--owned-embedded']) +const bin = process.env.MMAPP_PG_CLIENT_BIN +assert(bin && isAbsolute(bin) && resolve(bin) === bin, 'Set MMAPP_PG_CLIENT_BIN to the exact local PostgreSQL client directory') +const psql = join(bin, process.platform === 'win32' ? 'psql.exe' : 'psql') +const executable = await lstat(psql) +assert(executable.isFile() && !executable.isSymbolicLink(), 'PostgreSQL client must be a regular file') +const clientEnv = Object.fromEntries(Object.entries(process.env).filter(([name]) => !/^PG/i.test(name))) +const run = (command, args, options = {}) => execFileSync(command, args, { + timeout: 10000, maxBuffer: 16384, encoding: 'utf8', windowsHide: true, ...options, +}) +assert.match(run(psql, ['--version'], { env: clientEnv }), /^psql \(PostgreSQL\) (?:1[6-9]|[2-9][0-9])\./) +const witness = fileURLToPath(new URL('../../../tmp/mcp-netflow-linux-topology-20261104/collector-drain-witness.py', import.meta.url)) +const { IDENTITY_SQL, SQL } = JSON.parse(run('python', ['-I', '-B', '-c', + 'import json,runpy,sys; m=runpy.run_path(sys.argv[1]); print(json.dumps({k:m[k] for k in ("IDENTITY_SQL","SQL")}))', witness])) +assert(typeof SQL === 'string' && typeof IDENTITY_SQL === 'string') + +const directory = await mkdtemp(join(tmpdir(), 'mm-netflow-drain-pg-')) +const password = randomBytes(24).toString('hex') +const host = '127.0.0.1', database = 'postgres', user = 'mcp_test' +const listener = createServer() +let embedded, pool, started = false +try { + await new Promise((done, reject) => { listener.once('error', reject); listener.listen(0, host, done) }) + const { port } = listener.address() + await new Promise((done, reject) => listener.close(error => error ? reject(error) : done())) + embedded = new EmbeddedPostgres({ + databaseDir: join(directory, 'pg'), port, user, password, persistent: true, createPostgresUser: false, + initdbFlags: ['--encoding=UTF8'], postgresFlags: ['-h', host, '-c', 'unix_socket_directories='], + onLog: () => {}, onError: () => {}, + }) + await embedded.initialise() + await embedded.start() + started = true + pool = new pg.Pool({ host, port, database, user, password, max: 1, connectionTimeoutMillis: 5000 }) + await applySqlMigrations(pool) + const { rows: [version] } = await pool.query("SELECT current_setting('server_version_num')::int AS number, (SELECT count(*)::int FROM schema_migrations) AS migrations") + assert(version.number >= 180000 && version.number <= 189999) + assert.equal(version.migrations, MIGRATION_MANIFEST.length) + + const runId = 'a012bcde', owner = 'netflow-operator-' + runId, collectorPort = 51000 + const read = (sql, probePort = collectorPort) => JSON.parse(run(psql, ['-X', '-w', '-qAt', + '-h', host, '-p', String(port), '-U', user, '-d', database, '-v', 'ON_ERROR_STOP=1', + '-v', 'database=' + database, '-v', 'socket_dir=/tmp/mcp-netflow-' + runId + '/pg-socket', + '-v', 'run_id=' + runId, '-v', 'port=' + probePort], + { env: { ...clientEnv, PGCONNECT_TIMEOUT: '2', PGPASSWORD: password }, input: sql }).trim()) + const identity = read(IDENTITY_SQL) + assert.deepEqual(Object.keys(identity).sort(), ['oid', 'source']) + assert.match(identity.oid, /^[1-9][0-9]{0,9}$/) + // The production source predicate deliberately rejects this TCP fixture. + assert.equal(identity.source, false) + const expect = (planCount, enabled, verified, ordered = true, sqlPort = collectorPort) => + assert.deepEqual(read(SQL, sqlPort), { ...identity, connectionCount: 1, planCount, enabled, verified, ordered }) + + const uuid = index => 'a012bcde-0000-4000-8000-' + String(index).padStart(12, '0') + const connection = uuid(1) + await pool.query(`INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation) + VALUES($1,$2,$3,'[]','{}','inert-witness')`, [connection, 'Owned NetFlow ' + runId, owner]) + await pool.query("UPDATE traffic_flow_settings SET collector_ip='198.18.242.3', flow_listen_port=$1, enabled=false WHERE id=1", [collectorPort]) + expect(0, false, []) + + async function seed(index, enabled) { + const plan = uuid(index), operation = uuid(index + 1), receipt = uuid(index + 2) + const base = Date.UTC(2026, 0, 1) + index * 1000 + const at = offset => new Date(base + offset) + const result = { receiptId: receipt, configurationConfirmed: true, receiptConfirmed: true, + listener: 'request-acknowledged', automaticReplay: false } + await pool.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by,approved_at,created_at) + VALUES($1,$2,'netflow.settings.update',3,1,'inert-witness','{}','{}','completed',$3,$4,$5,$6)`, + [plan, connection, at(9000), owner, at(100), at(0)]) + await pool.query(`INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,verification,created_at,finished_at) + VALUES($1,$2,$3,$4,'applied','passed',$5,$6)`, [operation, plan, connection, 'inert-' + index, at(200), at(400)]) + await pool.query(`INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state,result,created_at,finished_at) + VALUES($1,0,'verified',$2,$3,$4,$5)`, + [operation, { service: 'netflow.settings.update', input: { receiptId: receipt, enabled } }, result, at(300), at(400)]) + await pool.query(`INSERT INTO mcp_audit(actor_id,action,connection_id,payload,created_at) + VALUES($1,'plan.approve',$2,$3,$4)`, [owner, connection, { planId: plan }, at(100)]) + await pool.query(`INSERT INTO flow_settings_intents(receipt_id,phase,request_proof,state,result,created_at) + VALUES($1,'configuration','inert-proof','completed',$2,$3),($1,'listener','inert-proof','completed',$2,$3)`, [receipt, result, at(300)]) + await pool.query('UPDATE traffic_flow_settings SET enabled=$1 WHERE id=1', [enabled]) + return [plan, operation, receipt] + } + const enabled = await seed(10, true) + expect(1, true, [enabled]) + const disabled = await seed(20, false) + expect(2, false, [enabled, disabled]) + expect(2, null, [enabled, disabled], true, collectorPort + 1) + + await pool.query("UPDATE flow_settings_intents SET request_proof='foreign' WHERE receipt_id=$1 AND phase='listener'", [disabled[2]]) + expect(2, false, [enabled]) + await pool.query("UPDATE flow_settings_intents SET request_proof='inert-proof' WHERE receipt_id=$1 AND phase='listener'", [disabled[2]]) + const { rows: [duplicate] } = await pool.query(`INSERT INTO mcp_audit(actor_id,action,connection_id,payload) + VALUES($1,'plan.approve',$2,$3) RETURNING id`, [owner, connection, { planId: disabled[0] }]) + expect(2, false, [enabled]) + await pool.query('DELETE FROM mcp_audit WHERE id=$1', [duplicate.id]) + const { rows: [original] } = await pool.query(`UPDATE mcp_operations SET finished_at=(SELECT created_at + interval '1 second' FROM mcp_plans WHERE id=$2) + WHERE id=$1 RETURNING created_at`, [enabled[1], disabled[0]]) + expect(2, false, [enabled, disabled], false) + await pool.query("UPDATE mcp_operations SET finished_at=$2::timestamptz + interval '200 milliseconds' WHERE id=$1", [enabled[1], original.created_at]) + expect(2, false, [enabled, disabled]) + console.log(JSON.stringify({ migrations: version.migrations, exactPsqlQueries: true, inertEnableDisableAndRejections: true, + linuxSocketSourceAccepted: false, realMcpExecutionClaimed: false })) +} finally { + try { + await pool?.end() + } finally { + if (started) await embedded.stop() + if (listener.listening) await new Promise(done => listener.close(done)) + await rm(directory, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }) + } +} diff --git a/backend/scripts/acceptance/chr-netflow-endpoint-discovery.mjs b/backend/scripts/acceptance/chr-netflow-endpoint-discovery.mjs new file mode 100644 index 00000000..348049ed --- /dev/null +++ b/backend/scripts/acceptance/chr-netflow-endpoint-discovery.mjs @@ -0,0 +1,221 @@ +import { createHash } from 'node:crypto'; +import { readFile } from 'node:fs/promises'; +import { isIPv4 } from 'node:net'; +import { pathToFileURL } from 'node:url'; + +const routes = Object.freeze(['/system/resource', '/ip/traffic-flow', '/ip/traffic-flow/target']); +const policyRoutes = Object.freeze(['/system/resource', '/ip/settings', '/ip/firewall/connection/tracking', + '/ip/firewall/filter', '/ip/firewall/raw', '/ip/firewall/mangle', '/ip/firewall/nat', '/routing/rule', '/ip/vrf']); +const counterFields = ['ifindex', 'rx-packet', 'tx-packet', 'fp-rx-byte', 'fp-tx-byte', 'fp-rx-packet', 'fp-tx-packet']; +const nativeId = value => typeof value === 'string' && /^\*[a-f0-9]{1,16}$/i.test(value); +const ports = Object.freeze({ '194.226.121.150': 19081, '104.165.244.7': 19082 }); +const attestation = 'verified-known-host-161.104.46.222-owned-forward'; +const reviewedTransportHash = '67d86dbae2710a25145df8ab9469029182f21a31972c49f063cdb6bc3daa8ea6'; +const sha = bytes => createHash('sha256').update(bytes).digest('hex'); +const deny = () => { throw new Error('NetFlow discovery refused'); }; + +export async function loadPinnedNetFlowTransport(policy = false) { + const allowedPaths = policy ? policyRoutes : routes; + return loadTransport(allowedPaths, allowedPaths.length * 2, policy ? 131072 : 65536); +} + +async function loadTransport(allowedPaths, requests, responseLimit, totalLimit = responseLimit * requests) { + const bytes = await readFile(new URL('./chr-recursive-observation.mjs', import.meta.url)); + if (sha(bytes) !== reviewedTransportHash) deny(); + const reviewed = bytes.toString('utf8'); + const source = reviewed.slice(0, reviewed.indexOf('async function privateStdin()')) + .replace(/const ROOT = .*?;/, 'const ROOT = null;') + .replace(/export const PATHS = Object.freeze\(\[[\s\S]*?\]\);/, `export const PATHS = Object.freeze(${JSON.stringify(allowedPaths)});`) + .replace('response: 1048576, bytes: 8388608, requests: 256', + `response: ${responseLimit}, bytes: ${totalLimit}, requests: ${requests}`); + const transport = await import('data:text/javascript;base64,' + Buffer.from(source).toString('base64')); + if (JSON.stringify(transport.PATHS) !== JSON.stringify(allowedPaths)) deny(); + for (const [host, port] of Object.entries(ports)) { + if (endpoint(transport, host) !== `http://127.0.0.1:${port}`) deny(); + } + return transport; +} + +export async function discoverPinnedInterfaceCounterShape(read) { + const devices = []; + for (const host of Object.keys(ports)) { + const inventory = await read(host, '/interface'); + if (!Array.isArray(inventory) || inventory.length > 256 || + inventory.some(row => !nativeId(row?.['.id'])) || + new Set(inventory.map(row => row['.id'])).size !== inventory.length) deny(); + const preferred = type => inventory.find(row => row.type === type && + observedFlag(row.running) === true && observedFlag(row.disabled) === false); + const chosen = preferred('wg') ?? preferred('ether'); + if (!chosen) { + devices.push({ status: 'no-eligible-interface' }); + continue; + } + const exact = single(await read(host, '/interface/' + encodeURIComponent(chosen['.id']))); + if (exact['.id'] !== chosen['.id'] || exact.type !== chosen.type || + observedFlag(exact.running) !== true || observedFlag(exact.disabled) !== false) deny(); + const fields = Object.fromEntries(counterFields.map(key => { + const value = exact[key]; + return [key, { present: Object.hasOwn(exact, key), + type: value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value }]; + })); + devices.push({ status: 'shape-observed', fields }); + } + return { devices, ingressVerified: false, capturePerformed: false, mutationsPerformed: false }; +} + +function endpoint(transport, host) { + return transport.configuration(['--source', host, '--transport', 'ssh-loopback', '--output', + 'tmp/netflow-endpoint-discovery', '--owned-tunnel-attestation', attestation]).endpoint; +} + +function bool(value) { + if (value === true || value === 'true') return true; + if (value === false || value === 'false') return false; + return deny(); +} + +function single(rows) { + if (rows.length !== 1) deny(); + return rows[0]; +} + +function observedFlag(value) { + if ([true, 'true', 'yes'].includes(value)) return true; + if ([false, 'false', 'no'].includes(value)) return false; + return null; +} + +export async function discoverPinnedNetFlowPolicy(read) { + const exporters = []; + for (const host of Object.keys(ports)) { + const resource = single(await read(host, policyRoutes[0])); + if (typeof resource.version !== 'string' || !/^\d+\.\d+(?:\.\d+)?(?: \([a-z-]+\))?$/.test(resource.version)) deny(); + const ip = single(await read(host, policyRoutes[1])); + const tracking = single(await read(host, policyRoutes[2])); + const firewall = {}; + const vetoes = []; + const ipForward = observedFlag(ip['ip-forward']); + const allowFastPath = observedFlag(ip['allow-fast-path']); + const conntrack = tracking.enabled === 'auto' ? 'auto' : observedFlag(tracking.enabled); + if (ipForward !== true) vetoes.push('ip-forward-not-confirmed'); + if (allowFastPath !== false) vetoes.push('fast-path-not-confirmed-disabled'); + if (conntrack !== true) vetoes.push('conntrack-not-explicitly-enabled'); + for (const path of policyRoutes.slice(3, 7)) { + const rows = await read(host, path); + const enabled = rows.filter(row => observedFlag(row.disabled) !== true); + firewall[path.split('/').at(-1)] = { + total: rows.length, + notExplicitlyDisabled: enabled.length, + fastTrack: enabled.filter(row => row.action === 'fasttrack-connection').length, + }; + } + const routingRules = await read(host, policyRoutes[7]); + const activeRoutingRules = routingRules.filter(row => observedFlag(row.disabled) !== true).length; + if (activeRoutingRules) vetoes.push('routing-policy-present'); + const vrfs = await read(host, policyRoutes[8]); + const mainVrfOnly = vrfs.length === 1 && vrfs[0].name === 'main' && vrfs[0].interfaces === 'all' && + observedFlag(vrfs[0].disabled) !== true; + if (!mainVrfOnly) vetoes.push('main-vrf-only-not-confirmed'); + exporters.push({ host, version: resource.version, ipForward, allowFastPath, conntrack, firewall, + activeRoutingRules, vrfCount: vrfs.length, mainVrfOnly, vetoes }); + } + return { + status: exporters.some(item => item.vetoes.length) ? 'pre-topology-veto' : 'no-global-veto-observed', + exporters, routeSelectionVerified: false, tuplePolicyVerified: false, + ingressVerified: false, capturePerformed: false, mutationsPerformed: false, + }; +} + +function target(row) { + if (bool(row.disabled) || row.version !== 'ipfix') return null; + const destination = row['dst-address']; + const portText = String(row.port); + if (!isIPv4(destination) || !/^[1-9][0-9]{0,4}$/.test(portText) || Number(portText) > 65535) deny(); + const source = row['src-address']; + if (source !== undefined && source !== '' && !isIPv4(source)) deny(); + return { destination, port: Number(portText), configuredSource: source && source !== '0.0.0.0' ? source : null }; +} + +export async function discoverPinnedNetFlowEndpoints(read) { + const exporters = []; + for (const host of Object.keys(ports)) { + const resource = single(await read(host, routes[0])); + if (typeof resource.version !== 'string' || !/^\d+\.\d+(?:\.\d+)?(?: \([a-z-]+\))?$/.test(resource.version)) deny(); + const enabled = bool(single(await read(host, routes[1])).enabled); + const rows = await read(host, routes[2]); + if (rows.length > 64) deny(); + const configuredTargets = rows.map(target).filter(Boolean); + exporters.push({ host, version: resource.version, enabled, configuredTargets }); + } + const [first, second] = exporters; + const sameEndpoint = (a, b) => a.destination === b.destination && a.port === b.port; + const common = first.enabled && second.enabled + ? first.configuredTargets.filter(a => second.configuredTargets.some(b => sameEndpoint(a, b))) + : []; + const unique = common.length === 1 && second.configuredTargets.filter(b => sameEndpoint(common[0], b)).length === 1; + return { + status: common.length === 0 ? 'no-common-active-ipfix-target' : unique ? 'configured-endpoint' : 'ambiguous-endpoint', + configuredEndpoint: unique ? { destination: common[0].destination, port: common[0].port } : null, + exporters, + ingressVerified: false, + capturePerformed: false, + mutationsPerformed: false, + }; +} + +async function main() { + const mode = process.argv.slice(2); + if (mode.length !== 1) deny(); + const policy = mode[0] === '--check-policy' || mode[0] === '--read-pinned-policy'; + const counters = mode[0] === '--check-counters' || mode[0] === '--read-pinned-counters'; + const transport = counters ? await loadTransport(['/interface'], 4, 131072) : await loadPinnedNetFlowTransport(policy); + if (mode[0] === '--check-only' || mode[0] === '--check-policy') { + console.log(JSON.stringify({ transportSha256: reviewedTransportHash, loopbackPorts: ports, readPaths: transport.PATHS, networkUsed: false })); + return; + } + if (mode[0] === '--check-counters') { + const detail = await loadTransport(['/interface/*A'], 4, 16384); + if (detail.PATHS[0] !== '/interface/*A') deny(); + console.log(JSON.stringify({ transportSha256: reviewedTransportHash, loopbackPorts: ports, + readPaths: ['/interface', '/interface/'], networkUsed: false })); + return; + } + if (!['--read-pinned', '--read-pinned-policy', '--read-pinned-counters'].includes(mode[0]) || process.stdin.isTTY) deny(); + const chunks = []; + let length = 0; + for await (const chunk of process.stdin) { + length += chunk.length; + if (length > 4096) deny(); + chunks.push(chunk); + } + const input = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks))); + if (!input || input.attestation !== attestation || + Object.keys(input).length !== 2 || !input.passwords || Object.keys(input.passwords).length !== 2) deny(); + const passwords = Object.keys(ports).map(host => input.passwords[host]); + if (passwords.some(password => typeof password !== 'string' || !/^[^\u0000-\u001f\u007f]{1,512}$/u.test(password))) deny(); + const authorizations = passwords.map(password => 'Basic ' + Buffer.from('admin:' + password).toString('base64')); + const secrets = [...passwords, ...authorizations, ...authorizations.map(value => value.slice(6))].map(value => Buffer.from(value)); + const budget = { requests: 0, bytes: 0, rows: 0, deadline: Date.now() + 30000 }; + const discovery = counters ? discoverPinnedInterfaceCounterShape : policy ? discoverPinnedNetFlowPolicy : discoverPinnedNetFlowEndpoints; + const report = await discovery(async (host, route) => { + const authorization = authorizations[Object.keys(ports).indexOf(host)]; + let selected = transport; + if (counters && route !== '/interface') { + if (!route.startsWith('/interface/') || !nativeId(route.slice('/interface/'.length))) deny(); + selected = await loadTransport([route], 4, 16384, 524288); + } + const response = await selected.readResponse(endpoint(selected, host), route, authorization, budget); + if (secrets.some(secret => response.body.includes(secret))) deny(); + return response.rows; + }); + const output = JSON.stringify(report); + if (secrets.some(secret => Buffer.from(output).includes(secret))) deny(); + console.log(output); +} + +if (process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url) { + main().catch(() => { + console.error('NetFlow discovery refused'); + process.exitCode = 1; + }); +} diff --git a/backend/scripts/acceptance/chr-netflow-endpoint-discovery.test.mjs b/backend/scripts/acceptance/chr-netflow-endpoint-discovery.test.mjs new file mode 100644 index 00000000..386cbcc2 --- /dev/null +++ b/backend/scripts/acceptance/chr-netflow-endpoint-discovery.test.mjs @@ -0,0 +1,102 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { discoverPinnedInterfaceCounterShape, discoverPinnedNetFlowEndpoints, discoverPinnedNetFlowPolicy, loadPinnedNetFlowTransport } from './chr-netflow-endpoint-discovery.mjs'; + +const hosts = ['194.226.121.150', '104.165.244.7']; +const routes = ['/system/resource', '/ip/traffic-flow', '/ip/traffic-flow/target']; +const target = { disabled: 'false', version: 'ipfix', 'dst-address': '192.0.2.10', port: '2055', 'src-address': '0.0.0.0' }; + +function fixture(targets, enabled = ['true', 'true']) { + const calls = []; + const read = async (host, path) => { + calls.push([host, path]); + if (path === routes[0]) return [{ version: '7.24.5 (stable)' }]; + if (path === routes[1]) return [{ enabled: enabled[hosts.indexOf(host)] }]; + assert.equal(path, routes[2]); + return targets[hosts.indexOf(host)]; + }; + return { read, calls }; +} + +test('loads the reviewed source with the exact read-only path and relay fence', async () => { + const transport = await loadPinnedNetFlowTransport(); + assert.deepEqual(transport.PATHS, routes); + assert.throws(() => transport.readResponse('http://127.0.0.1:19082', '/ip/address', '', {}), /request-budget/); + assert.throws(() => transport.readResponse('http://127.0.0.1:19083', routes[0], '', {}), /non-whitelisted-endpoint/); +}); + +test('reports only a common configured endpoint, with no ingress or capture claim', async () => { + const { read, calls } = fixture([[target], [{ ...target, 'src-address': '192.0.2.20' }]]); + const report = await discoverPinnedNetFlowEndpoints(read); + assert.deepEqual(calls, hosts.flatMap(host => routes.map(path => [host, path]))); + assert.equal(report.status, 'configured-endpoint'); + assert.deepEqual(report.configuredEndpoint, { destination: '192.0.2.10', port: 2055 }); + assert.deepEqual(report.exporters.map(item => item.configuredTargets[0].configuredSource), [null, '192.0.2.20']); + assert.equal(report.ingressVerified, false); + assert.equal(report.capturePerformed, false); + assert.equal(report.mutationsPerformed, false); +}); + +test('returns successful absence for missing or disabled baseline', async () => { + for (const input of [ + fixture([[], [target]]), + fixture([[target], [target]], [true, false]), + fixture([[target], [{ ...target, disabled: true }]]), + fixture([[target], [{ ...target, version: '9' }]]), + ]) { + const result = await discoverPinnedNetFlowEndpoints(input.read); + assert.equal(result.status, 'no-common-active-ipfix-target'); + assert.equal(result.configuredEndpoint, null); + } +}); + +test('refuses malformed relevant rows and withholds ambiguous common endpoints', async () => { + const result = await discoverPinnedNetFlowEndpoints(fixture([[target], [target, target]]).read); + assert.equal(result.status, 'ambiguous-endpoint'); + assert.equal(result.configuredEndpoint, null); + await assert.rejects(discoverPinnedNetFlowEndpoints(fixture([[target], [{ ...target, port: '65536' }]]).read), /^Error: NetFlow discovery refused$/); +}); + +test('reports preliminary global vetoes and counts with no claim on tuple policy or routes', async () => { + const transport = await loadPinnedNetFlowTransport(true); + const calls = []; + const report = await discoverPinnedNetFlowPolicy(async (host, path) => { + calls.push([host, path]); + return ({ + '/system/resource': [{ version: '7.24.5 (stable)' }], + '/ip/settings': [{ 'ip-forward': 'yes', 'allow-fast-path': host === hosts[0] ? 'yes' : 'no' }], + '/ip/firewall/connection/tracking': [{ enabled: host === hosts[0] ? 'auto' : 'yes' }], + '/ip/firewall/filter': [{ action: 'fasttrack-connection' }, { action: 'drop', disabled: 'true' }], + '/ip/firewall/raw': [], '/ip/firewall/mangle': [], '/ip/firewall/nat': [], + '/routing/rule': host === hosts[0] ? [{}] : [], + '/ip/vrf': [{ name: 'main', interfaces: 'all' }], + })[path]; + }); + assert.deepEqual(calls, hosts.flatMap(host => transport.PATHS.map(path => [host, path]))); + assert.equal(report.status, 'pre-topology-veto'); + assert.deepEqual(report.exporters[0].vetoes, ['fast-path-not-confirmed-disabled', 'conntrack-not-explicitly-enabled', 'routing-policy-present']); + assert.deepEqual(report.exporters[1].vetoes, []); + assert.deepEqual(report.exporters[0].firewall.filter, { total: 2, notExplicitlyDisabled: 1, fastTrack: 1 }); + assert.equal(report.routeSelectionVerified, false); + assert.equal(report.tuplePolicyVerified, false); + assert.equal(report.ingressVerified, false); +}); + +test('counter shape reads exact selected native IDs and emits no identities or counter values', async () => { + const ether = { '.id': '*A', name: 'private-ether', type: 'ether', running: 'true', disabled: 'false' }; + const wg = { ...ether, '.id': '*B', name: 'private-wg', type: 'wg' }; + const calls = []; + const report = await discoverPinnedInterfaceCounterShape(async (host, route) => { + calls.push([host, route]); + if (route === '/interface') return host === hosts[0] ? [ether, wg] : [ether]; + return [{ ...(host === hosts[0] ? wg : ether), ifindex: '61455', 'rx-packet': 71922, 'fp-rx-byte': null }]; + }); + assert.deepEqual(calls, [[hosts[0], '/interface'], [hosts[0], '/interface/*B'], [hosts[1], '/interface'], [hosts[1], '/interface/*A']]); + assert.deepEqual(report.devices[0].fields.ifindex, { present: true, type: 'string' }); + assert.deepEqual(report.devices[0].fields['rx-packet'], { present: true, type: 'number' }); + assert.deepEqual(report.devices[0].fields['fp-rx-byte'], { present: true, type: 'null' }); + assert.deepEqual(report.devices[0].fields['tx-packet'], { present: false, type: 'undefined' }); + assert.doesNotMatch(JSON.stringify(report), /private|61455|71922|104\.165|194\.226|\*[AB]/); + await assert.rejects(discoverPinnedInterfaceCounterShape(async (_host, route) => + route === '/interface' ? [ether] : [{ ...ether, '.id': '*C' }]), /discovery refused/); +}); diff --git a/backend/scripts/acceptance/chr-recursive-direct-links.mjs b/backend/scripts/acceptance/chr-recursive-direct-links.mjs new file mode 100644 index 00000000..3fc9077a --- /dev/null +++ b/backend/scripts/acceptance/chr-recursive-direct-links.mjs @@ -0,0 +1,86 @@ +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import { lstat, readFile } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { decodeBody, SOURCES } from './chr-recursive-observation.mjs'; + +const reviewedReportSha256 = 'fccf5171c8e50730a3e7c1fcceeeb77f1e946589ac4a56c6e04668e0ff4663f9'; +const sha256 = bytes => createHash('sha256').update(bytes).digest('hex'); +const opaqueId = value => typeof value === 'string' && /^\*[a-f0-9]+$/i.test(value); +const decimalIndex = value => typeof value === 'string' && /^[1-9][0-9]{0,15}$/.test(value); + +export function inspectDirectLinks(routes, nexthops, interfaces, tables) { + function keyed(rows, field, valid) { + const index = new Map(); + for (const row of rows) { + const key = row[field]; + assert(valid(key) && !index.has(key), 'Missing or ambiguous ' + field); + index.set(key, row); + } + return index; + } + const byNexthopId = keyed(nexthops, '.id', opaqueId); + const byInterfaceIndex = keyed(interfaces, 'ifindex', decimalIndex); + const byTableName = keyed(tables, 'name', value => typeof value === 'string' && value !== '' && !/^[0-9]+$/.test(value)); + let opaqueReferences = 0, namedInterfaceCorroborations = 0, interfaceIdSubstitutionRefusals = 0; + for (const route of routes) { + assert(byTableName.has(route['routing-table']), 'Missing named routing table'); + if (!Object.hasOwn(route, 'nexthop-id')) continue; + const nexthop = byNexthopId.get(route['nexthop-id']); + assert(nexthop, 'No exact opaque nexthop match'); + opaqueReferences++; + const gateway = route['immediate-gw']; + const name = typeof gateway === 'string' ? /%([^%@]+)$/.exec(gateway)?.[1] : undefined; + if (!name) continue; + const index = nexthop['immediate-gw.interface-idx']; + const observed = byInterfaceIndex.get(index); + assert(observed?.name === name && interfaces.filter(row => row.name === name).length === 1, + 'No exact named interface corroboration for ifindex'); + namedInterfaceCorroborations++; + if (!interfaces.some(row => row.id === index && row.name === name)) interfaceIdSubstitutionRefusals++; + } + assert(opaqueReferences && namedInterfaceCorroborations && interfaceIdSubstitutionRefusals, 'Capture lacks discriminating direct links'); + return { opaqueReferences, namedInterfaceCorroborations, interfaceIdSubstitutionRefusals, + tableNames: byTableName.size, recursiveLinkRows: nexthops.filter(row => Object.keys(row).some(key => key === 'links' || key.startsWith('links.'))).length }; +} + +async function readPinned(directory, filename, digest) { + const file = path.join(directory, filename), stat = await lstat(file); + assert(stat.isFile() && !stat.isSymbolicLink() && stat.size <= 1048576, 'Unavailable bounded raw file'); + const bytes = await readFile(file); + assert(bytes.length === stat.size && sha256(bytes) === digest, 'Raw capture hash changed'); + return bytes; +} + +export async function checkReviewedDirectLinks(directory) { + const report = JSON.parse(await readPinned(directory, 'report.json', reviewedReportSha256)); + const sources = []; + for (const source of report.sources) { + assert(source.complete && source.versionBracketEqual && SOURCES[source.source]?.serverId === source.serverId); + const read = async (index, resource) => { + const record = source.records[index]; + assert(record.resource === resource && record.status === 200 && record.version === '7.24.5 (stable)'); + assert(record.file === `server${source.serverId}-${String(index + 1).padStart(2, '0')}-${resource.slice(1).replaceAll('/', '-')}.json`); + return decodeBody(await readPinned(directory, record.file, record.rawSha256)).rows; + }; + const routes = await read(1, '/routing/route'), nexthops = await read(2, '/routing/nexthop'); + assert.deepEqual(await read(7, '/routing/route'), routes); + assert.deepEqual(await read(6, '/routing/nexthop'), nexthops); + assert.equal(source.records[1].rawSha256, source.records[7].rawSha256); + assert.equal(source.records[2].rawSha256, source.records[6].rawSha256); + const interfaces = await read(3, '/interface'), tables = await read(4, '/routing/table'); + sources.push({ serverId: source.serverId, ...inspectDirectLinks(routes, nexthops, interfaces, tables) }); + } + return { reviewedReportSha256, sources, recursiveIdentity: 'unknown', semanticProof: false }; +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + try { + assert(process.argv.length === 3, 'Supply the reviewed offline capture directory'); + process.stdout.write(JSON.stringify(await checkReviewedDirectLinks(process.argv[2])) + '\n'); + } catch { + process.stderr.write('Reviewed direct-link corroboration refused.\n'); + process.exitCode = 1; + } +} diff --git a/backend/scripts/acceptance/chr-recursive-direct-links.test.mjs b/backend/scripts/acceptance/chr-recursive-direct-links.test.mjs new file mode 100644 index 00000000..f989fe19 --- /dev/null +++ b/backend/scripts/acceptance/chr-recursive-direct-links.test.mjs @@ -0,0 +1,17 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { inspectDirectLinks } from './chr-recursive-direct-links.mjs'; + +test('direct evidence requires exact opaque reference and corroborated ifindex namespace', () => { + const route = { 'routing-table': 'main', 'nexthop-id': '*A', 'immediate-gw': '192.0.2.1%ether1' }; + const nexthop = { '.id': '*A', 'immediate-gw.interface-idx': '4' }; + const iface = { id: '9', ifindex: '4', name: 'ether1' }; + const table = { '.id': '*0', name: 'main' }; + assert.deepEqual(inspectDirectLinks([route], [nexthop], [iface], [table]), { + opaqueReferences: 1, namedInterfaceCorroborations: 1, interfaceIdSubstitutionRefusals: 1, tableNames: 1, recursiveLinkRows: 0, + }); + assert.throws(() => inspectDirectLinks([{ ...route, 'nexthop-id': '*a' }], [nexthop], [iface], [table]), /exact opaque/); + assert.throws(() => inspectDirectLinks([{ ...route, 'routing-table': '0' }], [nexthop], [iface], [table]), /named routing table/); + assert.throws(() => inspectDirectLinks([route], [nexthop], [{ ...iface, id: '4', ifindex: '9' }], [table]), /corroboration/); + assert.throws(() => inspectDirectLinks([route], [nexthop], [iface, { ...iface, id: '3' }], [table]), /ambiguous ifindex/); +}); diff --git a/backend/scripts/acceptance/chr-recursive-observation.mjs b/backend/scripts/acceptance/chr-recursive-observation.mjs new file mode 100644 index 00000000..95f06378 --- /dev/null +++ b/backend/scripts/acceptance/chr-recursive-observation.mjs @@ -0,0 +1,177 @@ +import { request as httpRequest } from 'node:http'; +import { request as httpsRequest } from 'node:https'; +import { createHash } from 'node:crypto'; +import { mkdir, realpath, lstat, writeFile } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..'); +export const SOURCES = Object.freeze({ + '194.226.121.150': Object.freeze({ serverId: 1, port: 19081 }), + '104.165.244.7': Object.freeze({ serverId: 2, port: 19082 }), +}); +export const PATHS = Object.freeze([ + '/system/resource', '/routing/route', '/routing/nexthop', '/interface', + '/routing/table', '/ip/vrf', '/routing/nexthop', '/routing/route', '/system/resource', +]); +const LIMITS = Object.freeze({ response: 1048576, bytes: 8388608, requests: 256, rows: 1000, totalRows: 10000, duration: 120000 }); +const sha = value => createHash('sha256').update(value).digest('hex'); +const deny = code => { throw new Error(code); }; + +export function configuration(args) { + const options = {}; + for (let i = 0; i < args.length; i += 2) { + if (!['--source', '--transport', '--output', '--owned-tunnel-attestation'].includes(args[i]) || !args[i + 1] || options[args[i]] !== undefined) deny('invalid-options'); + options[args[i]] = args[i + 1]; + } + const source = options['--source'], transport = options['--transport']; + if (!Object.hasOwn(SOURCES, source) || !['https', 'ssh-loopback'].includes(transport) || !options['--output']) deny('invalid-source-or-transport'); + const attestation = options['--owned-tunnel-attestation']; + if (transport === 'ssh-loopback' && attestation !== 'verified-known-host-161.104.46.222-owned-forward') deny('owned-tunnel-attestation-required'); + if (transport === 'https' && attestation !== undefined) deny('irrelevant-tunnel-attestation'); + return { source, serverId: SOURCES[source].serverId, transport, output: options['--output'], + endpoint: transport === 'https' ? `https://${source}:443` : `http://127.0.0.1:${SOURCES[source].port}`, + tunnelAttestation: attestation ?? null }; +} + +export async function privateDirectory(output, root = ROOT) { + // Reject symlinks/junctions at every existing component, including tmp itself. + const base = path.resolve(root, 'tmp'), target = path.resolve(root, output); + const relative = path.relative(base, target); + if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) deny('output-must-be-inside-tmp'); + await mkdir(base, { recursive: true, mode: 0o700 }); + if ((await lstat(base)).isSymbolicLink() || await realpath(base) !== base) deny('output-symlink-refused'); + let current = base; + for (const part of relative.split(path.sep)) { + current = path.join(current, part); + try { await mkdir(current, { mode: 0o700 }); } catch (error) { if (error.code !== 'EEXIST') throw error; } + const stat = await lstat(current); + if (!stat.isDirectory() || stat.isSymbolicLink() || await realpath(current) !== current) deny('output-symlink-refused'); + } + // Exclusive claim prevents mixing runs and refuses existing captures. + await writeFile(path.join(target, 'capture.claim'), 'chr-recursive-observation-v1\n', { flag: 'wx', mode: 0o600 }); + return target; +} + +export function decodeBody(body) { + let text, payload; + try { text = new TextDecoder('utf-8', { fatal: true }).decode(body); payload = JSON.parse(text); } catch { deny('invalid-utf8-or-json'); } + const rows = Array.isArray(payload) ? payload : payload && typeof payload === 'object' ? [payload] : deny('invalid-row-shape'); + if (rows.length > LIMITS.rows || rows.some(row => !row || typeof row !== 'object' || Array.isArray(row))) deny('row-limit-or-shape'); + return { rows }; +} + +export function readResponse(endpoint, resource, authorization, budget) { + const allowed = Object.entries(SOURCES).flatMap(([host, item]) => [`https://${host}:443`, `http://127.0.0.1:${item.port}`]); + if (!allowed.includes(endpoint)) deny('non-whitelisted-endpoint'); + if (!PATHS.includes(resource) || budget.bytes >= LIMITS.bytes || budget.rows >= LIMITS.totalRows || ++budget.requests > LIMITS.requests || Date.now() >= budget.deadline) deny('request-budget'); + const url = new URL('/rest' + resource, endpoint); + return new Promise((resolve, reject) => { + const fail = (code, status) => reject(Object.assign(new Error(code), Number.isInteger(status) ? { httpStatus: status } : {})); + const req = (url.protocol === 'https:' ? httpsRequest : httpRequest)(url, { + method: 'GET', agent: false, rejectUnauthorized: true, maxHeaderSize: 16384, + headers: { Authorization: authorization, Accept: 'application/json', 'Accept-Encoding': 'identity' }, + }, res => { + const chunks = []; let size = 0; + res.on('data', chunk => { + size += chunk.length; budget.bytes += chunk.length; + if (size > LIMITS.response || budget.bytes > LIMITS.bytes) { req.destroy(); fail('byte-limit'); return; } + chunks.push(chunk); + }); + res.on('aborted', () => fail('response-aborted')); + res.on('error', () => fail('response-failed')); + res.on('end', () => { + const body = Buffer.concat(chunks); + // Raw failed HTTP responses are excluded: error bodies can reflect secrets. + if (res.statusCode !== 200) return fail('http-status-refused', res.statusCode); + const type = res.headers['content-type']; + if (typeof type !== 'string' || !/^application\/json(?:\s*;\s*charset=utf-8)?$/i.test(type) || res.headers['content-encoding'] && res.headers['content-encoding'] !== 'identity') return fail('response-encoding-refused'); + try { + const { rows } = decodeBody(body); + budget.rows += rows.length; + if (budget.rows > LIMITS.totalRows || Date.now() >= budget.deadline) return fail('response-budget'); + resolve({ body, rows, status: res.statusCode, contentType: type }); + } catch { fail('invalid-response'); } + }); + }); + const timer = setTimeout(() => { req.destroy(); fail('absolute-deadline'); }, Math.max(1, budget.deadline - Date.now())); + req.on('error', () => fail('transport-failed')); + req.on('close', () => clearTimeout(timer)); + req.end(); + }); +} + +export async function acquire(config, credentials) { + if (typeof credentials?.username !== 'string' || !credentials.username || credentials.username.length > 256 || /[:\r\n]/.test(credentials.username) || typeof credentials.password !== 'string' || !credentials.password || credentials.password.length > 4096 || /[\r\n]/.test(credentials.password)) deny('invalid-private-credentials'); + // Validate caller configuration again; exported API cannot supply a custom host. + const fixed = configuration(['--source', config.source, '--transport', config.transport, '--output', config.output, + ...(config.tunnelAttestation ? ['--owned-tunnel-attestation', config.tunnelAttestation] : [])]); + const directory = await privateDirectory(fixed.output); + const budget = { requests: 0, bytes: 0, rows: 0, deadline: Date.now() + LIMITS.duration }; + const authorization = 'Basic ' + Buffer.from(credentials.username + ':' + credentials.password).toString('base64'); + const report = { format: 'chr-recursive-raw-acquisition-v1', source: fixed.source, serverId: fixed.serverId, + transport: fixed.transport, tunnelAttestation: fixed.tunnelAttestation, startedAt: new Date().toISOString(), + semanticProof: false, complete: false, snapshotBracket: null, records: [], limits: LIMITS }; + let version, attemptedResource; + try { + for (const [index, resource] of PATHS.entries()) { + attemptedResource = resource; + const startedAt = new Date().toISOString(); + const response = await readResponse(fixed.endpoint, resource, authorization, budget); + // Never persist a body reflecting the private credential or Basic value. + if ([credentials.password, authorization].some(secret => response.body.includes(Buffer.from(secret)))) deny('credential-reflection-refused'); + if (resource === '/system/resource') { + const current = response.rows.length === 1 && response.rows[0].version; + if (typeof current !== 'string' || !/^7\.2[0-6](?:\.\d{1,3})?(?: \((?:stable|long-term)\))?$/.test(current) || version && version !== current) deny('unsupported-or-drifting-version'); + version = current; + } + const file = `${String(index + 1).padStart(2, '0')}-${resource.slice(1).replaceAll('/', '-')}.json`; + await writeFile(path.join(directory, file), response.body, { flag: 'wx', mode: 0o600 }); + report.records.push({ file, resource, startedAt, finishedAt: new Date().toISOString(), version, + status: response.status, contentType: response.contentType, bytes: response.body.length, rows: response.rows.length, rawSha256: sha(response.body) }); + } + const hashFor = resource => report.records.filter(record => record.resource === resource).map(record => record.rawSha256); + const [firstRoute, lastRoute] = hashFor('/routing/route'); + const [firstNexthop, lastNexthop] = hashFor('/routing/nexthop'); + report.snapshotBracket = { + routeByteEqual: firstRoute === lastRoute, + nexthopByteEqual: firstNexthop === lastNexthop, + }; + report.complete = true; + } catch (error) { + report.failure = 'acquisition-incomplete'; + report.failedResource = attemptedResource; + if (Number.isInteger(error.httpStatus) && error.httpStatus >= 100 && error.httpStatus <= 599) report.failedHttpStatus = error.httpStatus; + } + report.finishedAt = new Date().toISOString(); + report.usage = { requests: budget.requests, bytes: budget.bytes, rows: budget.rows }; + await writeFile(path.join(directory, 'report.json'), JSON.stringify(report, null, 2) + '\n', { flag: 'wx', mode: 0o600 }); + return report; +} + +async function privateStdin() { + // Piped stdin is private (no terminal echo). TTY raw mode hides all input. + const input = process.stdin, chunks = []; let size = 0; + if (input.isTTY) { process.stderr.write('Private credentials JSON (hidden), then Enter: '); input.setRawMode(true); } + try { + for await (const chunk of input) { + size += chunk.length; + if (size > 8192) deny('private-input-limit'); + if (input.isTTY && chunk.includes(3)) deny('private-input-cancelled'); + chunks.push(chunk); + if (input.isTTY && (chunk.includes(13) || chunk.includes(10))) break; + } + return JSON.parse(Buffer.concat(chunks).toString('utf8').trim()); + } finally { if (input.isTTY) { input.setRawMode(false); process.stderr.write('\n'); } } +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + try { + const config = configuration(process.argv.slice(2)); + const report = await acquire(config, await privateStdin()); + // Stdout contains metadata only; no native bodies, credentials or errors. + process.stdout.write(JSON.stringify({ complete: report.complete, snapshotBracket: report.snapshotBracket, + semanticProof: false, source: report.source, records: report.records.length }) + '\n'); + if (!report.complete) process.exitCode = 1; + } catch { process.stderr.write('Read-only acquisition refused or incomplete.\n'); process.exitCode = 1; } +} diff --git a/backend/scripts/acceptance/chr-recursive-observation.test.mjs b/backend/scripts/acceptance/chr-recursive-observation.test.mjs new file mode 100644 index 00000000..08b840c7 --- /dev/null +++ b/backend/scripts/acceptance/chr-recursive-observation.test.mjs @@ -0,0 +1,96 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { createServer } from 'node:http'; +import { randomUUID, createHash } from 'node:crypto'; +import { readFile, readdir } from 'node:fs/promises'; +import { fileURLToPath } from 'node:url'; +import { configuration, decodeBody, readResponse, acquire, privateDirectory, PATHS } from './chr-recursive-observation.mjs'; + +test('source/transport allowlist refuses arbitrary hosts, plaintext external, missing tunnel attestation and option injection', () => { + const args = ['--source', '194.226.121.150', '--transport', 'https', '--output', 'tmp/acquisition-test']; + assert.equal(configuration(args).endpoint, 'https://194.226.121.150:443'); + for (const changes of [args.map(x => x === '194.226.121.150' ? 'example.org' : x), args.map(x => x === 'https' ? 'http' : x), + args.map(x => x === 'https' ? 'ssh-loopback' : x), [...args, '--password', 'forbidden'], [...args, '--source', '104.165.244.7']]) assert.throws(() => configuration(changes)); + const tunnel = configuration([...args.map(x => x === 'https' ? 'ssh-loopback' : x), '--owned-tunnel-attestation', 'verified-known-host-161.104.46.222-owned-forward']); + assert.equal(tunnel.endpoint, 'http://127.0.0.1:19081'); +}); + +test('full own fixture saves nine exact immutable captures and metadata; reflected errors never enter reports', async () => { + const output = fileURLToPath(new URL('../../../tmp/chr-recursive-own-fixture-' + randomUUID(), import.meta.url)), seen = []; + let reflect = false, driftingResource; + const server = createServer((req, res) => { + seen.push(req.url); res.setHeader('Content-Type', 'application/json'); + if (reflect) { res.statusCode = 401; res.end('{"message":"own-fixture-secret-only"}'); return; } + if (req.url === '/rest/system/resource') res.end('[{"version":"7.26"}]\n'); + else if (req.url === driftingResource && seen.indexOf(req.url) !== seen.length - 1) res.end('[{".id":"*2"}]\n'); + else res.end('[{".id":"*1"}]\n'); + }); + await new Promise((resolve, reject) => { server.once('error', reject); server.listen(19081, '127.0.0.1', resolve); }); + const makeConfig = destination => configuration(['--source', '194.226.121.150', '--transport', 'ssh-loopback', '--output', destination, + '--owned-tunnel-attestation', 'verified-known-host-161.104.46.222-owned-forward']); + try { + const report = await acquire(makeConfig(output), { username: 'own-fixture', password: 'own-fixture-secret-only' }); + assert.equal(report.complete, true); assert.equal(report.semanticProof, false); + assert.deepEqual(report.snapshotBracket, { routeByteEqual: true, nexthopByteEqual: true }); + assert.deepEqual(seen, PATHS.map(resource => '/rest' + resource)); + for (const record of report.records) { + const body = await readFile(output + '/' + record.file); + assert.equal(createHash('sha256').update(body).digest('hex'), record.rawSha256); + assert.equal(body.length, record.bytes); assert.equal(record.version, '7.26'); + } + await assert.rejects(acquire(makeConfig(output), { username: 'own-fixture', password: 'own-fixture-secret-only' })); + assert.equal(seen.length, 9); + await assert.rejects(privateDirectory('../outside-tmp')); + for (const resource of ['route', 'nexthop']) { + seen.length = 0; + driftingResource = '/rest/routing/' + resource; + const driftOutput = output + '-' + resource; + const drift = await acquire(makeConfig(driftOutput), { username: 'own-fixture', password: 'own-fixture-secret-only' }); + assert.equal(drift.complete, true); + assert.equal(drift.semanticProof, false); + assert.deepEqual(drift.snapshotBracket, { routeByteEqual: resource !== 'route', nexthopByteEqual: resource !== 'nexthop' }); + assert.equal(drift.records.length, 9); + assert.deepEqual(JSON.parse(await readFile(driftOutput + '/report.json', 'utf8')).snapshotBracket, drift.snapshotBracket); + } + reflect = true; + const failedOutput = output + '-failed'; + const failed = await acquire(makeConfig(failedOutput), { username: 'own-fixture', password: 'own-fixture-secret-only' }); + assert.equal(failed.complete, false); assert.equal(failed.records.length, 0); + assert.equal(failed.snapshotBracket, null); + assert.equal(failed.failedResource, '/system/resource'); assert.equal(failed.failedHttpStatus, 401); + assert.deepEqual((await readdir(failedOutput)).sort(), ['capture.claim', 'report.json']); + assert(!String(await readFile(failedOutput + '/report.json')).includes('own-fixture-secret-only')); + } finally { await new Promise(resolve => server.close(resolve)); } +}); + +test('exact UTF8 and row bounds fail closed', () => { + assert.equal(decodeBody(Buffer.from('[{"version":"7.26"}]')).rows.length, 1); + for (const body of [Buffer.from([0xff]), Buffer.from('null'), Buffer.from('[[]]'), Buffer.from(JSON.stringify(Array.from({ length: 1001 }, () => ({}))))]) assert.throws(() => decodeBody(body)); +}); + +test('own loopback fixture observes exact GET paths, refuses redirect/error/encoding/bytes, no retry or follow', async () => { + let mode = 'normal', requests = 0; + const server = createServer((req, res) => { + requests++; + assert.equal(req.method, 'GET'); assert.equal(req.url, '/rest/system/resource'); + res.setHeader('Content-Type', 'application/json'); + if (mode === 'redirect') { res.statusCode = 302; res.setHeader('Location', 'http://example.invalid'); res.end('[]'); } + else if (mode === 'error') { res.statusCode = 401; res.end('{"error":"reflected-private-input"}'); } + else if (mode === 'encoding') { res.setHeader('Content-Encoding', 'gzip'); res.end('[]'); } + else if (mode === 'oversized') res.end(' '.repeat(1048577)); + else res.end('[{"version":"7.26"}]\n'); + }); + await new Promise((resolve, reject) => { server.once('error', reject); server.listen(19081, '127.0.0.1', resolve); }); + const budget = () => ({ requests: 0, bytes: 0, rows: 0, deadline: Date.now() + 5000 }); + try { + const first = await readResponse('http://127.0.0.1:19081', PATHS[0], 'Basic own-fixture', budget()); + assert.equal(first.body.toString(), '[{"version":"7.26"}]\n'); + for (mode of ['redirect', 'error', 'encoding', 'oversized']) await assert.rejects(readResponse('http://127.0.0.1:19081', PATHS[0], 'Basic own-fixture', budget())); + assert.equal(requests, 5); + assert.throws(() => readResponse('http://194.226.121.150', PATHS[0], '', budget())); + assert.throws(() => readResponse('http://127.0.0.1:19081', '/system/reboot', '', budget())); + assert.throws(() => readResponse('http://127.0.0.1:19081', PATHS[0], '', { ...budget(), requests: 256 })); + assert.throws(() => readResponse('http://127.0.0.1:19081', PATHS[0], '', { ...budget(), deadline: 0 })); + assert.equal(requests, 5); + } finally { await new Promise(resolve => server.close(resolve)); } +}); diff --git a/backend/scripts/acceptance/installation-memory-budget.mjs b/backend/scripts/acceptance/installation-memory-budget.mjs new file mode 100644 index 00000000..4fbcf8f8 --- /dev/null +++ b/backend/scripts/acceptance/installation-memory-budget.mjs @@ -0,0 +1,155 @@ +/** Arithmetic preflight only. No Docker/host access, changes or workload admission. + * Roles are operator-supplied labels, not authenticated service identities. + * Even a fitting budget cannot prove runtime <=2GB or permission to change limits. */ +const MiB = 1024 * 1024 +const roles = new Set(['backend', 'postgres', 'frontend', 'portal', 'proxy', 'agent', 'other']) +const singleton = new Set(['backend', 'postgres', 'frontend', 'portal']) +const integer = value => Number.isSafeInteger(value) && value >= 0 +const exact = (value, fields) => value !== null && typeof value === 'object' && !Array.isArray(value) + && Object.keys(value).sort().join('|') === [...fields].sort().join('|') +const id = value => typeof value === 'string' && /^[a-f0-9]{64}$/.test(value) + +export function evaluateInstallationMemoryBudget(input) { + if (!exact(input, ['hostMemTotalBytes', 'osReserveBytes', 'observedContainerIds', 'containers']) + || !integer(input.hostMemTotalBytes) || !integer(input.osReserveBytes) + || !Array.isArray(input.observedContainerIds) || !Array.isArray(input.containers) + || input.observedContainerIds.length > 128 || input.containers.length > 128) throw Error('Invalid bounded installation inventory') + if (input.observedContainerIds.some(value => !id(value)) || new Set(input.observedContainerIds).size !== input.observedContainerIds.length) throw Error('Invalid observed container set') + const seen = new Set(), counts = new Map(), reasons = new Set() + let configuredBytes = 0 + for (const row of input.containers) { + if (!exact(row, ['id', 'role', 'configuredLimitBytes']) || !id(row.id) || !roles.has(row.role) + || !integer(row.configuredLimitBytes) || seen.has(row.id)) throw Error('Invalid container budget') + seen.add(row.id); counts.set(row.role, (counts.get(row.role) ?? 0) + 1) + if (row.configuredLimitBytes === 0) reasons.add('UNBOUNDED_CONTAINER') + if (row.configuredLimitBytes > input.hostMemTotalBytes) reasons.add('CONTAINER_LIMIT_EXCEEDS_HOST') + if (!Number.isSafeInteger(configuredBytes + row.configuredLimitBytes)) throw Error('Container budget overflow') + configuredBytes += row.configuredLimitBytes + } + if (seen.size !== input.observedContainerIds.length || input.observedContainerIds.some(value => !seen.has(value))) reasons.add('INCOMPLETE_HOST_CONTAINER_INVENTORY') + for (const role of ['backend', 'postgres', 'frontend']) if (!counts.has(role)) reasons.add('REQUIRED_SERVICE_MISSING') + for (const role of singleton) if ((counts.get(role) ?? 0) > 1) reasons.add('DUPLICATE_APPLICATION_STACK') + if (input.hostMemTotalBytes === 0 || input.hostMemTotalBytes > 2 * 1024 * MiB) reasons.add('HOST_OUTSIDE_2GIB_TARGET') + if (input.osReserveBytes < 512 * MiB) reasons.add('OS_RESERVE_BELOW_CANDIDATE_FLOOR') + if (!Number.isSafeInteger(configuredBytes + input.osReserveBytes)) throw Error('Installation budget overflow') + const plannedBytes = configuredBytes + input.osReserveBytes + if (plannedBytes > input.hostMemTotalBytes) reasons.add('INSTALLATION_BUDGET_EXCEEDS_HOST') + return Object.freeze({ scope: 'arithmetic-preflight-only', budgetFits: reasons.size === 0, + wholeInstallation2GbAccepted: false, runtimeAcceptanceClaimed: false, changesAuthorized: false, + configuredContainerBytes: configuredBytes, osReserveBytes: input.osReserveBytes, + plannedBytes, remainingBytes: input.hostMemTotalBytes - plannedBytes, + observedContainers: input.observedContainerIds.length, reasons: Object.freeze([...reasons].sort()) }) +} + +/** Project the whitelist-only read-only inventory. Never substitute idle RSS or + * Docker's reported host ceiling for HostConfig.Memory. Labels/freshness are + * not authenticated here; this result must not be used as mutation authority. */ +export function evaluateObservedInstallationBudget(inventory, roleById, osReserveBytes = 512 * MiB) { + if (!inventory || inventory.readOnly !== true || inventory.containerSetStable !== true + || inventory.environmentRead !== false || inventory.serviceChanges !== false || inventory.newFixtureStarted !== false + || !Array.isArray(inventory.containers) || inventory.containers.length > 128 + || !roleById || typeof roleById !== 'object' || Array.isArray(roleById)) throw Error('Invalid read-only installation inventory') + const containers = inventory.containers.map(row => { + if (!row || !id(row.id) || row.running !== true || !integer(row.memoryLimitBytes) + || !/^sha256:[a-f0-9]{64}$/.test(row.imageId) || !Number.isFinite(Date.parse(row.startedAt))) throw Error('Incomplete configured-limit observation') + const label = Object.getOwnPropertyDescriptor(roleById, row.id) + if (!label || typeof label.value !== 'string' || !roles.has(label.value)) throw Error('Explicit container role missing') + return { id: row.id, role: label.value, configuredLimitBytes: row.memoryLimitBytes } + }) + const observedContainerIds = containers.map(row => row.id) + if (Object.keys(roleById).length !== observedContainerIds.length || Object.keys(roleById).some(key => !observedContainerIds.includes(key))) throw Error('Foreign or incomplete role inventory') + const budget = evaluateInstallationMemoryBudget({ hostMemTotalBytes: inventory.hostMemTotalBytes, osReserveBytes, observedContainerIds, containers }) + return Object.freeze({ ...budget, configuredLimitsSource: 'observed-HostConfig.Memory', roleLabelsAuthenticated: false, inventoryFreshnessProven: false }) +} + +/** NEW pure raw-text bridge for measure-memory schema2/3. This is NOT the + * separate GET-only inventory contract consumed by evaluateObserved above. + * No file/container/socket access or mutation authority. Parse raw text only: + * precision lost by a caller's previous JSON.parse cannot be recovered. */ +export function evaluateMeasuredInstallationBudget(rawText, roleById, osReserveBytes = 512 * MiB) { + if (typeof rawText !== 'string') throw Error('Raw measurement JSON text required; preparsed precision cannot be recovered') + if (Buffer.byteLength(rawText, 'utf8') > 32 * 1024 * 1024) throw Error('Measurement text bound exceeded') + // A narrow numeric lexical gate BEFORE JSON.parse, not a JSON parser. The + // Python producer emits only integer JSON numbers (PSI averages are strings). + // Reject decimal/exponent/unsafe integers even where JSON.parse would round + // a fractional lexeme into a seemingly safe integer. JSON.parse checks syntax. + const number = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/y + let quoted = false, escaped = false + for (let index = 0; index < rawText.length; index++) { + const char = rawText[index] + if (quoted) { if (escaped) escaped = false; else if (char === '\\') escaped = true; else if (char === '"') quoted = false; continue } + if (char === '"') { quoted = true; continue } + if (char === '-' || char >= '0' && char <= '9') { + number.lastIndex = index; const token = number.exec(rawText)?.[0] + if (!token || token === '-0' || !/^-?(?:0|[1-9][0-9]*)$/.test(token) || token.replace('-', '').length > 16) throw Error('Unsafe or noninteger measurement JSON number') + const value = BigInt(token) + if (value < -9007199254740991n || value > 9007199254740991n) throw Error('Unsafe measurement JSON integer') + index = number.lastIndex - 1 + } + } + const data = JSON.parse(rawText) + if (!data || ![2, 3].includes(data.schemaVersion) || data.readOnlyDockerAndProc !== true || data.serviceChanges !== 0 + || data.summary?.acceptancePassed !== false || !Array.isArray(data.observations) || !data.observations.length || data.observations.length > 300 + || !roleById || typeof roleById !== 'object' || Array.isArray(roleById)) throw Error('Unsupported or incomplete memory measurement') + const nonnegativeSafe = value => Number.isSafeInteger(value) && value >= 0 + const limit = value => value === 'max' || nonnegativeSafe(value) ? value : null + const swap = row => row.configuredMemorySwapStatus === 'available' && Number.isSafeInteger(row.configuredMemorySwapBytes) && row.configuredMemorySwapBytes >= -1 ? row.configuredMemorySwapBytes : null + const cpu = value => value && ['nr_periods', 'nr_throttled', 'throttled_usec'].every(key => nonnegativeSafe(value[key])) + ? Object.fromEntries(['nr_periods', 'nr_throttled', 'throttled_usec'].map(key => [key, value[key]])) : null + const pressure = host => { + if (host.memoryPressure?.status !== 'available') return null + const result = {} + for (const key of ['some', 'full']) { + const value = host.memoryPressure.metrics?.[key] + if (!value || !nonnegativeSafe(value.totalMicros)) return null + for (const avg of ['avg10Percent', 'avg60Percent', 'avg300Percent']) { + if (typeof value[avg] !== 'string' || !/^[0-9]{1,3}\.[0-9]{2}$/.test(value[avg]) || Number(value[avg]) > 100) return null + } + result[key] = Object.fromEntries(['avg10Percent', 'avg60Percent', 'avg300Percent', 'totalMicros'].map(name => [name, value[name]])) + } + return result + } + let baseline, hostMemory, priorElapsed = -1, complete = data.schemaVersion === 3 + const extendedObservations = [] + for (const observation of data.observations) { + if (!observation?.host || !nonnegativeSafe(observation.elapsedMs) || observation.elapsedMs < priorElapsed || observation.elapsedMs > 360000 + || !nonnegativeSafe(observation.host.MemTotalBytes) || !observation.host.MemTotalBytes + || !Array.isArray(observation.containers) || !observation.containers.length || observation.containers.length > 16) throw Error('Incomplete bounded measurement sample') + priorElapsed = observation.elapsedMs + hostMemory ??= observation.host.MemTotalBytes + if (hostMemory !== observation.host.MemTotalBytes) throw Error('Measurement host memory changed') + const ids = new Set(), identities = [], extended = [] + for (const row of observation.containers) { + if (!row || !id(row.id) || ids.has(row.id) || !/^sha256:[a-f0-9]{64}$/.test(row.imageId) || (typeof row.startedAt !== 'string' || !Number.isFinite(Date.parse(row.startedAt))) + || row.running !== true || row.runningAfter !== true || row.identityStableDuringSample !== true || row.oomKilled !== false || row.oomKilledAfter !== false + || !nonnegativeSafe(row.restartCount) || !Number.isSafeInteger(row.pid) || row.pid < 1 || row.pid > 2147483647 || !nonnegativeSafe(row.configuredLimitBytes)) throw Error('Unavailable measurement container identity or configured cap') + ids.add(row.id); identities.push([row.id, row.imageId, row.startedAt, row.pid, row.restartCount, row.configuredLimitBytes, data.schemaVersion === 3 ? swap(row) : null]) + if (data.schemaVersion === 3) { + const metrics = row.cgroupV2?.metrics, memoryMax = limit(metrics?.['memory.max']), swapMax = limit(metrics?.['memory.swap.max']) + const cpuThrottling = cpu(metrics?.['cpu.stat']), configuredMemorySwapBytes = swap(row) + const available = row.cgroupV2?.status === 'available' && row.resourceLimitsStableDuringSample === true + && memoryMax !== null && swapMax !== null && cpuThrottling !== null && configuredMemorySwapBytes !== null + complete &&= available + extended.push({id: row.id, memoryMax, swapMax, configuredMemorySwapBytes, cpuThrottling, available}) + } else extended.push({id: row.id, memoryMax: null, swapMax: null, configuredMemorySwapBytes: null, cpuThrottling: null, available: false}) + } + identities.sort((a, b) => a[0].localeCompare(b[0])) + const encoded = JSON.stringify(identities) + baseline ??= encoded + if (encoded !== baseline) throw Error('Measurement container set, identity or memory cap changed') + const memoryPressure = data.schemaVersion === 3 ? pressure(observation.host) : null + complete &&= memoryPressure !== null + extendedObservations.push({elapsedMs: observation.elapsedMs, memoryPressure, containers: extended}) + } + const captured = JSON.parse(baseline) + if (Object.keys(roleById).length !== captured.length || Object.keys(roleById).some(key => !captured.some(row => row[0] === key))) throw Error('Incomplete or foreign measured role inventory') + const containers = captured.map(([identifier, _image, _start, _pid, _restart, configuredLimitBytes]) => { + const descriptor = Object.getOwnPropertyDescriptor(roleById, identifier) + if (!descriptor || typeof descriptor.value !== 'string' || !roles.has(descriptor.value)) throw Error('Explicit measured container role missing') + return {id: identifier, role: descriptor.value, configuredLimitBytes} + }) + const budget = evaluateInstallationMemoryBudget({hostMemTotalBytes: hostMemory, osReserveBytes, observedContainerIds: containers.map(row => row.id), containers}) + return Object.freeze({...budget, measurementSchemaVersion: data.schemaVersion, measurementSamples: data.observations.length, + extendedMetricsComplete: complete, extendedObservations: Object.freeze(extendedObservations), + roleLabelsAuthenticated: false, inventoryFreshnessProven: false, hostContainerInventoryComplete: false, measurementSelectedContainersOnly: true, noSwapAccepted: false, sourceGenerationAuthenticated: false}) +} diff --git a/backend/scripts/acceptance/installation-memory-budget.test.mjs b/backend/scripts/acceptance/installation-memory-budget.test.mjs new file mode 100644 index 00000000..0fa4270b --- /dev/null +++ b/backend/scripts/acceptance/installation-memory-budget.test.mjs @@ -0,0 +1,46 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { evaluateInstallationMemoryBudget as evaluate } from './installation-memory-budget.mjs' +const MiB = 1024 * 1024 +function fixture() { + const containers = [['backend',512],['postgres',384],['frontend',160],['portal',192]].map(([role, limit], i) => ({ id: String(i+1).repeat(64), role, configuredLimitBytes: limit * MiB })) + return { hostMemTotalBytes: 2021212 * 1024, osReserveBytes: 512 * MiB, observedContainerIds: containers.map(row => row.id), containers } +} +test('complete four-service candidate fits arithmetic, never runtime acceptance or change permission', () => { + const input = fixture(), before = structuredClone(input), result = evaluate(input) + assert.equal(result.budgetFits,true); assert.equal(result.plannedBytes,1760 * MiB) + assert.equal(result.wholeInstallation2GbAccepted,false); assert.equal(result.runtimeAcceptanceClaimed,false); assert.equal(result.changesAuthorized,false) + assert(Object.isFrozen(result)); assert(Object.isFrozen(result.reasons)); assert.deepEqual(input,before) +}) +test('coexisting baseline and owned app/PG cannot represent one installation', () => { + const input = fixture() + for (const [i,role,limit] of [[5,'backend',320],[6,'postgres',192]]) { input.containers.push({id:String(i).repeat(64),role,configuredLimitBytes:limit*MiB}); input.observedContainerIds.push(String(i).repeat(64)) } + const result = evaluate(input) + assert.equal(result.budgetFits,false); assert(result.reasons.includes('DUPLICATE_APPLICATION_STACK')); assert(result.reasons.includes('INSTALLATION_BUDGET_EXCEEDS_HOST')) +}) +test('unknown host containers, missing and foreign inventory entries refuse', () => { + for (const mode of ['omitted','foreign','missing-service']) { + const input = fixture() + if (mode === 'omitted') input.observedContainerIds.push('a'.repeat(64)) + if (mode === 'foreign') input.containers[0].id='b'.repeat(64) + if (mode === 'missing-service') { input.containers.splice(0,1); input.observedContainerIds.splice(0,1) } + const result = evaluate(input); assert.equal(result.budgetFits,false) + assert(result.reasons.includes(mode === 'missing-service'?'REQUIRED_SERVICE_MISSING':'INCOMPLETE_HOST_CONTAINER_INVENTORY')) + } +}) +test('unlimited cap is distinct from observed idle RSS or Docker host limit display', () => { + const input = fixture(); input.containers[0].configuredLimitBytes=0 + assert(evaluate(input).reasons.includes('UNBOUNDED_CONTAINER')) + input.containers[0].configuredLimitBytes=3*1024*MiB + assert(evaluate(input).reasons.includes('CONTAINER_LIMIT_EXCEEDS_HOST')) +}) +test('extra service, reserve and physical host all count toward the same budget', () => { + const input = fixture(); input.containers.push({id:'a'.repeat(64),role:'other',configuredLimitBytes:256*MiB}); input.observedContainerIds.push('a'.repeat(64)) + assert(evaluate(input).reasons.includes('INSTALLATION_BUDGET_EXCEEDS_HOST')) + const low = fixture(); low.osReserveBytes=128*MiB; assert(evaluate(low).reasons.includes('OS_RESERVE_BELOW_CANDIDATE_FLOOR')) + const large = fixture(); large.hostMemTotalBytes=4*1024*MiB; assert(evaluate(large).reasons.includes('HOST_OUTSIDE_2GIB_TARGET')) +}) +test('malformed, duplicate, fractional, oversized or unsafe inventory rejects without I/O', () => { + const mutations = [input=>{input.inject='unknown'},input=>{input.containers[0].role='invented'},input=>{input.containers[0].configuredLimitBytes=-1},input=>{input.containers[0].configuredLimitBytes=0.5},input=>{input.containers[0].id='../foreign'},input=>{input.containers[1].id=input.containers[0].id},input=>{input.observedContainerIds.push(input.observedContainerIds[0])},input=>{input.containers[0].configuredLimitBytes=Number.MAX_SAFE_INTEGER},input=>{input.observedContainerIds=Array(129).fill('a'.repeat(64))}] + for (const mutate of mutations) { const input=fixture();mutate(input);assert.throws(()=>evaluate(input)) } +}) diff --git a/backend/scripts/acceptance/installation-memory-measurement.test.mjs b/backend/scripts/acceptance/installation-memory-measurement.test.mjs new file mode 100644 index 00000000..fdca0423 --- /dev/null +++ b/backend/scripts/acceptance/installation-memory-measurement.test.mjs @@ -0,0 +1,25 @@ +import assert from 'node:assert/strict' +import {test} from 'node:test' +import {evaluateMeasuredInstallationBudget as evaluate,evaluateInstallationMemoryBudget as legacy} from './installation-memory-budget.mjs' +const MiB=1024*1024 +function fixture(version=3){ + const roles=['backend','postgres','frontend','portal'],caps=[512,384,160,192] + const containers=roles.map((role,i)=>({id:String(i+1).repeat(64),imageId:'sha256:'+'a'.repeat(64),startedAt:'2026-11-02T00:00:00Z',pid:i+1,restartCount:0,running:true,runningAfter:true,identityStableDuringSample:true,oomKilled:false,oomKilledAfter:false,configuredLimitBytes:caps[i]*MiB,reportedLimitBytes:2*1024*MiB, + ...(version===3?{configuredMemorySwapBytes:caps[i]*MiB,configuredMemorySwapStatus:'available',resourceLimitsStableDuringSample:true,cgroupV2:{status:'available',metrics:{'memory.max':caps[i]*MiB,'memory.swap.max':0,'cpu.stat':{nr_periods:10,nr_throttled:2,throttled_usec:1234,notWhitelisted:999}}}}:{cgroupV2:{status:'available',metrics:{'memory.current':1,'memory.peak':2}}})})) + const p={avg10Percent:'0.00',avg60Percent:'0.01',avg300Percent:'1.25',totalMicros:123456} + const host={MemTotalBytes:2069721088,MemAvailableBytes:1000000000,pswpin:0,pswpout:0,oom_kill:0,...(version===3?{memoryPressure:{status:'available',metrics:{some:p,full:{...p,totalMicros:0}}}}:{})} + const data={schemaVersion:version,readOnlyDockerAndProc:true,serviceChanges:0,summary:{acceptancePassed:false},observations:[{elapsedMs:0,host,containers},{elapsedMs:1000,host:structuredClone(host),containers:structuredClone(containers)}]} + return {data,roleById:Object.fromEntries(containers.map((c,i)=>[c.id,roles[i]])),raw:()=>JSON.stringify(data)} +} +test('real schema2 producer contract remains arithmetic-only; missing extended measurements stay unknown',()=>{const f=fixture(2),r=evaluate(f.raw(),f.roleById);assert.equal(r.budgetFits,true);assert.equal(r.plannedBytes,1760*MiB);assert.equal(r.measurementSchemaVersion,2);assert.equal(r.extendedMetricsComplete,false);assert.equal(r.extendedObservations[0].memoryPressure,null);assert.equal(r.extendedObservations[0].containers[0].memoryMax,null);assert.equal(r.wholeInstallation2GbAccepted,false)}) +test('schema3 projects only fixed new fields and never converts data to runtime/no-swap authority',()=>{const f=fixture(),r=evaluate(f.raw(),f.roleById);assert.equal(r.extendedMetricsComplete,true);assert.equal(r.measurementSamples,2);assert.equal(r.extendedObservations[0].containers[0].cpuThrottling.throttled_usec,1234);assert.equal(r.extendedObservations[0].memoryPressure.some.avg300Percent,'1.25');assert(!JSON.stringify(r).includes('notWhitelisted'));assert.equal(r.runtimeAcceptanceClaimed,false);assert.equal(r.changesAuthorized,false);assert.equal(r.noSwapAccepted,false);assert.equal(r.inventoryFreshnessProven,false);assert.equal(r.hostContainerInventoryComplete,false);assert.equal(r.measurementSelectedContainersOnly,true)}) +test('numeric unlimited max/zero/-1/daemon-default preserve their different observed meanings',()=>{const f=fixture();for(const o of f.data.observations){const c=o.containers[0];c.cgroupV2.metrics['memory.max']='max';c.cgroupV2.metrics['memory.swap.max']='max';c.configuredMemorySwapBytes=-1}let r=evaluate(f.raw(),f.roleById);assert.equal(r.extendedObservations[0].containers[0].memoryMax,'max');assert.equal(r.extendedObservations[0].containers[0].configuredMemorySwapBytes,-1);assert.equal(r.noSwapAccepted,false);for(const o of f.data.observations)o.containers[0].configuredMemorySwapBytes=0;r=evaluate(f.raw(),f.roleById);assert.equal(r.extendedObservations[0].containers[0].configuredMemorySwapBytes,0)}) +test('missing schema3 fields, partial cgroup and invalid PSI are unknown rather than zero',()=>{for(const mutate of [o=>{delete o.containers[0].configuredMemorySwapBytes},o=>{o.containers[0].cgroupV2.status='partial'},o=>{o.containers[0].cgroupV2.metrics['memory.max']=null},o=>{delete o.containers[0].cgroupV2.metrics['cpu.stat'].nr_periods},o=>{o.host.memoryPressure.metrics.some.avg10Percent='100.01'},o=>{o.host.memoryPressure.status='unavailable'}]){const f=fixture();for(const o of f.data.observations)mutate(o);const r=evaluate(f.raw(),f.roleById);assert.equal(r.extendedMetricsComplete,false);assert.equal(r.wholeInstallation2GbAccepted,false)}}) +test('unsafe uint64 raw lexemes fail before JSON.parse; no rounded Number is recovered',()=>{for(const token of ['9007199254740992','9007199254740993','18446744073709551615','1.0000000000000001','9007199254740991.1','1e0','-0']){const f=fixture(),raw=f.raw().replace('123456',token);assert.throws(()=>evaluate(raw,f.roleById),/Unsafe|noninteger/);assert.throws(()=>evaluate(JSON.parse(raw),f.roleById),/Raw measurement JSON text required/)} }) +test('safe integer boundary is exact and large number-like text inside strings is not parsed as a counter',()=>{const f=fixture();for(const o of f.data.observations)o.host.memoryPressure.metrics.some.totalMicros=Number.MAX_SAFE_INTEGER;f.data.ignored='9007199254740993 1.0000000000000001 \\" escaped';const r=evaluate(f.raw(),f.roleById);assert.equal(r.extendedObservations[0].memoryPressure.some.totalMicros,Number.MAX_SAFE_INTEGER);assert(!JSON.stringify(r).includes('ignored'))}) +test('malformed JSON, unsupported schemas or claimed producer acceptance refuse',()=>{const f=fixture();assert.throws(()=>evaluate('{bad',f.roleById));for(const mutate of [d=>{d.schemaVersion=1},d=>{d.schemaVersion=4},d=>{d.serviceChanges=1},d=>{d.readOnlyDockerAndProc=false},d=>{d.summary.acceptancePassed=true},d=>{d.observations=[]}]){const x=fixture();mutate(x.data);assert.throws(()=>evaluate(x.raw(),x.roleById))}}) +test('full IDs, complete stable container set, OOM/running/process/image/caps stay mandatory',()=>{for(const mutate of [d=>{d.observations[1].containers.pop()},d=>{d.observations[1].containers.push(d.observations[1].containers[0])},d=>{d.observations[1].containers[0].pid++},d=>{d.observations[1].containers[0].runningAfter=false},d=>{d.observations[1].containers[0].oomKilledAfter=true},d=>{d.observations[1].containers[0].configuredLimitBytes++},d=>{d.observations[1].containers[0].configuredMemorySwapBytes++},d=>{d.observations[1].host.MemTotalBytes++},d=>{d.observations[1].containers[0].startedAt=123}]){const f=fixture();mutate(f.data);assert.throws(()=>evaluate(f.raw(),f.roleById))}}) +test('role labels remain exact own DATA descriptors, never an inherited/accessor authority',()=>{for(const kind of ['getter','inherited','missing','foreign']){const f=fixture(),key=f.data.observations[0].containers[0].id;if(kind==='getter')Object.defineProperty(f.roleById,key,{enumerable:true,get(){assert.fail('Getter executed')}});if(kind==='inherited'){const x=Object.create({[key]:'backend'});for(const [id,role]of Object.entries(f.roleById))if(id!==key)x[id]=role;f.roleById=x}if(kind==='missing')delete f.roleById[key];if(kind==='foreign')f.roleById['f'.repeat(64)]='other';assert.throws(()=>evaluate(f.raw(),f.roleById))}}) +test('legacy arithmetic export keeps soft metadata and no-runtime semantics',()=>{const f=fixture(),input={hostMemTotalBytes:2069721088,osReserveBytes:512*MiB,observedContainerIds:Object.keys(f.roleById),containers:f.data.observations[0].containers.map(c=>({id:c.id,role:f.roleById[c.id],configuredLimitBytes:c.configuredLimitBytes}))};assert.equal(legacy(input).budgetFits,true);assert.equal(legacy(input).changesAuthorized,false)}) +import {readFileSync} from 'node:fs' +test('actual base/candidate Python producers interoperate through synthetic raw wire files',()=>{for(const version of [2,3]){const raw=readFileSync(new URL('./schema'+version+'.main.synthetic.json',import.meta.url),'utf8');const r=evaluate(raw,{['a'.repeat(64)]:'backend'});assert.equal(r.measurementSchemaVersion,version);assert.equal(r.extendedMetricsComplete,version===3);assert.equal(r.budgetFits,false);assert.equal(r.wholeInstallation2GbAccepted,false);if(version===3){assert.equal(r.extendedObservations[0].containers[0].configuredMemorySwapBytes,1024);assert.equal(r.extendedObservations[0].memoryPressure.some.totalMicros,123456)}}}) diff --git a/backend/scripts/acceptance/installation-memory-observation.test.mjs b/backend/scripts/acceptance/installation-memory-observation.test.mjs new file mode 100644 index 00000000..32685ba4 --- /dev/null +++ b/backend/scripts/acceptance/installation-memory-observation.test.mjs @@ -0,0 +1,31 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { evaluateObservedInstallationBudget as evaluate } from './installation-memory-budget.mjs' +const MiB=1024*1024 +function fixture() { + const containers=['backend','postgres','frontend','portal'].map((role,index)=>({id:String(index+1).repeat(64),role,running:true,imageId:'sha256:'+'a'.repeat(64),startedAt:'2026-10-28T12:00:00Z',memoryLimitBytes:0,reportedLimitBytes:2*1024*MiB})) + const roleById=Object.fromEntries(containers.map(row=>[row.id,row.role])) + return {inventory:{readOnly:true,containerSetStable:true,environmentRead:false,serviceChanges:false,newFixtureStarted:false,hostMemTotalBytes:2021212*1024,containers},roleById} +} +test('observed zero configured caps stay unbounded despite positive Docker reported host limits',()=>{ + const f=fixture(), result=evaluate(f.inventory,f.roleById) + assert.equal(result.budgetFits,false);assert(result.reasons.includes('UNBOUNDED_CONTAINER'));assert.equal(result.configuredContainerBytes,0) + assert.equal(result.wholeInstallation2GbAccepted,false);assert.equal(result.roleLabelsAuthenticated,false);assert.equal(result.inventoryFreshnessProven,false) +}) +test('complete explicit configured caps can fit arithmetic without becoming runtime acceptance',()=>{ + const f=fixture();[512,384,160,192].forEach((limit,index)=>{f.inventory.containers[index].memoryLimitBytes=limit*MiB}) + const result=evaluate(f.inventory,f.roleById);assert(result.budgetFits);assert.equal(result.plannedBytes,1760*MiB);assert.equal(result.changesAuthorized,false) +}) +test('uncertain snapshots, started fixtures and missing limit/identity fields refuse',()=>{ + for(const mutate of [f=>{f.inventory.containerSetStable=false},f=>{f.inventory.serviceChanges=true},f=>{f.inventory.newFixtureStarted=true},f=>{delete f.inventory.containers[0].memoryLimitBytes},f=>{f.inventory.containers[0].running=false},f=>{f.inventory.containers[0].imageId='unknown'},f=>{f.inventory.containers[0].startedAt='invalid'}]){const f=fixture();mutate(f);assert.throws(()=>evaluate(f.inventory,f.roleById))} +}) +test('missing, foreign, inherited and accessor role labels cannot silently classify a service',()=>{ + for(const mode of ['missing','foreign','inherited','accessor']){ + const f=fixture(),key=f.inventory.containers[0].id + if(mode==='missing')delete f.roleById[key] + if(mode==='foreign')f.roleById['f'.repeat(64)]='other' + if(mode==='inherited'){const labels=Object.create({[key]:'backend'});for(const [id,role]of Object.entries(f.roleById))if(id!==key)labels[id]=role;f.roleById=labels} + if(mode==='accessor')Object.defineProperty(f.roleById,key,{enumerable:true,get(){assert.fail('Role getter must not execute')}}) + assert.throws(()=>evaluate(f.inventory,f.roleById)) + } +}) diff --git a/backend/scripts/acceptance/mcp-routing-forward-observer.mjs b/backend/scripts/acceptance/mcp-routing-forward-observer.mjs new file mode 100644 index 00000000..e5fe5b5b --- /dev/null +++ b/backend/scripts/acceptance/mcp-routing-forward-observer.mjs @@ -0,0 +1,58 @@ +import assert from 'node:assert/strict' +import { setTimeout as delay } from 'node:timers/promises' +import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client' +import { resumeMcpSteps } from './mcp-step-journal.mjs' + +const [phase, port, planId, connectionId] = process.argv.slice(2) +assert(process.send && process.env.MCP_FIXTURE_TOKEN && ['dispatch', 'resume'].includes(phase)) +const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i +assert.match(planId, uuid) +assert.match(connectionId, uuid) +const client = new Client({ name: 'owned-routing-observer', version: '1.0.0' }) +async function call(name, args) { + const reply = await client.callTool({ name, arguments: args }) + assert(!reply.isError, 'MCP fixture call failed: ' + name) + return reply.structuredContent?.result ?? JSON.parse(reply.content[0].text) +} + +try { + await client.connect(new StreamableHTTPClientTransport(new URL(`http://127.0.0.1:${port}/mcp`), { + requestInit: { headers: { Authorization: 'Bearer ' + process.env.MCP_FIXTURE_TOKEN } }, + })) + let operationId + const result = await resumeMcpSteps({ journal: 'routing-forward.jsonl', fixture: `routing-scoped-forward:${connectionId}:${planId}`, steps: [{ + id: 'forward-apply', kind: 'apply', + async run({ idempotencyKey, accept }) { + assert.equal(phase, 'dispatch', 'A resumed observer cannot send apply') + const receipt = await call('mm_change_apply', { planId, idempotencyKey }) + assert.deepEqual([receipt.plan_id, receipt.connection_id], [planId, connectionId]) + accept(receipt.id) + // The supervisor terminates this process after the journal fsync. + process.send(receipt.id) + await new Promise(resolve => process.once('disconnect', resolve)) + throw new Error('Observer was disconnected by its supervisor') + }, + async reconcile({ accepted }) { + assert.equal(phase, 'resume') + assert.ok(accepted) + const deadline = Date.now() + 60_000 + while (Date.now() < deadline) { + const receipt = await call('mm_operation_get', { operationId: accepted }) + assert.deepEqual([receipt.id, receipt.plan_id, receipt.connection_id], [accepted, planId, connectionId]) + if (!['pending', 'running'].includes(receipt.state)) { + assert.deepEqual([receipt.state, receipt.verification], ['applied', 'passed']) + operationId = accepted + return { state: 'verified', evidence: accepted } + } + await delay(400) + } + return { state: 'unknown' } + }, + }] }) + assert.deepEqual(result, { status: 'passed' }) + assert.ok(operationId) + await new Promise((resolve, reject) => process.send(operationId, error => error ? reject(error) : resolve())) +} finally { + await client.close() + if (process.connected) process.disconnect() +} diff --git a/backend/scripts/acceptance/mcp-server-poll-steps.mjs b/backend/scripts/acceptance/mcp-server-poll-steps.mjs new file mode 100644 index 00000000..d8503207 --- /dev/null +++ b/backend/scripts/acceptance/mcp-server-poll-steps.mjs @@ -0,0 +1,84 @@ +import assert from 'node:assert/strict' +import { resumeMcpSteps } from './mcp-step-journal.mjs' + +const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i + +function isProtocolStorageBusy(error) { + const text = error?.data?.text + if (error?.name !== 'SdkHttpError' || error.status !== 503 || + typeof text !== 'string' || Buffer.byteLength(text) > 1024) return false + try { + const body = JSON.parse(text) + return body?.code === 'PROTOCOL_STORAGE_BUSY' + } catch { + return false + } +} + +/** The caller supplies an already registered synthetic test server and owner review. */ +export function resumeServerPoll({ journal, fixture, serverId, connectionId, call, findPlan, findAcceptedOperation, assertApproved }) { + assert(Number.isSafeInteger(serverId) && serverId > 0) + assert.match(connectionId, uuid) + const find = async args => { + const planId = await findPlan?.({ ...args, connectionId, serverId }) + return planId ? { state: 'verified', evidence: planId } : { state: 'unknown' } + } + return resumeMcpSteps({ journal, fixture: `${fixture}:server-${serverId}:connection-${connectionId}`, steps: [ + { id: 'mm_server_poll_plan', kind: 'plan', reconcile: find, async run({ accept }) { + const result = await call('mm_server_poll_plan', { serverId }) + accept(result.planId) + return result.planId + } }, + { id: 'manager.plan-approved', kind: 'read', async run({ evidence }) { + const planId = evidence.get('mm_server_poll_plan') + await assertApproved(planId) + return planId + } }, + { id: 'mm_change_apply', kind: 'apply', + async reconcile(args) { + const id = args.accepted ?? await findAcceptedOperation?.({ + ...args, connectionId, planId: args.evidence.get('mm_server_poll_plan'), + }) + return id ? { state: 'verified', evidence: id } : { state: 'unknown' } + }, + async run({ evidence, idempotencyKey, accept }) { + const planId = evidence.get('mm_server_poll_plan') + const result = await call('mm_change_apply', { planId, idempotencyKey }) + accept(result.id) + return result.id + } }, + { id: 'mm_operation_get', kind: 'read', async run({ evidence }) { + const operationId = evidence.get('mm_change_apply') + let result + try { + result = await call('mm_operation_get', { operationId }) + } catch (error) { + if (!isProtocolStorageBusy(error)) throw error + throw Object.assign(new Error('MCP протокольное хранилище занято', { cause: error }), { + code: 'PROTOCOL_STORAGE_BUSY', + }) + } + assert.equal(result.id, operationId) + assert.equal(result.plan_id, evidence.get('mm_server_poll_plan')) + assert.equal(result.connection_id, connectionId) + if (result.state !== 'applied' || result.verification !== 'passed') { + const code = ['pending', 'running'].includes(result.state) ? 'POLL_PENDING' : 'POLL_UNVERIFIED' + throw Object.assign(new Error('Poll terminal result is unverified'), { code }) + } + assert.equal(result.steps.length, 1) + assert.equal(result.steps[0].state, 'verified') + const receipt = result.steps[0].result + assert.equal(receipt.operationId, operationId) + assert.match(receipt.receiptId, uuid) + assert.equal(receipt.nativeReads, 4) + assert.equal(receipt.complete, true) + assert.equal(receipt.maintenanceComplete, true) + assert.equal(receipt.configurationVerified, true) + assert.equal(receipt.automaticReplay, false) + assert(Number.isSafeInteger(receipt.completedPublications) && receipt.completedPublications >= 2) + assert.equal(receipt.confirmedPublications, receipt.completedPublications) + assert.equal(receipt.snapshot.serverId, serverId) + return operationId + } }, + ] }) +} diff --git a/backend/scripts/acceptance/mcp-step-journal.mjs b/backend/scripts/acceptance/mcp-step-journal.mjs new file mode 100644 index 00000000..810ce148 --- /dev/null +++ b/backend/scripts/acceptance/mcp-step-journal.mjs @@ -0,0 +1,119 @@ +import assert from 'node:assert/strict' +import { createHash } from 'node:crypto' +import { closeSync, constants, fstatSync, fsyncSync, openSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs' +import { isDeepStrictEqual } from 'node:util' + +const key = /^[a-z0-9][a-z0-9_.:-]{0,127}$/i +const evidenceId = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i +const errorCode = /^[A-Z][A-Z0-9_]{0,63}$/ +const validEvidence = (receipt, kind) => receipt === null ? kind === 'read' : typeof receipt === 'string' && evidenceId.test(receipt) + +/** A test journal only. The caller owns the fixture and all reconciliation reads. */ +export async function resumeMcpSteps({ journal, fixture, steps }) { + assert(key.test(fixture)) + const scope = steps.map(({ id, kind }) => ({ id, kind })) + assert(scope.length && new Set(scope.map(step => step.id)).size === scope.length) + assert(scope.every(({ id, kind }) => key.test(id) && ['read', 'plan', 'apply'].includes(kind))) + + // One process owns a journal at a time. A leftover lock requires operator reconciliation. + const lock = openSync(journal + '.lock', 'wx', 0o600) + let records + try { + let fd + try { + fd = openSync(journal, constants.O_RDWR | constants.O_APPEND | constants.O_CREAT | (constants.O_NOFOLLOW ?? 0), 0o600) + assert(fstatSync(fd).isFile(), 'Test journal must be a regular file') + const raw = readFileSync(fd, 'utf8') + assert(!raw || raw.endsWith('\n'), 'Truncated test receipt') + records = raw.trimEnd().split('\n').filter(Boolean).map(line => JSON.parse(line)) + const header = { phase: 'fixture', fixture, steps: scope } + const append = entry => { + writeFileSync(fd, JSON.stringify(entry) + '\n', 'utf8') + fsyncSync(fd) + records.push(entry) + } + if (!records.length) append(header) + assert(isDeepStrictEqual(records[0], header), 'Test fixture or ordered step list changed') + const state = new Map() + let cursor = 0 + for (const entry of records.slice(1)) { + const previous = state.get(entry.id) + assert(scope[cursor]?.id === entry.id, 'Unexpected test receipt order') + if (entry.phase === 'started') { + assert(!previous || previous.phase === 'retryable', 'Unexpected resumed dispatch') + assert(entry.attempt === (previous?.attempt ?? 0) + 1, 'Unexpected test attempt') + } else { + assert(['started', 'accepted', 'failed'].includes(previous?.phase) && entry.attempt === previous.attempt, 'Unbound test receipt') + assert(['passed', 'retryable', 'accepted', 'failed'].includes(entry.phase), 'Unknown test receipt') + if (entry.phase === 'accepted') { + assert(previous.phase === 'started' && scope[cursor].kind !== 'read' && + validEvidence(entry.evidence, scope[cursor].kind), 'Invalid MCP acceptance receipt') + } + if (entry.phase === 'failed') { + assert(previous.phase !== 'failed' && typeof entry.code === 'string' && errorCode.test(entry.code), 'Invalid failure receipt') + assert(entry.accepted === (previous.phase === 'accepted' ? previous.evidence : null), 'MCP acceptance receipt changed') + } + if (entry.phase === 'passed') { + assert(validEvidence(entry.evidence, scope[cursor].kind), 'Invalid stored MCP receipt ID') + const accepted = previous.phase === 'accepted' ? previous.evidence : previous.accepted + assert(!accepted || accepted === entry.evidence, 'MCP acceptance receipt changed') + cursor++ + } + if (entry.phase === 'retryable') { + assert(entry.accepted === null && (previous.phase === 'started' || + previous.phase === 'failed' && previous.accepted === null), 'Cannot retry an accepted MCP step') + } + } + state.set(entry.id, entry) + } + const evidence = new Map([...state].filter(([, entry]) => entry.phase === 'passed').map(([id, entry]) => [id, entry.evidence])) + const attemptKey = (step, attempt) => 'acceptance-' + createHash('sha256') + .update(JSON.stringify([fixture, step.id, step.kind, attempt])).digest('hex') + const record = (step, attempt, phase, details = {}) => append({ id: step.id, attempt, phase, ...details }) + const complete = (step, attempt, receipt) => { + assert(validEvidence(receipt, step.kind), 'Invalid MCP receipt ID') + record(step, attempt, 'passed', { evidence: receipt }) + evidence.set(step.id, receipt) + } + for (const step of steps.slice(cursor)) { + const previous = state.get(step.id) + let attempt = previous?.attempt ?? 0 + if (previous && ['started', 'accepted', 'failed'].includes(previous.phase)) { + const accepted = previous.phase === 'accepted' ? previous.evidence : previous.accepted ?? null + const result = step.kind === 'read' ? { state: 'no-effect' } : + await step.reconcile?.({ attempt, accepted, idempotencyKey: attemptKey(step, attempt), evidence: new Map(evidence) }) + if (result?.state === 'verified') { + assert(!accepted || accepted === result.evidence, 'Reconciled a different MCP receipt') + complete(step, attempt, result.evidence) + continue + } + if (accepted || result?.state !== 'no-effect') return { status: 'reconcile', step: step.id } + record(step, attempt, 'retryable', { accepted }) + } + attempt++ + record(step, attempt, 'started') + let receipt + let accepted = null + try { + receipt = await step.run({ attempt, idempotencyKey: attemptKey(step, attempt), evidence: new Map(evidence), accept(id) { + assert(accepted === null && step.kind !== 'read' && validEvidence(id, step.kind), 'Invalid MCP acceptance receipt') + record(step, attempt, 'accepted', { evidence: id }) + accepted = id + } }) + } catch (error) { + const code = typeof error?.code === 'string' && errorCode.test(error.code) ? error.code : 'TEST_STEP_FAILED' + record(step, attempt, 'failed', { code, accepted }) + return { status: 'failed', step: step.id } + } + assert(!accepted || accepted === receipt, 'Verified a different MCP receipt') + complete(step, attempt, receipt) + } + return { status: 'passed' } + } finally { + if (fd !== undefined) closeSync(fd) + } + } finally { + closeSync(lock) + unlinkSync(journal + '.lock') + } +} diff --git a/backend/scripts/acceptance/mcp-step-journal.test.mjs b/backend/scripts/acceptance/mcp-step-journal.test.mjs new file mode 100644 index 00000000..749c0ec9 --- /dev/null +++ b/backend/scripts/acceptance/mcp-step-journal.test.mjs @@ -0,0 +1,176 @@ +import assert from 'node:assert/strict' +import { appendFileSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import test from 'node:test' +import { resumeMcpSteps } from './mcp-step-journal.mjs' +import { resumeServerPoll } from './mcp-server-poll-steps.mjs' + +const uuid = 'ec4c6a09-3c3b-4a37-a370-c8821321807c' +const connectionId = 'd0d96834-948b-4cea-81d8-d8f9dc5e1429' +const operationId = 'c2cc9f5d-6d62-43b5-a6d2-ba7bedbd4fc1' +const pollReceipt = { operationId, receiptId: uuid, nativeReads: 4, complete: true, + maintenanceComplete: true, configurationVerified: true, automaticReplay: false, + confirmedPublications: 22, completedPublications: 22, snapshot: { serverId: 3, status: 'online' } } +function options(t, steps) { + const dir = mkdtempSync(join(tmpdir(), 'mm-mcp-step-test-')) + t.after(() => rmSync(dir, { recursive: true })) + return { journal: join(dir, 'history.jsonl'), fixture: 'owned-test', steps } +} + +test('resumes the failed read and preserves successful reads and failed attempts', async t => { + const calls = [] + const config = options(t, [ + { id: 'first', kind: 'read', run: async () => { calls.push('first'); return null } }, + { id: 'second', kind: 'read', run: async ({ attempt }) => { + calls.push('second:' + attempt) + if (attempt === 1) throw Error('test failure') + return null + } }, + ]) + assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'second' }) + assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' }) + assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' }) + assert.deepEqual(calls, ['first', 'second:1', 'second:2']) + assert.deepEqual(readFileSync(config.journal, 'utf8').trim().split('\n').map(line => JSON.parse(line).phase), + ['fixture', 'started', 'passed', 'started', 'failed', 'retryable', 'started', 'passed']) +}) + +test('unknown apply awaits reconciliation and a verified receipt advances without resending', async t => { + let sends = 0 + let verified = false + let sentKey + const config = options(t, [ + { id: 'plan', kind: 'plan', run: async () => uuid }, + { id: 'apply', kind: 'apply', run: async ({ evidence, accept, idempotencyKey }) => { + assert.equal(evidence.get('plan'), uuid) + sentKey = idempotencyKey + sends++ + accept(uuid) + throw Error('lost reply') + }, reconcile: async ({ accepted, idempotencyKey }) => { + assert.equal(accepted, uuid) + assert.equal(idempotencyKey, sentKey) + return verified ? { state: 'verified', evidence: uuid } : { state: 'no-effect' } + } }, + { id: 'next', kind: 'read', run: async ({ evidence }) => { assert.equal(evidence.get('apply'), uuid); return null } }, + ]) + assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'apply' }) + assert.deepEqual(await resumeMcpSteps(config), { status: 'reconcile', step: 'apply' }) + verified = true + assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' }) + assert.equal(sends, 1) +}) + +test('cannot resume a different fixture or an altered ordered step list', async t => { + const config = options(t, [{ id: 'read', kind: 'read', run: async () => null }]) + await resumeMcpSteps(config) + await assert.rejects(resumeMcpSteps({ ...config, fixture: 'other' }), /fixture or ordered step/) + await assert.rejects(resumeMcpSteps({ ...config, steps: [{ ...config.steps[0], id: 'other' }] }), /fixture or ordered step/) +}) + +test('refuses a stored retry marker after an accepted apply', async t => { + const config = options(t, [{ id: 'apply', kind: 'apply', async run({ accept }) { + accept(uuid) + throw new Error('lost reply') + } }]) + await resumeMcpSteps(config) + appendFileSync(config.journal, JSON.stringify({ id: 'apply', attempt: 1, phase: 'retryable', accepted: null }) + '\n') + await assert.rejects(resumeMcpSteps(config), /Cannot retry an accepted MCP step/) +}) + +test('the poll scenario resumes owner review and then reads the same accepted operation', async t => { + const { journal, fixture } = options(t, []) + const calls = [] + let approved = false + let running = true + let storageBusy = true + const config = { journal, fixture, serverId: 3, connectionId, assertApproved: async id => { + assert.equal(id, uuid) + if (!approved) throw Error('approval not visible') + }, async call(tool, args) { + calls.push(tool) + if (tool === 'mm_server_poll_plan') { assert.deepEqual(args, { serverId: 3 }); return { planId: uuid } } + if (tool === 'mm_change_apply') { + assert.equal(args.planId, uuid) + assert.match(args.idempotencyKey, /^acceptance-[0-9a-f]{64}$/) + return { id: operationId } + } + assert.deepEqual(args, { operationId }) + if (storageBusy) throw Object.assign(new Error('MCP storage busy'), { + name: 'SdkHttpError', status: 503, data: { text: '{"code":"PROTOCOL_STORAGE_BUSY"}' }, + }) + return { id: operationId, plan_id: uuid, connection_id: connectionId, + ...(running ? { state: 'running' } : { state: 'applied', verification: 'passed', + steps: [{ state: 'verified', result: pollReceipt }] }) } + } } + assert.equal((await resumeServerPoll(config)).step, 'manager.plan-approved') + approved = true + assert.equal((await resumeServerPoll(config)).step, 'mm_operation_get') + storageBusy = false + assert.equal((await resumeServerPoll(config)).step, 'mm_operation_get') + running = false + assert.deepEqual(await resumeServerPoll(config), { status: 'passed' }) + await assert.rejects(resumeServerPoll({ ...config, connectionId: uuid }), /fixture or ordered step/) + assert.deepEqual(calls, ['mm_server_poll_plan', 'mm_change_apply', 'mm_operation_get', 'mm_operation_get', 'mm_operation_get']) + const failures = readFileSync(journal, 'utf8').trim().split('\n').map(line => JSON.parse(line)) + .filter(entry => entry.id === 'mm_operation_get' && entry.phase === 'failed') + assert.deepEqual(failures.map(entry => entry.code), ['PROTOCOL_STORAGE_BUSY', 'POLL_PENDING']) +}) + +test('recovers a lost poll apply response by the exact connection, plan and idempotency key', async t => { + const { journal, fixture } = options(t, []) + let key, sends = 0, wrongBinding = true + const config = { journal, fixture, serverId: 3, connectionId, assertApproved: async () => {}, + async findAcceptedOperation(binding) { + assert.equal(binding.connectionId, connectionId) + assert.equal(binding.planId, uuid) + assert.equal(binding.idempotencyKey, key) + return operationId + }, + async call(tool, args) { + if (tool === 'mm_server_poll_plan') return { planId: uuid } + if (tool === 'mm_change_apply') { + sends++ + key = args.idempotencyKey + throw Error('apply response lost') + } + return { id: operationId, plan_id: uuid, connection_id: wrongBinding ? uuid : connectionId, + state: 'applied', verification: 'passed', steps: [{ state: 'verified', result: pollReceipt }] } + }, + } + assert.equal((await resumeServerPoll(config)).step, 'mm_change_apply') + assert.equal((await resumeServerPoll(config)).step, 'mm_operation_get') + wrongBinding = false + assert.deepEqual(await resumeServerPoll(config), { status: 'passed' }) + assert.equal(sends, 1) +}) + +test('rejects incomplete or misbound terminal poll receipts before recording success', async t => { + const { journal, fixture } = options(t, []) + const calls = [] + const receipts = [ + { ...pollReceipt, operationId: uuid }, + { ...pollReceipt, receiptId: undefined }, + { ...pollReceipt, confirmedPublications: 21 }, + { ...pollReceipt, confirmedPublications: 0, completedPublications: 0 }, + { ...pollReceipt, maintenanceComplete: false }, + { ...pollReceipt, snapshot: { serverId: 4, status: 'online' } }, + pollReceipt, + ] + const config = { journal, fixture, serverId: 3, connectionId, assertApproved: async () => {}, + async call(tool, args) { + calls.push(tool) + if (tool === 'mm_server_poll_plan') return { planId: uuid } + if (tool === 'mm_change_apply') return { id: operationId } + assert.deepEqual(args, { operationId }) + return { id: operationId, plan_id: uuid, connection_id: connectionId, state: 'applied', verification: 'passed', + steps: [{ state: 'verified', result: receipts.shift() }] } + }, + } + for (let i = 0; i < 6; i++) { + assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'mm_operation_get' }) + } + assert.deepEqual(await resumeServerPoll(config), { status: 'passed' }) + assert.deepEqual(calls, ['mm_server_poll_plan', 'mm_change_apply', ...Array(7).fill('mm_operation_get')]) +}) diff --git a/backend/scripts/acceptance/measure-memory-extended.test.py b/backend/scripts/acceptance/measure-memory-extended.test.py new file mode 100644 index 00000000..159bd71b --- /dev/null +++ b/backend/scripts/acceptance/measure-memory-extended.test.py @@ -0,0 +1,193 @@ +"""Synthetic-only: no Docker/socket, real /proc/cgroup reads, process enumeration or main().""" +import copy +import importlib.util +from pathlib import Path +from types import SimpleNamespace +import unittest +from unittest.mock import patch + +AREA = Path(__file__).parent +spec = importlib.util.spec_from_file_location('memory_measurement_extended', AREA / 'measure-memory.py') +m = importlib.util.module_from_spec(spec) +spec.loader.exec_module(m) + +MEMINFO = 'MemTotal: 2048 kB\nMemAvailable: 1024 kB\nSwapTotal: 4096 kB\nSwapFree: 3072 kB\n' +VMSTAT = 'pswpin 20\npswpout 30\noom_kill 0\n' +PSI = 'some avg10=0.00 avg60=1.25 avg300=100.00 total=123456\nfull avg10=0.00 avg60=0.01 avg300=1.00 total=0\n' +ID = 'a' * 64 +CG = '/sys/fs/cgroup/system.slice/docker-' + ID + '.scope' + +class Synthetic(unittest.TestCase): + def setUp(self): + self.reads = [] + self.gets = [] + self.before = {'Id': ID, 'Image': 'sha256:' + 'b' * 64, 'RestartCount': 0, + 'State': {'StartedAt': '2026-11-02T00:00:00Z', 'Pid': 123, 'Running': True, 'OOMKilled': False}, + 'HostConfig': {'Memory': 512, 'MemorySwap': 1024}, 'Config': {'Env': ['SECRET=never-retain']}} + self.stats = {'id': ID, 'memory_stats': {'usage': 100, 'limit': 2048, 'stats': {'inactive_file': 30}}} + self.files = {'/proc/meminfo': MEMINFO, '/proc/vmstat': VMSTAT, '/proc/pressure/memory': PSI, + '/proc/self/mountinfo': '1 0 0:1 / /sys/fs/cgroup rw - cgroup2 cgroup rw\n', + '/proc/123/cgroup': '0::/system.slice/docker-' + ID + '.scope\n', + '/proc/123/stat': '123 (owned fixture) S ' + ' '.join(['0'] * 18 + ['987'] + ['0'] * 8), + CG + '/memory.current': '100\n', CG + '/memory.peak': '200\n', + CG + '/memory.events': 'low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\n', + CG + '/memory.swap.current': '0\n', CG + '/memory.swap.peak': '0\n', + CG + '/memory.swap.events': 'high 0\nmax 0\nfail 0\n', + CG + '/memory.max': '512\n', CG + '/memory.swap.max': '512\n', + CG + '/cpu.stat': 'usage_usec 50\nuser_usec 30\nsystem_usec 20\nnr_periods 10\nnr_throttled 2\nthrottled_usec 1234\nnr_bursts 0\n'} + def deny(*_args, **_kwargs): + raise AssertionError('Real external/OS I/O is forbidden by synthetic test') + # Module-local OS proxy avoids changing unittest/Python's filesystem operations. + fake_os = SimpleNamespace(**vars(m.os)) + fake_os.path = SimpleNamespace(**vars(m.os.path)) + fake_os.sep = '/' # synthetic Linux path contract, not this Windows host + fake_os.open = deny + fake_os.stat = deny + fake_os.path.realpath = deny + self.enterContext(patch.object(m, 'os', fake_os)) + self.enterContext(patch.object(m, 'read_bounded', side_effect=deny)) + self.enterContext(patch.object(m, 'docker_get', side_effect=deny)) + self.enterContext(patch.object(m.socket, 'socket', side_effect=deny)) + + def read(self, name, expected_device=None, deadline=None): + self.reads.append(name) + if name not in self.files: + raise FileNotFoundError('synthetic missing') + value = self.files[name] + if isinstance(value, BaseException): + raise value + return value + + def observe(self, after=None, realpath=None, statter=None): + after = copy.deepcopy(self.before) if after is None else after + def get(name, deadline=None): + self.gets.append(name) + if self.gets == ['/containers/' + ID + '/json']: + return copy.deepcopy(self.before) + if self.gets == ['/containers/' + ID + '/json', '/containers/' + ID + '/stats?stream=false&one-shot=true']: + return copy.deepcopy(self.stats) + if self.gets == ['/containers/' + ID + '/json', '/containers/' + ID + '/stats?stream=false&one-shot=true', '/containers/' + ID + '/json']: + return copy.deepcopy(after) + raise AssertionError('Unexpected Docker path or extra request') + def statter_default(name): + allowed = {'/sys/fs/cgroup': (8, 1), CG: (8, 2), '/proc/123/root': (8, 3)} + if name not in allowed: + raise AssertionError('Unexpected stat path') + dev, ino = allowed[name] + return SimpleNamespace(st_dev=dev, st_ino=ino) + with patch.object(m, 'read_bounded', side_effect=self.read), patch.object(m, 'docker_get', side_effect=get), \ + patch.object(m.os.path, 'realpath', side_effect=realpath or (lambda p: p)), \ + patch.object(m.os, 'stat', side_effect=statter or statter_default): + return m.collect_container(ID) + + def test_limits_max_zero_finite_are_not_missing_or_host_ram(self): + for value, expected in [('max\n', 'max'), ('max', 'max'), ('0\n', 0), ('512\n', 512), (str(2**64-1), 2**64-1)]: + self.assertEqual(m.parse_limit(value), expected) + for bad in ('MAX', '-1', ' 0', '00', '1.0', str(2**64), 'max\n\n'): + with self.assertRaises(ValueError): m.parse_limit(bad) + + def test_cpu_throttling_units_required_fields_and_output_whitelist(self): + parsed = m.parse_cpu_throttling(self.files[CG + '/cpu.stat']) + self.assertEqual(parsed, {'nr_periods':10,'nr_throttled':2,'throttled_usec':1234}) + for bad in ('nr_periods 1\nnr_throttled 0\n', 'nr_periods 1\nnr_periods 2\nnr_throttled 0\nthrottled_usec 0\n', 'nr_periods -1\nnr_throttled 0\nthrottled_usec 0\n'): + with self.assertRaises(ValueError): m.parse_cpu_throttling(bad) + + def test_pressure_fixed_groups_percent_and_microseconds(self): + parsed = m.parse_memory_pressure(PSI) + self.assertEqual(parsed['some'], {'avg10Percent':'0.00','avg60Percent':'1.25','avg300Percent':'100.00','totalMicros':123456}) + self.assertEqual(parsed['full']['totalMicros'], 0) + for bad in (PSI.replace('100.00','100.01'), PSI.replace('1.25','nan'), PSI.replace('total=0','total=-1'), PSI.splitlines()[0], PSI.replace('full','some'), PSI+'extra\n', PSI.replace('total=0','total='+str(2**64))): + with self.assertRaises(ValueError): m.parse_memory_pressure(bad) + + def test_docker_memoryswap_raw_minus1_zero_finite_and_missing_differ(self): + for raw in (-1,0,512,2**63-1): + item = copy.deepcopy(self.before); item['HostConfig']['MemorySwap'] = raw + self.assertEqual(m.configured_memory_swap(item), raw) + for bad in (None,False,True,'0',-2,2**63): + item = copy.deepcopy(self.before); item['HostConfig']['MemorySwap'] = bad + self.assertIsNone(m.configured_memory_swap(item)) + for host in ({},None,[]): + self.assertIsNone(m.configured_memory_swap({'HostConfig':host})) + + def test_real_collection_paths_are_only_fixed_host_and_retained_cgroup(self): + row = self.observe() + self.assertTrue(row['identityStableDuringSample']) + self.assertTrue(row['resourceLimitsStableDuringSample']) + self.assertEqual(row['configuredMemorySwapBytes'], 1024) + self.assertEqual(row['cgroupV2']['status'], 'available') + self.assertEqual(row['cgroupV2']['metrics']['memory.max'], 512) + self.assertEqual(row['cgroupV2']['metrics']['cpu.stat']['throttled_usec'], 1234) + self.assertTrue(set(self.reads).issubset(set(self.files))) + self.assertNotIn('SECRET', str(row)); self.assertNotIn('Env', str(row)) + self.assertEqual(len(self.gets), 3) + + def test_unlimited_limits_do_not_become_numeric_zero(self): + self.files[CG+'/memory.max']='max\n'; self.files[CG+'/memory.swap.max']='max\n' + row=self.observe();self.assertEqual(row['cgroupV2']['metrics']['memory.max'],'max');self.assertEqual(row['cgroupV2']['metrics']['memory.swap.max'],'max') + + def test_each_new_cgroup_source_missing_or_malformed_is_unknown(self): + for name in ('memory.max','memory.swap.max','cpu.stat'): + for value in (None,'not-a-counter\n'): + with self.subTest(name=name,value=value): + self.setUp_fixture_only() + if value is None: self.files.pop(CG+'/'+name) + else: self.files[CG+'/'+name]=value + row=self.observe();self.assertEqual(row['cgroupV2']['status'],'partial');self.assertIsNone(row['cgroupV2']['metrics'][name]);self.assertIn(name,row['cgroupV2']['unavailable']) + def setUp_fixture_only(self): + # Recreate dictionary, not guards/patches; no real I/O. + self.files.update({CG+'/memory.max':'512\n',CG+'/memory.swap.max':'512\n',CG+'/cpu.stat':'nr_periods 10\nnr_throttled 2\nthrottled_usec 1234\n'});self.gets=[] + + def test_missing_or_changed_swap_configuration_cannot_authenticate_limits(self): + after=copy.deepcopy(self.before);after['HostConfig']['MemorySwap']=2048 + row=self.observe(after);self.assertFalse(row['resourceLimitsStableDuringSample']);self.assertEqual(row['cgroupV2']['status'],'partial');self.assertIsNone(row['cgroupV2']['metrics']['memory.swap.max']) + self.gets=[];self.before['HostConfig'].pop('MemorySwap');after=copy.deepcopy(self.before) + row=self.observe(after);self.assertIsNone(row['configuredMemorySwapBytes']);self.assertEqual(row['configuredMemorySwapStatus'],'unavailable');self.assertIsNone(row['resourceLimitsStableDuringSample']);self.assertEqual(row['cgroupV2']['status'],'partial') + + def test_memory_limit_change_cannot_reuse_stable_container_identity_as_limit_proof(self): + after=copy.deepcopy(self.before);after['HostConfig']['Memory']=1024 + row=self.observe(after) + self.assertTrue(row['identityStableDuringSample']) + self.assertFalse(row['resourceLimitsStableDuringSample']) + for key in ('memory.max','memory.swap.max'): + self.assertIsNone(row['cgroupV2']['metrics'][key]) + self.assertEqual(row['cgroupV2']['unavailable'][key],'container-resource-limits-changed-or-unavailable') + + def test_legacy_projection_fields_keep_cache_limit_and_rss_distinctions(self): + item=copy.deepcopy(self.before);item['HostConfig'].pop('MemorySwap');item['HostConfig']['Memory']=0 + row=m.project_container(item,self.stats,ID) + self.assertEqual(row['usageIncludingCacheBytes'],100) + self.assertEqual(row['usageMinusInactiveFileBytes'],70) + self.assertEqual(row['configuredLimitBytes'],0) + self.assertEqual(row['reportedLimitBytes'],2048) + self.assertEqual(row['configuredMemorySwapStatus'],'unavailable') + self.assertIsNone(row['configuredMemorySwapBytes']) + + def test_identity_drift_symlink_and_pid_generation_still_refuse(self): + after=copy.deepcopy(self.before);after['State']['Pid']=456 + row=self.observe(after);self.assertFalse(row['identityStableDuringSample']);self.assertIsNone(row['resourceLimitsStableDuringSample']);self.assertEqual(row['cgroupV2']['status'],'unavailable') + self.gets=[];row=self.observe(realpath=lambda p:'/other' if p.endswith('memory.swap.max') else p);self.assertEqual(row['cgroupV2']['status'],'partial');self.assertIsNone(row['cgroupV2']['metrics']['memory.swap.max']) + self.gets=[] + with patch.object(m,'process_generation',side_effect=[(1,8,3),(2,8,3)]): + self.assertEqual(self.observe()['cgroupV2']['status'],'unavailable') + + def test_host_collection_whitelists_one_psi_file_and_missing_is_explicit(self): + with patch.object(m,'read_bounded',side_effect=self.read): + host=m.collect_host();self.assertEqual(host['memoryPressure']['status'],'available');self.assertEqual(host['MemTotalBytes'],2048*1024) + self.assertEqual(self.reads,['/proc/meminfo','/proc/vmstat','/proc/pressure/memory']) + self.files.pop('/proc/pressure/memory');host=m.collect_host();self.assertEqual(host['memoryPressure']['status'],'unavailable');self.assertIsNone(host['memoryPressure']['metrics']) + + def test_deadline_never_becomes_partial_or_success(self): + self.files[CG+'/cpu.stat']=m.ObservationDeadlineExceeded('fixture-deadline') + with self.assertRaises(m.ObservationDeadlineExceeded): self.observe() + with patch.object(m,'read_bounded',side_effect=[MEMINFO,VMSTAT,m.ObservationDeadlineExceeded('fixture-deadline')]): + with self.assertRaises(m.ObservationDeadlineExceeded): m.collect_host() + + def test_summary_exposes_unknown_and_never_authorizes_runtime(self): + row=self.observe() + with patch.object(m,'read_bounded',side_effect=self.read):host=m.collect_host() + sample={'elapsedMs':0,'host':host,'containers':[row]} + out=m.summarize([sample]);self.assertTrue(out['allResourceLimitsStableDuringSample']);self.assertTrue(out['allHostMemoryPressureAvailable']);self.assertFalse(out['acceptancePassed']) + row['resourceLimitsStableDuringSample']=None;host['memoryPressure']['status']='unavailable' + out=m.summarize([sample]);self.assertFalse(out['allResourceLimitsStableDuringSample']);self.assertFalse(out['allHostMemoryPressureAvailable']);self.assertFalse(out['acceptancePassed']) + +if __name__=='__main__': unittest.main() diff --git a/backend/scripts/acceptance/measure-memory.py b/backend/scripts/acceptance/measure-memory.py new file mode 100644 index 00000000..cbec1bd3 --- /dev/null +++ b/backend/scripts/acceptance/measure-memory.py @@ -0,0 +1,459 @@ +"""Bounded Linux/Docker memory observation; GET only, no service changes. + +Run alongside the named acceptance workload. Output is evidence, never an +automatic declaration that a 2 GB deployment has passed its load acceptance. +Requires Python 3 and read access to /proc and the local Docker socket. +""" +import argparse +import http.client +import json +import os +import re +import socket +import stat +import threading +import time + +MAX_RESPONSE = 4 * 1024 * 1024 +MAX_FILE_BYTES = 65536 +MAX_FILE_LINES = 256 +CGROUP_ROOT = '/sys/fs/cgroup' +CGROUP_FILES = ('memory.current', 'memory.peak', 'memory.events', + 'memory.swap.current', 'memory.swap.peak', 'memory.swap.events', + 'memory.max', 'memory.swap.max', 'cpu.stat') + + +class CgroupUnavailable(ValueError): + """Fixed reason codes only; never include private paths or raw content.""" + + +class ObservationDeadlineExceeded(RuntimeError): + pass + + +def check_deadline(deadline): + if deadline is not None and time.monotonic() >= deadline: + raise ObservationDeadlineExceeded('Observation time bound exceeded') + + +def read_bounded(path, expected_device=None, deadline=None): + # Fixed production paths only; no CLI path or executable is accepted. + check_deadline(deadline) + descriptor = os.open(path, os.O_RDONLY | getattr(os, 'O_NOFOLLOW', 0) | getattr(os, 'O_NONBLOCK', 0)) + with os.fdopen(descriptor, 'rb') as handle: + identity = os.fstat(handle.fileno()) + if not stat.S_ISREG(identity.st_mode) or (expected_device is not None and identity.st_dev != expected_device): + raise ValueError('Non-regular observation') + raw = handle.read(MAX_FILE_BYTES + 1) + check_deadline(deadline) + if len(raw) > MAX_FILE_BYTES: + raise ValueError('Observation file too large') + text = raw.decode('ascii') + if len(text.splitlines()) > MAX_FILE_LINES: + raise ValueError('Too many observation lines') + return text + + +def parse_counter(text): + if not re.fullmatch(r'(?:0|[1-9][0-9]{0,19})\n?', text): + raise ValueError('Invalid counter') + value = int(text) + if value > 2 ** 64 - 1: + raise ValueError('Counter overflow') + return value + + +def parse_events(text, required): + lines = text.splitlines() + if not 1 <= len(lines) <= 32: + raise ValueError('Invalid event count') + result = {} + for line in lines: + fields = line.split(' ') + if len(fields) != 2 or not re.fullmatch(r'[a-z_]{1,64}', fields[0]) or fields[0] in result: + raise ValueError('Invalid event') + result[fields[0]] = parse_counter(fields[1]) + if not set(required).issubset(result): + raise ValueError('Incomplete events') + return result + + + +def parse_limit(text): + # Preserve unlimited vs zero vs unavailable; never substitute host RAM. + return 'max' if text in ('max', 'max\n') else parse_counter(text) + + +def parse_cpu_throttling(text): + required = ('nr_periods', 'nr_throttled', 'throttled_usec') + parsed = parse_events(text, required) + # Kernel extensions are validated but do not expand the output whitelist. + return {key: parsed[key] for key in required} + + +def parse_memory_pressure(text): + rows = text.splitlines() + if len(rows) != 2: + raise ValueError('Incomplete memory pressure') + result = {} + for line in rows: + match = re.fullmatch(r'(some|full) avg10=([0-9]{1,3}\.[0-9]{2}) avg60=([0-9]{1,3}\.[0-9]{2}) avg300=([0-9]{1,3}\.[0-9]{2}) total=((?:0|[1-9][0-9]{0,19}))', line) + if not match or match[1] in result: + raise ValueError('Invalid memory pressure') + for value in match.group(2, 3, 4): + whole, fraction = value.split('.') + if int(whole) * 100 + int(fraction) > 10000: + raise ValueError('Invalid memory pressure percentage') + result[match[1]] = {'avg10Percent': match[2], 'avg60Percent': match[3], + 'avg300Percent': match[4], 'totalMicros': parse_counter(match[5])} + if set(result) != {'some', 'full'}: + raise ValueError('Incomplete memory pressure') + return result + + +def configured_memory_swap(inspect): + # Docker MemorySwap is TOTAL memory+swap; 0 means daemon default, -1 unlimited. + host = inspect.get('HostConfig') + value = host.get('MemorySwap') if isinstance(host, dict) else None + if type(value) is not int or not -1 <= value <= 2 ** 63 - 1: + return None + return value + + +def resource_limits_stable(before, after): + values = [] + for item in (before, after): + host = item.get('HostConfig') + memory = host.get('Memory') if isinstance(host, dict) else None + swap = configured_memory_swap(item) + if type(memory) is not int or not 0 <= memory <= 2 ** 63 - 1 or swap is None: + return None + values.append((memory, swap)) + return values[0] == values[1] + + +def collect_host(deadline=None): + memory = parse_host(read_bounded('/proc/meminfo', deadline=deadline), + read_bounded('/proc/vmstat', deadline=deadline)) + try: + pressure = parse_memory_pressure(read_bounded('/proc/pressure/memory', deadline=deadline)) + memory['memoryPressure'] = {'status': 'available', 'metrics': pressure, 'reason': None} + except (OSError, ValueError, UnicodeError): + memory['memoryPressure'] = {'status': 'unavailable', 'metrics': None, + 'reason': 'missing-unreadable-or-malformed'} + return memory + + +def container_identity(inspect, identifier): + state = inspect['State'] + if (inspect.get('Id') != identifier or not re.fullmatch(r'[a-f0-9]{64}', identifier) + or type(state.get('Pid')) is not int or not 0 < state['Pid'] <= 2 ** 31 - 1 + or state.get('Running') is not True or type(state.get('OOMKilled')) is not bool + or not isinstance(state.get('StartedAt'), str) or not state['StartedAt'] + or type(inspect.get('RestartCount')) is not int or inspect['RestartCount'] < 0 + or not isinstance(inspect.get('Image'), str) or not inspect['Image']): + raise ValueError('Unavailable container identity') + return (identifier, inspect['Image'], state['StartedAt'], state['Pid'], inspect['RestartCount'], state['OOMKilled']) + + +def cgroup_location(pid, identifier, deadline=None): + check_deadline(deadline) + mounts = read_bounded('/proc/self/mountinfo', deadline=deadline).splitlines() + # Namespace-relative/subtree mounts are deliberately unsupported: their + # root cannot authenticate a host container's complete cgroup membership. + matches = [line.split(' - ', 1) for line in mounts if ' - ' in line] + if not any(left.split()[3:5] == ['/', CGROUP_ROOT] and right.split()[0] == 'cgroup2' + for left, right in matches): + raise CgroupUnavailable('unsupported-or-unavailable-host-cgroup2-mount') + membership = read_bounded('/proc/' + str(pid) + '/cgroup', deadline=deadline) + lines = membership.splitlines() + if len(lines) != 1 or not lines[0].startswith('0::/'): + raise CgroupUnavailable('unsupported-v1-hybrid-or-malformed-membership') + relative = lines[0][3:] + parts = relative.split('/')[1:] + if (not parts or any(not p or p in ('.', '..') or not re.fullmatch(r'[A-Za-z0-9_.:-]{1,128}', p) for p in parts) + or parts[-1] not in (identifier, 'docker-' + identifier + '.scope')): + raise ValueError('Unbound cgroup membership') + root = os.path.realpath(CGROUP_ROOT) + path = os.path.realpath(CGROUP_ROOT + relative) + if root != CGROUP_ROOT or path != root + relative or not path.startswith(root + os.sep): + raise ValueError('Cgroup path escaped') + return path, membership + + +def process_generation(pid, deadline=None): + check_deadline(deadline) + raw = read_bounded('/proc/' + str(pid) + '/stat', deadline=deadline) + prefix, suffix = raw.rsplit(')', 1) + if prefix.split(' ', 1)[0] != str(pid) or ' (' not in prefix: + raise ValueError('Invalid process identity') + fields = suffix.split() + if len(fields) < 20: + raise ValueError('Incomplete process identity') + start = parse_counter(fields[19]) + root = os.stat('/proc/' + str(pid) + '/root') + return start, root.st_dev, root.st_ino + + +def unavailable_cgroup(reason): + return {'version': 2, 'status': 'unavailable', 'reason': reason, + 'metrics': {name: None for name in CGROUP_FILES}, 'unavailable': {name: reason for name in CGROUP_FILES}} + + +def collect_container(identifier, deadline=None): + check_deadline(deadline) + before = docker_get('/containers/' + identifier + '/json', deadline) + stats = docker_get('/containers/' + identifier + '/stats?stream=false&one-shot=true', deadline) + check_deadline(deadline) + result = project_container(before, stats, identifier) + cgroup = unavailable_cgroup('identity-unavailable') + try: + identity = container_identity(before, identifier) + pid = identity[3] + generation = process_generation(pid, deadline) + path, membership = cgroup_location(pid, identifier, deadline) + directory = os.stat(path) + if directory.st_dev != os.stat(CGROUP_ROOT).st_dev: + raise ValueError('Cgroup filesystem changed') + metrics, missing = {}, {} + for name in CGROUP_FILES: + check_deadline(deadline) + try: + candidate = path + '/' + name + if os.path.realpath(candidate) != candidate: + raise ValueError('Symlinked metric') + raw = read_bounded(candidate, directory.st_dev, deadline=deadline) + check_deadline(deadline) + if name == 'memory.events': + metrics[name] = parse_events(raw, ('low', 'high', 'max', 'oom', 'oom_kill')) + elif name == 'memory.swap.events': + metrics[name] = parse_events(raw, ('high', 'max', 'fail')) + elif name in ('memory.max', 'memory.swap.max'): + metrics[name] = parse_limit(raw) + elif name == 'cpu.stat': + metrics[name] = parse_cpu_throttling(raw) + else: + metrics[name] = parse_counter(raw) + except (OSError, ValueError, UnicodeError): + metrics[name] = None + missing[name] = 'missing-unreadable-or-malformed' + check_deadline(deadline) + after_path, after_membership = cgroup_location(pid, identifier, deadline) + after_directory = os.stat(path) + if (process_generation(pid, deadline) != generation or after_path != path or after_membership != membership + or (directory.st_dev, directory.st_ino) != (after_directory.st_dev, after_directory.st_ino)): + cgroup = unavailable_cgroup('process-or-cgroup-changed') + else: + cgroup = {'version': 2, 'status': 'partial' if missing else 'available', + 'reason': 'some-metrics-unavailable' if missing else None, + 'metrics': metrics, 'unavailable': missing, + 'peakScope': 'cgroup lifetime or since external reset; not an isolated workload peak', + 'provenance': 'fixed host cgroup2 mount + exact Docker ID membership + process/root generation'} + except CgroupUnavailable as error: + cgroup = unavailable_cgroup(str(error)) + except (OSError, ValueError, KeyError, IndexError, UnicodeError): + cgroup = unavailable_cgroup('identity-process-or-cgroup-unavailable-or-unsupported') + # Even failed cgroup observations are bracketed by a fresh Docker inspect. + check_deadline(deadline) + after = docker_get('/containers/' + identifier + '/json', deadline) + check_deadline(deadline) + try: + stable = container_identity(before, identifier) == container_identity(after, identifier) + except (ValueError, KeyError, TypeError): + stable = False + if not stable: + cgroup = unavailable_cgroup('container-identity-changed-or-unavailable') + limits_stable = resource_limits_stable(before, after) if stable else None + if stable and limits_stable is not True and cgroup['status'] != 'unavailable': + for key in ('memory.max', 'memory.swap.max'): + cgroup['metrics'][key] = None + cgroup['unavailable'][key] = 'container-resource-limits-changed-or-unavailable' + cgroup['status'] = 'partial' + cgroup['reason'] = 'some-metrics-unavailable' + result.update({'resourceLimitsStableDuringSample': limits_stable, + 'pid': before['State'].get('Pid'), 'identityStableDuringSample': stable, + 'runningAfter': after.get('State', {}).get('Running'), + 'oomKilledAfter': after.get('State', {}).get('OOMKilled'), 'cgroupV2': cgroup}) + return result + + +def docker_get(path, deadline=None): + deadline = time.monotonic() + 5 if deadline is None else deadline + check_deadline(deadline) + connection = http.client.HTTPConnection('localhost', timeout=min(5, deadline - time.monotonic())) + owned_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + connection.sock = owned_socket + # A socket timeout alone permits an endless body that drips bytes. The + # watchdog owns precisely this request's socket, including after HTTP's + # Connection:close detaches it from HTTPConnection into HTTPResponse. + def expire(): + try: + owned_socket.shutdown(socket.SHUT_RDWR) + except OSError: + pass + finally: + owned_socket.close() + watchdog = threading.Timer(max(0, deadline - time.monotonic()), expire) + watchdog.daemon = True + watchdog_started = False + response = None + try: + watchdog.start() + watchdog_started = True + owned_socket.settimeout(min(5, max(0.001, deadline - time.monotonic()))) + check_deadline(deadline) + owned_socket.connect('/var/run/docker.sock') + check_deadline(deadline) + connection.request('GET', '/v1.45' + path, headers={'Connection': 'close'}) + response = connection.getresponse() + raw = response.read(MAX_RESPONSE + 1) + check_deadline(deadline) + if response.status != 200 or len(raw) > MAX_RESPONSE: + raise ValueError('Docker observation unavailable') + result = json.loads(raw) + check_deadline(deadline) + return result + except Exception: + check_deadline(deadline) + raise + finally: + watchdog.cancel() + if watchdog_started: + watchdog.join() + try: + if response is not None: + response.close() + finally: + try: + connection.close() + finally: + owned_socket.close() + + +def nonnegative(value): + if type(value) is not int or value < 0: + raise ValueError('Invalid numeric observation') + return value + + +def parse_host(meminfo, vmstat): + memory = {} + for line in meminfo.splitlines(): + fields = line.split() + if fields and fields[0] in ('MemTotal:', 'MemAvailable:', 'SwapTotal:', 'SwapFree:'): + if len(fields) != 3 or fields[2] != 'kB': + raise ValueError('Invalid meminfo units') + memory[fields[0][:-1] + 'Bytes'] = nonnegative(int(fields[1])) * 1024 + if len(memory) != 4 or memory['MemAvailableBytes'] > memory['MemTotalBytes'] or memory['SwapFreeBytes'] > memory['SwapTotalBytes']: + raise ValueError('Incomplete host memory observation') + for line in vmstat.splitlines(): + fields = line.split() + if fields and fields[0] in ('pswpin', 'pswpout', 'oom_kill'): + memory[fields[0]] = nonnegative(int(fields[1])) + return memory + + +def project_container(inspect, stats, identifier): + if inspect.get('Id') != identifier or stats.get('id') != identifier: + raise ValueError('Pinned container changed') + state = inspect['State'] + memory = stats['memory_stats'] + usage = nonnegative(memory['usage']) + inactive = memory.get('stats', {}).get('inactive_file') + if inactive is not None: + inactive = nonnegative(inactive) + if inactive > usage: + raise ValueError('Invalid cache observation') + return { + 'id': identifier, 'imageId': inspect['Image'], + 'startedAt': state['StartedAt'], 'running': state['Running'] is True, + 'oomKilled': state.get('OOMKilled'), 'restartCount': inspect.get('RestartCount'), + 'usageIncludingCacheBytes': usage, 'inactiveFileBytes': inactive, + 'usageMinusInactiveFileBytes': usage - inactive if inactive is not None else None, + 'configuredLimitBytes': nonnegative(inspect['HostConfig']['Memory']), + 'configuredMemorySwapBytes': configured_memory_swap(inspect), + 'configuredMemorySwapStatus': 'available' if configured_memory_swap(inspect) is not None else 'unavailable', + 'reportedLimitBytes': nonnegative(memory['limit']), + 'swapUsageBytes': memory.get('swap_usage'), + } + + +def summarize(samples): + if not samples: + raise ValueError('No observations') + baseline = {c['id']: (c['imageId'], c['startedAt'], c['restartCount'], c.get('pid'), c.get('oomKilled')) for c in samples[0]['containers']} + stable = all({c['id']: (c['imageId'], c['startedAt'], c['restartCount'], c.get('pid'), c.get('oomKilled')) for c in sample['containers']} == baseline + and all(c.get('identityStableDuringSample', True) for c in sample['containers']) for sample in samples) + counters = {} + for field in ('pswpin', 'pswpout', 'oom_kill'): + first = samples[0]['host'].get(field) + last = samples[-1]['host'].get(field) + counters[field + 'Delta'] = last - first if first is not None and last is not None and last >= first else None + return { + 'samples': len(samples), 'elapsedMs': samples[-1]['elapsedMs'], + 'hostMemTotalBytes': samples[0]['host']['MemTotalBytes'], + 'minHostAvailableBytes': min(s['host']['MemAvailableBytes'] for s in samples), + 'maxObservedContainerUsageIncludingCacheBytes': max(sum(c['usageIncludingCacheBytes'] for c in s['containers']) for s in samples), + 'containerIdentityAndStartStable': stable, + 'allObservedRunning': all(c['running'] is True and c.get('runningAfter') is True + and c.get('identityStableDuringSample') is True for s in samples for c in s['containers']), + 'anyObservedOomKilled': any(c['oomKilled'] is True or c.get('oomKilledAfter') is True for s in samples for c in s['containers']), + 'allCgroupV2MetricsAvailable': all(s['containers'] and all(c.get('cgroupV2', {}).get('status') == 'available' for c in s['containers']) for s in samples), + **counters, + 'allResourceLimitsStableDuringSample': all(c.get('resourceLimitsStableDuringSample') is True for s in samples for c in s['containers']) and all(s['containers'] for s in samples), + 'allHostMemoryPressureAvailable': all(s['host'].get('memoryPressure', {}).get('status') == 'available' for s in samples), + 'acceptancePassed': False, + 'scope': 'sampled evidence only; correlate with workload, limits, errors and latency; unsampled peaks are unknown', + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--daemon-id', required=True) + parser.add_argument('--container', action='append', required=True) + parser.add_argument('--samples', type=int, default=30) + parser.add_argument('--interval', type=float, default=1) + parser.add_argument('--output', required=True) + args = parser.parse_args() + if os.name != 'posix' or not hasattr(socket, 'AF_UNIX'): + parser.error('Run on the Linux Docker host') + if not re.fullmatch(r'[A-Za-z0-9-]{8,128}', args.daemon_id) or not 1 <= len(args.container) <= 16 or len(set(args.container)) != len(args.container) or any(not re.fullmatch(r'[a-f0-9]{64}', c) for c in args.container): + parser.error('Pin exact daemon ID and 1..16 distinct full container IDs') + if not 1 <= args.samples <= 300 or not 0.2 <= args.interval <= 10 or args.samples * args.interval > 300: + parser.error('Observation bound: 300 samples / 300 seconds') + started = time.monotonic() + deadline = started + 360 + if docker_get('/info', deadline).get('ID') != args.daemon_id: + raise ValueError('Pinned Docker daemon changed') + samples = [] + for index in range(args.samples): + check_deadline(deadline) + host = collect_host(deadline) + containers = [] + for identifier in args.container: + check_deadline(deadline) + containers.append(collect_container(identifier, deadline)) + check_deadline(deadline) + samples.append({'elapsedMs': round((time.monotonic() - started) * 1000), 'host': host, 'containers': containers}) + if index + 1 < args.samples: + time.sleep(min(args.interval, max(0, deadline - time.monotonic()))) + check_deadline(deadline) + if docker_get('/info', deadline).get('ID') != args.daemon_id: + raise ValueError('Pinned Docker daemon changed') + result = {'schemaVersion': 3, 'readOnlyDockerAndProc': True, 'serviceChanges': 0, 'summary': summarize(samples), 'observations': samples} + encoded = json.dumps(result, indent=2) + '\n' + check_deadline(deadline) + # Refuse to replace an existing evidence file. Never dump Docker env/logs. + with open(args.output, 'x', encoding='utf-8') as handle: + check_deadline(deadline) + handle.write(encoded) + check_deadline(deadline) + print(json.dumps(result['summary'])) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print(json.dumps({'status': 'observation-refused', 'errorClass': type(error).__name__, 'serviceChanges': 0})) + raise SystemExit(1) diff --git a/backend/scripts/acceptance/measure-memory.test.py b/backend/scripts/acceptance/measure-memory.test.py new file mode 100644 index 00000000..a9ac7dc4 --- /dev/null +++ b/backend/scripts/acceptance/measure-memory.test.py @@ -0,0 +1,384 @@ +import copy +import importlib.util +from pathlib import Path +import os +import tempfile +import io +import sys +import threading +import time +import unittest +from unittest.mock import patch + +spec = importlib.util.spec_from_file_location('memory_measure', Path(__file__).with_name('measure-memory.py')) +measure = importlib.util.module_from_spec(spec) +spec.loader.exec_module(measure) + + +class MemoryEvidenceTests(unittest.TestCase): + def test_host_units_and_missing_fields(self): + raw = 'MemTotal: 2048 kB\nMemAvailable: 1024 kB\nSwapTotal: 4096 kB\nSwapFree: 3072 kB\n' + parsed = measure.parse_host(raw, 'pswpin 20\npswpout 30\noom_kill 1\n') + self.assertEqual(parsed['MemTotalBytes'], 2048 * 1024) + self.assertEqual(parsed['pswpin'], 20) + for invalid in (raw.replace('kB', 'MB'), raw.replace('MemAvailable: 1024 kB\n', ''), raw.replace('MemAvailable: 1024', 'MemAvailable: 4096')): + with self.assertRaises(ValueError): + measure.parse_host(invalid, '') + + def test_cache_and_no_configured_limit_are_distinct(self): + item = {'Id': 'a' * 64, 'Image': 'sha256:fixed', 'State': {'StartedAt': 't0', 'Running': True}, 'HostConfig': {'Memory': 0}} + stats = {'id': 'a' * 64, 'memory_stats': {'usage': 100, 'limit': 2048, 'stats': {'inactive_file': 30}}} + result = measure.project_container(item, stats, 'a' * 64) + self.assertEqual(result['usageIncludingCacheBytes'], 100) + self.assertEqual(result['usageMinusInactiveFileBytes'], 70) + self.assertEqual(result['configuredLimitBytes'], 0) + with self.assertRaises(ValueError): + measure.project_container(item, stats, 'b' * 64) + + def test_simultaneous_peak_is_not_sum_of_separate_peaks(self): + host = {'MemTotalBytes': 2048, 'MemAvailableBytes': 1024, 'pswpin': 3, 'pswpout': 5, 'oom_kill': 1} + def container(identifier, usage): + return {'id': identifier, 'imageId': 'i', 'startedAt': 't0', 'restartCount': 0, 'running': True, 'oomKilled': False, 'usageIncludingCacheBytes': usage} + samples = [{'elapsedMs': 0, 'host': host, 'containers': [container('a', 100), container('b', 20)]}, + {'elapsedMs': 1000, 'host': {**host, 'pswpin': 4}, 'containers': [container('a', 20), container('b', 100)]}] + result = measure.summarize(samples) + self.assertEqual(result['maxObservedContainerUsageIncludingCacheBytes'], 120) + self.assertEqual(result['pswpinDelta'], 1) + self.assertTrue(result['containerIdentityAndStartStable']) + self.assertFalse(result['acceptancePassed']) + changed = copy.deepcopy(samples) + changed[-1]['containers'][0]['startedAt'] = 't1' + changed[-1]['containers'][1]['oomKilled'] = True + self.assertFalse(measure.summarize(changed)['containerIdentityAndStartStable']) + self.assertTrue(measure.summarize(changed)['anyObservedOomKilled']) + + def test_counter_rollback_is_unknown(self): + samples = [{'elapsedMs': i, 'host': {'MemTotalBytes': 2, 'MemAvailableBytes': 1, 'pswpin': value}, 'containers': []} for i, value in enumerate([100, 2])] + self.assertIsNone(measure.summarize(samples)['pswpinDelta']) + + +class CgroupEvidenceTests(unittest.TestCase): + identifier = 'a' * 64 + path = '/sys/fs/cgroup/system.slice/docker-' + identifier + '.scope' + + def setUp(self): + self.before = {'Id': self.identifier, 'Image': 'sha256:fixed', 'RestartCount': 0, + 'State': {'StartedAt': 'original-start', 'Pid': 123, 'Running': True, 'OOMKilled': False}, + 'HostConfig': {'Memory': 512, 'MemorySwap': 1024}} + self.stats = {'id': self.identifier, 'memory_stats': {'usage': 100, 'limit': 512, 'stats': {'inactive_file': 30}}} + self.files = { + '/proc/self/mountinfo': '1 0 0:1 / /sys/fs/cgroup rw - cgroup2 cgroup rw\n', + '/proc/123/cgroup': '0::/system.slice/docker-' + self.identifier + '.scope\n', + '/proc/123/stat': '123 (a name with ) parentheses) S ' + ' '.join(['0'] * 18 + ['987'] + ['0'] * 8), + self.path + '/memory.max': '512\n', self.path + '/memory.swap.max': '512\n', + self.path + '/cpu.stat': 'nr_periods 10\nnr_throttled 2\nthrottled_usec 1234\n', + self.path + '/memory.current': '100\n', self.path + '/memory.peak': '200\n', + self.path + '/memory.events': 'low 0\nhigh 1\nmax 2\noom 3\noom_kill 4\n', + self.path + '/memory.swap.current': '5\n', self.path + '/memory.swap.peak': '6\n', + self.path + '/memory.swap.events': 'high 0\nmax 2\nfail 1\n', + } + + def observe(self, after=None, reader=None, resolver=None, statter=None): + def read(path, _expected_device=None, deadline=None): + if path not in self.files: + raise FileNotFoundError() + return self.files[path] + queries = [] + def get(path, deadline=None): + queries.append(path) + if '/stats?' in path: + return self.stats + return self.before if len(queries) == 1 else (after or self.before) + with patch.object(measure, 'docker_get', side_effect=get), \ + patch.object(measure, 'read_bounded', side_effect=reader or read), \ + patch.object(measure.os.path, 'realpath', side_effect=resolver or (lambda path: path)), \ + patch.object(measure.os, 'stat', side_effect=statter or (lambda path: os.stat_result((0, 7, 8, 0, 0, 0, 0, 0, 0, 0)))), \ + patch.object(measure.os, 'sep', '/'): + result = measure.collect_container(self.identifier) + self.assertEqual(queries, ['/containers/' + self.identifier + '/json', + '/containers/' + self.identifier + '/stats?stream=false&one-shot=true', + '/containers/' + self.identifier + '/json']) + return result + + def test_exact_identity_metrics_and_docker_cache_are_distinct(self): + result = self.observe() + self.assertTrue(result['identityStableDuringSample']) + self.assertEqual(result['usageMinusInactiveFileBytes'], 70) + self.assertEqual(result['cgroupV2']['status'], 'available') + self.assertEqual(result['cgroupV2']['metrics']['memory.current'], 100) + self.assertEqual(result['cgroupV2']['metrics']['memory.peak'], 200) + self.assertEqual(result['cgroupV2']['metrics']['memory.events']['oom_kill'], 4) + self.assertEqual(result['cgroupV2']['metrics']['memory.swap.current'], 5) + self.assertEqual(result['cgroupV2']['metrics']['memory.swap.peak'], 6) + self.assertEqual(result['cgroupV2']['metrics']['memory.swap.events']['fail'], 1) + + def test_optional_swap_peak_missing_is_unknown_not_zero(self): + del self.files[self.path + '/memory.swap.peak'] + result = self.observe()['cgroupV2'] + self.assertEqual(result['status'], 'partial') + self.assertIsNone(result['metrics']['memory.swap.peak']) + self.assertEqual(result['metrics']['memory.peak'], 200) + self.assertIn('memory.swap.peak', result['unavailable']) + + def test_each_missing_metric_is_explicit_unknown(self): + for name in measure.CGROUP_FILES: + with self.subTest(name=name): + value = self.files.pop(self.path + '/' + name) + result = self.observe()['cgroupV2'] + self.assertIsNone(result['metrics'][name]) + self.assertEqual(result['status'], 'partial') + self.files[self.path + '/' + name] = value + + def test_malformed_metrics_never_coerce_to_zero(self): + for value in ('-1\n', '+1\n', ' 1\n', '01\n', '1\n2\n', '18446744073709551616\n', 'max\n'): + with self.subTest(value=value): + self.files[self.path + '/memory.current'] = value + self.assertIsNone(self.observe()['cgroupV2']['metrics']['memory.current']) + for value in ('oom 1\noom 2\n', 'low 0\nhigh -1\nmax 0\noom 0\noom_kill 0\n', 'low 0\n', 'low 0\n'): + with self.subTest(value=value): + self.files[self.path + '/memory.events'] = value + self.assertIsNone(self.observe()['cgroupV2']['metrics']['memory.events']) + + def test_docker_identity_drift_invalidates_entire_cgroup_sample(self): + for field, value in [('Pid', 124), ('StartedAt', 'new'), ('OOMKilled', True), ('Running', False)]: + after = copy.deepcopy(self.before); after['State'][field] = value + result = self.observe(after) + self.assertFalse(result['identityStableDuringSample']) + self.assertTrue(all(value is None for value in result['cgroupV2']['metrics'].values())) + for field, value in [('Id', 'b' * 64), ('RestartCount', 1), ('Image', 'sha256:new')]: + after = copy.deepcopy(self.before); after[field] = value + self.assertEqual(self.observe(after)['cgroupV2']['status'], 'unavailable') + + def test_process_reuse_rootfs_swap_and_membership_move_are_unknown(self): + for changed in ('/proc/123/stat', '/proc/123/cgroup'): + count = 0 + def read(path, _expected_device=None, deadline=None): + nonlocal count + if path == changed: + count += 1 + if count == 2: + return self.files[path].replace('987', '988') if path.endswith('/stat') else '0::/docker/' + 'b' * 64 + '\n' + return self.files[path] + self.assertEqual(self.observe(reader=read)['cgroupV2']['status'], 'unavailable') + count = 0 + def statter(path): + nonlocal count + if path == '/proc/123/root': + count += 1 + return os.stat_result((0, count, 8, 0, 0, 0, 0, 0, 0, 0)) + self.assertEqual(self.observe(statter=statter)['cgroupV2']['status'], 'unavailable') + + def test_unsupported_v1_namespace_subtree_and_unbound_membership(self): + original = self.files['/proc/123/cgroup'] + for value in ('2:memory:/docker/' + self.identifier + '\n', '0::/\n', '0::/docker/' + 'b' * 64 + '\n', + '0::/docker/' + self.identifier + '/../other\n', original.rstrip() + '/child\n', original + original): + self.files['/proc/123/cgroup'] = value + self.assertEqual(self.observe()['cgroupV2']['status'], 'unavailable') + self.files['/proc/123/cgroup'] = original + self.files['/proc/self/mountinfo'] = '1 0 0:1 /subtree /sys/fs/cgroup rw - cgroup2 cgroup rw\n' + self.assertEqual(self.observe()['cgroupV2']['status'], 'unavailable') + + def test_symlink_outside_or_to_other_container_is_refused(self): + for destination in ('/etc', '/sys/fs/cgroup/docker/' + 'b' * 64): + self.assertEqual(self.observe(resolver=lambda path: destination if path == self.path else path)['cgroupV2']['status'], 'unavailable') + result = self.observe(resolver=lambda path: '/etc/passwd' if path.endswith('/memory.current') else path) + self.assertIsNone(result['cgroupV2']['metrics']['memory.current']) + + def test_cgroup_directory_replacement_or_nested_filesystem_is_unknown(self): + for nested in (False, True): + calls = 0 + def statter(path): + nonlocal calls + if path == self.path: + calls += 1 + inode = 8 if path == self.path and calls > 1 else 7 + device = 9 if nested and path == self.path else 8 + return os.stat_result((0, inode, device, 0, 0, 0, 0, 0, 0, 0)) + result = self.observe(statter=statter)['cgroupV2'] + self.assertEqual(result['status'], 'unavailable') + self.assertTrue(all(value is None for value in result['metrics'].values())) + + def test_pid_reuse_unreadable_process_and_after_oom_stay_unknown(self): + after = copy.deepcopy(self.before); after['State']['OOMKilled'] = True + result = self.observe(after) + host = {'MemTotalBytes': 2048, 'MemAvailableBytes': 1024} + summary = measure.summarize([{'elapsedMs': 0, 'host': host, 'containers': [result]}]) + self.assertTrue(summary['anyObservedOomKilled']) + self.assertFalse(summary['containerIdentityAndStartStable']) + self.assertFalse(summary['allCgroupV2MetricsAvailable']) + self.assertFalse(summary['acceptancePassed']) + def read(path, _expected_device=None, deadline=None): + if path == '/proc/123/stat': + raise PermissionError() + return self.files[path] + self.assertEqual(self.observe(reader=read)['cgroupV2']['status'], 'unavailable') + + def test_real_temp_files_are_bounded_and_strictly_ascii(self): + with tempfile.TemporaryDirectory(dir=Path(__file__).resolve().parent) as directory: + path = Path(directory) / 'memory.current' + path.write_bytes(b'123\n') + self.assertEqual(measure.parse_counter(measure.read_bounded(path)), 123) + with self.assertRaises(ValueError): + measure.read_bounded(path, path.stat().st_dev + 1) + for raw in (b'0' * (measure.MAX_FILE_BYTES + 1), b'0\n' * (measure.MAX_FILE_LINES + 1), b'\xff'): + path.write_bytes(raw) + with self.assertRaises((ValueError, UnicodeError)): + measure.read_bounded(path) + + def test_after_stopped_or_unknown_identity_cannot_summarize_running(self): + for value in (False, None): + after = copy.deepcopy(self.before); after['State']['Running'] = value + result = self.observe(after) + self.assertIs(result['runningAfter'], value) + summary = measure.summarize([{'elapsedMs': 0, 'host': {'MemTotalBytes': 2048, 'MemAvailableBytes': 1024}, 'containers': [result]}]) + self.assertFalse(summary['allObservedRunning']) + result = self.observe() + summary = measure.summarize([{'elapsedMs': 0, 'host': {'MemTotalBytes': 2048, 'MemAvailableBytes': 1024}, 'containers': [result]}]) + self.assertTrue(summary['allObservedRunning']) + + def test_event_future_counters_retained_but_duplicates_and_overflow_refuse(self): + self.assertEqual(measure.parse_events('high 0\nmax 1\nfail 2\nfuture 3\n', ('high', 'max', 'fail'))['future'], 3) + for value in ('x 1\nx 2\n', 'x 18446744073709551616\n', ''.join('x_' + str(i) + ' 0\n' for i in range(33))): + with self.assertRaises(ValueError): + measure.parse_events(value, ()) + + +class DeadlineEvidenceTests(unittest.TestCase): + def test_expired_http_never_opens_socket_or_schedules_watchdog(self): + with patch.object(measure.time, 'monotonic', return_value=10), \ + patch.object(measure.socket, 'socket') as own_socket, patch.object(measure.threading, 'Timer') as timer: + with self.assertRaises(measure.ObservationDeadlineExceeded): + measure.docker_get('/info', 10) + own_socket.assert_not_called() + timer.assert_not_called() + + def test_expired_container_read_never_starts_io(self): + with patch.object(measure.time, 'monotonic', return_value=10), patch.object(measure, 'docker_get') as get: + with self.assertRaises(measure.ObservationDeadlineExceeded): + measure.collect_container('a' * 64, 10) + get.assert_not_called() + + def test_near_cutoff_after_stats_stops_before_proc_or_more_gets(self): + clock, queries = [9.9], [] + def get(path, deadline): + self.assertEqual(deadline, 10) + queries.append(path) + if len(queries) == 2: + clock[0] = 10 + return {} + with patch.object(measure.time, 'monotonic', side_effect=lambda: clock[0]), \ + patch.object(measure, 'docker_get', side_effect=get), patch.object(measure, 'read_bounded') as read: + with self.assertRaises(measure.ObservationDeadlineExceeded): + measure.collect_container('a' * 64, 10) + self.assertEqual(len(queries), 2) + read.assert_not_called() + + def test_metric_deadline_is_not_downgraded_to_partial_or_followed_by_io(self): + fixture = CgroupEvidenceTests(); fixture.setUp() + clock, reads, queries = [9.9], [], [] + def read(path, _device=None, deadline=None): + self.assertEqual(deadline, 10) + reads.append(path) + if path.endswith('/memory.current'): + clock[0] = 10 + return fixture.files[path] + def get(path, deadline): + queries.append(path) + return fixture.stats if '/stats?' in path else fixture.before + with patch.object(measure.time, 'monotonic', side_effect=lambda: clock[0]), \ + patch.object(measure, 'docker_get', side_effect=get), patch.object(measure, 'read_bounded', side_effect=read), \ + patch.object(measure.os.path, 'realpath', side_effect=lambda path: path), \ + patch.object(measure.os, 'stat', return_value=os.stat_result((0, 7, 8, 0, 0, 0, 0, 0, 0, 0))), \ + patch.object(measure.os, 'sep', '/'): + with self.assertRaises(measure.ObservationDeadlineExceeded): + measure.collect_container(fixture.identifier, 10) + self.assertEqual(reads[-1], fixture.path + '/memory.current') + self.assertEqual(len(queries), 2) + + def test_final_daemon_check_crosses_deadline_without_writing_report(self): + clock, calls = [0], [] + def get(path, deadline): + self.assertEqual(deadline, 360) + calls.append(path) + if len(calls) == 2: + clock[0] = 361 + return {'ID': 'owned-daemon'} + host = 'MemTotal: 2048 kB\nMemAvailable: 1024 kB\nSwapTotal: 0 kB\nSwapFree: 0 kB\n' + args = ['measure-memory.py', '--daemon-id', 'owned-daemon', '--container', 'a' * 64, '--samples', '1', '--output', 'NEVER_CREATED'] + with patch.object(sys, 'argv', args), patch.object(measure.os, 'name', 'posix'), \ + patch.object(measure.socket, 'AF_UNIX', 1, create=True), \ + patch.object(measure.time, 'monotonic', side_effect=lambda: clock[0]), patch.object(measure, 'docker_get', side_effect=get), \ + patch.object(measure, 'read_bounded', side_effect=[host, '', 'some avg10=0.00 avg60=0.00 avg300=0.00 total=0\nfull avg10=0.00 avg60=0.00 avg300=0.00 total=0\n']), patch.object(measure, 'collect_container', return_value={}), \ + patch.object(measure, 'summarize', return_value={}), patch('builtins.open') as output: + with self.assertRaises(measure.ObservationDeadlineExceeded): + measure.main() + output.assert_not_called() + + def test_slow_body_absolute_deadline_closes_only_own_socket_and_joins(self): + # A real HTTPResponse/BufferedReader and real watchdog timer, with an + # in-memory socket transport. No AF_UNIX availability or local network + # permissions are needed on Windows. Inactivity timeouts never fire: + # the body keeps producing bytes until shutdown interrupts the read. + class DripBody(io.RawIOBase): + def __init__(self, stopped): + self.header = b'HTTP/1.1 200 OK\r\nContent-Length: 1000\r\nConnection: close\r\n\r\n' + self.stopped = stopped + self.body_reads = 0 + def readable(self): + return True + def readinto(self, buffer): + if self.header: + value, self.header = self.header[:len(buffer)], self.header[len(buffer):] + buffer[:len(value)] = value + return len(value) + if self.stopped.wait(0.01): + return 0 + self.body_reads += 1 + buffer[0] = 32 + return 1 + class SocketTransport: + def __init__(self): + self.stopped = threading.Event() + self.body = DripBody(self.stopped) + self.closed = False + self.request = b'' + def connect(self, path): + self.path = path + def settimeout(self, value): + self.timeout = value + def sendall(self, value): + self.request += value + def makefile(self, _mode): + return io.BufferedReader(self.body) + def shutdown(self, _how): + self.stopped.set() + def close(self): + self.closed = True + client, unrelated = SocketTransport(), SocketTransport() + started = time.monotonic() + original_timer = measure.threading.Timer + timers = [] + def timer(*args, **kwargs): + value = original_timer(*args, **kwargs) + timers.append(value) + return value + with patch.object(measure.socket, 'AF_UNIX', 1, create=True), \ + patch.object(measure.socket, 'socket', return_value=client), \ + patch.object(measure.threading, 'Timer', side_effect=timer): + with self.assertRaises(measure.ObservationDeadlineExceeded): + measure.docker_get('/info', started + 0.12) + self.assertLess(time.monotonic() - started, 1.5) + self.assertTrue(client.closed) + self.assertTrue(client.stopped.is_set()) + self.assertGreater(client.body.body_reads, 0) + self.assertFalse(unrelated.closed) + self.assertFalse(unrelated.stopped.is_set()) + self.assertEqual(client.path, '/var/run/docker.sock') + self.assertIn(b'GET /v1.45/info ', client.request) + self.assertEqual(len(timers), 1) + self.assertFalse(timers[0].is_alive(), 'No deadline callback can perform late I/O after return') + + +if __name__ == '__main__': + unittest.main() + diff --git a/backend/scripts/acceptance/run-installation-memory.mjs b/backend/scripts/acceptance/run-installation-memory.mjs new file mode 100644 index 00000000..1f874dc9 --- /dev/null +++ b/backend/scripts/acceptance/run-installation-memory.mjs @@ -0,0 +1,63 @@ +/** Local smoke preparation only: owns a new embedded PG and one compiled child. + * Does not represent frontend/portal/Docker/whole-installation acceptance. */ +import assert from 'node:assert/strict' +import fs from 'node:fs/promises' +import { createRequire } from 'node:module' +import { tmpdir } from 'node:os' +import { join,resolve,dirname,basename } from 'node:path' +import { fileURLToPath } from 'node:url' +import { createServer } from 'node:net' +import { randomBytes,createHash } from 'node:crypto' +import { spawn } from 'node:child_process' +const root=resolve(dirname(fileURLToPath(import.meta.url)),'../../..') +assert(process.argv.length>=3&&process.argv.length<=5,'Supply a unique evidence name, optional compiled output and delayed-approval scenario') +const delayedApproval=process.argv[4]==='delayed-approval' +assert(process.argv[4]===undefined||delayedApproval,'Unknown isolated scenario') +const name=process.argv[2];assert(/^[a-z0-9][a-z0-9-]{3,99}$/.test(name),'Invalid evidence name') +const outputName=process.argv[3]??'dist-installation-memory' +assert(/^dist-installation-memory(?:-[a-z0-9-]{3,64})?$/.test(outputName),'Invalid compiled output name') +const prefix=resolve(root,'tmp',name), statusPath=prefix+'-status.json' +const exclusive=await fs.open(statusPath,'wx');await exclusive.close() +const state={scope:'local-backend-coload-only',wholeInstallation2GbAccepted:false,compiledOutputName:outputName,delayedHumanApprovalScenario:delayedApproval,status:'starting',nodeExitCode:null,sourcePinsUnchanged:false,cleanupConfirmed:false,startedAt:new Date().toISOString(),wrapperPid:process.pid} +const save=()=>fs.writeFile(statusPath,JSON.stringify(state,null,2)+'\n');await save() +const require=createRequire(new URL('../../package.json',import.meta.url)),EmbeddedPostgres=require('embedded-postgres').default +const directory=await fs.mkdtemp(join(tmpdir(),'mm-mcp-test-')),server=createServer() +await new Promise(done=>server.listen(0,'127.0.0.1',done));const port=server.address().port;await new Promise(done=>server.close(done)) +const password=randomBytes(24).toString('hex') +const postgres=new EmbeddedPostgres({databaseDir:join(directory,'pg'),port,user:'mcp_test',password,persistent:true,createPostgresUser:false, + initdbFlags:['--encoding=UTF8'],postgresFlags:['-h','127.0.0.1'],onLog:()=>{},onError:()=>{}}) +const output=resolve(root,'backend',outputName),entry=resolve(output,'services/installation-co-load.owned.test.js') +async function pins() { + const rows=[] + async function walk(dir){for(const item of await fs.readdir(dir,{withFileTypes:true})){const file=join(dir,item.name);if(item.isDirectory())await walk(file);else rows.push({path:file.slice(root.length+1).replaceAll('\\','/'),sha256:createHash('sha256').update(await fs.readFile(file)).digest('hex')})}} + await walk(output);return rows.sort((a,b)=>a.path.localeCompare(b.path)) +} +const before=await pins();await fs.writeFile(prefix+'-pins.json',JSON.stringify({before},null,2)+'\n') +let log,timer +try { + await postgres.initialise();await postgres.start() + log=await fs.open(prefix+'.log','wx') + const retained=Object.fromEntries(['SystemRoot','WINDIR','TEMP','TMP','LANG','LC_ALL'].filter(key=>process.env[key]).map(key=>[key,process.env[key]])) + const child=spawn(process.execPath,['--max-old-space-size=192','--max-semi-space-size=16',entry],{cwd:directory,windowsHide:true,stdio:['ignore',log.fd,log.fd],env:{...retained, + NODE_OPTIONS:'',NODE_ENV:'test',PORT:'8000',AUTH_ISSUER:'https://auth.shnt.top',AUTH_PORTAL_URL:'http://localhost:5175',SQLITE_IMPORT_FULL_HISTORY:'false', + MM_FLOW_READ_RESOURCE_PROFILE:'constrained',DATABASE_URL:`postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`,DATABASE_PATH:join(directory,'no-legacy.sqlite'), + MCP_STATE_DIR:join(directory,'state'),AUTH_REQUIRED:'true',AUTH_JWT_SECRET:randomBytes(32).toString('hex'),MCP_PUBLIC_URL:'http://localhost:8000/mcp',MMAPP_OWN_INSTALLATION_COLOAD:'true',MMAPP_OWN_COLOAD_DELAYED_APPROVAL:String(delayedApproval)}}) + state.status='running';state.childPid=child.pid;await save() + timer=setTimeout(()=>{state.outerTimedOut=true;child.kill()},300000) + state.nodeExitCode=await new Promise((done,reject)=>{child.once('error',reject);child.once('exit',code=>done(code??1))}) + clearTimeout(timer);await log.close();log=undefined + const after=await pins();state.sourcePinsUnchanged=JSON.stringify(before)===JSON.stringify(after) + await fs.writeFile(prefix+'-pins.json',JSON.stringify({before,after,unchanged:state.sourcePinsUnchanged},null,2)+'\n') + process.exitCode=state.nodeExitCode===0&&state.sourcePinsUnchanged?0:1 +}catch(error){state.failure=error instanceof Error?error.name:'UnknownError';process.exitCode=1} +finally { + if(timer)clearTimeout(timer);if(log)await log.close() + try{ + if(postgres.process?.exitCode===null)await postgres.stop() + const actual=await fs.realpath(directory),parent=await fs.realpath(tmpdir()) + assert.equal(dirname(actual),parent);assert.match(basename(actual),/^mm-mcp-test-[A-Za-z0-9-]+$/);assert.equal((await fs.lstat(directory)).isSymbolicLink(),false) + await fs.rm(actual,{recursive:true,force:true,maxRetries:20,retryDelay:100});state.cleanupConfirmed=true + }catch{state.cleanupConfirmed=false;process.exitCode=1} + state.status='terminal';state.finishedAt=new Date().toISOString();await save();console.log(JSON.stringify(state)) +} +if(state.cleanupConfirmed)process.exit(process.exitCode??1) diff --git a/backend/scripts/acceptance/schema2.main.synthetic.json b/backend/scripts/acceptance/schema2.main.synthetic.json new file mode 100644 index 00000000..69e72147 --- /dev/null +++ b/backend/scripts/acceptance/schema2.main.synthetic.json @@ -0,0 +1,81 @@ +{ + "schemaVersion": 2, + "readOnlyDockerAndProc": true, + "serviceChanges": 0, + "summary": { + "samples": 1, + "elapsedMs": 0, + "hostMemTotalBytes": 2097152, + "minHostAvailableBytes": 1048576, + "maxObservedContainerUsageIncludingCacheBytes": 100, + "containerIdentityAndStartStable": true, + "allObservedRunning": true, + "anyObservedOomKilled": false, + "allCgroupV2MetricsAvailable": true, + "pswpinDelta": 0, + "pswpoutDelta": 0, + "oom_killDelta": 0, + "acceptancePassed": false, + "scope": "sampled evidence only; correlate with workload, limits, errors and latency; unsampled peaks are unknown" + }, + "observations": [ + { + "elapsedMs": 0, + "host": { + "MemTotalBytes": 2097152, + "MemAvailableBytes": 1048576, + "SwapTotalBytes": 4194304, + "SwapFreeBytes": 3145728, + "pswpin": 20, + "pswpout": 30, + "oom_kill": 0 + }, + "containers": [ + { + "id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "imageId": "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "startedAt": "2026-11-02T00:00:00Z", + "running": true, + "oomKilled": false, + "restartCount": 0, + "usageIncludingCacheBytes": 100, + "inactiveFileBytes": 30, + "usageMinusInactiveFileBytes": 70, + "configuredLimitBytes": 512, + "reportedLimitBytes": 2048, + "swapUsageBytes": null, + "pid": 123, + "identityStableDuringSample": true, + "runningAfter": true, + "oomKilledAfter": false, + "cgroupV2": { + "version": 2, + "status": "available", + "reason": null, + "metrics": { + "memory.current": 100, + "memory.peak": 200, + "memory.events": { + "low": 0, + "high": 0, + "max": 0, + "oom": 0, + "oom_kill": 0 + }, + "memory.swap.current": 0, + "memory.swap.peak": 0, + "memory.swap.events": { + "high": 0, + "max": 0, + "fail": 0 + } + }, + "unavailable": {}, + "peakScope": "cgroup lifetime or since external reset; not an isolated workload peak", + "provenance": "fixed host cgroup2 mount + exact Docker ID membership + process/root generation" + } + } + ] + } + ] +} diff --git a/backend/scripts/acceptance/schema3.main.synthetic.json b/backend/scripts/acceptance/schema3.main.synthetic.json new file mode 100644 index 00000000..10c097c1 --- /dev/null +++ b/backend/scripts/acceptance/schema3.main.synthetic.json @@ -0,0 +1,111 @@ +{ + "schemaVersion": 3, + "readOnlyDockerAndProc": true, + "serviceChanges": 0, + "summary": { + "samples": 1, + "elapsedMs": 0, + "hostMemTotalBytes": 2097152, + "minHostAvailableBytes": 1048576, + "maxObservedContainerUsageIncludingCacheBytes": 100, + "containerIdentityAndStartStable": true, + "allObservedRunning": true, + "anyObservedOomKilled": false, + "allCgroupV2MetricsAvailable": true, + "pswpinDelta": 0, + "pswpoutDelta": 0, + "oom_killDelta": 0, + "allResourceLimitsStableDuringSample": true, + "allHostMemoryPressureAvailable": true, + "acceptancePassed": false, + "scope": "sampled evidence only; correlate with workload, limits, errors and latency; unsampled peaks are unknown" + }, + "observations": [ + { + "elapsedMs": 0, + "host": { + "MemTotalBytes": 2097152, + "MemAvailableBytes": 1048576, + "SwapTotalBytes": 4194304, + "SwapFreeBytes": 3145728, + "pswpin": 20, + "pswpout": 30, + "oom_kill": 0, + "memoryPressure": { + "status": "available", + "metrics": { + "some": { + "avg10Percent": "0.00", + "avg60Percent": "1.25", + "avg300Percent": "100.00", + "totalMicros": 123456 + }, + "full": { + "avg10Percent": "0.00", + "avg60Percent": "0.01", + "avg300Percent": "1.00", + "totalMicros": 0 + } + }, + "reason": null + } + }, + "containers": [ + { + "id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "imageId": "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "startedAt": "2026-11-02T00:00:00Z", + "running": true, + "oomKilled": false, + "restartCount": 0, + "usageIncludingCacheBytes": 100, + "inactiveFileBytes": 30, + "usageMinusInactiveFileBytes": 70, + "configuredLimitBytes": 512, + "configuredMemorySwapBytes": 1024, + "configuredMemorySwapStatus": "available", + "reportedLimitBytes": 2048, + "swapUsageBytes": null, + "resourceLimitsStableDuringSample": true, + "pid": 123, + "identityStableDuringSample": true, + "runningAfter": true, + "oomKilledAfter": false, + "cgroupV2": { + "version": 2, + "status": "available", + "reason": null, + "metrics": { + "memory.current": 100, + "memory.peak": 200, + "memory.events": { + "low": 0, + "high": 0, + "max": 0, + "oom": 0, + "oom_kill": 0 + }, + "memory.swap.current": 0, + "memory.swap.peak": 0, + "memory.swap.events": { + "high": 0, + "max": 0, + "fail": 0 + }, + "memory.max": 512, + "memory.swap.max": 512, + "cpu.stat": { + "nr_periods": 10, + "nr_throttled": 2, + "throttled_usec": 1234 + } + }, + "unavailable": {}, + "peakScope": "cgroup lifetime or since external reset; not an isolated workload peak", + "provenance": "fixed host cgroup2 mount + exact Docker ID membership + process/root generation" + } + } + ] + } + ] +} diff --git a/backend/scripts/build-database-operator.mjs b/backend/scripts/build-database-operator.mjs new file mode 100644 index 00000000..688573c3 --- /dev/null +++ b/backend/scripts/build-database-operator.mjs @@ -0,0 +1,103 @@ +/** Build-time only. Fixed operator artifact; never invoked by server startup. */ +import assert from 'node:assert/strict' +import {createHash} from 'node:crypto' +import {isBuiltin} from 'node:module' +import {readFile,writeFile,mkdir,stat,lstat,readdir} from 'node:fs/promises' +import path from 'node:path' +import {fileURLToPath,pathToFileURL} from 'node:url' +import ts from 'typescript' + +import {OPERATOR_SOURCES} from './database-operator-sources.mjs' +export {OPERATOR_SOURCES} + +const emitted=source=>source.replace(/\.mts$/,'.mjs').replace(/\.ts$/,'.js') +const digest=value=>createHash('sha256').update(value).digest('hex') +const packageName=specifier=>specifier.startsWith('@')?specifier.split('/').slice(0,2).join('/'):specifier.split('/')[0] +/** The transformer changes only module specifier AST nodes, never SQL/string data. */ +export function compileOperatorModule(source,fileName){ + const rewrite=specifier=>specifier.replace(/^(\.\.\/|\.\.\/\.\.\/)src\//,'$1dist/') + const result=ts.transpileModule(source,{fileName,reportDiagnostics:true,compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext,verbatimModuleSyntax:true,newLine:ts.NewLineKind.LineFeed},transformers:{before:[context=>{ + const visit=node=>{ + if(ts.isImportDeclaration(node)&&ts.isStringLiteral(node.moduleSpecifier))return context.factory.updateImportDeclaration(node,node.modifiers,node.importClause,context.factory.createStringLiteral(rewrite(node.moduleSpecifier.text)),node.attributes) + if(ts.isExportDeclaration(node)&&node.moduleSpecifier&&ts.isStringLiteral(node.moduleSpecifier))return context.factory.updateExportDeclaration(node,node.modifiers,node.isTypeOnly,node.exportClause,context.factory.createStringLiteral(rewrite(node.moduleSpecifier.text)),node.attributes) + if(ts.isCallExpression(node)&&node.expression.kind===ts.SyntaxKind.ImportKeyword){ + assert(node.arguments.length===1&&ts.isStringLiteral(node.arguments[0]),'Only fixed literal operator dynamic imports are supported') + return context.factory.updateCallExpression(node,node.expression,node.typeArguments,[context.factory.createStringLiteral(rewrite(node.arguments[0].text))]) + } + return ts.visitEachChild(node,visit,context) + } + return sourceFile=>ts.visitNode(sourceFile,visit) + }]}}) + const errors=result.diagnostics?.filter(item=>item.category===ts.DiagnosticCategory.Error)??[] + assert.equal(errors.length,0,'Operator transpilation diagnostics: '+errors.map(item=>ts.flattenDiagnosticMessageText(item.messageText,' ')).join('; ')) + return result.outputText +} +export function moduleSpecifiers(source,fileName){ + const parsed=ts.createSourceFile(fileName,source,ts.ScriptTarget.ES2022,true,ts.ScriptKind.JS),specifiers=[] + const visit=node=>{ + if((ts.isImportDeclaration(node)||ts.isExportDeclaration(node))&&node.moduleSpecifier){assert(ts.isStringLiteral(node.moduleSpecifier));specifiers.push(node.moduleSpecifier.text)} + if(ts.isCallExpression(node)&&node.expression.kind===ts.SyntaxKind.ImportKeyword){assert(node.arguments.length===1&&ts.isStringLiteral(node.arguments[0]),'Computed runtime import refused');specifiers.push(node.arguments[0].text)} + ts.forEachChild(node,visit) + } + visit(parsed);return specifiers +} +export async function buildDatabaseOperator({backendDirectory=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..'),outputDirectory=path.join(backendDirectory,'dist-operator')}={}){ + const backend=path.resolve(backendDirectory),output=path.resolve(outputDirectory),packageJson=JSON.parse(await readFile(path.join(backend,'package.json'),'utf8')) + const modules=new Map(),allowed=new Set(OPERATOR_SOURCES.map(emitted)),runtimeDependencies=new Set(),distImports=new Set() + const productionDependency=specifier=>{ + if(isBuiltin(specifier))return + const dependency=packageName(specifier);assert(Object.hasOwn(packageJson.dependencies,dependency),'Operator runtime dependency must be production: '+dependency);runtimeDependencies.add(dependency) + } + for(const file of OPERATOR_SOURCES){ + const raw=await readFile(path.join(backend,'scripts',file)),code=compileOperatorModule(new TextDecoder('utf-8',{fatal:true}).decode(raw),file),target=emitted(file) + const imports=moduleSpecifiers(code,target) + for(const specifier of imports){ + assert(!/\.(?:ts|mts)$/.test(specifier)&&!/(?:^|\/)src\//.test(specifier),'Source runtime import refused') + if(specifier.startsWith('.')){ + const resolved=path.posix.normalize(path.posix.join(path.posix.dirname(target),specifier)) + if(resolved.startsWith('../dist/')){ + const relative=resolved.slice('../dist/'.length) + assert(relative.endsWith('.js')&&!relative.split('/').includes('..'),'Fixed compiled backend helper required') + assert((await stat(path.join(backend,'dist',relative))).isFile(),'Run backend tsc before operator build') + distImports.add(relative) + }else assert(allowed.has(resolved),'Operator import outside fixed build graph: '+specifier) + }else productionDependency(specifier) + } + modules.set(target,{code,source:file,sourceSha256:digest(raw),sha256:digest(code),imports}) + } + // Validate the referenced compiled helper closure as part of the production + // build, not only in tests. Keep this bounded to the small operator graph. + const backendDistModules=new Map() + async function visitDist(file){ + assert(file.endsWith('.js')&&!/[\\:]/.test(file)&&!file.startsWith('/')&&!file.split('/').includes('..')&&!file.split('/').includes('src'),'Compiled helper escaped backend/dist') + if(backendDistModules.has(file))return + assert(backendDistModules.size<64,'Operator compiled helper graph exceeded 64 modules') + const raw=await readFile(path.join(backend,'dist',file)),imports=moduleSpecifiers(new TextDecoder('utf-8',{fatal:true}).decode(raw),file) + backendDistModules.set(file,{file,sha256:digest(raw),imports}) + for(const specifier of imports){ + assert(!/\.(?:ts|mts)$/.test(specifier)&&!/(?:^|\/)src\//.test(specifier),'Source runtime import refused') + if(specifier.startsWith('.'))await visitDist(path.posix.normalize(path.posix.join(path.posix.dirname(file),specifier))) + else productionDependency(specifier) + } + } + for(const file of distImports)await visitDist(file) + // Refuse stale/unlisted output rather than shipping an extra file (e.g. a key) + // through Docker's directory COPY. Never delete/adopt unknown build contents. + try{ + assert((await lstat(output)).isDirectory(),'Ordinary operator output directory required') + for(const item of await readdir(output,{recursive:true,withFileTypes:true})){ + const relative=path.relative(output,path.join(item.parentPath,item.name)).split(path.sep).join('/') + assert(item.isDirectory()?relative==='writer-profile':item.isFile()&&(allowed.has(relative)||relative==='build-manifest.json'),'Unexpected operator output entry: '+relative) + } + }catch(error){if(error.code!=='ENOENT')throw error} + // Validate the whole fixed graph before publishing any generated module. + await mkdir(output,{recursive:true}) + for(const [file,module] of modules){await mkdir(path.dirname(path.join(output,file)),{recursive:true});await writeFile(path.join(output,file),module.code)} + const manifest={format:'application-database-operator-build-v1',compilerVersion:ts.version,entrypoints:['database-writer-bootstrap-operator.mjs','database-writer-profile-operator.mjs'],modules:[...modules].map(([file,module])=>({file,source:module.source,sourceSha256:module.sourceSha256,sha256:module.sha256,imports:module.imports})),backendDistImports:[...distImports].sort(),backendDistModules:[...backendDistModules.values()].sort((a,b)=>a.file.localeCompare(b.file)),runtimeDependencies:[...runtimeDependencies].sort(),automaticStartup:false,readyAuthorized:false} + await writeFile(path.join(output,'build-manifest.json'),JSON.stringify(manifest,null,2)+'\n') + return manifest +} +if(process.argv[1]&&import.meta.url===pathToFileURL(path.resolve(process.argv[1])).href){ + assert.equal(process.argv.length,2,'Build takes no runtime configuration or key arguments') + const result=await buildDatabaseOperator();console.log(JSON.stringify({event:'database-operator-built',modules:result.modules.length,automaticStartup:false,readyAuthorized:false})) +} diff --git a/backend/scripts/build-database-operator.test.mjs b/backend/scripts/build-database-operator.test.mjs new file mode 100644 index 00000000..b5681e7e --- /dev/null +++ b/backend/scripts/build-database-operator.test.mjs @@ -0,0 +1,101 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import {readFile,writeFile,mkdir,copyFile,mkdtemp,rename} from 'node:fs/promises' +import {spawnSync} from 'node:child_process' +import path from 'node:path' +import {fileURLToPath,pathToFileURL} from 'node:url' +import {buildDatabaseOperator,compileOperatorModule,moduleSpecifiers,OPERATOR_SOURCES} from './build-database-operator.mjs' +import {stageDatabaseOperatorScripts} from './stage-database-operator.mjs' + +const backend=process.env.OPERATOR_BUILD_TEST_BACKEND??path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..') +test('AST rewrite changes static/export/dynamic module specifiers and preserves SQL/string values',()=>{ + const code=compileOperatorModule(`import {x} from '../src/db/x.js'; export {y} from '../../src/db/y.js'; const module=()=>import('../src/db/z.js'); export const sql="SELECT '../src/db/x.js'";`,'fixture.mts') + assert.deepEqual(moduleSpecifiers(code,'fixture.mjs'),['../dist/db/x.js','../../dist/db/y.js','../dist/db/z.js']) + assert(code.includes(`SELECT '../src/db/x.js'`));assert.throws(()=>compileOperatorModule('const load=name=>import(name)','fixture.mts'),/fixed literal/) +}) +test('fixed artifact is reproducible, resolves the compiled backend graph, and runs with plain Node22',async()=>{ + assert.equal(Number(process.versions.node.split('.')[0]),22,'Run this acceptance with the production Node22 major') + const temporaryParent=process.env.OPERATOR_BUILD_TEST_TMP??path.resolve(backend,'../tmp');await mkdir(temporaryParent,{recursive:true}) + const temporary=await mkdtemp(path.join(temporaryParent,'db-operator-build-')),projectBackend=path.join(temporary,'backend'),output=path.join(projectBackend,'dist-operator') + const packageJson=JSON.parse(await readFile(path.join(backend,'package.json'),'utf8')) + await mkdir(projectBackend,{recursive:true});await writeFile(path.join(projectBackend,'package.json'),JSON.stringify({type:'module',dependencies:packageJson.dependencies})) + const first=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output}),second=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output}) + assert.deepEqual(first,second);assert.equal(first.modules.length,OPERATOR_SOURCES.length);assert.equal(first.modules.length,31);assert.deepEqual(first.runtimeDependencies,['dotenv','pg','zod']);assert.equal(first.backendDistModules.length,30) + assert(first.modules.some(item=>item.file==='writer-profile/registry-enrollment.mjs')) + assert(first.modules.some(item=>item.file==='writer-profile/marker-fence-enrollment.mjs')) + assert(first.modules.some(item=>item.file==='writer-profile/fixed58-build-definition.js')) + assert(first.backendDistImports.includes('db/database-restore-quarantine-profile.js')) + assert(first.backendDistImports.includes('db/database-source-activation-record.js')) + assert(first.backendDistImports.includes('services/server-probe-legacy-source-inventory.js')) + assert(first.backendDistImports.includes('services/server-probe-legacy-archive-approval.js')) + assert(first.backendDistImports.includes('services/server-probe-legacy-handoff-inspection.js')) + assert(first.backendDistImports.includes('db/legacy-probe-0037-body.js')) + assert(first.backendDistImports.includes('db/controlled-schema-reference-core.js')) + assert(first.backendDistImports.includes('services/database-dump-connection.js')) + const copied=new Set() + async function copyDist(relative){ + if(copied.has(relative))return;copied.add(relative) + const original=path.join(backend,'dist',relative),target=path.join(projectBackend,'dist',relative),source=await readFile(original,'utf8') + await mkdir(path.dirname(target),{recursive:true});await copyFile(original,target) + for(const specifier of moduleSpecifiers(source,relative)){ + assert(!specifier.includes('/src/'));assert(!/\.(?:ts|mts)$/.test(specifier)) + if(specifier.startsWith('.')){const dependency=path.posix.normalize(path.posix.join(path.posix.dirname(relative),specifier));assert(!dependency.startsWith('../'));await copyDist(dependency)} + else if(!specifier.startsWith('node:')){const name=specifier.startsWith('@')?specifier.split('/').slice(0,2).join('/'):specifier.split('/')[0];assert(Object.hasOwn(packageJson.dependencies,name),'Compiled helper has undeclared production dependency '+name)} + } + } + for(const relative of first.backendDistImports)await copyDist(relative) + assert.deepEqual([...copied].sort(),first.backendDistModules.map(item=>item.file).sort()) + // Exercise the real build entry point with its default backend/output paths. + const scripts=path.join(projectBackend,'scripts') + assert.equal(await stageDatabaseOperatorScripts(scripts,path.join(backend,'scripts')),OPERATOR_SOURCES.length+2) + const buildScript=path.join(scripts,'build-database-operator.mjs') + const built=spawnSync(process.execPath,[buildScript],{cwd:temporary,env:{...process.env,NODE_OPTIONS:''},encoding:'utf8',timeout:30000}) + assert.equal(built.status,0,built.stderr);assert.equal(JSON.parse(built.stdout.trim()).modules,first.modules.length) + assert.deepEqual(JSON.parse(await readFile(path.join(output,'build-manifest.json'),'utf8')),first) + const configFile=path.join(projectBackend,'dist','config.js'),originalConfig=await readFile(configFile,'utf8') + for(const injected of ["import 'tsx';","import '../src/config.js';","import '../outside.js';"]){ + await writeFile(configFile,originalConfig+'\n'+injected) + await assert.rejects(buildDatabaseOperator({backendDirectory:projectBackend,outputDirectory:output}),/production|Source runtime import|escaped backend\/dist/) + } + await writeFile(configFile,originalConfig) + await writeFile(path.join(output,'unexpected-private.pem'),'not a key; stale-output test') + await assert.rejects(buildDatabaseOperator({backendDirectory:backend,outputDirectory:output}),/Unexpected operator output entry/) + const {unlink}=await import('node:fs/promises');await unlink(path.join(output,'unexpected-private.pem')) + await rename(path.join(projectBackend,'scripts'),path.join(temporary,'build-inputs')) + const guard=path.join(temporary,'runtime-guard.mjs'),telemetry=path.join(temporary,'telemetry.json') + const denied=[...new Set([...Object.keys(packageJson.devDependencies),'typescript','tsx'])] + // Block development imports and connections in the actual plain-Node process. + await writeFile(guard,`import {registerHooks,syncBuiltinESMExports} from 'node:module';import net from 'node:net';import tls from 'node:tls';import {writeFileSync} from 'node:fs';const denied=new Set(${JSON.stringify(denied)});let connections=0;const refuse=()=>{connections++;throw Error('Unexpected operator network connection')};net.Socket.prototype.connect=refuse;tls.connect=refuse;syncBuiltinESMExports();registerHooks({resolve(specifier,context,next){const name=specifier.startsWith('@')?specifier.split('/').slice(0,2).join('/'):specifier.split('/')[0];if(denied.has(name)||/\\.(?:ts|mts)$/.test(specifier)||specifier.includes('/backend/src/'))throw Error('Nonproduction import '+specifier);return next(specifier,context)}});process.on('exit',()=>writeFileSync(${JSON.stringify(telemetry)},JSON.stringify({connections})));`) + const env={...process.env,NODE_ENV:'production',NODE_OPTIONS:'',AUTH_REQUIRED:'false',DOTENV_CONFIG_QUIET:'true'} + // The workspace tmp projection resolves installed packages from its ancestor + // node_modules; the runtime hook rejects every development dependency. + const modulesToImport=first.modules.map(item=>path.join(output,item.file)) + const importer=path.join(temporary,'import-all.mjs');await writeFile(importer,`import {pathToFileURL} from 'node:url';for(const file of ${JSON.stringify(modulesToImport)})await import(pathToFileURL(file).href);console.log(JSON.stringify({imported:${modulesToImport.length},readyAuthorized:false}));`) + const imported=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,importer],{cwd:temporary,env,encoding:'utf8',timeout:30000}) + assert.equal(imported.status,0,imported.stderr);assert.equal(JSON.parse(imported.stdout.trim()).imported,first.modules.length);assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0) + const refusals=[] + for(const [entry,event,args] of [['database-writer-bootstrap-operator.mjs','application-writer-bootstrap-refused',['invalid']],['database-writer-profile-operator.mjs','application-writer-profile-publication-refused',['invalid']],['database-writer-profile-operator.mjs','database-maintenance-command-refused',['maintenance-inspect']],['database-writer-profile-operator.mjs','database-maintenance-command-refused',['maintenance-resolve']],['database-writer-profile-operator.mjs','database-source-activation-refused',['source-activate']],['database-writer-profile-operator.mjs','database-legacy-inspection-refused',['legacy-inspect']],['database-writer-profile-operator.mjs','application-writer-registry-enrollment-refused',['registry-enroll']],['database-writer-profile-operator.mjs','application-writer-marker-fence-enrollment-refused',['marker-fence-enroll']],['database-writer-profile-operator.mjs','application-writer-restore-profile-enrollment-refused',['restore-profile-enroll']]]){ + const child=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,path.join(output,entry),...args],{cwd:temporary,env,encoding:'utf8',timeout:30000}) + assert.equal(child.status,1,child.stderr);assert.equal(child.stdout,'');assert.equal(JSON.parse(child.stderr.trim()).event,event);assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0);refusals.push(event) + } + const catalogue=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,path.join(output,'database-writer-profile-operator.mjs'),'source-catalogue-inspect'],{cwd:temporary,env,encoding:'utf8',timeout:30000}) + assert.equal(catalogue.status,1,catalogue.stderr);assert.equal(catalogue.stdout,'');assert.equal(JSON.parse(catalogue.stderr.trim()).event,'application-writer-source-catalogue-inspection-refused') + assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0);refusals.push('application-writer-source-catalogue-inspection-refused') + for(const command of ['legacy-export','legacy-custody','legacy-handoff-inspect','legacy-cold-verify','legacy-handoff-execute','legacy-handoff-adopt','legacy-handoff-release','legacy-control-reference','legacy-source58-preflight','source58-foundation-enroll','source57-admission-enroll']){ + const invalid=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,path.join(output,'database-writer-profile-operator.mjs'),command],{cwd:temporary,env,encoding:'utf8',timeout:30000}) + const event={ + 'legacy-handoff-inspect':'database-legacy-handoff-inspection-refused','legacy-cold-verify':'database-legacy-cold-proof-refused', + 'legacy-handoff-execute':'database-legacy-handoff-execution-refused', + 'legacy-handoff-adopt':'database-legacy-handoff-adoption-refused', + 'legacy-handoff-release':'database-legacy-handoff-release-refused', + 'legacy-control-reference':'database-legacy-control-reference-refused', + 'legacy-source58-preflight':'database-legacy-source58-preflight-refused', + 'source58-foundation-enroll':'database-source58-foundation-refused', + 'source57-admission-enroll':'database-source57-admission-refused', + }[command]??`database-${command}-refused` + assert.equal(invalid.status,1,invalid.stderr);assert.equal(invalid.stdout,'');assert.equal(JSON.parse(invalid.stderr.trim()).event,event) + assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0);refusals.push(event) + } + const receipt={node:process.version,projection:temporary,modules:first.modules.length,compiledBackendModules:copied.size,buildCliExitCode:built.status,importedModules:modulesToImport.length,invalidArgumentsRefused:refusals,developmentDependenciesBlocked:denied,networkConnections:0,productionActivated:false,readyAuthorized:false} + await writeFile(path.join(temporary,'acceptance-receipt.json'),JSON.stringify(receipt,null,2)+'\n');console.log(JSON.stringify(receipt)) +}) diff --git a/backend/scripts/database-maintenance-resolution.test.mts b/backend/scripts/database-maintenance-resolution.test.mts new file mode 100644 index 00000000..407ba3e1 --- /dev/null +++ b/backend/scripts/database-maintenance-resolution.test.mts @@ -0,0 +1,100 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import {mkdtemp,mkdir,writeFile,readFile,readdir,rm,unlink,lstat} from 'node:fs/promises' +import {createHash,randomUUID} from 'node:crypto' +import {rootCertificates} from 'node:tls' +import {EventEmitter} from 'node:events' +import path from 'node:path' +import {fileURLToPath,pathToFileURL} from 'node:url' +import {runInNewContext} from 'node:vm' +import {createRequire} from 'node:module' +import * as strictReader from '../src/services/database-maintenance-startup.js' +import * as grammar from '../src/services/database-maintenance-resolution.js' +import * as manifest from '../src/db/migration-manifest.js' +import * as contract from './writer-profile/operator-contract.mjs' +const backend=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..'),require=createRequire(import.meta.url),ts=require('typescript'),hash=(value:Buffer|string)=>createHash('sha256').update(value).digest('hex') +async function own(work:(root:string)=>Promise){const temp=path.resolve(backend,'../tmp');await mkdir(temp,{recursive:true});const root=await mkdtemp(path.join(temp,'owned-resolution-command-'));try{await work(root)}finally{assert.equal(path.dirname(root),temp);assert(path.basename(root).startsWith('owned-resolution-command-'));await rm(root,{recursive:true,force:true})}} +async function model(root:string,localHost?:string){ + let owners=0,sql=0,releaseFailure=false,poolEndFailure=false,published=0,wrongOwner=false,controlSchema=false,observationFailure:Error|undefined + const capturedScopes:unknown[]=[] + const state=path.join(root,'state');await mkdir(state);await mkdir(path.join(state,'restore-vault'));await writeFile(path.join(state,'access-generation'),Buffer.alloc(32,7));const uncertain=randomUUID(),at=new Date().toISOString(),record=(value:string)=>JSON.stringify({operationId:uncertain,state:value,at})+'\n';await writeFile(path.join(state,'database-maintenance.jsonl'),record('intent')+record('unconfirmed')) + const source={systemIdentifier:'1234567890',databaseOid:'12345',databaseName:'managed',serverVersionNum:180004,inRecovery:false} + const schema={sha256:'a'.repeat(64),observedRaw:{sha256:'b'.repeat(64)},observedPhysical:{sha256:'c'.repeat(64)},sections:{relations:manifest.CURRENT_REQUIRED_TABLES.map(name=>({namespace:'public',name,kind:'r',persistence:'p',row_security:false,force_row_security:false})),columns:[{namespace:'public',relation:'schema_migrations',name:'id',dropped:false,type:'text'}]}} + class Pool {on(){};constructor(options:unknown){const ssl=(options as {ssl:false|{rejectUnauthorized:boolean}}).ssl;if(ssl===false)assert(['127.0.0.1','::1'].includes((options as {host:string}).host));else assert(ssl.rejectUnauthorized)}async connect(){owners++;const client=new EventEmitter();return Object.assign(client,{release:()=>{owners--;if(releaseFailure)throw Error('Synthetic release uncertainty')},query:async(text:string)=>{sql++;if(text.includes('pg_try_advisory_lock'))return {rows:[{locked:true}],command:'SELECT'};if(text.includes('pg_advisory_unlock'))return {rows:[{unlocked:true}],command:'SELECT'};if(text.includes('pg_namespace'))return {rows:controlSchema?[{nspname:'mmapp_control'}]:[],command:'SELECT'};if(text.includes('pg_control_system')){if(observationFailure)throw observationFailure;return {rows:[source],command:'SELECT'}};if(text.startsWith('SELECT id FROM public.schema_migrations'))return {rows:manifest.MIGRATION_MANIFEST.map(pin=>({id:pin.file.slice(0,-4)})),command:'SELECT'};return {rows:[],command:text.split(' ')[0]}}})}async end(){assert.equal(owners,0);if(poolEndFailure)throw Error('Synthetic pool retirement uncertainty')}} + const protectedSource=await readFile(path.join(backend,'scripts/writer-profile/operator-files.mjs'),'utf8'),protectedExports:Record={},actualUid=(await lstat(state)).uid + const protectedCode=ts.transpileModule(protectedSource,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText + runInNewContext(protectedCode,{exports:protectedExports,require,Buffer,process:{platform:'linux',getuid:()=>actualUid+1},Object,Error}) + const protectedDirectory=async(directory:string)=>{assert(path.resolve(directory).startsWith(root+path.sep)||path.resolve(directory)===root);if(wrongOwner)await (protectedExports.protectedDirectory as (path:string)=>Promise)(directory);await readdir(directory)} + // Explicit MODELED protection adapter: native POSIX mode/ownership and hardlink + // fsync publication remain separate Linux acceptance, never a Windows claim. + const files={sha:hash,protectedDirectory,readProtected:async(file:string,maximum:number,pin?:string)=>{assert(file.startsWith(root+path.sep));const raw=await readFile(file);assert(raw.length>0&&raw.length<=maximum);if(pin)assert.equal(hash(raw),pin);return raw},writeFile:async(file:string,raw:Buffer)=>{assert(file.startsWith(root+path.sep));await writeFile(file,raw,{flag:'wx'});published++},withJournalLease:async(directory:string,work:()=>Promise)=>{const lock=path.join(directory,'publisher-lease.lock');await writeFile(lock,'owned',{flag:'wx'});try{return await work()}finally{await unlink(lock)}}} + const readRecords=async(directory:string)=>{const records=path.join(directory,'maintenance-acceptances');let names:string[];try{names=await readdir(records)}catch(error){if((error as NodeJS.ErrnoException).code==='ENOENT')return [];throw error};assert(names.every(name=>/^[a-f0-9-]{36}\.json$/.test(name)));return Promise.all(names.sort().map(name=>readFile(path.join(records,name))))} + const moduleFile=path.join(backend,'scripts/writer-profile/maintenance-resolution.mts'),sourceText=(await readFile(moduleFile,'utf8')).replaceAll('import.meta.url',JSON.stringify(pathToFileURL(moduleFile).href)),exports:Record={} + const dependencies:Record={'./operator-files.mjs':files,'./operator-contract.mjs':contract,'../../src/services/database-maintenance-startup.js':{...strictReader,readMaintenanceAcceptanceRecords:readRecords},'../../src/services/database-maintenance-resolution.js':grammar,'../../src/db/migration-schema-inventory.js':{captureMigrationSchema:async(_client:unknown,options:{scope:unknown})=>{capturedScopes.push(options.scope);return schema}},'../../src/db/migration-manifest.js':manifest,pg:{Pool}} + const code=ts.transpileModule(sourceText,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText + runInNewContext(code,{exports,require:(name:string)=>name in dependencies?dependencies[name]:require(name),Buffer,URL,process:{env:{}},Date,Object,Map,Set,Error,console}) + const command=exports.maintenanceMain as (args:string[])=>Promise> + const tlsTransport={format:'database-maintenance-transport-v1',connection:{host:'db.example.invalid',port:5432,user:'existing-maintenance-user',password:'synthetic-only',database:'managed'},tls:{serverName:'db.example.invalid',spkiSha256:'f'.repeat(64)}},localTransport=(host:string)=>({format:'database-maintenance-transport-v1',mode:'local-loopback',connection:{...tlsTransport.connection,host}}) + const configs={transport:localHost===undefined?tlsTransport:localTransport(localHost),'runtime-target':{format:'database-maintenance-runtime-target-v1',databaseUrl:'postgresql://db.example.invalid:5432/managed?sslmode=require'}} + const args=['--state-dir',state] + for(const [name,value]of Object.entries(configs)){const raw=Buffer.from(JSON.stringify(value)),file=path.join(root,name+'.json');await writeFile(file,raw);args.push('--'+name,file,'--'+name+'-sha256',hash(raw))} + const ca=Buffer.from(rootCertificates[0]),caFile=path.join(root,'ca.pem');await writeFile(caFile,ca);if(localHost===undefined)args.push('--ca',caFile,'--ca-sha256',hash(ca)) + const output=path.join(root,'inspection.json') + async function inspect(destination=output){return command(['maintenance-inspect',...args,'--output',destination])} + async function approve(change?:(value:Record)=>void){const raw=await readFile(output),inspection=JSON.parse(raw.toString()),approval={format:'database-maintenance-current-state-approval-v1',resolutionId:randomUUID(),decision:'accept-observed-current-state',actorId:'host-human-reviewer',inspectionSha256:hash(raw),issuedAt:new Date().toISOString(),expiresAt:new Date(Date.now()+60000).toISOString(),managedBackendStopped:true,historicOutcomesRemainUnknown:true,automaticRestore:false};assert(inspection.createdAt<=approval.issuedAt);change?.(approval);const bytes=Buffer.from(JSON.stringify(approval)),file=path.join(root,'approval.json');await writeFile(file,bytes);return ['maintenance-resolve',...args,'--inspection',output,'--inspection-sha256',hash(raw),'--approval',file,'--approval-sha256',hash(bytes)]} + const toLocal=async(host='127.0.0.1')=>{const raw=Buffer.from(JSON.stringify(localTransport(host)));await writeFile(path.join(root,'transport.json'),raw);args[args.indexOf('--transport-sha256')+1]=hash(raw);for(const flag of ['--ca','--ca-sha256']){const at=args.indexOf(flag);if(at>=0)args.splice(at,2)}} + const configuration=exports.maintenancePoolConfiguration as (value:unknown,ca?:Buffer,pin?:string)=>{options:Record;transportBinding:{mode:string;endpointSha256:string}} + return {command,inspect,approve,toLocal,configuration,readRecords,state,output,sql:()=>sql,published:()=>published,failRelease:()=>{releaseFailure=true},failEnd:()=>{poolEndFailure=true},driftSchema:()=>{schema.sha256='0'.repeat(64)},driftTarget:()=>{source.databaseOid='54321'},wrongOwner:()=>{wrongOwner=true},withControlSchema:()=>{controlSchema=true},scopes:()=>capturedScopes,failObservation:(error:Error)=>{observationFailure=error}} +} +test('actual operator commands inspect then separately approved resolve; old uncertain bytes never rewritten',()=>own(async root=>{ + const fixture=await model(root),journal=path.join(fixture.state,'database-maintenance.jsonl'),before=await readFile(journal),inspected=await fixture.inspect();assert.equal(inspected.humanApprovalRequired,true);assert.equal((await fixture.readRecords(fixture.state)).length,0) + const result=await fixture.command(await fixture.approve());assert.equal(result.outcome,'committed');assert.equal(result.historicOutcomesChanged,false);assert.equal(result.archiveDataEquivalenceProved,false);assert.equal(result.automaticRestore,false) + assert.deepEqual(await readFile(journal),before);const records=await fixture.readRecords(fixture.state),facts=await strictReader.readMaintenanceRecoveryFacts(fixture.state);assert.equal(records.length,1);const accepted=grammar.evaluateMaintenanceAcceptances(records,facts.journal,facts.vault.evidence,grammar.runtimeDatabaseTargetSha256('postgresql://db.example.invalid:5432/managed?sslmode=require'));assert.equal(accepted.count,1) + fixture.driftSchema();const afterStatus=await fixture.inspect(path.join(root,'after-inspection.json'));assert.equal(afterStatus.state,'historically-accepted');assert.equal(afterStatus.humanApprovalRequired,false);assert.equal(afterStatus.historicOutcomesChanged,false) + await assert.rejects(fixture.command(await fixture.approve()),/No new unresolved|already|assert|Assertion/i) +})) +for(const type of ['expired','not-stopped','wrong-inspection'])test('actual resolve refuses '+type+' approval before connecting SQL',()=>own(async root=>{ + const fixture=await model(root);await fixture.inspect();const args=await fixture.approve(value=>{if(type==='expired')value.expiresAt=new Date(Date.now()-1).toISOString();if(type==='not-stopped')value.managedBackendStopped=false;if(type==='wrong-inspection')value.inspectionSha256='0'.repeat(64)}),before=fixture.sql();await assert.rejects(fixture.command(args));assert.equal(fixture.sql(),before);assert.equal((await fixture.readRecords(fixture.state)).length,0) +})) +for(const type of ['release','pool-end','schema-drift','target-drift','external-drift'])test('actual resolve refuses '+type+' before acceptance publication',()=>own(async root=>{ + const fixture=await model(root);await fixture.inspect();const args=await fixture.approve();if(type==='release')fixture.failRelease();if(type==='pool-end')fixture.failEnd() + if(type==='schema-drift')fixture.driftSchema();if(type==='target-drift')fixture.driftTarget() + if(type==='external-drift')await appendFileSafe(path.join(fixture.state,'database-maintenance.jsonl')) + await assert.rejects(fixture.command(args));assert.equal((await fixture.readRecords(fixture.state)).length,0) +})) +async function appendFileSafe(file:string){await writeFile(file,await readFile(file,'utf8')+JSON.stringify({operationId:randomUUID(),state:'intent',at:new Date().toISOString()})+'\n')} +test('existing shipped main dispatches both commands before writer-profile role/source flags',async()=>{ + const source=await readFile(path.join(backend,'scripts/database-writer-profile-operator.mts'),'utf8');assert(source.indexOf("['maintenance-inspect','maintenance-resolve']")own(async root=>{ + const fixture=await model(root);fixture.wrongOwner();await assert.rejects(fixture.inspect());assert.equal(fixture.sql(),0);assert.equal(fixture.published(),0);assert.equal((await fixture.readRecords(fixture.state)).length,0) +})) + +test('explicit local-loopback supports both IP families and binds selected mode in actual inspect/resolve',async()=>{ + for(const host of ['127.0.0.1','::1'])await own(async root=>{const fixture=await model(root,host);await fixture.inspect();const result=await fixture.command(await fixture.approve());assert.equal(result.outcome,'committed');const records=await fixture.readRecords(fixture.state);assert.equal(JSON.parse(records[0].toString()).knownCurrent.transport.mode,'local-loopback')}) +}) +test('plaintext remote/DNS/unspecified/mapped hosts refuse before SQL and never fall back',async()=>{ + for(const host of ['db.example.invalid','localhost','0.0.0.0','192.0.2.1','[::1]','::ffff:127.0.0.1'])await own(async root=>{const fixture=await model(root,host);await assert.rejects(fixture.inspect());assert.equal(fixture.sql(),0);assert.equal(fixture.published(),0)}) +}) +test('an approval for TLS observation cannot resolve through a changed local-loopback transport',()=>own(async root=>{ + const fixture=await model(root);await fixture.inspect();await fixture.toLocal();await assert.rejects(fixture.command(await fixture.approve()));assert.equal((await fixture.readRecords(fixture.state)).length,0) +})) +test('local mode explicitly disables SSL/environment substitution; TLS remains verified default without fallback',()=>own(async root=>{ + const fixture=await model(root),ConnectionParameters=require('pg/lib/connection-parameters'),raw={format:'database-maintenance-transport-v1',mode:'local-loopback',connection:{host:'127.0.0.1',port:5432,user:'existing-user',password:'synthetic-only',database:'managed'}} + const previous={PGHOST:process.env.PGHOST,PGSSLMODE:process.env.PGSSLMODE,PGOPTIONS:process.env.PGOPTIONS};process.env.PGHOST='remote.invalid';process.env.PGSSLMODE='require';process.env.PGOPTIONS='-c role=foreign' + try{const config=fixture.configuration(raw),actual=new ConnectionParameters(config.options);assert.equal(actual.host,'127.0.0.1');assert.equal(actual.ssl,false);assert.equal(actual.options,contract.STARTUP_OPTIONS);assert.equal(config.transportBinding.mode,'local-loopback')} + finally{for(const [key,value]of Object.entries(previous)){if(value===undefined)delete process.env[key];else process.env[key]=value}} + const ca=Buffer.from(rootCertificates[0]),tls={format:'database-maintenance-transport-v1',connection:{...raw.connection,host:'db.example.invalid'},tls:{serverName:'db.example.invalid',spkiSha256:'f'.repeat(64)}} + assert.throws(()=>fixture.configuration(tls));const secure=fixture.configuration(tls,ca,hash(ca));assert.equal(secure.transportBinding.mode,'tls');assert.equal((secure.options.ssl as {rejectUnauthorized:boolean}).rejectUnauthorized,true) + assert.throws(()=>fixture.configuration({...raw,mode:'plaintext'}));assert.throws(()=>fixture.configuration({...tls,mode:null},ca,hash(ca)));assert.throws(()=>fixture.configuration(raw,ca,hash(ca))) +})) + +// A normal installation has no optional writer-profile schema. Observation must +// select its exact namespace set without requiring installation of that system. +test('maintenance observes with and without the optional control namespace',async()=>{ + for(const present of [false,true])await own(async root=>{const fixture=await model(root);if(present)fixture.withControlSchema();await fixture.inspect();assert.equal(JSON.stringify(fixture.scopes()),JSON.stringify([{applicationNamespaces:['public'],excludedControlNamespaces:present?['mmapp_control']:[]}]))}) +}) +test('original observation failure survives confirmed rollback/unlock/release',()=>own(async root=>{ + const fixture=await model(root),failure=Error('Synthetic original observation failure');fixture.failObservation(failure);await assert.rejects(fixture.inspect(),error=>error===failure);assert.equal(fixture.published(),0);assert.equal((await fixture.readRecords(fixture.state)).length,0) +})) diff --git a/backend/scripts/database-operator-sources.mjs b/backend/scripts/database-operator-sources.mjs new file mode 100644 index 00000000..9bd71bfa --- /dev/null +++ b/backend/scripts/database-operator-sources.mjs @@ -0,0 +1,17 @@ +/** The fixed build inputs shared by the operator and Docker staging. */ +export const OPERATOR_SOURCES=Object.freeze([ + 'database-writer-bootstrap-operator.mjs','database-writer-profile-operator.mts', + 'writer-profile/operator-files.mjs','writer-profile/operator-contract.mjs','writer-profile/publisher.mjs','writer-profile/context.mjs', + 'writer-profile/protected-source-registry.mts','writer-profile/maintenance-resolution.mts','writer-profile/source-activation.mts', + 'writer-profile/legacy-cold-verification.mts', + 'writer-profile/legacy-handoff-execution.mts', + 'writer-profile/legacy-control-reference.mts', + 'writer-profile/source58-foundation.mts', + 'writer-profile/source57-admission-plan.mts', + 'writer-profile/registry-enrollment.mts', + 'writer-profile/marker-fence-enrollment.mts', + 'writer-profile/catalogue.mts','writer-profile/catalogue-value-bridge.ts','writer-profile/catalogue-projection.ts','writer-profile/catalogue-leaves.ts', + 'writer-profile/application-writer-profile-catalogue.ts','writer-profile/application-writer-profile-catalogue-values.ts', + 'writer-profile/application-writer-profile-catalogue-parent-policy.ts','writer-profile/application-writer-profile-canonical.ts', + 'writer-profile/fixed44-build-sql.ts','writer-profile/fixed44-build-definition.ts','writer-profile/fixed56-build-definition.ts','writer-profile/fixed56-build-delta.ts','writer-profile/fixed57-build-definition.ts','writer-profile/fixed58-build-definition.ts','writer-profile/expanded44-data-policy.ts', +]) diff --git a/backend/scripts/database-source-binding-operator.ts b/backend/scripts/database-source-binding-operator.ts new file mode 100644 index 00000000..c4f892dd --- /dev/null +++ b/backend/scripts/database-source-binding-operator.ts @@ -0,0 +1,74 @@ +/** Offline operator-only issuer proposal. Never imported by runtime/startup. + * Effective key/registry ACL isolation is required before deployment acceptance; + * mode bits and possession of CLI arguments do not establish that isolation. */ +import { createPrivateKey, createPublicKey, sign } from "node:crypto" +import { mkdir, open, realpath } from "node:fs/promises" +import path from "node:path" +import { z } from "zod" +import { DATABASE_SOURCE_SIGNATURE_DOMAINS, sourceBindingBodySchema, sourceBindingBytes, sourceBindingCanonical, sourceBindingSha256, + readDatabaseSourceBindingRegistry, type DatabaseSourceDeploymentPin } from "../src/db/database-source-binding.js" + +const requestSchema = sourceBindingBodySchema.omit({ format: true, ordinal: true }) +export type DatabaseSourceOperatorRequest = z.infer +function outside(parent: string, child: string) { + const relative = path.relative(parent, child) + return !!relative && (relative.startsWith(".." + path.sep) || relative === ".." || path.isAbsolute(relative)) +} +export async function provisionDatabaseSourceBindingOffline(options: { request: DatabaseSourceOperatorRequest; privateKeyFile: string; + outputDirectory: string; previousPin?: DatabaseSourceDeploymentPin }) { + const request = requestSchema.parse(options.request) + const privatePath = await realpath(options.privateKeyFile), output = path.resolve(options.outputDirectory) + const parent = await realpath(path.dirname(output)) + if (parent !== path.dirname(output) || !outside(output, privatePath)) throw Error("Private operator key must be outside new registry, without ambiguous parent links") + const stat = await open(privatePath, "r") + let privateBytes: Buffer + try { const meta = await stat.stat(); if (!meta.isFile() || meta.size < 1 || meta.size > 8192) throw Error("Bounded operator private key required"); privateBytes = await stat.readFile() } + finally { await stat.close() } + const privateKey = createPrivateKey(privateBytes); privateBytes.fill(0) + if (privateKey.type !== "private" || privateKey.asymmetricKeyType !== "ed25519") throw Error("Independent Ed25519 operator key required") + const publicKey = createPublicKey(privateKey), spki = publicKey.export({ type: "spki", format: "der" }) + const publicKeySpkiSha256 = sourceBindingSha256(spki) + const previous = options.previousPin ? await readDatabaseSourceBindingRegistry(options.previousPin) : undefined + if (previous && (previous.body.installationId !== request.installationId || previous.body.sourceIdentityId !== request.sourceIdentityId || + previous.publicKeySpkiSha256 !== publicKeySpkiSha256 || previous.body.generation === request.generation || previous.body.ordinal >= Number.MAX_SAFE_INTEGER)) throw Error("Previous current head/identity/key or distinct new generation required") + const body = sourceBindingBodySchema.parse({ ...request, format: "database-source-binding-v1", ordinal: previous ? previous.body.ordinal + 1 : 1 }) + const signed = (domain: string, payload: unknown) => sourceBindingBytes({ body: payload, + signature: sign(null, Buffer.from(domain + sourceBindingCanonical(payload)), privateKey).toString("base64url") }) + const bindingBytes = signed(DATABASE_SOURCE_SIGNATURE_DOMAINS.binding, body), bindingSha256 = sourceBindingSha256(bindingBytes) + const head = { format: "database-source-registry-head-v1", installationId: body.installationId, sourceIdentityId: body.sourceIdentityId, + generation: body.generation, ordinal: body.ordinal, bindingSha256, previousHeadSha256: previous?.headSha256 ?? null } + const headBytes = signed(DATABASE_SOURCE_SIGNATURE_DOMAINS.head, head), currentHeadSha256 = sourceBindingSha256(headBytes) + // No recursive mkdir and no existing-state adoption. A partial output remains + // for explicit operator review; uncertainty is never retried/overwritten. + await mkdir(output, { mode: 0o700 }) + const publish = async (filename: string, bytes: Buffer | string) => { + const handle = await open(path.join(output, filename), "wx", 0o600) + try { await handle.writeFile(bytes); await handle.sync() } finally { await handle.close() } + } + await publish("operator-public.pem", publicKey.export({ type: "spki", format: "pem" })) + await publish("binding-" + bindingSha256 + ".json", bindingBytes) + await publish("head-" + currentHeadSha256 + ".json", headBytes) + const deploymentPin = { registryDirectory: output, installationId: body.installationId, publicKeySpkiSha256, currentHeadSha256 } + // Public review artifact only; not installed into any application config. + await publish("proposed-deployment-pin.json", sourceBindingBytes(deploymentPin)) + return Object.freeze({ deploymentPin: Object.freeze(deploymentPin), keyIsolationVerified: false as const, + platformProtectionVerified: false as const, generationAuthenticated: false as const, readyAuthorized: false as const }) +} + +export async function databaseSourceBindingOperatorCli(args: string[]) { + const allowed = new Set(["request", "private-key", "output", "previous-pin"]), values = new Map() + for (const arg of args) { + const match = /^--([a-z-]+)=(.+)$/.exec(arg) + if (!match || !allowed.has(match[1]) || values.has(match[1])) throw Error("Exact explicit offline operator arguments required") + values.set(match[1], match[2]) + } + if (!["request", "private-key", "output"].every(key => values.has(key))) throw Error("Missing explicit offline operator arguments") + const boundedJson = async (filename: string) => { + const handle = await open(filename, "r") + try { const stat = await handle.stat(); if (!stat.isFile() || stat.size < 1 || stat.size > 8192) throw Error("Bounded operator input required"); return JSON.parse(await handle.readFile("utf8")) as unknown } + finally { await handle.close() } + } + const request = requestSchema.parse(await boundedJson(values.get("request")!)) + const previousPin = values.has("previous-pin") ? await boundedJson(values.get("previous-pin")!) as DatabaseSourceDeploymentPin : undefined + return provisionDatabaseSourceBindingOffline({ request, privateKeyFile: values.get("private-key")!, outputDirectory: values.get("output")!, previousPin }) +} diff --git a/backend/scripts/database-writer-bootstrap-operator.mjs b/backend/scripts/database-writer-bootstrap-operator.mjs new file mode 100644 index 00000000..ada79f75 --- /dev/null +++ b/backend/scripts/database-writer-bootstrap-operator.mjs @@ -0,0 +1,105 @@ +/** Explicit offline operator init. No database, application imports or activation. */ +import assert from 'node:assert/strict' +import {createHash, generateKeyPairSync} from 'node:crypto' +import {constants} from 'node:fs' +import {lstat, realpath, open, mkdir, link, unlink} from 'node:fs/promises' +import path from 'node:path' +import {pathToFileURL} from 'node:url' + +const digest = bytes => createHash('sha256').update(bytes).digest('hex') +const hex = value => typeof value === 'string' && /^[a-f0-9]{64}$/.test(value) +const uuid = value => typeof value === 'string' && /^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(value) +const exact = (value, keys) => value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === keys.length && keys.every(key => Object.hasOwn(value, key)) +const roles = ['runtime', 'operator', 'capture', 'publisher', 'control'] +export function deploymentRequest(value) { + assert(exact(value, ['format', 'installationId', 'sourceHeadSha256', 'source', 'roles', 'tls'])) + assert(value.format === 'application-writer-bootstrap-request-v1' && uuid(value.installationId) && hex(value.sourceHeadSha256)) + assert(exact(value.source, ['systemIdentifier', 'databaseOid', 'databaseName', 'serverVersionNum', 'inRecovery'])) + assert(typeof value.source.systemIdentifier === 'string' && /^[1-9][0-9]{0,19}$/.test(value.source.systemIdentifier) && BigInt(value.source.systemIdentifier) <= 18446744073709551615n) + assert(typeof value.source.databaseOid === 'string' && /^[1-9][0-9]{0,9}$/.test(value.source.databaseOid) && BigInt(value.source.databaseOid) <= 4294967295n) + assert(typeof value.source.databaseName === 'string' && /^[^\x00-\x1f\x7f]{1,63}$/.test(value.source.databaseName)) + assert(Number.isInteger(value.source.serverVersionNum) && value.source.serverVersionNum >= 180000 && value.source.serverVersionNum <= 189999 && value.source.inRecovery === false) + assert(exact(value.roles, roles)) + for (const role of roles) { + const pair = value.roles[role] + assert(exact(pair, ['name', 'signedOid']) && typeof pair.name === 'string' && /^[a-z_][a-z0-9_]{0,62}$/.test(pair.name)) + assert(typeof pair.signedOid === 'string' && /^[1-9][0-9]{0,9}$/.test(pair.signedOid) && BigInt(pair.signedOid) <= 4294967295n) + } + assert(new Set(roles.map(role => value.roles[role].name)).size === 5 && new Set(roles.map(role => value.roles[role].signedOid)).size === 5) + assert(exact(value.tls, ['serverName', 'caSha256', 'spkiSha256']) && typeof value.tls.serverName === 'string' && /^[a-zA-Z0-9][a-zA-Z0-9.-]{0,252}$/.test(value.tls.serverName) && hex(value.tls.caSha256) && hex(value.tls.spkiSha256)) + return structuredClone(value) +} +export function createBootstrapMaterial(raw, requestSha256) { + const request = deploymentRequest(raw) + assert(hex(requestSha256)) + const {privateKey, publicKey} = generateKeyPairSync('ed25519') + const publicKeyPem = publicKey.export({format: 'pem', type: 'spki'}).toString() + const publicKeySpkiSha256 = digest(publicKey.export({format: 'der', type: 'spki'})) + return { + privateKeyPem: privateKey.export({format: 'pem', type: 'pkcs8'}).toString(), publicKeyPem, + bootstrap: {format: 'application-writer-bootstrap-v1', verificationSubject: {installationId: request.installationId, publicKeyPem, publicKeySpkiSha256}, + deploymentRequest: request, deploymentRequestSha256: requestSha256, keyOrigin: 'explicit-offline-operator-init', + databaseObserved: false, rolesAuthenticated: false, sourceAuthenticated: false, approvedDefinitionSha256: null, + publicationAuthorized: false, generationAuthenticated: false, readyAuthorized: false}, + } +} +async function protectedDirectory(directory) { + assert(process.platform !== 'win32' && typeof process.getuid === 'function', 'Protected POSIX ownership required; Windows ACL backend unavailable') + assert(path.isAbsolute(directory) && await realpath(directory) === directory) + const uid = process.getuid(), stat = await lstat(directory) + assert(stat.isDirectory() && !stat.isSymbolicLink() && stat.uid === uid && (stat.mode & 0o077) === 0) + for (let ancestor = path.dirname(directory);; ancestor = path.dirname(ancestor)) { + const metadata = await lstat(ancestor) + assert(metadata.isDirectory() && !metadata.isSymbolicLink() && (metadata.uid === uid || metadata.uid === 0)) + assert((metadata.mode & 0o022) === 0 || metadata.uid === 0 && (metadata.mode & 0o1000) !== 0) + if (ancestor === path.dirname(ancestor)) break + } + return stat +} +async function publish(directory, name, bytes) { + await protectedDirectory(directory) + const temporary = path.join(directory, '.' + name + '.pending'), destination = path.join(directory, name) + const handle = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600) + try { await handle.writeFile(bytes); await handle.sync() } finally { await handle.close() } + await link(temporary, destination) // Atomic no-replace publication, never rename-overwrite. + await unlink(temporary) + const parent = await open(directory, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW) + try { await parent.sync() } finally { await parent.close() } +} +export async function initializeBootstrap({outputDirectory, deploymentFile, deploymentSha256}) { + assert(path.isAbsolute(outputDirectory) && path.resolve(outputDirectory) === outputDirectory && hex(deploymentSha256)) + await protectedDirectory(path.dirname(outputDirectory)) + assert(path.isAbsolute(deploymentFile) && await realpath(deploymentFile) === deploymentFile) + const requestHandle = await open(deploymentFile, constants.O_RDONLY | constants.O_NOFOLLOW) + let bytes + try { + const before = await requestHandle.stat() + assert(before.isFile() && before.uid === process.getuid() && (before.mode & 0o077) === 0 && before.size > 0 && before.size <= 16384) + bytes = Buffer.alloc(before.size + 1) + let size = 0 + while (size < bytes.length) { const read = await requestHandle.read(bytes, size, bytes.length - size, size); if (!read.bytesRead) break; size += read.bytesRead } + const after = await requestHandle.stat() + assert(size === before.size && after.size === before.size && after.mtimeMs === before.mtimeMs && after.ctimeMs === before.ctimeMs) + bytes = bytes.subarray(0, size) + } finally { await requestHandle.close() } + assert(digest(bytes) === deploymentSha256, 'Explicit independently approved deployment digest required') + const request = deploymentRequest(JSON.parse(new TextDecoder('utf-8', {fatal: true}).decode(bytes))) + await mkdir(outputDirectory, {mode: 0o700}) // EEXIST refuses; never adopt/overwrite. + const material = createBootstrapMaterial(request, deploymentSha256) + await publish(outputDirectory, 'signing-private.pem', material.privateKeyPem) + await publish(outputDirectory, 'operator-public.pem', material.publicKeyPem) + await publish(outputDirectory, 'bootstrap.json', JSON.stringify(material.bootstrap, null, 2) + '\n') // Completion marker last. + return {event: 'application-writer-bootstrap-initialized', installationId: request.installationId, + publicKeySpkiSha256: material.bootstrap.verificationSubject.publicKeySpkiSha256, + deploymentRequestSha256: deploymentSha256, databaseObserved: false, publicationAuthorized: false, readyAuthorized: false} +} +export async function main(args) { + assert(args.length === 7 && args[0] === 'init') + const values = new Map() + for (let i = 1; i < args.length; i += 2) { assert(['--output', '--deployment', '--deployment-sha256'].includes(args[i]) && !values.has(args[i])); values.set(args[i], args[i + 1]) } + return initializeBootstrap({outputDirectory: values.get('--output'), deploymentFile: values.get('--deployment'), deploymentSha256: values.get('--deployment-sha256')}) +} +if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) { + try { console.log(JSON.stringify(await main(process.argv.slice(2)))) } + catch { console.error(JSON.stringify({event: 'application-writer-bootstrap-refused', publicationAuthorized: false, readyAuthorized: false})); process.exitCode = 1 } +} diff --git a/backend/scripts/database-writer-bootstrap-operator.test.mjs b/backend/scripts/database-writer-bootstrap-operator.test.mjs new file mode 100644 index 00000000..e9dcca71 --- /dev/null +++ b/backend/scripts/database-writer-bootstrap-operator.test.mjs @@ -0,0 +1,43 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import {createHash, createPrivateKey, createPublicKey, sign, verify} from 'node:crypto' +import {mkdtemp, rm, writeFile, readdir, readFile, stat, chmod} from 'node:fs/promises' +import path from 'node:path' +import {tmpdir} from 'node:os' +import {deploymentRequest, createBootstrapMaterial, initializeBootstrap, main} from './database-writer-bootstrap-operator.mjs' +const hash = b => createHash('sha256').update(b).digest('hex') +const request = () => ({format: 'application-writer-bootstrap-request-v1', installationId: 'f8b410ef-11ba-4419-b6a6-e735e505450b', sourceHeadSha256: 'a'.repeat(64), + source: {systemIdentifier: '7387529813445422155', databaseOid: '16384', databaseName: 'operator-fixture', serverVersionNum: 180004, inRecovery: false}, + roles: Object.fromEntries(['runtime', 'operator', 'capture', 'publisher', 'control'].map((name, i) => [name, {name: 'fixture_' + name, signedOid: String(20000 + i)}])), + tls: {serverName: 'db.example.invalid', caSha256: 'b'.repeat(64), spkiSha256: 'c'.repeat(64)}}) +test('real distinct Ed25519 keypairs sign/verify and preserve externally pinned subject without authorization', () => { + const value = request(), a = createBootstrapMaterial(value, 'd'.repeat(64)), b = createBootstrapMaterial(value, 'd'.repeat(64)) + const privateKey = createPrivateKey(a.privateKeyPem), publicKey = createPublicKey(a.publicKeyPem), message = Buffer.from('owned-key-roundtrip') + assert(verify(null, message, publicKey, sign(null, message, privateKey))); assert(!verify(null, Buffer.from('changed'), publicKey, sign(null, message, privateKey))) + assert.equal(hash(publicKey.export({format: 'der', type: 'spki'})), a.bootstrap.verificationSubject.publicKeySpkiSha256) + assert.notEqual(a.publicKeyPem, b.publicKeyPem); assert.equal(a.bootstrap.verificationSubject.installationId, value.installationId) + assert.equal(a.bootstrap.publicationAuthorized, false); assert.equal(a.bootstrap.readyAuthorized, false); assert.equal(a.bootstrap.approvedDefinitionSha256, null) + assert.equal(a.bootstrap.databaseObserved, false); assert(!JSON.stringify(a.bootstrap).includes('PRIVATE KEY')); value.roles.runtime.name = 'changed'; assert.notEqual(a.bootstrap.deploymentRequest.roles.runtime.name, 'changed') +}) +test('missing roles/head/transport pin and malformed identities refuse rather than invent trust', () => { + for (const change of [v => delete v.roles.control, v => delete v.sourceHeadSha256, v => delete v.tls.spkiSha256, v => v.roles.operator = v.roles.runtime, v => v.source.databaseOid = 16384, v => v.source.inRecovery = true, v => v.extra = true]) { + const value = request(); change(value); assert.throws(() => deploymentRequest(value)) + } +}) +test('unknown/duplicate CLI options refuse without an implicit startup mode', async () => { + for (const args of [[], ['publish'], ['init', '--output', '/x', '--output', '/y', '--deployment-sha256', 'd'.repeat(64)]]) await assert.rejects(main(args)) +}) +test('actual protected filesystem init, no-replace and mismatched digest; unsupported Windows writes nothing', async () => { + const parent = await mkdtemp(path.join(tmpdir(), 'mm-bootstrap-init-')), output = path.join(parent, 'keys'), file = path.join(parent, 'deployment.json'), bytes = Buffer.from(JSON.stringify(request())) + try { + await chmod(parent, 0o700); await writeFile(file, bytes, {mode: 0o600}) + const options = {outputDirectory: output, deploymentFile: file, deploymentSha256: hash(bytes)} + if (process.platform === 'win32') { await assert.rejects(initializeBootstrap(options), /Windows ACL backend unavailable/); assert.deepEqual(await readdir(parent), ['deployment.json']); return } + await assert.rejects(initializeBootstrap({...options, deploymentSha256: '0'.repeat(64)})); assert.deepEqual(await readdir(parent), ['deployment.json']) + const receipt = await initializeBootstrap(options); assert.equal(receipt.publicationAuthorized, false); assert(!JSON.stringify(receipt).includes('PRIVATE KEY')) + assert.equal((await stat(output)).mode & 0o777, 0o700); assert.equal((await stat(path.join(output, 'signing-private.pem'))).mode & 0o777, 0o600) + const first = await readFile(path.join(output, 'signing-private.pem')); await assert.rejects(initializeBootstrap(options), {code: 'EEXIST'}); assert.deepEqual(await readFile(path.join(output, 'signing-private.pem')), first) + assert.deepEqual((await readdir(output)).sort(), ['bootstrap.json', 'operator-public.pem', 'signing-private.pem']) + assert.equal(JSON.parse(await readFile(path.join(output, 'bootstrap.json'), 'utf8')).verificationSubject.publicKeySpkiSha256, receipt.publicKeySpkiSha256) + } finally { await rm(parent, {recursive: true, force: true}) } +}) diff --git a/backend/scripts/database-writer-profile-operator.mts b/backend/scripts/database-writer-profile-operator.mts new file mode 100644 index 00000000..0792f264 --- /dev/null +++ b/backend/scripts/database-writer-profile-operator.mts @@ -0,0 +1,186 @@ +/** Explicit operator process only. No HTTP, runtime import, role provisioning or READY. */ +import assert from 'node:assert/strict' +import path from 'node:path' +import {pathToFileURL} from 'node:url' +import {createPrivateKey,createPublicKey,sign} from 'node:crypto' +import {readdir} from 'node:fs/promises' +import {readProtected,protectedDirectory,sha,withJournalLease,writeFile} from './writer-profile/operator-files.mjs' +import {approval,upgradeApproval,reconciliationApproval,poolOptions,roleKeys,assertJsDriverEnvironment} from './writer-profile/operator-contract.mjs' +import {deploymentRequest} from './database-writer-bootstrap-operator.mjs' +const hex=(v:unknown)=>typeof v==='string'&&/^[a-f0-9]{64}$/.test(v) +const parse=(b:Buffer)=>JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(b)) +const sourceCommands=['source-activate','source-revoke','source-status'] +export async function main(args:string[]){ + if(args[0]==='legacy-cold-verify')return (await import('./writer-profile/legacy-cold-verification.mjs')).verifyLegacyColdMain(args) + if(args[0]==='source58-foundation-enroll'||args[0]==='source57-admission-enroll')return (await import('./writer-profile/source58-foundation.mjs')).source58FoundationMain(args) + if(args[0]==='legacy-control-reference'||args[0]==='legacy-source58-preflight')return (await import('./writer-profile/legacy-control-reference.mjs')).legacyControlReferenceMain(args) + if(['legacy-handoff-execute','legacy-handoff-adopt','legacy-handoff-release'].includes(args[0]))return (await import('./writer-profile/legacy-handoff-execution.mjs')).legacyHandoffExecuteMain(args) + if(['registry-enroll','registry-inspect','restore-profile-enroll'].includes(args[0]))return (await import('./writer-profile/registry-enrollment.mjs')).registryEnrollmentMain(args) + if(args[0]==='marker-fence-enroll')return (await import('./writer-profile/marker-fence-enrollment.mjs')).markerFenceEnrollmentMain(args) + if(sourceCommands.includes(args[0]))return (await import('./writer-profile/source-activation.mjs')).sourceActivationMain(args) + if(['maintenance-inspect','maintenance-resolve'].includes(args[0]))return (await import('./writer-profile/maintenance-resolution.mjs')).maintenanceMain(args) + const legacyCustody=args[0]==='legacy-custody' + const legacyHandoff=args[0]==='legacy-handoff-inspect' + const legacyExport=legacyCustody||args[0]==='legacy-export' + const legacyInspection=legacyExport||legacyHandoff||args[0]==='legacy-inspect' + const catalogueInspection=args[0]==='source-catalogue-inspect' + const activePreflight=args[0]==='source-preflight',preflight=activePreflight||args[0]==='preflight' + const upgrade=args[0]==='upgrade' + const writes=upgrade||args[0]==='publish' + const flags=['bootstrap','bootstrap-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256'] + if(!legacyInspection)flags.push('definition','definition-sha256') + if(!legacyInspection&&!catalogueInspection)flags.push('approval','approval-sha256','journal') + if(legacyInspection||catalogueInspection||activePreflight||writes)flags.push('activation-directory','activation-sha256') + if(legacyExport)flags.push('archive-directory','source-signing-key') + if(legacyHandoff)flags.push('archive-directory','handoff-intent-sha256') + assert((legacyInspection||catalogueInspection||preflight||writes||args[0]==='reconcile')&&args.length===1+2*flags.length) + assertJsDriverEnvironment(process.env) + const values=new Map() + for(let i=1;ivalues.get(name)! + for(const flag of flags.filter(f=>f.endsWith('sha256')))assert(hex(get(flag))) + const bootstrap=parse(await readProtected(get('bootstrap'),32768,get('bootstrap-sha256'))) + assert(bootstrap.format==='application-writer-bootstrap-v1'&&bootstrap.publicationAuthorized===false&&bootstrap.readyAuthorized===false) + const request=deploymentRequest(bootstrap.deploymentRequest),subject=bootstrap.verificationSubject + assert(subject.installationId===request.installationId&&hex(subject.publicKeySpkiSha256)&&sha(createPublicKey(subject.publicKeyPem).export({format:'der',type:'spki'}))===subject.publicKeySpkiSha256) + const pinned=legacyInspection||catalogueInspection?undefined:(args[0]==='reconcile'?reconciliationApproval:upgrade?upgradeApproval:approval)(parse(await readProtected(get('approval'),16384,get('approval-sha256')))) + const definition=legacyInspection?undefined:parse(await readProtected(get('definition'),4194304,get('definition-sha256'))) + const transport=parse(await readProtected(get('transport'),16384,get('transport-sha256'))) + const ca=await readProtected(get('ca'),65536,request.tls.caSha256) + const options=poolOptions(transport,request,ca) + if(!legacyInspection&&!catalogueInspection)await protectedDirectory(get('journal')) + await protectedDirectory(get('source-registry')) + const privateKeyFile=path.join(path.dirname(get('bootstrap')),'signing-private.pem') + if(writes){const key=await readProtected(privateKeyFile,8192);key.fill(0)} + // All explicit trust files are checked before app helper/PG imports. No .env URL is used. + // Existing source helper loads config/dotenv. Complete it BEFORE pg's first + // import so a newly loaded NODE_PG_FORCE_NATIVE cannot select another driver. + const {createProtectedDatabaseSourceBindingVerifier}=await import('./writer-profile/protected-source-registry.mjs') + assertJsDriverEnvironment(process.env) + const [{default:pg},{createConnectedPublisher,requireCurrentCatalogueDefinition},{collectSource44Catalogue,collectSource56Catalogue,collectSource57Catalogue,collectSource58Catalogue,approvedDefinition}]=await Promise.all([ + import('pg'),import('./writer-profile/publisher.mjs'),import('./writer-profile/catalogue.mjs')]) + assertJsDriverEnvironment(process.env) + const reviewedDefinition=legacyInspection?undefined:approvedDefinition(definition) + const collectCatalogues={44:collectSource44Catalogue,56:collectSource56Catalogue,57:collectSource57Catalogue,58:collectSource58Catalogue} + const collectCatalogue=collectCatalogues[reviewedDefinition&&'edition' in reviewedDefinition?reviewedDefinition.edition:44] + if(!legacyInspection&&!catalogueInspection){ + assert.equal(reviewedDefinition!.definitionSha256,pinned!.definitionSha256) + if(args[0]!=='reconcile')requireCurrentCatalogueDefinition(pinned,reviewedDefinition) + } + const installation=Object.freeze({...subject,roles:Object.fromEntries(roleKeys.map(k=>[k,request.roles[k].signedOid])),roleBindings:request.roles,expectedSource:request.source,tls:Object.freeze({...request.tls})}) + const sourcePin={registryDirectory:get('source-registry'),installationId:subject.installationId,publicKeySpkiSha256:get('source-spki-sha256'),currentHeadSha256:request.sourceHeadSha256} + const archiveDirectory=legacyExport?get('archive-directory'):undefined + let handoff + if(legacyHandoff){ + const {verifyLegacyProbeHandoffIntent}=await import('../src/services/server-probe-legacy-handoff-inspection.js') + const directory=get('archive-directory'),maximum=64*1024*1024 + const [archive,signedApproval,coldArchive,intent,operatorPublicKeyPem]=await Promise.all([ + readProtected(path.join(directory,'legacy-probe.dump'),maximum),readProtected(path.join(directory,'legacy-probe-approval.json'),4096), + readProtected(path.join(directory,'legacy-probe-cold.dump'),maximum), + readProtected(path.join(directory,'legacy-probe-handoff-intent.json'),4096,get('handoff-intent-sha256')), + readProtected(path.join(sourcePin.registryDirectory,'operator-public.pem'),4096), + ]) + handoff=await verifyLegacyProbeHandoffIntent(sourcePin,{archive,signedApproval,coldArchive,intent,operatorPublicKeyPem}) + } + // Use the same protected registry evidence for inspection and publication. + const verifier=createProtectedDatabaseSourceBindingVerifier(sourcePin) + async function connected(sourceVerifier:typeof verifier,deadlineAt:number){ + const publisher=createConnectedPublisher(installation,(s:Parameters[0])=>collectCatalogue(s,installation),sourceVerifier,pinned,reviewedDefinition) + const pool=new pg.Pool(options) + // A pool-level async error never becomes raw stdout or a retry signal. + let asyncFailure=false;pool.on('error',()=>{asyncFailure=true}) + let result,primary:unknown,failed=false + try{ + const journalDirectory=get('journal') + if(legacyInspection)result=await publisher.inspectLegacyStorage(pool,{deadlineAt,archiveDirectory,custody:legacyCustody,handoff}) + else if(catalogueInspection)result=await publisher.inspectCurrentCatalogue(pool,{deadlineAt}) + else if(preflight)result=await publisher.preflightCurrentProfile(pool,{deadlineAt}) + else if(args[0]==='reconcile')result=await publisher.reconcilePendingPublication(pool,{journalDirectory,deadlineAt}) + else{ + const publication={privateKeyFile,expectedHeadSha256:pinned.expectedHeadSha256,journalDirectory,validityMs:pinned.validityMs,deadlineAt} + result=upgrade?await publisher.upgradeCurrentProfile(pool,publication):await publisher.publishCurrentProfile(pool,publication) + } + } + catch(error){primary=error;failed=true} + try{await pool.end()}catch(error){primary??=error;failed=true} + if(legacyInspection&&result?.state==='observed'&&result.source.platformProtectionVerified===true&&!failed&&!asyncFailure){ + const {installationId,sourceIdentityId,generation,ordinal,headSha256,bindingSha256}=result.source + if(legacyHandoff){assert(result.resolution);return {event:'database-legacy-handoff-inspection',...result.resolution,sourceActivationMatched:true,conversionAllowed:false}} + return {...(legacyExport?{archive:result.archive}:{}),...(result.lineageSnapshot?{lineageSnapshot:result.lineageSnapshot}:{}),event:'database-legacy-inspection',outcome:'observed',snapshot:result.snapshot, + source:{installationId,sourceIdentityId,generation,ordinal,headSha256,bindingSha256},sourceActivationMatched:true, + platformProtectionVerified:true,generationAuthenticated:false,allWriterDrainAuthenticated:false, + conversionAllowed:false,readyAuthorized:false,automaticReplay:false} + } + if(catalogueInspection&&result?.state==='observed'&&result.platformProtectionVerified===true&&!failed&&!asyncFailure){ + const {state:_state,...catalogue}=result + return {event:'application-writer-source-catalogue-inspection',outcome:'observed',...catalogue,automaticReplay:false,writerAuthorized:false} + } + if(preflight&&result?.state==='matched'&&result.platformProtectionVerified===true&&!failed&&!asyncFailure)return {event:'application-writer-profile-preflight',outcome:'matched',epoch:result.epoch,headSha256:result.headSha256,notAfter:result.notAfter,sourceMatched:true,rolesMatched:true,catalogueMatched:true,platformProtectionVerified:true,writerAuthorized:false,cleanupUnconfirmed:false,automaticReplay:false,generationAuthenticated:false,readyAuthorized:false} + if(result?.outcome==='committed')return {event:'application-writer-profile-publication',outcome:'committed',epoch:result.epoch,headSha256:result.headSha256,notAfter:result.notAfter, + cleanupUnconfirmed:result.cleanupUnconfirmed||asyncFailure||failed,journalUnconfirmed:!!result.journalUnconfirmed,automaticReplay:false,generationAuthenticated:false,readyAuthorized:false} + if(args[0]==='reconcile'&&result&&!failed&&!asyncFailure&&['idle','confirmed','not-committed'].includes(result.state))return {event:'application-writer-profile-reconciliation',outcome:result.state,headSha256:result.headSha256??null,cleanupUnconfirmed:false,journalUnconfirmed:false,automaticReplay:false,generationAuthenticated:false,readyAuthorized:false} + throw primary??Error('Publisher terminal unconfirmed') + } + const deadlineAt=Date.now()+120000 + if(!legacyInspection&&!catalogueInspection&&!activePreflight&&!writes)return connected(verifier,deadlineAt) + const {withActiveSource}=await import('./writer-profile/source-activation.mjs') + const publisherRole=request.roles.publisher.name + const expectedSource={...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:publisherRole,currentUser:publisherRole} + const active=()=>withActiveSource({directory:get('activation-directory'),expectedSha256:get('activation-sha256'),sourcePin,expectedSource,tlsSpkiSha256:request.tls.spkiSha256,deadlineAt},verifier, + guardedVerifier=>connected(guardedVerifier,deadlineAt)) + if(archiveDirectory)return withJournalLease(archiveDirectory,async()=>{ + assert.deepEqual(await readdir(archiveDirectory),['publisher-lease.lock'],'Empty protected archive directory required') + const keyBytes=await readProtected(get('source-signing-key'),8192) + let key + try{key=createPrivateKey(keyBytes)}finally{keyBytes.fill(0)} + assert(key.asymmetricKeyType==='ed25519'&&sha(createPublicKey(key).export({type:'spki',format:'der'}))===sourcePin.publicKeySpkiSha256,'Source signer differs from deployment pin') + const {sourceBindingBytes,sourceBindingCanonical}=await import('../src/db/database-source-binding.js') + const {LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN,LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN}=await import('../src/services/server-probe-legacy-archive-approval.js') + const result=await active() + assert(result.event==='database-legacy-inspection'&&Buffer.isBuffer(result.archive)&&Date.now()createHash('sha256').update(v).digest('hex') +const pinned=()=>({format:'application-writer-publication-approval-v1',manifestSha256:'a'.repeat(64),definitionSha256:'b'.repeat(64),catalogSha256:'c'.repeat(64),expectedHeadSha256:null,validityMs:86400000,expiresAt:Date.now()+60000}) +test('approval is explicit, exact, future-bounded and does not learn missing expected fields',()=>{ + const value=pinned();assert.deepEqual(approval(value),value) + for(const change of [(v:any)=>delete v.definitionSha256,(v:any)=>delete v.catalogSha256,(v:any)=>v.expiresAt=Date.now(),(v:any)=>v.validityMs=86400001,(v:any)=>v.expectedHeadSha256='observed',(v:any)=>v.provider=true]){const v=pinned();change(v);assert.throws(()=>approval(v))} +}) +test('isolated Pool1 options require pinned CA/host/role/database and native TLS verification',()=>{ + const ca=Buffer.from(rootCertificates[0]),request={roles:{publisher:{name:'installation_policy_signer'}},source:{databaseName:'owned'},tls:{serverName:'db.example.invalid',caSha256:hash(ca),spkiSha256:hash(new X509Certificate(ca).publicKey.export({format:'der',type:'spki'}))}} + const transport={host:'db.example.invalid',port:5432,user:'installation_policy_signer',password:'test-only',database:'owned'},config=poolOptions(transport,request,ca) + assert.equal(config.max,1);assert.equal(config.connectionTimeoutMillis,5000);assert.equal(config.ssl.rejectUnauthorized,true) + assert(config.ssl.checkServerIdentity('attacker.invalid',new X509Certificate(ca).toLegacyObject()) instanceof Error) + for(const changed of [{...transport,user:'admin'},{...transport,host:'attacker.invalid'},{...transport,database:'other'},{...transport,connectionString:'postgres://env-substitution'},{...transport,port:0}])assert.throws(()=>poolOptions(changed,request,ca)) + assert.throws(()=>poolOptions(transport,request,Buffer.from('not approved CA'))) +}) +test('real production envelope parser accepts operator-key signature and rejects tamper/foreign key',()=>{ + const now=Date.now(),{publicKey,privateKey}=generateKeyPairSync('ed25519'),installation={installationId:'788629ed-e00d-4af8-bc33-c705e5a7b62d',publicKeyPem:publicKey.export({type:'spki',format:'pem'}).toString(),publicKeySpkiSha256:hash(publicKey.export({type:'spki',format:'der'}))} + const body={format:'application-writer-policy-v1',installationId:installation.installationId,epoch:'1',issuedAt:now,notBefore:now,notAfter:now+86400000,source:{systemIdentifier:'7348518042453055651',databaseOid:'16384',databaseName:'owned',serverVersionNum:180004,inRecovery:false},sourceObservationSha256:'a'.repeat(64),roles:{runtime:'20001',operator:'20002',capture:'20003',publisher:'20004',control:'20005'},manifestSha256:'b'.repeat(64),definitionSha256:'c'.repeat(64),catalogSha256:'d'.repeat(64),futureLeaves:'fixed12-native-policy-v1'} + const signature=sign(null,Buffer.from(POLICY_DOMAIN+canonical(body)),privateKey).toString('base64url') + assert.equal(signedEnvelope(bytes({body,signature}),'policy',installation).body.epoch,'1') + assert.throws(()=>signedEnvelope(bytes({body:{...body,catalogSha256:'e'.repeat(64)},signature}),'policy',installation)) + const other=generateKeyPairSync('ed25519').publicKey;assert.throws(()=>signedEnvelope(bytes({body,signature}),'policy',{...installation,publicKeyPem:other.export({type:'spki',format:'pem'}).toString(),publicKeySpkiSha256:hash(other.export({type:'spki',format:'der'}))})) +}) +test('actual pg ConnectionParameters cannot inherit malicious PGOPTIONS and native selection refuses',()=>{ + const require=createRequire(new URL('../package.json',import.meta.url)),ConnectionParameters=require('pg/lib/connection-parameters') + const original=process.env.PGOPTIONS;process.env.PGOPTIONS='-c role=attacker -c search_path=attacker' + try{ + const ca=Buffer.from(rootCertificates[0]),request={roles:{publisher:{name:'installation_policy_signer'}},source:{databaseName:'owned'},tls:{serverName:'db.example.invalid',caSha256:hash(ca),spkiSha256:'a'.repeat(64)}} + const config=poolOptions({host:'db.example.invalid',port:5432,user:'installation_policy_signer',password:'test-only',database:'owned'},request,ca) + const actual=new ConnectionParameters(config);assert.equal(actual.options,STARTUP_OPTIONS);assert(!actual.options.includes('attacker'));assert.equal(actual.user,'installation_policy_signer');assert.equal(actual.ssl.rejectUnauthorized,true) + }finally{if(original===undefined)delete process.env.PGOPTIONS;else process.env.PGOPTIONS=original} + assert.doesNotThrow(()=>assertJsDriverEnvironment({}));for(const value of ['1','0','',undefined])assert.throws(()=>assertJsDriverEnvironment({NODE_PG_FORCE_NATIVE:value})) +}) + +test('reconciliation accepts expired original approval as copied inspection data; publication still refuses',()=>{ + const value={...pinned(),expiresAt:1},copy=reconciliationApproval(value) + assert.deepEqual(copy,value);assert.notEqual(copy,value);assert.throws(()=>approval(value)) + copy.catalogSha256='d'.repeat(64);assert.equal(value.catalogSha256,'c'.repeat(64)) + const future=pinned();assert.deepEqual(approval(future),reconciliationApproval(future)) +}) +test('reconciliation keeps exact digest/head/schema and original lifetime bounds',()=>{ + for(const mutate of [(v:any)=>delete v.definitionSha256,(v:any)=>v.catalogSha256='observed',(v:any)=>v.expectedHeadSha256='foreign',(v:any)=>v.extra=true,(v:any)=>v.format='other',(v:any)=>v.validityMs=0,(v:any)=>v.validityMs=86400001,(v:any)=>v.expiresAt=0,(v:any)=>v.expiresAt=-1,(v:any)=>v.expiresAt=Number.MAX_SAFE_INTEGER+1]){ + const value={...pinned(),expiresAt:1};mutate(value);assert.throws(()=>reconciliationApproval(value));assert.throws(()=>approval(value)) + } +}) +test('publication expiry guard remains mandatory at the real clock boundary',()=>{ + const value=pinned();value.expiresAt=Date.now();assert.throws(()=>approval(value));assert.deepEqual(reconciliationApproval(value),value) +}) +test('restore rebind requires a protected exact old head and changed approved catalogue',()=>{ + const value={...pinned(),format:REBIND58_APPROVAL_FORMAT,previousCatalogSha256:'d'.repeat(64),expectedHeadSha256:'e'.repeat(64)} + assert.deepEqual(upgradeApproval(value),value) + assert.throws(()=>approval(value)) + assert.throws(()=>upgradeApproval({...value,previousCatalogSha256:value.catalogSha256})) + assert.throws(()=>upgradeApproval({...value,expectedHeadSha256:null})) + assert.throws(()=>upgradeApproval({...value,expiresAt:1})) +}) diff --git a/backend/scripts/database-writer-profile-preflight.test.mts b/backend/scripts/database-writer-profile-preflight.test.mts new file mode 100644 index 00000000..0d7bc996 --- /dev/null +++ b/backend/scripts/database-writer-profile-preflight.test.mts @@ -0,0 +1,355 @@ +// Candidate component tests. Actual signature/current reader, modeled PG/source/catalogue. +// No genuine SQL, TLS session, filesystem ACL or generation acceptance is claimed. +import test from 'node:test' +import assert from 'node:assert/strict' +import fs from 'node:fs/promises' +import {createRequire} from 'node:module' +import {runInNewContext} from 'node:vm' +import {EventEmitter} from 'node:events' +import path from 'node:path' +import * as format from '../src/db/application-writer-profile-format.ts' +import {MIGRATION_MANIFEST,migrationManifestDigest} from '../src/db/migration-manifest.ts' +import {readCurrentApplicationWriterProfile} from '../src/db/application-writer-profile-registry-read.ts' +import {syntheticFixture,stagedModelClient} from '../src/db/application-writer-profile-registry-read-model.test.ts' +import {approvedDefinition} from './writer-profile/catalogue.mts' +import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './writer-profile/fixed44-build-definition.ts' +import {BUILD56_PROVENANCE} from './writer-profile/fixed56-build-definition.ts' +import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './writer-profile/fixed57-build-definition.ts' +import {approval as parseApproval,upgradeApproval,reconciliationApproval,isUpgradeApprovalFormat,UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT} from './writer-profile/operator-contract.mjs' +const require=createRequire(new URL('../package.json',import.meta.url)),ts=require('typescript'),pg=require('pg') +const candidate=await fs.readFile(new URL('./writer-profile/publisher.mjs',import.meta.url),'utf8') +interface PreflightDiagnostics {state:string;headSha256:string;sourceMatched:boolean;rolesMatched:boolean;catalogueMatched:boolean;platformProtectionVerified:boolean;writerAuthorized:boolean;generationAuthenticated:boolean;readyAuthorized:boolean} +type PublishOptions={privateKeyFile:string;expectedHeadSha256:string|null;journalDirectory:string;deadlineAt:number;validityMs?:number} +interface PublisherModule { + requireCurrentCatalogueDefinition(approval:unknown,definition:unknown):string + createConnectedPublisher(installation:unknown,collect:()=>Promise,sourceVerifier:{verify:()=>Promise<{platformProtectionVerified:boolean}>},approval:unknown,definition:unknown):{ + inspectLegacyStorage(pool:unknown,options:{deadlineAt:number}):Promise<{state:string;snapshot:{rowCount:number}}> + preflightCurrentProfile(pool:unknown,options:{deadlineAt:number}):Promise + publishCurrentProfile(pool:unknown,options:PublishOptions):Promise<{outcome:string}> + upgradeCurrentProfile(pool:unknown,options:PublishOptions):Promise<{outcome:string;epoch:string;headSha256:string}> + } +} +interface OperatorModule {main(args:string[]):Promise<{event:string;outcome:string;platformProtectionVerified:boolean;writerAuthorized:boolean;readyAuthorized:boolean;sourceActivationMatched?:boolean}>} +function compile(source:string,modules:Record):T{ + const exports:Record={};const compiled=ts.transpileModule(source.split('\nif(process.argv[1]')[0].replaceAll('import.meta.url',JSON.stringify('file:///candidate-not-cli.mts')),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText + runInNewContext(compiled,{exports,require:(name:string)=>{assert(name in modules,'Unapproved import '+name);return modules[name]},Buffer,URL,TextDecoder,Date,setTimeout,clearTimeout,Object,Error,AggregateError,Promise,Map,Set,process:{env:{},argv:[]}}, {filename:'candidate.mjs'}) + // The isolated VM loads the exact source above; tests exercise this narrow export contract. + return exports as T +} +const frozenDefinition=approvedDefinition({format:'application-writer-source44-definition-approval-v1',provenance:BUILD44_PROVENANCE,template:FIXED44_ROLE_TEMPLATE}) +const frozenMigrations=MIGRATION_MANIFEST.slice(0,44),markerIds=frozenMigrations.map(pin=>pin.file.slice(0,-4)) +const markerSql='SELECT id FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1' +function fixture(fault='',forPublication=false,upgradeMode?:'upgrade'|'upgrade57'|'ordinary-renewal'){ + const f=syntheticFixture('1'),security={sha256:'e'.repeat(64),roles:{...f.policy.roles}} + const predecessor=upgradeMode==='upgrade57'?BUILD56_PROVENANCE:BUILD44_PROVENANCE + const successor=upgradeMode==='upgrade57'?BUILD57_PROVENANCE:BUILD56_PROVENANCE + const target=upgradeMode?{definitionSha256:successor.templateSha256,manifestSha256:successor.manifestSha256}:frozenDefinition + const targetMigrations=MIGRATION_MANIFEST.slice(0,upgradeMode==='upgrade57'?57:56) + const catalogue={...target,catalogSha256:'f'.repeat(64),leafCount:117} + const currentBuild=fault==='current-build'||fault==='current-approved-frozen-definition' + f.policy.manifestSha256=predecessor.manifestSha256;f.policy.definitionSha256=predecessor.templateSha256 + f.policy.catalogSha256=upgradeMode?'d'.repeat(64):format.sha(format.canonical({base:catalogue.catalogSha256,security:security.sha256})) + if(fault==='signed-source')f.policy.source.databaseOid='999' + if(fault==='signed-role')f.policy.roles.operator='99' + if(fault==='signed-catalogue')f.policy.catalogSha256='1'.repeat(64) + if(fault==='signed-definition')f.policy.definitionSha256='1'.repeat(64) + if(fault==='signed-manifest')f.policy.manifestSha256='1'.repeat(64) + if(fault==='expired'){f.policy.issuedAt=Date.now()-60000;f.policy.notBefore=f.policy.issuedAt;f.policy.notAfter=Date.now()-1000} + const raw=f.signBody(f.policy,format.POLICY_DOMAIN),hash=format.sha(raw),top=f.state.latest[0] + top.policy_sha256=hash;top.head_bytes=f.signBody({format:'application-writer-head-v1',installationId:f.subject.installationId,epoch:'1',policySha256:hash,previousHeadSha256:null},format.HEAD_DOMAIN);top.head_sha256=format.sha(top.head_bytes) + f.state.current[0].head_sha256=top.head_sha256;f.state.policy=[{policy_sha256:hash,policy_bytes:raw}] + const priorHead=top.head_sha256 + if(fault==='tamper')f.state.policy[0].policy_bytes=Buffer.from(raw.toString().replace('synthetic','tampered')) + if(fault==='empty'){f.state.current=[];f.state.latest=[];f.state.policy=[]} + const model=stagedModelClient(f.state),queries:string[]=[],events:string[]=[],markerLimits:unknown[][]=[] + const writes:{sql:string;values?:unknown[]}[]=[] + let clocks=0,checkouts=0 + const client=Object.assign(new EventEmitter(),{query:async(sql:string,values?:unknown[])=>{ + queries.push(sql) + if(/^(INSERT|UPDATE) INTO |^UPDATE mmapp_control\./.test(sql))writes.push({sql,values}) + if(sql.includes('clock_timestamp()')&&++clocks===2&&fault==='late-clock')return {rows:[{now:'1'}]} + if(sql===markerSql){ + markerLimits.push(values??[]) + const expected=upgradeMode?targetMigrations.map(pin=>pin.file.slice(0,-4)):markerIds + const ids=fault==='marker-missing'?expected.slice(0,-1):fault==='marker-extra'||fault==='marker-drift'&&markerLimits.length===2?[...expected,'9999_foreign']:expected + return {rows:ids.map(id=>({id}))} + } + if(sql.startsWith('WITH')||sql.includes('clock_timestamp()'))return model.client.query(sql,values) + if(sql.startsWith('SELECT pg_catalog.pg_advisory_unlock'))return {rows:[{released:true}]} + if(sql==='ROLLBACK')return {rows:[],command:fault==='rollback-ack'?'':'ROLLBACK'} + if(sql.startsWith('INSERT INTO mmapp_control.current')||sql.startsWith('UPDATE mmapp_control.current'))return {rows:[{id:1}]} + return {rows:[],command:sql.split(' ')[0]} + },release:(bad:boolean)=>{events.push('release:'+bad)}}) + const pool=new pg.Pool();pool.connect=async()=>{checkouts++;return client} + const source={systemIdentifier:'123',databaseOid:'456',databaseName:'synthetic',serverVersionNum:180004,inRecovery:false} + let contexts=0,verifications=0 + const installation={...f.subject,expectedSource:source,roleBindings:{publisher:{name:'publisher',signedOid:'14'}}} + const pinned={expiresAt:Date.now()+60000,manifestSha256:['approval-manifest','current-approved-frozen-definition'].includes(fault)?migrationManifestDigest():target.manifestSha256,definitionSha256:catalogue.definitionSha256,catalogSha256:format.sha(format.canonical({base:catalogue.catalogSha256,security:security.sha256})),...(upgradeMode==='upgrade'||upgradeMode==='upgrade57'?{ + format:upgradeMode==='upgrade57'&&fault!=='wrong-transition'?UPGRADE57_APPROVAL_FORMAT:UPGRADE_APPROVAL_FORMAT,expectedHeadSha256:fault==='wrong-head'?'0'.repeat(64):priorHead,previousCatalogSha256:fault==='wrong-previous-catalogue'?'0'.repeat(64):'d'.repeat(64),validityMs:60000, + }:{})} + const definition=fault==='static-definition'?{...target,definitionSha256:'0'.repeat(64)}:target + const privateKeyFile=path.resolve('modeled-fixture-key.pem'),journalDirectory=path.resolve('modeled-journal'),files=new Map() + if(forPublication)files.set(privateKeyFile,Buffer.from(f.privateKey.export({format:'pem',type:'pkcs8'}))) + function file(filename:string){const value=files.get(filename);if(!value)throw Object.assign(Error('Fixture file absent'),{code:'ENOENT'});return value} + function stat(filename:string){return {size:file(filename).length,dev:1,ino:1,isFile:()=>true,isSymbolicLink:()=>false}} + const modules={ + pg,'node:crypto':require('node:crypto'),'node:fs/promises':{realpath:async(file:string)=>file,lstat:async(filename:string)=>stat(filename),unlink:async(filename:string)=>files.delete(filename),open:async(filename:string)=>({stat:async()=>stat(filename),readFile:async()=>Buffer.from(file(filename)),close:async()=>{}})},'node:path':path, + './fixed44-build-definition.js':{BUILD44_PROVENANCE},'./fixed56-build-definition.js':{BUILD56_PROVENANCE},'./fixed57-build-definition.js':{BUILD57_PROVENANCE}, + './operator-contract.mjs':{UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,isUpgradeApprovalFormat,upgradeApproval,retainPublisherTlsPeer:(actual:unknown)=>{ + assert.equal(actual,client) + if(fault==='initial-tls')throw Error('TLS peer refused') + return ()=>{if(fault==='lost-tls'&&verifications===2)throw Error('Retained TLS lost')} + }}, + './operator-files.mjs':{withJournalLease:async(_directory:string,work:()=>Promise)=>{assert(forPublication);events.push('journal-lease');return work()},writeFile:async(filename:string,value:Buffer)=>{assert(forPublication);files.set(filename,value)}}, + '../../src/db/database-source-observation.js':{captureDatabaseSourceObservation:async()=>{events.push('capture-source');return {}},assertDatabaseSourceObservationCurrent:async()=>{events.push('fresh-source');if(fault==='late-source')throw Error('Source changed')}}, + '../../src/services/server-probe-legacy-source-inventory.js':{captureLegacyProbeStorage:async()=>{events.push('capture-legacy');return {rowCount:1}}}, + '../../src/db/migration-manifest.js':{MIGRATION_MANIFEST:currentBuild?MIGRATION_MANIFEST:upgradeMode?targetMigrations:frozenMigrations,migrationManifestDigest:()=>currentBuild?migrationManifestDigest():target.manifestSha256}, + '../../src/db/application-writer-profile-format.js':format, + './context.mjs':{ownedContext:async()=>{contexts++;if(fault==='late-actor'&&contexts===2)throw Error('Actor changed');return {pid:1,source}},sameOwned:(a:unknown,b:unknown)=>assert.deepEqual(a,b),sourceCoordinates:(v:{source:typeof source})=>v.source,protectedStructure:async()=>{events.push('roles');if(fault==='role-structure')throw Error('Unsafe role');return security},readCurrent:forPublication&&!upgradeMode?async()=>null:readCurrentApplicationWriterProfile,HISTORY:'history',CURRENT:'current'}, + } + const api=compile(candidate,modules).createConnectedPublisher(installation,async()=>{events.push('catalogue');if(fault==='catalogue-eof')throw Error('Incomplete EOF');return fault==='catalogue-manifest'?{...catalogue,manifestSha256:migrationManifestDigest()}:fault==='catalogue-definition'?{...catalogue,definitionSha256:'0'.repeat(64)}:catalogue},{verify:async()=>{verifications++;events.push('verify-source');if(fault==='source-verifier')throw Error('Unsigned source');if(fault==='late-expiry'&&verifications===2)pinned.expiresAt=1;return {platformProtectionVerified:!(fault==='initial-unprotected'&&verifications===1||fault==='terminal-unprotected'&&verifications===2)}}},pinned,definition) + const publication=()=>({privateKeyFile,journalDirectory,expectedHeadSha256:upgradeMode?pinned.expectedHeadSha256??priorHead:null,deadlineAt:Date.now()+30000,validityMs:60000}) + return {run:()=>api.preflightCurrentProfile(pool,{deadlineAt:Date.now()+30000}),inspectLegacy:()=>api.inspectLegacyStorage(pool,{deadlineAt:Date.now()+30000}),publish:()=>api.publishCurrentProfile(pool,publication()),upgrade:()=>api.upgradeCurrentProfile(pool,publication()),queries,events,markerLimits,files,writes,priorHead,pinned,get checkouts(){return checkouts},f} +} +test('legacy inspection requires retained TLS, terminal source proof and acknowledged rollback',async()=>{ + const valid=fixture(),result=await valid.inspectLegacy() + assert.equal(result.state,'observed');assert.equal(result.snapshot.rowCount,1);assert.equal(valid.checkouts,1) + assert.deepEqual(valid.events,['capture-source','verify-source','capture-legacy','verify-source','fresh-source','release:false']) + assert.equal(valid.queries.filter(q=>q==='ROLLBACK').length,1);assert(!valid.queries.includes('COMMIT')) + for(const fault of ['initial-tls','lost-tls','late-source','rollback-ack'])await assert.rejects(fixture(fault).inspectLegacy()) +}) +test('current44 preflight authenticates signed current and actual component inputs on one read-only lease',async()=>{ + const f=fixture(),result=await f.run();assert.equal(result.state,'matched');assert.equal(result.headSha256,f.f.state.current[0].head_sha256) + assert.equal(result.sourceMatched,true);assert.equal(result.rolesMatched,true);assert.equal(result.catalogueMatched,true) + assert.equal(result.platformProtectionVerified,true) + assert.equal(result.writerAuthorized,false);assert.equal(result.generationAuthenticated,false);assert.equal(result.readyAuthorized,false) + assert.equal(f.queries.filter(q=>q.startsWith('BEGIN')).length,1);assert(f.queries.includes('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY'));assert.equal(f.queries.filter(q=>q==='ROLLBACK').length,1) + assert.equal(format.canonical(f.markerLimits),'[[45]]') + assert(!f.queries.some(q=>/^(?:INSERT|UPDATE|DELETE|COMMIT|CREATE|ALTER|DROP)\b/.test(q)));assert.equal(f.events.at(-1),'release:false') + assert.deepEqual(f.events.slice(0,6),['capture-source','verify-source','roles','catalogue','verify-source','fresh-source']) +}) +test('publisher refuses before SQL on a wrong peer and stops SQL when the retained TLS socket is lost',async()=>{ + const initial=fixture('initial-tls') + await assert.rejects(initial.run());assert.deepEqual(initial.queries,[]);assert.deepEqual(initial.events,['release:true']) + const lost=fixture('lost-tls') + await assert.rejects(lost.run());assert.equal(lost.events.at(-1),'release:true') + assert(!lost.queries.some(q=>q==='ROLLBACK'||q.includes('pg_advisory_unlock'))) +}) +for(const fault of ['initial-unprotected','terminal-unprotected'])test('preflight cannot promote '+fault+' into protected evidence',async()=>{ + const f=fixture(fault),result=await f.run();assert.equal(result.platformProtectionVerified,false) + assert.equal(result.writerAuthorized,false);assert.equal(result.readyAuthorized,false) + assert(!f.queries.some(q=>/^(?:INSERT|UPDATE|DELETE|COMMIT|CREATE|ALTER|DROP)\b/.test(q))) +}) +for(const fault of ['tamper','empty','expired','signed-source','signed-role','signed-catalogue','signed-definition','signed-manifest','role-structure','catalogue-eof','source-verifier','late-source','late-actor','late-expiry','late-clock','rollback-ack'])test('preflight refuses '+fault+' without any write or replay',async()=>{ + const f=fixture(fault);await assert.rejects(f.run());assert(!f.queries.some(q=>/^(?:INSERT|UPDATE|DELETE|COMMIT|CREATE|ALTER|DROP)\b/.test(q)));assert.equal(f.queries.filter(q=>q.startsWith('BEGIN')).length,1) +}) +for(const fault of ['current-build','current-approved-frozen-definition','approval-manifest','static-definition'])test('incompatible static manifest/definition refuses '+fault+' before either pool checkout or publication lease',async()=>{ + const f=fixture(fault,true) + await assert.rejects(f.run(),/Approved catalogue definition is not bound/) + await assert.rejects(f.publish(),/Approved catalogue definition is not bound/) + assert.equal(f.checkouts,0);assert.deepEqual(f.events,[]);assert.deepEqual(f.queries,[]) +}) +for(const fault of ['catalogue-manifest','catalogue-definition','marker-missing','marker-extra'])test('retained preflight refuses '+fault+' and rolls back',async()=>{ + const f=fixture(fault) + await assert.rejects(f.run(),error=>error instanceof Error&&/Catalogue evidence differs|Exact current migration marker/.test((error.cause as Error).message)) + assert.equal(f.checkouts,1);assert.equal(f.queries.filter(q=>q==='ROLLBACK').length,1) + assert(!f.queries.some(q=>/^(?:INSERT|UPDATE|DELETE|COMMIT)\b/.test(q))) + if(fault.startsWith('marker-'))assert.equal(format.canonical(f.markerLimits),'[[45]]') + else assert.deepEqual(f.markerLimits,[]) +}) +test('publication checks exact marker set on both retained transactions and refuses late marker drift before registry writes',async()=>{ + const positive=fixture('',true) + assert.equal((await positive.publish()).outcome,'committed') + assert.equal(positive.checkouts,1);assert.equal(format.canonical(positive.markerLimits),'[[45],[45]]') + assert.equal(positive.queries.filter(q=>q==='COMMIT').length,2) + const drift=fixture('marker-drift',true) + await assert.rejects(drift.publish(),error=>error instanceof Error&&/Exact current migration marker/.test((error.cause as Error).message)) + assert.equal(drift.checkouts,1);assert.equal(format.canonical(drift.markerLimits),'[[45],[45]]') + assert.equal(drift.queries.filter(q=>q==='COMMIT').length,1);assert.equal(drift.queries.filter(q=>q==='ROLLBACK').length,1) + assert(!drift.queries.some(q=>/^(?:INSERT|UPDATE|DELETE)\b/.test(q))) + assert([...drift.files.keys()].some(filename=>filename.endsWith('pending-publication.json'))) +}) +test('explicit 44 to 56 upgrade pins the signed predecessor, exact catalogue and CAS',async()=>{ + const f=fixture('',true,'upgrade') + const approved=upgradeApproval(f.pinned) + assert.equal(approved.expectedHeadSha256,f.priorHead) + assert.throws(()=>parseApproval(approved)) + for(const changed of [{expectedHeadSha256:null},{previousCatalogSha256:undefined},{expiresAt:1}]) { + assert.throws(()=>upgradeApproval({...approved,...changed})) + } + assert.equal(reconciliationApproval({...approved,expiresAt:1}).expectedHeadSha256,f.priorHead) + const result=await f.upgrade() + assert.equal(result.outcome,'committed');assert.equal(result.epoch,'2') + assert.equal(format.canonical(f.markerLimits),'[[57],[57]]') + assert.equal(f.writes.length,2) + const [history,current]=f.writes + assert.match(history.sql,/INSERT INTO mmapp_control.history/) + assert.equal(format.canonical(history.values?.slice(0,6)),format.canonical([f.f.subject.installationId,'2',result.headSha256,f.f.subject.installationId,'1',f.priorHead])) + const policy=format.signedEnvelope(history.values![7] as Buffer,'policy',f.f.subject).body + const head=format.signedEnvelope(history.values![8] as Buffer,'head',f.f.subject).body + assert.deepEqual(policy.source,f.f.policy.source);assert.deepEqual(policy.roles,f.f.policy.roles) + assert.equal(policy.manifestSha256,BUILD56_PROVENANCE.manifestSha256);assert.equal(policy.definitionSha256,BUILD56_PROVENANCE.templateSha256) + assert.equal(head.previousHeadSha256,f.priorHead) + assert.equal(format.canonical(current.values),format.canonical(['2',result.headSha256,f.f.subject.installationId,'1',f.priorHead])) +}) + +test('upgrade refuses drift and ordinary renewal cannot perform the 44 to 56 transition',async()=>{ + for(const fault of ['wrong-head','wrong-previous-catalogue','signed-manifest','signed-definition','signed-role','signed-source','marker-drift','initial-unprotected','terminal-unprotected']){ + const f=fixture(fault,true,'upgrade') + await assert.rejects(f.upgrade()) + assert.deepEqual(f.writes,[],fault) + } + const explicit=fixture('',true,'upgrade') + await assert.rejects(explicit.publish(),/explicit upgrade command/) + assert.equal(explicit.checkouts,0) + const renewal=fixture('',true,'ordinary-renewal') + await assert.rejects(renewal.publish(),error=>error instanceof Error&&/Renewal cannot adopt/.test((error.cause as Error).message)) + assert.deepEqual(renewal.writes,[]) +}) + +test('explicit 56 to 57 upgrade signs only the exact reviewed successor',async()=>{ + const definition=approvedDefinition({format:'application-writer-source57-definition-approval-v1',provenance:BUILD57_PROVENANCE,template:FIXED57_ROLE_TEMPLATE}) + assert.equal(definition.manifestSha256,migrationManifestDigest()) + assert.throws(()=>approvedDefinition({format:'application-writer-source57-definition-approval-v1',provenance:BUILD57_PROVENANCE,template:FIXED44_ROLE_TEMPLATE})) + const f=fixture('',true,'upgrade57') + await assert.rejects(f.publish(),/explicit upgrade command/) + const result=await f.upgrade() + assert.equal(result.outcome,'committed') + assert.equal(format.canonical(f.markerLimits),'[[58],[58]]') + const history=f.writes[0] + const policy=format.signedEnvelope(history.values![7] as Buffer,'policy',f.f.subject).body + const head=format.signedEnvelope(history.values![8] as Buffer,'head',f.f.subject).body + assert.equal(policy.manifestSha256,definition.manifestSha256) + assert.equal(policy.definitionSha256,definition.definitionSha256) + assert.equal(head.previousHeadSha256,f.priorHead) + for(const fault of ['wrong-transition','wrong-previous-catalogue','signed-manifest','signed-definition']){ + const refused=fixture(fault,true,'upgrade57') + await assert.rejects(refused.upgrade()) + assert.deepEqual(refused.writes,[],fault) + } +}) + +test('operator requires the protected source verifier for preflight, publication and reconciliation',async()=>{ + const cli=await fs.readFile(new URL('./database-writer-profile-operator.mts',import.meta.url),'utf8'),f=syntheticFixture('1'),reads:string[]=[],events:string[]=[] + const request={installationId:f.subject.installationId,roles:Object.fromEntries(['runtime','operator','capture','publisher','control'].map((k,i)=>[k,{name:k,signedOid:String(i+11)}])),source:f.policy.source,sourceHeadSha256:'a'.repeat(64),tls:{caSha256:'a'.repeat(64),spkiSha256:'d'.repeat(64)}} + const files:Record={bootstrap:{format:'application-writer-bootstrap-v1',publicationAuthorized:false,readyAuthorized:false,deploymentRequest:request,verificationSubject:f.subject},approval:{expiresAt:Date.now()+60000,definitionSha256:'b'.repeat(64)},definition:{},transport:{},ca:'fixture','signing-private.pem':'fixture'} + let failEnd=false,asyncError=false,protectedRefused=false,activationRefused=false,incompatibleDefinition=false + let catalogueEdition:44|56|57=44 + const catalogueCalls:number[]=[] + const retainedPools:Pool[]=[] + class Pool extends EventEmitter{constructor(){super();retainedPools.push(this)}async end(){events.push('pool-end');if(failEnd)throw Error('Pool cleanup unconfirmed')}} + const result={state:'matched',epoch:'1',headSha256:'c'.repeat(64),notAfter:Date.now()+60000,platformProtectionVerified:true} + const protectedVerifier={verify:async()=>({platformProtectionVerified:true})} + const guardedVerifier={...protectedVerifier} + const modules={ + 'node:assert/strict':assert,'node:path':require('node:path'),'node:url':require('node:url'),'node:crypto':require('node:crypto'), + './writer-profile/operator-files.mjs':{readProtected:async(file:string)=>{reads.push(file);assert(file in files);return Buffer.from(JSON.stringify(files[file]))},protectedDirectory:async()=>{},sha:format.sha}, + './writer-profile/operator-contract.mjs':{approval:(value:unknown)=>{events.push('approval');return value},upgradeApproval:(value:unknown)=>{events.push('upgrade-approval');return upgradeApproval(value)},reconciliationApproval:(value:unknown)=>{events.push('reconciliation-approval');return value},poolOptions:()=>{events.push('transport');return {}},roleKeys:['runtime','operator','capture','publisher','control'],assertJsDriverEnvironment:()=>{}}, + './database-writer-bootstrap-operator.mjs':{deploymentRequest:(value:unknown)=>value}, + './writer-profile/protected-source-registry.mjs':{createProtectedDatabaseSourceBindingVerifier:(pin:{registryDirectory:string;currentHeadSha256:string})=>{events.push('protected-source');assert.equal(pin.registryDirectory,'source-registry');assert.equal(pin.currentHeadSha256,request.sourceHeadSha256);if(protectedRefused)throw Error('Protected source refused');return protectedVerifier}}, + './writer-profile/source-activation.mjs':{withActiveSource:async(input:{directory:string;expectedSha256:string;sourcePin:{currentHeadSha256:string};expectedSource:unknown;tlsSpkiSha256:string;deadlineAt:number},verifier:unknown,work:(verifier:unknown)=>Promise)=>{ + assert.equal(verifier,protectedVerifier);assert.equal(input.directory,'active-registry');assert.equal(input.expectedSha256,'e'.repeat(64)) + assert.equal(input.sourcePin.currentHeadSha256,request.sourceHeadSha256);assert.equal(input.tlsSpkiSha256,request.tls.spkiSha256);assert(input.deadlineAt>Date.now()) + assert.equal(format.canonical(input.expectedSource),format.canonical({...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:'publisher',currentUser:'publisher'})) + events.push('activation-admission');if(activationRefused)throw Error('Activation refused') + const value=await work(guardedVerifier);events.push('activation-terminal');return value + }}, + pg:{Pool},'./writer-profile/publisher.mjs':{requireCurrentCatalogueDefinition:()=>{if(incompatibleDefinition)throw Error('Build manifest refused')},createConnectedPublisher:(_installation:unknown,collect:(session:unknown)=>Promise,verifier:unknown)=>{ + assert.equal(verifier,events.includes('activation-admission')?guardedVerifier:protectedVerifier) + return { + inspectLegacyStorage:async()=>{events.push('legacy-inspect');return {state:'observed',snapshot:{rowCount:1,sha256:'9'.repeat(64)},source:{platformProtectionVerified:true,generation:'source-generation'}}}, + preflightCurrentProfile:async()=>{await collect({});events.push('preflight');if(asyncError){const activePool=retainedPools.at(-1);assert(activePool);activePool.emit('error',Error('Async pool fault'))}return result}, + publishCurrentProfile:async()=>{await collect({});events.push('publish');return {...result,outcome:'committed'}}, + upgradeCurrentProfile:async()=>{await collect({});events.push('upgrade');return {...result,outcome:'committed'}}, + reconcilePendingPublication:async()=>{events.push('reconcile');return {state:'confirmed',headSha256:result.headSha256}}, + } + }}, + './writer-profile/catalogue.mjs':{ + collectSource44Catalogue:async()=>{catalogueCalls.push(44)}, + collectSource56Catalogue:async()=>{catalogueCalls.push(56)}, + collectSource57Catalogue:async()=>{catalogueCalls.push(57)}, + approvedDefinition:()=>({...(catalogueEdition===44?{}:{edition:catalogueEdition}),definitionSha256:'b'.repeat(64),manifestSha256:frozenDefinition.manifestSha256}), + }, + } + const flags=['bootstrap','bootstrap-sha256','approval','approval-sha256','definition','definition-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256','journal'],args=['preflight',...flags.flatMap(flag=>['--'+flag,flag.endsWith('sha256')?'a'.repeat(64):flag])] + const output=await compile(cli,modules).main(args) + assert.equal(output.event,'application-writer-profile-preflight');assert.equal(output.outcome,'matched');assert.equal(output.platformProtectionVerified,true);assert.equal(output.writerAuthorized,false);assert.equal(output.readyAuthorized,false) + assert.deepEqual(reads,['bootstrap','approval','definition','transport','ca']);assert.deepEqual(events,['approval','transport','protected-source','preflight','pool-end']) + failEnd=true;await assert.rejects(compile(cli,modules).main(args),/Pool cleanup unconfirmed/) + failEnd=false;asyncError=true;await assert.rejects(compile(cli,modules).main(args),/Publisher terminal unconfirmed/) + asyncError=false;result.platformProtectionVerified=false;await assert.rejects(compile(cli,modules).main(args),/Publisher terminal unconfirmed/) + result.platformProtectionVerified=true;protectedRefused=true;const before=retainedPools.length + await assert.rejects(compile(cli,modules).main(args),/Protected source refused/);assert.equal(retainedPools.length,before,'Protection failure must not open a Pool or fall back') + const activationFlags=['--activation-directory','active-registry','--activation-sha256','e'.repeat(64)] + const activated=(command:string)=>[command,...args.slice(1),...activationFlags] + for(const [command,approvalEvent,outcome] of [['publish','approval','committed'],['reconcile','reconciliation-approval','confirmed']]){ + const commandArgs=command==='publish'?activated(command):[command,...args.slice(1)] + const poolCount=retainedPools.length + await assert.rejects(compile(cli,modules).main(commandArgs),/Protected source refused/) + assert.equal(retainedPools.length,poolCount) + protectedRefused=false + events.length=0;reads.length=0 + const terminal=await compile(cli,modules).main(commandArgs) + assert.equal(terminal.outcome,outcome);assert.equal(terminal.readyAuthorized,false) + assert.deepEqual(events,command==='publish' + ?[approvalEvent,'transport','protected-source','activation-admission',command,'pool-end','activation-terminal'] + :[approvalEvent,'transport','protected-source',command,'pool-end']) + assert.equal(reads.includes('signing-private.pem'),command==='publish') + protectedRefused=true + } + const activeArgs=activated('source-preflight') + protectedRefused=false;activationRefused=true;events.length=0;reads.length=0 + const beforeActivation=retainedPools.length + await assert.rejects(compile(cli,modules).main(activeArgs),/Activation refused/) + assert.equal(retainedPools.length,beforeActivation) + assert.deepEqual(events,['approval','transport','protected-source','activation-admission']) + await assert.rejects(compile(cli,modules).main(['publish',...args.slice(1)])) + await assert.rejects(compile(cli,modules).main(activated('publish')), /Activation refused/) + assert.equal(retainedPools.length,beforeActivation) + activationRefused=false;events.length=0;reads.length=0 + const active=await compile(cli,modules).main(activeArgs) + assert.equal(active.event,'application-writer-profile-source-preflight');assert.equal(active.sourceActivationMatched,true) + assert.equal(active.readyAuthorized,false);assert.equal(active.writerAuthorized,false) + assert.deepEqual(events,['approval','transport','protected-source','activation-admission','preflight','pool-end','activation-terminal']) + assert.deepEqual(reads,['bootstrap','approval','definition','transport','ca']) + const legacyFlags=['bootstrap','bootstrap-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256'] + const legacyArgs=['legacy-inspect',...legacyFlags.flatMap(flag=>['--'+flag,flag.endsWith('sha256')?'a'.repeat(64):flag]),...activationFlags] + events.length=0;reads.length=0 + const legacy=await compile(cli,modules).main(legacyArgs) + assert.equal(legacy.event,'database-legacy-inspection');assert.equal(legacy.outcome,'observed');assert.equal(legacy.readyAuthorized,false) + assert.deepEqual(events,['transport','protected-source','activation-admission','legacy-inspect','pool-end','activation-terminal']) + assert.deepEqual(reads,['bootstrap','transport','ca']) + await assert.rejects(compile(cli,modules).main(legacyArgs.slice(0,-2))) + activationRefused=true;await assert.rejects(compile(cli,modules).main(legacyArgs),/Activation refused/);activationRefused=false + assert(catalogueCalls.length>0&&catalogueCalls.every(edition=>edition===44)) + catalogueEdition=56;catalogueCalls.length=0 + for(const commandArgs of [args,activeArgs,activated('publish')]){ + events.length=0 + await compile(cli,modules).main(commandArgs) + } + assert.deepEqual(catalogueCalls,[56,56,56]) + const publicationApproval=files.approval + files.approval={...fixture('',true,'upgrade').pinned,definitionSha256:'b'.repeat(64)} + events.length=0;reads.length=0;catalogueCalls.length=0 + const upgraded=await compile(cli,modules).main(activated('upgrade')) + assert.equal(upgraded.outcome,'committed') + assert.deepEqual(events,['upgrade-approval','transport','protected-source','activation-admission','upgrade','pool-end','activation-terminal']) + assert.deepEqual(catalogueCalls,[56]) + assert(reads.includes('signing-private.pem')) + catalogueEdition=57 + files.approval={...fixture('',true,'upgrade57').pinned,definitionSha256:'b'.repeat(64)} + events.length=0;reads.length=0;catalogueCalls.length=0 + assert.equal((await compile(cli,modules).main(activated('upgrade'))).outcome,'committed') + assert.deepEqual(catalogueCalls,[57]) + files.approval=publicationApproval + catalogueEdition=44;catalogueCalls.length=0 + incompatibleDefinition=true;events.length=0;reads.length=0 + const beforeBuildRefusal=retainedPools.length + for(const incompatibleArgs of [args,activeArgs,activated('publish')])await assert.rejects(compile(cli,modules).main(incompatibleArgs),/Build manifest refused/) + assert.equal(retainedPools.length,beforeBuildRefusal) + assert(!events.some(event=>event==='protected-source'||event==='activation-admission')) + const historical=await compile(cli,modules).main(['reconcile',...args.slice(1)]) + assert.equal(historical.outcome,'confirmed');assert.equal(historical.readyAuthorized,false) + assert.deepEqual(catalogueCalls,[]) +}) + diff --git a/backend/scripts/flow-read-profile-http-client.mjs b/backend/scripts/flow-read-profile-http-client.mjs new file mode 100644 index 00000000..0228f76a --- /dev/null +++ b/backend/scripts/flow-read-profile-http-client.mjs @@ -0,0 +1,45 @@ +if(!process.send)throw Error('Owned profile load client requires an IPC parent') +function requireLoopback(value){const url=new URL(value);if(url.protocol!=='http:'||!['127.0.0.1','localhost','[::1]'].includes(url.hostname)||url.username||url.password)throw Error('Owned test client only permits localhost HTTP')} +import {Client,StreamableHTTPClientTransport} from '@modelcontextprotocol/client' +import {getHeapStatistics} from 'node:v8' +const clients=new Map(),requests=new Map(),streams=new Map();let serial=0,peakRss=0,peakHeap=0 +const sample=()=>{const m=process.memoryUsage();peakRss=Math.max(peakRss,m.rss);peakHeap=Math.max(peakHeap,m.heapUsed);return{memory:m,peakRss,peakHeap,maxRssBytes:process.resourceUsage().maxRSS*1024,effectiveHeapSizeLimitBytes:getHeapStatistics().heap_size_limit}} +const timer=setInterval(sample,20) +const summarize=result=>{const row=result.structuredContent?.result??{};return{isError:result.isError===true,structuredContent:{result:{dataQuality:row.dataQuality??null,collectorQualityUnavailableReason:row.collectorQualityUnavailableReason,retainedRowsBytes:row.retainedRowsBytes,analyticsBytes:row.analytics?.bytes,eventBytes:row.events?.map(x=>x.data?.bytes),exporters:row.exporters?.length,clients:row.clients?.length,month:row.month}},hiddenSourceLeaked:JSON.stringify(result).includes('HIDDEN_MEMORY_SOURCE'),encodedBytes:Buffer.byteLength(JSON.stringify(result)),error:result.isError?result.content:undefined}} +async function run(message){ + const p=message.payload??{} + if(p.endpoint)requireLoopback(p.endpoint);if(p.url)requireLoopback(p.url) + if(message.operation==='connect'){ + const client=new Client({name:'external-owned-memory-client',version:'1'});await client.connect(new StreamableHTTPClientTransport(new URL(p.endpoint+'/mcp'),{requestInit:{headers:{authorization:'Bearer '+p.token}}}));const id=++serial;clients.set(id,client);return{id} + } + if(message.operation==='tool')return summarize(await clients.get(p.clientId).callTool(p.input)) + if(message.operation==='close'){await clients.get(p.clientId)?.close();clients.delete(p.clientId);return{closed:true}} + if(message.operation==='sse-open'){ + const abort=new AbortController(),response=await fetch(p.url,{signal:abort.signal});if(response.status!==200)throw Error('SSE status '+response.status) + const reader=response.body.getReader(),decoder=new TextDecoder();let text='' + for(;;){const chunk=await reader.read();if(chunk.done)throw Error('SSE closed before sample');text+=decoder.decode(chunk.value,{stream:true});const match=/event: (sample|error)\ndata: ([^\n]+)\n\n/.exec(text);if(match){if(match[1]!=='sample')throw Error('SSE source unavailable');const body=JSON.parse(match[2]),id=++serial;streams.set(id,{abort,reader});return{id,bytes:body.bytes}}} + } + if(message.operation==='sse-close'){const stream=streams.get(p.streamId);stream?.abort.abort();await stream?.reader.cancel().catch(()=>{});streams.delete(p.streamId);return{closed:true}} + if(message.operation==='http'){ + const abort=new AbortController();requests.set(message.id,abort) + try{const response=await fetch(p.url,{...p.options,signal:abort.signal}),body=await response.arrayBuffer();let summary;if(response.status===200&&(p.url.includes('/exporters')||p.url.includes('/analytics'))){const parsed=JSON.parse(new TextDecoder().decode(body));summary={exporters:parsed.exporters?.length,bytes:parsed.bytes}}return{status:response.status,bytes:body.byteLength,summary,text:body.byteLength<4096?new TextDecoder().decode(body):''}} + finally{requests.delete(message.id)} + } + if(message.operation==='batch'){ + const response=await fetch(p.endpoint+'/mcp',{method:'POST',headers:{authorization:'Bearer '+p.token,'content-type':'application/json',accept:'application/json, text/event-stream','mcp-protocol-version':'2025-06-18'},body:JSON.stringify([1,2].map(id=>({jsonrpc:'2.0',id,method:'tools/call',params:{name:'mm_netflow_analytics_report_get',arguments:{serverId:1,minutes:5}}})))}) + if(response.status!==200)throw Error('Batch HTTP status '+response.status) + const reader=response.body.getReader(),decoder=new TextDecoder(),messages=[];let text='' + for(;;){const chunk=await reader.read();if(chunk.done)break;text+=decoder.decode(chunk.value,{stream:true});let end;while((end=text.indexOf('\n\n'))>=0){const frame=text.slice(0,end);text=text.slice(end+2);for(const line of frame.split('\n'))if(line.startsWith('data: ')){const row=JSON.parse(line.slice(6));messages.push(row);if(row.error||row.result?.isError)process.send?.({type:'client-progress',id:message.id,refusals:1})}}} + if(messages.length!==2)throw Error('Expected two batch replies') + return{successes:messages.filter(row=>!row.error&&!row.result?.isError).length,refusals:messages.filter(row=>row.error||row.result?.isError).length} + } + throw Error('Unknown external client operation') +} +process.on('message',async message=>{ + if(message.type==='abort'){requests.get(message.requestId)?.abort();return} + if(message.type==='shutdown'){for(const abort of requests.values())abort.abort();for(const stream of streams.values())stream.abort.abort();for(const client of clients.values())try{await client.close()}catch{}clearInterval(timer);process.send?.({type:'client-report',report:sample()},()=>process.exit(0));return} + if(message.type!=='client-command')return + try{const result=await run(message);process.send?.({type:'client-result',id:message.id,ok:true,result})} + catch(error){process.send?.({type:'client-result',id:message.id,ok:false,error:error instanceof Error?error.message:String(error)})} +}) +process.send?.({type:'client-ready',report:sample()}) diff --git a/backend/scripts/legacy-probe-0037-owned.mjs b/backend/scripts/legacy-probe-0037-owned.mjs new file mode 100644 index 00000000..fef7be51 --- /dev/null +++ b/backend/scripts/legacy-probe-0037-owned.mjs @@ -0,0 +1,196 @@ +import assert from 'node:assert/strict' +import {createPublicKey,generateKeyPairSync,randomBytes,randomUUID,sign,X509Certificate} from 'node:crypto' +import {readFile,mkdir,writeFile} from 'node:fs/promises' +import {basename,dirname,join,resolve} from 'node:path' +import {tmpdir} from 'node:os' + +export async function verifyOwnedLegacy0037Handoff(pg,options,leafBytes,client,directory,inspectOnly=false){ + assert.equal(dirname(directory),resolve(tmpdir())) + assert.match(basename(directory),/^mm-archive-leaf-tls-/) + const fixtureState=join(directory,'mm-mcp-test-legacy-0037','state') + await mkdir(fixtureState,{recursive:true,mode:0o700}) + const tail=inspectOnly==='tail'?{user:'owned_commit_operator',password:randomBytes(24).toString('hex')}:null + const localLogin=tail??options + Object.assign(process.env,{MMAPP_OWN_PROBE_LEGACY_0037:'true',MMAPP_OWN_PROBE_LEGACY_ORPHANS:'true', + DATABASE_URL:`postgres://${localLogin.user}:${localLogin.password}@127.0.0.1:${options.port}/postgres`,MCP_STATE_DIR:fixtureState,AUTH_REQUIRED:'false'}) + const [{applyLegacyProbeFixtureMigrations},{initialDatabaseSchemaControlState},{runOwnedLegacyProbe0037Handoff}, + {captureLegacyProbeLineage,captureLegacyProbeStorage,LEGACY_PROBE_CUSTODY_LOCK,LEGACY_PROBE_RELATIONS}, + {runPinnedDatabaseSnapshotDump,runPinnedLegacyProbeSnapshotDump},{LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN}, + {sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry}, + {provisionDatabaseSourceBindingOffline},{retainPublisherTlsPeer},security,{collectLegacyServerProbeSnapshot}]=await Promise.all([ + import('../src/db/legacy-probe-fixture.ts'),import('../src/db/database-schema-control-model.ts'),import('../src/db/proposals/server-probe-legacy-0037-owned.ts'), + import('../src/services/server-probe-legacy-source-inventory.ts'),import('../src/services/database-dump-connection.ts'),import('../src/services/server-probe-legacy-archive-approval.ts'), + import('../src/db/database-source-binding.ts'),import('./database-source-binding-operator.ts'),import('./writer-profile/operator-contract.mjs'), + import('../src/modules/mcp/security.ts'),import('../src/services/server-probe-legacy-snapshot.ts'), + ]) + const live=new pg.Pool({...options,max:4}),coldDatabase='mm_owned_legacy_cold_'+randomUUID().replaceAll('-','') + let cold,coldAdmin + const store=async(name,value)=>{const file=join(directory,name);await writeFile(file,value,{flag:'wx',mode:0o600});return file} + const inspect=async(sql)=>{ + await sql.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + try { + let sqlState + const summary=await collectLegacyServerProbeSnapshot({query:async(text,values)=>{ + try { return await sql.query(text,values) } catch(error) { sqlState=error.code;throw error } + }},{deadlineAt:Date.now()+30000}) + assert.equal(summary.complete,true,JSON.stringify({reasons:summary.reasons,sqlState})) + const snapshot=await captureLegacyProbeStorage(sql),lineage=await captureLegacyProbeLineage(sql) + return {summary,snapshot,lineage} + } finally { await sql.query('ROLLBACK') } + } + try{ + if(tail){ + // Use the same canonical application owner from the first byte. The + // signed reference compares live owners and ACLs as part of its digest. + await live.query(`CREATE ROLE ${tail.user} LOGIN CREATEDB PASSWORD '${tail.password}'; + ALTER DATABASE postgres OWNER TO ${tail.user}; COMMENT ON DATABASE postgres IS NULL`) + const application=new pg.Pool({...options,...tail,max:4}) + try{await applyLegacyProbeFixtureMigrations(application)}finally{await application.end()} + }else await applyLegacyProbeFixtureMigrations(live) + await live.query(`CREATE SCHEMA mmapp_control; CREATE TABLE mmapp_control.database_schema_state(id smallint PRIMARY KEY CHECK(id=1),revision bigint NOT NULL CHECK(revision>=0),state jsonb NOT NULL); + CREATE TABLE mmapp_control.database_schema_intents(id uuid PRIMARY KEY,intent jsonb NOT NULL,terminal jsonb)`) + await live.query('INSERT INTO mmapp_control.database_schema_state VALUES(1,0,$1::jsonb)',[JSON.stringify(initialDatabaseSchemaControlState())]) + const owner='owned-legacy-history-owner',generation=randomUUID(),connection=randomUUID(),credential=randomUUID(),plan=randomUUID(),operation=randomUUID() + const restReceipt=randomUUID(),restExecution=randomUUID(),mcpReceipt=randomUUID(),quarantine=randomUUID(),quarantinedExecution=randomUUID() + const expiresAt=Date.UTC(2000,0,1),issuedAt=expiresAt-3600000,expiry=new Date(expiresAt).toISOString(),bindingProof='a'.repeat(64) + await live.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation) VALUES($1,'owned history',$2,'[]','{}',$3)",[connection,owner,generation]) + await live.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)",[credential,connection,randomUUID(),expiry]) + const binding={version:1,operation:'servers.probes.run',principal:{connectionId:connection,credentialId:credential,credentialExpiresAt:expiry}, + ownerId:owner,issuedAt,authorityExpiresAt:expiresAt,prepared:{receiptId:mcpReceipt,serverId:1,tool:'ping',expiresAt:expiry, + bindingCiphertext:await security.encryptSecret('owned original provider request')},bindingProof,stageBudget:2} + const step={kind:'service',service:'servers.probes.run',input:{receiptId:mcpReceipt,serverId:1, + inputDigest:await security.privateSecretFingerprint(security.fingerprint(binding),'server-probe-mcp-binding-v1'), + bindingCiphertext:await security.encryptSecret(JSON.stringify(binding))}} + const evidence={sealed:await security.encryptSecret(JSON.stringify({steps:[step],target:{scope:'servers',serverIds:[1]},requiresHuman:true, + verificationScope:'acknowledgement',mutationCost:2,validUntil:expiry}))} + await live.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by) + VALUES($1,$2,'servers.probes.run',1,1,$3,'{}',$4::jsonb,'completed',$5,$6)`,[plan,connection,generation,JSON.stringify(evidence),expiry,owner]) + await live.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,finished_at) VALUES($1,$2,$3,$4,'applied',clock_timestamp())",[operation,plan,connection,randomUUID()]) + await live.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)",[operation,JSON.stringify(security.redact(step))]) + await live.query('INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3)',[operation,connection,owner]) + const context={identity:{kind:'rest',ownerId:owner,executionId:restExecution,requestId:'owned-original-request',authentication:'jwt',issuedAt,expiresAt},sourceServerId:1,tool:'ping'} + await live.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool) + VALUES($1,$2,'rest',$3,$4,'jwt','owned-original-request',$5,$6,1,'ping')`,[restReceipt,bindingProof,restExecution,owner,issuedAt,expiry]) + await live.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,received_at) VALUES($1,1,'native-ping',1000,'received','received',clock_timestamp())",[restReceipt]) + const receipt={receiptId:restReceipt,tool:'ping',serverId:1,state:'completed',httpStatus:200,output:'owned original result',data:{kind:'ping'}, + attemptedStages:1,completedStages:1,automaticReplay:false,forwardingVerified:false} + const plaintext=JSON.stringify({version:1,context,bindingProof,receipt}) + await live.query(`UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,finished_at=clock_timestamp(),receipt_ciphertext=$2,receipt_proof=$3 + WHERE receipt_id=$1`,[restReceipt,await security.encryptSecret(plaintext),await security.privateSecretFingerprint(security.fingerprint(plaintext),'server-probe-journal-v1:receipt')]) + await live.query(`INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) + VALUES($1,repeat('b',64),'rest',$2,repeat('c',64),'JOURNAL_UNKNOWN',0,0)`,[quarantine,quarantinedExecution]) + const dependencySql=`SELECT jsonb_build_object('operation',to_jsonb(o),'plan',to_jsonb(p),'connection',to_jsonb(c),'step',to_jsonb(s),'credential',to_jsonb(k)) AS original + FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id JOIN public.mcp_connections c ON c.id=o.connection_id + JOIN public.mcp_operation_steps s ON s.operation_id=o.id JOIN public.mcp_credentials k ON k.connection_id=c.id WHERE o.id=$1 AND k.id=$2` + const dependencies=(await live.query(dependencySql,[operation,credential])).rows + assert.equal(dependencies.length,1) + const dependencyHash=sourceBindingSha256(sourceBindingBytes(dependencies)) + const beforeClient=await live.connect() + let before + try { before=await inspect(beforeClient) } finally { beforeClient.release() } + assert.equal(before.snapshot.rowCount,4);assert.equal(before.lineage.rowCount,4) + assert.equal(before.summary.counts.verified,2,JSON.stringify(before.summary.reasons));assert.equal(before.summary.counts.unresolved,1) + + const privateKey=generateKeyPairSync('ed25519').privateKey, signer=await store('owned-source-signer.pem',privateKey.export({type:'pkcs8',format:'pem'})) + const privatePassword=await store('owned-archive-password',options.password+'\n') + const peerLeaf=new X509Certificate(leafBytes),host='localhost',exporter=await live.connect() + let originalPin,archive,fullArchive,signedApproval + try{ + retainPublisherTlsPeer(exporter,{serverName:host,spkiSha256:sourceBindingSha256(peerLeaf.publicKey.export({type:'spki',format:'der'}))})() + await exporter.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + await exporter.query(LEGACY_PROBE_CUSTODY_LOCK) + const {rows:[identity]}=await exporter.query(`SELECT current_database() AS "databaseName",current_user::text AS "currentUser",session_user::text AS "sessionUser", + (pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier",pg_catalog.pg_is_in_recovery() AS "inRecovery", + current_setting('server_version_num')::int AS "serverVersionNum",d.oid::int AS "databaseOid",r.oid::text AS "signedOid" + FROM pg_catalog.pg_database d,pg_catalog.pg_roles r WHERE d.datname=current_database() AND r.rolname=current_user`) + const {signedOid,...source}=identity + const installationId=randomUUID(),sourceIdentityId=randomUUID(),request={installationId,sourceIdentityId,generation,source} + ;({deploymentPin:originalPin}=await provisionDatabaseSourceBindingOffline({request,privateKeyFile:signer,outputDirectory:join(directory,'owned-original-registry')})) + const registry=await readDatabaseSourceBindingRegistry(originalPin) + const storage=await captureLegacyProbeStorage(exporter),lineage=await captureLegacyProbeLineage(exporter) + assert.deepEqual(storage,before.snapshot);assert.deepEqual(lineage,before.lineage) + const {rows:[{snapshot}]}=await exporter.query('SELECT pg_catalog.pg_export_snapshot() AS snapshot') + const peer={host,port:options.port,database:'postgres',capture:{name:options.user,signedOid},leaf:peerLeaf.toString()} + archive=await runPinnedLegacyProbeSnapshotDump(peer,options.password,snapshot,directory,{}) + fullArchive=await runPinnedDatabaseSnapshotDump(peer,privatePassword,snapshot,{}) + const body={format:'legacy-probe-custody-approval-v1',installationId,sourceIdentityId,generation,ordinal:1, + headSha256:registry.headSha256,bindingSha256:registry.head.bindingSha256,archiveSha256:sourceBindingSha256(archive), + storageSha256:storage.sha256,lineageSha256:lineage.sha256} + signedApproval=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN+sourceBindingCanonical(body)),privateKey).toString('base64url')}) + } finally { try { await exporter.query('ROLLBACK') } finally { exporter.release() } } + await store('owned-legacy-probe.dump',archive);await store('owned-legacy-probe-approval.json',signedApproval) + const archiveFile=await store('owned-full-cold.dump',fullArchive) + const {stdout:toc}=await client('pg_restore',['--list',archiveFile],'pinned') + assert.doesNotMatch(toc,/mmapp_control/) + await live.query(`CREATE DATABASE ${coldDatabase} TEMPLATE template0`) + coldAdmin=new pg.Client({...options,database:coldDatabase});await coldAdmin.connect() + if(/\bSCHEMA - public\b/.test(toc))await coldAdmin.query('DROP SCHEMA public RESTRICT') + try { await client('pg_restore',['--dbname='+coldDatabase,'--exit-on-error','--no-owner',archiveFile],'pinned') } + catch { throw Error('Owned full legacy cold restore failed') } + const coldPassword=randomUUID() + await coldAdmin.query(`CREATE ROLE owned_legacy_reader LOGIN PASSWORD '${coldPassword}'; GRANT USAGE ON SCHEMA public TO owned_legacy_reader; GRANT SELECT ON TABLE + ${['schema_migrations','mcp_operations','mcp_operation_steps','mcp_plans','mcp_connections','mcp_credentials',...LEGACY_PROBE_RELATIONS].map(name=>`public.${name}`).join(', ')} TO owned_legacy_reader`) + cold=new pg.Client({...options,database:coldDatabase,user:'owned_legacy_reader',password:coldPassword});await cold.connect() + const coldState=join(directory,'owned-cold-key-state') + await mkdir(coldState,{mode:0o700}) + const archivedKey=await readFile(join(fixtureState,'encryption-key')) + try { await writeFile(join(coldState,'encryption-key'),archivedKey,{flag:'wx',mode:0o600}) } finally { archivedKey.fill(0) } + const assertCold=async()=>{ + let read + process.env.MCP_STATE_DIR=coldState + try { read=await inspect(cold) } finally { process.env.MCP_STATE_DIR=fixtureState } + assert.deepEqual(read,before) + assert.equal(sourceBindingSha256(sourceBindingBytes((await cold.query(dependencySql,[operation,credential])).rows)),dependencyHash) + assert((await cold.query('SELECT execution_id FROM public.server_probe_executions WHERE receipt_id=$1',[restReceipt])).rows[0]?.execution_id===restExecution,'Original receipt ID drift') + assert((await cold.query('SELECT execution_id FROM public.server_probe_execution_quarantines WHERE receipt_id=$1',[quarantine])).rows[0]?.execution_id===quarantinedExecution,'Original unresolved ID drift') + await assert.rejects(cold.query('DELETE FROM public.server_probe_execution_quarantines WHERE receipt_id=$1',[quarantine]),{code:'42501'}) + } + await assertCold() + if(['cold','execute','adopt','release','tail'].includes(inspectOnly)){ + const {verifyOwnedLegacyColdCommand}=await import('./legacy-probe-cold-verify-owned.mjs') + return await verifyOwnedLegacyColdCommand({live,coldAdmin,cold,options,directory,coldState,leafBytes,originalPin,archive,fullArchive,signedApproval, + execute:inspectOnly!=='cold',adopt:['adopt','release','tail'].includes(inspectOnly),release:['release','tail'].includes(inspectOnly), + tail,sourcePrivateKey:privateKey}) + } + if(inspectOnly){ + const {verifyOwnedHandoffInspector}=await import('./legacy-probe-handoff-inspect-owned.mjs') + return await verifyOwnedHandoffInspector({live,directory,options,leafBytes,originalPin,archive,fullArchive,signedApproval,privateKey,intentOnly:inspectOnly==='intent'}) + } + const operatorPublicKeyPem=Buffer.from(createPublicKey(privateKey).export({type:'spki',format:'pem'})) + const archiveApproval={archive,signedApproval,operatorPublicKeyPem}, input={live,cold,coldDatabase,fixtureDirectory:directory,originalPin,archiveApproval} + const expectPre0037=async()=>{ + const sql=await live.connect() + try { assert.deepEqual(await inspect(sql),before) } finally { sql.release() } + const {rows:[row]}=await live.query(`SELECT count(*)::int AS markers, to_regclass('public.server_probe_storage_admissions')::text AS v2 + FROM public.schema_migrations`) + assert.deepEqual(row,{markers:36,v2:null}) + } + await assert.rejects(runOwnedLegacyProbe0037Handoff({...input,archiveApproval:{...archiveApproval,archive:Buffer.concat([archive,Buffer.from('tamper')])}}),/approval refused/) + await coldAdmin.query("UPDATE public.mcp_operation_steps SET intended_state='{\"drift\":true}' WHERE operation_id=$1",[operation]) + await assert.rejects(runOwnedLegacyProbe0037Handoff(input),{code:'ERR_ASSERTION'}) + await coldAdmin.query('UPDATE public.mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1', + [operation,JSON.stringify(dependencies[0].original.step.intended_state)]) + await expectPre0037() + const competingWriter=await live.connect() + try{ + await competingWriter.query('BEGIN') + await competingWriter.query('LOCK TABLE public.mcp_plans IN ROW EXCLUSIVE MODE') + await assert.rejects(runOwnedLegacyProbe0037Handoff(input),{code:'55P03'}) + } finally { try { await competingWriter.query('ROLLBACK') } finally { competingWriter.release() } } + await assert.rejects(runOwnedLegacyProbe0037Handoff({...input,failAfterBody:true}),{code:'22012'}) + await expectPre0037() + const outcome=await runOwnedLegacyProbe0037Handoff(input) + assert.deepEqual(outcome,{archivedRows:4,lineageRows:4,readyAuthorized:false}) + await assertCold() + assert.equal(sourceBindingSha256(sourceBindingBytes((await live.query(dependencySql,[operation,credential])).rows)),dependencyHash) + const {rows:history}=await live.query("SELECT terminal->>'outcome' AS outcome FROM mmapp_control.database_schema_intents ORDER BY (intent->>'epoch')::int") + assert.deepEqual(history,[{outcome:'observed-failure'},{outcome:'observed-success'}]) + return {ownedAtomicLegacy0037Handoff:true,coldArchiveRows:4,verifiedHistoricalIdentities:2,unresolvedHistoricalIdentities:1,readyAuthorized:false} + } finally { + await Promise.allSettled([cold?.end(),coldAdmin?.end(),live.end()]) + if(inspectOnly){ + const {closePool}=await import('../src/db/index.ts') + await closePool() + } + } +} diff --git a/backend/scripts/legacy-probe-cold-verify-owned.mjs b/backend/scripts/legacy-probe-cold-verify-owned.mjs new file mode 100644 index 00000000..4012f622 --- /dev/null +++ b/backend/scripts/legacy-probe-cold-verify-owned.mjs @@ -0,0 +1,78 @@ +import assert from 'node:assert/strict' +import {createPublicKey,generateKeyPairSync,X509Certificate} from 'node:crypto' +import {mkdir,open,readFile,realpath,readdir,unlink,writeFile} from 'node:fs/promises' +import {dirname,join,sep} from 'node:path' + +export async function modelOwnedOperatorFiles(directory,sha){ + if(process.platform!=='win32')return + const {mock}=await import('node:test'),url=new URL('./writer-profile/operator-files.mjs',import.meta.url),files=await import(url.href) + const protectedDirectory=async path=>{assert(path===directory||path.startsWith(directory+sep));assert.equal(await realpath(path),path)} + return mock.module(url,{namedExports:{...files,protectedDirectory,async writeFile(path,blob,opts){ + await protectedDirectory(dirname(path));const file=await open(path,opts.flag,opts.mode);try{await file.writeFile(blob);await file.sync()}finally{await file.close()} + },async readProtected(path,limit,hash){ + await protectedDirectory(dirname(path));const bytes=await readFile(path);assert(bytes.length>0&&bytes.length<=limit);if(hash!==undefined)assert.equal(sha(bytes),hash);return bytes + },async withJournalLease(path,work){ + await protectedDirectory(path);const lock=join(path,'publisher-lease.lock'),file=await open(lock,'wx',0o600) + try{return await work()}finally{await file.close();await unlink(lock)} + }}}) +} + +export async function verifyOwnedLegacyColdCommand({live,coldAdmin,cold,options,directory,coldState,leafBytes,originalPin,archive,fullArchive,signedApproval,execute,adopt,release,tail,sourcePrivateKey}){ + assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_0037,'true') + const [{sourceBindingBytes,sourceBindingSha256},{captureLegacyProbeCredentialDigest,verifyLegacyColdProof},{captureLegacyProbeStorage,captureLegacyProbeLineage}]=await Promise.all([ + import('../src/db/database-source-binding.ts'),import('../src/services/server-probe-legacy-cold-proof.ts'),import('../src/services/server-probe-legacy-source-inventory.ts'), + ]) + const sha=sourceBindingSha256,store=async(path,data)=>{await writeFile(path,data,{flag:'wx',mode:0o600});return path} + const archiveDirectory=join(directory,'cold-protected-archive'),outputDirectory=join(directory,'cold-proof-output') + await mkdir(archiveDirectory,{mode:0o700});await mkdir(outputDirectory,{mode:0o700}) + for(const [name,data] of [['legacy-probe.dump',archive],['legacy-probe-approval.json',signedApproval],['legacy-probe-cold.dump',fullArchive]])await store(join(archiveDirectory,name),data) + await coldAdmin.query('GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO owned_legacy_reader') + const {rows:[identity]}=await cold.query(`SELECT current_database() AS "databaseName", current_user::text AS "currentUser", session_user::text AS "sessionUser", + (pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier",pg_catalog.pg_is_in_recovery() AS "inRecovery", + current_setting('server_version_num')::int AS "serverVersionNum",d.oid::int AS "databaseOid",r.oid::text AS "roleOid" + FROM pg_catalog.pg_database d,pg_catalog.pg_roles r WHERE d.datname=current_database() AND r.rolname=current_user`) + const {roleOid,...coldSource}=identity + const key=generateKeyPairSync('ed25519').privateKey,publicKey=createPublicKey(key),host='localhost' + const signerSpkiSha256=sha(publicKey.export({type:'spki',format:'der'})) + const storedKey=await readFile(join(coldState,'encryption-key'));const keySha256=sha(storedKey);storedKey.fill(0) + const approval=sourceBindingBytes({format:'legacy-probe-cold-verification-approval-v1',installationId:originalPin.installationId, + sourceHeadSha256:originalPin.currentHeadSha256,sourceSignerSpkiSha256:originalPin.publicKeySpkiSha256,custodySha256:sha(signedApproval), + coldArchiveSha256:sha(fullArchive),coldSignerSpkiSha256:signerSpkiSha256,keySha256,coldSource,coldRoleOid:roleOid, + tls:{serverName:host,caSha256:sha(leafBytes),spkiSha256:sha(new X509Certificate(leafBytes).publicKey.export({type:'spki',format:'der'}))},expiresAt:Date.now()+120000}) + const transport=sourceBindingBytes({host,port:options.port,user:cold.user,password:cold.password,database:cold.database}) + const argv=['legacy-cold-verify','--approval',await store(join(directory,'cold-verification.json'),approval),'--approval-sha256',sha(approval), + '--transport',await store(join(directory,'cold-transport.json'),transport),'--transport-sha256',sha(transport), + '--ca',await store(join(directory,'cold-ca.pem'),leafBytes),'--source-registry',originalPin.registryDirectory,'--archive-directory',archiveDirectory, + '--legacy-key-directory',coldState,'--cold-signing-key',await store(join(directory,'cold-signing.pem'),key.export({type:'pkcs8',format:'pem'})), + '--output-directory',outputDirectory] + const modeledFiles=await modelOwnedOperatorFiles(directory,sha) + try{ + const {main}=await import('./database-writer-profile-operator.mts') + if(!execute){ + await coldAdmin.query('GRANT UPDATE ON public.mcp_credentials TO owned_legacy_reader') + try{await assert.rejects(main(argv),/read-only privileges/)}finally{await coldAdmin.query('REVOKE UPDATE ON public.mcp_credentials FROM owned_legacy_reader')} + assert.deepEqual(await readdir(outputDirectory),[]) + } + const result=await main(argv),proof=await readFile(join(outputDirectory,result.proofFile)) + assert.equal(result.event,'database-legacy-cold-proof');assert.equal(result.proofSha256,sha(proof));assert.equal(result.conversionAllowed,false) + const source=await live.connect() + try{ + await source.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + const storage=await captureLegacyProbeStorage(source),lineage=await captureLegacyProbeLineage(source) + const pinned={publicKeyPem:Buffer.from(publicKey.export({type:'spki',format:'pem'})),signerSpkiSha256, + sourceSignerSpkiSha256:originalPin.publicKeySpkiSha256,approvalSha256:sha(approval),coldArchiveSha256:sha(fullArchive),custodySha256:sha(signedApproval), + storageSha256:storage.sha256,lineageSha256:lineage.sha256} + const verified=verifyLegacyColdProof(proof,pinned) + assert.equal(verified.credentialSha256,await captureLegacyProbeCredentialDigest(source));assert.equal(verified.keySha256,keySha256) + assert.deepEqual(verified.coldSource,coldSource) + assert.throws(()=>verifyLegacyColdProof(proof,{...pinned,coldArchiveSha256:'0'.repeat(64)}),/proof refused/) + assert.throws(()=>verifyLegacyColdProof(proof,{...pinned,sourceSignerSpkiSha256:signerSpkiSha256}),/proof refused/) + }finally{try{await source.query('ROLLBACK')}finally{source.release()}} + if(execute){ + const {verifyOwnedLegacyExecution}=await import('./legacy-probe-handoff-execute-owned.mjs') + return await verifyOwnedLegacyExecution({main,live,options,directory,archiveDirectory,coldState,leafBytes,originalPin,fullArchive,signedApproval, + adopt,release,tail,sourcePrivateKey,coldPrivateKey:key,coldPublicKey:publicKey,proof,coldApprovalSha256:sha(approval)}) + } + return {coldProofIssued:true,distinctIssuerVerified:true,credentialDigestMatched:true,writePrivilegedRoleRefused:true,readyAuthorized:false} + }finally{modeledFiles?.restore()} +} diff --git a/backend/scripts/legacy-probe-handoff-execute-owned.mjs b/backend/scripts/legacy-probe-handoff-execute-owned.mjs new file mode 100644 index 00000000..9d3a4f67 --- /dev/null +++ b/backend/scripts/legacy-probe-handoff-execute-owned.mjs @@ -0,0 +1,259 @@ +import assert from 'node:assert/strict' +import {randomBytes,randomUUID,sign,X509Certificate} from 'node:crypto' +import {mkdir,readFile,writeFile} from 'node:fs/promises' +import {join} from 'node:path' +import pg from 'pg' + +export async function verifyOwnedLegacyExecution({main,live,options,directory,archiveDirectory,coldState,leafBytes,originalPin,fullArchive,signedApproval, + adopt,release,tail,sourcePrivateKey,coldPrivateKey,coldPublicKey,proof,coldApprovalSha256}){ + assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_0037,'true') + const [{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry},{readReviewedLegacyProbe0037}, + {LEGACY_HANDOFF_EXECUTION_DOMAIN,LEGACY_HANDOFF_RELEASE_DOMAIN},{captureLegacyHandoffSchema},{LEGACY_PROBE_RELATIONS},{acquireDatabaseSchemaControl},{observation,exactHistory}, + {MIGRATION_MANIFEST,migrationManifestDigest},{DATABASE_RESTORE_QUARANTINE_DDL},{createBootstrapMaterial}]=await Promise.all([ + import('../src/db/database-source-binding.ts'),import('../src/db/legacy-probe-0037-body.ts'),import('./writer-profile/legacy-handoff-execution.mts'), + import('../src/services/server-probe-legacy-handoff-inspection.ts'),import('../src/services/server-probe-legacy-source-inventory.ts'), + import('../src/db/database-schema-control.ts'),import('../src/db/controlled-migration-history.ts'),import('../src/db/migration-manifest.ts'), + import('../src/db/database-restore-quarantine.ts'),import('./database-writer-bootstrap-operator.mjs'), + ]) + const sha=sourceBindingSha256,store=async(path,data)=>{await writeFile(path,data,{flag:'wx',mode:0o600});return path} + const publisher=options.user,operator='owned_commit_operator',control='owned_commit_control' + const appPassword=tail?.password??randomBytes(24).toString('hex'),controlPassword=randomBytes(24).toString('hex') + await live.query(DATABASE_RESTORE_QUARANTINE_DDL) + if(tail)assert.equal(tail.user,operator) + else await live.query(`CREATE ROLE ${operator} LOGIN PASSWORD '${appPassword}'; GRANT ${publisher} TO ${operator}; ALTER SCHEMA public OWNER TO ${operator}`) + await live.query(`CREATE ROLE ${control} LOGIN PASSWORD '${controlPassword}'; + CREATE ROLE owned_commit_runtime NOLOGIN; CREATE ROLE owned_commit_capture NOLOGIN; + GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${control},${operator}; GRANT pg_read_all_stats TO ${control}; + GRANT USAGE ON SCHEMA mmapp_control TO ${control},${operator}; GRANT SELECT, UPDATE ON mmapp_control.database_schema_state,mmapp_control.database_restore_quarantine TO ${control},${operator}; + GRANT SELECT, INSERT, UPDATE ON mmapp_control.database_schema_intents TO ${control},${operator}`) + let runTail + if(tail){ + const [{pool},{executeControlledSqlMigrations},{LEGACY_CONTROL_REFERENCE_DOMAIN,readControlledSchemaReference},{verifyControlledSchemaCurrentState}]=await Promise.all([ + import('../src/db/index.ts'),import('../src/db/controlled-migrations.ts'),import('../src/db/controlled-schema-reference-core.ts'), + import('../src/db/controlled-schema-verification.ts'), + ]) + runTail=async(quarantined,handoff)=>{ + const files=[],fileHashes=new Map(MIGRATION_MANIFEST.map(pin=>[pin.sha256,pin.file])) + const originalConnect=pool.connect + pool.connect=async()=>{ + const owned=await originalConnect.call(pool),query=owned.query,release=owned.release + owned.query=function(text,...args){ + const file=typeof text==='string'&&fileHashes.get(sha(Buffer.from(text))) + if(file)files.push(file) + return query.call(this,text,...args) + } + owned.release=function(...args){owned.query=query;owned.release=release;return release.apply(this,args)} + return owned + } + try{ + if(quarantined){ + await assert.rejects(executeControlledSqlMigrations(),error=>error.cause?.message==='Authenticated legacy migration release required') + assert.deepEqual(files,[]) + return + } + const result=await executeControlledSqlMigrations() + assert.equal(result.completedFiles,MIGRATION_MANIFEST.length);assert.equal(result.ready,false) + assert.deepEqual(files,MIGRATION_MANIFEST.slice(37).map(pin=>pin.file)) + }finally{pool.connect=originalConnect} + const referenceRoles={publisher:request.roles.publisher,operator:request.roles.operator,control:request.roles.control} + const approvalBody={format:'legacy-control-reference-approval-v1',manifestSha256,sourceHeadSha256:source.headSha256, + sourceTlsSpkiSha256:tls.spkiSha256,...referenceRoles,expiresAt:Date.now()+90000} + const referenceArgs=async(body,filename)=>{ + const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_CONTROL_REFERENCE_DOMAIN+sourceBindingCanonical(body)),coldPrivateKey).toString('base64url')}) + const flags=['--bootstrap','--bootstrap-sha256','--publisher-transport','--publisher-transport-sha256','--operator-transport','--operator-transport-sha256', + '--ca','--source-registry','--source-spki-sha256','--activation-directory','--activation-sha256','--cold-public-key','--cold-spki-sha256'] + return ['legacy-control-reference',...flags.flatMap(flag=>[flag,args[args.indexOf(flag)+1]]), + '--approval',await store(join(directory,filename),bytes),'--approval-sha256',sha(bytes)] + } + await assert.rejects(main(await referenceArgs({...approvalBody,control:referenceRoles.operator},'wrong-control-reference.json')),/independent split-control approval/) + const approved=await referenceArgs(approvalBody,'control-reference.json') + const reference=await main(approved),current=await verifyControlledSchemaCurrentState() + assert.equal(reference.splitControlApprovalSha256,approved.at(-1));assert.equal(reference.referenceDatabaseDropped,true) + assert.equal((await readControlledSchemaReference()).sha256,current.schemaSha256) + assert.equal(current.schemaSha256,reference.logicalSchemaSha256);assert.equal(current.referenceStatus,'matched');assert.equal(current.ready,false) + assert.equal(current.currentPristineDataInvariants,false) + await live.query('GRANT USAGE ON SCHEMA mmapp_control TO owned_commit_runtime') + try{ + const extra=await verifyControlledSchemaCurrentState() + assert.equal(extra.referenceStatus,'different');assert.equal(extra.ready,false);assert.equal(extra.dataFacts.length,0) + }finally{await live.query('REVOKE USAGE ON SCHEMA mmapp_control FROM owned_commit_runtime')} + const [{LEGACY_SOURCE58_PREFLIGHT_DOMAIN},{RESTORE_WRITER_PROFILE_DEFINITION_SHA256}]=await Promise.all([ + import('../src/db/legacy-source58-transition.ts'),import('../src/db/database-restore-quarantine-profile.ts'), + ]) + const targetCatalogueSha256=sha(Buffer.from('owned signed intended Source58 target; installation outside this preflight')) + const target={format:'legacy-source58-preflight-approval-v1',manifestSha256,sourceHeadSha256:source.headSha256, + sourceTlsSpkiSha256:tls.spkiSha256,currentSchemaSha256:reference.logicalSchemaSha256,...handoff, + targetDefinitionSha256:RESTORE_WRITER_PROFILE_DEFINITION_SHA256,targetCatalogueSha256,roles:request.roles,expiresAt:Date.now()+90000} + const preflightArgs=async(body,name)=>{ + const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_SOURCE58_PREFLIGHT_DOMAIN+sourceBindingCanonical(body)),coldPrivateKey).toString('base64url')}) + const flags=approved.slice(1,-4),operatorAt=flags.indexOf('--operator-transport') + flags.splice(operatorAt,4) + return ['legacy-source58-preflight',...flags,'--approval',await store(join(directory,name),bytes),'--approval-sha256',sha(bytes)] + } + await assert.rejects(main(await preflightArgs({...target,releaseApprovalSha256:'0'.repeat(64)},'wrong-source58-target.json')),/exact authenticated release/) + const observed=await main(await preflightArgs(target,'source58-target.json')) + assert.equal(observed.authenticatedFiles,MIGRATION_MANIFEST.length);assert.equal(observed.transitionAuthorized,false);assert.equal(observed.readyAuthorized,false) + assert.equal(observed.approvedTargetCatalogueSha256,targetCatalogueSha256) + const expectedBlockers=['CONTROL_NAMESPACE_OWNER','QUARANTINE_OWNER','WRITER_REGISTRY_MISSING','REVIEWED_MARKER_FENCE_MISSING', + 'ROLE_PROFILE_OPERATOR','ROLE_PROFILE_PUBLISHER','ROLE_PROFILE_RUNTIME','ROLE_PROFILE_CAPTURE'] + assert.deepEqual(observed.blockers,expectedBlockers) + return {manifestSha256,logicalSchemaSha256:current.schemaSha256,referenceLogicalSchemaSha256:reference.logicalSchemaSha256, + referenceStatus:current.referenceStatus,splitControlApprovalSha256:reference.splitControlApprovalSha256,exactSplitControlCatalogueMatched:true, + invalidRoleRefused:true,excessNamespaceAclRefused:true, + referenceDatabaseDropped:reference.referenceDatabaseDropped, + files,legacySource58PredecessorAttested:true,wrongReleasePinRefused:true,source58Blockers:observed.blockers, + source58TransitionAuthorized:false,readyAuthorized:current.ready,historicalDataEquivalent:current.historicalDataEquivalent} + } + } + const manifestSha256=migrationManifestDigest(),controller=await acquireDatabaseSchemaControl(live) + try{ + for(const migration of MIGRATION_MANIFEST.slice(0,36)){ + const id=randomUUID(),begun=await controller.transition({type:'begin',id,kind:'migration',manifestSha256,artifactSha256:migration.sha256}) + await controller.transition({type:'terminal',id,outcome:'observed-success',observationSha256:await observation(begun.state.intent,migration)}) + } + }finally{await controller.close()} + const {body,pin}=await readReviewedLegacyProbe0037(),preview=new pg.Client({...options,user:operator,password:appPassword}) + await preview.connect() + let preSchemaSha256,postSchemaSha256 + try{ + await preview.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + preSchemaSha256=await captureLegacyHandoffSchema(preview,Date.now()+30000);await preview.query('ROLLBACK') + await preview.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ WRITE') + await preview.query(`TRUNCATE TABLE ${LEGACY_PROBE_RELATIONS.map(name=>`ONLY public.${name}`).join(', ')} CONTINUE IDENTITY RESTRICT`) + await preview.query(body);await preview.query('SET TRANSACTION READ ONLY') + postSchemaSha256=await captureLegacyHandoffSchema(preview,Date.now()+30000) + }finally{try{await preview.query('ROLLBACK')}finally{await preview.end()}} + assert.notEqual(preSchemaSha256,postSchemaSha256) + const source=await readDatabaseSourceBindingRegistry(originalPin),host='localhost',tls={serverName:host,caSha256:sha(leafBytes), + spkiSha256:sha(new X509Certificate(leafBytes).publicKey.export({type:'spki',format:'der'}))} + const names={publisher,operator,control,runtime:'owned_commit_runtime',capture:'owned_commit_capture'} + const {rows:rows}=await live.query('SELECT rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1)',[Object.values(names)]) + const request={format:'application-writer-bootstrap-request-v1',installationId:originalPin.installationId,sourceHeadSha256:originalPin.currentHeadSha256,tls, + roles:Object.fromEntries(Object.entries(names).map(([scope,name])=>[scope,rows.find(role=>role.name===name)])),source:{systemIdentifier:source.body.source.systemIdentifier, + databaseOid:String(source.body.source.databaseOid),databaseName:source.body.source.databaseName,serverVersionNum:source.body.source.serverVersionNum,inRecovery:false}} + const bootstrap=sourceBindingBytes(createBootstrapMaterial(request,sha(sourceBindingBytes(request))).bootstrap) + const activeDirectory=join(directory,'owned-execute-activation');await mkdir(activeDirectory,{mode:0o700}) + const activation=sourceBindingBytes({format:'database-source-activation-v1',activationDirectory:activeDirectory,installationId:originalPin.installationId, + sourceIdentityId:source.body.sourceIdentityId,status:'active',publicKeySpkiSha256:originalPin.publicKeySpkiSha256, + generations:[{generation:source.body.generation,headSha256:source.headSha256,bindingSha256:source.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]}) + await store(join(activeDirectory,'source-activation.json'),activation) + const operationId=randomUUID(),approvalBody={format:'legacy-probe-0037-execution-approval-v1',decision:'execute-and-commit',operationId, + sourceHeadSha256:originalPin.currentHeadSha256,sourceTlsSpkiSha256:tls.spkiSha256,custodySha256:sha(signedApproval),coldArchiveSha256:sha(fullArchive), + coldProofSha256:sha(proof),coldApprovalSha256,preSchemaSha256,postSchemaSha256,manifestSha256,migrationSha256:pin.sha256,protectedRuntimePaused:true,expiresAt:Date.now()+90000} + const approved=sourceBindingBytes({body:approvalBody,signature:sign(null,Buffer.from(LEGACY_HANDOFF_EXECUTION_DOMAIN+sourceBindingCanonical(approvalBody)),coldPrivateKey).toString('base64url')}) + await store(join(archiveDirectory,'legacy-probe-cold-proof.json'),proof) + const shared=['--bootstrap',await store(join(directory,'execute-bootstrap.json'),bootstrap),'--bootstrap-sha256',sha(bootstrap), + '--ca',await store(join(directory,'execute-ca.pem'),leafBytes),'--source-registry',originalPin.registryDirectory,'--source-spki-sha256',originalPin.publicKeySpkiSha256, + '--activation-directory',activeDirectory,'--activation-sha256',sha(activation),'--archive-directory',archiveDirectory] + const args=['legacy-handoff-execute',...shared,'--legacy-key-directory',coldState,'--source-signing-key',await store(join(directory,'execute-source-key.pem'),sourcePrivateKey.export({type:'pkcs8',format:'pem'})), + '--approval',await store(join(directory,'execute-approval.json'),approved),'--approval-sha256',sha(approved),'--cold-public-key',await store(join(directory,'execute-cold-pub.pem'),coldPublicKey.export({type:'spki',format:'pem'})), + '--cold-spki-sha256',sha(coldPublicKey.export({type:'spki',format:'der'}))] + let publisherTransport,publisherSha + for(const [scope,user,password] of [['publisher',publisher,options.password],['operator',operator,appPassword],['control',control,controlPassword]]){ + const transport=sourceBindingBytes({host,port:options.port,database:options.database,user,password}) + const file=await store(join(directory,'execute-'+scope+'.json'),transport);args.push('--'+scope+'-transport',file,'--'+scope+'-transport-sha256',sha(transport)) + if(scope==='publisher'){publisherTransport=file;publisherSha=sha(transport)} + } + const {rows:[credential]}=await live.query('SELECT id,digest FROM public.mcp_credentials LIMIT 1') + await live.query('UPDATE public.mcp_credentials SET digest=$2 WHERE id=$1',[credential.id,randomUUID()]) + try{await assert.rejects(main(args),/Original credentials differ/)}finally{await live.query('UPDATE public.mcp_credentials SET digest=$2 WHERE id=$1',[credential.id,credential.digest])} + assert.equal((await live.query('SELECT operation_id FROM mmapp_control.database_restore_quarantine')).rows[0].operation_id,null) + const connect=pg.Pool.prototype.connect + let applicationCommits=0 + pg.Pool.prototype.connect=function(...args){ + const connection=connect.apply(this,args) + if(args.length||this.options.application_name!=='mmapp-legacy-handoff-owner')return connection + return connection.then(client=>{ + const query=client.query + client.query=function(text,...params){ + const result=query.call(this,text,...params) + if(text!=='COMMIT')return result + return result.then(()=>{applicationCommits++;throw Error('owned application COMMIT ACK lost')}) + } + return client + }) + } + try{await assert.rejects(main(args),{code:'LEGACY_HANDOFF_EXECUTION_UNKNOWN'})}finally{pg.Pool.prototype.connect=connect} + assert.equal(applicationCommits,1) + const intent=await readFile(join(archiveDirectory,'legacy-probe-handoff-intent.json')) + const unknown=JSON.parse(await readFile(join(archiveDirectory,'legacy-probe-handoff-application-unknown.json'),'utf8')) + assert.equal(unknown.operationId,operationId);assert.equal(unknown.automaticReplay,false) + const inspectArgs=['legacy-handoff-inspect',...shared,'--transport',publisherTransport,'--transport-sha256',publisherSha,'--handoff-intent-sha256',sha(intent)] + const inspected=await main(inspectArgs) + assert.equal(inspected.outcome,'committed');assert.equal(inspected.operationId,operationId);assert.equal(inspected.quarantineActive,true) + assert.equal((await live.query('SELECT operation_id FROM mmapp_control.database_restore_quarantine')).rows[0].operation_id,operationId) + await assert.rejects(main(args),{code:'DATABASE_RESTORE_QUARANTINE_UNAVAILABLE'}) + assert.equal(applicationCommits,1) + if(adopt){ + assert.equal(inspected.adoptionRecorded,false) + const adoptArgs=[...args];adoptArgs[0]='legacy-handoff-adopt' + adoptArgs.splice(adoptArgs.indexOf('--source-signing-key'),2,'--handoff-intent-sha256',sha(intent)) + let action=adoptArgs,unknownCode='LEGACY_HANDOFF_ADOPTION_UNKNOWN',releaseApprovalSha256 + if(release){ + const adopted=await main(adoptArgs) + assert.equal(adopted.quarantineActive,true) + if(runTail)await runTail(true) + const signedRelease=async(adoptionSha256,name)=>{ + const body={format:'legacy-probe-0037-release-approval-v1',decision:'release-migration-quarantine',operationId,intentSha256:sha(intent),adoptionSha256, + sourceHeadSha256:originalPin.currentHeadSha256,sourceTlsSpkiSha256:tls.spkiSha256,coldArchiveSha256:sha(fullArchive), + coldProofSha256:sha(proof),postSchemaSha256,manifestSha256,migrationSha256:pin.sha256,protectedRuntimePaused:true,expiresAt:Date.now()+90000} + const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_HANDOFF_RELEASE_DOMAIN+sourceBindingCanonical(body)),coldPrivateKey).toString('base64url')}) + return ['legacy-handoff-release',...adoptArgs.slice(1),'--release-approval',await store(join(directory,name),bytes),'--release-approval-sha256',sha(bytes)] + } + await assert.rejects(main(await signedRelease('0'.repeat(64),'wrong-release.json')),/Exact committed legacy handoff phase/) + assert.equal((await main(inspectArgs)).quarantineActive,true) + action=await signedRelease(adopted.observationSha256,'release.json') + releaseApprovalSha256=action.at(-1);unknownCode='LEGACY_HANDOFF_RELEASE_UNKNOWN' + } + let controlCommits=0 + pg.Pool.prototype.connect=function(...parameters){ + const connection=connect.apply(this,parameters) + if(parameters.length||this.options.application_name!=='mmapp-writer-registry-enroll')return connection + return connection.then(client=>{ + const query=client.query + client.query=function(text,...params){ + const result=query.call(this,text,...params) + if(text!=='COMMIT')return result + return result.then(ack=>{if(++controlCommits===2)throw Error('owned adoption COMMIT ACK lost');return ack}) + } + return client + }) + } + try{await assert.rejects(main(action),{code:unknownCode})}finally{pg.Pool.prototype.connect=connect} + assert.equal(controlCommits,2) + const readback=await main(inspectArgs) + assert.equal(readback.adoptionRecorded,true);assert.equal(readback.outcome,'committed');assert.equal(readback.readyAuthorized,false) + const {rows:[gate]}=await live.query('SELECT q.operation_id::text AS active,s.state FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1') + const {rows:history}=await live.query('SELECT id::text,intent,terminal FROM mmapp_control.database_schema_intents') + assert.equal(gate.active,release?null:operationId);assert.equal(gate.state.intent,null) + assert.equal(gate.state.terminal.intent.artifactSha256,sha(intent)) + assert.equal(await exactHistory(history,gate.state,manifestSha256,true),37) + if(release){ + assert.equal(readback.releaseRecorded,true);assert.equal(readback.quarantineActive,false);assert.equal(readback.releaseApprovalSha256,releaseApprovalSha256) + assert.equal(gate.state.terminal.legacyRelease.approvalSha256,releaseApprovalSha256) + const {rows:[head]}=await live.query('SELECT journal_prefix_bytes,journal_prefix_sha256 FROM mmapp_control.database_restore_quarantine') + assert.deepEqual(head,{journal_prefix_bytes:null,journal_prefix_sha256:null}) + const unknown=JSON.parse(await readFile(join(archiveDirectory,'legacy-probe-handoff-release-unknown.json'),'utf8')) + assert.deepEqual(unknown,{format:'legacy-probe-0037-release-unknown-v1',operationId,intentSha256:sha(intent),automaticReplay:false}) + await assert.rejects(main(action),/Legacy release attempt requires inspection/) + if(runTail){ + const markers=()=>live.query('SELECT id,xmin::text AS xmin,applied_at::text FROM public.schema_migrations ORDER BY id').then(result=>result.rows) + const ledger=()=>live.query('SELECT id::text,xmin::text AS xmin,intent,terminal FROM mmapp_control.database_schema_intents ORDER BY id').then(result=>result.rows) + const beforeMarkers=await markers(),beforeLedger=await ledger() + const result=await runTail(false,{operationId,intentSha256:sha(intent),releaseApprovalSha256}) + assert.deepEqual((await markers()).slice(0,37),beforeMarkers) + const afterLedger=new Map((await ledger()).map(row=>[row.id,row])) + assert(beforeLedger.every(row=>JSON.stringify(row)===JSON.stringify(afterLedger.get(row.id)))) + assert.equal((await main(inspectArgs)).releaseRecorded,true) + return {controlledLegacyTailCompleted:true,quarantinedDispatchRefused:true,originalReceiptsAndMarkersPreserved:true,...result} + } + return {legacyHandoffReleased:true,wrongAdoptionRefused:true,releaseAckLossObserved:true,exactAuthenticatedPrefix:37, + replayRefused:true,restoreJournalUnchanged:true,readyAuthorized:false} + } + const unknown=JSON.parse(await readFile(join(archiveDirectory,'legacy-probe-handoff-adoption-unknown.json'),'utf8')) + assert.deepEqual(unknown,{format:'legacy-probe-0037-adoption-unknown-v1',operationId,intentSha256:sha(intent),automaticReplay:false}) + await assert.rejects(main(adoptArgs),/Legacy adoption attempt requires inspection/) + return {legacyHandoffAdopted:true,adoptionAckLossObserved:true,exactAuthenticatedPrefix:37,replayRefused:true,quarantineCleared:false,readyAuthorized:false} + } + return {legacyHandoffExecuted:true,credentialDriftRefused:true,applicationAckLossObserved:true,committedReconciled:true,quarantineCleared:false,readyAuthorized:false} +} diff --git a/backend/scripts/legacy-probe-handoff-inspect-owned.mjs b/backend/scripts/legacy-probe-handoff-inspect-owned.mjs new file mode 100644 index 00000000..2fd4c08d --- /dev/null +++ b/backend/scripts/legacy-probe-handoff-inspect-owned.mjs @@ -0,0 +1,177 @@ +import assert from 'node:assert/strict' +import {randomUUID,sign,X509Certificate} from 'node:crypto' +import {mkdir,open,readFile,realpath,unlink,writeFile} from 'node:fs/promises' +import {dirname,join,sep} from 'node:path' + +export async function verifyOwnedHandoffInspector({live,directory,options,leafBytes,originalPin,archive,fullArchive,signedApproval,privateKey,intentOnly=false}){ + assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_0037,'true') + const [{acquireDatabaseSchemaControl},{observation},{MIGRATION_MANIFEST,migrationManifestDigest}, + {captureMigrationSchema},{ownedLegacyProbe0037Body},{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry}, + {LEGACY_PROBE_HANDOFF_DOMAIN},{createBootstrapMaterial},{LEGACY_PROBE_RELATIONS,LEGACY_PROBE_CUSTODY_RELATIONS},{DATABASE_RESTORE_QUARANTINE_DDL,beginDatabaseRestoreQuarantine}]=await Promise.all([ + import('../src/db/database-schema-control.ts'),import('../src/db/controlled-migration-history.ts'),import('../src/db/migration-manifest.ts'), + import('../src/db/migration-schema-inventory.ts'),import('../src/db/proposals/server-probe-legacy-0037-owned.ts'),import('../src/db/database-source-binding.ts'), + import('../src/services/server-probe-legacy-handoff-inspection.ts'),import('./database-writer-bootstrap-operator.mjs'), + import('../src/services/server-probe-legacy-source-inventory.ts'),import('../src/db/database-restore-quarantine.ts'), + ]) + const store=async(path,data)=>{await writeFile(path,data,{flag:'wx',mode:0o600});return path} + const {body:migrationBody,pin}=await ownedLegacyProbe0037Body(),manifestSha256=migrationManifestDigest() + const truncate=`TRUNCATE TABLE ${LEGACY_PROBE_RELATIONS.map(name=>`ONLY public.${name}`).join(', ')} CONTINUE IDENTITY RESTRICT` + const base=await live.connect() + let before,after + const schema=async sql=>{ + await sql.query("SET LOCAL search_path=pg_catalog; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'") + return (await captureMigrationSchema(sql,{scope:{applicationNamespaces:['public'],excludedControlNamespaces:['mmapp_control']},deadlineAt:Date.now()+30000})).sha256 + } + try { + await base.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');before=await schema(base);await base.query('ROLLBACK') + await base.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ WRITE') + await base.query(truncate);await base.query(migrationBody);await base.query('SET TRANSACTION READ ONLY');after=await schema(base) + } finally { try { await base.query('ROLLBACK') } finally { base.release() } } + assert.notEqual(before,after) + await live.query(DATABASE_RESTORE_QUARANTINE_DDL) + const control=await acquireDatabaseSchemaControl(live) + try { + // The guarded fixture already installed exactly these 36 files on the owned DB. + for(const migration of MIGRATION_MANIFEST.slice(0,36)){ + const id=randomUUID(), begun=await control.transition({type:'begin',id,kind:'migration',manifestSha256,artifactSha256:migration.sha256}) + await control.transition({type:'terminal',id,outcome:'observed-success',observationSha256:await observation(begun.state.intent,migration)}) + } + } finally { await control.close() } + const baseline=(await live.query('SELECT state FROM mmapp_control.database_schema_state WHERE id=1')).rows[0].state + const registry=await readDatabaseSourceBindingRegistry(originalPin),record=registry.body.source,host='localhost' + const tls={serverName:host,caSha256:sourceBindingSha256(leafBytes),spkiSha256:sourceBindingSha256(new X509Certificate(leafBytes).publicKey.export({type:'spki',format:'der'}))} + const names={publisher:options.user,runtime:'owned_handoff_runtime',operator:'owned_handoff_operator',control:'owned_handoff_control',capture:'owned_handoff_capture'} + await live.query(Object.values(names).filter(name=>name!==options.user).map(name=>`CREATE ROLE ${name} NOLOGIN`).join(';')) + const {rows:roles}=await live.query('SELECT rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1)',[Object.values(names)]) + const request={format:'application-writer-bootstrap-request-v1',installationId:originalPin.installationId,sourceHeadSha256:originalPin.currentHeadSha256,tls, + roles:Object.fromEntries(Object.entries(names).map(([scope,name])=>[scope,roles.find(role=>role.name===name)])), + source:{systemIdentifier:record.systemIdentifier,databaseOid:String(record.databaseOid),databaseName:record.databaseName,serverVersionNum:record.serverVersionNum,inRecovery:false}} + const boot=sourceBindingBytes(createBootstrapMaterial(request,sourceBindingSha256(sourceBindingBytes(request))).bootstrap) + const transport=sourceBindingBytes({host,port:options.port,user:options.user,password:options.password,database:options.database}) + const activationDirectory=join(directory,'owned-handoff-active');await mkdir(activationDirectory,{mode:0o700}) + const activation=sourceBindingBytes({format:'database-source-activation-v1',activationDirectory,installationId:originalPin.installationId, + sourceIdentityId:registry.body.sourceIdentityId,status:'active',publicKeySpkiSha256:originalPin.publicKeySpkiSha256, + generations:[{generation:registry.body.generation,headSha256:registry.headSha256,bindingSha256:registry.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]}) + await store(join(activationDirectory,'source-activation.json'),activation) + const argv=['legacy-handoff-inspect','--bootstrap',await store(join(directory,'owned-handoff-bootstrap.json'),boot),'--bootstrap-sha256',sourceBindingSha256(boot), + '--transport',await store(join(directory,'owned-handoff-transport.json'),transport),'--transport-sha256',sourceBindingSha256(transport), + '--ca',await store(join(directory,'owned-handoff-ca.pem'),leafBytes),'--source-registry',originalPin.registryDirectory,'--source-spki-sha256',originalPin.publicKeySpkiSha256, + '--activation-directory',activationDirectory,'--activation-sha256',sourceBindingSha256(activation)] + let modeledFiles + if(process.platform==='win32'){ + const {mock}=await import('node:test'),url=new URL('./writer-profile/operator-files.mjs',import.meta.url),files=await import(url.href) + const protectedDirectory=async path=>{assert(path===directory||path.startsWith(directory+sep));assert.equal(await realpath(path),path)} + modeledFiles=mock.module(url,{namedExports:{...files,protectedDirectory,async writeFile(path,blob,options){ + await protectedDirectory(dirname(path));const file=await open(path,options.flag,options.mode) + try{await file.writeFile(blob);await file.sync()}finally{await file.close()} + },async readProtected(path,limit,hash){ + await protectedDirectory(dirname(path));const bytes=await readFile(path);assert(bytes.length>0&&bytes.length<=limit);if(hash!==undefined)assert.equal(sourceBindingSha256(bytes),hash);return bytes + },async withJournalLease(path,work){ + await protectedDirectory(path);const lock=join(path,'publisher-lease.lock'),file=await open(lock,'wx',0o600) + try{return await work()}finally{await file.close();await unlink(lock)} + }}}) + } + try{ + const {main}=await import('./database-writer-profile-operator.mts') + const {publishLegacyHandoffIntent}=await import('./writer-profile/publisher.mjs') + if(intentOnly){ + const cases=['no-drain','control-refusal','acknowledged','lost-ack'] + for(const testCase of cases){ + const operationId=randomUUID(),archiveDirectory=join(directory,operationId);await mkdir(archiveDirectory,{mode:0o700}) + for(const [name,blob] of [['legacy-probe.dump',archive],['legacy-probe-approval.json',signedApproval],['legacy-probe-cold.dump',fullArchive]])await store(join(archiveDirectory,name),blob) + const application=await live.connect() + if(testCase==='control-refusal')await live.query(`CREATE FUNCTION mmapp_control.owned_refuse_quarantine() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'owned control refusal'; END $$; + CREATE TRIGGER owned_refuse_quarantine BEFORE UPDATE ON mmapp_control.database_restore_quarantine FOR EACH ROW EXECUTE FUNCTION mmapp_control.owned_refuse_quarantine()`) + let control,commits=0 + try{ + if(testCase!=='no-drain')for(const key of [8723130,8723102,8723110])await application.query('SELECT pg_advisory_lock($1::bigint)',[key]) + await application.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ WRITE') + if(testCase!=='no-drain')await application.query(`LOCK TABLE ${[...LEGACY_PROBE_CUSTODY_RELATIONS,'mcp_settings'].map(name=>`public.${name}`).join(', ')} IN ACCESS EXCLUSIVE MODE NOWAIT`) + const {rows:[{xid}]}=await application.query('SELECT pg_current_xact_id()::text AS xid') + const intent={format:'legacy-probe-0037-handoff-intent-v1',operationId,applicationXid:xid,approvalSha256:sourceBindingSha256(signedApproval), + coldArchiveSha256:sourceBindingSha256(fullArchive),preSchemaSha256:before,postSchemaSha256:after,manifestSha256,migrationSha256:pin.sha256} + const signedIntent=sourceBindingBytes({body:intent,signature:sign(null,Buffer.from(LEGACY_PROBE_HANDOFF_DOMAIN+sourceBindingCanonical(intent)),privateKey).toString('base64url')}) + control=await acquireDatabaseSchemaControl({async connect(){ + const client=await live.connect() + return {release:bad=>client.release(bad),async query(text,values){ + const result=await client.query(text,values) + if(text==='COMMIT'&&++commits===2&&testCase==='lost-ack')throw Error('owned control COMMIT ACK lost') + return result + }} + }}) + const publish=()=>publishLegacyHandoffIntent({application,control,sourcePin:originalPin,tlsPin:tls,archiveDirectory,signedIntent,deadlineAt:Date.now()+30000}) + const intentFile=join(archiveDirectory,'legacy-probe-handoff-intent.json'), unknownFile=join(archiveDirectory,'legacy-probe-handoff-control-unknown.json') + if(testCase==='acknowledged'){ + const receipt=await publish();assert.equal(receipt.intentRecorded,true);assert.equal(receipt.readyAuthorized,false) + }else if(testCase==='no-drain'){ + await assert.rejects(publish(),/exclusive drain no longer held/) + await assert.rejects(readFile(intentFile),{code:'ENOENT'}) + }else await assert.rejects(publish(),{outcomeUnknown:testCase==='lost-ack'}) + if(testCase!=='no-drain')assert.equal(sourceBindingSha256(await readFile(intentFile)),sourceBindingSha256(signedIntent)) + if(testCase==='lost-ack'){ + const observation=JSON.parse(await readFile(unknownFile,'utf8')) + assert.equal(observation.operationId,operationId);assert.equal(observation.automaticReplay,false) + await assert.rejects(publish(),{code:'EEXIST'}) + assert.equal(commits,2) + }else await assert.rejects(readFile(unknownFile),{code:'ENOENT'}) + const recorded=testCase==='acknowledged'||testCase==='lost-ack' + const {rows:[facts]}=await live.query(`SELECT q.operation_id,s.state->'intent'->>'id' AS pending, + (SELECT count(*)::int FROM mmapp_control.database_schema_intents) AS history + FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1`) + assert.deepEqual(facts,{operation_id:recorded?operationId:null,pending:recorded?operationId:null,history:recorded?37:36}) + await application.query('ROLLBACK') + await application.query('SELECT pg_advisory_unlock_all()') + if(recorded){ + const result=await main([...argv,'--archive-directory',archiveDirectory,'--handoff-intent-sha256',sourceBindingSha256(signedIntent)]) + assert.equal(result.outcome,'rolled-back');assert.equal(result.operationId,operationId);assert.equal(result.quarantineActive,true) + } + if(recorded&&testCase!=='lost-ack'){ + await live.query('DELETE FROM mmapp_control.database_schema_intents WHERE id=$1',[operationId]) + await live.query('UPDATE mmapp_control.database_schema_state SET state=$1::jsonb WHERE id=1',[JSON.stringify(baseline)]) + await live.query('UPDATE mmapp_control.database_restore_quarantine SET operation_id=NULL WHERE id=1') + } + }finally{ + try{await control?.close()}finally{application.release(true)} + if(testCase==='control-refusal')await live.query('DROP TRIGGER owned_refuse_quarantine ON mmapp_control.database_restore_quarantine; DROP FUNCTION mmapp_control.owned_refuse_quarantine()') + } + } + return {legacyHandoffIntentPublished:true,controlRefusalAtomic:true,controlAckLossRecordedUnknown:true,applicationCommit:false,readyAuthorized:false} + } + for(const commits of [false,true]){ + const operationId=randomUUID(),archiveDirectory=join(directory,operationId);await mkdir(archiveDirectory,{mode:0o700}) + for(const [name,blob] of [['legacy-probe.dump',archive],['legacy-probe-approval.json',signedApproval],['legacy-probe-cold.dump',fullArchive]])await store(join(archiveDirectory,name),blob) + const application=await live.connect() + try{ + await application.query('BEGIN');const {rows:[{xid}]}=await application.query('SELECT pg_current_xact_id()::text AS xid') + const body={format:'legacy-probe-0037-handoff-intent-v1',operationId,applicationXid:xid,approvalSha256:sourceBindingSha256(signedApproval), + coldArchiveSha256:sourceBindingSha256(fullArchive),preSchemaSha256:before,postSchemaSha256:after,manifestSha256,migrationSha256:pin.sha256} + const intent=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_PROBE_HANDOFF_DOMAIN+sourceBindingCanonical(body)),privateKey).toString('base64url')}) + const file=await open(join(archiveDirectory,'legacy-probe-handoff-intent.json'),'wx',0o600) + try { await file.writeFile(intent);await file.sync() } finally { await file.close() } + const input=[...argv,'--archive-directory',archiveDirectory,'--handoff-intent-sha256',sourceBindingSha256(intent)] + const controller=await acquireDatabaseSchemaControl(live) + try{await controller.transition({type:'begin',id:operationId,kind:'migration',manifestSha256,artifactSha256:sourceBindingSha256(intent)})}finally{await controller.close()} + await beginDatabaseRestoreQuarantine(live,operationId) + const inspect=async expected=>{ + const result=await main(input) + assert.deepEqual({outcome:result.outcome,operationId:result.operationId,quarantine:result.quarantineActive,ready:result.readyAuthorized}, + {outcome:expected,operationId,quarantine:true,ready:false}) + const {rows:[gates]}=await live.query(`SELECT q.operation_id,s.state->'intent'->>'id' AS pending + FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1`) + assert.deepEqual(gates,{operation_id:operationId,pending:operationId}) + } + await inspect('unknown') + if(commits){await application.query(truncate);await application.query(migrationBody)} + await application.query(commits?'COMMIT':'ROLLBACK') + await inspect(commits?'committed':'rolled-back') + if(!commits){ + // Own harness resets its control-only fixture for the independent COMMIT case. + await live.query('DELETE FROM mmapp_control.database_schema_intents WHERE id=$1',[operationId]) + await live.query('UPDATE mmapp_control.database_schema_state SET state=$1::jsonb WHERE id=1',[JSON.stringify(baseline)]) + await live.query('UPDATE mmapp_control.database_restore_quarantine SET operation_id=NULL WHERE id=1') + } + }finally{try{await application.query('ROLLBACK')}finally{application.release()}} + } + } finally { modeledFiles?.restore() } + return {legacyHandoffInspection:true,unknownObserved:true,rollbackObserved:true,commitObserved:true,quarantineCleared:false,readyAuthorized:false} +} diff --git a/backend/scripts/mcp-external-process/bridge-vlan-client.mjs b/backend/scripts/mcp-external-process/bridge-vlan-client.mjs new file mode 100644 index 00000000..50046272 --- /dev/null +++ b/backend/scripts/mcp-external-process/bridge-vlan-client.mjs @@ -0,0 +1,73 @@ +/** External SDK consumer. No application/store/runtime/database imports. */ +import assert from 'node:assert/strict' +import { randomUUID } from 'node:crypto' +import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client' + +const endpoint = new URL(process.env.OWN_MCP_ENDPOINT) +assert.equal(process.env.OWN_EXTERNAL_CLIENT, 'true') +assert.equal(endpoint.hostname, '127.0.0.1') +assert.notEqual(process.pid, Number(process.env.OWN_SERVER_PID)) +const client = new Client({ name: 'owned-linked-bridge-vlan-sdk', version: '1.0.0' }) +const boundedFetch = async (input, init) => { + const url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url) + assert.equal(url.origin, endpoint.origin) + return fetch(input, { ...init, redirect: 'manual', signal: AbortSignal.timeout(10000) }) +} +const payload = result => { + const text = result.content.find(item => item.type === 'text')?.text + assert(text && Buffer.byteLength(text) < 131072) + return JSON.parse(text) +} +const call = async (name, args) => { + const result = await client.callTool({ name, arguments: args }, undefined, { timeout: 10000 }) + assert(!result.isError, JSON.stringify(payload(result))) + return payload(result) +} +try { + await client.connect(new StreamableHTTPClientTransport(endpoint, { + requestInit: { headers: { authorization: 'Bearer ' + process.env.OWN_MCP_TOKEN } }, fetch: boundedFetch, + }), { timeout: 10000 }) + const tools = (await client.listTools({}, { timeout: 10000 })).tools + const read = tools.find(tool => tool.name === 'mm_bridge_vlan_configuration_get') + const configure = tools.find(tool => tool.name === 'mm_bridge_vlan_configuration_plan') + assert(read && configure) + assert.match(read.description, /Полная конфигурация bridge VLAN/) + assert.match(read.description, /Возвращает данные только из разрешённой области/) + assert.match(configure.description, /Связанный план VLAN membership, PVID и bridge filtering/) + assert.match(configure.description, /Подготавливает неизменяемый план/) + assert.equal(read.annotations.readOnlyHint, true) + assert.equal(configure.annotations.readOnlyHint, false) + assert.deepEqual(read.inputSchema.required, ['serverId']) + assert.deepEqual(configure.inputSchema.required, ['serverId', 'bridgeId', 'desired']) + const desiredSchema = configure.inputSchema.properties.desired + for (const name of ['ports', 'vlans', 'removePortIds', 'removeVlanIds']) assert(desiredSchema.properties[name]) + const serverId = Number(process.env.OWN_ALLOWED_SERVER) + const state = (await call(read.name, { serverId })).state + assert.equal(state.bridges[0]['.id'], '*1') + assert(state.interfaces.some(item => item.resource === 'ethernet' && item.id === '*3' && item.name === 'ether3')) + assert.equal(state.vlans[0]['vlan-ids'], '10') + const cpu = { resource: 'bridge', id: '*1' }, access = { resource: 'ethernet', id: '*2' }, trunk = { resource: 'ethernet', id: '*3' } + const plan = await call(configure.name, { serverId, bridgeId: '*1', desired: { + filtering: true, pvid: 1, + ports: [{ id: '*10', interface: access, pvid: 10, ingressFiltering: true, frameTypes: 'admit-all', disabled: false }, + { interface: trunk, pvid: 1, ingressFiltering: true, frameTypes: 'admit-only-vlan-tagged', disabled: false }], + vlans: [{ id: '*11', vlanIds: '10', tagged: [cpu], untagged: [access], disabled: false }, + { vlanIds: '20', tagged: [cpu, trunk], untagged: [], disabled: false }], removePortIds: [], removeVlanIds: [], + } }) + assert.equal(plan.state, 'awaiting-approval') + assert.equal(typeof plan.planId, 'string') + assert.equal(plan.changes.requiresHuman, true) + assert.deepEqual(plan.changes.steps.map(item => [item.step.resource, item.step.action]), + [['bridge_vlan', 'create'], ['bridge_port', 'create'], ['bridge', 'update']]) + const denied = await client.callTool({ name: 'mm_change_apply', arguments: { planId: plan.planId, idempotencyKey: randomUUID() } }, undefined, { timeout: 10000 }) + assert.equal(denied.isError, true) + const deniedPayload = payload(denied) + assert(JSON.stringify(deniedPayload).includes('APPROVAL_REQUIRED')) + const readback = (await call(read.name, { serverId })).state + assert.deepEqual(readback, state) + process.send({ kind: 'result', result: { externalSdkProcess: true, transport: 'streamable-http', clientPid: process.pid, + listedTools: [read, configure], state, plan, applyDenial: deniedPayload, unchangedReadback: true } }) +} finally { + await client.close() + process.disconnect?.() +} diff --git a/backend/scripts/mcp-external-process/bridge-vlan-fixture.ts b/backend/scripts/mcp-external-process/bridge-vlan-fixture.ts new file mode 100644 index 00000000..7e106061 --- /dev/null +++ b/backend/scripts/mcp-external-process/bridge-vlan-fixture.ts @@ -0,0 +1,84 @@ +import assert from 'node:assert/strict' +import { spawn } from 'node:child_process' +import { createServer } from 'node:http' +import { writeFile } from 'node:fs/promises' +import { fileURLToPath, pathToFileURL } from 'node:url' +import { isAbsolute, resolve } from 'node:path' + +assert.equal(process.env.OWN_EXTERNAL_PROCESS_PG, 'true') +assert.equal(process.env.NODE_ENV, 'test') +assert(isAbsolute(process.env.OWN_EXTERNAL_SOURCE_ROOT!)) +assert(isAbsolute(process.env.OWN_EXTERNAL_RESULT_FILE!)) +const database = new URL(process.env.DATABASE_URL!) +assert.equal(database.hostname, '127.0.0.1'); assert.equal(database.username, 'mcp_test') +assert.equal(database.pathname, '/postgres'); assert.notEqual(database.port, '5432') +const source = (path: string) => pathToFileURL(resolve(process.env.OWN_EXTERNAL_SOURCE_ROOT!, 'backend/src', path)).href +const rows: Record[]> = { + '/interface/ethernet': [{ '.id': '*2', name: 'ether2', disabled: 'false' }, { '.id': '*3', name: 'ether3', disabled: 'false' }, { '.id': '*4', name: 'ether-mgmt', disabled: 'false' }], + '/interface/bridge': [{ '.id': '*1', name: 'br-users', pvid: '1', 'vlan-filtering': 'false', 'frame-types': 'admit-all', 'ingress-filtering': 'true', mvrp: 'false', disabled: 'false' }], + '/interface/bridge/port': [{ '.id': '*10', bridge: 'br-users', interface: 'ether2', pvid: '10', 'ingress-filtering': 'true', 'frame-types': 'admit-all', disabled: 'false' }], + '/interface/bridge/vlan': [{ '.id': '*11', bridge: 'br-users', 'vlan-ids': '10', tagged: 'br-users', untagged: 'ether2', disabled: 'false', dynamic: 'false' }], + '/interface/vlan': [], '/interface/gre': [], '/interface/wireguard': [], '/interface/vxlan': [], + '/ip/address': [{ '.id': '*20', interface: 'ether-mgmt', address: '127.0.0.1/8', disabled: 'false', dynamic: 'false' }], '/ipv6/address': [], +} +const requests: string[] = [] +let writeAttempts = 0 +const router = createServer((req, res) => { + res.setHeader('content-type', 'application/json') + const path = decodeURIComponent(new URL(req.url!, 'http://127.0.0.1').pathname.replace(/^\/rest/, '')) + requests.push(req.method + ' ' + path) + if (req.method !== 'GET') { writeAttempts++; res.writeHead(405); res.end('{}'); return } + if (req.headers.authorization !== 'Basic ' + Buffer.from('fixture:fixture-only').toString('base64')) { res.writeHead(401); res.end('{}'); return } + if (!Object.hasOwn(rows, path)) { res.writeHead(404); res.end('{}'); return } + res.end(JSON.stringify(rows[path])) +}) +await new Promise(done => router.listen(0, '127.0.0.1', done)) +const { pool, closePool } = await import(source('db/index.js')) +const { applySqlMigrations } = await import(source('db/migrate.js')) +const { mcpConnectionCreateSchema } = await import('@mmapp/contracts/mcp') +const { createMcpConnection, updateMcpSettings } = await import(source('modules/mcp/store.js')) +const { buildApp } = await import(source('index.js')) +const actor = { id: 'owned-bridge-vlan-sdk-owner', email: 'bridge-sdk@example.invalid', name: 'Owned bridge VLAN SDK owner', apps: ['mm'], permissions: ['mm:settings:admin', 'mm:mcp:admin', 'mm:network:admin', 'mm:servers:admin'], expiresAt: Date.now() + 3600000, issuedAt: Date.now() - 1000 } +let app: Awaited> | undefined +let child: ReturnType | undefined +let childExit: Promise | undefined +try { + await applySqlMigrations(pool) + const serverId = (await pool.query("INSERT INTO servers(name,host,port,use_ssl,enabled,username,password) VALUES('owned bridge VLAN SDK','127.0.0.1',$1,false,true,'fixture','fixture-only') RETURNING id", [(router.address() as { port: number }).port])).rows[0].id + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false, logger: false }) + await app.listen({ host: '127.0.0.1', port: 0 }) + const endpoint = `http://127.0.0.1:${(app.server.address() as { port: number }).port}/mcp` + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: 'Owned bridge VLAN external SDK', mode: 'autonomous', + expiresAt: new Date(Date.now() + 1200000).toISOString(), permissions: ['mm:network:write'], + scope: { serverIds: [serverId], operationIds: ['network.bridge-vlan.read', 'network.bridge-vlan.configure', 'changes.apply'], allowGlobal: false }, + limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + let result: unknown, stdout = '', stderr = '' + child = spawn(process.execPath, [fileURLToPath(new URL('./bridge-vlan-client.mjs', import.meta.url))], { cwd: process.cwd(), windowsHide: true, + stdio: ['ignore', 'pipe', 'pipe', 'ipc'], env: { ...Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path'].filter(k => process.env[k]).map(k => [k, process.env[k]!])), + OWN_EXTERNAL_CLIENT: 'true', OWN_MCP_ENDPOINT: endpoint, OWN_MCP_TOKEN: issued.token, OWN_SERVER_PID: String(process.pid), OWN_ALLOWED_SERVER: String(serverId) } }) + child.on('message', (message: { kind: string; result?: unknown }) => { if (message.kind === 'result') result = message.result }) + child.stdout?.on('data', chunk => { stdout += String(chunk); if (stdout.length > 1048576) child?.kill() }) + child.stderr?.on('data', chunk => { stderr += String(chunk); if (stderr.length > 1048576) child?.kill() }) + childExit = new Promise((done, reject) => { child!.once('exit', code => done(code ?? 1)); child!.once('error', reject) }) + const timeout = setTimeout(() => child?.kill(), 60000) + const code = await childExit; clearTimeout(timeout) + assert.equal(code, 0, stderr.slice(0, 3000)); assert(result) + assert.equal(writeAttempts, 0) + assert.equal((await pool.query('SELECT count(*)::int AS count FROM mcp_operations')).rows[0].count, 0) + const plans = (await pool.query('SELECT operation_id,state,approved_by FROM mcp_plans')).rows + assert.equal(plans.length, 1); assert.equal(plans[0].operation_id, 'network.bridge-vlan.configure') + assert.equal(plans[0].state, 'awaiting-approval'); assert.equal(plans[0].approved_by, null) + const audit = (await pool.query('SELECT tool,status,error_code FROM mcp_requests ORDER BY created_at')).rows + assert(audit.some(row => row.error_code === 'APPROVAL_REQUIRED')) + const serialized = JSON.stringify({ result, stdout, stderr, audit }) + assert(!serialized.includes(issued.token)); assert(!serialized.includes('fixture-only')) + const final = { ...(result as object), actualOwnPg: true, actualProductionHttpServer: true, serverFixturePid: process.pid, + connectionMode: 'autonomous', plans, audit, nativeReadRequests: requests, nativeWriteAttempts: writeAttempts, + appliedOperations: 0, syntheticRouterOnly: true, productionDatabaseTouched: false, portalContacted: false } + await writeFile(process.env.OWN_EXTERNAL_RESULT_FILE!, JSON.stringify(final, null, 2)) + console.log('Owned bridge VLAN SDK passed:', JSON.stringify({ listedTools: 2, planState: plans[0].state, nativeReadRequests: requests.length, nativeWriteAttempts: writeAttempts })) +} finally { + if (child && child.exitCode === null && child.signalCode === null) { child.kill(); await childExit } + try { await app?.close() } finally { try { await closePool() } finally { router.closeIdleConnections(); await new Promise((done, reject) => router.close(error => error ? reject(error) : done())) } } +} diff --git a/backend/scripts/mcp-external-process/client.mjs b/backend/scripts/mcp-external-process/client.mjs new file mode 100644 index 00000000..f147c573 --- /dev/null +++ b/backend/scripts/mcp-external-process/client.mjs @@ -0,0 +1,412 @@ +/** External SDK consumer: imports NO server/store/runtime/database module. */ +import assert from 'node:assert/strict' +import { randomUUID, createHash } from 'node:crypto' +import { Client, StreamableHTTPClientTransport, auth } from '@modelcontextprotocol/client' +import { StdioClientTransport } from '@modelcontextprotocol/client/stdio' +const endpoint = new URL(process.env.OWN_MCP_ENDPOINT), origin = endpoint.origin +assert.equal(endpoint.hostname, '127.0.0.1'); assert.equal(process.env.OWN_EXTERNAL_CLIENT, 'true') +let phase = 'start', next = 0, maxResponseBytes = 0, stderrBytes = 0, bridgePid = null +const responseLimitBytes = 1024 * 1024 +const pending = new Map(), cases = [] +const activeClients = new Set() +let closing = false +async function closeClients() { await Promise.allSettled([...activeClients].map(client => client.close())) } +async function interrupted() { if (closing) return; closing = true; await closeClients(); process.disconnect?.(); process.exitCode = 1 } +process.on('SIGTERM', interrupted); process.on('SIGINT', interrupted); process.on('disconnect', interrupted) +process.on('message', m => { if (m?.kind === 'reply') { const p = pending.get(m.id); pending.delete(m.id); if (m.error) p?.reject(Error('Fixture control refused')); else p?.resolve(m.value) } }) +const control = command => new Promise((resolve, reject) => { const id = ++next; pending.set(id, { resolve, reject }); process.send({ kind: 'control', command, id }) }) +async function boundedFetch(input, init) { + const url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url) + assert.equal(url.origin, origin, 'External fixture refuses all nonlocal origins') + const response = await fetch(input, { ...init, redirect: 'manual', signal: init?.signal ? AbortSignal.any([init.signal, AbortSignal.timeout(5000)]) : AbortSignal.timeout(5000) }) + const chunks = []; let count = 0 + if (response.body) { + const reader = response.body.getReader() + try { + for (;;) { const item = await reader.read(); if (item.done) break; count += item.value.length; if (count >= responseLimitBytes) { await reader.cancel(); throw Error('Own external client response bound') }; chunks.push(item.value) } + } finally { reader.releaseLock() } + } + const bytes = Buffer.concat(chunks); maxResponseBytes = Math.max(maxResponseBytes, bytes.length) + return new Response(bytes.length ? bytes : null, { status: response.status, statusText: response.statusText, headers: response.headers }) +} +function content(result) { + assert(!result.isError) + const text = result.content.find(c => c.type === 'text')?.text + const parsed = JSON.parse(text) + assert(!text.includes(process.env.OWN_SERVER_SECRET)); assert(Buffer.byteLength(text) < 65536) + return parsed +} +async function denied(work) { try { const r = await work(); assert.equal(r?.isError, true) } catch (e) { if (e?.code === 'ERR_ASSERTION') throw e; assert(!/timeout|timed out/i.test(String(e?.message)), 'A timeout is not authorization denial') } } +async function opened(mode, token, maxBufferSize = 131072) { + const client = new Client({ name: 'actual-external-process-' + mode, version: '1.0.0' }) + activeClients.add(client) + let transport + if (mode === 'http') transport = new StreamableHTTPClientTransport(endpoint, { requestInit: { headers: { authorization: 'Bearer ' + token } }, fetch: boundedFetch }) + else { + transport = new StdioClientTransport({ command: process.execPath, args: ['--import', import.meta.resolve('tsx'), process.env.OWN_BRIDGE_FILE], + cwd: process.cwd(), env: { MCP_URL: endpoint.href, MCP_TOKEN: token }, stderr: 'pipe', maxBufferSize }) + transport.stderr?.on('data', chunk => { stderrBytes += chunk.length; const text = String(chunk); assert(!text.includes(token)); assert(stderrBytes < 1048576) }) + const start = transport.start.bind(transport) + transport.start = async () => { await start(); bridgePid = transport.pid; assert(bridgePid && bridgePid !== process.pid && bridgePid !== Number(process.env.OWN_SERVER_PID)); process.send({ kind: 'bridge-pid', pid: bridgePid }) } + } + const connecting = client.connect(transport, { timeout: 10000 }) + try { await connecting; return client } + catch (e) { await client.close().catch(() => {}); throw e } +} +async function scoped(client) { + const list = await client.listTools({}, { timeout: 5000 }) + assert.deepEqual(list.tools.map(t => t.name), ['mm_servers_list']) + const result = content(await client.callTool({ name: 'mm_servers_list', arguments: { limit: 1, offset: 0 } }, undefined, { timeout: 5000 })) + assert.equal(result.items.length, 1); assert.equal(result.items[0].id, Number(process.env.OWN_ALLOWED_SERVER)) + await denied(() => client.callTool({ name: 'mm_servers_list', arguments: { serverId: Number(process.env.OWN_FORBIDDEN_SERVER) } }, undefined, { timeout: 5000 })) + await denied(() => client.callTool({ name: 'mm_servers_list', arguments: { limit: 501 } }, undefined, { timeout: 5000 })) + await denied(() => client.callTool({ name: 'mm_servers_delete_plan', arguments: { serverId: Number(process.env.OWN_ALLOWED_SERVER) } }, undefined, { timeout: 5000 })) +} +try { + assert.notEqual(process.pid, Number(process.env.OWN_SERVER_PID)) + phase = 'http:owner-logs-lifecycle' + { + const headers = { authorization: 'Bearer ' + process.env.OWN_PORTAL_JWT, 'content-type': 'application/json' } + const allowedServer = Number(process.env.OWN_ALLOWED_SERVER), forbiddenServer = Number(process.env.OWN_FORBIDDEN_SERVER) + const issuedResponse = await boundedFetch(origin + '/api/mcp/connections', { + method: 'POST', headers, + body: JSON.stringify({ name: 'Owned portal log reader', mode: 'observer', expiresAt: new Date(Date.now() + 600000).toISOString(), + permissions: ['mm:servers:read'], scope: { serverIds: [allowedServer], operationIds: ['logs.query'], allowGlobal: false } }), + }) + assert.equal(issuedResponse.status, 200) + const { connection, token } = await issuedResponse.json() + const client = await opened('http', token) + const queryLogs = serverId => client.callTool({ + name: 'mm_logs_query', arguments: { serverId, q: 'external-sdk-log-', limit: 1 }, + }, undefined, { timeout: 5000 }) + try { + assert.deepEqual((await client.listTools({}, { timeout: 5000 })).tools.map(tool => tool.name), ['mm_logs_query']) + const result = content(await queryLogs(allowedServer)) + assert.deepEqual(result.entries.map(entry => entry.serverId), [allowedServer]) + await denied(() => queryLogs(forbiddenServer)) + const monitor = await boundedFetch(origin + '/api/mcp/requests?' + new URLSearchParams({ + connectionId: connection.id, tool: 'mm_logs_query', + }), { headers }) + assert.equal(monitor.status, 200) + const body = await monitor.text() + assert(!body.includes(token)) + const { items } = JSON.parse(body) + assert.equal(items.length, 2) + assert(items.every(row => row.connection_id === connection.id && row.tool === 'mm_logs_query')) + assert(items.some(row => row.status === 'denied')) + assert(items.some(row => row.error_code === null && row.finished_at && row.result?.entries?.[0]?.serverId === allowedServer)) + + const revoke = await boundedFetch(origin + '/api/mcp/connections/' + connection.id + '/status', { + method: 'POST', headers, body: JSON.stringify({ status: 'revoked' }), + }) + assert.equal(revoke.status, 200) + await denied(() => queryLogs(allowedServer)) + await assert.rejects(opened('http', token)) + cases.push('http: owner API issues restricted log access, monitors allow/deny and revokes the same SDK connection') + } finally { await client.close().catch(() => {}) } + } + for (const mode of ['http', 'stdio']) { + phase = mode + ':issue'; const initial = await control('new-connection'); let token = initial.token + phase = mode + ':initialize'; let client = await opened(mode, token) + try { + await scoped(client); cases.push(mode + ': separate SDK process initializes, filters tool/host scope, bounds page/result and redacts credentials') + phase = mode + ':policy-edit' + const editScope = async operationIds => { + const response = await boundedFetch(origin + '/api/mcp/connections/' + initial.connectionId, { + method: 'PUT', headers: { authorization: 'Bearer ' + process.env.OWN_PORTAL_JWT, 'content-type': 'application/json' }, + body: JSON.stringify({ name: 'Owned external SDK process', mode: 'observer', expiresAt: new Date(Date.now() + 600000).toISOString(), + permissions: ['mm:servers:read'], scope: { serverIds: [Number(process.env.OWN_ALLOWED_SERVER)], operationIds, allowGlobal: false }, limits: { callsPerMinute: 200 } }), + }) + assert.equal(response.status, 200) + } + await editScope(['logs.query']) + assert.deepEqual((await client.listTools({}, { timeout: 5000 })).tools.map(tool => tool.name), ['mm_logs_query']) + const allowedServer = Number(process.env.OWN_ALLOWED_SERVER), forbiddenServer = Number(process.env.OWN_FORBIDDEN_SERVER) + const queryLogs = arguments_ => client.callTool({ name: 'mm_logs_query', arguments: arguments_ }, undefined, { timeout: 5000 }) + const filters = { q: 'external-sdk-log-', limit: 1 } + const first = content(await queryLogs(filters)) + assert.ok(first.nextCursor) + const second = content(await queryLogs({ ...filters, cursor: first.nextCursor })) + assert.equal(second.nextCursor, null) + assert.deepEqual([first.entries, second.entries].map(entries => entries.map(({ serverId, message }) => ({ serverId, message }))), [ + [{ serverId: allowedServer, message: 'external-sdk-log-1' }], + [{ serverId: allowedServer, message: 'external-sdk-log-2' }], + ]) + await denied(() => queryLogs({ ...filters, serverId: forbiddenServer })) + cases.push(mode + ': populated log search pages return only allowed server records and reject the forbidden server') + await denied(() => client.callTool({ name: 'mm_servers_list', arguments: {} }, { timeout: 5000 })) + await editScope([]) + assert.deepEqual((await client.listTools({}, { timeout: 5000 })).tools, []) + await editScope(['servers.list']) + await scoped(client) + cases.push(mode + ': established client observes removed and restored tool scope through the web policy API') + if (mode === 'stdio' && process.env.OWN_EXTERNAL_CLEANUP_FAULT === 'failure') throw Error('Owned deliberate external client failure with bridge active') + if (mode === 'stdio' && process.env.OWN_EXTERNAL_CLEANUP_FAULT === 'timeout') { process.send({ kind: 'arm-cleanup-timeout' }); await new Promise(() => {}) } + phase = mode + ':result-bound'; await control('large-result') + try { + const bounded = content(await client.callTool({ name: 'mm_servers_list', arguments: { limit: 1 } }, undefined, { timeout: 5000 })) + assert.equal(bounded.items[0].comment.length, 16000); assert(!bounded.items[0].comment.includes('OWNED_TRUNCATION_TAIL')) + cases.push(mode + ': actual production public-string16k bound truncates owned40k comment through real SDK result') + } finally { await control('reset-result') } + phase = mode + ':rotate'; const rotated = await control('rotate'); await denied(() => client.callTool({ name: 'mm_servers_list', arguments: {} }, undefined, { timeout: 5000 })) + await client.close(); await assert.rejects(opened(mode, token)); token = rotated.token; client = await opened(mode, token) + content(await client.callTool({ name: 'mm_servers_list', arguments: {} }, undefined, { timeout: 5000 })); cases.push(mode + ': old credential and reconnect refused after rotation; fresh transport reconnect succeeds') + phase = mode + ':pause'; await control('pause'); await denied(() => client.callTool({ name: 'mm_servers_list', arguments: {} }, undefined, { timeout: 5000 })) + await control('resume'); content(await client.callTool({ name: 'mm_servers_list', arguments: {} }, undefined, { timeout: 5000 })); cases.push(mode + ': established client observes pause/resume on real requests') + phase = mode + ':revoke'; await control('revoke'); await denied(() => client.callTool({ name: 'mm_servers_list', arguments: {} }, undefined, { timeout: 5000 })) + await client.close(); await assert.rejects(opened(mode, token)); cases.push(mode + ': permanent revocation blocks established and newly initialized external clients') + } finally { await client.close().catch(() => {}) } + } + phase = 'http:body-bound'; const bound = await control('new-connection') + // Exercise the declared length check without racing the rejected upload. + const { request } = await import('node:http') + const oversized = await new Promise((resolve, reject) => { + const req = request(endpoint, { method: 'POST', headers: { authorization: 'Bearer ' + bound.token, + 'content-type': 'application/json', accept: 'application/json,text/event-stream', connection: 'close', + 'content-length': '1048577' }, signal: AbortSignal.timeout(5000) }, response => { + let bytes = 0 + response.on('data', chunk => { + bytes += chunk.length + if (bytes >= 65536) req.destroy(Error('Oversized refusal response')) + }) + response.once('error', reject) + response.once('end', () => resolve(response.statusCode)) + }) + req.once('error', reject) + req.flushHeaders() + }) + assert.equal(oversized, 413) + cases.push('actual HTTP request envelope over1MiB refused before tool execution; successful client results remain bounded') + + phase = 'sdk:pkce'; const grant = await control('new-connection') + const metadata = { client_name: 'owned-external-sdk-pkce', redirect_uris: [origin + '/owned-callback'], grant_types: ['authorization_code', 'refresh_token'], response_types: ['code'], token_endpoint_auth_method: 'none', scope: 'mcp' } + const registered = await boundedFetch(origin + '/api/mcp/oauth/clients', { method: 'POST', headers: { authorization: 'Bearer ' + process.env.OWN_PORTAL_JWT, 'content-type': 'application/json' }, body: JSON.stringify({ name: metadata.client_name, redirectUris: metadata.redirect_uris }) }) + assert.equal(registered.status, 200); const clientId = (await registered.json()).clientId + let tokens, verifier, authorizationCode, authorizationIssuer, discovery, consentAllowed = true + const state = randomUUID() + const provider = { redirectUrl: metadata.redirect_uris[0], clientMetadata: metadata, clientInformation: () => ({ client_id: clientId, token_endpoint_auth_method: 'none' }), + tokens: () => tokens, saveTokens: v => { tokens = v }, saveCodeVerifier: v => { verifier = v }, codeVerifier: () => verifier, state: () => state, + discoveryState: () => discovery, saveDiscoveryState: v => { discovery = v }, invalidateCredentials: scope => { if (scope === 'tokens') tokens = undefined }, + redirectToAuthorization: async url => { + if (!consentAllowed) throw Error('Synthetic consent disabled after revocation') + assert.equal(url.origin, origin); assert.equal(url.searchParams.get('code_challenge_method'), 'S256') + assert.equal(url.searchParams.get('code_challenge'), createHash('sha256').update(verifier).digest('base64url')) + const response = await boundedFetch(url); assert.equal(response.status, 302) + const requestId = new URL(response.headers.get('location')).searchParams.get('oauthRequest'); assert(requestId) + const consent = await boundedFetch(origin + '/api/mcp/oauth/consent', { method: 'POST', headers: { authorization: 'Bearer ' + process.env.OWN_PORTAL_JWT, 'content-type': 'application/json' }, body: JSON.stringify({ requestId, connectionId: grant.connectionId, approve: true }) }) + assert.equal(consent.status, 200); const callback = new URL((await consent.json()).redirectUrl) + assert.equal(callback.searchParams.get('state'), state); authorizationCode = callback.searchParams.get('code'); authorizationIssuer = callback.searchParams.get('iss') + } } + phase = 'sdk:pkce-redirect'; assert.equal(await auth(provider, { serverUrl: endpoint, scope: 'mcp', fetchFn: boundedFetch }), 'REDIRECT') + assert(authorizationCode && verifier); assert.equal(authorizationIssuer, origin) + phase = 'sdk:pkce-exchange'; assert.equal(await auth(provider, { serverUrl: endpoint, authorizationCode, iss: authorizationIssuer, fetchFn: boundedFetch }), 'AUTHORIZED') + assert(tokens?.access_token && tokens?.refresh_token) + const oauthClient = new Client({ name: 'external-sdk-real-pkce-mock-portal', version: '1.0.0' }) + activeClients.add(oauthClient) + try { + phase = 'sdk:pkce-initialize'; await oauthClient.connect(new StreamableHTTPClientTransport(endpoint, { authProvider: provider, fetch: boundedFetch }), { timeout: 10000 }) + content(await oauthClient.callTool({ name: 'mm_servers_list', arguments: {} }, undefined, { timeout: 5000 })) + cases.push('installed SDK auth() discovers actual OAuth metadata, produces PKCE/S256, exchanges one-use code and calls scoped tool; portal JWT/consent is synthetic') + phase = 'sdk:pkce-revoke'; const access = tokens.access_token, refresh = tokens.refresh_token + const revoked = await boundedFetch(origin + '/oauth/revoke', { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ token: refresh, client_id: clientId }).toString() }); assert.equal(revoked.status, 200) + consentAllowed = false; await assert.rejects(opened('http', access)) + await denied(() => oauthClient.callTool({ name: 'mm_servers_list', arguments: {} }, undefined, { timeout: 5000 })) + cases.push('actual OAuth family revocation refuses established and fresh external transport; no implicit new consent granted') + } finally { await oauthClient.close().catch(() => {}) } + phase = 'discovery:issue' + const catalogue = await control('discovery-connection') + const expectedNames = catalogue.names.toSorted() + assert(expectedNames.includes('mm_logs_follow')) + const discoveryByTransport = {} + for (const mode of ['http', 'stdio']) { + phase = `discovery:${mode}` + const client = await opened(mode, catalogue.token, responseLimitBytes) + try { + const names = [], cursors = new Set() + let cursor, largestPageBytes = 0, pages = 0 + do { + const page = await client.listTools(cursor ? { cursor } : {}, { timeout: 10000 }) + assert(page.tools.length > 0) + for (const tool of page.tools) { + assert.equal(tool.inputSchema.type, 'object') + names.push(tool.name) + } + const encoded = JSON.stringify(page) + assert(!encoded.includes(catalogue.token) && !encoded.includes(process.env.OWN_SERVER_SECRET)) + largestPageBytes = Math.max(largestPageBytes, Buffer.byteLength(encoded)) + assert(largestPageBytes < responseLimitBytes) + pages++ + cursor = page.nextCursor + if (cursor) { assert(!cursors.has(cursor)); cursors.add(cursor) } + assert(names.length <= expectedNames.length) + } while (cursor) + assert.deepEqual(names.toSorted(), expectedNames) + discoveryByTransport[mode] = { tools: names.length, pages, largestPageBytes } + cases.push(mode + ': external SDK discovers every delegated tool exactly once with an object input schema') + } finally { await client.close().catch(() => {}) } + } + phase = 'discovery:revoke' + await control('revoke') + for (const [mode, arm] of [['http', 'arm-log-revocation'], ['stdio', 'arm-log-revocation-after-commit'], ['http', 'arm-log-follow-revocation']]) { + phase = mode + ':' + arm + const { token } = await control(arm) + const logClient = await opened(mode, token) + try { + const follow = arm === 'arm-log-follow-revocation' + const tool = follow ? 'mm_logs_follow' : 'mm_logs_query' + const filters = { q: follow ? 'external-sdk-live-event' : 'external-sdk-log-1', limit: 1 } + if (follow) { + Object.assign(filters, { serverId: Number(process.env.OWN_ALLOWED_SERVER), waitSeconds: 0 }) + const start = content(await logClient.callTool({ name: tool, arguments: filters }, undefined, { timeout: 5000 })) + assert.deepEqual(start.entries, []) + assert.equal(typeof start.cursor, 'string') + filters.cursor = start.cursor + filters.waitSeconds = 10 + } + const query = logClient.callTool({ name: tool, arguments: filters }, undefined, { timeout: 10000 }) + const checked = query.then(result => { + assert.equal(result.isError, true) + const wire = JSON.stringify(result) + assert.match(wire, /OWNER_CLAIMS_REVOKED|UNAUTHORIZED/) + assert.doesNotMatch(wire, /external-sdk-log-1|external-sdk-live-event|"entries"|"nextCursor"|"cursor"/) + }) + const deliver = async () => { + if (follow) await control('publish-follow-fixture-row') + await control('deliver-log-revocation') + } + await Promise.all([checked, deliver()]) + await assert.rejects(opened(mode, token)) + cases.push(mode + ': signed portal revocation ' + (mode === 'stdio' ? 'after' : 'before') + ' audit commit denies ' + tool + ' result and reconnect') + } finally { await logClient.close().catch(() => {}) } + } + phase = 'portal:revoke-export-download' + const exportAccess = await control('new-artifact-connection') + const exportClient = await opened('http', exportAccess.token) + try { + const exported = content(await exportClient.callTool({ name: 'mm_logs_export', arguments: { + serverId: Number(process.env.OWN_ALLOWED_SERVER), q: 'external-sdk-log-1', format: 'json', + } }, undefined, { timeout: 5000 })) + assert.equal(exported.rows, 1) + assert.equal(exported.downloadPath, '/api/mcp/artifacts/' + exported.artifactId) + assert.doesNotMatch(JSON.stringify(exported), /external-sdk-log-1/) + const download = jwt => boundedFetch(origin + exported.downloadPath, { headers: { authorization: 'Bearer ' + jwt } }) + const before = await download(exportAccess.ownerJwt) + assert.equal(before.status, 200) + assert.equal(before.headers.get('cache-control'), 'private, no-store') + const bytes = Buffer.from(await before.arrayBuffer()) + assert.equal(createHash('sha256').update(bytes).digest('hex'), exported.sha256) + assert.match(bytes.toString('utf8'), /external-sdk-log-1/) + const fresh = await control('revoke-artifact-owner') + for (const jwt of [exportAccess.ownerJwt, fresh.ownerJwt]) { + const refused = await download(jwt) + assert.ok([401, 403].includes(refused.status)) + assert.equal(refused.headers.has('content-disposition'), false) + assert.doesNotMatch(await refused.text(), /external-sdk-log-1/) + } + const management = await boundedFetch(origin + '/api/mcp/connections', { headers: { authorization: 'Bearer ' + fresh.ownerJwt } }) + assert.equal(management.status, 200) + cases.push('portal owner revocation blocks an existing unexpired log artifact even with refreshed owner login') + } finally { await exportClient.close().catch(() => {}) } + phase = 'portal:approved-plan-apply' + { + const headers = { authorization: 'Bearer ' + process.env.OWN_PORTAL_JWT, 'content-type': 'application/json' } + const settings = async () => { + const response = await boundedFetch(origin + '/api/logs/settings', { headers }) + assert.equal(response.status, 200) + return response.json() + } + const before = await settings() + const intervalSec = before.intervalSec === 60 ? 61 : 60 + const issued = await boundedFetch(origin + '/api/mcp/connections', { + method: 'POST', headers, + body: JSON.stringify({ name: 'Owned portal log settings approval', mode: 'approval', + expiresAt: new Date(Date.now() + 600000).toISOString(), permissions: ['mm:settings:admin'], + scope: { serverIds: [Number(process.env.OWN_ALLOWED_SERVER), Number(process.env.OWN_FORBIDDEN_SERVER)], + operationIds: ['logs.settings.update', 'changes.apply', 'operations.read'], allowGlobal: true } }), + }) + assert.equal(issued.status, 200) + const { connection, token } = await issued.json() + const client = await opened('http', token) + try { + phase = 'portal:read-approved-plan-preview' + const call = (name, arguments_) => client.callTool({ name, arguments: arguments_ }, undefined, { timeout: 5000 }) + const plan = content(await call('mm_logs_settings_update_plan', { settings: { intervalSec } })) + assert.equal(plan.state, 'awaiting-approval') + const planUrl = origin + '/api/mcp/plans/' + plan.planId + const review = await boundedFetch(planUrl, { headers }) + assert.equal(review.status, 200) + const preview = await review.json() + assert.equal(preview.id, plan.planId) + assert.equal(preview.connectionId, connection.id) + assert.equal(preview.operationId, 'logs.settings.update') + assert.equal(preview.state, 'awaiting-approval') + assert.deepEqual(preview.input.settings, { intervalSec }) + phase = 'portal:refuse-unapproved-plan' + const idempotencyKey = randomUUID() + const apply = () => call('mm_change_apply', { planId: plan.planId, idempotencyKey }) + await denied(apply) + assert.deepEqual(await settings(), before) + phase = 'portal:approve-exact-plan' + const decision = await boundedFetch(planUrl + '/decision', { + method: 'POST', headers, body: JSON.stringify({ approve: true }), + }) + assert.equal(decision.status, 200) + phase = 'portal:apply-and-read-settings' + const operation = content(await apply()) + assert.match(operation.id, /^[a-f0-9-]{36}$/) + let terminal + const deadline = Date.now() + 10000 + do { + terminal = content(await call('mm_operation_get', { operationId: operation.id })) + if (!['pending', 'running'].includes(terminal.state)) break + await new Promise(resolve => setTimeout(resolve, 100)) + } while (Date.now() < deadline) + assert.equal(terminal.state, 'applied') + assert.equal(terminal.verification, 'passed') + assert.deepEqual(await settings(), { ...before, intervalSec }) + assert.equal(content(await apply()).id, operation.id) + + phase = 'portal:monitor-applied-plan' + const list = await boundedFetch(origin + '/api/mcp/operations?' + new URLSearchParams({ connectionId: connection.id }), { headers }) + assert.equal(list.status, 200) + const { items } = await list.json() + assert.deepEqual(items.map(({ id, plan_id, state, verification }) => ({ id, plan_id, state, verification })), [ + { id: operation.id, plan_id: plan.planId, state: 'applied', verification: 'passed' }, + ]) + phase = 'portal:revoke-after-apply' + const revoked = await boundedFetch(origin + '/api/mcp/connections/' + connection.id + '/status', { + method: 'POST', headers, body: JSON.stringify({ status: 'revoked' }), + }) + assert.equal(revoked.status, 200) + await denied(apply) + await assert.rejects(opened('http', token)) + cases.push('portal API approves the exact log settings plan; SDK applies once and owner sees the verified operation') + } finally { await client.close().catch(() => {}) } + } + phase = 'portal:revoke-approved-plan' + const planAccess = await control('new-revocable-plan-connection') + const planClient = await opened('http', planAccess.token) + try { + const plan = content(await planClient.callTool({ name: 'mm_logs_settings_update_plan', arguments: { + settings: { enabled: false }, + } }, undefined, { timeout: 5000 })) + assert.equal(plan.state, 'awaiting-approval') + const approved = await boundedFetch(origin + '/api/mcp/plans/' + plan.planId + '/decision', { + method: 'POST', headers: { authorization: 'Bearer ' + planAccess.ownerJwt, 'content-type': 'application/json' }, + body: JSON.stringify({ approve: true }), + }) + assert.equal(approved.status, 200) + assert.deepEqual(await approved.json(), { ok: true }) + await control('revoke-approved-plan-owner') + await denied(() => planClient.callTool({ name: 'mm_change_apply', arguments: { + planId: plan.planId, idempotencyKey: randomUUID(), + } }, undefined, { timeout: 5000 })) + cases.push('signed portal revocation invalidates the exact approved log settings plan before apply') + } finally { await planClient.close().catch(() => {}) } + process.send({ kind: 'result', result: { cases: cases.length, names: cases, externalClientPid: process.pid, serverPid: Number(process.env.OWN_SERVER_PID), bridgePid, + sdkOnlyExternalClient: true, maxObservedHttpResponseBytes: maxResponseBytes, clientResponseLimitBytes: responseLimitBytes, stdioSdkBufferLimitBytes: 131072, + discoveryByTransport, discoveryStdioSdkBufferLimitBytes: responseLimitBytes, + actualPublicStringLimitObserved: 16000, wholeGenericResultEnvelopeLimitAccepted: false, + syntheticPortalJwtAndConsent: true, liveAuthPortal: false, livePortalSender: false, endUserUi: false, wholeExternalDirectionComplete: false } }) +} catch (error) { process.send?.({ kind: 'failure', phase, errorType: error?.name ?? 'Error', completedCases: cases }); process.exitCode = 1 } +finally { closing = true; await closeClients(); process.disconnect?.() } diff --git a/backend/scripts/mcp-external-process/logs-read-client.mjs b/backend/scripts/mcp-external-process/logs-read-client.mjs new file mode 100644 index 00000000..741d29d0 --- /dev/null +++ b/backend/scripts/mcp-external-process/logs-read-client.mjs @@ -0,0 +1,205 @@ +import assert from 'node:assert/strict' +import { createHash } from 'node:crypto' +import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client' +import { resumeMcpSteps } from '../acceptance/mcp-step-journal.mjs' + +async function withClient(endpoint, token, run) { + const client = new Client({ name: 'owned-logs-sdk', version: '1.0.0' }) + try { + await client.connect(new StreamableHTTPClientTransport(endpoint, { + requestInit: { headers: { authorization: 'Bearer ' + token } }, + }), { timeout: 5000 }) + return await run(async (name, args = {}) => { + const reply = await client.callTool({ name, arguments: args }, undefined, { timeout: 5000 }) + assert(!reply.isError, name + ' failed') + return reply.structuredContent.result + }) + } finally { + await client.close() + } +} + +export async function verifyLogsHistory(endpoint, token, serverId, entries, journal, fixture, appendEvent) { + assert.equal(endpoint.hostname, '127.0.0.1') + const [newest, oldest] = entries + const filters = { serverId, q: 'external-sdk-log-', levels: ['info'] } + const config = { journal, fixture, steps: [ + { id: 'mm_logs_query', async check(call) { + const first = await call('mm_logs_query', { ...filters, limit: 1 }) + assert.deepEqual(first.entries.map(entry => entry.id), [newest.id]) + assert.ok(first.nextCursor) + const next = await call('mm_logs_query', { ...filters, limit: 1, cursor: first.nextCursor }) + assert.deepEqual(next.entries.map(entry => entry.id), [oldest.id]) + assert.equal(next.nextCursor, null) + } }, + { id: 'mm_logs_summary_get', async check(call) { + const summary = await call('mm_logs_summary_get', filters) + assert.equal(summary.total, 2) + assert.equal(summary.levels.info, 2) + } }, + { id: 'mm_logs_get', async check(call) { + const entry = await call('mm_logs_get', { serverId, id: newest.id }) + assert.equal(entry.id, newest.id) + assert.equal(entry.serverId, serverId) + assert.equal(entry.message, newest.message) + } }, + { id: 'mm_logs_context_get', async check(call) { + const context = await call('mm_logs_context_get', { serverId, id: newest.id }) + assert.equal(context.entry.id, newest.id) + assert.deepEqual(context.before.map(entry => [entry.serverId, entry.id]), [[serverId, oldest.id]]) + assert.deepEqual(context.after, []) + } }, + { id: 'mm_logs_follow', async check(call) { + const args = { ...filters, limit: 1, waitSeconds: 0 } + const first = await call('mm_logs_follow', args) + assert.deepEqual(first.entries, [], 'Initial follow starts at the current watermark') + assert.ok(first.cursor) + const id = await appendEvent() + const next = await call('mm_logs_follow', { ...args, cursor: first.cursor }) + assert.deepEqual(next.entries.map(entry => entry.id), [id]) + const end = await call('mm_logs_follow', { ...args, cursor: next.cursor }) + assert.deepEqual(end.entries, []) + } }, + ].map(({ id, check }) => ({ id, kind: 'read', async run() { + await withClient(endpoint, token, check) + return null + } })) } + const result = await resumeMcpSteps(config) + assert.equal(result.status, 'passed', JSON.stringify(result)) + return config.steps.map(step => step.id) +} + +export async function verifyLogsSettingsPlan(endpoint, token, connectionId, ownerJwt, journal, fixture) { + assert.equal(endpoint.hostname, '127.0.0.1') + const owner = async (path, body) => { + const response = await fetch(new URL(path, endpoint), { + headers: { authorization: 'Bearer ' + ownerJwt, 'content-type': 'application/json' }, + redirect: 'error', + signal: AbortSignal.timeout(5000), + ...(body && { method: 'POST', body: JSON.stringify(body) }), + }) + assert.equal(response.status, 200, path) + return response.json() + } + const settingsPath = '/api/logs/settings' + const before = await owner(settingsPath) + const intervalSec = before.intervalSec === 60 ? 61 : 60 + return withClient(endpoint, token, async call => { + const planStep = 'mm_logs_settings_update_plan' + const applyStep = 'mm_change_apply' + const config = { journal, fixture, steps: [ + { id: planStep, kind: 'plan', async run({ accept }) { + const plan = await call(planStep, { settings: { intervalSec } }) + accept(plan.planId) + assert.equal(plan.state, 'awaiting-approval') + return plan.planId + } }, + { id: 'owner.decision', kind: 'apply', async run({ evidence, accept }) { + const planId = evidence.get(planStep) + const path = '/api/mcp/plans/' + planId + const plan = await owner(path) + assert.equal(plan.id, planId) + assert.equal(plan.operationId, 'logs.settings.update') + assert.equal(plan.connectionId, connectionId) + assert.equal(plan.state, 'awaiting-approval') + assert.deepEqual(plan.input.settings, { intervalSec }) + assert.deepEqual(await owner(settingsPath), before) + assert.deepEqual(await owner(path + '/decision', { approve: true }), { ok: true }) + accept(planId) + return planId + }, async reconcile({ evidence }) { + const planId = evidence.get(planStep) + const plan = await owner('/api/mcp/plans/' + planId) + return plan.state === 'approved' && plan.connectionId === connectionId + ? { state: 'verified', evidence: planId } : { state: 'unknown' } + } }, + { id: applyStep, kind: 'apply', async run({ evidence, idempotencyKey, accept }) { + const operation = await call(applyStep, { planId: evidence.get(planStep), idempotencyKey }) + accept(operation.id) + return operation.id + } }, + { id: 'mm_operation_get', kind: 'read', async run({ evidence, attempt }) { + if (attempt === 1) throw Object.assign(new Error('Simulated interruption before status read'), { code: 'FIXTURE_INTERRUPTED' }) + const operationId = evidence.get(applyStep) + const deadline = Date.now() + 10000 + let operation + do { + operation = await call('mm_operation_get', { operationId }) + assert.equal(operation.id, operationId) + assert.equal(operation.plan_id, evidence.get(planStep)) + assert.equal(operation.connection_id, connectionId) + if (!['pending', 'running'].includes(operation.state)) break + await new Promise(resolve => setTimeout(resolve, 100)) + } while (Date.now() < deadline) + assert.equal(operation.state, 'applied') + assert.equal(operation.verification, 'passed') + assert.deepEqual(await owner(settingsPath), { ...before, intervalSec }) + return operationId + } }, + ] } + assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'mm_operation_get' }, journal) + assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' }, journal) + return config.steps.map(step => step.id) + }) +} + +export async function verifyLogsReads(endpoint, serverId, exportToken, settingsToken, ownerJwt, journal, fixture) { + assert.equal(endpoint.hostname, '127.0.0.1') + const publish = async (call, accept) => { + const artifact = await call('mm_logs_export', { serverId, q: 'external-sdk-log-1', format: 'json' }) + accept(artifact.artifactId) + assert.equal(artifact.rows, 1) + return artifact.artifactId + } + const read = async (call, artifactId) => { + const first = await call('mm_logs_export_read', { artifactId, limit: 16 }) + assert(Number.isSafeInteger(first.bytes) && first.bytes > 16 && first.bytes <= 8192) + assert.equal(first.offset, 0) + assert.equal(first.nextOffset, 16) + assert.equal(first.complete, false) + const last = await call('mm_logs_export_read', { artifactId, offset: first.nextOffset }) + assert.equal(last.offset, 16) + assert.equal(last.nextOffset, null) + assert.equal(last.complete, true) + for (const part of [first, last]) { + assert.equal(part.artifactId, artifactId) + assert.equal(part.sha256, first.sha256) + assert.equal(part.bytes, first.bytes) + } + const content = Buffer.concat([first, last].map(part => Buffer.from(part.dataBase64, 'base64'))) + assert.equal(content.length, first.bytes) + assert.equal(createHash('sha256').update(content).digest('hex'), first.sha256) + assert.match(content.toString('utf8'), /external-sdk-log-1/) + assert.doesNotMatch(content.toString('utf8'), /external-sdk-log-2/) + return artifactId + } + const config = { journal, fixture, steps: [ + { id: 'mm_logs_export', kind: 'apply', + run: ({ accept }) => withClient(endpoint, exportToken, call => publish(call, accept)), + // An unknown publication has no idempotency key. Stop for reconciliation. + }, + { id: 'mm_logs_export_read', kind: 'read', + run: ({ evidence }) => withClient(endpoint, exportToken, call => read(call, evidence.get('mm_logs_export'))), + }, + { id: 'mm_logs_settings_get', kind: 'read', async run({ attempt }) { + if (attempt === 1) throw Object.assign(new Error('Simulated interruption before settings read'), { + code: 'FIXTURE_INTERRUPTED', + }) + await withClient(endpoint, settingsToken, async call => { + const response = await fetch(new URL('/api/logs/settings', endpoint), { + headers: { authorization: 'Bearer ' + ownerJwt }, + redirect: 'error', + signal: AbortSignal.timeout(5000), + }) + assert.equal(response.status, 200) + const expected = await response.json() + assert.equal(expected.enabled, false) + assert.deepEqual(await call('mm_logs_settings_get'), expected) + }) + return null + } }, + ] } + assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'mm_logs_settings_get' }, journal) + assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' }, journal) + return config.steps.map(step => step.id) +} diff --git a/backend/scripts/mcp-external-process/poll-steps-fixture.mjs b/backend/scripts/mcp-external-process/poll-steps-fixture.mjs new file mode 100644 index 00000000..30dda747 --- /dev/null +++ b/backend/scripts/mcp-external-process/poll-steps-fixture.mjs @@ -0,0 +1,147 @@ +import assert from 'node:assert/strict' +import { createServer } from 'node:http' +import { join } from 'node:path' +import { setTimeout as delay } from 'node:timers/promises' +import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client' +import { resumeServerPoll } from '../acceptance/mcp-server-poll-steps.mjs' + +export async function verifyPollSteps(endpoint, pool, jwt, output) { + assert.equal(endpoint.hostname, '127.0.0.1') + const replies = { + '/rest/system/identity': { name: 'Owned synthetic router' }, + '/rest/system/resource': { version: '7.owned', 'board-name': 'Owned', uptime: '1h', 'cpu-load': '2', 'free-memory': '1024', 'total-memory': '2048' }, + '/rest/interface': [{ name: 'ether1', type: 'ether' }], + '/rest/ip/address': [{ interface: 'ether1', address: '192.0.2.1/24' }], + } + const native = [] + const router = createServer((request, response) => { + native.push(request.url) + if (request.method !== 'GET' || !Object.hasOwn(replies, request.url)) return response.writeHead(403).end() + response.setHeader('content-type', 'application/json') + response.end(JSON.stringify(replies[request.url])) + }) + let client + try { + await new Promise((resolve, reject) => router.once('error', reject).listen(0, '127.0.0.1', resolve)) + const { rows: [server] } = await pool.query(`INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) + VALUES('Owned poll','127.0.0.1',$1,false,false,true,'owned-router','fixture') RETURNING id`, [router.address().port]) + const serverId = Number(server.id) + const { rows: catalogue } = await pool.query('SELECT id FROM servers ORDER BY id') + const owner = async (path, body) => { + const response = await fetch(new URL(path, endpoint), { + headers: { authorization: 'Bearer ' + jwt, 'content-type': 'application/json' }, + ...(body && { method: 'POST', body: JSON.stringify(body) }), + redirect: 'error', signal: AbortSignal.timeout(5000), + }) + assert.equal(response.status, 200, path) + return response.json() + } + const { connection, token } = await owner('/api/mcp/connections', { + name: 'Owned poll steps', mode: 'approval', expiresAt: new Date(Date.now() + 180000).toISOString(), + permissions: ['mm:servers:read', 'mm:servers:write', 'mm:settings:admin'], + scope: { serverIds: catalogue.map(row => Number(row.id)), operationIds: ['servers.poll', 'changes.apply', 'operations.read'], allowGlobal: true }, + limits: { callsPerMinute: 120, mutationsPerHour: 10000, objectsPerPlan: 10000 }, + }) + const connect = async () => { + await client?.close() + client = new Client({ name: 'owned-poll-steps', version: '1.0.0' }) + await client.connect(new StreamableHTTPClientTransport(endpoint, { + requestInit: { headers: { authorization: 'Bearer ' + token } }, + }), { timeout: 5000 }) + } + await connect() + const journal = join(output, 'poll-' + connection.id + '.jsonl') + let planId + let approvedExpiresAt + let operationId + let operation + let interruptStatus = true + let applyKey + const config = { + journal, fixture: 'owned-poll', serverId, connectionId: connection.id, + async findAcceptedOperation({ connectionId: scope, planId: plan, idempotencyKey }) { + assert.equal(operationId, undefined) + assert.equal(scope, connection.id) + assert.equal(plan, planId) + assert.equal(idempotencyKey, applyKey) + const { rows } = await pool.query(`SELECT id FROM mcp_operations + WHERE connection_id=$1 AND plan_id=$2 AND idempotency_key=$3`, [scope, plan, idempotencyKey]) + assert.equal(rows.length, 1) + operationId = rows[0].id + return operationId + }, + async call(name, args) { + if (name === 'mm_operation_get' && interruptStatus) { + interruptStatus = false + assert.equal(args.operationId, operationId) + throw Object.assign(new Error('Simulated interruption before reading status'), { code: 'FIXTURE_INTERRUPTED' }) + } + const reply = await client.callTool({ name, arguments: args }, undefined, { timeout: name === 'mm_server_poll_plan' ? 45000 : 5000 }) + if (reply.isError) { + const error = JSON.parse(reply.content.find(item => item.type === 'text').text) + const code = /^[A-Z0-9_]{1,64}$/.test(error.code) ? error.code : 'POLL_SDK_ERROR' + console.error('Owned SDK tool refused:', name, code) + throw Object.assign(new Error(name + ' failed'), { code }) + } + const result = reply.structuredContent.result + if (name === 'mm_server_poll_plan') planId = result.planId + if (name === 'mm_change_apply') { + assert.equal(applyKey, undefined, 'Apply cannot be sent twice') + applyKey = args.idempotencyKey + // The endpoint accepted the request, but the journal never sees its response. + throw Object.assign(new Error('Simulated lost apply response'), { code: 'FIXTURE_LOST_APPLY' }) + } + if (name === 'mm_operation_get') operation = result + return result + }, + async assertApproved(id) { + const plan = await owner('/api/mcp/plans/' + id) + assert.equal(plan.connectionId, connection.id) + assert.equal(plan.operationId, 'servers.poll') + if (plan.state !== 'approved') throw Object.assign(new Error('Waiting for owner approval'), { code: 'POLL_AWAITING_APPROVAL' }) + const expiresAt = Date.parse(plan.expiresAt) + assert(expiresAt > Date.now(), 'Approved poll must have a live expiry') + approvedExpiresAt = expiresAt + }, + } + assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'manager.plan-approved' }, journal) + assert.ok(planId) + assert.deepEqual(await owner('/api/mcp/plans/' + planId + '/decision', { approve: true }), { ok: true }) + assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'mm_change_apply' }, journal) + assert.ok(applyKey) + assert.equal(operationId, undefined) + await connect() + assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'mm_operation_get' }, journal) + assert.ok(operationId) + await connect() + const observationStartedAt = Date.now() + let state + while (true) { + const { rows } = await pool.query('SELECT state FROM mcp_operations WHERE id=$1 AND connection_id=$2', [operationId, connection.id]) + state = rows[0]?.state + const remaining = approvedExpiresAt - Date.now() + if (!['pending', 'running'].includes(state) || remaining <= 0) break + await delay(Math.min(500, remaining)) + } + const observedMs = Date.now() - observationStartedAt + const approvedLifetimeRemainingAtStartMs = approvedExpiresAt - observationStartedAt + assert.deepEqual(await resumeServerPoll(config), { status: 'passed' }, + `${journal} DB state ${state} after ${observedMs} ms (original approval window ${approvedLifetimeRemainingAtStartMs} ms at start)`) + assert.equal(operation.id, operationId) + const receipt = operation.steps[0].result + assert.equal(receipt.snapshot.status, 'online') + assert.deepEqual(native.sort(), Object.keys(replies).sort()) + const { rows } = await pool.query(`SELECT tool,count(*)::int AS count FROM mcp_requests + WHERE connection_id=$1 AND tool IN ('mm_server_poll_plan','mm_change_apply','mm_operation_get') GROUP BY tool`, [connection.id]) + const calls = Object.fromEntries(rows.map(row => [row.tool, row.count])) + assert.equal(calls.mm_server_poll_plan, 1) + assert.equal(calls.mm_change_apply, 1) + assert.equal(calls.mm_operation_get, 1) + return { journal, operationId, calls, reconciledLostApply: true, sdkReconnects: 2, nativeReads: native.length, publications: receipt.completedPublications, + state: operation.state, verification: operation.verification, localPostgres: true, sdkHttp: true, + approvedExpiresAt: new Date(approvedExpiresAt).toISOString(), approvedLifetimeRemainingAtStartMs, observedMs } + } finally { + await client?.close() + if (router.listening) await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/scripts/mcp-external-process/result-envelope-fixture.mjs b/backend/scripts/mcp-external-process/result-envelope-fixture.mjs new file mode 100644 index 00000000..5241d367 --- /dev/null +++ b/backend/scripts/mcp-external-process/result-envelope-fixture.mjs @@ -0,0 +1,138 @@ +import assert from 'node:assert/strict' +import { randomUUID } from 'node:crypto' +import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client' +import { StdioClientTransport } from '@modelcontextprotocol/client/stdio' + +const limit = 1024 * 1024 +const budgetMeta = 'io.mikrotikmanager/result-budget' +const comment = 'Ж'.repeat(11000) +const payloadBytes = value => Buffer.byteLength(JSON.stringify(value), 'utf8') +const tools = ['mm_servers_list', 'mm_logs_settings_update_plan', 'mm_change_apply', 'mm_operation_get'] + +export async function verifyResultEnvelope(endpoint, pool, jwt, bridgeFile) { + assert.equal(new URL(endpoint).hostname, '127.0.0.1') + const { rows: [{ count: existingServers }] } = await pool.query('SELECT count(*)::int AS count FROM servers') + await pool.query(` + INSERT INTO servers(name,host,enabled,comment) + SELECT 'owned-envelope-'||n, '192.0.2.'||(10+n), false, $1 FROM generate_series(1,24) AS n + `, [comment]) + const { rows: servers } = await pool.query('SELECT id FROM servers ORDER BY id') + // Independently observe actual writes, including an accidental repeat with unchanged settings. + await pool.query(` + CREATE TABLE owned_log_settings_writes (interval_sec integer NOT NULL); + CREATE FUNCTION owned_count_log_settings_write() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN + INSERT INTO owned_log_settings_writes VALUES (NEW.interval_sec); + RETURN NEW; + END $$; + CREATE TRIGGER owned_count_log_settings_write AFTER UPDATE ON router_log_settings + FOR EACH ROW EXECUTE FUNCTION owned_count_log_settings_write()`) + const headers = { authorization: 'Bearer ' + jwt, 'content-type': 'application/json' } + const json = async (path, body) => { + const response = await fetch(new URL(path, endpoint), { + headers, signal: AbortSignal.timeout(10000), ...(body ? { method: 'POST', body: JSON.stringify(body) } : {}), + }) + assert.equal(response.status, 200, path) + return response.json() + } + const cases = [] + for (const mode of ['http', 'stdio']) { + const { connection, token } = await json('/api/mcp/connections', { + name: 'Owned result envelope ' + mode, mode: 'approval', expiresAt: new Date(Date.now() + 600000).toISOString(), + permissions: ['mm:servers:read', 'mm:settings:admin'], + limits: { serversPerPlan: servers.length, mutationsPerHour: servers.length }, + scope: { serverIds: servers.map(row => Number(row.id)), allowGlobal: true, + operationIds: ['servers.list', 'logs.settings.update', 'changes.apply', 'operations.read'] }, + }) + const client = new Client({ name: 'owned-result-envelope-' + mode, version: '1.0.0' }) + const transport = mode === 'http' + ? new StreamableHTTPClientTransport(new URL(endpoint), { requestInit: { headers: { authorization: 'Bearer ' + token } } }) + : new StdioClientTransport({ command: process.execPath, args: ['--import', import.meta.resolve('tsx'), bridgeFile], + cwd: process.cwd(), env: { MCP_URL: endpoint, MCP_TOKEN: token }, maxBufferSize: limit + 65536, stderr: 'pipe' }) + transport.stderr?.on('data', () => {}) + try { + await client.connect(transport, { timeout: 10000 }) + const observedTools = (await client.listTools({}, { timeout: 5000 })).tools.map(tool => tool.name) + assert.deepEqual(observedTools.sort(), [...tools].sort()) + const call = async (name, args) => { + const result = await client.callTool({ name, arguments: args }, undefined, { timeout: 10000 }) + assert(payloadBytes(result) <= limit, mode + ' ' + name) + return result + } + const output = result => { + if (result.isError) assert.fail(JSON.parse(result.content[0].text).code) + const text = JSON.parse(result.content[0].text) + assert.deepEqual(result.structuredContent.result, text) + return text + } + const accepted = await call('mm_servers_list', { limit: 23, offset: existingServers }) + const page = output(accepted) + assert.equal(page.items.length, 23) + assert(page.items.every(item => item.comment === comment)) + assert(payloadBytes(accepted) >= 1000000) + // Even without field names and framing this page's duplicated UTF-8 text exceeds 1 MiB. + assert(24 * Buffer.byteLength(comment, 'utf8') * 2 > limit) + const refused = await call('mm_servers_list', { limit: 24, offset: existingServers }) + assert.equal(refused.isError, true) + assert.equal(refused.structuredContent, undefined) + assert.equal(JSON.parse(refused.content[0].text).code, 'RESULT_TOO_LARGE') + assert.deepEqual(refused._meta[budgetMeta], { + version: 1, detailsOmitted: true, reason: 'byte-budget', automaticReplay: false, + }) + + const before = await json('/api/logs/settings') + const intervalSec = before.intervalSec === 60 ? 61 : 60 + const plan = output(await call('mm_logs_settings_update_plan', { settings: { intervalSec } })) + assert.equal(plan.state, 'awaiting-approval') + await json('/api/mcp/plans/' + plan.planId + '/decision', { approve: true }) + const applyArgs = { planId: plan.planId, idempotencyKey: randomUUID() } + const operation = output(await call('mm_change_apply', applyArgs)) + let terminal + for (const end = Date.now() + 10000; Date.now() < end;) { + terminal = output(await call('mm_operation_get', { operationId: operation.id })) + if (!['pending', 'running'].includes(terminal.state)) break + await new Promise(resolve => setTimeout(resolve, 100)) + } + assert.equal(terminal.state, 'applied') + assert.equal(terminal.verification, 'passed') + const writes = async () => (await pool.query('SELECT count(*)::int AS n FROM owned_log_settings_writes')).rows[0].n + const checkEffect = async () => { + assert.equal(await writes(), cases.length + 1) + assert.deepEqual(await json('/api/logs/settings'), { ...before, intervalSec }) + } + await checkEffect() + + // Supply namespaced synthetic optional details to the actual, already applied operation. + // The ordinary runtime read and duplicate-apply paths must keep its original outcome. + const oversizedDetails = JSON.stringify(Array(24).fill(comment)) + const staged = await pool.query(` + UPDATE mcp_operations SET result=jsonb_set(COALESCE(result, '{}'::jsonb), '{ownedEnvelopeDetails}', $2::jsonb) + WHERE id=$1 AND state='applied' RETURNING id`, [operation.id, oversizedDetails]) + assert.equal(staged.rowCount, 1) + const compactRead = await call('mm_operation_get', { operationId: operation.id }) + const compactApply = await call('mm_change_apply', applyArgs) + for (const compact of [compactRead, compactApply]) { + assert(payloadBytes(compact) < 2048) + assert.deepEqual(compact._meta[budgetMeta], refused._meta[budgetMeta]) + assert.deepEqual(output(compact), { + id: operation.id, plan_id: plan.planId, state: 'applied', verification: 'passed', error_code: null, + }) + } + await checkEffect() + const { rows: [counts] } = await pool.query(` + SELECT + (SELECT count(*)::int FROM mcp_operations WHERE connection_id=$1) AS operations, + (SELECT count(*)::int FROM mcp_operation_steps WHERE operation_id=$2 AND state='verified') AS verified_steps, + (SELECT count(*)::int FROM mcp_requests WHERE connection_id=$1 AND tool='mm_change_apply') AS apply_requests + `, [connection.id, operation.id]) + assert.deepEqual(counts, { operations: 1, verified_steps: 1, apply_requests: 2 }) + cases.push({ mode, nearLimitBytes: payloadBytes(accepted), refusalBytes: payloadBytes(refused), + compactReadBytes: payloadBytes(compactRead), compactApplyBytes: payloadBytes(compactApply), + ...counts, settingsWrites: 1 }) + } finally { + await client.close() + } + } + return { scope: 'complete CallToolResult from HTTP SDK and production stdio bridge on owned PostgreSQL', + limitBytes: limit, wholeGenericResultEnvelopeLimitAccepted: true, cases } +} diff --git a/backend/scripts/mcp-external-process/run-owned.mjs b/backend/scripts/mcp-external-process/run-owned.mjs new file mode 100644 index 00000000..b4d11f08 --- /dev/null +++ b/backend/scripts/mcp-external-process/run-owned.mjs @@ -0,0 +1,60 @@ +import EmbeddedPostgres from 'embedded-postgres' +import { mkdtemp, rm, mkdir, readFile, writeFile } from 'node:fs/promises' +import { join, resolve, relative, isAbsolute } from 'node:path' +import { fileURLToPath } from 'node:url' +import { randomBytes, createHash } from 'node:crypto' +import { createServer } from 'node:net' +import { spawn } from 'node:child_process' +const sourceRoot = resolve(process.argv[2] ?? fileURLToPath(new URL('../../../', import.meta.url))) +const scenario = process.argv[3] ?? 'acceptance' +if (!['acceptance', 'bridge-vlan', 'logs-read', 'logs-history', 'logs-settings', 'poll-steps', 'result-envelope'].includes(scenario)) throw Error('Unsupported owned external scenario') +const logsScenario = scenario.startsWith('logs-') +const envelopeScenario = scenario === 'result-envelope' +const reportRoot = resolve(sourceRoot, scenario === 'bridge-vlan' ? 'tmp/mcp-bridge-vlan-sdk-20261105' : + logsScenario || scenario === 'poll-steps' || envelopeScenario ? 'tmp/mcp-external-' + scenario : 'tmp/mcp-external-process-acceptance') +await mkdir(reportRoot, { recursive: true }) +const output = scenario === 'poll-steps' || envelopeScenario ? await mkdtemp(join(reportRoot, 'run-')) : reportRoot +const paths = ['backend/src/scripts/mcp-stdio.ts', 'backend/src/routes/mcp-transport.ts', 'backend/src/routes/mcp-oauth.ts', 'backend/src/modules/mcp/store.ts', + 'backend/src/modules/mcp/runtime.ts', 'backend/src/modules/mcp/actions.ts', 'backend/src/modules/mcp/security.ts', 'backend/src/modules/mcp/protocol-limits.ts', + 'backend/src/modules/mcp/portal-revocation.ts', 'backend/src/plugins/auth.ts', 'backend/src/index.ts', 'node_modules/@modelcontextprotocol/client/package.json', 'node_modules/@modelcontextprotocol/server/package.json'] +if (scenario !== 'bridge-vlan') paths.push('backend/scripts/mcp-external-process/client.mjs', 'backend/scripts/mcp-external-process/server-fixture.ts', + 'backend/src/routes/mcp-portal.ts', 'backend/src/modules/mcp/portal-revocation-event.ts', 'backend/src/services/router-logs-query.ts') +else paths.push('backend/scripts/mcp-external-process/bridge-vlan-client.mjs', 'backend/scripts/mcp-external-process/bridge-vlan-fixture.ts', + 'backend/src/modules/mcp/bridge-vlan-actions.ts', 'backend/src/services/bridge-vlan-configuration.ts', 'packages/contracts/src/bridge-vlan.ts') +if (logsScenario) paths.push('backend/scripts/mcp-external-process/logs-read-client.mjs', + 'backend/scripts/acceptance/mcp-step-journal.mjs', 'backend/src/modules/mcp/log-artifacts.ts') +if (scenario === 'poll-steps') paths.push('backend/scripts/mcp-external-process/poll-steps-fixture.mjs', + 'backend/scripts/acceptance/mcp-step-journal.mjs', 'backend/scripts/acceptance/mcp-server-poll-steps.mjs', + 'backend/src/modules/mcp/server-poll-production.ts', 'backend/src/services/server-poll-execution.ts') +if (envelopeScenario) paths.push('backend/scripts/mcp-external-process/run-owned.mjs', 'backend/scripts/mcp-external-process/result-envelope-fixture.mjs', + 'backend/src/modules/mcp/result-budget.ts', 'backend/src/modules/mcp/result-delivery.ts', 'backend/src/modules/mcp/flow-envelope-allowance.ts', + 'backend/src/modules/mcp/sdk-terminal-budget.ts', 'backend/src/modules/mcp/stdio-terminal-budget.ts') +async function pins() { const result = []; for (const path of paths) result.push({ path, sha256: createHash('sha256').update(await readFile(resolve(sourceRoot, path))).digest('hex') }); return result } +const before = await pins(), directory = await mkdtemp(join(resolve(sourceRoot, 'tmp'), 'mm-mcp-external-')), state = join(directory, 'state'); await mkdir(state) +const listener = createServer(); await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)); const port = listener.address().port; await new Promise(resolve => listener.close(resolve)) +const password = randomBytes(24).toString('hex') +const postgres = new EmbeddedPostgres({ databaseDir: join(directory, 'pg'), port, user: 'mcp_test', password, persistent: true, createPostgresUser: false, + initdbFlags: ['--encoding=UTF8'], postgresFlags: ['-h', '127.0.0.1'], onLog: () => {}, onError: () => {} }) +try { + await postgres.initialise(); await postgres.start() + process.exitCode = await new Promise((done, reject) => { + const child = spawn(process.execPath, ['--import', import.meta.resolve('tsx'), fileURLToPath(new URL(scenario === 'bridge-vlan' ? './bridge-vlan-fixture.ts' : './server-fixture.ts', import.meta.url))], { cwd: directory, + windowsHide: true, stdio: 'inherit', env: { ...Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path'].filter(k => process.env[k]).map(k => [k, process.env[k]])), + OWN_EXTERNAL_CLEANUP_FAULT: process.env.OWN_EXTERNAL_CLEANUP_FAULT, OWN_EXTERNAL_LOGS_SCENARIO: logsScenario ? scenario : '', + OWN_EXTERNAL_POLL_STEPS: scenario === 'poll-steps' ? 'true' : '', + OWN_EXTERNAL_RESULT_ENVELOPE: envelopeScenario ? 'true' : '', + OWN_EXTERNAL_SOURCE_ROOT: sourceRoot, OWN_EXTERNAL_RESULT_FILE: join(output, 'result.json'), NODE_ENV: 'test', OWN_EXTERNAL_PROCESS_PG: 'true', DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, DATABASE_PATH: join(directory, 'no-legacy.sqlite'), + MCP_STATE_DIR: state, AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: 'owned-external-synthetic-issuer-test-only', AUTH_ISSUER: 'https://synthetic-portal.invalid', AUTH_PORTAL_URL: 'https://synthetic-portal.invalid' } }) + child.once('error', reject); child.once('exit', code => done(code ?? 1)) + }) + const after = await pins(), unchanged = JSON.stringify(before) === JSON.stringify(after) + await writeFile(join(output, 'source-pins.json'), JSON.stringify({ before, after, unchanged, exitCode: process.exitCode }, null, 2)) + if (scenario === 'poll-steps') console.log('Poll checkpoint evidence:', output) + if (envelopeScenario) console.log('Result envelope evidence:', output) + console.log('External-client source/SDK pins unchanged:', unchanged); if (!unchanged) process.exitCode = 1 +} finally { + await postgres.stop(); const target = resolve(directory), rel = relative(resolve(resolve(sourceRoot, 'tmp')), target) + if (!rel || rel.startsWith('..') || isAbsolute(rel) || !target.includes('mm-mcp-external-')) throw Error('Unsafe exact owned cleanup') + await rm(target, { recursive: true, force: true, maxRetries: 10, retryDelay: 250 }); console.log('Owned external-client PostgreSQL stopped and exact temp directory removed') +} +process.exit(process.exitCode ?? 0) diff --git a/backend/scripts/mcp-external-process/server-fixture.ts b/backend/scripts/mcp-external-process/server-fixture.ts new file mode 100644 index 00000000..a8c4cf29 --- /dev/null +++ b/backend/scripts/mcp-external-process/server-fixture.ts @@ -0,0 +1,424 @@ +import assert from "node:assert/strict" +import { createHash, createHmac, randomUUID } from "node:crypto" +import { spawn } from "node:child_process" +import { createServer } from "node:net" +import { fileURLToPath, pathToFileURL } from "node:url" +import { dirname, resolve, isAbsolute } from "node:path" +import { SignJWT } from "jose" +import { writeFile } from "node:fs/promises" +assert.equal(process.env.OWN_EXTERNAL_PROCESS_PG, "true"); assert.equal(process.env.NODE_ENV, "test") +assert(process.env.OWN_EXTERNAL_SOURCE_ROOT && isAbsolute(process.env.OWN_EXTERNAL_SOURCE_ROOT)) +assert(process.env.OWN_EXTERNAL_RESULT_FILE && isAbsolute(process.env.OWN_EXTERNAL_RESULT_FILE)) +const source = (path: string) => pathToFileURL(resolve(process.env.OWN_EXTERNAL_SOURCE_ROOT!, "backend/src", path)).href +const database = new URL(process.env.DATABASE_URL ?? "") +assert.equal(database.hostname, "127.0.0.1"); assert.equal(database.username, "mcp_test"); assert.equal(database.pathname, "/postgres"); assert.notEqual(database.port, "5432") +const listener = createServer(); await new Promise(r => listener.listen(0, "127.0.0.1", r)); const port = (listener.address() as { port: number }).port +await new Promise(r => listener.close(() => r())) +const origin = `http://127.0.0.1:${port}`, endpoint = origin + "/mcp" +process.env.MCP_PUBLIC_URL = endpoint; process.env.MCP_UI_URL = origin; process.env.CORS_ORIGIN = origin +const { pool, closePool } = await import(source("db/index.js")) +const { applySqlMigrations } = await import(source("db/migrate.js")) +const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") +const { createMcpConnection, rotateConnectionToken, setConnectionStatus, updateMcpSettings } = await import(source("modules/mcp/store.js")) +const { listActions } = await import(source("modules/mcp/actions.js")) as typeof import("../../src/modules/mcp/actions.js") +const { env } = await import(source("config.js")) +const { buildApp } = await import(source("index.js")) +const { stopSchedulerForDatabaseOperation } = await import(source("services/scheduler.js")) +const actor = { id: "owned-external-process-owner", email: "external@example.invalid", name: "Owned external SDK fixture", apps: ["mm"], + permissions: ["mm:settings:admin", "mm:mcp:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000, issuedAt: Date.now() - 1000 } +let currentId: string | undefined, app: Awaited> | undefined, child: ReturnType | undefined +const credentials: string[] = [] +const bridgePids = new Set() +let childExit: Promise | undefined +let releaseLogRead: (() => void) | undefined +let logReadCompleted: Promise | undefined +let revocationOwnerId: string | undefined +const revokedLogRequests: { connectionId: string; tool: string }[] = [] +const originalConnect = pool.connect +const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) +async function deliverPortalRevocation(ownerId: string) { + const secret = randomUUID() + randomUUID() + process.env.MCP_PORTAL_WEBHOOK_SECRET = secret + const occurredAt = Date.now(), timestamp = String(Math.floor(occurredAt / 1000)) + const eventId = randomUUID(), body = JSON.stringify({ version: 2, eventId, userId: ownerId, type: "permissions.changed", eventOccurredAt: occurredAt }) + const signature = "sha256=" + createHmac("sha256", secret).update(timestamp + "." + body).digest("hex") + const response = await fetch(origin + "/_internal/mcp/portal/revocations", { + method: "POST", headers: { "content-type": "application/json", "x-mm-timestamp": timestamp, "x-mm-signature": signature }, + body, signal: AbortSignal.timeout(5000), + }) + assert.equal(response.status, 200) + assert.deepEqual(await response.json(), { ok: true, duplicate: false }) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_portal_events WHERE id=$1", [eventId])).rows[0].count, 1) + return occurredAt +} +function alive(pid: number) { try { process.kill(pid, 0); return true } catch (error) { if ((error as NodeJS.ErrnoException).code === "ESRCH") return false; throw error } } +async function drainChildAndBridges() { + if (child && child.exitCode === null && child.signalCode === null) { child.kill(); await Promise.race([childExit, delay(5000).then(() => { throw Error("Owned external child failed to exit") })]) } + const deadline = Date.now() + 5000 + while ([...bridgePids].some(alive) && Date.now() < deadline) await delay(25) + const remaining = [...bridgePids].filter(alive) + for (const pid of remaining) process.kill(pid) + const killDeadline = Date.now() + 5000 + while (remaining.some(alive) && Date.now() < killDeadline) await delay(25) + assert(!remaining.some(alive), "Owned SDK bridge grandchild failed to exit") + console.log("Owned client and all observed production bridge processes exited:", JSON.stringify({ childPid: child?.pid, exitCode: child?.exitCode, signalCode: child?.signalCode, bridgePids: [...bridgePids] })) +} +try { + await applySqlMigrations(pool) + for (const table of ["traffic_settings", "uptime_settings", "traffic_flow_settings", "servers_api_ping_settings", "certificate_renew_settings", "backup_schedule_settings", "geoip_settings", "internet_path_settings", "router_log_settings"]) await pool.query(`UPDATE ${table} SET enabled=false WHERE id=1`) + const secret = "OWNED_DEVICE_CREDENTIAL_MUST_NOT_ESCAPE" + const allowed = (await pool.query("INSERT INTO servers(name,host,enabled,password) VALUES('owned allowed','192.0.2.1',false,$1) RETURNING id", [secret])).rows[0].id + const forbidden = (await pool.query("INSERT INTO servers(name,host,enabled) VALUES('owned forbidden','192.0.2.2',false) RETURNING id")).rows[0].id + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + SELECT id,name,host,'fixture','00:00:01',now(),now()-n*interval '1 second',ARRAY['system'],'info','external-sdk-log-'||n,0 + FROM servers CROSS JOIN generate_series(1,2) AS n WHERE id=ANY($1::int[])`, [[allowed, forbidden]]) + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false, logger: false }); await app.listen({ port, host: "127.0.0.1" }) + // Settings changes call refreshScheduler even when startup skipped it. + await stopSchedulerForDatabaseOperation() + const jwt = await new SignJWT({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setIssuedAt().setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + if (process.env.OWN_EXTERNAL_RESULT_ENVELOPE === "true") { + const { verifyResultEnvelope } = await import("./result-envelope-fixture.mjs") + const report = await verifyResultEnvelope(endpoint, pool, jwt, fileURLToPath(new URL("../../src/scripts/mcp-stdio.ts", import.meta.url))) + await writeFile(process.env.OWN_EXTERNAL_RESULT_FILE!, JSON.stringify(report, null, 2)) + console.log(JSON.stringify(report)) + } else if (process.env.OWN_EXTERNAL_POLL_STEPS === "true") { + const { ensurePartitionFor } = await import(source("db/partitions.js")) + const { verifyPollSteps } = await import("./poll-steps-fixture.mjs") + await ensurePartitionFor(pool, "server_snapshots", "week", new Date()) + const report = await verifyPollSteps(new URL(endpoint), pool, jwt, dirname(process.env.OWN_EXTERNAL_RESULT_FILE!)) + await writeFile(process.env.OWN_EXTERNAL_RESULT_FILE!, JSON.stringify(report, null, 2)) + console.log(JSON.stringify(report)) + } else if (process.env.OWN_EXTERNAL_LOGS_SCENARIO === "logs-settings") { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned logs settings approval", mode: "approval", expiresAt: new Date(Date.now() + 600000).toISOString(), + permissions: ["mm:settings:admin"], scope: { serverIds: [Number(allowed), Number(forbidden)], + operationIds: ["logs.settings.update", "changes.apply", "operations.read"], allowGlobal: true }, + }), actor) + const fixture = `settings-${access.connection.id}` + const journal = resolve(dirname(process.env.OWN_EXTERNAL_RESULT_FILE!), fixture + ".jsonl") + const { verifyLogsSettingsPlan } = await import("./logs-read-client.mjs") + const cases = await verifyLogsSettingsPlan(new URL(endpoint), access.token, access.connection.id, jwt, journal, fixture) + const { rows } = await pool.query(`SELECT tool,count(*)::int AS count FROM mcp_requests + WHERE connection_id=$1 AND tool NOT LIKE 'protocol:%' GROUP BY tool ORDER BY tool`, [access.connection.id]) + const calls = Object.fromEntries(rows.map(row => [row.tool, row.count])) + assert.deepEqual(Object.keys(calls).sort(), ["mm_change_apply", "mm_logs_settings_update_plan", "mm_operation_get"]) + assert.equal(calls.mm_change_apply, 1) + assert.equal(calls.mm_logs_settings_update_plan, 1) + assert(calls.mm_operation_get >= 1) + const report = { cases, journal, calls, localPostgres: true, sdkHttp: true } + await writeFile(process.env.OWN_EXTERNAL_RESULT_FILE!, JSON.stringify(report, null, 2)) + console.log(JSON.stringify(report)) + } else if (process.env.OWN_EXTERNAL_LOGS_SCENARIO === "logs-history") { + // Follow checks that the server is enabled; collection is disabled and the + // synthetic server resolves to loopback even if that changes. + await pool.query("UPDATE servers SET enabled=true,host='127.0.0.1' WHERE id=$1", [allowed]) + await pool.query(`UPDATE router_log_entries + SET effective_at=date_trunc('second',now())-interval '1 second' + + CASE WHEN message='external-sdk-log-1' THEN interval '200 microseconds' ELSE interval '100 microseconds' END + WHERE server_id=$1`, [allowed]) + const history = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned logs history reader", mode: "observer", expiresAt: new Date(Date.now() + 600000).toISOString(), + permissions: ["mm:servers:read"], scope: { serverIds: [Number(allowed)], + operationIds: ["logs.query", "logs.summary", "logs.get", "logs.context", "logs.follow"], allowGlobal: false }, + }), actor) + const { rows } = await pool.query("SELECT id::text,message FROM router_log_entries WHERE server_id=$1 ORDER BY effective_at DESC", [allowed]) + assert.equal(rows.length, 2) + const fixture = `history-${history.connection.id}` + const journal = resolve(dirname(process.env.OWN_EXTERNAL_RESULT_FILE!), fixture + ".jsonl") + const { verifyLogsHistory } = await import("./logs-read-client.mjs") + const appendEvent = async () => (await pool.query(`INSERT INTO router_log_entries + (server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + SELECT id,name,host,'fixture','00:00:02',now(),now(),ARRAY['system'],'info','external-sdk-log-live',0 + FROM servers WHERE id=$1 RETURNING id::text`, [allowed])).rows[0].id + const cases = await verifyLogsHistory(new URL(endpoint), history.token, Number(allowed), rows, journal, fixture, appendEvent) + const report = { cases, journal, localPostgres: true, sdkHttp: true } + await writeFile(process.env.OWN_EXTERNAL_RESULT_FILE!, JSON.stringify(report, null, 2)) + console.log(JSON.stringify(report)) + } else if (process.env.OWN_EXTERNAL_LOGS_SCENARIO === "logs-read") { + const expiresAt = new Date(Date.now() + 600000).toISOString() + const exporter = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned logs export reader", mode: "observer", expiresAt, permissions: ["mm:servers:read"], + scope: { serverIds: [Number(allowed)], operationIds: ["logs.export", "logs.export.read"], allowGlobal: false }, + }), actor) + const settings = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned logs settings reader", mode: "observer", expiresAt, permissions: ["mm:settings:admin"], + scope: { serverIds: [], operationIds: ["logs.settings.read"], allowGlobal: true }, + }), actor) + const { verifyLogsReads } = await import("./logs-read-client.mjs") + const fixture = `logs-${exporter.connection.id}` + const journal = resolve(dirname(process.env.OWN_EXTERNAL_RESULT_FILE!), fixture + ".jsonl") + const cases = await verifyLogsReads(new URL(endpoint), Number(allowed), exporter.token, settings.token, jwt, journal, fixture) + const { rows } = await pool.query(`SELECT tool, count(*)::int AS count FROM mcp_requests + WHERE connection_id=ANY($1::uuid[]) AND tool NOT LIKE 'protocol:%' + GROUP BY tool ORDER BY tool`, [[exporter.connection.id, settings.connection.id]]) + assert.deepEqual(rows, [ + { tool: "mm_logs_export", count: 1 }, + { tool: "mm_logs_export_read", count: 2 }, + { tool: "mm_logs_settings_get", count: 1 }, + ]) + const report = { cases, journal, toolCalls: rows, localPostgres: true, sdkHttp: true, nativeRouterCalls: 0 } + await writeFile(process.env.OWN_EXTERNAL_RESULT_FILE!, JSON.stringify(report, null, 2)) + console.log(JSON.stringify(report)) + } else { + const artifactOwner = { ...actor, id: actor.id + "-artifact" } + const signArtifactOwner = () => new SignJWT({ sub: artifactOwner.id, apps: artifactOwner.apps, permissions: artifactOwner.permissions }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setIssuedAt().setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + let artifactConnectionId: string | undefined + const planOwner = { ...actor, id: actor.id + "-approved-plan", permissions: [...actor.permissions, "mm:mcp:approve"] } + let planConnectionId: string | undefined + const portalHeaders = { authorization: `Bearer ${jwt}`, "content-type": "application/json" } + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_connections WHERE owner_id=$1", [actor.id])).rows[0].count, 0) + const callback = "http://localhost:45678/callback?registered=1" + const registration = await fetch(origin + "/api/mcp/oauth/clients", { method: "POST", headers: portalHeaders, + body: JSON.stringify({ name: "Owned denial fixture", redirectUris: [callback] }) }) + assert.equal(registration.status, 200) + const { clientId } = await registration.json() as { clientId: string } + const consent = (payload: Record, headers = portalHeaders) => fetch(origin + "/api/mcp/oauth/consent", { method: "POST", headers, body: JSON.stringify(payload) }) + const authorize = async (state: string) => { + const parameters = new URLSearchParams({ client_id: clientId, redirect_uri: callback, response_type: "code", resource: endpoint, + code_challenge_method: "S256", code_challenge: createHash("sha256").update("v".repeat(64)).digest("base64url"), state }) + const response = await fetch(`${origin}/oauth/authorize?${parameters}`, { redirect: "manual" }) + assert.equal(response.status, 302) + const requestId = new URL(response.headers.get("location")!).searchParams.get("oauthRequest") + assert.ok(requestId) + return requestId + } + const requestId = await authorize("deny-without-connection") + assert.equal((await consent({ requestId, approve: false }, { ...portalHeaders, authorization: "" })).status, 401) + assert.equal((await consent({ requestId, approve: true })).status, 400) + const refusals = await Promise.all([consent({ requestId, approve: false }), consent({ requestId, approve: false })]) + assert.deepEqual(refusals.map(response => response.status).sort(), [200, 404]) + const { redirectUrl } = await refusals.find(response => response.status === 200)!.json() as { redirectUrl: string } + const redirect = new URL(redirectUrl) + const expected = new URL(callback) + expected.searchParams.set("state", "deny-without-connection") + expected.searchParams.set("iss", origin) + expected.searchParams.set("error", "access_denied") + assert.equal(redirect.href, expected.href) + const otherOwner = { ...actor, id: "owned-external-process-other-owner" } + const unrelated = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Unrelated paused connection", mode: "observer", + expiresAt: new Date(Date.now() + 600000).toISOString(), permissions: ["mm:servers:read"], + scope: { serverIds: [Number(allowed)], operationIds: ["servers.list"], allowGlobal: false } }), otherOwner) + await setConnectionStatus(unrelated.connection.id, "paused", otherOwner) + const legacyRequestId = await authorize("legacy-denial") + const legacy = await consent({ requestId: legacyRequestId, connectionId: unrelated.connection.id, approve: false }) + assert.equal(legacy.status, 200) + assert.equal((await consent({ requestId: legacyRequestId, connectionId: unrelated.connection.id, approve: false })).status, 404) + const expired = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Expired connection", mode: "observer", + expiresAt: new Date(Date.now() + 600000).toISOString(), permissions: ["mm:servers:read"], + scope: { serverIds: [Number(allowed)], operationIds: ["servers.list"], allowGlobal: false } }), actor) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{expiresAt}',to_jsonb($2::text)) WHERE id=$1", [expired.connection.id, "2000-01-01T00:00:00Z"]) + const expiredRequestId = await authorize("expired-approval") + assert.equal((await consent({ requestId: expiredRequestId, connectionId: expired.connection.id, approve: true })).status, 403) + const expiredRequest = (await pool.query("SELECT consumed_at,code_digest,connection_id FROM mcp_oauth_requests WHERE id=$1", [expiredRequestId])).rows[0] + assert.deepEqual(expiredRequest, { consumed_at: null, code_digest: null, connection_id: null }) + assert.equal((await consent({ requestId: expiredRequestId, approve: false })).status, 200) + const deniedRows = (await pool.query("SELECT consumed_at,code_digest,connection_id FROM mcp_oauth_requests WHERE client_id=$1", [clientId])).rows + assert.equal(deniedRows.length, 3) + assert(deniedRows.every(row => row.consumed_at instanceof Date && row.code_digest === null && row.connection_id === null)) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_credentials WHERE client_id=$1", [clientId])).rows[0].count, 0) + const denialAudit = (await pool.query("SELECT actor_id,connection_id,payload FROM mcp_audit WHERE action='oauth.consent' AND payload->>'clientId'=$1", [clientId])).rows + assert.equal(denialAudit.length, 3) + assert(denialAudit.every(row => row.actor_id === actor.id && row.connection_id === null && row.payload.approved === false)) + console.log("OAuth consent: expired approval refused before code issuance, denial uses exact redirect and one-use request without credentials") + const transcript: unknown[] = []; let failure: unknown, result: unknown + child = spawn(process.execPath, [fileURLToPath(new URL("./client.mjs", import.meta.url))], { cwd: process.cwd(), windowsHide: true, stdio: ["ignore", "pipe", "pipe", "ipc"], + env: { ...Object.fromEntries(["SystemRoot", "WINDIR", "TEMP", "TMP", "PATH", "Path"].filter(k => process.env[k]).map(k => [k, process.env[k]!])), + OWN_EXTERNAL_CLIENT: "true", OWN_MCP_ENDPOINT: endpoint, OWN_SERVER_PID: String(process.pid), OWN_ALLOWED_SERVER: String(allowed), OWN_FORBIDDEN_SERVER: String(forbidden), + OWN_BRIDGE_FILE: fileURLToPath(new URL(source("scripts/mcp-stdio.ts"))), OWN_PORTAL_JWT: jwt, OWN_SERVER_SECRET: secret, OWN_EXTERNAL_CLEANUP_FAULT: process.env.OWN_EXTERNAL_CLEANUP_FAULT } }) + childExit = new Promise(resolve => { child!.once("exit", status => resolve(status ?? 1)); child!.once("error", () => resolve(1)) }) + let stderr = "", stdout = "" + child.stderr?.on("data", chunk => { stderr += String(chunk); if (stderr.length > 1048576) child?.kill() }) + child.stdout?.on("data", chunk => { stdout += String(chunk); if (stdout.length > 1048576) child?.kill() }) + child.on("message", async (raw: unknown) => { + const message = raw as { kind: string; command?: string; id?: number; pid?: number; result?: unknown; phase?: string; errorType?: string } + if (message.kind === "bridge-pid") { assert(Number.isSafeInteger(message.pid) && message.pid! > 0 && message.pid !== process.pid && message.pid !== child?.pid); bridgePids.add(message.pid!); return } + if (message.kind === "arm-cleanup-timeout" && process.env.OWN_EXTERNAL_CLEANUP_FAULT === "timeout") { setTimeout(() => { failure = { phase: "owned-deliberate-cleanup-timeout" }; child?.kill() }, 25); return } + if (message.kind === "result") { result = message.result; return } + if (message.kind === "failure") { failure = { phase: message.phase, errorType: message.errorType }; return } + if (message.kind !== "control" || !Number.isSafeInteger(message.id)) return + try { + let value: unknown + if (message.command === "new-revocable-plan-connection") { + assert(!planConnectionId) + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned approved plan revocation", mode: "approval", permissions: ["mm:settings:admin"], + expiresAt: new Date(Date.now() + 600000).toISOString(), + scope: { serverIds: [Number(allowed), Number(forbidden)], operationIds: ["logs.settings.update", "changes.apply"], allowGlobal: true }, + }), planOwner) + currentId = planConnectionId = issued.connection.id + const ownerJwt = await new SignJWT({ sub: planOwner.id, apps: planOwner.apps, permissions: planOwner.permissions }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setIssuedAt().setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + credentials.push(issued.token, ownerJwt) + value = { token: issued.token, ownerJwt } + } else if (message.command === "revoke-approved-plan-owner") { + assert(planConnectionId && currentId === planConnectionId) + const plans = (await pool.query("SELECT id,state,approved_by FROM mcp_plans WHERE connection_id=$1", [planConnectionId])).rows + assert.equal(plans.length, 1) + assert.equal(plans[0].state, "approved") + assert.equal(plans[0].approved_by, planOwner.id) + await deliverPortalRevocation(planOwner.id) + value = { ok: true } + } else if (message.command === "new-artifact-connection") { + assert(!artifactConnectionId) + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned export revocation", mode: "observer", permissions: ["mm:servers:read"], + expiresAt: new Date(Date.now() + 600000).toISOString(), + scope: { serverIds: [Number(allowed)], operationIds: ["logs.export"], allowGlobal: false }, + }), artifactOwner) + currentId = artifactConnectionId = issued.connection.id + const ownerJwt = await signArtifactOwner() + credentials.push(issued.token, ownerJwt) + value = { token: issued.token, ownerJwt } + } else if (message.command === "revoke-artifact-owner") { + assert(artifactConnectionId && currentId === artifactConnectionId) + const occurredAt = await deliverPortalRevocation(artifactOwner.id) + while (Math.floor(Date.now() / 1000) <= Math.floor(occurredAt / 1000)) await delay(25) + const ownerJwt = await signArtifactOwner() + credentials.push(ownerJwt) + value = { ownerJwt } + } else if (["arm-log-revocation", "arm-log-revocation-after-commit", "arm-log-follow-revocation"].includes(message.command ?? "")) { + assert(!releaseLogRead) + const afterCommit = message.command === "arm-log-revocation-after-commit" + const follow = message.command === "arm-log-follow-revocation" + const owner = { ...actor, id: `${actor.id}-${follow ? "follow" : afterCommit ? "postcommit" : "precommit"}` } + revocationOwnerId = owner.id + if (follow) { + await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [allowed]) + await pool.query("UPDATE router_log_settings SET enabled=true WHERE id=1") + } + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned portal revocation during log read", mode: "observer", permissions: ["mm:servers:read"], + expiresAt: new Date(Date.now() + 600000).toISOString(), + scope: { serverIds: [Number(allowed)], operationIds: [follow ? "logs.follow" : "logs.query"], allowGlobal: false }, + }), owner) + currentId = issued.connection.id + revokedLogRequests.push({ connectionId: currentId, tool: follow ? "mm_logs_follow" : "mm_logs_query" }) + credentials.push(issued.token) + let completed!: () => void + logReadCompleted = new Promise(resolve => { completed = resolve }) + const hold = new Promise(resolve => { releaseLogRead = resolve }) + pool.connect = ((callback?: unknown) => { + if (typeof callback === "function") return Reflect.apply(originalConnect, pool, [callback]) + return (async () => { + const client = await originalConnect.call(pool) + const query = client.query, release = client.release + let completingLog = false + client.query = (async (...args: unknown[]) => { + const params = args[1] + if (typeof args[0] === "string" && args[0].startsWith("UPDATE mcp_requests SET status=$2,result=") + && Array.isArray(params) && params[1] === "completed" && String(params[2]).includes(follow ? "external-sdk-live-event" : "external-sdk-log-1")) { + completingLog = true + if (!afterCommit) { completed(); await hold } + } + const result = await Reflect.apply(query, client, args) + if (afterCommit && completingLog && args[0] === "COMMIT") { completed(); await hold } + return result + }) as typeof query + client.release = error => { + client.query = query + client.release = release + release(error) + } + return client + })() + }) as typeof originalConnect + value = { token: issued.token } + } else if (message.command === "publish-follow-fixture-row") { + assert(releaseLogRead && currentId && revokedLogRequests.at(-1)?.tool === "mm_logs_follow") + const deadline = Date.now() + 3000 + for (;;) { + const live = await pool.query("SELECT count(*)::int AS count FROM mcp_read_leases WHERE connection_id=$1", [currentId]) + if (live.rows[0].count === 1) break + assert(Date.now() < deadline, "Owned follow did not acquire a live read lease") + await delay(25) + } + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + SELECT id,name,host,'fixture','00:00:01',now(),now(),ARRAY['system'],'info','external-sdk-live-event',0 + FROM servers WHERE id=$1`, [allowed]) + value = { ok: true } + } else if (message.command === "deliver-log-revocation") { + assert(logReadCompleted && releaseLogRead && currentId && revocationOwnerId) + try { + await Promise.race([logReadCompleted, delay(5000).then(() => { throw Error("Owned log read did not reach the barrier") })]) + await deliverPortalRevocation(revocationOwnerId) + value = { ok: true } + } finally { + pool.connect = originalConnect + releaseLogRead() + releaseLogRead = undefined + logReadCompleted = undefined + revocationOwnerId = undefined + } + } else if (message.command === "new-connection") { + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned external SDK process", mode: "observer", expiresAt: new Date(Date.now() + 1200000).toISOString(), + permissions: ["mm:servers:read"], scope: { serverIds: [Number(allowed)], operationIds: ["servers.list"], allowGlobal: false }, limits: { callsPerMinute: 200 } }), actor) + currentId = issued.connection.id; credentials.push(issued.token); value = { token: issued.token, connectionId: currentId } + } else if (message.command === "discovery-connection") { + const operations = listActions().map(action => action.operation) + const permissions = [...new Set(operations.flatMap(operation => operation.requiredPermissions))] + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Owned full catalogue discovery", mode: "approval", expiresAt: new Date(Date.now() + 600000).toISOString(), + permissions, scope: { serverIds: [Number(allowed)], operationIds: operations.map(operation => operation.id), allowGlobal: true }, + }), { ...actor, permissions: [...new Set([...actor.permissions, ...permissions])] }) + currentId = issued.connection.id + credentials.push(issued.token) + value = { token: issued.token, names: operations.map(operation => operation.toolName) } + } else if (message.command === "large-result" || message.command === "reset-result") { + await pool.query("UPDATE servers SET comment=$2 WHERE id=$1", [allowed, message.command === "large-result" ? "Z".repeat(40000) + "OWNED_TRUNCATION_TAIL" : ""]) + value = { ok: true } + } else { + assert(currentId) + if (message.command === "rotate") { const rotated = await rotateConnectionToken(currentId, actor); credentials.push(rotated.token); value = rotated } + else if (message.command === "pause" || message.command === "resume" || message.command === "revoke") { await setConnectionStatus(currentId, message.command === "pause" ? "paused" : message.command === "resume" ? "active" : "revoked", actor); value = { ok: true } } + else throw Error("Unsupported exact fixture control") + } + transcript.push({ command: message.command, connectionId: currentId }) + child?.send({ kind: "reply", id: message.id, value }) + } catch { child?.send({ kind: "reply", id: message.id, error: true }) } + }) + const timer = setTimeout(() => { failure = { phase: "whole-external-client-deadline" }; child?.kill() }, 120000) + const code = await childExit; clearTimeout(timer) + if (code !== 0 || !result) { console.log("OWNED_EXTERNAL_PROCESS_FAILURE", JSON.stringify({ code, failure, stdout: stdout.slice(0,1000), stderr: stderr.slice(0,1000) })); throw Error("External SDK process did not complete") } + const logs = JSON.stringify((await pool.query("SELECT tool,input,result,error_code FROM mcp_requests ORDER BY created_at")).rows) + assert.equal(revokedLogRequests.length, 3) + assert.ok(planConnectionId) + const invalidated = (await pool.query("SELECT state FROM mcp_plans WHERE connection_id=$1", [planConnectionId])).rows + assert.deepEqual(invalidated, [{ state: "invalidated" }]) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_operations WHERE connection_id=$1", [planConnectionId])).rows[0].count, 0) + assert.equal((await pool.query("SELECT enabled FROM router_log_settings WHERE id=1")).rows[0].enabled, true) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_artifacts WHERE connection_id=$1 AND expires_at>now()", [artifactConnectionId])).rows[0].count, 1) + for (const { connectionId, tool } of revokedLogRequests) { + const revokedRead = (await pool.query("SELECT status,error_code,result FROM mcp_requests WHERE connection_id=$1 AND tool=$2 AND status='error'", [connectionId, tool])).rows + assert.equal(revokedRead.length, 1) + assert.equal(revokedRead[0].status, "error") + assert.deepEqual(Object.keys(revokedRead[0].result), ["message"]) + assert.doesNotMatch(JSON.stringify(revokedRead[0].result), /external-sdk-log-1|external-sdk-live-event|"entries"|"nextCursor"|"cursor"/) + assert.ok(["OWNER_CLAIMS_REVOKED", "UNAUTHORIZED"].includes(revokedRead[0].error_code)) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_read_leases WHERE connection_id=$1", [connectionId])).rows[0].count, 0) + } + for (const token of [...credentials, jwt, secret]) { assert(!logs.includes(token)); assert(!stdout.includes(token)); assert(!stderr.includes(token)) } + const final = { ...(result as object), actualOwnPg: true, actualProductionHttpServer: true, serverFixturePid: process.pid, + controlTransitions: transcript, auditCredentialLeakAbsent: true, denialWithoutConnection: true, + legacyDenialIgnoresPausedForeignConnection: true, nativeRouterCalls: 0, productionDatabaseTouched: false } + await writeFile(process.env.OWN_EXTERNAL_RESULT_FILE!, JSON.stringify(final, null, 2)) + console.log("Owned external process SDK result:", JSON.stringify(final)) + } +} finally { + pool.connect = originalConnect + releaseLogRead?.() + try { + await drainChildAndBridges() + } finally { + try { + if (app) await app.close() + } finally { + try { + await stopSchedulerForDatabaseOperation() + } finally { + await closePool() + } + } + } +} diff --git a/backend/scripts/mcp-external-process/stdio-follow.test.mjs b/backend/scripts/mcp-external-process/stdio-follow.test.mjs new file mode 100644 index 00000000..96be4004 --- /dev/null +++ b/backend/scripts/mcp-external-process/stdio-follow.test.mjs @@ -0,0 +1,73 @@ +import assert from 'node:assert/strict' +import { createServer } from 'node:http' +import { fileURLToPath } from 'node:url' +import { test } from 'node:test' +import { Client } from '@modelcontextprotocol/client' +import { StdioClientTransport } from '@modelcontextprotocol/client/stdio' +import { createMcpHandler, McpServer } from '@modelcontextprotocol/server' +import { toNodeHandler } from '@modelcontextprotocol/node' + +test('stdio cancellation reaches the in-flight upstream logs follow', { timeout: 15000 }, async () => { + const entered = Promise.withResolvers() + const cancelled = Promise.withResolvers() + const finish = Promise.withResolvers() + const handler = createMcpHandler(() => { + const server = new McpServer({ name: 'owned-stdio-follow', version: '1.0.0' }) + server.server.registerCapabilities({ tools: {} }) + server.server.setRequestHandler('tools/call', async ({ params }, context) => { + assert.equal(params.name, 'mm_logs_follow') + const signal = context.mcpReq.signal + const abort = () => { cancelled.resolve(); finish.resolve() } + if (signal.aborted) abort() + else signal.addEventListener('abort', abort, { once: true }) + entered.resolve() + try { await finish.promise } finally { signal.removeEventListener('abort', abort) } + return { content: [] } + }) + return server + }, { legacy: 'stateless' }) + const dispatch = toNodeHandler(handler) + const http = createServer((req, res) => { + res.once('close', () => { + if (req.method === 'POST' && !res.writableFinished) { cancelled.resolve(); finish.resolve() } + }) + void dispatch(req, res) + }) + const client = new Client({ name: 'owned-stdio-client', version: '1.0.0' }) + const within = (promise, phase) => { + const signal = AbortSignal.timeout(5000) + return new Promise((resolve, reject) => { + const abort = () => reject(new Error(phase, { cause: signal.reason })) + signal.addEventListener('abort', abort, { once: true }) + promise.then(resolve, reject).finally(() => signal.removeEventListener('abort', abort)) + }) + } + try { + await new Promise(resolve => http.listen(0, '127.0.0.1', resolve)) + const transport = new StdioClientTransport({ + command: process.execPath, + args: ['--import', import.meta.resolve('tsx'), fileURLToPath(new URL('../../src/scripts/mcp-stdio.ts', import.meta.url))], + env: { MCP_URL: `http://127.0.0.1:${http.address().port}/mcp`, MCP_TOKEN: 'synthetic-stdio-test' }, + stderr: 'pipe', + }) + const outbound = [] + const send = transport.send.bind(transport) + transport.send = (message, options) => { outbound.push(message.method); return send(message, options) } + await within(client.connect(transport), 'stdio connection deadline') + const caller = new AbortController() + const call = client.callTool({ name: 'mm_logs_follow', arguments: {} }, { signal: caller.signal }) + const rejected = assert.rejects(call) + void rejected.catch(() => {}) + await within(entered.promise, 'upstream did not receive follow') + caller.abort() + assert(outbound.includes('notifications/cancelled'), JSON.stringify(outbound)) + await within(Promise.all([cancelled.promise, rejected]), 'upstream did not observe cancellation') + await within(client.ping(), 'bridge did not remain usable') + } finally { + finish.resolve() + await client.close() + http.closeAllConnections() + await handler.close() + await new Promise(resolve => http.close(resolve)) + } +}) diff --git a/backend/scripts/provision-database-source-binding.mjs b/backend/scripts/provision-database-source-binding.mjs new file mode 100644 index 00000000..b1791f7e --- /dev/null +++ b/backend/scripts/provision-database-source-binding.mjs @@ -0,0 +1,10 @@ +// Explicit offline command only. Run with the TS loader; never startup/import. +import { databaseSourceBindingOperatorCli } from './database-source-binding-operator.ts' +try { + const result = await databaseSourceBindingOperatorCli(process.argv.slice(2)) + console.log(JSON.stringify(result, null, 2)) +} catch { + // Do not reflect request contents, key data or untrusted filenames. + console.error('Offline operator source-binding provisioning refused; inspect the explicitly selected output before any new manual invocation') + process.exitCode = 1 +} diff --git a/backend/scripts/source57-publication-owned.mjs b/backend/scripts/source57-publication-owned.mjs new file mode 100644 index 00000000..105bd566 --- /dev/null +++ b/backend/scripts/source57-publication-owned.mjs @@ -0,0 +1,115 @@ +import assert from 'node:assert/strict' +import {access,mkdir,readFile} from 'node:fs/promises' +import {join} from 'node:path' + +export async function verifyOwnedSource57Publication({pg,control,operator,actor,main,directory,store,sha,bootstrapMaterial,bootstrap,request,pin,registry,tls, + activation,activationDirectory,publisherTransport,publisherTransportSha256,controlTransport,controlTransportSha256,ca,quarantine,publishSource58}){ + const [{collectSource57Catalogue},{BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE},{readProtectedRegistryStructureData}, + {readCurrentApplicationWriterProfile},{sourceBindingBytes},{bytes,canonical},{retainPublisherTlsPeer}]=await Promise.all([ + import('./writer-profile/catalogue.mts'),import('./writer-profile/fixed57-build-definition.ts'), + import('../src/db/application-writer-profile-registry-structure.ts'),import('../src/db/application-writer-profile-registry-read.ts'), + import('../src/db/database-source-binding.ts'),import('../src/db/application-writer-profile-format.ts'),import('./writer-profile/operator-contract.mjs'), + ]) + const bootstrapDir=join(directory,'source57-offline-bootstrap'),journal=join(directory,'source57-publication-journal') + await mkdir(bootstrapDir,{mode:0o700});await mkdir(journal,{mode:0o700}) + const bootstrapFile=await store('source57-offline-bootstrap/bootstrap.json',bootstrap) + await store('source57-offline-bootstrap/signing-private.pem',bootstrapMaterial.privateKeyPem) + const definition=sourceBindingBytes({format:'application-writer-source57-definition-approval-v1',provenance:BUILD57_PROVENANCE,template:FIXED57_ROLE_TEMPLATE}) + const definitionFile=await store('source57-definition.json',definition) + const subject=bootstrapMaterial.bootstrap.verificationSubject + + // Review the already-provisioned isolated source through a separate ordinary + // operator backend, rather than letting the product publisher derive approval. + const approvedCatalogue=await (async()=>{ + const client=await operator.connect() + try{ + const retain=retainPublisherTlsPeer(client,tls) + await client.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + const [base,security]=[await collectSource57Catalogue(client,{roleBindings:request.roles}),await readProtectedRegistryStructureData(client,request.roles)] + retain() + return sha(canonical({base:base.catalogSha256,security:security.sha256})) + }finally{await client.query('ROLLBACK');client.release(true)} + })() + const shared=['--bootstrap',bootstrapFile,'--bootstrap-sha256',sha(bootstrap),'--transport',publisherTransport,'--transport-sha256',publisherTransportSha256, + '--ca',ca,'--source-registry',pin.registryDirectory,'--source-spki-sha256',pin.publicKeySpkiSha256, + '--definition',definitionFile,'--definition-sha256',sha(definition),'--journal',journal, + '--activation-directory',activationDirectory,'--activation-sha256',sha(activation)] + const approval=async(expectedHeadSha256,name)=>{ + const payload=sourceBindingBytes({format:'application-writer-publication-approval-v1',manifestSha256:BUILD57_PROVENANCE.manifestSha256, + definitionSha256:BUILD57_PROVENANCE.templateSha256,catalogSha256:approvedCatalogue,expectedHeadSha256,validityMs:120000,expiresAt:Date.now()+90000}) + return ['--approval',await store(name,payload),'--approval-sha256',sha(payload)] + } + const observe=async()=>{ + const connection=await operator.connect() + try{ + const retained=retainPublisherTlsPeer(connection,tls) + await connection.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + const current=await readCurrentApplicationWriterProfile(connection,subject) + retained() + return current + }finally{await connection.query('ROLLBACK');connection.release(true)} + } + const genesisArgs=await approval(null,'source57-genesis-approval.json') + const genesis=await main(['publish',...shared,...genesisArgs]) + assert.equal(genesis.epoch,'1');assert.equal(genesis.cleanupUnconfirmed,false);assert.equal(genesis.journalUnconfirmed,false) + const first=await observe() + assert.equal(first.epoch,'1');assert.equal(first.headSha256,genesis.headSha256);assert.equal(first.policy.catalogSha256,approvedCatalogue) + assert.deepEqual(first.policy.source,request.source) + assert.deepEqual(first.policy.roles,Object.fromEntries(Object.entries(request.roles).map(([name,role])=>[name,role.signedOid]))) + + if(!quarantine){ + await assert.rejects(main(['publish',...shared,...genesisArgs]),error=>error.cause?.message==='Expected protected current head mismatch') + assert.equal((await observe()).headSha256,first.headSha256) + } + const renewArgs=await approval(genesis.headSha256,'source57-renewal-approval.json') + const renewed=await main(['publish',...shared,...renewArgs]) + assert.equal(renewed.epoch,'2');assert.equal(renewed.cleanupUnconfirmed,false);assert.equal(renewed.journalUnconfirmed,false) + const second=await observe();assert.equal(second.epoch,'2');assert.equal(second.headSha256,renewed.headSha256) + const {rows:links}=await control.query(`SELECT epoch::text,head_sha256,previous_head_sha256 + FROM mmapp_control.application_writer_profile_history WHERE installation_id=$1 ORDER BY epoch`,[subject.installationId]) + assert.deepEqual(links,[{epoch:'1',head_sha256:genesis.headSha256,previous_head_sha256:null}, + {epoch:'2',head_sha256:renewed.headSha256,previous_head_sha256:genesis.headSha256}]) + const preflight=['source-preflight',...shared,...renewArgs] + const checked=await main(preflight) + assert.equal(checked.outcome,'matched');assert.equal(checked.headSha256,renewed.headSha256);assert.equal(checked.platformProtectionVerified,true) + if(quarantine){ + const {verifyOwnedSource58RestoreEnrollment}=await import('./source58-restore-enrollment-owned.mjs') + return await verifyOwnedSource58RestoreEnrollment({pg,control,actor,main,store,sha,request,approvedCatalogue,subject,renewed,observe, + bootstrapFile,bootstrap,controlTransport,controlTransportSha256,ca,shared,publishSource58}) + } + + const staleActivation=[...preflight] + staleActivation[staleActivation.indexOf('--activation-sha256')+1]='0'.repeat(64) + await assert.rejects(main(staleActivation),/Active activation digest changed/) + const replaceSignature=value=>({...value,signature:(value.signature[0]==='A'?'B':'A')+value.signature.slice(1)}) + const badRegistry=join(directory,'source57-invalid-source-signature');await mkdir(badRegistry,{mode:0o700}) + const headName='head-'+pin.currentHeadSha256+'.json',bindingName='binding-'+registry.head.bindingSha256+'.json' + const invalid=sourceBindingBytes(replaceSignature(JSON.parse(await readFile(join(pin.registryDirectory,headName),'utf8')))) + await store('source57-invalid-source-signature/operator-public.pem',await readFile(join(pin.registryDirectory,'operator-public.pem'))) + await store('source57-invalid-source-signature/'+bindingName,await readFile(join(pin.registryDirectory,bindingName))) + await store('source57-invalid-source-signature/head-'+sha(invalid)+'.json',invalid) + const refusedActivation=join(directory,'source57-invalid-activation');await mkdir(refusedActivation,{mode:0o700}) + const activationApproval=sourceBindingBytes({format:'database-source-activation-approval-v1',activationDirectory:refusedActivation, + sourcePin:{...pin,registryDirectory:badRegistry,currentHeadSha256:sha(invalid)},tlsSpkiSha256:tls.spkiSha256,expectedActivationSha256:null}) + await assert.rejects(main(['source-activate','--directory',refusedActivation,'--approval',await store('source57-invalid-activation-approval.json',activationApproval), + '--approval-sha256',sha(activationApproval)]),{code:'DATABASE_SOURCE_BINDING_REFUSED'}) + await assert.rejects(access(join(refusedActivation,'source-activation.json')),{code:'ENOENT'}) + + const {rows:[previous]}=await control.query(`SELECT head_bytes FROM mmapp_control.application_writer_profile_history WHERE installation_id=$1 AND epoch=1`,[subject.installationId]) + const replacePredecessor=raw=>control.query(`UPDATE mmapp_control.application_writer_profile_history SET head_bytes=$2 + WHERE installation_id=$1 AND epoch=1`,[subject.installationId,raw]) + await replacePredecessor(bytes(replaceSignature(JSON.parse(previous.head_bytes.toString('utf8'))))) + try{ + await assert.rejects(observe(),/Installation signature refused/) + await assert.rejects(main(preflight),error=>error.cause?.message==='Installation signature refused') + }finally{await replacePredecessor(previous.head_bytes)} + assert.equal((await observe()).headSha256,renewed.headSha256) + const runtime=actor('runtime') + try{await assert.rejects(readCurrentApplicationWriterProfile(runtime,subject),{code:'42501'})}finally{await runtime.end()} + const {rows:[gate]}=await control.query("SELECT pg_catalog.to_regclass('mmapp_control.database_restore_quarantine') IS NULL AS absent") + assert.equal(gate.absent,true) + return {source57PublicationVerified:true,exactRetainedSourcePreflight:true,independentCatalogueReviewed:true,publishedEpochs:['1','2'], + signedGenesisAndPredecessorVerified:true,signedPredecessorTamperingRefused:true,wrongWriterCasRejected:true,activationDigestTamperingRefused:true, + forgedSourceHeadSignatureRefused:true,runtimeRegistryReadRefusedBefore58:true,catalogueSha256:approvedCatalogue,writerHeadSha256:renewed.headSha256, + source58QuarantineInstalled:false,readyAuthorized:false} +} diff --git a/backend/scripts/source58-foundation-owned.mjs b/backend/scripts/source58-foundation-owned.mjs new file mode 100644 index 00000000..8cb30027 --- /dev/null +++ b/backend/scripts/source58-foundation-owned.mjs @@ -0,0 +1,169 @@ +import assert from 'node:assert/strict' +import {generateKeyPairSync,randomBytes,randomUUID,sign,X509Certificate} from 'node:crypto' +import {mkdir,writeFile} from 'node:fs/promises' +import {basename,dirname,join,resolve} from 'node:path' +import {tmpdir} from 'node:os' +import {modelOwnedOperatorFiles} from './legacy-probe-cold-verify-owned.mjs' + +export async function verifyOwnedSource58Foundation(pg,options,leaf,directory,admission=false){ + assert.equal(options.host,'127.0.0.1');assert.equal(options.database,'postgres');assert.equal(options.user,'archive_leaf_fixture') + assert.equal(dirname(directory),resolve(tmpdir()));assert.match(basename(directory),/^mm-archive-leaf-tls-/) + process.env.DATABASE_URL=`postgres://${options.user}:${options.password}@127.0.0.1:${options.port}/postgres` + const [{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry},{provisionDatabaseSourceBindingOffline}, + {migrationManifestDigest},{RESTORE_WRITER_PROFILE_DEFINITION_SHA256},{captureMigrationSchema},{applySqlMigrations},{createBootstrapMaterial}]=await Promise.all([ + import('../src/db/database-source-binding.ts'),import('./database-source-binding-operator.ts'),import('../src/db/migration-manifest.ts'), + import('../src/db/database-restore-quarantine-profile.ts'),import('../src/db/migration-schema-inventory.ts'),import('../src/db/migrate.ts'), + import('./database-writer-bootstrap-operator.mjs'), + ]) + const control=new pg.Pool(options),passwords={},names={control:options.user,operator:'owned_source58_operator',publisher:'owned_source58_publisher', + runtime:'owned_source58_runtime',capture:'owned_source58_capture'} + const sha=sourceBindingSha256,store=async(name,bytes)=>{const file=join(directory,name);await writeFile(file,bytes,{flag:'wx',mode:0o600});return file} + const restoreProfile=admission==='quarantine'||admission==='upgrade' + const publishes=admission==='publication'||restoreProfile + let publisher,operator,modeled + try{ + for(const scope of ['operator','publisher','runtime','capture']){ + passwords[scope]=randomBytes(24).toString('hex') + await control.query(`CREATE ROLE ${names[scope]} LOGIN PASSWORD '${passwords[scope]}'`) + } + await control.query(`REVOKE ALL ON DATABASE postgres FROM PUBLIC; + GRANT CONNECT ON DATABASE postgres TO ${[names.operator,names.publisher,names.runtime,names.capture].join(', ')}; + ALTER SCHEMA public OWNER TO ${names.operator}; CREATE SCHEMA mmapp_control; + GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${names.operator}, ${names.publisher}`) + const actor=scope=>new pg.Pool({...options,user:names[scope],password:passwords[scope]}) + publisher=actor('publisher');operator=actor('operator') + await applySqlMigrations(operator) + const {rows:[source]}=await publisher.query(`SELECT current_database() AS "databaseName",current_user::text AS "currentUser",session_user::text AS "sessionUser", + (pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier",pg_catalog.pg_is_in_recovery() AS "inRecovery", + current_setting('server_version_num')::int AS "serverVersionNum",d.oid::int AS "databaseOid" + FROM pg_catalog.pg_database d WHERE d.datname=current_database()`) + const {rows:liveRoles}=await control.query('SELECT rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1::text[])',[Object.values(names)]) + const roles=Object.fromEntries(Object.entries(names).map(([scope,name])=>[scope,liveRoles.find(role=>role.name===name)])) + const sourceKey=generateKeyPairSync('ed25519'),approver=generateKeyPairSync('ed25519') + const signer=await store('foundation-source-key.pem',sourceKey.privateKey.export({format:'pem',type:'pkcs8'})) + const {deploymentPin:pin}=await provisionDatabaseSourceBindingOffline({request:{installationId:randomUUID(),sourceIdentityId:randomUUID(),generation:randomUUID(),source}, + privateKeyFile:signer,outputDirectory:join(directory,'foundation-source-registry')}) + const registry=await readDatabaseSourceBindingRegistry(pin) + const tls={serverName:'localhost',caSha256:sha(leaf),spkiSha256:sha(new X509Certificate(leaf).publicKey.export({format:'der',type:'spki'}))} + const request={format:'application-writer-bootstrap-request-v1',installationId:pin.installationId,sourceHeadSha256:registry.headSha256,roles,tls, + source:{systemIdentifier:source.systemIdentifier,databaseOid:String(source.databaseOid),databaseName:source.databaseName,serverVersionNum:source.serverVersionNum,inRecovery:false}} + const bootstrapMaterial=createBootstrapMaterial(request,sha(sourceBindingBytes(request))) + const bootstrap=sourceBindingBytes(bootstrapMaterial.bootstrap) + const activationDirectory=join(directory,'foundation-activation');await mkdir(activationDirectory,{mode:0o700}) + const activation=sourceBindingBytes({format:'database-source-activation-v1',activationDirectory,installationId:pin.installationId,sourceIdentityId:registry.body.sourceIdentityId, + status:'active',publicKeySpkiSha256:pin.publicKeySpkiSha256,generations:[{generation:registry.body.generation,headSha256:registry.headSha256, + bindingSha256:registry.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]}) + await store('foundation-activation/source-activation.json',activation) + const states=async(includeControlEventTriggers=false)=>{ + const client=await control.connect() + try{ + await client.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + await client.query("SET LOCAL search_path=pg_catalog; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'; SET LOCAL statement_timeout='5s'") + return await captureMigrationSchema(client,{scope:{applicationNamespaces:['public','mmapp_control']},includeControlEventTriggers}) + }finally{await client.query('ROLLBACK');client.release(true)} + } + const baseline=await states() + modeled=await modelOwnedOperatorFiles(directory,sha) + const [{main},{SOURCE58_FOUNDATION_DOMAIN,source58FoundationSqlSha256}]=await Promise.all([ + import('./database-writer-profile-operator.mts'),import('./writer-profile/source58-foundation.mts'), + ]) + const manifestSha256=migrationManifestDigest(),foundationSha256=source58FoundationSqlSha256(roles) + const approved={format:'source58-foundation-approval-v1',decision:'install-foundations',bootstrapSha256:sha(bootstrap), + sourceHeadSha256:registry.headSha256,sourceTlsSpkiSha256:tls.spkiSha256,manifestSha256,definitionSha256:RESTORE_WRITER_PROFILE_DEFINITION_SHA256, + preSchemaSha256:baseline.sha256,foundationSha256,expiresAt:Date.now()+90000} + const args=['source58-foundation-enroll','--bootstrap',await store('foundation-bootstrap.json',bootstrap),'--bootstrap-sha256',sha(bootstrap), + '--source-registry',pin.registryDirectory,'--source-spki-sha256',pin.publicKeySpkiSha256,'--activation-directory',activationDirectory, + '--activation-sha256',sha(activation),'--ca',await store('foundation-ca.pem',leaf), + '--approval-public-key',await store('foundation-approval-pub.pem',approver.publicKey.export({format:'pem',type:'spki'})), + '--approval-spki-sha256',sha(approver.publicKey.export({format:'der',type:'spki'}))] + for(const [scope,password] of [['publisher',passwords.publisher],['control',options.password]]){ + const transport=sourceBindingBytes({host:'localhost',port:options.port,user:names[scope],password,database:options.database}) + args.push('--'+scope+'-transport',await store('foundation-'+scope+'-transport.json',transport),'--'+scope+'-transport-sha256',sha(transport)) + } + const approve=async(body,name)=>{ + const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(SOURCE58_FOUNDATION_DOMAIN+sourceBindingCanonical(body)),approver.privateKey).toString('base64url')}) + return [...args,'--approval',await store(name,bytes),'--approval-sha256',sha(bytes)] + } + if(!publishes){ + await assert.rejects(main(await approve({...approved,preSchemaSha256:'0'.repeat(64)},'foundation-wrong-base.json')),/Exact signed pre-foundation catalogue required/) + assert.equal((await states()).sha256,baseline.sha256) + } + const install=await approve(approved,'foundation-approval.json'),result=await main(install) + assert.equal(result.event,'database-source58-foundation');assert.equal(result.outcome,'committed');assert.equal(result.foundationSha256,foundationSha256) + assert.equal(result.readyAuthorized,false);assert.equal(result.cleanupUnconfirmed,false) + if(!publishes){ + await assert.rejects(main(install),/Unenrolled staging control namespace and marker required/) + await assert.rejects(operator.query('CREATE TABLE public.owned_foundation_ddl(id int)'),{code:'55000'}) + const connection=await operator.connect() + try{ + await connection.query('BEGIN') + await connection.query('SELECT pg_catalog.pg_advisory_xact_lock_shared(8723110::bigint)') + await connection.query('CREATE TABLE public.owned_foundation_ddl(id int)') + }finally{await connection.query('ROLLBACK');connection.release(true)} + } + const {rows:[state]}=await control.query(`SELECT + (SELECT count(*)::int FROM mmapp_control.application_writer_profile_current) AS heads, + (SELECT count(*)::int FROM mmapp_control.application_writer_profile_history) AS history, + pg_catalog.to_regclass('mmapp_control.database_restore_quarantine') IS NULL AS quarantine_absent, + (SELECT count(*)::int FROM pg_catalog.pg_trigger WHERE tgrelid='public.schema_migrations'::regclass AND NOT tgisinternal) AS marker_triggers`) + assert.deepEqual(state,{heads:0,history:0,quarantine_absent:true,marker_triggers:0}) + if(admission){ + const [{SOURCE57_ADMISSION_DOMAIN},{source57AdmissionSqlSha256},{BUILD57_PROVENANCE}, + {assertReviewedMigrationMarkerGuard},{collectSource57Catalogue},{readProtectedRegistryStructureData},{canonical}]=await Promise.all([ + import('./writer-profile/source58-foundation.mts'),import('./writer-profile/source57-admission-plan.mts'),import('./writer-profile/fixed57-build-definition.ts'), + import('../src/db/partition-guard-inheritance.ts'),import('./writer-profile/catalogue.mts'), + import('../src/db/application-writer-profile-registry-structure.ts'),import('./writer-profile/application-writer-profile-canonical.ts'), + ]) + const predecessor=await states(true),admissionSha256=source57AdmissionSqlSha256(roles) + const {foundationSha256:_foundation,...common}=approved + const body={...common,format:'source57-admission-approval-v1',decision:'install-admission', + definitionSha256:BUILD57_PROVENANCE.templateSha256,preSchemaSha256:predecessor.sha256,admissionSha256,expiresAt:Date.now()+90000} + const issue=async(body,name)=>{ + const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(SOURCE57_ADMISSION_DOMAIN+sourceBindingCanonical(body)),approver.privateKey).toString('base64url')}) + return ['source57-admission-enroll',...args.slice(1),'--approval',await store(name,bytes),'--approval-sha256',sha(bytes)] + } + if(!publishes){ + await assert.rejects(main(await issue({...body,preSchemaSha256:'0'.repeat(64)},'admission-wrong-base.json')),/Exact signed pre-admission catalogue required/) + assert.equal((await states(true)).sha256,predecessor.sha256) + } + const admissionArgs=await issue(body,'admission-approval.json'),result=await main(admissionArgs) + assert.equal(result.admissionSha256,admissionSha256);assert.equal(result.readyAuthorized,false) + if(publishes){ + const {verifyOwnedSource57Publication}=await import('./source57-publication-owned.mjs') + return await verifyOwnedSource57Publication({pg,control,operator,actor,main,directory,store,sha,bootstrapMaterial,bootstrap,request,pin,registry,tls, + activation,activationDirectory,publisherTransport:args[args.indexOf('--publisher-transport')+1],publisherTransportSha256:args[args.indexOf('--publisher-transport-sha256')+1], + controlTransport:args[args.indexOf('--control-transport')+1],controlTransportSha256:args[args.indexOf('--control-transport-sha256')+1], + ca:args[args.indexOf('--ca')+1],quarantine:restoreProfile,publishSource58:admission==='upgrade'}) + } + await assert.rejects(main(admissionArgs),/Unenrolled public relations and empty Source57 writer registry required/) + const markerReader=await operator.connect() + try{ + await markerReader.query('BEGIN') + await markerReader.query('SELECT pg_catalog.pg_advisory_xact_lock_shared(8723110::bigint)') + await assertReviewedMigrationMarkerGuard(markerReader) + }finally{await markerReader.query('ROLLBACK');markerReader.release(true)} + await assert.rejects(operator.query('TRUNCATE public.schema_migrations'),{code:'55000'}) + const runtime=actor('runtime') + try{ + assert.equal((await runtime.query('SELECT count(*)::int AS n FROM public.schema_migrations')).rows[0].n,57) + await assert.rejects(runtime.query('UPDATE public.schema_migrations SET applied_at=now()'),{code:'42501'}) + }finally{await runtime.end()} + const verified=await publisher.connect() + try{ + await verified.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + const [catalogue,security]=[await collectSource57Catalogue(verified,{roleBindings:roles}),await readProtectedRegistryStructureData(verified,roles)] + assert.equal(sha(canonical({base:catalogue.catalogSha256,security:security.sha256})),result.catalogueSha256) + }finally{await verified.query('ROLLBACK');verified.release(true)} + return {source57AdmissionInstalled:true,exactSource57CatalogueVerified:true,admissionSha256,catalogueSha256:result.catalogueSha256, + realOperatorReviewedMarkerAccepted:true,unadmittedMarkerTruncateRefused:true,runtimeMarkerUpdateRefused:true,existing57MarkersPreserved:true, + signedWrongPredecessorRefused:true,replayRefused:true,publishedWriterHead:false,restoreQuarantineInstalled:false,readyAuthorized:false} + } + return {source58FoundationInstalled:true,signedWrongPredecessorRefused:true,replayRefused:true,operatorDdlWithoutAttemptLockRefused:true, + operatorDdlUnderAttemptLockVerified:true,registryEmpty:true,markerCohortInstalled:false,readyAuthorized:false, + foundationSha256,registryStructureSha256:result.registryStructureSha256} + }finally{ + modeled?.restore() + await Promise.all([control.end(),publisher?.end(),operator?.end()]) + await (await import('../src/db/index.ts')).closePool() + } +} diff --git a/backend/scripts/source58-publication-owned.mjs b/backend/scripts/source58-publication-owned.mjs new file mode 100644 index 00000000..f4c6d2a4 --- /dev/null +++ b/backend/scripts/source58-publication-owned.mjs @@ -0,0 +1,66 @@ +import assert from 'node:assert/strict' + +export async function verifyOwnedSource58Publication({control,actor,main,store,sha,request,subject,renewed,approvedCatalogue,candidate, + candidateArgs,inspect,manifestSha256,definitionSha256}){ + const [{sourceBindingBytes},{bytes},{approvedRuntimeWriterProfile},{retainPublisherTlsPeer}]=await Promise.all([ + import('../src/db/database-source-binding.ts'),import('../src/db/application-writer-profile-format.ts'), + import('../src/db/database-runtime-writer-profile.ts'),import('./writer-profile/operator-contract.mjs'), + ]) + const catalogueApproval=async(format,name,previousCatalogue)=>{ + const payload=sourceBindingBytes({format,manifestSha256,definitionSha256,catalogSha256:candidate.sha256, + expectedHeadSha256:renewed.headSha256,validityMs:120000,expiresAt:Date.now()+90000, + ...(previousCatalogue?{previousCatalogSha256:previousCatalogue}:{})}) + return ['--approval',await store(name,payload),'--approval-sha256',sha(payload)] + } + const readRuntime=async(profile)=>{ + const runtime=actor('runtime'),reader=await runtime.connect() + try{ + const retain=retainPublisherTlsPeer(reader,request.tls) + await reader.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + const {rows:[identity]}=await reader.query(`SELECT oid::text AS oid,current_database() AS db + FROM pg_catalog.pg_roles WHERE rolname=SESSION_USER AND SESSION_USER=CURRENT_USER AND NOT (rolsuper OR rolcreaterole OR rolcreatedb OR rolbypassrls)`) + assert.deepEqual(identity,{oid:request.roles.runtime.signedOid,db:request.source.databaseName}) + const result=await profile.check(reader);retain();profile.assertLive() + return result + }finally{try{await reader.query('ROLLBACK')}finally{reader.release(true);await runtime.end()}} + } + const runtimeApproval=(epoch,head)=>approvedRuntimeWriterProfile(sourceBindingBytes({format:'database-runtime-writer-approval-v1', + publicKeyPem:subject.publicKeyPem,publicKeySpkiSha256:subject.publicKeySpkiSha256,minimumEpoch:epoch,minimumHeadSha256:head}), + {installationId:subject.installationId,source:request.source,roles:Object.fromEntries(Object.entries(request.roles).map(([scope,role])=>[scope,role.signedOid]))}) + await assert.rejects(readRuntime(runtimeApproval(renewed.epoch,renewed.headSha256)),/подписанный профиль записи PostgreSQL не подтверждён/) + const upgradeApproval=await catalogueApproval('application-writer-upgrade57-to58-approval-v1','restore58-upgrade-approval.json',approvedCatalogue) + const published=await main(['upgrade',...candidateArgs,...upgradeApproval]) + assert.equal(published.epoch,'3');assert.equal(published.outcome,'committed') + assert.equal(published.cleanupUnconfirmed,false);assert.equal(published.journalUnconfirmed,false) + assert.equal(published.readyAuthorized,false) + + const inspectArgs=['source-catalogue-inspect',...inspect] + const verified=await main(inspectArgs) + assert.equal(verified.outcome,'observed');assert.equal(verified.epoch,'3') + assert.equal(verified.headSha256,published.headSha256);assert.equal(verified.catalogSha256,candidate.sha256) + const preflightApproval=await catalogueApproval('application-writer-publication-approval-v1','restore58-preflight-approval.json') + const confirmed=await main(['source-preflight',...candidateArgs,...preflightApproval]) + assert.equal(confirmed.outcome,'matched');assert.equal(confirmed.headSha256,published.headSha256) + const runtimeProfile=runtimeApproval('3',published.headSha256) + const current=await readRuntime(runtimeProfile) + assert.equal(current.epoch,'3');assert.equal(current.sha256,published.headSha256) + + const {rows:[predecessor]}=await control.query(`SELECT head_bytes,head_sha256 FROM mmapp_control.application_writer_profile_history + WHERE installation_id=$1 AND epoch=2`,[subject.installationId]) + assert.equal(predecessor.head_sha256,renewed.headSha256) + const parsed=JSON.parse(predecessor.head_bytes.toString('utf8')) + parsed.signature=(parsed.signature[0]==='A'?'B':'A')+parsed.signature.slice(1) + const replace=raw=>control.query(`UPDATE mmapp_control.application_writer_profile_history SET head_bytes=$2 + WHERE installation_id=$1 AND epoch=2`,[subject.installationId,raw]) + await replace(bytes(parsed)) + try{ + await assert.rejects(main(inspectArgs),error=>error.cause?.message==='Installation signature refused') + await assert.rejects(readRuntime(runtimeProfile),/Installation signature refused/) + }finally{await replace(predecessor.head_bytes)} + assert.equal((await readRuntime(runtimeProfile)).sha256,published.headSha256) + assert.equal((await main(inspectArgs)).headSha256,published.headSha256) + return {source58WriterHeadPublished:true,source58PublishedEpoch:published.epoch,source57PredecessorEpoch:renewed.epoch, + source58CatalogueSha256:candidate.sha256,source58HeadSha256:published.headSha256,source58CatalogueInspectionAccepted:true, + source58ProtectedPreflightMatched:true,restrictedRuntimeProfileAccepted:true,prepublicationRuntimeProfileRefused:true, + tamperedPredecessorRefusedByOperatorAndRuntime:true,exactBytesRestoredBothReadersAccepted:true,readyAuthorized:false} +} diff --git a/backend/scripts/source58-restore-enrollment-owned.mjs b/backend/scripts/source58-restore-enrollment-owned.mjs new file mode 100644 index 00000000..8dd92690 --- /dev/null +++ b/backend/scripts/source58-restore-enrollment-owned.mjs @@ -0,0 +1,124 @@ +import assert from 'node:assert/strict' + +export async function verifyOwnedSource58RestoreEnrollment({pg,control,actor,main,store,sha,request,approvedCatalogue,subject,renewed,observe, + bootstrapFile,bootstrap,controlTransport,controlTransportSha256,ca,shared,publishSource58}){ + const [{collectSource57Catalogue,collectSource58Catalogue},{BUILD58_PROVENANCE,FIXED58_ROLE_TEMPLATE}, + {readProtectedRegistryStructureData},{readCurrentApplicationWriterProfile},{restoreProfileEnrollmentSql}, + {sourceBindingBytes},{bytes,canonical,KEY},{retainPublisherTlsPeer}]=await Promise.all([ + import('./writer-profile/catalogue.mts'),import('./writer-profile/fixed58-build-definition.ts'), + import('../src/db/application-writer-profile-registry-structure.ts'),import('../src/db/application-writer-profile-registry-read.ts'), + import('../src/db/database-restore-quarantine-profile.ts'),import('../src/db/database-source-binding.ts'), + import('../src/db/application-writer-profile-format.ts'),import('./writer-profile/operator-contract.mjs'), + ]) + const catalogue=async(client,edition)=>{ + const security=await readProtectedRegistryStructureData(client,request.roles,edition===58) + const base=await (edition===58?collectSource58Catalogue:collectSource57Catalogue)(client,{roleBindings:request.roles}) + return {sha256:sha(canonical({base:base.catalogSha256,security:security.sha256})),securitySha256:security.sha256} + } + const assertQuarantineAbsent=async()=>{ + const {rows:[state]}=await control.query(`SELECT pg_catalog.to_regclass('mmapp_control.database_restore_quarantine') IS NULL AS absent, + pg_catalog.has_schema_privilege($1,'mmapp_control','USAGE') AS runtime_schema, + pg_catalog.has_table_privilege($1,'mmapp_control.application_writer_profile_current','SELECT') AS runtime_registry`,[request.roles.runtime.name]) + assert.deepEqual(state,{absent:true,runtime_schema:false,runtime_registry:false}) + } + const client=await control.connect() + let candidate + try{ + const retain=retainPublisherTlsPeer(client,request.tls) + await client.query('BEGIN') + try{ + await client.query('SET LOCAL search_path=pg_catalog') + await client.query('SELECT pg_catalog.pg_advisory_xact_lock($1::bigint)',[KEY]) + await client.query('LOCK TABLE mmapp_control.application_writer_profile_current, mmapp_control.application_writer_profile_history IN SHARE MODE') + const {rows:[identity]}=await client.query(`SELECT oid::text AS oid, current_database() AS db, + (pg_catalog.pg_control_system()).system_identifier::text AS system, + (SELECT oid::text FROM pg_catalog.pg_database WHERE datname=current_database()) AS database_oid + FROM pg_catalog.pg_roles WHERE rolname=SESSION_USER AND SESSION_USER=CURRENT_USER`) + assert.deepEqual(identity,{oid:request.roles.control.signedOid,db:request.source.databaseName,system:request.source.systemIdentifier,database_oid:request.source.databaseOid}) + const predecessor=await readCurrentApplicationWriterProfile(client,subject) + assert.equal(predecessor.headSha256,renewed.headSha256) + assert.equal(predecessor.policy.catalogSha256,approvedCatalogue) + assert.equal((await catalogue(client,57)).sha256,approvedCatalogue) + await client.query(restoreProfileEnrollmentSql(request.roles.runtime.name,request.roles.operator.name)) + candidate=await catalogue(client,58);retain() + }finally{assert.equal((await client.query('ROLLBACK')).command,'ROLLBACK')} + await client.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + try{ + assert.equal((await catalogue(client,57)).sha256,approvedCatalogue) + assert.equal((await readCurrentApplicationWriterProfile(client,subject)).headSha256,renewed.headSha256) + retain() + }finally{assert.equal((await client.query('ROLLBACK')).command,'ROLLBACK')} + }finally{client.release(true)} + await assertQuarantineAbsent() + assert.notEqual(candidate.sha256,approvedCatalogue) + + const independentApproval=sourceBindingBytes({format:'application-writer-restore-profile-enrollment-v1', + catalogSha256:candidate.sha256,definitionSha256:BUILD58_PROVENANCE.templateSha256,deploymentRequest:request, + expectedHeadSha256:renewed.headSha256,manifestSha256:BUILD58_PROVENANCE.manifestSha256,previousCatalogSha256:approvedCatalogue}) + const enroll=['restore-profile-enroll','--bootstrap',bootstrapFile,'--bootstrap-sha256',sha(bootstrap), + '--approval',await store('restore58-reviewed-approval.json',independentApproval),'--approval-sha256',sha(independentApproval), + '--transport',controlTransport,'--transport-sha256',controlTransportSha256,'--ca',ca] + if(!publishSource58){ + const {rows:[first]}=await control.query(`SELECT head_bytes FROM mmapp_control.application_writer_profile_history + WHERE installation_id=$1 AND epoch=1`,[subject.installationId]) + const envelope=JSON.parse(first.head_bytes.toString('utf8')) + envelope.signature=(envelope.signature[0]==='A'?'B':'A')+envelope.signature.slice(1) + const replace=raw=>control.query(`UPDATE mmapp_control.application_writer_profile_history SET head_bytes=$2 + WHERE installation_id=$1 AND epoch=1`,[subject.installationId,raw]) + await replace(bytes(envelope)) + const connect=pg.Pool.prototype.connect + let ddlSent=0 + pg.Pool.prototype.connect=function(...parameters){ + const connection=connect.apply(this,parameters) + if(parameters.length||this.options.application_name!=='mmapp-writer-registry-enroll')return connection + return connection.then(client=>{ + const query=client.query + client.query=function(text,...args){ + if(typeof text==='string'&&text.includes('CREATE TABLE mmapp_control.database_restore_quarantine'))ddlSent++ + return query.call(this,text,...args) + } + return client + }) + } + try{await assert.rejects(main(enroll),/Installation signature refused/)} + finally{pg.Pool.prototype.connect=connect;await replace(first.head_bytes)} + assert.equal(ddlSent,0);await assertQuarantineAbsent() + } + + const installed=await main(enroll) + assert.equal(installed.outcome,'committed');assert.equal(installed.cleanupUnconfirmed,false) + assert.equal(installed.registryStructureSha256,candidate.securitySha256);assert.equal(installed.readyAuthorized,false) + const definition=sourceBindingBytes({format:'application-writer-source58-definition-approval-v1',provenance:BUILD58_PROVENANCE,template:FIXED58_ROLE_TEMPLATE}) + const candidateArgs=[...shared] + candidateArgs[candidateArgs.indexOf('--definition')+1]=await store('restore58-definition.json',definition) + candidateArgs[candidateArgs.indexOf('--definition-sha256')+1]=sha(definition) + const inspect=[...candidateArgs] + inspect.splice(inspect.indexOf('--journal'),2) + await assert.rejects(main(['source-catalogue-inspect',...inspect]),error=>error.cause?.message==='Approved source58 predecessor required') + if(publishSource58){ + const {verifyOwnedSource58Publication}=await import('./source58-publication-owned.mjs') + return await verifyOwnedSource58Publication({control,actor,main,store,sha,request,subject,renewed,approvedCatalogue,candidate, + candidateArgs,inspect,manifestSha256:BUILD58_PROVENANCE.manifestSha256,definitionSha256:BUILD58_PROVENANCE.templateSha256}) + } + const publisher=actor('publisher'),reader=await publisher.connect() + try{ + const retain=retainPublisherTlsPeer(reader,request.tls) + await reader.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + assert.equal((await catalogue(reader,58)).sha256,candidate.sha256) + assert.equal((await readCurrentApplicationWriterProfile(reader,subject)).headSha256,renewed.headSha256) + retain() + }finally{await reader.query('ROLLBACK');reader.release(true);await publisher.end()} + assert.equal((await observe()).headSha256,renewed.headSha256) + const runtime=actor('runtime') + try{ + assert.equal((await readCurrentApplicationWriterProfile(runtime,subject)).headSha256,renewed.headSha256) + const {rows:[row]}=await runtime.query('SELECT operation_id,journal_prefix_bytes,journal_prefix_sha256 FROM mmapp_control.database_restore_quarantine WHERE id=1') + assert.deepEqual(row,{operation_id:null,journal_prefix_bytes:null,journal_prefix_sha256:null}) + await assert.rejects(runtime.query('UPDATE mmapp_control.database_restore_quarantine SET operation_id=NULL'),{code:'42501'}) + }finally{await runtime.end()} + return {source58RestoreProfileInstalled:true,rolledBackCandidateCount:1,rollbackRestoredExactSource57:true, + independentlyPinnedTargetMatched:true,source57HeadPreserved:true,invalidPredecessorSignatureRefusedBeforeDdl:true, + runtimeReadQuarantineAndRegistry:true,runtimeQuarantineWriteRefused:true,source58CatalogueSha256:candidate.sha256, + source58InspectionStillRequiresPublishedHead:true, + source58WriterHeadPublished:false,readyAuthorized:false} +} diff --git a/backend/scripts/stage-database-operator.mjs b/backend/scripts/stage-database-operator.mjs new file mode 100644 index 00000000..19e93738 --- /dev/null +++ b/backend/scripts/stage-database-operator.mjs @@ -0,0 +1,29 @@ +/** Build-time only: copy the exact operator inputs into a scoped Docker context. */ +import assert from 'node:assert/strict' +import {constants} from 'node:fs' +import {copyFile,lstat,mkdir} from 'node:fs/promises' +import path from 'node:path' +import {fileURLToPath,pathToFileURL} from 'node:url' +import {OPERATOR_SOURCES} from './database-operator-sources.mjs' + +const scriptsDirectory=path.dirname(fileURLToPath(import.meta.url)) +const buildInputs=Object.freeze(['build-database-operator.mjs','database-operator-sources.mjs',...OPERATOR_SOURCES]) + +export async function stageDatabaseOperatorScripts(destination,source=scriptsDirectory){ + const from=path.resolve(source),to=path.resolve(destination) + assert(from!==to,'A separate operator staging directory is required') + // Refuse an incomplete source tree before writing any staged files. + for(const file of buildInputs)assert((await lstat(path.join(from,file))).isFile(),'Regular operator build input required: '+file) + for(const file of buildInputs){ + const target=path.join(to,file) + await mkdir(path.dirname(target),{recursive:true}) + await copyFile(path.join(from,file),target,constants.COPYFILE_EXCL) + } + return buildInputs.length +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(path.resolve(process.argv[1])).href){ + assert(process.argv.length===3&&process.argv[2],'A single Docker scripts destination is required') + const files=await stageDatabaseOperatorScripts(process.argv[2]) + console.log(JSON.stringify({event:'database-operator-inputs-staged',files})) +} diff --git a/backend/scripts/test-database-archive-leaf-tls.mjs b/backend/scripts/test-database-archive-leaf-tls.mjs new file mode 100644 index 00000000..54aee9df --- /dev/null +++ b/backend/scripts/test-database-archive-leaf-tls.mjs @@ -0,0 +1,399 @@ +import assert from 'node:assert/strict' +import {execFile} from 'node:child_process' +import {createHash, generateKeyPairSync, randomBytes, randomUUID, X509Certificate} from 'node:crypto' +import {chmod, copyFile, mkdir, mkdtemp, open, readFile, readdir, realpath, rm, unlink, writeFile} from 'node:fs/promises' +import {createServer} from 'node:net' +import {tmpdir} from 'node:os' +import {basename, dirname, join} from 'node:path' +import {promisify} from 'node:util' + +const mode = process.argv[2] +assert(process.argv.length === 3 && ['--openssl-only', '--owned-postgres', '--owned-legacy', '--owned-legacy-custody', '--owned-legacy-custody-shape', '--owned-legacy-0037-handoff', '--owned-legacy-handoff-inspect', '--owned-legacy-handoff-intent', '--owned-legacy-cold-verify', '--owned-legacy-handoff-execute','--owned-legacy-handoff-adopt','--owned-legacy-handoff-release','--owned-legacy-handoff-tail','--owned-source58-foundation','--owned-source57-admission','--owned-source57-publication','--owned-source58-restore-enrollment','--owned-source58-publication'].includes(mode)) +const openssl = process.env.MMAPP_TLS_FIXTURE_OPENSSL || 'openssl' +const clientBinary = name => process.env.MMAPP_PG_CLIENT_BIN + ? join(process.env.MMAPP_PG_CLIENT_BIN, name + (process.platform === 'win32' ? '.exe' : '')) : name +const run = promisify(execFile) +const temporary = await realpath(tmpdir()) +const directory = await realpath(await mkdtemp(join(temporary, 'mm-archive-leaf-tls-'))) +const execute = (command, args, options = {}) => run(command, args, { + cwd: directory, timeout: 15000, maxBuffer: 1024 * 1024, windowsHide: true, ...options, +}) +const certificate = name => join(directory, `${name}.pem`) +const key = name => join(directory, `${name}.key`) +const host = 'localhost' +const leafExtensions = ['basicConstraints=critical,CA:FALSE', 'keyUsage=critical,digitalSignature,keyEncipherment', + 'extendedKeyUsage=serverAuth', 'subjectAltName=DNS:localhost'] +const request = name => ['req', '-new', '-newkey', 'rsa:2048', '-nodes', '-subj', + `/CN=${name === 'delegated' ? 'Owned delegated fixture' : host}`, '-keyout', key(name)] + +async function issueCertificates() { + await execute(openssl, [...request('pinned'), '-x509', '-days', '2', '-out', certificate('pinned'), + ...leafExtensions.flatMap(extension => ['-addext', extension])]) + await execute(openssl, ['req', '-new', '-x509', '-newkey', 'rsa:2048', '-nodes', '-subj', '/CN=Owned fixture CA', + '-keyout', key('ca'), '-out', certificate('ca'), '-days', '2', '-addext', 'basicConstraints=critical,CA:TRUE', + '-addext', 'keyUsage=critical,keyCertSign,cRLSign']) + const extensions = join(directory, 'leaf.cnf') + await writeFile(extensions, leafExtensions.join('\n') + '\n', {flag:'wx'}) + for (const [name, issuer] of [['substitute', 'ca'], ['delegated', 'pinned']]) { + const csr = join(directory, `${name}.csr`) + await execute(openssl, [...request(name), '-out', csr]) + await execute(openssl, ['x509', '-req', '-in', csr, '-CA', certificate(issuer), '-CAkey', key(issuer), '-set_serial', '3', + '-days', '2', '-extfile', extensions, '-out', certificate(name)]) + } + const [pinned, substitute, delegated] = await Promise.all(['pinned', 'substitute', 'delegated'].map(async name => new X509Certificate(await readFile(certificate(name))))) + const spki = cert => createHash('sha256').update(cert.publicKey.export({type:'spki', format:'der'})).digest('hex') + assert.equal(pinned.ca, false) + assert.equal(pinned.subject, pinned.issuer) + assert(pinned.verify(pinned.publicKey)) + assert(delegated.verify(pinned.publicKey)) + assert.equal(new Set([spki(pinned), spki(substitute), spki(delegated)]).size, 3) + const verify = (trust, name) => execute(openssl, ['verify', '-purpose', 'sslserver', '-verify_hostname', host, + '-no-CApath', '-no-CAstore', '-CAfile', certificate(trust), certificate(name)]) + await verify('pinned', 'pinned') + await verify('ca', 'substitute') + await assert.rejects(verify('pinned', 'substitute'), {stderr: /unable to get local issuer certificate/i}) + await assert.rejects(verify('pinned', 'delegated'), {stderr: /invalid CA certificate|key usage does not include certificate signing/i}) +} + +async function verifyLegacyExport(pg, options, leafBytes, client) { + const holder = new pg.Client(options) + const {port,database} = options + const hash = value => createHash('sha256').update(value).digest('hex') + const pinned = new X509Certificate(leafBytes) + const custody=mode==='--owned-legacy-custody' + if(custody)Object.assign(process.env,{DATABASE_URL:`postgres://${options.user}:${options.password}@127.0.0.1:${port}/${database}`,MCP_STATE_DIR:join(directory,'legacy-app-state'),AUTH_REQUIRED:'false'}) + let app + await holder.connect() + try { + const {rows:[source]} = await holder.query(`SELECT d.oid::text AS "databaseOid", d.datname AS "databaseName", + (pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier", + pg_catalog.current_setting('server_version_num')::int AS "serverVersionNum",false AS "inRecovery" + FROM pg_catalog.pg_database d WHERE d.datname=pg_catalog.current_database()`) + const [{createConnectedPublisher}, {poolOptions}, {MIGRATION_MANIFEST}, {captureLegacyProbeStorage, captureLegacyProbeLineage, LEGACY_PROBE_RELATIONS}] = await Promise.all([ + import('./writer-profile/publisher.mjs'), import('./writer-profile/operator-contract.mjs'), import('../src/db/migration-manifest.ts'), + import('../src/services/server-probe-legacy-source-inventory.ts'), + ]) + const fixtureSchema = `CREATE TABLE public.schema_migrations(id text PRIMARY KEY); + CREATE TABLE public.mcp_plans(id uuid PRIMARY KEY, connection_id uuid, operation_id text, operation_version integer, state text, approved_by text, evidence jsonb); + CREATE TABLE public.mcp_connections(id uuid PRIMARY KEY, owner_id text); + CREATE TABLE public.mcp_operations(id uuid PRIMARY KEY, plan_id uuid, connection_id uuid, state text); + CREATE TABLE public.mcp_operation_steps(operation_id uuid, step_index integer, state text, intended_state jsonb); + CREATE TABLE public.mcp_owner_revocations(owner_id text PRIMARY KEY, invalid_before timestamptz); + CREATE TABLE public.mcp_credentials(id uuid PRIMARY KEY)` + const diagnosticDdl = await Promise.all(MIGRATION_MANIFEST.slice(34,36).map(pin => readFile(new URL(`../drizzle/${pin.file}`, import.meta.url), 'utf8'))) + const historicalMarkers = MIGRATION_MANIFEST.slice(0,34).map(pin => pin.file.slice(0,-4)) + const prepareLegacy = async sql => { + await sql.query(fixtureSchema) + await sql.query('INSERT INTO public.schema_migrations SELECT unnest($1::text[])', [historicalMarkers]) + for (const migration of diagnosticDdl) await sql.query(migration) + } + await prepareLegacy(holder) + if(mode==='--owned-legacy-custody-shape'){ + const inspectShape=async()=>{ + await holder.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + try { await captureLegacyProbeStorage(holder);return await captureLegacyProbeLineage(holder) } + finally { await holder.query('ROLLBACK') } + } + assert.equal((await inspectShape()).rowCount,0) + await holder.query('CREATE TABLE public.owned_inherited_mcp_operations() INHERITS (public.mcp_operations)') + await assert.rejects(inspectShape(),/Exact ordinary legacy custody tables required/) + return + } + // The raw archive also carries unresolved identities. No marker or proof is produced by export. + const anchor = randomUUID(), quarantine = randomUUID(), operation = randomUUID(), connection = randomUUID(), plan = randomUUID(), executionId = randomUUID() + const sourceRows = async sql => { + await sql.query("INSERT INTO public.mcp_operations VALUES($1,$2,$3,'applied')", [operation,plan,connection]) + await sql.query("INSERT INTO public.mcp_connections VALUES($1,'owned-actor')", [connection]) + await sql.query("INSERT INTO public.mcp_plans VALUES($1,$2,'servers.probes.run',1,'completed','owned-actor','{}')", [plan,connection]) + await sql.query("INSERT INTO public.mcp_operation_steps VALUES($1,0,'intent','{}')", [operation]) + } + await sourceRows(holder) + const issuedAt = Date.UTC(2000,0,1)-3600000, expiresAt = Date.UTC(2000,0,1), bindingProof='a'.repeat(64) + await holder.query(`INSERT INTO public.server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool) + VALUES($1,$3,'rest',$2,'owned-actor','jwt','owned-request',$4,$5,1,'ping')`, [anchor,executionId,bindingProof,issuedAt,new Date(expiresAt)]) + await holder.query("INSERT INTO public.server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms) VALUES($1,1,'native-ping',1000)", [anchor]) + if(custody){ + const {encryptSecret, fingerprint, privateSecretFingerprint}=await import('../src/modules/mcp/security.ts') + const context={identity:{kind:'rest',ownerId:'owned-actor',executionId,requestId:'owned-request',authentication:'jwt',issuedAt,expiresAt},sourceServerId:1,tool:'ping'} + const receipt={receiptId:anchor,tool:'ping',serverId:1,state:'completed',httpStatus:200,output:'owned historical receipt',data:{kind:'ping'},attemptedStages:1,completedStages:1,automaticReplay:false,forwardingVerified:false} + const plaintext=JSON.stringify({version:1,context,bindingProof,receipt}) + await holder.query("UPDATE public.server_probe_execution_stages SET state='received',outcome='received',received_at=clock_timestamp() WHERE receipt_id=$1", [anchor]) + await holder.query(`UPDATE public.server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,finished_at=clock_timestamp(),receipt_ciphertext=$2,receipt_proof=$3 WHERE receipt_id=$1`, + [anchor,await encryptSecret(plaintext),await privateSecretFingerprint(fingerprint(plaintext),'server-probe-journal-v1:receipt')]) + } + await holder.query(`INSERT INTO public.server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) + VALUES($1,repeat('b',64),'rest',$2,repeat('c',64),'JOURNAL_UNKNOWN',0,0)`, [quarantine, randomUUID()]) + await holder.query("INSERT INTO public.mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,'owned-actor')", [operation, connection]) + const publisherPassword = randomBytes(24).toString('hex'), publisher = 'archive_legacy_publisher' + const roleNames = {publisher,runtime:'archive_legacy_runtime',capture:'archive_legacy_capture',operator:'archive_legacy_operator',control:'archive_legacy_control'} + await holder.query(`CREATE ROLE ${publisher} LOGIN PASSWORD '${publisherPassword}'; + ${Object.values(roleNames).filter(role => role !== publisher).map(role => `CREATE ROLE ${role} NOLOGIN`).join('; ')}; + GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${publisher}; + GRANT SELECT ON public.schema_migrations, ${LEGACY_PROBE_RELATIONS.map(name => `public.${name}`).join(', ')} TO ${publisher}`) + const {rows:roleRows} = await holder.query('SELECT rolname AS name, oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1)', [Object.values(roleNames)]) + const roles = Object.fromEntries(Object.entries(roleNames).map(([scope,name])=>[scope,roleRows.find(row=>row.name===name)])) + const tls = {serverName:host,caSha256:hash(leafBytes),spkiSha256:hash(pinned.publicKey.export({format:'der', type:'spki'}))} + const transport = {host,port,user:publisher,password:publisherPassword,database} + const installation = {roleBindings:roles,expectedSource:source,tls} + const [{createBootstrapMaterial}, {provisionDatabaseSourceBindingOffline}, {sourceBindingBytes, readDatabaseSourceBindingRegistry}, {verifyLegacyProbeArchiveApproval}] = await Promise.all([ + import('./database-writer-bootstrap-operator.mjs'), import('./database-source-binding-operator.ts'), + import('../src/db/database-source-binding.ts'), import('../src/services/server-probe-legacy-archive-approval.ts'), + ]) + const fixtureFile = async (name, bytes) => { const file=join(directory,name); await writeFile(file,bytes,{flag:'wx',mode:0o600});return file } + const keyFile = await fixtureFile('legacy-source-signer.pem',generateKeyPairSync('ed25519').privateKey.export({type:'pkcs8',format:'pem'})) + const installationId = randomUUID(), sourceIdentityId = randomUUID(), generation = randomUUID() + const registryDirectory = join(directory,'legacy-registry') + const {deploymentPin} = await provisionDatabaseSourceBindingOffline({privateKeyFile:keyFile,outputDirectory:registryDirectory, + request:{installationId,sourceIdentityId,generation,source:{...source,databaseOid:Number(source.databaseOid),sessionUser:publisher,currentUser:publisher}}}) + const boot = {format:'application-writer-bootstrap-request-v1',installationId,sourceHeadSha256:deploymentPin.currentHeadSha256,source,roles,tls} + const bootstrap = sourceBindingBytes(createBootstrapMaterial(boot,hash(sourceBindingBytes(boot))).bootstrap) + const wire = sourceBindingBytes(transport), activationDirectory=join(directory,'legacy-activation'), archiveDirectory=join(directory,'legacy-export') + const registry = await readDatabaseSourceBindingRegistry(deploymentPin) + const activation = sourceBindingBytes({format:'database-source-activation-v1',activationDirectory,installationId,sourceIdentityId,status:'active', + publicKeySpkiSha256:deploymentPin.publicKeySpkiSha256,generations:[{generation,headSha256:registry.headSha256,bindingSha256:registry.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]}) + await mkdir(activationDirectory,{mode:0o700});await mkdir(archiveDirectory,{mode:0o700}) + await fixtureFile('legacy-activation/source-activation.json',activation) + const argv=[custody?'legacy-custody':'legacy-export','--bootstrap',await fixtureFile('legacy-bootstrap.json',bootstrap),'--bootstrap-sha256',hash(bootstrap), + '--transport',await fixtureFile('legacy-transport.json',wire),'--transport-sha256',hash(wire),'--ca',certificate('pinned'), + '--source-registry',registryDirectory,'--source-spki-sha256',deploymentPin.publicKeySpkiSha256, + '--activation-directory',activationDirectory,'--activation-sha256',hash(activation),'--archive-directory',archiveDirectory,'--source-signing-key', keyFile] + let modeledFiles + if(process.platform==='win32'){ + const {mock}=await import('node:test'), filesUrl=new URL('./writer-profile/operator-files.mjs',import.meta.url) + const files=await import(filesUrl.href) + const protectedDirectory=async path=>{assert(path===directory||path.startsWith(directory+'\\'));assert.equal(await realpath(path),path)} + modeledFiles=mock.module(filesUrl,{namedExports:{...files,protectedDirectory,async readProtected(path,limit,pin){ + await protectedDirectory(dirname(path));const bytes=await readFile(path);assert(bytes.length>0&&bytes.length<=limit); + if(pin!==undefined)assert.equal(hash(bytes),pin);return bytes + },writeFile,async withJournalLease(path,work){ + await protectedDirectory(path);const lock=join(path,'publisher-lease.lock'),handle=await open(lock,'wx',0o600) + try{return await work()}finally{await handle.close();await unlink(lock)} + }}}) + } + try { + const {main}=await import('./database-writer-profile-operator.mts') + if(custody){ + await assert.rejects(main(argv),error=>error.cause?.code==='42501') + const lockedTables=['schema_migrations','mcp_plans','mcp_connections','mcp_operations','mcp_operation_steps',...LEGACY_PROBE_RELATIONS].map(name=>`public.${name}`).join(', ') + await holder.query(`GRANT SELECT, MAINTAIN ON TABLE ${lockedTables} TO ${publisher}`) + await holder.query('BEGIN') + try { + await holder.query("UPDATE public.mcp_operations SET state='failed' WHERE id=$1",[operation]) + await assert.rejects(main(argv),error=>error.cause?.code==='55P03') + } finally { await holder.query('ROLLBACK') } + assert.deepEqual(await readdir(archiveDirectory),[]) + } + const legacy=await main(argv) + assert.equal(legacy.event,custody?'database-legacy-custody':'database-legacy-export');assert.equal(legacy.conversionAllowed,false);assert.equal(legacy.readyAuthorized,false) + assert.equal(legacy.snapshot.rowCount,4);assert.equal(JSON.stringify(legacy).includes('owned-actor'),false) + assert.deepEqual((await readdir(archiveDirectory)).sort(),[legacy.approvalFile,legacy.dumpFile].sort()) + const legacyFile=join(archiveDirectory,legacy.dumpFile),archive=await readFile(legacyFile) + const signedApproval=await readFile(join(archiveDirectory,legacy.approvalFile)),operatorPublicKeyPem=await readFile(join(registryDirectory,'operator-public.pem')) + const verified=await verifyLegacyProbeArchiveApproval(deploymentPin,{archive,signedApproval,operatorPublicKeyPem}) + assert.equal(verified.storageSha256,legacy.snapshot.sha256);assert.equal(verified.archiveSha256,legacy.archiveSha256) + await assert.rejects(verifyLegacyProbeArchiveApproval(deploymentPin,{archive:Buffer.concat([archive,Buffer.from('tamper')]),signedApproval,operatorPublicKeyPem})) + await assert.rejects(main(argv),/Empty protected archive directory required/) + const {stdout:legacyToc} = await client('pg_restore',['--list',legacyFile],'pinned') + const entries = legacyToc.split(/\r?\n/).filter(line => line && !line.startsWith(';')) + assert.equal(entries.length,4) + assert.deepEqual(entries.map(line => line.match(/ TABLE DATA public (\S+) /)?.[1]).sort(), [...LEGACY_PROBE_RELATIONS].sort()) + await holder.query('CREATE DATABASE archive_legacy_restored') + const restored = new pg.Client({...options,database:'archive_legacy_restored'}) + await restored.connect() + try { + await prepareLegacy(restored) + await sourceRows(restored) + // A sterile verification target keeps the exact column shapes and + // foreign keys. Production lifecycle triggers have no role in this readback. + await restored.query(`DROP TRIGGER server_probe_execution_immutable ON public.server_probe_executions; + DROP TRIGGER server_probe_stage_immutable ON public.server_probe_execution_stages; + DROP TRIGGER server_probe_quarantine_immutable ON public.server_probe_execution_quarantines; + DROP TRIGGER mcp_diagnostic_cancellation_immutable ON public.mcp_diagnostic_cancellations`) + try { + await client('pg_restore',['--dbname=archive_legacy_restored','--data-only','--exit-on-error','--no-owner',legacyFile],'pinned') + } catch { throw Error('Legacy archive restore failed on the isolated verification database') } + await restored.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + assert.deepEqual(await captureLegacyProbeStorage(restored),legacy.snapshot) + await restored.query('ROLLBACK') + } finally { await restored.end() } + let checks=0 + const legacyPool = new pg.Pool(poolOptions(transport, {roles,source,tls}, leafBytes)) + const operator = createConnectedPublisher(installation, undefined, {async verify() { + if (++checks === 2) throw Error('owned terminal source refusal') + if(custody){ + await holder.query("SET lock_timeout='200ms'") + try { await assert.rejects(holder.query("UPDATE public.mcp_operations SET state='failed' WHERE id=$1",[operation]),{code:'55P03'}) } + finally { await holder.query('RESET lock_timeout') } + } + return {platformProtectionVerified:true} + }}) + try { + await assert.rejects(operator.inspectLegacyStorage(legacyPool,{deadlineAt:Date.now()+30000,archiveDirectory:directory,custody}), + error=>error.cause?.message==='owned terminal source refusal') + assert.equal(checks,2) + } finally { await legacyPool.end() } + if(custody){ + app=await import('../src/db/index.ts') + const {inspectLegacyServerProbeSource}=await import('../src/services/server-probe-legacy-inspection.ts') + const now=Date.now(),actor={id:'owned-historical-settings-admin',email:'',name:'',apps:['mm'],permissions:['mm:settings:admin'],issuedAt:now-1000,expiresAt:now+60000} + const expectation={archive,signedApproval,operatorPublicKeyPem} + const inspect=evidence=>inspectLegacyServerProbeSource(actor,{deadlineAt:Date.now()+30000},undefined,evidence,deploymentPin) + const inspected=await inspect(expectation) + assert.equal(verified.format,'legacy-probe-custody-approval-v1');assert.equal(verified.lineageSha256,legacy.lineageSnapshot.sha256) + assert.equal(legacy.lineageSnapshot.rowCount,4);assert.deepEqual(inspected.lineageSnapshot,legacy.lineageSnapshot) + assert.equal(inspected.summary.counts.verified,1);assert.equal(inspected.summary.counts.unresolved,2) + assert.equal(inspected.summary.automaticReplay,false);assert.equal(inspected.conversionAllowed,false) + await holder.query("UPDATE public.mcp_operation_steps SET intended_state='{\"drift\":true}' WHERE operation_id=$1",[operation]) + await assert.rejects(inspect(expectation),{code:'STATE_UNAVAILABLE'}) + await holder.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + try { assert.deepEqual(await captureLegacyProbeStorage(holder),legacy.snapshot) } + finally { await holder.query('ROLLBACK') } + } + } finally { modeledFiles?.restore() } + } finally { await app?.pool.end();await holder.end() } +} + +async function verifyOwnedPostgres() { + const [{default:EmbeddedPostgres}, {default:pg}] = await Promise.all([import('embedded-postgres'), import('pg')]) + const listener = createServer() + await new Promise((resolve, reject) => listener.once('error', reject).listen(0, '127.0.0.1', resolve)) + const port = listener.address().port + await new Promise((resolve, reject) => listener.close(error => error ? reject(error) : resolve())) + const user = 'archive_leaf_fixture', database = 'postgres', password = randomBytes(24).toString('hex') + const serverCertificate = certificate('server'), serverKey = key('server') + const postgres = new EmbeddedPostgres({databaseDir: join(directory, 'pg'), port, user, password, persistent:true, + createPostgresUser:false, initdbFlags:['--encoding=UTF8'], postgresFlags:['-h', '127.0.0.1', '-c', 'ssl=on', + '-c', `ssl_cert_file=${serverCertificate}`, '-c', `ssl_key_file=${serverKey}`], onLog:()=>{}, onError:()=>{}}) + const childEnvironment = Object.fromEntries(['PATH', 'SystemRoot', 'WINDIR', 'TEMP', 'TMP'].filter(name => process.env[name]) + .map(name => [name, process.env[name]])) + const serviceFiles = new Map() + for (const trust of ['pinned', 'ca']) { + const file = join(directory, `pg-service-${trust}.conf`) + await writeFile(file, `[owned_archive]\nhost=${host}\nhostaddr=127.0.0.1\nport=${port}\nuser=${user}\npassword=${password}\ndbname=${database}\nsslmode=verify-full\nsslrootcert=${certificate(trust)}\ngssencmode=disable\nconnect_timeout=5\n`, + {flag:'wx', mode:0o600}) + serviceFiles.set(trust, file) + } + const client = (name, args, trust, options = {}) => execute(clientBinary(name), args, { + env:{...childEnvironment, PGSERVICE:'owned_archive', PGSERVICEFILE:serviceFiles.get(trust)}, ...options, + }) + const check = trust => client('psql', ['--no-psqlrc', '--no-password', '--tuples-only', '--no-align', '--command', + 'SELECT ssl FROM pg_catalog.pg_stat_ssl WHERE pid=pg_catalog.pg_backend_pid()'], trust) + const dump = (trust, extra = []) => client('pg_dump', ['--no-password', '--format=custom', '--data-only', '--strict-names', + '--table=public.archive_probe', ...extra], trust, {encoding:'buffer'}) + const badCertificate = async promise => assert.rejects(promise, error => /certificate verify failed|certificate verification failed/i.test(String(error.stderr))) + let started = false + async function startWith(name) { + await copyFile(certificate(name), serverCertificate) + await copyFile(key(name), serverKey) + await chmod(serverKey, 0o600) + await postgres.start() + started = true + } + async function stop() { await postgres.stop(); started = false } + try { + await postgres.initialise() + if(process.env.MMAPP_PG_CLIENT_BIN) process.env.PATH = process.env.MMAPP_PG_CLIENT_BIN + (process.platform === 'win32' ? ';' : ':') + process.env.PATH + await startWith('pinned') + assert.equal((await check('pinned')).stdout.trim(), 't') + const options = {host:'127.0.0.1', port, user, password, database, connectionTimeoutMillis:5000, statement_timeout:5000, + ssl:{ca:await readFile(certificate('pinned')), servername:host, rejectUnauthorized:true}} + if(['--owned-source58-foundation','--owned-source57-admission','--owned-source57-publication','--owned-source58-restore-enrollment','--owned-source58-publication'].includes(mode)){ + const {verifyOwnedSource58Foundation}=await import('./source58-foundation-owned.mjs') + return await verifyOwnedSource58Foundation(pg,options,await readFile(certificate('pinned')),directory, + mode==='--owned-source58-publication'?'upgrade':mode==='--owned-source58-restore-enrollment'?'quarantine': + mode==='--owned-source57-publication'?'publication':mode==='--owned-source57-admission') + } + if(['--owned-legacy-0037-handoff','--owned-legacy-handoff-inspect','--owned-legacy-handoff-intent','--owned-legacy-cold-verify','--owned-legacy-handoff-execute','--owned-legacy-handoff-adopt','--owned-legacy-handoff-release','--owned-legacy-handoff-tail'].includes(mode)){ + const {verifyOwnedLegacy0037Handoff}=await import('./legacy-probe-0037-owned.mjs') + return await verifyOwnedLegacy0037Handoff(pg,options,await readFile(certificate('pinned')),client,directory, + mode==='--owned-legacy-handoff-tail'?'tail':mode==='--owned-legacy-handoff-release'?'release':mode==='--owned-legacy-handoff-adopt'?'adopt':mode==='--owned-legacy-handoff-execute'?'execute':mode==='--owned-legacy-cold-verify'?'cold':mode==='--owned-legacy-handoff-intent'?'intent':mode==='--owned-legacy-handoff-inspect') + } + if (mode.startsWith('--owned-legacy')) return await verifyLegacyExport(pg, options, await readFile(certificate('pinned')), client) + const holder = new pg.Client(options), writer = new pg.Client(options) + let snapshot, productionPeer, archivePasswordFile, runPinnedDatabaseSnapshotDump + try { + await holder.connect() + await writer.connect() + await holder.query('CREATE TABLE public.archive_probe(id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY); INSERT INTO public.archive_probe DEFAULT VALUES') + await holder.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + snapshot = (await holder.query('SELECT pg_catalog.pg_export_snapshot() AS snapshot')).rows[0].snapshot + await writer.query('INSERT INTO public.archive_probe DEFAULT VALUES') + const {stdout:archive} = await dump('pinned', [`--snapshot=${snapshot}`]) + assert.equal(archive.subarray(0, 5).toString(), 'PGDMP') + const file = join(directory, 'archive-probe.dump') + await writeFile(file, archive, {flag:'wx', mode:0o600}) + const {stdout:decoded} = await client('pg_restore', ['--data-only', '--file=-', file], 'pinned') + assert.match(decoded.replaceAll('\r\n', '\n'), /COPY public\.archive_probe \(id\) FROM stdin;\n1\n\\\.\n/) + await holder.query('ROLLBACK') + const runtimePassword = randomBytes(24).toString('hex'), capturePassword = randomBytes(24).toString('hex') + archivePasswordFile = join(directory, 'archive-password') + await writeFile(archivePasswordFile, capturePassword, {flag:'wx',mode:0o600}) + await holder.query(`CREATE ROLE archive_runtime LOGIN PASSWORD '${runtimePassword}'; CREATE ROLE archive_capture LOGIN PASSWORD '${capturePassword}'; + REVOKE TEMP ON DATABASE postgres FROM PUBLIC; GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO archive_runtime; + GRANT SELECT ON TABLE public.archive_probe TO archive_runtime, archive_capture; + GRANT SELECT ON SEQUENCE public.archive_probe_id_seq TO archive_capture; + CREATE SCHEMA mmapp_control; CREATE TABLE mmapp_control.hidden_marker(value text)`) + const roles = Object.fromEntries((await holder.query(`SELECT CASE WHEN rolname='archive_runtime' THEN 'runtime' ELSE 'capture' END AS scope, + rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname IN ('archive_runtime','archive_capture')`)) + .rows.map(({scope,...role})=>[scope,role])) + const {rows:[source]} = await holder.query(`SELECT d.oid::text AS "databaseOid", d.datname AS "databaseName", + (pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier", + pg_catalog.current_setting('server_version_num')::int AS "serverVersionNum",false AS "inRecovery" + FROM pg_catalog.pg_database d WHERE d.datname=pg_catalog.current_database()`) + const hash = value => createHash('sha256').update(value).digest('hex') + const leafBytes = await readFile(certificate('pinned')), pinned = new X509Certificate(leafBytes) + const requestBytes = Buffer.from(JSON.stringify({format:'application-writer-bootstrap-request-v1',installationId:'92ae9f9b-b324-425f-9ed5-77be7c000947', + sourceHeadSha256:'f'.repeat(64),source,roles,tls:{serverName:host,caSha256:hash(leafBytes), + spkiSha256:hash(pinned.publicKey.export({format:'der', type:'spki'}))}})) + const requestFile = join(directory, 'runtime-request.json') + await writeFile(requestFile,requestBytes,{flag:'wx',mode:0o600}) + Object.assign(process.env,{DATABASE_URL:`postgres://archive_runtime:${runtimePassword}@${host}:${port}/${database}`, + DATABASE_RUNTIME_REQUEST_FILE:requestFile,DATABASE_RUNTIME_REQUEST_SHA256:hash(requestBytes),DATABASE_RUNTIME_CA_FILE:certificate('pinned'), + MCP_STATE_DIR:join(directory,'state')}) + const [{exportProtectedDatabaseSnapshot},{pool,readRuntimeDatabaseArchivePeer},{withSystemDatabaseOperationLock},dumpConnection] = await Promise.all([ + import('../src/services/database-protected-archive.ts'), import('../src/db/index.ts'), import('../src/db/operation-lock.ts'), import('../src/services/database-dump-connection.ts'), + ]) + runPinnedDatabaseSnapshotDump = dumpConnection.runPinnedDatabaseSnapshotDump + try { + const protectedArchive = await withSystemDatabaseOperationLock(()=>pool.connect(),async (runtime,discard)=>{ + productionPeer=readRuntimeDatabaseArchivePeer(runtime) + return exportProtectedDatabaseSnapshot(runtime,discard,archivePasswordFile) + }) + const actualArchive = join(directory,'protected.dump') + await writeFile(actualArchive,protectedArchive,{flag:'wx',mode:0o600}) + const productionToc = (await client('pg_restore',['--list',actualArchive],'pinned')).stdout + assert.match(productionToc,/TABLE DATA public archive_probe /) + assert.match(productionToc,/SEQUENCE SET public archive_probe_id_seq /) + assert.doesNotMatch(productionToc,/mmapp_control|hidden_marker/) + } finally { await pool.end() } + + } finally { await Promise.all([holder.end(), writer.end()]) } + await stop() + await startWith('substitute') + assert.equal((await check('ca')).stdout.trim(), 't') + assert.equal((await dump('ca')).stdout.subarray(0, 5).toString(), 'PGDMP') + await badCertificate(check('pinned')) + await badCertificate(dump('pinned')) + const assertPinnedDumpRefused = () => assert.rejects(runPinnedDatabaseSnapshotDump(productionPeer, archivePasswordFile, snapshot, {}), + error=>error.code==='DATABASE_DUMP_EXIT'&&error.diagnosticKinds.includes('tls')) + await assertPinnedDumpRefused() + await stop() + await startWith('delegated') + await badCertificate(check('pinned')) + await badCertificate(dump('pinned')) + await assertPinnedDumpRefused() + } finally { if (started) await stop() } +} + +try { + await issueCertificates() + const result=mode==='--openssl-only'?undefined:await verifyOwnedPostgres() + console.log(JSON.stringify({mode, exactLeafAccepted:true, ordinaryCaSubstitutionRefused:true, + nonCaDelegationRefused:true, realSnapshotDump:mode === '--owned-postgres', protectedProductDump:mode === '--owned-postgres', + legacyArchiveRoundTrip:mode === '--owned-legacy'||mode === '--owned-legacy-custody',legacyCustodyLockedAndVerified:mode === '--owned-legacy-custody', + inheritedCustodyTablesRefused:mode === '--owned-legacy-custody-shape',...result})) +} finally { + assert.equal(dirname(directory), temporary) + assert(basename(directory).startsWith('mm-archive-leaf-tls-')) + await rm(directory, {recursive:true, force:true, maxRetries:10, retryDelay:250}) +} diff --git a/backend/scripts/test-database-source-binding.mjs b/backend/scripts/test-database-source-binding.mjs new file mode 100644 index 00000000..6e3de3dd --- /dev/null +++ b/backend/scripts/test-database-source-binding.mjs @@ -0,0 +1,17 @@ +// Synthetic signatures/files only. Deliberately discard configured DB URLs, +// environment preload hooks and application secrets, including during SQL suites. +import { spawn } from 'node:child_process' +import { createRequire } from 'node:module' +import { fileURLToPath, pathToFileURL } from 'node:url' + +if (process.argv.length !== 2) throw Error('Source-binding synthetic runner takes no arguments') +const require = createRequire(import.meta.url) +const env = Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'LANG', 'LC_ALL'] + .filter(key => typeof process.env[key] === 'string').map(key => [key, process.env[key]])) +const child = spawn(process.execPath, ['--import', pathToFileURL(require.resolve('tsx')).href, '--test', + fileURLToPath(new URL('../src/db/database-source-binding.test.ts', import.meta.url))], { + cwd: new URL('../', import.meta.url), env: { ...env, MMAPP_OWN_SOURCE_BINDING_SYNTHETIC: 'true' }, + windowsHide: true, stdio: 'inherit', +}) +child.once('error', () => { console.error('Source-binding synthetic child could not start'); process.exitCode = 1 }) +child.once('exit', code => { process.exitCode = code ?? 1 }) diff --git a/backend/scripts/test-database-writer-retained-tls.mjs b/backend/scripts/test-database-writer-retained-tls.mjs new file mode 100644 index 00000000..2402f71b --- /dev/null +++ b/backend/scripts/test-database-writer-retained-tls.mjs @@ -0,0 +1,549 @@ +import assert from 'node:assert/strict' +import {execFile, execFileSync} from 'node:child_process' +import {randomBytes, randomUUID, X509Certificate} from 'node:crypto' +import {setDefaultResultOrder} from 'node:dns' +import {mkdtemp, mkdir, cp, readFile, writeFile, rename, rm, chmod, access} from 'node:fs/promises' +import {createServer} from 'node:net' +import {tmpdir} from 'node:os' +import {dirname, join, resolve} from 'node:path' +import {fileURLToPath} from 'node:url' +import {promisify} from 'node:util' +import EmbeddedPostgres from 'embedded-postgres' +import pg from 'pg' +import {tsImport} from 'tsx/esm/api' +import {poolOptions, retainPublisherTlsPeer} from './writer-profile/operator-contract.mjs' +import {sha} from './writer-profile/operator-files.mjs' + +const openssl = process.env.MMAPP_TLS_FIXTURE_OPENSSL || 'openssl' +const activationOnly = process.argv.includes('--activation-only') +const schemaOnly = process.argv.includes('--schema-only') +const runtimeRoleOnly = process.argv.includes('--runtime-role-only') +const registryOnly = process.argv.includes('--registry-only') +const run = promisify(execFile) +setDefaultResultOrder('ipv4first') +const host = 'localhost' +const reservePort = async () => { + const listener = createServer() + await new Promise((ready, reject) => listener.once('error', reject).listen(0, '127.0.0.1', ready)) + return listener +} +const releasePort = listener => new Promise((ready, reject) => listener.close(error => error ? reject(error) : ready())) +const sourcePort = await reservePort() +const clonePort = await reservePort() +const port = sourcePort.address().port, clonePortNumber = clonePort.address().port +await Promise.all([releasePort(sourcePort), releasePort(clonePort)]) +const transport = {host, port, user: 'mcp_test', password: randomBytes(24).toString('hex'), database: 'postgres'} +const runtime = 'tls_fixture_runtime', runtimePassword = randomBytes(24).toString('hex') +const runtimeUrl = targetPort => `postgresql://${runtime}:${runtimePassword}@${host}:${targetPort}/${transport.database}` +const runtimeRefused = {code: 'DATABASE_RUNTIME_SOURCE_REFUSED'} +const certFile = 'server.pem', keyFile = 'server-key.pem' +const root = await mkdtemp(join(tmpdir(), 'mm-writer-tls-owned-')) +const databaseDir = join(root, 'pg'), cloneDir = join(root, 'pg-clone') +const sourceTlsDir = join(root, 'source-tls'), cloneTlsDir = join(root, 'clone-tls') +const postgresLog = [] +const cluster = (databaseDir, port, tlsDir) => new EmbeddedPostgres({databaseDir, port, user: transport.user, password: transport.password, + persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'], + postgresFlags: ['-h', '127.0.0.1', '-c', 'ssl=on', '-c', `ssl_cert_file=${join(tlsDir, certFile)}`, '-c', `ssl_key_file=${join(tlsDir, keyFile)}`, '-c', 'log_line_prefix=[%u] '], + onLog: message => { postgresLog.push(message) }, onError: () => {}}) +const postgres = cluster(databaseDir, port, sourceTlsDir), clone = cluster(cloneDir, clonePortNumber, cloneTlsDir) +let sourceStarted = false, cloneStarted = false +const fingerprint = ca => sha(new X509Certificate(ca).publicKey.export({format:'der', type:'spki'})) +const issue = async tlsDir => { + await mkdir(tlsDir, {mode:0o700}) + execFileSync(openssl, ['req', '-new', '-newkey', 'rsa:2048', '-x509', '-nodes', '-days', '1', '-subj', '/CN=localhost', + '-addext', 'subjectAltName=DNS:localhost', '-keyout', keyFile, '-out', certFile], {cwd: tlsDir, stdio: 'ignore', windowsHide: true}) + await chmod(join(tlsDir, keyFile), 0o600) + return readFile(join(tlsDir, certFile)) +} +const withClient = async (config, work) => { + const pool = new pg.Pool({connectionTimeoutMillis:5000, ...config}) + let client + try { client = await pool.connect(); return await work(client) } + finally { client?.release(true); await pool.end() } +} +const query = "SELECT (pg_control_system()).system_identifier::text AS system, oid::text AS oid FROM pg_database WHERE datname=current_database()" +const createRuntime = async client => { + await client.query(`CREATE ROLE ${runtime} LOGIN PASSWORD '${runtimePassword}'; GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${runtime}`) + const {rows: [{oid}]} = await client.query('SELECT oid::text FROM pg_catalog.pg_roles WHERE rolname=$1', [runtime]) + return oid +} +const runtimeFiles = async (request, ca, targetPort = port) => { + const requestFile = join(root, 'runtime-request.json'), caFile = join(root, 'runtime-ca.pem') + const bytes = Buffer.from(JSON.stringify(request)) + await Promise.all([writeFile(requestFile, bytes), writeFile(caFile, ca)]) + return {databaseUrl: runtimeUrl(targetPort), requestFile, requestSha256: sha(bytes), caFile} +} +const unexpectedConnection = () => assert.fail('Unverified runtime connection became available to application SQL') + +async function verifyRegistryEnrollment(owner, runtimeRequest, ca) { + const {mock} = await import('node:test') + const filesUrl = new URL('./writer-profile/operator-files.mjs', import.meta.url) + const files = await import(filesUrl.href) + const {MIGRATION_MANIFEST, migrationManifestDigest} = await tsImport('../src/db/migration-manifest.ts', import.meta.url) + const {FIXED56_ROLE_TEMPLATE} = await tsImport('./writer-profile/fixed56-build-definition.ts', import.meta.url) + const operator = runtimeRequest.roles.operator.name, capture = runtimeRequest.roles.capture.name + const publisher = runtimeRequest.roles.publisher.name, control = runtimeRequest.roles.control.name + const password = randomBytes(24).toString('hex') + const guard = name => FIXED56_ROLE_TEMPLATE.functions.find(row => row.namespace === 'mmapp_control' && row.proname === name).definition + await owner.query(`CREATE ROLE ${operator} LOGIN; CREATE ROLE ${capture} LOGIN; CREATE ROLE ${publisher} LOGIN; + CREATE ROLE ${control} LOGIN PASSWORD '${password}'; ALTER SCHEMA mmapp_control OWNER TO ${control}; + REVOKE ALL ON SCHEMA mmapp_control FROM PUBLIC; + GRANT USAGE ON SCHEMA mmapp_control TO ${operator}, ${publisher}; + CREATE TABLE public.schema_migrations (id text PRIMARY KEY); + GRANT SELECT ON TABLE public.schema_migrations TO ${control}; + GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${operator}, ${publisher}, ${control}; + ${guard('candidate_require_preadmission')}; ${guard('candidate_truncate_preadmitted')}; + ALTER FUNCTION mmapp_control.candidate_require_preadmission() OWNER TO ${control}; + ALTER FUNCTION mmapp_control.candidate_truncate_preadmitted() OWNER TO ${control}; + REVOKE ALL ON FUNCTION mmapp_control.candidate_require_preadmission(), mmapp_control.candidate_truncate_preadmitted() FROM PUBLIC; + GRANT EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() TO ${operator}`) + await owner.query('INSERT INTO public.schema_migrations SELECT unnest($1::text[])', [MIGRATION_MANIFEST.map(pin => pin.file.slice(0, -4))]) + const keys = Object.keys(runtimeRequest.roles) + const names = keys.map(key => runtimeRequest.roles[key].name) + const {rows} = await owner.query('SELECT rolname AS name, oid::text AS oid FROM pg_catalog.pg_roles WHERE rolname=ANY($1)', [names]) + const roles = Object.fromEntries(keys.map((key, index) => [key, {name:names[index], signedOid:rows.find(row => row.name === names[index]).oid}])) + const manifestSha256 = migrationManifestDigest() + const bytes = new Map([ + ['owned-approval', Buffer.from(JSON.stringify({format:'application-writer-registry-enrollment-v1', manifestSha256, + deploymentRequest:{...runtimeRequest, roles}}))], + ['owned-transport', Buffer.from(JSON.stringify({...transport, user:control, password}))], + ['owned-ca', ca], + ]) + // Windows fixture supplies protected bytes. The real TLS, SQL and catalog verifier run unchanged. + const fileFixture = mock.module(filesUrl, {namedExports:{...files, async readProtected(name, max, expected) { + const value = bytes.get(name) + assert(value && value.length <= max && sha(value) === expected) + return value + }}}) + try { + const {registryEnrollmentMain} = await import('./writer-profile/registry-enrollment.mts') + const args = ['registry-enroll', '--approval', 'owned-approval', '--approval-sha256', sha(bytes.get('owned-approval')), + '--transport', 'owned-transport', '--transport-sha256', sha(bytes.get('owned-transport')), '--ca', 'owned-ca'] + const inspectArgs = ['registry-inspect', ...args.slice(1)] + const count = async () => (await owner.query(`SELECT count(*)::int AS n FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n + ON n.oid=c.relnamespace WHERE n.nspname='mmapp_control' AND c.relname IN ('application_writer_profile_history','application_writer_profile_current')`)).rows[0].n + assert.equal((await registryEnrollmentMain(inspectArgs)).outcome, 'absent') + assert.equal(await count(), 0) + await owner.query('CREATE TABLE mmapp_control.application_writer_profile_history (id int)') + try { await assert.rejects(registryEnrollmentMain(inspectArgs), /relations are incomplete/) } + finally { await owner.query('DROP TABLE mmapp_control.application_writer_profile_history') } + await owner.query(`GRANT pg_read_all_data TO ${runtime}`) + try { + await assert.rejects(registryEnrollmentMain(args), /Role ownership\/membership/) + assert.equal(await count(), 0) + } finally { await owner.query(`REVOKE pg_read_all_data FROM ${runtime}`) } + const report = await registryEnrollmentMain(args) + assert.equal(report.outcome, 'committed') + assert.equal(report.readyAuthorized, false) + assert.equal(report.cleanupUnconfirmed, false) + assert.equal(report.manifestSha256, manifestSha256) + assert.match(report.registryStructureSha256, /^[a-f0-9]{64}$/) + assert.equal(await count(), 2) + const observed = await registryEnrollmentMain(inspectArgs) + assert.equal(observed.outcome, 'installed') + assert.equal(observed.registryStructureSha256, report.registryStructureSha256) + await owner.query(`GRANT UPDATE ON mmapp_control.application_writer_profile_history TO ${runtime}`) + try { await assert.rejects(registryEnrollmentMain(inspectArgs), /effective grant boundary/) } + finally { await owner.query(`REVOKE UPDATE ON mmapp_control.application_writer_profile_history FROM ${runtime}`) } + await assert.rejects(registryEnrollmentMain(args), /Registry target\/owner\/absence refused/) + assert.equal(await count(), 2) + console.log('Owned registry enrollment and inspection: live TLS, strict catalog readback, rollback on role drift, ACL drift and repeat enrollment refused') + } finally { fileFixture.restore() } +} + +async function verifyRuntimeRolePrivileges(owner, createPool) { + const settable = 'tls_fixture_settable' + await owner.query(`CREATE ROLE ${settable} NOLOGIN; + CREATE TABLE public.runtime_privilege_fixture (value int); + INSERT INTO public.runtime_privilege_fixture VALUES (1); + GRANT SELECT, UPDATE ON public.runtime_privilege_fixture TO ${runtime}`) + const withRuntime = async work => { + const fresh = createPool(1) + try { await work(fresh) } finally { await fresh.end() } + } + await withRuntime(async fresh => { + const {rows} = await fresh.query('UPDATE public.runtime_privilege_fixture SET value=2 RETURNING value') + assert.deepEqual(rows, [{value:2}]) + }) + for (const [grant, revoke] of [ + [`GRANT pg_write_all_data TO ${runtime} WITH INHERIT TRUE, SET FALSE`, `REVOKE pg_write_all_data FROM ${runtime}`], + [`GRANT ${settable} TO ${runtime} WITH INHERIT FALSE, SET TRUE`, `REVOKE ${settable} FROM ${runtime}`], + [`GRANT ${runtime} TO ${settable}`, `REVOKE ${runtime} FROM ${settable}`], + [`GRANT TEMPORARY ON DATABASE postgres TO ${runtime}`, `REVOKE TEMPORARY ON DATABASE postgres FROM ${runtime}`], + [`GRANT CREATE ON SCHEMA mmapp_control TO ${runtime}`, `REVOKE CREATE ON SCHEMA mmapp_control FROM ${runtime}`], + [`GRANT SET ON PARAMETER session_replication_role TO ${runtime}`, `REVOKE SET ON PARAMETER session_replication_role FROM ${runtime}`], + [`ALTER TABLE public.runtime_privilege_fixture OWNER TO ${runtime}`, + `ALTER TABLE public.runtime_privilege_fixture OWNER TO ${transport.user}; + GRANT SELECT, UPDATE ON public.runtime_privilege_fixture TO ${runtime}`], + ]) { + await owner.query(grant) + try { + await withRuntime(async fresh => assert.rejects( + fresh.query('UPDATE public.runtime_privilege_fixture SET value=3'), runtimeRefused, grant, + )) + assert.deepEqual((await owner.query('SELECT value FROM public.runtime_privilege_fixture')).rows, [{value:2}]) + } finally { await owner.query(revoke) } + } + await withRuntime(async fresh => assert.deepEqual( + (await fresh.query('SELECT value FROM public.runtime_privilege_fixture')).rows, [{value:2}], + )) + await withRuntime(async retained => { + await retained.query('SELECT 1') + assert.equal(retained.idleCount, 1) + await owner.query(`GRANT ${settable} TO ${runtime}`) + try { + await assert.rejects(retained.query('UPDATE public.runtime_privilege_fixture SET value=3'), runtimeRefused) + assert.equal(retained.idleCount, 0) + } finally { await owner.query(`REVOKE ${settable} FROM ${runtime}`) } + await retained.query('SELECT 1') + assert.equal(retained.idleCount, 1) + await owner.query(`GRANT TEMPORARY ON DATABASE postgres TO ${runtime}`) + try { + await assert.rejects(retained.connect(), runtimeRefused) + assert.equal(retained.idleCount, 0) + } finally { await owner.query(`REVOKE TEMPORARY ON DATABASE postgres FROM ${runtime}`) } + assert.deepEqual((await retained.query('SELECT value FROM public.runtime_privilege_fixture')).rows, [{value:2}]) + }) + console.log('Runtime roles: direct DML accepted; unsafe privilege drift refused on fresh and retained TLS connections before application SQL') +} + +async function verifyRuntimeActivation(createPool, ownerPool, request) { + const {sourceBindingBytes} = await tsImport('../src/db/database-source-binding.ts', import.meta.url) + const {DATABASE_SOURCE_ACTIVATION_FILENAME} = await tsImport('../src/db/database-source-activation-record.ts', import.meta.url) + const directory = join(root, 'activation') + const file = join(directory, DATABASE_SOURCE_ACTIVATION_FILENAME), pending = `${file}.pending` + const state = { + format: 'database-source-activation-v1', activationDirectory: directory, installationId: request.installationId, + sourceIdentityId: randomUUID(), publicKeySpkiSha256: 'c'.repeat(64), status: 'active', + generations: [{generation: randomUUID(), headSha256: request.sourceHeadSha256, bindingSha256: 'b'.repeat(64), + tlsSpkiSha256: request.tls.spkiSha256}], + } + const active = sourceBindingBytes(state) + const revoked = sourceBindingBytes({...state, status:'revoked'}) + const publish = async bytes => { + await writeFile(pending, bytes, {flag:'wx', mode:0o600}) + await rename(pending, file) + } + await mkdir(directory, {mode:0o700}) + await publish(active) + await ownerPool.query(`CREATE SEQUENCE mmapp_control.activation_query; GRANT USAGE ON mmapp_control.activation_query TO ${runtime}`) + const blockedSql = "SELECT nextval('mmapp_control.activation_query')" + const pidSql = 'SELECT pg_backend_pid() AS pid' + const directoryKey = 'DATABASE_RUNTIME_ACTIVATION_DIRECTORY', digestKey = 'DATABASE_RUNTIME_ACTIVATION_SHA256' + const previousDirectory = process.env[directoryKey], previousDigest = process.env[digestKey] + process.env[directoryKey] = directory + process.env[digestKey] = sha(active) + let pool + try { + pool = createPool(1) + const {rows:[{pid}]} = await pool.query(pidSql) + const retained = await pool.connect() + let queued + try { + assert.equal((await retained.query(pidSql)).rows[0].pid, pid) + queued = assert.rejects(pool.connect().then(client => { + client.release() + unexpectedConnection() + }), runtimeRefused) + await writeFile(pending, revoked, {flag:'wx', mode:0o600}) + } finally { retained.release() } + await queued + await assert.rejects(pool.query(blockedSql), runtimeRefused) + await rm(pending) + assert.equal((await pool.query(pidSql)).rowCount, 1) + assert.equal(pool.idleCount, 1) + await publish(revoked) + await assert.rejects(pool.query(blockedSql), runtimeRefused) + await assert.rejects(pool.connect().then(client => { + client.release() + unexpectedConnection() + }), runtimeRefused) + const refuseFresh = async expectedDigest => { + process.env[digestKey] = expectedDigest + await assert.rejects(async () => { + const fresh = createPool(1) + try { await fresh.query(blockedSql) } + finally { await fresh.end() } + }, runtimeRefused) + } + await refuseFresh(sha(revoked)) + await publish(active) + await refuseFresh('0'.repeat(64)) + assert.equal((await pool.query(pidSql)).rowCount, 1) + const [generation] = state.generations + const newHead = 'd'.repeat(64), newSpki = 'e'.repeat(64) + for (const wrongState of [ + {...state, installationId:randomUUID()}, + {...state, generations:[{...generation, headSha256:newHead}]}, + {...state, generations:[{...generation, tlsSpkiSha256:newSpki}]}, + {...state, generations:[generation, {...generation, generation:randomUUID(), headSha256:newHead, tlsSpkiSha256:newSpki}]}, + ]) { + const wrong = sourceBindingBytes(wrongState) + await publish(wrong) + await refuseFresh(sha(wrong)) + } + const {rows:[{is_called}]} = await ownerPool.query('SELECT is_called FROM mmapp_control.activation_query') + assert.equal(is_called, false, 'Refused application SQL reached PostgreSQL') + } finally { + if (previousDirectory === undefined) delete process.env[directoryKey] + else process.env[directoryKey] = previousDirectory + if (previousDigest === undefined) delete process.env[digestKey] + else process.env[digestKey] = previousDigest + await pool?.end() + } + console.log('Runtime activation: retained PostgreSQL client admitted under active pin; pending, revoked, digest and latest generation mismatches refused before application SQL') +} + +async function verifyRestrictedStartup(ownerPool, validFiles) { + const sqlitePath = join(root, 'fixture.sqlite') + const runBackend = (args, env) => run(process.execPath, ['--import', import.meta.resolve('tsx'), ...args], { + cwd: root, timeout: 120_000, windowsHide: true, env: { + SystemRoot: process.env.SystemRoot, PATH: process.env.PATH, TEMP: process.env.TEMP, TMP: process.env.TMP, + NODE_ENV: 'test', DATABASE_PATH: sqlitePath, MCP_STATE_DIR: join(root, 'startup-state'), ...env, + }, + }) + const prepare = fileURLToPath(new URL('../src/scripts/prepare-database.ts', import.meta.url)) + const ownerUrl = `postgresql://${transport.user}:${transport.password}@127.0.0.1:${transport.port}/${transport.database}` + await runBackend([prepare], {DATABASE_URL: ownerUrl}) + await ownerPool.query(`REVOKE CREATE ON SCHEMA public FROM PUBLIC; + GRANT SELECT ON public.schema_migrations, public.traffic_settings, public.uptime_settings, + public.internet_path_settings, public.data_migration, public.servers TO ${runtime}; + ALTER ROLE ${runtime} SET log_statement = 'all'`) + const {FIXED56_ROLE_TEMPLATE} = await tsImport('./writer-profile/fixed56-build-definition.ts', import.meta.url) + const guardFunction = name => FIXED56_ROLE_TEMPLATE.functions.find(row => row.namespace === 'mmapp_control' && row.proname === name) + const admissionGuard = guardFunction('candidate_require_preadmission') + const truncateGuard = guardFunction('candidate_truncate_preadmitted') + const operatorGuard = guardFunction('candidate_operator_ddl') + const trigger = FIXED56_ROLE_TEMPLATE.triggers.find(row => row.relname === 'mcp_operation_replay_tombstones' && row.tgname === 'candidate_truncate_preadmitted') + const eventTrigger = FIXED56_ROLE_TEMPLATE.events.find(row => row.evtname === 'candidate_operator_ddl') + assert.equal(trigger.tgenabled, 'A') + assert.equal(eventTrigger.evtenabled, 'O') + assert.equal(eventTrigger.evttags, null) + await ownerPool.query(`${admissionGuard.definition}; ${truncateGuard.definition}; ${trigger.definition}; + ALTER TABLE public.mcp_operation_replay_tombstones ENABLE ALWAYS TRIGGER candidate_truncate_preadmitted; + ${operatorGuard.definition.replace('@mm-role:operator@', 'tls_operator')}; + CREATE EVENT TRIGGER ${eventTrigger.evtname} ON ${eventTrigger.evtevent} EXECUTE FUNCTION ${eventTrigger.function}; + REVOKE ALL ON FUNCTION mmapp_control.candidate_require_preadmission(), mmapp_control.candidate_truncate_preadmitted(), + mmapp_control.candidate_operator_ddl() FROM PUBLIC`) + + const databaseModule = new URL('../src/db/index.ts', import.meta.url).href + const bootstrapModule = new URL('../src/db/bootstrap.ts', import.meta.url).href + const runtimeEnv = { + DATABASE_URL: validFiles.databaseUrl, DATABASE_RUNTIME_REQUEST_FILE: validFiles.requestFile, + DATABASE_RUNTIME_REQUEST_SHA256: validFiles.requestSha256, DATABASE_RUNTIME_CA_FILE: validFiles.caFile, + } + const schemaRefused = 'DATABASE_RUNTIME_SCHEMA_REFUSED' + const startup = async (expectedError, schemaSha256) => { + const start = postgresLog.length + await runBackend(['--input-type=module', '--eval', ` + import assert from 'node:assert/strict'; + import {closePool, pool} from ${JSON.stringify(databaseModule)}; + import {initDatabase} from ${JSON.stringify(bootstrapModule)}; + try { + const {rows: [role]} = await pool.query(\`SELECT session_user::text AS login, + current_user::text AS principal, has_schema_privilege('public', 'CREATE') AS create_schema, + has_database_privilege(current_database(), 'CREATE,TEMP') AS create_database\`); + assert.deepEqual(role, {login:${JSON.stringify(runtime)}, principal:${JSON.stringify(runtime)}, create_schema:false, create_database:false}); + const expected = process.argv[1]; + if (expected === ${JSON.stringify(schemaRefused)}) await assert.rejects(initDatabase(), {code: expected}); + else if (expected) await assert.rejects(initDatabase(), new RegExp(expected)); + else await initDatabase(); + } finally { await closePool(); } + `, expectedError ?? ''], { + ...runtimeEnv, + ...(schemaSha256 === undefined ? {} : {DATABASE_RUNTIME_SCHEMA_SHA256: schemaSha256}), + }) + const statements = postgresLog.slice(start).join('').split(new RegExp(`\\[${runtime}\\] (?:LOG: +(?:statement|execute [^:]+)|STATEMENT):`, 'g')).slice(1) + const readsMigrationHistory = statements.some(statement => /^\s*SELECT id FROM public\.schema_migrations\b/i.test(statement)) + assert.equal(readsMigrationHistory, expectedError !== schemaRefused, 'Migration history must be read only after the schema guard passes') + if (schemaSha256 !== undefined) { + assert(statements.some(statement => /^\s*BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY\b/i.test(statement))) + assert(statements.some(statement => /^\s*\/\* migration-schema:columns \*\//i.test(statement))) + } + if (expectedError === schemaRefused) { + assert(!statements.some(statement => /\bFROM\s+(?:public\.)?(?:data_migration|servers|traffic_settings|uptime_settings|internet_path_settings)\b/i.test(statement)), 'Refused startup must stop before application reads') + } + assert(!statements.some(statement => /^\s*(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|TRUNCATE|COPY|CALL|DO)\b/i.test(statement)), 'PostgreSQL observed no runtime DDL or data writes during startup') + } + + const {captureRuntimeDatabaseSchema} = await tsImport('../src/db/database-runtime-schema.ts', import.meta.url) + const {withSystemDatabaseOperationLock} = await tsImport('../src/db/operation-lock.ts', import.meta.url) + const capture = () => withSystemDatabaseOperationLock(() => ownerPool.connect(), captureRuntimeDatabaseSchema) + const baseline = await capture() + const {format, serverVersion, sha256} = baseline + const inspect = fileURLToPath(new URL('../src/scripts/inspect-database-schema.ts', import.meta.url)) + const {stdout} = await runBackend([inspect], runtimeEnv) + assert.deepEqual(JSON.parse(stdout), {format, schemaSha256: sha256, serverVersion}) + await startup(undefined, sha256) + const migrations = () => ownerPool.query('SELECT id, applied_at FROM public.schema_migrations ORDER BY id').then(result => result.rows) + const before = await migrations() + await ownerPool.query('ALTER TABLE public.traffic_settings ALTER COLUMN interval_sec SET DEFAULT 31') + await startup(schemaRefused, sha256) + assert.deepEqual(await migrations(), before) + const {rows: [{column_default}]} = await ownerPool.query(`SELECT column_default FROM information_schema.columns + WHERE table_schema = 'public' AND table_name = 'traffic_settings' AND column_name = 'interval_sec'`) + assert.equal(column_default, '31') + await ownerPool.query('ALTER TABLE public.traffic_settings ALTER COLUMN interval_sec SET DEFAULT 30') + console.log('Restricted application startup: accepted the operator schema digest over TLS and refused changed column default before application reads without runtime DDL or data writes') + + const publicRows = ({sections}) => Object.values(sections).map(rows => rows.filter(row => row.namespace === 'public')) + const assertControlDrift = async () => { + const changed = await capture() + assert.notEqual(changed.sha256, sha256) + assert.deepEqual(publicRows(changed), publicRows(baseline)) + await startup(schemaRefused, sha256) + assert.deepEqual(await migrations(), before) + } + const driftedBody = admissionGuard.definition.replace('pg_advisory_xact_lock_shared(8723110::bigint)', 'pg_advisory_xact_lock_shared(8723111::bigint)') + assert.notEqual(driftedBody, admissionGuard.definition) + await ownerPool.query(driftedBody) + await assertControlDrift() + await ownerPool.query(admissionGuard.definition) + await ownerPool.query(`GRANT EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() TO ${runtime}`) + await assertControlDrift() + await ownerPool.query(`REVOKE EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() FROM ${runtime}`) + await ownerPool.query(`ALTER EVENT TRIGGER ${eventTrigger.evtname} DISABLE`) + await assertControlDrift() + await ownerPool.query(`ALTER EVENT TRIGGER ${eventTrigger.evtname} ENABLE`) + await startup(undefined, sha256) + console.log('Restricted application startup: refused control guard body, ACL and event trigger state drift with the public catalog and migration markers intact') + await startup() + const currentPartition = `flow_buckets_d_${new Date().toISOString().slice(0, 10).replaceAll('-', '')}` + const expiredPartition = 'flow_buckets_d_20000101' + const partitionExists = async name => (await ownerPool.query('SELECT to_regclass($1) IS NOT NULL AS present', [`public.${name}`])).rows[0].present + await ownerPool.query(`DROP TABLE public.${currentPartition}; CREATE TABLE public.${expiredPartition} + PARTITION OF public.flow_buckets FOR VALUES FROM ('2000-01-01T00:00:00Z') TO ('2000-01-02T00:00:00Z')`) + await startup(`Required PostgreSQL partition ${currentPartition} is absent or incompatible`) + assert.equal(await partitionExists(currentPartition), false) + + const {rows: [migration]} = await ownerPool.query('DELETE FROM public.schema_migrations WHERE id = (SELECT max(id) FROM public.schema_migrations) RETURNING id, applied_at') + await startup('Версия схемы PostgreSQL не совпадает') + const maintain = () => runBackend([prepare, '--partitions-only'], {DATABASE_URL: ownerUrl}) + await assert.rejects(maintain(), /Версия схемы PostgreSQL не совпадает/) + assert.equal(await partitionExists(currentPartition), false) + assert.equal(await partitionExists(expiredPartition), true) + assert.equal((await ownerPool.query('SELECT id FROM public.schema_migrations WHERE id=$1', [migration.id])).rowCount, 0) + await ownerPool.query('INSERT INTO public.schema_migrations (id, applied_at) VALUES ($1, $2)', [migration.id, migration.applied_at]) + + await writeFile(sqlitePath, 'SQLite format 3\0') + await maintain() + assert.equal(await partitionExists(currentPartition), true) + assert.equal(await partitionExists(expiredPartition), false) + assert.deepEqual(await migrations(), before) + await startup('оператор должен завершить импорт SQLite') + await rm(sqlitePath) + await startup() + console.log('Operator partition maintenance: missing migration refused before partition DDL; missing and expired partitions handled without migrations or SQLite import') +} + +try { + await postgres.initialise() + const first = await issue(sourceTlsDir) + await postgres.start(); sourceStarted = true + const pin = {serverName:host, caSha256:sha(first), spkiSha256:fingerprint(first)} + const request = {tls:pin, roles:{publisher:{name:transport.user}}, source:{databaseName:transport.database}} + const baseline = await withClient(poolOptions(transport, request, first), async client => { + const retained = retainPublisherTlsPeer(client, pin) + retained() + const {rows:[source]} = await client.query(query) + const {rows:[status]} = await client.query('SELECT ssl FROM pg_stat_ssl WHERE pid=pg_backend_pid()') + assert.equal(status.ssl, true) + await client.query(`CREATE SCHEMA mmapp_control; GRANT USAGE ON SCHEMA mmapp_control TO PUBLIC; + CREATE TABLE mmapp_control.notes (note text PRIMARY KEY); INSERT INTO mmapp_control.notes VALUES ('original')`) + await client.query('REVOKE CREATE, TEMP ON DATABASE postgres FROM PUBLIC; REVOKE EXECUTE ON FUNCTION pg_catalog.pg_control_system() FROM PUBLIC') + source.runtimeOid = await createRuntime(client) + const {rows: [{server_version_num}]} = await client.query('SHOW server_version_num') + source.serverVersionNum = Number(server_version_num) + const connection = client.connection, socket = connection.stream + connection.stream = undefined + try { assert.throws(retained, /retained TLS peer unavailable/) } finally { connection.stream = socket } + return source + }) + await withClient({...transport, ssl:false}, async client => assert.throws(() => retainPublisherTlsPeer(client,pin), /retained TLS peer unavailable/)) + + const runtimeRequest = { + format: 'application-writer-bootstrap-request-v1', installationId: randomUUID(), sourceHeadSha256: 'a'.repeat(64), + source: {systemIdentifier: baseline.system, databaseOid: baseline.oid, databaseName: transport.database, + serverVersionNum: baseline.serverVersionNum, inRecovery: false}, + roles: {runtime: {name: runtime, signedOid: baseline.runtimeOid}, operator: {name: 'tls_operator', signedOid: '30000'}, + capture: {name: 'tls_capture', signedOid: '30001'}, publisher: {name: 'tls_publisher', signedOid: '30002'}, + control: {name: 'tls_control', signedOid: '30003'}}, + tls: pin, + } + const validFiles = await runtimeFiles(runtimeRequest, first) + Object.assign(process.env, {DATABASE_URL: validFiles.databaseUrl, DATABASE_RUNTIME_REQUEST_FILE: validFiles.requestFile, + DATABASE_RUNTIME_REQUEST_SHA256: validFiles.requestSha256, DATABASE_RUNTIME_CA_FILE: validFiles.caFile}) + const {pool: appPool, createPool} = await tsImport('../src/db/index.ts', import.meta.url) + const {readRuntimeDatabasePoolOptions} = await tsImport('../src/db/database-runtime-source.ts', import.meta.url) + try { + const {rows: [identity]} = await appPool.query(`SELECT session_user::text AS login, current_user::text AS principal, + (SELECT ssl FROM pg_catalog.pg_stat_ssl WHERE pid=pg_backend_pid()) AS tls, + pg_catalog.has_database_privilege(current_database(), 'CREATE,TEMP') AS can_create_or_temp`) + assert.deepEqual(identity, {login: runtime, principal: runtime, tls: true, can_create_or_temp: false}) + await assert.rejects(appPool.query('SELECT * FROM mmapp_control.notes'), {code: '42501'}) + } finally { await appPool.end() } + const ownerPool = new pg.Pool(poolOptions(transport, request, first)) + try { + if (registryOnly) await verifyRegistryEnrollment(ownerPool, runtimeRequest, first) + else { + if (!schemaOnly && !activationOnly) await verifyRuntimeRolePrivileges(ownerPool, createPool) + if (!schemaOnly && !runtimeRoleOnly) await verifyRuntimeActivation(createPool, ownerPool, runtimeRequest) + if (!activationOnly && !runtimeRoleOnly) await verifyRestrictedStartup(ownerPool, validFiles) + } + } finally { await ownerPool.end() } + if (!activationOnly && !schemaOnly && !runtimeRoleOnly && !registryOnly) { + assert.throws(() => readRuntimeDatabasePoolOptions({...validFiles, databaseUrl: `${validFiles.databaseUrl}?sslmode=disable`}), runtimeRefused) + assert.throws(() => readRuntimeDatabasePoolOptions({...validFiles, requestSha256: '0'.repeat(64)}), runtimeRefused) + await withClient(poolOptions(transport, request, first), client => client.query(`REVOKE EXECUTE ON FUNCTION pg_catalog.pg_control_system() FROM ${runtime}`)) + await assert.rejects(withClient(readRuntimeDatabasePoolOptions(validFiles), unexpectedConnection), runtimeRefused) + await withClient(poolOptions(transport, request, first), client => client.query(`GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${runtime}`)) + const wrongDb = await runtimeFiles({...runtimeRequest, source: {...runtimeRequest.source, databaseOid: String(Number(baseline.oid) + 1)}}, first) + await assert.rejects(withClient(readRuntimeDatabasePoolOptions(wrongDb), unexpectedConnection), runtimeRefused) + await runtimeFiles(runtimeRequest, first) + await postgres.stop(); sourceStarted = false + await cp(databaseDir, cloneDir, {recursive:true, force:false, errorOnExist:true}) + await assert.rejects(access(join(cloneDir, keyFile)), {code:'ENOENT'}) + const second = await issue(cloneTlsDir) + assert.notEqual(fingerprint(second), pin.spkiSha256) + await postgres.start(); sourceStarted = true + await clone.start(); cloneStarted = true + const cloneTransport = {...transport, port:clonePortNumber} + const bothTrusted = Buffer.concat([first,second]) + await assert.rejects(withClient(poolOptions(cloneTransport, {...request,tls:{...pin,caSha256:sha(bothTrusted)}}, bothTrusted), + () => assert.fail('Wrong physical cluster must not reach application SQL')), /TLS SPKI refused/) + await withClient({...cloneTransport, ssl:{ca:bothTrusted, servername:host, rejectUnauthorized:true}}, async client => { + assert.equal(client.connection.stream.authorized, true) + const {rows:[source]} = await client.query(query) + assert.deepEqual(source, {system: baseline.system, oid: baseline.oid}) + assert.throws(() => retainPublisherTlsPeer(client,pin), /TLS SPKI refused/) + retainPublisherTlsPeer(client,{...pin,spkiSha256:fingerprint(second)})() + assert.deepEqual((await client.query('SELECT note FROM mmapp_control.notes')).rows, [{note:'original'}]) + await client.query("INSERT INTO mmapp_control.notes VALUES ('clone-only')") + }) + const cloneFiles = await runtimeFiles({...runtimeRequest, tls: {...pin, caSha256: sha(bothTrusted)}}, bothTrusted, clonePortNumber) + await assert.rejects(withClient(readRuntimeDatabasePoolOptions(cloneFiles), unexpectedConnection), runtimeRefused) + await runtimeFiles(runtimeRequest, first) + await withClient(poolOptions(transport, request, first), async client => { + retainPublisherTlsPeer(client,pin)() + assert.deepEqual((await client.query('SELECT note FROM mmapp_control.notes')).rows, [{note:'original'}]) + await client.query(`DROP OWNED BY ${runtime}; DROP ROLE ${runtime}`) + assert.notEqual(await createRuntime(client), baseline.runtimeOid) + }) + const freshAppPool = createPool(1) + try { await assert.rejects(freshAppPool.query('SELECT 1'), runtimeRefused) } + finally { await freshAppPool.end() } + console.log('Genuine PostgreSQL TLS: original accepted; independently writable physical clone with identical DB coordinates and a different CA-valid TLS key refused') + console.log('Runtime pool: restricted login accepted; TLS override, incorrect request digest, missing observation grant, wrong database OID, clone key and recreated role refused') + } +} finally { + const stops = await Promise.allSettled([ + cloneStarted ? clone.stop() : undefined, + sourceStarted ? postgres.stop() : undefined, + ]) + assert(stops.every(result => result.status === 'fulfilled'), 'Owned cluster stop unconfirmed') + assert.equal(dirname(resolve(root)), resolve(tmpdir())) + assert.match(root, /mm-writer-tls-owned-/) + await rm(root, {recursive:true, force:true, maxRetries:20, retryDelay:100}) + await assert.rejects(access(root), {code:'ENOENT'}) + console.log('Owned PostgreSQL and temporary TLS material removed') +} diff --git a/backend/scripts/test-flow-read-adapters.mjs b/backend/scripts/test-flow-read-adapters.mjs new file mode 100644 index 00000000..6fc03436 --- /dev/null +++ b/backend/scripts/test-flow-read-adapters.mjs @@ -0,0 +1,92 @@ +import assert from 'node:assert/strict' +import ts from 'typescript' +import { readFileSync } from 'node:fs' +import { createRequire } from 'node:module' +import { resolve } from 'node:path' +const require=createRequire(import.meta.url),zod=require('zod'),here=import.meta.dirname +function evaluate(source,mocks={}){ + const exports={},module={exports} + const code=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText + new Function('require','exports','module',code)(id=>{if(id==='zod')return zod;if(Object.hasOwn(mocks,id))return mocks[id];throw Error('Unexpected dependency '+id)},exports,module) + return module.exports +} +const contracts=evaluate(readFileSync(resolve(here,'../../packages/contracts/src/traffic-flow.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()=>{}}, +}) +const server={id:1,name:'selected',type:'jump-host'},dto={bpsNow:0,bpsWire:0,bpsOverlay:0,bytesWire:0,live:false,rxSeries:[],txSeries:[],services:[]} +const window={rows:[],topRowsPerBucketLimit:20,topConversationsLimit:20,since:'from',until:'to'} +class McpError extends Error{constructor(code,message,statusCode){super(message);this.code=code;this.statusCode=statusCode}} +const mocks={ + './flow-read-quality.js':qualityHelper, + '@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:[]})}, + '../../services/traffic-flow-source-limits.js':{readFlowSourceLimits:async()=>({topN:20}),readFlowMapSourceLimits:async()=>({topN:20,mapServiceMinSharePct:5})}, + '../servers/repository/servers-repository.js':{getServerRowById:async()=>server}, + '../../services/traffic-flow-server-source.js':{getBoundedFlowServerRowById:async()=>server}, + './flow-source-scope.js':{assertBoundedFlowSource:async()=>{},flowCatalogDevices:async()=>({selected:[server],incomplete:false})}, + './traffic-history-actions.js':{assertTrafficHistoryAccess:async()=>{},assertTrafficHistorySource:async()=>{}}, + './security.js':{McpError},'node:util':require('node:util'),'node:timers/promises':require('node:timers/promises'), + '../../services/traffic-flow-live-slots.js':{FLOW_LIVE_TICK_MS:2000,tryAcquireFlowLiveSlot:()=>true,releaseFlowLiveSlot:()=>slotReleases++}, + '../../services/traffic-flow-settings.js':{getTrafficFlowSettingsRow:async()=>({topN:20})}, + '../../services/traffic-flow-ifindex.js':{resolveIfaceName:()=>''}, + '../../services/traffic-flow-talkers-model.js':{projectFlowTalkers:()=>({talkers:[],totalConversations:0})}, + '../../services/traffic-flow-catalog-read.js':{readFlowCatalogWindow:async()=>window,readFlowExporterInputs:async()=>({window,catalog:{list:[server]},configuredIds:new Set([1]),configurationMembershipVerified:true}),readFlowClientInputs:async()=>({users:[],total:0,window}),observeFlowExporter:()=>({id:'1'}),observeFlowClient:()=>({})}, + './catalog-scope.js':{catalogPaging:zod.z.object({offset:zod.z.number().default(0),limit:zod.z.number().default(50)}),catalogDevices:async()=>({selected:[server],incomplete:false})}, +} +const actions=[] +for(const [file,method] of [['flow-catalog-actions.ts','registerFlowCatalogActions'],['flow-analytics-actions.ts','registerFlowAnalyticsActions'],['flow-map-actions.ts','registerFlowMapActions'],['flow-live-actions.ts','registerFlowLiveActions']]){ + const actionModule=evaluate(readFileSync(resolve(here,'../src/modules/mcp',file),'utf8'),mocks) + actionModule[method]((...args)=>actions.push({id:args[0],schema:args[7],read:args[8].read})) +} +assert.equal(actions.length,7) +const base={version:1,collectorEpoch:'00000000-0000-4000-8000-000000000001',scope:'collector-global',exportCoverage:'unverified',localAdmission:'partial',records:'1',bytes:'10',packets:'1',knownLowerBounds:{records:'1',bytes:'10',packets:'1'},firstAt:'2026-10-29T00:00:00.000Z',lastAt:'2026-10-29T00:00:00.000Z',intervalUnknown:false,reasons:{'aggregate-capacity':true,'ring-capacity':false,'key-width':false,'unsafe-numeric':false},counterOverflow:false,rawRowsPolicy:'topN-and-pending-cap-not-complete',lifetimeScope:'current-collector-isolate'} +const clean={...base,localAdmission:'no-local-loss-observed',records:'0',bytes:'0',packets:'0',knownLowerBounds:{records:'0',bytes:'0',packets:'0'},firstAt:null,lastAt:null,reasons:{'aggregate-capacity':false,'ring-capacity':false,'key-width':false,'unsafe-numeric':false}} +const cases=[null,base,{...base,localAdmission:'loss-quantity-unknown',bytes:null},{...base,localAdmission:'no-local-loss-observed'},clean] +let count=0 +for(const globalAllowed of [false,true])for(const state of cases){ + quality=state + for(const action of actions){ + const input=action.schema.parse(action.id==='traffic.flow.monthly.read'?{month:'2026-10'}:{serverId:1}),context={connection:{policy:{scope:{serverIds:[1],allowGlobal:globalAllowed,operationIds:globalAllowed?['netflow.settings.read']:[]}}}} + const output=await action.read(input,context),expected=contracts.flowIngestQualitySchema.safeParse(globalAllowed?state:null) + assert.deepEqual(output.dataQuality,expected.success?expected.data:null,action.id) + assert.equal(output.collectorQualityAvailable,expected.success,action.id) + assert.equal(output.collectorQualityScope,'current-collector-isolate',action.id) + assert.equal(output.incomplete,['traffic.flow.talkers.read','traffic.flow.analytics.read','traffic.flow.map.read','traffic.flow.live.read'].includes(action.id),action.id+' preserves original result completeness') + assert.equal(output.collectorObservationIncomplete,!expected.success||expected.data.localAdmission!=='no-local-loss-observed',action.id) + assert.equal(output.coverageVerified,false,action.id);assert.equal(output.exportCoverage,'unverified',action.id) + count++ + } +} +assert.equal(slotReleases,10) +console.log('PASS '+count+' actual registered MCP read adapter invocations: unavailable, partial, unknown quantities and invalid healthy claim; inert source/auth adapters; real semantic quality schema') + +let sourceChecks = 0, sourceChanged = false, legacy +const { registerLegacyFlowAnalyticsAction } = evaluate(readFileSync(resolve(here, '../src/modules/mcp/flow-analytics-actions.ts'), 'utf8'), { + ...mocks, + './flow-source-scope.js': { async assertBoundedFlowSource(_context, selected) { + assert.equal(selected, server) + sourceChecks++ + if (sourceChanged) throw new McpError('STATE_CONFLICT', 'Source changed', 409) + } }, + '../../services/traffic-flow-analytics.js': { async buildFlowAnalytics(query, scope) { + assert.deepEqual(query, { serverId: 1, minutes: 15, iface: 'ether1', skipHeavy: true }) + assert.deepEqual(scope.serverIds, [1]) + assert.equal(scope.maxRows, 20000) + await scope.authorize() + return { ...dto, bytes: 42, sources: [{ id: '192.0.2.1', label: 'private label' }], destinations: [] } + } }, +}) +registerLegacyFlowAnalyticsAction((...args) => { legacy = args }) +assert.equal(legacy[1], 'mm_netflow_analytics_get') +const legacyInput = legacy[7].parse({ serverId: 1, interface: 'ether1' }) +const legacyContext = { connection: { policy: { scope: { serverIds: [1] } } } } +const legacyOutput = await legacy[8].read(legacyInput, legacyContext) +assert.equal(legacyOutput.bytes, 42) +assert.deepEqual(legacyOutput.sources, [{ id: '192.0.2.1', label: '192.0.2.1' }]) +assert.equal(sourceChecks, 1) +sourceChanged = true +await assert.rejects(legacy[8].read(legacyInput, legacyContext), { code: 'STATE_CONFLICT' }) +console.log('PASS legacy MCP NetFlow analytics uses scoped source admission and preserves the legacy projection') diff --git a/backend/scripts/test-flow-read-http-owned.mjs b/backend/scripts/test-flow-read-http-owned.mjs new file mode 100644 index 00000000..7bf639f1 --- /dev/null +++ b/backend/scripts/test-flow-read-http-owned.mjs @@ -0,0 +1,76 @@ +import EmbeddedPostgres from 'embedded-postgres' +import { build, stop } from 'esbuild' +import { mkdtemp, rm, readFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve, dirname } from 'node:path' +import { createServer } from 'node:net' +import { randomBytes } from 'node:crypto' +import { spawn } from 'node:child_process' +import assert from 'node:assert/strict' +import { pathToFileURL } from 'node:url' + +if (process.argv.length !== 2) throw Error('Read HTTP acceptance requires its own disposable cluster and no arguments') +const repo = resolve(import.meta.dirname, '../..') +const directory = await mkdtemp(join(tmpdir(), 'mm-flow-http-owned-')) +const bundleDirectory = await mkdtemp(join(repo, 'tmp', 'mm-flow-http-owned-bundle-')) +const listener = createServer() +await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) +const port = listener.address().port +await new Promise(resolve => listener.close(resolve)) +const password = randomBytes(24).toString('hex') +const postgres = new EmbeddedPostgres({ + databaseDir: join(directory, 'pg'), port, user: 'mcp_test', password, + persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'], + postgresFlags: ['-h', '127.0.0.1', '-c', 'work_mem=4MB'], + onLog: () => {}, onError: () => {}, +}) +let started = false +let outcome = 1 +try { + const file = join(bundleDirectory, 'read-http-owned.mjs') + // Current application sources only: no candidate overlays or generated mocks. + await build({ + entryPoints: [join(repo, 'backend/src/services/traffic-flow-read-http.owned.test.ts')], + outfile: file, platform: 'node', format: 'esm', bundle: true, + packages: 'external', sourcemap: 'inline', + banner: { js: "import {createRequire as __ownedCreateRequire} from 'node:module';const require=__ownedCreateRequire(import.meta.url);" }, + plugins: [{ name: 'contracts-source', setup(builder) { + builder.onResolve({ filter: /^@mmapp\/contracts(?:\/.*)?$/ }, args => ({ + path: resolve(repo, 'packages/contracts/src', args.path.replace(/^@mmapp\/contracts\/?/, '') || 'index') + '.ts', + })) + builder.onLoad({ filter: /\.ts$/ }, async args => ({ + contents: (await readFile(args.path, 'utf8')).replaceAll('import.meta.url', JSON.stringify(pathToFileURL(args.path).href)), + loader: 'ts', + })) + } }], + }) + await postgres.initialise() + await postgres.start() + started = true + const exit = await new Promise((resolve, reject) => { + const child = spawn(process.execPath, [file], { + cwd: directory, windowsHide: true, stdio: 'inherit', + env: { ...process.env, + DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, + DATABASE_PATH: join(directory, 'unused.sqlite'), MCP_STATE_DIR: join(directory, 'state'), + AUTH_REQUIRED: 'true', AUTH_ISSUER: 'https://auth.test.local', + AUTH_JWT_SECRET: 'owned-flow-http-only-secret', MCP_PUBLIC_URL: 'http://localhost:8000/mcp', NODE_ENV: 'test', + NODE_PATH: join(repo, 'node_modules'), + }, + }) + child.once('error', reject) + child.once('exit', resolve) + }) + outcome = exit ?? 1 +} finally { + if (started) await postgres.stop() + assert.equal(dirname(resolve(directory)), resolve(tmpdir())) + assert.match(directory, /mm-flow-http-owned-/) + await rm(directory, { recursive: true, force: true, maxRetries: 20, retryDelay: 100 }) + assert.equal(dirname(resolve(bundleDirectory)), resolve(repo, 'tmp')) + assert.match(bundleDirectory, /mm-flow-http-owned-bundle-/) + await rm(bundleDirectory, { recursive: true, force: true, maxRetries: 20, retryDelay: 100 }) + stop() + console.log('Owned read HTTP cluster stopped and temporary resources removed') +} +process.exitCode = outcome diff --git a/backend/scripts/test-flow-read-profile-startup.mjs b/backend/scripts/test-flow-read-profile-startup.mjs new file mode 100644 index 00000000..50075de3 --- /dev/null +++ b/backend/scripts/test-flow-read-profile-startup.mjs @@ -0,0 +1,28 @@ +import{readFile,writeFile,mkdtemp,rm}from'node:fs/promises' +import{resolve,join,dirname}from'node:path' +import{spawn}from'node:child_process' +import assert from'node:assert/strict' +import ts from'typescript' +import{build,stop}from'esbuild' +const repo=resolve(import.meta.dirname,'../..'),folder=await mkdtemp(join(repo,'tmp','mm-flow-profile-startup-owned-')) +async function run(file,cwd,explicit,expected){ + const env={...process.env,NODE_ENV:'test',AUTH_REQUIRED:'false',DATABASE_URL:'postgresql://unused:unused@127.0.0.1:1/unused',EXPECT_PROFILE:expected};delete env.MM_FLOW_READ_RESOURCE_PROFILE;if(explicit!==undefined)env.MM_FLOW_READ_RESOURCE_PROFILE=explicit + return new Promise((resolve,reject)=>{let output='';const child=spawn(process.execPath,[file],{cwd,env,windowsHide:true,stdio:['ignore','pipe','pipe']});child.stdout.on('data',x=>output+=x);child.stderr.on('data',x=>output+=x);child.once('error',reject);child.once('exit',code=>resolve({code,output}))}) +} +try{ + const source=await readFile(resolve(repo,'backend/src/services/traffic-flow-read-resource-profile.ts'),'utf8'),js=ts.transpileModule(source,{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText + await writeFile(join(folder,'profile.mjs'),js);await writeFile(join(folder,'eager-negative.mjs'),js+'\ngetFlowReadResourceProfile();\n');await writeFile(join(folder,'load-env.mjs'),"import{config}from'dotenv';config({quiet:true});");await writeFile(join(folder,'.env'),'MM_FLOW_READ_RESOURCE_PROFILE=constrained\n') + for(const[profile,success]of[['profile.mjs',true],['eager-negative.mjs',false]]){ + await writeFile(join(folder,'policy.mjs'),`import{getFlowReadResourceProfile}from'./${profile}';import'./load-env.mjs';export const policy=getFlowReadResourceProfile();`) + await writeFile(join(folder,'order.mjs'),"import assert from'node:assert/strict';import{policy}from'./policy.mjs';assert.equal(policy.name,'constrained');") + const result=await run(join(folder,'order.mjs'),folder,undefined,'constrained');if(success)assert.equal(result.code,0,result.output);else{assert.notEqual(result.code,0);assert.match(result.output,/standard/)} + } + const abs=path=>JSON.stringify(resolve(repo,path).replaceAll('\\','/')) + await writeFile(join(folder,'actual.ts'),`import{getFlowReadResourceProfile}from${abs('backend/src/services/traffic-flow-read-resource-profile.ts')};import{FLOW_READ_RESERVATION_BYTES}from${abs('backend/src/services/traffic-flow-read-policy.ts')};import{pool}from${abs('backend/src/db/index.ts')};import assert from'node:assert/strict';const p=getFlowReadResourceProfile();assert.equal(p.name,process.env.EXPECT_PROFILE);assert.equal(FLOW_READ_RESERVATION_BYTES,p.reservationBytes);assert.equal(p.parallelSql,8);process.env.MM_FLOW_READ_RESOURCE_PROFILE=p.name==='standard'?'constrained':'standard';assert.equal(getFlowReadResourceProfile().name,p.name);await pool.end();`) + await build({entryPoints:[join(folder,'actual.ts')],outfile:join(folder,'actual.mjs'),bundle:true,format:'esm',platform:'node',packages:'external'}) + for(const[label,dotenv,explicit,expected]of[['standard','standard',undefined,'standard'],['dotenv-constrained','constrained',undefined,'constrained'],['explicit-standard','constrained','standard','standard'],['invalid','constrained','invalid','INVALID']]){ + await writeFile(join(folder,'.env'),'MM_FLOW_READ_RESOURCE_PROFILE='+dotenv+'\n');const result=await run(join(folder,'actual.mjs'),folder,explicit,expected) + if(expected==='INVALID'){assert.notEqual(result.code,0);assert.match(result.output,/MM_FLOW_READ_RESOURCE_PROFILE must be/)}else assert.equal(result.code,0,label+':'+result.output) + } + console.log('PASS pure import order/.env actual policy/explicit override/invalid profile/immutable selection') +}finally{stop();assert.equal(dirname(folder),resolve(repo,'tmp'));await rm(folder,{recursive:true,force:true,maxRetries:10,retryDelay:100})} diff --git a/backend/scripts/test-flow-read-sql-owned.mts b/backend/scripts/test-flow-read-sql-owned.mts new file mode 100644 index 00000000..1d53a5a0 --- /dev/null +++ b/backend/scripts/test-flow-read-sql-owned.mts @@ -0,0 +1,71 @@ +import assert from 'node:assert/strict' +import EmbeddedPostgres from 'embedded-postgres' +import pg from 'pg' +import { mkdtemp, rm, readFile } from 'node:fs/promises' +import ts from 'typescript' +import { createRequire } from 'node:module' +import { getTableColumns } from 'drizzle-orm' +import { drizzle } from 'drizzle-orm/node-postgres' +import { servers } from '../src/db/schema.js' +import { tmpdir } from 'node:os' +import { join, resolve, dirname } from 'node:path' +import { createServer } from 'node:net' +import { randomBytes } from 'node:crypto' +import { readBoundedFlowSql } from '../src/services/traffic-flow-bounded-sql.js' + +if(process.argv.length!==2)throw Error('Read source acceptance requires its own disposable cluster and no arguments') +const directory=await mkdtemp(join(tmpdir(),'mm-flow-read-owned-')) +const listener=createServer();await new Promise(resolve=>listener.listen(0,'127.0.0.1',resolve)) +const address=listener.address();assert.ok(address&&typeof address!=='string');const port=address.port +await new Promise(resolve=>listener.close(()=>resolve())) +const password=randomBytes(24).toString('hex') +const postgres=new EmbeddedPostgres({databaseDir:join(directory,'pg'),port,user:'mcp_test',password,persistent:true,createPostgresUser:false,initdbFlags:['--encoding=UTF8'],postgresFlags:['-h','127.0.0.1','-c','work_mem=4MB','-c','statement_timeout=10000'],onLog:()=>{},onError:()=>{}}) +let pool:pg.Pool|undefined,started=false +try{ + await postgres.initialise();await postgres.start();started=true + pool=new pg.Pool({host:'127.0.0.1',port,user:'mcp_test',password,database:'postgres',max:2}) + const max={maxRows:3,maxEncodedBytes:1024} + const query='SELECT i AS id,$1::text AS text FROM generate_series(1,3) i ORDER BY i' + const normal=await pool.query(query,['я']) + assert.deepEqual(await readBoundedFlowSql(pool,query,['я'],max),normal.rows) + assert.deepEqual(await readBoundedFlowSql(pool,'SELECT 1 id WHERE false',[],max),[]) + await assert.rejects(readBoundedFlowSql(pool,'SELECT i FROM generate_series(1,4) i',[],max),/превышает/) + await assert.rejects(readBoundedFlowSql(pool,'SELECT repeat(\'я\',10000) text',[],max),/превышает/) + await assert.rejects(readBoundedFlowSql(pool,'SELECT repeat(\'a\',600) text FROM generate_series(1,2)',[],max),/превышает/) + const large='SELECT 9223372036854775807::bigint AS counter,\'2026-10-29T12:00:00Z\'::timestamptz AS at,123456789012345678901234567890::numeric AS exact' + assert.deepEqual(await readBoundedFlowSql(pool,large,[],max),(await pool.query(large)).rows,'bigint/numeric/timestamp parser behavior is preserved') + for(const [name,value] of [['__flowRowCount',"'1'::text"],['__flowEncodedBytes',"'1'::text"],['__flowPresent','true']])await assert.rejects(readBoundedFlowSql(pool,`SELECT ${value} AS "${name}"`,[],max),/Reserved NetFlow source column collision/) + let overflowPayloads=0 + const watched={query:async(sql:string,values:unknown[])=>{const result=await pool!.query(sql,values);overflowPayloads=result.rows.filter(row=>row.__flowPresent===true).length;return result}} + await assert.rejects(readBoundedFlowSql(watched,'SELECT repeat(\'a\',1000000) text FROM generate_series(1,20)',[],max),/превышает/) + assert.equal(overflowPayloads,0,'20 MiB source must send only a null sentinel row; no source payload is decoded in Node') + await pool.query('CREATE TABLE flow_read_growth(text text);INSERT INTO flow_read_growth VALUES(\'a\')') + let calls=0,growthDecodedRows=0 + const growing={query:async(sql:string,values:unknown[])=>{ + const result=await pool!.query(sql,values);calls++ + if(calls===1)await pool!.query('UPDATE flow_read_growth SET text=repeat(\'a\',100000)') + if(calls===2)growthDecodedRows=result.rows.filter(row=>row.__flowPresent===true).length + return result + }} + await assert.rejects(readBoundedFlowSql(growing,'SELECT text FROM flow_read_growth',[],max),/превышает/) + assert.equal(calls,2);assert.equal(growthDecodedRows,0,'Concurrent growth cannot bypass the preflight reservation') + const columns=Object.entries(getTableColumns(servers)),quote=(s:string)=>'"'+s.replaceAll('"','""')+'"' + await pool.query('CREATE TABLE servers('+columns.map(([,column])=>quote(column.name)+' '+column.getSQLType()).join(',')+')') + await pool.query('INSERT INTO servers(id,name,host,created_at,updated_at) VALUES(1,\'selected\',\'192.0.2.1\',\'2026-10-29T00:00:00Z\',\'2026-10-29T00:00:00Z\')') + const code=ts.transpileModule(await readFile(new URL('../src/services/traffic-flow-server-source.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText + const serverModule={exports:{} as {readBoundedFlowServerRows:(ids:number[])=>Promise}},localRequire=createRequire(import.meta.url) + const mocks:Record={'drizzle-orm':localRequire('drizzle-orm'),'../db/schema.js':{servers},'../db/index.js':{pool},'./traffic-flow-bounded-sql.js':{readBoundedFlowSql}} + new Function('require','module','exports',code)((id:string)=>{if(!(id in mocks))throw Error('Unexpected dependency '+id);return mocks[id]},serverModule,serverModule.exports) + assert.deepEqual(await serverModule.exports.readBoundedFlowServerRows([1]),await drizzle(pool).select().from(servers),'Full server rows preserve Drizzle fingerprint inputs including timestamp strings') + await pool.query('UPDATE servers SET password=repeat(\'x\',5*1024*1024)') + await assert.rejects(serverModule.exports.readBoundedFlowServerRows([1]),/превышает/) + console.log('PASS actual owned PostgreSQL: complete rows, empty result, count refusal, UTF-8 byte refusal, cumulative source refusal, parser parity, oversize no-decode sentinel and concurrent-growth fence') + console.log('PASS actual bounded server source versus Drizzle mapping parity; 5 MiB credential/config text refused before source decode') +}finally{ + await pool?.end();if(started)await postgres.stop() + // Only the exact private mkdtemp child is eligible; never shell deletion. + assert.equal(dirname(resolve(directory)),resolve(tmpdir())) + assert.equal(directory.includes('mm-flow-read-owned-'),true) + await rm(directory,{recursive:true,force:true,maxRetries:20,retryDelay:100}) + console.log('Owned source SQL cluster stopped and temporary resources removed') +} diff --git a/backend/scripts/test-flow-settings-read-profiles-owned.mjs b/backend/scripts/test-flow-settings-read-profiles-owned.mjs new file mode 100644 index 00000000..bf2d4de2 --- /dev/null +++ b/backend/scripts/test-flow-settings-read-profiles-owned.mjs @@ -0,0 +1,60 @@ +import EmbeddedPostgres from 'embedded-postgres' +import { build,stop } from 'esbuild' +import { mkdtemp,readFile,rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { resolve,join,dirname } from 'node:path' +import { pathToFileURL } from 'node:url' +import { randomBytes } from 'node:crypto' +import { createServer } from 'node:net' +import { spawn } from 'node:child_process' +import assert from 'node:assert/strict' +if(process.argv.length!==2)throw Error('Profile settings tests accept no database or target arguments') +const terminal=child=>child.exitCode!==null||child.signalCode!==null||child.pid===undefined +async function stopOwnedChild(child,graceful=false){ + if(!child||terminal(child))return + await new Promise((done,reject)=>{ + let timer,forceTimer + const finish=()=>{clearTimeout(timer);clearTimeout(forceTimer);child.removeListener('exit',finish);done()} + child.once('exit',finish) + const force=()=>{ + if(terminal(child)){finish();return} + child.kill('SIGKILL') + forceTimer=setTimeout(()=>{if(terminal(child))finish();else{child.removeListener('exit',finish);reject(Error('Owned child failed to reach confirmed terminal state: '+child.pid))}},10000) + } + timer=setTimeout(force,graceful?5000:10000) + if(graceful&&child.connected)child.send({type:'shutdown'},error=>{if(error&&!terminal(child))child.kill()}) + else child.kill() + if(terminal(child))finish() + }) +} +const repo=resolve(import.meta.dirname,'../..'),bundle=await mkdtemp(join(repo,'tmp','mm-profile-cold-bundle-')) +try{ + const compiled=join(bundle,'case.mjs'),driverFile=resolve(repo,'backend/scripts/flow-read-profile-http-client.mjs') + await build({entryPoints:[resolve(repo,'backend/src/services/traffic-flow-settings-read-profile.owned.test.ts')],outfile:compiled,platform:'node',format:'esm',bundle:true,packages:'external',banner:{js:"import{createRequire as __req}from'node:module';const require=__req(import.meta.url);"},plugins:[{name:'current-source',setup(b){b.onResolve({filter:/^@mmapp\/contracts(?:\/.*)?$/},a=>({path:resolve(repo,'packages/contracts/src',a.path.replace(/^@mmapp\/contracts\/?/,'')||'index')+'.ts'}));b.onLoad({filter:/\.ts$/},async a=>({contents:(await readFile(a.path,'utf8')).replaceAll('import.meta.url',JSON.stringify(pathToFileURL(a.path).href)),loader:'ts'}))}}]}) + for(const profile of ['standard','constrained']){ + const directory=await mkdtemp(join(tmpdir(),'mm-flow-profile-cold-owned-')),listener=createServer() + await new Promise(resolve=>listener.listen(0,'127.0.0.1',resolve));const port=listener.address().port;await new Promise(resolve=>listener.close(resolve)) + const password=randomBytes(24).toString('hex'),postgres=new EmbeddedPostgres({databaseDir:join(directory,'pg'),port,user:'mcp_test',password,persistent:true,createPostgresUser:false,initdbFlags:['--encoding=UTF8'],postgresFlags:['-h','127.0.0.1','-c','shared_buffers=64MB','-c','work_mem=2MB'],onLog:()=>{},onError:()=>{}}) + let started=false,client,app + try{ + await postgres.initialise();await postgres.start();started=true + await new Promise((done,reject)=>{client=spawn(process.execPath,[driverFile],{cwd:directory,windowsHide:true,stdio:['ignore','inherit','inherit','ipc'],env:{...process.env,NODE_OPTIONS:'--max-old-space-size=192 --max-semi-space-size=16'}});client.on('message',message=>{if(message.type==='client-ready')done();if((message.type==='client-result'||message.type==='client-progress')&&app?.connected)app.send(message)});client.once('error',reject);client.once('exit',code=>{if(!app)reject(Error('Client startup exit '+code))})}) + const outcome=await new Promise((done,reject)=>{ + app=spawn(process.execPath,[compiled],{cwd:directory,windowsHide:true,stdio:['ignore','inherit','inherit','ipc'],env:{...process.env,NODE_OPTIONS:'--max-old-space-size=192 --max-semi-space-size=16',MM_FLOW_READ_RESOURCE_PROFILE:profile,MMAPP_OWN_FLOW_PROFILE_TEST:'true',DATABASE_URL:`postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`,DATABASE_PATH:join(directory,'unused.sqlite'),MCP_STATE_DIR:join(directory,'state'),AUTH_REQUIRED:'true',AUTH_ISSUER:'https://auth.test.local',AUTH_JWT_SECRET:'owned-profile-only',MCP_PUBLIC_URL:'http://localhost:8000/mcp',NODE_ENV:'test'}}) + const timeout=setTimeout(()=>reject(Error('Profile cold test timed out')),120000) + app.on('message',message=>{if(message.type==='client-command')client.send(message)}) + app.once('error',error=>{clearTimeout(timeout);reject(error)});app.once('exit',code=>{clearTimeout(timeout);done(code??1)}) + }) + assert.equal(outcome,0,profile+' public/settings profile failed') + }finally{ + const stopped=await Promise.allSettled([stopOwnedChild(app),stopOwnedChild(client,true)]) + for(const result of stopped)if(result.status==='rejected')throw result.reason + if(started)await postgres.stop() + assert.equal(dirname(resolve(directory)),resolve(tmpdir()));assert.match(directory,/mm-flow-profile-cold-owned-/);await rm(directory,{recursive:true,force:true,maxRetries:20,retryDelay:100}) + } + } +}finally{stop();assert.equal(dirname(resolve(bundle)),resolve(repo,'tmp'));await rm(bundle,{recursive:true,force:true,maxRetries:20,retryDelay:100})} + +// All owned children and files are confirmed closed above; avoid library ambient handles keeping the CLI alive. +console.log('PASS both owned profile fixtures and confirmed child cleanup') +process.exit(0) diff --git a/backend/scripts/test-mcp-postgres.mjs b/backend/scripts/test-mcp-postgres.mjs new file mode 100644 index 00000000..6dc621a7 --- /dev/null +++ b/backend/scripts/test-mcp-postgres.mjs @@ -0,0 +1,418 @@ +import EmbeddedPostgres from 'embedded-postgres' +import { runOwnedVrfProductionProfile } from './vrf-production-profile.mjs' +import { mkdtemp, rm, readFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve, relative, isAbsolute, delimiter, dirname } from 'node:path' +import { randomBytes } from 'node:crypto' +import { fileURLToPath } from 'node:url' +import { createServer } from 'node:net' +import { spawn, execFile } from 'node:child_process' + +// Explicit targeted reruns use a fresh owned regression database. They never +// claim the primary integration or terminal restore was rerun. +// Fixed groups only; ordinary full/selected regression semantics stay unchanged. +function profilesOnlyGroup(args) { + const groups = args.filter(arg => arg.startsWith('--profiles-group=')) + if (!groups.length) return null + const group = groups[0].slice('--profiles-group='.length) + if (groups.length !== 1 || args.length !== 3 || !args.includes('--all') || !args.includes('--profiles-only') || !['base37', 'vrf1'].includes(group)) { + throw new Error('Fixed profile group requires exactly --all --profiles-only --profiles-group=base37|vrf1') + } + return group +} +function regressionProfiles(profiles, selected, group) { + if (group === null) return [...new Set([...profiles, 'test:logs:pg', ...(!selected ? ['test:vrf:production'] : [])])] + if (!['base37', 'vrf1'].includes(group) || selected || profiles.length !== 36 || new Set(profiles).size !== 36 || profiles.includes('test:logs:pg') || profiles.includes('test:vrf:production')) { + throw new Error('Fixed 37+1 profile coverage changed; review groups before running') + } + return group === 'base37' ? [...profiles, 'test:logs:pg'] : ['test:vrf:production'] +} +// End fixed-group pure selectors. +const profilesGroup = profilesOnlyGroup(process.argv.slice(2)) +const profilesOnly = process.argv.includes('--profiles-only') +if (profilesOnly && (process.argv.length !== 4 + (profilesGroup ? 1 : 0) || !process.argv.includes('--all') || + process.argv.slice(2).some(arg => !['--all', '--profiles-only', ...(profilesGroup ? ['--profiles-group=' + profilesGroup] : [])].includes(arg)))) { + throw new Error('Profiles-only requires exactly --all --profiles-only, without restore, selected profiles or other flags') +} +const regressionOnly = process.argv.includes('--regression-only') +if (regressionOnly && (!process.argv.includes('--all') || process.argv.filter(arg => arg.startsWith('--profile=')).length !== 1 || + process.argv.slice(2).some(arg => !['--all', '--regression-only'].includes(arg) && !arg.startsWith('--profile=')))) { + throw new Error('Regression-only requires exactly --all --regression-only --profile=, without restore or other profiles') +} +const operatorDockerSql = process.argv.includes('--operator-docker-sql') +if (operatorDockerSql && process.argv.slice(2).some(arg => arg !== '--operator-docker-sql')) throw new Error('Operator Docker SQL requires a separate disposable cluster without restore') +const pollMemoryCost = process.argv.includes('--poll-memory-cost') +const pollMemoryBootstrap = process.argv.includes('--poll-memory-bootstrap') +const pollMemory = process.argv.includes('--poll-memory') || pollMemoryCost || pollMemoryBootstrap +if (pollMemory && (process.argv.length !== 3 || process.argv[2] !== (pollMemoryBootstrap ? '--poll-memory-bootstrap' : pollMemoryCost ? '--poll-memory-cost' : '--poll-memory'))) throw new Error('Compiled poll memory requires its own disposable cluster without other profiles') +const pollMemoryEnvironment = Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'LANG', 'LC_ALL'] + .filter(key => typeof process.env[key] === 'string').map(key => [key, process.env[key]])) +const pollJournalSnapshot = process.argv.includes('--poll-journal-snapshot') +if (pollJournalSnapshot && process.argv.slice(2).some(arg => arg !== '--poll-journal-snapshot')) throw new Error('Journal snapshot lock evidence requires its own disposable cluster') +const principalSecuritySource = process.argv.includes('--principal-security-source') +if (principalSecuritySource && process.argv.slice(2).some(arg => arg !== '--principal-security-source')) throw new Error('Principal security source requires its own disposable cluster') +const protocolLimits = process.argv.slice(2).includes('--protocol-limits') +if (protocolLimits && process.argv.length !== 3) throw new Error('Protocol limits acceptance requires its own disposable cluster') +const pollArchiveFlags = ['--poll-terminal-archive', '--poll-terminal-archive-lost-ack', '--poll-terminal-archive-history', '--poll-terminal-archive-history-cold', '--poll-terminal-archive-claim-loss'] +const chosenArchiveFlags = process.argv.slice(2).filter(arg => pollArchiveFlags.includes(arg)) +const pollArchiveLostAck = chosenArchiveFlags[0] === '--poll-terminal-archive-lost-ack' +const pollArchiveCold = chosenArchiveFlags[0] === '--poll-terminal-archive-history-cold' +const pollArchiveHistory = chosenArchiveFlags[0] === '--poll-terminal-archive-history' || pollArchiveCold +const pollArchiveClaimLoss = chosenArchiveFlags[0] === '--poll-terminal-archive-claim-loss' +const pollTerminalArchive = chosenArchiveFlags.length > 0 +if (pollTerminalArchive && (chosenArchiveFlags.length !== 1 || process.argv.slice(2).some(arg => ![chosenArchiveFlags[0], '--restore'].includes(arg)))) throw new Error('Poll terminal archive requires its own disposable cluster') +const pollArchiveSchema = process.argv.includes('--poll-archive-schema') +if (pollArchiveSchema && process.argv.slice(2).some(arg => arg !== '--poll-archive-schema')) throw new Error('Poll archive schema requires its own disposable cluster') +const telemetrySettingsConfiguration = process.argv.includes('--telemetry-settings-configuration') +if (telemetrySettingsConfiguration && process.argv.slice(2).some(arg => arg !== '--telemetry-settings-configuration')) throw new Error('Telemetry settings configuration requires its own disposable cluster') +const routingReservationSchema = process.argv.includes('--routing-reservation-schema') +if (routingReservationSchema && process.argv.slice(2).some(arg => arg !== '--routing-reservation-schema')) throw new Error('Routing reservation schema requires its own disposable cluster') +const routingOriginalEffects = process.argv.includes('--routing-original-effects') +const routingControllerFailoverOnly = process.argv.includes('--controller-failover-only') +const routingForwardRounds = routingOriginalEffects || process.argv.includes('--routing-network-forward-rounds') +if (routingControllerFailoverOnly && !routingOriginalEffects) throw new Error('Controller failover requires --routing-original-effects') +if (routingForwardRounds && process.argv.slice(2).some(arg => arg !== (routingOriginalEffects ? '--routing-original-effects' : '--routing-network-forward-rounds') && + !(routingControllerFailoverOnly && arg === '--controller-failover-only'))) throw new Error('Routing forward rounds requires its own disposable cluster') +const flowActiveServices = process.argv.includes('--flow-active-services') +if (flowActiveServices && process.argv.slice(2).some(arg => arg !== '--flow-active-services')) throw new Error('Flow active services requires its own disposable cluster') +const flowActiveRebuild = process.argv.includes('--flow-active-rebuild') +if (flowActiveRebuild && process.argv.slice(2).some(arg => arg !== '--flow-active-rebuild')) throw new Error('Flow active rebuild requires its own disposable cluster') +const flowQuality = process.argv.includes('--flow-quality') +if (flowQuality && process.argv.slice(2).some(arg => arg !== '--flow-quality')) throw new Error('Flow quality requires its own disposable cluster') +const flowWorkerIpc = process.argv.includes('--flow-worker-ipc') +if (flowWorkerIpc && process.argv.slice(2).some(arg => arg !== '--flow-worker-ipc')) throw new Error('Flow worker IPC requires its own disposable cluster') +const manualUptime = process.argv.includes('--manual-uptime') +if (manualUptime && process.argv.slice(2).some(arg => arg !== '--manual-uptime')) throw new Error('Manual uptime requires its own disposable cluster') +const manualPingGroup = process.argv.includes('--manual-ping-group') +if (manualPingGroup && process.argv.slice(2).some(arg => arg !== '--manual-ping-group')) throw new Error('Manual Ping group requires its own disposable cluster') +const manualTraffic = process.argv.includes('--manual-traffic') +if (manualTraffic && process.argv.slice(2).some(arg => arg !== '--manual-traffic')) throw new Error('Manual traffic requires its own disposable cluster') +const manualApiPing = process.argv.includes('--manual-api-ping') +if (manualApiPing && process.argv.slice(2).some(arg => arg !== '--manual-api-ping')) throw new Error('Manual API Ping requires its own disposable cluster') +const simpleSpeed = process.argv.includes('--simple-speed') +if (simpleSpeed && process.argv.slice(2).some(arg => arg !== '--simple-speed')) throw new Error('Simple speed acceptance requires its own disposable cluster') +const simpleSpeedChr = process.argv.includes('--simple-speed-chr') +if (simpleSpeedChr && process.argv.slice(2).some(arg => arg !== '--simple-speed-chr')) throw new Error('Simple speed CHR acceptance requires its own disposable cluster') +const publicRegistryMetadata = process.argv.includes('--public-registry-metadata') +if (publicRegistryMetadata && process.argv.slice(2).some(arg => arg !== '--public-registry-metadata')) throw new Error('Public registry metadata requires a separate disposable cluster without restore') +const operatorPeerMetadata = process.argv.includes('--operator-peer-metadata') +if (operatorPeerMetadata && process.argv.slice(2).some(arg => arg !== '--operator-peer-metadata')) throw new Error('Operator peer metadata requires a separate disposable cluster without restore') +const externalPeerMetadata = process.argv.includes('--external-peer-metadata') +if (externalPeerMetadata && process.argv.slice(2).some(arg => arg !== '--external-peer-metadata')) throw new Error('Guarded external peer metadata requires a separate disposable cluster without restore') +const pollOperationRetirement = process.argv.includes('--poll-operation-retirement') +const pollRetirementFocused = process.argv.includes('--skip-poll-capacity') +if (pollRetirementFocused && !pollOperationRetirement) throw new Error('--skip-poll-capacity requires --poll-operation-retirement') +const ordinaryOperationRetirement = process.argv.includes('--ordinary-operation-retirement') +const probeOperationRetirement = process.argv.includes('--probe-operation-retirement') +const mcpOperationOrigin = process.argv.includes('--mcp-operation-origin') || pollOperationRetirement || ordinaryOperationRetirement || probeOperationRetirement +if (mcpOperationOrigin && process.argv.slice(2).some(arg => !(pollOperationRetirement && arg === '--skip-poll-capacity') && + arg !== (probeOperationRetirement ? '--probe-operation-retirement' : ordinaryOperationRetirement ? '--ordinary-operation-retirement' : pollOperationRetirement ? '--poll-operation-retirement' : '--mcp-operation-origin'))) throw new Error('MCP operation root requires a separate disposable cluster without restore') +const mcpSpeedOriginal = process.argv.includes('--mcp-speed-original') +if (mcpSpeedOriginal && process.argv.slice(2).some(arg => arg !== '--mcp-speed-original')) throw new Error('MCP speed component requires a separate disposable cluster without restore') +const routerConfigurationGuard = process.argv.includes('--router-configuration-guard') +if (routerConfigurationGuard && process.argv.slice(2).some(arg => arg !== '--router-configuration-guard')) throw new Error('Router configuration guard requires a separate disposable cluster without restore') +const scheduledSpeedOriginal = process.argv.includes('--scheduled-speed-original') +if (scheduledSpeedOriginal && process.argv.slice(2).some(arg => arg !== '--scheduled-speed-original')) throw new Error('Scheduled speed origin requires a separate disposable cluster without restore') +const bandwidthSpeedWorkflow = process.argv.includes('--bandwidth-speed-workflow') +if (bandwidthSpeedWorkflow && process.argv.slice(2).some(arg => arg !== '--bandwidth-speed-workflow')) throw new Error('Whole speed workflow requires its own disposable cluster; restore is not implemented') +const restSpeedOriginal = process.argv.includes('--rest-speed-original') +if (restSpeedOriginal && process.argv.slice(2).some(arg => arg !== '--rest-speed-original')) throw new Error('REST speed origin acceptance requires its own disposable cluster; restore is not implemented for this profile') +const operationLocks = process.argv.includes('--operation-locks') +const serviceExecutionOrigin = process.argv.includes('--service-execution-origin') +const bandwidthNodeSource = process.argv.includes('--bandwidth-node-source') +const bandwidthExternalRegistry = process.argv.includes('--bandwidth-external-registry') +if (bandwidthExternalRegistry && process.argv.slice(2).some(arg => arg !== '--bandwidth-external-registry')) throw new Error('External registry acceptance requires its own disposable cluster; restore is not implemented for this profile') +const bandwidthNodeRegistry = process.argv.includes('--bandwidth-node-registry') +const bandwidthControlSource = process.argv.includes('--bandwidth-control-source') +const bandwidthAdmission = process.argv.includes('--bandwidth-admission') +const diagnosticCancelHost = process.argv.includes('--diagnostic-cancel-host') +const ipsecExportHttp = process.argv.includes('--ipsec-export-http') +const ipsecExportJournal = process.argv.includes('--ipsec-export-journal') +const routingRecoveryAuthority = process.argv.includes('--routing-recovery-authority') +const legacyFlowRebuild = process.argv.includes('--legacy-flow-rebuild') +const serverPollStorageFresh = process.argv.includes('--server-poll-storage-fresh') +const serverPollStorage = process.argv.includes('--server-poll-storage') +const serverPollDurable = process.argv.includes('--server-poll-durable') +const collectorPartitionWriters = process.argv.includes('--collector-partition-writers') +const routingReservations = process.argv.includes('--routing-reservations') +const routingReservationRuntime = process.argv.includes('--routing-reservation-runtime') +const routingNetworkNative = process.argv.includes('--routing-network-native') +const migrationDataInventory = process.argv.includes('--migration-data-inventory') +const migrationSchemaInventory = process.argv.includes('--migration-schema-inventory') +const databaseSchemaControl = process.argv.includes('--database-schema-control') +const controlledMigrations = process.argv.includes('--controlled-migrations') +const controlledSchemaReference = process.argv.includes('--controlled-schema-reference') +const probeStorageHistory = process.argv.includes('--probe-storage-history') +const probeStorageJournal = process.argv.includes('--probe-storage-journal') +const probeLegacyOrphans = process.argv.includes('--probe-legacy-orphans') +const probeStorageFresh = process.argv.includes('--probe-storage-fresh') +for (const [selected, flag] of [[operationLocks, '--operation-locks'], [serviceExecutionOrigin, '--service-execution-origin'], [bandwidthNodeSource, '--bandwidth-node-source'], [bandwidthNodeRegistry, '--bandwidth-node-registry'], [bandwidthExternalRegistry, '--bandwidth-external-registry'], [bandwidthControlSource, '--bandwidth-control-source'], [bandwidthAdmission, '--bandwidth-admission'], [serverPollStorageFresh, '--server-poll-storage-fresh'], [serverPollStorage, '--server-poll-storage'], [diagnosticCancelHost, '--diagnostic-cancel-host'], [ipsecExportHttp, '--ipsec-export-http'], [ipsecExportJournal, '--ipsec-export-journal'], [routingRecoveryAuthority, '--routing-recovery-authority'], [legacyFlowRebuild, '--legacy-flow-rebuild'], [serverPollDurable, '--server-poll-durable'], [collectorPartitionWriters, '--collector-partition-writers'], [routingReservations, '--routing-reservations'], [routingReservationRuntime, '--routing-reservation-runtime'], [routingNetworkNative, '--routing-network-native'], [migrationDataInventory, '--migration-data-inventory'], [migrationSchemaInventory, '--migration-schema-inventory'], [databaseSchemaControl, '--database-schema-control'], [controlledMigrations, '--controlled-migrations'], [controlledSchemaReference, '--controlled-schema-reference'], [probeStorageHistory, '--probe-storage-history'], [probeStorageJournal, '--probe-storage-journal'], [probeLegacyOrphans, '--probe-legacy-orphans'], [probeStorageFresh, '--probe-storage-fresh']]) { + if (selected && process.argv.slice(2).some(arg => ![flag, '--restore'].includes(arg))) throw new Error('Focused writer/recovery acceptance requires a separate disposable run') +} +const probeJournal = process.argv.includes('--server-probe-journal') +if (probeJournal && process.argv.slice(2).some(arg => !['--server-probe-journal', '--restore'].includes(arg))) throw new Error('Server probe journal acceptance requires a separate disposable run') +const flowHostKeys = process.argv.includes('--flow-host-keys') +if (flowHostKeys && process.argv.slice(2).some(arg => !['--flow-host-keys','--restore'].includes(arg))) throw new Error('Host keys acceptance requires a separate disposable run') +const flowRebuildMcp = process.argv.includes('--flow-rebuild-mcp') +if (flowRebuildMcp && process.argv.slice(2).some(arg => arg !== '--flow-rebuild-mcp')) throw new Error('MCP flow rebuild acceptance requires a separate disposable run') +const flowRebuild = process.argv.includes('--flow-rebuild') +if (flowRebuild && process.argv.slice(2).some(arg => arg !== '--flow-rebuild')) throw new Error('Flow rebuild SQL acceptance requires a separate disposable run') +const flowWrites = process.argv.includes('--flow-writes') +if (flowWrites && process.argv.slice(2).some(arg => arg !== '--flow-writes')) throw new Error('Flow writer SQL acceptance requires a separate disposable run') +const chrGreMutation = process.argv.includes('--chr-gre-mutation') +const chrGreRevision = process.argv.includes('--chr-gre-revision') +const chrGreRevisionRecovery = process.argv.includes('--chr-gre-revision-recovery') +const chrRead = process.argv.includes('--chr-read') +const chrIdentity = process.argv.includes('--chr-ospf-identity-read') +const chrRecursive = process.argv.includes('--chr-recursive-restore') +const chrFirewallRestore = process.argv.includes('--chr-firewall-restore') +const chrAddress = process.argv.includes('--chr-address-list') +const chrFirewallWrite = process.argv.includes('--chr-firewall-write') +const chrFirewallMove = process.argv.includes('--chr-firewall-move') +const chrVpn = process.argv.includes('--chr-vpn-catalog') +const chrFilters = process.argv.includes('--chr-filter-write') +const chrLegacyOspf = process.argv.includes('--chr-ospf-legacy') +const greRevision = process.argv.includes('--gre-revision') +const usersBindings = process.argv.includes('--users-bindings') +if (usersBindings && (process.argv.length !== 3 || process.argv[2] !== '--users-bindings')) throw new Error('User bindings acceptance requires its own disposable cluster') +const bridgeVlanConfiguration = process.argv.includes('--bridge-vlan-configuration') +if (bridgeVlanConfiguration && process.argv.slice(2).some(arg => arg !== '--bridge-vlan-configuration')) throw new Error('Bridge VLAN acceptance requires its own disposable cluster') +const applicationAdapters = process.argv.includes('--application-adapters') +const scopedRoutingDiagnostics = process.argv.includes('--scoped-routing-diagnostics') +const interfaceEventsCompatibility = process.argv.includes('--interface-events-compatibility') +const eventsOnly = process.argv.includes('--events-only') +const legacyBgpOnly = process.argv.includes('--legacy-bgp-only') +if ((eventsOnly || legacyBgpOnly) && (!interfaceEventsCompatibility || process.argv.length !== 4)) { + throw new Error('A focused events or legacy-BGP run requires the isolated interface events profile') +} +const wireguardExport = process.argv.includes('--wireguard-export') +if (wireguardExport && process.argv.includes('--wireguard-transfer')) throw new Error('Choose WireGuard transfer or export acceptance mode') +const wireguardTransfer = process.argv.includes('--wireguard-transfer') || wireguardExport +const scopedCheckpointAdapters = process.argv.includes('--scoped-checkpoint-adapters') +const nativeConservation = process.argv.includes('--native-conservation') +const nativeCreateId = process.argv.includes('--native-create-id') +const moduleCompletion = process.argv.includes('--module-completion') +const completionTarget = process.argv.find(arg => arg.startsWith('--only='))?.slice(7) ?? 'all' +if (!['all','wireguard','ipsec-init','ipsec-restore','ipsec-delete','ipsec-create','vrf'].includes(completionTarget) || (completionTarget !== 'all' && !moduleCompletion)) throw new Error('Unknown isolated module target') +if (moduleCompletion && process.argv.slice(2).some(arg => !['--module-completion', '--restore', `--only=${completionTarget}`].includes(arg))) throw new Error('Module completion acceptance requires a separate disposable run') +const ipsecAdapters = process.argv.includes('--ipsec-adapters') || moduleCompletion +if (nativeCreateId && (ipsecAdapters || process.argv.includes('--recovery-maintenance'))) throw new Error('Choose native CREATE ID or another focused acceptance mode') +if (ipsecAdapters && process.argv.includes('--recovery-maintenance')) throw new Error('Choose IPsec adapters or recovery maintenance acceptance mode') +if (nativeConservation && (nativeCreateId || ipsecAdapters || process.argv.includes('--recovery-maintenance'))) throw new Error('Choose native conservation or another focused acceptance mode') +const managementAdmissionOnly = process.argv.includes('--management-admission-only') +const administrationAdmissionOnly = process.argv.includes('--administration-admission-only') +const configurationAuditOnly = process.argv.includes('--configuration-audit-only') +const routerRequestDrainOnly = process.argv.includes('--router-request-drain-only') +const restReplicaOnly = process.argv.includes('--rest-replica-admission-only') +const restoreQuarantineOnly = process.argv.includes('--restore-quarantine-only') +const logsSdkScenario = process.argv.includes('--logs-request-lease-only') ? 'lease-loss' : process.argv.includes('--logs-sdk-cancellation-only') ? 'caller-cancel' : '' +const sqliteImportCliOnly = process.argv.includes('--sqlite-import-cli-only') +const recoveryMaintenance = process.argv.includes('--recovery-maintenance') || managementAdmissionOnly || administrationAdmissionOnly || configurationAuditOnly || routerRequestDrainOnly || restReplicaOnly || restoreQuarantineOnly || !!logsSdkScenario || sqliteImportCliOnly || ipsecAdapters || nativeCreateId || nativeConservation +if (Number(chrGreMutation) + Number(chrGreRevision) + Number(chrGreRevisionRecovery) + Number(chrFirewallRestore) + Number(chrAddress) + Number(chrFirewallWrite) + Number(chrRead) + Number(chrIdentity) + Number(chrRecursive) + Number(chrFirewallMove) + Number(chrVpn) + Number(chrFilters) + Number(chrLegacyOspf) + Number(process.argv.includes('--chr')) > 1) throw new Error('Choose exactly one CHR acceptance mode') +const chr = process.argv.includes('--chr') || chrGreMutation || chrGreRevision || chrGreRevisionRecovery || chrFirewallRestore || chrAddress || chrFirewallWrite || chrRead || chrIdentity || chrRecursive || chrFirewallMove || chrVpn || chrFilters || chrLegacyOspf +if (recoveryMaintenance && (chr || greRevision || applicationAdapters || scopedRoutingDiagnostics || interfaceEventsCompatibility || wireguardTransfer || scopedCheckpointAdapters)) throw new Error('Recovery maintenance SQL/HTTP mode cannot run with another focused mode') +if (scopedCheckpointAdapters && (chr || greRevision || applicationAdapters || scopedRoutingDiagnostics || interfaceEventsCompatibility || wireguardTransfer)) throw new Error('Scoped checkpoint SQL/HTTP mode cannot run with another focused mode') +if (wireguardTransfer && (chr || greRevision || applicationAdapters || scopedRoutingDiagnostics || interfaceEventsCompatibility)) throw new Error('WireGuard transfer SQL/HTTP mode cannot run with another focused mode') +if (interfaceEventsCompatibility && (chr || greRevision || applicationAdapters || scopedRoutingDiagnostics)) throw new Error('Interface events SQL/HTTP mode cannot run with other focused modes') +if (scopedRoutingDiagnostics && (chr || greRevision || applicationAdapters)) throw new Error('Scoped routing SQL/HTTP mode cannot run with other focused modes') +if (applicationAdapters && (chr || greRevision)) throw new Error('Application SQL/HTTP mode cannot run with other focused modes') +if (greRevision && chr) throw new Error('GRE revision SQL/HTTP mode cannot run with CHR acceptance') +if (chr) { + if (!process.env.MMAPP_TEST_CHR_HOSTS || !process.env.MMAPP_TEST_CHR_USER) throw new Error('CHR hosts and user must be explicitly configured') + if (!process.env.MMAPP_TEST_CHR_PASSWORD) { + if (!process.stdin.isTTY) throw new Error('Use a terminal for the private CHR password prompt') + process.stdout.write('CHR password (hidden): ') + process.env.MMAPP_TEST_CHR_PASSWORD = await new Promise((resolve, reject) => { + let value = '' + const timer = setTimeout(() => finish(new Error('Password input timed out')), 60000) + const finish = error => { + clearTimeout(timer); process.stdin.off('data', read); process.stdin.setRawMode(false); process.stdin.pause(); process.stdout.write('\n') + if (error) reject(error); else resolve(value) + } + const read = chunk => { + for (const character of chunk.toString()) { + if (character === '\r' || character === '\n') { finish(); return } + if (character === '\u0003') { finish(new Error('Cancelled')); return } + if (character === '\u007f' || character === '\b') value = value.slice(0, -1) + else value += character + } + } + process.stdin.setRawMode(true); process.stdin.resume(); process.stdin.on('data', read) + }) + } +} + +// A private disposable cluster: never reads backend/.env or the application DB. +const directory = await mkdtemp(join(tmpdir(), 'mm-mcp-test-')) +const listener = createServer() +await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) +const port = listener.address().port +await new Promise(resolve => listener.close(resolve)) +const password = randomBytes(24).toString('hex') +const postgres = new EmbeddedPostgres({ databaseDir: join(directory, 'pg'), port, user: 'mcp_test', password, + persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'], postgresFlags: ['-h', '127.0.0.1'], + onLog: () => {}, onError: () => {} }) +let exitCode = 1 +let vrfChildrenExitConfirmed = true +try { + await postgres.initialise() + await postgres.start() + const binaries = await import(`@embedded-postgres/${process.platform === 'win32' ? 'windows' : process.platform}-${process.arch}`) + const testPath = [process.env.MMAPP_PG_CLIENT_BIN, dirname(binaries.postgres), process.env.PATH ?? process.env.Path ?? ''].filter(Boolean).join(delimiter) + if (!profilesOnly && (restoreQuarantineOnly || process.argv.includes('--restore') || process.env.MMAPP_PG_CLIENT_BIN)) { + for (const command of ['pg_dump', 'pg_restore']) await new Promise((resolve, reject) => { + execFile(command, ['--version'], { env: { ...process.env, PATH: testPath }, windowsHide: true, timeout: 5000, maxBuffer: 4096 }, error => { + if (error) reject(new Error(`Restore acceptance requires working ${command}; set MMAPP_PG_CLIENT_BIN to the PostgreSQL client bin directory`)) + else resolve() + }) + }) + console.log('PostgreSQL restore client tools preflight PASS') + } + if (regressionOnly) console.log('Selected regression only: primary integration and terminal restore are not run') + if (profilesOnly) console.log('Profiles-only regression matrix: primary integration NOT RUN; terminal restore NOT RUN; full acceptance is not claimed') + const result = regressionOnly || profilesOnly ? 0 : await new Promise((resolve, reject) => { + const child = spawn(process.execPath, pollMemory ? ['--max-old-space-size=192', '--max-semi-space-size=16', fileURLToPath(new URL('../dist-mcp-memory/services/server-poll-memory.owned.test.js', import.meta.url))] : ['--import', 'tsx', ...(flowActiveRebuild ? ['--import', new URL('../src/services/traffic-flow-active-rebuild-preload.mjs', import.meta.url).href] : []), usersBindings ? 'src/modules/mcp/users-bindings-owned.test.ts' : bridgeVlanConfiguration ? 'src/modules/mcp/bridge-vlan-configuration-http.test.ts' : protocolLimits ? 'src/modules/mcp/protocol-limits.owned.test.ts' : principalSecuritySource ? 'src/modules/mcp/principal-security-source.owned.test.ts' : pollJournalSnapshot ? 'src/services/server-poll-journal-snapshot.owned.test.ts' : pollTerminalArchive ? 'src/services/server-poll-terminal-archive.owned.test.ts' : pollArchiveSchema ? 'src/db/server-poll-terminal-archives.sql.test.ts' : telemetrySettingsConfiguration ? 'src/modules/mcp/telemetry-settings-configuration.owned.test.ts' : routingReservationSchema ? 'src/db/routing-reservation-schema.owned.test.ts' : routingForwardRounds ? 'src/modules/mcp/routing-network-forward-rounds.owned.test.ts' : flowActiveServices ? 'src/services/traffic-flow-active-services.owned.test.ts' : flowActiveRebuild ? 'src/services/traffic-flow-active-rebuild.owned.test.ts' : flowQuality ? 'src/services/traffic-flow-quality.owned.test.ts' : flowWorkerIpc ? 'src/services/traffic-flow-worker-ipc.owned.test.ts' : manualUptime ? 'src/modules/mcp/manual-uptime-http.test.ts' : manualPingGroup ? 'src/modules/mcp/manual-ping-group-http.test.ts' : manualTraffic ? 'src/modules/mcp/manual-traffic-http.test.ts' : manualApiPing ? 'src/modules/mcp/manual-api-ping-http.test.ts' : simpleSpeedChr ? 'src/modules/mcp/simple-speed-chr.test.ts' : simpleSpeed ? 'src/modules/mcp/simple-speed-http.test.ts' : externalPeerMetadata ? 'src/services/bandwidth-external-peer-metadata.sql.test.ts' : operatorPeerMetadata ? 'src/services/bandwidth-operator-metadata.sql.test.ts' : publicRegistryMetadata ? 'src/services/bandwidth-public-registry.sql.test.ts' : operatorDockerSql ? 'src/services/bandwidth-operator-docker-stop.sql.test.ts' : probeOperationRetirement ? 'src/services/server-probe-operation-retirement.owned.test.ts' : ordinaryOperationRetirement ? 'src/modules/mcp/ordinary-operation-retirement.owned.test.ts' : pollOperationRetirement ? 'src/services/server-poll-operation-retirement.owned.test.ts' : mcpOperationOrigin ? 'src/modules/mcp/operation-execution-origin.sql.test.ts' : mcpSpeedOriginal ? 'src/modules/mcp/speed-bandwidth-original.sql.test.ts' : routerConfigurationGuard ? 'src/services/router-configuration-guard.sql.test.ts' : scheduledSpeedOriginal ? 'src/services/bandwidth-scheduled-speed-original.sql.test.ts' : bandwidthSpeedWorkflow ? 'src/services/bandwidth-speed-workflow.sql.test.ts' : restSpeedOriginal ? 'src/services/bandwidth-rest-speed-original.sql.test.ts' : operationLocks ? 'src/services/operation-config-lock-lease.sql.test.ts' : serviceExecutionOrigin ? 'src/modules/mcp/service-execution-origin.sql.test.ts' : bandwidthNodeSource ? 'src/services/bandwidth-node-source.sql.test.ts' : bandwidthExternalRegistry ? 'src/services/bandwidth-external-node-registry.sql.test.ts' : bandwidthNodeRegistry ? 'src/services/bandwidth-node-registry.sql.test.ts' : bandwidthControlSource ? 'src/services/bandwidth-control-source.sql.test.ts' : bandwidthAdmission ? 'src/services/bandwidth-admission.sql.test.ts' : serverPollStorageFresh ? 'src/services/server-poll-storage-fresh.sql.test.ts' : serverPollStorage ? 'src/services/server-poll-storage.sql.test.ts' : probeStorageFresh ? 'src/services/server-probe-storage-fresh.sql.test.ts' : probeLegacyOrphans ? 'src/services/server-probe-legacy-orphans.sql.test.ts' : diagnosticCancelHost ? 'src/modules/mcp/diagnostic-cancellation-storage.sql.test.ts' : ipsecExportHttp ? 'src/modules/mcp/ipsec-existing-certificate-export-http.sql.test.ts' : ipsecExportJournal ? 'src/services/ipsec-existing-certificate-export-rest-journal.sql.test.ts' : migrationDataInventory ? 'src/db/migration-data-inventory.sql.test.ts' : probeStorageHistory ? 'src/services/server-probe-storage-history.sql.test.ts' : probeStorageJournal ? 'src/services/server-probe-storage-journal.sql.test.ts' : controlledSchemaReference ? 'src/db/controlled-schema-reference.sql.test.ts' : controlledMigrations ? 'src/db/controlled-migrations.sql.test.ts' : databaseSchemaControl ? 'src/db/database-schema-control.sql.test.ts' : migrationSchemaInventory ? 'src/db/migration-schema-inventory.sql.test.ts' : routingNetworkNative ? 'src/modules/mcp/routing-network-native.sql.test.ts' : routingReservationRuntime ? 'src/modules/mcp/routing-reservation-runtime.sql.test.ts' : routingReservations ? 'src/modules/mcp/routing-reservation.sql.test.ts' : collectorPartitionWriters ? 'src/services/collector-partition-writes.sql.test.ts' : serverPollDurable ? 'src/services/server-poll-durable-session.sql.test.ts' : routingRecoveryAuthority ? 'src/modules/mcp/routing-recovery-authority.sql.test.ts' : legacyFlowRebuild ? 'src/test/legacy-flow-rebuild-sql.ts' : probeJournal ? 'src/services/server-probe-journal.sql.test.ts' : flowHostKeys ? 'src/modules/mcp/flow-host-keys-http.test.ts' : flowRebuildMcp ? 'src/modules/mcp/flow-facts-rebuild-http.test.ts' : flowRebuild ? 'src/test/flow-facts-rebuild-sql.ts' : flowWrites ? 'src/services/traffic-flow-write-transaction.pg.test.ts' : nativeConservation ? 'src/modules/mcp/native-conservation-sql.test.ts' : nativeCreateId ? 'src/modules/mcp/native-create-id-http.test.ts' : ipsecAdapters ? "src/modules/mcp/ipsec-adapters-http.test.ts" : recoveryMaintenance ? "src/modules/mcp/recovery-maintenance-http.test.ts" : scopedCheckpointAdapters ? "src/modules/mcp/scoped-checkpoint-adapters-http.test.ts" : wireguardTransfer ? 'src/modules/mcp/wireguard-transfer-http.test.ts' : interfaceEventsCompatibility ? 'src/modules/mcp/interface-events-compatibility-http.test.ts' : scopedRoutingDiagnostics ? 'src/modules/mcp/scoped-routing-diagnostics-http.test.ts' : applicationAdapters ? 'src/modules/mcp/application-adapters-http.test.ts' : greRevision ? 'src/modules/mcp/gre-revision-http.test.ts' : chrGreRevisionRecovery ? 'src/modules/mcp/chr-gre-revision-recovery.test.ts' : chrGreRevision ? 'src/modules/mcp/chr-gre-revision.test.ts' : chrLegacyOspf ? 'src/modules/mcp/chr-ospf-legacy.test.ts' : chrFilters ? 'src/modules/mcp/chr-filter-write.test.ts' : chrGreMutation ? 'src/modules/mcp/chr-gre-mutation.test.ts' : chrFirewallRestore ? 'src/modules/mcp/chr-firewall-restore.test.ts' : chrAddress ? 'src/modules/mcp/chr-address-list.test.ts' : chrFirewallWrite ? 'src/modules/mcp/chr-firewall-write.test.ts' : chrVpn ? 'src/modules/mcp/chr-vpn-catalog.test.ts' : chrFirewallMove ? 'src/modules/mcp/chr-firewall-move.test.ts' : chrRecursive ? 'src/modules/mcp/chr-recursive-restore.test.ts' : chrIdentity ? 'src/modules/mcp/chr-ospf-identity-readonly.test.ts' : chrRead ? 'src/modules/mcp/chr-readonly-acceptance.test.ts' : chr ? 'src/modules/mcp/chr-acceptance.test.ts' : 'src/modules/mcp/integration.test.ts'], { + cwd: pollMemory ? directory : new URL('../', import.meta.url), stdio: 'inherit', env: { ...(pollMemory ? pollMemoryEnvironment : process.env), + MMAPP_OWN_SKIP_POLL_CAPACITY: String(pollRetirementFocused), + MMAPP_OWN_PROTOCOL_LIMITS: String(protocolLimits), + MMAPP_OWN_EVENTS_ONLY: String(eventsOnly), + MMAPP_OWN_MANAGEMENT_ADMISSION_ONLY: String(managementAdmissionOnly), + MMAPP_OWN_ADMINISTRATION_ADMISSION_ONLY: String(administrationAdmissionOnly), + MMAPP_OWN_CONFIGURATION_AUDIT_ONLY: String(configurationAuditOnly), + MMAPP_OWN_ROUTER_REQUEST_DRAIN_ONLY: String(routerRequestDrainOnly), + MMAPP_OWN_REST_REPLICA_ONLY: String(restReplicaOnly), + MMAPP_OWN_RESTORE_QUARANTINE_ONLY: String(restoreQuarantineOnly), + MMAPP_OWN_LOGS_SDK_SCENARIO: logsSdkScenario, + MMAPP_OWN_SQLITE_IMPORT_CLI_ONLY: String(sqliteImportCliOnly), + MMAPP_OWN_LEGACY_BGP_ONLY: String(legacyBgpOnly), + ...(routingOriginalEffects ? { MMAPP_OWN_ROUTING_ORIGINAL_EFFECTS: 'true' } : {}), + MMAPP_OWN_ROUTING_CONTROLLER_FAILOVER_ONLY: String(routingControllerFailoverOnly), + DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, + DATABASE_PATH: join(directory, 'no-legacy-database.sqlite'), SQLITE_IMPORT_FULL_HISTORY: 'false', + MCP_STATE_DIR: join(directory, 'state'), AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: 'mcp-integration-test-only', MMAPP_TEST_WG_EXPORT_ONLY: String(wireguardExport), + MCP_PUBLIC_URL: 'http://localhost:8000/mcp', NODE_ENV: 'test', PATH: testPath, NODE_OPTIONS: pollMemory ? '' : process.env.NODE_OPTIONS, MMAPP_OWN_PRINCIPAL_SECURITY_SOURCE: String(principalSecuritySource), MMAPP_OWN_POLL_JOURNAL_SNAPSHOT: String(pollJournalSnapshot), MMAPP_OWN_POLL_MEMORY: String(pollMemory), MMAPP_OWN_POLL_BOOTSTRAP: String(pollMemoryBootstrap), MMAPP_OWN_POLL_COST_DIAGNOSTICS: String(pollMemoryCost), MMAPP_OWN_POLL_ARCHIVE_CLAIM_LOSS: String(pollArchiveClaimLoss), MMAPP_OWN_POLL_ARCHIVE_HISTORY: String(pollArchiveHistory), MMAPP_OWN_POLL_ARCHIVE_COLD: String(pollArchiveCold), MMAPP_OWN_POLL_ARCHIVE_LOST_ACK: String(pollArchiveLostAck), MMAPP_OWN_POLL_TERMINAL_ARCHIVE: String(pollTerminalArchive), MMAPP_OWN_POLL_ARCHIVE_SCHEMA: String(pollArchiveSchema), MMAPP_OWN_TELEMETRY_SETTINGS_CONFIGURATION: String(telemetrySettingsConfiguration), MMAPP_OWN_ROUTING_RESERVATION_SCHEMA: String(routingReservationSchema), MMAPP_OWN_ROUTING_FORWARD_ROUNDS: String(routingForwardRounds), MMAPP_OWN_FLOW_ACTIVE_SERVICES: String(flowActiveServices), MMAPP_OWN_FLOW_ACTIVE_REBUILD: String(flowActiveRebuild), MMAPP_OWN_FLOW_QUALITY: String(flowQuality), MMAPP_OWN_FLOW_WORKER_IPC: String(flowWorkerIpc), MMAPP_OWN_MANUAL_UPTIME: String(manualUptime), MMAPP_OWN_MANUAL_PING_GROUP: String(manualPingGroup), MMAPP_OWN_MANUAL_TRAFFIC: String(manualTraffic), MMAPP_OWN_MANUAL_API_PING: String(manualApiPing), MMAPP_OWN_SIMPLE_SPEED: String(simpleSpeed), MMAPP_OWN_SIMPLE_SPEED_CHR: String(simpleSpeedChr), MMAPP_OWN_DIAGNOSTIC_CANCEL_HOST: String(diagnosticCancelHost), MMAPP_OWN_OPERATOR_DOCKER_SQL: String(operatorDockerSql), MMAPP_OWN_PUBLIC_REGISTRY_METADATA: String(publicRegistryMetadata), MMAPP_OWN_OPERATOR_PEER_METADATA: String(operatorPeerMetadata), MMAPP_OWN_EXTERNAL_PEER_METADATA: String(externalPeerMetadata), MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY: String(externalPeerMetadata || operatorPeerMetadata || publicRegistryMetadata || operatorDockerSql || mcpSpeedOriginal || bandwidthSpeedWorkflow || bandwidthExternalRegistry), MMAPP_OWN_BANDWIDTH_SPEED_WORKFLOW: String(bandwidthSpeedWorkflow), MMAPP_OWN_REST_SPEED_ORIGINAL: String(restSpeedOriginal), MMAPP_OWN_MCP_OPERATION_ORIGIN: String(mcpOperationOrigin), MMAPP_OWN_PROBE_OPERATION_RETIREMENT: String(probeOperationRetirement), MMAPP_OWN_MCP_SPEED_ORIGINAL: String(mcpSpeedOriginal), MMAPP_OWN_SCHEDULED_SPEED_ORIGINAL: String(scheduledSpeedOriginal), MMAPP_TEST_ROUTER_GUARD_OWNED: String(routerConfigurationGuard), MMAPP_TEST_ROUTER_GUARD_CLUSTER_DISPOSABLE: String(routerConfigurationGuard), MMAPP_TEST_ROUTER_GUARD_ADMIN_URL: routerConfigurationGuard ? `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres` : '', MMAPP_OWN_BANDWIDTH_ADMISSION: String(externalPeerMetadata || operatorPeerMetadata || publicRegistryMetadata || operatorDockerSql || mcpOperationOrigin || mcpSpeedOriginal || scheduledSpeedOriginal || bandwidthSpeedWorkflow || restSpeedOriginal || bandwidthExternalRegistry || operationLocks || serviceExecutionOrigin || bandwidthAdmission || bandwidthControlSource || bandwidthNodeRegistry || bandwidthNodeSource), MMAPP_OWN_BANDWIDTH_RESTORE: String((bandwidthAdmission || bandwidthControlSource || bandwidthNodeRegistry || bandwidthNodeSource) && process.argv.includes('--restore')), MMAPP_OWN_IPSEC_EXPORT_HTTP: String(ipsecExportHttp), MMAPP_OWN_IPSEC_EXPORT_JOURNAL: String(ipsecExportJournal), MMAPP_OWN_SQL_DB: 'postgres', MMAPP_OWN_PROBE_LEGACY_ORPHANS: String(probeLegacyOrphans), MMAPP_OWN_PROBE_STORAGE_HISTORY: String(probeStorageHistory), MMAPP_OWN_PROBE_STORAGE_FRESH: String(probeStorageFresh), MMAPP_OWN_PROBE_STORAGE_JOURNAL: String(probeStorageJournal || probeStorageFresh), MMAPP_OWN_CONTROLLED_SCHEMA_REFERENCE: String(controlledSchemaReference), MMAPP_OWN_CONTROLLED_MIGRATIONS: String(controlledMigrations), MMAPP_OWN_DATABASE_SCHEMA_CONTROL: String(databaseSchemaControl), MMAPP_OWN_MIGRATION_DATA_INVENTORY: String(migrationDataInventory), MMAPP_OWN_MIGRATION_SCHEMA_INVENTORY: String(migrationSchemaInventory), MMAPP_OWN_ROUTING_NETWORK_NATIVE: String(routingNetworkNative), MMAPP_OWN_ROUTING_RESERVATIONS: String(routingReservations || routingReservationRuntime), MMAPP_OWN_COLLECTOR_PARTITION_WRITERS: String(collectorPartitionWriters), MMAPP_OWN_SERVER_POLL_STORAGE_FRESH: String(serverPollStorageFresh), MMAPP_OWN_SERVER_POLL_STORAGE: String(serverPollStorage), MMAPP_OWN_SERVER_POLL_DURABLE: String(serverPollDurable), MMAPP_OWN_ROUTING_RECOVERY_AUTHORITY: String(routingRecoveryAuthority), MMAPP_OWN_LEGACY_FLOW_REBUILD: String(legacyFlowRebuild), MMAPP_OWN_PROBE_JOURNAL: String(probeJournal), MMAPP_MODULE_COMPLETION_ONLY: String(moduleCompletion), MMAPP_MODULE_COMPLETION_TARGET: completionTarget, MMAPP_OWN_FLOW_HOST_KEYS: String(flowHostKeys), MMAPP_OWN_MCP_FLOW_REBUILD: String(flowRebuildMcp), MMAPP_OWN_FLOW_WRITES: String(flowWrites), MMAPP_OWN_FLOW_REBUILD: String(flowRebuild) }, + }) + child.once('error', reject); child.once('exit', code => resolve(code ?? 1)) + }) + exitCode = result + if (result === 0 && restoreQuarantineOnly) console.log('Restore quarantine: focused real PostgreSQL restore scenario complete') + else if (result === 0 && !regressionOnly && !profilesOnly && !usersBindings && !bridgeVlanConfiguration && !protocolLimits && !pollMemory && !principalSecuritySource && !pollJournalSnapshot && !pollArchiveSchema && !telemetrySettingsConfiguration && !routingReservationSchema && !routingForwardRounds && !flowActiveServices && !flowActiveRebuild && !flowQuality && !flowWorkerIpc && !manualUptime && !manualPingGroup && !manualTraffic && !manualApiPing && !simpleSpeed && !simpleSpeedChr && !externalPeerMetadata && !operatorPeerMetadata && !publicRegistryMetadata && !operatorDockerSql && !mcpOperationOrigin && !mcpSpeedOriginal && !routerConfigurationGuard && !scheduledSpeedOriginal && !bandwidthSpeedWorkflow && !restSpeedOriginal && !operationLocks && !serviceExecutionOrigin && !bandwidthNodeSource && !bandwidthNodeRegistry && !bandwidthExternalRegistry && !bandwidthControlSource && !bandwidthAdmission && !probeLegacyOrphans && !probeStorageJournal && !probeStorageHistory && !probeJournal && !chr && !flowWrites && !flowRebuild && !flowRebuildMcp && (process.argv.includes('--restore') || process.env.MMAPP_PG_CLIENT_BIN)) { + exitCode = await new Promise((resolve, reject) => { + const child = spawn(process.execPath, ['--import', 'tsx', 'src/modules/mcp/database-restore.test.ts'], { + cwd: new URL('../', import.meta.url), stdio: 'inherit', env: { ...process.env, + DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, + DATABASE_PATH: join(directory, 'no-legacy-database.sqlite'), SQLITE_IMPORT_FULL_HISTORY: 'false', + MCP_STATE_DIR: join(directory, 'state'), AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: 'mcp-integration-test-only', + MCP_PUBLIC_URL: 'http://localhost:8000/mcp', NODE_ENV: 'test', PATH: testPath, + MMAPP_OWN_SQL_DB: 'postgres', MMAPP_OWN_TERMINAL_RESTORE: 'true', MMAPP_OWN_TERMINAL_DATA_EQUIVALENCE: String(migrationDataInventory) }, + }) + child.once('error', reject); child.once('exit', code => resolve(code ?? 1)) + }) + } + else if (result === 0 && pollArchiveSchema) console.log('Poll archive schema: owned nonempty40 preservation, numbered41 and structural refusal guards; no positive MCP archive claim') + else if (result === 0 && telemetrySettingsConfiguration) console.log('Telemetry settings: genuine MCP/SQL configuration binding, collector metadata tolerated; no native or external sources') + else if (result === 0 && routingReservationSchema) console.log('Routing reservation numbered0040: ownSQL fresh/legacy/manual preservation; no READY, backfill, capabilities or public native activation') + else if (result === 0 && routingOriginalEffects) console.log('Routing original effects: approved manual revision 2 and original effects checked against disposable PostgreSQL and synthetic RouterOS HTTP') + else if (result === 0 && routingForwardRounds) console.log('Routing forward rounds: genuine approved running CONFIG_APPLIED, shared cumulative native budget; logs/security/autoRecovery remain unaccepted') + else if (result === 0 && flowActiveServices) console.log('Flow active settings/purge: production services and main Worker; disposable SQL/localhost only') + else if (result === 0 && flowActiveRebuild) console.log(process.env.MMAPP_OWN_FLOW_ACTIVE_READ === 'true' + ? 'Flow active analytics: production UDP Worker, REST and MCP; disposable SQL/localhost only' + : 'Flow active rebuild: production shared service and genuine Worker drain; disposable SQL/localhost only') + else if (result === 0 && flowQuality) console.log('Flow quality: production engine and genuine receipt SQL, explicit COMMIT ACK fault on owned cluster only; no CHR/whole-installation RAM claim') + else if (result === 0 && flowWorkerIpc) console.log('Flow worker IPC: production main/Worker lifecycle, localhost UDP, owned PostgreSQL only; real CHR and concurrent-spawn acceptance remain separate') + else if (result === 0 && manualUptime) console.log('Manual uptime: full resources then ping, actual shared service and deferred alerts; owned PostgreSQL/localhost only') + else if (result === 0 && manualPingGroup) console.log('Manual Ping group: existing normalization/mutex/samples/conditional global alerts; owned PostgreSQL/localhost only') + else if (result === 0 && manualTraffic) console.log('Manual traffic: full scheduler/events/settings/retention/alerts shared by REST and MCP; owned PostgreSQL/localhost only') + else if (result === 0 && manualApiPing) console.log('Manual API Ping: ordinary global collector with alerts hook, own PostgreSQL and localhost only') + else if (result === 0 && simpleSpeed) console.log('Simple RouterOS speed: public MCP/REST, ordinary shared service, own PostgreSQL and localhost RouterOS only') + else if (result === 0 && simpleSpeedChr) console.log('Simple RouterOS speed: own PostgreSQL, real two-CHR bounded test and ordinary MCP approval; no router configuration changes') + else if (result === 0 && externalPeerMetadata) console.log('Guarded peer sources: actual managed/external SQL-owner and localhost license composition; Q/catalog attestation synthetic, full operator window remains open') + else if (result === 0 && operatorPeerMetadata) console.log('Operator peer metadata: actual managed endpoint SQL and genuine localhost license origin; synthetic Q, external metadata and full operator window remain open') + else if (result === 0 && publicRegistryMetadata) console.log('Public registry metadata: actual owned PostgreSQL and same physical lease; no secret/Q/native/write authority or backend-window acceptance') + else if (result === 0 && operatorDockerSql) console.log('Operator Docker SQL: actual owned PostgreSQL with mocked Docker HTTP only; not a real Docker/backend-window acceptance') + else if (result === 0 && ordinaryOperationRetirement) console.log('Ordinary operation retirement: owned actual logs-settings apply and worker') + else if (result === 0 && pollOperationRetirement) console.log('Poll operation retirement: owned authenticated historical fixtures and current reader/GC') + else if (result === 0 && probeOperationRetirement) console.log('Diagnostic retirement: actual owned worker tick, PostgreSQL and product read/replay; no live scheduling') + else if (result === 0 && mcpOperationOrigin) console.log('MCP operation root: owned PostgreSQL ordinary runner provenance; no restore/recovery, global writer activation, CHR or deployment') + else if (result === 0 && mcpSpeedOriginal) console.log('MCP speed: genuine trusted service context with real connection/grant/SQL, host receipt and localhost native; synthetic Q/mapping; not a public MCP runner/action acceptance') + else if (result === 0 && routerConfigurationGuard) console.log('Router configuration guard: dedicated owned databases/roles and localhost native fixture; no application migration, production activation or real CHR') + else if (result === 0 && scheduledSpeedOriginal) console.log('Scheduled speed original: genuine fixed-job scheduler authority, actual owned SQL and localhost read-only TCP; no legacy collector, native bandwidth, history or runtime activation') + else if (result === 0 && bandwidthSpeedWorkflow) console.log('Whole speed workflow: actual REST/JWT, signed v2 control SQL, native fixture HTTP and history; synthetic Q/mapping; no runtime activation or real CHR') + else if (result === 0 && restSpeedOriginal) console.log('REST speed original profile: owned application SQL and localhost read-only TCP; no bandwidth POST, history, runtime activation or real CHR') + else if (result === 0 && operationLocks) console.log('Operation lock profile: owned PostgreSQL/localhost fixtures; no terminal restore or real CHR') + else if (result === 0 && serviceExecutionOrigin) console.log('Service origin profile: owned SQL only; no terminal restore or native networking') + else if (result === 0 && (bandwidthAdmission || bandwidthControlSource || bandwidthNodeRegistry || bandwidthExternalRegistry || bandwidthNodeSource)) console.log('Bandwidth proposal profile: dedicated owned data round-trip only; no normal enrollment or production restore claim') + else if (result === 0 && (probeLegacyOrphans || probeStorageJournal || probeStorageHistory || probeJournal)) console.log('Legacy-only profile: positive current-version terminal restore belongs to --probe-storage-fresh; no migration marker fabricated') + else if (result === 0 && profilesOnly) console.log(profilesGroup ? 'Profiles-only fixed group: ' + profilesGroup + '; partial matrix on its own fresh database' : 'Profiles-only: running the unchanged complete regression list plus mandatory test:logs:pg on one fresh owned database') + else if (result === 0 && serverPollStorageFresh && process.env.MMAPP_OWN_SERVER_POLL_SETTINGS_EPOCH === 'true') console.log('Targeted settings epoch upgrade and actual pg_dump/restore accepted; full primary/poll-storage terminal restore not run') + else if (result === 0 && !chr && !flowWrites && !flowRebuild && !flowRebuildMcp) console.log('Terminal restore not run: use --restore with PostgreSQL client tools on PATH, or set MMAPP_PG_CLIENT_BIN') + if (exitCode === 0 && (serverPollStorage || serverPollStorageFresh) && (process.argv.includes('--restore') || process.env.MMAPP_PG_CLIENT_BIN)) { + exitCode = await new Promise((resolve, reject) => { + const child = spawn(process.execPath, ['--import', 'tsx', 'src/services/server-poll-storage-restore.sql.test.ts'], { + cwd: new URL('../', import.meta.url), stdio: 'inherit', env: { ...process.env, + DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, + DATABASE_PATH: join(directory, 'no-legacy-database.sqlite'), SQLITE_IMPORT_FULL_HISTORY: 'false', + MCP_STATE_DIR: join(directory, 'state'), AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: 'mcp-integration-test-only', + NODE_ENV: 'test', PATH: testPath, MMAPP_OWN_SERVER_POLL_STORAGE_RESTORE: 'true' }, + }) + child.once('error', reject); child.once('exit', code => resolve(code ?? 1)) + }) + } + if (exitCode === 0 && process.argv.includes('--all')) { + if (!process.env.npm_execpath) throw new Error('Run --all through npm run test:mcp:pg:all') + const database = 'mm_accept_166_' + Date.now() + await postgres.createDatabase(database) + const packageJson = JSON.parse(await readFile(new URL('../package.json', import.meta.url), 'utf8')) + let profiles = packageJson.scripts['test:all'].split(/\s*&&\s*/).map(command => command.replace('npm run ', '')) + if (process.argv.includes('--remaining')) profiles = profiles.slice(profiles.indexOf('test:traffic-flow')) + const selected = process.argv.find(value => value.startsWith('--profile='))?.slice('--profile='.length) + if (selected) { + if (![...profiles, 'test:logs:pg', 'test:vrf:production'].includes(selected)) throw new Error('Unknown regression profile') + profiles = [selected] + } + const failures = [] + const requiredProfiles = regressionOnly ? [selected] : regressionProfiles(profiles, selected, profilesGroup) + for (const script of requiredProfiles) { + const startedAt = Date.now() + console.log('Regression profile started:', script) + if (script === 'test:vrf:production') { + vrfChildrenExitConfirmed = false + const receipt = await runOwnedVrfProductionProfile({ directory, database, databaseUrl: `postgresql://mcp_test:${password}@127.0.0.1:${port}/${database}` }) + vrfChildrenExitConfirmed = receipt.childExitConfirmed + if (!receipt.accepted) failures.push(script) + console.log('Regression profile finished:', JSON.stringify({ profile: script, passed: receipt.accepted, durationMs: Date.now() - startedAt })) + if (!vrfChildrenExitConfirmed) break + continue + } + const status = await new Promise((resolve, reject) => { + const child = spawn(process.execPath, [process.env.npm_execpath, 'run', script], { + cwd: new URL('../', import.meta.url), stdio: 'inherit', env: { ...process.env, + DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/${database}`, + DATABASE_PATH: join(directory, 'no-legacy-database.sqlite'), SQLITE_IMPORT_FULL_HISTORY: 'false', + MMAPP_OWN_SQL_DB: database, MCP_STATE_DIR: join(directory, 'regression-state'), + REQUIRE_TEST_POSTGRES: '1', + LOGS_PERFORMANCE: process.argv.includes('--performance') ? '1' : '0', + AUTH_REQUIRED: 'false', NODE_ENV: 'test' }, + }) + child.once('error', reject); child.once('exit', code => resolve(code ?? 1)) + }) + if (status !== 0) failures.push(script) + console.log('Regression profile finished:', JSON.stringify({ profile: script, passed: status === 0, durationMs: Date.now() - startedAt })) + } + exitCode = failures.length ? 1 : 0 + console.log('Regression failed profiles:', failures.length ? failures.join(', ') : 'none') + if (profilesOnly) console.log('Profiles-only matrix result:', exitCode === 0 ? 'PASS' : 'FAIL', 'profiles=' + requiredProfiles.length, ...(profilesGroup ? ['group=' + profilesGroup, 'completeMatrix=false'] : []), 'primaryIntegration=NOT_RUN terminalRestore=NOT_RUN fullAcceptance=false') + } +} catch (error) { + exitCode = 1 + console.error(error) +} finally { + await postgres.stop() + const rel = relative(resolve(tmpdir()), resolve(directory)) + if (!rel || rel.startsWith('..') || isAbsolute(rel) || !rel.startsWith('mm-mcp-test-')) throw new Error('Unsafe test cleanup path') + if (vrfChildrenExitConfirmed) await rm(directory, { recursive: true, force: true, maxRetries: 10, retryDelay: 250 }) + else console.error('VRF child exit unconfirmed; own test directory retained') + process.exitCode = exitCode +} +// embedded-postgres installs a beforeExit hook that otherwise forces exit(0). +process.exit(exitCode) diff --git a/backend/scripts/test-routing-controller-baseline.mjs b/backend/scripts/test-routing-controller-baseline.mjs new file mode 100644 index 00000000..2ae3d41f --- /dev/null +++ b/backend/scripts/test-routing-controller-baseline.mjs @@ -0,0 +1,75 @@ +import EmbeddedPostgres from 'embedded-postgres' +import { createRequire } from 'node:module' +import { mkdir, mkdtemp, readdir, readFile, writeFile, cp, rm } from 'node:fs/promises' +import { resolve, join, relative, dirname, basename } from 'node:path' +import { randomBytes, createHash } from 'node:crypto' +import { createServer } from 'node:net' +import { spawn } from 'node:child_process' + +const modes = ['--default-scoped', '--main-only', '--control-path-context', '--scoped-bare-readback', '--scoped-resume-forward', '--scoped-inverse-expiry', '--scoped-inverse-post-probe-expiry', '--scoped-inverse-checkpoint-drift', '--scoped-inverse-route-effects', '--scoped-inverse-effects-checkpoint-drift', '--scoped-inverse-lost-ack', '--manual-forward-expiry', '--manual-forward-post-intent-expiry'] +const allowed = new Set([...modes, '--check-only']), args = process.argv.slice(2) +const selected = modes.filter(flag => args.includes(flag)) +if (args.some(arg => !allowed.has(arg)) || new Set(args).size !== args.length || selected.length > 1) throw new Error('Usage: node backend/scripts/test-routing-controller-baseline.mjs [' + modes.join(' | ') + '] [--check-only]') +const root = resolve(import.meta.dirname, '../..'), require = createRequire(import.meta.url), ts = require('typescript') +const mode = selected[0]?.slice(2) ?? 'main-and-forward-rounds' +const target = join(root, 'backend/src/modules/mcp/routing-controller-active-writer.ts') +await readFile(target) +await readFile(join(root, 'backend/src/modules/mcp/routing-main-transition.ts')) +await readFile(join(root, 'backend/src/modules/mcp/routing-controller-baseline-acceptance.ts')) +if (process.argv.includes('--check-only')) { console.log(JSON.stringify({ mode, sourcePresent: true, acceptanceExecuted: false, productionApplied: false })); process.exit(0) } +await mkdir(join(root, 'tmp'), { recursive: true }) +const ownedPrefix = 'mm-mcp-test-routing-controller-baseline-' +const directory = await mkdtemp(join(root, 'tmp', ownedPrefix)), output = join(directory, 'dist'), sourcePins = [] +const hash = buffer => createHash('sha256').update(buffer).digest('hex') +async function compile(sourceDirectory) { + for (const entry of await readdir(sourceDirectory, { withFileTypes: true })) { + const path = join(sourceDirectory, entry.name) + if (entry.isDirectory()) { await compile(path); continue } + const name = relative(join(root, 'backend/src'), path).replaceAll('\\', '/') + if (name.endsWith('.test.ts') || name.endsWith('.d.ts')) continue + const bytes = await readFile(path), emitted = join(output, name.replace(/\.ts$/, '.js')) + await mkdir(dirname(emitted), { recursive: true }) + await writeFile(emitted, name.endsWith('.ts') ? ts.transpileModule(bytes.toString('utf8'), { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext, esModuleInterop: true, resolveJsonModule: true } }).outputText : bytes) + sourcePins.push({ path: name, sourceSha256: hash(bytes), emittedSha256: hash(await readFile(emitted)) }) + } +} +await writeFile(join(directory, 'package.json'), '{"type":"module"}') +await compile(join(root, 'backend/src')) +if (mode === 'scoped-resume-forward') { + for (const file of ['mcp-step-journal.mjs', 'mcp-routing-forward-observer.mjs']) { + await cp(new URL('./acceptance/' + file, import.meta.url), join(directory, file)) + } +} +await cp(join(root, 'backend/drizzle'), join(directory, 'drizzle'), { recursive: true }) +await writeFile(join(directory, 'snapshot-manifest.json'), JSON.stringify({ mode, productionApplied: false, wholeMutableWorktreeFrozen: false, sourcePins }, null, 2)) +const listener = createServer() +await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) +const port = listener.address().port +await new Promise(resolve => listener.close(resolve)) +const password = randomBytes(24).toString('hex') +const postgres = new EmbeddedPostgres({ databaseDir: join(directory, 'pg'), port, user: 'mcp_test', password, + persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'], postgresFlags: ['-h', '127.0.0.1'], onLog: () => {}, onError: () => {} }) +let exitCode = 1, log = '' +console.log(`Owned routing baseline acceptance: ${directory}; mode=${mode}`) +try { + await postgres.initialise(); await postgres.start() + const retained = Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path', 'LOCALAPPDATA', 'APPDATA'].filter(key => process.env[key]).map(key => [key, process.env[key]])) + exitCode = await new Promise((resolve, reject) => { + const child = spawn(process.execPath, [join(output, 'test/routing-controller-baseline-acceptance-main.js')], { cwd: directory, windowsHide: true, + env: { ...retained, DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, DATABASE_PATH: join(directory, 'no-legacy.sqlite'), + MCP_STATE_DIR: join(directory, 'state'), NODE_ENV: 'test', AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: 'routing-baseline-test-only', MCP_PUBLIC_URL: 'http://localhost:8000/mcp', + MMAPP_ROUTING_BASELINE_OWN_SQL: 'true', MMAPP_ROUTING_BASELINE_MODE: mode } }) + child.stdout.on('data', bytes => { process.stdout.write(bytes); log += bytes }); child.stderr.on('data', bytes => { process.stderr.write(bytes); log += bytes }) + child.once('error', reject); child.once('exit', code => resolve(code ?? 1)) + }) +} finally { + await writeFile(join(directory, 'runtime.log'), log) + // embedded-postgres waits for a new exit event; avoid waiting again if its + // owned child has already exited. This never targets another database. + if (postgres.process?.exitCode === null) await postgres.stop() + if (dirname(resolve(directory)) !== resolve(root, 'tmp') || !basename(directory).startsWith(ownedPrefix)) throw new Error('Owned routing cleanup boundary changed') + await rm(join(directory, 'pg'), { recursive: true, force: true, maxRetries: 20, retryDelay: 100 }) + console.log('Owned routing PostgreSQL stopped and its data removed; snapshot/log evidence retained') +} +console.log(JSON.stringify({ runDirectory: directory, mode, exitCode, productionApplied: false })) +process.exitCode = exitCode diff --git a/backend/scripts/test-server-poll-approval-owned.mjs b/backend/scripts/test-server-poll-approval-owned.mjs new file mode 100644 index 00000000..35cb769b --- /dev/null +++ b/backend/scripts/test-server-poll-approval-owned.mjs @@ -0,0 +1,17 @@ +/** Fixed local delayed-approval acceptance; only the original guarded runner + * creates/removes a disposable PostgreSQL. No target or restore arguments. */ +import assert from 'node:assert/strict' +import {spawn} from 'node:child_process' +import {dirname,resolve} from 'node:path' +import {fileURLToPath} from 'node:url' +assert.equal(process.argv.length,2) +const root=resolve(dirname(fileURLToPath(import.meta.url)),'../..') +const environment={...process.env,NODE_ENV:'test',MMAPP_OWN_SERVER_POLL_DELAYED_APPROVAL:'true', + MMAPP_OWN_SERVER_POLL_HTTP_ROUTE:'false',MMAPP_OWN_SERVER_POLL_SETTINGS_EPOCH:'false'} +// This fixed profile never requests the original runner's full terminal restore. +delete environment.MMAPP_PG_CLIENT_BIN +const child=spawn(process.execPath,[resolve(root,'backend/scripts/test-mcp-postgres.mjs'),'--server-poll-storage-fresh'],{ + cwd:root,windowsHide:true,stdio:'inherit',env:environment, +}) +const code=await new Promise((done,reject)=>{child.once('error',reject);child.once('exit',code=>done(code??1))}) +process.exitCode=code diff --git a/backend/scripts/test-server-poll-atomic-owned.mjs b/backend/scripts/test-server-poll-atomic-owned.mjs new file mode 100644 index 00000000..a350fe89 --- /dev/null +++ b/backend/scripts/test-server-poll-atomic-owned.mjs @@ -0,0 +1,12 @@ +/** Fixed disposable PostgreSQL profile, no target/deploy/restore arguments. */ +import assert from 'node:assert/strict' +import {spawn} from 'node:child_process' +import {dirname,resolve} from 'node:path' +import {fileURLToPath} from 'node:url' +assert.equal(process.argv.length,2) +const root=resolve(dirname(fileURLToPath(import.meta.url)),'../..') +const environment={...process.env,NODE_ENV:'test',MMAPP_OWN_SERVER_POLL_ATOMIC_MAINTENANCE:'true',MMAPP_OWN_SERVER_POLL_DELAYED_APPROVAL:'false',MMAPP_OWN_SERVER_POLL_HTTP_ROUTE:'false',MMAPP_OWN_SERVER_POLL_SETTINGS_EPOCH:'false'} +delete environment.MMAPP_PG_CLIENT_BIN +const child=spawn(process.execPath,[resolve(root,'backend/scripts/test-mcp-postgres.mjs'),'--server-poll-storage-fresh'],{cwd:root,windowsHide:true,stdio:'inherit',env:environment}) +const code=await new Promise((done,reject)=>{child.once('error',reject);child.once('exit',code=>done(code??1))}) +process.exitCode=code diff --git a/backend/scripts/test-server-poll-http-owned.mjs b/backend/scripts/test-server-poll-http-owned.mjs new file mode 100644 index 00000000..9ca5aeaa --- /dev/null +++ b/backend/scripts/test-server-poll-http-owned.mjs @@ -0,0 +1,13 @@ +/** Owns no application resources directly: the original guarded PostgreSQL + * runner creates/removes its fresh cluster and supplies every connection. */ +import assert from 'node:assert/strict' +import {spawn} from 'node:child_process' +import {dirname,resolve} from 'node:path' +import {fileURLToPath} from 'node:url' +assert.equal(process.argv.length,2,'HTTP poll acceptance takes no target or connection arguments') +const root=resolve(dirname(fileURLToPath(import.meta.url)),'../..') +const child=spawn(process.execPath,[resolve(root,'backend/scripts/test-mcp-postgres.mjs'),'--server-poll-storage-fresh'],{ + cwd:root,windowsHide:true,stdio:'inherit',env:{...process.env,NODE_ENV:'test',MMAPP_OWN_SERVER_POLL_HTTP_ROUTE:'true'}, +}) +const code=await new Promise((done,reject)=>{child.once('error',reject);child.once('exit',code=>done(code??1))}) +process.exitCode=code diff --git a/backend/scripts/test-server-poll-settings-epoch-owned.mjs b/backend/scripts/test-server-poll-settings-epoch-owned.mjs new file mode 100644 index 00000000..b0fcebb0 --- /dev/null +++ b/backend/scripts/test-server-poll-settings-epoch-owned.mjs @@ -0,0 +1,13 @@ +/** Fixed-target numbered migration and restore acceptance. Only the existing guarded runner creates + * the disposable PostgreSQL; no connection/remote/restore arguments accepted. */ +import assert from 'node:assert/strict' +import {spawn} from 'node:child_process' +import {dirname,resolve} from 'node:path' +import {fileURLToPath} from 'node:url' +assert.equal(process.argv.length,2,'Settings epoch acceptance takes no target or connection arguments') +const root=resolve(dirname(fileURLToPath(import.meta.url)),'../..') +const child=spawn(process.execPath,[resolve(root,'backend/scripts/test-mcp-postgres.mjs'),'--server-poll-storage-fresh'],{ + cwd:root,windowsHide:true,stdio:'inherit',env:{...process.env,NODE_ENV:'test',MMAPP_OWN_SERVER_POLL_SETTINGS_EPOCH:'true',MMAPP_OWN_SERVER_POLL_HTTP_ROUTE:'false'}, +}) +const code=await new Promise((done,reject)=>{child.once('error',reject);child.once('exit',code=>done(code??1))}) +process.exitCode=code diff --git a/backend/scripts/test-source-capture-adopted-postgres.mjs b/backend/scripts/test-source-capture-adopted-postgres.mjs new file mode 100644 index 00000000..90278d1a --- /dev/null +++ b/backend/scripts/test-source-capture-adopted-postgres.mjs @@ -0,0 +1,87 @@ +// Isolated regression only: no application config, configured DB URL or startup. +import { mkdtemp, rm, readFile, writeFile, mkdir } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve, relative, isAbsolute, basename } from 'node:path' +import { fileURLToPath, pathToFileURL } from 'node:url' +import { createRequire } from 'node:module' +import { randomBytes, createHash } from 'node:crypto' +import { createServer } from 'node:net' +import { spawn } from 'node:child_process' + +const args = process.argv.slice(2) +if (args.length > 1 || args[0] && !args[0].startsWith('--source-root=')) throw Error('Only --source-root=PATH review override is supported') +const sourceRoot = args[0] ? resolve(args[0].slice('--source-root='.length)) : fileURLToPath(new URL('../../', import.meta.url)) +const require = createRequire(join(sourceRoot, 'backend/package.json')) +const { default: EmbeddedPostgres } = await import(pathToFileURL(require.resolve('embedded-postgres')).href) +const sources = ['backend/src/services/partition-write-transaction.ts', 'backend/src/db/sqlite-import-control-writes.ts', + 'backend/src/db/migration-schema-inventory.ts', 'backend/src/db/migration-data-inventory.ts', 'backend/src/db/schema.ts', 'backend/src/db/index.ts'] +const ownFiles = [fileURLToPath(import.meta.url), fileURLToPath(new URL('../src/db/database-source-capture-adopted.sql-helper.ts', import.meta.url)), + fileURLToPath(new URL('../src/db/database-source-capture-adopted.sql.test.ts', import.meta.url))] +async function pins() { + const files = [...sources.map(path => ({ path, absolute: join(sourceRoot, path) })), ...ownFiles.map(absolute => ({ path: 'executed-fixture/' + basename(absolute), absolute }))] + return Promise.all(files.map(async ({ path, absolute }) => ({ path, sha256: createHash('sha256').update(await readFile(absolute)).digest('hex') }))) +} +const before = await pins() +const directory = await mkdtemp(join(tmpdir(), 'mm-source-capture-')) +const password = randomBytes(24).toString('hex'), ownerToken = randomBytes(24).toString('hex') +let postgres, failure, phase = 'port-and-owner-manifest', failedPhase, childExitCode = 1, unchanged = false, clusterStopped = false, directoryRemoved = false +try { + const listener = createServer() + await new Promise((done, reject) => { listener.once('error', reject); listener.listen(0, '127.0.0.1', done) }) + const port = listener.address().port + await new Promise((done, reject) => listener.close(error => error ? reject(error) : done())) + await writeFile(join(directory, 'owned-source-capture.json'), JSON.stringify({ ownerToken, port, username: 'source_operator' }), { flag: 'wx' }) + postgres = new EmbeddedPostgres({ databaseDir: join(directory, 'pg'), port, user: 'source_operator', password, + persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'], postgresFlags: ['-h', '127.0.0.1'], onLog: () => {}, onError: () => {} }) + phase = 'initialise-owned-postgres'; await postgres.initialise() + phase = 'start-owned-postgres'; await postgres.start() + phase = 'owned-sql-child' + childExitCode = await new Promise((done, reject) => { + const child = spawn(process.execPath, ['--import', pathToFileURL(require.resolve('tsx')).href, ownFiles[2]], { + cwd: directory, windowsHide: true, stdio: 'inherit', env: { + ...Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path'].filter(key => process.env[key]).map(key => [key, process.env[key]])), + MMAPP_OWN_SOURCE_CAPTURE: 'true', MMAPP_OWN_SOURCE_CAPTURE_DIRECTORY: directory, MMAPP_OWN_SOURCE_CAPTURE_ROOT: sourceRoot, + MMAPP_OWN_SOURCE_CAPTURE_TOKEN: ownerToken, MMAPP_OWN_SOURCE_CAPTURE_ADMIN_URL: `postgresql://source_operator:${password}@127.0.0.1:${port}/postgres`, + }, + }) + // No child subprocesses are spawned by the SQL fixture. Kill only this + // exact Node child on timeout, then await exit before stopping own cluster. + let timedOut = false + const timer = setTimeout(() => { timedOut = true; child.kill(); }, 120000) + child.once('error', error => { clearTimeout(timer); reject(error) }) + child.once('exit', code => { clearTimeout(timer); done(timedOut ? 1 : code ?? 1) }) + }) +} catch (error) { failure = error; failedPhase = phase } +finally { + try { + if (postgres) await postgres.stop() + clusterStopped = true + } catch (error) { failure ??= error } + // Delete only when stop succeeded, retaining failed-stop files for diagnosis. + if (clusterStopped) { + try { + const root = resolve(tmpdir()), target = resolve(directory), rel = relative(root, target) + if (!rel || rel.startsWith('..') || isAbsolute(rel) || !basename(target).startsWith('mm-source-capture-')) throw Error('Unsafe owned source capture cleanup path') + await rm(target, { recursive: true, force: true }); directoryRemoved = true + } catch (error) { failure ??= error } + } + try { + const after = await pins() + unchanged = JSON.stringify(before) === JSON.stringify(after) + await mkdir(join(sourceRoot, 'tmp'), { recursive: true }) + const evidencePath = join(sourceRoot, 'tmp', basename(directory) + '-pins.json') + await writeFile(evidencePath, JSON.stringify({ format: 'adopted-source-capture-owned-pins-v1', date: new Date().toISOString(), + before, after, unchanged, childExitCode, clusterStopped, directoryRemoved, + allWriterDrainAuthenticated: false, sourceGenerationAuthenticated: false, readyAuthorized: false }, null, 2)) + console.log('Owned source capture evidence:', evidencePath) + } catch (error) { failure ??= error } +} +console.log(JSON.stringify({ childExitCode, sourcePinsUnchanged: unchanged, clusterStopped, directoryRemoved, + allWriterDrainAuthenticated: false, sourceGenerationAuthenticated: false, readyAuthorized: false })) +// Never print connection strings or credentials in fixture error output. +if (failure) { + const text = String(failure) + const knownCode = ['ENOMEM', 'EACCES', 'EPERM', 'ENOENT', 'EADDRINUSE', 'ENOSPC'].find(code => text.includes(code)) ?? 'UNCLASSIFIED' + console.error(JSON.stringify({ ownedRunnerFailure: true, phase: failedPhase ?? 'pins-or-cleanup', knownCode })) +} +process.exitCode = failure || !unchanged || !clusterStopped || !directoryRemoved ? 1 : childExitCode diff --git a/backend/scripts/test-sqlite-import-control-postgres.mjs b/backend/scripts/test-sqlite-import-control-postgres.mjs new file mode 100644 index 00000000..b825f08c --- /dev/null +++ b/backend/scripts/test-sqlite-import-control-postgres.mjs @@ -0,0 +1,51 @@ +// Permanent runner candidate: fresh loopback PostgreSQL, no app config/.env. +import EmbeddedPostgres from 'embedded-postgres' +import { mkdtemp, rm, readFile, writeFile, mkdir } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve, relative, isAbsolute } from 'node:path' +import { fileURLToPath } from 'node:url' +import { randomBytes, createHash } from 'node:crypto' +import { createServer } from 'node:net' +import { spawn } from 'node:child_process' +const args = process.argv.slice(2) +if (args.length > 1 || args[0] && !args[0].startsWith('--production-root=')) throw Error('Only an explicit source-root override is supported for TMP review') +const sourceRoot = args[0] ? resolve(args[0].slice('--production-root='.length)) : fileURLToPath(new URL('../../', import.meta.url)) +const sources = ['backend/src/services/partition-write-transaction.ts', 'backend/src/db/sqlite-import-control-writes.ts', 'backend/src/db/sqlite-import.ts', 'backend/src/db/schema.ts'] +async function pins() { + const result = [] + for (const path of sources) result.push({ path, sha256: createHash('sha256').update(await readFile(join(sourceRoot, path))).digest('hex') }) + return result +} +const before = await pins(), directory = await mkdtemp(join(tmpdir(), 'mm-import-control-')) +const listener = createServer(); await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) +const port = listener.address().port; await new Promise(resolve => listener.close(resolve)) +const password = randomBytes(24).toString('hex') +const postgres = new EmbeddedPostgres({ databaseDir: join(directory, 'pg'), port, user: 'import_operator', password, + persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'], postgresFlags: ['-h', '127.0.0.1'], onLog: () => {}, onError: () => {} }) +try { + await postgres.initialise(); await postgres.start() + process.exitCode = await new Promise((done, reject) => { + const child = spawn(process.execPath, ['--import', import.meta.resolve('tsx'), fileURLToPath(new URL('../src/db/sqlite-import-control-writes.sql.test.ts', import.meta.url))], { + cwd: directory, windowsHide: true, stdio: 'inherit', env: { + ...Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path'].filter(k => process.env[k]).map(k => [k, process.env[k]])), + MMAPP_OWN_IMPORT_CONTROL: 'true', MMAPP_OWN_IMPORT_DIRECTORY: directory, MMAPP_OWN_IMPORT_SOURCE_ROOT: sourceRoot, + MMAPP_OWN_IMPORT_ADMIN_URL: `postgresql://import_operator:${password}@127.0.0.1:${port}/postgres` }, + }) + child.once('error', reject); child.once('exit', code => done(code ?? 1)) + }) + const after = await pins(), unchanged = JSON.stringify(before) === JSON.stringify(after) + const report = { format: 'sqlite-import-control-owned-pins-v1', date: new Date().toISOString(), before, after, unchanged, childExitCode: process.exitCode } + // Keep generated evidence out of source directories after integration. + // This is a local test artifact, never a production authority record. + await mkdir(join(sourceRoot, 'tmp'), { recursive: true }) + await writeFile(join(sourceRoot, 'tmp', 'sqlite-import-control-source-pins.json'), JSON.stringify(report, null, 2)) + console.log('Current production source pins unchanged:', unchanged) + if (!unchanged) process.exitCode = 1 +} finally { + await postgres.stop() + const root = resolve(tmpdir()), target = resolve(directory), rel = relative(root, target) + if (!rel || rel.startsWith('..') || isAbsolute(rel) || !target.includes('mm-import-control-')) throw Error('Unsafe owned import cleanup path') + await rm(target, { recursive: true, force: true }) + console.log('Owned importer PostgreSQL stopped and exact temp directory removed') +} +process.exit(process.exitCode ?? 0) diff --git a/backend/scripts/vrf-production-profile.mjs b/backend/scripts/vrf-production-profile.mjs new file mode 100644 index 00000000..cd6d74da --- /dev/null +++ b/backend/scripts/vrf-production-profile.mjs @@ -0,0 +1,152 @@ +import assert from 'node:assert/strict' +import { mkdir, writeFile, realpath, lstat, mkdtemp, open } from 'node:fs/promises' +import { basename, join, resolve, relative, isAbsolute } from 'node:path' +import { createRequire } from 'node:module' +import { pathToFileURL, fileURLToPath } from 'node:url' +import { randomBytes } from 'node:crypto' +import { spawn } from 'node:child_process' + + +const diagnosticLogCap = 1024 * 1024 +const diagnosticNames = new Set(['Error', 'AssertionError', 'McpError', 'TypeError', 'RangeError']) +const diagnosticCodes = new Set(['ERR_ASSERTION', 'VERIFICATION_FAILED', 'DEVICE_UNAVAILABLE', 'PLAN_EXPIRED', 'RATE_LIMITED', 'VRF_ACK_JOURNAL_UNCONFIRMED', 'VRF_VERIFIED_CHECKPOINT_UNCONFIRMED', '42883', '22P02']) +/** Pure projection only: no arbitrary log text, SQL, native rows or credentials. */ +export function projectVrfDiagnostics(log, privateValues = [], expectedSlice = null) { + assert.equal(typeof log, 'string'); assert(Array.isArray(privateValues)) + assert(Buffer.byteLength(log) <= diagnosticLogCap, 'Diagnostic source log exceeded existing cap') + let redacted = log + for (const value of [...privateValues, 'owned-vrf-loopback-password'].filter(value => typeof value === 'string' && value).sort((a,b) => b.length-a.length)) redacted = redacted.split(value).join('[REDACTED]') + redacted = redacted.replace(/postgres(?:ql)?:\/\/[^\s'"]+/gi, '[REDACTED_DATABASE_URL]') + .replace(/mmcp_[a-z]+_[A-Za-z0-9_-]+/g, '[REDACTED_MCP_TOKEN]') + .replace(/\beyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\b/g, '[REDACTED_JWT]') + const records = [], acceptedCaseSequence = []; let malformedLines = 0, omittedLines = 0, incrementalRecords = 0, truncatedRecords = 0, droppedCaseLabels = 0, lastAcceptedCase = null, finalComponentPresent = false + const labelPattern = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,159}$/ + const retainLabel = label => { if (typeof label !== 'string' || !labelPattern.test(label)) { droppedCaseLabels++; return }; if (acceptedCaseSequence.length === 128) { acceptedCaseSequence.shift(); droppedCaseLabels++ }; acceptedCaseSequence.push(label); lastAcceptedCase = label } + const retain = value => { if (records.length === 128) { records.shift(); truncatedRecords++ } records.push(value) } + for (const line of redacted.split(/\r?\n/).filter(Boolean)) { + let value; try { value = JSON.parse(line) } catch { + malformedLines++ + const name = /^(Error|AssertionError|McpError|TypeError|RangeError)(?:[ :\[]|$)/.exec(line)?.[1] + if (name) retain({ diagnosticError: true, safeName: name, safeCode: line.includes('[ERR_ASSERTION]') ? 'ERR_ASSERTION' : 'other' }) + const normalized = line.replaceAll('\\', '/'), position = /vrf-production-execution-owned\.(?:ts|mts):(\d{1,7}):(\d{1,7})\)?$/.exec(normalized) + if (position && (normalized.includes('/backend/src/test/vrf-production-execution-owned.ts:') || normalized.includes('/tmp/mcp-vrf-all68-staged-20261101/vrf-production-execution-owned.mts:'))) retain({ source: 'owned-vrf-fixture', line: Number(position[1]), column: Number(position[2]) }) + continue + } + if (!value || typeof value !== 'object' || Array.isArray(value)) { omittedLines++; continue } + const row = {} + if (value.acceptedCase) { incrementalRecords++; row.acceptedCaseRecord = true; const labels = Array.isArray(value.acceptedCase) ? value.acceptedCase : [value.acceptedCase]; if (labels.length <= 68) for (const label of labels) retainLabel(label); else droppedCaseLabels += labels.length } + if (Array.isArray(value.accepted)) { row.finalCaseCount = value.accepted.length; if (expectedSlice !== null && value.slice === expectedSlice) finalComponentPresent = true } + if (['planned','queued','running','applied','failed','cancelled','unknown','expired','rejected','unconfirmed'].includes(value.operationState)) row.operationState = value.operationState + for (const key of ['phase', 'elapsedMs', 'nativeReads', 'nativeWrites', 'roundTrips', 'lastSqlMs', 'totalSqlMs', 'maxSqlMs']) if (Number.isSafeInteger(value[key]) && value[key] >= 0) row[key] = value[key] + if (Number.isSafeInteger(value.wireguardPeerTelemetryTicks) && value.wireguardPeerTelemetryTicks > 0 && value.wireguardPeerTelemetryTicks <= 10000) row.wireguardPeerTelemetryTicks = value.wireguardPeerTelemetryTicks + if (value.diagnosticWaitSql === true) row.diagnosticWaitSql = true + if (value.diagnosticError === true) { row.diagnosticError = true; row.safeName = diagnosticNames.has(value.safeName) ? value.safeName : 'other'; row.safeCode = diagnosticCodes.has(value.safeCode) ? value.safeCode : 'other' } + if (!Object.keys(row).length) { omittedLines++; continue } + retain(row) + } + return { version: 1, rawLogRetained: false, sourceCapBytes: diagnosticLogCap, capturedLogBytes: Buffer.byteLength(log), malformedLines, omittedLines, incrementalRecords, truncatedRecords, finalComponentPresent, acceptedCaseSequence, lastAcceptedCase, droppedCaseLabels, records } +} +export function vrfSliceDiagnostic(result, log, childBudgetMs, privateValues = []) { + assert(result && typeof result === 'object') + assert(['baseline','physical','vlan','disabled','mixed','reconcile','sql-journal','guards','coexistence','alias','gre'].includes(result.slice)) + assert(Number.isSafeInteger(result.code) && typeof result.childExitConfirmed === 'boolean') + assert(Number.isSafeInteger(result.caseCount) && result.caseCount >= 0) + assert.equal(childBudgetMs, result.slice === 'baseline' ? 240000 : result.slice === 'disabled' ? 180000 : 120000) + assert(result.failure === null || ['child-deadline','child-log-limit','child-signal','child-error','child-error-after-pid','child-exit-unconfirmed','missing-component-receipt'].includes(result.failure)) + assert(result.accepted === (result.code === 0 && result.failure === null && result.childExitConfirmed)) + return { ...result, capMs: childBudgetMs, diagnostics: projectVrfDiagnostics(log, privateValues, result.slice), scope: 'same-owned-PG-test-only', actualRouter: false, forwardingVerified: false } +} +async function writeDiagnostic(path, value, maxBytes = 65536) { + const bytes = Buffer.from(JSON.stringify(value, null, 2) + '\n'); assert(bytes.length <= maxBytes) + const stream = await open(path, 'wx', 0o600) + try { await stream.writeFile(bytes); await stream.sync() } finally { await stream.close() } +} + +/** Existing test runner owns the PG lifecycle. This component creates only + * bounded Node children on that same synthetic DB, with an empty private cwd. + * It never reads backend/.env, creates a PG, or accepts an application URL. */ +export async function runOwnedVrfProductionProfile({ directory, database, databaseUrl }) { + const target = new URL(databaseUrl) + assert.equal(target.hostname, '127.0.0.1'); assert.equal(target.username, 'mcp_test') + assert(/^mm_accept_166_\d+$/.test(database)); assert.equal(target.pathname, '/' + database) + const owned = resolve(directory) + assert.equal(await realpath(owned), owned); assert(basename(owned).startsWith('mm-mcp-test-')) + assert(!(await lstat(owned)).isSymbolicLink()) + // Fixed destination derived from this source module, never caller/env controlled. + const repository = fileURLToPath(new URL('../../', import.meta.url)), reportRoot = join(repository, 'tmp') + assert.equal(await realpath(reportRoot), reportRoot); assert(!(await lstat(reportRoot)).isSymbolicLink()) + const diagnosticDirectory = await mkdtemp(join(reportRoot, 'mcp-vrf-profile-')) + assert.equal(await realpath(diagnosticDirectory), diagnosticDirectory); assert(!(await lstat(diagnosticDirectory)).isSymbolicLink()) + assert.equal(relative(reportRoot, diagnosticDirectory), basename(diagnosticDirectory)); assert(basename(diagnosticDirectory).startsWith('mcp-vrf-profile-')) + const persist = (name, value) => writeDiagnostic(join(diagnosticDirectory, name), value) + const diagnosticPath = relative(repository, diagnosticDirectory) + await persist('started.json', { version: 1, status: 'running', accepted: false, childExitConfirmed: false, scope: 'same-owned-PG-test-only' }) + console.log(JSON.stringify({ profile: 'test:vrf:production', diagnosticDirectory: diagnosticPath, status: 'running' })) + const results = [] + let currentSlice = null, currentChildExitConfirmed = false, lastSliceResult = null + try { + const entry = fileURLToPath(new URL('../src/test/vrf-production-execution-owned.ts', import.meta.url)) + const report = fileURLToPath(new URL('../../docs/mcp-chr-vrf-companion-native-v5.json', import.meta.url)) + const loader = pathToFileURL(createRequire(import.meta.url).resolve('tsx')).href + const safe = Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'LANG', 'LC_ALL'].filter(key => typeof process.env[key] === 'string').map(key => [key, process.env[key]])) + // Diagnose the disabled lifecycle first, then the synthetic GRE extension. + // Every original case remains required by the same full74 gate and caps. + for (const slice of ['disabled', 'gre', 'baseline', 'physical', 'vlan', 'mixed', 'reconcile', 'sql-journal', 'guards', 'coexistence', 'alias']) { + currentSlice = slice; currentChildExitConfirmed = false; lastSliceResult = null + await persist(slice + '-started.json', { version: 1, slice, status: 'running', accepted: false, childExitConfirmed: false }) + // Nine disabled lifecycle operations need their own aggregate child cap; + // measured seven operations alone consumed 105.5s under the original120s. + const childBudgetMs = slice === 'baseline' ? 240000 : slice === 'disabled' ? 180000 : 120000 + const cwd = join(owned, 'vrf-production-' + slice), rel = relative(owned, cwd) + assert(rel && !rel.startsWith('..') && !isAbsolute(rel)); await mkdir(cwd) + assert.equal(await realpath(cwd), cwd); assert(!(await lstat(cwd)).isSymbolicLink()) + let log = '', failure = null, exited = false, stopTimer, unknownTimer + const authSecret = randomBytes(32).toString('hex') + const child = spawn(process.execPath, ['--import', loader, entry], { cwd, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'], env: { + ...safe, NODE_OPTIONS: '', NODE_ENV: 'test', DATABASE_URL: databaseUrl, + DATABASE_PATH: join(cwd, 'no-legacy.sqlite'), MCP_STATE_DIR: join(cwd, 'state'), + AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: authSecret, MCP_PUBLIC_URL: 'http://localhost:8000/mcp', + MMAPP_OWN_VRF_EXECUTION: 'true', MMAPP_VRF_EXECUTION_SLICE: slice, MMAPP_VRF_NATIVE_REPORT: report, + MMAPP_VRF_SHARED_OWNED_PG: 'true', MMAPP_OWN_SQL_DB: database, + } }) + const stop = reason => { + failure ??= reason + if (exited) return + child.kill('SIGTERM') + stopTimer ??= setTimeout(() => { if (!exited) child.kill('SIGKILL') }, 5000) + } + const deadline = setTimeout(() => stop('child-deadline'), childBudgetMs) + for (const stream of [child.stdout, child.stderr]) stream.on('data', bytes => { + if (Buffer.byteLength(log) + bytes.length > 1024 * 1024) { stop('child-log-limit'); return } + log += bytes.toString() + }) + const code = await new Promise(resolveExit => { + child.once('exit', (status, signal) => { exited = true; if (signal) failure ??= 'child-signal'; resolveExit(status ?? 1) }) + child.once('error', () => { failure ??= 'child-error'; if (!child.pid) { exited = true; resolveExit(1) } else stop('child-error-after-pid') }) + unknownTimer = setTimeout(() => { if (!exited) { stop('child-exit-unconfirmed'); resolveExit(1) } }, childBudgetMs + 15000) + }) + clearTimeout(deadline); clearTimeout(stopTimer); clearTimeout(unknownTimer) + const expectedCases = { baseline: 11, physical: 7, vlan: 9, disabled: 9, mixed: 7, reconcile: 6, 'sql-journal': 1, guards: 11, coexistence: 3, alias: 4, gre: 6 } + const component = log.trim().split(/\r?\n/).flatMap(line => { try { return [JSON.parse(line)] } catch { return [] } }).findLast(value => value.slice === slice && Array.isArray(value.accepted)) + if (!component || component.accepted.length !== expectedCases[slice] || component.actualSql !== true || component.actualCrypto !== true || component.actualAuthPolicyApprovalRuntime !== true || component.fakeVerifier !== false || component.actualRouter !== false || component.forwardingVerified !== false) failure ??= 'missing-component-receipt' + if (slice === 'gre' && (!Number.isSafeInteger(component?.wireguardPeerTelemetryTicks) || component.wireguardPeerTelemetryTicks <= 6 || component.wireguardPeerTelemetryTicks > 10000)) failure ??= 'missing-component-receipt' + const result = { slice, accepted: code === 0 && !failure && exited, caseCount: component?.accepted?.length ?? 0, code, failure, childPid: child.pid ?? null, childExitConfirmed: exited } + lastSliceResult = result; currentChildExitConfirmed = exited + const diagnostic = vrfSliceDiagnostic(result, log, childBudgetMs, [databaseUrl, target.password, decodeURIComponent(target.password), authSecret]) + await persist(slice + '-result.json', diagnostic) + // Persist outside the owner's cleanup boundary BEFORE any next slice/return. + await writeFile(join(cwd, 'fixture.log'), JSON.stringify(diagnostic.diagnostics, null, 2)) + await writeFile(join(cwd, 'acceptance.json'), JSON.stringify(result, null, 2)) + results.push(result) + console.log(JSON.stringify({ profile: 'test:vrf:production', ...result })) + if (!result.accepted) break + } + const receipt = { accepted: results.length === 11 && results.every(result => result.accepted), childExitConfirmed: results.every(result => result.childExitConfirmed), sameOwnedPg: true, actualRouter: false, forwardingVerified: false, results } + await persist('terminal.json', { version: 1, status: 'terminal', ...receipt, requiredCases: 74, completedCases: results.reduce((sum, value) => sum + value.caseCount, 0), diagnosticDirectory: diagnosticPath }) + await writeFile(join(owned, 'vrf-production-acceptance.json'), JSON.stringify(receipt, null, 2)) + return receipt + } catch { + await persist('wrapper-failure.json', { version: 1, status: 'refused', accepted: false, phase: currentSlice, failure: 'wrapper-exception', childExitConfirmed: currentChildExitConfirmed && results.every(value => value.childExitConfirmed), completedSlices: results.length, lastSliceResult, results }) + throw Error('VRF profile refused; bounded diagnostics retained at ' + diagnosticPath) + } +} diff --git a/backend/scripts/vrf-production-profile.test.mjs b/backend/scripts/vrf-production-profile.test.mjs new file mode 100644 index 00000000..cfcd4bc4 --- /dev/null +++ b/backend/scripts/vrf-production-profile.test.mjs @@ -0,0 +1,45 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { vrfSliceDiagnostic } from './vrf-production-profile.mjs' + +test('only the nine-operation disabled slice receives its measured aggregate budget', () => { + for (const [slice, capMs] of [['disabled', 180000], ['gre', 120000], ['baseline', 240000]]) { + const result = { slice, accepted: false, caseCount: 0, code: 1, failure: 'child-deadline', childExitConfirmed: true } + assert.equal(vrfSliceDiagnostic(result, '', capMs).capMs, capMs) + for (const otherCap of [120000, 180000, 240000].filter(value => value !== capMs)) assert.throws(() => vrfSliceDiagnostic(result, '', otherCap)) + } +}) + +test('GRE lifecycle diagnostic retains the six-case receipt with existing privacy bounds', () => { + const accepted = ['vrf-create', 'vrf-rename', 'vrf-disable', 'vrf-reenable', 'vrf-membership-transfer-a-to-b', 'vrf-delete-addresses-remain'] + const result = { slice: 'gre', accepted: true, caseCount: 6, code: 0, failure: null, childExitConfirmed: true } + const log = accepted.map((acceptedCase, index) => JSON.stringify({ slice: 'gre', acceptedCase, nativeWrites: 1, wireguardPeerTelemetryTicks: (index + 1) * 4, password: 'fixture-private-value' })) + log.push(JSON.stringify({ slice: 'gre', accepted, wireguardPeerTelemetryTicks: 24, rawShape: 'synthetic GRE lifecycle', password: 'fixture-private-value' })) + const diagnostic = vrfSliceDiagnostic(result, log.join('\n'), 120000, ['fixture-private-value']) + assert.deepEqual(diagnostic.diagnostics.acceptedCaseSequence, accepted) + assert.equal(diagnostic.diagnostics.finalComponentPresent, true) + assert.equal(diagnostic.diagnostics.rawLogRetained, false) + assert.deepEqual(diagnostic.diagnostics.records.map(row => row.wireguardPeerTelemetryTicks), [4, 8, 12, 16, 20, 24, 24]) + assert.equal(diagnostic.actualRouter, false) + assert.equal(diagnostic.forwardingVerified, false) + assert.equal(JSON.stringify(diagnostic).includes('fixture-private-value'), false) + assert.throws(() => vrfSliceDiagnostic(result, '', 240000)) +}) + +test('peer telemetry diagnostic rejects nonnumeric and out-of-budget values', () => { + const result = { slice: 'gre', accepted: false, caseCount: 0, code: 1, failure: 'missing-component-receipt', childExitConfirmed: true } + for (const wireguardPeerTelemetryTicks of [0, -1, 10001, 1.5, 'private-raw-value', { secret: 'private-raw-value' }]) { + const diagnostic = vrfSliceDiagnostic(result, JSON.stringify({ slice: 'gre', wireguardPeerTelemetryTicks }), 120000) + assert.equal(diagnostic.diagnostics.records.some(row => Object.hasOwn(row, 'wireguardPeerTelemetryTicks')), false) + assert.equal(JSON.stringify(diagnostic).includes('private-raw-value'), false) + } +}) + +test('a partial GRE lifecycle is diagnostic evidence without a final component receipt', () => { + const result = { slice: 'gre', accepted: false, caseCount: 0, code: 1, failure: 'child-deadline', childExitConfirmed: true } + const diagnostic = vrfSliceDiagnostic(result, JSON.stringify({ slice: 'gre', acceptedCase: 'vrf-create' }), 120000) + assert.equal(diagnostic.accepted, false) + assert.equal(diagnostic.diagnostics.finalComponentPresent, false) + assert.equal(diagnostic.diagnostics.lastAcceptedCase, 'vrf-create') + assert.equal(diagnostic.capMs, 120000) +}) diff --git a/backend/scripts/writer-profile/application-writer-profile-canonical.ts b/backend/scripts/writer-profile/application-writer-profile-canonical.ts new file mode 100644 index 00000000..7f9716b0 --- /dev/null +++ b/backend/scripts/writer-profile/application-writer-profile-canonical.ts @@ -0,0 +1,9 @@ +import {createHash} from 'node:crypto' + +/** Exact writer-profile v1 canonical bytes. Pure value codec; no trust/authority. */ +export function canonical(value:unknown):string { + return JSON.stringify(value,(_key,item:unknown)=>item&&typeof item==='object'&&!Array.isArray(item) + ?Object.fromEntries(Object.entries(item).sort(([a],[b])=>ab?1:0)):item) +} +export const sha=(value:Uint8Array|string)=>createHash('sha256').update(value).digest('hex') +export const bytes=(value:unknown)=>Buffer.from(canonical(value)+'\n','utf8') diff --git a/backend/scripts/writer-profile/application-writer-profile-catalogue-parent-policy.ts b/backend/scripts/writer-profile/application-writer-profile-catalogue-parent-policy.ts new file mode 100644 index 00000000..35e14240 --- /dev/null +++ b/backend/scripts/writer-profile/application-writer-profile-catalogue-parent-policy.ts @@ -0,0 +1,9 @@ +// Fixed12 source-partition DATA policy, candidate build artifact for review. +// No observed-schema enrollment, caller policy, expected catalogue or authority. +import {canonical,sha} from './application-writer-profile-canonical.js' +const PERIODS=Object.freeze({flow_buckets:'d',flow_minute_stats:'d',flow_minute_dims:'d',flow_daily_dims:'m',flow_hour_facts:'d',flow_daily_facts:'m',traffic_samples:'w',servers_rest_ping_samples:'w',uptime_probe_samples:'w',uptime_resource_samples:'w',server_snapshots:'w',internet_path_snapshots:'w'} as const) +export type FixedCatalogueParentPeriodData='d'|'w'|'m' +export const FIXED_CATALOGUE_PARENT_POLICY_DATA_SHA256=sha(canonical(PERIODS)) +export function fixedCatalogueParentPeriodData(name:string):FixedCatalogueParentPeriodData|undefined { + return Object.hasOwn(PERIODS,name)?PERIODS[name as keyof typeof PERIODS]:undefined +} diff --git a/backend/scripts/writer-profile/application-writer-profile-catalogue-values.ts b/backend/scripts/writer-profile/application-writer-profile-catalogue-values.ts new file mode 100644 index 00000000..2d28a0c1 --- /dev/null +++ b/backend/scripts/writer-profile/application-writer-profile-catalogue-values.ts @@ -0,0 +1,90 @@ +// Private typed VALUE framing only. No SQL/pool/role/current-schema attestation. +// Transport limits are inherited unchanged from the accepted paged DATA codec; +// they DO NOT expand the legacy2048 total-constraint policy. +import {createHash,type Hash} from 'node:crypto' +import {canonical as writerCanonical} from './application-writer-profile-canonical.js' + +export type Json = null|boolean|number|string|Json[]|{[key:string]:Json} +export type CatalogueKind='global'|'base'|'leaf' +export interface CatalogueRecord {kind:CatalogueKind;identity:string;value:{[key:string]:Json}} +export const FRAME_LIMITS=Object.freeze({chunkBytes:4096,pageRows:128,recordBytes:2*1024*1024,streamBytes:64*1024*1024,identityBytes:1024,depth:64}) +export interface StreamMetrics {records:number;chunks:number;pages:number;payloadBytes:number;estimatedDataRowBytes:number;maxPagePayloadBytes:number;maxRecordBytes:number} +export interface CatalogueValueDigest {format:'catalogue-values-v2';baseSha256:string;leafSha256:string;snapshotSha256:string;metrics:Readonly;authority:false} +const rank:Record={global:0,base:1,leaf:2} +const domain=(name:string)=>createHash('sha256').update('mmapp:catalogue-'+name+':v2\0') +function object(value:unknown):value is Record{return !!value&&typeof value==='object'&&!Array.isArray(value)&&Object.getPrototypeOf(value)===Object.prototype} +function exactJson(value:Json,depth=0):void { + if(depth>FRAME_LIMITS.depth)throw Error('Catalogue nesting bound') + if(value===null||typeof value==='boolean'||typeof value==='string')return + if(typeof value==='number'){if(!Number.isSafeInteger(value)||Object.is(value,-0))throw Error('Exact integer/string catalogue scalar required');return} + if(Array.isArray(value)){for(const item of value)exactJson(item,depth+1);return} + if(!object(value))throw Error('Plain catalogue JSON required') + for(const key of Object.keys(value))exactJson(value[key] as Json,depth+1) +} +function canonical(value:Json):string{exactJson(value);return writerCanonical(value)} +function update(hash:Hash,encoded:Buffer){const length=Buffer.alloc(4);length.writeUInt32BE(encoded.length);hash.update(length);hash.update(encoded)} +function record(bytes:Buffer,kind:CatalogueKind,identity:string):CatalogueRecord{ + const parsed:unknown=JSON.parse(new TextDecoder('utf8',{fatal:true}).decode(bytes)) + if(!object(parsed)||Object.keys(parsed).sort().join(',')!=='identity,kind,value'||parsed.kind!==kind||parsed.identity!==identity||!object(parsed.value))throw Error('Exact catalogue record shape required') + // canonical() also rejects numbers/prototypes outside the scalar contract. + canonical(parsed as unknown as Json) + return parsed as unknown as CatalogueRecord +} +export class CatalogueValueStream { + private readonly base=domain('base-values');private readonly leaf=domain('leaf-values');private readonly snapshot=domain('snapshot-values') + private readonly metrics:StreamMetrics={records:0,chunks:0,pages:0,payloadBytes:0,estimatedDataRowBytes:0,maxPagePayloadBytes:0,maxRecordBytes:0} + private next=1n;private parts:Buffer[]=[];private bytes=0;private expectedParts=0;private currentKind?:CatalogueKind;private currentIdentity='' + private previous?:{kind:CatalogueKind;identity:string};private finished=false;private accepting=false;private failed=false;private baseRecords=0;private leafRecords=0; + *acceptPage(rows:readonly unknown[]):Generator{ + if(this.finished||this.accepting||this.failed||!Array.isArray(rows)||rows.length>FRAME_LIMITS.pageRows){this.failed=true;throw Error('Closed/oversized catalogue page')} + this.accepting=true;let complete=false + try{ + this.metrics.pages++;let pageBytes=0 + for(const raw of rows){ + if(!object(raw)||Object.keys(raw).sort().join(',')!=='chunk_count,chunk_index,identity,kind,payload,record_id')throw Error('Exact cursor row shape required') + const {record_id:id,chunk_index:part,chunk_count:count,kind,identity,payload}=raw + if(typeof id!=='string'||!/^[1-9][0-9]{0,18}$/.test(id)||BigInt(id)!==this.next||typeof kind!=='string'||!Object.hasOwn(rank,kind)||typeof identity!=='string'||Buffer.byteLength(identity)<1||Buffer.byteLength(identity)>FRAME_LIMITS.identityBytes||!Number.isInteger(part)||!Number.isInteger(count)||typeof part!=='number'||typeof count!=='number'||count<1||count>FRAME_LIMITS.recordBytes/FRAME_LIMITS.chunkBytes||part!==this.parts.length||part>=count||!Buffer.isBuffer(payload)||payload.length<1||payload.length>FRAME_LIMITS.chunkBytes||partFRAME_LIMITS.recordBytes||pageBytes>FRAME_LIMITS.pageRows*FRAME_LIMITS.chunkBytes||this.metrics.payloadBytes>FRAME_LIMITS.streamBytes)throw Error('Catalogue byte budget exceeded') + if(part===count-1){ + const value=record(Buffer.concat(this.parts,this.bytes),k,identity),encoded=Buffer.from(canonical(value as unknown as Json)) + if(encoded.length>FRAME_LIMITS.recordBytes)throw Error('Canonical catalogue record overflow') + const dynamic=k==='leaf'||k==='global'&&identity==='source_size' + update(this.snapshot,encoded);update(dynamic?this.leaf:this.base,encoded) + if(dynamic)this.leafRecords++;else this.baseRecords++ + this.metrics.records++;this.metrics.maxRecordBytes=Math.max(this.metrics.maxRecordBytes,this.bytes);this.previous={kind:k,identity};this.next++;this.parts=[];this.bytes=0;this.expectedParts=0 + yield value + } + } + this.metrics.maxPagePayloadBytes=Math.max(this.metrics.maxPagePayloadBytes,pageBytes) + complete=true + }finally{this.accepting=false;if(!complete)this.failed=true} + } + finish():CatalogueValueDigest{ + if(this.finished||this.accepting||this.failed||this.parts.length)throw Error('Catalogue stream incomplete/closed') + this.finished=true + // Each digest includes its own record count, so empty/partitioned streams + // cannot acquire an ambiguous concatenation encoding. + const digest=(hash:Hash,count:number)=>{const end=Buffer.alloc(8);end.writeBigUInt64BE(BigInt(count));return hash.update(end).digest('hex')} + return Object.freeze({format:'catalogue-values-v2',baseSha256:digest(this.base,this.baseRecords),leafSha256:digest(this.leaf,this.leafRecords),snapshotSha256:digest(this.snapshot,this.metrics.records),metrics:Object.freeze({...this.metrics}),authority:false}) + } +} + + + +/** Fail-closed DATA counter for the unchanged legacy total constraint ceiling. + * Paging is not permission to reset/reinterpret that total on every page. */ +export class FixedCatalogueConstraintRowBudgetData { + private count=0;private failed=false + acceptRows(rows:readonly unknown[]):number { + if(this.failed||!Array.isArray(rows)){this.failed=true;throw Error('Closed/invalid constraint row budget')} + if(this.count+rows.length>2048){this.failed=true;throw Error('Legacy total constraint row cap2048 exceeded')} + this.count+=rows.length;return this.count + } +} diff --git a/backend/scripts/writer-profile/application-writer-profile-catalogue.ts b/backend/scripts/writer-profile/application-writer-profile-catalogue.ts new file mode 100644 index 00000000..853ee0c4 --- /dev/null +++ b/backend/scripts/writer-profile/application-writer-profile-catalogue.ts @@ -0,0 +1,104 @@ +// Private staged full-catalogue DATA verifier. No pool, issuer, cache, authority or READY. +// Exact reviewed source templates are statically imported. +import {canonical,sha} from './application-writer-profile-canonical.js' +import {fixedCatalogueParentPeriodData} from './application-writer-profile-catalogue-parent-policy.js' +import {type CatalogueRecord} from './application-writer-profile-catalogue-values.js' +import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './fixed44-build-definition.js' +import {BUILD56_PROVENANCE,FIXED56_ROLE_TEMPLATE} from './fixed56-build-definition.js' +import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js' +import {BUILD58_PROVENANCE,FIXED58_ROLE_TEMPLATE,normalizeFixed58RestoreRows} from './fixed58-build-definition.js' +import {GLOBALS,GROUPS,object,rows,sorted,text,projectCatalogueData,type CatalogueEdition,type Inventory,type Row,type Json} from './catalogue-projection.js' +import {validateCatalogueLeafData} from './catalogue-leaves.js' +import {catalogueTopologyPolicy,Expanded44TopologyBudgetData} from './expanded44-data-policy.js' +export type LogicalRole='runtime'|'operator'|'capture'|'publisher'|'control' +// Same DATA shape as RegistryRoleBindingsData. Provenance must come from protected host separately. +export type InstallationRoleBindingsData=Readonly>> +const ROLE_KEYS:readonly LogicalRole[]=['runtime','operator','capture','publisher','control'] +const EXTRAS=['source_size','namespace_policy','publisher_column_escape'] as const +const definitions = { + 44: { provenance: BUILD44_PROVENANCE, template: FIXED44_ROLE_TEMPLATE }, + 56: { provenance: BUILD56_PROVENANCE, template: FIXED56_ROLE_TEMPLATE }, + 57: { provenance: BUILD57_PROVENANCE, template: FIXED57_ROLE_TEMPLATE }, + 58: { provenance: BUILD58_PROVENANCE, template: FIXED58_ROLE_TEMPLATE }, +} +const exact=(a:unknown,b:unknown,label:string)=>{if(canonical(a)!==canonical(b))throw Error('Writer catalogue refused: '+label)} +function captureBindings(input:InstallationRoleBindingsData):Readonly> { + exact(Object.keys(input).sort(),[...ROLE_KEYS].sort(),'binding roles') + const names=new Set(),oids=new Set(),result={} as Record + for(const role of ROLE_KEYS){const binding=input[role];exact(Object.keys(binding).sort(),['name','signedOid'],'binding fields') + // Deliberately narrow deparse envelope; unsupported quoted/reserved names fail closed. + if(!/^[a-z_][a-z0-9_]{0,62}$/.test(binding.name)||!/^[1-9][0-9]{0,9}$/.test(binding.signedOid)||BigInt(binding.signedOid)>4294967295n||names.has(binding.name)||oids.has(binding.signedOid))throw Error('Explicit distinct installation role binding required') + names.add(binding.name);oids.add(binding.signedOid);result[role]=binding.name + } + return Object.freeze(result) +} +function instantiateExpected(names:Readonly>, template:Readonly):Inventory { + const walk=(value:Json,key=''):Json=>{if(typeof value==='string')return value.replace(/@mm-role:(runtime|operator|capture|publisher|control)@/g,(_match,role:LogicalRole)=>names[role]);if(Array.isArray(value)){const result=value.map(v=>walk(v));return key==='acl'?(result as string[]).sort():result}if(value&&typeof value==='object')return Object.fromEntries(Object.entries(value).map(([k,v])=>[k,walk(v,k)]));return value} + const data=walk(template as unknown as Json) as unknown as Inventory + return Object.fromEntries(Object.entries(data).map(([key,data])=>[key,sorted(data)])) +} +export class Fixed44CatalogueDataVerifier { + private readonly names:Readonly>;private readonly expected:Inventory;private readonly bindingsDigest:string + private readonly globals:Inventory={};private headers:Row[]|undefined;private globalDone=false;private closed=false;private failed=false + private readonly bases=new Set();private readonly leaves=new Set();private readonly templates=new Map() + private readonly topology:Expanded44TopologyBudgetData;private counts?:Row + private readonly policy:ReturnType + constructor(bindings:InstallationRoleBindingsData, private readonly edition:CatalogueEdition=44){ + this.names=captureBindings(bindings) + this.expected=instantiateExpected(this.names,definitions[edition].template) + this.bindingsDigest=sha(canonical(bindings)) + this.policy=catalogueTopologyPolicy(edition) + this.topology=new Expanded44TopologyBudgetData(edition) + } + consume(record:CatalogueRecord):void {if(this.closed||this.failed)throw Error('Closed/refused catalogue verifier');try{this.consumeOpen(record)}catch(error){this.failed=true;throw error}} + private consumeOpen(record:CatalogueRecord):void { + if(record.kind==='global'){ + if(this.globalDone)throw Error('Late global refused');exact(Object.keys(record.value),['rows'],'global shape');const data=rows(record.value.rows) + if(record.identity==='base_tables'){if(this.headers)throw Error('Duplicate base header');this.headers=data;return} + if(![...GLOBALS,...EXTRAS].includes(record.identity as typeof GLOBALS[number])||Object.hasOwn(this.globals,record.identity))throw Error('Unknown/duplicate global section') + this.globals[record.identity]=data;return + } + this.closeGlobals() + if(record.kind==='leaf'&&record.identity==='!inventory_counts'){if(this.counts||this.bases.size!==this.policy.baseRelations)throw Error('Early/duplicate counts');exact(Object.keys(record.value).sort(),['base','leaf'],'count shape');this.counts=record.value;return} + exact(Object.keys(record.value).sort(),['table',...GROUPS].sort(),'relation shape') + const table=object(record.value.table),groups:Inventory={} + if(table.relname!==record.identity||table.relpersistence!=='p')throw Error('Relation identity/persistence refused') + for(const name of GROUPS)groups[name]=rows(record.value[name]) + if(record.kind==='base'){ + if(this.counts||table.relispartition!==false||this.bases.has(record.identity))throw Error('Duplicate/late base') + const header=this.headers!.find(t=>t.relname===record.identity);if(!header)throw Error('Foreign base refused');exact(table,header,'header/table') + const actual=projectCatalogueData({...this.globals,...groups,tables:this.headers!}) + for(const name of GROUPS)exact(this.edition===58?normalizeFixed58RestoreRows(record.identity,name,actual[name]):actual[name], + this.expected[name].filter(r=>r.relname===record.identity),'base '+name+' '+record.identity) + this.topology.acceptRelation('base',record.identity,groups);this.bases.add(record.identity);if(fixedCatalogueParentPeriodData(record.identity))this.templates.set(record.identity,{table,groups});return + } + if(record.kind!=='leaf'||!this.counts||this.bases.size!==this.policy.baseRelations||this.leaves.has(record.identity))throw Error('Early/duplicate leaf') + const edges=groups.inheritance;if(edges.length!==1)throw Error('Exact one leaf edge required') + const parent=this.templates.get(text(edges[0].parent));if(!parent)throw Error('Foreign leaf parent refused') + const raw:Inventory={...this.globals,tables:[parent.table,table],inheritance:edges} + for(const name of GROUPS)if(name!=='inheritance')raw[name]=[...parent.groups[name],...groups[name]] + validateCatalogueLeafData(raw,parent.table,table);this.topology.acceptRelation('leaf',record.identity,groups,text(edges[0].parent));this.leaves.add(record.identity) + } + private closeGlobals():void { + if(this.globalDone)return + if(!this.headers)throw Error('Missing base header') + exact(Object.keys(this.globals).sort(),[...GLOBALS,...EXTRAS].sort(),'complete globals') + const blank=Object.fromEntries(GROUPS.map(name=>[name,[]])) as Inventory + const projected=projectCatalogueData({...this.globals,...blank,tables:this.headers}) + exact(projected.tables,this.expected.tables,'exact source'+this.edition+' base identities/owner/ACL') + for(const name of GLOBALS)exact(projected[name],this.expected[name],'global '+name) + const size=this.globals.source_size + if(size.length!==1)throw Error('Source size shape refused') + exact(Object.keys(size[0]).sort(),['tables','function_bytes','default_bytes','check_bytes','trigger_bytes'].sort(),'source size fields') + if(typeof size[0].tables!=='number'||!Number.isSafeInteger(size[0].tables)||size[0].tables<1||size[0].tables>this.policy.maxRelations)throw Error('Expanded'+this.edition+' total relation DATA cap exceeded') + if(['function_bytes','default_bytes','check_bytes','trigger_bytes'].some(k=>typeof size[0][k]!=='string'||!/^[0-9]+$/.test(text(size[0][k]))||BigInt(text(size[0][k]))>2097152n))throw Error('Source byte bound refused') + if(this.globals.namespace_policy.length!==1)throw Error('Namespace shape refused') + exact(Object.keys(this.globals.namespace_policy[0]).sort(),['relations','rules','routines','types','namespaces','default_acl'].sort(),'namespace fields') + if(Object.values(this.globals.namespace_policy[0]).some(v=>v!==false)||this.globals.publisher_column_escape.length)throw Error('Namespace/column escape refused') + this.globalDone=true + } + finish(){if(this.closed||this.failed)throw Error('Closed/refused catalogue verifier');try{this.closeGlobals();const base=this.policy.baseRelations;if(this.bases.size!==base||this.templates.size!==this.policy.parents||!this.counts||this.counts.base!==base||this.counts.leaf!==this.leaves.size||this.globals.source_size[0].tables!==base+this.leaves.size)throw Error('Incomplete catalogue/counts');this.closed=true + const provenance=definitions[this.edition].provenance + return Object.freeze({format:`fixed${this.edition}-catalogue-expanded-data-v1` as const,policyVersion:this.policy.version,topology:this.topology.finish(),admissionClassificationVerified:false,definitionSha256:provenance.templateSha256,manifestSha256:provenance.manifestSha256,baseRelations:this.bases.size,leafRelations:this.leaves.size,bindingsDataSha256:this.bindingsDigest,cleanFourRoleSqlVerified:false,productionInstallationPolicyApproved:false,authority:false,readyAuthorized:false,defaultSupportAuthorized:false}) + }catch(error){this.failed=true;throw error}} +} diff --git a/backend/scripts/writer-profile/catalogue-leaves.ts b/backend/scripts/writer-profile/catalogue-leaves.ts new file mode 100644 index 00000000..211bd64d --- /dev/null +++ b/backend/scripts/writer-profile/catalogue-leaves.ts @@ -0,0 +1,39 @@ +// Typed port of the accepted twelve-parent leaf checks, with duplicate detection in stream owner. +import {canonical} from './application-writer-profile-canonical.js' +import {fixedCatalogueParentPeriodData} from './application-writer-profile-catalogue-parent-policy.js' +import {omit,sorted,text,type Inventory,type Row} from './catalogue-projection.js' +const guards=['candidate_application_statement_guard','candidate_truncate_preadmitted','server_partition_writer_guard'] +const exact=(a:unknown,b:unknown,label:string)=>{if(canonical(a)!==canonical(b))throw Error('Catalogue leaf refused: '+label)} +export function validateCatalogueLeafData(raw:Inventory,parent:Row,leaf:Row):void { + const parentName=text(parent.relname),name=text(leaf.relname),period=fixedCatalogueParentPeriodData(parentName) + const edges=raw.inheritance + if(edges.length!==1||edges[0].parent!==parentName||edges[0].child!==name)throw Error('Exact direct leaf parent required') + const suffix=name.slice(parentName.length+1) + if(!period||!name.startsWith(parentName+'_')||!new RegExp('^'+period+'_[0-9]{'+(period==='m'?6:8)+'}$').test(suffix)||leaf.relkind!=='r'||leaf.relpersistence!=='p'||leaf.relispartition!==true||leaf.owner!==parent.owner||leaf.namespace!=='public'||leaf.relrowsecurity!==false||leaf.relforcerowsecurity!==false||leaf.partition_key!==null)throw Error('Fixed managed leaf identity/owner refused') + const stamp=suffix.slice(2),iso=stamp.slice(0,4)+'-'+stamp.slice(4,6)+'-'+(period==='m'?'01':stamp.slice(6,8)) + const start=new Date(iso+'T00:00:00.000Z'),end=new Date(start) + if(!Number.isFinite(+start)||start.toISOString().slice(0,10)!==iso||period==='w'&&start.getUTCDay()!==1)throw Error('Leaf calendar refused') + if(period==='m')end.setUTCMonth(end.getUTCMonth()+1);else end.setUTCDate(end.getUTCDate()+(period==='w'?7:1)) + if(start.getUTCFullYear()<100||end.getUTCFullYear()>9998)throw Error('Bounded leaf calendar refused') + const bounds=/^FOR VALUES FROM \('([^']+)'\) TO \('([^']+)'\)$/.exec(text(leaf.partition_bound)) + const instant=(s:string)=>Date.parse(s.length===10?s+'T00:00:00.000Z':s) + if(!bounds||instant(bounds[1])!==+start||instant(bounds[2])!==+end)throw Error('Leaf UTC bound refused') + const acl=(r:Row)=>r.acl===null?null:(r.acl as string[]).slice().sort() + exact(acl(leaf),acl(parent),'ACL') + exact(leaf.reloptions,['flow_hour_facts','flow_daily_facts'].includes(parentName)?['fillfactor=70','autovacuum_vacuum_scale_factor=0.05','autovacuum_vacuum_cost_limit=2000']:null,'storage') + exact(raw.columns.filter(r=>r.relname===name).map(r=>({...r,relname:parentName})),raw.columns.filter(r=>r.relname===parentName),'columns/defaults/identity') + const functions=new Map(raw.functions.map(f=>[text(f.oid),text(f.namespace)+'.'+text(f.proname)+'('+text(f.arguments)+')'])) + const user=raw.triggers.filter(t=>t.relname===name&&!t.tgisinternal),source=raw.triggers.filter(t=>t.relname===parentName&&!t.tgisinternal) + if(user.length!==3||source.length!==3||new Set(user.map(t=>t.tgname)).size!==3||user.some(t=>!guards.includes(text(t.tgname))))throw Error('Leaf guard set refused') + for(const t of user){const p=source.find(p=>p.tgname===t.tgname) + if(!p||t.tgtype!==p.tgtype||t.tgenabled!=='A'||functions.get(text(t.tgfoid))!==functions.get(text(p.tgfoid))||t.tgnargs!==0||t.arg_bytes!==0||t.unconditional!==true||t.tgdeferrable!==false||t.tginitdeferred!==false||t.tgoldtable!==null||t.tgnewtable!==null||t.tgconstraint!=='0'||(t.tgname==='server_partition_writer_guard'?t.tgparentid!==p.oid:t.tgparentid!=='0'))throw Error('Leaf trigger structure refused') + exact(text(t.definition).replaceAll(name,parentName),p.definition,'guard definition') + } + const inherited=raw.triggers.filter(t=>t.relname===name&&t.tgisinternal),parentInternal=raw.triggers.filter(t=>t.relname===parentName&&t.tgisinternal) + if(inherited.length!==parentInternal.length||new Set(inherited.map(t=>t.tgparentid)).size!==inherited.length||inherited.some(t=>!parentInternal.some(p=>t.tgparentid===p.oid&&t.tgfoid===p.tgfoid&&t.tgtype===p.tgtype&&t.tgenabled===p.tgenabled&&t.tgnargs===p.tgnargs&&t.arg_bytes===p.arg_bytes&&t.unconditional===p.unconditional&&t.tgdeferrable===p.tgdeferrable&&t.tginitdeferred===p.tginitdeferred&&t.tgoldtable===p.tgoldtable&&t.tgnewtable===p.tgnewtable)))throw Error('Inherited FK trigger identity/state refused') + const constraints=(relname:string)=>sorted(raw.constraints.filter(r=>r.relname===relname).map(r=>omit(r,['relname','conname']))) + exact(constraints(name),constraints(parentName),'constraints') + const indexes=(relname:string)=>sorted(raw.indexes.filter(r=>r.relname===relname).map(r=>{const m=/^CREATE (UNIQUE )?INDEX [^]+? ON (?:ONLY )?public\.[a-z_0-9]+ (USING [^]+)$/.exec(text(r.definition));if(!m)throw Error('Leaf index template refused');return {...omit(r,['relname','index_name','definition']),definition:m[2]}})) + exact(indexes(name),indexes(parentName),'indexes') + exact(raw.roleGrants.filter(r=>r.relname===name).map(r=>omit(r,['relname'])),raw.roleGrants.filter(r=>r.relname===parentName).map(r=>omit(r,['relname'])),'effective grants') +} diff --git a/backend/scripts/writer-profile/catalogue-projection.ts b/backend/scripts/writer-profile/catalogue-projection.ts new file mode 100644 index 00000000..5f5c3c5e --- /dev/null +++ b/backend/scripts/writer-profile/catalogue-projection.ts @@ -0,0 +1,35 @@ +// Private DATA projection. OIDs remain checked through structural readers separately. +import {canonical} from './application-writer-profile-canonical.js' +export type Json=null|boolean|number|string|Json[]|{[key:string]:Json} +export type Row=Record +export type Inventory=Record +export const GROUPS=['columns','triggers','constraints','indexes','roleGrants','inheritance'] as const +export type CatalogueEdition = 44 | 56 | 57 | 58 +export const GLOBALS=['database','effectiveDatabase','effectiveFunctions','effectiveSchemas','effectiveSequences','events','functions','identities','memberships','parameters','roles','schemas','settings','sequences'] as const +export const sorted=(rows:Row[])=>rows.slice().sort((a,b)=>canonical(a)canonical(b)?1:0) +export const omit=(row:Row,names:readonly string[]):Row=>Object.fromEntries(Object.entries(row).filter(([key])=>!names.includes(key))) +export function text(value:Json|undefined):string {if(typeof value!=='string')throw Error('Catalogue string required');return value} +export function object(value:Json|undefined):Row {if(!value||typeof value!=='object'||Array.isArray(value))throw Error('Catalogue object required');return value} +export function rows(value:Json|undefined):Row[] {if(!Array.isArray(value))throw Error('Catalogue array required');return value.map(object)} +const acl=(value:Json):Json=>value===null?null:rowsOfStrings(value).slice().sort() +function rowsOfStrings(value:Json):string[]{if(!Array.isArray(value)||value.some(v=>typeof v!=='string'))throw Error('Catalogue string array required');return value as string[]} +export function projectCatalogueData(raw:Inventory):Inventory { + const bases=new Set(raw.tables.filter(t=>t.relispartition===false).map(t=>text(t.relname))) + const names=new Map(raw.tables.map(t=>[text(t.oid),text(t.relname)])) + const functions=new Map(raw.functions.map(f=>[text(f.oid),text(f.namespace)+'.'+text(f.proname)+'('+text(f.arguments)+')'])) + const base=(data:Row[])=>data.filter(r=>bases.has(text(r.relname))) + const result:Inventory={ + tables:base(raw.tables).map(r=>({...omit(r,['oid','relpersistence']),acl:acl(r.acl)})), + columns:base(raw.columns),identities:raw.identities.map(r=>omit(r,['relation_oid'])), + sequences:raw.sequences.map(r=>{const owner=names.get(text(r.owner_relation_oid));if(!owner)throw Error('Sequence owner relation missing');return {...omit(r,['oid','owner_relation_oid']),acl:acl(r.acl),owner_relation:owner}}), + functions:raw.functions.map(r=>({...omit(r,['oid']),acl:acl(r.acl)})), + triggers:base(raw.triggers).map(t=>({...omit(t,['oid','tgfoid','tgparentid','tgconstraint']),tgname:t.tgisinternal?text(t.tgname).replace(/_[0-9]+$/,'_{oid}'):t.tgname,function:functions.get(text(t.tgfoid))??'pg_catalog#'+text(t.tgfoid),parent:t.tgparentid!=='0',constraint:t.tgconstraint!=='0',definition:t.tgisinternal?text(t.definition).replace(/(RI_ConstraintTrigger_[ac])_[0-9]+/g,'$1_{oid}'):t.definition})), + inheritance:raw.inheritance.filter(r=>bases.has(text(r.child))),constraints:base(raw.constraints),indexes:base(raw.indexes), + schemas:raw.schemas.map(r=>({...r,acl:acl(r.acl)})),database:raw.database.map(r=>({...r,datname:'current-database',acl:acl(r.acl)})), + effectiveSchemas:raw.effectiveSchemas,effectiveDatabase:raw.effectiveDatabase,memberships:raw.memberships, + effectiveFunctions:raw.effectiveFunctions.map(r=>omit(r,['oid'])),effectiveSequences:raw.effectiveSequences, + events:raw.events.map(r=>{const fn=functions.get(text(r.function_oid));if(!fn)throw Error('Event function missing');return {...omit(r,['function_oid']),function:fn}}), + parameters:raw.parameters,settings:raw.settings.map(r=>({...r,database:r.database==='all-databases'?r.database:'current-database'})),roles:raw.roles,roleGrants:base(raw.roleGrants), + } + return Object.fromEntries(Object.entries(result).map(([name,data])=>[name,sorted(data)])) +} diff --git a/backend/scripts/writer-profile/catalogue-value-bridge.ts b/backend/scripts/writer-profile/catalogue-value-bridge.ts new file mode 100644 index 00000000..c12ab27e --- /dev/null +++ b/backend/scripts/writer-profile/catalogue-value-bridge.ts @@ -0,0 +1,15 @@ +// Typed bounded VALUE -> fixed44 semantic bridge. Requires real EOF, never emits authority. +// SQL retained cursor/context/source-before-and-after remains the protected host's obligation. +import {CatalogueValueStream} from './application-writer-profile-catalogue-values.js' +import {Fixed44CatalogueDataVerifier,type InstallationRoleBindingsData} from './application-writer-profile-catalogue.js' +import type {CatalogueEdition} from './catalogue-projection.js' +export class Fixed44CatalogueValueBridgeData { + private readonly values=new CatalogueValueStream();private readonly schema:Fixed44CatalogueDataVerifier + private eof=false;private closed=false;private failed=false + constructor(bindings:InstallationRoleBindingsData,edition:CatalogueEdition=44){this.schema=new Fixed44CatalogueDataVerifier(bindings,edition)} + acceptPage(page:readonly unknown[]):void { + if(this.eof||this.closed||this.failed)throw Error('Closed/refused VALUE catalogue bridge') + try{for(const record of this.values.acceptPage(page))this.schema.consume(record);if(page.length===0)this.eof=true}catch(error){this.failed=true;throw error} + } + finish(){if(this.closed||this.failed||!this.eof)throw Error('Real cursor EOF required for VALUE catalogue bridge');try{const values=this.values.finish(),catalogue=this.schema.finish();this.closed=true;return Object.freeze({values,catalogue,authority:false,sourceGenerationAuthenticated:false,allWriterDrainAuthenticated:false,readyAuthorized:false})}catch(error){this.failed=true;throw error}} +} diff --git a/backend/scripts/writer-profile/catalogue.mts b/backend/scripts/writer-profile/catalogue.mts new file mode 100644 index 00000000..18405670 --- /dev/null +++ b/backend/scripts/writer-profile/catalogue.mts @@ -0,0 +1,57 @@ +import assert from 'node:assert/strict' +import {Fixed44CatalogueValueBridgeData} from './catalogue-value-bridge.js' +import {FIXED44_CATALOGUE_BUILD_SQL} from './fixed44-build-sql.js' +import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './fixed44-build-definition.js' +import {BUILD56_PROVENANCE,FIXED56_ROLE_TEMPLATE} from './fixed56-build-definition.js' +import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js' +import {BUILD58_PROVENANCE,FIXED58_ROLE_TEMPLATE} from './fixed58-build-definition.js' +import {assertRestoreProfileQuarantine} from '../../src/db/database-restore-quarantine-profile.js' +import {canonical,sha} from './application-writer-profile-canonical.js' +import type {CatalogueEdition} from './catalogue-projection.js' +export function approvedDefinition(value:unknown){ + assert(value&&typeof value==='object');const v=value as Record + assert.deepEqual(Object.keys(v).sort(),['format','provenance','template']) + if(v.format==='application-writer-source44-definition-approval-v1'){ + assert.equal(canonical(v.provenance),canonical(BUILD44_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED44_ROLE_TEMPLATE)) + return Object.freeze({definitionSha256:BUILD44_PROVENANCE.templateSha256,manifestSha256:BUILD44_PROVENANCE.manifestSha256}) + } + if(v.format==='application-writer-source57-definition-approval-v1'){ + assert.equal(canonical(v.provenance),canonical(BUILD57_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED57_ROLE_TEMPLATE)) + return Object.freeze({edition:57 as const,definitionSha256:BUILD57_PROVENANCE.templateSha256,manifestSha256:BUILD57_PROVENANCE.manifestSha256}) + } + if(v.format==='application-writer-source58-definition-approval-v1'){ + assert.equal(canonical(v.provenance),canonical(BUILD58_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED58_ROLE_TEMPLATE)) + return Object.freeze({edition:58 as const,definitionSha256:BUILD58_PROVENANCE.templateSha256,manifestSha256:BUILD58_PROVENANCE.manifestSha256}) + } + assert.equal(v.format,'application-writer-source56-definition-approval-v1') + assert.equal(canonical(v.provenance),canonical(BUILD56_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED56_ROLE_TEMPLATE)) + return Object.freeze({edition:56 as const,definitionSha256:BUILD56_PROVENANCE.templateSha256,manifestSha256:BUILD56_PROVENANCE.manifestSha256}) +} +type CatalogueSession = {query:(sql:string,values?:unknown[])=>Promise<{rows:Record[]} >} +type CatalogueInstallation = {roleBindings:ConstructorParameters[0]} +assert.equal(FIXED44_CATALOGUE_BUILD_SQL.split('LIMIT 104').length,2) +const fixed56Sql = FIXED44_CATALOGUE_BUILD_SQL.replace('LIMIT 104','LIMIT 110') +/** Real incremental DATA verifier on caller's retained transaction. No pool or attestor. */ +export const collectSource44Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 44) +export const collectSource56Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 56) +export const collectSource57Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 57) +export const collectSource58Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 58) +async function collectCatalogue(s:CatalogueSession,installation:CatalogueInstallation,edition:CatalogueEdition){ + const settings=(await s.query("SELECT pg_catalog.current_setting('search_path') AS path,pg_catalog.current_setting('TimeZone') AS zone,pg_catalog.current_setting('DateStyle') AS dates")).rows + assert.equal(settings.length,1);assert(['path','zone','dates'].every(k=>typeof settings[0][k]==='string')) + let cursor=false,primary:unknown,failed=false + try{ + await s.query("SELECT pg_catalog.set_config('search_path','pg_catalog, public',true),pg_catalog.set_config('TimeZone','UTC',true),pg_catalog.set_config('DateStyle','ISO, YMD',true)") + const bindings=installation.roleBindings,values=['operator','runtime','capture','publisher','control'].map(k=>bindings[k as keyof typeof bindings].name) + if(edition===58)await assertRestoreProfileQuarantine(s,bindings.runtime.name,bindings.operator.name,bindings.control.name) + const bridge=new Fixed44CatalogueValueBridgeData(bindings,edition) + await s.query('DECLARE mm_publisher44 NO SCROLL CURSOR WITHOUT HOLD FOR '+(edition===44?FIXED44_CATALOGUE_BUILD_SQL:fixed56Sql),values);cursor=true + for(;;){const page=(await s.query('FETCH FORWARD 128 FROM mm_publisher44')).rows;bridge.acceptPage(page);if(page.length===0)break} + const result=bridge.finish();await s.query('CLOSE mm_publisher44');cursor=false + return {catalogSha256:sha(canonical({values:result.values.snapshotSha256,catalogue:result.catalogue})),definitionSha256:result.catalogue.definitionSha256,manifestSha256:result.catalogue.manifestSha256,leafCount:result.catalogue.leafRelations} + }catch(error){primary=error;failed=true;throw error} + finally{ + try{if(cursor)await s.query('CLOSE mm_publisher44');await s.query("SELECT pg_catalog.set_config('search_path',$1,true),pg_catalog.set_config('TimeZone',$2,true),pg_catalog.set_config('DateStyle',$3,true)",[settings[0].path,settings[0].zone,settings[0].dates])} + catch(error){if(failed)throw new AggregateError([primary,error],'Catalogue and context cleanup failed');throw error} + } +} diff --git a/backend/scripts/writer-profile/context.mjs b/backend/scripts/writer-profile/context.mjs new file mode 100644 index 00000000..dceb75c3 --- /dev/null +++ b/backend/scripts/writer-profile/context.mjs @@ -0,0 +1,28 @@ +// Internal fixed SQL/state verification. No signing, provisioning, pool or issuer. +import {canonical} from '../../src/db/application-writer-profile-format.js' +import {readProtectedRegistryStructureData} from '../../src/db/application-writer-profile-registry-structure.js' +export {readCurrentApplicationWriterProfile as readCurrent,HISTORY,CURRENT} from '../../src/db/application-writer-profile-registry-read.js' + + +const same=(a,b,phase)=>{if(canonical(a)!==canonical(b))throw Error('Protected registry refused: '+phase)} +export async function ownedContext(client,installation,actor,phase){ + const rows=(await client.query(`SELECT CURRENT_USER::text AS actor,SESSION_USER::text AS login,r.oid::text AS role_oid, + d.oid::text AS database_oid,d.datname AS database_name,pg_catalog.pg_backend_pid() AS pid, + pg_catalog.pg_current_xact_id_if_assigned()::text AS xid,pg_catalog.current_setting('transaction_read_only') AS read_only, + pg_catalog.current_setting('transaction_isolation') AS isolation,pg_catalog.current_setting('server_version_num')::int AS version, + pg_catalog.pg_is_in_recovery() AS recovery,control.system_identifier::text AS system_identifier, + (EXTRACT(EPOCH FROM pg_catalog.clock_timestamp())*1000)::bigint::text AS server_now, + EXISTS(SELECT 1 FROM pg_catalog.pg_locks l WHERE l.pid=pg_catalog.pg_backend_pid() AND l.locktype='advisory' + AND l.database=d.oid AND l.classid=0 AND l.objid=8723110 AND l.objsubid=1 AND l.granted + AND l.mode=ANY($1::text[])) AS admitted + FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_database d CROSS JOIN pg_catalog.pg_control_system() control + WHERE r.rolname=CURRENT_USER AND d.datname=pg_catalog.current_database()`,[phase.startsWith('publisher')?['ExclusiveLock']:['ShareLock','ExclusiveLock']])).rows + const c=rows[0],expectedOid=installation.roleBindings.publisher.signedOid + if(actor!==installation.roleBindings.publisher.name||rows.length!==1||!c||c.actor!==actor||c.login!==actor||c.role_oid!==expectedOid||c.recovery!==false||c.version<180000||c.version>=190000||c.admitted!==true||!Number.isSafeInteger(c.pid)||!/^[1-9][0-9]*$/.test(c.database_oid)||!(/^[1-9][0-9]{0,19}$/).test(c.system_identifier))throw Error('Distinct actual actor/source/admission context refused') + if(phase==='publisher-read'&&(c.read_only!=='on'||c.isolation!=='repeatable read')||phase==='publisher-inspect'&&(c.read_only!=='on'||c.isolation!=='read committed')||['publisher-write','operator'].includes(phase)&&(c.read_only!=='off'||c.isolation!=='read committed'||!(/^[1-9][0-9]*$/).test(c.xid??'')))throw Error('Distinct owned transaction context refused') + return c +} +export function sameOwned(a,b){same(Object.fromEntries(['actor','login','role_oid','database_oid','database_name','pid','xid','read_only','isolation','version','recovery','system_identifier','admitted'].map(k=>[k,a[k]])),Object.fromEntries(['actor','login','role_oid','database_oid','database_name','pid','xid','read_only','isolation','version','recovery','system_identifier','admitted'].map(k=>[k,b[k]])),'backend/fullXID/lifetime')} +export const sourceCoordinates=c=>({systemIdentifier:c.system_identifier,databaseOid:c.database_oid,databaseName:c.database_name,serverVersionNum:c.version,inRecovery:false}) + +export function protectedStructure(client,installation,runtimeReads=false){return readProtectedRegistryStructureData(client,installation.roleBindings,runtimeReads)} diff --git a/backend/scripts/writer-profile/expanded44-data-policy.ts b/backend/scripts/writer-profile/expanded44-data-policy.ts new file mode 100644 index 00000000..eb8358bc --- /dev/null +++ b/backend/scripts/writer-profile/expanded44-data-policy.ts @@ -0,0 +1,134 @@ +// VERSIONED DATA-only candidate. Never installation policy, enrollment or writer permission. +import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './fixed44-build-definition.js' +import {BUILD56_PROVENANCE,FIXED56_ROLE_TEMPLATE} from './fixed56-build-definition.js' +import {BUILD57_PROVENANCE} from './fixed57-build-definition.js' +import {BUILD58_PROVENANCE} from './fixed58-build-definition.js' +import {fixedCatalogueParentPeriodData} from './application-writer-profile-catalogue-parent-policy.js' +import {GROUPS,type CatalogueEdition,type Inventory} from './catalogue-projection.js' +export const EXPANDED44_POLICY_VERSION='source44-managed-leaves-3000-data-v1' as const +const PLANNED=2000,EXPIRED=1000,PER_PARENT=1000,LEAVES=PLANNED+EXPIRED +const source=FIXED44_ROLE_TEMPLATE +const baseNames=new Set(source.tables.map(row=>String(row.relname))) +const parents=[...baseNames].filter(name=>fixedCatalogueParentPeriodData(name)!==undefined) +const perRelation=new Map([...baseNames].map(name=>[name,Object.fromEntries(GROUPS.map(group=>[group,source[group].filter(row=>(group==='inheritance'?row.child:row.relname)===name).length])) as Record])) +const base=Object.freeze(Object.fromEntries(GROUPS.map(group=>[group,source[group].length])) as Record) +const leafMax=Object.freeze(Object.fromEntries(GROUPS.map(group=>[group,group==='inheritance'?1:Math.max(...parents.map(name=>perRelation.get(name)![group]))])) as Record) +if(baseNames.size!==104||source.tables.length!==104||parents.length!==12||leafMax.columns!==18||leafMax.constraints!==17||leafMax.triggers!==5||leafMax.indexes!==2||leafMax.roleGrants!==4)throw Error('Exact reviewed source44 topology changed; rederive policy') +for(const row of source.constraints)if(parents.includes(String(row.relname))&&!['n','c','p','f'].includes(String(row.contype)))throw Error('Unreviewed parent constraint semantics') +const total=Object.freeze(Object.fromEntries(GROUPS.map(group=>[group,base[group]+LEAVES*leafMax[group]])) as Record) +export const EXPANDED44_DATA_POLICY=Object.freeze({version:EXPANDED44_POLICY_VERSION,baseRelations:baseNames.size,parents:parents.length,maxPlannedLeaves:PLANNED,maxExpiredBacklog:EXPIRED,maxManagedLeaves:LEAVES,maxPerParent:PER_PARENT,maxRelations:baseNames.size+LEAVES,maxWeightedPlan:2000,baseRows:base,maxLeafRows:leafMax,maxRows:total,definitionSha256:BUILD44_PROVENANCE.templateSha256,authority:false,productionInstallationPolicyApproved:false,settingsAdmissionAuthorized:false,readyAuthorized:false}) +const source56PerRelation = new Map(FIXED56_ROLE_TEMPLATE.tables.map(({relname}) => [String(relname), Object.fromEntries( + GROUPS.map(group => [group, FIXED56_ROLE_TEMPLATE[group].filter(row => (group === 'inheritance' ? row.child : row.relname) === relname).length]), +) as Record])) +const source56BaseRows = Object.freeze(Object.fromEntries(GROUPS.map(group => [group, FIXED56_ROLE_TEMPLATE[group].length])) as Record) +if (source56PerRelation.size !== 110 || FIXED56_ROLE_TEMPLATE.tables.length !== 110 || + [...source56PerRelation.keys()].filter(name => fixedCatalogueParentPeriodData(name)).length !== parents.length || + parents.some(name => GROUPS.some(group => source56PerRelation.get(name)?.[group] !== perRelation.get(name)![group]))) { + throw Error('Unexpected source56 base or managed partition topology') +} +const EXPANDED56_DATA_POLICY = Object.freeze({ + ...EXPANDED44_DATA_POLICY, + version: 'source56-managed-leaves-3000-data-v1', + baseRelations: source56PerRelation.size, + maxRelations: source56PerRelation.size + LEAVES, + baseRows: source56BaseRows, + maxRows: Object.freeze(Object.fromEntries(GROUPS.map(group => [group, source56BaseRows[group] + LEAVES * leafMax[group]])) as Record), + definitionSha256: BUILD56_PROVENANCE.templateSha256, +}) +const auditCounts = source56PerRelation.get('mcp_audit')! +const source57PerRelation = new Map(source56PerRelation).set('mcp_audit', {...auditCounts, indexes: auditCounts.indexes + 1}) +const EXPANDED57_DATA_POLICY = Object.freeze({ + ...EXPANDED56_DATA_POLICY, + version: 'source57-managed-leaves-3000-data-v1', + baseRows: Object.freeze({...source56BaseRows, indexes: source56BaseRows.indexes + 1}), + maxRows: Object.freeze({...EXPANDED56_DATA_POLICY.maxRows, indexes: EXPANDED56_DATA_POLICY.maxRows.indexes + 1}), + definitionSha256: BUILD57_PROVENANCE.templateSha256, +}) +const EXPANDED58_DATA_POLICY = Object.freeze({ + ...EXPANDED57_DATA_POLICY, + version: 'source58-managed-leaves-3000-data-v1', + definitionSha256: BUILD58_PROVENANCE.templateSha256, +}) +const policies = {44: EXPANDED44_DATA_POLICY, 56: EXPANDED56_DATA_POLICY, 57: EXPANDED57_DATA_POLICY, 58: EXPANDED58_DATA_POLICY} +const relationCounts = {44: perRelation, 56: source56PerRelation, 57: source57PerRelation, 58: source57PerRelation} +export const catalogueTopologyPolicy = (edition: CatalogueEdition) => policies[edition] +export const expanded44ParentRowCountsData=()=>Object.freeze(Object.fromEntries(parents.map(name=>[name,Object.freeze({...perRelation.get(name)!})]))) +export interface ManagedLeafData {readonly parent:string;readonly name:string} +export function validateManagedLeafNameData(input:ManagedLeafData):void { + if(!input||Object.keys(input).sort().join(',')!=='name,parent'||typeof input.parent!=='string'||typeof input.name!=='string')throw Error('Exact managed leaf DATA shape required') + const period=fixedCatalogueParentPeriodData(input.parent) + if(!period||!input.name.startsWith(input.parent+'_'+period+'_'))throw Error('Unknown/cross-parent managed leaf') + const stamp=input.name.slice(input.parent.length+3) + if(!new RegExp('^[0-9]{'+(period==='m'?6:8)+'}$').test(stamp))throw Error('Managed leaf name shape refused') + const iso=stamp.slice(0,4)+'-'+stamp.slice(4,6)+'-'+(period==='m'?'01':stamp.slice(6,8)),start=new Date(iso+'T00:00:00Z'),end=new Date(start) + if(!Number.isFinite(+start)||start.toISOString().slice(0,10)!==iso||period==='w'&&start.getUTCDay()!==1)throw Error('Managed leaf calendar refused') + if(period==='m')end.setUTCMonth(end.getUTCMonth()+1);else end.setUTCDate(end.getUTCDate()+(period==='w'?7:1)) + if(start.getUTCFullYear()<100||end.getUTCFullYear()>9998)throw Error('Managed leaf bounded calendar refused') +} +/** Only exact template-validated relation counts enter this sticky cumulative DATA budget. */ +export class Expanded44TopologyBudgetData { + constructor(private readonly edition: CatalogueEdition = 44) {} + private readonly names=new Set();private readonly perParent=new Map();private readonly rows=Object.fromEntries(GROUPS.map(g=>[g,0])) as Record + private bases=0;private leaves=0;private failed=false;private closed=false + acceptRelation(kind:'base'|'leaf',name:string,groups:Inventory,parent?:string):void { + try{ + const policy = catalogueTopologyPolicy(this.edition) + const counts = relationCounts[this.edition] + if(this.failed||this.closed||this.names.has(name)||this.names.size>=policy.maxRelations)throw Error('Closed/duplicate/total relation DATA envelope') + const expected=kind==='base'&&parent===undefined?counts.get(name):kind==='leaf'&&parent!==undefined&&parents.includes(parent)?{...counts.get(parent)!,inheritance:1}:undefined + if(!expected)throw Error('Unknown source'+this.edition+' base/parent') + if(kind==='leaf'){ + validateManagedLeafNameData({name,parent:parent!}) + if(this.leaves>=LEAVES||(this.perParent.get(parent!)??0)>=PER_PARENT)throw Error('Managed leaf total/per-parent DATA envelope') + } + for(const group of GROUPS){if(!Array.isArray(groups[group])||groups[group].length!==expected[group]||this.rows[group]+groups[group].length>policy.maxRows[group])throw Error('Exact source'+this.edition+' '+group+' DATA row envelope')} + this.names.add(name);for(const group of GROUPS)this.rows[group]+=groups[group].length + if(kind==='base')this.bases++;else{this.leaves++;this.perParent.set(parent!,1+(this.perParent.get(parent!)??0))} + }catch(error){this.failed=true;throw error} + } + finish(){if(this.failed||this.closed||this.bases!==catalogueTopologyPolicy(this.edition).baseRelations)throw Error('Incomplete/refused expanded topology DATA');this.closed=true;return Object.freeze({baseRelations:this.bases,leafRelations:this.leaves,rows:Object.freeze({...this.rows}),perParent:Object.freeze(Object.fromEntries(this.perParent)),authority:false})} +} +/** Proposed projected admission DATA, not a trusted expiry observation or permission. + * Genuine host must classify current leaves and bind same-session maintenance/settings separately. + * Planned can include future leaves not present yet. Unclassified current leaves refuse. + */ +export function validateExpanded44AdmissionData(input:Readonly<{current:readonly ManagedLeafData[];planned:readonly ManagedLeafData[];expiredBacklog:readonly ManagedLeafData[]}>){ + if(!input||Object.keys(input).sort().join(',')!=='current,expiredBacklog,planned')throw Error('Exact admission DATA fields required') + const parse=(values:readonly ManagedLeafData[],cap:number)=>{if(!Array.isArray(values)||values.length>cap)throw Error('Admission DATA list envelope');const map=new Map();for(const row of values){validateManagedLeafNameData(row);if(map.has(row.name))throw Error('Duplicate admission leaf');map.set(row.name,row)}return map} + const current=parse(input.current,LEAVES),planned=parse(input.planned,PLANNED),expired=parse(input.expiredBacklog,EXPIRED) + let weighted=0;for(const row of planned.values())weighted+=['flow_hour_facts','flow_daily_facts'].includes(row.parent)?2:1 + if(weighted>2000)throw Error('Source weighted planned budget exceeded') + const projected=new Map(planned) + for(const [name,row]of expired){if(projected.has(name)||!current.has(name))throw Error('Overlapping/noncurrent expired DATA');projected.set(name,row)} + for(const [name,row]of current){const classified=projected.get(name);if(!classified||classified.parent!==row.parent)throw Error('Unclassified/cross-parent current leaf DATA')} + if(projected.size>LEAVES)throw Error('Projected managed leaves DATA envelope') + const perParent=new Map();for(const row of projected.values()){const count=1+(perParent.get(row.parent)??0);if(count>PER_PARENT)throw Error('Projected per-parent DATA envelope');perParent.set(row.parent,count)} + return Object.freeze({version:EXPANDED44_POLICY_VERSION,currentLeaves:current.size,plannedLeaves:planned.size,expiredBacklog:expired.size,projectedLeaves:projected.size,weightedPlan:weighted,perParent:Object.freeze(Object.fromEntries(perParent)),authority:false,expiryClassificationAuthenticated:false,settingsAdmissionAuthorized:false,readyAuthorized:false}) +} + +/** Exact query meaning, not deparser-byte guesses. Unknown internal tree sizes + * remain the unchanged actual SQL scalar preflight's obligation. */ +export const EXPANDED44_SOURCE_SIZE_SEMANTICS_DATA=Object.freeze({ + function_bytes:'sum pg_proc.prosrc UTF8 bytes; fixed global functions, no per-leaf routine copy', + default_bytes:'sum pg_attrdef.adbin::text pg_node_tree bytes; NOT default_expression deparser bytes', + check_bytes:'sum pg_constraint.conbin::text pg_node_tree bytes; NOT pg_get_constraintdef bytes', + trigger_bytes:'sum octet_length(tgqual::text)+octet_length(tgargs); NULL qualifier terms are ignored by SQL sum; NOT trigger definition bytes', + unchangedEachSourceScalarLimitBytes:2097152,internalTreeSizesDerived:false,actualSqlRequired:true,authority:false +}) +export const expanded44LeafSourceShapeFactsData=()=>Object.freeze(Object.fromEntries(parents.map(parent=>{ + const columns=source.columns.filter(row=>row.relname===parent),constraints=source.constraints.filter(row=>row.relname===parent),triggers=source.triggers.filter(row=>row.relname===parent) + return [parent,Object.freeze({defaultExpressionRows:columns.filter(row=>row.default_expression!==null).length,checkExpressionRows:constraints.filter(row=>row.contype==='c').length,qualifierRows:triggers.filter(row=>row.unconditional!==true).length,argumentBytes:triggers.reduce((sum,row)=>sum+Number(row.arg_bytes),0),routineCopiesPerLeaf:0,internalTreeBytesKnown:false})] +}))) + +/** Exact extremum of expression ROW COUNTS, not an invented pg_node_tree byte cap. + * Unit leaf costs +1000 capacity/parent +3000 total => top3 source costs. + * Different scalar extrema may require different valid topologies. + */ +export function expanded44SourceShapeExtremaData(){ + const facts=expanded44LeafSourceShapeFactsData() + const maximize=(field:'defaultExpressionRows'|'checkExpressionRows')=>{ + const selected=Object.entries(facts).sort((a,b)=>b[1][field]-a[1][field]||(a[0]sum+value[field]*PER_PARENT,0),cohorts:Object.freeze(selected.map(([parent,value])=>Object.freeze({parent,leaves:PER_PARENT,rowsPerLeaf:value[field]})))}) + } + return Object.freeze({baseDefaultExpressionRows:source.columns.filter(row=>row.default_expression!==null).length,baseCheckExpressionRows:source.constraints.filter(row=>row.contype==='c').length,baseQualifiedTriggerRows:source.triggers.filter(row=>row.unconditional!==true).length,maxAddedDefaultExpressionRows:maximize('defaultExpressionRows'),maxAddedCheckExpressionRows:maximize('checkExpressionRows'),perLeafQualifiedTriggerRows:0,perLeafRoutineCopies:0,internalTreeByteUpperBoundKnown:false,actualSqlSourceScalarGateRequired:true,authority:false}) +} diff --git a/backend/scripts/writer-profile/fixed44-build-definition.ts b/backend/scripts/writer-profile/fixed44-build-definition.ts new file mode 100644 index 00000000..01389ab8 --- /dev/null +++ b/backend/scripts/writer-profile/fixed44-build-definition.ts @@ -0,0 +1,4 @@ +// OFFLINE source-derived candidate; clean4role SQL verification PENDING. No installation enrollment. +import type {Inventory} from "./catalogue-projection.js" +export const BUILD44_PROVENANCE={"format":"source44-build-candidate-v1","manifestSha256":"fffae3381732e6f276ba276aca7185f6a87587edf8272ba42763b7f49bc59ef5","reviewed43Sha256":"4dd21cf909628f2750294e0d4398064d5219f6547c1bcebec910efabe9d57dcd","migration44Sha256":"37b88d1679be9bb47f16ccc8cae7373f20f9c05e7a8f662c3775f18d78bb5613","templateSha256":"56bbe286968fd950e5239672158f198eb2d600b7fa2fcb290c2ec5befd08d90c","baseTables":104,"newColumns":13,"newConstraints":21,"newFunctions":3,"newIndexes":5,"newApplicationTriggers":4,"newInheritedWriterGuards":3,"sourceNamingAlgorithm":"PG18 makeObjectName balanced ASCII byte truncation; ChooseConstraintName collision-free branch only; collisions refuse","namingSources":["https://raw.githubusercontent.com/postgres/postgres/REL_18_STABLE/src/backend/commands/indexcmds.c","https://raw.githubusercontent.com/postgres/postgres/REL_18_STABLE/src/backend/catalog/pg_constraint.c"],"cleanFourRoleSqlVerified":false,"productionInstallationPolicyApproved":false,"authority":false} as const +export const FIXED44_ROLE_TEMPLATE:Readonly={"columns":[{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"access_scope","attnotnull":false,"attnum":8,"attstorage":"x","default_expression":null,"relname":"mcp_artifacts","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"account_private_key","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"''::text","relname":"acme_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ack_proof","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_poll_acks","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"acknowledged_at","attnotnull":false,"attnum":6,"attstorage":"p","default_expression":null,"relname":"mcp_drift","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"action","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_audit","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"action","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'route'::text","relname":"filter_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"action_id","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"vrf_production_tickets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"action_version","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"active","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"true","relname":"app_users","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"actor_id","attnotnull":false,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"actor_id","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_audit","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"actor_id","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_diagnostic_cancellations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"actor_id","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"admissions","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"0","relname":"server_poll_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"admissions","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"0","relname":"server_probe_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"after_sealed","attnotnull":false,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_operation_checkpoints","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"api_key","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"evobgp_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"applied_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"now()","relname":"schema_migrations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"approved_at","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"mcp_plans","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"approved_by","attnotnull":false,"attnum":11,"attstorage":"x","default_expression":null,"relname":"mcp_plans","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"archive_bytes","attnotnull":false,"attnum":21,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"archive_bytes","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"server_poll_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"archive_proof","attnotnull":true,"attnum":17,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"asn","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_asn_meta","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"asn","attnotnull":true,"attnum":12,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"asn","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"0","relname":"flow_ip_meta","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"asn","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":null,"relname":"flow_daily_facts","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"asn","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":null,"relname":"flow_hour_facts","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"asn_build_at","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"geoip_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"attempted_stages","attnotnull":false,"attnum":16,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"attempted_stages","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_quarantines","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"attempts","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"1","relname":"mcp_unauthorized_buckets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"attestation","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_management_channels","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"audience","attnotnull":false,"attnum":10,"attstorage":"x","default_expression":null,"relname":"mcp_credentials","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"audit_retention_days","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"180","relname":"mcp_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authentication","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_expires_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_expires_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_expires_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_issued_at","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_issued_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_profile_id","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_profile_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"authority_profile_version","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"avatar","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"''::text","relname":"app_users","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bandwidth_at","attnotnull":false,"attnum":13,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"base_url","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"''::text","relname":"evobgp_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"batch_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_write_receipts","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"before_proof","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_poll_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"before_sealed","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_operation_checkpoints","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"before_state","attnotnull":false,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_operation_steps","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":12,"attstorage":"x","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_quarantines","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"server_poll_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_probe_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_poll_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":null,"relname":"mcp_routing_forward_charges","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_proof","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":null,"relname":"mcp_routing_reservations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_sealed","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_sealed","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_routing_forward_charges","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"binding_sealed","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_routing_reservations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"board_name","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"server_snapshots","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bot_token","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"''::text","relname":"alert_telegram_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bucket","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_unauthorized_buckets","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bucket","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_budget_buckets","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bucket_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"flow_buckets","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bucket_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"flow_hour_facts","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bucket_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"flow_minute_dims","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bucket_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"flow_minute_stats","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bytes","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"0","relname":"flow_minute_stats","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bytes","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"flow_daily_dims","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bytes","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"flow_minute_dims","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bytes","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"0","relname":"flow_daily_facts","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bytes","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"0","relname":"flow_hour_facts","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"bytes","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"cancelled_at","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"candidate_id","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_path_checks","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"cert_name","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"challenge","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"channel","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"'telegram'::text","relname":"alert_outbox","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"charge_units","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"charge_units","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"1","relname":"mcp_routing_forward_charges","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"chat_id","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"alert_telegram_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"check_gateway","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":"''::text","relname":"recursive_routes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ciphertext","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":null,"relname":"vrf_production_tickets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ciphertext","attnotnull":true,"attnum":15,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ciphertext","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_secrets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ciphertext","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_artifacts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ciphertext_bytes","attnotnull":true,"attnum":16,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_attempted_at","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_attempted_at","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_token","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_token","attnotnull":false,"attnum":7,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claimed_at","attnotnull":false,"attnum":14,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"client_hash","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_unauthorized_buckets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"client_id","attnotnull":false,"attnum":9,"attstorage":"x","default_expression":null,"relname":"mcp_credentials","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"client_id","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"cloudflare_api_token","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"acme_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"code_digest","attnotnull":false,"attnum":11,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"collector_ip","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"'10.255.254.1'::text","relname":"traffic_flow_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"combine_mode","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"'any'::text","relname":"alert_groups","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"comment","attnotnull":true,"attnum":12,"attstorage":"x","default_expression":"''::text","relname":"recursive_routes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"comment","attnotnull":true,"attnum":13,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"comment","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":"''::text","relname":"user_interface_bindings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"committed_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"flow_write_receipts","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"community","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"filter_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"community_name","attnotnull":false,"attnum":5,"attstorage":"x","default_expression":null,"relname":"filter_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"compact_at","attnotnull":false,"attnum":20,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"compacted_admissions","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"server_poll_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"compaction_claim","attnotnull":false,"attnum":23,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"compaction_claim_xid","attnotnull":false,"attnum":25,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"xid8"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"compaction_claimed_at","attnotnull":false,"attnum":24,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"compaction_xid","attnotnull":true,"attnum":19,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"xid8"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"completed_stages","attnotnull":false,"attnum":17,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"completed_stages","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_quarantines","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"compressed_bytes","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"condition","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"condition_line","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"alert_rule_conditions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"config","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_routing_profiles","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"config_sealed","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_management_channels","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"configuration_proof","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_operation_checkpoints","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"configuration_sealed","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_operation_checkpoints","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"confirm_stability_sec","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"alert_rules","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"mcp_oauth_requests","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":false,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_requests","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"mcp_audit","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_budget_buckets","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_artifacts","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_credentials","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_diagnostic_cancellations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_grants","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_path_checks","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_plans","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_read_leases","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_routing_profiles","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_operations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_routing_forward_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"consumed_at","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"mcp_oauth_requests","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"context_ciphertext","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_poll_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"context_proof","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"server_poll_quarantines","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"context_proof","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_poll_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"context_proof","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"context_proof","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"server_poll_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"context_verified","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"false","relname":"mcp_routing_observations","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"conversations","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"0","relname":"flow_minute_stats","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"cooldown","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":"'5м'::text","relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"cooldown_override","attnotnull":false,"attnum":5,"attstorage":"x","default_expression":null,"relname":"alert_groups","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"country","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":"''::text","relname":"recursive_routes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"country","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"country","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"flow_ip_meta","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"country","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"flow_daily_facts","type":"character(2)"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"country","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"flow_hour_facts","type":"character(2)"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"country_build_at","attnotnull":false,"attnum":7,"attstorage":"p","default_expression":null,"relname":"geoip_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"cpu_load","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"server_snapshots","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"cpu_load","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"uptime_resource_samples","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"now()","relname":"filter_rules","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"now()","relname":"geoip_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_probe_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"now()","relname":"app_users","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"now()","relname":"mcp_requests","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"now()","relname":"uptime_probes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"now()","relname":"mcp_plans","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"now()","relname":"mcp_routing_reservations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"now()","relname":"uptime_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"now()","relname":"alert_rules","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"now()","relname":"mcp_scoped_recovery_intents","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"now()","relname":"recursive_routes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":16,"attstorage":"p","default_expression":"now()","relname":"mcp_routing_observations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":18,"attstorage":"p","default_expression":"now()","relname":"servers","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":18,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":19,"attstorage":"p","default_expression":"now()","relname":"traffic_flow_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"events","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":20,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_probe_executions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":20,"attstorage":"p","default_expression":"now()","relname":"uptime_speed_probes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":21,"attstorage":"p","default_expression":"now()","relname":"uptime_speed_test_runs","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"clock_timestamp()","relname":"mcp_diagnostic_cancellations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_poll_receipts","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"now()","relname":"mcp_portal_events","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"now()","relname":"mcp_recovery_intents","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_poll_quarantines","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"mcp_oauth_clients","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"mcp_operation_checkpoints","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"mcp_protections","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"mcp_routing_reservation_slots","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_poll_acks","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_poll_intents","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"now()","relname":"alert_groups","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"now()","relname":"flow_settings_intents","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"now()","relname":"mcp_audit","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"now()","relname":"mcp_secrets","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_poll_executions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_probe_execution_stages","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"now()","relname":"mcp_artifacts","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"now()","relname":"mcp_connections","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"now()","relname":"mcp_credentials","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"now()","relname":"mcp_path_checks","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"now()","relname":"servers_api_ping_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"config_revisions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"internet_path_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"mcp_operation_steps","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"traffic_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"backup_entries","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"clock_timestamp()","relname":"server_probe_execution_quarantines","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"now()","relname":"alert_outbox","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"now()","relname":"mcp_operations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"now()","relname":"user_interface_bindings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":null,"relname":"mcp_routing_forward_charges","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_by","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_protections","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_by","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_secrets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"created_by","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_management_channels","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"credential_id","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"mcp_routing_profiles","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"day","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"flow_daily_dims","type":"date"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"day","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"flow_daily_facts","type":"date"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"deadline","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"deadline","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"decoded_bytes","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dedupe_key","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"alert_outbox","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"default_zone_id","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":"''::text","relname":"acme_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"description","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"''::text","relname":"filter_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"destination_server_id","attnotnull":false,"attnum":13,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"detected_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"now()","relname":"mcp_drift","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"digest","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_credentials","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"digest","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":null,"relname":"vrf_production_tickets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dim","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"flow_daily_dims","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dim","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"flow_minute_dims","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"direction","attnotnull":true,"attnum":12,"attstorage":"x","default_expression":"'both'::text","relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"direction","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"'both'::text","relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"directory_url","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"'https://acme-v02.api.letsencrypt.org/directory'::text","relname":"acme_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"disabled","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"false","relname":"recursive_routes","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"distance","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"1","relname":"recursive_routes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dl_mbps","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst","attnotnull":true,"attnum":4,"attstorage":"m","default_expression":null,"relname":"flow_buckets","type":"inet"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_address","attnotnull":false,"attnum":8,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_address","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"recursive_routes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_interface","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"''::text","relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_interface","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_routing_observations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_interface","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"''::text","relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_interface_address","attnotnull":false,"attnum":10,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_port","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_server_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_server_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"dst_server_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"uptime_speed_test_runs","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"duration_ms","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"mcp_requests","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"duration_ms","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"0","relname":"scheduler_runs","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"duration_sec","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"10","relname":"uptime_speed_test_runs","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"duration_sec","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"10","relname":"uptime_speed_probes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"effect_proof","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"server_poll_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"effective_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":null,"relname":"router_log_entries","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"email","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":"''::text","relname":"app_users","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"true","relname":"servers","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"false","relname":"mcp_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"false","relname":"router_log_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"false","relname":"servers_api_ping_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"false","relname":"traffic_flow_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"true","relname":"backup_schedule_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"true","relname":"certificate_renew_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"true","relname":"geoip_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"true","relname":"internet_path_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"true","relname":"traffic_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":"true","relname":"uptime_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"false","relname":"evobgp_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"true","relname":"alert_groups","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"true","relname":"mcp_oauth_clients","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"false","relname":"mcp_routing_profiles","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"true","relname":"alert_rules","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"true","relname":"uptime_probes","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"enabled","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"true","relname":"uptime_speed_probes","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"entity_id","attnotnull":false,"attnum":9,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"entity_type","attnotnull":false,"attnum":8,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error","attnotnull":false,"attnum":13,"attstorage":"x","default_expression":null,"relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error","attnotnull":false,"attnum":20,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error","attnotnull":false,"attnum":5,"attstorage":"x","default_expression":null,"relname":"servers_rest_ping_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"scheduler_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"traffic_collection_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error_code","attnotnull":false,"attnum":8,"attstorage":"x","default_expression":null,"relname":"mcp_operations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error_code","attnotnull":false,"attnum":9,"attstorage":"x","default_expression":null,"relname":"mcp_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"error_code","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_quarantines","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"etags_json","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"'{}'::jsonb","relname":"geoip_settings","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"event_at","attnotnull":false,"attnum":7,"attstorage":"p","default_expression":null,"relname":"router_log_entries","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"event_type","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"evidence","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_path_checks","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"evidence","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":null,"relname":"mcp_plans","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"execution_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"execution_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"server_poll_executions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"execution_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"server_poll_quarantines","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"execution_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"execution_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_quarantines","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"execution_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"execution_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expected","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_drift","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expires_at","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":null,"relname":"mcp_plans","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expires_at","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expires_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_read_leases","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expires_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_credentials","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expires_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":null,"relname":"mcp_artifacts","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expires_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"mcp_management_channels","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"expires_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"mcp_oauth_requests","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"failures","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"0","relname":"router_log_collection_state","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"family","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_routing_observations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"family_id","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"mcp_credentials","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"fetched_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"flow_asn_meta","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"fetched_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"flow_ip_meta","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"filename","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_artifacts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"filename","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"fingerprint","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"config_revisions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"mcp_operations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"certificate_issue_jobs","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"mcp_requests","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":false,"attnum":15,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":false,"attnum":21,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_recovery_intents","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"mcp_operation_steps","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"finished_at","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"scheduler_runs","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"fired_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"alert_history","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"flags","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"0","relname":"traffic_samples","type":"smallint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"flow_end_ms","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"flow_listen_port","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"4739","relname":"traffic_flow_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"flow_start_ms","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"format","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"'rsc'::text","relname":"backup_schedule_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"free_hdd_space","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"0","relname":"uptime_resource_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"free_memory","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"server_snapshots","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"free_memory","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"uptime_resource_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"frequency","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"'daily'::text","relname":"backup_schedule_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"gap_at","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"router_log_collection_state","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"gateway","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"recursive_routes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"gateway","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"''::text","relname":"filter_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"gateway_tunnel_id","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":"''::text","relname":"filter_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"gc_not_before","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"'infinity'::timestamp with time zone","relname":"server_probe_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"gc_not_before","attnotnull":true,"attnum":16,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"generation","attnotnull":false,"attnum":10,"attstorage":"x","default_expression":null,"relname":"mcp_routing_profiles","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"generation","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":null,"relname":"router_log_entries","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"generation","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"router_log_collection_state","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"generation","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_connections","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"generation","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_plans","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"generation","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_management_channels","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"grant_expires_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":null,"relname":"mcp_routing_profiles","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"group_id","attnotnull":false,"attnum":13,"attstorage":"x","default_expression":null,"relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"group_id","attnotnull":false,"attnum":3,"attstorage":"x","default_expression":null,"relname":"alert_history","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"holder","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"''::text","relname":"flow_asn_meta","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"holder","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"''::text","relname":"flow_ip_meta","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"host","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"router_log_collection_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"host","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"host","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"router_log_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"host_private_key","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"''::text","relname":"traffic_flow_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"host_public_key","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":"''::text","relname":"traffic_flow_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"hour","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"3","relname":"backup_schedule_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"hub_server_id","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"traffic_flow_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"acme_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"alert_engine_cursor","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"alert_telegram_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"backup_schedule_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"backup_storage_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"certificate_renew_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"data_migration","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"evobgp_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"geoip_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"internet_path_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_artifacts","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_connections","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_credentials","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_drift","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_grants","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_management_channels","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_oauth_requests","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_operations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_path_checks","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_plans","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_portal_events","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_protections","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_read_leases","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_requests","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_routing_profiles","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_secrets","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"router_log_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"servers_api_ping_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"traffic_flow_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"traffic_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"uptime_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_groups","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_history","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_outbox","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_rule_conditions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_rule_targets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"app_users","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"config_revisions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_clients","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"scheduler_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"schema_migrations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"uptime_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"user_interface_bindings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"idempotency_key","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_operations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_hash","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_management_channels","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_kind","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_poll_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_kind","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_poll_quarantines","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_kind","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_poll_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_kind","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_quarantines","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_kind","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_kind","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_probe_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_name","attnotnull":false,"attnum":12,"attstorage":"x","default_expression":null,"relname":"server_snapshots","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"identity_proof","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_quarantines","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"iface","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"flow_daily_facts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"iface","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"flow_hour_facts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"in_iface","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":"''::text","relname":"flow_buckets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"input","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"'{}'::jsonb","relname":"mcp_requests","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"intended_state","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_operation_steps","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"intent_proof","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"intent_proof","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_poll_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"intent_sealed","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interface_name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"traffic_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interface_name","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"user_interface_bindings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interface_type","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'other'::text","relname":"user_interface_bindings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interval_sec","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"10","relname":"router_log_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interval_sec","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"120","relname":"servers_api_ping_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interval_sec","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"21600","relname":"certificate_renew_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interval_sec","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"30","relname":"traffic_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interval_sec","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"300","relname":"internet_path_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interval_sec","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"15","relname":"uptime_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"interval_sec","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"0","relname":"uptime_probes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"invalid_before","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_owner_revocations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"inverse_configuration_proof","attnotnull":false,"attnum":23,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"inverse_configuration_sealed","attnotnull":false,"attnum":22,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"inverse_intent_proof","attnotnull":false,"attnum":21,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"inverse_intent_sealed","attnotnull":false,"attnum":20,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"issued_at","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"job_key","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"scheduler_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"keep_count","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"7","relname":"backup_schedule_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"keep_local_copy","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"true","relname":"backup_storage_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"key","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"flow_daily_dims","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"key","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"flow_minute_dims","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"key_type","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":"'rsa2048'::text","relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"kind","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_engine_prev_live","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"kind","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"flow_write_receipts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"kind","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_budget_buckets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"kind","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_stages","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"kind","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_credentials","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"kind","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'manual'::text","relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"lan_subnet","attnotnull":true,"attnum":15,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"lane","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_routing_reservation_slots","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_attempt_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"traffic_collection_state","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_check_at","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"geoip_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_collected_at","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"uptime_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_collected_at","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"servers_api_ping_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_collected_at","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"certificate_renew_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_collected_at","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"internet_path_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_collected_at","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"traffic_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_collected_at","attnotnull":false,"attnum":6,"attstorage":"p","default_expression":null,"relname":"router_log_collection_state","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_datagram_at","attnotnull":false,"attnum":14,"attstorage":"p","default_expression":null,"relname":"traffic_flow_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_duration_ms","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"uptime_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_duration_ms","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"backup_schedule_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_duration_ms","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"servers_api_ping_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_duration_ms","attnotnull":false,"attnum":6,"attstorage":"p","default_expression":null,"relname":"certificate_renew_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_duration_ms","attnotnull":false,"attnum":6,"attstorage":"p","default_expression":null,"relname":"internet_path_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_duration_ms","attnotnull":false,"attnum":6,"attstorage":"p","default_expression":null,"relname":"traffic_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":11,"attstorage":"x","default_expression":null,"relname":"alert_outbox","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":12,"attstorage":"x","default_expression":null,"relname":"uptime_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":13,"attstorage":"x","default_expression":null,"relname":"backup_schedule_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":14,"attstorage":"x","default_expression":null,"relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":16,"attstorage":"x","default_expression":null,"relname":"traffic_flow_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"geoip_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"servers_api_ping_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"certificate_renew_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"internet_path_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"router_log_collection_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_error","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"traffic_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_exporter_ip","attnotnull":false,"attnum":15,"attstorage":"x","default_expression":null,"relname":"traffic_flow_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_fired_at","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"''::text","relname":"alert_engine_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_payload_hash","attnotnull":false,"attnum":3,"attstorage":"x","default_expression":null,"relname":"alert_engine_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_ping_at","attnotnull":false,"attnum":17,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_ping_error","attnotnull":false,"attnum":18,"attstorage":"x","default_expression":null,"relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_ping_loss_pct","attnotnull":false,"attnum":16,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_ping_rtt_ms","attnotnull":false,"attnum":15,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_run_at","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_run_at","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"backup_schedule_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_rx_avg_mbps","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_seen","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"app_users","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_source_finished_at","attnotnull":false,"attnum":2,"attstorage":"p","default_expression":null,"relname":"alert_engine_cursor","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_status","attnotnull":false,"attnum":13,"attstorage":"x","default_expression":null,"relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_succeeded_at","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"traffic_collection_state","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_success_at","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"geoip_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_test_at","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"backup_storage_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_test_error","attnotnull":false,"attnum":12,"attstorage":"x","default_expression":null,"relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_tx_avg_mbps","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"last_used_at","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"mcp_connections","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"lat","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"flow_ip_meta","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"latency_ms","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"servers_rest_ping_samples","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"latency_ms","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"server_snapshots","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"level","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":null,"relname":"router_log_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"level","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"lng","attnotnull":false,"attnum":5,"attstorage":"p","default_expression":null,"relname":"flow_ip_meta","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"login","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"app_users","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"loss_pct","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"loss_pct","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"uptime_probe_samples","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"map_service_min_share_pct","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"5","relname":"traffic_flow_settings","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"max_retries","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"3","relname":"alert_outbox","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"max_stages","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"mcp_connection_id","attnotnull":false,"attnum":7,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"mcp_credential_id","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"measurement_known","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"true","relname":"uptime_probe_samples","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"media_type","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_artifacts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"message","attnotnull":true,"attnum":12,"attstorage":"x","default_expression":null,"relname":"router_log_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"message","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"alert_history","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"message","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"message_thread_id","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"alert_telegram_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"mgmt_tunnel_ip","attnotnull":true,"attnum":17,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"minute","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"backup_schedule_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"mode","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"month_day","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"1","relname":"backup_schedule_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"''::text","relname":"app_users","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"alert_groups","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_connections","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_clients","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"name","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"uptime_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"nat_dst","attnotnull":false,"attnum":16,"attstorage":"m","default_expression":null,"relname":"flow_buckets","type":"inet"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"nat_dst_port","attnotnull":true,"attnum":18,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"nat_src","attnotnull":false,"attnum":15,"attstorage":"m","default_expression":null,"relname":"flow_buckets","type":"inet"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"nat_src_port","attnotnull":true,"attnum":17,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"native_counter_identity","attnotnull":false,"attnum":10,"attstorage":"x","default_expression":null,"relname":"traffic_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"next_attempt_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":null,"relname":"alert_outbox","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"next_hop","attnotnull":false,"attnum":12,"attstorage":"m","default_expression":null,"relname":"flow_buckets","type":"inet"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"next_poll_at","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"router_log_collection_state","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"note","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"config_revisions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"notes","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"object_ref","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_protections","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"observation_error","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"uptime_probe_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"observed","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_drift","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ok","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"servers_rest_ping_samples","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ok","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"1","relname":"flow_ip_meta","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_diagnostic_cancellations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_operation_checkpoints","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_operation_steps","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_drift","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_plans","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_version","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"mcp_plans","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"original_activity_at","attnotnull":false,"attnum":22,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"original_activity_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"original_expires_at","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"out_iface","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":"''::text","relname":"flow_buckets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"outcome","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_stages","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"outcome","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"server_poll_acks","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_claims_issued_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"to_timestamp((0)::double precision)","relname":"mcp_connections","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"mcp_owner_revocations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_portal_events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_probe_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_connections","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_poll_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"vrf_production_tickets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_id","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_permissions","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_connections","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"owner_valid_until","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"now()","relname":"mcp_connections","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"packets","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"flow_minute_stats","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"packets","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"flow_daily_dims","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"packets","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"flow_minute_dims","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"packets","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"0","relname":"flow_daily_facts","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"packets","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"0","relname":"flow_hour_facts","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"packets","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"packets_received","attnotnull":true,"attnum":17,"attstorage":"p","default_expression":"0","relname":"traffic_flow_settings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"password","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"payload","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_grants","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"payload","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'{}'::jsonb","relname":"mcp_audit","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"payload","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'[]'::jsonb","relname":"config_revisions","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"payload","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_plans","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"payload_hash","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"alert_engine_confirm_pending","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"payload_hash","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_portal_events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"peer_name","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"''::text","relname":"user_interface_bindings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"peer_public_key","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"traffic_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"peer_public_key","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"''::text","relname":"user_interface_bindings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"phase","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"flow_settings_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ping_at","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ping_enabled","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"true","relname":"uptime_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ping_error","attnotnull":false,"attnum":18,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ping_loss_pct","attnotnull":false,"attnum":17,"attstorage":"p","default_expression":null,"relname":"uptime_speed_test_runs","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ping_ms","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ping_rtt_ms","attnotnull":false,"attnum":16,"attstorage":"p","default_expression":null,"relname":"uptime_speed_test_runs","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"plan_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_operations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"plan_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"policy","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_connections","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"policy_version","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"mcp_routing_profiles","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"policy_version","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_plans","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"poll_configuration_epoch","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"gen_random_uuid()","relname":"internet_path_settings","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"poll_configuration_epoch","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"gen_random_uuid()","relname":"traffic_settings","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"poll_configuration_epoch","attnotnull":true,"attnum":15,"attstorage":"p","default_expression":"gen_random_uuid()","relname":"uptime_settings","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"polled_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"server_snapshots","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"port","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"443","relname":"servers","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"prefix","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"flow_ip_meta","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"prefix","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'10.255.254.0/24'::text","relname":"traffic_flow_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"probe_filter","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'—'::text","relname":"uptime_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"probe_id","attnotnull":false,"attnum":2,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"probe_id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"uptime_probe_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"probe_interval_sec","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"15","relname":"uptime_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"profile_hash","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_path_checks","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"profile_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservation_slots","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"profile_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"mcp_routing_forward_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"profile_id","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"profile_id","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"profile_version","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"proto","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"smallint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"protocol","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":"'tcp'::text","relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"protocol","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'tcp'::text","relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"provider","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"'local'::text","relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"public_endpoint","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"''::text","relname":"traffic_flow_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"purged_at","attnotnull":false,"attnum":9,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"purpose","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_secrets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"raw_time","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"router_log_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reason","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"mcp_protections","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reason","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_unauthorized_buckets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_ciphertext","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_poll_receipts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_ciphertext","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_payloads","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_settings_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_acks","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_executions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_quarantines","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_receipts","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_payloads","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_quarantines","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_stages","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_proof","attnotnull":false,"attnum":19,"attstorage":"x","default_expression":null,"relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_proof","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_poll_receipts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_proof","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_probe_execution_payloads","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"received_at","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_stages","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"received_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":null,"relname":"router_log_entries","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"recovery_mode","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":"'always'::text","relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"recovery_operation_id","attnotnull":false,"attnum":6,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"recovery_operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"recovery_plan_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"recovery_receipt_id","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"recovery_stability_sec","attnotnull":false,"attnum":12,"attstorage":"p","default_expression":null,"relname":"alert_rules","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"redirect_uri","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"redirect_uris","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_clients","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"renew_before_days","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"30","relname":"certificate_renew_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"request_id","attnotnull":false,"attnum":9,"attstorage":"x","default_expression":null,"relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"request_proof","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"flow_settings_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"requested_at","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"now()","relname":"certificate_issue_jobs","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"requests_retention_days","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"30","relname":"mcp_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reservation_id","attnotnull":false,"attnum":18,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reservation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservation_slots","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reserved_bytes","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"server_poll_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reserved_bytes","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"server_probe_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reserved_files","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"0","relname":"server_probe_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"reserved_rows","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"0","relname":"server_poll_storage_usage","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"resolution_version","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":"0","relname":"alert_outbox","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"resource","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"resources_enabled","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"true","relname":"uptime_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"result","attnotnull":false,"attnum":5,"attstorage":"x","default_expression":null,"relname":"flow_settings_intents","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"result","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_operation_steps","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"result","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_operations","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"result","attnotnull":false,"attnum":8,"attstorage":"x","default_expression":null,"relname":"mcp_requests","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"result","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"flow_write_receipts","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"result_proof","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_poll_acks","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retention_days","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":null,"relname":"server_probe_storage_admissions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retention_days","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":null,"relname":"server_poll_storage_admissions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retention_days","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"14","relname":"internet_path_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retention_days","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"14","relname":"router_log_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retention_days","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"14","relname":"traffic_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retention_days","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"14","relname":"uptime_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retention_hours","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"24","relname":"traffic_flow_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"retry_count","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"alert_outbox","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"revision","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"revoked_at","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"mcp_management_channels","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"revoked_at","attnotnull":false,"attnum":4,"attstorage":"p","default_expression":null,"relname":"mcp_grants","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"revoked_at","attnotnull":false,"attnum":6,"attstorage":"p","default_expression":null,"relname":"mcp_credentials","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"role","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'viewer'::text","relname":"app_users","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ros_version","attnotnull":false,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_snapshots","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"router_id","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"router_log_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"routing_endpoint_epoch","attnotnull":true,"attnum":20,"attstorage":"p","default_expression":"gen_random_uuid()","relname":"servers","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"routing_table","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"'main'::text","relname":"mcp_routing_observations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"routing_table","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"'main'::text","relname":"recursive_routes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rtt_ms","attnotnull":false,"attnum":3,"attstorage":"p","default_expression":null,"relname":"uptime_probe_samples","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rule_chat_id","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"''::text","relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rule_id","attnotnull":false,"attnum":2,"attstorage":"x","default_expression":null,"relname":"alert_history","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rule_id","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_engine_confirm_pending","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rule_id","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"alert_rule_conditions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rule_id","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"alert_rule_targets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rule_name","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"alert_history","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rx_avg_mbps","attnotnull":false,"attnum":15,"attstorage":"p","default_expression":null,"relname":"uptime_speed_test_runs","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rx_bps","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"0","relname":"traffic_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"rx_bytes","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"traffic_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_access_key_id","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":"''::text","relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_bucket","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"''::text","relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_endpoint","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_etag","attnotnull":false,"attnum":11,"attstorage":"x","default_expression":null,"relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_force_path_style","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"true","relname":"backup_storage_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_key","attnotnull":false,"attnum":10,"attstorage":"x","default_expression":null,"relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_prefix","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'mikrotik'::text","relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_region","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":"'us-east-1'::text","relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"s3_secret_access_key","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":"''::text","relname":"backup_storage_settings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"safety_ms","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"60000","relname":"server_probe_storage_admissions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"safety_ms","attnotnull":true,"attnum":15,"attstorage":"p","default_expression":"60000","relname":"server_poll_storage_admissions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sampled_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"internet_path_snapshots","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sampled_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"servers_rest_ping_samples","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sampled_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"uptime_probe_samples","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sampled_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"uptime_resource_samples","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sampled_at","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"traffic_samples","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"scope","attnotnull":false,"attnum":7,"attstorage":"p","default_expression":null,"relname":"recursive_routes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"scope","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"server_poll_storage_usage","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"scope","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"server_probe_storage_usage","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"scope","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"scope_key","attnotnull":true,"attnum":1,"attstorage":"x","default_expression":null,"relname":"alert_engine_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"section","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"config_revisions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sent_at","attnotnull":false,"attnum":10,"attstorage":"p","default_expression":null,"relname":"alert_outbox","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sent_ok","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"true","relname":"alert_history","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":false,"attnum":2,"attstorage":"p","default_expression":null,"relname":"backup_entries","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":false,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_drift","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":false,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_operation_steps","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":false,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_secrets","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_buckets","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_daily_dims","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_daily_facts","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_hour_facts","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_minute_dims","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"flow_minute_stats","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"router_log_collection_state","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"servers_rest_ping_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"traffic_collection_state","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"traffic_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"uptime_resource_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"config_revisions","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"filter_rules","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_management_channels","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"recursive_routes","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"router_log_entries","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"server_snapshots","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"user_interface_bindings","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_id","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"certificate_issue_jobs","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_ids","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'{}'::bigint[]","relname":"mcp_requests","type":"bigint[]"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_name","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"router_log_collection_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_name","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_name","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"router_log_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"server_updated_at","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"traffic_collection_state","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"service","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"flow_daily_facts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"service","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"flow_hour_facts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"severity","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"alert_history","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"severity","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"show_on_dashboard","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"false","relname":"uptime_probes","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"since_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"alert_engine_confirm_pending","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"site","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":"''::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"size_bytes","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"backup_entries","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"slot_phase","attnotnull":true,"attnum":19,"attstorage":"x","default_expression":"'recovery-intent'::text","relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"snapshot","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'[]'::jsonb","relname":"router_log_collection_state","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sort_index","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"alert_rule_conditions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sort_index","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"0","relname":"alert_rule_targets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sort_order","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"0","relname":"uptime_probes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sort_order","attnotnull":true,"attnum":19,"attstorage":"p","default_expression":"0","relname":"uptime_speed_probes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sort_order","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"0","relname":"filter_rules","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sort_order","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"0","relname":"recursive_routes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source","attnotnull":true,"attnum":15,"attstorage":"x","default_expression":null,"relname":"mcp_routing_observations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":"'manual'::text","relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"config_revisions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_configuration_proof","attnotnull":false,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_digest","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_module","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_routing_forward_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_operation_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_operation_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_plan_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_routing_forward_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_plan_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_plan_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_proof","attnotnull":false,"attnum":17,"attstorage":"x","default_expression":null,"relname":"mcp_routing_observations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_proof","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"flow_write_receipts","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_proof","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":null,"relname":"vrf_production_tickets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_receipt_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_server_id","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":null,"relname":"server_probe_executions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_sha256","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_step_index","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_step_index","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_routing_forward_charges","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_step_index","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"mcp_routing_reservations","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_step_index","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_routing_manual_inverse_charges","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"speed_enabled","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"true","relname":"uptime_settings","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"speed_interval_sec","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"60","relname":"uptime_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sqlite_imported_at","attnotnull":false,"attnum":2,"attstorage":"p","default_expression":null,"relname":"data_migration","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sqlite_path","attnotnull":false,"attnum":3,"attstorage":"x","default_expression":null,"relname":"data_migration","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"sqlite_sha256","attnotnull":false,"attnum":4,"attstorage":"x","default_expression":null,"relname":"data_migration","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src","attnotnull":true,"attnum":3,"attstorage":"m","default_expression":null,"relname":"flow_buckets","type":"inet"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_address","attnotnull":false,"attnum":7,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_interface","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"''::text","relname":"uptime_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_interface","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":"''::text","relname":"uptime_speed_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_interface","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_routing_observations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_interface","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"''::text","relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_interface_address","attnotnull":false,"attnum":9,"attstorage":"x","default_expression":null,"relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_port","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"flow_buckets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"uptime_probes","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_server_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"uptime_speed_probes","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"src_server_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"uptime_speed_test_runs","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"stage","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_poll_acks","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"stage","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_poll_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"stage_index","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_stages","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"started_at","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"certificate_issue_jobs","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"started_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"scheduler_runs","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":false,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_oauth_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":11,"attstorage":"x","default_expression":"'issued'::text","relname":"vrf_production_tickets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":12,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":15,"attstorage":"x","default_expression":"'claimed'::text","relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"mcp_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":"'claimed'::text","relname":"flow_settings_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_operation_steps","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_routing_reservation_slots","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'intent'::text","relname":"server_probe_execution_stages","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'pending'::text","relname":"mcp_operations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'{}'::jsonb","relname":"mcp_routing_profiles","type":"jsonb"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":null,"relname":"mcp_plans","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state_proof","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":null,"relname":"mcp_routing_reservations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"state_sealed","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":null,"relname":"mcp_routing_reservations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":19,"attstorage":"x","default_expression":"'done'::text","relname":"uptime_speed_test_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"'queued'::text","relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"'offline'::text","relname":"uptime_resource_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":"'pending'::text","relname":"alert_outbox","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"server_snapshots","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'down'::text","relname":"uptime_probe_samples","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_path_checks","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"scheduler_runs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"traffic_collection_state","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"status","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"step","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":"'queued'::text","relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"step_index","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_operation_checkpoints","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"step_index","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_operation_steps","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"storage","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"'local'::text","relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"storage_state","attnotnull":true,"attnum":19,"attstorage":"x","default_expression":"'full'::text","relname":"server_poll_storage_admissions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"target","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"alert_rule_targets","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"target","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"target","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"uptime_probes","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"target_scope","attnotnull":false,"attnum":8,"attstorage":"p","default_expression":null,"relname":"recursive_routes","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"terminal_digest","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":null,"relname":"server_poll_archives","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ticket_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"vrf_production_tickets","type":"uuid"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"timeout_ms","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"server_probe_execution_stages","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"title","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"events","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"tool","attnotnull":true,"attnum":14,"attstorage":"x","default_expression":null,"relname":"server_probe_executions","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"tool","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_requests","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"top_n","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":"200","relname":"traffic_flow_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"topics","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":null,"relname":"router_log_entries","type":"text[]"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"total_hdd_space","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"0","relname":"uptime_resource_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"total_memory","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"server_snapshots","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"total_memory","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"uptime_resource_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"trust_store","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"'www,api'::text","relname":"certificate_issue_jobs","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"tx_avg_mbps","attnotnull":false,"attnum":14,"attstorage":"p","default_expression":null,"relname":"uptime_speed_test_runs","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"tx_bps","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"0","relname":"traffic_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"tx_bytes","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"traffic_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"type","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"alert_rules","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"type","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"'home-router'::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"ul_mbps","attnotnull":false,"attnum":11,"attstorage":"p","default_expression":null,"relname":"mcp_routing_observations","type":"double precision"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"unique_dst","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"flow_minute_stats","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"unique_src","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"0","relname":"flow_minute_stats","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"update_interval_sec","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"604800","relname":"geoip_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":10,"attstorage":"p","default_expression":"now()","relname":"user_interface_bindings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"now()","relname":"filter_rules","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":11,"attstorage":"p","default_expression":"now()","relname":"geoip_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":"now()","relname":"app_users","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":12,"attstorage":"p","default_expression":"now()","relname":"uptime_probes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":13,"attstorage":"p","default_expression":"now()","relname":"backup_storage_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"now()","relname":"backup_schedule_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":14,"attstorage":"p","default_expression":"now()","relname":"uptime_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":15,"attstorage":"p","default_expression":"now()","relname":"alert_rules","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":15,"attstorage":"p","default_expression":"now()","relname":"recursive_routes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":19,"attstorage":"p","default_expression":"now()","relname":"servers","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":20,"attstorage":"p","default_expression":"now()","relname":"traffic_flow_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":21,"attstorage":"p","default_expression":"now()","relname":"uptime_speed_probes","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"now()","relname":"alert_engine_cursor","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"now()","relname":"alert_engine_prev_live","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"now()","relname":"mcp_owner_revocations","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"alert_telegram_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"evobgp_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"now()","relname":"mcp_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"now()","relname":"acme_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"now()","relname":"alert_groups","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"certificate_renew_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"mcp_connections","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"mcp_routing_profiles","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"now()","relname":"servers_api_ping_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"now()","relname":"internet_path_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"updated_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"now()","relname":"traffic_settings","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"upload_error","attnotnull":false,"attnum":12,"attstorage":"x","default_expression":null,"relname":"backup_entries","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"uptime","attnotnull":false,"attnum":8,"attstorage":"x","default_expression":null,"relname":"server_snapshots","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"uptime_seconds","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"0","relname":"uptime_resource_samples","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"use_ssl","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"true","relname":"servers","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"used","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"mcp_budget_buckets","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"used_bytes","attnotnull":true,"attnum":18,"attstorage":"p","default_expression":"0","relname":"server_poll_storage_admissions","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"used_rows","attnotnull":true,"attnum":17,"attstorage":"p","default_expression":"0","relname":"server_poll_storage_admissions","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"user_id","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"user_interface_bindings","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"username","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":"'admin'::text","relname":"servers","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"verification","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":"'pending'::text","relname":"mcp_operations","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"verified_at","attnotnull":true,"attnum":9,"attstorage":"p","default_expression":"now()","relname":"mcp_management_channels","type":"timestamp with time zone"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"verified_proof","attnotnull":false,"attnum":17,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"verified_sealed","attnotnull":false,"attnum":16,"attstorage":"x","default_expression":null,"relname":"mcp_scoped_recovery_intents","type":"text"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"verify_ssl","attnotnull":true,"attnum":8,"attstorage":"p","default_expression":"false","relname":"servers","type":"boolean"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"version","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"server_poll_archives","type":"smallint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"version","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":"1","relname":"mcp_routing_profiles","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"week_day","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"0","relname":"backup_schedule_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"wg_listen_port","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":"51821","relname":"traffic_flow_settings","type":"integer"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"a","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_audit","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"a","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"router_log_entries","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"d","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"filter_rules","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"d","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"internet_path_snapshots","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"d","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"recursive_routes","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"d","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_snapshots","type":"bigint"},{"acl":null,"attcompression":"","attgenerated":"","attidentity":"d","attname":"id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"servers","type":"bigint"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"domain_names","attnotnull":true,"attnum":7,"attstorage":"x","default_expression":null,"relname":"certificate_issue_jobs","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"payload_json","attnotnull":false,"attnum":10,"attstorage":"x","default_expression":null,"relname":"events","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"payload_json","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":"'{}'::jsonb","relname":"alert_engine_prev_live","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"payload_json","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"internet_path_snapshots","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"payload_json","attnotnull":true,"attnum":8,"attstorage":"x","default_expression":null,"relname":"alert_outbox","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"peers_json","attnotnull":true,"attnum":18,"attstorage":"x","default_expression":"'[]'::jsonb","relname":"traffic_flow_settings","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"raw_interfaces","attnotnull":false,"attnum":13,"attstorage":"x","default_expression":null,"relname":"server_snapshots","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"raw_ip_addresses","attnotnull":false,"attnum":14,"attstorage":"x","default_expression":null,"relname":"server_snapshots","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"report_json","attnotnull":false,"attnum":5,"attstorage":"x","default_expression":null,"relname":"data_migration","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"result_json","attnotnull":false,"attnum":8,"attstorage":"x","default_expression":null,"relname":"scheduler_runs","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"sections_json","attnotnull":true,"attnum":9,"attstorage":"x","default_expression":"'[]'::jsonb","relname":"app_users","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"server_ids_json","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":"'[]'::jsonb","relname":"backup_schedule_settings","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"servers_json","attnotnull":true,"attnum":10,"attstorage":"x","default_expression":"'[]'::jsonb","relname":"app_users","type":"jsonb"},{"acl":null,"attcompression":"l","attgenerated":"","attidentity":"","attname":"wan_uplinks","attnotnull":true,"attnum":16,"attstorage":"x","default_expression":"'[]'::jsonb","relname":"servers","type":"jsonb"}],"constraints":[{"condeferrable":false,"condeferred":false,"conname":"acme_settings_account_private_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL account_private_key","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"acme_settings_cloudflare_api_token_not_null","contype":"n","convalidated":true,"definition":"NOT NULL cloudflare_api_token","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"acme_settings_default_zone_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL default_zone_id","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"acme_settings_directory_url_not_null","contype":"n","convalidated":true,"definition":"NOT NULL directory_url","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"acme_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"acme_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"acme_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"acme_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"acme_settings"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_confirm_pending_payload_hash_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload_hash","relname":"alert_engine_confirm_pending"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_confirm_pending_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (rule_id)","relname":"alert_engine_confirm_pending"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_confirm_pending_rule_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL rule_id","relname":"alert_engine_confirm_pending"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_confirm_pending_since_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL since_at","relname":"alert_engine_confirm_pending"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_cursor_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"alert_engine_cursor"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_cursor_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_engine_cursor"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_cursor_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_engine_cursor"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_cursor_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"alert_engine_cursor"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_prev_live_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((kind = ANY (ARRAY['gre'::text, 'bgp'::text])))","relname":"alert_engine_prev_live"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_prev_live_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL kind","relname":"alert_engine_prev_live"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_prev_live_payload_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload_json","relname":"alert_engine_prev_live"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_prev_live_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (kind)","relname":"alert_engine_prev_live"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_prev_live_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"alert_engine_prev_live"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_state_last_fired_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL last_fired_at","relname":"alert_engine_state"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_state_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (scope_key)","relname":"alert_engine_state"},{"condeferrable":false,"condeferred":false,"conname":"alert_engine_state_scope_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL scope_key","relname":"alert_engine_state"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_combine_mode_check","contype":"c","convalidated":true,"definition":"CHECK ((combine_mode = ANY (ARRAY['any'::text, 'all'::text])))","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_combine_mode_not_null","contype":"n","convalidated":true,"definition":"NOT NULL combine_mode","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL name","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_groups_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"alert_groups"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_fired_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL fired_at","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_message_not_null","contype":"n","convalidated":true,"definition":"NOT NULL message","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_rule_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL rule_name","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_sent_ok_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sent_ok","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_severity_check","contype":"c","convalidated":true,"definition":"CHECK ((severity = ANY (ARRAY['critical'::text, 'warning'::text, 'info'::text])))","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_history_severity_not_null","contype":"n","convalidated":true,"definition":"NOT NULL severity","relname":"alert_history"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_channel_check","contype":"c","convalidated":true,"definition":"CHECK ((channel = 'telegram'::text))","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_channel_not_null","contype":"n","convalidated":true,"definition":"NOT NULL channel","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_dedupe_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dedupe_key","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_max_retries_not_null","contype":"n","convalidated":true,"definition":"NOT NULL max_retries","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_next_attempt_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL next_attempt_at","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_payload_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload_json","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_resolution_version_check","contype":"c","convalidated":true,"definition":"CHECK ((resolution_version >= 0))","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_resolution_version_not_null","contype":"n","convalidated":true,"definition":"NOT NULL resolution_version","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_retry_count_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retry_count","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['pending'::text, 'processing'::text, 'unknown'::text, 'sent'::text, 'failed'::text])))","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_outbox_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"alert_outbox"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_conditions_condition_line_not_null","contype":"n","convalidated":true,"definition":"NOT NULL condition_line","relname":"alert_rule_conditions"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_conditions_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_rule_conditions"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_conditions_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_rule_conditions"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_conditions_rule_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (rule_id) REFERENCES alert_rules(id) ON DELETE CASCADE","relname":"alert_rule_conditions"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_conditions_rule_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL rule_id","relname":"alert_rule_conditions"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_conditions_sort_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sort_index","relname":"alert_rule_conditions"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_targets_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_rule_targets"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_targets_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_rule_targets"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_targets_rule_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (rule_id) REFERENCES alert_rules(id) ON DELETE CASCADE","relname":"alert_rule_targets"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_targets_rule_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL rule_id","relname":"alert_rule_targets"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_targets_sort_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sort_index","relname":"alert_rule_targets"},{"condeferrable":false,"condeferred":false,"conname":"alert_rule_targets_target_not_null","contype":"n","convalidated":true,"definition":"NOT NULL target","relname":"alert_rule_targets"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_condition_not_null","contype":"n","convalidated":true,"definition":"NOT NULL condition","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_cooldown_not_null","contype":"n","convalidated":true,"definition":"NOT NULL cooldown","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_group_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (group_id) REFERENCES alert_groups(id) ON DELETE SET NULL","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL name","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_recovery_mode_check","contype":"c","convalidated":true,"definition":"CHECK ((recovery_mode = ANY (ARRAY['always'::text, 'never'::text, 'conditional'::text])))","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_recovery_mode_not_null","contype":"n","convalidated":true,"definition":"NOT NULL recovery_mode","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_rule_chat_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL rule_chat_id","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_severity_check","contype":"c","convalidated":true,"definition":"CHECK ((severity = ANY (ARRAY['critical'::text, 'warning'::text, 'info'::text])))","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_severity_not_null","contype":"n","convalidated":true,"definition":"NOT NULL severity","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_target_not_null","contype":"n","convalidated":true,"definition":"NOT NULL target","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_type_not_null","contype":"n","convalidated":true,"definition":"NOT NULL type","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_rules_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"alert_rules"},{"condeferrable":false,"condeferred":false,"conname":"alert_telegram_settings_bot_token_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bot_token","relname":"alert_telegram_settings"},{"condeferrable":false,"condeferred":false,"conname":"alert_telegram_settings_chat_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL chat_id","relname":"alert_telegram_settings"},{"condeferrable":false,"condeferred":false,"conname":"alert_telegram_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"alert_telegram_settings"},{"condeferrable":false,"condeferred":false,"conname":"alert_telegram_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"alert_telegram_settings"},{"condeferrable":false,"condeferred":false,"conname":"alert_telegram_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"alert_telegram_settings"},{"condeferrable":false,"condeferred":false,"conname":"alert_telegram_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"alert_telegram_settings"},{"condeferrable":false,"condeferred":false,"conname":"app_users_active_not_null","contype":"n","convalidated":true,"definition":"NOT NULL active","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_avatar_not_null","contype":"n","convalidated":true,"definition":"NOT NULL avatar","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_email_not_null","contype":"n","convalidated":true,"definition":"NOT NULL email","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_login_key","contype":"u","convalidated":true,"definition":"UNIQUE (login)","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_login_not_null","contype":"n","convalidated":true,"definition":"NOT NULL login","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL name","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_role_check","contype":"c","convalidated":true,"definition":"CHECK ((role = ANY (ARRAY['admin'::text, 'operator'::text, 'viewer'::text])))","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_role_not_null","contype":"n","convalidated":true,"definition":"NOT NULL role","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_sections_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sections_json","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_servers_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL servers_json","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"app_users_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"app_users"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_filename_not_null","contype":"n","convalidated":true,"definition":"NOT NULL filename","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((kind = ANY (ARRAY['manual'::text, 'auto'::text])))","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL kind","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE SET NULL","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_server_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_name","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_size_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL size_bytes","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_storage_check","contype":"c","convalidated":true,"definition":"CHECK ((storage = ANY (ARRAY['local'::text, 's3'::text, 'both'::text])))","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_entries_storage_not_null","contype":"n","convalidated":true,"definition":"NOT NULL storage","relname":"backup_entries"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_format_check","contype":"c","convalidated":true,"definition":"CHECK ((format = ANY (ARRAY['rsc'::text, 'backup'::text])))","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_format_not_null","contype":"n","convalidated":true,"definition":"NOT NULL format","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_frequency_check","contype":"c","convalidated":true,"definition":"CHECK ((frequency = ANY (ARRAY['daily'::text, 'weekly'::text, 'monthly'::text])))","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_frequency_not_null","contype":"n","convalidated":true,"definition":"NOT NULL frequency","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_hour_not_null","contype":"n","convalidated":true,"definition":"NOT NULL hour","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_keep_count_not_null","contype":"n","convalidated":true,"definition":"NOT NULL keep_count","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_minute_not_null","contype":"n","convalidated":true,"definition":"NOT NULL minute","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_month_day_not_null","contype":"n","convalidated":true,"definition":"NOT NULL month_day","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_server_ids_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_ids_json","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_schedule_settings_week_day_not_null","contype":"n","convalidated":true,"definition":"NOT NULL week_day","relname":"backup_schedule_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_keep_local_copy_not_null","contype":"n","convalidated":true,"definition":"NOT NULL keep_local_copy","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_provider_check","contype":"c","convalidated":true,"definition":"CHECK ((provider = ANY (ARRAY['local'::text, 's3'::text])))","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_provider_not_null","contype":"n","convalidated":true,"definition":"NOT NULL provider","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_s3_access_key_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL s3_access_key_id","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_s3_bucket_not_null","contype":"n","convalidated":true,"definition":"NOT NULL s3_bucket","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_s3_endpoint_not_null","contype":"n","convalidated":true,"definition":"NOT NULL s3_endpoint","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_s3_force_path_style_not_null","contype":"n","convalidated":true,"definition":"NOT NULL s3_force_path_style","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_s3_prefix_not_null","contype":"n","convalidated":true,"definition":"NOT NULL s3_prefix","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_s3_region_not_null","contype":"n","convalidated":true,"definition":"NOT NULL s3_region","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_s3_secret_access_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL s3_secret_access_key","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"backup_storage_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"backup_storage_settings"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_cert_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL cert_name","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_domain_names_not_null","contype":"n","convalidated":true,"definition":"NOT NULL domain_names","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_key_type_not_null","contype":"n","convalidated":true,"definition":"NOT NULL key_type","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_requested_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL requested_at","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_source_check","contype":"c","convalidated":true,"definition":"CHECK ((source = ANY (ARRAY['manual'::text, 'scheduler'::text])))","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_source_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['queued'::text, 'running'::text, 'done'::text, 'failed'::text])))","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_step_not_null","contype":"n","convalidated":true,"definition":"NOT NULL step","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_issue_jobs_trust_store_not_null","contype":"n","convalidated":true,"definition":"NOT NULL trust_store","relname":"certificate_issue_jobs"},{"condeferrable":false,"condeferred":false,"conname":"certificate_renew_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"certificate_renew_settings"},{"condeferrable":false,"condeferred":false,"conname":"certificate_renew_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"certificate_renew_settings"},{"condeferrable":false,"condeferred":false,"conname":"certificate_renew_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"certificate_renew_settings"},{"condeferrable":false,"condeferred":false,"conname":"certificate_renew_settings_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interval_sec","relname":"certificate_renew_settings"},{"condeferrable":false,"condeferred":false,"conname":"certificate_renew_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"certificate_renew_settings"},{"condeferrable":false,"condeferred":false,"conname":"certificate_renew_settings_renew_before_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL renew_before_days","relname":"certificate_renew_settings"},{"condeferrable":false,"condeferred":false,"conname":"certificate_renew_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"certificate_renew_settings"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_fingerprint_not_null","contype":"n","convalidated":true,"definition":"NOT NULL fingerprint","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_payload_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_section_not_null","contype":"n","convalidated":true,"definition":"NOT NULL section","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"config_revisions_source_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source","relname":"config_revisions"},{"condeferrable":false,"condeferred":false,"conname":"data_migration_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"data_migration"},{"condeferrable":false,"condeferred":false,"conname":"data_migration_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"data_migration"},{"condeferrable":false,"condeferred":false,"conname":"data_migration_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"data_migration"},{"condeferrable":false,"condeferred":false,"conname":"events_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_event_type_not_null","contype":"n","convalidated":true,"definition":"NOT NULL event_type","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_level_check","contype":"c","convalidated":true,"definition":"CHECK ((level = ANY (ARRAY['critical'::text, 'warning'::text, 'info'::text])))","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_level_not_null","contype":"n","convalidated":true,"definition":"NOT NULL level","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_message_not_null","contype":"n","convalidated":true,"definition":"NOT NULL message","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_source_module_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_module","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"events_title_not_null","contype":"n","convalidated":true,"definition":"NOT NULL title","relname":"events"},{"condeferrable":false,"condeferred":false,"conname":"evobgp_settings_api_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL api_key","relname":"evobgp_settings"},{"condeferrable":false,"condeferred":false,"conname":"evobgp_settings_base_url_not_null","contype":"n","convalidated":true,"definition":"NOT NULL base_url","relname":"evobgp_settings"},{"condeferrable":false,"condeferred":false,"conname":"evobgp_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"evobgp_settings"},{"condeferrable":false,"condeferred":false,"conname":"evobgp_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"evobgp_settings"},{"condeferrable":false,"condeferred":false,"conname":"evobgp_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"evobgp_settings"},{"condeferrable":false,"condeferred":false,"conname":"evobgp_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"evobgp_settings"},{"condeferrable":false,"condeferred":false,"conname":"evobgp_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"evobgp_settings"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_action_check","contype":"c","convalidated":true,"definition":"CHECK ((action = ANY (ARRAY['route'::text, 'blackhole'::text])))","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_action_not_null","contype":"n","convalidated":true,"definition":"NOT NULL action","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_community_not_null","contype":"n","convalidated":true,"definition":"NOT NULL community","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_description_not_null","contype":"n","convalidated":true,"definition":"NOT NULL description","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_gateway_not_null","contype":"n","convalidated":true,"definition":"NOT NULL gateway","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_gateway_tunnel_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL gateway_tunnel_id","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_sort_order_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sort_order","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"filter_rules_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"filter_rules"},{"condeferrable":false,"condeferred":false,"conname":"flow_asn_meta_asn_not_null","contype":"n","convalidated":true,"definition":"NOT NULL asn","relname":"flow_asn_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_asn_meta_fetched_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL fetched_at","relname":"flow_asn_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_asn_meta_holder_not_null","contype":"n","convalidated":true,"definition":"NOT NULL holder","relname":"flow_asn_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_asn_meta_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (asn)","relname":"flow_asn_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_bucket_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bucket_at","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bytes","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_dst_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_dst_port_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_port","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_flow_end_ms_not_null","contype":"n","convalidated":true,"definition":"NOT NULL flow_end_ms","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_flow_start_ms_not_null","contype":"n","convalidated":true,"definition":"NOT NULL flow_start_ms","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_in_iface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL in_iface","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_nat_dst_port_not_null","contype":"n","convalidated":true,"definition":"NOT NULL nat_dst_port","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_nat_src_port_not_null","contype":"n","convalidated":true,"definition":"NOT NULL nat_src_port","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_out_iface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL out_iface","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_packets_not_null","contype":"n","convalidated":true,"definition":"NOT NULL packets","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, bucket_at, src, dst, proto, src_port, dst_port, in_iface)","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_proto_not_null","contype":"n","convalidated":true,"definition":"NOT NULL proto","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_src_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_buckets_src_port_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_port","relname":"flow_buckets"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bytes","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_day_not_null","contype":"n","convalidated":true,"definition":"NOT NULL day","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_dim_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dim","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL key","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_packets_not_null","contype":"n","convalidated":true,"definition":"NOT NULL packets","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, day, dim, key)","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_dims_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"flow_daily_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_asn_not_null","contype":"n","convalidated":true,"definition":"NOT NULL asn","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bytes","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_country_not_null","contype":"n","convalidated":true,"definition":"NOT NULL country","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_day_not_null","contype":"n","convalidated":true,"definition":"NOT NULL day","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_iface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL iface","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_packets_not_null","contype":"n","convalidated":true,"definition":"NOT NULL packets","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, day, iface, country, service, asn)","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_daily_facts_service_not_null","contype":"n","convalidated":true,"definition":"NOT NULL service","relname":"flow_daily_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_asn_not_null","contype":"n","convalidated":true,"definition":"NOT NULL asn","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_bucket_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bucket_at","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bytes","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_country_not_null","contype":"n","convalidated":true,"definition":"NOT NULL country","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_iface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL iface","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_packets_not_null","contype":"n","convalidated":true,"definition":"NOT NULL packets","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, bucket_at, iface, country, service, asn)","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_hour_facts_service_not_null","contype":"n","convalidated":true,"definition":"NOT NULL service","relname":"flow_hour_facts"},{"condeferrable":false,"condeferred":false,"conname":"flow_ip_meta_asn_not_null","contype":"n","convalidated":true,"definition":"NOT NULL asn","relname":"flow_ip_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_ip_meta_country_not_null","contype":"n","convalidated":true,"definition":"NOT NULL country","relname":"flow_ip_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_ip_meta_fetched_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL fetched_at","relname":"flow_ip_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_ip_meta_holder_not_null","contype":"n","convalidated":true,"definition":"NOT NULL holder","relname":"flow_ip_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_ip_meta_ok_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ok","relname":"flow_ip_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_ip_meta_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (prefix)","relname":"flow_ip_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_ip_meta_prefix_not_null","contype":"n","convalidated":true,"definition":"NOT NULL prefix","relname":"flow_ip_meta"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_bucket_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bucket_at","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bytes","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_dim_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dim","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL key","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_packets_not_null","contype":"n","convalidated":true,"definition":"NOT NULL packets","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, bucket_at, dim, key)","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_dims_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"flow_minute_dims"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_bucket_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bucket_at","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bytes","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_conversations_not_null","contype":"n","convalidated":true,"definition":"NOT NULL conversations","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_packets_not_null","contype":"n","convalidated":true,"definition":"NOT NULL packets","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, bucket_at)","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_unique_dst_not_null","contype":"n","convalidated":true,"definition":"NOT NULL unique_dst","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_minute_stats_unique_src_not_null","contype":"n","convalidated":true,"definition":"NOT NULL unique_src","relname":"flow_minute_stats"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_phase_check","contype":"c","convalidated":true,"definition":"CHECK ((phase = ANY (ARRAY['configuration'::text, 'listener'::text])))","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_phase_not_null","contype":"n","convalidated":true,"definition":"NOT NULL phase","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id, phase)","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_request_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL request_proof","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_state_check","contype":"c","convalidated":true,"definition":"CHECK ((state = ANY (ARRAY['claimed'::text, 'completed'::text])))","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_settings_intents_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"flow_settings_intents"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_batch_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL batch_id","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_committed_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL committed_at","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((kind = ANY (ARRAY['ingest'::text, 'facts'::text, 'rebuild'::text])))","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL kind","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (batch_id)","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_result_check","contype":"c","convalidated":true,"definition":"CHECK (((jsonb_typeof(result) = 'object'::text) AND (octet_length((result)::text) <= 65536)))","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_result_not_null","contype":"n","convalidated":true,"definition":"NOT NULL result","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_source_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((source_proof ~ '^[a-f0-9]{64}$'::text))","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"flow_write_receipts_source_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_proof","relname":"flow_write_receipts"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_etags_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL etags_json","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_update_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL update_interval_sec","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"geoip_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"geoip_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interval_sec","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_poll_configuration_epoch_not_null","contype":"n","convalidated":true,"definition":"NOT NULL poll_configuration_epoch","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retention_days","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"internet_path_settings"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_snapshots_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"internet_path_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_snapshots_payload_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload_json","relname":"internet_path_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_snapshots_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id, sampled_at)","relname":"internet_path_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"internet_path_snapshots_sampled_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sampled_at","relname":"internet_path_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_ciphertext_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ciphertext","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expires_at","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_filename_not_null","contype":"n","convalidated":true,"definition":"NOT NULL filename","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_media_type_not_null","contype":"n","convalidated":true,"definition":"NOT NULL media_type","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_artifacts_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_artifacts"},{"condeferrable":false,"condeferred":false,"conname":"mcp_audit_action_not_null","contype":"n","convalidated":true,"definition":"NOT NULL action","relname":"mcp_audit"},{"condeferrable":false,"condeferred":false,"conname":"mcp_audit_actor_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL actor_id","relname":"mcp_audit"},{"condeferrable":false,"condeferred":false,"conname":"mcp_audit_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_audit"},{"condeferrable":false,"condeferred":false,"conname":"mcp_audit_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_audit"},{"condeferrable":false,"condeferred":false,"conname":"mcp_audit_payload_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload","relname":"mcp_audit"},{"condeferrable":false,"condeferred":false,"conname":"mcp_audit_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_audit"},{"condeferrable":false,"condeferred":false,"conname":"mcp_budget_buckets_bucket_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bucket","relname":"mcp_budget_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_budget_buckets_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id) ON DELETE CASCADE","relname":"mcp_budget_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_budget_buckets_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_budget_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_budget_buckets_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL kind","relname":"mcp_budget_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_budget_buckets_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (connection_id, kind, bucket)","relname":"mcp_budget_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_budget_buckets_used_check","contype":"c","convalidated":true,"definition":"CHECK ((used >= 0))","relname":"mcp_budget_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_budget_buckets_used_not_null","contype":"n","convalidated":true,"definition":"NOT NULL used","relname":"mcp_budget_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_generation_not_null","contype":"n","convalidated":true,"definition":"NOT NULL generation","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL name","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_owner_claims_issued_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_claims_issued_at","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_owner_permissions_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_permissions","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_owner_valid_until_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_valid_until","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_policy_not_null","contype":"n","convalidated":true,"definition":"NOT NULL policy","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_connections_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"mcp_connections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id) ON DELETE CASCADE","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_digest_key","contype":"u","convalidated":true,"definition":"UNIQUE (digest)","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_digest_not_null","contype":"n","convalidated":true,"definition":"NOT NULL digest","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expires_at","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((kind = ANY (ARRAY['bearer'::text, 'access'::text, 'refresh'::text])))","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL kind","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_credentials_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_credentials"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_actor_id_check","contype":"c","convalidated":true,"definition":"CHECK (((length(actor_id) >= 1) AND (length(actor_id) <= 200)))","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_actor_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL actor_id","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (operation_id) REFERENCES mcp_operations(id)","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_diagnostic_cancellations_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (operation_id)","relname":"mcp_diagnostic_cancellations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_drift_detected_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL detected_at","relname":"mcp_drift"},{"condeferrable":false,"condeferred":false,"conname":"mcp_drift_expected_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expected","relname":"mcp_drift"},{"condeferrable":false,"condeferred":false,"conname":"mcp_drift_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_drift"},{"condeferrable":false,"condeferred":false,"conname":"mcp_drift_observed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL observed","relname":"mcp_drift"},{"condeferrable":false,"condeferred":false,"conname":"mcp_drift_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (operation_id) REFERENCES mcp_operations(id)","relname":"mcp_drift"},{"condeferrable":false,"condeferred":false,"conname":"mcp_drift_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_drift"},{"condeferrable":false,"condeferred":false,"conname":"mcp_drift_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_drift"},{"condeferrable":false,"condeferred":false,"conname":"mcp_grants_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id) ON DELETE CASCADE","relname":"mcp_grants"},{"condeferrable":false,"condeferred":false,"conname":"mcp_grants_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_grants"},{"condeferrable":false,"condeferred":false,"conname":"mcp_grants_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_grants"},{"condeferrable":false,"condeferred":false,"conname":"mcp_grants_payload_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload","relname":"mcp_grants"},{"condeferrable":false,"condeferred":false,"conname":"mcp_grants_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_grants"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_attestation_not_null","contype":"n","convalidated":true,"definition":"NOT NULL attestation","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_config_sealed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL config_sealed","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_created_by_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_by","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expires_at","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_generation_not_null","contype":"n","convalidated":true,"definition":"NOT NULL generation","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_identity_hash_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_hash","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_server_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (server_id)","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_management_channels_verified_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL verified_at","relname":"mcp_management_channels"},{"condeferrable":false,"condeferred":false,"conname":"mcp_manual_inverse_source_step_unique","contype":"u","convalidated":true,"definition":"UNIQUE (source_operation_id, source_step_index)","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_clients_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_oauth_clients"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_clients_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"mcp_oauth_clients"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_clients_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_oauth_clients"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_clients_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL name","relname":"mcp_oauth_clients"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_clients_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_oauth_clients"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_clients_redirect_uris_not_null","contype":"n","convalidated":true,"definition":"NOT NULL redirect_uris","relname":"mcp_oauth_clients"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_challenge_not_null","contype":"n","convalidated":true,"definition":"NOT NULL challenge","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_client_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (client_id) REFERENCES mcp_oauth_clients(id)","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_client_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL client_id","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_code_digest_key","contype":"u","convalidated":true,"definition":"UNIQUE (code_digest)","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expires_at","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_redirect_uri_not_null","contype":"n","convalidated":true,"definition":"NOT NULL redirect_uri","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_resource_not_null","contype":"n","convalidated":true,"definition":"NOT NULL resource","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_oauth_requests_scope_not_null","contype":"n","convalidated":true,"definition":"NOT NULL scope","relname":"mcp_oauth_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_checkpoints_before_sealed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL before_sealed","relname":"mcp_operation_checkpoints"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_checkpoints_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_operation_checkpoints"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_checkpoints_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (operation_id) REFERENCES mcp_operations(id)","relname":"mcp_operation_checkpoints"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_checkpoints_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_operation_checkpoints"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_checkpoints_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (operation_id, step_index)","relname":"mcp_operation_checkpoints"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_checkpoints_step_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL step_index","relname":"mcp_operation_checkpoints"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_steps_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_operation_steps"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_steps_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (operation_id) REFERENCES mcp_operations(id)","relname":"mcp_operation_steps"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_steps_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_operation_steps"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_steps_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (operation_id, step_index)","relname":"mcp_operation_steps"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_steps_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"mcp_operation_steps"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operation_steps_step_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL step_index","relname":"mcp_operation_steps"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_connection_id_idempotency_key_key","contype":"u","convalidated":true,"definition":"UNIQUE (connection_id, idempotency_key)","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_idempotency_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL idempotency_key","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_plan_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (plan_id) REFERENCES mcp_plans(id)","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_plan_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (plan_id)","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL plan_id","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_operations_verification_not_null","contype":"n","convalidated":true,"definition":"NOT NULL verification","relname":"mcp_operations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_owner_revocations_invalid_before_not_null","contype":"n","convalidated":true,"definition":"NOT NULL invalid_before","relname":"mcp_owner_revocations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_owner_revocations_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"mcp_owner_revocations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_owner_revocations_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (owner_id)","relname":"mcp_owner_revocations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_owner_revocations_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"mcp_owner_revocations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_candidate_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL candidate_id","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_evidence_not_null","contype":"n","convalidated":true,"definition":"NOT NULL evidence","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_profile_hash_not_null","contype":"n","convalidated":true,"definition":"NOT NULL profile_hash","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['passed'::text, 'failed'::text, 'unknown'::text])))","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_path_checks_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"mcp_path_checks"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_evidence_not_null","contype":"n","convalidated":true,"definition":"NOT NULL evidence","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expires_at","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_generation_not_null","contype":"n","convalidated":true,"definition":"NOT NULL generation","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_operation_version_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_version","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_payload_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_policy_version_not_null","contype":"n","convalidated":true,"definition":"NOT NULL policy_version","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_plans_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"mcp_plans"},{"condeferrable":false,"condeferred":false,"conname":"mcp_portal_events_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_portal_events"},{"condeferrable":false,"condeferred":false,"conname":"mcp_portal_events_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_portal_events"},{"condeferrable":false,"condeferred":false,"conname":"mcp_portal_events_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"mcp_portal_events"},{"condeferrable":false,"condeferred":false,"conname":"mcp_portal_events_payload_hash_not_null","contype":"n","convalidated":true,"definition":"NOT NULL payload_hash","relname":"mcp_portal_events"},{"condeferrable":false,"condeferred":false,"conname":"mcp_portal_events_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_portal_events"},{"condeferrable":false,"condeferred":false,"conname":"mcp_protections_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_protections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_protections_created_by_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_by","relname":"mcp_protections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_protections_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_protections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_protections_object_ref_not_null","contype":"n","convalidated":true,"definition":"NOT NULL object_ref","relname":"mcp_protections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_protections_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_protections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_protections_reason_not_null","contype":"n","convalidated":true,"definition":"NOT NULL reason","relname":"mcp_protections"},{"condeferrable":false,"condeferred":false,"conname":"mcp_read_leases_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id) ON DELETE CASCADE","relname":"mcp_read_leases"},{"condeferrable":false,"condeferred":false,"conname":"mcp_read_leases_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_read_leases"},{"condeferrable":false,"condeferred":false,"conname":"mcp_read_leases_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expires_at","relname":"mcp_read_leases"},{"condeferrable":false,"condeferred":false,"conname":"mcp_read_leases_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_read_leases"},{"condeferrable":false,"condeferred":false,"conname":"mcp_read_leases_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_read_leases"},{"condeferrable":false,"condeferred":false,"conname":"mcp_recovery_intents_authority_profile_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (authority_profile_id) REFERENCES mcp_routing_profiles(id)","relname":"mcp_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_recovery_intents_authority_profile_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL authority_profile_id","relname":"mcp_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_recovery_intents_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_recovery_intents_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (operation_id) REFERENCES mcp_operations(id)","relname":"mcp_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_recovery_intents_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_recovery_intents_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (operation_id)","relname":"mcp_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_recovery_intents_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"mcp_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id) ON DELETE SET NULL","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_input_not_null","contype":"n","convalidated":true,"definition":"NOT NULL input","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_server_ids_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_ids","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_requests_tool_not_null","contype":"n","convalidated":true,"definition":"NOT NULL tool","relname":"mcp_requests"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_binding_sealed_check","contype":"c","convalidated":true,"definition":"CHECK (((octet_length(binding_sealed) >= 1) AND (octet_length(binding_sealed) <= 262144)))","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_binding_sealed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_sealed","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_charge_units_check","contype":"c","convalidated":true,"definition":"CHECK ((charge_units = 1))","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_charge_units_not_null","contype":"n","convalidated":true,"definition":"NOT NULL charge_units","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (source_operation_id)","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_profile_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL profile_id","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_source_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_operation_id","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_source_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_plan_id","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_source_step_index_check","contype":"c","convalidated":true,"definition":"CHECK ((source_step_index = 0))","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_forward_charges_source_step_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_step_index","relname":"mcp_routing_forward_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charg_recovery_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL recovery_operation_id","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_binding_sealed_check","contype":"c","convalidated":true,"definition":"CHECK (((octet_length(binding_sealed) >= 1) AND (octet_length(binding_sealed) <= 262144)))","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_binding_sealed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_sealed","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_charge_units_check","contype":"c","convalidated":true,"definition":"CHECK ((charge_units = 1))","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_charge_units_not_null","contype":"n","convalidated":true,"definition":"NOT NULL charge_units","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_owner_id_check","contype":"c","convalidated":true,"definition":"CHECK (((octet_length(owner_id) >= 1) AND (octet_length(owner_id) <= 1000)))","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (recovery_operation_id)","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_profile_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL profile_id","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_profile_version_check","contype":"c","convalidated":true,"definition":"CHECK ((profile_version > 0))","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_profile_version_not_null","contype":"n","convalidated":true,"definition":"NOT NULL profile_version","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_recovery_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL recovery_plan_id","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_source_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_operation_id","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_source_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_plan_id","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_source_step_index_check","contype":"c","convalidated":true,"definition":"CHECK ((source_step_index = 0))","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_manual_inverse_charges_source_step_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_step_index","relname":"mcp_routing_manual_inverse_charges"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observation_source_proof","contype":"c","convalidated":true,"definition":"CHECK (((source_proof IS NULL) OR (source_proof ~ '^[a-f0-9]{64}$'::text)))","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_context_verified_not_null","contype":"n","convalidated":true,"definition":"NOT NULL context_verified","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_dl_mbps_check","contype":"c","convalidated":true,"definition":"CHECK (((dl_mbps IS NULL) OR (dl_mbps >= (0)::double precision)))","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_dst_interface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_interface","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_dst_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_server_id","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_family_check","contype":"c","convalidated":true,"definition":"CHECK ((family = ANY (ARRAY['ipv4'::text, 'ipv6'::text])))","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_family_not_null","contype":"n","convalidated":true,"definition":"NOT NULL family","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_loss_pct_check","contype":"c","convalidated":true,"definition":"CHECK (((loss_pct IS NULL) OR ((loss_pct >= (0)::double precision) AND (loss_pct <= (100)::double precision))))","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_ping_ms_check","contype":"c","convalidated":true,"definition":"CHECK (((ping_ms IS NULL) OR (ping_ms >= (0)::double precision)))","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_routing_table_not_null","contype":"n","convalidated":true,"definition":"NOT NULL routing_table","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_source_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_src_interface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_interface","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_src_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_server_id","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_observations_ul_mbps_check","contype":"c","convalidated":true,"definition":"CHECK (((ul_mbps IS NULL) OR (ul_mbps >= (0)::double precision)))","relname":"mcp_routing_observations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_config_not_null","contype":"n","convalidated":true,"definition":"NOT NULL config","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_credential_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (credential_id) REFERENCES mcp_credentials(id)","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_grant_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL grant_expires_at","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_profiles_version_not_null","contype":"n","convalidated":true,"definition":"NOT NULL version","relname":"mcp_routing_profiles"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_lane_check","contype":"c","convalidated":true,"definition":"CHECK ((lane = ANY (ARRAY['forward'::text, 'inverse'::text])))","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_lane_not_null","contype":"n","convalidated":true,"definition":"NOT NULL lane","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (reservation_id, lane)","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_profile_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (profile_id) REFERENCES mcp_routing_profiles(id)","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_profile_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL profile_id","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_reservation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (reservation_id) REFERENCES mcp_routing_reservations(id)","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_reservation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL reservation_id","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_state_check","contype":"c","convalidated":true,"definition":"CHECK ((state = ANY (ARRAY['held'::text, 'spent'::text, 'proven-unused'::text, 'unknown'::text])))","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservation_slots_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"mcp_routing_reservation_slots"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_binding_sealed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_sealed","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_deadline_not_null","contype":"n","convalidated":true,"definition":"NOT NULL deadline","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_profile_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (profile_id) REFERENCES mcp_routing_profiles(id)","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_profile_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL profile_id","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_revision_check","contype":"c","convalidated":true,"definition":"CHECK ((revision >= 1))","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_revision_not_null","contype":"n","convalidated":true,"definition":"NOT NULL revision","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_source_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (source_operation_id) REFERENCES mcp_operations(id)","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_source_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_operation_id","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_source_operation_id_source_step_in_key","contype":"u","convalidated":true,"definition":"UNIQUE (source_operation_id, source_step_index)","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_source_plan_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (source_plan_id) REFERENCES mcp_plans(id)","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_source_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_plan_id","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_source_step_index_check","contype":"c","convalidated":true,"definition":"CHECK ((source_step_index = 0))","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_source_step_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_step_index","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_state_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state_proof","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_routing_reservations_state_sealed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state_sealed","relname":"mcp_routing_reservations"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_authority_profile_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (authority_profile_id) REFERENCES mcp_routing_profiles(id)","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_check","contype":"c","convalidated":true,"definition":"CHECK (((mode = 'manual'::text) OR ((authority_profile_id IS NOT NULL) AND (authority_profile_version IS NOT NULL))))","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_check1","contype":"c","convalidated":true,"definition":"CHECK (((state = 'verified'::text) = ((verified_sealed IS NOT NULL) AND (verified_proof IS NOT NULL))))","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_deadline_not_null","contype":"n","convalidated":true,"definition":"NOT NULL deadline","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_intent_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL intent_proof","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_intent_sealed_not_null","contype":"n","convalidated":true,"definition":"NOT NULL intent_sealed","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_mode_check","contype":"c","convalidated":true,"definition":"CHECK ((mode = ANY (ARRAY['manual'::text, 'reserved'::text])))","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_mode_not_null","contype":"n","convalidated":true,"definition":"NOT NULL mode","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (source_operation_id, source_step_index)","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_recovery_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (recovery_operation_id) REFERENCES mcp_operations(id)","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_recovery_receipt_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (recovery_receipt_id)","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_recovery_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL recovery_receipt_id","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_reservation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (reservation_id) REFERENCES mcp_routing_reservations(id)","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_slot_phase_not_null","contype":"n","convalidated":true,"definition":"NOT NULL slot_phase","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_source_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (source_operation_id) REFERENCES mcp_operations(id)","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_source_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_operation_id","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_source_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_receipt_id","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_source_step_index_check","contype":"c","convalidated":true,"definition":"CHECK ((source_step_index >= 0))","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_source_step_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_step_index","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_state_check","contype":"c","convalidated":true,"definition":"CHECK ((state = ANY (ARRAY['intent'::text, 'verified'::text, 'unknown'::text])))","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_scoped_recovery_intents_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"mcp_secrets_ciphertext_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ciphertext","relname":"mcp_secrets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_secrets_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"mcp_secrets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_secrets_created_by_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_by","relname":"mcp_secrets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_secrets_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_secrets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_secrets_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_secrets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_secrets_purpose_not_null","contype":"n","convalidated":true,"definition":"NOT NULL purpose","relname":"mcp_secrets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_audit_retention_days_check","contype":"c","convalidated":true,"definition":"CHECK (((audit_retention_days >= 30) AND (audit_retention_days <= 3650)))","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_audit_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL audit_retention_days","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_requests_retention_days_check","contype":"c","convalidated":true,"definition":"CHECK (((requests_retention_days >= 1) AND (requests_retention_days <= 365)))","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_requests_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL requests_retention_days","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"mcp_settings"},{"condeferrable":false,"condeferred":false,"conname":"mcp_unauthorized_buckets_attempts_not_null","contype":"n","convalidated":true,"definition":"NOT NULL attempts","relname":"mcp_unauthorized_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_unauthorized_buckets_bucket_not_null","contype":"n","convalidated":true,"definition":"NOT NULL bucket","relname":"mcp_unauthorized_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_unauthorized_buckets_client_hash_not_null","contype":"n","convalidated":true,"definition":"NOT NULL client_hash","relname":"mcp_unauthorized_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_unauthorized_buckets_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (bucket, client_hash, reason)","relname":"mcp_unauthorized_buckets"},{"condeferrable":false,"condeferred":false,"conname":"mcp_unauthorized_buckets_reason_not_null","contype":"n","convalidated":true,"definition":"NOT NULL reason","relname":"mcp_unauthorized_buckets"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_check_gateway_not_null","contype":"n","convalidated":true,"definition":"NOT NULL check_gateway","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_comment_not_null","contype":"n","convalidated":true,"definition":"NOT NULL comment","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_country_not_null","contype":"n","convalidated":true,"definition":"NOT NULL country","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_disabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL disabled","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_distance_not_null","contype":"n","convalidated":true,"definition":"NOT NULL distance","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_dst_address_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_address","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_gateway_not_null","contype":"n","convalidated":true,"definition":"NOT NULL gateway","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_routing_table_not_null","contype":"n","convalidated":true,"definition":"NOT NULL routing_table","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_sort_order_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sort_order","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"recursive_routes_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"recursive_routes"},{"condeferrable":false,"condeferred":false,"conname":"reserved_inverse_evidence_pairs","contype":"c","convalidated":true,"definition":"CHECK ((((inverse_intent_sealed IS NULL) = (inverse_intent_proof IS NULL)) AND ((inverse_configuration_sealed IS NULL) = (inverse_configuration_proof IS NULL)) AND ((inverse_configuration_sealed IS NULL) OR (inverse_intent_sealed IS NOT NULL)) AND ((inverse_intent_sealed IS NULL) OR (mode = 'reserved'::text))))","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"router_log_collection_state_failures_not_null","contype":"n","convalidated":true,"definition":"NOT NULL failures","relname":"router_log_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"router_log_collection_state_generation_not_null","contype":"n","convalidated":true,"definition":"NOT NULL generation","relname":"router_log_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"router_log_collection_state_host_not_null","contype":"n","convalidated":true,"definition":"NOT NULL host","relname":"router_log_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"router_log_collection_state_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id)","relname":"router_log_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"router_log_collection_state_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"router_log_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"router_log_collection_state_server_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_name","relname":"router_log_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"router_log_collection_state_snapshot_not_null","contype":"n","convalidated":true,"definition":"NOT NULL snapshot","relname":"router_log_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_effective_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL effective_at","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_generation_not_null","contype":"n","convalidated":true,"definition":"NOT NULL generation","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_host_not_null","contype":"n","convalidated":true,"definition":"NOT NULL host","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_level_not_null","contype":"n","convalidated":true,"definition":"NOT NULL level","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_message_not_null","contype":"n","convalidated":true,"definition":"NOT NULL message","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_raw_time_not_null","contype":"n","convalidated":true,"definition":"NOT NULL raw_time","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_received_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL received_at","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_router_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL router_id","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_server_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_name","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_entries_topics_not_null","contype":"n","convalidated":true,"definition":"NOT NULL topics","relname":"router_log_entries"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_interval_sec_check","contype":"c","convalidated":true,"definition":"CHECK (((interval_sec >= 5) AND (interval_sec <= 3600)))","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interval_sec","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_retention_days_check","contype":"c","convalidated":true,"definition":"CHECK (((retention_days >= 1) AND (retention_days <= 365)))","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"router_log_settings_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retention_days","relname":"router_log_settings"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_duration_ms_not_null","contype":"n","convalidated":true,"definition":"NOT NULL duration_ms","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_finished_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL finished_at","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_job_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL job_key","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_started_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL started_at","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['ok'::text, 'error'::text])))","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"scheduler_runs_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"scheduler_runs"},{"condeferrable":false,"condeferred":false,"conname":"schema_migrations_applied_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL applied_at","relname":"schema_migrations"},{"condeferrable":false,"condeferred":false,"conname":"schema_migrations_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"schema_migrations"},{"condeferrable":false,"condeferred":false,"conname":"schema_migrations_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"schema_migrations"},{"condeferrable":false,"condeferred":false,"conname":"scoped_reservation_slot_shape","contype":"c","convalidated":true,"definition":"CHECK ((((mode = 'manual'::text) AND (reservation_id IS NULL) AND (slot_phase = 'recovery-intent'::text) AND (source_configuration_proof IS NOT NULL)) OR ((mode = 'reserved'::text) AND (reservation_id IS NOT NULL) AND (((slot_phase = 'reservation-held'::text) AND (source_configuration_proof IS NULL)) OR ((slot_phase = ANY (ARRAY['reservation-bound'::text, 'reserved-attempt'::text])) AND (source_configuration_proof IS NOT NULL))))))","relname":"mcp_scoped_recovery_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_ack_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((ack_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_ack_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ack_proof","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_check","contype":"c","convalidated":true,"definition":"CHECK (((stage ~~ 'native.%'::text) OR (outcome = 'confirmed'::text)))","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_outcome_check","contype":"c","convalidated":true,"definition":"CHECK ((outcome = ANY (ARRAY['confirmed'::text, 'read-failed'::text])))","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_outcome_not_null","contype":"n","convalidated":true,"definition":"NOT NULL outcome","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id, stage)","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_receipt_id_stage_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id, stage) REFERENCES server_poll_intents(receipt_id, stage)","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_result_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((result_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_result_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL result_proof","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_acks_stage_not_null","contype":"n","convalidated":true,"definition":"NOT NULL stage","relname":"server_poll_acks"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_archive_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((archive_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_archive_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL archive_proof","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_check","contype":"c","convalidated":true,"definition":"CHECK ((((ciphertext_bytes >= 1) AND (ciphertext_bytes <= 131072)) AND (ciphertext_bytes = octet_length(ciphertext))))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_ciphertext_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ciphertext_bytes","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_ciphertext_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ciphertext","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_compaction_xid_not_null","contype":"n","convalidated":true,"definition":"NOT NULL compaction_xid","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_compressed_bytes_check","contype":"c","convalidated":true,"definition":"CHECK (((compressed_bytes >= 1) AND (compressed_bytes <= 98048)))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_compressed_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL compressed_bytes","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_context_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((context_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_context_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL context_proof","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_decoded_bytes_check","contype":"c","convalidated":true,"definition":"CHECK (((decoded_bytes >= 1) AND (decoded_bytes <= 16777216)))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_decoded_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL decoded_bytes","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_execution_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (execution_id)","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_execution_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL execution_id","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_original_activity_at_check","contype":"c","convalidated":true,"definition":"CHECK (isfinite(original_activity_at))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_original_activity_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL original_activity_at","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_original_expires_at_check","contype":"c","convalidated":true,"definition":"CHECK (isfinite(original_expires_at))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_original_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL original_expires_at","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_receipt_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id) REFERENCES server_poll_storage_admissions(receipt_id)","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_source_digest_check","contype":"c","convalidated":true,"definition":"CHECK ((source_digest ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_source_digest_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_digest","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_source_sha256_check","contype":"c","convalidated":true,"definition":"CHECK ((source_sha256 ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_source_sha256_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_sha256","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_terminal_digest_check","contype":"c","convalidated":true,"definition":"CHECK ((terminal_digest ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_terminal_digest_not_null","contype":"n","convalidated":true,"definition":"NOT NULL terminal_digest","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_version_check","contype":"c","convalidated":true,"definition":"CHECK ((version = 1))","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_version_not_null","contype":"n","convalidated":true,"definition":"NOT NULL version","relname":"server_poll_archives"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_compaction_claim_shape","contype":"c","convalidated":true,"definition":"CHECK ((((compaction_claim IS NULL) AND (compaction_claimed_at IS NULL) AND (compaction_claim_xid IS NULL)) OR ((compaction_claim IS NOT NULL) AND (compaction_claimed_at IS NOT NULL) AND (compaction_claim_xid IS NOT NULL) AND isfinite(compaction_claimed_at))))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_context_ciphertext_check","contype":"c","convalidated":true,"definition":"CHECK ((octet_length(context_ciphertext) <= 2097152))","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_context_ciphertext_not_null","contype":"n","convalidated":true,"definition":"NOT NULL context_ciphertext","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_context_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((context_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_context_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL context_proof","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_execution_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL execution_id","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_identity_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((identity_kind = ANY (ARRAY['mcp'::text, 'rest'::text])))","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_identity_kind_execution_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (identity_kind, execution_id)","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_identity_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_kind","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_executions_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_before_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((before_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_before_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL before_proof","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_effect_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((effect_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_effect_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL effect_proof","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_intent_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((intent_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_intent_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL intent_proof","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id, stage)","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_receipt_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id) REFERENCES server_poll_executions(receipt_id)","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_stage_check","contype":"c","convalidated":true,"definition":"CHECK ((stage ~ '^(native\\.(identity|resource|interfaces|addresses)|publish\\.[0-9]{1,4})$'::text))","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_intents_stage_not_null","contype":"n","convalidated":true,"definition":"NOT NULL stage","relname":"server_poll_intents"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_context_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((context_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_context_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL context_proof","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_execution_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL execution_id","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_identity_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((identity_kind = ANY (ARRAY['mcp'::text, 'rest'::text])))","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_identity_kind_execution_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (identity_kind, execution_id)","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_identity_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_kind","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_quarantines_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_receipt_ciphertext_check","contype":"c","convalidated":true,"definition":"CHECK ((octet_length(receipt_ciphertext) <= 262144))","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_receipt_ciphertext_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_ciphertext","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_receipt_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id) REFERENCES server_poll_executions(receipt_id)","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_receipt_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((receipt_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_receipts_receipt_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_proof","relname":"server_poll_receipts"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_authority_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL authority_expires_at","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_authority_issued_at_check","contype":"c","convalidated":true,"definition":"CHECK (isfinite(authority_issued_at))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_authority_issued_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL authority_issued_at","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_check","contype":"c","convalidated":true,"definition":"CHECK ((isfinite(authority_expires_at) AND (authority_expires_at > authority_issued_at)))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_check1","contype":"c","convalidated":true,"definition":"CHECK ((((used_rows >= 0) AND (used_rows <= 4019)) AND (used_rows <= ((max_stages * 2) + 3))))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_check2","contype":"c","convalidated":true,"definition":"CHECK ((((identity_kind = 'mcp'::text) AND (connection_id IS NOT NULL)) OR ((identity_kind = 'rest'::text) AND (connection_id IS NULL))))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_check3","contype":"c","convalidated":true,"definition":"CHECK ((((claim_token IS NOT NULL) AND (claim_attempted_at IS NULL)) OR ((claim_token IS NULL) AND (claim_attempted_at IS NOT NULL))))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_check4","contype":"c","convalidated":true,"definition":"CHECK ((gc_not_before >= ((authority_expires_at + make_interval(days => retention_days)) + '00:01:00'::interval)))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_context_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((context_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_context_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL context_proof","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_execution_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL execution_id","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_gc_not_before_check","contype":"c","convalidated":true,"definition":"CHECK (isfinite(gc_not_before))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_gc_not_before_not_null","contype":"n","convalidated":true,"definition":"NOT NULL gc_not_before","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_identity_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((identity_kind = ANY (ARRAY['mcp'::text, 'rest'::text])))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_identity_kind_execution_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (identity_kind, execution_id)","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_identity_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_kind","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_max_stages_check","contype":"c","convalidated":true,"definition":"CHECK (((max_stages >= 8) AND (max_stages <= 2008)))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_max_stages_not_null","contype":"n","convalidated":true,"definition":"NOT NULL max_stages","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_owner_id_check","contype":"c","convalidated":true,"definition":"CHECK ((((length(owner_id) >= 1) AND (length(owner_id) <= 200)) AND (btrim(owner_id) = owner_id) AND (owner_id !~ '[[:cntrl:]]'::text)))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_retention_days_check","contype":"c","convalidated":true,"definition":"CHECK (((retention_days >= 1) AND (retention_days <= 365)))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retention_days","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_safety_ms_check","contype":"c","convalidated":true,"definition":"CHECK ((safety_ms = 60000))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_safety_ms_not_null","contype":"n","convalidated":true,"definition":"NOT NULL safety_ms","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_storage_state_check","contype":"c","convalidated":true,"definition":"CHECK ((storage_state = ANY (ARRAY['full'::text, 'compacted'::text])))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_storage_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL storage_state","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_used_bytes_check","contype":"c","convalidated":true,"definition":"CHECK (((used_bytes >= 0) AND (used_bytes <= 8388608)))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_used_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL used_bytes","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_admissions_used_rows_not_null","contype":"n","convalidated":true,"definition":"NOT NULL used_rows","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_archive_charge","contype":"c","convalidated":true,"definition":"CHECK (((archive_bytes >= compacted_admissions) AND (archive_bytes <= (compacted_admissions * 131072))))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_bytes_charge","contype":"c","convalidated":true,"definition":"CHECK (((reserved_bytes = (((admissions * 8421376) + (compacted_admissions * 32768)) + archive_bytes)) AND (reserved_bytes <=\nCASE\n WHEN (scope = 'global'::text) THEN 1077936128\n ELSE 67371008\nEND)))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_compact_shape","contype":"c","convalidated":true,"definition":"CHECK ((((storage_state = 'full'::text) AND (compact_at IS NULL) AND (archive_bytes IS NULL) AND (original_activity_at IS NULL)) OR ((storage_state = 'compacted'::text) AND (identity_kind = 'mcp'::text) AND (claim_token IS NULL) AND (claim_attempted_at IS NOT NULL) AND (compaction_claim IS NOT NULL) AND (compaction_claimed_at IS NOT NULL) AND (compaction_claim_xid IS NOT NULL) AND (compact_at IS NOT NULL) AND (archive_bytes IS NOT NULL) AND (original_activity_at IS NOT NULL) AND isfinite(compact_at) AND ((archive_bytes >= 1) AND (archive_bytes <= 131072)) AND isfinite(original_activity_at) AND (original_activity_at >= created_at) AND (compact_at > (authority_expires_at + ((safety_ms)::double precision * '00:00:00.001'::interval))))))","relname":"server_poll_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_rows_charge","contype":"c","convalidated":true,"definition":"CHECK (((reserved_rows = ((admissions * 4019) + (compacted_admissions * 2))) AND (reserved_rows <=\nCASE\n WHEN (scope = 'global'::text) THEN 514432\n ELSE 32152\nEND)))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_admissions_check","contype":"c","convalidated":true,"definition":"CHECK (((admissions >= 0) AND (admissions <= 128)))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_admissions_not_null","contype":"n","convalidated":true,"definition":"NOT NULL admissions","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_archive_bytes_check","contype":"c","convalidated":true,"definition":"CHECK ((archive_bytes >= 0))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_archive_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL archive_bytes","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_check2","contype":"c","convalidated":true,"definition":"CHECK (((scope = 'global'::text) OR (admissions <= 8)))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_compacted_admissions_check","contype":"c","convalidated":true,"definition":"CHECK ((compacted_admissions >= 0))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_compacted_admissions_not_null","contype":"n","convalidated":true,"definition":"NOT NULL compacted_admissions","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (scope)","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_reserved_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL reserved_bytes","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_reserved_rows_not_null","contype":"n","convalidated":true,"definition":"NOT NULL reserved_rows","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_scope_check","contype":"c","convalidated":true,"definition":"CHECK (((scope = 'global'::text) OR ((scope ~~ 'owner:%'::text) AND ((length(scope) >= 7) AND (length(scope) <= 206)))))","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_poll_storage_usage_scope_not_null","contype":"n","convalidated":true,"definition":"NOT NULL scope","relname":"server_poll_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_payloads_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_probe_execution_payloads"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_payloads_receipt_ciphertext_check","contype":"c","convalidated":true,"definition":"CHECK (((octet_length(receipt_ciphertext) >= 1) AND (octet_length(receipt_ciphertext) <= 262144)))","relname":"server_probe_execution_payloads"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_payloads_receipt_ciphertext_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_ciphertext","relname":"server_probe_execution_payloads"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_payloads_receipt_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id) REFERENCES server_probe_executions(receipt_id)","relname":"server_probe_execution_payloads"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_payloads_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_execution_payloads"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_payloads_receipt_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((receipt_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_execution_payloads"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_payloads_receipt_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_proof","relname":"server_probe_execution_payloads"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantin_identity_kind_execution_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (identity_kind, execution_id)","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_attempted_stages_check","contype":"c","convalidated":true,"definition":"CHECK (((attempted_stages >= 0) AND (attempted_stages <= 11)))","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_attempted_stages_not_null","contype":"n","convalidated":true,"definition":"NOT NULL attempted_stages","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_check","contype":"c","convalidated":true,"definition":"CHECK (((completed_stages >= 0) AND (completed_stages <= attempted_stages)))","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_completed_stages_not_null","contype":"n","convalidated":true,"definition":"NOT NULL completed_stages","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_error_code_check","contype":"c","convalidated":true,"definition":"CHECK ((error_code ~ '^[A-Z_]{1,64}$'::text))","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_error_code_not_null","contype":"n","convalidated":true,"definition":"NOT NULL error_code","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_execution_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL execution_id","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_identity_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((identity_kind = ANY (ARRAY['mcp'::text, 'rest'::text])))","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_identity_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_kind","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_identity_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((identity_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_identity_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_proof","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_quarantines_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_execution_quarantines"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_check","contype":"c","convalidated":true,"definition":"CHECK ((((state = 'intent'::text) AND (outcome IS NULL) AND (received_at IS NULL)) OR ((state = 'received'::text) AND (outcome IS NOT NULL) AND (received_at IS NOT NULL))))","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((kind = ANY (ARRAY['source-dns-A'::text, 'native-ping'::text, 'native-traceroute'::text, 'native-bandwidth'::text, 'native-route-read'::text, 'manager-dns-A'::text, 'manager-dns-AAAA'::text, 'manager-dns-MX'::text, 'manager-dns-NS'::text, 'manager-dns-TXT'::text, 'manager-dns-CNAME'::text, 'manager-dns-PTR'::text, 'native-mtu-1500'::text, 'native-mtu-1492'::text, 'native-mtu-1480'::text, 'native-mtu-1476'::text, 'native-mtu-1472'::text, 'native-mtu-1468'::text, 'native-mtu-1400'::text, 'native-mtu-1280'::text, 'native-mtu-1024'::text, 'native-mtu-576'::text])))","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL kind","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_outcome_check","contype":"c","convalidated":true,"definition":"CHECK ((outcome = ANY (ARRAY['received'::text, 'source-unavailable'::text])))","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id, stage_index)","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_receipt_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id) REFERENCES server_probe_executions(receipt_id)","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_stage_index_check","contype":"c","convalidated":true,"definition":"CHECK (((stage_index >= 1) AND (stage_index <= 11)))","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_stage_index_not_null","contype":"n","convalidated":true,"definition":"NOT NULL stage_index","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_state_check","contype":"c","convalidated":true,"definition":"CHECK ((state = ANY (ARRAY['intent'::text, 'received'::text])))","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_timeout_ms_check","contype":"c","convalidated":true,"definition":"CHECK (((timeout_ms >= 1) AND (timeout_ms <= 120000)))","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_execution_stages_timeout_ms_not_null","contype":"n","convalidated":true,"definition":"NOT NULL timeout_ms","relname":"server_probe_execution_stages"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_actor_id_check","contype":"c","convalidated":true,"definition":"CHECK (((length(actor_id) >= 1) AND (length(actor_id) <= 200)))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_actor_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL actor_id","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_attempted_stages_check","contype":"c","convalidated":true,"definition":"CHECK (((attempted_stages >= 0) AND (attempted_stages <= 11)))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_authentication_check","contype":"c","convalidated":true,"definition":"CHECK ((authentication = ANY (ARRAY['mcp'::text, 'jwt'::text, 'local'::text])))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_authentication_not_null","contype":"n","convalidated":true,"definition":"NOT NULL authentication","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_authority_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL authority_expires_at","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_authority_issued_at_check","contype":"c","convalidated":true,"definition":"CHECK ((authority_issued_at >= 0))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_authority_issued_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL authority_issued_at","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_check","contype":"c","convalidated":true,"definition":"CHECK (((completed_stages >= 0) AND (completed_stages <= attempted_stages)))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_check1","contype":"c","convalidated":true,"definition":"CHECK ((((identity_kind = 'mcp'::text) AND (authentication = 'mcp'::text) AND (mcp_connection_id IS NOT NULL) AND (mcp_credential_id IS NOT NULL) AND (request_id IS NULL)) OR ((identity_kind = 'rest'::text) AND (authentication = ANY (ARRAY['jwt'::text, 'local'::text])) AND (mcp_connection_id IS NULL) AND (mcp_credential_id IS NULL) AND (request_id IS NOT NULL) AND ((length(request_id) >= 1) AND (length(request_id) <= 200)))))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_check2","contype":"c","convalidated":true,"definition":"CHECK (((tool = 'bandwidth'::text) = (destination_server_id IS NOT NULL)))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_destination_server_id_check","contype":"c","convalidated":true,"definition":"CHECK ((destination_server_id > 0))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_execution_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL execution_id","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_identity_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((identity_kind = ANY (ARRAY['mcp'::text, 'rest'::text])))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_identity_kind_execution_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (identity_kind, execution_id)","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_identity_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_kind","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_mcp_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (mcp_connection_id) REFERENCES mcp_connections(id)","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_mcp_credential_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (mcp_credential_id) REFERENCES mcp_credentials(id)","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_receipt_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((receipt_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_source_server_id_check","contype":"c","convalidated":true,"definition":"CHECK ((source_server_id > 0))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_source_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_server_id","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_state_check","contype":"c","convalidated":true,"definition":"CHECK ((state = ANY (ARRAY['claimed'::text, 'completed'::text, 'failed'::text, 'cancelled'::text])))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_tool_check","contype":"c","convalidated":true,"definition":"CHECK ((tool = ANY (ARRAY['ping'::text, 'traceroute'::text, 'bandwidth'::text, 'dns'::text, 'route'::text, 'mtu'::text])))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_executions_tool_not_null","contype":"n","convalidated":true,"definition":"NOT NULL tool","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_authority_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL authority_expires_at","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_binding_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((binding_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_binding_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL binding_proof","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_check","contype":"c","convalidated":true,"definition":"CHECK ((gc_not_before >= ((authority_expires_at + make_interval(days => retention_days)) + ((safety_ms)::double precision * '00:00:00.001'::interval))))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_check1","contype":"c","convalidated":true,"definition":"CHECK (((claim_token IS NULL) OR ((claim_attempted_at IS NULL) AND (purged_at IS NULL) AND (cancelled_at IS NULL))))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_check2","contype":"c","convalidated":true,"definition":"CHECK (((cancelled_at IS NULL) OR ((claim_token IS NULL) AND (claim_attempted_at IS NOT NULL))))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_execution_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL execution_id","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_gc_not_before_not_null","contype":"n","convalidated":true,"definition":"NOT NULL gc_not_before","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_identity_kind_check","contype":"c","convalidated":true,"definition":"CHECK ((identity_kind = ANY (ARRAY['mcp'::text, 'rest'::text])))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_identity_kind_execution_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (identity_kind, execution_id)","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_identity_kind_not_null","contype":"n","convalidated":true,"definition":"NOT NULL identity_kind","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_owner_id_check","contype":"c","convalidated":true,"definition":"CHECK (((length(owner_id) >= 1) AND (length(owner_id) <= 200)))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_retention_days_check","contype":"c","convalidated":true,"definition":"CHECK (((retention_days >= 1) AND (retention_days <= 365)))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retention_days","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_safety_ms_check","contype":"c","convalidated":true,"definition":"CHECK (((safety_ms >= 60000) AND (safety_ms <= 86400000)))","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_admissions_safety_ms_not_null","contype":"n","convalidated":true,"definition":"NOT NULL safety_ms","relname":"server_probe_storage_admissions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_terminal_state","contype":"c","convalidated":true,"definition":"CHECK ((((state = 'claimed'::text) AND (receipt_proof IS NULL) AND (finished_at IS NULL) AND (attempted_stages IS NULL) AND (completed_stages IS NULL)) OR ((state <> 'claimed'::text) AND (receipt_proof IS NOT NULL) AND (finished_at IS NOT NULL) AND (attempted_stages IS NOT NULL) AND (completed_stages IS NOT NULL))))","relname":"server_probe_executions"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_admissions_check","contype":"c","convalidated":true,"definition":"CHECK ((admissions >= 0))","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_admissions_not_null","contype":"n","convalidated":true,"definition":"NOT NULL admissions","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_check","contype":"c","convalidated":true,"definition":"CHECK (((reserved_files >= 0) AND (reserved_files <= admissions)))","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_check1","contype":"c","convalidated":true,"definition":"CHECK ((reserved_bytes = ((admissions * 32768) + (reserved_files * 262144))))","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (scope)","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_reserved_bytes_check","contype":"c","convalidated":true,"definition":"CHECK ((reserved_bytes >= 0))","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_reserved_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL reserved_bytes","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_reserved_files_not_null","contype":"n","convalidated":true,"definition":"NOT NULL reserved_files","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_scope_check","contype":"c","convalidated":true,"definition":"CHECK (((scope = 'global'::text) OR ((scope ~~ 'owner:%'::text) AND ((length(scope) >= 7) AND (length(scope) <= 206)))))","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_probe_storage_usage_scope_not_null","contype":"n","convalidated":true,"definition":"NOT NULL scope","relname":"server_probe_storage_usage"},{"condeferrable":false,"condeferred":false,"conname":"server_snapshots_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"server_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"server_snapshots_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id, polled_at)","relname":"server_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"server_snapshots_polled_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL polled_at","relname":"server_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"server_snapshots_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"server_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"server_snapshots_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"server_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"server_snapshots_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['online'::text, 'offline'::text])))","relname":"server_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"server_snapshots_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"server_snapshots"},{"condeferrable":false,"condeferred":false,"conname":"servers_api_ping_settings_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"servers_api_ping_settings"},{"condeferrable":false,"condeferred":false,"conname":"servers_api_ping_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"servers_api_ping_settings"},{"condeferrable":false,"condeferred":false,"conname":"servers_api_ping_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"servers_api_ping_settings"},{"condeferrable":false,"condeferred":false,"conname":"servers_api_ping_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"servers_api_ping_settings"},{"condeferrable":false,"condeferred":false,"conname":"servers_api_ping_settings_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interval_sec","relname":"servers_api_ping_settings"},{"condeferrable":false,"condeferred":false,"conname":"servers_api_ping_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"servers_api_ping_settings"},{"condeferrable":false,"condeferred":false,"conname":"servers_api_ping_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"servers_api_ping_settings"},{"condeferrable":false,"condeferred":false,"conname":"servers_asn_not_null","contype":"n","convalidated":true,"definition":"NOT NULL asn","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_comment_not_null","contype":"n","convalidated":true,"definition":"NOT NULL comment","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_country_not_null","contype":"n","convalidated":true,"definition":"NOT NULL country","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_host_not_null","contype":"n","convalidated":true,"definition":"NOT NULL host","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_lan_subnet_not_null","contype":"n","convalidated":true,"definition":"NOT NULL lan_subnet","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_mgmt_tunnel_ip_not_null","contype":"n","convalidated":true,"definition":"NOT NULL mgmt_tunnel_ip","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL name","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_password_not_null","contype":"n","convalidated":true,"definition":"NOT NULL password","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_port_not_null","contype":"n","convalidated":true,"definition":"NOT NULL port","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_rest_ping_samples_ok_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ok","relname":"servers_rest_ping_samples"},{"condeferrable":false,"condeferred":false,"conname":"servers_rest_ping_samples_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, sampled_at)","relname":"servers_rest_ping_samples"},{"condeferrable":false,"condeferred":false,"conname":"servers_rest_ping_samples_sampled_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sampled_at","relname":"servers_rest_ping_samples"},{"condeferrable":false,"condeferred":false,"conname":"servers_rest_ping_samples_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"servers_rest_ping_samples"},{"condeferrable":false,"condeferred":false,"conname":"servers_rest_ping_samples_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"servers_rest_ping_samples"},{"condeferrable":false,"condeferred":false,"conname":"servers_routing_endpoint_epoch_not_null","contype":"n","convalidated":true,"definition":"NOT NULL routing_endpoint_epoch","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_site_not_null","contype":"n","convalidated":true,"definition":"NOT NULL site","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_type_check","contype":"c","convalidated":true,"definition":"CHECK ((type = ANY (ARRAY['jump-host'::text, 'exit-node'::text, 'home-router'::text])))","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_type_not_null","contype":"n","convalidated":true,"definition":"NOT NULL type","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_use_ssl_not_null","contype":"n","convalidated":true,"definition":"NOT NULL use_ssl","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_username_not_null","contype":"n","convalidated":true,"definition":"NOT NULL username","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_verify_ssl_not_null","contype":"n","convalidated":true,"definition":"NOT NULL verify_ssl","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"servers_wan_uplinks_not_null","contype":"n","convalidated":true,"definition":"NOT NULL wan_uplinks","relname":"servers"},{"condeferrable":false,"condeferred":false,"conname":"traffic_collection_state_last_attempt_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL last_attempt_at","relname":"traffic_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"traffic_collection_state_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id)","relname":"traffic_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"traffic_collection_state_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"traffic_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"traffic_collection_state_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"traffic_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"traffic_collection_state_server_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_updated_at","relname":"traffic_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"traffic_collection_state_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['pending'::text, 'ok'::text, 'error'::text])))","relname":"traffic_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"traffic_collection_state_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"traffic_collection_state"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_collector_ip_not_null","contype":"n","convalidated":true,"definition":"NOT NULL collector_ip","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_flow_listen_port_not_null","contype":"n","convalidated":true,"definition":"NOT NULL flow_listen_port","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_host_private_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL host_private_key","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_host_public_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL host_public_key","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_map_service_min_share_pct_not_null","contype":"n","convalidated":true,"definition":"NOT NULL map_service_min_share_pct","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_packets_received_not_null","contype":"n","convalidated":true,"definition":"NOT NULL packets_received","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_peers_json_not_null","contype":"n","convalidated":true,"definition":"NOT NULL peers_json","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_prefix_not_null","contype":"n","convalidated":true,"definition":"NOT NULL prefix","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_public_endpoint_not_null","contype":"n","convalidated":true,"definition":"NOT NULL public_endpoint","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_retention_hours_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retention_hours","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_top_n_not_null","contype":"n","convalidated":true,"definition":"NOT NULL top_n","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_flow_settings_wg_listen_port_not_null","contype":"n","convalidated":true,"definition":"NOT NULL wg_listen_port","relname":"traffic_flow_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_flags_not_null","contype":"n","convalidated":true,"definition":"NOT NULL flags","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_interface_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interface_name","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_peer_public_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL peer_public_key","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, sampled_at, interface_name, peer_public_key)","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_rx_bps_not_null","contype":"n","convalidated":true,"definition":"NOT NULL rx_bps","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_rx_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL rx_bytes","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_sampled_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sampled_at","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_tx_bps_not_null","contype":"n","convalidated":true,"definition":"NOT NULL tx_bps","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_samples_tx_bytes_not_null","contype":"n","convalidated":true,"definition":"NOT NULL tx_bytes","relname":"traffic_samples"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interval_sec","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_poll_configuration_epoch_not_null","contype":"n","convalidated":true,"definition":"NOT NULL poll_configuration_epoch","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retention_days","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"traffic_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"traffic_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_loss_pct_not_null","contype":"n","convalidated":true,"definition":"NOT NULL loss_pct","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_measurement_known_not_null","contype":"n","convalidated":true,"definition":"NOT NULL measurement_known","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (probe_id, sampled_at)","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_probe_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (probe_id) REFERENCES uptime_probes(id) ON DELETE CASCADE","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_probe_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL probe_id","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_sampled_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sampled_at","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['up'::text, 'warn'::text, 'down'::text])))","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probe_samples_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"uptime_probe_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interval_sec","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL name","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_probe_filter_not_null","contype":"n","convalidated":true,"definition":"NOT NULL probe_filter","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_show_on_dashboard_not_null","contype":"n","convalidated":true,"definition":"NOT NULL show_on_dashboard","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_sort_order_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sort_order","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_src_interface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_interface","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_src_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (src_server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_src_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_server_id","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_target_not_null","contype":"n","convalidated":true,"definition":"NOT NULL target","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_probes_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"uptime_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_cpu_load_not_null","contype":"n","convalidated":true,"definition":"NOT NULL cpu_load","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_free_hdd_space_not_null","contype":"n","convalidated":true,"definition":"NOT NULL free_hdd_space","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_free_memory_not_null","contype":"n","convalidated":true,"definition":"NOT NULL free_memory","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (server_id, sampled_at)","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_sampled_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sampled_at","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['online'::text, 'offline'::text])))","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_total_hdd_space_not_null","contype":"n","convalidated":true,"definition":"NOT NULL total_hdd_space","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_total_memory_not_null","contype":"n","convalidated":true,"definition":"NOT NULL total_memory","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_resource_samples_uptime_seconds_not_null","contype":"n","convalidated":true,"definition":"NOT NULL uptime_seconds","relname":"uptime_resource_samples"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_id_check","contype":"c","convalidated":true,"definition":"CHECK ((id = 1))","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interval_sec","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_ping_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ping_enabled","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_poll_configuration_epoch_not_null","contype":"n","convalidated":true,"definition":"NOT NULL poll_configuration_epoch","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_probe_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL probe_interval_sec","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_resources_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL resources_enabled","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_retention_days_not_null","contype":"n","convalidated":true,"definition":"NOT NULL retention_days","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_speed_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL speed_enabled","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_speed_interval_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL speed_interval_sec","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_settings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"uptime_settings"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_direction_check","contype":"c","convalidated":true,"definition":"CHECK ((direction = ANY (ARRAY['transmit'::text, 'receive'::text, 'both'::text])))","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_direction_not_null","contype":"n","convalidated":true,"definition":"NOT NULL direction","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_dst_interface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_interface","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_dst_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (dst_server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_dst_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_server_id","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_duration_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL duration_sec","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_enabled_not_null","contype":"n","convalidated":true,"definition":"NOT NULL enabled","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_last_status_check","contype":"c","convalidated":true,"definition":"CHECK ((last_status = ANY (ARRAY['done'::text, 'error'::text])))","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_protocol_check","contype":"c","convalidated":true,"definition":"CHECK ((protocol = ANY (ARRAY['tcp'::text, 'udp'::text])))","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_protocol_not_null","contype":"n","convalidated":true,"definition":"NOT NULL protocol","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_sort_order_not_null","contype":"n","convalidated":true,"definition":"NOT NULL sort_order","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_src_interface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_interface","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_src_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (src_server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_src_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_server_id","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_probes_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"uptime_speed_probes"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_direction_check","contype":"c","convalidated":true,"definition":"CHECK ((direction = ANY (ARRAY['transmit'::text, 'receive'::text, 'both'::text])))","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_direction_not_null","contype":"n","convalidated":true,"definition":"NOT NULL direction","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_dst_interface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_interface","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_dst_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (dst_server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_dst_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL dst_server_id","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_duration_sec_not_null","contype":"n","convalidated":true,"definition":"NOT NULL duration_sec","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_protocol_check","contype":"c","convalidated":true,"definition":"CHECK ((protocol = ANY (ARRAY['tcp'::text, 'udp'::text])))","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_protocol_not_null","contype":"n","convalidated":true,"definition":"NOT NULL protocol","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_src_interface_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_interface","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_src_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (src_server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_src_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL src_server_id","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_status_check","contype":"c","convalidated":true,"definition":"CHECK ((status = ANY (ARRAY['done'::text, 'error'::text])))","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"uptime_speed_test_runs_status_not_null","contype":"n","convalidated":true,"definition":"NOT NULL status","relname":"uptime_speed_test_runs"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_comment_not_null","contype":"n","convalidated":true,"definition":"NOT NULL comment","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_created_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL created_at","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL id","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_interface_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interface_name","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_interface_type_check","contype":"c","convalidated":true,"definition":"CHECK ((interface_type = ANY (ARRAY['ether'::text, 'bridge'::text, 'vlan'::text, 'vxlan'::text, 'gre'::text, 'wg'::text, 'ipsec'::text, 'other'::text])))","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_interface_type_not_null","contype":"n","convalidated":true,"definition":"NOT NULL interface_type","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_peer_name_not_null","contype":"n","convalidated":true,"definition":"NOT NULL peer_name","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_peer_public_key_not_null","contype":"n","convalidated":true,"definition":"NOT NULL peer_public_key","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (id)","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_server_id_interface_name_peer_publi_key","contype":"u","convalidated":true,"definition":"UNIQUE (server_id, interface_name, peer_public_key)","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_updated_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL updated_at","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_user_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (user_id) REFERENCES app_users(id) ON DELETE CASCADE","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"user_interface_bindings_user_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL user_id","relname":"user_interface_bindings"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_action_id_check","contype":"c","convalidated":true,"definition":"CHECK ((action_id = ANY (ARRAY['ros.vrf.create'::text, 'ros.vrf.update'::text, 'ros.vrf.delete'::text])))","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_action_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL action_id","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_action_version_check","contype":"c","convalidated":true,"definition":"CHECK ((action_version > 0))","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_action_version_not_null","contype":"n","convalidated":true,"definition":"NOT NULL action_version","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_check","contype":"c","convalidated":true,"definition":"CHECK (((expires_at > issued_at) AND (expires_at <= (issued_at + '00:05:00'::interval))))","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_check1","contype":"c","convalidated":true,"definition":"CHECK ((((state = 'issued'::text) AND (claimed_at IS NULL)) OR ((state = 'claimed'::text) AND (claimed_at IS NOT NULL))))","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_ciphertext_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ciphertext","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_digest_check","contype":"c","convalidated":true,"definition":"CHECK ((digest ~ '^[a-f0-9]{64}$'::text))","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_digest_not_null","contype":"n","convalidated":true,"definition":"NOT NULL digest","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_expires_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL expires_at","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_issued_at_not_null","contype":"n","convalidated":true,"definition":"NOT NULL issued_at","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_owner_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL owner_id","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (ticket_id)","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_plan_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (plan_id) REFERENCES mcp_plans(id)","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_plan_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (plan_id)","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL plan_id","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_server_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (server_id) REFERENCES servers(id)","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_server_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL server_id","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_source_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((source_proof ~ '^[a-f0-9]{64}$'::text))","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_source_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_proof","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_state_check","contype":"c","convalidated":true,"definition":"CHECK ((state = ANY (ARRAY['issued'::text, 'claimed'::text])))","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_state_not_null","contype":"n","convalidated":true,"definition":"NOT NULL state","relname":"vrf_production_tickets"},{"condeferrable":false,"condeferred":false,"conname":"vrf_production_tickets_ticket_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL ticket_id","relname":"vrf_production_tickets"},{"condeferrable":true,"condeferred":true,"conname":"server_poll_admission_final_guard","contype":"t","convalidated":true,"definition":"TRIGGER DEFERRABLE INITIALLY DEFERRED","relname":"server_poll_storage_admissions"},{"condeferrable":true,"condeferred":true,"conname":"server_poll_archive_final_guard","contype":"t","convalidated":true,"definition":"TRIGGER DEFERRABLE INITIALLY DEFERRED","relname":"server_poll_archives"},{"condeferrable":true,"condeferred":true,"conname":"server_poll_compact_final_guard","contype":"t","convalidated":true,"definition":"TRIGGER DEFERRABLE INITIALLY DEFERRED","relname":"server_poll_storage_admissions"}],"database":[{"acl":["@mm-role:control@=CTc/@mm-role:control@","@mm-role:capture@=c/@mm-role:control@","@mm-role:operator@=c/@mm-role:control@","@mm-role:publisher@=c/@mm-role:control@","@mm-role:runtime@=c/@mm-role:control@"],"datname":"current-database","owner":"@mm-role:control@"}],"effectiveDatabase":[{"connect":true,"create":false,"rolname":"@mm-role:capture@","temporary":false},{"connect":true,"create":false,"rolname":"@mm-role:operator@","temporary":false},{"connect":true,"create":false,"rolname":"@mm-role:publisher@","temporary":false},{"connect":true,"create":false,"rolname":"@mm-role:runtime@","temporary":false}],"effectiveFunctions":[{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_operator_ddl","rolname":"@mm-role:capture@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_operator_ddl","rolname":"@mm-role:operator@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_operator_ddl","rolname":"@mm-role:publisher@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_operator_ddl","rolname":"@mm-role:runtime@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_require_preadmission","rolname":"@mm-role:capture@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_require_preadmission","rolname":"@mm-role:operator@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_require_preadmission","rolname":"@mm-role:publisher@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_require_preadmission","rolname":"@mm-role:runtime@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_truncate_preadmitted","rolname":"@mm-role:capture@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_truncate_preadmitted","rolname":"@mm-role:publisher@"},{"arguments":"","execute":false,"nspname":"mmapp_control","proname":"candidate_truncate_preadmitted","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"mmapp_control","proname":"candidate_truncate_preadmitted","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_diagnostic_cancellation_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_diagnostic_cancellation_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_diagnostic_cancellation_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_diagnostic_cancellation_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_source","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_source","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_source","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_forward_charge_source","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_source","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_source","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_source","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_routing_manual_inverse_charge_source","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_scoped_recovery_manual_version","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_scoped_recovery_manual_version","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_scoped_recovery_manual_version","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"mcp_scoped_recovery_manual_version","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"mm_routing_endpoint_epoch","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"mm_routing_endpoint_epoch","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"mm_routing_endpoint_epoch","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"mm_routing_endpoint_epoch","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_partition_writer_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_partition_writer_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_partition_writer_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_partition_writer_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_final_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_final_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_final_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_final_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_finish","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_finish","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_finish","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_finish","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_archive_insert_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_compacted_row_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_compacted_row_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_compacted_row_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_compacted_row_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_journal_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_journal_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_journal_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_journal_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_settings_epoch_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_settings_epoch_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_settings_epoch_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_settings_epoch_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_accounting_valid","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_accounting_valid","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_accounting_valid","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_accounting_valid","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_admission_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_admission_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_admission_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_admission_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_meter","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_meter","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_meter","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_poll_storage_row_meter","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_execution_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_execution_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_execution_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_execution_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_quarantine_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_quarantine_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_quarantine_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_quarantine_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_stage_immutable","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_stage_immutable","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_stage_immutable","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_stage_immutable","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_admission_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_admission_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_admission_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_admission_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_anchor_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_anchor_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_anchor_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_anchor_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_payload_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_payload_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_payload_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_payload_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_quarantine_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_quarantine_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_quarantine_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_quarantine_guard","rolname":"@mm-role:runtime@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_stage_guard","rolname":"@mm-role:capture@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_stage_guard","rolname":"@mm-role:operator@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_stage_guard","rolname":"@mm-role:publisher@"},{"arguments":"","execute":true,"nspname":"public","proname":"server_probe_storage_stage_guard","rolname":"@mm-role:runtime@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_compact_allowed","rolname":"@mm-role:capture@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_compact_allowed","rolname":"@mm-role:operator@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_compact_allowed","rolname":"@mm-role:publisher@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_compact_allowed","rolname":"@mm-role:runtime@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_gc_allowed","rolname":"@mm-role:capture@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_gc_allowed","rolname":"@mm-role:operator@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_gc_allowed","rolname":"@mm-role:publisher@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_poll_storage_gc_allowed","rolname":"@mm-role:runtime@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_probe_storage_gc_allowed","rolname":"@mm-role:capture@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_probe_storage_gc_allowed","rolname":"@mm-role:operator@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_probe_storage_gc_allowed","rolname":"@mm-role:publisher@"},{"arguments":"id uuid","execute":true,"nspname":"public","proname":"server_probe_storage_gc_allowed","rolname":"@mm-role:runtime@"},{"arguments":"receipt_uuid uuid","execute":true,"nspname":"public","proname":"server_poll_storage_source_payload","rolname":"@mm-role:capture@"},{"arguments":"receipt_uuid uuid","execute":true,"nspname":"public","proname":"server_poll_storage_source_payload","rolname":"@mm-role:operator@"},{"arguments":"receipt_uuid uuid","execute":true,"nspname":"public","proname":"server_poll_storage_source_payload","rolname":"@mm-role:publisher@"},{"arguments":"receipt_uuid uuid","execute":true,"nspname":"public","proname":"server_poll_storage_source_payload","rolname":"@mm-role:runtime@"}],"effectiveSchemas":[{"create":false,"nspname":"mmapp_control","rolname":"@mm-role:capture@","usage":false},{"create":false,"nspname":"mmapp_control","rolname":"@mm-role:operator@","usage":true},{"create":false,"nspname":"mmapp_control","rolname":"@mm-role:publisher@","usage":true},{"create":false,"nspname":"mmapp_control","rolname":"@mm-role:runtime@","usage":false},{"create":false,"nspname":"public","rolname":"@mm-role:capture@","usage":true},{"create":false,"nspname":"public","rolname":"@mm-role:publisher@","usage":true},{"create":false,"nspname":"public","rolname":"@mm-role:runtime@","usage":true},{"create":true,"nspname":"public","rolname":"@mm-role:operator@","usage":true}],"effectiveSequences":[{"read":false,"relname":"filter_rules_id_seq","rolname":"@mm-role:publisher@","update":false,"usage":false},{"read":false,"relname":"filter_rules_id_seq","rolname":"@mm-role:runtime@","update":false,"usage":false},{"read":false,"relname":"internet_path_snapshots_id_seq","rolname":"@mm-role:publisher@","update":false,"usage":false},{"read":false,"relname":"internet_path_snapshots_id_seq","rolname":"@mm-role:runtime@","update":false,"usage":false},{"read":false,"relname":"mcp_audit_id_seq","rolname":"@mm-role:publisher@","update":false,"usage":false},{"read":false,"relname":"mcp_audit_id_seq","rolname":"@mm-role:runtime@","update":false,"usage":false},{"read":false,"relname":"recursive_routes_id_seq","rolname":"@mm-role:publisher@","update":false,"usage":false},{"read":false,"relname":"recursive_routes_id_seq","rolname":"@mm-role:runtime@","update":false,"usage":false},{"read":false,"relname":"router_log_entries_id_seq","rolname":"@mm-role:publisher@","update":false,"usage":false},{"read":false,"relname":"router_log_entries_id_seq","rolname":"@mm-role:runtime@","update":false,"usage":false},{"read":false,"relname":"server_snapshots_id_seq","rolname":"@mm-role:publisher@","update":false,"usage":false},{"read":false,"relname":"server_snapshots_id_seq","rolname":"@mm-role:runtime@","update":false,"usage":false},{"read":false,"relname":"servers_id_seq","rolname":"@mm-role:publisher@","update":false,"usage":false},{"read":false,"relname":"servers_id_seq","rolname":"@mm-role:runtime@","update":false,"usage":false},{"read":true,"relname":"filter_rules_id_seq","rolname":"@mm-role:capture@","update":false,"usage":false},{"read":true,"relname":"filter_rules_id_seq","rolname":"@mm-role:operator@","update":true,"usage":true},{"read":true,"relname":"internet_path_snapshots_id_seq","rolname":"@mm-role:capture@","update":false,"usage":false},{"read":true,"relname":"internet_path_snapshots_id_seq","rolname":"@mm-role:operator@","update":true,"usage":true},{"read":true,"relname":"mcp_audit_id_seq","rolname":"@mm-role:capture@","update":false,"usage":false},{"read":true,"relname":"mcp_audit_id_seq","rolname":"@mm-role:operator@","update":true,"usage":true},{"read":true,"relname":"recursive_routes_id_seq","rolname":"@mm-role:capture@","update":false,"usage":false},{"read":true,"relname":"recursive_routes_id_seq","rolname":"@mm-role:operator@","update":true,"usage":true},{"read":true,"relname":"router_log_entries_id_seq","rolname":"@mm-role:capture@","update":false,"usage":false},{"read":true,"relname":"router_log_entries_id_seq","rolname":"@mm-role:operator@","update":true,"usage":true},{"read":true,"relname":"server_snapshots_id_seq","rolname":"@mm-role:capture@","update":false,"usage":false},{"read":true,"relname":"server_snapshots_id_seq","rolname":"@mm-role:operator@","update":true,"usage":true},{"read":true,"relname":"servers_id_seq","rolname":"@mm-role:capture@","update":false,"usage":false},{"read":true,"relname":"servers_id_seq","rolname":"@mm-role:operator@","update":true,"usage":true}],"events":[{"evtenabled":"O","evtevent":"ddl_command_start","evtname":"candidate_operator_ddl","evttags":null,"function":"mmapp_control.candidate_operator_ddl()","function_name":"candidate_operator_ddl","function_namespace":"mmapp_control","owner":"@mm-role:control@"}],"functions":[{"acl":["@mm-role:control@=X/@mm-role:control@","@mm-role:operator@=X/@mm-role:control@"],"arguments":"","definition":"CREATE OR REPLACE FUNCTION mmapp_control.candidate_truncate_preadmitted()\n RETURNS trigger\n LANGUAGE plpgsql\n SECURITY DEFINER\n SET search_path TO 'pg_catalog'\nAS $function$\nBEGIN PERFORM mmapp_control.candidate_require_preadmission(); RETURN NULL; END $function$\n","language":"plpgsql","namespace":"mmapp_control","owner":"@mm-role:control@","proconfig":["search_path=pg_catalog"],"prokind":"f","proname":"candidate_truncate_preadmitted","prosecdef":true},{"acl":["@mm-role:control@=X/@mm-role:control@"],"arguments":"","definition":"CREATE OR REPLACE FUNCTION mmapp_control.candidate_operator_ddl()\n RETURNS event_trigger\n LANGUAGE plpgsql\n SECURITY DEFINER\n SET search_path TO 'pg_catalog'\nAS $function$\nBEGIN\n IF SESSION_USER='@mm-role:operator@' THEN PERFORM mmapp_control.candidate_require_preadmission(); END IF;\nEND $function$\n","language":"plpgsql","namespace":"mmapp_control","owner":"@mm-role:control@","proconfig":["search_path=pg_catalog"],"prokind":"f","proname":"candidate_operator_ddl","prosecdef":true},{"acl":["@mm-role:control@=X/@mm-role:control@"],"arguments":"","definition":"CREATE OR REPLACE FUNCTION mmapp_control.candidate_require_preadmission()\n RETURNS void\n LANGUAGE plpgsql\n SECURITY DEFINER\n SET search_path TO 'pg_catalog'\nAS $function$\nBEGIN\n IF NOT EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE pid=pg_catalog.pg_backend_pid()\n AND locktype='advisory' AND classid=0 AND objid=8723110 AND objsubid=1\n AND granted AND mode IN ('ShareLock','ExclusiveLock')) THEN\n RAISE EXCEPTION 'operator SQL requires preheld writer admission' USING ERRCODE='55000';\n END IF;\n PERFORM pg_catalog.pg_advisory_xact_lock_shared(8723110::bigint);\nEND $function$\n","language":"plpgsql","namespace":"mmapp_control","owner":"@mm-role:control@","proconfig":["search_path=pg_catalog"],"prokind":"f","proname":"candidate_require_preadmission","prosecdef":true},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mcp_diagnostic_cancellation_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nDECLARE original_receipt uuid;\nBEGIN\n IF TG_OP='DELETE' THEN\n SELECT a.receipt_id INTO original_receipt FROM server_probe_storage_admissions a\n JOIN mcp_operations o ON o.id=a.execution_id AND o.connection_id=OLD.connection_id\n WHERE a.identity_kind='mcp' AND a.execution_id=OLD.operation_id AND a.owner_id=OLD.actor_id;\n IF FOUND AND server_probe_storage_gc_allowed(original_receipt) THEN RETURN OLD; END IF;\n END IF;\n RAISE EXCEPTION 'diagnostic cancellation lifetime has not elapsed';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mcp_diagnostic_cancellation_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mcp_routing_forward_charge_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nDECLARE retention integer;\nBEGIN\n IF TG_OP<>'DELETE' THEN RAISE EXCEPTION 'immutable forward charge; rewrite/refund forbidden'; END IF;\n SELECT requests_retention_days INTO retention FROM public.mcp_settings WHERE id=1 FOR SHARE;\n IF retention IS NULL OR retention NOT BETWEEN 1 AND 365\n OR OLD.created_at>clock_timestamp()-interval '1 hour'\n OR OLD.created_at>=clock_timestamp()-make_interval(days=>retention)\n OR EXISTS(SELECT 1 FROM public.mcp_operations WHERE id=OLD.source_operation_id AND state IN('pending','running'))\n THEN RAISE EXCEPTION 'forward charge is still retained or unresolved'; END IF;\n RETURN OLD;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mcp_routing_forward_charge_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mcp_routing_forward_charge_source()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nBEGIN\n IF NOT EXISTS(\n SELECT 1 FROM public.mcp_operations o\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\n JOIN public.mcp_routing_profiles r ON r.id=NEW.profile_id AND r.connection_id=o.connection_id\n WHERE o.id=NEW.source_operation_id AND p.id=NEW.source_plan_id\n AND o.connection_id=NEW.connection_id AND o.state='pending' AND p.state='executing'\n AND p.operation_id='routing.community.switch' AND p.operation_version=7\n ) THEN RAISE EXCEPTION 'forward charge differs from actual pending admission source'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mcp_routing_forward_charge_source","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mcp_routing_manual_inverse_charge_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\r\nDECLARE retention integer;\r\nBEGIN\r\n IF TG_OP<>'DELETE' THEN RAISE EXCEPTION 'immutable manual inverse charge; rewrite/refund forbidden'; END IF;\r\n SELECT requests_retention_days INTO retention FROM public.mcp_settings WHERE id=1 FOR SHARE;\r\n IF retention IS NULL OR retention NOT BETWEEN 1 AND 365\r\n OR OLD.created_at>clock_timestamp()-interval '1 hour'\r\n OR OLD.created_at>=clock_timestamp()-make_interval(days=>retention)\r\n OR EXISTS(SELECT 1 FROM public.mcp_operations WHERE id=OLD.recovery_operation_id AND state IN('pending','running'))\r\n THEN RAISE EXCEPTION 'manual inverse charge still retained or unresolved'; END IF;\r\n RETURN OLD;\r\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mcp_routing_manual_inverse_charge_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mcp_routing_manual_inverse_charge_source()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\r\nBEGIN\r\n IF NOT EXISTS(\r\n SELECT 1 FROM public.mcp_operations o\r\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\r\n JOIN public.mcp_routing_profiles r ON r.id=NEW.profile_id AND r.connection_id=o.connection_id\r\n JOIN public.mcp_operations source ON source.id=NEW.source_operation_id AND source.connection_id=o.connection_id\r\n JOIN public.mcp_plans sp ON sp.id=source.plan_id AND sp.connection_id=source.connection_id\r\n JOIN public.mcp_connections c ON c.id=o.connection_id\r\n WHERE o.id=NEW.recovery_operation_id AND p.id=NEW.recovery_plan_id\r\n AND o.connection_id=NEW.connection_id AND c.owner_id=NEW.owner_id\r\n AND r.version=NEW.profile_version AND o.state='pending' AND p.state='executing'\r\n AND p.operation_id='operations.recover' AND p.operation_version=7\r\n AND sp.id=NEW.source_plan_id AND sp.operation_id='routing.community.switch' AND sp.operation_version=7\r\n AND source.state IN('applied','partial')\r\n ) THEN RAISE EXCEPTION 'manual inverse charge differs from pending admission'; END IF;\r\n RETURN NEW;\r\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mcp_routing_manual_inverse_charge_source","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mcp_scoped_recovery_manual_version()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\r\nBEGIN\r\n IF NEW.mode='manual' AND NOT pg_try_advisory_xact_lock(hashtextextended('mcp-runner-admission',0))\r\n THEN RAISE EXCEPTION 'manual intent admission snapshot busy'; END IF;\r\n IF NEW.mode='manual' AND NOT EXISTS(\r\n SELECT 1 FROM public.mcp_operations o\r\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\r\n JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0\r\n JOIN public.mcp_operations source ON source.id=NEW.source_operation_id AND source.connection_id=o.connection_id\r\n WHERE o.id=NEW.recovery_operation_id AND o.state='running'\r\n AND p.state='executing' AND p.operation_id='operations.recover' AND p.operation_version=7\r\n AND NEW.source_step_index=0 AND s.state='intent'\r\n AND s.intended_state->>'kind'='service'\r\n AND s.intended_state->>'service'='routing.community.scoped.recover'\r\n AND s.intended_state->'input'->>'mode'='manual'\r\n AND s.intended_state->'input'->>'sourceOperationId'=NEW.source_operation_id::text\r\n AND s.intended_state->'input'->>'receiptId'=NEW.recovery_receipt_id::text\r\n ) THEN RAISE EXCEPTION 'manual inverse requires current exact running recovery'; END IF;\r\n RETURN NEW;\r\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mcp_scoped_recovery_manual_version","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mm_routing_endpoint_epoch()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nBEGIN\n IF ROW(NEW.host,NEW.port,NEW.username,NEW.password,NEW.use_ssl,NEW.verify_ssl,NEW.enabled)\n IS DISTINCT FROM ROW(OLD.host,OLD.port,OLD.username,OLD.password,OLD.use_ssl,OLD.verify_ssl,OLD.enabled) THEN\n NEW.routing_endpoint_epoch := gen_random_uuid();\n ELSE\n NEW.routing_endpoint_epoch := OLD.routing_endpoint_epoch;\n END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mm_routing_endpoint_epoch","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_partition_writer_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog'\nAS $function$ BEGIN PERFORM pg_advisory_xact_lock_shared(8723110::bigint); IF TG_OP='DELETE' THEN RETURN OLD; END IF; RETURN NEW; END; $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog"],"prokind":"f","proname":"server_partition_writer_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_archive_final_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nDECLARE id uuid; a public.server_poll_storage_admissions%ROWTYPE;\nBEGIN\n id:=CASE WHEN TG_OP='DELETE' THEN OLD.receipt_id ELSE NEW.receipt_id END;\n SELECT * INTO a FROM public.server_poll_storage_admissions WHERE receipt_id=id;\n IF FOUND AND a.storage_state='compacted' AND(EXISTS(SELECT 1 FROM public.server_poll_executions WHERE receipt_id=id)\n OR EXISTS(SELECT 1 FROM public.server_poll_intents WHERE receipt_id=id) OR EXISTS(SELECT 1 FROM public.server_poll_acks WHERE receipt_id=id)\n OR EXISTS(SELECT 1 FROM public.server_poll_receipts WHERE receipt_id=id) OR EXISTS(SELECT 1 FROM public.server_poll_quarantines WHERE receipt_id=id))\n THEN RAISE EXCEPTION 'compacted poll retained incomplete original children'; END IF;\n IF NOT public.server_poll_storage_accounting_valid() THEN RAISE EXCEPTION 'poll final archive or exact ledger mismatch'; END IF;\n RETURN NULL;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_archive_final_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_archive_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$ BEGIN\n IF TG_OP='DELETE' AND public.server_poll_storage_gc_allowed(OLD.receipt_id) THEN RETURN OLD; END IF;\n RAISE EXCEPTION 'poll archive is immutable until original full retention GC';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_archive_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_archive_insert_finish()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$ BEGIN\n DELETE FROM public.server_poll_acks WHERE receipt_id=NEW.receipt_id;\n DELETE FROM public.server_poll_intents WHERE receipt_id=NEW.receipt_id;\n DELETE FROM public.server_poll_receipts WHERE receipt_id=NEW.receipt_id;\n DELETE FROM public.server_poll_executions WHERE receipt_id=NEW.receipt_id;\n UPDATE public.server_poll_storage_admissions SET storage_state='compacted',compact_at=NEW.created_at,archive_bytes=NEW.ciphertext_bytes,original_activity_at=NEW.original_activity_at WHERE receipt_id=NEW.receipt_id AND storage_state='full';\n IF NOT FOUND THEN RAISE EXCEPTION 'poll admission did not transition with archive'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_archive_insert_finish","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_archive_insert_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nDECLARE a public.server_poll_storage_admissions%ROWTYPE; payload jsonb; activity timestamptz;\nBEGIN\n payload:=public.server_poll_storage_source_payload(NEW.receipt_id);\n IF payload IS NULL OR NOT public.server_poll_storage_accounting_valid() THEN RAISE EXCEPTION 'poll source is not eligible for terminal archive'; END IF;\n SELECT * INTO a FROM public.server_poll_storage_admissions WHERE receipt_id=NEW.receipt_id FOR UPDATE;\n SELECT GREATEST(a.authority_expires_at,a.created_at,a.claim_attempted_at,(payload#>>'{terminal,operation,finished_at}')::timestamptz,\n (payload#>>'{terminal,plan,expires_at}')::timestamptz,(payload#>>'{terminal,step,finished_at}')::timestamptz,\n (payload#>>'{journal,execution,created_at}')::timestamptz,(payload#>>'{journal,receipt,created_at}')::timestamptz,\n (SELECT max((v->>'created_at')::timestamptz) FROM jsonb_array_elements(payload#>'{journal,intents}') v),\n (SELECT max((v->>'created_at')::timestamptz) FROM jsonb_array_elements(payload#>'{journal,acks}') v)) INTO activity;\n IF a.compaction_claim IS NULL OR a.compaction_claimed_at IS NULL OR NOT isfinite(a.compaction_claimed_at)\n OR a.compaction_claim_xid IS NULL OR a.compaction_claim_xid=pg_current_xact_id()\n OR NEW.version<>1 OR NEW.execution_id<>a.execution_id OR NEW.connection_id<>a.connection_id OR NEW.owner_id<>a.owner_id\n OR NEW.binding_proof<>a.binding_proof OR NEW.context_proof<>a.context_proof OR NEW.original_expires_at<>a.authority_expires_at\n OR NEW.original_activity_at<>activity OR NEW.decoded_bytes<>octet_length(payload::text)\n OR NEW.source_sha256<>encode(sha256(convert_to(payload::text,'UTF8')),'hex')\n OR NEW.ciphertext_bytes<>octet_length(NEW.ciphertext) OR NEW.ciphertext_bytes>131072\n OR NEW.ciphertext !~ '^[A-Za-z0-9+/]+={0,2}$'\n OR replace(encode(decode(NEW.ciphertext,'base64'),'base64'),E'\\n','')<>NEW.ciphertext\n OR octet_length(decode(NEW.ciphertext,'base64'))<>NEW.compressed_bytes+28\n THEN RAISE EXCEPTION 'poll archive does not match the entire immutable source'; END IF;\n NEW.created_at:=clock_timestamp(); NEW.compaction_xid:=pg_current_xact_id();\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC","search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_archive_insert_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_compacted_row_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$ BEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0));\n IF EXISTS(SELECT 1 FROM public.server_poll_storage_admissions WHERE receipt_id=NEW.receipt_id AND storage_state='compacted') THEN RAISE EXCEPTION 'compacted poll journal cannot reopen'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_compacted_row_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_journal_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$ BEGIN\n IF TG_OP='DELETE' AND EXISTS(SELECT 1 FROM public.server_poll_archives r JOIN public.server_poll_storage_admissions a USING(receipt_id)\n WHERE r.receipt_id=OLD.receipt_id AND r.compaction_xid=pg_current_xact_id() AND a.storage_state='full') THEN RETURN OLD; END IF;\n IF TG_OP='DELETE' AND public.server_poll_storage_gc_allowed(OLD.receipt_id) THEN RETURN OLD; END IF;\n RAISE EXCEPTION 'server poll journal is immutable outside complete archive or original lifetime GC';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_journal_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_settings_epoch_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog'\nAS $function$\r\nBEGIN\r\n IF TG_TABLE_SCHEMA <> 'public' OR TG_TABLE_NAME NOT IN\r\n ('traffic_settings', 'uptime_settings', 'internet_path_settings')\r\n OR TG_LEVEL <> 'ROW' OR TG_WHEN <> 'BEFORE' THEN\r\n RAISE EXCEPTION 'unexpected poll settings epoch trigger target';\r\n END IF;\r\n IF TG_OP = 'INSERT' THEN\r\n -- Never accept an epoch supplied by an importer or application caller.\r\n NEW.poll_configuration_epoch := pg_catalog.gen_random_uuid();\r\n ELSIF TG_OP = 'UPDATE' THEN\r\n -- Every unknown/future field is configuration by default. Only these four\r\n -- current collector fields are excluded; created_at stays significant.\r\n IF (pg_catalog.to_jsonb(NEW) - ARRAY['poll_configuration_epoch',\r\n 'last_collected_at', 'last_duration_ms', 'last_error', 'updated_at'])\r\n IS DISTINCT FROM\r\n (pg_catalog.to_jsonb(OLD) - ARRAY['poll_configuration_epoch',\r\n 'last_collected_at', 'last_duration_ms', 'last_error', 'updated_at']) THEN\r\n NEW.poll_configuration_epoch := pg_catalog.gen_random_uuid();\r\n ELSE\r\n NEW.poll_configuration_epoch := OLD.poll_configuration_epoch;\r\n END IF;\r\n ELSE\r\n RAISE EXCEPTION 'unexpected poll settings epoch trigger operation';\r\n END IF;\r\n RETURN NEW;\r\nEND;\r\n$function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog"],"prokind":"f","proname":"server_poll_settings_epoch_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_storage_accounting_valid()\n RETURNS boolean\n LANGUAGE sql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\n WITH charges AS (\n SELECT owner_id,CASE WHEN storage_state='full' THEN 1 ELSE 0 END::bigint AS heavy,\n CASE WHEN storage_state='compacted' THEN 1 ELSE 0 END::bigint AS compact,\n COALESCE(archive_bytes,0)::bigint AS archives,\n CASE WHEN storage_state='full' THEN 4019 ELSE 2 END::bigint AS rows,\n CASE WHEN storage_state='full' THEN 8421376 ELSE 32768+archive_bytes END::bigint AS bytes\n FROM public.server_poll_storage_admissions\n ), expected AS (\n SELECT 'global' AS scope,COALESCE(sum(heavy),0) AS heavy,COALESCE(sum(compact),0) AS compact,COALESCE(sum(archives),0) AS archives,COALESCE(sum(rows),0) AS rows,COALESCE(sum(bytes),0) AS bytes FROM charges\n UNION ALL SELECT 'owner:'||owner_id,sum(heavy),sum(compact),sum(archives),sum(rows),sum(bytes) FROM charges GROUP BY owner_id\n ) SELECT NOT EXISTS(SELECT 1 FROM public.server_poll_storage_usage u FULL JOIN expected e USING(scope)\n WHERE u.scope IS NULL OR u.admissions<>COALESCE(e.heavy,0) OR u.compacted_admissions<>COALESCE(e.compact,0)\n OR u.archive_bytes<>COALESCE(e.archives,0) OR u.reserved_rows<>COALESCE(e.rows,0) OR u.reserved_bytes<>COALESCE(e.bytes,0))\n AND NOT EXISTS(SELECT 1 FROM public.server_poll_storage_admissions a LEFT JOIN public.server_poll_archives r USING(receipt_id)\n WHERE (a.storage_state='full' AND r.receipt_id IS NOT NULL) OR(a.storage_state='compacted' AND(r.receipt_id IS NULL OR a.archive_bytes<>r.ciphertext_bytes)))\n$function$\n","language":"sql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_storage_accounting_valid","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_storage_admission_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nDECLARE retention integer; charged integer; r public.server_poll_archives%ROWTYPE;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0));\n IF TG_OP='DELETE' THEN\n IF NOT public.server_poll_storage_gc_allowed(OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_executions WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_intents WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_acks WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_receipts WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_quarantines WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_archives WHERE receipt_id=OLD.receipt_id) THEN RAISE EXCEPTION 'poll lifetime or children prevent admission deletion'; END IF;\n UPDATE public.server_poll_storage_usage SET admissions=admissions-CASE WHEN OLD.storage_state='full' THEN 1 ELSE 0 END,\n compacted_admissions=compacted_admissions-CASE WHEN OLD.storage_state='compacted' THEN 1 ELSE 0 END,\n archive_bytes=archive_bytes-COALESCE(OLD.archive_bytes,0),\n reserved_rows=reserved_rows-CASE WHEN OLD.storage_state='full' THEN 4019 ELSE 2 END,\n reserved_bytes=reserved_bytes-CASE WHEN OLD.storage_state='full' THEN 8421376 ELSE 32768+OLD.archive_bytes END\n WHERE scope IN('global','owner:'||OLD.owner_id);\n GET DIAGNOSTICS charged=ROW_COUNT; IF charged<>2 THEN RAISE EXCEPTION 'poll quota refund mismatch'; END IF; RETURN OLD;\n END IF;\n IF TG_OP='UPDATE' THEN\n -- This durable one-shot marker is written and ACKed in its OWN transaction\n -- before archive construction. A committed/unknown claim never resets and\n -- the worker never retries destructive work against an already claimed full\n -- source. Claim time is maintenance metadata, not renewed execution activity.\n IF OLD.storage_state='full' AND NEW.storage_state='full' AND OLD.compaction_claim IS NULL AND OLD.compaction_claimed_at IS NULL AND OLD.compaction_claim_xid IS NULL\n AND NEW.compaction_claim IS NOT NULL AND NEW.compaction_claim_xid IS NULL\n AND(to_jsonb(OLD)-ARRAY['compaction_claim','compaction_claimed_at','compaction_claim_xid'])=(to_jsonb(NEW)-ARRAY['compaction_claim','compaction_claimed_at','compaction_claim_xid'])\n AND public.server_poll_storage_compact_allowed(OLD.receipt_id)\n THEN NEW.compaction_claimed_at:=clock_timestamp(); NEW.compaction_claim_xid:=pg_current_xact_id(); RETURN NEW; END IF;\n IF(to_jsonb(OLD)-ARRAY['claim_token','claim_attempted_at','used_rows','used_bytes','storage_state','compact_at','archive_bytes','original_activity_at']) IS DISTINCT FROM\n (to_jsonb(NEW)-ARRAY['claim_token','claim_attempted_at','used_rows','used_bytes','storage_state','compact_at','archive_bytes','original_activity_at']) THEN RAISE EXCEPTION 'poll admission identity is immutable'; END IF;\n IF OLD.storage_state<>'full' THEN RAISE EXCEPTION 'compacted poll identity cannot reopen or change'; END IF;\n IF NEW.storage_state='compacted' THEN\n SELECT * INTO r FROM public.server_poll_archives WHERE receipt_id=OLD.receipt_id AND compaction_xid=pg_current_xact_id();\n IF NOT FOUND OR NEW.used_rows<>OLD.used_rows OR NEW.used_bytes<>OLD.used_bytes OR NEW.claim_token IS DISTINCT FROM OLD.claim_token\n OR NEW.claim_attempted_at IS DISTINCT FROM OLD.claim_attempted_at OR NEW.archive_bytes<>r.ciphertext_bytes OR NEW.original_activity_at<>r.original_activity_at\n OR EXISTS(SELECT 1 FROM public.server_poll_executions WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_intents WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_acks WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_receipts WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_quarantines WHERE receipt_id=OLD.receipt_id)\n THEN RAISE EXCEPTION 'poll compaction requires the complete current-transaction archive'; END IF;\n NEW.compact_at:=r.created_at;\n UPDATE public.server_poll_storage_usage SET admissions=admissions-1,compacted_admissions=compacted_admissions+1,\n archive_bytes=archive_bytes+r.ciphertext_bytes,reserved_rows=reserved_rows-4019+2,reserved_bytes=reserved_bytes-8421376+32768+r.ciphertext_bytes\n WHERE scope IN('global','owner:'||OLD.owner_id) AND admissions>=1;\n GET DIAGNOSTICS charged=ROW_COUNT; IF charged<>2 THEN RAISE EXCEPTION 'poll compaction charge mismatch'; END IF;\n RETURN NEW;\n END IF;\n IF NEW.compact_at IS NOT NULL OR NEW.archive_bytes IS NOT NULL OR NEW.original_activity_at IS NOT NULL THEN RAISE EXCEPTION 'full poll cannot carry archive metadata'; END IF;\n IF OLD.claim_token IS NOT NULL AND NEW.claim_token IS NULL AND OLD.claim_attempted_at IS NULL AND NEW.claim_attempted_at IS NOT NULL\n AND NEW.used_rows=OLD.used_rows AND NEW.used_bytes=OLD.used_bytes AND OLD.authority_expires_at>clock_timestamp()\n THEN NEW.claim_attempted_at:=clock_timestamp(); RETURN NEW; END IF;\n IF OLD.claim_token IS NOT DISTINCT FROM NEW.claim_token AND OLD.claim_attempted_at IS NOT DISTINCT FROM NEW.claim_attempted_at\n AND NEW.used_rows=OLD.used_rows+1 AND NEW.used_bytes>OLD.used_bytes THEN RETURN NEW; END IF;\n RAISE EXCEPTION 'poll admission cannot refresh, reopen or refund itself';\n END IF;\n IF NEW.storage_state<>'full' OR NEW.compact_at IS NOT NULL OR NEW.archive_bytes IS NOT NULL OR NEW.original_activity_at IS NOT NULL\n OR NEW.compaction_claim IS NOT NULL OR NEW.compaction_claimed_at IS NOT NULL OR NEW.compaction_claim_xid IS NOT NULL\n OR NOT public.server_poll_storage_accounting_valid() THEN RAISE EXCEPTION 'new poll requires full reservation and exact ledger'; END IF;\n SELECT requests_retention_days INTO retention FROM public.mcp_settings WHERE id=1;\n IF retention IS NULL OR retention NOT BETWEEN 1 AND 365 OR NEW.authority_issued_at>clock_timestamp() OR NEW.authority_expires_at<=clock_timestamp()\n OR NEW.claim_token IS NULL OR NEW.claim_attempted_at IS NOT NULL OR NEW.used_rows<>0 OR NEW.used_bytes<>0 THEN RAISE EXCEPTION 'new poll admission needs original live authority'; END IF;\n IF EXISTS(SELECT 1 FROM public.server_poll_storage_admissions WHERE receipt_id=NEW.receipt_id OR(identity_kind=NEW.identity_kind AND execution_id=NEW.execution_id)) THEN RAISE EXCEPTION 'poll identity already spent'; END IF;\n NEW.created_at:=clock_timestamp(); NEW.retention_days:=retention; NEW.safety_ms:=60000;\n NEW.gc_not_before:=NEW.authority_expires_at+make_interval(days=>retention)+interval '60 seconds';\n INSERT INTO public.server_poll_storage_usage(scope) VALUES('owner:'||NEW.owner_id) ON CONFLICT DO NOTHING;\n IF(SELECT count(*) FROM public.server_poll_storage_usage WHERE scope IN('global','owner:'||NEW.owner_id)\n AND admissions2 THEN RAISE EXCEPTION 'poll admission quota exhausted'; END IF;\n UPDATE public.server_poll_storage_usage SET admissions=admissions+1,reserved_rows=reserved_rows+4019,reserved_bytes=reserved_bytes+8421376 WHERE scope IN('global','owner:'||NEW.owner_id);\n GET DIAGNOSTICS charged=ROW_COUNT; IF charged<>2 THEN RAISE EXCEPTION 'poll admission quota unavailable'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC","search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_storage_admission_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_storage_row_guard()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nDECLARE a server_poll_storage_admissions%ROWTYPE; n integer;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0));\n SELECT * INTO a FROM server_poll_storage_admissions WHERE receipt_id=NEW.receipt_id FOR UPDATE;\n IF NOT FOUND THEN RAISE EXCEPTION 'poll journal needs a committed admission'; END IF;\n IF TG_TABLE_NAME='server_poll_quarantines' THEN\n IF NEW.identity_kind<>a.identity_kind OR NEW.execution_id<>a.execution_id OR NEW.context_proof<>a.context_proof\n THEN RAISE EXCEPTION 'poll quarantine admission mismatch'; END IF;\n ELSE\n IF a.claim_token IS NOT NULL OR a.claim_attempted_at IS NULL OR a.authority_expires_at<=clock_timestamp()\n THEN RAISE EXCEPTION 'poll admission is not consumed and live'; END IF;\n END IF;\n IF TG_TABLE_NAME='server_poll_executions' THEN\n IF NEW.identity_kind<>a.identity_kind OR NEW.execution_id<>a.execution_id OR NEW.context_proof<>a.context_proof OR NEW.binding_proof<>a.binding_proof\n THEN RAISE EXCEPTION 'poll anchor admission mismatch'; END IF;\n ELSIF TG_TABLE_NAME IN('server_poll_intents','server_poll_acks') THEN\n IF NEW.stage LIKE 'publish.%' AND substring(NEW.stage FROM 9)::integer>=a.max_stages-4 THEN RAISE EXCEPTION 'poll stage exceeds private reservation'; END IF;\n IF TG_TABLE_NAME='server_poll_intents' THEN SELECT count(*) INTO n FROM server_poll_intents WHERE receipt_id=NEW.receipt_id;\n ELSE SELECT count(*) INTO n FROM server_poll_acks WHERE receipt_id=NEW.receipt_id; END IF;\n IF n>=a.max_stages THEN RAISE EXCEPTION 'poll stage row budget exhausted'; END IF;\n END IF;\n IF TG_TABLE_NAME<>'server_poll_quarantines' AND EXISTS(SELECT 1 FROM server_poll_quarantines WHERE receipt_id=NEW.receipt_id)\n THEN RAISE EXCEPTION 'poll identity quarantined'; END IF;\n NEW.created_at:=clock_timestamp();\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_poll_storage_row_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_storage_row_meter()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nDECLARE cost bigint; n integer;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0));\n cost:=pg_column_size(NEW);\n UPDATE server_poll_storage_admissions SET used_rows=used_rows+1,used_bytes=used_bytes+cost\n WHERE receipt_id=NEW.receipt_id AND used_rows1 THEN RAISE EXCEPTION 'poll journal byte or row budget exhausted'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_poll_storage_row_meter","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_execution_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$ BEGIN\n IF TG_OP='DELETE' THEN\n IF server_probe_storage_gc_allowed(OLD.receipt_id) THEN RETURN OLD; END IF;\n RAISE EXCEPTION 'probe execution lifetime has not elapsed';\n END IF;\n IF OLD.state<>'claimed' OR (to_jsonb(OLD)-ARRAY['state','attempted_stages','completed_stages','receipt_proof','finished_at']) IS DISTINCT FROM\n (to_jsonb(NEW)-ARRAY['state','attempted_stages','completed_stages','receipt_proof','finished_at']) OR NEW.state='claimed'\n THEN RAISE EXCEPTION 'probe execution identity or terminal receipt is immutable'; END IF;\n IF EXISTS(SELECT 1 FROM server_probe_execution_quarantines WHERE receipt_id=OLD.receipt_id)\n THEN RAISE EXCEPTION 'probe execution quarantined'; END IF;\n RETURN NEW; END $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_execution_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_quarantine_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$ BEGIN\n IF TG_OP='DELETE' AND server_probe_storage_gc_allowed(OLD.receipt_id) THEN RETURN OLD; END IF;\n RAISE EXCEPTION 'probe quarantine remains immutable'; END $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_quarantine_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_stage_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$ BEGIN\n IF TG_OP='DELETE' THEN\n IF server_probe_storage_gc_allowed(OLD.receipt_id) THEN RETURN OLD; END IF;\n RAISE EXCEPTION 'probe stage lifetime has not elapsed';\n END IF;\n PERFORM receipt_id FROM server_probe_executions WHERE receipt_id=NEW.receipt_id AND state='claimed' FOR UPDATE;\n IF NOT FOUND OR EXISTS(SELECT 1 FROM server_probe_execution_quarantines WHERE receipt_id=NEW.receipt_id) THEN RAISE EXCEPTION 'probe attempt is not executable'; END IF;\n IF TG_OP='INSERT' AND (NEW.stage_index<>(SELECT COALESCE(max(stage_index),0)+1 FROM server_probe_execution_stages WHERE receipt_id=NEW.receipt_id)\n OR EXISTS(SELECT 1 FROM server_probe_execution_stages WHERE receipt_id=NEW.receipt_id AND state<>'received'))\n THEN RAISE EXCEPTION 'probe stages must be serial and contiguous'; END IF;\n IF TG_OP='UPDATE' AND (OLD.state<>'intent' OR NEW.state<>'received' OR (to_jsonb(OLD)-ARRAY['state','outcome','received_at']) IS DISTINCT FROM (to_jsonb(NEW)-ARRAY['state','outcome','received_at']))\n THEN RAISE EXCEPTION 'probe stage terminal state is immutable'; END IF;\n RETURN NEW; END $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_stage_immutable","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_storage_admission_guard()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nDECLARE current_retention integer; activity timestamptz;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0));\n IF TG_OP='DELETE' THEN\n IF server_probe_storage_gc_allowed(OLD.receipt_id) THEN RETURN OLD; END IF;\n RAISE EXCEPTION 'probe admission lifetime has not elapsed';\n END IF;\n SELECT requests_retention_days INTO current_retention FROM mcp_settings WHERE id=1;\n IF current_retention IS NULL OR current_retention NOT BETWEEN 1 AND 365 THEN RAISE EXCEPTION 'probe retention unavailable'; END IF;\n IF TG_OP='INSERT' THEN\n IF NEW.purged_at IS NOT NULL OR (NEW.cancelled_at IS NULL AND\n (NEW.authority_expires_at<=clock_timestamp() OR NEW.claim_token IS NULL OR NEW.claim_attempted_at IS NOT NULL))\n OR (NEW.cancelled_at IS NOT NULL AND (NEW.claim_token IS NOT NULL OR NEW.claim_attempted_at IS NULL))\n THEN RAISE EXCEPTION 'invalid fresh probe admission'; END IF;\n IF NEW.cancelled_at IS NOT NULL THEN NEW.cancelled_at:=clock_timestamp(); NEW.claim_attempted_at:=NEW.cancelled_at; END IF;\n NEW.retention_days:=current_retention; RETURN NEW;\n END IF;\n IF (to_jsonb(OLD)-ARRAY['claim_token','claim_attempted_at','purged_at','cancelled_at']) IS DISTINCT FROM\n (to_jsonb(NEW)-ARRAY['claim_token','claim_attempted_at','purged_at','cancelled_at'])\n THEN RAISE EXCEPTION 'immutable probe admission identity'; END IF;\n IF OLD.cancelled_at IS DISTINCT FROM NEW.cancelled_at THEN\n IF OLD.cancelled_at IS NULL AND NEW.cancelled_at IS NOT NULL AND NEW.claim_token IS NULL\n AND NEW.purged_at IS NOT DISTINCT FROM OLD.purged_at AND NEW.claim_attempted_at IS NOT NULL\n AND (OLD.claim_attempted_at IS NULL OR NEW.claim_attempted_at IS NOT DISTINCT FROM OLD.claim_attempted_at)\n THEN NEW.cancelled_at:=clock_timestamp(); IF OLD.claim_attempted_at IS NULL THEN NEW.claim_attempted_at:=NEW.cancelled_at; END IF; RETURN NEW; END IF;\n RAISE EXCEPTION 'probe cancellation is irreversible';\n END IF;\n IF OLD.purged_at IS NULL AND NEW.purged_at IS NULL AND OLD.claim_token IS NOT NULL AND NEW.claim_token IS NULL\n AND OLD.cancelled_at IS NULL AND OLD.claim_attempted_at IS NULL AND NEW.claim_attempted_at IS NOT NULL AND OLD.authority_expires_at>clock_timestamp()\n THEN RETURN NEW; END IF;\n IF OLD.purged_at IS NULL AND NEW.purged_at IS NOT NULL AND NEW.claim_token IS NULL\n AND NEW.claim_attempted_at IS NOT DISTINCT FROM OLD.claim_attempted_at THEN\n SELECT GREATEST(OLD.authority_expires_at,COALESCE(e.finished_at,OLD.authority_expires_at),COALESCE(q.created_at,OLD.authority_expires_at)) INTO activity\n FROM (SELECT 1) x LEFT JOIN server_probe_executions e ON e.receipt_id=OLD.receipt_id\n LEFT JOIN server_probe_execution_quarantines q ON q.receipt_id=OLD.receipt_id;\n IF activityGREATEST(OLD.retention_days,current_retention)) THEN RETURN NEW; END IF;\n END IF;\n RAISE EXCEPTION 'probe admission transition refused';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_storage_admission_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_storage_anchor_guard()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nDECLARE a server_probe_storage_admissions%ROWTYPE;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0));\n SELECT * INTO a FROM server_probe_storage_admissions WHERE receipt_id=NEW.receipt_id FOR UPDATE;\n IF NOT FOUND OR a.owner_id<>NEW.actor_id OR a.identity_kind<>NEW.identity_kind OR a.execution_id<>NEW.execution_id\n OR a.binding_proof<>NEW.binding_proof OR a.authority_expires_at<>NEW.authority_expires_at\n OR a.purged_at IS NOT NULL OR a.cancelled_at IS NOT NULL OR a.claim_token IS NOT NULL OR a.claim_attempted_at IS NULL OR a.authority_expires_at<=clock_timestamp()\n THEN RAISE EXCEPTION 'probe anchor has no live consumed admission'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_storage_anchor_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_storage_payload_guard()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$\nDECLARE a server_probe_storage_admissions%ROWTYPE; e server_probe_executions%ROWTYPE;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0));\n IF TG_OP='UPDATE' THEN RAISE EXCEPTION 'probe payload is immutable'; END IF;\n IF TG_OP='DELETE' THEN\n PERFORM receipt_id FROM server_probe_storage_admissions WHERE receipt_id=OLD.receipt_id AND purged_at IS NOT NULL FOR UPDATE;\n IF NOT FOUND AND NOT server_probe_storage_gc_allowed(OLD.receipt_id) THEN RAISE EXCEPTION 'probe payload is not purged'; END IF;\n RETURN OLD;\n END IF;\n SELECT * INTO a FROM server_probe_storage_admissions WHERE receipt_id=NEW.receipt_id FOR UPDATE;\n IF NOT FOUND OR a.purged_at IS NOT NULL OR a.cancelled_at IS NOT NULL OR a.claim_token IS NOT NULL OR a.claim_attempted_at IS NULL OR a.authority_expires_at<=clock_timestamp()\n THEN RAISE EXCEPTION 'probe payload has no live admission'; END IF;\n SELECT * INTO e FROM server_probe_executions WHERE receipt_id=NEW.receipt_id FOR UPDATE;\n IF NOT FOUND OR e.state='claimed' OR e.receipt_proof<>NEW.receipt_proof OR e.actor_id<>a.owner_id OR e.binding_proof<>a.binding_proof\n OR e.identity_kind<>a.identity_kind OR e.execution_id<>a.execution_id OR EXISTS(SELECT 1 FROM server_probe_execution_quarantines q\n WHERE q.receipt_id=NEW.receipt_id OR(q.identity_kind=a.identity_kind AND q.execution_id=a.execution_id))\n THEN RAISE EXCEPTION 'probe payload terminal identity mismatch or quarantine'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_storage_payload_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_storage_quarantine_guard()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$ BEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0));\n PERFORM receipt_id FROM server_probe_storage_admissions WHERE receipt_id=NEW.receipt_id AND identity_kind=NEW.identity_kind\n AND execution_id=NEW.execution_id AND binding_proof=NEW.binding_proof FOR UPDATE;\n IF NOT FOUND THEN RAISE EXCEPTION 'probe quarantine has no committed matching admission'; END IF;\n RETURN NEW; END $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_storage_quarantine_guard","prosecdef":false},{"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_storage_stage_guard()\n RETURNS trigger\n LANGUAGE plpgsql\nAS $function$ BEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0));\n PERFORM receipt_id FROM server_probe_storage_admissions WHERE receipt_id=NEW.receipt_id AND purged_at IS NULL AND cancelled_at IS NULL\n AND claim_token IS NULL AND claim_attempted_at IS NOT NULL AND authority_expires_at>clock_timestamp() FOR UPDATE;\n IF NOT FOUND THEN RAISE EXCEPTION 'probe stage has no live admission'; END IF;\n RETURN NEW; END $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"server_probe_storage_stage_guard","prosecdef":false},{"acl":null,"arguments":"id uuid","definition":"CREATE OR REPLACE FUNCTION public.server_poll_storage_compact_allowed(id uuid)\n RETURNS boolean\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nDECLARE a public.server_poll_storage_admissions%ROWTYPE; o public.mcp_operations%ROWTYPE; p public.mcp_plans%ROWTYPE; s public.mcp_operation_steps%ROWTYPE; n integer; pubs integer; activity timestamptz;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0));\n SELECT * INTO a FROM public.server_poll_storage_admissions WHERE receipt_id=id FOR UPDATE;\n IF NOT FOUND OR a.storage_state<>'full' OR a.identity_kind<>'mcp' OR a.claim_token IS NOT NULL OR a.claim_attempted_at IS NULL\n OR a.authority_expires_at+a.safety_ms*interval '1 millisecond'>=clock_timestamp()\n OR EXISTS(SELECT 1 FROM public.server_poll_quarantines WHERE receipt_id=id OR(identity_kind=a.identity_kind AND execution_id=a.execution_id))\n THEN RETURN false; END IF;\n IF NOT pg_try_advisory_xact_lock(hashtextextended('mcp-operation:'||a.execution_id::text,0)) THEN RETURN false; END IF;\n IF NOT pg_try_advisory_xact_lock(hashtextextended('server-poll-execution-v1:mcp:'||a.execution_id::text,0)) THEN RETURN false; END IF;\n IF NOT pg_try_advisory_xact_lock(hashtextextended('server-poll-receipt-v1:'||a.receipt_id::text,0)) THEN RETURN false; END IF;\n BEGIN\n SELECT * INTO o FROM public.mcp_operations WHERE mcp_operations.id=a.execution_id AND connection_id=a.connection_id FOR UPDATE NOWAIT;\n IF NOT FOUND OR o.state<>'applied' OR o.verification<>'passed' OR o.error_code IS NOT NULL OR o.finished_at IS NULL THEN RETURN false; END IF;\n SELECT * INTO p FROM public.mcp_plans WHERE mcp_plans.id=o.plan_id AND connection_id=a.connection_id FOR UPDATE NOWAIT;\n IF NOT FOUND OR p.operation_id<>'servers.poll' OR p.operation_version<>1 OR p.state<>'completed' OR p.approved_by IS NULL OR p.approved_at IS NULL\n OR p.created_at>p.approved_at OR p.approved_at>o.created_at OR o.created_at>=LEAST(p.expires_at,a.authority_expires_at) THEN RETURN false; END IF;\n SELECT * INTO s FROM public.mcp_operation_steps WHERE operation_id=o.id AND step_index=0 FOR UPDATE NOWAIT;\n IF NOT FOUND OR s.state<>'verified' OR s.server_id IS NOT NULL OR s.result IS NULL OR s.finished_at IS NULL\n OR(SELECT count(*) FROM public.mcp_operation_steps WHERE operation_id=o.id)<>1\n OR NOT EXISTS(SELECT 1 FROM public.mcp_connections c WHERE c.id=a.connection_id AND c.owner_id=a.owner_id)\n OR s.intended_state->>'kind' IS DISTINCT FROM 'service' OR s.intended_state->>'service' IS DISTINCT FROM 'servers.poll'\n OR s.intended_state#>>'{input,receiptId}' IS DISTINCT FROM id::text\n OR s.result->>'operationId' IS DISTINCT FROM o.id::text OR s.result->>'receiptId' IS DISTINCT FROM id::text\n OR s.result->'nativeReads' IS DISTINCT FROM '4'::jsonb\n OR s.result->'complete' IS DISTINCT FROM 'true'::jsonb OR s.result->'maintenanceComplete' IS DISTINCT FROM 'true'::jsonb\n OR s.result->'configurationVerified' IS DISTINCT FROM 'true'::jsonb OR s.result->'automaticReplay' IS DISTINCT FROM 'false'::jsonb\n THEN RETURN false; END IF;\n EXCEPTION WHEN lock_not_available THEN RETURN false; END;\n IF NOT EXISTS(SELECT 1 FROM public.server_poll_executions e WHERE e.receipt_id=id AND e.identity_kind=a.identity_kind AND e.execution_id=a.execution_id AND e.binding_proof=a.binding_proof AND e.context_proof=a.context_proof)\n OR NOT EXISTS(SELECT 1 FROM public.server_poll_receipts r WHERE r.receipt_id=id AND r.created_atpubs+4 OR pubs NOT IN(a.max_stages-6,a.max_stages-4) OR a.used_rows<>2*n+2\n OR(SELECT count(*) FROM public.server_poll_acks WHERE receipt_id=id)<>n\n OR s.result->'confirmedPublications' IS DISTINCT FROM to_jsonb(pubs)\n OR s.result->'completedPublications' IS DISTINCT FROM to_jsonb(pubs)\n OR EXISTS(SELECT 1 FROM public.server_poll_acks WHERE receipt_id=id AND outcome<>'confirmed')\n OR EXISTS(SELECT 1 FROM public.server_poll_acks WHERE receipt_id=id AND created_at>=a.authority_expires_at)\n OR EXISTS(SELECT 1 FROM unnest(ARRAY['native.identity','native.resource','native.interfaces','native.addresses']) wanted(stage)\n WHERE NOT EXISTS(SELECT 1 FROM public.server_poll_intents i JOIN public.server_poll_acks k USING(receipt_id,stage) WHERE i.receipt_id=id AND i.stage=wanted.stage AND k.outcome='confirmed'))\n OR EXISTS(SELECT 1 FROM generate_series(0,pubs-1) wanted(stage) WHERE NOT EXISTS(SELECT 1 FROM public.server_poll_intents i JOIN public.server_poll_acks k USING(receipt_id,stage) WHERE i.receipt_id=id AND i.stage='publish.'||wanted.stage::text AND k.outcome='confirmed'))\n THEN RETURN false; END IF;\n SELECT GREATEST(a.authority_expires_at,a.created_at,a.claim_attempted_at,o.finished_at,p.expires_at,s.finished_at,\n (SELECT created_at FROM public.server_poll_executions WHERE receipt_id=id),(SELECT max(created_at) FROM public.server_poll_intents WHERE receipt_id=id),\n (SELECT max(created_at) FROM public.server_poll_acks WHERE receipt_id=id),(SELECT created_at FROM public.server_poll_receipts WHERE receipt_id=id)) INTO activity;\n RETURN isfinite(activity) AND activity+a.safety_ms*interval '1 millisecond'=clock_timestamp() OR a.gc_not_before>=clock_timestamp() THEN RETURN false; END IF;\n SELECT requests_retention_days INTO retention FROM public.mcp_settings WHERE mcp_settings.id=1 FOR SHARE;\n IF retention IS NULL OR retention NOT BETWEEN 1 AND 365 THEN RETURN false; END IF;\n terminal_at:=a.created_at;\n IF a.identity_kind='mcp' THEN\n IF NOT pg_try_advisory_xact_lock(hashtextextended('mcp-operation:'||a.execution_id::text,0)) THEN RETURN false; END IF;\n BEGIN\n SELECT GREATEST(o.finished_at,p.expires_at) INTO terminal_at FROM public.mcp_operations o\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id JOIN public.mcp_connections c ON c.id=o.connection_id\n WHERE o.id=a.execution_id AND o.connection_id=a.connection_id AND c.owner_id=a.owner_id AND p.operation_id='servers.poll'\n AND o.state IN('applied','failed','unknown','rejected','partial','cancelled') AND o.finished_at IS NOT NULL FOR UPDATE OF o,p NOWAIT;\n IF NOT FOUND OR terminal_at IS NULL THEN RETURN false; END IF;\n EXCEPTION WHEN lock_not_available THEN RETURN false; END;\n END IF;\n IF a.storage_state='compacted' THEN activity:=GREATEST(a.original_activity_at,a.authority_expires_at,terminal_at);\n ELSE\n SELECT GREATEST(a.authority_expires_at,a.created_at,COALESCE(a.claim_attempted_at,a.created_at),terminal_at,\n COALESCE((SELECT created_at FROM public.server_poll_executions WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT max(created_at) FROM public.server_poll_intents WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT max(created_at) FROM public.server_poll_acks WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT created_at FROM public.server_poll_receipts WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT created_at FROM public.server_poll_quarantines WHERE receipt_id=id),a.created_at)) INTO activity;\n END IF;\n RETURN isfinite(activity) AND activity+make_interval(days=>GREATEST(a.retention_days,retention))+a.safety_ms*interval '1 millisecond'=clock_timestamp() OR a.authority_expires_at>=clock_timestamp() THEN RETURN false; END IF;\n SELECT settings.requests_retention_days INTO current_retention FROM mcp_settings settings WHERE settings.id=1;\n IF current_retention IS NULL OR current_retention NOT BETWEEN 1 AND 365 THEN RETURN false; END IF;\n terminal_at:=a.created_at;\n IF a.identity_kind='mcp' THEN\n SELECT GREATEST(o.finished_at,p.expires_at,COALESCE(cancellation.created_at,o.finished_at)) INTO terminal_at\n FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\n JOIN mcp_connections owner_connection ON owner_connection.id=o.connection_id\n LEFT JOIN mcp_diagnostic_cancellations cancellation ON cancellation.operation_id=o.id\n WHERE o.id=a.execution_id AND owner_connection.owner_id=a.owner_id AND p.operation_id='servers.probes.run'\n AND o.state IN ('applied','failed','unknown','rejected','partial','cancelled') AND o.finished_at IS NOT NULL\n AND (cancellation.operation_id IS NULL OR(cancellation.connection_id=o.connection_id AND cancellation.actor_id=a.owner_id))\n FOR UPDATE OF o,p,owner_connection;\n IF NOT FOUND OR terminal_at IS NULL THEN RETURN false; END IF;\n END IF;\n SELECT GREATEST(a.created_at,a.authority_expires_at,terminal_at,COALESCE(a.cancelled_at,a.created_at),\n COALESCE((SELECT finished_at FROM server_probe_executions WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT created_at FROM server_probe_execution_quarantines WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT max(GREATEST(created_at,COALESCE(received_at,created_at))) FROM server_probe_execution_stages WHERE receipt_id=id),a.created_at)) INTO activity;\n RETURN activity+make_interval(days=>GREATEST(a.retention_days,current_retention))+a.safety_ms*interval '1 millisecond'16777216 THEN RETURN NULL; END IF;\n RETURN payload;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC","search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_storage_source_payload","prosecdef":false}],"identities":[{"attidentity":"a","attname":"id","column_number":1,"relname":"mcp_audit","sequence_name":"public.mcp_audit_id_seq"},{"attidentity":"a","attname":"id","column_number":1,"relname":"router_log_entries","sequence_name":"public.router_log_entries_id_seq"},{"attidentity":"d","attname":"id","column_number":1,"relname":"filter_rules","sequence_name":"public.filter_rules_id_seq"},{"attidentity":"d","attname":"id","column_number":1,"relname":"internet_path_snapshots","sequence_name":"public.internet_path_snapshots_id_seq"},{"attidentity":"d","attname":"id","column_number":1,"relname":"recursive_routes","sequence_name":"public.recursive_routes_id_seq"},{"attidentity":"d","attname":"id","column_number":1,"relname":"server_snapshots","sequence_name":"public.server_snapshots_id_seq"},{"attidentity":"d","attname":"id","column_number":1,"relname":"servers","sequence_name":"public.servers_id_seq"}],"indexes":[{"definition":"CREATE INDEX idx_alert_history_fired_at ON public.alert_history USING btree (fired_at)","index_name":"idx_alert_history_fired_at","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"alert_history"},{"definition":"CREATE INDEX idx_alert_history_rule_id ON public.alert_history USING btree (rule_id)","index_name":"idx_alert_history_rule_id","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"alert_history"},{"definition":"CREATE INDEX idx_alert_outbox_pending_next ON public.alert_outbox USING btree (next_attempt_at) WHERE (status = 'pending'::text)","index_name":"idx_alert_outbox_pending_next","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"alert_outbox"},{"definition":"CREATE INDEX idx_alert_outbox_status_next_attempt ON public.alert_outbox USING btree (status, next_attempt_at)","index_name":"idx_alert_outbox_status_next_attempt","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"alert_outbox"},{"definition":"CREATE INDEX idx_alert_rule_conditions_rule ON public.alert_rule_conditions USING btree (rule_id)","index_name":"idx_alert_rule_conditions_rule","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"alert_rule_conditions"},{"definition":"CREATE INDEX idx_alert_rule_targets_rule ON public.alert_rule_targets USING btree (rule_id)","index_name":"idx_alert_rule_targets_rule","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"alert_rule_targets"},{"definition":"CREATE INDEX idx_backup_entries_server_created ON public.backup_entries USING btree (server_id, created_at)","index_name":"idx_backup_entries_server_created","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"backup_entries"},{"definition":"CREATE INDEX idx_config_revisions_server_section_created ON public.config_revisions USING btree (server_id, section, created_at DESC)","index_name":"idx_config_revisions_server_section_created","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"config_revisions"},{"definition":"CREATE INDEX idx_events_created_at ON public.events USING btree (created_at DESC)","index_name":"idx_events_created_at","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"events"},{"definition":"CREATE INDEX idx_events_event_type_created_at ON public.events USING btree (event_type, created_at DESC)","index_name":"idx_events_event_type_created_at","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"events"},{"definition":"CREATE INDEX idx_events_level_created_at ON public.events USING btree (level, created_at DESC)","index_name":"idx_events_level_created_at","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"events"},{"definition":"CREATE INDEX idx_events_source_created_at ON public.events USING btree (source_module, created_at DESC)","index_name":"idx_events_source_created_at","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"events"},{"definition":"CREATE INDEX idx_filter_rules_server_sort ON public.filter_rules USING btree (server_id, sort_order)","index_name":"idx_filter_rules_server_sort","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"filter_rules"},{"definition":"CREATE INDEX idx_flow_buckets_server_time ON ONLY public.flow_buckets USING btree (server_id, bucket_at)","index_name":"idx_flow_buckets_server_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"flow_buckets"},{"definition":"CREATE INDEX idx_flow_daily_dims_day ON ONLY public.flow_daily_dims USING btree (day, dim)","index_name":"idx_flow_daily_dims_day","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"flow_daily_dims"},{"definition":"CREATE INDEX idx_flow_minute_dims_time ON ONLY public.flow_minute_dims USING btree (bucket_at, dim)","index_name":"idx_flow_minute_dims_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"flow_minute_dims"},{"definition":"CREATE INDEX idx_internet_path_snapshots_sampled ON ONLY public.internet_path_snapshots USING btree (sampled_at)","index_name":"idx_internet_path_snapshots_sampled","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"internet_path_snapshots"},{"definition":"CREATE INDEX idx_recursive_routes_server_sort ON public.recursive_routes USING btree (server_id, sort_order)","index_name":"idx_recursive_routes_server_sort","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"recursive_routes"},{"definition":"CREATE INDEX idx_scheduler_runs_finished_at ON public.scheduler_runs USING btree (finished_at)","index_name":"idx_scheduler_runs_finished_at","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"scheduler_runs"},{"definition":"CREATE INDEX idx_scheduler_runs_job_time ON public.scheduler_runs USING btree (job_key, started_at)","index_name":"idx_scheduler_runs_job_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"scheduler_runs"},{"definition":"CREATE INDEX idx_server_snapshots_server_time ON ONLY public.server_snapshots USING btree (server_id, polled_at)","index_name":"idx_server_snapshots_server_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"server_snapshots"},{"definition":"CREATE INDEX idx_traffic_samples_server_iface_time ON ONLY public.traffic_samples USING btree (server_id, interface_name, sampled_at)","index_name":"idx_traffic_samples_server_iface_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"traffic_samples"},{"definition":"CREATE INDEX idx_uptime_probes_server_sort ON public.uptime_probes USING btree (src_server_id, sort_order)","index_name":"idx_uptime_probes_server_sort","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"uptime_probes"},{"definition":"CREATE INDEX idx_uptime_speed_probes_src_sort ON public.uptime_speed_probes USING btree (src_server_id, sort_order)","index_name":"idx_uptime_speed_probes_src_sort","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"uptime_speed_probes"},{"definition":"CREATE INDEX idx_uptime_speed_test_runs_created_at ON public.uptime_speed_test_runs USING btree (created_at)","index_name":"idx_uptime_speed_test_runs_created_at","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"uptime_speed_test_runs"},{"definition":"CREATE INDEX idx_user_iface_bind_user ON public.user_interface_bindings USING btree (user_id)","index_name":"idx_user_iface_bind_user","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"user_interface_bindings"},{"definition":"CREATE INDEX mcp_artifacts_expiry ON public.mcp_artifacts USING btree (expires_at)","index_name":"mcp_artifacts_expiry","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_artifacts"},{"definition":"CREATE INDEX mcp_audit_handshake_cooldown ON public.mcp_audit USING btree (connection_id, created_at DESC) WHERE (action = 'connection.handshake.start'::text)","index_name":"mcp_audit_handshake_cooldown","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_audit"},{"definition":"CREATE INDEX mcp_audit_time ON public.mcp_audit USING btree (created_at, id)","index_name":"mcp_audit_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_audit"},{"definition":"CREATE INDEX mcp_credentials_connection ON public.mcp_credentials USING btree (connection_id)","index_name":"mcp_credentials_connection","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_credentials"},{"definition":"CREATE INDEX mcp_credentials_readiness_expired ON public.mcp_credentials USING btree (connection_id, expires_at) WHERE (client_id = 'mcp-readiness-probe'::text)","index_name":"mcp_credentials_readiness_expired","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_credentials"},{"definition":"CREATE INDEX mcp_grants_credential ON public.mcp_grants USING btree (connection_id, ((payload ->> 'credentialId'::text)))","index_name":"mcp_grants_credential","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_grants"},{"definition":"CREATE INDEX mcp_path_checks_latest ON public.mcp_path_checks USING btree (connection_id, profile_hash, candidate_id, created_at DESC)","index_name":"mcp_path_checks_latest","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_path_checks"},{"definition":"CREATE INDEX mcp_plans_connection ON public.mcp_plans USING btree (connection_id, created_at)","index_name":"mcp_plans_connection","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_plans"},{"definition":"CREATE INDEX mcp_portal_events_time ON public.mcp_portal_events USING btree (created_at)","index_name":"mcp_portal_events_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_portal_events"},{"definition":"CREATE INDEX mcp_requests_connection_time ON public.mcp_requests USING btree (connection_id, created_at, id)","index_name":"mcp_requests_connection_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_requests"},{"definition":"CREATE INDEX mcp_requests_server ON public.mcp_requests USING gin (server_ids)","index_name":"mcp_requests_server","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_requests"},{"definition":"CREATE INDEX mcp_routing_forward_charges_retention ON public.mcp_routing_forward_charges USING btree (created_at, source_operation_id)","index_name":"mcp_routing_forward_charges_retention","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_routing_forward_charges"},{"definition":"CREATE INDEX mcp_routing_forward_charges_window ON public.mcp_routing_forward_charges USING btree (profile_id, created_at)","index_name":"mcp_routing_forward_charges_window","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_routing_forward_charges"},{"definition":"CREATE INDEX mcp_routing_manual_inverse_charges_retention ON public.mcp_routing_manual_inverse_charges USING btree (created_at, recovery_operation_id)","index_name":"mcp_routing_manual_inverse_charges_retention","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_routing_manual_inverse_charges"},{"definition":"CREATE INDEX mcp_routing_manual_inverse_charges_window ON public.mcp_routing_manual_inverse_charges USING btree (profile_id, created_at)","index_name":"mcp_routing_manual_inverse_charges_window","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_routing_manual_inverse_charges"},{"definition":"CREATE INDEX mcp_routing_observations_leg ON public.mcp_routing_observations USING btree (src_server_id, dst_server_id, src_interface, family, routing_table, created_at DESC)","index_name":"mcp_routing_observations_leg","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_routing_observations"},{"definition":"CREATE INDEX mcp_routing_profiles_enabled ON public.mcp_routing_profiles USING btree (updated_at) WHERE enabled","index_name":"mcp_routing_profiles_enabled","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_routing_profiles"},{"definition":"CREATE INDEX mcp_routing_reservation_slots_window ON public.mcp_routing_reservation_slots USING btree (profile_id, created_at)","index_name":"mcp_routing_reservation_slots_window","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_routing_reservation_slots"},{"definition":"CREATE INDEX mcp_scoped_recovery_manual_history ON public.mcp_scoped_recovery_intents USING btree (mode, source_operation_id, source_step_index) WHERE (mode = 'manual'::text)","index_name":"mcp_scoped_recovery_manual_history","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"mcp_scoped_recovery_intents"},{"definition":"CREATE INDEX router_logs_level_time ON public.router_log_entries USING btree (level, effective_at DESC)","index_name":"router_logs_level_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"router_log_entries"},{"definition":"CREATE INDEX router_logs_received ON public.router_log_entries USING btree (received_at)","index_name":"router_logs_received","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"router_log_entries"},{"definition":"CREATE INDEX router_logs_server_cursor ON public.router_log_entries USING btree (server_id, id)","index_name":"router_logs_server_cursor","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"router_log_entries"},{"definition":"CREATE INDEX router_logs_server_time ON public.router_log_entries USING btree (server_id, effective_at DESC, id DESC)","index_name":"router_logs_server_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"router_log_entries"},{"definition":"CREATE INDEX router_logs_time ON public.router_log_entries USING btree (effective_at DESC, id DESC)","index_name":"router_logs_time","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"router_log_entries"},{"definition":"CREATE INDEX router_logs_topics ON public.router_log_entries USING gin (topics)","index_name":"router_logs_topics","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"router_log_entries"},{"definition":"CREATE INDEX server_poll_storage_expiry ON public.server_poll_storage_admissions USING btree (gc_not_before, receipt_id)","index_name":"server_poll_storage_expiry","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"server_poll_storage_admissions"},{"definition":"CREATE INDEX server_probe_storage_admissions_owner_age ON public.server_probe_storage_admissions USING btree (owner_id, authority_expires_at, receipt_id)","index_name":"server_probe_storage_admissions_owner_age","indisprimary":false,"indisready":true,"indisunique":false,"indisvalid":true,"relname":"server_probe_storage_admissions"},{"definition":"CREATE UNIQUE INDEX acme_settings_pkey ON public.acme_settings USING btree (id)","index_name":"acme_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"acme_settings"},{"definition":"CREATE UNIQUE INDEX alert_engine_confirm_pending_pkey ON public.alert_engine_confirm_pending USING btree (rule_id)","index_name":"alert_engine_confirm_pending_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_engine_confirm_pending"},{"definition":"CREATE UNIQUE INDEX alert_engine_cursor_pkey ON public.alert_engine_cursor USING btree (id)","index_name":"alert_engine_cursor_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_engine_cursor"},{"definition":"CREATE UNIQUE INDEX alert_engine_prev_live_pkey ON public.alert_engine_prev_live USING btree (kind)","index_name":"alert_engine_prev_live_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_engine_prev_live"},{"definition":"CREATE UNIQUE INDEX alert_engine_state_pkey ON public.alert_engine_state USING btree (scope_key)","index_name":"alert_engine_state_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_engine_state"},{"definition":"CREATE UNIQUE INDEX alert_groups_pkey ON public.alert_groups USING btree (id)","index_name":"alert_groups_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_groups"},{"definition":"CREATE UNIQUE INDEX alert_history_pkey ON public.alert_history USING btree (id)","index_name":"alert_history_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_history"},{"definition":"CREATE UNIQUE INDEX alert_outbox_pkey ON public.alert_outbox USING btree (id)","index_name":"alert_outbox_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_outbox"},{"definition":"CREATE UNIQUE INDEX alert_rule_conditions_pkey ON public.alert_rule_conditions USING btree (id)","index_name":"alert_rule_conditions_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_rule_conditions"},{"definition":"CREATE UNIQUE INDEX alert_rule_targets_pkey ON public.alert_rule_targets USING btree (id)","index_name":"alert_rule_targets_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_rule_targets"},{"definition":"CREATE UNIQUE INDEX alert_rules_pkey ON public.alert_rules USING btree (id)","index_name":"alert_rules_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_rules"},{"definition":"CREATE UNIQUE INDEX alert_telegram_settings_pkey ON public.alert_telegram_settings USING btree (id)","index_name":"alert_telegram_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_telegram_settings"},{"definition":"CREATE UNIQUE INDEX app_users_login_key ON public.app_users USING btree (login)","index_name":"app_users_login_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"app_users"},{"definition":"CREATE UNIQUE INDEX app_users_pkey ON public.app_users USING btree (id)","index_name":"app_users_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"app_users"},{"definition":"CREATE UNIQUE INDEX backup_entries_pkey ON public.backup_entries USING btree (id)","index_name":"backup_entries_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"backup_entries"},{"definition":"CREATE UNIQUE INDEX backup_schedule_settings_pkey ON public.backup_schedule_settings USING btree (id)","index_name":"backup_schedule_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"backup_schedule_settings"},{"definition":"CREATE UNIQUE INDEX backup_storage_settings_pkey ON public.backup_storage_settings USING btree (id)","index_name":"backup_storage_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"backup_storage_settings"},{"definition":"CREATE UNIQUE INDEX certificate_issue_jobs_pkey ON public.certificate_issue_jobs USING btree (id)","index_name":"certificate_issue_jobs_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"certificate_issue_jobs"},{"definition":"CREATE UNIQUE INDEX certificate_renew_settings_pkey ON public.certificate_renew_settings USING btree (id)","index_name":"certificate_renew_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"certificate_renew_settings"},{"definition":"CREATE UNIQUE INDEX config_revisions_pkey ON public.config_revisions USING btree (id)","index_name":"config_revisions_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"config_revisions"},{"definition":"CREATE UNIQUE INDEX data_migration_pkey ON public.data_migration USING btree (id)","index_name":"data_migration_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"data_migration"},{"definition":"CREATE UNIQUE INDEX events_pkey ON public.events USING btree (id)","index_name":"events_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"events"},{"definition":"CREATE UNIQUE INDEX evobgp_settings_pkey ON public.evobgp_settings USING btree (id)","index_name":"evobgp_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"evobgp_settings"},{"definition":"CREATE UNIQUE INDEX filter_rules_pkey ON public.filter_rules USING btree (id)","index_name":"filter_rules_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"filter_rules"},{"definition":"CREATE UNIQUE INDEX flow_asn_meta_pkey ON public.flow_asn_meta USING btree (asn)","index_name":"flow_asn_meta_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_asn_meta"},{"definition":"CREATE UNIQUE INDEX flow_buckets_pkey ON ONLY public.flow_buckets USING btree (server_id, bucket_at, src, dst, proto, src_port, dst_port, in_iface)","index_name":"flow_buckets_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_buckets"},{"definition":"CREATE UNIQUE INDEX flow_daily_dims_pkey ON ONLY public.flow_daily_dims USING btree (server_id, day, dim, key)","index_name":"flow_daily_dims_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_daily_dims"},{"definition":"CREATE UNIQUE INDEX flow_daily_facts_pkey ON ONLY public.flow_daily_facts USING btree (server_id, day, iface, country, service, asn)","index_name":"flow_daily_facts_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_daily_facts"},{"definition":"CREATE UNIQUE INDEX flow_hour_facts_pkey ON ONLY public.flow_hour_facts USING btree (server_id, bucket_at, iface, country, service, asn)","index_name":"flow_hour_facts_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_hour_facts"},{"definition":"CREATE UNIQUE INDEX flow_ip_meta_pkey ON public.flow_ip_meta USING btree (prefix)","index_name":"flow_ip_meta_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_ip_meta"},{"definition":"CREATE UNIQUE INDEX flow_minute_dims_pkey ON ONLY public.flow_minute_dims USING btree (server_id, bucket_at, dim, key)","index_name":"flow_minute_dims_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_minute_dims"},{"definition":"CREATE UNIQUE INDEX flow_minute_stats_pkey ON ONLY public.flow_minute_stats USING btree (server_id, bucket_at)","index_name":"flow_minute_stats_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_minute_stats"},{"definition":"CREATE UNIQUE INDEX flow_settings_intents_pkey ON public.flow_settings_intents USING btree (receipt_id, phase)","index_name":"flow_settings_intents_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_settings_intents"},{"definition":"CREATE UNIQUE INDEX flow_write_receipts_pkey ON public.flow_write_receipts USING btree (batch_id)","index_name":"flow_write_receipts_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"flow_write_receipts"},{"definition":"CREATE UNIQUE INDEX geoip_settings_pkey ON public.geoip_settings USING btree (id)","index_name":"geoip_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"geoip_settings"},{"definition":"CREATE UNIQUE INDEX idx_alert_outbox_dedupe ON public.alert_outbox USING btree (dedupe_key)","index_name":"idx_alert_outbox_dedupe","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"alert_outbox"},{"definition":"CREATE UNIQUE INDEX idx_backup_entries_filename ON public.backup_entries USING btree (filename)","index_name":"idx_backup_entries_filename","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"backup_entries"},{"definition":"CREATE UNIQUE INDEX internet_path_settings_pkey ON public.internet_path_settings USING btree (id)","index_name":"internet_path_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"internet_path_settings"},{"definition":"CREATE UNIQUE INDEX internet_path_snapshots_pkey ON ONLY public.internet_path_snapshots USING btree (id, sampled_at)","index_name":"internet_path_snapshots_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"internet_path_snapshots"},{"definition":"CREATE UNIQUE INDEX mcp_artifacts_pkey ON public.mcp_artifacts USING btree (id)","index_name":"mcp_artifacts_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_artifacts"},{"definition":"CREATE UNIQUE INDEX mcp_audit_pkey ON public.mcp_audit USING btree (id)","index_name":"mcp_audit_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_audit"},{"definition":"CREATE UNIQUE INDEX mcp_budget_buckets_pkey ON public.mcp_budget_buckets USING btree (connection_id, kind, bucket)","index_name":"mcp_budget_buckets_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_budget_buckets"},{"definition":"CREATE UNIQUE INDEX mcp_connections_pkey ON public.mcp_connections USING btree (id)","index_name":"mcp_connections_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_connections"},{"definition":"CREATE UNIQUE INDEX mcp_credentials_digest_key ON public.mcp_credentials USING btree (digest)","index_name":"mcp_credentials_digest_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_credentials"},{"definition":"CREATE UNIQUE INDEX mcp_credentials_pkey ON public.mcp_credentials USING btree (id)","index_name":"mcp_credentials_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_credentials"},{"definition":"CREATE UNIQUE INDEX mcp_diagnostic_cancellations_pkey ON public.mcp_diagnostic_cancellations USING btree (operation_id)","index_name":"mcp_diagnostic_cancellations_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_diagnostic_cancellations"},{"definition":"CREATE UNIQUE INDEX mcp_drift_open_step ON public.mcp_drift USING btree (operation_id, ((expected ->> 'stepIndex'::text))) WHERE (acknowledged_at IS NULL)","index_name":"mcp_drift_open_step","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_drift"},{"definition":"CREATE UNIQUE INDEX mcp_drift_pkey ON public.mcp_drift USING btree (id)","index_name":"mcp_drift_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_drift"},{"definition":"CREATE UNIQUE INDEX mcp_grants_pkey ON public.mcp_grants USING btree (id)","index_name":"mcp_grants_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_grants"},{"definition":"CREATE UNIQUE INDEX mcp_management_channels_pkey ON public.mcp_management_channels USING btree (id)","index_name":"mcp_management_channels_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_management_channels"},{"definition":"CREATE UNIQUE INDEX mcp_management_channels_server_id_key ON public.mcp_management_channels USING btree (server_id)","index_name":"mcp_management_channels_server_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_management_channels"},{"definition":"CREATE UNIQUE INDEX mcp_manual_inverse_source_step_unique ON public.mcp_routing_manual_inverse_charges USING btree (source_operation_id, source_step_index)","index_name":"mcp_manual_inverse_source_step_unique","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_manual_inverse_charges"},{"definition":"CREATE UNIQUE INDEX mcp_oauth_clients_pkey ON public.mcp_oauth_clients USING btree (id)","index_name":"mcp_oauth_clients_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_oauth_clients"},{"definition":"CREATE UNIQUE INDEX mcp_oauth_requests_code_digest_key ON public.mcp_oauth_requests USING btree (code_digest)","index_name":"mcp_oauth_requests_code_digest_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_oauth_requests"},{"definition":"CREATE UNIQUE INDEX mcp_oauth_requests_pkey ON public.mcp_oauth_requests USING btree (id)","index_name":"mcp_oauth_requests_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_oauth_requests"},{"definition":"CREATE UNIQUE INDEX mcp_operation_checkpoints_pkey ON public.mcp_operation_checkpoints USING btree (operation_id, step_index)","index_name":"mcp_operation_checkpoints_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operation_checkpoints"},{"definition":"CREATE UNIQUE INDEX mcp_operation_steps_pkey ON public.mcp_operation_steps USING btree (operation_id, step_index)","index_name":"mcp_operation_steps_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operation_steps"},{"definition":"CREATE UNIQUE INDEX mcp_operations_connection_id_idempotency_key_key ON public.mcp_operations USING btree (connection_id, idempotency_key)","index_name":"mcp_operations_connection_id_idempotency_key_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operations"},{"definition":"CREATE UNIQUE INDEX mcp_operations_pkey ON public.mcp_operations USING btree (id)","index_name":"mcp_operations_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operations"},{"definition":"CREATE UNIQUE INDEX mcp_operations_plan_id_key ON public.mcp_operations USING btree (plan_id)","index_name":"mcp_operations_plan_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operations"},{"definition":"CREATE UNIQUE INDEX mcp_owner_revocations_pkey ON public.mcp_owner_revocations USING btree (owner_id)","index_name":"mcp_owner_revocations_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_owner_revocations"},{"definition":"CREATE UNIQUE INDEX mcp_path_checks_pkey ON public.mcp_path_checks USING btree (id)","index_name":"mcp_path_checks_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_path_checks"},{"definition":"CREATE UNIQUE INDEX mcp_plans_pkey ON public.mcp_plans USING btree (id)","index_name":"mcp_plans_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_plans"},{"definition":"CREATE UNIQUE INDEX mcp_portal_events_pkey ON public.mcp_portal_events USING btree (id)","index_name":"mcp_portal_events_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_portal_events"},{"definition":"CREATE UNIQUE INDEX mcp_protections_pkey ON public.mcp_protections USING btree (id)","index_name":"mcp_protections_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_protections"},{"definition":"CREATE UNIQUE INDEX mcp_read_leases_pkey ON public.mcp_read_leases USING btree (id)","index_name":"mcp_read_leases_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_read_leases"},{"definition":"CREATE UNIQUE INDEX mcp_recovery_intents_pkey ON public.mcp_recovery_intents USING btree (operation_id)","index_name":"mcp_recovery_intents_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_recovery_intents"},{"definition":"CREATE UNIQUE INDEX mcp_requests_pkey ON public.mcp_requests USING btree (id)","index_name":"mcp_requests_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_requests"},{"definition":"CREATE UNIQUE INDEX mcp_routing_forward_charges_pkey ON public.mcp_routing_forward_charges USING btree (source_operation_id)","index_name":"mcp_routing_forward_charges_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_forward_charges"},{"definition":"CREATE UNIQUE INDEX mcp_routing_manual_inverse_charges_pkey ON public.mcp_routing_manual_inverse_charges USING btree (recovery_operation_id)","index_name":"mcp_routing_manual_inverse_charges_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_manual_inverse_charges"},{"definition":"CREATE UNIQUE INDEX mcp_routing_observations_pkey ON public.mcp_routing_observations USING btree (id)","index_name":"mcp_routing_observations_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_observations"},{"definition":"CREATE UNIQUE INDEX mcp_routing_profiles_pkey ON public.mcp_routing_profiles USING btree (id)","index_name":"mcp_routing_profiles_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_profiles"},{"definition":"CREATE UNIQUE INDEX mcp_routing_reservation_slots_pkey ON public.mcp_routing_reservation_slots USING btree (reservation_id, lane)","index_name":"mcp_routing_reservation_slots_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_reservation_slots"},{"definition":"CREATE UNIQUE INDEX mcp_routing_reservations_pkey ON public.mcp_routing_reservations USING btree (id)","index_name":"mcp_routing_reservations_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_reservations"},{"definition":"CREATE UNIQUE INDEX mcp_routing_reservations_source_operation_id_source_step_in_key ON public.mcp_routing_reservations USING btree (source_operation_id, source_step_index)","index_name":"mcp_routing_reservations_source_operation_id_source_step_in_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_routing_reservations"},{"definition":"CREATE UNIQUE INDEX mcp_scoped_recovery_intents_pkey ON public.mcp_scoped_recovery_intents USING btree (source_operation_id, source_step_index)","index_name":"mcp_scoped_recovery_intents_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_scoped_recovery_intents"},{"definition":"CREATE UNIQUE INDEX mcp_scoped_recovery_intents_recovery_receipt_id_key ON public.mcp_scoped_recovery_intents USING btree (recovery_receipt_id)","index_name":"mcp_scoped_recovery_intents_recovery_receipt_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_scoped_recovery_intents"},{"definition":"CREATE UNIQUE INDEX mcp_secrets_pkey ON public.mcp_secrets USING btree (id)","index_name":"mcp_secrets_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_secrets"},{"definition":"CREATE UNIQUE INDEX mcp_settings_pkey ON public.mcp_settings USING btree (id)","index_name":"mcp_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_settings"},{"definition":"CREATE UNIQUE INDEX mcp_unauthorized_buckets_pkey ON public.mcp_unauthorized_buckets USING btree (bucket, client_hash, reason)","index_name":"mcp_unauthorized_buckets_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_unauthorized_buckets"},{"definition":"CREATE UNIQUE INDEX recursive_routes_pkey ON public.recursive_routes USING btree (id)","index_name":"recursive_routes_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"recursive_routes"},{"definition":"CREATE UNIQUE INDEX router_log_collection_state_pkey ON public.router_log_collection_state USING btree (server_id)","index_name":"router_log_collection_state_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"router_log_collection_state"},{"definition":"CREATE UNIQUE INDEX router_log_entries_pkey ON public.router_log_entries USING btree (id)","index_name":"router_log_entries_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"router_log_entries"},{"definition":"CREATE UNIQUE INDEX router_log_settings_pkey ON public.router_log_settings USING btree (id)","index_name":"router_log_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"router_log_settings"},{"definition":"CREATE UNIQUE INDEX scheduler_runs_pkey ON public.scheduler_runs USING btree (id)","index_name":"scheduler_runs_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"scheduler_runs"},{"definition":"CREATE UNIQUE INDEX schema_migrations_pkey ON public.schema_migrations USING btree (id)","index_name":"schema_migrations_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"schema_migrations"},{"definition":"CREATE UNIQUE INDEX server_poll_acks_pkey ON public.server_poll_acks USING btree (receipt_id, stage)","index_name":"server_poll_acks_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_acks"},{"definition":"CREATE UNIQUE INDEX server_poll_archives_execution_id_key ON public.server_poll_archives USING btree (execution_id)","index_name":"server_poll_archives_execution_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_archives"},{"definition":"CREATE UNIQUE INDEX server_poll_archives_pkey ON public.server_poll_archives USING btree (receipt_id)","index_name":"server_poll_archives_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_archives"},{"definition":"CREATE UNIQUE INDEX server_poll_executions_identity_kind_execution_id_key ON public.server_poll_executions USING btree (identity_kind, execution_id)","index_name":"server_poll_executions_identity_kind_execution_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_executions"},{"definition":"CREATE UNIQUE INDEX server_poll_executions_pkey ON public.server_poll_executions USING btree (receipt_id)","index_name":"server_poll_executions_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_executions"},{"definition":"CREATE UNIQUE INDEX server_poll_intents_pkey ON public.server_poll_intents USING btree (receipt_id, stage)","index_name":"server_poll_intents_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_intents"},{"definition":"CREATE UNIQUE INDEX server_poll_quarantines_identity_kind_execution_id_key ON public.server_poll_quarantines USING btree (identity_kind, execution_id)","index_name":"server_poll_quarantines_identity_kind_execution_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_quarantines"},{"definition":"CREATE UNIQUE INDEX server_poll_quarantines_pkey ON public.server_poll_quarantines USING btree (receipt_id)","index_name":"server_poll_quarantines_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_quarantines"},{"definition":"CREATE UNIQUE INDEX server_poll_receipts_pkey ON public.server_poll_receipts USING btree (receipt_id)","index_name":"server_poll_receipts_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_receipts"},{"definition":"CREATE UNIQUE INDEX server_poll_storage_admissions_identity_kind_execution_id_key ON public.server_poll_storage_admissions USING btree (identity_kind, execution_id)","index_name":"server_poll_storage_admissions_identity_kind_execution_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_storage_admissions"},{"definition":"CREATE UNIQUE INDEX server_poll_storage_admissions_pkey ON public.server_poll_storage_admissions USING btree (receipt_id)","index_name":"server_poll_storage_admissions_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_storage_admissions"},{"definition":"CREATE UNIQUE INDEX server_poll_storage_usage_pkey ON public.server_poll_storage_usage USING btree (scope)","index_name":"server_poll_storage_usage_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_storage_usage"},{"definition":"CREATE UNIQUE INDEX server_probe_execution_payloads_pkey ON public.server_probe_execution_payloads USING btree (receipt_id)","index_name":"server_probe_execution_payloads_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_execution_payloads"},{"definition":"CREATE UNIQUE INDEX server_probe_execution_quarantin_identity_kind_execution_id_key ON public.server_probe_execution_quarantines USING btree (identity_kind, execution_id)","index_name":"server_probe_execution_quarantin_identity_kind_execution_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_execution_quarantines"},{"definition":"CREATE UNIQUE INDEX server_probe_execution_quarantines_pkey ON public.server_probe_execution_quarantines USING btree (receipt_id)","index_name":"server_probe_execution_quarantines_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_execution_quarantines"},{"definition":"CREATE UNIQUE INDEX server_probe_execution_stages_pkey ON public.server_probe_execution_stages USING btree (receipt_id, stage_index)","index_name":"server_probe_execution_stages_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_execution_stages"},{"definition":"CREATE UNIQUE INDEX server_probe_executions_identity_kind_execution_id_key ON public.server_probe_executions USING btree (identity_kind, execution_id)","index_name":"server_probe_executions_identity_kind_execution_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_executions"},{"definition":"CREATE UNIQUE INDEX server_probe_executions_pkey ON public.server_probe_executions USING btree (receipt_id)","index_name":"server_probe_executions_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_executions"},{"definition":"CREATE UNIQUE INDEX server_probe_storage_admissions_identity_kind_execution_id_key ON public.server_probe_storage_admissions USING btree (identity_kind, execution_id)","index_name":"server_probe_storage_admissions_identity_kind_execution_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_storage_admissions"},{"definition":"CREATE UNIQUE INDEX server_probe_storage_admissions_pkey ON public.server_probe_storage_admissions USING btree (receipt_id)","index_name":"server_probe_storage_admissions_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_storage_admissions"},{"definition":"CREATE UNIQUE INDEX server_probe_storage_usage_pkey ON public.server_probe_storage_usage USING btree (scope)","index_name":"server_probe_storage_usage_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_storage_usage"},{"definition":"CREATE UNIQUE INDEX server_snapshots_pkey ON ONLY public.server_snapshots USING btree (id, polled_at)","index_name":"server_snapshots_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_snapshots"},{"definition":"CREATE UNIQUE INDEX servers_api_ping_settings_pkey ON public.servers_api_ping_settings USING btree (id)","index_name":"servers_api_ping_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"servers_api_ping_settings"},{"definition":"CREATE UNIQUE INDEX servers_pkey ON public.servers USING btree (id)","index_name":"servers_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"servers"},{"definition":"CREATE UNIQUE INDEX servers_rest_ping_samples_pkey ON ONLY public.servers_rest_ping_samples USING btree (server_id, sampled_at)","index_name":"servers_rest_ping_samples_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"servers_rest_ping_samples"},{"definition":"CREATE UNIQUE INDEX traffic_collection_state_pkey ON public.traffic_collection_state USING btree (server_id)","index_name":"traffic_collection_state_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"traffic_collection_state"},{"definition":"CREATE UNIQUE INDEX traffic_flow_settings_pkey ON public.traffic_flow_settings USING btree (id)","index_name":"traffic_flow_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"traffic_flow_settings"},{"definition":"CREATE UNIQUE INDEX traffic_samples_pkey ON ONLY public.traffic_samples USING btree (server_id, sampled_at, interface_name, peer_public_key)","index_name":"traffic_samples_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"traffic_samples"},{"definition":"CREATE UNIQUE INDEX traffic_settings_pkey ON public.traffic_settings USING btree (id)","index_name":"traffic_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"traffic_settings"},{"definition":"CREATE UNIQUE INDEX uptime_probe_samples_pkey ON ONLY public.uptime_probe_samples USING btree (probe_id, sampled_at)","index_name":"uptime_probe_samples_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"uptime_probe_samples"},{"definition":"CREATE UNIQUE INDEX uptime_probes_pkey ON public.uptime_probes USING btree (id)","index_name":"uptime_probes_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"uptime_probes"},{"definition":"CREATE UNIQUE INDEX uptime_resource_samples_pkey ON ONLY public.uptime_resource_samples USING btree (server_id, sampled_at)","index_name":"uptime_resource_samples_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"uptime_resource_samples"},{"definition":"CREATE UNIQUE INDEX uptime_settings_pkey ON public.uptime_settings USING btree (id)","index_name":"uptime_settings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"uptime_settings"},{"definition":"CREATE UNIQUE INDEX uptime_speed_probes_pkey ON public.uptime_speed_probes USING btree (id)","index_name":"uptime_speed_probes_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"uptime_speed_probes"},{"definition":"CREATE UNIQUE INDEX uptime_speed_test_runs_pkey ON public.uptime_speed_test_runs USING btree (id)","index_name":"uptime_speed_test_runs_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"uptime_speed_test_runs"},{"definition":"CREATE UNIQUE INDEX user_interface_bindings_pkey ON public.user_interface_bindings USING btree (id)","index_name":"user_interface_bindings_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"user_interface_bindings"},{"definition":"CREATE UNIQUE INDEX user_interface_bindings_server_id_interface_name_peer_publi_key ON public.user_interface_bindings USING btree (server_id, interface_name, peer_public_key)","index_name":"user_interface_bindings_server_id_interface_name_peer_publi_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"user_interface_bindings"},{"definition":"CREATE UNIQUE INDEX vrf_production_tickets_pkey ON public.vrf_production_tickets USING btree (ticket_id)","index_name":"vrf_production_tickets_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"vrf_production_tickets"},{"definition":"CREATE UNIQUE INDEX vrf_production_tickets_plan_id_key ON public.vrf_production_tickets USING btree (plan_id)","index_name":"vrf_production_tickets_plan_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"vrf_production_tickets"}],"inheritance":[],"memberships":[{"admin_option":false,"granted_role":"pg_read_all_settings","grantor":"@mm-role:control@","inherit_option":true,"member":"pg_monitor","set_option":true},{"admin_option":false,"granted_role":"pg_read_all_stats","grantor":"@mm-role:control@","inherit_option":true,"member":"pg_monitor","set_option":true},{"admin_option":false,"granted_role":"pg_stat_scan_tables","grantor":"@mm-role:control@","inherit_option":true,"member":"pg_monitor","set_option":true}],"parameters":[{"can_set":false,"name":"event_triggers","rolname":"@mm-role:capture@"},{"can_set":false,"name":"event_triggers","rolname":"@mm-role:operator@"},{"can_set":false,"name":"event_triggers","rolname":"@mm-role:publisher@"},{"can_set":false,"name":"event_triggers","rolname":"@mm-role:runtime@"},{"can_set":false,"name":"session_replication_role","rolname":"@mm-role:capture@"},{"can_set":false,"name":"session_replication_role","rolname":"@mm-role:operator@"},{"can_set":false,"name":"session_replication_role","rolname":"@mm-role:publisher@"},{"can_set":false,"name":"session_replication_role","rolname":"@mm-role:runtime@"}],"roleGrants":[{"delete":false,"insert":false,"read":false,"relname":"acme_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_engine_confirm_pending","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_engine_cursor","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_engine_prev_live","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_engine_state","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_groups","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_history","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_outbox","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_rule_conditions","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_rule_targets","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_rules","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"alert_telegram_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"app_users","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"backup_entries","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"backup_schedule_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"backup_storage_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"certificate_issue_jobs","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"certificate_renew_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"config_revisions","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"data_migration","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"events","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"evobgp_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"filter_rules","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_asn_meta","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_buckets","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_daily_dims","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_daily_facts","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_hour_facts","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_ip_meta","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_minute_dims","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_minute_stats","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_settings_intents","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"flow_write_receipts","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"geoip_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"internet_path_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"internet_path_snapshots","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_artifacts","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_audit","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_budget_buckets","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_connections","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_credentials","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_diagnostic_cancellations","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_drift","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_grants","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_management_channels","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_oauth_clients","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_oauth_requests","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_operation_checkpoints","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_operation_steps","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_operations","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_owner_revocations","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_path_checks","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_plans","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_portal_events","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_protections","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_read_leases","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_recovery_intents","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_requests","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_routing_forward_charges","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_routing_manual_inverse_charges","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_routing_observations","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_routing_profiles","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_routing_reservation_slots","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_routing_reservations","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_scoped_recovery_intents","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_secrets","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"mcp_unauthorized_buckets","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"recursive_routes","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"router_log_collection_state","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"router_log_entries","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"router_log_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"scheduler_runs","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_acks","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_archives","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_executions","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_intents","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_quarantines","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_receipts","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_storage_admissions","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_poll_storage_usage","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_probe_execution_payloads","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_probe_execution_quarantines","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_probe_execution_stages","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_probe_executions","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_probe_storage_admissions","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_probe_storage_usage","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"server_snapshots","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"servers","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"servers_api_ping_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"servers_rest_ping_samples","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"traffic_collection_state","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"traffic_flow_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"traffic_samples","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"traffic_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"uptime_probe_samples","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"uptime_probes","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"uptime_resource_samples","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"uptime_settings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"uptime_speed_probes","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"uptime_speed_test_runs","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"user_interface_bindings","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":false,"relname":"vrf_production_tickets","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"acme_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_engine_confirm_pending","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_engine_cursor","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_engine_prev_live","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_engine_state","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_groups","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_history","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_outbox","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_rule_conditions","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_rule_targets","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_rules","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"alert_telegram_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"app_users","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"backup_entries","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"backup_schedule_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"backup_storage_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"certificate_issue_jobs","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"certificate_renew_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"config_revisions","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"data_migration","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"data_migration","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"events","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"evobgp_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"filter_rules","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_asn_meta","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_buckets","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_daily_dims","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_daily_facts","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_hour_facts","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_ip_meta","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_minute_dims","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_minute_stats","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_settings_intents","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"flow_write_receipts","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"geoip_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"internet_path_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"internet_path_snapshots","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_artifacts","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_audit","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_budget_buckets","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_connections","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_credentials","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_diagnostic_cancellations","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_drift","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_grants","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_management_channels","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_oauth_clients","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_oauth_requests","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_operation_checkpoints","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_operation_steps","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_operations","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_owner_revocations","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_path_checks","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_plans","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_portal_events","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_protections","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_read_leases","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_recovery_intents","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_requests","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_routing_forward_charges","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_routing_manual_inverse_charges","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_routing_observations","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_routing_profiles","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_routing_reservation_slots","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_routing_reservations","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_scoped_recovery_intents","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_secrets","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"mcp_unauthorized_buckets","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"recursive_routes","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"router_log_collection_state","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"router_log_entries","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"router_log_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"scheduler_runs","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"schema_migrations","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"schema_migrations","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"schema_migrations","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_acks","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_archives","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_executions","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_intents","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_quarantines","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_receipts","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_storage_admissions","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_poll_storage_usage","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_probe_execution_payloads","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_probe_execution_quarantines","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_probe_execution_stages","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_probe_executions","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_probe_storage_admissions","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_probe_storage_usage","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"server_snapshots","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"servers","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"servers_api_ping_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"servers_rest_ping_samples","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"traffic_collection_state","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"traffic_flow_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"traffic_samples","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"traffic_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"uptime_probe_samples","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"uptime_probes","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"uptime_resource_samples","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"uptime_settings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"uptime_speed_probes","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"uptime_speed_test_runs","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"user_interface_bindings","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":false,"insert":false,"read":true,"relname":"vrf_production_tickets","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},{"delete":true,"insert":true,"read":true,"relname":"acme_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"acme_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_confirm_pending","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_confirm_pending","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_cursor","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_cursor","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_prev_live","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_prev_live","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_state","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_engine_state","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_groups","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_groups","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_history","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_history","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_outbox","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_outbox","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_rule_conditions","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_rule_conditions","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_rule_targets","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_rule_targets","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_rules","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_rules","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_telegram_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"alert_telegram_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"app_users","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"app_users","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"backup_entries","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"backup_entries","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"backup_schedule_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"backup_schedule_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"backup_storage_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"backup_storage_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"certificate_issue_jobs","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"certificate_issue_jobs","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"certificate_renew_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"certificate_renew_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"config_revisions","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"config_revisions","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"data_migration","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"events","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"events","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"evobgp_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"evobgp_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"filter_rules","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"filter_rules","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_asn_meta","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_asn_meta","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_buckets","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_buckets","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_daily_dims","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_daily_dims","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_daily_facts","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_daily_facts","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_hour_facts","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_hour_facts","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_ip_meta","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_ip_meta","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_minute_dims","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_minute_dims","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_minute_stats","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_minute_stats","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_settings_intents","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_settings_intents","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_write_receipts","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"flow_write_receipts","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"geoip_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"geoip_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"internet_path_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"internet_path_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"internet_path_snapshots","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"internet_path_snapshots","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_artifacts","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_artifacts","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_audit","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_audit","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_budget_buckets","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_budget_buckets","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_connections","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_connections","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_credentials","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_credentials","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_diagnostic_cancellations","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_diagnostic_cancellations","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_drift","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_drift","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_grants","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_grants","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_management_channels","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_management_channels","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_oauth_clients","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_oauth_clients","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_oauth_requests","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_oauth_requests","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_operation_checkpoints","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_operation_checkpoints","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_operation_steps","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_operation_steps","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_operations","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_operations","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_owner_revocations","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_owner_revocations","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_path_checks","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_path_checks","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_plans","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_plans","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_portal_events","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_portal_events","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_protections","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_protections","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_read_leases","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_read_leases","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_recovery_intents","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_recovery_intents","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_requests","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_requests","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_forward_charges","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_forward_charges","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_manual_inverse_charges","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_manual_inverse_charges","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_observations","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_observations","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_profiles","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_profiles","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_reservation_slots","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_reservation_slots","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_reservations","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_routing_reservations","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_scoped_recovery_intents","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_scoped_recovery_intents","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_secrets","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_secrets","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_unauthorized_buckets","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"mcp_unauthorized_buckets","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"recursive_routes","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"recursive_routes","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"router_log_collection_state","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"router_log_collection_state","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"router_log_entries","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"router_log_entries","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"router_log_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"router_log_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"scheduler_runs","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"scheduler_runs","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"schema_migrations","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_acks","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_acks","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_archives","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_archives","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_executions","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_executions","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_intents","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_intents","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_quarantines","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_quarantines","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_receipts","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_receipts","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_storage_admissions","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_storage_admissions","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_storage_usage","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_poll_storage_usage","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_execution_payloads","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_execution_payloads","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_execution_quarantines","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_execution_quarantines","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_execution_stages","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_execution_stages","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_executions","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_executions","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_storage_admissions","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_storage_admissions","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_storage_usage","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_probe_storage_usage","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_snapshots","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"server_snapshots","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"servers","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"servers","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"servers_api_ping_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"servers_api_ping_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"servers_rest_ping_samples","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"servers_rest_ping_samples","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_collection_state","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_collection_state","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_flow_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_flow_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_samples","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_samples","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"traffic_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_probe_samples","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_probe_samples","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_probes","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_probes","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_resource_samples","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_resource_samples","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_settings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_settings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_speed_probes","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_speed_probes","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_speed_test_runs","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"uptime_speed_test_runs","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"user_interface_bindings","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"user_interface_bindings","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true},{"delete":true,"insert":true,"read":true,"relname":"vrf_production_tickets","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},{"delete":true,"insert":true,"read":true,"relname":"vrf_production_tickets","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":true}],"roles":[{"rolbypassrls":false,"rolcanlogin":true,"rolcreatedb":false,"rolcreaterole":false,"rolinherit":true,"rolname":"@mm-role:capture@","rolreplication":false,"rolsuper":false},{"rolbypassrls":false,"rolcanlogin":true,"rolcreatedb":false,"rolcreaterole":false,"rolinherit":true,"rolname":"@mm-role:operator@","rolreplication":false,"rolsuper":false},{"rolbypassrls":false,"rolcanlogin":true,"rolcreatedb":false,"rolcreaterole":false,"rolinherit":true,"rolname":"@mm-role:publisher@","rolreplication":false,"rolsuper":false},{"rolbypassrls":false,"rolcanlogin":true,"rolcreatedb":false,"rolcreaterole":false,"rolinherit":true,"rolname":"@mm-role:runtime@","rolreplication":false,"rolsuper":false},{"rolbypassrls":true,"rolcanlogin":true,"rolcreatedb":true,"rolcreaterole":true,"rolinherit":true,"rolname":"@mm-role:control@","rolreplication":true,"rolsuper":true}],"schemas":[{"acl":["@mm-role:control@=UC/@mm-role:control@","@mm-role:operator@=U/@mm-role:control@","@mm-role:publisher@=U/@mm-role:control@"],"nspname":"mmapp_control","owner":"@mm-role:control@"},{"acl":["@mm-role:capture@=U/@mm-role:operator@","@mm-role:operator@=UC/@mm-role:operator@","@mm-role:publisher@=U/@mm-role:operator@","@mm-role:runtime@=U/@mm-role:operator@"],"nspname":"public","owner":"@mm-role:operator@"}],"sequences":[{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=rwU/@mm-role:operator@"],"deptype":"i","namespace":"public","owner":"@mm-role:operator@","owner_column":1,"owner_relation":"filter_rules","relname":"filter_rules_id_seq","seqcache":"1","seqcycle":false,"seqincrement":"1","seqmax":"9223372036854775807","seqmin":"1","seqstart":"1","type_oid":20},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=rwU/@mm-role:operator@"],"deptype":"i","namespace":"public","owner":"@mm-role:operator@","owner_column":1,"owner_relation":"internet_path_snapshots","relname":"internet_path_snapshots_id_seq","seqcache":"1","seqcycle":false,"seqincrement":"1","seqmax":"9223372036854775807","seqmin":"1","seqstart":"1","type_oid":20},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=rwU/@mm-role:operator@"],"deptype":"i","namespace":"public","owner":"@mm-role:operator@","owner_column":1,"owner_relation":"mcp_audit","relname":"mcp_audit_id_seq","seqcache":"1","seqcycle":false,"seqincrement":"1","seqmax":"9223372036854775807","seqmin":"1","seqstart":"1","type_oid":20},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=rwU/@mm-role:operator@"],"deptype":"i","namespace":"public","owner":"@mm-role:operator@","owner_column":1,"owner_relation":"recursive_routes","relname":"recursive_routes_id_seq","seqcache":"1","seqcycle":false,"seqincrement":"1","seqmax":"9223372036854775807","seqmin":"1","seqstart":"1","type_oid":20},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=rwU/@mm-role:operator@"],"deptype":"i","namespace":"public","owner":"@mm-role:operator@","owner_column":1,"owner_relation":"router_log_entries","relname":"router_log_entries_id_seq","seqcache":"1","seqcycle":false,"seqincrement":"1","seqmax":"9223372036854775807","seqmin":"1","seqstart":"1","type_oid":20},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=rwU/@mm-role:operator@"],"deptype":"i","namespace":"public","owner":"@mm-role:operator@","owner_column":1,"owner_relation":"server_snapshots","relname":"server_snapshots_id_seq","seqcache":"1","seqcycle":false,"seqincrement":"1","seqmax":"9223372036854775807","seqmin":"1","seqstart":"1","type_oid":20},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=rwU/@mm-role:operator@"],"deptype":"i","namespace":"public","owner":"@mm-role:operator@","owner_column":1,"owner_relation":"servers","relname":"servers_id_seq","seqcache":"1","seqcycle":false,"seqincrement":"1","seqmax":"9223372036854775807","seqmin":"1","seqstart":"1","type_oid":20}],"settings":[],"tables":[{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:publisher@=r/@mm-role:operator@","@mm-role:runtime@=r/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"schema_migrations","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (bucket_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"flow_buckets","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (bucket_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"flow_hour_facts","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (bucket_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"flow_minute_dims","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (bucket_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"flow_minute_stats","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (day)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"flow_daily_dims","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (day)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"flow_daily_facts","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (polled_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"server_snapshots","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (sampled_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"internet_path_snapshots","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (sampled_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"servers_rest_ping_samples","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (sampled_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"traffic_samples","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (sampled_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"uptime_probe_samples","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":"RANGE (sampled_at)","relforcerowsecurity":false,"relispartition":false,"relkind":"p","relname":"uptime_resource_samples","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"acme_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_engine_confirm_pending","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_engine_cursor","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_engine_prev_live","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_engine_state","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_groups","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_history","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_outbox","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_rule_conditions","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_rule_targets","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_rules","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"alert_telegram_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"app_users","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"backup_entries","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"backup_schedule_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"backup_storage_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"certificate_issue_jobs","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"certificate_renew_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"config_revisions","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"events","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"evobgp_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"filter_rules","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"flow_asn_meta","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"flow_ip_meta","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"flow_settings_intents","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"flow_write_receipts","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"geoip_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"internet_path_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_artifacts","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_audit","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_budget_buckets","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_connections","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_credentials","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_diagnostic_cancellations","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_drift","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_grants","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_management_channels","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_oauth_clients","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_oauth_requests","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_operation_checkpoints","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_operation_steps","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_operations","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_owner_revocations","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_path_checks","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_plans","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_portal_events","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_protections","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_read_leases","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_recovery_intents","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_requests","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_routing_forward_charges","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_routing_manual_inverse_charges","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_routing_observations","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_routing_profiles","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_routing_reservation_slots","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_routing_reservations","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_scoped_recovery_intents","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_secrets","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_unauthorized_buckets","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"recursive_routes","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"router_log_collection_state","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"router_log_entries","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"router_log_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"scheduler_runs","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_acks","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_archives","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_executions","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_intents","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_quarantines","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_receipts","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_storage_admissions","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_storage_usage","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_execution_payloads","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_execution_quarantines","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_execution_stages","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_executions","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_storage_admissions","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_storage_usage","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"servers","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"servers_api_ping_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"traffic_collection_state","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"traffic_flow_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"traffic_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"uptime_probes","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"uptime_settings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"uptime_speed_probes","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"uptime_speed_test_runs","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"user_interface_bindings","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=arwd/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"vrf_production_tickets","reloptions":null,"relrowsecurity":false},{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=r/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"data_migration","reloptions":null,"relrowsecurity":false}],"triggers":[{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.acme_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"acme_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_confirm_pending FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_confirm_pending","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_cursor FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_cursor","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_prev_live FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_prev_live","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_state FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_groups FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_groups","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_history FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_history","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_outbox FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_outbox","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_rule_conditions FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_rule_conditions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_rule_targets FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_rule_targets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_rules FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_telegram_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_telegram_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.app_users FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"app_users","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.backup_entries FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"backup_entries","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.backup_schedule_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"backup_schedule_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.backup_storage_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"backup_storage_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.certificate_issue_jobs FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"certificate_issue_jobs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.certificate_renew_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"certificate_renew_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.config_revisions FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"config_revisions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.data_migration FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"data_migration","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.events FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"events","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.evobgp_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"evobgp_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.filter_rules FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"filter_rules","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_asn_meta FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_asn_meta","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_buckets FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_daily_dims FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_daily_dims","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_daily_facts FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_daily_facts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_hour_facts FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_hour_facts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_ip_meta FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_ip_meta","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_minute_dims FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_minute_dims","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_minute_stats FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_minute_stats","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_settings_intents FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_settings_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_write_receipts FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_write_receipts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.geoip_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"geoip_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.internet_path_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"internet_path_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.internet_path_snapshots FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"internet_path_snapshots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_artifacts FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_artifacts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_audit FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_audit","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_budget_buckets FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_budget_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_connections FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_credentials FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_diagnostic_cancellations FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_drift FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_drift","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_grants FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_grants","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_management_channels FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_management_channels","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_oauth_clients FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_oauth_clients","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_oauth_requests FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_oauth_requests","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operation_checkpoints FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operation_checkpoints","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operation_steps FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operation_steps","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operations FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_owner_revocations FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_owner_revocations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_path_checks FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_path_checks","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_plans FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_portal_events FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_portal_events","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_protections FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_protections","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_read_leases FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_read_leases","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_recovery_intents FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_recovery_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_requests FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_requests","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_forward_charges FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_forward_charges","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_manual_inverse_charges FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_manual_inverse_charges","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_observations FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_observations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_profiles FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_reservation_slots FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_reservation_slots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_reservations FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_scoped_recovery_intents FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_secrets FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_secrets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_unauthorized_buckets FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_unauthorized_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.recursive_routes FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"recursive_routes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.router_log_collection_state FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"router_log_collection_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.router_log_entries FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"router_log_entries","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.router_log_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"router_log_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.scheduler_runs FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"scheduler_runs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.schema_migrations FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"schema_migrations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_acks FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_archives FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_executions FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_intents FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_quarantines FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_quarantines","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_receipts FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_storage_admissions FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_storage_usage FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_storage_usage","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_payloads FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_execution_payloads","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_quarantines FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_execution_quarantines","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_stages FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_execution_stages","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_executions FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_storage_admissions FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_storage_admissions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_storage_usage FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_storage_usage","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_snapshots FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_snapshots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.servers FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.servers_api_ping_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"servers_api_ping_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.servers_rest_ping_samples FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"servers_rest_ping_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_collection_state FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_collection_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_flow_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_flow_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_samples FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_probe_samples FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_probe_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_probes FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_probes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_resource_samples FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_resource_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_settings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_speed_probes FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_speed_probes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_speed_test_runs FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_speed_test_runs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.user_interface_bindings FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"user_interface_bindings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.vrf_production_tickets FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_application_statement_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":30,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.acme_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"acme_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_engine_confirm_pending FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_engine_confirm_pending","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_engine_cursor FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_engine_cursor","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_engine_prev_live FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_engine_prev_live","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_engine_state FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_engine_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_groups FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_groups","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_history FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_history","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_outbox FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_outbox","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_rule_conditions FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_rule_conditions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_rule_targets FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_rule_targets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_rules FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.alert_telegram_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"alert_telegram_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.app_users FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"app_users","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.backup_entries FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"backup_entries","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.backup_schedule_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"backup_schedule_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.backup_storage_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"backup_storage_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.certificate_issue_jobs FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"certificate_issue_jobs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.certificate_renew_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"certificate_renew_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.config_revisions FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"config_revisions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.data_migration FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"data_migration","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.events FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"events","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.evobgp_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"evobgp_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.filter_rules FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"filter_rules","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_asn_meta FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_asn_meta","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_buckets FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_daily_dims FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_daily_dims","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_daily_facts FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_daily_facts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_hour_facts FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_hour_facts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_ip_meta FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_ip_meta","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_minute_dims FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_minute_dims","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_minute_stats FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_minute_stats","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_settings_intents FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_settings_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.flow_write_receipts FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"flow_write_receipts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.geoip_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"geoip_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.internet_path_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"internet_path_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.internet_path_snapshots FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"internet_path_snapshots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_artifacts FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_artifacts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_audit FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_audit","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_budget_buckets FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_budget_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_connections FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_credentials FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_diagnostic_cancellations FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_drift FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_drift","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_grants FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_grants","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_management_channels FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_management_channels","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_oauth_clients FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_oauth_clients","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_oauth_requests FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_oauth_requests","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_operation_checkpoints FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_operation_checkpoints","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_operation_steps FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_operation_steps","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_operations FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_owner_revocations FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_owner_revocations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_path_checks FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_path_checks","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_plans FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_portal_events FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_portal_events","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_protections FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_protections","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_read_leases FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_read_leases","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_recovery_intents FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_recovery_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_requests FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_requests","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_routing_forward_charges FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_routing_forward_charges","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_routing_manual_inverse_charges FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_routing_manual_inverse_charges","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_routing_observations FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_routing_observations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_routing_profiles FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_routing_reservation_slots FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_routing_reservation_slots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_routing_reservations FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_scoped_recovery_intents FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_secrets FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_secrets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.mcp_unauthorized_buckets FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_unauthorized_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.recursive_routes FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"recursive_routes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.router_log_collection_state FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"router_log_collection_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.router_log_entries FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"router_log_entries","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.router_log_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"router_log_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.scheduler_runs FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"scheduler_runs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.schema_migrations FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"schema_migrations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_acks FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_archives FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_executions FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_intents FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_quarantines FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_quarantines","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_receipts FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_storage_admissions FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_poll_storage_usage FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_storage_usage","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_probe_execution_payloads FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_execution_payloads","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_probe_execution_quarantines FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_execution_quarantines","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_probe_execution_stages FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_execution_stages","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_probe_executions FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_probe_storage_admissions FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_storage_admissions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_probe_storage_usage FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_storage_usage","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.server_snapshots FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_snapshots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.servers FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.servers_api_ping_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"servers_api_ping_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.servers_rest_ping_samples FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"servers_rest_ping_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.traffic_collection_state FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"traffic_collection_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.traffic_flow_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"traffic_flow_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.traffic_samples FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"traffic_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.traffic_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"traffic_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.uptime_probe_samples FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"uptime_probe_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.uptime_probes FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"uptime_probes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.uptime_resource_samples FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"uptime_resource_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.uptime_settings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"uptime_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.uptime_speed_probes FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"uptime_speed_probes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.uptime_speed_test_runs FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"uptime_speed_test_runs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.user_interface_bindings FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"user_interface_bindings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.vrf_production_tickets FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"candidate_truncate_preadmitted","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_diagnostic_cancellation_immutable BEFORE DELETE OR UPDATE ON public.mcp_diagnostic_cancellations FOR EACH ROW EXECUTE FUNCTION mcp_diagnostic_cancellation_immutable()","function":"public.mcp_diagnostic_cancellation_immutable()","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_diagnostic_cancellation_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_routing_forward_charge_immutable BEFORE DELETE OR UPDATE ON public.mcp_routing_forward_charges FOR EACH ROW EXECUTE FUNCTION mcp_routing_forward_charge_immutable()","function":"public.mcp_routing_forward_charge_immutable()","parent":false,"relname":"mcp_routing_forward_charges","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_routing_forward_charge_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_routing_forward_charge_no_truncate BEFORE TRUNCATE ON public.mcp_routing_forward_charges FOR EACH STATEMENT EXECUTE FUNCTION mcp_routing_forward_charge_immutable()","function":"public.mcp_routing_forward_charge_immutable()","parent":false,"relname":"mcp_routing_forward_charges","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_routing_forward_charge_no_truncate","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_routing_forward_charge_source BEFORE INSERT ON public.mcp_routing_forward_charges FOR EACH ROW EXECUTE FUNCTION mcp_routing_forward_charge_source()","function":"public.mcp_routing_forward_charge_source()","parent":false,"relname":"mcp_routing_forward_charges","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_routing_forward_charge_source","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_routing_manual_inverse_charge_immutable BEFORE DELETE OR UPDATE ON public.mcp_routing_manual_inverse_charges FOR EACH ROW EXECUTE FUNCTION mcp_routing_manual_inverse_charge_immutable()","function":"public.mcp_routing_manual_inverse_charge_immutable()","parent":false,"relname":"mcp_routing_manual_inverse_charges","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_routing_manual_inverse_charge_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_routing_manual_inverse_charge_no_truncate BEFORE TRUNCATE ON public.mcp_routing_manual_inverse_charges FOR EACH STATEMENT EXECUTE FUNCTION mcp_routing_manual_inverse_charge_immutable()","function":"public.mcp_routing_manual_inverse_charge_immutable()","parent":false,"relname":"mcp_routing_manual_inverse_charges","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_routing_manual_inverse_charge_no_truncate","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_routing_manual_inverse_charge_source BEFORE INSERT ON public.mcp_routing_manual_inverse_charges FOR EACH ROW EXECUTE FUNCTION mcp_routing_manual_inverse_charge_source()","function":"public.mcp_routing_manual_inverse_charge_source()","parent":false,"relname":"mcp_routing_manual_inverse_charges","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_routing_manual_inverse_charge_source","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_scoped_recovery_manual_version BEFORE INSERT ON public.mcp_scoped_recovery_intents FOR EACH ROW EXECUTE FUNCTION mcp_scoped_recovery_manual_version()","function":"public.mcp_scoped_recovery_manual_version()","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_scoped_recovery_manual_version","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mm_routing_endpoint_epoch BEFORE UPDATE ON public.servers FOR EACH ROW EXECUTE FUNCTION mm_routing_endpoint_epoch()","function":"public.mm_routing_endpoint_epoch()","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mm_routing_endpoint_epoch","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":19,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.acme_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"acme_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_confirm_pending FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_confirm_pending","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_cursor FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_cursor","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_prev_live FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_prev_live","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_engine_state FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_engine_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_groups FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_groups","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_history FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_history","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_outbox FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_outbox","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_rule_conditions FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_rule_conditions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_rule_targets FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_rule_targets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_rules FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.alert_telegram_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"alert_telegram_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.app_users FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"app_users","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.backup_entries FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"backup_entries","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.backup_schedule_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"backup_schedule_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.backup_storage_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"backup_storage_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.certificate_issue_jobs FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"certificate_issue_jobs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.certificate_renew_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"certificate_renew_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.config_revisions FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"config_revisions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.data_migration FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"data_migration","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.events FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"events","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.evobgp_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"evobgp_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.filter_rules FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"filter_rules","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_asn_meta FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_asn_meta","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_buckets FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_daily_dims FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_daily_dims","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_daily_facts FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_daily_facts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_hour_facts FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_hour_facts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_ip_meta FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_ip_meta","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_minute_dims FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_minute_dims","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_minute_stats FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_minute_stats","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_settings_intents FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_settings_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.flow_write_receipts FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"flow_write_receipts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.geoip_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"geoip_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.internet_path_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"internet_path_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.internet_path_snapshots FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"internet_path_snapshots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_artifacts FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_artifacts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_audit FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_audit","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_budget_buckets FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_budget_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_connections FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_credentials FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_diagnostic_cancellations FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_drift FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_drift","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_grants FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_grants","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_management_channels FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_management_channels","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_oauth_clients FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_oauth_clients","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_oauth_requests FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_oauth_requests","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operation_checkpoints FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operation_checkpoints","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operation_steps FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operation_steps","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operations FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_owner_revocations FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_owner_revocations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_path_checks FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_path_checks","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_plans FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_portal_events FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_portal_events","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_protections FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_protections","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_read_leases FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_read_leases","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_recovery_intents FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_recovery_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_requests FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_requests","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_forward_charges FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_forward_charges","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_manual_inverse_charges FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_manual_inverse_charges","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_observations FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_observations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_profiles FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_reservation_slots FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_reservation_slots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_routing_reservations FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_scoped_recovery_intents FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_secrets FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_secrets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_unauthorized_buckets FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_unauthorized_buckets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.recursive_routes FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"recursive_routes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.router_log_collection_state FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"router_log_collection_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.router_log_entries FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"router_log_entries","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.router_log_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"router_log_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.scheduler_runs FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"scheduler_runs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.schema_migrations FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"schema_migrations","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_acks FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_archives FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_executions FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_intents FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_quarantines FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_quarantines","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_receipts FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_storage_admissions FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_storage_usage FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_storage_usage","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_payloads FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_execution_payloads","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_quarantines FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_execution_quarantines","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_stages FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_execution_stages","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_executions FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_storage_admissions FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_storage_admissions","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_storage_usage FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_storage_usage","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_snapshots FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_snapshots","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.servers FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.servers_api_ping_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"servers_api_ping_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.servers_rest_ping_samples FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"servers_rest_ping_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_collection_state FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_collection_state","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_flow_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_flow_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_samples FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.traffic_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"traffic_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_probe_samples FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_probe_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_probes FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_probes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_resource_samples FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_resource_samples","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_settings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_speed_probes FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_speed_probes","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.uptime_speed_test_runs FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"uptime_speed_test_runs","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.user_interface_bindings FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"user_interface_bindings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.vrf_production_tickets FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_partition_writer_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_ack_immutable BEFORE DELETE OR UPDATE ON public.server_poll_acks FOR EACH ROW EXECUTE FUNCTION server_poll_journal_immutable()","function":"public.server_poll_journal_immutable()","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_ack_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_archive_immutable BEFORE DELETE OR UPDATE ON public.server_poll_archives FOR EACH ROW EXECUTE FUNCTION server_poll_archive_immutable()","function":"public.server_poll_archive_immutable()","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_archive_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_archive_insert_finish AFTER INSERT ON public.server_poll_archives FOR EACH ROW EXECUTE FUNCTION server_poll_archive_insert_finish()","function":"public.server_poll_archive_insert_finish()","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_archive_insert_finish","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_archive_insert_guard BEFORE INSERT ON public.server_poll_archives FOR EACH ROW EXECUTE FUNCTION server_poll_archive_insert_guard()","function":"public.server_poll_archive_insert_guard()","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_archive_insert_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_compacted_ack_guard BEFORE INSERT ON public.server_poll_acks FOR EACH ROW EXECUTE FUNCTION server_poll_compacted_row_guard()","function":"public.server_poll_compacted_row_guard()","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_compacted_ack_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_compacted_anchor_guard BEFORE INSERT ON public.server_poll_executions FOR EACH ROW EXECUTE FUNCTION server_poll_compacted_row_guard()","function":"public.server_poll_compacted_row_guard()","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_compacted_anchor_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_compacted_intent_guard BEFORE INSERT ON public.server_poll_intents FOR EACH ROW EXECUTE FUNCTION server_poll_compacted_row_guard()","function":"public.server_poll_compacted_row_guard()","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_compacted_intent_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_compacted_quarantine_guard BEFORE INSERT ON public.server_poll_quarantines FOR EACH ROW EXECUTE FUNCTION server_poll_compacted_row_guard()","function":"public.server_poll_compacted_row_guard()","parent":false,"relname":"server_poll_quarantines","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_compacted_quarantine_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_compacted_receipt_guard BEFORE INSERT ON public.server_poll_receipts FOR EACH ROW EXECUTE FUNCTION server_poll_compacted_row_guard()","function":"public.server_poll_compacted_row_guard()","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_compacted_receipt_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_execution_immutable BEFORE DELETE OR UPDATE ON public.server_poll_executions FOR EACH ROW EXECUTE FUNCTION server_poll_journal_immutable()","function":"public.server_poll_journal_immutable()","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_execution_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_intent_immutable BEFORE DELETE OR UPDATE ON public.server_poll_intents FOR EACH ROW EXECUTE FUNCTION server_poll_journal_immutable()","function":"public.server_poll_journal_immutable()","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_intent_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_quarantine_immutable BEFORE DELETE OR UPDATE ON public.server_poll_quarantines FOR EACH ROW EXECUTE FUNCTION server_poll_journal_immutable()","function":"public.server_poll_journal_immutable()","parent":false,"relname":"server_poll_quarantines","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_quarantine_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_receipt_immutable BEFORE DELETE OR UPDATE ON public.server_poll_receipts FOR EACH ROW EXECUTE FUNCTION server_poll_journal_immutable()","function":"public.server_poll_journal_immutable()","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_receipt_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_settings_epoch BEFORE INSERT OR UPDATE ON public.internet_path_settings FOR EACH ROW EXECUTE FUNCTION server_poll_settings_epoch_guard()","function":"public.server_poll_settings_epoch_guard()","parent":false,"relname":"internet_path_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_settings_epoch","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":23,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_settings_epoch BEFORE INSERT OR UPDATE ON public.traffic_settings FOR EACH ROW EXECUTE FUNCTION server_poll_settings_epoch_guard()","function":"public.server_poll_settings_epoch_guard()","parent":false,"relname":"traffic_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_settings_epoch","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":23,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_settings_epoch BEFORE INSERT OR UPDATE ON public.uptime_settings FOR EACH ROW EXECUTE FUNCTION server_poll_settings_epoch_guard()","function":"public.server_poll_settings_epoch_guard()","parent":false,"relname":"uptime_settings","tgdeferrable":false,"tgenabled":"A","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_settings_epoch","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":23,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_ack_guard BEFORE INSERT ON public.server_poll_acks FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_guard()","function":"public.server_poll_storage_row_guard()","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_ack_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_ack_meter AFTER INSERT ON public.server_poll_acks FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_meter()","function":"public.server_poll_storage_row_meter()","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_ack_meter","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_admission_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_storage_admissions FOR EACH ROW EXECUTE FUNCTION server_poll_storage_admission_guard()","function":"public.server_poll_storage_admission_guard()","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_admission_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_anchor_guard BEFORE INSERT ON public.server_poll_executions FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_guard()","function":"public.server_poll_storage_row_guard()","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_anchor_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_anchor_meter AFTER INSERT ON public.server_poll_executions FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_meter()","function":"public.server_poll_storage_row_meter()","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_anchor_meter","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_intent_guard BEFORE INSERT ON public.server_poll_intents FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_guard()","function":"public.server_poll_storage_row_guard()","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_intent_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_intent_meter AFTER INSERT ON public.server_poll_intents FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_meter()","function":"public.server_poll_storage_row_meter()","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_intent_meter","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_quarantine_guard BEFORE INSERT ON public.server_poll_quarantines FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_guard()","function":"public.server_poll_storage_row_guard()","parent":false,"relname":"server_poll_quarantines","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_quarantine_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_quarantine_meter AFTER INSERT ON public.server_poll_quarantines FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_meter()","function":"public.server_poll_storage_row_meter()","parent":false,"relname":"server_poll_quarantines","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_quarantine_meter","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_receipt_guard BEFORE INSERT ON public.server_poll_receipts FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_guard()","function":"public.server_poll_storage_row_guard()","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_receipt_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_storage_receipt_meter AFTER INSERT ON public.server_poll_receipts FOR EACH ROW EXECUTE FUNCTION server_poll_storage_row_meter()","function":"public.server_poll_storage_row_meter()","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_storage_receipt_meter","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_execution_immutable BEFORE DELETE OR UPDATE ON public.server_probe_executions FOR EACH ROW EXECUTE FUNCTION server_probe_execution_immutable()","function":"public.server_probe_execution_immutable()","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_execution_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_quarantine_immutable BEFORE DELETE OR UPDATE ON public.server_probe_execution_quarantines FOR EACH ROW EXECUTE FUNCTION server_probe_quarantine_immutable()","function":"public.server_probe_quarantine_immutable()","parent":false,"relname":"server_probe_execution_quarantines","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_quarantine_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_stage_immutable BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_stages FOR EACH ROW EXECUTE FUNCTION server_probe_stage_immutable()","function":"public.server_probe_stage_immutable()","parent":false,"relname":"server_probe_execution_stages","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_stage_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_storage_admission_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_storage_admissions FOR EACH ROW EXECUTE FUNCTION server_probe_storage_admission_guard()","function":"public.server_probe_storage_admission_guard()","parent":false,"relname":"server_probe_storage_admissions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_storage_admission_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_storage_anchor_guard BEFORE INSERT OR UPDATE ON public.server_probe_executions FOR EACH ROW EXECUTE FUNCTION server_probe_storage_anchor_guard()","function":"public.server_probe_storage_anchor_guard()","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_storage_anchor_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":23,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_storage_payload_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_execution_payloads FOR EACH ROW EXECUTE FUNCTION server_probe_storage_payload_guard()","function":"public.server_probe_storage_payload_guard()","parent":false,"relname":"server_probe_execution_payloads","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_storage_payload_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_storage_quarantine_guard BEFORE INSERT ON public.server_probe_execution_quarantines FOR EACH ROW EXECUTE FUNCTION server_probe_storage_quarantine_guard()","function":"public.server_probe_storage_quarantine_guard()","parent":false,"relname":"server_probe_execution_quarantines","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_storage_quarantine_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_storage_stage_guard BEFORE INSERT OR UPDATE ON public.server_probe_execution_stages FOR EACH ROW EXECUTE FUNCTION server_probe_storage_stage_guard()","function":"public.server_probe_storage_stage_guard()","parent":false,"relname":"server_probe_execution_stages","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_storage_stage_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":23,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.alert_groups FROM alert_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_setnull_del\"()","function":"pg_catalog#1650","parent":false,"relname":"alert_groups","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.alert_rules FROM alert_rule_conditions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.alert_rules FROM alert_rule_targets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.app_users FROM user_interface_bindings NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"app_users","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_artifacts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_budget_buckets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_credentials NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_diagnostic_cancellations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_grants NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_oauth_requests NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_path_checks NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_read_leases NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_requests NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_setnull_del\"()","function":"pg_catalog#1650","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM vrf_production_tickets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_credentials FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_credentials FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_oauth_clients FROM mcp_oauth_requests NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_oauth_clients","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_diagnostic_cancellations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_drift NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_operation_checkpoints NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_operation_steps NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_operations FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_plans FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_plans FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_plans FROM vrf_production_tickets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_routing_profiles FROM mcp_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_routing_profiles FROM mcp_routing_reservation_slots NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_routing_profiles FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_routing_profiles FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_routing_reservations FROM mcp_routing_reservation_slots NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_routing_reservations FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.server_poll_executions FROM server_poll_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.server_poll_executions FROM server_poll_receipts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.server_poll_intents FROM server_poll_acks NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.server_poll_storage_admissions FROM server_poll_archives NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.server_probe_executions FROM server_probe_execution_payloads NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.server_probe_executions FROM server_probe_execution_stages NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM backup_entries NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_setnull_del\"()","function":"pg_catalog#1650","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM certificate_issue_jobs NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM config_revisions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM filter_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM flow_buckets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM flow_daily_dims NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM flow_daily_facts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM flow_hour_facts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM flow_minute_dims NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM flow_minute_stats NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM mcp_management_channels NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM recursive_routes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM server_snapshots NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM servers_rest_ping_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM traffic_collection_state NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM traffic_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM uptime_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM uptime_resource_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM uptime_speed_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM uptime_speed_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM uptime_speed_test_runs NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM uptime_speed_test_runs NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM user_interface_bindings NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.servers FROM vrf_production_tickets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.uptime_probes FROM uptime_probe_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"uptime_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":9,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.alert_groups FROM alert_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"alert_groups","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.alert_rules FROM alert_rule_conditions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.alert_rules FROM alert_rule_targets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.app_users FROM user_interface_bindings NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"app_users","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_artifacts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_budget_buckets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_credentials NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_diagnostic_cancellations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_grants NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_oauth_requests NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_path_checks NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_read_leases NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_requests NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_connections FROM vrf_production_tickets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_connections","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_credentials FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_credentials FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_oauth_clients FROM mcp_oauth_requests NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_oauth_clients","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_diagnostic_cancellations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_drift NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_operation_checkpoints NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_operation_steps NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_plans FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_plans FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_plans FROM vrf_production_tickets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_routing_profiles FROM mcp_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_routing_profiles FROM mcp_routing_reservation_slots NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_routing_profiles FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_routing_profiles FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_routing_reservations FROM mcp_routing_reservation_slots NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.mcp_routing_reservations FROM mcp_scoped_recovery_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.server_poll_executions FROM server_poll_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.server_poll_executions FROM server_poll_receipts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"server_poll_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.server_poll_intents FROM server_poll_acks NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.server_poll_storage_admissions FROM server_poll_archives NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.server_probe_executions FROM server_probe_execution_payloads NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.server_probe_executions FROM server_probe_execution_stages NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM backup_entries NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM certificate_issue_jobs NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM config_revisions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM filter_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM flow_buckets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM flow_daily_dims NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM flow_daily_facts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM flow_hour_facts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM flow_minute_dims NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM flow_minute_stats NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM mcp_management_channels NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM recursive_routes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM server_snapshots NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM servers_rest_ping_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM traffic_collection_state NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM traffic_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM uptime_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM uptime_resource_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM uptime_speed_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM uptime_speed_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM uptime_speed_test_runs NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM uptime_speed_test_runs NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM user_interface_bindings NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.servers FROM vrf_production_tickets NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"servers","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER UPDATE ON public.uptime_probes FROM uptime_probe_samples NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"uptime_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_a_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.alert_rule_conditions FROM alert_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"alert_rule_conditions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.alert_rule_targets FROM alert_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"alert_rule_targets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.alert_rules FROM alert_groups NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.backup_entries FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"backup_entries","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.certificate_issue_jobs FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"certificate_issue_jobs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.config_revisions FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"config_revisions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.filter_rules FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"filter_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.flow_buckets FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"flow_buckets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.flow_daily_dims FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"flow_daily_dims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.flow_daily_facts FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"flow_daily_facts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.flow_hour_facts FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"flow_hour_facts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.flow_minute_dims FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"flow_minute_dims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.flow_minute_stats FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"flow_minute_stats","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_artifacts FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_artifacts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_budget_buckets FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_budget_buckets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_credentials FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_diagnostic_cancellations FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_diagnostic_cancellations FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_drift FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_drift","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_grants FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_grants","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_management_channels FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_management_channels","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_oauth_requests FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_oauth_requests","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_oauth_requests FROM mcp_oauth_clients NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_oauth_requests","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_operation_checkpoints FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_operation_checkpoints","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_operation_steps FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_operation_steps","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_operations FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_operations FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_path_checks FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_path_checks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_plans FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_read_leases FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_read_leases","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_recovery_intents FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_recovery_intents FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_requests FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_requests","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_profiles FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_profiles FROM mcp_credentials NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_reservation_slots FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_reservation_slots","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_reservation_slots FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_reservation_slots","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_reservations FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_reservations FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_reservations FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_routing_reservations FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_scoped_recovery_intents FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_scoped_recovery_intents FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_scoped_recovery_intents FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.mcp_scoped_recovery_intents FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.recursive_routes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"recursive_routes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_poll_acks FROM server_poll_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_poll_archives FROM server_poll_storage_admissions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_poll_intents FROM server_poll_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_poll_receipts FROM server_poll_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_probe_execution_payloads FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_probe_execution_payloads","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_probe_execution_stages FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_probe_execution_stages","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_probe_executions FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_probe_executions FROM mcp_credentials NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.server_snapshots FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_snapshots","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.servers_rest_ping_samples FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"servers_rest_ping_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.traffic_collection_state FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"traffic_collection_state","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.traffic_samples FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"traffic_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.uptime_probe_samples FROM uptime_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"uptime_probe_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.uptime_probes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"uptime_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.uptime_resource_samples FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"uptime_resource_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.uptime_speed_probes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"uptime_speed_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.uptime_speed_probes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"uptime_speed_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.uptime_speed_test_runs FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"uptime_speed_test_runs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.uptime_speed_test_runs FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"uptime_speed_test_runs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.user_interface_bindings FROM app_users NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"user_interface_bindings","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.user_interface_bindings FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"user_interface_bindings","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.vrf_production_tickets FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.vrf_production_tickets FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER INSERT ON public.vrf_production_tickets FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":5,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.alert_rule_conditions FROM alert_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"alert_rule_conditions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.alert_rule_targets FROM alert_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"alert_rule_targets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.alert_rules FROM alert_groups NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"alert_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.backup_entries FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"backup_entries","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.certificate_issue_jobs FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"certificate_issue_jobs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.config_revisions FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"config_revisions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.filter_rules FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"filter_rules","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.flow_buckets FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"flow_buckets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.flow_daily_dims FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"flow_daily_dims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.flow_daily_facts FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"flow_daily_facts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.flow_hour_facts FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"flow_hour_facts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.flow_minute_dims FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"flow_minute_dims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.flow_minute_stats FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"flow_minute_stats","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_artifacts FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_artifacts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_budget_buckets FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_budget_buckets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_credentials FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_credentials","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_diagnostic_cancellations FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_diagnostic_cancellations FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_diagnostic_cancellations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_drift FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_drift","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_grants FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_grants","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_management_channels FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_management_channels","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_oauth_requests FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_oauth_requests","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_oauth_requests FROM mcp_oauth_clients NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_oauth_requests","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_operation_checkpoints FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_operation_checkpoints","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_operation_steps FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_operation_steps","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_operations FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_operations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_path_checks FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_path_checks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_plans FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_plans","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_read_leases FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_read_leases","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_recovery_intents FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_recovery_intents FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_requests FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_requests","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_profiles FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_profiles FROM mcp_credentials NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_profiles","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_reservation_slots FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_reservation_slots","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_reservation_slots FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_reservation_slots","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_reservations FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_reservations FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_reservations FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_routing_reservations FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_routing_reservations","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_scoped_recovery_intents FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_scoped_recovery_intents FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_scoped_recovery_intents FROM mcp_routing_profiles NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.mcp_scoped_recovery_intents FROM mcp_routing_reservations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_scoped_recovery_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.recursive_routes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"recursive_routes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_poll_acks FROM server_poll_intents NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_poll_acks","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_poll_archives FROM server_poll_storage_admissions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_poll_intents FROM server_poll_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_poll_intents","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_poll_receipts FROM server_poll_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_poll_receipts","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_probe_execution_payloads FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_probe_execution_payloads","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_probe_execution_stages FROM server_probe_executions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_probe_execution_stages","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_probe_executions FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_probe_executions FROM mcp_credentials NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_probe_executions","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.server_snapshots FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_snapshots","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.servers_rest_ping_samples FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"servers_rest_ping_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.traffic_collection_state FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"traffic_collection_state","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.traffic_samples FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"traffic_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.uptime_probe_samples FROM uptime_probes NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"uptime_probe_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.uptime_probes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"uptime_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.uptime_resource_samples FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"uptime_resource_samples","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.uptime_speed_probes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"uptime_speed_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.uptime_speed_probes FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"uptime_speed_probes","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.uptime_speed_test_runs FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"uptime_speed_test_runs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.uptime_speed_test_runs FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"uptime_speed_test_runs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.user_interface_bindings FROM app_users NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"user_interface_bindings","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.user_interface_bindings FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"user_interface_bindings","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.vrf_production_tickets FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.vrf_production_tickets FROM mcp_plans NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_c_{oid}\" AFTER UPDATE ON public.vrf_production_tickets FROM servers NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"vrf_production_tickets","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":true,"tgname":"RI_ConstraintTrigger_c_{oid}","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER server_poll_admission_final_guard AFTER INSERT OR DELETE ON public.server_poll_storage_admissions DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION server_poll_archive_final_guard()","function":"public.server_poll_archive_final_guard()","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":true,"tgenabled":"O","tginitdeferred":true,"tgisinternal":false,"tgname":"server_poll_admission_final_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":13,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER server_poll_archive_final_guard AFTER INSERT OR DELETE ON public.server_poll_archives DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION server_poll_archive_final_guard()","function":"public.server_poll_archive_final_guard()","parent":false,"relname":"server_poll_archives","tgdeferrable":true,"tgenabled":"O","tginitdeferred":true,"tgisinternal":false,"tgname":"server_poll_archive_final_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":13,"unconditional":true},{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER server_poll_compact_final_guard AFTER UPDATE ON public.server_poll_storage_admissions DEFERRABLE INITIALLY DEFERRED FOR EACH ROW WHEN ((old.storage_state IS DISTINCT FROM new.storage_state)) EXECUTE FUNCTION server_poll_archive_final_guard()","function":"public.server_poll_archive_final_guard()","parent":false,"relname":"server_poll_storage_admissions","tgdeferrable":true,"tgenabled":"O","tginitdeferred":true,"tgisinternal":false,"tgname":"server_poll_compact_final_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":false}]} diff --git a/backend/scripts/writer-profile/fixed44-build-sql.ts b/backend/scripts/writer-profile/fixed44-build-sql.ts new file mode 100644 index 00000000..1aa5e244 --- /dev/null +++ b/backend/scripts/writer-profile/fixed44-build-sql.ts @@ -0,0 +1,2 @@ +// Fixed query from accepted43, role-literal parameters only; limits/framing unchanged. Clean44 SQL proof pending. +export const FIXED44_CATALOGUE_BUILD_SQL="WITH c_tables AS NOT MATERIALIZED (SELECT c.oid::text AS oid,n.nspname AS namespace,c.relname,c.relkind,c.relispartition,c.relpersistence,c.relrowsecurity,c.relforcerowsecurity,c.reloptions,\n pg_catalog.pg_get_userbyid(c.relowner) AS owner,c.relacl::text[] AS acl,pg_catalog.pg_get_expr(c.relpartbound,c.oid) AS partition_bound,\n pg_catalog.pg_get_partkeydef(c.oid) AS partition_key FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n WHERE n.nspname='public' AND c.relkind IN('r','p')),\nc_columns AS NOT MATERIALIZED (SELECT c.relname,a.attnum::int,a.attname,pg_catalog.format_type(a.atttypid,a.atttypmod) AS type,\n a.attnotnull,a.attidentity,a.attgenerated,a.attstorage,a.attcompression,a.attacl::text[] AS acl,pg_catalog.pg_get_expr(d.adbin,d.adrelid) AS default_expression\n FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON c.oid=a.attrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum\n WHERE n.nspname='public' AND c.relkind IN('r','p') AND a.attnum>0 AND NOT a.attisdropped),\nc_identities AS NOT MATERIALIZED (SELECT c.relname,c.oid::text AS relation_oid,a.attnum::int AS column_number,a.attname,a.attidentity,\n pg_catalog.pg_get_serial_sequence(pg_catalog.format('%I.%I',n.nspname,c.relname),a.attname) AS sequence_name\n FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON c.oid=a.attrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n WHERE n.nspname='public' AND c.relkind IN('r','p') AND NOT c.relispartition AND a.attnum>0 AND NOT a.attisdropped AND a.attidentity<>'' LIMIT 33),\nc_sequences AS NOT MATERIALIZED (SELECT s.oid::text AS oid,n.nspname AS namespace,s.relname,pg_catalog.pg_get_userbyid(s.relowner) AS owner,\n s.relacl::text[] AS acl,q.seqtypid::int AS type_oid,q.seqstart::text,q.seqincrement::text,q.seqmax::text,q.seqmin::text,q.seqcache::text,q.seqcycle,\n d.refobjid::text AS owner_relation_oid,d.refobjsubid::int AS owner_column,d.deptype\n FROM pg_catalog.pg_class s JOIN pg_catalog.pg_namespace n ON n.oid=s.relnamespace JOIN pg_catalog.pg_sequence q ON q.seqrelid=s.oid\n LEFT JOIN pg_catalog.pg_depend d ON d.classid='pg_catalog.pg_class'::regclass AND d.objid=s.oid AND d.objsubid=0 AND d.refclassid='pg_catalog.pg_class'::regclass AND d.refobjsubid>0 AND d.deptype IN('a','i')\n WHERE n.nspname='public' LIMIT 33),\nc_functions AS NOT MATERIALIZED (SELECT p.oid::text,n.nspname AS namespace,p.proname,pg_catalog.pg_get_function_identity_arguments(p.oid) AS arguments,\n p.prokind,p.prosecdef,p.proconfig,p.proacl::text[] AS acl,pg_catalog.pg_get_userbyid(p.proowner) AS owner,l.lanname AS language,\n pg_catalog.pg_get_functiondef(p.oid) AS definition FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace\n JOIN pg_catalog.pg_language l ON l.oid=p.prolang WHERE n.nspname IN('public','mmapp_control') AND p.prokind IN('f','p') LIMIT 257),\nc_triggers AS NOT MATERIALIZED (SELECT t.oid::text AS oid,c.relname,t.tgname,t.tgtype::int,t.tgenabled,t.tgisinternal,t.tgfoid::text,t.tgparentid::text,t.tgconstraint::text,t.tgdeferrable,t.tginitdeferred,t.tgoldtable,t.tgnewtable,\n t.tgnargs,octet_length(t.tgargs)::int AS arg_bytes,t.tgqual IS NULL AS unconditional,pg_catalog.pg_get_triggerdef(t.oid) AS definition\n FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n WHERE n.nspname='public'),\nc_inheritance AS NOT MATERIALIZED (SELECT p.relname AS parent,c.relname AS child FROM pg_catalog.pg_inherits i\n JOIN pg_catalog.pg_class p ON p.oid=i.inhparent JOIN pg_catalog.pg_class c ON c.oid=i.inhrelid JOIN pg_catalog.pg_namespace n ON n.oid=p.relnamespace\n WHERE n.nspname='public' AND p.relkind IN('r','p')),\nc_constraints AS NOT MATERIALIZED (SELECT c.relname,k.conname,k.contype,k.convalidated,k.condeferrable,k.condeferred,\n pg_catalog.pg_get_constraintdef(k.oid) AS definition FROM pg_catalog.pg_constraint k JOIN pg_catalog.pg_class c ON c.oid=k.conrelid\n JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public'),\nc_indexes AS NOT MATERIALIZED (SELECT c.relname,i.relname AS index_name,x.indisvalid,x.indisready,x.indisunique,x.indisprimary,\n pg_catalog.pg_get_indexdef(i.oid) AS definition FROM pg_catalog.pg_index x JOIN pg_catalog.pg_class c ON c.oid=x.indrelid\n JOIN pg_catalog.pg_class i ON i.oid=x.indexrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n WHERE n.nspname='public'),\nc_schemas AS NOT MATERIALIZED (SELECT nspname,pg_catalog.pg_get_userbyid(nspowner) AS owner,nspacl::text[] AS acl FROM pg_catalog.pg_namespace WHERE nspname IN('public','mmapp_control')),\nc_database AS NOT MATERIALIZED (SELECT datname,pg_catalog.pg_get_userbyid(datdba) AS owner,datacl::text[] AS acl FROM pg_catalog.pg_database WHERE datname=current_database()),\nc_effectiveSchemas AS NOT MATERIALIZED (SELECT r.rolname,n.nspname,pg_catalog.has_schema_privilege(r.oid,n.oid,'USAGE') AS usage,\n pg_catalog.has_schema_privilege(r.oid,n.oid,'CREATE') AS create FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_namespace n\n WHERE r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name) AND n.nspname IN('public','mmapp_control')),\nc_effectiveDatabase AS NOT MATERIALIZED (SELECT r.rolname,pg_catalog.has_database_privilege(r.oid,d.oid,'CONNECT') AS connect,\n pg_catalog.has_database_privilege(r.oid,d.oid,'CREATE') AS create,pg_catalog.has_database_privilege(r.oid,d.oid,'TEMP') AS temporary\n FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_database d WHERE r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name) AND d.datname=current_database()),\nc_memberships AS NOT MATERIALIZED (SELECT role.rolname AS granted_role,member.rolname AS member,grantor.rolname AS grantor,m.admin_option,m.inherit_option,m.set_option\n FROM pg_catalog.pg_auth_members m JOIN pg_catalog.pg_roles role ON role.oid=m.roleid JOIN pg_catalog.pg_roles member ON member.oid=m.member\n JOIN pg_catalog.pg_roles grantor ON grantor.oid=m.grantor LIMIT 257),\nc_effectiveFunctions AS NOT MATERIALIZED (SELECT r.rolname,n.nspname,p.oid::text AS oid,p.proname,\n pg_catalog.pg_get_function_identity_arguments(p.oid) AS arguments,pg_catalog.has_function_privilege(r.oid,p.oid,'EXECUTE') AS execute\n FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace\n WHERE r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name) AND n.nspname IN('public','mmapp_control') AND p.prokind IN('f','p')\n LIMIT 1025),\nc_effectiveSequences AS NOT MATERIALIZED (SELECT r.rolname,c.relname,pg_catalog.has_sequence_privilege(r.oid,c.oid,'USAGE') AS usage,\n pg_catalog.has_sequence_privilege(r.oid,c.oid,'SELECT') AS read,pg_catalog.has_sequence_privilege(r.oid,c.oid,'UPDATE') AS update\n FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n WHERE r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name) AND n.nspname='public' AND c.relkind='S' LIMIT 129),\nc_events AS NOT MATERIALIZED (SELECT e.evtname,e.evtevent,e.evtenabled,pg_catalog.pg_get_userbyid(e.evtowner) AS owner,e.evtfoid::text AS function_oid,e.evttags,\n n.nspname AS function_namespace,p.proname AS function_name FROM pg_catalog.pg_event_trigger e JOIN pg_catalog.pg_proc p ON p.oid=e.evtfoid\n JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace LIMIT 33),\nc_parameters AS NOT MATERIALIZED (SELECT r.rolname,v.name,pg_catalog.has_parameter_privilege(r.oid,v.name,'SET') AS can_set\n FROM pg_catalog.pg_roles r CROSS JOIN(VALUES('event_triggers'),('session_replication_role'))v(name)\n WHERE r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name)),\nc_rawSettings AS NOT MATERIALIZED (SELECT COALESCE(d.datname,'all-databases') AS database,COALESCE(r.rolname,'all-roles') AS role,s.setconfig\n FROM pg_catalog.pg_db_role_setting s LEFT JOIN pg_catalog.pg_database d ON d.oid=s.setdatabase LEFT JOIN pg_catalog.pg_roles r ON r.oid=s.setrole\n WHERE(s.setdatabase=0 OR d.datname=current_database())AND(s.setrole=0 OR r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name)) LIMIT 33),\nc_roles AS NOT MATERIALIZED (SELECT rolname,rolsuper,rolcreatedb,rolcreaterole,rolreplication,rolbypassrls,rolinherit,rolcanlogin FROM pg_catalog.pg_roles WHERE rolname IN($5::pg_catalog.name,$2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name,$4::pg_catalog.name)),\nc_roleGrants AS NOT MATERIALIZED (SELECT r.rolname,c.relname,pg_catalog.has_table_privilege(r.oid,c.oid,'SELECT') AS read,\n pg_catalog.has_table_privilege(r.oid,c.oid,'INSERT') AS insert,pg_catalog.has_table_privilege(r.oid,c.oid,'UPDATE') AS update,\n pg_catalog.has_table_privilege(r.oid,c.oid,'DELETE') AS delete,pg_catalog.has_table_privilege(r.oid,c.oid,'TRUNCATE') AS truncate,\n pg_catalog.has_table_privilege(r.oid,c.oid,'TRIGGER') AS trigger FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_class c\n JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name)\n AND n.nspname='public' AND c.relkind IN('r','p')),\nrecords AS (SELECT 'global'::text AS kind,'identities'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.relname),'[]'::jsonb)) AS value FROM c_identities q\nUNION ALL\nSELECT 'global'::text AS kind,'sequences'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.relname),'[]'::jsonb)) AS value FROM c_sequences q\nUNION ALL\nSELECT 'global'::text AS kind,'functions'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.namespace,q.proname,q.arguments),'[]'::jsonb)) AS value FROM c_functions q\nUNION ALL\nSELECT 'global'::text AS kind,'schemas'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.nspname),'[]'::jsonb)) AS value FROM c_schemas q\nUNION ALL\nSELECT 'global'::text AS kind,'database'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.datname),'[]'::jsonb)) AS value FROM c_database q\nUNION ALL\nSELECT 'global'::text AS kind,'effectiveSchemas'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.rolname,q.nspname),'[]'::jsonb)) AS value FROM c_effectiveSchemas q\nUNION ALL\nSELECT 'global'::text AS kind,'effectiveDatabase'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.rolname),'[]'::jsonb)) AS value FROM c_effectiveDatabase q\nUNION ALL\nSELECT 'global'::text AS kind,'memberships'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.granted_role,q.member,q.grantor),'[]'::jsonb)) AS value FROM c_memberships q\nUNION ALL\nSELECT 'global'::text AS kind,'effectiveFunctions'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.rolname,q.nspname,q.proname,q.arguments),'[]'::jsonb)) AS value FROM c_effectiveFunctions q\nUNION ALL\nSELECT 'global'::text AS kind,'effectiveSequences'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.rolname,q.relname),'[]'::jsonb)) AS value FROM c_effectiveSequences q\nUNION ALL\nSELECT 'global'::text AS kind,'events'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.evtname),'[]'::jsonb)) AS value FROM c_events q\nUNION ALL\nSELECT 'global'::text AS kind,'parameters'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.rolname,q.name),'[]'::jsonb)) AS value FROM c_parameters q\nUNION ALL\nSELECT 'global'::text AS kind,'settings'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg((pg_catalog.to_jsonb(q)-'setconfig')||pg_catalog.jsonb_build_object('keys',ARRAY(SELECT pg_catalog.split_part(v,'=',1) FROM pg_catalog.unnest(q.setconfig)v),'valueSha256',pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(pg_catalog.to_jsonb(q.setconfig)::text,'UTF8')),'hex')) ORDER BY q.database,q.role),'[]'::jsonb)) AS value FROM c_rawSettings q\nUNION ALL\nSELECT 'global'::text AS kind,'roles'::text AS identity,pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.rolname),'[]'::jsonb)) AS value FROM c_roles q\nUNION ALL\nSELECT 'global','source_size',pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q)),'[]'::jsonb)) FROM(SELECT\n (SELECT count(*) FROM(SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relkind IN('r','p'))s) AS tables,\n (SELECT COALESCE(sum(pg_catalog.octet_length(p.prosrc)),0)::text FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace WHERE n.nspname IN('public','mmapp_control')) AS function_bytes,\n (SELECT COALESCE(sum(pg_catalog.octet_length(d.adbin::text)),0)::text FROM pg_catalog.pg_attrdef d JOIN pg_catalog.pg_class c ON c.oid=d.adrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public') AS default_bytes,\n (SELECT COALESCE(sum(pg_catalog.octet_length(k.conbin::text)),0)::text FROM pg_catalog.pg_constraint k JOIN pg_catalog.pg_namespace n ON n.oid=k.connamespace WHERE n.nspname IN('public','mmapp_control')) AS check_bytes,\n (SELECT COALESCE(sum(pg_catalog.octet_length(t.tgqual::text)+pg_catalog.octet_length(t.tgargs)),0)::text FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public') AS trigger_bytes)q\nUNION ALL\nSELECT 'global','publisher_column_escape',pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q)),'[]'::jsonb)) FROM(SELECT c.relname FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relkind IN('r','p') AND c.relname<>'schema_migrations' AND(pg_catalog.has_any_column_privilege($4::pg_catalog.name::pg_catalog.name,c.oid,'SELECT') OR pg_catalog.has_any_column_privilege($4::pg_catalog.name::pg_catalog.name,c.oid,'INSERT') OR pg_catalog.has_any_column_privilege($4::pg_catalog.name::pg_catalog.name,c.oid,'UPDATE') OR pg_catalog.has_any_column_privilege($4::pg_catalog.name::pg_catalog.name,c.oid,'REFERENCES')) LIMIT 1)q\nUNION ALL\nSELECT 'global','namespace_policy',pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q)),'[]'::jsonb)) FROM(SELECT\n EXISTS(SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relkind NOT IN('r','p','S','i','I')) AS relations,\n EXISTS(SELECT 1 FROM pg_catalog.pg_rewrite r JOIN pg_catalog.pg_class c ON c.oid=r.ev_class JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public') AS rules,\n EXISTS(SELECT 1 FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace WHERE n.nspname IN('public','mmapp_control') AND p.prokind NOT IN('f','p')) AS routines,\n EXISTS(SELECT 1 FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid=t.typnamespace LEFT JOIN pg_catalog.pg_class c ON c.oid=t.typrelid LEFT JOIN pg_catalog.pg_type e ON e.oid=t.typelem LEFT JOIN pg_catalog.pg_class ec ON ec.oid=e.typrelid WHERE n.nspname='public' AND(t.typtype='c' AND c.relkind IN('r','p') OR t.typelem<>0 AND e.typarray=t.oid AND ec.relkind IN('r','p')) IS NOT TRUE) AS types,\n EXISTS(SELECT 1 FROM pg_catalog.pg_namespace n WHERE n.nspname NOT IN('public','mmapp_control','pg_catalog','information_schema') AND n.nspname!~'^pg_(toast|temp_)') AS namespaces,\n EXISTS(SELECT 1 FROM pg_catalog.pg_default_acl d JOIN pg_catalog.pg_roles r ON r.oid=d.defaclrole WHERE r.rolname IN($5::pg_catalog.name,$2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name)) AS default_acl)q\nUNION ALL\nSELECT 'global','base_tables',pg_catalog.jsonb_build_object('rows',COALESCE(pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.relname COLLATE \"C\"),'[]'::jsonb)) FROM(SELECT * FROM c_tables WHERE NOT relispartition ORDER BY relname COLLATE \"C\" LIMIT 104)q\nUNION ALL\nSELECT 'leaf','!inventory_counts',pg_catalog.jsonb_build_object('base',(SELECT count(*)::int FROM c_tables WHERE NOT relispartition),'leaf',(SELECT count(*)::int FROM c_tables WHERE relispartition))\nUNION ALL\nSELECT CASE WHEN bundle_relation.relispartition THEN 'leaf' ELSE 'base' END,bundle_relation.relname,pg_catalog.jsonb_build_object('table',pg_catalog.to_jsonb(bundle_relation),\n'columns',COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.attnum) FROM(SELECT c.relname,a.attnum::int,a.attname,pg_catalog.format_type(a.atttypid,a.atttypmod) AS type,\n a.attnotnull,a.attidentity,a.attgenerated,a.attstorage,a.attcompression,a.attacl::text[] AS acl,pg_catalog.pg_get_expr(d.adbin,d.adrelid) AS default_expression\n FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON c.oid=a.attrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum\n WHERE n.nspname='public' AND c.relkind IN('r','p') AND a.attnum>0 AND NOT a.attisdropped AND c.oid=bundle_relation.oid::oid)q),'[]'::jsonb),\n'triggers',COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.tgname) FROM(SELECT t.oid::text AS oid,c.relname,t.tgname,t.tgtype::int,t.tgenabled,t.tgisinternal,t.tgfoid::text,t.tgparentid::text,t.tgconstraint::text,t.tgdeferrable,t.tginitdeferred,t.tgoldtable,t.tgnewtable,\n t.tgnargs,octet_length(t.tgargs)::int AS arg_bytes,t.tgqual IS NULL AS unconditional,pg_catalog.pg_get_triggerdef(t.oid) AS definition\n FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n WHERE n.nspname='public' AND c.oid=bundle_relation.oid::oid)q),'[]'::jsonb),\n'constraints',COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.conname) FROM(SELECT c.relname,k.conname,k.contype,k.convalidated,k.condeferrable,k.condeferred,\n pg_catalog.pg_get_constraintdef(k.oid) AS definition FROM pg_catalog.pg_constraint k JOIN pg_catalog.pg_class c ON c.oid=k.conrelid\n JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.oid=bundle_relation.oid::oid)q),'[]'::jsonb),\n'indexes',COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.index_name) FROM(SELECT c.relname,i.relname AS index_name,x.indisvalid,x.indisready,x.indisunique,x.indisprimary,\n pg_catalog.pg_get_indexdef(i.oid) AS definition FROM pg_catalog.pg_index x JOIN pg_catalog.pg_class c ON c.oid=x.indrelid\n JOIN pg_catalog.pg_class i ON i.oid=x.indexrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace\n WHERE n.nspname='public' AND c.oid=bundle_relation.oid::oid)q),'[]'::jsonb),\n'roleGrants',COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.rolname) FROM(SELECT r.rolname,c.relname,pg_catalog.has_table_privilege(r.oid,c.oid,'SELECT') AS read,\n pg_catalog.has_table_privilege(r.oid,c.oid,'INSERT') AS insert,pg_catalog.has_table_privilege(r.oid,c.oid,'UPDATE') AS update,\n pg_catalog.has_table_privilege(r.oid,c.oid,'DELETE') AS delete,pg_catalog.has_table_privilege(r.oid,c.oid,'TRUNCATE') AS truncate,\n pg_catalog.has_table_privilege(r.oid,c.oid,'TRIGGER') AS trigger FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_class c\n JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE r.rolname IN($2::pg_catalog.name,$3::pg_catalog.name,$1::pg_catalog.name,$4::pg_catalog.name)\n AND n.nspname='public' AND c.relkind IN('r','p') AND c.oid=bundle_relation.oid::oid)q),'[]'::jsonb),\n'inheritance',COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(q) ORDER BY q.parent,q.child) FROM(SELECT p.relname AS parent,c.relname AS child FROM pg_catalog.pg_inherits i\n JOIN pg_catalog.pg_class p ON p.oid=i.inhparent JOIN pg_catalog.pg_class c ON c.oid=i.inhrelid JOIN pg_catalog.pg_namespace n ON n.oid=p.relnamespace\n WHERE n.nspname='public' AND p.relkind IN('r','p') AND c.oid=bundle_relation.oid::oid)q),'[]'::jsonb)) FROM c_tables bundle_relation),\nnumbered AS(SELECT row_number() OVER(ORDER BY CASE kind WHEN 'global' THEN 0 WHEN 'base' THEN 1 ELSE 2 END,identity COLLATE \"C\") AS record_no,kind,identity,pg_catalog.convert_to(pg_catalog.jsonb_build_object('kind',kind,'identity',identity,'value',value)::text,'UTF8') AS encoded FROM records),\nframed AS(SELECT *,CASE WHEN pg_catalog.octet_length(encoded)>2097152 THEN 0 ELSE (pg_catalog.octet_length(encoded)+4095)/4096 END AS chunks FROM numbered)\nSELECT record_no::text AS record_id,part::int AS chunk_index,chunks::int AS chunk_count,kind,identity,CASE WHEN chunks=0 THEN NULL::bytea ELSE substring(encoded FROM part*4096+1 FOR 4096) END AS payload FROM framed CROSS JOIN LATERAL pg_catalog.generate_series(0,GREATEST(0,chunks-1))part ORDER BY record_no,part" diff --git a/backend/scripts/writer-profile/fixed56-build-definition.ts b/backend/scripts/writer-profile/fixed56-build-definition.ts new file mode 100644 index 00000000..86b77d20 --- /dev/null +++ b/backend/scripts/writer-profile/fixed56-build-definition.ts @@ -0,0 +1,22 @@ +import { canonical, sha } from "./application-writer-profile-canonical.js" +import { sorted, type Inventory } from "./catalogue-projection.js" +import { FIXED44_ROLE_TEMPLATE } from "./fixed44-build-definition.js" +import { FIXED56_DELTA } from "./fixed56-build-delta.js" + +export const BUILD56_PROVENANCE = Object.freeze({ + format: "source56-build-candidate-v1", + manifestSha256: "b371d8d261f7dd69cf0e2ebc48d4316e3ad804360046bbaaf20e715da8812521", + templateSha256: "e6c2ea106e6fcbd8af7a3ed3ded052c2267be92d3bad41c2ef9e62ec95d2b3d9", +}) + +export const FIXED56_ROLE_TEMPLATE: Readonly = Object.freeze(Object.fromEntries( + Object.entries(FIXED44_ROLE_TEMPLATE).map(([section, rows]) => { + const delta = FIXED56_DELTA[section] + const removed = new Set(delta?.remove) + return [section, sorted([...rows.filter((_, index) => !removed.has(index)), ...(delta?.add ?? [])])] + }), +)) + +if (sha(canonical(FIXED56_ROLE_TEMPLATE)) !== BUILD56_PROVENANCE.templateSha256) { + throw new Error("Fixed56 writer catalogue changed") +} diff --git a/backend/scripts/writer-profile/fixed56-build-delta.ts b/backend/scripts/writer-profile/fixed56-build-delta.ts new file mode 100644 index 00000000..81ef239f --- /dev/null +++ b/backend/scripts/writer-profile/fixed56-build-delta.ts @@ -0,0 +1,303 @@ +// Build data from the owned PostgreSQL 18 current56 ACL candidate. Removal indices are relative to the exact fixed44 template. +import type { Row } from "./catalogue-projection.js" + +export const FIXED56_DELTA: Readonly> = { + columns: { + remove: [], + add: [ + {"acl":["@mm-role:runtime@=w/@mm-role:operator@"],"attcompression":"","attgenerated":"","attidentity":"","attname":"capture_proof","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"server_probe_terminal_proofs","type":"text"}, + {"acl":["@mm-role:runtime@=w/@mm-role:operator@"],"attcompression":"","attgenerated":"","attidentity":"","attname":"proof","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"server_probe_retired_sources","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"action_id","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"mcp_operation_replay_tombstones","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_token","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"server_poll_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_token","attnotnull":true,"attnum":5,"attstorage":"p","default_expression":null,"relname":"mcp_ordinary_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_token","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":null,"relname":"server_probe_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_xid","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":"pg_current_xact_id()","relname":"server_poll_retirement_claims","type":"xid8"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_xid","attnotnull":true,"attnum":6,"attstorage":"p","default_expression":"pg_current_xact_id()","relname":"mcp_ordinary_retirement_claims","type":"xid8"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"claim_xid","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":"pg_current_xact_id()","relname":"server_probe_retirement_claims","type":"xid8"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_operation_replay_tombstones","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"mcp_ordinary_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"connection_id","attnotnull":true,"attnum":4,"attstorage":"p","default_expression":null,"relname":"server_probe_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"idempotency_digest","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_operation_replay_tombstones","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"idempotency_digest","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_probe_terminal_proofs","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"old_archive_proof","attnotnull":true,"attnum":2,"attstorage":"x","default_expression":null,"relname":"server_poll_retirement_claims","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_operation_replay_tombstones","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"mcp_ordinary_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_terminal_proofs","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"server_probe_retired_sources","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"operation_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"server_probe_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"plan_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_operation_replay_tombstones","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"plan_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"mcp_ordinary_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"plan_id","attnotnull":true,"attnum":3,"attstorage":"p","default_expression":null,"relname":"server_probe_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_poll_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_retired_sources","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":1,"attstorage":"p","default_expression":null,"relname":"server_probe_retirement_claims","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"receipt_id","attnotnull":true,"attnum":2,"attstorage":"p","default_expression":null,"relname":"server_probe_terminal_proofs","type":"uuid"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_probe_retired_sources","type":"jsonb"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source","attnotnull":true,"attnum":3,"attstorage":"x","default_expression":null,"relname":"server_probe_terminal_proofs","type":"jsonb"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_proof","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"mcp_ordinary_retirement_claims","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_proof","attnotnull":true,"attnum":4,"attstorage":"x","default_expression":null,"relname":"server_probe_terminal_proofs","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"source_proof","attnotnull":true,"attnum":5,"attstorage":"x","default_expression":null,"relname":"server_probe_retirement_claims","type":"text"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"target_global","attnotnull":true,"attnum":7,"attstorage":"p","default_expression":null,"relname":"mcp_operation_replay_tombstones","type":"boolean"}, + {"acl":null,"attcompression":"","attgenerated":"","attidentity":"","attname":"target_server_ids","attnotnull":true,"attnum":6,"attstorage":"x","default_expression":null,"relname":"mcp_operation_replay_tombstones","type":"bigint[]"}, + ], + }, + constraints: { + remove: [725], + add: [ + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombston_connection_id_idempotency_dig_key","contype":"u","convalidated":true,"definition":"UNIQUE (connection_id, idempotency_digest)","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_action_id_check","contype":"c","convalidated":true,"definition":"CHECK (((length(action_id) >= 1) AND (length(action_id) <= 200)))","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_action_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL action_id","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_idempotency_digest_check","contype":"c","convalidated":true,"definition":"CHECK ((idempotency_digest ~ '^[0-9a-f]{64}$'::text))","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_idempotency_digest_not_null","contype":"n","convalidated":true,"definition":"NOT NULL idempotency_digest","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (operation_id)","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_plan_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (plan_id)","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL plan_id","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_target_global_not_null","contype":"n","convalidated":true,"definition":"NOT NULL target_global","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_target_server_ids_check","contype":"c","convalidated":true,"definition":"CHECK ((array_position(target_server_ids, NULL::bigint) IS NULL))","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_operation_replay_tombstones_target_server_ids_not_null","contype":"n","convalidated":true,"definition":"NOT NULL target_server_ids","relname":"mcp_operation_replay_tombstones"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_claim_token_not_null","contype":"n","convalidated":true,"definition":"NOT NULL claim_token","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_claim_xid_not_null","contype":"n","convalidated":true,"definition":"NOT NULL claim_xid","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_connection_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (connection_id) REFERENCES mcp_connections(id)","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (operation_id)","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_plan_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (plan_id)","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL plan_id","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_source_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((source_proof ~ '^[0-9a-f]{64}$'::text))","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"mcp_ordinary_retirement_claims_source_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_proof","relname":"mcp_ordinary_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_archives_version_check","contype":"c","convalidated":true,"definition":"CHECK (((version = 1) OR ((version = 2) AND (decoded_bytes <= 24576) AND (compressed_bytes <= 24576) AND (ciphertext_bytes <= 33000))))","relname":"server_poll_archives"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_retirement_claims_claim_token_not_null","contype":"n","convalidated":true,"definition":"NOT NULL claim_token","relname":"server_poll_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_retirement_claims_claim_xid_not_null","contype":"n","convalidated":true,"definition":"NOT NULL claim_xid","relname":"server_poll_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_retirement_claims_old_archive_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((old_archive_proof ~ '^[0-9a-f]{64}$'::text))","relname":"server_poll_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_retirement_claims_old_archive_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL old_archive_proof","relname":"server_poll_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_retirement_claims_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_poll_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_retirement_claims_receipt_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id) REFERENCES server_poll_storage_admissions(receipt_id) ON DELETE CASCADE","relname":"server_poll_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_poll_retirement_claims_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_poll_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_check","contype":"c","convalidated":true,"definition":"CHECK (((NOT ((source ->> 'version'::text) IS DISTINCT FROM '1'::text)) AND (NOT ((source ->> 'receiptId'::text) IS DISTINCT FROM (receipt_id)::text)) AND (NOT ((source ->> 'executionId'::text) IS DISTINCT FROM (operation_id)::text))))","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (operation_id) REFERENCES mcp_operation_replay_tombstones(operation_id)","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_operation_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (operation_id)","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((proof ~ '^[0-9a-f]{64}$'::text))","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL proof","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_receipt_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (receipt_id) REFERENCES server_probe_storage_admissions(receipt_id) ON DELETE CASCADE","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_source_check","contype":"c","convalidated":true,"definition":"CHECK (((jsonb_typeof(source) = 'object'::text) AND ((octet_length((source)::text) >= 1) AND (octet_length((source)::text) <= 8192))))","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retired_sources_source_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source","relname":"server_probe_retired_sources"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_claim_token_not_null","contype":"n","convalidated":true,"definition":"NOT NULL claim_token","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_claim_xid_not_null","contype":"n","convalidated":true,"definition":"NOT NULL claim_xid","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_connection_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL connection_id","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_operation_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (operation_id)","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (receipt_id)","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_plan_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (plan_id)","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_plan_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL plan_id","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_source_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((source_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_retirement_claims_source_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_proof","relname":"server_probe_retirement_claims"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_capture_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((capture_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_capture_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL capture_proof","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_check","contype":"c","convalidated":true,"definition":"CHECK (((NOT ((source ->> 'version'::text) IS DISTINCT FROM '1'::text)) AND (NOT ((source ->> 'executionId'::text) IS DISTINCT FROM (operation_id)::text)) AND (NOT ((source ->> 'receiptId'::text) IS DISTINCT FROM (receipt_id)::text))))","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_idempotency_digest_check","contype":"c","convalidated":true,"definition":"CHECK ((idempotency_digest ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_idempotency_digest_not_null","contype":"n","convalidated":true,"definition":"NOT NULL idempotency_digest","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_operation_id_fkey","contype":"f","convalidated":true,"definition":"FOREIGN KEY (operation_id) REFERENCES mcp_operations(id)","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_operation_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL operation_id","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_pkey","contype":"p","convalidated":true,"definition":"PRIMARY KEY (operation_id)","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_receipt_id_key","contype":"u","convalidated":true,"definition":"UNIQUE (receipt_id)","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_receipt_id_not_null","contype":"n","convalidated":true,"definition":"NOT NULL receipt_id","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_source_check","contype":"c","convalidated":true,"definition":"CHECK (((jsonb_typeof(source) = 'object'::text) AND ((octet_length((source)::text) >= 1) AND (octet_length((source)::text) <= 8192))))","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_source_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_source_proof_check","contype":"c","convalidated":true,"definition":"CHECK ((source_proof ~ '^[a-f0-9]{64}$'::text))","relname":"server_probe_terminal_proofs"}, + {"condeferrable":false,"condeferred":false,"conname":"server_probe_terminal_proofs_source_proof_not_null","contype":"n","convalidated":true,"definition":"NOT NULL source_proof","relname":"server_probe_terminal_proofs"}, + {"condeferrable":true,"condeferred":true,"conname":"server_poll_retirement_final_guard","contype":"t","convalidated":true,"definition":"TRIGGER DEFERRABLE INITIALLY DEFERRED","relname":"server_poll_archives"}, + ], + }, + database: { + remove: [0], + add: [ + {"acl":["@mm-role:capture@=c/@mm-role:control@","@mm-role:control@=CTc/@mm-role:control@","@mm-role:operator@=c/@mm-role:control@","@mm-role:publisher@=c/@mm-role:control@","@mm-role:runtime@=c/@mm-role:control@"],"datname":"current-database","owner":"@mm-role:control@"}, + ], + }, + effectiveFunctions: { + remove: [], + add: [ + {"arguments":"","execute":true,"nspname":"public","proname":"mcp_ordinary_retirement_claim_immutable","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"mcp_ordinary_retirement_claim_immutable","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"mcp_ordinary_retirement_claim_immutable","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"mcp_ordinary_retirement_claim_immutable","rolname":"@mm-role:runtime@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_claim_immutable","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_claim_immutable","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_claim_immutable","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_claim_immutable","rolname":"@mm-role:runtime@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_final_guard","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_final_guard","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_final_guard","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_final_guard","rolname":"@mm-role:runtime@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_requires_claim","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_requires_claim","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_requires_claim","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_poll_retirement_requires_claim","rolname":"@mm-role:runtime@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_immutable","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_immutable","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_immutable","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_immutable","rolname":"@mm-role:runtime@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_insert_guard","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_insert_guard","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_insert_guard","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retired_source_insert_guard","rolname":"@mm-role:runtime@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retirement_claim_guard","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retirement_claim_guard","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retirement_claim_guard","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_retirement_claim_guard","rolname":"@mm-role:runtime@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_terminal_proof_guard","rolname":"@mm-role:capture@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_terminal_proof_guard","rolname":"@mm-role:operator@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_terminal_proof_guard","rolname":"@mm-role:publisher@"}, + {"arguments":"","execute":true,"nspname":"public","proname":"server_probe_terminal_proof_guard","rolname":"@mm-role:runtime@"}, + {"arguments":"target_receipt uuid, current_retention integer","execute":true,"nspname":"public","proname":"server_probe_retired_gc_allowed","rolname":"@mm-role:capture@"}, + {"arguments":"target_receipt uuid, current_retention integer","execute":true,"nspname":"public","proname":"server_probe_retired_gc_allowed","rolname":"@mm-role:operator@"}, + {"arguments":"target_receipt uuid, current_retention integer","execute":true,"nspname":"public","proname":"server_probe_retired_gc_allowed","rolname":"@mm-role:publisher@"}, + {"arguments":"target_receipt uuid, current_retention integer","execute":true,"nspname":"public","proname":"server_probe_retired_gc_allowed","rolname":"@mm-role:runtime@"}, + ], + }, + functions: { + remove: [7,8,12,19,31,32], + add: [ + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.mcp_ordinary_retirement_claim_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF TG_OP='INSERT' AND NEW.claim_xid=pg_current_xact_id() AND EXISTS(\n SELECT 1 FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\n WHERE o.id=NEW.operation_id AND o.plan_id=NEW.plan_id AND o.connection_id=NEW.connection_id\n AND o.state='applied' AND o.verification='passed' AND o.error_code IS NULL\n AND isfinite(o.finished_at) AND o.finished_at>'kind'='service'\n AND s.intended_state->>'service'='routing.community.scoped.recover'\n AND s.intended_state->'input'->>'mode'='manual'\n AND s.intended_state->'input'->>'sourceOperationId'=NEW.source_operation_id::text\n AND s.intended_state->'input'->>'receiptId'=NEW.recovery_receipt_id::text\n ) THEN RAISE EXCEPTION 'manual inverse requires current exact running recovery'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":null,"prokind":"f","proname":"mcp_scoped_recovery_manual_version","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_archive_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF TG_OP='DELETE' AND public.server_poll_storage_gc_allowed(OLD.receipt_id) THEN RETURN OLD; END IF;\n IF TG_OP='UPDATE' AND OLD.version=1 AND NEW.version=2\n AND current_setting('mm.poll_retire_receipt',true)=OLD.receipt_id::text\n AND NEW.ciphertext_bytes<=OLD.ciphertext_bytes\n AND NEW.ciphertext ~ '^[A-Za-z0-9+/]+={0,2}$'\n AND replace(encode(decode(NEW.ciphertext,'base64'),'base64'),E'\\n','')=NEW.ciphertext\n AND octet_length(decode(NEW.ciphertext,'base64'))=NEW.compressed_bytes+28\n AND (to_jsonb(OLD)-ARRAY['version','source_digest','source_sha256','decoded_bytes','compressed_bytes','ciphertext','ciphertext_bytes','archive_proof'])=\n (to_jsonb(NEW)-ARRAY['version','source_digest','source_sha256','decoded_bytes','compressed_bytes','ciphertext','ciphertext_bytes','archive_proof'])\n AND EXISTS(\n SELECT 1 FROM public.server_poll_storage_admissions a JOIN public.mcp_operations o ON o.id=a.execution_id AND o.connection_id=a.connection_id\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\n JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0\n JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=o.id AND t.plan_id=p.id AND t.connection_id=o.connection_id\n JOIN public.mcp_connections c ON c.id=o.connection_id AND c.owner_id=a.owner_id\n WHERE a.receipt_id=OLD.receipt_id AND a.identity_kind='mcp' AND a.storage_state='compacted'\n AND a.execution_id=OLD.execution_id AND a.connection_id=OLD.connection_id AND a.owner_id=OLD.owner_id\n AND a.binding_proof=OLD.binding_proof AND a.context_proof=OLD.context_proof AND a.archive_bytes=OLD.ciphertext_bytes\n AND a.original_activity_at=OLD.original_activity_at AND a.authority_expires_at=OLD.original_expires_at\n AND o.state='applied' AND o.verification='passed' AND o.error_code IS NULL AND isfinite(o.finished_at)\n AND o.finished_at0\n ) THEN RETURN NEW; END IF;\n RAISE EXCEPTION 'poll archive immutable outside exact retirement or original lifetime GC';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC","search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_archive_immutable","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_retirement_claim_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF TG_OP='INSERT' AND NEW.claim_xid=pg_current_xact_id() AND EXISTS(\n SELECT 1 FROM public.server_poll_archives r WHERE r.receipt_id=NEW.receipt_id\n AND r.version=1 AND r.archive_proof=NEW.old_archive_proof\n ) THEN RETURN NEW; END IF;\n IF TG_OP='DELETE' AND pg_trigger_depth()>1 AND NOT EXISTS(\n SELECT 1 FROM public.server_poll_storage_admissions a WHERE a.receipt_id=OLD.receipt_id\n ) THEN RETURN OLD; END IF;\n RAISE EXCEPTION 'poll retirement claim cannot be replaced or reopened';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_retirement_claim_immutable","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_retirement_final_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF NOT EXISTS(SELECT 1 FROM public.mcp_operation_replay_tombstones t\n WHERE t.operation_id=NEW.execution_id AND t.connection_id=NEW.connection_id AND t.action_id='servers.poll'\n AND t.target_global AND NOT EXISTS(SELECT 1 FROM public.mcp_plans p WHERE p.id=t.plan_id))\n OR EXISTS(SELECT 1 FROM public.mcp_operations o WHERE o.id=NEW.execution_id)\n OR NOT public.server_poll_storage_accounting_valid()\n THEN RAISE EXCEPTION 'poll retirement is incomplete'; END IF;\n RETURN NULL;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_retirement_final_guard","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_retirement_requires_claim()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF NOT EXISTS(SELECT 1 FROM public.server_poll_retirement_claims c\n WHERE c.receipt_id=OLD.receipt_id AND c.old_archive_proof=OLD.archive_proof\n AND c.claim_token::text=current_setting('mm.poll_retire_claim',true)\n AND c.claim_xid<>pg_current_xact_id())\n THEN RAISE EXCEPTION 'poll retirement requires prior exact claim'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_retirement_requires_claim","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_poll_storage_admission_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nDECLARE retention integer; charged integer; r public.server_poll_archives%ROWTYPE;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0));\n IF TG_OP='DELETE' THEN\n IF NOT public.server_poll_storage_gc_allowed(OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_executions WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_intents WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_acks WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_receipts WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_quarantines WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_archives WHERE receipt_id=OLD.receipt_id) THEN RAISE EXCEPTION 'poll lifetime or children prevent admission deletion'; END IF;\n UPDATE public.server_poll_storage_usage SET admissions=admissions-CASE WHEN OLD.storage_state='full' THEN 1 ELSE 0 END,\n compacted_admissions=compacted_admissions-CASE WHEN OLD.storage_state='compacted' THEN 1 ELSE 0 END,\n archive_bytes=archive_bytes-COALESCE(OLD.archive_bytes,0),\n reserved_rows=reserved_rows-CASE WHEN OLD.storage_state='full' THEN 4019 ELSE 2 END,\n reserved_bytes=reserved_bytes-CASE WHEN OLD.storage_state='full' THEN 8421376 ELSE 32768+OLD.archive_bytes END\n WHERE scope IN('global','owner:'||OLD.owner_id);\n GET DIAGNOSTICS charged=ROW_COUNT; IF charged<>2 THEN RAISE EXCEPTION 'poll quota refund mismatch'; END IF; RETURN OLD;\n END IF;\n IF TG_OP='UPDATE' THEN\n IF OLD.storage_state='compacted' AND NEW.storage_state='compacted'\n AND current_setting('mm.poll_retire_receipt',true)=OLD.receipt_id::text\n AND (to_jsonb(OLD)-'archive_bytes')=(to_jsonb(NEW)-'archive_bytes') THEN\n SELECT * INTO r FROM public.server_poll_archives WHERE receipt_id=OLD.receipt_id AND version=2;\n IF NOT FOUND OR r.ciphertext_bytes<>NEW.archive_bytes OR NEW.archive_bytes>OLD.archive_bytes\n OR r.execution_id<>OLD.execution_id OR r.connection_id<>OLD.connection_id OR r.owner_id<>OLD.owner_id\n THEN RAISE EXCEPTION 'retired poll charge source mismatch'; END IF;\n UPDATE public.server_poll_storage_usage\n SET archive_bytes=archive_bytes-OLD.archive_bytes+NEW.archive_bytes,\n reserved_bytes=reserved_bytes-OLD.archive_bytes+NEW.archive_bytes\n WHERE scope IN ('global','owner:'||OLD.owner_id) AND compacted_admissions>=1 AND archive_bytes>=OLD.archive_bytes;\n GET DIAGNOSTICS charged=ROW_COUNT;\n IF charged<>2 THEN RAISE EXCEPTION 'retired poll charge adjustment mismatch'; END IF;\n RETURN NEW;\n END IF;\n -- This durable one-shot marker is written and ACKed in its OWN transaction\n -- before archive construction. A committed/unknown claim never resets and\n -- the worker never retries destructive work against an already claimed full\n -- source. Claim time is maintenance metadata, not renewed execution activity.\n IF OLD.storage_state='full' AND NEW.storage_state='full' AND OLD.compaction_claim IS NULL AND OLD.compaction_claimed_at IS NULL AND OLD.compaction_claim_xid IS NULL\n AND NEW.compaction_claim IS NOT NULL AND NEW.compaction_claim_xid IS NULL\n AND(to_jsonb(OLD)-ARRAY['compaction_claim','compaction_claimed_at','compaction_claim_xid'])=(to_jsonb(NEW)-ARRAY['compaction_claim','compaction_claimed_at','compaction_claim_xid'])\n AND public.server_poll_storage_compact_allowed(OLD.receipt_id)\n THEN NEW.compaction_claimed_at:=clock_timestamp(); NEW.compaction_claim_xid:=pg_current_xact_id(); RETURN NEW; END IF;\n IF(to_jsonb(OLD)-ARRAY['claim_token','claim_attempted_at','used_rows','used_bytes','storage_state','compact_at','archive_bytes','original_activity_at']) IS DISTINCT FROM\n (to_jsonb(NEW)-ARRAY['claim_token','claim_attempted_at','used_rows','used_bytes','storage_state','compact_at','archive_bytes','original_activity_at']) THEN RAISE EXCEPTION 'poll admission identity is immutable'; END IF;\n IF OLD.storage_state<>'full' THEN RAISE EXCEPTION 'compacted poll identity cannot reopen or change'; END IF;\n IF NEW.storage_state='compacted' THEN\n SELECT * INTO r FROM public.server_poll_archives WHERE receipt_id=OLD.receipt_id AND compaction_xid=pg_current_xact_id();\n IF NOT FOUND OR NEW.used_rows<>OLD.used_rows OR NEW.used_bytes<>OLD.used_bytes OR NEW.claim_token IS DISTINCT FROM OLD.claim_token\n OR NEW.claim_attempted_at IS DISTINCT FROM OLD.claim_attempted_at OR NEW.archive_bytes<>r.ciphertext_bytes OR NEW.original_activity_at<>r.original_activity_at\n OR EXISTS(SELECT 1 FROM public.server_poll_executions WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_intents WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_acks WHERE receipt_id=OLD.receipt_id)\n OR EXISTS(SELECT 1 FROM public.server_poll_receipts WHERE receipt_id=OLD.receipt_id) OR EXISTS(SELECT 1 FROM public.server_poll_quarantines WHERE receipt_id=OLD.receipt_id)\n THEN RAISE EXCEPTION 'poll compaction requires the complete current-transaction archive'; END IF;\n NEW.compact_at:=r.created_at;\n UPDATE public.server_poll_storage_usage SET admissions=admissions-1,compacted_admissions=compacted_admissions+1,\n archive_bytes=archive_bytes+r.ciphertext_bytes,reserved_rows=reserved_rows-4019+2,reserved_bytes=reserved_bytes-8421376+32768+r.ciphertext_bytes\n WHERE scope IN('global','owner:'||OLD.owner_id) AND admissions>=1;\n GET DIAGNOSTICS charged=ROW_COUNT; IF charged<>2 THEN RAISE EXCEPTION 'poll compaction charge mismatch'; END IF;\n RETURN NEW;\n END IF;\n IF NEW.compact_at IS NOT NULL OR NEW.archive_bytes IS NOT NULL OR NEW.original_activity_at IS NOT NULL THEN RAISE EXCEPTION 'full poll cannot carry archive metadata'; END IF;\n IF OLD.claim_token IS NOT NULL AND NEW.claim_token IS NULL AND OLD.claim_attempted_at IS NULL AND NEW.claim_attempted_at IS NOT NULL\n AND NEW.used_rows=OLD.used_rows AND NEW.used_bytes=OLD.used_bytes AND OLD.authority_expires_at>clock_timestamp()\n THEN NEW.claim_attempted_at:=clock_timestamp(); RETURN NEW; END IF;\n IF OLD.claim_token IS NOT DISTINCT FROM NEW.claim_token AND OLD.claim_attempted_at IS NOT DISTINCT FROM NEW.claim_attempted_at\n AND NEW.used_rows=OLD.used_rows+1 AND NEW.used_bytes>OLD.used_bytes THEN RETURN NEW; END IF;\n RAISE EXCEPTION 'poll admission cannot refresh, reopen or refund itself';\n END IF;\n IF NEW.storage_state<>'full' OR NEW.compact_at IS NOT NULL OR NEW.archive_bytes IS NOT NULL OR NEW.original_activity_at IS NOT NULL\n OR NEW.compaction_claim IS NOT NULL OR NEW.compaction_claimed_at IS NOT NULL OR NEW.compaction_claim_xid IS NOT NULL\n OR NOT public.server_poll_storage_accounting_valid() THEN RAISE EXCEPTION 'new poll requires full reservation and exact ledger'; END IF;\n SELECT requests_retention_days INTO retention FROM public.mcp_settings WHERE id=1;\n IF retention IS NULL OR retention NOT BETWEEN 1 AND 365 OR NEW.authority_issued_at>clock_timestamp() OR NEW.authority_expires_at<=clock_timestamp()\n OR NEW.claim_token IS NULL OR NEW.claim_attempted_at IS NOT NULL OR NEW.used_rows<>0 OR NEW.used_bytes<>0 THEN RAISE EXCEPTION 'new poll admission needs original live authority'; END IF;\n IF EXISTS(SELECT 1 FROM public.server_poll_storage_admissions WHERE receipt_id=NEW.receipt_id OR(identity_kind=NEW.identity_kind AND execution_id=NEW.execution_id)) THEN RAISE EXCEPTION 'poll identity already spent'; END IF;\n NEW.created_at:=clock_timestamp(); NEW.retention_days:=retention; NEW.safety_ms:=60000;\n NEW.gc_not_before:=NEW.authority_expires_at+make_interval(days=>retention)+interval '60 seconds';\n INSERT INTO public.server_poll_storage_usage(scope) VALUES('owner:'||NEW.owner_id) ON CONFLICT DO NOTHING;\n IF(SELECT count(*) FROM public.server_poll_storage_usage WHERE scope IN('global','owner:'||NEW.owner_id)\n AND admissions2 THEN RAISE EXCEPTION 'poll admission quota exhausted'; END IF;\n UPDATE public.server_poll_storage_usage SET admissions=admissions+1,reserved_rows=reserved_rows+4019,reserved_bytes=reserved_bytes+8421376 WHERE scope IN('global','owner:'||NEW.owner_id);\n GET DIAGNOSTICS charged=ROW_COUNT; IF charged<>2 THEN RAISE EXCEPTION 'poll admission quota unavailable'; END IF;\n RETURN NEW;\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC","search_path=pg_catalog, public"],"prokind":"f","proname":"server_poll_storage_admission_guard","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_retired_source_immutable()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF TG_OP='DELETE' THEN\n IF current_setting('mm.probe_storage_gc',true)='on'\n AND current_setting('mm.probe_retired_gc_receipt',true)=OLD.receipt_id::text\n AND current_setting('mm.probe_retired_gc_proof',true)=OLD.proof\n AND NOT EXISTS(SELECT 1 FROM public.server_probe_storage_admissions WHERE receipt_id=OLD.receipt_id)\n AND NOT EXISTS(SELECT 1 FROM public.server_probe_executions WHERE receipt_id=OLD.receipt_id)\n AND NOT EXISTS(SELECT 1 FROM public.server_probe_execution_payloads WHERE receipt_id=OLD.receipt_id)\n AND NOT EXISTS(SELECT 1 FROM public.server_probe_execution_stages WHERE receipt_id=OLD.receipt_id)\n THEN RETURN OLD; END IF;\n END IF;\n RAISE EXCEPTION 'retired diagnostic source is immutable';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_probe_retired_source_immutable","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_retired_source_insert_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF EXISTS(\n SELECT 1 FROM public.server_probe_retirement_claims r\n JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=r.operation_id AND t.plan_id=r.plan_id\n AND t.connection_id=r.connection_id AND t.action_id='servers.probes.run' AND NOT t.target_global\n JOIN public.mcp_operations o ON o.id=r.operation_id AND o.plan_id=r.plan_id AND o.connection_id=r.connection_id\n JOIN public.server_probe_storage_admissions a ON a.receipt_id=r.receipt_id AND a.execution_id=r.operation_id\n WHERE r.receipt_id=NEW.receipt_id AND r.operation_id=NEW.operation_id AND r.source_proof=NEW.proof\n AND r.claim_xid<>pg_current_xact_id() AND r.claim_token::text=current_setting('mm.probe_retirement_claim',true)\n AND NEW.source->>'planId'=r.plan_id::text AND NEW.source->>'connectionId'=r.connection_id::text\n AND NEW.source->>'ownerId'=a.owner_id AND NEW.source->>'bindingProof'=a.binding_proof\n AND NEW.source->'serverIds'=to_jsonb(t.target_server_ids)\n ) THEN RETURN NEW; END IF;\n RAISE EXCEPTION 'diagnostic archive requires an exact committed retirement claim';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_probe_retired_source_insert_guard","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_retirement_claim_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\n SET \"TimeZone\" TO 'UTC'\nAS $function$\nBEGIN\n IF TG_OP='INSERT' AND NEW.claim_xid=pg_current_xact_id() AND EXISTS(\n SELECT 1 FROM public.mcp_operations o\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\n JOIN public.mcp_connections c ON c.id=o.connection_id\n WHERE o.id=NEW.operation_id AND p.id=NEW.plan_id AND c.id=NEW.connection_id\n AND o.state='applied' AND o.verification='inconclusive' AND o.error_code IS NULL\n AND isfinite(o.finished_at) AND o.finished_at>'ownerId'=c.owner_id AND f.source->>'connectionId'=c.id::text AND f.source->>'planId'=p.id::text)\n AND NOT EXISTS(SELECT 1 FROM public.server_probe_storage_admissions a\n WHERE a.receipt_id=NEW.receipt_id OR(a.identity_kind='mcp' AND a.execution_id=o.id))))\n ) THEN RETURN NEW; END IF;\n RAISE EXCEPTION 'diagnostic retirement claim cannot be replaced or reopened';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public","TimeZone=UTC"],"prokind":"f","proname":"server_probe_retirement_claim_guard","prosecdef":false}, + {"acl":null,"arguments":"","definition":"CREATE OR REPLACE FUNCTION public.server_probe_terminal_proof_guard()\n RETURNS trigger\n LANGUAGE plpgsql\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nBEGIN\n IF TG_OP='INSERT' THEN\n IF EXISTS(\n SELECT 1 FROM public.mcp_operations o\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\n JOIN public.mcp_connections c ON c.id=o.connection_id\n JOIN public.server_probe_storage_admissions a ON a.execution_id=o.id AND a.identity_kind='mcp' AND a.owner_id=c.owner_id\n JOIN public.server_probe_executions j ON j.receipt_id=a.receipt_id\n JOIN public.server_probe_execution_payloads b ON b.receipt_id=a.receipt_id AND b.receipt_proof=j.receipt_proof\n WHERE o.id=NEW.operation_id AND a.receipt_id=NEW.receipt_id AND a.purged_at IS NULL AND a.cancelled_at IS NULL\n AND a.claim_token IS NULL AND a.claim_attempted_at IS NOT NULL\n AND o.state='applied' AND o.verification='inconclusive' AND o.error_code IS NULL\n AND p.state='completed' AND p.operation_id='servers.probes.run' AND p.operation_version=1\n AND j.state='completed' AND j.execution_id=o.id AND j.mcp_connection_id=c.id AND j.actor_id=c.owner_id\n AND NEW.source->>'connectionId'=c.id::text AND NEW.source->>'planId'=p.id::text\n AND NEW.source->>'ownerId'=a.owner_id AND NEW.source->>'bindingProof'=a.binding_proof\n AND NEW.source->>'receiptProof'=b.receipt_proof\n AND NEW.source->>'ciphertextSha256'=encode(sha256(convert_to(b.receipt_ciphertext,'UTF8')),'hex')\n ) THEN RETURN NEW; END IF;\n ELSIF TG_OP='DELETE' THEN\n IF EXISTS(\n SELECT 1 FROM public.server_probe_retirement_claims r\n JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=r.operation_id AND t.plan_id=r.plan_id AND t.connection_id=r.connection_id\n WHERE r.operation_id=OLD.operation_id AND r.receipt_id=OLD.receipt_id AND r.source_proof=OLD.source_proof\n AND r.claim_xid<>pg_current_xact_id() AND r.claim_token::text=current_setting('mm.probe_retirement_claim',true)\n AND t.action_id='servers.probes.run' AND NOT t.target_global AND t.idempotency_digest=OLD.idempotency_digest\n AND OLD.source->'serverIds'=to_jsonb(t.target_server_ids)\n ) THEN RETURN OLD; END IF;\n END IF;\n RAISE EXCEPTION 'diagnostic terminal proof must match its original source or exact retirement claim';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["search_path=pg_catalog, public"],"prokind":"f","proname":"server_probe_terminal_proof_guard","prosecdef":false}, + {"acl":null,"arguments":"id uuid","definition":"CREATE OR REPLACE FUNCTION public.server_poll_storage_gc_allowed(id uuid)\n RETURNS boolean\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nDECLARE a public.server_poll_storage_admissions%ROWTYPE; retention integer; activity timestamptz; terminal_at timestamptz;\nBEGIN\n IF current_setting('mm.poll_storage_gc',true) IS DISTINCT FROM 'on' THEN RETURN false; END IF;\n PERFORM pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0));\n SELECT * INTO a FROM public.server_poll_storage_admissions WHERE receipt_id=id FOR UPDATE;\n IF NOT FOUND OR a.authority_expires_at>=clock_timestamp() OR a.gc_not_before>=clock_timestamp() THEN RETURN false; END IF;\n SELECT requests_retention_days INTO retention FROM public.mcp_settings WHERE mcp_settings.id=1 FOR SHARE;\n IF retention IS NULL OR retention NOT BETWEEN 1 AND 365 THEN RETURN false; END IF;\n terminal_at:=a.created_at;\n IF a.identity_kind='mcp' THEN\n IF NOT pg_try_advisory_xact_lock(hashtextextended('mcp-operation:'||a.execution_id::text,0)) THEN RETURN false; END IF;\n BEGIN\n SELECT GREATEST(o.finished_at,p.expires_at) INTO terminal_at FROM public.mcp_operations o\n JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id JOIN public.mcp_connections c ON c.id=o.connection_id\n WHERE o.id=a.execution_id AND o.connection_id=a.connection_id AND c.owner_id=a.owner_id AND p.operation_id='servers.poll'\n AND o.state IN('applied','failed','unknown','rejected','partial','cancelled') AND o.finished_at IS NOT NULL FOR UPDATE OF o,p NOWAIT;\n IF NOT FOUND THEN\n IF a.storage_state<>'compacted' OR EXISTS(SELECT 1 FROM public.mcp_operations o WHERE o.id=a.execution_id) OR NOT EXISTS (\n SELECT 1 FROM public.mcp_operation_replay_tombstones t JOIN public.mcp_connections c ON c.id=t.connection_id\n WHERE t.operation_id=a.execution_id AND t.connection_id=a.connection_id AND c.owner_id=a.owner_id\n AND t.action_id='servers.poll' AND t.target_global AND cardinality(t.target_server_ids)>0\n AND NOT EXISTS(SELECT 1 FROM public.mcp_plans WHERE mcp_plans.id=t.plan_id)\n ) OR EXISTS(SELECT 1 FROM public.server_poll_archives r WHERE r.receipt_id=id AND\n (r.version<>2 OR r.execution_id<>a.execution_id OR r.connection_id<>a.connection_id OR r.owner_id<>a.owner_id\n OR r.original_activity_at IS DISTINCT FROM a.original_activity_at OR r.original_expires_at<>a.authority_expires_at\n OR r.binding_proof<>a.binding_proof OR r.context_proof<>a.context_proof OR r.ciphertext_bytes<>a.archive_bytes))\n THEN RETURN false; END IF;\n terminal_at:=a.original_activity_at;\n END IF;\n IF terminal_at IS NULL THEN RETURN false; END IF;\n EXCEPTION WHEN lock_not_available THEN RETURN false; END;\n END IF;\n IF a.storage_state='compacted' THEN activity:=GREATEST(a.original_activity_at,a.authority_expires_at,terminal_at);\n ELSE\n SELECT GREATEST(a.authority_expires_at,a.created_at,COALESCE(a.claim_attempted_at,a.created_at),terminal_at,\n COALESCE((SELECT created_at FROM public.server_poll_executions WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT max(created_at) FROM public.server_poll_intents WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT max(created_at) FROM public.server_poll_acks WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT created_at FROM public.server_poll_receipts WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT created_at FROM public.server_poll_quarantines WHERE receipt_id=id),a.created_at)) INTO activity;\n END IF;\n RETURN isfinite(activity) AND activity+make_interval(days=>GREATEST(a.retention_days,retention))+a.safety_ms*interval '1 millisecond'=clock_timestamp() OR a.authority_expires_at>=clock_timestamp() THEN RETURN false; END IF;\n SELECT settings.requests_retention_days INTO current_retention FROM mcp_settings settings WHERE settings.id=1;\n IF current_retention IS NULL OR current_retention NOT BETWEEN 1 AND 365 THEN RETURN false; END IF;\n IF EXISTS(SELECT 1 FROM public.server_probe_retired_sources WHERE receipt_id=id) THEN\n RETURN public.server_probe_retired_gc_allowed(id,current_retention);\n END IF;\n terminal_at:=a.created_at;\n IF a.identity_kind='mcp' THEN\n SELECT GREATEST(o.finished_at,p.expires_at,COALESCE(cancellation.created_at,o.finished_at)) INTO terminal_at\n FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id\n JOIN mcp_connections owner_connection ON owner_connection.id=o.connection_id\n LEFT JOIN mcp_diagnostic_cancellations cancellation ON cancellation.operation_id=o.id\n WHERE o.id=a.execution_id AND owner_connection.owner_id=a.owner_id AND p.operation_id='servers.probes.run'\n AND o.state IN ('applied','failed','unknown','rejected','partial','cancelled') AND o.finished_at IS NOT NULL\n AND (cancellation.operation_id IS NULL OR(cancellation.connection_id=o.connection_id AND cancellation.actor_id=a.owner_id))\n FOR UPDATE OF o,p,owner_connection;\n IF NOT FOUND OR terminal_at IS NULL THEN RETURN false; END IF;\n END IF;\n SELECT GREATEST(a.created_at,a.authority_expires_at,terminal_at,COALESCE(a.cancelled_at,a.created_at),\n COALESCE((SELECT finished_at FROM server_probe_executions WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT created_at FROM server_probe_execution_quarantines WHERE receipt_id=id),a.created_at),\n COALESCE((SELECT max(GREATEST(created_at,COALESCE(received_at,created_at))) FROM server_probe_execution_stages WHERE receipt_id=id),a.created_at)) INTO activity;\n RETURN activity+make_interval(days=>GREATEST(a.retention_days,current_retention))+a.safety_ms*interval '1 millisecond'>'activityAt')::timestamptz,a.retention_days,a.safety_ms\n INTO activity,retained_days,safety\n FROM public.server_probe_retired_sources r\n JOIN public.server_probe_storage_admissions a ON a.receipt_id=r.receipt_id AND a.execution_id=r.operation_id\n JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=r.operation_id\n JOIN public.mcp_connections c ON c.id=t.connection_id AND c.owner_id=a.owner_id\n WHERE r.receipt_id=target_receipt AND r.proof=current_setting('mm.probe_retired_gc_proof',true)\n AND a.identity_kind='mcp' AND a.cancelled_at IS NULL AND a.claim_token IS NULL AND isfinite(a.claim_attempted_at)\n AND t.action_id='servers.probes.run' AND NOT t.target_global\n AND r.source->>'planId'=t.plan_id::text AND r.source->>'connectionId'=c.id::text\n AND r.source->'serverIds'=to_jsonb(t.target_server_ids) AND r.source->>'ownerId'=a.owner_id\n AND r.source->>'bindingProof'=a.binding_proof\n AND (r.source->>'authorityExpiresAt')::timestamptz=a.authority_expires_at\n AND (r.source->>'gcNotBefore')::timestamptz=a.gc_not_before\n AND (r.source->>'retentionDays')::int=a.retention_days AND (r.source->>'safetyMs')::int=a.safety_ms\n AND isfinite(a.authority_expires_at) AND isfinite(a.gc_not_before)\n AND a.authority_expires_atactivity))\n OR EXISTS(SELECT 1 FROM public.server_probe_execution_stages s WHERE s.receipt_id=target_receipt\n AND GREATEST(s.created_at,COALESCE(s.received_at,s.created_at))>activity)\n THEN RETURN false; END IF;\n RETURN activity+make_interval(days=>GREATEST(retained_days,current_retention))\n +GREATEST(safety,60000)*interval '1 millisecond' = Object.freeze({ + ...FIXED56_ROLE_TEMPLATE, + indexes: sorted([...FIXED56_ROLE_TEMPLATE.indexes, { + relname: "mcp_audit", + index_name: "mcp_audit_routing_charge_profile_window", + indisvalid: true, + indisready: true, + indisunique: false, + indisprimary: false, + definition: "CREATE INDEX mcp_audit_routing_charge_profile_window ON public.mcp_audit USING btree (connection_id, (((payload -> 'anchor'::text) ->> 'profileId'::text)), created_at, id) WHERE (action = 'routing.charge.admitted'::text)", + }]), +}) + +if (sha(canonical(FIXED57_ROLE_TEMPLATE)) !== BUILD57_PROVENANCE.templateSha256) { + throw new Error("Fixed57 writer catalogue changed") +} diff --git a/backend/scripts/writer-profile/fixed58-build-definition.test.mts b/backend/scripts/writer-profile/fixed58-build-definition.test.mts new file mode 100644 index 00000000..1a04a2bb --- /dev/null +++ b/backend/scripts/writer-profile/fixed58-build-definition.test.mts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { sorted } from "./catalogue-projection.js" +import { FIXED57_ROLE_TEMPLATE } from "./fixed57-build-definition.js" +import { FIXED58_ROLE_TEMPLATE, normalizeFixed58RestoreRows } from "./fixed58-build-definition.js" + +test("source58 accepts only the reviewed dump layouts and leaves raw catalogue data intact", () => { + const relation = "server_probe_executions" + const reviewed = FIXED57_ROLE_TEMPLATE.columns.filter(row => row.relname === relation) + const compact = reviewed.map(row => typeof row.attnum === "number" && row.attnum > 18 ? { ...row, attnum: row.attnum - 1 } : row) + assert.deepEqual(normalizeFixed58RestoreRows(relation, "columns", compact), sorted(reviewed)) + assert(compact.some(row => row.attnum === 18)) + const moved = compact.map(row => row.attnum === 18 ? { ...row, attnum: 16 } : row) + assert.notDeepEqual(normalizeFixed58RestoreRows(relation, "columns", moved), sorted(reviewed)) + const checks = FIXED58_ROLE_TEMPLATE.restorePortable.filter(row => typeof row.conname === "string") + assert.equal(checks.length, 3) + for (const check of checks) { + const original = FIXED57_ROLE_TEMPLATE.constraints.find(row => row.relname === check.relname && row.conname === check.conname)! + const restored = { ...original, definition: check.definition } + assert.deepEqual(normalizeFixed58RestoreRows(String(check.relname), "constraints", [restored]), [original]) + assert.equal(restored.definition, check.definition) + const weakened = { ...restored, definition: "CHECK (true)" } + assert.deepEqual(normalizeFixed58RestoreRows(String(check.relname), "constraints", [weakened]), [weakened]) + } +}) diff --git a/backend/scripts/writer-profile/fixed58-build-definition.ts b/backend/scripts/writer-profile/fixed58-build-definition.ts new file mode 100644 index 00000000..f42c5282 --- /dev/null +++ b/backend/scripts/writer-profile/fixed58-build-definition.ts @@ -0,0 +1,50 @@ +import { canonical, sha } from "./application-writer-profile-canonical.js" +import { sorted, type Inventory, type Row } from "./catalogue-projection.js" +import { BUILD57_PROVENANCE, FIXED57_ROLE_TEMPLATE } from "./fixed57-build-definition.js" +import { RESTORE_WRITER_PROFILE_DEFINITION_SHA256, RESTORE_WRITER_PROFILE_MANIFEST_SHA256, restoreProfileEnrollmentSql } from "../../src/db/database-restore-quarantine-profile.js" + +const compactedProbeLayout = { relname: "server_probe_executions", removedAttnum: 18 } +const restoredChecks = [ + { relname: "server_poll_archives", conname: "server_poll_archives_check", + definition: "CHECK (((ciphertext_bytes >= 1) AND (ciphertext_bytes <= 131072) AND (ciphertext_bytes = octet_length(ciphertext))))" }, + { relname: "server_poll_storage_admissions", conname: "server_poll_storage_admissions_owner_id_check", + definition: "CHECK (((length(owner_id) >= 1) AND (length(owner_id) <= 200) AND (btrim(owner_id) = owner_id) AND (owner_id !~ '[[:cntrl:]]'::text)))" }, + { relname: "server_poll_storage_admissions", conname: "server_poll_storage_admissions_check1", + definition: "CHECK (((used_rows >= 0) AND (used_rows <= 4019) AND (used_rows <= ((max_stages * 2) + 3))))" }, +] + +export const FIXED58_ROLE_TEMPLATE: Readonly = Object.freeze({ + ...FIXED57_ROLE_TEMPLATE, + schemas: sorted(FIXED57_ROLE_TEMPLATE.schemas.map(row => row.nspname === "mmapp_control" + ? { ...row, acl: [...row.acl as string[], "@mm-role:runtime@=U/@mm-role:control@"].sort() } : row)), + effectiveSchemas: sorted(FIXED57_ROLE_TEMPLATE.effectiveSchemas.map(row => row.nspname === "mmapp_control" && row.rolname === "@mm-role:runtime@" + ? { ...row, usage: true } : row)), + restoreControl: [{ enrollmentSha256: sha(restoreProfileEnrollmentSql("restore_runtime", "restore_operator")) }], + restorePortable: [compactedProbeLayout, ...restoredChecks], +}) + +export function normalizeFixed58RestoreRows(relation: string, group: string, rows: Row[]): Row[] { + if (relation === compactedProbeLayout.relname && group === "columns" && rows.some(row => row.attnum === compactedProbeLayout.removedAttnum)) { + // Exact comparison below still checks names, their order, types, ACL and other properties. + return sorted(rows.map(row => typeof row.attnum === "number" && row.attnum >= compactedProbeLayout.removedAttnum + ? { ...row, attnum: row.attnum + 1 } : row)) + } + if (group === "constraints" && restoredChecks.some(check => check.relname === relation)) { + return sorted(rows.map(row => { + const restored = restoredChecks.find(check => check.relname === relation && check.conname === row.conname && check.definition === row.definition) + return restored ? { ...row, definition: FIXED57_ROLE_TEMPLATE.constraints.find(original => + original.relname === relation && original.conname === restored.conname)!.definition } : row + })) + } + return rows +} + +export const BUILD58_PROVENANCE = Object.freeze({ + format: "source58-restore-profile-v1", + manifestSha256: RESTORE_WRITER_PROFILE_MANIFEST_SHA256, + templateSha256: RESTORE_WRITER_PROFILE_DEFINITION_SHA256, +}) + +if (sha(canonical(FIXED58_ROLE_TEMPLATE)) !== BUILD58_PROVENANCE.templateSha256 || BUILD58_PROVENANCE.manifestSha256 !== BUILD57_PROVENANCE.manifestSha256) { + throw new Error("Fixed58 writer catalogue changed") +} diff --git a/backend/scripts/writer-profile/legacy-cold-verification.mts b/backend/scripts/writer-profile/legacy-cold-verification.mts new file mode 100644 index 00000000..30773f23 --- /dev/null +++ b/backend/scripts/writer-profile/legacy-cold-verification.mts @@ -0,0 +1,95 @@ +import assert from 'node:assert/strict' +import {createPrivateKey,createPublicKey,sign} from 'node:crypto' +import path from 'node:path' +import {z} from 'zod' +import {assertJsDriverEnvironment,poolOptions,retainPublisherTlsPeer} from './operator-contract.mjs' +import {readProtected,withJournalLease,writeFile,sha} from './operator-files.mjs' +import {readProtectedDatabaseSourceBindingRegistry} from './protected-source-registry.mjs' +import {sourceBindingBodySchema,sourceBindingBytes,sourceBindingCanonical} from '../../src/db/database-source-binding.js' +import {assertDatabaseSourceObservationCurrent,captureDatabaseSourceObservation} from '../../src/db/database-source-observation.js' +import {verifyLegacyProbeArchiveApproval} from '../../src/services/server-probe-legacy-archive-approval.js' +import {captureLegacyProbeCredentialDigest,LEGACY_COLD_DIGEST,LEGACY_PROBE_COLD_PROOF_DOMAIN,legacyColdProofBody} from '../../src/services/server-probe-legacy-cold-proof.js' +import {captureLegacyProbeLineage,captureLegacyProbeStorage} from '../../src/services/server-probe-legacy-source-inventory.js' + +const approvalSchema=z.object({format:z.literal('legacy-probe-cold-verification-approval-v1'),installationId:z.uuid(), + sourceHeadSha256:LEGACY_COLD_DIGEST,sourceSignerSpkiSha256:LEGACY_COLD_DIGEST,custodySha256:LEGACY_COLD_DIGEST,coldArchiveSha256:LEGACY_COLD_DIGEST, + coldSignerSpkiSha256:LEGACY_COLD_DIGEST,keySha256:LEGACY_COLD_DIGEST,coldSource:sourceBindingBodySchema.shape.source, + coldRoleOid:z.string().regex(/^[1-9][0-9]{0,9}$/),tls:z.object({serverName:z.string().regex(/^[A-Za-z0-9][A-Za-z0-9.-]{0,252}$/), + caSha256:LEGACY_COLD_DIGEST,spkiSha256:LEGACY_COLD_DIGEST}).strict(),expiresAt:z.number().int().positive()}).strict() +const parse=(bytes:Buffer)=>JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(bytes)) +export async function verifyLegacyColdMain(args:string[]){ + assert(args[0]==='legacy-cold-verify');assertJsDriverEnvironment(process.env) + const flags=['approval','approval-sha256','transport','transport-sha256','ca','source-registry','archive-directory','legacy-key-directory','cold-signing-key','output-directory'] + assert.equal(args.length,1+flags.length*2) + const values=new Map() + for(let i=1;ivalues.get(name)! + const approvalSha256=LEGACY_COLD_DIGEST.parse(get('approval-sha256')) + const approval=approvalSchema.parse(parse(await readProtected(get('approval'),8192,approvalSha256))) + assert(approval.expiresAt>Date.now()&&approval.expiresAt-Date.now()<=900000&&approval.coldSignerSpkiSha256!==approval.sourceSignerSpkiSha256) + const deadlineAt=Math.min(Date.now()+60000,approval.expiresAt),pin={registryDirectory:get('source-registry'),installationId:approval.installationId, + publicKeySpkiSha256:approval.sourceSignerSpkiSha256,currentHeadSha256:approval.sourceHeadSha256} + const directory=get('archive-directory'),maxArchive=64*1024*1024 + const [ca,transport,archive,signedApproval,coldArchive,operatorPublicKeyPem]=await Promise.all([ + readProtected(get('ca'),65536,approval.tls.caSha256),readProtected(get('transport'),16384,LEGACY_COLD_DIGEST.parse(get('transport-sha256'))), + readProtected(path.join(directory,'legacy-probe.dump'),maxArchive),readProtected(path.join(directory,'legacy-probe-approval.json'),4096,approval.custodySha256), + readProtected(path.join(directory,'legacy-probe-cold.dump'),maxArchive,approval.coldArchiveSha256),readProtected(path.join(pin.registryDirectory,'operator-public.pem'),4096), + ]) + const legacyKey=await readProtected(path.join(get('legacy-key-directory'),'encryption-key'),32,approval.keySha256) + try{assert.equal(legacyKey.length,32)}finally{legacyKey.fill(0)} + const keyBytes=await readProtected(get('cold-signing-key'),8192) + let key + try{key=createPrivateKey(keyBytes)}finally{keyBytes.fill(0)} + assert(key.asymmetricKeyType==='ed25519'&&sha(createPublicKey(key).export({type:'spki',format:'der'}))===approval.coldSignerSpkiSha256) + assert.equal(coldArchive.subarray(0,5).toString(),'PGDMP') + const custody=await verifyLegacyProbeArchiveApproval(pin,{archive,signedApproval,operatorPublicKeyPem}) + assert.equal(custody.format,'legacy-probe-custody-approval-v1') + const original=await readProtectedDatabaseSourceBindingRegistry(pin,{deadlineAt}) + const cold=approval.coldSource + assert(cold.systemIdentifier!==original.body.source.systemIdentifier||cold.databaseOid!==original.body.source.databaseOid,'Cold database must differ from the original source') + assert(cold.serverVersionNum>=180000&&cold.serverVersionNum<190000,'Cold verification requires PostgreSQL 18') + assert.equal(cold.currentUser,cold.sessionUser) + const options=poolOptions(parse(transport),{source:cold,roles:{publisher:{name:cold.sessionUser}},tls:approval.tls},ca) + const {default:pg}=await import('pg');assertJsDriverEnvironment(process.env) + return withJournalLease(get('output-directory'),async()=>{ + const pool=new pg.Pool(options) + let failed=false;pool.on('error',()=>{failed=true}) + let hashes + try{ + const client=await pool.connect() + try{ + const retained=retainPublisherTlsPeer(client,approval.tls) + const sql={query:async(text:string,values?:unknown[])=>{ + assert(!failed&&Date.now()0&&storage.sha256===custody.storageSha256&&lineage.sha256===custody.lineageSha256,'Restored cold rows differ from signed custody') + hashes={storageSha256:storage.sha256,lineageSha256:lineage.sha256,credentialSha256:await captureLegacyProbeCredentialDigest(sql)} + await assertDatabaseSourceObservationCurrent(sql,observation,{deadlineAt}) + assert.equal((await sql.query('ROLLBACK')).command,'ROLLBACK') + } finally {client.release(true)} + } finally {await pool.end()} + assert(!failed&&Date.now()JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(bytes)) + +export async function legacyControlReferenceMain(args:string[]) { + const preflight=args[0]==='legacy-source58-preflight' + const flags=['bootstrap','bootstrap-sha256','publisher-transport','publisher-transport-sha256', + 'ca','source-registry','source-spki-sha256','activation-directory','activation-sha256','approval','approval-sha256','cold-public-key','cold-spki-sha256'] + if(!preflight)flags.push('operator-transport','operator-transport-sha256') + assert((preflight||args[0]==='legacy-control-reference')&&args.length===1+2*flags.length) + assertJsDriverEnvironment(process.env) + const values=new Map() + for(let i=1;ivalues.get(name)! + for(const flag of flags.filter(name=>name.endsWith('sha256')))assert(/^[a-f0-9]{64}$/.test(get(flag))) + await protectedDirectory(mcpStateDirectory()) + const bootstrap=parse(await readProtected(get('bootstrap'),32768,get('bootstrap-sha256'))),request=deploymentRequest(bootstrap.deploymentRequest) + assert(bootstrap.format==='application-writer-bootstrap-v1'&&bootstrap.publicationAuthorized===false&&bootstrap.readyAuthorized===false) + const sourcePin={registryDirectory:get('source-registry'),installationId:request.installationId, + publicKeySpkiSha256:get('source-spki-sha256'),currentHeadSha256:request.sourceHeadSha256} + const [bytes,coldKey]=await Promise.all([readProtected(get('approval'),8192,get('approval-sha256')),readProtected(get('cold-public-key'),4096)]) + const pins={coldSpkiSha256:get('cold-spki-sha256'),sourceSpkiSha256:sourcePin.publicKeySpkiSha256, + sourceHeadSha256:sourcePin.currentHeadSha256,sourceTlsSpkiSha256:request.tls.spkiSha256,roles:request.roles} + const approval=preflight?undefined:verifyLegacyControlReferenceApproval(bytes,coldKey,pins) + const transition=preflight?verifyLegacySource58PreflightApproval(bytes,coldKey,pins):undefined + const ca=await readProtected(get('ca'),65536,request.tls.caSha256) + const transport=async(role:'publisher'|'operator')=>poolOptions(parse(await readProtected(get(role+'-transport'),16384,get(role+'-transport-sha256'))),request,ca,role) + const [publisher,application]=await Promise.all([transport('publisher'),preflight?undefined:transport('operator')]) + const {default:pg}=await import('pg') + assertJsDriverEnvironment(process.env) + const deadlineAt=Math.min((transition??approval)!.expiresAt,Date.now()+120000) + const expectedSource={...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:request.roles.publisher.name,currentUser:request.roles.publisher.name} + return withActiveSource({directory:get('activation-directory'),expectedSha256:get('activation-sha256'),sourcePin,expectedSource, + tlsSpkiSha256:request.tls.spkiSha256,deadlineAt},createProtectedDatabaseSourceBindingVerifier(sourcePin),async verifier=>{ + const source=new pg.Pool(publisher),liveApplication=application&&new pg.Pool(application) + let failed=false + for(const pool of [source,liveApplication])pool?.on('error',()=>{failed=true}) + const retain=(client:PoolClient)=>{ + const peer=retainPublisherTlsPeer(client,request.tls) + return ()=>{assert(!failed);peer()} + } + async function observe(inspect?:(sql:SchemaInventoryClient)=>Promise){ + const client=await source.connect() + try{ + const peer=retain(client),sql={query:async(text:string,parameters?:unknown[])=>{ + peer();assert(Date.now()inspectApprovedLegacySource58Predecessor(sql,transition,deadlineAt)) + return {event:'database-legacy-source58-preflight',outcome:'observed',...observation} + } + assert(approval&&application&&liveApplication) + await observe() + const result=await provisionControlledSchemaReferenceOn({approval,liveApplication,application,publisher,systemIdentifier:request.source.systemIdentifier, + sourceDatabaseOid:request.source.databaseOid,retain,beforePublish:async()=>{await observe()}}) + return {event:'database-legacy-control-reference',outcome:'committed',...result} + }finally{await Promise.all([source.end(),liveApplication?.end()])} + }) +} diff --git a/backend/scripts/writer-profile/legacy-handoff-execution.mts b/backend/scripts/writer-profile/legacy-handoff-execution.mts new file mode 100644 index 00000000..7653fab1 --- /dev/null +++ b/backend/scripts/writer-profile/legacy-handoff-execution.mts @@ -0,0 +1,243 @@ +import assert from 'node:assert/strict' +import {createPrivateKey,createPublicKey,sign,verify} from 'node:crypto' +import path from 'node:path' +import {lstat} from 'node:fs/promises' +import {z} from 'zod' +import {assertJsDriverEnvironment,poolOptions,retainPublisherTlsPeer} from './operator-contract.mjs' +import {readProtected,writeFile,sha} from './operator-files.mjs' +import {createProtectedDatabaseSourceBindingVerifier} from './protected-source-registry.mjs' +import {withActiveSource} from './source-activation.mjs' +import {publishLegacyHandoffIntent} from './publisher.mjs' +import {deploymentRequest} from '../database-writer-bootstrap-operator.mjs' +import {acquireDatabaseSchemaControl} from '../../src/db/database-schema-control.js' +import {assertDatabaseRestoreQuarantineOpen} from '../../src/db/database-restore-quarantine.js' +import {captureDatabaseSourceObservation,assertDatabaseSourceObservationCurrent} from '../../src/db/database-source-observation.js' +import {sourceBindingBytes,sourceBindingCanonical} from '../../src/db/database-source-binding.js' +import {readReviewedLegacyProbe0037} from '../../src/db/legacy-probe-0037-body.js' +import {migrationManifestDigest} from '../../src/db/migration-manifest.js' +import {SYSTEM_DATABASE_OPERATION_LOCK,SYSTEM_DATABASE_REQUEST_LOCK} from '../../src/db/operation-lock.js' +import {LEGACY_COLD_DIGEST,verifyLegacyColdProof,captureLegacyProbeCredentialDigest} from '../../src/services/server-probe-legacy-cold-proof.js' +import {verifyLegacyProbeArchiveApproval} from '../../src/services/server-probe-legacy-archive-approval.js' +import {captureLegacyHandoffSchema,checkLegacyHandoffPostState,LEGACY_HANDOFF_ADOPTION_RELATIONS,LEGACY_PROBE_HANDOFF_DOMAIN, + verifyLegacyHandoffAdoption,verifyLegacyProbeHandoffIntent} from '../../src/services/server-probe-legacy-handoff-inspection.js' +import {captureLegacyProbeStorage,LEGACY_PROBE_CUSTODY_RELATIONS,LEGACY_PROBE_RELATIONS} from '../../src/services/server-probe-legacy-source-inventory.js' +import type {PoolClient} from 'pg' + +export const LEGACY_HANDOFF_EXECUTION_DOMAIN='mmapp:legacy-probe-0037-execute:ed25519:v1\n' +export const LEGACY_HANDOFF_RELEASE_DOMAIN='mmapp:legacy-probe-0037-release:ed25519:v1\n' +const hash=LEGACY_COLD_DIGEST +const approvalEnvelope=z.object({body:z.object({format:z.literal('legacy-probe-0037-execution-approval-v1'),decision:z.literal('execute-and-commit'), + operationId:z.uuidv4(),sourceHeadSha256:hash,sourceTlsSpkiSha256:hash,custodySha256:hash,coldArchiveSha256:hash,coldProofSha256:hash, + coldApprovalSha256:hash,preSchemaSha256:hash,postSchemaSha256:hash,manifestSha256:hash,migrationSha256:hash, + protectedRuntimePaused:z.literal(true),expiresAt:z.number().int().positive()}).strict(),signature:z.string().regex(/^[A-Za-z0-9_-]{86}$/)}).strict() +const releaseEnvelope=approvalEnvelope.extend({body:z.object({format:z.literal('legacy-probe-0037-release-approval-v1'), + decision:z.literal('release-migration-quarantine'),operationId:z.uuidv4(),intentSha256:hash,adoptionSha256:hash,sourceHeadSha256:hash, + sourceTlsSpkiSha256:hash,coldArchiveSha256:hash,coldProofSha256:hash,postSchemaSha256:hash,manifestSha256:hash,migrationSha256:hash, + protectedRuntimePaused:z.literal(true),expiresAt:z.number().int().positive()}).strict()}) +const parse=(raw:Buffer)=>JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(raw)) +const pendingUnknown=()=>Object.assign(Error('Legacy 0037 outcome must be inspected'),{code:'LEGACY_HANDOFF_EXECUTION_UNKNOWN'}) + +export async function legacyHandoffExecuteMain(args:string[]){ + const release=args[0]==='legacy-handoff-release',adopt=release||args[0]==='legacy-handoff-adopt' + assert(adopt||args[0]==='legacy-handoff-execute');assertJsDriverEnvironment(process.env) + const flags=['bootstrap','bootstrap-sha256','publisher-transport','publisher-transport-sha256','operator-transport','operator-transport-sha256', + 'control-transport','control-transport-sha256','ca','source-registry','source-spki-sha256','activation-directory','activation-sha256', + 'archive-directory','legacy-key-directory','approval','approval-sha256','cold-public-key','cold-spki-sha256',adopt?'handoff-intent-sha256':'source-signing-key'] + if(release)flags.push('release-approval','release-approval-sha256') + assert.equal(args.length,1+flags.length*2) + const values=new Map() + for(let i=1;ivalues.get(name)! + for(const flag of flags.filter(name=>name.endsWith('sha256')))hash.parse(get(flag)) + const bootstrap=parse(await readProtected(get('bootstrap'),32768,get('bootstrap-sha256'))),request=deploymentRequest(bootstrap.deploymentRequest) + assert(bootstrap.format==='application-writer-bootstrap-v1'&&bootstrap.publicationAuthorized===false&&bootstrap.readyAuthorized===false) + const sourcePin=Object.freeze({registryDirectory:get('source-registry'),installationId:request.installationId, + publicKeySpkiSha256:get('source-spki-sha256'),currentHeadSha256:request.sourceHeadSha256}) + const coldSignerSpkiSha256=get('cold-spki-sha256'),coldPublicKey=await readProtected(get('cold-public-key'),4096) + const approvingKey=createPublicKey(coldPublicKey) + assert(coldSignerSpkiSha256!==sourcePin.publicKeySpkiSha256&&approvingKey.asymmetricKeyType==='ed25519'&&sha(approvingKey.export({type:'spki',format:'der'}))===coldSignerSpkiSha256) + const approvalBytes=await readProtected(get('approval'),8192,get('approval-sha256')),raw=parse(approvalBytes) + const {body:approval,signature}=approvalEnvelope.parse(raw),signatureBytes=Buffer.from(signature,'base64url') + assert(sourceBindingBytes(raw).equals(approvalBytes)&&signatureBytes.toString('base64url')===signature&&verify(null, + Buffer.from(LEGACY_HANDOFF_EXECUTION_DOMAIN+sourceBindingCanonical(approval)),approvingKey,signatureBytes),'Independent exact 0037 execution approval required') + const {body:migration,pin}=await readReviewedLegacyProbe0037() + assert((adopt||approval.expiresAt>Date.now()&&approval.expiresAt-Date.now()<=900000)&&approval.migrationSha256===pin.sha256&&approval.manifestSha256===migrationManifestDigest()&& + approval.sourceHeadSha256===sourcePin.currentHeadSha256&&approval.sourceTlsSpkiSha256===request.tls.spkiSha256) + const archiveDirectory=get('archive-directory'),max=64*1024*1024 + const [archive,signedApproval,coldProof,operatorPublicKeyPem]=await Promise.all([ + readProtected(path.join(archiveDirectory,'legacy-probe.dump'),max),readProtected(path.join(archiveDirectory,'legacy-probe-approval.json'),4096,approval.custodySha256), + readProtected(path.join(archiveDirectory,'legacy-probe-cold-proof.json'),8192,approval.coldProofSha256),readProtected(path.join(sourcePin.registryDirectory,'operator-public.pem'),4096), + ]) + const coldArchive=await readProtected(path.join(archiveDirectory,'legacy-probe-cold.dump'),max,approval.coldArchiveSha256) + const custody=await verifyLegacyProbeArchiveApproval(sourcePin,{archive,signedApproval,operatorPublicKeyPem}) + assert.equal(custody.format,'legacy-probe-custody-approval-v1') + const coldPin={publicKeyPem:coldPublicKey,signerSpkiSha256:coldSignerSpkiSha256,sourceSignerSpkiSha256:sourcePin.publicKeySpkiSha256, + approvalSha256:approval.coldApprovalSha256,custodySha256:approval.custodySha256,coldArchiveSha256:approval.coldArchiveSha256, + storageSha256:custody.storageSha256,lineageSha256:custody.lineageSha256} + const coldObservedAt=adopt?parse(coldProof)?.body?.observedAt:undefined + if(adopt)assert(Number.isSafeInteger(coldObservedAt)) + const cold=verifyLegacyColdProof(coldProof,coldPin,coldObservedAt) + const legacyKey=await readProtected(path.join(get('legacy-key-directory'),'encryption-key'),32,cold.keySha256) + try{assert.equal(legacyKey.length,32)}finally{legacyKey.fill(0)} + let key,adopting + let releaseProof:{approvalSha256:string;adoptionSha256:string}|undefined,releaseExpiresAt=Infinity + const stage=release?'release':'adoption' + const adoptionUnknownFile=path.join(archiveDirectory,`legacy-probe-handoff-${stage}-unknown.json`) + const adoptionAttemptFile=path.join(archiveDirectory,`legacy-probe-handoff-${stage}-attempt.json`) + if(adopt){ + for(const file of [adoptionAttemptFile,adoptionUnknownFile]){ + try{await lstat(file);throw Error(`Legacy ${stage} attempt requires inspection`)}catch(error){if((error as NodeJS.ErrnoException).code!=='ENOENT')throw error} + } + const intent=await readProtected(path.join(archiveDirectory,'legacy-probe-handoff-intent.json'),4096,get('handoff-intent-sha256')) + adopting=await verifyLegacyProbeHandoffIntent(sourcePin,{archive,signedApproval,coldArchive,intent,operatorPublicKeyPem}) + assert(adopting.operationId===approval.operationId&&adopting.approvalSha256===approval.custodySha256&&adopting.coldArchiveSha256===approval.coldArchiveSha256&& + adopting.preSchemaSha256===approval.preSchemaSha256&&adopting.postSchemaSha256===approval.postSchemaSha256&& + adopting.executionApprovalSha256===sha(approvalBytes)&&adopting.coldProofSha256===approval.coldProofSha256,'Execution approval differs from durable handoff') + if(release){ + const bytes=await readProtected(get('release-approval'),8192,get('release-approval-sha256')),raw=parse(bytes) + const {body,signature}=releaseEnvelope.parse(raw),sig=Buffer.from(signature,'base64url') + assert(sourceBindingBytes(raw).equals(bytes)&&sig.toString('base64url')===signature&&verify(null, + Buffer.from(LEGACY_HANDOFF_RELEASE_DOMAIN+sourceBindingCanonical(body)),approvingKey,sig),'Independent signed release approval required') + assert(body.operationId===adopting.operationId&&body.intentSha256===adopting.intentSha256&&body.sourceHeadSha256===approval.sourceHeadSha256&& + body.sourceTlsSpkiSha256===approval.sourceTlsSpkiSha256&&body.coldArchiveSha256===approval.coldArchiveSha256&&body.coldProofSha256===approval.coldProofSha256&& + body.postSchemaSha256===approval.postSchemaSha256&&body.manifestSha256===approval.manifestSha256&&body.migrationSha256===approval.migrationSha256&& + body.expiresAt>Date.now()&&body.expiresAt-Date.now()<=900000,'Release approval differs from exact legacy handoff') + releaseProof={approvalSha256:sha(bytes),adoptionSha256:body.adoptionSha256};releaseExpiresAt=body.expiresAt + } + }else{ + const keyBytes=await readProtected(get('source-signing-key'),8192) + try{key=createPrivateKey(keyBytes)}finally{keyBytes.fill(0)} + assert(key.asymmetricKeyType==='ed25519'&&sha(createPublicKey(key).export({type:'spki',format:'der'}))===sourcePin.publicKeySpkiSha256) + } + const ca=await readProtected(get('ca'),65536,request.tls.caSha256) + const transport=async(role:'publisher'|'operator'|'control')=>poolOptions(parse(await readProtected(get(role+'-transport'),16384,get(role+'-transport-sha256'))),request,ca,role) + const [publisherOptions,applicationOptions,controlOptions]=await Promise.all([transport('publisher'),transport('operator'),transport('control')]) + const {default:pg}=await import('pg');assertJsDriverEnvironment(process.env) + const deadlineAt=adopt?Math.min(Date.now()+120000,releaseExpiresAt):Math.min(Date.now()+120000,approval.expiresAt,cold.expiresAt) + const expectedSource={...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:request.roles.publisher.name,currentUser:request.roles.publisher.name} + const verifier=createProtectedDatabaseSourceBindingVerifier(sourcePin) + return withActiveSource({directory:get('activation-directory'),expectedSha256:get('activation-sha256'),sourcePin,expectedSource, + tlsSpkiSha256:request.tls.spkiSha256,deadlineAt},verifier,async sourceVerifier=>{ + const publisher=new pg.Pool(publisherOptions),application=new pg.Pool({...applicationOptions,application_name:'mmapp-legacy-handoff-owner'}),controlPool=new pg.Pool(controlOptions) + let failed=false,committed=false,commitSent=false,controlUnknown=false,recorded=false,app:PoolClient|undefined,control + let intentSha256:string|undefined,cleanupUnconfirmed=false + for(const pool of [publisher,application,controlPool])pool.on('error',()=>{failed=true}) + const fence=()=>assert(!failed&&Date.now(){ + const retained=retainPublisherTlsPeer(client,request.tls) + return {query:async(text:string,values?:unknown[])=>{fence();retained();const result=await client.query(text,values);retained();fence();return result}} + } + const assertRole=async(sql:ReturnType,role:'publisher'|'operator'|'control')=>{ + const {rows:[actual]}=await sql.query(`SELECT r.rolname AS actor,r.oid::text AS role_oid,d.oid::text AS db_oid,d.datname AS db_name, + c.system_identifier::text AS system,pg_catalog.current_setting('server_version_num')::int AS version,pg_catalog.pg_is_in_recovery() AS recovery + FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_database d CROSS JOIN pg_catalog.pg_control_system() c + WHERE r.rolname=CURRENT_USER AND r.rolname=SESSION_USER AND d.datname=pg_catalog.current_database()`) + assert.deepEqual(actual,{actor:request.roles[role].name,role_oid:request.roles[role].signedOid,db_oid:request.source.databaseOid, + db_name:request.source.databaseName,system:request.source.systemIdentifier,version:request.source.serverVersionNum,recovery:false}) + } + const observePublisher=async(checkSchema:boolean,inspect?:(sql:ReturnType)=>Promise)=>{ + const client=await publisher.connect() + try{ + const sql=retainedSql(client) + await assertRole(sql,'publisher') + await sql.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') + await sql.query("SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'") + const observation=await captureDatabaseSourceObservation(sql,{deadlineAt}) + await sourceVerifier.verify(sql,observation,{deadlineAt}) + if(checkSchema)assert.equal(await captureLegacyHandoffSchema(sql,deadlineAt),approval.preSchemaSha256,'Original 0036 catalogue differs from reviewed approval') + const result=await inspect?.(sql) + await assertDatabaseSourceObservationCurrent(sql,observation,{deadlineAt}) + assert.equal((await sql.query('ROLLBACK')).command,'ROLLBACK') + return result + }finally{client.release(true)} + } + try{ + control=await acquireDatabaseSchemaControl({async connect(){ + const client=await controlPool.connect() + try{ + const sql=retainedSql(client);await assertRole(sql,'control') + if(adopting){ + const {rows:[gate]}=await sql.query(`SELECT q.operation_id::text AS active,s.state->'intent'->>'id' AS pending, + s.state->'terminal'->'intent'->>'id' AS adopted + FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1`) + assert(gate?.active===adopting.operationId&&(release?gate.pending===null&&gate.adopted===adopting.operationId:gate.pending===adopting.operationId),'Exact quarantined legacy phase required') + }else await assertDatabaseRestoreQuarantineOpen(sql) + return {...sql,release:(destroy?:boolean)=>client.release(destroy)} + }catch(error){client.release(true);throw error} + }}) + app=await application.connect();const sql=retainedSql(app) + await assertRole(sql,'operator') + for(const lock of [SYSTEM_DATABASE_REQUEST_LOCK,SYSTEM_DATABASE_OPERATION_LOCK,8723110]){ + const {rows:[row]}=await sql.query('SELECT pg_catalog.pg_try_advisory_lock($1::bigint) AS held',[lock]);assert.equal(row.held,true,'Legacy exclusive drain unavailable') + } + await sql.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ WRITE') + if(adopting){ + await sql.query(`LOCK TABLE ${LEGACY_HANDOFF_ADOPTION_RELATIONS.map(name=>`public.${name}`).join(', ')} IN SHARE MODE NOWAIT`) + const adoption=await observePublisher(false,observer=>verifyLegacyHandoffAdoption(sql,observer,adopting,deadlineAt,releaseProof)) + assert(adoption);await observePublisher(false) + assert(adoption.appliedAt<=Date.now()&&adoption.appliedAt<=approval.expiresAt&&approval.expiresAt-adoption.appliedAt<=900000,'Original approval was outside its signed execution window') + verifyLegacyColdProof(coldProof,coldPin,adoption.appliedAt) + await writeFile(adoptionAttemptFile,sourceBindingBytes({format:`legacy-probe-0037-${stage}-attempt-v1`, + operationId:adopting.operationId,intentSha256:adopting.intentSha256,automaticReplay:false}),{flag:'wx',mode:0o600}) + try{ + const receipt=release?await control.releaseLegacyHandoff(adoption):await control.adoptLegacyHandoff(adoption) + const terminal=receipt.state.terminal! + return {event:`database-legacy-handoff-${stage}`,outcome:'committed',operationId:adopting.operationId,intentSha256:adopting.intentSha256, + observationSha256:terminal.legacyRelease?.observationSha256??terminal.observationSha256,quarantineActive:!release,automaticReplay:false,readyAuthorized:false} + }catch(error){ + if((error as {outcomeUnknown?:boolean})?.outcomeUnknown===true){ + const value=sourceBindingBytes({format:`legacy-probe-0037-${stage}-unknown-v1`,operationId:adopting.operationId,intentSha256:adopting.intentSha256,automaticReplay:false}) + try{await writeFile(adoptionUnknownFile,value,{flag:'wx',mode:0o600})}catch{/* Preserve the existing intent and control history. */} + throw Object.assign(Error(`Legacy 0037 ${stage} must be inspected`),{code:release?'LEGACY_HANDOFF_RELEASE_UNKNOWN':'LEGACY_HANDOFF_ADOPTION_UNKNOWN'}) + } + throw error + } + } + const historyTables=[...LEGACY_PROBE_CUSTODY_RELATIONS,'mcp_credentials','mcp_settings'].sort().map(name=>`public.${name}`).join(', ') + await sql.query(`LOCK TABLE ${historyTables} IN SHARE MODE NOWAIT`) + await observePublisher(true) + await sql.query(`LOCK TABLE ${historyTables} IN ACCESS EXCLUSIVE MODE NOWAIT`) + await captureLegacyProbeStorage(sql) + assert.equal(await captureLegacyProbeCredentialDigest(sql),cold.credentialSha256,'Original credentials differ from verified cold rows') + const {rows:[triggers]}=await sql.query(`SELECT EXISTS(SELECT FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid + WHERE c.relnamespace='public'::regnamespace AND c.relname::text=ANY($1::text[]) AND (t.tgtype::int & 32)<>0) AS truncate_trigger`,[LEGACY_PROBE_RELATIONS]) + assert.equal(triggers.truncate_trigger,false,'Unreviewed diagnostic TRUNCATE trigger') + const {rows:[{xid}]}=await sql.query('SELECT pg_catalog.pg_current_xact_id()::text AS xid') + const intent={format:'legacy-probe-0037-handoff-intent-v1',operationId:approval.operationId,applicationXid:xid, + approvalSha256:approval.custodySha256,coldArchiveSha256:approval.coldArchiveSha256,preSchemaSha256:approval.preSchemaSha256, + postSchemaSha256:approval.postSchemaSha256,manifestSha256:approval.manifestSha256,migrationSha256:approval.migrationSha256, + executionApprovalSha256:sha(approvalBytes),coldProofSha256:approval.coldProofSha256} + assert(key) + const signedIntent=sourceBindingBytes({body:intent,signature:sign(null,Buffer.from(LEGACY_PROBE_HANDOFF_DOMAIN+sourceBindingCanonical(intent)),key).toString('base64url')}) + intentSha256=sha(signedIntent) + try{await publishLegacyHandoffIntent({application:app,control,sourcePin,tlsPin:request.tls,archiveDirectory,signedIntent,deadlineAt});recorded=true} + catch(error){const state=error as {outcomeUnknown?:boolean;controlAcknowledged?:boolean};controlUnknown=state?.outcomeUnknown===true||state?.controlAcknowledged===true;throw error} + await sql.query("SET LOCAL search_path=public,pg_catalog,pg_temp; SET LOCAL synchronous_commit=on") + await sql.query(`TRUNCATE TABLE ${LEGACY_PROBE_RELATIONS.map(name=>`ONLY public.${name}`).join(', ')} CONTINUE IDENTITY RESTRICT`) + await sql.query(migration) + await sql.query('SET TRANSACTION READ ONLY') + assert(await checkLegacyHandoffPostState(sql,approval.postSchemaSha256,xid,deadlineAt),'Exact 0037 post-state refused') + await observePublisher(false) + fence();commitSent=true + // Observe the physical acknowledgement before any subsequent check can fail. + assert.equal((await app.query('COMMIT')).command,'COMMIT');committed=true + }catch(error){ + if(!commitSent&&app){try{await app.query('ROLLBACK')}catch{/* Destroy the checked-out application backend. */}} + if(commitSent||controlUnknown||recorded){ + if(commitSent){ + const value=sourceBindingBytes({format:'legacy-probe-0037-application-unknown-v1',operationId:approval.operationId,intentSha256,automaticReplay:false}) + try{await writeFile(path.join(archiveDirectory,'legacy-probe-handoff-application-unknown.json'),value,{flag:'wx',mode:0o600})}catch{/* Existing protected intent remains the recovery reference. */} + } + throw pendingUnknown() + } + throw error + }finally{ + try{await control?.close()}catch{cleanupUnconfirmed=true} + try{app?.release(true)}catch{cleanupUnconfirmed=true} + const closed=await Promise.allSettled([publisher.end(),application.end(),controlPool.end()]);cleanupUnconfirmed ||= closed.some(result=>result.status==='rejected') + } + assert(recorded&&committed) + return {event:'database-legacy-handoff-execution',outcome:'committed',operationId:approval.operationId,intentSha256,quarantineActive:true, + cleanupUnconfirmed:cleanupUnconfirmed||failed,automaticReplay:false,readyAuthorized:false} + }) +} diff --git a/backend/scripts/writer-profile/maintenance-resolution.mts b/backend/scripts/writer-profile/maintenance-resolution.mts new file mode 100644 index 00000000..b08935d6 --- /dev/null +++ b/backend/scripts/writer-profile/maintenance-resolution.mts @@ -0,0 +1,129 @@ +/** Explicit host-operator inspection/acceptance. No restore, service restart, role + * enrollment or source/READY issuer. Populated data is accepted, not certified. */ +import assert from 'node:assert/strict' +import path from 'node:path' +import {fileURLToPath} from 'node:url' +import {mkdir,readdir} from 'node:fs/promises' +import {readProtected,protectedDirectory,writeFile,withJournalLease,sha} from './operator-files.mjs' +import {poolOptions,STARTUP_OPTIONS,assertJsDriverEnvironment} from './operator-contract.mjs' +import {readMaintenanceRecoveryFacts,readMaintenanceAcceptanceRecords} from '../../src/services/database-maintenance-startup.js' +import {parseMaintenanceJSON,runtimeDatabaseTargetSha256,evaluateMaintenanceAcceptances,validateMaintenanceKnownCurrent,validateMaintenanceAcceptance,MAINTENANCE_ACCEPTANCE_FORMAT,type MaintenanceKnownCurrent} from '../../src/services/database-maintenance-resolution.js' +import {captureMigrationSchema} from '../../src/db/migration-schema-inventory.js' +import {MIGRATION_MANIFEST,CURRENT_REQUIRED_TABLES,migrationManifestDigest,inventoryMigrationDirectory} from '../../src/db/migration-manifest.js' +import type {Pool,PoolClient} from 'pg' +const hex=(v:unknown):v is string=>typeof v==='string'&&/^[a-f0-9]{64}$/.test(v) +function object(v:unknown):asserts v is Record{assert(v&&typeof v==='object'&&!Array.isArray(v))} +function exact(v:unknown,keys:string[]):asserts v is Record{object(v);assert.equal(Object.keys(v).sort().join('\0'),keys.sort().join('\0'))} +const stamp=(v:unknown):v is string=>typeof v==='string'&&Number.isFinite(Date.parse(v))&&new Date(v).toISOString()===v +const stable=(v:unknown)=>JSON.stringify(v) +const flags=['state-dir','transport','transport-sha256','runtime-target','runtime-target-sha256'] +const validityMs=900000 +/** Explicit local-container transport never falls back from TLS or environment. + * Caller launch review supplies --network container:; + * this module has no Docker socket/API and claims only loopback + physical SQL proof. */ +export function maintenancePoolConfiguration(value:unknown,ca?:Buffer,caSha256?:string){ + object(value);const mode=value.mode===undefined?'tls':value.mode;assert(mode==='tls'||mode==='local-loopback') + exact(value,mode==='local-loopback'?['format','mode','connection']:value.mode===undefined?['format','connection','tls']:['format','mode','connection','tls']) + assert.equal(value.format,'database-maintenance-transport-v1');exact(value.connection,['host','port','user','password','database']);const connection=value.connection + assert(typeof connection.host==='string'&&typeof connection.user==='string'&&/^[^\x00-\x1f\x7f]{1,63}$/.test(connection.user)&&typeof connection.password==='string'&&connection.password.length>0&&connection.password.length<=4096&&!connection.password.includes('\0')&&typeof connection.database==='string'&&/^[^\x00-\x1f\x7f]{1,63}$/.test(connection.database)&&typeof connection.port==='number'&&Number.isSafeInteger(connection.port)&&Number(connection.port)>0&&Number(connection.port)<=65535) + let options,identity:unknown + if(mode==='tls'){ + exact(value.tls,['serverName','spkiSha256']);assert(typeof value.tls.serverName==='string'&&/^[a-zA-Z0-9][a-zA-Z0-9.-]{0,252}$/.test(value.tls.serverName)&&hex(value.tls.spkiSha256));assert(ca&&hex(caSha256)) + options=poolOptions(connection,{roles:{publisher:{name:connection.user}},source:{databaseName:connection.database},tls:{...value.tls,caSha256}},ca) + identity={mode,host:connection.host,port:connection.port,database:connection.database,tls:{...value.tls,caSha256}} + }else{ + assert(connection.host==='127.0.0.1'||connection.host==='::1','Plaintext maintenance requires exact loopback and reviewed PostgreSQL container namespace');assert(ca===undefined&&caSha256===undefined) + options={host:connection.host,port:connection.port,user:connection.user,password:connection.password,database:connection.database,max:1,connectionTimeoutMillis:5000,idleTimeoutMillis:5000,statement_timeout:10000,lock_timeout:5000,options:STARTUP_OPTIONS,application_name:'mmapp-maintenance-acceptance',client_encoding:'UTF8',ssl:false} + identity={mode,host:connection.host,port:connection.port,database:connection.database} + } + return {options,transportBinding:{mode,endpointSha256:sha(stable(identity))} as MaintenanceKnownCurrent['transport']} +} +export function maintenanceApproval(value:unknown,inspectionSha256:string,inspectionCreatedAt:string){ + exact(value,['format','resolutionId','decision','actorId','inspectionSha256','issuedAt','expiresAt','managedBackendStopped','historicOutcomesRemainUnknown','automaticRestore']) + assert(value.format==='database-maintenance-current-state-approval-v1'&&value.decision==='accept-observed-current-state'&&typeof value.resolutionId==='string'&&/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(value.resolutionId)) + assert(typeof value.actorId==='string'&&/^[^\x00-\x1f\x7f]{1,160}$/.test(value.actorId)&&value.inspectionSha256===inspectionSha256&&stamp(value.issuedAt)&&stamp(value.expiresAt)) + const now=Date.now();assert(Date.parse(value.issuedAt)>=Date.parse(inspectionCreatedAt)&&Date.parse(value.issuedAt)<=now&&now,transport:MaintenanceKnownCurrent['transport']){ + let client:PoolClient|undefined,locked=false,begun=false,bad=false,faulted=false + const fault=()=>{faulted=true},live=()=>{assert(!faulted)} + try{ + client=await pool.connect();client.on('error',fault);client.on('end',fault) + const query=async(text:string,values:unknown[]=[])=>{live();const result=await client!.query(text,values);live();return result} + await query("SET statement_timeout='10s'");await query("SET lock_timeout='5s'") + const lease=await query('SELECT pg_catalog.pg_try_advisory_lock(8723110::bigint) AS locked');assert(lease.rows.length===1&&lease.rows[0].locked===true);locked=true + await query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');begun=true + for(const sql of ["SET LOCAL search_path=pg_catalog","SET LOCAL TimeZone='UTC'","SET LOCAL DateStyle='ISO, YMD'","SET LOCAL standard_conforming_strings=on","SET LOCAL statement_timeout='10s'"])await query(sql) + const identity=await query(`SELECT c.system_identifier::text AS "systemIdentifier",d.oid::text AS "databaseOid",d.datname AS "databaseName",pg_catalog.current_setting('server_version_num')::int AS "serverVersionNum",pg_catalog.pg_is_in_recovery() AS "inRecovery" FROM pg_catalog.pg_database d CROSS JOIN pg_catalog.pg_control_system() c WHERE d.datname=pg_catalog.current_database()`) + assert.equal(identity.rows.length,1) + // The optional writer-profile control schema is not installed by ordinary + // application migrations. Observe its presence in this same transaction; + // detailed capture still refuses every other unexpected namespace. + const control=await query("SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname='mmapp_control'") + assert(control.rows.length<=1&&control.rows.every(row=>row.nspname==='mmapp_control')) + const schema=await captureMigrationSchema({query:async(text,values)=>query(text,values)},{scope:{applicationNamespaces:['public'],excludedControlNamespaces:control.rows.length?['mmapp_control']:[]},deadlineAt:Date.now()+60000}) + const marker=schema.sections.relations.find(row=>row.namespace==='public'&&row.name==='schema_migrations'),column=schema.sections.columns.find(row=>row.namespace==='public'&&row.relation==='schema_migrations'&&row.name==='id'&&row.dropped===false) + assert(marker?.kind==='r'&&marker.persistence==='p'&&marker.row_security===false&&marker.force_row_security===false&&column?.type==='text') + for(const table of CURRENT_REQUIRED_TABLES)assert(schema.sections.relations.some(row=>row.namespace==='public'&&row.name===table&&['r','p'].includes(String(row.kind))&&row.row_security===false&&row.force_row_security===false)) + const markers=(await query('SELECT id FROM public.schema_migrations ORDER BY id LIMIT $1',[MIGRATION_MANIFEST.length+1])).rows + assert.equal(stable(markers),stable(MIGRATION_MANIFEST.map(entry=>({id:entry.file.slice(0,-4)})))) + const knownCurrent={transport,source:identity.rows[0],schemaSha256:schema.sha256,rawSchemaSha256:schema.observedRaw.sha256,physicalSchemaSha256:schema.observedPhysical.sha256,manifestSha256:migrationManifestDigest(),markersSha256:sha(stable(markers))} + validateMaintenanceKnownCurrent(knownCurrent) + const end=await query('ROLLBACK');assert.equal(end.command,'ROLLBACK');begun=false + const unlock=await query('SELECT pg_catalog.pg_advisory_unlock(8723110::bigint) AS unlocked');assert(unlock.rows.length===1&&unlock.rows[0].unlocked===true);locked=false + return {knownCurrent,schema} + }catch(error){bad=true;throw error}finally{ + if(client){let cleanupFailed=false + try{if(begun&&!faulted){const end=await client.query('ROLLBACK');assert.equal(end.command,'ROLLBACK')}if(locked&&!faulted){const end=await client.query('SELECT pg_catalog.pg_advisory_unlock(8723110::bigint) AS unlocked');assert(end.rows[0]?.unlocked===true)}}catch{cleanupFailed=true} + try{client.off('error',fault);client.off('end',fault)}catch{cleanupFailed=true} + try{client.release(bad||faulted||cleanupFailed)}catch{cleanupFailed=true} + assert(!cleanupFailed&&!faulted,'Read-only maintenance owner cleanup unconfirmed') + } + } +} +async function snapshot(stateDirectory:string,target:string,records:readonly Buffer[]){ + const state=await readMaintenanceRecoveryFacts(stateDirectory);assert(state.kind==='journal'&&state.generation) + const accepted=evaluateMaintenanceAcceptances(records,state.journal,state.vault.evidence,target) + const unresolvedOperations=state.operations.filter(op=>op.state!=='applied'),vaultCoverage=state.vault.evidence.filter(marker=>state.vault.unresolvedFiles.includes(marker.file)) + const newUnresolved=unresolvedOperations.some(op=>!accepted.acceptedCoreIds.has(op.operationId))||state.vault.unresolvedFiles.some(file=>!accepted.acceptedVaultFiles.has(file)) + return {newUnresolved,external:{journalCoverage:{prefixBytes:state.journal.length,sha256:sha(state.journal),unresolvedOperations},vaultCoverage,generationObservedSha256:sha(state.generation),previousResolutionSha256:accepted.headSha256}} +} +export async function maintenanceMain(args:string[]){ + assertJsDriverEnvironment(process.env);const resolve=args[0]==='maintenance-resolve';assert(resolve||args[0]==='maintenance-inspect') + const required=[...flags,...(resolve?['inspection','inspection-sha256','approval','approval-sha256']:['output'])],names=[...required,'ca','ca-sha256'],values=new Map() + assert(args.length===1+required.length*2||args.length===1+(required.length+2)*2) + for(let i=1;ivalues.get(name)! + for(const name of [...values.keys()].filter(name=>name.endsWith('sha256')))assert(hex(get(name))) + const stateDirectory=get('state-dir');await protectedDirectory(stateDirectory) + const transport=parseMaintenanceJSON(await readProtected(get('transport'),16384,get('transport-sha256')));object(transport);const mode=transport.mode===undefined?'tls':transport.mode;assert(mode==='tls'||mode==='local-loopback');assert(values.has('ca')===(mode==='tls')) + const ca=mode==='tls'?await readProtected(get('ca'),65536,get('ca-sha256')):undefined + const runtime=parseMaintenanceJSON(await readProtected(get('runtime-target'),16384,get('runtime-target-sha256')));exact(runtime,['format','databaseUrl']);assert(runtime.format==='database-maintenance-runtime-target-v1'&&typeof runtime.databaseUrl==='string');const runtimeUrl=new URL(runtime.databaseUrl);assert(!runtimeUrl.username&&!runtimeUrl.password) + const target=runtimeDatabaseTargetSha256(runtime.databaseUrl),{options,transportBinding}=maintenancePoolConfiguration(transport,ca,mode==='tls'?get('ca-sha256'):undefined) + exact(transport.connection,['host','port','user','password','database']);assert.equal(decodeURIComponent(runtimeUrl.pathname.slice(1)),transport.connection.database) + const records=await readMaintenanceAcceptanceRecords(stateDirectory);if(resolve)assert(records.length<64,'Maintenance acceptance history bound reached');const currentState=await snapshot(stateDirectory,target,records),before=currentState.external;if(resolve)assert(currentState.newUnresolved,'No new unresolved history to accept') + await inventoryMigrationDirectory(path.join(path.dirname(path.dirname(path.dirname(fileURLToPath(import.meta.url)))),'drizzle')) + let inspection:Record|undefined,approved:ReturnType|undefined + if(resolve){inspection=parseMaintenanceJSON(await readProtected(get('inspection'),67108864,get('inspection-sha256'))) as Record;exact(inspection,['format','createdAt','expiresAt','runtimeTargetSha256','acceptanceRequired','external','knownCurrent','schema']);assert(inspection.format==='database-maintenance-current-state-inspection-v1'&&stamp(inspection.createdAt)&&stamp(inspection.expiresAt)&&Date.parse(inspection.createdAt)<=Date.now()&&Date.now(){const pool=new pg.Pool(options);let failed=false;pool.on('error',()=>{failed=true});try{const result=await observeMaintenanceCurrent(pool,transportBinding);await pool.end();assert(!failed);return result}catch(error){try{await pool.end()}catch{/* preserve refusal */}throw error}} + if(!resolve){const result=await runProof();assert.equal(stable((await snapshot(stateDirectory,target,await readMaintenanceAcceptanceRecords(stateDirectory))).external),stable(before));const nowMs=Date.now(),now=new Date(nowMs).toISOString(),out={format:'database-maintenance-current-state-inspection-v1',createdAt:now,expiresAt:new Date(nowMs+validityMs).toISOString(),runtimeTargetSha256:target,acceptanceRequired:currentState.newUnresolved,external:before,...result};const raw=Buffer.from(stable(out));assert(raw.length<=67108864);await writeFile(get('output'),raw,{flag:'wx',mode:0o600});return {event:'database-maintenance-inspection',inspectionSha256:sha(raw),unresolvedCoreOperations:before.journalCoverage.unresolvedOperations.length,unresolvedVaultFiles:before.vaultCoverage.length,state:currentState.newUnresolved?'inspection-required':before.previousResolutionSha256?'historically-accepted':'no-unresolved-history',historicalAcceptanceHeadSha256:before.previousResolutionSha256,humanApprovalRequired:currentState.newUnresolved,knownCurrentObserved:true,archiveDataEquivalenceProved:false,historicOutcomesChanged:false,automaticRestore:false}} + const resolutionDirectory=path.join(stateDirectory,'maintenance-acceptances');await mkdir(resolutionDirectory,{recursive:true,mode:0o700});await protectedDirectory(resolutionDirectory) + return withJournalLease(resolutionDirectory,async()=>{ + // Existing immutable records are reread while our proven O_EXCL lease is held. + // Runtime reader refuses this lease; never infer stale ownership from PID/time. + const expected=records.map(raw=>{const v=parseMaintenanceJSON(raw) as {resolutionId:string};return v.resolutionId+'.json'}).sort() + assert.equal((await readdir(resolutionDirectory)).sort().join('\0'),[...expected,'publisher-lease.lock'].sort().join('\0')) + for(let i=0;iJSON.parse(new TextDecoder('utf8',{fatal:true}).decode(bytes)) +const receiptEvent='application-writer-marker-fence-enrollment' + +// The reviewed fixed57 migration-marker cohort, qualified independently of search_path. +const installSql=`CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.schema_migrations + FOR EACH STATEMENT EXECUTE FUNCTION public.server_partition_writer_guard(); +CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON public.schema_migrations + FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted(); +CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.schema_migrations + FOR EACH ROW EXECUTE FUNCTION public.server_partition_writer_guard(); +ALTER TABLE public.schema_migrations ENABLE ALWAYS TRIGGER candidate_application_statement_guard; +ALTER TABLE public.schema_migrations ENABLE ALWAYS TRIGGER candidate_truncate_preadmitted; +ALTER TABLE public.schema_migrations ENABLE ALWAYS TRIGGER server_partition_writer_guard;` +export const reviewedMarkerFenceSql=installSql + +const targetSql=`SELECT d.oid::text AS "databaseOid", d.datname::text AS "databaseName", + control.system_identifier::text AS "systemIdentifier", pg_catalog.current_setting('server_version_num')::int AS "serverVersionNum", + pg_catalog.pg_is_in_recovery() AS "inRecovery", r.oid::text AS "roleOid" + FROM pg_catalog.pg_database d CROSS JOIN pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_control_system() control + WHERE d.datname=pg_catalog.current_database() AND r.rolname=SESSION_USER AND SESSION_USER=CURRENT_USER AND r.rolname=$1 + AND pg_catalog.current_setting('transaction_read_only')='off' AND pg_catalog.current_setting('session_replication_role')='origin' + AND pg_catalog.current_setting('event_triggers')='on'` + +const eventSql=`SELECT e.evtname::text, e.evtevent::text, e.evtenabled::text, e.evttags, + pg_catalog.pg_get_userbyid(e.evtowner) AS owner, n.nspname AS function_namespace,p.proname AS function_name, + p.proowner::text AS function_owner, p.proacl::text[] AS acl, pg_catalog.pg_get_functiondef(p.oid) AS definition + FROM pg_catalog.pg_event_trigger e JOIN pg_catalog.pg_proc p ON p.oid=e.evtfoid + JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace LIMIT 2` + +type EnrollmentQuery=>(text:string,parameters?:unknown[])=>Promise<{rows:R[]}> +type Source={databaseOid:string;databaseName:string;systemIdentifier:string;serverVersionNum:number;inRecovery:false} + +/** SQL component for the retained operator transaction. The command owns TLS, approval and terminal acknowledgement. */ +export async function enrollMarkerFenceInTransaction(query:EnrollmentQuery,bindings:RegistryRoleBindingsData,source:Source,expectedCatalogue:string){ + await query('SET LOCAL search_path = pg_catalog') + await query('SELECT pg_catalog.pg_advisory_xact_lock($1::bigint)',[KEY]) + const {operator,control}=bindings + assert.deepEqual((await query(targetSql,[operator.name])).rows,[{...source,roleOid:operator.signedOid}]) + const security=await readProtectedRegistryStructureData({query},bindings) + await query('LOCK TABLE ONLY public.schema_migrations IN ACCESS EXCLUSIVE MODE') + const markers=MIGRATION_MANIFEST.map(pin=>({id:pin.file.slice(0,-4)})) + const actualMarkers=await query('SELECT id FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1',[markers.length+1]) + assert.deepEqual(actualMarkers.rows,markers,'Marker fence requires exact current migration markers') + await assertReviewedMigrationMarkerGuard({query},'unenrolled') + const [{function:_function,owner:_owner,...event}]=FIXED57_ROLE_TEMPLATE.events + const allowedFunction=FIXED57_ROLE_TEMPLATE.functions.find(fn=>fn.proname==='candidate_operator_ddl')! + assert.deepEqual((await query(eventSql)).rows,[{...event,owner:control.name,function_owner:control.signedOid, + acl:[`${control.name}=X/${control.name}`],definition:allowedFunction.definition.replace('@mm-role:operator@',operator.name)}], + 'Marker fence requires the exact reviewed operator DDL gate before writing') + await query(installSql) + await assertReviewedMigrationMarkerGuard({query}) + const catalogue=await collectSource57Catalogue({query},{roleBindings:bindings}) + assert.equal(sha(canonical({base:catalogue.catalogSha256,security:security.sha256})),expectedCatalogue,'Enrolled catalogue differs from approval') +} + +export async function markerFenceEnrollmentMain(args:string[]){ + assert(args[0]==='marker-fence-enroll'&&args.length===1+2*flags.length) + assertJsDriverEnvironment(process.env) + const flagsByName=new Map() + for(let i=1;ireadProtected(flagsByName.get(name)!,maximum,pin) + const approval=json(await read('approval',16384,flagsByName.get('approval-sha256')!)) + const manifestSha256=migrationManifestDigest() + assert.deepEqual(Object.keys(approval).sort(),['catalogSha256','definitionSha256','deploymentRequest','format','manifestSha256']) + assert(approval.format==='application-writer-marker-fence-enrollment-v1'&&approval.manifestSha256===manifestSha256&& + manifestSha256===BUILD57_PROVENANCE.manifestSha256&&approval.definitionSha256===BUILD57_PROVENANCE.templateSha256&& + typeof approval.catalogSha256==='string'&&digest.test(approval.catalogSha256)) + const request=deploymentRequest(approval.deploymentRequest),bindings:RegistryRoleBindingsData=request.roles + const transport=json(await read('transport',16384,flagsByName.get('transport-sha256')!)) + const ca=await read('ca',65536,request.tls.caSha256) + const options=poolOptions(transport,request,ca,'operator') + const {default:pg}=await import('pg') + const pool=new pg.Pool({...options,query_timeout:12_000}) + const deadline=Date.now()+120_000 + let client:PoolClient|undefined,commitSent=false,committed=false,failure:unknown + const onError=(error:Error)=>{failure??=error} + pool.on('error',onError) + try{ + const connected=await pool.connect() + client=connected + connected.on('error',onError) + const assertPeer=retainPublisherTlsPeer(connected,request.tls) + const fence=()=>{if(failure)throw failure;assert(Date.now()>(text:string,parameters?:unknown[])=>{ + fence() + const result=await connected.query(text,parameters) + fence() + return result + } + await query('BEGIN') + await enrollMarkerFenceInTransaction(query,bindings,request.source,approval.catalogSha256) + fence() + commitSent=true + const ack=await connected.query('COMMIT') + assert.equal(ack.command,'COMMIT') + committed=true + }catch(error){failure??=error} + finally{ + try{client?.release(true)}catch(error){failure??=error} + try{await pool.end()}catch(error){failure??=error} + } + if(committed)return {event:receiptEvent,outcome:'committed',installationId:request.installationId,manifestSha256, + catalogSha256:approval.catalogSha256,cleanupUnconfirmed:!!failure,automaticReplay:false,readyAuthorized:false} + if(commitSent)throw Object.assign(Error('Marker fence enrollment requires inspection',{cause:failure}),{code:'MARKER_FENCE_TERMINAL_UNKNOWN'}) + throw failure??Error('Marker fence enrollment refused') +} diff --git a/backend/scripts/writer-profile/marker-fence-enrollment.test.mts b/backend/scripts/writer-profile/marker-fence-enrollment.test.mts new file mode 100644 index 00000000..864dafa5 --- /dev/null +++ b/backend/scripts/writer-profile/marker-fence-enrollment.test.mts @@ -0,0 +1,67 @@ +import assert from 'node:assert/strict' +import {EventEmitter} from 'node:events' +import {mock,test} from 'node:test' +import {MIGRATION_MANIFEST,migrationManifestDigest} from '../../src/db/migration-manifest.js' +import {canonical,sha} from '../../src/db/application-writer-profile-format.js' +import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js' + +test('marker enrollment gates exact DDL on the retained operator and checks the approved result before COMMIT',async()=>{ + const roles=Object.fromEntries(['runtime','operator','capture','publisher','control'].map((role,index)=>[role,{name:`app_${role}`,signedOid:String(20000+index)}])) + const source={databaseOid:'30000',databaseName:'fixture',systemIdentifier:'121212',serverVersionNum:180004,inRecovery:false} + const pin='a'.repeat(64) + const deploymentRequest={format:'application-writer-bootstrap-request-v1',installationId:'7435bc0c-b7c8-40d8-be88-5f6ae478a221',sourceHeadSha256:pin, + source,roles,tls:{serverName:'localhost',caSha256:pin,spkiSha256:pin}} + const approval={format:'application-writer-marker-fence-enrollment-v1',manifestSha256:migrationManifestDigest(),definitionSha256:BUILD57_PROVENANCE.templateSha256, + catalogSha256:sha(canonical({base:pin,security:pin})),deploymentRequest} + const args=['marker-fence-enroll','--approval','approval','--approval-sha256',pin,'--transport','transport','--transport-sha256',pin,'--ca','ca'] + const [{function:_function,owner:_owner,...event}]=FIXED57_ROLE_TEMPLATE.events + const expectedFunction=FIXED57_ROLE_TEMPLATE.functions.find(fn=>fn.proname==='candidate_operator_ddl')! + const eventRow={...event,owner:roles.control.name,function_owner:roles.control.signedOid,acl:[`${roles.control.name}=X/${roles.control.name}`], + definition:expectedFunction.definition.replace('@mm-role:operator@',roles.operator.name)} + let scenario='success' + const calls:string[]=[] + class Client extends EventEmitter{ + async query(sql:string){ + calls.push(sql) + if(sql.includes('pg_catalog.pg_control_system()'))return {rows:[{...source,roleOid:roles.operator.signedOid}]} + if(sql.includes('FROM public.schema_migrations'))return {rows:MIGRATION_MANIFEST.map(pin=>({id:pin.file.slice(0,-4)}))} + if(sql.includes('pg_catalog.pg_event_trigger'))return {rows:[scenario==='event-drift'?{...eventRow,evtenabled:'D'}:eventRow]} + if(sql==='COMMIT'&&scenario==='lost-ack')throw Error('Synthetic ACK loss') + return {command:sql,rows:[]} + } + release(destroy:boolean){assert(destroy);calls.push('release')} + } + class Pool extends EventEmitter{ + async connect(){return new Client()} + async end(){calls.push('end')} + } + mock.module('pg',{defaultExport:{Pool}}) + mock.module('./operator-files.mjs',{namedExports:{readProtected:async(path:string,_limit:number,expected:string)=>{ + assert.equal(expected,pin);return Buffer.from(JSON.stringify(path==='approval'?approval:{})) + }}}) + mock.module('./operator-contract.mjs',{namedExports:{assertJsDriverEnvironment:()=>{},poolOptions:(_transport:unknown,request:typeof deploymentRequest,_ca:Buffer,role:string)=>{ + assert.deepEqual(request,deploymentRequest);assert.equal(role,'operator');return {} + },retainPublisherTlsPeer:()=>()=>{}}}) + mock.module('../../src/db/application-writer-profile-registry-structure.js',{namedExports:{readProtectedRegistryStructureData:async()=>({sha256:pin})}}) + mock.module('../../src/db/partition-guard-inheritance.js',{namedExports:{assertReviewedMigrationMarkerGuard:async(_sql:unknown,state='installed')=>{ + calls.push(state);if(scenario==='partial'&&state==='unenrolled')throw Error('Synthetic preexisting guard') + }}}) + mock.module('./catalogue.mjs',{namedExports:{collectSource57Catalogue:async()=>{ + calls.push('collect');return {catalogSha256:scenario==='catalogue-drift'?'b'.repeat(64):pin} + }}}) + const {markerFenceEnrollmentMain}=await import('./marker-fence-enrollment.mjs') + const result=await markerFenceEnrollmentMain(args) + assert.equal(result.outcome,'committed');assert.equal(result.readyAuthorized,false) + const exclusive=calls.findIndex(sql=>sql.includes('pg_advisory_xact_lock')),table=calls.findIndex(sql=>sql.startsWith('LOCK TABLE')) + const ddl=calls.findIndex(sql=>sql.startsWith('CREATE TRIGGER')) + assert(exclusive>=0&&exclusivesql.startsWith('CREATE TRIGGER')).length,['catalogue-drift','lost-ack'].includes(variant)?1:0) + assert.equal(calls.filter(sql=>sql==='COMMIT').length,variant==='lost-ack'?1:0) + assert.deepEqual(calls.slice(-2),['release','end']) + } +}) diff --git a/backend/scripts/writer-profile/operator-contract.mjs b/backend/scripts/writer-profile/operator-contract.mjs new file mode 100644 index 00000000..4f0fabc1 --- /dev/null +++ b/backend/scripts/writer-profile/operator-contract.mjs @@ -0,0 +1,65 @@ +import assert from 'node:assert/strict' +import {X509Certificate} from 'node:crypto' +import {checkServerIdentity,TLSSocket} from 'node:tls' +import {sha} from './operator-files.mjs' +export const roleKeys=Object.freeze(['runtime','operator','capture','publisher','control']) +export const STARTUP_OPTIONS='-c statement_timeout=10000 -c lock_timeout=5000 -c client_encoding=UTF8' +export function assertJsDriverEnvironment(environment){assert(!Object.hasOwn(environment,'NODE_PG_FORCE_NATIVE'),'Native pg driver selector refused')} +const hex=v=>typeof v==='string'&&/^[a-f0-9]{64}$/.test(v) +const exact=(v,keys)=>v&&typeof v==='object'&&!Array.isArray(v)&&Object.keys(v).length===keys.length&&keys.every(k=>Object.hasOwn(v,k)) +const publicationFormat='application-writer-publication-approval-v1' +export const UPGRADE_APPROVAL_FORMAT='application-writer-upgrade44-to56-approval-v1' +export const UPGRADE57_APPROVAL_FORMAT='application-writer-upgrade56-to57-approval-v1' +export const UPGRADE58_APPROVAL_FORMAT='application-writer-upgrade57-to58-approval-v1' +export const REBIND58_APPROVAL_FORMAT='application-writer-rebind58-after-restore-approval-v1' +export const isUpgradeApprovalFormat=format=>[UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,UPGRADE58_APPROVAL_FORMAT,REBIND58_APPROVAL_FORMAT].includes(format) +function parsedApproval(value,format=publicationFormat){ + const upgrade=isUpgradeApprovalFormat(format) + const keys=['format','manifestSha256','definitionSha256','catalogSha256','expectedHeadSha256','validityMs','expiresAt'] + if(upgrade)keys.push('previousCatalogSha256') + assert(exact(value,keys)&&value.format===format) + assert(['manifestSha256','definitionSha256','catalogSha256'].every(k=>hex(value[k]))) + assert(upgrade?hex(value.expectedHeadSha256)&&hex(value.previousCatalogSha256):value.expectedHeadSha256===null||hex(value.expectedHeadSha256)) + if(format===REBIND58_APPROVAL_FORMAT)assert(value.catalogSha256!==value.previousCatalogSha256,'Restored catalogue must have a distinct independently approved digest') + assert(Number.isSafeInteger(value.validityMs)&&value.validityMs>0&&value.validityMs<=86400000&&Number.isSafeInteger(value.expiresAt)&&value.expiresAt>0) + return structuredClone(value) +} +export function approval(value){ + const pinned=parsedApproval(value);assert(pinned.expiresAt>Date.now());return pinned +} +export function upgradeApproval(value){ + assert(isUpgradeApprovalFormat(value?.format)) + const pinned=parsedApproval(value,value.format);assert(pinned.expiresAt>Date.now());return pinned +} +// Pinned historical inspection data only; never publication authorization. +export function reconciliationApproval(value){return parsedApproval(value,isUpgradeApprovalFormat(value?.format)?value.format:publicationFormat)} +function peerError(pin,host,cert){ + const failed=checkServerIdentity(pin.serverName,cert);if(failed)return failed + try{if(host!==pin.serverName||sha(new X509Certificate(cert.raw).publicKey.export({format:'der',type:'spki'}))!==pin.spkiSha256)return Error('Publisher TLS SPKI refused')}catch{return Error('Publisher TLS certificate refused')} +} + +// pg replaces connection.stream with the negotiated TLS socket. Hold its +// identity on the retained client; handshake callbacks are not session evidence. +export function retainPublisherTlsPeer(client,pin){ + const socket=client.connection?.stream + const assertCurrent=()=>{ + assert(socket instanceof TLSSocket&&client.connection?.stream===socket&&socket.encrypted&&socket.authorized&&!socket.destroyed&&socket.writable,'Publisher retained TLS peer unavailable') + } + assertCurrent() + const failed=peerError(pin,socket.servername,socket.getPeerCertificate());if(failed)throw failed + socket.disableRenegotiation() + return assertCurrent +} +export function poolOptions(transport,request,ca,role='publisher'){ + assert(role==='publisher'||role==='control'||role==='operator') + assert(exact(transport,['host','port','user','password','database'])) + assert(typeof transport.host==='string'&&transport.host===request.tls.serverName&&Number.isSafeInteger(transport.port)&&transport.port>0&&transport.port<=65535&&transport.user===request.roles[role].name&&typeof transport.password==='string'&&transport.password.length>0&&transport.password.length<=4096&&transport.database===request.source.databaseName) + assert(sha(ca)===request.tls.caSha256);new X509Certificate(ca) + return {host:transport.host,port:transport.port,user:transport.user,password:transport.password,database:transport.database,max:1,connectionTimeoutMillis:5000,idleTimeoutMillis:5000,statement_timeout:10000,lock_timeout:5000,options:STARTUP_OPTIONS,application_name:role==='control'?'mmapp-writer-registry-enroll':role==='operator'?'mmapp-writer-marker-fence-enroll':'mmapp-writer-publisher',client_encoding:'UTF8', + ssl:{ca,servername:request.tls.serverName,rejectUnauthorized:true,checkServerIdentity:(host,cert)=>peerError(request.tls,host,cert)}} +} +export function assertPublicationApproval(current,catalogue,structure,pinned,manifestSha256){ + assert(pinned.expiresAt>Date.now()&&pinned.manifestSha256===manifestSha256&&pinned.definitionSha256===catalogue.definitionSha256) + assert(sha(JSON.stringify({base:catalogue.catalogSha256,security:structure.sha256}))===pinned.catalogSha256) + assert((current?.headSha256??null)===pinned.expectedHeadSha256) +} diff --git a/backend/scripts/writer-profile/operator-files.mjs b/backend/scripts/writer-profile/operator-files.mjs new file mode 100644 index 00000000..1b40ada9 --- /dev/null +++ b/backend/scripts/writer-profile/operator-files.mjs @@ -0,0 +1,58 @@ +import assert from 'node:assert/strict' +import {constants} from 'node:fs' +import {open,lstat,realpath,link,unlink} from 'node:fs/promises' +import path from 'node:path' +import {createHash} from 'node:crypto' +export const sha=bytes=>createHash('sha256').update(bytes).digest('hex') +export async function protectedDirectory(directory){ + assert(process.platform!=='win32'&&typeof process.getuid==='function','Native Windows ACL backend unavailable') + assert(path.isAbsolute(directory)&&await realpath(directory)===directory) + const uid=process.getuid(),s=await lstat(directory) + assert(s.isDirectory()&&!s.isSymbolicLink()&&s.uid===uid&&(s.mode&0o077)===0) + for(let p=path.dirname(directory);;p=path.dirname(p)){const s=await lstat(p);assert(s.isDirectory()&&!s.isSymbolicLink()&&(s.uid===uid||s.uid===0));assert((s.mode&0o022)===0||s.uid===0&&(s.mode&0o1000)!==0);if(p===path.dirname(p))break} +} +export async function readProtected(file,maximum,expectedSha){ + await protectedDirectory(path.dirname(file));assert(await realpath(file)===file) + const f=await open(file,constants.O_RDONLY|constants.O_NOFOLLOW) + try{ + const a=await f.stat();assert(a.isFile()&&a.uid===process.getuid()&&(a.mode&0o077)===0&&a.size>0&&a.size<=maximum) + const out=Buffer.alloc(a.size+1);let size=0 + while(size{const d=await open(directory,constants.O_RDONLY|constants.O_DIRECTORY|constants.O_NOFOLLOW);try{await d.sync()}finally{await d.close()}} + try{ + identity=await handle.stat({bigint:true});await handle.writeFile('mmapp-writer-journal-lease-v1\n');await handle.sync();await syncDirectory() + result=await work() + }catch(error){primary=error;failed=true} + finally{ + try{ + await protectedDirectory(directory) + const current=await lstat(filename,{bigint:true}) + if(!identity||!current.isFile()||current.isSymbolicLink()||current.dev!==identity.dev||current.ino!==identity.ino)throw Error('Journal lease identity changed') + await unlink(filename);await syncDirectory() + }catch(error){cleanupFailed=true;primary??=error} + try{await handle.close()}catch(error){cleanupFailed=true;primary??=error} + } + // Known COMMIT remains known if the filesystem cleanup fails later. + if(!failed&&result?.outcome==='committed'&&cleanupFailed)return Object.freeze({...result,journalUnconfirmed:true}) + if(failed||cleanupFailed)throw primary + return result +} diff --git a/backend/scripts/writer-profile/operator-journal-lease.test.mjs b/backend/scripts/writer-profile/operator-journal-lease.test.mjs new file mode 100644 index 00000000..5c98577c --- /dev/null +++ b/backend/scripts/writer-profile/operator-journal-lease.test.mjs @@ -0,0 +1,68 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import {mkdtemp, chmod, readdir, writeFile, readFile, unlink, rm} from 'node:fs/promises' +import {tmpdir} from 'node:os' +import path from 'node:path' +import {withJournalLease} from './operator-files.mjs' + +const posix = process.platform !== 'win32' +async function owned(work) { + const directory = await mkdtemp(path.join(tmpdir(), 'mm-operator-lease-')) + try { await chmod(directory, 0o700); await work(directory) } + finally { await rm(directory, {recursive: true, force: true}) } +} + +test('Windows refuses before callback or lease creation', {skip: posix}, async () => { + await owned(async directory => { + let called = false + await assert.rejects(withJournalLease(directory, async () => { called = true }), /Windows ACL/) + assert.equal(called, false) + assert.deepEqual(await readdir(directory), []) + }) +}) + +test('POSIX cooperative exclusion spans the entire callback and cleanup', {skip: !posix}, async () => { + await owned(async directory => { + let entered, release + const ready = new Promise(resolve => { entered = resolve }) + const held = new Promise(resolve => { release = resolve }) + const first = withJournalLease(directory, async () => { entered(); await held; return 'first' }) + await ready + try { + let secondCalled = false + await assert.rejects(withJournalLease(directory, async () => { secondCalled = true }), {code: 'EEXIST'}) + assert.equal(secondCalled, false) + assert.deepEqual(await readdir(directory), ['publisher-lease.lock']) + } finally { release(); await first } + assert.deepEqual(await readdir(directory), []) + assert.equal(await withJournalLease(directory, async () => 'next'), 'next') + }) +}) + +test('POSIX unknown work retains its pending cursor and original error', {skip: !posix}, async () => { + await owned(async directory => { + const pending = path.join(directory, 'pending-publication.json'), failure = new Error('unknown publication') + await assert.rejects(withJournalLease(directory, async () => { + await writeFile(pending, 'pending', {flag: 'wx', mode: 0o600}) + throw failure + }), error => error === failure) + assert.equal(await readFile(pending, 'utf8'), 'pending') + assert.deepEqual(await readdir(directory), ['pending-publication.json']) + }) +}) + +test('POSIX foreign replacement remains and known COMMIT survives cleanup refusal', {skip: !posix}, async () => { + await owned(async directory => { + const lease = path.join(directory, 'publisher-lease.lock') + const result = await withJournalLease(directory, async () => { + await unlink(lease) + await writeFile(lease, 'foreign lease', {flag: 'wx', mode: 0o600}) + return {outcome: 'committed', epoch: '9', headSha256: 'a'.repeat(64)} + }) + assert.equal(result.outcome, 'committed') + assert.equal(result.epoch, '9') + assert.equal(result.journalUnconfirmed, true) + assert.equal(await readFile(lease, 'utf8'), 'foreign lease') + await assert.rejects(withJournalLease(directory, async () => assert.fail('foreign lease was adopted')), {code: 'EEXIST'}) + }) +}) diff --git a/backend/scripts/writer-profile/protected-source-registry.mts b/backend/scripts/writer-profile/protected-source-registry.mts new file mode 100644 index 00000000..4b2db065 --- /dev/null +++ b/backend/scripts/writer-profile/protected-source-registry.mts @@ -0,0 +1,84 @@ +/** Explicit operator/lifecycle read component. No startup, grants or READY. */ +import assert from 'node:assert/strict' +import {lstat} from 'node:fs/promises' +import path from 'node:path' +import {protectedDirectory,readProtected} from './operator-files.mjs' +import {createDatabaseSourceBindingVerifier,readDatabaseSourceBindingRegistry, + type DatabaseSourceDeploymentPin} from '../../src/db/database-source-binding.js' +import type {DatabaseSourceObservation,DatabaseSourceObservationOptions} from '../../src/db/database-source-observation.js' +import type {ProbeStorageSql} from '../../src/services/server-probe-journal-maintenance.js' + +const hex=(value:unknown):value is string=>typeof value==='string'&&/^[a-f0-9]{64}$/.test(value) +function fence(options:DatabaseSourceObservationOptions){ + assert(Number.isSafeInteger(options.deadlineAt)&&Date.now()={} + for(let p=pin.registryDirectory;;p=path.dirname(p)){ + directories[p]=await identity(p,p===pin.registryDirectory) + if(p===path.dirname(p))break + } + const names=['operator-public.pem','head-'+pin.currentHeadSha256+'.json'] + const content:Record={},identities:Record={} + const collect=async(name:string,maximum:number,expected?:string)=>{ + const filename=path.join(pin.registryDirectory,name) + const before=await identity(filename,true) + content[name]=await readProtected(filename,maximum,expected) + assert.equal(await identity(filename,true),before,'Protected source file changed during read') + identities[name]=before + } + await collect(names[0],4096) + await collect(names[1],8192,pin.currentHeadSha256) + const head=JSON.parse(new TextDecoder('utf8',{fatal:true}).decode(content[names[1]])) + assert(hex(head?.body?.bindingSha256),'Pinned source binding digest required') + const bindingName='binding-'+head.body.bindingSha256+'.json' + await collect(bindingName,8192,head.body.bindingSha256) + await protectedDirectory(pin.registryDirectory) + for(const [directory,wanted]of Object.entries(directories))assert.equal(await identity(directory,directory===pin.registryDirectory),wanted,'Protected source directory changed') + return {directories,identities,content} +} +function same(before:Awaited>,after:Awaited>){ + assert.deepEqual(after.directories,before.directories,'Protected source directory drift') + assert.deepEqual(after.identities,before.identities,'Protected source identity/mode drift') + assert.deepEqual(after.content,before.content,'Protected source content drift') +} +function captured(raw:DatabaseSourceDeploymentPin){ + const pin=Object.freeze({...raw}) + // Existing strict deployment schema rejects unknown/malformed pin fields. + const verifier=createDatabaseSourceBindingVerifier(pin) + return {pin,verifier} +} + +/** Signature/head/source DATA plus actual POSIX protection of the pinned files. + * The supplied pin still needs independent deployment approval; protection + * cannot distinguish a restored clone/generation or authenticate all writers. */ +export async function readProtectedDatabaseSourceBindingRegistry(raw:DatabaseSourceDeploymentPin,options:DatabaseSourceObservationOptions){ + const {pin}=captured(raw);const lifetime={...options};fence(lifetime) + const before=await protectedState(pin);fence(lifetime) + const result=await readDatabaseSourceBindingRegistry(pin);fence(lifetime) + const after=await protectedState(pin);fence(lifetime);same(before,after) + return Object.freeze({...result,platformProtectionVerified:true as const, + generationAuthenticated:false as const,allWriterDrainAuthenticated:false as const,readyAuthorized:false as const}) +} + +/** Same-facade source verification is retained; filesystem JSON is never a + * source observation, writer capability or independent generation authority. */ +export function createProtectedDatabaseSourceBindingVerifier(raw:DatabaseSourceDeploymentPin){ + const {pin,verifier}=captured(raw) + return Object.freeze({async verify(sql:ProbeStorageSql,observation:DatabaseSourceObservation,options:DatabaseSourceObservationOptions){ + const lifetime={...options};fence(lifetime) + const before=await protectedState(pin);fence(lifetime) + const result=await verifier.verify(sql,observation,lifetime);fence(lifetime) + const after=await protectedState(pin);fence(lifetime);same(before,after) + return Object.freeze({...result,platformProtectionVerified:true as const, + generationAuthenticated:false as const,allWriterDrainAuthenticated:false as const,readyAuthorized:false as const}) + }}) +} diff --git a/backend/scripts/writer-profile/protected-source-registry.test.mts b/backend/scripts/writer-profile/protected-source-registry.test.mts new file mode 100644 index 00000000..07436e8c --- /dev/null +++ b/backend/scripts/writer-profile/protected-source-registry.test.mts @@ -0,0 +1,85 @@ +/** Genuine Ed25519/files; POSIX permission metadata is modeled on Windows. + * No SQL, service, deployment or real POSIX acceptance is claimed. */ +import test from 'node:test' +import assert from 'node:assert/strict' +import fs from 'node:fs/promises' +import path from 'node:path' +import os from 'node:os' +import crypto from 'node:crypto' +import {createRequire} from 'node:module' +import type {Stats,BigIntStats} from 'node:fs' +import type {DatabaseSourceDeploymentPin} from '../../src/db/database-source-binding.ts' +import type * as ProtectedApi from './protected-source-registry.mts' +import {runInNewContext} from 'node:vm' +import {readProtectedDatabaseSourceBindingRegistry} from './protected-source-registry.mts' +import {readDatabaseSourceBindingRegistry,sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,DATABASE_SOURCE_SIGNATURE_DOMAINS} from '../../src/db/database-source-binding.ts' +const require=createRequire(import.meta.url),ts=require('typescript') +const helperSource=await fs.readFile(new URL('./operator-files.mjs',import.meta.url),'utf8') +const source=await fs.readFile(new URL('./protected-source-registry.mts',import.meta.url),'utf8') +const deadline=()=>({deadlineAt:Date.now()+10000}) +function compile(text:string){return ts.transpileModule(text,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText} +function evaluate>(text:string,imports:Record,processModel:unknown=process):T{ + const exports:Record={} + runInNewContext(compile(text),{exports,require:(name:string)=>imports[name]??require(name),process:processModel,Buffer,TextDecoder,console}) + return exports as T +} +interface Fixture {pin:DatabaseSourceDeploymentPin;faults:Record;api:typeof ProtectedApi;verificationReads:()=>number;root:string} +async function own(work:(f:Fixture)=>Promise){ + const root=await fs.mkdtemp(path.join(os.tmpdir(),'mm-protected-source-')) + try{ + const key=crypto.generateKeyPairSync('ed25519'),installationId=crypto.randomUUID() + const signed=(body:unknown,domain:string)=>sourceBindingBytes({body,signature:crypto.sign(null,Buffer.from(domain+sourceBindingCanonical(body)),key.privateKey).toString('base64url')}) + const body={format:'database-source-binding-v1',installationId,sourceIdentityId:crypto.randomUUID(),generation:crypto.randomUUID(),ordinal:1, + source:{systemIdentifier:'123',databaseOid:16384,databaseName:'synthetic',sessionUser:'capture',currentUser:'capture',inRecovery:false,serverVersionNum:180004}} + const binding=signed(body,DATABASE_SOURCE_SIGNATURE_DOMAINS.binding),bindingSha256=sourceBindingSha256(binding) + const head=signed({format:'database-source-registry-head-v1',installationId,sourceIdentityId:body.sourceIdentityId,generation:body.generation,ordinal:1,bindingSha256,previousHeadSha256:null},DATABASE_SOURCE_SIGNATURE_DOMAINS.head) + const pin={registryDirectory:root,installationId,publicKeySpkiSha256:sourceBindingSha256(key.publicKey.export({format:'der',type:'spki'})),currentHeadSha256:sourceBindingSha256(head)} + await fs.writeFile(path.join(root,'operator-public.pem'),key.publicKey.export({format:'pem',type:'spki'}),{mode:0o600}) + await fs.writeFile(path.join(root,'head-'+pin.currentHeadSha256+'.json'),head,{mode:0o600}) + await fs.writeFile(path.join(root,'binding-'+bindingSha256+'.json'),binding,{mode:0o600}) + const faults={modeFile:false,modeDirectory:false,symlink:false,replacement:false,lateMode:false,changed:false};let verificationReads=0 + const metadata=(s:Stats|BigIntStats,filename:string,big:boolean)=>{ + const n=(value:number)=>big?BigInt(value):value,isRoot=filename===root,isFile=s.isFile() + const mode=(isFile?0o100600:0o40700)|((isFile&&faults.modeFile||isRoot&&faults.modeDirectory||isFile&&faults.lateMode&&faults.changed)?0o022:0) + return {...s,mode:n(mode),uid:n(1000),gid:n(1000),ino:faults.replacement&&faults.changed&&isFile?(typeof s.ino==='bigint'?s.ino+1n:s.ino+1):s.ino, + isFile:()=>isFile,isDirectory:()=>s.isDirectory(),isSymbolicLink:()=>faults.symlink&&isRoot} + } + const modelFs={...fs,lstat:async(filename:string,options?:{bigint?:boolean})=>metadata(await fs.lstat(filename,{bigint:options?.bigint===true}),filename,options?.bigint===true),open:async(filename:string,flags:number)=>{ + const file=await fs.open(filename,flags) + return {stat:async()=>metadata(await file.stat(),filename,false),read:file.read.bind(file),close:file.close.bind(file)} + }} + const processModel={platform:'linux',getuid:()=>1000} + const proven=evaluate(helperSource,{'node:fs/promises':modelFs},processModel) + const bindingModule={readDatabaseSourceBindingRegistry:async(p:DatabaseSourceDeploymentPin)=>{ + verificationReads++;const result=await readDatabaseSourceBindingRegistry(p);faults.changed=true;return result + },createDatabaseSourceBindingVerifier:()=>({verify:async()=>{verificationReads++;faults.changed=true;return {genuineSourceFactsMatched:true,generationAuthenticated:false,readyAuthorized:false}}})} + const api=evaluate(source,{'node:fs/promises':modelFs,'./operator-files.mjs':proven,'../../src/db/database-source-binding.js':bindingModule},processModel) + await work({pin,faults,api,verificationReads:()=>verificationReads,root}) + }finally{const rel=path.relative(os.tmpdir(),root);assert(rel&&!rel.startsWith('..')&&!path.isAbsolute(rel));await fs.rm(root,{recursive:true,force:true})} +} + +test('unsupported Windows refuses before registry signature reads',{skip:process.platform!=='win32'},async()=>{ + await assert.rejects(readProtectedDatabaseSourceBindingRegistry({registryDirectory:path.resolve('not-created-protected-source'),installationId:crypto.randomUUID(),publicKeySpkiSha256:'a'.repeat(64),currentHeadSha256:'b'.repeat(64)},deadline()),/Windows ACL/) +}) +test('proven POSIX helpers plus genuine signature/head bytes retain false generation/READY',()=>own(async f=>{ + const value=await f.api.readProtectedDatabaseSourceBindingRegistry(f.pin,deadline()) + assert.equal(value.platformProtectionVerified,true);assert.equal(value.generationAuthenticated,false);assert.equal(value.allWriterDrainAuthenticated,false);assert.equal(value.readyAuthorized,false) + assert.equal(value.headSha256,f.pin.currentHeadSha256);assert.equal(f.verificationReads(),1) +})) +for(const fault of ['modeFile','modeDirectory','symlink'])test(fault+' refuses before cryptographic registry read',()=>own(async f=>{ + f.faults[fault]=true;await assert.rejects(f.api.readProtectedDatabaseSourceBindingRegistry(f.pin,deadline()));assert.equal(f.verificationReads(),0) +})) +for(const fault of ['replacement','lateMode'])test(fault+' during genuine signature verification refuses protected output',()=>own(async f=>{ + f.faults[fault]=true;await assert.rejects(f.api.readProtectedDatabaseSourceBindingRegistry(f.pin,deadline()));assert.equal(f.verificationReads(),1) +})) +test('expired/aborted lifetime refuses before filesystem or source access',()=>own(async f=>{ + const abort=new AbortController();abort.abort() + for(const options of [{deadlineAt:Date.now()-1},{deadlineAt:Date.now()+1000,signal:abort.signal}])await assert.rejects(f.api.readProtectedDatabaseSourceBindingRegistry(f.pin,options)) + assert.equal(f.verificationReads(),0) +})) +test('source verifier drift cannot grant protected output',()=>own(async f=>{ + f.faults.lateMode=true + const verify=f.api.createProtectedDatabaseSourceBindingVerifier(f.pin).verify + await assert.rejects(verify({} as Parameters[0],{} as Parameters[1],deadline())) + assert.equal(f.verificationReads(),1) +})) diff --git a/backend/scripts/writer-profile/publisher.mjs b/backend/scripts/writer-profile/publisher.mjs new file mode 100644 index 00000000..bffc7412 --- /dev/null +++ b/backend/scripts/writer-profile/publisher.mjs @@ -0,0 +1,345 @@ +// OPERATOR/CLI ONLY. Runtime host must never import this private-key module. +import pg from 'pg' +import {createPrivateKey,createPublicKey,sign,X509Certificate} from 'node:crypto' +import {open,lstat,realpath,unlink} from 'node:fs/promises' +import {readProtected,writeFile,withJournalLease} from './operator-files.mjs' +import {retainPublisherTlsPeer,upgradeApproval,isUpgradeApprovalFormat,UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,UPGRADE58_APPROVAL_FORMAT,REBIND58_APPROVAL_FORMAT} from './operator-contract.mjs' +import {BUILD44_PROVENANCE} from './fixed44-build-definition.js' +import {BUILD56_PROVENANCE} from './fixed56-build-definition.js' +import {BUILD57_PROVENANCE} from './fixed57-build-definition.js' +import {BUILD58_PROVENANCE} from './fixed58-build-definition.js' +import path from 'node:path' +import {captureDatabaseSourceObservation,assertDatabaseSourceObservationCurrent} from '../../src/db/database-source-observation.js' +import {MIGRATION_MANIFEST,migrationManifestDigest} from '../../src/db/migration-manifest.js' + + +import {ownedContext,sameOwned,sourceCoordinates,protectedStructure,readCurrent,HISTORY,CURRENT} from './context.mjs' +import {bytes,canonical,sha,nextEpoch,POLICY_DOMAIN,HEAD_DOMAIN,KEY,liveValidity,policySchema,headSchema,publicKey} from '../../src/db/application-writer-profile-format.js' +const upgradeTransitions = { + [UPGRADE_APPROVAL_FORMAT]: { predecessor: BUILD44_PROVENANCE, successor: BUILD56_PROVENANCE, oldEdition: 44, newEdition: 56 }, + [UPGRADE57_APPROVAL_FORMAT]: { predecessor: BUILD56_PROVENANCE, successor: BUILD57_PROVENANCE, oldEdition: 56, newEdition: 57 }, + [UPGRADE58_APPROVAL_FORMAT]: { predecessor: BUILD57_PROVENANCE, successor: BUILD58_PROVENANCE, oldEdition: 57, newEdition: 58 }, + [REBIND58_APPROVAL_FORMAT]: { predecessor: BUILD58_PROVENANCE, successor: BUILD58_PROVENANCE, oldEdition: 58, newEdition: 58 }, +} +export function requireCurrentCatalogueDefinition(approval,definition){ + const manifestSha256=migrationManifestDigest() + if(approval.manifestSha256!==manifestSha256||definition.manifestSha256!==manifestSha256||definition.definitionSha256!==approval.definitionSha256)throw Error('Approved catalogue definition is not bound to this build migration manifest') + return manifestSha256 +} + +/** Trusted held owner only. The independent issuer supplies the signed bytes. */ +export async function publishLegacyHandoffIntent({application,control,sourcePin,tlsPin,archiveDirectory,signedIntent,deadlineAt}){ + if(!Buffer.isBuffer(signedIntent)||signedIntent.length===0||signedIntent.length>4096)throw Error('Bounded signed legacy intent required') + const intentBytes=Buffer.from(signedIntent),pin=Object.freeze({...sourcePin}) + if(!Number.isSafeInteger(deadlineAt)||Date.now()>=deadlineAt||deadlineAt-Date.now()>120000)throw Error('Bounded held legacy handoff lifetime required') + const live=retainPublisherTlsPeer(application,tlsPin) + const fence=()=>{live();if(Date.now()>=deadlineAt)throw Error('Held legacy handoff deadline exceeded')} + let controlAcknowledged=false + try{return await withJournalLease(archiveDirectory,async()=>{ + const {assertHeldLegacyProbeOwner,verifyHeldLegacyProbeOwner,verifyLegacyProbeHandoffIntent}=await import('../../src/services/server-probe-legacy-handoff-inspection.js') + const {readProtectedDatabaseSourceBindingRegistry}=await import('./protected-source-registry.mjs') + await readProtectedDatabaseSourceBindingRegistry(pin,{deadlineAt}) + const [archive,signedApproval,coldArchive,operatorPublicKeyPem]=await Promise.all([ + readProtected(path.join(archiveDirectory,'legacy-probe.dump'),64*1024*1024), + readProtected(path.join(archiveDirectory,'legacy-probe-approval.json'),4096), + readProtected(path.join(archiveDirectory,'legacy-probe-cold.dump'),64*1024*1024), + readProtected(path.join(pin.registryDirectory,'operator-public.pem'),4096), + ]) + const intent=await verifyLegacyProbeHandoffIntent(pin,{archive,signedApproval,coldArchive,intent:intentBytes,operatorPublicKeyPem}) + const sql={query:async(text,values)=>{fence();const result=await application.query(text,values);fence();return result}} + const proof=await verifyHeldLegacyProbeOwner(sql,intent) + const intentFile=path.join(archiveDirectory,'legacy-probe-handoff-intent.json') + await writeFile(intentFile,intentBytes,{flag:'wx',mode:0o600}) + await readProtected(intentFile,4096,intent.intentSha256) + await assertHeldLegacyProbeOwner(sql,proof) + try{await control.beginLegacyHandoff(proof);controlAcknowledged=true}catch(error){ + if(error?.outcomeUnknown===true){ + const unknown=bytes({format:'legacy-probe-0037-control-unknown-v1',operationId:intent.operationId,intentSha256:intent.intentSha256,automaticReplay:false}) + try{await writeFile(path.join(archiveDirectory,'legacy-probe-handoff-control-unknown.json'),unknown,{flag:'wx',mode:0o600})}catch{/* The durable signed intent remains for inspection. */} + } + throw error + } + await assertHeldLegacyProbeOwner(sql,proof) + return Object.freeze({operationId:intent.operationId,intentSha256:intent.intentSha256,intentRecorded:true,quarantineActive:true,automaticReplay:false,readyAuthorized:false}) + })}catch(error){ + if(controlAcknowledged)throw Object.assign(Error('Legacy handoff quarantine is pending inspection',{cause:error}),{controlAcknowledged:true}) + throw error + } +} + +export function createConnectedPublisher(installation,collectFixedCatalogue,sourceVerifier,approval,definition){ + const PUBLISHER=installation.roleBindings.publisher.name +class ProfilePublicationError extends Error{ + automaticReplay=false + constructor(outcome,phase,proposal,cleanupUnconfirmed,cause){super('Protected policy publication requires exact inspection',{cause});Object.assign(this,{outcome,phase,proposal,cleanupUnconfirmed})} +} +async function privateKey(filename){ + const resolved=await realpath(filename),meta=await lstat(filename) + if(resolved!==path.resolve(filename)||!meta.isFile()||meta.isSymbolicLink()||meta.size<1||meta.size>8192)throw Error('Explicit bounded operator-only key file required') + const handle=await open(resolved,'r');let data + try{const stat=await handle.stat();if(stat.dev!==meta.dev||stat.ino!==meta.ino||stat.size!==meta.size)throw Error('Operator key changed');data=await handle.readFile();if(data.length!==stat.size)throw Error('Operator key size changed')}finally{await handle.close()} + let key;try{key=createPrivateKey(data)}finally{data.fill(0)} + if(key.asymmetricKeyType!=='ed25519'||sha(createPublicKey(key).export({format:'der',type:'spki'}))!==installation.publicKeySpkiSha256)throw Error('Private key does not match fixed installation') + publicKey(installation);return key +} +// One retained actual Pool client; no injected SQL facade, SET ROLE, new pool, +// detached application callbacks or cleanup/COMMIT races. +async function session(pool,deadlineAt,work){ + if(!(pool instanceof pg.Pool)||!Number.isSafeInteger(deadlineAt)||deadlineAt<=Date.now()||deadlineAt-Date.now()>120000)throw Error('Bounded genuine publisher pool/lifetime required') + let client,assertTransport,retired=false,released=false,locked=false,begun=false,commitSent=false,committed=false,confirmedValue,proposal,phase='checkout',stopReject,cleanupUnconfirmed=false + const lifetime=new AbortController() + const stopped=new Promise((_,reject)=>{stopReject=reject});void stopped.catch(()=>{}) + const release=bad=>{if(!client||released)return;released=true;client.off('error',fault);client.off('end',fault);try{client.release(bad)}catch{cleanupUnconfirmed=true}} + const fault=()=>{if(retired)return;retired=true;cleanupUnconfirmed=true;lifetime.abort();release(true);stopReject(Error('Publisher connection/lifetime ended'))} + const timer=setTimeout(fault,Math.max(1,deadlineAt-Date.now())),bounded=p=>Promise.race([p,stopped]) + const fence=()=>{ + if(retired||Date.now()>=deadlineAt)throw Error('Publisher lifetime closed') + try{assertTransport()}catch(error){fault();throw error} + } + const query=async(text,values)=>{fence();return bounded(client.query(text,values))} + const state={query,signal:lifetime.signal,archiveLeaf:()=>{ + fence() + const leaf=new X509Certificate(client.connection.stream.getPeerCertificate().raw) + // libpq trusts this certificate directly and must not accept a delegated certificate. + if(leaf.ca||leaf.subject!==leaf.issuer||!leaf.verify(leaf.publicKey))throw Error('Publisher archive requires a self-signed non-CA TLS leaf') + return leaf.toString() + },phase:value=>{phase=value},proposal:value=>{proposal=Object.freeze(value)},begun:value=>{begun=value},commit:async value=>{ + fence() + confirmedValue=Object.freeze({...value}) + commitSent=true + const response=await bounded(client.query('COMMIT').then(result=>{if(result.command==='COMMIT'){committed=true;begun=false}return result})) + if(response.command!=='COMMIT')throw Error('Publisher COMMIT acknowledgement absent') + }} + let value,primary,failed=false + try{ + const pending=pool.connect().then(value=>{client=value;if(retired){release(true);return value}client.on('error',fault);client.on('end',fault);return value});void pending.catch(()=>{}) + await bounded(pending);if(retired)throw Error('Late publisher checkout') + try{assertTransport=retainPublisherTlsPeer(client,installation.tls)}catch(error){retired=true;release(true);throw error} + await query("SET statement_timeout='10s'");await query("SET lock_timeout='5s'") + phase='exclusive-admission';await query('SELECT pg_catalog.pg_advisory_lock($1::bigint)',[KEY]);locked=true + value=await work(state) + }catch(error){failed=true;primary=error} + finally{ + if(!retired&&client){ + if(begun&&!commitSent)try{const r=await query('ROLLBACK');if(r.command!=='ROLLBACK')throw Error('Publisher rollback unconfirmed');begun=false}catch{fault()} + if(commitSent&&!committed){cleanupUnconfirmed=true;retired=true;release(true)} + if(!retired&&locked)try{const r=await query('SELECT pg_catalog.pg_advisory_unlock($1::bigint) AS released',[KEY]);if(r.rows.length!==1||r.rows[0].released!==true)throw Error('Publisher unlock unconfirmed');locked=false}catch{fault()} + if(!retired)try{await query('RESET lock_timeout');await query('RESET statement_timeout')}catch{fault()} + } + clearTimeout(timer);release(retired||cleanupUnconfirmed||begun||locked||committed&&failed) + } + if(committed)return Object.freeze({...confirmedValue,...value,outcome:'committed',proposal,cleanupUnconfirmed:cleanupUnconfirmed||retired||failed,automaticReplay:false}) + if(failed||cleanupUnconfirmed||retired)throw new ProfilePublicationError(commitSent||begun?'unknown':'not-committed',phase,proposal,cleanupUnconfirmed||retired,primary) + return value +} +async function observeCurrentCatalogue(s,manifestSha256){ + const security=await protectedStructure(s,installation,definition?.edition===58),catalogue=await collectFixedCatalogue(s) + if(catalogue.manifestSha256!==manifestSha256||catalogue.definitionSha256!==definition.definitionSha256)throw Error('Catalogue evidence differs from the approved migration manifest/definition') + // Inspect one extra row so an unexpected marker cannot hide after the expected prefix. + const expected=MIGRATION_MANIFEST.map(pin=>pin.file.slice(0,-4)) + const markers=(await s.query('SELECT id FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1',[expected.length+1])).rows.map(row=>row.id) + if(canonical(markers)!==canonical(expected))throw Error('Exact current migration marker metadata required') + return {security,catalogue,catalogSha256:sha(canonical({base:catalogue.catalogSha256,security:security.sha256}))} +} +async function publishCurrentProfile(pool,{privateKeyFile,expectedHeadSha256,journalDirectory,validityMs=86400000,deadlineAt=Date.now()+120000},manifestSha256,upgrade){ + const approved=upgrade??approval + if(!Number.isSafeInteger(validityMs)||validityMs<1||validityMs>86400000||expectedHeadSha256!==null&&!(/^[a-f0-9]{64}$/).test(expectedHeadSha256))throw Error('Explicit previous head and bounded validity required') + const key=await privateKey(privateKeyFile),directory=await realpath(journalDirectory),pendingFile=path.join(directory,'pending-publication.json') + try{await lstat(pendingFile);throw Error('Pending publication must be reconciled before signing')}catch(error){if(error.code!=='ENOENT')throw error} + const result=await session(pool,deadlineAt,async s=>{ + s.phase('publisher-read');await s.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');s.begun(true) + const first=await ownedContext(s,installation,PUBLISHER,'publisher-read') + const facade=Object.freeze({query:(sql,values)=>s.query(sql,values)}),options={deadlineAt} + const observation=await captureDatabaseSourceObservation(facade,options) + const sourceEvidence=await sourceVerifier.verify(facade,observation,options) + if(upgrade&&sourceEvidence.platformProtectionVerified!==true)throw Error('Upgrade requires protected source evidence') + if(canonical(sourceCoordinates(first))!==canonical(installation.expectedSource))throw Error('Pinned source mismatch') + const {security,catalogue,catalogSha256}=await observeCurrentCatalogue(s,manifestSha256) + const old=await readCurrent(s,installation,{allowEmpty:true,validNow:false}) + if((old?.headSha256??null)!==expectedHeadSha256)throw Error('Expected protected current head mismatch') + if(approved.expiresAt<=Date.now()||approved.catalogSha256!==catalogSha256)throw Error('Independently approved policy differs from actual catalogue') + const sameSubject=old&&canonical(old.policy.source)===canonical(sourceCoordinates(first))&&canonical(old.policy.roles)===canonical(security.roles) + if(upgrade){ + const {predecessor,oldEdition}=upgradeTransitions[upgrade.format] + if(!sameSubject||old.policy.manifestSha256!==predecessor.manifestSha256|| + old.policy.definitionSha256!==predecessor.templateSha256||old.policy.catalogSha256!==upgrade.previousCatalogSha256){ + throw Error(`Upgrade requires the exact signed source${oldEdition} predecessor on the same source and roles`) + } + }else if(old&&(!sameSubject||old.policy.manifestSha256!==manifestSha256||old.policy.definitionSha256!==catalogue.definitionSha256||old.policy.catalogSha256!==catalogSha256)){ + throw Error('Renewal cannot adopt a changed source/subject/policy') + } + const terminalSourceEvidence=await sourceVerifier.verify(facade,observation,options) + if(upgrade&&terminalSourceEvidence.platformProtectionVerified!==true)throw Error('Upgrade requires terminal protected source evidence') + await assertDatabaseSourceObservationCurrent(facade,observation,options);sameOwned(first,await ownedContext(s,installation,PUBLISHER,'publisher-read')) + await s.query('COMMIT');s.begun(false) + const number=old?nextEpoch(old.epoch):'1',now=Date.now() + const body=policySchema.parse({format:'application-writer-policy-v1',installationId:installation.installationId,epoch:number,issuedAt:now,notBefore:now,notAfter:now+validityMs,source:sourceCoordinates(first),sourceObservationSha256:sha(canonical(observation)),roles:security.roles,manifestSha256,definitionSha256:catalogue.definitionSha256,catalogSha256,futureLeaves:'fixed12-native-policy-v1'}) + const envelope=(domain,body)=>bytes({body,signature:sign(null,Buffer.from(domain+canonical(body)),key).toString('base64url')}) + const policyBytes=envelope(POLICY_DOMAIN,body),policySha256=sha(policyBytes) + const head=headSchema.parse({format:'application-writer-head-v1',installationId:installation.installationId,epoch:number,policySha256,previousHeadSha256:old?.headSha256??null}) + const headBytes=envelope(HEAD_DOMAIN,head),headSha256=sha(headBytes) + const proposal={installationId:installation.installationId,epoch:number,headSha256,previousHeadSha256:head.previousHeadSha256,policySha256,publisherPid:first.pid} + s.proposal(proposal) + // Public, bounded, exclusively created journal; neither key nor SQL data. + await writeFile(path.join(directory,'proposal-'+headSha256+'.json'),bytes(proposal),{flag:'wx',mode:0o600}) + await writeFile(pendingFile,bytes(proposal),{flag:'wx',mode:0o600}) + s.phase('publisher-write');await s.query('BEGIN ISOLATION LEVEL READ COMMITTED READ WRITE');s.begun(true) + await s.query('SELECT pg_catalog.pg_current_xact_id()');await s.query('SELECT pg_catalog.pg_advisory_xact_lock_shared($1::bigint)',[KEY]) + const owner=await ownedContext(s,installation,PUBLISHER,'publisher-write') + if(owner.pid!==first.pid||canonical(sourceCoordinates(owner))!==canonical(sourceCoordinates(first)))throw Error('Retained publisher source/backend changed') + await s.query(`SELECT id FROM mmapp_control.${CURRENT} WHERE id=1 FOR UPDATE`) + const current=await readCurrent(s,installation,{allowEmpty:true,validNow:false}) + if((current?.headSha256??null)!==expectedHeadSha256)throw Error('CAS predecessor changed') + const again=await observeCurrentCatalogue(s,manifestSha256) + if(again.catalogSha256!==catalogSha256)throw Error('Policy drift during retained exclusive publication') + if(Date.now()>=body.notAfter||Date.now()>=approved.expiresAt)throw Error('Policy or approval expired before publication dispatch') + await s.query(`INSERT INTO mmapp_control.${HISTORY}(installation_id,epoch,head_sha256,previous_installation_id,previous_epoch,previous_head_sha256,policy_sha256,policy_bytes,head_bytes) VALUES($1::uuid,$2::bigint,$3,$4::uuid,$5::bigint,$6,$7,$8::bytea,$9::bytea)`,[installation.installationId,number,headSha256,old?installation.installationId:null,old?.epoch??null,old?.headSha256??null,policySha256,policyBytes,headBytes]) + const changed=old?await s.query(`UPDATE mmapp_control.${CURRENT} SET epoch=$1::bigint,head_sha256=$2 WHERE id=1 AND installation_id=$3::uuid AND epoch=$4::bigint AND head_sha256=$5 RETURNING id`,[number,headSha256,installation.installationId,old.epoch,old.headSha256]):await s.query(`INSERT INTO mmapp_control.${CURRENT}(id,installation_id,epoch,head_sha256) VALUES(1,$1::uuid,$2::bigint,$3) RETURNING id`,[installation.installationId,number,headSha256]) + if(changed.rows.length!==1)throw Error('Exactly one protected current CAS required') + sameOwned(owner,await ownedContext(s,installation,PUBLISHER,'publisher-write'));if(Date.now()>=body.notAfter||Date.now()>=approved.expiresAt)throw Error('Policy or approval expired before publication terminal');s.phase('commit');const result={epoch:number,headSha256,notAfter:body.notAfter};await s.commit(result) + const terminal=await ownedContext(s,installation,PUBLISHER,'publisher-session') + if(terminal.pid!==first.pid||canonical(sourceCoordinates(terminal))!==canonical(sourceCoordinates(first)))throw Error('Publisher session continuity unconfirmed after actual ACK') + return result + }) + // A journal failure cannot turn a confirmed publication into retryable failure. + if(result.outcome==='committed'&&!result.cleanupUnconfirmed){try{const cursor=await boundedPending(pendingFile);if(cursor.headSha256!==result.headSha256)throw Error('Pending cursor differs');await unlink(pendingFile)}catch{return Object.freeze({...result,journalUnconfirmed:true})}} + return result +} +async function inspectPublication(pool,proposal,{deadlineAt=Date.now()+120000}={}){ + if(!proposal||proposal.installationId!==installation.installationId||!(/^[a-f0-9]{64}$/).test(proposal.headSha256)||proposal.previousHeadSha256!==null&&!(/^[a-f0-9]{64}$/).test(proposal.previousHeadSha256))throw Error('Bounded public proposal required') + return session(pool,deadlineAt,async s=>{ + s.phase('publication-inspection');await s.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');s.begun(true) + const first=await ownedContext(s,installation,PUBLISHER,'publisher-read');await protectedStructure(s,installation,definition?.edition===58) + if(canonical(sourceCoordinates(first))!==canonical(installation.expectedSource))throw Error('Pinned inspection source mismatch') + const facade=Object.freeze({query:(sql,values)=>s.query(sql,values)}),options={deadlineAt} + const observation=await captureDatabaseSourceObservation(facade,options);await sourceVerifier.verify(facade,observation,options) + const current=await readCurrent(s,installation,{allowEmpty:true,validNow:false}) + const proposed=(await s.query(`SELECT epoch::text,head_sha256 FROM mmapp_control.${HISTORY} WHERE installation_id=$1::uuid AND epoch=$2::bigint`,[installation.installationId,proposal.epoch])).rows + await sourceVerifier.verify(facade,observation,options);sameOwned(first,await ownedContext(s,installation,PUBLISHER,'publisher-read')) + const response=await s.query('ROLLBACK');if(response.command!=='ROLLBACK')throw Error('Inspection terminal unconfirmed');s.begun(false) + if(current?.headSha256===proposal.headSha256&&proposed.length===1&&proposed[0].head_sha256===proposal.headSha256)return {state:'confirmed',headSha256:current.headSha256} + if((current?.headSha256??null)===proposal.previousHeadSha256&&proposed.length===0)return {state:'not-committed',headSha256:current?.headSha256??null} + throw Error('Publication state requires operator inspection; no retry') + }) +} +// Read-only diagnostics on the actual protected publisher session. This does +// not admit application/operator writes, bootstrap, restore, or READY. +async function preflightCurrentProfile(pool,{deadlineAt=Date.now()+120000}={}){ + const manifestSha256=requireCurrentCatalogueDefinition(approval,definition) + return session(pool,deadlineAt,async s=>{ + s.phase('profile-preflight');await s.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');s.begun(true) + const first=await ownedContext(s,installation,PUBLISHER,'publisher-read') + if(canonical(sourceCoordinates(first))!==canonical(installation.expectedSource))throw Error('Pinned preflight source mismatch') + const facade=Object.freeze({query:(sql,values)=>s.query(sql,values)}),options={deadlineAt} + const observation=await captureDatabaseSourceObservation(facade,options),initialSource=await sourceVerifier.verify(facade,observation,options) + const {security,catalogue,catalogSha256}=await observeCurrentCatalogue(s,manifestSha256) + const current=await readCurrent(s,installation) + if(!current)throw Error('Current signed policy required for preflight') + if(approval.expiresAt<=Date.now()||approval.catalogSha256!==catalogSha256)throw Error('Independent preflight approval differs from actual catalogue') + if(canonical(current.policy.source)!==canonical(sourceCoordinates(first))||canonical(current.policy.roles)!==canonical(security.roles)||current.policy.manifestSha256!==manifestSha256||current.policy.definitionSha256!==catalogue.definitionSha256||current.policy.catalogSha256!==catalogSha256)throw Error('Current signed policy differs from actual source/roles/catalogue') + const terminalSource=await sourceVerifier.verify(facade,observation,options);await assertDatabaseSourceObservationCurrent(facade,observation,options) + sameOwned(first,await ownedContext(s,installation,PUBLISHER,'publisher-read')) + // Recheck the live interval after the potentially lengthy catalogue walk. + const clock=(await s.query('SELECT (EXTRACT(EPOCH FROM pg_catalog.clock_timestamp())*1000)::bigint::text AS now')).rows + if(clock.length!==1)throw Error('Preflight terminal server clock unavailable') + liveValidity(current.policy,Number(clock[0].now)) + if(approval.expiresAt<=Date.now())throw Error('Preflight approval expired before terminal') + const terminal=await s.query('ROLLBACK');if(terminal.command!=='ROLLBACK')throw Error('Preflight terminal unconfirmed');s.begun(false) + return Object.freeze({state:'matched',epoch:current.epoch,headSha256:current.headSha256,notAfter:current.policy.notAfter, + sourceMatched:true,rolesMatched:true,catalogueMatched:true,platformProtectionVerified:initialSource?.platformProtectionVerified===true&&terminalSource?.platformProtectionVerified===true,writerAuthorized:false,generationAuthenticated:false,readyAuthorized:false}) + }) +} +async function inspectCurrentCatalogue(pool,{deadlineAt=Date.now()+120000}={}){ + const manifestSha256=migrationManifestDigest() + if(definition?.edition!==58||definition.manifestSha256!==manifestSha256||definition.definitionSha256!==BUILD58_PROVENANCE.templateSha256)throw Error('Approved source58 definition required') + return session(pool,deadlineAt,async s=>{ + s.phase('source-catalogue-inspection');await s.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');s.begun(true) + const first=await ownedContext(s,installation,PUBLISHER,'publisher-read') + if(canonical(sourceCoordinates(first))!==canonical(installation.expectedSource))throw Error('Pinned inspection source mismatch') + const facade=Object.freeze({query:(sql,values)=>s.query(sql,values)}),options={deadlineAt} + const observation=await captureDatabaseSourceObservation(facade,options) + const initial=await sourceVerifier.verify(facade,observation,options) + const {security,catalogSha256}=await observeCurrentCatalogue(s,manifestSha256) + const current=await readCurrent(s,installation,{validNow:false}) + if(!current||current.policy.definitionSha256!==definition.definitionSha256||current.policy.manifestSha256!==manifestSha256|| + canonical(current.policy.source)!==canonical(sourceCoordinates(first))||canonical(current.policy.roles)!==canonical(security.roles)){ + throw Error('Approved source58 predecessor required') + } + const terminal=await sourceVerifier.verify(facade,observation,options) + if(initial.platformProtectionVerified!==true||terminal.platformProtectionVerified!==true)throw Error('Protected source evidence required') + await assertDatabaseSourceObservationCurrent(facade,observation,options) + sameOwned(first,await ownedContext(s,installation,PUBLISHER,'publisher-read')) + if((await s.query('ROLLBACK')).command!=='ROLLBACK')throw Error('Catalogue inspection rollback unconfirmed') + s.begun(false) + return Object.freeze({state:'observed',epoch:current.epoch,headSha256:current.headSha256,previousCatalogSha256:current.policy.catalogSha256, + catalogSha256,manifestSha256,definitionSha256:definition.definitionSha256,platformProtectionVerified:true,readyAuthorized:false}) + }) +} +async function inspectLegacyStorage(pool,{deadlineAt,archiveDirectory,custody=false,handoff}){ + const {captureLegacyProbeStorage,captureLegacyProbeLineage,LEGACY_PROBE_CUSTODY_LOCK}=await import('../../src/services/server-probe-legacy-source-inventory.js') + return session(pool,deadlineAt,async s=>{ + s.phase('legacy-storage-inspection');await s.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');s.begun(true) + if(custody)await s.query(LEGACY_PROBE_CUSTODY_LOCK) + const first=await ownedContext(s,installation,PUBLISHER,'publisher-read') + if(canonical(sourceCoordinates(first))!==canonical(installation.expectedSource))throw Error('Pinned legacy source mismatch') + const facade=Object.freeze({query:(sql,values)=>s.query(sql,values)}),options={deadlineAt} + await s.query("SET LOCAL TimeZone = 'UTC'; SET LOCAL DateStyle = 'ISO, YMD'") + const observation=await captureDatabaseSourceObservation(facade,options) + const initial=await sourceVerifier.verify(facade,observation,options) + if(handoff&&initial.platformProtectionVerified!==true)throw Error('Protected legacy handoff source required') + const resolution=handoff?await (await import('../../src/services/server-probe-legacy-handoff-inspection.js')).observeLegacyProbeHandoff(facade,handoff,deadlineAt):undefined + const snapshot=handoff?undefined:await captureLegacyProbeStorage(facade) + const lineageSnapshot=custody?await captureLegacyProbeLineage(facade):undefined + let archive + if(archiveDirectory!==undefined){ + if(initial.platformProtectionVerified!==true)throw Error('Protected legacy source evidence required') + s.phase('legacy-storage-export') + const {runPinnedLegacyProbeSnapshotDump}=await import('../../src/services/database-dump-connection.js') + const {rows}=await s.query('SELECT pg_catalog.pg_export_snapshot() AS snapshot') + const {host,port,database,password}=pool.options + if(host!==installation.tls.serverName||database!==first.database_name||!Number.isInteger(port)||typeof password!=='string')throw Error('Legacy export transport mismatch') + const peer={host,port,database,capture:installation.roleBindings.publisher,leaf:s.archiveLeaf()} + archive=await runPinnedLegacyProbeSnapshotDump(peer,password,rows[0]?.snapshot,archiveDirectory,{signal:s.signal,timeoutMs:Math.max(1,deadlineAt-Date.now())}) + if(archive.subarray(0,5).toString()!=='PGDMP')throw Error('Legacy export did not return a PostgreSQL archive') + } + const source=await sourceVerifier.verify(facade,observation,options) + await assertDatabaseSourceObservationCurrent(facade,observation,options) + sameOwned(first,await ownedContext(s,installation,PUBLISHER,'publisher-read')) + const terminal=await s.query('ROLLBACK');if(terminal.command!=='ROLLBACK')throw Error('Legacy inspection rollback unconfirmed');s.begun(false) + return Object.freeze({state:'observed',snapshot,source,...(archive ? {archive} : {}),...(lineageSnapshot?{lineageSnapshot}:{}),...(resolution?{resolution}:{})}) + }) +} +async function boundedPending(filename){ + const h=await open(filename,'r');try{const stat=await h.stat();if(!stat.isFile()||stat.size<1||stat.size>2048)throw Error('Bounded operator pending cursor required');const raw=await h.readFile();if(raw.length!==stat.size)throw Error('Pending cursor changed');const p=JSON.parse(raw.toString('utf8'));if(!bytes(p).equals(raw)||Object.keys(p).sort().join(',')!=='epoch,headSha256,installationId,policySha256,previousHeadSha256,publisherPid'||!(/^[1-9][0-9]*$/).test(p.epoch)||!Number.isSafeInteger(p.publisherPid))throw Error('Exact pending cursor refused');return p}finally{await h.close()} +} +async function reconcilePendingPublication(pool,{journalDirectory,deadlineAt=Date.now()+120000}){ + const directory=await realpath(journalDirectory),filename=path.join(directory,'pending-publication.json') + let proposal;try{proposal=await boundedPending(filename)}catch(error){if(error.code==='ENOENT')return {state:'idle'};throw error} + const result=await inspectPublication(pool,proposal,{deadlineAt}) + const after=await boundedPending(filename);if(canonical(after)!==canonical(proposal))throw Error('Pending cursor changed during protected inspection') + await unlink(filename) + return {...result,proposal} +} +return Object.freeze({ + inspectLegacyStorage, + inspectCurrentCatalogue, + preflightCurrentProfile, + publishCurrentProfile:async(pool,options)=>{ + if(isUpgradeApprovalFormat(approval.format))throw Error('Upgrade approval requires the explicit upgrade command') + const manifestSha256=requireCurrentCatalogueDefinition(approval,definition) + return withJournalLease(options.journalDirectory,()=>publishCurrentProfile(pool,options,manifestSha256)) + }, + upgradeCurrentProfile:async(pool,options)=>{ + const upgrade=upgradeApproval(approval) + const manifestSha256=requireCurrentCatalogueDefinition(upgrade,definition) + const {successor,newEdition}=upgradeTransitions[upgrade.format] + if(manifestSha256!==successor.manifestSha256||definition.definitionSha256!==successor.templateSha256|| + upgrade.expectedHeadSha256!==options.expectedHeadSha256||upgrade.validityMs!==options.validityMs){ + throw Error(`Exact source${newEdition} upgrade target and predecessor approval required`) + } + return withJournalLease(options.journalDirectory,()=>publishCurrentProfile(pool,options,manifestSha256,upgrade)) + }, + inspectPublication, + reconcilePendingPublication:(pool,options)=>withJournalLease(options.journalDirectory,()=>reconcilePendingPublication(pool,options)), +}) +} diff --git a/backend/scripts/writer-profile/registry-enrollment.mts b/backend/scripts/writer-profile/registry-enrollment.mts new file mode 100644 index 00000000..a924ecf2 --- /dev/null +++ b/backend/scripts/writer-profile/registry-enrollment.mts @@ -0,0 +1,185 @@ +import assert from 'node:assert/strict' +import type {PoolClient} from 'pg' +import {deploymentRequest} from '../database-writer-bootstrap-operator.mjs' +import {assertJsDriverEnvironment,poolOptions,retainPublisherTlsPeer} from './operator-contract.mjs' +import {readProtected} from './operator-files.mjs' +import {KEY,canonical,publicKey,sha} from '../../src/db/application-writer-profile-format.js' +import {readCurrentApplicationWriterProfile} from '../../src/db/application-writer-profile-registry-read.js' +import {BUILD57_PROVENANCE} from './fixed57-build-definition.js' +import {readProtectedRegistryStructureData,type RegistryRoleBindingsData} from '../../src/db/application-writer-profile-registry-structure.js' +import {MIGRATION_MANIFEST,migrationManifestDigest} from '../../src/db/migration-manifest.js' +import {RESTORE_WRITER_PROFILE_DEFINITION_SHA256,RESTORE_WRITER_PROFILE_MANIFEST_SHA256,restoreProfileEnrollmentSql} from '../../src/db/database-restore-quarantine-profile.js' + +const baseFlags=['approval','approval-sha256','transport','transport-sha256','ca'] +const digest=/^[a-f0-9]{64}$/ +const json=(bytes:Buffer)=>JSON.parse(new TextDecoder('utf8',{fatal:true}).decode(bytes)) + +// The accepted registry fixture's two tables and relation ACLs. Other schema +// objects and cross-owner metadata grants are setup prerequisites. +const registrySql=`CREATE TABLE mmapp_control.application_writer_profile_history ( + installation_id uuid NOT NULL, epoch bigint NOT NULL CHECK(epoch>0), + head_sha256 text NOT NULL CHECK(head_sha256~'^[a-f0-9]{64}$'), + previous_installation_id uuid, previous_epoch bigint, previous_head_sha256 text, + policy_sha256 text NOT NULL CHECK(policy_sha256~'^[a-f0-9]{64}$'), + policy_bytes bytea NOT NULL CHECK(octet_length(policy_bytes) BETWEEN 1 AND 1048576), + head_bytes bytea NOT NULL CHECK(octet_length(head_bytes) BETWEEN 1 AND 8192), + PRIMARY KEY(installation_id,epoch), UNIQUE(installation_id,epoch,head_sha256), + CHECK((epoch=1 AND previous_installation_id IS NULL AND previous_epoch IS NULL AND previous_head_sha256 IS NULL) + OR(epoch>1 AND previous_installation_id IS NOT NULL AND previous_installation_id=installation_id + AND previous_epoch IS NOT NULL AND previous_epoch=epoch-1 AND previous_head_sha256 IS NOT NULL)), + FOREIGN KEY(previous_installation_id,previous_epoch,previous_head_sha256) + REFERENCES mmapp_control.application_writer_profile_history(installation_id,epoch,head_sha256) MATCH FULL +); +CREATE TABLE mmapp_control.application_writer_profile_current ( + id smallint PRIMARY KEY CHECK(id=1), installation_id uuid NOT NULL, epoch bigint NOT NULL, head_sha256 text NOT NULL, + FOREIGN KEY(installation_id,epoch,head_sha256) REFERENCES mmapp_control.application_writer_profile_history(installation_id,epoch,head_sha256) +);` + +const targetSql=(inspect:boolean,restoreProfile:boolean)=>`SELECT d.oid::text AS "databaseOid", d.datname::text AS "databaseName", + control.system_identifier::text AS "systemIdentifier", pg_catalog.current_setting('server_version_num')::int AS "serverVersionNum", + pg_catalog.pg_is_in_recovery() AS "inRecovery", r.oid::text AS "roleOid" + FROM pg_catalog.pg_database d CROSS JOIN pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_control_system() control + JOIN pg_catalog.pg_namespace n ON n.nspname='mmapp_control' + WHERE d.datname=pg_catalog.current_database() AND r.rolname=SESSION_USER AND SESSION_USER=CURRENT_USER + AND r.rolname=$1 AND n.nspowner=r.oid AND pg_catalog.current_setting('transaction_read_only')='${inspect?'on':'off'}' + AND pg_catalog.current_setting('session_replication_role')='origin' AND pg_catalog.current_setting('event_triggers')='on' + ${inspect||restoreProfile?'':`AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_class c WHERE c.relnamespace=n.oid + AND c.relname IN('application_writer_profile_history','application_writer_profile_current'))`}` +const registryRelNames = ['application_writer_profile_current','application_writer_profile_history'] + +export function writerRegistryEnrollmentSql(bindings:RegistryRoleBindingsData){ + const {operator,publisher}=bindings + return [registrySql,`REVOKE ALL ON mmapp_control.application_writer_profile_history, mmapp_control.application_writer_profile_current FROM PUBLIC; + GRANT SELECT ON mmapp_control.application_writer_profile_history, mmapp_control.application_writer_profile_current TO "${operator.name}"; + GRANT SELECT, INSERT ON mmapp_control.application_writer_profile_history TO "${publisher.name}"; + GRANT SELECT, INSERT, UPDATE ON mmapp_control.application_writer_profile_current TO "${publisher.name}"`] +} + +/** Exact registry body for a separately authorized, retained control transaction. */ +export async function enrollWriterRegistryInTransaction(sql:Parameters[0],bindings:RegistryRoleBindingsData){ + for(const statement of writerRegistryEnrollmentSql(bindings))await sql.query(statement) + return readProtectedRegistryStructureData(sql,bindings) +} + +export async function registryEnrollmentMain(args:string[]){ + const inspect=args[0]==='registry-inspect' + const restoreProfile=args[0]==='restore-profile-enroll' + const flags=restoreProfile?[...baseFlags,'bootstrap','bootstrap-sha256']:baseFlags + assert((inspect||restoreProfile||args[0]==='registry-enroll')&&args.length===1+2*flags.length) + assertJsDriverEnvironment(process.env) + const values=new Map() + for(let i=1;ivalues.get(name)! + const approval=json(await readProtected(get('approval'),16384,get('approval-sha256'))) + const manifestSha256=migrationManifestDigest() + if(restoreProfile){ + assert.deepEqual(Object.keys(approval).sort(),['catalogSha256','definitionSha256','deploymentRequest','expectedHeadSha256','format','manifestSha256','previousCatalogSha256']) + assert(approval.format==='application-writer-restore-profile-enrollment-v1'&&manifestSha256===RESTORE_WRITER_PROFILE_MANIFEST_SHA256&& + approval.definitionSha256===RESTORE_WRITER_PROFILE_DEFINITION_SHA256&&[approval.catalogSha256,approval.previousCatalogSha256,approval.expectedHeadSha256] + .every(value=>typeof value==='string'&&digest.test(value))) + }else{ + assert.deepEqual(Object.keys(approval).sort(),['deploymentRequest','format','manifestSha256']) + assert(approval.format==='application-writer-registry-enrollment-v1') + } + assert.equal(approval.manifestSha256,manifestSha256) + const request=deploymentRequest(approval.deploymentRequest) + let subject:Parameters[0]|undefined + if(restoreProfile){ + const bootstrap=json(await readProtected(get('bootstrap'),32768,get('bootstrap-sha256'))) + assert(bootstrap.format==='application-writer-bootstrap-v1'&&bootstrap.publicationAuthorized===false&&bootstrap.readyAuthorized===false&& + canonical(deploymentRequest(bootstrap.deploymentRequest))===canonical(request),'Independently pinned bootstrap required for restore enrollment') + assert(bootstrap.verificationSubject?.installationId===request.installationId) + subject=bootstrap.verificationSubject + publicKey(subject!) + } + const bindings:RegistryRoleBindingsData=request.roles + const transport=json(await readProtected(get('transport'),16384,get('transport-sha256'))) + const ca=await readProtected(get('ca'),65536,request.tls.caSha256) + const options=poolOptions(transport,request,ca,'control') + const {default:pg}=await import('pg') + const pool=new pg.Pool({...options,query_timeout:12_000}) + const deadline=Date.now()+120_000 + let client:PoolClient|undefined,commitSent=false,committed=false,cleanupUnconfirmed=false + let registryStructureSha256:string|undefined + let observed:'absent'|'installed'|undefined + let failure:unknown + const onError=(error:Error)=>{failure??=error} + pool.on('error',onError) + try{ + const connected=await pool.connect() + client=connected + connected.on('error',onError) + const assertPeer=retainPublisherTlsPeer(connected,request.tls) + const fence=()=>{if(failure)throw failure;assert(Date.now()>(text:string,parameters?:unknown[])=>{ + fence() + const result=await connected.query(text,parameters) + fence() + return result + } + await query(inspect?'BEGIN READ ONLY':'BEGIN') + await query('SET LOCAL search_path = pg_catalog') + await query('SELECT pg_catalog.pg_advisory_xact_lock($1::bigint)',[KEY]) + const {control,operator,runtime}=bindings + const target=await query(targetSql(inspect,restoreProfile),[control.name]) + assert.deepEqual(target.rows,[{...request.source,roleOid:control.signedOid}],'Registry target/owner/absence refused') + const roles=await query<{name:string;oid:string}>('SELECT rolname::text AS name, oid::text AS oid FROM pg_catalog.pg_roles WHERE rolname=ANY($1::text[])', + [Object.values(bindings).map(role=>role.name)]) + assert.deepEqual(new Map(roles.rows.map(role=>[role.name,role.oid])),new Map(Object.values(bindings).map(role=>[role.name,role.signedOid]))) + const markers=MIGRATION_MANIFEST.map(pin=>({id:pin.file.slice(0,-4)})) + const actualMarkers=await query('SELECT id FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1',[markers.length+1]) + assert.deepEqual(actualMarkers.rows,markers,'Registry enrollment requires exact current migration markers') + if(inspect){ + const relations=await query<{name:string}>(`SELECT c.relname::text AS name FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='mmapp_control' + AND c.relname=ANY($1::text[]) ORDER BY c.relname COLLATE "C"`,[registryRelNames]) + if(relations.rows.length){ + assert.deepEqual(relations.rows.map(row=>row.name),registryRelNames,'Registry relations are incomplete') + registryStructureSha256=(await readProtectedRegistryStructureData({query},bindings)).sha256 + } + assert.equal((await query('ROLLBACK')).command,'ROLLBACK') + observed=relations.rows.length?'installed':'absent' + }else{ + if(restoreProfile){ + const {collectSource57Catalogue,collectSource58Catalogue}=await import('./catalogue.mjs') + const previous=await readProtectedRegistryStructureData({query},bindings),before=await collectSource57Catalogue({query},{roleBindings:bindings}) + assert.equal(sha(canonical({base:before.catalogSha256,security:previous.sha256})),approval.previousCatalogSha256) + await query('LOCK TABLE mmapp_control.application_writer_profile_current, mmapp_control.application_writer_profile_history IN SHARE MODE') + const current=await readCurrentApplicationWriterProfile({query},subject!) + assert(current?.headSha256===approval.expectedHeadSha256&¤t.policy.manifestSha256===BUILD57_PROVENANCE.manifestSha256&& + current.policy.definitionSha256===BUILD57_PROVENANCE.templateSha256&¤t.policy.catalogSha256===approval.previousCatalogSha256&& + canonical(current.policy.source)===canonical(request.source)&&canonical(current.policy.roles)===canonical(previous.roles), + 'Exact signed Source57 predecessor required for restore enrollment') + await query(restoreProfileEnrollmentSql(runtime.name,operator.name)) + const installed=await readProtectedRegistryStructureData({query},bindings,true),after=await collectSource58Catalogue({query},{roleBindings:bindings}) + registryStructureSha256=installed.sha256 + assert.equal(sha(canonical({base:after.catalogSha256,security:installed.sha256})),approval.catalogSha256) + assert.equal((await readCurrentApplicationWriterProfile({query},subject!))?.headSha256,current.headSha256) + }else{ + registryStructureSha256=(await enrollWriterRegistryInTransaction({query},bindings)).sha256 + } + fence() + commitSent=true + const ack=await connected.query('COMMIT') + assert.equal(ack.command,'COMMIT') + committed=true + } + }catch(error){failure??=error} + finally{ + // Destroying the retained session rolls back any unsuccessful open transaction. + try{client?.release(true)}catch(error){failure??=error;cleanupUnconfirmed=true} + try{await pool.end()}catch(error){failure??=error;cleanupUnconfirmed=true} + } + if(committed)return {event:restoreProfile?'application-writer-restore-profile-enrollment':'application-writer-registry-enrollment',outcome:'committed', + installationId:request.installationId,manifestSha256,registryStructureSha256,cleanupUnconfirmed:cleanupUnconfirmed||!!failure, + automaticReplay:false,readyAuthorized:false} + if(observed&&!failure)return {event:'application-writer-registry-inspection',outcome:observed, + installationId:request.installationId,manifestSha256,registryStructureSha256,automaticReplay:false,readyAuthorized:false} + if(commitSent)throw Object.assign(Error('Registry enrollment requires inspection',{cause:failure}),{code:'WRITER_REGISTRY_TERMINAL_UNKNOWN'}) + throw failure??Error('Registry enrollment refused') +} diff --git a/backend/scripts/writer-profile/registry-enrollment.test.mts b/backend/scripts/writer-profile/registry-enrollment.test.mts new file mode 100644 index 00000000..d956a0a9 --- /dev/null +++ b/backend/scripts/writer-profile/registry-enrollment.test.mts @@ -0,0 +1,74 @@ +import assert from 'node:assert/strict' +import {EventEmitter} from 'node:events' +import {mock,test} from 'node:test' +import {MIGRATION_MANIFEST,migrationManifestDigest} from '../../src/db/migration-manifest.js' + +test('enrollment uses the independently pinned owner, validates readback, and never retries an unknown COMMIT',async()=>{ + const digest='a'.repeat(64) + const roles=Object.fromEntries(['runtime','operator','capture','publisher','control'].map((role,index)=>[role,{name:`app_${role}`,signedOid:String(20000+index)}])) + const source={databaseOid:'30000',databaseName:'fixture',systemIdentifier:'121212',serverVersionNum:180004,inRecovery:false} + const deploymentRequest={format:'application-writer-bootstrap-request-v1',installationId:'7435bc0c-b7c8-40d8-be88-5f6ae478a221',sourceHeadSha256:digest, + source,roles,tls:{serverName:'localhost',caSha256:digest,spkiSha256:digest}} + const manifestSha256=migrationManifestDigest() + const approval={format:'application-writer-registry-enrollment-v1',manifestSha256,deploymentRequest} + const args=['registry-enroll','--approval','approval','--approval-sha256',digest,'--transport','transport','--transport-sha256',digest,'--ca','ca'] + let mode='success' + const trace:string[]=[] + class Client extends EventEmitter{ + async query(sql:string){ + trace.push(sql) + if(sql.includes('pg_catalog.pg_control_system()'))return {rows:mode==='wrong-target'?[]:[{...source,roleOid:roles.control.signedOid}]} + if(sql.includes('AS name, oid::text'))return {rows:Object.values(roles).map(role=>({name:role.name,oid:role.signedOid}))} + if(sql.includes('FROM public.schema_migrations'))return {rows:mode==='wrong-markers'?[]:MIGRATION_MANIFEST.map(pin=>({id:pin.file.slice(0,-4)}))} + if(sql.includes('c.relname=ANY'))return {rows:mode==='absent'?[]: + ['application_writer_profile_current',...(mode==='partial'?[]:['application_writer_profile_history'])].map(name=>({name}))} + if(sql==='COMMIT'&&mode==='ack-lost')throw Error('Synthetic ACK loss') + if(sql==='ROLLBACK'&&mode==='rollback-lost')throw Error('Synthetic ROLLBACK loss') + return {command:sql,rows:[]} + } + release(destroy:boolean){assert(destroy);trace.push('release')} + } + class Pool extends EventEmitter{ + async connect(){trace.push('connect');return new Client()} + async end(){trace.push('end')} + } + mock.module('pg',{defaultExport:{Pool}}) + mock.module('./operator-files.mjs',{namedExports:{readProtected:async(path:string,_limit:number,expected:string)=>{ + assert.equal(expected,digest) + return Buffer.from(JSON.stringify(path==='approval'?approval:{})) + }}}) + mock.module('./operator-contract.mjs',{namedExports:{assertJsDriverEnvironment:()=>{},poolOptions:(_transport:unknown,request:typeof deploymentRequest,_ca:Buffer,role:string)=>{ + assert.deepEqual(request,deploymentRequest);assert.equal(role,'control');return {} + },retainPublisherTlsPeer:()=>()=>{}}}) + mock.module('../../src/db/application-writer-profile-registry-structure.js',{namedExports:{readProtectedRegistryStructureData:async()=>{ + trace.push('profile-verified');if(mode==='drift')throw Error('Synthetic catalogue drift') + return {sha256:digest} + }}}) + const {registryEnrollmentMain}=await import('./registry-enrollment.mjs') + const before=await registryEnrollmentMain(args) + assert.equal(before.outcome,'committed');assert.equal(before.registryStructureSha256,digest);assert.equal(before.readyAuthorized,false) + const lock=trace.findIndex(sql=>sql.includes('pg_advisory_xact_lock')),ddl=trace.findIndex(sql=>sql.startsWith('CREATE TABLE')) + assert(lock>=0&&locksql.startsWith('CREATE TABLE')).length,scenario.startsWith('wrong-')?0:1) + assert.equal(trace.filter(sql=>sql==='COMMIT').length,scenario==='ack-lost'?1:0) + } + const inspectArgs=['registry-inspect',...args.slice(1)] + for(const scenario of ['absent','installed','partial','drift','rollback-lost']){ + mode=scenario;trace.length=0 + if(scenario==='absent'||scenario==='installed'){ + const result=await registryEnrollmentMain(inspectArgs) + assert.equal(result.outcome,scenario) + assert.equal(result.registryStructureSha256,scenario==='installed'?digest:undefined) + assert.equal(result.readyAuthorized,false) + assert.deepEqual(trace.slice(-3),['ROLLBACK','release','end']) + }else await assert.rejects(registryEnrollmentMain(inspectArgs),Error) + assert.equal(trace[1],'BEGIN READ ONLY') + assert.equal(trace.some(sql=>sql.startsWith('CREATE TABLE')||sql==='COMMIT'),false) + } +}) diff --git a/backend/scripts/writer-profile/source-activation.mts b/backend/scripts/writer-profile/source-activation.mts new file mode 100644 index 00000000..1009d830 --- /dev/null +++ b/backend/scripts/writer-profile/source-activation.mts @@ -0,0 +1,149 @@ +/** Deployment operator state. Store outside PGDATA, backups and runtime mounts. */ +import assert from 'node:assert/strict' +import {constants} from 'node:fs' +import {lstat,open,rename} from 'node:fs/promises' +import path from 'node:path' +import {z} from 'zod' +import {DATABASE_SOURCE_ACTIVATION_FILENAME as filename,MAX_DATABASE_SOURCE_ACTIVATION_BYTES as maximum, + databaseSourceActivationRecordSchema,parseDatabaseSourceActivationRecord,type DatabaseSourceActivationRecord as State} from '../../src/db/database-source-activation-record.js' +import {sourceBindingBytes,sourceBindingCanonical,type DatabaseSourceBindingBody} from '../../src/db/database-source-binding.js' +import {protectedDirectory,readProtected,sha,withJournalLease} from './operator-files.mjs' +import {readProtectedDatabaseSourceBindingRegistry, type createProtectedDatabaseSourceBindingVerifier} from './protected-source-registry.mjs' + +const digest=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid() +const sourcePin=z.object({registryDirectory:z.string().min(1).max(4096),installationId:uuid,publicKeySpkiSha256:digest,currentHeadSha256:digest}).strict() +const directoryPath=z.string().min(1).max(4096) +const approval=z.object({format:z.literal('database-source-activation-approval-v1'),activationDirectory:directoryPath,sourcePin,tlsSpkiSha256:digest, + expectedActivationSha256:digest.nullable()}).strict() +const parse=(bytes:Buffer):unknown=>JSON.parse(new TextDecoder('utf8',{fatal:true}).decode(bytes)) + +async function present(file:string){ + try{await lstat(file);return true}catch(error){if((error as NodeJS.ErrnoException).code==='ENOENT')return false;throw error} +} + +// Caller holds the same protected lease used by all activation writers. +async function current(directory:string){ + const file=path.join(directory,filename) + assert(!await present(file+'.pending'),'Unresolved activation transition') + if(!await present(file))return undefined + const bytes=await readProtected(file,maximum),state=parseDatabaseSourceActivationRecord(bytes) + assert.equal(state.activationDirectory,directory,'Activation record belongs to another directory') + return {state,activationSha256:sha(bytes)} +} + +async function persist(directory:string,state:State){ + await protectedDirectory(directory) + const bytes=sourceBindingBytes(databaseSourceActivationRecordSchema.parse(state)) + assert(bytes.length<=maximum) + const file=path.join(directory,filename),pending=file+'.pending' + const parent=await open(directory,constants.O_RDONLY|constants.O_DIRECTORY|constants.O_NOFOLLOW) + let confirmed=false,cleanupUnconfirmed=false + try{ + const handle=await open(pending,constants.O_WRONLY|constants.O_CREAT|constants.O_EXCL|constants.O_NOFOLLOW,0o600) + try{await handle.writeFile(bytes);await handle.sync()}finally{await handle.close()} + try{await rename(pending,file);await parent.sync()} + catch{throw Object.assign(Error('Activation terminal requires inspection'),{code:'SOURCE_ACTIVATION_TERMINAL_UNKNOWN'})} + confirmed=true + }finally{try{await parent.close()}catch{cleanupUnconfirmed=confirmed}} + return {activationSha256:sha(bytes),cleanupUnconfirmed} +} + +function receipt(state:State,activationSha256:string,outcome:'committed'|'observed'){ + return {event:'database-source-activation',outcome,activationSha256,status:state.status, + ordinal:state.generations.length,...state.generations.at(-1),generationAuthenticated:false,readyAuthorized:false,automaticReplay:false} +} + +type SourceVerifier=ReturnType + +/** Keep revocation and generation changes fenced through SQL and pool cleanup. */ +export async function withActiveSource(input:{directory:string,expectedSha256:string,sourcePin:z.infer,expectedSource:DatabaseSourceBindingBody['source'],tlsSpkiSha256:string,deadlineAt:number}, + verifier:SourceVerifier,work:(guardedVerifier:SourceVerifier)=>Promise){ + const {directory,deadlineAt}=input + const expectedSha256=digest.parse(input.expectedSha256),tlsSpkiSha256=digest.parse(input.tlsSpkiSha256) + const pin=sourcePin.parse(input.sourcePin) + return withJournalLease(directory,async()=>{ + async function readActive(){ + assert(Date.now()){ + await readActive() + const evidence=await verifier.verify(...args) + assert(evidence.platformProtectionVerified&&evidence.operatorSignatureVerified&&evidence.currentHeadPinVerified&&evidence.genuineSourceFactsMatched,'Unprotected activation source evidence') + assertGeneration(evidence) + await readActive() + sourceChecks++ + return evidence + }}) + const result=await work(guardedVerifier) + try{ + assert.equal(sourceChecks,2,'Initial and terminal source evidence required') + await readActive() + }catch(error){ + // The retained SQL session already acknowledged COMMIT. + if(result&&typeof result==='object'&&'outcome' in result&&result.outcome==='committed')return {...result,cleanupUnconfirmed:true} + throw error + } + return result + }) +} + +export async function sourceActivationMain(args:string[]){ + const command=args[0], flags=command==='source-activate'?['directory','approval','approval-sha256']: + command==='source-revoke'?['directory','expected-sha256']:['directory'] + assert(['source-activate','source-revoke','source-status'].includes(command)&&args.length===1+2*flags.length) + const values=new Map() + for(let i=1;i{ + const before=await current(directory) + if(command==='source-status'){ + assert(before,'Activation record absent') + return receipt(before.state,before.activationSha256,'observed') + } + if(command==='source-revoke'){ + const expected=digest.parse(values.get('expected-sha256')) + assert(before?.activationSha256===expected&&before.state.status==='active','Active activation digest changed') + const revoked={...before.state,status:'revoked' as const} + const saved=await persist(directory,revoked) + return {...receipt(revoked,saved.activationSha256,'committed'),cleanupUnconfirmed:saved.cleanupUnconfirmed} + } + const request=approval.parse(parse(await readProtected(values.get('approval')!,16384,digest.parse(values.get('approval-sha256'))))) + assert.equal(request.activationDirectory,directory,'Approval belongs to another activation directory') + assert.equal(before?.activationSha256??null,request.expectedActivationSha256,'Activation predecessor changed') + const registry=await readProtectedDatabaseSourceBindingRegistry(request.sourcePin,{deadlineAt:Date.now()+120000}) + const previous=before?.state.generations??[] + assert.equal(registry.head.ordinal,previous.length+1,'Source ordinal changed') + assert.equal(registry.head.previousHeadSha256,previous.at(-1)?.headSha256??null,'Signed predecessor differs') + if(before){ + assert.equal(registry.head.installationId,before.state.installationId) + assert.equal(registry.head.sourceIdentityId,before.state.sourceIdentityId) + assert.equal(registry.publicKeySpkiSha256,before.state.publicKeySpkiSha256) + } + assert(!previous.some(item=>item.generation.toLowerCase()===registry.head.generation.toLowerCase()||item.tlsSpkiSha256===request.tlsSpkiSha256),'Generation or TLS key was already used') + const next:State={format:'database-source-activation-v1',activationDirectory:directory,installationId:registry.head.installationId, + sourceIdentityId:registry.head.sourceIdentityId,publicKeySpkiSha256:registry.publicKeySpkiSha256,status:'active', + generations:[...previous,{generation:registry.head.generation,headSha256:registry.headSha256, + bindingSha256:registry.head.bindingSha256,tlsSpkiSha256:request.tlsSpkiSha256}]} + const saved=await persist(directory,next) + return {...receipt(next,saved.activationSha256,'committed'),cleanupUnconfirmed:saved.cleanupUnconfirmed} + }) +} diff --git a/backend/scripts/writer-profile/source-activation.test.mts b/backend/scripts/writer-profile/source-activation.test.mts new file mode 100644 index 00000000..8d536f4c --- /dev/null +++ b/backend/scripts/writer-profile/source-activation.test.mts @@ -0,0 +1,153 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import fs from 'node:fs/promises' +import path from 'node:path' +import os from 'node:os' +import {createHash,generateKeyPairSync,randomUUID} from 'node:crypto' +import {createRequire} from 'node:module' +import {runInNewContext} from 'node:vm' +import {provisionDatabaseSourceBindingOffline} from '../database-source-binding-operator.ts' +import * as activationRecord from '../../src/db/database-source-activation-record.ts' +import {readDatabaseSourceBindingRegistry,sourceBindingBytes,sourceBindingCanonical,type DatabaseSourceDeploymentPin} from '../../src/db/database-source-binding.ts' +import type * as Activation from './source-activation.mts' + +const require=createRequire(import.meta.url),ts=require('typescript') +const source=await fs.readFile(new URL('./source-activation.mts',import.meta.url),'utf8') +const sha=(value:Buffer)=>createHash('sha256').update(value).digest('hex') + +test('protected activation transition model preserves revocation and refuses concurrent, stale or reused generations',async()=>{ + const root=await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(),'mm-source-activation-test-'))) + try{ + // Genuine issuer signatures and filesystem files. Windows models the + // protection/lease boundary and directory fsync used on the POSIX target. + const lock=path.join(root,'test-lease'),state=path.join(root,'source-activation.json') + const files={sha,protectedDirectory:async(directory:string)=>{assert.equal(directory,root)}, + readProtected:async(filename:string,maximum:number,expected?:string)=>{ + assert.equal(path.dirname(filename),root) + const bytes=await fs.readFile(filename);assert(bytes.length<=maximum) + if(expected)assert.equal(sha(bytes),expected) + return bytes + },async withJournalLease(directory:string,work:()=>Promise){ + assert.equal(directory,root);const handle=await fs.open(lock,'wx') + try{return await work()}finally{await handle.close();await fs.unlink(lock)} + }} + let parentFailure='' + const modelFs={...fs,open:async(filename:string,flags:number,mode?:number)=>filename===root? + {sync:async()=>{if(parentFailure==='sync')throw Error('fixture directory sync failure')}, + close:async()=>{if(parentFailure==='close')throw Error('fixture directory close failure')}}:fs.open(filename,flags,mode)} + const imports:Record={'node:fs/promises':modelFs,'./operator-files.mjs':files, + './protected-source-registry.mjs':{readProtectedDatabaseSourceBindingRegistry:readDatabaseSourceBindingRegistry}, + '../../src/db/database-source-activation-record.js':activationRecord, + '../../src/db/database-source-binding.js':{sourceBindingBytes,sourceBindingCanonical}} + const load=()=>{ + const exports={} + const compiled=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText + runInNewContext(compiled,{exports,require:(name:string)=>imports[name]??require(name),Buffer,TextDecoder}) + return exports as typeof Activation + } + const {withActiveSource}=load() + let {sourceActivationMain:run}=load() + const privateKeyFile=path.join(root,'issuer.pem'),key=generateKeyPairSync('ed25519').privateKey + await fs.writeFile(privateKeyFile,key.export({format:'pem',type:'pkcs8'}),{mode:0o600}) + const installationId=randomUUID(),sourceIdentityId=randomUUID() + const issue=async(previousPin?:DatabaseSourceDeploymentPin,generation=randomUUID())=>{ + return (await provisionDatabaseSourceBindingOffline({privateKeyFile,previousPin,outputDirectory:path.join(root,randomUUID()), + request:{installationId,sourceIdentityId,generation,source:{systemIdentifier:'123',databaseOid:16384,databaseName:'synthetic', + sessionUser:'publisher',currentUser:'publisher',inRecovery:false,serverVersionNum:180004}}})).deploymentPin + } + const status=['source-status','--directory',root] + const revoke=(expected:string)=>['source-revoke','--directory',root,'--expected-sha256',expected] + const activate=async(pin:DatabaseSourceDeploymentPin,tlsSpkiSha256:string,expectedActivationSha256:string|null,activationDirectory=root)=>{ + const bytes=sourceBindingBytes({format:'database-source-activation-approval-v1',activationDirectory,sourcePin:pin,tlsSpkiSha256,expectedActivationSha256}) + const file=path.join(root,randomUUID()+'.json');await fs.writeFile(file,bytes,{mode:0o600,flag:'wx'}) + return ['source-activate','--directory',root,'--approval',file,'--approval-sha256',sha(bytes)] + } + const g1=await issue(undefined,'5ac5e8bd-0eb2-498b-901a-dd2010cd4b31'),g2=await issue(g1),g3=await issue(g2) + await assert.rejects(run(await activate(g1,'1'.repeat(64),null,path.join(root,'other'))),/another activation directory/) + const start=await activate(g1,'1'.repeat(64),null) + const races=await Promise.allSettled([run(start),run(start)]) + assert.deepEqual(races.map(value=>value.status).sort(),['fulfilled','rejected'],races.map(value=>value.status==='rejected'?String(value.reason):value.status).join(', ')) + const first=await run(status) + assert.equal(first.ordinal,1);assert.equal(first.headSha256,g1.currentHeadSha256) + assert.equal(first.generationAuthenticated,false) + const firstBytes=await fs.readFile(state) + const record=activationRecord.parseDatabaseSourceActivationRecord(firstBytes) + const [generation]=record.generations + await fs.writeFile(state,Buffer.concat([firstBytes,Buffer.from('\n')])) + await assert.rejects(run(status),/Noncanonical activation record/) + await fs.writeFile(state,sourceBindingBytes({...record,generations:[generation,{...generation, + generation:generation.generation.toUpperCase(),headSha256:'c'.repeat(64),tlsSpkiSha256:'d'.repeat(64)}]})) + await assert.rejects(run(status),/Reused activation identity/) + await fs.writeFile(state,firstBytes) + const registry=await readDatabaseSourceBindingRegistry(g1) + const evidence={format:'database-source-binding-evidence-v1' as const, + installationId:registry.head.installationId,sourceIdentityId:registry.head.sourceIdentityId,generation:registry.head.generation, + ordinal:registry.head.ordinal,bindingSha256:registry.head.bindingSha256,headSha256:registry.headSha256, + operatorSignatureVerified:true as const,currentHeadPinVerified:true as const, + genuineSourceFactsMatched:true as const,platformProtectionVerified:true as const,generationAuthenticated:false as const, + allWriterDrainAuthenticated:false as const,readyAuthorized:false as const} + // The actual preflight exercises the checked-out PostgreSQL client. The guard fixture models its source verifier. + type Verifier=Parameters[1] + const verifierArgs=[] as unknown as Parameters + const preflight={directory:root,sourcePin:g1,expectedSource:registry.body.source,expectedSha256:first.activationSha256,tlsSpkiSha256:'1'.repeat(64),deadlineAt:Date.now()+30000} + const refuseConnect=()=>{throw Error('Reached SQL connection before activation was trusted')} + const verifier={verify:async()=>evidence} + await assert.rejects(withActiveSource({...preflight,expectedSha256:'0'.repeat(64)},verifier,refuseConnect),/activation digest changed/) + await assert.rejects(withActiveSource({...preflight,tlsSpkiSha256:'2'.repeat(64)},verifier,refuseConnect),/Activation key differs/) + await assert.rejects(withActiveSource({...preflight,sourcePin:g2},verifier,refuseConnect),/differs from signed source generation/) + await assert.rejects(withActiveSource({...preflight,expectedSource:{...preflight.expectedSource,currentUser:'different'}},verifier,refuseConnect),/differs from reviewed configuration/) + let verifications=0 + const result=await withActiveSource(preflight,{verify:async()=>{verifications++;return evidence}},async guarded=>{ + assert.equal(await guarded.verify(...verifierArgs),evidence) + await assert.rejects(run(revoke(first.activationSha256)),{code:'EEXIST'}) + assert.equal(await guarded.verify(...verifierArgs),evidence) + return 'matched' + }) + assert.equal(result,'matched');assert.equal(verifications,2) + await assert.rejects(fs.access(lock),{code:'ENOENT'}) + await assert.rejects(withActiveSource(preflight,verifier,async guarded=>{ + await guarded.verify(...verifierArgs) + }),/Initial and terminal source evidence required/) + const committed=await withActiveSource(preflight,verifier,async guarded=>{ + await guarded.verify(...verifierArgs) + await guarded.verify(...verifierArgs) + await fs.writeFile(state+'.pending','interrupted',{flag:'wx'}) + return {outcome:'committed',epoch:'1'} + }) + assert.deepEqual({...committed},{outcome:'committed',epoch:'1',cleanupUnconfirmed:true}) + await fs.unlink(state+'.pending') + let reads=0 + await assert.rejects(withActiveSource(preflight,{verify:async()=>++reads===1?evidence:{...evidence,bindingSha256:'0'.repeat(64)}},async guarded=>{ + await guarded.verify(...verifierArgs) + await guarded.verify(...verifierArgs) + }),/differs from signed source generation/) + await fs.writeFile(state+'.pending','interrupted',{flag:'wx'}) + await assert.rejects(withActiveSource(preflight,verifier,refuseConnect),/Unresolved activation transition/) + await fs.unlink(state+'.pending') + await assert.rejects(run(start),/predecessor changed/) + const revoked=await run(revoke(first.activationSha256)) + await assert.rejects(withActiveSource({...preflight,expectedSha256:revoked.activationSha256},verifier,refuseConnect),/activation digest changed/) + run=load().sourceActivationMain + assert.equal((await run(status)).status,'revoked') + await assert.rejects(run(revoke(first.activationSha256)),/digest changed/) + await assert.rejects(run(await activate(g3,'3'.repeat(64),revoked.activationSha256)),/ordinal changed/) + const second=await run(await activate(g2,'2'.repeat(64),revoked.activationSha256)) + assert.equal(second.ordinal,2);assert.equal(second.status,'active') + await assert.rejects(run(await activate(g3,'1'.repeat(64),second.activationSha256)),/already used/) + const repeatedGeneration=await issue(g2,first.generation!.toUpperCase()) + await assert.rejects(run(await activate(repeatedGeneration,'3'.repeat(64),second.activationSha256)),/already used/) + await assert.rejects(run(await activate({...g3,currentHeadSha256:'0'.repeat(64)},'3'.repeat(64),second.activationSha256))) + assert.equal((await run(status)).activationSha256,second.activationSha256) + parentFailure='sync' + await assert.rejects(run(revoke(second.activationSha256)),{code:'SOURCE_ACTIVATION_TERMINAL_UNKNOWN'}) + parentFailure='close' + const observed=await run(status) + assert.equal(observed.status,'revoked') + const third=await run(await activate(g3,'3'.repeat(64),observed.activationSha256)) + assert.equal(third.outcome,'committed') + assert('cleanupUnconfirmed' in third && third.cleanupUnconfirmed===true) + await fs.writeFile(state+'.pending','interrupted',{flag:'wx'}) + await assert.rejects(run(status),/Unresolved activation transition/) + await assert.rejects(run(revoke(third.activationSha256)),/Unresolved activation transition/) + }finally{await fs.rm(root,{recursive:true,force:true})} +}) diff --git a/backend/scripts/writer-profile/source57-admission-plan.mts b/backend/scripts/writer-profile/source57-admission-plan.mts new file mode 100644 index 00000000..23f845a5 --- /dev/null +++ b/backend/scripts/writer-profile/source57-admission-plan.mts @@ -0,0 +1,48 @@ +import assert from 'node:assert/strict' +import {FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js' +import {fixedCatalogueParentPeriodData} from './application-writer-profile-catalogue-parent-policy.js' +import {reviewedMarkerFenceSql} from './marker-fence-enrollment.mjs' +import {sourceBindingBytes,sourceBindingSha256} from '../../src/db/database-source-binding.js' +import type {RegistryRoleBindingsData} from '../../src/db/application-writer-profile-registry-structure.js' + +const privileges={r:'SELECT',a:'INSERT',w:'UPDATE',d:'DELETE'} as const +const identifier=(value:unknown)=>{assert(typeof value==='string'&&/^[a-z_][a-z0-9_]{0,62}$/.test(value));return '"'+value+'"'} +const qualified=(value:unknown)=>'public.'+identifier(value) +const allowedScopes=['runtime','publisher','capture'] as const + +export function reviewedSource57Admission(roles:RegistryRoleBindingsData){ + const sql:string[]=[] + function grants(entries:unknown,suffix:string,column?:string){ + assert(Array.isArray(entries)) + for(const entry of entries){ + assert(typeof entry==='string') + if(entry==='@mm-role:operator@=arwdDxtm/@mm-role:operator@'||entry==='@mm-role:operator@=rwU/@mm-role:operator@')continue + const match=/^@mm-role:(runtime|publisher|capture)@=([rawd]+)\/@mm-role:operator@$/.exec(entry) + assert(match&&allowedScopes.includes(match[1] as typeof allowedScopes[number])) + const allowed=[...match[2]].map(key=>privileges[key as keyof typeof privileges]+(column?` (${column})`:'')).join(', ') + sql.push(`GRANT ${allowed} ON ${suffix} TO ${identifier(roles[match[1] as typeof allowedScopes[number]].name)}`) + } + } + for(const table of FIXED57_ROLE_TEMPLATE.tables){ + const name=qualified(table.relname) + grants(table.acl,'TABLE '+name) + if(table.relname==='schema_migrations')continue + if(!fixedCatalogueParentPeriodData(String(table.relname))){ + sql.push(`CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON ${name} FOR EACH ROW EXECUTE FUNCTION public.server_partition_writer_guard()`, + `ALTER TABLE ONLY ${name} ENABLE ALWAYS TRIGGER server_partition_writer_guard`) + } + sql.push(`CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON ${name} FOR EACH STATEMENT EXECUTE FUNCTION public.server_partition_writer_guard()`, + `ALTER TABLE ONLY ${name} ENABLE ALWAYS TRIGGER candidate_application_statement_guard`, + `CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON ${name} FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()`, + `ALTER TABLE ONLY ${name} ENABLE ALWAYS TRIGGER candidate_truncate_preadmitted`) + } + for(const column of FIXED57_ROLE_TEMPLATE.columns){ + if(column.acl!==null)grants(column.acl,'TABLE '+qualified(column.relname),identifier(column.attname)) + } + for(const sequence of FIXED57_ROLE_TEMPLATE.sequences)grants(sequence.acl,'SEQUENCE '+qualified(sequence.relname)) + sql.push(reviewedMarkerFenceSql) + const tables=FIXED57_ROLE_TEMPLATE.tables.map(row=>({name:row.relname,kind:row.relkind})) + const sequences=FIXED57_ROLE_TEMPLATE.sequences.map(row=>({name:row.relname})) + return {sql,tables,sequences,sha256:sourceBindingSha256(sourceBindingBytes(sql))} +} +export function source57AdmissionSqlSha256(roles:RegistryRoleBindingsData){return reviewedSource57Admission(roles).sha256} diff --git a/backend/scripts/writer-profile/source58-foundation.mts b/backend/scripts/writer-profile/source58-foundation.mts new file mode 100644 index 00000000..22fbcccc --- /dev/null +++ b/backend/scripts/writer-profile/source58-foundation.mts @@ -0,0 +1,231 @@ +import assert from 'node:assert/strict' +import {createPublicKey,verify} from 'node:crypto' +import type {PoolClient} from 'pg' +import {z} from 'zod' +import {deploymentRequest} from '../database-writer-bootstrap-operator.mjs' +import {assertJsDriverEnvironment,poolOptions,retainPublisherTlsPeer} from './operator-contract.mjs' +import {readProtected,sha} from './operator-files.mjs' +import {createProtectedDatabaseSourceBindingVerifier} from './protected-source-registry.mjs' +import {withActiveSource} from './source-activation.mjs' +import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js' +import {BUILD58_PROVENANCE} from './fixed58-build-definition.js' +import {enrollWriterRegistryInTransaction,writerRegistryEnrollmentSql} from './registry-enrollment.mjs' +import {reviewedSource57Admission} from './source57-admission-plan.mjs' +import {collectSource57Catalogue} from './catalogue.mjs' +import {canonical} from './application-writer-profile-canonical.js' +import {readProtectedRegistryStructureData} from '../../src/db/application-writer-profile-registry-structure.js' +import {captureDatabaseSourceObservation} from '../../src/db/database-source-observation.js' +import {sourceBindingBytes,sourceBindingCanonical} from '../../src/db/database-source-binding.js' +import {captureMigrationSchema} from '../../src/db/migration-schema-inventory.js' +import {MIGRATION_MANIFEST,migrationManifestDigest} from '../../src/db/migration-manifest.js' +import {SYSTEM_DATABASE_OPERATION_LOCK,SYSTEM_DATABASE_REQUEST_LOCK} from '../../src/db/operation-lock.js' + +export const SOURCE58_FOUNDATION_DOMAIN='mmapp:source58-foundation:ed25519:v1\n' +export const SOURCE57_ADMISSION_DOMAIN='mmapp:source57-admission:ed25519:v1\n' +const hash=z.string().regex(/^[a-f0-9]{64}$/) +const fields={ + bootstrapSha256:hash,sourceHeadSha256:hash,sourceTlsSpkiSha256:hash,manifestSha256:hash,definitionSha256:hash, + preSchemaSha256:hash,expiresAt:z.number().int().positive(), +} +const approval=z.object({body:z.discriminatedUnion('format',[ + z.object({...fields,format:z.literal('source58-foundation-approval-v1'),decision:z.literal('install-foundations'),foundationSha256:hash}).strict(), + z.object({...fields,format:z.literal('source57-admission-approval-v1'),decision:z.literal('install-admission'),admissionSha256:hash}).strict(), + ]), + signature:z.string().regex(/^[A-Za-z0-9_-]{86}$/)}).strict() +const parse=(bytes:Buffer)=>JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(bytes)) +type Request=ReturnType +type Sql={query:>(text:string,values?:unknown[])=>Promise<{rows:T[];command?:string}>} +const keys=['candidate_require_preadmission','candidate_truncate_preadmitted','candidate_operator_ddl','server_partition_writer_guard'] +const sorted=(rows:Record[])=>rows.map(sourceBindingCanonical).sort() +const withSortedAcl=(row:Record)=>({...row,acl:Array.isArray(row.acl)?[...row.acl].sort():row.acl}) +const identifier=(name:string)=>{assert(/^[a-z_][a-z0-9_]{0,62}$/.test(name));return '"'+name+'"'} +const bind=(text:string,roles:Request['roles'])=>text.replace(/@mm-role:(operator|control|publisher|runtime|capture)@/g,(_,scope)=>roles[scope].name) + +function reviewedFoundation(roles:Request['roles']){ + const expected=FIXED57_ROLE_TEMPLATE.functions.filter(row=>keys.includes(String(row.proname))).map(row=>JSON.parse(bind(sourceBindingCanonical(row),roles))) + const publicGuard=expected.find(row=>row.namespace==='public') + const functions=keys.slice(0,3).map(name=>expected.find(row=>row.proname===name).definition.replace('CREATE OR REPLACE FUNCTION ','CREATE FUNCTION ')) + const schemas=FIXED57_ROLE_TEMPLATE.schemas.map(row=>JSON.parse(bind(sourceBindingCanonical(row),roles))) + const events=FIXED57_ROLE_TEMPLATE.events.map(row=>JSON.parse(bind(sourceBindingCanonical(row),roles))) + const {operator,publisher,runtime,capture}=roles + const sql=[ + 'ALTER FUNCTION public.server_partition_writer_guard() SET search_path = pg_catalog', + 'REVOKE ALL ON SCHEMA public, mmapp_control FROM PUBLIC', + `GRANT USAGE ON SCHEMA public TO ${[publisher,runtime,capture].map(role=>identifier(role.name)).join(', ')}`, + `GRANT USAGE ON SCHEMA mmapp_control TO ${[operator,publisher].map(role=>identifier(role.name)).join(', ')}`, + ...functions, + `REVOKE ALL ON FUNCTION ${keys.slice(0,3).map(name=>'mmapp_control.'+name+'()').join(', ')} FROM PUBLIC`, + `GRANT EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() TO ${identifier(operator.name)}`, + 'CREATE EVENT TRIGGER candidate_operator_ddl ON ddl_command_start EXECUTE FUNCTION mmapp_control.candidate_operator_ddl()', + ] + return {expected,publicGuard,schemas,events,sql,sha256:sha(sourceBindingBytes([...sql,...writerRegistryEnrollmentSql(roles)]))} +} +export function source58FoundationSqlSha256(roles:Request['roles']){return reviewedFoundation(roles).sha256} + +const functionSql=`SELECT n.nspname AS namespace,p.proname, pg_catalog.pg_get_function_identity_arguments(p.oid) AS arguments, + l.lanname AS language,pg_catalog.pg_get_userbyid(p.proowner) AS owner,p.proconfig,p.prokind,p.prosecdef,p.proacl::text[] AS acl, + pg_catalog.pg_get_functiondef(p.oid) AS definition + FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace JOIN pg_catalog.pg_language l ON l.oid=p.prolang + WHERE (n.nspname='public' AND p.proname=$1) OR (n.nspname='mmapp_control' AND p.proname=ANY($2::text[])) LIMIT 5` +const eventSql=`SELECT e.evtname,e.evtevent,e.evtenabled,e.evttags, pg_catalog.pg_get_userbyid(e.evtowner) AS owner, + n.nspname AS function_namespace,p.proname AS function_name,n.nspname||'.'||p.proname||'()' AS function + FROM pg_catalog.pg_event_trigger e JOIN pg_catalog.pg_proc p ON p.oid=e.evtfoid + JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace LIMIT 2` + +async function checkTarget(sql:Sql,request:Request){ + const subject=await sql.query(`SELECT (pg_catalog.pg_control_system()).system_identifier::text AS system,d.oid::text AS database, + d.datname AS name,pg_catalog.current_setting('server_version_num')::int AS version,r.oid::text AS role,r.rolsuper AS superuser, + n.nspowner::text AS control_owner, pub.nspowner::text AS public_owner + FROM pg_catalog.pg_database d CROSS JOIN pg_catalog.pg_roles r + JOIN pg_catalog.pg_namespace n ON n.nspname='mmapp_control' + JOIN pg_catalog.pg_namespace pub ON pub.nspname='public' + WHERE d.datname=pg_catalog.current_database() AND r.rolname=SESSION_USER AND SESSION_USER=CURRENT_USER + AND NOT pg_catalog.pg_is_in_recovery() AND pg_catalog.current_setting('session_replication_role')='origin' + AND pg_catalog.current_setting('event_triggers')='on'`) + const {source,roles}=request + assert.deepEqual(subject.rows,[{system:source.systemIdentifier,database:source.databaseOid,name:source.databaseName,version:source.serverVersionNum, + role:roles.control.signedOid,superuser:true,control_owner:roles.control.signedOid,public_owner:roles.operator.signedOid}],'Exact authorized control DBA and namespace owners required') + const {rows}=await sql.query(`SELECT rolname AS name,oid::text AS oid FROM pg_catalog.pg_roles WHERE rolname=ANY($1::text[])`,[Object.values(roles).map(role=>role.name)]) + assert.deepEqual(sorted(rows),sorted(Object.values(roles).map(role=>({name:role.name,oid:role.signedOid}))),'Exact five role OIDs required') + const markers=await sql.query('SELECT id FROM ONLY public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1',[MIGRATION_MANIFEST.length+1]) + assert.deepEqual(markers.rows,MIGRATION_MANIFEST.map(pin=>({id:pin.file.slice(0,-4)})),'Exact current migration markers required') +} + +async function checkFresh(sql:Sql,review:ReturnType){ + const fresh=await sql.query(`SELECT NOT EXISTS(SELECT FROM pg_catalog.pg_class WHERE relnamespace='mmapp_control'::pg_catalog.regnamespace) + AND NOT EXISTS(SELECT FROM pg_catalog.pg_proc WHERE pronamespace='mmapp_control'::pg_catalog.regnamespace) + AND NOT EXISTS(SELECT FROM pg_catalog.pg_type WHERE typnamespace='mmapp_control'::pg_catalog.regnamespace) + AND NOT EXISTS(SELECT FROM pg_catalog.pg_event_trigger) + AND NOT EXISTS(SELECT FROM pg_catalog.pg_trigger WHERE tgrelid='public.schema_migrations'::pg_catalog.regclass AND NOT tgisinternal) AS empty`) + assert.deepEqual(fresh.rows,[{empty:true}],'Unenrolled staging control namespace and marker required') + const funcs=await sql.query(functionSql,[keys[3],keys.slice(0,3)]) + assert.deepEqual(funcs.rows,[{...review.publicGuard,proconfig:null,definition:review.publicGuard.definition.replace(" SET search_path TO 'pg_catalog'\n",'')}], + 'Original numbered public guard required') +} + +async function checkFoundation(sql:Sql,review:ReturnType){ + const functions=(await sql.query(functionSql,[keys[3],keys.slice(0,3)])).rows + assert.deepEqual(sorted(functions.map(withSortedAcl)),sorted(review.expected.map(withSortedAcl)),'Exact reviewed guard function readback required') + assert.deepEqual(sorted((await sql.query(eventSql)).rows),sorted(review.events),'Exact reviewed event trigger readback required') + const schemas=await sql.query(`SELECT nspname,pg_catalog.pg_get_userbyid(nspowner) AS owner,nspacl::text[] AS acl + FROM pg_catalog.pg_namespace WHERE nspname IN('public','mmapp_control')`) + assert.deepEqual(sorted(schemas.rows.map(withSortedAcl)),sorted(review.schemas.map(withSortedAcl)),'Exact reviewed schema ACL required') +} + +async function checkUnenrolledRelations(sql:Sql,plan:ReturnType,operatorOid:string){ + const tables=await sql.query(`SELECT c.relname AS name,c.relkind::text AS kind,c.relowner::text AS owner FROM pg_catalog.pg_class c + WHERE c.relnamespace='public'::pg_catalog.regnamespace AND c.relkind IN ('r','p') LIMIT $1`,[plan.tables.length+1]) + assert.deepEqual(sorted(tables.rows),sorted(plan.tables.map(row=>({...row,owner:operatorOid}))),'Exact unpartitioned staging relation inventory required') + const sequences=await sql.query(`SELECT c.relname AS name,c.relowner::text AS owner FROM pg_catalog.pg_class c + WHERE c.relnamespace='public'::pg_catalog.regnamespace AND c.relkind='S' LIMIT $1`,[plan.sequences.length+1]) + assert.deepEqual(sorted(sequences.rows),sorted(plan.sequences.map(row=>({...row,owner:operatorOid}))),'Exact staging sequence inventory required') + const {rows}=await sql.query(`SELECT NOT EXISTS(SELECT FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid + WHERE c.relnamespace='public'::pg_catalog.regnamespace AND t.tgname IN ('candidate_application_statement_guard','candidate_truncate_preadmitted')) + AND NOT EXISTS(SELECT FROM mmapp_control.application_writer_profile_history) AND NOT EXISTS(SELECT FROM mmapp_control.application_writer_profile_current) + AND pg_catalog.to_regclass('mmapp_control.database_restore_quarantine') IS NULL AS fresh`) + assert.deepEqual(rows,[{fresh:true}],'Unenrolled public relations and empty Source57 writer registry required') +} + +export async function source58FoundationMain(args:string[]){ + const flags=['bootstrap','bootstrap-sha256','publisher-transport','publisher-transport-sha256','control-transport','control-transport-sha256','ca', + 'source-registry','source-spki-sha256','activation-directory','activation-sha256','approval','approval-sha256','approval-public-key','approval-spki-sha256'] + const admission=args[0]==='source57-admission-enroll' + assert((admission||args[0]==='source58-foundation-enroll')&&args.length===1+2*flags.length);assertJsDriverEnvironment(process.env) + const values=new Map() + for(let index=1;indexvalues.get(name)! + for(const flag of flags.filter(name=>name.endsWith('sha256')))hash.parse(get(flag)) + const bootstrap=parse(await readProtected(get('bootstrap'),32768,get('bootstrap-sha256'))),request=deploymentRequest(bootstrap.deploymentRequest) + assert(bootstrap.format==='application-writer-bootstrap-v1'&&bootstrap.publicationAuthorized===false&&bootstrap.readyAuthorized===false) + const bytes=await readProtected(get('approval'),8192,get('approval-sha256')),raw=parse(bytes),{body,signature}=approval.parse(raw) + const pem=await readProtected(get('approval-public-key'),4096),key=createPublicKey(pem),sig=Buffer.from(signature,'base64url') + const sourcePin={registryDirectory:get('source-registry'),installationId:request.installationId,publicKeySpkiSha256:get('source-spki-sha256'),currentHeadSha256:request.sourceHeadSha256} + assert(!pem.includes('PRIVATE KEY')&&key.asymmetricKeyType==='ed25519'&&get('approval-spki-sha256')!==sourcePin.publicKeySpkiSha256&& + sha(key.export({type:'spki',format:'der'}))===get('approval-spki-sha256')&&sourceBindingBytes(raw).equals(bytes)&&sig.toString('base64url')===signature&& + verify(null,Buffer.from((admission?SOURCE57_ADMISSION_DOMAIN:SOURCE58_FOUNDATION_DOMAIN)+sourceBindingCanonical(body)),key,sig),'Independent signed operator approval required') + const review=reviewedFoundation(request.roles) + const relationPlan=admission?reviewedSource57Admission(request.roles):undefined + const planMatches=body.format==='source57-admission-approval-v1'? + admission&&body.definitionSha256===BUILD57_PROVENANCE.templateSha256&&body.admissionSha256===relationPlan?.sha256: + !admission&&body.definitionSha256===BUILD58_PROVENANCE.templateSha256&&body.foundationSha256===review.sha256 + assert(body.bootstrapSha256===get('bootstrap-sha256')&&body.sourceHeadSha256===sourcePin.currentHeadSha256&&body.sourceTlsSpkiSha256===request.tls.spkiSha256&& + body.manifestSha256===migrationManifestDigest()&&planMatches&&body.expiresAt>Date.now()&&body.expiresAt-Date.now()<=900000,'Exact reviewed writer plan required') + const ca=await readProtected(get('ca'),65536,request.tls.caSha256) + const transport=async(role:'publisher'|'control')=>poolOptions(parse(await readProtected(get(role+'-transport'),16384,get(role+'-transport-sha256'))),request,ca,role) + const [publisherOptions,controlOptions]=await Promise.all([transport('publisher'),transport('control')]) + const {default:pg}=await import('pg');assertJsDriverEnvironment(process.env) + const expectedSource={...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:request.roles.publisher.name,currentUser:request.roles.publisher.name} + const deadlineAt=Math.min(body.expiresAt,Date.now()+120000) + return withActiveSource({directory:get('activation-directory'),expectedSha256:get('activation-sha256'),sourcePin,expectedSource, + tlsSpkiSha256:request.tls.spkiSha256,deadlineAt},createProtectedDatabaseSourceBindingVerifier(sourcePin),async verifier=>{ + const publisher=new pg.Pool(publisherOptions),control=new pg.Pool(controlOptions) + let lost=false,commitSent=false,committed=false,cleanupUnconfirmed=false,client:PoolClient|undefined,registryStructureSha256:string|undefined,catalogueSha256:string|undefined + for(const pool of [publisher,control])pool.on('error',()=>{lost=true}) + const retained=(client:PoolClient)=>{ + const check=retainPublisherTlsPeer(client,request.tls) + return {check,sql:{query:async>(text:string,values?:unknown[])=>{ + assert(!lost&&Date.now()(text,values) + check();assert(!lost&&Date.now()result.status==='rejected') + } + assert(committed&®istryStructureSha256) + return {event:admission?'database-source57-admission':'database-source58-foundation',outcome:'committed',approvalSha256:sha(bytes), + ...(relationPlan?{admissionSha256:relationPlan.sha256,catalogueSha256}:{foundationSha256:review.sha256}), + registryStructureSha256,cleanupUnconfirmed:cleanupUnconfirmed||lost,automaticReplay:false,readyAuthorized:false} + }) +} diff --git a/backend/src/config.ts b/backend/src/config.ts index 5a44b666..cf88fc0d 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -12,6 +12,7 @@ const isProd = process.env.NODE_ENV === "production" const envSchema = z.object({ DATABASE_URL: z.string().default("postgres://mmapp:mmapp@127.0.0.1:5432/mmapp"), + DATABASE_RESTORE_URL_FILE: z.string().optional(), DATABASE_PATH: z.string().default("./mikrotik.db"), SQLITE_IMPORT_FULL_HISTORY: z.boolean().default(false), PORT: z.coerce.number().int().positive().default(8000), @@ -24,6 +25,7 @@ const envSchema = z.object({ const raw = { DATABASE_URL: process.env.DATABASE_URL, + DATABASE_RESTORE_URL_FILE: process.env.DATABASE_RESTORE_URL_FILE?.trim() || undefined, DATABASE_PATH: process.env.DATABASE_PATH, SQLITE_IMPORT_FULL_HISTORY: boolEnv(process.env.SQLITE_IMPORT_FULL_HISTORY, false), PORT: process.env.PORT, @@ -58,6 +60,7 @@ if (parsed.data.AUTH_REQUIRED && parsed.data.AUTH_JWT_SECRET.length < 8) { export const env = { DATABASE_URL: parsed.data.DATABASE_URL, + DATABASE_RESTORE_URL_FILE: parsed.data.DATABASE_RESTORE_URL_FILE, DATABASE_PATH: parsed.data.DATABASE_PATH, sqliteImportFullHistory: parsed.data.SQLITE_IMPORT_FULL_HISTORY, PORT: parsed.data.PORT, diff --git a/backend/src/index.ts b/backend/src/index.ts index aa1dc478..0ce25e33 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -4,7 +4,11 @@ import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zo import { monitorEventLoopDelay } from "node:perf_hooks" import { env } from "./config.js" import { initDatabase } from "./db/bootstrap.js" +import { mcpStateDirectory } from "./modules/mcp/security.js" +import { assertDatabaseMaintenanceStartup } from "./services/database-maintenance-startup.js" import { closePool } from "./db/index.js" +import { databaseRestoreQuarantineEnabled } from "./db/database-restore-quarantine.js" +import { checkDatabaseApiStartup } from "./modules/mcp/database-request-lease.js" import authPlugin, { requireApiAuth } from "./plugins/auth.js" import { installDatabaseRequestGate } from "./plugins/database-request-gate.js" import serversRoutes from "./routes/servers.js" @@ -21,7 +25,7 @@ import uptimeRoutes from "./routes/uptime.js" import networkRoutes from "./routes/network.js" import internetPathRoutes from "./routes/internet-path.js" import evobgpRoutes from "./routes/evobgp.js" -import probesRoutes from "./routes/probes.js" +import { createStorageProbeRoutes } from "./routes/probe-storage-handler.js" import schedulerRoutes from "./routes/scheduler.js" import sidebarCountsRoutes from "./routes/sidebar-counts.js" import alertsRoutes from "./routes/alerts.js" @@ -38,19 +42,33 @@ import greRoutes from "./routes/gre.js" import usersRoutes from "./routes/users.js" import statisticsRoutes from "./routes/statistics.js" import logsRoutes from "./routes/logs.js" +import mcpRoutes from "./routes/mcp.js" +import mcpTransport from "./routes/mcp-transport.js" +import { mcpOauth } from "./routes/mcp-oauth.js" +import { mcpPortal } from "./routes/mcp-portal.js" +import { startMcpWorkers } from "./modules/mcp/workers.js" import { refreshScheduler, stopScheduler } from "./services/scheduler.js" import { getFlowWorkerHealth, startTrafficFlowListener, stopTrafficFlowListener } from "./services/traffic-flow-ingest.js" import { agentSocket } from "./services/traffic-flow-agent-client.js" import { reconcileFlowHost, withFlowConfigurationLock } from "./services/traffic-flow-host-agent.js" import { initGeoip } from "./services/traffic-flow-geoip.js" +import { initializeAutomaticSchedulerRoot } from "./services/scheduler-automatic-root.js" + const eventLoopDelay = monitorEventLoopDelay({ resolution: 20 }) eventLoopDelay.enable() export async function buildApp(opts?: { logger?: boolean startScheduler?: boolean + onRoute?: (route: { method: string | string[]; url: string }) => void }): Promise { + if (process.env.DATABASE_RUNTIME_SCHEMA_SHA256 !== undefined) await initDatabase() + await assertDatabaseMaintenanceStartup(mcpStateDirectory(), env.DATABASE_URL) + if (databaseRestoreQuarantineEnabled() || process.env.DATABASE_RUNTIME_ACTIVATION_DIRECTORY !== undefined) { + await checkDatabaseApiStartup() + } + initializeAutomaticSchedulerRoot() // Explicit trusted bootstrap, including startScheduler:false. const usePrettyLogger = opts?.logger !== false && process.env.NODE_ENV !== "production" const app = Fastify({ @@ -74,6 +92,7 @@ export async function buildApp(opts?: { }) app.setValidatorCompiler(validatorCompiler) + if (opts?.onRoute) app.addHook("onRoute", opts.onRoute) app.setSerializerCompiler(serializerCompiler) app.setErrorHandler((error: FastifyError, request, reply) => { @@ -129,7 +148,7 @@ export async function buildApp(opts?: { await app.register(networkRoutes, { prefix: "/api" }) await app.register(internetPathRoutes, { prefix: "/api" }) await app.register(evobgpRoutes, { prefix: "/api" }) - await app.register(probesRoutes, { prefix: "/api" }) + await app.register(createStorageProbeRoutes(), { prefix: "/api" }) await app.register(schedulerRoutes, { prefix: "/api" }) await app.register(sidebarCountsRoutes, { prefix: "/api" }) await app.register(alertsRoutes, { prefix: "/api" }) @@ -146,7 +165,20 @@ export async function buildApp(opts?: { await app.register(usersRoutes, { prefix: "/api" }) await app.register(statisticsRoutes, { prefix: "/api" }) await app.register(logsRoutes, { prefix: "/api" }) + await app.register(mcpRoutes, { prefix: "/api" }) + await app.register(mcpTransport) + await app.register(mcpOauth) + await app.register(mcpPortal) + const stopMcpWorkers = opts?.startScheduler !== false + ? startMcpWorkers(() => app.log.warn("MCP: фоновая проверка не завершена; изменения не повторяются")) + : undefined + app.addHook("onClose", async () => { + await stopMcpWorkers?.() + // Settings and database maintenance can start consumers after the app has booted. + stopScheduler() + stopTrafficFlowListener() + }) if (opts?.startScheduler !== false) { await refreshScheduler() await initGeoip() @@ -155,10 +187,6 @@ export async function buildApp(opts?: { try { await withFlowConfigurationLock(reconcileFlowHost) } catch { app.log.warn("NetFlow: агент хоста не синхронизирован; проверьте состояние в настройках NetFlow") } } - app.addHook("onClose", async () => { - stopScheduler() - stopTrafficFlowListener() - }) } return app @@ -170,7 +198,7 @@ const isMain = if (isMain) { try { - await initDatabase() + if (process.env.DATABASE_RUNTIME_SCHEMA_SHA256 === undefined) await initDatabase() const app = await buildApp() let shuttingDown = false const shutdown = async (code: number) => { diff --git a/backend/src/lib/permissions.test.ts b/backend/src/lib/permissions.test.ts index 19927d8d..ed2059b8 100644 --- a/backend/src/lib/permissions.test.ts +++ b/backend/src/lib/permissions.test.ts @@ -66,4 +66,12 @@ assert.equal(permissionForRequest("POST","/api/servers/1/ospf/optimize/preview?x assert.equal(permissionForRequest("POST","/api/servers/1/ospf/optimize"),"mm:network:write") assert.equal(permissionForRequest("POST","/api/servers/1/ospf/optimize/preview/extra"),"mm:network:write") assert.equal(permissionForRequest("PUT","/api/servers/1"),"mm:servers:write") +assert.equal(hasPermission(["mm:settings:admin"], "mm:mcp:read"), true) +assert.equal(hasPermission(["mm:settings:admin"], "mm:mcp:approve"), true) +assert.equal(hasPermission(["mm:settings:admin"], "mm:mcp:pause"), true) +assert.equal(hasPermission(["mm:mcp:read"], "mm:mcp:admin"), false) +assert.equal(permissionForRequest("GET", "/api/mcp/catalog"), "mm:mcp:read") +assert.equal(permissionForRequest("POST", "/api/mcp/policy/preview"), "mm:mcp:admin") +assert.equal(permissionForRequest("GET", "/api/mcp/not-yet-implemented"), "mm:mcp:admin") +assert.equal(permissionForRequest("GET", "/api/mcp-other"), "mm:dashboard:read") console.log("permissions.test.ts: ok") diff --git a/backend/src/lib/permissions.ts b/backend/src/lib/permissions.ts index 5d35fafb..f25c91aa 100644 --- a/backend/src/lib/permissions.ts +++ b/backend/src/lib/permissions.ts @@ -10,6 +10,8 @@ export type AuthUser = { apps: string[] permissions: string[] isAdmin?: boolean + expiresAt?: number + issuedAt?: number } export function hasPermission( @@ -17,7 +19,7 @@ export function hasPermission( required: string, ): boolean { if (granted.includes(required)) return true - if (required.startsWith("mm:users:") && granted.includes("mm:settings:admin")) { + if ((required.startsWith("mm:users:") || required.startsWith("mm:mcp:")) && granted.includes("mm:settings:admin")) { return true } const parts = required.split(":") @@ -42,6 +44,15 @@ type Rule = { } const RULES: Rule[] = [ + { methods: ["GET"], match: p => /^\/api\/mcp\/oauth\/requests\//.test(p), permission: "mm:mcp:read" }, + { 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.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" }, + { methods: ["POST"], match: p => ["/api/mcp/pause", "/api/mcp/stop-automation"].includes(p), permission: "mm:mcp:pause" }, + { methods: ["GET", "POST", "PUT", "PATCH", "DELETE"], match: p => p === "/api/mcp" || p.startsWith("/api/mcp/"), permission: "mm:mcp:admin" }, { methods: ["GET", "PUT"], match: p => p === "/api/logs/settings", permission: "mm:settings:admin" }, { methods: ["GET"], match: p => p === "/api/logs" || p.startsWith("/api/logs/"), permission: "mm:servers:read" }, { methods: ["GET"], match: path => path === "/api/traffic/flow/host-files", permission: "mm:traffic:write" }, diff --git a/backend/src/modules/events/repository/events-repository.ts b/backend/src/modules/events/repository/events-repository.ts index 3dbdc63e..c978dcb0 100644 --- a/backend/src/modules/events/repository/events-repository.ts +++ b/backend/src/modules/events/repository/events-repository.ts @@ -41,9 +41,9 @@ function mapEventRow(row: typeof events.$inferSelect): EventItem { } } -export async function insertEventsBatch(items: EventInsertInput[]) { +export async function insertEventsBatch(items: EventInsertInput[], writer: Pick) { if (items.length === 0) return - await db.insert(events) + await writer.insert(events) .values( items.map((item) => ({ id: item.id, diff --git a/backend/src/modules/events/service/events-service.ts b/backend/src/modules/events/service/events-service.ts index dd02044e..1f32ffb4 100644 --- a/backend/src/modules/events/service/events-service.ts +++ b/backend/src/modules/events/service/events-service.ts @@ -7,6 +7,8 @@ import { type ListEventsQuery, } from "@mmapp/contracts/events" import { insertEventsBatch, listEvents, type EventInsertInput } from "../repository/events-repository.js" +import { executeGuardedApplicationWrite } from "../../../services/config-mutation-policy.js" +import { withPartitionWrite } from "../../../services/partition-write-transaction.js" function normalizeEventInput(input: AppendEventBody): EventInsertInput { return { @@ -24,13 +26,35 @@ function normalizeEventInput(input: AppendEventBody): EventInsertInput { } export async function appendEvent(input: AppendEventBody) { - const parsed = appendEventSchema.parse(input) - await insertEventsBatch([normalizeEventInput(parsed)]) + await appendEvents([input]) } export async function appendEvents(inputs: AppendEventBody[]) { - const rows = inputs.map((entry) => normalizeEventInput(appendEventSchema.parse(entry))) - await insertEventsBatch(rows) + const rows = prepareEventRows(inputs) + if (rows.length > 0) await withPartitionWrite(({ db }) => insertEventsBatch(rows, db)) +} + +/** Stable server-generated identities let an approved batch be verified without replay. */ +export function prepareEventRows(inputs: AppendEventBody[]): EventInsertInput[] { + return inputs.map(entry => normalizeEventInput(appendEventSchema.parse(entry))) +} + +/** Client batches additionally require application mutation policy. */ +export async function appendClientEventRows(rows: EventInsertInput[], beforeInsert?: () => Promise) { + return executeGuardedApplicationWrite({ kind: "events-journal", id: "global" }, "all", async () => { + let refusal: { error: unknown } | undefined + try { + await withPartitionWrite(async ({ db }) => { + try { await beforeInsert?.() } + catch (error) { + // Preserve the original pre-insert refusal after the owner rolls back. + refusal = { error } + throw error + } + await insertEventsBatch(rows, db) + }) + } catch (error) { throw refusal ? refusal.error : error } + }, async () => rows.map(row => ({ kind: "event", id: row.id }))) } export async function readEvents(query: Partial): Promise { diff --git a/backend/src/modules/mcp/acme-settings-actions.test.ts b/backend/src/modules/mcp/acme-settings-actions.test.ts new file mode 100644 index 00000000..45b6a587 --- /dev/null +++ b/backend/src/modules/mcp/acme-settings-actions.test.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict" +import { acmeSettingsActionSchema, ACME_CLOUDFLARE_SECRET_PURPOSE } from "./acme-settings-actions.js" +assert.deepEqual(acmeSettingsActionSchema.parse({}), { settings: {} }) +assert.deepEqual(acmeSettingsActionSchema.parse({ settings: { defaultZoneId: null }, secretRef: null }), { settings: { defaultZoneId: null }, secretRef: null }) +for (const input of [ + { cloudflareApiToken: "PLAINTEXT" }, { settings: { cloudflareApiToken: "PLAINTEXT" } }, + { secretRef: "not-a-ref" }, { secretRef: "00000000-0000-4000-8000-000000000001", clearToken: true }, + ...["https://user:pass@example.com/directory", "https://example.com/directory?secret=token", "file:///private/key", "https://example.com/directory#token"].map(directoryUrl => ({ settings: { directoryUrl } })), +]) assert.equal(acmeSettingsActionSchema.safeParse(input).success, false) +assert.equal(acmeSettingsActionSchema.safeParse({ settings: { directoryUrl: "https://directory.example/acme", defaultZoneId: "" }, secretRef: "00000000-0000-4000-8000-000000000001" }).success, true) +assert.equal(ACME_CLOUDFLARE_SECRET_PURPOSE, "acme-cloudflare:https://api.cloudflare.com/client/v4") +console.log("✓ ACME MCP schema: all public fields, reference replacement/explicit clear, no plaintext or credential-bearing URL") diff --git a/backend/src/modules/mcp/acme-settings-actions.ts b/backend/src/modules/mcp/acme-settings-actions.ts new file mode 100644 index 00000000..6cc51665 --- /dev/null +++ b/backend/src/modules/mcp/acme-settings-actions.ts @@ -0,0 +1,114 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { performAcmeSettingsUpdate, readAcmeSettingsRecord } from "../../services/acme-settings-update.js" +import { planAcmeSettingsUpdate, publicAcmeSettings } from "../../services/acme-settings-update-model.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { decryptSecret, fingerprint, privateSecretFingerprint, redact, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +/** Cloudflare credentials are sent exclusively to this provider, never the ACME directory. */ +export const ACME_CLOUDFLARE_SECRET_PURPOSE = "acme-cloudflare:https://api.cloudflare.com/client/v4" +const directory = z.string().url().max(2048).refine(value => { + try { const url = new URL(value); return ["http:","https:"].includes(url.protocol) && !url.username && !url.password && !url.hash && !url.search } catch { return false } +}, "ACME directory должен быть HTTP(S) URL без credentials/query/hash") +const fields = z.object({ directoryUrl: directory.optional(), defaultZoneId: z.string().max(4096).nullable().optional() }).strict() +export const acmeSettingsActionSchema = z.object({ settings: fields.default({}), secretRef: z.string().uuid().nullable().optional() }).strict() +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, 409) +async function authorize(input: Record, signal?: AbortSignal) { + signal?.throwIfAborted() + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const connection = await effectiveMcpConnection(principal) + if (connection.owner_id !== input.ownerId || !connection.policy.scope.allowGlobal || !hasPermission(connection.policy.permissions, "mm:certificates:write") || !hasPermission(connection.owner_permissions, "mm:certificates:write")) throw fail("Право изменения настроек ACME отозвано", "PERMISSION_DENIED") + if (fingerprint(connection.policy) !== input.policyProof) throw fail("Допуск изменился", "RESET_REQUIRED") + const ids = (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => Number(row.id)) + if (ids.length > 1000 || ids.some(id => !Number.isSafeInteger(id) || id <= 0 || !connection.policy.scope.serverIds.includes(id))) throw fail("Настройки ACME затрагивают весь каталог серверов", "SERVER_DENIED") + signal?.throwIfAborted() + return ids +} +async function reference(input: Record) { + if (!input.secretRef) return undefined + const secret = (await pool.query("SELECT CASE WHEN octet_length(purpose)<=512 THEN purpose ELSE NULL END AS purpose,server_id,CASE WHEN octet_length(ciphertext)<=16384 THEN ciphertext ELSE NULL END AS ciphertext,created_by,xmin::text AS revision FROM mcp_secrets WHERE id=$1 AND created_by=$2", [input.secretRef,input.ownerId])).rows[0] + if (!secret || typeof secret.ciphertext !== "string" || secret.server_id !== null || secret.created_by !== input.ownerId || secret.purpose !== ACME_CLOUDFLARE_SECRET_PURPOSE) throw fail("Токен не выдан владельцу для Cloudflare API", "SECRET_UNAVAILABLE") + let value: string + try { value = await decryptSecret(secret.ciphertext) } catch { throw fail("Токен Cloudflare недоступен", "SECRET_UNAVAILABLE") } + if (!value.trim() || Buffer.byteLength(value,"utf8") > 4096 || /[\r\n\u0000]/.test(value)) throw fail("Формат выданного токена Cloudflare не поддерживается", "SECRET_UNAVAILABLE") + return { value, proof: await privateSecretFingerprint(fingerprint(secret), "acme-settings-reference-v1") } +} +async function source(input: Record, signal?: AbortSignal) { + // Full scope is checked before either the settings secrets or reference are read. + const ids = await authorize(input, signal), row = await readAcmeSettingsRecord() + if (row && (!directory.safeParse(row.directoryUrl).success || typeof row.defaultZoneId !== "string" || row.defaultZoneId.length > 4096)) throw fail("Текущая конфигурация ACME не поддерживается безопасным адаптером", "STATE_UNAVAILABLE") + const busy = (await pool.query("SELECT id FROM certificate_issue_jobs WHERE status IN ('queued','running') ORDER BY id LIMIT 1")).rows.length > 0 + if (busy) throw fail("ACME занято выпуском сертификата") + const ref = await reference(input) + const protections = (await pool.query("SELECT id,xmin::text AS revision,CASE WHEN octet_length(object_ref::text)<=65536 THEN object_ref ELSE NULL END AS 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'='acme-settings' ORDER BY id LIMIT 1001", [ids.map(String)])).rows + if (protections.length > 1000 || protections.some(row=>!row.object_ref)) throw fail("Превышен предел защит ACME", "STATE_UNAVAILABLE") + if (fingerprint(await authorize(input, signal)) !== fingerprint(ids)) throw fail("Каталог серверов изменился при чтении ACME") + // Sealed plans round-trip through JSON; optional public DTO fields must not + // turn into a different source fingerprint merely because JSON drops undefined. + const state = { ...publicAcmeSettings(row), defaultZoneId: row?.defaultZoneId ?? "", updatedAt: row?.updatedAt ?? null, proof: await privateSecretFingerprint(fingerprint({ row: row ?? null, ids, protections, reference: ref?.proof ?? null }), "acme-settings-source-v2") } + return { ids, row, ref, state } +} +export function registerAcmeSettingsActions(register: typeof add) { + const current = async (input: Record) => (await source(input)).state + registerMcpService("certificates.acme.update", { current, + execute: async input => { + if (input.proofRevision !== 2 || !z.string().uuid().safeParse(input.receiptId).success) throw fail("Нет совместимого proof настроек ACME", "UNSUPPORTED_CAPABILITY") + const before = await source(input) + if (fingerprint(before.state) !== input.beforeFingerprint) throw fail("Настройки ACME, каталог или ссылка на токен изменились после подтверждения") + const patch = { ...fields.parse(input.settings), ...(input.secretRef === null ? { cloudflareApiToken: null } : before.ref ? { cloudflareApiToken: before.ref.value } : {}) } + const fence = async () => { + await assertVerifiedConfigurationAccess() + if (fingerprint((await source(input)).state) !== input.beforeFingerprint) throw fail("Настройки ACME изменились перед SQL") + await assertVerifiedConfigurationAccess() + } + let dispatched = false + try { + await fence() + const result = await performAcmeSettingsUpdate(patch, { expected: before.row ?? null, beforeWrite: async () => { await fence(); dispatched = true }, afterWrite: async () => { await assertVerifiedConfigurationAccess(); await authorize(input) } }) + const after = await source(input) + const desired = { ...planAcmeSettingsUpdate(before.row, patch, result.saved.updatedAt), revision: result.saved.revision } + if (fingerprint(after.row) !== fingerprint(result.saved) || fingerprint(desired) !== fingerprint(after.row)) return { configurationSaved: false, receiptId: input.receiptId, configurationMismatch: true, attempts: 1, automaticReplay: false } + return { configurationSaved: true, receiptId: input.receiptId, settings: publicAcmeSettings(after.row), stateProof: after.state.proof, providerTested: false, certificateIssued: false, attempts: 1, automaticReplay: false } + } catch (error) { + if (dispatched) throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Итог записи ACME не подтверждён; автоматический повтор запрещён", 502) + throw error + } + }, + verify: async (input, result) => { + const receipt = result as { configurationSaved?: boolean; stateProof?: string; receiptId?: unknown } | null + return receipt?.configurationSaved === true && receipt.receiptId === input.receiptId && (await current(input)).proof === receipt.stateProof + }, + reconcile: async (input, allow) => { + if (input.proofRevision !== 2 || !z.string().uuid().safeParse(input.receiptId).success || typeof input.beforeFingerprint !== "string") throw fail("Нет совместимого proof и checkpoint настроек ACME", "UNSUPPORTED_CAPABILITY") + await allow(); const before = await current(input) + const principal = input.principal as { connectionId: string } + const checkpoints = (await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.state='verified' AND s.intended_state=$2::jsonb AND s.result->>'configurationSaved'='true' AND s.result->>'receiptId'=$3 LIMIT 2", [principal.connectionId,JSON.stringify(redact({kind:"service",service:"certificates.acme.update",input})),input.receiptId])).rows + const receipt = checkpoints.length === 1 ? checkpoints[0].result : undefined + const desired = typeof receipt?.stateProof === "string" && /^[a-f0-9]{64}$/.test(receipt.stateProof) && receipt.stateProof === before.proof + await allow(); const after = await current(input) + if (fingerprint(after) !== fingerprint(before)) throw fail("Настройки ACME изменились во время сверки") + // Only an exact verified checkpoint can attribute the final write; an + // unknown attempt cannot adopt a later token with coincidentally equal values. + return { matchesDesiredState: desired, matchesOriginalState: fingerprint(after) === input.beforeFingerprint } + }, + }) + register("certificates.acme.update", "mm_acme_settings_update_plan", "Сохранить настройки ACME/Cloudflare с отдельной ссылкой на токен", "certificates", "mm:certificates:write", "write", "global", acmeSettingsActionSchema, { + prepare: async (input, context) => { + const request = { ...input, principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy) } + const before = await source(request, context.signal) + const objects = [{ kind: "acme-settings", id: "1" }, ...before.ids.map(serverId => ({ kind: "server", id: String(serverId), serverId }))] + const protections = 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'='acme-settings' LIMIT 1001", [before.ids.map(String)]) + if (protections.rows.length > 1000) throw fail("Превышен предел защит ACME", "STATE_UNAVAILABLE") + for (const { object_ref: ref } of protections.rows) objects.push(ref) + if (fingerprint((await source(request, context.signal)).state) !== fingerprint(before.state)) throw fail("Настройки ACME изменились при подготовке") + return { target: { scope: "global" }, objects, snapshots: [], before: before.state, requiresHuman: true, mutationCost: 1, verificationScope: "configuration", + steps: [{ kind: "service", service: "certificates.acme.update", input: { ...request, proofRevision: 2, receiptId: randomUUID(), beforeFingerprint: fingerprint(before.state) } }], + warnings: ["Глобальные directoryUrl и defaultZoneId влияют на последующие выпуски сертификатов для всех серверов. Активные/ожидающие выпуски блокируют изменение.", "Пропущенный secretRef сохраняет токен; null очищает его; выданная ссылка заменяет токен. Ссылка принадлежит владельцу и имеет purpose acme-cloudflare:https://api.cloudflare.com/client/v4.", "Изменяется только SQL-конфигурация. Нет запросов к ACME/Cloudflare, проверки токена, выпуска сертификата или изменения DNS. Существующий account key сохраняется.", "Неопределённый результат не повторяется и не подтверждается совпадением значений; требуется самостоятельная сверка человеком."] } + }, + }) +} diff --git a/backend/src/modules/mcp/acme-test-actions.ts b/backend/src/modules/mcp/acme-test-actions.ts new file mode 100644 index 00000000..037f4ca8 --- /dev/null +++ b/backend/src/modules/mcp/acme-test-actions.ts @@ -0,0 +1,64 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { cloudflareTokenTestUrl, performAcmeSettingsTest } from "../../services/acme-settings-test.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, privateSecretFingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, 409) +/** Token material is never part of the public state or service input. */ +export async function readAcmeTestSource(input: Record, signal?: AbortSignal) { + const authorize = async () => { + if (signal?.aborted) throw fail("Проверка отменена", "READ_CANCELLED") + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const current = await effectiveMcpConnection(principal) + if (current.owner_id !== input.ownerId || !current.policy.scope.allowGlobal || !hasPermission(current.policy.permissions, "mm:certificates:write") || !hasPermission(current.owner_permissions, "mm:certificates:write")) throw fail("Право проверки ACME отозвано", "PERMISSION_DENIED") + if (fingerprint(current.policy) !== input.policyProof) throw fail("Допуск изменился", "RESET_REQUIRED") + const ids = (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => row.id) + if (ids.length > 1000 || ids.some(id => !current.policy.scope.serverIds.includes(id))) throw fail("Проверка ACME требует полный каталог серверов", "SERVER_DENIED") + if (signal?.aborted) throw fail("Проверка отменена", "READ_CANCELLED") + return ids + } + const ids = await authorize() + const row = (await pool.query<{ token: string | null; updated: unknown }>(`SELECT CASE WHEN octet_length(cloudflare_api_token)<=4096 THEN cloudflare_api_token ELSE NULL END AS token, updated_at AS updated FROM acme_settings WHERE id=1`)).rows[0] + if (!row || typeof row.token !== "string" || !row.token.trim() || /[\r\n\u0000]/.test(row.token)) throw fail("Сохранённый токен Cloudflare недоступен", "SECRET_UNAVAILABLE") + if (fingerprint(await authorize()) !== fingerprint(ids)) throw fail("Каталог серверов изменился") + return { ids, token: row.token.trim(), publicState: { tokenConfigured: true, destination: cloudflareTokenTestUrl, proof: await privateSecretFingerprint(fingerprint({ row, ids }), "acme-token-test-v1") } } +} + +export function registerAcmeTestActions(register: typeof add) { + const current = async (input: Record) => (await readAcmeTestSource(input)).publicState + registerMcpService("certificates.acme.test", { + current, + execute: async input => { + const source = await readAcmeTestSource(input) + const fence = async () => { + await assertVerifiedConfigurationAccess() + if (fingerprint(await current(input)) !== input.beforeFingerprint) throw fail("Настройки ACME изменились после подтверждения") + } + await fence() + const result = await performAcmeSettingsTest({}, { readStored: async () => source.token, beforeDispatch: fence, afterDispatch: fence }) + if (result.outcome === "unknown") throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Ответ Cloudflare не подтверждён; автоматический повтор запрещён", 502) + return { providerAccepted: result.ok, outcome: result.outcome, attempts: 1, configurationChanged: false, certificateIssued: false, dnsChanged: false } + }, + verify: async (_input, result) => (result as { providerAccepted?: boolean } | null)?.providerAccepted === true, + reconcile: async (_input, authorize) => { await authorize(); return { matchesDesiredState: false, matchesOriginalState: false } }, + }) + register("certificates.acme.test", "mm_acme_settings_test_plan", "Проверить сохранённый Cloudflare token одним запросом", "certificates", "mm:certificates:write", "write", "global", z.object({}).strict(), { + prepare: async (input, context) => { + const request = { ...input, principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy) } + const source = await readAcmeTestSource(request, context.signal) + const objects = [{ kind: "acme-settings", id: "1" }, { kind: "acme-settings-test", id: "1" }, ...source.ids.map(serverId => ({ kind: "server", id: String(serverId), serverId }))] + const protections = 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' IN ('acme-settings','acme-settings-test') LIMIT 1001", [source.ids.map(String)]) + if (protections.rows.length > 1000) throw fail("Превышен предел защит ACME", "STATE_UNAVAILABLE") + for (const { object_ref: ref } of protections.rows) objects.push(ref) + if (fingerprint((await readAcmeTestSource(request, context.signal)).publicState) !== fingerprint(source.publicState)) throw fail("Настройки ACME изменились во время подготовки") + return { target: { scope: "global" }, objects, snapshots: [], before: source.publicState, requiresHuman: true, mutationCost: 1, verificationScope: "acknowledgement", + steps: [{ kind: "service", service: "certificates.acme.test", input: { ...request, beforeFingerprint: fingerprint(source.publicState) } }], + warnings: ["Один GET Cloudflare /zones с сохранённым токеном. Настройки, DNS, ACME-аккаунт и сертификаты не изменяются.", "Ответ подтверждает только доступ к Cloudflare API. Unknown не повторяется; сверка не выполняет новый запрос.", "Для проверки другого токена человек использует REST UI; модель не принимает и не получает plaintext токена."] } + }, + }) +} diff --git a/backend/src/modules/mcp/action-types.ts b/backend/src/modules/mcp/action-types.ts new file mode 100644 index 00000000..a25fa4a0 --- /dev/null +++ b/backend/src/modules/mcp/action-types.ts @@ -0,0 +1,45 @@ +import type { z } from "zod" +import type { PoolClient } from "pg" +import type { McpOperationDescriptor, McpPolicyPreviewRequest } from "@mmapp/contracts/mcp" +import type { ConnectionRow, McpPrincipal } from "./store.js" +import type { RosSnapshot, RosStep } from "./resources.js" +import type { RoutingConfirmationObservations } from "./routing-model.js" + +export interface ActionContext { + connection: ConnectionRow; principal: McpPrincipal; signal?: AbortSignal; logsFollowMaintenance?: AbortSignal + /** Process-internal persistent controller identity; never parsed from tool JSON. */ + persistentRoutingProfile?: { id: string; version: number } + /** Process-internal sink for the controller's measurement identities. */ + captureRoutingConfirmation?: (observations: RoutingConfirmationObservations) => void + /** Available only during target resolution, never retained by the read/prepare handler. */ + transactionClient?: Pick +} +export type ChangeStep = { kind: "ros"; step: RosStep } | { kind: "service"; service: string; input: Record } +export interface ChangeEvidence { + /** Explicit opt-in on typed VRF plans; adds private precondition evidence, + * never permission for native companion side effects. */ + vrfProductionAdmission?: true; + target: McpPolicyPreviewRequest["target"]; + objects: McpPolicyPreviewRequest["objects"]; + snapshots: RosSnapshot[]; + steps: ChangeStep[]; + /** Server-calculated upper bound for batched service mutations. */ + mutationCost?: number; + before?: unknown; + requiresHuman?: boolean; + verificationScope?: "configuration" | "acknowledgement"; + validUntil?: string; + observationChecks?: Array<{ id: string; srcServerId: number; dstServerId: number; srcInterface: string; dstInterface?: string; metric?: "pingAt" | "bandwidthAt"; family: string; table: string; fingerprint: string; validUntil: string }>; + ospfOwnerGraph?: { family: "ipv4" | "ipv6"; sourceSecretProofs: Array<{ serverId: number; proof: string }>; instances: Array<{ serverId: number; fingerprint: string }> }; + managementChannels?: Array<{ channelId: string; serverId: number; expiresAt: string }>; + recovery?: { step: ChangeStep; expected: unknown; validUntil: string; authorityProfileId: string }; + healthChecks?: Array<{ serverId: number; resource: string; id: string; field: "active" | "resolved" | "running" | "state" | "bfd-up"; phase?: "after"; identity?: Record; nextHopInterface?: { name: string; id: string } }>; + warnings: string[]; +} +export interface McpAction { + operation: McpOperationDescriptor; + schema: z.ZodObject; + target: (input: Record, context: ActionContext) => McpPolicyPreviewRequest["target"] | Promise; + read?: (input: Record, context: ActionContext) => Promise; + prepare?: (input: Record, context: ActionContext) => Promise; +} diff --git a/backend/src/modules/mcp/actions.ts b/backend/src/modules/mcp/actions.ts new file mode 100644 index 00000000..fc4e9b74 --- /dev/null +++ b/backend/src/modules/mcp/actions.ts @@ -0,0 +1,724 @@ +import { registerIpsecServerDeleteActions } from "./ipsec-server-delete-actions.js" +import { registerIpsecServerInitActions } from "./ipsec-server-init-actions.js" +import { registerIpsecRevisionActions } from "./ipsec-revision-actions.js" +import { registerWireguardRevisionActions } from "./wireguard-revision-actions.js" +import { registerFlowHostKeysActions } from "./flow-host-keys-actions.js" +import { registerIpsecUserCreateActions } from "./ipsec-user-create-actions.js" +import { registerFlowSettingsActions } from "./flow-settings-actions.js" +import { registerIpsecExistingCertificateExportActions } from "./ipsec-existing-certificate-export-registration.js" +import { registerIpsecUserUpdateActions } from "./ipsec-user-update-actions.js" +import { prepareIpsecNativeWrite } from "./ipsec-native-write-actions.js" +import { isIP } from "node:net" +import { setTimeout as sleep } from "node:timers/promises" +import { logsFollowCancellation } from "./logs-follow-cancellation.js" +import { z } from "zod" +import { logFiltersSchema, logSequenceInputSchema, logSettingsPatchSchema } from "@mmapp/contracts" +import { appUserCreateSchema, appUserUpdateSchema, appUserRoleSchema, interfaceCatalogStateSchema, sectionPermSchema, serverPermSchema, userBindingCreateSchema } from "@mmapp/contracts/users" +import type { McpOperationDescriptor } from "@mmapp/contracts/mcp" +import { hasPermission } from "../../lib/permissions.js" +import { assertOperationReplayScope, findOperationReplayTombstone, operationHistoryExpired } from "./operation-replay-tombstones.js" +import { pool } from "../../db/index.js" +import { getServerReadById } from "../servers/service/servers-service.js" +import { listUsers, getUserById, getInterfaceCatalogState, projectLegacyInterfaceCatalog, validateUserBindingCreate, UsersServiceError } from "../users/service/users-service.js" +import { getLogSettings } from "../../services/router-logs-collector.js" +import { readLogPage, readLogSummary, readLogContext, readLogEntry } from "../../services/router-logs-query.js" +import { logWhere } from "../../services/router-logs-model.js" +import { readBoundedLogExportRows, serializeLogExport, withLogExportSlot, LogExportLimitError, LogExportBusyError } from "../../services/router-logs-export.js" +import { assertLogExportAccess, createLogExportArtifact, registerLogArtifactActions } from "./log-artifacts.js" +import { registerAlertsOutboxActions } from "./alerts-outbox-actions.js" +import { gatewayReferencesTable, isDynamicRouteInsideTable } from "../../services/routing-gateway-reference.js" +import { readLogStream } from "../../services/router-logs-query.js" +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 { effectiveMcpConnection } from "./store.js" +import type { McpAction, ActionContext, ChangeEvidence } from "./action-types.js" +import { registerRoutingActions } from "./routing-actions.js" +import { registerApplicationActions } from "./application-actions.js" +import { registerProbeActions } from "./probe-actions.js" +import { registerServerProbeActions } from "./server-probe-registration.js" +import { registerServerPollAction } from "./server-poll-registration.js" +import { registerSpeedTestActions } from "./speed-test-actions.js" +import { registerManualApiPingActions } from "./manual-api-ping-actions.js" +import { registerManualUptimeActions } from "./manual-uptime-actions.js" +import { registerManualPingGroupActions } from "./manual-ping-group-actions.js" +import { registerManualTrafficActions } from "./manual-traffic-actions.js" +import { registerTrafficLiveActions } from "./traffic-live-actions.js" +import { registerTrafficHistoryActions } from "./traffic-history-actions.js" +import { registerSidebarCountsActions } from "./sidebar-counts-actions.js" +import { registerTrafficUserHistoryActions } from "./traffic-user-history-actions.js" +import { registerBgpExportActions } from "./bgp-export-actions.js" +import { registerBgpPrefixActions } from "./bgp-prefix-actions.js" +import { registerFilterWriteActions } from "./filter-write-actions.js" +import { registerFlowCatalogActions } from "./flow-catalog-actions.js" +import { registerFlowAnalyticsActions } from "./flow-analytics-actions.js" +import { registerFlowMapActions } from "./flow-map-actions.js" +import { registerFlowLiveActions } from "./flow-live-actions.js" +import { registerAcmeTestActions } from "./acme-test-actions.js" +import { registerAcmeSettingsActions } from "./acme-settings-actions.js" +import { registerGeoipUpdateActions } from "./geoip-update-actions.js" +import { registerBackupDownloadActions } from "./backup-download-actions.js" +import { registerFlowOverlayActions } from "./flow-overlay-actions.js" +import { registerWireguardInterfaceCreateActions } from "./wireguard-interface-create-actions.js" +import { registerWireguardPeerCreateActions } from "./wireguard-peer-create-actions.js" +import { registerHostFilesActions } from "./host-files-actions.js" +import { registerTelegramSettingsActions } from "./telegram-settings-actions.js" +import { registerTelegramTestActions } from "./telegram-test-actions.js" +import { registerOspfStateActions } from "./ospf-state-actions.js" +import { registerOspfRouteProofActions } from "./ospf-route-proof-actions.js" +import { registerLegacyOspfActions } from "./ospf-legacy-actions.js" +import { prepareWireguardInterfaceDeletion, registerWireguardInterfaceDeleteActions } from "./wireguard-interface-delete-actions.js" +import { prepareWireguardPeerUpdate, registerWireguardPeerUpdateActions } from "./wireguard-peer-update-actions.js" +import { wireguardPeerNativeUpdate } from "../../services/wireguard-peer-update.js" +import { prepareWireguardPeerDeletion, registerWireguardPeerDeleteActions } from "./wireguard-peer-delete-actions.js" +import { registerFirewallActions } from "./firewall-actions.js" +import { registerBfdMoveActions } from "./bfd-move-actions.js" +import { registerGreMutationActions } from "./gre-mutation-actions.js" +import { registerGreRevisionActions } from "./gre-revision-actions.js" +import { prepareGreNativeChange, registerGreNativeServices } from "./gre-native-actions.js" +import { registerVpnCatalogActions } from "./vpn-catalog-actions.js" +import { registerEvoBgpActions } from "./evobgp-actions.js" +import { registerRoutingProfileActions } from "./routing-profiles.js" +import { readRpfDependencies, registerRoutingControls } from "./routing-controls.js" +import { registerLifecycleActions } from "./lifecycle-actions.js" +import { registerRecoveryActions } from "./recovery-actions.js" +import { registerPathActions } from "./path-verification.js" +import { registerDatabaseRestore } from "./database-restore.js" +import { registerDatabaseBackupActions } from "./database-backup-actions.js" +import { registerTerminalDiagnosticActions } from "./terminal-diagnostic-actions.js" +import { registerEventsBatchActions } from "./events-batch-actions.js" +import { registerGlobalReconciliationActions } from "./global-reconciliation-actions.js" +import { registerWireguardInterfaceUpdateActions } from "./wireguard-interface-update-actions.js" +import { registerWireguardImportActions } from "./wireguard-import-actions.js" +import { registerWireguardExportActions } from "./wireguard-export-actions.js" +import { registerServerConnectionActions } from "./server-connection-actions.js" +import { prepareIpsecPeerDeletion, registerIpsecPeerDeleteActions } from "./ipsec-peer-delete-actions.js" +import { registerIpsecUserDeleteActions } from "./ipsec-user-delete-actions.js" +import { prepareNativeIpsecCertificateDeletion, registerIpsecCertificateDeleteActions } from "./ipsec-certificate-delete-actions.js" +import { prepareNativeIpsecPeerUpdate, registerIpsecPeerUpdateActions } from "./ipsec-peer-update-actions.js" +import { registerFlowPurgeActions } from "./flow-purge-actions.js" +import { registerFlowFactsRebuildActions } from "./flow-facts-rebuild-actions.js" +import { wireguardInterfaceNativeUpdate } from "../../services/wireguard-interface-update.js" +import { addRoutingDependencies, ospfInterfaceResources, resolveOspfInterfaceSelectors } from "./routing-dependencies.js" +import { registerConfigHistory } from "./config-history.js" +import { registerDeviceCatalogActions } from "./device-catalog-actions.js" +import { registerRecursiveRecovery } from "./recursive-recovery.js" +import { registerNetworkHostActions } from "./network-host-actions.js" +import { registerNetworkNeighborActions } from "./network-neighbor-actions.js" +import { registerIpv6NdMutationActions } from "./ipv6-nd-actions.js" +import { registerArpActions } from "./arp-actions.js" +import { registerBgpFilterBindingActions } from "./bgp-filter-binding-actions.js" +import { registerBgpCommunityPolicyActions } from "./bgp-community-policy-actions.js" +import { registerBridgeVlanActions, prepareGenericBridgeVlanChange } from "./bridge-vlan-actions.js" + +const serverId = z.number().int().positive().max(Number.MAX_SAFE_INTEGER) +const rowId = z.string().trim().min(1).max(200) +const paging = { limit: z.number().int().min(1).max(500).default(100), offset: z.number().int().min(0).max(1000000).default(0) } +const scope = (input: Record, context: ActionContext) => ({ scope: "servers" as const, serverIds: typeof input.serverId === "number" ? [input.serverId] : context.connection.policy.scope.serverIds }) +const actions: McpAction[] = [] +export function add(id: string, toolName: string, title: string, section: string, permission: string | readonly string[] | null, effect: McpOperationDescriptor["effect"], targetScope: McpOperationDescriptor["scope"], schema: z.ZodObject, handlers: Pick & Partial> & { version?: number }) { + const { version = 1, ...implementationHandlers } = handlers + const operation: McpOperationDescriptor = { + id, toolName, title, section, effect, scope: targetScope, risk: effect === "read" ? "ordinary" : "dangerous", + version, implementation: "available", requiredPermissions: typeof permission === "string" ? [permission] : permission ? [...permission] : [], + annotations: { readOnlyHint: effect === "read", destructiveHint: effect === "write", idempotentHint: effect === "read", openWorldHint: true }, + } + registerMcpOperation(operation) + actions.push({ operation, schema, target: targetScope === "servers" ? scope : () => ({ scope: targetScope }), ...implementationHandlers }) +} + +registerConfigHistory(add) +registerDeviceCatalogActions(add) +registerRecursiveRecovery(add) +registerNetworkHostActions(add) +registerNetworkNeighborActions(add) +registerIpv6NdMutationActions(add) +registerArpActions(add) +registerBgpFilterBindingActions(add) +registerBgpCommunityPolicyActions(add) +registerBridgeVlanActions(add) +registerTrafficHistoryActions(add) +registerSidebarCountsActions(add) +registerLogArtifactActions(add) +registerWireguardExportActions(add) +registerAlertsOutboxActions(add) +registerTrafficUserHistoryActions(add) +registerBgpExportActions(add) +registerBgpPrefixActions(add) +registerFilterWriteActions(add) + +for (const resource of rosResources) { + add(`ros.${resource.id}.list`, `mm_${resource.id}_list`, resource.title, resource.permission.split(":")[1], resource.permission + ":read", "read", "servers", z.object({ serverId, ...paging }).strict(), { + read: async input => { + const { client } = await routerClient(Number(input.serverId)), raw = await client.get(resource.path) + const rows = Array.isArray(raw) ? raw : [raw], offset = Number(input.offset), limit = Number(input.limit) + const page = rows.slice(offset, offset + limit) + const items = resource.id === "container" ? page.map(row => { + if (!row || typeof row !== "object" || Array.isArray(row)) return null + const { cmd: _cmd, entrypoint: _entrypoint, env: _env, ...visible } = row as Record + return visible + }) : page + return { items: redact(items), total: rows.length, nextOffset: offset + limit < rows.length ? offset + limit : null, observedAt: new Date().toISOString() } + }, + }) + if (resource.readOnly) continue + for (const action of ["create", "update", "delete"] as const) { + if (action === "create" && !resource.create || action === "delete" && !resource.remove) continue + const publicFields = resource.id === "ospf_instance" ? resource.fields.omit({ "in-filter-chain": true, "out-filter-chain": true, "out-filter-select": true }) : resource.fields + const schema = action === "create" || ["ip_settings", "ipv6_settings"].includes(resource.id) + ? z.object({ serverId, fields: publicFields.default({}), ...(resource.id === "vrf" ? { productionAdmission: z.literal(true).optional() } : {}), ...(resource.id === "bfd_configuration" ? { placeBeforeId: z.string().regex(/^\*[a-z0-9]+$/i).max(200).optional() } : {}) }).strict() + : z.object({ serverId, id: rowId.optional(), fields: publicFields.default({}), ...(resource.id === "vrf" ? { productionAdmission: z.literal(true).optional() } : {}) }).strict() + const delegated = resource.id === "wireguard" && action !== "create" || resource.id === "wireguard_peer" || resource.id === "gre" && action !== "create" || resource.id === "ipsec_peer" && action !== "create" || resource.id === "certificate" && action === "delete" || resource.id === "container" && action === "delete" + const guardsOspfInterfaceUse = action !== "create" && !delegated && ospfInterfaceResources.includes(resource.id) + const previousVersion = action === "create" ? (["ospf_template", "wireguard_peer", "wireguard", "bfd_configuration"].includes(resource.id) ? 3 : 2) : resource.id === "ipsec_peer" || resource.id === "certificate" && action === "delete" ? 2 : resource.id === "wireguard" && action === "update" ? 3 : resource.id === "ospf_template" && action !== "delete" || resource.id === "wireguard_peer" || resource.id === "wireguard" || resource.id === "ip_settings" ? 2 : 1 + add(`ros.${resource.id}.${action}`, `mm_${resource.id}_${action}_plan`, `${resource.title}: ${action}`, resource.permission.split(":")[1], resource.id === "certificate" && action === "update" ? ["mm:certificates:write", "mm:network:write"] : resource.permission + ":write", "write", "servers", schema, { + version: resource.id === "ipsec_peer" ? (action === "create" ? 5 : 3) : resource.id === "certificate" ? (action === "update" ? 4 : 3) : previousVersion + (delegated ? 0 : 1) + (resource.id === "bfd_configuration" ? 4 : resource.id === "ospf_template" ? (action === "update" ? 8 : 7) : 0) + (resource.id === "vrf" ? (action === "delete" ? 3 : 2) : 0) + (resource.id === "ospf_instance" ? 2 : 0) + (["bridge", "bridge_port", "bridge_vlan", "ip_settings", "ipv6_settings"].includes(resource.id) ? 1 : 0) + (guardsOspfInterfaceUse ? 1 : 0), + prepare: async (input, context) => { + if (resource.id === "vrf" && input.productionAdmission !== true) throw new McpError("UNSUPPORTED_CAPABILITY", "VRF требует явного productionAdmission:true для проверенного native executor; raw план недоступен", 409) + const sid = Number(input.serverId), id = input.id as string | undefined + const fields = validateRosStep(resource, action, input.fields, id) + if (resource.id === "ipsec_peer" && action === "create") return prepareIpsecNativeWrite("ros.ipsec_peer.create", sid, undefined, input.fields, context) + if (resource.id === "certificate" && action === "update") return prepareIpsecNativeWrite("ros.certificate.update", sid, id, input.fields, context) + if (resource.id === "ipsec_peer" && action === "delete") return prepareIpsecPeerDeletion(sid, String(id), context, false) + if (resource.id === "ipsec_peer" && action === "update") return prepareNativeIpsecPeerUpdate(sid, String(id), input.fields, context) + if (resource.id === "certificate" && action === "delete") return prepareNativeIpsecCertificateDeletion(sid, String(id), context) + if (resource.id === "wireguard" && action === "delete") return prepareWireguardInterfaceDeletion(sid, String(id), context, false) + if (resource.id === "wireguard_peer" && action === "delete") return prepareWireguardPeerDeletion(sid, String(id), context) + if (resource.id === "wireguard_peer" && action === "update") return prepareWireguardPeerUpdate(sid, String(id), wireguardPeerNativeUpdate(input.fields as Record), context) + if (resource.id === "wireguard" && action === "update") return findAction("mm_wireguard_interface_edit_plan").prepare!({ serverId: sid, interfaceId: String(id), patch: wireguardInterfaceNativeUpdate(input.fields as Record) }, context) + if (resource.id === "wireguard_peer" && action === "create") { + const { interface: parentReference, ...peerFields } = fields + const parents = (await readRosSnapshot(sid, "wireguard")).rows.filter(row => row[".id"] === parentReference || row.name === parentReference) + if (parents.length !== 1 || typeof parents[0].name !== "string") throw new McpError("STATE_CONFLICT", "Родитель WireGuard не найден однозначно", 409) + return findAction("mm_wireguard_peer_add_plan").prepare!({ serverId: sid, interfaceName: parents[0].name, ...wireguardPeerNativeUpdate(peerFields) }, context) + } + if (resource.id === "container" && action === "delete") return findAction("mm_container_remove_plan").prepare!({ serverId: sid, id }, context) + if (resource.id === "gre" && action !== "create") return prepareGreNativeChange(sid,action,id,input.fields) + for (const key of ["address", "dst-address"] as const) { + if (fields[key]) { + const [ip, prefix] = fields[key].split("/") + const expected = resource.id.startsWith("ipv6") ? 6 : 4 + if (isIP(ip) !== expected || prefix !== undefined && (!/^\d+$/.test(prefix) || Number(prefix) > (expected === 6 ? 128 : 32))) throw new McpError("INVALID_INPUT", "Некорректный IP-префикс", 400) + } + } + const before = await readRosSnapshot(sid, resource.id) + const current = action === "create" ? undefined : id ? before.rows.find(row => row[".id"] === id) : before.rows[0] + if (action !== "create" && !current) throw new McpError("NOT_FOUND", "Объект RouterOS не найден", 404) + if (action !== "create" && (current?.dynamic === "true" || current?.builtin === "true" || current?.default === "true")) throw new McpError("PROTECTED_OBJECT", "Системный или динамический объект нельзя изменять") + if (resource.id === "ospf_template" && action === "update" && fields["instance-id"] !== undefined) { + const previousId = current?.["instance-id"] + if (typeof previousId !== "string" || !/^\d+$/.test(previousId) || Number(previousId) > 255) { + throw new McpError("STATE_UNAVAILABLE", "RouterOS не подтвердил исходный instance-id для обратного плана", 409) + } + } + if (resource.id.endsWith("settings") && Object.keys(fields).some(key => !(key in (current ?? {})))) throw new McpError("UNSUPPORTED_CAPABILITY", "Устройство не подтвердило поддержку свойства", 409) + const references: ChangeEvidence["objects"] = [{ kind: resource.id, id: id ?? String(fields.name ?? "new"), serverId: sid }] + if (resource.id.endsWith("settings")) references.push({ kind: resource.id, id: "singleton", serverId: sid }) + const snapshots = [before] + const rpf = resource.id === "ip_settings" && fields["rp-filter"] ? await readRpfDependencies(sid, fields["rp-filter"], before.sourceProof) : undefined + if (rpf) snapshots.push(...rpf.snapshots) + if (["ospf_instance", "ospf_area", "ospf_template", "bfd_configuration", "bgp_instance", "bgp_connection", "vrf"].includes(resource.id)) await addRoutingDependencies(sid, resource.id, action, current, fields, snapshots, references) + if (["ipv4_route", "ipv6_route", "routing_rule", "bgp_connection"].includes(resource.id)) { + const tables = await readRosSnapshot(sid, "routing_table") + snapshots.push(tables) + const tableNames = new Set([current?.["routing-table"], fields["routing-table"], resource.id === "routing_rule" ? current?.table : undefined, resource.id === "routing_rule" ? fields.table : undefined].filter(value => typeof value === "string" && value.length)) + if (["ipv4_route", "ipv6_route"].includes(resource.id) && !tableNames.size) tableNames.add("main") + for (const name of tableNames) { + const table = tables.rows.find(row => row.name === name) + if (!table || table.disabled === "true") throw new McpError("STATE_CONFLICT", "Таблица маршрутизации отсутствует или отключена", 409) + references.push({ kind: "routing_table", id: String(table[".id"]), serverId: sid }) + } + } + if (resource.id === "routing_table" && action !== "create" && (action === "delete" || fields.name && fields.name !== current?.name || fields.disabled === "true")) { + // Keep exact dependency snapshots in the sealed plan: a route/rule added + // after approval must also prevent deleting or renaming its table. + const tableName = String(current?.name ?? "") + if (!tableName) throw new McpError("STATE_UNAVAILABLE", "Не подтверждено имя таблицы маршрутизации", 409) + for (const dependency of ["ipv4_route", "ipv6_route", "routing_rule", "vrf", "ospf_instance", "bgp_instance", "bgp_connection"]) { + const snapshot = await readRosSnapshot(sid, dependency) + snapshots.push(snapshot) + let inUse: boolean + try { inUse = snapshot.rows.some(row => row["routing-table"] === tableName || dependency === "routing_rule" && row.table === tableName || dependency === "vrf" && row.name === tableName || ["ipv4_route", "ipv6_route"].includes(dependency) && gatewayReferencesTable(row.gateway, [tableName, String(current?.[".id"])])) } + catch { throw new McpError("STATE_UNAVAILABLE", "Не подтверждены gateway dependencies таблицы", 409) } + if (inUse) throw new McpError("STATE_CONFLICT", "Таблица используется маршрутом, gateway, правилом, VRF или протоколом маршрутизации", 409) + } + } + // Inventory proves references and captures dependency changes between preview and apply. + const interfaceResources = ospfInterfaceResources + if ([...interfaceResources, "bridge_port", "bridge_vlan", "vrf", "ipv4_address", "ipv6_address", "ospf_template", "bfd_configuration"].includes(resource.id)) { + for (const dependency of [...interfaceResources, "bridge_port", "bridge_vlan", "ipv4_address", "ipv6_address", "vrf"]) { + if (dependency === resource.id || snapshots.some(snapshot => snapshot.resource === dependency)) continue + try { snapshots.push(await readRosSnapshot(sid, dependency)) } + catch { throw new McpError("STATE_CONFLICT", "Не удалось подтвердить зависимости интерфейса", 409) } + } + const inventory = snapshots.filter(snapshot => interfaceResources.includes(snapshot.resource)).flatMap(snapshot => snapshot.rows.map(row => ({ resource: snapshot.resource, row }))) + const interfaces = new Set(inventory.map(item => String(item.row.name))) + for (const field of ["interface", "bridge"] as const) if (fields[field] && !interfaces.has(fields[field])) throw new McpError("INVALID_INPUT", "Неизвестный родительский интерфейс", 400) + const members = ["vrf", "ospf_template", "bfd_configuration"].includes(resource.id) ? [current?.interfaces, fields.interfaces].filter((value): value is string => typeof value === "string" && (resource.id === "ospf_template" || !["all", "none"].includes(value))).flatMap(value => value.split(",").map(name => name.trim())) : [] + let resolvedOspfInterfaces: Set | undefined + if (resource.id === "ospf_template" && members.length) { + resolvedOspfInterfaces = await resolveOspfInterfaceSelectors(sid, [...new Set(members)], interfaces, snapshots, references) + } + if (resource.id === "vrf" && fields.interfaces !== undefined && (!fields.interfaces.trim() || fields.interfaces.split(",").includes("all"))) throw new McpError("INVALID_INPUT", "Выберите конкретные интерфейсы VRF или none", 400) + const referenced = [current?.interface, current?.bridge, fields.interface, fields.bridge, ...(resolvedOspfInterfaces ?? members), ...(resource.id === "bridge_vlan" ? [fields.tagged, fields.untagged].filter(Boolean).flatMap(value => value.split(",")) : [])].filter((value): value is string => typeof value === "string" && value.length > 0).map(value => value.trim()) + for (const name of new Set(referenced)) { + const parent = inventory.find(item => item.row.name === name) + if (!parent && resolvedOspfInterfaces?.has(name)) continue + if (!parent) throw new McpError("INVALID_INPUT", "Требуется явный существующий интерфейс", 400) + references.push({ kind: parent.resource, id: String(parent.row[".id"]), serverId: sid }) + } + if (resource.id === "ospf_template" && (fields.networks || current?.networks)) { + // RouterOS matches local prefixes and can use the remote endpoint on PtP links. + // Include the entire known interface/address inventory in object protection. + const included = new Set(references.map(object => `${object.kind}/${object.id}`)) + for (const snapshot of snapshots.filter(item => [...interfaceResources, "ipv4_address", "ipv6_address"].includes(item.resource))) { + for (const row of snapshot.rows) { + const nativeId = row[".id"] + if (typeof nativeId !== "string" || !nativeId) throw new McpError("STATE_CONFLICT", "Не подтверждён ID зависимости шаблона OSPF", 409) + const key = `${snapshot.resource}/${nativeId}` + if (included.has(key)) continue + if (references.length >= 1000) throw new McpError("STATE_UNAVAILABLE", "Каталог зависимостей OSPF превышает предел проверки объектов", 409) + included.add(key) + references.push({ kind: snapshot.resource, id: nativeId, serverId: sid }) + } + } + } + if (resource.id === "bridge_vlan") { + for (const iface of [fields.tagged, fields.untagged].filter(Boolean).flatMap(value => value.split(","))) if (!interfaces.has(iface.trim())) throw new McpError("INVALID_INPUT", "Неизвестный участник VLAN", 400) + if (fields.tagged?.split(",").some(iface => fields.untagged?.split(",").includes(iface))) throw new McpError("INVALID_INPUT", "Порт не может быть одновременно tagged и untagged", 400) + } + const existingName = String(current?.name ?? "") + if ((action === "delete" || fields.name && fields.name !== existingName) && existingName) { + if (guardsOspfInterfaceUse) { + const ospf = await readRosSnapshot(sid, "ospf_template", undefined, before.sourceProof) + snapshots.push(ospf, await readRosSnapshot(sid, "bfd_configuration", undefined, before.sourceProof)) + const selectors = ospf.rows.flatMap(row => typeof row.interfaces === "string" && row.interfaces.trim() + ? row.interfaces.split(",").map(name => name.trim()) : []) + if (selectors.length) { + const used = await resolveOspfInterfaceSelectors(sid, [...new Set(selectors)], interfaces, snapshots, []) + if (used.has(existingName)) throw new McpError("STATE_CONFLICT", "Интерфейс используется селектором шаблона OSPF", 409) + const rawIndex = snapshots.findIndex(snapshot => snapshot.resource === "interface_inventory") + if (rawIndex !== -1) { + // The successful rename/delete changes the same interface in this raw view. + // The typed target snapshot keeps its exact identity; lists and members stay sealed. + const [raw] = snapshots.splice(rawIndex, 1) + const matches = raw.rows.filter(row => row.name === existingName) + if (matches.length !== 1) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён ID изменяемого интерфейса OSPF", 409) + const ids = [existingName, current?.[".id"], matches[0][".id"]] + if (snapshots.find(snapshot => snapshot.resource === "interface_list_member")?.rows.some(row => ids.includes(row.interface))) { + throw new McpError("STATE_CONFLICT", "Сначала удалите интерфейс из списка RouterOS", 409) + } + } + } + } + const dependent = snapshots.filter(snapshot => ![resource.id, "interface_list", "interface_list_member"].includes(snapshot.resource)).some(snapshot => snapshot.rows.some(row => { + if (resource.id === "vrf" && ["ipv4_route", "ipv6_route"].includes(snapshot.resource) && isDynamicRouteInsideTable(row, [existingName, String(current?.[".id"])])) return false + return Object.values(row).some(value => typeof value === "string" && value.split(",").includes(existingName)) + })) + if (dependent) throw new McpError("STATE_CONFLICT", "Объект используется: сначала подготовьте явное изменение зависимостей", 409) + } + } + const linkedVlan = await prepareGenericBridgeVlanChange({ serverId: sid, resource: resource.id, action, id, fields }, context) + if (linkedVlan) { + const captured = new Map(snapshots.map(snapshot => [snapshot.resource, snapshot])) + for (const snapshot of linkedVlan.snapshots) { + const previous = captured.get(snapshot.resource) + if (previous && (previous.fingerprint !== snapshot.fingerprint || previous.sourceProof !== snapshot.sourceProof)) throw new McpError("STATE_CONFLICT", "Зависимости VLAN изменились во время подготовки", 409) + captured.set(snapshot.resource, snapshot) + } + const objects = new Map([...references, ...linkedVlan.objects].map(object => [`${object.serverId}/${object.kind}/${object.id}`, object])) + return { ...linkedVlan, snapshots: [...captured.values()], objects: [...objects.values()] } + } + const placeBeforeId = resource.id === "vrf" && action === "create" ? before.rows[0]?.[".id"] : input.placeBeforeId + if (resource.id === "vrf" && action === "create" && typeof placeBeforeId !== "string") throw new McpError("STATE_CONFLICT", "Не подтверждён порядок существующих VRF", 409) + const changesBfdMatch = action === "delete" || ["interfaces", "addresses", "address-list", "vrf", "disabled", "forbid-bfd"].some(key => fields[key] !== undefined) + const bfdOrderAnchor = resource.id === "bfd_configuration" ? (action === "create" ? placeBeforeId : changesBfdMatch ? id : undefined) : undefined + if (typeof bfdOrderAnchor === "string") { + const ids = before.rows.map(row => row[".id"]) + const position = ids.indexOf(bfdOrderAnchor) + if (position < 0 || new Set(ids).size !== ids.length) throw new McpError("STATE_CONFLICT", "Не найдена однозначная позиция правила BFD", 409) + const affected = ids.slice(action === "create" ? position : position + 1) + if (references.length + affected.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Каталог зависимостей BFD превышает предел проверки объектов", 409) + for (const id of affected) { + if (typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id)) throw new McpError("STATE_CONFLICT", "Не подтверждён ID правила BFD", 409) + references.push({ kind: resource.id, id, serverId: sid }) + } + } + const changesOspfExports = resource.id === "ospf_instance" && (fields.redistribute !== undefined || fields["originate-default"] !== undefined || fields["out-filter-chain"] !== undefined || fields["out-filter-select"] !== undefined) + const ospfExportWarnings = changesOspfExports ? [ + "Изменение может перераспределить маршруты или изменить объявление маршрута по умолчанию.", + ...((fields["out-filter-chain"] ?? current?.["out-filter-chain"]) ? ["Если источники перераспределения и originate-default не заданы, выходной фильтр может обработать все активные маршруты."] : []), + ...(["always", "if-installed"].includes(String(fields["originate-default"] ?? current?.["originate-default"])) ? ["Выходной фильтр не может отклонить маршрут по умолчанию, который сгенерировал OSPF."] : []), + ] : [] + if (resource.id === "ospf_template" && fields["instance-id"] !== undefined) ospfExportWarnings.push("Смена instance-id может разорвать соседство OSPF, если ID на обеих сторонах различаются.") + return { target: { scope: "servers", serverIds: [sid] }, objects: references, snapshots, + ...(resource.id === "vrf" && input.productionAdmission === true ? { vrfProductionAdmission: true as const } : {}), + steps: splitOspfUnsetUpdate({ serverId: sid, resource: resource.id, action, id, fields, ...(typeof placeBeforeId === "string" ? { placeBeforeId } : {}) }).map(step => ({ kind: "ros" as const, step })), + requiresHuman: true, + warnings: ["Нативное изменение может затронуть управление устройством и требует подтверждения человеком.", "Автоматическое каскадное изменение зависимостей не выполняется.", ...ospfExportWarnings, ...(resource.id === "bfd_configuration" && action === "create" && fields.addresses === undefined ? ["Правило BFD без адресного фильтра может затронуть всех соседей на указанных интерфейсах."] : []), ...(rpf?.warnings ?? []), ...(resource.id === "vrf" && action === "delete" ? ["При удалении VRF RouterOS может удалить автоматический IPv6 link-local и его connected-маршрут пустого bridge; статические адреса сохраняются."] : []), ...(placeBeforeId ? [resource.id === "bfd_configuration" ? `Правило BFD создаётся перед записью ${placeBeforeId}. Порядок влияет на выбор правила.` : `VRF создаётся перед записью ${placeBeforeId}. Порядок определяет принадлежность интерфейсов.`] : [])], + } + }, + }) + } +} + +add("routing.ospf.filter.bind", "mm_ospf_filter_binding_plan", "Привязка фильтров существующего OSPF instance", "network", ["mm:network:write", "mm:filters:read"], "write", "servers", z.object({ serverId, id: rowId, fields: ospfFilterFields.refine(fields => Object.values(fields).some(value => value !== undefined), "Укажите привязку фильтра OSPF") }).strict(), { + prepare: (input, context) => findAction("mm_ospf_instance_update_plan").prepare!(input, context), +}) + +for (const definition of [ + { id: "interfaces.update", tool: "mm_interfaces_update_plan", title: "План изменения интерфейса", resources: ["ethernet", "bridge", "bridge_port", "bridge_vlan", "vlan", "gre", "wireguard", "vxlan"] }, + { id: "firewall.update", tool: "mm_firewall_rule_update_plan", title: "План изменения правила firewall", resources: ["ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw"] }, +] as const) { + add(definition.id, definition.tool, definition.title, "network", "mm:network:write", "write", "servers", z.object({ serverId, resource: z.enum(definition.resources), id: rowId, fields: z.record(z.string().max(100), z.union([z.string().max(1000), z.number().finite(), z.boolean()])) }).strict(), { + version: definition.id === "interfaces.update" ? 5 : 2, + prepare: async (input, context) => { + const resource = resourceById(String(input.resource)), fields = resource.fields.safeParse(input.fields) + if (!fields.success) throw new McpError("INVALID_INPUT", "Поля не соответствуют типизированной схеме выбранного объекта", 400) + const handler = actions.find(action => action.operation.id === `ros.${resource.id}.update`) + if (!handler?.prepare) throw new McpError("UNSUPPORTED_CAPABILITY", "Этот объект не поддерживает изменение", 409) + return handler.prepare({ serverId: input.serverId, id: input.id, fields: fields.data }, context) + }, + }) +} + +add("capabilities.read", "mm_capabilities_get", "Доступные возможности", "mcp", null, "read", "connection", z.object({}).strict(), { + read: async (_input, context) => ({ operations: listMcpOperations().filter(operation => operation.implementation === "available" && context.connection.policy.scope.operationIds.includes(operation.id)), schemaVersion: 1 }), +}) +add("servers.list", "mm_servers_list", "Серверы", "servers", "mm:servers:read", "read", "servers", z.object({ serverId: serverId.optional(), ...paging }).strict(), { + read: async (input, context) => { + const ids = typeof input.serverId === "number" ? [input.serverId] : context.connection.policy.scope.serverIds + const { rows } = await pool.query<{ id: number }>("SELECT id FROM servers WHERE id=ANY($1::bigint[]) ORDER BY id LIMIT $2 OFFSET $3", [ids, input.limit, input.offset]) + return { items: await Promise.all(rows.map(row => getServerReadById(Number(row.id)))), observedAt: new Date().toISOString() } + }, +}) +function scopedUser(user: Awaited> | null, context: ActionContext) { + if (!user) throw new McpError("NOT_FOUND", "Пользователь не найден", 404) + const allowed = context.connection.policy.scope.serverIds + const hidden = user.bindings.some(binding => !allowed.includes(binding.serverId)) || user.servers.some(server => !allowed.includes(Number(server.serverId))) + return { ...user, bindings: user.bindings.filter(binding => allowed.includes(binding.serverId)), servers: user.servers.filter(server => allowed.includes(Number(server.serverId))), incomplete: hidden } +} +add("users.list", "mm_users_list", "Пользователи", "users", "mm:users:read", "read", "global", z.object(paging).strict(), { + read: async (input, context) => ({ items: (await listUsers()).slice(Number(input.offset), Number(input.offset) + Number(input.limit)).map(user => scopedUser(user, context)) }), +}) +add("users.get", "mm_users_get", "Пользователь по ID", "users", "mm:users:read", "read", "global", z.object({ id: rowId }).strict(), { + read: async (input, context) => ({ user: scopedUser(await getUserById(String(input.id)), context) }), +}) +const invalidInterfaceCatalog = () => new McpError("STATE_UNAVAILABLE", "Не подтверждён контекст каталога интерфейсов.", 502) +async function readMcpInterfaceCatalog(serverId: number) { + const state = await getInterfaceCatalogState(serverId).catch(error => { + if (error instanceof UsersServiceError) { + if (error.status === 404) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + if (error.status === 502) throw invalidInterfaceCatalog() + } + throw error + }) + const parsed = interfaceCatalogStateSchema.safeParse(state) + if (!parsed.success || parsed.data.serverId !== serverId) throw invalidInterfaceCatalog() + return parsed.data +} +function catalogueExceedsPublicLimits(value: unknown): boolean { + if (typeof value === "string") return value.length > MAX_MCP_PUBLIC_STRING_LENGTH + if (Array.isArray(value)) return value.length > MAX_MCP_PUBLIC_ARRAY_ITEMS || value.some(catalogueExceedsPublicLimits) + return value !== null && typeof value === "object" && Object.values(value).some(catalogueExceedsPublicLimits) +} +function intactInterfaceCatalog(result: T): T { + if (catalogueExceedsPublicLimits(result)) throw new McpError("BUDGET_EXCEEDED", "Каталог интерфейсов превышает предел ответа MCP.", 413) + return result +} +add("users.interfaces.read", "mm_user_interfaces_list", "Каталог интерфейсов для привязок пользователей", "users", "mm:users:read", "read", "servers", z.object({ serverId }).strict(), { + read: async input => intactInterfaceCatalog({ interfaces: projectLegacyInterfaceCatalog(await readMcpInterfaceCatalog(Number(input.serverId))) }), +}) +add("users.interfaces.state", "mm_user_interfaces_state_get", "Состояние каталога интерфейсов для привязок", "users", "mm:users:read", "read", "servers", z.object({ serverId }).strict(), { + read: async input => intactInterfaceCatalog(await readMcpInterfaceCatalog(Number(input.serverId))), +}) +for (const action of ["create", "update", "delete", "binding_add", "binding_remove"] as const) { + const schema = action === "create" ? z.object({ user: appUserCreateSchema.omit({ role: true, sections: true, servers: true }).strict() }).strict() + : action === "update" ? z.object({ id: rowId, user: appUserUpdateSchema.omit({ role: true, sections: true, servers: true }).strict() }).strict() + : action === "binding_add" ? z.object({ id: rowId, binding: userBindingCreateSchema }).strict() + : action === "binding_remove" ? z.object({ id: rowId, bindingId: rowId }).strict() + : z.object({ id: rowId }).strict() + add(`users.${action}`, `mm_users_${action}_plan`, `Пользователи: ${action}`, "users", "mm:users:write", "write", "global", schema, { + prepare: async (input, context) => { + const before = input.id ? await getUserById(String(input.id)) : null + if (input.id && scopedUser(before, context).incomplete) throw new McpError("SERVER_DENIED", "Есть зависимости за пределами доступа") + const user = input.user as { servers?: { serverId: string }[] } | undefined + const binding = input.binding as { serverId?: number } | undefined + const ids = [...new Set([...(user?.servers ?? []).map(server => Number(server.serverId)), ...(binding?.serverId ? [binding.serverId] : []), + ...(before?.bindings ?? []).map(binding => binding.serverId), ...(before?.servers ?? []).map(server => Number(server.serverId))])] + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Сервер не разрешён") + if (action === "binding_remove" && !before?.bindings.some(binding => binding.id === input.bindingId)) { + throw new McpError("NOT_FOUND", "Привязка не найдена", 404) + } + if (action === "binding_add") { + try { + await validateUserBindingCreate(String(input.id), userBindingCreateSchema.parse(input.binding), { signal: context.signal }) + } catch (error) { + if (error instanceof UsersServiceError) { + if (error.status === 409) throw new McpError("STATE_CONFLICT", error.message, 409) + if (error.status === 400) throw new McpError("INVALID_INPUT", error.message, 400) + if (error.status === 404) throw new McpError("NOT_FOUND", error.message, 404) + } + throw error + } + } + return { target: { scope: "global" }, objects: [{ kind: "user", id: String(input.id ?? "new") }, ...ids.map(id => ({ kind: "server", id: String(id), serverId: id }))], snapshots: [], before, + steps: [{ kind: "service", service: `users.${action}`, input }], warnings: ["Изменение /users не меняет JWT-права портала."] } + }, + }) +} + +add("users.permissions.update", "mm_users_permissions_update_plan", "Роль и права каталога /users", "users", "mm:settings:admin", "write", "global", z.object({ id: rowId, permissions: z.object({ role: appUserRoleSchema.optional(), sections: z.array(sectionPermSchema.strict()).max(100).optional(), servers: z.array(serverPermSchema.strict()).max(1000).optional() }).strict().refine(value => Object.keys(value).length > 0, "Нет изменений прав") }).strict(), { + prepare: async (input, context) => { + const before = await getUserById(String(input.id)), visible = scopedUser(before, context) + if (visible.incomplete) throw new McpError("SERVER_DENIED", "Права пользователя затрагивают скрытые серверы") + const permissions = input.permissions as { servers?: { serverId: string }[] } + const serverIds = [...new Set([...(before?.servers ?? []).map(server => Number(server.serverId)), ...(before?.bindings ?? []).map(binding => binding.serverId), ...(permissions.servers ?? []).map(server => Number(server.serverId))])] + if (serverIds.some(serverId => !Number.isSafeInteger(serverId) || !context.connection.policy.scope.serverIds.includes(serverId))) throw new McpError("SERVER_DENIED", "Назначаемые права выходят за область подключения") + if (serverIds.length && (await pool.query("SELECT id FROM servers WHERE id=ANY($1::bigint[])", [serverIds])).rows.length !== serverIds.length) throw new McpError("NOT_FOUND", "Назначаемый сервер отсутствует", 404) + return { target: { scope: "global" }, objects: [{ kind: "user", id: String(input.id) }, ...serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId }))], snapshots: [], before, requiresHuman: true, steps: [{ kind: "service", service: "users.update", input: { id: input.id, user: input.permissions } }], warnings: ["Роль и права относятся к каталогу /users и не изменяют JWT или аккаунты портала."] } + }, +}) + +const logInput = z.object({ serverId: serverId.optional(), from: z.string().datetime({ offset: true }).optional(), to: z.string().datetime({ offset: true }).optional(), + levels: z.array(z.enum(["critical", "error", "warning", "info", "debug", "unknown"])).max(6).default([]), topics: z.array(z.string().max(100)).max(30).default([]), + q: z.string().max(500).default(""), preset: z.enum(["", "login-failure", "interfaces", "routing", "firewall"]).default(""), + limit: z.number().int().min(1).max(200).default(100), cursor: z.string().max(3000).optional(), +}).strict() +function logFilters(input: Record, context: ActionContext) { + return { ...logFiltersSchema.parse({ ...input, levels: (input.levels as string[]).join(","), topics: (input.topics as string[]).join(",") }), allowedServerIds: context.connection.policy.scope.serverIds } +} +add("logs.query", "mm_logs_query", "История логов", "logs", "mm:servers:read", "read", "servers", logInput, { + read: async (input, context) => { + const generation = await accessGeneration() + let filters: ReturnType + let cursor: string | undefined, watermark: string | undefined + if (input.cursor) { + try { + const state = JSON.parse(await decryptSecret(String(input.cursor))) as { generation: string; scope: string; cursor: string; watermark: string; from: string } + filters = logFilters({ ...input, from: input.from ?? state.from }, context) + if (state.generation !== generation || state.scope !== fingerprint({ filters, connection: context.connection.id })) throw new Error("cursor") + cursor = state.cursor; watermark = state.watermark + } catch { throw new McpError("RESET_REQUIRED", "Курсор устарел или относится к другим фильтрам", 409) } + } else filters = logFilters(input, context) + const result = await readLogPage(filters, { cursor, watermark, limit: Number(input.limit) }) + return { ...result, nextCursor: result.nextCursor ? await encryptSecret(JSON.stringify({ generation, scope: fingerprint({ filters, connection: context.connection.id }), cursor: result.nextCursor, watermark: result.watermark, from: filters.from })) : null } + }, +}) +add("logs.summary", "mm_logs_summary_get", "Статистика логов", "logs", "mm:servers:read", "read", "servers", logInput, { + read: async (input, context) => { const { settings: _settings, ...summary } = await readLogSummary(logFilters(input, context)); return summary }, +}) +for (const withContext of [false, true]) add(withContext ? "logs.context" : "logs.get", withContext ? "mm_logs_context_get" : "mm_logs_get", "Запись журнала", "logs", "mm:servers:read", "read", "servers", z.object({ serverId, id: logSequenceInputSchema }).strict(), { + read: async input => { + const id = String(input.id), serverId = Number(input.serverId) + const result = await (withContext ? readLogContext(id, serverId) : readLogEntry(id, serverId)) + if (!result) throw new McpError("NOT_FOUND", "Событие не найдено", 404) + return result + }, +}) +add("logs.follow", "mm_logs_follow", "Новые события", "logs", "mm:servers:read", "read", "servers", logInput.safeExtend({ serverId, limit: logInput.shape.limit.default(200), cursor: z.string().max(8192).optional(), waitSeconds: z.number().int().min(0).max(30).default(30) }), { + read: async (input, context) => { + const maintenance = context.logsFollowMaintenance + const signal = context.signal + const waitSignal = maintenance && signal ? AbortSignal.any([maintenance, signal]) : maintenance ?? signal + const assertActive = () => { const failure = logsFollowCancellation(maintenance, signal); if (failure) throw failure } + assertActive() + const serverId = Number(input.serverId) + const until = Date.now() + Number(input.waitSeconds) * 1000 + const accessProof = fingerprint([context.connection.policy, context.connection.owner_permissions]) + let release: (() => void) | undefined + try { + const generation = await accessGeneration() + let filters: ReturnType + let cursor = "0" + if (input.cursor) { + try { + const saved = JSON.parse(await decryptSecret(String(input.cursor))) + filters = logFilters({ ...input, from: input.from ?? saved.from }, context) + if (saved.kind !== "follow" || saved.generation !== generation || saved.scope !== fingerprint({ filters, connection: context.connection.id }) || !logSequenceInputSchema.safeParse(saved.cursor).success) throw new Error("cursor") + cursor = saved.cursor + } catch { throw new McpError("RESET_REQUIRED", "Курсор follow устарел или относится к другим фильтрам", 409) } + } else { + filters = logFilters(input, context) + cursor = (await pool.query("SELECT COALESCE(max(id),0)::text AS cursor FROM router_log_entries WHERE server_id=$1", [serverId])).rows[0].cursor + } + while (true) { + assertActive() + const current = await effectiveMcpConnection(context.principal) + assertActive() + if (fingerprint([current.policy, current.owner_permissions]) !== accessProof) throw new McpError("RESET_REQUIRED", "Права или область доступа изменились; начните новое чтение", 409) + const available = await pool.query("SELECT 1 FROM servers WHERE id=$1 AND enabled=true", [serverId]) + assertActive() + if (!available.rowCount) throw new McpError("DEVICE_UNAVAILABLE", "Сервер отключён или не найден", 409) + release ??= subscribeLogDevice(serverId) + const batch = await readLogStream(filters, cursor, Number(input.limit), waitSignal) + assertActive() + cursor = batch.cursor + if (batch.entries.length || !batch.enabled || Date.now() >= until) return { ...batch, cursor: await encryptSecret(JSON.stringify({ kind: "follow", generation, scope: fingerprint({ filters, connection: context.connection.id }), cursor, from: filters.from })) } + await sleep(1000, undefined, { signal: waitSignal }) + } + } finally { release?.() } + }, +}) +add("logs.export", "mm_logs_export", "Экспорт логов", "logs", "mm:servers:read", "read", "servers", logInput.safeExtend({ format: z.enum(["csv", "json"]).default("json") }), { + read: async (input, context) => { + const where = logWhere(logFilters(input, context)) + const serverIds = input.serverId ? [Number(input.serverId)] : context.connection.policy.scope.serverIds + await assertLogExportAccess(context, serverIds) + try { + return await withLogExportSlot(async () => { + const rows = await readBoundedLogExportRows(where) + await assertLogExportAccess(context, serverIds) + const masked = rows.map(row => redact(row)) as Record[] + const format = input.format as "json" | "csv", content = serializeLogExport(masked, format, false) + return { ...await createLogExportArtifact(content, format, serverIds, context), rows: rows.length } + }) + } catch (error) { await assertLogExportAccess(context, serverIds); if (error instanceof LogExportLimitError) throw new McpError("EXPORT_LIMIT", error.message, 422); if (error instanceof LogExportBusyError) throw new McpError("EXPORT_BUSY", error.message, 429); throw error } + }, +}) +add("logs.settings.read", "mm_logs_settings_get", "Настройки логов", "logs", "mm:settings:admin", "read", "global", z.object({}).strict(), { read: async () => getLogSettings() }) +add("logs.settings.update", "mm_logs_settings_update_plan", "Изменение сборщика логов", "logs", "mm:settings:admin", "write", "global", z.object({ settings: logSettingsPatchSchema }).strict(), { + prepare: async (input, context) => { + const ids = (await pool.query<{ id: number }>("SELECT id FROM servers")).rows.map(row => Number(row.id)) + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Глобальная настройка сборщика затрагивает скрытые серверы") + const before = await getLogSettings() + const settings = logSettingsPatchSchema.parse(input.settings) + const warnings: string[] = [] + if (before.enabled && settings.enabled === false) warnings.push("После применения сборщик прекратит получать новые события.") + if (settings.retentionDays !== undefined && settings.retentionDays < before.retentionDays) { + warnings.push(`Срок хранения сокращается с ${before.retentionDays} до ${settings.retentionDays} суток. При последующей очистке удаляются записи, полученные более ${settings.retentionDays} суток назад.`) + } + return { + target: { scope: "global" }, + objects: ids.map(serverId => ({ kind: "server", id: String(serverId), serverId })), + snapshots: [], before, steps: [{ kind: "service", service: "logs.settings.update", input }], warnings, + } + }, +}) + +export function listActions(): readonly McpAction[] { return actions } +export function findAction(toolName: string): McpAction { + const action = actions.find(candidate => candidate.operation.toolName === toolName) + if (!action) throw new McpError("UNKNOWN_OPERATION", "Инструмент не реализован", 404) + return action +} +export function findActionByOperation(id: string): McpAction { + const action = actions.find(candidate => candidate.operation.id === id) + if (!action) throw new McpError("UNKNOWN_OPERATION", "Операция не реализована", 404) + return action +} + +async function assertPlanReadScope(plan: Awaited>, context: ActionContext) { + if (plan.connection_id !== context.connection.id) throw new McpError("NOT_FOUND", "План не найден", 404) + if (!context.connection.policy.scope.operationIds.includes(plan.operation_id)) throw new McpError("OPERATION_DENIED", "Исходное действие не входит в текущий допуск") + const source = findActionByOperation(plan.operation_id) + if (source.operation.requiredPermissions.some(permission => !hasPermission(context.connection.policy.permissions, permission) || !hasPermission(context.connection.owner_permissions, permission))) throw new McpError("PERMISSION_DENIED", "Право исходного действия отозвано") + const evidence = JSON.parse(await decryptSecret(plan.evidence.sealed)) as ChangeEvidence + const ids = [...(evidence.target.scope === "servers" ? evidence.target.serverIds : []), ...evidence.objects.flatMap(object => object.serverId === undefined ? [] : [object.serverId])] + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id)) || evidence.target.scope === "global" && !context.connection.policy.scope.allowGlobal) throw new McpError("SERVER_DENIED", "План находится вне текущего допуска") +} +add("changes.read", "mm_change_get", "Просмотр своего плана", "mcp", null, "read", "connection", z.object({ planId: z.string().uuid() }).strict(), { + read: async (input, context) => { + const { loadPlan, publicPlan } = await import("./runtime.js") + const plan = await loadPlan(String(input.planId)) + await assertPlanReadScope(plan, context) + return publicPlan(plan) + }, +}) +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.cancel", "mm_change_cancel", "Отменить свой незапущенный план", "mcp", null, "read", "connection", z.object({ planId: z.string().uuid() }).strict(), { + read: async (input, context) => { + const { transaction, audit } = await import("./store.js") + await assertPlanReadScope(await (await import("./runtime.js")).loadPlan(String(input.planId)), context) + return transaction(async client => { + const { rows } = await client.query("UPDATE mcp_plans SET state='cancelled' WHERE id=$1 AND connection_id=$2 AND state IN ('draft','awaiting-approval','approved') RETURNING id", [input.planId, context.connection.id]) + if (!rows.length) throw new McpError("STATE_CONFLICT", "План не найден или уже запущен", 409) + await audit(client, context.connection.owner_id, "plan.cancel", context.connection.id, { planId: input.planId }) + return { cancelled: true } + }) + }, +}) +add("operations.read", "mm_operation_get", "Результат своей операции", "mcp", null, "read", "connection", z.object({ operationId: z.string().uuid() }).strict(), { + read: async (input, context) => { + const { rows: [operation] } = await pool.query("SELECT * FROM mcp_operations WHERE id=$1 AND connection_id=$2", [input.operationId, context.connection.id]) + if (!operation) { + const tombstone = await findOperationReplayTombstone(pool, context.connection.id, String(input.operationId)) + if (!tombstone) throw new McpError("NOT_FOUND", "Операция не найдена", 404) + assertOperationReplayScope(tombstone, context.connection) + operationHistoryExpired() + } + await assertPlanReadScope(await (await import("./runtime.js")).loadPlan(operation.plan_id), context) + const { rows: steps } = await pool.query("SELECT * FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [input.operationId]) + return { ...operation, steps } + }, +}) + +registerRoutingActions(add) +registerRoutingProfileActions(add) +registerRoutingControls(add) +registerLifecycleActions(add) +registerRecoveryActions(add) +registerPathActions(add) +registerApplicationActions(add) +registerProbeActions(add) +registerServerProbeActions(add) +registerServerPollAction(add) +registerSpeedTestActions(add) +registerManualApiPingActions(add) +registerManualUptimeActions(add) +registerManualPingGroupActions(add) +registerManualTrafficActions(add) +registerTrafficLiveActions(add) +registerFlowCatalogActions(add) +registerFlowAnalyticsActions(add) +registerFlowMapActions(add) +registerFlowLiveActions(add) +registerAcmeTestActions(add) +registerAcmeSettingsActions(add) +registerGeoipUpdateActions(add) +registerBackupDownloadActions(add) +registerFlowOverlayActions(add) +registerWireguardInterfaceCreateActions(add) +registerWireguardPeerCreateActions(add) +registerHostFilesActions(add) +registerTelegramSettingsActions(add) +registerTelegramTestActions(add) +registerOspfStateActions(add) +registerOspfRouteProofActions(add) +registerLegacyOspfActions(add) +registerWireguardPeerDeleteActions(add) +registerWireguardPeerUpdateActions(add) +registerWireguardInterfaceDeleteActions(add) +registerFirewallActions(add) +registerBfdMoveActions(add) +registerGreMutationActions(add) +registerGreRevisionActions(add) +registerGreNativeServices() +registerVpnCatalogActions(add) +registerEvoBgpActions(add) +registerDatabaseRestore(add) +registerDatabaseBackupActions(add) +registerTerminalDiagnosticActions(add) +registerEventsBatchActions(add) +registerGlobalReconciliationActions(add) +registerWireguardInterfaceUpdateActions(add) +registerWireguardImportActions(add) +registerServerConnectionActions(add) +registerIpsecPeerDeleteActions(add) +registerIpsecUserDeleteActions(add) +registerIpsecUserUpdateActions(add) +registerIpsecServerDeleteActions(add) +registerIpsecServerInitActions(add) +registerIpsecRevisionActions(add) +registerWireguardRevisionActions(add) +registerIpsecCertificateDeleteActions(add) +registerIpsecPeerUpdateActions(add) +registerFlowPurgeActions(add) +registerFlowFactsRebuildActions(add) +registerFlowHostKeysActions(add) +registerIpsecUserCreateActions(add) +registerFlowSettingsActions(add) +registerIpsecExistingCertificateExportActions(add) diff --git a/backend/src/modules/mcp/administration-admission-acceptance.ts b/backend/src/modules/mcp/administration-admission-acceptance.ts new file mode 100644 index 00000000..70ce5cb7 --- /dev/null +++ b/backend/src/modules/mcp/administration-admission-acceptance.ts @@ -0,0 +1,172 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { readdir, readFile } from "node:fs/promises" +import path from "node:path" +import { SignJWT } from "jose" +import type { PoolClient } from "pg" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import { SYSTEM_DATABASE_OPERATION_LOCK, withOperationLock } from "../../db/operation-lock.js" +import type { AuthUser } from "../../lib/permissions.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "../../services/partition-write-transaction.js" +import { withApplicationCollectorLock } from "../../services/config-mutation-lock.js" +import { buildApp } from "../../index.js" +import { createMcpConnection, setConnectionStatus } from "./store.js" +import { decryptBinary, decryptSecret, mcpStateDirectory } from "./security.js" + +async function loseArchiveCommitAcknowledgement(actorId: string, upload: () => PromiseLike) { + const originals = new Map() + let commitCount = 0 + const disarm = (_error: Error | undefined, client: PoolClient) => { + const query = originals.get(client) + if (query) { + client.query = query + originals.delete(client) + } + } + const arm = (client: PoolClient) => { + const query = client.query + originals.set(client, query) + let archiveAudit = false + client.query = ((...args: unknown[]) => { + const [statement, values] = args + if (statement === "INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)" && + Array.isArray(values) && values[0] === actorId && values[1] === "database.archive.upload") archiveAudit = true + const result = Reflect.apply(query, client, args) + if (statement !== "COMMIT" || !archiveAudit) return result + return Promise.resolve(result).then(commit => { + assert.equal(commit.command, "COMMIT") + commitCount++ + if (commitCount === 1) throw new Error("Owned fixture lost the archive COMMIT acknowledgement") + return commit + }) + }) as PoolClient["query"] + } + pool.on("acquire", arm) + pool.on("release", disarm) + try { + const result = await upload() + assert.equal(commitCount, 1, "The upload must issue exactly one COMMIT") + return result + } finally { + pool.off("acquire", arm) + pool.off("release", disarm) + for (const client of originals.keys()) disarm(undefined, client) + } +} + +export async function verifyAdministrationAdmission(actor: AuthUser) { + const app = await buildApp({ startScheduler: false, logger: false }) + try { + const reader = { ...actor, permissions: [...actor.permissions, "mm:servers:read"] } + const { connection } = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned admin admission", mode: "observer", + expiresAt: new Date(Date.now() + 600_000).toISOString(), permissions: ["mm:servers:read"], + scope: { serverIds: [], operationIds: ["logs.query"], allowGlobal: true } }), reader) + await setConnectionStatus(connection.id, "paused", reader) + const profiles = [randomUUID(), randomUUID()] + await pool.query(`INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at) + SELECT id,$2,'{}'::jsonb,true,now()+interval '10 minutes' FROM unnest($1::uuid[]) AS p(id)`, [profiles, connection.id]) + const token = await new SignJWT({ sub: reader.id, apps: reader.apps, permissions: reader.permissions }) + .setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime("15m").setIssuer(process.env.AUTH_ISSUER!) + .sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET!)) + const post = (path: string, payload?: object) => app.inject({ method: "POST", url: `/api/mcp${path}`, + headers: { authorization: `Bearer ${token}` }, payload }) + const check = () => post(`/connections/${connection.id}/check`) + const pause = () => post(`/routing-profiles/${profiles[0]}/pause`) + const stop = () => post("/stop-automation") + const secretValue = "owned-admission-synthetic-secret" + const secret = () => post("/secrets", { purpose: "owned-admission-fixture", value: secretValue }) + const snapshot = () => pool.query(`SELECT (SELECT enabled FROM mcp_settings WHERE id=1) AS enabled, + (SELECT count(*)::int FROM mcp_routing_profiles WHERE enabled) AS active, + (SELECT count(*)::int FROM mcp_secrets) AS secrets, + (SELECT coalesce(jsonb_agg(action ORDER BY id),'[]') FROM mcp_audit WHERE actor_id=$1) AS audit`, [actor.id]).then(result => result.rows[0]) + const before = await snapshot() + assert(before.enabled && before.active === 2 && before.secrets === 0) + await withOperationLock(() => pool.connect(), SYSTEM_DATABASE_OPERATION_LOCK, async () => { + for (const response of await Promise.all([check(), pause(), stop()])) { + assert.equal(response.statusCode, 503) + assert.equal(response.json().code, "PROTOCOL_STORAGE_BUSY") + } + assert.deepEqual(await snapshot(), before) + }) + await withOperationLock(() => pool.connect(), PARTITION_WRITER_ADVISORY_KEY, async () => { + assert.equal((await secret()).statusCode, 503) + assert.deepEqual(await snapshot(), before) + const checked = await check() + assert.equal(checked.statusCode, 200) + assert.equal(checked.json().protocolProbe.status, "skipped") + assert.equal((await pause()).statusCode, 200) + assert.equal((await snapshot()).active, 1) + assert.equal((await stop()).statusCode, 200) + }) + const stored = await secret() + assert.equal(stored.statusCode, 200) + const { rows: [record] } = await pool.query("SELECT ciphertext FROM mcp_secrets WHERE id=$1", [stored.json().secretRef]) + assert.notEqual(record.ciphertext, secretValue) + assert.equal(await decryptSecret(record.ciphertext), secretValue) + const after = await snapshot() + assert.deepEqual([after.enabled, after.active, after.secrets], [false, 0, 1]) + assert.deepEqual(after.audit, [...before.audit, "connection.check", "controller.pause", "automation.emergency-stop", "secret.create"]) + + const vault = path.join(mcpStateDirectory(), "restore-vault") + const files = async () => (await readdir(vault).catch((error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT") return [] + throw error + })).sort() + const originalFiles = await files() + const dump = Buffer.from("PGDMP owned archive admission fixture") + const upload = () => app.inject({ method: "POST", url: "/api/mcp/database/archives", + headers: { authorization: `Bearer ${token}`, "content-type": "application/octet-stream" }, payload: dump }) + for (const key of [SYSTEM_DATABASE_OPERATION_LOCK, PARTITION_WRITER_ADVISORY_KEY]) { + await withOperationLock(() => pool.connect(), key, async () => { + const blocked = await upload() + assert.equal(blocked.statusCode, 503) + assert.equal(blocked.json().code, "PROTOCOL_STORAGE_BUSY") + assert.deepEqual(await files(), originalFiles) + assert.deepEqual(await snapshot(), after) + }) + } + await withApplicationCollectorLock(async () => { + assert.equal((await upload()).statusCode, 503) + assert.deepEqual(await files(), originalFiles) + assert.deepEqual(await snapshot(), after) + }) + await pool.query("ALTER TABLE mcp_audit ADD CONSTRAINT owned_archive_audit_refusal CHECK (action <> 'database.archive.upload') NOT VALID") + try { + assert.equal((await upload()).statusCode, 503) + assert.deepEqual(await files(), originalFiles) + assert.deepEqual(await snapshot(), after) + } finally { + await pool.query("ALTER TABLE mcp_audit DROP CONSTRAINT owned_archive_audit_refusal") + } + const uploaded = await upload() + assert.equal(uploaded.statusCode, 200) + const receipt = uploaded.json() + assert.equal(receipt.sha256, createHash("sha256").update(dump).digest("hex")) + assert.equal(receipt.sizeBytes, dump.length) + assert(!(await readFile(path.join(vault, receipt.archiveRef + ".archive.bin"))).includes(dump)) + const uploads = async () => (await pool.query("SELECT payload FROM mcp_audit WHERE actor_id=$1 AND action='database.archive.upload' ORDER BY id", [actor.id])).rows + assert.deepEqual(await uploads(), [{ payload: receipt }]) + + const beforeLostAck = await files() + const responseWithoutReceipt = await loseArchiveCommitAcknowledgement(actor.id, upload) + assert.equal(responseWithoutReceipt.statusCode, 503) + assert.equal(responseWithoutReceipt.json().code, "AUDIT_UNAVAILABLE") + const recordedUploads = await uploads() + assert.equal(recordedUploads.length, 2) + assert.deepEqual(recordedUploads[0], { payload: receipt }) + const retained = recordedUploads[1].payload + assert(!responseWithoutReceipt.body.includes(retained.archiveRef)) + assert.equal(retained.sha256, receipt.sha256) + assert.equal(retained.sizeBytes, dump.length) + assert(Date.parse(retained.expiresAt) > Date.now()) + assert.deepEqual(await files(), [...beforeLostAck, retained.archiveRef + ".archive.bin", retained.archiveRef + ".archive.json"].sort()) + const ciphertext = await readFile(path.join(vault, retained.archiveRef + ".archive.bin")) + assert(!ciphertext.includes(dump)) + assert.deepEqual(await decryptBinary(ciphertext), dump) + const metadata = JSON.parse(await readFile(path.join(vault, retained.archiveRef + ".archive.json"), "utf8")) + assert.deepEqual({ archiveRef: metadata.id, sha256: metadata.sha256, sizeBytes: metadata.sizeBytes, expiresAt: metadata.expiresAt }, retained) + console.log("MCP restore archive: one committed audit and encrypted archive retained after lost COMMIT acknowledgement") + console.log("MCP administration admission: audit, pause, emergency stop, secret storage and restore archive upload passed") + } finally { await app.close() } +} diff --git a/backend/src/modules/mcp/alerts-outbox-actions.ts b/backend/src/modules/mcp/alerts-outbox-actions.ts new file mode 100644 index 00000000..85bcd5e8 --- /dev/null +++ b/backend/src/modules/mcp/alerts-outbox-actions.ts @@ -0,0 +1,60 @@ +import { z } from "zod" +import { alertsOutboxRetrySchema, alertsOutboxRetryReceiptSchema } from "@mmapp/contracts/alerts" +import { pool } from "../../db/index.js" +import { readOutboxRetrySource, retryUnknownOutbox, OutboxRetrySourceUnavailable, type OutboxRetrySource } from "../../services/alert-engine/outbox.js" +import { OutboxRetryConflict } from "../../services/alerts-outbox-retry.js" +import { privateSecretFingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const operation = "alerts.outbox.retry" +const schema = alertsOutboxRetrySchema.safeExtend({ id: z.string().min(1).max(200).refine(value => value === value.trim() && !/[\u0000-\u001f\u007f]/.test(value)) }) +const sourceProof = (source: OutboxRetrySource) => privateSecretFingerprint(JSON.stringify(source), "alerts-outbox-retry") +async function dependencyIds() { + const serverIds = (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => Number(row.id)) + if (serverIds.length > 999) throw new McpError("STATE_UNAVAILABLE", "Каталог зависимостей очереди превышает 999 серверов", 409) + return serverIds +} +async function current(input: Record) { + const serverIds = await dependencyIds() + const permitted = input.serverIds + if (Array.isArray(permitted) && serverIds.some(id => !permitted.includes(id))) throw new McpError("SERVER_DENIED", "Глобальная очередь содержит зависимости вне области плана", 403) + let source: OutboxRetrySource | null + try { source = await readOutboxRetrySource(String(input.id)) } + catch (error) { if (error instanceof OutboxRetrySourceUnavailable) throw new McpError("STATE_UNAVAILABLE", error.message, 409); throw error } + return { serverIds, value: source ? { id: source.id, channel: source.channel, status: source.status, resolutionVersion: source.resolution_version, proof: await sourceProof(source) } : null } +} + +export function registerAlertsOutboxActions(register: typeof add) { + registerMcpService(operation, { + current, + execute: async input => { + const retry = alertsOutboxRetrySchema.parse({ expectedVersion: input.expectedVersion, acknowledgedDuplicateRisk: input.acknowledgedDuplicateRisk }) + try { + return await retryUnknownOutbox(String(input.id), retry, async source => { + if (typeof input.sourceProof !== "string" || await sourceProof(source) !== input.sourceProof) throw new McpError("STATE_CONFLICT", "Запись уведомления изменилась после подтверждения плана", 409) + if (JSON.stringify(await dependencyIds()) !== JSON.stringify(input.serverIds)) throw new McpError("STATE_CONFLICT", "Каталог серверов изменился после подтверждения плана", 409) + }) + } catch (error) { + if (error instanceof OutboxRetryConflict) throw new McpError("STATE_CONFLICT", error.message, 409) + if (error instanceof OutboxRetrySourceUnavailable) throw new McpError("STATE_UNAVAILABLE", error.message, 409) + throw error + } + }, + verify: async (input, result) => { + const receipt = alertsOutboxRetryReceiptSchema.safeParse(result) + return receipt.success && receipt.data.id === input.id && receipt.data.resolutionVersion === Number(input.expectedVersion) + 1 + }, + }) + register(operation, "mm_alerts_outbox_retry_plan", "Повтор уведомления с неизвестным результатом", "alerts", "mm:alerts:write", "write", "global", schema, { + prepare: async (input, context) => { + const before = await current({ ...input, serverIds: context.connection.policy.scope.serverIds }) + if (!before.value) throw new McpError("NOT_FOUND", "Запись очереди не найдена", 404) + if (before.value.channel !== "telegram" || before.value.status !== "unknown" || before.value.resolutionVersion !== input.expectedVersion) throw new McpError("STATE_CONFLICT", "Обновите очередь: повтор разрешён только для unknown с текущей версией", 409) + if (before.serverIds.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Глобальная очередь требует доступа ко всему каталогу серверов", 403) + return { target: { scope: "global" }, objects: [{ kind: "alerts-outbox", id: String(input.id) }, ...before.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId }))], + snapshots: [], before, steps: [{ kind: "service", service: operation, input: { ...input, sourceProof: before.value.proof, serverIds: before.serverIds } }], requiresHuman: true, + verificationScope: "acknowledgement", warnings: ["Предыдущее уведомление могло быть доставлено. Повтор может создать дубликат у получателя; требуется отдельное подтверждение человеком.", "Подтверждается только повторная постановка в очередь. Фоновый отправитель может отправить сообщение после выполнения плана; доставка получателю не подтверждается.", "Изменение записи или каталога серверов инвалидирует план. Текст и адресат остаются в исходной записи и не передаются нейросети."] } + }, + }) +} diff --git a/backend/src/modules/mcp/application-actions.ts b/backend/src/modules/mcp/application-actions.ts new file mode 100644 index 00000000..7c1c63c3 --- /dev/null +++ b/backend/src/modules/mcp/application-actions.ts @@ -0,0 +1,278 @@ +import { z } from "zod" +import { geoipSettingsPatchSchema } from "@mmapp/contracts/geoip" +import { pingProbeWriteSchema, speedProbeWriteSchema, type PingProbeWrite } from "@mmapp/contracts" +import { serverCreateSchema, serverUpdateSchema } from "@mmapp/contracts/servers" +import { putRulesSchema } from "@mmapp/contracts/alerts" +import { statisticsQuerySchema, statisticsPivotQuerySchema } from "@mmapp/contracts/statistics" +import { putBackupScheduleSettingsSchema } from "@mmapp/contracts/backups" +import { readBackupJob } from "../../services/backup-job-store.js" +import { projectScopedBackupJob } from "../../services/backup-job-read.js" +import { registerBackupStorageActions } from "./backup-storage-actions.js" +import { pool } from "../../db/index.js" +import { createServer, updateServer, deleteServer, listServerSnapshots } from "../servers/service/servers-service.js" +import { resolveRosSrcIpv4 } from "../../utils/ros-src-address.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { getAlertsOverview, replaceAlertsConfig } from "../../services/alerts-service.js" +import { getSchedulerStatus, JOB_KEYS, listSchedulerRuns, refreshScheduler, runSchedulerJobNow } from "../../services/scheduler.js" +import { getBackupById, getBackupScheduleSettings, getBackupStorageSettings, runBackupForServer, restoreBackupToDevice, deleteBackupRecord, updateBackupScheduleSettings, testBackupStorageConnection, syncBackupsFromS3 } from "../../services/backup-service.js" +import { getCertificateRenewSettings, updateCertificateRenewSettings, getAcmeSettingsPublic } from "../../services/certificates-service.js" +import { getAlertsOutboxOverview } from "../../services/alert-engine/outbox.js" +import { readEvents } from "../events/service/events-service.js" +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 { 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" +import { getServersApiPingSettings, updateServersApiPingSettings } from "../../services/servers-rest-ping-collector.js" +import { getInternetPathSettings, updateInternetPathSettings } from "../../services/internet-path-collector.js" +import { getGeoipSettings, updateGeoipSettings } from "../../services/geoip-settings.js" +import { buildGeoipStatus } from "../../services/geoip-status.js" +import { publicEvoBgpSettings, readEvoBgpSettings } from "../../services/evobgp-settings.js" +import { readEvoBgpCatalogPage } from "./evobgp-catalog.js" +import { readWanRuntimeForServer } from "../../services/wan-runtime.js" +import { readTrafficConfiguration, readUptimeConfiguration, readServersApiPingConfiguration } from "./telemetry-settings-configuration.js" +import { registerLegacyFlowAnalyticsAction } from "./flow-analytics-actions.js" +import { registerMcpService } from "./service-registry.js" +import { decryptSecret, fingerprint, McpError } from "./security.js" +import type { add } from "./actions.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" + +const sid = z.number().int().positive().max(Number.MAX_SAFE_INTEGER) +const paging = { limit: z.number().int().min(1).max(200).default(100), offset: z.number().int().min(0).max(100000).default(0) } +async function serverSecret(input: Record, context: ActionContext) { + const config = input.server as Record, reference = input.passwordRef + if (!reference) return config + const { rows: [secret] } = await pool.query("SELECT * FROM mcp_secrets WHERE id=$1 AND created_by=$2", [reference, context.connection.owner_id]) + const existing = input.serverId ? await getServerRowById(Number(input.serverId)) : undefined + const host = String(config.host ?? existing?.host ?? ""), port = Number(config.port ?? existing?.port ?? 443) + if (!secret || secret.purpose !== `server-login:${host}:${port}` || secret.server_id && Number(secret.server_id) !== input.serverId) throw new McpError("SECRET_UNAVAILABLE", "Секрет не выдан этому назначению") + return { ...config, password: await decryptSecret(secret.ciphertext) } +} +export function registerApplicationActions(register: typeof add) { + registerBackupStorageActions(register) + register("servers.wan-runtime.read", "mm_server_wan_runtime_get", "WAN IPv4/main: шлюз, интерфейс и адреса без доказательства forwarding", "servers", "mm:servers:read", "read", "servers", z.object({ serverId: sid, ...paging }).strict(), { + read: async input => { + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const result = await readWanRuntimeForServer(server), offset = Number(input.offset), limit = Number(input.limit) + const uplinks = result.uplinks.slice(offset, offset + limit).map(uplink => ({ ...uplink, addresses: uplink.addresses.slice(0, 100), addressesTruncated: uplink.addresses.length > 100 })) + return { ...result, serverId: server.id, observedAt: new Date().toISOString(), uplinks, totalUplinks: result.uplinks.length, nextOffset: offset + limit < result.uplinks.length ? offset + limit : null, + incomplete: result.activeDefaultsTruncated || uplinks.length < result.uplinks.length || uplinks.some(uplink => uplink.addressesTruncated) } + }, + }) + register("servers.snapshots.read", "mm_server_snapshots_get", "История состояния выбранного сервера", "servers", "mm:servers:read", "read", "servers", z.object({ serverId: sid, limit: z.number().int().min(1).max(200).default(50) }).strict(), { + read: async input => { + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const snapshots = await listServerSnapshots(server.id, Number(input.limit)) + return { serverId: server.id, snapshots, incomplete: snapshots.length === Number(input.limit) } + }, + }) + register("servers.source-address.read", "mm_server_source_address_get", "IPv4 адрес управляющего host, без подтверждения адреса интерфейса", "servers", "mm:servers:read", "read", "servers", z.object({ serverId: sid }).strict(), { + read: async input => { + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + return { host: server.host, ipv4: await resolveRosSrcIpv4(server.host), purpose: "management-host-resolution", interfaceAddressVerified: false } + }, + }) + function query(id: string, tool: string, title: string, permission: string, read: NonNullable[8]["read"]>, schema = z.object({}).strict()) { + register(id, tool, title, permission.split(":")[1], permission, "read", "global", schema, { read: async (input, context) => { + const ids = (await pool.query<{ id: number }>("SELECT id FROM servers")).rows.map(row => Number(row.id)) + const { rows: grants } = await pool.query("SELECT payload FROM mcp_grants WHERE connection_id=$1 AND revoked_at IS NULL AND payload->>'credentialId'=$2", [context.connection.id, context.principal.credentialId]) + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id) || grants.some(grant => !grant.payload.scope?.serverIds?.includes(id)))) throw new McpError("SERVER_DENIED", "Глобальный обзор содержит серверы вне эффективной области доступа") + return read(input, context) + } }) + } + function change(id: string, tool: string, title: string, permission: string, schema: z.ZodObject, + current: (input: Record) => Promise, execute: (input: Record) => Promise, serverScoped = false) { + const dependencyIds = async () => (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => Number(row.id)) + const observed = async (input: Record) => serverScoped ? current(input) : { value: await current(input), serverIds: await dependencyIds() } + const verifiedConfiguration = ["alerts.replace", "backups.delete", "uptime.ping.probes.replace", "uptime.speed.probes.replace", "netflow.host.reconcile"].includes(id) || id.endsWith(".update") + registerMcpService(id, { current: observed, execute, verify: async (input, result) => { + if (input.settings && typeof input.settings === "object") { + const saved = await current(input) as Record + return !!saved && Object.entries(input.settings).every(([key, value]) => fingerprint(saved[key]) === fingerprint(value)) + } + if (id === "alerts.replace") return !!result && fingerprint(await current(input)) === fingerprint({ revision: (result as { revision: unknown }).revision }) + if (id === "backups.delete") return !await getBackupById(String(input.backupId)) + if (id === "netflow.host.reconcile") { + const status = await getFlowHostStatus() + return status.reachable && status.interfaceUp && status.configMatches === true + } + if (Array.isArray(input.probes)) { + const saved = await current(input) as Record[] + return saved.length === input.probes.length && input.probes.every((probe, index) => Object.entries(probe).every(([key, value]) => fingerprint(saved[index][key]) === fingerprint(key === "intervalSec" && Number(value) > 0 ? Math.max(5, Number(value)) : value))) + } + return result !== undefined + } }) + register(id, tool, title, permission.split(":")[1], permission, "write", serverScoped ? "servers" : "global", schema, { + ...(["traffic.settings.update", "uptime.settings.update", "servers.api-ping.settings.update"].includes(id) ? { version: 2 } : {}), + prepare: async (input, context) => { + const serverId = typeof input.serverId === "number" ? input.serverId : undefined + if (serverId && !context.connection.policy.scope.serverIds.includes(serverId)) throw new McpError("SERVER_DENIED", "Сервер не разрешён") + const before = await observed(input), ids = serverScoped ? [serverId!] : (before as { serverIds: number[] }).serverIds + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Глобальная настройка затрагивает серверы вне выданной области") + const objects: ChangeEvidence["objects"] = ids.map(serverId => ({ kind: "server", id: String(serverId), serverId })) + const domain = ({ "traffic.settings.update": "traffic-settings", "uptime.settings.update": "uptime-settings", "alerts.replace": "alerts-configuration", "backups.schedule.update": "backup-schedule", "certificates.renew.update": "certificate-renewal", "netflow.host.reconcile": "netflow-host", "servers.api-ping.settings.update": "servers-api-ping-settings", "internet-path.settings.update": "internet-path-settings" } as Record)[id] + if (domain) objects.push({ kind: domain, id: "1" }) + if (id === "geoip.settings.update") objects.push({ kind: "geoip-settings", id: "1" }) + if (Array.isArray(input.probes)) { + if (input.probes.some(probe => [probe.srcServerId, probe.dstServerId].filter(value => value !== undefined).some(value => !ids.includes(Number(value))))) throw new McpError("SERVER_DENIED", "Проба ссылается на неизвестный или недоступный сервер") + const old = (before as { value: { id: string }[] }).value + objects.push(...[...new Set([...old.map(probe => probe.id), ...input.probes.map(probe => String(probe.id))])].map(probeId => ({ kind: id.includes(".ping.") ? "ping-probe" : "speed-probe", id: probeId }))) + } + return { target: serverScoped ? { scope: "servers", serverIds: [serverId!] } : { scope: "global" }, objects, snapshots: [], before, steps: [{ kind: "service", service: id, input }], verificationScope: verifiedConfiguration ? "configuration" : "acknowledgement", warnings: [...(id === "geoip.settings.update" ? ["При включённом GeoIP обновление расписания запускает проверку зеркала сразу и затем по интервалу. Подтверждается сохранение настроек; доставка и готовность баз проверяются отдельно через состояние GeoIP. Отключение не отменяет уже начатую загрузку."] : []), ...(!verifiedConfiguration ? ["Подтверждается приём/завершение команды сервисом. Итоговое состояние внешней системы требует отдельной проверки."] : []), ...(Array.isArray(input.probes) ? ["Заменяется весь каталог проб, включая удаление отсутствующих ID. Включённые bandwidth-пробы создают повторяющуюся сетевую нагрузку по расписанию."] : []), ...(serverScoped ? [] : ["Глобальная операция затрагивает всё приложение. Проверен полный текущий каталог серверов; его изменение инвалидирует план."])], requiresHuman: true } + }, + }) + } + for (const action of ["create", "update", "delete"] as const) { + const schema = action === "delete" ? z.object({ serverId: sid }).strict() : z.object({ ...(action === "update" ? { serverId: sid } : {}), server: (action === "create" ? serverCreateSchema.omit({ password: true }) : serverUpdateSchema.omit({ password: true })).strict(), passwordRef: z.string().uuid().optional() }).strict() + const id = `servers.${action}` + registerMcpService(id, { + current: async input => input.serverId ? getServerRowById(Number(input.serverId)) : null, + execute: async input => { + if (action === "delete") { await deleteServer(Number(input.serverId)); return { deleted: true } } + const config = action === "create" ? serverCreateSchema.parse(input.resolvedServer) : serverUpdateSchema.parse(input.resolvedServer) + return action === "create" ? createServer(config as z.infer) : updateServer(Number(input.serverId), config) + }, + verify: async (input, result) => { + if (action === "delete") return !await getServerRowById(Number(input.serverId)) + const id = action === "create" ? Number((result as { id?: number } | undefined)?.id) : Number(input.serverId) + const saved = await getServerRowById(id) + if (!saved) return false + const expected = action === "create" ? serverCreateSchema.parse(input.resolvedServer) : serverUpdateSchema.parse(input.resolvedServer) + return Object.entries(expected).every(([key, value]) => fingerprint(saved[key as keyof typeof saved]) === fingerprint(value)) + }, + }) + register(id, `mm_servers_${action}_plan`, `Серверы: ${action}`, "servers", "mm:servers:write", "write", action === "create" ? "global" : "servers", schema, { + prepare: async (input, context) => { + const before = input.serverId ? await getServerRowById(Number(input.serverId)) : null + if (action !== "create" && !before) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const config = input.server as Record | undefined + if (action === "update" && ["host", "port", "username", "useSsl", "verifySsl"].some(key => config?.[key] !== undefined && config[key] !== before?.[key as keyof typeof before]) && !input.passwordRef) throw new McpError("SECRET_UNAVAILABLE", "Для изменения параметров подключения нужен секрет, привязанный к назначению") + const resolvedServer = action === "delete" ? undefined : await serverSecret(input, context) + return { target: action === "create" ? { scope: "global" } : { scope: "servers", serverIds: [Number(input.serverId)] }, objects: input.serverId ? [{ kind: "server", id: String(input.serverId), serverId: Number(input.serverId) }] : [], snapshots: [], before, + steps: [{ kind: "service", service: id, input: { ...input, resolvedServer } }], requiresHuman: true, warnings: ["Новый сервер не добавляется автоматически в разрешения MCP."] } + }, + }) + } + + query("alerts.read", "mm_alerts_get", "Оповещения и история", "mm:alerts:read", async () => getAlertsOverview()) + query("alerts.outbox.read", "mm_alerts_outbox_get", "Очередь уведомлений", "mm:alerts:read", async () => getAlertsOutboxOverview()) + change("alerts.replace", "mm_alerts_update_plan", "Настройка правил оповещений", "mm:alerts:write", z.object({ configuration: putRulesSchema }).strict(), + async () => { const result = await getAlertsOverview(); return { revision: result.revision } }, + async input => { const result = await replaceAlertsConfig(putRulesSchema.parse(input.configuration)); await refreshScheduler(); return result }) + query("scheduler.read", "mm_scheduler_get", "Состояние планировщика", "mm:settings:admin", async () => getSchedulerStatus()) + query("evobgp.settings.read", "mm_evobgp_settings_get", "Публичные настройки интеграции EvoBGP", "mm:settings:admin", async () => publicEvoBgpSettings(await readEvoBgpSettings())) + query("evobgp.catalog.read", "mm_evobgp_catalog_get", "Каталог EvoBGP: модули, ASN, префиксы и Community", "mm:settings:admin", readEvoBgpCatalogPage, z.object(paging).strict()) + const trafficSettings = z.object({ enabled: z.boolean().optional(), intervalSec: z.number().int().min(5).max(3600).optional(), retentionDays: z.number().int().min(1).max(365).optional() }).strict().refine(value => Object.keys(value).length > 0) + const uptimeSettings = z.object({ enabled: z.boolean().optional(), resourcesEnabled: z.boolean().optional(), pingEnabled: z.boolean().optional(), speedEnabled: z.boolean().optional(), intervalSec: z.number().int().min(5).max(86400).optional(), probeIntervalSec: z.number().int().min(5).max(86400).optional(), speedIntervalSec: z.number().int().min(10).max(86400).optional(), retentionDays: z.number().int().min(1).max(365).optional() }).strict().refine(value => Object.keys(value).length > 0) + query("traffic.settings.read", "mm_traffic_settings_get", "Настройки сбора трафика", "mm:traffic:read", async () => readPublicTrafficSettings()) + query("geoip.read", "mm_geoip_get", "Состояние локальных баз GeoIP и обновлений", "mm:traffic:read", async () => buildGeoipStatus()) + const geoipSettings = geoipSettingsPatchSchema.strict().refine(value => Object.keys(value).length > 0) + change("geoip.settings.update", "mm_geoip_settings_update_plan", "План настроек обновления GeoIP", "mm:traffic:write", z.object({ settings: geoipSettings }).strict(), async () => { + const settings = await getGeoipSettings() + return { enabled: settings.enabled, updateIntervalSec: settings.updateIntervalSec } + }, async input => { + const settings = await updateGeoipSettings(geoipSettings.parse(input.settings)) + await refreshScheduler() + return { settings, verificationScope: "configuration-persistence" } + }) + 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("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[0]); await refreshScheduler(); return result }) + query("internet-path.settings.read", "mm_internet_path_settings_get", "Настройки наблюдений Internet path", "mm:network:read", async () => getInternetPathSettings()) + const readInternetPathConfiguration = async () => { + const row = await getInternetPathSettings() + // Collection also updates updatedAt: bind plans to configuration, not run status. + return { enabled: row.enabled, intervalSec: row.intervalSec, retentionDays: row.retentionDays } + } + change("internet-path.settings.update", "mm_internet_path_settings_update_plan", "План сбора Internet path", "mm:network:write", z.object({ settings: z.object({ enabled: z.boolean().optional(), intervalSec: z.number().int().min(30).max(86400).optional(), retentionDays: z.number().int().min(1).max(365).optional() }).strict().refine(value => Object.keys(value).length > 0) }).strict(), readInternetPathConfiguration, async input => { const result = await updateInternetPathSettings(input.settings as Parameters[0]); await refreshScheduler(); return result }) + query("netflow.host.read", "mm_netflow_host_get", "Состояние хоста NetFlow", "mm:settings:admin", async () => getFlowHostStatus()) + change("netflow.host.reconcile", "mm_netflow_host_reconcile_plan", "План применения сохранённой конфигурации хоста NetFlow", "mm:settings:admin", z.object({}).strict(), async () => { + if (!agentSocket()) throw new McpError("UNSUPPORTED_CAPABILITY", "Локальный агент NetFlow не настроен", 409) + const row = await getTrafficFlowSettingsRow() + return { configurationHash: fingerprint({ collectorIp: row.collectorIp, prefix: row.prefix, flowListenPort: row.flowListenPort, wgListenPort: row.wgListenPort, hostPublicKey: row.hostPublicKey, hostPrivateKey: row.hostPrivateKey, peersJson: row.peersJson }) } + }, async () => withFlowConfigurationLock(() => reconcileFlowHost())) + const pingCatalog = async () => (await readProbeRows()).map(row => ({ id: row.id, srcServerId: row.srcServerId, srcInterface: row.srcInterface, name: row.name, target: row.target, filter: row.probeFilter, enabled: row.enabled, showOnDashboard: row.showOnDashboard, intervalSec: row.intervalSec })) + const speedCatalog = async () => (await readSpeedProbeCatalog()).map(row => ({ id: row.id, srcServerId: row.srcServerId, dstServerId: row.dstServerId, srcInterface: row.srcInterface, dstInterface: row.dstInterface, protocol: row.protocol, direction: row.direction, durationSec: row.durationSec, enabled: row.enabled })) + query("uptime.ping.probes.read", "mm_ping_probes_get", "Каталог Ping-проб", "mm:uptime:read", pingCatalog) + query("uptime.speed.probes.read", "mm_speed_probes_get", "Каталог bandwidth-проб", "mm:uptime:read", speedCatalog) + const boundedCatalog = (item: z.ZodObject) => z.object({ probes: z.array(item.strict()).max(500).refine(rows => new Set(rows.map(row => row.id)).size === rows.length, "Повторный ID пробы") }).strict() + change("uptime.ping.probes.replace", "mm_ping_probes_replace_plan", "План полного каталога Ping-проб", "mm:uptime:write", boundedCatalog(pingProbeWriteSchema), pingCatalog, async input => { await replaceProbes(input.probes as PingProbeWrite[]); await refreshScheduler(); return { saved: true } }) + change("uptime.speed.probes.replace", "mm_speed_probes_replace_plan", "План полного каталога bandwidth-проб", "mm:uptime:write", boundedCatalog(speedProbeWriteSchema.safeExtend({ durationSec: z.number().int().min(3).max(30) })), speedCatalog, async input => { await replaceSpeedProbeCatalog(input.probes as Parameters[0]); await refreshScheduler(); return { saved: true } }) + change("scheduler.run", "mm_scheduler_run_plan", "Запуск задания", "mm:settings:admin", z.object({ jobKey: z.enum(JOB_KEYS) }).strict(), async () => null, async input => runSchedulerJobNow(String(input.jobKey))) + change("scheduler.refresh", "mm_scheduler_refresh_plan", "Перечитать расписания", "mm:settings:admin", z.object({}).strict(), async () => null, async () => { await refreshScheduler(); return { refreshed: true } }) + query("events.read", "mm_events_list", "События приложения", "mm:dashboard:read", async input => ({ events: await readEvents({ limit: Number(input.limit) }) }), z.object({ limit: z.number().int().min(1).max(200).default(100) }).strict()) + + register("backups.jobs.read", "mm_backup_job_get", "Состояние фоновой backup-задачи приложения", "backups", "mm:backups:read", "read", "servers", z.object({ jobId: z.string().min(1).max(200), ...paging }).strict(), { + read: async (input, context) => { + let result: ReturnType + try { result = projectScopedBackupJob(readBackupJob(String(input.jobId)), new Set(context.connection.policy.scope.serverIds), Number(input.offset), Number(input.limit)) } + catch { throw new McpError("STATE_UNAVAILABLE", "Прогресс backup-задачи не подтверждён", 503) } + if (!result) throw new McpError("NOT_FOUND", "Backup-задача не найдена в разрешённой области", 404) + const rows = await pool.query("SELECT id FROM servers WHERE id=ANY($1::bigint[])", [result.serverIds]) + if (rows.rows.length !== result.serverIds.length) throw new McpError("NOT_FOUND", "Устройства backup-задачи отсутствуют в каталоге", 404) + return result + }, + }) + register("backups.list", "mm_backups_list", "Резервные копии", "backups", "mm:backups:read", "read", "servers", z.object({ serverId: sid, ...paging }).strict(), { + read: async input => { + const { rows } = await pool.query<{ id: string }>("SELECT id FROM backup_entries WHERE server_id=$1 ORDER BY created_at DESC LIMIT $2 OFFSET $3", [String(input.serverId), input.limit, input.offset]) + return { items: await Promise.all(rows.map(row => getBackupById(row.id))) } + }, + }) + change("backups.create", "mm_backups_create_plan", "Создание backup", "mm:backups:write", z.object({ serverId: sid, notes: z.string().max(1000).default("") }).strict(), async input => getServerRowById(Number(input.serverId)), async input => runBackupForServer(String(input.serverId), "manual", String(input.notes)), true) + for (const action of ["restore", "delete"] as const) { + change(`backups.${action}`, `mm_backups_${action}_plan`, `Backup: ${action}`, "mm:backups:write", z.object({ serverId: sid, backupId: z.string().min(1).max(200) }).strict(), async input => { + const backup = await getBackupById(String(input.backupId)); if (!backup || backup.serverId !== input.serverId) throw new McpError("NOT_FOUND", "Резервная копия не найдена", 404); return backup + }, async input => action === "restore" ? restoreBackupToDevice(String(input.backupId)) : deleteBackupRecord(String(input.backupId)), true) + } + query("backups.schedule.read", "mm_backup_schedule_get", "Расписание backup", "mm:backups:read", async () => getBackupScheduleSettings()) + query("backups.storage.read", "mm_backup_storage_get", "Состояние хранилища backup", "mm:backups:read", async () => getBackupStorageSettings()) + change("backups.schedule.update", "mm_backup_schedule_update_plan", "Изменение расписания backup", "mm:backups:write", z.object({ settings: putBackupScheduleSettingsSchema }).strict(), async () => getBackupScheduleSettings(), async input => { const result = await updateBackupScheduleSettings(putBackupScheduleSettingsSchema.parse(input.settings)); await refreshScheduler(); return result }) + change("backups.storage.test", "mm_backup_storage_test_plan", "Проверка хранилища backup", "mm:backups:write", z.object({}).strict(), async () => getBackupStorageSettings(), async () => testBackupStorageConnection()) + change("backups.storage.sync", "mm_backup_storage_sync_plan", "Синхронизация backup из S3", "mm:backups:write", z.object({}).strict(), async () => null, async () => syncBackupsFromS3()) + query("certificates.acme.read", "mm_acme_settings_get", "Публичные настройки ACME", "mm:certificates:read", async () => getAcmeSettingsPublic()) + query("certificates.renew.read", "mm_certificate_renew_get", "Настройки продления сертификатов", "mm:certificates:read", async () => getCertificateRenewSettings()) + change("certificates.renew.update", "mm_certificate_renew_update_plan", "Настройка продления сертификатов", "mm:certificates:write", z.object({ settings: z.object({ enabled: z.boolean().optional(), intervalSec: z.number().int().min(60).max(86400).optional(), renewBeforeDays: z.number().int().min(1).max(365).optional() }).strict() }).strict(), async () => getCertificateRenewSettings(), async input => updateCertificateRenewSettings(input.settings as Parameters[0])) + + for (const table of ["traffic_samples", "uptime_resource_samples", "uptime_probe_samples"] as const) { + register(`monitoring.${table}.read`, `mm_${table}_query`, `История: ${table}`, "monitoring", table === "traffic_samples" ? "mm:traffic:read" : "mm:uptime:read", "read", "servers", z.object({ serverId: sid, ...paging }).strict(), { + read: async input => { + if (table === "uptime_probe_samples") { + const { rows } = await pool.query("SELECT s.* FROM uptime_probe_samples s JOIN uptime_probes p ON p.id=s.probe_id WHERE p.src_server_id=$1 ORDER BY s.sampled_at DESC, s.probe_id DESC LIMIT $2 OFFSET $3", [input.serverId, input.limit, input.offset]); return { items: rows } + } + const orderBy = table === "traffic_samples" ? "sampled_at DESC, interface_name DESC, peer_public_key DESC" : "sampled_at DESC" + const { rows } = await pool.query(`SELECT * FROM ${table} WHERE server_id=$1 ORDER BY ${orderBy} LIMIT $2 OFFSET $3`, [input.serverId, input.limit, input.offset]); return { items: rows } + }, + }) + } + registerLegacyFlowAnalyticsAction(register) + register("monitoring.flow.monthly", "mm_netflow_monthly_get", "Месячная статистика NetFlow выбранного сервера", "traffic", "mm:traffic:read", "read", "servers", z.object({ serverId: sid, month: z.string().regex(/^\d{4}-(0[1-9]|1[0-2])$/) }).strict(), { + read: async input => ({ serverId: input.serverId, source: "PostgreSQL/flow_daily_dims", observedAt: new Date().toISOString(), statistics: await getFlowMonthly(String(input.month), Number(input.serverId), { maxKeys: 10000 }) }), + }) + const period = { serverId: sid, from: z.string().date(), to: z.string().date() } + for (const pivot of [false, true]) { + const schema = (pivot ? statisticsPivotQuerySchema : statisticsQuerySchema).extend(period).strict().superRefine((input, context) => { + const span = Date.parse(input.to) - Date.parse(input.from) + if (span < 0 || span > 30 * 86400000) context.addIssue({ code: "custom", message: "Статистика MCP ограничена 31 календарным днём" }) + }) + register(pivot ? "statistics.pivot" : "statistics.read", pivot ? "mm_statistics_pivot_get" : "mm_statistics_get", pivot ? "Сводная статистика выбранного сервера" : "Статистика выбранного сервера", "traffic", "mm:traffic:read", "read", "servers", schema, { + read: async input => { + if (!pivot) return getStatistics(statisticsQuerySchema.parse(input)) + const parsed = statisticsPivotQuerySchema.parse(input) + if (pivotDimsConflict(parsed.row, parsed.col)) throw new McpError("INVALID_INPUT", "Строки и столбцы сводной должны отличаться", 400) + return getStatisticsPivot(parsed) + }, + }) + } +} diff --git a/backend/src/modules/mcp/application-adapters-http.test.ts b/backend/src/modules/mcp/application-adapters-http.test.ts new file mode 100644 index 00000000..93036c44 --- /dev/null +++ b/backend/src/modules/mcp/application-adapters-http.test.ts @@ -0,0 +1,37 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED="true" +process.env.AUTH_JWT_SECRET="mcp-owned-application-acceptance-secret" +process.env.AUTH_ISSUER="https://auth.test.local" +const {pool,closePool}=await import("../../db/index.js") +const {applySqlMigrations}=await import("../../db/migrate.js") +const {updateMcpSettings}=await import("./store.js") +const {buildApp}=await import("../../index.js") +const actor={id:"application-acceptance-owner",apps:["mm"],permissions:["mm:settings:admin","mm:network:admin"],expiresAt:Date.now()+3600000} +let app:Awaited>|undefined +try{ + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n,0,"Disposable empty database required") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,enabled) VALUES(1,'owned-fixture','127.0.0.1',9,false,false)") + await pool.query("INSERT INTO geoip_settings(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await pool.query("INSERT INTO backup_storage_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({enabled:true},actor) + app=await buildApp({startScheduler:false});await app.ready() + await (await import("../../test/mcp-users-bindings.js")).verifyMcpUserBindings(actor) + await (await import("./routing-community-context-acceptance.js")).acceptRouteCommunityContextMcp(actor) + await (await import("../../test/mcp-acme-test.js")).verifyMcpAcmeTest(actor) + await (await import("../../test/mcp-acme-settings.js")).verifyMcpAcmeSettings(actor) + await (await import("../../test/mcp-geoip-update.js")).verifyMcpGeoipUpdate(actor) + await (await import("../../test/mcp-flow-overlay.js")).verifyMcpFlowOverlay(actor) + await (await import("../../test/mcp-backup-download.js")).verifyMcpBackupDownload(actor,app) + await (await import("./wireguard-interface-create-acceptance.js")).acceptWireguardInterfaceCreation(actor) + await (await import("./wireguard-peer-create-acceptance.js")).acceptWireguardPeerCreation(actor) + await (await import("../../test/mcp-database-backup.js")).verifyMcpDatabaseBackup(actor,app) +}finally{ + await pool.query("UPDATE servers SET enabled=false").catch(()=>{}) + // Database export resumes shared background work even for this scheduler-free + // app fixture. Drain it before closing the pool, as the full integration does. + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close();await closePool() +} diff --git a/backend/src/modules/mcp/arp-actions.ts b/backend/src/modules/mcp/arp-actions.ts new file mode 100644 index 00000000..84baccd4 --- /dev/null +++ b/backend/src/modules/mcp/arp-actions.ts @@ -0,0 +1,66 @@ +import { z } from "zod" +import { isIP } from "node:net" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" +import { isStaticArpRow, readRosSnapshot, resourceById, validateRosStep } from "./resources.js" +import { ospfInterfaceResources } from "./routing-dependencies.js" +import { McpError } from "./security.js" + +const serverId = z.number().int().positive().safe() +const rowId = z.string().regex(/^\*[a-z0-9]+$/i).max(200) + +export function registerArpActions(register: typeof add) { + const resource = resourceById("ip_arp") + for (const action of ["create", "update", "delete"] as const) { + const schema = z.object({ serverId, ...(action === "create" ? {} : { id: rowId }), ...(action === "delete" ? {} : { fields: resource.fields }) }).strict() + register(`ros.ip_arp.${action}`, `mm_ip_arp_${action}_plan`, `${resource.title}: ${action}`, "network", "mm:network:write", "write", "servers", schema, { + prepare: async input => { + const sid = Number(input.serverId), id = typeof input.id === "string" ? input.id : undefined + const fields = validateRosStep(resource, action, input.fields ?? {}, id) + const before = await readRosSnapshot(sid, resource.id) + const current = action === "create" ? undefined : before.rows.find(row => row[".id"] === id) + if (action !== "create" && !current) throw new McpError("NOT_FOUND", "Запись ARP не найдена", 404) + if (current && !isStaticArpRow(current)) throw new McpError("PROTECTED_OBJECT", "План изменяет только подтверждённую статическую запись ARP", 409) + if (action === "update" && Object.keys(fields).some(key => { + const value = current?.[key], validator = resource.fields.shape[key] + if (value === undefined || value === null) return true + const previous = validator.safeParse(true).success && (value === "true" || value === "false") ? value === "true" : value + return !validator.safeParse(previous).success + })) throw new McpError("STATE_UNAVAILABLE", "Не подтверждено прежнее значение ARP для обратного плана", 409) + const desired = { ...current, ...fields } + if (typeof desired.interface !== "string" || !desired.interface || typeof desired.address !== "string" || isIP(desired.address) !== 4) throw new McpError("STATE_UNAVAILABLE", "Не подтверждена привязка записи ARP", 409) + if (action !== "delete") { + const mac = desired["mac-address"], proxy = desired.published === "true" + const validMac = typeof mac === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/.test(mac) && Number.parseInt(mac.slice(0, 2), 16) % 2 === 0 + if ((!validMac || mac === "00:00:00:00:00:00") && !(proxy && (mac === undefined || mac === "00:00:00:00:00:00"))) throw new McpError("INVALID_INPUT", "Требуется индивидуальный MAC-адрес или явная публикация proxy ARP", 400) + if (before.rows.some(row => row[".id"] !== id && row.address === desired.address && (row.interface === desired.interface || typeof row.interface !== "string" || !row.interface))) throw new McpError("STATE_CONFLICT", "Адрес ARP уже занят или его привязка не подтверждена", 409) + } + const names = new Set([desired.interface, current?.interface].filter((name): name is string => typeof name === "string" && name.length > 0)) + const inventory = await readRosSnapshot(sid, "interface_inventory", undefined, before.sourceProof) + const objects: ChangeEvidence["objects"] = [{ kind: resource.id, id: id ?? "new", serverId: sid }] + const snapshots = [before, inventory] + const addRef = (kind: string, value: unknown) => { + if (typeof value !== "string" || !/^\*[a-z0-9]+$/i.test(value)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён ID интерфейса ARP", 409) + objects.push({ kind, id: value, serverId: sid }) + } + for (const name of names) { + const rows = inventory.rows.filter(row => row.name === name) + if (rows.length !== 1) throw new McpError("STATE_CONFLICT", "Интерфейс ARP отсутствует или неоднозначен", 409) + addRef("interface", rows[0][".id"]) + } + for (const kind of ospfInterfaceResources) { + const snapshot = await readRosSnapshot(sid, kind, undefined, before.sourceProof) + snapshots.push(snapshot) + for (const row of snapshot.rows) if (names.has(String(row.name))) addRef(kind, row[".id"]) + } + const vrfs = await readRosSnapshot(sid, "vrf", undefined, before.sourceProof) + snapshots.push(vrfs) + const assigned = new Set([...names].map(name => vrfs.rows.find(row => typeof row.interfaces === "string" && row.interfaces.split(",").map(member => member.trim()).some(member => member === "all" || member === name)))) + for (const vrf of assigned) if (vrf) addRef("vrf", vrf[".id"]) + return { target: { scope: "servers", serverIds: [sid] }, objects, snapshots, requiresHuman: true, + steps: [{ kind: "ros", step: { serverId: sid, resource: resource.id, action, id, fields } }], + warnings: ["Изменение статической записи меняет разрешение адреса IPv4 в MAC на указанном интерфейсе.", ...(desired.published === "true" ? ["Публикация включает ответы proxy ARP для этого адреса при наличии активного маршрута."] : [])] } + }, + }) + } +} diff --git a/backend/src/modules/mcp/arp-write-acceptance.ts b/backend/src/modules/mcp/arp-write-acceptance.ts new file mode 100644 index 00000000..3f50029f --- /dev/null +++ b/backend/src/modules/mcp/arp-write-acceptance.ts @@ -0,0 +1,167 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { applyPlan, approvePlan, invokeMcpTool } from "./runtime.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js" + +type NativeRow = Record +type OperationState = { state: string; verification: string; error_code: string | null } + +/** Runs on a disposable PostgreSQL database and models RouterOS using loopback HTTP. */ +export async function acceptArpWriteMcp(actor: AuthUser) { + const arpPath = "/ip/arp" + const nativeId = "*A5" + const ethernet = [ + { ".id": "*E1", name: "ether1", type: "ether", disabled: "false" }, + { ".id": "*E2", name: "ether2", type: "ether", disabled: "false" }, + ] + const staticFlags = { dynamic: "false", dhcp: "false", disabled: "false", published: "false" } + const original: NativeRow[] = [ + { ...staticFlags, ".id": "*A1", address: "192.0.2.1", interface: "ether1", "mac-address": "02:00:00:00:00:01", dynamic: "true" }, + { ...staticFlags, ".id": "*A2", address: "192.0.2.2", interface: "ether1", "mac-address": "02:00:00:00:00:02", dhcp: "true" }, + { ...staticFlags, ".id": "*A3", address: "192.0.2.3", interface: "ether2", "mac-address": "02:00:00:00:00:03" }, + ] + const arpRows = structuredClone(original) + const catalogs: Record = { + [arpPath]: arpRows, + "/interface": ethernet, + "/interface/ethernet": ethernet, + "/interface/bridge": [], + "/interface/bridge/port": [], + "/interface/bridge/vlan": [], + "/interface/vlan": [], + "/interface/gre": [], + "/interface/wireguard": [], + "/interface/vxlan": [], + "/interface/list": [], + "/interface/list/member": [], + "/ip/address": [], + "/ipv6/address": [], + "/ip/vrf": [{ ".id": "*F1", name: "main", interfaces: "all" }], + } + const writes: { method: string; path: string; fields: NativeRow }[] = [] + const router = createServer(async (req, res) => { + try { + const url = new URL(req.url ?? "", "http://127.0.0.1") + const path = decodeURIComponent(url.pathname.replace(/^\/rest/, "")) + res.setHeader("content-type", "application/json") + if (req.method === "GET" && Object.hasOwn(catalogs, path)) { + res.end(JSON.stringify(catalogs[path])) + return + } + const method = req.method ?? "" + const index = path.startsWith(arpPath + "/") ? arpRows.findIndex(row => row[".id"] === path.slice(arpPath.length + 1)) : -1 + const creating = method === "PUT" && path === arpPath + const updating = method === "PATCH" && index !== -1 + const deleting = method === "DELETE" && index !== -1 + if (!creating && !updating && !deleting) { + res.writeHead(404).end("{}") + return + } + + let body = "" + for await (const chunk of req) body += chunk + const fields: NativeRow = body ? JSON.parse(body) : {} + writes.push({ method, path, fields }) + if (creating) { + const row = { ...staticFlags, ".id": nativeId, ...fields } + arpRows.push(row) + res.writeHead(201).end(JSON.stringify(row)) + return + } + if (updating) { + Object.assign(arpRows[index], fields) + res.end(JSON.stringify(arpRows[index])) + return + } + arpRows.splice(index, 1) + res.writeHead(204).end() + } catch { + res.writeHead(500).end("{}") + } + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let serverId: number | undefined + let connectionId: string | undefined + const waitForOperation = async (id: string) => { + const deadline = Date.now() + 45_000 + while (Date.now() < deadline) { + const { rows: [row] } = await pool.query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id]) + if (row && !["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw new Error("ARP operation remains live") + } + try { + const port = (router.address() as { port: number }).port + const { rows: [server] } = await pool.query<{ id: number }>("INSERT INTO servers(name,host,port,use_ssl,password) VALUES('arp-write','127.0.0.1',$1,false,'fixture-only') RETURNING id", [port]) + serverId = Number(server.id) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Static ARP writes", mode: "approval", expiresAt: new Date(Date.now() + 1_800_000).toISOString(), + permissions: ["mm:network:read", "mm:network:write"], + scope: { serverIds: [serverId], operationIds: ["ros.ip_arp.create", "ros.ip_arp.update", "ros.ip_arp.delete", "operations.recover"], allowGlobal: false }, + }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = (tool: string, input: Record) => invokeMcpTool(principal, tool, { serverId, ...input }) as Promise<{ planId: string }> + const dispatch = async (planId: string) => { + const operation = await applyPlan(principal, planId, randomUUID()) + return { id: operation.id, ...await waitForOperation(operation.id) } + } + const apply = async (planId: string) => { + await approvePlan(planId, actor, true) + const operation = await dispatch(planId) + assert.equal(operation.state, "applied", JSON.stringify(operation)) + assert.equal(operation.verification, "passed", JSON.stringify(operation)) + return operation + } + const run = async (tool: string, input: Record) => apply((await prepare(tool, input)).planId) + + await assert.rejects(prepare("mm_ip_arp_update_plan", { id: "*A1", fields: { disabled: true } }), { code: "PROTECTED_OBJECT" }) + await assert.rejects(prepare("mm_ip_arp_delete_plan", { id: "*A2" }), { code: "PROTECTED_OBJECT" }) + assert.equal(writes.length, 0) + + const fields = { address: "192.0.2.5", interface: "ether1", "mac-address": "02:00:00:00:00:AB", published: false, disabled: false } + const stale = await prepare("mm_ip_arp_create_plan", { fields }) + await approvePlan(stale.planId, actor, true) + const foreign = { ...staticFlags, ".id": "*AF", address: fields.address, interface: fields.interface, "mac-address": "02:00:00:00:00:03" } + arpRows.push(foreign) + try { + assert.equal((await dispatch(stale.planId)).state, "rejected") + assert.equal(writes.length, 0) + assert.deepEqual(arpRows, [...original, foreign]) + } finally { + arpRows.splice(arpRows.indexOf(foreign), 1) + } + + await run("mm_ip_arp_create_plan", { fields }) + const created = arpRows.find(row => row[".id"] === nativeId) + assert.deepEqual(created, { ...staticFlags, ".id": nativeId, address: fields.address, interface: fields.interface, "mac-address": "02:00:00:00:00:ab" }) + + const updated = await run("mm_ip_arp_update_plan", { id: nativeId, fields: { "mac-address": "02:00:00:00:00:BC", published: true } }) + assert.equal(created?.["mac-address"], "02:00:00:00:00:bc") + assert.equal(created?.published, "true") + const inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: updated.id }) as { planId: string } + await apply(inverse.planId) + assert.equal(created?.["mac-address"], "02:00:00:00:00:ab") + assert.equal(created?.published, "false") + await run("mm_ip_arp_delete_plan", { id: nativeId }) + assert.deepEqual(arpRows, original) + assert.deepEqual(writes, [ + { method: "PUT", path: arpPath, fields: { ...fields, "mac-address": "02:00:00:00:00:ab", published: "false", disabled: "false" } }, + { method: "PATCH", path: `${arpPath}/${nativeId}`, fields: { "mac-address": "02:00:00:00:00:bc", published: "true" } }, + { method: "PATCH", path: `${arpPath}/${nativeId}`, fields: { "mac-address": "02:00:00:00:00:ab", published: "false" } }, + { method: "DELETE", path: `${arpPath}/${nativeId}`, fields: {} }, + ]) + console.log("✓ ARP SQL/HTTP: verified static create/update/inverse/delete, normalized MAC, protected dynamic/DHCP records and post-approval address drift") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + if (serverId !== undefined) await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + router.closeAllConnections() + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/authorized-bindings.ts b/backend/src/modules/mcp/authorized-bindings.ts new file mode 100644 index 00000000..314b2046 --- /dev/null +++ b/backend/src/modules/mcp/authorized-bindings.ts @@ -0,0 +1,19 @@ +import { hasPermission } from "../../lib/permissions.js" + +interface RegistrationPolicy { + policy: { scope: { operationIds: readonly string[] }; permissions: readonly string[]; mode: string } + owner_permissions: readonly string[] +} +interface RegisteredAction { operation: { id: string; requiredPermissions: readonly string[]; effect: string } } +/** Exact existing server-registration policy. Priority and SDK registration + * consume the SAME returned action objects; this does not replace runtime + * revalidation or manufacture effective/temporary-grant authorization. + */ +export function authorizedMcpBindings(actions: readonly T[], connection: RegistrationPolicy): T[] { + return actions.filter(action => { + const descriptor = action.operation + return connection.policy.scope.operationIds.includes(descriptor.id) && + descriptor.requiredPermissions.every(permission => hasPermission(connection.policy.permissions, permission) && hasPermission(connection.owner_permissions, permission)) && + (connection.policy.mode !== "observer" || descriptor.effect === "read") + }) +} diff --git a/backend/src/modules/mcp/backup-download-actions.ts b/backend/src/modules/mcp/backup-download-actions.ts new file mode 100644 index 00000000..a755eefa --- /dev/null +++ b/backend/src/modules/mcp/backup-download-actions.ts @@ -0,0 +1,46 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { accessGeneration, fingerprint, McpError } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import { registerMcpService } from "./service-registry.js" +import { createBackupDownloadArtifact, backupDownloadAuthority, backupDownloadAuthoritySchema, readBackupDownloadSource, verifyBackupDownloadArtifact } from "./backup-download-artifact.js" +import type { add } from "./actions.js" + +export function registerBackupDownloadActions(register: typeof add) { + const authority=(input:Record)=>backupDownloadAuthoritySchema.parse(input.authority) + const current=async(input:Record)=>({proof:(await readBackupDownloadSource(authority(input))).proof}) + registerMcpService("backups.download",{current, + execute:async input=>createBackupDownloadArtifact(authority(input),String(input.expectedProof)), + verify:async(input,result)=>{ + const receipt=result as {artifactId?:string;humanDownloadOnly?:boolean;backupContentReturned?:boolean}|null + return receipt?.humanDownloadOnly===true && receipt.backupContentReturned===false && !!receipt.artifactId && await verifyBackupDownloadArtifact(receipt.artifactId,authority(input),String(input.expectedProof)) + }, + }) + register("backups.download","mm_backup_download_plan","Подготовить владельцу скачивание полной резервной копии после человеческого подтверждения","backups","mm:backups:write","write","servers",z.object({backupId:z.string().min(1).max(200)}).strict(),{ + target:async(input,context)=>{ + const row=(await (context.transactionClient??pool).query("SELECT server_id FROM backup_entries WHERE id=$1",[input.backupId])).rows[0] + if(!row?.server_id)throw new McpError("SERVER_DENIED","Принадлежность резервной копии не подтверждена",403) + return {scope:"servers",serverIds:[Number(row.server_id)]} + }, + prepare:async(input,context)=>{ + const authorize=async()=>{ + if(context.signal?.aborted)throw new McpError("READ_CANCELLED","Подготовка скачивания отменена",409) + const fresh=await effectiveMcpConnection(context.principal) + if(fingerprint(fresh.policy)!==fingerprint(context.connection.policy))throw new McpError("RESET_REQUIRED","Допуск изменился; подготовьте скачивание заново",409) + } + await authorize() + const row=(await pool.query("SELECT server_id FROM backup_entries WHERE id=$1",[input.backupId])).rows[0] + if(!row?.server_id)throw new McpError("SERVER_DENIED","Принадлежность резервной копии не подтверждена",403) + const access=backupDownloadAuthority(context.principal,context.connection.owner_id,context.connection.policy,await accessGeneration(),String(input.backupId),Number(row.server_id)) + const state=await readBackupDownloadSource(access,authorize,context.signal) + const objects=[{kind:"backup-download",id:String(input.backupId),serverId:access.serverId},{kind:"backup",id:String(input.backupId),serverId:access.serverId},{kind:"server",id:String(access.serverId),serverId:access.serverId}] + const protections=await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001",[String(access.serverId)]) + if(protections.rows.length>1000)throw new McpError("STATE_UNAVAILABLE","Превышен предел защит резервной копии",409) + for(const {object_ref:ref} of protections.rows)objects.push(ref) + await authorize() + return {target:{scope:"servers",serverIds:[access.serverId]},objects,snapshots:[],before:{proof:state.proof},requiresHuman:true,mutationCost:1, + steps:[{kind:"service",service:"backups.download",input:{authority:access,expectedProof:state.proof,beforeFingerprint:fingerprint({proof:state.proof})}}], + warnings:["Полная резервная копия может содержать пароли, приватные ключи и закрытую конфигурацию RouterOS. Подтверждение разрешает раскрытие только вошедшему владельцу через web API на 15 минут.","Модель получает только ссылку и контрольную сумму; содержимое резервной копии в MCP не возвращается. Файл ограничен 8 МиБ, чтение — 45 секундами.","Отзыв, пауза, смена прав, владельца, поколения доступа, metadata, содержимого или подключения S3 блокируют выдачу. Для нового экспорта требуется новый человеческий план; автоматического повтора нет."]} + }, + }) +} diff --git a/backend/src/modules/mcp/backup-download-artifact.ts b/backend/src/modules/mcp/backup-download-artifact.ts new file mode 100644 index 00000000..9cc51661 --- /dev/null +++ b/backend/src/modules/mcp/backup-download-artifact.ts @@ -0,0 +1,143 @@ +import { createHash, randomUUID, timingSafeEqual } from "node:crypto" +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { readBackupContent, type BackupMeta } from "../../services/backup-service.js" +import { assertVerifiedConfigurationAccess, executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { assertLogArtifactBudget } from "./log-artifacts.js" +import { assertPortalClaimsCurrent } from "./portal-revocation.js" +import { connectionWrite, effectiveMcpConnection, type McpPrincipal } from "./store.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint } from "./security.js" + +const MAX_BYTES = 8 * 1048576, MAX_CIPHER = 16 * 1048576 +const digest = z.string().regex(/^[a-f0-9]{64}$/) +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.iso.datetime() }).strict() +export const backupDownloadAuthoritySchema = z.object({ principal: principalSchema, ownerId: z.string().min(1).max(200), policyProof: digest, generation: digest, backupId: z.string().min(1).max(200), serverId: z.number().int().positive().safe() }).strict() +export type BackupDownloadAuthority = z.infer +const scopeSchema = z.object({ version: z.literal(1), kind: z.literal("backup-download"), authority: backupDownloadAuthoritySchema, sourceProof: digest, + bytes: z.number().int().min(0).max(MAX_BYTES), sha256: digest, encryptedSha256: digest, proof: digest }).strict() +type Header = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date } +type Meta = Header & { access_scope: z.infer; encrypted_bytes: number } +const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "backup-download-artifact-v1") +const signed = (header: Header, scope: Omit, "proof">) => ({ id:header.id,connection_id:header.connection_id,filename:header.filename,media_type:header.media_type,expires_at:header.expires_at.toISOString(),scope }) +const fail = (message: string, code = "ARTIFACT_INVALID") => new McpError(code, message, 409) + +export async function assertBackupDownloadAccess(raw: unknown, client: Pick = pool) { + const authority = backupDownloadAuthoritySchema.parse(raw), connection = await effectiveMcpConnection(authority.principal, client) + if (connection.owner_id !== authority.ownerId || !hasPermission(connection.owner_permissions,"mm:backups:write") || !hasPermission(connection.policy.permissions,"mm:backups:write") || !connection.policy.scope.operationIds.includes("backups.download") || !connection.policy.scope.serverIds.includes(authority.serverId)) throw new McpError("PERMISSION_DENIED","Нет допуска к полной резервной копии",403) + if (fingerprint(connection.policy) !== authority.policyProof || await accessGeneration() !== authority.generation) throw fail("Допуск изменился; подготовьте скачивание заново","RESET_REQUIRED") + const row = (await client.query("SELECT server_id FROM backup_entries WHERE id=$1 AND EXISTS(SELECT 1 FROM servers WHERE id=backup_entries.server_id)",[authority.backupId])).rows[0] + if (!row || Number(row.server_id)!==authority.serverId) throw fail("Принадлежность резервной копии не подтверждена","SERVER_DENIED") + const denied=(await client.query("SELECT 1 FROM mcp_protections WHERE object_ref->>'protection'='deny' AND (object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) OR (object_ref->>'kind' IN ('backup','backup-download') AND object_ref->>'id'=$2)) LIMIT 1",[String(authority.serverId),authority.backupId])).rows[0] + if(denied)throw fail("Резервная копия заблокирована защитой объекта","PROTECTED_OBJECT") + return { connection, serverIds: [authority.serverId] } +} + +async function sourceMetadata(authority: BackupDownloadAuthority) { + const row=(await pool.query(`SELECT CASE WHEN octet_length(to_jsonb(b)::text)<=32768 THEN to_jsonb(b) ELSE NULL END AS metadata,b.xmin::text AS revision FROM backup_entries b WHERE id=$1`,[authority.backupId])).rows[0] + const storage=(await pool.query(`SELECT CASE WHEN octet_length(to_jsonb(s)::text)<=32768 THEN to_jsonb(s) ELSE NULL END AS metadata,s.xmin::text AS revision FROM backup_storage_settings s WHERE id=1`)).rows[0] + const protections=(await pool.query("SELECT CASE WHEN octet_length(object_ref::text)<=32768 THEN object_ref ELSE NULL END AS object_ref,xmin::text AS revision FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) OR (object_ref->>'kind' IN ('backup','backup-download') AND object_ref->>'id'=$2) ORDER BY object_ref::text LIMIT 1001",[String(authority.serverId),authority.backupId])).rows + if(!row?.metadata || (storage && !storage.metadata)) throw fail("Источник резервной копии превышает предел или отсутствует","STATE_UNAVAILABLE") + if(protections.length>1000 || protections.some(row=>!row.object_ref))throw fail("Защиты резервной копии превышают предел","STATE_UNAVAILABLE") + return {backup:row.metadata,backupRevision:row.revision,storage:storage?.metadata??null,storageRevision:storage?.revision??null,protections} +} +export async function readBackupDownloadSource(authority: BackupDownloadAuthority, authorize: () => Promise = async()=>{}, signal?: AbortSignal) { + const deadline=AbortSignal.timeout(45000), bounded=signal?AbortSignal.any([signal,deadline]):deadline + const check=async()=>{bounded.throwIfAborted();await authorize();await assertBackupDownloadAccess(authority);bounded.throwIfAborted()} + await check() + const before=await sourceMetadata(authority) + await check() + let content:Buffer + let origin: "s3" | "local" | undefined + try { content=await readBackupContent({id:authority.backupId} as BackupMeta,{maxBytes:MAX_BYTES,signal:bounded,authorize:check,sourceRead:source=>{origin=source}}) } + catch {await check();throw fail("Файл резервной копии недоступен, превышает 8 МиБ или его чтение прервано","STATE_UNAVAILABLE")} + await check() + const after=await sourceMetadata(authority) + if(fingerprint(before)!==fingerprint(after))throw fail("Источник изменился во время чтения","STATE_CONFLICT") + await check() + if(!origin)throw fail("Источник файла не подтверждён","STATE_UNAVAILABLE") + const filename=String(after.backup.filename).replace(/[^a-zA-Z0-9._-]/g,"_").slice(0,255) || "backup.bin" + return {content,filename,serverIds:[authority.serverId],proof:await sign({metadata:after,origin,content:content.toString("base64")})} +} + +export async function createBackupDownloadArtifact(authority: BackupDownloadAuthority, expectedProof: string) { + const authorize = assertVerifiedConfigurationAccess + return executeGuardedApplicationWrite({kind:"backup-download",id:authority.backupId},(await assertBackupDownloadAccess(authority)).serverIds,async()=>{ + const source = await readBackupDownloadSource(authority,authorize) + if (source.proof!==expectedProof) throw fail("Резервная копия изменилась после подтверждения","STATE_CONFLICT") + const ciphertext = await encryptSecret(source.content.toString("base64")) + if(Buffer.byteLength(ciphertext)>MAX_CIPHER) throw fail("Зашифрованный пакет превышает предел","EXPORT_LIMIT") + const header: Header = { id: randomUUID(), connection_id: authority.principal.connectionId, filename: source.filename, media_type: "application/octet-stream", expires_at: new Date(Date.now()+900000) } + const access = { version: 1 as const, kind: "backup-download" as const, authority, sourceProof: source.proof, bytes: source.content.length, sha256: hash(source.content), encryptedSha256: hash(ciphertext) } + const scope = scopeSchema.parse({...access,proof:await sign(signed(header,access))}) + await authorize() + if ((await readBackupDownloadSource(authority,authorize)).proof!==source.proof) throw fail("Конфигурация изменилась во время формирования пакета","STATE_CONFLICT") + await connectionWrite(async client=>{ + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE",[header.connection_id]) + await assertBackupDownloadAccess(authority,client) + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))") + await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()") + const usage = (await client.query(`SELECT count(*) FILTER(WHERE connection_id=$1)::text AS files,COALESCE(sum(octet_length(ciphertext)) FILTER(WHERE connection_id=$1),0)::text AS own_bytes,COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts`,[header.connection_id])).rows[0] + assertLogArtifactBudget(BigInt(usage.files),BigInt(usage.own_bytes),BigInt(usage.total_bytes),Buffer.byteLength(ciphertext)) + await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)",[header.id,header.connection_id,header.filename,header.media_type,ciphertext,JSON.stringify(scope),header.expires_at]) + }) + await authorize();await assertBackupDownloadAccess(authority) + return {artifactId:header.id,downloadPath:`/api/mcp/artifacts/${header.id}`,expiresInSeconds:900,bytes:access.bytes,sha256:access.sha256,humanDownloadOnly:true,backupContentReturned:false,configurationApplied:false} + }) +} + +async function loadMeta(id: string): Promise { + const row = (await pool.query("/* MCP backup artifact metadata */ SELECT id,connection_id,filename,media_type,expires_at,CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope,octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND expires_at>now() AND octet_length(filename)<=1024 AND octet_length(media_type)<=200",[id])).rows[0] + if (!row) throw new McpError("NOT_FOUND","Пакет отсутствует или срок истёк",404) + const scope = scopeSchema.safeParse(row.access_scope) + if (!scope.success || !(row.expires_at instanceof Date) || !Number.isFinite(row.expires_at.getTime()) || !Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes>MAX_CIPHER) throw fail("Метаданные пакета не подтверждены") + const {proof,...access} = scope.data, expected = await sign(signed(row,access)) + if (!timingSafeEqual(Buffer.from(proof,"hex"),Buffer.from(expected,"hex")) || row.connection_id!==access.authority.principal.connectionId) throw fail("Подпись пакета не совпала") + return {...row,access_scope:scope.data} +} +export async function verifyBackupDownloadArtifact(id: string, authority: BackupDownloadAuthority, sourceProof: string) { + const meta=await loadMeta(id) + await assertBackupDownloadAccess(authority) + if(fingerprint(meta.access_scope.authority)!==fingerprint(authority) || meta.access_scope.sourceProof!==sourceProof)return false + const payload=(await pool.query<{ciphertext:string}>("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=16777216",[id,meta.access_scope.proof])).rows[0] + await assertBackupDownloadAccess(authority) + return !!payload && hash(payload.ciphertext)===meta.access_scope.encryptedSha256 && (await readBackupDownloadSource(authority)).proof===sourceProof +} +/** Receipt listing validates sealed metadata and access without reading source or artifact payload. */ +export async function humanBackupDownloadMetadata(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent) { + const meta = await loadMeta(id), scope = meta.access_scope + await checkClaims(actor) + if (actor.id !== scope.authority.ownerId || !hasPermission(actor.permissions, "mm:backups:write")) throw new McpError("PERMISSION_DENIED", "Файл доступен только владельцу", 403) + await assertBackupDownloadAccess(scope.authority) + return scope +} +export async function readHumanBackupDownloadArtifact(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent, signal?:AbortSignal) { + const deadline=AbortSignal.timeout(45000),bounded=signal?AbortSignal.any([signal,deadline]):deadline + bounded.throwIfAborted() + const meta=await loadMeta(id), scope=meta.access_scope + const authorize=async()=>{ + bounded.throwIfAborted() + await checkClaims(actor) + if(actor.id!==scope.authority.ownerId || !hasPermission(actor.permissions,"mm:backups:write")) throw new McpError("PERMISSION_DENIED","Резервная копия доступна только владельцу с правом управления backup",403) + await assertBackupDownloadAccess(scope.authority) + if(meta.expires_at.getTime()<=Date.now()) throw fail("Срок пакета истёк") + } + await authorize() + if((await readBackupDownloadSource(scope.authority,authorize,bounded)).proof!==scope.sourceProof) throw fail("Резервная копия изменилась; подготовьте новый пакет","STATE_CONFLICT") + const payload=(await pool.query<{ciphertext:string}>("/* MCP backup artifact payload */ SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=16777216",[id,scope.proof])).rows[0] + await authorize() + if(!payload || hash(payload.ciphertext)!==scope.encryptedSha256) throw fail("Зашифрованный пакет изменился") + let content: Buffer + try{content=Buffer.from(await decryptSecret(payload.ciphertext),"base64")}catch{throw fail("Пакет не расшифрован")} + await authorize() + if(Buffer.byteLength(content)!==scope.bytes || hash(content)!==scope.sha256) throw fail("Размер или SHA-256 пакета не совпал") + if((await readBackupDownloadSource(scope.authority,authorize,bounded)).proof!==scope.sourceProof) throw fail("Конфигурация изменилась во время выдачи пакета","STATE_CONFLICT") + await authorize() + return {filename:meta.filename,mediaType:meta.media_type,content} +} + +export function backupDownloadAuthority(principal: McpPrincipal, ownerId: string, policy: unknown, generation: string, backupId:string, serverId:number) { + return backupDownloadAuthoritySchema.parse({principal,ownerId,policyProof:fingerprint(policy),generation,backupId,serverId}) +} diff --git a/backend/src/modules/mcp/backup-storage-actions.test.ts b/backend/src/modules/mcp/backup-storage-actions.test.ts new file mode 100644 index 00000000..0bdade30 --- /dev/null +++ b/backend/src/modules/mcp/backup-storage-actions.test.ts @@ -0,0 +1,10 @@ +import assert from "node:assert/strict" +import { backupStorageSecretPurpose as purpose } from "./backup-storage-actions.js" +const destination={s3Endpoint:"https://s3.example.test",s3Region:"us-east-1",s3Bucket:"backups"} +assert.equal(purpose(destination),"backup-s3:https://s3.example.test|us-east-1|backups") +assert.equal(purpose({...destination,s3Endpoint:""}),"backup-s3:aws-default|us-east-1|backups") +assert.equal(purpose({...destination,s3Endpoint:" https://s3.example.test ",s3Bucket:" backups "}),purpose(destination)) +for(const patch of [{s3Endpoint:"https://user:password@s3.example.test"},{s3Endpoint:"https://s3.example.test?secret=x"},{s3Endpoint:"https://s3.example.test#x"},{s3Endpoint:"ftp://s3.example.test"},{s3Region:""},{s3Bucket:" "},{s3Region:"a|b"},{s3Bucket:"a|b"}])assert.throws(()=>purpose({...destination,...patch})) +assert.notEqual(purpose({...destination,s3Endpoint:"https://other.example.test"}),purpose(destination)) +assert.notEqual(purpose({...destination,s3Bucket:"other"}),purpose(destination)) +console.log("Backup S3 secret destination: exact endpoint/region/bucket binding, AWS-default target, delimiter collision, URL credentials and incomplete target guards passed") diff --git a/backend/src/modules/mcp/backup-storage-actions.ts b/backend/src/modules/mcp/backup-storage-actions.ts new file mode 100644 index 00000000..e90e9e19 --- /dev/null +++ b/backend/src/modules/mcp/backup-storage-actions.ts @@ -0,0 +1,82 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { readBackupStorageMutationState, updateBackupStorageSettings } from "../../services/backup-service.js" +import { decryptSecret, fingerprint, privateSecretFingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" + +const endpoint=z.string().trim().max(350).refine(value=>{ + if(!value)return true + try {const url=new URL(value);return ["http:","https:"].includes(url.protocol)&&!url.username&&!url.password&&!url.search&&!url.hash}catch{return false} +},"Endpoint должен быть HTTP(S) URL без credentials/query/hash") +const destinationPart=z.string().trim().max(64).refine(value=>!/[|\r\n]/.test(value),"Недопустимый разделитель назначения S3") +const fields=z.object({provider:z.enum(["local","s3"]).optional(),s3Endpoint:endpoint.optional(),s3Region:destinationPart.optional(),s3Bucket:destinationPart.optional(),s3Prefix:z.string().max(1024).optional(),s3AccessKeyId:z.string().max(256).optional(),s3ForcePathStyle:z.boolean().optional(),keepLocalCopy:z.boolean().optional()}).strict() +const schema=z.object({settings:fields.default({}),secretRef:z.string().uuid().optional()}).strict().refine(value=>Object.keys(value.settings).length>0||Boolean(value.secretRef),"Нужно изменение настроек или ссылка на секрет") +type State=Awaited> +export function backupStorageSecretPurpose(configuration:Pick) { + const parsed=endpoint.safeParse(configuration.s3Endpoint) + const region=destinationPart.safeParse(configuration.s3Region),bucket=destinationPart.safeParse(configuration.s3Bucket) + if(!parsed.success||!region.success||!bucket.success||!region.data||!bucket.data)throw new McpError("STATE_CONFLICT","Не подтверждены endpoint/region/bucket назначения S3",409) + const purpose=`backup-s3:${parsed.data||"aws-default"}|${region.data}|${bucket.data}` + if(purpose.length>512)throw new McpError("INVALID_INPUT","Назначение секрета S3 превышает предел",400) + return purpose +} +async function selectedServers() { + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>Number(row.id)) + if(ids.length>1000)throw new McpError("STATE_UNAVAILABLE","Каталог серверов превышает предел проверки",503) + return ids +} +async function resolveReference(input:Record,state:State) { + if(!input.secretRef)return undefined + const next={...state.configuration,...fields.parse(input.settings)} + const {rows:[secret]}=await pool.query("SELECT purpose,server_id,ciphertext,created_by FROM mcp_secrets WHERE id=$1 AND created_by=$2",[input.secretRef,input.ownerId]) + if(!secret||secret.server_id!==null||secret.purpose!==backupStorageSecretPurpose(next))throw new McpError("SECRET_UNAVAILABLE","Секрет не выдан владельцу для выбранного хранилища S3",403) + const parsed=z.string().min(1).max(4096).safeParse(await decryptSecret(secret.ciphertext)) + if(!parsed.success)throw new McpError("SECRET_UNAVAILABLE","Формат выданного секрета S3 не поддерживается",403) + const value=parsed.data + return {value,referenceFingerprint:fingerprint(secret)} +} +async function current(input:Record) { + const serverIds=await selectedServers(),state=await readBackupStorageMutationState() + if(!fields.required().safeParse(state.configuration).success)throw new McpError("STATE_UNAVAILABLE","Текущая конфигурация S3 не подтверждена; endpoint не должен содержать credentials",503) + const next={...state.configuration,...fields.parse(input.settings)} + if(state.busy)throw new McpError("STATE_CONFLICT","Хранилище занято созданием или синхронизацией архивов",409) + if(state.hasRemoteEntries&&(next.provider!=="s3"||(["s3Endpoint","s3Region","s3Bucket","s3ForcePathStyle"] as const).some(key=>next[key]!==state.configuration[key])))throw new McpError("STATE_CONFLICT","Существующие S3-архивы требуют сохранения исходного подключения",409) + // An empty bucket cannot construct an S3 client. Clearing dormant settings is + // allowed, but restoring a usable destination requires its own reference. + if(state.secretConfigured&&next.s3Bucket.trim()&&!input.secretRef&&(["s3Endpoint","s3Region","s3Bucket"] as const).some(key=>next[key]!==state.configuration[key]))throw new McpError("SECRET_UNAVAILABLE","Перенос сохранённого секрета на новое назначение S3 требует выданной этому назначению ссылки",403) + const reference=await resolveReference(input,state) + return {...state,serverIds,secretRefFingerprint:reference?.referenceFingerprint??null} +} +export function registerBackupStorageActions(register:typeof add) { + registerMcpService("backups.storage.update",{current,execute:async input=>{ + const before=await current(input) + if(fingerprint(before)!==input.beforeFingerprint)throw new McpError("STATE_CONFLICT","Настройки, секрет, архивы или серверы изменились после подтверждения",409) + const reference=await resolveReference(input,before) + if((reference?.referenceFingerprint??null)!==input.secretRefFingerprint)throw new McpError("STATE_CONFLICT","Ссылка на секрет изменилась перед записью",409) + const patch={...fields.parse(input.settings),...(reference?{s3SecretAccessKey:reference.value}:{})} + await updateBackupStorageSettings(patch) + return {configurationSaved:true,configurationFingerprint:fingerprint({...before.configuration,...fields.parse(input.settings)}),secretFingerprint:reference?await privateSecretFingerprint(reference.value,"backup-storage"):before.secretFingerprint,storageTestVerified:false} + },verify:async(_input,result)=>{ + const receipt=result as {configurationSaved?:boolean;configurationFingerprint?:string;secretFingerprint?:string}|null + if(receipt?.configurationSaved!==true)return false + const saved=await readBackupStorageMutationState() + return saved.secretFingerprint===receipt.secretFingerprint&&fingerprint(saved.configuration)===receipt.configurationFingerprint + }}) + register("backups.storage.update","mm_backup_storage_update_plan","Изменение конфигурации backup-хранилища с отдельной ссылкой на секрет","backups","mm:backups:write","write","global",schema,{ + prepare:async(input,context)=>{ + const ids=await selectedServers() + if(ids.some(id=>!context.connection.policy.scope.serverIds.includes(id)))throw new McpError("SERVER_DENIED","Глобальное хранилище затрагивает серверы вне разрешённой области",403) + const request={...input,ownerId:context.connection.owner_id},before=await current(request) + if(before.serverIds.join(",")!==ids.join(","))throw new McpError("STATE_CONFLICT","Каталог серверов изменился во время подготовки",409) + const objects:ChangeEvidence["objects"]=[{kind:"backup-storage",id:"1"},...ids.map(serverId=>({kind:"server",id:String(serverId),serverId}))] + const {rows:protections}=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[])) LIMIT 1001",[ids.map(String)]) + if(protections.length>1000)throw new McpError("STATE_UNAVAILABLE","Превышен предел проверки защит",503) + for(const {object_ref:ref} of protections)objects.push({kind:ref.kind,id:ref.id,serverId:ref.serverId}) + return {target:{scope:"global"},objects,snapshots:[],before,requiresHuman:true,mutationCost:1, + steps:[{kind:"service",service:"backups.storage.update",input:{...request,beforeFingerprint:fingerprint(before),secretRefFingerprint:before.secretRefFingerprint}}], + warnings:["Глобальная конфигурация backup-хранилища изменяется для всех серверов приложения.","Смена credentials/prefix может повлиять на последующие загрузки и доступ к архивам. Проверка S3 выполняется отдельным подтверждённым действием.","Секрет передаётся только через выданную владельцу ссылку с purpose backup-s3:||.","Занятое созданием/синхронизацией хранилище отклонит изменение; автоматического повтора нет."]} + }, + }) +} diff --git a/backend/src/modules/mcp/bandwidth-service-context.sql-helper.ts b/backend/src/modules/mcp/bandwidth-service-context.sql-helper.ts new file mode 100644 index 00000000..97aa109f --- /dev/null +++ b/backend/src/modules/mcp/bandwidth-service-context.sql-helper.ts @@ -0,0 +1,200 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +/** Owned SQL-only acceptance of the genuine production runner's private service + * context. No RouterOS request, bandwidth admission or production registration. */ +import assert from "node:assert/strict" +import { randomUUID, createHash } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { isDeepStrictEqual } from "node:util" +import type { McpPrincipal } from "./store.js" +import type { VerifiedMcpServiceExecution } from "../../services/config-mutation-policy.js" +import { assertOwnedBandwidthSql } from "../../services/bandwidth-admission-process.sql-helper.js" + +export async function acceptBandwidthServiceContextSql(serverIds: readonly number[]) { + assertOwnedBandwidthSql() + assert.equal(serverIds.length, 3); assert.equal(new Set(serverIds).size, 3) + assert(serverIds.every(id => Number.isSafeInteger(id) && id > 0)) + const { pool } = await import("../../db/index.js") + const { z } = await import("zod") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { add } = await import("./actions.js") + const { registerMcpService } = await import("./service-registry.js") + const { createMcpConnection, authenticateMcpToken } = await import("./store.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution } = await import("../../services/config-mutation-policy.js") + const { readCurrentMcpServiceExecution } = await import("./service-execution-origin.js") + const { readMcpTemporaryGrantOrigin } = await import("./temporary-grant-origin.js") + const ids = [...serverIds].sort((a, b) => a - b) + assert.equal((await pool.query("SELECT id FROM public.servers WHERE id=ANY($1::bigint[])", [ids])).rows.length, 3) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + const originalEnabled = (await pool.query("SELECT enabled FROM public.mcp_settings WHERE id=1")).rows[0]?.enabled + assert.equal(typeof originalEnabled, "boolean") + const suffix = randomUUID().replaceAll("-", ""), operationName = "fixture.bandwidth.context." + suffix + const serviceName = "fixture.bandwidth.service-context." + suffix, toolName = "mm_fixture_bw_context_" + suffix + const secret = "SYNTHETIC_PRIVATE_SERVICE_PASSWORD_" + randomUUID() + const before = { fixture: "owned-sealed-service-state" }, connections: string[] = [], tokens: VerifiedMcpServiceExecution[] = [] + const checks: string[] = [], outcomes: boolean[] = [] + type Expected = { principal: McpPrincipal; ownerId: string; connectionId: string; approved: boolean; evidenceExpiry: number;grantExpiresAt:number|null;grantRowId:string|null } + const expectedPlans = new Map(), seenSteps = new Map() + let preparedExpiry = 0, verified = 0, currentPlanChecks = 0, rejectedPlanChanges = 0, rejectedGrantChanges=0, pendingReplayRefusals=0 + const privateInput = (stepIndex: number) => ({ fixtureStep: stepIndex, password: secret, serverIds: ids }) + const safe = (value: unknown) => { assert(!JSON.stringify(value).includes(secret), "Private service material escaped") } + registerMcpService(serviceName, { + current: async () => before, + verify: async (_input, result) => { + assert.deepEqual(result, { fixtureServiceReceipt: true }); safe(result); verified++; return true + }, + execute: async (input, context) => { + assert(context && context.phase === "forward") + const token = await captureVerifiedMcpServiceExecution(), inspected = await inspectVerifiedMcpServiceExecution(token) + const originalScope={deadlineAt:Date.now()+10000} + assert(isDeepStrictEqual(await readCurrentMcpServiceExecution(token,originalScope),inspected));currentPlanChecks++ + const expected = expectedPlans.get(inspected.planId); assert(expected) + const currentGrant=await readMcpTemporaryGrantOrigin(pool,inspected.connectionId,inspected.principal.credentialId) + assert.equal(inspected.originalTemporaryGrantProof,currentGrant.proof);assert.equal(inspected.originalTemporaryGrantExpiresAt,expected.grantExpiresAt) + assert.equal(context.operationId, inspected.operationId); assert.equal(context.stepIndex, inspected.stepIndex) + assert.equal(inspected.stepIndex, input.fixtureStep) + assert(isDeepStrictEqual(input, privateInput(inspected.stepIndex)), "Decrypted runner step input mismatch") + assert(isDeepStrictEqual(inspected.serviceInput, privateInput(inspected.stepIndex)), "Private inspected service input mismatch") + assert(inspected.serviceInput.password === secret, "Private inspected credential mismatch") + assert.equal(inspected.operationName, operationName); assert.equal(inspected.serviceName, serviceName); assert.equal(inspected.phase, "forward") + assert.equal(inspected.operationVersion, 2); assert.deepEqual(inspected.principal, expected.principal) + assert.equal(inspected.connectionId, expected.connectionId); assert.equal(inspected.ownerId, expected.ownerId) + assert.equal(inspected.approved, expected.approved); assert.equal(inspected.serverWide, true) + assert.deepEqual([...inspected.serverIds].sort((a, b) => a - b), ids) + assert.deepEqual(inspected.objects, ids.map(serverId => ({ kind: "server", id: String(serverId), serverId }))) + const row = (await pool.query(`SELECT o.state AS operation_state,p.state AS plan_state,s.state AS step_state, + o.plan_id,o.connection_id,p.operation_id,p.operation_version,p.policy_version,p.generation,p.expires_at, + p.approved_by,p.evidence,p.payload,c.owner_id,c.owner_valid_until,c.policy,s.intended_state + FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id + JOIN public.mcp_connections c ON c.id=o.connection_id + JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=$2 WHERE o.id=$1`, [context.operationId, context.stepIndex])).rows[0] + assert(row); assert.equal(row.operation_state, "running"); assert.equal(row.plan_state, "executing"); assert.equal(row.step_state, "intent") + assert.equal(row.plan_id, inspected.planId); assert.equal(row.connection_id, inspected.connectionId); assert.equal(row.owner_id, inspected.ownerId) + assert.equal(row.operation_id, inspected.operationName); assert.equal(row.operation_version, inspected.operationVersion) + assert.equal(row.policy_version, inspected.policyVersion); assert.equal(row.generation, inspected.generation) + assert.equal(inspected.sealedEvidenceProof,createHash("sha256").update(row.evidence.sealed).digest("hex")) + assert.equal(inspected.sealedPayloadProof,createHash("sha256").update(row.payload.sealed).digest("hex")) + assert.equal(!!row.approved_by, expected.approved) + safe(row.intended_state); assert.equal(row.intended_state.input.password, "[redacted]") + if(inspected.stepIndex===0){ + const replacementId=randomUUID(),grantPayload={credentialId:inspected.principal.credentialId,id:randomUUID(),startsAt:new Date(Date.now()-1000).toISOString(),expiresAt:new Date(Date.now()+100000).toISOString(),scope:{serverIds:ids,operationIds:[operationName],allowGlobal:false}} + if(expected.grantRowId){ + const originalGrant=(await pool.query("SELECT payload FROM public.mcp_grants WHERE id=$1",[expected.grantRowId])).rows[0].payload + await pool.query("UPDATE public.mcp_grants SET payload=jsonb_set(payload,'{expiresAt}',to_jsonb($2::text)) WHERE id=$1",[expected.grantRowId,new Date(expected.grantExpiresAt!+1000).toISOString()]) + try{await assert.rejects(readCurrentMcpServiceExecution(token,{deadlineAt:Date.now()+10000}));rejectedGrantChanges++} + finally{await pool.query("UPDATE public.mcp_grants SET payload=$2::jsonb WHERE id=$1",[expected.grantRowId,JSON.stringify(originalGrant)])} + await pool.query("UPDATE public.mcp_grants SET revoked_at=now() WHERE id=$1",[expected.grantRowId]) + try{ + await pool.query("INSERT INTO public.mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)",[replacementId,inspected.connectionId,JSON.stringify(originalGrant)]) + await assert.rejects(readCurrentMcpServiceExecution(token,{deadlineAt:Date.now()+10000}));rejectedGrantChanges++ + }finally{await pool.query("DELETE FROM public.mcp_grants WHERE id=$1",[replacementId]);await pool.query("UPDATE public.mcp_grants SET revoked_at=NULL WHERE id=$1",[expected.grantRowId])} + }else{ + await pool.query("INSERT INTO public.mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)",[replacementId,inspected.connectionId,JSON.stringify(grantPayload)]) + try{await assert.rejects(readCurrentMcpServiceExecution(token,{deadlineAt:Date.now()+10000}));rejectedGrantChanges++} + finally{await pool.query("DELETE FROM public.mcp_grants WHERE id=$1",[replacementId])} + } + await readCurrentMcpServiceExecution(token,{deadlineAt:Date.now()+10000}) + // Only this fixture's active step/plan is changed and restored. The + // runtime is awaiting this handler; no native operation is involved. + const changes=[ + {change:"UPDATE public.mcp_operation_steps SET intended_state='{}'::jsonb WHERE operation_id=$1 AND step_index=0",restore:"UPDATE public.mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1 AND step_index=0",id:inspected.operationId,value:JSON.stringify(row.intended_state)}, + {change:"UPDATE public.mcp_plans SET evidence=jsonb_set(evidence,'{sealed}',to_jsonb((evidence->>'sealed')||'!')) WHERE id=$1",restore:"UPDATE public.mcp_plans SET evidence=$2::jsonb WHERE id=$1",id:inspected.planId,value:JSON.stringify(row.evidence)}, + {change:"UPDATE public.mcp_plans SET payload=jsonb_set(payload,'{sealed}',to_jsonb((payload->>'sealed')||'!')) WHERE id=$1",restore:"UPDATE public.mcp_plans SET payload=$2::jsonb WHERE id=$1",id:inspected.planId,value:JSON.stringify(row.payload)}, + {change:"UPDATE public.mcp_plans SET operation_version=operation_version+1 WHERE id=$1",restore:"UPDATE public.mcp_plans SET operation_version=$2 WHERE id=$1",id:inspected.planId,value:row.operation_version}, + ] + for(const change of changes){ + await pool.query(change.change,[change.id]) + try{await assert.rejects(readCurrentMcpServiceExecution(token,{deadlineAt:Date.now()+10000}));rejectedPlanChanges++} + finally{await pool.query(change.restore,[change.id,change.value])} + await readCurrentMcpServiceExecution(token,{deadlineAt:Date.now()+10000});currentPlanChecks++ + } + } + assert.equal(inspected.originalExpiresAt, Math.min(row.expires_at.getTime(), expected.evidenceExpiry, + Date.parse(expected.principal.credentialExpiresAt), Date.parse(row.policy.expiresAt), row.owner_valid_until.getTime(),expected.grantExpiresAt??Infinity)) + assert(inspected.originalExpiresAt > Date.now()); assert(Object.isFrozen(inspected)); assert(Object.isFrozen(inspected.serviceInput)) + await assert.rejects(inspectVerifiedMcpServiceExecution({ ...token })) + if (tokens.length) await assert.rejects(inspectVerifiedMcpServiceExecution(tokens[tokens.length - 1])) + tokens.push(token) + const seen = seenSteps.get(inspected.planId) ?? []; seen.push(inspected.stepIndex); seenSteps.set(inspected.planId, seen) + outcomes.push(inspected.approved) + return { fixtureServiceReceipt: true } + }, + }) + add(operationName, toolName, "Owned bandwidth runner context fixture", "servers", "mm:servers:write", "write", "servers", z.object({}).strict(), { + version: 2, target: () => ({ scope: "servers", serverIds: ids }), prepare: async () => { + preparedExpiry = Date.now() + 120000 + return { target: { scope: "servers", serverIds: ids }, objects: ids.map(serverId => ({ kind: "server" as const, id: String(serverId), serverId })), snapshots: [], + steps: [0, 1].map(stepIndex => ({ kind: "service" as const, service: serviceName, input: privateInput(stepIndex) })), + before, warnings: [], validUntil: new Date(preparedExpiry).toISOString() } + }, + }) + try { + await pool.query("UPDATE public.mcp_settings SET enabled=true WHERE id=1") + await assert.rejects(captureVerifiedMcpServiceExecution()) + for (const approved of [true, false]) { + const actor = { id: randomUUID(), email: "owned-runner@example.test", name: "Synthetic owned runner authority", apps: ["mm"], + permissions: ["mm:servers:write", "mm:servers:read", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 300000 } + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned runner private context", mode: approved ? "approval" : "autonomous", + expiresAt: new Date(actor.expiresAt).toISOString(), permissions: ["mm:servers:write", "mm:servers:read"], + scope: { serverIds: ids, operationIds: [operationName], allowGlobal: false }, + ...(approved ? {} : { dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: [operationName] }), + limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connections.push(access.connection.id) + const principal = await authenticateMcpToken(access.token) + let grantRowId:string|null=null,grantExpiresAt:number|null=null + if(approved){ + grantRowId=randomUUID();grantExpiresAt=Date.now()+90000 + await pool.query("INSERT INTO public.mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)",[grantRowId,access.connection.id,JSON.stringify({credentialId:principal.credentialId,id:randomUUID(),startsAt:new Date(Date.now()-1000).toISOString(),expiresAt:new Date(grantExpiresAt).toISOString(),scope:{serverIds:ids,operationIds:[operationName],allowGlobal:false}})]) + } + const prepared = await invokeMcpTool(principal, toolName, {}) as { planId: string; state: string } + safe(prepared); assert.equal(prepared.state, approved ? "awaiting-approval" : "draft") + expectedPlans.set(prepared.planId, { principal, ownerId: actor.id, connectionId: access.connection.id, approved, evidenceExpiry: preparedExpiry,grantExpiresAt,grantRowId }) + if (approved) await approvePlan(prepared.planId, actor, true) + const operation = await applyPlan(principal, prepared.planId, randomUUID()) + let terminal: { state: string; result: unknown; error_code: unknown } | undefined + for (const until = Date.now() + 30000; Date.now() < until;) { + const row = (await pool.query("SELECT state,result,error_code FROM public.mcp_operations WHERE id=$1", [operation.id])).rows[0] + assert(row) + if (!["pending", "running"].includes(row.state)) { terminal = row; break } + await delay(20) + } + assert(terminal, "Owned runner did not reach terminal state"); assert.equal(terminal.state, "applied", "Owned runner did not apply SQL-only fixture steps") + safe(terminal); assert.deepEqual(seenSteps.get(prepared.planId), [0, 1]) + const steps = (await pool.query("SELECT state,intended_state,result FROM public.mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [operation.id])).rows + assert.equal(steps.length, 2); assert(steps.every(row => row.state === "verified")); safe(steps) + assert.equal((await pool.query("SELECT approved_by FROM public.mcp_plans WHERE id=$1", [prepared.planId])).rows[0].approved_by !== null, approved) + for (const token of tokens) await assert.rejects(inspectVerifiedMcpServiceExecution(token)) + // Simulate an owned pending row surviving without its admission closure. + // Re-apply may return it, but must never manufacture a new grant pin or + // schedule another service continuation from the current access alone. + const previousTokens=tokens.length + await pool.query("UPDATE public.mcp_operations SET state='pending' WHERE id=$1",[operation.id]) + try{ + const replay=await applyPlan(principal,prepared.planId,randomUUID()) + assert.equal(replay.id,operation.id);await delay(150) + assert.equal((await pool.query("SELECT state FROM public.mcp_operations WHERE id=$1",[operation.id])).rows[0].state,"pending") + assert.equal(tokens.length,previousTokens);pendingReplayRefusals++ + }finally{await pool.query("UPDATE public.mcp_operations SET state='applied' WHERE id=$1",[operation.id])} + checks.push(approved ? "human-approved genuine production runner: two exact sealed service steps, SQL running/executing/intent provenance, private decrypted input and redacted public intent; tokens expire on step exit" + : "explicit autonomous dangerous-operation grant: two genuine service steps with approved=false, same principal/plan/generation/expiry provenance, verification and no human approval fabrication") + } + assert.equal(verified, 4); assert.deepEqual(outcomes, [true, true, false, false]);assert.equal(currentPlanChecks,12);assert.equal(rejectedPlanChanges,8);assert.equal(rejectedGrantChanges,3) + assert.equal(pendingReplayRefusals,2) + return { checks, flows: 2, verifiedServiceSteps: verified, productionRunnerTokens: tokens.length, currentPlanChecks, rejectedPlanChanges, rejectedGrantChanges,pendingReplayRefusals, nativeRequests: 0, operatorAuthoritySynthetic: true, productionActivation: false } + } finally { + try { await pool.query("UPDATE public.mcp_settings SET enabled=$1 WHERE id=1", [originalEnabled]) } + finally { + try { + if (connections.length) { + await pool.query("DELETE FROM public.mcp_operation_checkpoints WHERE operation_id IN (SELECT id FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[]))", [connections]) + await pool.query("DELETE FROM public.mcp_operation_steps WHERE operation_id IN (SELECT id FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[]))", [connections]) + await pool.query("DELETE FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[])", [connections]) + await pool.query("DELETE FROM public.mcp_plans WHERE connection_id=ANY($1::uuid[])", [connections]) + await pool.query("DELETE FROM public.mcp_requests WHERE connection_id=ANY($1::uuid[])", [connections]) + await pool.query("DELETE FROM public.mcp_connections WHERE id=ANY($1::uuid[])", [connections]) + } + } + finally { tokens.splice(0); expectedPlans.clear() } + } + } +} diff --git a/backend/src/modules/mcp/bfd-move-actions.ts b/backend/src/modules/mcp/bfd-move-actions.ts new file mode 100644 index 00000000..2a40e6c9 --- /dev/null +++ b/backend/src/modules/mcp/bfd-move-actions.ts @@ -0,0 +1,65 @@ +import { z } from "zod" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { readRoutingPolicySnapshot, resourceById, rosSnapshotSourceProof, routerClient } from "./resources.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const service = "routing.bfd.configuration.move" +const resource = "bfd_configuration" +const nativeId = z.string().max(200).regex(/^\*[a-z0-9]+$/i) +const schema = z.object({ serverId: z.number().int().positive().safe(), id: nativeId, placeBeforeId: nativeId }).strict().refine(input => input.id !== input.placeBeforeId) + +async function read(input: z.infer, expectedSource?: string) { + const snapshot = await readRoutingPolicySnapshot(input.serverId, resource, expectedSource) + const rows = snapshot.rows + if (rows.some(row => !nativeId.safeParse(row[".id"]).success) || !snapshot.sourceProof) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены ID каталога BFD", 409) + const target = rows.find(row => row[".id"] === input.id) + const anchor = rows.find(row => row[".id"] === input.placeBeforeId) + if (!target || !anchor) throw new McpError("NOT_FOUND", "Правило или позиция BFD не найдены", 404) + if ([target, anchor].some(row => ["dynamic", "builtin", "default"].some(key => row[key] !== null && ![false, "false", "no"].includes(row[key] as boolean | string)))) { + throw new McpError("PROTECTED_OBJECT", "Перемещение требует статического правила и позиции BFD", 409) + } + const moved = rows.filter(row => row !== target) + moved.splice(moved.indexOf(anchor), 0, target) + // RouterOS changes .nextid when it relinks the list. The ordered ID vector + // proves position, and the separate raw snapshot still fences pre-write drift. + const orderedRuleProof = (items: typeof rows) => fingerprint(items.map(row => [row[".id"], row.ruleProof])) + return { order: rows.map(row => String(row[".id"])), proof: snapshot.fingerprint, orderedProof: orderedRuleProof(rows), source: snapshot.sourceProof, desiredProof: orderedRuleProof(moved) } +} + +export function registerBfdMoveActions(register: typeof add) { + const current = (input: Record) => read(schema.parse(input.move), String(input.source)) + registerMcpService(service, { + current, + execute: async input => { + const move = schema.parse(input.move), { client, server } = await routerClient(move.serverId) + if (await rosSnapshotSourceProof(server) !== input.source) throw new McpError("STATE_CONFLICT", "Источник BFD изменился перед отправкой", 409) + await client.post(resourceById(resource).path + "/move", { ".id": move.id, destination: move.placeBeforeId }, 15000, undefined, async () => { + await assertVerifiedConfigurationAccess() + const state = await current(input) + if (state.proof !== input.beforeProof || state.desiredProof !== input.afterProof) throw new McpError("STATE_CONFLICT", "Каталог BFD изменился перед отправкой", 409) + await assertVerifiedConfigurationAccess() + }) + return { acknowledged: true } + }, + verify: async input => (await current(input)).orderedProof === input.afterProof, + reconcile: async (input, authorize) => { + await authorize() + const { proof, orderedProof } = await current(input) + await authorize() + return { matchesOriginalState: proof === input.beforeProof, matchesDesiredState: orderedProof === input.afterProof } + }, + }) + register(service, "mm_bfd_configuration_move_plan", "Перемещение правила BFD перед указанной записью", "network", "mm:network:write", "write", "servers", schema, { + version: 2, + prepare: async input => { + const move = schema.parse(input), before = await read(move) + if (before.orderedProof === before.desiredProof) throw new McpError("STATE_CONFLICT", "Правило BFD уже находится в указанной позиции", 409) + return { target: { scope: "servers", serverIds: [move.serverId] }, objects: before.order.map(id => ({ kind: resource, id, serverId: move.serverId })), + snapshots: [], before, requiresHuman: true, verificationScope: "configuration", + steps: [{ kind: "service", service, input: { serverId: move.serverId, move, source: before.source, beforeProof: before.proof, afterProof: before.desiredProof } }], + warnings: ["Перемещение меняет порядок сопоставления правил BFD. Проверьте влияние на действующие сессии."] } + }, + }) +} diff --git a/backend/src/modules/mcp/bgp-community-policy-actions.ts b/backend/src/modules/mcp/bgp-community-policy-actions.ts new file mode 100644 index 00000000..b30b75a4 --- /dev/null +++ b/backend/src/modules/mcp/bgp-community-policy-actions.ts @@ -0,0 +1,101 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { bgpCommunityPolicySchema, compileBgpCommunityPolicy, readCompiledBgpCommunityPolicy } from "../../services/bgp-community-policy-model.js" +import { readOwnedRoutingPolicy, mutateOwnedRoutingPolicy } from "../../services/owned-routing-policy.js" +import { nativeBgpRows } from "../../services/bgp-export-model.js" +import { stableBgpPolicyRow } from "../../services/bgp-filter-binding-model.js" +import { executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import { routingFilterBodiesMatch } from "../../services/routing-filter-body.js" +import type { ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const schema = bgpCommunityPolicySchema.safeExtend({ serverId: z.number().int().positive().safe(), action: z.enum(["replace", "delete"]).default("replace") }).superRefine((value, context) => { + if (value.action === "delete" && (value.rules.length || value.defaultAction !== "reject")) context.addIssue({ code: "custom", message: "Удаление принимает пустые rules и defaultAction=reject" }) +}) +const service = "routing.bgp.community.policy" +const paths = { bgp_connection: "/routing/bgp/connection", bgp_template: "/routing/bgp/template", bgp_instance: "/routing/bgp/instance", ospf_instance: "/routing/ospf/instance", rip_instance: "/routing/rip/instance", isis_instance: "/routing/isis/instance", routing_filter_select: "/routing/filter/select-rule", routing_table: "/routing/table", vrf: "/ip/vrf" } +const proof = (sid: number, policy: string, kind: string, value: unknown) => privateSecretFingerprint(fingerprint(value), `bgp-community-policy:${sid}:${policy}:${kind}`) +async function state(input: Record) { + try { + const serverId = Number(input.serverId), compiled = compileBgpCommunityPolicy({ policyId: input.policyId, rules: input.rules, defaultAction: input.defaultAction }) + const { server, client } = await routerClient(serverId), serverProof = await proof(serverId, compiled.policy.policyId, "server", server) + if (input.expectedServerProof !== undefined && input.expectedServerProof !== serverProof) throw new McpError("STATE_CONFLICT", "Назначение или credentials policy изменились; новый источник не опрашивается", 409) + const native = await readOwnedRoutingPolicy(client, compiled.chain, compiled.comment) + const snapshots = await Promise.all(Object.entries(paths).map(async ([kind, path]) => { + const rows = nativeBgpRows(await client.get(path)); if (rows.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Каталог policy dependencies превышает 1000 строк", 409) + return { kind, rows } + })) + const references: ChangeEvidence["objects"] = [] + for (const snapshot of snapshots) for (const row of snapshot.rows) { + if (Object.entries(row).some(([key, value]) => (key.includes("filter") || key === "do-where" || snapshot.kind === "routing_filter_select" && key === "chain") && (typeof value === "string" ? value.split(",").includes(compiled.chain) : Array.isArray(value) && value.includes(compiled.chain)))) references.push({ kind: snapshot.kind, id: row[".id"], serverId }) + } + const queryProof = [] + // Native positive queries cover omitted/effective binding properties, including + // every template, rather than treating a missing GET key as a known false. + for (const [kind, path] of [["bgp_connection", "/routing/bgp/connection"], ["bgp_template", "/routing/bgp/template"]] as const) { + const rows = nativeBgpRows(await client.post(path + "/print", { ".proplist": ".id,input.filter,output.filter-chain", ".query": ["input.filter=" + compiled.chain, "output.filter-chain=" + compiled.chain, "#|"] })) + const catalog = snapshots.find(row => row.kind === kind)!.rows + if (rows.length > 1000 || rows.some(row => !catalog.some(source => source[".id"] === row[".id"]) || row["input.filter"] !== compiled.chain && row["output.filter-chain"] !== compiled.chain)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён native query зависимостей policy", 409) + queryProof.push({ kind, rows: rows.map(stableBgpPolicyRow) }) + references.push(...rows.map(row => ({ kind, id: row[".id"], serverId }))) + } + for (const row of native.rows.filter(row => row[".id"] !== native.current?.[".id"])) { + if (typeof row.rule !== "string") throw new McpError("STATE_UNAVAILABLE", "Не подтверждён код других цепочек при проверке references", 409) + const jumps = row.rule.match(/\bjump\b/gi) ?? [], targets = [...row.rule.matchAll(/\bjump\s+(?:"([^"\\]*)"|([A-Za-z0-9_.:@/-]+))\s*(?:[;}]|$)/gi)] + if (jumps.length !== targets.length && input.action === "delete") throw new McpError("STATE_UNAVAILABLE", "Opaque jump references не подтверждены; удаление запрещено", 409) + if (targets.some(match => (match[1] ?? match[2]) === compiled.chain)) references.push({ kind: "routing_filter", id: row[".id"], serverId }) + } + const uniqueReferences = [...new Map(references.map(row => [fingerprint(row), row])).values()] + if (input.action === "delete" && uniqueReferences.length) throw new McpError("STATE_CONFLICT", "Policy используется routing client/template/jump; сначала нужен отдельный план отвязки", 409) + const protections = (await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(serverId)])).rows + if (protections.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Policy protections превышают 1000 объектов", 409) + const objects: ChangeEvidence["objects"] = [{ kind: "bgp-community-policy", id: compiled.policy.policyId, serverId }, ...native.rows.map(row => ({ kind: "routing_filter", id: row[".id"], serverId })), + ...snapshots.flatMap(snapshot => snapshot.rows.map(row => ({ kind: snapshot.kind, id: row[".id"], serverId }))), ...protections.map(({ object_ref: ref }) => ({ kind: ref.kind, id: ref.id, serverId }))] + const uniqueObjects = [...new Map(objects.map(row => [fingerprint(row), row])).values()] + if (uniqueObjects.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Policy dependency closure превышает 1000 объектов", 409) + const desired = input.action === "delete" ? null : compiled.body + const dependencyProof = await proof(serverId, compiled.policy.policyId, "dependencies", { serverProof, snapshots: snapshots.map(snapshot => ({ kind: snapshot.kind, rows: snapshot.rows.map(stableBgpPolicyRow) })), queryProof, protections }) + const previousPolicy = native.current ? readCompiledBgpCommunityPolicy(compiled.policy.policyId, native.current.rule) : null + const before = { serverId, policyId: compiled.policy.policyId, chain: compiled.chain, nativeId: native.current?.[".id"] ?? null, exists: !!native.current, currentDisabled: native.current?.disabled ?? null, previousPolicy, previousPolicyRecognized: !!previousPolicy, previousBodyBytes: native.current ? Buffer.byteLength(String(native.current.rule), "utf8") : 0, references: uniqueReferences, nativeReferenceQueryMatches: queryProof.reduce((sum, query) => sum + query.rows.length, 0), + desiredPolicy: compiled.policy, desiredAction: input.action, compiledBytes: Buffer.byteLength(compiled.body, "utf8"), serverProof, dependencyProof, + nativeProof: await proof(serverId, compiled.policy.policyId, "native", native.proofRows), compiledProof: await proof(serverId, compiled.policy.policyId, "compiled", desired), + nativeDispatch: desired === null ? !!native.current : !native.current || !routingFilterBodiesMatch(native.current.rule, desired) || native.current.disabled !== "false" } + return { server, client, compiled, native, desired, before, objects: uniqueObjects } + } catch (error) { if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Не подтверждены policy или routing dependencies; запись не выполняется", 409) } +} +export function registerBgpCommunityPolicyActions(register: typeof add) { + registerMcpService(service, { + current: async input => (await state(input)).before, + execute: async input => { + const initial = await state(input) + if (fingerprint(initial.before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "Policy или зависимости изменились после подтверждения", 409) + return executeGuardedApplicationWrite({ kind: "bgp-community-policy", id: initial.compiled.policy.policyId }, [initial.server.id], async () => { + const result = await mutateOwnedRoutingPolicy(initial.client, initial.compiled.chain, initial.compiled.comment, initial.desired, async native => { + const live = await state(input) + if (fingerprint(live.before) !== input.beforeFingerprint || fingerprint(native.proofRows) !== fingerprint(live.native.proofRows)) throw new McpError("STATE_CONFLICT", "Policy изменена перед native dispatch", 409) + }) + return { nativeDispatch: result.nativeDispatch, action: result.action, configurationVerified: true, forwardingVerified: false, nativeProof: await proof(initial.server.id, initial.compiled.policy.policyId, "native", result.after.proofRows), dependencyProof: initial.before.dependencyProof } + }, async () => initial.objects) + }, + verify: async (input, result) => { + const after = await state(input), receipt = result as { nativeProof: string; dependencyProof: string } + const nativeMatches = after.before.nativeProof === receipt.nativeProof, dependenciesMatch = after.before.dependencyProof === receipt.dependencyProof + const desiredMatches = after.desired === null ? !after.native.current : routingFilterBodiesMatch(after.native.current?.rule, after.desired) && after.native.current?.disabled === "false" + if (!nativeMatches || !dependenciesMatch || !desiredMatches) console.warn("bgp-community-policy.verification.mismatch", { serverId: after.server.id, nativeMatches, dependenciesMatch, desiredMatches }) + return nativeMatches && dependenciesMatch && desiredMatches + }, + }) + register("routing.bgp.community.policy", "mm_bgp_community_policy_plan", "Человеческий план owned export-цепочки: CIDR и стандартные Community, без raw script; привязка BGP отдельным планом", "bgp", ["mm:filters:write", "mm:bgp:write", "mm:network:read"], "write", "servers", schema, { prepare: async input => { + const saved = await state(input) + return { target: { scope: "servers", serverIds: [saved.server.id] }, objects: saved.objects, snapshots: [], before: saved.before, mutationCost: 1, requiresHuman: true, verificationScope: "configuration", + steps: [{ kind: "service", service, input: { ...input, beforeFingerprint: fingerprint(saved.before), expectedServerProof: saved.before.serverProof } }], + warnings: ["Полностью заменяется и включается одна owned цепочка mmapp-bgp-out-policyId. CIDR приводятся к адресу сети, порядок правил сохраняется. Default reject может отозвать все остальные префиксы.", + "Set/append/clear меняют только стандартные Community. Цепочка не привязывается к peer автоматически; все использующие её routing clients затрагиваются её изменением.", + "Удаление запрещено при native BGP/template, других routing client и jump references. Opaque jump при удалении требует отдельного анализа.", + "Никакого автоматического refresh/повтора/rollback. Обмен attributes, выбор маршрута и forwarding требуют отдельной сетевой проверки.", + ...(saved.native.current && !saved.before.previousPolicyRecognized ? ["Текущее тело owned policy не распознано как точный результат компилятора. PreviousPolicy неизвестна; смысл native кода не восстановлен. Замена удалит это тело целиком."] : [])] } + } }) +} diff --git a/backend/src/modules/mcp/bgp-export-actions.ts b/backend/src/modules/mcp/bgp-export-actions.ts new file mode 100644 index 00000000..47195b55 --- /dev/null +++ b/backend/src/modules/mcp/bgp-export-actions.ts @@ -0,0 +1,131 @@ +import { z } from "zod" +import { readStaticBgpConnection } from "../../services/bgp-connection-read.js" +import { bgpExportConnection, bgpExportSources, bgpRedistributionMode, bgpTemplateReferences, nativeBgpExportValue, nativeBgpRows, type BgpExportMode, type NativeBgpRow } from "../../services/bgp-export-model.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { readRosSnapshot, routerClient, type ConfigurationRow } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import type { ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const modeSchema = z.enum(["disabled", "connected", "static"]).describe("Connected/static перераспределяют соответствующие маршруты таблицы peer. Disabled очищает только redistribution; output.network/default-originate и сам peer не отключаются.") +const schema = z.object({ serverId: z.number().int().positive().safe(), id: z.string().trim().min(1).max(200), mode: modeSchema }).strict() +const service = "routing.bgp.export" +const modes = (values: BgpExportMode[]) => [...new Set(values)].sort() as BgpExportMode[] +const configurationProof = (serverId: number, id: string, value: unknown) => privateSecretFingerprint(fingerprint(value), `bgp-export-connection:${serverId}:${id}`) +function parseState(phase: string, parse: () => T): T { + try { return parse() } catch (error) { + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Не подтверждён этап BGP export: " + phase, 409) + } +} + +async function readState(input: Record) { + const serverId = Number(input.serverId), id = String(input.id), mode = modeSchema.parse(input.mode) + const { server, client } = await routerClient(serverId) + const serverProof = await privateSecretFingerprint(fingerprint(server), `bgp-export-server:${serverId}`) + if (input.expectedServerProof !== undefined && input.expectedServerProof !== serverProof) throw new McpError("STATE_CONFLICT", "Назначение или credentials сервера BGP изменились; новое подключение не опрашивается", 409) + const { connection: hydrated } = await readStaticBgpConnection(client, id) + const localResponse = await client.post("/routing/bgp/connection/get", { ".id": id, "value-name": "output.redistribute" }) + const localMode = parseState("локальное output.redistribute", () => bgpRedistributionMode(localResponse)) + const templates = nativeBgpRows(await client.get("/routing/bgp/template")) + const usedTemplates = new Map() + const resolveTemplate = async (reference: string, stack: string[] = []): Promise => { + const found = templates.filter(row => row[".id"] === reference || row.name === reference) + if (found.length !== 1 || stack.length >= 16 || stack.includes(found[0][".id"])) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён граф шаблонов BGP", 409) + const row = found[0], cached = usedTemplates.get(row[".id"]) + if (typeof row.name !== "string" || !row.name.trim() || row.name.length > 100) throw new McpError("STATE_UNAVAILABLE", "Не подтверждено имя шаблона BGP", 409) + if (cached) return cached.mode + if (usedTemplates.size >= 100) throw new McpError("LIMIT_EXCEEDED", "Граф BGP превышает 100 шаблонов", 409) + const [effective] = await readOspfEffectiveDisabled([row], nativeId => client.post("/routing/bgp/template/get", { ".id": nativeId, "value-name": "disabled" })) + if (!["false", "no"].includes(String(effective.disabled))) throw new McpError("STATE_CONFLICT", "Используемый шаблон BGP отключён или его состояние неизвестно", 409) + const setting = bgpRedistributionMode(await client.post("/routing/bgp/template/get", { ".id": row[".id"], "value-name": "output.redistribute" })) + const refs = bgpTemplateReferences(await client.post("/routing/bgp/template/get", { ".id": row[".id"], "value-name": "templates" })) + const parents: BgpExportMode[] = [] + for (const ref of refs) parents.push(await resolveTemplate(ref, [...stack, row[".id"]])) + if (new Set(parents).size > 1) throw new McpError("STATE_UNAVAILABLE", "Шаблоны BGP задают неоднозначный набор источников", 409) + const result = setting ?? parents[0] ?? "disabled" + const stable = { ...Object.fromEntries(Object.entries(effective).filter(([key]) => !["inactive", "invalid", "running"].includes(key) && !key.startsWith("debug."))), ".id": row[".id"], disabled: "false" } + usedTemplates.set(row[".id"], { row: stable, mode: result, references: refs, localMode: setting }) + return result + } + const templateRefs = bgpTemplateReferences(await client.post("/routing/bgp/connection/get", { ".id": id, "value-name": "templates" })) + const inherited: BgpExportMode[] = [] + for (const ref of templateRefs) inherited.push(await resolveTemplate(ref)) + if (new Set(inherited).size > 1) throw new McpError("STATE_UNAVAILABLE", "BGP-подключение наследует неоднозначные источники", 409) + const inheritedMode = inherited[0] ?? "disabled", currentMode = localMode ?? inheritedMode + if (mode === "disabled" && currentMode !== "disabled" && inheritedMode !== "disabled") throw new McpError("STATE_CONFLICT", "Очистка output.redistribute может вернуть экспорт из шаблона; сначала измените наследование отдельным планом", 409) + const connection = parseState("static/dynamic, disabled и явный instance/VRF/table", () => bgpExportConnection(hydrated, currentMode)) + const sourceModes = input.sourceModes === undefined ? modes([currentMode, mode]) : modes(z.array(modeSchema).min(1).max(3).parse(input.sourceModes)) + if (!sourceModes.includes(currentMode) || !sourceModes.includes(mode)) throw new McpError("STATE_CONFLICT", "Набор источников экспорта изменился", 409) + const parentIds = ["bgp_instance", "vrf", "routing_table", "ipv4_address", "ipv6_address", "routing_filter"] + const parents = await Promise.all(parentIds.map(resource => readRosSnapshot(serverId, resource))) + if (parents.some(snapshot => snapshot.rows.length > 1000)) throw new McpError("LIMIT_EXCEEDED", "Каталог зависимостей BGP превышает 1000 объектов", 409) + if (parents.filter(snapshot => ["ipv4_address", "ipv6_address", "routing_filter"].includes(snapshot.resource)).some(snapshot => snapshot.rows.some(row => typeof row[".id"] !== "string" || !row[".id"].trim() || row[".id"].length > 200))) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены native IDs адресов или фильтров BGP", 409) + const rows = (resource: string) => parents.find(snapshot => snapshot.resource === resource)!.rows + const unique = (resource: string, name: unknown) => { + const found = rows(resource).filter(row => row.name === name) + if (found.length !== 1 || typeof found[0][".id"] !== "string" || found[0].disabled === "true") throw new McpError("STATE_CONFLICT", "Не подтверждена зависимость экспорта BGP: " + resource, 409) + return found[0] + } + const instance = unique("bgp_instance", connection.instance), vrf = unique("vrf", connection.vrf), table = unique("routing_table", connection["routing-table"]) + if (instance.vrf !== connection.vrf || instance["routing-table"] !== connection["routing-table"] || vrf.name !== table.name) throw new McpError("STATE_CONFLICT", "Экспорт требует одинаковые явные instance/connection VRF и таблицу", 409) + const [v4, v6] = await Promise.all([client.get("/ip/route"), client.get("/ipv6/route")]) + const sources = [...parseState("IPv4 source origins/table/prefixes", () => bgpExportSources(v4, String(table.name), sourceModes, 4)), ...parseState("IPv6 source origins/table/prefixes", () => bgpExportSources(v6, String(table.name), sourceModes, 6))] + if (sources.length > 500) throw new McpError("LIMIT_EXCEEDED", "Экспорт BGP превышает 500 исходных маршрутов", 409) + const objects: ChangeEvidence["objects"] = [ + { kind: "bgp-export", id, serverId }, { kind: "bgp_connection", id, serverId }, + { kind: "bgp_instance", id: String(instance[".id"]), serverId }, { kind: "vrf", id: String(vrf[".id"]), serverId }, { kind: "routing_table", id: String(table[".id"]), serverId }, + ...sources.map(row => ({ kind: row.family === 4 ? "ipv4_route" : "ipv6_route", id: row.id, serverId })), + ...parents.filter(snapshot => ["ipv4_address", "ipv6_address", "routing_filter"].includes(snapshot.resource)).flatMap(snapshot => snapshot.rows.map(row => ({ kind: snapshot.resource, id: String(row[".id"]), serverId }))), + ...[...usedTemplates.keys()].map(nativeId => ({ kind: "bgp_template", id: nativeId, serverId })), + ] + if (objects.some(row => !row.id || row.id === "undefined") || objects.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены зависимости экспорта BGP", 409) + const parentProof = parents.map(snapshot => ({ resource: snapshot.resource, rows: ["bgp_instance", "vrf", "routing_table"].includes(snapshot.resource) ? snapshot.rows.filter(row => row[".id"] === ({ bgp_instance: instance, vrf, routing_table: table } as Record)[snapshot.resource][".id"]) : snapshot.rows })) + const templateProof = [...usedTemplates.values()].sort((a, b) => a.row[".id"].localeCompare(b.row[".id"])) + const before = { serverId, serverProof, id, instance: connection.instance, vrf: connection.vrf, table: connection["routing-table"], mode: currentMode, localMode, inheritedMode, + templates: templateProof.map(value => ({ id: value.row[".id"], name: value.row.name, mode: value.mode })), + sourceModes, sources: sources.map(({ proof: _proof, ...row }) => row), + connectionProof: await configurationProof(serverId, id, connection), sourceProof: await privateSecretFingerprint(fingerprint({ serverProof, parentProof, sources, templateProof }), `bgp-export-sources:${serverId}:${id}`) } + return { server, client, connection, before, objects } +} +async function state(input: Record) { + try { return await readState(input) } + catch (error) { if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Не подтверждены effective export, происхождение маршрутов или явный контекст BGP", 409) } +} +async function current(input: Record) { return (await state(input)).before } + +export function registerBgpExportActions(register: typeof add) { + registerMcpService(service, { + current, + execute: async input => { + const before = await state(input) + if (fingerprint(before.before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "Подключение или источники BGP изменились после подтверждения", 409) + const mode = modeSchema.parse(input.mode), expected = bgpExportConnection({ ...before.connection, ".id": String(input.id) }, mode) + const receipt = { mode, connectionProof: await configurationProof(Number(input.serverId), String(input.id), expected), sourceProof: before.before.sourceProof, nativeDispatch: before.before.mode !== mode, forwardingVerified: false } + if (before.before.mode !== mode) await executeGuardedApplicationWrite({ kind: "bgp-export", id: String(input.id) }, [Number(input.serverId)], async () => { + const live = await state(input) + if (fingerprint(live.before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "Источники BGP изменились перед записью", 409) + await live.client.patch(`/routing/bgp/connection/${encodeURIComponent(String(input.id))}`, { "output.redistribute": nativeBgpExportValue(mode) }) + }, async () => before.objects) + return receipt + }, + verify: async (input, result) => { + const expected = result as { mode: string; connectionProof: string; sourceProof: string }, saved = (await state(input)).before + return saved.mode === expected.mode && saved.connectionProof === expected.connectionProof && saved.sourceProof === expected.sourceProof + }, + }) + register("routing.bgp.export", "mm_bgp_export_plan", "Человеческий план перераспределения connected/static в BGP с проверкой источников; disabled отключает только перераспределение", "bgp", ["mm:bgp:write", "mm:network:read", "mm:filters:read"], "write", "servers", schema, { + prepare: async input => { + const initial = await state(input), before = initial.before, request = { ...input, sourceModes: before.sourceModes, beforeFingerprint: fingerprint(before), expectedServerProof: before.serverProof } + return { target: { scope: "servers", serverIds: [Number(input.serverId)] }, objects: initial.objects, snapshots: [], before, + steps: [{ kind: "service", service, input: request }], mutationCost: 1, requiresHuman: true, verificationScope: "configuration", + warnings: ["Изменяется output.redistribute одного BGP-подключения. Connected/static экспортируют подходящие маршруты его явной таблицы; это не список разрешённых префиксов.", + "Существующие input/output filters не изменяются; они могут разрешать или отклонять экспорт. Их конфигурация и исходные маршруты запечатаны в плане.", + "Отключается только перераспределение connected/static. Output networks и default-originate не меняются; очистка в активный inherited export из шаблона отклоняется.", + "Включение экспорта требует отдельного человеческого подтверждения. Проверяется конфигурация, обмен префиксами и forwarding требуют отдельных наблюдений.", + "Поля TCP MD5 и исходные данные фильтров не выводятся: приватные proof привязаны к закрытому серверному ключу. Неопределённая запись не повторяется автоматически."] } + }, + }) +} diff --git a/backend/src/modules/mcp/bgp-filter-binding-actions.ts b/backend/src/modules/mcp/bgp-filter-binding-actions.ts new file mode 100644 index 00000000..4ad889f1 --- /dev/null +++ b/backend/src/modules/mcp/bgp-filter-binding-actions.ts @@ -0,0 +1,125 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { readStaticBgpConnection } from "../../services/bgp-connection-read.js" +import { nativeBgpRows, bgpTemplateReferences, type NativeBgpRow } from "../../services/bgp-export-model.js" +import { bgpFilterName, bgpFilterProperty, effectiveBgpFilter, stableBgpPolicyRow, type BgpFilterDirection } from "../../services/bgp-filter-binding-model.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { canonicalVrfMembers } from "../../services/vrf-members.js" +import { fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { routerClient } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import type { ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const name = z.string().max(100).refine(value => !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(value)) +const schema = z.object({ serverId: z.number().int().positive().safe(), id: z.string().regex(/^\*[A-Za-z0-9]+$/).max(200), direction: z.enum(["input", "output"]), chain: name }).strict() +const service = "routing.bgp.filter.bind" +const proof = (sid: number, id: string, domain: string, value: unknown) => privateSecretFingerprint(fingerprint(value), `bgp-filter-binding:${sid}:${id}:${domain}`) + +async function readState(input: Record) { + const serverId = Number(input.serverId), id = String(input.id), desired = String(input.chain), property = bgpFilterProperty(input.direction as BgpFilterDirection) + const { server, client } = await routerClient(serverId), serverProof = await proof(serverId, id, "server", server) + if (input.expectedServerProof !== undefined && input.expectedServerProof !== serverProof) throw new McpError("STATE_CONFLICT", "Назначение или credentials BGP изменились; новый источник не опрашивается", 409) + const { connection, connections } = await readStaticBgpConnection(client, id) + if (connections.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Каталог BGP превышает 1000 подключений", 409) + for (const key of ["instance", "vrf", "routing-table"]) if (connection[key] === undefined || connection[key] === "") { + const value = bgpFilterName(await client.post("/routing/bgp/connection/get", { ".id": id, "value-name": key })) + if (!value) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён effective контекст BGP: " + key, 409) + connection[key] = value + } + const templates = nativeBgpRows(await client.get("/routing/bgp/template")) + if (templates.length > 100) throw new McpError("LIMIT_EXCEEDED", "Каталог BGP templates превышает 100", 409) + const used = new Map() + const resolve = async (reference: string, stack: string[] = []): Promise => { + const found = templates.filter(row => row[".id"] === reference || row.name === reference) + if (found.length !== 1 || stack.length >= 16 || stack.includes(found[0][".id"])) throw new Error("BGP template graph not confirmed") + const row = found[0], cached = used.get(row[".id"]) + if (cached) return cached.effective + const [hydrated] = await readOspfEffectiveDisabled([row], nativeId => client.post("/routing/bgp/template/get", { ".id": nativeId, "value-name": "disabled" })) + if (!["false", "no"].includes(String(hydrated.disabled))) throw new Error("BGP template is disabled or unknown") + const local = bgpFilterName(await client.post("/routing/bgp/template/get", { ".id": row[".id"], "value-name": property })) + const references = bgpTemplateReferences(await client.post("/routing/bgp/template/get", { ".id": row[".id"], "value-name": "templates" })) + const parents = []; for (const ref of references) parents.push(await resolve(ref, [...stack, row[".id"]])) + const effective = effectiveBgpFilter(local, parents) + used.set(row[".id"], { row: { ...stableBgpPolicyRow(hydrated), ".id": row[".id"] }, local, references, effective }); return effective + } + const localChain = bgpFilterName(await client.post("/routing/bgp/connection/get", { ".id": id, "value-name": property })) + const references = bgpTemplateReferences(await client.post("/routing/bgp/connection/get", { ".id": id, "value-name": "templates" })) + const parents = []; for (const ref of references) parents.push(await resolve(ref)) + const inheritedChain = new Set(parents).size > 1 ? null : parents[0] ?? "", effectiveChain = effectiveBgpFilter(localChain, parents) + if (desired === "" && inheritedChain !== "") throw new McpError("STATE_CONFLICT", "Очистка local binding включила бы inherited filter; нужен отдельный план шаблона", 409) + const snapshots = await Promise.all([["bgp_instance", "/routing/bgp/instance"], ["vrf", "/ip/vrf"], ["routing_table", "/routing/table"]].map(async ([resource, path]) => ({ resource, rows: nativeBgpRows(await client.get(path)) }))) + if (snapshots.some(row => row.rows.length > 1000)) throw new Error("BGP context exceeds 1000 rows") + const unique = (kind: string, reference: unknown) => { + const rows = snapshots.find(row => row.resource === kind)!.rows.filter(row => row.name === reference || row[".id"] === reference) + if (rows.length !== 1 || rows[0].disabled === "true" || [true, "true", "yes"].includes(rows[0].invalid as boolean | string)) throw new Error("BGP context not confirmed") + return rows[0] + } + const instance = unique("bgp_instance", connection.instance), vrf = unique("vrf", connection.vrf), table = unique("routing_table", connection["routing-table"]) + const [effectiveInstance] = await readOspfEffectiveDisabled([instance], nativeId => client.post("/routing/bgp/instance/get", { ".id": nativeId, "value-name": "disabled" })) + if (!["false", "no"].includes(String(effectiveInstance.disabled))) throw new Error("BGP instance disabled/unknown") + Object.assign(instance, effectiveInstance) + for (const key of ["vrf", "routing-table"]) if (instance[key] === undefined || instance[key] === "") { + const value = bgpFilterName(await client.post("/routing/bgp/instance/get", { ".id": String(instance[".id"]), "value-name": key })) + if (!value) throw new Error("BGP instance effective context not confirmed") + instance[key] = value + } + if (unique("vrf", instance.vrf)[".id"] !== vrf[".id"] || unique("routing_table", instance["routing-table"])[".id"] !== table[".id"] || vrf.name !== table.name) throw new Error("BGP instance/connection contexts disagree") + const filters = nativeBgpRows(await client.get("/routing/filter/rule")) + if (filters.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Каталог BGP filters превышает 1000 строк", 409) + const selected = filters.filter(row => row.chain === desired) + if (desired && !selected.length) throw new McpError("NOT_FOUND", "Выбранная цепочка фильтров не найдена", 404) + if (selected.some(row => [true, "true", "yes"].includes(row.invalid as boolean | string))) throw new McpError("STATE_CONFLICT", "Выбранная цепочка содержит invalid rule", 409) + const effectiveFilters = desired ? await readOspfEffectiveDisabled(selected, nativeId => client.post("/routing/filter/rule/get", { ".id": nativeId, "value-name": "disabled" })) : [] + const protections = (await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(serverId)])).rows + if (protections.length > 1000) throw new Error("BGP protection closure exceeds limit") + const objects: ChangeEvidence["objects"] = [{ kind: "bgp-policy", id, serverId }, { kind: "bgp_connection", id, serverId }, + ...[["bgp_instance", instance], ["vrf", vrf], ["routing_table", table]].map(([kind, row]) => ({ kind: String(kind), id: String((row as Record)[".id"]), serverId })), + ...filters.map(row => ({ kind: "routing_filter", id: row[".id"], serverId })), ...[...used.keys()].map(nativeId => ({ kind: "bgp_template", id: nativeId, serverId })), + ...protections.map(({ object_ref: ref }) => ({ kind: ref.kind, id: ref.id, serverId }))] + const uniqueObjects = [...new Map(objects.map(row => [fingerprint(row), row])).values()] + if (uniqueObjects.length > 1000) throw new Error("BGP dependency closure exceeds limit") + const canonicalConnection = { ...stableBgpPolicyRow(connection), [property]: effectiveChain } + const dependencyProof = await proof(serverId, id, "dependencies", { serverProof, otherConnections: connections.filter(row => row[".id"] !== id).map(stableBgpPolicyRow), instance: stableBgpPolicyRow(instance), vrf: { ...stableBgpPolicyRow(vrf), ...(vrf.interfaces !== undefined ? { interfaces: canonicalVrfMembers(vrf.interfaces) } : {}) }, table: stableBgpPolicyRow(table), + filters: filters.map(stableBgpPolicyRow), used: [...used.values()], references, protections }) + const before = { serverId, id, direction: input.direction, property, instance: instance.name, vrf: vrf.name, table: table.name, localChain, inheritedChain, effectiveChain, + selectedRuleCount: selected.length, selectedEnabledRuleCount: effectiveFilters.filter(row => ["false", "no"].includes(String(row.disabled))).length, serverProof, dependencyProof, + connectionProof: await proof(serverId, id, "connection", canonicalConnection) } + return { server, client, connection: canonicalConnection, before, objects: uniqueObjects } +} +async function state(input: Record) { + try { return await readState(input) } catch (error) { if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Не подтверждены BGP binding, templates или routing dependencies", 409) } +} +export function registerBgpFilterBindingActions(register: typeof add) { + registerMcpService(service, { + current: async input => (await state(input)).before, + execute: async input => { + const initial = await state(input) + if (fingerprint(initial.before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "BGP policy изменилась после подтверждения", 409) + const desired = String(input.chain), nativeDispatch = desired ? initial.before.localChain !== desired : initial.before.localChain !== null && initial.before.localChain !== "" + const result = { desired, nativeDispatch, dependencyProof: initial.before.dependencyProof, connectionProof: await proof(initial.server.id, String(input.id), "connection", { ...initial.connection, [initial.before.property]: desired }), forwardingVerified: false } + await executeGuardedApplicationWrite({ kind: "bgp-policy", id: String(input.id) }, [initial.server.id], async () => { + const live = await state(input) + if (fingerprint(live.before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "BGP dependencies изменились перед dispatch", 409) + if (nativeDispatch) await live.client.patch("/routing/bgp/connection/" + encodeURIComponent(String(input.id)), { [live.before.property]: desired }) + }, async () => initial.objects) + return result + }, + verify: async (input, result) => { + const receipt = result as { desired: string; connectionProof: string; dependencyProof: string }, after = (await state(input)).before + return after.effectiveChain === receipt.desired && (receipt.desired ? after.localChain === receipt.desired : !after.localChain) && after.connectionProof === receipt.connectionProof && after.dependencyProof === receipt.dependencyProof + }, + }) + register("routing.bgp.filter.bind", "mm_bgp_filter_binding_plan", "Человеческий план локальной input/output привязки BGP к существующей цепочке; пустая chain очищает local binding", "bgp", ["mm:bgp:write", "mm:filters:read", "mm:network:read"], "write", "servers", schema, { + prepare: async input => { + const initial = await state(input) + return { target: { scope: "servers", serverIds: [initial.server.id] }, objects: initial.objects, snapshots: [], before: initial.before, requiresHuman: true, verificationScope: "configuration", mutationCost: 1, + steps: [{ kind: "service", service, input: { ...input, beforeFingerprint: fingerprint(initial.before), expectedServerProof: initial.before.serverProof } }], + warnings: ["Меняется одна локальная привязка BGP; цепочка не создаётся и её код не изменяется. Изменение относится ко всем AFI/префиксам этого peer и может повлиять на выбор/экспорт маршрутов.", + "Фильтры/templates и контекст VRF/таблицы запечатаны. Очистка в непустой inherited filter запрещена; шаблоны не изменяются неявно.", + "Семантика jumps/opaque правил и доступность пути этим планом не подтверждаются. Пустая или полностью отключённая цепочка может отклонить все префиксы.", + "Только подтверждение человеком; после partial/unknown нужен новый анализ. Автоматического повтора, rollback или refresh BGP не выполняется."] } + }, + }) +} diff --git a/backend/src/modules/mcp/bgp-prefix-actions.ts b/backend/src/modules/mcp/bgp-prefix-actions.ts new file mode 100644 index 00000000..c2a9bdf1 --- /dev/null +++ b/backend/src/modules/mcp/bgp-prefix-actions.ts @@ -0,0 +1,48 @@ +import { z } from "zod" +import { hasPermission } from "../../lib/permissions.js" +import { bgpPrefixRows } from "../../services/bgp-prefix-model.js" +import { nativeBgpRows } from "../../services/bgp-export-model.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { routerClient } from "./resources.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, McpError } from "./security.js" +import type { ActionContext } from "./action-types.js" +import type { add } from "./actions.js" + +const permissions = ["mm:bgp:read", "mm:network:read"] +async function access(context: ActionContext) { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение BGP префиксов отменено", 409) + const current = await effectiveMcpConnection(context.principal) + if (permissions.some(permission => !hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission))) throw new McpError("PERMISSION_DENIED", "Право чтения BGP/сети отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Область чтения BGP изменилась", 409) +} +export function registerBgpPrefixActions(register: typeof add) { + register("routing.bgp.prefixes.read", "mm_bgp_prefixes_get", "Наблюдаемые BGP-префиксы IPv4/IPv6 в явной таблице; active nullable, без доказательства forwarding или attribution к peer", "bgp", permissions, "read", "servers", z.object({ + serverId: z.number().int().positive().safe(), family: z.enum(["ipv4", "ipv6"]).default("ipv4"), + table: z.string().trim().min(1).max(100).refine(value => !/[\u0000-\u001f\u007f]/.test(value)).default("main"), + offset: z.number().int().min(0).max(10000).default(0), limit: z.number().int().min(1).max(200).default(100), + }).strict(), { read: async (input, context) => { + await access(context) + const { server, client } = await routerClient(Number(input.serverId)), startedAt = new Date().toISOString(), table = String(input.table) + const tableState = async () => { + await access(context) + const selected = nativeBgpRows(await client.get("/routing/table", 10000, context.signal)).filter(row => row.name === table) + if (selected.length !== 1) throw new McpError("STATE_UNAVAILABLE", "Таблица BGP не найдена или неоднозначна", 409) + return fingerprint(selected) + } + try { + const before = await tableState() + await access(context) + const family = input.family === "ipv4" ? 4 : 6 + const rows = bgpPrefixRows(await client.get(`${family === 4 ? "/ip/route" : "/ipv6/route"}?routing-table=${encodeURIComponent(table)}`, 10000, context.signal), table, family) + if (before !== await tableState()) throw new McpError("STATE_CONFLICT", "Таблица BGP изменилась во время чтения", 409) + const fresh = await getServerRowById(server.id) + if (!fresh || fingerprint(fresh) !== fingerprint(server)) throw new McpError("STATE_CONFLICT", "Источник BGP изменился во время чтения", 409) + await access(context) + const offset = Number(input.offset), limit = Number(input.limit), items = rows.slice(offset, offset + limit) + return { serverId: server.id, family: input.family, table, items, total: rows.length, nextOffset: offset + limit < rows.length ? offset + limit : null, incomplete: items.length < rows.length, + startedAt, observedAt: new Date().toISOString(), timestampKind: "backend-receipt", fingerprint: fingerprint(rows), source: "routeros-route-view", atomicSnapshot: false, + sessionAttributionVerified: false, advertisedRoutesVerified: false, forwardingVerified: false } + } catch (error) { await access(context); if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "BGP route view недоступен, неоднозначен или превышает 10000 строк; успешный пустой ответ не подтверждён", 409) } + } }) +} diff --git a/backend/src/modules/mcp/bridge-vlan-actions.ts b/backend/src/modules/mcp/bridge-vlan-actions.ts new file mode 100644 index 00000000..da6e2eae --- /dev/null +++ b/backend/src/modules/mcp/bridge-vlan-actions.ts @@ -0,0 +1,153 @@ +import { z } from "zod" +import { isIP } from "node:net" +import { bridgeVlanConfigurationRequestSchema } from "@mmapp/contracts/bridge-vlan" +import { buildBridgeVlanConfiguration, bridgeVlanCurrentDesired, BridgeVlanConfigurationError, type BridgeVlanState } from "../../services/bridge-vlan-configuration.js" +import { readRosSnapshot, routerClient, rosSnapshotSourceProof, type RosSnapshot, type RosStep } from "./resources.js" +import { verifyBridgeManagementChannel } from "./management.js" +import { McpError } from "./security.js" +import { canonicalBridgeVlanValue } from "../../services/bridge-vlan-values.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const interfaceResources = ["ethernet", "bridge", "vlan", "gre", "wireguard", "vxlan"] +const resources = [...interfaceResources, "bridge_port", "bridge_vlan", "ipv4_address", "ipv6_address"] +export async function readBridgeVlanState(serverId: number, context: Pick): Promise<{ state: BridgeVlanState; snapshots: RosSnapshot[] }> { + const snapshots: RosSnapshot[] = [] + const active = async () => { if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение конфигурации VLAN отменено", 409) } + // Bind every catalogue to the same configured device source. The executor + // rechecks these complete configuration snapshots before each native write. + for (const resource of resources) snapshots.push(await readRosSnapshot(serverId, resource, active, snapshots[0]?.sourceProof)) + const rows = (resource: string) => snapshots.find(snapshot => snapshot.resource === resource)!.rows + const state: BridgeVlanState = { + bridges: rows("bridge"), ports: rows("bridge_port"), vlans: rows("bridge_vlan"), + interfaces: snapshots.filter(snapshot => interfaceResources.includes(snapshot.resource)).flatMap(snapshot => snapshot.rows.map(row => ({ + resource: snapshot.resource, id: String(row[".id"]), name: String(row.name), disabled: row.disabled, + }))), + vlanInterfaces: rows("vlan"), ipv4Addresses: rows("ipv4_address"), ipv6Addresses: rows("ipv6_address"), + } + return { state, snapshots } +} + +export async function prepareBridgeVlanConfiguration(raw: unknown, context: ActionContext): Promise { + const input = bridgeVlanConfigurationRequestSchema.parse(raw) + let management: Awaited> | undefined + try { management = await verifyBridgeManagementChannel(input.serverId) } + catch (error) { + // Only lack of a current alternate channel permits primary-path admission. + // Identity, transport and other verification failures stay fail-closed. + if (!(error instanceof McpError) || error.code !== "APPROVAL_REQUIRED") throw error + } + const { state, snapshots } = await readBridgeVlanState(input.serverId, context) + const primaryManagement = management === undefined + if (!management) { + const { server } = await routerClient(input.serverId) + if (!isIP(server.host)) throw new McpError("MANAGEMENT_UNSAFE", "Основной адрес управления должен быть буквальным IP, подтверждённым в конфигурации устройства", 409) + if (!snapshots[0]?.sourceProof || await rosSnapshotSourceProof(server) !== snapshots[0].sourceProof) throw new McpError("STATE_CONFLICT", "Источник основного управления изменился во время подготовки VLAN", 409) + const expiresAt = Math.min(Date.parse(context.connection.policy.expiresAt), context.connection.owner_valid_until.getTime(), Date.parse(context.principal.credentialExpiresAt)) + if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) throw new McpError("APPROVAL_REQUIRED", "Нет действующего допуска для основного канала управления", 409) + management = { channelId: "primary", serverId: input.serverId, host: server.host, expiresAt: new Date(expiresAt).toISOString() } + } + try { + const plan = buildBridgeVlanConfiguration({ ...input, state, management, now: Date.now() }) + if (!plan.steps.length) throw new McpError("STATE_CONFLICT", "Конфигурация VLAN уже соответствует плану", 409) + return { + target: { scope: "servers", serverIds: [input.serverId] }, objects: plan.objects, + snapshots, steps: plan.steps.map(step => ({ kind: "ros" as const, step })), + requiresHuman: true, validUntil: management.expiresAt, + ...(primaryManagement ? {} : { managementChannels: [{ channelId: management.channelId, serverId: management.serverId, expiresAt: management.expiresAt }] }), + warnings: ["Связанный план изменяет VLAN memberships и PVID в указанном порядке; включение filtering выполняется последним.", + "Каждая подтверждённая запись проверяется до следующей. При неизвестном результате дальнейшие записи останавливаются.", + primaryManagement ? "Используется основной адрес управления; независимость канала не подтверждена. Обязательно подтверждение человеком; каждый префикс плана сохраняет подтверждённый management-доступ." + : "Проверяется конфигурация и доступность зарегистрированного management-канала. Передача пользовательского трафика требует отдельной сетевой проверки."], + } + } catch (error) { + if (error instanceof BridgeVlanConfigurationError) throw new McpError(error.code, error.message, 409) + throw error + } +} + +/** A legacy single-row tool must pass the same effective-state checks, but may + * not acquire authority to edit other resource kinds through this delegation. */ +export async function prepareGenericBridgeVlanChange(step: RosStep, context: ActionContext): Promise { + const sensitive = step.resource === "bridge" ? ["pvid", "vlan-filtering", "frame-types", "ingress-filtering"] : step.resource === "bridge_port" + ? ["bridge", "interface", "pvid", "ingress-filtering", "frame-types", "disabled"] : ["bridge", "vlan-ids", "tagged", "untagged", "disabled"] + if (!["bridge", "bridge_port", "bridge_vlan"].includes(step.resource) || step.resource === "bridge" && step.action !== "update" + || step.action === "update" && !Object.keys(step.fields).some(key => sensitive.includes(key))) return undefined + const { state } = await readBridgeVlanState(step.serverId, context) + const rows = step.resource === "bridge" ? state.bridges : step.resource === "bridge_port" ? state.ports : state.vlans + const row = rows.find(item => item[".id"] === step.id) + const bridgeName = step.resource === "bridge" ? row?.name : step.fields.bridge ?? row?.bridge + const bridges = state.bridges.filter(item => item.name === bridgeName) + if (bridges.length !== 1 || step.resource !== "bridge" && row && row.bridge !== bridgeName) throw new McpError("STATE_CONFLICT", "Смена bridge требует отдельных явных планов удаления и добавления порта", 409) + const bridgeId = String(bridges[0][".id"]) + try { + const desired = bridgeVlanCurrentDesired(state, bridgeId) + const bool = (value: unknown): boolean => { + if (value === "true" || value === true) return true + if (value === "false" || value === false) return false + throw new McpError("INVALID_INPUT", "Нужны явные параметры порта VLAN", 400) + } + const ref = (name: unknown) => { + const matches = state.interfaces.filter(iface => iface.name === name) + if (matches.length !== 1) throw new McpError("INVALID_INPUT", "Не подтверждён стабильный ID участника VLAN", 400) + return { resource: matches[0].resource, id: matches[0].id } + } + if (step.resource === "bridge") { + if (Object.keys(step.fields).some(key => !sensitive.includes(key))) throw new McpError("INVALID_INPUT", "Подготовьте свойства bridge и VLAN filtering отдельными планами", 400) + if (step.fields.pvid !== undefined) desired.pvid = Number(step.fields.pvid) + if (step.fields["vlan-filtering"] !== undefined) desired.filtering = bool(step.fields["vlan-filtering"]) + if (step.fields["frame-types"] !== undefined) desired.frameTypes = bridgeVlanConfigurationRequestSchema.shape.desired.shape.frameTypes.parse(step.fields["frame-types"]) + if (step.fields["ingress-filtering"] !== undefined) desired.ingressFiltering = bool(step.fields["ingress-filtering"]) + } else if (step.resource === "bridge_port") { + const index = desired.ports.findIndex(port => port.id === step.id) + if (step.action === "delete") { + if (index < 0) throw new McpError("NOT_FOUND", "Статический порт не найден", 404) + desired.ports.splice(index, 1); desired.removePortIds.push(step.id!) + } else { + const effective = { ...row, ...step.fields } + const port = { ...(step.action === "update" ? { id: step.id } : {}), interface: ref(effective.interface), pvid: Number(effective.pvid), + ingressFiltering: bool(effective["ingress-filtering"]), frameTypes: effective["frame-types"], disabled: bool(effective.disabled), + ...(effective.comment === undefined ? {} : { comment: effective.comment }) } + const parsed = bridgeVlanConfigurationRequestSchema.shape.desired.shape.ports.element.parse(port) + if (step.action === "create") desired.ports.push(parsed) + else { if (index < 0) throw new McpError("NOT_FOUND", "Статический порт не найден", 404); desired.ports[index] = parsed } + } + } else { + const index = desired.vlans.findIndex(vlan => vlan.id === step.id) + if (step.action === "delete") { + if (index < 0) throw new McpError("NOT_FOUND", "Статическая VLAN membership не найдена", 404) + desired.vlans.splice(index, 1); desired.removeVlanIds.push(step.id!) + } else { + const effective = { ...row, ...step.fields } + const members = (value: unknown) => value === undefined || value === "" ? [] : String(value).split(",").map(ref) + const vlan = { ...(step.action === "update" ? { id: step.id } : {}), vlanIds: effective["vlan-ids"], tagged: members(effective.tagged), untagged: members(effective.untagged), + disabled: effective.disabled === undefined && step.action === "create" ? false : bool(effective.disabled), ...(effective.comment === undefined ? {} : { comment: effective.comment }) } + const parsed = bridgeVlanConfigurationRequestSchema.shape.desired.shape.vlans.element.parse(vlan) + if (step.action === "create") desired.vlans.push(parsed) + else { if (index < 0) throw new McpError("NOT_FOUND", "Статическая VLAN membership не найдена", 404); desired.vlans[index] = parsed } + } + } + const evidence = await prepareBridgeVlanConfiguration({ serverId: step.serverId, bridgeId, desired }, context) + if (evidence.steps.length !== 1 || evidence.steps[0].kind !== "ros" || evidence.steps[0].step.resource !== step.resource || evidence.steps[0].step.action !== step.action || evidence.steps[0].step.id !== step.id) { + throw new McpError("APPROVAL_REQUIRED", "Для связанных изменений используйте явный план mm_bridge_vlan_configuration_plan с соответствующим разрешением", 409) + } + const native = evidence.steps[0].step + if (step.action === "update" && Object.entries(native.fields).some(([key, value]) => !Object.hasOwn(step.fields, key) + || (step.resource === "bridge_vlan" ? canonicalBridgeVlanValue(key, value) !== canonicalBridgeVlanValue(key, step.fields[key]) : value !== step.fields[key]))) { + throw new McpError("STATE_CONFLICT", "Состояние VLAN изменилось во время подготовки; незапрошенные поля не будут перезаписаны", 409) + } + return evidence + } catch (error) { + if (error instanceof BridgeVlanConfigurationError) throw new McpError(error.code, error.message, 409) + throw error + } +} + +export function registerBridgeVlanActions(register: typeof add) { + register("network.bridge-vlan.read", "mm_bridge_vlan_configuration_get", "Полная конфигурация bridge VLAN и стабильные ID участников", "network", "mm:network:read", "read", "servers", z.object({ serverId: z.number().int().positive() }).strict(), { + read: async (input, context) => ({ state: (await readBridgeVlanState(Number(input.serverId), context)).state }), + }) + register("network.bridge-vlan.configure", "mm_bridge_vlan_configuration_plan", "Связанный план VLAN membership, PVID и bridge filtering", "network", "mm:network:write", "write", "servers", bridgeVlanConfigurationRequestSchema, { + prepare: prepareBridgeVlanConfiguration, + }) +} diff --git a/backend/src/modules/mcp/bridge-vlan-configuration-http.test.ts b/backend/src/modules/mcp/bridge-vlan-configuration-http.test.ts new file mode 100644 index 00000000..d504d4e1 --- /dev/null +++ b/backend/src/modules/mcp/bridge-vlan-configuration-http.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "bridge-vlan-owned-acceptance-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const { buildApp } = await import("../../index.js") +const actor = { id: "bridge-vlan-owner", email: "bridge@example.invalid", name: "Bridge VLAN owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin"], expiresAt: Date.now() + 3600000 } +let app: Awaited> | undefined +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false }); await app.ready() + await (await import("../../test/mcp-bridge-vlan-configuration.js")).verifyMcpBridgeVlanConfiguration(actor, app) +} finally { + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close(); await closePool() +} diff --git a/backend/src/modules/mcp/bridge-vlan-projection.test.ts b/backend/src/modules/mcp/bridge-vlan-projection.test.ts new file mode 100644 index 00000000..4781236d --- /dev/null +++ b/backend/src/modules/mcp/bridge-vlan-projection.test.ts @@ -0,0 +1,76 @@ +import assert from "node:assert/strict" +import test from "node:test" +import { configurationRows, resourceById, rosSnapshotVersion, type ConfigurationRow, type RosSnapshot, type RosStep } from "./resources.js" +import { createRosPrefix } from "./ros-prefix.js" +import { verifyRosStepSnapshots } from "./ros-step-verification.js" +import { fingerprint, McpError } from "./security.js" + +const snapshot = (resource: string, raw: ConfigurationRow[]): RosSnapshot => { + const rows = configurationRows(resourceById(resource), raw) + return { serverId: 1, resource, rows, fingerprint: fingerprint(rows), sourceProof: "a".repeat(64), projectionVersion: rosSnapshotVersion(resource) } +} +const membership = { ".id": "*1", bridge: "br", "vlan-ids": "20-22", tagged: "ether2,ether3", dynamic: "false", disabled: "false" } + +test("VLAN readback accepts equivalent VID/member sets and omitted empty fields, retaining exact native identity", () => { + const before = snapshot("bridge_vlan", [membership]) + const after = snapshot("bridge_vlan", [{ ...membership, "vlan-ids": "22,20,21", tagged: "ether3,ether2" }]) + const step: RosStep = { serverId: 1, resource: "bridge_vlan", action: "update", id: "*1", fields: { "vlan-ids": "20-22", tagged: "ether3,ether2", untagged: "", comment: "" } } + assert.equal(verifyRosStepSnapshots(step, {}, before, after), true) + assert.equal(verifyRosStepSnapshots(step, {}, before, snapshot("bridge_vlan", [{ ...membership, "vlan-ids": "20-23" }])), false) + assert.equal(verifyRosStepSnapshots(step, {}, before, snapshot("bridge_vlan", [{ ...membership, ".id": "*2" }])), false) + assert.throws(() => snapshot("bridge_vlan", [{ ...membership, "vlan-ids": "20,20-22" }]), /Недостоверная/) +}) + +test("PVID-generated rows may change while the sealed static VLAN configuration remains exact", async () => { + const port = { ".id": "*2", bridge: "br", interface: "ether2", pvid: "10", disabled: "false" } + const dynamic = { ".id": "*D", bridge: "br", "vlan-ids": "10", untagged: "ether2", dynamic: "true" } + const raw = new Map([["bridge_port", [port]], ["bridge_vlan", [membership, dynamic]]]) + const initial = [...raw].map(([resource, rows]) => snapshot(resource, rows)) + const read = async (_server: number, resource: string, active: () => Promise = async () => {}) => { await active(); return snapshot(resource, raw.get(resource)!) } + const prefix = createRosPrefix(initial, read) + await prefix.check() + raw.set("bridge_port", [{ ...port, pvid: "20" }]) + raw.set("bridge_vlan", [membership, { ...dynamic, ".id": "*E", "vlan-ids": "20" }]) + const step: RosStep = { serverId: 1, resource: "bridge_port", action: "update", id: "*2", fields: { pvid: "20" } } + await prefix.advance(step, {}, await read(1, "bridge_port")) + await prefix.check() + raw.set("bridge_vlan", [{ ...membership, tagged: "ether2" }, dynamic]) + await assert.rejects(prefix.check(), error => error instanceof McpError && error.code === "STATE_CONFLICT") +}) + +test("full VLAN ranges and long sparse VID sets stay representable in plans and inverse snapshots", () => { + const resource = resourceById("bridge_vlan") + const expanded = Array.from({ length: 4094 }, (_, index) => String(index + 1)).join(",") + const sparse = Array.from({ length: 2047 }, (_, index) => String(index * 2 + 1)).join(",") + assert.ok(sparse.length > 100) + for (const value of ["1-4094", sparse]) assert.equal(resource.fields.safeParse({ "vlan-ids": value }).success, true) + for (const value of ["0", "4095", "10-9", "10,10-12"]) assert.equal(resource.fields.safeParse({ "vlan-ids": value }).success, false) + const before = snapshot("bridge_vlan", [membership]) + const after = snapshot("bridge_vlan", [{ ...membership, "vlan-ids": expanded }]) + assert.equal(after.rows[0]["vlan-ids"], "1-4094", "Native expansion must not overflow the public inverse DTO") + assert.equal(verifyRosStepSnapshots({ serverId: 1, resource: "bridge_vlan", action: "update", id: "*1", fields: { "vlan-ids": "1-4094" } }, {}, before, after), true) +}) + +test("unknown dynamic flags are retained; only explicit RouterOS-generated VLAN rows are excluded", () => { + assert.equal(snapshot("bridge_vlan", [{ ...membership, dynamic: "unknown" }]).rows.length, 1) + for (const dynamic of [true, "true", "yes"]) assert.equal(snapshot("bridge_vlan", [{ ...membership, dynamic }]).rows.length, 0) + assert.equal(snapshot("bridge_vlan", [{ ...membership, dynamic: "false" }]).rows.length, 1) +}) + +test("old bridge CPU, port and VLAN projection contracts cannot execute after semantic projection changes", () => { + for (const resource of ["bridge", "bridge_port", "bridge_vlan"]) { + const current = snapshot(resource, resource === "bridge_vlan" ? [membership] : [{ ".id": "*2", bridge: "br", interface: "ether2", pvid: "10" }]) + assert.equal(current.projectionVersion, 4) + assert.throws(() => createRosPrefix([{ ...current, projectionVersion: 3 }]), error => error instanceof McpError && error.code === "PLAN_EXPIRED") + } +}) +test("CPU frame admission and MVRP configuration are sealed, with no MVRP write permission", () => { + const bridge = { ".id": "*1", name: "br", pvid: "10", "vlan-filtering": "true", "frame-types": "admit-all", "ingress-filtering": "true", mvrp: "false", running: "true" } + const original = snapshot("bridge", [bridge]) + for (const field of ["frame-types", "ingress-filtering", "mvrp"]) { + const changed = snapshot("bridge", [{ ...bridge, [field]: field === "frame-types" ? "admit-only-vlan-tagged" : "false" === bridge[field as keyof typeof bridge] ? "true" : "false" }]) + assert.notEqual(changed.fingerprint, original.fingerprint) + } + assert.equal(snapshot("bridge", [{ ...bridge, running: "false" }]).fingerprint, original.fingerprint) + assert.equal(resourceById("bridge").fields.safeParse({ mvrp: true }).success, false) +}) diff --git a/backend/src/modules/mcp/bridge-vlan-recovery.ts b/backend/src/modules/mcp/bridge-vlan-recovery.ts new file mode 100644 index 00000000..cfa999da --- /dev/null +++ b/backend/src/modules/mcp/bridge-vlan-recovery.ts @@ -0,0 +1,135 @@ +import { bridgeVlanStateSchema, type BridgeVlanDesired, type BridgeVlanState } from "@mmapp/contracts/bridge-vlan" +import { bridgeVlanCurrentDesired } from "../../services/bridge-vlan-configuration.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import { prepareBridgeVlanConfiguration, readBridgeVlanState } from "./bridge-vlan-actions.js" +import { acknowledgedCreateId, assertNoCallerCreateId, recoveryRosStepId } from "./native-create-identity.js" +import { readRosSnapshot, rosSnapshotVersion, type RosSnapshot, type RosStep } from "./resources.js" +import { verifyRosStepSnapshots } from "./ros-step-verification.js" +import { decryptSecret, fingerprint, McpError } from "./security.js" + +type Checkpoint = { step_index: number; before_sealed: string; after_sealed: string | null } +type Intended = { step_index: number; state: string; result: unknown } +const key = (snapshot: Pick) => `${snapshot.serverId}:${snapshot.resource}` +function conflict(message: string): never { throw new McpError("STATE_CONFLICT", message, 409) } + +export function needsBridgeVlanRecovery(sourceAction: string, evidence: ChangeEvidence): boolean { + if (sourceAction === "network.bridge-vlan.configure") return true + return evidence.steps.some(change => { + if (change.kind !== "ros") return false + const step = change.step + if (step.resource === "bridge") return step.action === "update" && Object.keys(step.fields).some(field => ["pvid", "vlan-filtering", "frame-types", "ingress-filtering"].includes(field)) + const sensitive = step.resource === "bridge_port" ? ["bridge", "interface", "pvid", "ingress-filtering", "frame-types", "disabled"] + : step.resource === "bridge_vlan" ? ["bridge", "vlan-ids", "tagged", "untagged", "disabled"] : [] + return sensitive.length > 0 && (step.action !== "update" || Object.keys(step.fields).some(field => sensitive.includes(field))) + }) +} + +function validSnapshot(snapshot: RosSnapshot): boolean { + return snapshot.projectionVersion === rosSnapshotVersion(snapshot.resource) && !!snapshot.sourceProof && Array.isArray(snapshot.rows) && snapshot.fingerprint === fingerprint(snapshot.rows) +} +function sameSnapshot(left: RosSnapshot, right: RosSnapshot): boolean { + return validSnapshot(left) && validSnapshot(right) && key(left) === key(right) && left.fingerprint === right.fingerprint && left.sourceProof === right.sourceProof +} +function stateFromSnapshots(serverId: number, snapshots: Map): BridgeVlanState { + const rows = (resource: string) => { + const snapshot = snapshots.get(key({ serverId, resource })) + if (!snapshot || !validSnapshot(snapshot)) conflict("Нет полного исходного снимка bridge VLAN с актуальной проекцией") + return snapshot.rows + } + return bridgeVlanStateSchema.parse({ bridges: rows("bridge"), ports: rows("bridge_port"), vlans: rows("bridge_vlan"), + vlanInterfaces: rows("vlan"), ipv4Addresses: rows("ipv4_address"), ipv6Addresses: rows("ipv6_address"), + interfaces: ["ethernet", "bridge", "vlan", "gre", "wireguard", "vxlan"].flatMap(resource => rows(resource).map(row => ({ resource, id: String(row[".id"]), name: String(row.name), disabled: row.disabled }))), + }) +} + +/** Fresh typed inverse of a completely verified transition. Deleted port/VLAN + * rows are recreated without their old IDs; new rows are removed only by ACK. */ +export async function prepareBridgeVlanRecovery(input: { + operationId: string; evidence: ChangeEvidence; checkpoints: Checkpoint[]; intended: Intended[]; +}, context: ActionContext): Promise { + const { operationId, evidence, checkpoints, intended } = input + if (!evidence.steps.length || evidence.steps.some(change => change.kind !== "ros" || !["bridge", "bridge_port", "bridge_vlan"].includes(change.step.resource))) { + throw new McpError("UNSUPPORTED_CAPABILITY", "Обратный VLAN план не поддерживает смешанные ресурсы", 409) + } + const native = evidence.steps.map(change => (change as { kind: "ros"; step: RosStep }).step) + if (native.some(step => step.resource === "bridge" && step.action !== "update")) { + throw new McpError("UNSUPPORTED_CAPABILITY", "Создание или удаление самого bridge не поддерживает типизированный обратный VLAN план", 409) + } + const serverIds = [...new Set(native.map(step => step.serverId))] + if (serverIds.length !== 1 || evidence.target.scope !== "servers" || evidence.target.serverIds.length !== 1 || evidence.target.serverIds[0] !== serverIds[0]) conflict("Нет однозначного сервера исходного VLAN плана") + const serverId = serverIds[0] + if (!context.connection.policy.scope.serverIds.includes(serverId)) throw new McpError("SERVER_DENIED", "VLAN сервер вне текущего допуска") + if (intended.length !== native.length || checkpoints.length !== native.length || new Set(intended.map(item => item.step_index)).size !== native.length || new Set(checkpoints.map(item => item.step_index)).size !== native.length) conflict("Нет полного набора подтверждённых шагов VLAN") + const baseline = new Map() + for (const snapshot of evidence.snapshots) { + if (snapshot.serverId !== serverId || !validSnapshot(snapshot) || baseline.has(key(snapshot))) conflict("Исходные VLAN снимки или их версии не подтверждены") + baseline.set(key(snapshot), snapshot) + } + const baselineState = stateFromSnapshots(serverId, baseline), expected = new Map(baseline) + const created: Array<{ resource: "bridge_port" | "bridge_vlan"; id: string }> = [] + const deleted: Array<{ resource: "bridge_port" | "bridge_vlan"; id: string }> = [] + for (let index = 0; index < native.length; index++) { + const step = native[index], checkpoint = checkpoints.find(item => item.step_index === index), receipt = intended.find(item => item.step_index === index) + assertNoCallerCreateId(step) + if (!checkpoint?.before_sealed || !checkpoint.after_sealed || receipt?.state !== "verified") conflict("Нет полного проверенного checkpoint VLAN; требуется ручная сверка") + const before = JSON.parse(await decryptSecret(checkpoint.before_sealed)) as RosSnapshot + const after = JSON.parse(await decryptSecret(checkpoint.after_sealed)) as RosSnapshot + const previous = expected.get(key({ serverId, resource: step.resource })) + if (!previous || !sameSnapshot(previous, before) || !validSnapshot(after) || before.sourceProof !== after.sourceProof || !verifyRosStepSnapshots(step, receipt.result, before, after)) conflict("Исходный переход VLAN или источник не подтверждён") + if (step.action === "create") { + const ack = acknowledgedCreateId(receipt.result), actual = recoveryRosStepId(step, before.rows, after.rows) + if (!ack || actual !== ack || !["bridge_port", "bridge_vlan"].includes(step.resource)) conflict("Нет точного ACK и checkpoint ID созданного VLAN объекта") + created.push({ resource: step.resource as "bridge_port" | "bridge_vlan", id: ack }) + } else if (step.action === "delete") { + if (!step.id || !["bridge_port", "bridge_vlan"].includes(step.resource)) conflict("Не подтверждён ID удалённого VLAN объекта") + deleted.push({ resource: step.resource as "bridge_port" | "bridge_vlan", id: step.id }) + } + expected.set(key(after), after) + } + const bridgeNames = new Set(native.map(step => { + const rows = step.resource === "bridge" ? baselineState.bridges : step.resource === "bridge_port" ? baselineState.ports : baselineState.vlans + const row = rows.find(item => item[".id"] === step.id) + const name = step.resource === "bridge" ? row?.name : step.fields.bridge ?? row?.bridge + if (typeof name !== "string" || !name) conflict("Не подтверждён исходный bridge VLAN") + return name + })) + const bridges = baselineState.bridges.filter(row => bridgeNames.has(String(row.name))) + if (bridgeNames.size !== 1 || bridges.length !== 1) conflict("Обратный VLAN план требует один однозначный bridge") + const bridgeId = String(bridges[0][".id"]), desired: BridgeVlanDesired = bridgeVlanCurrentDesired(baselineState, bridgeId) + for (const item of deleted) { + const terminal = expected.get(key({ serverId, resource: item.resource })) + const original = item.resource === "bridge_port" ? desired.ports.find(row => row.id === item.id) : desired.vlans.find(row => row.id === item.id) + if (!original || !terminal || terminal.rows.some(row => row[".id"] === item.id)) conflict("Нет подтверждённого удаления исходной VLAN identity") + delete original.id + } + for (const item of created) { + const originalRows = baseline.get(key({ serverId, resource: item.resource }))!.rows + const terminalRows = expected.get(key({ serverId, resource: item.resource }))!.rows + if (originalRows.some(row => row[".id"] === item.id) || !terminalRows.some(row => row[".id"] === item.id)) conflict("ACK ID VLAN не является подтверждённым новым объектом") + if (item.resource === "bridge_port") desired.removePortIds.push(item.id) + else desired.removeVlanIds.push(item.id) + } + const compare = async (snapshots: RosSnapshot[]): Promise => { + const captured = new Map(snapshots.map(snapshot => [key(snapshot), snapshot])) + for (const snapshot of snapshots) { + const terminal = expected.get(key(snapshot)) + if (!terminal || !sameSnapshot(terminal, snapshot)) conflict("После операции изменились VLAN конфигурация, зависимости или источник; обратный план запрещён") + } + for (const [resourceKey, terminal] of expected) { + const current = captured.get(resourceKey) ?? await readRosSnapshot(serverId, terminal.resource, undefined, terminal.sourceProof) + if (!sameSnapshot(terminal, current)) conflict("После операции обнаружен drift зависимости VLAN; обратный план запрещён") + captured.set(resourceKey, current) + } + return [...captured.values()] + } + await compare((await readBridgeVlanState(serverId, context)).snapshots) + // Reconstruct through the same model, with a freshly verified management + // channel. Old receipts never confer authority to run the inverse. + const inverse = await prepareBridgeVlanConfiguration({ serverId, bridgeId, desired }, context) + const snapshots = await compare(inverse.snapshots) + const objects = new Map([...evidence.objects, ...inverse.objects].map(object => [`${object.serverId}/${object.kind}/${object.id}`, object])) + return { ...inverse, snapshots, objects: [...objects.values()], requiresHuman: true, + warnings: [`Типизированный обратный VLAN план операции ${operationId}; все исходные шаги и ACK проверены.`, ...inverse.warnings, + "Удалённые port/VLAN rows создаются с новыми ID RouterOS. Unknown операции не повторяются; восстановление самого bridge не поддерживается."], + } +} diff --git a/backend/src/modules/mcp/catalog-scope.ts b/backend/src/modules/mcp/catalog-scope.ts new file mode 100644 index 00000000..bbcbb3e6 --- /dev/null +++ b/backend/src/modules/mcp/catalog-scope.ts @@ -0,0 +1,16 @@ +import { z } from "zod" +import { and, eq, inArray, asc } from "drizzle-orm" +import { db } from "../../db/index.js" +import { servers } from "../../db/schema.js" +import type { ActionContext } from "./action-types.js" + +export const catalogPaging = z.object({ serverId: z.number().int().positive().safe().optional(), serverOffset: z.number().int().min(0).max(10000).default(0), serverLimit: z.number().int().min(1).max(10).default(5), offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(200).default(100) }).strict() + +/** Resolve only the policy already intersected with the credential's grant, + * before constructing clients or polling a device. */ +export async function catalogDevices(input: Record, context: ActionContext, options: { includeDisabled?: boolean } = {}) { + const ids = input.serverId ? [Number(input.serverId)] : context.connection.policy.scope.serverIds + const eligible = ids.length ? await db.select().from(servers).where(and(inArray(servers.id, ids), ...(options.includeDisabled ? [] : [eq(servers.enabled, true)]))).orderBy(asc(servers.id)) : [] + const start = Number(input.serverOffset), selected = eligible.slice(start, start + Number(input.serverLimit)) + return { selected, serverIds: selected.map(row => row.id), totalServers: eligible.length, nextServerOffset: start + selected.length < eligible.length ? start + selected.length : null, incomplete: selected.length < eligible.length } +} diff --git a/backend/src/modules/mcp/change-cost.ts b/backend/src/modules/mcp/change-cost.ts new file mode 100644 index 00000000..6faa2fbf --- /dev/null +++ b/backend/src/modules/mcp/change-cost.ts @@ -0,0 +1,8 @@ +import type { ChangeEvidence } from "./action-types.js" +import { McpError } from "./security.js" + +export function changeMutationCost(evidence: Pick): number { + const cost = evidence.mutationCost + if (cost !== undefined && (!Number.isSafeInteger(cost) || cost < 1 || cost > 10000 || cost < evidence.steps.length)) throw new McpError("STATE_CONFLICT", "Не подтверждена стоимость пакетного плана", 409) + return Math.max(1, evidence.steps.length, cost ?? 0) +} diff --git a/backend/src/modules/mcp/chr-acceptance.test.ts b/backend/src/modules/mcp/chr-acceptance.test.ts new file mode 100644 index 00000000..6632a281 --- /dev/null +++ b/backend/src/modules/mcp/chr-acceptance.test.ts @@ -0,0 +1,346 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile, writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { findAction, listActions } from "./actions.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { MikrotikClient, MikrotikError } from "../../services/mikrotik.js" +import { McpError } from "./security.js" +import type { AuthUser } from "../../lib/permissions.js" +import { acceptChrOverlay, cleanupChrOverlay } from "./chr-overlay-acceptance.js" +import { cleanupChrCommunityGatewayResolvers } from "./chr-bgp-community-acceptance.js" +import { cleanupChrBgpFixture } from "./chr-bgp-acceptance.js" +import { chrNativeFailure } from "./chr-native-failure.js" +import { readChrRecoverySource } from "./chr-recovery-source.js" +import { cleanupChrOspf } from "./chr-ospf-acceptance.js" +import { chrFixtureVrf, chrOspfForwarding, chrOspfPrefix } from "./chr-fixture-context.js" +import { cleanupChrOspfPrefix, readChrOspfPrefixSnapshot } from "./chr-ospf-prefix-acceptance.js" +import { acceptChrOspfPassive } from "./chr-ospf-passive.js" +import { acceptChrWireguardInterface } from "./chr-wireguard-interface.js" +import { acceptChrWireguardPeer } from "./chr-wireguard-peer.js" +import { acceptChrWireguardInterfaceUpdate } from "./chr-wireguard-interface-update.js" + +// Explicit lab allowlist: cannot accidentally point this mutating test at production. +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.ok(hosts.length > 0 && hosts.length <= 2 && new Set(hosts).size === hosts.length) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const username = process.env.MMAPP_TEST_CHR_USER, password = process.env.MMAPP_TEST_CHR_PASSWORD +assert.ok(username && password, "Explicit lab credentials required") +const tunnelPorts = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (tunnelPorts) assert.ok(tunnelPorts.length === hosts.length && tunnelPorts.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +const actor: AuthUser = { id: "chr-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:bgp:admin", "mm:servers:admin", "mm:filters:read"], expiresAt: Date.now() + 3600000 } +const marker = "mcp-test-" + randomUUID().slice(0, 8) +const routingAcceptance = process.env.MMAPP_TEST_CHR_ROUTING === "true" +const overlayAcceptance = process.env.MMAPP_TEST_CHR_OVERLAY === "true" +const ospfAcceptance = process.env.MMAPP_TEST_CHR_OSPF === "true" +const ospfSourceAcceptance = process.env.MMAPP_TEST_CHR_OSPF_SOURCES === "true" +const ospfForwarding = chrOspfForwarding() +const ospfPrefix = chrOspfPrefix() +const ospfPassiveOnly = process.env.MMAPP_TEST_CHR_OSPF_PASSIVE_ONLY === "true" +const wireguardPeerOnly = process.env.MMAPP_TEST_CHR_WG_PEER_ONLY === "true" +const wireguardPeerUpdate = process.env.MMAPP_TEST_CHR_WG_PEER_UPDATE === "true" +const wireguardInterfaceDelete = process.env.MMAPP_TEST_CHR_WG_INTERFACE_DELETE === "true" +const wireguardInterfaceUpdate = process.env.MMAPP_TEST_CHR_WG_INTERFACE_UPDATE === "true" +assert.ok(!wireguardInterfaceDelete || wireguardPeerOnly && !wireguardPeerUpdate, "WireGuard interface cascade requires its isolated peer fixture mode") +assert.ok(!wireguardPeerUpdate || wireguardPeerOnly, "WireGuard peer update requires its isolated peer scenario") +assert.ok(!ospfPassiveOnly || !ospfAcceptance && !overlayAcceptance && !routingAcceptance, "Passive-only mode must be isolated from forwarding scenarios") +assert.ok(!ospfSourceAcceptance || ospfAcceptance, "Source-bound OSPF acceptance requires OSPF") +const ecmpAcceptance = process.env.MMAPP_TEST_CHR_ECMP === "true" +const bgpAcceptance = process.env.MMAPP_TEST_CHR_BGP === "true" +const bgpExportAcceptance = process.env.MMAPP_TEST_CHR_BGP_EXPORT === "true" +const bgpForwardAcceptance = process.env.MMAPP_TEST_CHR_BGP_FORWARD === "true" +const bgpFilterBindingAcceptance = process.env.MMAPP_TEST_CHR_BGP_FILTER_BINDING === "true" +const bgpCommunityAcceptance = process.env.MMAPP_TEST_CHR_BGP_COMMUNITY === "true" +assert.ok(!wireguardPeerOnly || hosts.length === 1 && !ospfPassiveOnly && !overlayAcceptance && !ospfAcceptance && !routingAcceptance && !bgpAcceptance && !ecmpAcceptance, "WireGuard peer mode requires one isolated device") +assert.ok(!wireguardInterfaceUpdate || !wireguardPeerOnly && !wireguardInterfaceDelete && !wireguardPeerUpdate && !ospfPassiveOnly && !overlayAcceptance && !ospfAcceptance && !routingAcceptance && !bgpAcceptance && !ecmpAcceptance, "WireGuard interface update requires its isolated disabled fixture mode") +assert.ok(!ospfForwarding || ospfSourceAcceptance && !bgpAcceptance && !ecmpAcceptance, "OSPF forwarding requires its isolated source-bound scenario") +assert.ok(process.env.MMAPP_TEST_CHR_COMMUNITY_GATEWAY_RESOLVER !== "true" || bgpCommunityAcceptance && process.env.MMAPP_TEST_CHR_COMMUNITY_LITERAL_GATEWAY !== "true") +assert.ok(process.env.MMAPP_TEST_CHR_BGP_BINDING_FAULTS !== "false" || bgpCommunityAcceptance) +const overlayCleanupOnly = process.env.MMAPP_TEST_CHR_OVERLAY_CLEANUP_ONLY === "true" +assert.ok(!ospfPrefix || hosts.length === 2 && overlayAcceptance && !ospfAcceptance && !ospfForwarding && !ospfPassiveOnly && !bgpAcceptance && !ecmpAcceptance && !routingAcceptance && !wireguardPeerOnly && !wireguardInterfaceUpdate, "Prefix acceptance requires its isolated two-CHR overlay") +assert.ok(!ospfPrefix || !process.env.MMAPP_TEST_CHR_OVERLAY_CLEANUP_MARKER || overlayCleanupOnly, "Prefix recovery requires cleanup-only mode") +assert.ok(!overlayCleanupOnly || overlayAcceptance && process.env.MMAPP_TEST_CHR_OVERLAY_CLEANUP_MARKER) +if (bgpFilterBindingAcceptance) actor.permissions.push("mm:filters:write") +assert.ok(!ospfAcceptance || overlayAcceptance, "OSPF acceptance requires the isolated overlay") +assert.ok(!ecmpAcceptance || overlayAcceptance, "ECMP acceptance requires the isolated overlay") +assert.ok(!bgpAcceptance || overlayAcceptance, "BGP acceptance requires the isolated overlay") +assert.ok(!bgpExportAcceptance || bgpAcceptance, "BGP prefix acceptance requires BGP and the isolated overlay") +assert.ok(!bgpForwardAcceptance || bgpExportAcceptance, "BGP forwarding probes require prefix acceptance") +assert.ok(!bgpFilterBindingAcceptance || bgpForwardAcceptance, "BGP filter-binding acceptance requires prefix and packet acceptance") +assert.ok(!bgpCommunityAcceptance || bgpFilterBindingAcceptance, "Community propagation requires filter-binding and packet acceptance") +const requestGapText = process.env.MMAPP_TEST_CHR_REQUEST_GAP_MS ?? "0" +assert.ok(["0", "100", "250", "500"].includes(requestGapText)) +const requestGapMs = Number(requestGapText) +let pacing = Promise.resolve(), lastRequestAt = 0 +const pace = async () => { + if (!requestGapMs) return + const ready = pacing.then(async () => { const wait = requestGapMs - (Date.now() - lastRequestAt); if (wait > 0) await delay(wait); lastRequestAt = Date.now() }) + pacing = ready.catch(() => {}); await ready +} +const report: Record = { startedAt: new Date().toISOString(), marker, tools: listActions().length, nativeRequestGapMs: requestGapMs, checks: [], complete: false, ...(ospfSourceAcceptance ? { optimizerRevision: findAction("mm_ospf_optimizer_plan").operation.version, measurementRevision: findAction("mm_routing_measurement_plan").operation.version, sourceMigration: "0029_mcp_routing_sources" } : {}) } +const checks = report.checks as Record[] +if (wireguardPeerOnly) Object.assign(report, { fixtureSchema: wireguardPeerUpdate ? "wg-peer-update-v1" : "wg-peer-delete-v1", ...(wireguardPeerUpdate ? { businessUpdateRevision: findAction("mm_wireguard_peer_edit_plan").operation.version, nativeUpdateRevision: findAction("mm_wireguard_peer_update_plan").operation.version } : {}), businessDeleteRevision: findAction("mm_wireguard_peer_remove_plan").operation.version, nativeDeleteRevision: findAction("mm_wireguard_peer_delete_plan").operation.version }) +if (wireguardInterfaceDelete) Object.assign(report, { fixtureSchema: "wg-interface-delete-v1", businessInterfaceDeleteRevision: findAction("mm_wireguard_interface_remove_plan").operation.version, nativeInterfaceDeleteRevision: findAction("mm_wireguard_delete_plan").operation.version }) +if (ospfForwarding) Object.assign(report, { ospfForwarding: true, fixtureSchema: "ospf-forward-v1", routeProofOperation: "routing.ospf.route.proof.read", vrfNames: [1, 2].map(id => chrFixtureVrf(marker, id)) }) +if (ospfPrefix) Object.assign(report, { fixtureSchema: "ospf-prefix-v1", hosts }) +const reportUrl = new URL(wireguardInterfaceDelete ? "../../../../docs/mcp-chr-wireguard-interface-delete-acceptance.json" : wireguardPeerUpdate ? "../../../../docs/mcp-chr-wireguard-peer-update-acceptance.json" : wireguardPeerOnly ? "../../../../docs/mcp-chr-wireguard-peer-delete-acceptance.json" : ospfPassiveOnly ? "../../../../docs/mcp-chr-ospf-passive-toggle-acceptance.json" : ospfForwarding ? (overlayCleanupOnly ? "../../../../docs/mcp-chr-ospf-forward-recovery.json" : "../../../../docs/mcp-chr-ospf-forward-acceptance.json") : ospfSourceAcceptance ? (overlayCleanupOnly ? "../../../../docs/mcp-chr-ospf-source-recovery.json" : "../../../../docs/mcp-chr-ospf-source-acceptance.json") : bgpCommunityAcceptance ? "../../../../docs/mcp-chr-bgp-community-acceptance.json" : bgpFilterBindingAcceptance ? "../../../../docs/mcp-chr-bgp-filter-binding-acceptance.json" : bgpForwardAcceptance ? "../../../../docs/mcp-chr-bgp-forward-acceptance.json" : bgpExportAcceptance ? "../../../../docs/mcp-chr-bgp-prefix-acceptance.json" : bgpAcceptance ? "../../../../docs/mcp-chr-bgp-acceptance.json" : ecmpAcceptance ? "../../../../docs/mcp-chr-ecmp-acceptance.json" : ospfAcceptance ? "../../../../docs/mcp-chr-ospf-acceptance.json" : overlayAcceptance ? "../../../../docs/mcp-chr-overlay-acceptance.json" : routingAcceptance ? "../../../../docs/mcp-chr-path-acceptance.json" : "../../../../docs/mcp-chr-acceptance.json", import.meta.url) +let checkpointQueue = Promise.resolve() +const wgUpdateReportUrl = new URL(`../../../../docs/mcp-chr-wg-interface-update-${marker.slice(-8)}-acceptance.json`, import.meta.url) +const prefixReportUrl = new URL(`../../../../docs/mcp-chr-ospf-prefix-${marker.slice(-8)}-${overlayCleanupOnly ? "recovery" : "acceptance"}.json`, import.meta.url) +if (wireguardInterfaceUpdate) Object.assign(report, { fixtureSchema: "wg-interface-update-v2", privateProofDomain: "wireguard-interface-update-v2", businessRevision: findAction("mm_wireguard_interface_edit_plan").operation.version, nativeAliasRevision: findAction("mm_wireguard_update_plan").operation.version, forwardingVerified: false }) +const checkpoint = () => { + const payload = JSON.stringify(report, null, 2) + "\n" + const pending = checkpointQueue.catch(() => {}).then(() => writeFile(ospfPrefix ? prefixReportUrl : wireguardInterfaceUpdate ? wgUpdateReportUrl : overlayCleanupOnly && !ospfSourceAcceptance ? new URL("../../../../docs/mcp-chr-overlay-recovery.json", import.meta.url) : reportUrl, payload)) + checkpointQueue = pending; return pending +} +const originalPost = MikrotikClient.prototype.post, originalPatch = MikrotikClient.prototype.patch +const originalGet = MikrotikClient.prototype.get, originalDelete = MikrotikClient.prototype.delete +const originalPut = MikrotikClient.prototype.put +const captureNativeFailure = async (error: unknown, method: string, path: string, startedAt: number) => { + if (checks.filter(row => row.phase === "native-request-failure").length >= 256) return + checks.push(chrNativeFailure(error, method, path, startedAt)); await checkpoint() +} +MikrotikClient.prototype.get = async function(path: string, timeoutMs?: number, signal?: AbortSignal, maxResponseBytes?: number): Promise { + await pace(); const startedAt = Date.now() + try { return await originalGet.call(this, path, timeoutMs, signal, maxResponseBytes) as T } catch (error) { await captureNativeFailure(error, "GET", path, startedAt); throw error } +} +MikrotikClient.prototype.delete = async function(path: string, timeoutMs?: number, beforeDispatch?:()=>Promise): Promise { + await pace(); const startedAt = Date.now() + try { const result = await originalDelete.call(this, path, timeoutMs, beforeDispatch); if (wireguardInterfaceDelete) { checks.push({ phase: "wg-cascade-native-delete-ack", path, at: new Date().toISOString() }); await checkpoint() } return result } catch (error) { await captureNativeFailure(error, "DELETE", path, startedAt); throw error } +} +MikrotikClient.prototype.put = async function(path: string, body: Record, timeoutMs?: number, beforeDispatch?:()=>Promise): Promise { + await pace(); const startedAt = Date.now() + try { return await originalPut.call(this, path, body, timeoutMs, beforeDispatch) as T } catch (error) { await captureNativeFailure(error, "PUT", path, startedAt); throw error } +} +const captureParser = async (error: unknown, path: string) => { + if (!bgpCommunityAcceptance || !(error instanceof MikrotikError) || error.statusCode !== 400 || !path.startsWith("/routing/filter/rule")) return + let detail = "" + try { const parsed = JSON.parse(error.body); if (typeof parsed.detail === "string") detail = parsed.detail } catch { /* Never save headers/raw provider payloads. */ } + detail = detail.replaceAll(password!, "[redacted]").replace(/(?:password|secret|token|private-key)\s*[=:]\s*[^\s;,]+/gi, "[redacted]").slice(0, 500) + checks.push({ phase: "community-owned-parser-error", path, nativeStatus: 400, detail }); await checkpoint() +} +{ + MikrotikClient.prototype.post = async function(path: string, body: Record, timeoutMs?: number, signal?: AbortSignal): Promise { + await pace(); const startedAt = Date.now() + try { return await originalPost.call(this, path, body, timeoutMs, signal) as T } catch (error) { await captureNativeFailure(error, "POST", path, startedAt); await captureParser(error, path); throw error } + } + MikrotikClient.prototype.patch = async function(path: string, body: Record, timeoutMs?: number, beforeDispatch?:()=>Promise): Promise { + await pace(); const startedAt = Date.now() + try { return await originalPatch.call(this, path, body, timeoutMs, beforeDispatch) as T } catch (error) { await captureNativeFailure(error, "PATCH", path, startedAt); await captureParser(error, path); throw error } + } +} +try { + await checkpoint() + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Acceptance requires an empty disposable database") + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) { + const serverId = index + 1 + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [serverId, marker, tunnelPorts ? "127.0.0.1" : host, tunnelPorts?.[index] ?? 80, username, password]) + } + const operationIds = ["routing.topology.read", "routing.path.verify", "operations.recover", "operations.reconcile", "ros.bridge.list", "ros.bridge.create", "ros.bridge.update", "ros.bridge.delete", "ros.ipv4_route.list", "ros.ipv6_route.list", ...["routing_table", "ipv4_route"].flatMap(resource => ["create", "update", "delete"].map(action => `ros.${resource}.${action}`))] + if (overlayAcceptance) operationIds.push(...["gre", "vrf", "ipv4_address", "ipv6_address"].flatMap(resource => ["create", "update", "delete"].map(action => `ros.${resource}.${action}`))) + if (wireguardPeerOnly) operationIds.push("network.wireguard.peers.delete", "ros.wireguard.create", "ros.wireguard.delete", "ros.wireguard_peer.create", "ros.wireguard_peer.delete") + if (wireguardInterfaceDelete) operationIds.push("network.wireguard.interfaces.delete", ...["ipv4_address", "ipv6_address"].flatMap(resource => ["create", "delete"].map(action => `ros.${resource}.${action}`))) + if (wireguardPeerUpdate) operationIds.push("network.wireguard.peers.update", "ros.wireguard_peer.update") + if (wireguardInterfaceUpdate) operationIds.push("network.wireguard.interfaces.update", "ros.wireguard.create", "ros.wireguard.update", "ros.wireguard.delete") + if (ospfForwarding) operationIds.push("routing.ospf.route.proof.read") + if (ospfPrefix) operationIds.push("routing.ospf.route.proof.read", "firewall.addressLists.create", "firewall.addressLists.delete", ...["ospf_instance", "ospf_area", "ospf_template"].flatMap(resource => ["create", "update", "delete"].map(action => `ros.${resource}.${action}`))) + if (ospfAcceptance) operationIds.push("diagnostics.bandwidth", "routing.ospf.optimize", ...["ospf_instance", "ospf_area", "ospf_template", "bfd_configuration"].flatMap(resource => ["create", "update", "delete"].map(action => `ros.${resource}.${action}`))) + if (ospfPassiveOnly) operationIds.push(...["ospf_instance", "ospf_area", "ospf_template", "vrf"].flatMap(resource => ["create", "update", "delete"].map(action => `ros.${resource}.${action}`))) + if (ecmpAcceptance) operationIds.push("routing.ecmp.update", "ros.ipv6_route.create", "ros.ipv6_route.delete") + if (bgpAcceptance) operationIds.push(...["bgp_instance", "bgp_connection"].flatMap(resource => ["create", "update", "delete"].map(action => `ros.${resource}.${action}`))) + if (bgpExportAcceptance) operationIds.push("routing.bgp.export", "routing.bgp.prefixes.read") + if (bgpFilterBindingAcceptance) operationIds.push("routing.bgp.filter.bind", "filters.rules.replace") + if (bgpCommunityAcceptance) operationIds.push("routing.bgp.community.policy") + if (process.env.MMAPP_TEST_CHR_COMMUNITY_GATEWAY_RESOLVER === "true") operationIds.push("ros.ipv6_route.create", "ros.ipv6_route.delete") + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + (bgpCommunityAcceptance || ospfAcceptance ? 2700000 : 1800000)).toISOString(), permissions: ["mm:network:read", "mm:network:write", ...(bgpAcceptance ? ["mm:bgp:read", "mm:bgp:write"] : []), ...(bgpExportAcceptance ? ["mm:filters:read"] : []), ...(bgpFilterBindingAcceptance ? ["mm:filters:write"] : [])], scope: { serverIds: hosts.map((_, index) => index + 1), operationIds, allowGlobal: false }, limits: { callsPerMinute: 120, concurrentReads: 4, mutationsPerHour: bgpCommunityAcceptance ? 240 : ospfAcceptance || ecmpAcceptance || bgpAcceptance ? 160 : 40, serversPerPlan: 2, objectsPerPlan: 100 } }), actor) + const principal = await authenticateMcpToken(issued.token) + const execute = async (tool: string, args: Record) => { + // Preserve the stage and IDs even if the disposable DB is removed after an + // interrupted acceptance. Never persist tool arguments or provider payloads. + const trace: Record = { phase: "mcp-operation", tool, serverId: typeof args.serverId === "number" ? args.serverId : undefined, stage: "prepare", state: "preparing" } + checks.push(trace); await checkpoint() + try { + const plan = await invokeMcpTool(principal, tool, args) as { planId: string; state: string } + trace.planId = plan.planId; trace.stage = "approval" + assert.equal(plan.state, "awaiting-approval") + await assert.rejects(applyPlan(principal, plan.planId, randomUUID())) + await approvePlan(plan.planId, actor, true) + trace.stage = "apply" + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + trace.operationId = operation.id; trace.stage = "verification"; trace.state = "running"; await checkpoint() + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + for (let attempt = 0; attempt < (wireguardInterfaceDelete && tool === "mm_wireguard_interface_remove_plan" ? 960 : 240); attempt++) { + const row = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { + trace.state = row.state; trace.stage = "terminal" + if (row.state !== "applied") { + const detail = (await pool.query("SELECT error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + trace.errorCode = detail.error_code + console.error({ tool, state: row.state, errorCode: detail.error_code }) + if (tool === "mm_routing_table_create_plan") console.error({ tableReadBack: (await readRosSnapshot(Number(args.serverId), "routing_table")).rows.filter(row => row.name === marker) }) + } + await checkpoint() + assert.equal(row.state, "applied", `Operation ${operation.id} ended ${row.state}`) + const steps = (await pool.query("SELECT state FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows + assert.ok(steps.length > 0 && steps.every(step => step.state === "verified")) + return operation.id + } + await delay(500) + } + throw new Error(`Operation ${operation.id} remains pending; do not automatically retry writes`) + } catch (error) { + if (["preparing", "running"].includes(String(trace.state))) trace.state = "unconfirmed" + trace.errorCode ??= error instanceof McpError ? error.code : "TEST_STAGE_UNCONFIRMED" + await checkpoint(); throw error + } + } + const cleanupMarker = process.env.MMAPP_TEST_CHR_CLEANUP_MARKER + const overlayCleanupMarker = process.env.MMAPP_TEST_CHR_OVERLAY_CLEANUP_MARKER + let recoveryBaseline: { serverId: number; resource: string; fingerprint: string }[] = [] + if (overlayCleanupMarker) { + assert.ok(overlayAcceptance, "Overlay cleanup requires explicit overlay mode") + if (overlayCleanupOnly) { + const recoveryReport = process.env.MMAPP_TEST_CHR_OVERLAY_RECOVERY_REPORT ?? "mcp-chr-bgp-community-interrupted-overlay.json" + assert.match(recoveryReport, /^mcp-chr-[a-z0-9-]+\.json$/) + const previous = JSON.parse(await readFile(new URL("../../../../docs/" + recoveryReport, import.meta.url), "utf8")) + assert.equal(previous.marker, overlayCleanupMarker) + assert.equal(previous.complete, false) + assert.ok(typeof previous.finishedAt === "string" && Number.isFinite(Date.parse(previous.finishedAt)) && Date.parse(previous.finishedAt) <= Date.now(), "Recovery refuses a report whose original process has not finished") + const recoverySource = readChrRecoverySource(previous, overlayCleanupMarker) + assert.equal(previous.fixtureSchema === "ospf-prefix-v1", ospfPrefix, "Prefix recovery mode must match the source report") + if (ospfPrefix) assert.deepEqual(previous.hosts, hosts, "Prefix recovery requires the original sender and receiver") + assert.equal(recoverySource.ospfForwarding, ospfForwarding, "Recovery fixture mode must match the source report") + recoveryBaseline = recoverySource.baselines + checks.push({ phase: "recovery-source", report: recoveryReport, kind: recoverySource.kind, originalMarker: overlayCleanupMarker, originalFinishedAt: previous.finishedAt, baselines: recoveryBaseline.length }); await checkpoint() + if (recoverySource.kind === "ospf") { + assert.ok(ospfAcceptance, "OSPF recovery requires explicitly scoped OSPF permissions") + await cleanupChrOspf(overlayCleanupMarker, execute) + } + if (recoverySource.kind === "ospf-prefix") await cleanupChrOspfPrefix(overlayCleanupMarker, execute) + if (bgpAcceptance) await cleanupChrBgpFixture(overlayCleanupMarker, execute) + if (process.env.MMAPP_TEST_CHR_COMMUNITY_GATEWAY_RESOLVER === "true") { + const recordedRoutes = previous.checks.filter((row: { phase?: string }) => row.phase === "community-gateway-resolver-created").flatMap((row: { nativeRoutes?: unknown }) => { assert.ok(Array.isArray(row.nativeRoutes) && row.nativeRoutes.length === 1); return row.nativeRoutes }) + await cleanupChrCommunityGatewayResolvers(overlayCleanupMarker, execute, recordedRoutes) + } + } + await cleanupChrOverlay(hosts, overlayCleanupMarker, execute) + checks.push({ interruptedOverlayRemoved: overlayCleanupMarker }) + } + if (cleanupMarker) { + assert.match(cleanupMarker, /^mcp-test-[0-9a-f]{8}$/) + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const tables = (await readRosSnapshot(serverId, "routing_table")).rows.filter(row => row.name === cleanupMarker) + if (!tables.length) continue + assert.ok(tables.every(row => row.fib === "false"), "Cleanup only accepts isolated non-FIB tables") + assert.ok(!(await client.get[]>("/routing/rule")).some(row => row.table === cleanupMarker)) + for (const resource of ["ipv4_route", "ipv6_route"]) assert.ok(!(await readRosSnapshot(serverId, resource)).rows.some(row => row["routing-table"] === cleanupMarker), "Interrupted table must be empty before cleanup") + for (const row of tables) await execute("mm_routing_table_delete_plan", { serverId, id: row[".id"] }) + checks.push({ host, interruptedRunCleanup: cleanupMarker, emptyNonFibTableRemoved: true }) + } + } + if (overlayCleanupOnly) { + const readBaseline = ospfPrefix ? readChrOspfPrefixSnapshot : readRosSnapshot + for (const baseline of recoveryBaseline) assert.equal((await readBaseline(baseline.serverId, baseline.resource)).fingerprint, baseline.fingerprint, "Interrupted overlay baseline not restored") + checks.push({ phase: "interrupted-overlay-baseline-restored", marker: overlayCleanupMarker, restoredFingerprints: recoveryBaseline.length, newFixtureCreated: false }); await checkpoint() + } else if (wireguardInterfaceUpdate) await acceptChrWireguardInterfaceUpdate(marker, principal, hosts.map((_, index) => index + 1), execute, checks, checkpoint) + else if (wireguardInterfaceDelete) await acceptChrWireguardInterface(marker, principal, execute, checks, checkpoint) + else if (wireguardPeerOnly) await acceptChrWireguardPeer(marker, principal, execute, checks, checkpoint, wireguardPeerUpdate) + else if (ospfPassiveOnly) await acceptChrOspfPassive(marker, execute, checks, checkpoint) + else if (overlayAcceptance) await acceptChrOverlay(hosts, marker, principal, execute, checks, checkpoint) + else for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const resource = await client.get>("/system/resource") + const baseline = await readRosSnapshot(serverId, "bridge") + assert.ok(!baseline.rows.some(row => row.name === marker)) + const routeBaseline = await Promise.all(["ipv4_route", "ipv6_route"].map(resource => readRosSnapshot(serverId, resource))) + const tableBaseline = await readRosSnapshot(serverId, "routing_table") + assert.ok(!tableBaseline.rows.some(row => row.name === marker)) + const topology = await invokeMcpTool(principal, "mm_routing_topology_get", { serverId }) as { resources: Record; observedPath: boolean } + assert.equal(topology.observedPath, false) + assert.ok(topology.resources.interfaces.observed && topology.resources.ipv4.observed && topology.resources.ipv6.observed) + let pathProof: unknown + if (routingAcceptance && hosts.length === 2) { + const peerId = serverId === 1 ? 2 : 1, { client: peerClient } = await routerClient(peerId) + const addresses = await client.get[]>("/ip/address"), peerAddresses = await peerClient.get[]>("/ip/address") + const source = addresses.filter(row => row.interface === "ether1" && row.disabled !== "true" && row.address.split("/")[0] === host), peer = peerAddresses.filter(row => row.interface === "ether1" && row.disabled !== "true" && row.address.split("/")[0] === hosts[peerId - 1]) + assert.equal(source.length, 1); assert.equal(peer.length, 1) + // Two physical devices validate the command/parser and direct control path. + // JumpHost/ExitNode collapse to the peer; this is explicitly not a 3-node failover lab. + const profile = { name: marker, serverId, community: "65000:65000", family: "ipv4", probeVrf: "main", candidates: [{ id: "direct-lab-path", jumpHostId: peerId, exitNodeId: peerId, wanInterface: "ether1", downstreamInterface: "ether1", gateway: peer[0].address.split("/")[0], gatewayRef: { resource: "ipv4_route", id: "not-used-by-probe" }, controlProbe: { targetInterface: "ether1", targetAddress: hosts[peerId - 1], sourceAddress: host, expectedHops: [{ serverId: peerId, interface: "ether1", address: hosts[peerId - 1] }] } }] } + pathProof = await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "direct-lab-path", proofPurpose: "control-path" }) + assert.equal((pathProof as { status: string }).status, "passed", JSON.stringify(pathProof)) + } + await assert.rejects(invokeMcpTool(principal, "mm_bridge_list", { serverId: 99999 })) + // Never touch an existing interface: an empty disabled bridge has no ports or addresses. + let cleanup = false + try { + cleanup = true // Includes ambiguous create outcomes: finally re-reads before attempting cleanup. + await execute("mm_bridge_create_plan", { serverId, fields: { name: marker, disabled: true, comment: marker } }) + let own = (await readRosSnapshot(serverId, "bridge")).rows.filter(row => row.name === marker && row.comment === marker) + assert.equal(own.length, 1); assert.equal(own[0].disabled, "true") + const id = own[0][".id"] + const updateOperationId = await execute("mm_bridge_update_plan", { serverId, id, fields: { comment: marker + ":verified" } }) + own = (await readRosSnapshot(serverId, "bridge")).rows.filter(row => row[".id"] === id) + assert.equal(own[0].comment, marker + ":verified") + await execute("mm_operation_recovery_plan", { operationId: updateOperationId }) + own = (await readRosSnapshot(serverId, "bridge")).rows.filter(row => row[".id"] === id) + assert.equal(own[0].comment, marker) + await execute("mm_bridge_delete_plan", { serverId, id }) + cleanup = false + } finally { + if (cleanup) { + const own = (await readRosSnapshot(serverId, "bridge")).rows.filter(row => row.name === marker && [marker, marker + ":verified"].includes(String(row.comment)) && row.disabled === "true") + // Bounded cleanup uses the same approved executor; no broad name matching or direct DELETE. + for (const row of own) await execute("mm_bridge_delete_plan", { serverId, id: row[".id"] }) + } + } + assert.equal((await readRosSnapshot(serverId, "bridge")).fingerprint, baseline.fingerprint, "Bridge configuration must return to its baseline") + if (routingAcceptance) { + // No rules or interfaces select this unique non-FIB table. Its routes never enter + // packet forwarding; existing main/default/management routes remain untouched. + try { + await execute("mm_routing_table_create_plan", { serverId, fields: { name: marker, fib: false } }) + const tables = (await readRosSnapshot(serverId, "routing_table")).rows.filter(row => row.name === marker) + assert.equal(tables.length, 1); assert.equal(tables[0].fib, "false") + await execute("mm_ipv4_route_create_plan", { serverId, fields: { "dst-address": "192.0.2.0/24", "routing-table": marker, gateway: "ether1", distance: 10, disabled: true, comment: marker } }) + const own = (await readRosSnapshot(serverId, "ipv4_route")).rows.filter(row => row.comment === marker && row["routing-table"] === marker) + assert.equal(own.length, 1) + await assert.rejects(invokeMcpTool(principal, "mm_routing_table_delete_plan", { serverId, id: tables[0][".id"] }), /Таблица используется/) + const id = own[0][".id"] + const operationId = await execute("mm_ipv4_route_update_plan", { serverId, id, fields: { distance: 20, disabled: false } }) + let current = (await readRosSnapshot(serverId, "ipv4_route")).rows.find(row => row[".id"] === id)! + assert.equal(current.distance, "20"); assert.equal(current.disabled, "false") + await execute("mm_operation_recovery_plan", { operationId }) + current = (await readRosSnapshot(serverId, "ipv4_route")).rows.find(row => row[".id"] === id)! + assert.equal(current.distance, "10"); assert.equal(current.disabled, "true") + checks.push({ host, isolatedRouting: { tableFib: false, prefix: "192.0.2.0/24", approvedRouteCreateUpdate: true, enabledReadBack: true, usedTableDeletionRejected: true, approvedInverseRestoredDistanceAndDisabled: true, forwardingTest: false } }) + } finally { + // Exact ownership and destination checks, including ambiguous create results. + const own = (await readRosSnapshot(serverId, "ipv4_route")).rows.filter(row => row.comment === marker && row["routing-table"] === marker && row["dst-address"] === "192.0.2.0/24") + for (const row of own) await execute("mm_ipv4_route_delete_plan", { serverId, id: row[".id"] }) + assert.ok(!(await readRosSnapshot(serverId, "ipv4_route")).rows.some(row => row["routing-table"] === marker)) + for (const row of (await readRosSnapshot(serverId, "routing_table")).rows.filter(row => row.name === marker)) await execute("mm_routing_table_delete_plan", { serverId, id: row[".id"] }) + } + assert.equal((await readRosSnapshot(serverId, "routing_table")).fingerprint, tableBaseline.fingerprint, "Routing table configuration must return to baseline") + } + for (const before of routeBaseline) assert.equal((await readRosSnapshot(serverId, before.resource)).fingerprint, before.fingerprint, "Existing route configuration changed during acceptance") + checks.push({ host, routerOs: resource.version, topology: Object.fromEntries(Object.entries(topology.resources).map(([key, value]) => [key, { observed: value.observed, count: value.items.length }])), approvedCreateUpdateDelete: true, approvedInversePlan: true, pathProof, twoDeviceControlPathOnly: routingAcceptance, idempotency: true, readBack: true, bridgeBaselineRestored: true, routeConfigurationUnchanged: true }) + console.log(`✓ ${host}: actual topology, approval, create/update/delete, read-back, idempotency, cleanup`) + } + report.complete = true +} finally { + MikrotikClient.prototype.post = originalPost; MikrotikClient.prototype.patch = originalPatch + MikrotikClient.prototype.get = originalGet; MikrotikClient.prototype.delete = originalDelete + MikrotikClient.prototype.put = originalPut + report.finishedAt = new Date().toISOString() + await checkpoint() + await closePool() +} diff --git a/backend/src/modules/mcp/chr-address-list.test.ts b/backend/src/modules/mcp/chr-address-list.test.ts new file mode 100644 index 00000000..88005293 --- /dev/null +++ b/backend/src/modules/mcp/chr-address-list.test.ts @@ -0,0 +1,98 @@ +import { fingerprint } from "./security.js" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === 2 && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const marker = "mcp-test-" + randomUUID().slice(0, 8) +const actor: AuthUser = { id: "chr-address-list-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const report = { startedAt: new Date().toISOString(), marker, complete: false, packetMatchingVerified: false, fixtures: "unused-address-list-v4-v6-permanent-and-timed", checks, finishedAt: "" } +const save = () => writeFile(new URL("../../../../docs/mcp-chr-address-list-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, marker, ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const operationIds = ["firewall.addressLists.create", "firewall.addressLists.update", "firewall.addressLists.toggle", "firewall.addressLists.delete"] + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, mutationsPerHour: 100 }, scope: { serverIds: [1, 2], operationIds, allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const execute = async (tool: string, input: Record) => { + const plan = await invokeMcpTool(principal, tool, input) as { planId: string } + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + for (let attempt = 0; attempt < 200; attempt++) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { + assert.equal(row.state, "applied", tool + ": " + row.state + "/" + row.error_code) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + return operation.id + } + await delay(300) + } + throw Error("Operation pending; never replay lab writes automatically") + } + await save() + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const resources = ["ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw"] + const baseline = await Promise.all(resources.map(resource => readRosSnapshot(serverId, resource))) + const paths = ["/ip/firewall/address-list", "/ipv6/firewall/address-list"] + const addressBaseline = await Promise.all(paths.map(path => client.get[]>(path))) + assert.ok(addressBaseline.every(rows => rows.length === 0), "Lab refuses existing address-list entries") + checks.push({ host, phase: "preflight", baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })), addressFingerprints: addressBaseline.map(fingerprint) }); await save() + try { + for (const family of ["ip", "ip6"] as const) { + const path = family === "ip" ? paths[0] : paths[1] + const fields = { list: marker, address: family === "ip" ? "198.18.250.1/32" : "2001:db8:250::1/128", disabled: true, comment: marker } + const invoke = (operation: string, extra: Record) => execute("mm_firewall_address_list_" + operation + "_plan", { serverId, address: { family, operation, ...extra } }) + await invoke("create", { fields }) + let rows = await client.get[]>(path) + assert.equal(rows.length, 1); const rosId = rows[0][".id"] + assert.equal(rows[0].disabled, "true"); assert.equal(rows[0].list, marker) + await invoke("update", { rosId, fields: { ...fields, address: family === "ip" ? "198.18.250.0-198.18.250.255" : "2001:db8:250:0:0:0:0:1/64", comment: marker + " edited" } }) + await invoke("toggle", { rosId, disabled: false }) + rows = await client.get[]>(path); assert.equal(rows[0].disabled, "false") + await invoke("toggle", { rosId, disabled: true }); await invoke("delete", { rosId }) + assert.deepEqual(await client.get(path), []) + await invoke("create", { fields: { ...fields, disabled: false, timeout: "2m" } }) + rows = await client.get[]>(path); assert.equal(rows.length, 1); assert.equal(rows[0].dynamic, "true") + const timedId = rows[0][".id"] + const timedPlan = await invokeMcpTool(principal, "mm_firewall_address_list_delete_plan", { serverId, address: { family, operation: "delete", rosId: timedId } }) as { planId: string } + await delay(1500); await approvePlan(timedPlan.planId, actor, true) + const timedDelete = await applyPlan(principal, timedPlan.planId, randomUUID()) + for (let attempt = 0; attempt < 200; attempt++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [timedDelete.id])).rows[0].state + if (!["pending", "running"].includes(state)) { assert.equal(state, "applied"); break } + if (attempt === 199) throw Error("Timed delete pending; do not replay") + await delay(300) + } + assert.deepEqual(await client.get(path), []) + checks.push({ host, family, phase: "permanent-crud-and-countdown-delete", idempotencyVerified: true, unusedList: true, nativeTimedFields: { dynamic: rows[0].dynamic, timeout: rows[0].timeout } }); await save() + } + } finally { + for (const family of ["ip", "ip6"]) { + const path = family === "ip" ? paths[0] : paths[1] + for (const row of await client.get[]>(path)) if (row.list === marker && row.comment?.startsWith(marker)) await execute("mm_firewall_address_list_delete_plan", { serverId, address: { family, operation: "delete", rosId: row[".id"] } }) + } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Firewall baseline changed") + for (const [index, path] of paths.entries()) assert.equal(fingerprint(await client.get(path)), fingerprint(addressBaseline[index]), "Address-list baseline changed") + checks.push({ host, phase: "cleanup", baselineRestored: true }); await save() + } + console.log("✓ CHR " + serverId + ": IPv4/IPv6 address-list CRUD, native canonicalization, finite countdown deletion and cleanup") + } + report.complete = true; report.finishedAt = new Date().toISOString(); await save() +} finally { await closePool() } diff --git a/backend/src/modules/mcp/chr-bgp-acceptance.ts b/backend/src/modules/mcp/chr-bgp-acceptance.ts new file mode 100644 index 00000000..fa4ef3fd --- /dev/null +++ b/backend/src/modules/mcp/chr-bgp-acceptance.ts @@ -0,0 +1,186 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import { invokeMcpTool } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { McpPrincipal } from "./store.js" +import { acceptChrBgpPrefixPath } from "./chr-bgp-forward-acceptance.js" +import { acceptChrBgpCommunity, cleanupChrCommunityPolicy, cleanupChrCommunityGatewayResolvers } from "./chr-bgp-community-acceptance.js" +type Execute = (tool: string, args: Record) => Promise +/** Recovery of an already terminated fixture, never creation or replay. */ +export async function cleanupChrBgpFixture(marker: string, execute: Execute) { + assert.match(marker, /^mcp-test-[a-f0-9]{8}$/) + const instance = marker + "-bgp", vrf = marker + "-vrf", subnet = Number.parseInt(marker.slice(-2), 16), hex = Number.parseInt(marker.slice(-4), 16).toString(16) + for (const serverId of [1, 2]) { + for (const resource of ["bgp_connection", "bgp_instance"]) { + const own = (await readRosSnapshot(serverId, resource)).rows.filter(row => row.comment === marker) + assert.ok(own.length <= (resource === "bgp_connection" ? 2 : 1)) + for (const row of own) { + assert.equal(row.vrf, vrf); assert.equal(row["routing-table"], vrf) + if (resource === "bgp_instance") { assert.equal(row.name, instance); assert.equal(String(row.as), serverId === 1 ? "64512" : "64513") } + else { + assert.ok([marker + "-peer", marker + "-peer-v6"].includes(String(row.name))); assert.equal(row.instance, instance) + const v6 = row.name === marker + "-peer-v6", width = v6 ? 128 : 32 + const address = (id: number) => v6 ? `fd6d:6370:${hex}:1::${id}` : `198.18.${subnet}.${id}` + const expected = (value: unknown, id: number) => [address(id), address(id) + "/" + width, address(id) + "@" + vrf, address(id) + "/" + width + "@" + vrf].includes(String(value)) + assert.ok(expected(row["remote.address"], serverId === 1 ? 2 : 1) && expected(row["local.address"], serverId), "Recovery refuses changed BGP endpoints") + } + await execute(`mm_${resource}_delete_plan`, { serverId, id: row[".id"] }) + } + } + } + await cleanupChrCommunityPolicy(marker, execute) + for (const serverId of [1, 2]) { + const { client } = await routerClient(serverId) + const filters = await client.get[]>("/routing/filter/rule") + assert.ok(filters.every(row => row.chain === "bgp-in" && row.comment === "MikrotikManager: " + marker), "Recovery refuses foreign input filters") + if (filters.length) await execute("mm_filter_rules_replace_plan", { serverId, rules: [], requireUnbound: true }) + } +} + +export async function acceptChrBgp(marker: string, principal: McpPrincipal, execute: Execute, checks: Record[], checkpoint: () => Promise) { + const instance = marker + "-bgp", connection = marker + "-peer", vrf = marker + "-vrf", subnet = Number.parseInt(marker.slice(-2), 16) + const addresses = [`198.18.${subnet}.1`, `198.18.${subnet}.2`] + const exportAcceptance = process.env.MMAPP_TEST_CHR_BGP_EXPORT === "true", hextet = Number.parseInt(marker.slice(-4), 16).toString(16) + const bindingAcceptance = process.env.MMAPP_TEST_CHR_BGP_FILTER_BINDING === "true" + const communityAcceptance = process.env.MMAPP_TEST_CHR_BGP_COMMUNITY === "true" + const peers = [{ name: connection, family: "ipv4", afi: exportAcceptance ? "ip" : "ip,ipv6", addresses }, + ...(exportAcceptance ? [{ name: connection + "-v6", family: "ipv6", afi: "ipv6", addresses: [`fd6d:6370:${hextet}:1::1`, `fd6d:6370:${hextet}:1::2`] }] : [])] + const baseline = [] + for (const serverId of [1, 2]) for (const resource of ["bgp_instance", "bgp_connection", "ipv4_route", "ipv6_route", ...(bindingAcceptance ? ["routing_filter"] : [])]) baseline.push(await readRosSnapshot(serverId, resource)) + if (bindingAcceptance) for (const row of baseline.filter(row => row.resource === "routing_filter")) assert.equal(row.rows.length, 0, "Binding fixture requires initially empty routing filters") + assert.ok(!baseline.flatMap(snapshot => snapshot.rows).some(row => row.name === instance || row.name === connection || row.comment === marker)) + const observe = async (serverId: number, established: boolean, family?: string) => { + const { client } = await routerClient(serverId) + const selected = peers.filter(peer => !family || peer.family === family) + for (let attempt = 0; attempt < 30; attempt++) { + const sessions = await client.get[]>("/routing/bgp/session") + const matches = (row: Record, peer: typeof peers[number]) => row.instance === instance && row.name?.startsWith(peer.name + "-") && row["remote.address"]?.split("@")[0].split("/")[0] === peer.addresses[serverId === 1 ? 1 : 0] + const own = sessions.filter(row => selected.some(peer => matches(row, peer))) + if (established ? own.length === selected.length && selected.every(peer => own.filter(row => matches(row, peer) && row.established === "true").length === 1) : own.every(row => row.established !== "true")) { + if (established) { + const parent = (await readRosSnapshot(serverId, "bgp_instance")).rows.find(row => row.name === instance && row.comment === marker)! + const bindings = (await readRosSnapshot(serverId, "bgp_connection")).rows + assert.equal(parent.vrf, vrf); assert.equal(parent["routing-table"], vrf) + for (const peer of selected) { + const row = own.find(row => matches(row, peer))!, binding = bindings.find(row => row.name === peer.name && row.comment === marker)! + checks.push({ serverId, phase: "bgp-session-observed", family: peer.family, session: Object.fromEntries(["name", "instance", "established", "vrf", "routing-table", "remote.address", "remote.as", "remote.afi", "local.address", "local.as", "local.afi"].map(key => [key, row[key] ?? null])) }); await checkpoint() + assert.equal(binding.instance, instance); assert.equal(binding.vrf, vrf); assert.equal(binding["routing-table"], vrf) + if (row.vrf !== undefined) assert.equal(row.vrf, vrf) + if (row["routing-table"] !== undefined) assert.equal(row["routing-table"], vrf) + assert.equal(Number(row["remote.as"]), serverId === 1 ? 64513 : 64512) + const families = row["remote.afi"]?.split(",") ?? [] + assert.ok(peer.afi.split(",").every(afi => families.includes(afi)), JSON.stringify(row)) + } + } + return own.map(row => ({ name: row.name, instance: row.instance, established: row.established ?? null, vrf: row.vrf ?? null, contextProof: "explicit-instance-and-connection", remoteAddress: row["remote.address"], remoteAs: row["remote.as"], remoteAfi: row["remote.afi"], prefixCount: row["prefix-count"] ?? null })) + } + if (attempt === 29) { checks.push({ serverId, bgpUnconfirmed: true, expectedEstablished: established, sessions: own }); await checkpoint(); assert.fail("BGP state was not confirmed in the isolated VRF") } + await delay(2000) + } + return [] + } + const observePrefixes = async (serverId: number, present: boolean, families = ["ipv4", "ipv6"]) => { + const peerId = serverId === 1 ? 2 : 1 + const expected = [{ resource: "/ip/route", family: "ipv4", prefix: `198.19.${subnet}.${peerId}/32` }, { resource: "/ipv6/route", family: "ipv6", prefix: `fd6d:6370:${hextet}:2::${peerId}/128` }].filter(target => families.includes(target.family)) + for (let attempt = 0; attempt < 30; attempt++) { + const observed = await Promise.all(expected.map(async target => { + const view = await invokeMcpTool(principal, "mm_bgp_prefixes_get", { serverId, family: target.family, table: vrf, limit: 200 }) as { items: { prefix: string; table: string; active: boolean | null }[]; incomplete: boolean; forwardingVerified: boolean } + assert.equal(view.incomplete, false); assert.equal(view.forwardingVerified, false) + const own = view.items.filter(row => row.table === vrf && row.prefix === target.prefix) + assert.ok(own.length <= 1, "Ambiguous BGP fixture prefix") + return { family: target.family, prefix: target.prefix, table: vrf, present: own.length === 1, active: own[0]?.active ?? null, source: "mm_bgp_prefixes_get" } + })) + if (present ? observed.every(row => row.present && row.active) : observed.every(row => !row.present)) { + checks.push({ serverId, phase: present ? "bgp-prefixes-received" : "bgp-prefixes-withdrawn", observed, forwardingTest: false, communityTest: false }); await checkpoint() + if (process.env.MMAPP_TEST_CHR_BGP_FORWARD === "true") for (const target of expected) { + checks.push(await acceptChrBgpPrefixPath(marker, principal, serverId, target.family === "ipv4" ? "ipv4" : "ipv6", target.prefix.split("/")[0], present)); await checkpoint() + } + return + } + if (attempt === 29) { checks.push({ serverId, phase: "bgp-prefixes-unconfirmed", expectedPresent: present, observed }); await checkpoint(); assert.fail("BGP prefix exchange/withdrawal not confirmed") } + await delay(2000) + } + } + checks.push({ bgpPreflight: true, baselineFingerprints: baseline.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })), prefixExchangeTested: false, prefixExchangeRequested: exportAcceptance }); await checkpoint() + try { + for (const serverId of [1, 2]) { + await execute("mm_bgp_instance_create_plan", { serverId, fields: { name: instance, as: serverId === 1 ? 64512 : 64513, "router-id": addresses[serverId - 1], vrf, "routing-table": vrf, comment: marker } }) + for (const peer of peers) await execute("mm_bgp_connection_create_plan", { serverId, fields: { name: peer.name, instance, "remote.address": peer.addresses[serverId === 1 ? 1 : 0], "local.address": peer.addresses[serverId - 1], "remote.as": serverId === 1 ? 64513 : 64512, "local.role": "ebgp", connect: true, listen: true, afi: peer.afi, vrf, "routing-table": vrf, comment: marker } }) + const row = (await readRosSnapshot(serverId, "bgp_instance")).rows.find(row => row.name === instance)! + await assert.rejects(invokeMcpTool(principal, "mm_bgp_instance_delete_plan", { serverId, id: row[".id"] }), /используется подключением/) + } + for (const serverId of [1, 2]) { checks.push({ serverId, phase: "bgp-established", sessions: await observe(serverId, true) }); await checkpoint() } + if (exportAcceptance) { + const configureExport = async (serverId: number, mode: "connected" | "disabled") => { + for (const peer of peers) { + const row = (await readRosSnapshot(serverId, "bgp_connection")).rows.find(row => row.name === peer.name && row.comment === marker)! + const operationId = await execute("mm_bgp_export_plan", { serverId, id: row[".id"], mode }) + checks.push({ serverId, phase: "bgp-export-configured", family: peer.family, mode, operationId, verificationScope: "configuration" }); await checkpoint() + } + } + for (const serverId of [1, 2]) await configureExport(serverId, "connected") + for (const serverId of [1, 2]) await observePrefixes(serverId, true) + if (bindingAcceptance && process.env.MMAPP_TEST_CHR_BGP_BINDING_FAULTS !== "false") { + for (const serverId of [1, 2]) await execute("mm_filter_rules_replace_plan", { serverId, rules: [{ community: "64512:999", action: "blackhole", gateway: "", gatewayTunnelId: "", description: "Owned unmatched Community; implicit reject is deliberate" }] }) + const restart = async (serverId: number) => { + const row = (await readRosSnapshot(serverId, "bgp_connection")).rows.find(row => row.name === connection && row.comment === marker)! + await execute("mm_bgp_connection_update_plan", { serverId, id: row[".id"], fields: { disabled: true } }) + await execute("mm_bgp_connection_update_plan", { serverId, id: row[".id"], fields: { disabled: false } }) + await observe(1, true, "ipv4"); await observe(2, true, "ipv4") + } + for (const [serverId, direction] of [[1, "input"], [2, "output"]] as const) { + const row = (await readRosSnapshot(serverId, "bgp_connection")).rows.find(row => row.name === connection && row.comment === marker)! + await execute("mm_bgp_filter_binding_plan", { serverId, id: row[".id"], direction, chain: "bgp-in" }) + await restart(serverId) + // Input rejects can remain in RIB as filtered/inactive. A missing row + // is not required; the separately verified packet path must fail. + const view = await invokeMcpTool(principal, "mm_bgp_prefixes_get", { serverId: 1, family: "ipv4", table: vrf, limit: 200 }) as { items: { prefix: string; active: boolean | null }[] } + checks.push({ phase: "bgp-binding-rejected-rib", serverId, direction, prefixes: view.items, communityTaggingVerified: false }); await checkpoint() + checks.push(await acceptChrBgpPrefixPath(marker, principal, 1, "ipv4", `198.19.${subnet}.2`, false)); await checkpoint() + await observePrefixes(1, true, ["ipv6"]) + await execute("mm_bgp_filter_binding_plan", { serverId, id: row[".id"], direction, chain: "" }) + await restart(serverId); await observePrefixes(1, true) + checks.push({ phase: "bgp-filter-binding-packet-accepted", serverId, direction, independentAfiPreserved: true, explicitApprovedPeerRestart: true, communityTaggingVerified: false }); await checkpoint() + } + } + if (bindingAcceptance && process.env.MMAPP_TEST_CHR_BGP_BINDING_FAULTS === "false") { + assert.equal(communityAcceptance, true) + checks.push({ phase: "binding-fault-suite-not-repeated", priorAcceptance: "mcp-chr-bgp-filter-binding-acceptance.json", reason: "Focused Community diagnostic; binding/peer restart/read-back still exercised by Community helper" }); await checkpoint() + } + if (communityAcceptance) await acceptChrBgpCommunity(marker, principal, execute, checks, checkpoint) + await configureExport(2, "disabled") + await observePrefixes(1, false) + await configureExport(2, "connected") + await observePrefixes(1, true) + checks.push({ prefixExchangeTested: true, family: ["ipv4", "ipv6"], explicitExportWithdrawalAndReannounce: true, communityTest: false, forwardingTest: false }); await checkpoint() + } + const peer = (await readRosSnapshot(2, "bgp_connection")).rows.find(row => row.name === connection && row.comment === marker)! + const operationId = await execute("mm_bgp_connection_update_plan", { serverId: 2, id: peer[".id"], fields: { disabled: true } }) + checks.push({ phase: "bgp-administratively-disabled", sessions: await observe(1, false, exportAcceptance ? "ipv4" : undefined) }); await checkpoint() + if (exportAcceptance) { + await observePrefixes(1, false, ["ipv4"]) + await observe(1, true, "ipv6"); await observePrefixes(1, true, ["ipv6"]) + checks.push({ independentAddressFamilyStopVerified: true, independentPhysicalPathVerified: false }); await checkpoint() + } + await execute("mm_operation_recovery_plan", { operationId }) + for (const serverId of [1, 2]) { checks.push({ serverId, phase: "bgp-recovered", sessions: await observe(serverId, true) }); await checkpoint() } + if (exportAcceptance) for (const serverId of [1, 2]) await observePrefixes(serverId, true) + } finally { + for (const serverId of [1, 2]) for (const resource of ["bgp_connection", "bgp_instance"]) { + const own = (await readRosSnapshot(serverId, resource)).rows.filter(row => row.comment === marker && (resource === "bgp_instance" ? row.name === instance : peers.some(peer => row.name === peer.name))) + for (const row of own) { + if (resource === "bgp_instance") assert.equal(row.vrf, vrf) + else assert.equal(row.instance, instance) + await execute(`mm_${resource}_delete_plan`, { serverId, id: row[".id"] }) + } + } + for (const serverId of [1, 2]) await observe(serverId, false) + if (communityAcceptance) await cleanupChrCommunityPolicy(marker, execute) + if (bindingAcceptance) for (const serverId of [1, 2]) await execute("mm_filter_rules_replace_plan", { serverId, rules: [], requireUnbound: true }) + if (exportAcceptance) for (const serverId of [1, 2]) await observePrefixes(serverId, false) + if (process.env.MMAPP_TEST_CHR_COMMUNITY_GATEWAY_RESOLVER === "true") await cleanupChrCommunityGatewayResolvers(marker, execute) + for (const snapshot of baseline) assert.equal((await readRosSnapshot(snapshot.serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "BGP configuration/routes baseline changed after cleanup") + checks.push({ bgpBaselineRestored: true, activeFixtureSessionsRemaining: 0, historicalSessionCacheCleared: false }); await checkpoint() + console.log("✓ BGP cleanup: owned configuration removed and baseline restored") + } +} diff --git a/backend/src/modules/mcp/chr-bgp-community-acceptance.ts b/backend/src/modules/mcp/chr-bgp-community-acceptance.ts new file mode 100644 index 00000000..5575dd81 --- /dev/null +++ b/backend/src/modules/mcp/chr-bgp-community-acceptance.ts @@ -0,0 +1,178 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import { invokeMcpTool } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { McpPrincipal } from "./store.js" +import { acceptChrBgpPrefixPath } from "./chr-bgp-forward-acceptance.js" +import { bgpCommunityPolicyChain, bgpCommunityPolicyComment } from "../../services/bgp-community-policy-model.js" + +type Execute = (tool: string, args: Record) => Promise +async function diagnosticRoutes(serverId: number, prefix: string, table: string) { + const { client } = await routerClient(serverId) + const payload = await client.get("/routing/route?dst-address=" + encodeURIComponent(prefix) + "&routing-table=" + encodeURIComponent(table)) + const fields = new Set([".id", "afi", "contribution", "dst-address", "routing-table", "gateway", "immediate-gw", "active", "filtered", "unreachable", "blackhole", "distance", "scope", "target-scope", "bgp", "bgp.communities", "bgp-communities", "communities"]) + assert.ok(Array.isArray(payload) && payload.length <= 20) + return payload.map(row => { + assert.ok(row && typeof row === "object") + return Object.fromEntries(Object.entries(row).filter(([key, value]) => fields.has(key) && ["string", "boolean"].includes(typeof value)).map(([key, value]) => [key, typeof value === "string" ? value.slice(0, 300) : value])) + }) +} +export async function cleanupChrCommunityGatewayResolvers(marker: string, execute: Execute, recordedRoutes?: readonly Record[]) { + assert.match(marker, /^mcp-test-[a-f0-9]{8}$/) + const subnet = Number.parseInt(marker.slice(-2), 16), hex = Number.parseInt(marker.slice(-4), 16).toString(16) + for (const family of ["ipv4", "ipv6"]) { + const resource = family + "_route", prefix = family === "ipv4" ? `198.18.${subnet}.2/32` : `fd6d:6370:${hex}:1::2/128` + const own = (await readRosSnapshot(1, resource)).rows.filter(row => row.comment === marker + "-gw-lookup") + assert.ok(own.length <= 1) + for (const row of own) { + assert.ok(row["dst-address"] === prefix && row["routing-table"] === "main", "Resolver cleanup refuses a changed destination") + if (row.gateway !== marker + "-gre@" + marker + "-vrf") { + // A removed VRF is printed as @*nativeID. Accept only an explicitly + // recorded route identity, unchanged config, an absent VRF object, and + // the original exact lab GRE. No other changed gateway is accepted. + const dangling = typeof row.gateway === "string" ? row.gateway.match(new RegExp("^" + marker + "-gre@(\\*[A-Za-z0-9]+)$"))?.[1] : undefined + const prior = recordedRoutes?.find(saved => saved[".id"] === row[".id"] && saved["dst-address"] === prefix && saved["routing-table"] === "main" && saved.gateway === marker + "-gre@" + marker + "-vrf") + assert.ok(dangling && prior && ["scope", "target-scope", "distance"].every(key => row[key] === prior[key]), "Resolver cleanup refuses an unrecorded or changed gateway") + const vrfs = (await readRosSnapshot(1, "vrf")).rows + assert.ok(!vrfs.some(vrf => vrf[".id"] === dangling || vrf.name === marker + "-vrf"), "Resolver cleanup refuses a live VRF reference") + const gre = (await readRosSnapshot(1, "gre")).rows.filter(iface => iface.name === marker + "-gre" && iface.comment === marker) + assert.ok(gre.length === 1 && gre[0]["local-address"] === "194.226.121.150" && gre[0]["remote-address"] === "104.165.244.7", "Resolver cleanup refuses a changed GRE") + console.log("✓ Recorded owned resolver: dangling deleted-VRF identity confirmed", { id: row[".id"], prefix, danglingVrfId: dangling }) + } + } + for (const row of own) await execute(`mm_${resource}_delete_plan`, { serverId: 1, id: row[".id"] }) + } +} +export const chrCommunityPolicyId = (marker: string) => marker + "-community" +export async function cleanupChrCommunityPolicy(marker: string, execute: Execute) { + const policyId = chrCommunityPolicyId(marker), { client } = await routerClient(2) + const rows = await client.get[]>("/routing/filter/rule") + const own = rows.filter(row => row.chain === bgpCommunityPolicyChain(policyId) || row.comment === bgpCommunityPolicyComment(policyId)) + assert.ok(own.length <= 1 && own.every(row => row.chain === bgpCommunityPolicyChain(policyId) && row.comment === bgpCommunityPolicyComment(policyId)), "Cleanup refuses foreign/ambiguous output policy") + if (own.length) await execute("mm_bgp_community_policy_plan", { serverId: 2, policyId, rules: [], action: "delete" }) +} +export async function acceptChrBgpCommunity(marker: string, principal: McpPrincipal, execute: Execute, checks: Record[], checkpoint: () => Promise) { + const subnet = Number.parseInt(marker.slice(-2), 16), hex = Number.parseInt(marker.slice(-4), 16).toString(16), vrf = marker + "-vrf", policyId = chrCommunityPolicyId(marker) + const targets = { ipv4: `198.19.${subnet}.2`, ipv6: `fd6d:6370:${hex}:2::2` }, chain = bgpCommunityPolicyChain(policyId) + const peer = async (serverId: number, family: "ipv4" | "ipv6") => { + const rows = (await readRosSnapshot(serverId, "bgp_connection")).rows.filter(row => row.name === marker + "-peer" + (family === "ipv6" ? "-v6" : "") && row.comment === marker && row.vrf === vrf) + assert.equal(rows.length, 1); return String(rows[0][".id"]) + } + const restart = async (family: "ipv4" | "ipv6") => { + const id = await peer(2, family) + await execute("mm_bgp_connection_update_plan", { serverId: 2, id, fields: { disabled: true } }) + const prefix = targets[family] + (family === "ipv4" ? "/32" : "/128") + let withdrawn = false + for (let n = 0; n < 30; n++) { + const view = await invokeMcpTool(principal, "mm_bgp_prefixes_get", { serverId: 1, family, table: vrf, limit: 200 }) as { items: { id: string; prefix: string; active: boolean | null }[] } + const rows = view.items.filter(row => row.prefix === prefix) + if (!rows.length || rows.every(row => row.active === false)) { withdrawn = true; break } + await delay(2000) + } + assert.equal(withdrawn, true, "Old RIB must be absent/inactive before the fresh peer generation") + checks.push({ phase: "community-peer-down-prefix-fence", family, prefix, oldActiveRouteRemoved: true }); await checkpoint() + await execute("mm_bgp_connection_update_plan", { serverId: 2, id, fields: { disabled: false } }) + } + const observe = async (family: "ipv4" | "ipv6", mode: "forward" | "missing" | "blackhole", phase: string) => { + const prefix = targets[family] + (family === "ipv4" ? "/32" : "/128") + let lastObserved: unknown = null, lastBlackholeObservation: unknown = null + for (let n = 0; n < 30; n++) { + const view = await invokeMcpTool(principal, "mm_bgp_prefixes_get", { serverId: 1, family, table: vrf, limit: 200 }) as { items: { id: string; prefix: string; active: boolean | null }[] } + const rows = view.items.filter(row => row.prefix === prefix); assert.ok(rows.length <= 1) + lastObserved = rows + if (mode === "missing" ? !rows.length || rows[0].active === false : rows.length === 1 && rows[0].active === true) { + if (mode === "blackhole") { + const nativeRoutes = await diagnosticRoutes(1, prefix, vrf) + lastBlackholeObservation = nativeRoutes + const route = nativeRoutes[0] + const communities = typeof route?.["bgp.communities"] === "string" ? String(route["bgp.communities"]).split(",").map(value => value.trim()) : [] + const expectedTags = family === "ipv4" ? ["64512:998", "64512:999"] : ["64512:1000", "64512:1001"] + const bothTags = communities.length === 2 && expectedTags.every(tag => communities.includes(tag)) + if (!bothTags || nativeRoutes.length !== 1 || route[".id"] !== rows[0].id || route["dst-address"] !== prefix || route["routing-table"] !== vrf || ![true, "true", "yes"].includes(route.blackhole as string | boolean) || ![true, "true", "yes"].includes(route.active as string | boolean)) { + await delay(2000); continue + } + checks.push({ phase: "community-blackhole-observed", family, prefix, nativeBlackhole: true, appendBothTagsDirectlyObserved: true, source: "/routing/route", nativeRoutes }); await checkpoint() + } + checks.push({ phase, family, mode, prefix, observedActive: rows[0]?.active ?? null, packetProof: await acceptChrBgpPrefixPath(marker, principal, 1, family, targets[family], mode === "forward"), freshApprovedPeerRestart: true }); await checkpoint(); return + } + await delay(2000) + } + // Preserve scoped read-only evidence before finally removes the fixture. + // /routing/route also lists filtered routes, unlike the selected FIB view. + const diagnostics: unknown[] = [] + for (const serverId of [1, 2]) { + try { + const { client } = await routerClient(serverId) + diagnostics.push({ serverId, routes: await diagnosticRoutes(serverId, prefix, vrf) }) + const sessions = await client.get[]>("/routing/bgp/session") + assert.ok(Array.isArray(sessions) && sessions.length <= 100) + diagnostics.push({ serverId, sessions: sessions.filter(row => typeof row.name === "string" && row.name.startsWith(marker + "-peer")).map(row => Object.fromEntries(["name", "established", "state", "uptime", "afi", "remote.address", "local.address"].flatMap(key => ["string", "boolean"].includes(typeof row[key]) ? [[key, String(row[key]).slice(0, 100)]] : []))) }) + } catch { diagnostics.push({ serverId, diagnosticUnavailable: true }) } + } + checks.push({ phase: "community-route-unconfirmed-diagnostic", failedPhase: phase, family, mode, prefix, lastObserved, lastBlackholeObservation, diagnostics }); await checkpoint() + throw new Error("Community route state not confirmed: " + phase + "/" + family) + } + const literalGatewayDiagnostic = process.env.MMAPP_TEST_CHR_COMMUNITY_LITERAL_GATEWAY === "true" + const resolverDiagnostic = process.env.MMAPP_TEST_CHR_COMMUNITY_GATEWAY_RESOLVER === "true" + assert.ok(!resolverDiagnostic || !literalGatewayDiagnostic) + checks.push({ phase: "community-gateway-mode", mode: literalGatewayDiagnostic ? "literal-address-only-diagnostic" : "explicit-gre-interface", vrfIsolationPreserved: true }); await checkpoint() + if (resolverDiagnostic) { + const targetScope = process.env.MMAPP_TEST_CHR_COMMUNITY_RESOLVER_TARGET_SCOPE ?? "10" + assert.ok(["5", "10"].includes(targetScope)) + for (const family of ["ipv4", "ipv6"]) { + const resource = family + "_route", prefix = family === "ipv4" ? `198.18.${subnet}.2/32` : `fd6d:6370:${hex}:1::2/128` + assert.ok(!(await readRosSnapshot(1, resource)).rows.some(row => row["routing-table"] === "main" && row["dst-address"] === prefix), "Resolver fixture refuses an existing exact main route") + await execute(`mm_${resource}_create_plan`, { serverId: 1, fields: { "dst-address": prefix, gateway: marker + "-gre@" + vrf, "routing-table": "main", scope: 10, "target-scope": Number(targetScope), comment: marker + "-gw-lookup" } }) + const nativeRoutes = await diagnosticRoutes(1, prefix, "main") + checks.push({ phase: "community-gateway-resolver-created", serverId: 1, family, prefix, lookupTable: "main", destinationVrf: vrf, scope: 10, targetScope: Number(targetScope), nativeRoutes, ownedExactHostRoute: true, separateHumanPlan: true }); await checkpoint() + assert.ok(nativeRoutes.length === 1 && [true, "true"].includes(nativeRoutes[0].active as string | boolean) && typeof nativeRoutes[0]["immediate-gw"] === "string" && nativeRoutes[0]["immediate-gw"], "Resolver requires a positively active route and resolved gateway") + } + } + const inputRules = [ + { community: "64512:999", action: "route", gateway: `198.18.${subnet}.2`, gatewayTunnelId: literalGatewayDiagnostic ? "" : marker + "-gre", description: "Owned IPv4 Community match" }, + { community: "64512:1000", action: "route", gateway: `fd6d:6370:${hex}:1::2`, gatewayTunnelId: literalGatewayDiagnostic ? "" : marker + "-gre", description: "Owned IPv6 Community match" }, + ] + const outputRules = (v4 = "64512:999", v6 = "64512:1000") => [ + { prefix: targets.ipv4 + "/32", communityMode: "set", communities: [v4] }, { prefix: targets.ipv6 + "/128", communityMode: "set", communities: [v6] }, + ] + const output = (rules: unknown[]) => execute("mm_bgp_community_policy_plan", { serverId: 2, policyId, rules }) + await execute("mm_filter_rules_replace_plan", { serverId: 1, rules: inputRules }) + await output(outputRules()) + for (const family of ["ipv4", "ipv6"] as const) { + await execute("mm_bgp_filter_binding_plan", { serverId: 2, id: await peer(2, family), direction: "output", chain }) + await execute("mm_bgp_filter_binding_plan", { serverId: 1, id: await peer(1, family), direction: "input", chain: "bgp-in" }) + } + const probe = await invokeMcpTool(principal, "mm_bgp_community_policy_plan", { serverId: 2, policyId, rules: outputRules() }) as { changes: { before: { nativeReferenceQueryMatches: number } } } + assert.ok(probe.changes.before.nativeReferenceQueryMatches >= 2, "Native OR reference query must find both bound AFI peers") + await assert.rejects(invokeMcpTool(principal, "mm_bgp_community_policy_plan", { serverId: 2, policyId, rules: [], action: "delete" })) + for (const family of ["ipv4", "ipv6"] as const) { await restart(family); await observe(family, "forward", "community-set-matched") } + for (const family of ["ipv4", "ipv6"] as const) { + await output(family === "ipv4" ? outputRules("64512:998") : outputRules(undefined, "64512:1001")) + await restart(family); await observe(family, "missing", "community-mismatch-rejected") + await observe(family === "ipv4" ? "ipv6" : "ipv4", "forward", "community-other-afi-preserved") + await output(outputRules()); await restart(family); await observe(family, "forward", "community-match-restored") + } + for (const family of ["ipv4", "ipv6"] as const) { + const otherFamily = family === "ipv4" ? "ipv6" : "ipv4", prefix = targets[family] + (family === "ipv4" ? "/32" : "/128") + const oldTag = family === "ipv4" ? "64512:998" : "64512:1001", newTag = family === "ipv4" ? "64512:999" : "64512:1000" + const otherRule = outputRules()[family === "ipv4" ? 1 : 0] + const pipeline = [ + { prefix, communityMode: "set", communities: [oldTag], action: "continue" }, + { prefix, communityMode: "append", communities: [newTag] }, otherRule, + ] + await output(pipeline); await restart(family); await observe(family, "forward", "community-append-new-tag-matched") + await execute("mm_filter_rules_replace_plan", { serverId: 1, rules: [{ community: oldTag, action: "blackhole", gateway: "", gatewayTunnelId: "", description: "Prove append preserved previous tag: " + family }, ...inputRules] }) + await restart(family); await observe(family, "blackhole", "community-append-old-tag-preserved") + await observe(otherFamily, "forward", "community-append-other-afi-preserved") + await output([{ ...pipeline[0] }, { ...pipeline[1], action: "continue" }, { prefix, communityMode: "clear" }, otherRule]) + await restart(family); await observe(family, "missing", "community-clear-removed-both-tags") + await output(outputRules()); await restart(family); await observe(family, "forward", "community-after-clear-restored") + } + for (const family of ["ipv4", "ipv6"] as const) { + await execute("mm_bgp_filter_binding_plan", { serverId: 1, id: await peer(1, family), direction: "input", chain: "" }) + await execute("mm_bgp_filter_binding_plan", { serverId: 2, id: await peer(2, family), direction: "output", chain: "" }) + } + await cleanupChrCommunityPolicy(marker, execute) + checks.push({ phase: "community-export-policy-accepted", ipv4AndIpv6Match: true, afiMismatchIsolation: true, appendPreservedOldAndAddedNew: true, clearRemovedBoth: true, blackholeObserved: true, + appendClearBlackholeFamilies: ["ipv4", "ipv6"], attributesDirectlyObserved: true, propagationEvidence: "native append tag pairs plus fresh sessions, controlled Community predicates and packet/RIB changes", independentPhysicalPaths: false, endUserTrafficVerified: false }); await checkpoint() +} diff --git a/backend/src/modules/mcp/chr-bgp-forward-acceptance.ts b/backend/src/modules/mcp/chr-bgp-forward-acceptance.ts new file mode 100644 index 00000000..39bdb350 --- /dev/null +++ b/backend/src/modules/mcp/chr-bgp-forward-acceptance.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict" +import { invokeMcpTool } from "./runtime.js" +import { readRosSnapshot } from "./resources.js" +import type { McpPrincipal } from "./store.js" + +/** Probe a remote owned loopback, whose only forward route is learned via BGP. + * The reply uses the connected GRE subnet; this is a two-node control probe. */ +export async function acceptChrBgpPrefixPath(marker: string, principal: McpPrincipal, serverId: number, family: "ipv4" | "ipv6", target: string, present: boolean) { + const peerId = serverId === 1 ? 2 : 1, gre = marker + "-gre", loop = marker + "-bgp-loop", vrf = marker + "-vrf" + const subnet = Number.parseInt(marker.slice(-2), 16), hextet = Number.parseInt(marker.slice(-4), 16).toString(16) + const addresses = family === "ipv4" ? [`198.18.${subnet}.1`, `198.18.${subnet}.2`] : [`fd6d:6370:${hextet}:1::1`, `fd6d:6370:${hextet}:1::2`] + const source = (await readRosSnapshot(serverId, "gre")).rows.find(row => row.name === gre && row.comment === marker) + assert.ok(source) + const profile = { name: marker, serverId, community: "65000:65000", family, table: vrf, probeVrf: vrf, candidates: [{ id: "bgp-prefix", jumpHostId: peerId, exitNodeId: peerId, wanInterface: gre, downstreamInterface: gre, + gateway: addresses[peerId - 1], gatewayRef: { resource: "gre", id: source[".id"] }, controlProbe: { targetInterface: loop, targetAddress: target, sourceAddress: addresses[serverId - 1], expectedHops: [{ serverId: peerId, interface: loop, address: target }] } }] } + const proof = await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "bgp-prefix", proofPurpose: "control-path" }) as { status: string; packetCounts?: { sent: number; received: number } } + assert.equal(proof.status, present ? "passed" : "failed", JSON.stringify(proof)) + assert.equal(proof.packetCounts?.sent, 3) + assert.equal(proof.packetCounts?.received, present ? 3 : 0) + return { serverId, family, phase: "bgp-prefix-path", expectedPresent: present, proof, proofScope: "two-node-BGP-forward-with-connected-GRE-return", independentPhysicalPaths: false, endUserTrafficVerified: false } +} diff --git a/backend/src/modules/mcp/chr-ecmp-acceptance.ts b/backend/src/modules/mcp/chr-ecmp-acceptance.ts new file mode 100644 index 00000000..9e8b58d4 --- /dev/null +++ b/backend/src/modules/mcp/chr-ecmp-acceptance.ts @@ -0,0 +1,67 @@ +import assert from "node:assert/strict" +import { invokeMcpTool } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { McpPrincipal } from "./store.js" +type Execute = (tool: string, args: Record) => Promise + +export async function acceptChrEcmp(marker: string, principal: McpPrincipal, execute: Execute, checks: Record[], checkpoint: () => Promise) { + const gre = marker + "-gre", vrf = marker + "-vrf", bridge = marker + "-lo" + const subnet = Number.parseInt(marker.slice(-2), 16), hextet = Number.parseInt(marker.slice(-4), 16).toString(16) + const markerComment = "MikrotikManager: " + marker + const resources = ["bridge", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route"] + const baseline = [] + for (const serverId of [1, 2]) for (const resource of resources) baseline.push(await readRosSnapshot(serverId, resource)) + assert.ok(!baseline.flatMap(snapshot => snapshot.rows).some(row => row.name === bridge || row.comment === markerComment)) + const targets = { ipv4: `198.19.${subnet}.2`, ipv6: `fd6d:6370:${hextet}:2::2` } + const alt = { ipv4: [`198.18.${subnet}.5`, `198.18.${subnet}.6`], ipv6: [`fd6d:6370:${hextet}:3::1`, `fd6d:6370:${hextet}:3::2`] } + const vrfRow = (await readRosSnapshot(2, "vrf")).rows.find(row => row.name === vrf && row.comment === marker)! + const probe = async (family: "ipv4" | "ipv6", gatewayId: string) => { + const sourceAddress = family === "ipv4" ? `198.18.${subnet}.1` : `fd6d:6370:${hextet}:1::1` + const profile = { name: marker, serverId: 1, community: "65000:65000", family, table: vrf, probeVrf: vrf, candidates: [{ id: "ecmp-lab", jumpHostId: 2, exitNodeId: 2, wanInterface: gre, downstreamInterface: gre, gateway: targets[family], gatewayRef: { resource: family === "ipv4" ? "ipv4_route" : "ipv6_route", id: gatewayId }, controlProbe: { targetInterface: bridge, targetAddress: targets[family], sourceAddress, expectedHops: [{ serverId: 2, interface: bridge, address: targets[family] }] } }] } + return await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "ecmp-lab", proofPurpose: "control-path" }) as { status: string } + } + checks.push({ ecmpPreflight: true, baselineFingerprints: baseline.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })), independentPhysicalPaths: false }); await checkpoint() + try { + await execute("mm_bridge_create_plan", { serverId: 2, fields: { name: bridge, comment: markerComment, "protocol-mode": "none", disabled: false } }) + await execute("mm_vrf_update_plan", { serverId: 2, id: vrfRow[".id"], fields: { interfaces: gre + "," + bridge } }) + await execute("mm_ipv4_address_create_plan", { serverId: 2, fields: { interface: bridge, address: targets.ipv4 + "/32", comment: markerComment } }) + await execute("mm_ipv6_address_create_plan", { serverId: 2, fields: { interface: bridge, address: targets.ipv6 + "/128", advertise: false, comment: markerComment } }) + for (const serverId of [1, 2]) { + await execute("mm_ipv4_address_create_plan", { serverId, fields: { interface: gre, address: alt.ipv4[serverId - 1] + "/30", comment: markerComment } }) + await execute("mm_ipv6_address_create_plan", { serverId, fields: { interface: gre, address: alt.ipv6[serverId - 1] + "/64", advertise: false, comment: markerComment } }) + } + for (const family of ["ipv4", "ipv6"] as const) { + const resource = family === "ipv4" ? "ipv4_route" : "ipv6_route", prefix = targets[family] + (family === "ipv4" ? "/32" : "/128") + const gateways = [family === "ipv4" ? `198.18.${subnet}.2` : `fd6d:6370:${hextet}:1::2`, alt[family][1]] + for (const [index, gateway] of gateways.entries()) await execute(`mm_${resource}_create_plan`, { serverId: 1, fields: { "dst-address": prefix, "routing-table": vrf, gateway: gateway + "@" + vrf, distance: (index + 1) * 10, comment: markerComment } }) + const { client } = await routerClient(1), path = family === "ipv4" ? "/ip/route" : "/ipv6/route" + const own = (await client.get[]>(path)).filter(row => row.comment === markerComment && row["dst-address"] === prefix && row["routing-table"] === vrf) + assert.equal(own.length, 2); assert.ok(own.some(row => row.active !== "true"), "Fixture must include a valid inactive higher-distance route") + assert.ok(own.every(row => !!row["immediate-gw"])) + const operationId = await execute("mm_ecmp_plan", { serverId: 1, family, routeIds: own.map(row => row[".id"]), distance: 10 }) + const equal = (await client.get[]>(path)).filter(row => own.some(item => item[".id"] === row[".id"])) + assert.ok(equal.every(row => row.active === "true" && row.distance === "10"), JSON.stringify(equal)) + const proof = await probe(family, own[0][".id"]) + assert.equal(proof.status, "passed", JSON.stringify(proof)) + await execute("mm_operation_recovery_plan", { operationId }) + const restored = (await readRosSnapshot(1, resource)).rows.filter(row => own.some(item => item[".id"] === row[".id"])) + assert.deepEqual(restored.map(row => Number(row.distance)).sort((a, b) => a - b), [10, 20]) + const inverseProof = await probe(family, own[0][".id"]) + assert.equal(inverseProof.status, "passed", JSON.stringify(inverseProof)) + checks.push({ family, phase: "ecmp", inactiveCandidateAccepted: true, equalActiveRoutes: true, proof, inverseDistanceRestored: true, inverseProof, independentPhysicalPaths: false }); await checkpoint() + console.log(`✓ ${family}: unequal distances → ECMP → approved inverse; routed loopback probes passed`) + for (const row of own) await execute(`mm_${resource}_delete_plan`, { serverId: 1, id: row[".id"] }) + } + } finally { + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of (await readRosSnapshot(1, resource)).rows.filter(row => row.comment === markerComment && row["routing-table"] === vrf && [targets.ipv4 + "/32", targets.ipv6 + "/128"].includes(String(row["dst-address"])))) await execute(`mm_${resource}_delete_plan`, { serverId: 1, id: row[".id"] }) + for (const serverId of [1, 2]) for (const resource of ["ipv4_address", "ipv6_address"]) for (const row of (await readRosSnapshot(serverId, resource)).rows.filter(row => row.comment === markerComment && [gre, bridge].includes(String(row.interface)))) await execute(`mm_${resource}_delete_plan`, { serverId, id: row[".id"] }) + const current = (await readRosSnapshot(2, "vrf")).rows.find(row => row[".id"] === vrfRow[".id"]) + if (current && current.interfaces !== gre) { + assert.equal(current.interfaces, gre + "," + bridge) + await execute("mm_vrf_update_plan", { serverId: 2, id: vrfRow[".id"], fields: { interfaces: gre } }) + } + for (const row of (await readRosSnapshot(2, "bridge")).rows.filter(row => row.name === bridge && row.comment === markerComment)) await execute("mm_bridge_delete_plan", { serverId: 2, id: row[".id"] }) + for (const snapshot of baseline) assert.equal((await readRosSnapshot(snapshot.serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "ECMP fixture baseline changed") + checks.push({ ecmpBaselineRestored: true }); await checkpoint() + } +} diff --git a/backend/src/modules/mcp/chr-filter-write.test.ts b/backend/src/modules/mcp/chr-filter-write.test.ts new file mode 100644 index 00000000..a5051d78 --- /dev/null +++ b/backend/src/modules/mcp/chr-filter-write.test.ts @@ -0,0 +1,168 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile, writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { AuthUser } from "../../lib/permissions.js" +import { MikrotikClient, MikrotikError } from "../../services/mikrotik.js" +import { compileBgpCommunityPolicy, bgpCommunityPolicyChain, bgpCommunityPolicyComment } from "../../services/bgp-community-policy-model.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === 2 && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const cleanupMarker = process.env.MMAPP_TEST_CHR_FILTER_CLEANUP_MARKER +const ipv6Acceptance = process.env.MMAPP_TEST_CHR_FILTER_IPV6 === "true" +const communityPolicyAcceptance = process.env.MMAPP_TEST_CHR_COMMUNITY_POLICY === "true" +const vrfAcceptance = process.env.MMAPP_TEST_CHR_FILTER_VRF === "true" +assert.ok(!vrfAcceptance || ipv6Acceptance && !cleanupMarker && !communityPolicyAcceptance) +if (cleanupMarker) assert.match(cleanupMarker, /^mcp-test-[a-f0-9]{8}$/) +const marker = cleanupMarker ?? "mcp-test-" + randomUUID().slice(0, 8), gre = marker + (cleanupMarker ? '-g } "$' : ipv6Acceptance ? "-g-test" : "-g test") +const actor: AuthUser = { id: "chr-filter-write-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin", "mm:filters:admin", "mm:bgp:admin"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const report = { startedAt: new Date().toISOString(), marker, complete: false, fixtures: "require-unbound-routing-context-and-disabled-owned-GRE", communityPropagationVerified: false, forwardingVerified: false, checks, finishedAt: "" } +const save = () => writeFile(new URL(cleanupMarker ? "../../../../docs/mcp-chr-filter-write-recovery.json" : vrfAcceptance ? "../../../../docs/mcp-chr-filter-vrf-acceptance.json" : communityPolicyAcceptance ? "../../../../docs/mcp-chr-community-policy-acceptance.json" : ipv6Acceptance ? "../../../../docs/mcp-chr-filter-ipv6-acceptance.json" : "../../../../docs/mcp-chr-filter-write-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +const originalPost = MikrotikClient.prototype.post, originalPatch = MikrotikClient.prototype.patch +const captureParser = async (error: unknown, path: string) => { + if (!(error instanceof MikrotikError) || error.statusCode !== 400 || !path.startsWith("/routing/filter/rule")) return + let detail = "" + try { const body = JSON.parse(error.body); if (typeof body.detail === "string") detail = body.detail } catch { /* Never save response headers or raw payloads. */ } + detail = detail.replaceAll(process.env.MMAPP_TEST_CHR_PASSWORD!, "[redacted]").replace(/(?:password|secret|token|private-key)\s*[=:]\s*[^\s;,]+/gi, "[redacted]").slice(0, 500) + checks.push({ phase: "owned-native-parser-error", path, nativeStatus: 400, detail }); await save() + console.log({ ownedFilterParserDetail: detail }) +} +MikrotikClient.prototype.post = async function(path: string, body: Record, timeoutMs?: number, signal?: AbortSignal): Promise { + try { return await originalPost.call(this, path, body, timeoutMs, signal) as T } catch (error) { await captureParser(error, path); throw error } +} +MikrotikClient.prototype.patch = async function(path: string, body: Record, timeoutMs?: number,beforeDispatch?:()=>Promise): Promise { + try { return await originalPatch.call(this, path, body, timeoutMs,beforeDispatch) as T } catch (error) { await captureParser(error, path); throw error } +} +try { + await save(); await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, marker, ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:filters:write", "mm:network:read", "mm:network:write", "mm:bgp:read", ...(communityPolicyAcceptance ? ["mm:bgp:write"] : [])], limits: { callsPerMinute: 120, mutationsPerHour: 100 }, scope: { serverIds: [1, 2], operationIds: ["ros.gre.create", "ros.gre.delete", "filters.rules.replace", "filters.revision.restore", "routing.filters.catalog.read", ...(vrfAcceptance ? ["ros.vrf.create", "ros.vrf.delete"] : []), ...(communityPolicyAcceptance ? ["routing.bgp.community.policy"] : [])], allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const execute = async (tool: string, input: Record) => { + const plan = await invokeMcpTool(principal, tool, input) as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval"); await assert.rejects(applyPlan(principal, plan.planId, randomUUID())) + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + for (let attempt = 0; attempt < 240; attempt++) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { + checks.push({ tool, operationId: operation.id, state: row.state, errorCode: row.error_code }); await save() + if (row.state !== "applied" && tool === "mm_bgp_community_policy_plan" && input.action !== "delete") { + const compiled = compileBgpCommunityPolicy({ policyId: input.policyId, rules: input.rules, defaultAction: input.defaultAction }), { client } = await routerClient(Number(input.serverId)) + const rows = (await client.get[]>("/routing/filter/rule")).filter(value => value.chain === compiled.chain && value.comment === compiled.comment) + checks.push({ phase: "policy-failure-observed", serverId: input.serverId, nativeRows: rows.map(value => { + const actual = value.rule ?? "", firstDifference = Array.from({ length: Math.max(actual.length, compiled.body.length) }, (_, index) => index).find(index => actual[index] !== compiled.body[index]) ?? -1 + return { id: value[".id"], disabled: value.disabled ?? null, invalid: value.invalid ?? null, expectedBytes: compiled.body.length, actualBytes: actual.length, firstDifference, + expectedCharacter: compiled.body.charCodeAt(firstDifference), actualCharacter: actual.charCodeAt(firstDifference), matchesWithoutHostMasks: actual === compiled.body.replace(/\/(?:32|128)(?=\))/g, "") } + }) }); await save() + } + if (row.state !== "applied" && tool.startsWith("mm_filter_")) { + const { client } = await routerClient(Number(input.serverId)) + const filters = await client.get[]>("/routing/filter/rule") + checks.push({ phase: "filter-failure-observed", serverId: input.serverId, nativeRows: filters.map(value => ({ id: value[".id"], owned: value.comment === "MikrotikManager: " + marker, disabled: value.disabled ?? null, invalid: value.invalid ?? null, ruleBytes: value.rule?.length ?? null })), + nativeAudit: (await pool.query("SELECT action,payload->>'method' AS method,payload->>'path' AS path FROM mcp_audit WHERE connection_id=$1 AND action IN ('configuration.intent','configuration.acknowledged','configuration.unconfirmed') ORDER BY id DESC LIMIT 10", [access.connection.id])).rows }); await save() + } + assert.equal(row.state, "applied", tool + ": " + row.state + "/" + row.error_code) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id); return operation.id + } + await delay(500) + } + throw Error("Operation pending; do not replay native writes") + } + const nativeWrites = async () => (await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action='configuration.intent' AND payload->>'path' LIKE '/routing/filter/rule%'", [access.connection.id])).rows[0].n + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const resources = ["gre", "routing_filter", "bgp_instance", "bgp_connection", "ospf_instance", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "vrf", "routing_table"] + const baseline = await Promise.all(resources.map(resource => readRosSnapshot(serverId, resource))) + if (cleanupMarker) { + const failed = JSON.parse(await readFile(new URL("../../../../docs/mcp-chr-filter-write-runtime-fence-error.json", import.meta.url), "utf8")) + assert.equal(failed.marker, cleanupMarker) + assert.deepEqual(await client.get("/routing/filter/rule"), []) + const owned = baseline.find(snapshot => snapshot.resource === "gre")!.rows.filter(row => row.name === gre && row.comment === marker) + for (const row of owned) { assert.equal(row.disabled, "true"); await execute("mm_gre_delete_plan", { serverId, id: row[".id"] }) } + const expected = failed.checks.find((row: { host?: string; phase?: string }) => row.host === host && row.phase === "preflight") + if (expected) for (const row of expected.baselineFingerprints) assert.equal((await readRosSnapshot(serverId, row.resource)).fingerprint, row.fingerprint) + else assert.equal(owned.length, 0) + checks.push({ host, phase: "cleanup", baselineRestored: !!expected, ownedRemoved: owned.length }); await save(); continue + } + for (const path of ["/routing/bgp/instance", "/routing/bgp/connection", "/routing/ospf/instance", "/routing/rip/instance", "/routing/isis/instance", "/routing/filter/select-rule", "/routing/filter/rule"]) assert.deepEqual(await client.get(path), [], "Native filter fixture refuses any existing routing contexts or filters") + assert.ok(!baseline.flatMap(snapshot => snapshot.rows).some(row => row.name === gre || row.comment === marker)) + checks.push({ host, phase: "preflight", baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })), routingContextsEmpty: true }); await save() + const blackhole = { community: "64512:11", communityName: "CHR fixture", action: "blackhole", gateway: "", gatewayTunnelId: "", description: "fixture" } + const route = { community: "64512:12", action: "route", gateway: "192.0.2.2", gatewayTunnelId: gre, description: "first }\nsecond" } + const route6 = { community: "64512:13", action: "route", gateway: "fd00:1::2", gatewayTunnelId: gre, description: "Owned IPv6 parser and metadata proof" } + const expandedRules = [blackhole, route, ...(ipv6Acceptance ? [route6] : [])] + const replace = (rules: unknown[], source = "apply") => execute("mm_filter_rules_replace_plan", { serverId, rules, source, requireUnbound: true }) + const policyId = marker + "-community" + try { + if (communityPolicyAcceptance) { + const policy = (rules: unknown[], action = "replace") => execute("mm_bgp_community_policy_plan", { serverId, policyId, rules, action }) + const rules = [{ prefix: "198.19.1.2/32", communityMode: "set", communities: ["64512:999"] }, { prefix: "fd6d:6370:1:2::2/128", communityMode: "set", communities: ["64512:1000"] }] + await policy(rules) + const beforeNoop = await nativeWrites(); await policy(rules); assert.equal(await nativeWrites(), beforeNoop) + await policy([{ prefix: "192.0.2.0/24", match: "subnet", minLength: 25, maxLength: 32, communityMode: "set", communities: ["64512:998"], action: "continue" }, + { prefix: "::/0", match: "subnet", minLength: 128, maxLength: 128, communityMode: "append", communities: ["64512:999"], action: "continue" }, + { prefix: "0.0.0.0/0", match: "subnet", communityMode: "clear" }]) + await policy([], "delete") + // Production has the separate managed input chain beside this output chain. + // Prove their native ordering/preservation rather than testing an empty table only. + await replace([blackhole]) + await policy(rules) + const withInputNoop = await nativeWrites(); await policy(rules); assert.equal(await nativeWrites(), withInputNoop) + await policy([], "delete") + checks.push({ host, phase: "community-policy-parser-noop-delete", exactDualStack: true, subnetRangeAndSingleLength: true, setAppendClear: true, propagationVerified: false }); await save(); continue + } + await assert.rejects(invokeMcpTool(principal, "mm_filter_rules_replace_plan", { serverId, rules: [{ ...route, gatewayTunnelId: gre + ' "$' }], requireUnbound: true })) + await execute("mm_gre_create_plan", { serverId, fields: { name: gre, "local-address": host, "remote-address": hosts[1 - index], disabled: true, comment: marker } }) + if (vrfAcceptance) await execute("mm_vrf_create_plan", { serverId, fields: { name: marker + "-vrf", interfaces: gre, comment: marker } }) + const globalGre = (await client.get[]>("/interface")).filter(row => row.name === gre) + const specificGre = (await client.get[]>("/interface/gre")).filter(row => row.name === gre) + checks.push({ host, phase: "interface-identity", globalIds: globalGre.map(row => row[".id"]), greIds: specificGre.map(row => row[".id"]) }); await save() + await replace([blackhole]) + const revisionId = (await pool.query("SELECT id FROM config_revisions WHERE server_id=$1 AND section='filters' ORDER BY created_at DESC LIMIT 1", [serverId])).rows[0].id + await replace(expandedRules) + const beforeNoop = await nativeWrites(); await replace(expandedRules); assert.equal(await nativeWrites(), beforeNoop) + await replace([{ ...blackhole, communityName: "copied fixture" }, ...expandedRules.slice(1)], "copy"); assert.equal(await nativeWrites(), beforeNoop) + const catalog = await invokeMcpTool(principal, "mm_bgp_filter_rules_get", { serverId, limit: 200 }) as { rulesets: { live: boolean; stale: boolean; rules: { items: { community: string; gatewayTunnelId: string; description: string }[] } }[] } + assert.equal(catalog.rulesets[0].live, true); assert.equal(catalog.rulesets[0].stale, false) + const observed = catalog.rulesets[0].rules.items.find(row => row.community === route.community)! + assert.equal(observed.gatewayTunnelId, gre); assert.equal(observed.description, route.description) + await execute("mm_filter_revision_restore_plan", { serverId, revisionId, requireUnbound: true }) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM filter_rules WHERE server_id=$1", [serverId])).rows[0].n, 1) + checks.push({ host, phase: "create-replace-noop-copy-restore", nativeCompilerAccepted: true, quoteAndCommentRoundtrip: true, noForwardingClaim: true }); await save() + } finally { + if (communityPolicyAcceptance) { + const own = (await client.get[]>("/routing/filter/rule")).filter(row => row.chain === bgpCommunityPolicyChain(policyId) || row.comment === bgpCommunityPolicyComment(policyId)) + assert.ok(own.length <= 1 && own.every(row => row.chain === bgpCommunityPolicyChain(policyId) && row.comment === bgpCommunityPolicyComment(policyId))) + if (own.length) await execute("mm_bgp_community_policy_plan", { serverId, policyId, rules: [], action: "delete" }) + } + const raw = await client.get[]>("/routing/filter/rule") + assert.ok(raw.every(row => row.chain === "bgp-in" && row.comment === "MikrotikManager: " + marker), "Cleanup refuses foreign filters") + await replace([]) + if (vrfAcceptance) { + const ownVrf = (await readRosSnapshot(serverId, "vrf")).rows.filter(row => row.name === marker + "-vrf" && row.comment === marker) + for (const row of ownVrf) { assert.equal(row.interfaces, gre); await execute("mm_vrf_delete_plan", { serverId, id: row[".id"] }) } + } + const ownGre = (await readRosSnapshot(serverId, "gre")).rows.filter(row => row.name === gre && row.comment === marker) + for (const row of ownGre) { assert.equal(row.disabled, "true"); await execute("mm_gre_delete_plan", { serverId, id: row[".id"] }) } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Configuration baseline changed after filter cleanup") + for (const path of ["/routing/rip/instance", "/routing/isis/instance", "/routing/filter/select-rule"]) assert.deepEqual(await client.get(path), []) + checks.push({ host, phase: "cleanup", baselineRestored: true }); await save() + } + console.log("✓ CHR " + serverId + ": Community compiler, replace/no-op/copy/revision, read-back and cleanup") + } + report.complete = true +} finally { MikrotikClient.prototype.post = originalPost; MikrotikClient.prototype.patch = originalPatch; report.finishedAt = new Date().toISOString(); await save(); await closePool() } diff --git a/backend/src/modules/mcp/chr-firewall-move.test.ts b/backend/src/modules/mcp/chr-firewall-move.test.ts new file mode 100644 index 00000000..dc51bb07 --- /dev/null +++ b/backend/src/modules/mcp/chr-firewall-move.test.ts @@ -0,0 +1,82 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === 2 && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const marker = "mcp-test-" + randomUUID().slice(0, 8), comments = [1, 2, 3].map(id => `${marker} #${id}`) +const actor: AuthUser = { id: "chr-firewall-move-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const report = { startedAt: new Date().toISOString(), marker, complete: false, packetMatchingVerified: false, fixtures: "three-disabled-rules-in-unreferenced-chain", checks, finishedAt: "" } +const save = () => writeFile(new URL("../../../../docs/mcp-chr-firewall-move-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, marker, ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const operationIds = ["ros.ip_filter.create", "ros.ip_filter.delete", "firewall.rules.move", "firewall.state.read"] + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, mutationsPerHour: 100 }, scope: { serverIds: [1, 2], operationIds, allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const execute = async (tool: string, input: Record) => { + const plan = await invokeMcpTool(principal, tool, input) as { planId: string; changes: { before?: { rows: Record[] } } } + if (tool === "mm_firewall_rule_move_plan") { + const rows = plan.changes.before?.rows + assert.equal(rows?.length, 3, "Never approve reordering an unexpected table") + assert.ok(rows!.every(row => row.chain === marker && comments.includes(row.comment) && row.disabled === "true")) + } + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + for (let attempt = 0; attempt < 100; attempt++) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { + assert.equal(row.state, "applied", `${tool}: ${row.state}/${row.error_code}`) + return { operationId: operation.id, planId: plan.planId, key } + } + await delay(300) + } + throw Error("Operation pending; never automatically replay lab writes") + } + await save() + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const baseline = await Promise.all(["ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw"].map(resource => readRosSnapshot(serverId, resource))) + assert.equal(baseline[0].rows.length, 0, "Lab move refuses an existing filter table") + checks.push({ host, phase: "preflight", baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })) }); await save() + try { + for (const comment of comments) await execute("mm_ip_filter_create_plan", { serverId, fields: { chain: marker, action: "accept", disabled: true, comment, "src-address": "192.0.2.0/24", "dst-address": "198.51.100.0/24" } }) + const own = await client.get[]>("/ip/firewall/filter") + assert.equal(own.length, 3); assert.ok(own.every(row => row.chain === marker && comments.includes(row.comment) && row.disabled === "true")) + const ids = comments.map(comment => { const rows = own.filter(row => row.comment === comment); assert.equal(rows.length, 1); return rows[0][".id"] }) + const moved = await execute("mm_firewall_rule_move_plan", { serverId, move: { family: "ip", table: "filter", rosId: ids[0], destinationRosId: ids[2] } }) + assert.deepEqual((await client.get[]>("/ip/firewall/filter")).map(row => row[".id"]), [ids[1], ids[0], ids[2]]) + assert.equal((await applyPlan(principal, moved.planId, moved.key)).id, moved.operationId) + const intents = await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent' AND payload->>'path'='/ip/firewall/filter/move' AND (payload->>'serverId')::int=$1", [serverId]) + assert.equal(intents.rows[0].n, 1) + const reverted = await execute("mm_firewall_rule_move_plan", { serverId, move: { family: "ip", table: "filter", rosId: ids[0], destinationRosId: ids[1] } }) + assert.deepEqual((await client.get[]>("/ip/firewall/filter")).map(row => row[".id"]), ids) + const state = await invokeMcpTool(principal, "mm_firewall_state_get", { serverId }) + assert.ok(!JSON.stringify(state).includes(process.env.MMAPP_TEST_CHR_PASSWORD!)) + checks.push({ host, phase: "move-and-explicit-return", operationId: moved.operationId, returnOperationId: reverted.operationId, idempotencyVerified: true, allRulesDisabled: true, sharedCatalog: state }); await save() + } finally { + for (const row of await client.get[]>("/ip/firewall/filter")) if (row.chain === marker && comments.includes(row.comment)) { + assert.equal(row.disabled, "true"); await execute("mm_ip_filter_delete_plan", { serverId, id: row[".id"] }) + } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Lab firewall baseline changed") + checks.push({ host, phase: "cleanup", baselineRestored: true }); await save() + } + console.log(`✓ CHR ${serverId}: disabled private-chain firewall move, exact return, no replay and complete baseline cleanup`) + } + report.complete = true; report.finishedAt = new Date().toISOString(); await save() +} finally { await closePool() } diff --git a/backend/src/modules/mcp/chr-firewall-restore.test.ts b/backend/src/modules/mcp/chr-firewall-restore.test.ts new file mode 100644 index 00000000..a0cf894b --- /dev/null +++ b/backend/src/modules/mcp/chr-firewall-restore.test.ts @@ -0,0 +1,97 @@ +import { canonicalFirewallSnapshot } from "../../services/entity-snapshots.js" +import { fingerprintPayload } from "../../services/config-revisions.js" +import { fingerprint } from "./security.js" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile, readFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === 2 && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const marker = "mcp-test-" + randomUUID().slice(0, 8) +let previous: { marker?: string; complete?: boolean; startedAt?: string; checks?: { host?: string; phase?: string; baselineFingerprints?: { fingerprint: string }[]; addressFingerprints?: string[] }[] } | undefined +try { previous = JSON.parse(await readFile(new URL("../../../../docs/mcp-chr-firewall-restore-acceptance.json", import.meta.url), "utf8")) } catch { /* First run. */ } +const recoveryMarker = previous?.complete === false && /^mcp-test-[a-f0-9]{8}$/.test(previous.marker ?? "") && Date.now() - Date.parse(previous.startedAt ?? "") < 7200000 ? previous.marker : undefined +const actor: AuthUser = { id: "chr-firewall-restore-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const report = { startedAt: new Date().toISOString(), marker, complete: false, packetMatchingVerified: false, checks, finishedAt: "" } +const save = () => writeFile(new URL("../../../../docs/mcp-chr-firewall-restore-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, marker, ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, mutationsPerHour: 100 }, scope: { serverIds: [1, 2], operationIds: ["firewall.revision.restore"], allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const execute = async (serverId: number, revisionId: string) => { + const plan = await invokeMcpTool(principal, "mm_firewall_revision_restore_plan", { serverId, revisionId }) as { planId: string; changes: { before: { state: { chunks: { rows: Record[] }[] } } } } + assert.ok(plan.changes.before.state.chunks.every(chunk => chunk.rows.every(row => row.disabled === "true" && [marker, recoveryMarker].some(prefix => prefix && row.comment?.startsWith(prefix)))), "Never approve unrelated/active lab rules") + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + for (let attempt = 0; attempt < 300; attempt++) { + const row = (await pool.query("SELECT state,error_code,result FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { + if (row.state !== "applied") { checks.push({ phase: "failed-operation", state: row.state, code: row.error_code, message: row.result, steps: (await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows }); await save() } + assert.equal(row.state, "applied", row.state + "/" + row.error_code + "/" + row.result) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + return operation.id + } + await delay(300) + } + throw Error("Restore pending; never replay lab writes automatically") + } + await save() + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + if (recoveryMarker) { + const knownEmpty = "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945" + const proof = previous?.checks?.find(check => check.host === host && check.phase === "preflight") + if (proof?.baselineFingerprints?.length === 8 && proof.addressFingerprints?.length === 2 && proof.baselineFingerprints.every(row => row.fingerprint === knownEmpty) && proof.addressFingerprints.every(value => value === knownEmpty)) { + const rows = (await Promise.all(["ip", "ipv6"].flatMap(family => ["filter", "nat", "mangle", "raw", "address-list"].map(table => client.get[]>("/" + family + "/firewall/" + table))))).flat() + if (rows.length) { + assert.ok(rows.every(row => row.disabled === "true" && row.comment?.startsWith(recoveryMarker)), "Refuse recovery with foreign/active data") + const cleanupId = randomUUID(), empty = canonicalFirewallSnapshot({}) + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,$2,'firewall','apply',$3,$4::jsonb)", [cleanupId, serverId, fingerprintPayload(empty), JSON.stringify(empty)]) + await execute(serverId, cleanupId) + checks.push({ host, phase: "previous-fixtures-reconciled", previousMarker: recoveryMarker }); await save() + } + } + } + const baseline = await Promise.all(["ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw"].map(resource => readRosSnapshot(serverId, resource))) + const paths = ["/ip/firewall/address-list", "/ipv6/firewall/address-list"], addressBaseline = await Promise.all(paths.map(path => client.get[]>(path))) + assert.ok(baseline.every(row => row.rows.length === 0) && addressBaseline.every(rows => rows.length === 0), "Lab restore refuses any existing Firewall configuration") + const payload = canonicalFirewallSnapshot({ rules: (["ip", "ip6"] as const).flatMap(family => ["accept", "accept", "drop"].map(action => ({ family, table: "filter" as const, chain: marker, action, comment: marker, disabled: true, native: { "connection-mark": "no-mark" } }))), addressLists: (["ip", "ip6"] as const).map(family => ({ family, list: marker, address: family === "ip" ? "198.18.249.0/24" : "2001:db8:249::/64", comment: marker, disabled: true })) }) + const revisionId = randomUUID(), emptyId = randomUUID(), empty = canonicalFirewallSnapshot({}) + for (const [id, value] of [[revisionId, payload], [emptyId, empty]] as const) await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,$2,'firewall','apply',$3,$4::jsonb)", [id, serverId, fingerprintPayload(value), JSON.stringify(value)]) + checks.push({ host, phase: "preflight", baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })), addressFingerprints: addressBaseline.map(fingerprint) }); await save() + try { + const operationId = await execute(serverId, revisionId) + for (const family of ["ip", "ipv6"]) { + const rows = await client.get[]>("/" + family + "/firewall/filter") + assert.deepEqual(rows.map(row => row.action), ["accept", "accept", "drop"]); assert.ok(rows.every(row => row.chain === marker && row.disabled === "true" && row["connection-mark"] === "no-mark")) + const lists = await client.get[]>("/" + family + "/firewall/address-list"); assert.equal(lists.length, 1); assert.equal(lists[0].list, marker) + } + const step = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [operationId])).rows[0].result + assert.equal(step.verified, true); assert.equal(step.completedCommands, 8); assert.equal(step.revisionRecorded, true) + checks.push({ host, phase: "restored", staticRules: 6, staticLists: 2, opaqueMatcherVerified: true, duplicateOrderVerified: true, idempotencyVerified: true }); await save() + } finally { + await execute(serverId, emptyId) + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Firewall baseline changed") + for (const [index, path] of paths.entries()) assert.equal(fingerprint(await client.get(path)), fingerprint(addressBaseline[index]), "Address-list baseline changed") + checks.push({ host, phase: "cleanup", baselineRestored: true }); await save() + } + console.log("✓ CHR " + serverId + ": native matcher/order/duplicates, staged static restore and explicit empty restore; baseline recovered") + } + report.complete = true; report.finishedAt = new Date().toISOString(); await save() +} finally { await closePool() } diff --git a/backend/src/modules/mcp/chr-firewall-write.test.ts b/backend/src/modules/mcp/chr-firewall-write.test.ts new file mode 100644 index 00000000..2cc42b8e --- /dev/null +++ b/backend/src/modules/mcp/chr-firewall-write.test.ts @@ -0,0 +1,84 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === 2 && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const marker = "mcp-test-" + randomUUID().slice(0, 8) +const actor: AuthUser = { id: "chr-firewall-write-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const report = { startedAt: new Date().toISOString(), marker, complete: false, packetMatchingVerified: false, fixtures: "private-unreferenced-chain-v4-v6", checks, finishedAt: "" } +const save = () => writeFile(new URL("../../../../docs/mcp-chr-firewall-write-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, marker, ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const operationIds = ["firewall.rules.create", "firewall.rules.update", "firewall.rules.toggle", "firewall.rules.delete", "ros.ip_filter.delete", "ros.ipv6_filter.delete"] + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, mutationsPerHour: 100 }, scope: { serverIds: [1, 2], operationIds, allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const execute = async (tool: string, input: Record) => { + const plan = await invokeMcpTool(principal, tool, input) as { planId: string } + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + for (let attempt = 0; attempt < 200; attempt++) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { + assert.equal(row.state, "applied", tool + ": " + row.state + "/" + row.error_code) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + return operation.id + } + await delay(300) + } + throw Error("Operation pending; never replay lab writes automatically") + } + await save() + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const resources = ["ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw"] + const baseline = await Promise.all(resources.map(resource => readRosSnapshot(serverId, resource))) + assert.ok(baseline.filter(snapshot => ["ip_filter", "ipv6_filter"].includes(snapshot.resource)).every(snapshot => snapshot.rows.length === 0), "Lab refuses existing filter rules") + checks.push({ host, phase: "preflight", baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })) }); await save() + try { + for (const family of ["ip", "ip6"] as const) { + const path = family === "ip" ? "/ip/firewall/filter" : "/ipv6/firewall/filter" + const fields = { chain: marker, action: "accept", disabled: true, comment: marker, srcAddress: family === "ip" ? "192.0.2.0/24" : "2001:db8::/64" } + const invoke = (operation: string, extra: Record) => execute("mm_firewall_rule_form_" + operation + "_plan", { serverId, rule: { family, table: "filter", operation, ...extra } }) + await invoke("create", { fields }) + const rows = await client.get[]>(path) + assert.equal(rows.length, 1); const rosId = rows[0][".id"] + assert.equal(rows[0].disabled, "true"); assert.equal(rows[0].chain, marker) + await invoke("update", { rosId, fields: { ...fields, action: "drop", comment: marker + " edited" } }) + const edited = await client.get[]>(path) + assert.equal(edited[0].action, "drop"); assert.equal(edited[0]["src-address"], fields.srcAddress) + await invoke("toggle", { rosId, disabled: false }) + assert.equal((await client.get[]>(path))[0].disabled, "false") + await invoke("toggle", { rosId, disabled: true }) + await invoke("delete", { rosId }) + assert.deepEqual(await client.get(path), []) + checks.push({ host, family, phase: "create-edit-toggle-delete", idempotencyVerified: true, chainUnreferenced: true }); await save() + } + } finally { + for (const family of ["ip", "ip6"]) { + const path = family === "ip" ? "/ip/firewall/filter" : "/ipv6/firewall/filter", resource = family === "ip" ? "ip_filter" : "ipv6_filter" + for (const row of await client.get[]>(path)) if (row.chain === marker && row.comment.startsWith(marker)) await execute("mm_" + resource + "_delete_plan", { serverId, id: row[".id"] }) + } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Firewall baseline changed") + checks.push({ host, phase: "cleanup", baselineRestored: true }); await save() + } + console.log("✓ CHR " + serverId + ": IPv4/IPv6 firewall form CRUD, exact matchers, no replay and cleanup") + } + report.complete = true; report.finishedAt = new Date().toISOString(); await save() +} finally { await closePool() } diff --git a/backend/src/modules/mcp/chr-fixture-context.ts b/backend/src/modules/mcp/chr-fixture-context.ts new file mode 100644 index 00000000..473ccfe9 --- /dev/null +++ b/backend/src/modules/mcp/chr-fixture-context.ts @@ -0,0 +1,8 @@ +import assert from "node:assert/strict" + +export const chrOspfForwarding = () => process.env.MMAPP_TEST_CHR_OSPF_FORWARD === "true" +export const chrOspfPrefix = () => process.env.MMAPP_TEST_CHR_OSPF_PREFIX === "true" +export function chrFixtureVrf(marker: string, serverId: number, forwarding = chrOspfForwarding()) { + assert.match(marker, /^mcp-test-[0-9a-f]{8}$/); assert.ok([1, 2].includes(serverId)) + return marker + (forwarding ? `-vrf-${serverId}` : "-vrf") +} diff --git a/backend/src/modules/mcp/chr-gre-mutation.test.ts b/backend/src/modules/mcp/chr-gre-mutation.test.ts new file mode 100644 index 00000000..8b406422 --- /dev/null +++ b/backend/src/modules/mcp/chr-gre-mutation.test.ts @@ -0,0 +1,118 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS??"").split(",").filter(Boolean), ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +assert.equal(hosts.length,2);assert.equal(new Set(hosts).size,2);assert.ok(hosts.every(host=>["194.226.121.150","104.165.244.7"].includes(host))) +if(ports)assert.ok(ports.length===2&&ports.every(port=>Number.isInteger(port)&&port>=1024&&port<=65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER&&process.env.MMAPP_TEST_CHR_PASSWORD) +const marker="mcp-gre-"+randomUUID().slice(0,8), actor:AuthUser={id:"chr-gre-service-owner",apps:["mm"],permissions:["mm:settings:admin","mm:network:admin","mm:servers:admin"],expiresAt:Date.now()+3600000} +const checks:Record[]=[] +const report={startedAt:new Date().toISOString(),marker,complete:false,forwardingVerified:false,ipsecSaVerified:false,fixtures:"disabled owned GRE; static v4 then v6",checks,finishedAt:""} +const save=()=>writeFile(new URL("../../../../docs/mcp-chr-gre-mutation-acceptance.json",import.meta.url),JSON.stringify(report,null,2)+"\n") +try { + await applySqlMigrations(pool);assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n,0) + await updateMcpSettings({enabled:true},actor) + for(const [index,host]of hosts.entries())await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)",[index+1,marker,ports?"127.0.0.1":host,ports?.[index]??80,process.env.MMAPP_TEST_CHR_USER,process.env.MMAPP_TEST_CHR_PASSWORD]) + const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:marker,mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],limits:{callsPerMinute:120,mutationsPerHour:300},scope:{serverIds:[1,2],operationIds:["network.gre.create","network.gre.update","network.gre.delete","ros.gre.delete","ros.gre.update","ros.ipv4_address.delete","ros.ipv6_address.delete"],allowGlobal:false}}),actor) + const principal=await authenticateMcpToken(access.token) + const execute=async(tool:string,input:Record)=>{ + const plan=await invokeMcpTool(principal,tool,input) as {planId:string};await approvePlan(plan.planId,actor,true) + const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key) + for(let attempt=0;attempt<200;attempt++){ + const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[operation.id])).rows[0] + if(!["pending","running"].includes(row.state)){ + const steps=(await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index",[operation.id])).rows + checks.push({tool,serverId:input.serverId,operationId:operation.id,state:row.state,errorCode:row.error_code,steps});await save() + assert.equal(row.state,"applied",tool+": "+row.state+"/"+row.error_code) + assert.equal((await applyPlan(principal,plan.planId,key)).id,operation.id) + return + } + await delay(300) + } + throw Error("GRE operation is still running; do not replay lab writes") + } + await save() + for(const[index,host]of hosts.entries()){ + const serverId=index+1,{client}=await routerClient(serverId),name=marker+"-"+serverId,nativeName=marker+"-native-"+serverId,renamedName=marker+"-renamed-"+serverId + const ownedIds=new Set() + const ipv4=`198.18.253.${index*4+1}/30`,ipv6=`fd00:ab:45:${serverId}::1/64` + const resources=["gre","ipv4_address","ipv6_address","vrf","routing_table","ipv4_route","ipv6_route","routing_filter"] + const baseline=await Promise.all(resources.map(resource=>readRosSnapshot(serverId,resource))) + const nativeGre=await client.get[]>("/interface/gre") + assert.ok(!nativeGre.some(row=>row.name===name)) + for(const path of ["/ip/address","/ipv6/address"])assert.ok(!(await client.get[]>(path)).some(row=>row.address===ipv4||row.address===ipv6),"Fixture address already exists") + try { + await execute("mm_gre_tunnel_create_plan",{serverId,clearIpsecSecret:true,fields:{name,remoteAddress:"198.51.100.254",localInnerIp:ipv4,enabled:false,comment:marker,mtu:1400,keepaliveInterval:10,keepaliveRetries:10}}) + let gre=(await client.get[]>("/interface/gre")).find(row=>row.name===name)! + assert.ok(gre);assert.equal(gre.disabled,"true");assert.equal(gre.mtu,"1400") + const rosId=gre[".id"] + ownedIds.add(rosId) + assert.ok((await client.get[]>("/ip/address")).some(row=>row.interface===name&&row.address===ipv4)) + await execute("mm_gre_tunnel_update_plan",{serverId,rosId,fields:{localInnerIp:ipv6,mtu:1390,comment:marker+" edited",enabled:false}}) + gre=(await client.get[]>("/interface/gre")).find(row=>row[".id"]===rosId)! + assert.equal(gre.disabled,"true");assert.equal(gre.mtu,"1390") + assert.ok(!(await client.get[]>("/ip/address")).some(row=>row.interface===name)) + const address=(await client.get[]>("/ipv6/address")).find(row=>row.interface===name&&row.address===ipv6)! + assert.ok(address);assert.equal(address.advertise,"false") + await execute("mm_gre_tunnel_delete_plan",{serverId,rosId}) + assert.ok(!(await client.get[]>("/interface/gre")).some(row=>row.name===name)) + checks.push({host,phase:"disabled-v4-v6-crud",nativeId:rosId,ipv6Advertise:address.advertise,explicitEmptyPskConfigurationVerified:true,idempotencyVerified:true,forwardingVerified:false});await save() + await execute("mm_gre_tunnel_create_plan",{serverId,fields:{name:nativeName,remoteAddress:"198.51.100.254",enabled:false,comment:marker,mtu:1400}}) + const native=(await client.get[]>("/interface/gre")).find(row=>row.name===nativeName)! + assert.ok(native);assert.equal(native.disabled,"true") + ownedIds.add(native[".id"]) + await execute("mm_gre_update_plan",{serverId,id:native[".id"],fields:{name:renamedName,comment:""}}) + const renamed=(await client.get[]>("/interface/gre")).find(row=>row[".id"]===native[".id"])! + assert.equal(renamed.name,renamedName);assert.equal(renamed.disabled,"true");assert.equal(renamed.mtu,"1400");assert.equal(renamed.comment??"","") + await execute("mm_gre_delete_plan",{serverId,id:native[".id"]}) + checks.push({host,phase:"native-alias-rename-delete",nativeId:native[".id"],oneInterfaceOnly:true,emptyCommentVerified:true,idempotencyVerified:true,forwardingVerified:false});await save() + } catch(error) { + const own=(await client.get[]>("/interface/gre")).find(row=>row.name===name&&row.comment?.startsWith(marker)) + if(own)for(const[label,body]of [ + ["get-id",{".id":own[".id"],"value-name":"ipsec-secret"}], + ["get-number",{number:own[".id"],"value-name":"ipsec-secret"}], + ["get-numbers",{numbers:own[".id"],"value-name":"ipsec-secret"}], + ["print-id",{"show-sensitive":"",".proplist":".id,ipsec-secret",".query":[".id="+own[".id"]]}], + ["print-name",{"show-sensitive":"",".proplist":".id,ipsec-secret",".query":["name="+name]}], + ["print-default",{".proplist":".id,ipsec-secret",".query":["name="+name]}], + ] as const){ + try{ + const raw=await client.post("/interface/gre/"+(label.startsWith("get")?"get":"print"),body as Record) + const items=Array.isArray(raw)?raw:[raw] + checks.push({host,phase:"psk-source-diagnostic",method:label,records:items.length,shape:items.map(row=>({retType:typeof row?.ret,retEmpty:row?.ret==="",secretType:typeof row?.["ipsec-secret"],secretEmpty:row?.["ipsec-secret"]==="",identityPresent:typeof row?.[".id"]==="string"}))}) + }catch(probe){checks.push({host,phase:"psk-source-diagnostic",method:label,httpStatus:probe&&typeof probe==="object"&&"statusCode"in probe?probe.statusCode:null})} + } + await save() + try { + const {readGreMutationState}=await import("../../services/gre-mutation-state.js") + const {server}=await routerClient(serverId) + await readGreMutationState(server,{operation:"delete",name}) + } catch(diagnostic) { + const message=diagnostic instanceof Error?diagnostic.message:"" + checks.push({host,phase:"failure-diagnosis",sourceError:message.startsWith("Не подтверждено")||message.startsWith("Неполный")?message:"Unexpected read-back failure"});await save() + } + throw error + } finally { + // A failed service operation is never replayed. Cleanup is a separate, + // freshly approved native plan limited to this run's exact fixture name. + for(const gre of await client.get[]>("/interface/gre"))if([name,nativeName,renamedName].includes(gre.name)&&(gre.comment?.startsWith(marker)||ownedIds.has(gre[".id"]))){ + if(gre.disabled!=="true")await execute("mm_gre_update_plan",{serverId,id:gre[".id"],fields:{disabled:true}}) + for(const[resource,path]of [["ipv4_address","/ip/address"],["ipv6_address","/ipv6/address"]])for(const row of await client.get[]>(path))if(row.interface===gre.name&&[ipv4,ipv6].includes(row.address)&&row.dynamic!=="true")await execute(`mm_${resource}_delete_plan`,{serverId,id:row[".id"]}) + await execute("mm_gre_delete_plan",{serverId,id:gre[".id"]}) + } + for(const snapshot of baseline)assert.equal((await readRosSnapshot(serverId,snapshot.resource)).fingerprint,snapshot.fingerprint,"GRE lab baseline differs: "+snapshot.resource) + checks.push({host,phase:"cleanup",baselineRestored:true,fingerprints:baseline.map(row=>({resource:row.resource,fingerprint:row.fingerprint}))});await save() + } + console.log(`✓ CHR ${serverId}: service GRE CRUD, disabled v4→v6/empty PSK, native rename/delete, idempotency and baseline restored`) + } + report.complete=true;report.finishedAt=new Date().toISOString();await save() +} finally {await closePool()} diff --git a/backend/src/modules/mcp/chr-gre-revision-recovery.test.ts b/backend/src/modules/mcp/chr-gre-revision-recovery.test.ts new file mode 100644 index 00000000..e1644588 --- /dev/null +++ b/backend/src/modules/mcp/chr-gre-revision-recovery.test.ts @@ -0,0 +1,67 @@ +import assert from "node:assert/strict" +import {randomUUID} from "node:crypto" +import {readFile,writeFile} from "node:fs/promises" +import {setTimeout as delay} from "node:timers/promises" +import {pool,closePool} from "../../db/index.js" +import {applySqlMigrations} from "../../db/migrate.js" +import {MikrotikClient} from "../../services/mikrotik.js" +import {mcpConnectionCreateSchema} from "@mmapp/contracts/mcp" +import {createMcpConnection,authenticateMcpToken,updateMcpSettings} from "./store.js" +import {invokeMcpTool,approvePlan,applyPlan} from "./runtime.js" +import {readRosSnapshot,routerClient} from "./resources.js" +import {chrNativeFailure} from "./chr-native-failure.js" +import type {AuthUser} from "../../lib/permissions.js" + +// This is a separate new human cleanup, never a replay of an unknown operation. +const prior=JSON.parse(await readFile(new URL("../../../../docs/mcp-chr-gre-revision-f1147896-auth-interrupted.json",import.meta.url),"utf8")) +assert.equal(prior.marker,"mcp-test-f1147896");assert.equal(prior.complete,false);assert.ok(prior.finishedAt) +assert.ok(prior.checks.some((row:Record)=>row.phase==="mcp-operation"&&row.state==="unknown")) +const hosts=(process.env.MMAPP_TEST_CHR_HOSTS??"").split(","),ports=process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +assert.deepEqual(hosts,["194.226.121.150","104.165.244.7"]);assert.equal(process.env.MMAPP_TEST_CHR_USER,"admin");assert.ok(process.env.MMAPP_TEST_CHR_PASSWORD) +if(ports)assert.ok(ports.length===2&&ports.every(port=>Number.isInteger(port)&&port>=1024&&port<=65535)) +const marker=prior.marker as string,checks:Record[]=[],dispatches:Record[]=[] +const report={startedAt:new Date().toISOString(),marker,complete:false,originalAttemptAccepted:false,unknownOperationReplayed:false,checks,dispatches, + originalPrivateProofPortable:false,privateProofLimitation:"The original disposable MCP signing key and SQL source rows were removed by the finished harness. Recovery can compare all 16 original native fingerprints; it cannot recreate server 2 original private dependency HMAC."} +const save=()=>writeFile(new URL("../../../../docs/mcp-chr-gre-revision-f1147896-recovery.json",import.meta.url),JSON.stringify(report,null,2)+"\n") +const get=MikrotikClient.prototype.get,post=MikrotikClient.prototype.post,del=MikrotikClient.prototype.delete +let pace=Promise.resolve(),last=0 +const gap=()=>{const work=pace.then(async()=>{const wait=300-(Date.now()-last);if(wait>0)await delay(wait);last=Date.now()});pace=work.catch(()=>{});return work} +MikrotikClient.prototype.get=async function(path:string,timeout?:number,signal?:AbortSignal){await gap();try{return await get.call(this,path,timeout,signal)as T}catch(error){checks.push(chrNativeFailure(error,"GET",path,Date.now()));await save();throw error}} +MikrotikClient.prototype.post=async function(path:string,body:Record,timeout?:number,signal?:AbortSignal){assert.ok(path.endsWith("/get")||path.endsWith("/print"));await gap();return await post.call(this,path,body,timeout,signal)as T} +MikrotikClient.prototype.delete=async function(path:string,timeout?:number,before?:()=>Promise){assert.match(path,/^\/(?:interface\/gre|ip\/address|ipv6\/address)\/\*[A-Za-z0-9]+$/);await gap();return del.call(this,path,timeout,async()=>{await before?.();dispatches.push({method:"DELETE",path,at:new Date().toISOString()});await save()})} +const actor:AuthUser={id:"chr-gre-revision-recovery",apps:["mm"],permissions:["mm:settings:admin","mm:network:admin"],expiresAt:Date.now()+7200000} +try{ + await save();await applySqlMigrations(pool);assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n,0) + await updateMcpSettings({enabled:true},actor) + for(const[index,host]of hosts.entries())await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)",[index+1,marker,ports?"127.0.0.1":host,ports?.[index]??80,"admin",process.env.MMAPP_TEST_CHR_PASSWORD]) + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:marker+" recovery",mode:"approval",expiresAt:new Date(Date.now()+7200000).toISOString(),permissions:["mm:network:read","mm:network:write"],scope:{serverIds:[1,2],operationIds:["ros.gre.delete","ros.ipv4_address.delete","ros.ipv6_address.delete"],allowGlobal:false},limits:{callsPerMinute:1000,mutationsPerHour:100}}),actor),principal=await authenticateMcpToken(issued.token) + const execute=async(tool:string,input:Record)=>{ + const plan=await invokeMcpTool(principal,tool,input)as{planId:string;state:string};assert.equal(plan.state,"awaiting-approval");await approvePlan(plan.planId,actor,true) + const operation=await applyPlan(principal,plan.planId,randomUUID());checks.push({phase:"fresh-cleanup-plan",tool,serverId:input.serverId,planId:plan.planId,operationId:operation.id});await save() + for(let i=0;i<480;i++){const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[operation.id])).rows[0];if(!["pending","running"].includes(row.state)){checks.push({phase:"cleanup-terminal",operationId:operation.id,state:row.state,errorCode:row.error_code});await save();assert.equal(row.state,"applied");return}await delay(500)} + throw Error("Live cleanup operation; no new commands or retry allowed") + } + // Confirm both endpoints and known ownership before the first cleanup command. + for(const serverId of [1,2]){const {client}=await routerClient(serverId);await client.get("/system/resource");const rows=await client.get[]>("/interface/gre");assert.ok(rows.every(row=>[marker+"-a",marker+"-b"].includes(row.name)&&row.comment===marker&&row.disabled==="true"));checks.push({phase:"preflight",serverId,ownedGre:rows.length});await save()} + for(const serverId of [1,2]){ + const {client}=await routerClient(serverId) + const allowed=new Set([`198.18.251.${serverId}/30`,`198.18.252.${serverId}/30`,`fd00:ab:46:${serverId}::1/64`]) + for(const gre of await client.get[]>("/interface/gre")){ + assert.ok([marker+"-a",marker+"-b"].includes(gre.name)&&gre.comment===marker&&gre.disabled==="true") + assert.equal(gre["remote-address"],gre.name===marker+"-a"?"198.51.100.253":"198.51.100.254") + for(const [resource,path]of [["ipv4_address","/ip/address"],["ipv6_address","/ipv6/address"]])for(const address of await client.get[]>(path))if(address.interface===gre.name){ + // Kernel link-local rows are not independently configurable and leave + // with the positively owned parent. Other dynamic addresses refuse cleanup. + if(path==="/ipv6/address"&&address.dynamic==="true"&&/^fe80:/i.test(address.address))continue + assert.equal(address.dynamic,"false");assert.ok(allowed.has(address.address));await execute(`mm_${resource}_delete_plan`,{serverId,id:address[".id"]}) + } + await execute("mm_gre_delete_plan",{serverId,id:gre[".id"]}) + } + const baselines=prior.checks.find((row:Record)=>row.phase==="preflight").baselineFingerprints.filter((row:{serverId:number})=>row.serverId===serverId) + assert.equal(baselines.length,8) + for(const baseline of baselines)assert.equal((await readRosSnapshot(serverId,baseline.resource)).fingerprint,baseline.fingerprint,`Original native baseline ${serverId}:${baseline.resource}`) + checks.push({phase:"original-native-baseline-restored",serverId,fingerprints:8,ownedObjectsRemoved:true});await save() + } + report.complete=true +}catch(error){checks.push({phase:"failure",name:error instanceof Error?error.name:"unknown"});process.exitCode=1} +finally{Object.assign(report,{finishedAt:new Date().toISOString()});await save();MikrotikClient.prototype.get=get;MikrotikClient.prototype.post=post;MikrotikClient.prototype.delete=del;await closePool()} diff --git a/backend/src/modules/mcp/chr-gre-revision.test.ts b/backend/src/modules/mcp/chr-gre-revision.test.ts new file mode 100644 index 00000000..f1326a4e --- /dev/null +++ b/backend/src/modules/mcp/chr-gre-revision.test.ts @@ -0,0 +1,180 @@ +import assert from "node:assert/strict" +import {randomUUID} from "node:crypto" +import {writeFile} from "node:fs/promises" +import {setTimeout as delay} from "node:timers/promises" +import {pool,closePool} from "../../db/index.js" +import {applySqlMigrations} from "../../db/migrate.js" +import {canonicalGreSnapshot} from "../../services/entity-snapshots.js" +import {fingerprintPayload} from "../../services/config-revisions.js" +import {readGreRestoreState,greRestoreStateProof} from "../../services/gre-revision-restore.js" +import {MikrotikClient,MikrotikError} from "../../services/mikrotik.js" +import {mcpConnectionCreateSchema} from "@mmapp/contracts/mcp" +import {createMcpConnection,authenticateMcpToken,updateMcpSettings} from "./store.js" +import {invokeMcpTool,approvePlan,applyPlan} from "./runtime.js" +import {readRosSnapshot,routerClient,type RosSnapshot} from "./resources.js" +import {chrNativeFailure} from "./chr-native-failure.js" +import type {AuthUser} from "../../lib/permissions.js" + +const hosts=(process.env.MMAPP_TEST_CHR_HOSTS??"").split(",") +assert.deepEqual(hosts,["194.226.121.150","104.165.244.7"]) +assert.equal(process.env.MMAPP_TEST_CHR_USER,"admin");assert.ok(process.env.MMAPP_TEST_CHR_PASSWORD) +const ports=process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if(ports)assert.ok(ports.length===2&&ports.every(port=>Number.isInteger(port)&&port>=1024&&port<=65535)) +const marker="mcp-test-"+randomUUID().slice(0,8),resources=["gre","ipv4_address","ipv6_address","vrf","routing_table","ipv4_route","ipv6_route","routing_filter"] +const actor:AuthUser={id:"chr-gre-revision-owner",apps:["mm"],permissions:["mm:settings:admin","mm:network:admin"],expiresAt:Date.now()+7200000} +const checks:Record[]=[],dispatches:{method:string;path:string;at:string}[]=[] +const report={startedAt:new Date().toISOString(),marker,complete:false,scope:"Public GRE whole-section revision restore configuration only",operationVersion:1,nativeRequestGapMs:100,forwardingVerified:false,ipv6ReadinessVerified:false,ipsecSaVerified:false,checks,dispatches} +const reportUrl=new URL("../../../../docs/mcp-chr-gre-revision-acceptance.json",import.meta.url) +let saves=Promise.resolve() +const save=()=>{const value=JSON.stringify(report,null,2)+"\n";const next=saves.then(()=>writeFile(reportUrl,value));saves=next.catch(()=>{});return next} +let pacing=Promise.resolve(),lastRequest=0 +const pace=async()=>{const next=pacing.then(async()=>{const wait=100-(Date.now()-lastRequest);if(wait>0)await delay(wait);lastRequest=Date.now()});pacing=next.catch(()=>{});await next} +const originalGet=MikrotikClient.prototype.get,originalPost=MikrotikClient.prototype.post,originalPut=MikrotikClient.prototype.put,originalPatch=MikrotikClient.prototype.patch,originalDelete=MikrotikClient.prototype.delete +const failure=async(error:unknown,method:string,path:string,at:number)=>{ + if(checks.filter(c=>c.phase==="native-request-failure").length>=64)return + checks.push(chrNativeFailure(error,method,path,at)) + if(error instanceof MikrotikError&&error.statusCode===400&&["PUT","PATCH","DELETE"].includes(method)&&/^\/interface\/gre(?:\/\*[A-Za-z0-9]+)?$/.test(path)){ + // Only our empty-PSK fixture writes reach this profile. Never retain a raw + // provider body, authorization header, credentials or arbitrary key value. + try{const value=JSON.parse(error.body);for(const field of ["message","detail"])if(typeof value?.[field]==="string"){ + const detail=value[field].replaceAll(process.env.MMAPP_TEST_CHR_PASSWORD!,"[redacted]").replace(/(?:password|secret|token|private-key|ipsec-secret)\s*[=:]\s*(?:"[^"]*"|'[^']*'|[^\s;,]+)/gi,"[redacted]").slice(0,300) + checks.push({phase:"owned-gre-parser-error",method,path,field,detail}) + }}catch{/* A non-JSON body is not evidence suitable for the report. */} + } + await save() +} +const dispatched=async(method:string,path:string,before?:()=>Promise)=>{await before?.();assert.match(path,/^\/(?:interface\/gre|ip\/address|ipv6\/address)(?:\/\*[A-Za-z0-9]+)?$/);dispatches.push({method,path,at:new Date().toISOString()});await save()} +MikrotikClient.prototype.get=async function(path:string,timeout?:number,signal?:AbortSignal):Promise{await pace();const at=Date.now();try{ + const value=await originalGet.call(this,path,timeout,signal)as T + if(["/interface","/interface/gre"].includes(path)&&Array.isArray(value)&&checks.filter(c=>c.phase==="owned-interface-shape").length<128){ + const owned=value.filter(row=>row&&typeof row.name==="string"&&row.name.startsWith(marker+"-")) + if(owned.length)checks.push({phase:"owned-interface-shape",path,rows:owned.map(row=>({id:row[".id"],name:row.name,type:row.type??null,dynamic:row.dynamic??null}))}) + } + return value +}catch(error){await failure(error,"GET",path,at);throw error}} +MikrotikClient.prototype.post=async function(path:string,body:Record,timeout?:number,signal?:AbortSignal):Promise{ + await pace();const at=Date.now() + try{ + const value=await originalPost.call(this,path,body,timeout,signal)as T + if(path==="/interface/print"&&Array.isArray(body[".query"])&&checks.filter(c=>c.phase==="interface-flag-query").length<128){ + checks.push({phase:"interface-flag-query",query:body[".query"],rows:Array.isArray(value)?value.map(row=>({id:row[".id"],name:row.name,type:row.type??null,dynamic:row.dynamic??null})):null}) + } + if(/^\/(?:interface\/gre|ip\/address|ipv6\/address)\/get$/.test(path)&&checks.filter(c=>c.phase==="getter-shape").length<256){ + const ret=value&&typeof value==="object"&&"ret"in value?value.ret:undefined + checks.push({phase:"getter-shape",path,property:body["value-name"],array:Array.isArray(value),records:Array.isArray(value)?value.length:null,retType:typeof ret,retEmpty:ret===""}) + } + return value + }catch(error){ + if(path.endsWith("/get")&&typeof body["value-name"]==="string")checks.push({phase:"getter-failure",path,property:body["value-name"],nativeStatus:error instanceof MikrotikError?error.statusCode:null}) + await failure(error,"POST",path,at);throw error + } +} +MikrotikClient.prototype.put=async function(path:string,body:Record,timeout?:number,before?:()=>Promise):Promise{await pace();const at=Date.now();try{return await originalPut.call(this,path,body,timeout,()=>dispatched("PUT",path,before))as T}catch(error){await failure(error,"PUT",path,at);throw error}} +MikrotikClient.prototype.patch=async function(path:string,body:Record,timeout?:number,before?:()=>Promise):Promise{await pace();const at=Date.now();try{return await originalPatch.call(this,path,body,timeout,()=>dispatched("PATCH",path,before))as T}catch(error){await failure(error,"PATCH",path,at);throw error}} +MikrotikClient.prototype.delete=async function(path:string,timeout?:number,before?:()=>Promise):Promise{await pace();const at=Date.now();try{return await originalDelete.call(this,path,timeout,()=>dispatched("DELETE",path,before))}catch(error){await failure(error,"DELETE",path,at);throw error}} +let activeOperation:string|undefined +try{ + await save();await applySqlMigrations(pool);assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n,0) + await updateMcpSettings({enabled:true},actor) + for(const[index,host]of hosts.entries())await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)",[index+1,marker,ports?"127.0.0.1":host,ports?.[index]??80,process.env.MMAPP_TEST_CHR_USER,process.env.MMAPP_TEST_CHR_PASSWORD]) + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:marker,mode:"approval",expiresAt:new Date(Date.now()+7200000).toISOString(),permissions:["mm:network:read","mm:network:write"],scope:{serverIds:[1,2],operationIds:["network.gre.revision.restore","operations.reconcile","ros.gre.update","ros.gre.delete","ros.ipv4_address.delete","ros.ipv6_address.delete"],allowGlobal:false},limits:{callsPerMinute:1000,mutationsPerHour:500}}),actor) + const principal=await authenticateMcpToken(issued.token) + const execute=async(tool:string,input:Record)=>{ + const trace:Record={phase:"mcp-operation",tool,serverId:input.serverId??null,stage:"prepare"};checks.push(trace);await save() + const plan=await invokeMcpTool(principal,tool,input)as{planId:string;state:string} + assert.equal(plan.state,"awaiting-approval");trace.planId=plan.planId + await assert.rejects(applyPlan(principal,plan.planId,randomUUID()));await approvePlan(plan.planId,actor,true) + const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key);activeOperation=operation.id + trace.operationId=operation.id;trace.stage="running";await save() + assert.equal((await applyPlan(principal,plan.planId,key)).id,operation.id) + for(let attempt=0;attempt<480;attempt++){ + const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[operation.id])).rows[0] + if(!["pending","running"].includes(row.state)){ + activeOperation=undefined;trace.stage="terminal";trace.state=row.state;trace.errorCode=row.error_code + const steps=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index",[operation.id])).rows + trace.receipts=steps.map(row=>row.result);await save() + if(row.state!=="applied"){ + try{const {server}=await routerClient(Number(input.serverId));await readGreRestoreState(server);checks.push({phase:"failed-operation-read-diagnostic",readable:true})} + catch(error){const message=error instanceof Error?error.message:"";checks.push({phase:"failed-operation-read-diagnostic",readable:false,reason:/^(?:Неполный native-каталог|Повторяющиеся native ID|Не подтверждено |Не подтверждены |Противоречивое происхождение|Restore требует|Истёк |Превышен )/.test(message)?message.slice(0,240):"unclassified"})} + await save() + } + assert.equal(row.state,"applied",`${tool}: ${row.state}/${row.error_code}`) + return{operationId:operation.id,result:steps[0]?.result as {verified?:boolean;commandCount?:number;completedCommands?:number;revisionRecorded?:boolean;forwardingVerified?:boolean}|undefined} + } + await delay(500) + } + throw Error("Native operation remains live; do not replay or race cleanup") + } + const baselines:RosSnapshot[]=[],proofs=new Map() + // Both devices must pass before the first mutation on either one. + for(const serverId of [1,2]){ + const {server,client}=await routerClient(serverId),state=await readGreRestoreState(server) + assert.equal(state.gre.length,0,"Whole-section test refuses any foreign GRE") + proofs.set(serverId,await greRestoreStateProof(state)) + const system=await client.get>("/system/resource") + for(const resource of resources)baselines.push(await readRosSnapshot(serverId,resource)) + checks.push({phase:"preflight-device",serverId,routerOs:system.version,emptyGre:true,stateProof:proofs.get(serverId)}) + } + checks.push({phase:"preflight",baselineFingerprints:baselines.map(({serverId,resource,fingerprint})=>({serverId,resource,fingerprint})),foreignGreAbsent:true});await save() + for(const serverId of [1,2]){ + const {server,client}=await routerClient(serverId),a=marker+"-a",b=marker+"-b",a4=`198.18.251.${serverId}/30`,b4=`198.18.252.${serverId}/30`,a6=`fd00:ab:46:${serverId}::1/64` + const allowedAddresses=new Set([a4,b4,a6]),ownedIds=new Set() + const first=canonicalGreSnapshot({tunnels:[{name:a,localAddress:"",remoteAddress:"198.51.100.253",localInnerIp:a4,innerAddresses:[{address:a4,disabled:false,comment:marker},{address:a6,disabled:false,advertise:false,noDad:false,comment:marker}],remoteInnerIp:"",comment:marker,disabled:true,mtu:1400,keepalive:"10s,10",dscp:"inherit",clampTcpMss:true,allowFastPath:true,ipsecSecret:""}]}) + const second=canonicalGreSnapshot({tunnels:[{...first.tunnels[0],mtu:1390},{...first.tunnels[0],name:b,remoteAddress:"198.51.100.254",localInnerIp:b4,innerAddresses:[{address:b4,disabled:false,comment:marker}],mtu:1380}]}) + const revision=async(payload:unknown)=>{const id=randomUUID();await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,$2,'gre','copy',$3,$4::jsonb)",[id,serverId,fingerprintPayload(payload),JSON.stringify(payload)]);return id} + const aRevision=await revision(first),bRevision=await revision(second),emptyRevision=await revision({tunnels:[]}) + let attempted=false + try{ + for(const path of ["/ip/address","/ipv6/address"])assert.ok(!(await client.get[]>(path)).some(row=>allowedAddresses.has(row.address)),"Fixture address collision") + attempted=true + const created=await execute("mm_gre_revision_restore_plan",{serverId,revisionId:aRevision}) + assert.equal(created.result?.verified,true);assert.equal(created.result.commandCount,3) + let rows=await client.get[]>("/interface/gre") + assert.equal(rows.length,1);assert.equal(rows[0].name,a);assert.equal(rows[0].disabled,"true");assert.equal(rows[0].mtu,"1400");ownedIds.add(rows[0][".id"]) + assert.ok((await client.get[]>("/ip/address")).some(row=>row.interface===a&&row.address===a4)) + assert.ok((await client.get[]>("/ipv6/address")).some(row=>row.interface===a&&row.address===a6&&row.advertise!=="true")) + const beforeNoop=dispatches.length,noop=await execute("mm_gre_revision_restore_plan",{serverId,revisionId:aRevision}) + assert.equal(noop.result?.commandCount,0);assert.equal(dispatches.length,beforeNoop) + const expanded=await execute("mm_gre_revision_restore_plan",{serverId,revisionId:bRevision}) + assert.equal(expanded.result?.commandCount,3);rows=await client.get[]>("/interface/gre") + assert.equal(rows.length,2);assert.ok(rows.every(row=>[a,b].includes(row.name)&&row.disabled==="true"&&row.comment===marker)) + for(const row of rows)ownedIds.add(row[".id"]) + assert.equal(rows.find(row=>row.name===a)![".id"],[...ownedIds][0]);assert.equal(rows.find(row=>row.name===a)!.mtu,"1390") + const beforeReconcile=dispatches.length,reconciled=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:expanded.operationId})as{results:{matchesDesiredState:boolean}[]} + assert.equal(reconciled.results[0].matchesDesiredState,true);assert.equal(dispatches.length,beforeReconcile) + const restored=await execute("mm_gre_revision_restore_plan",{serverId,revisionId:aRevision}) + assert.equal(restored.result?.commandCount,3);rows=await client.get[]>("/interface/gre") + assert.equal(rows.length,1);assert.equal(rows[0].name,a);assert.equal(rows[0].mtu,"1400");assert.ok(!(await client.get[]>("/ip/address")).some(row=>row.interface===b)) + const cleared=await execute("mm_gre_revision_restore_plan",{serverId,revisionId:emptyRevision}) + assert.equal(cleared.result?.commandCount,3);assert.equal((await client.get("/interface/gre")).length,0) + assert.equal(cleared.result.revisionRecorded,true) + checks.push({phase:"revision-cycle",serverId,firstCommands:3,noOpCommands:0,expandCommands:3,restoreCommands:3,emptyCommands:3,disabledThroughout:true,dualStackConfigured:true,secondGreRemoved:true,readonlyReconcile:true,explicitEmptyPsk:true,forwardingVerified:false});await save() + }finally{ + if(activeOperation){checks.push({phase:"cleanup-refused-live-operation",serverId,operationId:activeOperation});await save();throw Error("Unfinished native operation; cleanup cannot race it")} + if(attempted){ + for(const gre of await client.get[]>("/interface/gre"))if([a,b].includes(gre.name)){ + assert.ok(gre.comment===marker||ownedIds.has(gre[".id"]));assert.equal(gre["remote-address"],gre.name===a?"198.51.100.253":"198.51.100.254") + if(gre.disabled!=="true")await execute("mm_gre_update_plan",{serverId,id:gre[".id"],fields:{disabled:true}}) + for(const[resource,path]of [["ipv4_address","/ip/address"],["ipv6_address","/ipv6/address"]])for(const address of await client.get[]>(path))if(address.interface===gre.name&&address.dynamic!=="true"){ + assert.ok(allowedAddresses.has(address.address),"Refuse an unexpected address during cleanup") + await execute(`mm_${resource}_delete_plan`,{serverId,id:address[".id"]}) + } + await execute("mm_gre_delete_plan",{serverId,id:gre[".id"]}) + } + for(const baseline of baselines.filter(row=>row.serverId===serverId))assert.equal((await readRosSnapshot(serverId,baseline.resource)).fingerprint,baseline.fingerprint,`Baseline changed: ${serverId}:${baseline.resource}`) + const afterProof=await greRestoreStateProof(await readGreRestoreState(server)) + assert.equal(afterProof,proofs.get(serverId),"Whole registered native/SQL dependency proof changed after cleanup") + checks.push({phase:"baseline-restored",serverId,fingerprints:resources.length,wholeDependencyProofRestored:true,ownedObjectsRemoved:true});await save() + } + } + console.log(`✓ CHR ${serverId}: public GRE revision create/no-op/expand/restore/empty, dual-stack configuration, readonly reconcile and original dependency proof restored`) + } + assert.equal(dispatches.length,24) + report.complete=true;checks.push({phase:"all-baselines-restored",fingerprints:baselines.length,wholeDependencyProofs:2,configurationOnly:true});await save() +}catch(error){ + checks.push({phase:"failure",name:error instanceof Error?error.name:"unknown",code:error&&typeof error==="object"&&"code"in error?String(error.code).slice(0,80):"TEST_STAGE_UNCONFIRMED"});process.exitCode=1 +}finally{ + Object.assign(report,{finishedAt:new Date().toISOString()});await save() + MikrotikClient.prototype.get=originalGet;MikrotikClient.prototype.post=originalPost;MikrotikClient.prototype.put=originalPut;MikrotikClient.prototype.patch=originalPatch;MikrotikClient.prototype.delete=originalDelete + await closePool() +} diff --git a/backend/src/modules/mcp/chr-native-failure.test.ts b/backend/src/modules/mcp/chr-native-failure.test.ts new file mode 100644 index 00000000..bf8c47b6 --- /dev/null +++ b/backend/src/modules/mcp/chr-native-failure.test.ts @@ -0,0 +1,15 @@ +import assert from "node:assert/strict" +import { MikrotikError } from "../../services/mikrotik.js" +import { chrNativeFailure } from "./chr-native-failure.js" + +const secret = "private-test-secret" +const http = chrNativeFailure(new MikrotikError(401, "/ip/address", secret), "GET", "/ip/address", Date.now()) +assert.equal(http.reason, "http"); assert.equal(http.nativeStatus, 401) +assert.equal(JSON.stringify(http).includes(secret), false) +assert.equal(chrNativeFailure(new Error(`Connection to ${secret} timed out after 10s`), "GET", "/ip/address", Date.now()).reason, "timeout") +for (const [code, reason] of [["ECONNRESET", "connection_reset"], ["ECONNREFUSED", "connection_refused"], ["EAI_AGAIN", "dns"]]) assert.equal(chrNativeFailure(Object.assign(new Error(secret), { code }), "POST", "/routing/id/get", Date.now()).reason, reason) +assert.equal(chrNativeFailure(new Error(secret), secret, "/ip/address?password=" + secret, Date.now()).path, "[unclassified]") +assert.equal(chrNativeFailure(new Error(secret), secret, secret, Date.now()).method, "unknown") +assert.equal(chrNativeFailure(new DOMException(secret, "AbortError"), "GET", "/ip/address", Date.now()).reason, "aborted") +assert.equal(http.automaticRetry, false) +console.log("CHR failure diagnostics: classified transport/HTTP metadata, private payloads excluded") diff --git a/backend/src/modules/mcp/chr-native-failure.ts b/backend/src/modules/mcp/chr-native-failure.ts new file mode 100644 index 00000000..54bc4406 --- /dev/null +++ b/backend/src/modules/mcp/chr-native-failure.ts @@ -0,0 +1,20 @@ +import { MikrotikError } from "../../services/mikrotik.js" + +/** Acceptance diagnostics only: never retain messages, bodies, headers or request arguments. */ +export function chrNativeFailure(error: unknown, method: string, path: string, startedAt: number) { + const text = error instanceof Error ? error.message : "" + const code = error instanceof Error ? (error as NodeJS.ErrnoException).code : undefined + const reason = error instanceof MikrotikError ? "http" + : code === "ECONNRESET" ? "connection_reset" + : code === "ECONNREFUSED" ? "connection_refused" + : code === "ENOTFOUND" || code === "EAI_AGAIN" ? "dns" + : code === "ETIMEDOUT" || /timed out|timeout/i.test(text) ? "timeout" + : error instanceof Error && error.name === "AbortError" ? "aborted" : "transport_unknown" + return { + phase: "native-request-failure", at: new Date().toISOString(), + method: ["GET", "POST", "PUT", "PATCH", "DELETE"].includes(method) ? method : "unknown", + path: /^\/(?:routing|ip|ipv6|interface|system|tool|ping)(?:\/[a-z0-9_.*-]+)*$/i.test(path) && path.length <= 300 ? path : "[unclassified]", + reason, ...(error instanceof MikrotikError ? { nativeStatus: error.statusCode } : {}), + elapsedMs: Math.max(0, Date.now() - startedAt), automaticRetry: false, + } +} diff --git a/backend/src/modules/mcp/chr-ospf-acceptance.ts b/backend/src/modules/mcp/chr-ospf-acceptance.ts new file mode 100644 index 00000000..8cc01524 --- /dev/null +++ b/backend/src/modules/mcp/chr-ospf-acceptance.ts @@ -0,0 +1,123 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { McpPrincipal } from "./store.js" +import { requireOspfBfdSession } from "./ospf-health.js" +import { pool } from "../../db/index.js" +import { readRoutingObservations } from "./routing-observations.js" +import { chrFixtureVrf, chrOspfForwarding } from "./chr-fixture-context.js" +import { startChrOspfForwarding } from "./chr-ospf-forwarding.js" + +type Execute = (tool: string, args: Record) => Promise +export async function cleanupChrOspf(marker: string, execute: Execute) { + assert.match(marker, /^mcp-test-[0-9a-f]{8}$/) + const errors: unknown[] = [] + for (const serverId of [1, 2]) try { + for (const resource of ["ospf_template", "ospf_area", "ospf_instance", "bfd_configuration"]) { + const own = (await readRosSnapshot(serverId, resource)).rows.filter(row => row.comment === marker) + for (const row of own) { + if (resource === "ospf_template") { + assert.ok([marker + "-gre", ...(chrOspfForwarding() ? [marker + "-bgp-loop"] : [])].includes(String(row.interfaces))) + if (row.interfaces === marker + "-bgp-loop") assert.ok([true, false, "true", "false"].includes(row.passive as string | boolean)) + assert.ok([marker + "-area-v2", marker + "-area-v3"].includes(String(row.area))) + } + if (resource === "ospf_area") assert.ok([marker + "-ospf-v2", marker + "-ospf-v3"].includes(String(row.instance))) + if (resource === "ospf_instance" || resource === "bfd_configuration") assert.equal(row.vrf, chrFixtureVrf(marker, serverId)) + if (resource === "bfd_configuration") assert.equal(row.interfaces, marker + "-gre") + await execute(`mm_${resource}_delete_plan`, { serverId, id: row[".id"] }) + } + } + } catch (error) { errors.push(error) } + if (errors.length) throw new AggregateError(errors, "Owned OSPF/BFD cleanup incomplete") +} + +/** Runs inside the owned overlay; no redistribution, main table or public OSPF. */ +export async function acceptChrOspf(marker: string, principal: McpPrincipal, execute: Execute, checks: Record[], checkpoint: () => Promise) { + const gre = marker + "-gre", vrf = chrFixtureVrf(marker, 1), versions = ["2", "3"] as const + const resources = ["ospf_template", "ospf_area", "ospf_instance", "bfd_configuration"] + const baseline = [] + const localV4: string[] = [], localV6: string[] = [] + for (const serverId of [1, 2]) { + for (const resource of resources) baseline.push(await readRosSnapshot(serverId, resource)) + const { client } = await routerClient(serverId) + const v4 = await client.get[]>("/ip/address"), v6 = await client.get[]>("/ipv6/address") + localV4.push(v4.find(row => row.interface === gre && row.comment === marker)!.address.split("/")[0]) + const linkLocal = v6.filter(row => row.interface === gre && /^fe80:/i.test(row.address)) + assert.equal(linkLocal.length, 1); localV6.push(linkLocal[0].address.split("/")[0]) + } + checks.push({ ospfPreflight: true, baselineFingerprints: baseline.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })) }); await checkpoint() + try { + for (const serverId of [1, 2]) { + const vrf = chrFixtureVrf(marker, serverId) + const peer = serverId === 1 ? 1 : 0 + for (const address of [localV4[peer] + "/32", localV6[peer] + "/128"]) await execute("mm_bfd_configuration_create_plan", { serverId, fields: { interfaces: gre, addresses: address, vrf, "min-rx": "1s", "min-tx": "1s", multiplier: 3, comment: marker } }) + for (const version of versions) { + const instance = marker + "-ospf-v" + version, area = marker + "-area-v" + version + await execute("mm_ospf_instance_create_plan", { serverId, fields: { name: instance, version, "router-id": localV4[serverId - 1], vrf, "routing-table": vrf, disabled: false, comment: marker } }) + await execute("mm_ospf_area_create_plan", { serverId, fields: { name: area, instance, "area-id": "0.0.0.0", type: "default", disabled: false, comment: marker } }) + await execute("mm_ospf_template_create_plan", { serverId, fields: { interfaces: gre, area, type: "ptp", cost: 30, "use-bfd": true, "hello-interval": "1s", "dead-interval": "4s", disabled: false, comment: marker } }) + if (chrOspfForwarding()) await execute("mm_ospf_template_create_plan", { serverId, fields: { interfaces: marker + "-bgp-loop", area, passive: true, cost: 30, disabled: false, comment: marker } }) + } + console.log(`✓ CHR ${serverId}: isolated OSPFv2/v3 and bounded BFD rules configured`) + } + for (const serverId of [1, 2]) { + const vrf = chrFixtureVrf(marker, serverId) + const { client } = await routerClient(serverId) + for (const version of versions) { + const instance = marker + "-ospf-v" + version + for (let attempt = 0; attempt < 30; attempt++) { + const neighbors = await client.getOspfNeighbors(), sessions = await client.getBfdSessions() + const matching = neighbors.filter(row => row.instance === instance && row.interface === gre && row.state.toLowerCase() === "full") + let proof: unknown = null + if (matching.length === 1) try { proof = requireOspfBfdSession(sessions as unknown as Record[], matching[0].address, gre, vrf) } catch { /* bounded observation wait; never retries a write */ } + if (proof) { + checks.push({ serverId, phase: "ospf-full-bfd-up", version, neighbor: { instance, address: matching[0].address, interface: gre, state: matching[0].state }, bfd: proof }); await checkpoint(); break + } + if (attempt === 29) { checks.push({ serverId, phase: "ospf-unconfirmed", version, neighbors, sessions }); await checkpoint(); assert.fail("OSPF Full/BFD Up not confirmed in the isolated VRF") } + await delay(2000) + } + } + } + const forwarding = chrOspfForwarding() ? await startChrOspfForwarding(marker, principal, checks, checkpoint) : null + for (const family of ["ipv4", "ipv6"] as const) { + await execute("mm_routing_measurement_plan", { measurement: { srcServerId: 1, dstServerId: 2, srcInterface: gre, dstInterface: gre, family, vrf, bandwidth: false } }) + if (process.env.MMAPP_TEST_CHR_OSPF_SOURCES === "true") { + const latest = async () => (await readRoutingObservations([1, 2])).find(row => row.family === family && row.srcInterface === gre && row.dstInterface === gre)! + assert.equal((await latest()).sourceVerified, true); assert.equal((await latest()).contextVerified, true) + // Only the disposable manager DB changes; native credentials/TLS stay untouched. + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=2") + assert.equal((await latest()).sourceVerified, false) + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=2") + assert.equal((await latest()).sourceVerified, false, "Returning to previous parameters must not revive a measurement") + await execute("mm_routing_measurement_plan", { measurement: { srcServerId: 1, dstServerId: 2, srcInterface: gre, dstInterface: gre, family, vrf, bandwidth: false } }) + const fresh = await latest() + assert.equal(fresh.sourceVerified, true); assert.equal(fresh.contextVerified, true) + checks.push({ phase: "measurement-source-epoch", family, signedFreshMeasurement: true, peerDriftRejected: true, abaRejected: true, freshMeasurementRestoresEligibility: true, pingMs: fresh.pingMs, lossPct: fresh.lossPct }); await checkpoint() + } + const operationId = await execute("mm_ospf_optimizer_plan", { serverId: 1, family, table: vrf, ecmpPaths: 1, pingWeight: 100 }) + const version = family === "ipv4" ? "2" : "3", other = family === "ipv4" ? "3" : "2" + const rows = (await readRosSnapshot(1, "ospf_template")).rows + assert.equal(rows.find(row => row.area === marker + "-area-v" + version && row.interfaces === gre)!.cost, "10") + assert.equal(rows.find(row => row.area === marker + "-area-v" + other && row.interfaces === gre)!.cost, "30", "Optimizer crossed the requested family") + if (forwarding) await forwarding.sample(family, "after-cost", -20) + await execute("mm_operation_recovery_plan", { operationId }) + if (forwarding) await forwarding.sample(family, "after-inverse", 0) + checks.push({ family, optimizerApplied: true, selectedCost: 10, foreignFamilyUntouched: true, inverseCostRestored: true, bfdHealthVerified: true }); await checkpoint() + } + if (forwarding) for (const family of ["ipv4", "ipv6"] as const) { + const version = family === "ipv4" ? "2" : "3" + const loopTemplate = (await readRosSnapshot(1, "ospf_template")).rows.find(row => row.area === marker + "-area-v" + version && row.interfaces === marker + "-bgp-loop" && row.comment === marker)! + assert.equal(loopTemplate.passive, "true") + await execute("mm_ospf_template_update_plan", { serverId: 1, id: loopTemplate[".id"], fields: { passive: false } }) + assert.equal((await readRosSnapshot(1, "ospf_template")).rows.find(row => row[".id"] === loopTemplate[".id"])!.passive, "false") + await execute("mm_ospf_template_update_plan", { serverId: 1, id: loopTemplate[".id"], fields: { passive: true } }) + assert.equal((await readRosSnapshot(1, "ospf_template")).rows.find(row => row[".id"] === loopTemplate[".id"])!.passive, "true") + await forwarding.sample(family, "after-passive-restore", 0) + checks.push({ phase: "ospf-passive-toggle", family, falseVerified: true, trueRestored: true }); await checkpoint() + } + } finally { + await cleanupChrOspf(marker, execute) + for (const snapshot of baseline) assert.equal((await readRosSnapshot(snapshot.serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "OSPF/BFD baseline changed after cleanup") + checks.push({ ospfBaselineRestored: true }); await checkpoint() + } +} diff --git a/backend/src/modules/mcp/chr-ospf-forwarding.ts b/backend/src/modules/mcp/chr-ospf-forwarding.ts new file mode 100644 index 00000000..693eee74 --- /dev/null +++ b/backend/src/modules/mcp/chr-ospf-forwarding.ts @@ -0,0 +1,50 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import type { readOspfRouteProof } from "../../services/ospf-route-proof.js" +import { chrFixtureVrf } from "./chr-fixture-context.js" +import { readRosSnapshot } from "./resources.js" +import { McpError } from "./security.js" +import { invokeMcpTool } from "./runtime.js" +import type { McpPrincipal } from "./store.js" + +export async function startChrOspfForwarding(marker: string, principal: McpPrincipal, checks: Record[], checkpoint: () => Promise) { + const gre = marker + "-gre", loop = marker + "-bgp-loop", subnet = Number.parseInt(marker.slice(-2), 16), hextet = Number.parseInt(marker.slice(-4), 16).toString(16) + const baseline = new Map() + const loopAddress = (serverId: number, family: string) => family === "ipv4" ? `198.19.${subnet}.${serverId}` : `fd6d:6370:${hextet}:2::${serverId}` + const linkAddress = (serverId: number, family: string) => family === "ipv4" ? `198.18.${subnet}.${serverId}` : `fd6d:6370:${hextet}:1::${serverId}` + const sample = async (family: "ipv4" | "ipv6", phase: string, delta: number) => { + for (const serverId of [1, 2]) { + const peerId = 3 - serverId, table = chrFixtureVrf(marker, serverId) + const prefix = loopAddress(peerId, family) + (family === "ipv4" ? "/32" : "/128"), key = serverId + ":" + family + let proof: Awaited> | undefined + for (let attempt = 0; attempt < 20; attempt++) { + try { + const result = await invokeMcpTool(principal, "mm_ospf_route_proof_get", { serverId, prefix, table, interface: gre }) as { route: Awaited> } + const observed = result.route + const expected = baseline.get(key) + if (expected !== undefined && observed.metric !== expected + (serverId === 1 ? delta : 0)) throw new Error("OSPF route metric has not converged") + proof = observed; break + } catch (error) { + if (error instanceof McpError && error.code !== "STATE_UNAVAILABLE") throw error + // Only repeat bounded reads; never repeat an optimizer/inverse write. + if (attempt === 19) { + const reason = error instanceof Error && error.message === "OSPF route metric has not converged" ? "metric-not-converged" : "native-read-unconfirmed" + checks.push({ phase: "ospf-forward-unconfirmed", serverId, family, table, prefix, stage: phase, reason }); await checkpoint() + throw new Error("Native OSPF forwarding route or metric was not confirmed") + } + await delay(1000) + } + } + assert.ok(proof) + if (!baseline.has(key)) baseline.set(key, proof.metric) + checks.push({ phase: "ospf-forward-rib", stage: phase, serverId, family, proof, expectedMetricDelta: serverId === 1 ? delta : 0 }); await checkpoint() + const ownGre = (await readRosSnapshot(serverId, "gre")).rows.find(row => row.name === gre && row.comment === marker)! + const profile = { name: marker, serverId, community: "65000:65000", family, table, probeVrf: table, candidates: [{ id: "ospf-loop", jumpHostId: peerId, exitNodeId: peerId, wanInterface: gre, downstreamInterface: gre, gateway: linkAddress(peerId, family), gatewayRef: { resource: "gre", id: ownGre[".id"] }, controlProbe: { targetInterface: loop, targetAddress: loopAddress(peerId, family), sourceAddress: linkAddress(serverId, family), expectedHops: [{ serverId: peerId, interface: loop, address: loopAddress(peerId, family) }] } }] } + const packet = await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "ospf-loop", proofPurpose: "control-path" }) as { status: string; packetCounts?: { sent: number; received: number }; trace?: { verified: boolean } } + checks.push({ phase: "ospf-forward-packets", stage: phase, serverId, family, proof: packet, sourceInterface: gre, destinationInterface: loop, returnPath: "connected-gre", independentPaths: false }); await checkpoint() + assert.equal(packet.status, "passed"); assert.deepEqual(packet.packetCounts, { sent: 3, received: 3 }); assert.equal(packet.trace?.verified, true) + } + } + await sample("ipv4", "baseline", 0); await sample("ipv6", "baseline", 0) + return { sample } +} diff --git a/backend/src/modules/mcp/chr-ospf-identity-readonly.test.ts b/backend/src/modules/mcp/chr-ospf-identity-readonly.test.ts new file mode 100644 index 00000000..bf89be7f --- /dev/null +++ b/backend/src/modules/mcp/chr-ospf-identity-readonly.test.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { MikrotikClient } from "../../services/mikrotik.js" +import { readOspfIdentityCatalog, ospfLiteralRouterId } from "../../services/ospf-identity-catalog.js" +import { readOspfLinkCatalog } from "../../services/ospf-link-catalog.js" +import { closePool } from "../../db/index.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",") +assert.deepEqual(hosts, ["194.226.121.150", "104.165.244.7"]) +const username = process.env.MMAPP_TEST_CHR_USER, password = process.env.MMAPP_TEST_CHR_PASSWORD +assert.ok(username && password) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === 2 && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +const checks: Record[] = [], report = { startedAt: new Date().toISOString(), complete: false, deviceMutations: 0, fullOspfAcceptance: false, nativeMcpAdmissionTested: false, checks } +const linkRead = process.env.MMAPP_TEST_CHR_OSPF_LINK_READONLY === "true" +try { + for (const [index, host] of hosts.entries()) { + const client = new MikrotikClient({ host: ports ? "127.0.0.1" : host, port: ports?.[index] ?? 80, username, password, useSsl: false, verifySsl: true }) + const system = await client.get>("/system/resource") + const actual = await readOspfIdentityCatalog(client, () => delay(500)) + if (linkRead) for (const family of ["ipv4", "ipv6"] as const) { + const link = await readOspfLinkCatalog(client, family, () => delay(500)) + checks.push({ host, phase: "link-source-read", family, actualOspfInstances: link.identity.instances.length, neighbors: link.neighbors.length, addresses: link.addresses.length, nativeAddressFlagsRead: true, deviceMutations: 0 }) + } + // No OSPF instance is created. Exercise the real named-ID source with a + // synthetic consumer; every routing/id request remains an actual native read. + const adapter = { + async get(path: string): Promise { + if (path === "/routing/ospf/instance") return [{ ".id": "*fixture", name: "synthetic-readonly", "router-id": "main", version: "2", vrf: "main", disabled: "false" }] as T + assert.equal(path, "/routing/id") + return client.get(path) + }, + async post(path: string, body: Record): Promise { + assert.equal(path, "/routing/id/get") + assert.ok(["disabled", "inactive"].includes(body["value-name"])) + return client.post(path, body) + }, + } + const elected = await readOspfIdentityCatalog(adapter, () => delay(500)) + assert.ok(ospfLiteralRouterId(elected.instances[0]["router-id"])) + checks.push({ host, routerOs: system.version, uptime: system.uptime, actualOspfInstances: actual.instances.length, actualCatalogRead: true, actualNamedIdSourceRead: true, syntheticOspfConsumer: true, resolvedRouterId: elected.instances[0]["router-id"], namedReference: "main", deviceMutations: 0 }) + console.log(`✓ ${host}: actual OSPF catalog and routing/id named reference read; no native writes`) + } + report.complete = true +} finally { + await writeFile(new URL(linkRead ? "../../../../docs/mcp-chr-ospf-link-readonly.json" : "../../../../docs/mcp-chr-ospf-identity-readonly.json", import.meta.url), JSON.stringify({ ...report, finishedAt: new Date().toISOString() }, null, 2) + "\n") + await closePool() +} diff --git a/backend/src/modules/mcp/chr-ospf-legacy.test.ts b/backend/src/modules/mcp/chr-ospf-legacy.test.ts new file mode 100644 index 00000000..f6155412 --- /dev/null +++ b/backend/src/modules/mcp/chr-ospf-legacy.test.ts @@ -0,0 +1,137 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient, type RosSnapshot } from "./resources.js" +import { listActions } from "./actions.js" +import { chrNativeFailure } from "./chr-native-failure.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts=(process.env.MMAPP_TEST_CHR_HOSTS??"").split(",") +assert.deepEqual(hosts,["194.226.121.150","104.165.244.7"]) +const username=process.env.MMAPP_TEST_CHR_USER,password=process.env.MMAPP_TEST_CHR_PASSWORD +assert.equal(username,"admin");assert.ok(password) +const ports=process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if(ports)assert.ok(ports.length===2&&ports.every(port=>Number.isInteger(port)&&port>=1024&&port<=65535)) +const marker="mcp-test-"+randomUUID().slice(0,8) +const resources=["ospf_template","ospf_area","ospf_instance","vrf","routing_table","bridge","bridge_port","bridge_vlan","ipv4_address","ipv6_address","ipv4_route","ipv6_route"] +const actor:AuthUser={id:"chr-legacy-ospf-owner",apps:["mm"],permissions:["mm:settings:admin","mm:network:admin"],expiresAt:Date.now()+7200000} +const checks:Record[]=[],costPatches:{path:string;at:string}[]=[] +const report={startedAt:new Date().toISOString(),marker,complete:false,scope:"Legacy OSPF public apply/restore configuration only",tools:listActions().length,forwardingVerified:false,healthVerified:false,threeNodeFailover:false,nativeRequestGapMs:500,checks,costPatches} +const reportUrl=new URL("../../../../docs/mcp-chr-ospf-legacy-acceptance.json",import.meta.url) +let queue=Promise.resolve() +const save=()=>{const value=JSON.stringify(report,null,2)+"\n";queue=queue.catch(()=>{}).then(()=>writeFile(reportUrl,value));return queue} +let pacing=Promise.resolve(),lastRequest=0 +const pace=async()=>{const next=pacing.then(async()=>{const wait=500-(Date.now()-lastRequest);if(wait>0)await delay(wait);lastRequest=Date.now()});pacing=next.catch(()=>{});await next} +const originalGet=MikrotikClient.prototype.get,originalPost=MikrotikClient.prototype.post,originalPatch=MikrotikClient.prototype.patch,originalPut=MikrotikClient.prototype.put,originalDelete=MikrotikClient.prototype.delete +const failure=async(error:unknown,method:string,path:string,started:number)=>{if(checks.filter(row=>row.phase==="native-request-failure").length<64){checks.push(chrNativeFailure(error,method,path,started));await save()}} +MikrotikClient.prototype.get=async function(path:string,timeout?:number,signal?:AbortSignal):Promise{await pace();const at=Date.now();try{return await originalGet.call(this,path,timeout,signal)as T}catch(error){await failure(error,"GET",path,at);throw error}} +MikrotikClient.prototype.post=async function(path:string,body:Record,timeout?:number,signal?:AbortSignal):Promise{await pace();const at=Date.now();try{return await originalPost.call(this,path,body,timeout,signal)as T}catch(error){await failure(error,"POST",path,at);throw error}} +MikrotikClient.prototype.patch=async function(path:string,body:Record,timeout?:number,beforeDispatch?:()=>Promise):Promise{ + await pace();const at=Date.now() + try{return await originalPatch.call(this,path,body,timeout,async()=>{await beforeDispatch?.();costPatches.push({path,at:new Date().toISOString()})})as T}catch(error){await failure(error,"PATCH",path,at);throw error} +} +MikrotikClient.prototype.put=async function(path:string,body:Record,timeout?:number,beforeDispatch?:()=>Promise):Promise{await pace();const at=Date.now();try{return await originalPut.call(this,path,body,timeout,beforeDispatch)as T}catch(error){await failure(error,"PUT",path,at);throw error}} +MikrotikClient.prototype.delete=async function(path:string,timeout?:number,beforeDispatch?:()=>Promise):Promise{await pace();const at=Date.now();try{return await originalDelete.call(this,path,timeout,beforeDispatch)}catch(error){await failure(error,"DELETE",path,at);throw error}} +let activeOperation:string|undefined +try{ + await save();await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n,0) + for(const[index,host]of hosts.entries())await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)",[index+1,marker,ports?"127.0.0.1":host,ports?.[index]??80,username,password]) + await updateMcpSettings({enabled:true},actor) + const operationIds=[...["bridge","vrf","ospf_instance","ospf_area","ospf_template"].flatMap(resource=>["create","delete"].map(action=>`ros.${resource}.${action}`)),"routing.ospf.application.optimize","routing.ospf.application.restore","operations.reconcile"] + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:marker,mode:"approval",expiresAt:new Date(Date.now()+7200000).toISOString(),permissions:["mm:network:read","mm:network:write"],scope:{serverIds:[1,2],operationIds,allowGlobal:false},limits:{callsPerMinute:1000,mutationsPerHour:500}}),actor) + const principal=await authenticateMcpToken(issued.token) + const execute=async(tool:string,args:Record)=>{ + const trace:Record={phase:"mcp-operation",tool,serverId:args.serverId??null,stage:"prepare"};checks.push(trace);await save() + const plan=await invokeMcpTool(principal,tool,args)as{planId:string;state:string} + trace.planId=plan.planId;assert.equal(plan.state,"awaiting-approval") + await assert.rejects(applyPlan(principal,plan.planId,randomUUID()));await approvePlan(plan.planId,actor,true) + const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key) + activeOperation=operation.id;trace.operationId=operation.id;trace.stage="running";await save() + assert.equal((await applyPlan(principal,plan.planId,key)).id,operation.id) + for(let attempt=0;attempt<480;attempt++){ + const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[operation.id])).rows[0] + if(!["pending","running"].includes(row.state)){ + activeOperation=undefined;trace.stage="terminal";trace.state=row.state;trace.errorCode=row.error_code;await save() + assert.equal(row.state,"applied",`MCP operation ended ${row.state}`) + return operation.id + } + await delay(500) + } + throw Error("Native operation remains live; cleanup must not race its writes") + } + const baselines:RosSnapshot[]=[] + for(const serverId of [1,2]){ + const {client}=await routerClient(serverId),system=await client.get>("/system/resource") + checks.push({phase:"system",serverId,routerOs:system.version}) + for(const resource of resources){const snapshot=await readRosSnapshot(serverId,resource);baselines.push(snapshot);assert.ok(!snapshot.rows.some(row=>row.comment===marker||String(row.name??"").startsWith(marker)))} + assert.equal(baselines.find(row=>row.serverId===serverId&&row.resource==="ospf_template")!.rows.length,0,"Refuse foreign OSPF templates before any fixture mutation") + assert.equal((await client.get("/routing/ospf/neighbor")).length,0,"Configuration-only test requires no existing neighbors") + } + checks.push({phase:"preflight",foreignTemplatesAbsent:true,neighborsAbsent:true,baselineFingerprints:baselines.map(({serverId,resource,fingerprint})=>({serverId,resource,fingerprint}))});await save() + for(const serverId of [1,2]){ + const bridges=[marker+"-a",marker+"-b"],vrf=marker+"-vrf",instance=marker+"-instance",area=marker+"-area" + let attempted=false,ownedTableId:string|undefined + try{ + attempted=true + for(const name of bridges)await execute("mm_bridge_create_plan",{serverId,fields:{name,disabled:true,comment:marker}}) + await execute("mm_vrf_create_plan",{serverId,fields:{name:vrf,interfaces:bridges.join(","),comment:marker}}) + ownedTableId=String((await readRosSnapshot(serverId,"routing_table")).rows.find(row=>row.name===vrf)?.[".id"]??"") + assert.ok(ownedTableId) + await execute("mm_ospf_instance_create_plan",{serverId,fields:{name:instance,version:"2","router-id":`198.19.253.${serverId}`,vrf,"routing-table":vrf,disabled:false,comment:marker}}) + await execute("mm_ospf_area_create_plan",{serverId,fields:{name:area,instance,"area-id":"0.0.0.0",type:"default",disabled:false,comment:marker}}) + for(const[index,name]of bridges.entries())await execute("mm_ospf_template_create_plan",{serverId,fields:{interfaces:name,area,cost:30+index*10,passive:true,disabled:false,comment:marker}}) + const current=async()=>{ + const rows=(await readRosSnapshot(serverId,"ospf_template")).rows + assert.equal(rows.length,2);assert.ok(rows.every(row=>row.comment===marker&&bridges.includes(String(row.interfaces))&&row.area===area&&row.passive==="true"&&row.disabled==="false")) + return rows.sort((a,b)=>String(a.interfaces).localeCompare(String(b.interfaces))) + } + assert.deepEqual((await current()).map(row=>Number(row.cost)),[30,40]) + const beforePatches=costPatches.length + const optimized=await execute("mm_ospf_application_optimize_plan",{serverId,pingWeight:60}) + assert.deepEqual((await current()).map(row=>Number(row.cost)),[10,20]);assert.equal(costPatches.length,beforePatches+2) + const result=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[optimized])).rows[0].result + assert.equal(result.nativeWrites,2);assert.equal(result.forwardingVerified,false) + const observation=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:optimized})as{results:{matchesDesiredState:boolean}[]} + assert.equal(observation.results[0].matchesDesiredState,true);assert.equal(costPatches.length,beforePatches+2) + await execute("mm_ospf_application_restore_plan",{operationId:optimized}) + assert.deepEqual((await current()).map(row=>Number(row.cost)),[30,40]);assert.equal(costPatches.length,beforePatches+4) + checks.push({phase:"legacy-apply-restore",serverId,initialCosts:[30,40],optimizedCosts:[10,20],restoredCosts:[30,40],costPatchDispatches:4,readonlyReconcile:true,approvedInverse:true,forwardingVerified:false});await save() + }finally{ + if(activeOperation){checks.push({phase:"cleanup-refused-live-operation",serverId,operationId:activeOperation});await save();throw Error("Unfinished native operation; do not race cleanup")} + if(attempted){ + for(const resource of ["ospf_template","ospf_area","ospf_instance","vrf","routing_table","bridge"]){ + for(const row of (await readRosSnapshot(serverId,resource)).rows.filter(row=>resource==="routing_table"?row.name===vrf:row.comment===marker)){ + if(resource==="ospf_template")assert.ok(bridges.includes(String(row.interfaces))&&row.area===area&&row.passive==="true") + if(resource==="ospf_area")assert.ok(row.name===area&&row.instance===instance) + if(resource==="ospf_instance")assert.ok(row.name===instance&&row.vrf===vrf) + if(resource==="vrf")assert.ok(row.name===vrf&&String(row.interfaces).split(",").sort().join(",")===bridges.join(",")) + if(resource==="routing_table")assert.equal(row[".id"],ownedTableId) + if(resource==="bridge")assert.ok(bridges.includes(String(row.name))&&row.disabled==="true") + await execute(`mm_${resource}_delete_plan`,{serverId,id:row[".id"]}) + } + } + for(const baseline of baselines.filter(row=>row.serverId===serverId))assert.equal((await readRosSnapshot(serverId,baseline.resource)).fingerprint,baseline.fingerprint,`Baseline changed: ${serverId}:${baseline.resource}`) + checks.push({phase:"baseline-restored",serverId,fingerprints:resources.length,ownedObjectsRemoved:true});await save() + } + } + } + assert.equal(costPatches.length,8) + for(const baseline of baselines)assert.equal((await readRosSnapshot(baseline.serverId,baseline.resource)).fingerprint,baseline.fingerprint) + report.complete=true;checks.push({phase:"all-baselines-restored",fingerprints:baselines.length,configurationOnly:true,originalIpv6ForwardingGateStillOpen:true});await save() + console.log("✓ Both CHRs: public legacy OSPF 30/40 → 10/20 → 30/40, eight cost PATCHes, approved restore, readonly reconcile and 24 baseline fingerprints restored; no forwarding claim") +}catch(error){ + checks.push({phase:"failure",name:error instanceof Error?error.name:"unknown",code:error&&typeof error==="object"&&"code"in error?String(error.code).slice(0,80):"TEST_STAGE_UNCONFIRMED"}) + process.exitCode=1 +}finally{ + Object.assign(report,{finishedAt:new Date().toISOString()});await save() + MikrotikClient.prototype.get=originalGet;MikrotikClient.prototype.post=originalPost;MikrotikClient.prototype.patch=originalPatch;MikrotikClient.prototype.put=originalPut;MikrotikClient.prototype.delete=originalDelete + await closePool() +} diff --git a/backend/src/modules/mcp/chr-ospf-passive.ts b/backend/src/modules/mcp/chr-ospf-passive.ts new file mode 100644 index 00000000..3405d276 --- /dev/null +++ b/backend/src/modules/mcp/chr-ospf-passive.ts @@ -0,0 +1,50 @@ +import assert from "node:assert/strict" +import { readRosSnapshot, routerClient } from "./resources.js" + +/** No addresses or ports; disabled templates/bridge in a separate empty VRF. */ +export async function acceptChrOspfPassive(marker: string, execute: (tool: string, args: Record) => Promise, checks: Record[], checkpoint: () => Promise) { + assert.match(marker, /^mcp-test-[a-f0-9]{8}$/) + const serverId = 1, loop = marker + "-passive-loop", vrf = marker + "-passive-vrf", resources = ["ospf_template", "ospf_area", "ospf_instance", "vrf", "routing_table", "bridge"] + const baselines = await Promise.all(resources.map(resource => readRosSnapshot(serverId, resource))) + assert.ok(baselines.every(snapshot => !snapshot.rows.some(row => row.comment === marker || typeof row.name === "string" && row.name.startsWith(marker)))) + checks.push({ phase: "passive-preflight", baselineFingerprints: baselines.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })) }); await checkpoint() + try { + await execute("mm_bridge_create_plan", { serverId, fields: { name: loop, disabled: true, comment: marker } }) + await execute("mm_vrf_create_plan", { serverId, fields: { name: vrf, interfaces: loop, comment: marker } }) + for (const version of ["2", "3"]) { + const instance = marker + "-passive-v" + version, area = marker + "-passive-area-v" + version + await execute("mm_ospf_instance_create_plan", { serverId, fields: { name: instance, version, "router-id": "198.19.254.254", vrf, "routing-table": vrf, disabled: false, comment: marker } }) + await execute("mm_ospf_area_create_plan", { serverId, fields: { name: area, instance, "area-id": "0.0.0.0", type: "default", disabled: false, comment: marker } }) + await execute("mm_ospf_template_create_plan", { serverId, fields: { interfaces: loop, area, cost: 30, passive: false, disabled: true, comment: marker } }) + const own = () => readRosSnapshot(serverId, "ospf_template").then(snapshot => snapshot.rows.find(row => row.comment === marker && row.interfaces === loop && row.area === area)!) + let row = await own(); assert.equal(row.passive, "false") + await execute("mm_ospf_template_update_plan", { serverId, id: row[".id"], fields: { passive: true, cost: 40 } }) + row = await own(); assert.equal(row.passive, "true"); assert.equal(row.cost, "40") + try { + const operationId = await execute("mm_ospf_template_update_plan", { serverId, id: row[".id"], fields: { passive: false, cost: 30 } }) + row = await own(); assert.equal(row.passive, "false"); assert.equal(row.cost, "30") + await execute("mm_operation_recovery_plan", { operationId }) + row = await own(); assert.equal(row.passive, "true"); assert.equal(row.cost, "40") + checks.push({ phase: "passive-switch-verified", version, createFalse: true, setTrue: true, unsetFalse: true, combinedFieldsSeparateIntents: true, inverseRestored: true }); await checkpoint() + } finally { + const { client } = await routerClient(serverId) + const native = (await client.get[]>("/routing/ospf/interface-template")).filter(item => item.comment === marker && item.interfaces === loop && item.area === area) + checks.push({ phase: "passive-native-readback", version, rows: native.map(item => ({ id: item[".id"], passivePresent: Object.hasOwn(item, "passive"), passive: item.passive ?? null, cost: item.cost, disabled: item.disabled })) }); await checkpoint() + } + } + } finally { + for (const resource of resources) { + for (const row of (await readRosSnapshot(serverId, resource)).rows.filter(row => row.comment === marker)) { + if (resource === "ospf_template") assert.ok(row.interfaces === loop && ["2", "3"].some(version => row.area === marker + "-passive-area-v" + version)) + if (resource === "ospf_area") assert.ok(["2", "3"].some(version => row.instance === marker + "-passive-v" + version)) + if (resource === "ospf_instance") assert.ok(row.vrf === vrf && ["2", "3"].some(version => row.name === marker + "-passive-v" + version)) + if (resource === "vrf") assert.ok(row.name === vrf && String(row.interfaces) === loop) + if (resource === "routing_table") assert.equal(row.name, vrf) + if (resource === "bridge") assert.ok(row.name === loop && row.disabled === "true") + await execute(`mm_${resource}_delete_plan`, { serverId, id: row[".id"] }) + } + } + for (const baseline of baselines) assert.equal((await readRosSnapshot(serverId, baseline.resource)).fingerprint, baseline.fingerprint) + checks.push({ phase: "passive-baseline-restored", fingerprints: baselines.length, noAddressesOrPortsCreated: true }); await checkpoint() + } +} diff --git a/backend/src/modules/mcp/chr-ospf-prefix-acceptance.test.ts b/backend/src/modules/mcp/chr-ospf-prefix-acceptance.test.ts new file mode 100644 index 00000000..66f38259 --- /dev/null +++ b/backend/src/modules/mcp/chr-ospf-prefix-acceptance.test.ts @@ -0,0 +1,9 @@ +import assert from "node:assert/strict" +import { hasExactOspfLsaPrefix } from "./chr-ospf-prefix-acceptance.js" + +const prefix = "fd6d:6370:abcd:3::/64" +assert.ok(hasExactOspfLsaPrefix("prefixes=[FD6D:6370:ABCD:3:0:0:0:0/64, fd00::/48]", prefix)) +assert.ok(hasExactOspfLsaPrefix("prefix=fd6d:6370:abcd:3::/64\nmetric=10", prefix)) +for (const body of ["fd6d:6370:abcd:3::/65", "fd6d:6370:abcd:4::/64", "fd6d:6370:abcd:3::", "fd6d:6370:abcd:3::/64junk", "opaque data"]) { + assert.equal(hasExactOspfLsaPrefix(body, prefix), false) +} diff --git a/backend/src/modules/mcp/chr-ospf-prefix-acceptance.ts b/backend/src/modules/mcp/chr-ospf-prefix-acceptance.ts new file mode 100644 index 00000000..58dc3e29 --- /dev/null +++ b/backend/src/modules/mcp/chr-ospf-prefix-acceptance.ts @@ -0,0 +1,101 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import { canonicalOspfRouteProofPrefix } from "../../services/ospf-route-proof.js" +import { observedBoolean } from "../../services/ospf-observed-values.js" +import { chrFixtureVrf } from "./chr-fixture-context.js" +import { cleanupChrOspf } from "./chr-ospf-acceptance.js" +import { readRosSnapshot, routerClient, type RosSnapshot } from "./resources.js" +import { invokeMcpTool } from "./runtime.js" +import { fingerprint, McpError } from "./security.js" +import type { McpPrincipal } from "./store.js" + +type Execute = (tool: string, args: Record) => Promise +const prefixFor = (marker: string) => `fd6d:6370:${Number.parseInt(marker.slice(-4), 16).toString(16)}:3::/64` +const lsaPath = "/routing/ospf/lsa?.proplist=instance,type,originator,id,sequence,flushing,body" + +export function hasExactOspfLsaPrefix(body: string, prefix: string) { + return [...body.matchAll(/(?:^|[^\w:%])([a-f0-9:]+\/\d+)(?![\w:%/])/gi)] + .some(match => canonicalOspfRouteProofPrefix(match[1]) === prefix) +} + +export async function readChrOspfPrefixSnapshot(serverId: number, resource: string): Promise> { + if (resource !== "ipv6_address_list") return readRosSnapshot(serverId, resource) + const { client } = await routerClient(serverId) + const rows = await client.get[]>("/ipv6/firewall/address-list?.proplist=.id,list,address,comment,disabled,dynamic", 10_000, undefined, 1_048_576) + assert.ok(Array.isArray(rows) && rows.length <= 1000 && rows.every(row => row && typeof row.list === "string" && typeof row.address === "string" && typeof row[".id"] === "string" && /^\*[a-z0-9]+$/i.test(row[".id"]))) + assert.equal(new Set(rows.map(row => row[".id"])).size, rows.length) + const projected = rows.map(row => ({ ".id": String(row[".id"]), list: row.list, address: row.address, comment: row.comment, disabled: row.disabled, dynamic: row.dynamic })) + .sort((left, right) => left[".id"].localeCompare(right[".id"])) + return { serverId, resource, rows: projected, fingerprint: fingerprint(projected) } +} + +export async function cleanupChrOspfPrefix(marker: string, execute: Execute) { + await cleanupChrOspf(marker, execute) + const { rows } = await readChrOspfPrefixSnapshot(1, "ipv6_address_list") + const owned = rows.filter(row => row.list === marker) + assert.ok(owned.length <= 1 && owned.every(row => row.comment === marker && canonicalOspfRouteProofPrefix(row.address) === prefixFor(marker) && typeof row[".id"] === "string"), "Prefix cleanup refuses a foreign entry") + for (const row of owned) await execute("mm_firewall_address_list_delete_plan", { serverId: 1, address: { operation: "delete", family: "ip6", rosId: row[".id"] } }) +} + +export async function acceptChrOspfPrefix(marker: string, principal: McpPrincipal, execute: Execute, checks: Record[], checkpoint: () => Promise) { + const gre = marker + "-gre", instance = marker + "-ospf-v3", area = marker + "-area-v3", prefix = prefixFor(marker) + const routerIds = [1, 2].map(id => `198.18.${Number.parseInt(marker.slice(-2), 16)}.${id}`) + const baseline = [] + for (const serverId of [1, 2]) for (const resource of ["ospf_instance", "ospf_area", "ospf_template", "ipv6_address_list"]) { + const snapshot = await readChrOspfPrefixSnapshot(serverId, resource) + assert.ok(!snapshot.rows.some(row => row.comment === marker || row.list === marker || canonicalOspfRouteProofPrefix(row.address) === prefix || row.name === instance || row.name === area || routerIds.includes(String(row["router-id"]))), "Prefix fixture overlaps existing configuration") + baseline.push(snapshot) + } + checks.push({ phase: "ospf-prefix-preflight", ospfPrefixPreflight: true, prefix, baselineFingerprints: baseline.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })) }) + await checkpoint() + try { + for (const serverId of [1, 2]) { + const vrf = chrFixtureVrf(marker, serverId) + await execute("mm_ospf_instance_create_plan", { serverId, fields: { name: instance, version: "3", "router-id": routerIds[serverId - 1], vrf, "routing-table": vrf, disabled: false, comment: marker } }) + await execute("mm_ospf_area_create_plan", { serverId, fields: { name: area, instance, "area-id": "0.0.0.0", type: "default", disabled: false, comment: marker } }) + await execute("mm_ospf_template_create_plan", { serverId, fields: { interfaces: gre, area, type: "ptp", "hello-interval": "1s", "dead-interval": "4s", disabled: false, comment: marker } }) + } + await execute("mm_firewall_address_list_create_plan", { serverId: 1, address: { operation: "create", family: "ip6", fields: { list: marker, address: prefix, comment: marker } } }) + const templates = (await readRosSnapshot(1, "ospf_template")).rows.filter(row => row.comment === marker && row.area === area && row.interfaces === gre) + assert.equal(templates.length, 1) + const id = templates[0][".id"] + assert.ok(typeof id === "string" && /^\*[a-z0-9]+$/i.test(id)) + const { client } = await routerClient(2) + const sample = async (stage: string, present: boolean) => { + let last: Record = {} + for (let attempt = 0; attempt < 15; attempt++) { + const neighbors = await client.getOspfNeighbors() + const full = neighbors.filter(row => row.instance === instance && row.interface === gre && row["router-id"] === routerIds[0] && row.state?.toLowerCase() === "full").length === 1 + if (full) { + const rows = await client.get[]>(lsaPath, 10_000, undefined, 1_048_576) + assert.ok(Array.isArray(rows) && rows.length <= 10000 && rows.every(row => row && typeof row === "object" && typeof row.instance === "string" && typeof row.originator === "string")) + const eligible = rows.filter(row => row.instance === instance && row.originator === routerIds[0] && observedBoolean(row.flushing) !== true) + const complete = eligible.length > 0 && eligible.every(row => typeof row.body === "string" && row.body.trim() !== "") + const lsa = eligible.filter(row => typeof row.body === "string" && hasExactOspfLsaPrefix(row.body, prefix)).map(row => ({ id: row.id, type: row.type, sequence: row.sequence })) + const route = await invokeMcpTool(principal, "mm_ospf_route_proof_get", { serverId: 2, prefix, table: chrFixtureVrf(marker, 2), interface: gre, expectAbsent: !present }).catch(error => { + if (error instanceof McpError && error.code === "STATE_UNAVAILABLE") return null + throw error + }) + last = { full, lsa, complete, route } + if (present ? lsa.length > 0 && route !== null : complete && lsa.length === 0 && route !== null) { + checks.push({ phase: "ospf-prefix-observation", stage, ...last }); await checkpoint() + return + } + } else last = { full } + if (attempt < 14) await delay(1000) + } + checks.push({ phase: "ospf-prefix-unconfirmed", stage, ...last }); await checkpoint() + assert.fail(`OSPF prefix ${stage} did not converge on the receiver`) + } + await sample("baseline", false) + for (const [stage, value] of [["advertised", marker], ["withdrawn", ""], ["readvertised", marker]]) { + const operationId = await execute("mm_ospf_template_update_plan", { serverId: 1, id, fields: { "prefix-list": value } }) + checks.push({ phase: "ospf-prefix-plan-applied", stage, operationId }); await checkpoint() + await sample(stage, value !== "") + } + } finally { + await cleanupChrOspfPrefix(marker, execute) + for (const snapshot of baseline) assert.equal((await readChrOspfPrefixSnapshot(snapshot.serverId, snapshot.resource)).fingerprint, snapshot.fingerprint) + checks.push({ phase: "ospf-prefix-baseline-restored" }); await checkpoint() + } +} diff --git a/backend/src/modules/mcp/chr-overlay-acceptance.ts b/backend/src/modules/mcp/chr-overlay-acceptance.ts new file mode 100644 index 00000000..bd0b659e --- /dev/null +++ b/backend/src/modules/mcp/chr-overlay-acceptance.ts @@ -0,0 +1,181 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import { invokeMcpTool } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { McpPrincipal } from "./store.js" +import { acceptChrOspf } from "./chr-ospf-acceptance.js" +import { acceptChrOspfPrefix } from "./chr-ospf-prefix-acceptance.js" +import { acceptChrEcmp } from "./chr-ecmp-acceptance.js" +import { acceptChrBgp } from "./chr-bgp-acceptance.js" +import { chrFixtureVrf, chrOspfForwarding, chrOspfPrefix } from "./chr-fixture-context.js" +import { waitForChrOverlayReadiness,readChrIpv6ReadinessContext,type ChrOverlayProof } from "./chr-overlay-readiness.js" + +type Execute = (tool: string, args: Record) => Promise + +export async function cleanupChrOverlay(hosts: string[], marker: string, execute: Execute) { + assert.match(marker, /^mcp-test-[0-9a-f]{8}$/) + const gre = marker + "-gre", loop = marker + "-bgp-loop", errors: unknown[] = [] + for (const [index] of hosts.entries()) { + const serverId = index + 1, vrf = chrFixtureVrf(marker, serverId) + try { + for (const resource of ["ipv6_address", "ipv4_address", "vrf", "gre", "bridge"]) { + const rows = (await readRosSnapshot(serverId, resource)).rows + const own = rows.filter(row => row.comment === marker && (resource.endsWith("address") ? [gre, loop].includes(String(row.interface)) : row.name === (resource === "vrf" ? vrf : resource === "bridge" ? loop : gre))) + for (const row of own) { + if (resource.endsWith("address")) assert.ok((row.interface === gre ? [`198.18.${Number.parseInt(marker.slice(-2), 16)}.${serverId}/30`, `fd6d:6370:${Number.parseInt(marker.slice(-4), 16).toString(16)}:1::${serverId}/64`] : [`198.19.${Number.parseInt(marker.slice(-2), 16)}.${serverId}/32`, `fd6d:6370:${Number.parseInt(marker.slice(-4), 16).toString(16)}:2::${serverId}/128`]).includes(String(row.address)), "Cleanup refuses an unexpected address") + if (resource === "vrf") assert.ok([gre, [gre, loop].sort().join(",")].includes(String(row.interfaces).split(",").sort().join(",")), "Cleanup refuses a VRF with foreign members") + if (resource === "gre") { assert.equal(row["local-address"], hosts[index]); assert.equal(row["remote-address"], hosts[1 - index]) } + await execute(`mm_${resource}_delete_plan`, { serverId, id: row[".id"] }) + } + } + } catch (error) { errors.push(error) } + } + if (errors.length) throw new AggregateError(errors, "Owned overlay cleanup incomplete; inspect the recorded marker before further writes") +} + +/** Mutates only a unique owned GRE/VRF fixture on the caller's allowlisted CHRs. */ +export async function acceptChrOverlay(hosts: string[], marker: string, principal: McpPrincipal, execute: Execute, checks: Record[], checkpoint: () => Promise) { + assert.equal(hosts.length, 2, "Overlay acceptance requires both allowlisted CHRs") + const gre = marker + "-gre", loop = marker + "-bgp-loop" + const bgpExport = process.env.MMAPP_TEST_CHR_BGP_EXPORT === "true" + const ownedLoop = bgpExport || chrOspfForwarding() + const subnet = Number.parseInt(marker.slice(-2), 16) + const hextet = Number.parseInt(marker.slice(-4), 16).toString(16) + const ipv4 = [`198.18.${subnet}.1`, `198.18.${subnet}.2`] + const ipv6 = [`fd6d:6370:${hextet}:1::1`, `fd6d:6370:${hextet}:1::2`] + const resources = ["gre", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table"] + if (process.env.MMAPP_TEST_CHR_BGP === "true") resources.push("bgp_instance", "bgp_connection") + if (ownedLoop) resources.push("bridge") + if (process.env.MMAPP_TEST_CHR_BGP_FILTER_BINDING === "true") resources.push("routing_filter") + const baseline = [] + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, vrf = chrFixtureVrf(marker, serverId), { client } = await routerClient(serverId) + for (const resource of resources) baseline.push(await readRosSnapshot(serverId, resource)) + const rows = baseline.filter(snapshot => snapshot.serverId === serverId).flatMap(snapshot => snapshot.rows) + assert.ok(!rows.some(row => row.name === gre || row.name === vrf || row.name === loop || row.comment === marker)) + assert.ok(!rows.some(row => row["local-address"] === host && row["remote-address"] === hosts[1 - index]), "Existing GRE endpoints must not be reused") + assert.ok(!rows.some(row => String(row.address ?? "").startsWith(`198.18.${subnet}.`) || ownedLoop && String(row.address ?? "").startsWith(`198.19.${subnet}.`) || String(row.address ?? "").startsWith(`fd6d:6370:${hextet}:`)), "Fixture subnets already in use") + const wan = await client.get[]>("/ip/address") + assert.ok(wan.some(row => row.interface === "ether1" && row.address.split("/")[0] === host && row.disabled !== "true")) + const settings = await client.get>("/ipv6/settings") + assert.equal(settings["disable-ipv6"], "false", "IPv6 must already be enabled; acceptance never changes global settings") + } + checks.push({ overlayPreflight: true, baselineFingerprints: baseline.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })) }) + await checkpoint() // Persist ownership and baseline before the first external write. + let attempted = false + try { + attempted = true + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, vrf = chrFixtureVrf(marker, serverId) + await execute("mm_gre_create_plan", { serverId, fields: { name: gre, "local-address": host, "remote-address": hosts[1 - index], disabled: true, comment: marker } }) + if (ownedLoop) await execute("mm_bridge_create_plan", { serverId, fields: { name: loop, "protocol-mode": "none", comment: marker, ...(chrOspfForwarding() ? { disabled: true } : {}) } }) + await execute("mm_vrf_create_plan", { serverId, fields: { name: vrf, interfaces: ownedLoop ? [gre, loop].sort().join(",") : gre, comment: marker } }) + const ownVrf = (await readRosSnapshot(serverId, "vrf")).rows + assert.equal(ownVrf[0].name, vrf, "New VRF must precede main and existing VRFs") + if (ownedLoop) { + const { client } = await routerClient(serverId), native = (await client.get[]>("/ip/vrf")).find(row => row.name === vrf) + checks.push({ serverId, phase: "vrf-membership-readback", nativeMembers: native?.interfaces ?? null, canonicalMembers: ownVrf[0].interfaces }); await checkpoint() + } + await execute("mm_ipv4_address_create_plan", { serverId, fields: { address: ipv4[index] + "/30", interface: gre, comment: marker } }) + await execute("mm_ipv6_address_create_plan", { serverId, fields: { address: ipv6[index] + "/64", interface: gre, advertise: false, comment: marker } }) + if (ownedLoop) { + await execute("mm_ipv4_address_create_plan", { serverId, fields: { address: `198.19.${subnet}.${serverId}/32`, interface: loop, comment: marker } }) + await execute("mm_ipv6_address_create_plan", { serverId, fields: { address: `fd6d:6370:${hextet}:2::${serverId}/128`, interface: loop, advertise: false, comment: marker } }) + } + if (chrOspfForwarding()) { + const ownLoop = (await readRosSnapshot(serverId, "bridge")).rows.find(row => row.name === loop && row.comment === marker)! + assert.equal(ownLoop.disabled, "true") + await execute("mm_bridge_update_plan", { serverId, id: ownLoop[".id"], fields: { disabled: false } }) + } + const ownGre = (await readRosSnapshot(serverId, "gre")).rows.find(row => row.name === gre && row.comment === marker)! + await assert.rejects(invokeMcpTool(principal, "mm_gre_delete_plan", { serverId, id: ownGre[".id"] }), error => error instanceof Error && (error as { statusCode?: number }).statusCode === 409 && /используется/i.test(error.message)) + await execute("mm_gre_update_plan", { serverId, id: ownGre[".id"], fields: { disabled: false } }) + console.log(`✓ ${host}: owned GRE/VRF and dual-stack addresses configured`) + } + if (chrOspfForwarding()) for (const serverId of [1, 2]) { + const { client } = await routerClient(serverId), vrf = chrFixtureVrf(marker, serverId) + const owned = (await client.get[]>("/ipv6/address")).filter(row => row.comment === marker && [gre, loop].includes(row.interface)) + checks.push({ phase: "ospf-forward-address-context", serverId, vrf, addresses: owned.map(row => ({ id: row[".id"], interface: row.interface, address: row.address, vrf: row.vrf ?? null })) }); await checkpoint() + assert.equal(owned.length, 2) + assert.ok(owned.every(row => row.vrf === undefined || row.vrf === vrf), "Owned IPv6 address escaped its local VRF") + } + for (const serverId of [1, 2]) { + const vrf = chrFixtureVrf(marker, serverId), { client } = await routerClient(serverId) + for (let attempt = 0; attempt < 15; attempt++) { + const rows = await client.get[]>("/interface/gre") + if (rows.some(row => row.name === gre && row.running === "true")) break + assert.ok(attempt < 14, "GRE did not become running; no firewall or underlay changes are allowed") + await delay(1000) + } + // Connected addresses must resolve exclusively in the owned VRF, never main. + for (const resource of ["ipv4_route", "ipv6_route"]) { + const rows = (await readRosSnapshot(serverId, resource)).rows + const prefix = resource === "ipv4_route" ? `198.18.${subnet}.0/30` : `fd6d:6370:${hextet}:1::/64` + const connected = rows.filter(row => row["dst-address"] === prefix) + assert.ok(connected.length > 0 && connected.every(row => row["routing-table"] === vrf), "Connected overlay route escaped its VRF") + } + } + if (chrOspfPrefix()) { + await acceptChrOspfPrefix(marker, principal, execute, checks, checkpoint) + return + } + const verify = async (serverId: number, family: "ipv4" | "ipv6") => { + const vrf = chrFixtureVrf(marker, serverId), peerId = serverId === 1 ? 2 : 1, addresses = family === "ipv4" ? ipv4 : ipv6 + const ownGre = (await readRosSnapshot(serverId, "gre")).rows.find(row => row.name === gre && row.comment === marker)! + const profile = { name: marker, serverId, community: "65000:65000", family, table: vrf, probeVrf: vrf, candidates: [{ id: "overlay", jumpHostId: peerId, exitNodeId: peerId, wanInterface: gre, downstreamInterface: gre, gateway: addresses[peerId - 1], gatewayRef: { resource: "gre", id: ownGre[".id"] }, controlProbe: { targetInterface: gre, targetAddress: addresses[peerId - 1], sourceAddress: addresses[serverId - 1], expectedHops: [{ serverId: peerId, interface: gre, address: addresses[peerId - 1] }] } }] } + return await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "overlay", proofPurpose: "control-path" }) as ChrOverlayProof + } + for (const serverId of [1, 2]) for (const family of ["ipv4", "ipv6"] as const) { + const {client}=await routerClient(serverId) + const proof = await waitForChrOverlayReadiness(async()=>{ + if(family==="ipv6"){checks.push({phase:"overlay-ipv6-readiness-context",serverId,stage:"before-probe",context:await readChrIpv6ReadinessContext(client,marker,gre)});await checkpoint()} + const observed=await verify(serverId,family) + if(family==="ipv6"){checks.push({phase:"overlay-ipv6-readiness-context",serverId,stage:"after-probe",context:await readChrIpv6ReadinessContext(client,marker,gre)});await checkpoint()} + return observed + },async event=>{checks.push({phase:"overlay-readiness-probe",serverId,family,...event,configurationWritesRetried:false});await checkpoint()}) + checks.push({ host: hosts[serverId - 1], family, phase: "overlay-up", proof }) + await checkpoint() + assert.equal(proof.status, "passed", JSON.stringify(proof)) + console.log(`✓ ${hosts[serverId - 1]}: ${family} GRE/VRF ping and traceroute`) + } + if (process.env.MMAPP_TEST_CHR_OSPF === "true") await acceptChrOspf(marker, principal, execute, checks, checkpoint) + if (process.env.MMAPP_TEST_CHR_ECMP === "true") await acceptChrEcmp(marker, principal, execute, checks, checkpoint) + if (process.env.MMAPP_TEST_CHR_BGP === "true") await acceptChrBgp(marker, principal, execute, checks, checkpoint) + const peerGre = (await readRosSnapshot(2, "gre")).rows.find(row => row.name === gre && row.comment === marker)! + assert.equal(peerGre.disabled, "false") + const faultOperation = await execute("mm_gre_update_plan", { serverId: 2, id: peerGre[".id"], fields: { disabled: true } }) + for (const family of ["ipv4", "ipv6"] as const) { + const proof = await verify(1, family) + checks.push({ host: hosts[0], family, phase: "peer-disabled", proof }) + await checkpoint() + assert.equal(proof.status, "failed", "A lab fault must have explicit loss evidence, not an unknown command result") + } + for (const serverId of [1, 2]) { + const { client } = await routerClient(serverId) + assert.ok((await client.get>("/system/resource")).version) + } + // GRE updates now use the shared service executor. Restore the known prior + // flag through a fresh human-approved service plan, not the ROS-only inverse. + const recoveryOperation = await execute("mm_gre_update_plan", { serverId: 2, id: peerGre[".id"], fields: { disabled: false } }) + checks.push({ overlayRecovery: true, faultOperation, recoveryOperation, separateHumanPlan: true }); await checkpoint() + for (const family of ["ipv4", "ipv6"] as const) { + const proof = await verify(1, family) + checks.push({ host: hosts[0], family, phase: "recovered", proof }) + await checkpoint() + assert.equal(proof.status, "passed", JSON.stringify(proof)) + } + checks.push({ overlayTrafficVerified: true, explicitLinkFailureVerified: true, approvedInverseRecoveryVerified: true, managementSurvivedFault: true, threeNodeFailover: false }) + } catch (error) { + checks.push({ overlayFailure: error instanceof Error ? { name: error.name, message: error.message.slice(0, 500), code: "code" in error ? error.code : undefined } : { name: "unknown" } }) + await checkpoint() + throw error + } finally { + if (attempted) { + await cleanupChrOverlay(hosts, marker, execute) + for (const snapshot of baseline) assert.equal((await readRosSnapshot(snapshot.serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, `Overlay cleanup changed ${snapshot.serverId}:${snapshot.resource}`) + checks.push({ overlayBaselineRestored: true, resources, marker }) + await checkpoint() + console.log("✓ Both CHRs: owned overlay removed, all configuration baselines restored") + } + } +} diff --git a/backend/src/modules/mcp/chr-overlay-readiness.test.ts b/backend/src/modules/mcp/chr-overlay-readiness.test.ts new file mode 100644 index 00000000..4e3aabec --- /dev/null +++ b/backend/src/modules/mcp/chr-overlay-readiness.test.ts @@ -0,0 +1,28 @@ +import assert from "node:assert/strict" +import { waitForChrOverlayReadiness,readChrIpv6ReadinessContext,type ChrOverlayProof } from "./chr-overlay-readiness.js" +import { MikrotikError } from "../../services/mikrotik.js" +const good:ChrOverlayProof={status:"passed",packetCounts:{sent:3,received:3},trace:{verified:true,completeTrace:true}} +const partial:ChrOverlayProof={status:"unknown",reason:"PING_PARTIAL_LOSS",packetCounts:{sent:3,received:2}} +async function run(proofs:ChrOverlayProof[]){ + let calls=0,waits=0;const recorded:{attempt:number;consecutive:number;ready:boolean;proof:ChrOverlayProof}[]=[] + const pending=waitForChrOverlayReadiness(async()=>{calls++;return proofs[Math.min(calls-1,proofs.length-1)]},async event=>{recorded.push(event)},async()=>{waits++}) + return {pending,recorded,counts:()=>({calls,waits})} +} +const stable=await run([good]);await stable.pending;assert.deepEqual(stable.counts(),{calls:2,waits:1}) +const cold=await run([partial,good,good]);await cold.pending;assert.equal(cold.recorded[0].proof.status,"unknown");assert.equal(cold.recorded[0].ready,false);assert.deepEqual(cold.counts(),{calls:3,waits:2}) +const intermittent=await run([good,partial,good,partial]);await assert.rejects(intermittent.pending,/four observations/);assert.deepEqual(intermittent.counts(),{calls:4,waits:3}) +for(const bad of [{status:"failed",packetCounts:{sent:3,received:0}},{status:"unknown",reason:"COMMAND_UNCONFIRMED"},{...good,packetCounts:{sent:2,received:2}},{...good,trace:{verified:true,completeTrace:false}},{...partial,packetCounts:{sent:4,received:3}}]){ + const one=await run([bad]);await assert.rejects(one.pending,/no retry/);assert.equal(one.counts().calls,1) +} +let failures=0 +await assert.rejects(waitForChrOverlayReadiness(async()=>{failures++;throw Error("native read failed")},async()=>{},async()=>{}));assert.equal(failures,1) +const marker="mcp-test-1234abcd",gre=marker+"-gre",paths:string[]=[] +const client={get:async(path:string):Promise=>{paths.push(path);return(path==="/ipv6/address"?[{interface:gre,comment:marker,address:"fd00::1/64","auth-key":"NEVER_READINESS_KEY"},{interface:"foreign",comment:"foreign",address:"NEVER_FOREIGN"}]:[{interface:gre,address:"fd00::2",status:"incomplete","mac-address":"NEVER_MAC"},{interface:"foreign",address:"NEVER_FOREIGN"}])as T}} +const context=await readChrIpv6ReadinessContext(client,marker,gre) +assert.deepEqual(paths,["/ipv6/address","/ipv6/neighbor"]);assert.equal(context.addresses[0].invalid,null);assert.equal(context.neighbors![0].status,"incomplete") +assert.equal(context.causeOfPacketLossVerified,false);assert.ok(!JSON.stringify(context).includes("NEVER_")) +const unsupported=await readChrIpv6ReadinessContext({get:async(path:string):Promise=>{if(path==="/ipv6/neighbor")throw new MikrotikError(404,path,"unsupported");return[]as T}},marker,gre) +assert.equal(unsupported.neighborCacheObserved,false);assert.equal(unsupported.neighbors,null) +await assert.rejects(readChrIpv6ReadinessContext({get:async():Promise=>{throw new MikrotikError(401,"/ipv6/neighbor","refused")}},marker,gre)) +await assert.rejects(readChrIpv6ReadinessContext(client,marker,"foreign"));assert.equal(paths.length,2) +console.log("CHR overlay readiness: two consecutive 3/3 complete traces, preserved initial partial, strict four-read bound, no retry for failure/malformed/unconfirmed/transport, scoped IPv6 diagnostics and unknown flags; no configuration writes") diff --git a/backend/src/modules/mcp/chr-overlay-readiness.ts b/backend/src/modules/mcp/chr-overlay-readiness.ts new file mode 100644 index 00000000..7691fb39 --- /dev/null +++ b/backend/src/modules/mcp/chr-overlay-readiness.ts @@ -0,0 +1,52 @@ +import { setTimeout as delay } from "node:timers/promises" +import { MikrotikError, type MikrotikClient } from "../../services/mikrotik.js" +import { isIP } from "node:net" +import { observedBoolean } from "../../services/ospf-observed-values.js" + +export interface ChrOverlayProof { + status:string;reason?:string;packetCounts?:{sent:number;received:number}; + trace?:{verified?:boolean;completeTrace?:boolean};[key:string]:unknown; +} +/** Fixture bring-up only. Repeats bounded observations, never a configuration mutation. */ +export async function waitForChrOverlayReadiness

(verify:()=>Promise

,record:(event:{attempt:number;consecutive:number;ready:boolean;proof:P})=>Promise,wait:()=>Promise=()=>delay(1000)){ + let consecutive=0 + for(let attempt=1;attempt<=4;attempt++){ + const proof=await verify() + const passed=proof.status==="passed"&&proof.packetCounts?.sent===3&&proof.packetCounts.received===3&&proof.trace?.verified===true&&proof.trace.completeTrace===true + const startupPartial=proof.status==="unknown"&&proof.reason==="PING_PARTIAL_LOSS"&&proof.packetCounts?.sent===3&&[1,2].includes(proof.packetCounts.received) + consecutive=passed?consecutive+1:0 + const ready=consecutive===2 + await record({attempt,consecutive,ready,proof}) + if(ready)return proof + if(!passed&&!startupPartial)throw Error("Overlay readiness has an unconfirmed/failed proof; no retry is allowed") + if(attempt<4)await wait() + } + throw Error("Overlay did not confirm two consecutive 3/3 complete paths within four observations") +} + +/** Diagnostic projection of /ipv6/neighbor, documented by MikroTik IPv6 Neighbor Discovery. + * Missing flags stay null. Cache observations do not attest a cause of packet loss. */ +export async function readChrIpv6ReadinessContext(client:Pick,marker:string,gre:string){ + if(!/^mcp-test-[a-f0-9]{8}$/.test(marker)||gre!==marker+"-gre")throw Error("Unowned readiness interface") + const read=async(path:string)=>{ + const rows=await client.get(path) + if(!Array.isArray(rows)||rows.length>2000||Buffer.byteLength(JSON.stringify(rows))>1048576||rows.some(row=>!row||typeof row!=="object"||Array.isArray(row)))throw Error("Unconfirmed bounded IPv6 readiness catalog") + return rows as Record[] + } + const addresses=(await read("/ipv6/address")).filter(row=>row.comment===marker&&row.interface===gre) + let neighbors:Record[]|null=null + try{neighbors=(await read("/ipv6/neighbor")).filter(row=>row.interface===gre)}catch(error){if(!(error instanceof MikrotikError)||error.statusCode!==404)throw error} + const project=(rows:Record[],fields:string[])=>rows.map(row=>Object.fromEntries(fields.map(key=>{ + const value=row[key] + if(value===undefined||value===null)return[key,null] + if(["disabled","invalid","tentative","router"].includes(key))return[key,typeof value==="boolean"?value:typeof value==="string"?observedBoolean(value):null] + if(typeof value!=="string"||value.length>200||/[\u0000-\u001f\u007f]/.test(value))throw Error("Unconfirmed IPv6 context field") + if(key==="address"&&isIP(value.split("/")[0].split("%")[0])!==6)throw Error("Unconfirmed IPv6 context address") + if(key===".id"&&!/^\*[a-z0-9]+$/i.test(value))throw Error("Unconfirmed IPv6 context ID") + if(key==="status"&&!['noarp','incomplete','stale','reachable','delay','probe','failed','tentative','preferred','deprecated','invalid'].includes(value))return[key,null] + if(["vrf","VRF"].includes(key)&&value!=="main"&&!value.startsWith(marker+"-"))return[key,null] + return[key,value] + }))) + return {observedAt:new Date().toISOString(),interface:gre,addresses:project(addresses,[".id","interface","address","vrf","disabled","invalid","tentative","status"]), + neighborCacheObserved:neighbors!==null,neighbors:neighbors===null?null:project(neighbors,[".id","interface","address","status","router","vrf","VRF"]),causeOfPacketLossVerified:false} +} diff --git a/backend/src/modules/mcp/chr-readonly-acceptance.test.ts b/backend/src/modules/mcp/chr-readonly-acceptance.test.ts new file mode 100644 index 00000000..0da77295 --- /dev/null +++ b/backend/src/modules/mcp/chr-readonly-acceptance.test.ts @@ -0,0 +1,134 @@ +import assert from "node:assert/strict" +import { writeFile } from "node:fs/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { preflightPaths } from "./ecmp-preflight.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === hosts.length && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const actor: AuthUser = { id: "chr-readonly-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin", "mm:filters:admin", "mm:certificates:admin", "mm:bgp:admin", "mm:uptime:read", "mm:traffic:read"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const underlayDiagnostic = process.env.MMAPP_TEST_CHR_UNDERLAY === "true" +const report = { startedAt: new Date().toISOString(), complete: false, deviceMutations: 0, networkValidation: "not-performed", allUnderlayPacketsReceived: null as boolean | null, checks, finishedAt: "" } +const save = () => writeFile(new URL(underlayDiagnostic ? "../../../../docs/mcp-chr-underlay-diagnostic.json" : "../../../../docs/mcp-chr-catalog-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +try { + await save() + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, "CHR readonly acceptance", ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const operationIds = ["routing.ospf.application.preview", "routing.filters.catalog.read", "routing.recursive.catalog.read", "routing.recursive.gateways.catalog.read", "routing.ecmp.preflight.read", "routing.gre.catalog.read", "containers.state.read", "certificates.state.read", "servers.snapshots.read", "servers.source-address.read", "servers.wan-runtime.read", "routing.bgp.sessions.read", "routing.bgp.sessions.raw.read", "uptime.ping.interfaces.read", "uptime.speed.interfaces.read", "traffic.live.read", "routing.ospf.state.read", "routing.bfd.sessions.read", "firewall.state.read", "network.wireguard.state.read", "network.ipsec.state.read", "network.vxlan.state.read", "network.gre.state.read"] + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "CHR read-only catalogs", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:servers:read", "mm:filters:read", "mm:certificates:read", "mm:bgp:read", "mm:uptime:read", "mm:traffic:read"], scope: { serverIds: [1, 2], operationIds, allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + await save() + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const resources = ["gre", "vrf", "routing_table", "ipv4_route", "ipv6_route", "ip_settings", "ipv6_settings", ...["ip", "ipv6"].flatMap(family => ["filter", "nat", "mangle", "raw"].map(table => `${family}_${table}`))] + const baseline = [] + for (const resource of resources) baseline.push(await readRosSnapshot(serverId, resource)) + const resource = await client.get>("/system/resource") + if (underlayDiagnostic) { + const destination = hosts[1 - index] + const fields = new Set(["seq", "sent", "received", "packet-loss", "status", "time", "avg-rtt", "min-rtt", "max-rtt"]) + const settings = await client.get>("/ip/settings") + for (let round = 1; round <= 3; round++) { + const result = await client.post[]>("/tool/ping", { address: destination, count: "3", interval: "1s", "src-address": host }, 20000) + assert.ok(Array.isArray(result) && result.length > 0 && result.length <= 10) + const packets = result.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => fields.has(key)))) + const last = packets.at(-1)!, allReceived = String(last.sent) === "3" && String(last.received) === "3" && String(last["packet-loss"]) === "0" + report.allUnderlayPacketsReceived = (report.allUnderlayPacketsReceived ?? true) && allReceived + checks.push({ host, destination, phase: "underlay-ping", round, interval: "1s", packets, allReceived, routerOs: String(resource.version), rpFilter: settings["rp-filter"] ?? null, ipForward: settings["ip-forward"] ?? null }) + await save() + } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Diagnostic changed configuration or observed external drift") + checks.push({ host, phase: "underlay-configuration-check", configurationUnchanged: true }) + report.networkValidation = "underlay ICMP only; no GRE/BGP or end-user forwarding claim" + await save(); continue + } + const catalogs = [] + for (const tool of ["mm_ospf_application_preview", "mm_bgp_filter_rules_get", "mm_recursive_routes_catalog_get", "mm_recursive_gateways_catalog_get", "mm_gre_catalog_get", "mm_containers_state_get", "mm_certificates_state_get", "mm_server_snapshots_get", "mm_server_source_address_get", "mm_server_wan_runtime_get", "mm_bgp_sessions_get", "mm_bgp_sessions_raw_get"]) { + const result = await invokeMcpTool(principal, tool, { serverId,...(tool==="mm_ospf_application_preview"?{peerServerIds:[serverId]}:{}) }) as Record + if(tool==="mm_ospf_application_preview"){ + assert.equal(result.proposalOnly,true);assert.equal(result.executionAvailable,false);assert.equal(result.metricContextVerified,false);assert.equal(result.forwardingVerified,false) + assert.deepEqual(result.allowedMetricServerIds,[serverId]);assert.ok(Array.isArray((result.interfaces as {items:unknown[]}).items)) + } + if (tool === "mm_bgp_filter_rules_get") { + const rulesets = result.rulesets as { serverId: string; live: boolean; stale: boolean; error: string | null; nativeRules: { total: number } | null }[] + assert.equal(rulesets.length, 1); assert.equal(rulesets[0].serverId, String(serverId)); assert.equal(rulesets[0].live, true); assert.equal(rulesets[0].stale, false); assert.equal(rulesets[0].error, null) + assert.ok(rulesets[0].nativeRules); assert.equal(result.writesPerformed, false); assert.equal(result.forwardingVerified, false) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM filter_rules WHERE server_id=$1", [serverId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE server_id=$1 AND section='filters'", [serverId])).rows[0].n, 0) + } + if (tool === "mm_recursive_routes_catalog_get") { assert.equal(result.live, true); assert.equal(result.stale, false) } + if (tool === "mm_gre_catalog_get") assert.deepEqual(result.failures, []) + if (tool === "mm_server_wan_runtime_get") { assert.ok(["confirmed", "ambiguous", "unknown", "none"].includes(String(result.defaultRouteStatus))); assert.equal(result.forwardingVerified, false) } + if (tool === "mm_bgp_sessions_get") assert.deepEqual(result.failures, []) + assert.ok(!JSON.stringify(result).includes(process.env.MMAPP_TEST_CHR_PASSWORD!), "Device password must not enter the acceptance report") + if (tool === "mm_containers_state_get") { + const states = result.servers as { serverId: number; availability: string; failures: unknown[] }[] + assert.equal(states.length, 1); assert.equal(states[0].serverId, serverId) + assert.notEqual(states[0].availability, "error"); assert.deepEqual(states[0].failures, []) + } + if (tool === "mm_certificates_state_get") { + const states = result.servers as { serverId: string; error: string | null }[] + assert.equal(states.length, 1); assert.equal(states[0].serverId, String(serverId)); assert.equal(states[0].error, null) + } + // Store bounded already-masked MCP DTOs, never credentials/raw RouterOS rows. + catalogs.push({ tool, result }) + } + const preflights = [] + for (const tool of ["mm_wireguard_state_get", "mm_ipsec_state_get", "mm_vxlan_state_get", "mm_gre_state_get"]) { + const result = await invokeMcpTool(principal, tool, { serverId }) as { failures: unknown[]; serverIds: number[] } + assert.deepEqual(result.failures, []); assert.deepEqual(result.serverIds, [serverId]) + assert.ok(!JSON.stringify(result).includes(process.env.MMAPP_TEST_CHR_PASSWORD!)) + catalogs.push({ tool, result }) + } + const firewall = await invokeMcpTool(principal, "mm_firewall_state_get", { serverId, limit: 200 }) as { failures: unknown[]; serverIds: number[]; packetMatchingVerified: boolean } + assert.deepEqual(firewall.failures, []); assert.deepEqual(firewall.serverIds, [serverId]); assert.equal(firewall.packetMatchingVerified, false) + assert.ok(!JSON.stringify(firewall).includes(process.env.MMAPP_TEST_CHR_PASSWORD!)) + catalogs.push({ tool: "mm_firewall_state_get", result: firewall }) + for (const tool of ["mm_ospf_state_get", "mm_bfd_sessions_get"]) { + const result = await invokeMcpTool(principal, tool, { serverId }) as { servers: { serverId: number }[]; failures: unknown[]; incomplete: boolean } + assert.equal(result.servers.length, 1); assert.equal(result.servers[0].serverId, serverId) + assert.deepEqual(result.failures, []); assert.equal(result.incomplete, false) + assert.ok(!JSON.stringify(result).includes(process.env.MMAPP_TEST_CHR_PASSWORD!)) + catalogs.push({ tool, result }) + } + for (const tool of ["mm_ping_source_interfaces_get", "mm_speed_test_interfaces_get"]) { + const result = await invokeMcpTool(principal, tool, { serverId }) as { interfaces: { name: string }[] } + assert.ok(result.interfaces.length > 0, "CHR must confirm an eligible probe interface") + assert.ok(!JSON.stringify(result).includes(process.env.MMAPP_TEST_CHR_PASSWORD!)) + catalogs.push({ tool, result }) + if (tool === "mm_ping_source_interfaces_get") { + const live = await invokeMcpTool(principal, "mm_traffic_live_get", { serverId, interface: result.interfaces[0].name }) as { samples: { rxMbps: number; txMbps: number }[]; forwardingVerified: boolean } + assert.equal(live.samples.length, 1); assert.equal(live.forwardingVerified, false) + assert.ok([live.samples[0].rxMbps, live.samples[0].txMbps].every(value => Number.isFinite(value) && value >= 0)) + catalogs.push({ tool: "mm_traffic_live_get", result: live }) + } + } + for (const family of ["ipv4", "ipv6"] as const) { + const raw = await client.get[]>(preflightPaths(family).routes) + const routeIds = raw.slice(0, 2).map(row => String(row[".id"])) + while (routeIds.length < 2) routeIds.push("*unobserved-" + routeIds.length) + const result = await invokeMcpTool(principal, "mm_ecmp_preflight_get", { serverId, family, routeIds }) as { sources: Record; networkValidation: string; rpf: { supported: boolean } } + assert.equal(result.networkValidation, "not-performed"); assert.equal(result.rpf.supported, family === "ipv4") + assert.ok(Object.values(result.sources).every(source => source.observed), "CHR did not confirm every preflight source") + preflights.push(result) + } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Read-only acceptance changed configuration or observed external drift") + checks.push({ host, routerOs: String(resource.version), catalogs, preflights, baselineRestored: true, baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })) }) + await save(); console.log(`✓ CHR ${serverId}: scoped catalogs and IPv4/IPv6 ECMP preflight; device configuration unchanged`) + } + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action LIKE 'configuration.%'")).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations")).rows[0].n, 0) + report.complete = true; report.finishedAt = new Date().toISOString(); await save() +} finally { await closePool() } diff --git a/backend/src/modules/mcp/chr-recovery-source.test.ts b/backend/src/modules/mcp/chr-recovery-source.test.ts new file mode 100644 index 00000000..e491d6f9 --- /dev/null +++ b/backend/src/modules/mcp/chr-recovery-source.test.ts @@ -0,0 +1,40 @@ +import assert from "node:assert/strict" +import { readChrRecoverySource } from "./chr-recovery-source.js" + +const marker = "mcp-test-1234abcd" +const rows = (resources: string[]) => [1, 2].flatMap(serverId => resources.map(resource => ({ serverId, resource, fingerprint: "a".repeat(64) }))) +const overlay = rows(["gre", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table"]) +const ospf = rows(["ospf_template", "ospf_area", "ospf_instance", "bfd_configuration"]) +const source = { marker, complete: false, finishedAt: new Date(Date.now() - 1000).toISOString(), optimizerRevision: 6, measurementRevision: 2, sourceMigration: "0029_mcp_routing_sources", checks: [{ overlayPreflight: true, baselineFingerprints: overlay }] } +assert.equal(readChrRecoverySource(source, marker).kind, "overlay") +const full = { ...source, checks: [...source.checks, { ospfPreflight: true, baselineFingerprints: ospf }, { tool: "mm_ospf_template_create_plan", state: "unconfirmed" }] } +assert.equal(readChrRecoverySource(full, marker).kind, "ospf") +assert.equal(readChrRecoverySource({ ...full, optimizerRevision: 7 }, marker).kind, "ospf") +assert.equal(readChrRecoverySource(full, marker).baselines.length, 22) +const forward = { ...full, ospfForwarding: true, fixtureSchema: "ospf-forward-v1", checks: [{ overlayPreflight: true, baselineFingerprints: [...overlay, ...rows(["bridge"])] }, full.checks[1]] } +assert.equal(readChrRecoverySource(forward, marker).baselines.length, 24) +assert.equal(readChrRecoverySource(forward, marker).ospfForwarding, true) +assert.equal(readChrRecoverySource(source, marker).ospfForwarding, false) +assert.throws(() => readChrRecoverySource({ ...forward, fixtureSchema: undefined }, marker)) +assert.throws(() => readChrRecoverySource({ ...forward, ospfForwarding: false }, marker)) +assert.throws(() => readChrRecoverySource({ ...full, ospfForwarding: true, fixtureSchema: "ospf-forward-v1" }, marker)) +assert.throws(() => readChrRecoverySource({ ...full, checks: forward.checks }, marker)) +for (const bad of [ + { ...full, complete: true }, { ...full, finishedAt: undefined }, { ...full, finishedAt: new Date(Date.now() + 100000).toISOString() }, + { ...full, marker: "mcp-test-ffffffff" }, { ...full, optimizerRevision: 8 }, { ...full, optimizerRevision: "6" }, { ...full, measurementRevision: 1 }, + { ...full, checks: [...source.checks, { ospfPreflight: true, baselineFingerprints: ospf.slice(1) }] }, + { ...full, checks: [...source.checks, { ospfPreflight: true, baselineFingerprints: [...ospf.slice(1), ospf[1]] }] }, + { ...full, checks: [...source.checks, { tool: "mm_bfd_configuration_create_plan" }] }, + { ...full, checks: [...full.checks, full.checks[1]] }, +]) assert.throws(() => readChrRecoverySource(bad, marker)) +const legacy = { marker, complete: false, finishedAt: source.finishedAt, checks: [{ overlayPreflight: true, baselineFingerprints: [...overlay, ...rows(["bgp_instance", "bgp_connection", "bridge", "routing_filter"])] }] } +assert.equal(readChrRecoverySource(legacy, marker).kind, "bgp") +assert.equal(readChrRecoverySource(legacy, marker).baselines.length, 22) +const prefixOverlay = { marker, complete: false, finishedAt: source.finishedAt, fixtureSchema: "ospf-prefix-v1", checks: source.checks } +assert.equal(readChrRecoverySource(prefixOverlay, marker).kind, "overlay") +const prefix = { ...prefixOverlay, checks: [...prefixOverlay.checks, { ospfPrefixPreflight: true, baselineFingerprints: rows(["ospf_template", "ospf_area", "ospf_instance", "ipv6_address_list"]) }] } +assert.equal(readChrRecoverySource(prefix, marker).kind, "ospf-prefix") +assert.equal(readChrRecoverySource(prefix, marker).baselines.length, 22) +assert.throws(() => readChrRecoverySource({ ...prefixOverlay, checks: [...prefixOverlay.checks, { tool: "mm_firewall_address_list_create_plan" }] }, marker)) +assert.throws(() => readChrRecoverySource({ ...prefix, checks: [...prefix.checks, prefix.checks[1]] }, marker)) +console.log("CHR recovery source: terminal marker-bound OSPF/overlay/BGP receipts, exact scoped baselines and malformed reports rejected") diff --git a/backend/src/modules/mcp/chr-recovery-source.ts b/backend/src/modules/mcp/chr-recovery-source.ts new file mode 100644 index 00000000..7aec20b5 --- /dev/null +++ b/backend/src/modules/mcp/chr-recovery-source.ts @@ -0,0 +1,46 @@ +import assert from "node:assert/strict" +import { z } from "zod" + +const overlay = ["gre", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table"] +const ospf = ["ospf_template", "ospf_area", "ospf_instance", "bfd_configuration"] +const bgp = ["bgp_instance", "bgp_connection", "bridge", "routing_filter"] +const baselineSchema = z.array(z.object({ serverId: z.union([z.literal(1), z.literal(2)]), resource: z.string(), fingerprint: z.string().regex(/^[a-f0-9]{64}$/) }).strict()).max(24) + +/** Validate a finalized marker-bound report. The launcher/operator must separately confirm its process is terminal before invoking cleanup. */ +export function readChrRecoverySource(value: unknown, marker: string) { + const report = z.object({ marker: z.string(), complete: z.literal(false), finishedAt: z.string().datetime(), checks: z.array(z.record(z.string(), z.unknown())).max(10000), optimizerRevision: z.unknown().optional(), measurementRevision: z.unknown().optional(), sourceMigration: z.unknown().optional(), ospfForwarding: z.boolean().optional(), fixtureSchema: z.unknown().optional() }).passthrough().parse(value) + assert.match(marker, /^mcp-test-[0-9a-f]{8}$/); assert.equal(report.marker, marker) + assert.ok(Date.parse(report.finishedAt) <= Date.now(), "Recovery refuses a live or future report") + const read = (flag: string, resources: string[]) => { + const receipts = report.checks.filter(row => row[flag] === true) + assert.equal(receipts.length, 1, "Exactly one baseline receipt required") + const rows = baselineSchema.parse(receipts[0].baselineFingerprints) + assert.equal(rows.length, resources.length * 2) + assert.equal(new Set(rows.map(row => row.serverId + ":" + row.resource)).size, rows.length) + assert.ok(rows.every(row => resources.includes(row.resource))) + return rows + } + const source = report.optimizerRevision !== undefined || report.measurementRevision !== undefined || report.sourceMigration !== undefined + if (report.fixtureSchema === "ospf-prefix-v1") { + assert.ok(!source && !report.ospfForwarding) + const baselines = read("overlayPreflight", overlay) + if (report.checks.some(row => row.ospfPrefixPreflight === true)) { + return { kind: "ospf-prefix" as const, ospfForwarding: false, baselines: [...baselines, ...read("ospfPrefixPreflight", ["ospf_template", "ospf_area", "ospf_instance", "ipv6_address_list"])] } + } + assert.ok(!report.checks.some(row => typeof row.tool === "string" && /^mm_(?:ospf_|firewall_address_list_)/.test(row.tool)), "Prefix recovery requires its preflight receipt") + return { kind: "overlay" as const, ospfForwarding: false, baselines } + } + if (!source) { + assert.ok(!report.ospfForwarding && report.fixtureSchema === undefined) + return { kind: "bgp" as const, ospfForwarding: false, baselines: read("overlayPreflight", [...overlay, ...bgp]) } + } + assert.ok([3, 4, 5, 6, 7].includes(Number(report.optimizerRevision)) && typeof report.optimizerRevision === "number") + assert.equal(report.measurementRevision, 2); assert.equal(report.sourceMigration, "0029_mcp_routing_sources") + const ospfForwarding = report.ospfForwarding === true + if (ospfForwarding) assert.equal(report.fixtureSchema, "ospf-forward-v1") + else assert.equal(report.fixtureSchema, undefined) + const baselines = read("overlayPreflight", [...overlay, ...(ospfForwarding ? ["bridge"] : [])]) + if (report.checks.some(row => row.ospfPreflight === true)) return { kind: "ospf" as const, ospfForwarding, baselines: [...baselines, ...read("ospfPreflight", ospf)] } + assert.ok(!report.checks.some(row => typeof row.tool === "string" && /^mm_(?:ospf_|bfd_)/.test(row.tool)), "OSPF writes without their own preflight receipt cannot authorize recovery") + return { kind: "overlay" as const, ospfForwarding, baselines } +} diff --git a/backend/src/modules/mcp/chr-recursive-restore.test.ts b/backend/src/modules/mcp/chr-recursive-restore.test.ts new file mode 100644 index 00000000..0dad558b --- /dev/null +++ b/backend/src/modules/mcp/chr-recursive-restore.test.ts @@ -0,0 +1,119 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { parseRecursiveCatalog } from "../../services/recursive-route-catalog.js" +import { readRecursiveNativeRowsForServer, readRecursiveRoutesForServer } from "../../routes/recursive-routes.js" +import { fingerprintPayload } from "../../services/config-revisions.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === hosts.length && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const marker = "mcp-test-" + randomUUID().slice(0, 8), tableName = marker + "-rec" +const actor: AuthUser = { id: "chr-recursive-restore-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const report = { startedAt: new Date().toISOString(), marker, complete: false, trafficValidation: false, checks, finishedAt: "" } +const save = () => writeFile(new URL("../../../../docs/mcp-chr-recursive-restore-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, marker, ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const operationIds = ["routing.recursive.revision.restore", "routing.recursive.routes.replace", "routing.recursive.routes.read", "routing.recursive.gateways.read", "ros.routing_table.create", "ros.routing_table.delete", "ros.ipv4_route.create", "ros.ipv4_route.delete"] + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], limits: { callsPerMinute: 120, concurrentReads: 4, mutationsPerHour: 100, serversPerPlan: 2, objectsPerPlan: 100 }, scope: { serverIds: [1, 2], operationIds, allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const execute = async (tool: string, input: Record) => { + const plan = await invokeMcpTool(principal, tool, input) as { planId: string; changes: { before: { observed: { routingTable: string; comment: string }[] }; steps: { input: { routes: { routingTable: string; comment: string; disabled: boolean }[] } }[] } } + if (["mm_recursive_revision_restore_plan", "mm_recursive_routes_replace_plan"].includes(tool)) { + assert.equal(plan.changes.before.observed.length, 1, "Never auto-approve replacement of an unexpected owned set") + assert.ok(plan.changes.before.observed.every(row => row.routingTable === tableName && row.comment === marker)) + assert.ok(plan.changes.steps[0].input.routes.every(row => row.routingTable === tableName && row.comment === marker && row.disabled)) + } + await approvePlan(plan.planId, actor, true) + const operation = await applyPlan(principal, plan.planId, randomUUID()) + for (let attempt = 0; attempt < 100; attempt++) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { + assert.equal(row.state, "applied", `${tool}: ${row.state}/${row.error_code}`) + return operation.id + } + await delay(300) + } + throw Error("Operation pending; never automatically replay lab writes") + } + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { server, client } = await routerClient(serverId) + const baseline = await Promise.all(["ipv4_route", "routing_table"].map(resource => readRosSnapshot(serverId, resource))) + assert.equal(parseRecursiveCatalog(await readRecursiveNativeRowsForServer(server)).length, 0, "Lab restore refuses existing owned recursive routes") + assert.ok(!baseline[1].rows.some(row => row.name === tableName)) + checks.push({ host, phase: "preflight", baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })) }); await save() + try { + await execute("mm_routing_table_create_plan", { serverId, fields: { name: tableName, fib: false } }) + const table = (await readRosSnapshot(serverId, "routing_table")).rows.find(row => row.name === tableName) + assert.equal(table?.fib, "false") + await execute("mm_ipv4_route_create_plan", { serverId, fields: { "dst-address": `198.18.250.${serverId}/32`, gateway: "192.0.2.1", "routing-table": tableName, distance: 10, scope: 30, "target-scope": 10, "check-gateway": "none", disabled: true, comment: "MikrotikManager:recursive " + marker } }) + const owned = parseRecursiveCatalog(await readRecursiveNativeRowsForServer(server)) + assert.equal(owned.length, 1); assert.equal(owned[0].routingTable, tableName); assert.equal(owned[0].comment, marker) + const cachedBeforeRestore = await readRecursiveRoutesForServer(server) + assert.equal(cachedBeforeRestore.live, true); assert.equal(cachedBeforeRestore.routes.length, 1) + const logicalId = cachedBeforeRestore.routes[0].id + const payload = [{ ...owned[0], distance: 25, country: "lab-metadata" }], revisionId = randomUUID() + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,$2,'recursive-routes','apply',$3,$4::jsonb)", [revisionId, serverId, fingerprintPayload(payload), JSON.stringify(payload)]) + const restored = await execute("mm_recursive_revision_restore_plan", { serverId, revisionId }) + const after = parseRecursiveCatalog(await readRecursiveNativeRowsForServer(server)) + assert.equal(after.length, 1); assert.equal(after[0].distance, 25); assert.equal(after[0].disabled, true); assert.notEqual(after[0].id, owned[0].id) + assert.equal((await pool.query("SELECT country FROM recursive_routes WHERE server_id=$1", [serverId])).rows[0].country, "lab-metadata") + checks.push({ host, phase: "restore", operationId: restored, nativeDistance: 25, disabled: true, nativeIdRecreated: true, metadataVerified: true }); await save() + const binding = (await pool.query("INSERT INTO filter_rules(server_id,community,gateway_tunnel_id) VALUES($1,'65000:997',$2) RETURNING id", [serverId, "rec:" + logicalId])).rows[0].id + try { + for (let attempt = 0; attempt < 2; attempt++) { + const live = await readRecursiveRoutesForServer(server) + assert.equal(live.live, true); assert.equal(live.routes[0].id, logicalId); assert.equal(live.routes[0].country, "lab-metadata") + } + const metadataBefore = (await pool.query("SELECT * FROM recursive_routes WHERE server_id=$1", [serverId])).rows + const revisionsBefore = (await pool.query("SELECT * FROM config_revisions WHERE server_id=$1 ORDER BY id", [serverId])).rows + const observed = await invokeMcpTool(principal, "mm_recursive_routes_get", { serverId }) as { routes: { id: string }[]; live: boolean; forwardingVerified: boolean } + assert.equal(observed.live, true); assert.equal(observed.routes[0].id, logicalId); assert.equal(observed.forwardingVerified, false) + await invokeMcpTool(principal, "mm_recursive_gateways_get", { serverId, limit: 1 }) + assert.deepEqual((await pool.query("SELECT * FROM recursive_routes WHERE server_id=$1", [serverId])).rows, metadataBefore) + assert.deepEqual((await pool.query("SELECT * FROM config_revisions WHERE server_id=$1 ORDER BY id", [serverId])).rows, revisionsBefore) + assert.equal((await pool.query("SELECT gateway_tunnel_id FROM filter_rules WHERE id=$1", [binding])).rows[0].gateway_tunnel_id, "rec:" + logicalId) + checks.push({ host, phase: "stable-identity-readers", stableLogicalId: true, bindingPreserved: true, observerHasNoMetadataWrites: true, forwardingVerified: false }); await save() + const edited = await execute("mm_recursive_routes_replace_plan", { serverId, routes: [{ ...payload[0], id: logicalId, distance: 30 }] }) + const afterEdit = await readRecursiveRoutesForServer(server) + assert.equal(afterEdit.live, true); assert.equal(afterEdit.routes[0].id, logicalId); assert.equal(afterEdit.routes[0].distance, 30); assert.equal(afterEdit.routes[0].disabled, true) + assert.equal((await pool.query("SELECT gateway_tunnel_id FROM filter_rules WHERE id=$1", [binding])).rows[0].gateway_tunnel_id, "rec:" + logicalId) + checks.push({ host, phase: "full-set-edit", operationId: edited, nativeDistance: 30, disabled: true, stableLogicalId: true, bindingPreserved: true, perCommandReadBack: true }); await save() + } finally { await pool.query("DELETE FROM filter_rules WHERE id=$1", [binding]) } + const directCleared = await execute("mm_recursive_routes_replace_plan", { serverId, routes: [], source: "copy" }) + assert.equal(parseRecursiveCatalog(await readRecursiveNativeRowsForServer(server)).length, 0) + checks.push({ host, phase: "full-set-empty", operationId: directCleared, ownedSetEmpty: true }); await save() + await execute("mm_ipv4_route_create_plan", { serverId, fields: { "dst-address": `198.18.250.${serverId}/32`, gateway: "192.0.2.1", "routing-table": tableName, distance: 10, scope: 30, "target-scope": 10, "check-gateway": "none", disabled: true, comment: "MikrotikManager:recursive " + marker } }) + const emptyRevision = randomUUID() + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,$2,'recursive-routes','apply',$3,'[]'::jsonb)", [emptyRevision, serverId, fingerprintPayload([])]) + const cleared = await execute("mm_recursive_revision_restore_plan", { serverId, revisionId: emptyRevision }) + assert.equal(parseRecursiveCatalog(await readRecursiveNativeRowsForServer(server)).length, 0) + checks.push({ host, phase: "empty-revision", operationId: cleared, ownedSetEmpty: true }); await save() + } finally { + const own = (await readRosSnapshot(serverId, "ipv4_route")).rows.filter(row => row["routing-table"] === tableName && row.comment === "MikrotikManager:recursive " + marker) + for (const row of own) await execute("mm_ipv4_route_delete_plan", { serverId, id: row[".id"] }) + assert.ok(!(await client.get[]>("/routing/rule")).some(row => row.table === tableName)) + const tables = (await readRosSnapshot(serverId, "routing_table")).rows.filter(row => row.name === tableName) + for (const row of tables) { assert.equal(row.fib, "false"); await execute("mm_routing_table_delete_plan", { serverId, id: row[".id"] }) } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Lab baseline changed") + checks.push({ host, phase: "cleanup", baselineRestored: true }); await save() + } + console.log(`✓ CHR ${serverId}: disabled non-FIB recursive restore, native/cache/revision read-back, empty restore and baseline cleanup`) + } + report.complete = true; report.finishedAt = new Date().toISOString(); await save() +} finally { await closePool() } diff --git a/backend/src/modules/mcp/chr-source-baseline-diagnostic.ts b/backend/src/modules/mcp/chr-source-baseline-diagnostic.ts new file mode 100644 index 00000000..5047ec7e --- /dev/null +++ b/backend/src/modules/mcp/chr-source-baseline-diagnostic.ts @@ -0,0 +1,69 @@ +import assert from "node:assert/strict" +import { readFile, writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { MikrotikClient } from "../../services/mikrotik.js" +import { configurationRows, resourceById } from "./resources.js" +import { fingerprint } from "./security.js" + +assert.equal(process.env.MMAPP_TEST_CHR_SOURCE_DIAGNOSTIC, "true") +assert.ok(process.stdin.isTTY, "Private terminal required") +const original = JSON.parse(await readFile(new URL("../../../../docs/mcp-chr-ospf-source-interrupted.json", import.meta.url), "utf8")) +assert.equal(original.marker, "mcp-test-c74d0d3c"); assert.equal(original.complete, false); assert.ok(original.finishedAt) +const baselines = original.checks.find((row: { overlayPreflight?: boolean }) => row.overlayPreflight).baselineFingerprints as { serverId: number; resource: string; fingerprint: string }[] +assert.equal(baselines.length, 14) +process.stdout.write("CHR password (hidden): ") +let secret = await new Promise((resolve, reject) => { + let value = "" + const finish = (error?: Error) => { clearTimeout(timer); process.stdin.off("data", read); process.stdin.setRawMode(false); process.stdin.pause(); process.stdout.write("\n"); if (error) reject(error); else resolve(value) } + const read = (chunk: Buffer) => { for (const c of chunk.toString()) { if (c === "\r" || c === "\n") { finish(); return } if (c === "\u0003") { finish(new Error("Cancelled")); return } if (c === "\b" || c === "\u007f") value = value.slice(0, -1); else value += c } } + const timer = setTimeout(() => finish(new Error("Password timeout")), 60000) + process.stdin.setRawMode(true); process.stdin.resume(); process.stdin.on("data", read) +}) +const checks: Record[] = [], report = { startedAt: new Date().toISOString(), originalMarker: original.marker, deviceMutations: 0, complete: false, allExact: false, checks } +try { + for (const serverId of [1, 2]) { + const client = new MikrotikClient({ host: "127.0.0.1", port: serverId === 1 ? 19081 : 19082, useSsl: false, verifySsl: true, username: "admin", password: secret }) + const system = await client.get>("/system/resource") + checks.push({ serverId, phase: "system", version: system.version, uptime: system.uptime }) + for (const baseline of baselines.filter(row => row.serverId === serverId)) { + await delay(500) + const spec = resourceById(baseline.resource), rows = configurationRows(spec, await client.get(spec.path)) + const hash = fingerprint(rows), ownRemaining = rows.some(row => JSON.stringify(row).includes(original.marker)) + const check: Record = { serverId, resource: baseline.resource, expected: baseline.fingerprint, actual: hash, exact: hash === baseline.fingerprint, ownRemaining, rows: rows.length } + if (!check.exact) { + // Diagnose one generated native ID without changing any router value. + const dynamic = rows.flatMap((row, index) => [true, "true", "yes"].includes(row.dynamic as string | boolean) ? [index] : []) + if (dynamic.length === 1) { + const index = dynamic[0] + for (let n = 0; n < 8192; n++) { + const oldId = "*" + n.toString(16).toUpperCase(), candidate = rows.map((row, i) => i === index ? { ...row, ".id": oldId } : row).sort((a, b) => String(a[".id"]).localeCompare(String(b[".id"]))) + if (fingerprint(candidate) === baseline.fingerprint) { check.onlyGeneratedIdChanged = { previous: oldId, current: rows[index][".id"] }; break } + } + } + if (!check.onlyGeneratedIdChanged && dynamic.length > 0 && dynamic.length <= 10 && dynamic.every(index => /^\*[a-f0-9]{1,16}$/i.test(String(rows[index][".id"])))) { + const indices = new Set(dynamic) + for (let offset = -8192; offset <= 8192; offset++) { + const candidate = rows.map((row, index) => { + if (!indices.has(index)) return row + const value = BigInt("0x" + String(row[".id"]).slice(1)) + BigInt(offset) + return { ...row, ".id": value >= 0n ? "*" + value.toString(16).toUpperCase() : "invalid" } + }).sort((a, b) => String(a[".id"]).localeCompare(String(b[".id"]))) + if (fingerprint(candidate) === baseline.fingerprint) { + check.onlyGeneratedIdsChanged = { commonOffset: offset, count: dynamic.length } + break + } + } + } + if (baseline.resource.endsWith("_route")) check.routeIdentities = rows.map(row => ({ id: row[".id"], dynamic: row.dynamic, table: row["routing-table"] })) + if (baseline.resource === "ipv4_address") check.safeAddressRows = rows.map(row => ({ id: row[".id"], address: row.address, interface: row.interface, disabled: row.disabled, dynamic: row.dynamic, hasComment: row.comment !== undefined })) + } + checks.push(check) + await writeFile(new URL("../../../../docs/mcp-chr-ospf-source-baseline-diagnostic.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") + } + } + report.allExact = checks.filter(row => row.resource).every(row => row.exact) + report.complete = true +} finally { + secret = "" + await writeFile(new URL("../../../../docs/mcp-chr-ospf-source-baseline-diagnostic.json", import.meta.url), JSON.stringify({ ...report, finishedAt: new Date().toISOString() }, null, 2) + "\n") +} diff --git a/backend/src/modules/mcp/chr-vpn-catalog.test.ts b/backend/src/modules/mcp/chr-vpn-catalog.test.ts new file mode 100644 index 00000000..7807a86f --- /dev/null +++ b/backend/src/modules/mcp/chr-vpn-catalog.test.ts @@ -0,0 +1,87 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import type { AuthUser } from "../../lib/permissions.js" + +const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) +assert.equal(hosts.length, 2); assert.equal(new Set(hosts).size, 2) +assert.ok(hosts.every(host => ["194.226.121.150", "104.165.244.7"].includes(host))) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert.ok(ports.length === 2 && ports.every(port => Number.isInteger(port) && port >= 1024 && port <= 65535)) +assert.ok(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD) +const marker = "mcp-test-" + randomUUID().slice(0, 8) +const actor: AuthUser = { id: "chr-vpn-catalog-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +const checks: Record[] = [] +const report = { startedAt: new Date().toISOString(), marker, complete: false, trafficValidation: false, checks, finishedAt: "" } +const save = () => writeFile(new URL("../../../../docs/mcp-chr-vpn-catalog-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +const definitions = [ + { resource: "gre", suffix: "gre", path: "/interface/gre", fields: { "local-address": "0.0.0.0", "remote-address": "198.18.255.254", mtu: 1400, keepalive: "10s,10" } }, + { resource: "wireguard", suffix: "wg", path: "/interface/wireguard", fields: { "listen-port": 13231, mtu: 1420 } }, + { resource: "vxlan", suffix: "vx", path: "/interface/vxlan", fields: { vni: 16770001, port: 4789 } }, + { resource: "ipsec_peer", suffix: "ike", path: "/ip/ipsec/peer", fields: { address: "198.18.255.254/32", "exchange-mode": "ike2", profile: "default", passive: true } }, +] as const +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", [index + 1, marker, ports ? "127.0.0.1" : host, ports?.[index] ?? 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + const operationIds = [...definitions.flatMap(row => [`ros.${row.resource}.create`, `ros.${row.resource}.delete`]), "network.wireguard.state.read", "network.vxlan.state.read", "network.ipsec.state.read", "network.gre.state.read"] + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, mutationsPerHour: 100 }, scope: { serverIds: [1, 2], operationIds, allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const execute = async (tool: string, input: Record) => { + const plan = await invokeMcpTool(principal, tool, input) as { planId: string } + await approvePlan(plan.planId, actor, true) + const operation = await applyPlan(principal, plan.planId, randomUUID()) + for (let attempt = 0; attempt < 150; attempt++) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (!["pending", "running"].includes(row.state)) { assert.equal(row.state, "applied", `${tool}: ${row.state}/${row.error_code}`); return operation.id } + await delay(300) + } + throw Error("Operation pending; never replay lab writes automatically") + } + await save() + for (const [index, host] of hosts.entries()) { + const serverId = index + 1, { client } = await routerClient(serverId) + const baseline = await Promise.all([...definitions.map(row => row.resource), "ipv4_address", "ipv6_address"].map(resource => readRosSnapshot(serverId, resource))) + assert.ok(baseline.slice(0, definitions.length).every(snapshot => snapshot.rows.length === 0), "Lab refuses existing GRE/WireGuard/VXLAN/IPsec peer configs") + checks.push({ host, phase: "preflight", baselineFingerprints: baseline.map(({ resource, fingerprint }) => ({ resource, fingerprint })) }); await save() + try { + for (const definition of definitions) await execute(`mm_${definition.resource}_create_plan`, { serverId, fields: { ...definition.fields, name: `${marker}-${definition.suffix}`, disabled: true, comment: marker } }) + const wg = await invokeMcpTool(principal, "mm_wireguard_state_get", { serverId }) as { interfaces: { name: string; enabled: boolean | null }[]; failures: unknown[]; keyMaterialIncluded: boolean } + assert.deepEqual(wg.failures, []); assert.equal(wg.interfaces.length, 1); assert.equal(wg.interfaces[0].enabled, false); assert.equal(wg.keyMaterialIncluded, false) + const rawWg = await client.get[]>("/interface/wireguard"), key = rawWg.find(row => row.name === `${marker}-wg`)?.["private-key"] + if (key) assert.ok(!JSON.stringify(wg).includes(key), "Generated native private key must not enter MCP output") + const rawVx = await client.get[]>("/interface/vxlan") + checks.push({ host, phase: "native-vxlan-fields", rows: rawVx.map(row => Object.fromEntries([".id", "name", "l2mtu", "mtu", "arp", "arp-proxy", "mac-learning", "learning", "running", "disabled", "port"].map(field => [field, row[field]]))) }); await save() + const vx = await invokeMcpTool(principal, "mm_vxlan_state_get", { serverId }) as { tunnels: { name: string; enabled: boolean; l2mtu: number | null; dstPort: number }[]; failures: unknown[] } + assert.deepEqual(vx.failures, []); assert.equal(vx.tunnels.length, 1); assert.equal(vx.tunnels[0].enabled, false) + assert.equal(vx.tunnels[0].dstPort, 4789); assert.equal("port" in vx.tunnels[0], false) + if (rawVx[0].l2mtu === undefined) assert.equal(vx.tunnels[0].l2mtu, null) + const ike = await invokeMcpTool(principal, "mm_ipsec_state_get", { serverId }) as { servers: { peers: { name: string; disabled: boolean | null }[] }[]; failures: unknown[] } + assert.deepEqual(ike.failures, []); assert.equal(ike.servers[0].peers.length, 1); assert.equal(ike.servers[0].peers[0].disabled, true) + const gre = await invokeMcpTool(principal, "mm_gre_state_get", { serverId }) as { tunnels: { name: string; enabled: boolean | null; mtu: number | null; keepaliveInterval: number | null; keepaliveRetries: number | null; remoteAddress: string }[]; failures: unknown[]; forwardingVerified: boolean } + assert.deepEqual(gre.failures, []); assert.equal(gre.tunnels.length, 1); assert.equal(gre.tunnels[0].name, marker + "-gre") + assert.equal(gre.tunnels[0].enabled, false); assert.equal(gre.tunnels[0].mtu, 1400); assert.equal(gre.tunnels[0].remoteAddress, "198.18.255.254") + assert.equal(gre.tunnels[0].keepaliveInterval, 10); assert.equal(gre.tunnels[0].keepaliveRetries, 10); assert.equal(gre.forwardingVerified, false) + const dto = { wireguard: wg, vxlan: vx, ipsec: ike, gre } + assert.ok(!JSON.stringify(dto).includes(process.env.MMAPP_TEST_CHR_PASSWORD!)) + if (key) assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [access.connection.id])).rows).includes(key)) + checks.push({ host, phase: "populated-disabled-catalogs", keyMaterialExcluded: true, result: dto }); await save() + } finally { + for (const definition of [...definitions].reverse()) for (const row of await client.get[]>(definition.path)) if (row.name === `${marker}-${definition.suffix}` && row.comment === marker) { + assert.equal(row.disabled, "true"); await execute(`mm_${definition.resource}_delete_plan`, { serverId, id: row[".id"] }) + } + for (const snapshot of baseline) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint, "Lab VPN baseline changed") + checks.push({ host, phase: "cleanup", baselineRestored: true }); await save() + } + console.log(`✓ CHR ${serverId}: disabled GRE/WG/VXLAN/IPsec peers, populated catalogs without private keys, complete cleanup`) + } + report.complete = true; report.finishedAt = new Date().toISOString(); await save() +} finally { await closePool() } diff --git a/backend/src/modules/mcp/chr-wireguard-interface-update.ts b/backend/src/modules/mcp/chr-wireguard-interface-update.ts new file mode 100644 index 00000000..1669c6cc --- /dev/null +++ b/backend/src/modules/mcp/chr-wireguard-interface-update.ts @@ -0,0 +1,88 @@ +import assert from "node:assert/strict" +import { pool } from "../../db/index.js" +import { readWireguardInterfaceUpdateState, planWireguardInterfaceUpdate, projectWireguardInterfaceUpdate } from "../../services/wireguard-interface-update.js" +import { wireguardInterfaceDeleteValue } from "../../services/wireguard-interface-delete.js" +import { wireguardConfigurationRows } from "../../services/wireguard-peer-delete.js" +import { greStableRows } from "../../services/gre-mutation-state.js" +import { fingerprint } from "./security.js" +import { wireguardCreateKey, wireguardPublicKey } from "../../services/wireguard-interface-create.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { invokeMcpTool } from "./runtime.js" +import type { McpPrincipal } from "./store.js" + +/** Owned disabled fixtures: native identity and basic PATCH, never forwarding. */ +export async function acceptChrWireguardInterfaceUpdate(marker: string, principal: McpPrincipal, serverIds: number[], execute: (tool: string, args: Record) => Promise, checks: Record[], checkpoint: () => Promise) { + assert.match(marker, /^mcp-test-[a-f0-9]{8}$/) + for (const serverId of serverIds) { + const name = `${marker}-wg-patch-${serverId}`, renamed = name + "-renamed" + const { server, client } = await routerClient(serverId) + const baseline = await readWireguardInterfaceUpdateState(server, "*0") + const resources = ["wireguard", "wireguard_peer", "ipv4_address", "ipv6_address"] + const snapshots = await Promise.all(resources.map(resource => readRosSnapshot(serverId, resource))) + assert.ok(snapshots.every(snapshot => !snapshot.rows.some(row => row.name === name || row.name === renamed || row.comment === marker))) + checks.push({ phase: "wg-interface-update-preflight", serverId, privateBaseline: baseline.proof, baselines: snapshots.map(({ resource, fingerprint }) => ({ resource, fingerprint })) }); await checkpoint() + let id: string | undefined + try { + await execute("mm_wireguard_create_plan", { serverId, fields: { name, disabled: true, comment: marker, mtu: 1420 } }) + const own = (await readRosSnapshot(serverId, "wireguard")).rows.filter(row => row.name === name && row.comment === marker) + assert.equal(own.length, 1); assert.equal(own[0].disabled, "true"); id = String(own[0][".id"]) + const generic = await client.get[]>("/interface?.proplist=.id,name,type,disabled", 10000, undefined, 1048576) + const match = generic.filter(row => row[".id"] === id || row.name === name) + assert.equal(match.length, 1); assert.equal(match[0][".id"], id); assert.equal(match[0].name, name); assert.equal(match[0].type, "wg") + checks.push({ phase: "wg-interface-nonempty-identity", serverId, nativeId: id, genericId: match[0][".id"], type: match[0].type, disabled: true }); await checkpoint() + const patch = { name: renamed, listenPort: 51901, mtu: 1400, comment: marker + "-updated", disabled: true } + const before = await readWireguardInterfaceUpdateState(server, id) + const commands = planWireguardInterfaceUpdate(before, id, patch) + assert.equal(commands.length, 1) + const expected = projectWireguardInterfaceUpdate(before, commands[0]) + let operationId: string + try { operationId = await execute("mm_wireguard_interface_edit_plan", { serverId, interfaceId: id, patch }) } + catch (error) { + const after = await readWireguardInterfaceUpdateState(server, id) + const left = wireguardInterfaceDeleteValue(expected), right = wireguardInterfaceDeleteValue(after) + const sections = Object.keys(left).filter(key => fingerprint(left[key as keyof typeof left]) !== fingerprint(right[key as keyof typeof right])) + const differences: { collection: string; nativeId: string; fields: string[] }[] = [] + const permittedFields = new Set(["name", "mtu", "listen-port", "disabled", "comment", "public-key", "private-key", "mac-address", "arp", "arp-timeout", "default-name", "type", "dynamic"]) + const compare = (collection: string, a: Record[], b: Record[]) => { + const old = greStableRows(wireguardConfigurationRows(a)), fresh = greStableRows(wireguardConfigurationRows(b)) + for (const nativeId of new Set([...old, ...fresh].map(row => row[".id"]))) { + if (typeof nativeId !== "string" || !/^\*[a-z0-9]+$/i.test(nativeId)) throw new Error("Invalid diagnostic native ID") + const first = old.find(row => row[".id"] === nativeId), second = fresh.find(row => row[".id"] === nativeId) + if (fingerprint(first ?? null) === fingerprint(second ?? null)) continue + const keys = [...new Set([...Object.keys(first ?? {}), ...Object.keys(second ?? {})])].filter(key => first?.[key] !== second?.[key]) + differences.push({ collection, nativeId, fields: [...new Set(keys.map(key => permittedFields.has(key) ? key : "opaque-field"))] }) + } + } + compare("wireguard", expected.base.interfaces, after.base.interfaces) + for (const path of Object.keys(expected.dependencies)) compare(path, expected.dependencies[path], after.dependencies[path]) + const first = expected.base.interfaces.find(row => row[".id"] === id)!, second = after.base.interfaces.find(row => row[".id"] === id)! + const originalKey = wireguardCreateKey.safeParse(first?.["private-key"]), observedKey = wireguardCreateKey.safeParse(second?.["private-key"]) + const clamp = (value: string) => { const bytes = Buffer.from(value, "base64"); bytes[0] &= 248; bytes[31] = (bytes[31] & 127) | 64; return bytes } + const keyFacts = { originalPresent: first?.["private-key"] !== undefined, observedPresent: second?.["private-key"] !== undefined, originalValid: originalKey.success, observedValid: observedKey.success, publicIdentityUnchanged: first?.["public-key"] === second?.["public-key"], originalDerivesPublic: originalKey.success && wireguardPublicKey(originalKey.data) === first?.["public-key"], observedDerivesPublic: observedKey.success && wireguardPublicKey(observedKey.data) === second?.["public-key"], clampedScalarsEqual: originalKey.success && observedKey.success && clamp(originalKey.data).equals(clamp(observedKey.data)) } + checks.push({ phase: "wg-interface-update-projection-diagnostic", serverId, sections, differences: differences.slice(0, 100), keyFacts, sourceProofUnchanged: expected.base.serverProof === after.base.serverProof, rawValuesDisclosed: false }); await checkpoint() + throw error + } + let current = (await readRosSnapshot(serverId, "wireguard")).rows.find(row => row[".id"] === id) + assert.ok(current); assert.equal(current.name, renamed); assert.equal(current.comment, marker + "-updated"); assert.equal(Number(current["listen-port"]), 51901); assert.equal(Number(current.mtu), 1400); assert.equal(current.disabled, "true") + const result = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0 AND state='verified'", [operationId])).rows[0]?.result + assert.equal(result?.verified, true); assert.equal(result.commandCount, 1); assert.equal(result.completedCommands, 1) + const reconciled = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] } + assert.equal(reconciled.automaticRetry, false); assert.equal(reconciled.results[0]?.matchesDesiredState, true) + await execute("mm_wireguard_update_plan", { serverId, id, fields: { comment: marker, mtu: 1420, disabled: true } }) + current = (await readRosSnapshot(serverId, "wireguard")).rows.find(row => row[".id"] === id) + assert.equal(current?.comment, marker); assert.equal(Number(current?.mtu), 1420); assert.equal(current?.name, renamed); assert.equal(current?.disabled, "true") + checks.push({ phase: "wg-interface-update-verified", serverId, operationId, nativeId: id, businessAndNativeAlias: true, fields: ["name", "listenPort", "mtu", "comment", "disabled"], exactVerifiedReconcile: true, fixtureRemainedDisabled: true, forwardingVerified: false, keyRotationVerified: false, addressAndPeerRenameVerified: false }); await checkpoint() + } finally { + // A distinct approved cleanup plan observes current state; never resend an unknown PATCH. + const rows = (await readRosSnapshot(serverId, "wireguard")).rows.filter(row => [name, renamed].includes(String(row.name)) && [marker, marker + "-updated"].includes(String(row.comment))) + assert.ok(rows.length <= 1) + for (const row of rows) { + assert.equal(row.disabled, "true"); if (id !== undefined) assert.equal(row[".id"], id) + await execute("mm_wireguard_delete_plan", { serverId, id: row[".id"] }) + } + for (const snapshot of snapshots) assert.equal((await readRosSnapshot(serverId, snapshot.resource)).fingerprint, snapshot.fingerprint) + assert.equal((await readWireguardInterfaceUpdateState(server, "*0")).proof, baseline.proof) + checks.push({ phase: "wg-interface-update-baseline-restored", serverId, fingerprints: snapshots.length, privateProofRestored: true, unknownForwardNotReplayed: true, forwardingVerified: false }); await checkpoint() + } + } +} diff --git a/backend/src/modules/mcp/chr-wireguard-interface.ts b/backend/src/modules/mcp/chr-wireguard-interface.ts new file mode 100644 index 00000000..0ea6bd02 --- /dev/null +++ b/backend/src/modules/mcp/chr-wireguard-interface.ts @@ -0,0 +1,54 @@ +import assert from "node:assert/strict" +import { pool } from "../../db/index.js" +import { generateWireGuardKeyPair } from "../../services/wg-keys.js" +import { readWireguardInterfaceDeleteState } from "../../services/wireguard-interface-delete.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { invokeMcpTool } from "./runtime.js" +import type { McpPrincipal } from "./store.js" + +/** Explicit cascade on an owned disabled interface, two disabled peers and disabled v4/v6 addresses. */ +export async function acceptChrWireguardInterface(marker: string, principal: McpPrincipal, execute: (tool: string, args: Record) => Promise, checks: Record[], checkpoint: () => Promise) { + assert.match(marker, /^mcp-test-[a-f0-9]{8}$/) + const serverId = 1, name = marker + "-wg-cascade", keys = [generateWireGuardKeyPair().publicKey, generateWireGuardKeyPair().publicKey] + const { server } = await routerClient(serverId), privateBaseline = await readWireguardInterfaceDeleteState(server, "*0") + const resources = ["wireguard", "wireguard_peer", "ipv4_address", "ipv6_address"] + const baselines = await Promise.all(resources.map(resource => readRosSnapshot(serverId, resource))) + assert.ok(baselines.every(snapshot => !snapshot.rows.some(row => row.name === name || row.comment === marker))) + checks.push({ phase: "wg-interface-preflight", baselineFingerprints: baselines.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })), privateProofBaseline: privateBaseline.proof }); await checkpoint() + let cascadeStarted = false + try { + await execute("mm_wireguard_create_plan", { serverId, fields: { name, disabled: true, comment: marker } }) + const own = (await readRosSnapshot(serverId, "wireguard")).rows.filter(row => row.name === name && row.comment === marker) + assert.equal(own.length, 1); assert.equal(own[0].disabled, "true") + const id = String(own[0][".id"]) + for (const [index, publicKey] of keys.entries()) await execute("mm_wireguard_peer_create_plan", { serverId, fields: { interface: name, "public-key": publicKey, "allowed-address": index ? "2001:db8:243::2/128" : "198.19.243.2/32", disabled: true, comment: marker } }) + await execute("mm_ipv4_address_create_plan", { serverId, fields: { address: "198.19.243.1/30", interface: name, disabled: true, comment: marker } }) + await execute("mm_ipv6_address_create_plan", { serverId, fields: { address: "2001:db8:243::1/64", interface: name, disabled: true, advertise: false, comment: marker } }) + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_delete_plan", { serverId, id }), (error: { code?: string }) => error.code === "DEPENDENCY_CONFLICT") + checks.push({ phase: "wg-interface-owned-fixture", interfaceId: id, interfaceDisabled: true, peersDisabled: true, addressesDisabled: true, peers: 2, staticAddresses: 2, nativeAliasCascadeRefused: true }); await checkpoint() + cascadeStarted = true + const operationId = await execute("mm_wireguard_interface_remove_plan", { serverId, interfaceId: id }) + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operationId])).rows[0].result + assert.equal(receipt.verified, true); assert.equal(receipt.commandCount, 5); assert.equal(receipt.completedCommands, 5) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesExpectedPrefixState: boolean; verifiedPrefixCommands: number }[] } + assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, true); assert.equal(reconciliation.results[0].matchesExpectedPrefixState, true); assert.equal(reconciliation.results[0].verifiedPrefixCommands, 5) + const intents = (await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action='configuration.intent' AND payload->>'method'='DELETE'", [principal.connectionId])).rows[0].n + assert.equal(intents, 5) + checks.push({ phase: "wg-interface-cascade-verified", operationId, commandCount: receipt.commandCount, completedCommands: receipt.completedCommands, nativeDeleteIntents: intents, perCommandReadBack: true, readOnlyReconciliation: true, revisionRecorded: receipt.revisionRecorded, forwardingVerified: false }); await checkpoint() + } finally { + const target = (await readRosSnapshot(serverId, "wireguard")).rows.filter(row => row.name === name && row.comment === marker) + const trace = checks.filter(row => row.phase === "mcp-operation" && row.tool === "mm_wireguard_interface_remove_plan").at(-1) + if (target.length && cascadeStarted && trace?.state !== "rejected" && !["prepare", "approval"].includes(String(trace?.stage))) { + checks.push({ phase: "wg-interface-cleanup-unconfirmed", marker, ownedDisabledFixtureRemaining: true, uncertainCascadeNotReplayed: true }); await checkpoint() + throw new Error("Owned WireGuard cascade requires read-only reconciliation; native commands will not be repeated") + } + if (target.length) { + for (const row of (await readRosSnapshot(serverId, "wireguard_peer")).rows.filter(row => row.interface === name && row.comment === marker && keys.includes(String(row["public-key"])))) await execute("mm_wireguard_peer_delete_plan", { serverId, id: row[".id"] }) + for (const [resource, address] of [["ipv4_address", "198.19.243.1/30"], ["ipv6_address", "2001:db8:243::1/64"]]) for (const row of (await readRosSnapshot(serverId, resource)).rows.filter(row => row.interface === name && row.comment === marker && row.address === address)) await execute(`mm_${resource}_delete_plan`, { serverId, id: row[".id"] }) + for (const row of target) await execute("mm_wireguard_delete_plan", { serverId, id: row[".id"] }) + } + for (const baseline of baselines) assert.equal((await readRosSnapshot(serverId, baseline.resource)).fingerprint, baseline.fingerprint) + assert.equal((await readWireguardInterfaceDeleteState(server, "*0")).proof, privateBaseline.proof) + checks.push({ phase: "wg-interface-baseline-restored", fingerprints: baselines.length, privateProofRestored: true, staticConfigurationRestored: true, forwardingVerified: false }); await checkpoint() + } +} diff --git a/backend/src/modules/mcp/chr-wireguard-peer.ts b/backend/src/modules/mcp/chr-wireguard-peer.ts new file mode 100644 index 00000000..5f57d590 --- /dev/null +++ b/backend/src/modules/mcp/chr-wireguard-peer.ts @@ -0,0 +1,99 @@ +import assert from "node:assert/strict" +import { pool } from "../../db/index.js" +import { generateWireGuardKeyPair } from "../../services/wg-keys.js" +import { readWireguardPeerDeletionState } from "../../services/wireguard-peer-delete.js" +import { projectWireguardPeerUpdate, wireguardPeerNativeUpdate, wireguardPeerUpdateDifferences, observedWireguardPeerKeepalive } from "../../services/wireguard-peer-update.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { findAction } from "./actions.js" +import type { McpPrincipal } from "./store.js" +import { invokeMcpTool } from "./runtime.js" + +/** A disabled, addressless interface and disabled peer never participate in traffic. */ +export async function acceptChrWireguardPeer(marker: string, principal: McpPrincipal, execute: (tool: string, args: Record) => Promise, checks: Record[], checkpoint: () => Promise, updatePeer = false) { + assert.match(marker, /^mcp-test-[a-f0-9]{8}$/) + const serverId = 1, name = marker + "-wg", { publicKey } = generateWireGuardKeyPair() + const createInterface = { serverId, fields: { name, disabled: true, comment: marker } } + const createPeer = { serverId, fields: { interface: name, "public-key": publicKey, "allowed-address": "198.19.254.2/32", disabled: true, comment: marker } } + findAction("mm_wireguard_create_plan").schema.parse(createInterface) + findAction("mm_wireguard_peer_create_plan").schema.parse(createPeer) + findAction("mm_wireguard_peer_remove_plan").schema.parse({ serverId, peerId: "*1" }) + const { server } = await routerClient(serverId), privateBaseline = await readWireguardPeerDeletionState(server) + const resources = ["wireguard", "wireguard_peer", "ipv4_address", "ipv6_address"] + const baselines = await Promise.all(resources.map(resource => readRosSnapshot(serverId, resource))) + assert.ok(baselines.every(snapshot => !snapshot.rows.some(row => row.name === name || row.comment === marker))) + checks.push({ phase: "wg-peer-preflight", baselineFingerprints: baselines.map(({ serverId, resource, fingerprint }) => ({ serverId, resource, fingerprint })), privateProofBaseline: privateBaseline.proof }); await checkpoint() + let deletionStarted = false + const ownedPublicKeys = new Set([publicKey]) + try { + await execute("mm_wireguard_create_plan", createInterface) + const own = (await readRosSnapshot(serverId, "wireguard")).rows.filter(row => row.name === name && row.comment === marker) + assert.equal(own.length, 1); assert.equal(own[0].disabled, "true") + await execute("mm_wireguard_peer_create_plan", createPeer) + const peers = (await readRosSnapshot(serverId, "wireguard_peer")).rows.filter(row => row.interface === name && row.comment === marker && row["public-key"] === publicKey) + assert.equal(peers.length, 1); assert.equal(peers[0].disabled, "true") + checks.push({ phase: "wg-owned-peer-created", interfaceName: name, peerId: peers[0][".id"], peerPublicKey: publicKey, interfaceDisabled: true, peerDisabled: true }); await checkpoint() + if (updatePeer) { + const peerId = String(peers[0][".id"]), replacementKey = generateWireGuardKeyPair().publicKey + ownedPublicKeys.add(replacementKey) + const patch = { publicKey: replacementKey, allowedAddresses: ["198.19.254.9/32", "2001:db8:ffff::9/128"], endpointAddress: "192.0.2.123", endpointPort: 13232, persistentKeepalive: 60, comment: marker, name: marker + "-peer", disabled: true, clientAddress: "198.19.254.9/32", clientDns: "192.0.2.53", clientEndpoint: "192.0.2.99" } + const updateOperationId = await execute("mm_wireguard_peer_edit_plan", { serverId, peerId, patch }) + const reconciled = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: updateOperationId }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] } + assert.equal(reconciled.automaticRetry, false); assert.equal(reconciled.results[0].matchesDesiredState, true) + const clearFields = { "public-key": publicKey, "allowed-address": "198.19.254.2/32", "endpoint-address": "", "endpoint-port": 0, "persistent-keepalive": "0", comment: marker, disabled: true } + const clearDesired = projectWireguardPeerUpdate(await readWireguardPeerDeletionState(server), peerId, wireguardPeerNativeUpdate(clearFields)) + let clearOperationId: string + try { clearOperationId = await execute("mm_wireguard_peer_update_plan", { serverId, id: peerId, fields: clearFields }) } + catch (error) { + try { + const actual = await readWireguardPeerDeletionState(server) + checks.push({ phase: "wg-peer-clear-difference", fields: wireguardPeerUpdateDifferences(clearDesired, actual, peerId), nativeValuesWithheld: true }) + const selected = actual.peers.find(row => row[".id"] === peerId)! + if (selected?.interface === name && selected.comment === marker && ownedPublicKeys.has(selected["public-key"])) { + const { client } = await routerClient(serverId) + const getter = await client.post("/interface/wireguard/peers/get", { ".id": peerId, "value-name": "persistent-keepalive" }) + const ret = getter && typeof getter === "object" && "ret" in getter ? getter.ret : undefined + const present = await client.post("/interface/wireguard/peers/print", { ".proplist": [".id"], ".query": ["persistent-keepalive"] }) + const zero = await client.post("/interface/wireguard/peers/print", { ".proplist": [".id"], ".query": ["persistent-keepalive=0"] }) + const ids = (value: unknown) => Array.isArray(value) && value.every(row => row && typeof row === "object" && typeof row[".id"] === "string") ? value.map(row => row[".id"]) : null + checks.push({ phase: "wg-peer-keepalive-source", propertyPresent: Object.hasOwn(selected, "persistent-keepalive"), propertyEmpty: selected["persistent-keepalive"] === "", propertySeconds: observedWireguardPeerKeepalive(selected["persistent-keepalive"] ?? ""), getterShape: Array.isArray(getter) ? getter.length === 0 ? "empty-array" : "array" : "object", getterEmpty: ret === "", getterSeconds: typeof ret === "string" ? observedWireguardPeerKeepalive(ret) : null, selectedInPresentQuery: ids(present)?.includes(peerId) ?? null, selectedInZeroQuery: ids(zero)?.includes(peerId) ?? null, nativeValuesWithheld: true }) + } + } catch { checks.push({ phase: "wg-peer-clear-difference", sourceUnavailable: true, nativeValuesWithheld: true }) } + await checkpoint(); throw error + } + // Empty fields are genuine clears; the public service retains every other field. + const current = (await readWireguardPeerDeletionState(server)).peers.find(row => row[".id"] === peerId)! + assert.equal(current["public-key"], publicKey); assert.equal(current["endpoint-address"] ?? "", "") + const path = "/interface/wireguard/peers/" + encodeURIComponent(peerId) + const updateIntents = (await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action='configuration.intent' AND payload->>'method'='PATCH' AND payload->>'path'=$2", [principal.connectionId, path])).rows[0].n + assert.equal(updateIntents, 2) + checks.push({ phase: "wg-peer-update-verified", updateOperationId, clearOperationId, patchIntents: updateIntents, publicFieldsUpdated: Object.keys(patch).length, readOnlyReconciliation: true, parentAndForeignPeersPreserved: true, disabledInterfaceAndPeer: true, configuredAddressesUnchanged: true, forwardingVerified: false }); await checkpoint() + } + deletionStarted = true + const operationId = await execute("mm_wireguard_peer_remove_plan", { serverId, peerId: peers[0][".id"] }) + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operationId])).rows[0].result + assert.equal(receipt.verified, true) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesOriginalState: boolean }[] } + assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results.length, 1) + assert.equal(reconciliation.results[0].matchesDesiredState, true); assert.equal(reconciliation.results[0].matchesOriginalState, false) + const path = "/interface/wireguard/peers/" + encodeURIComponent(String(peers[0][".id"])) + const intents = (await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action='configuration.intent' AND payload->>'method'='DELETE' AND payload->>'path'=$2", [principal.connectionId, path])).rows[0].n + assert.equal(intents, 1) + assert.ok(!(await readRosSnapshot(serverId, "wireguard_peer")).rows.some(row => row[".id"] === peers[0][".id"])) + assert.equal((await readRosSnapshot(serverId, "wireguard")).rows.find(row => row[".id"] === own[0][".id"])!.disabled, "true") + checks.push({ phase: "wg-peer-deletion-verified", operationId, deleteIntents: intents, nativeReadBack: true, readOnlyReconciliation: true, parentPreserved: true, revisionRecorded: receipt.revisionRecorded, forwardingVerified: false }); await checkpoint() + } finally { + const remaining = (await readRosSnapshot(serverId, "wireguard_peer")).rows.filter(row => row.comment === marker && row.interface === name && ownedPublicKeys.has(String(row["public-key"]))) + const deletionTrace = checks.filter(row => row.phase === "mcp-operation" && row.tool === "mm_wireguard_peer_remove_plan").at(-1) + const mayCleanPeer = !deletionStarted || !deletionTrace?.operationId || deletionTrace.state === "rejected" + if (remaining.length && !mayCleanPeer) { + checks.push({ phase: "wg-peer-cleanup-unconfirmed", marker, ownedDisabledFixtureRemaining: true, uncertainDeleteNotReplayed: true }); await checkpoint() + throw new Error("Owned WireGuard fixture requires reconciliation; uncertain DELETE will not be repeated") + } + for (const row of remaining) await execute("mm_wireguard_peer_delete_plan", { serverId, id: row[".id"] }) + assert.ok(!(await readRosSnapshot(serverId, "wireguard_peer")).rows.some(row => row.interface === name)) + for (const row of (await readRosSnapshot(serverId, "wireguard")).rows.filter(row => row.name === name && row.comment === marker)) await execute("mm_wireguard_delete_plan", { serverId, id: row[".id"] }) + for (const baseline of baselines) assert.equal((await readRosSnapshot(serverId, baseline.resource)).fingerprint, baseline.fingerprint) + assert.equal((await readWireguardPeerDeletionState(server)).proof, privateBaseline.proof) + checks.push({ phase: "wg-peer-baseline-restored", fingerprints: baselines.length, privateProofRestored: true, configuredAddressesUnchanged: true, trafficVerified: false }); await checkpoint() + } +} diff --git a/backend/src/modules/mcp/config-history.ts b/backend/src/modules/mcp/config-history.ts new file mode 100644 index 00000000..d731fb8d --- /dev/null +++ b/backend/src/modules/mcp/config-history.ts @@ -0,0 +1,18 @@ +import { z } from "zod" +import { CONFIG_SECTIONS, CONFIG_REVISION_KEEP, listRevisions } from "../../services/config-revisions.js" +import type { add } from "./actions.js" + +export function registerConfigHistory(register: typeof add) { + for (const section of CONFIG_SECTIONS) { + const permissionSection = section === "filters" ? "filters" : "network" + register(`config.${section}.revisions.read`, `mm_${section.replaceAll("-", "_")}_revisions_get`, `История конфигурации: ${section}`, permissionSection, `mm:${permissionSection}:read`, "read", "servers", z.object({ serverId: z.number().int().positive().max(Number.MAX_SAFE_INTEGER) }).strict(), { + read: async input => { + // Shared REST service returns metadata only, never the stored payload + // (WireGuard/IPsec revisions may contain credentials). Scope evaluation + // happens before this server-specific SQL query. + const revisions = await listRevisions(Number(input.serverId), section) + return { revisions, retentionLimit: CONFIG_REVISION_KEEP, incomplete: revisions.length === CONFIG_REVISION_KEEP } + }, + }) + } +} diff --git a/backend/src/modules/mcp/control-lane.ts b/backend/src/modules/mcp/control-lane.ts new file mode 100644 index 00000000..d1813ac5 --- /dev/null +++ b/backend/src/modules/mcp/control-lane.ts @@ -0,0 +1,26 @@ +/** Caller passes the ACTUAL authorized registration descriptors, never caller + * output/state/id or an external list. Lane selection grants no authority: + * existing schema/auth/policy/runtime checks still run in the SDK callback. + */ +interface RegisteredOperation { id: string; toolName: string; effect: string } +const tools: Readonly> = Object.freeze({ + mm_capabilities_get: { id: "capabilities.read", effect: "read" }, + mm_change_get: { id: "changes.read", effect: "read" }, + mm_operation_get: { id: "operations.read", effect: "read" }, + mm_server_probe_receipt_get: { id: "servers.probes.receipt.read", effect: "read" }, + mm_change_cancel: { id: "changes.cancel", effect: "read" }, + mm_server_probe_cancel: { id: "servers.probes.cancel", effect: "write" }, +}) +const protocols = new Set(["initialize", "ping", "tools/list", "notifications/initialized", "notifications/cancelled"]) +export function classifyMcpLane(body: unknown, authorizedRegistrations: readonly RegisteredOperation[]): "control" | "normal" { + const item: unknown = Array.isArray(body) && body.length === 1 ? body[0] : body + if (!item || typeof item !== "object" || Array.isArray(item)) return "normal" + const request = item as { method?: unknown; params?: unknown } + if (typeof request.method !== "string") return "normal" + if (protocols.has(request.method)) return "control" + if (request.method !== "tools/call" || !request.params || typeof request.params !== "object") return "normal" + const name = (request.params as { name?: unknown }).name + if (typeof name !== "string" || !Object.hasOwn(tools, name)) return "normal" + const expected = tools[name] + return authorizedRegistrations.some(row => row.toolName === name && row.id === expected.id && row.effect === expected.effect) ? "control" : "normal" +} diff --git a/backend/src/modules/mcp/control-path-context-acceptance.ts b/backend/src/modules/mcp/control-path-context-acceptance.ts new file mode 100644 index 00000000..180535e1 --- /dev/null +++ b/backend/src/modules/mcp/control-path-context-acceptance.ts @@ -0,0 +1,144 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { managedComment } from "../../managed-markers.js" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "../../services/partition-write-transaction.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js" +import { invokeMcpTool } from "./runtime.js" +import { requireRecentPath } from "./path-verification.js" +import { readRouteCommunityContext } from "./routing-community-context.js" + +/** Disposable SQL and loopback HTTP only; no CHR or native mutations. */ +export async function verifyMcpControlPathContext(actor: AuthUser) { + const sid = 134 + let requests = 0, bgpReads = 0, probes = 0, writes = 0, counter = 0, bgpPresent = false, listMember = false + let packetLoss = "0" + let opaque = "NEVER_CONTROL_PATH_NATIVE", afterTrace: (() => Promise) | undefined, afterRead: (() => Promise) | undefined + const body = compileManagedFilterRules([{ community: "65000:21", gateway: "192.0.2.1", outIface: "", action: "route", description: "" }]) + const collections = (path: string): Record[] => ({ + "/routing/table": ["main", "diag", "policy-only"].map((name, index) => ({ ".id": "*t" + index, name, fib: "true", dynamic: name === "diag" ? "true" : "false" })), + "/ip/vrf": [{ ".id": "*vd", name: "diag", interfaces: listMember ? "WAN-LIST" : "wan", disabled: "false" }, { ".id": "*vm", name: "main", interfaces: "all", disabled: "false" }], + "/interface": ["wan", "hop", "target"].map((name, index) => ({ ".id": "*if" + index, name, disabled: "false", running: "true", "rx-byte": String(++counter), opaque })), + "/ip/address": ["wan", "hop", "target"].map((iface, index) => ({ ".id": "*a" + index, interface: iface, address: "192.0.2." + (index + 1) + "/32", disabled: "false", invalid: "false" })), + "/routing/bgp/connection": [{ ".id": "*c", name: "peer", instance: "default", vrf: "main", "routing-table": "policy-only", afi: "ip", "input.filter": "bgp-in", "output.filter-chain": "", templates: "", disabled: "false" }], + "/routing/bgp/template": [], "/routing/bgp/instance": [{ ".id": "*i", name: "default", vrf: "main", "routing-table": "main", disabled: "false" }], + "/routing/filter/rule": [{ ".id": "*f", chain: "bgp-in", disabled: "false", comment: managedComment("NEVER_CONTROL_PATH_COMMENT"), rule: body }], + "/ip/route": [{ ".id": "*r", "dst-address": "192.0.2.1/32", "routing-table": "main", disabled: "false", comment: managedComment("recursive") }], + } as Record[]>)[path] ?? [] + const native = createServer(async (request, response) => { + requests++; response.setHeader("content-type", "application/json") + const path = (request.url ?? "").replace(/^\/rest/, "") + if (afterRead) { const hook = afterRead; afterRead = undefined; await hook() } + if (path.startsWith("/routing/bgp")) { bgpReads++; if (!bgpPresent) { response.statusCode = 404; response.end("{}"); return } } + if (request.method === "GET") { response.end(JSON.stringify(collections(path))); return } + let raw = ""; for await (const part of request) raw += part + const args = raw ? JSON.parse(raw) : {} + if (request.method === "POST" && path.endsWith("/get")) { + const value = collections(path.slice(0, -4)).find(row => row[".id"] === args[".id"])?.[args["value-name"]] + response.end(JSON.stringify(value === undefined ? [] : { ret: value })); return + } + if (request.method === "POST" && ["/tool/ping", "/tool/traceroute"].includes(path)) { + probes++; assert.equal(args.vrf, "diag"); assert.equal(args.interface, "wan"); assert.equal(args["src-address"], "192.0.2.1") + if (path === "/tool/ping") { assert.equal(args.count, "3"); response.end(JSON.stringify([{ sent: "3", received: "3", "packet-loss": packetLoss, "avg-rtt": "1ms" }])); return } + if (afterTrace) { const hook = afterTrace; afterTrace = undefined; await hook() } + response.end(JSON.stringify(["192.0.2.2", "192.0.2.3"].map(address => ({ address, sent: "1", loss: "0" })))); return + } + writes++; response.statusCode = 405; response.end("{}") + }) + await new Promise(resolve => native.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined + try { + assert.equal((await pool.query("SELECT count(*)::int AS count FROM servers WHERE id=$1", [sid])).rows[0].count, 0) + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'control-path-context','127.0.0.1',$2,false,'NEVER_CONTROL_PATH_LOGIN')", [sid, (native.address() as { port: number }).port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Control path purpose", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [sid], operationIds: ["routing.path.verify"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const profile = routeAiProfileSchema.parse({ name: "diagnostic is not Community forwarding", serverId: sid, family: "ipv4", community: "65000:21", table: "policy-only", probeVrf: "diag", bgpContext: { connectionId: "*c", instance: "default", inputChain: "bgp-in", vrf: "main" }, candidates: [{ id: "a", jumpHostId: sid, exitNodeId: sid, wanInterface: "wan", downstreamInterface: "hop", gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*r" }, controlProbe: { sourceAddress: "192.0.2.1", targetInterface: "target", targetAddress: "192.0.2.3", expectedHops: [{ serverId: sid, interface: "hop", address: "192.0.2.2" }, { serverId: sid, interface: "target", address: "192.0.2.3" }] } }] }) + const check = () => invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "a", proofPurpose: "control-path" }) as Promise<{ id: string; status: string; reason?: string; proofPurpose: string; proofVersion: number; proofScope: string; returnPathVerified: boolean }> + const passed = await check() + assert.equal(passed.status, "passed"); assert.equal(passed.proofPurpose, "control-path"); assert.equal(passed.proofVersion, 2) + assert.equal(passed.proofScope, "observed-control-probes-only"); assert.equal(passed.returnPathVerified, false); assert.equal(bgpReads, 0); assert.equal(probes, 2) + const blocker = await pool.connect() + let pending: ReturnType | undefined + try { + await blocker.query("BEGIN") + await blocker.query("SELECT pg_advisory_xact_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + pending = check() + void pending.catch(() => {}) + const deadline = Date.now() + 2500 + let waiting = false + while (Date.now() < deadline) { + const { rows } = await blocker.query("SELECT 1 FROM pg_locks WHERE locktype='advisory' AND database=(SELECT oid FROM pg_database WHERE datname=current_database()) AND classid=0 AND objid=$1::oid AND objsubid=1 AND mode='ShareLock' AND NOT granted", [PARTITION_WRITER_ADVISORY_KEY]) + if (rows.length) { waiting = true; break } + await new Promise(resolve => setTimeout(resolve, 20)) + } + assert(waiting, "path proof publication must wait for the partition writer lock") + const { rows: [before] } = await blocker.query("SELECT count(*)::int AS count FROM mcp_path_checks WHERE connection_id=$1", [connectionId]) + assert.equal(before.count, 1) + } finally { + try { await blocker.query("ROLLBACK") } finally { blocker.release() } + } + assert.equal((await pending)?.status, "passed") + const beforeMalformed = probes + packetLoss = "0NEVER_CONTROL_PATH_LOSS" + const malformed = await check() + packetLoss = "0" + assert.equal(malformed.status, "unknown") + assert.equal(malformed.reason, "PING_COUNTERS_INCONSISTENT") + assert.equal(probes, beforeMalformed + 1, "malformed ping must never reach traceroute") + const [storedMalformed] = (await pool.query("SELECT status,evidence FROM mcp_path_checks WHERE id=$1", [malformed.id])).rows + assert.equal(storedMalformed.status, "unknown") + assert.equal(storedMalformed.evidence.reason, malformed.reason) + assert.equal(typeof storedMalformed.evidence.proof, "string") + assert.ok(!JSON.stringify(storedMalformed.evidence).includes("NEVER_CONTROL_PATH_LOSS")) + const beforeAdopt = requests + await assert.rejects(requireRecentPath(profile, "a", connectionId), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN") + assert.equal(requests, beforeAdopt, "a control receipt is rejected before any native Community acquisition") + const saved = (await pool.query("SELECT evidence FROM mcp_path_checks WHERE id=$1", [passed.id])).rows[0].evidence + await pool.query("UPDATE mcp_path_checks SET evidence=evidence-'proofPurpose'-'proofVersion' WHERE id=$1", [passed.id]) + await assert.rejects(requireRecentPath(profile, "a", connectionId), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN") + assert.equal(requests, beforeAdopt, "legacy evidence without a purpose is not upgraded") + await pool.query("UPDATE mcp_path_checks SET evidence=$2::jsonb WHERE id=$1", [passed.id, JSON.stringify({ ...saved, proofPurpose: "community" })]) + bgpPresent = true + await assert.rejects(requireRecentPath(profile, "a", connectionId), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN", "relabeling the purpose cannot cross the HMAC domain") + const privateContext = await readRouteCommunityContext(profile) + assert.equal(privateContext.before.scopedExecutionAvailable, true); assert.equal(privateContext.before.executionAvailable, false) + assert.ok(privateContext.ownedTarget); assert.equal(privateContext.ownedTarget.desiredTargetProof(body), privateContext.before.targetRuleProof) + const desiredBody = compileManagedFilterRules([{ community: "65000:21", gateway: "192.0.2.9", outIface: "", action: "route", description: "" }]) + const desiredProof = privateContext.ownedTarget.desiredTargetProof(desiredBody) + privateContext.ownedTarget.rawRow.comment = "mutated exposed clone" + assert.equal(privateContext.ownedTarget.desiredTargetProof(desiredBody), desiredProof, "the desired seal uses an immutable original row") + assert.ok(!JSON.stringify(privateContext.before).includes("NEVER_")); assert.ok(!("ownedTarget" in privateContext.before)) + await pool.query("UPDATE mcp_path_checks SET evidence=$2::jsonb WHERE id=$1", [passed.id, JSON.stringify(saved)]) + bgpPresent = false + const beforeDefault = probes + const defaultPurpose = await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "a" }) as { status: string; proofPurpose: string } + assert.equal(defaultPurpose.proofPurpose, "community"); assert.equal(defaultPurpose.status, "unknown"); assert.equal(probes, beforeDefault) + listMember = true + assert.equal((await check()).status, "unknown", "an unknown earlier interface list cannot fall through to main/all") + listMember = false + afterTrace = async () => { opaque = "NEVER_CONTROL_PATH_DRIFT" } + assert.equal((await check()).reason, "STATE_CONFLICT") + let finalRequests = 0 + afterTrace = async () => { await pool.query("UPDATE servers SET password='NEVER_CONTROL_PATH_ROTATED' WHERE id=$1", [sid]); finalRequests = requests } + assert.equal((await check()).reason, "STATE_CONFLICT"); assert.equal(requests, finalRequests, "source drift is checked before polling a replacement endpoint") + const cancelled = new AbortController(), beforeCancel = requests + afterRead = async () => { cancelled.abort() } + await assert.rejects(invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "a", proofPurpose: "control-path" }, cancelled.signal)) + assert.equal(requests, beforeCancel + 1) + afterRead = () => setConnectionStatus(connectionId!, "paused", actor) + const beforeRevoke = requests + await assert.rejects(check()); assert.equal(requests, beforeRevoke + 1) + assert.equal(writes, 0) + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows).includes("NEVER_")) + console.log("✓ Control path context: BGP-absent bounded probes, independent VRF/IP/interface/hops, explicit purpose and HMAC isolation from Community and legacy evidence, scoped private target API, late dependency/source/cancel/revocation fences, return-path unknown, private results and zero writes") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=$1", [sid]) + native.closeAllConnections(); await new Promise(resolve => native.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/control-path-context.ts b/backend/src/modules/mcp/control-path-context.ts new file mode 100644 index 00000000..065575fb --- /dev/null +++ b/backend/src/modules/mcp/control-path-context.ts @@ -0,0 +1,88 @@ +import { isConfirmedRoutingFibTable } from "../../services/routing-table-fib.js" +import type { RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { nativeBgpRows, type NativeBgpRow } from "../../services/bgp-export-model.js" +import { stableBgpPolicyRow } from "../../services/bgp-filter-binding-model.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { fingerprint, McpError, privateFingerprinter, constantEqual } from "./security.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { routeContextClients } from "./routing-community-context-native.js" +import { readMeasurementAddress } from "./routing-measurement-address.js" +import type { RouteContextOptions } from "./routing-community-context.js" +import { controlPathVrfOwner } from "./control-path-model.js" +import { pathProofTag } from "./path-proof-purpose.js" + +/** A diagnostic proof has no BGP/community dependency and cannot authorize a + * Community switch. Its endpoint, membership and address evidence is private. */ +export async function readControlPathContext(profile: RouteAiProfile, candidateId: string, options: RouteContextOptions = {}) { + try { + const candidate = profile.candidates.find(candidate => candidate.id === candidateId), probe = candidate?.controlProbe + if (!candidate || !probe) throw new McpError("MEASUREMENT_UNKNOWN", "Контрольный probe пути не настроен", 409) + const authorize = async () => { + if (options.signal?.aborted) throw new McpError("READ_CANCELLED", "Контрольный путь отменён", 409) + await options.authorize?.() + } + await authorize() + const ids = [...new Set([profile.serverId, candidate.jumpHostId, candidate.exitNodeId, ...probe.expectedHops.map(hop => hop.serverId)])].sort((a, b) => a - b) + const endpoints = await readRoutingEndpoints(ids) + if (endpoints.length !== ids.length || endpoints.some(endpoint => !endpoint.enabled)) throw new McpError("DEVICE_UNAVAILABLE", "Источник контрольного пути отсутствует или отключён", 409) + const sources = await createRoutingSourceContext(endpoints) + if (options.expectedSources && fingerprint(sources.expected) !== fingerprint(options.expectedSources)) throw new McpError("STATE_CONFLICT", "Источник контрольного пути изменился; новый endpoint не опрашивается", 409) + const sign = await privateFingerprinter("route-control-path-context-v1"), clients = routeContextClients(endpoints, options.signal) + const fence = async () => { await authorize(); await sources.assertCurrent() } + const snapshots: unknown[] = [], cache = new Map() + let rowsRead = 0 + const get = async (serverId: number, path: string) => { + const key = serverId + path, cached = cache.get(key) + if (cached) return cached + await fence() + const rows = nativeBgpRows(await clients.get(serverId)!.get(path)) + if (rows.length > 1000 || (rowsRead += rows.length) > 10000) throw new McpError("LIMIT_EXCEEDED", "Каталог контрольного пути превышает лимит", 409) + cache.set(key, rows) + snapshots.push({ serverId, path, rows: rows.map(row => path === "/interface" ? Object.fromEntries(Object.entries(row).filter(([key]) => !/^(?:rx-|tx-|fp-|last-link-|link-downs$|driver-)/.test(key))) : stableBgpPolicyRow(row)) }) + return rows + } + const post = async (serverId: number, path: string, body: Record) => { + await fence() + const value = await clients.get(serverId)!.post(path, body) + snapshots.push({ serverId, path, body, value }); return value + } + const enabled = async (serverId: number, path: string, row: NativeBgpRow) => { + const [effective] = await readOspfEffectiveDisabled([row], id => post(serverId, path + "/get", { ".id": id, "value-name": "disabled" })) + if (![false, "false", "no"].includes(effective.disabled as string | boolean) || [true, "true", "yes"].includes(effective.invalid as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Зависимость контрольного пути отключена или неизвестна", 409) + return effective + } + const unique = (rows: NativeBgpRow[], name: string) => { + const matching = rows.filter(row => row.name === name) + if (matching.length !== 1) throw new McpError("STATE_UNAVAILABLE", "Имя зависимости контрольного пути неоднозначно", 409) + return matching[0] + } + const sourceId = profile.serverId, table = unique(await get(sourceId, "/routing/table"), profile.probeVrf) + if (!isConfirmedRoutingFibTable(table)) throw new McpError("STATE_UNAVAILABLE", "FIB probe VRF контрольного пути не подтверждён", 409) + const vrfs = await get(sourceId, "/ip/vrf"), effectiveVrfs: NativeBgpRow[] = [] + await enabled(sourceId, "/ip/vrf", unique(vrfs, profile.probeVrf)) + for (const row of vrfs) effectiveVrfs.push([true, "true", "yes"].includes(row.disabled as string | boolean) ? row : await enabled(sourceId, "/ip/vrf", row)) + const interfaces = await get(sourceId, "/interface") + await enabled(sourceId, "/interface", unique(interfaces, candidate.wanInterface)) + if (controlPathVrfOwner(effectiveVrfs, new Set(interfaces.map(row => String(row.name))), candidate.wanInterface) !== profile.probeVrf) throw new McpError("STATE_CONFLICT", "WAN интерфейс не принадлежит probe VRF контрольного пути", 409) + const address = async (serverId: number, iface: string, selected?: string) => { + await enabled(serverId, "/interface", unique(await get(serverId, "/interface"), iface)) + const path = profile.family === "ipv4" ? "/ip/address" : "/ipv6/address" + const result = await readMeasurementAddress(await get(serverId, path), { dstInterface: iface, dstAddress: selected, family: profile.family }, id => post(serverId, path + "/get", { ".id": id, "value-name": "disabled" })) + if (result.linkLocal) throw new McpError("STATE_UNAVAILABLE", "Link-local контрольный путь требует отдельного scoped probe", 409) + return result.address + } + const destination = await address(candidate.exitNodeId, probe.targetInterface, probe.targetAddress), expected: string[] = [] + for (const hop of probe.expectedHops) expected.push(await address(hop.serverId, hop.interface, hop.address)) + const source = await address(sourceId, candidate.wanInterface, probe.sourceAddress) + await fence() + const dependencyProof = sign("dependencies:" + fingerprint({ profile, candidateId, sources: sources.expected, snapshots })) + const before = { contextVerified: true, proofPurpose: "control-path" as const, probeVrf: profile.probeVrf, family: profile.family, dependencyProof, targetRuleProof: null, sourceProofs: sources.expected } + return { before, clients, fence, probeEndpoints: new Map([[candidateId, { destination, expected, source }]]), + signEvidence: (value: unknown) => sign(pathProofTag("control-path") + fingerprint({ dependencyProof, value })), + verifyEvidence: (value: unknown, proof: unknown) => typeof proof === "string" && constantEqual(sign(pathProofTag("control-path") + fingerprint({ dependencyProof, value })), proof), + } + } catch (error) { + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Не подтверждены ограниченные зависимости контрольного пути", 409) + } +} diff --git a/backend/src/modules/mcp/control-path-model.test.ts b/backend/src/modules/mcp/control-path-model.test.ts new file mode 100644 index 00000000..7ae93a97 --- /dev/null +++ b/backend/src/modules/mcp/control-path-model.test.ts @@ -0,0 +1,28 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { controlPathVrfOwner } from "./control-path-model.js" +import { isCommunityPathEvidence, pathProofTag, PATH_PROOF_VERSION } from "./path-proof-purpose.js" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" + +test("control-path and Community proofs require explicit distinct purpose and version", () => { + assert.notEqual(pathProofTag("control-path"), pathProofTag("community")) + assert.equal(isCommunityPathEvidence({ proofPurpose: "community", proofVersion: PATH_PROOF_VERSION }), true) + for (const value of [{}, { proofPurpose: "control-path", proofVersion: PATH_PROOF_VERSION }, { proofPurpose: "community" }, { proofPurpose: "community", proofVersion: 1 }]) assert.equal(isCommunityPathEvidence(value), false) +}) +test("control-path membership respects row order and refuses unknown earlier interface lists", () => { + const interfaces = new Set(["wan", "other"]), main = { name: "main", interfaces: "all", disabled: "false" }, diag = { name: "diag", interfaces: "wan", disabled: "false" } + assert.equal(controlPathVrfOwner([diag, main], interfaces, "wan"), "diag") + assert.equal(controlPathVrfOwner([main, diag], interfaces, "wan"), "main") + assert.equal(controlPathVrfOwner([{ ...diag, disabled: "true" }, main], interfaces, "wan"), "main") + for (const row of [{ ...diag, interfaces: "WAN-LIST" }, { ...diag, disabled: undefined }, { ...diag, invalid: "true" }]) assert.throws(() => controlPathVrfOwner([row, main], interfaces, "wan")) + assert.throws(() => controlPathVrfOwner([main], interfaces, "missing")) +}) +test("path admission restricts each hop to its candidate devices before context acquisition", () => { + const candidate = { id: "a", wanInterface: "wan", downstreamInterface: "down", jumpHostId: 2, exitNodeId: 3, gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*r" }, controlProbe: { targetInterface: "target", expectedHops: [{ serverId: 1, interface: "wan" }, { serverId: 2, interface: "hop" }, { serverId: 3, interface: "target" }] } } + const profile = { name: "scoped hops", serverId: 1, community: "65000:21", family: "ipv4", candidates: [candidate, { ...candidate, id: "b", jumpHostId: 4, exitNodeId: 5, controlProbe: undefined }] } + assert.equal(routeAiProfileSchema.safeParse(profile).success, true) + for (const serverId of [4, 99]) { + const invalid = { ...profile, candidates: [{ ...candidate, controlProbe: { ...candidate.controlProbe, expectedHops: [{ serverId, interface: "foreign" }] } }, profile.candidates[1]] } + assert.equal(routeAiProfileSchema.safeParse(invalid).success, false, "a hop outside the selected candidate cannot extend the credential scope, including another candidate's device") + } +}) diff --git a/backend/src/modules/mcp/control-path-model.ts b/backend/src/modules/mcp/control-path-model.ts new file mode 100644 index 00000000..2567e7b7 --- /dev/null +++ b/backend/src/modules/mcp/control-path-model.ts @@ -0,0 +1,19 @@ +import { canonicalVrfMembers } from "../../services/vrf-members.js" +import { McpError } from "./security.js" + +/** Native VRF row order is significant. Unknown selectors cannot be skipped in + * favor of a later main/all row; disabled rows are explicitly excluded. */ +export function controlPathVrfOwner(vrfs: readonly Record[], interfaceNames: ReadonlySet, iface: string) { + if (!interfaceNames.has(iface)) throw new McpError("STATE_UNAVAILABLE", "Интерфейс контрольного пути отсутствует", 409) + for (const row of vrfs) { + if ([true, "true", "yes"].includes(row.disabled as string | boolean)) continue + if (![false, "false", "no"].includes(row.disabled as string | boolean) || [true, "true", "yes"].includes(row.invalid as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Состояние VRF контрольного пути не подтверждено", 409) + const members = canonicalVrfMembers(row.interfaces).split(",") + if (members.some(member => !["all", "none"].includes(member) && !interfaceNames.has(member))) throw new McpError("STATE_UNAVAILABLE", "VRF interface-list membership контрольного пути не подтверждено", 409) + if (members.includes("all") || members.includes(iface)) { + if (typeof row.name !== "string" || !row.name) throw new McpError("STATE_UNAVAILABLE", "Имя VRF контрольного пути не подтверждено", 409) + return row.name + } + } + return null +} diff --git a/backend/src/modules/mcp/coverage.test.ts b/backend/src/modules/mcp/coverage.test.ts new file mode 100644 index 00000000..de5913ae --- /dev/null +++ b/backend/src/modules/mcp/coverage.test.ts @@ -0,0 +1,16 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { coverageSchema, collectApplicationRoutes, routeKey } from "./coverage.js" +import { listActions } from "./actions.js" + +const coverage = coverageSchema.parse(JSON.parse(await readFile(new URL("../../../mcp-coverage.json", import.meta.url), "utf8"))) +const actual = await collectApplicationRoutes() +assert.equal(new Set(coverage.routes.map(routeKey)).size, coverage.routes.length, "Duplicate coverage route") +assert.deepEqual(coverage.routes.map(routeKey).sort(), actual.map(routeKey).sort(), "REST routes changed: update mcp-coverage.json with a typed tool, human-only reason, or explicit pending adapter") +const implemented = new Set(listActions().map(action => action.operation.id)) +for (const route of coverage.routes) { + assert.equal(route.status === "mediated", route.operations.length > 0, routeKey(route)) + for (const operation of route.operations) assert.ok(implemented.has(operation), `Unknown tool mapping: ${operation} for ${routeKey(route)}`) + if (route.status === "excluded") assert.match(route.path, /^(?:\/api\/(?:mcp(?:\/|$)|auth\/config$)|\/mcp$|\/oauth\/|\/\.well-known\/|\/_internal\/mcp\/|\/health$)/, "Business endpoint cannot be silently excluded") +} +console.log("MCP REST coverage:", JSON.stringify({ tools: listActions().length, routes: actual.length, mediated: coverage.routes.filter(route => route.status === "mediated").length, excluded: coverage.routes.filter(route => route.status === "excluded").length, pending: coverage.routes.filter(route => route.status === "pending").length })) diff --git a/backend/src/modules/mcp/coverage.ts b/backend/src/modules/mcp/coverage.ts new file mode 100644 index 00000000..bb608b65 --- /dev/null +++ b/backend/src/modules/mcp/coverage.ts @@ -0,0 +1,18 @@ +import { z } from "zod" +import { buildApp } from "../../index.js" + +export const coverageEntrySchema = z.object({ method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]), path: z.string().min(1), status: z.enum(["mediated", "excluded", "pending"]), operations: z.array(z.string()).max(100), reason: z.string().min(15) }).strict() +export const coverageSchema = z.object({ version: z.literal(1), routes: z.array(coverageEntrySchema) }).strict() +export type CoverageEntry = z.infer +export const routeKey = (route: Pick) => route.method + " " + route.path +export async function collectApplicationRoutes() { + const routes = new Map>() + const app = await buildApp({ logger: false, startScheduler: false, onRoute: route => { + for (const method of Array.isArray(route.method) ? route.method : [route.method]) { + if (!["GET", "POST", "PUT", "PATCH", "DELETE"].includes(method)) continue + const item = { method: method as CoverageEntry["method"], path: route.url } + routes.set(routeKey(item), item) + } + } }) + try { await app.ready(); return [...routes.values()].sort((a, b) => routeKey(a).localeCompare(routeKey(b))) } finally { await app.close() } +} diff --git a/backend/src/modules/mcp/database-backup-actions.ts b/backend/src/modules/mcp/database-backup-actions.ts new file mode 100644 index 00000000..009fa892 --- /dev/null +++ b/backend/src/modules/mcp/database-backup-actions.ts @@ -0,0 +1,52 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { fingerprint, McpError, redact } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import { registerMcpService } from "./service-registry.js" +import { createDatabaseBackupArtifact, databaseBackupAuthority, databaseBackupAuthoritySchema, readDatabaseBackupSource, verifyDatabaseBackupArtifact } from "./database-backup-artifact.js" +import type { add } from "./actions.js" + +export function registerDatabaseBackupActions(register: typeof add) { + const authority = (input: Record) => databaseBackupAuthoritySchema.parse(input.authority) + const current = async (input: Record) => ({ proof: (await readDatabaseBackupSource(authority(input))).proof }) + const verify = async (input: Record, result: unknown) => { + const receipt = result as { artifactId?: string; humanDownloadOnly?: boolean; databaseContentReturned?: boolean; bytes?: unknown; sha256?: unknown } | null + return receipt?.humanDownloadOnly === true && receipt.databaseContentReturned === false && !!receipt.artifactId && await verifyDatabaseBackupArtifact(receipt.artifactId, authority(input), String(input.expectedProof), String(input.attemptRef), receipt) + } + registerMcpService("database.backup", { current, + execute: input => createDatabaseBackupArtifact(authority(input), String(input.expectedProof), z.string().uuid().parse(input.attemptRef)), + verify, + reconcile: async (input, authorize) => { + await authorize() + const access = authority(input), attemptRef = z.string().uuid().parse(input.attemptRef) + const rows = (await pool.query(`SELECT s.intended_state,s.result FROM mcp_operation_steps s + JOIN mcp_operations o ON o.id=s.operation_id JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + WHERE o.connection_id=$1 AND p.operation_id='database.backup' AND s.state='verified' + AND s.intended_state->>'kind'='service' AND s.intended_state->>'service'='database.backup' + AND s.intended_state->'input'->>'attemptRef'=$2 + AND octet_length(s.intended_state::text)<=32768 AND octet_length(s.result::text)<=4096 LIMIT 2`, [access.principal.connectionId, attemptRef])).rows + await authorize() + // Creating a dump leaves topology metadata unchanged. Only the exact saved + // verified receipt proves export; a missing acknowledgement stays unresolved. + const exact = rows.length === 1 && fingerprint(rows[0].intended_state) === fingerprint(redact({ kind: "service", service: "database.backup", input })) + const desired = exact && await verify(input, rows[0].result).catch(() => false) + await authorize() + return { matchesDesiredState: desired, matchesOriginalState: false } + }, + }) + register("database.backup", "mm_database_backup_plan", "Создать полную резервную копию PostgreSQL для скачивания владельцем после подтверждения", "settings", "mm:settings:admin", "write", "global", z.object({}).strict(), { + prepare: async (_input, context) => { + const authorize = async () => { + context.signal?.throwIfAborted() + if (fingerprint((await effectiveMcpConnection(context.principal)).policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Допуск изменился; подготовьте план заново", 409) + } + await authorize() + const access = await databaseBackupAuthority(context.principal, context.connection.owner_id, context.connection.policy), source = await readDatabaseBackupSource(access) + await authorize() + return { target: { scope: "global" }, objects: [{ kind: "database-backup", id: "global" }, ...source.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), ...source.objects], snapshots: [], before: { proof: source.proof }, requiresHuman: true, mutationCost: 1, + steps: [{ kind: "service", service: "database.backup", input: { authority: access, attemptRef: randomUUID(), expectedProof: source.proof, beforeFingerprint: fingerprint({ proof: source.proof }) } }], + warnings: ["Полная база содержит пароли, ключи и закрытые данные. Архив создаётся только после человеческого подтверждения и доступен вошедшему владельцу 15 минут.", "Модель получает только квитанцию; содержимое базы доступно через человеческий web API. Предел архива — 64 МиБ, экспорта — 120 секунд.", "MCP-экспорт поддерживает PostgreSQL URI с одним host и значениями, представимыми в service file. Неподдержанная форма подключения отклоняется без потери параметров.", "Экспорт временно приостанавливает обработчики базы, планировщик и NetFlow. Отзыв допуска или изменение области блокирует экспорт и выдачу. Автоматического повторения нет."] } + }, + }) +} diff --git a/backend/src/modules/mcp/database-backup-artifact.ts b/backend/src/modules/mcp/database-backup-artifact.ts new file mode 100644 index 00000000..b8dbbe58 --- /dev/null +++ b/backend/src/modules/mcp/database-backup-artifact.ts @@ -0,0 +1,138 @@ +import { createHash, randomUUID, timingSafeEqual } from "node:crypto" +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { env } from "../../config.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { exportSystemDatabaseBackupWithEvent } from "../../services/system-database-backup.js" +import { assertVerifiedConfigurationAccess, executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { DatabaseDumpCommandError, MAX_DATABASE_DUMP_BYTES } from "../../services/bounded-database-dump.js" +import { databaseDumpServiceFile } from "../../services/database-dump-connection.js" +import { MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES } from "./log-artifacts.js" +import { assertPortalClaimsCurrent } from "./portal-revocation.js" +import { connectionWrite, effectiveMcpConnection, type McpPrincipal } from "./store.js" +import { accessGeneration, decryptBinary, encryptBinary, fingerprint, McpError, privateSecretFingerprint } from "./security.js" + +export const MAX_DATABASE_BACKUP_CIPHER_BYTES = Math.ceil((MAX_DATABASE_DUMP_BYTES + 28) / 3) * 4 +const digest = z.string().regex(/^[a-f0-9]{64}$/) +export const databaseBackupAuthoritySchema = z.object({ + principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.iso.datetime() }).strict(), + ownerId: z.string().min(1).max(200), policyProof: digest, generation: digest, endpointProof: digest, +}).strict() +export type DatabaseBackupAuthority = z.infer +const scopeSchema = z.object({ version: z.literal(1), kind: z.literal("database-backup"), authority: databaseBackupAuthoritySchema, + attemptRef: z.string().uuid(), sourceProof: digest, bytes: z.number().int().positive().max(MAX_DATABASE_DUMP_BYTES), sha256: digest, encryptedSha256: digest, proof: digest }).strict() +type Header = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date } +type Meta = Header & { access_scope: z.infer; encrypted_bytes: number } +const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "database-backup-artifact-v1") +const endpoint = () => privateSecretFingerprint(env.DATABASE_URL, "database-backup-endpoint-v1") +const signed = (header: Header, scope: Omit, "proof">) => ({ id: header.id, connection_id: header.connection_id, filename: header.filename, media_type: header.media_type, expires_at: header.expires_at.toISOString(), scope }) +const fail = (message: string, code = "ARTIFACT_INVALID") => new McpError(code, message, 409) + +/** Only bounded topology and protection metadata is read here; never pg_dump or archive bytes. */ +export async function readDatabaseBackupSource(raw: unknown, client: Pick = pool) { + const authority = databaseBackupAuthoritySchema.parse(raw), connection = await effectiveMcpConnection(authority.principal, client) + if (connection.owner_id !== authority.ownerId || !hasPermission(connection.owner_permissions, "mm:settings:admin") || !hasPermission(connection.policy.permissions, "mm:settings:admin") || !connection.policy.scope.allowGlobal || !connection.policy.scope.operationIds.includes("database.backup")) throw new McpError("PERMISSION_DENIED", "Нет допуска к полной базе приложения", 403) + if (fingerprint(connection.policy) !== authority.policyProof || await accessGeneration() !== authority.generation || await endpoint() !== authority.endpointProof) throw fail("Допуск к базе изменился", "RESET_REQUIRED") + // Security epochs rotate for endpoint/credential/enabled changes (including + // A→B→A), while ordinary scheduler telemetry updates keep the proof stable. + const servers = (await client.query("SELECT id,routing_endpoint_epoch AS revision FROM servers ORDER BY id LIMIT 1001")).rows + const protections = (await client.query("SELECT CASE WHEN octet_length(object_ref::text)<=32768 THEN object_ref ELSE NULL END AS object_ref,xmin::text AS revision FROM mcp_protections ORDER BY object_ref::text LIMIT 1001")).rows + if (servers.length > 1000 || servers.some(row => !z.string().uuid().safeParse(row.revision).success) || protections.length > 1000 || protections.some(row => !row.object_ref)) throw fail("Область полной базы превышает предел", "STATE_UNAVAILABLE") + const serverIds = servers.map(row => Number(row.id)) + if (serverIds.some(id => !Number.isSafeInteger(id) || id < 1 || !connection.policy.scope.serverIds.includes(id))) throw fail("Полная база содержит сервер вне подключения", "SERVER_DENIED") + if (protections.some(row => row.object_ref.protection === "deny")) throw fail("Полная база содержит запрещённый объект", "PROTECTED_OBJECT") + // Validate representability during planning/current checks after access and + // full-scope protection checks, before approval or an execution intent exists. + // This is pure validation: no service file, PostgreSQL child or public URI. + try { databaseDumpServiceFile(env.DATABASE_URL) } + catch (error) { + if (error instanceof DatabaseDumpCommandError) throw new McpError(error.code, error.message, 409) + throw error + } + return { serverIds, objects: protections.map(row => row.object_ref), proof: await sign({ authority, servers, protections }) } +} +export async function databaseBackupAuthority(principal: McpPrincipal, ownerId: string, policy: unknown) { + return databaseBackupAuthoritySchema.parse({ principal, ownerId, policyProof: fingerprint(policy), generation: await accessGeneration(), endpointProof: await endpoint() }) +} +export async function createDatabaseBackupArtifact(authority: DatabaseBackupAuthority, expectedProof: string, attemptRef: string) { + z.string().uuid().parse(attemptRef) + const source = await readDatabaseBackupSource(authority) + const authorize = async () => { await assertVerifiedConfigurationAccess(); if ((await readDatabaseBackupSource(authority)).proof !== expectedProof) throw fail("Область базы изменилась после подтверждения", "STATE_CONFLICT") } + return executeGuardedApplicationWrite({ kind: "database-backup", id: "global" }, source.serverIds, async () => { + let stage = "authorize" + try { + await authorize() + stage = "export" + const result = await exportSystemDatabaseBackupWithEvent({ authorize }) + stage = "export-authorize" + await authorize() + if (!result.buffer.subarray(0, 5).equals(Buffer.from("PGDMP"))) throw fail("Экспорт не является архивом PostgreSQL") + stage = "encrypt" + const ciphertext = (await encryptBinary(result.buffer)).toString("base64") + if (Buffer.byteLength(ciphertext) > MAX_DATABASE_BACKUP_CIPHER_BYTES) throw fail("Зашифрованный архив превышает предел", "EXPORT_LIMIT") + const header: Header = { id: randomUUID(), connection_id: authority.principal.connectionId, filename: result.filename, media_type: "application/octet-stream", expires_at: new Date(Date.now() + 900000) } + const access = { version: 1 as const, kind: "database-backup" as const, authority, attemptRef, sourceProof: expectedProof, bytes: result.buffer.length, sha256: hash(result.buffer), encryptedSha256: hash(ciphertext) } + const scope = scopeSchema.parse({ ...access, proof: await sign(signed(header, access)) }) + stage = "publish" + await connectionWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [header.connection_id]) + if ((await readDatabaseBackupSource(authority, client)).proof !== expectedProof) throw fail("Область базы изменилась", "STATE_CONFLICT") + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))") + await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()") + const usage = (await client.query("SELECT count(*) FILTER(WHERE connection_id=$1 AND access_scope->>'kind'='database-backup')::text AS files,COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts", [header.connection_id])).rows[0] + if (BigInt(usage.files) >= 2n || BigInt(usage.total_bytes) + BigInt(Buffer.byteLength(ciphertext)) > BigInt(MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES)) throw new McpError("EXPORT_QUOTA_EXCEEDED", "Квота временных архивов исчерпана", 429) + await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)", [header.id, header.connection_id, header.filename, header.media_type, ciphertext, JSON.stringify(scope), header.expires_at]) + }) + stage = "publication-authorize" + await authorize() + return { artifactId: header.id, downloadPath: `/api/mcp/artifacts/${header.id}`, expiresInSeconds: 900, bytes: access.bytes, sha256: access.sha256, humanDownloadOnly: true, databaseContentReturned: false, configurationApplied: false, + warnings: ["listenerError", "schedulerError", "eventError"].filter(key => !!result[key as keyof typeof result]) } + } catch (error) { + if (process.env.NODE_ENV === "test") { + const code = error instanceof DatabaseDumpCommandError || error instanceof McpError ? error.code : error && typeof error === "object" && "code" in error && typeof error.code === "string" && /^[0-9A-Z]{5}$/.test(error.code) ? error.code : null + console.error("Database backup failure diagnostic:", JSON.stringify({ stage, code, ...(error instanceof DatabaseDumpCommandError ? { diagnosticKinds: error.diagnosticKinds, exitCode: error.exitCode } : {}) })) + } + if (error instanceof DatabaseDumpCommandError) throw new McpError(error.code, error.message, 503) + throw error + } + }, async () => (await readDatabaseBackupSource(authority)).objects) +} +async function loadMeta(id: string): Promise { + const row = (await pool.query("/* MCP database artifact metadata */ SELECT id,connection_id,filename,media_type,expires_at,CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope,octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND expires_at>now() AND octet_length(filename)<=1024 AND octet_length(media_type)<=200", [id])).rows[0] + if (!row) throw new McpError("NOT_FOUND", "Архив отсутствует или срок истёк", 404) + const scope = scopeSchema.safeParse(row.access_scope) + if (!scope.success || !(row.expires_at instanceof Date) || !Number.isFinite(row.expires_at.getTime()) || !Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes < 1 || row.encrypted_bytes > MAX_DATABASE_BACKUP_CIPHER_BYTES) throw fail("Метаданные архива не подтверждены") + const { proof, ...access } = scope.data, expected = await sign(signed(row, access)) + if (!timingSafeEqual(Buffer.from(proof, "hex"), Buffer.from(expected, "hex")) || row.connection_id !== access.authority.principal.connectionId) throw fail("Подпись архива не совпала") + return { ...row, access_scope: scope.data } +} +export async function verifyDatabaseBackupArtifact(id: string, authority: DatabaseBackupAuthority, sourceProof: string, attemptRef: string, receipt?: { bytes?: unknown; sha256?: unknown }) { + const meta = await loadMeta(id), source = await readDatabaseBackupSource(authority) + if (fingerprint(meta.access_scope.authority) !== fingerprint(authority) || meta.access_scope.attemptRef !== attemptRef || meta.access_scope.sourceProof !== sourceProof || source.proof !== sourceProof || (receipt && (receipt.bytes !== meta.access_scope.bytes || receipt.sha256 !== meta.access_scope.sha256))) return false + const payload = (await pool.query<{ ciphertext: string }>("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=$3", [id, meta.access_scope.proof, MAX_DATABASE_BACKUP_CIPHER_BYTES])).rows[0] + return !!payload && hash(payload.ciphertext) === meta.access_scope.encryptedSha256 && (await readDatabaseBackupSource(authority)).proof === sourceProof +} +export async function humanDatabaseBackupMetadata(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent) { + const meta = await loadMeta(id), scope = meta.access_scope + await checkClaims(actor) + if (actor.id !== scope.authority.ownerId || !hasPermission(actor.permissions, "mm:settings:admin")) throw new McpError("PERMISSION_DENIED", "Архив доступен только владельцу с правом администрирования", 403) + if ((await readDatabaseBackupSource(scope.authority)).proof !== scope.sourceProof) throw fail("Область архива изменилась", "STATE_CONFLICT") + return scope +} +export async function readHumanDatabaseBackupArtifact(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent, signal?: AbortSignal) { + const bounded = signal ? AbortSignal.any([signal, AbortSignal.timeout(45000)]) : AbortSignal.timeout(45000) + const meta = await loadMeta(id), scope = meta.access_scope + const authorize = async () => { bounded.throwIfAborted(); await humanDatabaseBackupMetadata(id, actor, checkClaims); bounded.throwIfAborted(); if (meta.expires_at.getTime() <= Date.now()) throw fail("Срок архива истёк") } + await authorize() + const payload = (await pool.query<{ ciphertext: string }>("/* MCP database artifact payload */ SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=$3", [id, scope.proof, MAX_DATABASE_BACKUP_CIPHER_BYTES])).rows[0] + await authorize() + if (!payload || hash(payload.ciphertext) !== scope.encryptedSha256) throw fail("Зашифрованный архив изменился") + let content: Buffer + try { content = await decryptBinary(Buffer.from(payload.ciphertext, "base64")) } catch { throw fail("Архив не расшифрован") } + await authorize() + if (content.length !== scope.bytes || hash(content) !== scope.sha256 || !content.subarray(0, 5).equals(Buffer.from("PGDMP"))) throw fail("Размер или SHA-256 архива не совпал") + await authorize() + return { filename: meta.filename, mediaType: meta.media_type, content } +} diff --git a/backend/src/modules/mcp/database-maintenance.test.ts b/backend/src/modules/mcp/database-maintenance.test.ts new file mode 100644 index 00000000..f13f52cd --- /dev/null +++ b/backend/src/modules/mcp/database-maintenance.test.ts @@ -0,0 +1,32 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { executeMcpDatabaseMaintenance } from "./database-maintenance.js" +test("restore invalidates access after durable intent and before target database work", async () => { + const calls: string[] = [] + assert.deepEqual(await executeMcpDatabaseMaintenance(async () => { calls.push("restore"); return { restored: true } }, { record: async state => { calls.push(state) }, invalidateAccess: async () => { calls.push("invalidate") }, close: async () => { calls.push("close") } }), { restored: true }) + assert.deepEqual(calls, ["intent", "invalidate", "restore", "applied", "close"]) +}) +test("failed intent or generation invalidation prevents restore", async () => { + for (const stage of ["intent", "invalidate"]) { + let writes = 0 + await assert.rejects(executeMcpDatabaseMaintenance(async () => { writes++; return {} }, { record: async state => { if (stage === state) throw new Error(stage) }, invalidateAccess: async () => { if (stage === "invalidate") throw new Error(stage) }, close: async () => {} }), new RegExp(stage)) + assert.equal(writes, 0) + } +}) +test("post-restore audit errors remain warnings and never trigger command replay", async () => { + let writes = 0 + const result = await executeMcpDatabaseMaintenance(async () => { writes++; return { restored: true } }, { record: async state => { if (state === "applied") throw new Error("disk") }, invalidateAccess: async () => {}, close: async () => {} }) + assert.equal(writes, 1); assert.equal(result.restored, true); assert.match(result.maintenanceError!, /Не повторяйте/) +}) +test("an applied archive with failed migrations writes durable unconfirmed maintenance", async () => { + const states: string[] = [], outcome = { restored: true, migrationError: "Pending migration failed" } + const result = await executeMcpDatabaseMaintenance(async () => outcome, { + record: async state => { states.push(state) }, invalidateAccess: async () => {}, close: async () => {}, + }) + assert.deepEqual(result, outcome) + assert.deepEqual(states, ["intent", "unconfirmed"]) +}) +test("unconfirmed-journal and close failures preserve a failed restore's original error", async () => { + const original = new Error("restore failed") + await assert.rejects(executeMcpDatabaseMaintenance(async () => { throw original }, { record: async state => { if (state === "unconfirmed") throw new Error("journal") }, invalidateAccess: async () => {}, close: async () => { throw new Error("close") } }), error => error === original) +}) diff --git a/backend/src/modules/mcp/database-maintenance.ts b/backend/src/modules/mcp/database-maintenance.ts new file mode 100644 index 00000000..705ea785 --- /dev/null +++ b/backend/src/modules/mcp/database-maintenance.ts @@ -0,0 +1,21 @@ +interface MaintenanceDependencies { + record: (state: "intent" | "applied" | "unconfirmed") => Promise + invalidateAccess: () => Promise + close: () => Promise +} +/** A confirmed restore cannot be relabelled failed by a later journal error. */ +export async function executeMcpDatabaseMaintenance(work: () => Promise, deps: MaintenanceDependencies): Promise { + let result: (T & { migrationError?: string }) | undefined, failed = false, failure: unknown, warning: string | undefined + try { + await deps.record("intent") + await deps.invalidateAccess() + result = await work() + } catch (error) { + failed = true; failure = error + try { await deps.record("unconfirmed") } catch { /* preserve the primary outcome */ } + } + if (!failed) try { await deps.record(result!.migrationError ? "unconfirmed" : "applied") } catch { warning = "База восстановлена, но внешний журнал MCP не подтвердил завершение. Не повторяйте restore." } + try { await deps.close() } catch { warning ??= "Не удалось закрыть внешний журнал восстановления MCP" } + if (failed) throw failure + return { ...result!, ...(warning ? { maintenanceError: warning } : {}) } +} diff --git a/backend/src/modules/mcp/database-request-lease.ts b/backend/src/modules/mcp/database-request-lease.ts new file mode 100644 index 00000000..e49a7fc4 --- /dev/null +++ b/backend/src/modules/mcp/database-request-lease.ts @@ -0,0 +1,94 @@ +import { AsyncLocalStorage } from "node:async_hooks" +import { assertRuntimeDatabaseActivationCurrent, createPool } from "../../db/index.js" +import { SYSTEM_DATABASE_REQUEST_LOCK } from "../../db/operation-lock.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../../db/database-restore-quarantine.js" +import { sharedDatabaseGate } from "../../services/shared-database-gate.js" +import { McpError } from "./security.js" + +const limit = 8 +const leases = createPool(limit, true) +leases.on("error", () => console.error("MCP database request lease lost on idle connection")) +const apiLeases = createPool(limit, true) +apiLeases.on("error", () => console.error("REST database request lease lost on idle connection")) +const detachedApiLeases = createPool(2, true) +detachedApiLeases.on("error", () => console.error("Detached REST database lease lost on idle connection")) +const context = new AsyncLocalStorage<{ accepting: boolean; children: Set>; signal: AbortSignal }>() +let active = 0 + +export function mcpDatabaseRequestSignal(): AbortSignal | undefined { return context.getStore()?.signal } + +export function withActiveDatabaseRequest(run: (signal: AbortSignal) => Promise): Promise | undefined { + const parent = context.getStore() + if (parent?.signal.aborted) return Promise.reject(parent.signal.reason) + if (!parent?.accepting) return undefined + const child = Promise.resolve().then(() => { parent.signal.throwIfAborted(); return run(parent.signal) }) + parent.children.add(child) + const settled = () => { parent.children.delete(child) } + void child.then(settled, settled) + return child +} + +export function withMcpDatabaseRequest(run: () => Promise): Promise { return withDatabaseRequest(run, true) } +export function withDatabaseApiRequest(run: () => Promise): Promise { return withDatabaseRequest(run, false) } +export function checkDatabaseApiStartup(): Promise { + return withDatabaseRequest(async () => {}, false, false, true) +} + +async function withDatabaseRequest(run: () => Promise, mcp: boolean, detached = false, starting = false): Promise { + const nested = withActiveDatabaseRequest(run) + if (nested) return nested + if (mcp && active >= limit) throw new McpError("RATE_LIMITED", "Лимит одновременно выполняемых запросов MCP", 429) + if (mcp) active++ + try { + const client = await (detached ? detachedApiLeases : mcp ? leases : apiLeases).connect() + let destroy = true, acquired = false + const lifetime = new AbortController() + const disconnected = () => lifetime.abort(new McpError("PROTOCOL_STORAGE_BUSY", "Соединение с допуском MCP потеряно", 503)) + client.on("error", disconnected) + client.on("end", disconnected) + try { + if (starting) await client.query("SET lock_timeout = '10s'") + const { rows } = await client.query<{ acquired: boolean }>(starting + ? "SELECT true AS acquired FROM pg_catalog.pg_advisory_lock_shared($1::bigint)" + : "SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired", [SYSTEM_DATABASE_REQUEST_LOCK]) + lifetime.signal.throwIfAborted() + if (rows[0]?.acquired === false) { + destroy = false + throw new McpError("PROTOCOL_STORAGE_BUSY", "Выполняется операция с базой приложения", 503) + } + if (rows[0]?.acquired !== true) throw new McpError("AUDIT_UNAVAILABLE", "Допуск MCP не подтверждён", 503) + acquired = true + if (starting) await client.query("RESET lock_timeout") + if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client) + assertRuntimeDatabaseActivationCurrent() + lifetime.signal.throwIfAborted() + const owner = { accepting: true, children: new Set>(), signal: lifetime.signal } + try { return await context.run(owner, run) } + finally { + owner.accepting = false + await Promise.allSettled(owner.children) + } + } finally { + if (acquired && !lifetime.signal.aborted) { + try { + const { rows } = await client.query<{ released: boolean }>( + "SELECT pg_catalog.pg_advisory_unlock_shared($1::bigint) AS released", [SYSTEM_DATABASE_REQUEST_LOCK]) + destroy = rows[0]?.released !== true + } catch { /* Destroy the backend rather than return an uncertain session lock. */ } + } + client.off("error", disconnected) + client.off("end", disconnected) + client.release(destroy || lifetime.signal.aborted) + } + } finally { if (mcp) active-- } +} + +/** A scheduled operation owns a new lease, even when the submitting request is still active. */ +export function withMcpRunnerDatabaseRequest(run: () => Promise): Promise { + return context.exit(() => withMcpDatabaseRequest(run)) +} + +/** Detached jobs keep their own lease through terminal persistence after the HTTP response. */ +export function withDetachedDatabaseApiRequest(run: () => Promise): Promise { + return context.exit(() => sharedDatabaseGate.request(() => withDatabaseRequest(run, false, true))) +} diff --git a/backend/src/modules/mcp/database-restore-data-equivalence.sql-helper.ts b/backend/src/modules/mcp/database-restore-data-equivalence.sql-helper.ts new file mode 100644 index 00000000..19d82cae --- /dev/null +++ b/backend/src/modules/mcp/database-restore-data-equivalence.sql-helper.ts @@ -0,0 +1,222 @@ +import { createHash, randomUUID } from "node:crypto" +import { spawn } from "node:child_process" +import { readFile, realpath, unlink, writeFile } from "node:fs/promises" +import path from "node:path" +import { isDeepStrictEqual } from "node:util" +import { setTimeout as delay } from "node:timers/promises" +import pg from "pg" +import { captureMigrationData, compareMigrationData, type CapturedMigrationData } from "../../db/migration-data-inventory.js" + +// Private destructive acceptance helper. No application entry point imports it. +class OwnedRestoreDataError extends Error {} +function refuse(category: string): never { throw new OwnedRestoreDataError(`Owned restore data equivalence refused: ${category}`) } +const identifier = (value: string) => '"' + value.replaceAll('"', '""') + '"' +const literal = (value: string) => "'" + value.replaceAll("'", "''") + "'" +const profileSql = `SELECT pg_catalog.pg_encoding_to_char(d.encoding) AS encoding, + d.datcollate AS collate,d.datctype AS ctype,d.datlocprovider::text AS provider, + pg_catalog.to_jsonb(d)->>'datlocale' AS locale,pg_catalog.to_jsonb(d)->>'daticurules' AS rules, + d.datcollversion AS version,t.spcname AS tablespace,pg_catalog.pg_get_userbyid(d.datdba) AS owner, + d.datacl::text AS acl,pg_catalog.shobj_description(d.oid,'pg_database') AS comment + FROM pg_catalog.pg_database d JOIN pg_catalog.pg_tablespace t ON t.oid=d.dattablespace + WHERE d.datname=pg_catalog.current_database()` + +async function snapshot(pool: pg.Pool): Promise { + const client = await pool.connect() + try { + await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + await client.query(`SET LOCAL search_path=pg_catalog; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'; + SET LOCAL standard_conforming_strings=on; SET LOCAL statement_timeout='5s'; SET LOCAL row_security=off; + SET LOCAL extra_float_digits=3; SET LOCAL bytea_output='hex'; SET LOCAL IntervalStyle='postgres'; + SET LOCAL client_encoding='UTF8'; SET LOCAL lc_monetary='C'; SET LOCAL lc_numeric='C'`) + return await captureMigrationData(client) + } finally { + try { await client.query("ROLLBACK") } finally { client.release() } + } +} + +/** pg_restore receives no URI/password argv; discard every diagnostic byte. */ +async function restoreClone(file: string, url: URL, database: string) { + const childEnvironment = Object.fromEntries(Object.entries(process.env).filter(([key]) => !/^PG/i.test(key))) + Object.assign(childEnvironment, { PGHOST: "127.0.0.1", PGPORT: url.port || "5432", PGUSER: decodeURIComponent(url.username), + PGDATABASE: database, PGPASSWORD: decodeURIComponent(url.password), PGCONNECT_TIMEOUT: "5" }) + await new Promise((resolve, reject) => { + const child = spawn("pg_restore", ["--single-transaction", "--exit-on-error", "--no-password", "--dbname", database, file], + { env: childEnvironment, shell: false, windowsHide: true, stdio: ["ignore", "ignore", "pipe"] }) + let failed = false, bytes = 0 + const timer = setTimeout(() => { failed = true; child.kill("SIGKILL") }, 120000) + child.stderr.on("data", (chunk: Buffer) => { bytes += chunk.length; if (bytes > 1048576) { failed = true; child.kill("SIGKILL") } }) + child.on("error", () => { failed = true }) + child.on("close", code => { clearTimeout(timer); if (failed || code !== 0) reject(new OwnedRestoreDataError("Owned restore data equivalence refused: archive restore")); else resolve() }) + }) +} + +function difference(expected: CapturedMigrationData, actual: CapturedMigrationData): string { + if (expected.schemaSha256 !== actual.schemaSha256) return "schema" + for (const name of ["tables", "sequences"] as const) { + const left = expected[name], right = actual[name] + for (let index = 0; index < Math.max(left.length, right.length); index++) { + if (!isDeepStrictEqual(left[index], right[index])) { + const relation = left[index] ?? right[index] + // Catalog names only; never interpolate driver messages or row values. + return `${name}: ${JSON.stringify([relation.namespace, relation.name])}` + } + } + } + return "evidence" +} + +export interface OwnedRestoreDataComparisonInput { + operationId: string + receipt: Record + actorId: string + startedAt: number +} + +async function prepareExpectation(archive: Buffer) { + // These checks deliberately precede config/security/configured-pool imports. + if (process.env.MMAPP_OWN_TERMINAL_DATA_EQUIVALENCE !== "true" || process.env.MMAPP_OWN_TERMINAL_RESTORE !== "true") refuse("owned opt-in") + let url: URL + try { url = new URL(process.env.DATABASE_URL ?? "") } catch { refuse("owned connection") } + const state = path.resolve(process.env.MCP_STATE_DIR ?? "") + if (!["postgres:", "postgresql:"].includes(url.protocol) || url.hostname !== "127.0.0.1" || decodeURIComponent(url.username) !== "mcp_test" || url.pathname !== "/postgres" || + !process.env.MMAPP_OWN_SQL_DB || decodeURIComponent(url.pathname.slice(1)) !== process.env.MMAPP_OWN_SQL_DB || url.search || url.hash || + !process.env.MCP_STATE_DIR || path.basename(state) !== "state" || !/^mm-mcp-test-[A-Za-z0-9-]+$/.test(path.basename(path.dirname(state)))) refuse("owned connection/state") + if (path.resolve(await realpath(state)).toLowerCase() !== state.toLowerCase()) refuse("owned state path") + if (!Buffer.isBuffer(archive) || archive.length < 5 || archive.length > 64 * 1024 * 1024 || archive.subarray(0, 5).toString() !== "PGDMP") refuse("archive") + const { pool: target } = await import("../../db/index.js") + const { env } = await import("../../config.js") + if (env.DATABASE_URL !== url.toString() && env.DATABASE_URL !== process.env.DATABASE_URL) refuse("configured connection") + const { redact } = await import("./security.js") + const identity = (await target.query("SELECT current_database() AS db,session_user AS owner,current_user AS active_owner")).rows[0] + if (identity.db !== process.env.MMAPP_OWN_SQL_DB || identity.owner !== "mcp_test" || identity.active_owner !== "mcp_test") refuse("target identity") + const profile = (await target.query(profileSql)).rows[0] + if (!profile || profile.owner !== "mcp_test" || profile.comment !== null || profile.acl !== null || profile.tablespace !== "pg_default") refuse("source database profile") + const archiveSha256 = createHash("sha256").update(archive).digest("hex") + const suffix = randomUUID().replaceAll("-", ""), database = `mm_restore_data_${suffix}` + const file = path.join(state, `restore-data-${suffix}.dump`) + const cloneUrl = new URL(url); cloneUrl.pathname = "/" + database + const clone = new pg.Pool({ connectionString: cloneUrl.toString(), max: 1, connectionTimeoutMillis: 5000, statement_timeout: 5000 }) + let created = false, fileOwned = false, closed = false, comparing = false + let verifiedReceipt: { context: unknown; audit: unknown } | undefined + let beforeConsumers: Readonly<{ + equal: true; equalityPhase: "before-consumers"; evidenceOnly: true; ready: false; enrollmentAuthorized: false; + allReplicaQuiescence: false; externalQuiescence: false; archiveSnapshotValidated: true; + captureStartedAt: number; capturedAt: number; tableCount: number; sequenceCount: number; rowCount: number; sha256: string + }> | undefined + const close = async () => { + if (closed) return + closed = true + let cleanupFailed = false + try { await clone.end() } catch { cleanupFailed = true } + if (created) { + try { + if (!/^mm_restore_data_[a-f0-9]{32}$/.test(database) || database !== `mm_restore_data_${suffix}`) refuse("cleanup database identity") + const row = (await target.query("SELECT datname,pg_catalog.pg_get_userbyid(datdba) AS owner FROM pg_catalog.pg_database WHERE datname=$1", [database])).rows[0] + if (!row || row.datname !== database || row.owner !== "mcp_test") refuse("cleanup database owner") + await target.query(`DROP DATABASE ${identifier(database)}`) + } catch { cleanupFailed = true } + } + if (fileOwned) { try { await unlink(file) } catch { cleanupFailed = true } } + if (cleanupFailed) refuse("owned cleanup") + } + try { + const provider = ({ c: "libc", i: "icu", b: "builtin" } as Record)[String(profile.provider)] + if (!provider || typeof profile.encoding !== "string" || typeof profile.collate !== "string" || typeof profile.ctype !== "string") refuse("source locale profile") + let locale = "" + if (provider === "icu" || provider === "builtin") { + if (typeof profile.locale !== "string") refuse("source locale") + locale = ` ${provider === "icu" ? "ICU_LOCALE" : "BUILTIN_LOCALE"} ${literal(profile.locale)}` + } + if (profile.rules !== null) { if (provider !== "icu" || typeof profile.rules !== "string") refuse("source locale rules"); locale += ` ICU_RULES ${literal(profile.rules)}` } + if (profile.version !== null) { if (typeof profile.version !== "string") refuse("source collation version"); locale += ` COLLATION_VERSION ${literal(profile.version)}` } + await target.query(`CREATE DATABASE ${identifier(database)} WITH TEMPLATE template0 OWNER mcp_test ENCODING ${literal(profile.encoding)} LC_COLLATE ${literal(profile.collate)} LC_CTYPE ${literal(profile.ctype)} LOCALE_PROVIDER ${provider}${locale}`) + created = true + await writeFile(file, archive, { flag: "wx", mode: 0o600 }); fileOwned = true + await restoreClone(file, url, database) + if (!isDeepStrictEqual((await clone.query(profileSql)).rows[0], profile)) refuse("clone database profile") + const rawArchive = await snapshot(clone) + compareMigrationData(rawArchive, rawArchive) + return { + async compareBeforeConsumers() { + if (closed || comparing || beforeConsumers) refuse("pre-consumer comparison lifecycle") + comparing = true + try { + const captureStartedAt = Date.now() + // This is the complete raw archive expectation. The caller pauses the + // owned restore after migrations and before any consumer restarts. + // No table/column exception or expected runtime append is applied. + const actual = await snapshot(target) + try { compareMigrationData(rawArchive, actual) } catch { refuse(difference(rawArchive, actual)) } + beforeConsumers = Object.freeze({ equal: true as const, equalityPhase: "before-consumers" as const, + evidenceOnly: true as const, ready: false as const, enrollmentAuthorized: false as const, + allReplicaQuiescence: false as const, externalQuiescence: false as const, archiveSnapshotValidated: true as const, + captureStartedAt, capturedAt: Date.now(), tableCount: actual.tables.length, sequenceCount: actual.sequences.length, + rowCount: actual.rowCount, sha256: actual.sha256 }) + return beforeConsumers + } catch (error) { + if (error instanceof OwnedRestoreDataError) throw error + refuse("pre-consumer capture/comparison") + } finally { comparing = false } + }, + async compareAfterRestore(input: OwnedRestoreDataComparisonInput) { + if (closed || comparing) refuse("comparison lifecycle") + comparing = true + try { + const { operationId, receipt, actorId, startedAt } = input + if (!/^[a-f0-9-]{36}$/i.test(operationId) || !actorId || !Number.isSafeInteger(startedAt) || startedAt > Date.now() || + receipt.operationId !== operationId || receipt.state !== "applied" || receipt.accessRevoked !== true || receipt.automaticRetry !== false) refuse("expected receipt") + const finishedAt = typeof receipt.finishedAt === "string" ? Date.parse(receipt.finishedAt) : NaN + if (!beforeConsumers || !Number.isFinite(finishedAt) || finishedAt > Date.now() || + beforeConsumers.captureStartedAt < startedAt || beforeConsumers.capturedAt > finishedAt || + beforeConsumers.captureStartedAt > beforeConsumers.capturedAt) refuse("pre-consumer evidence window") + if (typeof receipt.archiveRef !== "string" || !/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(receipt.archiveRef)) refuse("receipt archive reference") + const vaultFile = path.join(state, "restore-vault", receipt.archiveRef) + const metadata = JSON.parse(await readFile(vaultFile + ".archive.json", "utf8")) as Record + const used = JSON.parse(await readFile(vaultFile + ".used", "utf8")) as Record + if (!metadata || metadata.id !== receipt.archiveRef || metadata.sha256 !== archiveSha256 || metadata.sizeBytes !== archive.length || + metadata.ownerId !== actorId || !used || used.operationId !== operationId) refuse("archive binding") + const expectedPayload = JSON.parse(JSON.stringify(redact(receipt))) as unknown + const context = { operationId, actorId, startedAt, receipt: expectedPayload } + if (verifiedReceipt && !isDeepStrictEqual(verifiedReceipt.context, context)) refuse("comparison context changed") + const deadline = Date.now() + 10000 + let audit: Record | undefined + for (;;) { + const rows = (await target.query(`SELECT id::text AS id,actor_id,connection_id,payload,created_at::text AS created_at, + (created_at >= to_timestamp($2::double precision/1000) AND created_at <= clock_timestamp()) AS in_window + FROM public.mcp_audit WHERE action='database.restore.receipt' AND payload->>'operationId'=$1 LIMIT 2`, [operationId, startedAt])).rows + if (rows.length > 1) refuse("audit multiplicity") + if (rows.length === 1) { audit = rows[0]; break } + if (Date.now() >= deadline) refuse("audit timeout") + await delay(50) + } + if (!audit || audit.actor_id !== actorId || audit.connection_id !== null || audit.in_window !== true || typeof audit.created_at !== "string" || + typeof audit.id !== "string" || !/^[1-9]\d*$/.test(audit.id) || !isDeepStrictEqual(audit.payload, expectedPayload)) refuse("audit validation") + const auditIdentity = { id: audit.id, createdAt: audit.created_at } + if (verifiedReceipt && !isDeepStrictEqual(verifiedReceipt.audit, auditIdentity)) refuse("audit receipt changed") + verifiedReceipt = { context, audit: auditIdentity } + // Consumers have resumed by this point. Report the frozen full-data + // observation at its actual phase, then independent receipt evidence; + // do not recapture or manufacture a post-consumer expected database. + return Object.freeze({ ...beforeConsumers, dataEqualBeforeConsumers: true as const, + postAuditReceiptExact: true as const, archiveShaBound: true as const, auditRecordsObserved: 1 as const }) + } catch (error) { + if (error instanceof OwnedRestoreDataError) throw error + refuse("capture/comparison") + } finally { comparing = false } + }, + close, + } + } catch (error) { + try { await close() } catch { /* Preserve the primary sanitized failure. */ } + if (error instanceof OwnedRestoreDataError) throw error + refuse("preparation") + } +} + +export async function prepareOwnedRestoreDataExpectation(archive: Buffer) { + try { return await prepareExpectation(archive) } + catch (error) { + if (error instanceof OwnedRestoreDataError) throw error + refuse("preparation") + } +} diff --git a/backend/src/modules/mcp/database-restore.test.ts b/backend/src/modules/mcp/database-restore.test.ts new file mode 100644 index 00000000..886aa928 --- /dev/null +++ b/backend/src/modules/mcp/database-restore.test.ts @@ -0,0 +1,218 @@ +import assert from "node:assert/strict" +import { randomUUID, createHash, createHmac } from "node:crypto" +import { readFile, writeFile } from "node:fs/promises" +import path from "node:path" +import type { PoolClient } from "pg" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { type AuthUser } from "../../lib/permissions.js" +import { accessGeneration, mcpStateDirectory } from "./security.js" +import { createMcpConnection, authenticateMcpToken, validatePrincipal, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { uploadRestoreArchive, readRestoreReceipt, executeMcpDatabaseRestore } from "./database-restore.js" +import { exportSystemDatabaseBackup, restoreSystemDatabaseBackup } from "../../services/system-database-backup.js" +import { initializeAutomaticSchedulerRoot } from "../../services/scheduler-automatic-root.js" +import { sharedDatabaseGate } from "../../services/shared-database-gate.js" +import { stopSchedulerForDatabaseOperation } from "../../services/scheduler.js" +import { stopTrafficFlowListenerForDatabaseOperation } from "../../services/traffic-flow-ingest.js" +import { acceptPortalRevocation, assertPortalClaimsCurrent } from "./portal-revocation.js" + +// This destructive test is only invoked by the private temporary-cluster runner. +const url = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") +assert.equal(process.env.MMAPP_OWN_TERMINAL_RESTORE, "true") +assert.equal(url.hostname, "127.0.0.1") +assert.equal(url.pathname.slice(1), process.env.MMAPP_OWN_SQL_DB) +assert.match(mcpStateDirectory(), /mm-mcp-test-/) +// Standalone owned runner has no buildApp bootstrap; initialize before actor/request scopes. +initializeAutomaticSchedulerRoot() +const actor: AuthUser = { id: "restore-owner", email: "restore@example.invalid", name: "Restore acceptance", apps: ["mm"], permissions: ["mm:settings:admin"], expiresAt: Date.now() + 3600000 } +const verifyData = process.env.MMAPP_OWN_TERMINAL_DATA_EQUIVALENCE === "true" +let dataExpectation: Awaited> | undefined +const originalConnect = pool.connect +let migrationCaptures = 0 +async function waitForDatabaseRetirement() { + let timer: ReturnType | undefined + try { + await Promise.race([sharedDatabaseGate.whenOpen(), new Promise((_, reject) => { + timer = setTimeout(() => reject(Error("Terminal restore did not retire its database admission")), 10000) + })]) + } finally { clearTimeout(timer) } +} + +try { + // Every owned restore profile archives inert domain collectors, not only the + // data-equivalence profile. Otherwise restart can launch unrelated downloads + // or router polling and race the final scheduler drain. Lifecycle SQL remains + // real; production scheduler configuration and drain checks are unchanged. + for (const table of ["traffic_settings", "uptime_settings", "traffic_flow_settings", "servers_api_ping_settings", "certificate_renew_settings", "backup_schedule_settings", "geoip_settings", "internet_path_settings", "router_log_settings"]) await pool.query(`UPDATE ${table} SET enabled=false WHERE id=1`) + await pool.query("UPDATE uptime_settings SET resources_enabled=false,ping_enabled=false,speed_enabled=false WHERE id=1") + await pool.query("DELETE FROM mcp_protections") + await updateMcpSettings({ enabled: true }, actor) + await pool.query("CREATE TABLE mcp_restore_acceptance_marker (value text NOT NULL)") + await pool.query("INSERT INTO mcp_restore_acceptance_marker VALUES('before')") + const serverIds = (await pool.query("SELECT id FROM servers")).rows.map(row => Number(row.id)) + // Combined owned profiles retain their evidence/disabled synthetic servers. + // The human restore fixture grants this exact catalog instead of inheriting + // the ordinary five-server default; production policy guards stay unchanged. + assert(serverIds.length <= 1000) + const issue = (owner = actor) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Database restore acceptance", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:settings:admin"], scope: { allowGlobal: true, serverIds, operationIds: ["database.restore"] }, limits: { serversPerPlan: Math.max(1, serverIds.length) } }), owner) + const cutoff = Date.now() - 1000 + const revokedOwner = { ...actor, id: "restore-revocation-after-backup", issuedAt: cutoff - 1 } + const oldAccess = await issue(revokedOwner), oldPrincipal = await authenticateMcpToken(oldAccess.token) + const readHistoricalAccess = async () => (await pool.query<{ status: string; credential_current: boolean; generation: string }>( + "SELECT c.policy->>'status' AS status, k.revoked_at IS NULL AND k.expires_at>now() AS credential_current, c.generation FROM mcp_connections c JOIN mcp_credentials k ON k.connection_id=c.id WHERE c.id=$1 AND k.id=$2", + [oldPrincipal.connectionId, oldPrincipal.credentialId], + )).rows[0] + const beforeDumpAccess = await readHistoricalAccess() + assert.deepEqual(beforeDumpAccess, { status: "active", credential_current: true, generation: await accessGeneration() }) + const dump = await exportSystemDatabaseBackup() + assert.equal(dump.buffer.subarray(0, 5).toString(), "PGDMP") + if (verifyData) dataExpectation = await (await import("./database-restore-data-equivalence.sql-helper.js")).prepareOwnedRestoreDataExpectation(dump.buffer) + const portalSecret = "restore-acceptance-only-portal-secret-32", stamp = String(Math.floor(Date.now() / 1000)) + const eventId = randomUUID() + const portalEvent = Buffer.from(JSON.stringify({ version: 2, eventId, userId: revokedOwner.id, type: "permissions.changed", eventOccurredAt: cutoff })) + assert.deepEqual(await acceptPortalRevocation(portalEvent, stamp, "sha256=" + createHmac("sha256", portalSecret).update(stamp + ".").update(portalEvent).digest("hex"), portalSecret), { ok: true, duplicate: false }) + const cutoffFile = path.join(mcpStateDirectory(), "owner-revocations", createHash("sha256").update(revokedOwner.id).digest("hex") + ".json") + const cutoffBytes = await readFile(cutoffFile) + assert.deepEqual(JSON.parse(cutoffBytes.toString("utf8")), { invalidBefore: cutoff }) + assert.deepEqual(await readHistoricalAccess(), { ...beforeDumpAccess, status: "revoked", credential_current: false }) + await assert.rejects(validatePrincipal(oldPrincipal), { code: "OWNER_CLAIMS_REVOKED" }) + const source = await uploadRestoreArchive(dump.buffer, actor) + const encrypted = await readFile(path.join(mcpStateDirectory(), "restore-vault", source.archiveRef + ".archive.bin")) + assert.ok(!encrypted.equals(dump.buffer)) + assert.ok(!encrypted.subarray(0, 5).equals(Buffer.from("PGDMP"))) + await assert.rejects(uploadRestoreArchive(dump.buffer, { ...actor, permissions: ["mm:mcp:admin"] })) + await assert.rejects(uploadRestoreArchive(Buffer.from("invalid"), actor)) + const issued = await issue(), principal = await authenticateMcpToken(issued.token) + const other = await issue({ ...actor, id: "another-owner" }), otherPrincipal = await authenticateMcpToken(other.token) + await assert.rejects(invokeMcpTool(otherPrincipal, "mm_database_restore_plan", { archiveRef: source.archiveRef, expectedSha256: source.sha256 })) + await assert.rejects(invokeMcpTool(principal, "mm_database_restore_plan", { archiveRef: source.archiveRef, expectedSha256: "0".repeat(64) })) + const prepared = await invokeMcpTool(principal, "mm_database_restore_plan", { archiveRef: source.archiveRef, expectedSha256: source.sha256 }) as { planId: string; state: string } + assert.equal(prepared.state, "awaiting-approval") + await assert.rejects(applyPlan(principal, prepared.planId, "restore-acceptance")) + await approvePlan(prepared.planId, actor, true) + + // Legacy direct restore must respect protected objects in every domain. + const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,reason,created_by) VALUES($1,$2::jsonb,'fixture',$3)", [protection, JSON.stringify({ kind: "user", id: "protected-user", protection: "deny" }), actor.id]) + await assert.rejects(restoreSystemDatabaseBackup(dump.buffer)) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + await pool.query("UPDATE mcp_restore_acceptance_marker SET value='after'") + const generation = await accessGeneration() + + // Emulates the admitted HTTP apply request: runner must wait, never deadlock it. + const restoreStartedAt = Date.now() + if (dataExpectation) { + // Owned-test observation only: capture after migrations release their + // advisory lease, before the production restore resumes consumers. + pool.connect = function (...args: unknown[]) { + if (args.length) return Reflect.apply(originalConnect, pool, args) + return (Reflect.apply(originalConnect, pool, []) as Promise).then(client => { + let migrationClient = false + return new Proxy(client, { + get(target, property) { + if (property === "query") return (...queryArgs: unknown[]) => { + const sql = queryArgs[0] + if (typeof sql === "string" && sql.includes("CREATE TABLE IF NOT EXISTS schema_migrations")) migrationClient = true + const result = Reflect.apply(target.query, target, queryArgs) + if (migrationClient && sql === "RESET statement_timeout" && queryArgs.length === 1) { + return Promise.resolve(result).then(async value => { + assert.equal(++migrationCaptures, 1) + await dataExpectation!.compareBeforeConsumers() + return value + }) + } + return result + } + const value = Reflect.get(target, property, target) + return typeof value === "function" ? value.bind(target) : value + }, + }) + }) + } as typeof pool.connect + } + const operation = await sharedDatabaseGate.request(() => applyPlan(principal, prepared.planId, "restore-acceptance")) + let receipt: Record = {} + for (let i = 0; i < 600; i++) { + receipt = await readRestoreReceipt(operation.id, actor).catch(() => ({})) + if (["applied", "unknown", "blocked"].includes(String(receipt.state))) break + await delay(100) + } + assert.equal(receipt.state, "applied", JSON.stringify(receipt)) + assert.equal(receipt.accessRevoked, true) + pool.connect = originalConnect + if (dataExpectation) { + await stopSchedulerForDatabaseOperation() + await stopTrafficFlowListenerForDatabaseOperation() + await (await import("../../services/database-background-work.js")).databaseBackgroundWork.drain() + const dataInput = { operationId: operation.id, receipt, actorId: actor.id, startedAt: restoreStartedAt } + await assert.rejects(dataExpectation.compareAfterRestore({ ...dataInput, actorId: "foreign-owner" })) + await assert.rejects(dataExpectation.compareAfterRestore({ ...dataInput, operationId: randomUUID() })) + await assert.rejects(dataExpectation.compareAfterRestore({ ...dataInput, receipt: { ...receipt, accessRevoked: false } })) + console.log("Owned terminal restore data equivalence:", JSON.stringify(await dataExpectation.compareAfterRestore(dataInput))) + assert.equal(migrationCaptures, 1) + assert.equal((await dataExpectation.compareAfterRestore(dataInput)).auditRecordsObserved, 1) + } + assert.equal((await pool.query("SELECT value FROM mcp_restore_acceptance_marker")).rows[0].value, "before") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE id=$1", [operation.id])).rows[0].n, 0) + assert.notEqual(await accessGeneration(), generation) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_owner_revocations WHERE owner_id=$1", [revokedOwner.id])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_portal_events WHERE id=$1", [eventId])).rows[0].n, 0) + assert.deepEqual(await readHistoricalAccess(), beforeDumpAccess) + assert.deepEqual(await readFile(cutoffFile), cutoffBytes) + // Both paths must report the external owner cutoff, independently of the + // blanket generation change that also invalidates older connections. + await assert.rejects(assertPortalClaimsCurrent(revokedOwner), { code: "OWNER_CLAIMS_REVOKED" }) + await assert.rejects(authenticateMcpToken(oldAccess.token), { code: "OWNER_CLAIMS_REVOKED" }) + await assert.rejects(validatePrincipal(oldPrincipal), { code: "OWNER_CLAIMS_REVOKED" }) + await assert.rejects(authenticateMcpToken(issued.token)) + await assert.rejects(readRestoreReceipt(operation.id, { ...actor, permissions: ["mm:mcp:read"] })) + // The applied receipt is durable before the terminal callback releases its + // last SQL lease/exclusive admission. Await that existing owner, rather than + // racing its cleanup with the stale-callback/no-replay assertion below. + await waitForDatabaseRetirement() + await assert.rejects(issue(revokedOwner), { code: "OWNER_CLAIMS_REVOKED" }) + const refreshedOwnerAccess = await issue({ ...revokedOwner, issuedAt: cutoff + 1 }) + const refreshedOwner = await validatePrincipal(await authenticateMcpToken(refreshedOwnerAccess.token)) + assert.equal(refreshedOwner.owner_id, revokedOwner.id) + assert.equal(refreshedOwner.generation, await accessGeneration()) + assert.notEqual(refreshedOwner.generation, beforeDumpAccess.generation) + // A stale callback or process replay cannot invoke pg_restore a second time. + await pool.query("UPDATE mcp_restore_acceptance_marker SET value='after-receipt'") + await executeMcpDatabaseRestore(operation.id, principal) + assert.equal((await pool.query("SELECT value FROM mcp_restore_acceptance_marker")).rows[0].value, "after-receipt") + assert.equal((await readRestoreReceipt(operation.id, actor)).state, "applied") + if (dataExpectation) { + console.log("Owned restore data negative cases: foreign owner, wrong operation and unconfirmed receipt refused; repeat validation writes no audit; full equality observed before consumers resume") + } + console.log("✓ Terminal restore: real pg_dump/pg_restore transaction, encrypted human upload, approval, global protection, request drain, external receipt, restored live credential refused by external Portal cutoff, post-cutoff claims admitted and no replay") + + // Archive authentication is checked before a terminal attempt can replace data. + await updateMcpSettings({ enabled: true }, actor) + const next = await issue(), nextPrincipal = await authenticateMcpToken(next.token), tampered = await uploadRestoreArchive(dump.buffer, actor) + const badPlan = await invokeMcpTool(nextPrincipal, "mm_database_restore_plan", { archiveRef: tampered.archiveRef, expectedSha256: tampered.sha256 }) as { planId: string } + await approvePlan(badPlan.planId, actor, true) + await writeFile(path.join(mcpStateDirectory(), "restore-vault", tampered.archiveRef + ".archive.bin"), Buffer.from("corrupted")) + const generationBeforeCorruption = await accessGeneration() + const bad = await applyPlan(nextPrincipal, badPlan.planId, "corrupted-archive") + // Admission, durable receipt writes and authority checks are asynchronous. + // Use the same terminal-state budget as the successful restore above; never + // interpret a still-running receipt as a rejection or dispatch apply again. + for (let i = 0; i < 600; i++) { + receipt = await readRestoreReceipt(bad.id, actor).catch(() => ({})) + if (["blocked", "applied", "unknown"].includes(String(receipt.state))) break + await delay(100) + } + assert.equal(receipt.state, "blocked", JSON.stringify(receipt)) + await waitForDatabaseRetirement() + assert.equal(await accessGeneration(), generationBeforeCorruption) + assert.equal((await pool.query("SELECT value FROM mcp_restore_acceptance_marker")).rows[0].value, "after-receipt") + console.log("✓ Tampered archive is blocked before data replacement") +} finally { + pool.connect = originalConnect + await stopSchedulerForDatabaseOperation() + await stopTrafficFlowListenerForDatabaseOperation() + await dataExpectation?.close() + await closePool() +} diff --git a/backend/src/modules/mcp/database-restore.ts b/backend/src/modules/mcp/database-restore.ts new file mode 100644 index 00000000..7da6e833 --- /dev/null +++ b/backend/src/modules/mcp/database-restore.ts @@ -0,0 +1,176 @@ +import { createHash, randomUUID } from "node:crypto" +import { mkdir, open, readFile, readdir, unlink } from "node:fs/promises" +import path from "node:path" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { validateSystemDatabaseRestoreArchive } from "../../services/system-database-restore-execution.js" +import { restoreSystemDatabaseBackup, assertSystemDatabaseRestoreTools, withSystemDatabaseRestoreAdmission } from "../../services/system-database-backup.js" +import { withVerifiedConfigurationChange } from "../../services/config-mutation-policy.js" +import { accessGeneration, decryptBinary, encryptBinary, decryptSecret, McpError, mcpStateDirectory } from "./security.js" +import { audit, requestAccountingWrite, validatePrincipal, type McpPrincipal } from "./store.js" +import { publishStateFile } from "./state-file-publish.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" +import { acquireApplicationMutationTransactionLock } from "../../services/config-mutation-lock.js" +import { tryAdmitPartitionWriter } from "../../services/partition-write-transaction.js" +import {assertOriginalMcpTemporaryGrant,type OriginalServiceGrant} from "./temporary-grant-origin.js" +import {reconcileRestoreReceiptAudit,inspectRestoreReceiptAudit} from "../../services/database-restore-audit.js" + +export const MAX_RESTORE_ARCHIVE_BYTES = 64 * 1024 * 1024 +const metadataSchema = z.object({ id: z.string().uuid(), ownerId: z.string().min(1), sha256: z.string().regex(/^[a-f0-9]{64}$/), sizeBytes: z.number().int().positive().max(MAX_RESTORE_ARCHIVE_BYTES), generation: z.string(), expiresAt: z.string().datetime(), createdAt: z.string().datetime() }).strict() +type Archive = z.infer +const vault = () => path.join(mcpStateDirectory(), "restore-vault") +const filename = (id: string, suffix: string) => path.join(vault(), z.string().uuid().parse(id) + suffix) +async function durableWrite(target: string, content: Buffer | string, exclusive = false) { + await mkdir(vault(), { recursive: true, mode: 0o700 }) + const destination = exclusive ? target : target + "." + randomUUID() + ".tmp" + const handle = await open(destination, "wx", 0o600) + try { await handle.writeFile(content); await handle.sync() } finally { await handle.close() } + if (!exclusive) { + try { await publishStateFile(destination, target) } + catch (error) { await unlink(destination).catch(() => {}); throw error } + } +} +async function archive(id: string, ownerId: string): Promise { + let data: Archive + try { data = metadataSchema.parse(JSON.parse(await readFile(filename(id, ".archive.json"), "utf8"))) } + catch { throw new McpError("NOT_FOUND", "Архив восстановления не найден", 404) } + if (data.id !== id || data.ownerId !== ownerId) throw new McpError("NOT_FOUND", "Архив восстановления не найден", 404) + if (Date.parse(data.expiresAt) <= Date.now() || data.generation !== await accessGeneration()) throw new McpError("PLAN_EXPIRED", "Архив или поколение доступа устарели", 409) + try { await readFile(filename(id, ".used")); throw new McpError("STATE_CONFLICT", "Архив уже использован; для нового восстановления загрузите его заново", 409) } + catch (error) { if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error } + return data +} +export async function uploadRestoreArchive(buffer: Buffer, actor: AuthUser) { + if (!hasPermission(actor.permissions, "mm:settings:admin")) throw new McpError("PERMISSION_DENIED", "Для архива нужны административные права системы") + if (!Buffer.isBuffer(buffer) || !buffer.length || buffer.length > MAX_RESTORE_ARCHIVE_BYTES) throw new McpError("INVALID_INPUT", "Ожидается архив PostgreSQL размером до 64 МиБ", 400) + validateSystemDatabaseRestoreArchive(buffer) + return requestAccountingWrite(async client => { + if (!await tryAdmitPartitionWriter(client)) throw new McpError("PROTOCOL_STORAGE_BUSY", "Выполняется операция с базой приложения", 503) + await acquireApplicationMutationTransactionLock(client) + const data = await prepareArchiveUpload(buffer, actor) + const receipt = { archiveRef: data.id, sha256: data.sha256, sizeBytes: data.sizeBytes, expiresAt: data.expiresAt } + // A lost COMMIT acknowledgement may have left an audit referencing the archive. + try { + await durableWrite(filename(data.id, ".archive.bin"), await encryptBinary(buffer), true) + await durableWrite(filename(data.id, ".archive.json"), JSON.stringify(data), true) + await audit(client, actor.id, "database.archive.upload", null, receipt) + return receipt + } catch (error) { + await unlink(filename(data.id, ".archive.bin")).catch(() => {}) + await unlink(filename(data.id, ".archive.json")).catch(() => {}) + throw error + } + }) +} +async function prepareArchiveUpload(buffer: Buffer, actor: AuthUser): Promise { + await mkdir(vault(), { recursive: true, mode: 0o700 }) + const files = (await readdir(vault())).filter(file => /^[a-f0-9-]{36}\.archive\.json$/.test(file)) + let active = 0 + for (const file of files) { + const raw = metadataSchema.safeParse(JSON.parse(await readFile(path.join(vault(), file), "utf8"))) + if (!raw.success) continue + if (Date.parse(raw.data.expiresAt) > Date.now()) { active++; continue } + if (file !== raw.data.id + ".archive.json") continue + await unlink(filename(raw.data.id, ".archive.bin")).catch(() => {}) + await unlink(filename(raw.data.id, ".archive.json")).catch(() => {}) + } + if (active >= 4) throw new McpError("BUDGET_EXCEEDED", "Уже загружены четыре действующих архива; дождитесь их истечения", 429) + return { id: randomUUID(), ownerId: actor.id, sha256: createHash("sha256").update(buffer).digest("hex"), sizeBytes: buffer.length, generation: await accessGeneration(), createdAt: new Date().toISOString(), expiresAt: new Date(Math.min(Date.now() + 900000, actor.expiresAt ?? Date.now() + 900000)).toISOString() } +} +export async function readRestoreReceipt(id: string, actor: AuthUser) { + if (!hasPermission(actor.permissions, "mm:settings:admin")) throw new McpError("PERMISSION_DENIED", "Нужны административные права системы") + try { return JSON.parse(await readFile(filename(id, ".receipt.json"), "utf8")) as Record } + catch { throw new McpError("NOT_FOUND", "Внешняя квитанция восстановления не найдена", 404) } +} +export async function readRestoreReceiptAuditStatus(id:string,actor:AuthUser){ + await readRestoreReceipt(id,actor) + return inspectRestoreReceiptAudit(pool,mcpStateDirectory(),id) +} +export function registerDatabaseRestore(register: typeof add) { + register("database.restore", "mm_database_restore_plan", "Восстановить БД из загруженного человеком архива", "settings", "mm:settings:admin", "write", "global", z.object({ archiveRef: z.string().uuid(), expectedSha256: z.string().regex(/^[a-f0-9]{64}$/) }).strict(), { + prepare: async (input, context) => { + const source = await archive(String(input.archiveRef), context.connection.owner_id) + if (source.sha256 !== input.expectedSha256) throw new McpError("STATE_CONFLICT", "Хеш архива не совпадает", 409) + try { await assertSystemDatabaseRestoreTools() } catch { throw new McpError("UNSUPPORTED_CAPABILITY", "На backend недоступны pg_restore/psql; установите PostgreSQL client tools", 503) } + const ids = (await pool.query("SELECT id FROM servers")).rows.map(row => Number(row.id)) + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Полное восстановление затрагивает серверы вне подключения") + return { target: { scope: "global" }, objects: ids.map(serverId => ({ kind: "server", id: String(serverId), serverId })), snapshots: [], steps: [{ kind: "service", service: "database.restore", input: { archiveRef: source.id, sha256: source.sha256 } }], before: { archiveRef: source.id, sha256: source.sha256, sizeBytes: source.sizeBytes }, requiresHuman: true, + warnings: ["Данные приложения будут заменены. Старые MCP-токены, grants и планы будут аннулированы до записи.", "После restore результат читается человеком из внешней квитанции: журнал исходной операции может исчезнуть вместе со старой БД.", "Неопределённый restore не повторяется. Для нового восстановления нужна новая загрузка архива."] } + }, + }) +} +/** This terminal operation must never hold a database-request admission while asking for an exclusive restore. */ +export async function executeMcpDatabaseRestore(operationId: string, principal: McpPrincipal,originalGrant?:OriginalServiceGrant) { + return withSystemDatabaseRestoreAdmission(async () => { + const lease = await pool.connect() + let locked = false, unsafe = false + try { + locked = (await lease.query<{ locked: boolean }>("SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS locked", ["mcp-operation:" + operationId])).rows[0].locked + if (locked) await executeRestore(operationId, principal,originalGrant) + } finally { + if (locked) try { await lease.query("SELECT pg_advisory_unlock(hashtextextended($1,0))", ["mcp-operation:" + operationId]) } catch { unsafe = true } + lease.release(unsafe) + } + }) +} +async function executeRestore(operationId: string, principal: McpPrincipal,originalGrant?:OriginalServiceGrant) { + const { loadPlan } = await import("./runtime.js") + const { rows: [operation] } = await pool.query("SELECT * FROM mcp_operations WHERE id=$1 AND connection_id=$2", [operationId, principal.connectionId]) + if (!operation || operation.state !== "pending") return + const plan = await loadPlan(operation.plan_id), evidence = JSON.parse(await decryptSecret(plan.evidence.sealed)) as ChangeEvidence + const payload = JSON.parse(await decryptSecret(plan.payload.sealed)) as { archiveRef: string; expectedSha256: string } + let receipt: Record = { operationId, archiveRef: payload.archiveRef, state: "running", startedAt: new Date().toISOString(), automaticRetry: false } + try { await durableWrite(filename(operationId, ".receipt.json"), JSON.stringify(receipt), true) } + catch (error) { if (error instanceof Error && "code" in error && error.code === "EEXIST") return; throw error } + let attempted = false, stage = "authority", restoreActorId:string|undefined + const assertAuthority = async () => { + await assertOriginalMcpTemporaryGrant(pool,principal.connectionId,principal.credentialId,originalGrant) + const connection = await validatePrincipal(principal) + if (!plan.approved_by || plan.state !== "executing" || plan.operation_id !== "database.restore" || plan.expires_at.getTime() <= Date.now() || plan.policy_version !== connection.policy.version || plan.generation !== await accessGeneration() || !connection.policy.scope.allowGlobal || !connection.policy.scope.operationIds.includes("database.restore") || !hasPermission(connection.owner_permissions, "mm:settings:admin") || !hasPermission(connection.policy.permissions, "mm:settings:admin")) throw new McpError("PERMISSION_DENIED", "Допуск или подтверждение восстановления устарели") + const ids = (await pool.query("SELECT id FROM servers")).rows.map(row => Number(row.id)) + 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", "Восстановление заменяет запрещённый защищённый объект") + return { connection, ids } + } + try { + const { connection, ids } = await assertAuthority(), source = await archive(payload.archiveRef, connection.owner_id) + restoreActorId=connection.owner_id + stage = "archive-read" + if (source.sha256 !== payload.expectedSha256) throw new McpError("STATE_CONFLICT", "Архив подменён", 409) + const buffer = await decryptBinary(await readFile(filename(source.id, ".archive.bin"))) + 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 () => { + 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) + attempted = true + } })) + receipt = { ...receipt, state: "applied", finishedAt: new Date().toISOString(), accessRevoked: true, warnings: Object.keys(outcome).filter(key => key.endsWith("Error") && outcome[key as keyof typeof outcome]) } + stage = "receipt-publish" + await durableWrite(filename(operationId, ".receipt.json"), JSON.stringify(receipt)) + } catch (error) { + // The isolated destructive acceptance runner can print bounded diagnostics + // without archive SQL, server credentials, or arbitrary error text. + if (!(error instanceof McpError) && process.env.MMAPP_OWN_TERMINAL_RESTORE === "true" && process.env.NODE_ENV === "test") { + const message = error instanceof Error ? error.message : "" + const kinds = ["pg_restore", "psql", "already exists", "does not exist", "cannot drop", "constraint", "permission denied", "deadlock", "lock timeout", "out of shared memory", "connection", "syntax error", "duplicate key"].filter(value => message.toLowerCase().includes(value)) + const identifiers = [...message.matchAll(/\b(?:table|relation|constraint|index|column) "([a-z_][a-z0-9_]{0,100})"/g)].map(match => match[1]).slice(0, 10) + const code = error && typeof error === "object" && "code" in error && typeof error.code === "string" && /^(?:E[A-Z]{1,20}|[0-9A-Z]{5})$/.test(error.code) ? error.code : null + console.error("Owned restore failure diagnostic:", JSON.stringify({ stage, code, kinds, identifiers })) + } + receipt = { ...receipt, state: attempted ? "unknown" : "blocked", errorCode: error instanceof McpError ? error.code : "RESTORE_UNCONFIRMED", finishedAt: new Date().toISOString() } + await durableWrite(filename(operationId, ".receipt.json"), JSON.stringify(receipt)) + if (!attempted) await pool.query("UPDATE mcp_operations SET state='rejected',error_code=$2,finished_at=now() WHERE id=$1", [operationId, receipt.errorCode]).catch(() => {}) + } + // Audit failure is a separate pending/unconfirmed import AFTER durable + // applied receipt publication. It must never relabel confirmed restore. + if(receipt.state==="applied"){ + try{await reconcileRestoreReceiptAudit(pool,mcpStateDirectory(),operationId,restoreActorId)}catch{/* Startup reconciles the existing external receipt; never replay restore. */} + } +} diff --git a/backend/src/modules/mcp/device-catalog-actions.ts b/backend/src/modules/mcp/device-catalog-actions.ts new file mode 100644 index 00000000..117fda4b --- /dev/null +++ b/backend/src/modules/mcp/device-catalog-actions.ts @@ -0,0 +1,53 @@ +import { listContainersState } from "../../services/containers-live.js" +import { listCertificatesState } from "../../services/certificates-service.js" +import { catalogDevices, catalogPaging } from "./catalog-scope.js" +import { listFirewallAll } from "../../services/firewall-live.js" +import type { add } from "./actions.js" + +export function registerDeviceCatalogActions(register: typeof add) { + register("firewall.state.read", "mm_firewall_state_get", "Firewall IPv4/IPv6: правила, списки адресов, счётчики и неизвестные состояния", "network", "mm:network:read", "read", "servers", catalogPaging, { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context), result = await listFirewallAll(selected, true, false) + const offset = Number(input.offset), limit = Number(input.limit), observed = result.observed! + const collections = { rules: observed.rules, addressLists: observed.addressLists } + return { ...page, serverIds: selected.map(server => server.id), failures: result.failures, + collections: Object.fromEntries(Object.entries(collections).map(([key, rows]) => [key, { items: rows.slice(offset, offset + limit), total: rows.length, nextOffset: offset + limit < rows.length ? offset + limit : null }])), + incomplete: page.incomplete || result.failures.length > 0 || Object.values(collections).some(rows => rows.slice(offset, offset + limit).length < rows.length), + unknownRuleStates: observed.rules.filter(row => row.enabled === null || row.dynamic === null || row.log === null).length, + unknownRuleCounters: observed.rules.filter(row => row.hits === null).length, + unknownAddressStates: observed.addressLists.filter(row => row.disabled === null || row.dynamic === null).length, + observedAt: new Date().toISOString(), timestampKind: "backend-receipt", packetMatchingVerified: false } + }, + }) + register("containers.state.read", "mm_containers_state_get", "Наблюдаемое состояние контейнеров без значений env и команд", "network", "mm:network:read", "read", "servers", catalogPaging, { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context) + const result = await listContainersState(selected), offset = Number(input.offset), limit = Number(input.limit) + let incomplete = page.incomplete + const servers = result.servers.map(server => { + const rows = [...server.containers].sort((a, b) => a.rosId.localeCompare(b.rosId)) + incomplete ||= server.availability !== "available" || server.failures.length > 0 || rows.slice(offset, offset + limit).length < rows.length || rows.slice(offset, offset + limit).some(row => row.envs.length > 100) + return { ...server, containers: rows.slice(offset, offset + limit).map(container => ({ ...container, + // Env is a key/value array: generic object-key masking cannot detect + // credentials inside its value. Never pass any value or free-form cmd. + envs: container.envs.slice(0, 100).map(env => ({ key: env.key, value: "[redacted]" })), cmd: undefined, + envsTruncated: container.envs.length > 100, + })), total: server.containers.length, nextOffset: offset + limit < server.containers.length ? offset + limit : null } + }) + return { ...result, ...page, servers, incomplete, source: "shared REST container collector", environmentValuesIncluded: false, commandsIncluded: false, forwardingVerified: false } + }, + }) + register("certificates.state.read", "mm_certificates_state_get", "Наблюдаемые сертификаты: сроки, статус и ошибки устройств", "certificates", "mm:certificates:read", "read", "servers", catalogPaging, { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context) + const result = await listCertificatesState(selected), offset = Number(input.offset), limit = Number(input.limit) + let incomplete = page.incomplete + const servers = result.servers.map(server => { + const rows = [...server.certificates].sort((a,b) => a.id.localeCompare(b.id)) + incomplete ||= server.error !== null || rows.slice(offset, offset + limit).length < rows.length + return { ...server, certificates: rows.slice(offset, offset + limit), total: rows.length, nextOffset: offset + limit < rows.length ? offset + limit : null } + }) + return { ...result, ...page, servers, incomplete } + }, + }) +} diff --git a/backend/src/modules/mcp/diagnostic-cancellation-source.sql-helper.ts b/backend/src/modules/mcp/diagnostic-cancellation-source.sql-helper.ts new file mode 100644 index 00000000..20da373d --- /dev/null +++ b/backend/src/modules/mcp/diagnostic-cancellation-source.sql-helper.ts @@ -0,0 +1,99 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" + +/** Read/lock acceptance of existing source + current grant components only. + * Does not import, execute or simulate the pending production host patch. */ +export async function acceptDiagnosticCancellationSourceSql(serverId: number) { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../../db/index.js") + const security = await import("./security.js") + const { effectiveMcpConnection } = await import("./store.js") + const { loadDiagnosticCancellationSource, readServerProbeExecutionSource } = await import("./diagnostic-cancellation-source.js") + const operationId = randomUUID(), connectionId = randomUUID(), originalCredentialId = randomUUID(), credentialId = randomUUID(), receiptId = randomUUID(), planId = randomUUID() + const ownerId = "owned-source-" + randomUUID(), at = Date.now(), oldExpiry = new Date(at - 3600000).toISOString(), until = new Date(at + 3600000).toISOString() + const principal = { connectionId, credentialId, credentialExpiresAt: until } + const policy = { id: connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: until, scope: { serverIds: [serverId], operationIds: ["servers.probes.run"], allowGlobal: false } } + const binding = { version: 1, operation: "servers.probes.run", principal: { connectionId, credentialId: originalCredentialId, credentialExpiresAt: oldExpiry }, ownerId, issuedAt: at - 7200000, authorityExpiresAt: Date.parse(oldExpiry), prepared: { receiptId, serverId, tool: "ping", expiresAt: oldExpiry, bindingCiphertext: await security.encryptSecret("owned synthetic provider request") }, bindingProof: "a".repeat(64), stageBudget: 2 } + const proof = (value: unknown) => security.privateSecretFingerprint(security.fingerprint(value), "server-probe-mcp-binding-v1") + const input = { receiptId, serverId, inputDigest: await proof(binding), bindingCiphertext: await security.encryptSecret(JSON.stringify(binding)) } + const step = { kind: "service", service: "servers.probes.run", input } + const evidence = { steps: [step], target: { scope: "servers", serverIds: [serverId] }, requiresHuman: true, verificationScope: "acknowledgement", mutationCost: 2, validUntil: oldExpiry } + const sealedEvidence = { sealed: await security.encryptSecret(JSON.stringify(evidence)) } + const generation = await security.accessGeneration() + const previousEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled + const cases: string[] = [] + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + try { + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned source acceptance',$2,$3::jsonb,$4::jsonb,$5,$6,clock_timestamp())", [connectionId, ownerId, JSON.stringify(policy.permissions), JSON.stringify(policy), generation, until]) + for (const [id, expiry] of [[originalCredentialId, oldExpiry], [credentialId, until]]) await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [id, connectionId, randomUUID(), expiry]) + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by) VALUES($1,$2,'servers.probes.run',1,1,$3,'{}',$4::jsonb,'executing',$5,$6)", [planId, connectionId, generation, JSON.stringify(sealedEvidence), oldExpiry, ownerId]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'pending')", [operationId, planId, connectionId, randomUUID()]) + const read = async () => { + const client = await pool.connect() + try { + await client.query("BEGIN"); await client.query("SET LOCAL statement_timeout='2000ms'") + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0))") + return await loadDiagnosticCancellationSource(client, operationId, { connectionId, ownerId }) + } finally { await client.query("ROLLBACK"); client.release(true) } + } + const snapshotRead = async () => { + const client = await pool.connect() + try { + await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + await client.query("SET LOCAL statement_timeout='2000ms'") + const result = await readServerProbeExecutionSource(client, operationId, { connectionId, ownerId }) + const settings = (await client.query("SELECT current_setting('transaction_read_only') AS read_only, current_setting('transaction_isolation') AS isolation")).rows[0] + assert.equal(settings.read_only, "on"); assert.equal(settings.isolation, "repeatable read") + assert(Object.isFrozen(result) && Object.isFrozen(result.originalContext) && Object.isFrozen(result.originalContext.identity)) + return result + } finally { await client.query("ROLLBACK"); client.release(true) } + } + const source = await read() + const snapshotSource = await snapshotRead() + const { originalContext, ...historicalSource } = snapshotSource + assert.deepEqual(historicalSource, source) + assert.equal(originalContext.identity.executionId, operationId) + assert.equal(originalContext.identity.expiresAt, Date.parse(oldExpiry)) + assert.equal(originalContext.identity.kind, "mcp") + cases.push("actual REPEATABLE READ READ ONLY snapshot authenticates original expired context without row locks; locked source digest parity") + assert.equal(source.scope.receiptId, receiptId); assert.equal(source.scope.authorityExpiresAt, oldExpiry) + assert.equal(source.originalCredentialId, originalCredentialId); assert.equal(source.sourceState, "pending"); assert.equal(source.terminal, false) + assert.equal((await effectiveMcpConnection(principal)).owner_id, ownerId) + await assert.rejects(effectiveMcpConnection({ ...principal, credentialId: originalCredentialId, credentialExpiresAt: oldExpiry })) + cases.push("actual encrypted pending source with no step preserves expired original credential while independent current credential validates") + await pool.query("UPDATE mcp_operations SET state='running' WHERE id=$1", [operationId]) + await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", [operationId, JSON.stringify(security.redact(step))]) + assert.equal((await read()).sourceState, "running") + cases.push("actual running operation requires exact intent step and source is loaded under owned SQL row locks") + await pool.query("UPDATE mcp_operation_steps SET intended_state='{}'::jsonb WHERE operation_id=$1", [operationId]) + await assert.rejects(read()); await assert.rejects(snapshotRead()) + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1", [operationId, JSON.stringify(security.redact(step))]) + cases.push("actual intended step drift refuses authenticated source") + await pool.query("UPDATE mcp_plans SET operation_version=2 WHERE id=$1", [planId]); await assert.rejects(read()) + await pool.query("UPDATE mcp_plans SET operation_version=1 WHERE id=$1", [planId]) + cases.push("actual operation version drift refuses source") + const forged = { ...evidence, steps: [{ ...step, input: { ...input, inputDigest: "b".repeat(64) } }] } + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [planId, JSON.stringify({ sealed: await security.encryptSecret(JSON.stringify(forged)) })]); await assert.rejects(read()) + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [planId, JSON.stringify(sealedEvidence)]) + cases.push("actual encrypted evidence with forged keyed input digest refuses source") + for (const state of ["rejected", "partial", "applied", "unknown", "failed", "cancelled"]) { + await pool.query("UPDATE mcp_operations SET state=$2 WHERE id=$1", [operationId, state]) + await pool.query("UPDATE mcp_plans SET state='completed' WHERE id=$1", [planId]) + const terminal = await read(); assert.equal(terminal.terminal, true); assert.equal(terminal.sourceState, state) + } + cases.push("actual runner terminal states including rejected and partial are recognized as non-executable terminal sources") + const grantId = randomUUID() + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grantId, connectionId, JSON.stringify({ id: grantId, credentialId, startsAt: new Date(at - 1000).toISOString(), expiresAt: until, scope: { serverIds: [], operationIds: ["servers.probes.run"], allowGlobal: false } })]) + assert.deepEqual((await effectiveMcpConnection(principal)).policy.scope.serverIds, []) + assert.deepEqual((await read()).serverIds, [serverId]) + cases.push("actual temporary grant narrows current authority independently of unchanged original source scope") + await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [credentialId]) + await assert.rejects(effectiveMcpConnection(principal)) + cases.push("actual current credential revocation refuses grant validation without rewriting source") + return { cases, actualSql: true, productionCrypto: true, syntheticOriginalExecution: true, productionHostExecuted: false, cancellationsWritten: 0, nativeCalls: 0 } + } finally { await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousEnabled]) } +} diff --git a/backend/src/modules/mcp/diagnostic-cancellation-source.test.ts b/backend/src/modules/mcp/diagnostic-cancellation-source.test.ts new file mode 100644 index 00000000..4ff7cf95 --- /dev/null +++ b/backend/src/modules/mcp/diagnostic-cancellation-source.test.ts @@ -0,0 +1,125 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { mkdtemp, readdir, rm, rmdir } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { createDiagnosticCancellationSourceForTests, createReadServerProbeExecutionSourceForTests, readServerProbeExecutionSource } from "./diagnostic-cancellation-source.js" +import { fingerprint, redact } from "./security.js" + +const proof = async (value: unknown) => createHash("sha256").update(fingerprint(value)).digest("hex") +function fixture() { + const operationId = randomUUID(), connectionId = randomUUID(), credentialId = randomUUID(), receiptId = randomUUID() + const owner = { connectionId, ownerId: "owner" }, sealed = new Map() + const binding = { version: 1, operation: "servers.probes.run", principal: { connectionId, credentialId, credentialExpiresAt: "2020-01-01T00:00:00.000Z" }, ownerId: owner.ownerId, issuedAt: 1, authorityExpiresAt: 1577836800000, prepared: { receiptId, serverId: 9, tool: "ping", expiresAt: "2020-01-01T00:00:00.000Z", bindingCiphertext: "original-inner" }, bindingProof: "a".repeat(64), stageBudget: 2 } + const input = { receiptId, serverId: 9, inputDigest: "", bindingCiphertext: "original-binding" } + const step = { kind: "service", service: "servers.probes.run", input } + const evidence = { steps: [step], target: { scope: "servers", serverIds: [9] }, requiresHuman: true, verificationScope: "acknowledgement", validUntil: binding.prepared.expiresAt, mutationCost: 2 } + const row: Record = { operation_id: operationId, connection_id: connectionId, owner_id: owner.ownerId, operation_state: "running", operation_name: "servers.probes.run", operation_version: 1, plan_state: "executing", approved_by: "human", evidence: { sealed: "original-evidence" }, step_index: 0, step_state: "intent", intended_state: null } + let rows = [row], calls = 0 + const refresh = async () => { input.inputDigest = await proof(binding); sealed.set(input.bindingCiphertext, JSON.stringify(binding)); sealed.set("original-evidence", JSON.stringify(evidence)); row.intended_state = redact(step) } + const crypto = { proof, open: async (value: string) => { const raw = sealed.get(value); if (!raw) throw new Error("PRIVATE_SECRET"); return raw } } + const load = createDiagnosticCancellationSourceForTests(crypto), read = createReadServerProbeExecutionSourceForTests(crypto) + const readSql: string[] = [] + const client = { query: async (sql: string, values?: unknown[]) => { calls++; assert.ok(sql.includes("LEFT JOIN mcp_operation_steps")); assert.deepEqual(values, [operationId, connectionId, owner.ownerId]); assert.match(sql,/FOR UPDATE OF o,p,c/); assert.ok(!/INSERT|UPDATE|DELETE/.test(sql.replace("FOR UPDATE OF o,p,c",""))); return { rows } } } + const snapshot = { query: async (sql: string, values?: unknown[]) => { + readSql.push(sql); assert.deepEqual(values, [operationId, connectionId, owner.ownerId]) + assert.ok(sql.includes("LEFT JOIN mcp_operation_steps")); assert.match(sql, /LIMIT 2$/) + assert.ok(!/FOR UPDATE|FOR SHARE|INSERT|UPDATE|DELETE|BEGIN|COMMIT/.test(sql)) + return { rows } + } } + return { load: () => load(client, operationId, owner), read: () => read(snapshot, operationId, owner), readSql, + readWithExistingKey: () => readServerProbeExecutionSource(snapshot, operationId, owner), + refresh, binding, input, evidence, row, sealed, setRows: (value: typeof rows) => { rows=value }, calls: () => calls, operationId, credentialId } +} +{ + const f=fixture(); await f.refresh(); const source=await f.load() + assert.equal(source.scope.executionId,f.operationId); assert.equal(source.scope.bindingProof,f.binding.bindingProof); assert.equal(source.originalCredentialId,f.credentialId) + assert.equal(source.scope.authorityExpiresAt,"2020-01-01T00:00:00.000Z"); assert.equal(source.terminal,false); assert.equal(f.calls(),1) + assert.ok(Object.isFrozen(source) && Object.isFrozen(source.scope) && Object.isFrozen(source.serverIds)) + assert.equal("originalContext" in source, false) + const original=source.sourceDigest + f.row.operation_state="pending"; f.row.step_index=null; f.row.step_state=null; f.row.intended_state=null + assert.equal((await f.load()).sourceState,"pending") + f.row.step_index=0; f.row.step_state="pending"; f.row.intended_state=redact(f.evidence.steps[0]); assert.equal((await f.load()).terminal,false) + f.row.operation_state="applied"; f.row.plan_state="completed"; f.row.step_state="acknowledged" + const terminal=await f.load(); assert.equal(terminal.terminal,true); assert.equal(terminal.sourceDigest,original) + f.row.approved_by="different-human"; assert.notEqual((await f.load()).sourceDigest,original) +} +const mutations: Array<(f:ReturnType)=>void|Promise> = [ + f=>{f.setRows([])}, f=>{f.setRows([f.row,f.row])}, + f=>{f.row.operation_id=randomUUID()}, f=>{f.row.connection_id=randomUUID()}, f=>{f.row.owner_id="foreign"}, + f=>{f.row.operation_name="servers.poll"}, f=>{f.row.operation_version=2}, f=>{f.row.approved_by=null}, + f=>{f.row.step_index=1}, f=>{f.row.step_state="applied"}, f=>{f.row.plan_state="approved"}, f=>{f.row.intended_state={ mismatch:true }}, + f=>{f.row.evidence={sealed:"missing"}}, f=>{f.sealed.set("original-evidence","x".repeat(2097153))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,steps:[...f.evidence.steps,...f.evidence.steps]}))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,requiresHuman:false}))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,verificationScope:"configuration"}))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,validUntil:"2030-01-01T00:00:00.000Z"}))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,target:{scope:"servers",serverIds:[8]}}))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,mutationCost:1}))}, + async f=>{f.binding.ownerId="foreign";await f.refresh()}, + async f=>{f.binding.principal.connectionId=randomUUID();await f.refresh()}, + async f=>{f.binding.stageBudget=1;f.evidence.mutationCost=1;await f.refresh()}, + f=>{f.sealed.set("original-binding",JSON.stringify({...f.binding,bindingProof:"b".repeat(64)}))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,steps:[{...f.evidence.steps[0],input:{...f.input,receiptId:randomUUID()}}]}))}, + f=>{f.sealed.set("original-evidence",JSON.stringify({...f.evidence,steps:[{...f.evidence.steps[0],input:{...f.input,serverId:8}}]}))}, + async f=>{Object.assign(f.binding,{unexpected:true});await f.refresh()}, +] +for (const mutate of mutations) { + const f=fixture();await f.refresh();await mutate(f) + await assert.rejects(f.load(),error=>error instanceof Error && !error.message.includes("PRIVATE_SECRET")) + await assert.rejects(f.read(),error=>error instanceof Error && !error.message.includes("PRIVATE_SECRET")) +} +{ + const f = fixture(); await f.refresh() + const locked = await f.load(), snapshot = await f.read() + assert.equal(snapshot.sourceDigest, locked.sourceDigest) + assert.deepEqual(Object.keys(snapshot).filter(key => key !== "originalContext"), Object.keys(locked)) + assert.equal(f.readSql.length, 1) + const context = snapshot.originalContext + assert.deepEqual(context, { identity: { kind: "mcp", ownerId: f.binding.ownerId, executionId: f.operationId, + connectionId: f.binding.principal.connectionId, credentialId: f.credentialId, issuedAt: 1, + expiresAt: 1577836800000, operationName: "servers.probes.run" }, sourceServerId: 9, tool: "ping" }) + assert.equal("destinationServerId" in context, false) + assert.ok(Object.isFrozen(snapshot) && Object.isFrozen(context) && Object.isFrozen(context.identity)) + assert.throws(() => { Object.assign(context.identity, { expiresAt: Date.now() + 300000 }) }, TypeError) + assert.throws(() => { Object.assign(context, { sourceServerId: 8 }) }, TypeError) + assert.equal(context.identity.expiresAt, f.binding.authorityExpiresAt) + assert(context.identity.expiresAt < Date.now()) + assert.equal(JSON.stringify(snapshot).includes("original-binding"), false) + assert.equal(JSON.stringify(snapshot).includes("original-inner"), false) + // The historical loader needs neither today's credential nor live authority. + f.row.operation_state = "unknown"; f.row.plan_state = "expired" + const terminal = await f.read(); assert.equal(terminal.terminal, true) + assert.equal(terminal.originalContext.identity.expiresAt, context.identity.expiresAt) + assert.equal(terminal.sourceDigest, snapshot.sourceDigest) +} +{ + const f = fixture() + Object.assign(f.binding.prepared, { tool: "bandwidth", destinationServerId: 12 }) + f.binding.stageBudget = 1; f.evidence.mutationCost = 1; f.evidence.target.serverIds = [9, 12] + await f.refresh() + const source = await f.read() + assert.deepEqual(source.serverIds, [9, 12]); assert.equal(source.originalContext.destinationServerId, 12) + assert.equal(source.originalContext.tool, "bandwidth"); assert.equal(source.stageBudget, 1) + assert.equal(source.originalContext.identity.credentialId, f.credentialId) +} +{ + const state = await mkdtemp(join(tmpdir(), "mm-legacy-source-test-")) + const previousState = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = state + try { + const f = fixture() + await f.refresh() + f.row.evidence = { sealed: Buffer.alloc(32).toString("base64") } + await assert.rejects(f.readWithExistingKey, { code: "STATE_CONFLICT" }) + assert.equal(f.readSql.length, 1) + assert.deepEqual(await readdir(state), [], "Historical source reads must leave missing encryption state untouched") + } finally { + if (previousState === undefined) delete process.env.MCP_STATE_DIR + else process.env.MCP_STATE_DIR = previousState + await rm(join(state, "encryption-key"), { force: true }) + await rmdir(state) + } +} +console.log(`diagnostic-cancellation-source: locked/read-only source, missing key, expired original contexts and ${mutations.length} shared tamper checks passed`) diff --git a/backend/src/modules/mcp/diagnostic-cancellation-source.ts b/backend/src/modules/mcp/diagnostic-cancellation-source.ts new file mode 100644 index 00000000..885dc87f --- /dev/null +++ b/backend/src/modules/mcp/diagnostic-cancellation-source.ts @@ -0,0 +1,126 @@ +import { z } from "zod" +import type { ProbeStorageScope, ProbeStorageSql } from "../../services/server-probe-journal-maintenance.js" +import type { ServerProbeJournalContext } from "../../services/server-probe-journal.js" +import { SERVER_PROBE_OPERATION } from "../../services/server-probe-authority.js" +import { openServerProbeMcpBinding } from "./server-probe-actions.js" +import { decryptSecret, decryptSecretExisting, fingerprint, privateFingerprinterExisting, privateSecretFingerprint, redact, McpError } from "./security.js" + +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() +interface SourceCrypto { open(value: string): Promise; proof(value: unknown): Promise } +export interface DiagnosticCancellationOwner { connectionId: string; ownerId: string } +export interface DiagnosticCancellationSource { + readonly scope: Readonly + readonly sourceDigest: string + readonly sourceState: string + readonly terminal: boolean + readonly serverIds: readonly number[] + readonly stageBudget: number + readonly originalCredentialId: string +} +export interface ServerProbeExecutionSource extends DiagnosticCancellationSource { + readonly originalContext: Readonly & { readonly identity: Readonly> } +} +const productionCrypto: SourceCrypto = { + open: decryptSecret, + proof: value => privateSecretFingerprint(fingerprint(value), "server-probe-mcp-binding-v1"), +} + +const sourceSelect = `/* diagnostic-cancellation:source */ + SELECT o.id AS operation_id,o.connection_id,c.owner_id,o.state AS operation_state, + p.operation_id AS operation_name,p.operation_version,p.state AS plan_state,p.approved_by, + CASE WHEN octet_length(p.evidence::text)<=2097152 THEN p.evidence ELSE NULL END AS evidence, + s.step_index,s.state AS step_state,s.intended_state + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_connections c ON c.id=o.connection_id + LEFT JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE o.id=$1 AND o.connection_id=$2 AND c.owner_id=$3 LIMIT 2` + +/** SQL-free authenticated historical decoding; deliberately no current-access + * check, clock or native/source lookup. Expired original contexts stay expired. */ +async function validateSourceRow(row: Record, operationId: string, owner: Readonly, crypto: SourceCrypto): Promise> { + if (row.operation_id !== operationId || row.connection_id !== owner.connectionId || row.owner_id !== owner.ownerId || row.operation_name !== SERVER_PROBE_OPERATION || row.operation_version !== 1 || typeof row.approved_by !== "string" || !row.approved_by.trim()) return deny() + const wrapper = sealedSchema.safeParse(row.evidence) + if (!wrapper.success || Buffer.byteLength(JSON.stringify(row.evidence), "utf8") > MAX_BYTES) return deny() + let evidence: Record + try { + const plaintext = await crypto.open(wrapper.data.sealed) + if (Buffer.byteLength(plaintext, "utf8") > MAX_BYTES) return deny() + const parsed: unknown = JSON.parse(plaintext) + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return deny() + evidence = parsed as Record + } catch { return deny() } + const steps = z.array(z.object({ kind: z.literal("service"), service: z.literal(SERVER_PROBE_OPERATION), input: z.record(z.string(), z.unknown()) }).strict()).length(1).safeParse(evidence.steps) + if (!steps.success) return deny() + const step = steps.data[0] + let opened: Awaited> + try { opened = await openServerProbeMcpBinding(step.input, crypto) } catch { return deny() } + const { binding, input } = opened + const serverIds = [...new Set([binding.prepared.serverId, ...(binding.prepared.destinationServerId !== undefined ? [binding.prepared.destinationServerId] : [])])].sort((a,b) => a-b) + const expectedBudget = binding.prepared.tool === "mtu" ? 11 : ["ping", "traceroute"].includes(binding.prepared.tool) ? 2 : 1 + if (binding.ownerId !== owner.ownerId || binding.principal.connectionId !== owner.connectionId || binding.stageBudget !== expectedBudget || evidence.mutationCost !== binding.stageBudget || evidence.requiresHuman !== true || evidence.verificationScope !== "acknowledgement" || evidence.validUntil !== binding.prepared.expiresAt || fingerprint(evidence.target) !== fingerprint({ scope: "servers", serverIds })) return deny() + const state = z.enum(["pending", "running", "applied", "unknown", "failed", "cancelled", "rejected", "partial"]).safeParse(row.operation_state) + if (!state.success) return deny() + const terminal = state.data !== "pending" && state.data !== "running" + // Queued operations have no step until dispatch in the current runner. + const absentStep = row.step_index === null && row.step_state === null && row.intended_state === null + if (state.data === "pending" ? row.plan_state !== "executing" || (!absentStep && row.step_state !== "pending") : state.data === "running" ? row.plan_state !== "executing" || row.step_state !== "intent" : !["executing", "completed", "cancelled", "expired", "invalidated"].includes(String(row.plan_state))) return deny() + if (!absentStep && (row.step_index !== 0 || fingerprint(row.intended_state) !== fingerprint(redact({ kind: "service", service: SERVER_PROBE_OPERATION, input })))) return deny() + if (state.data === "running" && absentStep) return deny() + const scope: Readonly = Object.freeze({ ownerId: binding.ownerId, receiptId: input.receiptId, identityKind: "mcp", executionId: operationId, bindingProof: binding.bindingProof, authorityExpiresAt: new Date(binding.authorityExpiresAt).toISOString() }) + const sourceDigest = await crypto.proof({ operationId, connectionId: owner.connectionId, ownerId: owner.ownerId, evidence: wrapper.data, intendedState: row.intended_state, approvedBy: row.approved_by }) + const identity = Object.freeze({ kind: "mcp" as const, ownerId: binding.ownerId, executionId: operationId, + connectionId: binding.principal.connectionId, credentialId: binding.principal.credentialId, + issuedAt: binding.issuedAt, expiresAt: binding.authorityExpiresAt, operationName: SERVER_PROBE_OPERATION }) + const originalContext = Object.freeze({ identity, sourceServerId: binding.prepared.serverId, + ...(binding.prepared.destinationServerId !== undefined ? { destinationServerId: binding.prepared.destinationServerId } : {}), tool: binding.prepared.tool }) + return Object.freeze({ scope, sourceDigest, sourceState: state.data, terminal, serverIds: Object.freeze(serverIds), stageBudget: binding.stageBudget, originalCredentialId: binding.principal.credentialId, originalContext }) +} + +function checkLookup(operationId: string, owner: Readonly) { + uuid.parse(operationId); uuid.parse(owner.connectionId) + if (!owner.ownerId || owner.ownerId.length > 200) return deny() +} +function sourceLoader(crypto: SourceCrypto) { + return async (client: ProbeStorageSql, operationId: string, owner: Readonly): Promise> => { + checkLookup(operationId, owner) + // Existing cancellation path retains the host-owned transaction and locks. + const { rows } = await client.query(sourceSelect + " FOR UPDATE OF o,p,c", [operationId, owner.connectionId, owner.ownerId]) + if (rows.length !== 1) return deny() + const source = await validateSourceRow(rows[0], operationId, owner, crypto) + // Preserve the existing cancellation result; historical context is private + // to the explicit read-only loader, not an expanded cancellation response. + return Object.freeze({ scope: source.scope, sourceDigest: source.sourceDigest, sourceState: source.sourceState, + terminal: source.terminal, serverIds: source.serverIds, stageBudget: source.stageBudget, originalCredentialId: source.originalCredentialId }) + } +} +function readSourceLoader(crypto: SourceCrypto) { + return async (client: ProbeStorageSql, operationId: string, owner: Readonly): Promise> => { + checkLookup(operationId, owner) + // Fixed SELECT without row locks. The trusted caller owns the bounded + // READ ONLY REPEATABLE READ snapshot; no SQL/lock-mode flag is accepted. + const { rows } = await client.query(sourceSelect, [operationId, owner.connectionId, owner.ownerId]) + if (rows.length !== 1) return deny() + return validateSourceRow(rows[0], operationId, owner, crypto) + } +} + +/** Internal host boundary: authenticate source only; host must check CURRENT + * cancellation authority separately before any durable cancellation write. */ +export const loadDiagnosticCancellationSource = sourceLoader(productionCrypto) +/** Test-only crypto seam. Never accept these dependencies from tool JSON. */ +export const createDiagnosticCancellationSourceForTests = sourceLoader +/** Historical authentication only; confers no execution/cancellation grant. */ +export const readServerProbeExecutionSource = readSourceLoader({ + open: decryptSecretExisting, + proof: async value => (await privateFingerprinterExisting("server-probe-mcp-binding-v1"))(fingerprint(value)), +}) +export const createReadServerProbeExecutionSourceForTests = readSourceLoader +/** Exact previously locked source projection for private history conversion. */ +export function authenticateServerProbeExecutionSourceRow(row: Record, operationId: string, + owner: Readonly) { + checkLookup(operationId, owner) + return validateSourceRow(row, operationId, owner, productionCrypto) +} diff --git a/backend/src/modules/mcp/diagnostic-cancellation-storage.sql.test.ts b/backend/src/modules/mcp/diagnostic-cancellation-storage.sql.test.ts new file mode 100644 index 00000000..1c39c7dc --- /dev/null +++ b/backend/src/modules/mcp/diagnostic-cancellation-storage.sql.test.ts @@ -0,0 +1,262 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile, realpath, mkdir } from "node:fs/promises" +import { basename, dirname, resolve } from "node:path" +import type { PoolClient, QueryConfig } from "pg" + +// Destructive acceptance is restricted to the runner's private temporary PG. +// All checks precede importing the configured pool or the approved host. +assert.equal(process.env.MMAPP_OWN_DIAGNOSTIC_CANCEL_HOST, "true") +const url = new URL(process.env.DATABASE_URL ?? "") +assert.equal(url.hostname, "127.0.0.1"); assert.equal(decodeURIComponent(url.username), "mcp_test") +assert.equal(url.pathname, "/postgres"); assert.equal(url.pathname.slice(1), process.env.MMAPP_OWN_SQL_DB) +assert(process.env.MCP_STATE_DIR) +const ownedState = resolve(process.env.MCP_STATE_DIR) +assert.equal(basename(ownedState), "state"); assert.match(basename(dirname(ownedState)), /^mm-mcp-test-[A-Za-z0-9-]+$/) +assert.equal(resolve(await realpath(dirname(ownedState))).toLowerCase(), dirname(ownedState).toLowerCase()) +await mkdir(ownedState, { recursive: true, mode: 0o700 }) +assert.equal(resolve(await realpath(ownedState)).toLowerCase(), ownedState.toLowerCase()) +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const security = await import("./security.js") +const { requestStorageDiagnosticCancellation, createStorageDiagnosticCancellationRequest, PROBE_STORAGE_LIMITS } = await import("./diagnostic-cancellation-storage.js") +const { createProbeJournalStorage, ProbeStorageUnknown } = await import("../../services/server-probe-journal-maintenance.js") +const { SERVER_PROBE_OPERATION } = await import("../../services/server-probe-authority.js") +const cases: string[] = [] +const pass = (name: string) => { cases.push(name); console.log("✓ Owned cancellation host: " + name) } +let serverId: number + +async function fixture(running = false) { + const ownerId = "cancel-host-" + randomUUID(), connectionId = randomUUID(), credentialId = randomUUID(), originalCredentialId = randomUUID() + const operationId = randomUUID(), planId = randomUUID(), receiptId = randomUUID(), at = Date.now() + const until = new Date(at + 3600000).toISOString(), originalUntil = new Date(at + 1800000).toISOString() + const principal = { connectionId, credentialId, credentialExpiresAt: until } + const policy = { id: connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: until, + scope: { serverIds: [serverId], operationIds: [SERVER_PROBE_OPERATION], allowGlobal: false } } + const binding = { version: 1, operation: SERVER_PROBE_OPERATION, principal: { connectionId, credentialId: originalCredentialId, credentialExpiresAt: originalUntil }, + ownerId, issuedAt: at - 1000, authorityExpiresAt: Date.parse(originalUntil), prepared: { receiptId, serverId, tool: "ping", expiresAt: originalUntil, + bindingCiphertext: await security.encryptSecret("owned synthetic request; no provider exists") }, bindingProof: "a".repeat(64), stageBudget: 2 } + const input = { receiptId, serverId, inputDigest: await security.privateSecretFingerprint(security.fingerprint(binding), "server-probe-mcp-binding-v1"), + bindingCiphertext: await security.encryptSecret(JSON.stringify(binding)) } + const step = { kind: "service", service: SERVER_PROBE_OPERATION, input } + const evidence = { steps: [step], target: { scope: "servers", serverIds: [serverId] }, requiresHuman: true, verificationScope: "acknowledgement", mutationCost: 2, validUntil: originalUntil } + const sealed = { sealed: await security.encryptSecret(JSON.stringify(evidence)) }, generation = await security.accessGeneration() + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned cancellation host',$2,$3::jsonb,$4::jsonb,$5,$6,clock_timestamp())", [connectionId, ownerId, JSON.stringify(policy.permissions), JSON.stringify(policy), generation, until]) + for (const [id, expiry] of [[credentialId, until], [originalCredentialId, originalUntil]]) await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [id, connectionId, randomUUID(), expiry]) + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by) VALUES($1,$2,$3,1,1,$4,'{}',$5::jsonb,'executing',$6,$7)", [planId, connectionId, SERVER_PROBE_OPERATION, generation, JSON.stringify(sealed), originalUntil, ownerId]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,$5)", [operationId, planId, connectionId, randomUUID(), running ? "running" : "pending"]) + if (running) await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", [operationId, JSON.stringify(security.redact(step))]) + const scope = { ownerId, receiptId, identityKind: "mcp" as const, executionId: operationId, bindingProof: binding.bindingProof, authorityExpiresAt: originalUntil } + return { ownerId, connectionId, credentialId, originalCredentialId, operationId, planId, receiptId, principal, policy, scope, binding, at } +} +type Fixture = Awaited> +const quota = async (ownerId: string) => (await pool.query("SELECT scope,admissions::text,reserved_files::text,reserved_bytes::text FROM server_probe_storage_usage WHERE scope=ANY($1::text[]) ORDER BY scope", [["global", "owner:" + ownerId]])).rows +async function counts(f: Fixture) { + return (await pool.query(`SELECT (SELECT count(*)::int FROM mcp_diagnostic_cancellations WHERE operation_id=$1) AS cancellations, + (SELECT count(*)::int FROM mcp_audit WHERE action='diagnostic.cancel-requested' AND payload->>'operationId'=$1::text) AS audits, + (SELECT count(*)::int FROM server_probe_storage_admissions WHERE receipt_id=$2) AS admissions`, [f.operationId, f.receiptId])).rows[0] +} +async function absent(f: Fixture, before: Awaited>) { + assert.deepEqual(await counts(f), { cancellations: 0, audits: 0, admissions: 0 }); assert.deepEqual(await quota(f.ownerId), before) +} +type Hook = (text: string, client: PoolClient) => Promise +async function proxyHost(work: () => Promise, hooks: { before?: Hook; after?: Hook } = {}) { + const saved = pool.connect, original = pool.connect.bind(pool), backends = new Set(), texts: string[] = [] + let checkouts = 0, nestedCallbacks = 0 + // Wrapping checked-out objects never mutates a raw client's query method. + // Pool.query's callback overload passes straight through to the real pool. + pool.connect = ((callback?: unknown) => { + if (typeof callback === "function") { nestedCallbacks++; return (original as (...args: unknown[]) => unknown)(callback) } + return original().then(client => { + checkouts++; backends.add((client as PoolClient & { processID: number }).processID) + const rawQuery = client.query.bind(client) + return new Proxy(client, { get(target, key) { + if (key === "query") return (...args: unknown[]) => { + if (typeof args.at(-1) === "function") return (rawQuery as (...args: unknown[]) => unknown)(...args) + return (async () => { + const text = typeof args[0] === "string" ? args[0] : (args[0] as QueryConfig).text + texts.push(text); await hooks.before?.(text, client) + const result = await (rawQuery as (...args: unknown[]) => Promise)(...args) + await hooks.after?.(text, client); return result + })() + } + const value = Reflect.get(target, key, target) + return typeof value === "function" ? value.bind(target) : value + } }) + }) + }) as typeof pool.connect + try { return { result: await work(), checkouts, backends, texts, nestedCallbacks } } + finally { pool.connect = saved } +} +async function activeClaim(f: Fixture) { + const storage = createProbeJournalStorage({ policy: { ...PROBE_STORAGE_LIMITS, retentionDays: 30 }, safetyWindowMs: 60000, + connect: async () => { const client = await pool.connect(); return { query: (text, values) => client.query(text, values), release: destroy => client.release(destroy) } }, authorize: async () => "c".repeat(64) }) + await storage.claim(f.scope, { deadlineAt: Date.now() + 120000 }, async sql => { + await sql.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,authority_issued_at,authority_expires_at,source_server_id,tool) + VALUES($1,$2,'mcp',$3,$4,'mcp',$5,$6,$7,$8,$9,'ping')`, [f.receiptId, f.binding.bindingProof, f.operationId, f.ownerId, f.connectionId, f.originalCredentialId, f.binding.issuedAt, f.scope.authorityExpiresAt, serverId]) + }) + return storage +} + +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length) + await pool.query("UPDATE traffic_settings SET enabled=false; UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false; UPDATE mcp_settings SET enabled=true,requests_retention_days=30") + const installed = (await pool.query("SELECT to_regclass('public.server_probe_storage_admissions') AS name")).rows[0].name + if (!installed) { + const ddl = await pool.connect() + try { await ddl.query(await readFile(new URL("../../db/proposals/server-probe-storage-v2.sql", import.meta.url), "utf8")) } finally { ddl.release(true) } + } + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('owned-cancel-host','127.0.0.1',9,true) RETURNING id")).rows[0].id) + { + const f = await fixture(true); const storage = await activeClaim(f), before = await quota(f.ownerId) + const observed = await proxyHost(() => requestStorageDiagnosticCancellation(f.principal, f.operationId)) + assert.deepEqual(observed.result, { operationId: f.operationId, cancelRequested: true, terminal: false, automaticReplay: false }) + assert.equal(observed.checkouts, 1); assert.equal(observed.backends.size, 1); assert.equal(observed.nestedCallbacks, 0) + assert(observed.texts.findIndex(text => text.includes("probe-storage:lock")) < observed.texts.findIndex(text => text.includes("diagnostic-cancellation:source"))) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }); assert.deepEqual(await quota(f.ownerId), before) + await requestStorageDiagnosticCancellation(f.principal, f.operationId) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }); assert.deepEqual(await quota(f.ownerId), before) + const row = (await pool.query("SELECT claim_token,cancelled_at FROM server_probe_storage_admissions WHERE receipt_id=$1", [f.receiptId])).rows[0] + assert.equal(row.claim_token, null); assert(row.cancelled_at) + await assert.rejects(storage.claim(f.scope, { deadlineAt: Date.now() + 120000 }, async () => assert.fail("Cancelled identity cannot execute again"))) + assert.deepEqual(await quota(f.ownerId), before) + pass("actual active v2 claim cancels once on one backend after storage lock; duplicate shares quota without debit/refund or replay") + } + { + const f = await fixture(); const before = await quota(f.ownerId) + await requestStorageDiagnosticCancellation(f.principal, f.operationId) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }) + const after = await quota(f.ownerId), owner = after.find(row => row.scope === "owner:" + f.ownerId) + assert(owner); assert.equal(owner.admissions, "1"); assert.equal(owner.reserved_bytes, "294912"); assert.equal(owner.reserved_files, "1") + assert.equal(BigInt(after.find(row => row.scope === "global")!.reserved_bytes) - BigInt(before.find(row => row.scope === "global")!.reserved_bytes), 294912n) + await requestStorageDiagnosticCancellation(f.principal, f.operationId); assert.deepEqual(await quota(f.ownerId), after) + pass("pending source cancellation creates exactly one charged admission and one audit; duplicate changes nothing") + } + { + const f = await fixture(); await pool.query("UPDATE mcp_operations SET state='applied' WHERE id=$1;", [f.operationId]) + await pool.query("UPDATE mcp_plans SET state='completed' WHERE id=$1", [f.planId]); const before = await quota(f.ownerId) + assert.deepEqual(await requestStorageDiagnosticCancellation(f.principal, f.operationId), { operationId: f.operationId, cancelRequested: false, terminal: true, automaticReplay: false }) + await absent(f, before); pass("terminal source returns a no-op without cancellation, audit or quota mutation") + } + for (const reason of ["wrong-owner", "scope", "credential", "source", "settings"] as const) { + const f = await fixture(), before = await quota(f.ownerId) + if (reason === "wrong-owner") await pool.query("UPDATE mcp_connections SET owner_id='different-owned-user' WHERE id=$1", [f.connectionId]) + if (reason === "scope") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]'::jsonb) WHERE id=$1", [f.connectionId]) + if (reason === "credential") await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [f.credentialId]) + if (reason === "source") await pool.query("UPDATE mcp_plans SET operation_version=2 WHERE id=$1", [f.planId]) + if (reason === "settings") await pool.query("UPDATE mcp_settings SET enabled=false WHERE id=1") + try { await assert.rejects(requestStorageDiagnosticCancellation(f.principal, f.operationId)); await absent(f, before) } + finally { if (reason === "settings") await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") } + pass(reason + " refusal writes no cancellation/audit/admission and changes no quota") + } + for (const reason of ["retention", "credential", "source"] as const) { + const f = await fixture(), before = await quota(f.ownerId), mutator = await pool.connect() + let changed = false + try { + await assert.rejects(proxyHost(() => requestStorageDiagnosticCancellation(f.principal, f.operationId), { before: async text => { + if (changed || !text.includes("probe-storage:lock")) return + changed = true + if (reason === "retention") await mutator.query("UPDATE mcp_settings SET requests_retention_days=31 WHERE id=1") + if (reason === "credential") await mutator.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [f.credentialId]) + if (reason === "source") await mutator.query("UPDATE mcp_plans SET operation_version=2 WHERE id=$1", [f.planId]) + } })) + assert(changed); await absent(f, before) + } finally { mutator.release(true); if (reason === "retention") await pool.query("UPDATE mcp_settings SET requests_retention_days=30 WHERE id=1") } + pass(reason + " drift committed on another backend before global lock is reauthorized and leaves atomic zero writes") + } + { + const f = await fixture(), before = await quota(f.ownerId) + let failed = false + await assert.rejects(proxyHost(() => requestStorageDiagnosticCancellation(f.principal, f.operationId), { after: async text => { + if (!failed && text.startsWith("INSERT INTO mcp_audit")) { failed = true; throw Error("Owned audit acknowledgement failure before COMMIT") } + } })) + assert(failed); await absent(f, before) + pass("failure after actual cancellation/audit INSERT before COMMIT rolls back cancellation, audit, admission and all quota") + } + { + const f = await fixture(); let lost = false + await assert.rejects(proxyHost(() => createStorageDiagnosticCancellationRequest()(f.principal, f.operationId), { after: async text => { + if (!lost && text === "COMMIT") { lost = true; throw Error("Owned lost actual COMMIT acknowledgement") } + } }), ProbeStorageUnknown) + assert(lost); assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }); const after = await quota(f.ownerId) + await requestStorageDiagnosticCancellation(f.principal, f.operationId) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }); assert.deepEqual(await quota(f.ownerId), after) + pass("lost actual cancellation COMMIT ACK reports unknown; independent repeat observes one spent admission and no second audit/debit") + } + { + const f = await fixture(true); await activeClaim(f); const before = await quota(f.ownerId) + const observed = await proxyHost(() => Promise.all([ + requestStorageDiagnosticCancellation(f.principal, f.operationId), + requestStorageDiagnosticCancellation(f.principal, f.operationId), + ])) + assert(observed.result.every(result => result.cancelRequested === true)) + assert.equal(observed.checkouts, 2); assert.equal(observed.nestedCallbacks, 0) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }); assert.deepEqual(await quota(f.ownerId), before) + pass("two concurrent actual host requests serialize without deadlock and commit one cancellation/audit against one shared admission") + } + { + const f = await fixture(), observer = await pool.connect() + let reached!: () => void, release!: () => void + let actualCommitObserved = false + const committed = new Promise(resolve => { reached = resolve }) + const resume = new Promise(resolve => { release = resolve }) + const pending = proxyHost(() => requestStorageDiagnosticCancellation(f.principal, f.operationId), { after: async text => { + if (text === "COMMIT") { actualCommitObserved = true; reached(); await resume } + } }) + const timer = setTimeout(() => { reached(); release() }, 30000) + try { + await committed + assert(actualCommitObserved, "Host must reach the actual COMMIT barrier before timeout") + const durable = (await observer.query("SELECT count(*)::int AS n FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [f.operationId])).rows[0].n + assert.equal(durable, 1, "Independent backend sees actual COMMIT before the host receives its acknowledgement") + await observer.query("UPDATE mcp_operations SET state='applied' WHERE id=$1", [f.operationId]) + await observer.query("UPDATE mcp_plans SET state='completed' WHERE id=$1", [f.planId]) + release() + const observed = await pending + assert.equal(observed.result.cancelRequested, true); assert.equal(observed.checkouts, 1); assert.equal(observed.nestedCallbacks, 0) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }) + pass("terminal transition after actual cancellation COMMIT but before host acknowledgement preserves the committed stop record") + } finally { clearTimeout(timer); release(); await pending.catch(() => {}); observer.release(true) } + } + { + const f = await fixture() + let reached!: () => void, release!: () => void, reservationCommitted = false, anchorCallbacks = 0 + const reachedReservation = new Promise(resolve => { reached = resolve }) + const resume = new Promise(resolve => { release = resolve }) + const storage = createProbeJournalStorage({ policy: { ...PROBE_STORAGE_LIMITS, retentionDays: 30 }, safetyWindowMs: 60000, + authorize: async () => "c".repeat(64), connect: async () => { + const client = await pool.connect() + return { release: destroy => client.release(destroy), query: async (text, values) => { + const result = await client.query(text, values) + if (text === "COMMIT" && !reservationCommitted) { reservationCommitted = true; reached(); await resume } + return result + } } + } }) + const claim = storage.claim(f.scope, { deadlineAt: Date.now() + 120000 }, async () => { anchorCallbacks++; assert.fail("Cancellation won before the anchor could execute") }) + .then(() => false, () => true) + const timer = setTimeout(() => { reached(); release() }, 30000) + try { + await reachedReservation; assert(reservationCommitted, "Claimer must actually commit its reservation before the timeout") + assert.equal((await requestStorageDiagnosticCancellation(f.principal, f.operationId)).cancelRequested, true) + const charged = await quota(f.ownerId) + release(); assert.equal(await claim, true); assert.equal(anchorCallbacks, 0) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }); assert.deepEqual(await quota(f.ownerId), charged) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE receipt_id=$1", [f.receiptId])).rows[0].n, 0) + pass("actual cancellation between reservation COMMIT and claim consumption spends the existing admission and prevents the anchor callback without extra quota") + } finally { clearTimeout(timer); release(); await claim } + } + { + const f = await fixture(true); await activeClaim(f) + const before = await quota(f.ownerId), protectionId = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protectionId, JSON.stringify({ kind: "server", id: String(serverId), serverId, protection: "deny" }), f.ownerId]) + try { + assert.equal((await requestStorageDiagnosticCancellation(f.principal, f.operationId)).cancelRequested, true) + assert.deepEqual(await counts(f), { cancellations: 1, audits: 1, admissions: 1 }); assert.deepEqual(await quota(f.ownerId), before) + } + finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) } + pass("server deny protection preserves the owner's stop capability for an already active claim; no native write and no additional quota charge") + } + console.log(JSON.stringify({ suite: "approved owned diagnostic cancellation host", actualSql: true, productionHostExecuted: true, cases, nativeCalls: 0, apiCalls: 0 })) +} finally { await closePool() } diff --git a/backend/src/modules/mcp/diagnostic-cancellation-storage.ts b/backend/src/modules/mcp/diagnostic-cancellation-storage.ts new file mode 100644 index 00000000..53e2b25c --- /dev/null +++ b/backend/src/modules/mcp/diagnostic-cancellation-storage.ts @@ -0,0 +1,111 @@ +/** Approved opt-in source host. Public API registration remains separate. + * Opt-in v2 cancellation host; one backend owns authority, source and quota. */ +import { z } from "zod" +import type { PoolClient, QueryConfig } from "pg" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { createProbeJournalStorage, validateProbeStoragePolicy, + type ProbeStorageConnection, type ProbeStoragePolicy } from "../../services/server-probe-journal-maintenance.js" +import { effectiveMcpConnection, type McpPrincipal } from "./store.js" +import { fingerprint, McpError } from "./security.js" +import { SERVER_PROBE_OPERATION } from "../../services/server-probe-authority.js" +import { loadDiagnosticCancellationSource, type DiagnosticCancellationSource } from "./diagnostic-cancellation-source.js" + +export const PROBE_STORAGE_LIMITS = Object.freeze({ globalBytes: 256 * 1048576, ownerBytes: 24 * 1048576, + globalFiles: 1024, ownerFiles: 128, globalAdmissions: 8192, ownerAdmissions: 1024 }) +export type ProbeStorageLimits = Omit +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict() +const deny = (): never => { throw new McpError("STATE_CONFLICT", "Допуск или исходная диагностика изменились", 409) } +function policy(raw: ProbeStorageLimits, retention: unknown) { + return validateProbeStoragePolicy({ retention_days: retention, global_bytes: raw.globalBytes, owner_bytes: raw.ownerBytes, + global_files: raw.globalFiles, owner_files: raw.ownerFiles, global_admissions: raw.globalAdmissions, owner_admissions: raw.ownerAdmissions }) +} + +/** Limits are trusted deployment configuration, never tool/request JSON. + * Crypto, pool, source loader and settings reader are fixed production code. */ +export function createStorageDiagnosticCancellationRequest(limits: ProbeStorageLimits = PROBE_STORAGE_LIMITS) { + const configured = Object.freeze({ ...limits }); policy(configured, 30) + return async (rawPrincipal: McpPrincipal, operationId: string, signal?: AbortSignal) => { + const principal = principalSchema.parse(structuredClone(rawPrincipal)); z.string().uuid().parse(operationId) + const deadlineAt = Math.min(Date.now() + 120000, Date.parse(principal.credentialExpiresAt)) + const live = () => { signal?.throwIfAborted(); if (Date.now() >= deadlineAt) return deny() } + live() + const client = await pool.connect() + let released = false, unsafe = false, inTransaction = false + const release = (destroy = false) => { if (!released) { released = true; client.release(unsafe || destroy) } } + const query = async (text: string, values?: unknown[]) => { + live() + const request: QueryConfig & { query_timeout: number } = { text, values, query_timeout: Math.max(1, Math.min(10000, deadlineAt - Date.now())) } + try { + const result = await client.query(request) + if (text === "BEGIN") inTransaction = true + if (text === "COMMIT") { if (result.command !== "COMMIT") throw Error("Cancellation COMMIT not acknowledged"); inTransaction = false } + if (text === "ROLLBACK") inTransaction = false + live(); return result + } catch (error) { unsafe = true; throw error } + } + // Store functions issue fixed string SQL. The raw bounded backend avoids + // recursive authorization and waiting for a second pool slot under locks. + const authoritySql: Pick = { query: query as PoolClient["query"] } + const connection: ProbeStorageConnection = { query, release } + try { + const readPolicy = async () => { + const rows = (await query("SELECT enabled,requests_retention_days FROM mcp_settings WHERE id=1")).rows + if (rows.length !== 1 || rows[0].enabled !== true) return deny() + return policy(configured, rows[0].requests_retention_days) + } + const current = async () => { + live() + const access = await effectiveMcpConnection(principal, authoritySql) + const credentials = (await query("SELECT expires_at FROM mcp_credentials WHERE id=$1 AND connection_id=$2 AND revoked_at IS NULL AND expires_at>clock_timestamp()", [principal.credentialId, principal.connectionId])).rows + if (credentials.length !== 1 || access.policy.mode === "observer" + || !hasPermission(access.owner_permissions, "mm:servers:write") || !hasPermission(access.policy.permissions, "mm:servers:write") + || !access.policy.scope.operationIds.includes(SERVER_PROBE_OPERATION)) return deny() + live() + const storagePolicy = await readPolicy() + return { access, storagePolicy, proof: fingerprint({ principal, ownerId: access.owner_id, policy: access.policy, + permissions: access.owner_permissions, generation: access.generation, ownerClaims: access.owner_claims_issued_at, + ownerValidUntil: access.owner_valid_until, credentialExpiresAt: credentials[0].expires_at, storagePolicy }) } + } + const initial = await current() + let original: Readonly | undefined + const source = async (sql: Parameters[0]) => { + const value = await loadDiagnosticCancellationSource(sql, operationId, { connectionId: initial.access.id, ownerId: initial.access.owner_id }) + if (value.serverIds.some(id => !initial.access.policy.scope.serverIds.includes(id))) return deny() + return value + } + const immutable = (value: Readonly) => fingerprint({ scope: value.scope, sourceDigest: value.sourceDigest, + serverIds: value.serverIds, stageBudget: value.stageBudget, credentialId: value.originalCredentialId }) + const storage = createProbeJournalStorage({ policy: initial.storagePolicy, safetyWindowMs: 60000, + connect: async () => connection, + authorize: async (ownerId, operation, bound) => { + if (ownerId !== initial.access.owner_id || operation !== "cancel") return deny() + const fresh = await current(); if (fresh.proof !== initial.proof) return deny() + if (bound && (bound.ownerId !== ownerId || original && fingerprint(bound) !== fingerprint(original.scope))) return deny() + // No source locks before the global storage lock. After a known + // COMMIT, a terminal transition cannot erase its durable stop record. + if (original && inTransaction && immutable(await source({ query })) !== immutable(original)) return deny() + return initial.proof + } }) + const result = await storage.cancel(initial.access.owner_id, { deadlineAt, signal }, async sql => { + const value = await source(sql) + if (value.terminal) return null + original = value; return value.scope + }, async (sql, bound) => { + const value = await source(sql) + if (!original || value.terminal || immutable(value) !== immutable(original) || fingerprint(value.scope) !== fingerprint(bound)) return deny() + const inserted = (await sql.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3) ON CONFLICT DO NOTHING RETURNING operation_id", [operationId, initial.access.id, initial.access.owner_id])).rows + if (inserted.length === 1 && inserted[0].operation_id === operationId) { + await sql.query("INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,'diagnostic.cancel-requested',$2,$3::jsonb)", [initial.access.owner_id, initial.access.id, JSON.stringify({ operationId })]) + } else { + if (inserted.length !== 0) return deny() + const prior = (await sql.query("SELECT operation_id,connection_id,actor_id FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [operationId])).rows + if (prior.length !== 1 || prior[0].operation_id !== operationId || prior[0].connection_id !== initial.access.id || prior[0].actor_id !== initial.access.owner_id) return deny() + } + return { operationId, cancelRequested: true, terminal: false, automaticReplay: false } + }) + return result ?? { operationId, cancelRequested: false, terminal: true, automaticReplay: false } + } finally { release(true) } + } +} +export const requestStorageDiagnosticCancellation = createStorageDiagnosticCancellationRequest() diff --git a/backend/src/modules/mcp/diagnostic-cancellation.test.ts b/backend/src/modules/mcp/diagnostic-cancellation.test.ts new file mode 100644 index 00000000..4f30c8c9 --- /dev/null +++ b/backend/src/modules/mcp/diagnostic-cancellation.test.ts @@ -0,0 +1,70 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { McpError } from "./security.js" +import { createDiagnosticCancellationRequest, withDiagnosticCancellation } from "./diagnostic-cancellation.js" +import type { ConnectionRow } from "./store.js" + +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Actual SQL forbidden") }) as never +pool.connect = (() => { throw Error("Actual SQL forbidden") }) as never +let cases = 0 +// Production has an application listener/in-flight I/O. This inert signal-only +// test supplies a referenced handle while the watch itself remains unref'ed. +const keepAlive = setInterval(() => {}, 1000) +const context = { operationId: randomUUID(), phase: "forward" as const, stepIndex: 0 } +const sleep = (time: number) => new Promise(resolve => setTimeout(resolve, time)) +try { + { + let granted = true, cancelled = false, reads = 0, signal: AbortSignal | undefined + const deps = { intervalMs: 10, authorize: async () => { if (!granted) throw new McpError("PERMISSION_DENIED", "revoked") }, read: async () => { reads++; return { state: "running", operation: "servers.probes.run", cancelled } } } + assert.equal(await withDiagnosticCancellation(context, async value => { signal = value; return "completed" }, deps), "completed") + assert.equal(signal!.aborted, true, "Detached work loses its signal after the service ends"); cases++ + cancelled = true + let started = false + await assert.rejects(withDiagnosticCancellation(context, async () => { started = true }, deps), error => error instanceof McpError && error.code === "READ_CANCELLED") + assert.equal(started, false); cases++ + cancelled = false + const pending = withDiagnosticCancellation(context, async value => { + cancelled = true + await new Promise((_resolve, reject) => value.addEventListener("abort", () => reject(value.reason), { once: true })) + }, deps) + await assert.rejects(pending, error => error instanceof McpError && error.code === "READ_CANCELLED"); cases++ + cancelled = false + await assert.rejects(withDiagnosticCancellation(context, async value => { + granted = false + await new Promise((_resolve, reject) => value.addEventListener("abort", () => reject(value.reason), { once: true })) + }, deps), error => error instanceof McpError && error.code === "PERMISSION_DENIED"); cases++ + const count = reads; await sleep(25); assert.equal(reads, count, "No watch continues after the operation ends") + } + { + const deps = { intervalMs: 10, authorize: async () => {}, read: async () => ({ state: "running", operation: "other.operation", cancelled: false }) } + await assert.rejects(withDiagnosticCancellation(context, async () => "unreachable", deps), McpError); cases++ + await assert.rejects(withDiagnosticCancellation({ operationId: context.operationId, phase: "recovery" }, async () => "unreachable", deps), McpError); cases++ + } + const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 300000).toISOString() } + const initial = { id: principal.connectionId, owner_id: "owner", generation: "g", owner_permissions: ["mm:servers:write"], policy: { mode: "approval", permissions: ["mm:servers:write"], scope: { operationIds: ["servers.probes.run"], serverIds: [1, 2] } } } as ConnectionRow + let current = structuredClone(initial), state = "running", actualOperation = "servers.probes.run", ids = [1, 2], evidence = true, inserted = 0, audits = 0, afterLock: (() => void) | undefined + let existing: { operation_id: string; connection_id: string; actor_id: string } | undefined + const client = { async query(sql: string, values: unknown[]) { + if (sql.startsWith("SELECT o.state")) { assert.equal(values[1], principal.connectionId); afterLock?.(); return { rows: [{ state, operation_id: actualOperation, evidence: evidence ? { sealed: "owned-evidence" } : null }] } } + if (sql.startsWith("INSERT INTO mcp_diagnostic")) { inserted++; assert.equal(values[2], "owner"); if (existing) return { rows: [] }; existing = { operation_id: String(values[0]), connection_id: String(values[1]), actor_id: String(values[2]) }; return { rows: [{ operation_id: values[0] }] } } + if (sql.startsWith("SELECT operation_id,connection_id,actor_id")) return { rows: existing ? [structuredClone(existing)] : [] } + throw Error("Unexpected query") + } } + const request = createDiagnosticCancellationRequest({ connection: async () => structuredClone(current), transaction: (async (work: (value: unknown) => Promise) => work(client)) as never, open: async () => JSON.stringify({ target: { scope: "servers", serverIds: ids } }), audit: async () => { audits++ } }) + assert.deepEqual(await request(principal, context.operationId), { operationId: context.operationId, cancelRequested: true, terminal: false, automaticReplay: false }); assert.equal(inserted, 1); assert.equal(audits, 1); cases++ + assert.equal((await request(principal, context.operationId)).cancelRequested, true); assert.equal(audits, 1); cases++ + inserted = 1 + existing!.actor_id = "foreign" + await assert.rejects(request(principal, context.operationId), error => error instanceof McpError && error.code === "STATE_CONFLICT"); assert.equal(audits, 1); cases++ + existing!.actor_id = "owner"; inserted = 1 + state = "applied"; await request(principal, context.operationId); assert.equal(inserted, 1); cases++ + state = "running"; ids = [1, 3]; await assert.rejects(request(principal, context.operationId), McpError); assert.equal(inserted, 1); cases++ + ids = [1, 2]; actualOperation = "users.delete"; await assert.rejects(request(principal, context.operationId), McpError); cases++ + actualOperation = "servers.probes.run"; evidence = false; await assert.rejects(request(principal, context.operationId), McpError); cases++ + evidence = true; afterLock = () => { current.owner_permissions = [] }; await assert.rejects(request(principal, context.operationId), McpError); assert.equal(inserted, 1); cases++ + afterLock = undefined; current = structuredClone(initial); current.policy.scope.operationIds = []; await assert.rejects(request(principal, context.operationId), McpError); cases++ + current = structuredClone(initial); current.policy.mode = "observer"; await assert.rejects(request(principal, context.operationId), error => error instanceof McpError && error.code === "PERMISSION_DENIED"); assert.equal(inserted, 1); cases++ + console.log(`Diagnostic cancellation: ${cases} inert cases passed; owned full-scope durable requests, active AbortSignal, late revoke/cancel, observer refusal, terminal/no inverse and watch teardown; no actual SQL`) +} finally { clearInterval(keepAlive); pool.query = oldQuery; pool.connect = oldConnect } diff --git a/backend/src/modules/mcp/diagnostic-cancellation.ts b/backend/src/modules/mcp/diagnostic-cancellation.ts new file mode 100644 index 00000000..180f5160 --- /dev/null +++ b/backend/src/modules/mcp/diagnostic-cancellation.ts @@ -0,0 +1,99 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { SERVER_PROBE_OPERATION } from "../../services/server-probe-authority.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection, transaction, audit, type McpPrincipal } from "./store.js" +import { McpError, fingerprint, decryptSecret } from "./security.js" +import type { McpServiceExecutionContext } from "./service-registry.js" + +const uuid = z.string().uuid() +const stop = (code: string, message: string, status = 409): never => { throw new McpError(code, message, status) } +interface RunningDiagnostic { state: string; operation: string; cancelled: boolean } +const productionWatch = { + authorize: assertVerifiedConfigurationAccess, + intervalMs: 1000, + async read(operationId: string): Promise { + const client = await pool.connect(); let completed = false + try { + await client.query("BEGIN READ ONLY") + await client.query("SET LOCAL statement_timeout='2000ms'") + const rows = (await client.query("SELECT o.state,p.operation_id AS operation,EXISTS(SELECT 1 FROM mcp_diagnostic_cancellations c WHERE c.operation_id=o.id) AS cancelled FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id WHERE o.id=$1 LIMIT 2", [operationId])).rows + await client.query("COMMIT"); completed = true + return rows.length === 1 ? rows[0] as RunningDiagnostic : undefined + } finally { client.release(!completed) } + }, +} +export type DiagnosticCancellationWatch = typeof productionWatch + +/** Only explicitly cancellable diagnostics use this lane. The signal follows + * durable cancellation across replicas; pending work must honor AbortSignal. + * A cancellation never executes an inverse or repeats a measurement. */ +export async function withDiagnosticCancellation(context: McpServiceExecutionContext, work: (signal: AbortSignal) => Promise, overrides: Partial = {}): Promise { + if (context.phase !== "forward" || !uuid.safeParse(context.operationId).success || !Number.isSafeInteger(context.stepIndex) || context.stepIndex < 0) return stop("INVALID_INPUT", "Диагностика требует исходный контекст операции", 400) + const deps = { ...productionWatch, ...overrides } + if (!Number.isSafeInteger(deps.intervalMs) || deps.intervalMs < 10 || deps.intervalMs > 5000) return stop("INVALID_INPUT", "Некорректный интервал отмены", 400) + const controller = new AbortController() + let active = true, polling: Promise | undefined, timer: ReturnType | undefined + const check = async () => { + controller.signal.throwIfAborted() + await deps.authorize() + const state = await deps.read(context.operationId) + await deps.authorize() + if (!state || state.operation !== SERVER_PROBE_OPERATION || !["pending", "running"].includes(state.state)) return stop("STATE_CONFLICT", "Диагностика больше не выполняется") + if (state.cancelled) return stop("READ_CANCELLED", "Запрошена отмена диагностики") + controller.signal.throwIfAborted() + } + try { + await check() + timer = setInterval(() => { + if (!active || polling) return + polling = check().catch(error => { + if (active) controller.abort(error instanceof McpError ? error : new McpError("STATE_UNAVAILABLE", "Состояние отмены диагностики недоступно", 503)) + }).finally(() => { polling = undefined }) + }, deps.intervalMs) + timer.unref() + const result = await work(controller.signal) + await check() + return result + } finally { + active = false + if (timer) clearInterval(timer) + if (polling) await polling + controller.abort(new McpError("READ_CANCELLED", "Контекст диагностики завершён", 409)) + } +} + +const productionRequest = { connection: effectiveMcpConnection, transaction, open: decryptSecret, audit } +export function createDiagnosticCancellationRequest(overrides: Partial = {}) { + const deps = { ...productionRequest, ...overrides } + return async (principal: McpPrincipal, operationId: string) => { + uuid.parse(operationId) + const initial = await deps.connection(principal) + if (initial.policy.mode === "observer" || !hasPermission(initial.owner_permissions, "mm:servers:write") || !hasPermission(initial.policy.permissions, "mm:servers:write") || !initial.policy.scope.operationIds.includes(SERVER_PROBE_OPERATION)) return stop("PERMISSION_DENIED", "Нет права отмены серверной диагностики", 403) + return deps.transaction(async client => { + const current = await deps.connection(principal, client) + if (fingerprint(current.policy) !== fingerprint(initial.policy) || fingerprint(current.owner_permissions) !== fingerprint(initial.owner_permissions) || current.owner_id !== initial.owner_id || current.generation !== initial.generation) return stop("STATE_CONFLICT", "Допуск отмены изменился") + const rows = (await client.query("SELECT o.state,p.operation_id,CASE WHEN octet_length(p.evidence::text)<=262144 THEN p.evidence ELSE NULL END AS evidence FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id WHERE o.id=$1 AND o.connection_id=$2 FOR UPDATE OF o", [operationId, principal.connectionId])).rows + if (rows.length !== 1 || rows[0].operation_id !== SERVER_PROBE_OPERATION) return stop("NOT_FOUND", "Диагностика не найдена", 404) + const raw = rows[0].evidence?.sealed + if (typeof raw !== "string" || raw.length > 262144) return stop("STATE_UNAVAILABLE", "Область диагностики не подтверждена", 503) + let target: { scope: "servers"; serverIds: number[] } + try { target = z.object({ scope: z.literal("servers"), serverIds: z.array(z.number().int().positive().safe()).min(1).max(2) }).strict().parse(JSON.parse(await deps.open(raw)).target) } catch { return stop("STATE_UNAVAILABLE", "Область диагностики не подтверждена", 503) } + if (new Set(target.serverIds).size !== target.serverIds.length || target.serverIds.some(id => !current.policy.scope.serverIds.includes(id))) return stop("SERVER_DENIED", "Диагностика находится вне текущей области", 403) + if (!["pending", "running"].includes(rows[0].state)) return { operationId, cancelRequested: false, terminal: true, automaticReplay: false } + const fresh = await deps.connection(principal, client) + if (fingerprint(fresh.policy) !== fingerprint(current.policy) || fingerprint(fresh.owner_permissions) !== fingerprint(current.owner_permissions) || fresh.owner_id !== current.owner_id || fresh.generation !== current.generation) return stop("STATE_CONFLICT", "Допуск отмены изменился перед записью") + const inserted = await client.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3) ON CONFLICT DO NOTHING RETURNING operation_id", [operationId, current.id, current.owner_id]) + if (inserted.rows.length === 1 && inserted.rows[0].operation_id === operationId) { + await deps.audit(client, current.owner_id, "diagnostic.cancel-requested", current.id, { operationId }) + } else { + if (inserted.rows.length !== 0) return stop("STATE_CONFLICT", "Запись отмены не подтверждена") + const prior = (await client.query("SELECT operation_id,connection_id,actor_id FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [operationId])).rows + if (prior.length !== 1 || prior[0].operation_id !== operationId || prior[0].connection_id !== current.id || prior[0].actor_id !== current.owner_id) return stop("STATE_CONFLICT", "Исходная отмена принадлежит другому допуску") + } + return { operationId, cancelRequested: true, terminal: false, automaticReplay: false } + }) + } +} +export const requestDiagnosticCancellation = createDiagnosticCancellationRequest() diff --git a/backend/src/modules/mcp/ecmp-preflight.test.ts b/backend/src/modules/mcp/ecmp-preflight.test.ts new file mode 100644 index 00000000..0cef0604 --- /dev/null +++ b/backend/src/modules/mcp/ecmp-preflight.test.ts @@ -0,0 +1,98 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { analyzeEcmpPreflight, duplicateEcmpAdjacency, ecmpAdjacency, preflightObservation, preflightPaths, type PreflightObservation, type PreflightSource } from "./ecmp-preflight.js" + +function fixture(): Record { + const result = Object.fromEntries(Object.keys(preflightPaths("ipv4")).map(key => [key, { rows: [], error: null }])) as Record + result.routes.rows = [1, 2].map(index => ({ ".id": `*${index}`, "dst-address": "192.0.2.0/24", "routing-table": "main", gateway: `192.0.2.${index}`, "immediate-gw": `192.0.2.${index}%gre${index}`, disabled: "false", active: index === 1 ? "true" : "false", distance: String(index * 10) })) + result.settings.rows = [{ "rp-filter": "no", "ipv4-multipath-hash-policy": "l3", "multipath-hash-policy": "l4" }] + result.tables.rows = [{ ".id": "*main", name: "main", fib: "true" }] + result.connectionTracking.rows = [{ enabled: "auto" }] + return result +} +test("preflight observes unequal/inactive candidates without claiming packet/path acceptance", () => { + const result = analyzeEcmpPreflight("ipv4", ["*1", "*2"], fixture()) + assert.equal(result.configurationObserved, true); assert.equal(result.routeSummary[1].active, false) + assert.equal(result.decision, "review-required"); assert.equal(result.independentPaths, "not-proven"); assert.equal(result.networkValidation, "not-performed") + assert.equal(result.hashPolicy, "l3"); assert.deepEqual(result.findings, []) +}) +test("preflight flags missing, ambiguous or inactive FIB without accepting unrelated tables", () => { + for (const tables of [ + [], + [{ ".id": "*main", name: "main" }], + [{ ".id": "*main", name: "main", fib: "" }], + [{ ".id": "*main", name: "main", fib: "false" }], + [{ ".id": "*main", name: "main", fib: "true", disabled: "true" }], + [{ ".id": "*main", name: "main", fib: "true", invalid: "true" }], + [{ ".id": "*main", name: "main", fib: "true" }, { ".id": "*alias", name: "main", fib: "true" }], + ]) { + const sources = fixture() + sources.tables.rows = [...tables, { ".id": "*other", name: "other", fib: "true" }] + const result = analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources) + assert.equal(result.configurationObserved, false) + assert.deepEqual(result.findings.find(item => item.code === "FIB_TABLE_UNCONFIRMED")?.ids, ["*1", "*2"]) + } +}) +test("NAT, FastTrack, marks, notrack, strict RPF and shared egress require explicit review", () => { + const sources = fixture() + sources.filter.rows = [{ ".id": "*fast", action: "fasttrack-connection", disabled: "false" }, { ".id": "*disabled", action: "fasttrack-connection", disabled: "true" }] + sources.nat.rows = [{ ".id": "*nat", action: "masquerade", disabled: "false" }] + sources.mangle.rows = [{ ".id": "*mark", action: "mark-routing", disabled: "false" }] + sources.raw.rows = [{ ".id": "*notrack", action: "notrack", disabled: "false" }] + sources.settings.rows[0]["rp-filter"] = "strict" + sources.connectionTracking.rows[0].enabled = "no" + sources.routes.rows[1]["immediate-gw"] = "192.0.2.2%gre1" + const result = analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources) + for (const code of ["NAT_AFFINITY_REVIEW", "FASTTRACK_REVIEW", "POLICY_ROUTING_REVIEW", "NOTRACK_REVIEW", "STRICT_RPF_REVIEW", "SHARED_EGRESS_INTERFACE", "CONNECTION_TRACKING_DISABLED"]) assert.ok(result.findings.some(item => item.code === code), code) + assert.deepEqual(result.findings.find(item => item.code === "FASTTRACK_REVIEW")?.ids, ["*fast"]) +}) +test("failed/malformed/omitted evidence is unknown, never an empty healthy catalog", () => { + for (const raw of [null, {}, [null], [{ ".id": "*1" }, { ".id": "*1" }], [{ name: "missing-id" }]]) assert.ok(preflightObservation(raw).error) + assert.ok(preflightObservation([], true).error); assert.ok(preflightObservation([{}, {}], true).error) + const sources = fixture() + sources.nat = { rows: [], error: "unavailable" }; delete sources.settings.rows[0]["ipv4-multipath-hash-policy"] + sources.raw.rows = [{ ".id": "*raw", action: "notrack" }] + const result = analyzeEcmpPreflight("ipv4", ["*1", "*missing"], sources) + assert.equal(result.configurationObserved, false) + for (const code of ["OBSERVATION_UNAVAILABLE", "ROUTE_NOT_OBSERVED", "HASH_POLICY_UNKNOWN", "RULE_ENABLEMENT_UNKNOWN", "NOTRACK_REVIEW"]) assert.ok(result.findings.some(item => item.code === code), code) + assert.equal(result.sources.nat.observed, false) + const absentGateways = fixture() + absentGateways.routes.rows.forEach(row => { delete row.gateway }) + assert.ok(!analyzeEcmpPreflight("ipv4", ["*1", "*2"], absentGateways).findings.some(item => item.code === "DUPLICATE_GATEWAY"), "Two unknown gateways do not prove duplication") + const malformedSettings = fixture() + malformedSettings.settings.rows = [{ "ipv4-multipath-hash-policy": { toString: null }, "rp-filter": 0 }] + assert.equal(analyzeEcmpPreflight("ipv4", ["*1", "*2"], malformedSettings).configurationObserved, false) +}) +test("IPv6 uses IPv6 paths/hash policy and explicitly has no IPv4 RPF setting", () => { + assert.equal(preflightPaths("ipv6").nat, "/ipv6/firewall/nat") + assert.equal(preflightPaths("ipv6").connectionTracking, "/ip/firewall/connection/tracking") + const result = analyzeEcmpPreflight("ipv6", ["*1", "*2"], fixture()) + assert.deepEqual(result.rpf, { supported: false, mode: null }); assert.equal(result.hashPolicy, "l4") + assert.ok(!result.findings.some(item => item.code.startsWith("RPF"))) +}) +test("ECMP identifies adjacency by canonical address and egress link", () => { + const duplicate = (first: string, second: string) => duplicateEcmpAdjacency([ecmpAdjacency(first), ecmpAdjacency(second)]) + assert.ok(duplicate("fe80::1%gre1@main", "fe80:0:0:0:0:0:0:1%gre1@tenant")) + assert.ok(!duplicate("fe80::1%gre1", "fe80::1%gre2")) + assert.ok(!duplicate("192.0.2.1%gre1", "192.0.2.2%gre1")) + assert.ok(duplicate("192.0.2.1", "192.0.2.1%gre1")) + assert.equal(ecmpAdjacency("fe80::1"), null) + assert.equal(ecmpAdjacency("192.0.2.1%gre1,192.0.2.2%gre2"), null) + assert.equal(ecmpAdjacency("gre1"), null) + assert.deepEqual(ecmpAdjacency("gre1", new Set(["gre1"])), { address: null, interface: "gre1" }) + const sources = fixture() + sources.routes.rows[1]["immediate-gw"] = sources.routes.rows[0]["immediate-gw"] + assert.ok(analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources).findings.some(item => item.code === "DUPLICATE_IMMEDIATE_GATEWAY")) +}) +test("interface-only ECMP next-hop requires a unique observed name", () => { + const sources = fixture() + sources.routes.rows[0]["immediate-gw"] = "gre2" + const analyze = (rows: Record[]) => analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources, preflightObservation(rows)) + const known = analyze([{ ".id": "*gre", name: "gre2" }]) + assert.equal(known.configurationObserved, true) + assert.ok(known.findings.some(item => item.code === "SHARED_EGRESS_INTERFACE")) + const ambiguous = analyze([{ ".id": "*gre", name: "gre2" }, { ".id": "*other", name: "gre2" }]) + assert.equal(ambiguous.configurationObserved, false) + assert.ok(ambiguous.findings.some(item => item.code === "EGRESS_INTERFACE_UNKNOWN")) + assert.equal(analyze([{ name: "gre2" }]).sources.interfaces.observed, false) +}) diff --git a/backend/src/modules/mcp/ecmp-preflight.ts b/backend/src/modules/mcp/ecmp-preflight.ts new file mode 100644 index 00000000..300af941 --- /dev/null +++ b/backend/src/modules/mcp/ecmp-preflight.ts @@ -0,0 +1,122 @@ +import { ipv6ToInt } from "../../services/traffic-flow-ip.js" +import { isConfirmedRoutingFibTable } from "../../services/routing-table-fib.js" +import { routeGatewayContext } from "./routing-community-context-model.js" + +type Row = Record +export function confirmedEcmpTable(rows: readonly Row[], name: string) { + const matches = rows.filter(row => row.name === name) + if (matches.length !== 1) return undefined + const table = matches[0], id = table[".id"] + // The /routing/table catalogue can return an empty FIB value for a non-FIB table. + if (typeof id !== "string" || !id.trim() || table.fib === "" || !isConfirmedRoutingFibTable(table)) return undefined + return { id } +} +export type PreflightObservation = { rows: Row[]; error: string | null } +export const preflightPaths = (family: "ipv4" | "ipv6") => { + const base = family === "ipv4" ? "ip" : "ipv6" + return { routes: `/${base}/route`, settings: `/${base}/settings`, filter: `/${base}/firewall/filter`, nat: `/${base}/firewall/nat`, mangle: `/${base}/firewall/mangle`, raw: `/${base}/firewall/raw`, rules: "/routing/rule", tables: "/routing/table", vrfs: "/ip/vrf", connectionTracking: "/ip/firewall/connection/tracking" } +} +export type PreflightSource = keyof ReturnType +export function preflightObservation(raw: unknown, singleton = false): PreflightObservation { + const rows = singleton && !Array.isArray(raw) && raw && typeof raw === "object" ? [raw] : raw + if (!Array.isArray(rows) || singleton && rows.length !== 1 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row))) return { rows: [], error: "Некорректный ответ устройства" } + if (!singleton) { + const ids = rows.map(row => row[".id"]) + if (ids.some(id => typeof id !== "string" || !id.trim()) || new Set(ids).size !== ids.length) return { rows: [], error: "Неоднозначные ID наблюдаемого каталога" } + } + return { rows, error: null } +} +const text = (value: unknown) => typeof value === "string" && value.trim() ? value.trim() : null +const flag = (value: unknown) => value === true || value === "true" || value === "yes" ? true : value === false || value === "false" || value === "no" ? false : null + +export function ecmpAdjacency(value: unknown, interfaces?: ReadonlySet) { + if (typeof value !== "string" || !value || /[\r\n,]/.test(value)) return null + try { + const hop = routeGatewayContext(value) + const address = hop.family === 6 ? ipv6ToInt(hop.address) : hop.address + // fe80::/10 needs a confirmed link to identify the adjacency. + if (address === null || (typeof address === "bigint" && address >> 118n === 0x3fan && hop.interface === null)) return null + return { address, interface: hop.interface } + } catch { + return interfaces?.has(value) ? { address: null, interface: value } : null + } +} + +export function confirmedEcmpInterfaces(rows: readonly Row[]) { + const names = new Map() + const ambiguous = new Set() + for (const row of rows) { + if (typeof row.name !== "string" || !row.name.trim() || typeof row[".id"] !== "string" || !row[".id"].trim()) continue + if (names.has(row.name)) ambiguous.add(row.name) + names.set(row.name, row[".id"]) + } + for (const name of ambiguous) names.delete(name) + return names +} + +export function confirmedEcmpInterfaceNames(rows: readonly Row[]) { + return new Set(confirmedEcmpInterfaces(rows).keys()) +} + +export function duplicateEcmpAdjacency(hops: ReturnType[]) { + return hops.some((hop, index) => hop && hops.slice(0, index).some(previous => + previous && previous.address === hop.address + && (previous.interface === hop.interface || previous.interface === null || hop.interface === null))) +} + +/** Configuration evidence only. Rule presence does not prove packet matching, + * physical independence, NAT affinity, return path or traffic distribution. */ +export function analyzeEcmpPreflight(family: "ipv4" | "ipv6", routeIds: string[], observed: Record, inventory?: PreflightObservation) { + const findings: { code: string; level: "review" | "unknown"; resource: string; ids: string[]; objectCount: number; idsTruncated: boolean; message: string }[] = [] + const add = (code: string, level: "review" | "unknown", resource: string, ids: string[], message: string) => findings.push({ code, level, resource, ids: ids.slice(0, 100), objectCount: ids.length, idsTruncated: ids.length > 100, message }) + const sources = inventory ? { ...observed, interfaces: inventory } : observed + for (const [resource, observation] of Object.entries(sources)) if (observation.error) add("OBSERVATION_UNAVAILABLE", "unknown", resource, [], observation.error) + const candidates = routeIds.map(id => observed.routes.rows.find(row => row[".id"] === id)) + const selected = candidates.filter((row): row is Row => !!row) + if (selected.length !== routeIds.length) add("ROUTE_NOT_OBSERVED", "unknown", "routes", routeIds.filter((_id, index) => !candidates[index]), "Не все выбранные маршруты подтверждены") + const routeSummary = selected.map(row => ({ id: String(row[".id"]), prefix: text(row["dst-address"]), table: text(row["routing-table"]), gateway: text(row.gateway), immediateGateway: text(row["immediate-gw"]), active: flag(row.active), disabled: flag(row.disabled), distance: text(row.distance) })) + if (routeSummary.some(row => !row.prefix || !row.table || !row.gateway || !row.immediateGateway || row.disabled === null)) add("ROUTE_CONTEXT_UNKNOWN", "unknown", "routes", routeSummary.map(row => row.id), "Не подтверждены prefix/table/gateway/disabled; отсутствие свойства не считается значением по умолчанию") + if (routeSummary.some(row => row.disabled === true)) add("DISABLED_ROUTE", "review", "routes", routeSummary.filter(row => row.disabled === true).map(row => row.id), "В наборе есть отключённые маршруты") + if (new Set(routeSummary.map(row => row.prefix).filter(Boolean)).size > 1 || new Set(routeSummary.map(row => row.table).filter(Boolean)).size > 1) add("DIFFERENT_ROUTE_CONTEXTS", "review", "routes", routeSummary.map(row => row.id), "Prefix или routing table выбранных маршрутов различаются") + if (observed.tables.error === null) { + const unconfirmed = new Set() + for (const table of new Set(routeSummary.map(row => row.table))) { + if (table !== null && !confirmedEcmpTable(observed.tables.rows, table)) unconfirmed.add(table) + } + const affected = routeSummary.filter(row => row.table !== null && unconfirmed.has(row.table)) + if (affected.length) add("FIB_TABLE_UNCONFIRMED", "unknown", "routes", affected.map(row => row.id), "Для выбранных маршрутов не подтверждена однозначная активная таблица FIB") + } + const knownGateways = routeSummary.map(row => row.gateway).filter(Boolean) + if (new Set(knownGateways).size !== knownGateways.length) add("DUPLICATE_GATEWAY", "review", "routes", routeSummary.map(row => row.id), "Next-hop выбранных маршрутов повторяется") + const names = inventory?.error === null ? confirmedEcmpInterfaceNames(inventory.rows) : undefined + const hops = selected.map(row => ecmpAdjacency(row["immediate-gw"], names)) + if (duplicateEcmpAdjacency(hops)) add("DUPLICATE_IMMEDIATE_GATEWAY", "review", "routes", routeSummary.map(row => row.id), "Выбранные маршруты разрешаются в один и тот же next-hop") + const interfaces = hops.map(hop => hop?.interface ?? null) + if (interfaces.some(iface => iface === null)) add("EGRESS_INTERFACE_UNKNOWN", "unknown", "routes", routeSummary.map(row => row.id), "Не подтверждён интерфейс immediate gateway") + else if (new Set(interfaces).size < interfaces.length) add("SHARED_EGRESS_INTERFACE", "review", "routes", routeSummary.map(row => row.id), "Несколько next-hop используют один интерфейс; независимость путей не подтверждена") + const maybeEnabled = (resource: PreflightSource) => { + const rows = observed[resource].rows.filter(row => flag(row.disabled) !== true) + const unknown = rows.filter(row => flag(row.disabled) === null) + if (unknown.length) add("RULE_ENABLEMENT_UNKNOWN", "unknown", resource, unknown.map(row => String(row[".id"])), "Для части объектов не подтверждён disabled; они остаются в проверяемом наборе") + return rows + } + const fastTrack = maybeEnabled("filter").filter(row => row.action === "fasttrack-connection") + const nat = maybeEnabled("nat"), mangle = maybeEnabled("mangle") + const raw = maybeEnabled("raw").filter(row => row.action === "notrack" || row.action === "jump"), rules = maybeEnabled("rules") + if (fastTrack.length) add("FASTTRACK_REVIEW", "review", "filter", fastTrack.map(row => String(row[".id"])), "Обнаружен FastTrack: проверить исключение трафика вне main и влияние на наблюдаемость") + if (nat.length) add("NAT_AFFINITY_REVIEW", "review", "nat", nat.map(row => String(row[".id"])), "Проверить NAT, привязку соединений к выходу и обратный путь; совпадение пакетов не вычислялось") + if (mangle.length || rules.length) add("POLICY_ROUTING_REVIEW", "review", "routing", [...mangle, ...rules].map(row => String(row[".id"])), "Обнаружены маркировки/переходы или routing rules; проверить фактическую таблицу и асимметрию") + if (raw.length) add("NOTRACK_REVIEW", "review", "raw", raw.map(row => String(row[".id"])), "Проверить notrack и переходы raw относительно NAT/stateful firewall") + const settings = observed.settings.rows[0] ?? {}, hashKey = family === "ipv4" ? "ipv4-multipath-hash-policy" : "multipath-hash-policy" + const hashValue = text(settings[hashKey]), hashPolicy = hashValue && ["l3", "l4", "l3-inner"].includes(hashValue) ? hashValue : null + if (!hashPolicy) add("HASH_POLICY_UNKNOWN", "unknown", "settings", [], "Устройство не подтвердило hash policy") + const rpfValue = text(settings["rp-filter"]), rpf = family === "ipv4" && rpfValue && ["no", "loose", "strict"].includes(rpfValue) ? rpfValue : null + if (family === "ipv4" && !rpf) add("RPF_UNKNOWN", "unknown", "settings", [], "Режим IPv4 RPF не подтверждён") + if (rpf === "strict") add("STRICT_RPF_REVIEW", "review", "settings", [], "Strict RPF требует проверки обратного пути и совместимости с таблицами/асимметрией") + const trackingRaw = observed.connectionTracking.rows[0]?.enabled, trackingValue = typeof trackingRaw === "boolean" ? String(trackingRaw) : text(trackingRaw) + const connectionTracking = trackingValue && ["auto", "yes", "no", "true", "false"].includes(trackingValue) ? trackingValue : null + if (!connectionTracking) add("CONNECTION_TRACKING_UNKNOWN", "unknown", "connectionTracking", [], "Режим connection tracking не подтверждён") + else if (connectionTracking === "no" || connectionTracking === "false") add("CONNECTION_TRACKING_DISABLED", "review", "connectionTracking", [], "Connection tracking отключён; проверить зависимости NAT/stateful firewall") + return { family, decision: "review-required", configurationObserved: !findings.some(item => item.level === "unknown"), networkValidation: "not-performed", independentPaths: "not-proven", routeSummary, hashPolicy, rpf: { supported: family === "ipv4", mode: rpf }, connectionTracking, + sources: Object.fromEntries(Object.entries(sources).map(([key, value]) => [key, { observed: value.error === null, count: value.rows.length }])), findings } +} diff --git a/backend/src/modules/mcp/effective-connection-source.test.ts b/backend/src/modules/mcp/effective-connection-source.test.ts new file mode 100644 index 00000000..0f65b8da --- /dev/null +++ b/backend/src/modules/mcp/effective-connection-source.test.ts @@ -0,0 +1,89 @@ +/** Component test: actual store validation/parser with fake SQL rows and an + * isolated real generation/revocation filesystem. No PostgreSQL or network. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mkdtemp, rm } from "node:fs/promises" +import { join } from "node:path" +import { fileURLToPath } from "node:url" +import { test } from "node:test" +import type { PoolClient } from "pg" +import { mcpConnectionPolicySchema } from "@mmapp/contracts/mcp" +import { pool, closePool } from "../../db/index.js" +import { accessGeneration, fingerprint } from "./security.js" +import { effectiveMcpConnection, effectiveMcpConnectionSource, type ConnectionRow, type McpPrincipal } from "./store.js" + +test("effective connection source reuses precisely the grant rows it validates; fake SQL component only", async t => { + const state = await mkdtemp(join(fileURLToPath(new URL("../../../../tmp/", import.meta.url)), "mm-mcp-grant-source-")) + const previousState = process.env.MCP_STATE_DIR, originalQuery = pool.query, originalConnect = pool.connect + process.env.MCP_STATE_DIR = state + pool.query = (() => { throw Error("Component must not use default SQL") }) as never + pool.connect = (() => { throw Error("Component must not acquire a backend") }) as never + try { + const generation = await accessGeneration(), now = Date.now() + const principal: McpPrincipal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(now + 60000).toISOString() } + const connection: ConnectionRow = { id: principal.connectionId, name: "component", owner_id: "owner", owner_permissions: ["mm:servers:write"], generation, + created_at: new Date(now), updated_at: new Date(now), last_used_at: null, owner_valid_until: new Date(now + 60000), owner_claims_issued_at: new Date(now - 1000), + policy: mcpConnectionPolicySchema.parse({ id: principal.connectionId, version: 1, status: "active", mode: "approval", dangerousPolicy: "approval", expiresAt: principal.credentialExpiresAt, + permissions: ["mm:servers:write"], scope: { serverIds: [1, 2], operationIds: ["servers.poll", "operations.global.reconcile"], allowGlobal: true }, + limits: { callsPerMinute: 100, mutationsPerHour: 100, serversPerPlan: 100, objectsPerPlan: 100 } }) } + const grant = () => ({ payload: { credentialId: principal.credentialId, id: "source-grant", startsAt: new Date(now - 1000).toISOString(), expiresAt: new Date(now + 30000).toISOString(), + scope: { serverIds: [1], operationIds: ["servers.poll"], allowGlobal: false } } }) + let rows: { payload: Record }[] = [], active = true + const commands: string[] = [] + const client = { async query(text: string, values: unknown[]) { + commands.push(text) + if (text.startsWith("/* mcp-principal:coherent-source-v1 */")) { + assert.deepEqual(values.slice(0, 2), [principal.credentialId, principal.connectionId]) + const utc = (value: Date) => value.toISOString().replace(/(\.\d{3})Z$/, "$1000Z") + const core = { ...structuredClone(connection), __ps_exists: true, __ps_oversize: false, __ps_bytes: 4096, __ps_guard: true, __ps_isolation: "read committed", + __ps_credential: active ? principal.credentialId : null, __ps_credential_current: active, __ps_credential_expires: active ? utc(new Date(principal.credentialExpiresAt)) : null, + __ps_credential_revoked: null, __ps_owner_until: utc(connection.owner_valid_until), __ps_owner_issued: utc(connection.owner_claims_issued_at), + __ps_cutoff_owner: null, __ps_cutoff: null, __ps_cutoff_text: null, __ps_settings: 1, __ps_enabled: true, __ps_policy_sha256: fingerprint(connection.policy) } + return { rows: values[2] && rows.length ? rows.map((row, index) => ({ ...core, __ps_grant_id: "grant-row-" + index, __ps_grant_oversize: false, __ps_grant_payload: structuredClone(row.payload), __ps_grant_sha256: fingerprint(row.payload) })) + : [{ ...core, __ps_grant_id: null, __ps_grant_oversize: null, __ps_grant_payload: null, __ps_grant_sha256: null }] } + } + throw Error("Unexpected component SQL") + } } as unknown as Pick + const assertOneRead = (start: number) => { + const calls = commands.slice(start) + assert.equal(calls.length, 2) + assert(calls.every(text => text.startsWith("/* mcp-principal:coherent-source-v1 */"))) + assert.match(calls.at(-1)!, /ORDER BY id LIMIT 2/) + } + await t.test("no grant retains ordinary connection; public delegate returns identical DTO", async () => { + let start = commands.length + const source = await effectiveMcpConnectionSource(principal, client) + assert.deepEqual(source.grantRows, []); assert.deepEqual(source.connection, connection); assertOneRead(start) + start = commands.length + assert.deepEqual(await effectiveMcpConnection(principal, client), source.connection); assertOneRead(start) + }) + await t.test("single source preserves full raw descriptor while intersecting all scope dimensions", async () => { + rows = [grant()]; const start = commands.length + const source = await effectiveMcpConnectionSource(principal, client) + assert.deepEqual(source.grantRows, rows) + assert.deepEqual(source.connection.policy.scope, { serverIds: [1], operationIds: ["servers.poll"], allowGlobal: false }) + assert.deepEqual(connection.policy.scope.serverIds, [1, 2]); assertOneRead(start) + }) + await t.test("expired/future and duplicate source refuse, with exactly two coherent source queries", async () => { + for (const candidate of [ + [{ payload: { ...grant().payload, startsAt: new Date(now - 2000).toISOString(), expiresAt: new Date(now - 1000).toISOString() } }], + [{ payload: { ...grant().payload, startsAt: new Date(now + 10000).toISOString() } }], [grant(), grant()], + ]) { + rows = candidate; const start = commands.length + await assert.rejects(effectiveMcpConnectionSource(principal, client), { code: "GRANT_INACTIVE" }); assertOneRead(start) + } + }) + await t.test("no source read survives revoked principal and no source result is cached", async () => { + rows = [grant()]; active = false; const start = commands.length + await assert.rejects(effectiveMcpConnectionSource(principal, client), { code: "UNAUTHORIZED" }) + assert.equal(commands.slice(start).some(text => text.startsWith("SELECT payload")), false) + active = true; rows = [] + assert.deepEqual((await effectiveMcpConnectionSource(principal, client)).grantRows, []) + }) + } finally { + pool.query = originalQuery; pool.connect = originalConnect + if (previousState === undefined) delete process.env.MCP_STATE_DIR + else process.env.MCP_STATE_DIR = previousState + await closePool(); await rm(state, { recursive: true, force: true }) + } +}) diff --git a/backend/src/modules/mcp/events-batch-actions.ts b/backend/src/modules/mcp/events-batch-actions.ts new file mode 100644 index 00000000..3e1c7dfb --- /dev/null +++ b/backend/src/modules/mcp/events-batch-actions.ts @@ -0,0 +1,91 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { appendClientEventRows, prepareEventRows } from "../events/service/events-service.js" +import type { EventInsertInput } from "../events/repository/events-repository.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { accessGeneration, encryptSecret, decryptSecret, fingerprint, privateSecretFingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import { eventsBatchSchema, assertEventBatchBudget } from "./events-batch-model.js" +import type { add } from "./actions.js" + +const operation = "events.batch.append" +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict() +const conflict = () => new McpError("STATE_CONFLICT", "Пакет событий или область доступа изменились", 409) +async function catalog() { + const rows = (await pool.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows + if (rows.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Каталог зависимостей журнала превышает лимит", 409) + return rows.map(row => Number(row.id)) +} +async function sourceProof(ids: number[]) { return privateSecretFingerprint(fingerprint(ids), "events-batch-catalog-v1") } +async function access(input: Record, verified = false) { + if (verified) await assertVerifiedConfigurationAccess() + const principal = principalSchema.parse(input.principal), connection = await effectiveMcpConnection(principal) + if (connection.owner_id !== input.ownerId || connection.policy.mode === "observer" || !hasPermission(connection.owner_permissions, "mm:dashboard:read") || !hasPermission(connection.policy.permissions, "mm:dashboard:read")) throw new McpError("PERMISSION_DENIED", "Допуск к записи событий отозван", 403) + if (fingerprint(connection.policy) !== input.policyProof || connection.generation !== input.connectionGeneration || await accessGeneration() !== input.generation) throw conflict() + const ids = await catalog() + if (!connection.policy.scope.allowGlobal || ids.some(id => !connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Глобальный журнал требует полного каталога серверов", 403) + if (input.catalogProof !== await sourceProof(ids)) throw conflict() + return { principal, ids } +} +async function batch(input: Record) { + const rows = JSON.parse(await decryptSecret(String(input.batchCiphertext))) as EventInsertInput[] + assertEventBatchBudget(rows) + if (!Array.isArray(rows) || !rows.length || rows.length > 200 || new Set(rows.map(row => row.id)).size !== rows.length || rows.some(row => !z.string().uuid().safeParse(row.id).success)) throw conflict() + if (input.batchProof !== await privateSecretFingerprint(fingerprint({ rows, receiptId: input.receiptId, principal: input.principal, ownerId: input.ownerId }), "events-batch-request-v1")) throw conflict() + return rows +} +async function observed(rows: EventInsertInput[]) { + const budget = (await pool.query(`WITH chosen AS MATERIALIZED (SELECT * FROM events WHERE id=ANY($1::text[]) ORDER BY id LIMIT 201), + limits AS (SELECT count(*) AS n,coalesce(sum(octet_length(row_to_json(chosen)::text)),0) AS bytes FROM chosen) + SELECT n>200 OR bytes>131072 AS overflow, CASE WHEN n<=200 AND bytes<=131072 + THEN (SELECT coalesce(jsonb_agg(to_jsonb(chosen) ORDER BY id),'[]') FROM chosen) END AS items FROM limits`, [rows.map(row => row.id)])).rows[0] + if (!budget || budget.overflow || !Array.isArray(budget.items)) throw new McpError("LIMIT_EXCEEDED", "Сохранённые события превышают бюджет", 409) + return budget.items.map((row: Record) => ({ id: row.id, createdAt: new Date(String(row.created_at)).toISOString(), level: row.level, eventType: row.event_type, sourceModule: row.source_module, title: row.title, message: row.message, + entityType: row.entity_type ?? null, entityId: row.entity_id ?? null, payload: row.payload_json ?? {} })) +} +const canonical = (rows: EventInsertInput[]) => rows.map(row => ({ ...row, createdAt: new Date(row.createdAt).toISOString(), entityType: row.entityType ?? null, entityId: row.entityId ?? null, payload: row.payload ?? {} })).sort((a, b) => a.id.localeCompare(b.id)) +async function matches(input: Record) { + const rows = await batch(input), saved = await observed(rows) + return { original: saved.length === 0, desired: fingerprint(saved) === fingerprint(canonical(rows)) } +} +const resultFor = (input: Record, rows: EventInsertInput[]) => ({ receiptId: input.receiptId, inserted: rows.length, eventIds: rows.map(row => row.id), journalEntriesOnly: true, actionsExecuted: false }) +export function registerEventsBatchActions(register: typeof add) { + registerMcpService(operation, { + current: async input => { await access(input); const state = await matches(input); if (!state.original) throw conflict(); return { catalogProof: input.catalogProof, batchAbsent: true } }, + execute: async input => { + await access(input, true); const rows = await batch(input) + await appendClientEventRows(rows, async () => { await access(input, true); if (!(await matches(input)).original) throw conflict(); await access(input, true) }) + await access(input, true) + if (!(await matches(input)).desired) throw new McpError("VERIFICATION_FAILED", "Запись пакета событий не подтверждена", 409) + return resultFor(input, rows) + }, + verify: async (input, result) => { await access(input); return fingerprint(result) === fingerprint(resultFor(input, await batch(input))) && (await matches(input)).desired }, + reconcile: async (input, authorize) => { + await authorize(); await access(input); const state = await matches(input) + const result = resultFor(input, await batch(input)) + const checkpoints = (await pool.query(`SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id + JOIN mcp_plans p ON p.id=o.plan_id WHERE o.connection_id=$1 AND p.operation_id=$2 AND s.state='verified' + AND s.intended_state=$3::jsonb AND s.result=$4::jsonb LIMIT 2`, [principalSchema.parse(input.principal).connectionId, operation, JSON.stringify(redact({ kind: "service", service: operation, input })), JSON.stringify(result)])).rows + await authorize(); await access(input) + return { matchesOriginalState: state.original, matchesDesiredState: state.desired && checkpoints.length === 1, configurationMatchesDesired: state.desired } + }, + }) + register(operation, "mm_events_batch_append_plan", "Добавить пакет записей в журнал событий приложения", "dashboard", "mm:dashboard:read", "write", "global", eventsBatchSchema, { + prepare: async (input, context) => { + context.signal?.throwIfAborted() + try { assertEventBatchBudget(input) } catch { throw new McpError("INVALID_INPUT", "Пакет событий превышает JSON-бюджет 64 КиБ/12 уровней", 400) } + const request = eventsBatchSchema.parse(input), rows = JSON.parse(JSON.stringify(prepareEventRows(request.events))) as EventInsertInput[] + for (const row of rows) { if (!Number.isFinite(Date.parse(row.createdAt))) throw new McpError("INVALID_INPUT", "Время события не распознано", 400); row.createdAt = new Date(row.createdAt).toISOString() } + try { assertEventBatchBudget(rows) } catch { throw new McpError("INVALID_INPUT", "Нормализованный пакет событий превышает JSON-бюджет", 400) } + const ids = await catalog(), catalogProof = await sourceProof(ids), receiptId = randomUUID() + const sealed: Record = { principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), connectionGeneration: context.connection.generation, generation: await accessGeneration(), catalogProof, receiptId, batchCiphertext: await encryptSecret(JSON.stringify(rows)) } + sealed.batchProof = await privateSecretFingerprint(fingerprint({ rows, receiptId, principal: context.principal, ownerId: context.connection.owner_id }), "events-batch-request-v1") + await access(sealed); context.signal?.throwIfAborted() + return { target: { scope: "global" }, objects: [{ kind: "events-journal", id: "global" }, ...rows.map(row => ({ kind: "event", id: row.id })), ...ids.map(serverId => ({ kind: "server", id: String(serverId), serverId }))], snapshots: [], before: { catalogProof, batchAbsent: true }, requiresHuman: true, mutationCost: 1, + steps: [{ kind: "service", service: operation, input: sealed }], warnings: ["Будет добавлено записей: " + rows.length + ". Подтвердите содержимое пакета во входных данных плана.", "Это записи клиентского журнала, а не доказательства выполнения системных действий. sourceModule/eventType/entity/payload сохраняются как предоставленные данные.", "Пакет вставляется атомарно с серверными UUID. Неизвестный результат не повторяется; для успешной сверки нужен точный VERIFIED checkpoint."] } + }, + }) +} diff --git a/backend/src/modules/mcp/events-batch-model.test.ts b/backend/src/modules/mcp/events-batch-model.test.ts new file mode 100644 index 00000000..96bdd96c --- /dev/null +++ b/backend/src/modules/mcp/events-batch-model.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { eventsBatchSchema, assertEventBatchBudget } from "./events-batch-model.js" +import { prepareEventRows } from "../events/service/events-service.js" +const event = { level: "warning", eventType: " client.note ", sourceModule: "system", title: " title ", message: " message ", entityType: " server ", entityId: " 1 ", payload: { known: 0, nested: { flag: false }, empty: null }, createdAt: "2026-10-24T00:00:00Z" } +test("event batch preserves all REST fields and normalizes once with unique server UUIDs", () => { + const input = eventsBatchSchema.parse({ events: [event, { ...event, entityId: undefined }] }) + const rows = prepareEventRows(input.events) + assert.equal(new Set(rows.map(row => row.id)).size, 2) + assert.deepEqual({ ...rows[0], id: undefined }, { id: undefined, createdAt: event.createdAt, level: event.level, eventType: "client.note", sourceModule: "system", title: "title", message: "message", entityType: "server", entityId: "1", payload: event.payload }) + assert.equal(rows[1].entityId, undefined) + assertEventBatchBudget(rows) +}) +test("event admission bounds bytes, JSON complexity and unsupported values before SQL", () => { + for (const input of [{ events: [] }, { events: Array(201).fill(event) }, { events: [{ ...event, id: "forged" }] }, { events: [event], receiptId: "forged" }]) assert.equal(eventsBatchSchema.safeParse(input).success, false) + assert.throws(() => assertEventBatchBudget({ message: "я".repeat(32769) })) + assert.throws(() => assertEventBatchBudget({ value: BigInt(1) })) + assert.throws(() => assertEventBatchBudget({ value: NaN })) + let nested: unknown = null; for (let depth = 0; depth < 14; depth++) nested = { nested } + assert.throws(() => assertEventBatchBudget(nested)) +}) diff --git a/backend/src/modules/mcp/events-batch-model.ts b/backend/src/modules/mcp/events-batch-model.ts new file mode 100644 index 00000000..ce3f7a9f --- /dev/null +++ b/backend/src/modules/mcp/events-batch-model.ts @@ -0,0 +1,17 @@ +import { z } from "zod" +import { appendEventSchema } from "@mmapp/contracts/events" +export const eventsBatchSchema = z.object({ events: z.array(appendEventSchema.strict()).min(1).max(200) }).strict() +/** Bound opaque JSON before encryption, SQL, preview and hashing. */ +export function assertEventBatchBudget(value: unknown) { + let nodes = 0 + const visit = (item: unknown, depth: number) => { + if (++nodes > 20000 || depth > 12) throw new Error("Event JSON complexity limit") + if (item === null || typeof item === "string" || typeof item === "boolean") return + if (typeof item === "number" && Number.isFinite(item)) return + if (Array.isArray(item)) { for (const entry of item) visit(entry, depth + 1); return } + if (item && typeof item === "object" && Object.getPrototypeOf(item) === Object.prototype) { for (const entry of Object.values(item)) if (entry !== undefined) visit(entry, depth + 1); return } + throw new Error("Event payload must be JSON") + } + visit(value, 0) + if (Buffer.byteLength(JSON.stringify(value), "utf8") > 65536) throw new Error("Event JSON byte limit") +} diff --git a/backend/src/modules/mcp/evobgp-actions.ts b/backend/src/modules/mcp/evobgp-actions.ts new file mode 100644 index 00000000..81271533 --- /dev/null +++ b/backend/src/modules/mcp/evobgp-actions.ts @@ -0,0 +1,74 @@ +import { z } from "zod" +import { evoBgpSettingsPatchSchema, normalizeEvoBgpBaseUrl, normalizeEvoBgpApiKey, type EvoBgpSavePayload } from "@mmapp/contracts/evobgp" +import { pool } from "../../db/index.js" +import { publicEvoBgpSettings, readEvoBgpSettings, writeEvoBgpSettings } from "../../services/evobgp-settings.js" +import { registerMcpService } from "./service-registry.js" +import { decryptSecret, fingerprint, McpError } from "./security.js" +import { probeEvoBgp } from "../../services/evobgp-probe.js" +import type { add } from "./actions.js" + +const schema = z.object({ + settings: z.object({ baseUrl: evoBgpSettingsPatchSchema.shape.baseUrl, enabled: z.boolean().optional() }).strict(), + apiKeyRef: z.string().uuid().nullable().optional(), +}).strict().refine(input => Object.values(input.settings).some(value => value !== undefined) || input.apiKeyRef !== undefined, "Укажите изменённые настройки или ссылку на ключ") + +export function registerEvoBgpActions(register: typeof add) { + const id = "evobgp.settings.update" + const dependencies = async () => (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => Number(row.id)) + register("evobgp.connection.check", "mm_evobgp_connection_check", "Проверка доступности EvoBGP без сохранения настроек", "settings", "mm:settings:admin", "read", "global", z.object({ baseUrl: evoBgpSettingsPatchSchema.shape.baseUrl, apiKeyRef: z.string().uuid().optional() }).strict(), { + read: async (input, context) => { + const ids = await dependencies() + if (ids.length > 1000 || ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Проверка EvoBGP требует полного каталога серверов") + const row = await readEvoBgpSettings(), root = normalizeEvoBgpBaseUrl(input.baseUrl === undefined ? row.baseUrl : String(input.baseUrl)) + let key = root === normalizeEvoBgpBaseUrl(row.baseUrl) ? row.apiKey : "" + if (input.apiKeyRef) { + const { rows: [secret] } = await pool.query("SELECT purpose,server_id,ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2", [input.apiKeyRef, context.connection.owner_id]) + if (!root || !secret || secret.server_id !== null || secret.purpose !== `evobgp-api:${root}`) throw new McpError("SECRET_UNAVAILABLE", "Секрет не выдан владельцу для этого URL EvoBGP") + const parsed = evoBgpSettingsPatchSchema.safeParse({ apiKey: await decryptSecret(secret.ciphertext) }) + if (!parsed.success || !parsed.data.apiKey) throw new McpError("SECRET_UNAVAILABLE", "Некорректный секрет EvoBGP") + key = parsed.data.apiKey + } + key = normalizeEvoBgpApiKey(key) + if (!root || !key) throw new McpError("SECRET_UNAVAILABLE", "Нужны URL и привязанный к нему API-ключ EvoBGP", 409) + await probeEvoBgp(root, key, context.signal) + if (fingerprint(await readEvoBgpSettings()) !== fingerprint(row)) throw new McpError("STATE_CONFLICT", "Настройки изменились во время проверки; повторите запрос", 409) + return { ok: true, checkedAt: new Date().toISOString(), configurationSaved: false, catalogVerified: false, routingVerified: false } + }, + }) + const current = async () => { + const row = await readEvoBgpSettings() + return { settings: publicEvoBgpSettings(row), configurationVersion: fingerprint(row), serverIds: await dependencies() } + } + registerMcpService(id, { + current, + execute: async input => { await writeEvoBgpSettings(input.patch as EvoBgpSavePayload); return { settings: publicEvoBgpSettings(await readEvoBgpSettings()) } }, + verify: async input => { + const saved = await readEvoBgpSettings(), patch = evoBgpSettingsPatchSchema.parse(input.patch) + return Object.entries(patch).every(([key, value]) => fingerprint(saved[key as keyof typeof saved]) === fingerprint(key === "apiKey" ? value ?? "" : value)) + }, + }) + register(id, "mm_evobgp_settings_update_plan", "План настроек EvoBGP с ключом, привязанным к URL", "settings", "mm:settings:admin", "write", "global", schema, { + prepare: async (input, context) => { + const before = await current() + if (before.serverIds.length > 1000 || before.serverIds.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Настройка EvoBGP требует полного каталога серверов") + const requested = schema.parse(input), row = await readEvoBgpSettings() + const root = requested.settings.baseUrl ?? row.baseUrl + const patch: EvoBgpSavePayload = { ...requested.settings } + if (root !== row.baseUrl && row.apiKey && requested.apiKeyRef === undefined) throw new McpError("SECRET_UNAVAILABLE", "Для нового URL нужен привязанный к нему секрет либо явное удаление прежнего ключа") + if (requested.apiKeyRef === null) patch.apiKey = null + else if (requested.apiKeyRef) { + const { rows: [secret] } = await pool.query("SELECT purpose,server_id,ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2", [requested.apiKeyRef, context.connection.owner_id]) + if (!root || !secret || secret.server_id !== null || secret.purpose !== `evobgp-api:${root}`) throw new McpError("SECRET_UNAVAILABLE", "Секрет не выдан владельцу для этого URL EvoBGP") + const parsed = evoBgpSettingsPatchSchema.safeParse({ apiKey: await decryptSecret(secret.ciphertext) }) + if (!parsed.success || !parsed.data.apiKey) throw new McpError("SECRET_UNAVAILABLE", "Секрет EvoBGP должен содержать непустой однострочный ключ") + patch.apiKey = parsed.data.apiKey + } + return { + target: { scope: "global" }, before, snapshots: [], mutationCost: 2, + objects: [{ kind: "evobgp-settings", id: "1" }, ...before.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId }))], + steps: [{ kind: "service", service: id, input: { patch } }], verificationScope: "configuration", requiresHuman: true, + warnings: ["Изменяется общая интеграция EvoBGP. Подтверждается сохранение настроек и ключа; доступность API и свежесть каталога требуют отдельной проверки.", "Ключ хранится в зашифрованном плане и не возвращается модели. Смена URL с прежним ключом требует нового секрета для назначения или явного удаления ключа."], + } + }, + }) +} diff --git a/backend/src/modules/mcp/evobgp-catalog.ts b/backend/src/modules/mcp/evobgp-catalog.ts new file mode 100644 index 00000000..f5073671 --- /dev/null +++ b/backend/src/modules/mcp/evobgp-catalog.ts @@ -0,0 +1,28 @@ +import { normalizeEvoBgpBaseUrl, normalizeEvoBgpApiKey } from "@mmapp/contracts/evobgp" +import { readEvoBgpSettings } from "../../services/evobgp-settings.js" +import { fetchBoundedEvoCatalog } from "../../services/evobgp-client.js" +import { parseEvoCatalogRaw } from "../../services/evobgp-catalog.js" +import { formatEvoBgpCatalog } from "../../services/evobgp-catalog-model.js" +import { distinctServersByFilterCommunityValue } from "../../services/evobgp-community-counts.js" +import { fingerprint, McpError } from "./security.js" +import type { ActionContext } from "./action-types.js" + +/** Invoked only after the global read helper has verified the entire scope. */ +export async function readEvoBgpCatalogPage(input: Record, context: ActionContext) { + const settings = await readEvoBgpSettings() + if (!settings.enabled) throw new McpError("INTEGRATION_DISABLED", "Интеграция EvoBGP выключена", 409) + const root = normalizeEvoBgpBaseUrl(settings.baseUrl), key = normalizeEvoBgpApiKey(settings.apiKey) + if (!root || !key) throw new McpError("SECRET_UNAVAILABLE", "Не заданы URL и ключ EvoBGP", 409) + const raw = parseEvoCatalogRaw(await fetchBoundedEvoCatalog(root, key, undefined, { signal: context.signal })) + const counts = await distinctServersByFilterCommunityValue() + const result = formatEvoBgpCatalog(raw, counts), offset = Number(input.offset), limit = Number(input.limit) + // A misconfigured upstream can reflect a credential into a title. Never + // return that literal even when it is nested under an ordinary field name. + const withoutCredential = (value: unknown): unknown => typeof value === "string" ? value.includes(key) ? "[redacted]" : value : Array.isArray(value) ? value.map(withoutCredential) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).map(([field, item]) => [field, withoutCredential(item)])) : value + const collections = { modules: result.modules, domains: result.domains, ipRanges: result.ipRanges, asns: result.asns, communities: result.communities } + const pages = Object.fromEntries(Object.entries(collections).map(([name, rows]) => [name, { total: rows.length, nextOffset: offset + limit < rows.length ? offset + limit : null }])) + return withoutCredential({ fetchedAt: result.fetchedAt, catalogVersion: fingerprint({ raw, counts: [...counts].sort(([a], [b]) => a.localeCompare(b)) }), offset, limit, pages, + ...Object.fromEntries(Object.entries(collections).map(([name, rows]) => [name, rows.slice(offset, offset + limit)])), + incomplete: Object.values(collections).some(rows => rows.slice(offset, offset + limit).length < rows.length), serverCountsScope: "full-effective-catalog", source: "evobgp", + }) +} diff --git a/backend/src/modules/mcp/filter-write-actions.ts b/backend/src/modules/mcp/filter-write-actions.ts new file mode 100644 index 00000000..d7397fce --- /dev/null +++ b/backend/src/modules/mcp/filter-write-actions.ts @@ -0,0 +1,125 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { applyFiltersToServer, cacheRulesetsForServer, toRouterRuleBody } from "../../routes/filters.js" +import { readCachedRecursiveRoutes } from "../../routes/recursive-routes.js" +import { canonicalFilterRules, getRevisionById, fingerprintPayload } from "../../services/config-revisions.js" +import { filterRulesWriteSchema, validatedFilterRules } from "../../services/filter-rules-write-model.js" +import { managedFilterNeedsWrite, readManagedFilterState } from "../../services/filter-rules-mutation.js" +import { managedComment, isOwnedRecursiveComment } from "../../managed-markers.js" +import { nativeBgpRows, type NativeBgpRow } from "../../services/bgp-export-model.js" +import { planBgpInApply } from "../../services/config-apply-plan.js" +import { fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { routerClient } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import type { ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const sid = z.number().int().positive().safe(), permissions = ["mm:filters:write", "mm:network:read", "mm:bgp:read"] +const unbound = z.boolean().default(false).describe("Требовать отсутствие BGP/OSPF/RIP/IS-IS instances/connections, select rules и других routing filters; безопасная начальная настройка без активного routing context") +const schema = z.object({ serverId: sid, rules: filterRulesWriteSchema, source: z.enum(["apply", "copy"]).default("apply"), requireUnbound: unbound }).strict() +const restoreSchema = z.object({ serverId: sid, revisionId: z.string().min(1).max(100), requireUnbound: unbound }).strict() +const proof = (serverId: number, domain: string, value: unknown) => privateSecretFingerprint(fingerprint(value), `filters-replace:${serverId}:${domain}`) +const stable = (row: NativeBgpRow) => Object.fromEntries(Object.entries(row).filter(([key]) => !["active", "inactive", "invalid", "running", "uptime", "expires-after", "last-started", "prefix-count", "immediate-gw", "gateway-status", "unreachable"].includes(key) && !key.startsWith("debug."))) +const paths = { gre: "/interface/gre", ipv4_address: "/ip/address", ipv6_address: "/ipv6/address", ipv4_route: "/ip/route?static=true", ipv6_route: "/ipv6/route?static=true", bgp_connection: "/routing/bgp/connection", bgp_template: "/routing/bgp/template", bgp_instance: "/routing/bgp/instance", ospf_instance: "/routing/ospf/instance", rip_instance: "/routing/rip/instance", isis_instance: "/routing/isis/instance", routing_filter_select: "/routing/filter/select-rule", routing_table: "/routing/table", vrf: "/ip/vrf" } + +async function state(input: Record) { + try { + const serverId = Number(input.serverId), rules = validatedFilterRules(input.rules), { server, client } = await routerClient(serverId) + const serverProof = await proof(serverId, "server", server) + if (input.expectedServerProof !== undefined && input.expectedServerProof !== serverProof) throw new McpError("STATE_CONFLICT", "Назначение или credentials фильтров изменились; новый источник не опрашивается", 409) + let revisionProof: string | null = null + if (input.revisionId !== undefined) { + const revision = await getRevisionById(String(input.revisionId), { serverId, section: "filters" }) + if (!revision || revision.fingerprint !== fingerprintPayload(revision.payload) || fingerprint(canonicalFilterRules(validatedFilterRules(revision.payload))) !== fingerprint(canonicalFilterRules(rules))) throw new McpError("STATE_CONFLICT", "Исходная ревизия filters удалена, изменена или повреждена", 409) + revisionProof = await proof(serverId, "revision", revision) + } + const native = await readManagedFilterState(client) + if (planBgpInApply(native.rows, rules.length).conflictIds.length) throw new McpError("STATE_CONFLICT", "Неуправляемые Community-фильтры не могут быть автоматически заменены", 409) + const [cached, recursive, ...snapshots] = await Promise.all([cacheRulesetsForServer(serverId), readCachedRecursiveRoutes(serverId), ...Object.values(paths).map(async path => { + const rows = nativeBgpRows(await client.get(path)) + if (rows.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Каталог зависимостей фильтров превышает 1000 строк", 409) + return rows + })]) + if (cached.length > 1000 || recursive.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Метаданные фильтров или recursive превышают 1000 строк", 409) + const dependencies = Object.fromEntries(Object.keys(paths).map((key, index) => [key, snapshots[index]])) as Record + const contexts = ["bgp_instance", "bgp_connection", "ospf_instance", "rip_instance", "isis_instance", "routing_filter_select"] as const + if (input.requireUnbound === true && (contexts.some(key => dependencies[key].length > 0) || native.rows.length !== native.managed.length)) throw new McpError("STATE_CONFLICT", "RequireUnbound: на сервере есть routing context, select rules или другие фильтры", 409) + const refs: ChangeEvidence["objects"] = [{ kind: "filters", id: "managed-bgp-in", serverId }, ...native.rows.map(row => ({ kind: "routing_filter", id: row[".id"], serverId }))] + for (const rule of rules) { + refs.push({ kind: "filter-community", id: rule.community, serverId }) + if (rule.action === "blackhole" || !rule.gatewayTunnelId) continue + if (rule.gatewayTunnelId.startsWith("rec:")) { + const row = recursive.find(row => "rec:" + row.id === rule.gatewayTunnelId) + if (!row || row.dstAddress.split("/")[0] !== rule.gateway || isIP(rule.gateway) !== 4) throw new McpError("STATE_CONFLICT", "Recursive reference/gateway не совпадают на выбранном сервере", 409) + const matching = dependencies.ipv4_route.filter(route => route.static === "true" && route.dynamic === "false" && typeof route.comment === "string" && isOwnedRecursiveComment(route.comment) && route["dst-address"] === (row.dstAddress.includes("/") ? row.dstAddress : row.dstAddress + "/32") && route["routing-table"] === row.routingTable && route.gateway === row.gateway) + if (matching.length !== 1) throw new McpError("STATE_CONFLICT", "Native recursive dependency отсутствует или неоднозначна", 409) + refs.push({ kind: "recursive-route", id: row.id, serverId }, { kind: "ipv4_route", id: matching[0][".id"], serverId }) + } else { + const matching = dependencies.gre.filter(row => row.name === rule.gatewayTunnelId) + if (matching.length !== 1) throw new McpError("STATE_CONFLICT", "GRE dependency отсутствует или неоднозначна", 409) + refs.push({ kind: "gre", id: matching[0][".id"], serverId }) + } + } + for (const row of cached) if (/^\d{1,5}:\d{1,5}$/.test(row.community)) refs.push({ kind: "filter-community", id: row.community, serverId }) + for (const kind of [...contexts, "bgp_template"] as const) for (const row of dependencies[kind]) refs.push({ kind, id: row[".id"], serverId }) + const protections = (await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(serverId)])).rows + if (protections.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Проверка защит превышает 1000 объектов", 409) + for (const { object_ref: ref } of protections) refs.push({ kind: ref.kind, id: ref.id, serverId }) + const objects = [...new Map(refs.map(ref => [fingerprint(ref), ref])).values()] + if (objects.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Зависимости фильтров превышают 1000 объектов", 409) + const dependencyData = Object.fromEntries(Object.entries(dependencies).map(([key, rows]) => [key, rows.filter(row => !(key === "ipv6_address" && row.dynamic === "true" && typeof row.address === "string" && /^fe80:/i.test(row.address))).map(stable)])) + const dependencyComponents = Object.fromEntries(await Promise.all(Object.entries(dependencyData).map(async ([key, rows]) => [key, await proof(serverId, "component:" + key, rows)]))) + const compiled = await toRouterRuleBody(serverId, rules, dependencies.gre) + const before = { serverId, serverProof, revisionProof, requireUnbound: input.requireUnbound === true, routingContexts: Object.fromEntries(contexts.map(key => [key, dependencies[key].length])), nativeDispatch: managedFilterNeedsWrite(native, compiled, managedComment(server.name || server.host)), cachedRuleCount: cached.length, managedNativeCount: native.managed.length, nativeFilterCount: native.rows.length, + previousRules: cached.map(rule => ({ community: /^\d{1,5}:\d{1,5}$/.test(rule.community) ? rule.community : null, action: rule.action === "blackhole" ? "blackhole" : rule.action === "route" ? "route" : "unknown", gateway: isIP(rule.gateway) ? rule.gateway : null, referenceKind: rule.gatewayTunnelId.startsWith("rec:") ? "recursive" : rule.gatewayTunnelId ? "interface" : "literal", hasDescription: !!rule.description, hasCommunityName: !!rule.communityName })), + desiredRules: canonicalFilterRules(rules), nativeProof: await proof(serverId, "native", native.proofRows), metadataProof: await proof(serverId, "metadata", canonicalFilterRules(cached)), + dependencyProof: await proof(serverId, "dependencies", { serverProof, dependencyData, recursive }), dependencyComponents, compiledProof: await proof(serverId, "compiled", compiled) } + return { server, client, rules, native, objects, before, compiled } + } catch (error) { if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Не подтверждены исходные filters/recursive/сетевые зависимости; запись не выполняется", 409) } +} +export function registerFilterWriteActions(register: typeof add) { + registerMcpService("filters.managed.replace", { + current: async input => (await state(input)).before, + execute: async input => { + const initial = await state(input), serverId = initial.server.id + if (fingerprint(initial.before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "Filters или зависимости изменились после подтверждения", 409) + const result = await applyFiltersToServer(initial.server, initial.rules, input.source === "copy" ? "copy" : input.source === "rollback" ? "rollback" : "apply", { beforeWrite: async (body, native) => { + const live = await state(input) + if (fingerprint(live.before) !== input.beforeFingerprint || await proof(serverId, "compiled", body) !== input.compiledProof || await proof(serverId, "native", native.proofRows) !== input.nativeProof || await proof(serverId, "native", (await readManagedFilterState(live.client)).proofRows) !== input.nativeProof) throw new McpError("STATE_CONFLICT", "Источник фильтров изменился перед первой записью", 409) + } }).catch(error => { + if (error instanceof McpError && error.code === "VERIFICATION_FAILED") console.warn("filters.native-stage.failed", { serverId, code: error.code, reason: error.message }) + throw error + }) + const after = await readManagedFilterState(initial.client) + return { ...result, nativeProof: await proof(serverId, "native", after.proofRows), dependencyProof: input.dependencyProof, dependencyComponents: initial.before.dependencyComponents, configurationVerified: true, forwardingVerified: false } + }, + verify: async (input, receipt) => { + const result = receipt as { nativeProof: string; dependencyProof: string; dependencyComponents: Record; configurationVerified: boolean }, after = await state(input) + const nativeMatches = after.before.nativeProof === result.nativeProof, dependenciesMatch = after.before.dependencyProof === result.dependencyProof, metadataMatches = after.before.metadataProof === await proof(after.server.id, "metadata", canonicalFilterRules(after.rules)) + if (!result.configurationVerified || !nativeMatches || !dependenciesMatch || !metadataMatches) { + console.warn("filters.verification.mismatch", { serverId: after.server.id, nativeMatches, dependenciesMatch, metadataMatches, changedComponents: Object.keys(after.before.dependencyComponents).filter(key => after.before.dependencyComponents[key] !== result.dependencyComponents?.[key]) }) + return false + } + const revision = (await pool.query("SELECT payload FROM config_revisions WHERE server_id=$1 AND section='filters' ORDER BY created_at DESC,id DESC LIMIT 1", [after.server.id])).rows[0] + const revisionMatches = !!revision && fingerprintPayload(revision.payload) === fingerprintPayload(canonicalFilterRules(after.rules)) + if (!revisionMatches) console.warn("filters.verification.revision-mismatch", { serverId: after.server.id }) + return revisionMatches + }, + }) + const prepare = async (input: Record): Promise => { + const saved = await state(input) + const request = { ...input, rules: saved.rules, beforeFingerprint: fingerprint(saved.before), expectedServerProof: saved.before.serverProof, nativeProof: saved.before.nativeProof, dependencyProof: saved.before.dependencyProof, compiledProof: saved.before.compiledProof } + return { target: { scope: "servers", serverIds: [saved.server.id] }, objects: saved.objects, snapshots: [], before: saved.before, mutationCost: (saved.before.nativeDispatch ? 1 : 0) + 1, requiresHuman: true, verificationScope: "configuration", + steps: [{ kind: "service", service: "filters.managed.replace", input: request }], warnings: ["Полностью заменяется один управляемый bgp-in набор. Пустой список удаляет этот набор; чужие фильтры и их порядок сохраняются.", + "Влияние распространяется на все routing clients, использующие bgp-in на этом сервере. BGP/OSPF/RIP/IS-IS и select contexts запечатаны; отдельная таблица/семейство не выбирается.", + "Обычный gateway без reference задаётся как literal IP. Наличие GRE/recursive проверяется, их доступность и forwarding этим планом не подтверждаются; disabled dependencies не включаются.", + "Private HMAC proofs скрывают ключи, native script и исходные описания. После unknown/partial нужен новый анализ, автоматического повтора и универсального inverse нет."] } + } + register("filters.rules.replace", "mm_filter_rules_replace_plan", "Человеческий план полной замены управляемых Community-фильтров bgp-in", "filters", permissions, "write", "servers", schema, { prepare }) + register("filters.revision.restore", "mm_filter_revision_restore_plan", "Человеческий план восстановления ревизии управляемых Community-фильтров", "filters", permissions, "write", "servers", restoreSchema, { prepare: async input => { + const revision = await getRevisionById(String(input.revisionId), { serverId: Number(input.serverId), section: "filters" }) + if (!revision || revision.fingerprint !== fingerprintPayload(revision.payload)) throw new McpError("STATE_CONFLICT", "Ревизия выбранного сервера/filters не найдена или повреждена", 409) + return prepare({ serverId: input.serverId, rules: validatedFilterRules(revision.payload), source: "rollback", revisionId: input.revisionId, requireUnbound: input.requireUnbound }) + } }) +} diff --git a/backend/src/modules/mcp/firewall-actions.ts b/backend/src/modules/mcp/firewall-actions.ts new file mode 100644 index 00000000..d198dd5a --- /dev/null +++ b/backend/src/modules/mcp/firewall-actions.ts @@ -0,0 +1,167 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { firewallMoveSchema, readFirewallMoveState, movedFirewallRows, moveFirewallRule } from "../../services/firewall-rule-move.js" +import { routerClient, readRosSnapshot } from "./resources.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { firewallRuleMutationSchema, readFirewallRuleState, assertFirewallRuleTarget, writeFirewallRule } from "../../services/firewall-rule-write.js" +import { firewallAddressMutationSchema, readFirewallAddressState, assertFirewallAddressTarget, writeFirewallAddress, expiryMatches, type AddressExpiry } from "../../services/firewall-address-write.js" +import { readFirewallRestoreRevision, readFirewallRestoreState, firewallRestoreProof, firewallRestoreOps, firewallRestoreCommandCount, restoreFirewallRevision } from "../../services/firewall-revision-restore.js" + +async function checkOspfPrefixBindings(input: Record, beforeRead?: () => Promise) { + const proof = input.ospfPrefixBindings as { fingerprint?: string; sourceProof?: string } | undefined + if (!proof?.fingerprint || !proof.sourceProof) throw new McpError("PLAN_EXPIRED", "Нет снимка привязок prefix-list OSPF; подготовьте новый план", 409) + const current = await readRosSnapshot(Number(input.serverId), "ospf_prefix_bindings", beforeRead, proof.sourceProof) + return current.fingerprint === proof.fingerprint +} + +export function registerFirewallActions(register: typeof add) { + const restoreId = "firewall.revision.restore" + const restoreCurrent = async (input: Record) => { + const sid = Number(input.serverId), revision = await readFirewallRestoreRevision(sid, String(input.revisionId)) + if (input.ospfPrefixBindings && !await checkOspfPrefixBindings(input)) throw new McpError("STATE_CONFLICT", "Привязки prefix-list OSPF изменились после одобрения", 409) + return { revisionFingerprint: revision.revisionFingerprint, state: firewallRestoreProof(await readFirewallRestoreState((await routerClient(sid)).server)) } + } + registerMcpService(restoreId, { current: restoreCurrent, + execute: async input => { + const { catalog: _catalog, ...receipt } = await restoreFirewallRevision((await routerClient(Number(input.serverId))).server, String(input.revisionId), String(input.beforeFingerprint), String(input.revisionFingerprint), + input.ospfPrefixBindings ? () => checkOspfPrefixBindings(input, assertVerifiedConfigurationAccess) : undefined) + return receipt + }, + verify: async (input, result) => { + const receipt = result as { verified?: boolean; afterFingerprint?: string } | null + return receipt?.verified === true && fingerprint(firewallRestoreProof(await readFirewallRestoreState((await routerClient(Number(input.serverId))).server))) === receipt.afterFingerprint + && (!input.ospfPrefixBindings || await checkOspfPrefixBindings(input)) + }, + }) + register(restoreId, "mm_firewall_revision_restore_plan", "План восстановления Firewall из ревизии", "network", "mm:network:write", "write", "servers", z.object({ serverId: z.number().int().positive().safe(), revisionId: z.string().uuid() }).strict(), { + version: 2, + prepare: async input => { + const sid = Number(input.serverId), revision = await readFirewallRestoreRevision(sid, String(input.revisionId)), state = await readFirewallRestoreState((await routerClient(sid)).server) + const ops = firewallRestoreOps(revision.desired, state), objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(sid), serverId: sid }] + for (const chunk of state.chunks) for (const row of chunk.rows) objects.push({ kind: `${chunk.family === "ip6" ? "ipv6" : "ip"}_${chunk.table === "address-list" ? "address_list" : chunk.table}`, id: row[".id"], serverId: sid }) + const ospf = ops.some(op => op.path.startsWith("/ipv6/firewall/address-list")) ? await readRosSnapshot(sid, "ospf_prefix_bindings") : undefined + const listNames = new Set([ + ...state.chunks.filter(chunk => chunk.family === "ip6" && chunk.table === "address-list").flatMap(chunk => chunk.rows.map(row => row.list)), + ...revision.desired.addressLists.filter(row => row.family === "ip6").map(row => row.list), + ]) + const affected = ospf?.rows.filter(row => typeof row["prefix-list"] === "string" && listNames.has(row["prefix-list"])) ?? [] + for (const row of affected) objects.push({ kind: "ospf_template", id: String(row[".id"]), serverId: sid }) + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(sid)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел проверки защит", 503) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: sid }) + const before = { revisionFingerprint: revision.revisionFingerprint, state: firewallRestoreProof(state) } + return { target: { scope: "servers", serverIds: [sid] }, before, snapshots: [], objects, mutationCost: firewallRestoreCommandCount(ops) + 1, requiresHuman: true, + steps: [{ kind: "service", service: restoreId, input: { ...input, desired: revision.desired, beforeFingerprint: fingerprint(before.state), revisionFingerprint: revision.revisionFingerprint, ...(ospf ? { ospfPrefixBindings: { fingerprint: ospf.fingerprint, sourceProof: ospf.sourceProof } } : {}) } }], + warnings: ["Заменяется весь постоянный Firewall IPv4/IPv6: filter/nat/mangle/raw и address-list. Меняются native ID; порядок и число одинаковых правил берутся из ревизии. Пустая ревизия удаляет весь статический набор.", ...(affected.length ? ["Восстанавливаемый IPv6-список используется шаблонами OSPFv3. Изменение может повлиять на объявляемые префиксы."] : []), "Новые записи сначала создаются отключёнными и проверяются; после успешной подготовки старые статические записи удаляются, нужные новые включаются. Это не атомарная транзакция: переключение может изменить управление и существующие соединения.", "Динамические записи не удаляются и сохраняют взаимный порядок; после замены они будут перед восстановленным статическим набором. Их исчезновение/появление во время операции останавливает дальнейшие стадии.", "Старые ревизии без native-полей восстанавливают только сохранённую форму, а не отсутствующие matcher-поля исходного устройства. Новые ревизии сохраняют native matcher-поля. RAM/timeout записи не восстанавливаются.", "Каждая стадия проверяется до следующей команды. Потерянный ответ не повторяется; обратного автоматического restore нет. После partial/unknown необходима сверка. Проверка пакетов/связности не выполняется."] } + }, + }) + for (const [index, operation] of ["create", "update", "toggle", "delete"].entries()) { + const service = `firewall.addressLists.${operation}` + const address = (input: Record) => firewallAddressMutationSchema.options[index].parse(input.address) + const state = async (input: Record) => readFirewallAddressState((await routerClient(Number(input.serverId))).server, address(input)) + const checkOspfBindings = async (input: Record, beforeRead?: () => Promise) => { + if (address(input).family !== "ip6") return true + return checkOspfPrefixBindings(input, beforeRead) + } + registerMcpService(service, { current: async input => { + const next = await state(input), mutation = address(input) + if (input.beforeFingerprint && mutation.operation !== "create" && !expiryMatches(input.expectedExpiry as AddressExpiry | undefined, next.timers[mutation.rosId])) throw new McpError("STATE_CONFLICT", "Срок address-list изменился после подтверждения", 409) + if (!await checkOspfBindings(input)) throw new McpError("STATE_CONFLICT", "Привязки prefix-list OSPF изменились после одобрения", 409) + return next.stable + }, + execute: async input => writeFirewallAddress((await routerClient(Number(input.serverId))).server, address(input), String(input.beforeFingerprint), input.expectedExpiry as AddressExpiry | undefined, + address(input).family === "ip6" ? async () => { + if (!await checkOspfBindings(input, assertVerifiedConfigurationAccess)) throw new McpError("STATE_CONFLICT", "Привязки prefix-list OSPF изменились после одобрения", 409) + } : undefined), + verify: async (input, result) => { + const receipt = result as { verified?: boolean; afterFingerprint?: string; affectedId?: string; afterExpiry?: AddressExpiry } | null + if (receipt?.verified !== true) return false + const next = await state(input) + return fingerprint(next.stable) === receipt.afterFingerprint && expiryMatches(receipt.afterExpiry, receipt.affectedId ? next.timers[receipt.affectedId] : undefined) + && await checkOspfBindings(input) + }, + }) + register(service, `mm_firewall_address_list_${operation}_plan`, `План ${["создания", "изменения", "переключения активности", "удаления"][index]} записи address-list`, "network", "mm:network:write", "write", "servers", z.object({ serverId: z.number().int().positive().safe(), address: firewallAddressMutationSchema.options[index] }).strict(), { + version: 2, + prepare: async input => { + const mutation = address(input), sid = Number(input.serverId), before = await state(input) + assertFirewallAddressTarget(mutation, before) + const resource = `${mutation.family === "ip6" ? "ipv6" : "ip"}_address_list` + const objects: ChangeEvidence["objects"] = before.stable.rows.map(row => ({ kind: resource, id: row[".id"], serverId: sid })) + objects.push({ kind: "server", id: String(sid), serverId: sid }) + const ospf = mutation.family === "ip6" ? await readRosSnapshot(sid, "ospf_prefix_bindings") : undefined + const oldList = mutation.operation === "create" ? undefined : before.stable.rows.find(row => row[".id"] === mutation.rosId)?.list + const newList = mutation.operation === "create" || mutation.operation === "update" ? mutation.fields.list : undefined + const affectedLists = new Set([oldList, newList].filter(Boolean)) + const affectedOspf = ospf?.rows.filter(row => typeof row["prefix-list"] === "string" && affectedLists.has(row["prefix-list"])) ?? [] + for (const row of affectedOspf) objects.push({ kind: "ospf_template", id: String(row[".id"]), serverId: sid }) + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(sid)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел проверки защит", 503) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: sid }) + const expiry = mutation.operation === "create" ? undefined : before.timers[mutation.rosId] + if (expiry && expiry.earliest < Date.now() + 5000) throw new McpError("STATE_CONFLICT", "Address-list истекает слишком скоро для подтверждения", 409) + return { target: { scope: "servers", serverIds: [sid] }, before: before.stable, snapshots: [], objects, mutationCost: 2, requiresHuman: true, + ...(expiry ? { validUntil: new Date(expiry.earliest - 2000).toISOString() } : {}), + steps: [{ kind: "service", service, input: { ...input, beforeFingerprint: fingerprint(before.stable), ...(expiry ? { expectedExpiry: expiry } : {}), ...(ospf ? { ospfPrefixBindings: { fingerprint: ospf.fingerprint, sourceProof: ospf.sourceProof } } : {}) } }], + warnings: ["Address-list может менять поведение всех использующих его правил; проверяются все зарегистрированные защиты сервера. Требуется человек.", ...(affectedOspf.length ? ["IPv6-список используется шаблонами OSPFv3. Изменение может повлиять на объявляемые префиксы."] : []), "Timeout — оставшееся время: проверяется окно срока с учётом секунды точности native-таймера и длительности запроса. Изменение срока вне этого окна блокирует старый план; естественное уменьшение не считается дрейфом. Временная запись хранится в RAM и может исчезнуть.", "Форма сохраняет непустые переданные поля. Поддержка изменения dynamic-записи зависит от устройства; отказ не повторяется. Неоднозначный DNS-эффект или неподтверждённый read-back требует сверки, а не повторной команды. Packet matching и DNS forwarding не проверяются. Статус ревизии возвращается отдельно."] } + }, + }) + } + for (const [index, operation] of ["create", "update", "toggle", "delete"].entries()) { + const service = `firewall.rules.${operation}` + const rule = (input: Record) => firewallRuleMutationSchema.options[index].parse(input.rule) + const current = async (input: Record) => readFirewallRuleState((await routerClient(Number(input.serverId))).server, rule(input)) + registerMcpService(service, { current, + execute: async input => writeFirewallRule((await routerClient(Number(input.serverId))).server, rule(input), String(input.beforeFingerprint)), + verify: async (input, result) => { + const receipt = result as { verified?: boolean; afterFingerprint?: string } | null + return receipt?.verified === true && fingerprint(await current(input)) === receipt.afterFingerprint + }, + }) + register(service, `mm_firewall_rule_form_${operation}_plan`, `План ${["создания", "изменения", "переключения активности", "удаления"][index]} правила Firewall через сервис приложения`, "network", "mm:network:write", "write", "servers", z.object({ serverId: z.number().int().positive().safe(), rule: firewallRuleMutationSchema.options[index] }).strict(), { + prepare: async input => { + const mutation = rule(input), sid = Number(input.serverId), before = await current(input) + assertFirewallRuleTarget(mutation, before) + const resource = `${mutation.family === "ip6" ? "ipv6" : "ip"}_${mutation.table}` + const objects: ChangeEvidence["objects"] = before.rows.map(row => ({ kind: resource, id: row[".id"], serverId: sid })) + objects.push({ kind: "server", id: String(sid), serverId: sid }) + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(sid)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел проверки защит", 503) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: sid }) + return { target: { scope: "servers", serverIds: [sid] }, before, snapshots: [await readRosSnapshot(sid, resource)], objects, mutationCost: 2, requiresHuman: true, + steps: [{ kind: "service", service, input: { ...input, beforeFingerprint: fingerprint(before) } }], + warnings: ["Firewall может влиять на управление, NAT и существующие соединения; требуются человеческое подтверждение и проверка защищённых зависимостей сервера.", "Порядок и все непрозрачные matcher-поля сохраняются; байтовые/пакетные счётчики не считаются дрейфом. Проверяется конфигурация, передача пакетов не проверяется.", "Пустые поля формы не очищают существующие matcher-поля, как и в REST. При потере ответа команда не повторяется; восстановление готовится отдельным планом. Статус ревизии возвращается отдельно."] } + }, + }) + } + const id = "firewall.rules.move" + const current = async (input: Record) => readFirewallMoveState((await routerClient(Number(input.serverId))).server, firewallMoveSchema.parse(input.move)) + registerMcpService(id, { + current, + execute: async input => moveFirewallRule((await routerClient(Number(input.serverId))).server, firewallMoveSchema.parse(input.move), String(input.beforeFingerprint)), + verify: async (input, result) => { + if (!(result as { verified?: boolean } | null)?.verified) return false + return fingerprint(await current(input)) === input.afterFingerprint + }, + }) + register(id, "mm_firewall_rule_move_plan", "План точного перемещения правила Firewall", "network", "mm:network:write", "write", "servers", z.object({ serverId: z.number().int().positive().safe(), move: firewallMoveSchema }).strict(), { + prepare: async input => { + const before = await current(input), move = firewallMoveSchema.parse(input.move), sid = Number(input.serverId) + const after = movedFirewallRows(before.rows, move) + if (fingerprint(before.rows) === fingerprint(after)) throw new McpError("STATE_CONFLICT", "Правило уже находится в запрошенной позиции", 409) + const resource = `${move.family === "ip6" ? "ipv6" : "ip"}_${move.table}` + const objects: ChangeEvidence["objects"] = before.rows.map(row => ({ kind: resource, id: row[".id"], serverId: sid })) + objects.push({ kind: "server", id: String(sid), serverId: sid }) + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(sid)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел проверки защит", 503) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: sid }) + return { target: { scope: "servers", serverIds: [sid] }, before, snapshots: [await readRosSnapshot(sid, resource)], objects, mutationCost: 2, requiresHuman: true, + steps: [{ kind: "service", service: id, input: { ...input, beforeFingerprint: fingerprint(before), afterFingerprint: fingerprint({ ...before, rows: after }) } }], + warnings: ["Меняется порядок всей выбранной таблицы Firewall. Полная конфигурация и порядок проверяются перед командой и после неё.", "Перемещаются только подтверждённо статические правила. Изменение может затронуть управление и существующие соединения; проверка пакетов/связности отдельно не выполняется.", "Ревизия сохраняется только при полном подтверждённом снимке; отсутствие ревизии видно в результате шага. При потере ответа команда не повторяется; автоматический inverse не создаётся."] } + }, + }) +} diff --git a/backend/src/modules/mcp/flow-analytics-actions.ts b/backend/src/modules/mcp/flow-analytics-actions.ts new file mode 100644 index 00000000..99ed633a --- /dev/null +++ b/backend/src/modules/mcp/flow-analytics-actions.ts @@ -0,0 +1,92 @@ +import { assertBoundedFlowSource as assertTrafficHistorySource } from "./flow-source-scope.js" +import { withCurrentFlowReadQuality } from "./flow-read-quality.js" +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 { getBoundedFlowServerRowById as getServerRowById } from "../../services/traffic-flow-server-source.js" +import { assertTrafficHistoryAccess } from "./traffic-history-actions.js" +import { McpError } from "./security.js" +import type { add } from "./actions.js" + +export function registerLegacyFlowAnalyticsAction(register: typeof add) { + register("monitoring.flow.analytics", "mm_netflow_analytics_get", "NetFlow выбранного сервера", "traffic", "mm:traffic:read", "read", "servers", z.object({ + serverId: z.number().int().positive().safe(), minutes: z.number().int().min(1).max(1440).default(15), + interface: z.string().min(1).max(100).optional(), + }).strict(), { + read: async (input, context) => { + await assertTrafficHistoryAccess(context) + const server = await getServerRowById(Number(input.serverId)) + 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 }) + return { + serverId: input.serverId, source: "PostgreSQL/flow_buckets", observedAt: new Date().toISOString(), + 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, + categories: result.categories, services: result.services, + sources: result.sources.map(row => ({ ...row, label: row.id })), + destinations: result.destinations.map(row => ({ ...row, label: row.id })), + } + } catch (error) { + await assertTrafficHistoryAccess(context) + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Аналитика недоступна, изменилась во время чтения или превышает предел; уменьшите окно", 409) + } + }, + }) +} + +export function registerFlowAnalyticsActions(register: typeof add) { + register("traffic.flow.analytics.read", "mm_netflow_analytics_report_get", "NetFlow: нагрузка, приложения и разговоры выбранного сервера", "traffic", "mm:traffic:read", "read", "servers", z.object({ + serverId: z.number().int().positive().safe(), minutes: z.number().int().min(1).max(1440).default(15), + userId: z.string().min(1).max(100).optional(), + interface: z.string().trim().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f]/.test(value) && value !== "__all__").optional(), + dedup: z.boolean().default(true), excludeMesh: z.boolean().default(true), excludeOverlay: z.boolean().default(true), + offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(100).default(50), + }).strict(), { + read: async (input, context) => { + await assertTrafficHistoryAccess(context) + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const authorize = () => assertTrafficHistorySource(context, server) + const startedAt = new Date().toISOString() + 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, + 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 })) + const offset = Number(input.offset), limit = Number(input.limit), pages: Record = {} + const analytics: Record = { ...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. + 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, + 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" }, + 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)), + incomplete: true, coverageVerified: false, rowSumIsNetworkTotal: false, forwardingVerified: false, + inferredPathsVerified: false, historicalOwnershipVerified: false, attribution: "current named whole-interface bindings; users with hidden bindings excluded", + backgroundEnrichmentStarted: false, collectorStarted: false } + if (Buffer.byteLength(JSON.stringify(output)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Ответ аналитики превышает 1 МиБ; уменьшите страницу", 409) + return withCurrentFlowReadQuality(output,context) + } catch (error) { + await assertTrafficHistoryAccess(context) + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Аналитика недоступна, изменилась во время чтения или превышает предел; уменьшите окно", 409) + } + }, + }) +} diff --git a/backend/src/modules/mcp/flow-catalog-actions.ts b/backend/src/modules/mcp/flow-catalog-actions.ts new file mode 100644 index 00000000..0d720911 --- /dev/null +++ b/backend/src/modules/mcp/flow-catalog-actions.ts @@ -0,0 +1,82 @@ +import { assertBoundedFlowSource as assertTrafficHistorySource } from "./flow-source-scope.js" +import { withCurrentFlowReadQuality } from "./flow-read-quality.js" +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 { projectFlowTalkers } from "../../services/traffic-flow-talkers-model.js" +import { resolveIfaceName } from "../../services/traffic-flow-ifindex.js" +import { assertTrafficHistoryAccess } from "./traffic-history-actions.js" +import { getTrafficFlowSettingsRow } from "../../services/traffic-flow-settings.js" +import { McpError } from "./security.js" +import { catalogPaging } from "./catalog-scope.js" +import { flowCatalogDevices as catalogDevices } from "./flow-source-scope.js" +import type { add } from "./actions.js" + +const sourceSemantics = { coverageVerified: false, rowSumIsNetworkTotal: false, deduplicatedAcrossInterfaces: false, forwardingVerified: false } +export function registerFlowCatalogActions(register: typeof add) { + const schema = catalogPaging.safeExtend({ minutes: z.number().int().min(1).max(1440).default(15) }) + register("traffic.flow.talkers.read", "mm_netflow_talkers_get", "NetFlow: основные разговоры разрешённых серверов", "traffic", "mm:traffic:read", "read", "servers", schema, { + read: async (input, context) => { + 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 topN = window.topConversationsLimit ?? 20 + const projection = projectFlowTalkers(window.rows, Number(input.minutes), 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) + await assertTrafficHistoryAccess(context) + return withCurrentFlowReadQuality({ ...page, items, total: projection.talkers.length, totalConversations: projection.totalConversations, + nextOffset: offset + items.length < projection.talkers.length ? offset + items.length : null, + 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, + source: "flow_buckets plus pending; scope before SQL and retained conversation aggregation", + bpsMeaning: "retained conversation bytes averaged over the entire requested window", + ...sourceSemantics },context) + } catch (error) { + await assertTrafficHistoryAccess(context) + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Разговоры NetFlow недоступны или превышен предел источника 50000 строк; сократите область или окно", 409) + } + }, + }) + 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 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, + 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) + }, + }) + 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))) + 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), + source: "retained ingress rows and current named whole-interface bindings", topRowsPerBucketLimit: data.window.topRowsPerBucketLimit, + from: data.window.since, to: data.window.until, ...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])$/) }), { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context, { includeDisabled: true }) + const statistics = await getFlowMonthly(String(input.month), selected.map(row => row.id), { maxKeys: 10000 }), offset = Number(input.offset), limit = Number(input.limit) + const collections = Object.fromEntries((["countries", "services", "asns"] as const).map(key => [key, { items: statistics[key].slice(offset, offset + limit), total: statistics[key].length, nextOffset: offset + limit < statistics[key].length ? offset + limit : null }])) + return withCurrentFlowReadQuality({ ...page, month: statistics.month, bytes: statistics.bytes, collections, incomplete: page.incomplete || Object.values(collections).some(value => value.items.length < value.total), + source: "flow_daily_dims; scope before SQL SUM/GROUP BY", bpsMeaning: "stored bytes averaged over the entire calendar month", coverageVerified: false, deduplicationVerified: false, wireRateVerified: false, forwardingVerified: false },context) + }, + }) +} diff --git a/backend/src/modules/mcp/flow-envelope-allowance.test.ts b/backend/src/modules/mcp/flow-envelope-allowance.test.ts new file mode 100644 index 00000000..edcf6d19 --- /dev/null +++ b/backend/src/modules/mcp/flow-envelope-allowance.test.ts @@ -0,0 +1,37 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { planFlowEnvelope } from "./flow-envelope-allowance.js" +import { boundedToolError, boundedToolResult } from "./result-budget.js" +import { deliverRegisteredToolResult } from "./result-delivery.js" +const info = { name: "MikrotikManager", version: "1.0.0" } +const read = { id: "traffic.flow.fixture", effect: "read" as const, hasRead: true, hasPrepare: false } +test("same2MiB/8MiB caps, exact id/framing, existing1024 reserve and B1 ceiling", () => { + for (const cap of [2, 8].map(value => value * 1048576)) for (const era of ["legacy", "modern"] as const) { + const allowance = planFlowEnvelope("я\\\"".repeat(100), era, cap, info) + assert(allowance.readByteLimit <= 1048576) + assert(allowance.readByteLimit + allowance.reservedEnvelopeBytes <= cap) + assert.equal(allowance.reservedEnvelopeBytes, allowance.terminalEnvelopeBytes + 1024) + } + assert(planFlowEnvelope("a".repeat(1048576 - 2), "modern", 2 * 1048576, info).readByteLimit < 1048576) +}) +test("read narrowing never produces empty healthy quality or fake metadata", () => { + const decision = boundedToolResult({ quality: "unknown", metadata: { received: 2, dropped: 1 }, rows: "x".repeat(5000) }, "read", undefined, 8192) + assert.equal(decision.kind, "result") + if (decision.kind === "result") { assert.equal(decision.result.isError, true); assert.equal(decision.result.structuredContent, undefined); assert(!JSON.stringify(decision.result).includes('"quality"')) } +}) +test("read allowance is validated before effects and cannot change applied outcome budget", async () => { + let invoked = 0 + for (const contract of [read, { ...read, id: "changes.apply", effect: "write" as const }]) { + const result = await deliverRegisteredToolResult(contract, async () => { invoked++; return {} }, () => boundedToolError("INTERNAL_ERROR", "ошибка"), undefined, 100) + assert.equal(result.isError, true) + } + assert.equal(invoked, 0) +}) +test("bounded invocation error is retained too; fitting quality and metadata stay intact", async () => { + const charged: number[] = [] + const error = await deliverRegisteredToolResult(read, async () => { throw Error("private") }, () => boundedToolError("STATE_UNAVAILABLE", "Источник временно недоступен"), bytes => charged.push(bytes), 8192) + assert.equal(error.isError, true); assert.equal(charged[0], Buffer.byteLength(JSON.stringify(error))) + const dto = { quality: "unknown", metadata: { received: 2, dropped: 1 } } + const fit = await deliverRegisteredToolResult(read, async () => dto, () => { throw Error("unexpected") }, undefined, 8192) + assert.deepEqual(fit.structuredContent, { result: dto }) +}) diff --git a/backend/src/modules/mcp/flow-envelope-allowance.ts b/backend/src/modules/mcp/flow-envelope-allowance.ts new file mode 100644 index 00000000..25484942 --- /dev/null +++ b/backend/src/modules/mcp/flow-envelope-allowance.ts @@ -0,0 +1,39 @@ +import { MCP_CALL_RESULT_BYTES } from "./result-budget.js" +import { MCP_REQUEST_BODY_BYTES } from "./wire-admission.js" + +export const FLOW_PROTOCOL_RESERVE_BYTES = 1024 // existing per-result reserve, retained +type ServerInfo = { name: string; version: string } +/** Shared current tools/call stamp arithmetic, NOT a stream/heap bound. + * Discovery/cache fields are measured in the actual encoded SDK message. */ +export function measureMcpTerminalEnvelope(idBytes: number, era: "legacy" | "modern", serverInfo: ServerInfo) { + if (!Number.isSafeInteger(idBytes) || idBytes < 1 || !["legacy", "modern"].includes(era)) throw Error("Unproved wire context") + const modernStamp = era === "modern" ? Buffer.byteLength(JSON.stringify({ resultType: "complete", _meta: { "io.modelcontextprotocol/serverInfo": serverInfo } })) - 1 : 0 + const jsonRpcEnvelopeBytes = idBytes + 33 + modernStamp + const terminalEnvelopeBytes = jsonRpcEnvelopeBytes + (era === "legacy" ? 23 : 0) + if (!Number.isSafeInteger(modernStamp) || modernStamp < 0 || !Number.isSafeInteger(jsonRpcEnvelopeBytes) || !Number.isSafeInteger(terminalEnvelopeBytes)) throw Error("Unproved wire arithmetic") + return Object.freeze({ idBytes, jsonRpcEnvelopeBytes, terminalEnvelopeBytes, modernStamp }) +} +/** HTTP admission domain remains 1MiB. The pure codec measurement above is + * not permission to widen HTTP/Flow limits for another transport. */ +export function planMcpTerminalEnvelope(idBytes: number, era: "legacy" | "modern", serverInfo: ServerInfo) { + if (!Number.isSafeInteger(idBytes) || idBytes < 1 || idBytes > MCP_REQUEST_BODY_BYTES || !["legacy", "modern"].includes(era)) throw Error("Unproved wire context") + return measureMcpTerminalEnvelope(idBytes, era, serverInfo) +} +/** + * Narrow current tools/call contract: B1 structuredContent is object-shaped; + * modern default-auto has no related notifications on these NetFlow reads. + * Own top-level _meta on an error uses less stamp space than the no-meta case. + * Bounds the terminal message, NOT keepalive/HTTP headers/global heap. + * Existing NetFlow allowance/caps unchanged; delegates only the arithmetic. + */ +export function planFlowEnvelope(id: unknown, era: "legacy" | "modern", outputCap: number, serverInfo: ServerInfo) { + if (typeof id !== "string" && typeof id !== "number") throw Error("Unproved request id") + if (typeof id === "number" && !Number.isFinite(id) || !["legacy", "modern"].includes(era)) throw Error("Unproved wire context") + const idBytes = Buffer.byteLength(JSON.stringify(id)) + if (!Number.isSafeInteger(outputCap) || outputCap <= 0 || idBytes > MCP_REQUEST_BODY_BYTES) throw Error("Unproved output capacity") + const { terminalEnvelopeBytes } = planMcpTerminalEnvelope(idBytes, era, serverInfo) + const reservedEnvelopeBytes = terminalEnvelopeBytes + FLOW_PROTOCOL_RESERVE_BYTES + const readByteLimit = Math.min(MCP_CALL_RESULT_BYTES, outputCap - reservedEnvelopeBytes) + if (readByteLimit < 8192) throw Error("No bounded read-result allowance") + return Object.freeze({ idBytes, terminalEnvelopeBytes, reservedEnvelopeBytes, readByteLimit }) +} diff --git a/backend/src/modules/mcp/flow-facts-rebuild-acceptance.ts b/backend/src/modules/mcp/flow-facts-rebuild-acceptance.ts new file mode 100644 index 00000000..e5ca50e7 --- /dev/null +++ b/backend/src/modules/mcp/flow-facts-rebuild-acceptance.ts @@ -0,0 +1,127 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import type { FastifyInstance } from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import { ensurePartitionFor } from "../../db/partitions.js" +import { env } from "../../config.js" +import type { AuthUser } from "../../lib/permissions.js" +import { rememberServerIfaces } from "../../services/traffic-flow-ifindex.js" +import { seedFlowCatalogForTests } from "../../services/traffic-flow-classify.js" +import { seedRipeCacheForTests } from "../../services/traffic-flow-ripe.js" +import { getServerCatalog, loadFlowTopology, invalidateFlowCatalogCache } from "../../services/traffic-flow-topology.js" +import { getFlowListenerState, getFlowWorkerHealth, ingestParsedFlowsForServerForTests, peekPendingFlows } from "../../services/traffic-flow-ingest.js" +import { createMcpConnection, authenticateMcpToken, rotateConnectionToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { decryptSecret } from "./security.js" +import type { ChangeEvidence } from "./action-types.js" + +/** Root-owned disposable FULL-schema database only. The supplied application + * must expose the actual /api/traffic/flow/rebuild-facts route with JWT auth. + * This helper does not start/stop/restart a collector or perform network I/O. */ +export async function acceptMcpFlowFactsRebuild(actor: AuthUser, app: FastifyInstance) { + assert.equal(process.env.MMAPP_OWN_MCP_FLOW_REBUILD, "true") + assert.equal(new URL(process.env.DATABASE_URL!).hostname, "127.0.0.1") + assert.equal(env.authRequired, true, "REST parity requires real owner-JWT authorization") + for (const table of ["flow_buckets", "flow_hour_facts", "flow_daily_facts"]) assert.equal(Number((await pool.query(`SELECT count(*) AS n FROM ${table}`)).rows[0].n), 0, "isolated empty traffic store required") + assert.equal(peekPendingFlows().length, 0) + assert.equal(getFlowWorkerHealth().alive, false, "fixture must use the existing main writer with no worker") + const listener = getFlowListenerState(), serverId = 198, userId = "mcp-rebuild-" + randomUUID(), connections: string[] = [] + const protectionId = randomUUID(), bindingId = randomUUID(), jobs: string[] = [] + let networkAttempts = 0 + const savedFetch = globalThis.fetch + globalThis.fetch = (async () => { networkAttempts++; throw Error("MCP rebuild acceptance forbids network access") }) as typeof fetch + const wait = async (id: string) => { + for (const deadline = Date.now() + 60000; Date.now() < deadline;) { + const row = (await pool.query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw Error("MCP rebuild remains live; no replay is permitted") + } + const count = async (table: string) => Number((await pool.query(`SELECT count(*) AS n FROM ${table}`)).rows[0].n) + const source = async (id: string) => JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [id])).rows[0].evidence.sealed)) as ChangeEvidence + try { + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=$1", [serverId])).rows[0].n, 0) + await pool.query("INSERT INTO servers(id,name,host,type,wan_uplinks) VALUES($1,'mcp-rebuild-fixture','203.0.113.198','jump-host',$2::jsonb)", [serverId, JSON.stringify([{ iface: "ether1", ip: "203.0.113.198" }])]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES($1,$1,'MCP rebuild fixture')", [userId]) + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type) VALUES($1,$2,$3,'gre-client','gre')", [bindingId, userId, serverId]) + const now = new Date(), day = now.toISOString().slice(0, 10), minute = now.toISOString().slice(0, 16) + ":00Z" + await ensurePartitionFor(pool, "server_snapshots", "week", now) + await pool.query("INSERT INTO server_snapshots(server_id,polled_at,status,raw_interfaces) VALUES($1,$2,'online',$3::jsonb)", [serverId, now, JSON.stringify([{ name: "gre-client", type: "gre", ifindex: "10" }, { name: "ether1", type: "ether" }])]) + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await ensurePartitionFor(pool, "flow_buckets", "day", now) + 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,$2,'95.167.1.10','8.8.8.8',6,1234,443,10,1,'10','ether1')", [serverId, minute]) + rememberServerIfaces(serverId, [{ name: "gre-client", ifindex: "10" }]) + seedRipeCacheForTests({ prefix: "8.8.8.0/24", asn: 15169, country: "US", lat: null, lng: null, holder: "GOOGLE", ok: true, fetchedAt: Date.now() }) + seedFlowCatalogForTests({ cidrs: [{ cidr: "8.8.8.0/24", purpose: "fixture-service" }] }) + invalidateFlowCatalogCache(); await getServerCatalog(); await loadFlowTopology() + const ids = (await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id)) + const configuration = mcpConnectionCreateSchema.parse({ name: "MCP facts rebuild", mode: "autonomous", dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["traffic.flow.facts.rebuild"], permissions: ["mm:traffic:write"], expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { allowGlobal: true, serverIds: ids, operationIds: ["traffic.flow.facts.rebuild", "traffic.flow.facts.rebuild.reconcile"] }, limits: { callsPerMinute: 240, mutationsPerHour: 1000 } }) + const access = await createMcpConnection(configuration, actor); connections.push(access.connection.id) + const principal = await authenticateMcpToken(access.token) + const prepare = () => invokeMcpTool(principal, "mm_netflow_facts_rebuild_plan", {}) as Promise<{ planId: string; state: string; changes: unknown }> + const beforeInvalid = await count("mcp_requests") + for (const input of [{ serverIds: ids }, { force: true }, { jobId: randomUUID() }, { jobId: undefined }]) await assert.rejects(invokeMcpTool(principal, "mm_netflow_facts_rebuild_plan", input)) + assert.equal(await count("mcp_requests"), beforeInvalid) + const plan = await prepare(), evidence = await source(plan.planId), step = evidence.steps[0] + assert.equal(step.kind, "service"); if (step.kind !== "service") throw Error("service required") + const jobId = String(step.input.jobId); jobs.push(jobId) + assert.equal(plan.state, "awaiting-approval", "human approval is mandatory in autonomous mode") + assert.equal(evidence.mutationCost, 3) + assert.deepEqual([...new Set(evidence.objects.flatMap(object => object.kind === "server" ? [object.serverId] : []))], ids) + assert.equal((plan.changes as { steps: { input: { bindingCiphertext: string } }[] }).steps[0].input.bindingCiphertext, "[redacted]") + const beforeApproval = await count("flow_write_receipts") + await assert.rejects(applyPlan(principal, plan.planId, randomUUID())) + assert.equal(await count("flow_write_receipts"), beforeApproval) + ingestParsedFlowsForServerForTests(serverId, [{ src: "95.167.1.10", dst: "8.8.8.8", proto: 6, srcPort: 1235, dstPort: 443, bytes: 5, packets: 2, inIface: "10", outIface: "ether1" }]) + assert.equal(peekPendingFlows({ serverId }).length, 1) + await approvePlan(plan.planId, actor, true) + const operation = await applyPlan(principal, plan.planId, randomUUID()), finished = await wait(operation.id) + assert.equal(finished.state, "applied"); assert.equal(finished.verification, "passed") + assert.equal(peekPendingFlows({ serverId }).length, 0, "actual existing main writer drained") + const result = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0].result + assert.deepEqual(result, { ok: true, buckets: 2, facts: 1, days: [day], jobId }) + assert.equal(Number((await pool.query("SELECT sum(bytes) AS n FROM flow_daily_facts WHERE server_id=$1", [serverId])).rows[0].n), 15) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=$1", [jobId])).rows[0].n, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM flow_write_receipts WHERE batch_id=$1 AND kind='rebuild'", [jobId])).rows[0].n, 1) + assert.ok((await pool.query("SELECT count(*)::int AS n FROM flow_write_receipts WHERE kind='ingest'")).rows[0].n >= 1) + const checkpoint = { events: await count("events"), receipts: await count("flow_write_receipts") } + await pool.query("UPDATE mcp_operations SET state='unknown',verification='inconclusive' WHERE id=$1", [operation.id]) + // New credentials on the same current connection can reconcile a historical + // publication. The old approved binding is not a live-authority fallback. + const rotated = await rotateConnectionToken(access.connection.id, actor) + const fresh = await authenticateMcpToken(rotated.token) + const reconciled = await invokeMcpTool(fresh, "mm_netflow_facts_rebuild_reconcile", { operationId: operation.id }) as { matchesDesiredState: boolean; matchesOriginalState: boolean; automaticRetry: boolean } + assert.equal(reconciled.matchesDesiredState, true); assert.equal(reconciled.matchesOriginalState, false); assert.equal(reconciled.automaticRetry, false) + assert.deepEqual({ events: await count("events"), receipts: await count("flow_write_receipts") }, checkpoint) + await pool.query("UPDATE mcp_plans SET operation_version=2 WHERE id=$1", [plan.planId]) + await assert.rejects(invokeMcpTool(fresh, "mm_netflow_facts_rebuild_reconcile", { operationId: operation.id })) + await pool.query("UPDATE mcp_plans SET operation_version=1 WHERE id=$1", [plan.planId]) + const other = await createMcpConnection({ ...configuration, name: "Other rebuild owner context" }, actor); connections.push(other.connection.id) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(other.token), "mm_netflow_facts_rebuild_reconcile", { operationId: operation.id })) + const narrow = await createMcpConnection({ ...configuration, name: "Narrow rebuild", scope: { ...configuration.scope, serverIds: ids.filter(id => id !== serverId) } }, actor); connections.push(narrow.connection.id) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(narrow.token), "mm_netflow_facts_rebuild_plan", {})) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protectionId, JSON.stringify({ kind: "database-partition", id: "flow_daily_facts/flow_daily_facts_d_20990101", protection: "deny" }), actor.id]) + const protectedBefore = await count("flow_write_receipts") + await assert.rejects(invokeMcpTool(fresh, "mm_netflow_facts_rebuild_plan", {})) + assert.equal(await count("flow_write_receipts"), protectedBefore) + assert.equal((await invokeMcpTool(fresh, "mm_netflow_facts_rebuild_reconcile", { operationId: operation.id }) as { matchesDesiredState: boolean }).matchesDesiredState, true, "late protection does not erase a historical receipt") + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) + const restBefore = await count("events") + const jwt = app.jwt.sign({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }, { iss: env.authIssuer, expiresIn: "10m" }) + const response = await app.inject({ method: "POST", url: "/api/traffic/flow/rebuild-facts", headers: { authorization: "Bearer " + jwt } }) + assert.equal(response.statusCode, 200, response.body) + assert.deepEqual(response.json(), { ok: true, buckets: 2, facts: 1, days: [day] }) + assert.equal(await count("events"), restBefore + 1, "shared service writes exactly one REST rebuild event") + assert.equal(getFlowWorkerHealth().alive, false); assert.deepEqual(getFlowListenerState(), listener); assert.equal(networkAttempts, 0) + console.log("MCP/REST flow rebuild: full schema, actual main strict drain, mandatory human approval, exact private receipt/public DTO, scope/protection/source-version/cross-connection fences, fresh-credential read-only reconciliation and JWT REST parity") + } finally { + globalThis.fetch = savedFetch + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) + for (const id of connections) await setConnectionStatus(id, "revoked", actor) + // The runner owns the database lifecycle. Retain operation/event/receipt + // evidence; it must dispose of the private DB after this acceptance. + } +} diff --git a/backend/src/modules/mcp/flow-facts-rebuild-actions.test.ts b/backend/src/modules/mcp/flow-facts-rebuild-actions.test.ts new file mode 100644 index 00000000..ee0a0218 --- /dev/null +++ b/backend/src/modules/mcp/flow-facts-rebuild-actions.test.ts @@ -0,0 +1,166 @@ +import assert from "node:assert/strict" +import { createDecipheriv, randomBytes, randomUUID } from "node:crypto" +import type { PoolClient } from "pg" +import { mcpConnectionPolicySchema } from "@mmapp/contracts/mcp" +import { createFlowFactsRebuildAdapter, flowFactsRebuildActionSchema, flowFactsRebuildReconcileSchema, publicFlowFactsRebuildReceipt } from "./flow-facts-rebuild-actions.js" +import { FlowFactsRebuildUnknown } from "../../services/traffic-flow-facts-rebuild-service.js" +import type { ActionContext } from "./action-types.js" +import type { ConnectionRow } from "./store.js" +import { fingerprint, redact, seal } from "./security.js" +import { publicMcpChangeEvidence } from "./routing-community-public.js" + +function fixture() { + const key = randomBytes(32), principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } + const connection: ConnectionRow = { id: principal.connectionId, name: "fixture", owner_id: randomUUID(), owner_permissions: ["mm:traffic:write"], generation: "fixture-generation", created_at: new Date(), updated_at: new Date(), last_used_at: null, owner_valid_until: new Date(Date.now() + 600000), owner_claims_issued_at: new Date(), policy: mcpConnectionPolicySchema.parse({ id: principal.connectionId, version: 1, status: "active", mode: "autonomous", expiresAt: principal.credentialExpiresAt, permissions: ["mm:traffic:write"], scope: { serverIds: [1, 2], operationIds: ["traffic.flow.facts.rebuild", "traffic.flow.facts.rebuild.reconcile"], allowGlobal: true } }) } + let catalog = [1, 2], configProof = "a".repeat(64), revoked = false, effects = 0, rebuildCalls = 0, reconcileCalls = 0, unknown = false, committed = true + const clients: unknown[] = [], queries: unknown[] = [] + const operationId = randomUUID() + let sourceRow: Record | undefined + const query = async (sql: unknown, args?: unknown[]) => { + queries.push(sql) + if (String(sql).includes("FROM mcp_operations")) return { rows: sourceRow && args?.[0] === operationId && args?.[1] === sourceRow.connection_id ? [sourceRow] : [] } + assert.equal(sql, "SELECT id FROM servers ORDER BY id LIMIT 1001"); return { rows: catalog.map(id => ({ id })) } + } + const client = { query } as unknown as Pick, owned = { query } as unknown as PoolClient + let saved: Record | null = null, duringWrite: (() => void) | undefined, afterWrite: (() => void) | undefined + const open = async (value: string) => { const bytes = Buffer.from(value, "base64"), cipher = createDecipheriv("aes-256-gcm", key, bytes.subarray(0, 12)); cipher.setAuthTag(bytes.subarray(12, 28)); return Buffer.concat([cipher.update(bytes.subarray(28)), cipher.final()]).toString("utf8") } + const adapter = createFlowFactsRebuildAdapter({ + client, seal: async value => seal(value, key), open, verifiedAccess: async () => {}, + connection: async (_principal, actual) => { clients.push(actual); if (revoked) throw Error("revoked"); return connection }, + configuration: async (ids, authorize, actual) => { assert.deepEqual(ids, [1, 2]); assert.ok(actual); await authorize(); return { proof: configProof, serverIds: [...ids], objects: [{ kind: "netflow-facts", id: "global" }, { kind: "database-partition", id: "flow_daily_facts/flow_daily_facts_d_20990101" }] } }, + drain: async (authorize, ids) => { assert.deepEqual(ids, [1, 2]); await authorize(); effects++ }, + rebuild: async (authority, input = {}) => { + rebuildCalls++; assert.equal(input.expectedConfigurationProof, configProof) + await authority.authorize(owned); duringWrite?.(); await authority.authorize(owned) + await authority.drainExisting(() => authority.authorize(owned), authority.serverIds) + effects++; saved = { ok: true, buckets: 2, facts: 1, days: ["2026-10-23"], jobId: input.jobId!, configurationProof: configProof, classifierProof: "b".repeat(64), resultProof: "c".repeat(64) } + if (unknown) throw new FlowFactsRebuildUnknown(input.jobId!, configProof) + afterWrite?.() + return saved as Awaited> + }, + reconcile: async (_job, _proof, authority) => { reconcileCalls++; await authority.authorize(owned); return { committed, result: committed ? saved : null } }, + }) + const context: ActionContext = { principal, connection: structuredClone(connection) } + const prepare = async () => { + const evidence = await adapter.prepare({}, context), step = evidence.steps[0] + assert.equal(step.kind, "service"); if (step.kind !== "service") throw Error("service expected") + sourceRow = { id: operationId, state: "unknown", connection_id: connection.id, source_action: "traffic.flow.facts.rebuild", source_version: 1, evidence: { sealed: seal(JSON.stringify(evidence), key) } } + return { evidence, input: step.input } + } + return { adapter, context, connection, prepare, open, owned, clients, queries, operationId, + source: (patch: Record) => { Object.assign(sourceRow!, patch) }, + stats: () => ({ effects, rebuildCalls, reconcileCalls }), + catalog: (ids: number[]) => { catalog = ids }, config: () => { configProof = "d".repeat(64) }, revoke: () => { revoked = true }, + setUnknown: () => { unknown = true }, setAbsent: () => { committed = false }, during: (fn: () => void) => { duringWrite = fn }, + after: (fn: () => void) => { afterWrite = fn }, + } +} + +assert.deepEqual(flowFactsRebuildActionSchema.parse({}), {}) +assert.equal(flowFactsRebuildReconcileSchema.safeParse({ jobId: randomUUID() }).success, false) +for (const value of [{ serverIds: undefined }, { jobId: randomUUID() }, { force: true }]) assert.equal(flowFactsRebuildActionSchema.safeParse(value).success, false) +{ + const f = fixture(), { evidence, input } = await f.prepare() + assert.equal(evidence.requiresHuman, true, "autonomous mode still requires human approval") + assert.equal(evidence.mutationCost, 3) + assert.deepEqual(evidence.objects.filter(object => object.kind === "server").map(object => object.serverId), [1, 2]) + assert.ok(evidence.objects.some(object => object.kind === "database-partition" && object.id.includes("20990101")), "future protected leaves remain in the footprint") + const visible = redact(publicMcpChangeEvidence("traffic.flow.facts.rebuild", evidence)) as { steps: { input: Record }[] } + assert.equal(visible.steps[0].input.bindingCiphertext, "[redacted]") + for (const key of ["principal", "policyProof", "ownerProof", "configurationProof", "classifierProof", "resultProof"]) assert.ok(!JSON.stringify(visible).includes(key)) + const result = await f.adapter.service.execute(input) + assert.deepEqual(Object.keys(result as object).sort(), ["buckets", "days", "facts", "jobId", "ok"]) + assert.equal(await f.adapter.service.verify!(input, result), true) + assert.equal(await f.adapter.service.verify!(input, { ...(result as object), configProof: "leak" }), false) + assert.ok(f.clients.includes(f.owned), "authority uses the supplied transaction client") + assert.equal((await f.adapter.readReconciliation({ operationId: f.operationId }, f.context)).matchesDesiredState, true) + assert.equal(f.stats().rebuildCalls, 1, "verify and reconcile never execute the rebuild") +} +for (const mutate of [(input: Record) => { input.jobId = randomUUID() }, (input: Record) => { input.serverIds = [1] }, (input: Record) => { input.bindingCiphertext = "tampered" }]) { + const f = fixture(), { input } = await f.prepare(), before = f.queries.length + mutate(input); await assert.rejects(f.adapter.service.execute(input)); assert.equal(f.queries.length, before); assert.equal(f.stats().effects, 0) +} +for (const change of [ + (f: ReturnType) => { f.connection.policy.scope.allowGlobal = false }, + (f: ReturnType) => { f.connection.policy.scope.serverIds = [1] }, + (f: ReturnType) => { f.connection.policy.scope.operationIds = [] }, + (f: ReturnType) => { f.connection.owner_permissions.push("mm:network:read") }, + (f: ReturnType) => { f.connection.policy.permissions = [] }, + (f: ReturnType) => { f.connection.policy.status = "paused" }, + (f: ReturnType) => { f.connection.policy.expiresAt = "2000-01-01T00:00:00.000Z" }, + (f: ReturnType) => { f.connection.owner_id = randomUUID() }, + (f: ReturnType) => { f.connection.generation = "changed" }, + (f: ReturnType) => f.catalog([1, 2, 3]), + (f: ReturnType) => f.config(), // Includes late protections in config-v3. + (f: ReturnType) => f.revoke(), +]) { + const f = fixture(), { input } = await f.prepare(); change(f) + await assert.rejects(f.adapter.service.current(input)); await assert.rejects(f.adapter.service.execute(input)); assert.equal(f.stats().effects, 0) +} +{ + const f = fixture(); f.context.principal.credentialExpiresAt = "2000-01-01T00:00:00.000Z" + await assert.rejects(f.prepare()); assert.equal(f.stats().effects, 0) +} +{ + const f = fixture(), { input } = await f.prepare() + f.during(() => f.connection.owner_permissions.push("mm:network:read")) + await assert.rejects(f.adapter.service.execute(input)); assert.ok(f.clients.includes(f.owned)); assert.equal(f.stats().effects, 0, "late authority changes stop before drain/publication") +} +{ + const f = fixture(), { input } = await f.prepare(); f.setUnknown() + await assert.rejects(f.adapter.service.execute(input), (error: unknown) => (error as { code?: string }).code === "MUTATION_OUTCOME_UNKNOWN") + const count = f.stats().rebuildCalls + assert.deepEqual(await f.adapter.service.reconcile!(input, async () => {}), { matchesDesiredState: true, matchesOriginalState: false }) + f.setAbsent() + assert.deepEqual(await f.adapter.service.reconcile!(input, async () => {}), { matchesDesiredState: false, matchesOriginalState: false }) + assert.equal(f.stats().rebuildCalls, count) + f.revoke(); await assert.rejects(f.adapter.readReconciliation({ operationId: f.operationId }, f.context)); assert.equal(f.stats().rebuildCalls, count) +} +for (const mutate of [ + (f: ReturnType) => f.source({ source_action: "traffic.flow.purge" }), + (f: ReturnType) => f.source({ source_version: 2 }), + (f: ReturnType) => f.source({ state: "running" }), + (f: ReturnType) => f.source({ state: "pending" }), + (f: ReturnType) => f.source({ connection_id: randomUUID() }), + (f: ReturnType) => f.source({ evidence: { sealed: "tampered" } }), + (f: ReturnType) => { f.connection.policy.scope.operationIds = ["traffic.flow.facts.rebuild.reconcile"]; f.context.connection = structuredClone(f.connection) }, + (f: ReturnType) => { f.connection.policy.scope.serverIds = [1]; f.context.connection = structuredClone(f.connection) }, + (f: ReturnType) => { f.connection.policy.scope.allowGlobal = false; f.context.connection = structuredClone(f.connection) }, + (f: ReturnType) => { f.connection.policy.status = "paused"; f.context.connection = structuredClone(f.connection) }, + (f: ReturnType) => { f.connection.owner_permissions = []; f.context.connection = structuredClone(f.connection) }, +]) { + const f = fixture(); await f.prepare(); mutate(f) + await assert.rejects(f.adapter.readReconciliation({ operationId: f.operationId }, f.context)) + assert.equal(f.stats().rebuildCalls, 0); assert.equal(f.stats().reconcileCalls, 0) +} +{ + const f = fixture(), { input } = await f.prepare(); await f.adapter.service.execute(input) + // Fresh current credentials may read historical publication even after the + // approved source config or the previous credential's deadline has changed. + f.config() + const realNow = Date.now + try { + Date.now = () => realNow() + 700000 + f.connection.policy.expiresAt = new Date(Date.now() + 900000).toISOString() + f.connection.owner_valid_until = new Date(Date.now() + 900000) + f.context.connection = structuredClone(f.connection) + f.context.principal = { ...f.context.principal, credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 900000).toISOString() } + assert.ok(Date.parse(JSON.parse(await f.open(String(input.bindingCiphertext))).principal.credentialExpiresAt) < Date.now()) + assert.equal((await f.adapter.readReconciliation({ operationId: f.operationId }, f.context)).matchesDesiredState, true) + } finally { Date.now = realNow } + let calls = 0 + await assert.rejects(f.adapter.service.reconcile!(input, async () => { if (++calls > 1) throw Error("fresh grant revoked") })) + assert.equal(f.stats().rebuildCalls, 1) +} +{ + const f = fixture(), { input } = await f.prepare(); f.after(() => f.revoke()) + await assert.rejects(f.adapter.service.execute(input), (error: unknown) => (error as { code?: string }).code === "MUTATION_OUTCOME_UNKNOWN") + assert.equal(f.stats().effects, 2, "publication was acknowledged before the final access check failed") + assert.equal(f.stats().rebuildCalls, 1) +} +{ + const value = { ok: true, buckets: 1, facts: 1, days: ["2026-10-23"], jobId: randomUUID(), configurationProof: "a".repeat(64), classifierProof: "b".repeat(64), resultProof: "c".repeat(64) } + for (const patch of [{ jobId: randomUUID() }, { configurationProof: "d".repeat(64) }, { days: ["2026-02-30"] }, { days: ["2026-10-23", "2026-10-23"] }, { facts: 2 }, { classifierProof: undefined }, { raw: "secret" }]) assert.throws(() => publicFlowFactsRebuildReceipt({ ...value, ...patch }, value.jobId, value.configurationProof)) + assert.equal(fingerprint(publicFlowFactsRebuildReceipt(value, value.jobId, value.configurationProof)), fingerprint({ ok: true, buckets: 1, facts: 1, days: ["2026-10-23"], jobId: value.jobId })) +} +console.log("MCP flow facts rebuild: strict schema, encrypted job/catalog binding, public redaction, owned-client authority, late revoke/protection/source fences, exact receipts and reconciliation without replay") diff --git a/backend/src/modules/mcp/flow-facts-rebuild-actions.ts b/backend/src/modules/mcp/flow-facts-rebuild-actions.ts new file mode 100644 index 00000000..19424606 --- /dev/null +++ b/backend/src/modules/mcp/flow-facts-rebuild-actions.ts @@ -0,0 +1,163 @@ +import { randomUUID } from "node:crypto" +import type { PoolClient } from "pg" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { drainExistingTrafficFlowWriters } from "../../services/traffic-flow-ingest.js" +import { readFactRebuildConfiguration, rebuildFlowFactsSafely, reconcileFlowFactsRebuild, FlowFactsRebuildUnknown } from "../../services/traffic-flow-facts-rebuild-service.js" +import { effectiveMcpConnection } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, McpError } from "./security.js" +import { registerMcpService, type McpServiceHandler } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const operation = "traffic.flow.facts.rebuild" +const reconcileOperation = "traffic.flow.facts.rebuild.reconcile" +export const flowFactsRebuildActionSchema = z.object({}).strict() +export const flowFactsRebuildReconcileSchema = z.object({ operationId: z.string().uuid() }).strict() +const idsSchema = z.array(z.number().int().positive().max(Number.MAX_SAFE_INTEGER)).max(1000).refine(ids => new Set(ids).size === ids.length && ids.every((id, index) => index === 0 || id > ids[index - 1])) +const proofSchema = z.string().regex(/^[a-f0-9]{64}$/) +const bindingSchema = z.object({ + principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: proofSchema, policyProof: proofSchema, + connectionGeneration: z.string().min(1), jobId: z.string().uuid(), serverIds: idsSchema, configurationProof: proofSchema, +}).strict() +const inputSchema = z.object({ jobId: z.string().uuid(), serverIds: idsSchema, bindingCiphertext: z.string().min(1).max(262144) }).strict() +type Binding = z.infer +const conflict = () => new McpError("STATE_CONFLICT", "Источник или допуск пересчёта NetFlow изменились; подготовьте новый план", 409) +const daySchema = z.string().regex(/^\d{4}-\d{2}-\d{2}$/).refine(day => Number.isFinite(Date.parse(day + "T00:00:00Z")) && new Date(day + "T00:00:00Z").toISOString().slice(0, 10) === day) +const receiptSchema = z.object({ ok: z.literal(true), buckets: z.number().int().min(0).max(1000000), facts: z.number().int().min(0), days: z.array(daySchema).max(1000), jobId: z.string().uuid(), configurationProof: proofSchema, classifierProof: proofSchema, resultProof: proofSchema }).strict() + +/** Durable receipts stay private. The model only receives these five fields. */ +export function publicFlowFactsRebuildReceipt(result: unknown, jobId: string, configurationProof: string) { + const value = receiptSchema.parse(result) + if (value.jobId !== jobId || value.configurationProof !== configurationProof || value.facts > value.buckets || new Set(value.days).size !== value.days.length || fingerprint([...value.days].sort()) !== fingerprint(value.days) || (value.buckets === 0 ? value.facts !== 0 || value.days.length !== 0 : value.days.length === 0)) throw conflict() + return { ok: true as const, buckets: value.buckets, facts: value.facts, days: [...value.days], jobId: value.jobId } +} + +const production = { + client: pool as Pick, + connection: effectiveMcpConnection, + configuration: readFactRebuildConfiguration, + rebuild: rebuildFlowFactsSafely, + reconcile: reconcileFlowFactsRebuild, + drain: drainExistingTrafficFlowWriters, + verifiedAccess: assertVerifiedConfigurationAccess, + seal: encryptSecret, open: decryptSecret, +} + +/** Injectable application boundaries permit model checks without a database or + * collector. Production always uses the shared transaction/drain implementation. */ +export function createFlowFactsRebuildAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const catalog = async (client: Pick = deps.client) => idsSchema.parse((await client.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => Number(row.id))) + const authorize = async (binding: Binding, client: Pick = deps.client, verified = false) => { + if (verified) await deps.verifiedAccess() + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now()) throw new McpError("UNAUTHORIZED", "Срок credential истёк", 401) + const connection = await deps.connection(binding.principal, client) + if (connection.policy.status !== "active" || Date.parse(connection.policy.expiresAt) <= Date.now() || connection.owner_valid_until.getTime() <= Date.now()) throw new McpError("UNAUTHORIZED", "Допуск пересчёта недействителен", 401) + if (connection.owner_id !== binding.ownerId || connection.policy.mode === "observer" || !connection.policy.scope.allowGlobal || !connection.policy.scope.operationIds.includes(operation) || binding.serverIds.some(id => !connection.policy.scope.serverIds.includes(id)) || !hasPermission(connection.policy.permissions, "mm:traffic:write") || !hasPermission(connection.owner_permissions, "mm:traffic:write")) throw new McpError("PERMISSION_DENIED", "Глобальный пересчёт NetFlow больше не разрешён", 403) + if (fingerprint(connection.policy) !== binding.policyProof || fingerprint(connection.owner_permissions) !== binding.ownerProof || connection.generation !== binding.connectionGeneration || fingerprint(await catalog(client)) !== fingerprint(binding.serverIds)) throw conflict() + } + const request = async (input: Record) => { + const value = inputSchema.parse(input), binding = bindingSchema.parse(JSON.parse(await deps.open(value.bindingCiphertext))) + if (value.jobId !== binding.jobId || fingerprint(value.serverIds) !== fingerprint(binding.serverIds)) throw conflict() + return binding + } + const source = async (binding: Binding, verified = false) => { + const check = () => authorize(binding, deps.client, verified) + const current = await deps.configuration(binding.serverIds, check, deps.client) + if (current.proof !== binding.configurationProof) throw conflict() + await check() + return current + } + const receipt = async (binding: Binding, externalAuthorize: () => Promise = async () => {}, freshAuthority = false) => { + const check = async (client?: PoolClient) => { await externalAuthorize(); if (!freshAuthority) await authorize(binding, client) } + if (!freshAuthority) await source(binding) + const saved = await deps.reconcile(binding.jobId, binding.configurationProof, { authorize: check }) + await externalAuthorize(); if (!freshAuthority) await source(binding) + return saved.committed ? publicFlowFactsRebuildReceipt(saved.result, binding.jobId, binding.configurationProof) : null + } + const service: McpServiceHandler = { + current: async input => { + const binding = await request(input); await source(binding) + return { jobId: binding.jobId, serverIds: binding.serverIds, configurationUnchanged: true } + }, + execute: async input => { + const binding = await request(input) + await source(binding, true) + let publicationConfirmed = false + try { + const result = await deps.rebuild({ serverIds: binding.serverIds, authorize: client => authorize(binding, client, true), drainExisting: deps.drain }, { jobId: binding.jobId, expectedConfigurationProof: binding.configurationProof }) + publicationConfirmed = true + await source(binding, true) + return publicFlowFactsRebuildReceipt(result, binding.jobId, binding.configurationProof) + } catch (error) { + if (publicationConfirmed || error instanceof FlowFactsRebuildUnknown || (error as { code?: string } | null)?.code === "FLOW_COMMIT_UNKNOWN") throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Результат пересчёта не подтверждён. Разрешена только сверка; повтор запрещён", 503) + throw error + } + }, + verify: async (input, result) => { + const saved = await receipt(await request(input)) + return saved !== null && fingerprint(saved) === fingerprint(result) + }, + reconcile: async (input, externalAuthorize) => { + await externalAuthorize() + const saved = await receipt(await request(input), externalAuthorize, true) + // An absent publication receipt cannot prove that earlier strict drains + // had no effects. Never turn absence into a retry or an inverse. + return { matchesDesiredState: saved !== null, matchesOriginalState: false } + }, + } + const prepare = async (input: Record, context: ActionContext): Promise => { + flowFactsRebuildActionSchema.parse(input); context.signal?.throwIfAborted() + const serverIds = await catalog(), jobId = randomUUID() + const binding: Binding = { principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), connectionGeneration: context.connection.generation, jobId, serverIds, configurationProof: "0".repeat(64) } + const check = async () => { context.signal?.throwIfAborted(); await authorize(binding) } + const initial = await deps.configuration(serverIds, check, deps.client) + binding.configurationProof = initial.proof + await source(binding); context.signal?.throwIfAborted() + const sealed = { jobId, serverIds, bindingCiphertext: await deps.seal(JSON.stringify(bindingSchema.parse(binding))) } + return { target: { scope: "global" }, objects: [...serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), ...initial.objects], snapshots: [], before: { jobId, serverIds, configurationUnchanged: true }, requiresHuman: true, mutationCost: 3, + steps: [{ kind: "service", service: operation, input: sealed }], warnings: [ + "Глобальный пересчёт фактов из всех сохранённых flow buckets требует прав на полный текущий каталог серверов. Существующие факты затронутых дней будут заменены; обратного шага нет.", + "Используются текущая топология и сохранённые данные классификаторов; внешнее enrichment не запускается. Изменение настроек, источников или защит после подтверждения останавливает пересчёт.", + "Существующий collector продолжает обычный приём. Перед публикацией выполняется строгий drain существующих main/worker writers без запуска, остановки или перезапуска collector.", + "Бюджет — до трёх транзакций: main drain, worker drain и атомарная публикация фактов с квитанцией. Занятая транзакция или неизвестный COMMIT не повторяются автоматически. При unknown разрешена только read-only сверка этого jobId.", + ] } + } + const readReconciliation = async (input: Record, context: ActionContext) => { + const { operationId } = flowFactsRebuildReconcileSchema.parse(input) + const rows = (await deps.client.query(`SELECT o.id,o.state,o.connection_id,p.operation_id AS source_action,p.operation_version AS source_version,p.evidence + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.connection_id=$2 LIMIT 2`, [operationId, context.connection.id])).rows + if (rows.length !== 1) throw new McpError("NOT_FOUND", "Исходная операция пересчёта не найдена", 404) + const row = rows[0] + if (row.source_action !== operation || row.source_version !== 1 || row.connection_id !== context.connection.id) throw conflict() + if (!["applied", "partial", "unknown", "failed", "cancelled", "rejected", "interrupted"].includes(row.state)) throw new McpError("STATE_CONFLICT", "Исходная операция ещё не завершена; повтор запрещён", 409) + if (!row.evidence || typeof row.evidence.sealed !== "string") throw conflict() + const evidence = JSON.parse(await deps.open(row.evidence.sealed)) as ChangeEvidence + if (evidence.target?.scope !== "global" || evidence.steps?.length !== 1 || evidence.steps[0].kind !== "service" || evidence.steps[0].service !== operation) throw conflict() + const binding = await request(evidence.steps[0].input) + if (fingerprint(evidence.before) !== fingerprint({ jobId: binding.jobId, serverIds: binding.serverIds, configurationUnchanged: true })) throw conflict() + const freshAuthorize = async () => { + context.signal?.throwIfAborted() + if (Date.parse(context.principal.credentialExpiresAt) <= Date.now()) throw new McpError("UNAUTHORIZED", "Срок credential истёк", 401) + const current = await deps.connection(context.principal, deps.client) + if (current.policy.status !== "active" || Date.parse(current.policy.expiresAt) <= Date.now() || current.owner_valid_until.getTime() <= Date.now()) throw new McpError("UNAUTHORIZED", "Допуск сверки недействителен", 401) + if (binding.principal.connectionId !== current.id || current.id !== context.connection.id || current.owner_id !== binding.ownerId || current.policy.mode === "observer" || !current.policy.scope.allowGlobal || ![operation, reconcileOperation].every(id => current.policy.scope.operationIds.includes(id)) || binding.serverIds.some(id => !current.policy.scope.serverIds.includes(id)) || !hasPermission(current.owner_permissions, "mm:traffic:write") || !hasPermission(current.policy.permissions, "mm:traffic:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска к исходному глобальному пересчёту", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions) || current.generation !== context.connection.generation) throw conflict() + } + await freshAuthorize() + const saved = await receipt(binding, freshAuthorize, true) + return { operationId, jobId: binding.jobId, state: String(row.state), matchesDesiredState: saved !== null, matchesOriginalState: false, automaticRetry: false } + } + return { prepare, service, readReconciliation } +} + +export function registerFlowFactsRebuildActions(register: typeof add) { + const adapter = createFlowFactsRebuildAdapter() + registerMcpService(operation, adapter.service) + register(operation, "mm_netflow_facts_rebuild_plan", "Пересчитать факты NetFlow из сохранённых сессий", "traffic", "mm:traffic:write", "write", "global", flowFactsRebuildActionSchema, { version: 1, prepare: adapter.prepare }) + register(reconcileOperation, "mm_netflow_facts_rebuild_reconcile", "Сверить квитанцию пересчёта NetFlow без повторной записи", "traffic", "mm:traffic:write", "read", "global", flowFactsRebuildReconcileSchema, { version: 1, read: adapter.readReconciliation }) +} diff --git a/backend/src/modules/mcp/flow-facts-rebuild-http.test.ts b/backend/src/modules/mcp/flow-facts-rebuild-http.test.ts new file mode 100644 index 00000000..e77bba6e --- /dev/null +++ b/backend/src/modules/mcp/flow-facts-rebuild-http.test.ts @@ -0,0 +1,30 @@ +import assert from "node:assert/strict" + +// This runner is only admitted by the dedicated disposable-cluster harness. +const endpoint = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") +assert.equal(process.env.MMAPP_OWN_MCP_FLOW_REBUILD, "true") +assert.equal(endpoint.hostname, "127.0.0.1") +assert.equal(endpoint.pathname, "/postgres") +assert.match(process.env.MCP_STATE_DIR ?? "", /mm-mcp-test-/) +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-flow-rebuild-acceptance-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const { buildApp } = await import("../../index.js") +const actor = { id: "flow-rebuild-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:traffic:admin", "mm:network:admin"], expiresAt: Date.now() + 3600000 } +let app: Awaited> | undefined +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await pool.query("INSERT INTO geoip_settings(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false }) + await app.ready() + await (await import("./flow-facts-rebuild-acceptance.js")).acceptMcpFlowFactsRebuild(actor, app) +} finally { + await app?.close() + await closePool() +} diff --git a/backend/src/modules/mcp/flow-host-keys-actions.test.ts b/backend/src/modules/mcp/flow-host-keys-actions.test.ts new file mode 100644 index 00000000..5fdc9a2a --- /dev/null +++ b/backend/src/modules/mcp/flow-host-keys-actions.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict" +import { randomUUID, randomBytes, createDecipheriv, createHmac } from "node:crypto" +import { mcpConnectionPolicySchema } from "@mmapp/contracts/mcp" +import { createFlowHostKeysAdapter, flowHostKeysActionSchema } from "./flow-host-keys-actions.js" +import { fingerprint, seal, redact } from "./security.js" +import type { ConnectionRow } from "./store.js" +import type { FlowSettingsConfiguration } from "../../services/traffic-flow-settings-execution.js" +import { publicMcpChangeEvidence } from "./routing-community-public.js" + +function fixture(complete = false) { + const key = randomBytes(32), principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } + const connection: ConnectionRow = { id: principal.connectionId, name: "fixture", owner_id: "owner", owner_permissions: ["mm:traffic:write"], generation: "generation", created_at: new Date(), updated_at: new Date(), last_used_at: null, owner_valid_until: new Date(Date.now() + 600000), owner_claims_issued_at: new Date(), policy: mcpConnectionPolicySchema.parse({ id: principal.connectionId, version: 1, status: "active", mode: "autonomous", expiresAt: principal.credentialExpiresAt, permissions: ["mm:traffic:write"], scope: { serverIds: [1, 2], operationIds: ["traffic.flow.settings.generate-keys"], allowGlobal: true } }) } + const configuration: FlowSettingsConfiguration = { id: 1, hostPublicKey: complete ? "public-existing" : "", hostPrivateKey: complete ? "PRIVATE-EXISTING" : "", future: "opaque", peersJson: [{ privateOpaque: "retained" }] } + let catalog = [1, 2], protections: Record[] = [], writes = 0, verified: Record[] = [] + let beforeWrite = () => {}, afterWrite = () => {} + const proof = async (value: unknown) => createHmac("sha256", key).update(fingerprint(value)).digest("hex") + const adapter = createFlowHostKeysAdapter({ + proof, seal: async value => seal(value, key), + open: async value => { const data = Buffer.from(value, "base64"), c = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)); c.setAuthTag(data.subarray(12, 28)); return Buffer.concat([c.update(data.subarray(28)), c.final()]).toString() }, + connection: async () => connection, verifiedAccess: async () => {}, + query: async sql => sql.includes("mcp_operation_steps") ? verified : sql.includes("mcp_protections") ? protections : catalog.map(id => ({ id })), + read: async authorize => { await authorize?.(); return structuredClone(configuration) }, + generate: async control => { + await control?.authorize?.(); beforeWrite(); await control?.authorize?.() + assert.deepEqual(control?.expected?.before, configuration) + const created = !configuration.hostPublicKey || !configuration.hostPrivateKey + if (created) { configuration.hostPublicKey = "public-created"; configuration.hostPrivateKey = "PRIVATE-CREATED"; writes++ } + afterWrite(); await control?.authorize?.() + return { ok: true, receiptId: control!.receiptId!, created, publicKey: configuration.hostPublicKey, afterProof: await proof(configuration), listenerRestarted: false, privateMaterialExported: false, automaticReplay: false } + }, + }) + return { adapter, connection, principal, prepare: () => adapter.prepare({}, { connection: structuredClone(connection), principal }), + get configuration() { return configuration }, get writes() { return writes }, + catalog: () => { catalog = [1] }, protect: () => { protections = [{ object_ref: { kind: "netflow-settings", id: "1", protection: "deny" }, revision: "2" }] }, + before: (fn: () => void) => { beforeWrite = fn }, after: (fn: () => void) => { afterWrite = fn }, + verified: (value: Record) => { verified = [value] } } +} +const input = (evidence: Awaited["prepare"]>>) => { assert.equal(evidence.steps[0].kind, "service"); if (evidence.steps[0].kind !== "service") throw Error(); return evidence.steps[0].input } +for (const raw of [{ force: true }, { serverId: 1 }, { includePrivateKey: true }, { receiptId: randomUUID() }]) assert.equal(flowHostKeysActionSchema.safeParse(raw).success, false) +for (const complete of [false, true]) { + const f = fixture(complete), evidence = await f.prepare(), request = input(evidence) + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 1) + assert.deepEqual(evidence.objects.filter(ref => ref.kind === "server").map(ref => ref.serverId), [1, 2]) + const visible = JSON.stringify(redact(publicMcpChangeEvidence("traffic.flow.settings.generate-keys", evidence))) + for (const secret of ["PRIVATE-EXISTING", "privateOpaque", "beforeProof", "ownerProof", "principal"]) assert.ok(!visible.includes(secret)) + const receipt = await f.adapter.service.execute(request) + assert.equal(f.writes, complete ? 0 : 1) + assert.equal(await f.adapter.service.verify!(request, receipt), true) + assert.equal(await f.adapter.service.verify!(request, { ...receipt as object, privateMaterialExported: true }), false) + assert.equal((await f.adapter.service.reconcile!(request, async () => {})).matchesDesiredState, false, "matching row without VERIFIED is insufficient") + f.verified({ connection_id: f.principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: "traffic.flow.settings.generate-keys", input: request }), result: receipt }) + assert.equal((await f.adapter.service.reconcile!(request, async () => {})).matchesDesiredState, true) + assert.ok(!JSON.stringify(receipt).includes("PRIVATE")) +} +for (const change of [ + (f: ReturnType) => { f.connection.policy.status = "paused" }, + (f: ReturnType) => { f.connection.policy.scope.allowGlobal = false }, + (f: ReturnType) => { f.connection.policy.scope.serverIds = [1] }, + (f: ReturnType) => { f.connection.owner_permissions = [] }, + (f: ReturnType) => { f.connection.generation = "changed" }, + (f: ReturnType) => { f.configuration.future = "drift" }, + (f: ReturnType) => f.catalog(), (f: ReturnType) => f.protect(), +]) { + const f = fixture(), request = input(await f.prepare()); change(f) + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 0) +} +{ + const f = fixture(), request = input(await f.prepare()); f.before(() => f.protect()) + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 0) +} +{ + const f = fixture(), request = input(await f.prepare()); f.after(() => { f.connection.policy.status = "paused" }) + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 1) +} +{ + const f = fixture(), request = input(await f.prepare()); request.receiptId = randomUUID() + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 0) +} +console.log("NetFlow host key adapter: human/global/full catalog, private binding, source/protection/revocation, public-only receipt, no VERIFIED adoption and no listener; injected dependencies only") diff --git a/backend/src/modules/mcp/flow-host-keys-actions.ts b/backend/src/modules/mcp/flow-host-keys-actions.ts new file mode 100644 index 00000000..e7941047 --- /dev/null +++ b/backend/src/modules/mcp/flow-host-keys-actions.ts @@ -0,0 +1,106 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { generateFlowHostKeys, readFlowHostKeysConfiguration, flowSettingsPrivateProof } from "../../services/traffic-flow-host-keys-service.js" +import { effectiveMcpConnection } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, redact, McpError } from "./security.js" +import { registerMcpService, type McpServiceHandler } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { FlowSettingsPlan } from "../../services/traffic-flow-settings-execution.js" +import type { add } from "./actions.js" + +const operation = "traffic.flow.settings.generate-keys" +export const flowHostKeysActionSchema = z.object({}).strict() +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const ids = z.array(z.number().int().positive().safe()).max(1000).refine(value => value.every((id, index) => index === 0 || value[index - 1] < id)) +const bindingSchema = z.object({ + operation: z.literal(operation), version: z.literal(1), receiptId: z.string().uuid(), + principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: hash, policyProof: hash, generation: z.string().min(1), serverIds: ids, + beforeProof: hash, protectionProof: hash, + before: z.record(z.string(), z.unknown()).refine(value => typeof value.hostPrivateKey === "string" && typeof value.hostPublicKey === "string"), +}).strict() +type Binding = z.infer +const inputSchema = z.object({ receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(524288) }).strict() +const receiptSchema = z.object({ ok: z.literal(true), receiptId: z.string().uuid(), created: z.boolean(), publicKey: z.string().min(1).max(100), afterProof: hash, + privateMaterialExported: z.literal(false), listenerRestarted: z.literal(false), automaticReplay: z.literal(false) }).strict() +const conflict = () => new McpError("STATE_CONFLICT", "Конфигурация, защиты или допуск генерации ключей NetFlow изменились", 409) +const production = { + connection: effectiveMcpConnection, read: readFlowHostKeysConfiguration, generate: generateFlowHostKeys, + seal: encryptSecret, open: decryptSecret, proof: flowSettingsPrivateProof, verifiedAccess: assertVerifiedConfigurationAccess, + query: async (sql: string, values: unknown[] = []) => (await pool.query(sql, values)).rows as Record[], +} +export function createFlowHostKeysAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const catalog = async () => ids.parse((await deps.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).map(row => Number(row.id))) + const protections = async (serverIds: number[]) => { + const rows = await deps.query(`SELECT object_ref,xmin::text AS revision 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' IN ('netflow-settings','netflow-host') AND object_ref->>'id'='1') ORDER BY id LIMIT 1001`, [serverIds.map(String)]) + if (rows.length > 1000) throw conflict() + return { proof: await deps.proof(rows), objects: rows.map(row => row.object_ref as ChangeEvidence["objects"][number]) } + } + const authorize = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + const current = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || Date.parse(current.policy.expiresAt) <= Date.now() || current.owner_valid_until.getTime() <= Date.now() || !current.policy.scope.allowGlobal || !current.policy.scope.operationIds.includes(operation) || binding.serverIds.some(id => !current.policy.scope.serverIds.includes(id)) || !hasPermission(current.owner_permissions, "mm:traffic:write") || !hasPermission(current.policy.permissions, "mm:traffic:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска генерации ключей NetFlow", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof || fingerprint(await catalog()) !== fingerprint(binding.serverIds) || (await protections(binding.serverIds)).proof !== binding.protectionProof) throw conflict() + signal?.throwIfAborted() + } + const request = async (raw: Record) => { + const input = inputSchema.parse(raw), binding = bindingSchema.parse(JSON.parse(await deps.open(input.bindingCiphertext))) + if (binding.receiptId !== input.receiptId || await deps.proof(binding.before) !== binding.beforeProof) throw conflict() + return binding + } + const current = async (binding: Binding, external?: () => Promise) => { + const check = () => authorize(binding, undefined, external) + await check(); const configuration = await deps.read(check); await check() + return { configuration, proof: await deps.proof(configuration) } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + flowHostKeysActionSchema.parse(raw); context.signal?.throwIfAborted() + const serverIds = await catalog(), protection = await protections(serverIds) + const binding: Binding = { operation, version: 1, receiptId: randomUUID(), principal: context.principal, + ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, + serverIds, protectionProof: protection.proof, beforeProof: "0".repeat(64), before: {} } + const check = () => authorize(binding, context.signal) + await check(); binding.before = await deps.read(check); binding.beforeProof = await deps.proof(binding.before); await check() + return { target: { scope: "global" }, snapshots: [], requiresHuman: true, mutationCost: 1, + objects: [{ kind: "netflow-settings", id: "1" }, { kind: "netflow-host", id: "1" }, ...serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), ...protection.objects], + before: { proof: binding.beforeProof }, steps: [{ kind: "service", service: operation, input: { receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(bindingSchema.parse(binding))) } }], + warnings: ["Создаётся ключевая пара хоста NetFlow только при отсутствии полной пары. Уже сохранённая полная пара не заменяется. При неполной паре заменяются оба поля, как в REST.", "Изменение глобальное и требует текущего допуска ко всем серверам. Модель получает публичный ключ и квитанцию; приватный ключ не экспортируется.", "Collector и host-agent не запускаются и не перезапускаются; RouterOS, SSH и файлы не затрагиваются. Квитанция не подтверждает готовность туннеля.", "При потере результата повтор этой попытки запрещён. Совпадение конфигурации без сохранённого VERIFIED не подтверждает завершение операции."] } + } + const matches = (value: unknown, binding: Binding, proof: string) => { + const receipt = receiptSchema.safeParse(value) + const completeBefore = Boolean(binding.before.hostPublicKey && binding.before.hostPrivateKey) + return receipt.success && receipt.data.receiptId === binding.receiptId && receipt.data.afterProof === proof && receipt.data.created === !completeBefore && (!completeBefore || receipt.data.publicKey === binding.before.hostPublicKey) + } + const service: McpServiceHandler = { + current: async input => ({ proof: (await current(await request(input))).proof }), + execute: async input => { + const binding = await request(input), before = await current(binding) + if (before.proof !== binding.beforeProof) throw conflict() + const check = async () => { await deps.verifiedAccess(); await authorize(binding) } + const expected: FlowSettingsPlan = { command: { kind: "ensure-keys" }, before: before.configuration } + const receipt = await deps.generate({ receiptId: binding.receiptId, expected, authorize: check }) + const after = await current(binding) + if (!matches(receipt, binding, after.proof) || receipt.publicKey !== after.configuration.hostPublicKey) throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Результат создания ключей не подтверждён; повтор запрещён", 503) + return receipt + }, + verify: async (input, result) => { const binding = await request(input); return matches(result, binding, (await current(binding)).proof) }, + reconcile: async (input, external) => { + const binding = await request(input), latest = await current(binding, external) + const rows = await deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId]) + const row = rows[0] + const verified = rows.length === 1 && row.connection_id === binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && matches(row.result, binding, latest.proof) + if ((await current(binding, external)).proof !== latest.proof) throw conflict() + return { matchesDesiredState: verified, matchesOriginalState: latest.proof === binding.beforeProof } + }, + } + return { prepare, service } +} +export function registerFlowHostKeysActions(register: typeof add) { + const adapter = createFlowHostKeysAdapter() + registerMcpService(operation, adapter.service) + register(operation, "mm_netflow_host_keys_generate_plan", "Создать отсутствующие ключи хоста NetFlow", "traffic", "mm:traffic:write", "write", "global", flowHostKeysActionSchema, { prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/flow-host-keys-http.test.ts b/backend/src/modules/mcp/flow-host-keys-http.test.ts new file mode 100644 index 00000000..0f73e9a5 --- /dev/null +++ b/backend/src/modules/mcp/flow-host-keys-http.test.ts @@ -0,0 +1,150 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import type { Pool } from "pg" + +const endpoint = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") +assert.equal(process.env.MMAPP_OWN_FLOW_HOST_KEYS, "true") +assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.pathname, "/postgres") +assert.match(process.env.MCP_STATE_DIR ?? "", /mm-mcp-test-/) +process.env.AUTH_REQUIRED = "true"; process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { createFlowSettingsStore } = await import("../../services/traffic-flow-settings-store.js") +const { generateFlowHostKeys, flowSettingsPrivateProof } = await import("../../services/traffic-flow-host-keys-service.js") +const { invalidateTrafficFlowSettingsCache } = await import("../../services/traffic-flow-settings.js") +const { withFlowConfigurationLock } = await import("../../services/traffic-flow-host-agent.js") +const { withApplicationMutationLock } = await import("../../services/config-mutation-lock.js") +const { getFlowListenerState, getFlowWorkerHealth } = await import("../../services/traffic-flow-ingest.js") +const { buildApp } = await import("../../index.js") +const { env } = await import("../../config.js") +const { createMcpConnection, authenticateMcpToken, updateMcpSettings, transaction } = await import("./store.js") +const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") +const { decryptSecret } = await import("./security.js") +const actor = { id: "flow-keys-fixture-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:traffic:admin"], expiresAt: Date.now() + 3600000 } +let app: Awaited> | undefined +let network = 0 +const originalFetch = globalThis.fetch +globalThis.fetch = (async () => { network++; throw Error("Isolated key generation prohibits network") }) as typeof fetch +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await pool.query("ALTER TABLE traffic_flow_settings ADD COLUMN fixture_future jsonb NOT NULL DEFAULT '{\"opaque\":true}'::jsonb") + const store = createFlowSettingsStore(pool, async () => {}, invalidateTrafficFlowSettingsCache) + { + const before = await store.read(), receiptId = randomUUID(), proof = await flowSettingsPrivateProof(before) + await store.claim(receiptId, "configuration", proof) + await assert.rejects(store.claim(receiptId, "configuration", proof)) + await pool.query("UPDATE traffic_flow_settings SET packets_received=111,last_error='telemetry',updated_at=now() WHERE id=1") + assert.deepEqual(await store.read(), before) + const desired = { ...before, hostPublicKey: "fixture-public", hostPrivateKey: "fixture-private" } + assert.equal(await store.compareAndSet(receiptId, proof, before, desired), true) + assert.deepEqual(await store.read(), desired) + assert.equal((await pool.query("SELECT packets_received FROM traffic_flow_settings WHERE id=1")).rows[0].packets_received, 111) + await assert.rejects(store.compareAndSet(receiptId, proof, before, desired)) + } + { + const before = await store.read(), id = randomUUID(), proof = await flowSettingsPrivateProof(before) + await store.claim(id, "configuration", proof) + await pool.query("UPDATE traffic_flow_settings SET fixture_future='{\"opaque\":false}' WHERE id=1") + assert.equal(await store.compareAndSet(id, proof, before, { ...before, hostPublicKey: "denied" }), false) + assert.notEqual((await store.read()).hostPublicKey, "denied") + } + { + const before = await store.read(), id = randomUUID(), proof = await flowSettingsPrivateProof(before) + await store.claim(id, "configuration", proof) + let commitAttempts = 0, quarantined = false + const lost = createFlowSettingsStore({ query: pool.query.bind(pool), connect: async () => { + const client = await pool.connect() + return { query: async (sql: string, values?: unknown[]) => { + const result = await client.query(sql, values) + if (sql === "COMMIT") { commitAttempts++; throw Error("synthetic lost commit ACK") } + return result + }, release: (unsafe?: boolean) => { quarantined = Boolean(unsafe); client.release(unsafe) } } + } } as unknown as Pick, async () => {}, invalidateTrafficFlowSettingsCache) + await assert.rejects(lost.compareAndSet(id, proof, before, { ...before, hostPublicKey: "lost-ack-public" })) + assert.equal(commitAttempts, 1); assert.equal(quarantined, true) + assert.equal((await store.read()).hostPublicKey, "lost-ack-public") + await assert.rejects(store.claim(id, "configuration", proof)) + } + { + const before = await store.read(), proof = await flowSettingsPrivateProof(before), attempts = [randomUUID(), randomUUID()] + await Promise.all(attempts.map(id => store.claim(id, "configuration", proof))) + const raced = await Promise.all(attempts.map((id, index) => store.compareAndSet(id, proof, before, { ...before, publicEndpoint: `fixture-${index}` }))) + assert.equal(raced.filter(Boolean).length, 1, "full CAS admits only one concurrent different update") + } + { + const before = await store.read(), id = randomUUID(), proof = await flowSettingsPrivateProof(before) + await store.claim(id, "configuration", proof) + let revoked = false + const denied = createFlowSettingsStore({ query: pool.query.bind(pool), connect: async () => { + const client = await pool.connect() + return { query: async (sql: string, values?: unknown[]) => { + const result = await client.query(sql, values) + if (sql.startsWith("UPDATE traffic_flow_settings")) revoked = true + return result + }, release: (unsafe?: boolean) => client.release(unsafe) } + } } as unknown as Pick, async () => { if (revoked) throw Error("synthetic revoke after UPDATE") }, invalidateTrafficFlowSettingsCache) + await assert.rejects(denied.compareAndSet(id, proof, before, { ...before, hostPublicKey: "must-rollback" })) + assert.deepEqual(await store.read(), before) + assert.equal((await pool.query("SELECT state FROM flow_settings_intents WHERE receipt_id=$1", [id])).rows[0].state, "claimed") + await assert.rejects(store.claim(id, "configuration", proof)) + } + const listener = getFlowListenerState() + for (const [publicKey, privateKey] of [["", ""], ["", "synthetic-existing-private"], ["synthetic-existing-public", ""], ["", ""]]) { + await pool.query("UPDATE traffic_flow_settings SET host_public_key=$1,host_private_key=$2 WHERE id=1", [publicKey, privateKey]); invalidateTrafficFlowSettingsCache() + const before = await store.read(), first = await generateFlowHostKeys() + assert.equal(first.created, true); assert.match(first.publicKey, /^[A-Za-z0-9+/]{43}=$/) + const after = await store.read() + assert.deepEqual({ ...after, hostPublicKey: before.hostPublicKey, hostPrivateKey: before.hostPrivateKey }, before) + const second = await withApplicationMutationLock(() => withFlowConfigurationLock(() => generateFlowHostKeys())) + assert.equal(second.created, false); assert.equal(second.publicKey, first.publicKey) + assert.deepEqual(await store.read(), after) + assert.ok(!JSON.stringify(first).includes(after.hostPrivateKey)) + } + const vrfTickets = await (await import("../../services/vrf-production-ticket-store-sql.test.js")).runVrfProductionTicketSqlAcceptance({ sql: pool, transaction, claimConnections: { connect: () => pool.connect() } }) + assert.equal(vrfTickets.passed, true) + console.log(`VRF production tickets SQL: ${vrfTickets.cases} actual one-use/owner/source/expiry/concurrency/unknown claims; no native execution grant`) + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false }); await app.ready() + const access = await createMcpConnection({ name: "host keys acceptance", mode: "autonomous", permissions: ["mm:traffic:write"], expiresAt: new Date(Date.now() + 600000).toISOString(), scope: { allowGlobal: true, serverIds: [], operationIds: ["traffic.flow.settings.generate-keys"] }, limits: { callsPerMinute: 240, mutationsPerHour: 1000 } }, actor) + const principal = await authenticateMcpToken(access.token) + const wait = async (id: string) => { + for (const deadline = Date.now() + 45000; Date.now() < deadline;) { + const row = (await pool.query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(20) + } + throw Error("Operation still live, do not replay") + } + await pool.query("UPDATE traffic_flow_settings SET host_public_key='',host_private_key='' WHERE id=1"); invalidateTrafficFlowSettingsCache() + const planned = await invokeMcpTool(principal, "mm_netflow_host_keys_generate_plan", {}) as { planId: string; state: string; changes: unknown } + assert.equal(planned.state, "awaiting-approval") + const evidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [planned.planId])).rows[0].evidence.sealed)) + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 1) + await assert.rejects(applyPlan(principal, planned.planId, randomUUID())) + await approvePlan(planned.planId, actor, true) + const operation = await applyPlan(principal, planned.planId, randomUUID()), finished = await wait(operation.id) + assert.equal(finished.state, "applied", JSON.stringify(finished)); assert.equal(finished.verification, "passed") + const result = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].result + const current = await store.read() + assert.equal(result.publicKey, current.hostPublicKey); assert.equal(result.created, true) + assert.ok(!JSON.stringify({ planned, result }).includes(current.hostPrivateKey)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM flow_settings_intents WHERE receipt_id=$1", [result.receiptId])).rows[0].n, 1) + await assert.rejects(generateFlowHostKeys({ receiptId: result.receiptId })) + const jwt = app.jwt.sign({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }, { iss: env.authIssuer, expiresIn: "10m" }) + const response = await app.inject({ method: "POST", url: "/api/traffic/flow/settings/generate-keys", headers: { authorization: "Bearer " + jwt } }) + assert.equal(response.statusCode, 200, response.body) + assert.equal(response.json().created, false); assert.equal(response.json().publicKey, result.publicKey) + assert.ok(!response.body.includes(current.hostPrivateKey)) + assert.equal((await app.inject({ method: "POST", url: "/api/traffic/flow/settings/generate-keys" })).statusCode, 401) + const protectedId = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [protectedId, { kind: "netflow-settings", id: "1", protection: "deny" }, actor.id]) + const denied = await app.inject({ method: "POST", url: "/api/traffic/flow/settings/generate-keys", headers: { authorization: "Bearer " + jwt } }) + assert.equal(denied.statusCode, 409, denied.body) + await assert.rejects(invokeMcpTool(principal, "mm_netflow_host_keys_generate_plan", {})) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectedId]) + assert.equal(network, 0); assert.equal(getFlowWorkerHealth().alive, false); assert.deepEqual(getFlowListenerState(), listener) + await (await import("./flow-settings-acceptance.js")).acceptMcpFlowSettingsUpdate(actor, app) + console.log("NetFlow keys SQL/HTTP: complete config CAS incl future columns; telemetry separation; unique claims; lost COMMIT no replay; real generation, reentrant shared locks; mandatory MCP human and private binding; JWT REST parity/protection; zero collector/network") +} finally { globalThis.fetch = originalFetch; await app?.close(); await closePool() } diff --git a/backend/src/modules/mcp/flow-live-actions.ts b/backend/src/modules/mcp/flow-live-actions.ts new file mode 100644 index 00000000..ad4688c8 --- /dev/null +++ b/backend/src/modules/mcp/flow-live-actions.ts @@ -0,0 +1,74 @@ +import { assertBoundedFlowSource as assertTrafficHistorySource } from "./flow-source-scope.js" +import { withCurrentFlowReadQuality } from "./flow-read-quality.js" +import { setTimeout as delay } from "node:timers/promises" +import { z } from "zod" +import { flowAnalyticsDtoSchema } from "@mmapp/contracts/traffic-flow" +import { safeBuildLiveFlowSample, LIVE_ANALYTICS_MINUTES } from "../../services/traffic-flow-analytics.js" +import { FLOW_LIVE_TICK_MS, tryAcquireFlowLiveSlot, releaseFlowLiveSlot } from "../../services/traffic-flow-live-slots.js" +import { getBoundedFlowServerRowById as getServerRowById } from "../../services/traffic-flow-server-source.js" +import { assertTrafficHistoryAccess } from "./traffic-history-actions.js" +import { McpError } from "./security.js" +import type { add } from "./actions.js" + +export function registerFlowLiveActions(register: typeof add) { + register("traffic.flow.live.read", "mm_netflow_live_observe", "NetFlow: ограниченное наблюдение live-потока", "traffic", "mm:traffic:read", "read", "servers", z.object({ + serverId: z.number().int().positive().safe(), userId: z.string().trim().min(1).max(100).optional(), + interface: z.string().trim().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f]/.test(value) && value !== "__all__").optional(), + dedup: z.boolean().default(true), excludeMesh: z.boolean().default(true), excludeOverlay: z.boolean().default(true), + samples: z.number().int().min(1).max(3).default(1), + offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(100).default(50), + }).strict(), { + read: async (input, context) => { + await assertTrafficHistoryAccess(context) + const serverId = Number(input.serverId) + // Keep this check local too: direct action invocations must not read a foreign source. + if (!context.connection.policy.scope.serverIds.includes(serverId)) throw new McpError("SCOPE_DENIED", "Сервер вне разрешённой области", 403) + const server = await getServerRowById(serverId) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const authorize = () => assertTrafficHistorySource(context, server) + await authorize() + if (!tryAcquireFlowLiveSlot()) throw new McpError("RATE_LIMITED", "Слишком много live-подписок", 429) + const readStartedAt = new Date().toISOString() + try { + const events = [] + for (let index = 0; index < Number(input.samples); index++) { + if (index) await delay(FLOW_LIVE_TICK_MS, undefined, { signal: context.signal }) + await authorize() + const sampleStartedAt = new Date().toISOString() + const event = await safeBuildLiveFlowSample({ serverId, 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 }) + await authorize() + // The REST formatter includes raw source error messages. Never publish these over MCP. + if (event.event !== "sample") throw new McpError("STATE_UNAVAILABLE", "Live NetFlow недоступен, источник изменился или превышает предел 20000 строк", 409) + const dto = flowAnalyticsDtoSchema.parse(event.data) + const data: Record = { ...dto } + const pages: Record = {} + const offset = Number(input.offset), limit = Number(input.limit) + for (const [key, value] of Object.entries(dto)) if (Array.isArray(value) && key !== "rxSeries" && key !== "txSeries") { + data[key] = value.slice(offset, offset + limit) + pages[key] = { total: value.length, nextOffset: offset + limit < value.length ? offset + limit : null } + } + events.push({ event: "sample" as const, sampleStartedAt, observedAt: new Date().toISOString(), data, pages }) + await authorize() + } + const output = { serverId, userId: input.userId ?? null, interface: input.interface ?? null, events, readStartedAt, observedAt: new Date().toISOString(), + observationComplete: true, streamContinues: false, sampleIntervalMs: FLOW_LIVE_TICK_MS, windowMinutes: LIVE_ANALYTICS_MINUTES, + 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", + 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 } + if (Buffer.byteLength(JSON.stringify(output)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Ответ live превышает 1 МиБ; уменьшите страницу или число samples", 409) + await authorize() + return withCurrentFlowReadQuality(output,context) + } catch (error) { + await authorize() + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Live NetFlow недоступен или чтение отменено", 409) + } finally { releaseFlowLiveSlot() } + }, + }) +} diff --git a/backend/src/modules/mcp/flow-map-actions.ts b/backend/src/modules/mcp/flow-map-actions.ts new file mode 100644 index 00000000..bc700ecf --- /dev/null +++ b/backend/src/modules/mcp/flow-map-actions.ts @@ -0,0 +1,65 @@ +import { assertBoundedFlowSource as assertTrafficHistorySource } from "./flow-source-scope.js" +import { withCurrentFlowReadQuality } from "./flow-read-quality.js" +import { z } from "zod" +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 { getBoundedFlowServerRowById as getServerRowById } from "../../services/traffic-flow-server-source.js" +import { assertTrafficHistoryAccess } from "./traffic-history-actions.js" +import { McpError } from "./security.js" +import type { add } from "./actions.js" + +export function registerFlowMapActions(register: typeof add) { + register("traffic.flow.map.read", "mm_netflow_map_hops_get", "NetFlow: предполагаемые связи, сервисы и страны выбранного сервера", "traffic", "mm:traffic:read", "read", "servers", z.object({ + serverId: z.number().int().positive().safe(), minutes: z.number().int().min(1).max(1440).default(15), + userId: z.string().min(1).max(100).optional(), + interface: z.string().trim().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f]/.test(value) && value !== "__all__").optional(), + dedup: z.boolean().default(true), excludeMesh: z.boolean().default(true), excludeOverlay: z.boolean().default(true), + offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(100).default(20), + nestedOffset: z.number().int().min(0).max(100000).default(0), nestedLimit: z.number().int().min(1).max(50).default(20), + }).strict(), { + read: async (input, context) => { + await assertTrafficHistoryAccess(context) + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const authorize = () => assertTrafficHistorySource(context, server), startedAt = new Date().toISOString() + 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, + 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 })) + const pages: Record = {} + const project = (value: unknown, path = "", depth = 0): unknown => { + if (Array.isArray(value)) { + const offset = Number(depth === 1 ? input.offset : input.nestedOffset), limit = Number(depth === 1 ? input.limit : input.nestedLimit) + pages[path] = { total: value.length, nextOffset: offset + limit < value.length ? offset + limit : null } + return value.slice(offset, offset + limit).map((row, index) => project(row, `${path}[${offset + index}]`, depth + 1)) + } + if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, row]) => [key, project(row, path ? `${path}.${key}` : key, depth + 1)])) + return value + } + const { live: listenerBound, ...map } = result + const projected = project(map) + 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, + 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, + referencedNodesMayBeOutsidePage: true, inferredPathsVerified: false, forwardingVerified: false, coverageVerified: false, + rowSumIsNetworkTotal: false, historicalOwnershipVerified: false, + attribution: "current named whole-interface bindings; users with hidden bindings excluded", + collectorStarted: false, backgroundEnrichmentStarted: false, tracerouteExecuted: false } + if (Buffer.byteLength(JSON.stringify(output)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Ответ карты превышает 1 МиБ; уменьшите страницы", 409) + return withCurrentFlowReadQuality(output,context) + } catch (error) { + await assertTrafficHistoryAccess(context) + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Карта недоступна, изменилась во время чтения или превышает предел; уменьшите окно", 409) + } + }, + }) +} diff --git a/backend/src/modules/mcp/flow-overlay-actions.ts b/backend/src/modules/mcp/flow-overlay-actions.ts new file mode 100644 index 00000000..3f54bdbc --- /dev/null +++ b/backend/src/modules/mcp/flow-overlay-actions.ts @@ -0,0 +1,96 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { trafficFlowPublicEndpointSchema } from "@mmapp/contracts/traffic-flow" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { effectiveMcpConnection } from "./store.js" +import { accessGeneration, encryptSecret, decryptSecret, fingerprint, redact, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { readOverlayState, validateOverlayConfiguration, type OverlayState, type OverlayOptions } from "../../services/traffic-flow-overlay-state.js" +import { executeFlowOverlay, type OverlayExecutionReceipt } from "../../services/traffic-flow-overlay-execution.js" +import { overlayMatchesDesired } from "../../services/traffic-flow-overlay-reconcile.js" +import { getFlowListenerState } from "../../services/traffic-flow-ingest.js" +import { getFlowHostStatus } from "../../services/traffic-flow-host-agent.js" +import type { add } from "./actions.js" + +const operation="traffic.flow.overlay.apply",permissions=["mm:traffic:write","mm:network:write","mm:settings:admin"] +const digest=z.string().regex(/^[a-f0-9]{64}$/) +const authoritySchema=z.object({principal:z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.iso.datetime()}).strict(),ownerId:z.string(),policyProof:digest,generation:digest}).strict() +type Authority=z.infer +async function authorizeOverlay(raw:unknown,signal?:AbortSignal) { + if(signal?.aborted)throw new McpError("READ_CANCELLED","Overlay отменён",409) + const authority=authoritySchema.parse(raw),connection=await effectiveMcpConnection(authority.principal) + if(connection.owner_id!==authority.ownerId||!connection.policy.scope.allowGlobal||!connection.policy.scope.operationIds.includes(operation)||permissions.some(permission=>!hasPermission(connection.owner_permissions,permission)||!hasPermission(connection.policy.permissions,permission)))throw new McpError("PERMISSION_DENIED","Нет полного допуска к настройке router/collector overlay",403) + if(fingerprint(connection.policy)!==authority.policyProof||await accessGeneration()!==authority.generation)throw new McpError("RESET_REQUIRED","Допуск overlay изменился",409) + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>row.id) + if(ids.length>1000||ids.some(id=>!connection.policy.scope.serverIds.includes(id)))throw new McpError("SERVER_DENIED","Overlay хоста требует все серверы приложения",403) + return ids +} +const options=(input:Record):OverlayOptions=>({publicEndpoint:input.publicEndpoint as string|undefined,disableGreFastPath:input.disableGreFastPath===true}) +export function registerFlowOverlayActions(register:typeof add) { + const state=async(input:Record,extra:()=>Promise=async()=>{})=>{ + const authorize=async()=>{await extra();await authorizeOverlay(input.authority)} + return readOverlayState(Number(input.serverId),options(input),authorize) + } + registerMcpService(operation,{ + current:async input=>({proof:(await state(input)).proof}), + execute:async input=>{ + const authorize=async()=>{await assertVerifiedConfigurationAccess();await authorizeOverlay(input.authority)} + await authorize() + const original=JSON.parse(await decryptSecret(String(input.originalSealed))) as OverlayState + if(original.proof!==input.expectedProof)throw new McpError("STATE_CONFLICT","Не подтверждён исходный proof overlay",409) + const result=await executeFlowOverlay(Number(input.serverId),options(input),{authorize,expectedProof:String(input.expectedProof)}) + await authorize() + if(result.stage==="host-pending")throw new McpError("MUTATION_OUTCOME_UNKNOWN","Применение Linux-хоста не подтверждено; нужна read-only сверка без повтора",502) + const after=await state(input,authorize) + if(!overlayMatchesDesired(original,after,options(input)))throw new McpError("VERIFICATION_FAILED","Полная конфигурация overlay не совпала с намерением; нужна сверка",409) + return {...result.receipt,stage:result.stage,serverId:Number(input.serverId),keyMaterialReturned:false,forwardingVerified:false,handshakeVerified:false,hostFilesReturned:false} + }, + verify:async(input,result)=>{ + const receipt=result as OverlayExecutionReceipt|null + if(!receipt?.configurationVerified||!receipt.hostInstallationRequired&&(!receipt.collectorListening||!getFlowListenerState().bound))return false + const after=await state(input),before=JSON.parse(await decryptSecret(String(input.originalSealed))) as OverlayState + if(after.proof!==receipt.afterProof||!overlayMatchesDesired(before,after,options(input)))return false + if(after.agentConfigured&&(await getFlowHostStatus()).configMatches!==true)return false + await authorizeOverlay(input.authority) + return (await state(input)).proof===after.proof + }, + reconcile:async(input,extra)=>{ + if(input.proofRevision!==1)throw new McpError("UNSUPPORTED_CAPABILITY","Нет совместимого proof overlay",409) + const authorize=async()=>{await extra();await authorizeOverlay(input.authority)} + await authorize();const before=JSON.parse(await decryptSecret(String(input.originalSealed))) as OverlayState + if(before.proof!==input.expectedProof)throw new McpError("STATE_CONFLICT","Исходный proof overlay не подтверждён",409) + const after=await state(input,authorize) + // Generated keys and native defaults may match the requested graph after an + // unrelated write. Only this private intent's verified final receipt can + // attribute the complete result to the operation; an unknown write stays unknown. + const receiptId=z.string().uuid().safeParse(input.receiptId) + const checkpoints=receiptId.success?(await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.state='verified' AND s.intended_state=$2::jsonb AND s.result->>'configurationVerified'='true' LIMIT 2",[authoritySchema.parse(input.authority).principal.connectionId,JSON.stringify(redact({kind:"service",service:operation,input}))])).rows:[] + const afterProof=checkpoints.length===1?digest.safeParse(checkpoints[0].result?.afterProof):undefined + const matchesDesiredState=afterProof?.success===true&&after.proof===afterProof.data&&overlayMatchesDesired(before,after,options(input))&&(!after.agentConfigured||getFlowListenerState().bound&&(await getFlowHostStatus()).configMatches===true) + await authorize() + if((await state(input,authorize)).proof!==after.proof)throw new McpError("STATE_CONFLICT","Overlay изменился во время сверки",409) + return {matchesDesiredState,matchesOriginalState:after.proof===before.proof} + }, + }) + register(operation,"mm_netflow_overlay_apply_plan","Настроить WireGuard/IPFIX overlay, коллектор и host peer с проверкой каждого шага","traffic",permissions,"write","global",z.object({serverId:z.number().int().positive().safe(),publicEndpoint:trafficFlowPublicEndpointSchema.refine(value=>value.length>0).optional(),disableGreFastPath:z.boolean().default(false)}).strict(),{ + prepare:async(input,context)=>{ + const authority:Authority={principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy),generation:await accessGeneration()} + const authorize=async()=>{await authorizeOverlay(authority,context.signal)} + const ids=await authorizeOverlay(authority,context.signal) + if(!ids.includes(Number(input.serverId)))throw new McpError("SERVER_DENIED","Сервер вне области overlay",403) + const before=await readOverlayState(Number(input.serverId),options(input),authorize,context.signal) + validateOverlayConfiguration(before,options(input)) + const objects=[{kind:"netflow-overlay",id:"1"},{kind:"netflow-settings",id:"1"},{kind:"netflow-host",id:"1"},{kind:"netflow-host-files",id:"1"},...ids.map(serverId=>({kind:"server",id:String(serverId),serverId}))] + const protections=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' IN ('netflow-overlay','netflow-settings','netflow-host','netflow-host-files') LIMIT 1001",[ids.map(String)]) + if(protections.rows.length>1000)throw new McpError("STATE_UNAVAILABLE","Превышен предел защит overlay",409) + for(const {object_ref:ref}of protections.rows)objects.push(ref) + await authorize() + if((await readOverlayState(Number(input.serverId),options(input),authorize,context.signal)).proof!==before.proof)throw new McpError("STATE_CONFLICT","Overlay изменился во время подготовки",409) + return {target:{scope:"global"},objects,snapshots:[],before:{proof:before.proof},requiresHuman:true,mutationCost:24+(input.disableGreFastPath?(before.router["/interface/gre"]?.length??0):0), + steps:[{kind:"service",service:operation,input:{...input,authority,receiptId:randomUUID(),expectedProof:before.proof,beforeFingerprint:fingerprint({proof:before.proof}),originalSealed:await encryptSecret(JSON.stringify(before)),proofRevision:1}}], + warnings:["Будут настроены wg-flow, адрес с маской настроенного префикса, peer коллектора, маршрут /32, firewall UDP13232, IPFIX all interfaces/cache256k/таймауты, host peer и глобальный UDP-коллектор. Эти изменения затрагивают все server peers хоста и требуют полного global scope.","При disableGreFastPath=true каждый статический GRE получает allow-fast-path=no; это повышает нагрузку CPU. Динамические GRE, конфликтующие адреса/маршруты/правила и неоднозначные объекты блокируются до записи.","Каждая native-команда отправляется один раз, проверяется чтением до следующего шага. Потеря ответа или несовпадение останавливает workflow с partial/unknown; автоматического повтора и универсального отката нет. Read-only reconcile не отправляет команд.",before.agentConfigured?"После сохранения peer применяется Linux-agent и проверяются configMatches и bind listener; неудача требует сверки.":"Linux-agent отсутствует: результат manual. Установка на Linux и bind collector не подтверждаются; владелец отдельно получает конфигурацию через подтверждаемый human-only export. Ключи и файлы MCP не возвращаются.","Успех подтверждает конфигурацию; handshake, достижимость collector и поступление IPFIX/forwarding не доказаны."]} + }, + }) +} diff --git a/backend/src/modules/mcp/flow-purge-actions.ts b/backend/src/modules/mcp/flow-purge-actions.ts new file mode 100644 index 00000000..c04fc996 --- /dev/null +++ b/backend/src/modules/mcp/flow-purge-actions.ts @@ -0,0 +1,98 @@ +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { listExpiredPartitions } from "../../db/partitions.js" +import { hasPermission } from "../../lib/permissions.js" +import { purgeTrafficFlowWithEvent } from "../../services/flow-purge-service.js" +import { flowPurgeTables, type FlowPurgeClient } from "../../services/traffic-flow-purge-execution.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, privateSecretFingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const operation="traffic.flow.purge" +const principalSchema=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict() +const partitionSchema=z.object({parent:z.string().regex(/^[a-z_]+$/),name:z.string().regex(/^[a-z_]+_[dwm]_\d{6,8}$/)}).strict() +const fail=(message:string,code="STATE_CONFLICT")=>new McpError(code,message,409) +// Runtime counters/updated_at are written by ingestion and intentionally reset by +// purge. Configuration, peers and key material remain private and must survive. +const configurationSql="SELECT CASE WHEN octet_length((to_jsonb(s)-ARRAY['packets_received','last_datagram_at','last_exporter_ip','last_error','updated_at'])::text)<=65536 THEN to_jsonb(s)-ARRAY['packets_received','last_datagram_at','last_exporter_ip','last_error','updated_at'] ELSE NULL END AS configuration FROM traffic_flow_settings s WHERE id=1" +export async function readFlowPurgeSource(input:Record,signal?:AbortSignal,client:FlowPurgeClient=pool){ + const authorize=async()=>{ + if(signal?.aborted)throw fail("Подготовка сброса отменена","READ_CANCELLED") + // Reuse the locked transaction for authority reads as well as source reads. + // The executor exposes only the text-query subset of the same PG client. + const connection=await effectiveMcpConnection(principalSchema.parse(input.principal),client as Pick) + if(connection.owner_id!==input.ownerId || !connection.policy.scope.allowGlobal || !connection.policy.scope.operationIds.includes(operation) || !hasPermission(connection.policy.permissions,"mm:traffic:write") || !hasPermission(connection.owner_permissions,"mm:traffic:write"))throw fail("Право глобального сброса трафика отозвано","PERMISSION_DENIED") + if(fingerprint(connection.policy)!==input.policyProof)throw fail("Допуск изменился","RESET_REQUIRED") + const ids=(await client.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>Number(row.id)) + if(ids.length>1000 || ids.some(id=>!Number.isSafeInteger(id)||id<=0||!connection.policy.scope.serverIds.includes(id)))throw fail("Сброс требует полного каталога серверов","SERVER_DENIED") + return ids + } + const ids=await authorize() + const catalog=(await client.query("SELECT id,xmin::text AS revision FROM servers ORDER BY id LIMIT 1001")).rows + if(fingerprint(catalog.map(row=>Number(row.id)))!==fingerprint(ids))throw fail("Каталог серверов изменился") + const row=(await client.query(configurationSql)).rows[0] + if(!row?.configuration || typeof row.configuration!=="object")throw fail("Настройки NetFlow отсутствуют или превышают предел","STATE_UNAVAILABLE") + const configuration=row.configuration as Record + const retention=(await client.query("SELECT 'traffic' AS kind,retention_days FROM traffic_settings WHERE id=1 UNION ALL SELECT 'uptime',retention_days FROM uptime_settings WHERE id=1 UNION ALL SELECT 'internet-path',retention_days FROM internet_path_settings WHERE id=1 ORDER BY kind")).rows + if(fingerprint(await authorize())!==fingerprint(ids))throw fail("Каталог серверов изменился") + return{ids,publicState:{serverIds:ids,listenerEnabled:configuration.enabled===true,configurationProof:await privateSecretFingerprint(fingerprint({ids,catalog,configuration,retention}),"flow-purge-source-v1")}} +} +async function previewCounts(){ + const client=await pool.connect() + try{ + await client.query("BEGIN READ ONLY");await client.query("SET LOCAL statement_timeout='5s'") + const counts:Record={} + for(const[key,table]of Object.entries(flowPurgeTables)){ + const count=Number((await client.query(`SELECT count(*)::text AS n FROM ${table}`)).rows[0]?.n) + if(!Number.isSafeInteger(count)||count<0)throw fail("Не подтверждён счётчик данных","STATE_UNAVAILABLE") + counts[key]=count + } + await client.query("COMMIT");return counts + }catch(error){try{await client.query("ROLLBACK")}catch{};throw error}finally{client.release()} +} +export function registerFlowPurgeActions(register:typeof add){ + const current=async(input:Record)=>(await readFlowPurgeSource(input)).publicState + const assertSource=async(input:Record,client?:FlowPurgeClient)=>{ + if(fingerprint((await readFlowPurgeSource(input,undefined,client)).publicState)!==input.beforeFingerprint)throw fail("Каталог, настройки, peers или ключи изменились после подтверждения") + } + registerMcpService(operation,{ + current, + execute:async input=>{ + const fence=async(client?:FlowPurgeClient)=>{await assertVerifiedConfigurationAccess();await assertSource(input,client)} + await fence() + const partitions=z.array(partitionSchema).max(1000).parse(input.partitions),seen=new Set() + const approved=new Set(partitions.map(partition=>`${partition.parent}/${partition.name}`)) + if(approved.size!==partitions.length)throw fail("Неоднозначный список разделов") + const result=await purgeTrafficFlowWithEvent({beforeDrain:()=>fence(),beforeDelete:client=>fence(client),beforeMaintenance:()=>fence(),beforePartitionDrop:async partition=>{ + await fence() + const key=`${partition.parent}/${partition.name}` + if(!approved.has(key)||seen.has(key)||seen.size>=partitions.length)throw fail("Раздел отсутствует в подтверждённом списке или превышен бюджет обслуживания") + seen.add(key) + },beforeRestart:()=>fence(),beforeRecord:()=>fence()}) + // Restoring normal UDP ingestion may immediately add new rows. Verification + // checks preserved configuration and the confirmed transaction, never zero. + await fence() + return{...result,...(result.partitionError?{partitionError:"Удаление подтверждено; результат обслуживания разделов не подтверждён"}:{}),...(result.listenerError?{listenerError:"Удаление подтверждено; восстановление приёма требует проверки"}:{}),...(result.eventError?{eventError:"Удаление подтверждено; запись события не подтверждена"}:{}),partitionDropAttempts:seen.size,partitionDropBudget:partitions.length,configurationPreserved:true,attempts:1,retries:0,notificationsScheduled:false,listenerRestartIsNormalOperation:true} + }, + verify:async(input,result)=>{await assertSource(input);return(result as{ok?:boolean;configurationPreserved?:boolean}|null)?.ok===true&&(result as{configurationPreserved?:boolean}).configurationPreserved===true}, + reconcile:async(_input,authorize)=>{await authorize();return{matchesDesiredState:false,matchesOriginalState:false}}, + }) + register(operation,"mm_netflow_purge_plan","Безвозвратно сбросить все данные NetFlow","traffic","mm:traffic:write","write","global",z.object({}).strict(),{ + prepare:async(input,context)=>{ + const request={...input,principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy)} + const source=await readFlowPurgeSource(request,context.signal),counts=await previewCounts(),partitions=await listExpiredPartitions(pool) + const objects=[{kind:"netflow-purge",id:"global"},{kind:"netflow-settings",id:"1"},...source.ids.map(serverId=>({kind:"server",id:String(serverId),serverId})),...partitions.map(partition=>({kind:"database-partition",id:`${partition.parent}/${partition.name}`}))] + const protections=await pool.query("SELECT object_ref FROM mcp_protections LIMIT 1001") + if(protections.rows.length>1000)throw fail("Превышен предел защит","STATE_UNAVAILABLE") + for(const row of protections.rows)objects.push(row.object_ref) + if(fingerprint((await readFlowPurgeSource(request,context.signal)).publicState)!==fingerprint(source.publicState))throw fail("Источник изменился во время подготовки") + return{target:{scope:"global"},objects,snapshots:[],before:source.publicState,requiresHuman:true,mutationCost:2+partitions.length,verificationScope:"acknowledgement", + steps:[{kind:"service",service:operation,input:{...request,partitions,beforeFingerprint:fingerprint(source.publicState)}}], + warnings:[`Предварительные счётчики строк (оценка, не снимок удаления): ${JSON.stringify(counts)}`,"Будут удалены ВСЕ текущие строки шести таблиц NetFlow и сброшены runtime counters. Новые данные до подтверждённой остановки тоже входят в сброс. Отката нет.","UDP worker останавливается с подтверждением flush/выхода; отсутствие подтверждения блокирует DELETE и restart. Настройки, ключи и peers сохраняются. Listener затем восстанавливает обычный приём и его фоновое enrichment; новые flows после restart допустимы.","Сохраняется полный REST-сценарий обслуживания: после сброса штатная retention policy удаляет истёкшие разделы NetFlow, traffic, server-ping, uptime, server snapshots и internet-path. Это глобальное обслуживание, не только шесть очищаемых таблиц.","Сценарий не запускает alert engine и не отправляет уведомлений. Журнал, partition maintenance и listener могут потребовать отдельной проверки после подтверждённого удаления. Бюджет: одна операция удаления, одна попытка записи события и по одной попытке DROP для каждого показанного в плане истёкшего раздела. Новые не включённые в план разделы не удаляются.","Потеря ответа COMMIT остаётся unknown. Повторное исполнение и восстановление по признаку пустых таблиц запрещены; новый сброс требует нового человеческого плана."]} + }, + }) +} + diff --git a/backend/src/modules/mcp/flow-read-quality.test.ts b/backend/src/modules/mcp/flow-read-quality.test.ts new file mode 100644 index 00000000..90705f0d --- /dev/null +++ b/backend/src/modules/mcp/flow-read-quality.test.ts @@ -0,0 +1,46 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { createRequire } from "node:module" +import { test } from "node:test" +import ts from "typescript" +import { flowIngestQualitySchema } from "@mmapp/contracts/traffic-flow" +import { createFlowIngestQuality } from "../../services/traffic-flow-ingest-quality.js" +import type { ActionContext } from "./action-types.js" + +const code=ts.transpileModule(readFileSync(new URL("./flow-read-quality.ts",import.meta.url),"utf8"),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText +function setup(options:{allowGlobal:boolean;operations:string[];quality?:unknown;revokeAfterRead?:boolean}){ + let reads=0,checks=0 + const localRequire=createRequire(import.meta.url) + const mocks:Record={ + "@mmapp/contracts/traffic-flow":{flowIngestQualitySchema}, + "../../services/traffic-flow-ingest.js":{getFlowRuntimeCounters:async()=>{reads++;return{dataQuality:options.quality}}}, + "./traffic-history-actions.js":{assertTrafficHistoryAccess:async()=>{checks++;if(options.revokeAfterRead&&reads)throw Error("revoked")}}, + } + const loaded={exports:{} as {withCurrentFlowReadQuality:(result:object,context:ActionContext)=>Promise>}} + new Function("require","module","exports",code)((id:string)=>{if(id in mocks)return mocks[id];if(id==="@mmapp/contracts/traffic-flow")return localRequire(id);throw Error("Unexpected dependency "+id)},loaded,loaded.exports) + const context={connection:{policy:{scope:{allowGlobal:options.allowGlobal,operationIds:options.operations}}}} as ActionContext + return {read:()=>loaded.exports.withCurrentFlowReadQuality({incomplete:false},context),counts:()=>({reads,checks})} +} +test("server scope and partial global grants never read or publish collector-wide counters",async()=>{ + const quality=createFlowIngestQuality();quality.reject(987654,987,"aggregate-capacity") + for(const options of [{allowGlobal:false,operations:[]},{allowGlobal:true,operations:[]},{allowGlobal:false,operations:["netflow.settings.read"]}]){ + const fixture=setup({...options,quality:quality.snapshot()}),result=await fixture.read() + assert.equal(result.dataQuality,null);assert.equal(result.collectorQualityAvailable,false) + assert.equal(result.collectorQualityUnavailableReason,"global-telemetry-not-granted") + assert.equal(result.collectorObservationIncomplete,true);assert.equal(result.incomplete,false) + assert.ok(!JSON.stringify(result).includes("987654"));assert.deepEqual(fixture.counts(),{reads:0,checks:0}) + } +}) +test("explicit existing global telemetry grant preserves exact honest quality and page completeness",async()=>{ + const quality=createFlowIngestQuality();quality.reject(321,4,"ring-capacity") + const fixture=setup({allowGlobal:true,operations:["netflow.settings.read"],quality:quality.snapshot()}) + const result=await fixture.read() + assert.deepEqual(result.dataQuality,quality.snapshot());assert.equal(result.incomplete,false) + assert.equal(result.collectorObservationIncomplete,true);assert.equal(result.coverageVerified,false) + assert.equal(result.exportCoverage,"unverified");assert.deepEqual(fixture.counts(),{reads:1,checks:2}) +}) +test("invalid global quality stays unknown and revocation after source read refuses publication",async()=>{ + const invalid=await setup({allowGlobal:true,operations:["netflow.settings.read"],quality:{records:"0"}}).read() + assert.equal(invalid.dataQuality,null);assert.equal(invalid.collectorQualityUnavailableReason,"unobserved") + await assert.rejects(setup({allowGlobal:true,operations:["netflow.settings.read"],quality:createFlowIngestQuality().snapshot(),revokeAfterRead:true}).read(),/revoked/) +}) diff --git a/backend/src/modules/mcp/flow-read-quality.ts b/backend/src/modules/mcp/flow-read-quality.ts new file mode 100644 index 00000000..e0103795 --- /dev/null +++ b/backend/src/modules/mcp/flow-read-quality.ts @@ -0,0 +1,21 @@ +import { flowIngestQualitySchema } from "@mmapp/contracts/traffic-flow" +import { getFlowRuntimeCounters } from "../../services/traffic-flow-ingest.js" +import { assertTrafficHistoryAccess } from "./traffic-history-actions.js" +import type { ActionContext } from "./action-types.js" +export async function withCurrentFlowReadQuality(result:T,context:ActionContext) { + // Collector-wide loss counts include exporters outside a server grant. Use + // the existing explicit global telemetry authorization; never relabel them + // as loss counts of the selected server/user/interface. + const globalTelemetry=context.connection.policy.scope.allowGlobal&&context.connection.policy.scope.operationIds.includes("netflow.settings.read") + let observed:unknown=null + if(globalTelemetry){ + await assertTrafficHistoryAccess(context) + const runtime=await getFlowRuntimeCounters() + observed="dataQuality" in runtime?runtime.dataQuality:null + await assertTrafficHistoryAccess(context) + } + const parsed=flowIngestQualitySchema.safeParse(observed) + const dataQuality=parsed.success?parsed.data:null + return {...result,dataQuality,collectorQualityAvailable:dataQuality!==null,collectorQualityScope:"current-collector-isolate" as const,collectorQualityUnavailableReason:globalTelemetry?dataQuality===null?"unobserved":null:"global-telemetry-not-granted",exportCoverage:"unverified" as const, + collectorObservationIncomplete:dataQuality===null||dataQuality.localAdmission!=="no-local-loss-observed",coverageVerified:false} +} diff --git a/backend/src/modules/mcp/flow-settings-acceptance.ts b/backend/src/modules/mcp/flow-settings-acceptance.ts new file mode 100644 index 00000000..370169f6 --- /dev/null +++ b/backend/src/modules/mcp/flow-settings-acceptance.ts @@ -0,0 +1,107 @@ +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import type { FastifyInstance } from "fastify" +import type { AuthUser } from "../../lib/permissions.js" +import { pool } from "../../db/index.js" +import { env } from "../../config.js" +import { agentSocket } from "../../services/traffic-flow-agent-client.js" +import { createFlowSettingsStore } from "../../services/traffic-flow-settings-store.js" +import { invalidateTrafficFlowSettingsCache } from "../../services/traffic-flow-settings.js" +import { updateTrafficFlowSettingsWithListener } from "../../services/traffic-flow-settings-service.js" +import { getFlowWorkerHealth, getFlowListenerState } from "../../services/traffic-flow-ingest.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { decryptSecret } from "./security.js" +import { createFlowSettingsAdapter } from "./flow-settings-actions.js" +import type { ChangeEvidence } from "./action-types.js" + +/** Called only by the root-owned disposable --flow-host-keys SQL fixture. + * Exercises actual main-process full restart/flush with enabled=false. It never + * requests UDP workers; enabled=true worker wiring is proved by injected tests. */ +export async function acceptMcpFlowSettingsUpdate(actor: AuthUser, app: FastifyInstance) { + assert.equal(process.env.MMAPP_OWN_FLOW_HOST_KEYS, "true") + const endpoint = new URL(process.env.DATABASE_URL!) + assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.pathname, "/postgres") + assert.equal(env.authRequired, true); assert.ok(!agentSocket()) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + const store = createFlowSettingsStore(pool, async () => {}, invalidateTrafficFlowSettingsCache) + const before = await store.read() + assert.ok(before.hostPrivateKey); assert.equal(before.enabled, false); assert.equal(getFlowWorkerHealth().alive, false) + let network = 0 + const savedFetch = globalThis.fetch + globalThis.fetch = (async () => { network++; throw Error("Disabled settings acceptance forbids network") }) as typeof fetch + const protectionId = randomUUID() + const wait = async (id: string) => { + for (const deadline = Date.now() + 45000; Date.now() < deadline;) { + const row = (await pool.query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(20) + } + throw Error("Unconfirmed settings operation; do not replay") + } + try { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "full settings disabled acceptance", mode: "autonomous", permissions: ["mm:traffic:write"], expiresAt: new Date(Date.now() + 600000).toISOString(), scope: { allowGlobal: true, serverIds: [], operationIds: ["netflow.settings.update"] }, limits: { callsPerMinute: 240, mutationsPerHour: 1000, concurrentReads: 4, serversPerPlan: 1000, objectsPerPlan: 1000 } }), actor) + const principal = await authenticateMcpToken(access.token) + const patch = { enabled: false, collectorIp: "10.255.253.1", flowListenPort: 4740, wgListenPort: 51822, prefix: "10.255.253.0/24", publicEndpoint: "flow.example.test", hubServerId: null, retentionHours: 48, topN: 100, mapServiceMinSharePct: 0 } + const counts = async () => (await pool.query("SELECT (SELECT count(*)::int FROM flow_settings_intents) AS intents,(SELECT count(*)::int FROM flow_write_receipts) AS writes")).rows[0] + const stateBeforeBudget = await store.read(), countsBeforeBudget = await counts() + const limited = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "settings bound four rejects budget three", mode: "autonomous", permissions: ["mm:traffic:write"], expiresAt: new Date(Date.now()+600000).toISOString(), scope: {allowGlobal:true,serverIds:[],operationIds:["netflow.settings.update"]}, limits: {mutationsPerHour:3,callsPerMinute:240,serversPerPlan:1000,objectsPerPlan:1000} }),actor) + const limitedPrincipal = await authenticateMcpToken(limited.token) + await assert.rejects(invokeMcpTool(limitedPrincipal,"mm_netflow_settings_update_plan",{settings:patch}),error => (error as {code?:string}).code === "BUDGET_EXCEEDED") + assert.deepEqual(await store.read(),stateBeforeBudget) + assert.deepEqual(await counts(),countsBeforeBudget,"insufficient max-four budget cannot reach settings/intent/owned writer effects") + const oldVersion = await invokeMcpTool(principal,"mm_netflow_settings_update_plan",{settings:patch}) as {planId:string} + await approvePlan(oldVersion.planId,actor,true) + await pool.query("UPDATE mcp_plans SET operation_version=2 WHERE id=$1",[oldVersion.planId]) + await assert.rejects(applyPlan(principal,oldVersion.planId,randomUUID()),error=>(error as {code?:string}).code==="PLAN_EXPIRED") + assert.deepEqual(await store.read(),stateBeforeBudget) + assert.deepEqual(await counts(),countsBeforeBudget,"old underbudget version2 is refused before phase intent or writer effects") + for (const input of [patch, { mapServiceMinSharePct: 0 }]) { + const planned = await invokeMcpTool(principal, "mm_netflow_settings_update_plan", {settings:input}) as {planId:string;state:string} + assert.equal(planned.state,"awaiting-approval") + const evidence:ChangeEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1",[planned.planId])).rows[0].evidence.sealed)) + assert.equal(evidence.requiresHuman,true);assert.equal(evidence.mutationCost,4) + const step=evidence.steps[0];assert.equal(step.kind,"service");if(step.kind!=="service")throw Error() + const adapter=createFlowSettingsAdapter() + assert.deepEqual(await adapter.service.current!(step.input),evidence.before) + assert.equal((await adapter.service.reconcile!(step.input,async()=>{})).matchesDesiredState,false) + await assert.rejects(applyPlan(principal,planned.planId,randomUUID())) + await approvePlan(planned.planId,actor,true) + const operation=await applyPlan(principal,planned.planId,randomUUID()),finished=await wait(operation.id) + assert.equal(finished.state,"applied",JSON.stringify(finished));assert.equal(finished.verification,"passed") + const saved=(await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1",[operation.id])).rows[0] + assert.equal(saved.state,"verified") + const receipt=saved.result + assert.equal(receipt.configurationConfirmed,true);assert.equal(receipt.receiptConfirmed,true) + assert.equal(receipt.listener,"request-acknowledged");assert.equal(receipt.listenerReadinessConfirmed,false) + assert.equal(receipt.attempts,2);assert.equal(receipt.retries,0) + assert.deepEqual(await store.read(),{...before,...patch}) + const claims=(await pool.query("SELECT phase,state FROM flow_settings_intents WHERE receipt_id=$1 ORDER BY phase",[receipt.receiptId])).rows + assert.deepEqual(claims,[{phase:"configuration",state:"completed"},{phase:"listener",state:"completed"}]) + assert.equal((await adapter.service.reconcile!(step.input,async()=>{})).matchesDesiredState,true) + await assert.rejects(updateTrafficFlowSettingsWithListener(input,{receiptId:receipt.receiptId})) + assert.ok(!JSON.stringify({planned,receipt}).includes(String(before.hostPrivateKey))) + assert.equal(getFlowWorkerHealth().alive,false);assert.equal(getFlowListenerState().bound,false) + } + const jwt=app.jwt.sign({sub:actor.id,apps:actor.apps,permissions:actor.permissions},{iss:env.authIssuer,expiresIn:"10m"}) + const response=await app.inject({method:"PUT",url:"/api/traffic/flow/settings",headers:{authorization:"Bearer "+jwt},payload:{mapServiceMinSharePct:0}}) + assert.equal(response.statusCode,200,response.body);assert.equal(response.json().listenerError,undefined) + assert.equal((await app.inject({method:"PUT",url:"/api/traffic/flow/settings",payload:{enabled:false}})).statusCode,401) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)",[protectionId,{kind:"database-table",id:"flow_ip_meta",protection:"deny"},actor.id]) + const protectedResponse=await app.inject({method:"PUT",url:"/api/traffic/flow/settings",headers:{authorization:"Bearer "+jwt},payload:{topN:99}}) + assert.equal(protectedResponse.statusCode,409,protectedResponse.body) + await assert.rejects(invokeMcpTool(principal,"mm_netflow_settings_update_plan",{settings:{topN:99}})) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protectionId]) + const revoked=await invokeMcpTool(principal,"mm_netflow_settings_update_plan",{settings:{topN:98}}) as {planId:string} + await approvePlan(revoked.planId,actor,true);await setConnectionStatus(access.connection.id,"paused",actor) + await assert.rejects(applyPlan(principal,revoked.planId,randomUUID())) + assert.equal((await store.read()).topN,100) + assert.equal(network,0);assert.equal(getFlowWorkerHealth().alive,false);assert.equal(getFlowListenerState().bound,false) + console.log("Full settings SQL/HTTP/MCP: all ten fields and no-op/display restart with enabled=false, full private CAS/future preservation, durable two-phase receipt, VERIFIED reconciliation, JWT/protection/revocation parity; actual main flush only, no UDP worker/network acceptance") + } finally { + globalThis.fetch=savedFetch + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protectionId]) + } +} diff --git a/backend/src/modules/mcp/flow-settings-actions.test.ts b/backend/src/modules/mcp/flow-settings-actions.test.ts new file mode 100644 index 00000000..b1bee80d --- /dev/null +++ b/backend/src/modules/mcp/flow-settings-actions.test.ts @@ -0,0 +1,102 @@ +import assert from "node:assert/strict" +import { randomUUID, randomBytes, createDecipheriv, createHmac } from "node:crypto" +import { mcpConnectionPolicySchema } from "@mmapp/contracts/mcp" +import { createFlowSettingsAdapter, flowSettingsActionSchema } from "./flow-settings-actions.js" +import { fingerprint, seal, redact } from "./security.js" +import type { ConnectionRow } from "./store.js" +import type { FlowSettingsConfiguration } from "../../services/traffic-flow-settings-execution.js" +import { publicMcpChangeEvidence } from "./routing-community-public.js" + +function fixture() { + const key = randomBytes(32), principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } + const connection: ConnectionRow = { id: principal.connectionId, name: "fixture", owner_id: "owner", owner_permissions: ["mm:traffic:write"], generation: "generation", created_at: new Date(), updated_at: new Date(), last_used_at: null, owner_valid_until: new Date(Date.now() + 600000), owner_claims_issued_at: new Date(), policy: mcpConnectionPolicySchema.parse({ id: principal.connectionId, version: 1, status: "active", mode: "autonomous", expiresAt: principal.credentialExpiresAt, permissions: ["mm:traffic:write"], scope: { serverIds: [1, 2], operationIds: ["netflow.settings.update"], allowGlobal: true } }) } + const configuration: FlowSettingsConfiguration = { id: 1, hostPublicKey: "public-existing", hostPrivateKey: "PRIVATE-EXISTING", enabled:false, topN:200, future: "opaque", peersJson: [{ privateOpaque: "retained" }] } + let catalog = [1, 2], protections: Record[] = [], writes = 0, verified: Record[] = [] + let beforeWrite = () => {}, afterWrite = () => {} + const proof = async (value: unknown) => createHmac("sha256", key).update(fingerprint(value)).digest("hex") + const adapter = createFlowSettingsAdapter({ + proof, seal: async value => seal(value, key), + open: async value => { const data = Buffer.from(value, "base64"), c = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)); c.setAuthTag(data.subarray(12, 28)); return Buffer.concat([c.update(data.subarray(28)), c.final()]).toString() }, + connection: async () => connection, verifiedAccess: async () => {}, + query: async sql => sql.includes("mcp_operation_steps") ? verified : sql.includes("mcp_protections") ? protections : catalog.map(id => ({ id })), + read: async authorize => { await authorize?.(); return structuredClone(configuration) }, + update: async (patch, control) => { + await control?.authorize?.(); beforeWrite(); await control?.authorize?.() + assert.deepEqual(control?.expected?.before, configuration) + assert.deepEqual(control?.expected?.command, {kind:"patch",patch}) + Object.assign(configuration,patch); writes++ + afterWrite(); await control?.authorize?.() + return {ok:true,receiptId:control!.receiptId!,status:"applied",configurationConfirmed:true,listener:"request-acknowledged",listenerReadinessConfirmed:false,afterProof:await proof(configuration),receiptConfirmed:true,attempts:2,retries:0,privateMaterialExported:false,automaticReplay:false} + }, + }) + return { adapter, connection, principal, prepare: () => adapter.prepare({settings:{topN:100}}, { connection: structuredClone(connection), principal }), + get configuration() { return configuration }, get writes() { return writes }, + catalog: () => { catalog = [1] }, protect: () => { protections = [{ object_ref: { kind: "netflow-settings", id: "1", protection: "deny" }, revision: "2" }] }, + before: (fn: () => void) => { beforeWrite = fn }, after: (fn: () => void) => { afterWrite = fn }, + verified: (value: Record) => { verified = [value] }, + legacy: (request: Record, version = 1) => { + const data = Buffer.from(String(request.bindingCiphertext), "base64"), cipher = createDecipheriv("aes-256-gcm", key, data.subarray(0,12)) + cipher.setAuthTag(data.subarray(12,28)) + const binding = JSON.parse(Buffer.concat([cipher.update(data.subarray(28)),cipher.final()]).toString()) + assert.equal(binding.version,3) + binding.version=version + return {...request,bindingCiphertext:seal(JSON.stringify(binding),key)} + } } +} +const input = (evidence: Awaited["prepare"]>>) => { assert.equal(evidence.steps[0].kind, "service"); if (evidence.steps[0].kind !== "service") throw Error(); return evidence.steps[0].input } +for (const raw of [{topN:100}, {settings:{}}, {settings:{topN:100,ignored:true}}, {}, { force: true }, { serverId: 1 }, { includePrivateKey: true }, { receiptId: randomUUID() }]) assert.equal(flowSettingsActionSchema.safeParse(raw).success, false) +{ + const f = fixture(), evidence = await f.prepare(), request = input(evidence) + assert.deepEqual(await f.adapter.service.current!(request), evidence.before) + assert.equal(evidence.verificationScope, "configuration") + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 4) + assert.deepEqual(evidence.objects.filter(ref => ref.kind === "server").map(ref => ref.serverId), [1, 2]) + const visible = JSON.stringify(redact(publicMcpChangeEvidence("netflow.settings.update", evidence))) + for (const secret of ["PRIVATE-EXISTING", "privateOpaque", "beforeProof", "ownerProof", "principal"]) assert.ok(!visible.includes(secret)) + const receipt = await f.adapter.service.execute(request) + assert.equal(f.writes, 1) + assert.equal(await f.adapter.service.verify!(request, receipt), true) + assert.equal(await f.adapter.service.verify!(request, { ...receipt as object, privateMaterialExported: true }), false) + assert.equal((await f.adapter.service.reconcile!(request, async () => {})).matchesDesiredState, false, "matching row without VERIFIED is insufficient") + f.verified({ connection_id: f.principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: "netflow.settings.update", input: request }), result: receipt }) + assert.equal((await f.adapter.service.reconcile!(request, async () => {})).matchesDesiredState, true) + assert.ok(!JSON.stringify(receipt).includes("PRIVATE")) + assert.equal(await f.adapter.service.verify!(request, {...receipt as object, listenerReadinessConfirmed:true}),false) + assert.equal(await f.adapter.service.verify!(request, {...receipt as object, receiptConfirmed:false}),false) + assert.equal(await f.adapter.service.verify!(request, {...receipt as object, listener:"unknown",status:"partial"}),false) + assert.ok(evidence.objects.some(ref=>ref.kind==="database-table"&&ref.id==="flow_ip_meta")) + assert.ok(evidence.objects.some(ref=>ref.kind==="database-table"&&ref.id==="flow_buckets")) +} +for (const change of [ + (f: ReturnType) => { f.connection.policy.status = "paused" }, + (f: ReturnType) => { f.connection.policy.scope.allowGlobal = false }, + (f: ReturnType) => { f.connection.policy.scope.serverIds = [1] }, + (f: ReturnType) => { f.connection.owner_permissions = [] }, + (f: ReturnType) => { f.connection.generation = "changed" }, + (f: ReturnType) => { f.configuration.future = "drift" }, + (f: ReturnType) => f.catalog(), (f: ReturnType) => f.protect(), +]) { + const f = fixture(), request = input(await f.prepare()); change(f) + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 0) +} +{ + const f = fixture(), request = input(await f.prepare()); f.before(() => f.protect()) + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 0) +} +{ + const f = fixture(), request = input(await f.prepare()); f.after(() => { f.connection.policy.status = "paused" }) + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 1) +} +{ + const f = fixture(), request = input(await f.prepare()); request.receiptId = randomUUID() + await assert.rejects(f.adapter.service.execute(request)); assert.equal(f.writes, 0) +} +console.log("NetFlow settings adapter: strict full patch, human/global/catalog, encrypted source, protections/revoke, sanitized full ACK receipt and no VERIFIED adoption; injected dependencies only") + +{ + const f=fixture(),request=input(await f.prepare()) + await assert.rejects(f.adapter.service.execute({settings:{topN:100}})) + await assert.rejects(f.adapter.service.execute(f.legacy(request))) + await assert.rejects(f.adapter.service.execute(f.legacy(request,2))) + assert.equal(f.writes,0) +} diff --git a/backend/src/modules/mcp/flow-settings-actions.ts b/backend/src/modules/mcp/flow-settings-actions.ts new file mode 100644 index 00000000..e1d83244 --- /dev/null +++ b/backend/src/modules/mcp/flow-settings-actions.ts @@ -0,0 +1,116 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { readFlowHostKeysConfiguration, flowSettingsPrivateProof } from "../../services/traffic-flow-host-keys-service.js" +import { updateTrafficFlowSettingsWithListener, flowSettingsAffectedObjects } from "../../services/traffic-flow-settings-service.js" +import { flowSettingsPatchSchema } from "../../services/traffic-flow-settings-execution.js" +import { reviewFlowSettingsPlan } from "./flow-settings-review.js" +import { effectiveMcpConnection } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, redact, McpError } from "./security.js" +import { registerMcpService, type McpServiceHandler } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { FlowSettingsPlan } from "../../services/traffic-flow-settings-execution.js" +import type { add } from "./actions.js" + +const operation = "netflow.settings.update" +export const flowSettingsActionSchema = z.object({ settings: flowSettingsPatchSchema }).strict() +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const ids = z.array(z.number().int().positive().safe()).max(1000).refine(value => value.every((id, index) => index === 0 || value[index - 1] < id)) +const bindingSchema = z.object({ + operation: z.literal(operation), version: z.literal(3), receiptId: z.string().uuid(), + principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: hash, policyProof: hash, generation: z.string().min(1), serverIds: ids, + beforeProof: hash, protectionProof: hash, patch: flowSettingsPatchSchema, + before: z.record(z.string(), z.unknown()).refine(value => typeof value.hostPrivateKey === "string" && typeof value.hostPublicKey === "string"), +}).strict() +type Binding = z.infer +const publicState = (binding: Binding, proof: string) => ({ proof, review: reviewFlowSettingsPlan({ command: { kind: "patch", patch: binding.patch }, before: binding.before as FlowSettingsPlan["before"] }), patch: binding.patch, enabledAfter: binding.patch.enabled ?? binding.before.enabled }) +const inputSchema = z.object({ receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(524288) }).strict() +const receiptSchema = z.object({ ok: z.literal(true), receiptId: z.string().uuid(), status: z.literal("applied"), configurationConfirmed: z.literal(true), listener: z.literal("request-acknowledged"), listenerReadinessConfirmed: z.literal(false), afterProof: hash, receiptConfirmed: z.literal(true), attempts: z.literal(2), retries: z.literal(0), privateMaterialExported: z.literal(false), automaticReplay: z.literal(false) }).strict() +const conflict = () => new McpError("STATE_CONFLICT", "Конфигурация, защиты или допуск изменения настроек NetFlow изменились", 409) +const production = { + connection: effectiveMcpConnection, read: readFlowHostKeysConfiguration, update: updateTrafficFlowSettingsWithListener, + seal: encryptSecret, open: decryptSecret, proof: flowSettingsPrivateProof, verifiedAccess: assertVerifiedConfigurationAccess, + query: async (sql: string, values: unknown[] = []) => (await pool.query(sql, values)).rows as Record[], +} +export function createFlowSettingsAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const catalog = async () => ids.parse((await deps.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).map(row => Number(row.id))) + const protections = async (serverIds: number[]) => { + const rows = await deps.query(`SELECT object_ref,xmin::text AS revision FROM mcp_protections WHERE object_ref->>'serverId'=ANY($1::text[]) OR (object_ref->>'kind'='server' AND object_ref->>'id'=ANY($1::text[])) OR EXISTS(SELECT 1 FROM jsonb_array_elements($2::jsonb) AS target WHERE target->>'kind'=object_ref->>'kind' AND target->>'id'=object_ref->>'id') OR (object_ref->>'kind'='database-partition' AND object_ref->>'id' ~ '^flow_(buckets|minute_stats|minute_dims|daily_dims|hour_facts|daily_facts)/') ORDER BY id LIMIT 1001`, [serverIds.map(String), JSON.stringify(flowSettingsAffectedObjects)]) + if (rows.length > 1000) throw conflict() + return { proof: await deps.proof(rows), objects: rows.map(row => row.object_ref as ChangeEvidence["objects"][number]) } + } + const authorize = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + const current = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || Date.parse(current.policy.expiresAt) <= Date.now() || current.owner_valid_until.getTime() <= Date.now() || !current.policy.scope.allowGlobal || !current.policy.scope.operationIds.includes(operation) || binding.serverIds.some(id => !current.policy.scope.serverIds.includes(id)) || !hasPermission(current.owner_permissions, "mm:traffic:write") || !hasPermission(current.policy.permissions, "mm:traffic:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска изменения настроек NetFlow", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof || fingerprint(await catalog()) !== fingerprint(binding.serverIds) || (await protections(binding.serverIds)).proof !== binding.protectionProof) throw conflict() + signal?.throwIfAborted() + } + const request = async (raw: Record) => { + const input = inputSchema.parse(raw), binding = bindingSchema.parse(JSON.parse(await deps.open(input.bindingCiphertext))) + if (binding.receiptId !== input.receiptId || await deps.proof(binding.before) !== binding.beforeProof) throw conflict() + return binding + } + const current = async (binding: Binding, external?: () => Promise) => { + const check = () => authorize(binding, undefined, external) + await check(); const configuration = await deps.read(check); await check() + return { configuration, proof: await deps.proof(configuration) } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + const { settings: patch } = flowSettingsActionSchema.parse(raw); context.signal?.throwIfAborted() + const serverIds = await catalog(), protection = await protections(serverIds) + const binding: Binding = { operation, version: 3, receiptId: randomUUID(), principal: context.principal, + ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, + serverIds, protectionProof: protection.proof, beforeProof: "0".repeat(64), before: {}, patch } + const check = () => authorize(binding, context.signal) + await check(); binding.before = await deps.read(check); binding.beforeProof = await deps.proof(binding.before); await check() + return { target: { scope: "global" }, snapshots: [], requiresHuman: true, mutationCost: 4, verificationScope: "configuration", + objects: [...flowSettingsAffectedObjects, ...serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), ...protection.objects], + before: publicState(binding,binding.beforeProof), steps: [{ kind: "service", service: operation, input: { receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(bindingSchema.parse(binding))) } }], + warnings: [ + "Каждый patch, включая no-op и настройки отображения, сохраняет полный REST-сценарий: остановка collector, drain/flush, глобальные retention/top-N и обслуживание RIPE cache, затем запуск при enabled=true.", + "План затрагивает весь каталог серверов, flow store, partition DDL и RIPE cache. Полная конфигурация с ключами, peers и будущими полями зашифрована. Закреплённые агентом адрес, префикс и порты менять нельзя.", + "При успешном enabled=true collector продолжает обычные UDP/SQL и RIPE enrichment. VERIFIED подтверждает конфигурацию и сохранённую квитанцию завершения запроса запуска, не UDP readiness. RouterOS, SSH, host-agent apply, экспорт файлов и уведомления не выполняются.", + "Бюджет — до четырёх публикаций: один CAS настроек и до трёх owned flow writes — flush завершающегося worker, основной force flush и начальный force flush нового worker. Фазы команды — две; автоматических повторов нет. Конкурентный collector блокирует дополнительную остановку или запуск.", + "Конфигурация и listener имеют отдельные постоянные intents. Неизвестный или частичный исход не повторяется автоматически. Совпадение настроек без сохранённого VERIFIED не подтверждает выполнение.", + ] } + } + + const matches = (value: unknown, binding: Binding, proof: string) => { + const receipt = receiptSchema.safeParse(value) + return receipt.success && receipt.data.receiptId === binding.receiptId && receipt.data.afterProof === proof + } + + const service: McpServiceHandler = { + current: async input => { const binding = await request(input); return publicState(binding,(await current(binding)).proof) }, + execute: async input => { + const binding = await request(input), before = await current(binding) + if (before.proof !== binding.beforeProof) throw conflict() + const check = async () => { await deps.verifiedAccess(); await authorize(binding) } + const expected: FlowSettingsPlan = { command: { kind: "patch", patch: binding.patch }, before: before.configuration } + const receipt = await deps.update(binding.patch, { receiptId: binding.receiptId, expected, authorize: check }) + const after = await current(binding) + if (!matches(receipt, binding, after.proof)) throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Результат изменения настроек не подтверждён; повтор запрещён", 503) + return receipt + }, + verify: async (input, result) => { const binding = await request(input); return matches(result, binding, (await current(binding)).proof) }, + reconcile: async (input, external) => { + const binding = await request(input), latest = await current(binding, external) + const rows = await deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId]) + const row = rows[0] + const verified = rows.length === 1 && row.connection_id === binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && matches(row.result, binding, latest.proof) + if ((await current(binding, external)).proof !== latest.proof) throw conflict() + return { matchesDesiredState: verified, matchesOriginalState: latest.proof === binding.beforeProof } + }, + } + return { prepare, service } +} +export function registerFlowSettingsActions(register: typeof add) { + const adapter = createFlowSettingsAdapter() + registerMcpService(operation, adapter.service) + register(operation, "mm_netflow_settings_update_plan", "Изменить настройки и перезапустить collector NetFlow", "traffic", "mm:traffic:write", "write", "global", flowSettingsActionSchema, { version: 3, prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/flow-settings-review.ts b/backend/src/modules/mcp/flow-settings-review.ts new file mode 100644 index 00000000..dc387512 --- /dev/null +++ b/backend/src/modules/mcp/flow-settings-review.ts @@ -0,0 +1,25 @@ +import type { FlowSettingsPlan } from "../../services/traffic-flow-settings-execution.js" + +/** Public review only. The caller must seal the full plan as private operation + * state and bind its digest/version to the reviewed step. No key/file material. */ +export function reviewFlowSettingsPlan(plan: FlowSettingsPlan) { + const patch = plan.command.kind === "patch" + return { + operation: patch ? "netflow.settings.update" : "traffic.flow.settings.generate-keys", + scope: "global" as const, + requiresHumanApproval: true, + changedFields: plan.command.kind === "patch" ? Object.keys(plan.command.patch).sort() : ["hostPublicKey", "hostPrivateKey"], + effects: patch ? ["persist-settings", "stop-current-collector", "drain-and-flush-pending-flow-data", "global-retention-and-top-n-pruning", "ripe-cache-maintenance", "start-collector-if-enabled", "enabled-collector-udp-sql-and-ripe-enrichment"] + : ["install-host-key-pair-only-if-incomplete"], + collectorRestartRequired: patch, + ownedPublicationBudget: patch ? 4 : 1, + listenerReadinessConfirmed: false, + privateMaterialExported: false, + hostAgentApply: false, + routerWrites: false, + ssh: false, + s3: false, + hostFilesWritten: false, + notificationsScheduled: false, + } +} diff --git a/backend/src/modules/mcp/flow-source-scope.ts b/backend/src/modules/mcp/flow-source-scope.ts new file mode 100644 index 00000000..a60b6f44 --- /dev/null +++ b/backend/src/modules/mcp/flow-source-scope.ts @@ -0,0 +1,15 @@ +import { readBoundedFlowServerRows,getBoundedFlowServerRowById } from "../../services/traffic-flow-server-source.js" +import { assertTrafficHistoryAccess } from "./traffic-history-actions.js" +import { fingerprint,McpError } from "./security.js" +import type { ActionContext } from "./action-types.js" +export async function assertBoundedFlowSource(context:ActionContext,server:NonNullable>>){ + await assertTrafficHistoryAccess(context) + const fresh=await getBoundedFlowServerRowById(server.id) + if(!fresh||fingerprint(fresh)!==fingerprint(server))throw new McpError("STATE_CONFLICT","Конфигурация источника истории изменилась во время чтения",409) +} +export async function flowCatalogDevices(input:Record,context:ActionContext,options:{includeDisabled?:boolean}={}){ + const ids=input.serverId?[Number(input.serverId)]:context.connection.policy.scope.serverIds + const eligible=(await readBoundedFlowServerRows(ids)).filter(row=>options.includeDisabled||row.enabled) + const start=Number(input.serverOffset),selected=eligible.slice(start,start+Number(input.serverLimit)) + return {selected,serverIds:selected.map(row=>row.id),totalServers:eligible.length,nextServerOffset:start+selected.lengthnew McpError(code,message,409) +async function authorize(input:Record,signal?:AbortSignal){ + signal?.throwIfAborted() + const principal=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict().parse(input.principal) + const connection=await effectiveMcpConnection(principal) + if(connection.owner_id!==input.ownerId||!connection.policy.scope.allowGlobal||!hasPermission(connection.policy.permissions,"mm:traffic:write")||!hasPermission(connection.owner_permissions,"mm:traffic:write"))throw fail("Право обновления GeoIP отозвано","PERMISSION_DENIED") + if(fingerprint(connection.policy)!==input.policyProof)throw fail("Допуск изменился; подготовьте новый план","RESET_REQUIRED") + signal?.throwIfAborted() + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>Number(row.id)) + if(ids.length>1000||ids.some(id=>!Number.isSafeInteger(id)||id<=0||!connection.policy.scope.serverIds.includes(id)))throw fail("Обновление GeoIP требует все серверы приложения","SERVER_DENIED") + signal?.throwIfAborted() + return ids +} +async function source(input:Record,signal?:AbortSignal){ + const ids=await authorize(input,signal),state=await readGeoipManualUpdateState(signal) + if(state.running)throw fail("Обновление GeoIP уже выполняется") + if(fingerprint(await authorize(input,signal))!==fingerprint(ids))throw fail("Каталог серверов изменился при чтении GeoIP") + return {ids,state,publicState:{proof:fingerprint({ids,state}),files:state.files,settings:{enabled:state.settings.enabled,updateIntervalSec:state.settings.updateIntervalSec}}} +} +export function registerGeoipUpdateActions(register:typeof add){ + const current=async(input:Record)=>(await source(input)).publicState + registerMcpService("geoip.update",{current, + execute:async input=>{ + const before=await source(input) + if(fingerprint(before.publicState)!==input.beforeFingerprint)throw fail("GeoIP или каталог серверов изменились после подтверждения") + let dispatched=false + const fence=async()=>{ + await assertVerifiedConfigurationAccess() + if(fingerprint(await authorize(input))!==fingerprint(before.ids))throw fail("Каталог серверов изменился перед обновлением GeoIP") + const {settings}=await readGeoipManualMetadata() + if(settings.enabled!==before.state.settings.enabled||settings.updateIntervalSec!==before.state.settings.updateIntervalSec)throw fail("Настройки GeoIP изменились перед dispatch") + await assertVerifiedConfigurationAccess() + } + try{ + const result=await updateGeoipManually({beforeDispatch:fence,onDispatch:()=>{dispatched=true},beforeStart:async()=>{ + if(fingerprint((await source(input)).publicState)!==input.beforeFingerprint)throw fail("GeoIP изменился перед запуском") + }}) + await fence() + const after=await source(input),snapshot=result.snapshot + return {completed:true,ok:result.ok,checked:snapshot.checked,downloaded:snapshot.downloaded,skippedUnchanged:snapshot.skippedUnchanged,bytes:snapshot.bytes,errorCount:snapshot.errors.length,fatalError:Boolean(snapshot.fatalError),stateProof:after.publicState.proof,attempts:1,automaticReplay:false} + }catch(error){ + if(dispatched)throw new McpError("MUTATION_OUTCOME_UNKNOWN","Обновление GeoIP началось, итог не подтверждён. Повтор запрещён; требуется сверка локальных баз и метаданных",502) + throw error + } + }, + verify:async(input,result)=>{const receipt=result as {completed?:boolean;ok?:boolean;stateProof?:string}|null;return receipt?.completed===true&&receipt.ok===true&&(await current(input)).proof===receipt.stateProof}, + reconcile:async(input,allow)=>{ + await allow();await source(input);await allow() + // The mirror result was unknown at planning time. Local files/metadata + // cannot attribute a later change to this attempt or prove no request ran. + return {matchesDesiredState:false,matchesOriginalState:false} + }, + }) + register("geoip.update","mm_geoip_update_plan","Принудительно проверить и обновить локальные базы GeoIP","traffic","mm:traffic:write","write","global",z.object({}).strict(),{ + prepare:async(input,context)=>{ + const request={...input,principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy)},before=await source(request,context.signal) + const objects=[{kind:"geoip-update",id:"1"},{kind:"geoip-settings",id:"1"},...before.ids.map(serverId=>({kind:"server",id:String(serverId),serverId}))] + const protections=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' IN ('geoip-update','geoip-settings') LIMIT 1001",[before.ids.map(String)]) + if(protections.rows.length>1000)throw fail("Превышен предел защит GeoIP","STATE_UNAVAILABLE") + for(const {object_ref:ref} of protections.rows)objects.push(ref) + if(fingerprint((await source(request,context.signal)).publicState)!==fingerprint(before.publicState))throw fail("GeoIP изменился при подготовке") + return {target:{scope:"global"},objects,snapshots:[],before:before.publicState,requiresHuman:true,mutationCost:1,verificationScope:"configuration", + steps:[{kind:"service",service:"geoip.update",input:{...request,beforeFingerprint:fingerprint(before.publicState)}}], + warnings:["После человеческого подтверждения запускается принудительное обновление Country и ASN с фиксированного зеркала GitHub/P3TERX, даже если расписание отключено. До двух GET, каждый до 64 MiB и 120 секунд; redirect зеркала обрабатывается fetch.","ETag/304 сохраняет проверенную базу; новые файлы валидируются и заменяются отдельно. Возможен частичный результат, изменяются локальные файлы, readers и метаданные SQL для всего приложения.","Подтверждаются счётчики завершённой попытки и локальное состояние, а не актуальность зеркала. Ошибки не запускают повтор автоматически.","После потери подтверждения результат unknown. Read-only сверка не доказывает авторство обновления или отсутствие сетевого запроса и оставляет исход unresolved; новый подтверждённый план может повторить скачивание."]} + }, + }) +} diff --git a/backend/src/modules/mcp/global-reconciliation-actions.test.ts b/backend/src/modules/mcp/global-reconciliation-actions.test.ts new file mode 100644 index 00000000..14a8e295 --- /dev/null +++ b/backend/src/modules/mcp/global-reconciliation-actions.test.ts @@ -0,0 +1,73 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import type { McpAction, ActionContext } from "./action-types.js" +import { registerGlobalReconciliationActionsForTests } from "./global-reconciliation-actions.js" + +type Register = Parameters[0] +type Dependencies = Parameters[1] + +function fixture() { + const connectionId = randomUUID(), operationId = randomUUID(), ownerId = "current-retired-owner" + const context = { principal: { connectionId }, connection: { id: connectionId, owner_id: ownerId, owner_permissions: ["mm:servers:write"], + policy: { permissions: ["mm:servers:write"], scope: { allowGlobal: true, operationIds: ["servers.poll", "operations.global.reconcile"], serverIds: [1, 2] } } } } as ActionContext + let action!: Pick + let history: { affectedServerIds: number[] } | undefined = { affectedServerIds: [1, 2] } + let revoke = false, retiredCalls = 0, live = false + const deps = { + connection: async () => { + if (revoke) throw Error("revoked-current-grant") + return structuredClone(context.connection) + }, + query: async (sql: string) => { + if (sql.startsWith("SELECT id FROM servers")) return [{ id: 1 }] + if (live) return [{ id: operationId, state: "applied", operation_id: "servers.poll", evidence: { sealed: "live" } }] + return [] + }, + operation: (id: string) => ({ id, scope: "global", requiredPermissions: ["mm:servers:write"] }), + service: () => ({ reconcile: async () => ({ matchesDesiredState: true, matchesOriginalState: false }) }), + decrypt: async () => JSON.stringify({ target: { scope: "global" }, objects: [{ serverId: 1 }], steps: [{ kind: "service", service: "servers.poll", input: {} }] }), + retired: async (identity, authorize) => { + retiredCalls++ + assert.deepEqual(identity, { operationId, connectionId, ownerId }) + await authorize() + return history + }, + } satisfies Omit & Partial> + registerGlobalReconciliationActionsForTests(((...args: Parameters) => { action = args[8] }) as Register, deps as unknown as Dependencies) + return { context, operationId, history: (value: typeof history) => { history = value }, live: () => { live = true }, + revoke: () => { revoke = true }, calls: () => retiredCalls, read: () => action.read!({ operationId }, context), + target: () => action.target({ operationId }, context) } +} + +test("global action reconciles retired poll only under current ownership and returns no archived material", async () => { + const f = fixture() + assert.deepEqual(await f.target(), { scope: "global" }) + const result = await f.read() as { observedAt: string } + const { observedAt, ...rest } = result + assert(Number.isFinite(Date.parse(observedAt))) + assert.deepEqual(rest, { operationId: f.operationId, state: "applied", automaticRetry: false, + results: [{ stepIndex: 0, service: "servers.poll", matchesDesiredState: true, matchesOriginalState: false }] }) + assert.equal(f.calls(), 2) +}) + +test("global action refuses missing or inaccessible retired history", async () => { + const f = fixture() + f.history(undefined) + await assert.rejects(f.read(), { code: "NOT_FOUND" }) + f.history({ affectedServerIds: [1, 2] }) + f.context.connection.policy.scope.serverIds = [1] + await assert.rejects(f.read(), { code: "SERVER_DENIED" }) + f.context.connection.policy.scope.serverIds = [1, 2] + f.context.connection.policy.scope.operationIds = ["operations.global.reconcile"] + await assert.rejects(f.read(), { code: "PERMISSION_DENIED" }) + f.revoke() + await assert.rejects(f.read(), /revoked-current-grant/) +}) + +test("global action uses the live reconciliation path while original source exists", async () => { + const f = fixture() + f.live() + await f.read() + assert.equal(f.calls(), 0) +}) diff --git a/backend/src/modules/mcp/global-reconciliation-actions.ts b/backend/src/modules/mcp/global-reconciliation-actions.ts new file mode 100644 index 00000000..74f89878 --- /dev/null +++ b/backend/src/modules/mcp/global-reconciliation-actions.ts @@ -0,0 +1,91 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { readRetiredServerPollHistory } from "../../services/server-poll-history-reader.js" +import { hasPermission } from "../../lib/permissions.js" +import { effectiveMcpConnection } from "./store.js" +import { decryptSecret, fingerprint, McpError } from "./security.js" +import { findMcpOperation } from "./registry.js" +import { findMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { ConnectionRow } from "./store.js" + +const production = { + connection: effectiveMcpConnection, + query: async (sql: string, values: unknown[] = []) => (await pool.query(sql, values)).rows, + operation: findMcpOperation, + service: findMcpService, + decrypt: decryptSecret, + retired: readRetiredServerPollHistory, +} +type Dependencies = typeof production +const notFound = () => new McpError("NOT_FOUND", "Операция не найдена", 404) + +async function currentAccess(context: ActionContext, deps: Dependencies) { + context.signal?.throwIfAborted() + const current = await deps.connection(context.principal) + if (!current.policy.scope.allowGlobal || fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) throw new McpError("PERMISSION_DENIED", "Глобальный допуск сверки изменился", 403) + const ids = (await deps.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).map(row => Number(row.id)) + if (ids.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Каталог глобальной сверки превышает лимит", 409) + if (ids.some(id => !current.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Глобальная сверка требует доступа ко всему каталогу серверов", 403) + return current +} + +function requireOriginal(actionId: string, current: ConnectionRow, deps: Dependencies) { + const original = deps.operation(actionId) + if (!original || original.scope !== "global" || !current.policy.scope.operationIds.includes(original.id) || original.requiredPermissions.some(permission => !hasPermission(current.policy.permissions, permission) || !hasPermission(current.owner_permissions, permission))) throw new McpError("PERMISSION_DENIED", "Утрачено право на исходную глобальную операцию", 403) +} + +function requireServerScope(declared: number[], current: ConnectionRow) { + if (declared.some(id => !current.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Зависимость операции вне текущего глобального допуска", 403) +} + +async function authorizedSource(operationId: string, context: ActionContext, deps: Dependencies) { + const current = await currentAccess(context, deps) + const row = (await deps.query("SELECT o.id,o.state,p.operation_id,p.evidence FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id WHERE o.id=$1 AND o.connection_id=$2", [operationId, current.id]))[0] + if (!row) { + const authorize = async () => { + const fresh = await currentAccess(context, deps) + if (fresh.id !== current.id || fresh.owner_id !== current.owner_id) throw notFound() + } + const archived = await deps.retired({ operationId, connectionId: current.id, ownerId: current.owner_id }, authorize, + { deadlineAt: Date.now() + 10000, signal: context.signal }) + context.signal?.throwIfAborted() + if (!archived) throw notFound() + requireOriginal("servers.poll", current, deps) + requireServerScope(archived.affectedServerIds, current) + return { kind: "retired-poll" as const } + } + requireOriginal(row.operation_id, current, deps) + const evidence = JSON.parse(await deps.decrypt(row.evidence.sealed)) as ChangeEvidence + if (evidence.target.scope !== "global" || !evidence.steps.length || evidence.steps.some(step => step.kind !== "service" || !deps.service(step.service)?.reconcile)) throw new McpError("UNSUPPORTED_CAPABILITY", "Для глобальной операции нет безопасной read-only сверки", 409) + requireServerScope(evidence.objects.flatMap(object => object.serverId === undefined ? [] : [object.serverId]), current) + if (["pending", "running"].includes(row.state)) throw new McpError("STATE_CONFLICT", "Операция ещё выполняется", 409) + context.signal?.throwIfAborted() + return { kind: "live" as const, row, evidence } +} + +function registerActions(register: typeof add, deps: Dependencies) { + register("operations.global.reconcile", "mm_global_operation_reconcile", "Сверить глобальную сервисную операцию без повторной записи", "mcp", null, "read", "global", z.object({ operationId: z.string().uuid() }).strict(), { + target: async (input, context) => { await authorizedSource(String(input.operationId), context, deps); return { scope: "global" } }, + read: async (input, context) => { + const operationId = String(input.operationId) + const source = await authorizedSource(operationId, context, deps) + if (source.kind === "retired-poll") return { operationId, state: "applied", observedAt: new Date().toISOString(), + results: [{ stepIndex: 0, service: "servers.poll", matchesDesiredState: true, matchesOriginalState: false }], automaticRetry: false } + const { row, evidence } = source + const authorize = async () => { if ((await authorizedSource(operationId, context, deps)).kind !== "live") throw notFound() } + const results = [] + for (const [index, step] of evidence.steps.entries()) { + if (step.kind !== "service") throw new McpError("UNSUPPORTED_CAPABILITY", "Нативная глобальная сверка недоступна", 409) + await authorize() + results.push({ stepIndex: index, service: step.service, ...await deps.service(step.service)!.reconcile!(step.input, authorize) }) + } + await authorize() + return { operationId: row.id, state: row.state, observedAt: new Date().toISOString(), results, automaticRetry: false } + }, + }) +} + +export const registerGlobalReconciliationActions = (register: typeof add) => registerActions(register, production) +export const registerGlobalReconciliationActionsForTests = registerActions diff --git a/backend/src/modules/mcp/gre-mutation-actions.ts b/backend/src/modules/mcp/gre-mutation-actions.ts new file mode 100644 index 00000000..38a47c15 --- /dev/null +++ b/backend/src/modules/mcp/gre-mutation-actions.ts @@ -0,0 +1,63 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { greCreateFieldsSchema, greUpdateFieldsSchema, greMutationSchema, type GreMutation } from "../../services/gre-mutation-input.js" +import { greMutationProof, greTarget, readGreMutationState, type GreMutationState } from "../../services/gre-mutation-state.js" +import { mutateGreTunnel, planGreMutation } from "../../services/gre-mutation.js" +import { routerClient } from "./resources.js" +import { fingerprint, decryptSecret, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" + +const sid = z.number().int().positive().safe(), key = { rosId:z.string().regex(/^\*[A-Za-z0-9]+$/).optional(), name:z.string().trim().min(1).max(100).optional() } +const secret = { ipsecSecretRef:z.string().uuid().optional(), clearIpsecSecret:z.boolean().optional() } +const schemas = [ + z.object({serverId:sid,fields:greCreateFieldsSchema.omit({ipsecSecret:true}),...secret}).strict(), + z.object({serverId:sid,...key,fields:greUpdateFieldsSchema.omit({ipsecSecret:true}),...secret}).strict(), + z.object({serverId:sid,...key}).strict(), +] +export async function greMutationProtectionObjects(serverId:number,state:GreMutationState){ + const objects:ChangeEvidence["objects"]=[{kind:"server",id:String(serverId),serverId},...state.gre.map(row=>({kind:"gre",id:row[".id"],serverId}))] + for(const[kind,rows]of [["ipv4_address",state.ipv4],["ipv6_address",state.ipv6]]as const)for(const row of rows)objects.push({kind,id:row[".id"],serverId}) + const protections=await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001",[String(serverId)]) + if(protections.rows.length>1000)throw new McpError("STATE_UNAVAILABLE","Превышен предел защит GRE",503) + for(const{object_ref:ref}of protections.rows)objects.push({kind:ref.kind,id:ref.id,serverId}) + return objects +} +export function registerGreMutationActions(register: typeof add) { + for (const [index, operation] of ["create","update","delete"].entries()) { + const service = "network.gre."+operation + const current = async (input:Record) => greMutationProof(await readGreMutationState((await routerClient(Number(input.serverId))).server,greMutationSchema.parse(input.mutation))) + registerMcpService(service,{current,execute:async input=>{ + const {tunnel: _tunnel,...receipt} = await mutateGreTunnel((await routerClient(Number(input.serverId))).server,greMutationSchema.parse(input.mutation),String(input.beforeFingerprint)) + return receipt + },verify:async(input,result)=>{ + const receipt = result as {verified?:boolean;afterFingerprint?:string}|null + return receipt?.verified===true && fingerprint(await current(input))===receipt.afterFingerprint + }}) + register(service,"mm_gre_tunnel_"+operation+"_plan","План "+["создания","изменения","удаления"][index]+" GRE и его внутренних адресов", "network","mm:network:write","write","servers",schemas[index],{ + prepare:async(input,context)=>{ + const serverId = Number(input.serverId), {server} = await routerClient(serverId) + let mutation = greMutationSchema.parse({operation,...(operation==="create"?{fields:input.fields}:{...("rosId" in input?{rosId:input.rosId}:{}),...("name" in input?{name:input.name}:{}),...(operation==="update"?{fields:input.fields}:{})})}) + const state = await readGreMutationState(server,mutation), target = greTarget(mutation,state) + if (input.ipsecSecretRef && input.clearIpsecSecret) throw new McpError("INVALID_INPUT","Выберите новый PSK или явное удаление",400) + if (mutation.operation!=="delete" && (input.ipsecSecretRef || input.clearIpsecSecret)) { + const name = mutation.operation==="create"?mutation.fields.name:target?.name + let value = "" + if (input.ipsecSecretRef) { + const {rows:[secret]} = await pool.query("SELECT * FROM mcp_secrets WHERE id=$1 AND created_by=$2",[input.ipsecSecretRef,context.connection.owner_id]) + if (!name || !secret || secret.purpose!==`gre-ipsec:${serverId}:${name}` || Number(secret.server_id)!==serverId) throw new McpError("SECRET_UNAVAILABLE","PSK не выдан этому GRE/серверу") + value = await decryptSecret(secret.ciphertext) + } + mutation = greMutationSchema.parse({...mutation,fields:{...mutation.fields,ipsecSecret:value}}) as GreMutation + } + const ops = planGreMutation(server,mutation,state) + if (!ops.length || ops.length>50) throw new McpError("STATE_CONFLICT","Операция GRE должна содержать от 1 до 50 команд",409) + const before = greMutationProof(state), objects=await greMutationProtectionObjects(serverId,state) + return {target:{scope:"servers",serverIds:[serverId]},objects,snapshots:[],before,mutationCost:ops.length+1,requiresHuman:true, + steps:[{kind:"service",service,input:{serverId,mutation,beforeFingerprint:fingerprint(before)}}], + warnings:["GRE и статические IPv4/IPv6 адреса меняются одним сервисным планом, но не атомарно. Во время подготовки туннель отключается; включение выполняется последним шагом и может прервать соединения.","Проверяются native ID, все зарегистрированные защиты сервера, адреса и зависимости. Используемый GRE не удаляется; каскадного изменения маршрутов/протоколов/привязок нет.","Состояние сверяется перед каждой командой и после неё. Потеря ответа, игнорирование команды или дрейф останавливают операцию без повтора; автоматического отката нет.",...(input.clearIpsecSecret?["Запрошено явное удаление IPsec PSK."]:input.ipsecSecretRef?["Запрошена установка/замена IPsec PSK из секрета владельца для этого сервера и имени GRE."]:[]),"Remote inner IP — только метаданные комментария; связность, IPsec SA, MTU пути и forwarding не подтверждаются. Статус записи полной ревизии возвращается отдельно."]} + }, + }) + } +} diff --git a/backend/src/modules/mcp/gre-native-actions.ts b/backend/src/modules/mcp/gre-native-actions.ts new file mode 100644 index 00000000..7024b524 --- /dev/null +++ b/backend/src/modules/mcp/gre-native-actions.ts @@ -0,0 +1,26 @@ +import { greNativeMutationSchema, mutateGreNativeTunnel, planGreNativeMutation, readGreNativeMutationState } from "../../services/gre-native-mutation.js" +import { greMutationProof } from "../../services/gre-mutation-state.js" +import { fingerprint } from "./security.js" +import { routerClient } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import { greMutationProtectionObjects } from "./gre-mutation-actions.js" +import type { ChangeEvidence } from "./action-types.js" + +export function registerGreNativeServices(){ + for(const operation of ["update","delete"]as const){ + const current=async(input:Record)=>greMutationProof(await readGreNativeMutationState((await routerClient(Number(input.serverId))).server,greNativeMutationSchema.parse(input.mutation))) + registerMcpService("network.gre.native."+operation,{current,execute:async input=>mutateGreNativeTunnel((await routerClient(Number(input.serverId))).server,greNativeMutationSchema.parse(input.mutation),String(input.beforeFingerprint)),verify:async(input,result)=>{ + const receipt=result as {verified?:boolean;afterFingerprint?:string}|null + return receipt?.verified===true&&fingerprint(await current(input))===receipt.afterFingerprint + }}) + } +} +export async function prepareGreNativeChange(serverId:number,operation:"update"|"delete",id:string|undefined,fields:unknown):Promise{ + const mutation=greNativeMutationSchema.parse({operation,id,...(operation==="update"?{fields}:{})}),{server}=await routerClient(serverId) + const state=await readGreNativeMutationState(server,mutation) + planGreNativeMutation(mutation,state) + const before=greMutationProof(state) + return{target:{scope:"servers",serverIds:[serverId]},before,snapshots:[],objects:await greMutationProtectionObjects(serverId,state),mutationCost:2,requiresHuman:true, + steps:[{kind:"service",service:"network.gre.native."+operation,input:{serverId,mutation,beforeFingerprint:fingerprint(before)}}], + warnings:["Native GRE изменяет только сам интерфейс; адреса и зависимости не удаляются каскадно. Для rename/delete сначала снимите обнаруженные ссылки отдельными планами.","Перед записью проверяются полный видимый native proof, серверные DB-привязки, интерфейсные/маршрутные/протокольные и Firewall зависимости и зарегистрированные защиты сервера. Counters и runtime не являются конфигурацией.","После единственной native-команды проверяется точный эффект без изменения остальных объектов. Потерянный ответ или игнорирование команды не повторяются. Это проверка конфигурации, не связности; запись ревизии возвращается отдельно."]} +} diff --git a/backend/src/modules/mcp/gre-revision-actions.ts b/backend/src/modules/mcp/gre-revision-actions.ts new file mode 100644 index 00000000..16e65a8b --- /dev/null +++ b/backend/src/modules/mcp/gre-revision-actions.ts @@ -0,0 +1,83 @@ +import {randomUUID} from "node:crypto" +import {z} from "zod" +import {pool} from "../../db/index.js" +import {hasPermission} from "../../lib/permissions.js" +import {assertVerifiedConfigurationAccess} from "../../services/config-mutation-policy.js" +import {readGreRestoreRevision,readGreRestoreState,greRestoreStateProof,materializeGreRestore,validateGreRestoreSnapshot,planGreRevisionRestore,restoreGreRevision} from "../../services/gre-revision-restore.js" +import {greMutationProtectionObjects} from "./gre-mutation-actions.js" +import {effectiveMcpConnection} from "./store.js" +import {routerClient} from "./resources.js" +import {registerMcpService} from "./service-registry.js" +import {encryptSecret,decryptSecret,fingerprint,McpError} from "./security.js" +import type {add} from "./actions.js" +import type {ActionContext} from "./action-types.js" + +const service="network.gre.revision.restore" +const principalSchema=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict() +const fail=(message:string,code="STATE_CONFLICT")=>new McpError(code,message,409) +function request(input:Record,context:ActionContext){return{...input,principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy)}} +async function authorize(input:Record,signal?:AbortSignal,verified=false){ + signal?.throwIfAborted();if(verified)await assertVerifiedConfigurationAccess() + const current=await effectiveMcpConnection(principalSchema.parse(input.principal)),serverId=Number(input.serverId) + if(current.owner_id!==input.ownerId||current.policy.mode==="observer"||!hasPermission(current.policy.permissions,"mm:network:write")||!hasPermission(current.owner_permissions,"mm:network:write"))throw fail("Право восстановления GRE отозвано","PERMISSION_DENIED") + if(!current.policy.scope.serverIds.includes(serverId)||!current.policy.scope.operationIds.includes(service))throw fail("GRE restore вне разрешённой области","SERVER_DENIED") + if(fingerprint(current.policy)!==input.policyProof)throw fail("Допуск GRE restore изменился","RESET_REQUIRED") + signal?.throwIfAborted() +} +async function readState(input:Record,signal?:AbortSignal,verified=false){ + const access=()=>authorize(input,signal,verified) + await access();const {server}=await routerClient(Number(input.serverId)) + try{ + const state=await readGreRestoreState(server,access,signal) + await access();return{server,state,access,stateProof:await greRestoreStateProof(state)} + }catch(error){if(error instanceof McpError)throw error;throw fail("Состояние GRE restore недоступно","STATE_UNAVAILABLE")} +} +async function read(input:Record,signal?:AbortSignal,verified=false){ + await authorize(input,signal,verified) + try{ + const revision=await readGreRestoreRevision(Number(input.serverId),String(input.revisionId),()=>authorize(input,signal,verified)) + const source=await readState(input,signal,verified) + return {...source,revision,publicState:{stateProof:source.stateProof,revisionProof:revision.revisionProof}} + }catch(error){if(error instanceof McpError)throw error;throw fail("Ревизия или состояние GRE restore недоступны","STATE_UNAVAILABLE")} +} +export function registerGreRevisionActions(register:typeof add){ + registerMcpService(service,{ + current:async input=>(await read(input)).publicState, + execute:async input=>{ + const source=await read(input,undefined,true) + if(source.stateProof!==input.beforeProof||source.revision.revisionProof!==input.revisionProof)throw fail("GRE или ревизия изменились после подтверждения") + try{ + const desired=validateGreRestoreSnapshot(JSON.parse(await decryptSecret(z.object({ciphertext:z.string()}).strict().parse(input.desired).ciphertext))) + const {tunnels:_tunnels,...receipt}=await restoreGreRevision(source.server,String(input.revisionId),{beforeProof:String(input.beforeProof),revisionProof:String(input.revisionProof),desired,authorize:source.access}) + return receipt + }catch(error){if(error instanceof McpError)throw error;throw fail("Результат GRE restore не подтверждён; нужна сверка без повторной записи","MUTATION_OUTCOME_UNKNOWN")} + }, + verify:async(input,result)=>{ + const receipt=result as {verified?:boolean;afterFingerprint?:string}|null + // Appending rollback history may prune the source revision. Verify the + // confirmed whole native/SQL state without requiring that old row again. + return receipt?.verified===true&&(await readState(input)).stateProof===receipt.afterFingerprint + }, + reconcile:async(input,allowed)=>{ + await allowed();const source=await readState(input) + const rows=(await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->>'service'=$2 AND s.intended_state->'input'->>'receiptId'=$3 AND s.result->>'verified'='true' LIMIT 2",[principalSchema.parse(input.principal).connectionId,service,input.receiptId])).rows + await allowed();await source.access() + const fresh=await readState(input) + if(fresh.stateProof!==source.stateProof)throw fail("GRE изменился во время сверки") + const after=rows.length===1?rows[0].result?.afterFingerprint:undefined + return{matchesOriginalState:fresh.stateProof===input.beforeProof,matchesDesiredState:typeof after==="string"&&fresh.stateProof===after,verifiedFinalCheckpointAvailable:typeof after==="string",forwardingVerified:false} + }, + }) + register(service,"mm_gre_revision_restore_plan","Восстановить GRE и все статические адреса из ревизии после подтверждения","network","mm:network:write","write","servers",z.object({serverId:z.number().int().positive().safe(),revisionId:z.string().uuid()}).strict(),{ + prepare:async(input,context)=>{ + const req=request(input,context),source=await read(req,context.signal) + const desired=materializeGreRestore(source.revision.desired,source.state),steps=planGreRevisionRestore(desired,source.state) + const objects=await greMutationProtectionObjects(source.server.id,source.state) + await source.access() + const review={nativeCommands:steps.length,remove:source.state.gre.filter(row=>!desired.tunnels.some(t=>t.name===row.name)).map(row=>({name:row.name,id:row[".id"]})),restore:desired.tunnels.map(row=>({name:row.name,create:!source.state.gre.some(t=>t.name===row.name),localAddress:row.localAddress,remoteAddress:row.remoteAddress,enabled:!row.disabled,mtu:row.mtu,addresses:row.innerAddresses?.map(a=>({address:a.address,enabled:!a.disabled})),ipsecConfigured:row.ipsecSecret.length>0}))} + return{target:{scope:"servers",serverIds:[source.server.id]},objects,snapshots:[],before:source.publicState,requiresHuman:true,mutationCost:steps.length+1, + steps:[{kind:"service",service,input:{...req,review,beforeProof:source.stateProof,revisionProof:source.revision.revisionProof,receiptId:randomUUID(),desired:{ciphertext:await encryptSecret(JSON.stringify(desired))}}}], + warnings:["Восстанавливается весь раздел GRE сервера: отсутствующие в ревизии туннели и их статические адреса удаляются. Используемый GRE не удаляется; зависимости не изменяются каскадно.","До изменения адресов затронутые туннели отключаются; включение выполняется последним. Команды последовательные, не атомарные и могут прервать соединения.","Маскированный или пустой PSK старой ревизии сохраняет подтверждённый текущий ключ. Создание с неизвестным ключом запрещено; явное удаление PSK доступно отдельным планом GRE.","Перед каждой командой и после durable intent проверяются область, источники, защиты и вся конфигурация. Неизвестный ответ не повторяется, автоматического отката нет.","Сверка без записи сравнивает исходный proof и сохранённый проверенный конечный checkpoint. Без конечного checkpoint результат не объявляется достигнутым; новое восстановление требует нового человеческого плана.","Проверяется конфигурация; IPv6 readiness, IPsec SA и forwarding не подтверждаются. Запись rollback-ревизии отражается отдельно в receipt."]} + }, + }) +} diff --git a/backend/src/modules/mcp/gre-revision-http.test.ts b/backend/src/modules/mcp/gre-revision-http.test.ts new file mode 100644 index 00000000..8edd4151 --- /dev/null +++ b/backend/src/modules/mcp/gre-revision-http.test.ts @@ -0,0 +1,66 @@ +import assert from "node:assert/strict" +import {createServer} from "node:http" +import {pool,closePool} from "../../db/index.js" +import {applySqlMigrations} from "../../db/migrate.js" +import {updateMcpSettings} from "./store.js" +import {acceptGreRevisionRestore} from "../../test/mcp-gre-revision.js" +import type {AuthUser} from "../../lib/permissions.js" + +let collections:Record[]>={},dependencies:Record[]>={} +let writes=0,reads=0,nextId=0,ignored=false,lost=false,afterCommand:(()=>Promise)|undefined +const native=createServer(async(req,res)=>{ + res.setHeader("content-type","application/json") + const path=decodeURIComponent((req.url??"").slice(5)),bases=["/interface/gre","/ip/address","/ipv6/address"],base=bases.find(p=>p===path||path.startsWith(p+"/")) + reads++ + if(req.method==="GET"){ + const rows=base?(collections[base]??[]).map(row=>Object.fromEntries(Object.entries(row).filter(([k,v])=>(k!=="comment"||v!=="")&&(base!=="/interface/gre"||k!=="dynamic")))):path==="/interface"?dependencies[path]??[{".id":"*E",name:"ether1",type:"ether"},...(collections["/interface/gre"]??[]).map(row=>({".id":row[".id"],name:row.name,type:"gre",...(row.dynamic===undefined?{}:{dynamic:row.dynamic})}))]:dependencies[path]??[] + res.end(JSON.stringify(rows));return + } + let text="";for await(const chunk of req)text+=chunk + const data=text?JSON.parse(text):{} + if(path==="/interface/get"){ + const row=dependencies[path]?.find(row=>row[".id"]===data[".id"]) + if(row&&data["value-name"]==="dynamic"){res.end(JSON.stringify(row.ret==="empty-array"?[]:{ret:row.ret}));return} + res.statusCode=400;res.end(JSON.stringify({error:400,detail:"Property not available"}));return + } + if(path==="/interface/print"){ + assert.deepEqual(data[".query"],[".id="+dependencies["/interface/get"]?.[0]?.[".id"],data[".query"]?.includes("-dynamic")?"-dynamic":"dynamic","#&"]) + const key=data[".query"]?.includes("-dynamic")?"/interface/static-print":"/interface/dynamic-print" + res.end(JSON.stringify(dependencies[key]??[]));return + } + if(base&&path.endsWith("/get")){ + const row=(collections[base]??[]).find(r=>r[".id"]===data[".id"]) + res.end(JSON.stringify({ret:row?.[data["value-name"]]??(["comment","ipsec-secret","from-pool"].includes(data["value-name"])?"":"false")}));return + } + assert.ok(base,"Only typed GRE/address writes allowed") + writes++ + if(base==="/interface/gre"&&data["ipsec-secret"]===""&&["yes","true"].includes(data["allow-fast-path"])){ + res.statusCode=400;res.end(JSON.stringify({error:400,detail:"failure: cannot enable fastpath together with ipsec yet"}));return + } + const fields=Object.fromEntries(Object.entries(data).map(([k,v])=>[k,v==="yes"?"true":v==="no"?"false":String(v)])) + if(!ignored){ + if(req.method==="PUT"){ + const row=base==="/interface/gre"?{".id":"*G"+(++nextId),"local-address":"0.0.0.0",mtu:"1476",keepalive:"10s,10",dscp:"inherit",disabled:"false",dynamic:"false",running:"false","clamp-tcp-mss":"true","allow-fast-path":"true","ipsec-secret":"",comment:"",...fields}:{".id":"*A"+(++nextId),disabled:"false",dynamic:"false",invalid:"false",advertise:"false","no-dad":"false","eui-64":"false","from-pool":"",comment:"",...fields} + ;(collections[base]??=[]).push(row) + }else if(req.method==="PATCH")Object.assign(collections[base].find(r=>r[".id"]===path.slice(base.length+1))!,fields) + else if(req.method==="DELETE")collections[base]=collections[base].filter(r=>r[".id"]!==path.slice(base.length+1)) + else assert.fail("Unsupported native write") + } + await afterCommand?.() + if(lost){lost=false;req.socket.destroy();return} + res.end("{}") +}) +await new Promise(resolve=>native.listen(0,"127.0.0.1",resolve)) +try{ + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n,0,"Disposable empty SQL database required") + const address=native.address();assert.ok(address&&typeof address!=="string") + const actor:AuthUser={id:"gre-revision-http-owner",apps:["mm"],permissions:["mm:settings:admin","mm:network:admin","mm:servers:admin"],expiresAt:Date.now()+3600000} + await updateMcpSettings({enabled:true},actor) + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl) VALUES(1,'foreign','127.0.0.1',$1,false),(45,'gre-revision','127.0.0.1',$1,false)",[address.port]) + await acceptGreRevisionRestore(actor,{get:()=>collections,set:v=>{collections=v},writes:()=>writes,reads:()=>reads,ignored:v=>{ignored=v},lost:v=>{lost=v},afterCommand:v=>{afterCommand=v},dependencies:v=>{dependencies=v}}) +}finally{ + // A following dump/restore test may start collectors. Retired fixture sockets + // must never be advertised as enabled devices in the restored database. + try{await pool.query("UPDATE servers SET enabled=false WHERE id IN(1,45)")}finally{await new Promise(resolve=>native.close(()=>resolve()));await closePool()} +} diff --git a/backend/src/modules/mcp/host-files-actions.ts b/backend/src/modules/mcp/host-files-actions.ts new file mode 100644 index 00000000..fbe1d26a --- /dev/null +++ b/backend/src/modules/mcp/host-files-actions.ts @@ -0,0 +1,39 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { accessGeneration, fingerprint, McpError } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import { registerMcpService } from "./service-registry.js" +import { createHostFilesArtifact, hostExportAuthority, hostExportAuthoritySchema, readHostExportSource, verifyHostFilesArtifact } from "./host-files-artifact.js" +import type { add } from "./actions.js" + +export function registerHostFilesActions(register: typeof add) { + const authority=(input:Record)=>hostExportAuthoritySchema.parse(input.authority) + const current=async(input:Record)=>({proof:(await readHostExportSource(authority(input))).proof}) + registerMcpService("traffic.flow.hostFiles.export",{current, + execute:async input=>createHostFilesArtifact(authority(input),String(input.expectedProof)), + verify:async(input,result)=>{ + const receipt=result as {artifactId?:string;humanDownloadOnly?:boolean;keyMaterialReturned?:boolean}|null + return receipt?.humanDownloadOnly===true && receipt.keyMaterialReturned===false && !!receipt.artifactId && await verifyHostFilesArtifact(receipt.artifactId,authority(input),String(input.expectedProof)) + }, + }) + register("traffic.flow.hostFiles.export","mm_netflow_host_files_export_plan","Подготовить владельцу зашифрованный пакет файлов коллектора NetFlow","traffic","mm:traffic:write","write","global",z.object({}).strict(),{ + prepare:async(_input,context)=>{ + const authorize=async()=>{ + if(context.signal?.aborted)throw new McpError("READ_CANCELLED","Подготовка экспорта отменена",409) + const fresh=await effectiveMcpConnection(context.principal) + if(fingerprint(fresh.policy)!==fingerprint(context.connection.policy))throw new McpError("RESET_REQUIRED","Допуск изменился; подготовьте экспорт заново",409) + } + await authorize() + const access=hostExportAuthority(context.principal,context.connection.owner_id,context.connection.policy,await accessGeneration()) + const state=await readHostExportSource(access,authorize) + const objects=[{kind:"netflow-host-files",id:"1"},...state.serverIds.map(serverId=>({kind:"server",id:String(serverId),serverId}))] + const protections=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[])) LIMIT 1001",[state.serverIds.map(String)]) + if(protections.rows.length>1000)throw new McpError("STATE_UNAVAILABLE","Превышен предел защит экспорта",409) + for(const {object_ref:ref} of protections.rows)objects.push(ref) + await authorize() + return {target:{scope:"global"},objects,snapshots:[],before:{proof:state.proof},requiresHuman:true,mutationCost:1, + steps:[{kind:"service",service:"traffic.flow.hostFiles.export",input:{authority:access,expectedProof:state.proof,beforeFingerprint:fingerprint({proof:state.proof})}}], + warnings:["Пакет содержит приватный ключ WireGuard коллектора и конфигурацию всех его peers. После подтверждения доступен только вошедшему владельцу через web API, 15 минут.","MCP получает только ссылку и контрольную сумму; содержимое пакета нейросети не возвращается. Установка и применение файлов не выполняются.","Отзыв допуска, изменение прав, серверного каталога, ключей или конфигурации блокирует скачивание. Потребуется новый подтверждённый экспорт."]} + }, + }) +} diff --git a/backend/src/modules/mcp/host-files-artifact.ts b/backend/src/modules/mcp/host-files-artifact.ts new file mode 100644 index 00000000..00ecc14f --- /dev/null +++ b/backend/src/modules/mcp/host-files-artifact.ts @@ -0,0 +1,118 @@ +import { createHash, randomUUID, timingSafeEqual } from "node:crypto" +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { buildTrafficFlowHostFilesBundle } from "../../services/traffic-flow-host-files.js" +import { assertVerifiedConfigurationAccess, executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { assertLogArtifactBudget } from "./log-artifacts.js" +import { connectionWrite, effectiveMcpConnection, type McpPrincipal } from "./store.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint } from "./security.js" + +const MAX_BYTES = 1048576, MAX_CIPHER = 2 * 1048576 +const digest = z.string().regex(/^[a-f0-9]{64}$/) +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.iso.datetime() }).strict() +export const hostExportAuthoritySchema = z.object({ principal: principalSchema, ownerId: z.string().min(1).max(200), policyProof: digest, generation: digest }).strict() +export type HostExportAuthority = z.infer +const scopeSchema = z.object({ version: z.literal(1), kind: z.literal("netflow-host-files"), authority: hostExportAuthoritySchema, sourceProof: digest, + bytes: z.number().int().min(1).max(MAX_BYTES), sha256: digest, encryptedSha256: digest, proof: digest }).strict() +type Header = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date } +type Meta = Header & { access_scope: z.infer; encrypted_bytes: number } +const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "netflow-host-files-artifact-v1") +const signed = (header: Header, scope: Omit, "proof">) => ({ id:header.id,connection_id:header.connection_id,filename:header.filename,media_type:header.media_type,expires_at:header.expires_at.toISOString(),scope }) +const fail = (message: string, code = "ARTIFACT_INVALID") => new McpError(code, message, 409) + +export async function assertHostExportAccess(raw: unknown, client: Pick = pool) { + const authority = hostExportAuthoritySchema.parse(raw), connection = await effectiveMcpConnection(authority.principal, client) + if (connection.owner_id !== authority.ownerId || !connection.policy.scope.allowGlobal || !hasPermission(connection.owner_permissions,"mm:traffic:write") || !hasPermission(connection.policy.permissions,"mm:traffic:write") || !connection.policy.scope.operationIds.includes("traffic.flow.hostFiles.export")) throw new McpError("PERMISSION_DENIED","Нет полного допуска к экспорту ключей коллектора",403) + if (fingerprint(connection.policy) !== authority.policyProof || await accessGeneration() !== authority.generation) throw fail("Допуск изменился; подготовьте экспорт заново","RESET_REQUIRED") + const ids = (await client.query<{ id: number }>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>row.id) + if (ids.length>1000 || ids.some(id=>!connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED","Экспорт коллектора требует все серверы приложения",403) + return { connection, serverIds: ids } +} + +export async function readHostExportSource(authority: HostExportAuthority, authorize: () => Promise = async()=>{}) { + await authorize() + const access = await assertHostExportAccess(authority) + const row = (await pool.query<{ collectorIp: string; prefix: string; flowListenPort: number; wgListenPort: number; hostPrivateKey: string; peers: unknown }>(`SELECT collector_ip AS "collectorIp",prefix,flow_listen_port AS "flowListenPort",wg_listen_port AS "wgListenPort", + host_private_key AS "hostPrivateKey",CASE WHEN octet_length(peers_json::text)<=1048576 THEN peers_json ELSE NULL END AS peers FROM traffic_flow_settings WHERE id=1`)).rows[0] + if (!row || !Array.isArray(row.peers) || row.peers.length>1000 || row.peers.some(peer=>!peer || !Number.isSafeInteger(peer.serverId) || !access.serverIds.includes(peer.serverId))) throw fail("Не подтверждён полный набор peers коллектора","STATE_UNAVAILABLE") + await authorize();await assertHostExportAccess(authority) + let files + try { files = await buildTrafficFlowHostFilesBundle(row) } catch { throw fail("Файлы коллектора не сформированы; проверьте конфигурацию","STATE_UNAVAILABLE") } + const content = JSON.stringify({ version: 1, kind: "netflow-host-files", files },null,2)+"\n" + if (Buffer.byteLength(content)>MAX_BYTES) throw fail("Пакет файлов превышает 1 МиБ","EXPORT_LIMIT") + await authorize() + const final = await assertHostExportAccess(authority) + if (fingerprint(final.serverIds)!==fingerprint(access.serverIds)) throw fail("Каталог серверов изменился во время экспорта","STATE_CONFLICT") + return { content, serverIds: access.serverIds, proof: await sign({ serverIds: access.serverIds, content }) } +} + +export async function createHostFilesArtifact(authority: HostExportAuthority, expectedProof: string) { + const authorize = assertVerifiedConfigurationAccess + return executeGuardedApplicationWrite({kind:"netflow-host-files",id:"1"},(await assertHostExportAccess(authority)).serverIds,async()=>{ + const source = await readHostExportSource(authority,authorize) + if (source.proof!==expectedProof) throw fail("Файлы или ключи коллектора изменились после подтверждения","STATE_CONFLICT") + const ciphertext = await encryptSecret(source.content) + if(Buffer.byteLength(ciphertext)>MAX_CIPHER) throw fail("Зашифрованный пакет превышает предел","EXPORT_LIMIT") + const header: Header = { id: randomUUID(), connection_id: authority.principal.connectionId, filename: "netflow-host-files.json", media_type: "application/json", expires_at: new Date(Date.now()+900000) } + const access = { version: 1 as const, kind: "netflow-host-files" as const, authority, sourceProof: source.proof, bytes: Buffer.byteLength(source.content), sha256: hash(source.content), encryptedSha256: hash(ciphertext) } + const scope = scopeSchema.parse({...access,proof:await sign(signed(header,access))}) + await authorize() + if ((await readHostExportSource(authority,authorize)).proof!==source.proof) throw fail("Конфигурация изменилась во время формирования пакета","STATE_CONFLICT") + await connectionWrite(async client=>{ + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE",[header.connection_id]) + await assertHostExportAccess(authority,client) + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))") + await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()") + const usage = (await client.query(`SELECT count(*) FILTER(WHERE connection_id=$1)::text AS files,COALESCE(sum(octet_length(ciphertext)) FILTER(WHERE connection_id=$1),0)::text AS own_bytes,COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts`,[header.connection_id])).rows[0] + assertLogArtifactBudget(BigInt(usage.files),BigInt(usage.own_bytes),BigInt(usage.total_bytes),Buffer.byteLength(ciphertext)) + await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)",[header.id,header.connection_id,header.filename,header.media_type,ciphertext,JSON.stringify(scope),header.expires_at]) + }) + await authorize();await assertHostExportAccess(authority) + return {artifactId:header.id,downloadPath:`/api/mcp/artifacts/${header.id}`,expiresInSeconds:900,bytes:access.bytes,sha256:access.sha256,humanDownloadOnly:true,keyMaterialReturned:false,configurationApplied:false} + }) +} + +async function loadMeta(id: string): Promise { + const row = (await pool.query("/* MCP host artifact metadata */ SELECT id,connection_id,filename,media_type,expires_at,access_scope,octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND expires_at>now()",[id])).rows[0] + if (!row) throw new McpError("NOT_FOUND","Пакет отсутствует или срок истёк",404) + const scope = scopeSchema.safeParse(row.access_scope) + if (!scope.success || !(row.expires_at instanceof Date) || !Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes>MAX_CIPHER) throw fail("Метаданные пакета не подтверждены") + const {proof,...access} = scope.data, expected = await sign(signed(row,access)) + if (!timingSafeEqual(Buffer.from(proof,"hex"),Buffer.from(expected,"hex")) || row.connection_id!==access.authority.principal.connectionId) throw fail("Подпись пакета не совпала") + return {...row,access_scope:scope.data} +} +export async function verifyHostFilesArtifact(id: string, authority: HostExportAuthority, sourceProof: string) { + const meta=await loadMeta(id) + await assertHostExportAccess(authority) + if(fingerprint(meta.access_scope.authority)!==fingerprint(authority) || meta.access_scope.sourceProof!==sourceProof)return false + const payload=(await pool.query<{ciphertext:string}>("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152",[id,meta.access_scope.proof])).rows[0] + await assertHostExportAccess(authority) + return !!payload && hash(payload.ciphertext)===meta.access_scope.encryptedSha256 && (await readHostExportSource(authority)).proof===sourceProof +} +export async function readHumanHostFilesArtifact(id: string, actor: AuthUser) { + const meta=await loadMeta(id), scope=meta.access_scope + const authorize=async()=>{ + if(actor.id!==scope.authority.ownerId || !hasPermission(actor.permissions,"mm:traffic:write")) throw new McpError("PERMISSION_DENIED","Пакет с ключами доступен только владельцу с правом настройки NetFlow",403) + await assertHostExportAccess(scope.authority) + if(meta.expires_at.getTime()<=Date.now()) throw fail("Срок пакета истёк") + } + await authorize() + if((await readHostExportSource(scope.authority,authorize)).proof!==scope.sourceProof) throw fail("Файлы или ключи коллектора изменились; подготовьте новый пакет","STATE_CONFLICT") + const payload=(await pool.query<{ciphertext:string}>("/* MCP host artifact payload */ SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152",[id,scope.proof])).rows[0] + await authorize() + if(!payload || hash(payload.ciphertext)!==scope.encryptedSha256) throw fail("Зашифрованный пакет изменился") + let content: string + try{content=await decryptSecret(payload.ciphertext)}catch{throw fail("Пакет не расшифрован")} + await authorize() + if(Buffer.byteLength(content)!==scope.bytes || hash(content)!==scope.sha256) throw fail("Размер или SHA-256 пакета не совпал") + if((await readHostExportSource(scope.authority,authorize)).proof!==scope.sourceProof) throw fail("Конфигурация изменилась во время выдачи пакета","STATE_CONFLICT") + await authorize() + return {filename:meta.filename,mediaType:meta.media_type,content} +} + +export function hostExportAuthority(principal: McpPrincipal, ownerId: string, policy: unknown, generation: string) { + return hostExportAuthoritySchema.parse({principal,ownerId,policyProof:fingerprint(policy),generation}) +} diff --git a/backend/src/modules/mcp/human-artifact-receipts.test.ts b/backend/src/modules/mcp/human-artifact-receipts.test.ts new file mode 100644 index 00000000..14495c36 --- /dev/null +++ b/backend/src/modules/mcp/human-artifact-receipts.test.ts @@ -0,0 +1,147 @@ +import assert from "node:assert/strict" +import { humanArtifactReceipts } from "./human-artifact-receipts.js" +import { fingerprint, redact } from "./security.js" + +const id = "fa58fa40-4bcb-4616-9316-cb69463b3150", connectionId = "fa58fa40-4bcb-4616-9316-cb69463b3151" +const actor = { id: "owner", email: "owner@example.invalid", name: "Receipt fixture", apps: ["mm"], permissions: ["mm:backups:write"] } +const access = { kind: "backup-download", authority: { ownerId: actor.id, principal: { connectionId } }, sourceProof: "a".repeat(64), bytes: 20, sha256: "b".repeat(64) } +const receipt = { artifactId: id, bytes: 20, sha256: access.sha256, humanDownloadOnly: true, backupContentReturned: false, downloadPath: "https://unsafe.test", secret: "NEVER_RETURN" } +const row = { operation_id: id, connection_id: connectionId, step_state: "verified", service: "backups.download", receipt, access_scope: access, input: { authority: access.authority, expectedProof: access.sourceProof } } +const operations = [{ id, connection_id: connectionId, state: "applied" }] +let checked = 0 +const deps = (rows = [row]) => ({ query: async (sql: string, values: unknown[]) => { + assert.match(sql, /c.owner_id=\$2/); assert.match(sql, /s.state='verified'/); assert.match(sql, /a.connection_id=o.connection_id/); assert.match(sql, /a.expires_at>now\(\)/); assert.match(sql, /LIMIT 101/); assert.deepEqual(values, [[id], actor.id]); return { rows } +}, checkClaims: async () => { checked++ }, authorize: async () => access }) +const output = await humanArtifactReceipts(operations, actor, deps()) +assert.deepEqual(output.get(id), [{ artifactId: id, bytes: 20, sha256: access.sha256, humanDownloadOnly: true, backupContentReturned: false }]) +assert.equal(checked, 1) +assert.ok(!JSON.stringify([...output]).includes("NEVER_RETURN")) +assert.ok(!JSON.stringify([...output]).includes("unsafe.test")) +for (const bad of [ + { ...row, step_state: "applied" }, { ...row, service: "unknown" }, { ...row, operation_id: connectionId }, + { ...row, connection_id: id }, { ...row, receipt: { ...receipt, artifactId: "../unsafe" } }, + { ...row, receipt: { ...receipt, humanDownloadOnly: false } }, { ...row, receipt: { ...receipt, backupContentReturned: true } }, + { ...row, receipt: { ...receipt, bytes: 9 * 1048576 } }, + { ...row, access_scope: { ...access, authority: { ...access.authority, ownerId: "foreign" } } }, + { ...row, input: { ...row.input, expectedProof: "tampered" } }, +]) assert.equal((await humanArtifactReceipts(operations, actor, deps([bad]))).size, 0) +assert.equal((await humanArtifactReceipts(operations, actor, { ...deps(), authorize: async () => { throw Error("revoked") } })).size, 0) +assert.equal((await humanArtifactReceipts(operations, actor, { ...deps(), authorize: async () => ({ ...access, bytes: 21 }) })).size, 0) +assert.equal((await humanArtifactReceipts(operations, actor, { ...deps(), checkClaims: async () => { throw Error("stale JWT") } })).size, 0) +assert.equal((await humanArtifactReceipts(operations, actor, deps(Array.from({ length: 101 }, () => row)))).size, 0) +assert.equal((await humanArtifactReceipts(operations, { ...actor, permissions: [] }, { ...deps(), query: async () => { throw Error("must not query") } })).size, 0) +console.log("human artifact receipt projection tests passed") + +const databaseActor = { ...actor, permissions: ["mm:settings:admin"] } +const databaseAccess = { ...access, kind: "database-backup", bytes: 64 * 1048576, attemptRef: id } +const databaseReceipt = { artifactId: id, bytes: databaseAccess.bytes, sha256: access.sha256, humanDownloadOnly: true, databaseContentReturned: false } +const databaseRow = { ...row, service: "database.backup", receipt: databaseReceipt, access_scope: databaseAccess, input: { ...row.input, attemptRef: id } } +const databaseDeps = { ...deps([databaseRow as unknown as typeof row]), authorize: async (_id: string, _actor: unknown, kind?: string) => { assert.equal(kind, "database-backup"); return databaseAccess } } +assert.deepEqual((await humanArtifactReceipts(operations, databaseActor, databaseDeps)).get(id), [databaseReceipt]) +assert.equal((await humanArtifactReceipts(operations, actor, databaseDeps)).size, 0) +for (const bad of [ + { ...databaseRow, receipt: { ...databaseReceipt, databaseContentReturned: true } }, + { ...databaseRow, receipt: { ...databaseReceipt, bytes: 64 * 1048576 + 1 } }, + { ...databaseRow, receipt: { ...receipt, bytes: 20 } }, + { ...databaseRow, access_scope: { ...databaseAccess, kind: "backup-download" } }, +]) assert.equal((await humanArtifactReceipts(operations, databaseActor, { ...databaseDeps, query: async () => ({ rows: [bad] }) })).size, 0) +console.log("database artifact receipt projection tests passed") + +const networkActor = { ...actor, permissions: ["mm:network:write"] } +const wireguardAccess = { ...access, kind: "wireguard-export", bytes: 1048576 } +const wireguardReceipt = { artifactId: id, bytes: wireguardAccess.bytes, sha256: access.sha256, humanDownloadOnly: true, wireguardContentReturned: false } +const wireguardRow = { ...row, service: "network.wireguard.export", receipt: { ...wireguardReceipt, content: "PRIVATE_CONFIG_MUST_NOT_LEAVE", downloadPath: "https://unsafe.test" }, access_scope: wireguardAccess } +const wireguardDeps = { ...deps([wireguardRow as unknown as typeof row]), authorize: async (_id: string, _actor: unknown, kind?: string) => { assert.equal(kind, "wireguard-export"); return wireguardAccess } } +assert.deepEqual((await humanArtifactReceipts(operations, networkActor, wireguardDeps)).get(id), [wireguardReceipt]) +for (const wrongActor of [actor, databaseActor, { ...actor, permissions: ["mm:network:read"] }]) assert.equal((await humanArtifactReceipts(operations, wrongActor, wireguardDeps)).size, 0) +for (const bad of [ + { ...wireguardRow, receipt: { ...wireguardReceipt, wireguardContentReturned: true } }, + { ...wireguardRow, receipt: { ...wireguardReceipt, bytes: 1048577 } }, + { ...wireguardRow, receipt: { ...wireguardReceipt, bytes: 0 } }, + { ...wireguardRow, receipt: receipt }, + { ...wireguardRow, access_scope: { ...wireguardAccess, kind: "backup-download" } }, + { ...wireguardRow, input: { authority: { ...access.authority, request: { serverId: 99 } }, expectedProof: access.sourceProof } }, +]) assert.equal((await humanArtifactReceipts(operations, networkActor, { ...wireguardDeps, query: async () => ({ rows: [bad] }) })).size, 0) +assert.equal((await humanArtifactReceipts(operations, networkActor, { ...wireguardDeps, authorize: async () => { throw Error("grant revoked") } })).size, 0) +assert.equal((await humanArtifactReceipts([{ ...operations[0], state: "unknown" }], networkActor, { ...wireguardDeps, query: async () => { throw Error("unknown must not query") } })).size, 0) +console.log("WireGuard artifact receipt projection: owner network-write permission, exact kind/proof/size, unknown and private payload exclusion passed") + +// Metadata-only test: the option is a boolean; no file payload is created or downloaded. +const optionAuthority = { ...access.authority, request: { serverId: 143, interfaceName: "wg-export", format: "rsc", includePrivateKey: true } } +const optionAccess = { ...wireguardAccess, authority: optionAuthority } +const optionRow = { ...wireguardRow, access_scope: optionAccess, input: { authority: redact(optionAuthority), expectedProof: access.sourceProof } } +assert.deepEqual((await humanArtifactReceipts(operations, networkActor, { ...wireguardDeps, query: async () => ({ rows: [optionRow] }), authorize: async () => optionAccess })).get(id), [wireguardReceipt]) +assert.equal((await humanArtifactReceipts(operations, networkActor, { ...wireguardDeps, query: async () => ({ rows: [{ ...optionRow, input: { ...optionRow.input, expectedProof: "changed-content-proof" } }] }), authorize: async () => optionAccess })).size, 0) + +const ipsecInput = { serverId: 206, receiptId: id, bindingCiphertext: "encrypted-private-binding" } +const ipsecAccess = { kind: "ipsec-user-bundle", authority: { ...access.authority, serverId: 206, receiptId: id, inputProof: fingerprint(ipsecInput) }, afterProof: "c".repeat(64), bytes: 128, sha256: access.sha256 } +const ipsecReceipt = { artifactId: id, bytes: 128, sha256: access.sha256, humanDownloadOnly: true, ipsecContentReturned: false } +const ipsecRow = { ...row, service: "network.ipsec.users.create", input: ipsecInput, access_scope: ipsecAccess, receipt: { ...ipsecReceipt, historyRecorded: true, revisionRecorded: true, afterProof: ipsecAccess.afterProof, content: "PRIVATE_PKCS12", passphrase: "PRIVATE_PASSPHRASE" } } +const ipsecDeps = { query: async () => ({ rows: [ipsecRow] }), checkClaims: async () => {}, authorize: async (_id: string, _actor: unknown, kind?: string) => { assert.equal(kind, "ipsec-user-bundle"); return ipsecAccess } } +assert.deepEqual((await humanArtifactReceipts(operations, networkActor, ipsecDeps)).get(id), [ipsecReceipt]) +for (const bad of [{ ...ipsecRow, input: { ...ipsecInput, receiptId: connectionId } }, { ...ipsecRow, receipt: { ...ipsecRow.receipt, historyRecorded: false } }, { ...ipsecRow, receipt: { ...ipsecRow.receipt, afterProof: "changed" } }, { ...ipsecRow, receipt: { ...ipsecRow.receipt, ipsecContentReturned: true } }]) assert.equal((await humanArtifactReceipts(operations, networkActor, { ...ipsecDeps, query: async () => ({ rows: [bad] }) })).size, 0) +assert.equal((await humanArtifactReceipts(operations, networkActor, { ...ipsecDeps, authorize: async () => { throw Error("expired/revoked") } })).size, 0) +console.log("IPsec bundle receipt projection: opaque binding, exact history/proof, private payload exclusion passed") + +// Inactive export assembly: fake metadata and authorization only. No artifact, +// private bundle, RouterOS command, production activation or SQL pool is used. +const existingInput = { serverId: 207, receiptId: connectionId, bindingCiphertext: "[redacted]" } +const existingAccess = { version: 1, kind: "ipsec-existing-certificate-bundle", authority: { ...access.authority, + request: { serverId: 207, privateKeyExport: true, passphrase: { kind: "generate" } }, receiptId: connectionId, + certificateId: "*a1", inputProof: fingerprint(redact(existingInput)), expectedProof: "d".repeat(64) }, + afterProof: "d".repeat(64), bytes: 256, sha256: "e".repeat(64) } +const existingSafeReceipt = { artifactId: id, bytes: 256, sha256: existingAccess.sha256, humanDownloadOnly: true, ipsecContentReturned: false } +const existingResult = { ...existingSafeReceipt, ok: true, verified: true, receiptId: connectionId, serverId: 207, certificateId: "*a1", + commandCount: 2, completedCommands: 2, confirmedWrites: 2, afterProof: existingAccess.afterProof, automaticReplay: false, + forwardingVerified: false, expiresInSeconds: 900, downloadPath: `/api/mcp/artifacts/${id}`, + contents: "NEVER_EXISTING_CONTENTS", privateKey: "NEVER_EXISTING_KEY", passphrase: "NEVER_EXISTING_PASSPHRASE", + unknownDetails: { certificateName: "NEVER_EXISTING_DETAILS" } } +const existingRow = { ...row, service: "network.ipsec.existing-certificates.export", receipt: existingResult, input: existingInput, access_scope: existingAccess } +let existingClaims = 0, existingAuthorizations = 0 +const existingDeps = { query: async (sql: string, values: unknown[]) => { + assert.match(sql, /'network\.ipsec\.existing-certificates\.export'/) + assert.match(sql, /s.intended_state->>'service'=p.operation_id/) + assert.doesNotMatch(sql, /SELECT\s+(?:a\.)?ciphertext|pg_dump|pg_restore/i) + assert.deepEqual(values, [[id], networkActor.id]) + return { rows: [existingRow] } +}, checkClaims: async () => { existingClaims++ }, authorize: async (artifactId: string, actualActor: unknown, kind?: string) => { + existingAuthorizations++; assert.equal(artifactId, id); assert.equal(actualActor, networkActor) + assert.equal(kind, "ipsec-existing-certificate-bundle"); return existingAccess +} } +const existingOutput = await humanArtifactReceipts(operations, networkActor, existingDeps) +assert.deepEqual(existingOutput.get(id), [existingSafeReceipt]) +assert.equal(existingClaims, 1); assert.equal(existingAuthorizations, 1) +assert.doesNotMatch(JSON.stringify([...existingOutput]), /NEVER_|contents|privateKey|passphrase|unknownDetails|downloadPath|afterProof|certificateId/) +for (const wrongActor of [actor, databaseActor, { ...actor, permissions: ["mm:network:read"] }]) { + assert.equal((await humanArtifactReceipts(operations, wrongActor, existingDeps)).size, 0) +} +for (const patch of [ + { ok: false }, { verified: false }, { humanDownloadOnly: false }, { ipsecContentReturned: true }, { ipsecContentReturned: "false" }, + { commandCount: 1 }, { completedCommands: 1 }, { confirmedWrites: 1 }, { automaticReplay: true }, { forwardingVerified: true }, + { receiptId: id }, { serverId: 208 }, { certificateId: "*a2" }, { afterProof: "f".repeat(64) }, + { bytes: 0 }, { bytes: 1048577 }, { bytes: "256" }, { sha256: "f".repeat(64) }, { expiresInSeconds: 901 }, + { artifactId: "../unsafe" }, { downloadPath: "https://unsafe.test" }, { downloadPath: `/api/mcp/artifacts/${connectionId}` }, +]) { + const bad = { ...existingRow, receipt: { ...existingResult, ...patch } } + assert.equal((await humanArtifactReceipts(operations, networkActor, { ...existingDeps, query: async () => ({ rows: [bad] }) })).size, 0) +} +for (const [index, bad] of [ + { ...existingRow, service: "network.ipsec.users.create" }, { ...existingRow, service: "network.ipsec.existing-certificates.export.other" }, + { ...existingRow, step_state: "applied" }, { ...existingRow, input: { ...existingInput, serverId: 208 } }, + { ...existingRow, access_scope: { ...existingAccess, version: 2 } }, + { ...existingRow, access_scope: { ...existingAccess, kind: "ipsec-user-bundle" } }, + { ...existingRow, access_scope: { ...existingAccess, afterProof: "f".repeat(64) } }, + { ...existingRow, access_scope: { ...existingAccess, authority: { ...existingAccess.authority, inputProof: "f".repeat(64) } } }, + { ...existingRow, access_scope: { ...existingAccess, authority: { ...existingAccess.authority, expectedProof: "f".repeat(64) } } }, + { ...existingRow, access_scope: { ...existingAccess, authority: { ...existingAccess.authority, request: { serverId: "207" } } } }, + { ...existingRow, access_scope: { ...existingAccess, authority: { ...existingAccess.authority, ownerId: "foreign" } } }, +].entries()) { + assert.equal((await humanArtifactReceipts(operations, networkActor, { ...existingDeps, query: async () => ({ rows: [bad] }), authorize: async () => bad.access_scope })).size, 0, `existing binding negative ${index}`) +} +for (const failedAuthorization of [ + { ...existingDeps, authorize: async () => { throw Error("inactive/revoked") } }, + { ...existingDeps, authorize: async () => ({ ...existingAccess, bytes: 257 }) }, + { ...existingDeps, checkClaims: async () => { throw Error("stale claims") } }, +]) assert.equal((await humanArtifactReceipts(operations, networkActor, failedAuthorization)).size, 0) +assert.equal((await humanArtifactReceipts([{ ...operations[0], state: "unknown" }], networkActor, { ...existingDeps, query: async () => { throw Error("must not query") } })).size, 0) +console.log("Inactive existing IPsec certificate receipt projection: exact metadata/proofs/flags/path, owner-only authorization and private-field whitelist passed") diff --git a/backend/src/modules/mcp/human-artifact-receipts.ts b/backend/src/modules/mcp/human-artifact-receipts.ts new file mode 100644 index 00000000..044f499f --- /dev/null +++ b/backend/src/modules/mcp/human-artifact-receipts.ts @@ -0,0 +1,73 @@ +import { z } from "zod" +import { isDeepStrictEqual } from "node:util" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { fingerprint, redact } from "./security.js" + +const uuid = z.string().uuid() +const authority = z.object({ principal: z.object({ connectionId: uuid }), ownerId: z.string() }).passthrough() +const receiptBase = z.object({ artifactId: uuid, humanDownloadOnly: z.literal(true), sha256: z.string().regex(/^[a-f0-9]{64}$/) }) +const receipt = z.union([ + receiptBase.extend({ backupContentReturned: z.literal(false), bytes: z.number().int().min(0).max(8 * 1048576) }), + receiptBase.extend({ databaseContentReturned: z.literal(false), bytes: z.number().int().positive().max(64 * 1048576) }), + receiptBase.extend({ wireguardContentReturned: z.literal(false), bytes: z.number().int().positive().max(1048576) }), + receiptBase.extend({ ipsecContentReturned: z.literal(false), bytes: z.number().int().positive().max(1048576) }), +]) +const existingCertificateAuthority = authority.extend({ request: z.object({ serverId: z.number().int().positive().safe() }).passthrough(), + receiptId: uuid, certificateId: z.string().regex(/^\*[a-z0-9]+$/i).max(100), + inputProof: z.string().regex(/^[a-f0-9]{64}$/), expectedProof: z.string().regex(/^[a-f0-9]{64}$/) }) +const existingCertificateReceipt = receiptBase.extend({ bytes: z.number().int().positive().max(1048576), ipsecContentReturned: z.literal(false), + ok: z.literal(true), verified: z.literal(true), receiptId: uuid, serverId: z.number().int().positive().safe(), certificateId: z.string(), + afterProof: z.string().regex(/^[a-f0-9]{64}$/), commandCount: z.literal(2), completedCommands: z.literal(2), confirmedWrites: z.literal(2), + automaticReplay: z.literal(false), forwardingVerified: z.literal(false), expiresInSeconds: z.literal(900), downloadPath: z.string() }) +type Dependencies = { + query: (sql: string, values: unknown[]) => Promise<{ rows: Record[] }> + authorize: (id: string, actor: AuthUser, kind?: "backup-download" | "database-backup" | "wireguard-export" | "ipsec-user-bundle" | "ipsec-existing-certificate-bundle") => Promise + checkClaims: (actor: AuthUser) => Promise +} + +/** A small human-only projection. File payload and step inputs never leave this helper. */ +export async function humanArtifactReceipts(operations: Record[], actor: AuthUser, dependencies: Dependencies) { + const output = new Map[]>() + if (!["mm:backups:write", "mm:settings:admin", "mm:network:write"].some(permission => hasPermission(actor.permissions, permission))) return output + const ids = operations.filter(operation => operation.state === "applied" && uuid.safeParse(operation.id).success).slice(0, 50).map(operation => operation.id) + if (!ids.length) return output + const { rows } = await dependencies.query(`/* MCP human artifact receipt projection */ + SELECT o.id AS operation_id,o.connection_id,s.state AS step_state,s.intended_state->>'service' AS service, + CASE WHEN octet_length(s.result::text)<=4096 THEN s.result ELSE NULL END AS receipt, + CASE WHEN octet_length(a.access_scope::text)<=32768 THEN a.access_scope ELSE NULL END AS access_scope, + CASE WHEN octet_length(s.intended_state::text)<=32768 THEN s.intended_state->'input' ELSE NULL END AS input + FROM mcp_operations o JOIN mcp_connections c ON c.id=o.connection_id + JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id AND p.operation_id IN ('backups.download','database.backup','network.wireguard.export','network.ipsec.users.create','network.ipsec.existing-certificates.export') + JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.state='verified' + JOIN mcp_artifacts a ON a.id::text=s.result->>'artifactId' AND a.connection_id=o.connection_id AND a.expires_at>now() + WHERE o.id=ANY($1::uuid[]) AND o.state='applied' AND c.owner_id=$2 + AND s.intended_state->>'kind'='service' AND s.intended_state->>'service'=p.operation_id + ORDER BY o.id,s.step_index LIMIT 101`, [ids, actor.id]) + if (rows.length > 100) return output + for (const row of rows) { + const operation = operations.find(operation => operation.id === row.operation_id && operation.connection_id === row.connection_id && operation.state === "applied") + const parsed = receipt.safeParse(row.receipt), scope = row.access_scope as Record | null, input = row.input as Record | null + const access = authority.safeParse(scope?.authority) + const database = row.service === "database.backup", wireguard = row.service === "network.wireguard.export", ipsec = row.service === "network.ipsec.users.create", existingCertificate = row.service === "network.ipsec.existing-certificates.export" + const kind = existingCertificate ? "ipsec-existing-certificate-bundle" : ipsec ? "ipsec-user-bundle" : wireguard ? "wireguard-export" : database ? "database-backup" : "backup-download" + if (!operation || row.step_state !== "verified" || !["backups.download", "database.backup", "network.wireguard.export", "network.ipsec.users.create", "network.ipsec.existing-certificates.export"].includes(String(row.service)) || !parsed.success || scope?.kind !== kind || !access.success || access.data.ownerId !== actor.id || access.data.principal.connectionId !== row.connection_id) continue + if (!hasPermission(actor.permissions, wireguard || ipsec || existingCertificate ? "mm:network:write" : database ? "mm:settings:admin" : "mm:backups:write") || (ipsec || existingCertificate ? !("ipsecContentReturned" in parsed.data) : wireguard ? !("wireguardContentReturned" in parsed.data) : database ? !("databaseContentReturned" in parsed.data) : !("backupContentReturned" in parsed.data))) continue + if (scope.bytes !== parsed.data.bytes || scope.sha256 !== parsed.data.sha256) continue + if (existingCertificate) { + const bound = existingCertificateAuthority.safeParse(scope.authority), result = existingCertificateReceipt.safeParse(row.receipt) + if (!bound.success || !result.success || scope.version !== 1 || bound.data.inputProof !== fingerprint(redact(input)) || + bound.data.receiptId !== input?.receiptId || bound.data.request.serverId !== input?.serverId || + result.data.receiptId !== bound.data.receiptId || result.data.serverId !== bound.data.request.serverId || result.data.certificateId !== bound.data.certificateId || + result.data.afterProof !== scope.afterProof || scope.afterProof !== bound.data.expectedProof || result.data.downloadPath !== `/api/mcp/artifacts/${result.data.artifactId}`) continue + } else if (ipsec) { + const bound = scope.authority as Record, result = row.receipt as Record + if (bound.inputProof !== fingerprint(input) || bound.receiptId !== input?.receiptId || bound.serverId !== input?.serverId || scope.afterProof !== result.afterProof || result.historyRecorded !== true || result.revisionRecorded !== true) continue + } else if (!isDeepStrictEqual(wireguard ? redact(scope.authority) : scope.authority, input?.authority) || scope.sourceProof !== input?.expectedProof) continue + if (database && (!uuid.safeParse(scope.attemptRef).success || scope.attemptRef !== input?.attemptRef)) continue + try { await dependencies.checkClaims(actor); const verified = await dependencies.authorize(parsed.data.artifactId, actor, kind); if (!isDeepStrictEqual(verified, scope)) continue } + catch { continue } // Expired, paused, revoked or changed access no longer offers a download. + const previous = output.get(String(row.operation_id)) ?? [] + if (previous.length < 8 && !previous.some(item => item.artifactId === parsed.data.artifactId)) output.set(String(row.operation_id), [...previous, parsed.data]) + } + return output +} diff --git a/backend/src/modules/mcp/inflight-owner.test.ts b/backend/src/modules/mcp/inflight-owner.test.ts new file mode 100644 index 00000000..f3b68e86 --- /dev/null +++ b/backend/src/modules/mcp/inflight-owner.test.ts @@ -0,0 +1,249 @@ +import assert from "node:assert/strict" +import { EventEmitter } from "node:events" +import { parseFlowReadResourceProfile } from "../../services/traffic-flow-read-resource-profile.js" +import { createMcpInFlightBudget, McpInFlightRefusal, MCP_INFLIGHT_PLANNING_UNITS } from "./inflight-owner.js" +import { classifyMcpLane } from "./control-lane.js" +import { authorizedMcpBindings } from "./authorized-bindings.js" +import { runMcpTransportLifecycle, observeMcpResponseStatus, createMcpResponseObservation, MCP_RESPONSE_TERMINAL_WAIT_MS } from "./transport-lifecycle.js" + +class Response extends EventEmitter { + headersSent = false + statusCode = 200 + writableFinished = false + destroyed = false + finish() { this.writableFinished = true; this.emit("finish") } + close() { this.destroyed = true; this.emit("close") } +} +function defer() { let resolve!: (value: T) => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const passed: string[] = [] +async function main() { +{ + const response = new Response(), observed = createMcpResponseObservation(response) + let sdkClosed = false + const receipt = { actual: "response unchanged" } + const returned = await runMcpTransportLifecycle({ + setup() {}, + async dispatch() { + response.headersSent = true; response.statusCode = 405 + setImmediate(() => response.finish()) + return receipt + }, + close() { sdkClosed = true }, + async audit() { + assert(sdkClosed); assert.equal(observed.snapshot().terminalEvent, null) + const actual = await observed.settle() + assert.equal(actual.statusCode, 405); assert.equal(actual.statusCodeObserved, true) + assert.equal(actual.terminalEvent, "finish"); assert.equal(actual.writableFinished, true) + assert.equal(observed.snapshot(), actual) + assert.equal(response.listenerCount("finish"), 0); assert.equal(response.listenerCount("close"), 0) + }, + }) + assert.equal(returned, receipt) + passed.push("audit awaits actual late finish405 after SDK close without changing response or dispatch receipt") +} +{ + const response = new Response(), observed = createMcpResponseObservation(response), waiting = observed.settle() + response.close(); const first = await waiting + assert.equal(first.terminalEvent, "close"); assert.equal(first.writableFinished, false) + response.statusCode = 405; response.headersSent = true; response.finish() + assert.equal(await observed.settle(), first); assert.equal(observed.snapshot(), first) + assert.equal(first.statusCodeObserved, false); assert.equal(first.statusCode, null) + assert(Object.isFrozen(first)); assert.equal(response.listenerCount("finish"), 0); assert.equal(response.listenerCount("close"), 0) + passed.push("first actual close remains READ_CANCELLED evidence despite late write/finish and repeated settlement") +} +{ + assert.equal(MCP_RESPONSE_TERMINAL_WAIT_MS, 1000) + const response = new Response(), observed = createMcpResponseObservation(response), unknown = observed.snapshot() + const pending = observed.settle(); assert.equal(observed.settle(), pending) + assert.equal(await pending, unknown) + assert.equal(response.listenerCount("finish"), 0); assert.equal(response.listenerCount("close"), 0) + response.headersSent = true; response.statusCode = 405; response.finish(); response.close() + assert.equal(await observed.settle(), unknown); assert.equal(observed.snapshot(), unknown) + assert.deepEqual(unknown, { statusCodeObserved: false, statusCode: null, writableFinished: null, destroyed: null, terminalEvent: null }) + passed.push("1000ms cap retires listeners, shares bounded wait and seals unknown against later events/flags") +} +{ + for (const kind of ["finish", "close"] as const) { + const response = new Response(); response[kind]() + const observed = createMcpResponseObservation(response), unknown = observed.snapshot() + assert.equal(await observed.settle(), unknown); assert.equal(unknown.terminalEvent, null) + assert.equal(response.listenerCount("finish"), 0); assert.equal(response.listenerCount("close"), 0) + response.headersSent = true; response.statusCode = 405; response.emit("finish") + assert.equal(observed.snapshot(), unknown) + } + passed.push("already missed terminal remains unknown immediately with no listener or mutable-status backfill") +} +{ + const response = new Response(), observed = createMcpResponseObservation(response), primary = Error("actual dispatch failure") + await assert.rejects(runMcpTransportLifecycle({ setup() {}, async dispatch() { throw primary }, close() { response.close(); throw Error("secondary close") }, async audit(terminal) { + const first = await observed.settle(); assert.equal(first.terminalEvent, "close") + assert.equal(terminal.failureStage, "dispatch"); assert.equal(terminal.closeFailed, true) + } }), error => error === primary) + passed.push("awaited terminal settlement preserves original dispatch failure and secondary close bookkeeping") +} +{ + const response = new Response(), observed = createMcpResponseObservation(response) + assert.deepEqual(observed.snapshot(), { statusCodeObserved: false, statusCode: null, writableFinished: null, destroyed: null, terminalEvent: null }) + assert.equal(response.listenerCount("finish"), 1); assert.equal(response.listenerCount("close"), 1) + response.close(); const first = observed.snapshot() + assert.deepEqual(first, { statusCodeObserved: false, statusCode: null, writableFinished: false, destroyed: true, terminalEvent: "close" }) + response.headersSent = true; response.writableFinished = true; response.statusCode = 500; response.emit("finish"); response.emit("close") + assert.equal(observed.snapshot(), first); assert(Object.isFrozen(first)); assert.equal(response.listenerCount("finish"), 0); assert.equal(response.listenerCount("close"), 0) + const success = new Response(), finished = createMcpResponseObservation(success); success.headersSent = true; success.finish(); const firstFinish = finished.snapshot(); success.close() + assert.equal(finished.snapshot(), firstFinish); assert.equal(firstFinish.terminalEvent, "finish"); assert.equal(firstFinish.statusCode, 200); assert.equal(firstFinish.destroyed, false) + const alreadyClosed = new Response(); alreadyClosed.close(); const unknown = createMcpResponseObservation(alreadyClosed) + assert.equal(unknown.snapshot().terminalEvent, null); assert.equal(unknown.snapshot().statusCode, null); assert.equal(alreadyClosed.listenerCount("close"), 0) + passed.push("first actual terminal is immutable across late SDK raw-flag mutation; unknown until event and no terminal listener leaks") +} +{ + assert.deepEqual(observeMcpResponseStatus({ headersSent: false, writableFinished: false, statusCode: 200 }), { statusCode: null, statusCodeObserved: false }) + assert.deepEqual(observeMcpResponseStatus({ headersSent: true, writableFinished: false, statusCode: 503 }), { statusCode: 503, statusCodeObserved: true }) + assert.deepEqual(observeMcpResponseStatus({ headersSent: false, writableFinished: true, statusCode: 200 }), { statusCode: 200, statusCodeObserved: true }) + passed.push("default Node status before headers is unknown; only headers/end establish observed status") +} +{ + const sentinel = Error("setup"), calls: string[] = []; let row = "running" + await assert.rejects(runMcpTransportLifecycle({ setup() { calls.push("setup"); throw sentinel }, async dispatch() { calls.push("dispatch") }, close() { calls.push("close") }, async audit(state) { calls.push("audit"); assert.equal(state.failureStage, "setup"); assert.equal(state.dispatchStarted, false); row = "error" } }), error => error === sentinel) + assert.deepEqual(calls, ["setup", "close", "audit"]); assert.equal(row, "error") + passed.push("setup failure still closes and terminally audits protocol row before rethrowing original") +} +{ + const sentinel = Error("close"), receipt = { state: "applied", id: "actual-fixture-id" }; let sent: unknown, row = "running" + await assert.rejects(runMcpTransportLifecycle({ setup() {}, async dispatch() { sent = receipt; return receipt }, close() { throw sentinel }, async audit(state) { assert.equal(state.failureStage, "close"); assert.equal(state.dispatchStarted, true); row = "transport-error" } }), error => error === sentinel) + assert.equal(sent, receipt); assert.equal(receipt.state, "applied"); assert.equal(row, "transport-error") + passed.push("close failure cannot skip terminal protocol audit or rewrite an already sent receipt") +} +{ + const first = Error("dispatch"), logs: boolean[] = []; let auditAttempts = 0, closeAttempts = 0 + await assert.rejects(runMcpTransportLifecycle({ setup() {}, async dispatch() { throw first }, close() { closeAttempts++; throw Error("close-secondary") }, async audit(state) { auditAttempts++; assert.equal(state.failureStage, "dispatch"); assert.equal(state.closeFailed, true); throw Error("storage-secondary") }, auditUnavailable(_state, secondary) { logs.push(secondary) } }), error => error === first) + assert.equal(auditAttempts, 1); assert.equal(closeAttempts, 1); assert.deepEqual(logs, [true]) + passed.push("dispatch error wins over close/storage failures; one audit attempt with bounded secondary failure log") +} +{ + const sentinel = Error("audit unavailable"), receipt = { state: "pending", id: "actual-pending-id" }, logs: boolean[] = []; let sent: unknown, dispatched = 0, audited = 0, row = "running" + await assert.rejects(runMcpTransportLifecycle({ setup() {}, async dispatch() { dispatched++; sent = receipt; return receipt }, close() {}, async audit() { audited++; throw sentinel }, auditUnavailable(_state, secondary) { logs.push(secondary) } }), error => error === sentinel) + assert.equal(sent, receipt); assert.equal(dispatched, 1); assert.equal(audited, 1); assert.equal(row, "running"); assert.deepEqual(logs, [false]) + row = "persistence-unconfirmed"; assert.equal(row, "persistence-unconfirmed") + passed.push("audit outage after factual pending receipt is persistence-unconfirmed with no SDK/effect/update retry") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")), response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed) + const hold = defer(); let child!: Promise, audited = false + const run = lease.owner.dispatch(() => runMcpTransportLifecycle({ setup() {}, async dispatch() { child = lease.owner.callback(async () => hold.promise); return 1 }, close() { throw Error("close") }, async audit() { audited = true } })) + await assert.rejects(run); response.close(); assert(audited); assert.equal(budget.stats().owners, 1) + hold.resolve(7); assert.equal(await child, 7); assert.equal(budget.stats().owners, 0) + passed.push("close/audit completion still cannot retire an actual callback pending after response abort") +} +{ + let caught = false + try { await runMcpTransportLifecycle({ setup() { throw undefined }, async dispatch() { return 1 }, close() { throw Error("secondary") }, async audit() {} }) } catch (error) { caught = true; assert.equal(error, undefined) } + assert(caught); passed.push("first actual thrown undefined is preserved instead of replaced by later close error") +} +{ + const read = { operation: { id: "operations.read", toolName: "mm_operation_get", requiredPermissions: ["mm:servers:read"], effect: "read" } } + const cancel = { operation: { id: "servers.probes.cancel", toolName: "mm_server_probe_cancel", requiredPermissions: ["mm:servers:write"], effect: "write" } } + const connection = { policy: { scope: { operationIds: [read.operation.id, cancel.operation.id] }, permissions: ["mm:servers:write"], mode: "approval" }, owner_permissions: ["mm:servers:write"] } + const permitted = authorizedMcpBindings([read, cancel], connection) + assert.equal(permitted[0], read); assert.equal(permitted[1], cancel) + assert.equal(classifyMcpLane({ method: "tools/call", params: { name: cancel.operation.toolName } }, permitted.map(row => row.operation)), "control") + for (const narrowed of [ + { ...connection, owner_permissions: [] }, + { ...connection, policy: { ...connection.policy, permissions: [] } }, + { ...connection, policy: { ...connection.policy, scope: { operationIds: [] } } }, + ]) assert.equal(authorizedMcpBindings([read, cancel], narrowed).length, 0) + assert.deepEqual(authorizedMcpBindings([read, cancel], { ...connection, policy: { ...connection.policy, mode: "observer" } }), [read]) + passed.push("actual registration policy shares exact bindings; owner/policy/scope/observer negatives and RBAC write-implies-read preserved") +} +{ + const cancel = { id: "servers.probes.cancel", toolName: "mm_server_probe_cancel", effect: "write" } + const request = { method: "tools/call", params: { name: cancel.toolName } } + assert.equal(classifyMcpLane(request, [cancel]), "control"); assert.equal(classifyMcpLane([request], [cancel]), "control") + for (const changed of [[], [{ ...cancel, id: "changes.apply" }], [{ ...cancel, effect: "read" }]]) assert.equal(classifyMcpLane(request, changed), "normal") + assert.equal(classifyMcpLane({ method: "tools/call", params: { name: "mm_change_apply" } }, [{ id: "changes.apply", toolName: "mm_change_apply", effect: "write" }]), "normal") + assert.equal(classifyMcpLane({ method: "tools/call", params: { name: "toString" } }, []), "normal") + assert.equal(classifyMcpLane([request, request], [cancel]), "normal") + passed.push("control tool lane requires exact trusted authorized tool/id/effect; no apply/unknown/batch bypass") +} +{ + for (const method of ["initialize", "ping", "tools/list", "notifications/initialized", "notifications/cancelled"]) assert.equal(classifyMcpLane({ method }, []), "control") + for (const method of ["subscriptions/listen", "tools/call", "made-up-method"]) assert.equal(classifyMcpLane({ method }, []), "normal") + passed.push("only explicit discovery/cancellation protocol methods use control lane") +} +for (const name of ["standard", "constrained"] as const) { + const profile = parseFlowReadResourceProfile(name), budget = createMcpInFlightBudget(profile) + assert.equal(budget.limits.maxOwners, profile.maxOwners + 1); assert.equal(budget.limits.maxNormalOwners, profile.maxOwners); assert.equal(budget.limits.maxReservationUnits, (profile.maxOwners + 1) * MCP_INFLIGHT_PLANNING_UNITS) + const held = Array.from({ length: profile.maxOwners }, () => { const response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed); return { response, owner: lease.owner } }) + assert.deepEqual(budget.acquire(new Response(), "normal"), { allowed: false, reason: "capacity", code: "RATE_LIMITED" }) + const controlResponse = new Response(), control = budget.acquire(controlResponse, "control"); assert(control.allowed) + assert.equal(budget.acquire(new Response(), "control").allowed, false) + assert.equal(budget.stats().owners, profile.maxOwners + 1) + for (const { response, owner } of held) { await owner.dispatch(async () => 1); response.finish() } + await control.owner.dispatch(async () => 1); controlResponse.close() + assert.deepEqual(budget.stats(), { owners: 0, normal: 0, control: 0, reservedUnits: 0 }) + passed.push(name + ": new normal count preserves 4/2; separate control1 has explicit additional reservation cost") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")), response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed) + const hold = defer(), actualOutcome = { state: "applied", operationId: "actual-runtime-id" } + let callback!: Promise + const dispatch = lease.owner.dispatch(async () => { callback = lease.owner.callback(async () => { await hold.promise; return actualOutcome }); return "SDK-early-return" }) + assert.equal(await dispatch, "SDK-early-return"); response.close() + assert.equal(budget.stats().owners, 1) + await assert.rejects(lease.owner.callback(async () => "late"), McpInFlightRefusal) + hold.resolve("drain"); assert.equal(await callback, actualOutcome); assert.equal(budget.stats().owners, 0) + passed.push("abort + SDK early completion retains pending callback; actual applied object unchanged") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")), response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed) + const hold = defer(), outcome = new Error("actual callback failure") + const run = lease.owner.dispatch(() => lease.owner.callback(async () => { await hold.promise; throw outcome })) + const rejected = assert.rejects(run, error => error === outcome); response.finish(); assert.equal(budget.stats().owners, 1) + hold.resolve(1); await rejected; assert.equal(budget.stats().owners, 0) + passed.push("actual callback error unchanged after finish and drain") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")), response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed) + const hold = defer(), run = lease.owner.dispatch(async () => { const result = await lease.owner.callback(async () => 42); await hold.promise; return result }) + response.close(); assert.equal(budget.stats().owners, 1); hold.resolve(1); assert.equal(await run, 42); assert.equal(budget.stats().owners, 0) + passed.push("callback drain alone does not release a still-active SDK dispatch") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")), response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed) + assert.equal(await lease.owner.dispatch(() => lease.owner.callback(async () => 42)), 42); assert.equal(budget.stats().owners, 1) + response.finish(); response.close(); response.emit("finish"); assert.equal(budget.stats().owners, 0) + assert.equal(response.listenerCount("finish"), 0); assert.equal(response.listenerCount("close"), 0) + passed.push("callback/SDK completion waits for response; duplicate terminal events release once and detach") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")) + for (const terminal of ["finish", "close"] as const) { const response = new Response(); response[terminal](); assert.deepEqual(budget.acquire(response, "normal"), { allowed: false, reason: "response-closed", code: "READ_CANCELLED" }) } + assert.equal(budget.stats().owners, 0); passed.push("already terminal response refuses before acquiring or invoking") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")), response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed) + let effects = 0; response.close() + await assert.rejects(lease.owner.dispatch(async () => { effects++; return 1 }), McpInFlightRefusal) + assert.equal(effects, 0); assert.equal(budget.stats().owners, 0); passed.push("close before SDK dispatch refuses before effects and retires lease") +} +{ + const budget = createMcpInFlightBudget(parseFlowReadResourceProfile("constrained")), response = new Response(), lease = budget.acquire(response, "normal"); assert(lease.allowed) + let effects = 0 + await assert.rejects(lease.owner.callback(async () => { effects++; return 1 }), McpInFlightRefusal) + await lease.owner.dispatch(async () => 1) + await assert.rejects(lease.owner.dispatch(async () => { effects++; return 1 }), McpInFlightRefusal) + response.finish(); assert.equal(effects, 0); passed.push("callbacks outside actual dispatch and repeated dispatch cannot invoke") +} +{ + for (const profile of [{ maxOwners: 1 }, { maxOwners: 0 }, { maxOwners: 2.5 }, { maxOwners: 3 }, { maxOwners: 4 }, { maxOwners: Number.MAX_SAFE_INTEGER }]) { + assert.throws(() => createMcpInFlightBudget({ name: "constrained", ...profile })) + } + passed.push("invalid/overflow profile reservation arithmetic rejected at construction") +} +{ + assert.throws(() => createMcpInFlightBudget({ name: "standard", maxOwners: 2 })) + assert.throws(() => createMcpInFlightBudget({ name: "other" as "standard", maxOwners: 4 })) + passed.push("claimed profile name/count must match exact standard4 or constrained2") +} +console.log(JSON.stringify({ status: "component-only-pass", cases: passed.length, names: passed, sdkExecuted: false, postgresStarted: false, productionIntegrated: false })) +} +void main().catch(error => { console.error(error); process.exitCode = 1 }) diff --git a/backend/src/modules/mcp/inflight-owner.ts b/backend/src/modules/mcp/inflight-owner.ts new file mode 100644 index 00000000..8e5a195d --- /dev/null +++ b/backend/src/modules/mcp/inflight-owner.ts @@ -0,0 +1,88 @@ +import type { FlowReadResourceProfile } from "../../services/traffic-flow-read-resource-profile.js" + +type Lane = "normal" | "control" +interface ResponseLifecycle { + readonly writableFinished: boolean + readonly destroyed: boolean + once(event: "finish" | "close", listener: () => void): unknown + off(event: "finish" | "close", listener: () => void): unknown +} +type Profile = Pick +/** Planning/accounting unit only: B1 1MiB + existing encoded-ID boundary 1MiB + * + 1KiB terminal headroom. Not a universal SDK metadata/error/stream bound or + * memory lease. Generic units do NOT derive from NetFlow output/source bytes. + */ +export const MCP_INFLIGHT_PLANNING_UNITS = 2 * 1024 * 1024 + 1024 +export class McpInFlightRefusal extends Error { + readonly code = "READ_CANCELLED" + readonly statusCode = 409 + constructor(readonly reason: "response-closed" | "dispatch-closed") { super("Ответ MCP уже закрыт для нового выполнения") } +} + +/** Separate MCP accounting book, NEVER a NetFlow source lease or heap/wire bound. + * Normal count follows profile 4/2; one NEW separate control slot is explicit. + * This layer does not consume, subdivide or raise NetFlow source caps. Nothing is + * queued and no output/result is examined or rejected after a callback runs. + */ +export function createMcpInFlightBudget(profile: Profile) { + if (!(profile.name === "standard" && profile.maxOwners === 4 || profile.name === "constrained" && profile.maxOwners === 2) || + !Number.isSafeInteger((profile.maxOwners + 1) * MCP_INFLIGHT_PLANNING_UNITS)) throw Error("Invalid MCP in-flight profile") + const limits = Object.freeze({ maxOwners: profile.maxOwners + 1, maxNormalOwners: profile.maxOwners, + maxControlOwners: 1, reservationUnitsPerOwner: MCP_INFLIGHT_PLANNING_UNITS, + maxReservationUnits: (profile.maxOwners + 1) * MCP_INFLIGHT_PLANNING_UNITS }) + let normal = 0, control = 0, reservedUnits = 0 + const stats = () => ({ owners: normal + control, normal, control, reservedUnits }) + return { + limits, stats, + acquire(response: ResponseLifecycle, lane: Lane) { + if (lane !== "normal" && lane !== "control") throw Error("Invalid MCP in-flight lane") + if (response.writableFinished || response.destroyed) return { allowed: false as const, reason: "response-closed" as const, code: "READ_CANCELLED" as const } + if (lane === "normal" && normal >= limits.maxNormalOwners || + lane === "control" && control >= limits.maxControlOwners || + limits.reservationUnitsPerOwner > limits.maxReservationUnits - reservedUnits) { + return { allowed: false as const, reason: "capacity" as const, code: "RATE_LIMITED" as const } + } + if (lane === "normal") normal++; else control++ + reservedUnits += limits.reservationUnitsPerOwner + let responseFinished = false, dispatchStarted = false, dispatchFinished = false, pendingCallbacks = 0, released = false + const release = () => { + if (!released && responseFinished && dispatchFinished && pendingCallbacks === 0) { + released = true + if (lane === "normal") normal--; else control-- + reservedUnits -= limits.reservationUnitsPerOwner + } + } + const finished = () => { + if (responseFinished) return + responseFinished = true; response.off("finish", finished); response.off("close", finished); release() + } + response.once("finish", finished); response.once("close", finished) + // Handle a lifecycle transition during listener attachment too. + if (response.writableFinished || response.destroyed) finished() + return { allowed: true as const, owner: { + /** Must wrap the complete SDK/node-handler + handler.close lifetime. + * A close rejects NEW work only; an existing callback keeps its actual + * result or error. Never remap a confirmed effect during release. + */ + async dispatch(run: () => Promise): Promise { + if (dispatchStarted) throw new McpInFlightRefusal("dispatch-closed") + dispatchStarted = true + try { if (responseFinished) throw new McpInFlightRefusal("response-closed"); return await run() } + finally { dispatchFinished = true; release() } + }, + /** Wrap each actually registered SDK callback, including delivery. + * SDK handler returning early does not retire outstanding callbacks. + * Background durable operations and untracked detached tasks are NOT + * claimed as drained by this callback-level component. + */ + async callback(run: () => Promise): Promise { + if (responseFinished) throw new McpInFlightRefusal("response-closed") + if (!dispatchStarted || dispatchFinished) throw new McpInFlightRefusal("dispatch-closed") + pendingCallbacks++ + try { return await run() } + finally { pendingCallbacks--; release() } + }, + } } + }, + } +} diff --git a/backend/src/modules/mcp/integration-fixture-expiry.test.ts b/backend/src/modules/mcp/integration-fixture-expiry.test.ts new file mode 100644 index 00000000..35f5d9de --- /dev/null +++ b/backend/src/modules/mcp/integration-fixture-expiry.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { fixtureGrantExpiry } from "../../test/mcp-fixture-expiry.js" + +const now = Date.parse("2026-10-29T08:00:00.000Z") +test("long original access retains the existing thirty-minute fixture cap", () => { + assert.equal(Date.parse(fixtureGrantExpiry(now, now + 3600000)), now + 1800000 - 1000) +}) +test("late fixture stays strictly within its original remaining credential expiry", () => { + const original = now + 22 * 60000 + assert.equal(Date.parse(fixtureGrantExpiry(now, original)), original - 1000) + assert.equal(original, now + 22 * 60000) +}) +test("earliest original human, policy and credential bound wins", () => { + for (const bounds of [[now + 900000, now + 1200000, now + 1500000], [now + 1500000, now + 900000, now + 1200000]]) { + assert.equal(Date.parse(fixtureGrantExpiry(now, ...bounds)), now + 900000 - 1000) + } +}) +test("expired or insufficient remaining original access cannot be silently renewed", () => { + for (const expiry of [now - 1, now, now + 30000, now + 61000]) assert.throws(() => fixtureGrantExpiry(now, expiry), /Insufficient/) +}) +test("absent and malformed original bounds cannot mint a fresh fixture grant", () => { + assert.throws(() => fixtureGrantExpiry(now), /Missing/) + for (const bad of [NaN, Infinity, Number.MAX_SAFE_INTEGER, now + 0.5]) assert.throws(() => fixtureGrantExpiry(now, bad), /invalid/) +}) diff --git a/backend/src/modules/mcp/integration.test.ts b/backend/src/modules/mcp/integration.test.ts new file mode 100644 index 00000000..33a84811 --- /dev/null +++ b/backend/src/modules/mcp/integration.test.ts @@ -0,0 +1,3880 @@ +import assert from "node:assert/strict" +import { attestRoutingFixture } from "../../test/mcp-routing-fixture.js" +import { fixtureGrantExpiry } from "../../test/mcp-fixture-expiry.js" +import { verifyMcpGrantExpiryBounds } from "../../test/mcp-grant-expiry-bounds.js" +import { createHash, createHmac, randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import { fileURLToPath } from "node:url" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings, rotateConnectionToken, setConnectionStatus, revalidateMcpOwner, effectiveMcpConnection } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan, recoverMcpOperations } from "./runtime.js" +import { decryptSecret, encryptSecret, invalidateMcpAccess, tokenDigest } from "./security.js" +import { addRoutingDependencies } from "./routing-dependencies.js" +import { acceptOspfRouteProofMcp } from "./ospf-route-proof-acceptance.js" +import { acceptNetworkNeighborsMcp } from "./network-neighbors-acceptance.js" +import { acceptIpv6NdWriteMcp } from "./ipv6-nd-write-acceptance.js" +import { acceptArpWriteMcp } from "./arp-write-acceptance.js" +import { acceptWireguardInterfaceDeletion } from "./wireguard-interface-delete-acceptance.js" +import { acceptWireguardPeerUpdate } from "./wireguard-peer-update-acceptance.js" +import { acceptWireguardPeerDeletion } from "./wireguard-peer-delete-acceptance.js" +import { acceptGreRevisionRestore } from "../../test/mcp-gre-revision.js" +import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client" +import { StdioClientTransport } from "@modelcontextprotocol/client/stdio" +import type { AuthUser } from "../../lib/permissions.js" +import { managedComment } from "../../managed-markers.js" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" + +// One fixed authority deadline for the long integration suite; never renewed during the run. +const fixtureAuthExpiresAt = Date.now() + 4 * 60 * 60 * 1000 +const actor: AuthUser = { id: "integration-owner", email: "test@example.invalid", name: "Test", apps: ["mm"], permissions: ["mm:settings:admin", "mm:certificates:write", "mm:network:admin", "mm:bgp:admin", "mm:servers:admin", "mm:filters:admin"], expiresAt: fixtureAuthExpiresAt } +let settings = { "rp-filter": "no", "tcp-syncookies": "no", "ipv4-multipath-hash-policy": "l3" }, writes = 0 +let reconcileReadHook: (() => Promise) | undefined +let reconcileSettingsReads = 0 +const emptyEvoCatalog = { modules: { items: [] }, domains: { items: [] }, ip_ranges: { items: [] }, asns: { items: [] }, communities: { items: [] } } +let evoCatalogPayload: unknown = emptyEvoCatalog, evoCatalogCalls = 0, evoCatalogAuthorization: string | undefined, evoCatalogRedirect = false +let evoProbePayload: unknown = { items: [] }, evoProbeCalls = 0, evoProbeAuthorization: string | undefined, evoProbeRedirect = false +let bridge = { ".id": "*fixture", name: "fixture-bridge", comment: "baseline", disabled: "true" } +let healthState = true, failHealthAfterWrite = false, healthUnavailable = false, loseHealthResponse = false +let healthBfdRemote = "192.0.2.2%fixture-ospf" +let fixtureRouteTable: string | undefined +let fixtureRoutingRules: Record[] = [] +let interfaceFixtures = false, ignoreVrfPlacement = false, vrfWrites = 0 +let fixtureVrfs: Record[] = [] +// Preparation-only native catalogs for the business alias assertions. The +// genuine owned component separately proves actual alias-only execution. +let nativeAliasFixtures = false +const nativeAliasCatalogs: Record = { + "/rest/ip/vrf": [{ ".id": "*abc", name: "managed-vrf", interfaces: "fixture-gre", disabled: "false", comment: "MikrotikManager: integration" }, { ".id": "*0", name: "main", interfaces: "all", disabled: "false", builtin: "true" }], + "/rest/interface": [{ ".id": "*b1", name: "fixture-gre", type: "gre-tunnel", vrf: "managed-vrf" }, { ".id": "*b2", name: "fixture-bridge", type: "bridge", vrf: "main" }], + "/rest/interface/gre": [{ ".id": "*a1", name: "fixture-gre", disabled: "false", "local-address": "192.0.2.1", "remote-address": "198.51.100.2" }], + "/rest/interface/bridge": [{ ".id": "*a2", name: "fixture-bridge", disabled: "false" }], + "/rest/ip/address": [{ ".id": "*a3", address: "192.0.2.1/30", interface: "fixture-bridge", vrf: "main", dynamic: "false", disabled: "false" }], + "/rest/ip/route": [{ ".id": "*a4", "dst-address": "192.0.2.0/30", "routing-table": "main", gateway: "fixture-bridge", "immediate-gw": "fixture-bridge", dynamic: "true", connect: "true" }], + "/rest/routing/table": [{ ".id": "*0", name: "main", fib: "true", dynamic: "true" }, { ".id": "*abc", name: "managed-vrf", fib: "", dynamic: "true" }], + "/rest/ip/settings": {}, "/rest/ipv6/settings": {}, +} +let ospfFixtures = false +let ospfAnalysisFixtures = false, ospfAnalysisDispatches = 0, ospfAnalysisNativeRequests = 0 +let ospfAfterCostPatch: (() => void) | undefined +let ospfAnalysisExtraNeighbors: Record[] = [] +const ospfAnalysisAddresses = [{ ".id": "*la", interface: "fixture-gre", address: "192.0.2.1/30", disabled: "false", invalid: "false" }] +let ospfCostWriteFault = "none" +let ospfPassiveWrites = 0, ospfIgnoreUnset = false +let ospfPreviewPauseConnection:string|undefined +let countCatalogCalls = false, catalogCalls = 0, ospfTemplateFlagReads = 0 +let ospfPauseOnTemplatesConnection: string | undefined +let ospfTemplateListsBeforePause = 0 +let greCatalogFixtures = false +let recursiveCatalogFixtures = false, recursivePropertyFailure = false +let recursiveCatalogRows: Record[] = [] +let recursiveMutations = false, recursiveMutationCount = 0, recursiveIgnorePayload = false +let recursivePauseConnection: string | undefined +let deviceStateFixtures = false, containerStateFailure: "none" | "envs" | "unsupported" = "none", certificateClockKnown = true +let reverseContainerRows = false +let certificateStateMalformed = false +let filterCatalogFixtures = false, filterCatalogMalformed = false +let legacyRouteContextFixture = false +const legacyRouteContextRows: Record[]> = { + "/routing/table": [{ ".id": "*main", name: "main", fib: "true", dynamic: "true" }], + "/ip/vrf": [{ ".id": "*vrf", name: "main", interfaces: "all", disabled: "false" }], + "/routing/bgp/connection": [{ ".id": "*peer", name: "legacy-peer", instance: "legacy", vrf: "main", "routing-table": "main", afi: "ip", "input.filter": "bgp-in", "output.filter-chain": "", templates: "", disabled: "false" }], + "/routing/bgp/instance": [{ ".id": "*instance", name: "legacy", vrf: "main", "routing-table": "main", disabled: "false" }], + "/routing/bgp/template": [], + "/routing/filter/rule": [{ ".id": "*filter", chain: "bgp-in", rule: compileManagedFilterRules([{ community: "65000:10", gateway: "192.0.2.1", outIface: "", action: "route", description: "" }]), comment: managedComment("test-router"), disabled: "false" }], + "/ip/route": [{ ".id": "*1", "dst-address": "192.0.2.1/32", "routing-table": "main", comment: managedComment("recursive"), disabled: "false" }], + "/routing/ospf/instance": [], "/routing/rip/instance": [], "/routing/isis/instance": [], "/routing/filter/select-rule": [], "/routing/bgp/vpn": [], "/routing/rule": [], "/ip/firewall/mangle": [], "/ipv6/firewall/mangle": [], +} +let greMutationFixtures = false, greMutationIgnored = false, greMutationLost = false, greMutationCommands = 0, greMutationNextId = 100 +let greMutationRevoke: string | undefined +let greRevisionAfterCommand: (()=>Promise) | undefined +let greRevisionReads=0 +let greNativeCounterTick = false +let greNativeRouteReads = 0, greNativeLateDynamicAt = 0 +let greMutationCollections: Record[]> = {} +let greMutationDependencies: Record[]> = {} +let preflightFixtures: Record | undefined, preflightUnavailable = "" +let containerMutationFixtures = false, containerDeletes = 0, containerCommands = 0, ignoreContainerDelete = false, replaceContainerAfterCommand = false +let mutableContainers: Record[] = [] +const containerParent: { ".id": string; name: string; type: string; disabled?: string } = { ".id": "*veth", name: "fixture-veth", type: "veth", disabled: "false" } +let ecmpFixtures = false, ecmpWrites = 0, loseEcmpGateway = false +let fixtureEcmpRoutes: Record[] = [] +let wanFixtures = false, wanAddressFailure = false +let probeInterfaceFixtures = false, probeNativeCalls = 0, probeReadFailure = false +let liveFixtures = false, liveNativeCalls = 0, liveIncomplete = false +let liveRevokeConnection: string | undefined, liveChangeSource = false +let firewallStateFixtures = false, firewallStateCalls = 0, firewallKnownFlags = false, firewallFailedChunk = "", firewallDuplicate = false +let fwRestoreFixtures = false, fwRestoreCommands = 0, fwRestoreNextId = 1, fwRestoreIgnored = false, fwRestoreLost = false +let fwRestoreRevoke: string | undefined +let fwRestoreCollections: Record[]> = {} +let addressFixtures = false, addressCommands = 0, addressIgnored = false, addressExpiry = 0 +let addressRows: Record[] = [] +let firewallWriteFixtures = false, firewallWriteCommands = 0, firewallWriteIgnored = false +let firewallWriteRows: Record[] = [] +let firewallMoveFixtures = false, firewallMoveCommands = 0, firewallIgnoreMove = false +let firewallMoveRows = [1, 2, 3].map(id => ({ ".id": "*" + id, chain: "fixture-move", action: "accept", disabled: "true", dynamic: "false", log: "false", comment: "rule " + id, packets: "0", "connection-mark": "keep-exactly" })) +let vpnFixtures = false, vpnFailurePath = "", vpnAmbiguous = false, vpnUnknownSession = false, vpnDuplicate = false +const vpnCalls: string[] = [], vpnPrivate = "never-return-wg-private-fixture", vpnPsk = "never-return-ipsec-psk-fixture" +const wanDefault = { ".id": "*wan", "dst-address": "0.0.0.0/0", "routing-table": "main", active: "true", distance: "1", gateway: "192.0.2.1", "immediate-gw": "192.0.2.1%ether1" } +let wanRoutes: Record[] = [wanDefault] +const fixtureOspf: Record<"instance" | "area" | "interface-template", Record[]> = { + instance: [{ ".id": "*ospf", name: "fixture-ospf", vrf: "main", "router-id": "192.0.2.1", version: "2", disabled: "false" }], + area: [{ ".id": "*area", name: "fixture-area", instance: "fixture-ospf", disabled: "false", "area-id": "0.0.0.0" }], + "interface-template": [{ ".id": "*template", area: "fixture-area", interfaces: "fixture-gre", cost: "30", disabled: "false", "use-bfd": "true" }], +} +let bgpFixtures = false +let bgpSessionOverride: unknown = undefined +const fixtureBgp = { + instance: [{ ".id": "*bgp", name: "fixture-bgp", vrf: "main", "routing-table": "main", as: "64512", "router-id": "192.0.2.1", disabled: "false" }], + connection: [{ ".id": "*peer", name: "fixture-peer", instance: "fixture-bgp", "remote.address": "192.0.2.2", "local.address": "192.0.2.1", "remote.as": "64513", "local.role": "ebgp", disabled: "false" }], + session: [{ ".id": "*session", name: "fixture-peer-1", instance: "fixture-bgp", established: "true", "remote.address": "192.0.2.2", "remote.as": "64513", "local.as": "64512", "local.afi": "ip,ipv6", "remote.afi": "ip,ipv6" }], +} +const router = createServer((req, res) => { + if (nativeAliasFixtures) { + res.setHeader("content-type", "application/json") + if (req.method !== "GET") { vrfWrites++; res.statusCode = 405; res.end("{}"); return } + res.end(JSON.stringify(nativeAliasCatalogs[req.url ?? ""] ?? [])); return + } + if (ospfAnalysisFixtures) ospfAnalysisNativeRequests++ + if (legacyRouteContextFixture) { + const path = (req.url ?? "").replace(/^\/rest/, ""), collection = path.endsWith("/get") ? path.slice(0, -4) : path + if (collection in legacyRouteContextRows) { + res.setHeader("content-type", "application/json") + if (req.method === "GET" && path === collection) { res.end(JSON.stringify(legacyRouteContextRows[collection])); return } + if (req.method === "POST" && path.endsWith("/get")) { + let raw = ""; req.on("data", chunk => { raw += chunk }); req.on("end", () => { + const args = JSON.parse(raw), row = legacyRouteContextRows[collection].find(item => item[".id"] === args[".id"]), value = row?.[args["value-name"]] + res.end(JSON.stringify(value === undefined ? [] : { ret: value })) + }); return + } + res.statusCode = 405; res.end("{}"); return + } + } + if (req.url === "/v1/modules?limit=1") { + evoProbeCalls++; evoProbeAuthorization = req.headers.authorization + if (evoProbeRedirect) { res.writeHead(302, { Location: "/probe-redirect-target" }); res.end(); return } + res.setHeader("content-type", "application/json"); res.end(JSON.stringify(evoProbePayload)); return + } + if (req.url === "/v1/router-lists/catalog") { + evoCatalogCalls++; evoCatalogAuthorization = req.headers.authorization + if (evoCatalogRedirect) { res.writeHead(302, { Location: "/catalog-redirect-target" }); res.end(); return } + res.setHeader("content-type", "application/json"); res.end(JSON.stringify(evoCatalogPayload)); return + } + if (countCatalogCalls) catalogCalls++ + if (vpnFixtures && req.method === "GET") { + const path = req.url ?? "" + vpnCalls.push(path); res.setHeader("content-type", "application/json") + if (path === vpnFailurePath) { res.statusCode = 503; res.end("{}"); return } + const wg = [{ ".id": "*wg1", name: "wg1", "private-key": vpnPrivate, comment: vpnPrivate, disabled: "false", running: "true", "listen-port": "13231", mtu: "1420" }, { ".id": "*wg2", name: "wg2" }] + const identity = { ".id": "*I1", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:alice", secret: vpnPsk, comment: "MikrotikManager:ipsec user=" + vpnPsk } + const collections: Record = { + "/rest/interface/gre": vpnDuplicate ? [{ ".id": "*G", name: "gre" }, { ".id": "*G", name: "gre" }] : [{ ".id": "*G", name: "gre", disabled: "false", running: "true", "ipsec-secret": vpnPsk, comment: vpnPsk, mtu: "1400", keepalive: "10s,10" }, { ".id": "*U", name: "unknown" }], + "/rest/interface/wireguard": vpnDuplicate ? [wg[0], wg[0]] : wg, + "/rest/interface/wireguard/peers": [1, 2, 3, 4].map(id => ({ ".id": "*P" + id, interface: id === 4 ? "wg2" : "*wg1", "public-key": "public-" + id, "preshared-key": vpnPsk, name: vpnPsk, rx: "0", "persistent-keepalive": "0", disabled: "false", "allowed-address": "192.0.2.0/24,198.51.100.0/24,fd00::/64" })), + "/rest/ip/address": [{ ".id": "*a", interface: "*wg1", address: "192.0.2.1/24", disabled: "false", dynamic: "false", invalid: "false" }], "/rest/ipv6/address": [1, 2, 3].map(id => ({ ".id": "*a" + id, interface: id === 1 ? "*G" : "gre", address: "fd00::" + id + "/64", disabled: "false", dynamic: "false", invalid: "false" })), + "/rest/interface/vxlan": [{ ".id": "*vx1", name: "vx1", vni: "100", port: "4789", mtu: "1420", l2mtu: "65535", arp: "enabled", learning: "true", disabled: "false", running: "true" }], + "/rest/interface/vxlan/vteps": [1, 2, 3].map(id => ({ ".id": "*v" + id, interface: "*vx1", "remote-ip": "198.51.100." + id })), + "/rest/ip/ipsec/peer": [{ ".id": "*ike", name: "ike", "exchange-mode": "ike2", passive: "true", certificate: "ipsec-server" }], + "/rest/ip/ipsec/identity": vpnAmbiguous ? [identity, { ...identity, ".id": "*I2", comment: "MikrotikManager:ipsec user=second" }] : [identity], + "/rest/ip/ipsec/active-peers": [{ ".id": "*active", id: "alice", ...(vpnUnknownSession ? {} : { state: "established" }), "remote-address": "192.0.2.2" }], + "/rest/certificate": [{ ".id": "*CA", name: "ipsec-ca", "key-usage": "key-cert-sign,crl-sign", authority: "true", trusted: "false", "private-key": "true" }, { ".id": "*CRT", name: "ipsec-server", "key-usage": "tls-server", "common-name": "vpn.example.invalid", ca: "ipsec-ca", "invalid-after": "oct/31/2026 12:00:00", trusted: "false", "private-key": "true" }], + } + res.end(JSON.stringify(collections[path] ?? [])); return + } + if (fwRestoreFixtures && /^\/rest\/(?:ip|ipv6)\/firewall\//.test(req.url ?? "")) { + res.setHeader("content-type", "application/json") + const path = decodeURIComponent(req.url ?? ""), getPath = path.endsWith("/get"), base = getPath ? path.slice(0, -4) : path.replace(/\/\*[A-Za-z0-9]+$/, ""), id = path.slice(base.length + 1) + const rows = fwRestoreCollections[base] ?? (fwRestoreCollections[base] = []) + if (req.method === "GET") { res.end(JSON.stringify(rows)); return } + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", async () => { + const data = body ? JSON.parse(body) : {} + if (getPath) { res.end(JSON.stringify({ ret: rows.find(row => row[".id"] === data[".id"])?.[data["value-name"]] ?? "false" })); return } + fwRestoreCommands++ + const fields = Object.fromEntries(Object.entries(data).map(([key, value]) => [key, ["disabled", "log"].includes(key) ? value === "yes" ? "true" : "false" : value])) as Record + if (req.method === "PUT" && !fwRestoreIgnored) rows.push({ ".id": "*NEW" + fwRestoreNextId++, dynamic: "false", disabled: "false", ...(base.endsWith("address-list") ? {} : { log: "false" }), ...fields }) + if (req.method === "DELETE") fwRestoreCollections[base] = rows.filter(row => row[".id"] !== id) + if (req.method === "PATCH") Object.assign(rows.find(row => row[".id"] === id)!, fields) + if (fwRestoreRevoke) { const connectionId = fwRestoreRevoke; fwRestoreRevoke = undefined; await setConnectionStatus(connectionId, "paused", actor) } + if (fwRestoreLost) { fwRestoreLost = false; res.destroy(); return } + res.end("{}") + }); return + } + if (addressFixtures && /^\/rest\/(?:ip|ipv6)\/firewall\//.test(req.url ?? "")) { + res.setHeader("content-type", "application/json"); const path = decodeURIComponent(req.url ?? ""), base = "/rest/ip/firewall/address-list" + if (req.method === "GET") { res.end(JSON.stringify(path === base ? addressRows.map(row => row[".id"] === "*T" ? { ...row, timeout: Math.max(1, Math.floor((addressExpiry - Date.now()) / 1000)) + "s" } : row) : [])); return } + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", () => { + const data = body ? JSON.parse(body) : {} + if (path === base + "/get") { res.end(JSON.stringify({ ret: "false" })); return } + addressCommands++ + if (!addressIgnored) { + const fields = Object.fromEntries(Object.entries(data).map(([key, value]) => [key, key === "disabled" ? value === "yes" ? "true" : "false" : key === "address" ? String(value).replace(/\/32$/, "") : value])) as Record + if (req.method === "PUT") { const id = fields.timeout ? "*T" : "*C"; addressRows.push({ ".id": id, dynamic: fields.timeout ? "true" : "false", disabled: "false", ...fields }); if (fields.timeout) addressExpiry = Date.now() + 60000 } + if (req.method === "PATCH") Object.assign(addressRows.find(row => row[".id"] === path.slice(base.length + 1))!, fields) + if (req.method === "DELETE") addressRows = addressRows.filter(row => row[".id"] !== path.slice(base.length + 1)) + } + res.end("{}") + }); return + } + if (firewallWriteFixtures && /^\/rest\/(?:ip|ipv6)\/firewall\//.test(req.url ?? "")) { + res.setHeader("content-type", "application/json") + const path = decodeURIComponent(req.url ?? ""), base = "/rest/ip/firewall/filter" + if (req.method === "GET") { res.end(JSON.stringify(path === base ? firewallWriteRows : [])); return } + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", () => { + const data = body ? JSON.parse(body) : {} + if (path === base + "/get") { res.end(JSON.stringify({ ret: "false" })); return } + firewallWriteCommands++ + if (!firewallWriteIgnored) { + const fields = Object.fromEntries(Object.entries(data).map(([k, v]) => [k, ["disabled", "log"].includes(k) ? v === "yes" ? "true" : "false" : v])) as Record + if (req.method === "PUT") firewallWriteRows.push({ ".id": "*C", dynamic: "false", log: "false", disabled: "false", ...fields }) + if (req.method === "PATCH") Object.assign(firewallWriteRows.find(row => row[".id"] === path.slice(base.length + 1))!, fields) + if (req.method === "DELETE") firewallWriteRows = firewallWriteRows.filter(row => row[".id"] !== path.slice(base.length + 1)) + } + res.end("{}") + }); return + } + if (firewallMoveFixtures && /^\/rest\/(?:ip|ipv6)\/firewall\//.test(req.url ?? "")) { + res.setHeader("content-type", "application/json") + if (req.url === "/rest/ip/firewall/filter/move") { + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", () => { + const input = JSON.parse(body); firewallMoveCommands++ + if (!firewallIgnoreMove) { + const row = firewallMoveRows.find(row => row[".id"] === input.numbers)! + firewallMoveRows = firewallMoveRows.filter(item => item !== row) + const index = input.destination ? firewallMoveRows.findIndex(row => row[".id"] === input.destination) : firewallMoveRows.length + firewallMoveRows.splice(index, 0, row) + } + res.end("{}") + }); return + } + if (req.url === "/rest/ip/firewall/filter") { + for (const row of firewallMoveRows) row.packets = String(Number(row.packets) + 1) + res.end(JSON.stringify(firewallMoveRows)); return + } + res.end("[]"); return + } + if (firewallStateFixtures && /^\/rest\/(?:ip|ipv6)\/firewall\/(?:filter|nat|mangle|raw|address-list)$/.test(req.url ?? "")) { + firewallStateCalls++; res.setHeader("content-type", "application/json") + if (req.url === firewallFailedChunk) { res.statusCode = 503; res.end("{}"); return } + const flags = firewallKnownFlags ? { disabled: "false", dynamic: "false", log: "false" } : {} + const row = req.url!.endsWith("address-list") ? { ".id": "*list", list: "fixture-list", address: "192.0.2.1", ...flags } + : { ".id": "*rule", chain: "forward", action: "accept", packets: "0", "src-address-list": "fixture-list", "dst-address": "192.0.2.0/24", "in-interface": "ether1", "out-interface": "ether2", password: "never-return-firewall-key", ...flags } + res.end(JSON.stringify(firewallDuplicate && req.url === "/rest/ip/firewall/filter" ? [row, row] : [row])); return + } + if (liveFixtures && ["/rest/interface", "/rest/interface/monitor-traffic"].includes(req.url ?? "")) { + liveNativeCalls++; res.setHeader("content-type", "application/json") + if (req.url === "/rest/interface") { + res.end(JSON.stringify(["ether1", "ether2", "lo"].map(name => ({ name, running: "true", disabled: "false" })))); return + } + let body = "" + req.on("data", chunk => { body += chunk }) + req.on("end", () => { + const payload = JSON.parse(body), names = String(payload.interface).split(",") + assert.equal(payload.once, "") + const rows = names.slice(0, liveIncomplete ? 1 : undefined).map(name => ({ name, "rx-bits-per-second": "1000000", "tx-bits-per-second": "0", password: "never-return-live-key" })) + const update = liveRevokeConnection ? pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1", [liveRevokeConnection]) + : liveChangeSource ? pool.query("UPDATE servers SET updated_at=now() WHERE id=1") : Promise.resolve() + void update.then(() => res.end(JSON.stringify(rows))).catch(() => { res.statusCode = 500; res.end("{}") }) + }); return + } + if (probeInterfaceFixtures && ["/rest/interface", "/rest/interface/get", "/rest/ip/address"].includes(req.url?.split("?")[0] ?? "")) { + const path = new URL(req.url!, "http://localhost") + probeNativeCalls++; res.setHeader("content-type", "application/json") + if (path.pathname === "/rest/interface") { + assert.equal(req.method, "GET") + assert.deepEqual([...path.searchParams], [[".proplist", ".id,name,running,disabled"]]) + } + if (probeReadFailure) { res.statusCode = 503; res.end("{}"); return } + if (path.pathname === "/rest/interface/get") { + let body = ""; req.on("data", chunk => body += chunk) + req.on("end", () => { + const input = JSON.parse(body) + assert.equal(req.method, "POST") + assert.equal(input["value-name"], "disabled") + assert.ok(["*p2", "*p4"].includes(input[".id"])) + res.end(JSON.stringify({ ret: input[".id"] === "*p2" ? "false" : "invalid" })) + }) + return + } + const rows = path.pathname === "/rest/interface" ? [ + { ".id": "*p1", name: "ether1", running: "true", disabled: "false" }, + { ".id": "*p2", name: "gre-probe", running: "true" }, + { ".id": "*p3", name: "unknown-probe", disabled: "false" }, + { ".id": "*p4", name: "unknown-fallback", running: "true" }, + { ".id": "*p5", name: "invalid-flag", running: "true", disabled: "invalid" }, + ] + : [{ ".id": "*a1", interface: "ether1", address: "192.0.2.10/24", disabled: "false", invalid: "false", dynamic: "false", "private-key": "never-return-probe-key" }] + res.end(JSON.stringify(rows)); return + } + if (ospfAnalysisFixtures && req.url === "/rest/ip/address") { res.setHeader("content-type", "application/json"); res.end(JSON.stringify(ospfAnalysisAddresses)); return } + if (ospfFixtures && req.url?.startsWith("/rest/routing/ospf/interface-template") && ["PUT", "PATCH", "DELETE"].includes(req.method ?? "") || ospfFixtures && req.url === "/rest/routing/ospf/interface-template/unset") { + ospfPassiveWrites++ + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", () => { + res.setHeader("content-type", "application/json") + const fields = body ? JSON.parse(body) : {} + const id = req.url?.endsWith("/unset") ? fields[".id"] : decodeURIComponent(req.url!.split("/").at(-1)!) + const row = fixtureOspf["interface-template"].find(row => row[".id"] === id) + if (req.method === "PUT") { + const added = { ...fields, ".id": "*abc123", ...(Object.hasOwn(fields, "passive") ? { passive: "" } : {}) } + fixtureOspf["interface-template"].push(added); res.end(JSON.stringify(added)); return + } + if (!row) { res.statusCode = 404; res.end("{}"); return } + if (req.method === "DELETE") fixtureOspf["interface-template"] = fixtureOspf["interface-template"].filter(item => item !== row) + else if (req.url?.endsWith("/unset")) { if (!ospfIgnoreUnset && fields["value-name"] === "passive") delete row.passive } + else Object.assign(row, fields, Object.hasOwn(fields, "passive") ? { passive: "" } : {}) + res.end("{}") + }); return + } + if ((ospfFixtures || ospfAnalysisFixtures) && ["/rest/routing/ospf/interface-template/get", "/rest/routing/ospf/interface-template/print"].includes(req.url ?? "")) { + ospfTemplateFlagReads++ + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", () => { + res.setHeader("content-type", "application/json") + const input = JSON.parse(body) + if (req.url?.endsWith("/print")) { + const query = JSON.stringify(input[".query"]) + if (![JSON.stringify(["passive"]), JSON.stringify(["passive", "#!"])].includes(query)) { res.statusCode = 400; res.end("{}"); return } + const absent = query === JSON.stringify(["passive", "#!"]) + res.end(JSON.stringify(fixtureOspf["interface-template"].filter(row => ("passive" in row) !== absent).map(row => ({ ".id": row[".id"] })))); return + } + const row = fixtureOspf["interface-template"].find(row => row[".id"] === input[".id"]) as Record | undefined + if (!row || !["passive", "disabled", "use-bfd"].includes(input["value-name"])) { res.statusCode = 404; res.end("{}"); return } + if (input["value-name"] === "use-bfd" && row.interfaces === "fixture-loop") { res.end("[]"); return } + const key = input["value-name"], value = key === "use-bfd" && ospfAnalysisFixtures ? "false" : key === "passive" && row[key] === "" ? "true" : row[key] ?? "false" + if (key === "passive" && row[key] === undefined) { res.end("[]"); return } + res.end(JSON.stringify({ ret: value })) + }); return + } + if (ospfAnalysisFixtures && req.url?.startsWith("/rest/routing/ospf/")) { + if (req.method !== "GET") ospfAnalysisDispatches++ + res.setHeader("content-type", "application/json") + const key = req.url.slice("/rest/routing/ospf/".length) + if (req.method === "PATCH" && key.startsWith("interface-template/")) { + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", () => { + const id = decodeURIComponent(key.slice("interface-template/".length)), target = fixtureOspf["interface-template"].find(row => row[".id"] === id) + if (!target) { res.statusCode = 404; res.end("{}"); return } + if (ospfCostWriteFault !== "ignored") target.cost = JSON.parse(body).cost + if (ospfCostWriteFault === "changed-field") Object.assign(target, { opaque: "UNEXPECTED_OSPF_CHANGE" }) + if (ospfAfterCostPatch) { const work = ospfAfterCostPatch; ospfAfterCostPatch = undefined; work() } + if (ospfCostWriteFault === "lost-response") { req.socket.destroy(); return } + res.end("{}") + }); return + } + const rows = key === "neighbor" ? [{ ".id": "*analysis-neighbor", interface: "fixture-gre", instance: "fixture-ospf", "router-id": "192.0.2.2", address: "192.0.2.2", state: "Full" }, ...ospfAnalysisExtraNeighbors] + : key === "interface-template" ? fixtureOspf["interface-template"].map(row => ({ ...row, "use-bfd": row.interfaces === "fixture-loop" ? undefined : "false" })) : fixtureOspf[key as keyof typeof fixtureOspf] ?? [] + if (ospfPauseOnTemplatesConnection && key === "interface-template" && ospfTemplateListsBeforePause-- === 0) { + const connection = ospfPauseOnTemplatesConnection; ospfPauseOnTemplatesConnection = undefined + void pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}',to_jsonb('paused'::text)) WHERE id=$1", [connection]).then(() => res.end(JSON.stringify(rows)), () => { res.statusCode = 503; res.end("{}") }) + return + } + if(ospfPreviewPauseConnection&&key==="neighbor"){ + const connection=ospfPreviewPauseConnection;ospfPreviewPauseConnection=undefined + void pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}',to_jsonb('paused'::text)) WHERE id=$1",[connection]).then(()=>res.end(JSON.stringify(rows)),()=>{res.statusCode=503;res.end("{}")}) + return + } + res.end(JSON.stringify(rows)); return + } + if (wanFixtures && ["/rest/ip/dhcp-client", "/rest/ip/address", "/rest/ip/route"].includes(req.url ?? "")) { + res.setHeader("content-type", "application/json") + if (wanAddressFailure && req.url === "/rest/ip/address") { res.statusCode = 503; res.end("{}"); return } + const rows = req.url === "/rest/ip/route" ? wanRoutes : req.url === "/rest/ip/address" ? [{ address: "192.0.2.10/24", interface: "ether1" }] : [{ address: "192.0.2.10/24", interface: "ether1", gateway: "192.0.2.1", status: "bound", password: "never-return-wan-key" }] + res.end(JSON.stringify(rows)); return + } + if (containerMutationFixtures && req.url === "/rest/interface") { + res.setHeader("content-type", "application/json"); res.end(JSON.stringify([containerParent])); return + } + if (containerMutationFixtures && req.url?.startsWith("/rest/container")) { + res.setHeader("content-type", "application/json") + if (req.method === "DELETE") { + containerDeletes++ + if (!ignoreContainerDelete) mutableContainers = mutableContainers.filter(row => req.url !== "/rest/container/" + encodeURIComponent(String(row[".id"]))) + res.end("{}"); return + } + if (req.method === "POST") { + let raw = ""; req.on("data", chunk => { raw += chunk }); req.on("end", () => { + const input = JSON.parse(raw), row = mutableContainers.find(row => row[".id"] === input[".id"]) + if (!row) { res.statusCode = 404; res.end("{}"); return } + containerCommands++; row.status = req.url?.endsWith("/start") ? "running" : "stopped" + if (replaceContainerAfterCommand) row.name = "replacement-after-command" + res.end("{}") + }); return + } + res.end(JSON.stringify(mutableContainers)); return + } + if (preflightFixtures && req.url && Object.hasOwn(preflightFixtures, req.url)) { + res.setHeader("content-type", "application/json") + if (req.url === preflightUnavailable) { res.statusCode = 503; res.end("{}"); return } + res.end(JSON.stringify(preflightFixtures[req.url])); return + } + if (greMutationFixtures && req.url?.startsWith("/rest/")) { + greRevisionReads++ + res.setHeader("content-type","application/json") + const path = decodeURIComponent(req.url.slice(5)), bases = ["/interface/gre","/ip/address","/ipv6/address"] + const base = bases.find(base=>path===base||path.startsWith(base+"/")) + if (req.method === "GET") { + if(path==="/ip/route"){ + greNativeRouteReads++ + if(greNativeLateDynamicAt&&greNativeRouteReads>=greNativeLateDynamicAt)greMutationDependencies[path]=[{".id":"*LATEDYNAMIC",dynamic:"true",gateway:"192.0.2.2%native-gre"}] + } + if (base) { res.end(JSON.stringify((greMutationCollections[base]??[]).map(row=>Object.fromEntries(Object.entries(row).filter(([key,value])=>(key!=="comment"||value!=="")&&(base!=="/interface/gre"||key!=="dynamic")))))); return } + if (path==="/interface") { res.end(JSON.stringify(greMutationDependencies[path]??[{ ".id":"*E",name:"ether1",type:"ether" },...(greMutationCollections["/interface/gre"]??[]).map(row=>({".id":row[".id"],name:row.name,type:"gre",...(row.dynamic===undefined?{}:{dynamic:row.dynamic})}))]));return } + if(greNativeCounterTick&&path.includes("/firewall/"))for(const row of greMutationDependencies[path]??[]){row.packets=String(Number(row.packets??"0")+1);row.bytes=String(Number(row.bytes??"0")+100)} + res.end(JSON.stringify(greMutationDependencies[path]??[])); return + } + let body = ""; req.on("data",chunk=>body+=chunk); req.on("end",async()=>{ + const data = body?JSON.parse(body):{} + if(path==="/interface/get"){ + const row=greMutationDependencies[path]?.find(row=>row[".id"]===data[".id"]) + if(row&&data["value-name"]==="dynamic"){res.end(JSON.stringify(row.ret==="empty-array"?[]:{ret:row.ret}));return} + res.statusCode=400;res.end(JSON.stringify({error:400,detail:"Property not available"}));return + } + if(path==="/interface/print"){ + assert.deepEqual(data[".query"],[".id="+greMutationDependencies["/interface/get"]?.[0]?.[".id"],data[".query"]?.includes("-dynamic")?"-dynamic":"dynamic","#&"]) + const key=data[".query"]?.includes("-dynamic")?"/interface/static-print":"/interface/dynamic-print" + res.end(JSON.stringify(greMutationDependencies[key]??[]));return + } + if (path.endsWith("/get") && base) { + const row = (greMutationCollections[base]??[]).find(row=>row[".id"]===data[".id"]) + res.end(JSON.stringify({ret:row?.[data["value-name"]]??(["comment","ipsec-secret","from-pool"].includes(data["value-name"])?"":"false")})); return + } + greMutationCommands++ + if(base==="/interface/gre"&&data["ipsec-secret"]===""&&["yes","true"].includes(data["allow-fast-path"])){ + res.statusCode=400;res.end(JSON.stringify({error:400,detail:"failure: cannot enable fastpath together with ipsec yet"}));return + } + if (!greMutationIgnored && base) { + const fields = Object.fromEntries(Object.entries(data).map(([key,value])=>[key,value==="yes"?"true":value==="no"?"false":String(value)])) + if (req.method==="PUT") { + const row = base==="/interface/gre"?{".id":"*G"+(greMutationNextId++),"local-address":"0.0.0.0",mtu:"1476",keepalive:"10s,10",dscp:"inherit",disabled:"false",dynamic:"false",running:"false","clamp-tcp-mss":"true","allow-fast-path":"true","ipsec-secret":"",comment:"",...fields}:{".id":"*A"+(greMutationNextId++),disabled:"false",dynamic:"false",invalid:"false",advertise:"false","no-dad":"false","eui-64":"false","from-pool":"",comment:"",...fields} + ;(greMutationCollections[base]??=[]).push(row) + } else if (req.method==="PATCH") Object.assign(greMutationCollections[base].find(row=>row[".id"]===path.slice(base.length+1))!,fields) + else if (req.method==="DELETE") greMutationCollections[base]=greMutationCollections[base].filter(row=>row[".id"]!==path.slice(base.length+1)) + } + if (greMutationRevoke) { const id=greMutationRevoke;greMutationRevoke=undefined;await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1",[id]) } + await greRevisionAfterCommand?.() + if (greMutationLost) { greMutationLost=false;req.socket.destroy();return } + res.end("{}") + });return + } + if (filterCatalogFixtures && ["/rest/interface/gre", "/rest/routing/filter/rule"].includes(req.url ?? "")) { + res.setHeader("content-type","application/json") + if (req.url === "/rest/interface/gre") { res.end(JSON.stringify([{ ".id":"*G",name:"filter-gre","local-address":"192.0.2.1","remote-address":"192.0.2.2","ipsec-secret":"NEVER_FILTER_PSK" }])); return } + res.end(JSON.stringify(filterCatalogMalformed ? { rules: [] } : [ + {".id":"*F1",chain:"bgp-in",rule:"if (bgp-communities includes 64512:1) { set gw 192.0.2.2; accept; }",disabled:"true","private-key":"NEVER_FILTER_NATIVE_KEY"}, + {".id":"*F2",chain:"bgp-in",rule:"jump other;"}, + {".id":"*F3",chain:"other",rule:"reject;",disabled:"false"}, + ])); return + } + if (deviceStateFixtures && req.url?.startsWith("/rest/container")) { + res.setHeader("content-type", "application/json") + if (req.url === "/rest/container") { + if (containerStateFailure === "unsupported") { res.statusCode = 404; res.end("{}"); return } + res.end(JSON.stringify((reverseContainerRows ? [2, 1] : [1, 2]).map(index => ({ ".id": `*c${index}`, name: `fixture-container-${index}`, "remote-image": "test/image:latest", envlists: "fixture-env", cmd: "never-return-this-command", entrypoint: "never-return-this-entrypoint", env: "never-return-inline-env", running: index === 1 ? "true" : undefined })))); return + } + if (req.url === "/rest/container/envs") { + if (containerStateFailure === "envs") { res.statusCode = 503; res.end("{}"); return } + res.end(JSON.stringify([{ list: "fixture-env", key: "UNCLASSIFIED_VALUE", value: "never-return-this-env" }])); return + } + res.end("[]"); return + } + if (deviceStateFixtures && req.url === "/rest/system/clock") { + res.setHeader("content-type", "application/json"); res.end(JSON.stringify({ "gmt-offset": certificateClockKnown ? "+07:00" : "unknown" })); return + } + if (deviceStateFixtures && req.url === "/rest/certificate") { + res.setHeader("content-type", "application/json") + if (healthUnavailable) { res.statusCode = 503; res.end("{}"); return } + if (certificateStateMalformed) { res.end(JSON.stringify({ certificates: [] })); return } + res.end(JSON.stringify([{ ".id": "*cert1", name: "fixture-certificate", flags: "T", "invalid-before": "2025-01-01 00:00:00", "invalid-after": "2035-01-01 00:00:00", "private-key": "never-return-certificate-key" }, { ".id": "*cert2", name: "unknown-certificate" }])); return + } + if (recursiveCatalogFixtures && req.url?.startsWith("/rest/ip/route")) { + res.setHeader("content-type", "application/json") + if (healthUnavailable) { res.statusCode = 503; res.end("{}"); return } + if (recursiveMutations && req.method === "PUT") { + let raw = ""; req.on("data", chunk => { raw += chunk }); req.on("end", async () => { + const fields = JSON.parse(raw), row = { ...fields, ".id": `*created-${++recursiveMutationCount}`, static: "true" } + if (recursiveIgnorePayload) row.distance = "254" + recursiveCatalogRows.push(row) + if (recursivePauseConnection) { + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}',to_jsonb($2::text)) WHERE id=$1", [recursivePauseConnection, "paused"]) + recursivePauseConnection = undefined + } + res.end(JSON.stringify(row)) + }); return + } + if (recursiveMutations && req.method === "DELETE") { + recursiveMutationCount++ + recursiveCatalogRows = recursiveCatalogRows.filter(row => req.url !== "/rest/ip/route/" + encodeURIComponent(String(row[".id"]))) + res.end("{}"); return + } + if (req.url.endsWith("/get")) { + let raw = ""; req.on("data", chunk => { raw += chunk }); req.on("end", () => { + if (recursivePropertyFailure) { res.statusCode = 503; res.end("{}"); return } + const input = JSON.parse(raw); res.end(input["value-name"] === "disabled" ? JSON.stringify({ ret: "false" }) : "[]") + }); return + } + res.end(JSON.stringify(recursiveCatalogRows)); return + } + if (greCatalogFixtures && req.url === "/rest/interface/gre") { + res.setHeader("content-type", "application/json") + if (healthUnavailable) { res.statusCode = 503; res.end("{}"); return } + res.end(JSON.stringify([1, 2].map(id => ({ ".id": `*gre${id}`, name: `catalog-gre-${id}`, running: "true", disabled: "false", "ipsec-secret": "never-return-gre-key" })))); return + } + if (bgpFixtures && req.url?.startsWith("/rest/routing/bgp/")) { + const key = req.url.slice("/rest/routing/bgp/".length) as keyof typeof fixtureBgp + if (key in fixtureBgp) { res.setHeader("content-type", "application/json"); if (key === "session" && healthUnavailable) { res.statusCode = 503; res.end("{}"); return }; res.end(JSON.stringify(key === "session" && bgpSessionOverride !== undefined ? bgpSessionOverride : fixtureBgp[key])); return } + } + if (ecmpFixtures && req.url?.startsWith("/rest/ip/route")) { + res.setHeader("content-type", "application/json") + if (req.method === "PATCH") { + let raw = ""; req.on("data", chunk => { raw += chunk }); req.on("end", () => { + const row = fixtureEcmpRoutes.find(row => req.url!.endsWith(encodeURIComponent(row[".id"])))! + Object.assign(row, JSON.parse(raw)); ecmpWrites++ + const best = Math.min(...fixtureEcmpRoutes.map(row => Number(row.distance))) + for (const route of fixtureEcmpRoutes) route.active = String(Number(route.distance) === best) + if (loseEcmpGateway) fixtureEcmpRoutes[0]["immediate-gw"] = "" + res.end(JSON.stringify(row)) + }); return + } + res.end(JSON.stringify(fixtureEcmpRoutes)); return + } + if (req.url?.startsWith("/rest/routing/ospf/interface?.proplist=")) { + res.setHeader("content-type", "application/json") + res.end(JSON.stringify([{ ".id": "*runtime", interface: "fixture-ospf", state: "ptp", "auth-key": "never-return-ospf-key" }])) + return + } + if (ospfFixtures && req.url?.startsWith("/rest/routing/ospf/")) { + const key = req.url.slice("/rest/routing/ospf/".length) as keyof typeof fixtureOspf + if (key in fixtureOspf) { res.setHeader("content-type", "application/json"); res.end(JSON.stringify(fixtureOspf[key])); return } + } + if (req.url === "/rest/routing/bfd/session") { + res.setHeader("content-type", "application/json") + if (healthUnavailable) { res.statusCode = 503; res.end("{}"); return } + res.end(JSON.stringify([{ ".id": "*bfd", state: healthState ? "up" : "down", "remote-address": healthBfdRemote, "local-address": "192.0.2.1%fixture-ospf" }])); return + } + if (interfaceFixtures && req.url === "/rest/interface/gre") { + res.setHeader("content-type", "application/json"); res.end(JSON.stringify([{ ".id": "*gre", name: "fixture-gre", disabled: "false" }])); return + } + if (interfaceFixtures && req.url === "/rest/interface") { + res.setHeader("content-type", "application/json"); res.end(JSON.stringify([{ ".id": "*gre", name: "fixture-gre", type: "gre" }, { ".id": "*fixture", name: "fixture-bridge", type: "bridge" }])); return + } + if (interfaceFixtures && req.url?.startsWith("/rest/ip/vrf")) { + res.setHeader("content-type", "application/json") + if (req.method === "PUT") { + let raw = ""; req.on("data", chunk => { raw += chunk }); req.on("end", () => { + const { "place-before": before, ...fields } = JSON.parse(raw) + const row = { ".id": "*CREATEDVRF", ...fields }; vrfWrites++ + const index = ignoreVrfPlacement ? -1 : fixtureVrfs.findIndex(item => item[".id"] === before) + if (index >= 0) fixtureVrfs.splice(index, 0, row); else fixtureVrfs.push(row) + res.end(JSON.stringify(row)) + }); return + } + if (req.method === "DELETE") { fixtureVrfs = fixtureVrfs.filter(row => !req.url!.endsWith(encodeURIComponent(row[".id"]))); vrfWrites++; res.end("{}"); return } + res.end(JSON.stringify(fixtureVrfs)); return + } + if (req.url === "/rest/routing/table" || req.url === "/rest/routing/rule") { + res.setHeader("content-type", "application/json") + res.end(JSON.stringify(req.url.endsWith("/rule") ? fixtureRoutingRules : [{ ".id": "*main", name: "main", fib: "true" }, { ".id": "*table", name: "fixture-table", fib: "" }])); return + } + if (req.url === "/rest/routing/ospf/neighbor" || req.url === "/rest/ip/route") { + res.setHeader("content-type", "application/json") + if (healthUnavailable) { res.statusCode = 503; res.end(JSON.stringify({ error: "fixture unavailable" })); return } + res.end(JSON.stringify(req.url.endsWith("/neighbor") ? [{ ".id": "*neighbor", interface: "fixture-ospf", "router-id": "192.0.2.2", state: healthState ? "Full" : "Down" }] : [{ ".id": "*healthroute", "dst-address": "192.0.2.0/24", "routing-table": fixtureRouteTable, active: healthState ? "true" : "false" }])); return + } + if (req.url === "/rest/interface/bridge" || req.url === "/rest/interface/bridge/*fixture") { + res.setHeader("content-type", "application/json") + if (req.method === "PATCH") { + let raw = ""; req.on("data", chunk => { raw += chunk }); req.on("end", () => { bridge = { ...bridge, ...JSON.parse(raw) }; res.end(JSON.stringify(bridge)) }); return + } + res.end(JSON.stringify([bridge])); return + } + if (req.url === "/rest/system/license") { res.setHeader("content-type", "application/json"); res.end(JSON.stringify({ "system-id": "fixture-primary" })); return } + if (req.url === "/rest/ip/settings" && req.method !== "GET") { res.writeHead(400); res.end("{}"); return } + if (req.url === "/rest/ip/settings/set" && req.method !== "POST") { res.writeHead(405); res.end("{}"); return } + if (req.url !== "/rest/ip/settings" && req.url !== "/rest/ip/settings/set") { res.setHeader("content-type", "application/json"); res.end("[]"); return } + if (req.url === "/rest/ip/settings") { + reconcileSettingsReads++ + if (reconcileReadHook) { + const hook = reconcileReadHook; reconcileReadHook = undefined + void hook().then(() => { res.setHeader("content-type", "application/json"); res.end(JSON.stringify(settings)) }, () => { res.statusCode = 503; res.end("{}"); }) + return + } + } + if (req.url === "/rest/ip/settings/set") { + let body = ""; req.on("data", value => { body += value }); req.on("end", () => { + settings = { ...settings, ...JSON.parse(body) }; writes++ + if (failHealthAfterWrite) healthState = false + if (loseHealthResponse) healthUnavailable = true + res.writeHead(204); res.end() + }); return + } + res.setHeader("content-type", "application/json"); res.end(JSON.stringify(settings)) +}) +await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) +const port = (router.address() as { port: number }).port +let app: Awaited> | undefined +try { + const { configurationRows, resourceById } = await import("./resources.js") + const tableSpec = resourceById("routing_table") + assert.equal(configurationRows(tableSpec, [{ ".id": "*1", name: "isolated", fib: "" }])[0].fib, "false") + assert.equal(configurationRows(tableSpec, [{ ".id": "*1", name: "main", fib: "true" }])[0].fib, "true") + assert.equal(configurationRows(tableSpec, [{ ".id": "*1", name: "unknown" }])[0].fib, undefined) + assert.equal(configurationRows(resourceById("bridge"), [{ disabled: "" }])[0].disabled, "") + assert.deepEqual(configurationRows(resourceById("ipv6_address"), [{ ".id": "*auto", address: "fe80::1/64", dynamic: "true" }, { ".id": "*explicit", address: "fe80::2/64", dynamic: "false" }, { ".id": "*global", address: "fd00::1/64", dynamic: "true" }]).map(row => row[".id"]), ["*explicit", "*global"]) + const orderedVrfs = [{ ".id": "*a", name: "customer", interfaces: "ether2" }, { ".id": "*b", name: "main", interfaces: "all" }] + assert.deepEqual(configurationRows(resourceById("vrf"), [{ ".id": "*a", name: "customer", interfaces: ["ether3", "ether2"] }]), configurationRows(resourceById("vrf"), [{ ".id": "*a", name: "customer", interfaces: "ether2,ether3" }])) + assert.notDeepEqual(configurationRows(resourceById("vrf"), orderedVrfs), configurationRows(resourceById("vrf"), [...orderedVrfs].reverse())) + await applySqlMigrations(pool) + await applySqlMigrations(pool) + console.log("✓ All migrations apply and are repeatable on PostgreSQL 18") + await (await import("../../test/partition-timezones.js")).verifyPartitionTimezones(pool) + // refreshScheduler starts enabled jobs immediately. Keep external GeoIP + // downloads disabled in this owned fixture before any telemetry refresh. + await pool.query("INSERT INTO geoip_settings(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl) VALUES(1,'test-router','127.0.0.1',$1,false),(2,'outside-scope','127.0.0.1',$1,false)", [port]) + await updateMcpSettings({ enabled: true }, actor) + const { registerManagementChannel, verifyManagementChannel } = await import("./management.js") + let alternateIdentity = "fixture-primary" + const alternate = createServer((_req, res) => { res.setHeader("content-type", "application/json"); res.end(JSON.stringify({ "system-id": alternateIdentity })) }) + await new Promise(resolve => alternate.listen(0, "127.0.0.2", resolve)) + try { + const management = { serverId: 1, host: "127.0.0.2", port: (alternate.address() as { port: number }).port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture", attestation: "Изолированный тестовый канал с отдельным адресом loopback для проверки границы допуска", expiresAt: new Date(Date.now() + 1800000).toISOString() } + await assert.rejects(registerManagementChannel({ ...management, host: "127.0.0.1" }, actor)) + alternateIdentity = "different-device"; await assert.rejects(registerManagementChannel(management, actor)) + alternateIdentity = "fixture-primary" + const channel = await registerManagementChannel(management, actor) + assert.equal((await verifyManagementChannel(1, channel.id)).channelId, channel.id) + await pool.query("UPDATE mcp_management_channels SET revoked_at=now() WHERE id=$1", [channel.id]) + await assert.rejects(verifyManagementChannel(1, channel.id)) + console.log("✓ Independent channel rejects primary aliases and different devices; identity verification and revocation work") + } finally { await new Promise(resolve => alternate.close(() => resolve())) } + const input = mcpConnectionCreateSchema.parse({ name: "Integration", mode: "approval", expiresAt: new Date(fixtureAuthExpiresAt).toISOString(), permissions: ["mm:network:write", "mm:servers:read"], scope: { serverIds: [1], operationIds: ["ros.ip_settings.list", "ros.ip_settings.update", "servers.list", "operations.recover", "operations.reconcile"], allowGlobal: false } }) + const issued = await createMcpConnection(input, actor) + const principal = await authenticateMcpToken(issued.token) + const credentials = await pool.query("SELECT digest FROM mcp_credentials WHERE connection_id=$1", [issued.connection.id]) + assert.equal(credentials.rows[0].digest, tokenDigest(issued.token)); assert.notEqual(credentials.rows[0].digest, issued.token) + const read = await invokeMcpTool(principal, "mm_ip_settings_list", { serverId: 1 }) as { items: unknown[] } + assert.equal(read.items.length, 1) + await assert.rejects(invokeMcpTool(principal, "mm_ip_settings_list", { serverId: 2 })) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE status='denied'")).rows[0].n, 1) + console.log("✓ Opaque credentials, explicit server scope, denied-call audit") + const wanAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped WAN runtime", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:servers:read"], scope: { serverIds: [1], operationIds: ["servers.wan-runtime.read"], allowGlobal: false } }), actor) + const wanPrincipal = await authenticateMcpToken(wanAccess.token) + await pool.query("UPDATE servers SET wan_uplinks=$1::jsonb WHERE id=1", [JSON.stringify([{ id: "wan", iface: "ether1", name: "Fixture WAN", isp: "Fixture ISP", ip: "192.0.2.10" }])]) + wanFixtures = true; countCatalogCalls = true; catalogCalls = 0 + try { + type WanState = { defaultRouteStatus: string; defaultInterface: string | null; forwardingVerified: boolean; uplinks: { leasedIp: string | null; defaultKnown: boolean }[] } + const state = await invokeMcpTool(wanPrincipal, "mm_server_wan_runtime_get", { serverId: 1 }) as WanState + assert.equal(state.defaultRouteStatus, "confirmed"); assert.equal(state.defaultInterface, "ether1") + assert.equal(state.uplinks[0].leasedIp, "192.0.2.10"); assert.equal(state.forwardingVerified, false) + assert.equal(catalogCalls, 3); assert.ok(!JSON.stringify(state).includes("never-return-wan-key")) + await assert.rejects(invokeMcpTool(wanPrincipal, "mm_server_wan_runtime_get", { serverId: 2 })); assert.equal(catalogCalls, 3) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + await assert.rejects(invokeMcpTool(wanPrincipal, "mm_server_wan_runtime_get", { serverId: 1 })); assert.equal(catalogCalls, 3) + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + wanRoutes = [wanDefault, { ...wanDefault, ".id": "*wan2", gateway: "198.51.100.1", "immediate-gw": "198.51.100.1%ether2" }] + const ambiguous = await invokeMcpTool(wanPrincipal, "mm_server_wan_runtime_get", { serverId: 1 }) as WanState + assert.equal(ambiguous.defaultRouteStatus, "ambiguous"); assert.equal(ambiguous.defaultInterface, null); assert.equal(ambiguous.uplinks[0].defaultKnown, false) + wanAddressFailure = true + await assert.rejects(invokeMcpTool(wanPrincipal, "mm_server_wan_runtime_get", { serverId: 1 })) + } finally { + wanFixtures = false; wanAddressFailure = false; countCatalogCalls = false + await pool.query("UPDATE servers SET enabled=true,wan_uplinks='[]'::jsonb WHERE id=1") + } + console.log("✓ WAN runtime: only selected enabled server, native default/lease, explicit ECMP ambiguity, no credential echo or forwarding claim, failed source is not fabricated empty") + const historySections = ["filters", "recursive-routes", "firewall", "wireguard", "ipsec", "gre"] + const historyAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped configuration history", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:filters:read"], scope: { serverIds: [1], operationIds: historySections.map(section => `config.${section}.revisions.read`), allowGlobal: false } }), actor) + const historyPrincipal = await authenticateMcpToken(historyAccess.token) + const historyIds: string[] = [] + for (const section of historySections) { + for (const sid of [1, 2]) { + const id = randomUUID(); historyIds.push(id) + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload,note) VALUES($1,$2,$3,'observed',$4,$5::jsonb,$6)", [id, sid, section, id, JSON.stringify([{ privateKey: "never-return-this-payload", secret: "fixture" }]), `fixture-${sid}-${section}`]) + } + const tool = `mm_${section.replaceAll("-", "_")}_revisions_get` + const result = await invokeMcpTool(historyPrincipal, tool, { serverId: 1 }) as { revisions: { serverId: string; section: string; itemCount: number }[]; retentionLimit: number } + assert.equal(result.revisions.length, 1); assert.equal(result.retentionLimit, 50) + assert.equal(result.revisions[0].serverId, "1"); assert.equal(result.revisions[0].section, section); assert.equal(result.revisions[0].itemCount, 1) + assert.ok(!JSON.stringify(result).includes("never-return-this-payload")) + await assert.rejects(invokeMcpTool(historyPrincipal, tool, { serverId: 2 })) + await assert.rejects(invokeMcpTool(historyPrincipal, tool, { serverId: 1, section: "wireguard" })) + } + const historyDenied = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "History permission boundary", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["config.filters.revisions.read"], allowGlobal: false } }), actor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(historyDenied.token), "mm_filters_revisions_get", { serverId: 1 })) + for (let index = 0; index < 51; index++) { + const id = randomUUID(); historyIds.push(id) + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,1,'gre','observed',$1,'[]'::jsonb)", [id]) + } + const boundedHistory = await invokeMcpTool(historyPrincipal, "mm_gre_revisions_get", { serverId: 1 }) as { revisions: unknown[]; incomplete: boolean } + assert.equal(boundedHistory.revisions.length, 50); assert.equal(boundedHistory.incomplete, true) + await pool.query("DELETE FROM config_revisions WHERE id=ANY($1::text[])", [historyIds]) + console.log("✓ Six configuration histories share REST metadata service, bound results, reject hidden servers and never expose saved secret payloads") + const greCatalogAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped GRE catalog", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:filters:read"], scope: { serverIds: [1], operationIds: ["routing.gre.catalog.read"], allowGlobal: false } }), actor) + const greCatalogPrincipal = await authenticateMcpToken(greCatalogAccess.token) + greCatalogFixtures = true; countCatalogCalls = true; catalogCalls = 0 + const greCatalog = await invokeMcpTool(greCatalogPrincipal, "mm_gre_catalog_get", { limit: 1 }) as { serverIds: number[]; tunnels: { serverId: string }[]; total: number; nextOffset: number; failures: unknown[] } + assert.deepEqual(greCatalog.serverIds, [1]); assert.equal(greCatalog.total, 2); assert.equal(greCatalog.tunnels.length, 1); assert.equal(greCatalog.nextOffset, 1); assert.equal(greCatalog.tunnels[0].serverId, "1"); assert.equal(greCatalog.failures.length, 0); assert.equal(catalogCalls, 2) + assert.ok(!JSON.stringify(greCatalog).includes("never-return-gre-key")) + await assert.rejects(invokeMcpTool(greCatalogPrincipal, "mm_gre_catalog_get", { serverId: 2 })); assert.equal(catalogCalls, 2) + const greNext = await invokeMcpTool(greCatalogPrincipal, "mm_gre_catalog_get", { offset: 1, limit: 2 }) as { tunnels: { name: string }[]; nextOffset: null; incomplete: boolean } + assert.deepEqual(greNext.tunnels.map(tunnel => tunnel.name), ["catalog-gre-2"]) + assert.equal(greNext.nextOffset, null); assert.equal(greNext.incomplete, true) + healthUnavailable = true + const failedGre = await invokeMcpTool(greCatalogPrincipal, "mm_gre_catalog_get", {}) as { failures: { serverId: string }[]; incomplete: boolean } + assert.equal(failedGre.failures.length, 1); assert.equal(failedGre.failures[0].serverId, "1"); assert.equal(failedGre.incomplete, true) + greCatalogFixtures = false; countCatalogCalls = false; healthUnavailable = false + console.log("✓ GRE routing catalog shares REST polling/parser, scopes before polling, paginates, strips IPsec keys and preserves device failures") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl) VALUES(43,'filter-catalog-owner','127.0.0.1',$1,false),(44,'NEVER_FILTER_FOREIGN','192.0.2.44',1,false)",[port]) + await pool.query("INSERT INTO filter_rules(server_id,community,description) VALUES(43,'64512:99','own cached hint'),(44,'64512:100','NEVER_FILTER_CACHED')") + const filterAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Scoped BGP filters",mode:"observer",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:filters:read"],scope:{serverIds:[43],operationIds:["routing.filters.catalog.read"],allowGlobal:false}}),actor) + const filterPrincipal = await authenticateMcpToken(filterAccess.token) + filterCatalogFixtures = true; countCatalogCalls = true; catalogCalls = 0 + const { default: FilterFastify } = await import("fastify"), {validatorCompiler: filterValidator,serializerCompiler: filterSerializer} = await import("@fastify/type-provider-zod") + const filterHttp = FilterFastify(); filterHttp.setValidatorCompiler(filterValidator); filterHttp.setSerializerCompiler(filterSerializer); await filterHttp.register((await import("../../routes/filters.js")).default,{prefix:"/api"}) + type FilterPage = {rulesets:{serverId:string;live:boolean;stale:boolean;source:string;error:string|null;rules:{items:{community:string}[];total:number};nativeRules:{items:{rosId:string;enabled:boolean|null}[];total:number}|null}[];incomplete:boolean;writesPerformed:boolean} + try { + const beforeCache = (await pool.query("SELECT * FROM filter_rules WHERE server_id=43")).rows + const fresh = await invokeMcpTool(filterPrincipal,"mm_bgp_filter_rules_get",{limit:1}) as FilterPage + assert.equal(catalogCalls,2); assert.equal(fresh.rulesets[0].serverId,"43"); assert.equal(fresh.rulesets[0].live,true); assert.equal(fresh.rulesets[0].rules.items[0].community,"64512:1") + assert.equal(fresh.rulesets[0].nativeRules!.total,2); assert.equal(fresh.rulesets[0].nativeRules!.items[0].enabled,false); assert.equal(fresh.writesPerformed,false); assert.equal(fresh.incomplete,true) + assert.ok(!JSON.stringify(fresh).includes("NEVER_FILTER")); assert.deepEqual((await pool.query("SELECT * FROM filter_rules WHERE server_id=43")).rows,beforeCache) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE server_id=43")).rows[0].n,0) + const second = await invokeMcpTool(filterPrincipal,"mm_bgp_filter_rules_get",{offset:1,limit:1}) as FilterPage + assert.equal(second.rulesets[0].nativeRules!.items[0].rosId,"*F2"); assert.equal(second.rulesets[0].nativeRules!.items[0].enabled,null) + const noPoll = catalogCalls + await assert.rejects(invokeMcpTool(filterPrincipal,"mm_bgp_filter_rules_get",{serverId:44})); assert.equal(catalogCalls,noPoll) + const cache = await invokeMcpTool(filterPrincipal,"mm_bgp_filter_rules_get",{source:"cache"}) as FilterPage + assert.equal(catalogCalls,noPoll); assert.equal(cache.rulesets[0].rules.items[0].community,"64512:99"); assert.equal(cache.rulesets[0].nativeRules,null) + assert.ok(!JSON.stringify(cache).includes("NEVER_FILTER")) + const rest = await filterHttp.inject({method:"GET",url:"/api/filters/rules?serverId=43"}); assert.equal(rest.statusCode,200,rest.body); assert.equal(rest.json().live,true) + assert.deepEqual(rest.json().rulesets[0].rules.map((row:{community:string})=>row.community),fresh.rulesets[0].rules.items.map(row=>row.community)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE server_id=43")).rows[0].n,1) + filterCatalogMalformed = true + const stale = await invokeMcpTool(filterPrincipal,"mm_bgp_filter_rules_get",{}) as FilterPage + assert.equal(stale.rulesets[0].live,false); assert.equal(stale.rulesets[0].stale,true); assert.equal(stale.rulesets[0].source,"cache"); assert.equal(stale.rulesets[0].nativeRules,null); assert.ok(stale.rulesets[0].error) + assert.equal(stale.rulesets[0].rules.items[0].community,"64512:1"); assert.equal(stale.incomplete,true) + const failedRest = await filterHttp.inject({method:"GET",url:"/api/filters/rules?serverId=43"}); assert.equal(failedRest.json().stale,true) + await pool.query("UPDATE servers SET enabled=false WHERE id=43") + const beforeDisabled = catalogCalls + const disabled = await invokeMcpTool(filterPrincipal,"mm_bgp_filter_rules_get",{source:"cache"}) as FilterPage + assert.equal(disabled.rulesets[0].rules.items[0].community,"64512:1"); assert.equal(catalogCalls,beforeDisabled) + assert.equal((await filterHttp.inject({method:"GET",url:"/api/filters/rules?serverId=43"})).json().stale,true); assert.equal(catalogCalls,beforeDisabled) + for(const id of ["","bad","0","1abc"]) assert.equal((await filterHttp.inject({method:"GET",url:"/api/filters/rules?serverId="+id})).statusCode,400) + assert.equal(catalogCalls,beforeDisabled) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%NEVER_FILTER%'",[filterAccess.connection.id])).rows[0].n,0) + } finally { filterCatalogFixtures = false; filterCatalogMalformed = false; countCatalogCalls = false; await filterHttp.close(); await pool.query("DELETE FROM servers WHERE id IN(43,44)") } + console.log("✓ BGP filters: shared REST/MCP live/cache selection, strict native identity and relative order, unknown/disabled rows, no observer cache/revision writes, scoped fallback, hidden cache/keys, disabled no-poll and malformed GET rejection") + const recursiveAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped recursive catalogs", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["routing.recursive.catalog.read", "routing.recursive.gateways.catalog.read", "routing.recursive.routes.read", "routing.recursive.gateways.read"], allowGlobal: false } }), actor) + const recursivePrincipal = await authenticateMcpToken(recursiveAccess.token) + const recursiveFixture = { ".id": "*rec1", static: "true", "dst-address": "192.0.2.0/24", gateway: "192.0.2.1%fixture-gre", distance: "0", scope: "0", "target-scope": "0", "routing-table": "main", comment: "MikrotikManager:recursive fixture" } + recursiveCatalogRows = [recursiveFixture, { ...recursiveFixture, ".id": "*rec2", gateway: "192.0.2.2%fixture-gre", disabled: "true", "check-gateway": "ping", active: "false" }] + recursiveCatalogFixtures = true; countCatalogCalls = true; catalogCalls = 0 + await pool.query("INSERT INTO recursive_routes(server_id,sort_order,dst_address,gateway,distance,scope,target_scope,routing_table,check_gateway,country,comment,disabled) VALUES(1,0,'192.0.2.0/24','192.0.2.1%fixture-gre',0,0,0,'main','none','Fixture country','fixture',false)") + const recursiveBeforeObservation = (await pool.query("SELECT * FROM recursive_routes WHERE server_id=1 ORDER BY id")).rows + const recursiveRevisionsBefore = (await pool.query("SELECT * FROM config_revisions WHERE server_id=1 AND section='recursive-routes' ORDER BY id")).rows + const recursiveCatalog = await invokeMcpTool(recursivePrincipal, "mm_recursive_routes_catalog_get", { serverId: 1, limit: 1 }) as { routes: { distance: number; scope: number; disabled: boolean; checkGateway: string; country: string }[]; total: number; nextOffset: number; live: boolean; stale: boolean } + assert.equal(recursiveCatalog.total, 2); assert.equal(recursiveCatalog.nextOffset, 1); assert.equal(recursiveCatalog.routes.length, 1) + assert.equal(recursiveCatalog.live, true); assert.equal(recursiveCatalog.stale, false) + assert.deepEqual(recursiveCatalog.routes[0], { id: "*rec1", dstAddress: "192.0.2.0/24", gateway: "192.0.2.1%fixture-gre", distance: 0, scope: 0, targetScope: 0, routingTable: "main", checkGateway: "none", country: "Fixture country", comment: "fixture", disabled: false }) + assert.equal(catalogCalls, 3) + await assert.rejects(invokeMcpTool(recursivePrincipal, "mm_recursive_routes_catalog_get", { serverId: 2 })); assert.equal(catalogCalls, 3) + const recursiveBusinessRead = await invokeMcpTool(recursivePrincipal, "mm_recursive_routes_get", { serverId: 1, limit: 1 }) as { routes: { id: string }[]; total: number; live: boolean; forwardingVerified: boolean } + assert.equal(recursiveBusinessRead.routes[0].id, String(recursiveBeforeObservation[0].id)); assert.equal(recursiveBusinessRead.total, 2); assert.equal(recursiveBusinessRead.live, true); assert.equal(recursiveBusinessRead.forwardingVerified, false) + for (const name of ["mm_recursive_routes_catalog_get", "mm_recursive_routes_get"]) { + const lastPage = await invokeMcpTool(recursivePrincipal, name, { serverId: 1, offset: 1, limit: 2 }) as { routes: unknown[]; total: number; nextOffset: number | null; incomplete: boolean; stale: boolean } + assert.equal(lastPage.total, 2); assert.equal(lastPage.routes.length, 1) + assert.equal(lastPage.nextOffset, null); assert.equal(lastPage.incomplete, true); assert.equal(lastPage.stale, false) + } + recursivePropertyFailure = true + await assert.rejects(invokeMcpTool(recursivePrincipal, "mm_recursive_routes_catalog_get", { serverId: 1 })) + recursivePropertyFailure = false + recursiveCatalogRows = [{ ...recursiveFixture, blackhole: "true" }] + await assert.rejects(invokeMcpTool(recursivePrincipal, "mm_recursive_routes_catalog_get", { serverId: 1 })) + const recursiveFallback = await invokeMcpTool(recursivePrincipal, "mm_recursive_routes_get", { serverId: 1 }) as { routes: { id: string }[]; live: boolean; stale: boolean; incomplete: boolean } + assert.equal(recursiveFallback.live, false); assert.equal(recursiveFallback.stale, true); assert.equal(recursiveFallback.incomplete, true); assert.equal(recursiveFallback.routes[0].id, String(recursiveBeforeObservation[0].id)) + assert.deepEqual((await pool.query("SELECT * FROM config_revisions WHERE server_id=1 AND section='recursive-routes' ORDER BY id")).rows, recursiveRevisionsBefore) + assert.deepEqual((await pool.query("SELECT * FROM recursive_routes WHERE server_id=1 ORDER BY id")).rows, recursiveBeforeObservation) + recursiveCatalogRows = [{ ".id": "*gw1", gateway: "192.0.2.1%fixture-gre" }, { ".id": "*gw2", gateway: "192.0.2.2%fixture-gre", active: "false" }, { ".id": "*gw3", gateway: "192.0.2.3%fixture-gre", active: "true" }] + const gatewayCatalog = await invokeMcpTool(recursivePrincipal, "mm_recursive_gateways_catalog_get", { serverId: 1, limit: 2 }) as { gateways: { status: string }[]; total: number; nextOffset: number } + assert.deepEqual(gatewayCatalog.gateways.map(row => row.status), ["unknown", "inactive"]); assert.equal(gatewayCatalog.total, 3); assert.equal(gatewayCatalog.nextOffset, 2) + const gatewayLast = await invokeMcpTool(recursivePrincipal, "mm_recursive_gateways_catalog_get", { serverId: 1, offset: 2, limit: 3 }) as { gateways: { status: string }[]; nextOffset: null; incomplete: boolean } + assert.deepEqual(gatewayLast.gateways.map(row => row.status), ["active"]) + assert.equal(gatewayLast.nextOffset, null); assert.equal(gatewayLast.incomplete, true) + const legacyGateways = await invokeMcpTool(recursivePrincipal, "mm_recursive_gateways_get", { serverId: 1 }) as { gateways: { status: string }[]; forwardingVerified: boolean } + assert.deepEqual(legacyGateways.gateways.map(row => row.status), ["unknown", "down", "up"]); assert.equal(legacyGateways.forwardingVerified, false) + const legacyGatewayLast = await invokeMcpTool(recursivePrincipal, "mm_recursive_gateways_get", { serverId: 1, offset: 2, limit: 3 }) as { gateways: { status: string }[]; nextOffset: null; incomplete: boolean } + assert.deepEqual(legacyGatewayLast.gateways.map(row => row.status), ["up"]) + assert.equal(legacyGatewayLast.nextOffset, null); assert.equal(legacyGatewayLast.incomplete, true) + const beforeDeniedGateway = catalogCalls + await assert.rejects(invokeMcpTool(recursivePrincipal, "mm_recursive_gateways_catalog_get", { serverId: 2 })); assert.equal(catalogCalls, beforeDeniedGateway) + for (const name of ["mm_recursive_routes_get", "mm_recursive_gateways_get"]) await assert.rejects(invokeMcpTool(recursivePrincipal, name, { serverId: 2 })) + assert.equal(catalogCalls, beforeDeniedGateway) + healthUnavailable = true + await assert.rejects(invokeMcpTool(recursivePrincipal, "mm_recursive_gateways_catalog_get", { serverId: 1 })) + healthUnavailable = false; recursiveCatalogRows = [] + assert.equal((await invokeMcpTool(recursivePrincipal, "mm_recursive_routes_catalog_get", { serverId: 1 }) as { total: number }).total, 0) + assert.deepEqual((await pool.query("SELECT * FROM recursive_routes WHERE server_id=1 ORDER BY id")).rows, recursiveBeforeObservation) + recursiveCatalogFixtures = false; countCatalogCalls = false + const { storeRecursiveRoutes } = await import("../../services/recursive-route-store.js") + const { readCachedRecursiveRoutes, applyRecursiveToServer } = await import("../../routes/recursive-routes.js") + const cachedRecursive = await readCachedRecursiveRoutes(1), recursiveId = cachedRecursive[0].id + await storeRecursiveRoutes(1, cachedRecursive, "observed") + assert.deepEqual((await pool.query("SELECT * FROM recursive_routes WHERE server_id=1 ORDER BY id")).rows, recursiveBeforeObservation) + const binding = (await pool.query("INSERT INTO filter_rules(server_id,community,gateway_tunnel_id) VALUES(1,'65000:997',$1) RETURNING id", ["rec:" + recursiveId])).rows[0].id + try { + const changed = [{ ...cachedRecursive[0], id: "*RECREATED", gateway: "192.0.2.99%fixture-gre", distance: 7, country: "" }] + assert.equal((await storeRecursiveRoutes(1, changed, "observed"))[0].id, recursiveId) + assert.equal((await readCachedRecursiveRoutes(1))[0].country, "Fixture country") + for (const rows of [[], [{ ...cachedRecursive[0], dstAddress: "198.51.100.0/24" }], [{ ...cachedRecursive[0], routingTable: "other" }]]) { + const beforeWrites = writes + // Invalid metadata is rejected before constructing the RouterOS client. + await assert.rejects(applyRecursiveToServer({ id: 1 } as Parameters[0], rows, "apply"), /BGP/) + assert.equal(writes, beforeWrites) + } + const beforeForeign = (await pool.query("SELECT * FROM recursive_routes WHERE server_id=1")).rows + await assert.rejects(storeRecursiveRoutes(2, [{ ...cachedRecursive[0] }], "apply"), /сервере/) + assert.deepEqual((await pool.query("SELECT * FROM recursive_routes WHERE server_id=1")).rows, beforeForeign) + assert.equal((await pool.query("SELECT gateway_tunnel_id FROM filter_rules WHERE id=$1", [binding])).rows[0].gateway_tunnel_id, "rec:" + recursiveId) + await storeRecursiveRoutes(1, [{ ...cachedRecursive[0], country: "" }], "apply") + assert.equal((await readCachedRecursiveRoutes(1))[0].country, "") + } finally { + await pool.query("DELETE FROM filter_rules WHERE id=$1", [binding]) + await pool.query("DELETE FROM recursive_routes WHERE server_id=1") + } + await pool.query("DELETE FROM config_revisions WHERE server_id=1 AND section='recursive-routes'") + console.log("✓ Recursive shared readers: observer has no cache/revision writes, scoped stale fallback and unknown gateway state; SQL stable IDs, no-op timestamps, country ownership, rec bindings and pre-native referenced-prefix rejection") + const { fingerprintPayload: revisionFingerprint } = await import("../../services/config-revisions.js") + const recursiveRevisionIds: string[] = [] + const addRecursiveRevision = async (payload: unknown, sid = 1, section = "recursive-routes") => { + const id = randomUUID(); recursiveRevisionIds.push(id) + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,$2,$3,'apply',$4,$5::jsonb)", [id, sid, section, revisionFingerprint(payload), JSON.stringify(payload)]) + return id + } + const restorePayload = [{ id: "historical-id", dstAddress: "192.0.2.0/24", gateway: "192.0.2.2", distance: 0, scope: 0, targetScope: 0, routingTable: "main", checkGateway: "none", country: "fixture-country", comment: "restore-fixture", disabled: false }] + const restoreRevisionId = await addRecursiveRevision(restorePayload) + const recursiveRestoreAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Recursive revision restore", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, concurrentReads: 4, mutationsPerHour: 50, serversPerPlan: 2, objectsPerPlan: 100 }, scope: { serverIds: [1], operationIds: ["routing.recursive.revision.restore"], allowGlobal: false } }), actor) + const recursiveRestorePrincipal = await authenticateMcpToken(recursiveRestoreAccess.token) + const restoreRevisionArgs = { serverId: 1, revisionId: restoreRevisionId } + const foreignRecursive = { ".id": "*foreign-recursive", "dst-address": "0.0.0.0/0", gateway: "192.0.2.254", distance: "1", static: "true", comment: "Keep foreign default" } + recursiveCatalogRows = [{ ".id": "*old-recursive", "dst-address": "192.0.2.0/24", gateway: "192.0.2.1", distance: "1", scope: "30", "target-scope": "10", "routing-table": "main", static: "true", comment: "MikrotikManager:recursive old" }, { ...foreignRecursive }] + recursiveCatalogFixtures = true; recursiveMutations = true; countCatalogCalls = true; catalogCalls = 0 + for (const invalidRevision of [await addRecursiveRevision(restorePayload, 2), await addRecursiveRevision(restorePayload, 1, "gre"), await addRecursiveRevision([{}]), await addRecursiveRevision(Array.from({ length: 51 }, (_, index) => ({ ...restorePayload[0], id: String(index) })))]) { + await assert.rejects(invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", { ...restoreRevisionArgs, revisionId: invalidRevision })) + } + assert.equal(catalogCalls, 0) + await pool.query("UPDATE config_revisions SET fingerprint='corrupt' WHERE id=$1", [restoreRevisionId]) + await assert.rejects(invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs)); assert.equal(catalogCalls, 0) + await pool.query("UPDATE config_revisions SET fingerprint=$2 WHERE id=$1", [restoreRevisionId, revisionFingerprint(restorePayload)]) + await assert.rejects(invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", { ...restoreRevisionArgs, serverId: 2 })); assert.equal(catalogCalls, 0) + const tableRestoreProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [tableRestoreProtection, JSON.stringify({ kind: "routing_table", id: "*main", serverId: 1, protection: "deny" })]) + await assert.rejects(invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs)); assert.equal(recursiveMutationCount, 0) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [tableRestoreProtection]) + const lowBudgetRestore = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Recursive batch budget", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, concurrentReads: 4, mutationsPerHour: 2, serversPerPlan: 2, objectsPerPlan: 100 }, scope: { serverIds: [1], operationIds: ["routing.recursive.revision.restore"], allowGlobal: false } }), actor) + const lowBudgetPrincipal = await authenticateMcpToken(lowBudgetRestore.token) + await assert.rejects(invokeMcpTool(lowBudgetPrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs), error => error instanceof Error && "code" in error && error.code === "BUDGET_EXCEEDED") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [lowBudgetRestore.connection.id])).rows[0].n, 0) + const exactBudgetRestore = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Recursive exact budget", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { mutationsPerHour: 3 }, scope: { serverIds: [1], operationIds: ["routing.recursive.revision.restore"], allowGlobal: false } }), actor) + const exactBudgetPrincipal = await authenticateMcpToken(exactBudgetRestore.token) + const lowBudgetPlan = await invokeMcpTool(exactBudgetPrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs) as { planId: string; changes: { mutationCost: number } } + assert.equal(lowBudgetPlan.changes.mutationCost, 3) + await approvePlan(lowBudgetPlan.planId, actor, true) + // Another accepted operation consumed part of the budget after preview. + await pool.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'mutations',date_trunc('hour',now()),1)", [exactBudgetRestore.connection.id]) + await assert.rejects(applyPlan(exactBudgetPrincipal, lowBudgetPlan.planId, "batch-budget-denied"), error => error instanceof Error && "code" in error && error.code === "BUDGET_EXCEEDED") + assert.equal(recursiveMutationCount, 0) + await pool.query("INSERT INTO recursive_routes(server_id,sort_order,dst_address,gateway,distance,scope,target_scope,routing_table,check_gateway,country,comment,disabled) VALUES(1,0,'192.0.2.0/24','192.0.2.1',1,30,10,'main','none','','old',false)") + const boundRecursiveId = (await pool.query("SELECT id FROM recursive_routes WHERE server_id=1 LIMIT 1")).rows[0].id + const bindRecursiveFilter = async () => { + const result = await pool.query("INSERT INTO filter_rules(server_id,community,gateway_tunnel_id) VALUES(1,'65000:777',$1) RETURNING id", ["rec:" + boundRecursiveId]) + return result.rows[0].id + } + const boundFilterId = await bindRecursiveFilter() + await assert.rejects(invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs)) + await pool.query("DELETE FROM filter_rules WHERE id=$1", [boundFilterId]) + const lateBindingPlan = await invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs) as { planId: string } + await approvePlan(lateBindingPlan.planId, actor, true) + const lateFilterId = await bindRecursiveFilter() + const waitRecursiveOperation = async (id: string) => { + const deadline = Date.now() + 30000 + while (Date.now() < deadline) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state + if (!["running", "pending"].includes(state)) return state + await delay(30) + } + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0]?.state + throw Error(`MCP fixture operation did not finish within 30 seconds (state=${state})`) + } + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveRestorePrincipal, lateBindingPlan.planId, randomUUID())).id), "rejected") + assert.equal(recursiveMutationCount, 0) + await pool.query("DELETE FROM filter_rules WHERE id=$1", [lateFilterId]) + const staleRevisionPlan = await invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs) as { planId: string } + await approvePlan(staleRevisionPlan.planId, actor, true) + const changedPayload = [{ ...restorePayload[0], distance: 20 }] + await pool.query("UPDATE config_revisions SET payload=$2::jsonb,fingerprint=$3 WHERE id=$1", [restoreRevisionId, JSON.stringify(changedPayload), revisionFingerprint(changedPayload)]) + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveRestorePrincipal, staleRevisionPlan.planId, randomUUID())).id), "rejected") + assert.equal(recursiveMutationCount, 0) + await pool.query("UPDATE config_revisions SET payload=$2::jsonb,fingerprint=$3 WHERE id=$1", [restoreRevisionId, JSON.stringify(restorePayload), revisionFingerprint(restorePayload)]) + const restoreRevisionPlan = await invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs) as { planId: string } + await approvePlan(restoreRevisionPlan.planId, actor, true) + const budgetBeforeRestore = (await pool.query("SELECT used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations' AND bucket=date_trunc('hour',now())", [recursiveRestoreAccess.connection.id])).rows[0]?.used ?? 0 + const restoredRevisionOperation = await applyPlan(recursiveRestorePrincipal, restoreRevisionPlan.planId, "restore-recursive-once") + assert.equal(await waitRecursiveOperation(restoredRevisionOperation.id), "applied"); assert.equal(recursiveMutationCount, 2) + assert.deepEqual(recursiveCatalogRows.find(row => row[".id"] === "*foreign-recursive"), foreignRecursive) + assert.equal((await pool.query("SELECT distance,country FROM recursive_routes WHERE server_id=1")).rows[0].distance, 0) + assert.equal((await pool.query("SELECT distance,country FROM recursive_routes WHERE server_id=1")).rows[0].country, "fixture-country") + assert.equal((await pool.query("SELECT used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations' AND bucket=date_trunc('hour',now())", [recursiveRestoreAccess.connection.id])).rows[0].used - budgetBeforeRestore, 3) + await applyPlan(recursiveRestorePrincipal, restoreRevisionPlan.planId, "restore-recursive-once"); assert.equal(recursiveMutationCount, 2) + recursiveIgnorePayload = true + const mismatchedRestore = await invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs) as { planId: string } + await approvePlan(mismatchedRestore.planId, actor, true) + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveRestorePrincipal, mismatchedRestore.planId, randomUUID())).id), "partial") + recursiveIgnorePayload = false + await pool.query("UPDATE recursive_routes SET country='fixture-before-restore' WHERE server_id=1") + await pool.query("CREATE FUNCTION mcp_fixture_wrong_recursive_cache() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN NEW.country := 'unexpected-cache-value'; RETURN NEW; END $$") + await pool.query("CREATE TRIGGER mcp_fixture_wrong_recursive_cache BEFORE INSERT OR UPDATE ON recursive_routes FOR EACH ROW EXECUTE FUNCTION mcp_fixture_wrong_recursive_cache()") + try { + const badCacheRestore = await invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs) as { planId: string } + await approvePlan(badCacheRestore.planId, actor, true) + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveRestorePrincipal, badCacheRestore.planId, randomUUID())).id), "partial") + assert.equal((await pool.query("SELECT country FROM recursive_routes WHERE server_id=1")).rows[0].country, "unexpected-cache-value") + } finally { + await pool.query("DROP TRIGGER mcp_fixture_wrong_recursive_cache ON recursive_routes") + await pool.query("DROP FUNCTION mcp_fixture_wrong_recursive_cache()") + } + const revokedBatch = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Revoke inside recursive batch", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["routing.recursive.revision.restore"], allowGlobal: false } }), actor) + const revokedBatchPrincipal = await authenticateMcpToken(revokedBatch.token) + const revokeInsidePlan = await invokeMcpTool(revokedBatchPrincipal, "mm_recursive_revision_restore_plan", restoreRevisionArgs) as { planId: string } + await approvePlan(revokeInsidePlan.planId, actor, true) + const beforeRevokedBatch = recursiveMutationCount + recursivePauseConnection = revokedBatch.connection.id + const revokedBatchOperation = await applyPlan(revokedBatchPrincipal, revokeInsidePlan.planId, randomUUID()) + assert.equal(await waitRecursiveOperation(revokedBatchOperation.id), "unknown") + assert.equal(recursiveMutationCount - beforeRevokedBatch, 1, "Revocation must stop DELETE inside the same service step after the first acknowledged PUT") + assert.equal(recursiveCatalogRows.filter(row => String(row.comment).startsWith("MikrotikManager:recursive")).length, 2) + await assert.rejects(applyPlan(revokedBatchPrincipal, revokeInsidePlan.planId, randomUUID())) + assert.equal(recursiveMutationCount - beforeRevokedBatch, 1) + const emptyRecursiveRevision = await addRecursiveRevision([]) + const clearRecursivePlan = await invokeMcpTool(recursiveRestorePrincipal, "mm_recursive_revision_restore_plan", { ...restoreRevisionArgs, revisionId: emptyRecursiveRevision }) as { planId: string } + await approvePlan(clearRecursivePlan.planId, actor, true) + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveRestorePrincipal, clearRecursivePlan.planId, randomUUID())).id), "applied") + assert.deepEqual(recursiveCatalogRows, [foreignRecursive]); assert.equal((await pool.query("SELECT count(*)::int AS n FROM recursive_routes WHERE server_id=1")).rows[0].n, 0) + const recursiveWriteAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Recursive full-set editor", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], limits: { callsPerMinute: 120, concurrentReads: 4, mutationsPerHour: 50, serversPerPlan: 2, objectsPerPlan: 100 }, scope: { serverIds: [1], operationIds: ["routing.recursive.routes.replace"], allowGlobal: false } }), actor) + const recursiveWritePrincipal = await authenticateMcpToken(recursiveWriteAccess.token) + const directRecursive = { ...restorePayload[0], id: "", country: "direct-country", disabled: true } + for (const invalid of [{ serverId: 2, routes: [] }, { serverId: 1 }, { serverId: 1, routes: [{ ...directRecursive, distance: -1 }] }, { serverId: 1, routes: Array.from({ length: 51 }, () => directRecursive) }]) await assert.rejects(invokeMcpTool(recursiveWritePrincipal, "mm_recursive_routes_replace_plan", invalid)) + const directCreate = await invokeMcpTool(recursiveWritePrincipal, "mm_recursive_routes_replace_plan", { serverId: 1, routes: [directRecursive] }) as { planId: string; changes: { mutationCost: number } } + assert.equal(directCreate.changes.mutationCost, 2) + await assert.rejects(applyPlan(recursiveWritePrincipal, directCreate.planId, randomUUID())) + await approvePlan(directCreate.planId, actor, true) + const directCreated = await applyPlan(recursiveWritePrincipal, directCreate.planId, "recursive-direct-once") + assert.equal(await waitRecursiveOperation(directCreated.id), "applied") + const directCreateCount = recursiveMutationCount + await applyPlan(recursiveWritePrincipal, directCreate.planId, "recursive-direct-once"); assert.equal(recursiveMutationCount, directCreateCount) + const directCached = (await readCachedRecursiveRoutes(1))[0] + const directBinding = (await pool.query("INSERT INTO filter_rules(server_id,community,gateway_tunnel_id) VALUES(1,'65000:998',$1) RETURNING id", ["rec:" + directCached.id])).rows[0].id + try { + for (const routes of [[], [{ ...directCached, dstAddress: "198.51.100.0/24" }]]) await assert.rejects(invokeMcpTool(recursiveWritePrincipal, "mm_recursive_routes_replace_plan", { serverId: 1, routes }), /BGP/) + const edited = { ...directCached, gateway: "192.0.2.3", distance: 2, country: "" } + const editPlan = await invokeMcpTool(recursiveWritePrincipal, "mm_recursive_routes_replace_plan", { serverId: 1, routes: [edited] }) as { planId: string; changes: { mutationCost: number } } + assert.equal(editPlan.changes.mutationCost, 3) + await approvePlan(editPlan.planId, actor, true) + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveWritePrincipal, editPlan.planId, randomUUID())).id), "applied") + assert.equal((await readCachedRecursiveRoutes(1))[0].id, directCached.id); assert.equal((await readCachedRecursiveRoutes(1))[0].country, "") + assert.equal((await pool.query("SELECT gateway_tunnel_id FROM filter_rules WHERE id=$1", [directBinding])).rows[0].gateway_tunnel_id, "rec:" + directCached.id) + const staleMetadataPlan = await invokeMcpTool(recursiveWritePrincipal, "mm_recursive_routes_replace_plan", { serverId: 1, routes: [edited] }) as { planId: string } + await approvePlan(staleMetadataPlan.planId, actor, true) + await pool.query("UPDATE recursive_routes SET country='late-metadata' WHERE id=$1", [directCached.id]) + const beforeStale = recursiveMutationCount + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveWritePrincipal, staleMetadataPlan.planId, randomUUID())).id), "rejected") + assert.equal(recursiveMutationCount, beforeStale) + } finally { await pool.query("DELETE FROM filter_rules WHERE id=$1", [directBinding]) } + const directClear = await invokeMcpTool(recursiveWritePrincipal, "mm_recursive_routes_replace_plan", { serverId: 1, routes: [], source: "copy" }) as { planId: string } + await approvePlan(directClear.planId, actor, true) + assert.equal(await waitRecursiveOperation((await applyPlan(recursiveWritePrincipal, directClear.planId, randomUUID())).id), "applied") + assert.deepEqual(recursiveCatalogRows, [foreignRecursive]); assert.deepEqual(await readCachedRecursiveRoutes(1), []) + console.log("✓ Recursive full-set editor: explicit human approval, complete batch cost, empty/create/edit/copy, stable BGP binding, country clear, sealed metadata drift, foreign route retention and no replay") + recursiveCatalogFixtures = false; recursiveMutations = false; countCatalogCalls = false + await pool.query("DELETE FROM config_revisions WHERE server_id=1 AND section='recursive-routes' OR id=ANY($1::text[])", [recursiveRevisionIds]) + console.log("✓ Recursive revision restore seals scoped history/full owned set, blocks dependent BGP filters and drift, reserves all batch writes, preserves foreign routes, verifies native/cache/revision and supports explicit empty without replay") + const backupJobActor: AuthUser = { ...actor, id: "backup-job-owner", permissions: ["mm:settings:admin", "mm:backups:read"] } + const backupJobAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped backup progress", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:backups:read"], scope: { serverIds: [1], operationIds: ["backups.jobs.read"], allowGlobal: false } }), backupJobActor) + const backupJobPrincipal = await authenticateMcpToken(backupJobAccess.token) + const { createBackupsRoutes } = await import("../../routes/backups.js"), { databaseBackgroundWork } = await import("../../services/database-background-work.js"), { readBackupJob } = await import("../../services/backup-job-store.js") + let releaseBackupJob!: () => void + const waitBackupJob = new Promise(resolve => { releaseBackupJob = resolve }) + const backupJobsHttp = FilterFastify(); backupJobsHttp.setValidatorCompiler(filterValidator); backupJobsHttp.setSerializerCompiler(filterSerializer) + await backupJobsHttp.register(createBackupsRoutes({ listServers: async () => [{ id: 1 }, { id: 2 }], appendEvent: async () => {}, create: async id => { + await waitBackupJob + if (id === "2") throw Error("NEVER_BACKUP_JOB_FAILURE") + return { id: randomUUID(), serverId: 1, serverName: "NEVER_BACKUP_JOB_SERVER", filename: "NEVER_BACKUP_JOB_FILE", sizeBytes: 0, createdAt: new Date().toISOString(), kind: "manual", storage: "both", s3Key: "NEVER_BACKUP_JOB_KEY", uploadError: "NEVER_BACKUP_JOB_UPLOAD", notes: "NEVER_BACKUP_JOB_NOTE" } + } }), { prefix: "/api" }) + let singleBackupJobId = "" + const backupWritesBefore = writes + try { + const started = await backupJobsHttp.inject({ method: "POST", url: "/api/backups/create", payload: { serverIds: [1], notes: "NEVER_BACKUP_JOB_NOTES" } }) + assert.equal(started.statusCode, 202, started.body); singleBackupJobId = started.json().jobId + const pendingJob = await invokeMcpTool(backupJobPrincipal, "mm_backup_job_get", { jobId: singleBackupJobId }) as { completed: number; terminal: boolean; ephemeral: boolean } + assert.equal(pendingJob.completed, 0); assert.equal(pendingJob.terminal, false); assert.equal(pendingJob.ephemeral, true) + releaseBackupJob(); await databaseBackgroundWork.drain() + const restJob = (await backupJobsHttp.inject({ method: "GET", url: "/api/backups/jobs/" + singleBackupJobId })).json() + const finishedJob = await invokeMcpTool(backupJobPrincipal, "mm_backup_job_get", { jobId: singleBackupJobId, limit: 1 }) as { status: string; completed: number; hasUploadErrors: boolean; created: { items: { sizeBytes: number }[] } } + assert.equal(finishedJob.status, restJob.status); assert.equal(finishedJob.completed, restJob.completed); assert.equal(finishedJob.hasUploadErrors, true); assert.equal(finishedJob.created.items[0].sizeBytes, 0) + assert.equal(JSON.stringify(finishedJob).includes("NEVER_BACKUP_JOB"), false) + const emptyPage = await invokeMcpTool(backupJobPrincipal, "mm_backup_job_get", { jobId: singleBackupJobId, offset: 1, limit: 1 }) as { created: { items: unknown[]; total: number } } + assert.deepEqual(emptyPage.created.items, []); assert.equal(emptyPage.created.total, 1) + const mixed = await backupJobsHttp.inject({ method: "POST", url: "/api/backups/create", payload: { serverIds: [1, 2] } }) + assert.equal(mixed.statusCode, 202); await databaseBackgroundWork.drain() + await assert.rejects(invokeMcpTool(backupJobPrincipal, "mm_backup_job_get", { jobId: mixed.json().jobId }), /не найдена/) + await assert.rejects(invokeMcpTool(backupJobPrincipal, "mm_backup_job_get", { jobId: "missing-job" }), /не найдена/) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%NEVER_BACKUP_JOB%'", [backupJobAccess.connection.id])).rows[0].n, 0) + assert.equal(writes, backupWritesBefore) + } finally { releaseBackupJob(); await databaseBackgroundWork.drain(); await backupJobsHttp.close() } + assert.equal(readBackupJob(singleBackupJobId), undefined) + console.log("✓ Backup jobs share the actual REST queue: scoped whole-job visibility, queued/running/done progress, zero/upload status, bounded pages, no note/path/error leakage in response/audit, no RouterOS requests and app-close invalidation") + const storageActor: AuthUser={...actor,id:"backup-storage-owner",permissions:["mm:settings:admin","mm:backups:admin"]} + const storageServerIds=(await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row=>Number(row.id)) + const storagePolicy={name:"Backup storage settings",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:backups:write"],limits:{callsPerMinute:120,concurrentReads:4,mutationsPerHour:50,serversPerPlan:10,objectsPerPlan:100},scope:{serverIds:storageServerIds,operationIds:["backups.storage.update"],allowGlobal:true}} + const storageAccess=await createMcpConnection(mcpConnectionCreateSchema.parse(storagePolicy),storageActor),storagePrincipal=await authenticateMcpToken(storageAccess.token) + const oldStorageRows=(await pool.query("SELECT * FROM backup_storage_settings WHERE id=1")).rows + const storageSecretIds:string[]=[],storageEntryId=randomUUID(),storageProtection=randomUUID() + const {backupStorageSecretPurpose}=await import("./backup-storage-actions.js"),{withBackupStorageOperation}=await import("../../services/backup-storage-operation.js") + const storageSettings={provider:"s3" as const,s3Endpoint:"https://s3.example.test",s3Region:"us-east-1",s3Bucket:"backups",s3Prefix:"mcp-fixture",s3AccessKeyId:"fixture-access",s3ForcePathStyle:true,keepLocalCopy:true} + const storageSecretValue="NEVER_STORAGE_SECRET_INITIAL" + const addStorageSecret=async(purpose=backupStorageSecretPurpose(storageSettings),owner=storageActor.id,serverId:number|null=null,value=storageSecretValue)=>{ + const id=randomUUID();storageSecretIds.push(id) + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)",[id,purpose,serverId,await encryptSecret(value),owner]) + return id + } + const storageWritesBefore=writes + let releaseStorage:()=>void=()=>{},storagePending:Promise|undefined + try { + const secretRef=await addStorageSecret(),settingsArgs={settings:storageSettings,secretRef} + const partialAccess=await createMcpConnection(mcpConnectionCreateSchema.parse({...storagePolicy,name:"Partial storage scope",scope:{...storagePolicy.scope,serverIds:[1]}}),storageActor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(partialAccess.token),"mm_backup_storage_update_plan",settingsArgs),/вне разрешённой области/) + for(const invalid of [{},{settings:{s3SecretAccessKey:"NEVER_STORAGE_LITERAL"}},{settings:{s3Endpoint:"https://user:password@s3.example.test"}},{settings:storageSettings,secretRef:await addStorageSecret("wrong-purpose")},{settings:storageSettings,secretRef:await addStorageSecret(undefined,"other-owner")},{settings:storageSettings,secretRef:await addStorageSecret(undefined,storageActor.id,1)}])await assert.rejects(invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",invalid)) + const first=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",settingsArgs) as {planId:string} + assert.equal(JSON.stringify(first).includes(storageSecretValue),false) + await assert.rejects(applyPlan(storagePrincipal,first.planId,randomUUID())) + assert.deepEqual((await pool.query("SELECT * FROM backup_storage_settings WHERE id=1")).rows,oldStorageRows) + await approvePlan(first.planId,storageActor,true) + const applied=await applyPlan(storagePrincipal,first.planId,"storage-config-once") + assert.equal(await waitRecursiveOperation(applied.id),"applied") + const saved=(await pool.query("SELECT * FROM backup_storage_settings WHERE id=1")).rows + assert.equal(saved[0].s3_secret_access_key,storageSecretValue);assert.equal(saved[0].s3_endpoint,storageSettings.s3Endpoint);assert.equal(saved[0].last_test_at,null) + assert.equal((await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[applied.id])).rows[0].result.storageTestVerified,false) + await applyPlan(storagePrincipal,first.planId,"storage-config-once") + assert.deepEqual((await pool.query("SELECT * FROM backup_storage_settings WHERE id=1")).rows,saved) + const newStorageEndpoint="https://other.example.test" + await assert.rejects(invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{s3Endpoint:newStorageEndpoint}}),/новое назначение/) + const moveRef=await addStorageSecret(backupStorageSecretPurpose({...storageSettings,s3Endpoint:newStorageEndpoint}),storageActor.id,null,"NEVER_STORAGE_MOVED") + const movePlan=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{s3Endpoint:newStorageEndpoint},secretRef:moveRef}) as {planId:string} + await approvePlan(movePlan.planId,storageActor,true) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,movePlan.planId,randomUUID())).id),"applied") + assert.equal((await pool.query("SELECT s3_endpoint,s3_secret_access_key FROM backup_storage_settings WHERE id=1")).rows[0].s3_endpoint,newStorageEndpoint) + const moveBack=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{s3Endpoint:storageSettings.s3Endpoint},secretRef}) as {planId:string} + await approvePlan(moveBack.planId,storageActor,true) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,moveBack.planId,randomUUID())).id),"applied") + const dormant=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{provider:"local",s3Bucket:""}}) as {planId:string} + await approvePlan(dormant.planId,storageActor,true) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,dormant.planId,randomUUID())).id),"applied") + assert.equal((await pool.query("SELECT s3_bucket FROM backup_storage_settings WHERE id=1")).rows[0].s3_bucket,"") + await assert.rejects(invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:storageSettings}),/новое назначение/) + const enableAgain=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",settingsArgs) as {planId:string} + await approvePlan(enableAgain.planId,storageActor,true) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,enableAgain.planId,randomUUID())).id),"applied") + const beforeArchive=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:false}}) as {planId:string} + await approvePlan(beforeArchive.planId,storageActor,true) + await pool.query("INSERT INTO backup_entries(id,server_id,server_name,filename,size_bytes,kind,storage,s3_key,created_at) VALUES($1,1,'fixture',$2,0,'manual','s3','mcp-fixture/object.rsc',now())",[storageEntryId,storageEntryId+".rsc"]) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,beforeArchive.planId,randomUUID())).id),"rejected") + assert.equal((await pool.query("SELECT keep_local_copy FROM backup_storage_settings WHERE id=1")).rows[0].keep_local_copy,true) + for(const settings of [{provider:"local"},{s3Endpoint:"https://other.example.test"},{s3Bucket:"other"},{s3Region:"other"},{s3ForcePathStyle:false}])await assert.rejects(invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings}),/архивы/) + const prefixPlan=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{s3Prefix:"mcp-fixture-new",keepLocalCopy:false}}) as {planId:string} + await approvePlan(prefixPlan.planId,storageActor,true) + await pool.query("UPDATE backup_storage_settings SET last_test_at=now(),last_test_error='NEVER_STORAGE_OBSERVER_ERROR' WHERE id=1") + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,prefixPlan.planId,randomUUID())).id),"applied") + const afterPrefix=(await pool.query("SELECT * FROM backup_storage_settings WHERE id=1")).rows[0] + assert.equal(afterPrefix.keep_local_copy,false);assert.equal(afterPrefix.s3_prefix,"mcp-fixture-new");assert.equal(afterPrefix.s3_secret_access_key,storageSecretValue);assert.equal(afterPrefix.last_test_at,null);assert.equal(afterPrefix.last_test_error,null) + const staleKey=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:true}}) as {planId:string} + await approvePlan(staleKey.planId,storageActor,true) + await pool.query("UPDATE backup_storage_settings SET s3_secret_access_key='NEVER_STORAGE_EXTERNALLY_CHANGED' WHERE id=1") + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,staleKey.planId,randomUUID())).id),"rejected") + await pool.query("UPDATE backup_storage_settings SET s3_secret_access_key=$1 WHERE id=1",[storageSecretValue]) + const rotationRef=await addStorageSecret(undefined,storageActor.id,null,"NEVER_STORAGE_ROTATION") + const rotationPlan=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{secretRef:rotationRef}) as {planId:string} + await approvePlan(rotationPlan.planId,storageActor,true) + await pool.query("DELETE FROM mcp_secrets WHERE id=$1",[rotationRef]) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,rotationPlan.planId,randomUUID())).id),"rejected") + const validRotationRef=await addStorageSecret(undefined,storageActor.id,null,"NEVER_STORAGE_ROTATION_VALID") + const validRotation=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{secretRef:validRotationRef}) as {planId:string} + await approvePlan(validRotation.planId,storageActor,true) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,validRotation.planId,randomUUID())).id),"applied") + assert.equal((await pool.query("SELECT s3_secret_access_key FROM backup_storage_settings WHERE id=1")).rows[0].s3_secret_access_key,"NEVER_STORAGE_ROTATION_VALID") + const newServerPlan=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:true}}) as {planId:string} + await approvePlan(newServerPlan.planId,storageActor,true) + await pool.query("INSERT INTO servers(id,name,host,username,password) VALUES(49,'storage-catalog-fixture','127.0.0.1','admin','fixture')") + try{assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,newServerPlan.planId,randomUUID())).id),"rejected")}finally{await pool.query("DELETE FROM servers WHERE id=49")} + storagePending=withBackupStorageOperation(()=>new Promise(resolve=>{releaseStorage=resolve})) + await Promise.resolve() + await assert.rejects(invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:true}}),/занято/) + releaseStorage();await storagePending + const lateBusy=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:true}}) as {planId:string} + await approvePlan(lateBusy.planId,storageActor,true) + storagePending=withBackupStorageOperation(()=>new Promise(resolve=>{releaseStorage=resolve})) + await Promise.resolve() + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,lateBusy.planId,randomUUID())).id),"rejected") + releaseStorage();await storagePending + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'fixture')",[storageProtection,JSON.stringify({kind:"backup-storage",id:"1",protection:"deny"})]) + await assert.rejects(invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:true}})) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[storageProtection]) + await pool.query("CREATE FUNCTION mcp_fixture_ignore_storage() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN NEW.keep_local_copy := OLD.keep_local_copy; RETURN NEW; END $$") + await pool.query("CREATE TRIGGER mcp_fixture_ignore_storage BEFORE UPDATE ON backup_storage_settings FOR EACH ROW EXECUTE FUNCTION mcp_fixture_ignore_storage()") + try { + const ignored=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:true}}) as {planId:string} + await approvePlan(ignored.planId,storageActor,true) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,ignored.planId,randomUUID())).id),"partial") + assert.equal((await pool.query("SELECT keep_local_copy FROM backup_storage_settings WHERE id=1")).rows[0].keep_local_copy,false) + }finally{await pool.query("DROP TRIGGER mcp_fixture_ignore_storage ON backup_storage_settings");await pool.query("DROP FUNCTION mcp_fixture_ignore_storage()")} + await pool.query("CREATE FUNCTION mcp_fixture_change_storage_bucket() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN NEW.s3_bucket := 'unexpected-bucket'; RETURN NEW; END $$") + await pool.query("CREATE TRIGGER mcp_fixture_change_storage_bucket BEFORE UPDATE ON backup_storage_settings FOR EACH ROW EXECUTE FUNCTION mcp_fixture_change_storage_bucket()") + try{ + const changedUnspecified=await invokeMcpTool(storagePrincipal,"mm_backup_storage_update_plan",{settings:{keepLocalCopy:true}}) as {planId:string} + await approvePlan(changedUnspecified.planId,storageActor,true) + assert.equal(await waitRecursiveOperation((await applyPlan(storagePrincipal,changedUnspecified.planId,randomUUID())).id),"partial") + }finally{await pool.query("DROP TRIGGER mcp_fixture_change_storage_bucket ON backup_storage_settings");await pool.query("DROP FUNCTION mcp_fixture_change_storage_bucket()")} + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%NEVER_STORAGE%'",[storageAccess.connection.id])).rows[0].n,0) + assert.equal(writes,storageWritesBefore) + }finally{ + releaseStorage();await storagePending + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[storageProtection]) + await pool.query("DELETE FROM backup_entries WHERE id=$1",[storageEntryId]) + await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])",[storageSecretIds]) + await pool.query("DELETE FROM backup_storage_settings WHERE id=1") + if(oldStorageRows[0])await pool.query("INSERT INTO backup_storage_settings SELECT * FROM jsonb_populate_record(NULL::backup_storage_settings,$1::jsonb)",[JSON.stringify(oldStorageRows[0])]) + } + console.log("✓ Backup storage shares REST mutation: full global scope, human approval, private credential/secret-ref proof, destination/owner binding, remote archives, late catalog/credential/busy drift, preserved key, test invalidation, protection, partial verification and no replay or network dispatch") + const internetPathAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped Internet path history", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["routing.internet-path.latest.read"], allowGlobal: false } }), actor) + const internetPathPrincipal = await authenticateMcpToken(internetPathAccess.token) + const internetPathHttp = FilterFastify(); internetPathHttp.setValidatorCompiler(filterValidator); internetPathHttp.setSerializerCompiler(filterSerializer) + await internetPathHttp.register((await import("../../routes/internet-path.js")).default,{prefix:"/api"}) + const internetPathIds: number[]=[] + const pathSnapshotTime=new Date().toISOString() + const {ensurePartitionFor:ensurePathPartition}=await import("../../db/partitions.js") + for(const delta of [-3600000,0,3600000])await ensurePathPartition(pool,"internet_path_snapshots","week",new Date(Date.parse(pathSnapshotTime)+delta)) + const pathPayload={collectionComplete:true,sampledAt:pathSnapshotTime,internetTarget:"1.1.1.1", + servers:[{id:"1",name:"Visible home",type:"home-router",enabled:true,password:"NEVER_PATH_SECRET"},{id:"2",name:"NEVER_PATH_HIDDEN",enabled:"malformed"}], + greTunnels:[{id:"gre1",serverId:"1",name:"fixture-gre",enabled:true,status:"up",ipsec:{secret:"NEVER_PATH_SECRET"},comment:"NEVER_PATH_SECRET"},{id:"NEVER_PATH_HIDDEN",serverId:"2",mtu:"bad"}], + filtersRulesets:[{serverId:"1",rules:[{id:"r1",community:"65000:1",action:"route",gateway:"192.0.2.1",description:"NEVER_PATH_SECRET"},{id:"r2",community:"65000:2",action:"blackhole"}]},{serverId:"2",rules:"malformed"}], + speedProbes:[{id:"p1",srcServerId:"1",dstServerId:"1",lastTxAvgMbps:0,lastError:"NEVER_PATH_SECRET"},{id:"NEVER_PATH_HIDDEN",srcServerId:"1",dstServerId:"2",lastTxAvgMbps:"bad"}], + routeLookupByServerId:{"1":{gateway:null,routingMark:null},"2":"NEVER_PATH_HIDDEN"}, + wanRuntimeByHomeId:{"1":{defaultGateway:null,defaultInterface:null,uplinks:[{id:"wan",iface:"ether1",isDefault:false,password:"NEVER_PATH_SECRET"} ]},"2":"malformed"}, + } + countCatalogCalls=true;catalogCalls=0 + try { + assert.equal((await pool.query("SELECT count(*)::int AS n FROM internet_path_snapshots")).rows[0].n,0) + assert.equal((await invokeMcpTool(internetPathPrincipal,"mm_internet_path_latest_get",{}) as {snapshot:unknown}).snapshot,null) + const id=(await pool.query("INSERT INTO internet_path_snapshots(sampled_at,payload_json) VALUES($1,$2::jsonb) RETURNING id",[pathSnapshotTime,JSON.stringify(pathPayload)])).rows[0].id + internetPathIds.push(id) + const observed=await invokeMcpTool(internetPathPrincipal,"mm_internet_path_latest_get",{limit:1}) as {snapshot:{servers:{items:{id:number}[]};greTunnels:{items:{mtu:null}[]};speedProbes:{items:{lastTxAvgMbps:number;lastRxAvgMbps:null}[];total:number};filtersRulesets:{items:{rules:{total:number;nextOffset:number}}[]};live:boolean;forwardingVerified:boolean;stale:boolean};incomplete:boolean} + assert.equal(observed.snapshot.servers.items.length,1);assert.equal(observed.snapshot.servers.items[0].id,1) + assert.equal(observed.snapshot.speedProbes.total,1);assert.equal(observed.snapshot.speedProbes.items[0].lastTxAvgMbps,0);assert.equal(observed.snapshot.speedProbes.items[0].lastRxAvgMbps,null) + assert.equal(observed.snapshot.greTunnels.items[0].mtu,null);assert.equal(observed.snapshot.filtersRulesets.items[0].rules.total,2);assert.equal(observed.snapshot.filtersRulesets.items[0].rules.nextOffset,1) + assert.equal(observed.snapshot.live,false);assert.equal(observed.snapshot.forwardingVerified,false);assert.equal(observed.snapshot.stale,false);assert.equal(observed.incomplete,true) + assert.equal(JSON.stringify(observed).includes("NEVER_PATH"),false) + const rest=await internetPathHttp.inject({method:"GET",url:"/api/internet-path/latest"}) + assert.equal(rest.statusCode,200);assert.equal(rest.json().sampledAt,pathSnapshotTime);assert.equal(rest.json().snapshot.servers.length,2) + await assert.rejects(invokeMcpTool(internetPathPrincipal,"mm_internet_path_latest_get",{serverId:2})) + const updatePath=async(payload:unknown,time=pathSnapshotTime)=>pool.query("UPDATE internet_path_snapshots SET payload_json=$2::jsonb,sampled_at=$3 WHERE id=$1",[id,JSON.stringify(payload),time]) + for(const bad of [{...pathPayload,greTunnels:[pathPayload.greTunnels[0],pathPayload.greTunnels[0]]},{...pathPayload,greTunnels:[{id:"unbound"}]},{...pathPayload,servers:{bad:true}},"legacy serialized payload",{...pathPayload,filtersRulesets:[{serverId:"1",rules:"bad"}]}]){ + await updatePath(bad);await assert.rejects(invokeMcpTool(internetPathPrincipal,"mm_internet_path_latest_get",{}),/не подтверждён/) + } + const pastTime=new Date(Date.now()-3600000).toISOString() + await updatePath({...pathPayload,sampledAt:pastTime},pastTime) + assert.equal((await invokeMcpTool(internetPathPrincipal,"mm_internet_path_latest_get",{}) as {snapshot:{stale:boolean}}).snapshot.stale,true) + const futureTime=new Date(Date.now()+3600000).toISOString() + await updatePath({...pathPayload,sampledAt:futureTime},futureTime) + const future=await invokeMcpTool(internetPathPrincipal,"mm_internet_path_latest_get",{}) as {snapshot:{clockSkew:boolean;stale:boolean}} + assert.equal(future.snapshot.clockSkew,true);assert.equal(future.snapshot.stale,true) + assert.equal(catalogCalls,0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%NEVER_PATH%'",[internetPathAccess.connection.id])).rows[0].n,0) + } finally {countCatalogCalls=false;await internetPathHttp.close();await pool.query("DELETE FROM internet_path_snapshots WHERE id=ANY($1::bigint[])",[internetPathIds])} + console.log("✓ Stored Internet path: shared latest REST source, SQL projection before parsing hidden/mixed records, nested pages, zero/unknown fields, omitted GRE keys/notes, stale/future/malformed source, scoped audit and no device requests; no current routing claim") + const deviceStateAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped device states", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:certificates:read"], scope: { serverIds: [1], operationIds: ["containers.state.read", "ros.container.list", "certificates.state.read"], allowGlobal: false } }), { ...actor, permissions: [...actor.permissions, "mm:certificates:admin"] }) + const deviceStatePrincipal = await authenticateMcpToken(deviceStateAccess.token) + deviceStateFixtures = true; countCatalogCalls = true; catalogCalls = 0 + const containerState = await invokeMcpTool(deviceStatePrincipal, "mm_containers_state_get", { limit: 1 }) as { servers: { serverId: number; availability: string; containers: { status: string; envs: { value: string }[] }[]; total: number; nextOffset: number; failures: unknown[] }[] } + assert.equal(containerState.servers.length, 1); assert.equal(containerState.servers[0].serverId, 1); assert.equal(containerState.servers[0].total, 2); assert.equal(containerState.servers[0].nextOffset, 1) + assert.equal(containerState.servers[0].availability, "available"); assert.equal(containerState.servers[0].containers[0].status, "running"); assert.equal(containerState.servers[0].failures.length, 0) + assert.equal(containerState.servers[0].containers[0].envs[0].value, "[redacted]") + assert.ok(!JSON.stringify(containerState).includes("never-return")); assert.equal(catalogCalls, 3) + await assert.rejects(invokeMcpTool(deviceStatePrincipal, "mm_containers_state_get", { serverId: 2 })); assert.equal(catalogCalls, 3) + const containerNext = await invokeMcpTool(deviceStatePrincipal, "mm_containers_state_get", { offset: 1, limit: 1 }) as typeof containerState + assert.equal(containerNext.servers[0].containers[0].status, "unknown") + const containerRaw = await invokeMcpTool(deviceStatePrincipal, "mm_container_list", { serverId: 1, limit: 1 }) as { items: Record[]; total: number; nextOffset: number } + assert.equal(containerRaw.items[0][".id"], "*c1"); assert.equal(containerRaw.total, 2); assert.equal(containerRaw.nextOffset, 1) + assert.ok(!JSON.stringify(containerRaw).includes("never-return")) + assert.ok(["cmd", "entrypoint", "env"].every(field => !(field in containerRaw.items[0]))) + containerStateFailure = "envs" + const partialContainers = await invokeMcpTool(deviceStatePrincipal, "mm_containers_state_get", {}) as { servers: { failures: { resource: string }[] }[]; incomplete: boolean } + assert.equal(partialContainers.servers[0].failures[0].resource, "envs"); assert.equal(partialContainers.incomplete, true) + containerStateFailure = "unsupported" + const unsupportedContainers = await invokeMcpTool(deviceStatePrincipal, "mm_containers_state_get", {}) as { servers: { availability: string; containers: unknown[] }[]; incomplete: boolean } + assert.equal(unsupportedContainers.servers[0].availability, "unsupported"); assert.equal(unsupportedContainers.servers[0].containers.length, 0); assert.equal(unsupportedContainers.incomplete, true) + containerStateFailure = "none"; catalogCalls = 0 + const certificateState = await invokeMcpTool(deviceStatePrincipal, "mm_certificates_state_get", { limit: 1 }) as { servers: { serverId: string; certificates: { status: string; validUntil: string | null }[]; total: number; nextOffset: number; error: string | null }[]; incomplete: boolean } + assert.equal(certificateState.servers.length, 1); assert.equal(certificateState.servers[0].serverId, "1"); assert.equal(certificateState.servers[0].total, 2); assert.equal(certificateState.servers[0].nextOffset, 1) + assert.equal(certificateState.servers[0].certificates[0].status, "valid"); assert.equal(certificateState.servers[0].certificates[0].validUntil, "2034-12-31T17:00:00.000Z"); assert.equal(certificateState.servers[0].error, null) + assert.ok(!JSON.stringify(certificateState).includes("never-return")); assert.equal(catalogCalls, 3) + await assert.rejects(invokeMcpTool(deviceStatePrincipal, "mm_certificates_state_get", { serverId: 2 })); assert.equal(catalogCalls, 3) + certificateClockKnown = false + const unknownCertificate = await invokeMcpTool(deviceStatePrincipal, "mm_certificates_state_get", {}) as typeof certificateState + assert.equal(unknownCertificate.servers[0].certificates[0].status, "unknown"); assert.equal(unknownCertificate.servers[0].certificates[0].validUntil, null) + certificateClockKnown = true; healthUnavailable = true + const failedCertificates = await invokeMcpTool(deviceStatePrincipal, "mm_certificates_state_get", {}) as typeof certificateState + assert.ok(failedCertificates.servers[0].error); assert.equal(failedCertificates.incomplete, true) + healthUnavailable = false + const beforeEmptyPage = catalogCalls + const emptyDevicePage = await invokeMcpTool(deviceStatePrincipal, "mm_containers_state_get", { serverOffset: 1 }) as { servers: unknown[]; incomplete: boolean } + assert.equal(emptyDevicePage.servers.length, 0); assert.equal(emptyDevicePage.incomplete, true); assert.equal(catalogCalls, beforeEmptyPage) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + assert.equal((await invokeMcpTool(deviceStatePrincipal, "mm_certificates_state_get", {}) as typeof certificateState).servers.length, 0) + assert.equal(catalogCalls, beforeEmptyPage) + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%never-return%'", [deviceStateAccess.connection.id])).rows[0].n, 0) + const { default: ContainersFastify } = await import("fastify") + const { validatorCompiler: containerValidator, serializerCompiler: containerSerializer } = await import("@fastify/type-provider-zod") + const containersHttp = ContainersFastify(); containersHttp.setValidatorCompiler(containerValidator); containersHttp.setSerializerCompiler(containerSerializer) + await containersHttp.register((await import("../../routes/containers.js")).default, { prefix: "/api" }) + await containersHttp.register((await import("../../routes/certificates.js")).default, { prefix: "/api" }) + const otherEnabled = (await pool.query("SELECT id FROM servers WHERE enabled AND id<>1")).rows.map(row => row.id) + await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::bigint[])", [otherEnabled]) + try { + const fullState = await invokeMcpTool(deviceStatePrincipal, "mm_containers_state_get", {}) as { servers: { containers: { id:string; rosId:string; status:string; startOnBoot:boolean|null; envs:{value:string}[] }[] }[] } + for (const url of ["/api/containers", "/api/servers/1/containers"]) { + const response = await containersHttp.inject({method:"GET",url}); assert.equal(response.statusCode,200,response.body) + const restRows = response.json().containers as typeof fullState.servers[0]["containers"] + assert.deepEqual(restRows.map(row=>({id:row.id,rosId:row.rosId,status:row.status,startOnBoot:row.startOnBoot})),fullState.servers[0].containers.map(row=>({id:row.id,rosId:row.rosId,status:row.status,startOnBoot:row.startOnBoot}))) + assert.equal(restRows[0].envs[0].value,"never-return-this-env"); assert.equal(fullState.servers[0].containers[0].envs[0].value,"[redacted]") + } + reverseContainerRows = true + assert.equal((await invokeMcpTool(deviceStatePrincipal,"mm_containers_state_get",{limit:1}) as typeof fullState).servers[0].containers[0].rosId,"*c1") + const exhausted = await invokeMcpTool(deviceStatePrincipal,"mm_containers_state_get",{offset:100}) as {incomplete:boolean;servers:{containers:unknown[]}[]} + assert.equal(exhausted.incomplete,true); assert.equal(exhausted.servers[0].containers.length,0) + containerStateFailure = "envs" + for (const url of ["/api/containers","/api/servers/1/containers"]) assert.equal((await containersHttp.inject({method:"GET",url})).statusCode,502) + containerStateFailure = "unsupported" + assert.deepEqual((await containersHttp.inject({method:"GET",url:"/api/containers"})).json(),{containers:[]}) + assert.equal((await invokeMcpTool(deviceStatePrincipal,"mm_containers_state_get",{}) as {servers:{availability:string}[]}).servers[0].availability,"unsupported") + for (const method of ["GET","POST"] as const) { + const response = await containersHttp.inject({method,url:method==="GET"?"/api/certificates":"/api/certificates/refresh"}) + assert.equal(response.statusCode,200,response.body); assert.equal(response.json().failures.length,0) + const observed = await invokeMcpTool(deviceStatePrincipal,"mm_certificates_state_get",{}) as {servers:{certificates:{id:string;name:string;status:string;validUntil:string|null}[]}[]} + assert.deepEqual(response.json().certificates.map((row:{id:string;name:string})=>({id:row.id,name:row.name})),observed.servers[0].certificates.map(row=>({id:row.id,name:row.name}))) + assert.equal(observed.servers[0].certificates[0].validUntil,"2034-12-31T17:00:00.000Z"); assert.equal(observed.servers[0].certificates[1].status,"unknown") + assert.ok(!JSON.stringify(response.json()).includes("never-return-certificate-key")) + } + const certificateEnd = await invokeMcpTool(deviceStatePrincipal,"mm_certificates_state_get",{offset:100}) as {incomplete:boolean;servers:{certificates:unknown[]}[]} + assert.equal(certificateEnd.incomplete,true); assert.equal(certificateEnd.servers[0].certificates.length,0) + const { listCertificatesFromServers: legacyCertificateCatalog } = await import("../../services/certificates-service.js") + const noCertificatePolls = catalogCalls; assert.deepEqual(await legacyCertificateCatalog([]),{certificates:[],failures:[]}); assert.equal(catalogCalls,noCertificatePolls) + certificateStateMalformed = true + for (const method of ["GET","POST"] as const) { + const response = await containersHttp.inject({method,url:method==="GET"?"/api/certificates":"/api/certificates/refresh"}) + assert.equal(response.statusCode,200,response.body); assert.equal(response.json().certificates.length,0); assert.equal(response.json().failures.length,1) + assert.equal(response.json().failures[0].error,"Некорректный ответ каталога сертификатов") + } + const invalidCertificates = await invokeMcpTool(deviceStatePrincipal,"mm_certificates_state_get",{}) as {incomplete:boolean;servers:{error:string}[]} + assert.equal(invalidCertificates.incomplete,true); assert.equal(invalidCertificates.servers[0].error,"Некорректный ответ каталога сертификатов") + } finally { + containerStateFailure = "none"; reverseContainerRows = false; certificateStateMalformed = false + await pool.query("UPDATE servers SET enabled=true WHERE id=ANY($1::bigint[])",[otherEnabled]); await containersHttp.close() + } + deviceStateFixtures = false; countCatalogCalls = false + console.log("✓ Container/certificate states use shared collectors with pre-poll scope, paging, explicit unsupported/partial/unknown, timezone evidence and no env/command/key leakage in response or audit") + const serverHistoryAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Server history and source address", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:servers:read"], scope: { serverIds: [1], operationIds: ["servers.snapshots.read", "servers.source-address.read"], allowGlobal: false } }), actor) + const serverHistoryPrincipal = await authenticateMcpToken(serverHistoryAccess.token) + await (await import("../../db/partitions.js")).ensurePartitionFor(pool, "server_snapshots", "week", new Date("2026-10-03T00:00:00Z")) + await pool.query("INSERT INTO server_snapshots(server_id,polled_at,status,cpu_load,identity_name) VALUES(1,'2026-10-03T00:00:00Z','online',0,'visible-old'),(1,'2026-10-03T00:00:01Z','offline',NULL,'visible-new'),(2,'2026-10-03T00:00:02Z','online',99,'hidden-server')") + const serverHistory = await invokeMcpTool(serverHistoryPrincipal, "mm_server_snapshots_get", { serverId: 1, limit: 1 }) as { serverId: number; snapshots: { status: string; identityName: string }[]; incomplete: boolean } + assert.equal(serverHistory.serverId, 1); assert.equal(serverHistory.snapshots.length, 1); assert.equal(serverHistory.snapshots[0].status, "offline"); assert.equal(serverHistory.snapshots[0].identityName, "visible-new"); assert.equal(serverHistory.incomplete, true) + assert.ok(!JSON.stringify(serverHistory).includes("hidden-server")) + await assert.rejects(invokeMcpTool(serverHistoryPrincipal, "mm_server_snapshots_get", { serverId: 2 })) + await assert.rejects(invokeMcpTool(serverHistoryPrincipal, "mm_server_snapshots_get", { serverId: 1, limit: 201 })) + const sourceAddress = await invokeMcpTool(serverHistoryPrincipal, "mm_server_source_address_get", { serverId: 1 }) as { host: string; ipv4: string; interfaceAddressVerified: boolean } + assert.equal(sourceAddress.host, "127.0.0.1"); assert.equal(sourceAddress.ipv4, "127.0.0.1"); assert.equal(sourceAddress.interfaceAddressVerified, false) + await assert.rejects(invokeMcpTool(serverHistoryPrincipal, "mm_server_source_address_get", { serverId: 2 })) + await assert.rejects(invokeMcpTool(serverHistoryPrincipal, "mm_server_source_address_get", { serverId: 1, host: "outside.example.invalid" })) + await pool.query("DELETE FROM server_snapshots WHERE identity_name IN ('visible-old','visible-new','hidden-server')") + console.log("✓ Server history uses the shared bounded SQL service; source resolution accepts only the registered host and does not attest interface ownership") + countCatalogCalls = true + const beforeTrafficHistoryCalls = catalogCalls + await (await import("../../test/mcp-traffic-history.js")).verifyMcpTrafficHistory(actor) + await (await import("../../test/mcp-user-traffic-history.js")).verifyMcpUserTrafficHistory(actor) + await (await import("../../test/mcp-bgp-export.js")).verifyMcpBgpExport(actor) + await (await import("../../test/mcp-bgp-filter-binding.js")).verifyMcpBgpFilterBinding(actor) + await (await import("../../test/mcp-bgp-community-policy.js")).verifyMcpBgpCommunityPolicy(actor) + await (await import("../../test/mcp-bgp-prefixes.js")).verifyMcpBgpPrefixes(actor) + await (await import("../../test/mcp-filter-shared.js")).verifySharedFilterWrites() + await (await import("../../test/mcp-filter-write.js")).verifyMcpFilterWrites(actor) + await (await import("../../test/mcp-uptime-overview.js")).verifyMcpUptimeOverview(actor) + await (await import("../../test/mcp-network-hosts.js")).verifyMcpNetworkHosts(actor) + assert.equal(catalogCalls, beforeTrafficHistoryCalls) + await (await import("../../test/mcp-sidebar-counts.js")).verifyMcpSidebarCounts(actor, () => catalogCalls) + countCatalogCalls = false + const { preflightPaths } = await import("./ecmp-preflight.js") + await (await import("../../test/mcp-path-completion.js")).verifyMcpPathCompletion(actor) + await (await import("../../test/mcp-log-artifacts.js")).verifyMcpLogArtifacts(actor) + await (await import("../../test/mcp-alerts-outbox.js")).verifyMcpAlertsOutbox(actor) + preflightFixtures = Object.fromEntries(Object.values(preflightPaths("ipv4")).map(path => ["/rest" + path, []])) + preflightFixtures["/rest/ip/route"] = [1, 2].map(index => ({ ".id": `*p${index}`, "dst-address": "192.0.2.0/24", gateway: `192.0.2.${index}`, "immediate-gw": `192.0.2.${index}%same-gre`, "routing-table": "main", distance: "10", active: "true", disabled: "false" })) + preflightFixtures["/rest/ip/settings"] = { "rp-filter": "strict", "ipv4-multipath-hash-policy": "l3" } + preflightFixtures["/rest/routing/table"] = [{ ".id": "*main", name: "main", fib: "true" }] + preflightFixtures["/rest/ip/firewall/connection/tracking"] = { enabled: "auto" } + preflightFixtures["/rest/ip/firewall/filter"] = [{ ".id": "*ft", action: "fasttrack-connection", disabled: "false" }] + preflightFixtures["/rest/ip/firewall/nat"] = [{ ".id": "*nat", action: "masquerade", disabled: "false" }] + const preflightAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Read-only ECMP preflight", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["routing.ecmp.preflight.read"], allowGlobal: false } }), actor) + const preflightPrincipal = await authenticateMcpToken(preflightAccess.token), preflightWritesBefore = writes + countCatalogCalls = true; catalogCalls = 0 + const preflightInput = { serverId: 1, family: "ipv4", routeIds: ["*p1", "*p2"] } + const preflight = await invokeMcpTool(preflightPrincipal, "mm_ecmp_preflight_get", preflightInput) as { configurationObserved: boolean; findings: { code: string }[]; networkValidation: string } + assert.equal(preflight.configurationObserved, true); assert.equal(preflight.networkValidation, "not-performed"); assert.equal(catalogCalls, 10) + for (const code of ["SHARED_EGRESS_INTERFACE", "STRICT_RPF_REVIEW", "FASTTRACK_REVIEW", "NAT_AFFINITY_REVIEW"]) assert.ok(preflight.findings.some(item => item.code === code), code) + await assert.rejects(invokeMcpTool(preflightPrincipal, "mm_ecmp_preflight_get", { ...preflightInput, serverId: 2 })); assert.equal(catalogCalls, 10) + preflightFixtures["/rest/routing/table"] = [{ ".id": "*main", name: "main", fib: "" }] + const noFibPreflight = await invokeMcpTool(preflightPrincipal, "mm_ecmp_preflight_get", preflightInput) as typeof preflight + assert.equal(noFibPreflight.configurationObserved, false) + assert.ok(noFibPreflight.findings.some(item => item.code === "FIB_TABLE_UNCONFIRMED")) + preflightFixtures["/rest/routing/table"] = [{ ".id": "*main", name: "main", fib: "true" }] + preflightUnavailable = "/rest/ip/firewall/nat" + const unknownPreflight = await invokeMcpTool(preflightPrincipal, "mm_ecmp_preflight_get", preflightInput) as typeof preflight + assert.equal(unknownPreflight.configurationObserved, false); assert.ok(unknownPreflight.findings.some(item => item.code === "OBSERVATION_UNAVAILABLE")); assert.equal(writes, preflightWritesBefore) + preflightFixtures = undefined; preflightUnavailable = ""; countCatalogCalls = false + console.log("✓ ECMP preflight is read-only, scopes before polling, detects shared egress/FastTrack/NAT/strict RPF and preserves unknown resources without claiming network acceptance") + const removalAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Container removal", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["containers.remove", "containers.restart", "containers.start"], allowGlobal: false } }), actor) + const removalPrincipal = await authenticateMcpToken(removalAccess.token) + const legacyRemovalAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Legacy container delete alias", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["ros.container.delete"], allowGlobal: false } }), actor) + const legacyRemovalPrincipal = await authenticateMcpToken(legacyRemovalAccess.token) + const removalRow = { ".id": "*remove", name: "fixture-container", interface: "fixture-veth", status: "stopped", "start-on-boot": "false", cmd: "never-return-command-identity", comment: "fixture" } + const containerArgs = { serverId: 1, id: "*remove" } + const waitContainerOperation = async (id: string) => { + for (let attempt = 0; attempt < 100; attempt++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state + if (!["running", "pending"].includes(state)) return state + await delay(30) + } + throw Error("Container operation timed out") + } + containerMutationFixtures = true + for (const badStatus of ["running", "unknown", "starting", "error"]) { + mutableContainers = [{ ...removalRow, status: badStatus }] + await assert.rejects(invokeMcpTool(removalPrincipal, "mm_container_remove_plan", containerArgs)) + await assert.rejects(invokeMcpTool(legacyRemovalPrincipal, "mm_container_delete_plan", containerArgs)) + } + assert.equal(containerDeletes, 0); assert.equal(containerCommands, 0) + mutableContainers = [{ ...removalRow }] + for (const kind of ["interface", "veth"]) { + const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protection, JSON.stringify({ kind, id: "*veth", serverId: 1, protection: "deny" })]) + await assert.rejects(invokeMcpTool(removalPrincipal, "mm_container_remove_plan", containerArgs)) + await assert.rejects(invokeMcpTool(legacyRemovalPrincipal, "mm_container_delete_plan", containerArgs)) + await assert.rejects(invokeMcpTool(removalPrincipal, "mm_container_start_plan", containerArgs)) + await assert.rejects(invokeMcpTool(removalPrincipal, "mm_container_restart_plan", containerArgs)) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + } + for (const drift of ["configuration", "state", "parent"]) { + mutableContainers = [{ ...removalRow }]; containerParent.name = "fixture-veth" + const prepared = await invokeMcpTool(removalPrincipal, "mm_container_remove_plan", containerArgs) as { planId: string } + assert.ok(!JSON.stringify(prepared).includes("never-return-command-identity")) + await approvePlan(prepared.planId, actor, true) + if (drift === "configuration") mutableContainers[0].name = "replacement-same-id" + if (drift === "state") mutableContainers[0].status = "running" + if (drift === "parent") containerParent.name = "renamed-veth" + assert.equal(await waitContainerOperation((await applyPlan(removalPrincipal, prepared.planId, randomUUID())).id), "rejected") + } + assert.equal(containerDeletes, 0); containerParent.name = "fixture-veth" + mutableContainers = [{ ...removalRow }] + const restartPlan = await invokeMcpTool(removalPrincipal, "mm_container_restart_plan", containerArgs) as { planId: string } + await approvePlan(restartPlan.planId, actor, true) + assert.equal(await waitContainerOperation((await applyPlan(removalPrincipal, restartPlan.planId, randomUUID())).id), "applied") + assert.equal(containerCommands, 1); assert.equal(mutableContainers[0].status, "running") + const runningRestart = await invokeMcpTool(removalPrincipal, "mm_container_restart_plan", containerArgs) as { planId: string } + await approvePlan(runningRestart.planId, actor, true) + assert.equal(await waitContainerOperation((await applyPlan(removalPrincipal, runningRestart.planId, randomUUID())).id), "applied") + assert.equal(containerCommands, 3); assert.equal(mutableContainers[0].status, "running") + mutableContainers = [{ ...removalRow }] + const replacedStart = await invokeMcpTool(removalPrincipal, "mm_container_start_plan", containerArgs) as { planId: string } + await approvePlan(replacedStart.planId, actor, true); replaceContainerAfterCommand = true + assert.equal(await waitContainerOperation((await applyPlan(removalPrincipal, replacedStart.planId, randomUUID())).id), "partial") + replaceContainerAfterCommand = false + mutableContainers = [{ ...removalRow }] + const commandsBeforeRemoval = containerCommands + const removePlan = await invokeMcpTool(legacyRemovalPrincipal, "mm_container_delete_plan", containerArgs) as { planId: string } + await approvePlan(removePlan.planId, actor, true) + const removed = await applyPlan(legacyRemovalPrincipal, removePlan.planId, "remove-container-once") + assert.equal(await waitContainerOperation(removed.id), "applied"); assert.equal(containerDeletes, 1); assert.equal(mutableContainers.length, 0); assert.equal(containerCommands, commandsBeforeRemoval) + await applyPlan(legacyRemovalPrincipal, removePlan.planId, "remove-container-once"); assert.equal(containerDeletes, 1) + mutableContainers = [{ ...removalRow }]; ignoreContainerDelete = true + const unconfirmedRemoval = await invokeMcpTool(removalPrincipal, "mm_container_remove_plan", containerArgs) as { planId: string } + await approvePlan(unconfirmedRemoval.planId, actor, true) + const unconfirmed = await applyPlan(removalPrincipal, unconfirmedRemoval.planId, "unconfirmed-container-removal") + assert.equal(await waitContainerOperation(unconfirmed.id), "unknown"); assert.equal(containerDeletes, 2) + await applyPlan(removalPrincipal, unconfirmedRemoval.planId, "unconfirmed-container-removal"); assert.equal(containerDeletes, 2) + ignoreContainerDelete = false; delete containerParent.disabled + mutableContainers = [{ ...removalRow }] + const omittedParentFlag = await invokeMcpTool(removalPrincipal, "mm_container_remove_plan", containerArgs) as { planId: string } + await approvePlan(omittedParentFlag.planId, actor, true) + assert.equal(await waitContainerOperation((await applyPlan(removalPrincipal, omittedParentFlag.planId, randomUUID())).id), "applied") + assert.equal(containerDeletes, 3) + containerParent.disabled = "false"; containerMutationFixtures = false + console.log("✓ Container removal shares REST stopped-only service; native alias cannot bypass state/parent protections, drift and ID reuse reject writes, command identity and failed delete verification never claim success or replay") + const tableAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "routing-dependency-test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write"], scope: { serverIds: [1], operationIds: ["ros.routing_table.delete", "ros.ipv4_route.create"], allowGlobal: false } }), actor) + const tablePrincipal = await authenticateMcpToken(tableAccess.token) + await assert.rejects(invokeMcpTool(tablePrincipal, "mm_ipv4_route_create_plan", { serverId: 1, fields: { "dst-address": "192.0.2.0/24", "routing-table": "missing" } }), /Таблица маршрутизации/) + fixtureRouteTable = "fixture-table" + await assert.rejects(invokeMcpTool(tablePrincipal, "mm_routing_table_delete_plan", { serverId: 1, id: "*table" }), /Таблица используется/) + fixtureRouteTable = undefined + try { + for (const path of ["/rest/ip/route", "/rest/ipv6/route"]) for (const target of ["fixture-table", "*table"]) { + preflightFixtures = { "/rest/ip/route": [], "/rest/ipv6/route": [], [path]: [{ ".id": "*LOOKUP", "routing-table": "main", gateway: "fixture-gre@" + target, dynamic: "false" }] } + await assert.rejects(invokeMcpTool(tablePrincipal, "mm_routing_table_delete_plan", { serverId: 1, id: "*table" }), /Таблица используется/) + } + preflightFixtures = { "/rest/ip/route": [{ ".id": "*BADLOOKUP", "routing-table": "main", gateway: {} }], "/rest/ipv6/route": [] } + await assert.rejects(invokeMcpTool(tablePrincipal, "mm_routing_table_delete_plan", { serverId: 1, id: "*table" }), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + } finally { preflightFixtures = undefined } + fixtureRoutingRules = [{ ".id": "*rule", table: "fixture-table", action: "lookup" }] + await assert.rejects(invokeMcpTool(tablePrincipal, "mm_routing_table_delete_plan", { serverId: 1, id: "*table" }), /Таблица используется/) + fixtureRoutingRules = [] + const tablePlan = await invokeMcpTool(tablePrincipal, "mm_routing_table_delete_plan", { serverId: 1, id: "*table" }) as { planId: string } + await approvePlan(tablePlan.planId, actor, true) + fixtureRoutingRules = [{ ".id": "*late-rule", table: "fixture-table", action: "lookup" }] + const tableOperation = await applyPlan(tablePrincipal, tablePlan.planId, randomUUID()) + for (let attempt = 0; attempt < 100; attempt++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [tableOperation.id])).rows[0].state + if (!["pending", "running"].includes(state)) { assert.equal(state, "rejected"); break } + assert.ok(attempt < 99, "Table dependency operation did not finish") + await delay(30) + } + assert.equal((await pool.query("SELECT error_code FROM mcp_operations WHERE id=$1", [tableOperation.id])).rows[0].error_code, "STATE_CONFLICT") + fixtureRoutingRules = [] + console.log("✓ Routing tables: missing table rejected, route/rule references protected, post-approval dependency changes reject before write") + interfaceFixtures = true + const initialVrfs: Record[] = [{ ".id": "*older", name: "older", interfaces: "ether9" }, { ".id": "*MAINVRF", name: "main", interfaces: "all", builtin: "true" }] + fixtureVrfs = [...initialVrfs] + const interfaceAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "overlay-dependency-test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write"], scope: { serverIds: [1], operationIds: ["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete", "ros.ipv4_address.create", "ros.gre.delete", "operations.recover"], allowGlobal: false } }), actor) + const interfacePrincipal = await authenticateMcpToken(interfaceAccess.token) + const awaitOperation = async (id: string, expected: string) => { + const read = () => pool.query<{ state: string; error_code: string | null }>("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id]) + const deadline = performance.now() + 30_000 + while (performance.now() < deadline) { + const { state, error_code } = (await read()).rows[0] + if (!["pending", "running"].includes(state)) { assert.equal(state, expected, `operation=${id} errorCode=${error_code ?? "none"}`); return } + await delay(50) + } + const { state, error_code } = (await read()).rows[0] + const { rows: steps } = await pool.query("SELECT state,count(*)::int AS count FROM mcp_operation_steps WHERE operation_id=$1 GROUP BY state ORDER BY state", [id]) + assert.fail(`Fixture operation did not finish: operation=${id} state=${state} errorCode=${error_code ?? "none"} steps=${JSON.stringify(steps)} pool=${pool.totalCount}/${pool.idleCount}/${pool.waitingCount}`) + } + const addressArgs = { serverId: 1, fields: { address: "192.0.2.1/30", interface: "fixture-gre" } } + const addressPlan = await invokeMcpTool(interfacePrincipal, "mm_ipv4_address_create_plan", addressArgs) as { planId: string } + const addressEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [addressPlan.planId])).rows[0].evidence.sealed)) + assert.ok(addressEvidence.objects.some((row: { kind: string; id: string }) => row.kind === "gre" && row.id === "*gre")) + const greProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [greProtection, JSON.stringify({ kind: "gre", id: "*gre", serverId: 1, protection: "deny" })]) + await assert.rejects(invokeMcpTool(interfacePrincipal, "mm_ipv4_address_create_plan", addressArgs)) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [greProtection]) + // This legacy fixture has symbolic IDs and no native companions. It cannot + // grant production execution. Full CRUD/order/ACK/readback is covered by the + // registered genuine VRF production component with native raw vectors. + const vrfArgs = { serverId: 1, fields: { name: "owned-vrf", interfaces: "fixture-gre" } } + const plansBeforeLegacyVrf = Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [interfaceAccess.connection.id])).rows[0].n) + for (const action of ["create", "update", "delete"]) await assert.rejects(invokeMcpTool(interfacePrincipal, `mm_vrf_${action}_plan`, { ...vrfArgs, ...(action === "create" ? {} : { id: "*CREATEDVRF" }) }), /productionAdmission:true/) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [interfaceAccess.connection.id])).rows[0].n), plansBeforeLegacyVrf) + assert.equal(vrfWrites, 0) + // Synthetic existing state is only a dependency fixture, not an ACK proof. + fixtureVrfs.unshift({ ".id": "*CREATEDVRF", name: "owned-vrf", interfaces: "fixture-gre", disabled: "false" }) + const ownedVrfId = String(fixtureVrfs[0][".id"]), beforeReferencedVrf = vrfWrites + try { + for (const path of ["/rest/ip/route", "/rest/ipv6/route"]) for (const target of ["owned-vrf", ownedVrfId]) { + preflightFixtures = { "/rest/ip/route": [], "/rest/ipv6/route": [], [path]: [{ ".id": "*RESOLVER", "dst-address": path.includes("ipv6") ? "fd00::2/128" : "192.0.2.2/32", "routing-table": "main", gateway: "fixture-gre@" + target, dynamic: "false" }] } + await assert.rejects(invokeMcpTool(interfacePrincipal, "mm_vrf_delete_plan", { serverId: 1, id: ownedVrfId, productionAdmission: true }), /VRF используется gateway/) + await assert.rejects(invokeMcpTool(interfacePrincipal, "mm_vrf_update_plan", { serverId: 1, id: ownedVrfId, productionAdmission: true, fields: { name: "renamed-vrf" } }), /VRF используется gateway/) + await assert.rejects(invokeMcpTool(interfacePrincipal, "mm_vrf_update_plan", { serverId: 1, id: ownedVrfId, productionAdmission: true, fields: { disabled: true } }), /VRF используется gateway/) + } + preflightFixtures = { "/rest/ip/route": [{ ".id": "*SELF", "dst-address": "192.0.2.0/30", "routing-table": "owned-vrf", gateway: "fixture-gre@owned-vrf", dynamic: "true" }], "/rest/ipv6/route": [] } + // Preserve the dynamic self-route exception at its actual dependency + // layer; this incomplete symbolic fixture is not raw admission evidence. + await addRoutingDependencies(1, "vrf", "delete", fixtureVrfs[0], {}, [], []) + preflightFixtures["/rest/ip/route"] = [{ ".id": "*OWNSTATIC", "dst-address": "192.0.2.0/30", "routing-table": "owned-vrf", gateway: "fixture-gre", dynamic: "false" }] + await assert.rejects(invokeMcpTool(interfacePrincipal, "mm_vrf_delete_plan", { serverId: 1, id: ownedVrfId, productionAdmission: true }), /VRF используется настроенным маршрутом/) + const lateGateway = { ".id": "*RESOLVER", "dst-address": "192.0.2.2/32", "routing-table": "main", gateway: "fixture-gre@owned-vrf-other", dynamic: "false" } + preflightFixtures = { "/rest/ip/route": [lateGateway], "/rest/ipv6/route": [] } + await addRoutingDependencies(1, "vrf", "delete", fixtureVrfs[0], {}, [], []) + lateGateway.gateway = "fixture-gre@owned-vrf" + await assert.rejects(addRoutingDependencies(1, "vrf", "delete", fixtureVrfs[0], {}, [], []), /VRF используется gateway/) + assert.equal(vrfWrites, beforeReferencedVrf) + } finally { preflightFixtures = undefined } + await assert.rejects(invokeMcpTool(interfacePrincipal, "mm_gre_delete_plan", { serverId: 1, id: "*gre" }), /используется/) + // Late post-approval gateway drift, ignored ordering, full CRUD and explicit + // no-inverse/read-only reconciliation are tested by the genuine component. + fixtureVrfs = [...initialVrfs, { ".id": "*MANAGEDVRF", name: "managed-vrf", interfaces: "fixture-gre", comment: "MikrotikManager: integration" }] + const vrfAliasAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "VRF alias dependency", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write"], scope: { serverIds: [1], operationIds: ["routing.vrf.update"], allowGlobal: false } }), actor) + const vrfAliasPrincipal = await authenticateMcpToken(vrfAliasAccess.token) + nativeAliasFixtures = true + try { + const oldParentProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [oldParentProtection, JSON.stringify({ kind: "gre", id: "*a1", serverId: 1, protection: "deny" })]) + await assert.rejects(invokeMcpTool(vrfAliasPrincipal, "mm_vrf_plan", { serverId: 1, id: "*abc", interfaces: ["fixture-bridge"] }), { code: "PROTECTED_OBJECT" }) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [oldParentProtection]) + const connectedAliasRoutes = nativeAliasCatalogs["/rest/ip/route"] + try { + nativeAliasCatalogs["/rest/ip/route"] = [] + await assert.rejects(invokeMcpTool(vrfAliasPrincipal, "mm_vrf_plan", { serverId: 1, id: "*abc", interfaces: ["fixture-bridge"] }), /dependency connected route identity/) + } finally { nativeAliasCatalogs["/rest/ip/route"] = connectedAliasRoutes } + const vrfAliasPlan = await invokeMcpTool(vrfAliasPrincipal, "mm_vrf_plan", { serverId: 1, id: "*abc", interfaces: ["fixture-bridge"] }) as { planId: string; vrfTicket: { ticketId: string } } + assert.ok(vrfAliasPlan.vrfTicket.ticketId) + const vrfAliasEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [vrfAliasPlan.planId])).rows[0].evidence.sealed)) + assert.ok(vrfAliasEvidence.objects.some((row: { kind: string; id: string }) => row.kind === "gre" && row.id === "*a1")) + assert.ok(vrfAliasEvidence.objects.some((row: { kind: string; id: string }) => row.kind === "ipv4_address" && row.id === "*a3")) + assert.ok(vrfAliasEvidence.objects.some((row: { kind: string; id: string }) => row.kind === "ipv4_route" && row.id === "*a4")) + assert.equal(vrfAliasEvidence.snapshots.filter((row: { resource: string }) => row.resource === "vrf").length, 1) + assert.equal(vrfWrites, 0) + } finally { nativeAliasFixtures = false } + ospfFixtures = true + fixtureVrfs = [...initialVrfs] + const ospfAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "OSPF dependency test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write"], scope: { serverIds: [1], operationIds: ["ros.ospf_instance.create", "ros.ospf_instance.delete", "ros.ospf_area.delete", "ros.ospf_template.create", "ros.ospf_template.update", "ros.ospf_template.delete", "operations.recover", "ros.bfd_configuration.create"], allowGlobal: false } }), actor) + const ospfPrincipal = await authenticateMcpToken(ospfAccess.token) + const { readRosSnapshot: readPassiveSnapshot } = await import("./resources.js") + const passiveFixture = fixtureOspf["interface-template"][0] as Record, previousPassive = passiveFixture.passive + try { + passiveFixture.passive = "" + assert.equal((await readPassiveSnapshot(1, "ospf_template")).rows[0].passive, "true") + assert.equal(passiveFixture.passive, "", "Effective reads must not rewrite the native fixture") + delete passiveFixture.passive + assert.equal((await readPassiveSnapshot(1, "ospf_template")).rows[0].passive, "false") + } finally { if (previousPassive === undefined) delete passiveFixture.passive; else passiveFixture.passive = previousPassive } + const passiveCall = async (tool: string, args: Record) => { + const plan = await invokeMcpTool(ospfPrincipal, tool, args) as { planId: string } + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(ospfPrincipal, plan.planId, key) + return { ...plan, key, operation, state: await waitRecursiveOperation(operation.id) } + } + try { + const created = await passiveCall("mm_ospf_template_create_plan", { serverId: 1, fields: { area: "fixture-area", interfaces: "fixture-bridge", cost: 30, passive: false } }) + assert.equal(created.state, "applied") + assert.equal((await readPassiveSnapshot(1, "ospf_template")).rows.find(row => row[".id"] === "*abc123")!.passive, "false") + assert.equal((await passiveCall("mm_ospf_template_update_plan", { serverId: 1, id: "*abc123", fields: { passive: true, cost: 40 } })).state, "applied") + const writesBefore = ospfPassiveWrites + const changed = await passiveCall("mm_ospf_template_update_plan", { serverId: 1, id: "*abc123", fields: { passive: false, cost: 30 } }) + assert.equal(changed.state, "applied"); assert.equal(ospfPassiveWrites, writesBefore + 2) + const intended = (await pool.query("SELECT intended_state FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [changed.operation.id])).rows + assert.deepEqual(intended.map(row => row.intended_state.step.fields), [{ cost: "30" }, { passive: "false" }]) + assert.equal((await passiveCall("mm_operation_recovery_plan", { operationId: changed.operation.id })).state, "applied") + let row = (await readPassiveSnapshot(1, "ospf_template")).rows.find(row => row[".id"] === "*abc123")! + assert.equal(row.passive, "true"); assert.equal(row.cost, "40") + ospfIgnoreUnset = true + const beforeIgnored = ospfPassiveWrites + const ignored = await passiveCall("mm_ospf_template_update_plan", { serverId: 1, id: "*abc123", fields: { passive: false, cost: 30 } }) + assert.equal(ignored.state, "partial"); assert.equal(ospfPassiveWrites, beforeIgnored + 2) + assert.equal((await applyPlan(ospfPrincipal, ignored.planId, ignored.key)).id, ignored.operation.id); assert.equal(ospfPassiveWrites, beforeIgnored + 2) + row = (await readPassiveSnapshot(1, "ospf_template")).rows.find(row => row[".id"] === "*abc123")! + assert.equal(row.passive, "true"); assert.equal(row.cost, "30") + console.log("✓ OSPF passive switch: false create omits switch, true PATCH, explicit unset with separate write intent, combined fields/inverse, ignored unset is partial and never replayed") + } finally { + ospfIgnoreUnset = false + fixtureOspf["interface-template"] = fixtureOspf["interface-template"].filter(row => row[".id"] !== "*abc123") + } + const networkScoped = { serverId: 1, fields: { area: "fixture-area", networks: "192.0.2.1/30", passive: true, type: "nbma", priority: 0 } } + const interfaceProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [interfaceProtection, JSON.stringify({ kind: "bridge", id: bridge[".id"], serverId: 1, protection: "deny" })]) + try { + await assert.rejects(invokeMcpTool(ospfPrincipal, "mm_ospf_template_create_plan", networkScoped), { code: "PROTECTED_OBJECT" }) + } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [interfaceProtection]) } + assert.equal((await passiveCall("mm_ospf_template_create_plan", networkScoped)).state, "applied") + const networkRow = (await readPassiveSnapshot(1, "ospf_template")).rows.find(row => row[".id"] === "*abc123") + assert.equal(networkRow?.networks, "192.0.2.0/30"); assert.equal(networkRow?.type, "nbma"); assert.equal(networkRow?.priority, "0") + assert.equal((await passiveCall("mm_ospf_template_update_plan", { serverId: 1, id: "*abc123", fields: { type: "ptmp-broadcast", priority: 255 } })).state, "applied") + assert.equal((await passiveCall("mm_ospf_template_delete_plan", { serverId: 1, id: "*abc123" })).state, "applied") + await assert.rejects(invokeMcpTool(ospfPrincipal, "mm_ospf_instance_delete_plan", { serverId: 1, id: "*ospf" }), /используется областью/) + await assert.rejects(invokeMcpTool(ospfPrincipal, "mm_ospf_area_delete_plan", { serverId: 1, id: "*area" }), /используется шаблоном/) + await assert.rejects(invokeMcpTool(ospfPrincipal, "mm_ospf_template_create_plan", { serverId: 1, fields: { area: "missing", interfaces: "fixture-gre", cost: 10 } }), /Не подтверждена зависимость/) + await assert.rejects(invokeMcpTool(ospfPrincipal, "mm_bfd_configuration_create_plan", { serverId: 1, fields: { interfaces: "fixture-gre", addresses: "0.0.0.0/0", vrf: "main" } }), /ненулевой длиной/) + const vrfProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [vrfProtection, JSON.stringify({ kind: "vrf", id: "*MAINVRF", serverId: 1, protection: "deny" })]) + 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 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 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 } })) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [peerProtection]) + const bgpCatalogAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped BGP catalog", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:bgp:read"], scope: { serverIds: [1], operationIds: ["routing.bgp.catalog.read"], allowGlobal: false } }), actor) + const bgpCatalogPrincipal = await authenticateMcpToken(bgpCatalogAccess.token) + fixtureBgp.session.push({ ...fixtureBgp.session[0], ".id": "*other-session", name: "other-session" }) + countCatalogCalls = true; catalogCalls = 0 + const bgpCatalog = await invokeMcpTool(bgpCatalogPrincipal, "mm_bgp_catalog_get", { limit: 1 }) as { serverIds: number[]; items: { serverId: number }[]; total: number; nextOffset: number; failures: unknown[] } + assert.deepEqual(bgpCatalog.serverIds, [1]); assert.equal(bgpCatalog.total, 2); assert.equal(bgpCatalog.items.length, 1); assert.equal(bgpCatalog.nextOffset, 1); assert.equal(bgpCatalog.items[0].serverId, 1); assert.equal(bgpCatalog.failures.length, 0); assert.equal(catalogCalls, 1) + const lastBgpCatalogPage = await invokeMcpTool(bgpCatalogPrincipal, "mm_bgp_catalog_get", { offset: 1, limit: 2 }) as { items: { id: string }[]; total: number; nextOffset: number | null; incomplete: boolean } + assert.equal(lastBgpCatalogPage.total, 2) + assert.equal(lastBgpCatalogPage.items.length, 1) + assert.equal(lastBgpCatalogPage.nextOffset, null) + assert.equal(lastBgpCatalogPage.incomplete, true) + await assert.rejects(invokeMcpTool(bgpCatalogPrincipal, "mm_bgp_catalog_get", { serverId: 2 })); assert.equal(catalogCalls, 2) + healthUnavailable = true + const failedBgp = await invokeMcpTool(bgpCatalogPrincipal, "mm_bgp_catalog_get", {}) as { failures: { serverId: number }[]; incomplete: boolean } + assert.equal(failedBgp.failures.length, 1); assert.equal(failedBgp.failures[0].serverId, 1); assert.equal(failedBgp.incomplete, true) + healthUnavailable = false; countCatalogCalls = false + const bgpSessionAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP session diagnostics", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:bgp:read"], scope: { serverIds: [1], operationIds: ["routing.bgp.sessions.read", "routing.bgp.sessions.raw.read"], allowGlobal: false } }), actor) + const bgpSessionPrincipal = await authenticateMcpToken(bgpSessionAccess.token) + bgpSessionOverride = fixtureBgp.session.map(row => ({ ...row, "prefix-count": "0", "tcp-md5-key": "never-return-bgp-key" })) + countCatalogCalls = true; catalogCalls = 0 + try { + const parsed = await invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_get", { serverId: 1, limit: 1 }) as { sessions: { serverId: number; prefixesRx: number | null; prefixesTx: number | null; holdTime: number | null }[]; nextOffset: number; total: number } + assert.equal(parsed.total, 2); assert.equal(parsed.nextOffset, 1); assert.equal(parsed.sessions[0].serverId, 1) + assert.equal(parsed.sessions[0].prefixesRx, 0); assert.equal(parsed.sessions[0].prefixesTx, null); assert.equal(parsed.sessions[0].holdTime, null) + const raw = await invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_raw_get", { serverId: 1, limit: 1 }) as { raw: Record[]; total: number; nextOffset: number } + assert.equal(raw.total, 2); assert.equal(raw.nextOffset, 1); assert.equal(raw.raw[0]["tcp-md5-key"], "[redacted]") + assert.ok(!JSON.stringify(raw).includes("never-return-bgp-key")); assert.equal(catalogCalls, 2) + await assert.rejects(invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_raw_get", { serverId: 2 })); assert.equal(catalogCalls, 2) + await assert.rejects(invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_get", { serverId: 2 })); assert.equal(catalogCalls, 2) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + await assert.rejects(invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_raw_get", { serverId: 1 })); assert.equal(catalogCalls, 2) + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + for (const invalid of [[null], [fixtureBgp.session[0], fixtureBgp.session[0]]]) { + bgpSessionOverride = invalid + const result = await invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_get", {}) as { sessions: unknown[]; failures: unknown[]; incomplete: boolean } + assert.deepEqual(result.sessions, []); assert.equal(result.failures.length, 1); assert.equal(result.incomplete, true) + } + bgpSessionOverride = [null] + await assert.rejects(invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_raw_get", { serverId: 1 })) + bgpSessionOverride = [] + const emptySessions = await invokeMcpTool(bgpSessionPrincipal, "mm_bgp_sessions_get", {}) as { total: number; failures: unknown[]; incomplete: boolean } + assert.equal(emptySessions.total, 0); assert.deepEqual(emptySessions.failures, []); assert.equal(emptySessions.incomplete, false) + const diagnosticsAudit = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [bgpSessionAccess.connection.id])).rows + assert.ok(!JSON.stringify(diagnosticsAudit).includes("never-return-bgp-key")) + } finally { + bgpSessionOverride = undefined; countCatalogCalls = false; fixtureBgp.session.pop() + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + } + console.log("✓ Shared BGP sessions/raw: scope before polling, bounded pages, known zero versus unknown, source/duplicate failure preserved, disabled raw refused and native keys absent from result/audit") + bgpFixtures = false + const catalogAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped OSPF catalog", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["routing.ospf.catalog.read"], allowGlobal: false } }), actor) + const catalogPrincipal = await authenticateMcpToken(catalogAccess.token) + countCatalogCalls = true; catalogCalls = 0; ospfTemplateFlagReads = 0 + fixtureOspf.instance.push({ ...fixtureOspf.instance[0], ".id": "*another", name: "another-instance" }) + const ospfCatalog = await invokeMcpTool(catalogPrincipal, "mm_ospf_catalog_get", { limit: 1 }) as { serverIds: number[]; totalServers: number; failures: unknown[]; collections: { instances: { items: { serverId: number }[]; total: number; nextOffset: number | null } } } + assert.deepEqual(ospfCatalog.serverIds, [1]); assert.equal(ospfCatalog.totalServers, 1); assert.equal(ospfCatalog.failures.length, 0) + assert.equal(ospfCatalog.collections.instances.total, 2); assert.equal(ospfCatalog.collections.instances.items.length, 1); assert.equal(ospfCatalog.collections.instances.nextOffset, 1) + assert.equal(ospfCatalog.collections.instances.items[0].serverId, 1); assert.equal(catalogCalls, 9, "Seven collections and two complementary passive query receipts expected") + assert.equal(ospfTemplateFlagReads, 2) + await assert.rejects(invokeMcpTool(catalogPrincipal, "mm_ospf_catalog_get", { serverId: 2 })) + assert.equal(catalogCalls, 9); assert.equal(ospfTemplateFlagReads, 2) + const ospfCatalogLastPage = await invokeMcpTool(catalogPrincipal, "mm_ospf_catalog_get", { serverId: 1, offset: 1, limit: 2 }) as { incomplete: boolean; failures: unknown[]; collections: { instances: { items: { id: string }[]; total: number; nextOffset: number | null } } } + assert.deepEqual(ospfCatalogLastPage.failures, []) + assert.equal(ospfCatalogLastPage.collections.instances.total, 2) + assert.deepEqual(ospfCatalogLastPage.collections.instances.items.map(item => item.id), ["*another"]) + assert.equal(ospfCatalogLastPage.collections.instances.nextOffset, null) + assert.equal(ospfCatalogLastPage.incomplete, true) + const stateAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped OSPF/BFD state", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["routing.ospf.state.read", "routing.bfd.sessions.read"], allowGlobal: false } }), actor) + const statePrincipal = await authenticateMcpToken(stateAccess.token) + type StatePage = { totalServers: number; servers: { serverId: number; collections: Record[]; total: number; nextOffset: number | null }> | null }[]; failures: unknown[]; incomplete: boolean } + const stateBefore = catalogCalls + const ospfStatePage = await invokeMcpTool(statePrincipal, "mm_ospf_state_get", { limit: 1 }) as StatePage + assert.equal(catalogCalls, stateBefore + 10); assert.equal(ospfTemplateFlagReads, 6); assert.equal(ospfStatePage.totalServers, 1); assert.deepEqual(ospfStatePage.failures, []) + assert.equal(ospfStatePage.servers[0].serverId, 1); assert.equal(ospfStatePage.servers[0].collections!.instances.total, 2) + assert.equal(ospfStatePage.servers[0].collections!.instances.nextOffset, 1); assert.equal(ospfStatePage.incomplete, true) + assert.equal(ospfStatePage.servers[0].collections!.areas.items[0].areaId, "0.0.0.0") + const runtimeInterface = ospfStatePage.servers[0].collections!.runtimeInterfaces.items[0] + assert.equal(runtimeInterface.state, "ptp"); assert.equal(runtimeInterface.interface, "fixture-ospf") + assert.equal("auth-key" in runtimeInterface, false) + const bfd = await invokeMcpTool(statePrincipal, "mm_bfd_sessions_get", {}) as StatePage + assert.equal(catalogCalls, stateBefore + 11); assert.equal(bfd.servers[0].collections!.sessions.items[0].packetsRx, null) + assert.equal(bfd.servers[0].collections!.sessions.items[0].state, "Up") + await assert.rejects(invokeMcpTool(statePrincipal, "mm_bfd_sessions_get", { serverId: 2 })); assert.equal(catalogCalls, stateBefore + 11); assert.equal(ospfTemplateFlagReads, 6) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + const disabledState = await invokeMcpTool(statePrincipal, "mm_ospf_state_get", { serverId: 1 }) as StatePage + assert.equal(disabledState.totalServers, 0); assert.deepEqual(disabledState.servers, []); assert.equal(catalogCalls, stateBefore + 11); assert.equal(ospfTemplateFlagReads, 6) + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + healthUnavailable = true + const failedBfd = await invokeMcpTool(statePrincipal, "mm_bfd_sessions_get", {}) as StatePage + assert.equal(failedBfd.servers[0].collections, null); assert.equal(failedBfd.failures.length, 1); assert.equal(failedBfd.incomplete, true) + const failedState = await invokeMcpTool(statePrincipal, "mm_ospf_state_get", {}) as StatePage + assert.equal(failedState.servers[0].collections, null); assert.equal(failedState.failures.length, 1) + const failedCatalog = await invokeMcpTool(catalogPrincipal, "mm_ospf_catalog_get", { serverId: 1 }) as { failures: { serverId: number }[]; incomplete: boolean } + assert.equal(failedCatalog.failures.length, 1); assert.equal(failedCatalog.failures[0].serverId, 1); assert.equal(failedCatalog.incomplete, true) + healthUnavailable = false; countCatalogCalls = false; fixtureOspf.instance.pop() + console.log("✓ Shared OSPF/BFD state adapters: strict observed fields and areas, one scoped poll, BFD-only query, per-device paging, source failure and no poll for hidden/disabled devices") + await acceptOspfRouteProofMcp(actor) + await acceptNetworkNeighborsMcp(actor) + await acceptIpv6NdWriteMcp(actor) + await acceptArpWriteMcp(actor) + await acceptWireguardPeerDeletion(actor) + await acceptWireguardPeerUpdate(actor) + await acceptWireguardInterfaceDeletion(actor) + await (await import("./wireguard-interface-create-acceptance.js")).acceptWireguardInterfaceCreation(actor) + await (await import("./routing-community-context-acceptance.js")).acceptRouteCommunityContextMcp(actor) + await (await import("./wireguard-peer-create-acceptance.js")).acceptWireguardPeerCreation(actor) + await (await import("../../test/mcp-flow-analytics.js")).verifyMcpFlowAnalytics(actor) + await (await import("../../test/mcp-flow-map.js")).verifyMcpFlowMap(actor) + await (await import("../../test/mcp-flow-live.js")).verifyMcpFlowLive(actor) + await (await import("../../test/mcp-flow-overlay.js")).verifyMcpFlowOverlay(actor) + await (await import("../../test/mcp-acme-test.js")).verifyMcpAcmeTest(actor) + await (await import("../../test/mcp-acme-settings.js")).verifyMcpAcmeSettings(actor) + await (await import("../../test/mcp-geoip-update.js")).verifyMcpGeoipUpdate(actor) + await (await import("../../test/mcp-telegram-settings.js")).verifyMcpTelegramSettings(actor) + await (await import("../../test/mcp-telegram-test.js")).verifyMcpTelegramTest(actor) + ospfFixtures = false + interfaceFixtures = false; fixtureVrfs = []; ignoreVrfPlacement = false + console.log("✓ GRE/VRF: parent protection, dependent delete rejection, ordered placement/read-back, inverse cleanup and reorder conflict") + const fwAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped firewall state", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["firewall.state.read"], allowGlobal: false } }), actor) + const fwPrincipal = await authenticateMcpToken(fwAccess.token) + type FirewallPage = { serverIds: number[]; collections: { rules: { items: { enabled: boolean | null; hits: number | null; log: boolean | null; matches: { srcAddress: string | null; inInterface: string; outInterface: string } }[]; total: number; nextOffset: number | null }; addressLists: { total: number } }; failures: unknown[]; incomplete: boolean; unknownRuleStates: number } + const fwRevisions = async () => Number((await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE server_id=1 AND section='firewall'")).rows[0].n) + const revisionsBeforeUnknown = await fwRevisions() + firewallStateFixtures = true; firewallStateCalls = 0 + try { + const unknownFw = await invokeMcpTool(fwPrincipal, "mm_firewall_state_get", { limit: 1 }) as FirewallPage + assert.equal(firewallStateCalls, 10); assert.deepEqual(unknownFw.serverIds, [1]); assert.deepEqual(unknownFw.failures, []) + assert.equal(unknownFw.collections.rules.total, 8); assert.equal(unknownFw.collections.addressLists.total, 2) + assert.equal(unknownFw.collections.rules.items[0].hits, 0); assert.equal(unknownFw.collections.rules.items[0].enabled, null) + assert.equal(unknownFw.collections.rules.items[0].log, null); assert.equal(unknownFw.unknownRuleStates, 8) + assert.equal(unknownFw.collections.rules.items[0].matches.srcAddress, null) + assert.equal(unknownFw.collections.rules.items[0].matches.inInterface, "ether1"); assert.equal(unknownFw.collections.rules.items[0].matches.outInterface, "ether2") + assert.equal(unknownFw.collections.rules.nextOffset, 1); assert.equal(unknownFw.incomplete, true) + assert.equal(await fwRevisions(), revisionsBeforeUnknown, "Unknown flags cannot be persisted as a confirmed observed revision") + assert.ok(!JSON.stringify(unknownFw).includes("never-return-firewall-key")) + await assert.rejects(invokeMcpTool(fwPrincipal, "mm_firewall_state_get", { serverId: 2 })); assert.equal(firewallStateCalls, 10) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + assert.deepEqual((await invokeMcpTool(fwPrincipal, "mm_firewall_state_get", {}) as FirewallPage).serverIds, []); assert.equal(firewallStateCalls, 10) + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + firewallKnownFlags = true + const knownFw = await invokeMcpTool(fwPrincipal, "mm_firewall_state_get", { offset: 1, limit: 1 }) as FirewallPage + assert.equal(knownFw.collections.rules.items[0].enabled, true); assert.equal(knownFw.collections.rules.items[0].log, false); assert.equal(knownFw.unknownRuleStates, 0) + assert.equal(await fwRevisions(), revisionsBeforeUnknown, "MCP firewall read must leave confirmed revisions unchanged") + firewallFailedChunk = "/rest/ipv6/firewall/nat" + const partialFw = await invokeMcpTool(fwPrincipal, "mm_firewall_state_get", {}) as FirewallPage + assert.equal(partialFw.failures.length, 1); assert.equal(partialFw.collections.rules.total, 7); assert.equal(partialFw.incomplete, true) + assert.equal(await fwRevisions(), revisionsBeforeUnknown) + firewallFailedChunk = ""; firewallDuplicate = true + const invalidFw = await invokeMcpTool(fwPrincipal, "mm_firewall_state_get", {}) as FirewallPage + assert.equal(invalidFw.failures.length, 1); assert.equal(invalidFw.collections.rules.total, 7) + assert.equal(await fwRevisions(), revisionsBeforeUnknown) + } finally { + firewallStateFixtures = false; firewallKnownFlags = false; firewallFailedChunk = ""; firewallDuplicate = false + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + } + console.log("✓ Firewall shared catalog: exactly 10 scoped reads, legacy versus nullable flags/counters, preserved matches, paging, failed/duplicate slices, no secret reflection or speculative observed revision") + const fwMoveAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Firewall move plans", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["firewall.rules.move"] }, limits: { mutationsPerHour: 50 } }), actor) + const fwMovePrincipal = await authenticateMcpToken(fwMoveAccess.token), fwMoveArgs = { serverId: 1, move: { family: "ip", table: "filter", rosId: "*1", destinationRosId: "*3" } } + const waitFirewallMove = async (operationId: string) => { + for (let attempt = 0; attempt < 200; attempt++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operationId])).rows[0].state as string + if (!["pending", "running"].includes(state)) return state + await delay(25) + } + throw new Error("Firewall move did not complete") + } + firewallMoveFixtures = true; firewallMoveCommands = 0 + try { + const { withConfigMutationLock: fwConfigLock } = await import("../../services/config-mutation-lock.js") + const { moveFirewallRule: sharedFirewallMove } = await import("../../services/firewall-rule-move.js") + const { server: fwServer } = await (await import("./resources.js")).routerClient(1) + let releaseFwLease!: () => void, markFwLeaseReady!: () => void + const fwLeaseReady = new Promise(resolve => { markFwLeaseReady = resolve }) + const fwLeaseRelease = new Promise(resolve => { releaseFwLease = resolve }) + const fwLease = fwConfigLock([1], async () => { markFwLeaseReady(); await fwLeaseRelease }) + await fwLeaseReady + try { await assert.rejects(sharedFirewallMove(fwServer, { family: "ip", table: "filter", rosId: "*1", destinationRosId: "*3" }), /изменяется другой операцией/); assert.equal(firewallMoveCommands, 0) } + finally { releaseFwLease(); await fwLease } + await assert.rejects(sharedFirewallMove({ ...fwServer, host: "changed.invalid" }, { family: "ip", table: "filter", rosId: "*1", destinationRosId: "*3" }), /Источник Firewall изменился/) + assert.equal(firewallMoveCommands, 0) + await assert.rejects(invokeMcpTool(fwMovePrincipal, "mm_firewall_rule_move_plan", { ...fwMoveArgs, serverId: 2 })) + const protectedMove = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protectedMove, JSON.stringify({ kind: "ip_filter", id: "*2", serverId: 1, protection: "deny" }), actor.id]) + await assert.rejects(invokeMcpTool(fwMovePrincipal, "mm_firewall_rule_move_plan", fwMoveArgs)) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectedMove]) + const staleMove = await invokeMcpTool(fwMovePrincipal, "mm_firewall_rule_move_plan", fwMoveArgs) as { planId: string } + await approvePlan(staleMove.planId, actor, true) + firewallMoveRows[0]["connection-mark"] = "externally-changed" + assert.equal(await waitFirewallMove((await applyPlan(fwMovePrincipal, staleMove.planId, randomUUID())).id), "rejected"); assert.equal(firewallMoveCommands, 0) + firewallMoveRows[0]["connection-mark"] = "keep-exactly" + const movePlan = await invokeMcpTool(fwMovePrincipal, "mm_firewall_rule_move_plan", fwMoveArgs) as { planId: string } + await approvePlan(movePlan.planId, actor, true) + const moveKey = randomUUID(), moved = await applyPlan(fwMovePrincipal, movePlan.planId, moveKey) + assert.equal(await waitFirewallMove(moved.id), "applied"); assert.equal(firewallMoveCommands, 1) + assert.deepEqual(firewallMoveRows.map(row => row[".id"]), ["*2", "*1", "*3"]) + assert.ok(firewallMoveRows.every(row => row["connection-mark"] === "keep-exactly")) + assert.equal((await applyPlan(fwMovePrincipal, movePlan.planId, moveKey)).id, moved.id); assert.equal(firewallMoveCommands, 1) + const movedStep = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [moved.id])).rows[0].result + assert.equal(movedStep.verified, true); assert.equal(movedStep.revisionRecorded, true) + await assert.rejects(invokeMcpTool(fwMovePrincipal, "mm_firewall_rule_move_plan", fwMoveArgs), /уже находится/) + const ignoredArgs = { ...fwMoveArgs, move: { ...fwMoveArgs.move, destinationRosId: "*2" } } + const ignoredMove = await invokeMcpTool(fwMovePrincipal, "mm_firewall_rule_move_plan", ignoredArgs) as { planId: string } + await approvePlan(ignoredMove.planId, actor, true); firewallIgnoreMove = true + assert.equal(await waitFirewallMove((await applyPlan(fwMovePrincipal, ignoredMove.planId, randomUUID())).id), "partial"); assert.equal(firewallMoveCommands, 2) + firewallIgnoreMove = false; firewallMoveRows[0].dynamic = "true" + await assert.rejects(invokeMcpTool(fwMovePrincipal, "mm_firewall_rule_move_plan", ignoredArgs), /статического/); assert.equal(firewallMoveCommands, 2) + } finally { + firewallMoveFixtures = false; firewallIgnoreMove = false + firewallMoveRows = [1, 2, 3].map(id => ({ ".id": "*" + id, chain: "fixture-move", action: "accept", disabled: "true", dynamic: "false", log: "false", comment: "rule " + id, packets: "0", "connection-mark": "keep-exactly" })) + } + console.log("✓ Firewall move uses shared REST service: exact ordered proof, opaque matcher drift, protected table rows, counter changes tolerated, native/read-back/revision, no-op/dynamic rejection, partial and no replay") + const fwWriteAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Firewall form writes", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["firewall.rules.create", "firewall.rules.update", "firewall.rules.toggle", "firewall.rules.delete"] }, limits: { mutationsPerHour: 50 } }), actor) + const fwWritePrincipal = await authenticateMcpToken(fwWriteAccess.token) + const fwFields = { chain: "fixture-form", action: "accept", disabled: true, comment: "created", tlsHost: "*.example.invalid", layer7Proto: "fixture" } + const fwInput = (operation: string, extra: Record) => ({ serverId: 1, rule: { family: "ip", table: "filter", operation, ...extra } }) + const fwFormPlan = async (operation: string, extra: Record) => invokeMcpTool(fwWritePrincipal, "mm_firewall_rule_form_" + operation + "_plan", fwInput(operation, extra)) as Promise<{ planId: string }> + const fwFormApply = async (planId: string) => { await approvePlan(planId, actor, true); const key = randomUUID(), op = await applyPlan(fwWritePrincipal, planId, key); return { op, key, state: await waitFirewallMove(op.id) } } + const fwFormProtection = randomUUID() + firewallWriteFixtures = true; firewallWriteCommands = 0; firewallWriteRows = [{ ".id": "*A", chain: "fixture-form", action: "drop", disabled: "true", dynamic: "false", log: "false", "connection-mark": "opaque" }] + try { + await assert.rejects(invokeMcpTool(fwWritePrincipal, "mm_firewall_rule_form_create_plan", { ...fwInput("create", { fields: fwFields }), serverId: 2 })); assert.equal(firewallWriteCommands, 0) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [fwFormProtection, JSON.stringify({ kind: "ethernet", id: "*parent", serverId: 1, protection: "deny" }), actor.id]) + await assert.rejects(fwFormPlan("create", { fields: fwFields })); assert.equal(firewallWriteCommands, 0) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [fwFormProtection]) + const { writeFirewallRule: writeSharedFirewall } = await import("../../services/firewall-rule-write.js"), { withConfigMutationLock: holdFormLock } = await import("../../services/config-mutation-lock.js") + const formSource = (await (await import("./resources.js")).routerClient(1)).server + await assert.rejects(writeSharedFirewall({ ...formSource, host: "changed.invalid" }, { operation: "create", family: "ip", table: "filter", fields: fwFields }), /Источник/); assert.equal(firewallWriteCommands, 0) + let unlockForm!: () => void, readyForm!: () => void + const lockFormReady = new Promise(resolve => { readyForm = resolve }), lockFormRelease = new Promise(resolve => { unlockForm = resolve }) + const formLease = holdFormLock([1], async () => { readyForm(); await lockFormRelease }); await lockFormReady + try { await assert.rejects(writeSharedFirewall(formSource, { operation: "create", family: "ip", table: "filter", fields: fwFields }), /другой операцией/); assert.equal(firewallWriteCommands, 0) } finally { unlockForm(); await formLease } + const created = await fwFormApply((await fwFormPlan("create", { fields: fwFields })).planId) + assert.equal(created.state, "applied"); assert.equal(firewallWriteCommands, 1); assert.equal(firewallWriteRows.length, 2) + assert.equal(firewallWriteRows[1]["tls-host"], "*.example.invalid"); assert.equal(firewallWriteRows[1]["layer7-protocol"], "fixture") + assert.equal((await applyPlan(fwWritePrincipal, (await pool.query("SELECT plan_id FROM mcp_operations WHERE id=$1", [created.op.id])).rows[0].plan_id, created.key)).id, created.op.id); assert.equal(firewallWriteCommands, 1) + const stale = await fwFormPlan("toggle", { rosId: "*C", disabled: false }); firewallWriteRows[0]["connection-mark"] = "changed" + assert.equal((await fwFormApply(stale.planId)).state, "rejected"); assert.equal(firewallWriteCommands, 1); firewallWriteRows[0]["connection-mark"] = "opaque" + const updated = await fwFormApply((await fwFormPlan("update", { rosId: "*C", fields: { ...fwFields, comment: "edited", action: "drop" } })).planId) + assert.equal(updated.state, "applied"); assert.equal(firewallWriteRows[1].comment, "edited"); assert.equal(firewallWriteRows[0]["connection-mark"], "opaque") + const toggled = await fwFormApply((await fwFormPlan("toggle", { rosId: "*C", disabled: false })).planId) + assert.equal(toggled.state, "applied"); assert.equal(firewallWriteRows[1].disabled, "false") + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [toggled.op.id])).rows[0].result + assert.equal(receipt.verified, true); assert.equal(receipt.revisionRecorded, true) + firewallWriteIgnored = true + const ignored = await fwFormApply((await fwFormPlan("toggle", { rosId: "*C", disabled: true })).planId) + assert.equal(ignored.state, "partial"); firewallWriteIgnored = false + assert.equal((await fwFormApply((await fwFormPlan("delete", { rosId: "*C" })).planId)).state, "applied"); assert.deepEqual(firewallWriteRows.map(row => row[".id"]), ["*A"]) + firewallWriteRows[0].dynamic = "true"; await assert.rejects(fwFormPlan("delete", { rosId: "*A" }), /статическое/) + firewallWriteRows[0].dynamic = "false" + const { default: FastifyForm } = await import("fastify"), { validatorCompiler: formValidator, serializerCompiler: formSerializer } = await import("@fastify/type-provider-zod") + const formHttp = FastifyForm(); formHttp.setValidatorCompiler(formValidator); formHttp.setSerializerCompiler(formSerializer) + await formHttp.register((await import("../../routes/firewall.js")).default, { prefix: "/api" }) + try { + for (const rosId of ["*A,*B", "*A/../../", "not-native"]) { + const invalid = await formHttp.inject({ method: "DELETE", url: "/api/firewall/rules", payload: { serverId: "1", family: "ip", table: "filter", rosId } }); assert.equal(invalid.statusCode, 400) + } + for (const [method, payload] of [["POST", { ...fwFields }], ["PUT", { ...fwFields, rosId: "*C", comment: "REST edit" }], ["PATCH", { rosId: "*C", disabled: false }], ["DELETE", { rosId: "*C" }]] as const) { + const response = await formHttp.inject({ method, url: "/api/firewall/rules", payload: { serverId: "1", family: "ip", table: "filter", ...payload } }) + assert.equal(response.statusCode, method === "POST" ? 201 : 200, response.body); assert.equal(response.json().ok, true); assert.equal(response.json().revisionRecorded, true) + } + assert.deepEqual(firewallWriteRows.map(row => row[".id"]), ["*A"]) + } finally { await formHttp.close() } + + } finally { firewallWriteFixtures = false; firewallWriteIgnored = false; await pool.query("DELETE FROM mcp_protections WHERE id=$1", [fwFormProtection]) } + console.log("✓ Firewall form REST service: create/edit/toggle/delete, TLS/L7, opaque matcher preservation, scope/protected parent, stale proof, read-back/revision, partial and idempotency") + const addrAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Address-list forms", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["firewall.addressLists.create", "firewall.addressLists.update", "firewall.addressLists.toggle", "firewall.addressLists.delete"] }, limits: { mutationsPerHour: 60 } }), actor) + const addrPrincipal = await authenticateMcpToken(addrAccess.token), addrFields = { list: "fixture-list", address: "192.0.2.2/32", disabled: true, comment: "fixture" } + const addrPlan = async (operation: string, extra: Record) => invokeMcpTool(addrPrincipal, "mm_firewall_address_list_" + operation + "_plan", { serverId: 1, address: { family: "ip", operation, ...extra } }) as Promise<{ planId: string }> + const addrApply = async (planId: string) => { await approvePlan(planId, actor, true); const key = randomUUID(), op = await applyPlan(addrPrincipal, planId, key); return { op, key, state: await waitFirewallMove(op.id) } } + const addressProtection = randomUUID() + addressFixtures = true; addressCommands = 0; addressRows = [{ ".id": "*A", list: "unrelated", address: "198.51.100.1", disabled: "true", dynamic: "false", comment: "keep" }] + try { + await assert.rejects(invokeMcpTool(addrPrincipal, "mm_firewall_address_list_create_plan", { serverId: 2, address: { operation: "create", family: "ip", fields: addrFields } })); assert.equal(addressCommands, 0) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [addressProtection, JSON.stringify({ kind: "ip_filter", id: "*rule-using-list", serverId: 1, protection: "deny" }), actor.id]) + await assert.rejects(addrPlan("create", { fields: addrFields })); assert.equal(addressCommands, 0); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [addressProtection]) + const createdPlan = await addrPlan("create", { fields: addrFields }), created = await addrApply(createdPlan.planId) + assert.equal(created.state, "applied"); assert.equal(addressRows[1].address, "192.0.2.2"); assert.equal(addressCommands, 1) + assert.equal((await applyPlan(addrPrincipal, createdPlan.planId, created.key)).id, created.op.id); assert.equal(addressCommands, 1) + const stale = await addrPlan("toggle", { rosId: "*C", disabled: false }); addressRows[0].comment = "foreign drift" + assert.equal((await addrApply(stale.planId)).state, "rejected"); assert.equal(addressCommands, 1); addressRows[0].comment = "keep" + assert.equal((await addrApply((await addrPlan("update", { rosId: "*C", fields: { ...addrFields, comment: "edited" } })).planId)).state, "applied") + assert.equal((await addrApply((await addrPlan("toggle", { rosId: "*C", disabled: false })).planId)).state, "applied") + addressIgnored = true; assert.equal((await addrApply((await addrPlan("delete", { rosId: "*C" })).planId)).state, "partial"); addressIgnored = false + assert.equal((await addrApply((await addrPlan("delete", { rosId: "*C" })).planId)).state, "applied") + assert.equal((await addrApply((await addrPlan("create", { fields: { ...addrFields, timeout: "1m" } })).planId)).state, "applied") + const timerPlan = await addrPlan("delete", { rosId: "*T" }), writesBeforeTimer = addressCommands + addressExpiry += 60000 + assert.equal((await addrApply(timerPlan.planId)).state, "rejected"); assert.equal(addressCommands, writesBeforeTimer) + const natural = await addrPlan("delete", { rosId: "*T" }); await delay(1200) + assert.equal((await addrApply(natural.planId)).state, "applied"); assert.deepEqual(addressRows.map(row => row[".id"]), ["*A"]) + const { default: AddrFastify } = await import("fastify"), { validatorCompiler: addrValidator, serializerCompiler: addrSerializer } = await import("@fastify/type-provider-zod") + const addrHttp = AddrFastify(); addrHttp.setValidatorCompiler(addrValidator); addrHttp.setSerializerCompiler(addrSerializer); await addrHttp.register((await import("../../routes/firewall.js")).default, { prefix: "/api" }) + try { for (const [method, payload] of [["POST", { ...addrFields }], ["PUT", { ...addrFields, rosId: "*C", comment: "REST edited" }], ["PATCH", { rosId: "*C", disabled: false }], ["DELETE", { rosId: "*C" }]] as const) { + const response = await addrHttp.inject({ method, url: "/api/firewall/address-lists", payload: { serverId: "1", family: "ip", ...payload } }); assert.equal(response.statusCode, method === "POST" ? 201 : 200, response.body); assert.equal(response.json().revisionRecorded, true) + } } finally { await addrHttp.close() } + } finally { addressFixtures = false; addressIgnored = false; await pool.query("DELETE FROM mcp_protections WHERE id=$1", [addressProtection]) } + console.log("✓ Address-list shared REST/MCP forms: canonical hosts, CRUD, protected dependent rule, scope/stale/partial/idempotency, finite lifetime and countdown versus renewal drift") + const fwRevisionAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Firewall revision restore", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["firewall.revision.restore"] }, limits: { mutationsPerHour: 100 } }), actor) + const fwRevisionPrincipal = await authenticateMcpToken(fwRevisionAccess.token), fwRevisionId = randomUUID() + const { canonicalFirewallSnapshot: canonFw } = await import("../../services/entity-snapshots.js"), { fingerprintPayload: fwRevHash } = await import("../../services/config-revisions.js") + const fwSnapshot = canonFw({ rules: [1, 2].map(() => ({ family: "ip" as const, table: "filter" as const, chain: "fixture-restore", action: "accept", disabled: true, native: { "connection-mark": "opaque" } })), addressLists: [{ family: "ip6", list: "fixture-restore", address: "2001:db8::1", disabled: true }] }) + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,1,'firewall','apply',$2,$3::jsonb)", [fwRevisionId, fwRevHash(fwSnapshot), JSON.stringify(fwSnapshot)]) + const fwRevisionArgs = { serverId: 1, revisionId: fwRevisionId } + const fwRevisionPlan = async () => invokeMcpTool(fwRevisionPrincipal, "mm_firewall_revision_restore_plan", fwRevisionArgs) as Promise<{ planId: string }> + const fwRevisionApply = async (planId: string) => { await approvePlan(planId, actor, true); const key = randomUUID(), op = await applyPlan(fwRevisionPrincipal, planId, key); return { op, key, state: await waitFirewallMove(op.id) } } + fwRestoreFixtures = true; fwRestoreCommands = 0; fwRestoreCollections = { "/rest/ip/firewall/filter": [{ ".id": "*D", chain: "dynamic", action: "accept", disabled: "false", dynamic: "true", log: "false" }, { ".id": "*OLD", chain: "fixture-restore", action: "drop", disabled: "true", dynamic: "false", log: "false", "connection-mark": "original" }] } + try { + await assert.rejects(invokeMcpTool(fwRevisionPrincipal, "mm_firewall_revision_restore_plan", { ...fwRevisionArgs, serverId: 2 })); assert.equal(fwRestoreCommands, 0) + await pool.query("UPDATE config_revisions SET fingerprint='bad' WHERE id=$1", [fwRevisionId]); await assert.rejects(fwRevisionPlan()); assert.equal(fwRestoreCommands, 0) + await pool.query("UPDATE config_revisions SET fingerprint=$2 WHERE id=$1", [fwRevisionId, fwRevHash(fwSnapshot)]) + const stale = await fwRevisionPlan(); fwRestoreCollections["/rest/ip/firewall/filter"][1]["connection-mark"] = "drift" + assert.equal((await fwRevisionApply(stale.planId)).state, "rejected"); assert.equal(fwRestoreCommands, 0); fwRestoreCollections["/rest/ip/firewall/filter"][1]["connection-mark"] = "original" + const protection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ethernet", id: "*protected-dependency", serverId: 1, protection: "deny" }), actor.id]) + await assert.rejects(fwRevisionPlan()); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]); assert.equal(fwRestoreCommands, 0) + const low = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Firewall restore budget", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: ["firewall.revision.restore"] }, limits: { mutationsPerHour: 2 } }), actor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(low.token), "mm_firewall_revision_restore_plan", fwRevisionArgs), error => error instanceof Error && "code" in error && error.code === "BUDGET_EXCEEDED"); assert.equal(fwRestoreCommands, 0) + const exact = await fwRevisionPlan(), applied = await fwRevisionApply(exact.planId) + assert.equal(applied.state, "applied"); assert.equal(fwRestoreCommands, 4) + assert.deepEqual(fwRestoreCollections["/rest/ip/firewall/filter"].map(row => row["connection-mark"] ?? "dynamic"), ["dynamic", "opaque", "opaque"]) + assert.equal((await applyPlan(fwRevisionPrincipal, exact.planId, applied.key)).id, applied.op.id); assert.equal(fwRestoreCommands, 4) + const step = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [applied.op.id])).rows[0].result + assert.equal(step.revisionRecorded, true); assert.equal(step.completedCommands, 4) + const enabledRevision = randomUUID(), enabledSnapshot = { ...fwSnapshot, rules: fwSnapshot.rules.map(row => ({ ...row, disabled: false })) } + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,1,'firewall','apply',$2,$3::jsonb)", [enabledRevision, fwRevHash(enabledSnapshot), JSON.stringify(enabledSnapshot)]) + const activatePlan = await invokeMcpTool(fwRevisionPrincipal, "mm_firewall_revision_restore_plan", { ...fwRevisionArgs, revisionId: enabledRevision }) as { planId: string } + const activated = await fwRevisionApply(activatePlan.planId); assert.equal(activated.state, "applied") + assert.ok(fwRestoreCollections["/rest/ip/firewall/filter"].filter(row => row.dynamic === "false").every(row => row.disabled === "false")) + const activationStep = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [activated.op.id])).rows[0].result + assert.equal(activationStep.commandCount, 8); assert.equal(activationStep.completedCommands, 8); assert.equal(activationStep.acknowledgedCommands, 8) + + const oldIds = fwRestoreCollections["/rest/ip/firewall/filter"].map(row => row[".id"]) + fwRestoreIgnored = true; const beforeIgnored = fwRestoreCommands + assert.equal((await fwRevisionApply((await fwRevisionPlan()).planId)).state, "partial"); assert.equal(fwRestoreCommands, beforeIgnored + 1); assert.deepEqual(fwRestoreCollections["/rest/ip/firewall/filter"].map(row => row[".id"]), oldIds); fwRestoreIgnored = false + const { default: RestoreFastify } = await import("fastify"), { validatorCompiler: restoreValidator, serializerCompiler: restoreSerializer } = await import("@fastify/type-provider-zod") + const restoreHttp = RestoreFastify(); restoreHttp.setValidatorCompiler(restoreValidator); restoreHttp.setSerializerCompiler(restoreSerializer); await restoreHttp.register((await import("../../routes/firewall.js")).default, { prefix: "/api" }) + try { const response = await restoreHttp.inject({ method: "POST", url: "/api/firewall/revisions/" + fwRevisionId + "/restore", payload: { serverId: "1" } }); assert.equal(response.statusCode, 200, response.body); assert.equal(response.json().ok, true); assert.equal(response.json().rules.length, 3) } finally { await restoreHttp.close() } + fwRestoreLost = true; const beforeLost = fwRestoreCommands + assert.equal((await fwRevisionApply((await fwRevisionPlan()).planId)).state, "unknown"); assert.equal(fwRestoreCommands, beforeLost + 1) + assert.ok(fwRestoreCollections["/rest/ip/firewall/filter"].some(row => row.dynamic === "false" && row.disabled === "true")) + const beforeRevoke = fwRestoreCommands; fwRestoreRevoke = fwRevisionAccess.connection.id + assert.equal((await fwRevisionApply((await fwRevisionPlan()).planId)).state, "unknown"); assert.equal(fwRestoreCommands, beforeRevoke + 1) + } finally { fwRestoreFixtures = false; fwRestoreIgnored = false; fwRestoreLost = false; fwRestoreRevoke = undefined } + console.log("✓ Firewall revision shared REST/MCP restore: strict scoped revision, full matcher/order/multiplicity, disabled staging, per-command read-back, protected/budget/stale, ignored/lost stop, revocation and no replay") + const { ensurePartitionFor: flowPartition } = await import("../../db/partitions.js") + const { getTrafficFlowSettingsRow: flowSettingsRow, invalidateTrafficFlowSettingsCache: invalidateFlowSettings } = await import("../../services/traffic-flow-settings.js") + const { getServerCatalog: flowServerCatalog, invalidateFlowCatalogCache: invalidateFlowCatalog } = await import("../../services/traffic-flow-topology.js") + const { readFlowCatalogWindow: flowWindow } = await import("../../services/traffic-flow-catalog-read.js") + const { listStoredFlowRows: flowRows, ingestParsedFlowsForServerForTests: addPendingFlow, resetFlowRingsForTests: clearPendingFlows } = await import("../../services/traffic-flow-ingest.js") + const { getFlowMonthly: flowMonthly } = await import("../../services/traffic-flow-analytics.js") + const { disableRipeEnqueueForTests: disableFlowRipe, resetRipeCacheForTests: resetFlowRipe } = await import("../../services/traffic-flow-ripe.js") + await flowSettingsRow() + const savedFlow = (await pool.query("SELECT peers_json,host_private_key,top_n FROM traffic_flow_settings WHERE id=1")).rows[0] + const flowOpIds = ["traffic.flow.talkers.read", "traffic.flow.exporters.read", "traffic.flow.clients.read", "traffic.flow.monthly.read"] + const flowActor = { ...actor, id: "netflow-reports-owner", permissions: [...actor.permissions, "mm:traffic:read"] } + const makeFlowAccess = async (ids: number[]) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped flow reports", mode: "observer", expiresAt: new Date(Date.now()+1800000).toISOString(), permissions: ["mm:traffic:read"], scope: { serverIds: ids, operationIds: flowOpIds, allowGlobal: false }, limits: { callsPerMinute: 120 } }), flowActor) + const flowAccess = await makeFlowAccess([1]), flowPrincipal = await authenticateMcpToken(flowAccess.token) + const bucket = new Date(Date.now()-60000), oldBucket = new Date(Date.now()-7200000), futureBucket = new Date(Date.now()+3600000) + const flowMonth = new Date().toISOString().slice(0,7), flowDay = flowMonth+"-01", flowUsers = ["nf-a","nf-b","nf-peer","nf-mixed","nf-disabled","nf-split"] + for (const time of [bucket,oldBucket,futureBucket]) await flowPartition(pool,"flow_buckets","day",time) + await flowPartition(pool,"flow_daily_dims","month",new Date(flowDay+"T00:00:00Z")) + await pool.query("INSERT INTO servers(id,name,host,enabled) VALUES(42,'disabled-flow-history','192.0.2.42',false)") + clearPendingFlows() + disableFlowRipe() + try { + await pool.query("UPDATE traffic_flow_settings SET top_n=20,host_private_key='NEVER_FLOW_HOST_KEY',peers_json=$1::jsonb WHERE id=1", [JSON.stringify([{serverId:1,publicKey:"public-one"},{serverId:2,publicKey:{bad:"NEVER_FLOW_PEER_KEY"},privateKey:"NEVER_FLOW_PEER_KEY"},{serverId:42,publicKey:"public-history"}])]); invalidateFlowSettings() + for (const id of flowUsers) await pool.query("INSERT INTO app_users(id,login,name,active) VALUES($1,$1,$2,$3)",[id,id==="nf-mixed"?"NEVER_FLOW_HIDDEN_USER":id,id!=="nf-b"]) + for (const [userId,serverId,interfaceName,peer] of [["nf-a",1,"visible-iface",""],["nf-b",1,"other-iface",""],["nf-peer",1,"wg-whole","peer-public"],["nf-mixed",1,"mixed-iface",""],["nf-mixed",2,"NEVER_FLOW_HIDDEN_IFACE",""],["nf-disabled",42,"offline-iface",""],["nf-split",1,"split-iface",""],["nf-split",42,"split-history"," "]] as const) await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,$2,$3,$4,$5)",[randomUUID(),userId,serverId,interfaceName,peer]) + const putFlow = async (sid: number, iface: string, bytes: string | number, src: string, at=bucket) => pool.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,bytes,packets,in_iface) VALUES($1,$2,$3,'8.8.8.8',$4,1,$5)",[sid,at.toISOString(),src,bytes,iface]) + await putFlow(1,"visible-iface",1000,"198.18.242.1"); await putFlow(1,"2",500,"198.18.242.2"); await putFlow(1,"wg-whole",700,"198.18.242.3"); await putFlow(1,"mixed-iface",900,"198.18.242.4") + await putFlow(1,"visible-iface",8000,"198.18.242.5",oldBucket); await putFlow(1,"visible-iface",9000,"198.18.242.6",futureBucket) + await putFlow(2,"NEVER_FLOW_HIDDEN_IFACE","9007199254740993","198.18.242.7"); await putFlow(42,"offline-iface",300,"198.18.242.8") + await pool.query("INSERT INTO flow_daily_dims(server_id,day,dim,key,bytes,packets) VALUES(1,$1,'country','NF_US',1000,10),(1,$1,'country','NF_DE',500,5),(1,$1,'service','NF_VISIBLE_SERVICE',500,5),(1,$1,'asn','64512',200,2),(2,$1,'country','NEVER_FLOW_HIDDEN_DIM',9007199254740993,1),(42,$1,'country','NF_GB',300,3)",[flowDay]) + await pool.query("INSERT INTO flow_daily_dims(server_id,day,dim,key,bytes,packets) SELECT 2,$1,'country','NF_HIDDEN_'||n,1,1 FROM generate_series(1,10001) n",[flowDay]) + addPendingFlow(2,[{src:"198.18.242.9",dst:"8.8.4.4",proto:6,srcPort:1,dstPort:443,bytes:7777,packets:1,inIface:"NEVER_FLOW_PENDING_IFACE",outIface:""}]) + const { applyRingSnapshot: flowRingSnapshot } = await import("../../services/traffic-flow-engine.js") + const ringQuality=(await import("../../services/traffic-flow-ingest-quality.js")).createFlowIngestQuality().snapshot() + const visibleRing={key:"1\0__all__",inBps:[...Array(59).fill(0),1000000],outBps:Array(60).fill(0)} + flowRingSnapshot([visibleRing],ringQuality) + assert.throws(()=>flowRingSnapshot([visibleRing,{key:"2\0__all__",inBps:[NaN],outBps:[]}],ringQuality),/Invalid|oversized/) + invalidateFlowCatalog(); await flowServerCatalog() + countCatalogCalls = true; catalogCalls = 0 + type ExporterPage = { exporters: { id: string; retainedRowsBytes: number; wireRateMbps: null; availability: string; collectionEnabled: boolean }[]; configurationMembershipVerified: boolean; coverageVerified: boolean; serverIds: number[] } + const exporter = await invokeMcpTool(flowPrincipal,"mm_netflow_exporters_get",{minutes:5}) as ExporterPage + assert.deepEqual(exporter.exporters.map(row=>row.id),["1"]); assert.equal(exporter.exporters[0].retainedRowsBytes,3100); assert.equal(exporter.configurationMembershipVerified,true) + const ringResult = exporter.exporters[0] as unknown as {flowRing:{available:boolean;lastSlotIngressMbps:number;wireRateVerified:boolean}} + assert.equal(ringResult.flowRing.available,true); assert.equal(ringResult.flowRing.lastSlotIngressMbps,1); assert.equal(ringResult.flowRing.wireRateVerified,false) + assert.equal(exporter.exporters[0].wireRateMbps,null); assert.equal(exporter.exporters[0].availability,"unknown"); assert.equal(exporter.coverageVerified,false) + assert.ok(!JSON.stringify(exporter).includes("NEVER_FLOW")); assert.ok(!JSON.stringify(exporter).includes("outside-scope")); assert.ok(!("lastExporterIp" in exporter)) + await pool.query("UPDATE traffic_flow_settings SET peers_json=jsonb_set(peers_json,'{0,publicKey}','{}'::jsonb) WHERE id=1"); invalidateFlowSettings() + const badPeer = await invokeMcpTool(flowPrincipal,"mm_netflow_exporters_get",{minutes:5}) as {configurationMembershipVerified:boolean;incomplete:boolean;failures:unknown[];exporters:{storedPeerRecord:boolean|null}[]} + 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}),/предел/) + 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) + assert.equal(talkers.items[0].bytes,1000); assert.deepEqual(talkers.serverIds,[1]); assert.ok(!JSON.stringify(talkers).includes("NEVER_FLOW")); assert.ok(!("lastExporterIp" in talkers)) + const secondTalker = await invokeMcpTool(flowPrincipal,"mm_netflow_talkers_get",{minutes:5,offset:1,limit:1}) as TalkerPage + assert.equal(secondTalker.items[0].bytes,900) + addPendingFlow(1,[{src:"198.18.242.10",dst:"8.8.4.4",proto:6,srcPort:1,dstPort:443,bytes:222,packets:1,inIface:"visible-iface",outIface:""}]) + assert.equal((await invokeMcpTool(flowPrincipal,"mm_netflow_talkers_get",{minutes:5}) as TalkerPage).retainedRowsBytes,3322) + clearPendingFlows() + await pool.query("UPDATE traffic_flow_settings SET top_n=1 WHERE id=1"); invalidateFlowSettings() + const limitedTalkers = await invokeMcpTool(flowPrincipal,"mm_netflow_talkers_get",{minutes:5}) as TalkerPage + assert.equal(limitedTalkers.total,1); assert.equal(limitedTalkers.totalConversations,4); assert.equal(limitedTalkers.retainedRowsBytes,3100) + await pool.query("UPDATE traffic_flow_settings SET top_n=20 WHERE id=1"); invalidateFlowSettings() + type ClientPage = { clients: { id: string; ingressNamedRowsBytes: number|null; peerBindingsNotAttributed: number; ifIndexRowsNotAttributed: number; availability: string; completeSourceSet: boolean; accountEnabled: boolean }[]; total: number; nextOffset: number|null } + const clients = await invokeMcpTool(flowPrincipal,"mm_netflow_clients_get",{minutes:5}) as ClientPage + assert.deepEqual(clients.clients.map(row=>row.id),["nf-a","nf-b","nf-peer"]); assert.equal(clients.total,3) + assert.equal(clients.clients[0].ingressNamedRowsBytes,1000); assert.equal(clients.clients[0].ifIndexRowsNotAttributed,1) + assert.equal(clients.clients[1].accountEnabled,false); assert.equal(clients.clients[1].availability,"unknown") + assert.equal(clients.clients[2].ingressNamedRowsBytes,null); assert.equal(clients.clients[2].peerBindingsNotAttributed,1) + const clientPage = await invokeMcpTool(flowPrincipal,"mm_netflow_clients_get",{minutes:5,offset:1,limit:1}) as ClientPage + assert.deepEqual(clientPage.clients.map(row=>row.id),["nf-b"]); assert.equal(clientPage.total,3); assert.equal(clientPage.nextOffset,2) + type MonthPage = { bytes: number; collections: { countries: {items:{id:string;bytes:number}[];total:number;nextOffset:number|null} }; serverIds:number[] } + const monthly = await invokeMcpTool(flowPrincipal,"mm_netflow_monthly_report_get",{month:flowMonth,limit:1}) as MonthPage + assert.equal(monthly.bytes,1500); assert.equal(monthly.collections.countries.total,2); assert.equal(monthly.collections.countries.nextOffset,1); assert.equal(monthly.collections.countries.items[0].id,"NF_US") + assert.equal((await invokeMcpTool(flowPrincipal,"mm_netflow_monthly_report_get",{month:flowMonth,offset:1,limit:1}) as MonthPage).collections.countries.items[0].id,"NF_DE") + await assert.rejects(flowMonthly(flowMonth,[2],{maxKeys:10000}),/предел/) + for(const tool of ["mm_netflow_talkers_get","mm_netflow_exporters_get","mm_netflow_clients_get","mm_netflow_monthly_report_get"]) await assert.rejects(invokeMcpTool(flowPrincipal,tool,{serverId:2,...(tool.includes("monthly")?{month:flowMonth}:{minutes:5})})) + const disabled = await authenticateMcpToken((await makeFlowAccess([42])).token) + const disabledExporter = await invokeMcpTool(disabled,"mm_netflow_exporters_get",{minutes:5}) as ExporterPage + assert.equal(disabledExporter.exporters[0].retainedRowsBytes,300); assert.equal(disabledExporter.exporters[0].collectionEnabled,false); assert.equal(disabledExporter.exporters[0].availability,"unknown") + assert.equal((await invokeMcpTool(disabled,"mm_netflow_monthly_report_get",{month:flowMonth}) as MonthPage).bytes,300) + const broad = await makeFlowAccess([1,42]), broadPrincipal = await authenticateMcpToken(broad.token) + const split = await invokeMcpTool(broadPrincipal,"mm_netflow_clients_get",{minutes:5,serverLimit:1}) as ClientPage + assert.equal(split.clients.find(row=>row.id==="nf-split")?.completeSourceSet,false) + const wide = await makeFlowAccess([1,2]), widePrincipal = await authenticateMcpToken(wide.token), grantId = randomUUID() + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)",[grantId,wide.connection.id,JSON.stringify({id:grantId,credentialId:widePrincipal.credentialId,startsAt:new Date(Date.now()-1000).toISOString(),expiresAt:new Date(Date.now()+60000).toISOString(),scope:{allowGlobal:false,serverIds:[1],operationIds:flowOpIds}})]) + assert.deepEqual((await invokeMcpTool(widePrincipal,"mm_netflow_clients_get",{minutes:5}) as ClientPage).clients.map(row=>row.id),["nf-a","nf-b","nf-peer"]) + assert.equal((await invokeMcpTool(widePrincipal,"mm_netflow_monthly_report_get",{month:flowMonth}) as MonthPage).bytes,1500) + assert.equal((await invokeMcpTool(widePrincipal,"mm_netflow_talkers_get",{minutes:5}) as TalkerPage).retainedRowsBytes,3100) + assert.equal((await invokeMcpTool(disabled,"mm_netflow_talkers_get",{minutes:5}) as TalkerPage).retainedRowsBytes,300) + assert.equal((await invokeMcpTool(flowPrincipal,"mm_netflow_talkers_get",{serverOffset:10000}) as TalkerPage).total,0) + const empty = await authenticateMcpToken((await makeFlowAccess([])).token) + for(const tool of ["mm_netflow_talkers_get","mm_netflow_exporters_get","mm_netflow_clients_get","mm_netflow_monthly_report_get"]) await assert.rejects(invokeMcpTool(empty,tool,tool.includes("monthly")?{month:flowMonth}:{}),/сервер/) + assert.deepEqual((await invokeMcpTool(flowPrincipal,"mm_netflow_exporters_get",{serverOffset:10000}) as ExporterPage).exporters,[]) + assert.equal((await invokeMcpTool(flowPrincipal,"mm_netflow_clients_get",{serverOffset:10000}) as ClientPage).total,0) + assert.equal((await invokeMcpTool(flowPrincipal,"mm_netflow_monthly_report_get",{month:flowMonth,serverOffset:10000}) as MonthPage).bytes,0) + await pool.query("UPDATE flow_daily_dims SET bytes=9007199254740993 WHERE server_id=1 AND key='NF_US'") + await assert.rejects(invokeMcpTool(flowPrincipal,"mm_netflow_monthly_report_get",{month:flowMonth}),/счётчик/) + await pool.query("UPDATE flow_daily_dims SET bytes=1000 WHERE server_id=1 AND key='NF_US'") + assert.equal(catalogCalls,0,"Historical reports must not dispatch RouterOS requests") + const stored = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1",[flowAccess.connection.id])).rows + assert.ok(!JSON.stringify(stored).includes("NEVER_FLOW")) + await pool.query("DELETE FROM flow_daily_dims WHERE server_id=2 AND (key='NEVER_FLOW_HIDDEN_DIM' OR key LIKE 'NF_HIDDEN_%')") + await pool.query("UPDATE flow_buckets SET bytes=2000 WHERE server_id=2 AND src='198.18.242.7'"); clearPendingFlows() + const { default: FlowFastify } = await import("fastify"), { validatorCompiler: flowValidator, serializerCompiler: flowSerializer } = await import("@fastify/type-provider-zod") + const flowHttp = FlowFastify(); flowHttp.setValidatorCompiler(flowValidator); flowHttp.setSerializerCompiler(flowSerializer); await flowHttp.register((await import("../../routes/traffic-flow.js")).default,{prefix:"/api"}) + try { + const restTalkers = await flowHttp.inject({method:"GET",url:"/api/traffic/flow?range=5m"}) + assert.equal(restTalkers.statusCode,200,restTalkers.body) + const restVisible = restTalkers.json().talkers.filter((row:{serverId:string})=>row.serverId==="1") + const mcpVisible = await invokeMcpTool(flowPrincipal,"mm_netflow_talkers_get",{minutes:5}) as TalkerPage + assert.deepEqual(mcpVisible.items.map(row=>({bytes:row.bytes,bps:row.bps})),restVisible.map((row:{bytes:number;bps:number})=>({bytes:row.bytes,bps:row.bps}))) + 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("DELETE FROM flow_buckets WHERE server_id=1 AND src='198.18.242.11'") + const talkerRace = async (change:()=>Promise, principal=flowPrincipal, signal?:AbortSignal) => { + const blocker = await pool.connect() + let pending: Promise<{error:unknown}>|undefined + try { + await blocker.query("BEGIN"); await blocker.query("LOCK TABLE flow_buckets IN ACCESS EXCLUSIVE MODE") + pending = invokeMcpTool(principal,"mm_netflow_talkers_get",{minutes:5},signal).then(()=>({error:null}),error=>({error})) + let reached = false + for(let n=0;n<100;n++) { + reached = (await pool.query("SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query ILIKE '%FROM flow_buckets fb%') AS reached")).rows[0].reached + if(reached) break + await delay(20) + } + assert.equal(reached,true,"Talker race must block the actual scoped SQL read") + await change(); await blocker.query("COMMIT") + assert.ok((await pending).error,"Late cancellation/access/source drift must withhold all talker data") + } finally { await blocker.query("ROLLBACK"); blocker.release(); await pending } + } + const originalFlowName = (await pool.query("SELECT name FROM servers WHERE id=1")).rows[0].name + await talkerRace(()=>pool.query("UPDATE servers SET name='NEVER_FLOW_CHANGED_SOURCE' WHERE id=1")) + await pool.query("UPDATE servers SET name=$1 WHERE id=1",[originalFlowName]) + await talkerRace(async()=>{await pool.query("UPDATE traffic_flow_settings SET top_n=1 WHERE id=1");invalidateFlowSettings()}) + await pool.query("UPDATE traffic_flow_settings SET top_n=20 WHERE id=1");invalidateFlowSettings() + await talkerRace(()=>pool.query("UPDATE mcp_grants SET payload=jsonb_set(payload,'{scope,serverIds}','[]'::jsonb) WHERE id=$1",[grantId]),widePrincipal) + const originalFlowPermissions=(await pool.query("SELECT owner_permissions FROM mcp_connections WHERE id=$1",[flowAccess.connection.id])).rows[0].owner_permissions + await talkerRace(()=>pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1",[flowAccess.connection.id])) + await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb WHERE id=$1",[flowAccess.connection.id,JSON.stringify(originalFlowPermissions)]) + const talkerCancel=new AbortController() + await talkerRace(async()=>talkerCancel.abort(),flowPrincipal,talkerCancel.signal) + await talkerRace(()=>setConnectionStatus(flowAccess.connection.id,"paused",flowActor)) + 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") + } finally { + countCatalogCalls = false; clearPendingFlows(); resetFlowRipe() + await pool.query("DELETE FROM app_users WHERE id=ANY($1::text[])",[flowUsers]) + await pool.query("DELETE FROM flow_buckets WHERE src << '198.18.242.0/24'::inet AND server_id IN(1,2,42)") + await pool.query("DELETE FROM flow_daily_dims WHERE server_id IN(1,2,42) AND (key LIKE 'NF_%' OR key='NEVER_FLOW_HIDDEN_DIM' OR key='64512')") + await pool.query("DELETE FROM servers WHERE id=42") + await pool.query("UPDATE traffic_flow_settings SET peers_json=$1::jsonb,host_private_key=$2,top_n=$3 WHERE id=1",[JSON.stringify(savedFlow.peers_json),savedFlow.host_private_key,savedFlow.top_n]); invalidateFlowSettings(); invalidateFlowCatalog() + } + console.log("✓ NetFlow reports: scoped SQL before aggregation/parsing, hidden/mixed binding exclusion and grant narrowing, bounded pages/sources, future/pending/foreign filtering, disabled history, safe counters and real REST parity; no RouterOS dispatch") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl) VALUES(45,'gre-service-fixture','127.0.0.1',$1,false)",[port]) + const greServiceOperations = ["network.gre.create","network.gre.update","network.gre.delete"] + const greServiceAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GRE service plans",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[45],operationIds:greServiceOperations,allowGlobal:false},limits:{mutationsPerHour:200}}),actor) + const greServicePrincipal = await authenticateMcpToken(greServiceAccess.token) + const greNativeAccess=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GRE native aliases",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[45],operationIds:["ros.gre.update","ros.gre.delete"],allowGlobal:false},limits:{mutationsPerHour:100}}),actor) + const greNativePrincipal=await authenticateMcpToken(greNativeAccess.token) + const greNativePlan=async(operation:string,id:string,fields?:Record)=>invokeMcpTool(greNativePrincipal,"mm_gre_"+operation+"_plan",{serverId:45,id,...(fields?{fields}:{})})as Promise<{planId:string}> + const greNativeApply=async(planId:string)=>{await approvePlan(planId,actor,true);const key=randomUUID(),op=await applyPlan(greNativePrincipal,planId,key);return{key,op,state:await waitFirewallMove(op.id)}} + const greServicePlan = async(operation:string,args:Record)=>invokeMcpTool(greServicePrincipal,"mm_gre_tunnel_"+operation+"_plan",{serverId:45,...args}) as Promise<{planId:string}> + const greServiceApply = async(planId:string)=>{await approvePlan(planId,actor,true);const key=randomUUID(),op=await applyPlan(greServicePrincipal,planId,key);return{key,op,state:await waitFirewallMove(op.id)}} + const greServiceSecret = randomUUID(), greServiceProtection = randomUUID() + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,'gre-ipsec:45:gre-business',45,$2,$3)",[greServiceSecret,await encryptSecret("NEVER_GRE_SERVICE_PSK"),actor.id]) + greMutationFixtures=true;greMutationCommands=0;greMutationCollections={"/interface/gre":[],"/ip/address":[],"/ipv6/address":[]};greMutationDependencies={} + const {default:GreServiceFastify}=await import("fastify"),{validatorCompiler:greServiceValidator,serializerCompiler:greServiceSerializer}=await import("@fastify/type-provider-zod") + const greServiceHttp=GreServiceFastify();greServiceHttp.setValidatorCompiler(greServiceValidator);greServiceHttp.setSerializerCompiler(greServiceSerializer);await greServiceHttp.register((await import("../../routes/gre.js")).default,{prefix:"/api"}) + try { + const createFields={name:"gre-business",remoteAddress:"192.0.2.2",localInnerIp:"198.18.45.1/30",enabled:false} + await assert.rejects(invokeMcpTool(greServicePrincipal,"mm_gre_tunnel_create_plan",{serverId:1,fields:createFields}));assert.equal(greMutationCommands,0) + await assert.rejects(greServicePlan("create",{fields:{...createFields,ipsecSecret:"raw-not-permitted"}}));assert.equal(greMutationCommands,0) + await assert.rejects(greServicePlan("create",{fields:{...createFields,name:"wrong-secret-target"},ipsecSecretRef:greServiceSecret}));assert.equal(greMutationCommands,0) + const shortBudget=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GRE one-write budget",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[45],operationIds:greServiceOperations,allowGlobal:false},limits:{mutationsPerHour:1}}),actor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(shortBudget.token),"mm_gre_tunnel_create_plan",{serverId:45,fields:createFields}));assert.equal(greMutationCommands,0) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[greServiceProtection,JSON.stringify({kind:"vlan",id:"*protected",serverId:45,protection:"deny"}),actor.id]) + await assert.rejects(greServicePlan("create",{fields:createFields}));assert.equal(greMutationCommands,0) + assert.equal((await greServiceHttp.inject({method:"POST",url:"/api/gre/tunnels",payload:{serverId:"45",...createFields}})).statusCode,409);assert.equal(greMutationCommands,0) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[greServiceProtection]) + const createdPlan=await greServicePlan("create",{fields:createFields,ipsecSecretRef:greServiceSecret}),created=await greServiceApply(createdPlan.planId) + assert.equal(created.state,"applied");assert.equal(greMutationCommands,2);assert.equal(greMutationCollections["/interface/gre"][0].disabled,"true");assert.equal(greMutationCollections["/interface/gre"][0]["ipsec-secret"],"NEVER_GRE_SERVICE_PSK");assert.equal(greMutationCollections["/interface/gre"][0]["allow-fast-path"],"false") + await applyPlan(greServicePrincipal,createdPlan.planId,created.key);assert.equal(greMutationCommands,2) + const greId=greMutationCollections["/interface/gre"][0][".id"] + greMutationCollections["/interface/gre"][0]["dont-fragment"]="no" + const stale=await greServicePlan("update",{rosId:greId,fields:{mtu:1410}}) + greMutationCollections["/interface/gre"][0]["dont-fragment"]="yes" + assert.equal((await greServiceApply(stale.planId)).state,"rejected");assert.equal(greMutationCommands,2) + greMutationCollections["/interface/gre"][0]["dont-fragment"]="no" + greMutationCollections["/ip/address"].push({".id":"*SECOND",address:"198.18.99.1/32",interface:"gre-business",disabled:"false",dynamic:"false",invalid:"false",comment:"preserve-secondary"}) + const beforeNativeAddressCheck=greMutationCommands + await assert.rejects(greNativePlan("delete",greId),/адресом/) + await assert.rejects(greNativePlan("update",greId,{name:"native-bound-rename"}),/адресом/) + assert.equal(greMutationCommands,beforeNativeAddressCheck) + const changed=await greServiceApply((await greServicePlan("update",{rosId:greId,fields:{mtu:1400,localInnerIp:"fd00:45::1/64",enabled:true}})).planId) + assert.equal(changed.state,"applied");assert.equal(greMutationCollections["/interface/gre"][0].disabled,"false");assert.equal(greMutationCollections["/interface/gre"][0]["dont-fragment"],"no");assert.equal(greMutationCollections["/interface/gre"][0]["ipsec-secret"],"NEVER_GRE_SERVICE_PSK") + assert.equal(greMutationCollections["/ip/address"].length,1);assert.equal(greMutationCollections["/ip/address"][0][".id"],"*SECOND");assert.equal(greMutationCollections["/ipv6/address"][0].address,"fd00:45::1/64");assert.equal(greMutationCollections["/ipv6/address"][0].advertise,"false") + await assert.rejects(greServicePlan("update",{rosId:greId,name:"different",fields:{mtu:1420}})) + greMutationDependencies["/ip/route"]=[{".id":"*USE",gateway:"192.0.2.2%gre-business",dynamic:"false"}] + await assert.rejects(greServicePlan("delete",{rosId:greId}),/используется/);greMutationDependencies={} + await pool.query("INSERT INTO app_users(id,login,name) VALUES('gre-business-user','gre-business-user','GRE binding fixture')") + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name) VALUES($1,'gre-business-user',45,'gre-business')",[randomUUID()]) + await assert.rejects(greServicePlan("delete",{rosId:greId}),/привязкой/);await pool.query("DELETE FROM app_users WHERE id='gre-business-user'") + const removed=await greServiceApply((await greServicePlan("delete",{rosId:greId})).planId);assert.equal(removed.state,"applied");assert.equal(greMutationCollections["/interface/gre"].length,0);assert.equal(greMutationCollections["/ip/address"].length,0);assert.equal(greMutationCollections["/ipv6/address"].length,0) + for(const [method,payload,status] of [["POST",{serverId:"45",name:"rest-gre",remoteAddress:"192.0.2.3",localInnerIp:"fd00:46::1/64",enabled:false},201],["PATCH",{serverId:"45",name:"rest-gre",mtu:1400},200],["DELETE",{serverId:"45",name:"rest-gre"},200]] as const){const response=await greServiceHttp.inject({method,url:"/api/gre/tunnels",payload});assert.equal(response.statusCode,status,response.body);assert.equal(response.json().mutation.verified,true)} + assert.equal((await greServiceApply((await greServicePlan("create",{fields:{name:"native-gre",remoteAddress:"192.0.2.4",enabled:false,comment:"native note"}})).planId)).state,"applied") + const nativeId=greMutationCollections["/interface/gre"][0][".id"],nativeBefore=greMutationCommands + greMutationCollections["/interface/gre"][0]["dont-fragment"]="no" + await pool.query("INSERT INTO app_users(id,login,name) VALUES('gre-native-user','gre-native-user','Native GRE binding fixture')") + const nativeBindingId=randomUUID() + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name) VALUES($1,'gre-native-user',45,'native-gre')",[nativeBindingId]) + await assert.rejects(greNativePlan("delete",nativeId),/привязкой/);await assert.rejects(greNativePlan("update",nativeId,{name:"native-renamed"}),/привязкой/) + const {executeRosStep:directGreStep}=await import("./resources.js") + await assert.rejects(directGreStep({serverId:45,resource:"gre",action:"delete",id:nativeId,fields:{}}),/привязкой/) + await assert.rejects(directGreStep({serverId:45,resource:"gre",action:"update",id:nativeId,fields:{name:"bypass-rename"}}),/привязкой/) + assert.equal(greMutationCommands,nativeBefore) + const scalarNative=await greNativeApply((await greNativePlan("update",nativeId,{mtu:1395})).planId);assert.equal(scalarNative.state,"applied") + await pool.query("DELETE FROM user_interface_bindings WHERE id=$1",[nativeBindingId]) + const lateNative=await greNativePlan("delete",nativeId),beforeLateNative=greMutationCommands + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name) VALUES($1,'gre-native-user',45,'native-gre')",[nativeBindingId]) + assert.equal((await greNativeApply(lateNative.planId)).state,"rejected");assert.equal(greMutationCommands,beforeLateNative) + await pool.query("DELETE FROM app_users WHERE id='gre-native-user'") + const dynamicNative=await greNativePlan("delete",nativeId),beforeDynamicNative=greMutationCommands + // Apply reads the sealed current state and execution's initial state first; + // introduce the learned route only in the final pre-dispatch read. + greNativeLateDynamicAt=greNativeRouteReads+3 + assert.equal((await greNativeApply(dynamicNative.planId)).state,"unknown");assert.equal(greMutationCommands,beforeDynamicNative) + greNativeLateDynamicAt=0;greMutationDependencies["/ip/route"]=[] + greMutationDependencies["/ip/firewall/nat"]=[{".id":"*FWUSE",chain:"srcnat",action:"masquerade","out-interface":"native-gre",packets:"1",bytes:"100"}] + await assert.rejects(greNativePlan("delete",nativeId),/используется/);await assert.rejects(greNativePlan("update",nativeId,{name:"native-renamed"}),/используется/) + await assert.rejects(directGreStep({serverId:45,resource:"gre",action:"delete",id:nativeId,fields:{}}),/используется/) + greMutationDependencies["/ip/firewall/nat"]=[] + await assert.rejects(greNativePlan("update",nativeId,{name:"ether1"}),/уже используется/) + const nativeOpaque=await greNativePlan("update",nativeId,{mtu:1390}),beforeOpaque=greMutationCommands + greMutationCollections["/interface/gre"][0]["dont-fragment"]="yes" + assert.equal((await greNativeApply(nativeOpaque.planId)).state,"rejected");assert.equal(greMutationCommands,beforeOpaque) + greMutationCollections["/interface/gre"][0]["dont-fragment"]="no" + greMutationDependencies["/ip/firewall/filter"]=[{".id":"*COUNTER",chain:"forward",action:"accept","in-interface":"ether1",packets:"1",bytes:"100"}];greNativeCounterTick=true + const renamed=await greNativeApply((await greNativePlan("update",nativeId,{name:"native-renamed",comment:""})).planId);assert.equal(renamed.state,"applied");assert.equal(greMutationCollections["/interface/gre"][0].name,"native-renamed");assert.equal(greMutationCollections["/interface/gre"][0].comment,"");assert.equal(greMutationCollections["/interface/gre"][0]["dont-fragment"],"no") + const ignoredNative=await greNativePlan("update",nativeId,{mtu:1385}),beforeIgnoredNative=greMutationCommands;greMutationIgnored=true + assert.equal((await greNativeApply(ignoredNative.planId)).state,"partial");assert.equal(greMutationCommands,beforeIgnoredNative+1);greMutationIgnored=false + const lostNative=await greNativePlan("update",nativeId,{mtu:1380}),beforeLostNative=greMutationCommands;greMutationLost=true + const nativeLostResult=await greNativeApply(lostNative.planId);assert.equal(nativeLostResult.state,"unknown");assert.equal(greMutationCommands,beforeLostNative+1) + await applyPlan(greNativePrincipal,lostNative.planId,nativeLostResult.key);assert.equal(greMutationCommands,beforeLostNative+1) + const nativeDeleteResult = await greNativeApply((await greNativePlan("delete",nativeId)).planId) + assert.equal(nativeDeleteResult.state,"applied",JSON.stringify((await pool.query("SELECT state,error_code,result FROM mcp_operations WHERE id=$1",[nativeDeleteResult.op.id])).rows[0]));assert.equal(greMutationCollections["/interface/gre"].length,0) + greNativeCounterTick=false;greMutationDependencies={} + console.log("✓ Native GRE aliases and direct execution: sealed DB/firewall/opaque proof, user/address reference rejection, rename and omitted empty-comment read-back, late binding rejection, scalar update, counter tolerance, partial/lost and no replay") + await acceptGreRevisionRestore(actor,{get:()=>greMutationCollections,set:value=>{greMutationCollections=value},writes:()=>greMutationCommands,reads:()=>greRevisionReads,ignored:value=>{greMutationIgnored=value},lost:value=>{greMutationLost=value},afterCommand:value=>{greRevisionAfterCommand=value},dependencies:value=>{greMutationDependencies=value}}) + const ignored=await greServicePlan("create",{fields:{...createFields,name:"ignored-gre"}}),beforeIgnored=greMutationCommands;greMutationIgnored=true + assert.equal((await greServiceApply(ignored.planId)).state,"partial");assert.equal(greMutationCommands,beforeIgnored+1);greMutationIgnored=false + const lost=await greServicePlan("create",{fields:{...createFields,name:"lost-gre"}}),beforeLost=greMutationCommands;greMutationLost=true + const lostResult=await greServiceApply(lost.planId);assert.equal(lostResult.state,"unknown");assert.equal(greMutationCommands,beforeLost+1);assert.equal(greMutationCollections["/interface/gre"][0].disabled,"true") + await applyPlan(greServicePrincipal,lost.planId,lostResult.key);assert.equal(greMutationCommands,beforeLost+1) + greMutationCollections={"/interface/gre":[],"/ip/address":[],"/ipv6/address":[]} + const revoked=await greServicePlan("create",{fields:{...createFields,name:"revoked-gre"}}),beforeRevoke=greMutationCommands;greMutationRevoke=greServiceAccess.connection.id + assert.equal((await greServiceApply(revoked.planId)).state,"unknown");assert.equal(greMutationCommands,beforeRevoke+1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%NEVER_GRE_SERVICE_PSK%'",[greServiceAccess.connection.id])).rows[0].n,0) + } finally {greMutationFixtures=false;greMutationIgnored=false;greMutationLost=false;greMutationRevoke=undefined;greNativeCounterTick=false;greNativeLateDynamicAt=0;await greServiceHttp.close();await pool.query("DELETE FROM app_users WHERE id IN('gre-business-user','gre-native-user')");await pool.query("DELETE FROM mcp_protections WHERE id=$1",[greServiceProtection]);await pool.query("DELETE FROM mcp_secrets WHERE id=$1",[greServiceSecret]);await pool.query("DELETE FROM servers WHERE id=45")} + console.log("✓ GRE shared REST/MCP business CRUD: scoped native identity/proof, staged disabled v4/v6, preserved secondary/opaque/PSK, secret purpose, server-wide protections, parent/user dependencies, budget, stale rejection, partial/lost/no replay and revoke between commands") + const vpnAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped VPN catalogs", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["network.wireguard.state.read", "network.vxlan.state.read", "network.ipsec.state.read", "network.gre.state.read"], allowGlobal: false } }), actor) + const vpnPrincipal = await authenticateMcpToken(vpnAccess.token) + vpnFixtures = true; vpnCalls.length = 0 + try { + type WgPage = { interfaces: { enabled: boolean | null; status: string; address: string | null; addressStateVerified: boolean; peers: { rosId: string; transferRx: number | null; transferTx: number | null }[]; peersPage: { total: number; nextOffset: number | null } }[]; total: number; failures: unknown[]; incomplete: boolean; keyMaterialIncluded: boolean } + const wgPage = await invokeMcpTool(vpnPrincipal, "mm_wireguard_state_get", { limit: 1, peerLimit: 1 }) as WgPage + assert.equal(vpnCalls.length, 4); assert.deepEqual(wgPage.failures, []); assert.equal(wgPage.total, 2); assert.equal(wgPage.incomplete, true) + assert.equal(wgPage.interfaces[0].peersPage.total, 3); assert.equal(wgPage.interfaces[0].peersPage.nextOffset, 1) + assert.equal(wgPage.interfaces[0].peers[0].transferRx, 0); assert.equal(wgPage.interfaces[0].peers[0].transferTx, null) + assert.equal(wgPage.interfaces[0].address, "192.0.2.1/24"); assert.equal(wgPage.interfaces[0].addressStateVerified, true) + assert.equal(wgPage.keyMaterialIncluded, false); assert.ok(!JSON.stringify(wgPage).includes(vpnPrivate)); assert.ok(!JSON.stringify(wgPage).includes(vpnPsk)) + const wgNext = await invokeMcpTool(vpnPrincipal, "mm_wireguard_state_get", { limit: 1, peerOffset: 1, peerLimit: 1 }) as WgPage + assert.equal(wgNext.interfaces[0].peers[0].rosId, "*P2") + const prefixes = await invokeMcpTool(vpnPrincipal, "mm_wireguard_state_get", { limit: 1, peerLimit: 1, allowedOffset: 1, allowedLimit: 1 }) as { interfaces: { peers: { allowedIps: string[]; allowedIpsPage: { total: number; nextOffset: number } }[] }[]; incomplete: boolean } + assert.deepEqual(prefixes.interfaces[0].peers[0].allowedIps, ["198.51.100.0/24"]); assert.equal(prefixes.interfaces[0].peers[0].allowedIpsPage.total, 3); assert.equal(prefixes.interfaces[0].peers[0].allowedIpsPage.nextOffset, 2); assert.equal(prefixes.incomplete, true) + const wgUnknown = await invokeMcpTool(vpnPrincipal, "mm_wireguard_state_get", { offset: 1 }) as WgPage + assert.equal(wgUnknown.interfaces[0].enabled, null); assert.equal(wgUnknown.interfaces[0].status, "unknown") + const beforeGre = vpnCalls.length + type GrePage = { tunnels: { status: string; mtu: number | null; comment: string; localAddresses: { address: string; configurationVerified: boolean }[]; addressesPage: { total: number; nextOffset: number | null } }[]; failures: unknown[]; incomplete: boolean; configurationHistoryUpdated: boolean } + const historyBeforeGre = Number((await pool.query("SELECT count(*) AS n FROM config_revisions WHERE server_id=1 AND section='gre'")).rows[0].n) + const gre = await invokeMcpTool(vpnPrincipal, "mm_gre_state_get", { limit: 1, addressLimit: 1 }) as GrePage + assert.equal(vpnCalls.length, beforeGre + 3); assert.deepEqual(gre.failures, []); assert.equal(gre.tunnels[0].status, "up"); assert.equal(gre.tunnels[0].mtu, 1400) + assert.equal(gre.tunnels[0].addressesPage.total, 3); assert.equal(gre.tunnels[0].addressesPage.nextOffset, 1); assert.equal(gre.incomplete, true) + assert.equal(gre.tunnels[0].localAddresses[0].configurationVerified, true); assert.equal(gre.configurationHistoryUpdated, false); assert.ok(!JSON.stringify(gre).includes(vpnPsk)) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM config_revisions WHERE server_id=1 AND section='gre'")).rows[0].n), historyBeforeGre) + const greNext = await invokeMcpTool(vpnPrincipal, "mm_gre_state_get", { limit: 1, addressOffset: 1, addressLimit: 1 }) as GrePage + assert.equal(greNext.tunnels[0].localAddresses[0].address, "fd00::2/64") + const greUnknown = await invokeMcpTool(vpnPrincipal, "mm_gre_state_get", { offset: 1 }) as GrePage + assert.equal(greUnknown.tunnels[0].status, "unknown"); assert.equal(greUnknown.tunnels[0].mtu, null) + const beforeVxlan = vpnCalls.length + type VxPage = { tunnels: { remoteVteps: string[]; vtepsPage: { total: number; nextOffset: number | null } }[]; failures: unknown[]; incomplete: boolean } + const vx = await invokeMcpTool(vpnPrincipal, "mm_vxlan_state_get", { vtepLimit: 2 }) as VxPage + assert.equal(vpnCalls.length, beforeVxlan + 2); assert.deepEqual(vx.failures, []); assert.equal(vx.tunnels[0].vtepsPage.total, 3); assert.equal(vx.tunnels[0].vtepsPage.nextOffset, 2); assert.equal(vx.incomplete, true) + const vxNext = await invokeMcpTool(vpnPrincipal, "mm_vxlan_state_get", { vtepOffset: 2, vtepLimit: 2 }) as VxPage + assert.deepEqual(vxNext.tunnels[0].remoteVteps, ["198.51.100.3"]) + const beforeIpsec = vpnCalls.length + type IpsecPage = { clients: { disabled: boolean | null; online: boolean | null }[]; servers: { clientsOnline: number | null; clientsSessionUnknown: number; peers: { disabled: boolean | null; passive: boolean | null }[]; certs: { trusted: boolean | null; keyPairAvailable: boolean | null; expiryTimestampVerified: boolean }[]; certificatesPage: { total: number; nextOffset: number | null } }[]; failures: unknown[]; incomplete: boolean } + const ike = await invokeMcpTool(vpnPrincipal, "mm_ipsec_state_get", { detailLimit: 1 }) as IpsecPage + assert.equal(vpnCalls.length, beforeIpsec + 8); assert.deepEqual(ike.failures, []) + assert.equal(ike.clients[0].disabled, null); assert.equal(ike.clients[0].online, true); assert.equal(ike.servers[0].clientsOnline, 1) + assert.equal(ike.servers[0].peers[0].disabled, null); assert.equal(ike.servers[0].peers[0].passive, true) + assert.equal(ike.servers[0].certificatesPage.total, 2); assert.equal(ike.servers[0].certificatesPage.nextOffset, 1) + assert.equal(ike.servers[0].certs[0].trusted, false); assert.equal(ike.servers[0].certs[0].keyPairAvailable, true); assert.equal(ike.servers[0].certs[0].expiryTimestampVerified, false) + assert.ok(!JSON.stringify(ike).includes(vpnPsk)) + vpnAmbiguous = true + const ambiguousIke = await invokeMcpTool(vpnPrincipal, "mm_ipsec_state_get", {}) as IpsecPage + assert.ok(ambiguousIke.clients.every(row => row.online === null)); assert.equal(ambiguousIke.servers[0].clientsOnline, null); assert.equal(ambiguousIke.servers[0].clientsSessionUnknown, 2) + vpnAmbiguous = false; vpnUnknownSession = true + assert.equal((await invokeMcpTool(vpnPrincipal, "mm_ipsec_state_get", {}) as IpsecPage).clients[0].online, null); vpnUnknownSession = false + const beforeDenied = vpnCalls.length + for (const tool of ["mm_wireguard_state_get", "mm_vxlan_state_get", "mm_ipsec_state_get", "mm_gre_state_get"]) await assert.rejects(invokeMcpTool(vpnPrincipal, tool, { serverId: 2 })) + assert.equal(vpnCalls.length, beforeDenied) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + for (const tool of ["mm_wireguard_state_get", "mm_vxlan_state_get", "mm_ipsec_state_get", "mm_gre_state_get"]) assert.equal((await invokeMcpTool(vpnPrincipal, tool, {}) as { totalServers: number }).totalServers, 0) + assert.equal(vpnCalls.length, beforeDenied); await pool.query("UPDATE servers SET enabled=true WHERE id=1") + for (const [tool, path] of [["mm_gre_state_get", "/rest/interface/gre"], ["mm_gre_state_get", "/rest/ip/address"], ["mm_gre_state_get", "/rest/ipv6/address"], ["mm_wireguard_state_get", "/rest/interface/wireguard"], ["mm_vxlan_state_get", "/rest/interface/vxlan/vteps"], ["mm_ipsec_state_get", "/rest/ip/ipsec/identity"]]) { + vpnFailurePath = path + const failed = await invokeMcpTool(vpnPrincipal, tool, {}) as { failures: unknown[]; incomplete: boolean } + assert.equal(failed.failures.length, 1); assert.equal(failed.incomplete, true) + } + vpnFailurePath = ""; vpnDuplicate = true + assert.equal((await invokeMcpTool(vpnPrincipal, "mm_wireguard_state_get", {}) as WgPage).failures.length, 1) + assert.equal((await invokeMcpTool(vpnPrincipal, "mm_gre_state_get", {}) as GrePage).failures.length, 1) + const results = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [vpnAccess.connection.id])).rows + assert.ok(!JSON.stringify(results).includes(vpnPsk)); assert.ok(!JSON.stringify(results).includes(vpnPrivate)) + } finally { + vpnFixtures = false; vpnFailurePath = ""; vpnAmbiguous = false; vpnUnknownSession = false; vpnDuplicate = false + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + } + console.log("✓ VPN catalogs share REST polling/models: 3 GRE/4 WG/2 VXLAN/8 IPsec scoped GETs, nested paging, unknown flags/counters/sessions, keys and reflected credentials masked, failures/duplicates and disabled/hidden sources preserved") + await (await import("../../test/mcp-routing-sources.js")).verifyRoutingSources(actor) + let analysisPeerReads = 0 + let analysisPeerInstances = [{ ".id": "*peer-ospf", name: "peer-ospf", "router-id": "192.0.2.2", disabled: "false", version: "2", vrf: "main" }] + let analysisPeerNeighbors: Record[] = [{ ".id": "*pn", instance: "peer-ospf", interface: "peer", "router-id": "192.0.2.1", address: "192.0.2.1", state: "Full" }] + let analysisPeerAddresses = [{ ".id": "*pa", interface: "peer", address: "192.0.2.2/30", disabled: "false", invalid: "false" }] + const analysisPeer = createServer((req, res) => { analysisPeerReads++; if (ospfAnalysisFixtures) ospfAnalysisNativeRequests++; res.setHeader("content-type", "application/json"); res.end(JSON.stringify(req.url === "/rest/routing/ospf/neighbor" ? analysisPeerNeighbors : req.url === "/rest/ip/address" ? analysisPeerAddresses : analysisPeerInstances)) }) + await new Promise(resolve => analysisPeer.listen(0, "127.0.0.1", resolve)) + const analysisAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Observer OSPF analysis", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1, 2], operationIds: ["routing.ospf.analyze"], allowGlobal: false } }), actor) + const analysisPrincipal = await authenticateMcpToken(analysisAccess.token), observationId = randomUUID(), badObservationId = randomUUID() + ospfAnalysisFixtures = true; ospfAnalysisDispatches = 0 + await pool.query("UPDATE servers SET port=$1 WHERE id=2", [(analysisPeer.address() as { port: number }).port]) + await pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source) VALUES($1,1,2,'fixture-gre','peer','ipv4','main',12,0,100,200,now(),now(),true,'fixture')", [observationId]) + await attestRoutingFixture(observationId) + try { + type OspfAnalysis = { ranking: { currentCost: number; proposedCost: number; observationId: string; bandwidthObservationId: string | null; peerId: number }[]; serverIds: number[]; changes: number; proposalOnly: boolean; executionAvailable: boolean } + const applicationPreviewPolicy={name:"OSPF application preview",mode:"observer",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:read"],scope:{serverIds:[1,2],operationIds:["routing.ospf.application.preview"],allowGlobal:false}} + const applicationPreviewAccess=await createMcpConnection(mcpConnectionCreateSchema.parse(applicationPreviewPolicy),actor),applicationPreviewPrincipal=await authenticateMcpToken(applicationPreviewAccess.token) + const isolatedPreviewAccess=await createMcpConnection(mcpConnectionCreateSchema.parse({...applicationPreviewPolicy,name:"OSPF source-only preview",scope:{...applicationPreviewPolicy.scope,serverIds:[1]}}),actor),isolatedPreviewPrincipal=await authenticateMcpToken(isolatedPreviewAccess.token) + const {createOspfRoutes}=await import("../../routes/ospf.js"),{ensurePartitionFor:ensurePreviewPartition}=await import("../../db/partitions.js") + const previewHttp=FilterFastify();previewHttp.setValidatorCompiler(filterValidator);previewHttp.setSerializerCompiler(filterSerializer);await previewHttp.register(createOspfRoutes(),{prefix:"/api"}) + const previewProbeId=randomUUID(),previewTrafficTimes=[0,1,2].map(delta=>new Date(Date.now()-10000+delta).toISOString()),templatesBeforePreview=structuredClone(fixtureOspf["interface-template"]) + type ApplicationPreview={interfacesTotal:number;proposalOnly:boolean;executionAvailable:boolean;metricContextVerified:boolean;interfaces:{items:{currentCost:number;modeled:{pingMs:number;dlMbps:number;ulMbps:number;score:number;optimalCost:number};observed:{ping:{value:number|null;sourceId:string|null};dl:{value:number|null};ul:{value:number|null}}}[];total:number;nextOffset:number|null};changes:{total:number};allowedMetricServerIds:number[]} + try{ + await pool.query("INSERT INTO uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,last_ping_rtt_ms,last_ping_at,last_tx_avg_mbps,last_rx_avg_mbps,last_error) VALUES($1,1,2,'fixture-gre',0,now(),0,0,'NEVER_OSPF_PREVIEW_ERROR')",[previewProbeId]) + const beforePeer=analysisPeerReads,plansBeforePreview=(await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n + const isolated=await invokeMcpTool(isolatedPreviewPrincipal,"mm_ospf_application_preview",{serverId:1}) as ApplicationPreview + assert.equal(analysisPeerReads,beforePeer);assert.equal(isolated.interfaces.items[0].observed.ping.value,null);assert.equal(isolated.interfaces.items[0].observed.ping.sourceId,null) + assert.deepEqual(isolated.allowedMetricServerIds,[1]);assert.equal(JSON.stringify(isolated).includes(previewProbeId),false) + await assert.rejects(invokeMcpTool(isolatedPreviewPrincipal,"mm_ospf_application_preview",{serverId:1,peerServerIds:[2]}));assert.equal(analysisPeerReads,beforePeer) + const application=await invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1,limit:1}) as ApplicationPreview + assert.ok(analysisPeerReads>beforePeer);assert.equal(application.interfaces.items[0].observed.ping.value,0);assert.equal(application.interfaces.items[0].observed.dl.value,0);assert.equal(application.interfaces.items[0].observed.ul.value,0) + assert.equal(application.interfaces.items[0].modeled.pingMs,1);assert.equal(application.interfaces.items[0].modeled.dlMbps,1);assert.equal(application.metricContextVerified,false);assert.equal(application.proposalOnly,true);assert.equal(application.executionAvailable,false) + const restPreview=await previewHttp.inject({method:"POST",url:"/api/servers/1/ospf/optimize/preview",payload:{pingWeight:60}}) + assert.equal(restPreview.statusCode,200,restPreview.body) + assert.deepEqual(application.interfaces.items[0].modeled,{pingMs:restPreview.json().interfaces[0].pingMs,dlMbps:restPreview.json().interfaces[0].dlMbps,ulMbps:restPreview.json().interfaces[0].ulMbps,score:restPreview.json().interfaces[0].score,optimalCost:restPreview.json().interfaces[0].optimalCost}) + const {readLegacyOspfSealedSources}=await import("./ospf-legacy-source.js") + const {MikrotikClient}=await import("../../services/mikrotik.js") + const legacyServer=(await (await import("./resources.js")).routerClient(1)).server + let legacySourceGates=0 + const legacyOptions={serverIds:[1,2],authorize:async()=>{legacySourceGates++},signal:AbortSignal.timeout(30000)} + const legacySealed=await readLegacyOspfSealedSources(legacyServer,60,legacyOptions) + assert.deepEqual(legacySealed.plan.interfaces,restPreview.json().interfaces) + assert.ok(legacySourceGates>10);assert.ok(legacySealed.sourceCount>5) + assert.equal((await readLegacyOspfSealedSources(legacyServer,60,legacyOptions)).proof,legacySealed.proof) + const legacyFirst=legacySealed.plan.interfaces.find(row=>row.currentCost!==row.optimalCost)! + assert.ok(legacyFirst) + const legacyNativeCost=fixtureOspf["interface-template"].find(row=>row[".id"]===legacyFirst.id)! + const legacyOriginalCost=legacyNativeCost.cost + try{ + legacyNativeCost.cost=String(legacyFirst.optimalCost) + const prefix=await readLegacyOspfSealedSources(legacyServer,60,legacyOptions,{baseline:legacySealed.plan,completedCount:1}) + assert.equal(prefix.proof,legacySealed.proof) + await assert.rejects(readLegacyOspfSealedSources(legacyServer,60,legacyOptions,{baseline:legacySealed.plan,completedCount:0}),/не подтверждены/) + }finally{legacyNativeCost.cost=legacyOriginalCost} + const legacyProbeMetric=(await pool.query("SELECT last_ping_rtt_ms FROM uptime_speed_probes WHERE id=$1",[previewProbeId])).rows[0].last_ping_rtt_ms + try{ + await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=last_ping_rtt_ms+1 WHERE id=$1",[previewProbeId]) + assert.notEqual((await readLegacyOspfSealedSources(legacyServer,60,legacyOptions)).proof,legacySealed.proof) + }finally{await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=$2 WHERE id=$1",[previewProbeId,legacyProbeMetric])} + const legacyIntentBefore=(await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent'")).rows[0].n + const legacyDispatchBefore=ospfAnalysisDispatches + let legacyDispatchGate=0 + try{ + await assert.rejects(MikrotikClient.fromServer(legacyServer).setOspfInterfaceTemplateCost(legacyFirst.id,legacyFirst.optimalCost,legacyFirst.currentCost,undefined,async()=>{ + legacyDispatchGate++ + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent'")).rows[0].n,legacyIntentBefore+1) + await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=last_ping_rtt_ms+1 WHERE id=$1",[previewProbeId]) + if((await readLegacyOspfSealedSources(legacyServer,60,legacyOptions)).proof!==legacySealed.proof)throw Error("NEVER_PUBLISH_LEGACY_SOURCE_DRIFT") + }),error=>error instanceof Error&&"writeAttempted"in error&&error.writeAttempted===false&&!error.message.includes("NEVER_PUBLISH")) + assert.equal(legacyDispatchGate,1);assert.equal(ospfAnalysisDispatches,legacyDispatchBefore) + assert.equal(legacyNativeCost.cost,legacyOriginalCost) + }finally{await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=$2 WHERE id=$1",[previewProbeId,legacyProbeMetric])} + console.log("✓ Legacy OSPF native dispatch gate: SQL drift after durable configuration.intent rejected before PATCH, attempted=false and no provider/source error leakage") + const {executeSealedLegacyOspf,reconcileSealedLegacyOspf,LegacyOspfExecutionError}=await import("./ospf-legacy-execution.js") + const legacyExecutionBaseline=structuredClone(fixtureOspf["interface-template"]),legacyExecutionDispatches=ospfAnalysisDispatches + try{ + fixtureOspf["interface-template"].push({...legacyExecutionBaseline[0],".id":"*secondary",interfaces:"fixture-other",cost:"40"}) + const executionBaseline=structuredClone(fixtureOspf["interface-template"]) + const readExecution=()=>readLegacyOspfSealedSources(legacyServer,60,{...legacyOptions,signal:AbortSignal.timeout(45000)}) + const executionOptions={...legacyOptions,signal:AbortSignal.timeout(45000)} + const sealed=await readExecution() + assert.equal(sealed.plan.interfaces.length,2) + assert.deepEqual(await reconcileSealedLegacyOspf(legacyServer,sealed,executionOptions),{matchesOriginalState:true,matchesDesiredState:false,confirmedPrefix:0}) + const successBefore=ospfAnalysisDispatches + const receipt=await executeSealedLegacyOspf(legacyServer,sealed,executionOptions) + assert.equal(receipt.optimizedCount,2);assert.equal(ospfAnalysisDispatches,successBefore+2) + assert.deepEqual(await reconcileSealedLegacyOspf(legacyServer,sealed,executionOptions),{matchesOriginalState:false,matchesDesiredState:true,confirmedPrefix:2}) + assert.equal(ospfAnalysisDispatches,successBefore+2) + fixtureOspf["interface-template"]=structuredClone(executionBaseline) + await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=last_ping_rtt_ms+1 WHERE id=$1",[previewProbeId]) + const staleBefore=ospfAnalysisDispatches + await assert.rejects(executeSealedLegacyOspf(legacyServer,sealed,executionOptions),error=>error instanceof LegacyOspfExecutionError&&error.state==="rejected") + assert.equal(ospfAnalysisDispatches,staleBefore) + await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=$2 WHERE id=$1",[previewProbeId,legacyProbeMetric]) + for(const fault of ["ignored","lost-response","changed-field"] as const){ + fixtureOspf["interface-template"]=structuredClone(executionBaseline) + ospfCostWriteFault=fault + const before=ospfAnalysisDispatches + await assert.rejects(executeSealedLegacyOspf(legacyServer,sealed,{...executionOptions,signal:AbortSignal.timeout(45000)}),error=>error instanceof LegacyOspfExecutionError&&error.state==="unknown"&&!error.message.includes("NEVER_PUBLISH")) + assert.equal(ospfAnalysisDispatches,before+1,"An uncertain first write must stop the second and must not be retried") + ospfCostWriteFault="none" + const reconciled=await reconcileSealedLegacyOspf(legacyServer,sealed,{...executionOptions,signal:AbortSignal.timeout(45000)}) + assert.equal(reconciled.matchesDesiredState,false) + if(fault==="ignored")assert.equal(reconciled.matchesOriginalState,true) + if(fault==="lost-response")assert.equal(reconciled.confirmedPrefix,1) + if(fault==="changed-field")assert.equal(reconciled.confirmedPrefix,null) + assert.equal(ospfAnalysisDispatches,before+1,"Reconciliation must not dispatch any mutation") + } + }finally{ + fixtureOspf["interface-template"]=legacyExecutionBaseline;ospfAnalysisDispatches=legacyExecutionDispatches;ospfCostWriteFault="none" + await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=$2 WHERE id=$1",[previewProbeId,legacyProbeMetric]) + } + console.log("✓ Legacy OSPF sealed executor: two sequential cost PATCHes, full source checks before dispatch/after read-back, stale zero-write refusal, ignored/lost/collateral stop after one command, private errors and readonly prefix reconciliation; local SQL/HTTP only") + const legacyBeforeDenied=analysisPeerReads + await assert.rejects(readLegacyOspfSealedSources(legacyServer,60,{...legacyOptions,authorize:async()=>{throw Error("legacy access revoked")}}),/legacy access revoked/) + await assert.rejects(readLegacyOspfSealedSources(legacyServer,60,{...legacyOptions,signal:AbortSignal.abort()})) + await assert.rejects(readLegacyOspfSealedSources(legacyServer,60,{...legacyOptions,deadlineAt:Date.now()-1})) + assert.equal(analysisPeerReads,legacyBeforeDenied) + countCatalogCalls=true + const legacyBeforeStaleSource=catalogCalls + try{ + await assert.rejects(readLegacyOspfSealedSources({...legacyServer,password:"stale-client-credential"},60,{...legacyOptions,signal:AbortSignal.timeout(45000)}),/не подтверждены/) + assert.equal(catalogCalls,legacyBeforeStaleSource) + }finally{countCatalogCalls=false} + console.log("✓ Legacy OSPF sealed sources: actual SQL/native REST rank parity, stable private proof, confirmed cost prefix, wrong prefix refusal and pre-source access denial; no native mutation") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n,plansBeforePreview) + for(const time of previewTrafficTimes)await ensurePreviewPartition(pool,"traffic_samples","week",new Date(time)) + await pool.query("UPDATE uptime_speed_probes SET last_tx_avg_mbps=NULL,last_rx_avg_mbps=NULL WHERE id=$1",[previewProbeId]) + await pool.query("INSERT INTO traffic_samples(server_id,interface_name,peer_public_key,sampled_at,rx_bps,tx_bps,flags) VALUES(1,'fixture-gre','',$1,0,0,61),(1,'fixture-gre','NEVER_OSPF_PEER_KEY',$2,99000000,99000000,61)",[previewTrafficTimes[0],previewTrafficTimes[1]]) + const measuredZero=await invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1}) as ApplicationPreview + assert.equal(measuredZero.interfaces.items[0].observed.dl.value,0);assert.equal(measuredZero.interfaces.items[0].observed.ul.value,0) + await pool.query("INSERT INTO traffic_samples(server_id,interface_name,sampled_at,rx_bps,tx_bps,flags) VALUES(1,'fixture-gre',$1,0,0,36)",[previewTrafficTimes[2]]) + const unknownRate=await invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1}) as ApplicationPreview + assert.equal(unknownRate.interfaces.items[0].observed.dl.value,null);assert.equal(unknownRate.interfaces.items[0].observed.ul.value,null);assert.equal(unknownRate.interfaces.items[0].modeled.dlMbps,10) + await pool.query("UPDATE traffic_samples SET flags=0,rx_bps=40000000 WHERE server_id=1 AND interface_name='fixture-gre' AND sampled_at=$1",[previewTrafficTimes[2]]) + assert.equal((await invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1}) as ApplicationPreview).interfaces.items[0].observed.dl.value,null) + await pool.query("UPDATE uptime_speed_probes SET enabled=false WHERE id=$1",[previewProbeId]) + assert.equal((await invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1}) as ApplicationPreview).interfaces.items[0].observed.ping.value,null) + const paged=await invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1,offset:1,limit:1}) as ApplicationPreview + assert.deepEqual(paged.interfaces.items,[]);assert.equal(paged.interfaces.total,1);assert.equal(paged.interfaces.nextOffset,null) + fixtureOspf["interface-template"]=[] + const noPeerReads=analysisPeerReads,emptyPreview=await invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1}) as ApplicationPreview + assert.equal(emptyPreview.interfacesTotal,0);assert.equal(emptyPreview.changes.total,0);assert.equal(analysisPeerReads,noPeerReads) + fixtureOspf["interface-template"]=[{...templatesBeforePreview[0],cost:"unknown"}] + await assert.rejects(invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1}),/не подтверждён/) + fixtureOspf["interface-template"]=templatesBeforePreview + countCatalogCalls=true + const beforeCanceled=catalogCalls + await assert.rejects(invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1},AbortSignal.abort())) + assert.equal(catalogCalls,beforeCanceled);countCatalogCalls=false + const beforePause=analysisPeerReads + ospfPreviewPauseConnection=applicationPreviewAccess.connection.id + await assert.rejects(invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1})) + assert.equal(analysisPeerReads,beforePause) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}',to_jsonb('active'::text)) WHERE id=$1",[applicationPreviewAccess.connection.id]) + const disabledPeerReads=analysisPeerReads + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + await assert.rejects(invokeMcpTool(applicationPreviewPrincipal,"mm_ospf_application_preview",{serverId:1})) + assert.equal(analysisPeerReads,disabledPeerReads) + assert.equal(ospfAnalysisDispatches,0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=ANY($1::uuid[]) AND result::text LIKE '%NEVER_OSPF%'",[[applicationPreviewAccess.connection.id,isolatedPreviewAccess.connection.id]])).rows[0].n,0) + }finally{ + countCatalogCalls=false;ospfPreviewPauseConnection=undefined + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}',to_jsonb('active'::text)) WHERE id=$1",[applicationPreviewAccess.connection.id]) + fixtureOspf["interface-template"]=templatesBeforePreview + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + await pool.query("DELETE FROM uptime_speed_probes WHERE id=$1",[previewProbeId]) + await pool.query("DELETE FROM traffic_samples WHERE server_id=1 AND interface_name='fixture-gre' AND sampled_at=ANY($1::timestamptz[])",[previewTrafficTimes]) + await previewHttp.close() + } + console.log("✓ OSPF application preview shares REST ranking: scoped peer/native/SQL reads, hidden probe exclusion, modeled versus observed zero/unknown, aggregate continuity flags, no WG-peer substitution, pages, empty/malformed/disabled sources and no mutation/plan/audit leakage") + countCatalogCalls=true + try{await (await import("../../test/mcp-ospf-legacy.js")).acceptLegacyOspfPlans(actor,{ + get:()=>fixtureOspf["interface-template"],set:rows=>{fixtureOspf["interface-template"]=rows as typeof fixtureOspf["interface-template"]}, + reads:()=>catalogCalls,writes:()=>ospfAnalysisDispatches,resetWrites:value=>{ospfAnalysisDispatches=value},fault:value=>{ospfCostWriteFault=value}, + })}finally{countCatalogCalls=false} + assert.equal((await (await import("./routing-observations.js")).readRoutingObservations([1, 2]))[0].sourceVerified, false, "Disable/re-enable must invalidate previous source attestation") + await attestRoutingFixture(observationId) + const pingOnlyId = randomUUID(), otherPeerInterfaceId = randomUUID() + const insertPingObservation = (id: string, peerInterface: string) => pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,ping_at,context_verified,source) VALUES($1,1,2,'fixture-gre',$2,'ipv4','main',11,0,now(),true,'fixture')", [id, peerInterface]) + await insertPingObservation(pingOnlyId, "peer") + await attestRoutingFixture(pingOnlyId) + const proposal = await invokeMcpTool(analysisPrincipal, "mm_ospf_optimizer_analyze", { serverId: 1 }) as OspfAnalysis + assert.deepEqual(proposal.serverIds, [1, 2]); assert.equal(proposal.changes, 1) + assert.equal(proposal.ranking[0].currentCost, 30); assert.equal(proposal.ranking[0].proposedCost, 10) + assert.equal(proposal.ranking[0].observationId, pingOnlyId); assert.equal(proposal.ranking[0].bandwidthObservationId, observationId); assert.equal(proposal.ranking[0].peerId, 2) + assert.equal(proposal.proposalOnly, true); assert.equal(proposal.executionAvailable, false) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [analysisAccess.connection.id])).rows[0].n, 0) + assert.equal(ospfAnalysisDispatches, 0) + await assert.rejects(invokeMcpTool(analysisPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 })) + const writer = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "OSPF analysis comparison", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1, 2], operationIds: ["routing.ospf.optimize"], allowGlobal: false } }), actor) + const writerPrincipal = await authenticateMcpToken(writer.token) + const prepared = await invokeMcpTool(writerPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string; changes: { before: { ranking: unknown[] } } } + assert.deepEqual(prepared.changes.before.ranking, proposal.ranking) + const observationEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [prepared.planId])).rows[0].evidence.sealed)) + const { verifyRoutingObservationChecks } = await import("./routing-observations.js") + await verifyRoutingObservationChecks(observationEvidence.observationChecks) + await assert.rejects(verifyRoutingObservationChecks(observationEvidence.observationChecks.map((check: Record) => ({ ...check, validUntil: new Date(Date.now() - 1).toISOString() })))) + await approvePlan(prepared.planId, actor, true) + await insertPingObservation(otherPeerInterfaceId, "another-peer-interface") + await attestRoutingFixture(otherPeerInterfaceId) + await verifyRoutingObservationChecks(observationEvidence.observationChecks) + await pool.query("UPDATE mcp_routing_observations SET created_at=now()-interval '1 minute' WHERE id=$1", [observationId]) + await attestRoutingFixture(observationId) + await pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source) VALUES($1,1,2,'fixture-gre','peer','ipv4','main',12,50,100,200,now(),now(),true,'fixture')", [badObservationId]) + await attestRoutingFixture(badObservationId) + await assert.rejects(invokeMcpTool(analysisPrincipal, "mm_ospf_optimizer_analyze", { serverId: 1 }), "A newer bad result must block fallback to an older healthy observation") + await assert.rejects(verifyRoutingObservationChecks(observationEvidence.observationChecks)) + const staleOperation = await applyPlan(writerPrincipal, prepared.planId, randomUUID()) + let staleState = "pending" + for (let attempt = 0; attempt < 100; attempt++) { + staleState = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [staleOperation.id])).rows[0].state + if (!["pending", "running"].includes(staleState)) break + await delay(20) + } + assert.equal(staleState, "rejected"); assert.equal(ospfAnalysisDispatches, 0) + await pool.query("UPDATE mcp_routing_observations SET loss_pct=0,context_verified=false WHERE id=$1", [badObservationId]) + await attestRoutingFixture(badObservationId) + await assert.rejects(invokeMcpTool(analysisPrincipal, "mm_ospf_optimizer_analyze", { serverId: 1 })) + await pool.query("DELETE FROM mcp_routing_observations WHERE id=ANY($1::uuid[])", [[badObservationId, pingOnlyId, otherPeerInterfaceId]]) + await pool.query("UPDATE mcp_routing_observations SET loss_pct=5 WHERE id=$1", [observationId]) + await attestRoutingFixture(observationId) + await assert.rejects(verifyRoutingObservationChecks(observationEvidence.observationChecks)) + await assert.rejects(invokeMcpTool(analysisPrincipal, "mm_ospf_optimizer_analyze", { serverId: 1 })) + const overridden = await invokeMcpTool(analysisPrincipal, "mm_ospf_optimizer_analyze", { serverId: 1, maxLossPct: 5 }) as OspfAnalysis + assert.equal(overridden.ranking.length, 1) + await pool.query("UPDATE mcp_routing_observations SET loss_pct=0,ping_at=now()-interval '10 minutes' WHERE id=$1", [observationId]) + await attestRoutingFixture(observationId) + await assert.rejects(invokeMcpTool(analysisPrincipal, "mm_ospf_optimizer_analyze", { serverId: 1 })) + const limitedAnalysis = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "OSPF isolated analysis", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [1], operationIds: ["routing.ospf.analyze"], allowGlobal: false } }), actor) + const readsBefore = analysisPeerReads + await assert.rejects(invokeMcpTool(await authenticateMcpToken(limitedAnalysis.token), "mm_ospf_optimizer_analyze", { serverId: 1 })) + assert.equal(analysisPeerReads, readsBefore); assert.equal(ospfAnalysisDispatches, 0) + await pool.query("UPDATE mcp_routing_observations SET loss_pct=0,ping_at=now(),bandwidth_at=now(),context_verified=true WHERE id=$1", [observationId]) + await attestRoutingFixture(observationId) + const costBaseline = structuredClone(fixtureOspf["interface-template"]) + const waitCost = waitRecursiveOperation + const rejectGenuinelyQueuedOldVersion = async (principal: Parameters[0], planId: string, oldVersion: number, description: string) => { + const originalPoolQuery = pool.query, query = originalPoolQuery.bind(pool) + let release!: () => void + const barrier = new Promise(done => { release = done }) + let paused: { id: string; plan_id: string; state: string } | undefined + let operation: Awaited> | undefined, terminal: string | undefined + const dispatches = ospfAnalysisDispatches + // Delegate every original query with unchanged arguments/results. Hold + // only the REAL matching running row, before executeOperationWithLease + // can loadPlan. Admission and its private capabilities stay genuine. + pool.query = ((...args: unknown[]) => { + const actual = (query as (...values: unknown[]) => unknown)(...args) + if (args[0] !== "SELECT * FROM mcp_operations WHERE id=$1 AND state='running'") return actual + return (actual as Promise<{ rows: { id: string; plan_id: string; state: string }[] }>).then(async result => { + const row = result.rows[0] + if (!paused && row?.plan_id === planId) { + assert.equal(row.state, "running"); paused = row + await barrier + } + return result + }) + }) as typeof pool.query + try { + operation = await applyPlan(principal, planId, randomUUID()) + const deadline = Date.now() + 10000 + while (!paused) { + assert(Date.now() < deadline, "The genuine runner did not reach the matching queued-version observation") + await delay(10) + } + assert.equal(paused.id, operation.id); assert.equal(paused.plan_id, planId) + await query("UPDATE mcp_plans SET operation_version=$2 WHERE id=$1", [planId, oldVersion]) + } finally { + // Always release the original returned row, restore the observer and + // join the actual admitted operation before native fixture cleanup. + release(); pool.query = originalPoolQuery + if (operation) terminal = await waitCost(operation.id) + } + assert(operation) + assert.equal(terminal, "rejected", description) + assert.equal((await query("SELECT error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0].error_code, "PLAN_EXPIRED") + assert.equal(ospfAnalysisDispatches, dispatches) + } + let thirdOwnerRows: Record[] = [{ ".id": "*third", name: "third", "router-id": "192.0.2.103", disabled: "false", version: "2", vrf: "main" }], thirdOwnerReads = 0 + let thirdOwnerDuringRead: (() => Promise) | undefined + let thirdOwnerIds: Record[] = [] + let thirdOwnerNeighbors: Record[] = [], thirdOwnerAddresses: Record[] = [] + const thirdOwner = createServer(async (req, res) => { + thirdOwnerReads++ + if (ospfAnalysisFixtures) ospfAnalysisNativeRequests++ + if (thirdOwnerDuringRead) { const work = thirdOwnerDuringRead; thirdOwnerDuringRead = undefined; await work() } + res.setHeader("content-type", "application/json") + if (req.url === "/rest/routing/id") { res.end(JSON.stringify(thirdOwnerIds)); return } + if (req.url === "/rest/routing/ospf/neighbor") { res.end(JSON.stringify(thirdOwnerNeighbors)); return } + if (req.url === "/rest/ip/address") { res.end(JSON.stringify(thirdOwnerAddresses)); return } + if (req.url === "/rest/routing/ospf/instance/get") { + let body = ""; for await (const chunk of req) body += chunk + const key = JSON.parse(body)["value-name"] + res.end(JSON.stringify({ ret: ({ version: "2", vrf: "remote-named-vrf", "router-id": "main" } as Record)[key] })); return + } + res.end(JSON.stringify(thirdOwnerRows)) + }) + await new Promise(resolve => thirdOwner.listen(0, "127.0.0.1", resolve)) + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(103,'unselected-ospf-owner','127.0.0.1',$1,false,'NEVER_OSPF_UNUSED_SECRET')", [(thirdOwner.address() as { port: number }).port]) + const graphPolicy = { limits: { callsPerMinute: 120, mutationsPerHour: 100 }, name: "OSPF complete owners", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [1, 2, 103], operationIds: ["routing.ospf.optimize", "operations.recover"], allowGlobal: false } } + const graphAccess = await createMcpConnection(mcpConnectionCreateSchema.parse(graphPolicy), actor), graphPrincipal = await authenticateMcpToken(graphAccess.token) + const graphPrimaryInstances = structuredClone(fixtureOspf.instance) + const graphPeerInstances = structuredClone(analysisPeerInstances), graphPeerNeighbors = structuredClone(analysisPeerNeighbors), graphPeerAddresses = structuredClone(analysisPeerAddresses) + try { + const dispatchesBeforePassive = ospfAnalysisDispatches + fixtureOspf["interface-template"][0].passive = "true" + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN", "A passive template cannot become eligible through a stale Full neighbor") + for (const passive of ["true", "false"]) { + fixtureOspf["interface-template"] = [...structuredClone(costBaseline), { ...costBaseline[0], ".id": "*passive", interfaces: "fixture-loop", passive }] + const mixedPassive = await invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string } + const mixedRow = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [mixedPassive.planId])).rows[0] + const mixedEvidence = JSON.parse(await decryptSecret(mixedRow.evidence.sealed)) as { before: { ranking: { interface: string }[] }; steps: { step: { id: string } }[] } + assert.deepEqual(mixedEvidence.before.ranking.map(row => row.interface), [costBaseline[0].interfaces]) + assert.deepEqual(mixedEvidence.steps.map(row => row.step.id), [costBaseline[0][".id"]]) + } + assert.equal(ospfAnalysisDispatches, dispatchesBeforePassive, "Passive or no-Full templates with unknown BFD cannot block or enter the selected GRE plan") + fixtureOspf["interface-template"] = [{ ...costBaseline[0], interfaces: "fixture-loop" }] + ospfAnalysisExtraNeighbors = [{ ".id": "*loop", instance: "fixture-ospf", interface: "fixture-loop", state: "Full", "router-id": "192.0.2.2", address: "192.0.2.2" }] + try { + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }), (error: { code?: string; message?: string }) => error.code === "MEASUREMENT_UNKNOWN" && Boolean(error.message?.includes("use-bfd")), "Unknown BFD remains forbidden for a Full candidate") + } finally { ospfAnalysisExtraNeighbors = [] } + fixtureOspf["interface-template"] = structuredClone(costBaseline) + for (const listsBeforePause of [0, 1]) { + const readsBeforePause: number = ospfTemplateFlagReads + ospfPauseOnTemplatesConnection = graphAccess.connection.id; ospfTemplateListsBeforePause = listsBeforePause + try { + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 })) + assert.equal(ospfTemplateFlagReads, readsBeforePause + listsBeforePause * 2, "Pause committed during either template list must stop before another flag receipt") + } finally { ospfPauseOnTemplatesConnection = undefined; await setConnectionStatus(graphAccess.connection.id, "active", actor) } + } + const limitedGraph = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...graphPolicy, name: "OSPF owner budget", limits: { serversPerPlan: 2 } }), actor) + const readsBeforeBudget = [thirdOwnerReads, analysisPeerReads] + await assert.rejects(invokeMcpTool(await authenticateMcpToken(limitedGraph.token), "mm_ospf_optimizer_plan", { serverId: 1 })) + assert.deepEqual([thirdOwnerReads, analysisPeerReads], readsBeforeBudget, "All owner dependencies must fit policy before native polling") + await setConnectionStatus(limitedGraph.connection.id, "paused", actor) + for (const fault of ["none", "duplicate", "empty", "credential", "disabled", "during-read", "other-family", "family-change", "elected-change", "elected-inactive", "elected-defaults", "unrelated-vrf", "isolated-vrf", "peer-vrf", "peer-down", "peer-address", "peer-interface", "counter-only", "post-peer-down", "template-order", "post-template-order"]) { + fixtureOspf["interface-template"] = structuredClone(costBaseline) + if (fault === "template-order" || fault === "post-template-order") fixtureOspf["interface-template"].push({ ...costBaseline[0], ".id": "*passive", interfaces: "fixture-loop", passive: "true" }) + if (fault === "other-family" || fault === "family-change") Object.assign(thirdOwnerRows[0], { "router-id": "192.0.2.2", version: "3", vrf: "remote-named-vrf" }) + if (fault.startsWith("elected-")) { + thirdOwnerRows[0]["router-id"] = "main" + thirdOwnerIds = [{ ".id": "*ref", name: "main", "dynamic-id": "192.0.2.103", disabled: "false", inactive: "false" }] + if (fault === "elected-defaults") { delete thirdOwnerRows[0].version; delete thirdOwnerRows[0].vrf; delete thirdOwnerRows[0]["router-id"] } + } + if (fault === "unrelated-vrf") { + fixtureOspf.instance.push({ ".id": "*other", name: "other-instance", vrf: "isolated", "router-id": "192.0.2.200", version: "2", disabled: "false" }) + ospfAnalysisExtraNeighbors = [{ ".id": "*other-neighbor", interface: "other-iface", instance: "other-instance", "router-id": "192.0.2.1", address: "198.51.100.1", state: "Full" }] + thirdOwnerRows[0]["router-id"] = "192.0.2.1" + } + if (fault === "isolated-vrf") { + Object.assign(thirdOwnerRows[0], { "router-id": "192.0.2.2", vrf: "isolated-third-vrf" }) + thirdOwnerNeighbors = [{ ".id": "*tn", instance: "third", interface: "third", "router-id": "192.0.2.1", address: "198.51.100.1", state: "Full" }] + thirdOwnerAddresses = [{ ".id": "*ta", interface: "third", address: "198.51.100.3/24", disabled: "false", invalid: "false" }] + } + if (fault === "peer-vrf") analysisPeerInstances[0].vrf = "remote-different-vrf" + const prepared = await invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string; changes: { target: { serverIds: number[] }; ospfOwnerGraph: { sourceSecretProofs: unknown; instances: { serverId: number }[] } } } + assert.deepEqual(prepared.changes.target.serverIds, [1, 2, 103]) + assert.deepEqual(prepared.changes.ospfOwnerGraph.instances.map(row => row.serverId), [1, 2, 103]) + assert.equal(prepared.changes.ospfOwnerGraph.sourceSecretProofs, "[redacted]") + assert.ok(!JSON.stringify(prepared).includes("NEVER_OSPF_UNUSED_SECRET")) + await approvePlan(prepared.planId, actor, true) + if (fault === "duplicate") thirdOwnerRows[0]["router-id"] = "192.0.2.2" + if (fault === "empty") thirdOwnerRows = [] + if (fault === "credential") await pool.query("UPDATE servers SET password='changed-unused-owner' WHERE id=103") + if (fault === "disabled") await pool.query("UPDATE servers SET enabled=false WHERE id=103") + if (fault === "during-read") thirdOwnerDuringRead = () => pool.query("UPDATE servers SET password='changed-during-native-read' WHERE id=103") + if (fault === "family-change") thirdOwnerRows[0].version = "2" + if (fault === "elected-change") thirdOwnerIds[0]["dynamic-id"] = "192.0.2.2" + if (fault === "elected-inactive") thirdOwnerIds[0].inactive = "true" + if (fault === "peer-down") analysisPeerNeighbors[0].state = "Down" + if (fault === "peer-address") analysisPeerAddresses[0].address = "192.0.2.99/30" + if (fault === "peer-interface") analysisPeerNeighbors[0].interface = "another-interface" + if (fault === "counter-only") Object.assign(analysisPeerNeighbors[0], { adjacency: "99h", "state-changes": "999" }) + if (fault === "post-peer-down") ospfAfterCostPatch = () => { analysisPeerNeighbors[0].state = "Down" } + if (fault === "template-order") fixtureOspf["interface-template"].reverse() + if (fault === "post-template-order") ospfAfterCostPatch = () => { fixtureOspf["interface-template"].reverse() } + const dispatches: number = ospfAnalysisDispatches, requestsBeforeApply = ospfAnalysisNativeRequests, key = randomUUID(), operation = await applyPlan(graphPrincipal, prepared.planId, key) + const positive = ["none", "other-family", "elected-defaults", "unrelated-vrf", "isolated-vrf", "peer-vrf", "counter-only"].includes(fault) + assert.equal(await waitCost(operation.id), positive ? "applied" : fault === "post-peer-down" ? "partial" : fault === "post-template-order" ? "unknown" : "rejected", fault) + assert.equal(ospfAnalysisDispatches, dispatches + (positive || fault === "post-peer-down" || fault === "post-template-order" ? 1 : 0), fault) + assert.equal((await applyPlan(graphPrincipal, prepared.planId, key)).id, operation.id) + if (fault === "none") { + const nativeRequests = ospfAnalysisNativeRequests - requestsBeforeApply + assert.ok(nativeRequests <= 160, `One OSPF cost PATCH caused ${nativeRequests} native requests; full graph polling must not run inside each catalog callback`) + console.log(`✓ OSPF dispatch read budget: ${nativeRequests} native requests across three owners, including one PATCH; no nested full graph polling`) + const { rows: [version] } = await pool.query<{ operation_version: number }>("SELECT operation_version FROM mcp_plans WHERE id=$1", [prepared.planId]) + await pool.query("UPDATE mcp_plans SET operation_version=$2 WHERE id=$1", [prepared.planId, version.operation_version - 1]) + try { await assert.rejects(invokeMcpTool(graphPrincipal, "mm_operation_recovery_plan", { operationId: operation.id }), (error: { code?: string }) => error.code === "PLAN_EXPIRED") } + finally { await pool.query("UPDATE mcp_plans SET operation_version=$2 WHERE id=$1", [prepared.planId, version.operation_version]) } + const oldInverse = await invokeMcpTool(graphPrincipal, "mm_operation_recovery_plan", { operationId: operation.id }) as { planId: string } + await approvePlan(oldInverse.planId, actor, true) + await pool.query("UPDATE mcp_plans SET operation_version=1 WHERE id=$1", [oldInverse.planId]) + await assert.rejects(applyPlan(graphPrincipal, oldInverse.planId, randomUUID()), (error: { code?: string }) => error.code === "PLAN_EXPIRED") + const queuedInverse = await invokeMcpTool(graphPrincipal, "mm_operation_recovery_plan", { operationId: operation.id }) as { planId: string } + await approvePlan(queuedInverse.planId, actor, true) + await rejectGenuinelyQueuedOldVersion(graphPrincipal, queuedInverse.planId, 1, "Queued old inverse revision cannot escape the runner version check") + } + if (fault === "post-peer-down" || fault === "post-template-order") { + const verified = (await pool.query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1 AND state='verified'", [operation.id])).rows[0].n + assert.equal(verified, 0, "A peer or template-order change after ACK cannot leave a verified cost checkpoint") + } + if (fault === "none" || fault === "peer-vrf") { + const inverse = await invokeMcpTool(graphPrincipal, "mm_operation_recovery_plan", { operationId: operation.id }) as { planId: string } + const inverseEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [inverse.planId])).rows[0].evidence.sealed)) + assert.equal(inverseEvidence.steps[0].step.expectedCost, 10, "Inverse must compare the applied cost, not the original pre-write value") + assert.equal(inverseEvidence.steps[0].step.fields.cost, costBaseline[0].cost) + assert.deepEqual(inverseEvidence.ospfOwnerGraph.instances.map((row: { serverId: number }) => row.serverId), [1, 2, 103]) + await approvePlan(inverse.planId, actor, true) + if (fault === "peer-vrf") analysisPeerNeighbors[0].state = "Down" + const beforeInverse: number = ospfAnalysisDispatches, inverseKey = randomUUID(), inverseOperation = await applyPlan(graphPrincipal, inverse.planId, inverseKey) + assert.equal(await waitCost(inverseOperation.id), fault === "none" ? "applied" : "rejected") + assert.equal(ospfAnalysisDispatches, beforeInverse + (fault === "none" ? 1 : 0)) + assert.equal((await applyPlan(graphPrincipal, inverse.planId, inverseKey)).id, inverseOperation.id) + if (fault === "none") assert.deepEqual(fixtureOspf["interface-template"], costBaseline) + } + thirdOwnerRows = [{ ".id": "*third", name: "third", "router-id": "192.0.2.103", disabled: "false", version: "2", vrf: "main" }] + thirdOwnerIds = [] + thirdOwnerNeighbors = []; thirdOwnerAddresses = [] + analysisPeerInstances = structuredClone(graphPeerInstances); analysisPeerNeighbors = structuredClone(graphPeerNeighbors); analysisPeerAddresses = structuredClone(graphPeerAddresses) + ospfAnalysisExtraNeighbors = []; fixtureOspf.instance = structuredClone(graphPrimaryInstances) + await pool.query("UPDATE servers SET password='NEVER_OSPF_UNUSED_SECRET',enabled=true WHERE id=103") + } + const contextDispatches = ospfAnalysisDispatches + Object.assign(thirdOwnerRows[0], { "router-id": "192.0.2.2", vrf: "cloned-context" }) + thirdOwnerNeighbors = [{ ".id": "*tn", instance: "third", interface: "third", "router-id": "192.0.2.1", address: "192.0.2.1", state: "Full" }] + thirdOwnerAddresses = [{ ".id": "*ta", interface: "third", address: "192.0.2.2/30", disabled: "false", invalid: "false" }] + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN", "Two devices with identical reciprocal contexts remain ambiguous") + thirdOwnerRows[0]["router-id"] = "192.0.2.103"; thirdOwnerNeighbors = []; thirdOwnerAddresses = [] + analysisPeerInstances.push({ ...analysisPeerInstances[0], ".id": "*second", name: "second-vrf", vrf: "another-peer-vrf" }) + analysisPeerNeighbors.push({ ...analysisPeerNeighbors[0], ".id": "*secondn", instance: "second-vrf", interface: "another-peer-interface" }) + analysisPeerAddresses.push({ ...analysisPeerAddresses[0], ".id": "*seconda", interface: "another-peer-interface" }) + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN", "Two matching VRFs on the same peer are not merged by device ID") + analysisPeerInstances = structuredClone(graphPeerInstances); analysisPeerNeighbors = structuredClone(graphPeerNeighbors); analysisPeerAddresses = structuredClone(graphPeerAddresses) + await pool.query("UPDATE mcp_routing_observations SET dst_interface='wrong-peer-interface' WHERE id=$1", [observationId]) + await attestRoutingFixture(observationId) + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }), "Metric for another peer interface cannot be substituted") + await pool.query("UPDATE mcp_routing_observations SET dst_interface='peer' WHERE id=$1", [observationId]); await attestRoutingFixture(observationId) + assert.equal(ospfAnalysisDispatches, contextDispatches) + const preparationReads = thirdOwnerReads + thirdOwnerDuringRead = () => setConnectionStatus(graphAccess.connection.id, "paused", actor) + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 })) + assert.equal(thirdOwnerReads, preparationReads + 1, "Pause during preparation stops before another native context read") + await setConnectionStatus(graphAccess.connection.id, "active", actor) + const cancelledReads = thirdOwnerReads, preparationCancel = new AbortController() + thirdOwnerDuringRead = async () => preparationCancel.abort() + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }, preparationCancel.signal)) + assert.equal(thirdOwnerReads, cancelledReads + 1, "Cancellation stops before another native context read") + const changedReads = thirdOwnerReads + thirdOwnerDuringRead = () => pool.query("UPDATE servers SET password='changed-during-preparation' WHERE id=103") + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 })) + assert.equal(thirdOwnerReads, changedReads + 1, "Source drift during preparation stops before another native context read") + await pool.query("UPDATE servers SET password='NEVER_OSPF_UNUSED_SECRET' WHERE id=103") + fixtureOspf["interface-template"] = structuredClone(costBaseline) + await pool.query("UPDATE servers SET enabled=false WHERE id=103") + const disabledReads = [thirdOwnerReads, analysisPeerReads] + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 })) + assert.deepEqual([thirdOwnerReads, analysisPeerReads], disabledReads, "Monitoring disabled cannot be guessed to mean an absent Router ID owner") + await pool.query("UPDATE servers SET enabled=true WHERE id=103") + fixtureOspf["interface-template"] = structuredClone(costBaseline) + const graphLease = await pool.connect() + try { + await graphLease.query("SELECT pg_advisory_lock(hashtextextended('mm-config:103',0))") + const prepared = await invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string } + await approvePlan(prepared.planId, actor, true) + const dispatches = ospfAnalysisDispatches, key = randomUUID(), operation = await applyPlan(graphPrincipal, prepared.planId, key) + assert.equal(await waitCost(operation.id), "rejected"); assert.equal(ospfAnalysisDispatches, dispatches, "Unselected owner participates in the actual multi-router lease") + } finally { await graphLease.query("SELECT pg_advisory_unlock(hashtextextended('mm-config:103',0))"); graphLease.release() } + fixtureOspf["interface-template"] = structuredClone(costBaseline) + thirdOwnerRows = Array.from({ length: 10001 }, (_, n) => ({ ".id": "*third" + n, name: "third" + n, "router-id": "192.0.2.103", disabled: "false", version: "2", vrf: "main" })) + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 })) + thirdOwnerRows = [{ ".id": "*third", name: "third", "router-id": "192.0.2.103", disabled: "false", version: "2", vrf: "main", opaque: "x".repeat(1048576) }] + await assert.rejects(invokeMcpTool(graphPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 })) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%NEVER_OSPF_UNUSED_SECRET%'", [graphAccess.connection.id])).rows[0].n, 0) + console.log("✓ OSPF full owner graph: unselected third device sealed and locked, late duplicate/removal/credential/disable reject before PATCH, positive cost, scoped server budget before GET, private proofs and bounded graph") + console.log("✓ OSPF VRF link context: reciprocal Full plus both address/interface bindings, different VRF names, isolated duplicate accepted, cloned contexts and wrong peer metric refused; late/post-write drift, counter tolerance, preparation pause/cancel/source fencing and no replay") + } finally { + ospfAfterCostPatch = undefined + analysisPeerInstances = graphPeerInstances; analysisPeerNeighbors = graphPeerNeighbors; analysisPeerAddresses = graphPeerAddresses + ospfAnalysisExtraNeighbors = []; fixtureOspf.instance = graphPrimaryInstances + await setConnectionStatus(graphAccess.connection.id, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=103") + await new Promise(resolve => thirdOwner.close(() => resolve())) + fixtureOspf["interface-template"] = structuredClone(costBaseline) + } + try { + const savedPassword = (await pool.query("SELECT password FROM servers WHERE id=1")).rows[0].password + const credentialCandidate = await invokeMcpTool(writerPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string } + await approvePlan(credentialCandidate.planId, actor, true) + await pool.query("UPDATE servers SET password='NEVER_OSPF_CHANGED_CREDENTIAL' WHERE id=1") + const credentialDispatches = ospfAnalysisDispatches + try { + const op = await applyPlan(writerPrincipal, credentialCandidate.planId, randomUUID()) + assert.equal(await waitCost(op.id), "rejected") + assert.equal(ospfAnalysisDispatches, credentialDispatches, "Credential drift must stop before PATCH even if native configuration is identical") + } finally { await pool.query("UPDATE servers SET password=$1 WHERE id=1", [savedPassword]) } + await attestRoutingFixture(observationId) + const oldVersionCandidate = await invokeMcpTool(writerPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string } + await approvePlan(oldVersionCandidate.planId, actor, true) + await pool.query("UPDATE mcp_plans SET operation_version=6 WHERE id=$1", [oldVersionCandidate.planId]) + await assert.rejects(applyPlan(writerPrincipal, oldVersionCandidate.planId, randomUUID()), (error: { code?: string }) => error.code === "PLAN_EXPIRED") + assert.equal(ospfAnalysisDispatches, credentialDispatches) + const queuedVersionCandidate = await invokeMcpTool(writerPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string } + await approvePlan(queuedVersionCandidate.planId, actor, true) + await rejectGenuinelyQueuedOldVersion(writerPrincipal, queuedVersionCandidate.planId, 6, "Previously queued old revision must not escape the runner version check") + assert.equal(ospfAnalysisDispatches, credentialDispatches) + for (const fault of ["none", "ignored", "changed-field", "lost-response"]) { + fixtureOspf["interface-template"] = structuredClone(costBaseline) + Object.assign(fixtureOspf["interface-template"][0], { opaque: "NEVER_OSPF_OPAQUE" }) + ospfCostWriteFault = fault + const candidate = await invokeMcpTool(writerPrincipal, "mm_ospf_optimizer_plan", { serverId: 1 }) as { planId: string } + await approvePlan(candidate.planId, actor, true) + const key = randomUUID(), beforeDispatch: number = ospfAnalysisDispatches, op = await applyPlan(writerPrincipal, candidate.planId, key) + assert.equal(await waitCost(op.id), fault === "none" ? "applied" : "unknown") + assert.equal(ospfAnalysisDispatches, beforeDispatch + 1, "Cost must use exactly one PATCH and never replay it") + assert.equal((await applyPlan(writerPrincipal, candidate.planId, key)).id, op.id) + assert.equal(ospfAnalysisDispatches, beforeDispatch + 1) + if (fault === "none") { assert.equal(fixtureOspf["interface-template"][0].cost, "10"); assert.equal((fixtureOspf["interface-template"][0] as Record).opaque, "NEVER_OSPF_OPAQUE") } + } + } finally { fixtureOspf["interface-template"] = costBaseline; ospfCostWriteFault = "none" } + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND result::text LIKE '%NEVER_OSPF_OPAQUE%'", [writer.connection.id])).rows[0].n, 0) + console.log("✓ Shared OSPF cost writer over actual MCP/HTTP: sealed expected cost, opaque-field preservation, ignored/changed/lost result stop and idempotency without PATCH replay") + } finally { + ospfAnalysisFixtures = false + await pool.query("UPDATE servers SET port=$1 WHERE id=2", [port]) + await pool.query("DELETE FROM mcp_routing_observations WHERE id=ANY($1::uuid[])", [[observationId, badObservationId]]) + await new Promise(resolve => analysisPeer.close(() => resolve())) + } + console.log("✓ Observer OSPF analysis shares the sealed planner ranking, dispatches no writes/plans, narrows peer scope and rejects stale/lossy observations by default") + ecmpFixtures = true + const ecmpBaseline = [0, 1].map(index => ({ ".id": `*ecmp${index}`, "dst-address": "192.0.2.0/24", "routing-table": "main", gateway: `192.0.2.${index + 2}`, "immediate-gw": `192.0.2.${index + 2}%fixture-gre`, distance: String((index + 1) * 10), active: index === 0 ? "true" : "false", comment: "MikrotikManager: integration", disabled: "false" })) + fixtureEcmpRoutes = structuredClone(ecmpBaseline) + const ecmpAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "ECMP transition test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write"], scope: { serverIds: [1], operationIds: ["routing.ecmp.update", "routing.rpf.update", "ros.ip_settings.update", "operations.recover"], allowGlobal: false } }), actor) + const ecmpPrincipal = await authenticateMcpToken(ecmpAccess.token) + const ecmpArgs = { serverId: 1, family: "ipv4", routeIds: ["*ecmp0", "*ecmp1"], distance: 10 } + const equalPlan = await invokeMcpTool(ecmpPrincipal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + await approvePlan(equalPlan.planId, actor, true) + const equalOperation = await applyPlan(ecmpPrincipal, equalPlan.planId, randomUUID()) + await awaitOperation(equalOperation.id, "applied"); assert.equal(ecmpWrites, 2) + assert.ok(fixtureEcmpRoutes.every(row => row.active === "true" && row.distance === "10")) + const unequalPlan = await invokeMcpTool(ecmpPrincipal, "mm_operation_recovery_plan", { operationId: equalOperation.id }) as { planId: string } + await approvePlan(unequalPlan.planId, actor, true) + await awaitOperation((await applyPlan(ecmpPrincipal, unequalPlan.planId, randomUUID())).id, "applied") + assert.deepEqual(fixtureEcmpRoutes, ecmpBaseline) + const staleEcmp = await invokeMcpTool(ecmpPrincipal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + await approvePlan(staleEcmp.planId, actor, true) + fixtureEcmpRoutes[1]["immediate-gw"] = "" + await awaitOperation((await applyPlan(ecmpPrincipal, staleEcmp.planId, randomUUID())).id, "rejected"); assert.equal(ecmpWrites, 4) + fixtureEcmpRoutes = structuredClone(ecmpBaseline) + const brokenEcmp = await invokeMcpTool(ecmpPrincipal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + await approvePlan(brokenEcmp.planId, actor, true) + loseEcmpGateway = true + const brokenOperation = await applyPlan(ecmpPrincipal, brokenEcmp.planId, randomUUID()) + await awaitOperation(brokenOperation.id, "partial"); assert.equal(ecmpWrites, 5) + await applyPlan(ecmpPrincipal, brokenEcmp.planId, randomUUID()); await delay(30); assert.equal(ecmpWrites, 5) + loseEcmpGateway = false; fixtureEcmpRoutes = structuredClone(ecmpBaseline) + const tableProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [tableProtection, JSON.stringify({ kind: "routing_table", id: "*main", serverId: 1, protection: "deny" })]) + await assert.rejects(invokeMcpTool(ecmpPrincipal, "mm_ecmp_plan", ecmpArgs)) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [tableProtection]) + const settingsProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [settingsProtection, JSON.stringify({ kind: "ip_settings", id: "singleton", serverId: 1, protection: "deny" })]) + await assert.rejects(invokeMcpTool(ecmpPrincipal, "mm_ecmp_plan", { ...ecmpArgs, hashPolicy: "l4" })) + await assert.rejects(invokeMcpTool(ecmpPrincipal, "mm_rpf_plan", { serverId: 1, mode: "loose", acknowledgeAsymmetry: true })) + await assert.rejects(invokeMcpTool(ecmpPrincipal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } })) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [settingsProtection]) + ecmpFixtures = false + console.log("✓ ECMP: resolved inactive candidate, equal-active transition, inverse to unequal distances, lost resolution before/after write and table protection") + const plan = await invokeMcpTool(principal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } }) as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval") + await assert.rejects(applyPlan(principal, plan.planId, "test-key")) + await approvePlan(plan.planId, actor, true) + const operation = await applyPlan(principal, plan.planId, "test-key") + assert.equal((await applyPlan(principal, plan.planId, "test-key")).id, operation.id) + let state = "pending" + for (let index = 0; index < 100; index++) { + const row = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + state = row.state + if (!["pending", "running"].includes(state)) break + await delay(30) + } + assert.equal(state, "applied"); assert.equal(writes, 1); assert.equal(settings["rp-filter"], "loose") + assert.equal((await pool.query("SELECT state FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].state, "verified") + console.log("✓ Human approval, write-ahead intent, single idempotent write and read-back verification") + const { detectMcpDrift } = await import("./workers.js") + const driftCount = async () => (await pool.query("SELECT count(*)::int AS n FROM mcp_drift WHERE operation_id=$1", [operation.id])).rows[0].n + const readsBeforeMatchingDrift = reconcileSettingsReads + await detectMcpDrift() + assert(reconcileSettingsReads > readsBeforeMatchingDrift) + assert.equal(await driftCount(), 0) + const stale = await invokeMcpTool(principal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } }) as { planId: string } + await approvePlan(stale.planId, actor, true) + settings["rp-filter"] = "no" + const conflict = await applyPlan(principal, stale.planId, "conflict-key") + for (let index = 0; index < 100; index++) { + state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [conflict.id])).rows[0].state + if (!["pending", "running"].includes(state)) break + await delay(30) + } + assert.equal(state, "rejected"); assert.equal(writes, 1) + console.log("✓ Changed snapshots reject execution before contacting the write endpoint") + const readsBeforeConfirmedDrift = reconcileSettingsReads + await detectMcpDrift() + assert(reconcileSettingsReads > readsBeforeConfirmedDrift) + assert.equal(await driftCount(), 1) + await detectMcpDrift() + assert.equal(await driftCount(), 1) + assert.equal(writes, 1) + console.log("✓ Drift detection reads the bound RouterOS source, records one confirmed incident and never replays writes") + const expiring = await invokeMcpTool(principal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } }) as { planId: string } + const sealedEvidence = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [expiring.planId])).rows[0].evidence + const expiringEvidence = JSON.parse(await decryptSecret(sealedEvidence.sealed)) + expiringEvidence.validUntil = new Date(Date.now() - 1000).toISOString() + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [expiring.planId, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(expiringEvidence)) })]) + await approvePlan(expiring.planId, actor, true) + const mutationBudgetBeforeExpiry = (await pool.query("SELECT kind,bucket,used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations' ORDER BY bucket", [principal.connectionId])).rows + await assert.rejects(applyPlan(principal, expiring.planId, "expired-measurement"), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE plan_id=$1", [expiring.planId])).rows[0].n, 0) + assert.deepEqual((await pool.query("SELECT kind,bucket,used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations' ORDER BY bucket", [principal.connectionId])).rows, mutationBudgetBeforeExpiry) + assert.equal(writes, 1) + console.log("✓ Expired measurement evidence refuses admission with its own reason before operation, mutation debit or native write") + const { routerClient } = await import("./resources.js") + const nativeClient = (await routerClient(1)).client, protectionId = randomUUID(), writesBeforeProtection = writes + const { updateServer } = await import("../servers/service/servers-service.js") + const nameBeforeGuard = (await pool.query("SELECT name FROM servers WHERE id=1")).rows[0].name + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protectionId, JSON.stringify({ kind: "server", id: "1", serverId: 1, protection: "deny" })]) + try { + await assert.rejects(nativeClient.post("/ip/settings/set", { "rp-filter": "strict" }), /защитой/) + await assert.rejects(updateServer(1, { name: "must-not-change" })) + assert.equal((await pool.query("SELECT name FROM servers WHERE id=1")).rows[0].name, nameBeforeGuard) + assert.equal(writes, writesBeforeProtection) + } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) } + const auditCount = (await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent'")).rows[0].n + assert.ok(auditCount > 0) + const originalQuery = pool.query.bind(pool) + // Storage outage must be observed before any native network request is made. + pool.query = ((...args: unknown[]) => { + if (typeof args[0] === "string" && args[0].startsWith("INSERT INTO mcp_audit")) return Promise.reject(new Error("test audit outage")) + return (originalQuery as (...args: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { await assert.rejects(nativeClient.post("/ip/settings/set", { "rp-filter": "strict" }), /audit outage/); await assert.rejects(updateServer(1, { name: "must-not-change" }), /audit outage/); assert.equal((await pool.query("SELECT name FROM servers WHERE id=1")).rows[0].name, nameBeforeGuard); assert.equal(writes, writesBeforeProtection) } + finally { pool.query = originalQuery as typeof pool.query } + console.log("✓ Legacy native writes cannot bypass server protection or unavailable write-ahead audit") + const { withConfigMutationLock } = await import("../../services/config-mutation-lock.js") + let wake!: () => void, late!: Promise, lateWrites = 0 + const wait = new Promise(resolve => { wake = resolve }) + await withConfigMutationLock([1], async () => { + late = wait.then(() => withConfigMutationLock([1], async () => { lateWrites++ })) + }) + const competingLease = await pool.connect() + try { + await competingLease.query("SELECT pg_advisory_lock(hashtextextended('mm-config:1',0))") + wake(); await assert.rejects(late, /другой операцией/); assert.equal(lateWrites, 0) + } finally { await competingLease.query("SELECT pg_advisory_unlock(hashtextextended('mm-config:1',0))"); competingLease.release() } + console.log("✓ Detached children cannot reuse a released configuration lease") + const rotated = await rotateConnectionToken(issued.connection.id, actor) + await assert.rejects(authenticateMcpToken(issued.token)) + await authenticateMcpToken(rotated.token) + const { buildApp } = await import("../../index.js") + const reserve = createServer() + await new Promise(resolve => reserve.listen(0, "127.0.0.1", resolve)) + const appPort = (reserve.address() as { port: number }).port + await new Promise(resolve => reserve.close(() => resolve())) + const endpoint = `http://localhost:${appPort}/mcp` + process.env.MCP_PUBLIC_URL = endpoint + app = await buildApp({ startScheduler: false }) + await app.listen({ port: appPort, host: "127.0.0.1" }) + await (await import("../../test/users-profile-permissions.js")).verifyUsersProfilePermissions(app) + await (await import("../../test/mcp-backup-download.js")).verifyMcpBackupDownload(actor,app) + await (await import("../../test/mcp-database-backup.js")).verifyMcpDatabaseBackup(actor,app) + const fixtureScheduler = await import("../../services/scheduler-running.js") + const fixtureSchedulerWasPaused = fixtureScheduler.isSchedulerJobsPaused() + await fixtureScheduler.pauseSchedulerJobsAndWait() + try { + await (await import("./control-path-context-acceptance.js")).verifyMcpControlPathContext(actor) + await (await import("./routing-community-scoped-acceptance.js")).acceptScopedRouteCommunitySwitch(actor) + await (await import("../../test/mcp-terminal-diagnostics.js")).verifyMcpTerminalDiagnostics(actor, app) + await (await import("../../test/mcp-events-batch.js")).verifyMcpEventsBatch(actor, app) + await (await import("./wireguard-interface-update-acceptance.js")).acceptWireguardInterfaceUpdate(actor) + await (await import("./wireguard-import-acceptance.js")).acceptWireguardImport(actor) + await (await import("./wireguard-export-acceptance.js")).acceptWireguardExportMcp(actor, app) + await (await import("./protection-scope-acceptance.js")).acceptProtectionRegistrationScope(actor) + console.log("Routing controller CAS SQL:", await (await import("./routing-controller-cas-acceptance.js")).acceptRoutingControllerCas()) + await (await import("../../test/mcp-server-connection.js")).verifyMcpServerConnection(actor) + await (await import("./ipsec-peer-delete-acceptance.js")).acceptIpsecPeerDeletion(actor) + await (await import("./ipsec-user-delete-acceptance.js")).acceptIpsecUserDeletion(actor) + await (await import("./ipsec-certificate-delete-acceptance.js")).acceptIpsecCertificateDeletion(actor) + await (await import("./ipsec-peer-update-acceptance.js")).acceptIpsecPeerUpdate(actor) + await (await import("./management-transport-acceptance.js")).acceptManagementTransport(actor) + await (await import("../../test/mcp-flow-purge.js")).verifyMcpFlowPurge(actor) + await (await import("./routing-community-context-compatibility-acceptance.js")).acceptRouteCommunityContextCompatibilityMcp(actor) + } finally { if (!fixtureSchedulerWasPaused) fixtureScheduler.resumeSchedulerJobs() } + const client = new Client({ name: "integration", version: "1.0.0" }) + try { + await client.connect(new StreamableHTTPClientTransport(new URL(endpoint), { requestInit: { headers: { Authorization: `Bearer ${rotated.token}` } } })) + const listed = await client.listTools() + assert.ok(listed.tools.some(tool => tool.name === "mm_ip_settings_list")) + assert.ok(!listed.tools.some(tool => tool.name === "mm_servers_delete_plan")) + const result = await client.callTool({ name: "mm_ip_settings_list", arguments: { serverId: 1 } }) + assert.ok(!result.isError) + console.log("✓ Official SDK HTTP initialize, filtered tools/list and tools/call") + } finally { await client.close() } + { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + ...input, name: "Logs follow maintenance", permissions: ["mm:servers:read"], + scope: { serverIds: [1], operationIds: ["logs.follow"], allowGlobal: false }, + }), actor) + const follower = new Client({ name: "logs-follow-maintenance", version: "1.0.0" }) + const { sharedDatabaseGate } = await import("../../services/shared-database-gate.js") + const original = pool.query + let arrived!: () => void, release!: () => void + const reading = new Promise(resolve => { arrived = resolve }) + const held = new Promise(resolve => { release = resolve }) + try { + await follower.connect(new StreamableHTTPClientTransport(new URL(endpoint), { requestInit: { headers: { Authorization: `Bearer ${access.token}` } } })) + pool.query = (async (...args: Parameters) => { + const text = typeof args[0] === "string" ? args[0] : args[0]?.text + const result = await Reflect.apply(original, pool, args) + if (typeof text === "string" && text.startsWith("SELECT GREATEST(COALESCE(max(id),0)")) { + arrived() + await held + } + return result + }) as typeof original + const following = follower.callTool({ name: "mm_logs_follow", arguments: { serverId: 1, waitSeconds: 30 } }) + try { + const observation = new AbortController() + try { + await Promise.race([reading, following.then(() => { throw new Error("Follow finished before its stream query") }), + delay(5000, undefined, { signal: observation.signal }).then(() => { throw new Error("Follow stream query was not observed") })]) + } finally { observation.abort() } + let exclusiveEntered = false + const maintenance = sharedDatabaseGate.exclusive(async () => { exclusiveEntered = true }, 5000) + try { + await delay(25) + assert.equal(exclusiveEntered, false, "maintenance must wait for the pending database read") + } finally { + release() + await maintenance + } + const result = await following + assert.equal(result.isError, true) + assert.match(JSON.stringify(result), /RESET_REQUIRED/) + assert.doesNotMatch(JSON.stringify(result), /"cursor"/) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_read_leases WHERE connection_id=$1", [access.connection.id])).rows[0].count, 0) + } finally { release(); await following.catch(() => undefined) } + } finally { pool.query = original; await follower.close() } + } + const logsSdkAcceptance = await import("./logs-follow-sdk-cancellation-acceptance.js") + await logsSdkAcceptance.verifyLogsFollowSdkCancellation(app, endpoint, actor) + await logsSdkAcceptance.verifyLogsScopedSdkReads(app, endpoint, actor) + const stdio = new Client({ name: "stdio-integration", version: "1.0.0" }) + try { + await stdio.connect(new StdioClientTransport({ command: process.execPath, args: ["--import", "tsx", fileURLToPath(new URL("../../scripts/mcp-stdio.ts", import.meta.url))], env: { MCP_URL: endpoint, MCP_TOKEN: rotated.token }, stderr: "pipe" })) + assert.ok((await stdio.listTools()).tools.some(tool => tool.name === "mm_ip_settings_list")) + assert.ok(!(await stdio.callTool({ name: "mm_ip_settings_list", arguments: { serverId: 1 } })).isError) + console.log("✓ Official SDK stdio bridge initializes and calls the same authorized HTTP tools") + } finally { await stdio.close() } + const clientId = randomUUID(), redirectUri = "http://localhost:45678/callback", verifier = "v".repeat(64) + await pool.query("INSERT INTO mcp_oauth_clients(id,name,redirect_uris) VALUES($1,'integration',$2::jsonb)", [clientId, JSON.stringify([redirectUri])]) + const parameters = { response_type: "code", client_id: clientId, redirect_uri: redirectUri, resource: endpoint, scope: "mcp", state: "test-state", code_challenge_method: "S256", code_challenge: createHash("sha256").update(verifier).digest("base64url") } + const authorization = await app.inject({ url: "/oauth/authorize?" + new URLSearchParams(parameters) }) + assert.equal(authorization.statusCode, 302) + const requestId = new URL(authorization.headers.location!).searchParams.get("oauthRequest")! + const { SignJWT } = await import("jose"), { env } = await import("../../config.js") + const jwt = await new SignJWT({ sub: actor.id, apps: ["mm"], permissions: actor.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime(Math.floor(fixtureAuthExpiresAt / 1000)).sign(new TextEncoder().encode(env.jwtSecret)) + await (await import("../../test/mcp-host-files.js")).verifyMcpHostFiles(actor,app) + const readinessAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Actual readiness handshake", scope: { ...input.scope, operationIds: ["servers.list"] } }), actor) + const readinessHeaders = { authorization: "Bearer " + jwt } + const simulatorHeaders = { authorization: "Bearer " + await new SignJWT({ sub: "policy-preview-admin", apps: ["mm"], permissions: ["mm:mcp:admin"] }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) } + const policyPreview = (payload: Record) => app!.inject({ method: "POST", url: "/api/mcp/policy/preview", headers: simulatorHeaders, payload }) + const simulatedServerRead = { connectionId: readinessAccess.connection.id, operationId: "servers.list", target: { scope: "servers", serverIds: [1] } } + const simulation = await policyPreview(simulatedServerRead) + assert.equal(simulation.statusCode, 200, simulation.body) + assert.equal(simulation.json().result.decision, "allowed", "an administrator simulates the stored owner's rights, not the administrator's rights") + assert.equal(simulation.json().executionAvailable, false) + assert.equal((await policyPreview({ ...simulatedServerRead, ownerPermissions: ["mm:settings:admin"] })).statusCode, 400) + const ready = await app.inject({ method: "POST", url: `/api/mcp/connections/${readinessAccess.connection.id}/check`, headers: readinessHeaders, payload: {} }) + assert.equal(ready.statusCode, 200, ready.body) + assert.equal(ready.json().ok, true, ready.body); assert.equal(ready.json().checks.protocolHandshake, true) + assert.equal(ready.json().protocolProbe.status, "passed"); assert.equal(ready.json().protocolProbe.clientCredentialsVerified, false) + assert.equal(ready.json().protocolProbe.capabilitiesRead, "skipped") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_credentials WHERE connection_id=$1 AND client_id='mcp-readiness-probe'", [readinessAccess.connection.id])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_grants WHERE connection_id=$1", [readinessAccess.connection.id])).rows[0].n, 0) + const duplicateReady = await app.inject({ method: "POST", url: `/api/mcp/connections/${readinessAccess.connection.id}/check`, headers: readinessHeaders, payload: {} }) + assert.equal(duplicateReady.statusCode, 429) + await setConnectionStatus(readinessAccess.connection.id, "paused", actor) + const pausedSimulation = await policyPreview(simulatedServerRead) + assert.equal(pausedSimulation.statusCode, 200, pausedSimulation.body) + assert.equal(pausedSimulation.json().result.decision, "denied") + assert.ok(pausedSimulation.json().result.reasons.some((reason: { code: string }) => reason.code === "CONNECTION_INACTIVE")) + const pausedReady = await app.inject({ method: "POST", url: `/api/mcp/connections/${readinessAccess.connection.id}/check`, headers: readinessHeaders, payload: {} }) + assert.equal(pausedReady.statusCode, 200); assert.equal(pausedReady.json().protocolProbe.status, "skipped") + const readableAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Actual readiness capability read", scope: { ...input.scope, operationIds: ["servers.list", "capabilities.read"] } }), actor) + const readable = await app.inject({ method: "POST", url: `/api/mcp/connections/${readableAccess.connection.id}/check`, headers: readinessHeaders, payload: {} }) + assert.equal(readable.statusCode, 200, readable.body) + assert.equal(readable.json().ok, true, readable.body) + assert.equal(readable.json().protocolProbe.capabilitiesRead, "passed") + assert.equal(readable.json().protocolProbe.credentialType, "temporary-capabilities-read") + const readinessReads = (await pool.query("SELECT tool FROM mcp_requests WHERE connection_id=$1 AND tool NOT LIKE 'protocol:%'", [readableAccess.connection.id])).rows + assert.deepEqual(readinessReads.map(row => row.tool), ["mm_capabilities_get"]) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_credentials WHERE connection_id=$1 AND client_id='mcp-readiness-probe'", [readableAccess.connection.id])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_grants WHERE connection_id=$1", [readableAccess.connection.id])).rows[0].n, 0) + const emptyGrantAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Readiness has no operation authority" }), actor) + const { withMcpProbeCredential } = await import("./readiness.js") + let endedProbeToken = "" + await assert.rejects(withMcpProbeCredential(emptyGrantAccess.connection.id, actor, async token => { + endedProbeToken = token + const probePrincipal = await authenticateMcpToken(token) + const { effectiveMcpConnection } = await import("./store.js") + assert.deepEqual((await effectiveMcpConnection(probePrincipal)).policy.scope.operationIds, []) + await assert.rejects(invokeMcpTool(probePrincipal, "mm_ip_settings_list", { serverId: 1 })) + throw new Error("Simulated interrupted handshake") + })) + await assert.rejects(authenticateMcpToken(endedProbeToken)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_grants WHERE connection_id=$1", [emptyGrantAccess.connection.id])).rows[0].n, 0) + const readinessAudit = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1", [emptyGrantAccess.connection.id])).rows + assert.ok(!JSON.stringify(readinessAudit).includes(endedProbeToken)) + console.log("✓ Actual readiness initialize/tools-list and optional capabilities read over public HTTP: narrow grant, cleanup on success/error, cooldown and paused precheck skip") + const deviceConnection = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Web interfaces", scope: { ...input.scope, operationIds: ["ros.bridge.list", "ros.bridge.create"] } }), actor) + const deviceHeaders = { authorization: "Bearer " + jwt } + const deviceCatalog = await app.inject({ url: "/api/mcp/devices/catalog?connectionId=" + deviceConnection.connection.id, headers: deviceHeaders }) + assert.equal(deviceCatalog.statusCode, 200, deviceCatalog.body) + assert.deepEqual(deviceCatalog.json().servers.map((server: { id: number }) => server.id), [1]) + assert.equal(deviceCatalog.json().resources.find((resource: { id: string }) => resource.id === "bridge").create, true) + assert.equal((await app.inject({ url: `/api/mcp/devices/2/bridge?connectionId=${deviceConnection.connection.id}`, headers: deviceHeaders })).statusCode, 403) + const devicePlan = await app.inject({ method: "POST", url: "/api/mcp/devices/1/bridge/plan", headers: deviceHeaders, payload: { connectionId: deviceConnection.connection.id, action: "create", fields: { name: "own-web-test", disabled: true } } }) + assert.equal(devicePlan.statusCode, 200, devicePlan.body); assert.equal(devicePlan.json().state, "awaiting-approval") + const invalidDevice = await app.inject({ method: "POST", url: "/api/mcp/devices/1/bridge/plan", headers: deviceHeaders, payload: { connectionId: deviceConnection.connection.id, action: "create", fields: { name: "own-web-test", script: "forbidden" } } }) + assert.equal(invalidDevice.statusCode, 400, invalidDevice.body) + console.log("✓ Web interface catalog enforces server scope; typed creation requires approval and rejects arbitrary fields") + const consent = await app.inject({ method: "POST", url: "/api/mcp/oauth/consent", headers: { authorization: "Bearer " + jwt }, payload: { requestId, connectionId: issued.connection.id, approve: true } }) + assert.equal(consent.statusCode, 200, consent.body) + const redirect = new URL(consent.json().redirectUrl) + assert.equal(redirect.searchParams.get("state"), "test-state") + const codePayload = { grant_type: "authorization_code", client_id: clientId, redirect_uri: redirectUri, resource: endpoint, code: redirect.searchParams.get("code")!, code_verifier: verifier } + const tokenRequest = (payload: Record) => app!.inject({ method: "POST", url: "/oauth/token", headers: { "content-type": "application/x-www-form-urlencoded" }, payload: new URLSearchParams(payload).toString() }) + assert.equal((await tokenRequest({ ...codePayload, code_verifier: "x".repeat(64) })).statusCode, 400) + assert.equal((await tokenRequest({ ...codePayload, resource: "https://wrong.invalid/mcp" })).statusCode, 400) + const exchanged = await tokenRequest(codePayload) + assert.equal(exchanged.statusCode, 200, exchanged.body) + await authenticateMcpToken(exchanged.json().access_token) + assert.equal((await tokenRequest(codePayload)).statusCode, 400) + const refreshPayload = { grant_type: "refresh_token", client_id: clientId, resource: endpoint, refresh_token: exchanged.json().refresh_token } + const refreshed = await tokenRequest(refreshPayload) + assert.equal(refreshed.statusCode, 200) + assert.equal((await tokenRequest(refreshPayload)).statusCode, 400) + await assert.rejects(authenticateMcpToken(refreshed.json().access_token)) + console.log("✓ OAuth consent, PKCE, resource binding, one-use code, refresh rotation and reuse-family revocation") + const currentPrincipal = await authenticateMcpToken(rotated.token) + await assert.rejects(invokeMcpTool(currentPrincipal, "mm_operation_recovery_plan", { operationId: operation.id }), error => error instanceof Error && "code" in error && error.code === "STATE_CONFLICT") + const beforeRecovery = settings["rp-filter"] + const forward = await invokeMcpTool(currentPrincipal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } }) as { planId: string } + await approvePlan(forward.planId, actor, true) + const forwardOperation = await applyPlan(currentPrincipal, forward.planId, "recovery-forward") + const waitOperation = async (id: string) => { + // This observes asynchronous execution; it does not set an operation TTL. + // Genuine settings/drain execution can exceed the former 100 x 30ms loop. + const observationDeadline = performance.now() + 30000 + while (performance.now() < observationDeadline) { + const value = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state + if (!["pending", "running"].includes(value)) return value + await delay(30) + } + return "timeout" + } + assert.equal(await waitOperation(forwardOperation.id), "applied") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(107,'native-reconciliation','127.0.0.1',$1,false,'fixture')", [port]) + const reconcileAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Native reconciliation fences", scope: { serverIds: [107], operationIds: ["ros.ip_settings.update", "operations.reconcile"], allowGlobal: false } }), actor) + const reconcilePrincipal = await authenticateMcpToken(reconcileAccess.token) + try { + const reconcilePlan = await invokeMcpTool(reconcilePrincipal, "mm_ip_settings_update_plan", { serverId: 107, fields: { "rp-filter": "loose" } }) as { planId: string } + await approvePlan(reconcilePlan.planId, actor, true) + const reconcileOperation = await applyPlan(reconcilePrincipal, reconcilePlan.planId, randomUUID()) + assert.equal(await waitOperation(reconcileOperation.id), "applied") + const reconcileWrites = writes + const nativeReconcile = await invokeMcpTool(reconcilePrincipal, "mm_operation_reconcile", { operationId: reconcileOperation.id }) as { state: string; automaticRetry: boolean; results: { matchesVerifiedCheckpoint: boolean; comparisonScope: string; sourceUnchangedDuringRead: boolean; originalSourceIdentityVerified: boolean }[] } + assert.equal(nativeReconcile.state, "applied"); assert.equal(nativeReconcile.automaticRetry, false) + assert.equal(nativeReconcile.results[0].matchesVerifiedCheckpoint, true) + assert.equal(nativeReconcile.results[0].comparisonScope, "visible-resource-configuration") + assert.equal(nativeReconcile.results[0].sourceUnchangedDuringRead, true) + assert.equal(nativeReconcile.results[0].originalSourceIdentityVerified, false) + const reconcileAbort = new AbortController(), beforeReconcileCancel = reconcileSettingsReads + reconcileReadHook = async () => reconcileAbort.abort() + await assert.rejects(invokeMcpTool(reconcilePrincipal, "mm_operation_reconcile", { operationId: reconcileOperation.id }, reconcileAbort.signal), (error: { code?: string }) => error.code === "READ_CANCELLED") + assert.equal(reconcileSettingsReads, beforeReconcileCancel + 1) + reconcileReadHook = () => setConnectionStatus(reconcileAccess.connection.id, "paused", actor) + await assert.rejects(invokeMcpTool(reconcilePrincipal, "mm_operation_reconcile", { operationId: reconcileOperation.id })) + await setConnectionStatus(reconcileAccess.connection.id, "active", actor) + reconcileReadHook = async () => { + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=107") + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=107") + } + await assert.rejects(invokeMcpTool(reconcilePrincipal, "mm_operation_reconcile", { operationId: reconcileOperation.id }), (error: { code?: string }) => error.code === "STATE_CONFLICT") + assert.equal(writes, reconcileWrites) + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [reconcileOperation.id])).rows[0].state, "applied") + console.log("✓ Native reconciliation fences cancellation, paused access and endpoint ABA during HTTP without replay") + } finally { + reconcileReadHook = undefined + await setConnectionStatus(reconcileAccess.connection.id, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=107") + } + const recovery = await invokeMcpTool(currentPrincipal, "mm_operation_recovery_plan", { operationId: forwardOperation.id }) as { planId: string; state: string } + assert.equal(recovery.state, "awaiting-approval") + await approvePlan(recovery.planId, actor, true) + const restored = await applyPlan(currentPrincipal, recovery.planId, "recovery-once") + assert.equal(await waitOperation(restored.id), "applied"); assert.equal(settings["rp-filter"], beforeRecovery) + assert.equal((await applyPlan(currentPrincipal, recovery.planId, "recovery-once")).id, restored.id) + console.log("✓ Encrypted checkpoints support approved inverse plans, reject external drift and preserve idempotency") + const usersAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Users writer", permissions: ["mm:users:write"], scope: { serverIds: [1], operationIds: ["users.create", "users.update", "users.permissions.update"], allowGlobal: true } }), actor) + const usersPrincipal = await authenticateMcpToken(usersAccess.token) + const userData = { name: " MCP catalog fixture ", login: "mcp-" + randomUUID(), email: "fixture@example.invalid" } + await assert.rejects(invokeMcpTool(usersPrincipal, "mm_users_create_plan", { user: { ...userData, role: "admin" } })) + const userPlan = await invokeMcpTool(usersPrincipal, "mm_users_create_plan", { user: userData }) as { planId: string } + await approvePlan(userPlan.planId, actor, true) + assert.equal(await waitOperation((await applyPlan(usersPrincipal, userPlan.planId, "create-catalog-user")).id), "applied") + const catalogUser = (await pool.query("SELECT id,role FROM app_users WHERE login=$1", [userData.login])).rows[0] + assert.equal(catalogUser.role, "viewer") + await assert.rejects(invokeMcpTool(usersPrincipal, "mm_users_permissions_update_plan", { id: catalogUser.id, permissions: { role: "admin" } })) + const rolesAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Catalog roles", permissions: ["mm:settings:admin"], scope: { serverIds: [1], operationIds: ["users.permissions.update"], allowGlobal: true } }), actor) + const rolesPrincipal = await authenticateMcpToken(rolesAccess.token) + await assert.rejects(invokeMcpTool(rolesPrincipal, "mm_users_permissions_update_plan", { id: catalogUser.id, permissions: { servers: [{ serverId: "2", level: "read" }] } })) + const rolesPlan = await invokeMcpTool(rolesPrincipal, "mm_users_permissions_update_plan", { id: catalogUser.id, permissions: { role: "operator", servers: [{ serverId: "1", level: "read" }] } }) as { planId: string } + await approvePlan(rolesPlan.planId, actor, true) + assert.equal(await waitOperation((await applyPlan(rolesPrincipal, rolesPlan.planId, "catalog-role-only")).id), "applied") + assert.equal((await pool.query("SELECT role FROM app_users WHERE id=$1", [catalogUser.id])).rows[0].role, "operator") + const nameOnly = await invokeMcpTool(usersPrincipal, "mm_users_update_plan", { id: catalogUser.id, user: { name: "Renamed only" } }) as { planId: string } + await approvePlan(nameOnly.planId, actor, true) + assert.equal(await waitOperation((await applyPlan(usersPrincipal, nameOnly.planId, "name-without-privileges")).id), "applied") + const preservedUser = (await pool.query("SELECT role,servers_json FROM app_users WHERE id=$1", [catalogUser.id])).rows[0] + assert.equal(preservedUser.role, "operator") + assert.deepEqual(typeof preservedUser.servers_json === "string" ? JSON.parse(preservedUser.servers_json) : preservedUser.servers_json, [{ serverId: "1", level: "read" }]) + for (const type of ["bridge", "vlan", "vxlan", "ipsec"]) await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type) VALUES($1,$2,1,$3,$4)", [randomUUID(), catalogUser.id, "fixture-" + type, type]) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM user_interface_bindings WHERE user_id=$1", [catalogUser.id])).rows[0].n, 4) + console.log("✓ Catalog roles require a separate admin action; scoped permissions, normalized read-back and real interface types work") + await verifyMcpGrantExpiryBounds(app, actor, deviceHeaders) + const grantOperations = ["users.list", "users.get", "servers.list", "ros.ip_settings.update", "changes.read", "changes.cancel", "operations.read"] + const grantAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Temporary scope acceptance", permissions: ["mm:users:read", "mm:network:write", "mm:servers:read"], scope: { allowGlobal: true, serverIds: [1, 2], operationIds: [...grantOperations, "interfaces.update"] } }), actor) + const baseGrantPrincipal = await authenticateMcpToken(grantAccess.token) + const outsidePlan = await invokeMcpTool(baseGrantPrincipal, "mm_ip_settings_update_plan", { serverId: 2, fields: { "rp-filter": settings["rp-filter"] } }) as { planId: string } + await approvePlan(outsidePlan.planId, actor, true) + const outsideOperation = await applyPlan(baseGrantPrincipal, outsidePlan.planId, "outside-temporary-grant") + assert.equal(await waitOperation(outsideOperation.id), "applied") + const cancelOutside = await invokeMcpTool(baseGrantPrincipal, "mm_ip_settings_update_plan", { serverId: 2, fields: { "rp-filter": settings["rp-filter"] } }) as { planId: string } + const issuedGrant = await app.inject({ method: "POST", url: `/api/mcp/connections/${grantAccess.connection.id}/grants`, headers: deviceHeaders, payload: { id: randomUUID(), startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 600000).toISOString(), scope: { allowGlobal: true, serverIds: [1], operationIds: grantOperations } } }) + assert.equal(issuedGrant.statusCode, 200, issuedGrant.body) + const grantPrincipal = await authenticateMcpToken(issuedGrant.json().token) + const hiddenBinding = randomUUID() + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type) VALUES($1,$2,2,'hidden-binding','ether')", [hiddenBinding, catalogUser.id]) + const grantUser = await invokeMcpTool(grantPrincipal, "mm_users_get", { id: catalogUser.id }) as { user: { incomplete: boolean; bindings: { serverId: number }[] } } + assert.equal(grantUser.user.incomplete, true) + assert.ok(grantUser.user.bindings.every(binding => binding.serverId === 1)) + const grantList = await invokeMcpTool(grantPrincipal, "mm_users_list", {}) as { items: { bindings: { serverId: number }[] }[] } + assert.ok(grantList.items.every(user => user.bindings.every(binding => binding.serverId === 1))) + assert.deepEqual((await invokeMcpTool(grantPrincipal, "mm_servers_list", {}) as { items: { id: number }[] }).items.map(item => item.id), [1]) + for (const [tool, body] of [["mm_change_get", { planId: outsidePlan.planId }], ["mm_change_cancel", { planId: cancelOutside.planId }], ["mm_operation_get", { operationId: outsideOperation.id }]] as const) await assert.rejects(invokeMcpTool(grantPrincipal, tool, body)) + assert.equal((await pool.query("SELECT state FROM mcp_plans WHERE id=$1", [cancelOutside.planId])).rows[0].state, "awaiting-approval") + const insidePlan = await invokeMcpTool(grantPrincipal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": settings["rp-filter"] } }) as { planId: string } + assert.equal((await invokeMcpTool(grantPrincipal, "mm_change_get", { planId: insidePlan.planId }) as { id: string }).id, insidePlan.planId) + const grantClient = new Client({ name: "temporary-grant", version: "1.0.0" }) + try { + await grantClient.connect(new StreamableHTTPClientTransport(new URL(endpoint), { requestInit: { headers: { Authorization: "Bearer " + issuedGrant.json().token } } })) + const tools = (await grantClient.listTools()).tools.map(tool => tool.name) + assert.ok(tools.includes("mm_users_get")); assert.ok(!tools.includes("mm_interfaces_update_plan")) + } finally { await grantClient.close() } + await pool.query("DELETE FROM user_interface_bindings WHERE id=$1", [hiddenBinding]) + console.log("✓ Temporary grants narrow catalogs and SDK discovery; hidden user bindings and out-of-scope plan/operation details or cancellation are denied") + const healthAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Post-write health", scope: { ...input.scope, operationIds: ["ros.ip_settings.update"] } }), actor) + const healthPrincipal = await authenticateMcpToken(healthAccess.token) + const healthPlan = async (resource: string, id: string, field: string, identity?: Record) => { + const plan = await invokeMcpTool(healthPrincipal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": settings["rp-filter"] } }) as { planId: string } + const evidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0].evidence.sealed)) + evidence.healthChecks = [{ serverId: 1, resource, id, field, identity }] + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [plan.planId, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(evidence)) })]) + await approvePlan(plan.planId, actor, true) + return plan + } + try { + for (const [resource, id, field] of [["ospf_neighbor", "*neighbor", "state"], ["ipv4_route", "*healthroute", "active"], ["bfd_session", "*bfd", "bfd-up"]]) { + healthState = true; failHealthAfterWrite = true + const plan = await healthPlan(resource, id, field), before: number = writes + const operation = await applyPlan(healthPrincipal, plan.planId, randomUUID()) + assert.equal(await waitOperation(operation.id), "partial"); assert.equal(writes, before + 1) + assert.equal((await pool.query("SELECT error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0].error_code, "VERIFICATION_FAILED") + await applyPlan(healthPrincipal, plan.planId, "another-key"); await delay(30); assert.equal(writes, before + 1) + } + failHealthAfterWrite = false; healthState = false + const preflight = await healthPlan("ospf_neighbor", "*neighbor", "state"), before = writes + assert.equal(await waitOperation((await applyPlan(healthPrincipal, preflight.planId, randomUUID())).id), "rejected"); assert.equal(writes, before) + healthState = true + const reusedBfd = await healthPlan("bfd_session", "*bfd", "bfd-up", { "remote-address": healthBfdRemote }) + healthBfdRemote = "192.0.2.99%foreign" + assert.equal(await waitOperation((await applyPlan(healthPrincipal, reusedBfd.planId, randomUUID())).id), "rejected"); assert.equal(writes, before) + healthBfdRemote = "192.0.2.2%fixture-ospf" + healthState = true; loseHealthResponse = true + const unavailable = await healthPlan("ospf_neighbor", "*neighbor", "state") + assert.equal(await waitOperation((await applyPlan(healthPrincipal, unavailable.planId, randomUUID())).id), "unknown"); assert.equal(writes, before + 1) + console.log("✓ Post-write routing health: lost Full/active is partial, unavailable evidence is unknown, failed preflight writes nothing, no replay") + } finally { healthState = true; failHealthAfterWrite = false; healthUnavailable = false; loseHealthResponse = false; healthBfdRemote = "192.0.2.2%fixture-ospf" } + const limited = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Budget", limits: { callsPerMinute: 2, concurrentReads: 100 } }), actor) + const limitedPrincipal = await authenticateMcpToken(limited.token) + const concurrent = await Promise.allSettled(Array.from({ length: 12 }, () => invokeMcpTool(limitedPrincipal, "mm_ip_settings_list", { serverId: 1 }))) + assert.equal(concurrent.filter(value => value.status === "fulfilled").length, 2) + console.log("✓ Concurrent calls cannot overspend the atomic call budget") + const { reserveProtocolEnvelope, recordUnauthorizedProtocol } = await import("./protocol-limits.js") + const envelopes = await Promise.all(Array.from({ length: 16 }, () => reserveProtocolEnvelope(limitedPrincipal))) + assert.equal(envelopes.filter(value => value.allowed).length, 10) + const rejectedBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE tool='protocol:unauthorized'")).rows[0].n + for (let attempt = 0; attempt < 8; attempt++) await recordUnauthorizedProtocol("192.0.2.99", "UNAUTHORIZED") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE tool='protocol:unauthorized'")).rows[0].n, rejectedBefore + 1) + assert.equal((await pool.query("SELECT max(attempts)::int AS n FROM mcp_unauthorized_buckets WHERE reason='UNAUTHORIZED'")).rows[0].n, 8) + console.log("✓ Protocol metadata is atomically bounded; unauthorized repeats preserve counters without storing tokens") + const aliasConnection = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Interface alias acceptance", scope: { ...input.scope, operationIds: ["interfaces.update", "firewall.update"] } }), actor) + const aliasPrincipal = await authenticateMcpToken(aliasConnection.token) + await assert.rejects(invokeMcpTool(aliasPrincipal, "mm_interfaces_update_plan", { serverId: 1, resource: "ip_settings", id: "*fixture", fields: { comment: "out-of-allowlist" } })) + await assert.rejects(invokeMcpTool(aliasPrincipal, "mm_interfaces_update_plan", { serverId: 1, resource: "bridge", id: "*fixture", fields: { script: "not-allowed" } })) + const aliasPlan = await invokeMcpTool(aliasPrincipal, "mm_interfaces_update_plan", { serverId: 1, resource: "bridge", id: "*fixture", fields: { comment: "reviewed-alias" } }) as { planId: string } + await approvePlan(aliasPlan.planId, actor, true) + assert.equal(await waitOperation((await applyPlan(aliasPrincipal, aliasPlan.planId, "interface-alias")).id), "applied") + assert.equal(bridge.comment, "reviewed-alias") + console.log("✓ Named interface/firewall tools reuse typed native plans; resources and fields cannot escape their allowlists") + const telemetryActor = { ...actor, permissions: [...actor.permissions, "mm:uptime:admin", "mm:traffic:admin"] } + const telemetryOps = ["traffic.settings.read", "traffic.settings.update", "uptime.settings.update", "netflow.settings.update", "uptime.ping.probes.read", "uptime.ping.probes.replace", "uptime.speed.probes.read", "uptime.speed.probes.replace", "servers.api-ping.settings.update", "internet-path.settings.update"] + // NetFlow reserves four writes, seven successful updates reserve one each, + // and the stale Internet path plan reserves one before execution rejects it. + // Keep the production default budget unchanged; this fixture needs 12. + const telemetry = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Telemetry acceptance", limits: { ...input.limits, mutationsPerHour: 12 }, permissions: ["mm:uptime:write", "mm:traffic:write", "mm:servers:write", "mm:network:write"], scope: { allowGlobal: true, serverIds: [1, 2], operationIds: telemetryOps } }), telemetryActor) + const telemetryPrincipal = await authenticateMcpToken(telemetry.token) + await pool.query("DELETE FROM traffic_settings WHERE id=1") + assert.deepEqual(await invokeMcpTool(telemetryPrincipal, "mm_traffic_settings_get", {}), { + enabled: true, intervalSec: 30, retentionDays: 14, lastCollectedAt: null, + lastDurationMs: null, lastError: null, collectorRunning: true, + }) + assert.equal((await pool.query("SELECT id FROM traffic_settings WHERE id=1")).rowCount, 0) + const applyTelemetry = async (tool: string, value: Record, afterPrepare?: () => Promise) => { + const plan = await invokeMcpTool(telemetryPrincipal, tool, value) as { planId: string } + await afterPrepare?.() + await approvePlan(plan.planId, telemetryActor, true) + const operation = await applyPlan(telemetryPrincipal, plan.planId, randomUUID()) + assert.equal(await waitOperation(operation.id), "applied", JSON.stringify((await pool.query("SELECT error_code,result FROM mcp_operations WHERE id=$1", [operation.id])).rows[0])) + return operation + } + await applyTelemetry("mm_traffic_settings_update_plan", { settings: { enabled: false, intervalSec: 37 } }, async () => { + await pool.query("UPDATE traffic_settings SET last_collected_at=now(),last_duration_ms=123,last_error='synthetic collector status',updated_at=now() WHERE id=1") + }) + assert.deepEqual((await pool.query("SELECT enabled,interval_sec FROM traffic_settings WHERE id=1")).rows[0], { enabled: false, interval_sec: 37 }) + const storedTrafficSettings = await invokeMcpTool(telemetryPrincipal, "mm_traffic_settings_get", {}) as Record + assert.deepEqual(storedTrafficSettings, { + enabled: false, intervalSec: 37, retentionDays: 14, lastCollectedAt: storedTrafficSettings.lastCollectedAt, + lastDurationMs: 123, lastError: "synthetic collector status", collectorRunning: false, + }) + assert.ok(typeof storedTrafficSettings.lastCollectedAt === "string" && Number.isFinite(Date.parse(storedTrafficSettings.lastCollectedAt))) + await applyTelemetry("mm_uptime_settings_update_plan", { settings: { resourcesEnabled: false, pingEnabled: false, speedEnabled: false, intervalSec: 317 } }, async () => { + await pool.query("UPDATE uptime_settings SET last_collected_at=now(),last_duration_ms=123,last_error='synthetic collector status',updated_at=now() WHERE id=1") + }) + assert.deepEqual((await pool.query("SELECT resources_enabled,ping_enabled,speed_enabled,interval_sec FROM uptime_settings WHERE id=1")).rows[0], { resources_enabled: false, ping_enabled: false, speed_enabled: false, interval_sec: 317 }) + await applyTelemetry("mm_netflow_settings_update_plan", { settings: { enabled: false, topN: 23, mapServiceMinSharePct: 7 } }) + await applyTelemetry("mm_servers_api_ping_settings_update_plan", { settings: { enabled: false, intervalSec: 131 } }, async () => { + await pool.query("UPDATE servers_api_ping_settings SET last_collected_at=now(),last_duration_ms=123,last_error='synthetic collector status',updated_at=now() WHERE id=1") + }) + assert.deepEqual((await pool.query("SELECT enabled,interval_sec FROM servers_api_ping_settings WHERE id=1")).rows[0], { enabled: false, interval_sec: 131 }) + console.log("✓ Traffic, uptime and API Ping settings tolerate actual collector metadata changes between prepare/apply and persist requested configuration with ordinary scheduler activity") + const stalePathPlan = await invokeMcpTool(telemetryPrincipal, "mm_internet_path_settings_update_plan", { settings: { enabled: false, intervalSec: 368 } }) as { planId: string } + await approvePlan(stalePathPlan.planId, telemetryActor, true) + await pool.query("UPDATE internet_path_settings SET interval_sec=interval_sec+1 WHERE id=1") + const changedPathConfig = (await pool.query("SELECT enabled,interval_sec,retention_days FROM internet_path_settings WHERE id=1")).rows[0] + const stalePathOperation = await applyPlan(telemetryPrincipal, stalePathPlan.planId, randomUUID()) + assert.equal(await waitOperation(stalePathOperation.id), "rejected") + assert.equal((await pool.query("SELECT error_code FROM mcp_operations WHERE id=$1", [stalePathOperation.id])).rows[0].error_code, "STATE_CONFLICT") + assert.deepEqual((await pool.query("SELECT enabled,interval_sec,retention_days FROM internet_path_settings WHERE id=1")).rows[0], changedPathConfig) + await applyTelemetry("mm_internet_path_settings_update_plan", { settings: { enabled: false, intervalSec: 367, retentionDays: 9 } }, async () => { + await pool.query("UPDATE internet_path_settings SET last_collected_at=now(),last_duration_ms=123,last_error='synthetic collector status',updated_at=now() WHERE id=1") + }) + console.log("✓ Internet path plans tolerate collector status updates and reject changed configuration without writes") + const pingProbe = { id: "mcp-ping-fixture", srcServerId: 1, srcInterface: "ether1", name: "MCP disabled Ping", target: "192.0.2.1", enabled: false, intervalSec: 1 } + await applyTelemetry("mm_ping_probes_replace_plan", { probes: [pingProbe] }) + assert.equal((await pool.query("SELECT interval_sec FROM uptime_probes WHERE id=$1", [pingProbe.id])).rows[0].interval_sec, 5) + await assert.rejects(invokeMcpTool(telemetryPrincipal, "mm_ping_probes_replace_plan", { probes: [{ ...pingProbe, srcServerId: 999999 }] })) + const probeProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,reason,created_by) VALUES($1,$2::jsonb,'fixture',$3)", [probeProtection, JSON.stringify({ kind: "ping-probe", id: pingProbe.id, protection: "deny" }), actor.id]) + const { replaceProbes } = await import("../../services/uptime-collector.js") + await assert.rejects(replaceProbes([])) + await assert.rejects(invokeMcpTool(telemetryPrincipal, "mm_ping_probes_replace_plan", { probes: [] })) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [probeProtection]) + const speedProbe = { id: "mcp-speed-fixture", srcServerId: 1, dstServerId: 2, srcInterface: "ether1", dstInterface: "ether1", protocol: "tcp", direction: "both", durationSec: 7, enabled: false } + await applyTelemetry("mm_speed_probes_replace_plan", { probes: [speedProbe] }) + await pool.query("UPDATE uptime_speed_probes SET last_tx_avg_mbps=100,last_rx_avg_mbps=200,last_run_at=now(),last_status='done' WHERE id=$1", [speedProbe.id]) + await applyTelemetry("mm_speed_probes_replace_plan", { probes: [{ ...speedProbe, durationSec: 8 }] }) + const reset = (await pool.query("SELECT last_tx_avg_mbps,last_rx_avg_mbps,last_run_at FROM uptime_speed_probes WHERE id=$1", [speedProbe.id])).rows[0] + assert.deepEqual(reset, { last_tx_avg_mbps: null, last_rx_avg_mbps: null, last_run_at: null }) + await assert.rejects(invokeMcpTool(telemetryPrincipal, "mm_speed_probes_replace_plan", { probes: [{ ...speedProbe, durationSec: 31 }] })) + const limitedTelemetry = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Limited telemetry", permissions: ["mm:uptime:read"], scope: { allowGlobal: true, serverIds: [1], operationIds: ["uptime.ping.probes.read"] } }), telemetryActor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(limitedTelemetry.token), "mm_ping_probes_get", {})) + console.log("✓ Telemetry settings have actual read-back; probe catalogs enforce full scope, protected deletions, bounded bandwidth duration and measurement reset") + const probeAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Probe service adapters", permissions: ["mm:uptime:read", "mm:uptime:write"], scope: { allowGlobal: false, serverIds: [1, 2], operationIds: ["uptime.speed.runs.read", "uptime.speed.interfaces.read", "uptime.ping.interfaces.read", "uptime.ping.visibility.update"] } }), telemetryActor) + const probePrincipal = await authenticateMcpToken(probeAccess.token) + const probeLimited = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Limited probe readers", mode: "observer", permissions: ["mm:uptime:read"], scope: { allowGlobal: false, serverIds: [1], operationIds: ["uptime.speed.runs.read", "uptime.speed.interfaces.read", "uptime.ping.interfaces.read"] } }), telemetryActor) + const probeLimitedPrincipal = await authenticateMcpToken(probeLimited.token) + await pool.query("INSERT INTO servers(id,name,host,enabled) VALUES(3,'Foreign history fixture','192.0.2.253',false)") + await pool.query("INSERT INTO uptime_speed_test_runs(id,src_server_id,dst_server_id,rx_avg_mbps,ping_loss_pct,created_at) VALUES('speed-visible-z',1,2,0,0,'2026-10-10'),('speed-visible-a',2,1,0,0,'2026-10-10')") + await pool.query("INSERT INTO uptime_speed_test_runs(id,src_server_id,dst_server_id,tx_avg_mbps,created_at) VALUES('speed-foreign-src',3,1,-1,'2026-10-17'),('speed-foreign-dst',1,3,-1,'2026-10-17')") + await pool.query("INSERT INTO uptime_probes(id,src_server_id,name,target,enabled) VALUES('visibility-own',1,'Visibility fixture','192.0.2.1',false),('visibility-foreign',3,'Foreign visibility fixture','192.0.2.3',false)") + probeInterfaceFixtures = true; probeNativeCalls = 0 + try { + type Runs = { runs: { id: string; txAvgMbps: number | null; rxAvgMbps: number | null; afterBtPing: { lossPct: number | null } }[]; incomplete: boolean; nextOffset: number | null } + const history = await invokeMcpTool(probePrincipal, "mm_speed_test_runs_get", { limit: 1 }) as Runs + assert.equal(history.runs[0].id, "speed-visible-z"); assert.equal(history.runs[0].txAvgMbps, null); assert.equal(history.runs[0].rxAvgMbps, 0); assert.equal(history.runs[0].afterBtPing.lossPct, 0) + assert.equal(history.incomplete, true); assert.equal(history.nextOffset, 1) + const nextHistory = await invokeMcpTool(probePrincipal, "mm_speed_test_runs_get", { limit: 1, offset: 1 }) as Runs + assert.equal(nextHistory.runs[0].id, "speed-visible-a") + assert.equal(nextHistory.incomplete, false); assert.equal(nextHistory.nextOffset, null) + assert.deepEqual(await invokeMcpTool(probeLimitedPrincipal, "mm_speed_test_runs_get", {}), { runs: [], incomplete: false, nextOffset: null }) + const beforePingInterfaces = probeNativeCalls + const pingIfaces = await invokeMcpTool(probeLimitedPrincipal, "mm_ping_source_interfaces_get", { serverId: 1, limit: 1 }) as { interfaces: { name: string }[]; unknownInterfaces: { name: string }[]; total: number; incomplete: boolean; nextOffset: number } + assert.equal(pingIfaces.interfaces[0].name, "ether1"); assert.equal(pingIfaces.total, 2); assert.equal(pingIfaces.nextOffset, 1) + assert.deepEqual(pingIfaces.unknownInterfaces.map(row => row.name), ["unknown-probe", "unknown-fallback", "invalid-flag"]); assert.equal(pingIfaces.incomplete, true) + assert.equal(probeNativeCalls - beforePingInterfaces, 3) + const speedIfaces = await invokeMcpTool(probeLimitedPrincipal, "mm_speed_test_interfaces_get", { serverId: 1 }) as { interfaces: { addresses: string[]; addressStateVerified: boolean }[] } + assert.deepEqual(speedIfaces.interfaces[0].addresses, ["192.0.2.10"]); assert.equal(speedIfaces.interfaces[0].addressStateVerified, true) + assert.equal(probeNativeCalls - beforePingInterfaces, 7) + assert.ok(!JSON.stringify(speedIfaces).includes("never-return-probe-key")) + const nativeBefore = probeNativeCalls + await assert.rejects(invokeMcpTool(probeLimitedPrincipal, "mm_speed_test_interfaces_get", { serverId: 2 })); assert.equal(probeNativeCalls, nativeBefore) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + await assert.rejects(invokeMcpTool(probeLimitedPrincipal, "mm_ping_source_interfaces_get", { serverId: 1 })); assert.equal(probeNativeCalls, nativeBefore) + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + probeReadFailure = true + await assert.rejects(invokeMcpTool(probeLimitedPrincipal, "mm_speed_test_interfaces_get", { serverId: 1 })) + probeReadFailure = false + const visibilityArgs = { serverId: 1, probeId: "visibility-own", showOnDashboard: false } + await assert.rejects(invokeMcpTool(probePrincipal, "mm_ping_probe_visibility_plan", { ...visibilityArgs, probeId: "visibility-foreign" })) + const protectedVisibility = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protectedVisibility, JSON.stringify({ kind: "ping-probe", id: "visibility-own", protection: "deny" }), actor.id]) + try { + await assert.rejects(invokeMcpTool(probePrincipal, "mm_ping_probe_visibility_plan", visibilityArgs)) + await assert.rejects((await import("../../services/uptime-collector.js")).updateProbeShowOnDashboard("visibility-own", false)) + } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectedVisibility]) } + const staleVisibility = await invokeMcpTool(probePrincipal, "mm_ping_probe_visibility_plan", visibilityArgs) as { planId: string } + await approvePlan(staleVisibility.planId, telemetryActor, true) + await pool.query("UPDATE uptime_probes SET target='192.0.2.2' WHERE id='visibility-own'") + assert.equal(await waitOperation((await applyPlan(probePrincipal, staleVisibility.planId, randomUUID())).id), "rejected") + const visibility = await invokeMcpTool(probePrincipal, "mm_ping_probe_visibility_plan", visibilityArgs) as { planId: string } + await approvePlan(visibility.planId, telemetryActor, true) + const beforeVisibilityCalls = probeNativeCalls + assert.equal(await waitOperation((await applyPlan(probePrincipal, visibility.planId, randomUUID())).id), "applied"); assert.equal(probeNativeCalls, beforeVisibilityCalls) + assert.deepEqual((await pool.query("SELECT show_on_dashboard,enabled,target FROM uptime_probes WHERE id='visibility-own'")).rows[0], { show_on_dashboard: false, enabled: false, target: "192.0.2.2" }) + } finally { + probeInterfaceFixtures = false; probeReadFailure = false + await pool.query("DELETE FROM uptime_speed_test_runs WHERE id LIKE 'speed-visible-%' OR id LIKE 'speed-foreign-%'") + await pool.query("DELETE FROM uptime_probes WHERE id IN ('visibility-own','visibility-foreign')") + await pool.query("DELETE FROM servers WHERE id=3") + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + } + console.log("✓ Probe adapters: both history endpoints scoped in SQL before paging/parser, interface effective flags/unknowns/errors, protected/stale visibility plans with read-back and no network dispatch") + const liveAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Bounded live traffic", mode: "observer", permissions: ["mm:traffic:read"], scope: { allowGlobal: false, serverIds: [1], operationIds: ["traffic.live.read"] } }), telemetryActor) + const livePrincipal = await authenticateMcpToken(liveAccess.token) + liveFixtures = true; liveNativeCalls = 0 + try { + const sample = await invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1 }) as { samples: { rxMbps: number; txMbps: number }[]; forwardingVerified: boolean } + assert.equal(sample.samples[0].rxMbps, 2); assert.equal(sample.samples[0].txMbps, 0); assert.equal(sample.forwardingVerified, false) + assert.equal(liveNativeCalls, 2); assert.ok(!JSON.stringify(sample).includes("never-return-live-key")) + const separate = await invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1, interface: "ether1", samples: 2 }) as { samples: unknown[] } + assert.equal(separate.samples.length, 2); assert.equal(liveNativeCalls, 4) + const beforeDenied = liveNativeCalls + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 2 })) + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1, interface: "ether1,ether2" })) + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1, samples: 4 })) + const aborted = new AbortController(); aborted.abort() + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1 }, aborted.signal)) + await pool.query("UPDATE servers SET enabled=false WHERE id=1") + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1 })); assert.equal(liveNativeCalls, beforeDenied) + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + liveIncomplete = true + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1 })) + liveIncomplete = false; liveChangeSource = true + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1, samples: 3 })) + liveChangeSource = false; liveRevokeConnection = liveAccess.connection.id + const beforeRevoke = liveNativeCalls + await assert.rejects(invokeMcpTool(livePrincipal, "mm_traffic_live_get", { serverId: 1, samples: 3 })) + assert.equal(liveNativeCalls, beforeRevoke + 2, "Revocation must stop before another native monitor request") + } finally { + liveFixtures = false; liveIncomplete = false; liveChangeSource = false; liveRevokeConnection = undefined + await pool.query("UPDATE servers SET enabled=true WHERE id=1") + } + console.log("✓ Bounded live traffic: complete sums, single interface, exact zero, no secret payload, scope/disabled/cancellation, bounds, no partial sum and revocation/source drift before next dispatch") + const geoipAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "GeoIP acceptance", permissions: ["mm:traffic:read", "mm:traffic:write"], scope: { allowGlobal: true, serverIds: [1, 2], operationIds: ["geoip.read", "geoip.settings.update"] } }), telemetryActor) + const geoipPrincipal = await authenticateMcpToken(geoipAccess.token) + const { getGeoipSettings, updateGeoipSettings, touchGeoipRunMeta } = await import("../../services/geoip-settings.js") + const geoipStatus = await invokeMcpTool(geoipPrincipal, "mm_geoip_get", {}) as { running: boolean; settings: { enabled: boolean } } + assert.equal(geoipStatus.running, false) + await assert.rejects(invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: {} })) + await assert.rejects(invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: { updateIntervalSec: 60 } })) + await assert.rejects(invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: { mirrorUrl: "https://example.invalid" } })) + const geoipPlan = await invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: { enabled: false, updateIntervalSec: 86400 } }) as { planId: string } + await approvePlan(geoipPlan.planId, telemetryActor, true) + // Collector metadata must not invalidate a reviewed configuration change. + await touchGeoipRunMeta({ lastCheckAt: new Date().toISOString(), lastError: "fixture update observation" }) + assert.equal(await waitOperation((await applyPlan(geoipPrincipal, geoipPlan.planId, randomUUID())).id), "applied") + assert.equal((await getGeoipSettings()).enabled, false) + assert.equal((await getGeoipSettings()).updateIntervalSec, 86400) + assert.equal((await getGeoipSettings()).lastError, "fixture update observation") + const staleGeoip = await invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: { updateIntervalSec: 172800 } }) as { planId: string } + await approvePlan(staleGeoip.planId, telemetryActor, true) + await updateGeoipSettings({ updateIntervalSec: 259200 }) + assert.equal(await waitOperation((await applyPlan(geoipPrincipal, staleGeoip.planId, randomUUID())).id), "rejected") + assert.equal((await getGeoipSettings()).updateIntervalSec, 259200) + const geoipProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,reason,created_by) VALUES($1,$2::jsonb,'fixture',$3)", [geoipProtection, JSON.stringify({ kind: "geoip-settings", id: "1", protection: "deny" }), actor.id]) + try { + await assert.rejects(updateGeoipSettings({ enabled: true })) + await assert.rejects(invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: { enabled: true } })) + assert.equal((await getGeoipSettings()).enabled, false) + } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [geoipProtection]) } + const partialGeoipAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "GeoIP partial scope", permissions: ["mm:traffic:read", "mm:traffic:write"], scope: { allowGlobal: true, serverIds: [1], operationIds: ["geoip.read", "geoip.settings.update"] } }), telemetryActor) + const partialGeoipPrincipal = await authenticateMcpToken(partialGeoipAccess.token) + await assert.rejects(invokeMcpTool(partialGeoipPrincipal, "mm_geoip_get", {})) + await assert.rejects(invokeMcpTool(partialGeoipPrincipal, "mm_geoip_settings_update_plan", { settings: { enabled: true } })) + await pool.query("CREATE FUNCTION mcp_test_ignore_geoip() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RETURN OLD; END'") + await pool.query("CREATE TRIGGER mcp_test_ignore_geoip BEFORE UPDATE ON geoip_settings FOR EACH ROW EXECUTE FUNCTION mcp_test_ignore_geoip()") + try { + const ignored = await invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: { updateIntervalSec: 345600 } }) as { planId: string } + await approvePlan(ignored.planId, telemetryActor, true) + assert.equal(await waitOperation((await applyPlan(geoipPrincipal, ignored.planId, randomUUID())).id), "partial") + assert.equal((await getGeoipSettings()).updateIntervalSec, 259200) + } finally { + await pool.query("DROP TRIGGER mcp_test_ignore_geoip ON geoip_settings") + await pool.query("DROP FUNCTION mcp_test_ignore_geoip()") + } + const geoipCollector = await import("../../services/geoip-update-collector.js") + const savedGeoipDirectory = process.env.GEOIP_DIR + process.env.GEOIP_DIR = (await import("node:path")).join(process.env.MCP_STATE_DIR!, "geoip-fixture") + let mirrorRequests = 0 + geoipCollector.setGeoipFetchForTests(async () => { mirrorRequests++; return new Response(null, { status: 503 }) }) + try { + const enableGeoip = await invokeMcpTool(geoipPrincipal, "mm_geoip_settings_update_plan", { settings: { enabled: true } }) as { planId: string; changes: { warnings: string[] } } + assert.ok(enableGeoip.changes.warnings.some(warning => warning.includes("сразу"))) + await approvePlan(enableGeoip.planId, telemetryActor, true) + assert.equal(await waitOperation((await applyPlan(geoipPrincipal, enableGeoip.planId, randomUUID())).id), "applied") + for (let attempt = 0; attempt < 100 && (mirrorRequests < 2 || geoipCollector.getGeoipUpdateState().running); attempt++) await delay(20) + assert.equal(mirrorRequests, 2); assert.equal(geoipCollector.getGeoipUpdateState().running, false) + const failedMirrorStatus = await invokeMcpTool(geoipPrincipal, "mm_geoip_get", {}) as { settings: { enabled: boolean; lastError: string } } + assert.equal(failedMirrorStatus.settings.enabled, true) + assert.match(failedMirrorStatus.settings.lastError, /HTTP 503/) + assert.equal(mirrorRequests, 2, "Status reads must not restart the failed download") + } finally { + await updateGeoipSettings({ enabled: false }) + await (await import("../../services/scheduler.js")).refreshScheduler() + geoipCollector.resetGeoipUpdateForTests() + if (savedGeoipDirectory === undefined) delete process.env.GEOIP_DIR + else process.env.GEOIP_DIR = savedGeoipDirectory + } + console.log("✓ GeoIP uses shared guarded settings: strict input, stable metadata, stale-plan rejection, full scope, protected REST service and failed persistence read-back") + const evoAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "EvoBGP settings acceptance", limits: { mutationsPerHour: 50 }, permissions: ["mm:settings:admin"], scope: { allowGlobal: true, serverIds: [1, 2], operationIds: ["evobgp.settings.read", "evobgp.settings.update", "evobgp.catalog.read"] } }), actor) + const evoPrincipal = await authenticateMcpToken(evoAccess.token) + const limitedEvo = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "EvoBGP partial scope", permissions: ["mm:settings:admin"], scope: { allowGlobal: true, serverIds: [1], operationIds: ["evobgp.settings.read", "evobgp.settings.update", "evobgp.catalog.read"] } }), actor) + const limitedEvoPrincipal = await authenticateMcpToken(limitedEvo.token) + await assert.rejects(invokeMcpTool(limitedEvoPrincipal, "mm_evobgp_settings_get", {})) + await assert.rejects(invokeMcpTool(limitedEvoPrincipal, "mm_evobgp_settings_update_plan", { settings: { enabled: true } })) + const { readEvoBgpSettings, writeEvoBgpSettings } = await import("../../services/evobgp-settings.js") + await pool.query("DELETE FROM evobgp_settings") + assert.deepEqual(await invokeMcpTool(evoPrincipal, "mm_evobgp_settings_get", {}), { baseUrl: "", enabled: false, secretConfigured: false }) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM evobgp_settings")).rows[0].n, 0) + const evoRoot = "https://evo.example.invalid", evoKey = "fixture-evo-private-key" + const issueEvoSecret = async (purpose = `evobgp-api:${evoRoot}`, owner = actor.id, serverId: number | null = null) => { + const id = randomUUID() + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, purpose, serverId, await encryptSecret("Bearer " + evoKey), owner]) + return id + } + for (const ref of [await issueEvoSecret("evobgp-api:https://wrong.example.invalid"), await issueEvoSecret(undefined, "other-owner"), await issueEvoSecret(undefined, actor.id, 1)]) { + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_settings_update_plan", { settings: { baseUrl: evoRoot }, apiKeyRef: ref })) + } + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_settings_update_plan", { settings: { apiKey: "raw-input-rejected" } })) + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_settings_update_plan", { settings: {} })) + const evoKeyRef = await issueEvoSecret() + const applyEvo = async (args: Record, state = "applied") => { + const plan = await invokeMcpTool(evoPrincipal, "mm_evobgp_settings_update_plan", args) as { planId: string; changes: unknown } + assert.ok(!JSON.stringify(plan).includes(evoKey)) + await approvePlan(plan.planId, actor, true) + const operation = await applyPlan(evoPrincipal, plan.planId, randomUUID()) + assert.equal(await waitOperation(operation.id), state) + return operation + } + await applyEvo({ settings: { baseUrl: "EVO.example.invalid/", enabled: false }, apiKeyRef: evoKeyRef }) + assert.deepEqual(await readEvoBgpSettings(), { baseUrl: evoRoot, enabled: false, apiKey: evoKey }) + assert.deepEqual(await invokeMcpTool(evoPrincipal, "mm_evobgp_settings_get", {}), { baseUrl: evoRoot, enabled: false, secretConfigured: true }) + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_settings_update_plan", { settings: { baseUrl: "https://different.example.invalid" } })) + const staleEvo = await invokeMcpTool(evoPrincipal, "mm_evobgp_settings_update_plan", { settings: { enabled: true } }) as { planId: string } + await approvePlan(staleEvo.planId, actor, true) + await writeEvoBgpSettings({ apiKey: "fixture-replaced-private-key" }) + assert.equal(await waitOperation((await applyPlan(evoPrincipal, staleEvo.planId, randomUUID())).id), "rejected") + assert.equal((await readEvoBgpSettings()).enabled, false) + const evoProtection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,reason,created_by) VALUES($1,$2::jsonb,'fixture',$3)", [evoProtection, JSON.stringify({ kind: "evobgp-settings", id: "1", protection: "deny" }), actor.id]) + try { + await assert.rejects(writeEvoBgpSettings({ enabled: true })) + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_settings_update_plan", { settings: { enabled: true } })) + } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [evoProtection]) } + await pool.query("CREATE FUNCTION mcp_test_ignore_evo() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RETURN OLD; END'") + await pool.query("CREATE TRIGGER mcp_test_ignore_evo BEFORE UPDATE ON evobgp_settings FOR EACH ROW EXECUTE FUNCTION mcp_test_ignore_evo()") + try { await applyEvo({ settings: {}, apiKeyRef: evoKeyRef }, "partial") } + finally { + await pool.query("DROP TRIGGER mcp_test_ignore_evo ON evobgp_settings") + await pool.query("DROP FUNCTION mcp_test_ignore_evo()") + } + await applyEvo({ settings: { baseUrl: "https://different.example.invalid" }, apiKeyRef: null }) + assert.equal((await readEvoBgpSettings()).apiKey, "") + await applyEvo({ settings: { baseUrl: evoRoot }, apiKeyRef: evoKeyRef }) + await applyEvo({ settings: { enabled: true } }) + assert.deepEqual(await readEvoBgpSettings(), { baseUrl: evoRoot, enabled: true, apiKey: evoKey }) + const evoLogs = await pool.query("SELECT to_jsonb(request) AS payload FROM mcp_requests AS request WHERE connection_id=$1 UNION ALL SELECT intended_state FROM mcp_operation_steps WHERE operation_id IN (SELECT id FROM mcp_operations WHERE connection_id=$1)", [evoAccess.connection.id]) + assert.ok(!JSON.stringify(evoLogs.rows).includes(evoKey)) + console.log("✓ EvoBGP settings: read is non-mutating, owner/destination-bound opaque keys, encrypted/redacted plans, key rotation invalidates plans, protections and failed key read-back") + await writeEvoBgpSettings({ baseUrl: `http://127.0.0.1:${port}`, enabled: true }) + await assert.rejects(invokeMcpTool(limitedEvoPrincipal, "mm_evobgp_catalog_get", {})); assert.equal(evoCatalogCalls, 0) + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_catalog_get", { baseUrl: "https://untrusted.invalid" })); assert.equal(evoCatalogCalls, 0) + const emptyCatalog = await invokeMcpTool(evoPrincipal, "mm_evobgp_catalog_get", {}) as { communities: unknown[]; incomplete: boolean } + assert.deepEqual(emptyCatalog.communities, []); assert.equal(emptyCatalog.incomplete, false) + assert.equal(evoCatalogAuthorization, "Bearer " + evoKey) + const evoAssociations = (await pool.query("INSERT INTO filter_rules(server_id,community,gateway_tunnel_id) VALUES(1,' 64512:4242 ','fixture'),(1,'64512:4242','fixture'),(2,'64512:4242','fixture') RETURNING id")).rows.map(row => row.id) + try { + evoCatalogPayload = { ...emptyEvoCatalog, modules: { items: [{ id: "m", name: evoKey, type: "custom" }] }, + domains: { items: [1, 2].map(id => ({ module_id: "m", entry: { id: "d" + id, fqdn: `example${id}.invalid`, community_id: "c" } })) }, + asns: { items: [{ module_id: "m", entry: { id: "a", asn: 64512, community_id: "c" } }] }, + communities: { items: [{ id: "c", community: "64512:4242" }, { id: "z", community: "64512:4243" }] }, + } + type EvoPage = { catalogVersion: string; domains: { id: string }[]; communities: { serverCount: number; prefixCount: number | null }[]; pages: { domains: { total: number; nextOffset: number | null } }; incomplete: boolean } + const first = await invokeMcpTool(evoPrincipal, "mm_evobgp_catalog_get", { limit: 1 }) as EvoPage + assert.equal(first.pages.domains.total, 2); assert.equal(first.pages.domains.nextOffset, 1) + assert.equal(first.communities[0].serverCount, 2); assert.equal(first.communities[0].prefixCount, null) + assert.equal(first.incomplete, true); assert.ok(!JSON.stringify(first).includes(evoKey)) + const second = await invokeMcpTool(evoPrincipal, "mm_evobgp_catalog_get", { limit: 1, offset: 1 }) as EvoPage + assert.equal(second.catalogVersion, first.catalogVersion); assert.equal(second.pages.domains.nextOffset, null) + assert.notEqual(second.domains[0].id, first.domains[0].id); assert.equal(second.communities[0].prefixCount, 0) + evoCatalogPayload = { modules: { items: [] } } + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_catalog_get", {})) + evoCatalogRedirect = true + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_catalog_get", {})) + evoCatalogRedirect = false + await writeEvoBgpSettings({ enabled: false }) + const callsBeforeDisabled = evoCatalogCalls + await assert.rejects(invokeMcpTool(evoPrincipal, "mm_evobgp_catalog_get", {})); assert.equal(evoCatalogCalls, callsBeforeDisabled) + const catalogRequests = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1 AND tool='mm_evobgp_catalog_get'", [evoAccess.connection.id])).rows + assert.ok(!JSON.stringify(catalogRequests).includes(evoKey)) + } finally { + evoCatalogRedirect = false; evoCatalogPayload = emptyEvoCatalog + await pool.query("DELETE FROM filter_rules WHERE id=ANY($1::bigint[])", [evoAssociations]) + } + console.log("✓ EvoBGP catalog: full scope before upstream/SQL, saved credentials only, shared formatting, nullable counts, paging/version, credential reflection masked, disabled/malformed/redirect rejected") + const evoProbeAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "EvoBGP connection check", permissions: ["mm:settings:admin"], scope: { allowGlobal: true, serverIds: [1, 2], operationIds: ["evobgp.connection.check"] } }), actor) + const evoProbePrincipal = await authenticateMcpToken(evoProbeAccess.token) + const partialEvoProbeAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Partial EvoBGP connection check", permissions: ["mm:settings:admin"], scope: { allowGlobal: true, serverIds: [1], operationIds: ["evobgp.connection.check"] } }), actor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(partialEvoProbeAccess.token), "mm_evobgp_connection_check", {})); assert.equal(evoProbeCalls, 0) + const beforeProbe = await readEvoBgpSettings() + evoProbePayload = { items: [{ name: evoKey }] } + const probeReceipt = await invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", {}) as { ok: boolean; configurationSaved: boolean; routingVerified: boolean } + assert.equal(probeReceipt.ok, true); assert.equal(probeReceipt.configurationSaved, false); assert.equal(probeReceipt.routingVerified, false) + assert.equal(evoProbeAuthorization, "Bearer " + evoKey); assert.ok(!JSON.stringify(probeReceipt).includes(evoKey)) + assert.deepEqual(await readEvoBgpSettings(), beforeProbe) + await assert.rejects(invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", { baseUrl: "https://different.example.invalid" })); assert.equal(evoProbeCalls, 1) + await assert.rejects(invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", { apiKey: "raw-key-rejected" })); assert.equal(evoProbeCalls, 1) + const probeRoot = `http://127.0.0.1:${port}` + const foreignProbeRef = await issueEvoSecret(`evobgp-api:${probeRoot}`, "other-owner") + await assert.rejects(invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", { apiKeyRef: foreignProbeRef })); assert.equal(evoProbeCalls, 1) + await assert.rejects(invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", { apiKeyRef: evoKeyRef })); assert.equal(evoProbeCalls, 1) + const probeKeyRef = await issueEvoSecret(`evobgp-api:${probeRoot}`) + await writeEvoBgpSettings({ baseUrl: evoRoot }) + await invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", { baseUrl: probeRoot, apiKeyRef: probeKeyRef }) + assert.equal((await readEvoBgpSettings()).baseUrl, evoRoot) + evoProbePayload = { error: evoKey } + await assert.rejects(invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", { baseUrl: probeRoot, apiKeyRef: probeKeyRef }), error => error instanceof Error && !error.message.includes(evoKey)) + evoProbeRedirect = true + await assert.rejects(invokeMcpTool(evoProbePrincipal, "mm_evobgp_connection_check", { baseUrl: probeRoot, apiKeyRef: probeKeyRef })) + evoProbeRedirect = false; evoProbePayload = { items: [] } + const probeRequests = (await pool.query("SELECT result,error_code FROM mcp_requests WHERE connection_id=$1", [evoProbeAccess.connection.id])).rows + assert.ok(!JSON.stringify(probeRequests).includes(evoKey)) + console.log("✓ EvoBGP connection check: fixed bounded GET, full scope before network, owner/destination-bound draft secret, no save/credential reflection, malformed and redirects rejected") + const statsAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Scoped statistics", permissions: ["mm:traffic:read"], scope: { allowGlobal: false, serverIds: [1], operationIds: ["statistics.read", "statistics.pivot"] } }), telemetryActor) + const statsPrincipal = await authenticateMcpToken(statsAccess.token) + const { ensurePartitionFor } = await import("../../db/partitions.js") + await ensurePartitionFor(pool, "flow_hour_facts", "day", new Date("2026-10-03T00:00:00Z")) + await pool.query("INSERT INTO flow_hour_facts(server_id,bucket_at,iface,country,service,asn,bytes,packets) VALUES(1,'2026-10-03T00:00:00Z','fixture-stats','RU','FixtureStats',0,12345,10),(2,'2026-10-03T00:00:00Z','fixture-stats','US','ForeignStats',0,98765,90)") + const touched: number[] = [], { setRefreshIfacesForTests } = await import("../../services/traffic-flow-ifaces.js") + setRefreshIfacesForTests(async id => { touched.push(id) }) + try { + const summary = await invokeMcpTool(statsPrincipal, "mm_statistics_get", { serverId: 1, from: "2026-10-03", to: "2026-10-03", iface: "fixture-stats", planes: "all" }) as { kpis: { bytes: number }; services: { label: string }[] } + assert.equal(summary.kpis.bytes, 12345); assert.ok(!JSON.stringify(summary).includes("ForeignStats")) + const pivot = await invokeMcpTool(statsPrincipal, "mm_statistics_pivot_get", { serverId: 1, from: "2026-10-03", to: "2026-10-03", iface: "fixture-stats", planes: "all", row: "country", col: "service" }) as { rows: { total: number }[] } + assert.equal(pivot.rows.reduce((sum, row) => sum + row.total, 0), 12345) + assert.ok(touched.length > 0 && touched.every(id => id === 1), JSON.stringify(touched)) + await assert.rejects(invokeMcpTool(statsPrincipal, "mm_statistics_get", { serverId: 2, from: "2026-10-03", to: "2026-10-03" })) + await assert.rejects(invokeMcpTool(statsPrincipal, "mm_statistics_get", { serverId: 1, from: "2026-01-01", to: "2026-10-03" })) + } finally { setRefreshIfacesForTests(null) } + console.log("✓ Scoped statistics/pivot filter SQL and native interface refresh to the selected server; long windows and foreign servers are denied") + const savedSocket = process.env.FLOW_AGENT_SOCKET + const socket = process.platform === "win32" ? "\\\\.\\pipe\\mcp-flow-" + randomUUID() : (await import("node:path")).join((await import("node:os")).tmpdir(), "mcp-flow-" + randomUUID() + ".sock") + let hostApplies = 0, hostMatches = true + const flowAgent = createServer((request, response) => { + request.resume() + request.on("end", () => { + if (request.url === "/apply") hostApplies++ + response.setHeader("content-type", "application/json") + response.end(JSON.stringify({ interfaceUp: true, configMatches: hostMatches, publicKey: "public-key-fixture", listenPort: 51821, addresses: ["10.255.254.1/24"], peers: [], privateKey: "must-never-return" })) + }) + }) + await new Promise((resolve, reject) => { flowAgent.once("error", reject); flowAgent.listen(socket, resolve) }) + try { + process.env.FLOW_AGENT_SOCKET = socket + const hostConnection = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, name: "Host acceptance", permissions: ["mm:settings:admin"], scope: { allowGlobal: true, serverIds: [1, 2], operationIds: ["netflow.host.read", "netflow.host.reconcile"] } }), actor) + const hostPrincipal = await authenticateMcpToken(hostConnection.token) + assert.ok(!JSON.stringify(await invokeMcpTool(hostPrincipal, "mm_netflow_host_get", {})).includes("must-never-return")) + const hostPlan = await invokeMcpTool(hostPrincipal, "mm_netflow_host_reconcile_plan", {}) as { planId: string } + await approvePlan(hostPlan.planId, actor, true) + assert.equal(await waitOperation((await applyPlan(hostPrincipal, hostPlan.planId, "flow-host-apply")).id), "applied") + assert.equal(hostApplies, 1) + const protectedHost = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,reason,created_by) VALUES($1,$2::jsonb,'fixture',$3)", [protectedHost, JSON.stringify({ kind: "netflow-host", id: "1", protection: "deny" }), actor.id]) + await assert.rejects(invokeMcpTool(hostPrincipal, "mm_netflow_host_reconcile_plan", {})) + await assert.rejects((await import("../../services/traffic-flow-host-agent.js")).reconcileFlowHost()) + assert.equal(hostApplies, 1) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectedHost]) + const failedHost = await invokeMcpTool(hostPrincipal, "mm_netflow_host_reconcile_plan", {}) as { planId: string } + await approvePlan(failedHost.planId, actor, true) + hostMatches = false + assert.equal(await waitOperation((await applyPlan(hostPrincipal, failedHost.planId, "flow-host-unverified")).id), "partial") + assert.equal(hostApplies, 2) + console.log("✓ NetFlow host uses saved settings and real local-agent protocol; protected legacy apply is blocked and a mismatched read-back remains partial") + } finally { + if (savedSocket === undefined) delete process.env.FLOW_AGENT_SOCKET; else process.env.FLOW_AGENT_SOCKET = savedSocket + await new Promise(resolve => flowAgent.close(() => resolve())) + } + legacyRouteContextFixture = true + const profileConnection = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...input, permissions: [...input.permissions, "mm:filters:write"], name: "Controller", scope: { ...input.scope, operationIds: ["routing.profiles.save", "routing.profiles.read", "routing.analyze", "routing.community.switch", "routing.path.verify"] } }), actor) + const controllerPrincipal = await authenticateMcpToken(profileConnection.token) + const profileInput = { name: "Persisted", serverId: 1, family: "ipv4", community: "65000:10", candidates: [{ id: "single", wanInterface: "wan", downstreamInterface: "down", jumpHostId: 1, exitNodeId: 1, gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*1" } }] } + const legacyContext = await invokeMcpTool(controllerPrincipal, "mm_route_optimizer_analyze", { profile: profileInput }) as { contextVerified: boolean; currentCandidateId: string | null; executionAvailable: boolean; dependencyProof: string; targetRuleProof: string; sourceProofs: unknown } + assert.equal(legacyContext.contextVerified, true); assert.equal(legacyContext.currentCandidateId, "single"); assert.equal(legacyContext.executionAvailable, true, "legacy main profile needs no explicit table, probeVrf or bgpContext") + await assert.rejects(invokeMcpTool(controllerPrincipal, "mm_route_optimizer_plan", { profile: profileInput, candidateId: "single" }), error => error instanceof Error && "code" in error && error.code === "MEASUREMENT_UNKNOWN") + const { findMcpService, registerMcpService } = await import("./service-registry.js") + for (const iface of ["wan", "down"]) await pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source) VALUES($1,1,1,$2,'peer','ipv4','main',10,0,100,100,now()-interval '2 hours',now()-interval '2 hours',true,'fixture')", [randomUUID(), iface]) + await assert.rejects(findMcpService("routing.community")!.current({ serverId: 1, profile: profileInput, candidateId: "single" }), error => error instanceof Error && "code" in error && error.code === "STATE_CONFLICT", "an unbound legacy service input cannot bypass the native context proof") + await assert.rejects(findMcpService("routing.community")!.current({ serverId: 1, profile: profileInput, candidateId: "single", contextProof: legacyContext.dependencyProof, targetRuleProof: legacyContext.targetRuleProof, contextSources: legacyContext.sourceProofs }), error => error instanceof Error && "code" in error && error.code === "MEASUREMENT_UNKNOWN") + console.log("✓ Community plans reject missing measurements and recheck expired SLA evidence before any write") + const controllerOriginalAuthority = await effectiveMcpConnection(controllerPrincipal) + const controllerFixtureExpiry = () => fixtureGrantExpiry(Date.now(), Date.parse(controllerPrincipal.credentialExpiresAt), Date.parse(controllerOriginalAuthority.policy.expiresAt), controllerOriginalAuthority.owner_valid_until.getTime()) + const profilePlan = await invokeMcpTool(controllerPrincipal, "mm_route_profile_save_plan", { profile: profileInput, enabled: true, expiresAt: controllerFixtureExpiry() }) as { planId: string } + await approvePlan(profilePlan.planId, actor, true) + const saved = await applyPlan(controllerPrincipal, profilePlan.planId, "profile-save") + for (let index = 0; index < 100; index++) { + state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [saved.id])).rows[0].state + if (!["pending", "running"].includes(state)) break + await delay(30) + } + assert.equal(state, "applied") + const { tickRoutingProfiles } = await import("./routing-profiles.js") + await tickRoutingProfiles() + const persisted = (await pool.query("SELECT state,enabled FROM mcp_routing_profiles WHERE connection_id=$1", [profileConnection.connection.id])).rows[0] + assert.equal(persisted.enabled, true); assert.ok(persisted.state.checkedAt); assert.equal(persisted.state.planId, undefined) + await setConnectionStatus(profileConnection.connection.id, "paused", actor) + await tickRoutingProfiles() + assert.equal((await pool.query("SELECT enabled FROM mcp_routing_profiles WHERE connection_id=$1", [profileConnection.connection.id])).rows[0].enabled, false) + console.log("✓ Persistent controller preserves unknown path and stops after access revocation") + legacyRouteContextFixture = false + await setConnectionStatus(profileConnection.connection.id, "active", actor) + const webPayload = { connectionId: profileConnection.connection.id, profile: { ...profileInput, name: "From web" }, enabled: false, expiresAt: controllerFixtureExpiry() } + const webHeaders = { authorization: "Bearer " + jwt } + assert.equal((await app.inject({ method: "POST", url: "/api/mcp/routing-profiles/plan", payload: webPayload })).statusCode, 401) + const webPlanResponse = await app.inject({ method: "POST", url: "/api/mcp/routing-profiles/plan", headers: webHeaders, payload: webPayload }) + assert.equal(webPlanResponse.statusCode, 200, webPlanResponse.body) + const webPlanId = webPlanResponse.json().planId + const otherJwt = await new SignJWT({ sub: "other-owner", apps: ["mm"], permissions: actor.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + assert.equal((await app.inject({ method: "POST", url: `/api/mcp/plans/${webPlanId}/apply`, headers: { authorization: "Bearer " + otherJwt }, payload: {} })).statusCode, 403) + assert.equal((await app.inject({ method: "POST", url: `/api/mcp/plans/${webPlanId}/apply`, headers: webHeaders, payload: {} })).statusCode, 409) + assert.equal((await app.inject({ method: "POST", url: `/api/mcp/plans/${webPlanId}/decision`, headers: webHeaders, payload: { approve: true } })).statusCode, 200) + const webOperation = await app.inject({ method: "POST", url: `/api/mcp/plans/${webPlanId}/apply`, headers: webHeaders, payload: {} }) + assert.equal(webOperation.statusCode, 200, webOperation.body) + for (let index = 0; index < 100; index++) { + state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [webOperation.json().id])).rows[0].state + if (!["pending", "running"].includes(state)) break + await delay(30) + } + assert.equal(state, "applied") + console.log("✓ Human profile UI uses scoped plans, requires approval and rejects another owner's execution") + const orphanPlan = randomUUID(), orphanOperation = randomUUID() + await pool.query("INSERT INTO mcp_plans SELECT $1,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,'executing',expires_at,approved_by,approved_at,created_at FROM mcp_plans WHERE id=$2", [orphanPlan, stale.planId]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,created_at) VALUES($1,$2,$3,'orphan','running',now()-interval '3 minutes')", [orphanOperation, orphanPlan, issued.connection.id]) + const lease = await pool.connect() + try { + await lease.query("SELECT pg_advisory_lock(hashtextextended($1,0))", ["mcp-operation:" + orphanOperation]) + await recoverMcpOperations() + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [orphanOperation])).rows[0].state, "running") + } finally { await lease.query("SELECT pg_advisory_unlock(hashtextextended($1,0))", ["mcp-operation:" + orphanOperation]); lease.release() } + await recoverMcpOperations() + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [orphanOperation])).rows[0].state, "unknown") + console.log("✓ Recovery respects live runner leases and marks orphaned writes unknown") + const recoveryChannelServer = createServer((_req, res) => { res.setHeader("content-type", "application/json"); res.end(JSON.stringify({ "system-id": "fixture-primary" })) }) + await new Promise(resolve => recoveryChannelServer.listen(0, "127.0.0.2", resolve)) + const restoreService = findMcpService("routing.community.restore")!, savedService = { ...restoreService } + try { + const channel = await registerManagementChannel({ serverId: 1, host: "127.0.0.2", port: (recoveryChannelServer.address() as { port: number }).port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture", attestation: "Изолированный независимый канал для проверки заранее разрешённого восстановления", expiresAt: fixtureGrantExpiry(Date.now(), actor.expiresAt!) }, actor) + const { routeAiProfileSchema } = await import("@mmapp/contracts/mcp-routing") + const recoveryProfile = routeAiProfileSchema.parse({ ...profileInput, candidates: profileInput.candidates.map(candidate => ({ ...candidate, controlProbe: { targetInterface: "down", expectedHops: [{ serverId: 1, interface: "peer" }] } })), autoRecovery: true }), grantId = randomUUID() + const activePrincipal = await authenticateMcpToken(rotated.token) + const recoveryOriginalAuthority = await effectiveMcpConnection(activePrincipal) + const recoveryFixtureExpiry = fixtureGrantExpiry(Date.now(), Date.parse(activePrincipal.credentialExpiresAt), Date.parse(recoveryOriginalAuthority.policy.expiresAt), recoveryOriginalAuthority.owner_valid_until.getTime()) + await pool.query("INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version) VALUES($1,$2,$3::jsonb,true,$6,$4,$5,1)", [grantId, issued.connection.id, JSON.stringify(recoveryProfile), activePrincipal.credentialId, await (await import("./security.js")).accessGeneration(), recoveryFixtureExpiry]) + const before = settings["rp-filter"] + restoreService.current = async () => ({ value: settings["rp-filter"] }) + restoreService.execute = async () => nativeClient.post("/ip/settings/set", { "rp-filter": before }) + restoreService.verify = async () => settings["rp-filter"] === before + registerMcpService("fixture.confirmed-write-failed-probe", { current: async () => ({ value: settings["rp-filter"] }), execute: async () => nativeClient.post("/ip/settings/set", { "rp-filter": "loose" }), verify: async () => false }) + const prepared = await invokeMcpTool(activePrincipal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } }) as { planId: string } + const sealed = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [prepared.planId])).rows[0].evidence + const evidence = JSON.parse(await decryptSecret(sealed.sealed)) + evidence.steps = [{ kind: "service", service: "fixture.confirmed-write-failed-probe", input: {} }]; evidence.before = { value: before } + evidence.managementChannels = [await verifyManagementChannel(1, channel.id)] + evidence.recovery = { authorityProfileId: grantId, validUntil: new Date(Date.now() + 120000).toISOString(), expected: { value: "loose" }, step: { kind: "service", service: "routing.community.restore", input: { profile: recoveryProfile } } } + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [prepared.planId, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(evidence)) })]) + await approvePlan(prepared.planId, actor, true) + const writesBeforeLegacyWriter = writes + const attempted = await applyPlan(activePrincipal, prepared.planId, "reserved-recovery") + // This deliberately unsupported legacy profile has no authentic baseline. + // Replacing an unavailable restore service cannot enroll a generic writer. + // Genuine positive inverse coverage lives in the scoped production suite. + assert.ok(["rejected", "partial", "unknown"].includes(await waitOperation(attempted.id)), "Legacy writer must reach a terminal refusal, never remain pending or time out") + assert.equal(writes, writesBeforeLegacyWriter); assert.equal(settings["rp-filter"], before) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operation_checkpoints WHERE operation_id=$1 AND configuration_sealed IS NOT NULL", [attempted.id])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_recovery_intents WHERE operation_id=$1 AND state='verified'", [attempted.id])).rows[0].n, 0) + const beforeReplay = writes + await applyPlan(activePrincipal, prepared.planId, "reserved-recovery"); await delay(50); assert.equal(writes, beforeReplay) + await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", [grantId]) + const blockedPlan = await invokeMcpTool(activePrincipal, "mm_ip_settings_update_plan", { serverId: 1, fields: { "rp-filter": "loose" } }) as { planId: string } + const blockedSealed = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [blockedPlan.planId])).rows[0].evidence + const blockedEvidence = JSON.parse(await decryptSecret(blockedSealed.sealed)); blockedEvidence.managementChannels = evidence.managementChannels; blockedEvidence.recovery = evidence.recovery + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [blockedPlan.planId, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(blockedEvidence)) })]) + await approvePlan(blockedPlan.planId, actor, true) + const writesBeforeRevokedGrant = writes + await assert.rejects(applyPlan(activePrincipal, blockedPlan.planId, "revoked-recovery")) + assert.equal(writes, writesBeforeRevokedGrant); assert.equal(settings["rp-filter"], before) + console.log("✓ Unsupported legacy autoRecovery cannot enroll a generic native writer or verified recovery; replay and revoked grant produce no writes") + } finally { Object.assign(restoreService, savedService); await new Promise(resolve => recoveryChannelServer.close(() => resolve())) } + await pool.query("UPDATE mcp_connections SET owner_valid_until=now()-interval '1 second' WHERE id=$1", [limited.connection.id]) + await assert.rejects(authenticateMcpToken(limited.token)) + await revalidateMcpOwner(limited.connection.id, { ...actor, permissions: ["mm:mcp:read", "mm:servers:read"] }) + const narrowed = await authenticateMcpToken(limited.token) + await assert.rejects(invokeMcpTool(narrowed, "mm_ip_settings_list", { serverId: 1 })) + console.log("✓ Owner claims expire; revalidation cannot preserve removed owner permissions") + const portalOwner: AuthUser = { ...actor, id: "portal-revoked-owner", issuedAt: Date.now() - 60000 } + const portalConnection = await createMcpConnection(input, portalOwner) + const event = { eventId: randomUUID(), userId: portalOwner.id, type: "permissions.changed" } + const timestamp = String(Math.floor(Date.now() / 1000)), raw = JSON.stringify(event) + process.env.MCP_PORTAL_WEBHOOK_SECRET = "integration-only-portal-secret-32-characters" + const signature = "sha256=" + createHmac("sha256", process.env.MCP_PORTAL_WEBHOOK_SECRET).update(timestamp + "." + raw).digest("hex") + const sendEvent = (body = raw, stamp = timestamp, sig = signature) => app!.inject({ method: "POST", url: "/_internal/mcp/portal/revocations", headers: { "content-type": "application/json", "x-mm-timestamp": stamp, "x-mm-signature": sig }, payload: body }) + assert.equal((await sendEvent(raw + " ")).statusCode, 401) + assert.equal((await sendEvent(raw, String(Number(timestamp) - 600))).statusCode, 401) + assert.equal((await sendEvent()).statusCode, 200) + assert.equal((await sendEvent()).json().duplicate, true) + await assert.rejects(authenticateMcpToken(portalConnection.token)) + await assert.rejects(createMcpConnection(input, portalOwner)) + await assert.rejects(revalidateMcpOwner(portalConnection.connection.id, portalOwner)) + const oldPortalJwt = app.jwt.sign({ sub: portalOwner.id, apps: ["mm"], permissions: actor.permissions, iss: "https://auth.shnt.top", iat: Number(timestamp) - 60, exp: Number(timestamp) + 1800 }) + assert.equal((await app.inject({ method: "GET", url: "/api/mcp/connections", headers: { authorization: "Bearer " + oldPortalJwt } })).statusCode, 401) + const freshOwner = { ...portalOwner, issuedAt: (Number(timestamp) + 1) * 1000 } + const freshAccess = await createMcpConnection(input, freshOwner) + await authenticateMcpToken(freshAccess.token) + 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") + await setConnectionStatus(issued.connection.id, "paused", actor) + await assert.rejects(authenticateMcpToken(rotated.token)) + await setConnectionStatus(issued.connection.id, "active", actor) + await invalidateMcpAccess() + await assert.rejects(authenticateMcpToken(rotated.token)) + console.log("✓ Immediate pause and external-generation invalidation") + await (await import("./native-conservation-acceptance.js")).acceptNativeConservation(actor) + await (await import("./native-create-id-acceptance.js")).acceptNativeCreateId(actor) +} finally { + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close() + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + await closePool() +} + diff --git a/backend/src/modules/mcp/interface-events-compatibility-http.test.ts b/backend/src/modules/mcp/interface-events-compatibility-http.test.ts new file mode 100644 index 00000000..0c1ed908 --- /dev/null +++ b/backend/src/modules/mcp/interface-events-compatibility-http.test.ts @@ -0,0 +1,36 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-interface-events-acceptance-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const { buildApp } = await import("../../index.js") +const actor = { id: "interface-events-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:filters:admin", "mm:servers:admin", "mm:dashboard:read"], expiresAt: Date.now() + 3600000 } +let app: Awaited> | undefined +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,enabled) VALUES(1,'owned-fixture','127.0.0.1',9,false,false)") + await pool.query("INSERT INTO geoip_settings(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false }); await app.ready() + const legacyBgpOnly = process.env.MMAPP_OWN_LEGACY_BGP_ONLY === "true" + if (legacyBgpOnly) { + await (await import("./routing-community-context-compatibility-acceptance.js")).acceptRouteCommunityContextCompatibilityMcp(actor, { legacyOnly: true }) + } else { + await (await import("../../test/mcp-events-batch.js")).verifyMcpEventsBatch(actor, app) + } + if (!legacyBgpOnly && process.env.MMAPP_OWN_EVENTS_ONLY !== "true") { + await (await import("./wireguard-interface-update-acceptance.js")).acceptWireguardInterfaceUpdate(actor) + await (await import("./routing-community-context-compatibility-acceptance.js")).acceptRouteCommunityContextCompatibilityMcp(actor) + await (await import("./routing-community-context-acceptance.js")).acceptRouteCommunityContextMcp(actor) + await (await import("./routing-community-scoped-acceptance.js")).acceptScopedRouteCommunitySwitch(actor) + } +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close(); await closePool() +} diff --git a/backend/src/modules/mcp/ipsec-adapters-http.test.ts b/backend/src/modules/mcp/ipsec-adapters-http.test.ts new file mode 100644 index 00000000..b7c5825b --- /dev/null +++ b/backend/src/modules/mcp/ipsec-adapters-http.test.ts @@ -0,0 +1,38 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-ipsec-adapters-acceptance-secret" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const actor = { id: "ipsec-adapters-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:certificates:write"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({ enabled: true }, actor) + const target = process.env.MMAPP_MODULE_COMPLETION_TARGET ?? "all" + if (target === "all" || target === "vrf") { + const vrfRuntime = await (await import("./vrf-ticket-runtime-sql.test.js")).runVrfTicketRuntimeSqlAcceptance(actor) + assert.equal(vrfRuntime.passed, true) + console.log(`VRF runtime SQL: ${vrfRuntime.cases} real plan/ticket/quarantine cases; zero native writes`) + } + if (process.env.MMAPP_MODULE_COMPLETION_ONLY !== "true") { + await (await import("./ipsec-peer-delete-acceptance.js")).acceptIpsecPeerDeletion(actor) + await (await import("./ipsec-certificate-delete-acceptance.js")).acceptIpsecCertificateDeletion(actor) + await (await import("./ipsec-peer-update-acceptance.js")).acceptIpsecPeerUpdate(actor) + await (await import("./ipsec-native-write-acceptance.js")).acceptIpsecNativeWrite(actor) + await (await import("./ipsec-user-update-acceptance.js")).acceptIpsecUserUpdate(actor) + await (await import("./ipsec-server-delete-acceptance.js")).acceptIpsecServerDelete(actor) + await (await import("./ipsec-user-create-acceptance.js")).acceptIpsecUserCreate(actor) + } + if (target === "all" || target === "ipsec-init") await (await import("./ipsec-server-init-acceptance.js")).acceptIpsecServerInit(actor) + if (target === "ipsec-create") await (await import("./ipsec-user-create-acceptance.js")).acceptIpsecUserCreate(actor) + if (target === "ipsec-delete" || (target === "all" && process.env.MMAPP_MODULE_COMPLETION_ONLY === "true")) await (await import("./ipsec-server-delete-acceptance.js")).acceptIpsecServerDelete(actor) + if (target === "all" || target === "ipsec-restore") await (await import("./ipsec-revision-acceptance.js")).acceptIpsecRevisionRestore(actor) + if (target === "all" || target === "wireguard") await (await import("./wireguard-revision-acceptance.js")).acceptWireguardRevisionRestore(actor) +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/ipsec-certificate-delete-acceptance.ts b/backend/src/modules/mcp/ipsec-certificate-delete-acceptance.ts new file mode 100644 index 00000000..394d6428 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-certificate-delete-acceptance.ts @@ -0,0 +1,120 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import ipsecRoutes, { createIpsecRoutes } from "../../routes/ipsec.js" +import { getEnabledIpsecServerById } from "../../services/ipsec-live.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** SQL + owned loopback HTTP fixture. No external router, certificate issuance or secret export. */ +export async function acceptIpsecCertificateDeletion(actor: AuthUser) { + const serverId = 197, baseline: Record[]> = Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])) + baseline["/certificate"] = [{ ".id": "*c", name: "foreign-leaf", ca: "foreign-ca", "key-usage": "tls-client", issued: "false", opaque: "NEVER_CERT_OPAQUE" }, { ".id": "*a", name: "foreign-ca", authority: "true", crl: "true" }, { ".id": "*o", name: "other", opaque: "NEVER_OTHER_OPAQUE" }] + baseline["/ip/ipsec/peer"] = [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", certificate: "foreign-leaf", disabled: "true" }] + baseline["/ip/ipsec/identity"] = [{ ".id": "*i", peer: "ike", "remote-certificate": "foreign-leaf", "auth-method": "digital-signature", disabled: "true", secret: "NEVER_CERT_PSK", comment: "fixture" }] + let catalogs = structuredClone(baseline), mode = "normal", deletes = 0, reads = 0, connectionId: string | undefined + let duringRead: (() => Promise) | undefined, afterDelete: (() => Promise) | undefined + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json"); const path = (req.url ?? "").replace(/^\/rest/, "") + if (req.method === "GET") { + reads++ + if (duringRead) { const hook = duringRead; duringRead = undefined; void hook().then(() => res.end(JSON.stringify(catalogs[path] ?? [])), () => { res.statusCode = 503; res.end("{}") }); return } + if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_CERT_PROVIDER"}'); return } + res.end(JSON.stringify(catalogs[path] ?? [])); return + } + if (req.method === "DELETE" && path === "/certificate/*c") { + deletes++ + if (mode !== "ignored") catalogs["/certificate"] = catalogs["/certificate"].filter(row => row[".id"] !== "*c") + if (mode === "collateral") catalogs["/certificate"][1].opaque = "changed" + if (mode === "lost") { req.socket.destroy(); return } + if (afterDelete) { const hook = afterDelete; afterDelete = undefined; void hook().then(() => res.end("{}"), () => { res.statusCode = 503; res.end("{}") }); return } + res.end("{}"); return + } + res.statusCode = 400; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = () => { mode = "normal"; catalogs = structuredClone(baseline) } + const wait = async (id: string) => { const deadline = Date.now() + 140000; while (Date.now() < deadline) { const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; if (!["pending", "running"].includes(state)) return state; await delay(25) }; throw Error("Certificate operation remains live; never replay") } + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'certificate-delete-proof','127.0.0.1',$2,false,'NEVER_CERT_ENDPOINT')", [serverId, (router.address() as { port: number }).port]) + await rest.register(ipsecRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec certificate deletion", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read", "mm:certificates:write"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.certificates.delete", "ros.certificate.delete", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id; const principal = await authenticateMcpToken(access.token) + const prepare = async (force = true, name = "foreign-leaf") => { const plan = await invokeMcpTool(principal, "mm_ipsec_certificate_remove_plan", { serverId, name, force }) as { planId: string; state: string }; assert.equal(plan.state, "awaiting-approval"); assert.ok(!JSON.stringify(plan).includes("NEVER_")); return plan } + const reconcile = async (operationId: string) => await invokeMcpTool(principal, "mm_operation_reconcile", { operationId }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; configurationMatchesDesiredState: boolean; matchesOriginalState: boolean }[] } + const initialReads: number = reads + await assert.rejects(invokeMcpTool(principal, "mm_ipsec_certificate_remove_plan", { serverId: 1, name: "foreign-leaf" })) + await assert.rejects(invokeMcpTool(principal, "mm_ipsec_certificate_remove_plan", { serverId, name: "" })); assert.equal(reads, initialReads) + const aliasOnly = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Certificate alias without business grant", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read", "mm:certificates:write"], scope: { serverIds: [serverId], operationIds: ["ros.certificate.delete"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + try { const aliasPrincipal = await authenticateMcpToken(aliasOnly.token), priorReads: number = reads; await assert.rejects(invokeMcpTool(aliasPrincipal, "mm_certificate_delete_plan", { serverId, id: "*c" })); assert.equal(reads, priorReads) } finally { await setConnectionStatus(aliasOnly.connection.id, "paused", actor) } + const native = async (id = "*c") => await invokeMcpTool(principal, "mm_certificate_delete_plan", { serverId, id }) as { planId: string; state: string } + await assert.rejects(native(), /используется/); await assert.rejects(native("*a"), /всю цепочку/) + await assert.rejects(prepare(false), /используется/) + catalogs["/ip/ipsec/identity"][0].comment="NEVER_CERT_PSK" + await assert.rejects(prepare(false),error=>error instanceof Error&&!error.message.includes("NEVER_CERT_PSK")) + const masked=await rest.inject({method:"DELETE",url:`/api/ipsec/certs/${serverId}`,payload:{name:"foreign-leaf"}});assert.equal(masked.statusCode,409);assert.ok(!masked.body.includes("NEVER_CERT_PSK")) + catalogs["/certificate"][0].name="NEVER_CERT_PSK" + await assert.rejects(native(),error=>error instanceof Error&&!error.message.includes("NEVER_CERT_PSK")) + reset() + const blocked = await rest.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}`, payload: { name: "foreign-leaf" } }); assert.equal(blocked.statusCode, 409); assert.equal(blocked.json().usedBy.length, 2); assert.equal(deletes, 0) + for (const force of [false, true]) { await assert.rejects(prepare(force, "foreign-ca"), /всю цепочку/); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}?force=${force}`, payload: { name: "foreign-ca" } })).statusCode, 409) } + for (const key of ["issued", "revoked"]) { reset(); catalogs["/certificate"][0][key] = "true"; await assert.rejects(prepare(), /CRL/); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}?force=true`, payload: { name: "foreign-leaf" } })).statusCode, 409) } + reset();catalogs["/certificate"][1].crl="yes";catalogs["/certificate"][0].issued="yes" + await assert.rejects(prepare(),/CRL/);assert.equal((await rest.inject({method:"DELETE",url:`/api/ipsec/certs/${serverId}?force=true`,payload:{name:"foreign-leaf"}})).statusCode,409) + reset(); catalogs["/certificate"].push({ ".id": "*d", name: "foreign-ca", crl: "false" }); await assert.rejects(prepare(), /CA-издателя/); await assert.rejects(native(), /CA-издателя/); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}?force=true`, payload: { name: "foreign-leaf" } })).statusCode, 409) + reset(); catalogs["/ip/ipsec/peer"] = []; catalogs["/ip/ipsec/identity"] = [] + const nativePlan = await native(); assert.equal(nativePlan.state, "awaiting-approval"); await approvePlan(nativePlan.planId, actor, true); const beforeNative: number = deletes, nativeOp = await applyPlan(principal, nativePlan.planId, randomUUID()); assert.equal(await wait(nativeOp.id), "applied"); assert.equal(deletes, beforeNative + 1); assert.deepEqual(catalogs["/certificate"], baseline["/certificate"].slice(1)) + reset(); await assert.rejects(prepare(true, "missing"), /не найден/) + for (const fault of ["normal", "ignored", "collateral", "lost"]) { + reset(); const plan = await prepare(); await approvePlan(plan.planId, actor, true); mode = fault + const before: number = deletes, key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await wait(operation.id), fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial"); assert.equal(deletes, before + 1) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + const checked = await reconcile(operation.id); assert.equal(checked.automaticRetry, false); assert.equal(checked.results[0].matchesDesiredState, fault === "normal"); assert.equal(checked.results[0].configurationMatchesDesiredState, fault === "normal" || fault === "lost"); assert.equal(checked.results[0].matchesOriginalState, fault === "ignored"); assert.equal(deletes, before + 1) + if (fault === "normal") { + assert.deepEqual(catalogs["/ip/ipsec/peer"], baseline["/ip/ipsec/peer"]); assert.deepEqual(catalogs["/ip/ipsec/identity"], baseline["/ip/ipsec/identity"]); assert.deepEqual(catalogs["/certificate"], baseline["/certificate"].slice(1)) + const saved = (await pool.query("SELECT step_index,intended_state,result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0] + for (const change of ["history", "native", "intended"]) { + const intended = structuredClone(saved.intended_state), receipt = structuredClone(saved.result) + if (change === "history") receipt.historyRecorded = false + if (change === "native") receipt.certificateId = "*o" + if (change === "intended") intended.input.force = false + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb,result=$3::jsonb WHERE operation_id=$1 AND step_index=$4", [operation.id, JSON.stringify(intended), JSON.stringify(receipt), saved.step_index]) + const tampered = await reconcile(operation.id); assert.equal(tampered.results[0].configurationMatchesDesiredState, true); assert.equal(tampered.results[0].matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb,result=$3::jsonb WHERE operation_id=$1 AND step_index=$4", [operation.id, JSON.stringify(saved.intended_state), JSON.stringify(saved.result), saved.step_index]) + } + } + } + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); catalogs["/certificate"][2].opaque = "NEVER_DRIFT"; const beforeDrift: number = deletes; assert.equal(await wait((await applyPlan(principal, stale.planId, randomUUID())).id), "rejected"); assert.equal(deletes, beforeDrift) + reset(); const aba = await prepare(); await approvePlan(aba.planId, actor, true); await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=$1", [serverId]); await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=$1", [serverId]); assert.equal(await wait((await applyPlan(principal, aba.planId, randomUUID())).id), "rejected"); assert.equal(deletes, beforeDrift) + reset(); const protection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protection, JSON.stringify({ kind: "certificate", id: "*c", serverId, protection: "deny" })]); await assert.rejects(prepare()); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}?force=true`, payload: { name: "foreign-leaf" } })).statusCode, 409); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + reset(); const revoke = await prepare(); await approvePlan(revoke.planId, actor, true); afterDelete = () => setConnectionStatus(access.connection.id, "paused", actor); const uncertain = await applyPlan(principal, revoke.planId, randomUUID()); assert.equal(await wait(uncertain.id), "unknown"); await setConnectionStatus(access.connection.id, "active", actor) + const beforeRead: number = reads + await assert.rejects(reconcile(uncertain.id)); assert.equal(reads,beforeRead,"Old receipt is rejected before native reads after policy ABA") + reset(); const freshPlan=await prepare(); await approvePlan(freshPlan.planId,actor,true) + const freshOperation=await applyPlan(principal,freshPlan.planId,randomUUID()); assert.equal(await wait(freshOperation.id),"applied") + const beforeReconcile: number = deletes, freshReads: number = reads + duringRead = () => setConnectionStatus(access.connection.id,"paused",actor) + await assert.rejects(reconcile(freshOperation.id)); assert.equal(reads,freshReads+1); assert.equal(deletes,beforeReconcile) + await setConnectionStatus(access.connection.id,"active",actor) + reset(); const success = await rest.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}?force=true`, payload: { name: "foreign-leaf" } }); assert.equal(success.statusCode, 200); assert.equal(success.json().ok, true) + reset(); catalogs["/ip/ipsec/peer"] = []; catalogs["/ip/ipsec/identity"] = []; assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}`, payload: { name: "foreign-leaf" } })).statusCode, 200) + reset(); const history = Fastify(); history.setValidatorCompiler(validatorCompiler); history.setSerializerCompiler(serializerCompiler) + try { await history.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async () => null }), { prefix: "/api" }); const failed = await history.inject({ method: "DELETE", url: `/api/ipsec/certs/${serverId}?force=true`, payload: { name: "foreign-leaf" } }); assert.equal(failed.statusCode, 502); assert.equal(failed.json().state, "applied"); assert.equal(failed.json().historyRecorded, false) } finally { await history.close() } + reset(); mode = "provider-error"; await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows).includes("NEVER_")) + console.log("✓ Single certificate DELETE shared REST/MCP: any foreign cert, force references only, CA/CRL guards, opaque/source protections, native checkpoint/VERIFIED/history, lost unknown/no replay, revoke, ABA and privacy") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM mcp_protections WHERE object_ref->>'serverId'=$1", [String(serverId)]); await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + await rest.close(); router.closeAllConnections(); await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-certificate-delete-actions.test.ts b/backend/src/modules/mcp/ipsec-certificate-delete-actions.test.ts new file mode 100644 index 00000000..5b3d33ff --- /dev/null +++ b/backend/src/modules/mcp/ipsec-certificate-delete-actions.test.ts @@ -0,0 +1,13 @@ +import assert from "node:assert/strict" +import { hasVerifiedIpsecCertificateDeletion, prepareNativeIpsecCertificateDeletion } from "./ipsec-certificate-delete-actions.js" +import { redact } from "./security.js" +const input = { principal: { connectionId: "connection" }, receiptId: "receipt", name: "foreign-leaf", certificateId: "*a1", force: true, expectedAfterProof: "a".repeat(64), expectedProof: "b".repeat(64) } +const result = { receiptId: input.receiptId, name: input.name, certificateId: input.certificateId, force: true, verified: true, historyRecorded: true, revisionRecorded: true, commandCount: 1, completedCommands: 1, afterProof: input.expectedAfterProof } +const row = { connection_id: "connection", state: "verified", intended_state: redact({ kind: "service", service: "network.ipsec.certificates.delete", input }), result } +assert.equal(hasVerifiedIpsecCertificateDeletion(row, input, input.expectedAfterProof), true) +for (const patch of [{ state: "unknown" }, { state: "intent" }, { connection_id: "other" }, { intended_state: { ...row.intended_state, input: { ...input, force: false } } }]) assert.equal(hasVerifiedIpsecCertificateDeletion({ ...row, ...patch }, input, input.expectedAfterProof), false) +for (const patch of [{ receiptId: "other" }, { certificateId: "*a2" }, { force: false }, { historyRecorded: false }, { revisionRecorded: false }, { commandCount: 2 }, { completedCommands: 0 }, { afterProof: "c".repeat(64) }, { verified: false }]) assert.equal(hasVerifiedIpsecCertificateDeletion({ ...row, result: { ...result, ...patch } }, input, input.expectedAfterProof), false) +assert.equal(hasVerifiedIpsecCertificateDeletion(row, input, input.expectedProof), false) +await assert.rejects(prepareNativeIpsecCertificateDeletion(1, "foreign-leaf", {} as never)) +await assert.rejects(prepareNativeIpsecCertificateDeletion(1, "*c/invalid", {} as never)) +console.log("✓ IPsec reconciliation requires exact VERIFIED generic step, same connection/intended state/receipt/native ID/one acknowledged command/history proof") diff --git a/backend/src/modules/mcp/ipsec-certificate-delete-actions.ts b/backend/src/modules/mcp/ipsec-certificate-delete-actions.ts new file mode 100644 index 00000000..71996f9e --- /dev/null +++ b/backend/src/modules/mcp/ipsec-certificate-delete-actions.ts @@ -0,0 +1,103 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { deleteIpsecCertificateGuarded, readIpsecCertificateDeleteState, ipsecCertificateDeleteTarget, projectIpsecCertificateDelete, ipsecCertificateDeleteSchema } from "../../services/ipsec-certificate-delete.js" +import { ipsecPeerDeleteProof } from "../../services/ipsec-peer-delete.js" +import { maskNetworkSecrets, networkSecretValues } from "../../services/network-observed-read.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { routerClient } from "./resources.js" +import { accessGeneration, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const schema = ipsecCertificateDeleteSchema +const operation = "network.ipsec.certificates.delete" +async function access(input: Record, signal?: AbortSignal) { + if (signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение IPsec отменено", 409) + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const current = await effectiveMcpConnection(principal) + if (input.operationId !== operation || current.owner_id !== input.ownerId || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write") || !current.policy.scope.operationIds.includes(operation)) throw new McpError("PERMISSION_DENIED", "Допуск удаления IPsec отозван", 403) + if (!current.policy.scope.serverIds.includes(Number(input.serverId))) throw new McpError("SERVER_DENIED", "Сервер IPsec вне допуска", 403) + const request = schema.parse({ serverId: input.serverId, name: input.name, force: input.force }) + if (fingerprint(request) !== input.requestProof || fingerprint(current.policy) !== input.policyProof || fingerprint(current.owner_permissions) !== input.ownerProof || await accessGeneration() !== input.generation) throw new McpError("RESET_REQUIRED", "Допуск или запрос IPsec изменился", 409) + if (signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение IPsec отменено", 409) +} +async function source(input: Record, signal?: AbortSignal, allow: () => Promise = async () => {}) { + const authorize = async () => { await allow(); await access(input, signal) } + await authorize() + const { server } = await routerClient(Number(input.serverId)), state = await readIpsecCertificateDeleteState(server, authorize) + if (input.expectedSource !== undefined && input.expectedSource !== state.source) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился после подготовки", 409) + await authorize() + return { server, state, authorize } +} +export function hasVerifiedIpsecCertificateDeletion(candidate: unknown, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | null + return row.connection_id === (input.principal as { connectionId?: string } | undefined)?.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && result.receiptId === input.receiptId && result.verified === true && result.historyRecorded === true && result.revisionRecorded === true && result.name === input.name && result.certificateId === input.certificateId && result.force === input.force && result.commandCount === 1 && result.completedCommands === 1 && result.afterProof === proof && proof === input.expectedAfterProof +} +export async function prepareIpsecCertificateDeletion(serverId: number, name: string, context: ActionContext, force = false): Promise { + const request = schema.parse({ serverId, name, force }) + const internal = { ...request, requestProof: fingerprint(request), principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions), generation: await accessGeneration(), operationId: operation, receiptId: randomUUID() } + const { state, authorize } = await source(internal, context.signal) + if (maskNetworkSecrets(name, networkSecretValues(Object.values(state.catalogs).flat())) !== name) throw new McpError("STATE_UNAVAILABLE", "Имя сертификата содержит закрытые данные", 409) + const { cert, usedBy } = ipsecCertificateDeleteTarget(state, name, force) + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(serverId), serverId }, { kind: "certificate", id: cert[".id"], serverId }] + for (const item of state.protections as { object_ref: { kind: string; id: string } }[]) objects.push({ kind: item.object_ref.kind, id: item.object_ref.id, serverId }) + await authorize() + return { target: { scope: "servers", serverIds: [serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: 2, before: { proof: state.proof }, + steps: [{ kind: "service", service: operation, input: { ...internal, expectedSource: state.source, expectedProof: state.proof, certificateId: cert[".id"], expectedAfterProof: await ipsecPeerDeleteProof(projectIpsecCertificateDelete(state, cert[".id"])), proofRevision: 2, beforeFingerprint: fingerprint({ proof: state.proof }) } }], + warnings: [`Одна native DELETE выбранного сертификата. Ссылок peers/identities: ${usedBy.length}; force=${force}. Другие сертификаты, identities, peers и политики сохраняются; force разрешает разрыв ссылок, но не CA cascade или CRL bypass.`, "Удаление и managed-only история приложения учитываются отдельно. Потеря ответа останавливает операцию без повтора; совпадение конфигурации не подтверждает доставку ответа, историю или передачу трафика."] } +} +/** Native entry point uses the complete business guards and never exposes its raw catalog. */ +export async function prepareNativeIpsecCertificateDeletion(serverId: number, nativeId: string, context: ActionContext): Promise { + z.number().int().positive().safe().parse(serverId) + z.string().regex(/^\*[a-z0-9]+$/i).max(100).parse(nativeId) + const request = schema.parse({ serverId, name: "native-certificate-resolution", force: false }) + const internal = { ...request, requestProof: fingerprint(request), principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions), generation: await accessGeneration(), operationId: operation, receiptId: randomUUID() } + const aliasGrant = async () => { + const grant = await effectiveMcpConnection(context.principal) + if (!hasPermission(grant.owner_permissions, "mm:certificates:write") || !hasPermission(grant.policy.permissions, "mm:certificates:write") || !grant.policy.scope.operationIds.includes("ros.certificate.delete")) throw new McpError("PERMISSION_DENIED", "Native удаление сертификата вне допуска", 403) + } + // source rechecks network write, business scope, owner/policy/generation and alias grant before every GET. + const before = await source(internal, context.signal, aliasGrant) + const matches = before.state.catalogs["/certificate"].filter(row => row[".id"] === nativeId) + if (matches.length !== 1 || !matches[0].name?.trim()) throw new McpError("NOT_FOUND", "Native сертификат не найден однозначно", 404) + const name = matches[0].name.trim() + if (before.state.catalogs["/certificate"].filter(row => row.name?.trim() === name).length !== 1) throw new McpError("STATE_CONFLICT", "Неоднозначное имя native сертификата", 409) + const evidence = await prepareIpsecCertificateDeletion(serverId, name, context, false) + const step = evidence.steps[0] + if (step.kind !== "service" || step.input.certificateId !== nativeId || step.input.expectedProof !== before.state.proof || step.input.expectedSource !== before.state.source) throw new McpError("STATE_CONFLICT", "Native сертификат изменился во время подготовки", 409) + await before.authorize() + return evidence +} +export function registerIpsecCertificateDeleteActions(register: typeof add) { + const current = async (input: Record) => ({ proof: (await source(input)).state.proof }) + registerMcpService(operation, { current, + execute: async input => { + const before = await source(input) + return deleteIpsecCertificateGuarded(before.server, String(input.name), input.force === true, String(input.expectedProof), async () => { await assertVerifiedConfigurationAccess(); await before.authorize() }, { receiptId: String(input.receiptId), checkpoint: async value => { + await assertVerifiedConfigurationAccess(); await before.authorize() + await audit(pool, String(input.ownerId), "ipsec.certificate.delete.checkpoint", (input.principal as { connectionId: string }).connectionId, { ...value, receiptId: input.receiptId, inputProof: fingerprint(redact(input)) }) + } }) + }, + verify: async (input, result) => { + const latest = await source(input), candidate = { connection_id: (input.principal as { connectionId: string }).connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result } + return hasVerifiedIpsecCertificateDeletion(candidate, input, latest.state.proof) + }, + reconcile: async (input, allow) => { + if (input.proofRevision !== 2 || !z.string().uuid().safeParse(input.receiptId).success || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof) || typeof input.expectedAfterProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedAfterProof)) throw new McpError("UNSUPPORTED_CAPABILITY", "Нет совместимого private proof; требуется ручная сверка", 409) + const latest = await source(input, undefined, allow) + const generic = (await pool.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [(input.principal as { connectionId: string }).connectionId, input.receiptId])).rows + const checkpoints = (await pool.query("SELECT payload FROM mcp_audit WHERE action='ipsec.certificate.delete.checkpoint' AND actor_id=$1 AND connection_id=$2 AND payload->>'receiptId'=$3 AND payload->>'inputProof'=$4 ORDER BY id DESC LIMIT 1", [input.ownerId, (input.principal as { connectionId: string }).connectionId, input.receiptId, fingerprint(redact(input))])).rows + const cp = checkpoints[0]?.payload, checkpointMatches = cp?.certificateId === input.certificateId && cp.commandCount === 1 && cp.completedCommands === 1 && cp.afterProof === latest.state.proof + const verifiedFinal = checkpointMatches && generic.length === 1 && hasVerifiedIpsecCertificateDeletion(generic[0], input, latest.state.proof) + const final = await source(input, undefined, allow) + if (final.state.proof !== latest.state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился во время сверки", 409) + return { matchesOriginalState: latest.state.proof === input.expectedProof, configurationMatchesDesiredState: latest.state.proof === input.expectedAfterProof, matchesDesiredState: verifiedFinal, verifiedFinalCheckpointAvailable: verifiedFinal, automaticReplay: false, forwardingVerified: false } + }, + }) + register("network.ipsec.certificates.delete", "mm_ipsec_certificate_remove_plan", "Удалить один IPsec сертификат с проверкой CA, CRL и ссылок", "network", "mm:network:write", "write", "servers", schema, { version: 2, prepare: (input, context) => prepareIpsecCertificateDeletion(Number(input.serverId), String(input.name), context, input.force === true) }) +} diff --git a/backend/src/modules/mcp/ipsec-existing-certificate-export-actions.test.ts b/backend/src/modules/mcp/ipsec-existing-certificate-export-actions.test.ts new file mode 100644 index 00000000..3b457c83 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-existing-certificate-export-actions.test.ts @@ -0,0 +1,145 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import type { ActionContext } from "./action-types.js" +import type { ConnectionRow } from "./store.js" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import { fingerprint, McpError, redact } from "./security.js" +import { createIpsecExistingCertificateExportAdapter, ipsecExistingCertificateExportActionSchema, type IpsecExistingCertificateExportAdapterDependencies } from "./ipsec-existing-certificate-export-actions.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION as operation, ipsecExistingCertificateExportSecretPurpose, type IpsecExistingCertificateExportAuthority as Authority, type IpsecExistingCertificateExportState as State } from "../../services/ipsec-existing-certificate-export-model.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { ipsecUserUpdateValue } from "../../services/ipsec-user-update.js" +import { findMcpService } from "./service-registry.js" +import { createIpsecExistingCertificateExportFoundation } from "../../services/ipsec-existing-certificate-export.js" + +const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } +const connection = { id: principal.connectionId, owner_id: "owner", owner_permissions: ["mm:network:write"], owner_valid_until: new Date(Date.now() + 600000), generation: "generation", policy: { status: "active", mode: "autonomous", expiresAt: principal.credentialExpiresAt, permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: [operation], allowGlobal: false } } } as ConnectionRow +const context = { connection, principal } as ActionContext +const server = { id: 1, host: "fixture.test" } as ServerRow +const endpoint = "a".repeat(64), reference = "b".repeat(64), secretRef = randomUUID() +const request = { serverId: 1, selector: { kind: "client", clientId: "identity:*i" }, privateKeyExport: true, passphrase: { kind: "reference", secretRef } } +const state = (): State => ({ source: endpoint, proof: "", protections: [], catalogs: { + ...Object.fromEntries([...ipsecPeerDeletePaths, "/file"].map(path => [path, []])), + "/certificate": [{ ".id": "*c", name: "bound-cert", "common-name": "Original name", "key-usage": "tls-client", ca: "ca", opaque: "NEVER_PRIVATE_CERT" }, { ".id": "*a", name: "ca", authority: "true", "key-usage": "key-cert-sign", "private-key": "true" }, { ".id": "*s", name: "ipsec-server", "key-usage": "tls-server", "common-name": "vpn.fixture.test" }, { ".id": "*o", name: "foreign", opaque: "NEVER_FOREIGN_CERT" }], + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", certificate: "ipsec-server" }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", comment: "MikrotikManager:ipsec user=Renamed label", "auth-method": "digital-signature", "remote-certificate": "bound-cert", secret: "NEVER_PSK" }], +} }) +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Real SQL forbidden") }) as never +pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +try { + let current = structuredClone(connection), live = state(), source = endpoint, ref = reference, calls = 0, clock = Date.now(), materialCalls = 0, generated = 0 + let row: Record[] = [], artifactValid = true, executeHook: (() => void) | undefined, accessHook: (() => void) | undefined + let secret = { owner: "owner", server: 1, purpose: ipsecExistingCertificateExportSecretPurpose(1, { kind: "client", clientId: "identity:*i" }), revision: "1" } + const sealed = new Map(), artifacts = new Map() + let received: Authority | undefined + const deps: IpsecExistingCertificateExportAdapterDependencies = { + connection: async () => current, server: async () => server, sourceProof: async () => source, + read: async (_server, authorize, control) => { control.signal.throwIfAborted(); await authorize(); const result = structuredClone(live); result.proof = fingerprint(ipsecUserUpdateValue(result)); await authorize(); return result }, + material: async (a, authorize) => { materialCalls++; await authorize(); if (a.request.passphrase.kind === "generate") return { referenceProof: fingerprint({ generate: a.receiptId }) }; assert.equal(a.request.passphrase.secretRef, secretRef); if (secret.owner !== a.ownerId || secret.server !== a.request.serverId || secret.purpose !== ipsecExistingCertificateExportSecretPurpose(a.request.serverId, a.request.selector)) throw new McpError("SECRET_UNAVAILABLE", "Fixture reference rejected", 409); await authorize(); return { referenceProof: fingerprint({ ref, ...secret }) } }, + seal: async text => { const key = `sealed-${randomUUID()}`; sealed.set(key, text); return key }, open: async key => { const value = sealed.get(key); if (!value) throw new McpError("STATE_CONFLICT", "Unknown binding", 409); return value }, + verifiedAccess: async () => { accessHook?.() }, + execute: async a => { executeHook?.(); calls++; received = a; if (a.request.passphrase.kind === "generate") generated++; const artifactId = randomUUID(); artifacts.set(artifactId, fingerprint(a)); return { ok: true, verified: true, receiptId: a.receiptId, serverId: a.request.serverId, certificateId: a.certificateId, commandCount: 2, completedCommands: 2, confirmedWrites: 2, afterProof: a.expectedProof, artifactId, downloadPath: `/api/mcp/artifacts/${artifactId}`, expiresInSeconds: 900, bytes: 123, sha256: "c".repeat(64), humanDownloadOnly: true, ipsecContentReturned: false, automaticReplay: false, forwardingVerified: false } }, + verifyArtifact: async (id, a) => artifactValid && artifacts.get(id) === fingerprint(a), query: async () => row, clock: () => clock, + } + const adapter = createIpsecExistingCertificateExportAdapter(deps) + const reset = () => { current = structuredClone(connection); live = state(); source = endpoint; ref = reference; row = []; artifactValid = true; received = undefined; executeHook = undefined; accessHook = undefined; clock = Date.now(); secret = { owner: "owner", server: 1, purpose: ipsecExistingCertificateExportSecretPurpose(1, { kind: "client", clientId: "identity:*i" }), revision: "1" } } + const prepare = async (raw: Record = request) => { + const evidence = await adapter.prepare(raw, context) + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 2) + assert(evidence.objects.some(ref => ref.kind === "certificate" && ref.id === "*c") || (raw.selector as { kind: string }).kind === "certificate") + assert(!JSON.stringify(evidence).includes("NEVER_")); assert(!JSON.stringify(evidence).includes(secretRef)) + const step = evidence.steps[0]; if (step.kind !== "service") throw Error("Expected service") + const binding = JSON.parse(sealed.get(String(step.input.bindingCiphertext))!) + assert(!Object.hasOwn(binding, "inputProof")); assert.equal(binding.request.privateKeyExport, true) + return step.input + } + let input = await prepare(), result = await adapter.service.execute(input) as Record + assert.equal(calls, 1); assert.equal(received!.certificateName, "bound-cert"); assert.equal(received!.certificateId, "*c") + assert.equal(received!.inputProof, fingerprint(redact(input))) + assert.equal(await adapter.service.verify!(input, result), true) + assert(!JSON.stringify(result).includes("NEVER_")); assert(!Object.hasOwn(result, "bundle")) + assert.deepEqual(await adapter.service.reconcile!(input, async () => {}), { matchesOriginalState: true, matchesDesiredState: false, automaticReplay: false, forwardingVerified: false }) + row = [{ connection_id: principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result }] + assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, true) + for (const mutate of [(r: Record) => { r.state = "intent" }, (r: Record) => { r.connection_id = randomUUID() }, (r: Record) => { r.intended_state = {} }]) { const saved = structuredClone(row); mutate(row[0]); assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, false); row = saved } + row.push(structuredClone(row[0])); assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, false); row.pop() + for (const patch of [{ certificateId: "*wrong" }, { completedCommands: 1 }, { confirmedWrites: 1 }, { afterProof: "d".repeat(64) }, { downloadPath: "/wrong" }, { humanDownloadOnly: false }, { automaticReplay: true }, { bundle: "NEVER_PKCS12" }, { passphrase: "NEVER_PASSPHRASE" }, { contentB64: "NEVER_PKCS12" }]) assert.equal(await adapter.service.verify!(input, { ...result, ...patch }), false) + artifactValid = false; assert.equal(await adapter.service.verify!(input, result), false); assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, false) + for (const change of [() => { current.owner_id = "other" }, () => { current.generation = "revoked" }, () => { current.policy.scope.serverIds = [2] }, () => { current.policy.scope.operationIds = [] }, () => { current.owner_permissions = [] }, () => { current.policy.permissions = [] }, () => { current.policy.mode = "observer" }, () => { source = "e".repeat(64) }, () => { ref = "f".repeat(64) }, () => { secret.owner = "other" }, () => { secret.server = 2 }, () => { secret.purpose = "wrong" }, () => { secret.revision = "2" }, () => { live.protections.push({ object_ref: { kind: "certificate", id: "*c", protection: "deny" } }) }, () => { live.catalogs["/certificate"][0].opaque = "changed" }, () => { clock += 180001 }]) { + reset(); input = await prepare(); const before: number = calls; change(); await assert.rejects(adapter.service.execute(input)); assert.equal(calls, before) + } + for (const patch of [{ serverId: 2 }, { receiptId: randomUUID() }, { extra: true }, { bindingCiphertext: "unknown" }]) { reset(); input = await prepare(); const before: number = calls; await assert.rejects(adapter.service.execute({ ...input, ...patch })); assert.equal(calls, before) } + for (const patch of [{ privateKeyExport: false }, { passphrase: { kind: "reference", secretRef: "invalid" } }, { passphrase: { kind: "value", value: "NEVER_PASSPHRASE" } }, { psk: "NEVER_PSK" }, { extra: true }]) assert.equal(ipsecExistingCertificateExportActionSchema.safeParse({ ...request, ...patch }).success, false) + for (const change of [() => { live.catalogs["/ip/ipsec/identity"][0]["auth-method"] = "pre-shared-key" }, () => { live.catalogs["/ip/ipsec/identity"][0].comment = "foreign" }, () => { delete live.catalogs["/ip/ipsec/identity"][0]["remote-certificate"] }]) { reset(); change(); await assert.rejects(prepare()) } + reset(); await assert.rejects(prepare({ ...request, selector: { kind: "client", clientId: "cert:*c" } })) + for (const name of ["bound-cert", "ipsec-server", "foreign", "ca"]) { + reset(); const raw = { ...request, selector: { kind: "certificate", name }, passphrase: { kind: "generate" }, caPrivateKeyExport: name === "ca" } + input = await prepare(raw); assert.equal(generated, 0); result = await adapter.service.execute(input) as Record; assert.equal(received!.certificateName, name); assert.equal(await adapter.service.verify!(input, result), true); generated = 0 + } + reset(); await assert.rejects(prepare({ ...request, selector: { kind: "certificate", name: "ca" }, passphrase: { kind: "generate" } })) + reset(); const aborted = new AbortController(); aborted.abort(); const before: number = materialCalls; await assert.rejects(adapter.prepare(request, { ...context, signal: aborted.signal })); assert.equal(materialCalls, before) + reset(); input = await prepare(); const cancel = new AbortController(); cancel.abort(); const count = calls; await assert.rejects(adapter.service.execute(input, { operationId: randomUUID(), phase: "forward", stepIndex: 0, signal: cancel.signal })); assert.equal(calls, count) + reset(); input = await prepare(); await assert.rejects(adapter.service.reconcile!(input, async () => { current.generation = "late-revoke" })); assert.equal(calls, count) + for (const change of [() => { current.generation = "late-revoke" }, () => { source = "e".repeat(64) }, () => { live.protections.push({ object_ref: { kind: "certificate", id: "*c", protection: "deny" } }) }, () => { clock += 180001 }]) { + reset(); input = await prepare(); accessHook = change; const before: number = calls + await assert.rejects(adapter.service.execute(input)); assert.equal(calls, before) + } + reset(); input = await prepare(); executeHook = () => { current.generation = "after-dispatch-revoke" } + await assert.rejects(adapter.service.execute(input)) + reset(); input = await prepare(); result = await adapter.service.execute(input) as Record + const lateArtifact = createIpsecExistingCertificateExportAdapter({ ...deps, verifyArtifact: async () => { current.generation = "artifact-revoke"; return true } }) + await assert.rejects(lateArtifact.service.verify!(input, result)) + reset(); live.protections.push({ object_ref: { kind: "certificate", id: "*c", protection: "human-approval-required", privateNote: "NEVER_PROTECTION_PRIVATE" } }) + input = await prepare() + reset(); input = await prepare() + const blocked = createIpsecExistingCertificateExportAdapter({ ...deps, read: async () => new Promise(() => {}) }) + const key = String(input.bindingCiphertext), binding = JSON.parse(sealed.get(key)!) + binding.deadlineAt = clock + 25; sealed.set(key, JSON.stringify(binding)) + const keepAlive = setTimeout(() => {}, 1000), beforeBlocked = calls + try { await assert.rejects(blocked.service.execute(input)); assert.equal(calls, beforeBlocked) } finally { clearTimeout(keepAlive) } + // A legacy unqualified native ID shared with a cert-only row is ambiguous. + reset(); live.catalogs["/certificate"].push({ ".id": "*i", name: "ipsec-user-another", "key-usage": "tls-client" }) + await assert.rejects(prepare({ ...request, selector: { kind: "client", clientId: "*i" }, passphrase: { kind: "generate" } })) + input = await prepare({ ...request, passphrase: { kind: "generate" } }) + assert.equal((await adapter.service.execute(input) as Record).certificateId, "*c") + // Compose the real adapter and real executor, replacing only I/O. This also + // exercises the actual receipt spread and the durable claim input binding. + reset() + let transferred: Buffer | undefined, nativeCommands = 0 + const claims = new Set(), checkpoints: number[] = [] + const privateBytes = "NEVER_SYNTHETIC_COMPOSED_PKCS12" + const executeFoundation = createIpsecExistingCertificateExportFoundation({ + server: deps.server, sourceProof: deps.sourceProof, access: async () => {}, + material: deps.material, proof: async s => fingerprint(ipsecUserUpdateValue(s)), + read: (s, authorize, control) => deps.read(s, authorize, { signal: control?.signal ?? AbortSignal.timeout(1000), deadlineAt: control?.deadlineAt ?? clock + 1000 }), + boundary: async (_a, work) => work(), + claim: async a => { assert.equal(a.inputProof, fingerprint(redact(integratedInput))); if (claims.has(a.receiptId)) throw new McpError("STATE_CONFLICT", "already claimed", 409); claims.add(a.receiptId) }, + checkpoint: async (_a, value) => { checkpoints.push(value.completedCommands) }, + generatePassphrase: () => "NEVER_SYNTHETIC_COMPOSED_PASSWORD", + publish: async (a, bundle) => { + assert.equal(Buffer.from(bundle.contentB64!, "base64").toString(), privateBytes) + const artifactId = randomUUID(); artifacts.set(artifactId, fingerprint(a)) + return { artifactId, downloadPath: `/api/mcp/artifacts/${artifactId}`, expiresInSeconds: 900, bytes: 123, sha256: "c".repeat(64), humanDownloadOnly: true, ipsecContentReturned: false } + }, + client: () => ({ + post: async (_path: string, fields: Record, _timeout: number, _signal: AbortSignal, fence: () => Promise) => { await fence(); nativeCommands++; live.catalogs["/file"].push({ ".id": "*ownedexport", name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(privateBytes)) }); return {} }, + downloadOwnedFile: async (_file: unknown, control: { authorize: () => Promise }) => { await control.authorize(); transferred = Buffer.from(privateBytes); return transferred }, + delete: async (_path: string, _timeout: number, fence: () => Promise) => { await fence(); nativeCommands++; live.catalogs["/file"] = [] }, + }) as never, + }) + const composed = createIpsecExistingCertificateExportAdapter({ ...deps, execute: executeFoundation }) + const evidence = await composed.prepare(request, context), step = evidence.steps[0] + assert.equal(step.kind, "service"); if (step.kind !== "service") throw Error("Expected service") + const integratedInput = step.input + const composedReceipt = await composed.service.execute(integratedInput) + assert.equal(await composed.service.verify!(integratedInput, composedReceipt), true) + assert.equal(nativeCommands, 2); assert.deepEqual(checkpoints, [1, 2]); assert.ok(transferred!.every(byte => byte === 0)) + assert.ok(!JSON.stringify(composedReceipt).includes("NEVER_")) + await assert.rejects(composed.service.execute(integratedInput)); assert.equal(nativeCommands, 2) + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true); assert.equal(findMcpService(operation), undefined) + console.log("Unregistered existing-IPsec export adapter: sealed noncircular authority, both selectors/all roles, owner/source/protection/scope/reference/deadline checks, exact private receipt/artifact and read-only reconcile; no real SQL/native/PKI") +} finally { pool.query = oldQuery; pool.connect = oldConnect } + + + diff --git a/backend/src/modules/mcp/ipsec-existing-certificate-export-actions.ts b/backend/src/modules/mcp/ipsec-existing-certificate-export-actions.ts new file mode 100644 index 00000000..9cb33f32 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-existing-certificate-export-actions.ts @@ -0,0 +1,142 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { McpServiceHandler } from "./service-registry.js" +import { hasPermission } from "../../lib/permissions.js" +import { fingerprint, McpError, redact } from "./security.js" +import { ipsecExistingCertificateExportAuthoritySchema, ipsecExistingCertificateExportRequestSchema, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION as operation, planIpsecExistingCertificateExport, type IpsecExistingCertificateExportAuthority as Authority, type IpsecExistingCertificateExportState as State } from "../../services/ipsec-existing-certificate-export-model.js" + +export const ipsecExistingCertificateExportActionSchema = ipsecExistingCertificateExportRequestSchema +const bindingSchema = ipsecExistingCertificateExportAuthoritySchema.omit({ inputProof: true }).extend({ deadlineAt: z.number().int().positive() }).strict() +type Binding = z.infer +const inputSchema = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(262144) }).strict() +const protectionSchema = z.array(z.object({ object_ref: z.object({ kind: z.string().min(1), id: z.string().min(1), protection: z.string().optional() }).passthrough() }).strict()).max(1000) +const receiptSchema = z.object({ ok: z.literal(true), verified: z.literal(true), receiptId: z.string().uuid(), certificateId: z.string(), serverId: z.number().int().positive(), commandCount: z.literal(2), completedCommands: z.literal(2), confirmedWrites: z.literal(2), afterProof: z.string().regex(/^[a-f0-9]{64}$/), automaticReplay: z.literal(false), forwardingVerified: z.literal(false), artifactId: z.string().uuid(), downloadPath: z.string(), humanDownloadOnly: z.literal(true), ipsecContentReturned: z.literal(false), expiresInSeconds: z.literal(900), bytes: z.number().int().positive().max(1048576), sha256: z.string().regex(/^[a-f0-9]{64}$/) }).strict() + +/** No production defaults or registration: importing this factory cannot activate + * existing-client/server/CA key export. Dependencies are an explicit boundary. */ +export interface IpsecExistingCertificateExportAdapterDependencies { + connection: (principal: Authority["principal"]) => Promise + server: (id: number) => Promise + sourceProof: (server: ServerRow) => Promise + read: (server: ServerRow, authorize: () => Promise, control: { signal: AbortSignal; deadlineAt: number }) => Promise + material: (authority: Authority, authorize: () => Promise) => Promise<{ referenceProof: string }> + seal: (value: string) => Promise + open: (value: string) => Promise + execute: (authority: Authority, options: { signal: AbortSignal; timeoutMs: number }) => Promise + verifyArtifact: (id: string, authority: Authority, afterProof: string) => Promise + query: (sql: string, values: unknown[]) => Promise[]> + verifiedAccess: () => Promise + clock?: () => number +} +const conflict = () => new McpError("STATE_CONFLICT", "Допуск, источник, клиент или passphrase reference экспорта изменились", 409) + +export function createIpsecExistingCertificateExportAdapter(deps: IpsecExistingCertificateExportAdapterDependencies) { + const now = deps.clock ?? Date.now + const control = (deadlineAt: number, external?: AbortSignal) => { + const remaining = deadlineAt - now() + if (remaining <= 0) throw new McpError("READ_CANCELLED", "Срок плана приватного экспорта истёк", 409) + const timer = AbortSignal.timeout(Math.min(remaining, 180000)) + const signal = external ? AbortSignal.any([external, timer]) : timer + const check = () => { signal.throwIfAborted(); if (now() >= deadlineAt) throw new McpError("READ_CANCELLED", "Срок экспорта истёк", 409) } + const run = async (work: () => Promise) => { + check(); let abort!: () => void + const cancelled = new Promise((_resolve, reject) => { abort = () => reject(signal.reason); signal.addEventListener("abort", abort, { once: true }) }) + try { const value = await Promise.race([work(), cancelled]); check(); return value } finally { signal.removeEventListener("abort", abort) } + } + return { signal, deadlineAt, check, run } + } + const access = async (binding: Binding) => { + const c = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= now() || c.owner_id !== binding.ownerId || c.owner_valid_until.getTime() <= now() || c.policy.status !== "active" || c.policy.mode === "observer" || Date.parse(c.policy.expiresAt) <= now() || !c.policy.scope.serverIds.includes(binding.request.serverId) || !c.policy.scope.operationIds.includes(operation) || !hasPermission(c.owner_permissions, "mm:network:write") || !hasPermission(c.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска приватного экспорта", 403) + if (c.generation !== binding.generation || fingerprint(c.policy) !== binding.policyProof || fingerprint(c.owner_permissions) !== binding.ownerProof || fingerprint(binding.request) !== binding.requestProof) throw conflict() + } + // Persisted intended_state is redacted. Derive its exact proof after sealing; + // the sealed binding deliberately contains no proof of its own ciphertext. + const authority = (binding: Binding, input: Record): Authority => { + const { deadlineAt: _deadlineAt, ...value } = binding + return ipsecExistingCertificateExportAuthoritySchema.parse({ ...value, inputProof: fingerprint(redact(input)) }) + } + const unpack = async (raw: Record, signal?: AbortSignal) => { + const input = inputSchema.parse(raw), opening = control(now() + 45000, signal) + const binding = bindingSchema.parse(JSON.parse(await opening.run(() => deps.open(input.bindingCiphertext)))) + if (binding.request.serverId !== input.serverId || binding.receiptId !== input.receiptId) throw conflict() + return { input, binding, authority: authority(binding, input) } + } + const source = async (binding: Binding, a: Authority, c: ReturnType, external: () => Promise = async () => {}, compareReference = true) => { + const authorize = async () => { c.check(); await c.run(external); await c.run(() => access(binding)) } + const check = async () => { + await authorize() + const fresh = await c.run(() => deps.server(binding.request.serverId)) + if (!fresh || await c.run(() => deps.sourceProof(fresh)) !== binding.sourceProof) throw conflict() + const material = await c.run(() => deps.material(a, authorize)) + if (compareReference && material.referenceProof !== binding.referenceProof) throw conflict() + await authorize() + return { server: fresh, material } + } + const { server, material } = await check() + const state = await c.run(() => deps.read(server, check, { signal: c.signal, deadlineAt: c.deadlineAt })) + await check() + if (state.source !== binding.sourceProof) throw conflict() + const protections = protectionSchema.safeParse(state.protections) + if (!protections.success) throw new McpError("STATE_UNAVAILABLE", "Каталог защит приватного экспорта не подтверждён", 502) + if (protections.data.some(row => row.object_ref.protection === "deny")) throw new McpError("PROTECTED_OBJECT", "Приватный экспорт заблокирован защитой", 409) + return { server, state, material, protections: protections.data } + } + const receipt = (raw: unknown, b: Binding, proof: string) => { + const parsed = receiptSchema.safeParse(raw) + if (!parsed.success) return undefined + const r = parsed.data + return r.receiptId === b.receiptId && r.serverId === b.request.serverId && r.certificateId === b.certificateId && r.afterProof === proof && proof === b.expectedProof && r.downloadPath === `/api/mcp/artifacts/${r.artifactId}` ? r : undefined + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + const request = ipsecExistingCertificateExportActionSchema.parse(raw), deadlineAt = now() + 180000, c = control(deadlineAt, context.signal) + const b: Binding = bindingSchema.parse({ principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, request, receiptId: randomUUID(), certificateId: "*0", certificateName: "pending", role: "other", sourceProof: "0".repeat(64), expectedProof: "0".repeat(64), referenceProof: "0".repeat(64), requestProof: fingerprint(request), proofRevision: 1, deadlineAt }) + await c.run(() => access(b)) + const server = await c.run(() => deps.server(request.serverId)); if (!server) throw conflict() + b.sourceProof = await c.run(() => deps.sourceProof(server)) + const provisional = authority(b, {}), before = await source(b, provisional, c, undefined, false) + b.referenceProof = before.material.referenceProof + const plan = planIpsecExistingCertificateExport(before.state, server, request, b.receiptId) + b.certificateId = plan.certificateId; b.certificateName = plan.certificateName; b.role = plan.role; b.expectedProof = before.state.proof + if ((await source(b, authority(b, {}), c)).state.proof !== b.expectedProof) throw conflict() + const input = inputSchema.parse({ serverId: request.serverId, receiptId: b.receiptId, bindingCiphertext: await c.run(() => deps.seal(JSON.stringify(b))) }) + await c.run(() => access(b)) + return { target: { scope: "servers", serverIds: [request.serverId] }, objects: [{ kind: "server", id: String(request.serverId), serverId: request.serverId }, { kind: "certificate", id: plan.certificateId, serverId: request.serverId }, ...plan.companions.map(ref => ({ ...ref, serverId: request.serverId })), ...before.protections.map(row => ({ kind: row.object_ref.kind, id: row.object_ref.id, serverId: request.serverId }))], snapshots: [], before: { proof: b.expectedProof }, requiresHuman: true, mutationCost: 2, validUntil: new Date(deadlineAt).toISOString(), steps: [{ kind: "service", service: operation, input }], warnings: ["Подтверждение разрешает экспорт закрытого ключа выбранного существующего сертификата, чтение PKCS12 и удаление точного временного файла. Две native-команды без повторов и fallback.", ...(plan.role === "ca" ? ["Выбран закрытый ключ CA: отдельное caPrivateKeyExport обязательно; CREATE-клиент approval его не покрывает."] : []), "Приватный пакет доступен только вошедшему владельцу с network-write на 15 минут. Модель получает только квитанцию; готовность передачи трафика не подтверждается."] } + } + const service: McpServiceHandler = { + current: async raw => { const x = await unpack(raw); return { proof: (await source(x.binding, x.authority, control(x.binding.deadlineAt))).state.proof } }, + execute: async (raw, context) => { + const signal = context && "signal" in context ? context.signal : undefined + const x = await unpack(raw, signal), c = control(x.binding.deadlineAt, signal) + const before = await source(x.binding, x.authority, c) + const plan = planIpsecExistingCertificateExport(before.state, before.server, x.binding.request, x.binding.receiptId) + if (before.state.proof !== x.binding.expectedProof || plan.certificateId !== x.binding.certificateId || plan.certificateName !== x.binding.certificateName || plan.role !== x.binding.role) throw conflict() + await c.run(deps.verifiedAccess) + if ((await source(x.binding, x.authority, c)).state.proof !== x.binding.expectedProof) throw conflict() + // Executor remains responsible for durable intent/claim and per-dispatch fences. + const result = await c.run(() => deps.execute(x.authority, { signal: c.signal, timeoutMs: Math.min(180000, x.binding.deadlineAt - now()) })) + const after = await source(x.binding, x.authority, c), r = receipt(result, x.binding, after.state.proof) + if (!r || !await c.run(() => deps.verifyArtifact(r.artifactId, x.authority, r.afterProof))) throw new McpError("VERIFICATION_FAILED", "Приватный экспорт не подтверждён точной квитанцией", 502) + if ((await source(x.binding, x.authority, c)).state.proof !== r.afterProof) throw conflict() + return r + }, + verify: async (raw, result) => { + const x = await unpack(raw), c = control(x.binding.deadlineAt), after = await source(x.binding, x.authority, c), r = receipt(result, x.binding, after.state.proof) + if (!r || !await c.run(() => deps.verifyArtifact(r.artifactId, x.authority, r.afterProof))) return false + return (await source(x.binding, x.authority, c)).state.proof === r.afterProof + }, + reconcile: async (raw, external) => { + const x = await unpack(raw), c = control(x.binding.deadlineAt), latest = await source(x.binding, x.authority, c, external) + const rows = await c.run(() => deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id JOIN mcp_plans p ON p.id=o.plan_id WHERE o.connection_id=$1 AND p.operation_id=$2 AND p.approved_by IS NOT NULL AND o.state='applied' AND s.intended_state->'input'->>'receiptId'=$3 LIMIT 2", [x.binding.principal.connectionId, operation, x.binding.receiptId])) + const row = rows[0], r = receipt(row?.result, x.binding, latest.state.proof) + const exact = rows.length === 1 && row.connection_id === x.binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input: x.input })) + const desired = exact && !!r && await c.run(() => deps.verifyArtifact(r.artifactId, x.authority, r.afterProof)) + if ((await source(x.binding, x.authority, c, external)).state.proof !== latest.state.proof) throw conflict() + return { matchesOriginalState: latest.state.proof === x.binding.expectedProof, matchesDesiredState: desired, automaticReplay: false, forwardingVerified: false } + }, + } + return { prepare, service } +} + diff --git a/backend/src/modules/mcp/ipsec-existing-certificate-export-http.sql.test.ts b/backend/src/modules/mcp/ipsec-existing-certificate-export-http.sql.test.ts new file mode 100644 index 00000000..3b0831d7 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-existing-certificate-export-http.sql.test.ts @@ -0,0 +1,198 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, resolve } from "node:path" +import { mkdir, realpath } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import type { AuthUser } from "../../lib/permissions.js" + +// Guard before importing config, configured pools, runtime or production code. +assert.equal(process.env.MMAPP_OWN_IPSEC_EXPORT_HTTP, "true") +const ownedUrl = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") +assert(["postgres:", "postgresql:"].includes(ownedUrl.protocol)) +assert.equal(ownedUrl.hostname, "127.0.0.1"); assert.equal(decodeURIComponent(ownedUrl.username), "mcp_test") +assert.equal(ownedUrl.pathname, "/postgres"); assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres") +assert.equal(ownedUrl.search, ""); assert.equal(ownedUrl.hash, "") +assert(process.env.MCP_STATE_DIR) +const ownedState = resolve(process.env.MCP_STATE_DIR) +assert.equal(basename(ownedState), "state"); assert.match(basename(dirname(ownedState)), /^mm-mcp-test-[A-Za-z0-9-]+$/) +assert.equal(resolve(await realpath(dirname(ownedState))).toLowerCase(), dirname(ownedState).toLowerCase()) +await mkdir(ownedState, { recursive: true, mode: 0o700 }) +assert.equal(resolve(await realpath(ownedState)).toLowerCase(), ownedState.toLowerCase()) + +const { default: Fastify } = await import("fastify") +const { serializerCompiler, validatorCompiler } = await import("@fastify/type-provider-zod") +const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") +const { env } = await import("../../config.js") +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { default: authPlugin, requireApiAuth } = await import("../../plugins/auth.js") +const { createIpsecRoutes } = await import("../../routes/ipsec.js") +const { createMcpRoutes } = await import("../../routes/mcp.js") +const { listActions } = await import("./actions.js") +const { createMcpConnection, authenticateMcpToken, updateMcpSettings, setConnectionStatus } = await import("./store.js") +const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") +const { encryptSecret, decryptSecret } = await import("./security.js") +const { assertPortalClaimsCurrent } = await import("./portal-revocation.js") +const { getEnabledIpsecServerById } = await import("../../services/ipsec-live.js") +const { ipsecPeerDeletePaths } = await import("../../services/ipsec-peer-delete.js") +const { ipsecManagedComment, ipsecUserComment } = await import("../../services/ipsec-config.js") +const { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION: operation, ipsecExistingCertificateExportSecretPurpose } = await import("../../services/ipsec-existing-certificate-export-model.js") + +assert.equal(env.authRequired, true) +assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) +const publicToolsBefore = listActions().length +const serverId = 267, fakeP12 = "NEVER_SYNTHETIC_EXISTING_PKCS12", passphrase = "NEVER_EXISTING_EXPORT_PASSPHRASE" +const actor: AuthUser = { id: "ipsec-existing-http-owner", email: "owned@example.invalid", name: "Owned existing export", apps: ["mm"], permissions: ["mm:network:write", "mm:network:read", "mm:settings:admin"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 3600000 } +type Row = Record +const base: Record = { ...Object.fromEntries([...ipsecPeerDeletePaths, "/file"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", certificate: "ipsec-server", comment: ipsecManagedComment() }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "remote-certificate": "actual-client-cert", "mode-config": "ipsec-vpn", comment: ipsecUserComment("Display-label") }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "ipsec-vpn", "static-dns": "192.0.2.53" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign,crl-sign", authority: "true", "private-key": "true" }, { ".id": "*s", name: "ipsec-server", "common-name": "vpn.example.test", "key-usage": "tls-server" }, { ".id": "*c", name: "actual-client-cert", "common-name": "Certificate CN", "key-usage": "tls-client", "private-key": "true" }, { ".id": "*o", name: "foreign-other", "common-name": "Other CN" }], + "/file": [{ ".id": "*f", name: "foreign.p12", size: "10" }], +} +let catalogs = structuredClone(base), serial = 0, mode = "normal" +const commands: Array<{ method: string; path: string; name?: string }> = [] +const router = createServer(async (req, res) => { + res.setHeader("content-type", "application/json") + try { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")) + 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 } + 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" }) + 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)) + } else throw Error("unexpected native write") + res.end("{}") + } catch { res.statusCode = 500; res.end('{"error":"synthetic router failure"}') } +}) +const rest = Fastify(), portal = Fastify(), cases: string[] = [], secretIds: string[] = [], connections: string[] = [] +let inserted = false +const hidden = (value: unknown) => { const encoded = JSON.stringify(value); assert(!encoded.includes("NEVER_")); assert(!encoded.includes(Buffer.from(fakeP12).toString("base64"))) } +const reset = () => { catalogs = structuredClone(base); mode = "normal" } +const wait = async (id: string) => { + for (const deadline = Date.now() + 120000; Date.now() < deadline;) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (row && !["pending", "running"].includes(row.state)) return row as { state: string; error_code: string | null } + await delay(25) + } + throw Error("Owned operation did not finish; never replay") +} +try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*)::text AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal((await pool.query("SELECT session_user AS role,current_database() AS db")).rows[0].role, "mcp_test") + await updateMcpSettings({ enabled: true }, actor) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + assert.equal(Number((await pool.query("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0) + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'owned-existing-export','127.0.0.1',$2,false,'synthetic-endpoint-only')", [serverId, (router.address() as { port: number }).port]); inserted = true + assert.equal(listActions().length, publicToolsBefore) + assert(listActions().some(action => action.operation.id === operation)) + rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + await rest.register(authPlugin); rest.addHook("preHandler", requireApiAuth) + await rest.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async () => { throw Error("Existing export must not create revision") } }), { prefix: "/api" }) + await portal.register(authPlugin) + await portal.register(createMcpRoutes(assertPortalClaimsCurrent), { prefix: "/api" }) + const token = (id = actor.id, permissions = actor.permissions) => rest.jwt.sign({ sub: id, email: actor.email, name: actor.name, apps: ["mm"], permissions, iss: env.authIssuer }, { expiresIn: "1h" }) + const headers = (key = randomUUID(), owner = actor.id, permissions = actor.permissions) => ({ authorization: `Bearer ${token(owner, permissions)}`, "idempotency-key": key }) + const restCall = (name?: string, key = randomUUID(), ca = false) => rest.inject({ method: "POST", url: name ? `/api/ipsec/certs/${serverId}/export` : `/api/ipsec/users/${serverId}/identity%3A*i/cert`, headers: headers(key), payload: { passphrase, privateKeyExport: true, caPrivateKeyExport: ca, ...(name ? { name } : {}) } }) + for (const target of [undefined, "actual-client-cert", "ipsec-server", "foreign-other", "ca"]) { + reset(); const start = commands.length, key = randomUUID(), response = await restCall(target, key, target === "ca") + assert.equal(response.statusCode, 200, `owned REST export status ${response.statusCode}`) + const bundle = response.json(); assert.equal(bundle.passphrase, passphrase); assert.equal(Buffer.from(bundle.contentB64, "base64").toString(), fakeP12); assert.equal(bundle.user, target ? target === "actual-client-cert" ? "Certificate CN" : target === "ipsec-server" ? "vpn.example.test" : target === "foreign-other" ? "Other CN" : "ca" : "Display-label") + assert.equal(commands.length, start + 2); assert.equal(commands[start].name, target ?? "actual-client-cert"); assert.deepEqual(catalogs, base) + assert.notEqual((await restCall(target, key, target === "ca")).statusCode, 200); assert.equal(commands.length, start + 2) + } + cases.push("actual REST JWT mappings both selectors and all four certificate roles with durable same-key refusal") + reset(); const badStart = commands.length + assert.equal((await restCall("ca")).statusCode, 409); assert.equal(commands.length, badStart) + const badBody = await rest.inject({ method: "POST", url: `/api/ipsec/certs/${serverId}/export`, headers: headers(), payload: { name: "actual-client-cert", passphrase, privateKeyExport: true, serverId: serverId + 1 } }); assert.equal(badBody.statusCode, 400) + assert.equal((await rest.inject({ method: "POST", url: `/api/ipsec/certs/${serverId}/export`, headers: headers(randomUUID(), actor.id, ["mm:network:read"]), payload: { name: "actual-client-cert", passphrase, privateKeyExport: true } })).statusCode, 403) + mode = "lost"; const lostKey = randomUUID(), lost = await restCall("actual-client-cert", lostKey); assert.notEqual(lost.statusCode, 200); hidden(lost.json()); assert.equal(commands.length, badStart + 1) + assert.notEqual((await restCall("actual-client-cert", lostKey)).statusCode, 200); assert.equal(commands.length, badStart + 1) + cases.push("actual REST native lost acknowledgement is never resent with the spent durable key; CA and scope mismatches block") + reset() + const connection = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned existing export", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: [operation, "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connections.push(connection.connection.id) + const principal = await authenticateMcpToken(connection.token) + const selector = { kind: "client" as const, clientId: "identity:*i" }, secretId = randomUUID(); secretIds.push(secretId) + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [secretId, ipsecExistingCertificateExportSecretPurpose(serverId, selector), serverId, await encryptSecret(passphrase), actor.id]) + const request = { serverId, selector, privateKeyExport: true, caPrivateKeyExport: false, passphrase: { kind: "reference", secretRef: secretId } } + const prepare = () => invokeMcpTool(principal, "mm_ipsec_existing_certificate_export_plan", request) as Promise<{ planId: string; state: string }> + const unapproved = await prepare(); assert.equal(unapproved.state, "awaiting-approval"); hidden(unapproved) + const beforeApply = commands.length + await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, beforeApply) + await approvePlan(unapproved.planId, actor, true) + const key = randomUUID(), applied = await applyPlan(principal, unapproved.planId, key), outcome = await wait(applied.id) + assert.equal(outcome.state, "applied", JSON.stringify(outcome)); assert.equal(commands.length, beforeApply + 2); assert.deepEqual(catalogs, base) + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [applied.id])).rows[0].result + hidden(receipt); assert.equal(receipt.humanDownloadOnly, true); assert.equal(receipt.ipsecContentReturned, false); assert.equal("publication" in receipt, false); assert.equal("contentB64" in receipt, false) + const stored = (await pool.query("SELECT * FROM mcp_artifacts WHERE id=$1", [receipt.artifactId])).rows[0] + hidden(stored); assert.equal(stored.access_scope.kind, "ipsec-existing-certificate-bundle"); assert(stored.expires_at.getTime() <= Date.now() + 900000) + assert.equal(JSON.parse(Buffer.from(await decryptSecret(stored.ciphertext), "base64").toString()).passphrase, passphrase) + const portalHeaders = headers() + const download = await portal.inject({ url: receipt.downloadPath, headers: portalHeaders }); assert.equal(download.statusCode, 200, `owned portal download status ${download.statusCode}`); assert.equal(download.headers["cache-control"], "private, no-store"); assert.equal(download.json().passphrase, passphrase) + assert.equal((await portal.inject({ url: receipt.downloadPath, headers: headers(randomUUID(), "foreign-owner") })).statusCode, 403) + assert.equal((await portal.inject({ url: receipt.downloadPath, headers: headers(randomUUID(), actor.id, ["mm:network:read"]) })).statusCode, 403) + const listing = await portal.inject({ url: `/api/mcp/operations?connectionId=${connection.connection.id}`, headers: portalHeaders }); assert.equal(listing.statusCode, 200); hidden(listing.json()); assert(listing.json().items.find((item: { id: string }) => item.id === applied.id).artifactReceipts.some((item: { artifactId: string }) => item.artifactId === receipt.artifactId)) + await applyPlan(principal, unapproved.planId, key); assert.equal(commands.length, beforeApply + 2) + await pool.query("UPDATE mcp_artifacts SET expires_at=now()-interval '1 second' WHERE id=$1", [receipt.artifactId]); assert.equal((await portal.inject({ url: receipt.downloadPath, headers: portalHeaders })).statusCode, 404) + cases.push("actual MCP human approval in autonomous connection, exact human-only receipt, encrypted SQL artifact and real owner portal download/listing/TTL") + reset() + const missingCaStart = commands.length + await assert.rejects(invokeMcpTool(principal, "mm_ipsec_existing_certificate_export_plan", { serverId, selector: { kind: "certificate", name: "ca" }, privateKeyExport: true, caPrivateKeyExport: false, passphrase: { kind: "generate" } })) + assert.equal(commands.length, missingCaStart) + for (const [name, role, user] of [["actual-client-cert", "client", "Certificate CN"], ["ipsec-server", "server", "vpn.example.test"], ["foreign-other", "other", "Other CN"], ["ca", "ca", "ca"]] as const) { + reset() + const start = commands.length + const namedPlan = await invokeMcpTool(principal, "mm_ipsec_existing_certificate_export_plan", { serverId, selector: { kind: "certificate", name }, privateKeyExport: true, caPrivateKeyExport: role === "ca", passphrase: { kind: "generate" } }) as { planId: string; state: string } + assert.equal(namedPlan.state, "awaiting-approval"); hidden(namedPlan) + await assert.rejects(applyPlan(principal, namedPlan.planId, randomUUID())); assert.equal(commands.length, start) + await approvePlan(namedPlan.planId, actor, true) + const namedOperation = await applyPlan(principal, namedPlan.planId, randomUUID()) + const namedOutcome = await wait(namedOperation.id) + assert.equal(namedOutcome.state, "applied", JSON.stringify(namedOutcome)); assert.equal(commands.length, start + 2) + assert.equal(commands[start].name, name); assert.deepEqual(catalogs, base) + const namedReceipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [namedOperation.id])).rows[0].result + hidden(namedReceipt); assert.equal(namedReceipt.humanDownloadOnly, true); assert.equal(namedReceipt.ipsecContentReturned, false) + for (const field of ["publication", "contentB64", "passphrase", "sswanContent", "instructions"]) assert.equal(field in namedReceipt, false) + const namedStored = (await pool.query("SELECT * FROM mcp_artifacts WHERE id=$1", [namedReceipt.artifactId])).rows[0] + hidden(namedStored) + const namedAuthority = namedStored.access_scope.authority + assert.equal(namedAuthority.role, role); assert.equal(namedAuthority.certificateName, name); assert.deepEqual(namedAuthority.request.selector, { kind: "certificate", name }) + assert.equal(namedAuthority.request.caPrivateKeyExport, role === "ca"); assert.deepEqual(namedAuthority.request.passphrase, { kind: "generate" }) + const namedDownload = await portal.inject({ url: namedReceipt.downloadPath, headers: portalHeaders }) + assert.equal(namedDownload.statusCode, 200, `owned by-name portal download status ${namedDownload.statusCode}`) + assert.equal(namedDownload.headers["cache-control"], "private, no-store") + const namedBundle = namedDownload.json() + assert.equal(namedBundle.user, user); assert.equal(namedBundle.sswanFilename, `${name}.sswan`); assert.equal(namedBundle.serverEndpoint, "vpn.example.test") + assert.equal(Buffer.from(namedBundle.contentB64, "base64").toString(), fakeP12) + assert.equal(typeof namedBundle.passphrase, "string"); assert(namedBundle.passphrase.length >= 8) + assert.equal(JSON.parse(Buffer.from(await decryptSecret(namedStored.ciphertext), "base64").toString()).passphrase, namedBundle.passphrase) + } + cases.push("actual MCP by-name all four roles with explicit CA intent and owner portal") + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); catalogs["/certificate"][2].name = "changed-bound-target" + const staleStart = commands.length; assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, staleStart) + reset(); const cancelled = new AbortController(); cancelled.abort(); await assert.rejects(invokeMcpTool(principal, "mm_ipsec_existing_certificate_export_plan", request, cancelled.signal)); assert.equal(commands.length, staleStart) + const revoke = await prepare(); await approvePlan(revoke.planId, actor, true); await setConnectionStatus(connection.connection.id, "paused", actor); await assert.rejects(applyPlan(principal, revoke.planId, randomUUID())); assert.equal(commands.length, staleStart) + cases.push("actual stale target, pre-dispatch cancellation and revoked connection refuse without native export") + for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) hidden((await pool.query(table === "mcp_operation_steps" ? "SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1" : `SELECT * FROM ${table} WHERE connection_id=$1`, [connection.connection.id])).rows) + hidden((await pool.query("SELECT payload FROM mcp_audit WHERE actor_id=$1 AND action LIKE 'ipsec.existing.export.rest.%'", [actor.id])).rows) + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) + console.log("Owned existing IPsec export SQL/HTTP:", JSON.stringify({ cases: cases.length, names: cases, migrations: MIGRATION_MANIFEST.length, bothRestPaths: true, roles: 4, actualMcpRuntime: true, actualSqlJournal: true, actualOwnerPortal: true, syntheticLoopbackOnly: true, productionActivated: true, readyAuthorized: false })) +} finally { + for (const id of connections) { await setConnectionStatus(id, "paused", actor).catch(() => {}); await pool.query("DELETE FROM mcp_artifacts WHERE connection_id=$1", [id]) } + await rest.close(); await portal.close() + await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])", [secretIds]) + if (inserted) await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + router.closeAllConnections(); if (router.listening) await new Promise(resolve => router.close(() => resolve())) + await closePool() +} diff --git a/backend/src/modules/mcp/ipsec-existing-certificate-export-production.test.ts b/backend/src/modules/mcp/ipsec-existing-certificate-export-production.test.ts new file mode 100644 index 00000000..41948df8 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-existing-certificate-export-production.test.ts @@ -0,0 +1,53 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION as operation } from "../../services/ipsec-existing-certificate-export-model.js" +import { McpError } from "./security.js" +import { findMcpService } from "./service-registry.js" +import type { ActionContext } from "./action-types.js" + +const originalQuery = pool.query, originalConnect = pool.connect, originalClient = MikrotikClient.fromServer, originalFetch = globalThis.fetch +let sql = 0, native = 0, network = 0 +pool.query = ((statement: string) => { + sql++ + assert.equal(statement.startsWith("/* mcp-principal:coherent-source-v1 */"), true) + throw new McpError("UNAUTHORIZED", "Synthetic credential unavailable", 401) +}) as never +pool.connect = (() => { throw Error("Real SQL transaction forbidden") }) as never +MikrotikClient.fromServer = (() => { native++; throw Error("Native client forbidden") }) as never +globalThis.fetch = (async () => { network++; throw Error("Network forbidden") }) as typeof fetch +try { + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) + assert.equal(findMcpService(operation), undefined) + const { createIpsecExistingCertificateExportProductionAdapter } = await import("./ipsec-existing-certificate-export-production.js") + const first = createIpsecExistingCertificateExportProductionAdapter(), second = createIpsecExistingCertificateExportProductionAdapter() + assert.notEqual(first, second) + assert.equal(sql, 0); assert.equal(native, 0); assert.equal(network, 0) + assert.equal(findMcpService(operation), undefined) + const unavailable = (code: string) => (error: unknown) => error instanceof McpError && error.code === code && !error.message.includes("NEVER_") + // Short ciphertext rejects before durable encryption-key access or SQL. + const input = { serverId: 1, receiptId: randomUUID(), bindingCiphertext: "YQ==" } + const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } + const context = { principal, connection: { owner_id: "synthetic-owner", owner_permissions: ["mm:network:write"], generation: "fixture", policy: { status: "active", mode: "approval", permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: [operation] } } } } as ActionContext + const request = { serverId: 1, selector: { kind: "client", clientId: "identity:*c" }, privateKeyExport: true, passphrase: { kind: "generate" } } + let reconciled = 0 + for (const adapter of [first, second]) { + const prior: number = sql + await assert.rejects(adapter.prepare(request, context), unavailable("UNAUTHORIZED")) + await assert.rejects(adapter.prepare({ ...request, selector: { kind: "certificate", name: "ca" }, caPrivateKeyExport: true }, context), unavailable("UNAUTHORIZED")) + assert.equal(sql, prior + 2) + await assert.rejects(adapter.prepare({ ...request, privateKeyExport: false }, context)) + await assert.rejects(adapter.service.current(input), unavailable("SECRET_UNAVAILABLE")) + await assert.rejects(adapter.service.execute(input, { operationId: randomUUID(), phase: "forward", stepIndex: 0 }), unavailable("SECRET_UNAVAILABLE")) + await assert.rejects(adapter.service.verify!(input, { privateKeyExport: true }), unavailable("SECRET_UNAVAILABLE")) + await assert.rejects(adapter.service.reconcile!(input, async () => { reconciled++ }), unavailable("SECRET_UNAVAILABLE")) + assert.equal(sql, prior + 2) + } + assert.equal(reconciled, 0); assert.equal(native, 0); assert.equal(network, 0) + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true); assert.equal(findMcpService(operation), undefined) + console.log("Activated existing-IPsec production assembly: import/construction inert; missing credentials, malformed intent and forged ciphertext reject before native or material access; only intercepted principal-source SQL") +} finally { + pool.query = originalQuery; pool.connect = originalConnect; MikrotikClient.fromServer = originalClient; globalThis.fetch = originalFetch +} + diff --git a/backend/src/modules/mcp/ipsec-existing-certificate-export-production.ts b/backend/src/modules/mcp/ipsec-existing-certificate-export-production.ts new file mode 100644 index 00000000..28610039 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-existing-certificate-export-production.ts @@ -0,0 +1,46 @@ +import { pool } from "../../db/index.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { exportIpsecExistingCertificateGuarded, readIpsecExistingCertificateExportState, resolveIpsecExistingCertificateExportPassphrase } from "../../services/ipsec-existing-certificate-export.js" +import { createIpsecExistingCertificateExportArtifact, verifyIpsecExistingCertificateExportArtifact } from "../../services/ipsec-existing-certificate-export-artifact.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED } from "../../services/ipsec-existing-certificate-export-model.js" +import { createIpsecExistingCertificateExportAdapter, type IpsecExistingCertificateExportAdapterDependencies } from "./ipsec-existing-certificate-export-actions.js" +import { effectiveMcpConnection } from "./store.js" +import { decryptSecret, encryptSecret, McpError } from "./security.js" + +function activation() { + if (!IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED) throw new McpError("APPROVAL_REQUIRED", "Экспорт существующих сертификатов/CA выключен", 409) +} + +/** Explicit construction only. No action/service registration or I/O on import + * or construction. Every entrypoint gates before decryption, SQL, or native + * reads; the executor and artifact publisher retain their independent gates. */ +export function createIpsecExistingCertificateExportProductionAdapter() { + const dependencies: IpsecExistingCertificateExportAdapterDependencies = { + connection: effectiveMcpConnection, + server: getServerRowById, + sourceProof: ipsecPeerDeleteSourceProof, + read: readIpsecExistingCertificateExportState, + material: resolveIpsecExistingCertificateExportPassphrase, + seal: encryptSecret, + open: decryptSecret, + execute: (authority, options) => exportIpsecExistingCertificateGuarded(authority, async (...args) => { + const receipt = await createIpsecExistingCertificateExportArtifact(...args) + if (receipt.humanDownloadOnly !== true || receipt.ipsecContentReturned !== false) throw new McpError("VERIFICATION_FAILED", "Приватный пакет не подтверждён human-only квитанцией", 502) + return { ...receipt, humanDownloadOnly: true as const, ipsecContentReturned: false as const } + }, options), + verifyArtifact: verifyIpsecExistingCertificateExportArtifact, + query: async (sql, values) => (await pool.query(sql, values)).rows as Record[], + verifiedAccess: assertVerifiedConfigurationAccess, + } + const adapter = createIpsecExistingCertificateExportAdapter(dependencies) + const prepare: typeof adapter.prepare = async (input, context) => { activation(); return adapter.prepare(input, context) } + const service: typeof adapter.service = { + current: async input => { activation(); return adapter.service.current(input) }, + execute: async (input, context) => { activation(); return adapter.service.execute(input, context) }, + verify: async (input, result) => { activation(); return adapter.service.verify!(input, result) }, + reconcile: async (input, authorize) => { activation(); return adapter.service.reconcile!(input, authorize) }, + } + return { prepare, service } +} diff --git a/backend/src/modules/mcp/ipsec-existing-certificate-export-registration.test.ts b/backend/src/modules/mcp/ipsec-existing-certificate-export-registration.test.ts new file mode 100644 index 00000000..b87c96b0 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-existing-certificate-export-registration.test.ts @@ -0,0 +1,50 @@ +import assert from "node:assert/strict" +import type { add } from "./actions.js" +import type { createIpsecExistingCertificateExportAdapter } from "./ipsec-existing-certificate-export-actions.js" +import { findMcpService, registerMcpService } from "./service-registry.js" +import { pool } from "../../db/index.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION as operation } from "../../services/ipsec-existing-certificate-export-model.js" + +const oldQuery = pool.query, oldConnect = pool.connect, oldClient = MikrotikClient.fromServer, oldFetch = globalThis.fetch +let sql = 0, native = 0, network = 0 +pool.query = (() => { sql++; throw Error("Real SQL forbidden") }) as never +pool.connect = (() => { sql++; throw Error("Real SQL forbidden") }) as never +MikrotikClient.fromServer = (() => { native++; throw Error("Real native forbidden") }) as never +globalThis.fetch = (async () => { network++; throw Error("Real network forbidden") }) as typeof fetch +try { + const { installIpsecExistingCertificateExportAdapter, registerIpsecExistingCertificateExportActions } = await import("./ipsec-existing-certificate-export-registration.js") + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) + assert.equal(findMcpService(operation), undefined) + assert.equal(sql, 0); assert.equal(native, 0); assert.equal(network, 0) + const productionCalls: Parameters[] = [] + assert.equal(registerIpsecExistingCertificateExportActions((...args) => { productionCalls.push(args) }), true) + assert.equal(productionCalls.length, 1) + const productionService = findMcpService(operation) + assert(productionService) + assert.equal(typeof productionService.current, "function"); assert.equal(typeof productionService.execute, "function") + assert.equal(typeof productionService.verify, "function"); assert.equal(typeof productionService.reconcile, "function") + assert.equal(productionCalls[0][0], operation); assert.equal(productionCalls[0][1], "mm_ipsec_existing_certificate_export_plan") + // Invalid inputs reach the real factory parser, before actor/SQL/native work. + await assert.rejects(productionCalls[0][8].prepare!({}, {} as never)) + await assert.rejects(productionService.current({})) + assert.equal(sql, 0); assert.equal(native, 0); assert.equal(network, 0) + // The injectable composition seam uses fake callbacks; it does not install a + // duplicate real service into this isolated process's registry. + const calls: Parameters[] = [], services: Parameters[] = [] + const register: typeof add = (...args) => { calls.push(args) } + const unavailable = async () => { throw Error("Synthetic registration must not execute") } + const adapter: ReturnType = { prepare: unavailable, service: { current: unavailable, execute: unavailable } } + installIpsecExistingCertificateExportAdapter(register, (...args) => { services.push(args) }, adapter) + assert.equal(services.length, 1); assert.equal(services[0][0], operation); assert.equal(services[0][1], adapter.service) + assert.equal(calls.length, 1) + const [id, tool, , section, permission, effect, scope, schema, handlers] = calls[0] + assert.equal(id, operation); assert.equal(tool, "mm_ipsec_existing_certificate_export_plan") + assert.equal(section, "network"); assert.equal(permission, "mm:network:write"); assert.equal(effect, "write"); assert.equal(scope, "servers") + assert.equal(handlers.prepare, adapter.prepare); assert.equal(handlers.version, 1) + assert.equal(schema.safeParse({ serverId: 1, selector: { kind: "certificate", name: "client" }, privateKeyExport: true, passphrase: { kind: "generate" } }).success, true) + assert.equal(schema.safeParse({ serverId: 1, selector: { kind: "certificate", name: "client" }, privateKeyExport: true, passphrase: "NEVER_RAW" }).success, false) + assert.equal(findMcpService(operation), productionService) + assert.equal(sql, 0); assert.equal(native, 0); assert.equal(network, 0) + console.log("Activated existing-IPsec registration: import inert; one real typed production service/descriptor installed; real parser rejects invalid inputs; injectable seam preserves exact delegation without duplicate global registration or I/O") +} finally { pool.query = oldQuery; pool.connect = oldConnect; MikrotikClient.fromServer = oldClient; globalThis.fetch = oldFetch } diff --git a/backend/src/modules/mcp/ipsec-existing-certificate-export-registration.ts b/backend/src/modules/mcp/ipsec-existing-certificate-export-registration.ts new file mode 100644 index 00000000..8665ebe6 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-existing-certificate-export-registration.ts @@ -0,0 +1,18 @@ +import type { add } from "./actions.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION } from "../../services/ipsec-existing-certificate-export-model.js" +import { ipsecExistingCertificateExportActionSchema, type createIpsecExistingCertificateExportAdapter } from "./ipsec-existing-certificate-export-actions.js" +import { createIpsecExistingCertificateExportProductionAdapter } from "./ipsec-existing-certificate-export-production.js" +import { registerMcpService } from "./service-registry.js" + +/** Composition seam for a trusted server/test assembly. No default I/O or + * registration runs on import. JSON/tool requests cannot install an adapter. */ +export function installIpsecExistingCertificateExportAdapter(register: typeof add, service: typeof registerMcpService, adapter: ReturnType) { + service(IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION, adapter.service) + register(IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION, "mm_ipsec_existing_certificate_export_plan", "Экспорт существующего сертификата IPsec с явным приватным/CA-допуском и выдачей файла только владельцу", "network", "mm:network:write", "write", "servers", ipsecExistingCertificateExportActionSchema, { version: 1, prepare: adapter.prepare }) +} + +export function registerIpsecExistingCertificateExportActions(register: typeof add): boolean { + if (!IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED) return false + installIpsecExistingCertificateExportAdapter(register, registerMcpService, createIpsecExistingCertificateExportProductionAdapter()) + return true +} diff --git a/backend/src/modules/mcp/ipsec-native-dispatch.test.ts b/backend/src/modules/mcp/ipsec-native-dispatch.test.ts new file mode 100644 index 00000000..b1c82f9a --- /dev/null +++ b/backend/src/modules/mcp/ipsec-native-dispatch.test.ts @@ -0,0 +1,19 @@ +import assert from "node:assert/strict" +import { pool } from "../../db/index.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { executeRosStep } from "./resources.js" +import { McpError } from "./security.js" + +// Retired generic steps may exist in old sealed inverse plans. Refuse them +// before even resolving a device, instead of relying only on a UI/tool alias. +const query = pool.query, fromServer = MikrotikClient.fromServer +let attempts = 0 +pool.query = (() => { attempts++; throw Error("unexpected SQL") }) as typeof pool.query +MikrotikClient.fromServer = (() => { attempts++; throw Error("unexpected native client") }) as typeof MikrotikClient.fromServer +try { + for (const resource of ["certificate", "ipsec_peer"]) for (const action of ["create", "update", "delete"] as const) { + await assert.rejects(executeRosStep({ serverId: 1, resource, action, ...(action === "create" ? {} : { id: "*a" }), fields: {} }), error => error instanceof McpError && error.code === "UNSUPPORTED_CAPABILITY") + } + assert.equal(attempts, 0) +} finally { pool.query = query; MikrotikClient.fromServer = fromServer } +console.log("Retired generic IPsec writes and inverses refuse before SQL/native access; full private service proofs are mandatory") diff --git a/backend/src/modules/mcp/ipsec-native-write-acceptance.ts b/backend/src/modules/mcp/ipsec-native-write-acceptance.ts new file mode 100644 index 00000000..8661aee4 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-native-write-acceptance.ts @@ -0,0 +1,169 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { approvePlan, applyPlan, invokeMcpTool } from "./runtime.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js" + +/** Owned temporary PostgreSQL + loopback HTTP acceptance. The fixture models + * native ACK/defaults and explicit scalar rename projections. It is not CHR + * acceptance or a claim about forwarding or real RouterOS rename behavior. */ +export async function acceptIpsecNativeWrite(actor: AuthUser) { + const serverId = 187, peerPath = "/ip/ipsec/peer", identityPath = "/ip/ipsec/identity", profilePath = "/ip/ipsec/profile", certPath = "/certificate" + const base: Record[]> = Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])) + base[profilePath] = [{ ".id": "*d1", name: "default", default: "true", dynamic: "false", "dh-group": "modp2048", opaque: "NEVER_PROFILE_OPAQUE" }] + base[peerPath] = [{ ".id": "*p1", name: "existing-peer", profile: "default", address: "192.0.2.1", "exchange-mode": "ike2", passive: "false", disabled: "true", certificate: "fixture-ca", opaque: "NEVER_PEER_OPAQUE" }] + base[identityPath] = [{ ".id": "*i1", peer: "existing-peer", certificate: "fixture-ca", "remote-certificate": "*c1", disabled: "true", secret: "NEVER_IDENTITY_SECRET", opaque: "NEVER_IDENTITY_OPAQUE" }] + base["/ip/ipsec/policy"] = [{ ".id": "*s1", peer: "existing-peer", dynamic: "false", template: "false", opaque: "NEVER_POLICY_OPAQUE" }] + base[certPath] = [{ ".id": "*c1", name: "fixture-ca", trusted: "false", "private-key": "NEVER_CERTIFICATE_KEY", opaque: "NEVER_CERT_OPAQUE" }, { ".id": "*c2", name: "fixture-child", ca: "fixture-ca", trusted: "false", opaque: "NEVER_CHILD_OPAQUE" }] + let catalogs = structuredClone(base), mode = "normal", reads = 0, puts = 0, patches = 0, nextId = 0 + let connectionId: string | undefined, afterIntent: (() => void) | undefined + const query = pool.query.bind(pool), commands: Array<{ method: string; path: string; body: Record }> = [] + const reset = () => { catalogs = structuredClone(base); mode = "normal"; afterIntent = undefined } + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")) + res.setHeader("content-type", "application/json") + if (req.method === "GET") { reads++; res.end(JSON.stringify(catalogs[path] ?? [])); return } + if (!(req.method === "PUT" && path === peerPath) && !(req.method === "PATCH" && path === `${certPath}/*c1`)) { res.statusCode = 405; res.end("{}"); return } + let text = ""; for await (const chunk of req) text += chunk + const body = JSON.parse(text) as Record + commands.push({ method: req.method!, path, body }) + let assigned: string | undefined + if (req.method === "PUT") { + puts++; assigned = `*new${++nextId}` + if (mode !== "ignored") catalogs[peerPath]!.push({ ".id": assigned, ...body, profile: body.profile === "*d1" ? "default" : body.profile ?? "default", "exchange-mode": body["exchange-mode"] ?? "main", passive: body.passive ?? "false", disabled: body.disabled ?? "false", "native-extra-default": "observed-only-after-ack" }) + } else { + patches++ + if (mode !== "ignored") { + const oldName = catalogs[certPath]![0]!.name! + Object.assign(catalogs[certPath]![0]!, body) + if (body.name && mode !== "rename-no-refs") { + for (const row of catalogs[certPath]!) if (row.ca === oldName) row.ca = body.name + for (const parent of [identityPath, peerPath]) for (const row of catalogs[parent]!) for (const field of ["certificate", "remote-certificate"]) if (row[field] === oldName) row[field] = body.name + } + } + } + if (mode === "collateral") catalogs[identityPath]![0]!["new-foreign-field"] = "NEVER_FOREIGN_OPAQUE" + if (mode === "lost") { req.socket.destroy(); return } + res.end(JSON.stringify(assigned ? { ".id": assigned } : {})) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const wait = async (id: string) => { + for (const deadline = Date.now() + 140000; Date.now() < deadline;) { + const row = (await query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw Error("Native IPsec remains active; never replay") + } + try { + assert.equal(Number((await query("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0) + await query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-native-write','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]) + const operations = ["ros.ipsec_peer.create", "ros.certificate.update", "operations.reconcile"] + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Native IPsec acceptance", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write", "mm:certificates:write"], scope: { serverIds: [serverId], operationIds: operations, allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = (kind: "peer" | "certificate", fields?: Record) => invokeMcpTool(principal, kind === "peer" ? "mm_ipsec_peer_create_plan" : "mm_certificate_update_plan", { serverId, ...(kind === "certificate" ? { id: "*c1" } : {}), fields: fields ?? (kind === "peer" ? { name: "new-peer", address: "192.0.2.9", disabled: true } : { trusted: true }) }) as Promise<{ planId: string; state: string }> + const apply = async (plan: { planId: string }) => { + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + return { plan, key, operation, ...await wait(operation.id) } + } + const noReplay = async (result: Awaited>, total: number) => { + await applyPlan(principal, result.plan.planId, result.key) + assert.equal(puts + patches, total) + } + const notVerified = async (result: Awaited>) => { + assert.notEqual(result.state, "applied"); assert.notEqual(result.verification, "passed") + assert.equal((await query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1 AND state='verified'", [result.operation.id])).rows[0].n, 0) + const checked = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { results: Array<{ matchesDesiredState: boolean }> } + assert.equal(checked.results[0]!.matchesDesiredState, false) + } + + const unapproved = await prepare("peer") + assert.equal(unapproved.state, "awaiting-approval") + await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(puts + patches, 0) + let result = await apply(unapproved) + assert.equal(result.state, "applied"); assert.equal(puts, 1) + assert.equal(catalogs[peerPath]!.at(-1)!["native-extra-default"], "observed-only-after-ack") + assert.deepEqual(catalogs[identityPath], base[identityPath]); assert.deepEqual(catalogs[certPath], base[certPath]) + await noReplay(result, puts + patches) + const receipt = (await query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [result.operation.id])).rows[0].result + assert.equal(receipt.verified, true); assert.equal(receipt.forwardingVerified, false); assert.ok(!JSON.stringify(receipt).includes("NEVER_")) + + reset(); result = await apply(await prepare("certificate")); assert.equal(result.state, "applied") + assert.deepEqual(commands.at(-1), { method: "PATCH", path: "/certificate/*c1", body: { trusted: "true" } }) + assert.equal(catalogs[certPath]![0]!.opaque, "NEVER_CERT_OPAQUE") + reset(); result = await apply(await prepare("certificate", { name: "renamed-ca", trusted: true })); assert.equal(result.state, "applied") + assert.equal(catalogs[identityPath]![0]!.certificate, "renamed-ca"); assert.equal(catalogs[identityPath]![0]!["remote-certificate"], "*c1") + assert.equal(catalogs[peerPath]![0]!.certificate, "renamed-ca"); assert.equal(catalogs[certPath]![1]!.ca, "renamed-ca") + + for (const kind of ["peer", "certificate"] as const) for (const fault of ["collateral", "lost", "ignored", ...(kind === "certificate" ? ["rename-no-refs"] : [])]) { + reset(); const plan = await prepare(kind, kind === "certificate" ? { name: "renamed-ca", trusted: true } : undefined), before: number = puts + patches + mode = fault; result = await apply(plan) + assert.equal(result.state, fault === "lost" ? "unknown" : "partial", `${kind}/${fault}`) + assert.equal(puts + patches, before + 1); await noReplay(result, before + 1); await notVerified(result) + } + + reset(); const stale = await prepare("peer"), beforeStale = puts + await approvePlan(stale.planId, actor, true); catalogs[profilePath]![0]!["dh-group"] = "modp3072" + assert.equal((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "rejected"); assert.equal(puts, beforeStale) + reset(); const late = await prepare("peer"); await approvePlan(late.planId, actor, true) + pool.query = ((...args: unknown[]) => { + const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "", params = args[1] as unknown[] | undefined + const pending = (query as (...values: unknown[]) => Promise)(...args) + if (afterIntent && sql.startsWith("INSERT INTO mcp_audit") && params?.[1] === "configuration.intent" && String(params?.[3]).includes('"method":"PUT"')) { + const mutate = afterIntent; afterIntent = undefined + return pending.then(value => { mutate(); return value }) + } + return pending + }) as typeof pool.query + afterIntent = () => { catalogs[profilePath]![0]!["dh-group"] = "modp4096" } + try { + const before = puts, stopped = await wait((await applyPlan(principal, late.planId, randomUUID())).id) + assert.notEqual(stopped.state, "applied"); assert.notEqual(stopped.verification, "passed"); assert.equal(afterIntent, undefined); assert.equal(puts, before) + } finally { pool.query = query as typeof pool.query } + + reset() + const certOnly = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Certificates without network access", mode: "approval", expiresAt: new Date(Date.now() + 600000).toISOString(), permissions: ["mm:certificates:write"], scope: { serverIds: [serverId], operationIds: ["ros.certificate.update"], allowGlobal: false } }), actor) + try { + const before = reads + await assert.rejects(invokeMcpTool(await authenticateMcpToken(certOnly.token), "mm_certificate_update_plan", { serverId, id: "*c1", fields: { trusted: true } })) + assert.equal(reads, before, "missing network permission rejects before native GET") + } finally { await setConnectionStatus(certOnly.connection.id, "paused", actor) } + for (const [kind, id, operationKind] of [["ipsec_profile", "*d1", "peer"], ["ipsec_identity", "*i1", "peer"], ["ipsec_identity", "*i1", "certificate"]] as const) { + reset(); const protection = randomUUID(), before: number = puts + patches + await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind, id, serverId, protection: "deny" }), actor.id]) + try { await assert.rejects(prepare(operationKind)); assert.equal(puts + patches, before) } + finally { await query("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + } + for (const kind of ["peer", "certificate"] as const) { + reset(); const sourceChanged = await prepare(kind) + await approvePlan(sourceChanged.planId, actor, true) + const beforeReads: number = reads, beforeWrites: number = puts + patches + await query("UPDATE servers SET password='CHANGED_ENDPOINT_SECRET' WHERE id=$1", [serverId]) + try { + const stopped = await wait((await applyPlan(principal, sourceChanged.planId, randomUUID())).id) + assert.equal(stopped.state, "rejected"); assert.equal(reads, beforeReads, "endpoint proof must reject before the first GET") + assert.equal(puts + patches, beforeWrites) + } finally { await query("UPDATE servers SET password='NEVER_ENDPOINT' WHERE id=$1", [serverId]) } + } + for (const kind of ["peer", "certificate"] as const) { + reset(); const old = await prepare(kind), before: number = puts + patches + await query("UPDATE mcp_plans SET operation_version=operation_version-1 WHERE id=$1", [old.planId]) + await approvePlan(old.planId, actor, true); await assert.rejects(applyPlan(principal, old.planId, randomUUID()), error => (error as {code?:string}).code === "PLAN_EXPIRED"); assert.equal(puts + patches, before) + } + assert.ok(!JSON.stringify((await query("SELECT payload FROM mcp_audit WHERE connection_id=$1", [connectionId])).rows).includes("NEVER_")) + console.log("Native IPsec SQL/loopback: peer ACK defaults, exact opaque conservation, certificate trust/linked rename, denied parent/identity, permission-before-GET, stale/late parent zero PUT, collateral/lost/no replay and old revisions fail closed; no CHR or forwarding claim") + } finally { + pool.query = query as typeof pool.query + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await query("DELETE FROM mcp_protections WHERE object_ref->>'serverId'=$1", [String(serverId)]) + await query("DELETE FROM servers WHERE id=$1", [serverId]) + router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-native-write-actions.ts b/backend/src/modules/mcp/ipsec-native-write-actions.ts new file mode 100644 index 00000000..34cfb901 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-native-write-actions.ts @@ -0,0 +1,120 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { readIpsecPeerDeleteState, ipsecPeerDeleteProof, ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { planIpsecCertificateUpdate } from "../../services/ipsec-certificate-update-model.js" +import { networkSecretValues, maskNetworkSecrets } from "../../services/network-observed-read.js" +import { planIpsecPeerCreate, verifyIpsecPeerCreate } from "./ipsec-peer-create-dependencies.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient, resourceById, validateRosStep } from "./resources.js" +import { encryptSecret, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" + +const operations = ["ros.ipsec_peer.create", "ros.certificate.update"] as const +const proof = z.string().regex(/^[a-f0-9]{64}$/) +const bindingSchema = z.object({ + operation: z.enum(operations), serverId: z.number().int().positive().safe(), id: z.string().regex(/^\*[a-z0-9]+$/i).max(100).optional(), + fields: z.record(z.string(), z.union([z.string(), z.boolean()])), + principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: proof, policyProof: proof, generation: z.string().min(1), + expectedProof: proof, sourceProof: proof, receiptId: z.string().uuid(), +}).strict() +type Binding = z.infer +const inputSchema = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(262144) }).strict() +const receiptSchema = z.object({ ok: z.literal(true), receiptId: z.string().uuid(), nativeId: z.string().regex(/^\*[a-z0-9]+$/i).max(100), afterProof: proof, commandCount: z.literal(1), verified: z.literal(true), forwardingVerified: z.literal(false) }).strict() +const conflict = () => new McpError("STATE_CONFLICT", "IPsec, зависимости или допуск изменились; требуется новый план", 409) +const production = { connection: effectiveMcpConnection, router: routerClient, read: readIpsecPeerDeleteState, proof: ipsecPeerDeleteProof, seal: encryptSecret, open: decryptSecret, verifiedAccess: assertVerifiedConfigurationAccess } + +/** Full private IPsec state replaces the lossy generic native projection for + * these two entry points. No configuration history or collector is started. */ +export function createIpsecNativeWriteAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const request = async (input: Record) => { + const value = inputSchema.parse(input), binding = bindingSchema.parse(JSON.parse(await deps.open(value.bindingCiphertext))) + if (value.receiptId !== binding.receiptId || value.serverId !== binding.serverId) throw conflict() + return binding + } + const authorize = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now()) throw new McpError("UNAUTHORIZED", "Срок допуска IPsec истёк", 401) + const current = await deps.connection(binding.principal) + const permissions = binding.operation === "ros.certificate.update" ? ["mm:network:write", "mm:certificates:write"] : ["mm:network:write"] + if (current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || current.owner_valid_until.getTime() <= Date.now() || Date.parse(current.policy.expiresAt) <= Date.now() || !current.policy.scope.operationIds.includes(binding.operation) || !current.policy.scope.serverIds.includes(binding.serverId) || permissions.some(permission => !hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission))) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска native IPsec", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof) throw conflict() + signal?.throwIfAborted() + } + const source = async (binding: Binding, signal?: AbortSignal, external?: () => Promise) => { + const check = () => authorize(binding, signal, external) + await check() + const { server, client } = await deps.router(binding.serverId) + if (await ipsecPeerDeleteSourceProof(server) !== binding.sourceProof) throw conflict() + const state = await deps.read(server, check) + if (binding.sourceProof !== state.source) throw conflict() + await check(); return { server, client, state, check } + } + const prepare = async (operation: Binding["operation"], serverId: number, id: string | undefined, fields: unknown, context: ActionContext): Promise => { + const resource = resourceById(operation === "ros.ipsec_peer.create" ? "ipsec_peer" : "certificate") + const parsed = resource.fields.parse(fields) as Record + validateRosStep(resource, operation === "ros.ipsec_peer.create" ? "create" : "update", parsed, id) + const binding: Binding = bindingSchema.parse({ operation, serverId, ...(id === undefined ? {} : { id }), fields: parsed, principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, expectedProof: "0".repeat(64), sourceProof: "0".repeat(64), receiptId: randomUUID() }) + await authorize(binding, context.signal) + const { server } = await deps.router(serverId), state = await deps.read(server, () => authorize(binding, context.signal)) + binding.sourceProof = state.source; binding.expectedProof = state.proof + const secrets = networkSecretValues(Object.values(state.catalogs).flat()) + if (fingerprint(maskNetworkSecrets(parsed, secrets)) !== fingerprint(parsed)) throw new McpError("STATE_UNAVAILABLE", "Поля плана содержат закрытые данные", 409) + const plan = operation === "ros.ipsec_peer.create" ? planIpsecPeerCreate(state, validateRosStep(resource, "create", parsed)) : planIpsecCertificateUpdate(state, id!, parsed) + const closure = "objects" in plan ? plan.objects : plan.companions + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(serverId), serverId }, { kind: resource.id, id: id ?? String(parsed.name), serverId }, ...closure.map(ref => ({ ...ref, serverId }))] + for (const protection of state.protections as Array<{ object_ref: { kind: string; id: string } }>) objects.push({ kind: protection.object_ref.kind, id: protection.object_ref.id, serverId }) + if ((await source(binding, context.signal)).state.proof !== binding.expectedProof) throw conflict() + return { target: { scope: "servers", serverIds: [serverId] }, objects, snapshots: [], before: { proof: state.proof }, requiresHuman: true, mutationCost: 1, + steps: [{ kind: "service", service: operation, input: { serverId, receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(binding)) } }], + warnings: ["Одна native-команда с полным private proof IPsec, источника, защит и зависимостей. После ACK требуется точный read-back всей конфигурации. История managed IPsec этим native-входом не создаётся.", "Потеря ответа, изменение стороннего поля или неподтверждённое автоматическое обновление ссылок останавливают операцию без повтора. Модель не получает закрытые каталоги. Сетевая передача и автоматический rename ссылок на настоящем CHR отдельно не подтверждены."] } + } + const service = (operation: Binding["operation"]) => ({ + current: async (input: Record) => { const binding = await request(input); if (binding.operation !== operation) throw conflict(); return { proof: (await source(binding)).state.proof } }, + execute: async (input: Record) => { + const binding = await request(input); if (binding.operation !== operation) throw conflict() + return withApplicationMutationLock(() => withConfigMutationLock([binding.serverId], () => executeGuardedApplicationWrite({ kind: operation === "ros.ipsec_peer.create" ? "ipsec_peer" : "certificate", id: binding.id ?? String(binding.fields.name) }, [binding.serverId], async () => { + await deps.verifiedAccess() + const before = await source(binding) + if (before.state.proof !== binding.expectedProof) throw conflict() + const plan = operation === "ros.ipsec_peer.create" ? planIpsecPeerCreate(before.state, validateRosStep(resourceById("ipsec_peer"), "create", binding.fields)) : planIpsecCertificateUpdate(before.state, binding.id!, binding.fields) + let dispatched = false, result: unknown + const fence = async () => { await deps.verifiedAccess(); if ((await source(binding)).state.proof !== binding.expectedProof) throw conflict(); dispatched = true } + try { result = operation === "ros.ipsec_peer.create" ? await before.client.put("/ip/ipsec/peer", plan.body, 15000, fence) : await before.client.patch("/certificate/" + encodeURIComponent(binding.id!), plan.body, 15000, fence) } + catch (error) { if (!dispatched) throw error; throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Ответ native IPsec не подтверждён; повтор запрещён", 503) } + let after: Awaited> + try { after = await source(binding) } catch { throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Native IPsec отправлен, read-back недоступен; повтор запрещён", 503) } + const nativeId = operation === "ros.ipsec_peer.create" ? (result as Record | null)?.[".id"] : binding.id + const matches = operation === "ros.ipsec_peer.create" ? await verifyIpsecPeerCreate(before.state, after.state, plan.body, nativeId, deps.proof) : await deps.proof((plan as ReturnType).projected) === after.state.proof + if (!matches) throw new McpError("VERIFICATION_FAILED", "Native IPsec или сохранность зависимостей не подтверждены; повтор запрещён", 409) + return receiptSchema.parse({ ok: true, receiptId: binding.receiptId, nativeId, afterProof: after.state.proof, commandCount: 1, verified: true, forwardingVerified: false }) + }))) + }, + verify: async (input: Record, result: unknown) => { + const binding = await request(input), receipt = receiptSchema.safeParse(result) + if (binding.operation !== operation || !receipt.success || receipt.data.receiptId !== binding.receiptId || (operation === "ros.certificate.update" && receipt.data.nativeId !== binding.id)) return false + return (await source(binding)).state.proof === receipt.data.afterProof + }, + reconcile: async (input: Record, external: () => Promise) => { + const binding = await request(input); if (binding.operation !== operation) throw conflict() + const state = (await source(binding, undefined, external)).state + const rows = (await pool.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId])).rows + const row = rows[0], result = receiptSchema.safeParse(row?.result) + const verified = rows.length === 1 && row.state === "verified" && row.connection_id === binding.principal.connectionId && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && result.success && result.data.receiptId === binding.receiptId && result.data.afterProof === state.proof && (operation !== "ros.certificate.update" || result.data.nativeId === binding.id) + if ((await source(binding, undefined, external)).state.proof !== state.proof) throw conflict() + return { matchesDesiredState: verified, matchesOriginalState: state.proof === binding.expectedProof, automaticReplay: false, forwardingVerified: false } + }, + }) + return { prepare, service } +} + +const adapter = createIpsecNativeWriteAdapter() +for (const operation of operations) registerMcpService(operation, adapter.service(operation)) +export const prepareIpsecNativeWrite = adapter.prepare diff --git a/backend/src/modules/mcp/ipsec-peer-create-dependencies.test.ts b/backend/src/modules/mcp/ipsec-peer-create-dependencies.test.ts new file mode 100644 index 00000000..f6894b3f --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-create-dependencies.test.ts @@ -0,0 +1,83 @@ +import assert from "node:assert/strict" +import type { IpsecPeerDeleteState } from "../../services/ipsec-peer-delete.js" +import { planIpsecPeerCreate, projectIpsecPeerCreate, verifyIpsecPeerCreate } from "./ipsec-peer-create-dependencies.js" +import { fingerprint, McpError } from "./security.js" + +const profile = "/ip/ipsec/profile", peer = "/ip/ipsec/peer", identity = "/ip/ipsec/identity", policy = "/ip/ipsec/policy" +const state = (): IpsecPeerDeleteState => ({ source: "private-source", proof: "private-proof", protections: [{ object_ref: { kind: "ipsec_profile", id: "*D" } }], catalogs: { + [profile]: [{ ".id": "*D", name: "default", default: "true", "opaque-setting": "preserved" }, { ".id": "*C", name: "custom" }], + [peer]: [{ ".id": "*A", name: "existing", profile: "default", "opaque-setting": "preserved" }, { ".id": "*B", name: "other", profile: "*C" }], + [identity]: [{ ".id": "*I", peer: "existing", secret: "private-secret" }], + [policy]: [{ ".id": "*P", peer: "other, existing", "opaque-setting": "preserved" }, { ".id": "*T", peer: "", template: "true" }], + "/certificate": [{ ".id": "*Q", "private-key": "opaque" }], +} }) +const fields = { name: "new-peer", address: "192.0.2.1", comment: "" } +const conflict = (error: unknown) => error instanceof McpError && error.code === "STATE_CONFLICT" +const invalid = (error: unknown) => error instanceof McpError && error.code === "INVALID_INPUT" +const initial = state(), saved = structuredClone(initial) +assert.deepEqual(planIpsecPeerCreate(initial, fields), { + body: { ...fields, profile: "default" }, profileId: "*D", commandCount: 1, + objects: [{ kind: "ipsec_profile", id: "*D" }, { kind: "ipsec_peer", id: "*A" }, { kind: "ipsec_identity", id: "*I" }, { kind: "ipsec_policy", id: "*P" }], +}) +assert.deepEqual(initial, saved, "preflight never mutates private state") +assert.equal(planIpsecPeerCreate(initial, { ...fields, profile: "*C" }).profileId, "*C") +assert.deepEqual(planIpsecPeerCreate(initial, { ...fields, profile: "custom" }).objects, [{ kind: "ipsec_profile", id: "*C" }, { kind: "ipsec_peer", id: "*B" }, { kind: "ipsec_policy", id: "*P" }]) + +for (const change of [ + (s: IpsecPeerDeleteState) => { s.catalogs[profile] = [] }, + (s: IpsecPeerDeleteState) => { s.catalogs[profile]!.push({ ".id": "*E", name: "default" }) }, + (s: IpsecPeerDeleteState) => { s.catalogs[profile]![0]!.dynamic = "true" }, + (s: IpsecPeerDeleteState) => { s.catalogs[profile]![0]!.disabled = "unknown"; s.catalogs[profile]![0]!.dynamic = "unknown" }, + (s: IpsecPeerDeleteState) => { s.catalogs[profile]![0]![".id"] = "bad" }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]![1]!.name = fields.name }, + (s: IpsecPeerDeleteState) => { delete s.catalogs[peer]![0]!.profile }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]![1]!.name = "*A"; s.catalogs[identity]![0]!.peer = "*A" }, + (s: IpsecPeerDeleteState) => { s.catalogs[identity]![0]!.peer = fields.name }, + (s: IpsecPeerDeleteState) => { s.catalogs[identity]![0]!.peer = "orphan" }, + (s: IpsecPeerDeleteState) => { delete s.catalogs[identity]![0]!.peer }, + (s: IpsecPeerDeleteState) => { s.catalogs[policy]![0]!.peer = `other, ${fields.name}` }, + (s: IpsecPeerDeleteState) => { s.catalogs[policy]![0]!.peer = "existing," }, + (s: IpsecPeerDeleteState) => { s.catalogs[policy]![0]!.dynamic = "unknown" }, + (s: IpsecPeerDeleteState) => { s.catalogs[identity]!.push({ ...s.catalogs[identity]![0]! }) }, + (s: IpsecPeerDeleteState) => { delete s.catalogs[policy] }, +]) { + const candidate = state(); change(candidate) + assert.throws(() => planIpsecPeerCreate(candidate, fields), conflict) +} +for (const change of [{ profile: "" }, { profile: " default " }, { profile: "missing" }]) assert.throws(() => planIpsecPeerCreate(state(), { ...fields, ...change }), conflict) +for (const change of [{ name: "*F" }, { name: " spaced " }, { address: "" }, { certificate: "*C" }, { passive: "unknown" }, { "exchange-mode": "unknown" }]) assert.throws(() => planIpsecPeerCreate(state(), { ...fields, ...change }), invalid) +const dynamic = state() +dynamic.catalogs[policy]!.push({ ".id": "*R", dynamic: "true", peer: "transient-live-peer", "ph2-state": "established" }) +assert.equal(planIpsecPeerCreate(dynamic, fields).objects.some(ref => ref.id === "*R"), false, "runtime-derived policy is not a configured dependency") +const projected = projectIpsecPeerCreate(initial, fields, "*NEW") +assert.deepEqual(projected.catalogs[peer]!.at(-1), { ...fields, profile: "default", ".id": "*NEW" }) +projected.catalogs[peer]!.pop() +assert.deepEqual(projected, initial, "projection conserves all unrelated raw fields, secrets, protections and source") +assert.deepEqual(initial, saved) +for (const assigned of [undefined, "", "bad", "*A", { ".id": "*NEW" }]) assert.throws(() => projectIpsecPeerCreate(initial, fields, assigned), conflict) +const pureProof = async ({ source, catalogs, protections }: IpsecPeerDeleteState) => fingerprint({ source, catalogs, protections }) +const actual = projectIpsecPeerCreate(initial, fields, "*NEW") +Object.assign(actual.catalogs[peer]!.at(-1)!, { "opaque-native-default": "preserved", passive: "no", disabled: "no" }) +const actualSaved = structuredClone(actual) +assert.equal(await verifyIpsecPeerCreate(initial, actual, fields, "*NEW", pureProof), true) +assert.equal(await verifyIpsecPeerCreate(initial, actual, { ...fields, profile: "*D", disabled: "false" }, "*NEW", pureProof), true) +assert.deepEqual(actual, actualSaved, "verification never mutates read-back") +for (const change of [ + (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.name = "different" }, + (s: IpsecPeerDeleteState) => { delete s.catalogs[peer]!.at(-1)!.profile }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.profile = "*D" }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.dynamic = "true" }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.builtin = "unknown" }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.default = "true" }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]![0]!["opaque-setting"] = "changed" }, + (s: IpsecPeerDeleteState) => { s.catalogs[identity]![0]!.secret = "changed" }, + (s: IpsecPeerDeleteState) => { s.catalogs["/certificate"]![0]!["private-key"] = "changed" }, + (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.push({ ".id": "*UNEXPECTED", name: "foreign" }) }, + (s: IpsecPeerDeleteState) => { s.source = "changed" }, + (s: IpsecPeerDeleteState) => { s.protections = [] }, +]) { + const candidate = structuredClone(actual); change(candidate) + assert.equal(await verifyIpsecPeerCreate(initial, candidate, fields, "*NEW", pureProof), false) +} +for (const assigned of [undefined, "bad", "*A", "*MISSING"]) assert.equal(await verifyIpsecPeerCreate(initial, actual, fields, assigned, pureProof), false) +console.log("IPsec peer CREATE dependencies: exact/default profile, closure, late-bound references, native identity and private-state conservation passed") diff --git a/backend/src/modules/mcp/ipsec-peer-create-dependencies.ts b/backend/src/modules/mcp/ipsec-peer-create-dependencies.ts new file mode 100644 index 00000000..cffd17fc --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-create-dependencies.ts @@ -0,0 +1,106 @@ +import type { IpsecPeerDeleteState } from "../../services/ipsec-peer-delete.js" +import { observedBoolean } from "../../services/ospf-observed-values.js" +import { McpError } from "./security.js" + +type Row = Record +type Ref = { kind: string; id: string } +const paths = { profile: "/ip/ipsec/profile", peer: "/ip/ipsec/peer", identity: "/ip/ipsec/identity", policy: "/ip/ipsec/policy" } as const +const fail = (message: string) => new McpError("STATE_CONFLICT", message, 409) +const nativeId = (value: unknown): value is string => typeof value === "string" && /^\*[a-z0-9]+$/i.test(value) && value.length <= 100 + +function catalogue(state: IpsecPeerDeleteState, path: string): Row[] { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row) || !nativeId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(rows.map(row => row[".id"])).size !== rows.length) throw fail("Неполный native-каталог зависимостей IPsec") + return rows +} +function resolve(rows: Row[], reference: unknown): Row { + if (typeof reference !== "string" || !reference || reference.trim() !== reference) throw fail("Не подтверждена ссылка IPsec") + const matches = rows.filter(row => row.name === reference || row[".id"] === reference) + if (matches.length !== 1) throw fail("Отсутствующая или неоднозначная ссылка IPsec") + return matches[0]! +} +function stable(row: Row) { + for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) === null) throw fail("Не подтверждён native-флаг IPsec") + if (row.dynamic !== undefined && observedBoolean(row.dynamic) !== false) throw fail("Динамическая зависимость IPsec не подтверждена") +} + +/** Pure native CREATE preflight. The caller seals the full private state/proof, + * including opaque fields, protections and source, and checks it before PUT. + * No history, SQL, native access or inferred runtime state is introduced here. */ +export function planIpsecPeerCreate(state: IpsecPeerDeleteState, fields: Record) { + const allowed = ["name", "address", "exchange-mode", "profile", "passive", "disabled", "comment"] + if (!fields || Object.entries(fields).some(([key, value]) => !allowed.includes(key) || typeof value !== "string") || !fields.name || fields.name.trim() !== fields.name || nativeId(fields.name) || !fields.address) throw new McpError("INVALID_INPUT", "Некорректные native поля создания IPsec peer", 400) + for (const flag of ["passive", "disabled"]) if (fields[flag] !== undefined && observedBoolean(fields[flag]) === null) throw new McpError("INVALID_INPUT", "Некорректный boolean IPsec peer", 400) + if (fields["exchange-mode"] !== undefined && !["ike2", "main", "aggressive"].includes(fields["exchange-mode"])) throw new McpError("INVALID_INPUT", "Некорректный exchange-mode IPsec peer", 400) + const profiles = catalogue(state, paths.profile), peers = catalogue(state, paths.peer), identities = catalogue(state, paths.identity), policies = catalogue(state, paths.policy) + const parent = resolve(profiles, fields.profile ?? "default") + stable(parent) + if (resolve(profiles, parent.name)[".id"] !== parent[".id"]) throw fail("Не подтверждено каноническое имя profile IPsec") + if (peers.some(row => row.name === fields.name || row[".id"] === fields.name)) throw fail("Имя создаваемого IPsec peer уже занято") + const objects: Ref[] = [{ kind: "ipsec_profile", id: parent[".id"]! }] + const relatedPeers = new Set() + for (const peer of peers) { + stable(peer) + if (resolve(peers, peer.name)[".id"] !== peer[".id"]) throw fail("Не подтверждено каноническое имя peer IPsec") + const profile = resolve(profiles, peer.profile) + if (profile[".id"] === parent[".id"]) { + relatedPeers.add(peer[".id"]!) + objects.push({ kind: "ipsec_peer", id: peer[".id"]! }) + } + } + // Validate every configured reference, even outside the parent closure: an + // orphan name/ID can otherwise become bound by CREATE's assigned identity. + for (const [kind, rows] of [["ipsec_identity", identities], ["ipsec_policy", policies]] as const) for (const row of rows) { + if (kind === "ipsec_policy" && observedBoolean(row.dynamic) === true) continue + stable(row) + if (typeof row.peer !== "string") throw fail("Не подтверждена ссылка peer в зависимости IPsec") + if (kind === "ipsec_policy" && row.peer === "") continue + const references = kind === "ipsec_policy" ? row.peer.split(",").map(value => value.trim()) : [row.peer] + if (references.some(reference => reference === fields.name)) throw fail("Создание IPsec peer захватит существующую ссылку") + const targets = references.map(reference => resolve(peers, reference)) + if (targets.some(peer => relatedPeers.has(peer[".id"]!))) objects.push({ kind, id: row[".id"]! }) + } + // Make RouterOS's omitted profile default explicit and pin its exact ID via + // the whole-state proof. Preserve all other caller values verbatim. + return { body: { ...fields, profile: fields.profile ?? "default" }, objects, profileId: parent[".id"]!, commandCount: 1 as const } +} + +/** Project only after an authoritative native CREATE acknowledgement. */ +export function projectIpsecPeerCreate(state: IpsecPeerDeleteState, fields: Record, assignedId: unknown): IpsecPeerDeleteState { + const plan = planIpsecPeerCreate(state, fields) + if (!nativeId(assignedId) || state.catalogs[paths.peer]!.some(row => row[".id"] === assignedId)) throw fail("Не подтверждён новый native ID созданного IPsec peer") + const projected = structuredClone(state) + projected.catalogs[paths.peer]!.push({ ...plan.body, ".id": assignedId }) + return projected +} + +/** Verify conservation against the actual ACK row, whose native defaults are + * unknown before CREATE. Only that new row may acquire additional fields. + * Injecting proof keeps tests entirely pure; production uses the private proof. */ +export async function verifyIpsecPeerCreate( + before: IpsecPeerDeleteState, + after: IpsecPeerDeleteState, + fields: Record, + assignedId: unknown, + proof: (state: IpsecPeerDeleteState) => Promise = async state => (await import("../../services/ipsec-peer-delete.js")).ipsecPeerDeleteProof(state), +): Promise { + const plan = planIpsecPeerCreate(before, fields) + if (!nativeId(assignedId) || catalogue(before, paths.peer).some(row => row[".id"] === assignedId)) return false + const rows = catalogue(after, paths.peer), added = rows.filter(row => row[".id"] === assignedId) + if (added.length !== 1) return false + const row = added[0]! + for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) return false + const parent = catalogue(before, paths.profile).find(row => row[".id"] === plan.profileId)! + // Native profile read-back uses its canonical name, even when the caller + // supplied a native ID. No fallback for a missing profile observation. + if (!parent.name || row.profile !== parent.name) return false + for (const [key, value] of Object.entries(plan.body)) { + if (key === "profile") continue + if (key === "passive" || key === "disabled") { + if (observedBoolean(row[key]) !== observedBoolean(value)) return false + } else if (row[key] !== value) return false + } + const conserved = structuredClone(after) + conserved.catalogs[paths.peer] = conserved.catalogs[paths.peer]!.filter(row => row[".id"] !== assignedId) + return await proof(conserved) === await proof(before) +} diff --git a/backend/src/modules/mcp/ipsec-peer-delete-acceptance.ts b/backend/src/modules/mcp/ipsec-peer-delete-acceptance.ts new file mode 100644 index 00000000..86779a39 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-delete-acceptance.ts @@ -0,0 +1,125 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import ipsecRoutes, { createIpsecRoutes } from "../../routes/ipsec.js" +import { getEnabledIpsecServerById } from "../../services/ipsec-live.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Opt-in SQL + loopback HTTP acceptance. Never invokes a real RouterOS or external endpoint. */ +export async function acceptIpsecPeerDeletion(actor: AuthUser) { + const serverId = 193, baseline: Record[]> = Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])) + baseline["/ip/ipsec/peer"] = [{ ".id": "*a1", name: "ipsec-proof", disabled: "true", comment: "MikrotikManager:ipsec", opaque: "NEVER_IPSEC_OPAQUE" }, { ".id": "*a2", name: "ipsec-preserved", disabled: "true" }] + baseline["/ip/ipsec/identity"] = [{ ".id": "*b1", peer: "ipsec-proof", disabled: "true", secret: "NEVER_IPSEC_PSK", "auth-method": "pre-shared-key" }] + let catalogs = structuredClone(baseline), mode = "normal", deletes = 0, reads = 0, connectionId: string | undefined + let duringRead: (() => Promise) | undefined + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json") + const path = (req.url ?? "").replace(/^\/rest/, "") + if (req.method === "GET") { reads++; if (duringRead) { const hook = duringRead; duringRead = undefined; void hook().then(() => res.end(JSON.stringify(catalogs[path] ?? [])), () => { res.statusCode = 503; res.end("{}") }); return }; if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_IPSEC_PROVIDER"}'); return }; res.end(JSON.stringify(catalogs[path] ?? [])); return } + if (req.method === "DELETE" && path === "/ip/ipsec/peer/*a1") { + deletes++ + if (mode !== "ignored") catalogs["/ip/ipsec/peer"] = catalogs["/ip/ipsec/peer"].filter(row => row[".id"] !== "*a1") + if (mode === "collateral") catalogs["/ip/ipsec/identity"][0].secret = "changed" + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}"); return + } + res.statusCode = 400; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = () => { mode = "normal"; catalogs = structuredClone(baseline) } + const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; if (!["pending", "running"].includes(state)) return state; await delay(25) }; throw Error("IPsec operation remains live; never replay") } + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-peer-proof','127.0.0.1',$2,false,'NEVER_IPSEC_ENDPOINT_PASSWORD')", [serverId, (router.address() as { port: number }).port]) + await rest.register(ipsecRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec peer deletion", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.peers.delete", "ros.ipsec_peer.delete", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = async (force = true) => { + const plan = await invokeMcpTool(principal, "mm_ipsec_peer_remove_plan", { serverId, peerId: "*a1", force }) as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval"); assert.ok(!JSON.stringify(plan).includes("NEVER_")); return plan + } + const count = reads + await assert.rejects(invokeMcpTool(principal, "mm_ipsec_peer_remove_plan", { serverId: 1, peerId: "*a1" })) + await assert.rejects(invokeMcpTool(principal, "mm_ipsec_peer_remove_plan", { serverId, peerId: "ipsec-proof" })) + assert.equal(reads, count) + await assert.rejects(prepare(false), /identity/) + const aliasOnly = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Native peer alias without business grant", mode: "approval", expiresAt: new Date(Date.now()+1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [serverId], operationIds: ["ros.ipsec_peer.delete"], allowGlobal: false } }),actor) + try { + const beforeDenied=reads + await assert.rejects(invokeMcpTool(await authenticateMcpToken(aliasOnly.token),"mm_ipsec_peer_delete_plan",{serverId,id:"*a1"})) + assert.equal(reads,beforeDenied) + } finally { await setConnectionStatus(aliasOnly.connection.id,"paused",actor) } + await assert.rejects(invokeMcpTool(principal,"mm_ipsec_peer_delete_plan",{serverId,id:"*a1"}),/identity/) + catalogs["/ip/ipsec/identity"]=[] + const native = await invokeMcpTool(principal,"mm_ipsec_peer_delete_plan",{serverId,id:"*a1"}) as {planId:string} + await approvePlan(native.planId,actor,true) + const nativeBefore=deletes + assert.equal(await wait((await applyPlan(principal,native.planId,randomUUID())).id),"applied") + assert.equal(deletes,nativeBefore+1) + reset() + const blockedBefore=deletes + const blocked = await rest.inject({ method: "DELETE", url: `/api/ipsec/peers/${serverId}/*a1` }); assert.equal(blocked.statusCode, 409); assert.equal(deletes, blockedBefore) + for (const fault of ["normal", "ignored", "collateral", "lost"]) { + reset(); const plan = await prepare(); await approvePlan(plan.planId, actor, true); mode = fault + const before: number = deletes, key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await wait(operation.id), fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial") + assert.equal(deletes, before + 1) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + const reconciled = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; configurationMatchesDesiredState: boolean; matchesOriginalState: boolean }[] } + assert.equal(reconciled.automaticRetry, false); assert.equal(reconciled.results[0].matchesDesiredState, fault === "normal"); assert.equal(reconciled.results[0].configurationMatchesDesiredState, ["normal", "lost"].includes(fault)); assert.equal(reconciled.results[0].matchesOriginalState, fault === "ignored"); assert.equal(deletes, before + 1) + if (fault === "normal") { + assert.deepEqual(catalogs["/ip/ipsec/identity"], baseline["/ip/ipsec/identity"]) + const saved = (await pool.query("SELECT step_index,intended_state,result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0] + for (const tamper of ["history", "intended", "native", "command-count"]) { + const wrongInput = structuredClone(saved.intended_state), wrongResult = structuredClone(saved.result) + if (tamper === "history") wrongResult.historyRecorded = false + if (tamper === "intended") wrongInput.input.force = false + if (tamper === "native") wrongResult.peerId = "*a2" + if (tamper === "command-count") wrongResult.completedCommands = 0 + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb,result=$3::jsonb WHERE operation_id=$1 AND step_index=$4", [operation.id, JSON.stringify(wrongInput), JSON.stringify(wrongResult), saved.step_index]) + const checked = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as typeof reconciled + assert.equal(checked.results[0].configurationMatchesDesiredState, true); assert.equal(checked.results[0].matchesDesiredState, false); assert.equal(deletes, before + 1) + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb,result=$3::jsonb WHERE operation_id=$1 AND step_index=$4", [operation.id, JSON.stringify(saved.intended_state), JSON.stringify(saved.result), saved.step_index]) + } + } + if (fault === "lost") { + const priorReads: number = reads, priorDeletes: number = deletes + duringRead = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id })) + assert.equal(reads, priorReads + 1); assert.equal(deletes, priorDeletes) + await setConnectionStatus(access.connection.id, "active", actor) + } + } + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); catalogs["/ip/ipsec/identity"][0].secret = "NEVER_IPSEC_DRIFT" + const beforeStale = deletes; assert.equal(await wait((await applyPlan(principal, stale.planId, randomUUID())).id), "rejected"); assert.equal(deletes, beforeStale) + reset(); const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protection, JSON.stringify({ kind: "ipsec_peer", id: "*a1", serverId, protection: "deny" })]) + await assert.rejects(prepare()); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/peers/${serverId}/*a1?force=true` })).statusCode, 409) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + reset(); mode = "provider-error"; await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + reset(); const success = await rest.inject({ method: "DELETE", url: `/api/ipsec/peers/${serverId}/*a1?force=true` }); assert.equal(success.statusCode, 200); assert.equal(success.json().historyRecorded, true); assert.deepEqual(catalogs["/ip/ipsec/identity"], baseline["/ip/ipsec/identity"]) + reset() + const historyFailure = Fastify(); historyFailure.setValidatorCompiler(validatorCompiler); historyFailure.setSerializerCompiler(serializerCompiler) + try { + await historyFailure.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async () => { throw Error("NEVER_HISTORY_PROVIDER") } }), { prefix: "/api" }) + const beforeHistory = deletes, failedHistory = await historyFailure.inject({ method: "DELETE", url: `/api/ipsec/peers/${serverId}/*a1?force=true` }) + assert.equal(failedHistory.statusCode, 502); assert.equal(failedHistory.json().state, "applied"); assert.equal(failedHistory.json().historyRecorded, false); assert.equal(deletes, beforeHistory + 1); assert.ok(!failedHistory.body.includes("NEVER_")) + } finally { await historyFailure.close() } + const recorded = JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows); assert.ok(!recorded.includes("NEVER_")) + console.log("✓ IPsec peer deletion shared REST/MCP: exact native ID, force noncascade, source/opaque drift, protections, one DELETE, partial/unknown, private proof and no replay") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM mcp_protections WHERE object_ref->>'serverId'=$1", [String(serverId)]) + await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + await rest.close(); router.closeAllConnections(); await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-peer-delete-actions.test.ts b/backend/src/modules/mcp/ipsec-peer-delete-actions.test.ts new file mode 100644 index 00000000..2c5cd26e --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-delete-actions.test.ts @@ -0,0 +1,11 @@ +import assert from "node:assert/strict" +import { hasVerifiedIpsecPeerDeletion } from "./ipsec-peer-delete-actions.js" +import { redact } from "./security.js" +const input = { principal: { connectionId: "connection" }, receiptId: "receipt", peerId: "*a1", force: true, expectedAfterProof: "a".repeat(64), expectedProof: "b".repeat(64) } +const result = { receiptId: input.receiptId, peerId: input.peerId, force: true, verified: true, historyRecorded: true, revisionRecorded: true, commandCount: 1, completedCommands: 1, afterProof: input.expectedAfterProof } +const row = { connection_id: "connection", state: "verified", intended_state: redact({ kind: "service", service: "network.ipsec.peers.delete", input }), result } +assert.equal(hasVerifiedIpsecPeerDeletion(row, input, input.expectedAfterProof), true) +for (const patch of [{ state: "unknown" }, { state: "intent" }, { connection_id: "other" }, { intended_state: { ...row.intended_state, input: { ...input, force: false } } }]) assert.equal(hasVerifiedIpsecPeerDeletion({ ...row, ...patch }, input, input.expectedAfterProof), false) +for (const patch of [{ receiptId: "other" }, { peerId: "*a2" }, { force: false }, { historyRecorded: false }, { revisionRecorded: false }, { commandCount: 2 }, { completedCommands: 0 }, { afterProof: "c".repeat(64) }, { verified: false }]) assert.equal(hasVerifiedIpsecPeerDeletion({ ...row, result: { ...result, ...patch } }, input, input.expectedAfterProof), false) +assert.equal(hasVerifiedIpsecPeerDeletion(row, input, input.expectedProof), false) +console.log("✓ IPsec reconciliation requires exact VERIFIED generic step, same connection/intended state/receipt/native ID/one acknowledged command/history proof") diff --git a/backend/src/modules/mcp/ipsec-peer-delete-actions.ts b/backend/src/modules/mcp/ipsec-peer-delete-actions.ts new file mode 100644 index 00000000..e5e811b0 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-delete-actions.ts @@ -0,0 +1,73 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { deleteIpsecPeerGuarded, readIpsecPeerDeleteState, ipsecPeerDeleteTarget, projectIpsecPeerDelete, ipsecPeerDeleteProof } from "../../services/ipsec-peer-delete.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { accessGeneration, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const schema = z.object({ serverId: z.number().int().positive().safe(), peerId: z.string().regex(/^\*[a-z0-9]+$/i).max(100), force: z.boolean().default(false) }).strict() +const operation = "network.ipsec.peers.delete" +async function access(input: Record, signal?: AbortSignal) { + if (signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение IPsec отменено", 409) + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const current = await effectiveMcpConnection(principal) + if (input.operationId !== operation || current.owner_id !== input.ownerId || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write") || !current.policy.scope.operationIds.includes(operation)) throw new McpError("PERMISSION_DENIED", "Допуск удаления IPsec отозван", 403) + if (!current.policy.scope.serverIds.includes(Number(input.serverId))) throw new McpError("SERVER_DENIED", "Сервер IPsec вне допуска", 403) + const request = schema.parse({ serverId: input.serverId, peerId: input.peerId, force: input.force }) + if (fingerprint(request) !== input.requestProof || fingerprint(current.policy) !== input.policyProof || fingerprint(current.owner_permissions) !== input.ownerProof || await accessGeneration() !== input.generation) throw new McpError("RESET_REQUIRED", "Допуск или запрос IPsec изменился", 409) + if (signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение IPsec отменено", 409) +} +async function source(input: Record, signal?: AbortSignal, allow: () => Promise = async () => {}) { + const authorize = async () => { await allow(); await access(input, signal) } + await authorize() + const { server } = await routerClient(Number(input.serverId)), state = await readIpsecPeerDeleteState(server, authorize) + if (input.expectedSource !== undefined && input.expectedSource !== state.source) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился после подготовки", 409) + await authorize() + return { server, state, authorize } +} +export function hasVerifiedIpsecPeerDeletion(candidate: unknown, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | null + return row.connection_id === (input.principal as { connectionId?: string } | undefined)?.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && result.receiptId === input.receiptId && result.verified === true && result.historyRecorded === true && result.revisionRecorded === true && result.peerId === input.peerId && result.force === input.force && result.commandCount === 1 && result.completedCommands === 1 && result.afterProof === proof && proof === input.expectedAfterProof +} +export async function prepareIpsecPeerDeletion(serverId: number, peerId: string, context: ActionContext, force = false): Promise { + const request = schema.parse({ serverId, peerId, force }) + const internal = { ...request, requestProof: fingerprint(request), principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions), generation: await accessGeneration(), operationId: operation, receiptId: randomUUID() } + const { state, authorize } = await source(internal, context.signal) + const { linked } = ipsecPeerDeleteTarget(state, peerId, force) + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(serverId), serverId }, { kind: "ipsec_peer", id: peerId, serverId }] + for (const item of state.protections as { object_ref: { kind: string; id: string } }[]) objects.push({ kind: item.object_ref.kind, id: item.object_ref.id, serverId }) + await authorize() + return { target: { scope: "servers", serverIds: [serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: 2, before: { proof: state.proof }, + steps: [{ kind: "service", service: operation, input: { ...internal, expectedSource: state.source, expectedProof: state.proof, expectedAfterProof: await ipsecPeerDeleteProof(projectIpsecPeerDelete(state, peerId)), proofRevision: 2, beforeFingerprint: fingerprint({ proof: state.proof }) } }], + warnings: [`Одна native DELETE выбранного peer. Связанных identities: ${linked.length}; force=${force}. Identity, сертификаты, mode-config и политики сохраняются; force разрешает разрыв ссылок.`, "Удаление и managed-only история приложения учитываются отдельно. Потеря ответа останавливает операцию без повтора; совпадение конфигурации не подтверждает доставку ответа, историю или передачу трафика."] } +} +export function registerIpsecPeerDeleteActions(register: typeof add) { + const current = async (input: Record) => ({ proof: (await source(input)).state.proof }) + registerMcpService(operation, { current, + execute: async input => { + const before = await source(input) + return deleteIpsecPeerGuarded(before.server, String(input.peerId), input.force === true, String(input.expectedProof), async () => { await assertVerifiedConfigurationAccess(); await before.authorize() }, { receiptId: String(input.receiptId) }) + }, + verify: async (input, result) => { + const latest = await source(input), candidate = { connection_id: (input.principal as { connectionId: string }).connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result } + return hasVerifiedIpsecPeerDeletion(candidate, input, latest.state.proof) + }, + reconcile: async (input, allow) => { + if (input.proofRevision !== 2 || !z.string().uuid().safeParse(input.receiptId).success || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof) || typeof input.expectedAfterProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedAfterProof)) throw new McpError("UNSUPPORTED_CAPABILITY", "Нет совместимого private proof; требуется ручная сверка", 409) + const latest = await source(input, undefined, allow) + const generic = (await pool.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [(input.principal as { connectionId: string }).connectionId, input.receiptId])).rows + const verifiedFinal = generic.length === 1 && hasVerifiedIpsecPeerDeletion(generic[0], input, latest.state.proof) + const final = await source(input, undefined, allow) + if (final.state.proof !== latest.state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился во время сверки", 409) + return { matchesOriginalState: latest.state.proof === input.expectedProof, configurationMatchesDesiredState: latest.state.proof === input.expectedAfterProof, matchesDesiredState: verifiedFinal, verifiedFinalCheckpointAvailable: verifiedFinal, automaticReplay: false, forwardingVerified: false } + }, + }) + register("network.ipsec.peers.delete", "mm_ipsec_peer_remove_plan", "Удалить IPsec peer с точным force и проверкой сохранности конфигурации", "network", "mm:network:write", "write", "servers", schema, { version: 2, prepare: (input, context) => prepareIpsecPeerDeletion(Number(input.serverId), String(input.peerId), context, input.force === true) }) +} diff --git a/backend/src/modules/mcp/ipsec-peer-update-acceptance.ts b/backend/src/modules/mcp/ipsec-peer-update-acceptance.ts new file mode 100644 index 00000000..20110feb --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-update-acceptance.ts @@ -0,0 +1,124 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createIpsecRoutes } from "../../routes/ipsec.js" +import { getEnabledIpsecServerById } from "../../services/ipsec-live.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** SQL/loopback acceptance only. The fixture models the EXPECTED named-reference + * projection; it does not prove RouterOS rename behavior or forwarding. Root + * must wire both shared REST and the generic native update alias first. */ +export async function acceptIpsecPeerUpdate(actor:AuthUser){ + const serverId=196,base:Record[]>=Object.fromEntries(ipsecPeerDeletePaths.map(path=>[path,[]])) + base["/ip/ipsec/peer"]=[{".id":"*p1",name:"before",address:"192.0.2.1","exchange-mode":"ike2",passive:"false",profile:"default",disabled:"true",comment:"MikrotikManager:ipsec",opaque:"NEVER_PEER_OPAQUE"},{".id":"*p2",name:"other",disabled:"true"}] + base["/ip/ipsec/identity"]=[{".id":"*i1",peer:"before",disabled:"true","auth-method":"pre-shared-key",secret:"NEVER_PSK",comment:"MikrotikManager:ipsec"},{".id":"*i2",peer:"*p1",secret:"NEVER_PSK_ID"}] + base["/ip/ipsec/policy"]=[{".id":"*s1",peer:"before,other",dynamic:"false",comment:"MikrotikManager:ipsec",opaque:"NEVER_POLICY"}] + let catalogs=structuredClone(base),mode="normal",patches=0,reads=0,historyCalls=0,connectionId:string|undefined,duringRead:(()=>Promise)|undefined + const bodies:Record[]=[],originalQuery=pool.query.bind(pool) + const router=createServer(async(req,res)=>{ + const path=(req.url??"").replace(/^\/rest/,"");res.setHeader("content-type","application/json") + if(req.method==="GET"){ + reads++;if(duringRead){const hook=duringRead;duringRead=undefined;await hook()} + if(mode==="oversized"){res.end(JSON.stringify("x".repeat(1048577)));return} + if(mode==="malformed"){res.end("{NEVER_PROVIDER");return} + res.end(JSON.stringify(catalogs[path]??[]));return + } + if(req.method==="PATCH"&&path==="/ip/ipsec/peer/*p1"){ + let text="";for await(const chunk of req)text+=chunk + const body=JSON.parse(text)as Record;bodies.push(body);patches++ + if(mode!=="ignored"){ + const previous=catalogs["/ip/ipsec/peer"][0].name;Object.assign(catalogs["/ip/ipsec/peer"][0],body) + if(body.name&&mode!=="rename-no-refs"){ + for(const identity of catalogs["/ip/ipsec/identity"])if(identity.peer===previous)identity.peer=body.name + for(const policy of catalogs["/ip/ipsec/policy"])if(policy.peer)policy.peer=policy.peer.split(",").map(value=>value===previous?body.name:value).join(",") + } + } + if(mode==="collateral")catalogs["/ip/ipsec/identity"][0].secret="NEVER_COLLATERAL" + if(mode==="lost"){req.socket.destroy();return} + res.end("{}");return + } + res.statusCode=405;res.end("{}") + }) + await new Promise(resolve=>router.listen(0,"127.0.0.1",resolve)) + const rest=Fastify();rest.setValidatorCompiler(validatorCompiler);rest.setSerializerCompiler(serializerCompiler) + const reset=()=>{mode="normal";catalogs=structuredClone(base)} + const wait=async(id:string)=>{for(const deadline=Date.now()+140000;Date.now(){historyCalls++;const value=await input.capture();assert.ok(value);return{id:"fixture-history"}as never}}),{prefix:"/api"}) + const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"IPsec update",mode:"autonomous",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[serverId],operationIds:["network.ipsec.peers.update","ros.ipsec_peer.update","operations.reconcile"],allowGlobal:false},limits:{callsPerMinute:120,mutationsPerHour:1000}}),actor) + connectionId=access.connection.id + const principal=await authenticateMcpToken(access.token) + const prepare=(patch:Record={})=>invokeMcpTool(principal,"mm_ipsec_peer_edit_plan",{serverId,peerId:"*p1",patch})as Promise<{planId:string;state:string}> + const apply=async(patch:Record)=>{const plan=await prepare(patch);assert.equal(plan.state,"awaiting-approval");assert.ok(!JSON.stringify(plan).includes("NEVER_"));await approvePlan(plan.planId,actor,true);const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key);return{plan,key,operation,...await wait(operation.id)}} + const aliasOnly=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Alias without business scope",mode:"approval",expiresAt:new Date(Date.now()+600000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[serverId],operationIds:["ros.ipsec_peer.update"],allowGlobal:false}}),actor) + try{const before=reads;await assert.rejects(invokeMcpTool(await authenticateMcpToken(aliasOnly.token),"mm_ipsec_peer_update_plan",{serverId,id:"*p1",fields:{name:"blocked"}}));assert.equal(reads,before)}finally{await setConnectionStatus(aliasOnly.connection.id,"paused",actor)} + const grant=randomUUID();await originalQuery("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)",[grant,access.connection.id,JSON.stringify({id:grant,credentialId:principal.credentialId,startsAt:new Date(Date.now()-1000).toISOString(),expiresAt:new Date(Date.now()+60000).toISOString(),scope:{serverIds:[],operationIds:["network.ipsec.peers.update"],allowGlobal:false}})]) + try{const before=reads;await assert.rejects(prepare({name:"blocked"}));assert.equal(reads,before)}finally{await originalQuery("DELETE FROM mcp_grants WHERE id=$1",[grant])} + for(const certificate of["","arbitrary"]){ + const before=patches,history=historyCalls + await assert.rejects(prepare({certificate})) + const response=await rest.inject({method:"PATCH",url:`/api/ipsec/peers/${serverId}/*p1`,payload:{certificate}});assert.equal(response.statusCode,400);assert.equal(patches,before);assert.equal(historyCalls,history) + } + const request={name:"after",address:"",exchangeMode:"",passive:true,profile:"",disabled:false} + const unapproved=await prepare(request);await assert.rejects(applyPlan(principal,unapproved.planId,randomUUID()));assert.equal(patches,0) + let result=await apply(request);assert.equal(result.state,"applied");assert.deepEqual(bodies.at(-1),{name:"after",address:"",profile:"","exchange-mode":"",passive:"yes",disabled:"no"}) + assert.equal(catalogs["/ip/ipsec/identity"][0].peer,"after");assert.equal(catalogs["/ip/ipsec/identity"][1].peer,"*p1");assert.equal(catalogs["/ip/ipsec/policy"][0].peer,"after,other") + const receipt=(await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.operation.id])).rows[0].result + assert.equal(receipt.historyRecorded,true);assert.equal(receipt.nativeRenameAcceptanceVerified,false);assert.ok(!JSON.stringify(receipt).includes("NEVER_")) + const reconciled=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:result.operation.id})as{automaticRetry:boolean;results:{matchesDesiredState:boolean}[]} + assert.equal(reconciled.automaticRetry,false);assert.equal(reconciled.results[0].matchesDesiredState,true) + const saved=(await originalQuery("SELECT intended_state,result FROM mcp_operation_steps WHERE operation_id=$1",[result.operation.id])).rows[0] + for(const tamper of["history","intended","native","count","state"]){ + const intended=structuredClone(saved.intended_state),value=structuredClone(saved.result) + if(tamper==="history")value.historyRecorded=false + if(tamper==="intended")intended.input.patch.name="tampered" + if(tamper==="native")value.peerId="*p2" + if(tamper==="count")value.completedCommands=0 + await originalQuery("UPDATE mcp_operation_steps SET intended_state=$2::jsonb,result=$3::jsonb,state=$4 WHERE operation_id=$1",[result.operation.id,JSON.stringify(intended),JSON.stringify(value),tamper==="state"?"applied":"verified"]) + const denied=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:result.operation.id})as{results:{matchesDesiredState:boolean}[]} + assert.equal(denied.results[0].matchesDesiredState,false) + } + await originalQuery("UPDATE mcp_operation_steps SET intended_state=$2::jsonb,result=$3::jsonb,state='verified' WHERE operation_id=$1",[result.operation.id,JSON.stringify(saved.intended_state),JSON.stringify(saved.result)]) + const once=patches;await applyPlan(principal,result.plan.planId,result.key);assert.equal(patches,once) + reset();const response=await rest.inject({method:"PATCH",url:`/api/ipsec/peers/${serverId}/*p1`,payload:request});assert.equal(response.statusCode,200,response.body);assert.deepEqual(bodies.at(-1),bodies.at(-2));assert.equal(historyCalls,1) + for(const patch of[{}, {name:"before"}]){reset();const before:number=patches;result=await apply(patch);assert.equal(result.state,"applied");assert.equal(patches,before+(Object.keys(patch).length?1:0));const row=(await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.operation.id])).rows[0].result;assert.equal(row.historyRecorded,true)} + reset();const alias=await invokeMcpTool(principal,"mm_ipsec_peer_update_plan",{serverId,id:"*p1",fields:{name:"alias",address:"",profile:"default","exchange-mode":"ike2",passive:true,disabled:false,comment:"native alias comment"}})as{planId:string} + await approvePlan(alias.planId,actor,true);assert.equal((await wait((await applyPlan(principal,alias.planId,randomUUID())).id)).state,"applied");assert.equal(catalogs["/ip/ipsec/identity"][0].peer,"alias");assert.equal(catalogs["/ip/ipsec/peer"][0].comment,"native alias comment") + for(const fault of["lost","ignored","collateral","rename-no-refs"]){reset();mode=fault;const before:number=patches;result=await apply(request);assert.equal(result.state,fault==="lost"?"unknown":"partial");assert.equal(patches,before+1);await applyPlan(principal,result.plan.planId,result.key);assert.equal(patches,before+1);const reconciliation=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:result.operation.id})as{results:{matchesDesiredState:boolean}[]};assert.equal(reconciliation.results[0].matchesDesiredState,false);assert.equal(patches,before+1)} + reset();let historyFailed=false + pool.query=((...args:unknown[])=>{const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text??"";if(/^insert into "config_revisions"/i.test(sql)){historyFailed=true;return Promise.reject(Error("NEVER_HISTORY_PROVIDER"))}return(originalQuery as(...args:unknown[])=>unknown)(...args)})as typeof pool.query + try{const before:number=patches;result=await apply({...request,name:"history-failure"});assert.equal(result.state,"partial");assert.equal(historyFailed,true);assert.equal(patches,before+1);await applyPlan(principal,result.plan.planId,result.key);assert.equal(patches,before+1)}finally{pool.query=originalQuery as typeof pool.query} + reset();const late=await prepare(request);await approvePlan(late.planId,actor,true);let fenced=false + pool.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text??"",params=args[1]as unknown[]|undefined + const pending=(originalQuery as(...args:unknown[])=>Promise)(...args) + if(!fenced&&sql.startsWith("INSERT INTO mcp_audit")&¶ms?.[1]==="configuration.intent"&&String(params?.[3]).includes('"method":"PATCH"')){fenced=true;return pending.then(value=>{catalogs["/ip/ipsec/identity"][0].secret="NEVER_LATE_SOURCE";return value})} + return pending + })as typeof pool.query + try{const before:number=patches;assert.equal((await wait((await applyPlan(principal,late.planId,randomUUID())).id)).state,"unknown");assert.equal(fenced,true);assert.equal(patches,before)}finally{pool.query=originalQuery as typeof pool.query} + reset();const stale=await prepare(request);await approvePlan(stale.planId,actor,true);catalogs["/ip/ipsec/identity"][0].secret="NEVER_STALE";const beforeStale=patches;assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected");assert.equal(patches,beforeStale) + reset();const protection=randomUUID();await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"ipsec_identity",id:"*i1",serverId,protection:"deny"}),actor.id]) + try{await assert.rejects(prepare(request));assert.equal((await rest.inject({method:"PATCH",url:`/api/ipsec/peers/${serverId}/*p1`,payload:request})).statusCode,409);assert.equal(patches,beforeStale)}finally{await originalQuery("DELETE FROM mcp_protections WHERE id=$1",[protection])} + reset();mode="oversized";await assert.rejects(prepare(request));mode="malformed";await assert.rejects(prepare(request));reset() + const cancelled=new AbortController();cancelled.abort();await assert.rejects(invokeMcpTool(principal,"mm_ipsec_peer_edit_plan",{serverId,peerId:"*p1",patch:request},cancelled.signal)) + duringRead=()=>originalQuery("UPDATE servers SET password='changed-source' WHERE id=$1",[serverId]);await assert.rejects(prepare(request));await originalQuery("UPDATE servers SET password='NEVER_ENDPOINT' WHERE id=$1",[serverId]) + await setConnectionStatus(access.connection.id,"paused",actor);await assert.rejects(prepare(request)) + assert.ok(!JSON.stringify((await originalQuery("SELECT payload FROM mcp_audit WHERE connection_id=$1",[connectionId])).rows).includes("NEVER_")) + assert.ok(reads>0) + console.log("✓ IPsec peer PATCH SQL/loopback: all REST fields/empty clears, certificate400, same-value PATCH/noop history, expected reference projection, ignored/collateral/lost/no-auto-rename fail closed, shared REST/protections/source and no replay; native rename/forwarding gate remains open") + }finally{ + pool.query=originalQuery as typeof pool.query + if(connectionId)await setConnectionStatus(connectionId,"paused",actor) + await rest.close();await originalQuery("DELETE FROM servers WHERE id=$1",[serverId]);router.closeAllConnections();await new Promise(resolve=>router.close(()=>resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-peer-update-actions.test.ts b/backend/src/modules/mcp/ipsec-peer-update-actions.test.ts new file mode 100644 index 00000000..efc7e2ce --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-update-actions.test.ts @@ -0,0 +1,14 @@ +import assert from "node:assert/strict" +import { fingerprint, redact } from "./security.js" +import { ipsecPeerUpdateActionSchema, hasVerifiedIpsecPeerUpdate } from "./ipsec-peer-update-actions.js" +const request={serverId:1,peerId:"*p1",patch:{address:"",exchangeMode:"",profile:"",passive:false,disabled:true}} +assert.deepEqual(ipsecPeerUpdateActionSchema.parse(request),request) +for(const patch of[{certificate:""},{certificate:"private"},{secret:"raw"},{passive:"yes"}])assert.equal(ipsecPeerUpdateActionSchema.safeParse({...request,patch}).success,false) +assert.equal(ipsecPeerUpdateActionSchema.safeParse({...request,patch:{}}).success,true) +const input={...request,principal:{connectionId:"connection"},receiptId:"receipt",expectedAfterProof:"after",commandCount:1} +const row={connection_id:"connection",state:"verified",intended_state:redact({kind:"service",service:"network.ipsec.peers.update",input}),result:{receiptId:"receipt",verified:true,historyRecorded:true,revisionRecorded:true,peerId:"*p1",commandCount:1,completedCommands:1,afterProof:"after"}} +assert.equal(hasVerifiedIpsecPeerUpdate(row,input,"after"),true) +for(const mutate of[(value:typeof row)=>{value.state="applied"},(value:typeof row)=>{value.result.historyRecorded=false},(value:typeof row)=>{value.result.completedCommands=0},(value:typeof row)=>{value.result.peerId="*p2"},(value:typeof row)=>{value.result.afterProof="wrong"}]){const bad=structuredClone(row);mutate(bad);assert.equal(hasVerifiedIpsecPeerUpdate(bad,input,"after"),false)} +assert.equal(hasVerifiedIpsecPeerUpdate(row,{...input,patch:{name:"tampered"}},"after"),false) +assert.ok(!fingerprint(row).includes("raw")) +console.log("IPsec peer update MCP schema and exact generic VERIFIED receipt binding") diff --git a/backend/src/modules/mcp/ipsec-peer-update-actions.ts b/backend/src/modules/mcp/ipsec-peer-update-actions.ts new file mode 100644 index 00000000..ee17380b --- /dev/null +++ b/backend/src/modules/mcp/ipsec-peer-update-actions.ts @@ -0,0 +1,72 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { ipsecPeerUpdatePatchSchema, ipsecPeerUpdateNativePatch, planIpsecPeerUpdate, readIpsecPeerUpdateState, ipsecPeerUpdateProof, updateIpsecPeerGuarded } from "../../services/ipsec-peer-update.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { routerClient } from "./resources.js" +import { accessGeneration, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +export const ipsecPeerUpdateActionSchema=z.object({serverId:z.number().int().positive().safe(),peerId:z.string().regex(/^\*[a-z0-9]+$/i).max(100),patch:ipsecPeerUpdatePatchSchema}).strict() +const operation="network.ipsec.peers.update" +async function access(input:Record,signal?:AbortSignal){ + if(signal?.aborted)throw new McpError("READ_CANCELLED","Чтение IPsec отменено",409) + const principal=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict().parse(input.principal) + const current=await effectiveMcpConnection(principal) + if(input.operationId!==operation||current.owner_id!==input.ownerId||!hasPermission(current.owner_permissions,"mm:network:write")||!hasPermission(current.policy.permissions,"mm:network:write")||!current.policy.scope.operationIds.includes(operation))throw new McpError("PERMISSION_DENIED","Допуск PATCH IPsec отозван",403) + if(!current.policy.scope.serverIds.includes(Number(input.serverId)))throw new McpError("SERVER_DENIED","Сервер IPsec вне допуска",403) + const request=ipsecPeerUpdateActionSchema.parse({serverId:input.serverId,peerId:input.peerId,patch:input.patch}) + if(fingerprint(request)!==input.requestProof||fingerprint(current.policy)!==input.policyProof||fingerprint(current.owner_permissions)!==input.ownerProof||await accessGeneration()!==input.generation)throw new McpError("RESET_REQUIRED","Допуск или запрос IPsec изменился",409) + if(signal?.aborted)throw new McpError("READ_CANCELLED","Чтение IPsec отменено",409) +} +async function source(input:Record,signal?:AbortSignal,allow:()=>Promise=async()=>{}){ + const authorize=async()=>{await allow();await access(input,signal)} + await authorize() + const{server}=await routerClient(Number(input.serverId)),state=await readIpsecPeerUpdateState(server,authorize) + if(input.expectedSource!==undefined&&input.expectedSource!==state.source)throw new McpError("STATE_CONFLICT","Источник IPsec изменился",409) + await authorize();return{server,state,authorize} +} +export function hasVerifiedIpsecPeerUpdate(candidate:unknown,input:Record,proof:string){ + if(!candidate||typeof candidate!=="object")return false + const row=candidate as Record,result=row.result as Record|null + return row.connection_id===(input.principal as{connectionId?:string}|undefined)?.connectionId&&row.state==="verified"&&fingerprint(row.intended_state)===fingerprint(redact({kind:"service",service:operation,input}))&&!!result&&result.receiptId===input.receiptId&&result.verified===true&&result.historyRecorded===true&&result.revisionRecorded===true&&result.peerId===input.peerId&&result.commandCount===input.commandCount&&result.completedCommands===input.commandCount&&result.afterProof===proof&&proof===input.expectedAfterProof +} +export async function prepareIpsecPeerUpdate(serverId:number,peerId:string,patch:unknown,context:ActionContext):Promise{ + const request=ipsecPeerUpdateActionSchema.parse({serverId,peerId,patch}) + const internal={...request,requestProof:fingerprint(request),principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy),ownerProof:fingerprint(context.connection.owner_permissions),generation:await accessGeneration(),operationId:operation,receiptId:randomUUID()} + const{state,authorize}=await source(internal,context.signal),plan=planIpsecPeerUpdate(state,peerId,patch) + const objects:ChangeEvidence["objects"]=[{kind:"server",id:String(serverId),serverId},{kind:"ipsec_peer",id:peerId,serverId},...plan.companions.map(ref=>({...ref,serverId}))] + for(const item of state.protections as{object_ref:{kind:string;id:string}}[])objects.push({...item.object_ref,serverId}) + await authorize() + return{target:{scope:"servers",serverIds:[serverId]},objects,snapshots:[],requiresHuman:true,mutationCost:plan.commandCount+1+plan.companions.length,before:{proof:state.proof}, + steps:[{kind:"service",service:operation,input:{...internal,expectedSource:state.source,expectedProof:state.proof,expectedAfterProof:await ipsecPeerUpdateProof(plan.projected),commandCount:plan.commandCount,proofRevision:2,beforeFingerprint:fingerprint({proof:state.proof})}}], + warnings:[`Будет отправлено native PATCH: ${plan.commandCount}; затем обязательная managed-only история IPsec. Пустые строки адреса/profile/exchange-mode передаются как есть; same-value поля не пропускают PATCH. Пустой patch оставляет только историю.`, `Ожидаемых автоматических ссылок при rename: ${plan.companions.length}. Дополнительные PATCH identities/policies не отправляются; только точный whole-state read-back может подтвердить результат. Бюджет включает peer PATCH, историю и все затронутые ссылки.`,"Автоматический native rename ещё требует отдельной CHR-приёмки. Несовпадение ссылок или постороннее изменение даёт partial; потеря ответа — unknown. Ни повтор, ни обратное изменение автоматически не выполняются; пересылка трафика не подтверждается."]} +} +/** Generic native updates still require an explicit business-operation grant; + * the shared source access check runs before the first RouterOS read. */ +export async function prepareNativeIpsecPeerUpdate(serverId:number,nativeId:string,fields:unknown,context:ActionContext):Promise{ + return prepareIpsecPeerUpdate(serverId,nativeId,ipsecPeerUpdateNativePatch(fields),context) +} +export function registerIpsecPeerUpdateActions(register:typeof add){ + registerMcpService(operation,{ + current:async input=>({proof:(await source(input)).state.proof}), + execute:async input=>{ + const before=await source(input) + return updateIpsecPeerGuarded(before.server,String(input.peerId),input.patch,String(input.expectedProof),async()=>{await assertVerifiedConfigurationAccess();await before.authorize()},{receiptId:String(input.receiptId),checkpoint:async value=>{await assertVerifiedConfigurationAccess();await before.authorize();await audit(pool,String(input.ownerId),"ipsec.peer.update.checkpoint",(input.principal as{connectionId:string}).connectionId,{...value,inputProof:fingerprint(redact(input))})}}) + }, + verify:async(input,result)=>{const latest=await source(input);return hasVerifiedIpsecPeerUpdate({connection_id:(input.principal as{connectionId:string}).connectionId,state:"verified",intended_state:redact({kind:"service",service:operation,input}),result},input,latest.state.proof)}, + reconcile:async(input,allow)=>{ + if(input.proofRevision!==2||!z.string().uuid().safeParse(input.receiptId).success||![0,1].includes(Number(input.commandCount))||typeof input.expectedProof!=="string"||!/^[a-f0-9]{64}$/.test(input.expectedProof)||typeof input.expectedAfterProof!=="string"||!/^[a-f0-9]{64}$/.test(input.expectedAfterProof))throw new McpError("UNSUPPORTED_CAPABILITY","Нет совместимого private proof PATCH IPsec",409) + const latest=await source(input,undefined,allow) + const rows=(await pool.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2",[(input.principal as{connectionId:string}).connectionId,input.receiptId])).rows + const verified=rows.length===1&&hasVerifiedIpsecPeerUpdate(rows[0],input,latest.state.proof) + if((await source(input,undefined,allow)).state.proof!==latest.state.proof)throw new McpError("STATE_CONFLICT","IPsec изменился во время сверки",409) + return{matchesOriginalState:latest.state.proof===input.expectedProof,configurationMatchesDesiredState:latest.state.proof===input.expectedAfterProof,matchesDesiredState:verified,verifiedFinalCheckpointAvailable:verified,automaticReplay:false,forwardingVerified:false} + }, + }) + register(operation,"mm_ipsec_peer_edit_plan","Изменить IPsec peer с точными native ссылками и managed-only историей","network","mm:network:write","write","servers",ipsecPeerUpdateActionSchema,{version:2,prepare:(input,context)=>prepareIpsecPeerUpdate(Number(input.serverId),String(input.peerId),input.patch,context)}) +} diff --git a/backend/src/modules/mcp/ipsec-revision-acceptance.ts b/backend/src/modules/mcp/ipsec-revision-acceptance.ts new file mode 100644 index 00000000..a096b8c0 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-revision-acceptance.ts @@ -0,0 +1,107 @@ +import assert from "node:assert/strict" +import { installLoopbackFixtureAgent } from "../../test/loopback-http-agent.js" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createIpsecRoutes } from "../../routes/ipsec.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { ipsecExternalPoolDependencyPaths } from "../../services/ipsec-pool-dependencies.js" +import { canonicalIpsecSnapshot } from "../../services/entity-snapshots.js" +import { appendRevisionIfChanged, fingerprintPayload } from "../../services/config-revisions.js" +import { ipsecManagedComment } from "../../services/ipsec-config.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Opt-in serial disposable SQL + loopback acceptance. No live RouterOS. */ +export async function acceptIpsecRevisionRestore(actor: AuthUser) { + type Row = Record + const serverId = 209, query = pool.query.bind(pool), comment = ipsecManagedComment("restore") + const base: Record = { + ...Object.fromEntries([...ipsecPeerDeletePaths, ...ipsecExternalPoolDependencyPaths, "/ip/route", "/interface", "/interface/list", "/interface/list/member"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*1", name: "owned-peer", address: "0.0.0.0/0", "exchange-mode": "ike2", profile: "profile", passive: "true", disabled: "false", comment, opaque: "NEVER_PEER" }], + "/ip/ipsec/identity": [{ ".id": "*2", peer: "owned-peer", "auth-method": "pre-shared-key", secret: "NEVER_PSK", "remote-id": "old-id", "mode-config": "ipsec-vpn", comment, disabled: "false", opaque: "NEVER_IDENTITY" }, { ".id": "*3", peer: "owned-peer", "auth-method": "digital-signature", certificate: "server-cert", "mode-config": "ipsec-vpn", "generate-policy": "port-strict", "policy-template-group": "group", comment: ipsecManagedComment("cert"), disabled: "false" }], + "/ip/ipsec/mode-config": [{ ".id": "*4", name: "ipsec-vpn", "address-pool": "pool", responder: "true", "static-dns": "192.0.2.53", opaque: "NEVER_MC" }], + "/ip/pool": [{ ".id": "*5", name: "pool", ranges: "10.77.0.2-10.77.0.254", comment, opaque: "NEVER_POOL" }], + "/ip/ipsec/policy": [{ ".id": "*6", template: "true", group: "group", "src-address": "0.0.0.0/0", "dst-address": "10.77.0.0/24", proposal: "proposal", comment, opaque: "NEVER_POLICY" }], + "/ip/firewall/nat": [{ ".id": "*7", chain: "srcnat", action: "masquerade", "src-address": "10.77.0.0/24", "out-interface-list": "WAN", "ipsec-policy": "out,none", comment, opaque: "NEVER_NAT" }], + "/ip/ipsec/profile": [{ ".id": "*8", name: "profile", opaque: "NEVER_PROFILE" }], "/ip/ipsec/proposal": [{ ".id": "*9", name: "proposal", opaque: "NEVER_PROPOSAL" }], "/ip/ipsec/policy/group": [{ ".id": "*a", name: "group", opaque: "NEVER_GROUP" }], + "/certificate": [{ ".id": "*b", name: "ca", fingerprint: "signed-ca", "invalid-after": "2030-01-01" }, { ".id": "*c", name: "server-cert", ca: "ca", fingerprint: "signed", "invalid-after": "2030-01-01", "private-key": "true", opaque: "NEVER_CERT" }], + "/interface": [{ ".id": "*d", name: "ether1" }], "/interface/list": [{ ".id": "*e", name: "WAN" }], + } + const desired = canonicalIpsecSnapshot({ peers: [{ name: "owned-peer", address: "", exchangeMode: "ike2", profile: "profile", passive: true, disabled: false, comment }], identities: [{ peerName: "owned-peer", authMethod: "pre-shared-key", secret: "(hidden)", modeConfig: "ipsec-vpn", comment }, { peerName: "owned-peer", authMethod: "digital-signature", certificate: "server-cert", modeConfig: "ipsec-vpn", generatePolicy: "port-strict", policyTemplateGroup: "group", comment: ipsecManagedComment("cert") }], modeConfigs: [{ name: "ipsec-vpn", addressPool: "pool", staticDns: "" }], pools: [{ name: "pool", ranges: "10.77.0.2-10.77.0.200", comment }], policies: [{ srcAddress: "0.0.0.0/0", dstAddress: "10.78.0.0/24", proposal: "proposal", comment }], nat: [{ chain: "srcnat", action: "masquerade", srcAddress: "10.78.0.0/24", outInterface: "ether1", comment }] }) + let catalogs = structuredClone(base), serial = 0, mode = "normal", connectionId: string | undefined, afterAck: (() => Promise) | undefined, revisionId = "" + const commands: Array<{ method: string; path: string; fields: Row }> = [] + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")); res.setHeader("content-type", "application/json") + try { + 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 : {}; commands.push({ method: req.method!, path, fields }) + const catalog = Object.hasOwn(catalogs, path) ? path : Object.keys(catalogs).sort((a, b) => b.length - a.length).find(value => path.startsWith(value + "/")); assert(catalog) + let result: Row = {} + if (mode !== "ignored") { + if (req.method === "PUT") { result = { ".id": `*new${++serial}`, ...fields, "native-default": "NEVER_NEW_DEFAULT", ...(mode === "recordedDefault" && catalog === "/ip/ipsec/identity" ? { "remote-id": "auto" } : {}) }; catalogs[catalog].push(result) } + else if (req.method === "PATCH") { const row = catalogs[catalog].find(row => row[".id"] === path.slice(catalog.length + 1)); assert(row); Object.assign(row, fields); if (mode === "omitClears") for (const [key, value] of Object.entries(fields)) if (value === "") delete row[key] } + else if (req.method === "DELETE") catalogs[catalog] = catalogs[catalog].filter(row => row[".id"] !== path.slice(catalog.length + 1)) + else throw Error("unsupported mutation") + } + if (mode === "collateral") catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_COLLATERAL" + if (afterAck) { const run = afterAck; afterAck = undefined; await run() } + if (mode === "lost") { req.socket.destroy(); return } + if (mode === "missingAck" && req.method === "PUT") result = {} + res.end(JSON.stringify(result)) + } catch { res.statusCode = 500; res.end('{"error":"NEVER_PROVIDER"}') } + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const closeFixtureAgent = installLoopbackFixtureAgent(router) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = () => { catalogs = structuredClone(base); serial = 0; mode = "normal"; afterAck = undefined } + const hidden = (value: unknown) => assert(!JSON.stringify(value).includes("NEVER_"), "private revision or raw state leaked") + const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const row = (await query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) }; throw Error("Restore active; never replay") } + try { + assert.equal(Number((await query("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0) + await query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-restore','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]) + await rest.register(createIpsecRoutes(), { prefix: "/api" }) + const save = async (payload = desired) => { revisionId = (await appendRevisionIfChanged({ serverId, section: "ipsec", source: "copy", payload: canonicalIpsecSnapshot(payload) })).revision.id; return revisionId } + await save() + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec revision restore", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.revisions.restore", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id; const principal = await authenticateMcpToken(access.token) + const prepare = () => invokeMcpTool(principal, "mm_ipsec_revision_restore_plan", { serverId, revisionId }) as Promise<{ planId: string; state: string }> + const apply = async () => { const plan = await prepare(); hidden(plan); assert.equal(plan.state, "awaiting-approval"); await approvePlan(plan.planId, actor, true); const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key); return { plan, key, operation, ...await wait(operation.id) } } + const receipt = async (id: string) => (await query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [id])).rows[0].result + const normalize = (entries: typeof commands) => entries.map(row => ({ ...row, path: row.path.replace(/\*new\d+/g, "*created") })) + for (const clears of ["normal", "omitClears"]) { + reset(); mode = clears; await save(); const start = commands.length, result = await apply() + assert.equal(result.state, "applied", JSON.stringify({ result, receipt: await receipt(result.operation.id), native: commands.slice(start).map(row => ({ method: row.method, path: row.path, fieldNames: Object.keys(row.fields) })) })); hidden(result) + const accepted = normalize(commands.slice(start)); hidden(await receipt(result.operation.id)); assert.equal(catalogs["/ip/ipsec/identity"].find(row => row[".id"] === "*2")!.secret, "NEVER_PSK") + assert.equal(catalogs["/ip/ipsec/policy"][0]["dst-address"], "10.78.0.0/24"); assert.equal(catalogs["/ip/ipsec/policy"][0].opaque, "NEVER_POLICY") + for (const path of ["/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group"]) assert.deepEqual(catalogs[path], base[path]) + reset(); mode = clears; const restStart = commands.length, response = await rest.inject({ method: "POST", url: `/api/ipsec/revisions/${revisionId}/restore`, payload: { serverId } }); assert.equal(response.statusCode, 200, response.body); assert.deepEqual(normalize(commands.slice(restStart)), accepted) + } + reset(); await save(canonicalIpsecSnapshot({})); const deletion = await apply(); assert.equal(deletion.state, "applied"); for (const path of ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat"]) assert.deepEqual(catalogs[path], []) + const withNewPool = { ...desired, pools: [...desired.pools, { name: "new-pool", ranges: "192.0.2.2-192.0.2.10", comment: ipsecManagedComment("new-pool") }] } + reset(); await save(withNewPool); const created = await apply(); assert.equal(created.state, "applied"); assert.equal(catalogs["/ip/pool"].find(row => row.name === "new-pool")!["native-default"], "NEVER_NEW_DEFAULT") + reset(); mode = "missingAck"; const ackStart = commands.length, missingAck = await apply(); assert(["partial", "unknown"].includes(missingAck.state)); assert.equal(commands[ackStart].method, "PUT"); assert.equal(commands[ackStart].fields.name, "new-pool"); assert.equal(commands.length, ackStart + 1); await applyPlan(principal, missingAck.plan.planId, missingAck.key); assert.equal(commands.length, ackStart + 1) + reset(); await save({ ...desired, identities: [...desired.identities, { ...desired.identities.find(row => row.authMethod === "pre-shared-key")!, secret: "NEVER_NEW_PSK", comment: ipsecManagedComment("new-explicit") }] }); mode = "recordedDefault" + const defaultStart = commands.length, defaultResult = await apply(); assert(["partial", "unknown"].includes(defaultResult.state)); const defaultDispatches = commands.slice(defaultStart); assert.equal(defaultDispatches.at(-1)!.method, "PUT"); assert.equal(defaultDispatches.at(-1)!.path, "/ip/ipsec/identity"); assert.equal(defaultDispatches.filter(row => row.path === "/ip/firewall/nat/*7").length, 0); const defaultStoppedAt = commands.length; await applyPlan(principal, defaultResult.plan.planId, defaultResult.key); assert.equal(commands.length, defaultStoppedAt) + reset(); await save(); const start = commands.length, unapproved = await prepare(); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, start) + for (const path of ["/ip/ipsec/profile", "/certificate", ...ipsecExternalPoolDependencyPaths, "/interface/list"]) { reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); catalogs[path].push({ ".id": "*late", name: "late", opaque: "NEVER_LATE" }); assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, start) } + reset(); const staleRevision = await prepare(); await approvePlan(staleRevision.planId, actor, true); await query("UPDATE config_revisions SET note='changed' WHERE id=$1", [revisionId]); assert.notEqual((await wait((await applyPlan(principal, staleRevision.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, start) + for (const mutate of [() => { catalogs["/certificate"] = [] }, () => { catalogs["/certificate"][1]["private-key"] = "false" }, () => { catalogs["/ip/ipsec/profile"] = [] }, () => { catalogs["/ip/ipsec/proposal"] = [] }, () => { catalogs["/ip/ipsec/policy/group"] = [] }, () => { catalogs["/ip/ipsec/policy"] = [] }, () => { catalogs["/ip/ipsec/policy"].push({ ".id": "*consumer", comment: "foreign", peer: "other,*0001" }) }]) { reset(); mutate(); await assert.rejects(prepare()); assert.equal(commands.length, start) } + for (const path of ipsecExternalPoolDependencyPaths) { reset(); catalogs[path].push({ ".id": "*allocator", [path.startsWith("/ppp/") ? "remote-address" : "address-pool"]: "*0005" }); await assert.rejects(prepare()); assert.equal(commands.length, start) } + reset(); await query("UPDATE config_revisions SET fingerprint=$2 WHERE id=$1", [revisionId, "0".repeat(64)]); await assert.rejects(prepare()); await query("UPDATE config_revisions SET fingerprint=$2 WHERE id=$1", [revisionId, fingerprintPayload(desired)]) + for (const fault of ["lost", "ignored", "collateral"]) { reset(); mode = fault; const begin = commands.length, result = await apply(); assert(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, begin + 1); await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, begin + 1); const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] }; assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, false); assert.equal(commands.length, begin + 1) } + reset(); const begin = commands.length; afterAck = () => setConnectionStatus(connectionId!, "paused", actor); const revoked = await apply(); assert(["partial", "unknown"].includes(revoked.state)); assert.equal(commands.length, begin + 1) + for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) hidden((await query(table === "mcp_operation_steps" ? "SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1" : `SELECT * FROM ${table} WHERE connection_id=$1`, [connectionId])).rows) + console.log("✓ IPsec revision SQL/loopback: full recorded fields/policies and exact clears, managed deletion, REST parity, revision fingerprint/xmin drift, hidden PSK preserve, PKI/dependency/pool foreign guards, loss/collateral/ignored no replay, revoke and private audit") + } finally { + closeFixtureAgent() + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await rest.close(); await query("DELETE FROM servers WHERE id=$1", [serverId]); router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-revision-actions.test.ts b/backend/src/modules/mcp/ipsec-revision-actions.test.ts new file mode 100644 index 00000000..03a61784 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-revision-actions.test.ts @@ -0,0 +1,60 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import type { ActionContext } from "./action-types.js" +import type { ConnectionRow } from "./store.js" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import { createIpsecRevisionRestoreAdapter, ipsecRevisionRestoreActionSchema } from "./ipsec-revision-actions.js" +import { redact } from "./security.js" +const operation = "network.ipsec.revisions.restore", sourceProof = "a".repeat(64), beforeProof = "b".repeat(64), afterProof = "c".repeat(64) +const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } +const connection = { id: principal.connectionId, owner_id: "fixture-owner", owner_permissions: ["mm:network:write"], generation: "fixture-generation", owner_valid_until: new Date(Date.now() + 600000), policy: { mode: "autonomous", status: "active", expiresAt: principal.credentialExpiresAt, permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: [operation], allowGlobal: false } } } as ConnectionRow +const context = { principal, connection } as ActionContext +const request = { serverId: 1, revisionId: randomUUID() } +assert.equal(ipsecRevisionRestoreActionSchema.safeParse(request).success, true) +for (const key of ["payload", "psk", "secret", "passphrase", "unknown"]) assert.equal(ipsecRevisionRestoreActionSchema.safeParse({ ...request, [key]: "NEVER_RAW" }).success, false) +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Real SQL forbidden") }) as never; pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +try { + const sealed = new Map() + let current = structuredClone(connection), proof = beforeProof, endpoint = sourceProof, revisionProof = "d".repeat(64), writes = 0, history = true + let stored: Record[] = [], inject: (() => void) | undefined + const adapter = createIpsecRevisionRestoreAdapter({ + connection: async () => current, + router: async () => ({ server: { id: 1 } as ServerRow, client: {} as never }), sourceProof: async () => endpoint, + revision: async (_serverId, revisionId, authorize) => { await authorize?.(); assert.equal(revisionId, request.revisionId); return { proof: revisionProof, payload: {} as never } }, + read: async (_server, authorize) => { await authorize?.(); return { source: sourceProof, proof, catalogs: {}, protections: [{ object_ref: { kind: "certificate", id: "*protected" } }] } }, + seal: async value => { const key = `sealed:${randomUUID()}`; sealed.set(key, value); return key }, open: async value => sealed.get(value)!, + query: async () => stored, verifiedAccess: async () => {}, checkpoint: async () => {}, + plan: (() => ({ commands: [{ action: "create", path: "/certificate", fields: {} }], companions: [{ kind: "ipsec_peer", id: "*p" }], desired: {} })) as never, + restore: (async (_server: unknown, revisionId: string, expected: string, authorize: () => Promise, options: { receiptId: string; checkpoint: (value: unknown) => Promise }) => { + assert.equal(expected, beforeProof); assert.equal(revisionId, request.revisionId) + await authorize(); inject?.(); await authorize(); writes++; proof = afterProof + await options.checkpoint({ receiptId: options.receiptId, completedCommands: 1, commandCount: 1, afterProof }) + return { ok: true, verified: true, receiptId: options.receiptId, serverId: 1, revisionId: request.revisionId, commandCount: 1, completedCommands: 1, confirmedWrites: 1, afterProof, historyRecorded: history, revisionRecorded: history, automaticReplay: false, forwardingVerified: false } + }) as never, + }) + const prepare = async () => { + const evidence = await adapter.prepare(request, context) + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 2) + assert(evidence.objects.some(row => row.id === "*p")); assert(evidence.objects.some(row => row.id === "*protected")) + assert(!JSON.stringify(evidence).includes("NEVER_RAW")) + const step = evidence.steps[0]; if (step.kind !== "service") throw Error("service expected") + return step.input + } + const reset = () => { proof = beforeProof; current = structuredClone(connection); endpoint = sourceProof; revisionProof = "d".repeat(64); inject = undefined; history = true; stored = [] } + let input = await prepare(), receipt = await adapter.service.execute(input) + assert.equal(writes, 1); assert(!JSON.stringify(receipt).includes("NEVER_RAW")); assert.equal(await adapter.service.verify!(input, receipt), true) + stored = [{ connection_id: principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result: receipt }] + assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, true) + for (const field of ["historyRecorded", "revisionRecorded", "verified", "receiptId", "commandCount", "afterProof", "serverId", "revisionId"]) { + stored[0].result = { ...receipt as object, [field]: "changed" }; assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, false) + } + reset(); input = await prepare(); history = false; receipt = await adapter.service.execute(input); assert.equal(await adapter.service.verify!(input, receipt), false) + for (const mutate of [() => { proof = afterProof }, () => { endpoint = "e".repeat(64) }, () => { revisionProof = "e".repeat(64) }, () => { current.policy.status = "paused" }, () => { current.owner_permissions = [] }, () => { current.generation = "changed" }, () => { current.policy.scope.serverIds = [] }, () => { current.policy.scope.operationIds = [] }]) { + reset(); input = await prepare(); const start = writes; mutate(); await assert.rejects(adapter.service.execute(input)); assert.equal(writes, start) + } + reset(); input = await prepare(); inject = () => { current.policy.status = "paused" }; const start = writes; await assert.rejects(adapter.service.execute(input)); assert.equal(writes, start) + reset(); input = await prepare(); const aborted = new AbortController(); aborted.abort(); await assert.rejects(adapter.prepare(request, { ...context, signal: aborted.signal })) + console.log("IPsec revision adapter: mandatory approval, sealed row binding, revision/source/grant/owner cancellation, checkpoint/history and receipt reconciliation, no payload or secrets returned") +} finally { pool.query = oldQuery; pool.connect = oldConnect } diff --git a/backend/src/modules/mcp/ipsec-revision-actions.ts b/backend/src/modules/mcp/ipsec-revision-actions.ts new file mode 100644 index 00000000..36c8d43f --- /dev/null +++ b/backend/src/modules/mcp/ipsec-revision-actions.ts @@ -0,0 +1,96 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { planIpsecRevisionRestore } from "../../services/ipsec-revision-restore-model.js" +import { readIpsecRevisionRestoreState, readIpsecRevisionRestoreRevision, restoreIpsecRevisionGuarded } from "../../services/ipsec-revision-restore.js" +import { routerClient } from "./resources.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService, type McpServiceHandler } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const operation = "network.ipsec.revisions.restore", hash = z.string().regex(/^[a-f0-9]{64}$/) +export const ipsecRevisionRestoreActionSchema = z.object({ serverId: z.number().int().positive().safe(), revisionId: z.string().uuid() }).strict() +const bindingSchema = ipsecRevisionRestoreActionSchema.extend({ + operation: z.literal(operation), principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: hash, policyProof: hash, generation: z.string().min(1), sourceProof: hash, expectedProof: hash, expectedRevisionProof: hash, + requestProof: hash, commandCount: z.number().int().min(0).max(1000), receiptId: z.string().uuid(), proofRevision: z.literal(1), +}).strict() +type Binding = z.infer +const serviceInput = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(262144) }).strict() +const production = { + connection: effectiveMcpConnection, router: routerClient, read: readIpsecRevisionRestoreState, revision: readIpsecRevisionRestoreRevision, sourceProof: ipsecPeerDeleteSourceProof, + plan: planIpsecRevisionRestore, restore: restoreIpsecRevisionGuarded, seal: encryptSecret, open: decryptSecret, verifiedAccess: assertVerifiedConfigurationAccess, + query: async (sql: string, values: unknown[]) => (await pool.query(sql, values)).rows as Record[], + checkpoint: async (ownerId: string, connectionId: string, value: unknown) => audit(pool, ownerId, "ipsec.revision.restore.checkpoint", connectionId, value), +} +const conflict = () => new McpError("STATE_CONFLICT", "IPsec, ревизия, источник или допуск отката изменились", 409) +export function hasVerifiedIpsecRevisionRestore(candidate: unknown, binding: Binding, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | undefined + return row.connection_id === binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && + result.ok === true && result.verified === true && result.receiptId === binding.receiptId && result.serverId === binding.serverId && result.revisionId === binding.revisionId && result.commandCount === binding.commandCount && result.completedCommands === binding.commandCount && result.confirmedWrites === binding.commandCount && + result.historyRecorded === true && result.revisionRecorded === true && result.afterProof === proof && result.forwardingVerified === false && result.automaticReplay === false +} +export function createIpsecRevisionRestoreAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const access = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + const current = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || current.owner_valid_until.getTime() <= Date.now() || Date.parse(current.policy.expiresAt) <= Date.now() || !current.policy.scope.operationIds.includes(operation) || !current.policy.scope.serverIds.includes(binding.serverId) || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска отката IPsec", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof || fingerprint({ serverId: binding.serverId, revisionId: binding.revisionId }) !== binding.requestProof) throw conflict() + signal?.throwIfAborted() + } + const request = async (input: Record) => { const parsed = serviceInput.parse(input), binding = bindingSchema.parse(JSON.parse(await deps.open(parsed.bindingCiphertext))); if (binding.serverId !== parsed.serverId || binding.receiptId !== parsed.receiptId) throw conflict(); return binding } + const source = async (binding: Binding, signal?: AbortSignal, external?: () => Promise) => { + const authorize = async () => { await access(binding, signal, external); if ((await deps.revision(binding.serverId, binding.revisionId, () => access(binding, signal, external))).proof !== binding.expectedRevisionProof) throw conflict() } + await authorize() + const { server } = await deps.router(binding.serverId) + if (await deps.sourceProof(server) !== binding.sourceProof) throw conflict() + const state = await deps.read(server, authorize) + if (state.source !== binding.sourceProof) throw conflict() + await authorize(); return { server, state, authorize, revision: await deps.revision(binding.serverId, binding.revisionId, () => access(binding, signal, external)) } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + const value = ipsecRevisionRestoreActionSchema.parse(raw) + const binding = bindingSchema.parse({ ...value, operation, principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, sourceProof: "0".repeat(64), expectedProof: "0".repeat(64), expectedRevisionProof: "0".repeat(64), requestProof: fingerprint(value), commandCount: 0, receiptId: randomUUID(), proofRevision: 1 }) + await access(binding, context.signal) + const revision = await deps.revision(value.serverId, value.revisionId, () => access(binding, context.signal)) + binding.expectedRevisionProof = revision.proof + const { server } = await deps.router(value.serverId); binding.sourceProof = await deps.sourceProof(server) + const before = await source(binding, context.signal), plan = deps.plan(before.state, revision.payload) + binding.expectedProof = before.state.proof; binding.commandCount = plan.commands.length + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(value.serverId), serverId: value.serverId }, { kind: "ipsec_revision", id: value.revisionId, serverId: value.serverId }, ...plan.companions.map(ref => ({ ...ref, serverId: value.serverId }))] + for (const protection of before.state.protections as Array<{ object_ref: { kind: string; id: string } }>) objects.push({ kind: protection.object_ref.kind, id: protection.object_ref.id, serverId: value.serverId }) + if ((await source(binding, context.signal)).state.proof !== binding.expectedProof) throw conflict() + return { target: { scope: "servers", serverIds: [value.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: plan.commands.length + 1, before: { proof: before.state.proof }, + steps: [{ kind: "service", service: operation, input: { serverId: value.serverId, receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(binding)) } }], + warnings: [`Native-команд отката: ${plan.commands.length}; затем обязательная managed-only история rollback.`, "Ревизия, полный raw IPsec/NAT/pool dependency graph, источник, owner и текущий допуск перепроверяются перед каждой командой. PSK из ревизии не возвращается модели; скрытый PSK сохраняется только у существующей PSK identity с подтверждённым полем секрета.", "PKI не восстанавливается и не генерируется. Отсутствующие ключи, сертификаты, profile, proposal, policy group и NAT интерфейсы блокируют весь план до записи. Политика без существующего уникального managed соответствия отклоняется: снимок не содержит native template/security контекст. Отсутствующий mode-config также отклоняется: responder/address-prefix-length не записаны в снимке; существующие значения этих полей сохраняются.", "Recorded поля восстанавливаются с явными clears; не записанные native поля сохраняются. Изменение legacy peer.certificate или mode-config comment без подтверждённой native capability отклоняется заранее. Потеря ответа, collateral drift или missing ACK останавливают операцию без повтора и автоматического обратного изменения. Передача трафика не подтверждается."] } + } + const service: McpServiceHandler = { + current: async input => ({ proof: (await source(await request(input))).state.proof }), + execute: async input => { + const binding = await request(input), before = await source(binding) + if (before.state.proof !== binding.expectedProof || deps.plan(before.state, before.revision.payload).commands.length !== binding.commandCount) throw conflict() + const authorize = async () => { await deps.verifiedAccess(); await before.authorize() }; await authorize() + return deps.restore(before.server, binding.revisionId, binding.expectedProof, authorize, { expectedRevisionProof: binding.expectedRevisionProof, receiptId: binding.receiptId, checkpoint: async value => { await authorize(); await deps.checkpoint(binding.ownerId, binding.principal.connectionId, { ...value, inputProof: fingerprint(redact(input)) }) } }) + }, + verify: async (input, result) => { const binding = await request(input), after = await source(binding); return hasVerifiedIpsecRevisionRestore({ connection_id: binding.principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result }, binding, input, after.state.proof) }, + reconcile: async (input, external) => { + const binding = await request(input), latest = await source(binding, undefined, external); await latest.authorize() + const rows = await deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId]) + const verified = rows.length === 1 && hasVerifiedIpsecRevisionRestore(rows[0], binding, input, latest.state.proof) + if ((await source(binding, undefined, external)).state.proof !== latest.state.proof) throw conflict() + return { matchesOriginalState: latest.state.proof === binding.expectedProof, matchesDesiredState: verified, automaticReplay: false, forwardingVerified: false } + }, + } + return { prepare, service } +} +export function registerIpsecRevisionActions(register: typeof add) { + const adapter = createIpsecRevisionRestoreAdapter(); registerMcpService(operation, adapter.service) + register(operation, "mm_ipsec_revision_restore_plan", "Восстановить IPsec ревизию с точными native полями и полным dependency preflight", "network", "mm:network:write", "write", "servers", ipsecRevisionRestoreActionSchema, { version: 1, prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/ipsec-server-delete-acceptance.ts b/backend/src/modules/mcp/ipsec-server-delete-acceptance.ts new file mode 100644 index 00000000..32f063e1 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-server-delete-acceptance.ts @@ -0,0 +1,142 @@ +import { ipsecExternalPoolDependencyPaths } from "../../services/ipsec-pool-dependencies.js" +import assert from "node:assert/strict" +import { installLoopbackFixtureAgent } from "../../test/loopback-http-agent.js" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createIpsecRoutes } from "../../routes/ipsec.js" +import { getEnabledIpsecServerById } from "../../services/ipsec-live.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Owned PostgreSQL/loopback acceptance; actual RouterOS cascade and forwarding + * acceptance remain separate. The simulator enforces an explicit raw manifest. */ +export async function acceptIpsecServerDelete(actor: AuthUser) { + type Row = Record + const serverId = 186, originalQuery = pool.query.bind(pool), marker = "MikrotikManager:ipsec" + const base: Record = { + ...Object.fromEntries([...ipsecPeerDeletePaths,...ipsecExternalPoolDependencyPaths].map(path => [path, []])), + "/ip/ipsec/identity": [ + { ".id": "*i1", peer: "owned-peer", "auth-method": "digital-signature", certificate: "ipsec-server", "remote-certificate": "ipsec-user-alice", "mode-config": "ipsec-vpn", "policy-template-group": "mmapp-ipsec-vpn", comment: `${marker} user=Alice`, secret: "NEVER_MANAGED_PSK" }, + { ".id": "*i2", peer: "foreign-peer", "auth-method": "pre-shared-key", "mode-config": "foreign-mc", "policy-template-group": "foreign-group", comment: "foreign identity", secret: "NEVER_FOREIGN_PSK", opaque: "NEVER_FOREIGN_IDENTITY" }, + ], + "/ip/ipsec/peer": [{ ".id": "*p1", name: "owned-peer", "exchange-mode": "ike2", profile: "profile", comment: marker }, { ".id": "*p2", name: "foreign-peer", profile: "profile", opaque: "NEVER_FOREIGN_PEER" }], + "/ip/ipsec/policy": [{ ".id": "*s1", peer: "owned-peer", group: "mmapp-ipsec-vpn", proposal: "proposal", dynamic: "false", comment: marker }, { ".id": "*s2", peer: "foreign-peer", group: "foreign-group", proposal: "proposal", dynamic: "false", opaque: "NEVER_FOREIGN_POLICY" }], + "/ip/ipsec/mode-config": [{ ".id": "*m1", name: "ipsec-vpn", "address-pool": "owned-pool" }, { ".id": "*m2", name: "foreign-mc", "address-pool": "foreign-pool", opaque: "NEVER_FOREIGN_MC" }], + "/ip/pool": [{ ".id": "*l1", name: "owned-pool", ranges: "192.0.2.1-192.0.2.20", comment: marker }, { ".id": "*l2", name: "foreign-pool", ranges: "198.51.100.1-198.51.100.20", opaque: "NEVER_FOREIGN_POOL" }], + "/ip/firewall/nat": [{ ".id": "*n1", chain: "srcnat", action: "accept", comment: marker }, { ".id": "*n2", chain: "srcnat", action: "masquerade", opaque: "NEVER_FOREIGN_NAT" }], + "/ip/ipsec/policy/group": [{ ".id": "*g1", name: "mmapp-ipsec-vpn" }, { ".id": "*g2", name: "foreign-group", opaque: "NEVER_FOREIGN_GROUP" }], + "/ip/ipsec/profile": [{ ".id": "*f1", name: "profile", comment: marker, opaque: "NEVER_RETAIN_PROFILE" }], + "/ip/ipsec/proposal": [{ ".id": "*r1", name: "proposal", comment: marker, opaque: "NEVER_RETAIN_PROPOSAL" }], + "/certificate": [{ ".id": "*c1", name: "ipsec-ca", authority: "true", "key-usage": "key-cert-sign,crl-sign", crl: "false" }, { ".id": "*c2", name: "ipsec-server", ca: "ipsec-ca", issued: "true", "key-usage": "tls-server", "private-key": "true" }, { ".id": "*c3", name: "ipsec-user-alice", ca: "*c1", issued: "true", "key-usage": "tls-client", opaque: "NEVER_CLIENT_CERT" }, { ".id": "*c4", name: "foreign-ca", authority: "true" }, { ".id": "*c5", name: "foreign-cert", ca: "foreign-ca", opaque: "NEVER_FOREIGN_CERT" }], + } + const managed: Record = { "/ip/ipsec/identity": ["*i1"], "/ip/ipsec/peer": ["*p1"], "/ip/ipsec/policy": ["*s1"], "/ip/ipsec/mode-config": ["*m1"], "/ip/pool": ["*l1"], "/ip/firewall/nat": ["*n1"], "/ip/ipsec/policy/group": ["*g1"] } + let catalogs = structuredClone(base), mode = "normal", reads = 0, historyCalls = 0, connectionId: string | undefined, inserted = false + let afterAck: (() => Promise) | undefined, onRead: (() => Promise) | undefined, ticking = false, clock = 100000 + const commands: string[] = [] + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")); res.setHeader("content-type", "application/json") + try { + if (req.method === "GET") { reads++; if (ticking) for (const row of catalogs["/certificate"]) row["expires-after"] = `${clock--}s`; if (onRead) { const hook = onRead; onRead = undefined; await hook() }; res.end(JSON.stringify(catalogs[path] ?? [])); return } + const catalog = Object.keys(catalogs).sort((a, b) => b.length - a.length).find(value => path.startsWith(value + "/")) + if (req.method !== "DELETE" || !catalog) { res.statusCode = 405; res.end("{}"); return } + commands.push(path) + if (mode !== "ignored") { + const id = path.slice(catalog.length + 1), target = catalogs[catalog].find(row => row[".id"] === id); assert.ok(target) + const removed = new Set([id]) + if (catalog === "/certificate" && mode !== "cascade-root-only") { + const queue = [target] + for (let index = 0; index < queue.length; index++) for (const row of catalogs[catalog]) if (!removed.has(row[".id"]) && [queue[index].name, queue[index][".id"]].includes(row.ca)) { removed.add(row[".id"]); queue.push(row) } + if (mode === "cascade-extra" && id === "*c1") removed.add("*c5") + } + catalogs[catalog] = catalogs[catalog].filter(row => !removed.has(row[".id"])) + } + if (mode === "collateral") catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_COLLATERAL" + if (afterAck) { const hook = afterAck; afterAck = undefined; await hook() } + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}") + } catch { res.statusCode = 500; res.end('{"error":"NEVER_PROVIDER"}') } + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const closeFixtureAgent = installLoopbackFixtureAgent(router) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = (crl = false) => { mode = "normal"; catalogs = structuredClone(base); if (crl) catalogs["/certificate"][0].crl = "true"; afterAck = undefined; onRead = undefined; ticking = false } + const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const row = (await originalQuery("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) }; throw Error("IPsec server removal remains live; never replay") } + const desired = (before: Record, removeCertificates: boolean) => Object.fromEntries(Object.entries(before).map(([path, rows]) => [path, rows.filter(row => !(managed[path] ?? []).includes(row[".id"]) && !(removeCertificates && path === "/certificate" && ["*c1", "*c2", "*c3"].includes(row[".id"])))])) + try { + assert.equal(Number((await originalQuery("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0) + await originalQuery("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-server-delete','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]); inserted = true + await rest.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async input => { historyCalls++; assert.ok(await input.capture()); return { id: "fixture-history" } as never } }), { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec server delete", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.server.delete", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = (removeCertificates?: boolean) => invokeMcpTool(principal, "mm_ipsec_server_remove_plan", { serverId, ...(removeCertificates === undefined ? {} : { removeCertificates }) }) as Promise<{ planId: string; state: string }> + const apply = async (removeCertificates?: boolean) => { const plan = await prepare(removeCertificates); assert.equal(plan.state, "awaiting-approval"); assert.ok(!JSON.stringify(plan).includes("NEVER_")); await approvePlan(plan.planId, actor, true); const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key); return { plan, key, operation, ...await wait(operation.id) } } + const noReplay = async (result: Awaited>) => { + const before = commands.length + const persisted = (await originalQuery("SELECT state FROM mcp_operation_steps WHERE operation_id=$1", [result.operation.id])).rows + assert.ok(persisted.every(row => row.state !== "verified")) + await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, before) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] } + assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, false); assert.equal(commands.length, before) + } + const unapproved = await prepare(false); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, 0) + for (const scenario of [{ remove: false, crl: false }, { remove: true, crl: false }, { remove: true, crl: true }]) { + reset(scenario.crl); const original = structuredClone(catalogs), before: number = commands.length, result = await apply(scenario.remove) + assert.equal(result.state, "applied"); assert.deepEqual(catalogs, desired(original, scenario.remove)); assert.deepEqual(catalogs["/ip/ipsec/profile"], original["/ip/ipsec/profile"]); assert.deepEqual(catalogs["/ip/ipsec/proposal"], original["/ip/ipsec/proposal"]) + const expectedNative = ["/ip/ipsec/identity/*i1", "/ip/ipsec/policy/*s1", "/ip/ipsec/peer/*p1", "/ip/ipsec/mode-config/*m1", "/ip/pool/*l1", "/ip/firewall/nat/*n1", "/ip/ipsec/policy/group/*g1"] + assert.deepEqual(commands.slice(before, before + 7), expectedNative) + if (scenario.crl) assert.deepEqual(commands.slice(before + 7), ["/certificate/*c1"]) + else if (!scenario.remove) assert.equal(commands.length, before + 7) + else assert.deepEqual(new Set(commands.slice(before + 7)), new Set(["/certificate/*c1", "/certificate/*c2", "/certificate/*c3"])) + const receipt: Record = (await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [result.operation.id])).rows[0].result; assert.equal(receipt.historyRecorded, true); assert.ok(!JSON.stringify(receipt).includes("NEVER_")) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { results: { matchesDesiredState: boolean }[] }; assert.equal(reconciliation.results[0].matchesDesiredState, true) + const successfulCommands = commands.slice(before); reset(scenario.crl); const restBefore: number = commands.length, historyBefore = historyCalls + const response = await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}?removeCertificates=${scenario.remove}` }); assert.equal(response.statusCode, 200, response.body); assert.deepEqual(response.json(), { ok: true }); assert.deepEqual(commands.slice(restBefore), successfulCommands); assert.deepEqual(catalogs, desired(original, scenario.remove)); assert.equal(historyCalls, historyBefore + 1) + } + reset(); ticking = true + catalogs["/ip/ipsec/policy"].push({ ".id": "*runtime", dynamic: "true", peer: "vanished-runtime-peer", group: "vanished-runtime-group", comment: marker }) + const withCountdown = await apply(false) + assert.equal(withCountdown.state, "applied", "documented expiry countdown must not invalidate the full manifest") + assert.equal(catalogs["/certificate"].length, base["/certificate"].length) + assert.ok(clock < 100000); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}?removeCertificates=false` })).statusCode, 200) + reset(); let result = await apply(); assert.equal(result.state, "applied"); assert.equal(catalogs["/certificate"].length, 2) + let before = commands.length; result = await apply(); assert.equal(result.state, "applied"); assert.equal(commands.length, before); assert.equal((await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [result.operation.id])).rows[0].result.historyRecorded, true) + const noopHistory = historyCalls; assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}` })).statusCode, 200); assert.equal(commands.length, before); assert.equal(historyCalls, noopHistory + 1) + const denied = [ + ...ipsecExternalPoolDependencyPaths.flatMap(path=>["owned-pool","*L1","*00l1"].map(ref=>()=>{catalogs[path].push({".id":"*consumer",name:"foreign",disabled:"true",...(path.startsWith("/ppp/")?{"remote-address":ref}:{"address-pool":ref})})})), + () => { catalogs["/ip/ipsec/mode-config"][1]["address-pool"] = "owned-pool" }, + () => { catalogs["/ip/ipsec/mode-config"][1]["address-pool"] = "*l1" }, + () => { catalogs["/ip/ipsec/peer"][1].certificate = "ipsec-server" }, + () => { catalogs["/ip/ipsec/peer"][1].certificate = "*c2" }, + () => { catalogs["/certificate"][4].ca = "*c1" }, + () => { catalogs["/ip/ipsec/peer"].push({ ".id": "*p3", name: "owned-peer" }) }, + () => { catalogs["/ip/pool"].push({ ".id": "*l1", name: "duplicate-id" }) }, + ] + for (const change of denied) { reset(); change(); before = commands.length; await assert.rejects(prepare(true)); assert.equal(commands.length, before); const response = await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}` }); assert.ok([409, 502].includes(response.statusCode), response.body); assert.equal(commands.length, before) } + for(const path of ipsecExternalPoolDependencyPaths){reset();const stalePool=await prepare(true);await approvePlan(stalePool.planId,actor,true);catalogs[path].push({".id":"*consumer",opaque:"NEVER_LATE_POOL_CONSUMER"});before=commands.length;assert.notEqual((await wait((await applyPlan(principal,stalePool.planId,randomUUID())).id)).state,"applied");assert.equal(commands.length,before)} + for (const fault of ["ignored", "lost", "collateral"]) { reset(); mode = fault; before = commands.length; result = await apply(true); assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1); await noReplay(result) } + for (const fault of ["cascade-root-only", "cascade-extra"]) { reset(true); mode = fault; before = commands.length; result = await apply(true); assert.equal(result.state, "partial"); assert.equal(commands.length, before + 8); await noReplay(result) } + reset(); before = commands.length; afterAck = async () => { catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_LATE_OPAQUE" }; result = await apply(true); assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1); await noReplay(result) + reset(); const stale = await prepare(true); await approvePlan(stale.planId, actor, true); catalogs["/ip/ipsec/proposal"][0].opaque = "NEVER_STALE_PROPOSAL"; before = commands.length; assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, before) + reset(true); const protection = randomUUID(); await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "certificate", id: "*c3", serverId, protection: "deny" }), actor.id]) + try { before = commands.length; await assert.rejects(prepare(true)); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/server/${serverId}` })).statusCode, 409); assert.equal(commands.length, before) } finally { await originalQuery("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + reset(); onRead = () => originalQuery("UPDATE servers SET password='changed-source' WHERE id=$1", [serverId]); before = commands.length; await assert.rejects(prepare(true)); assert.equal(commands.length, before); await originalQuery("UPDATE servers SET password='NEVER_ENDPOINT' WHERE id=$1", [serverId]) + const readBefore = reads; await setConnectionStatus(access.connection.id, "paused", actor); await assert.rejects(prepare(true)); assert.equal(reads, readBefore) + for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) { + const rows = table === "mcp_operation_steps" ? await originalQuery("SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1", [connectionId]) : await originalQuery(`SELECT * FROM ${table} WHERE connection_id=$1`, [connectionId]); assert.ok(!JSON.stringify(rows.rows).includes("NEVER_"), `${table} leaked private fixture data`) + } + console.log("✓ IPsec server removal SQL/loopback: full managed manifest, REST parity/defaults/noop history, raw foreign/profile/proposal preservation, closed CRL CA cascade, foreign/protected dependencies and ambiguity denied, per-step drift/fault stop without replay; native cascade/forwarding gate remains open") + } finally { + closeFixtureAgent() + pool.query = originalQuery as typeof pool.query + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await rest.close(); if (inserted) await originalQuery("DELETE FROM servers WHERE id=$1", [serverId]); router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-server-delete-actions.test.ts b/backend/src/modules/mcp/ipsec-server-delete-actions.test.ts new file mode 100644 index 00000000..7c3d98f5 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-server-delete-actions.test.ts @@ -0,0 +1,104 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import type { ActionContext } from "./action-types.js" +import type { ConnectionRow } from "./store.js" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import type { IpsecPeerDeleteState } from "../../services/ipsec-peer-delete.js" +import type { IpsecServerDeleteCommand } from "../../services/ipsec-server-delete-model.js" +import { createIpsecServerDeleteAdapter, ipsecServerDeleteActionSchema } from "./ipsec-server-delete-actions.js" +import { fingerprint, redact } from "./security.js" + +const operation = "network.ipsec.server.delete", sourceProof = "a".repeat(64), beforeProof = "b".repeat(64), afterProof = "c".repeat(64) +const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } +const connection = { id: principal.connectionId, owner_id: "fixture-owner", owner_permissions: ["mm:network:write"], generation: "fixture-generation", owner_valid_until: new Date(Date.now() + 600000), policy: { mode: "autonomous", status: "active", expiresAt: principal.credentialExpiresAt, permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: [operation], allowGlobal: false } } } as ConnectionRow +const context = { principal, connection } as ActionContext +assert.deepEqual(ipsecServerDeleteActionSchema.parse({ serverId: 1 }), { serverId: 1, removeCertificates: true }) +for (const raw of [{ serverId: 1, removeCertificates: "true" }, { serverId: 1, psk: "NEVER_SECRET" }, { serverId: 0 }, { serverId: 1, force: true }]) assert.equal(ipsecServerDeleteActionSchema.safeParse(raw).success, false) + +const originalQuery = pool.query, originalConnect = pool.connect +const forbidden = () => { throw Error("Unexpected real SQL/native/network access") } +pool.query = forbidden as typeof pool.query; pool.connect = forbidden as typeof pool.connect +try { + let current: ConnectionRow, state: IpsecPeerDeleteState, endpoint = sourceProof, reads = 0, deletes = 0, histories = 0, historyFails = false, noCommands = false, lateAuthority: (() => void) | undefined + let storedRows: Record[] = [], unexpectedManifest = false + const sealed = new Map() + const reset = () => { + current = structuredClone(connection); endpoint = sourceProof; historyFails = false; noCommands = false; lateAuthority = undefined; unexpectedManifest = false; storedRows = [] + state = { source: sourceProof, proof: beforeProof, catalogs: {}, protections: [{ object_ref: { kind: "ipsec_identity", id: "*protected" } }] } + } + reset() + const commands = (removeCertificates: boolean): IpsecServerDeleteCommand[] => noCommands ? [] : removeCertificates ? [{ path: "/certificate", id: "*ca", cascadeIds: ["*child"] }] : [{ path: "/ip/ipsec/identity", id: "*i1" }] + const objects = (removeCertificates: boolean) => noCommands ? [] : removeCertificates ? [{ kind: "certificate", id: "*ca" }, { kind: "certificate", id: "*child" }] : [{ kind: "ipsec_identity", id: "*i1" }] + const adapter = createIpsecServerDeleteAdapter({ + connection: async () => current, + router: async () => ({ server: { id: 1 } as ServerRow, client: {} as never }), + sourceProof: async () => endpoint, + read: async (_server, authorize) => { await authorize?.(); reads++; await authorize?.(); return structuredClone(state) }, + seal: async value => { const key = `sealed:${randomUUID()}`; sealed.set(key, value); return key }, + open: async value => { const found = sealed.get(value); if (!found) throw Error("invalid ciphertext"); return found }, + manifestProof: async value => fingerprint({ privateTestKey: "fixed-pure-fixture", value }), + query: async () => storedRows, verifiedAccess: async () => {}, checkpoint: async () => {}, + plan: (_state, removeCertificates = true) => ({ commands: unexpectedManifest ? [{ path: "/certificate", id: "*different" }] : commands(removeCertificates), objects: objects(removeCertificates), companions: objects(removeCertificates), removeCertificates }), + stages: async (before, manifest) => [before.proof, ...manifest.map(() => afterProof)], + remove: (async (_server: unknown, removeCertificates: boolean, expected: string, authorize: () => Promise, options: { receiptId: string; checkpoint: (value: unknown) => Promise }) => { + assert.equal(expected, beforeProof); await authorize(); lateAuthority?.(); await authorize() + const manifest = commands(removeCertificates), affectedObjects = manifest.reduce((sum, cmd) => sum + 1 + (cmd.cascadeIds?.length ?? 0), 0) + deletes += manifest.length; histories++; state.proof = manifest.length ? afterProof : beforeProof + const value = { ok: true, verified: true, receiptId: options.receiptId, removeCertificates, commandCount: manifest.length, completedCommands: manifest.length, confirmedWrites: manifest.length, affectedObjects, afterProof: state.proof, historyRecorded: !historyFails, revisionRecorded: !historyFails, automaticReplay: false, forwardingVerified: false } + await options.checkpoint({ receiptId: options.receiptId, commandCount: manifest.length, completedCommands: manifest.length, afterProof: state.proof }); return value + }) as never, + }) + const prepare = async (removeCertificates = true) => { + const evidence = await adapter.prepare({ serverId: 1, removeCertificates }, context) + assert.equal(evidence.requiresHuman, true) + assert.equal(evidence.mutationCost, noCommands ? 1 : removeCertificates ? 3 : 2) + assert.ok(evidence.objects.some(ref => ref.id === "*protected")) + if (!noCommands && removeCertificates) assert.ok(evidence.objects.some(ref => ref.id === "*child"), "CA cascade child explicitly protected") + const step = evidence.steps[0]!; if (step.kind !== "service") throw Error("Expected service") + assert.deepEqual(Object.keys(step.input).sort(), ["bindingCiphertext", "receiptId", "serverId"]) + assert.equal(JSON.parse(sealed.get(String(step.input.bindingCiphertext))!).proofRevision,2,"New plans require the v2 dependency proof") + return { evidence, input: step.input } + } + let plan = await prepare() + assert.ok(plan.evidence.warnings.some(value => value.includes("*ca: *child"))) + assert.ok(plan.evidence.warnings.some(value => value.includes("Profiles и proposals сохраняются"))) + assert.deepEqual(await adapter.service.current(plan.input), { proof: beforeProof }) + let receipt = await adapter.service.execute(plan.input) + assert.equal(deletes, 1); assert.equal(histories, 1); assert.equal(await adapter.service.verify!(plan.input, receipt), true) + storedRows = [{ connection_id: principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input: plan.input }), result: receipt }] + assert.equal((await adapter.service.reconcile!(plan.input, async () => {})).matchesDesiredState, true) + for (const field of ["ok", "verified", "receiptId", "removeCertificates", "commandCount", "completedCommands", "confirmedWrites", "affectedObjects", "historyRecorded", "revisionRecorded", "afterProof", "forwardingVerified", "automaticReplay"]) { + const saved = structuredClone(storedRows[0]!) + ;(storedRows[0]!.result as Record)[field] = "tampered" + assert.equal((await adapter.service.reconcile!(plan.input, async () => {})).matchesDesiredState, false, field) + storedRows = [saved] + } + storedRows[0]!.state = "applied"; assert.equal((await adapter.service.reconcile!(plan.input, async () => {})).matchesDesiredState, false) + reset(); plan = await prepare(false); receipt = await adapter.service.execute(plan.input); assert.equal(await adapter.service.verify!(plan.input, receipt), true) + reset(); noCommands = true; plan = await prepare(); const beforeEmpty = deletes; receipt = await adapter.service.execute(plan.input) + assert.equal(deletes, beforeEmpty); assert.equal(await adapter.service.verify!(plan.input, receipt), true, "empty manifest still requires history") + reset(); plan = await prepare(); historyFails = true; receipt = await adapter.service.execute(plan.input); assert.equal(await adapter.service.verify!(plan.input, receipt), false) + for (const mutate of [() => { endpoint = "d".repeat(64) }, () => { current.policy.permissions = [] }, () => { current.policy.scope.operationIds = [] }, () => { current.policy.scope.serverIds = [] }, () => { current.generation = "rotated-generation" }]) { + reset(); plan = await prepare(); const before = reads; mutate(); await assert.rejects(adapter.service.current(plan.input)); assert.equal(reads, before, "authority/source drift rejects before native GET") + } + reset(); plan = await prepare(); unexpectedManifest = true; const beforeManifest = deletes; await assert.rejects(adapter.service.execute(plan.input)); assert.equal(deletes, beforeManifest) + reset(); plan = await prepare(); lateAuthority = () => { current.policy.permissions = [] }; const beforeLate = deletes; await assert.rejects(adapter.service.execute(plan.input)); assert.equal(deletes, beforeLate) + reset(); plan = await prepare(); const cipher = String(plan.input.bindingCiphertext), binding = JSON.parse(sealed.get(cipher)!) + for (const mutate of [ + (value: Record) => { value.proofRevision = 1 }, + (value: Record) => { value.affectedObjects = 1 }, + (value: Record) => { value.commandCount = 2 }, + (value: Record) => { value.stageProofs = [beforeProof] }, + (value: Record) => { value.manifestProof = "f".repeat(64) }, + ]) { + const changed = structuredClone(binding); mutate(changed); sealed.set(cipher, JSON.stringify(changed)); const before = reads + await assert.rejects(adapter.service.current(plan.input)); assert.equal(reads, before) + } + sealed.set(cipher, JSON.stringify(binding)) + await assert.rejects(adapter.service.current({ ...plan.input, serverId: 2 })); await assert.rejects(adapter.service.current({ ...plan.input, receiptId: randomUUID() })) + const cancelled = new AbortController(); cancelled.abort(); await assert.rejects(adapter.prepare({ serverId: 1 }, { ...context, signal: cancelled.signal })) + reset(); plan = await prepare(); let checks = 0 + await adapter.service.reconcile!(plan.input, async () => { checks++; if (checks === 2) throw Error("external authorization revoked") }).then(() => assert.fail("external revocation must fail"), () => {}) + console.log("IPsec server MCP adapter: exact sealed HMAC manifest/stages, CA cascade budget/protections, human-only removal, source/access-before-GET, history-only empty manifest, mandatory-history receipts and verified-only reconciliation") +} finally { pool.query = originalQuery; pool.connect = originalConnect } diff --git a/backend/src/modules/mcp/ipsec-server-delete-actions.ts b/backend/src/modules/mcp/ipsec-server-delete-actions.ts new file mode 100644 index 00000000..4634ad72 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-server-delete-actions.ts @@ -0,0 +1,125 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { planIpsecServerDelete } from "../../services/ipsec-server-delete-model.js" +import { deleteIpsecServerGuarded, readIpsecServerDeleteState, ipsecServerDeleteStageProofs } from "../../services/ipsec-server-delete.js" +import { routerClient } from "./resources.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, privateSecretFingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" +import type { McpServiceHandler } from "./service-registry.js" + +const operation = "network.ipsec.server.delete", hash = z.string().regex(/^[a-f0-9]{64}$/) +export const ipsecServerDeleteActionSchema = z.object({ serverId: z.number().int().positive().safe(), removeCertificates: z.boolean().default(true) }).strict() +const objectRef = z.object({ kind: z.string().min(1).max(100), id: z.string().min(1).max(200) }).strict() +const nativeId = z.string().regex(/^\*[a-z0-9]+$/i).max(100) +const command = z.object({ path: z.enum(["/ip/ipsec/identity", "/ip/ipsec/peer", "/ip/ipsec/policy", "/ip/ipsec/mode-config", "/ip/pool", "/ip/firewall/nat", "/ip/ipsec/policy/group", "/certificate"]), id: nativeId, cascadeIds: z.array(nativeId).max(2000).optional() }).strict() +const bindingSchema = ipsecServerDeleteActionSchema.extend({ + operation: z.literal(operation), principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: hash, policyProof: hash, generation: z.string().min(1), sourceProof: hash, expectedProof: hash, + requestProof: hash, manifestProof: hash, commands: z.array(command).max(1000), objects: z.array(objectRef).max(3000), stageProofs: z.array(hash).min(1).max(1001), + commandCount: z.number().int().min(0).max(1000), affectedObjects: z.number().int().min(0).max(3000), receiptId: z.string().uuid(), proofRevision: z.literal(2), +}).strict() +type Binding = z.infer +const inputSchema = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(2097152) }).strict() +const production = { + connection: effectiveMcpConnection, router: routerClient, read: readIpsecServerDeleteState, sourceProof: ipsecPeerDeleteSourceProof, + plan: planIpsecServerDelete, stages: ipsecServerDeleteStageProofs, remove: deleteIpsecServerGuarded, seal: encryptSecret, open: decryptSecret, verifiedAccess: assertVerifiedConfigurationAccess, + manifestProof: async (value: unknown) => privateSecretFingerprint(fingerprint(value), "ipsec-server-delete-manifest-v2"), + query: async (sql: string, values: unknown[]) => (await pool.query(sql, values)).rows as Record[], + checkpoint: async (ownerId: string, connectionId: string, value: unknown) => audit(pool, ownerId, "ipsec.server.delete.checkpoint", connectionId, value), +} +const conflict = () => new McpError("STATE_CONFLICT", "Манифест удаления IPsec, источник или допуск изменились", 409) +const manifest = (value: Pick) => ({ commands: value.commands, objects: value.objects, removeCertificates: value.removeCertificates }) + +export function hasVerifiedIpsecServerDelete(candidate: unknown, binding: Binding, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | undefined + return row.connection_id === binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && + result.ok === true && result.verified === true && result.receiptId === binding.receiptId && result.removeCertificates === binding.removeCertificates && result.commandCount === binding.commandCount && result.completedCommands === binding.commandCount && result.confirmedWrites === binding.commandCount && result.affectedObjects === binding.affectedObjects && + result.historyRecorded === true && result.revisionRecorded === true && result.afterProof === proof && proof === binding.stageProofs.at(-1) && result.forwardingVerified === false && result.automaticReplay === false +} + +/** Whole-server removal has one server-sealed immutable manifest and no replay. + * All native commands, indirect CA removals and history consume the budget. */ +export function createIpsecServerDeleteAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const authorize = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + const current = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || current.owner_valid_until.getTime() <= Date.now() || Date.parse(current.policy.expiresAt) <= Date.now() || !current.policy.scope.operationIds.includes(operation) || !current.policy.scope.serverIds.includes(binding.serverId) || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска удаления сервера IPsec", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof || fingerprint({ serverId: binding.serverId, removeCertificates: binding.removeCertificates }) !== binding.requestProof) throw conflict() + signal?.throwIfAborted() + } + const request = async (raw: Record) => { + const input = inputSchema.parse(raw), binding = bindingSchema.parse(JSON.parse(await deps.open(input.bindingCiphertext))) + if (input.serverId !== binding.serverId || input.receiptId !== binding.receiptId || binding.commandCount !== binding.commands.length || binding.affectedObjects !== binding.commands.reduce((sum, cmd) => sum + 1 + (cmd.cascadeIds?.length ?? 0), 0) || binding.stageProofs.length !== binding.commandCount + 1 || binding.stageProofs[0] !== binding.expectedProof || await deps.manifestProof(manifest(binding)) !== binding.manifestProof) throw conflict() + return binding + } + const source = async (binding: Binding, signal?: AbortSignal, external?: () => Promise) => { + const check = () => authorize(binding, signal, external) + await check() + const { server } = await deps.router(binding.serverId) + if (await deps.sourceProof(server) !== binding.sourceProof) throw conflict() + const state = await deps.read(server, check) + if (state.source !== binding.sourceProof) throw conflict() + await check(); return { server, state, check } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + const value = ipsecServerDeleteActionSchema.parse(raw) + const binding: Binding = bindingSchema.parse({ ...value, operation, principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, sourceProof: "0".repeat(64), expectedProof: "0".repeat(64), requestProof: fingerprint(value), manifestProof: "0".repeat(64), commands: [], objects: [], stageProofs: ["0".repeat(64)], commandCount: 0, affectedObjects: 0, receiptId: randomUUID(), proofRevision: 2 }) + await authorize(binding, context.signal) + const { server } = await deps.router(value.serverId) + binding.sourceProof = await deps.sourceProof(server) + const before = await source(binding, context.signal), plan = deps.plan(before.state, value.removeCertificates) + binding.expectedProof = before.state.proof; binding.commands = plan.commands; binding.objects = plan.objects + binding.commandCount = plan.commands.length; binding.affectedObjects = plan.commands.reduce((sum, cmd) => sum + 1 + (cmd.cascadeIds?.length ?? 0), 0) + binding.stageProofs = await deps.stages(before.state, plan.commands) + if (binding.stageProofs.length !== binding.commandCount + 1 || binding.stageProofs[0] !== binding.expectedProof) throw conflict() + binding.manifestProof = await deps.manifestProof(manifest(binding)) + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(value.serverId), serverId: value.serverId }, { kind: "ipsec_server", id: String(value.serverId), serverId: value.serverId }, ...plan.objects.map(ref => ({ ...ref, serverId: value.serverId })), ...plan.companions.map(ref => ({ ...ref, serverId: value.serverId }))] + for (const protection of before.state.protections as Array<{ object_ref: { kind: string; id: string } }>) objects.push({ kind: protection.object_ref.kind, id: protection.object_ref.id, serverId: value.serverId }) + if ((await source(binding, context.signal)).state.proof !== binding.expectedProof) throw conflict() + const summary = [...new Set(plan.objects.map(ref => ref.kind))].map(kind => `${kind}: ${plan.objects.filter(ref => ref.kind === kind).length}`).join(", ") || "нет управляемых объектов" + const cascades = plan.commands.filter(cmd => cmd.cascadeIds?.length).map(cmd => `${cmd.id}: ${cmd.cascadeIds!.join(", ")}`).join("; ") + return { target: { scope: "servers", serverIds: [value.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: binding.affectedObjects + 1, before: { proof: before.state.proof }, + steps: [{ kind: "service", service: operation, input: { serverId: value.serverId, receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(binding)) } }], + warnings: [`Необратимое удаление управляемой конфигурации IPsec: ${summary}. Native DELETE: ${binding.commandCount}; затронутых объектов: ${binding.affectedObjects}; затем обязательная managed-only история.`, `Сертификаты: ${value.removeCertificates ? "удаляются по точному манифесту" : "сохраняются"}. CA cascade: ${cascades || "нет"}. Profiles и proposals сохраняются.`, "Каждый объект, включая косвенно удаляемые сертификаты, входит в бюджет и проверку защит. После каждой команды требуется точный whole-state read-back; пустой манифест записывает только историю.", "При потере ответа или изменении сторонней конфигурации операция останавливается без повтора и автоматического отката. Сетевая передача и поведение настоящего CHR отдельно не подтверждаются. Native IPv4 pool graph не доказывает внешние динамические RADIUS Framed-Pool assignments."] } + } + const service: McpServiceHandler = { + current: async input => ({ proof: (await source(await request(input))).state.proof }), + execute: async input => { + const binding = await request(input), before = await source(binding) + if (before.state.proof !== binding.expectedProof) throw conflict() + const plan = deps.plan(before.state, binding.removeCertificates) + if (await deps.manifestProof({ commands: plan.commands, objects: plan.objects, removeCertificates: binding.removeCertificates }) !== binding.manifestProof || fingerprint(await deps.stages(before.state, plan.commands)) !== fingerprint(binding.stageProofs)) throw conflict() + const check = async () => { await deps.verifiedAccess(); await before.check() } + await check() + return deps.remove(before.server, binding.removeCertificates, binding.expectedProof, check, { receiptId: binding.receiptId, checkpoint: async value => { await check(); await deps.checkpoint(binding.ownerId, binding.principal.connectionId, { ...value, inputProof: fingerprint(redact(input)), manifestProof: binding.manifestProof }) } }) + }, + verify: async (input, result) => { + const binding = await request(input), after = await source(binding) + return hasVerifiedIpsecServerDelete({ connection_id: binding.principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result }, binding, input, after.state.proof) + }, + reconcile: async (input, external) => { + const binding = await request(input), latest = await source(binding, undefined, external) + await latest.check() + const rows = await deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId]) + const verified = rows.length === 1 && hasVerifiedIpsecServerDelete(rows[0], binding, input, latest.state.proof) + if ((await source(binding, undefined, external)).state.proof !== latest.state.proof) throw conflict() + return { matchesOriginalState: latest.state.proof === binding.expectedProof, matchesDesiredState: verified, automaticReplay: false, forwardingVerified: false } + }, + } + return { prepare, service } +} + +export function registerIpsecServerDeleteActions(register: typeof add) { + const adapter = createIpsecServerDeleteAdapter() + registerMcpService(operation, adapter.service) + register(operation, "mm_ipsec_server_remove_plan", "Удалить управляемый сервер IPsec по полному манифесту", "network", "mm:network:write", "write", "servers", ipsecServerDeleteActionSchema, { version: 2, prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/ipsec-server-init-acceptance.ts b/backend/src/modules/mcp/ipsec-server-init-acceptance.ts new file mode 100644 index 00000000..15aae965 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-server-init-acceptance.ts @@ -0,0 +1,148 @@ +import assert from "node:assert/strict" +import { installLoopbackFixtureAgent } from "../../test/loopback-http-agent.js" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createIpsecRoutes } from "../../routes/ipsec.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { IPSEC_CA_CERT, IPSEC_COMMON_NAME, IPSEC_POLICY_GROUP, IPSEC_SERVER_CERT, ipsecManagedComment } from "../../services/ipsec-config.js" +import { ipsecExternalPoolDependencyPaths } from "../../services/ipsec-pool-dependencies.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Opt-in serial disposable SQL + loopback RouterOS acceptance; no live router. */ +export async function acceptIpsecServerInit(actor: AuthUser) { + type Row = Record + const serverId = 208, query = pool.query.bind(pool) + const base: Record = { ...Object.fromEntries([...ipsecPeerDeletePaths, ...ipsecExternalPoolDependencyPaths].map(path => [path, []])), + "/ip/ipsec/profile": [{ ".id": "*foreign", name: "foreign-profile", opaque: "NEVER_FOREIGN_PROFILE" }], + "/ip/route": [{ ".id": "*route", "dst-address": "0.0.0.0/0", "immediate-gw": "192.0.2.1%ether1", active: "true", opaque: "NEVER_ROUTE" }], + "/interface": [{ ".id": "*interface", name: "ether1", disabled: "false", opaque: "NEVER_INTERFACE" }], + } + let catalogs = structuredClone(base), serial = 0, mode = "normal", connectionId: string | undefined, afterAck: (() => Promise) | undefined + const commands: Array<{ method: string; path: string; fields: Row }> = [] + const originalGet = MikrotikClient.prototype.get, originalPut = MikrotikClient.prototype.put + const transportFailures: Array<{ method: string; path: string; code: string; causeCode: string; name: string; status?: number; timeout: boolean }> = [] + const requestCounts: Record = {} + const knownCodes = new Set(["EADDRNOTAVAIL", "ENOBUFS", "EACCES", "ECONNRESET", "ECONNABORTED", "ECONNREFUSED", "ETIMEDOUT", "EHOSTUNREACH", "ENETUNREACH", "EPIPE", "EMFILE", "ENFILE", "EADDRINUSE", "EAI_AGAIN", "ENOTFOUND", "ABORT_ERR"]) + const knownPaths = new Set([...ipsecPeerDeletePaths, ...ipsecExternalPoolDependencyPaths, "/ip/route", "/interface"]) + const observeFailure = (method: string, path: string, error: unknown) => { + const value = error as { code?: unknown; name?: unknown; statusCode?: unknown; message?: unknown; cause?: { code?: unknown } } | null + transportFailures.push({ method, path: knownPaths.has(path) ? path : "unknown-catalog", code: typeof value?.code === "string" && knownCodes.has(value.code) ? value.code : "UNCLASSIFIED", causeCode: typeof value?.cause?.code === "string" && knownCodes.has(value.cause.code) ? value.cause.code : "UNCLASSIFIED", name: typeof value?.name === "string" && ["Error", "AbortError", "TimeoutError", "MikrotikError", "SyntaxError", "RangeError"].includes(value.name) ? value.name : "unknown-error", ...(typeof value?.statusCode === "number" && Number.isInteger(value.statusCode) && value.statusCode >= 0 && value.statusCode <= 599 ? { status: value.statusCode } : {}), timeout: typeof value?.message === "string" && /timed out after|timeout|aborted due to timeout/i.test(value.message) }) + } + // Diagnostic wrappers retain the actual production transport and rethrow the + // exact error. Never serialize provider message/body, credentials or fields. + MikrotikClient.prototype.get = async function(path: string, timeoutMs?: number, signal?: AbortSignal, maxResponseBytes?: number): Promise { + requestCounts[`GET ${knownPaths.has(path) ? path : "unknown-catalog"}`] = (requestCounts[`GET ${knownPaths.has(path) ? path : "unknown-catalog"}`] ?? 0) + 1 + try { return await originalGet.call(this, path, timeoutMs, signal, maxResponseBytes) as T } catch (error) { observeFailure("GET", path, error); throw error } + } + MikrotikClient.prototype.put = async function(path: string, fields: Row, timeoutMs?: number, beforeDispatch?: () => Promise): Promise { + try { return await originalPut.call(this, path, fields, timeoutMs, beforeDispatch) as T } catch (error) { observeFailure("PUT", path, error); throw error } + } + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")); res.setHeader("content-type", "application/json") + try { + 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 : {} + commands.push({ method: req.method!, path, fields }) + let result: Row = {} + if (mode !== "ignored") { + if (path === "/certificate/sign") { + const row = catalogs["/certificate"].find(row => row.name === fields.name); assert(row) + Object.assign(row, { ...(fields.ca ? { ca: fields.ca } : {}), fingerprint: "synthetic-fingerprint", "invalid-before": "2026-01-01", "invalid-after": "2030-01-01", "private-key": "true", "serial-number": "synthetic" }) + } else { + const catalog = Object.hasOwn(catalogs, path) ? path : Object.keys(catalogs).sort((a, b) => b.length - a.length).find(value => path.startsWith(value + "/")); assert(catalog) + if (req.method === "PUT") { result = { ".id": `*new${++serial}`, ...fields, "native-default": "NEVER_NATIVE_DEFAULT" }; catalogs[catalog].push(result) } + else if (req.method === "PATCH") { const row = catalogs[catalog].find(row => row[".id"] === path.slice(catalog.length + 1)); assert(row); Object.assign(row, fields) } + else throw Error("unexpected mutation") + } + } + if (mode === "collateral") catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_COLLATERAL" + if (afterAck) { const run = afterAck; afterAck = undefined; await run() } + if (mode === "lost") { req.socket.destroy(); return } + if (mode === "missingAck" && req.method === "PUT") result = {} + res.end(JSON.stringify(result)) + } catch { res.statusCode = 500; res.end('{"error":"NEVER_PROVIDER"}') } + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const closeFixtureAgent = installLoopbackFixtureAgent(router) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = () => { catalogs = structuredClone(base); serial = 0; mode = "normal"; afterAck = undefined; transportFailures.length = 0 } + const hidden = (value: unknown) => assert(!JSON.stringify(value).includes("NEVER_"), "private fixture data leaked") + const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const row = (await query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) }; throw Error("Init remains active; never replay") } + try { + assert.equal(Number((await query("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0) + await query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-init','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]) + await rest.register(createIpsecRoutes(), { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec init", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.server.init", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const request = { serverEndpoint: "vpn.example.test", poolCidr: "10.77.0.0/24", dns: "192.0.2.53", caDaysValid: 3650, serverDaysValid: 900, clientDaysValid: 180, createNatRule: true } + const prepare = (body: Record = request) => invokeMcpTool(principal, "mm_ipsec_server_init_plan", { serverId, request: body }) as Promise<{ planId: string; state: string }> + const apply = async (body: Record = request) => { const plan = await prepare(body); hidden(plan); assert.equal(plan.state, "awaiting-approval"); await approvePlan(plan.planId, actor, true); const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key); return { plan, key, operation, ...await wait(operation.id) } } + const receipt = async (id: string) => (await query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [id])).rows[0].result + const normalize = (entries: typeof commands) => entries.map(row => ({ ...row, path: row.path.replace(/\*new\d+/g, "*created") })) + for (const nat of [false, true]) { + reset(); const start = commands.length, body = { ...request, createNatRule: nat, serverEndpoint: nat ? request.serverEndpoint : "2001:0DB8:0:0:0:0:0:10" }, result = await apply(body) + assert.equal(result.state, "applied", JSON.stringify({ result, nativeDispatches: commands.slice(start).map(command => ({ method: command.method, path: command.path, fieldNames: Object.keys(command.fields) })), rowCounts: Object.fromEntries(Object.entries(catalogs).map(([path, rows]) => [path, rows.length])), storedReceipt: await receipt(result.operation.id) })); hidden(result) + const accepted = normalize(commands.slice(start)), saved = await receipt(result.operation.id); hidden(saved); assert.equal(saved.historyRecorded, true) + assert.equal(saved.commandCount, nat ? 14 : 13) + assert.equal(commands.slice(start).find(row => row.method === "PUT" && row.path === "/certificate" && row.fields.name === IPSEC_SERVER_CERT)?.fields["subject-alt-name"], nat ? "DNS:vpn.example.test" : "IP:2001:db8::10") + assert.equal(commands.slice(start).filter(row => row.path === "/certificate/sign").length, 2) + assert(!commands.slice(start).some(row => /export|^\/file/.test(row.path))) + for (const [path, rows] of Object.entries(base)) for (const row of rows) assert.deepEqual(catalogs[path].find(current => current[".id"] === row[".id"]), row) + const rerunStart = commands.length, rerun = await apply(body); assert.equal(rerun.state, "applied", JSON.stringify(rerun)); assert(!commands.slice(rerunStart).some(row => row.path === "/certificate/sign" || row.method === "PUT")) + reset(); const restStart = commands.length + const response = await rest.inject({ method: "POST", url: "/api/ipsec/server/init", payload: { ...body, serverId } }); assert.equal(response.statusCode, 201, JSON.stringify({ response: response.json(), transportFailures, requestCounts, nativeDispatches: commands.slice(restStart).map(row => ({ method: row.method, path: row.path, fieldNames: Object.keys(row.fields) })) })) + assert.deepEqual(normalize(commands.slice(restStart)), accepted) + } + reset(); let start = commands.length + for (const body of [{ ...request, psk: "NEVER_RAW" }, { ...request, passphrase: "NEVER_RAW" }, { ...request, poolCidr: "invalid" }]) await assert.rejects(prepare(body)) + const invalidEndpoint = { ...request, serverEndpoint: "192.0.2.999" } + await assert.rejects(prepare(invalidEndpoint), { code: "INVALID_INPUT" }) + assert.equal((await rest.inject({ method: "POST", url: "/api/ipsec/server/init", payload: { ...invalidEndpoint, serverId } })).statusCode, 400) + const unapproved = await prepare(); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, start) + for (const path of [...ipsecPeerDeletePaths.filter(path => path !== "/ip/ipsec/active-peers"), "/interface", "/ip/route", ...ipsecExternalPoolDependencyPaths]) { + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true) + catalogs[path].push({ ".id": "*late", name: "late", opaque: "NEVER_LATE" }) + assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, start) + } + for (const modify of [ + () => catalogs["/certificate"].push({ ".id": "*ca", name: IPSEC_CA_CERT, comment: "foreign" }), + () => catalogs["/certificate"].push({ ".id": "*ca", name: IPSEC_CA_CERT, comment: ipsecManagedComment("CA") }), + () => catalogs["/ip/ipsec/profile"].push({ ".id": "*owned", name: IPSEC_COMMON_NAME, dynamic: "true", comment: ipsecManagedComment() }), + () => catalogs["/ip/ipsec/policy/group"].push({ ".id": "*group", name: IPSEC_POLICY_GROUP }), + ]) { reset(); modify(); await assert.rejects(prepare()); assert.equal(commands.length, start) } + reset(); const seeded = await apply(); assert.equal(seeded.state, "applied"); const managed = structuredClone(catalogs) + for (const path of ipsecExternalPoolDependencyPaths) { + catalogs = structuredClone(managed) + catalogs[path].push({ ".id": "*allocator", name: "foreign-allocator", [path.startsWith("/ppp/") ? "remote-address" : "address-pool"]: IPSEC_COMMON_NAME }) + const before = commands.length; await assert.rejects(prepare()); assert.equal(commands.length, before) + } + for (const fault of ["lost", "ignored", "collateral", "missingAck"]) { + reset(); mode = fault; start = commands.length; const result = await apply() + const diagnostic = async () => ({ fault, operation: (await query("SELECT state,error_code,result FROM mcp_operations WHERE id=$1", [result.operation.id])).rows[0], steps: (await query("SELECT step_index,state,result FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [result.operation.id])).rows, transportFailures, requestCounts, nativeDispatches: commands.slice(start).map(row => ({ method: row.method, path: row.path, fieldNames: Object.keys(row.fields) })) }) + if (!["partial", "unknown"].includes(result.state) || commands.length !== start + 1) assert.fail(JSON.stringify(await diagnostic())) + assert.equal(result.error_code, fault === "lost" ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", JSON.stringify(await diagnostic())) + assert.equal(commands.length, start + 1) + await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, start + 1) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] }; assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, false); assert.equal(commands.length, start + 1) + } + reset(); start = commands.length; afterAck = () => setConnectionStatus(connectionId!, "paused", actor) + const revoked = await apply(); assert(["partial", "unknown"].includes(revoked.state)); assert.equal(commands.length, start + 1) + for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) hidden((await query(table === "mcp_operation_steps" ? "SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1" : `SELECT * FROM ${table} WHERE connection_id=$1`, [connectionId])).rows) + console.log("✓ IPsec init SQL/loopback: full REST parity, NAT on/off, CA/server sign, owned reuse, all-config-catalog stale proofs, foreign/system conflict, lost/ignored/collateral/missing ACK, no replay, grant revocation, private audit") + } finally { + closeFixtureAgent() + MikrotikClient.prototype.get = originalGet; MikrotikClient.prototype.put = originalPut + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await rest.close(); await query("DELETE FROM servers WHERE id=$1", [serverId]); router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-server-init-actions.test.ts b/backend/src/modules/mcp/ipsec-server-init-actions.test.ts new file mode 100644 index 00000000..ccbe0112 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-server-init-actions.test.ts @@ -0,0 +1,59 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import type { ActionContext } from "./action-types.js" +import type { ConnectionRow } from "./store.js" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import { createIpsecServerInitAdapter, ipsecServerInitActionSchema } from "./ipsec-server-init-actions.js" +import { redact } from "./security.js" +const operation = "network.ipsec.server.init", sourceProof = "a".repeat(64), beforeProof = "b".repeat(64), afterProof = "c".repeat(64) +const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } +const connection = { id: principal.connectionId, owner_id: "fixture-owner", owner_permissions: ["mm:network:write"], generation: "fixture-generation", owner_valid_until: new Date(Date.now() + 600000), policy: { mode: "autonomous", status: "active", expiresAt: principal.credentialExpiresAt, permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: [operation], allowGlobal: false } } } as ConnectionRow +const context = { principal, connection } as ActionContext +const request = { serverId: 1, request: { serverEndpoint: "vpn.example.test", poolCidr: "10.77.0.0/24", dns: "192.0.2.53", caDaysValid: 3650, serverDaysValid: 900, clientDaysValid: 180, createNatRule: true } } +assert.equal(ipsecServerInitActionSchema.safeParse(request).success, true) +for (const key of ["psk", "passphrase", "privateKey", "export", "unknown"]) assert.equal(ipsecServerInitActionSchema.safeParse({ ...request, request: { ...request.request, [key]: "NEVER_RAW" } }).success, false) +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Real SQL forbidden") }) as never; pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +try { + const sealed = new Map() + let current = structuredClone(connection), proof = beforeProof, endpoint = sourceProof, writes = 0, history = true + let stored: Record[] = [], inject: (() => void) | undefined + const adapter = createIpsecServerInitAdapter({ + connection: async () => current, + router: async () => ({ server: { id: 1 } as ServerRow, client: {} as never }), sourceProof: async () => endpoint, + read: async (_server, authorize) => { await authorize?.(); return { source: sourceProof, proof, catalogs: {}, protections: [{ object_ref: { kind: "certificate", id: "*protected" } }] } }, + seal: async value => { const key = `sealed:${randomUUID()}`; sealed.set(key, value); return key }, open: async value => sealed.get(value)!, + query: async () => stored, verifiedAccess: async () => {}, checkpoint: async () => {}, + plan: (() => ({ commands: [{ action: "create", path: "/certificate", fields: {} }], companions: [{ kind: "ipsec_peer", id: "*p" }], target: { kind: "ipsec_server", id: "1" } })) as never, + init: (async (_server: unknown, body: Record, expected: string, authorize: () => Promise, options: { receiptId: string; checkpoint: (value: unknown) => Promise }) => { + assert.equal(expected, beforeProof); assert.deepEqual(body, { ...request.request, serverId: 1 }) + await authorize(); inject?.(); await authorize(); writes++; proof = afterProof + await options.checkpoint({ receiptId: options.receiptId, completedCommands: 1, commandCount: 1, afterProof }) + return { ok: true, verified: true, receiptId: options.receiptId, serverId: 1, commandCount: 1, completedCommands: 1, confirmedWrites: 1, afterProof, historyRecorded: history, revisionRecorded: history, automaticReplay: false, forwardingVerified: false, summary: { raw: "NEVER_RAW_SUMMARY" } } + }) as never, + }) + const prepare = async () => { + const evidence = await adapter.prepare(request, context) + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 2) + assert(evidence.objects.some(row => row.id === "*p")); assert(evidence.objects.some(row => row.id === "*protected")) + assert(!JSON.stringify(evidence).includes("NEVER_RAW")) + const step = evidence.steps[0]; if (step.kind !== "service") throw Error("service expected") + return step.input + } + const reset = () => { proof = beforeProof; current = structuredClone(connection); endpoint = sourceProof; inject = undefined; history = true; stored = [] } + let input = await prepare(), receipt = await adapter.service.execute(input) + assert.equal(writes, 1); assert(!JSON.stringify(receipt).includes("NEVER_RAW")); assert.equal(await adapter.service.verify!(input, receipt), true) + stored = [{ connection_id: principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result: receipt }] + assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, true) + for (const field of ["historyRecorded", "revisionRecorded", "verified", "receiptId", "commandCount", "afterProof", "serverId"]) { + stored[0].result = { ...receipt as object, [field]: "changed" }; assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, false) + } + reset(); input = await prepare(); history = false; receipt = await adapter.service.execute(input); assert.equal(await adapter.service.verify!(input, receipt), false) + for (const mutate of [() => { proof = afterProof }, () => { endpoint = "d".repeat(64) }, () => { current.policy.status = "paused" }, () => { current.owner_permissions = [] }, () => { current.generation = "changed" }, () => { current.policy.scope.serverIds = [] }, () => { current.policy.scope.operationIds = [] }]) { + reset(); input = await prepare(); const start = writes; mutate(); await assert.rejects(adapter.service.execute(input)); assert.equal(writes, start) + } + reset(); input = await prepare(); inject = () => { current.policy.status = "paused" }; const start = writes; await assert.rejects(adapter.service.execute(input)); assert.equal(writes, start) + reset(); input = await prepare(); const aborted = new AbortController(); aborted.abort(); await assert.rejects(adapter.prepare(request, { ...context, signal: aborted.signal })) + console.log("IPsec init adapter: all REST fields, mandatory approval, sealed request, source/grant/owner cancellation, checkpoint/history and receipt reconciliation, no model summary or private inputs") +} finally { pool.query = oldQuery; pool.connect = oldConnect } diff --git a/backend/src/modules/mcp/ipsec-server-init-actions.ts b/backend/src/modules/mcp/ipsec-server-init-actions.ts new file mode 100644 index 00000000..761e41fe --- /dev/null +++ b/backend/src/modules/mcp/ipsec-server-init-actions.ts @@ -0,0 +1,109 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { ipsecInitRequestSchema } from "@mmapp/contracts/ipsec" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { planIpsecServerInit } from "../../services/ipsec-server-init-model.js" +import { readIpsecServerInitState, initIpsecServerGuarded } from "../../services/ipsec-server-init.js" +import { routerClient } from "./resources.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService, type McpServiceHandler } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const operation = "network.ipsec.server.init" +const hash = z.string().regex(/^[a-f0-9]{64}$/) +// The REST init contract contains no secrets or private export. Unknown plaintext +// keys (PSK, passphrase, private keys) are rejected at this public boundary. +export const ipsecServerInitActionSchema = z.object({ serverId: z.number().int().positive().safe(), request: ipsecInitRequestSchema.omit({ serverId: true }).strict() }).strict() +const bindingSchema = ipsecServerInitActionSchema.extend({ + operation: z.literal(operation), principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: hash, policyProof: hash, generation: z.string().min(1), sourceProof: hash, expectedProof: hash, + requestProof: hash, commandCount: z.number().int().min(0).max(2000), receiptId: z.string().uuid(), proofRevision: z.literal(3), +}).strict() +type Binding = z.infer +const serviceInput = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(262144) }).strict() +const production = { + connection: effectiveMcpConnection, router: routerClient, read: readIpsecServerInitState, sourceProof: ipsecPeerDeleteSourceProof, + plan: planIpsecServerInit, init: initIpsecServerGuarded, seal: encryptSecret, open: decryptSecret, verifiedAccess: assertVerifiedConfigurationAccess, + query: async (sql: string, values: unknown[]) => (await pool.query(sql, values)).rows as Record[], + checkpoint: async (ownerId: string, connectionId: string, value: unknown) => audit(pool, ownerId, "ipsec.server.init.checkpoint", connectionId, value), +} +const conflict = () => new McpError("STATE_CONFLICT", "IPsec, источник или допуск инициализации изменились", 409) +export function hasVerifiedIpsecServerInit(candidate: unknown, binding: Binding, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | undefined + return row.connection_id === binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && + result.ok === true && result.verified === true && result.receiptId === binding.receiptId && result.serverId === binding.serverId && result.commandCount === binding.commandCount && result.completedCommands === binding.commandCount && result.confirmedWrites === binding.commandCount && + result.historyRecorded === true && result.revisionRecorded === true && result.afterProof === proof && result.forwardingVerified === false && result.automaticReplay === false +} +export function createIpsecServerInitAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const access = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + const current = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || current.owner_valid_until.getTime() <= Date.now() || Date.parse(current.policy.expiresAt) <= Date.now() || !current.policy.scope.operationIds.includes(operation) || !current.policy.scope.serverIds.includes(binding.serverId) || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска инициализации IPsec", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof || fingerprint({ serverId: binding.serverId, request: binding.request }) !== binding.requestProof) throw conflict() + signal?.throwIfAborted() + } + const request = async (input: Record) => { + const parsed = serviceInput.parse(input), binding = bindingSchema.parse(JSON.parse(await deps.open(parsed.bindingCiphertext))) + if (binding.serverId !== parsed.serverId || binding.receiptId !== parsed.receiptId) throw conflict() + return binding + } + const source = async (binding: Binding, signal?: AbortSignal, external?: () => Promise) => { + const authorize = async () => { await access(binding, signal, external) } + await authorize() + const { server } = await deps.router(binding.serverId) + if (await deps.sourceProof(server) !== binding.sourceProof) throw conflict() + const state = await deps.read(server, authorize) + if (state.source !== binding.sourceProof) throw conflict() + await authorize(); return { server, state, authorize } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + const value = ipsecServerInitActionSchema.parse(raw) + const binding = bindingSchema.parse({ ...value, operation, principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, sourceProof: "0".repeat(64), expectedProof: "0".repeat(64), requestProof: fingerprint(value), commandCount: 0, receiptId: randomUUID(), proofRevision: 3 }) + await access(binding, context.signal) + const { server } = await deps.router(value.serverId) + binding.sourceProof = await deps.sourceProof(server) + const before = await source(binding, context.signal), plan = deps.plan(before.state, server, { ...value.request, serverId: value.serverId }) + binding.expectedProof = before.state.proof; binding.commandCount = plan.commands.length + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(value.serverId), serverId: value.serverId }, { ...plan.target, serverId: value.serverId }, ...plan.companions.map(ref => ({ ...ref, serverId: value.serverId }))] + for (const protection of before.state.protections as Array<{ object_ref: { kind: string; id: string } }>) objects.push({ kind: protection.object_ref.kind, id: protection.object_ref.id, serverId: value.serverId }) + if ((await source(binding, context.signal)).state.proof !== binding.expectedProof) throw conflict() + return { target: { scope: "servers", serverIds: [value.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: plan.commands.length + 1, before: { proof: before.state.proof }, + steps: [{ kind: "service", service: operation, input: { serverId: value.serverId, receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(binding)) } }], + warnings: [`Native-команд: ${plan.commands.length}; затем обязательная managed-only история IPsec.`, "Создаются CA и серверный приватный ключ только при отсутствии сертификатов; подпись разрешена только новым ACK-owned сертификатам. Приватный экспорт не выполняется. Существующий неподписанный сертификат блокирует весь план до первой записи.", "Все поля REST поддерживаются. clientDaysValid принимается мастером для совместимости и не создаёт клиентский сертификат. Сертификат с существующим именем повторно не выпускается при изменении endpoint или срока.", "Каждая команда проверяет полный private proof 11 native-каталогов IPsec, маршрутов и интерфейсов, источник и защиты. Потеря ответа или несовпадение read-back останавливают операцию без повтора и автоматического отката; передача трафика не подтверждается."] } + } + const service: McpServiceHandler = { + current: async input => ({ proof: (await source(await request(input))).state.proof }), + execute: async input => { + const binding = await request(input), before = await source(binding) + if (before.state.proof !== binding.expectedProof) throw conflict() + const plan = deps.plan(before.state, before.server, { ...binding.request, serverId: binding.serverId }) + if (plan.commands.length !== binding.commandCount) throw conflict() + const authorize = async () => { await deps.verifiedAccess(); await before.authorize() } + await authorize() + const { summary: _summary, ...receipt } = await deps.init(before.server, { ...binding.request, serverId: binding.serverId }, binding.expectedProof, authorize, { receiptId: binding.receiptId, checkpoint: async value => { await authorize(); await deps.checkpoint(binding.ownerId, binding.principal.connectionId, { ...value, inputProof: fingerprint(redact(input)) }) } }) + return receipt + }, + verify: async (input, result) => { const binding = await request(input), after = await source(binding); return hasVerifiedIpsecServerInit({ connection_id: binding.principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result }, binding, input, after.state.proof) }, + reconcile: async (input, external) => { + const binding = await request(input), latest = await source(binding, undefined, external) + await latest.authorize() + const rows = await deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId]) + const verified = rows.length === 1 && hasVerifiedIpsecServerInit(rows[0], binding, input, latest.state.proof) + if ((await source(binding, undefined, external)).state.proof !== latest.state.proof) throw conflict() + return { matchesOriginalState: latest.state.proof === binding.expectedProof, matchesDesiredState: verified, automaticReplay: false, forwardingVerified: false } + }, + } + return { prepare, service } +} +export function registerIpsecServerInitActions(register: typeof add) { + const adapter = createIpsecServerInitAdapter() + registerMcpService(operation, adapter.service) + register(operation, "mm_ipsec_server_init_plan", "Инициализировать сервер IKEv2 с полным preflight и проверкой каждой native-команды", "network", "mm:network:write", "write", "servers", ipsecServerInitActionSchema, { version: 4, prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/ipsec-user-create-acceptance.ts b/backend/src/modules/mcp/ipsec-user-create-acceptance.ts new file mode 100644 index 00000000..14a87933 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-create-acceptance.ts @@ -0,0 +1,134 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import jwt from "@fastify/jwt" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { env } from "../../config.js" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createIpsecRoutes } from "../../routes/ipsec.js" +import { createMcpRoutes } from "../../routes/mcp.js" +import { getEnabledIpsecServerById } from "../../services/ipsec-live.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { encryptSecret } from "./security.js" +import { ipsecUserCreateSecretPurpose } from "./ipsec-user-create-actions.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Opt-in serial disposable SQL/loopback acceptance. All certificate bytes are inert synthetic ASCII. */ +export async function acceptIpsecUserCreate(actor: AuthUser) { + type Row = Record + const serverId = 206, secretIds: string[] = [], query = pool.query.bind(pool), fakeP12 = "NEVER_SYNTHETIC_PKCS12_BYTES" + const base: Record = { ...Object.fromEntries([...ipsecPeerDeletePaths, "/file"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", passive: "yes", profile: "profile", comment: "MikrotikManager:ipsec", opaque: "NEVER_PEER" }], + "/ip/ipsec/identity": [{ ".id": "*listener", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "mode-config": "ipsec-vpn", "policy-template-group": "group", opaque: "NEVER_LISTENER" }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "ipsec-vpn", "address-pool": "pool", "static-dns": "192.0.2.53", opaque: "NEVER_MC" }], + "/ip/pool": [{ ".id": "*o", name: "pool", ranges: "192.0.2.1-192.0.2.50" }], + "/ip/ipsec/profile": [{ ".id": "*r", name: "profile", opaque: "NEVER_PROFILE" }], + "/ip/ipsec/policy/group": [{ ".id": "*g", name: "group" }], + "/ip/ipsec/policy": [{ ".id": "*y", group: "group", proposal: "proposal", opaque: "NEVER_POLICY" }], + "/ip/ipsec/proposal": [{ ".id": "*q", name: "proposal" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign,crl-sign", authority: "true", "private-key": "true" }, { ".id": "*s", name: "ipsec-server", "common-name": "vpn.example.test", "key-usage": "tls-server", ca: "ca", opaque: "NEVER_SERVER_CERT" }], + "/file": [{ ".id": "*f", name: "foreign.p12", size: "10", opaque: "NEVER_FOREIGN_FILE" }], + } + let catalogs = structuredClone(base), serial = 0, mode = "normal", connectionId: string | undefined, afterAck: (() => Promise) | undefined + const commands: Array<{ method: string; path: string; fields: Row }> = [] + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")); res.setHeader("content-type", "application/json") + try { + 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 } + commands.push({ method: req.method!, path, fields }) + let result: Row = {} + if (mode !== "ignored") { + if (path === "/certificate/sign") { + const row = catalogs["/certificate"].find(row => row.name === fields.name); assert(row) + Object.assign(row, { ca: fields.ca, 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") catalogs["/file"].push({ ".id": `*new${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" }) + 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) } + else if (req.method === "PATCH") { const row = catalogs[catalog].find(row => row[".id"] === path.slice(catalog.length + 1)); assert(row); Object.assign(row, fields) } + else if (req.method === "DELETE") catalogs[catalog] = catalogs[catalog].filter(row => row[".id"] !== path.slice(catalog.length + 1)) + else throw Error("unexpected mutation") + } + } + if (mode === "collateral") catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_COLLATERAL" + if (afterAck) { const run = afterAck; afterAck = undefined; await run() } + if (mode === "lost") { req.socket.destroy(); return } + res.end(JSON.stringify(result)) + } catch { res.statusCode = 500; res.end('{"error":"NEVER_PROVIDER"}') } + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const portal = Fastify(); await portal.register(jwt, { secret: env.jwtSecret }); await portal.register(createMcpRoutes(async () => {}), { prefix: "/api" }) + const reset = () => { catalogs = structuredClone(base); serial = 0; mode = "normal"; afterAck = undefined } + const hidden = (value: unknown) => assert(!JSON.stringify(value).includes("NEVER_"), "private fixture data leaked") + const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const row = (await query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) }; throw Error("Creation remains active; never replay") } + const secret = async (kind: "psk" | "passphrase", purpose?: string, owner = actor.id) => { const id = randomUUID(); secretIds.push(id); await query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, purpose ?? ipsecUserCreateSecretPurpose(serverId, "Alice", kind), serverId, await encryptSecret(kind === "psk" ? "NEVER_PSK_SECRET" : "NEVER_EXPORT_PASSPHRASE"), owner]); return id } + try { + assert.equal(Number((await query("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0) + await query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-create','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]) + await query("INSERT INTO app_users(id,login,name) VALUES('ipsec-create-other-owner','ipsec-create-other-owner','IPsec fixture')") + await rest.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async input => { await input.capture(); return { id: "fixture-history" } as never } }), { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec create", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.users.create", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token), psk = await secret("psk"), passphrase = await secret("passphrase") + const prepare = (request: Record) => invokeMcpTool(principal, "mm_ipsec_user_create_plan", { serverId, request }) as Promise<{ planId: string; state: string }> + const apply = async (request: Record) => { const plan = await prepare(request); hidden(plan); assert.equal(plan.state, "awaiting-approval"); await approvePlan(plan.planId, actor, true); const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key); return { plan, key, operation, ...await wait(operation.id) } } + const receipt = async (id: string) => (await query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [id])).rows[0].result + const token = (id = actor.id, permissions = actor.permissions) => portal.jwt.sign({ sub: id, apps: ["mm"], permissions }, { expiresIn: "1h" }) + const headers = { authorization: `Bearer ${token()}` } + const normalize = (entries: typeof commands) => entries.map(row => ({ ...row, path: row.path.replace(/\*new\d+/g, "*created"), fields: Object.fromEntries(Object.entries(row.fields).map(([key, value]) => [key, key === "file-name" ? "export-prefix" : value])) })) + for (const [authMethod, staticIp] of [["pre-shared-key", undefined], ["pre-shared-key", "192.0.2.7"], ["certificate", undefined], ["certificate", "192.0.2.7"]] as const) { + reset(); const start = commands.length, request = { name: "Alice", authMethod, ...(staticIp ? { staticIp } : {}), ...(authMethod === "certificate" ? { daysValid: 365, passphraseSecretRef: passphrase, privateKeyExport: true } : { pskSecretRef: psk, remoteId: "alice.example.test" }) } + const result = await apply(request); assert.equal(result.state, "applied", JSON.stringify(result)); hidden(result) + const accepted = normalize(commands.slice(start)), saved = await receipt(result.operation.id); hidden(saved); assert.equal(saved.historyRecorded, true) + assert.deepEqual(catalogs["/file"], base["/file"]); assert.deepEqual(catalogs["/ip/ipsec/profile"], base["/ip/ipsec/profile"]) + for (const [path, rows] of Object.entries(base)) for (const row of rows) assert.deepEqual(catalogs[path].find(current => current[".id"] === row[".id"]), row, `existing raw object changed: ${path}/${row[".id"]}`) + if (authMethod === "certificate") { + assert.equal(saved.humanDownloadOnly, true); assert.equal(saved.ipsecContentReturned, false) + 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((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)) + await query("UPDATE mcp_artifacts SET expires_at=now()-interval '1 second' WHERE id=$1", [saved.artifactId]); assert.equal((await portal.inject({ url: saved.downloadPath, headers })).statusCode, 404) + } + reset(); const restStart = commands.length + 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(); 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) + for (const reference of [await secret("psk", "wrong-purpose"), await secret("psk", undefined, "ipsec-create-other-owner"), await secret("psk", ipsecUserCreateSecretPurpose(serverId, "other-client", "psk"))]) await assert.rejects(prepare({ name: "Alice", authMethod: "pre-shared-key", pskSecretRef: reference })) + const wrongServer = await secret("psk"); await query("UPDATE mcp_secrets SET server_id=NULL WHERE id=$1", [wrongServer]); await assert.rejects(prepare({ name: "Alice", authMethod: "pre-shared-key", pskSecretRef: wrongServer })) + reset(); catalogs["/ip/ipsec/mode-config"].push({ ".id": "*foreign", name: "mc-ipsec-alice", address: "192.0.2.8", opaque: "NEVER_FOREIGN_MC" }); catalogs["/ip/ipsec/identity"].push({ ".id": "*consumer", peer: "ike", "auth-method": "pre-shared-key", "mode-config": "*FOREIGN", comment: "other client" }) + await assert.rejects(prepare({ name: "Alice", authMethod: "pre-shared-key", pskSecretRef: psk, staticIp: "192.0.2.7" })); assert.equal(commands.length, rawStart) + reset() + const unapproved = await prepare({ name: "Alice", authMethod: "pre-shared-key", pskSecretRef: psk }); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, rawStart) + reset(); const stale = await prepare({ name: "Alice", authMethod: "pre-shared-key", pskSecretRef: psk }); await approvePlan(stale.planId, actor, true); catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_LATE"; assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, rawStart) + for (const fault of ["lost", "ignored", "collateral"]) { + reset(); mode = fault; const start = commands.length, result = await apply({ name: "Alice", authMethod: "pre-shared-key", pskSecretRef: psk, staticIp: "192.0.2.7" }); assert(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, start + 1) + await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, start + 1) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] }; assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, false); assert.equal(commands.length, start + 1) + } + reset(); const revoked = await secret("psk"), start = commands.length; afterAck = () => query("DELETE FROM mcp_secrets WHERE id=$1", [revoked]) + const result = await apply({ name: "Alice", authMethod: "pre-shared-key", pskSecretRef: revoked, staticIp: "192.0.2.7" }); assert(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, start + 1) + reset(); const cert = await apply({ name: "Alice", privateKeyExport: true, passphraseSecretRef: passphrase }), saved = await receipt(cert.operation.id) + await setConnectionStatus(connectionId, "paused", actor) + assert.notEqual((await portal.inject({ url: saved.downloadPath, headers })).statusCode, 200) + for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) hidden((await query(table === "mcp_operation_steps" ? "SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1" : `SELECT * FROM ${table} WHERE connection_id=$1`, [connectionId])).rows) + console.log("✓ IPsec create SQL/loopback: all REST modes, cert sign/export/cleanup, full raw conservation, purpose/owner/revocation, no replay, encrypted owner portal artifact and TTL") + } finally { + if (connectionId) { await setConnectionStatus(connectionId, "paused", actor); await query("DELETE FROM mcp_artifacts WHERE connection_id=$1", [connectionId]) } + await rest.close(); await portal.close(); await query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])", [secretIds]); await query("DELETE FROM app_users WHERE id='ipsec-create-other-owner'"); await query("DELETE FROM servers WHERE id=$1", [serverId]); router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-user-create-actions.test.ts b/backend/src/modules/mcp/ipsec-user-create-actions.test.ts new file mode 100644 index 00000000..fe5f8365 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-create-actions.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import type { ActionContext } from "./action-types.js" +import type { ConnectionRow } from "./store.js" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import { createIpsecUserCreateAdapter, ipsecUserCreateActionSchema, ipsecUserCreateSecretPurpose } from "./ipsec-user-create-actions.js" +import { redact } from "./security.js" +const operation = "network.ipsec.users.create", sourceProof = "a".repeat(64), beforeProof = "b".repeat(64), afterProof = "c".repeat(64) +const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } +const connection = { id: principal.connectionId, owner_id: "fixture-owner", owner_permissions: ["mm:network:write"], generation: "fixture-generation", owner_valid_until: new Date(Date.now() + 600000), policy: { mode: "autonomous", status: "active", expiresAt: principal.credentialExpiresAt, permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: [operation], allowGlobal: false } } } as ConnectionRow +const context = { principal, connection } as ActionContext +const id = randomUUID(), request = { serverId: 1, request: { name: "Alice", authMethod: "pre-shared-key", pskSecretRef: id } } +assert.equal(ipsecUserCreateActionSchema.safeParse(request).success, true) +assert.equal(ipsecUserCreateActionSchema.safeParse({ serverId: 1, request: { name: "Alice", privateKeyExport: true, staticIp: "192.0.2.7", daysValid: 365, passphraseSecretRef: id } }).success, true) +for (const bad of [{ name: "Alice" }, { name: "Alice", authMethod: "pre-shared-key" }, { name: "Alice", privateKeyExport: true, psk: "raw-PSK" }, { name: "Alice", privateKeyExport: true, passphrase: "raw-passphrase" }, { name: "Alice", privateKeyExport: true, unknown: true }]) assert.equal(ipsecUserCreateActionSchema.safeParse({ serverId: 1, request: bad }).success, false) +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Real SQL forbidden") }) as never; pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +try { + const sealed = new Map() + let current = structuredClone(connection), proof = beforeProof, endpoint = sourceProof, writes = 0, history = true + let secretRow: Record = { created_by: connection.owner_id, server_id: 1, purpose: ipsecUserCreateSecretPurpose(1, "Alice", "psk"), ciphertext: "secret-cipher" } + let stored: Record[] = [], received: unknown, inject: (() => void) | undefined + const artifacts = new Map>() + const adapter = createIpsecUserCreateAdapter({ + connection: async () => current, + router: async () => ({ server: { id: 1 } as ServerRow, client: {} as never }), sourceProof: async () => endpoint, + read: async (_server, authorize) => { await authorize?.(); return { source: sourceProof, proof, catalogs: {}, protections: [{ object_ref: { kind: "certificate", id: "*protected" } }] } }, + seal: async value => { const key = `sealed:${randomUUID()}`; sealed.set(key, value); return key }, + open: async value => value === "secret-cipher" ? "NEVER_RAW_PSK" : sealed.get(value)!, + query: async (sql, values) => { + if (sql.startsWith("INSERT INTO mcp_secrets")) { artifacts.set(values[0], { purpose: values[1], server_id: values[2], ciphertext: values[3], created_by: values[4] }); return [] } + if (sql.includes("mcp_secrets")) return artifacts.has(values[0]) ? [structuredClone(artifacts.get(values[0])!)] : [structuredClone(secretRow)] + return stored + }, + verifiedAccess: async () => {}, checkpoint: async () => {}, + artifact: async (authority, bundle) => { const id = randomUUID(); artifacts.set(id, { authority, ciphertext: await (async () => { const key = randomUUID(); sealed.set(key, JSON.stringify(bundle)); return key })() }); return { artifactId: id, downloadPath: `/api/mcp/artifacts/${id}`, expiresInSeconds: 900, bytes: 100, sha256: "d".repeat(64), humanDownloadOnly: true, ipsecContentReturned: false } }, + verifyArtifact: async (id, authority) => JSON.stringify(artifacts.get(id)?.authority) === JSON.stringify(authority), + plan: ((_state: unknown, _server: unknown, body: Record) => ({ commands: [{ action: "create", path: "/ip/ipsec/identity", fields: {} }], companions: [{ kind: "ipsec_peer", id: "*p" }], target: { kind: "ipsec_user", id: body.name }, certName: undefined })) as never, + create: (async (_server: unknown, body: Record, expected: string, authorize: () => Promise, options: { receiptId: string; checkpoint: (value: unknown) => Promise; saveBundle: (bundle: unknown, proof: string) => Promise> }) => { + assert.equal(expected, beforeProof); received = body + await authorize(); inject?.(); await authorize(); writes++; proof = afterProof + await options.checkpoint({ receiptId: options.receiptId, completedCommands: 1, commandCount: 1, afterProof }) + const artifact = body.authMethod === "certificate" ? await options.saveBundle({ contentB64: "NEVER_RAW_PKCS12", passphrase: "NEVER_RAW_PASSPHRASE" }, afterProof) : undefined + return { ok: true, verified: true, receiptId: options.receiptId, clientId: "Alice", commandCount: 1, completedCommands: 1, confirmedWrites: 1, afterProof, historyRecorded: history, revisionRecorded: history, automaticReplay: false, forwardingVerified: false, client: { raw: "NEVER_RAW_CLIENT" }, ...(artifact ?? {}) } + }) as never, + }) + const prepare = async (value: Record = request) => { + const evidence = await adapter.prepare(value, context) + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, ipsecUserCreateActionSchema.parse(value).request.authMethod === "certificate" ? 3 : 2) + assert(evidence.objects.some(row => row.id === "*p")); assert(evidence.objects.some(row => row.id === "*protected")) + assert(!JSON.stringify(evidence).includes("NEVER_RAW")); assert(!JSON.stringify(evidence).includes(id)) + const step = evidence.steps[0]; if (step.kind !== "service") throw Error("service expected") + return step.input + } + const reset = () => { proof = beforeProof; current = structuredClone(connection); endpoint = sourceProof; inject = undefined; history = true; stored = [] } + let input = await prepare(), receipt = await adapter.service.execute(input) + assert.equal(writes, 1); assert.deepEqual(received, { name: "Alice", authMethod: "pre-shared-key", serverId: 1, psk: "NEVER_RAW_PSK" }) + assert(!JSON.stringify(receipt).includes("NEVER_RAW")); assert.equal(await adapter.service.verify!(input, receipt), true) + stored = [{ connection_id: principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result: receipt }] + assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, true) + stored[0].result = { ...receipt as object, historyRecorded: false } + assert.equal((await adapter.service.reconcile!(input, async () => {})).matchesDesiredState, false) + reset(); input = await prepare(); history = false; receipt = await adapter.service.execute(input) + assert.equal(await adapter.service.verify!(input, receipt), false) + reset(); input = await prepare({ serverId: 1, request: { name: "Alice", privateKeyExport: true } }); receipt = await adapter.service.execute(input) + assert(!JSON.stringify(receipt).includes("NEVER_RAW")); assert.equal(await adapter.service.verify!(input, receipt), true) + const bundleId = (receipt as Record).artifactId + assert(artifacts.has(bundleId)); assert(!JSON.stringify(artifacts.get(bundleId)).includes("NEVER_RAW")) + artifacts.get(bundleId)!.authority = { ownerId: "other" } + assert.equal(await adapter.service.verify!(input, receipt), false) + for (const mutate of [() => { endpoint = "d".repeat(64) }, () => { proof = afterProof }, () => { current.generation = "changed" }, () => { secretRow.purpose = "wrong" }, () => { secretRow.server_id = 2 }, () => { secretRow.created_by = "other" }]) { + reset(); secretRow = { created_by: connection.owner_id, server_id: 1, purpose: ipsecUserCreateSecretPurpose(1, "Alice", "psk"), ciphertext: "secret-cipher" }; input = await prepare() + const count = writes; mutate(); await assert.rejects(adapter.service.execute(input)); assert.equal(writes, count) + } + reset(); secretRow = { created_by: connection.owner_id, server_id: 1, purpose: ipsecUserCreateSecretPurpose(1, "Alice", "psk"), ciphertext: "secret-cipher" }; input = await prepare(); inject = () => { current.policy.status = "revoked" as never } + const count = writes; await assert.rejects(adapter.service.execute(input)); assert.equal(writes, count) + console.log("ipsec-user-create-actions: human gate, secret scope, encrypted binding, history and reconcile passed") +} finally { pool.query = oldQuery; pool.connect = oldConnect; await pool.end() } diff --git a/backend/src/modules/mcp/ipsec-user-create-actions.ts b/backend/src/modules/mcp/ipsec-user-create-actions.ts new file mode 100644 index 00000000..6b628a31 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-create-actions.ts @@ -0,0 +1,152 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { ipsecUserCreateRequestSchema } from "@mmapp/contracts/ipsec" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { planIpsecUserCreate } from "../../services/ipsec-user-create-model.js" +import { readIpsecUserCreateState, createIpsecUserGuarded } from "../../services/ipsec-user-create.js" +import { routerClient } from "./resources.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" +import type { McpServiceHandler } from "./service-registry.js" +import { createIpsecUserBundleArtifact, verifyIpsecUserBundleArtifact, type IpsecUserBundleAuthority } from "./ipsec-user-create-artifact.js" + +const operation = "network.ipsec.users.create" +export const ipsecUserCreateSecretPurpose = (serverId: number, clientId: string, kind: "psk" | "passphrase") => `ipsec-user-create-${kind}:${serverId}:${clientId}` +const hash = z.string().regex(/^[a-f0-9]{64}$/) +export const ipsecUserCreatePlanRequestSchema = ipsecUserCreateRequestSchema.omit({ serverId: true, psk: true, passphrase: true }).extend({ pskSecretRef: z.string().uuid().optional(), passphraseSecretRef: z.string().uuid().optional(), privateKeyExport: z.boolean().default(false) }).strict().superRefine((value, context) => { + if (value.authMethod === "pre-shared-key" && !value.pskSecretRef) context.addIssue({ code: "custom", message: "PSK требует secret reference", path: ["pskSecretRef"] }) + if (value.authMethod === "certificate" && !value.privateKeyExport) context.addIssue({ code: "custom", message: "Создание сертификата включает приватный экспорт и требует явного privateKeyExport", path: ["privateKeyExport"] }) +}) +export const ipsecUserCreateActionSchema = z.object({ serverId: z.number().int().positive().safe(), request: ipsecUserCreatePlanRequestSchema }).strict() +const bindingSchema = ipsecUserCreateActionSchema.extend({ + operation: z.literal(operation), principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: hash, policyProof: hash, generation: z.string().min(1), sourceProof: hash, expectedProof: hash, + referenceProof: hash, requestProof: hash, commandCount: z.number().int().min(0).max(100), receiptId: z.string().uuid(), proofRevision: z.literal(2), +}).strict() +type Binding = z.infer +const serviceInput = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(262144) }).strict() +const production = { + connection: effectiveMcpConnection, router: routerClient, read: readIpsecUserCreateState, sourceProof: ipsecPeerDeleteSourceProof, + plan: planIpsecUserCreate, create: createIpsecUserGuarded, seal: encryptSecret, open: decryptSecret, verifiedAccess: assertVerifiedConfigurationAccess, + query: async (sql: string, values: unknown[]) => (await pool.query(sql, values)).rows as Record[], + checkpoint: async (ownerId: string, connectionId: string, value: unknown) => audit(pool, ownerId, "ipsec.user.create.checkpoint", connectionId, value), + artifact: createIpsecUserBundleArtifact, verifyArtifact: verifyIpsecUserBundleArtifact, +} +const conflict = () => new McpError("STATE_CONFLICT", "Клиент IPsec, источник, ссылка секрета или допуск изменились", 409) + +export function hasVerifiedIpsecUserCreate(candidate: unknown, binding: Binding, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | undefined + return row.connection_id === binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && + result.ok === true && result.verified === true && result.receiptId === binding.receiptId && result.clientId === binding.request.name.trim() && result.commandCount === binding.commandCount && result.completedCommands === binding.commandCount && result.confirmedWrites === binding.commandCount && + result.historyRecorded === true && result.revisionRecorded === true && result.afterProof === proof && result.forwardingVerified === false && result.automaticReplay === false && + (binding.request.authMethod !== "certificate" || (z.string().uuid().safeParse(result.artifactId).success && result.humanDownloadOnly === true && result.ipsecContentReturned === false)) && !Object.hasOwn(result, "bundle") +} + +/** Private binding never contains plaintext PSK. Every material resolution checks + * its current owner/purpose/server/cipher metadata before another native access. */ +export function createIpsecUserCreateAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const access = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + const current = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || current.owner_valid_until.getTime() <= Date.now() || Date.parse(current.policy.expiresAt) <= Date.now() || !current.policy.scope.operationIds.includes(operation) || !current.policy.scope.serverIds.includes(binding.serverId) || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска изменения клиента IPsec", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof || fingerprint({ serverId: binding.serverId, request: binding.request }) !== binding.requestProof) throw conflict() + signal?.throwIfAborted() + } + const material = async (binding: Binding, signal?: AbortSignal, external?: () => Promise, compare = true) => { + await access(binding, signal, external) + const { pskSecretRef, passphraseSecretRef, privateKeyExport: _privateKeyExport, ...request } = binding.request + const references: unknown[] = [], secrets: { psk?: string; passphrase?: string } = {} + for (const [kind, id] of [["psk", pskSecretRef], ["passphrase", passphraseSecretRef]] as const) { + if (!id) continue + const rows = await deps.query("SELECT purpose,server_id,created_by,CASE WHEN octet_length(ciphertext)<=32768 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2 LIMIT 2", [id, binding.ownerId]) + const row = rows[0] + if (rows.length !== 1 || !row || row.created_by !== binding.ownerId || row.server_id !== binding.serverId || row.purpose !== ipsecUserCreateSecretPurpose(binding.serverId, binding.request.name.trim(), kind) || typeof row.ciphertext !== "string") throw new McpError("SECRET_UNAVAILABLE", "Ссылка секрета не принадлежит владельцу, серверу, клиенту и назначению IPsec", 409) + references.push({ id, ...row }) + const value = await deps.open(row.ciphertext).catch(() => { throw new McpError("SECRET_UNAVAILABLE", "Секрет PSK недоступен", 409) }) + if (!z.string().min(8).max(16384).safeParse(value).success) throw new McpError("SECRET_UNAVAILABLE", "Неподдерживаемый формат секрета IPsec", 409) + secrets[kind] = value + } + const referenceProof = fingerprint(references) + if (compare && referenceProof !== binding.referenceProof) throw conflict() + await access(binding, signal, external) + return { request: { ...request, serverId: binding.serverId, ...secrets }, referenceProof } + } + const request = async (input: Record) => { + const parsed = serviceInput.parse(input), binding = bindingSchema.parse(JSON.parse(await deps.open(parsed.bindingCiphertext))) + if (binding.serverId !== parsed.serverId || binding.receiptId !== parsed.receiptId) throw conflict() + return binding + } + const artifactAuthority = (binding: Binding, input: Record): IpsecUserBundleAuthority => ({ principal: binding.principal, ownerId: binding.ownerId, policyProof: binding.policyProof, ownerProof: binding.ownerProof, generation: binding.generation, serverId: binding.serverId, clientName: binding.request.name.trim(), receiptId: binding.receiptId, sourceProof: binding.sourceProof, inputProof: fingerprint(redact(input)) }) + const artifactAvailable = async (binding: Binding, input: Record, result: unknown) => { + if (binding.request.authMethod !== "certificate") return true + const value = result as Record | null, id = value?.artifactId + if (!z.string().uuid().safeParse(id).success) return false + return deps.verifyArtifact(String(id), artifactAuthority(binding, input), String(value?.afterProof)) + } + const source = async (binding: Binding, signal?: AbortSignal, external?: () => Promise) => { + const authorize = async () => { await material(binding, signal, external) } + await authorize() + const { server } = await deps.router(binding.serverId) + if (await deps.sourceProof(server) !== binding.sourceProof) throw conflict() + const state = await deps.read(server, authorize) + if (state.source !== binding.sourceProof) throw conflict() + await authorize(); return { server, state, authorize, request: (await material(binding, signal, external)).request } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + const value = ipsecUserCreateActionSchema.parse(JSON.parse(JSON.stringify(ipsecUserCreateActionSchema.parse(raw)))) + const binding: Binding = bindingSchema.parse({ ...value, operation, principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, sourceProof: "0".repeat(64), expectedProof: "0".repeat(64), referenceProof: fingerprint([]), requestProof: fingerprint(value), commandCount: 0, receiptId: randomUUID(), proofRevision: 2 }) + const resolved = await material(binding, context.signal, undefined, false) + binding.referenceProof = resolved.referenceProof + const { server } = await deps.router(value.serverId) + binding.sourceProof = await deps.sourceProof(server) + const before = await source(binding, context.signal), plan = deps.plan(before.state, server, resolved.request, `mm-ipsec-${binding.receiptId}`) + binding.expectedProof = before.state.proof; binding.commandCount = plan.commands.length + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(value.serverId), serverId: value.serverId }, { kind: "ipsec_user", id: value.request.name.trim(), serverId: value.serverId }, { ...plan.target, serverId: value.serverId }, ...plan.companions.map(ref => ({ ...ref, serverId: value.serverId }))] + for (const protection of before.state.protections as Array<{ object_ref: { kind: string; id: string } }>) objects.push({ kind: protection.object_ref.kind, id: protection.object_ref.id, serverId: value.serverId }) + if ((await source(binding, context.signal)).state.proof !== binding.expectedProof) throw conflict() + return { target: { scope: "servers", serverIds: [value.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: plan.commands.length + 1 + (before.request.authMethod === "certificate" ? 1 : 0), before: { proof: before.state.proof }, + steps: [{ kind: "service", service: operation, input: { serverId: value.serverId, receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(binding)) } }], + warnings: [`Native-команд: ${plan.commands.length}; затем обязательная managed-only история IPsec.`, "PSK и passphrase принимаются только через owner/purpose/server/client secret reference; приватный bundle сохраняется зашифрованным и не возвращается модели.", ...(value.request.authMethod === "certificate" ? ["Подтверждение включает генерацию клиентского приватного ключа, подпись CA, экспорт PKCS12 и удаление временного файла на RouterOS. Содержимое читается по точному native ID: предел /file/get — 61438 байт; автоповтор при неопределённом ответе и переадресация по имени запрещены."] : []), "Каждая команда проверяет полный private proof, источник, защиты и ссылку секрета. Потеря ответа или несовпадение read-back останавливают операцию без повтора; передача трафика не подтверждается."] } + } + const service: McpServiceHandler = { + current: async input => ({ proof: (await source(await request(input))).state.proof }), + execute: async input => { + const binding = await request(input), before = await source(binding) + if (before.state.proof !== binding.expectedProof) throw conflict() + const plan = deps.plan(before.state, before.server, before.request, `mm-ipsec-${binding.receiptId}`) + if (plan.commands.length !== binding.commandCount) throw conflict() + const authorize = async () => { await deps.verifiedAccess(); await before.authorize() } + await authorize() + const result = await deps.create(before.server, before.request, binding.expectedProof, authorize, { receiptId: binding.receiptId, exportPrefix: `mm-ipsec-${binding.receiptId}`, checkpoint: async value => { await authorize(); await deps.checkpoint(binding.ownerId, binding.principal.connectionId, { ...value, inputProof: fingerprint(redact(input)) }) }, saveBundle: async (bundle, afterProof) => deps.artifact(artifactAuthority(binding, input), bundle, afterProof, authorize) }) + const { client: _client, bundle: _bundle, ...receipt } = result + return receipt + }, + verify: async (input, result) => { + const binding = await request(input), after = await source(binding) + return hasVerifiedIpsecUserCreate({ connection_id: binding.principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result }, binding, input, after.state.proof) && await artifactAvailable(binding, input, result) + }, + reconcile: async (input, external) => { + const binding = await request(input), latest = await source(binding, undefined, external) + await latest.authorize() + const rows = await deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId]) + const verified = rows.length === 1 && hasVerifiedIpsecUserCreate(rows[0], binding, input, latest.state.proof) && await artifactAvailable(binding, input, rows[0].result) + if ((await source(binding, undefined, external)).state.proof !== latest.state.proof) throw conflict() + return { matchesOriginalState: latest.state.proof === binding.expectedProof, matchesDesiredState: verified, automaticReplay: false, forwardingVerified: false } + }, + } + return { prepare, service } +} + +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: 2, prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/ipsec-user-create-artifact.ts b/backend/src/modules/mcp/ipsec-user-create-artifact.ts new file mode 100644 index 00000000..509e5490 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-create-artifact.ts @@ -0,0 +1,109 @@ +import { createHash, randomUUID, timingSafeEqual } from "node:crypto" +import { z } from "zod" +import type { IpsecCertBundle } from "@mmapp/contracts/ipsec" +import { pool } from "../../db/index.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { readIpsecUserCreateState } from "../../services/ipsec-user-create.js" +import { assertLogArtifactBudget } from "./log-artifacts.js" +import { assertPortalClaimsCurrent } from "./portal-revocation.js" +import { connectionWrite, effectiveMcpConnection } from "./store.js" +import { decryptSecret, encryptSecret, fingerprint, privateSecretFingerprint, McpError } from "./security.js" + +const digest = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +export const ipsecUserBundleAuthoritySchema = z.object({ + principal: z.object({ connectionId: uuid, credentialId: uuid, credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), policyProof: digest, ownerProof: digest, generation: z.string().min(1), serverId: z.number().int().positive(), + clientName: z.string().min(1).max(64), receiptId: uuid, sourceProof: digest, inputProof: digest, +}).strict() +export type IpsecUserBundleAuthority = z.infer +const scopeSchema = z.object({ version: z.literal(1), kind: z.literal("ipsec-user-bundle"), authority: ipsecUserBundleAuthoritySchema, afterProof: digest, bytes: z.number().int().positive().max(1048576), sha256: digest, encryptedSha256: digest, proof: digest }).strict() +type Header = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date } +type Meta = Header & { access_scope: z.infer; encrypted_bytes: number } +const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "ipsec-user-bundle-artifact-v1") +const signed = (header: Header, scope: Omit) => ({ id: header.id, connection_id: header.connection_id, filename: header.filename, media_type: header.media_type, expires_at: header.expires_at.toISOString(), scope }) +const fail = (message: string, code = "ARTIFACT_INVALID") => new McpError(code, message, 409) +export const ipsecUserBundleReceipt = (header: Pick, scope: Pick) => ({ artifactId: header.id, downloadPath: `/api/mcp/artifacts/${header.id}`, expiresInSeconds: 900, bytes: scope.bytes, sha256: scope.sha256, humanDownloadOnly: true, ipsecContentReturned: false }) + +async function access(authority: IpsecUserBundleAuthority, client: Pick = pool) { + const current = await effectiveMcpConnection(authority.principal, client) + if (current.owner_id !== authority.ownerId || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write") || !current.policy.scope.operationIds.includes("network.ipsec.users.create") || !current.policy.scope.serverIds.includes(authority.serverId) || current.policy.mode === "observer" || Date.parse(authority.principal.credentialExpiresAt) <= Date.now()) throw new McpError("PERMISSION_DENIED", "Нет допуска к приватному пакету IPsec", 403) + if (current.generation !== authority.generation || fingerprint(current.policy) !== authority.policyProof || fingerprint(current.owner_permissions) !== authority.ownerProof) throw fail("Допуск пакета IPsec изменился", "RESET_REQUIRED") + const denied = (await client.query("SELECT 1 FROM mcp_protections WHERE object_ref->>'protection'='deny' AND (object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1)) LIMIT 1", [String(authority.serverId)])).rows[0] + if (denied) throw fail("Пакет IPsec заблокирован защитой", "PROTECTED_OBJECT") +} +async function source(authority: IpsecUserBundleAuthority, authorize: () => Promise = async () => {}) { + const check = async () => { await authorize(); await access(authority) } + await check() + const server = await getServerRowById(authority.serverId) + if (!server || await ipsecPeerDeleteSourceProof(server) !== authority.sourceProof) throw fail("Источник пакета IPsec изменился", "STATE_CONFLICT") + return readIpsecUserCreateState(server, check) +} +export async function createIpsecUserBundleArtifact(authority: IpsecUserBundleAuthority, bundle: IpsecCertBundle, afterProof: string, authorize: () => Promise) { + ipsecUserBundleAuthoritySchema.parse(authority) + await authorize(); await access(authority) + const content = Buffer.from(JSON.stringify(bundle)), ciphertext = await encryptSecret(content.toString("base64")) + if (!content.length || content.length > 1048576 || Buffer.byteLength(ciphertext) > 2097152) throw fail("Приватный пакет IPsec превышает предел", "EXPORT_LIMIT") + if ((await source(authority, authorize)).proof !== afterProof) throw fail("IPsec изменился перед сохранением пакета", "STATE_CONFLICT") + const header: Header = { id: randomUUID(), connection_id: authority.principal.connectionId, filename: "ipsec-client-bundle.json", media_type: "application/json", expires_at: new Date(Date.now() + 900000) } + const unsigned = { version: 1 as const, kind: "ipsec-user-bundle" as const, authority, afterProof, bytes: content.length, sha256: hash(content), encryptedSha256: hash(ciphertext) } + const scope = scopeSchema.parse({ ...unsigned, proof: await sign(signed(header, unsigned)) }) + await connectionWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [header.connection_id]); await access(authority, client) + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))") + await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()") + const usage = (await client.query("SELECT count(*) FILTER(WHERE connection_id=$1)::text AS files,COALESCE(sum(octet_length(ciphertext)) FILTER(WHERE connection_id=$1),0)::text AS own_bytes,COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts", [header.connection_id])).rows[0] + assertLogArtifactBudget(BigInt(usage.files), BigInt(usage.own_bytes), BigInt(usage.total_bytes), Buffer.byteLength(ciphertext)) + await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)", [header.id, header.connection_id, header.filename, header.media_type, ciphertext, JSON.stringify(scope), header.expires_at]) + }) + await authorize(); await access(authority) + return ipsecUserBundleReceipt(header, scope) +} +async function meta(id: string): Promise { + const row = (await pool.query("SELECT id,connection_id,filename,media_type,expires_at,CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope,octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND expires_at>now() AND octet_length(filename)<=1024 AND octet_length(media_type)<=200", [id])).rows[0] + if (!row) throw new McpError("NOT_FOUND", "Пакет IPsec отсутствует или срок истёк", 404) + const scope = scopeSchema.safeParse(row.access_scope) + if (!scope.success || !(row.expires_at instanceof Date) || !Number.isFinite(row.expires_at.getTime()) || !Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes <= 0 || row.encrypted_bytes > 2097152) throw fail("Метаданные пакета IPsec не подтверждены") + const { proof, ...unsigned } = scope.data + if (row.connection_id !== unsigned.authority.principal.connectionId || !timingSafeEqual(Buffer.from(proof, "hex"), Buffer.from(await sign(signed(row, unsigned)), "hex"))) throw fail("Подпись пакета IPsec не совпала") + return { ...row, access_scope: scope.data } +} +async function checkpoint(row: Meta) { + const scope = row.access_scope + const rows = (await pool.query("SELECT s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id JOIN mcp_plans p ON p.id=o.plan_id WHERE o.connection_id=$1 AND o.state='applied' AND p.operation_id='network.ipsec.users.create' AND p.approved_by IS NOT NULL AND s.state='verified' AND s.intended_state->>'kind'='service' AND s.intended_state->>'service'='network.ipsec.users.create' AND s.result->>'artifactId'=$2 LIMIT 2", [row.connection_id, row.id])).rows + const candidate = rows[0], input = candidate?.intended_state?.input, result = candidate?.result + if (rows.length !== 1 || fingerprint(input) !== scope.authority.inputProof || !result || result.receiptId !== scope.authority.receiptId || result.clientId !== scope.authority.clientName || result.historyRecorded !== true || result.revisionRecorded !== true || result.verified !== true || result.ok !== true || result.completedCommands !== result.commandCount || result.confirmedWrites !== result.commandCount || result.afterProof !== scope.afterProof || result.automaticReplay !== false || result.forwardingVerified !== false || Object.entries(ipsecUserBundleReceipt(row, scope)).some(([key, value]) => result[key] !== value)) throw fail("Нет точной проверенной квитанции приватного экспорта IPsec", "STATE_CONFLICT") +} +export async function verifyIpsecUserBundleArtifact(id: string, authority: IpsecUserBundleAuthority, afterProof: string) { + const row = await meta(id), scope = row.access_scope + await access(authority) + if (fingerprint(authority) !== fingerprint(scope.authority) || scope.afterProof !== afterProof) return false + const payload = (await pool.query("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152", [id, scope.proof])).rows[0] + await access(authority) + return !!payload && hash(payload.ciphertext) === scope.encryptedSha256 && (await source(authority)).proof === afterProof +} +export async function humanIpsecUserBundleMetadata(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent) { + const row = await meta(id), scope = row.access_scope + await checkClaims(actor) + if (actor.id !== scope.authority.ownerId || !hasPermission(actor.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Пакет IPsec доступен только владельцу", 403) + await access(scope.authority); await checkpoint(row) + return scope +} +export async function readHumanIpsecUserBundleArtifact(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent, signal?: AbortSignal) { + const bounded = signal ? AbortSignal.any([signal, AbortSignal.timeout(45000)]) : AbortSignal.timeout(45000) + const row = await meta(id), scope = row.access_scope + const authorize = async () => { bounded.throwIfAborted(); await checkClaims(actor); if (actor.id !== scope.authority.ownerId || !hasPermission(actor.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Пакет IPsec доступен только владельцу", 403); await access(scope.authority); await checkpoint(row); if (row.expires_at.getTime() <= Date.now()) throw fail("Срок пакета IPsec истёк") } + await authorize() + if ((await source(scope.authority, authorize)).proof !== scope.afterProof) throw fail("IPsec изменился после создания пакета", "STATE_CONFLICT") + const payload = (await pool.query("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152", [id, scope.proof])).rows[0] + await authorize() + if (!payload || hash(payload.ciphertext) !== scope.encryptedSha256) throw fail("Шифротекст пакета IPsec изменился") + let content: Buffer + try { content = Buffer.from(await decryptSecret(payload.ciphertext), "base64") } catch { throw fail("Пакет IPsec не расшифрован") } + if (content.length !== scope.bytes || hash(content) !== scope.sha256) throw fail("Размер или SHA-256 пакета IPsec не совпал") + if ((await source(scope.authority, authorize)).proof !== scope.afterProof) throw fail("IPsec изменился при выдаче пакета", "STATE_CONFLICT") + await authorize() + return { filename: row.filename, mediaType: row.media_type, content } +} diff --git a/backend/src/modules/mcp/ipsec-user-delete-acceptance.ts b/backend/src/modules/mcp/ipsec-user-delete-acceptance.ts new file mode 100644 index 00000000..5a8aa3a2 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-delete-acceptance.ts @@ -0,0 +1,115 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import ipsecRoutes, { createIpsecRoutes } from "../../routes/ipsec.js" +import { getEnabledIpsecServerById } from "../../services/ipsec-live.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Explicit SQL + loopback HTTP fixture only. Never calls a real RouterOS or exports a key. */ +export async function acceptIpsecUserDeletion(actor: AuthUser) { + const serverId = 194, base: Record[]> = Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])) + base["/ip/ipsec/peer"] = [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", disabled: "true", opaque: "NEVER_USER_PEER" }] + base["/ip/ipsec/identity"] = [{ ".id": "*i", peer: "ike", "auth-method": "digital-signature", "remote-certificate": "ipsec-user-alice", certificate: "server", "mode-config": "mc-ipsec-alice", comment: "MikrotikManager:ipsec user=Alice", disabled: "true", secret: "NEVER_USER_SECRET" }, { ".id": "*l", peer: "ike", "auth-method": "digital-signature", certificate: "server", "mode-config": "ipsec-vpn", disabled: "true", comment: "MikrotikManager:ipsec IKEv2 listener" }] + base["/ip/ipsec/mode-config"] = [{ ".id": "*m", name: "mc-ipsec-alice", address: "198.18.0.1", responder: "true" }, { ".id": "*s", name: "ipsec-vpn", "address-pool": "vpn-pool" }] + base["/certificate"] = [{ ".id": "*c", name: "ipsec-user-alice", "common-name": "Alice", "key-usage": "tls-client", ca: "ca", opaque: "NEVER_USER_CERT" }, { ".id": "*a", name: "ca", "key-usage": "key-cert-sign", authority: "true" }, { ".id": "*v", name: "server", "key-usage": "tls-server", "common-name": "vpn.fixture", ca: "ca" }] + let catalogs = structuredClone(base), mode = "normal", failAt = 1, dispatched = 0, deletes = 0, reads = 0, connectionId: string | undefined + let duringRead: (() => Promise) | undefined, afterDelete: (() => Promise) | undefined + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json"); const path = (req.url ?? "").replace(/^\/rest/, "") + if (req.method === "GET") { + reads++ + if (duringRead) { const hook = duringRead; duringRead = undefined; void hook().then(() => res.end(JSON.stringify(catalogs[path] ?? [])), () => { res.statusCode = 503; res.end("{}") }); return } + if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_USER_PROVIDER"}'); return } + res.end(JSON.stringify(catalogs[path] ?? [])); return + } + if (req.method === "DELETE") { + const split = path.lastIndexOf("/"), collection = path.slice(0, split), id = decodeURIComponent(path.slice(split + 1)) + if (!["/ip/ipsec/identity", "/ip/ipsec/mode-config", "/certificate"].includes(collection) || !catalogs[collection].some(row => row[".id"] === id)) { res.statusCode = 404; res.end("{}"); return } + deletes++; dispatched++ + if (!(mode === "ignored" && dispatched === failAt)) catalogs[collection] = catalogs[collection].filter(row => row[".id"] !== id) + if (mode === "collateral" && dispatched === failAt) catalogs["/ip/ipsec/peer"][0].opaque = "changed" + if (mode === "lost" && dispatched === failAt) { req.socket.destroy(); return } + if (afterDelete) { const hook = afterDelete; afterDelete = undefined; void hook().then(() => res.end("{}"), () => { res.statusCode = 503; res.end("{}") }); return } + res.end("{}"); return + } + res.statusCode = 400; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = () => { mode = "normal"; dispatched = 0; catalogs = structuredClone(base) } + const wait = async (id: string) => { const deadline = Date.now() + 140000; do { const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; if (!["pending", "running"].includes(state)) return state; await delay(25) } while (Date.now() < deadline); throw Error("IPsec user operation live; never replay") } + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-user-delete-proof','127.0.0.1',$2,false,'NEVER_USER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]) + await rest.register(ipsecRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec user deletion", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.users.delete", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id; const principal = await authenticateMcpToken(access.token) + const prepare = async (removeCertificate = true, clientId = "identity:*i") => { const plan = await invokeMcpTool(principal, "mm_ipsec_user_remove_plan", { serverId, clientId, removeCertificate }) as { planId: string; state: string }; assert.equal(plan.state, "awaiting-approval"); assert.ok(!JSON.stringify(plan).includes("NEVER_")); return plan } + const reconcile = async (operationId: string) => await invokeMcpTool(principal, "mm_operation_reconcile", { operationId }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; configurationMatchesDesiredState: boolean; configurationPrefixCommands: number | null; verifiedPrefixCommands: number | null }[] } + const initialReads = reads + await assert.rejects(invokeMcpTool(principal, "mm_ipsec_user_remove_plan", { serverId: 1, clientId: "identity:*i" })) + await assert.rejects(invokeMcpTool(principal, "mm_ipsec_user_remove_plan", { serverId, clientId: "Alice" })); assert.equal(reads, initialReads) + for (const fault of ["normal", "lost", "ignored", "collateral"]) for (const stage of fault === "normal" ? [3] : [1, 2, 3]) { + reset(); const plan = await prepare(); await approvePlan(plan.planId, actor, true); mode = fault; failAt = stage + const before = deletes, key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await wait(operation.id), fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial") + assert.equal(deletes, before + (fault === "normal" ? 3 : stage)); assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + const checked = await reconcile(operation.id); assert.equal(checked.automaticRetry, false); assert.equal(checked.results[0].matchesDesiredState, fault === "normal"); assert.equal(checked.results[0].configurationMatchesDesiredState, fault === "normal" || fault === "lost" && stage === 3) + if (fault === "lost") { assert.equal(checked.results[0].configurationPrefixCommands, stage); assert.equal(checked.results[0].verifiedPrefixCommands, null) } + assert.equal(deletes, before + (fault === "normal" ? 3 : stage)) + if (fault === "normal") { + assert.deepEqual(catalogs["/ip/ipsec/identity"], [base["/ip/ipsec/identity"][1]]); assert.deepEqual(catalogs["/certificate"], base["/certificate"].slice(1)) + const saved = (await pool.query("SELECT step_index,intended_state,result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0] + for (const change of ["history", "intended", "native", "count"]) { + const wrongInput = structuredClone(saved.intended_state), wrongResult = structuredClone(saved.result) + if (change === "history") wrongResult.historyRecorded = false + if (change === "intended") wrongInput.input.removeCertificate = false + if (change === "native") wrongResult.clientId = "identity:*l" + if (change === "count") wrongResult.completedCommands = 2 + await pool.query("UPDATE mcp_operation_steps SET intended_state=$3::jsonb,result=$4::jsonb WHERE operation_id=$1 AND step_index=$2", [operation.id, saved.step_index, JSON.stringify(wrongInput), JSON.stringify(wrongResult)]) + const tampered = await reconcile(operation.id); assert.equal(tampered.results[0].configurationMatchesDesiredState, true); assert.equal(tampered.results[0].matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_steps SET intended_state=$3::jsonb,result=$4::jsonb WHERE operation_id=$1 AND step_index=$2", [operation.id, saved.step_index, JSON.stringify(saved.intended_state), JSON.stringify(saved.result)]) + } + } + } + reset(); catalogs["/ip/ipsec/identity"] = [catalogs["/ip/ipsec/identity"][1]] + const noop = await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/cert:*c?removeCertificate=false` }); assert.equal(noop.statusCode, 200); assert.equal(dispatched, 0) + const certOnly = await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/cert:*c` }); assert.equal(certOnly.statusCode, 200); assert.equal(dispatched, 1); assert.deepEqual(catalogs["/certificate"], base["/certificate"].slice(1)) + reset(); catalogs["/certificate"].push({ ".id": "*d", name: "descendant", ca: "ipsec-user-alice" }); await assert.rejects(prepare(), /цепочку/); assert.equal(dispatched, 0) + reset(); const aba = await prepare(); await approvePlan(aba.planId, actor, true) + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=$1", [serverId]); await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=$1", [serverId]) + assert.equal(await wait((await applyPlan(principal, aba.planId, randomUUID())).id), "rejected"); assert.equal(dispatched, 0) + reset(); const noCert = await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/identity:*i?removeCertificate=false` }); assert.equal(noCert.statusCode, 200); assert.equal(dispatched, 2); assert.deepEqual(catalogs["/certificate"], base["/certificate"]) + reset(); catalogs["/ip/ipsec/identity"][1]["remote-certificate"] = "ipsec-user-alice"; catalogs["/ip/ipsec/identity"][1]["mode-config"] = "mc-ipsec-alice" + await assert.rejects(prepare(), /другой identity/); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/identity:*i` })).statusCode, 409); assert.equal(dispatched, 0) + assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/identity:*i?removeCertificate=false` })).statusCode, 200); assert.equal(dispatched, 1); assert.deepEqual(catalogs["/ip/ipsec/mode-config"], base["/ip/ipsec/mode-config"]) + reset(); catalogs["/certificate"][0][".id"] = "*i" + assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/*i` })).statusCode, 409); assert.equal(dispatched, 0) + assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/cert:*i` })).statusCode, 200); assert.equal(dispatched, 3) + reset(); catalogs["/certificate"][1].crl = "true"; catalogs["/certificate"][0].issued = "true"; await assert.rejects(prepare(), /CRL/); assert.equal(dispatched, 0) + reset(); const protection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protection, JSON.stringify({ kind: "certificate", id: "*c", serverId, protection: "deny" })]) + await assert.rejects(prepare()); assert.equal((await rest.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/identity:*i` })).statusCode, 409); assert.equal(dispatched, 0); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); catalogs["/ip/ipsec/identity"][1].secret = "NEVER_LATE_DRIFT"; assert.equal(await wait((await applyPlan(principal, stale.planId, randomUUID())).id), "rejected"); assert.equal(dispatched, 0) + reset(); const revoked = await prepare(); await approvePlan(revoked.planId, actor, true); afterDelete = () => setConnectionStatus(access.connection.id, "paused", actor); const stopped = await applyPlan(principal, revoked.planId, randomUUID()); assert.equal(await wait(stopped.id), "unknown"); assert.equal(dispatched, 1); await setConnectionStatus(access.connection.id, "active", actor) + const staleReads = reads; await assert.rejects(reconcile(stopped.id)); assert.equal(reads, staleReads, "The pre-pause receipt is rejected before native reads after the policy version changes") + reset(); const freshReadPlan = await prepare(); await approvePlan(freshReadPlan.planId, actor, true) + const freshReadOperation = await applyPlan(principal, freshReadPlan.planId, randomUUID()); assert.equal(await wait(freshReadOperation.id), "applied") + const priorReads = reads; duringRead = () => setConnectionStatus(access.connection.id, "paused", actor); await assert.rejects(reconcile(freshReadOperation.id)); assert.equal(reads, priorReads + 1); await setConnectionStatus(access.connection.id, "active", actor) + reset(); const brokenHistory = Fastify(); brokenHistory.setValidatorCompiler(validatorCompiler); brokenHistory.setSerializerCompiler(serializerCompiler) + try { await brokenHistory.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async () => null }), { prefix: "/api" }); const result = await brokenHistory.inject({ method: "DELETE", url: `/api/ipsec/users/${serverId}/identity:*i` }); assert.equal(result.statusCode, 502); assert.equal(result.json().state, "applied"); assert.equal(result.json().historyRecorded, false); assert.equal(dispatched, 3) } finally { await brokenHistory.close() } + reset(); mode = "provider-error"; await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows).includes("NEVER_")) + console.log("✓ Shared IPsec user delete: full certificate optional cascade, namespaces, shared listener/mode-config/cert refs, CRL/protection, every-stage lost/ignored/collateral, checkpoints, history failure, late grant revoke, privacy and no replay") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM mcp_protections WHERE object_ref->>'serverId'=$1", [String(serverId)]); await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + await rest.close(); router.closeAllConnections(); await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/ipsec-user-delete-actions.test.ts b/backend/src/modules/mcp/ipsec-user-delete-actions.test.ts new file mode 100644 index 00000000..c4efb9c6 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-delete-actions.test.ts @@ -0,0 +1,11 @@ +import assert from "node:assert/strict" +import { hasVerifiedIpsecUserDeletion } from "./ipsec-user-delete-actions.js" +import { redact } from "./security.js" +const input = { principal: { connectionId: "connection" }, receiptId: "receipt", clientId: "*a1", removeCertificate: true, expectedStageProofs: ["b".repeat(64), "c".repeat(64), "d".repeat(64), "a".repeat(64)], commandCount: 3, expectedProof: "b".repeat(64) } +const result = { receiptId: input.receiptId, clientId: input.clientId, removeCertificate: true, verified: true, historyRecorded: true, revisionRecorded: true, commandCount: 3, completedCommands: 3, afterProof: input.expectedStageProofs.at(-1)! } +const row = { connection_id: "connection", state: "verified", intended_state: redact({ kind: "service", service: "network.ipsec.users.delete", input }), result } +assert.equal(hasVerifiedIpsecUserDeletion(row, input, input.expectedStageProofs.at(-1)!), true) +for (const patch of [{ state: "unknown" }, { state: "intent" }, { connection_id: "other" }, { intended_state: { ...row.intended_state, input: { ...input, removeCertificate: false } } }]) assert.equal(hasVerifiedIpsecUserDeletion({ ...row, ...patch }, input, input.expectedStageProofs.at(-1)!), false) +for (const patch of [{ receiptId: "other" }, { clientId: "*a2" }, { removeCertificate: false }, { historyRecorded: false }, { revisionRecorded: false }, { commandCount: 2 }, { completedCommands: 0 }, { afterProof: "c".repeat(64) }, { verified: false }]) assert.equal(hasVerifiedIpsecUserDeletion({ ...row, result: { ...result, ...patch } }, input, input.expectedStageProofs.at(-1)!), false) +assert.equal(hasVerifiedIpsecUserDeletion(row, input, input.expectedProof), false) +console.log("✓ IPsec reconciliation requires exact VERIFIED generic step, same connection/intended state/receipt/native ID/all acknowledged commands/history proof") diff --git a/backend/src/modules/mcp/ipsec-user-delete-actions.ts b/backend/src/modules/mcp/ipsec-user-delete-actions.ts new file mode 100644 index 00000000..30048e62 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-delete-actions.ts @@ -0,0 +1,80 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { deleteIpsecUserGuarded, readIpsecUserDeleteState, planIpsecUserDelete, ipsecUserDeleteStageProofs } from "../../services/ipsec-user-delete.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { routerClient } from "./resources.js" +import { accessGeneration, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const schema = z.object({ serverId: z.number().int().positive().safe(), clientId: z.string().regex(/^(identity|cert):\*[a-z0-9]+$/i).max(110), removeCertificate: z.boolean().default(true) }).strict() +const operation = "network.ipsec.users.delete" +async function access(input: Record, signal?: AbortSignal) { + if (signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение IPsec отменено", 409) + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const current = await effectiveMcpConnection(principal) + if (input.operationId !== operation || current.owner_id !== input.ownerId || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write") || !current.policy.scope.operationIds.includes(operation)) throw new McpError("PERMISSION_DENIED", "Допуск удаления IPsec отозван", 403) + if (!current.policy.scope.serverIds.includes(Number(input.serverId))) throw new McpError("SERVER_DENIED", "Сервер IPsec вне допуска", 403) + const request = schema.parse({ serverId: input.serverId, clientId: input.clientId, removeCertificate: input.removeCertificate }) + if (fingerprint(request) !== input.requestProof || fingerprint(current.policy) !== input.policyProof || fingerprint(current.owner_permissions) !== input.ownerProof || await accessGeneration() !== input.generation) throw new McpError("RESET_REQUIRED", "Допуск или запрос IPsec изменился", 409) + if (signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение IPsec отменено", 409) +} +async function source(input: Record, signal?: AbortSignal, allow: () => Promise = async () => {}) { + const authorize = async () => { await allow(); await access(input, signal) } + await authorize() + const { server } = await routerClient(Number(input.serverId)), state = await readIpsecUserDeleteState(server, authorize) + if (input.expectedSource !== undefined && input.expectedSource !== state.source) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился после подготовки", 409) + await authorize() + return { server, state, authorize } +} +export function hasVerifiedIpsecUserDeletion(candidate: unknown, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | null + return row.connection_id === (input.principal as { connectionId?: string } | undefined)?.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && result.receiptId === input.receiptId && result.verified === true && result.historyRecorded === true && result.revisionRecorded === true && result.clientId === input.clientId && result.removeCertificate === input.removeCertificate && result.commandCount === input.commandCount && result.completedCommands === input.commandCount && result.afterProof === proof && Array.isArray(input.expectedStageProofs) && proof === input.expectedStageProofs.at(-1) +} +export async function prepareIpsecUserDeletion(serverId: number, clientId: string, context: ActionContext, removeCertificate = true): Promise { + const request = schema.parse({ serverId, clientId, removeCertificate }) + const internal = { ...request, requestProof: fingerprint(request), principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions), generation: await accessGeneration(), operationId: operation, receiptId: randomUUID() } + const { state, server, authorize } = await source(internal, context.signal) + const commands = planIpsecUserDelete(state, server, clientId, removeCertificate) + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(serverId), serverId }, ...commands.map(command => ({ kind: ({ "/ip/ipsec/identity": "ipsec_identity", "/ip/ipsec/mode-config": "ipsec_mode_config", "/certificate": "certificate" })[command.path], id: command.id, serverId }))] + for (const item of state.protections as { object_ref: { kind: string; id: string } }[]) objects.push({ kind: item.object_ref.kind, id: item.object_ref.id, serverId }) + await authorize() + return { target: { scope: "servers", serverIds: [serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: commands.length + 1, before: { proof: state.proof }, + steps: [{ kind: "service", service: operation, input: { ...internal, expectedSource: state.source, expectedProof: state.proof, commandCount: commands.length, expectedStageProofs: await ipsecUserDeleteStageProofs(state, commands), proofRevision: 2, beforeFingerprint: fingerprint({ proof: state.proof }) } }], + warnings: [`Удаление клиента: ${commands.length} native DELETE и отдельная запись managed-only истории. removeCertificate=${removeCertificate}; CA, серверный сертификат, чужие ссылки и общие mode-config сохраняются.`, "Команды проверяются последовательно; операция не атомарна. Потеря ответа останавливает выполнение без повтора. Совпадение конфигурации отдельно от подтверждения команд и истории."] } + +} +export function registerIpsecUserDeleteActions(register: typeof add) { + const current = async (input: Record) => ({ proof: (await source(input)).state.proof }) + registerMcpService(operation, { current, + execute: async input => { + const before = await source(input) + return deleteIpsecUserGuarded(before.server, String(input.clientId), input.removeCertificate === true, String(input.expectedProof), async () => { await assertVerifiedConfigurationAccess(); await before.authorize() }, { receiptId: String(input.receiptId), checkpoint: async value => { + await assertVerifiedConfigurationAccess(); await before.authorize() + await audit(pool, String(input.ownerId), "ipsec.user.delete.checkpoint", (input.principal as { connectionId: string }).connectionId, { ...value, inputProof: fingerprint(redact(input)) }) + } }) + }, + verify: async (input, result) => { + const latest = await source(input), candidate = { connection_id: (input.principal as { connectionId: string }).connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result } + return hasVerifiedIpsecUserDeletion(candidate, input, latest.state.proof) + }, + reconcile: async (input, allow) => { + if (input.proofRevision !== 2 || !z.string().uuid().safeParse(input.receiptId).success || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof) || !Array.isArray(input.expectedStageProofs) || input.expectedStageProofs.length < 1 || input.expectedStageProofs.length > 4 || input.expectedStageProofs.some(value => typeof value !== "string" || !/^[a-f0-9]{64}$/.test(value)) || input.commandCount !== input.expectedStageProofs.length - 1) throw new McpError("UNSUPPORTED_CAPABILITY", "Нет совместимого private proof; требуется ручная сверка", 409) + const latest = await source(input, undefined, allow) + const generic = (await pool.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [(input.principal as { connectionId: string }).connectionId, input.receiptId])).rows + const verifiedFinal = generic.length === 1 && hasVerifiedIpsecUserDeletion(generic[0], input, latest.state.proof) + const final = await source(input, undefined, allow) + if (final.state.proof !== latest.state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился во время сверки", 409) + const stage = input.expectedStageProofs.indexOf(latest.state.proof) + const checkpoints = (await pool.query("SELECT payload FROM mcp_audit WHERE action='ipsec.user.delete.checkpoint' AND actor_id=$1 AND connection_id=$2 AND payload->>'receiptId'=$3 AND payload->>'inputProof'=$4 ORDER BY id DESC LIMIT 1", [input.ownerId, (input.principal as { connectionId: string }).connectionId, input.receiptId, fingerprint(redact(input))])).rows + const checkpoint = checkpoints[0]?.payload + return { matchesOriginalState: stage === 0, configurationMatchesDesiredState: stage === input.commandCount, configurationPrefixCommands: stage < 0 ? null : stage, verifiedPrefixCommands: checkpoint?.afterProof === latest.state.proof && checkpoint.commandCount === input.commandCount && checkpoint.completedCommands === stage ? stage : null, matchesDesiredState: verifiedFinal, verifiedFinalCheckpointAvailable: verifiedFinal, automaticReplay: false, forwardingVerified: false } + }, + }) + register("network.ipsec.users.delete", "mm_ipsec_user_remove_plan", "Удалить IPsec клиента, его персональный mode-config и необязательный сертификат", "network", "mm:network:write", "write", "servers", schema, { version: 2, prepare: (input, context) => prepareIpsecUserDeletion(Number(input.serverId), String(input.clientId), context, input.removeCertificate === true) }) +} diff --git a/backend/src/modules/mcp/ipsec-user-update-acceptance.ts b/backend/src/modules/mcp/ipsec-user-update-acceptance.ts new file mode 100644 index 00000000..38061323 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-update-acceptance.ts @@ -0,0 +1,142 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createIpsecRoutes } from "../../routes/ipsec.js" +import { getEnabledIpsecServerById } from "../../services/ipsec-live.js" +import { ipsecPeerDeletePaths } from "../../services/ipsec-peer-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { encryptSecret } from "./security.js" +import { ipsecUserPskPurpose } from "./ipsec-user-update-actions.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Serial SQL + loopback acceptance. No live RouterOS or forwarding assertion. */ +export async function acceptIpsecUserUpdate(actor: AuthUser) { + type Row = Record + const serverId = 198, secretIds: string[] = [], originalQuery = pool.query.bind(pool) + const pskPurpose = ipsecUserPskPurpose(serverId, "identity:*i") + const base: Record = { + ...Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", passive: "yes", profile: "profile", comment: "MikrotikManager:ipsec", opaque: "NEVER_PEER" }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:alice", "mode-config": "mc-ipsec-alice", comment: "MikrotikManager:ipsec user=Alice", disabled: "true", secret: "NEVER_OLD_PSK", opaque: "NEVER_IDENTITY" }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "mc-ipsec-alice", address: "192.0.2.1", opaque: "NEVER_OLD_MC" }, { ".id": "*s", name: "ipsec-vpn", "address-pool": "pool", opaque: "NEVER_SHARED_MC" }], + "/ip/pool": [{ ".id": "*l", name: "pool", ranges: "192.0.2.1-192.0.2.20" }], + "/ip/ipsec/profile": [{ ".id": "*f", name: "profile", opaque: "NEVER_PROFILE" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign", authority: "true" }, { ".id": "*c", name: "ipsec-user-Alice", "common-name": "Alice", "key-usage": "tls-client", ca: "ca", opaque: "NEVER_CLIENT_CERT" }, { ".id": "*v", name: "ipsec-server", "key-usage": "tls-server", ca: "ca" }], + } + let catalogs = structuredClone(base), mode = "normal", serial = 0, historyCalls = 0, connectionId: string | undefined + let afterAck: (() => Promise) | undefined, onRead: (() => Promise) | undefined + const commands: Array<{ method: string; path: string; fields: Row }> = [] + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")); res.setHeader("content-type", "application/json") + try { + if (req.method === "GET") { if (onRead) { const hook = onRead; onRead = undefined; await hook() }; res.end(JSON.stringify(catalogs[path] ?? [])); return } + const catalog = Object.keys(catalogs).find(value => path === value || path.startsWith(value + "/")) + if (!catalog || !["PUT", "PATCH", "DELETE"].includes(req.method ?? "")) { res.statusCode = 405; res.end("{}"); return } + let text = ""; for await (const chunk of req) text += chunk + const fields = text ? JSON.parse(text) as Row : {}, id = path.slice(catalog.length + 1) + commands.push({ method: req.method!, path, fields }) + let result: Row = {} + if (mode !== "ignored") { + if (req.method === "PUT") { result = { ".id": `*new${++serial}`, ...fields, "native-default": "NEVER_CREATE_DEFAULT" }; catalogs[catalog].push(result) } + else if (req.method === "PATCH") { const row = catalogs[catalog].find(value => value[".id"] === id); assert.ok(row); Object.assign(row, fields) } + else catalogs[catalog] = catalogs[catalog].filter(value => value[".id"] !== id) + } + if (mode === "collateral") catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_COLLATERAL" + if (afterAck) { const hook = afterAck; afterAck = undefined; await hook() } + if (mode === "lost") { req.socket.destroy(); return } + res.end(JSON.stringify(result)) + } catch { res.statusCode = 500; res.end('{"error":"NEVER_PROVIDER"}') } + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = () => { mode = "normal"; catalogs = structuredClone(base); afterAck = undefined; onRead = undefined } + const certOnly = () => { reset(); catalogs["/ip/ipsec/identity"] = [{ ".id": "*listener", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "mode-config": "ipsec-vpn" }] } + const wait = async (id: string) => { for (const deadline = Date.now() + 140000; Date.now() < deadline;) { const row = (await originalQuery("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) }; throw Error("IPsec user update remains live; never replay") } + const secret = async (purpose: string, owner = actor.id) => { const id = randomUUID(); secretIds.push(id); await originalQuery("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, purpose, serverId, await encryptSecret("NEVER_USER_PSK"), owner]); return id } + try { + assert.equal(Number((await originalQuery("SELECT count(*)::text AS n FROM servers WHERE id=$1", [serverId])).rows[0].n), 0) + await originalQuery("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'ipsec-user-update','127.0.0.1',$2,false,'NEVER_ENDPOINT')", [serverId, (router.address() as { port: number }).port]) + await originalQuery("INSERT INTO app_users(id,login,name) VALUES('ipsec-update-other-owner','ipsec-update-other-owner','IPsec fixture')") + await rest.register(createIpsecRoutes({ findServer: getEnabledIpsecServerById, recordRevision: async input => { historyCalls++; assert.ok(await input.capture()); return { id: "fixture-history" } as never } }), { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "IPsec user update", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["network.ipsec.users.update", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = (patch: Record = {}, clientId = "identity:*i") => invokeMcpTool(principal, "mm_ipsec_user_edit_plan", { serverId, clientId, patch }) as Promise<{ planId: string; state: string }> + const apply = async (patch: Record, clientId = "identity:*i") => { + const plan = await prepare(patch, clientId); assert.equal(plan.state, "awaiting-approval"); assert.ok(!JSON.stringify(plan).includes("NEVER_")); await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key), state = await wait(operation.id) + return { plan, key, operation, ...state } + } + const receipt = async (id: string) => (await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [id])).rows[0].result + const noReplay = async (result: Awaited>, sourceChanged = false) => { + const before = commands.length; await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, before) + if (sourceChanged) await assert.rejects(invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }), (error: unknown) => !!error && typeof error === "object" && "code" in error && ["STATE_CONFLICT", "RESET_REQUIRED"].includes(String(error.code))) + else { + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean }[] } + assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, false) + } + assert.equal(commands.length, before) + } + for (const patch of [{ name: "Bob", staticIp: "" }, { psk: "NEVER_RAW_PSK" }]) { const before = commands.length; await assert.rejects(prepare(patch)); const response = await rest.inject({ method: "PATCH", url: `/api/ipsec/users/${serverId}/identity:*i`, payload: { name: "Bob", staticIp: "" } }); assert.equal(response.statusCode, 400); assert.equal(commands.length, before) } + const unapproved = await prepare({ disabled: false }); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(commands.length, 0) + const pskRef = await secret(pskPurpose) + const full = { name: "Bob", staticIp: "192.0.2.9", remoteId: "bob", disabled: false, pskSecretRef: pskRef } + let before = commands.length, result = await apply(full) + assert.equal(result.state, "applied"); assert.deepEqual(commands.slice(before).map(row => row.method), ["PATCH", "PUT", "PATCH", "DELETE", "PATCH"]) + assert.equal(catalogs["/ip/ipsec/identity"][0].secret, "NEVER_USER_PSK"); assert.equal(catalogs["/ip/ipsec/identity"][0]["remote-id"], "key-id:bob"); assert.equal(catalogs["/ip/ipsec/identity"][0]["mode-config"], "mc-ipsec-bob") + assert.equal(catalogs["/ip/ipsec/mode-config"].some(row => row.name === "mc-ipsec-alice"), false) + assert.equal((await receipt(result.operation.id)).historyRecorded, true) + const successReconcile = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { results: { matchesDesiredState: boolean }[] }; assert.equal(successReconcile.results[0].matchesDesiredState, true) + const fullCommands = commands.slice(before).map(row => ({ ...row, path: row.path.replace(/\*new\d+/, "*created") })) + reset(); before = commands.length + const response = await rest.inject({ method: "PATCH", url: `/api/ipsec/users/${serverId}/identity:*i`, payload: { name: "Bob", staticIp: "192.0.2.9", remoteId: "bob", disabled: false, psk: "NEVER_USER_PSK" } }); assert.equal(response.statusCode, 200, response.body); assert.equal(historyCalls, 1) + assert.deepEqual(commands.slice(before).map(row => ({ ...row, path: row.path.replace(/\*new\d+/, "*created") })), fullCommands) + for (const patch of [{}, { name: "Alice" }]) { reset(); before = commands.length; result = await apply(patch); assert.equal(result.state, "applied"); assert.equal(commands.length, before); assert.equal((await receipt(result.operation.id)).historyRecorded, true) } + reset(); result = await apply({ staticIp: null }); assert.equal(result.state, "applied"); assert.equal(catalogs["/ip/ipsec/identity"][0]["mode-config"], "ipsec-vpn"); assert.equal(catalogs["/ip/ipsec/mode-config"].length, 1) + reset(); catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", comment: "Other", "mode-config": "mc-ipsec-alice" }); before = commands.length; result = await apply({ staticIp: null }); assert.equal(result.state, "applied"); assert.equal(commands.length, before + 1); assert.ok(catalogs["/ip/ipsec/mode-config"].some(row => row.name === "mc-ipsec-alice")) + reset(); const foreign = { ".id": "*foreign", name: "mc-ipsec-bob", address: "192.0.2.8", "address-pool": "pool", "static-dns": "192.0.2.53", opaque: "NEVER_FOREIGN" }; catalogs["/ip/ipsec/mode-config"].push(structuredClone(foreign)); catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", comment: "Other", "mode-config": "mc-ipsec-bob" }); result = await apply({ name: "Bob", staticIp: "192.0.2.9" }); assert.equal(result.state, "applied"); assert.deepEqual(catalogs["/ip/ipsec/mode-config"].find(row => row[".id"] === "*foreign"), { ...foreign, address: "192.0.2.9", responder: "yes", "address-prefix-length": "32" }) + certOnly(); before = commands.length; result = await apply({ disabled: true, remoteId: "ignored", staticIp: null }, "cert:*c"); assert.equal(result.state, "applied"); assert.equal(commands.length, before) + for (const patch of [{ name: "Bob" }, { name: "Bob", staticIp: "192.0.2.9" }]) { certOnly(); before = commands.length; result = await apply(patch, "cert:*c"); assert.equal(result.state, "applied"); assert.equal(commands.length, before + ("staticIp" in patch ? 2 : 1)); assert.equal(catalogs["/ip/ipsec/identity"].at(-1)?.["remote-certificate"], "ipsec-user-Alice") } + reset(); const wrongPurpose = await secret("wrong-purpose"); before = commands.length; await assert.rejects(prepare({ pskSecretRef: wrongPurpose })); assert.equal(commands.length, before) + const wrongOwner = await secret(pskPurpose, "ipsec-update-other-owner"); await assert.rejects(prepare({ pskSecretRef: wrongOwner })); assert.equal(commands.length, before) + const wrongTarget = await secret(ipsecUserPskPurpose(serverId, "identity:*other")); await assert.rejects(prepare({ pskSecretRef: wrongTarget })); assert.equal(commands.length, before) + const deleted = await secret(pskPurpose), lateSecret = await prepare({ pskSecretRef: deleted }); await approvePlan(lateSecret.planId, actor, true); await originalQuery("DELETE FROM mcp_secrets WHERE id=$1", [deleted]); assert.notEqual((await wait((await applyPlan(principal, lateSecret.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, before) + reset(); const revokedAfterAck = await secret(pskPurpose); before = commands.length + afterAck = () => originalQuery("DELETE FROM mcp_secrets WHERE id=$1", [revokedAfterAck]) + result = await apply({ name: "Bob", staticIp: "192.0.2.9", pskSecretRef: revokedAfterAck }) + assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1, "revoked PSK ref stops before the next command") + await applyPlan(principal, result.plan.planId, result.key); assert.equal(commands.length, before + 1) + await assert.rejects(invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }), error => (error as {code?:string}).code === "SECRET_UNAVAILABLE") + assert.equal(commands.length, before + 1) + for (const fault of ["ignored", "lost", "collateral"]) { reset(); mode = fault; before = commands.length; result = await apply({ name: "Bob", staticIp: "192.0.2.9" }); assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1); await noReplay(result) } + reset(); const stale = await prepare({ name: "Bob" }); await approvePlan(stale.planId, actor, true); catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_LATE_OPAQUE"; before = commands.length; assert.notEqual((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, before) + reset(); before = commands.length; afterAck = async () => { catalogs["/ip/ipsec/profile"][0].opaque = "NEVER_AFTER_ACK" }; result = await apply({ name: "Bob", staticIp: "192.0.2.9" }); assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1); await noReplay(result) + reset(); onRead = () => originalQuery("UPDATE servers SET password='changed-source' WHERE id=$1", [serverId]); before = commands.length; await assert.rejects(prepare({ name: "Bob" })); assert.equal(commands.length, before); await originalQuery("UPDATE servers SET password='NEVER_ENDPOINT' WHERE id=$1", [serverId]) + reset(); const staleSource = await prepare({ name: "Bob" }); await approvePlan(staleSource.planId, actor, true); before = commands.length + await originalQuery("UPDATE servers SET password='changed-source' WHERE id=$1", [serverId]) + try { assert.notEqual((await wait((await applyPlan(principal, staleSource.planId, randomUUID())).id)).state, "applied"); assert.equal(commands.length, before) } finally { await originalQuery("UPDATE servers SET password='NEVER_ENDPOINT' WHERE id=$1", [serverId]) } + reset(); const staleProtection = await prepare({ name: "Bob" }); await approvePlan(staleProtection.planId, actor, true); before = commands.length + const lateProtection = randomUUID(); await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [lateProtection, JSON.stringify({ kind: "ipsec_identity", id: "*i", serverId, protection: "deny" }), actor.id]) + try { await assert.rejects(applyPlan(principal, staleProtection.planId, randomUUID()), error => (error as {code?:string}).code === "PROTECTED_OBJECT"); assert.equal(commands.length, before) } finally { await originalQuery("DELETE FROM mcp_protections WHERE id=$1", [lateProtection]) } + reset(); before = commands.length; afterAck = () => originalQuery("UPDATE servers SET password='changed-source' WHERE id=$1", [serverId]) + try { result = await apply({ name: "Bob", staticIp: "192.0.2.9" }); assert.ok(["partial", "unknown"].includes(result.state)); assert.equal(commands.length, before + 1); await noReplay(result, true) } finally { await originalQuery("UPDATE servers SET password='NEVER_ENDPOINT' WHERE id=$1", [serverId]) } + reset(); const protection = randomUUID(); await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ipsec_identity", id: "*i", serverId, protection: "deny" }), actor.id]) + try { before = commands.length; await assert.rejects(prepare({ name: "Bob" })); assert.equal((await rest.inject({ method: "PATCH", url: `/api/ipsec/users/${serverId}/identity:*i`, payload: { name: "Bob" } })).statusCode, 409); assert.equal(commands.length, before) } finally { await originalQuery("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + for (const table of ["mcp_audit", "mcp_plans", "mcp_operation_steps"]) { + const rows = table === "mcp_operation_steps" ? await originalQuery("SELECT s.* FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1", [connectionId]) : await originalQuery(`SELECT * FROM ${table} WHERE connection_id=$1`, [connectionId]) + assert.ok(!JSON.stringify(rows.rows).includes("NEVER_"), `${table} leaked private fixture data`) + } + console.log("✓ IPsec user update SQL/loopback: full REST parity, mode-config upsert/cleanup/shared consumers, cert promotion/ignored fields/noop history, purpose-bound PSK refs, drift/fault stop and no replay; native forwarding gate remains open") + } finally { + pool.query = originalQuery as typeof pool.query + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await rest.close(); await originalQuery("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])", [secretIds]); await originalQuery("DELETE FROM app_users WHERE id='ipsec-update-other-owner'"); await originalQuery("DELETE FROM servers WHERE id=$1", [serverId]); router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) + } +} + diff --git a/backend/src/modules/mcp/ipsec-user-update-actions.test.ts b/backend/src/modules/mcp/ipsec-user-update-actions.test.ts new file mode 100644 index 00000000..a9f45844 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-update-actions.test.ts @@ -0,0 +1,102 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import type { ActionContext } from "./action-types.js" +import type { ConnectionRow } from "./store.js" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import type { IpsecPeerDeleteState } from "../../services/ipsec-peer-delete.js" +import { createIpsecUserUpdateAdapter, ipsecUserUpdateActionSchema, ipsecUserPskPurpose } from "./ipsec-user-update-actions.js" +import { redact } from "./security.js" + +const operation = "network.ipsec.users.update", sourceProof = "a".repeat(64), beforeProof = "b".repeat(64), afterProof = "c".repeat(64) +const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } +const connection = { id: principal.connectionId, owner_id: "fixture-owner", owner_permissions: ["mm:network:write"], generation: "fixture-generation", owner_valid_until: new Date(Date.now() + 600000), policy: { mode: "autonomous", status: "active", expiresAt: principal.credentialExpiresAt, permissions: ["mm:network:write"], scope: { serverIds: [1], operationIds: [operation], allowGlobal: false } } } as ConnectionRow +const context = { principal, connection } as ActionContext +const request = { serverId: 1, clientId: "identity:*i1", patch: {} } +assert.equal(ipsecUserPskPurpose(1, request.clientId), "ipsec-user-psk:1:identity:*i1") +assert.equal(ipsecUserUpdateActionSchema.safeParse({ ...request, patch: { psk: "NEVER_RAW_PSK" } }).success, false) +assert.equal(ipsecUserUpdateActionSchema.safeParse({ ...request, patch: { pskSecretRef: randomUUID(), staticIp: null, disabled: false, remoteId: "", name: "new" } }).success, true) +assert.equal(ipsecUserUpdateActionSchema.safeParse({ ...request, patch: { unknown: true } }).success, false) + +const originalQuery = pool.query, originalConnect = pool.connect +const forbidden = () => { throw Error("Unexpected real SQL/native/network access") } +pool.query = forbidden as typeof pool.query; pool.connect = forbidden as typeof pool.connect +try { + let current = structuredClone(connection), state: IpsecPeerDeleteState, endpoint = sourceProof, reads = 0, commands = 0, histories = 0, ignored: string[] = [], failHistory = false + let secretRow: Record | undefined, onRead: (() => void) | undefined, beforeCommand: (() => void) | undefined + let rows: Record[] = [], latestInput: Record | undefined, latestReceipt: Record | undefined, receivedPatch: unknown + const sealed = new Map() + const reset = () => { + current = structuredClone(connection); endpoint = sourceProof; ignored = []; failHistory = false; secretRow = undefined; onRead = undefined; beforeCommand = undefined; rows = [] + state = { source: sourceProof, proof: beforeProof, catalogs: {}, protections: [{ object_ref: { kind: "ipsec_identity", id: "*protected" } }] } + } + reset() + const adapter = createIpsecUserUpdateAdapter({ + connection: async () => current, + router: async () => ({ server: { id: 1 } as ServerRow, client: {} as never }), + sourceProof: async () => endpoint, + read: async (_server, authorize) => { await authorize?.(); reads++; onRead?.(); await authorize?.(); return structuredClone(state) }, + seal: async value => { const key = `sealed:${randomUUID()}`; sealed.set(key, value); return key }, + open: async value => { if (value.startsWith("psk-cipher")) return "NEVER_PLAINTEXT_PSK"; const found = sealed.get(value); if (!found) throw Error("invalid ciphertext"); return found }, + query: async sql => sql.includes("mcp_secrets") ? secretRow ? [structuredClone(secretRow)] : [] : rows, + verifiedAccess: async () => {}, checkpoint: async () => {}, + plan: ((_state: unknown, _server: unknown, clientId: string, patch: Record) => ({ commands: Object.keys(patch).length ? [{ action: "update", path: "/ip/ipsec/identity", id: "*i1", fields: {} }] : [], companions: [{ kind: "ipsec_peer", id: "*p1" }], target: { kind: "ipsec_user", id: clientId }, ignoredFields: ignored })) as never, + update: (async (_server: unknown, clientId: string, patch: Record, expected: string, authorize: () => Promise, options: { receiptId: string; checkpoint: (value: unknown) => Promise }) => { + assert.equal(expected, beforeProof); receivedPatch = patch + await authorize(); beforeCommand?.(); await authorize() + const count = Object.keys(patch).length ? 1 : 0; commands += count; histories++ + state.proof = afterProof + const result = { ok: true, verified: true, receiptId: options.receiptId, clientId, commandCount: count, completedCommands: count, confirmedWrites: count, afterProof, historyRecorded: !failHistory, revisionRecorded: !failHistory, automaticReplay: false, forwardingVerified: false } + await options.checkpoint({ receiptId: options.receiptId, commandCount: count, completedCommands: count, afterProof }); latestReceipt = result + return result + }) as never, + }) + const input = async (patch: Record = {}) => { + const evidence = await adapter.prepare({ ...request, patch }, context) + assert.equal(evidence.requiresHuman, true) + assert.equal(evidence.mutationCost, Object.keys(patch).length ? 2 : 1) + assert.ok(evidence.objects.some(ref => ref.kind === "ipsec_peer" && ref.id === "*p1")) + assert.ok(evidence.objects.some(ref => ref.id === "*protected")) + assert.ok(!JSON.stringify(evidence).includes("NEVER_PLAINTEXT")) + const step = evidence.steps[0]!; assert.equal(step.kind, "service") + if (step.kind !== "service") throw Error("Expected service") + latestInput = step.input; return { input: step.input, evidence } + } + let prepared = await input() + assert.deepEqual(await adapter.service.current(prepared.input), { proof: beforeProof }) + let receipt = await adapter.service.execute(prepared.input) + assert.equal(commands, 0); assert.equal(histories, 1); assert.equal(await adapter.service.verify!(prepared.input, receipt), true) + reset(); prepared = await input({ disabled: false, remoteId: "" }); receipt = await adapter.service.execute(prepared.input) + assert.deepEqual(receivedPatch, { disabled: false, remoteId: "" }); assert.equal(await adapter.service.verify!(prepared.input, receipt), true) + rows = [{ connection_id: principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input: prepared.input }), result: receipt }] + assert.equal((await adapter.service.reconcile!(prepared.input, async () => {})).matchesDesiredState, true) + for (const key of ["historyRecorded", "revisionRecorded", "verified", "completedCommands", "confirmedWrites", "receiptId", "clientId", "afterProof"]) { + const saved = structuredClone(rows[0]!) + ;(rows[0]!.result as Record)[key] = "tampered" + assert.equal((await adapter.service.reconcile!(prepared.input, async () => {})).matchesDesiredState, false, key) + rows = [saved] + } + rows[0]!.state = "applied"; assert.equal((await adapter.service.reconcile!(prepared.input, async () => {})).matchesDesiredState, false) + reset(); prepared = await input({ disabled: true }); failHistory = true; receipt = await adapter.service.execute(prepared.input) + assert.equal(await adapter.service.verify!(prepared.input, receipt), false, "native success without mandatory history never VERIFIED") + reset(); ignored = ["remoteId", "disabled"]; prepared = await input({ remoteId: "ignored", disabled: true }); assert.ok(prepared.evidence.warnings.some(text => text.includes("remoteId, disabled"))) + + reset(); const secretId = randomUUID() + const goodSecret = { purpose: ipsecUserPskPurpose(1, request.clientId), server_id: 1, created_by: connection.owner_id, ciphertext: "psk-cipher-v1" } + secretRow = { ...goodSecret }; prepared = await input({ pskSecretRef: secretId }) + assert.ok(!sealed.get(String(prepared.input.bindingCiphertext))!.includes("NEVER_PLAINTEXT_PSK"), "binding stores reference metadata only") + receipt = await adapter.service.execute(prepared.input); assert.deepEqual(receivedPatch, { psk: "NEVER_PLAINTEXT_PSK" }); assert.equal(await adapter.service.verify!(prepared.input, receipt), true) + for (const mismatch of [{ created_by: "another-owner" }, { server_id: 2 }, { purpose: "ipsec-user-psk:1:other" }, { ciphertext: "psk-cipher-v2" }]) { + reset(); secretRow = { ...goodSecret }; prepared = await input({ pskSecretRef: secretId }); const before = reads + secretRow = { ...goodSecret, ...mismatch }; await assert.rejects(adapter.service.current(prepared.input)); assert.equal(reads, before, "secret drift rejects before native GET") + } + reset(); secretRow = { ...goodSecret }; prepared = await input({ pskSecretRef: secretId }); beforeCommand = () => { secretRow = undefined }; const beforeCommands = commands + await assert.rejects(adapter.service.execute(prepared.input)); assert.equal(commands, beforeCommands, "revocation before command prevents dispatch") + for (const mutate of [() => { endpoint = "d".repeat(64) }, () => { current.policy.permissions = [] }, () => { current.policy.scope.operationIds = [] }, () => { current.generation = "new-generation" }]) { + reset(); prepared = await input({ disabled: true }); const before = reads; mutate(); await assert.rejects(adapter.service.current(prepared.input)); assert.equal(reads, before) + } + reset(); prepared = await input(); await assert.rejects(adapter.service.current({ ...prepared.input, serverId: 2 })); await assert.rejects(adapter.service.current({ ...prepared.input, receiptId: randomUUID() })) + const cancelled = new AbortController(); cancelled.abort(); await assert.rejects(adapter.prepare(request, { ...context, signal: cancelled.signal })) + assert.ok(latestInput && latestReceipt) + console.log("IPsec user MCP adapter: strict secret refs, owner/purpose/server/cipher rechecks, pre-GET source/access gates, history-only patch, sealed binding, receipt reconciliation and mandatory history fail closed") +} finally { pool.query = originalQuery; pool.connect = originalConnect } diff --git a/backend/src/modules/mcp/ipsec-user-update-actions.ts b/backend/src/modules/mcp/ipsec-user-update-actions.ts new file mode 100644 index 00000000..eda210d5 --- /dev/null +++ b/backend/src/modules/mcp/ipsec-user-update-actions.ts @@ -0,0 +1,136 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { ipsecUserPatchSchema } from "@mmapp/contracts/ipsec" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { ipsecPeerDeleteSourceProof } from "../../services/ipsec-peer-delete.js" +import { planIpsecUserUpdate } from "../../services/ipsec-user-update-model.js" +import { readIpsecUserUpdateState, updateIpsecUserGuarded } from "../../services/ipsec-user-update.js" +import { routerClient } from "./resources.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { encryptSecret, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" +import type { McpServiceHandler } from "./service-registry.js" + +const operation = "network.ipsec.users.update" +export const ipsecUserPskPurpose = (serverId: number, clientId: string) => `ipsec-user-psk:${serverId}:${clientId}` +const hash = z.string().regex(/^[a-f0-9]{64}$/) +export const ipsecUserUpdatePlanPatchSchema = ipsecUserPatchSchema.omit({ psk: true }).extend({ pskSecretRef: z.string().uuid().optional() }).strict() +export const ipsecUserUpdateActionSchema = z.object({ serverId: z.number().int().positive().safe(), clientId: z.string().min(1).max(200).regex(/^[^\x00-\x1f\x7f]+$/), patch: ipsecUserUpdatePlanPatchSchema }).strict() +const bindingSchema = ipsecUserUpdateActionSchema.extend({ + operation: z.literal(operation), principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict(), + ownerId: z.string().min(1), ownerProof: hash, policyProof: hash, generation: z.string().min(1), sourceProof: hash, expectedProof: hash, + referenceProof: hash, requestProof: hash, commandCount: z.number().int().min(0).max(100), receiptId: z.string().uuid(), proofRevision: z.literal(2), +}).strict() +type Binding = z.infer +const serviceInput = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), bindingCiphertext: z.string().min(1).max(262144) }).strict() +const production = { + connection: effectiveMcpConnection, router: routerClient, read: readIpsecUserUpdateState, sourceProof: ipsecPeerDeleteSourceProof, + plan: planIpsecUserUpdate, update: updateIpsecUserGuarded, seal: encryptSecret, open: decryptSecret, verifiedAccess: assertVerifiedConfigurationAccess, + query: async (sql: string, values: unknown[]) => (await pool.query(sql, values)).rows as Record[], + checkpoint: async (ownerId: string, connectionId: string, value: unknown) => audit(pool, ownerId, "ipsec.user.update.checkpoint", connectionId, value), +} +const conflict = () => new McpError("STATE_CONFLICT", "Клиент IPsec, источник, ссылка секрета или допуск изменились", 409) + +export function hasVerifiedIpsecUserUpdate(candidate: unknown, binding: Binding, input: Record, proof: string): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | undefined + return row.connection_id === binding.principal.connectionId && row.state === "verified" && fingerprint(row.intended_state) === fingerprint(redact({ kind: "service", service: operation, input })) && !!result && + result.ok === true && result.verified === true && result.receiptId === binding.receiptId && result.clientId === binding.clientId && result.commandCount === binding.commandCount && result.completedCommands === binding.commandCount && result.confirmedWrites === binding.commandCount && + result.historyRecorded === true && result.revisionRecorded === true && result.afterProof === proof && result.forwardingVerified === false && result.automaticReplay === false +} + +/** Private binding never contains plaintext PSK. Every material resolution checks + * its current owner/purpose/server/cipher metadata before another native access. */ +export function createIpsecUserUpdateAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const access = async (binding: Binding, signal?: AbortSignal, external: () => Promise = async () => {}) => { + signal?.throwIfAborted(); await external() + const current = await deps.connection(binding.principal) + if (Date.parse(binding.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== binding.ownerId || current.policy.status !== "active" || current.policy.mode === "observer" || current.owner_valid_until.getTime() <= Date.now() || Date.parse(current.policy.expiresAt) <= Date.now() || !current.policy.scope.operationIds.includes(operation) || !current.policy.scope.serverIds.includes(binding.serverId) || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска изменения клиента IPsec", 403) + if (current.generation !== binding.generation || fingerprint(current.policy) !== binding.policyProof || fingerprint(current.owner_permissions) !== binding.ownerProof || fingerprint({ serverId: binding.serverId, clientId: binding.clientId, patch: binding.patch }) !== binding.requestProof) throw conflict() + signal?.throwIfAborted() + } + const material = async (binding: Binding, signal?: AbortSignal, external?: () => Promise, compare = true) => { + await access(binding, signal, external) + const { pskSecretRef, ...patch } = binding.patch + let referenceProof = fingerprint([]), psk: string | undefined + if (pskSecretRef) { + const rows = await deps.query("SELECT purpose,server_id,created_by,CASE WHEN octet_length(ciphertext)<=16384 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2 LIMIT 2", [pskSecretRef, binding.ownerId]) + const row = rows[0] + if (rows.length !== 1 || !row || row.created_by !== binding.ownerId || row.server_id !== binding.serverId || row.purpose !== ipsecUserPskPurpose(binding.serverId, binding.clientId) || typeof row.ciphertext !== "string") throw new McpError("SECRET_UNAVAILABLE", "Ссылка PSK не принадлежит владельцу, серверу и клиенту IPsec", 409) + referenceProof = fingerprint({ id: pskSecretRef, ...row }) + if (compare && referenceProof !== binding.referenceProof) throw conflict() + const value = await deps.open(row.ciphertext).catch(() => { throw new McpError("SECRET_UNAVAILABLE", "Секрет PSK недоступен", 409) }) + if (!z.string().min(8).max(16384).safeParse(value).success) throw new McpError("SECRET_UNAVAILABLE", "Неподдерживаемый формат PSK", 409) + psk = value + } + if (compare && referenceProof !== binding.referenceProof) throw conflict() + await access(binding, signal, external) + return { patch: { ...patch, ...(psk === undefined ? {} : { psk }) }, referenceProof } + } + const request = async (input: Record) => { + const parsed = serviceInput.parse(input), binding = bindingSchema.parse(JSON.parse(await deps.open(parsed.bindingCiphertext))) + if (binding.serverId !== parsed.serverId || binding.receiptId !== parsed.receiptId) throw conflict() + return binding + } + const source = async (binding: Binding, signal?: AbortSignal, external?: () => Promise) => { + const authorize = async () => { await material(binding, signal, external) } + await authorize() + const { server } = await deps.router(binding.serverId) + if (await deps.sourceProof(server) !== binding.sourceProof) throw conflict() + const state = await deps.read(server, authorize) + if (state.source !== binding.sourceProof) throw conflict() + await authorize(); return { server, state, authorize, patch: (await material(binding, signal, external)).patch } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + const value = ipsecUserUpdateActionSchema.parse(JSON.parse(JSON.stringify(ipsecUserUpdateActionSchema.parse(raw)))) + const binding: Binding = bindingSchema.parse({ ...value, operation, principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), generation: context.connection.generation, sourceProof: "0".repeat(64), expectedProof: "0".repeat(64), referenceProof: fingerprint([]), requestProof: fingerprint(value), commandCount: 0, receiptId: randomUUID(), proofRevision: 2 }) + const resolved = await material(binding, context.signal, undefined, false) + binding.referenceProof = resolved.referenceProof + const { server } = await deps.router(value.serverId) + binding.sourceProof = await deps.sourceProof(server) + const before = await source(binding, context.signal), plan = deps.plan(before.state, server, value.clientId, resolved.patch) + binding.expectedProof = before.state.proof; binding.commandCount = plan.commands.length + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(value.serverId), serverId: value.serverId }, { kind: "ipsec_user", id: value.clientId, serverId: value.serverId }, { ...plan.target, serverId: value.serverId }, ...plan.companions.map(ref => ({ ...ref, serverId: value.serverId }))] + for (const protection of before.state.protections as Array<{ object_ref: { kind: string; id: string } }>) objects.push({ kind: protection.object_ref.kind, id: protection.object_ref.id, serverId: value.serverId }) + if ((await source(binding, context.signal)).state.proof !== binding.expectedProof) throw conflict() + return { target: { scope: "servers", serverIds: [value.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: plan.commands.length + 1, before: { proof: before.state.proof }, + steps: [{ kind: "service", service: operation, input: { serverId: value.serverId, receiptId: binding.receiptId, bindingCiphertext: await deps.seal(JSON.stringify(binding)) } }], + warnings: [`Native-команд: ${plan.commands.length}; затем обязательная managed-only история IPsec. Пустой patch записывает только историю.`, ...(plan.ignoredFields.length ? [`Для выбранного клиента поля не меняют конфигурацию: ${plan.ignoredFields.join(", ")}.`] : []), "PSK принимается только через owner/purpose/server/client secret reference; закрытые данные не возвращаются модели.", "Каждая команда проверяет полный private proof, источник, защиты и ссылку секрета. Потеря ответа или несовпадение read-back останавливают операцию без повтора; передача трафика не подтверждается."] } + } + const service: McpServiceHandler = { + current: async input => ({ proof: (await source(await request(input))).state.proof }), + execute: async input => { + const binding = await request(input), before = await source(binding) + if (before.state.proof !== binding.expectedProof) throw conflict() + const plan = deps.plan(before.state, before.server, binding.clientId, before.patch) + if (plan.commands.length !== binding.commandCount) throw conflict() + const authorize = async () => { await deps.verifiedAccess(); await before.authorize() } + await authorize() + return deps.update(before.server, binding.clientId, before.patch, binding.expectedProof, authorize, { receiptId: binding.receiptId, checkpoint: async value => { await authorize(); await deps.checkpoint(binding.ownerId, binding.principal.connectionId, { ...value, inputProof: fingerprint(redact(input)) }) } }) + }, + verify: async (input, result) => { + const binding = await request(input), after = await source(binding) + return hasVerifiedIpsecUserUpdate({ connection_id: binding.principal.connectionId, state: "verified", intended_state: redact({ kind: "service", service: operation, input }), result }, binding, input, after.state.proof) + }, + reconcile: async (input, external) => { + const binding = await request(input), latest = await source(binding, undefined, external) + await latest.authorize() + const rows = await deps.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [binding.principal.connectionId, binding.receiptId]) + const verified = rows.length === 1 && hasVerifiedIpsecUserUpdate(rows[0], binding, input, latest.state.proof) + if ((await source(binding, undefined, external)).state.proof !== latest.state.proof) throw conflict() + return { matchesOriginalState: latest.state.proof === binding.expectedProof, matchesDesiredState: verified, automaticReplay: false, forwardingVerified: false } + }, + } + return { prepare, service } +} + +export function registerIpsecUserUpdateActions(register: typeof add) { + const adapter = createIpsecUserUpdateAdapter() + registerMcpService(operation, adapter.service) + register(operation, "mm_ipsec_user_edit_plan", "Изменить клиента IPsec с проверкой всех native-команд и managed-only историей", "network", "mm:network:write", "write", "servers", ipsecUserUpdateActionSchema, { version: 2, prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/ipv6-nd-actions.ts b/backend/src/modules/mcp/ipv6-nd-actions.ts new file mode 100644 index 00000000..2856b193 --- /dev/null +++ b/backend/src/modules/mcp/ipv6-nd-actions.ts @@ -0,0 +1,85 @@ +import { z } from "zod" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" +import { readRosSnapshot, resourceById, validateRosStep, type ConfigurationRow } from "./resources.js" +import { ospfInterfaceResources } from "./routing-dependencies.js" +import { observedBoolean, observedDurationMs } from "../../services/ospf-observed-values.js" +import { McpError } from "./security.js" + +const rowId = z.string().regex(/^\*[a-z0-9]+$/i).max(200) +const serverId = z.number().int().positive().safe() +const enabledFlag = (value: unknown) => observedBoolean(typeof value === "boolean" ? String(value) : typeof value === "string" ? value : undefined) === true + +function checkTimings(resource: string, row: ConfigurationRow) { + const millis = (key: string) => row[key] === "infinity" ? Infinity : typeof row[key] === "string" ? observedDurationMs(row[key]) : null + if (resource === "ipv6_nd_prefix") { + const valid = millis("valid-lifetime"), preferred = millis("preferred-lifetime") + if (valid !== null && preferred !== null && preferred > valid) throw new McpError("INVALID_INPUT", "Предпочтительный срок префикса превышает действительный", 400) + } else { + const max = typeof row["ra-interval"] === "string" ? observedDurationMs(row["ra-interval"].split("-")[1]) : null + const lifetime = millis("ra-lifetime") + if (max !== null && lifetime !== null && lifetime > 0 && lifetime < max) throw new McpError("INVALID_INPUT", "Срок Router Advertisement меньше максимального интервала", 400) + } +} + +export function registerIpv6NdMutationActions(register: typeof add) { + for (const id of ["ipv6_nd", "ipv6_nd_prefix", "ipv6_nd_settings"]) for (const action of ["create", "update", "delete"] as const) { + const resource = resourceById(id) + const globalSettings = id === "ipv6_nd_settings" + if (globalSettings && action !== "update") continue + const schema = z.object({ serverId, ...(action === "create" || globalSettings ? {} : { id: rowId }), ...(action === "delete" ? {} : { fields: resource.fields }) }).strict() + register(`ros.${id}.${action}`, `mm_${id}_${action}_plan`, `${resource.title}: ${action}`, "network", "mm:network:write", "write", "servers", schema, { + version: globalSettings ? 1 : 2, + prepare: async input => { + const sid = Number(input.serverId), nativeId = typeof input.id === "string" ? input.id : undefined + const fields = validateRosStep(resource, action, input.fields ?? {}, nativeId) + if (fields.interface === "none" || id === "ipv6_nd_prefix" && (fields.interface === "all" || fields["6to4-interface"] === "all")) { + throw new McpError("INVALID_INPUT", "Требуется конкретный интерфейс IPv6 ND", 400) + } + const before = await readRosSnapshot(sid, id) + const current = action === "create" ? undefined : globalSettings ? before.rows[0] : before.rows.find(row => row[".id"] === nativeId) + if (action !== "create" && !current) throw new McpError("NOT_FOUND", "Запись IPv6 ND не найдена", 404) + const editableDefault = id === "ipv6_nd" && current?.interface === "all" && enabledFlag(current.default) + if (current && (enabledFlag(current.dynamic) || enabledFlag(current.builtin) && !editableDefault || enabledFlag(current.default) && (action === "delete" || fields.interface !== undefined && fields.interface !== current.interface))) { + throw new McpError("PROTECTED_OBJECT", "Встроенную или динамическую запись IPv6 ND нельзя переместить или удалить этим планом", 409) + } + if (action === "update" && Object.keys(fields).some(key => { + const value = current?.[key], field = resource.fields.shape[key] + if (value === undefined || value === null) return true + const oldValue = typeof value === "string" && field.safeParse(true).success ? observedBoolean(value) + : typeof value === "string" && field.safeParse(1).success && /^\d+$/.test(value) ? Number(value) : value + return !field.safeParse(oldValue).success + })) throw new McpError("STATE_UNAVAILABLE", "Исходное значение IPv6 ND для обратного плана не подтверждено", 409) + const desired = { ...current, ...fields } + if (action === "create" || ["ra-interval", "ra-lifetime", "valid-lifetime", "preferred-lifetime"].some(key => fields[key] !== undefined)) checkTimings(id, desired) + if (!globalSettings && ((current && typeof current.interface !== "string") || !desired.interface)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён интерфейс IPv6 ND", 409) + const references = [current?.interface, desired.interface, current?.["6to4-interface"], desired["6to4-interface"]] + const names = new Set(references.filter((value): value is string => typeof value === "string" && value !== "none")) + const uses6to4 = [current?.["6to4-interface"], desired["6to4-interface"]].some(value => typeof value === "string" && value !== "none") + const all = globalSettings || names.has("all") + const raw = await readRosSnapshot(sid, "interface_inventory", undefined, before.sourceProof) + for (const name of names) if (name !== "all" && raw.rows.filter(row => row.name === name).length !== 1) throw new McpError("STATE_CONFLICT", "Интерфейс IPv6 ND отсутствует или неоднозначен", 409) + const objects: ChangeEvidence["objects"] = [{ kind: id, id: globalSettings ? "singleton" : nativeId ?? "new", serverId: sid }] + const snapshots = [before, raw] + const addRef = (kind: string, value: unknown) => { + if (typeof value !== "string" || !/^\*[a-z0-9]+$/i.test(value) || objects.length >= 1000) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены ID зависимостей IPv6 ND", 409) + objects.push({ kind, id: value, serverId: sid }) + } + for (const row of raw.rows) if (all || names.has(String(row.name))) addRef("interface", row[".id"]) + const peerResources = ["ipv6_nd", "ipv6_nd_prefix"].filter(kind => kind !== id) + for (const kind of [...ospfInterfaceResources, "ipv6_address", ...(uses6to4 ? ["ipv4_address"] : []), ...peerResources]) { + const snapshot = await readRosSnapshot(sid, kind, undefined, before.sourceProof) + snapshots.push(snapshot) + for (const row of snapshot.rows) { + const name = String(ospfInterfaceResources.includes(kind) ? row.name : row.interface) + if (all || names.has(name) || peerResources.includes(kind) && name === "all") addRef(kind, row[".id"]) + } + } + return { target: { scope: "servers", serverIds: [sid] }, objects, snapshots, requiresHuman: true, + steps: [{ kind: "ros", step: { serverId: sid, resource: id, action, id: nativeId, fields } }], + warnings: globalSettings ? ["Настройки влияют на приём Router Advertisement и выбор маршрута IPv6 по умолчанию на всём маршрутизаторе."] + : ["Изменение влияет на Router Advertisement и параметры IPv6 клиентов.", ...(all ? ["Правило all затрагивает все интерфейсы, у которых нет более точной настройки ND."] : [])] } + }, + }) + } +} diff --git a/backend/src/modules/mcp/ipv6-nd-write-acceptance.ts b/backend/src/modules/mcp/ipv6-nd-write-acceptance.ts new file mode 100644 index 00000000..ce02cda1 --- /dev/null +++ b/backend/src/modules/mcp/ipv6-nd-write-acceptance.ts @@ -0,0 +1,301 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { ZodError } from "zod" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { applyPlan, approvePlan, invokeMcpTool } from "./runtime.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js" + +type NativeRow = Record +type OperationState = { state: string; verification: string; error_code: string | null } + +/** Runs on a disposable PostgreSQL database and models RouterOS using loopback HTTP. */ +export async function acceptIpv6NdWriteMcp(actor: AuthUser, listsOnly = false) { + const ndPath = "/ipv6/nd" + const prefixPath = "/ipv6/nd/prefix" + const settingsPath = "/ipv6/nd/settings" + const routeDistanceKey = "router-advertisement-route-distance" + const ignoredOptionsKey = "router-advertisement-ignored-options" + const prefixId = "*A5" + const optionalRaId = "*A6" + const ra = { ".id": "*A1", interface: "all", default: "true", "advertise-dns": "no", disabled: "false", pref64: "64:ff9b::/96" } + const settings: NativeRow = { [routeDistanceKey]: "120", [ignoredOptionsKey]: "dns,mtu" } + const unrelated = { ".id": "*A2", interface: "ether2", prefix: "2001:db8:20::/64", "valid-lifetime": "1d", "preferred-lifetime": "30m", dynamic: "false", disabled: "false" } + const ethernet = [ + { ".id": "*E1", name: "ether1", type: "ether", disabled: "false" }, + { ".id": "*E2", name: "ether2", type: "ether", disabled: "false" }, + ] + const catalogs: Record = { + [ndPath]: [ra], + [prefixPath]: [structuredClone(unrelated)], + "/interface": ethernet, + "/interface/ethernet": ethernet, + "/interface/bridge": [], + "/interface/bridge/port": [], + "/interface/bridge/vlan": [], + "/interface/vlan": [], + "/interface/gre": [], + "/interface/wireguard": [], + "/interface/vxlan": [], + "/interface/list": [], + "/interface/list/member": [], + "/ip/address": [], + "/ipv6/address": [], + "/ip/vrf": [], + } + const writes: { method: string; path: string; fields: NativeRow }[] = [] + const router = createServer(async (req, res) => { + try { + const url = new URL(req.url ?? "", "http://127.0.0.1") + const path = decodeURIComponent(url.pathname.replace(/^\/rest/, "")) + res.setHeader("content-type", "application/json") + if (req.method === "GET" && path === settingsPath) { + res.end(JSON.stringify(settings)) + return + } + if (req.method === "GET" && Object.hasOwn(catalogs, path)) { + res.end(JSON.stringify(catalogs[path])) + return + } + const parent = [settingsPath, prefixPath, ndPath].find(menu => path === menu || path.startsWith(menu + "/")) + if (!parent) { res.writeHead(404).end("{}"); return } + const method = req.method ?? "" + const rows = catalogs[parent] ?? [] + const index = rows.findIndex(row => row[".id"] === path.slice(parent.length + 1)) + const setting = method === "POST" && path === settingsPath + "/set" + const creating = method === "PUT" && path === parent && parent !== settingsPath + const updating = method === "PATCH" && index !== -1 + const deleting = method === "DELETE" && index !== -1 + if (!setting && !creating && !updating && !deleting) { res.writeHead(405).end("{}"); return } + + let body = "" + for await (const chunk of req) body += chunk + const fields: NativeRow = body ? JSON.parse(body) : {} + writes.push({ method, path, fields }) + if (setting) { + Object.assign(settings, fields) + res.end("[]") + return + } + if (creating) { + const row = { ".id": parent === prefixPath ? prefixId : optionalRaId, dynamic: "false", disabled: "false", ...fields } + rows.push(row) + res.writeHead(201).end(JSON.stringify(row)) + return + } + if (updating) { + Object.assign(rows[index], fields) + res.end(JSON.stringify(rows[index])) + return + } + rows.splice(index, 1) + res.writeHead(204).end() + } catch { + res.writeHead(500).end("{}") + } + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let serverId: number | undefined + let connectionId: string | undefined + const protection = randomUUID() + const waitForOperation = async (id: string) => { + const deadline = Date.now() + 45_000 + while (Date.now() < deadline) { + const { rows: [row] } = await pool.query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id]) + if (row && !["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw new Error("IPv6 ND operation remains live") + } + try { + const port = (router.address() as { port: number }).port + const { rows: [server] } = await pool.query<{ id: number }>("INSERT INTO servers(name,host,port,use_ssl,password) VALUES('ipv6-nd-write','127.0.0.1',$1,false,'fixture-only') RETURNING id", [port]) + serverId = Number(server.id) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "IPv6 ND native writes", mode: "approval", expiresAt: new Date(Date.now() + 1_800_000).toISOString(), + permissions: ["mm:network:read", "mm:network:write"], + scope: { + serverIds: [serverId], + operationIds: ["ros.ipv6_nd.create", "ros.ipv6_nd.update", "ros.ipv6_nd.delete", "ros.ipv6_nd_prefix.create", "ros.ipv6_nd_prefix.update", "ros.ipv6_nd_prefix.delete", "network.ipv6.nd.settings.get", "ros.ipv6_nd_settings.update", "operations.recover"], + allowGlobal: false, + }, + limits: { callsPerMinute: 120, mutationsPerHour: 100 }, + }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = (tool: string, input: Record) => invokeMcpTool(principal, tool, { serverId, ...input }) as Promise<{ planId: string }> + const dispatch = async (planId: string) => { + const operation = await applyPlan(principal, planId, randomUUID()) + return { id: operation.id, ...await waitForOperation(operation.id) } + } + const apply = async (planId: string) => { + await approvePlan(planId, actor, true) + const operation = await dispatch(planId) + assert.equal(operation.state, "applied", JSON.stringify(operation)) + assert.equal(operation.verification, "passed", JSON.stringify(operation)) + return operation + } + const run = async (tool: string, input: Record) => apply((await prepare(tool, input)).planId) + const recover = async (operationId: string) => { + const inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId }) as { planId: string } + return apply(inverse.planId) + } + if (listsOnly) { + const empty = { dns: "", pref64: "" } + const configured = { dns: "2001:db8::53,2001:db8::54", pref64: "2001:db8:64::/96,2001:db8:65::/64" } + const rule: NativeRow = { ".id": optionalRaId, interface: "ether1", disabled: "true", "advertise-dns": "no", ...empty } + const initial = structuredClone(rule) + const defaultBefore = structuredClone(ra) + catalogs[ndPath].push(rule) + + const tool = "mm_ipv6_nd_update_plan", id = optionalRaId + const populated = await run(tool, { id, fields: configured }) + assert.deepEqual(rule, { ...initial, ...configured }) + const cleared = await run(tool, { id, fields: empty }) + assert.deepEqual(rule, initial) + const restored = await recover(cleared.id) + assert.deepEqual(rule, { ...initial, ...configured }) + assert.deepEqual(ra, defaultBefore) + + for (const key of ["dns", "pref64"] as const) { + delete rule[key] + await assert.rejects(prepare(tool, { id, fields: { [key]: configured[key] } }), { code: "STATE_UNAVAILABLE" }) + await assert.rejects(prepare(tool, { id, fields: { [key]: "" } }), { code: "STATE_UNAVAILABLE" }) + rule[key] = configured[key] + } + assert.deepEqual(writes, [configured, empty, configured].map(fields => ({ method: "PATCH", path: `${ndPath}/${id}`, fields }))) + + for (const operation of [populated, cleared, restored]) { + const { rows } = await pool.query( + `SELECT s.state, c.before_sealed IS NOT NULL AS before_saved, c.after_sealed IS NOT NULL AS after_saved + FROM mcp_operation_steps s LEFT JOIN mcp_operation_checkpoints c USING (operation_id,step_index) + WHERE s.operation_id=$1`, [operation.id], + ) + assert.deepEqual(rows, [{ state: "verified", before_saved: true, after_saved: true }]) + } + console.log("✓ IPv6 ND DNS/PREF64 SQL/HTTP: explicit empty values, one verified step per approved PATCH or inverse, absent baselines rejected") + return + } + const readSettings = async () => { + const result = await invokeMcpTool(principal, "mm_ipv6_nd_settings_get", { serverId }) as { settings: { routeDistance: number; ignoredOptions: string[] } } + return { routeDistance: result.settings.routeDistance, ignoredOptions: result.settings.ignoredOptions } + } + await run("mm_ipv6_nd_prefix_create_plan", { + fields: { interface: "ether1", prefix: "2001:db8:10::/64", "valid-lifetime": "1d", "preferred-lifetime": "30m", autonomous: true }, + }) + const created = catalogs[prefixPath].find(row => row[".id"] === prefixId) + assert.ok(created) + assert.equal(created.prefix, "2001:db8:10::/64") + assert.equal(created["valid-lifetime"], "86400s") + assert.equal(created["preferred-lifetime"], "1800s") + const original = structuredClone(created) + for (const [tool, fields] of [ + ["mm_ipv6_nd_create_plan", { interface: "none" }], + ["mm_ipv6_nd_prefix_create_plan", { interface: "all", prefix: "2001:db8:11::/64" }], + ["mm_ipv6_nd_prefix_create_plan", { interface: "ether1", prefix: "2001:db8:11::/64", "6to4-interface": "all" }], + ] as const) { + await assert.rejects(prepare(tool, { fields }), { code: "INVALID_INPUT" }) + } + await assert.rejects(prepare("mm_ipv6_nd_prefix_update_plan", { id: prefixId, fields: { "preferred-lifetime": "2d" } }), { code: "INVALID_INPUT" }) + await assert.rejects(prepare("mm_ipv6_nd_delete_plan", { id: ra[".id"] }), { code: "PROTECTED_OBJECT" }) + assert.equal(writes.length, 1) + + const lifetimes = { "valid-lifetime": "2d", "preferred-lifetime": "1h" } + const updated = await run("mm_ipv6_nd_prefix_update_plan", { id: prefixId, fields: lifetimes }) + assert.equal(created["valid-lifetime"], "172800s") + assert.equal(created["preferred-lifetime"], "3600s") + await recover(updated.id) + assert.deepEqual(created, original) + + const raInput = { id: ra[".id"], fields: { "advertise-dns": "self" } } + const writesBeforeProtection = writes.length + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ethernet", id: "*E1", serverId, protection: "deny" }), actor.id]) + try { + await assert.rejects(prepare("mm_ipv6_nd_update_plan", raInput), { code: "PROTECTED_OBJECT" }) + assert.equal(writes.length, writesBeforeProtection) + } finally { + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + } + + const stale = await prepare("mm_ipv6_nd_prefix_update_plan", { id: prefixId, fields: lifetimes }) + await approvePlan(stale.planId, actor, true) + const writesBeforeDrift = writes.length + ethernet[0][".id"] = "*E3" + try { + assert.equal((await dispatch(stale.planId)).state, "rejected") + assert.equal(writes.length, writesBeforeDrift) + assert.deepEqual(created, original) + } finally { + ethernet[0][".id"] = "*E1" + } + + const sourceAddress = { ".id": "*D1", interface: "ether2", address: "192.0.2.5/24" } + catalogs["/ip/address"].push(sourceAddress) + created["6to4-interface"] = "ether2" + try { + const stale6to4 = await prepare("mm_ipv6_nd_prefix_update_plan", { id: prefixId, fields: lifetimes }) + await approvePlan(stale6to4.planId, actor, true) + sourceAddress.address = "192.0.2.6/24" + assert.equal((await dispatch(stale6to4.planId)).state, "rejected") + assert.equal(writes.length, writesBeforeDrift) + } finally { + catalogs["/ip/address"].pop() + delete created["6to4-interface"] + } + + await run("mm_ipv6_nd_update_plan", raInput) + assert.equal(ra["advertise-dns"], "self") + const beforeInvalidPref64 = writes.length + await assert.rejects( + prepare("mm_ipv6_nd_update_plan", { id: ra[".id"], fields: { pref64: "2001:db8:64::/65" } }), + error => error instanceof ZodError && error.issues.some(issue => issue.path.join(".") === "fields.pref64"), + ) + assert.equal(writes.length, beforeInvalidPref64) + const previousRa = structuredClone(ra) + const pref64 = await run("mm_ipv6_nd_update_plan", { id: ra[".id"], fields: { pref64: "2001:0DB8:0064::/96,2001:0DB8:0065::/64" } }) + assert.deepEqual(ra.pref64.split(",").sort(), ["2001:db8:64::/96", "2001:db8:65::/64"]) + assert.equal(ra["advertise-dns"], "self") + await recover(pref64.id) + assert.deepEqual(ra, previousRa) + + assert.deepEqual(await readSettings(), { routeDistance: 120, ignoredOptions: ["dns", "mtu"] }) + const distance = await run("mm_ipv6_nd_settings_update_plan", { fields: { [routeDistanceKey]: 150 } }) + assert.deepEqual(writes.at(-1)?.fields, { [routeDistanceKey]: "150" }) + assert.deepEqual(await readSettings(), { routeDistance: 150, ignoredOptions: ["dns", "mtu"] }) + await recover(distance.id) + assert.deepEqual(await readSettings(), { routeDistance: 120, ignoredOptions: ["dns", "mtu"] }) + const clearOptions = await run("mm_ipv6_nd_settings_update_plan", { fields: { [ignoredOptionsKey]: "" } }) + assert.deepEqual(writes.at(-1)?.fields, { [ignoredOptionsKey]: "" }) + assert.deepEqual(await readSettings(), { routeDistance: 120, ignoredOptions: [] }) + await recover(clearOptions.id) + assert.deepEqual(await readSettings(), { routeDistance: 120, ignoredOptions: ["dns", "mtu"] }) + + await run("mm_ipv6_nd_create_plan", { fields: { interface: "ether2", disabled: true, "ra-interval": "3m20s-10m", "ra-lifetime": "30m" } }) + assert.equal(catalogs[ndPath].find(row => row[".id"] === optionalRaId)?.["ra-interval"], "200s-600s") + await run("mm_ipv6_nd_delete_plan", { id: optionalRaId }) + assert.deepEqual(catalogs[ndPath], [ra]) + await run("mm_ipv6_nd_prefix_delete_plan", { id: prefixId }) + assert.deepEqual(catalogs[prefixPath], [unrelated]) + assert.deepEqual(writes.map(({ method, path }) => ({ method, path })), [ + { method: "PUT", path: prefixPath }, + { method: "PATCH", path: `${prefixPath}/${prefixId}` }, + { method: "PATCH", path: `${prefixPath}/${prefixId}` }, + { method: "PATCH", path: `${ndPath}/${ra[".id"]}` }, + { method: "PATCH", path: `${ndPath}/${ra[".id"]}` }, + { method: "PATCH", path: `${ndPath}/${ra[".id"]}` }, + ...Array.from({ length: 4 }, () => ({ method: "POST", path: `${settingsPath}/set` })), + { method: "PUT", path: ndPath }, + { method: "DELETE", path: `${ndPath}/${optionalRaId}` }, + { method: "DELETE", path: `${prefixPath}/${prefixId}` }, + ]) + console.log("✓ IPv6 ND SQL/HTTP: verified prefix writes, RA DNS and PREF64, partial ND settings updates and inverse, interface protection and post-approval drift") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + if (serverId !== undefined) await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + router.closeAllConnections() + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/lifecycle-actions.ts b/backend/src/modules/mcp/lifecycle-actions.ts new file mode 100644 index 00000000..89cc2c2d --- /dev/null +++ b/backend/src/modules/mcp/lifecycle-actions.ts @@ -0,0 +1,134 @@ +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { z } from "zod" +import { certificateIssueRequestSchema } from "@mmapp/contracts/certificates" +import { containerRuntimeStatus } from "../../services/container-runtime-status.js" +import { startContainer, stopContainer, removeContainer } from "../../services/containers-live.js" +import { acceptCertificateIssueRequest } from "../../services/certificate-issue-request.js" +import { createIssueJobRecordIfAbsent, getIssueJobRecord, getServerRowByIdString, toIssueJobDto } from "../../services/certificates-service.js" +import { runCertificateIssueJob } from "../../services/certificate-issue-runner.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import { fingerprint, McpError } from "./security.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" + +const serverId = z.number().int().positive(), id = z.string().min(1).max(100) +async function containerCurrent(input: Record) { + const { client } = await routerClient(Number(input.serverId)) + const rows = z.array(z.object({ ".id": z.string() }).passthrough()).parse(await client.get("/container")) + const matches = rows.filter(row => row[".id"] === input.id) + if (matches.length !== 1) throw new McpError("STATE_CONFLICT", "Контейнер отсутствует или его ID неоднозначен", 409) + const row = matches[0] + const configuration = Object.fromEntries([".id", "name", "root-dir", "remote-image", "interface", "envlist", "envlists", "mounts", "mountlists", "cmd", "hostname", "comment", "start-on-boot"].map(key => [key, row[key]])) + // Runtime counters/status are excluded; free-form cmd and mount/env config + // are sealed by their hash instead of being returned to the AI or audit. + return { id: input.id, status: containerRuntimeStatus(row), configurationFingerprint: fingerprint(configuration), interfaceName: typeof row.interface === "string" && row.interface.trim() ? row.interface.trim() : null } +} +async function removalCurrent(input: Record) { + const current = await containerCurrent(input) + if (!current.interfaceName) throw new McpError("STATE_CONFLICT", "Не подтверждён интерфейс контейнера", 409) + const { client } = await routerClient(Number(input.serverId)) + const inventory = z.array(z.object({ ".id": z.string().min(1), name: z.string().min(1), type: z.string().min(1), disabled: z.union([z.string(), z.boolean()]).optional() }).passthrough()).parse(await client.get("/interface")) + if (new Set(inventory.map(row => row[".id"])).size !== inventory.length) throw new McpError("STATE_CONFLICT", "Неоднозначные ID интерфейсов", 409) + const parents = inventory.filter(row => row.name === current.interfaceName) + if (parents.length !== 1) throw new McpError("STATE_CONFLICT", "Интерфейс контейнера отсутствует или неоднозначен", 409) + const parent = parents[0] + return { ...current, parent: { id: parent[".id"], name: parent.name, type: parent.type, disabled: parent.disabled ?? null } } +} +function containerObjects(sid: number, id: string, parent: Awaited>["parent"]): ChangeEvidence["objects"] { + const nativeKind = parent.type === "ether" ? "ethernet" : parent.type + return [{ kind: "container", id, serverId: sid }, { kind: "interface", id: parent.id, serverId: sid }, { kind: nativeKind, id: parent.id, serverId: sid }] +} +export function registerLifecycleActions(register: typeof add) { + for (const action of ["start", "stop"] as const) { + registerMcpService(`containers.${action}`, { + current: removalCurrent, + execute: async input => { + const current = await removalCurrent(input) + if (current.configurationFingerprint !== input.configurationFingerprint || fingerprint(current.parent) !== input.parentFingerprint || !["running", "stopped"].includes(current.status)) throw new McpError("STATE_CONFLICT", "Конфигурация, интерфейс или состояние контейнера изменились", 409) + const { server } = await routerClient(Number(input.serverId)); await (action === "start" ? startContainer : stopContainer)(server, String(input.id)); return { acknowledged: true } + }, + verify: async input => { + for (let attempt = 0; attempt < 10; attempt++) { + const current = await removalCurrent(input) + if (current.configurationFingerprint !== input.configurationFingerprint || fingerprint(current.parent) !== input.parentFingerprint) return false + if (current.status === (action === "start" ? "running" : "stopped")) return true + await delay(500) + } + return false + }, + }) + } + for (const action of ["start", "stop", "restart"] as const) { + register(`containers.${action}`, `mm_container_${action}_plan`, `Контейнер: ${action}`, "network", "mm:network:write", "write", "servers", z.object({ serverId, id }).strict(), { + prepare: async input => { + const before = await removalCurrent(input), sid = Number(input.serverId) + if (!["running", "stopped"].includes(before.status)) throw new McpError("STATE_CONFLICT", "Состояние контейнера неизвестно или переходное", 409) + const actions = action === "restart" ? before.status === "running" ? ["stop", "start"] : ["start"] : [action] + const steps: ChangeEvidence["steps"] = actions.map(action => ({ kind: "service", service: `containers.${action}`, input: { ...input, configurationFingerprint: before.configurationFingerprint, parentFingerprint: fingerprint(before.parent) } })) + return { target: { scope: "servers", serverIds: [sid] }, objects: containerObjects(sid, String(input.id), before.parent), snapshots: [await readRosSnapshot(sid, "container")], before, steps, requiresHuman: true, warnings: ["Состояние проверяется после каждой команды; повторное выполнение при потере подтверждения запрещено."] } + }, + }) + } + registerMcpService("containers.remove", { + current: removalCurrent, + execute: async input => { + const current = await removalCurrent(input) + if (current.status !== "stopped" || fingerprint(current) !== fingerprint(input.expected)) throw new McpError("STATE_CONFLICT", "Удаление требует исходный остановленный контейнер и неизменный интерфейс", 409) + const { server } = await routerClient(Number(input.serverId)) + await removeContainer(server, String(input.id)) + return { acknowledged: true, removedId: input.id } + }, + verify: async input => { + const { client } = await routerClient(Number(input.serverId)) + const rows = z.array(z.object({ ".id": z.string().min(1) }).passthrough()).parse(await client.get("/container")) + return !rows.some(row => row[".id"] === input.id) + }, + }) + register("containers.remove", "mm_container_remove_plan", "Удаление подтверждённо остановленного контейнера", "network", "mm:network:write", "write", "servers", z.object({ serverId, id }).strict(), { + prepare: async input => { + const before = await removalCurrent(input), sid = Number(input.serverId) + if (before.status !== "stopped") throw new McpError("STATE_CONFLICT", "Сначала остановите контейнер отдельным планом и обновите каталог", 409) + return { target: { scope: "servers", serverIds: [sid] }, objects: containerObjects(sid, String(input.id), before.parent), + snapshots: [await readRosSnapshot(sid, "container")], before, steps: [{ kind: "service", service: "containers.remove", input: { ...input, expected: before } }], requiresHuman: true, + warnings: ["Удаляется только остановленный контейнер. Не выполняются скрытая остановка, удаление mounts/env/interface или файлов root-dir.", "Универсального обратного плана для удаления нет. При потере подтверждения требуется сверка; команда не повторяется."] } + }, + }) + registerMcpService("certificates.issue", { + current: async input => { + const snapshot = await readRosSnapshot(Number(input.serverId), "certificate") + return snapshot.rows + }, + execute: async input => { + let queuedId: string | undefined + const accepted = await acceptCertificateIssueRequest(certificateIssueRequestSchema.parse(input), { + getServer: getServerRowByIdString, getJob: getIssueJobRecord, createJob: createIssueJobRecordIfAbsent, + queue: jobId => { queuedId = jobId }, + }) + // Await the existing runner inside the operation lease. A queued receipt alone + // must never be presented as an installed certificate. + if (queuedId) await runCertificateIssueJob(queuedId) + const job = await getIssueJobRecord(accepted.jobId) + if (!job || job.status !== "done") throw new McpError("VERIFICATION_FAILED", "Выпуск сертификата не подтверждён; проверьте существующую задачу", 409) + return { jobId: accepted.jobId, status: job.status } + }, + verify: async input => (await getIssueJobRecord(String(input.requestId)))?.status === "done", + }) + register("certificates.issue", "mm_certificate_issue_plan", "Выпуск и установка сертификата через настроенный ACME", "certificates", "mm:certificates:write", "write", "servers", certificateIssueRequestSchema.extend({ serverId }).strict(), { + prepare: async input => { + const sid = Number(input.serverId), snapshot = await readRosSnapshot(sid, "certificate") + const requestId = input.requestId ?? randomUUID() + if (await getIssueJobRecord(String(requestId))) throw new McpError("STATE_CONFLICT", "Для существующей задачи используйте инструмент просмотра; повторный выпуск запрещён", 409) + return { target: { scope: "servers", serverIds: [sid] }, objects: [{ kind: "server", id: String(sid), serverId: sid }], snapshots: [snapshot], before: snapshot.rows, + steps: [{ kind: "service", service: "certificates.issue", input: { ...input, requestId } }], requiresHuman: true, warnings: ["Используются сохранённые сервером настройки ACME. Секрет Cloudflare не передаётся нейросети.", "Операция завершается только после подтверждённого результата существующего процесса выпуска."] } + }, + }) + register("certificates.jobs.read", "mm_certificate_job_get", "Состояние задачи выпуска сертификата", "certificates", "mm:certificates:read", "read", "servers", z.object({ serverId, jobId: z.string().uuid() }).strict(), { + read: async input => { + const job = await getIssueJobRecord(String(input.jobId)) + if (!job || Number(job.serverId) !== input.serverId) throw new McpError("NOT_FOUND", "Задача не найдена", 404) + return toIssueJobDto(job) + }, + }) +} diff --git a/backend/src/modules/mcp/log-artifacts.ts b/backend/src/modules/mcp/log-artifacts.ts new file mode 100644 index 00000000..cdffb871 --- /dev/null +++ b/backend/src/modules/mcp/log-artifacts.ts @@ -0,0 +1,113 @@ +import { createHash, randomUUID, timingSafeEqual } from "node:crypto" +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { MAX_LOG_EXPORT_BYTES, withLogExportSlot, LogExportBusyError } from "../../services/router-logs-export.js" +import { connectionWrite, effectiveMcpConnection } from "./store.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, MAX_MCP_PUBLIC_STRING_LENGTH, McpError, privateSecretFingerprint } from "./security.js" +import type { ActionContext } from "./action-types.js" +import type { add } from "./actions.js" + +export const MAX_LOG_ARTIFACT_CHUNK_BYTES = Math.floor(MAX_MCP_PUBLIC_STRING_LENGTH / 4) * 3 +const MAX_CIPHERTEXT_BYTES = 12 * 1024 * 1024 +export const MAX_LOG_ARTIFACTS_PER_CONNECTION = 8 +export const MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION = 24 * 1024 * 1024 +export const MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES = 256 * 1024 * 1024 +export function assertLogArtifactBudget(count: bigint, ownBytes: bigint, totalBytes: bigint, addedBytes: number) { + if (count < 0n || ownBytes < 0n || totalBytes < ownBytes || !Number.isSafeInteger(addedBytes) || addedBytes < 0) throw new McpError("STATE_UNAVAILABLE", "Квота выгрузок не подтверждена", 503) + if (count >= BigInt(MAX_LOG_ARTIFACTS_PER_CONNECTION) || ownBytes + BigInt(addedBytes) > BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION) || totalBytes + BigInt(addedBytes) > BigInt(MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES)) throw new McpError("EXPORT_QUOTA_EXCEEDED", "Квота временных выгрузок исчерпана; дождитесь истечения файлов", 429) +} +const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const digest = z.string().regex(/^[a-f0-9]{64}$/) +const accessSchema = z.object({ version: z.literal(1), kind: z.literal("logs"), serverIds: z.array(z.number().int().positive().safe()).max(1000).refine(ids => new Set(ids).size === ids.length), policyVersion: z.number().int().positive(), generation: digest, bytes: z.number().int().min(0).max(MAX_LOG_EXPORT_BYTES), sha256: digest, encryptedSha256: digest, proof: digest }).strict() +type Access = z.infer +type Meta = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date; access_scope: Access } +const signedHeader = (row: Omit, access: Omit) => ({ id: row.id, connectionId: row.connection_id, filename: row.filename, mediaType: row.media_type, expiresAt: row.expires_at.toISOString(), access }) +const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "mcp-log-export-artifact:v1") + +export async function assertLogExportAccess(context: ActionContext, serverIds: readonly number[], client?: Pick) { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение выгрузки отменено", 409) + const current = await effectiveMcpConnection(context.principal, client) + if (!hasPermission(current.owner_permissions, "mm:servers:read") || !hasPermission(current.policy.permissions, "mm:servers:read")) throw new McpError("PERMISSION_DENIED", "Право чтения логов отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Доступ изменился; повторите чтение выгрузки", 409) + if (serverIds.some(id => !current.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Выгрузка содержит недоступные серверы", 403) + return current +} +export async function createLogExportArtifact(content: string, format: "json" | "csv", serverIds: number[], context: ActionContext) { + await assertLogExportAccess(context, serverIds) + const bytes = Buffer.byteLength(content, "utf8") + if (bytes > MAX_LOG_EXPORT_BYTES) throw new McpError("EXPORT_LIMIT", "Выгрузка превышает 8 MiB", 422) + const generation = await accessGeneration(), ciphertext = await encryptSecret(content) + const header = { id: randomUUID(), connection_id: context.connection.id, filename: "router-logs." + format, media_type: format === "json" ? "application/json" : "text/csv", expires_at: new Date(Date.now() + 900000) } + const access = { version: 1 as const, kind: "logs" as const, serverIds: [...new Set(serverIds)].sort((a, b) => a - b), policyVersion: context.connection.policy.version, generation, bytes, sha256: hash(Buffer.from(content, "utf8")), encryptedSha256: hash(ciphertext) } + const accessScope = accessSchema.parse({ ...access, proof: await sign(signedHeader(header, access)) }) + await assertLogExportAccess(context, serverIds) + if (generation !== await accessGeneration()) throw new McpError("RESET_REQUIRED", "Поколение доступа изменилось", 409) + await connectionWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [context.connection.id]) + await assertLogExportAccess(context, serverIds, client) + if (generation !== await accessGeneration()) throw new McpError("RESET_REQUIRED", "Поколение доступа изменилось", 409) + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))") + await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()") + const { rows: [usage] } = await client.query(`SELECT count(*) FILTER(WHERE connection_id=$1)::text AS files, + COALESCE(sum(octet_length(ciphertext)) FILTER(WHERE connection_id=$1),0)::text AS own_bytes, + COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts`, [context.connection.id]) + assertLogArtifactBudget(BigInt(usage.files), BigInt(usage.own_bytes), BigInt(usage.total_bytes), Buffer.byteLength(ciphertext, "utf8")) + await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)", [header.id, header.connection_id, header.filename, header.media_type, ciphertext, JSON.stringify(accessScope), header.expires_at]) + }) + await assertLogExportAccess(context, serverIds) + return { artifactId: header.id, downloadPath: `/api/mcp/artifacts/${header.id}`, expiresInSeconds: 900, bytes, sha256: access.sha256 } +} +async function loadMeta(id: string, context: { connection: { id: string }; transactionClient?: Pick }): Promise { + const query = context.transactionClient ?? pool + const { rows: [row] } = await query.query(`/* MCP log artifact metadata */ SELECT id,connection_id,filename,media_type,expires_at, + CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope, + octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND connection_id=$2 AND expires_at>now()`, [id, context.connection.id]) + if (!row) throw new McpError("NOT_FOUND", "Выгрузка отсутствует или срок истёк", 404) + const parsed = accessSchema.safeParse(row.access_scope) + if (!parsed.success) throw new McpError("ARTIFACT_SCOPE_UNKNOWN", "У выгрузки нет подтверждённой области доступа; сформируйте её заново", 409) + if (!Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes > MAX_CIPHERTEXT_BYTES || !(row.expires_at instanceof Date) || !Number.isFinite(row.expires_at.getTime())) throw new McpError("ARTIFACT_INVALID", "Не подтверждены метаданные выгрузки", 409) + const { proof, ...access } = parsed.data, expected = await sign(signedHeader(row, access)) + if (!timingSafeEqual(Buffer.from(proof, "hex"), Buffer.from(expected, "hex"))) throw new McpError("ARTIFACT_INVALID", "Подпись области выгрузки не совпала", 409) + return { ...row, access_scope: parsed.data } +} +export async function loadLogExportArtifactMetadata(id: string, connectionId: string) { + return loadMeta(id,{connection:{id:connectionId}}) +} +async function assertMetaAccess(meta: Meta, context: ActionContext) { + const current = await assertLogExportAccess(context, meta.access_scope.serverIds) + if (meta.access_scope.policyVersion !== current.policy.version || meta.access_scope.generation !== await accessGeneration() || meta.expires_at.getTime() <= Date.now()) throw new McpError("RESET_REQUIRED", "Выгрузка относится к прежнему доступу или срок истёк; сформируйте её заново", 409) +} +export function registerLogArtifactActions(register: typeof add) { + register("logs.export.read", "mm_logs_export_read", "Чтение части разрешённой выгрузки логов: base64, offset и SHA-256", "logs", "mm:servers:read", "read", "servers", z.object({ artifactId: z.string().uuid(), offset: z.number().int().min(0).max(MAX_LOG_EXPORT_BYTES).default(0), limit: z.number().int().min(1).max(MAX_LOG_ARTIFACT_CHUNK_BYTES).default(MAX_LOG_ARTIFACT_CHUNK_BYTES) }).strict(), { + target: async (input, context) => { + try { return { scope: "servers", serverIds: (await loadMeta(String(input.artifactId), context)).access_scope.serverIds } } + catch { return { scope: "servers", serverIds: context.connection.policy.scope.serverIds } } // The audited handler reports the safe error; no payload is loaded here. + }, + read: async (input, context) => { + let checkedMeta: Meta | undefined + try { + return await withLogExportSlot(async () => { + const meta = await loadMeta(String(input.artifactId), context) + checkedMeta = meta + await assertMetaAccess(meta, context) + const offset = Number(input.offset), limit = Number(input.limit) + if (offset > meta.access_scope.bytes) throw new McpError("INVALID_INPUT", "Offset находится за концом выгрузки", 400) + const { rows: [payload] } = await pool.query(`/* MCP log artifact payload */ SELECT ciphertext FROM mcp_artifacts + WHERE id=$1 AND connection_id=$2 AND access_scope->>'proof'=$3 AND expires_at>now() + AND octet_length(ciphertext)<=${MAX_CIPHERTEXT_BYTES} FOR SHARE`, [meta.id, context.connection.id, meta.access_scope.proof]) + await assertMetaAccess(meta, context) + if (!payload || hash(payload.ciphertext) !== meta.access_scope.encryptedSha256) throw new McpError("ARTIFACT_INVALID", "Зашифрованная выгрузка изменилась", 409) + let content: Buffer + try { content = Buffer.from(await decryptSecret(payload.ciphertext), "utf8") } + catch { throw new McpError("ARTIFACT_INVALID", "Выгрузка не расшифрована", 409) } + await assertMetaAccess(meta, context) + if (content.length !== meta.access_scope.bytes || hash(content) !== meta.access_scope.sha256) throw new McpError("ARTIFACT_INVALID", "Размер или SHA-256 выгрузки не совпал", 409) + const end = Math.min(offset + limit, content.length) + return { artifactId: meta.id, filename: meta.filename, mediaType: meta.media_type, encoding: "base64", dataBase64: content.subarray(offset, end).toString("base64"), offset, nextOffset: end < content.length ? end : null, complete: end === content.length, bytes: content.length, sha256: meta.access_scope.sha256, expiresAt: meta.expires_at.toISOString() } + }) + } catch (error) { await assertLogExportAccess(context, checkedMeta?.access_scope.serverIds ?? []); if (error instanceof LogExportBusyError) throw new McpError("EXPORT_BUSY", error.message, 429); throw error } + }, + }) +} diff --git a/backend/src/modules/mcp/logs-follow-cancellation.ts b/backend/src/modules/mcp/logs-follow-cancellation.ts new file mode 100644 index 00000000..3430a3d9 --- /dev/null +++ b/backend/src/modules/mcp/logs-follow-cancellation.ts @@ -0,0 +1,6 @@ +import { McpError } from "./security.js" + +export function logsFollowCancellation(maintenance?: AbortSignal, caller?: AbortSignal): McpError | undefined { + if (maintenance?.aborted) return maintenance.reason + if (caller?.aborted) return new McpError("READ_CANCELLED", "Ожидание новых событий отменено", 409) +} diff --git a/backend/src/modules/mcp/logs-follow-sdk-cancellation-acceptance.ts b/backend/src/modules/mcp/logs-follow-sdk-cancellation-acceptance.ts new file mode 100644 index 00000000..1acfe4ff --- /dev/null +++ b/backend/src/modules/mcp/logs-follow-sdk-cancellation-acceptance.ts @@ -0,0 +1,331 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import type { IncomingMessage, ServerResponse } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import type { FastifyInstance } from "fastify" +import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client" +import { SignJWT } from "jose" +import type { LogEntry } from "@mmapp/contracts" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import { SYSTEM_DATABASE_REQUEST_LOCK } from "../../db/operation-lock.js" +import { env } from "../../config.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createMcpConnection, setConnectionStatus } from "./store.js" +import { mcpDatabaseRequestSignal } from "./database-request-lease.js" + +export async function verifyLogsScopedSdkReads(app: FastifyInstance, endpoint: string, actor: AuthUser) { + const historicalId = 93, hiddenId = 94, marker = `mcp-scoped-logs-${randomUUID()}` + const from = "2001-01-01T00:00:00Z", to = "2001-01-01T01:00:00Z" + const client = new Client({ name: "logs-scoped-sdk-reads", version: "1.0.0" }) + try { + await pool.query("INSERT INTO servers(id,name,host,enabled,username,password) VALUES($1,$2,'127.0.0.1',false,'fixture','fixture')", [historicalId, marker]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: marker, mode: "observer", permissions: ["mm:servers:read"], + expiresAt: new Date(Math.min(actor.expiresAt ?? Infinity, Date.now() + 600_000)).toISOString(), + scope: { serverIds: [1, historicalId], operationIds: ["logs.get", "logs.context", "logs.summary", "logs.export"], allowGlobal: false }, + }), actor) + const { rows } = await pool.query<{ id: string; serverId: number; index: string }>(` + INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + SELECT server_id,$1,'127.0.0.1',n::text,'00:30:00',now(),$2::timestamptz+(30+n)*interval '1 minute',ARRAY['system'],'info', + CASE WHEN server_id=$4 THEN $1 || ' hidden-row' ELSE $1 END,0 + FROM (SELECT 1 AS server_id,n FROM generate_series(-12,12) AS n UNION ALL SELECT $3,0 UNION ALL SELECT $4,0) AS fixture + RETURNING id::text,server_id AS "serverId",router_id AS "index"`, [marker, from, historicalId, hiddenId]) + await pool.query("INSERT INTO router_log_collection_state(server_id,server_name,host) SELECT id,$1,'127.0.0.1' FROM unnest($2::int[]) AS id", [marker, [historicalId, hiddenId]]) + assert.equal((await pool.query("DELETE FROM servers WHERE id=$1 AND name=$2", [historicalId, marker])).rowCount, 1) + await client.connect(new StreamableHTTPClientTransport(new URL(endpoint), { + requestInit: { headers: { Authorization: `Bearer ${access.token}` } }, + })) + const idAt = (serverId: number, index: number) => { + const row = rows.find(item => item.serverId === serverId && Number(item.index) === index) + assert.ok(row) + return row.id + } + const call = (name: string, args: Record) => client.callTool({ name, arguments: args }) + const resultOf = async (name: string, args: Record) => { + const response = await call(name, args) + assert.ok(!response.isError) + return (response.structuredContent as { result: T }).result + } + const id = idAt(1, 0) + assert.equal((await resultOf("mm_logs_get", { serverId: 1, id })).id, id) + type LogContext = { entry: LogEntry; before: LogEntry[]; after: LogEntry[] } + const context = await resultOf("mm_logs_context_get", { serverId: 1, id }) + assert.equal(context.entry.id, id) + assert.deepEqual(context.before.map(row => row.id), Array.from({ length: 10 }, (_, i) => idAt(1, i - 10))) + assert.deepEqual(context.after.map(row => row.id), Array.from({ length: 10 }, (_, i) => idAt(1, i + 1))) + const historical = { serverId: historicalId, id: idAt(historicalId, 0) } + assert.equal((await resultOf("mm_logs_get", historical)).serverId, historicalId) + const historicalContext = await resultOf("mm_logs_context_get", historical) + assert.equal(historicalContext.entry.id, historical.id) + assert.deepEqual([...historicalContext.before, ...historicalContext.after], []) + for (const name of ["mm_logs_get", "mm_logs_context_get"]) { + const maximumId = await call(name, { serverId: 1, id: "09223372036854775807" }) + assert.equal(maximumId.isError, true) + assert.match(JSON.stringify(maximumId), /NOT_FOUND/) + for (const id of ["9223372036854775808", "99999999999999999999"]) { + const invalid = await call(name, { serverId: 1, id }) + assert.equal(invalid.isError, true) + assert.match(JSON.stringify(invalid), /ID журнала выходит за допустимый диапазон/) + } + const foreignId = idAt(hiddenId, 0) + const foreign = await call(name, { serverId: 1, id: foreignId }) + assert.equal(foreign.isError, true) + assert.match(JSON.stringify(foreign), /NOT_FOUND/) + assert.doesNotMatch(JSON.stringify(foreign), new RegExp(marker)) + const outOfScope = await call(name, { serverId: hiddenId, id: foreignId }) + assert.equal(outOfScope.isError, true) + assert.match(JSON.stringify(outOfScope), /SERVER_DENIED/) + } + const summary = await resultOf<{ total: number; levels: { info: number }; states: { serverId: number }[] }>( + "mm_logs_summary_get", { from, to, q: marker }, + ) + assert.equal(summary.total, 26) + assert.equal(summary.levels.info, 26) + assert.ok(summary.states.some(state => state.serverId === historicalId)) + assert.ok(summary.states.every(state => state.serverId === 1 || state.serverId === historicalId)) + assert.ok(!("settings" in summary)) + console.log("✓ MCP logs SDK: scoped entries, same-server context and historical summary") + + const portalJwt = (subject: string, permissions: string[]) => + new SignJWT({ sub: subject, apps: ["mm"], permissions }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("5m") + .sign(new TextEncoder().encode(env.jwtSecret)) + const ownerToken = await portalJwt(actor.id, actor.permissions) + const otherToken = await portalJwt(`${actor.id}-other`, ["mm:mcp:read", "mm:servers:read"]) + const restrictedToken = await portalJwt(actor.id, ["mm:mcp:read"]) + type Export = { artifactId: string; downloadPath: string; expiresInSeconds: number; rows: number; sha256: string } + const exportLogs = (format: "csv" | "json") => resultOf("mm_logs_export", { from, to, q: marker, format }) + const download = (url: string, token?: string) => + app.inject({ url, headers: token ? { authorization: `Bearer ${token}` } : {} }) + const csv = await exportLogs("csv") + assert.equal(csv.rows, 26) + assert.equal(csv.expiresInSeconds, 900) + assert.equal(csv.downloadPath, `/api/mcp/artifacts/${csv.artifactId}`) + const response = await download(csv.downloadPath, ownerToken) + assert.equal(response.statusCode, 200) + assert.equal(response.headers["cache-control"], "private, no-store") + assert.match(String(response.headers["content-type"]), /^text\/csv/) + assert.equal(createHash("sha256").update(response.rawPayload).digest("hex"), csv.sha256) + assert.ok(!response.body.includes("hidden-row")) + assert.equal((await download(csv.downloadPath)).statusCode, 401) + assert.equal((await download(csv.downloadPath, access.token)).statusCode, 401) + assert.equal((await download(csv.downloadPath, otherToken)).statusCode, 404) + assert.equal((await download(csv.downloadPath, restrictedToken)).statusCode, 403) + + const json = await exportLogs("json") + const jsonResponse = await download(json.downloadPath, ownerToken) + assert.equal(jsonResponse.statusCode, 200) + const jsonRows = jsonResponse.json>>() + assert.equal(jsonRows.length, json.rows) + assert.ok(jsonRows.every(row => !Object.hasOwn(row, "host"))) + assert.equal(createHash("sha256").update(jsonResponse.rawPayload).digest("hex"), json.sha256) + await pool.query("UPDATE mcp_artifacts SET expires_at=now()-interval '1 second' WHERE id=$1 AND connection_id=$2", [json.artifactId, access.connection.id]) + assert.equal((await download(json.downloadPath, ownerToken)).statusCode, 404) + await setConnectionStatus(access.connection.id, "revoked", actor) + assert.equal((await download(csv.downloadPath, ownerToken)).statusCode, 403) + console.log("✓ MCP logs export: CSV owner download, scope, non-cacheable response, expiry and revocation") + } finally { + await client.close() + await pool.query("DELETE FROM router_log_entries WHERE message=ANY($1::text[])", [[marker, `${marker} hidden-row`]]) + await pool.query("DELETE FROM router_log_collection_state WHERE server_id=ANY($1::int[]) AND server_name=$2", [[historicalId, hiddenId], marker]) + await pool.query("DELETE FROM servers WHERE id=$1 AND name=$2", [historicalId, marker]) + } +} + +export async function verifyLogsFollowSdkCancellation(app: FastifyInstance, endpoint: string, actor: AuthUser, leaseLossOnly = false) { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Logs follow SDK cancellation", mode: "approval", permissions: ["mm:servers:read"], + expiresAt: new Date(Math.min(actor.expiresAt ?? Infinity, Date.now() + 600_000)).toISOString(), + scope: { serverIds: [1], operationIds: ["logs.follow", "logs.query"], allowGlobal: false }, + }), actor) + const client = new Client({ name: "logs-follow-sdk-cancellation", version: "1.0.0" }, { + versionNegotiation: { mode: { pin: "2026-07-28" } }, + }) + const caller = new AbortController(), bound = new AbortController() + const deadline = delay(5000, undefined, { signal: bound.signal }).then(() => { throw new Error("SDK cancellation fixture deadline") }) + void deadline.catch(() => undefined) + let entered!: () => void, release!: () => void, closed!: () => void + const enteredRead = new Promise(resolve => { entered = resolve }) + const holdRead = new Promise(resolve => { release = resolve }) + const responseClosed = new Promise(resolve => { closed = resolve }) + let readCount = 0, entryQueries = 0 + let requestLease: AbortSignal | undefined + const originalQuery = pool.query + const observeClose = (request: IncomingMessage, response: ServerResponse) => { + if (request.method === "POST" && request.url === "/mcp") { + response.once("close", () => { if (!response.writableFinished) closed() }) + } + } + try { + await client.connect(new StreamableHTTPClientTransport(new URL(endpoint), { + requestInit: { headers: { Authorization: `Bearer ${access.token}` } }, + })) + app.server.on("request", observeClose) + pool.query = (async (...args: unknown[]) => { + const query = args[0] + const sql = typeof query === "object" && query !== null && "text" in query ? query.text : query + if (typeof sql === "string" && sql.includes("ORDER BY router_log_entries.id LIMIT")) entryQueries++ + const result = await Reflect.apply(originalQuery, pool, args) + if (typeof sql === "string" && sql.startsWith("SELECT GREATEST(COALESCE(max(id),0)")) { + readCount++ + requestLease = mcpDatabaseRequestSignal() + entered() + await holdRead + } + return result + }) as typeof originalQuery + const follow = client.callTool({ name: "mm_logs_follow", arguments: { serverId: 1, waitSeconds: 30 } }, { signal: caller.signal }) + const expectedError = leaseLossOnly ? "PROTOCOL_STORAGE_BUSY" : "READ_CANCELLED" + const refused = leaseLossOnly ? follow.then(result => { + assert.equal(result.isError, true) + const wire = JSON.stringify(result) + assert.match(wire, /PROTOCOL_STORAGE_BUSY/) + assert.doesNotMatch(wire, /"cursor"|"entries"/) + }) : assert.rejects(follow) + void refused.catch(() => undefined) + try { + await Promise.race([enteredRead, deadline]) + if (leaseLossOnly) { + assert(requestLease && !requestLease.aborted) + const leaseAborted = new Promise(resolve => requestLease!.addEventListener("abort", () => resolve(), { once: true })) + const { rows } = await pool.query(`SELECT l.pid FROM pg_locks l JOIN pg_stat_activity a USING(pid) + WHERE l.locktype='advisory' AND l.classid=0 AND l.objid=$1::oid AND l.objsubid=1 AND l.mode='ShareLock' AND l.granted + AND a.datname=current_database() AND a.state='idle' AND a.query='SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired'`, [SYSTEM_DATABASE_REQUEST_LOCK]) + assert.equal(rows.length, 1, "Expected exactly one owned SDK request lease") + assert.equal((await pool.query("SELECT pg_terminate_backend($1) AS stopped", [rows[0].pid])).rows[0].stopped, true) + await Promise.race([leaseAborted, deadline]) + } else { + caller.abort() + await Promise.race([Promise.all([refused, responseClosed]), deadline]) + } + const live = await pool.query("SELECT count(*)::int AS count FROM mcp_read_leases WHERE connection_id=$1", [access.connection.id]) + assert.equal(live.rows[0].count, 1, "caller cancellation must wait for the pending read before releasing the lease") + release() + await Promise.race([refused, deadline]) + for (;;) { + const { rows: [audit] } = await pool.query( + "SELECT status,error_code,result FROM mcp_requests WHERE connection_id=$1 AND tool='mm_logs_follow'", [access.connection.id], + ) + const protocol = await pool.query("SELECT count(*)::int AS count FROM mcp_requests WHERE connection_id=$1 AND tool='protocol:tools/call' AND finished_at IS NOT NULL", [access.connection.id]) + if (audit && audit.status !== "running" && protocol.rows[0].count === 1) { + assert.equal(audit.status, "error") + assert.equal(audit.error_code, expectedError) + assert.doesNotMatch(JSON.stringify(audit.result), /"cursor"/) + break + } + await Promise.race([delay(25), deadline]) + } + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_read_leases WHERE connection_id=$1", [access.connection.id])).rows[0].count, 0) + assert.equal(readCount, 1, "cancelled follow does not dispatch a second read") + assert.equal(entryQueries, 0, "cancelled follow does not fetch entries after the pending watermark") + } finally { + caller.abort() + release() + await refused.catch(() => undefined) + } + pool.query = originalQuery + if (leaseLossOnly) { + const next = await client.callTool({ name: "mm_logs_follow", arguments: { serverId: 1, waitSeconds: 0 } }) + assert.equal(next.isError ?? false, false) + assert.equal(typeof (next.structuredContent as { result?: { cursor?: unknown } })?.result?.cursor, "string") + console.log("MCP logs SDK request lease loss: no entries or cursor, audit and lease settled, next call works") + return + } + const from = "2000-01-01T00:00:00Z", to = "2000-01-01T00:30:00Z" + const followOnce = (filters: { from?: string; cursor?: string }) => client.callTool({ + name: "mm_logs_follow", arguments: { serverId: 1, waitSeconds: 0, to, ...filters }, + }) + const initial = await followOnce({ from }) + assert.ok(!initial.isError) + const cursor = (initial.structuredContent as { result?: { cursor?: unknown } } | undefined)?.result?.cursor + assert.ok(typeof cursor === "string" && cursor.length > 0) + assert.ok(!(await followOnce({ from, cursor })).isError) + assert.ok(!(await followOnce({ cursor })).isError, "omitting from must resume the historical cursor's period") + const changed = await followOnce({ from: "2000-01-01T00:01:00Z", cursor }) + assert.equal(changed.isError, true) + assert.match(JSON.stringify(changed), /RESET_REQUIRED/) + assert.doesNotMatch(JSON.stringify(changed), /"cursor"/) + console.log("✓ MCP logs follow: historical cursor resumes, changed from requires reset") + + const marker = `mcp-logs-cursor-${randomUUID()}` + const readFollowPage = async (options: { cursor?: string; limit?: number }) => { + const result = await client.callTool({ name: "mm_logs_follow", arguments: { serverId: 1, from, to, q: marker, waitSeconds: 0, ...options } }) + assert.ok(!result.isError) + return (result.structuredContent as { result: { entries: { id: string }[]; cursor: string } }).result + } + const start = await readFollowPage({}) + assert.deepEqual(start.entries, []) + const { rows: [initialSettings] } = await pool.query<{ enabled: boolean }>("SELECT enabled FROM router_log_settings WHERE id=1") + try { + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + SELECT 1,'cursor fixture','127.0.0.1','fixture','00:02:00',now(),$1::timestamptz+n*interval '2 minutes',ARRAY['system'],'info',$2,0 + FROM generate_series(1,2) AS n`, [from, marker]) + const defaultFollow = await readFollowPage({ cursor: start.cursor }) + assert.equal(defaultFollow.entries.length, 2) + const firstFollow = await readFollowPage({ cursor: start.cursor, limit: 1 }) + const secondFollow = await readFollowPage({ cursor: firstFollow.cursor, limit: 1 }) + assert.equal(firstFollow.entries.length, 1) + assert.equal(secondFollow.entries.length, 1) + assert.deepEqual([...firstFollow.entries, ...secondFollow.entries].map(row => row.id), defaultFollow.entries.map(row => row.id)) + assert.deepEqual((await readFollowPage({ cursor: secondFollow.cursor, limit: 1 })).entries, []) + console.log("✓ MCP logs follow: requested batch limit and lossless cursor progression") + const query = (filters: { from?: string; cursor?: string }) => client.callTool({ + name: "mm_logs_query", arguments: { serverId: 1, q: marker, limit: 1, to, ...filters }, + }) + type Page = { entries: { id: string }[]; nextCursor: string | null } + const first = await query({ from }) + assert.ok(!first.isError) + const firstPage = (first.structuredContent as { result: Page }).result + assert.equal(firstPage.entries.length, 1) + assert.ok(firstPage.nextCursor) + const next = await query({ cursor: firstPage.nextCursor }) + assert.ok(!next.isError) + const nextPage = (next.structuredContent as { result: Page }).result + assert.equal(nextPage.entries.length, 1) + assert.notEqual(nextPage.entries[0].id, firstPage.entries[0].id) + assert.equal(nextPage.nextCursor, null) + console.log("✓ MCP logs query: historical pagination resumes without resending from") + + await pool.query("UPDATE router_log_settings SET enabled=true WHERE id=1") + for (const source of ["permissions", "server"] as const) { + const sourceChanged = source === "permissions" + ? ["UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [access.connection.id]] as const + : ["UPDATE servers SET enabled=false WHERE id=$1", [1]] as const + const restoreSource = source === "permissions" + ? ["UPDATE mcp_connections SET owner_permissions=$2::jsonb WHERE id=$1", [access.connection.id, JSON.stringify(actor.permissions)]] as const + : ["UPDATE servers SET enabled=true WHERE id=$1", [1]] as const + let reads = 0 + pool.query = (async (...args: unknown[]) => { + const result = await Reflect.apply(originalQuery, pool, args) + if (typeof args[0] === "string" && args[0].startsWith("SELECT GREATEST(COALESCE(max(id),0)")) { + reads++ + if (reads === 1) await Reflect.apply(originalQuery, pool, sourceChanged) + } + return result + }) as typeof originalQuery + try { + const stopped = await client.callTool({ name: "mm_logs_follow", arguments: { + serverId: 1, from, to, q: marker, cursor: secondFollow.cursor, waitSeconds: 5, + } }) + assert.equal(stopped.isError, true) + assert.match(JSON.stringify(stopped), source === "permissions" ? /RESET_REQUIRED/ : /DEVICE_UNAVAILABLE/) + assert.doesNotMatch(JSON.stringify(stopped), /"cursor"|"entries"/) + assert.equal(reads, 1, "A stopped follower does not fetch a second batch") + } finally { + pool.query = originalQuery + await Reflect.apply(originalQuery, pool, restoreSource) + } + } + console.log("✓ MCP logs follow: owner permissions and source availability are rechecked while waiting") + } finally { + await pool.query("UPDATE router_log_settings SET enabled=$1 WHERE id=1", [initialSettings.enabled]) + await pool.query("DELETE FROM router_log_entries WHERE server_id=1 AND message=$1", [marker]) + } + } finally { + bound.abort() + pool.query = originalQuery + app.server.off("request", observeClose) + await client.close() + } +} diff --git a/backend/src/modules/mcp/management-transport-acceptance.ts b/backend/src/modules/mcp/management-transport-acceptance.ts new file mode 100644 index 00000000..6576187a --- /dev/null +++ b/backend/src/modules/mcp/management-transport-acceptance.ts @@ -0,0 +1,167 @@ +import assert from "node:assert/strict" +import { createServer, type ServerResponse } from "node:http" +import { randomUUID } from "node:crypto" +import { SignJWT } from "jose" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "../../services/partition-write-transaction.js" +import { registerManagementChannel, verifyManagementChannel } from "./management.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { encryptSecret } from "./security.js" +import { assertManagementTransport, assertManagementTransportBinding, managementTransportClient, patchScopedCommunityViaManagementTransport, prepareManagementTransportBinding, resolveManagementTransport } from "./management-transport.js" + +/** SQL + two explicit loopback listeners. Never addresses a real router. */ +export async function acceptManagementTransport(actor: AuthUser, admissionOnly = false) { + const sid = 1707000 + Math.floor(Math.random() * 100000) + let primaryReads = 0, alternateReads = 0, patches = 0, primaryDown = false, authorized = true + let inserted = false + let mode: "normal" | "identity-mismatch" | "lost" | "redirect" | "huge" | "slow" = "normal" + const responses = new Set() + const primary = createServer((req, res) => { + primaryReads++ + if (primaryDown) { req.socket.destroy(); return } + res.setHeader("content-type", "application/json"); res.end(JSON.stringify([{ "system-id": "fixed-loopback-router" }])) + }) + const alternate = createServer(async (req, res) => { + alternateReads++; res.setHeader("content-type", "application/json") + if (req.url === "/rest/system/license") { res.end(JSON.stringify([{ "system-id": mode === "identity-mismatch" ? "other-router" : "fixed-loopback-router" }])); return } + if (req.method === "PATCH") { + patches++; assert.equal(decodeURIComponent(req.url!), "/rest/routing/filter/rule/*a") + let raw = ""; for await (const chunk of req) raw += chunk + assert.deepEqual(JSON.parse(raw), { rule: "original-rule" }) + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}"); return + } + if (mode === "redirect") { res.writeHead(307, { Location: "http://127.0.0.1:1/never-follow" }); res.end("{}"); return } + if (mode === "huge") { res.end('"' + "x".repeat(1048577) + '"'); return } + if (mode === "slow") { responses.add(res); res.once("close", () => responses.delete(res)); res.writeHead(200); res.write("["); return } + res.end(JSON.stringify({ version: "7.20", fixture: "alternate" })) + }) + const options = () => ({ authorize: async () => { if (!authorized) throw new Error("fixture authority revoked") }, deadline: Date.now() + 30000 }) + try { + const listen = (server: ReturnType, host: string) => new Promise((resolve, reject) => { + server.once("error", reject) + server.listen(0, host, () => { server.off("error", reject); resolve() }) + }) + await listen(primary, "127.0.0.1"); await listen(alternate, "127.0.0.2") + const primaryPort = (primary.address() as { port: number }).port, alternatePort = (alternate.address() as { port: number }).port + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'management-transport-fixture','127.0.0.1',$2,false,'fixture-primary-private')", [sid, primaryPort]) + inserted = true + const input = { serverId: sid, host: "127.0.0.2", port: alternatePort, useSsl: false, verifySsl: true, + username: "fixture", password: "fixture-alternate-private", attestation: "Fixture independent loopback channel for SQL acceptance", expiresAt: new Date(Math.min(Date.now() + 600000, actor.expiresAt ?? Infinity) - 1000).toISOString() } + const channel = await registerManagementChannel(input, { ...actor, permissions: [...new Set([...actor.permissions, "mm:network:admin"])] }) + if (admissionOnly) { + const { buildApp } = await import("../../index.js") + const app = await buildApp({ startScheduler: false, logger: false }) + const blocker = await pool.connect() + try { + const token = await new SignJWT({ sub: actor.id, apps: actor.apps, permissions: [...actor.permissions, "mm:mcp:admin"] }) + .setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime("15m").setIssuer(process.env.AUTH_ISSUER!) + .sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET!)) + const headers = { authorization: `Bearer ${token}` }, url = "/api/mcp/management-channels" + const register = () => app.inject({ method: "POST", url, headers, payload: input }) + const revoke = (id: string) => app.inject({ method: "DELETE", url: `${url}/${id}`, headers }) + const snapshot = () => pool.query(`SELECT (SELECT to_jsonb(c) FROM mcp_management_channels c WHERE c.server_id=$1) AS channel, + (SELECT coalesce(jsonb_agg(action ORDER BY id), '[]') FROM mcp_audit WHERE actor_id=$2 AND action LIKE 'management.%') AS audit`, [sid, actor.id]).then(result => result.rows[0]) + const before = await snapshot() + await blocker.query("BEGIN") + await blocker.query("SELECT pg_advisory_xact_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + const [, refusedRegistration, refusedRevocation] = await Promise.all([ + assert.rejects(verifyManagementChannel(sid, channel.id)), register(), revoke(channel.id), + ]) + assert.deepEqual([refusedRegistration.statusCode, refusedRevocation.statusCode], [503, 503]) + assert.deepEqual(await snapshot(), before) + await blocker.query("ROLLBACK") + assert.equal((await verifyManagementChannel(sid, channel.id)).channelId, channel.id) + assert.notEqual((await snapshot()).channel.verified_at, before.channel.verified_at) + const replacement = await register() + assert.equal(replacement.statusCode, 200, replacement.body) + assert.notEqual(replacement.json().id, channel.id) + assert.equal((await revoke(replacement.json().id)).statusCode, 200) + const after = await snapshot() + assert.ok(after.channel.revoked_at) + assert.deepEqual(after.audit, [...before.audit, "management.register", "management.revoke"]) + await assert.rejects(verifyManagementChannel(sid, replacement.json().id), { code: "APPROVAL_REQUIRED" }) + assert.equal(patches, 0) + return { accepted: true, protectedWrites: 3, routerWrites: patches } + } finally { + try { await blocker.query("ROLLBACK") } finally { blocker.release(); await app.close() } + } + } + const binding = await prepareManagementTransportBinding(sid, options()) + const beforeMetadataPrimary = primaryReads, beforeMetadataAlternate = alternateReads + const metadata = await assertManagementTransportBinding(binding) + assert.equal(metadata.channelId, binding.channelId) + assert.equal(primaryReads, beforeMetadataPrimary); assert.equal(alternateReads, beforeMetadataAlternate) + const source = await createRoutingSourceContext(await readRoutingEndpoints([sid])) + assert.ok(!JSON.stringify(binding).includes("fixture-alternate-private")); assert.ok(!JSON.stringify(binding).includes("127.0.0.2")) + primaryDown = true + const beforePrimary = primaryReads + const capability = await resolveManagementTransport(binding, options()), client = managementTransportClient(capability) + assert.equal((await client.get<{ fixture: string }>("/system/resource")).fixture, "alternate") + assert.equal(primaryReads, beforePrimary, "resolve/read must not require reachable primary") + assert.deepEqual((await createRoutingSourceContext(await readRoutingEndpoints([sid]))).expected, source.expected, "alternate transport preserves logical primary HMAC") + await patchScopedCommunityViaManagementTransport(capability, "*a", "original-rule") + assert.equal(patches, 1) + await assert.rejects(patchScopedCommunityViaManagementTransport(capability, "*a", "original-rule"), /Повтор/) + assert.equal(patches, 1) + + mode = "lost" + const lost = await resolveManagementTransport(binding, options()) + await assert.rejects(patchScopedCommunityViaManagementTransport(lost, "*a", "original-rule")) + await assert.rejects(patchScopedCommunityViaManagementTransport(lost, "*a", "original-rule"), /Повтор/) + assert.equal(patches, 2, "lost inverse is not replayed by the same capability") + mode = "identity-mismatch" + await assert.rejects(resolveManagementTransport(binding, options()), /другому RouterOS/) + mode = "normal" + + for (const value of ["redirect", "huge", "slow"] as const) { + const bounded = managementTransportClient(await resolveManagementTransport(binding, options())) + mode = value + const before = alternateReads + await assert.rejects(bounded.get("/system/resource", 100)) + assert.equal(alternateReads, before + 1, "failed read is never retried or redirected") + mode = "normal" + } + const abort = new AbortController(), cancelled = managementTransportClient(await resolveManagementTransport(binding, options()), abort.signal) + abort.abort(); const beforeCancelled = alternateReads + await assert.rejects(cancelled.get("/system/resource")); assert.equal(alternateReads, beforeCancelled) + await assert.rejects(resolveManagementTransport(binding, { ...options(), deadline: Date.now() - 1 })) + assert.equal(alternateReads, beforeCancelled) + + const beforeRevocation = alternateReads + authorized = false + await assert.rejects(client.get("/system/resource"), /revoked/) + assert.equal(alternateReads, beforeRevocation); authorized = true + const saved = (await pool.query("SELECT * FROM mcp_management_channels WHERE id=$1", [channel.id])).rows[0] + await pool.query("UPDATE mcp_management_channels SET generation='different-fixture-generation' WHERE id=$1", [channel.id]) + await assert.rejects(assertManagementTransportBinding(binding)) + await assert.rejects(resolveManagementTransport(binding, options())); assert.equal(alternateReads, beforeRevocation) + await pool.query("UPDATE mcp_management_channels SET generation=$2 WHERE id=$1", [channel.id, saved.generation]) + await pool.query("UPDATE mcp_management_channels SET revoked_at=now() WHERE id=$1", [channel.id]) + await assert.rejects(assertManagementTransportBinding(binding)) + await assert.rejects(assertManagementTransport(capability)); assert.equal(alternateReads, beforeRevocation) + await pool.query("UPDATE mcp_management_channels SET revoked_at=NULL,id=$2 WHERE id=$1", [channel.id, randomUUID()]) + await assert.rejects(assertManagementTransportBinding(binding)) + await assert.rejects(resolveManagementTransport(binding, options())) + await pool.query("UPDATE mcp_management_channels SET id=$2 WHERE server_id=$1", [sid, channel.id]) + await pool.query("UPDATE mcp_management_channels SET config_sealed=$2 WHERE id=$1", [channel.id, await encryptSecret(JSON.stringify({ host: "127.0.0.2", port: alternatePort, useSsl: false, verifySsl: true, username: "fixture", password: "changed-private" }))]) + await assert.rejects(assertManagementTransportBinding(binding)) + await assert.rejects(resolveManagementTransport(binding, options())); assert.equal(alternateReads, beforeRevocation) + await pool.query("UPDATE mcp_management_channels SET config_sealed=$2,expires_at=now()-interval '1 second' WHERE id=$1", [channel.id, saved.config_sealed]) + await assert.rejects(assertManagementTransportBinding(binding)) + await assert.rejects(resolveManagementTransport(binding, options())) + await pool.query("UPDATE mcp_management_channels SET expires_at=$2 WHERE id=$1", [channel.id, saved.expires_at]) + await pool.query("UPDATE servers SET port=$2 WHERE id=$1", [sid, primaryPort === 65535 ? 65534 : primaryPort + 1]) + await pool.query("UPDATE servers SET port=$2 WHERE id=$1", [sid, primaryPort]) + await assert.rejects(assertManagementTransportBinding(binding)) + await assert.rejects(resolveManagementTransport(binding, options())); assert.equal(alternateReads, beforeRevocation) + assert.equal(primaryReads, beforePrimary) + return { accepted: true, primaryRequestsAfterFailure: 0, exactPatches: patches, noReplay: true, logicalSourcePreserved: true, metadataAdmissionCases: 7, metadataNativeRequests: 0 } + } finally { + for (const response of responses) response.destroy() + primary.closeAllConnections(); alternate.closeAllConnections() + await Promise.all([new Promise(resolve => primary.close(() => resolve())), new Promise(resolve => alternate.close(() => resolve()))]) + if (inserted) await pool.query("DELETE FROM servers WHERE id=$1", [sid]) + } +} diff --git a/backend/src/modules/mcp/management-transport.test.ts b/backend/src/modules/mcp/management-transport.test.ts new file mode 100644 index 00000000..b91eed10 --- /dev/null +++ b/backend/src/modules/mcp/management-transport.test.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { assertManagementTransport, assertManagementTransportBinding, managementTransportClient, patchScopedCommunityViaManagementTransport, resolveManagementTransport, type ManagementTransportBinding, type ManagementTransportCapability } from "./management-transport.js" + +test("JSON properties cannot forge a registered transport capability", async () => { + const forged = JSON.parse('{"kind":"verified-management-transport","serverId":1,"endpoint":{"host":"127.0.0.1","port":1}}') as ManagementTransportCapability + assert.throws(() => managementTransportClient(forged), /capability/) + await assert.rejects(assertManagementTransport(forged), /capability/) + await assert.rejects(patchScopedCommunityViaManagementTransport(forged, "*a", "reject;"), /capability/) +}) + +test("plain endpoint input cannot be resolved as a sealed management binding", async () => { + const raw = { revision: 1, serverId: 1, channelId: "missing", expiresAt: new Date(Date.now() + 60000).toISOString(), host: "127.0.0.1", port: 1 } as unknown as ManagementTransportBinding + await assert.rejects(resolveManagementTransport(raw, { authorize: async () => {}, deadline: Date.now() + 1000 })) + await assert.rejects(assertManagementTransportBinding(raw)) +}) + +test("metadata admission refuses cancelled or unsealed input without native capability", async () => { + const abort = new AbortController(); abort.abort() + await assert.rejects(assertManagementTransportBinding({} as ManagementTransportBinding, abort.signal), /отменён/) + for (const raw of [{ kind: "verified-management-transport" }, { revision: 1, serverId: 1, channelId: "missing", proof: "f".repeat(64), sealed: "forged" }]) { + await assert.rejects(assertManagementTransportBinding(raw as unknown as ManagementTransportBinding)) + } +}) diff --git a/backend/src/modules/mcp/management-transport.ts b/backend/src/modules/mcp/management-transport.ts new file mode 100644 index 00000000..59133ef2 --- /dev/null +++ b/backend/src/modules/mcp/management-transport.ts @@ -0,0 +1,193 @@ +import http from "node:http" +import https from "node:https" +import { isIP } from "node:net" +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { createRoutingSourceContext, readRoutingEndpoints, type RoutingEndpoint } from "./routing-observation-source.js" +import { patchScopedCommunity } from "./routing-community-scoped-native.js" + +const proof = z.string().regex(/^[a-f0-9]{64}$/) +const packetSchema = z.object({ revision: z.literal(1), serverId: z.number().int().positive(), channelId: z.string().uuid(), + generation: z.string().min(1), expiresAt: z.string().datetime(), identityHash: proof, configProof: proof, sourceProof: proof }).strict() +const bindingSchema = z.object({ revision: z.literal(1), serverId: z.number().int().positive(), channelId: z.string().uuid(), expiresAt: z.string().datetime(), + sealed: z.string().min(1).max(131072), proof }).strict() +const configSchema = z.object({ host: z.string().refine(value => !!isIP(value)), port: z.number().int().min(1).max(65535), + useSsl: z.boolean(), verifySsl: z.literal(true), username: z.string().min(1).max(100), password: z.string().min(1).max(1000) }).strict() +export type ManagementTransportBinding = Readonly> +/** No property or serialized token can manufacture the WeakMap capability. */ +export interface ManagementTransportCapability { readonly kind: "verified-management-transport" } +export type ManagementTransportOptions = { authorize: () => Promise; deadline: number; signal?: AbortSignal } +type Packet = z.infer +type Budget = { bytes: number; calls: number } +type State = { packet: Packet; endpoint: Readonly; options: ManagementTransportOptions; signal: AbortSignal; budget: Budget; writeAttempted: boolean } +const capabilities = new WeakMap() +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, 409) +const privateProof = (value: unknown, domain: string) => privateSecretFingerprint(fingerprint(value), "management-transport-" + domain + "-v1") + +function signalFor(options: ManagementTransportOptions, expiry: string) { + const remaining = Math.min(options.deadline, Date.parse(expiry), Date.now() + 120000) - Date.now() + if (!Number.isFinite(remaining) || remaining <= 0 || options.signal?.aborted) throw fail("Срок независимого транспорта истёк", "READ_CANCELLED") + return AbortSignal.any([AbortSignal.timeout(Math.max(1, Math.floor(remaining))), ...(options.signal ? [options.signal] : [])]) +} +function state(capability: ManagementTransportCapability) { + const value = capabilities.get(capability) + if (!value) throw fail("Неподтверждённая capability независимого транспорта", "PERMISSION_DENIED") + if (value.signal.aborted || Date.now() >= value.options.deadline) throw fail("Независимый transport отменён или истёк", "READ_CANCELLED") + return value +} +async function currentChannel(serverId: number, channelId?: string, db: Pick = pool) { + const rows = (await db.query("SELECT id,server_id,config_sealed,identity_hash,generation,expires_at FROM mcp_management_channels WHERE server_id=$1 AND revoked_at IS NULL AND expires_at>now()", [serverId])).rows + const row = rows[0] + if (rows.length !== 1 || channelId && row.id !== channelId || row.generation !== await accessGeneration()) throw fail("Допуск независимого транспорта изменён или отозван", "APPROVAL_REQUIRED") + if (typeof row.config_sealed !== "string" || Buffer.byteLength(row.config_sealed) > 131072) throw fail("Некорректный размер конфигурации management") + let config: z.infer + try { config = configSchema.parse(JSON.parse(await decryptSecret(row.config_sealed))) } + catch { throw fail("Конфигурация management не подтверждена") } + return { row, config, configProof: await privateProof(config, "config") } +} +async function logicalSource(serverId: number, db: Pick = pool) { + const endpoints = await readRoutingEndpoints([serverId], db) + if (endpoints.length !== 1 || !endpoints[0].enabled) throw fail("Логический источник управления отключён или отсутствует") + const source = await createRoutingSourceContext(endpoints) + return { endpoint: endpoints[0], proof: source.expected[0].proof } +} + +/** Fixed registered endpoint, one request, no redirects/retries; actual bytes + * are bounded before parse across all context/probe batches of a capability. */ +async function read(endpoint: Readonly, path: string, body: Record | undefined, + signal: AbortSignal, deadline: number, budget: Budget, timeoutMs = 10000): Promise { + if (signal.aborted || Date.now() >= deadline || ++budget.calls > 2000) throw fail("Истёк бюджет независимого транспорта", "READ_CANCELLED") + if (!/^\/[a-z0-9/.-]+$/i.test(path) || path.length > 1000) throw fail("Некорректный путь независимого транспорта", "INVALID_INPUT") + return new Promise((resolve, reject) => { + const req = (endpoint.useSsl ? https : http).request({ hostname: endpoint.host, port: endpoint.port, path: "/rest" + path, + method: body ? "POST" : "GET", signal, rejectUnauthorized: endpoint.useSsl ? endpoint.verifySsl : undefined, + headers: { Authorization: "Basic " + Buffer.from(endpoint.username + ":" + endpoint.password).toString("base64"), "Content-Type": "application/json" } }, res => { + const chunks: Buffer[] = []; let bytes = 0 + res.once("error", reject); res.once("aborted", () => reject(fail("Ответ management прерван", "STATE_UNAVAILABLE"))) + if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) { req.destroy(fail("Management ответ не подтверждён", "STATE_UNAVAILABLE")); res.destroy(); return } + if (Number(res.headers["content-length"] ?? 0) > 1048576) { req.destroy(fail("Ответ management превышает лимит", "LIMIT_EXCEEDED")); res.destroy(); return } + res.on("data", (chunk: Buffer) => { + bytes += chunk.length; budget.bytes += chunk.length + if (bytes > 1048576 || budget.bytes > 8 * 1048576) { req.destroy(fail("Бюджет management превышен", "LIMIT_EXCEEDED")); res.destroy(); return } + chunks.push(chunk) + }) + res.once("end", () => { + try { resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")) as T) } + catch { reject(fail("Некорректный JSON management", "INVALID_NATIVE_RESPONSE")) } + }) + }) + const timer = setTimeout(() => req.destroy(fail("Истёк срок запроса management", "READ_CANCELLED")), Math.max(1, Math.min(timeoutMs, deadline - Date.now()))) + req.once("error", reject); req.once("close", () => clearTimeout(timer)) + req.end(body ? JSON.stringify(body) : undefined) + }) +} +async function verifyIdentity(value: State) { + const raw = await read(value.endpoint, "/system/license", undefined, value.signal, value.options.deadline, value.budget, 5000) + const parsed = z.union([z.record(z.string(), z.unknown()), z.array(z.record(z.string(), z.unknown())).length(1)]).parse(raw) + const row = Array.isArray(parsed) ? parsed[0] : parsed + const identity = row["system-id"] ?? row["software-id"] + if (typeof identity !== "string" || !identity.trim() || fingerprint(identity.trim()) !== value.packet.identityHash) throw fail("Независимый канал ведёт к другому RouterOS") +} + +/** Preparation intentionally retains the original primary+alternate attestation. */ +export async function prepareManagementTransportBinding(serverId: number, options: ManagementTransportOptions): Promise { + if (options.signal?.aborted || Date.now() >= options.deadline) throw fail("Подготовка management отменена", "READ_CANCELLED") + await options.authorize() + const source = await logicalSource(serverId), { row, config, configProof } = await currentChannel(serverId) + const packet = packetSchema.parse({ revision: 1, serverId, channelId: row.id, generation: row.generation, expiresAt: row.expires_at.toISOString(), + identityHash: row.identity_hash, configProof, sourceProof: source.proof }) + const initial: State = { packet, endpoint: Object.freeze({ ...source.endpoint }), options: { ...options }, signal: signalFor(options, packet.expiresAt), budget: { bytes: 0, calls: 0 }, writeAttempted: false } + const fence = async () => { + await options.authorize() + if (initial.signal.aborted || Date.now() >= options.deadline) throw fail("Подготовка management отменена", "READ_CANCELLED") + const current = await currentChannel(serverId, row.id), after = await logicalSource(serverId) + if (after.proof !== source.proof || current.configProof !== configProof || current.row.identity_hash !== row.identity_hash || current.row.expires_at.getTime() !== row.expires_at.getTime() || current.row.generation !== row.generation) throw fail("Management изменился во время подготовки") + } + // Both identities are checked at fixed, captured endpoints. Re-reading a + // routerClient here would risk contacting a newly edited primary address. + await fence(); await verifyIdentity(initial); await fence() + await verifyIdentity({ ...initial, endpoint: Object.freeze({ ...source.endpoint, ...config }) }); await fence() + const binding = Object.freeze({ revision: 1 as const, serverId, channelId: packet.channelId, expiresAt: packet.expiresAt, + sealed: await encryptSecret(JSON.stringify(packet)), proof: await privateProof(packet, "binding") }) + // Also proves the packet can be resolved while its original bounds hold. + await resolveManagementTransport(binding, options) + return binding +} + +async function inspectManagementBinding(raw: ManagementTransportBinding, signal?: AbortSignal, db: Pick = pool) { + if (signal?.aborted) throw fail("Management binding отменён", "READ_CANCELLED") + const binding = bindingSchema.parse(raw), packet = packetSchema.parse(JSON.parse(await decryptSecret(binding.sealed))) + if (binding.proof !== await privateProof(packet, "binding") || binding.serverId !== packet.serverId || binding.channelId !== packet.channelId || binding.expiresAt !== packet.expiresAt) throw fail("Management binding повреждён") + if (Date.parse(packet.expiresAt) <= Date.now()) throw fail("Management binding истёк", "READ_CANCELLED") + const channel = await currentChannel(packet.serverId, packet.channelId, db), source = await logicalSource(packet.serverId, db) + if (packet.generation !== channel.row.generation || packet.expiresAt !== channel.row.expires_at.toISOString() || packet.identityHash !== channel.row.identity_hash || packet.configProof !== channel.configProof || packet.sourceProof !== source.proof) throw fail("Management binding больше не соответствует источнику") + if (signal?.aborted || Date.parse(packet.expiresAt) <= Date.now()) throw fail("Management binding отменён или истёк", "READ_CANCELLED") + return { binding, packet, channel, source } +} + +/** Metadata-only admission: fixed private HMAC, current registered channel and + * logical endpoint epochs. No native HTTP, identity probe or capability is + * created. The caller checks its live owner/policy separately; deliberately no + * authorize callback here, avoiding access -> metadata -> access recursion. */ +export async function assertManagementTransportBinding(raw: ManagementTransportBinding, signal?: AbortSignal, db: Pick = pool) { + const { binding } = await inspectManagementBinding(raw, signal, db) + return { serverId: binding.serverId, channelId: binding.channelId, proof: binding.proof, expiresAt: binding.expiresAt } +} + +/** Primary is read from SQL for epochs only; no request is sent to its address. */ +export async function resolveManagementTransport(raw: ManagementTransportBinding, options: ManagementTransportOptions): Promise { + await options.authorize() + const { packet, channel, source } = await inspectManagementBinding(raw, options.signal) + const signal = signalFor(options, packet.expiresAt) + const capability: ManagementTransportCapability = Object.freeze({ kind: "verified-management-transport" }) + const value: State = { packet, endpoint: Object.freeze({ ...source.endpoint, ...channel.config }), options: { ...options }, signal, budget: { bytes: 0, calls: 0 }, writeAttempted: false } + capabilities.set(capability, value) + try { await assertManagementTransport(capability); await verifyIdentity(value); await assertManagementTransport(capability); return capability } + catch (error) { capabilities.delete(capability); throw error } +} + +/** Live policy callback, logical source epochs and exact registered channel. */ +export async function assertManagementTransport(capability: ManagementTransportCapability) { + const value = state(capability) + await value.options.authorize() + const channel = await currentChannel(value.packet.serverId, value.packet.channelId), source = await logicalSource(value.packet.serverId) + if (channel.row.generation !== value.packet.generation || channel.row.identity_hash !== value.packet.identityHash || channel.row.expires_at.toISOString() !== value.packet.expiresAt || channel.configProof !== value.packet.configProof || source.proof !== value.packet.sourceProof) throw fail("Management канал или source epoch изменился") + state(capability) +} + +/** Used only by internal context options; unknown/forged capabilities fail closed. */ +export function managementTransportClient(capability: ManagementTransportCapability, signal?: AbortSignal) { + const value = state(capability) + const requestSignal = signal ? AbortSignal.any([signal, value.signal]) : value.signal + const request = async (path: string, body?: Record, timeoutMs?: number) => { + if (requestSignal.aborted) throw fail("Контекст management отменён", "READ_CANCELLED") + await assertManagementTransport(capability) + const result = await read(value.endpoint, path, body, requestSignal, value.options.deadline, value.budget, timeoutMs) + await assertManagementTransport(capability) + if (requestSignal.aborted) throw fail("Контекст management отменён", "READ_CANCELLED") + return result + } + return { serverId: value.packet.serverId, get: (path: string, timeoutMs?: number) => request(path, undefined, timeoutMs), + post: (path: string, body: Record, timeoutMs?: number) => { + if (!path.endsWith("/get") && !path.endsWith("/print") && !["/tool/ping", "/tool/traceroute"].includes(path)) throw fail("Management контекст допускает только чтение", "PERMISSION_DENIED") + return request(path, body, timeoutMs) + } } +} + +/** Durable recovery intent remains the caller's obligation. A capability may + * attempt only one inverse PATCH, including a lost/aborted response. */ +export async function patchScopedCommunityViaManagementTransport(capability: ManagementTransportCapability, nativeId: string, rule: string, configurationSignal?: AbortSignal) { + const value = state(capability) + configurationSignal?.throwIfAborted() + if (value.writeAttempted) throw fail("Повтор management PATCH запрещён", "MUTATION_OUTCOME_UNKNOWN") + value.writeAttempted = true + await assertManagementTransport(capability) + await verifyIdentity(value) + await assertManagementTransport(capability) + configurationSignal?.throwIfAborted() + const signal = configurationSignal ? AbortSignal.any([value.signal, configurationSignal]) : value.signal + await patchScopedCommunity(value.endpoint, nativeId, rule, signal) + await assertManagementTransport(capability) +} diff --git a/backend/src/modules/mcp/management.ts b/backend/src/modules/mcp/management.ts new file mode 100644 index 00000000..184426ed --- /dev/null +++ b/backend/src/modules/mcp/management.ts @@ -0,0 +1,72 @@ +import { isIP } from "node:net" +import { lookup } from "node:dns/promises" +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { MikrotikClient, type MikrotikConnectParams } from "../../services/mikrotik.js" +import { routerClient } from "./resources.js" +import { accessGeneration, encryptSecret, decryptSecret, fingerprint, McpError } from "./security.js" +import { audit, connectionWrite } from "./store.js" +import type { AuthUser } from "../../lib/permissions.js" +import { hasPermission } from "../../lib/permissions.js" + +export const managementChannelSchema = z.object({ + serverId: z.number().int().positive(), host: z.string().refine(value => !!isIP(value), "Нужен буквальный IP независимого канала"), + port: z.number().int().min(1).max(65535), useSsl: z.boolean(), verifySsl: z.literal(true), + username: z.string().min(1).max(100), password: z.string().min(1).max(1000), + attestation: z.string().trim().min(30).max(2000), expiresAt: z.string().datetime({ offset: true }), +}).strict() +async function identity(client: MikrotikClient) { + const raw = await client.get | Record[]>("/system/license", 5000) + const row = Array.isArray(raw) ? raw[0] : raw + const id = row?.["system-id"] ?? row?.["software-id"] + if (typeof id !== "string" || !id.trim()) throw new McpError("UNSUPPORTED_CAPABILITY", "Устройство не подтвердило стабильный идентификатор лицензии", 409) + return fingerprint(id.trim()) +} +export async function registerManagementChannel(raw: unknown, actor: AuthUser) { + if (!hasPermission(actor.permissions, "mm:network:admin")) throw new McpError("PERMISSION_DENIED", "Для допуска независимого управления нужны права администратора сети") + const input = managementChannelSchema.parse(raw), { server, client } = await routerClient(input.serverId) + const primary = isIP(server.host) ? [server.host] : (await lookup(server.host, { all: true })).map(value => value.address) + if (primary.includes(input.host)) throw new McpError("INVALID_INPUT", "Другой порт или DNS-алиас основного адреса не является независимым каналом", 400) + const expiry = Date.parse(input.expiresAt) + if (expiry <= Date.now() || expiry > Math.min(Date.now() + 86400000, actor.expiresAt ?? Date.now() + 900000)) throw new McpError("INVALID_EXPIRATION", "Допуск канала ограничен 24 часами и текущими правами человека", 400) + const config: MikrotikConnectParams = { host: input.host, port: input.port, useSsl: input.useSsl, verifySsl: true, username: input.username, password: input.password } + const alternate = new MikrotikClient(config) + const generation = await accessGeneration() + const [a, b] = await Promise.all([identity(client), identity(alternate)]) + if (a !== b) throw new McpError("STATE_CONFLICT", "Независимый адрес ведёт к другому RouterOS", 409) + const id = randomUUID(), sealed = await encryptSecret(JSON.stringify(config)) + await connectionWrite(async db => { + if (generation !== await accessGeneration() || expiry <= Date.now()) throw new McpError("APPROVAL_REQUIRED", "Срок или поколение допуска канала изменились во время проверки", 409) + await db.query("INSERT INTO mcp_management_channels(id,server_id,config_sealed,identity_hash,attestation,created_by,generation,expires_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8) ON CONFLICT(server_id) DO UPDATE SET id=EXCLUDED.id,config_sealed=EXCLUDED.config_sealed,identity_hash=EXCLUDED.identity_hash,attestation=EXCLUDED.attestation,created_by=EXCLUDED.created_by,generation=EXCLUDED.generation,expires_at=EXCLUDED.expires_at,verified_at=now(),revoked_at=NULL", [id, input.serverId, sealed, a, input.attestation, actor.id, generation, input.expiresAt]) + await audit(db, actor.id, "management.register", null, { channelId: id, serverId: input.serverId, expiresAt: input.expiresAt, verification: "identity-and-reachability", independence: "human-attestation" }) + }) + return { id, serverId: input.serverId, expiresAt: input.expiresAt, verification: "identity-and-reachability", independence: "human-attestation" } +} +async function verifiedManagementChannel(serverId: number, requiredId?: string) { + const { rows: [row] } = await pool.query("SELECT * FROM mcp_management_channels WHERE server_id=$1 AND revoked_at IS NULL AND expires_at>now()", [serverId]) + if (!row || requiredId && row.id !== requiredId || row.generation !== await accessGeneration()) throw new McpError("APPROVAL_REQUIRED", "Нет актуального проверенного независимого канала управления", 409) + const config = JSON.parse(await decryptSecret(row.config_sealed)) as MikrotikConnectParams + const primary = (await routerClient(serverId)).client + const hashes = await Promise.all([identity(new MikrotikClient(config)), identity(primary)]) + if (hashes.some(value => value !== row.identity_hash)) throw new McpError("STATE_CONFLICT", "Идентичность основного или management-канала изменилась", 409) + await connectionWrite(async db => { + const changed = await db.query("UPDATE mcp_management_channels SET verified_at=clock_timestamp() WHERE id=$1 AND generation=$2 AND revoked_at IS NULL AND expires_at>clock_timestamp() RETURNING id", [row.id, await accessGeneration()]) + if (!changed.rowCount) throw new McpError("APPROVAL_REQUIRED", "Допуск management-канала был отозван во время проверки", 409) + }) + return { channelId: row.id as string, serverId, expiresAt: row.expires_at.toISOString() as string, host: config.host } +} +export async function verifyManagementChannel(serverId: number, requiredId?: string) { + const { host: _host, ...receipt } = await verifiedManagementChannel(serverId, requiredId) + return receipt +} +/** Internal VLAN preparation uses the verified endpoint to trace its actual + * address/interface. It is not accepted from tool input or returned in the + * public channel receipt. */ +export async function verifyBridgeManagementChannel(serverId: number, requiredId?: string) { + return verifiedManagementChannel(serverId, requiredId) +} +export async function managementEvidence(serverIds: number[]) { + try { return { requiresHuman: false, managementChannels: await Promise.all([...new Set(serverIds)].map(serverId => verifyManagementChannel(serverId))) } } + catch { return { requiresHuman: true, managementChannels: undefined } } +} diff --git a/backend/src/modules/mcp/manual-api-ping-actions.test.ts b/backend/src/modules/mcp/manual-api-ping-actions.test.ts new file mode 100644 index 00000000..87cc8851 --- /dev/null +++ b/backend/src/modules/mcp/manual-api-ping-actions.test.ts @@ -0,0 +1,120 @@ +/** Ordinary MCP adapter + ACTUAL shared manual executor with inert collector + * and alert hook. No database, network, notifications or scheduler execution. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { beforeEach, mock, test } from "node:test" +import { createManualDataCollection } from "../../services/manual-data-collection-execution.js" +import type { add } from "./actions.js" + +const permission="mm:servers:write",principal={connectionId:randomUUID(),credentialId:randomUUID(),credentialExpiresAt:new Date(Date.now()+60000).toISOString()} +const context={principal,connection:{owner_id:"owned-manual-actor"}} as Parameters[8]["prepare"]>>[1] +const forward={phase:"forward" as const,operationId:randomUUID(),stepIndex:0} +const epochs=[randomUUID(),randomUUID(),randomUUID()],queries:string[]=[],events:string[]=[] +let connection:{owner_id:string;owner_permissions:string[];policy:{version:number;permissions:string[];scope:{allowGlobal:boolean;serverIds:number[]}}} +let catalog:Array<{id:number;enabled:boolean;routingEndpointEpoch:string}>,settings:{enabled:boolean;intervalSec:number}|undefined,skipped=false,collectorFailure:Error|undefined,readFailure=false,failReceiptAfterCollector=false,lastError="" +let lastCollectedAt="2026-10-29T00:00:00.000Z",collectCalls=0,alertCalls=0 +const shared=createManualDataCollection({ + async collectServersRestPing(){collectCalls++;events.push("collector");if(collectorFailure)throw collectorFailure;return {skipped}}, + async readServersRestPingSettings(){events.push("receipt-settings");if(readFailure||failReceiptAfterCollector&&events.includes("global-alert-hook"))throw Error("receipt read uncertain");if(!settings){settings={enabled:false,intervalSec:120};events.push("initialize-settings")}return {lastCollectedAt,lastDurationMs:23,lastError}}, + async collectUptime(){assert.fail("wrong shared operation")},async readUptimeSettings(){return {}},async collectPingGroup(){assert.fail("wrong shared operation")}, + scheduleGlobalAlerts(){alertCalls++;events.push("global-alert-hook")}, +}) +mock.module("../../db/index.js",{namedExports:{pool:{async query(sql:string){queries.push(sql) + if(sql.startsWith('SELECT id,enabled,routing_endpoint_epoch'))return {rows:structuredClone(catalog)} + assert.equal(sql,'SELECT enabled,interval_sec AS "intervalSec" FROM servers_api_ping_settings WHERE id=1') + return {rows:settings?[structuredClone(settings)]:[]} +}}}}) +mock.module("./store.js",{namedExports:{async effectiveMcpConnection(){return structuredClone(connection)}}}) +mock.module("../../services/config-mutation-policy.js",{namedExports:{async assertVerifiedConfigurationAccess(){events.push("authorize")}}}) +mock.module("../../services/manual-data-collection.js",{namedExports:{manualDataCollection:shared}}) +const {registerManualApiPingActions}=await import("./manual-api-ping-actions.js"),{findMcpService}=await import("./service-registry.js") +let registration:Parameters|undefined +registerManualApiPingActions((...args:Parameters)=>{registration=args});assert(registration) +const [operation,tool,_title,_section,requiredPermission,effect,scope,schema,handlers]=registration,service=findMcpService("servers.api-ping.collect")! +assert(service) +async function prepare(){const evidence=await handlers.prepare!({},context),step=evidence.steps[0];assert(step.kind==="service");return {evidence,sealed:step.input}} +beforeEach(()=>{ + queries.length=0;events.length=0;collectCalls=0;alertCalls=0;skipped=false;collectorFailure=undefined;readFailure=false;failReceiptAfterCollector=false;lastError="";lastCollectedAt="2026-10-29T00:00:00.000Z" + connection={owner_id:context.connection.owner_id,owner_permissions:[permission],policy:{version:1,permissions:[permission],scope:{allowGlobal:true,serverIds:[1,2,3]}}} + catalog=epochs.map((routingEndpointEpoch,index)=>({id:index+1,enabled:index!==2,routingEndpointEpoch}));settings={enabled:true,intervalSec:120} +}) + +test("GLOBAL manual API Ping has REST-parity permission, human plan and honest full alert/outbox footprint",async()=>{ + assert.equal(operation,"servers.api-ping.collect");assert.equal(tool,"mm_api_ping_collect_plan");assert.equal(requiredPermission,permission);assert.equal(effect,"write");assert.equal(scope,"global") + assert(schema.safeParse({}).success);assert.equal(schema.safeParse({serverId:1}).success,false);assert.equal(service.cancellableDiagnostic,undefined) + const {evidence,sealed}=await prepare() + assert.deepEqual(evidence.target,{scope:"global"});assert.equal(evidence.requiresHuman,true);assert.equal(evidence.steps.length,1);assert.equal(evidence.verificationScope,"acknowledgement") + assert.deepEqual(evidence.objects.filter(value=>value.kind==="server").map(value=>value.serverId),[1,2,3]) + for(const kind of ["servers-api-ping-settings","partition-maintenance","alert-engine","alerts-outbox"])assert(evidence.objects.some(value=>value.kind===kind)) + const warnings=evidence.warnings.join(" ");for(const word of ["GLOBAL ALERT ENGINE","outbox","Telegram","samples","partitions"])assert(warnings.includes(word)) + assert.equal(typeof sealed.beforeFingerprint,"string");assert.equal(queries.length,2);assert.equal(collectCalls,0);assert.equal(alertCalls,0) + assert(queries.every(sql=>!sql.includes("password")&&!sql.includes("username")&&!sql.includes("last_collected_at"))) +}) + +test("global denial, missing permission and incomplete ALL catalog including disabled refuse before effect",async()=>{ + for(const mode of ["global","permission","owner","missing-disabled"]){ + connection.policy.scope.allowGlobal=true;connection.policy.permissions=[permission];connection.owner_permissions=[permission];connection.policy.scope.serverIds=[1,2,3] + if(mode==="global")connection.policy.scope.allowGlobal=false + if(mode==="permission")connection.policy.permissions=[] + if(mode==="owner")connection.owner_permissions=[] + if(mode==="missing-disabled")connection.policy.scope.serverIds=[1,2] + await assert.rejects(prepare());assert.equal(collectCalls,0);assert.equal(alertCalls,0) + } +}) + +test("bounded public catalog and nullable settings snapshot never initialize during plan/current",async()=>{ + settings=undefined;const {evidence,sealed}=await prepare();assert.deepEqual(await service.current(sealed),evidence.before);assert.equal(settings,undefined);assert.equal(collectCalls,0);assert(!events.includes("initialize-settings")) + settings={enabled:true,intervalSec:120};catalog=Array.from({length:1001},(_,index)=>({id:index+1,enabled:true,routingEndpointEpoch:epochs[0]})) + await assert.rejects(prepare());assert.equal(collectCalls,0);assert.equal(alertCalls,0) + assert(queries.every(sql=>sql.startsWith("SELECT ")));assert(queries.some(sql=>sql.endsWith("ORDER BY id LIMIT 1001"))) +}) + +test("missing settings initialize ONLY inside approved shared execution; null-to-configured drift invalidates old plan",async()=>{ + settings=undefined;const {sealed}=await prepare();assert.equal(settings,undefined) + const result=await service.execute(sealed,forward);assert.equal(await service.verify!(sealed,result),true);assert.deepEqual(settings,{enabled:false,intervalSec:120});assert.equal(collectCalls,1);assert.equal(alertCalls,1) + settings=undefined;const next=await prepare();settings={enabled:true,intervalSec:60} + await assert.rejects(service.execute(next.sealed,forward));assert.equal(collectCalls,1) +}) + +test("changing last receipt timestamps does not invalidate stable public configuration fingerprint",async()=>{ + const {evidence,sealed}=await prepare();lastCollectedAt="2026-10-29T00:01:00.000Z" + assert.deepEqual(await service.current(sealed),evidence.before) + const result=await service.execute(sealed,forward) as {acknowledged:boolean;lastCollectedAt:string;skipped:boolean} + assert.equal(result.acknowledged,true);assert.equal(result.lastCollectedAt,lastCollectedAt);assert.equal(result.skipped,false) + assert.equal(collectCalls,1);assert.equal(alertCalls,1) +}) + +test("one shared executor call preserves settings receipt and global alert hook; summary strips errors and claims no delivery",async()=>{ + const {sealed}=await prepare();lastError="PRIVATE_NATIVE_PASSWORD_OR_STACK" + const result=await service.execute(sealed,forward) + assert.deepEqual(result,{acknowledged:true,skipped:false,lastCollectedAt,lastDurationMs:23,lastError:true,globalAlertHookRequested:true,alertDeliveryConfirmed:false}) + assert.equal(collectCalls,1);assert.equal(alertCalls,1);assert.equal(events.filter(value=>value==="collector").length,1) + assert.deepEqual(events.slice(-4),["receipt-settings","collector","global-alert-hook","receipt-settings"]) + assert.equal(JSON.stringify(result).includes(lastError),false);assert.equal(await service.verify!(sealed,result),true) + assert.deepEqual(await service.reconcile!(sealed,async()=>{}),{matchesDesiredState:false,matchesOriginalState:false});assert.equal(collectCalls,1) +}) + +test("ACTUAL shared skipped snapshot still invokes global alert hook but MCP reports busy without collected success",async()=>{ + const {sealed}=await prepare();skipped=true + await assert.rejects(service.execute(sealed,forward),(error:unknown)=>!!error&&typeof error==="object"&&"code"in error&&error.code==="COLLECTOR_BUSY") + assert.equal(collectCalls,1);assert.equal(alertCalls,1);assert.equal(await service.verify!(sealed,{acknowledged:true,skipped:true}),false) +}) + +test("scope revocation/catalog/settings/endpoint drift refuses before shared executor; recovery cannot dispatch",async()=>{ + const {sealed}=await prepare() + await assert.rejects(service.execute(sealed,{phase:"recovery",operationId:forward.operationId}));assert.equal(collectCalls,0) + connection.policy.scope.serverIds=[1,2];await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,0) + connection.policy.scope.serverIds=[1,2,3];catalog[0].routingEndpointEpoch=randomUUID();await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,0) + catalog[0].routingEndpointEpoch=epochs[0];settings!.intervalSec=121;await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,0) +}) + +test("collector or receipt failure is never automatically replayed, including after alert hook already ran",async()=>{ + const {sealed}=await prepare();collectorFailure=Error("collector uncertain") + await assert.rejects(service.execute(sealed,forward),error=>error===collectorFailure);assert.equal(collectCalls,1);assert.equal(alertCalls,0) + collectorFailure=undefined;readFailure=true + // Shared pre-read fails before this attempt reaches the collector. + await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,1);assert.equal(alertCalls,0) + readFailure=false;failReceiptAfterCollector=true + await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,2);assert.equal(alertCalls,1) + await service.reconcile!(sealed,async()=>{});assert.equal(collectCalls,2);assert.equal(alertCalls,1) +}) diff --git a/backend/src/modules/mcp/manual-api-ping-actions.ts b/backend/src/modules/mcp/manual-api-ping-actions.ts new file mode 100644 index 00000000..47e91972 --- /dev/null +++ b/backend/src/modules/mcp/manual-api-ping-actions.ts @@ -0,0 +1,77 @@ +/** Full manual API Ping, including the existing deferred GLOBAL alert hook. + * Common MCP plans/runtime own approval, audit, locks, intent and no replay. */ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { manualDataCollection } from "../../services/manual-data-collection.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const permission="mm:servers:write" // Same permission as REST /api/servers-api-ping/collect-now. +const principalSchema=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict() +const identitySchema=z.object({principal:principalSchema,ownerId:z.string().min(1).max(200)}).strict() +const hash=z.string().regex(/^[a-f0-9]{64}$/) +const serviceSchema=identitySchema.extend({policyProof:hash,beforeFingerprint:hash}).strict() +const serverSchema=z.object({id:z.number().int().positive().safe(),enabled:z.boolean(),routingEndpointEpoch:z.string().uuid()}).strict() +const settingsSchema=z.object({enabled:z.boolean(),intervalSec:z.number().int().positive().max(2147483647)}).strict() +type Identity=z.infer&{policyProof?:string} +async function source(input:Identity){ + const connection=await effectiveMcpConnection(input.principal) + if(connection.owner_id!==input.ownerId||!connection.policy.scope.allowGlobal||!hasPermission(connection.policy.permissions,permission)||!hasPermission(connection.owner_permissions,permission))throw new McpError("PERMISSION_DENIED","Полный API Ping требует глобального доступа к сбору и alert engine",403) + const policyProof=fingerprint(connection.policy) + if(input.policyProof&&input.policyProof!==policyProof)throw new McpError("STATE_CONFLICT","Допуск изменился; подготовьте новый план",409) + const catalog=await pool.query('SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM servers ORDER BY id LIMIT 1001') + const parsed=z.array(serverSchema).max(1000).safeParse(catalog.rows) + if(!parsed.success||parsed.data.some((row,index)=>index>0&&row.id<=parsed.data[index-1].id))throw new McpError("STATE_UNAVAILABLE","Каталог API Ping отсутствует или превышает предел 1000 устройств",409) + // Disabled devices still belong to the global alert/outbox footprint. + if(parsed.data.some(row=>!connection.policy.scope.serverIds.includes(row.id)))throw new McpError("SERVER_DENIED","Полный API Ping требует весь каталог, включая отключённые устройства",403) + const settings=await pool.query('SELECT enabled,interval_sec AS "intervalSec" FROM servers_api_ping_settings WHERE id=1') + const publicSettings=settings.rows.length===1?settingsSchema.safeParse(settings.rows[0]):undefined + if(settings.rows.length>1||settings.rows.length===1&&!publicSettings?.success)throw new McpError("STATE_UNAVAILABLE","Настройки API Ping не подтверждены",409) + // A missing singleton is a stable null precondition, not a planning write. + // The already-shared manual executor initializes its standard defaults only + // after this human-approved operation enters the audited execution path. + return {before:{servers:parsed.data,settings:publicSettings?.success?publicSettings.data:null},serverIds:parsed.data.map(row=>row.id),policyProof} +} +function receiptSummary(value:unknown){ + const receipt=z.object({ok:z.literal(true),skipped:z.boolean(),lastCollectedAt:z.unknown().optional(),lastDurationMs:z.unknown().optional(),lastError:z.unknown().optional()}).parse(value) + if(receipt.skipped)throw new McpError("COLLECTOR_BUSY","API Ping пропущен занятой очередью; global alert hook уже мог быть вызван. Автоматический повтор запрещён",409) + const lastCollectedAt=typeof receipt.lastCollectedAt==="string"&&receipt.lastCollectedAt.length<=64&&Number.isFinite(Date.parse(receipt.lastCollectedAt))?receipt.lastCollectedAt:null + const lastDurationMs=typeof receipt.lastDurationMs==="number"&&Number.isFinite(receipt.lastDurationMs)&&receipt.lastDurationMs>=0?receipt.lastDurationMs:null + return {acknowledged:true,skipped:false,lastCollectedAt,lastDurationMs,lastError:!!receipt.lastError,globalAlertHookRequested:true,alertDeliveryConfirmed:false} +} +export function registerManualApiPingActions(register:typeof add){ + registerMcpService("servers.api-ping.collect",{ + current:async raw=>(await source(serviceSchema.parse(raw))).before, + execute:async(raw,context)=>{ + if(context?.phase!=="forward"||!z.string().uuid().safeParse(context.operationId).success)throw new McpError("INVALID_EXECUTION_CONTEXT","Ручной сбор требует подтверждённого MCP плана",409) + const input=serviceSchema.parse(raw) + await assertVerifiedConfigurationAccess() + const current=await source(input) + if(fingerprint(current.before)!==input.beforeFingerprint)throw new McpError("STATE_CONFLICT","Каталог или настройки API Ping изменились после подтверждения",409) + await assertVerifiedConfigurationAccess() + // The shared executor owns actual collection, transition samples, + // partition maintenance, settings receipt AND the global alert hook. + return receiptSummary(await manualDataCollection.collectServersRestPing()) + }, + verify:async(_input,result)=>!!result&&typeof result==="object"&&"acknowledged"in result&&result.acknowledged===true&&"skipped"in result&&result.skipped===false, + reconcile:async(_input,authorize)=>{await authorize();return {matchesDesiredState:false,matchesOriginalState:false}}, + }) + register("servers.api-ping.collect","mm_api_ping_collect_plan","Ручной API Ping и обработка оповещений","uptime",permission,"write","global",z.object({}).strict(),{ + version:1, + prepare:async(_raw,context)=>{ + const identity=identitySchema.parse({principal:context.principal,ownerId:context.connection.owner_id}),current=await source(identity) + return {target:{scope:"global"},objects:[...current.serverIds.map(serverId=>({kind:"server",id:String(serverId),serverId})), + {kind:"servers-api-ping-settings",id:"1"},{kind:"servers-rest-ping-samples",id:"global"},{kind:"partition-maintenance",id:"global"},{kind:"alerts-configuration",id:"1"},{kind:"alert-engine",id:"global"},{kind:"alerts-outbox",id:"global"}], + snapshots:[],before:current.before,requiresHuman:true,mutationCost:1,verificationScope:"acknowledgement", + steps:[{kind:"service",service:"servers.api-ping.collect",input:{...identity,policyProof:current.policyProof,beforeFingerprint:fingerprint(current.before)}}], + warnings:["ГЛОБАЛЬНЫЙ сбор: API Ping всех включённых устройств, обычные и переходные samples, обслуживание partitions и обновление настроек сбора.", + "Shared executor вызывает отложенный GLOBAL ALERT ENGINE/outbox для всего приложения, включая историю отключённых устройств; возможна последующая доставка уведомлений/Telegram. Это не scoped read-only проверка.", + "Если настройки ещё отсутствуют, при выполнении shared executor создаёт стандартную строку: автосбор выключен, интервал 120 секунд. Подготовка плана ничего не создаёт.", + "Занятый collector может пропустить сбор, но global alert hook всё равно вызывается. Receipt не доказывает свежий sample или доставку уведомлений; неопределённый результат не повторяется автоматически."]} + }, + }) +} diff --git a/backend/src/modules/mcp/manual-api-ping-http.test.ts b/backend/src/modules/mcp/manual-api-ping-http.test.ts new file mode 100644 index 00000000..2d5aab63 --- /dev/null +++ b/backend/src/modules/mcp/manual-api-ping-http.test.ts @@ -0,0 +1,131 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +/** Disposable ordinary MCP/REST manual collector acceptance. Real application + * SQL, localhost HTTP and deferred global alert engine; no notifier recipients. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" + +async function main() { + assert.equal(process.env.MMAPP_OWN_MANUAL_API_PING, "true") + const target = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(target.protocol)); assert.equal(target.hostname, "127.0.0.1") + assert.equal(target.username, "mcp_test"); assert(target.password); assert.equal(target.pathname, "/postgres") + assert(Number.isSafeInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) + assert.equal(target.search, ""); assert.equal(target.hash, "") + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)); assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.AUTH_REQUIRED = "true"; process.env.AUTH_JWT_SECRET = "owned-manual-api-ping-acceptance-secret"; process.env.AUTH_ISSUER = "https://owned-api-ping.test" + const { pool, closePool } = await import("../../db/index.js"), { applySqlMigrations } = await import("../../db/migrate.js") + const { ensurePartitionsAround } = await import("../../db/partitions.js"), { buildApp } = await import("../../index.js") + const { createMcpConnection, authenticateMcpToken, updateMcpSettings } = await import("./store.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const hooks = await import("../../services/alert-collector-hooks.js"), { executeSchedulerJob } = await import("../../services/scheduler.js") + const { manualDataCollection } = await import("../../services/manual-data-collection.js") + const owner = { id: "owned-manual-api-ping-owner", email: "owned-api-ping@example.test", name: "Owned API Ping", apps: ["mm"], permissions: ["mm:settings:admin", "mm:servers:write"], expiresAt: Date.now() + 3600000 } + let gets = 0, disabledGets = 0, fixtureFailed = false, hold = false, releaseHold: (() => void) | undefined, barrier: Promise | undefined + let engineRuns = 0, engineFailed = false + const engines = new Set>(), groups: string[] = [] + const responders = [1, 2].map(index => createServer((request, response) => { + void (async () => { + if (request.headers.authorization === "Basic " + Buffer.from("owned-disabled:owned-disabled-password").toString("base64")) disabledGets++ + else { assert.equal(request.headers.authorization, "Basic " + Buffer.from(`owned-${index}:owned-local-password`).toString("base64")); gets++ } + assert.equal(request.method, "GET"); assert.equal(request.url?.split("?")[0], "/rest/system/identity") + if (hold) await barrier + response.setHeader("content-type", "application/json"); response.end(JSON.stringify({ name: "owned-router-" + index })) + })().catch(() => { fixtureFailed = true; response.destroy() }) + })) + let app: Awaited> | undefined, busyRun: Promise | undefined + const samples = async () => Number((await pool.query("SELECT count(*)::int AS n FROM servers_rest_ping_samples")).rows[0].n) + const settings = async () => (await pool.query("SELECT enabled,last_collected_at,last_duration_ms,last_error FROM servers_api_ping_settings WHERE id=1")).rows[0] + const until = async (check: () => Promise | boolean) => { const end = performance.now() + 20000; while (!await check()) { assert(performance.now() < end, "Owned manual collector did not finish; never replay"); await delay(25) } } + const terminal = async (id: string) => { let state = ""; await until(async () => { state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; return !["pending", "running"].includes(state) }); return state } + try { + await applySqlMigrations(pool); assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + await ensurePartitionsAround(pool) + // Deliberately exercise an absent singleton in this verified disposable DB; + // normal migrations may already seed defaults. Planning must stay read-only. + await pool.query("DELETE FROM servers_api_ping_settings WHERE id=1") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers_api_ping_settings")).rows[0].n),0,"Missing settings must be initialized by the shared executor, not planning") + await pool.query("INSERT INTO alert_telegram_settings(id,bot_token,chat_id) VALUES(1,'','') ON CONFLICT(id) DO UPDATE SET bot_token='',chat_id='' ") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM alert_outbox")).rows[0].n), 0) + for (const responder of responders) await new Promise(resolve => responder.listen(0, "127.0.0.1", resolve)) + const ids: number[] = [] + for (let index = 0; index < 3; index++) ids.push(Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) VALUES($1,'127.0.0.1',$2,false,false,$3,$4,$5) RETURNING id", + ["owned-api-ping-" + index, (responders[index % 2].address() as { port: number }).port, index === 2 ? "owned-disabled" : "owned-" + (index + 1), index === 2 ? "owned-disabled-password" : "owned-local-password", index < 2])).rows[0].id)) + await pool.query("INSERT INTO alert_rules(id,name,type,target,condition,severity,enabled,rule_chat_id) VALUES($1,'owned-api-ping-rule','server','owned-api-ping-0','offline','warning',true,'')", [randomUUID()]) + await updateMcpSettings({ enabled: true }, owner); app = await buildApp({ startScheduler: false }); await app.ready() + hooks.wireAlertEngineRunner(async () => { + const pending = (async () => { + await executeSchedulerJob("alert_engine") + const row = (await pool.query("SELECT status,result_json FROM scheduler_runs WHERE job_key='alert_engine' ORDER BY finished_at DESC LIMIT 1")).rows[0] + assert(row); assert.equal(row.status, "ok"); assert.equal(row.result_json.skippedNoTelegram, true); engineRuns++ + })(); engines.add(pending) + try { await pending } + catch { engineFailed = true; throw Error("Owned actual alert engine failed") } + finally { engines.delete(pending) } + }) + const issue = async (serverIds: number[], allowGlobal: boolean) => authenticateMcpToken((await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned manual API Ping", mode: "approval", + expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:servers:write"], scope: { serverIds, operationIds: ["servers.api-ping.collect"], allowGlobal }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner)).token) + const principal = await issue(ids, true) + const prepare = () => invokeMcpTool(principal, "mm_api_ping_collect_plan", {}) as Promise<{ planId: string; state: string }> + const approve = async () => { const plan = await prepare(); await approvePlan(plan.planId, owner, true); return plan } + console.log("Owned manual API Ping stage:", "global-catalog-approval") + await assert.rejects(invokeMcpTool(await issue(ids, false), "mm_api_ping_collect_plan", {})) + await assert.rejects(invokeMcpTool(await issue(ids.slice(0, 2), true), "mm_api_ping_collect_plan", {})) + const unapproved = await prepare(); assert.equal(unapproved.state, "awaiting-approval"); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())) + assert.equal(gets, 0); assert.equal(await samples(), 0) + groups.push("GLOBAL, all catalog IDs including disabled server and human approval are enforced before HTTP") + + console.log("Owned manual API Ping stage:", "full-mcp-collector") + const plan = await approve(), key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await terminal(operation.id), "applied"); assert.equal(gets, 2); assert.equal(disabledGets, 0); assert.equal(await samples(), 2) + const row = await settings(); assert.equal(row.enabled,false);assert(row.last_collected_at); assert(Number.isFinite(row.last_duration_ms)); assert.equal(row.last_error, "") + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].result + assert.equal(receipt.acknowledged, true); assert.equal(receipt.skipped, false); assert(receipt.lastCollectedAt); assert.equal(receipt.lastError, false) + assert.equal(receipt.globalAlertHookRequested, true); assert.equal(receipt.alertDeliveryConfirmed, false) + assert(Buffer.byteLength(JSON.stringify(receipt)) < 16384) + await until(() => engineRuns >= 1); assert.equal(engineFailed, false) + groups.push("ordinary MCP collector GETs enabled servers only, persists samples/settings and invokes actual deferred global alert engine without recipients") + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, operation.id) + assert.equal(gets, 2); assert.equal(await samples(), 2) + groups.push("reapplying the same plan returns its journal operation without fresh collection") + + console.log("Owned manual API Ping stage:", "rest-parity") + const token = await new SignJWT({ sub: owner.id, apps: owner.apps, permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime("1h").setIssuer(process.env.AUTH_ISSUER!).sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET)) + const response = await app.inject({ method: "POST", url: "/api/servers-api-ping/collect-now", headers: { authorization: "Bearer " + token }, payload: {} }) + assert.equal(response.statusCode, 200); const rest = response.json(); assert.equal(rest.ok, true); assert.equal(rest.skipped, false) + assert(rest.lastCollectedAt); assert.equal(typeof rest.lastDurationMs, "number"); assert.equal(rest.lastError, null) + assert.equal(gets, 4); assert.equal(disabledGets, 0); assert.equal(await samples(), 4) + await until(() => engineRuns >= 2) + groups.push("signed-JWT REST uses the same complete collector and receipt fields") + + console.log("Owned manual API Ping stage:", "busy-is-not-fresh") + hold = true; barrier = new Promise(resolve => { releaseHold = resolve }) + busyRun = manualDataCollection.collectServersRestPing(); await until(() => gets === 6) + const busyPlan = await approve(), busyOperation = await applyPlan(principal, busyPlan.planId, randomUUID()) + assert.equal(await terminal(busyOperation.id), "unknown"); assert.equal(gets, 6); assert.equal(await samples(), 4) + hold = false; assert(releaseHold); releaseHold(); await busyRun; busyRun = undefined + await until(() => engineRuns >= 3); assert.equal(await samples(), 6); assert.equal(disabledGets, 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM alert_outbox")).rows[0].n), 0) + assert.equal(fixtureFailed, false); assert.equal(engineFailed, false) + groups.push("shared busy collector is not reported as fresh MCP success and is not retried; existing active collector completes normally") + console.log("Owned manual API Ping accepted:", JSON.stringify({ groups: groups.length, checks: groups, identityGets: gets, disabledGets, sampleRows: await samples(), actualGlobalAlertRuns: engineRuns, notifierDelivery: false })) + } finally { + hold = false; releaseHold?.(); await busyRun?.catch(() => {}) + hooks.clearAlertCollectorHooksTimer() + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await Promise.allSettled([...engines]); hooks.clearAlertCollectorHooksTimer() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close() + for (const responder of responders) { responder.closeAllConnections(); await new Promise(resolve => responder.close(() => resolve())) } + await closePool() + } +} +void main().catch(() => { console.error("Owned manual API Ping acceptance failed; no real RouterOS/notifier action"); process.exitCode = 1 }) diff --git a/backend/src/modules/mcp/manual-ping-group-actions.test.ts b/backend/src/modules/mcp/manual-ping-group-actions.test.ts new file mode 100644 index 00000000..e7084730 --- /dev/null +++ b/backend/src/modules/mcp/manual-ping-group-actions.test.ts @@ -0,0 +1,55 @@ +/** Synthetic SQL/current state; actual shared manual operation and alert hook. + * No native, SQL or notifier acceptance is claimed by these adapter units. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { beforeEach, mock, test } from "node:test" +import type { add } from "./actions.js" +import { createManualDataCollection } from "../../services/manual-data-collection-execution.js" +const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 60000).toISOString() } +const context = { principal, connection: { owner_id: "ping-group-owner" } } as Parameters[8]["prepare"]>>[1] +const forward = { phase: "forward" as const, operationId: randomUUID(), stepIndex: 0 }, epoch = randomUUID() +let permission = true, global = true, all = true, drift = false, polled = 1, calls = 0, alerts = 0, failure: Error | undefined +const queries: string[] = [], received: string[][] = [] +const shared = createManualDataCollection({ async collectServersRestPing() { assert.fail() }, async collectUptime() { assert.fail() }, async readServersRestPingSettings() { return {} }, async readUptimeSettings() { return {} }, + async collectPingGroup(ids) { calls++; received.push(ids); if (failure) throw failure; return { polled } }, scheduleGlobalAlerts() { alerts++ } }) +mock.module("../../db/index.js", { namedExports: { pool: { async query(sql: string, args?: unknown[]) { + queries.push(sql) + if (sql.includes("FROM servers ORDER")) return { rows: [{ id: 1, enabled: true, routingEndpointEpoch: epoch }, { id: 2, enabled: false, routingEndpointEpoch: epoch }] } + if (sql.includes("FROM uptime_probes")) return { rows: (args?.[0] as string[]).includes("p") ? [{ id: "p", srcServerId: 1, srcInterface: "", target: drift ? "198.18.0.2" : "198.18.0.1", enabled: false, intervalSec: 0 }] : [] } + assert(sql.includes("FROM uptime_settings")); return { rows: [] } +} } } }) +mock.module("./store.js", { namedExports: { async effectiveMcpConnection() { return { owner_id: context.connection.owner_id, owner_permissions: permission ? ["mm:uptime:write"] : [], policy: { permissions: ["mm:uptime:write"], scope: { allowGlobal: global, serverIds: all ? [1, 2] : [1] } } } } } }) +mock.module("../../services/config-mutation-policy.js", { namedExports: { async assertVerifiedConfigurationAccess() {} } }) +mock.module("../../services/manual-data-collection.js", { namedExports: { manualDataCollection: shared } }) +const { registerManualPingGroupActions, manualPingGroupSchema } = await import("./manual-ping-group-actions.js"), { findMcpService } = await import("./service-registry.js") +let registration: Parameters | undefined +registerManualPingGroupActions((...args: Parameters) => { registration = args }); assert(registration) +const [op, tool, _title, _section, required, effect, scope, schema, handlers] = registration, service = findMcpService("uptime.ping.group.collect")! +async function prepare(probeIds: unknown[] = ["p"]) { const evidence = await handlers.prepare!(schema.parse({ probeIds }), context), step = evidence.steps[0]; assert(step.kind === "service"); return { evidence, sealed: step.input } } +beforeEach(() => { permission = global = all = true; drift = false; polled = 1; calls = alerts = 0; failure = undefined; queries.length = received.length = 0 }) +test("exact legacy raw-ID normalization, dedup and first64 bounded input", () => { + assert.deepEqual(manualPingGroupSchema.parse({ probeIds: ["p", " p ", "p", null, 7, "", " ", "unknown"] }), { probeIds: ["p", " p ", "unknown"] }) + assert.equal(manualPingGroupSchema.parse({ probeIds: Array.from({ length: 65 }, (_, i) => "p" + i) }).probeIds.length, 64) + assert.equal(schema.safeParse({ probeIds: Array(1025).fill("p") }).success, false); assert.equal(schema.safeParse({ probeIds: ["x".repeat(201)] }).success, false) + assert.equal(schema.safeParse({ source: {} }).success, false) +}) +test("global all-catalog human plan uses REST permission and no credential/timestamp planning reads", async () => { + assert.equal(op, "uptime.ping.group.collect"); assert.equal(tool, "mm_ping_group_collect_plan"); assert.equal(required, "mm:uptime:write"); assert.equal(effect, "write"); assert.equal(scope, "global") + const { evidence, sealed } = await prepare(["p", "p", "unknown"]) + assert.equal(evidence.requiresHuman, true); assert.deepEqual(sealed.probeIds, ["p", "unknown"]); assert.deepEqual(evidence.target, { scope: "global" }) + assert.deepEqual(evidence.objects.filter(row => row.kind === "server").map(row => row.serverId), [1, 2]); assert.equal(calls, 0) + assert(queries.every(sql => sql.startsWith("SELECT") && !/password|username|last_collected_at/.test(sql))) +}) +test("global/owner permission/missing disabled catalog deny before collection", async () => { + for (const mode of ["global", "permission", "all"]) { global = permission = all = true; if (mode === "global") global = false; if (mode === "permission") permission = false; if (mode === "all") all = false; await assert.rejects(prepare()); assert.equal(calls, 0) } +}) +test("selected config drift denies, disabled probe remains selected, exact shared receipt and alert parity", async () => { + const { sealed } = await prepare(); drift = true; await assert.rejects(service.execute(sealed, forward)); assert.equal(calls, 0) + drift = false; const result = await service.execute(sealed, forward); assert.equal(await service.verify!(sealed, result), true); assert.deepEqual(received, [["p"]]); assert.equal(alerts, 1) + assert.deepEqual(result, { acknowledged: true, polled: 1, globalAlertHookRequested: true, alertDeliveryConfirmed: false }) +}) +test("empty/unknown group polled0 is honest success without alerts; busy and uncertain never retry", async () => { + polled = 0; const { sealed } = await prepare([]); assert.deepEqual(await service.execute(sealed, forward), { acknowledged: true, polled: 0, globalAlertHookRequested: false, alertDeliveryConfirmed: false }); assert.equal(alerts, 0) + for (const error of [Object.assign(Error("shared busy"), { statusCode: 409 }), Error("commit acknowledgement uncertain")]) { const plan = await prepare(); failure = error; const before = calls; await assert.rejects(service.execute(plan.sealed, forward), value => value === error); assert.equal(calls, before + 1); assert.equal(alerts, 0) } + assert.deepEqual(await service.reconcile!({}, async () => {}), { matchesDesiredState: false, matchesOriginalState: false }) +}) diff --git a/backend/src/modules/mcp/manual-ping-group-actions.ts b/backend/src/modules/mcp/manual-ping-group-actions.ts new file mode 100644 index 00000000..9f4fe112 --- /dev/null +++ b/backend/src/modules/mcp/manual-ping-group-actions.ts @@ -0,0 +1,72 @@ +/** Ordinary full manual Ping group, including deferred GLOBAL alert effects. */ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { manualDataCollection } from "../../services/manual-data-collection.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const permission = "mm:uptime:write" +// Exact shared REST/collector semantics: blank-filter, RAW string dedup and +// first 64 IDs. Whitespace in a nonblank ID is preserved, not an ID alias. +export const manualPingGroupSchema = z.object({ probeIds: z.array(z.unknown()).max(1024).refine(values => values.every(value => typeof value !== "string" || value.length <= 200), "ID пробы превышает предел").default([]).transform(values => { + const strings = values.filter((value): value is string => typeof value === "string" && value.trim() !== "") + return [...new Set(strings)].slice(0, 64) +}) }).strict() +const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict() +const identity = z.object({ principal, ownerId: z.string().min(1).max(200) }).strict() +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const sealed = identity.extend({ probeIds: z.array(z.string().max(200)).max(64), policyProof: hash, beforeFingerprint: hash }).strict() +const server = z.object({ id: z.number().int().positive().safe(), enabled: z.boolean(), routingEndpointEpoch: z.string().uuid() }).strict() +const probe = z.object({ id: z.string(), srcServerId: z.number().int().positive().safe(), srcInterface: z.string(), target: z.string(), enabled: z.boolean(), intervalSec: z.number().int() }).strict() +const settings = z.object({ enabled: z.boolean(), resourcesEnabled: z.boolean(), pingEnabled: z.boolean(), speedEnabled: z.boolean(), intervalSec: z.number().int(), probeIntervalSec: z.number().int(), speedIntervalSec: z.number().int(), retentionDays: z.number().int() }).strict() +async function source(input: z.infer & { probeIds: string[]; policyProof?: string }) { + const connection = await effectiveMcpConnection(input.principal) + if (connection.owner_id !== input.ownerId || !connection.policy.scope.allowGlobal || !hasPermission(connection.policy.permissions, permission) || !hasPermission(connection.owner_permissions, permission)) throw new McpError("PERMISSION_DENIED", "Ручной Ping group требует глобального доступа", 403) + const policyProof = fingerprint(connection.policy) + if (input.policyProof && input.policyProof !== policyProof) throw new McpError("STATE_CONFLICT", "Допуск изменился", 409) + const catalog = z.array(server).max(1000).safeParse((await pool.query('SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM servers ORDER BY id LIMIT 1001')).rows) + if (!catalog.success || catalog.data.some((row, i) => i > 0 && row.id <= catalog.data[i - 1].id)) throw new McpError("STATE_UNAVAILABLE", "Каталог устройств не подтверждён", 409) + if (catalog.data.some(row => !connection.policy.scope.serverIds.includes(row.id))) throw new McpError("SERVER_DENIED", "Нужен весь каталог, включая отключённые устройства", 403) + const rows = (await pool.query('SELECT id,src_server_id::int AS "srcServerId",src_interface AS "srcInterface",target,enabled,interval_sec AS "intervalSec" FROM uptime_probes WHERE id=ANY($1::text[]) ORDER BY id LIMIT 65', [input.probeIds])).rows + const selected = z.array(probe).max(64).safeParse(rows) + if (!selected.success || selected.data.some(row => !input.probeIds.includes(row.id)) || new Set(selected.data.map(row => row.id)).size !== selected.data.length) throw new McpError("STATE_UNAVAILABLE", "Пробы Ping не подтверждены", 409) + const config = (await pool.query('SELECT enabled,resources_enabled AS "resourcesEnabled",ping_enabled AS "pingEnabled",speed_enabled AS "speedEnabled",interval_sec AS "intervalSec",probe_interval_sec AS "probeIntervalSec",speed_interval_sec AS "speedIntervalSec",retention_days AS "retentionDays" FROM uptime_settings WHERE id=1')).rows + const parsed = config.length === 1 ? settings.safeParse(config[0]) : undefined + if (config.length > 1 || config.length === 1 && !parsed?.success) throw new McpError("STATE_UNAVAILABLE", "Настройки uptime не подтверждены", 409) + return { before: { servers: catalog.data, probes: selected.data, settings: parsed?.success ? parsed.data : null }, serverIds: catalog.data.map(row => row.id), policyProof } +} +export function registerManualPingGroupActions(register: typeof add) { + registerMcpService("uptime.ping.group.collect", { + current: async raw => (await source(sealed.parse(raw))).before, + execute: async (raw, context) => { + if (context?.phase !== "forward" || !z.string().uuid().safeParse(context.operationId).success) throw new McpError("INVALID_EXECUTION_CONTEXT", "Нужен подтверждённый MCP план", 409) + const input = sealed.parse(raw) + await assertVerifiedConfigurationAccess() + if (fingerprint((await source(input)).before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "Пробы, каталог или настройки изменились", 409) + await assertVerifiedConfigurationAccess() + // Shared operation owns mutex409, enabled-source filtering, count4, + // admitted partition sample writes and alert hook only when polled>0. + const result = z.object({ ok: z.literal(true), polled: z.number().int().min(0).max(64) }).parse(await manualDataCollection.collectPingGroup(input.probeIds)) + return { acknowledged: true, polled: result.polled, globalAlertHookRequested: result.polled > 0, alertDeliveryConfirmed: false } + }, + verify: async (_input, value) => !!value && typeof value === "object" && "acknowledged" in value && value.acknowledged === true && "polled" in value && typeof value.polled === "number" && Number.isInteger(value.polled) && value.polled >= 0 && value.polled <= 64, + reconcile: async (_input, authorize) => { await authorize(); return { matchesDesiredState: false, matchesOriginalState: false } }, + }) + register("uptime.ping.group.collect", "mm_ping_group_collect_plan", "Ручной Ping группы и обработка оповещений", "uptime", permission, "write", "global", manualPingGroupSchema, { + version: 1, + prepare: async (raw, context) => { + const input = manualPingGroupSchema.parse(raw), actor = identity.parse({ principal: context.principal, ownerId: context.connection.owner_id }), current = await source({ ...actor, ...input }) + return { target: { scope: "global" }, objects: [...current.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), + ...current.before.probes.map(row => ({ kind: "uptime-probe", id: row.id, serverId: row.srcServerId })), { kind: "uptime-settings", id: "1" }, { kind: "uptime-probe-samples", id: "global" }, { kind: "partition-maintenance", id: "global" }, { kind: "alerts-configuration", id: "1" }, { kind: "alert-engine", id: "global" }, { kind: "alerts-outbox", id: "global" }], + snapshots: [], before: current.before, requiresHuman: true, mutationCost: 1, verificationScope: "acknowledgement", + steps: [{ kind: "service", service: "uptime.ping.group.collect", input: { ...actor, ...input, policyProof: current.policyProof, beforeFingerprint: fingerprint(current.before) } }], + warnings: ["GLOBAL: ручной Ping выбранных проб, count=4, samples/partitions и отложенный GLOBAL ALERT ENGINE/outbox при polled>0; возможна последующая доставка Telegram. Доставка не подтверждается receipt.", + "Пустые/non-string ID удаляются, точные дубликаты удаляются, сохраняются первые 64 ID. Пробелы внутри непустого ID не обрезаются. Неизвестные ID и источники disabled пропускаются; enabled самой пробы не ограничивает ручной сбор.", + "Пустая группа возвращает polled=0 без alert hook; shared busy409 и неопределённый результат не повторяются автоматически. Планирование не создаёт настройки и не читает credentials."] } + }, + }) +} diff --git a/backend/src/modules/mcp/manual-ping-group-http.test.ts b/backend/src/modules/mcp/manual-ping-group-http.test.ts new file mode 100644 index 00000000..9195762c --- /dev/null +++ b/backend/src/modules/mcp/manual-ping-group-http.test.ts @@ -0,0 +1,88 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +/** Owned disposable application SQL + authenticated localhost RouterOS fixture. + * No actual RouterOS, external recipients, alternative kernel or authority pool. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" + +async function main() { + assert.equal(process.env.MMAPP_OWN_MANUAL_PING_GROUP, "true") + const target = new URL(process.env.DATABASE_URL ?? "") + assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test"); assert.equal(target.pathname, "/postgres"); assert(target.password) + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)); assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state"); assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.AUTH_REQUIRED = "true"; process.env.AUTH_JWT_SECRET = "owned-manual-ping-group-secret"; process.env.AUTH_ISSUER = "https://owned-ping-group.test" + const { pool, closePool } = await import("../../db/index.js"), { applySqlMigrations } = await import("../../db/migrate.js") + const { ensurePartitionsAround } = await import("../../db/partitions.js"), { buildApp } = await import("../../index.js") + const { createMcpConnection, authenticateMcpToken, updateMcpSettings } = await import("./store.js"), { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const hooks = await import("../../services/alert-collector-hooks.js"), scheduler = await import("../../services/scheduler.js"), { manualDataCollection } = await import("../../services/manual-data-collection.js") + const owner = { id: "owned-manual-ping-group-owner", email: "owned-ping@example.test", name: "Owned Ping group", apps: ["mm"], permissions: ["mm:settings:admin", "mm:uptime:write"], expiresAt: Date.now() + 3600000 } + let posts = 0, fixtureFailed = false, hold = false, releaseHold: (() => void) | undefined, barrier: Promise | undefined, engines = 0, engineFailed = false + const runningEngines = new Set>(), checks: string[] = [] + const server = createServer((req, res) => { void (async () => { + assert.equal(req.method, "POST"); assert.equal(req.url, "/rest/tool/ping"); assert.equal(req.headers.authorization, "Basic " + Buffer.from("owned-ping:owned-local-password").toString("base64")) + let text = ""; for await (const chunk of req) { text += chunk; assert(text.length < 4096) } + const body = JSON.parse(text); assert.equal(body.count, "4"); assert.equal(body.interval, "0.2s"); assert(["198.18.0.1", "198.18.0.2"].includes(body.address)); posts++ + if (hold) await barrier + res.setHeader("content-type", "application/json"); res.end(JSON.stringify([{ sent: "4", received: "4", "packet-loss": "0", "avg-rtt": "1ms", "min-rtt": "1ms", "max-rtt": "1ms" }])) + })().catch(() => { fixtureFailed = true; res.destroy() }) }) + let app: Awaited> | undefined, busy: Promise | undefined + const until = async (check: () => boolean | Promise) => { const end = performance.now() + 20000; while (!await check()) { assert(performance.now() < end, "Owned collection did not finish; never replay"); await delay(25) } } + const terminal = async (id: string) => { let state = ""; await until(async () => { state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; return !["pending", "running"].includes(state) }); return state } + const samples = async () => Number((await pool.query("SELECT count(*) AS n FROM uptime_probe_samples")).rows[0].n) + try { + await applySqlMigrations(pool); assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0); assert.equal(Number((await pool.query("SELECT count(*) AS n FROM uptime_probes")).rows[0].n), 0) + await ensurePartitionsAround(pool) + await pool.query("INSERT INTO alert_telegram_settings(id,bot_token,chat_id) VALUES(1,'','') ON CONFLICT(id) DO UPDATE SET bot_token='',chat_id=''") + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); const address = server.address(); assert(address && typeof address !== "string") + const ids: number[] = [] + for (const enabled of [true, false]) ids.push(Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) VALUES('owned-ping-group','127.0.0.1',$1,false,false,'owned-ping','owned-local-password',$2) RETURNING id", [address.port, enabled])).rows[0].id)) + const p = randomUUID(), disabledProbe = randomUUID(), disabledSource = randomUUID() + for (const [id, src, enabled, destination] of [[p, ids[0], true, "198.18.0.1"], [disabledProbe, ids[0], false, "198.18.0.2"], [disabledSource, ids[1], true, "198.18.0.1"]] as const) await pool.query("INSERT INTO uptime_probes(id,src_server_id,name,target,enabled) VALUES($1,$2,'owned-ping',$3,$4)", [id, src, destination, enabled]) + await updateMcpSettings({ enabled: true }, owner); app = await buildApp({ startScheduler: false }); await app.ready() + hooks.wireAlertEngineRunner(async () => { const pending = (async () => { + await scheduler.executeSchedulerJob("alert_engine") + const row = (await pool.query("SELECT status,result_json FROM scheduler_runs WHERE job_key='alert_engine' ORDER BY finished_at DESC LIMIT 1")).rows[0] + assert(row); assert.equal(row.status, "ok"); assert.equal(row.result_json.skippedNoTelegram, true); engines++ + })(); runningEngines.add(pending); try { await pending } catch { engineFailed = true; throw Error("Owned alert engine failed") } finally { runningEngines.delete(pending) } }) + const issue = async (serverIds: number[], allowGlobal: boolean) => authenticateMcpToken((await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned manual Ping group", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:uptime:write"], scope: { serverIds, allowGlobal, operationIds: ["uptime.ping.group.collect"] }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner)).token) + const principal = await issue(ids, true), input = { probeIds: [p, p, " " + p + " ", null, "", " ", disabledProbe, disabledSource, "unknown"] } + const prepare = async (value: { probeIds: unknown[] } = input) => invokeMcpTool(principal, "mm_ping_group_collect_plan", value) as Promise<{ planId: string; state: string }> + const approve = async (value: { probeIds: unknown[] } = input) => { const plan = await prepare(value); await approvePlan(plan.planId, owner, true); return plan } + await assert.rejects(invokeMcpTool(await issue(ids, false), "mm_ping_group_collect_plan", input)); await assert.rejects(invokeMcpTool(await issue([ids[0]], true), "mm_ping_group_collect_plan", input)) + const unapproved = await prepare(); assert.equal(unapproved.state, "awaiting-approval"); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(posts, 0) + checks.push("GLOBAL + ALL catalog including disabled source + human approval deny before native") + const plan = await approve(), key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await terminal(operation.id), "applied"); assert.equal(posts, 2); assert.equal(await samples(), 2) + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].result + assert.deepEqual(receipt, { acknowledged: true, polled: 2, globalAlertHookRequested: true, alertDeliveryConfirmed: false }) + await until(() => engines >= 1); assert.equal(engineFailed, false) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id); assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, operation.id); assert.equal(posts, 2); assert.equal(await samples(), 2) + checks.push("raw-ID normalization/dedup, disabled probe polled, disabled source and unknown ignored, count4 persisted once; actual alert engine; same plan never recollects") + // Empty bypasses the shared busy mutex; neither empty nor missing IDs queues alerts. + for (const probeIds of [[], ["unknown", disabledSource]]) { const zero = await approve({ probeIds }), before = engines, zeroOp = await applyPlan(principal, zero.planId, randomUUID()); assert.equal(await terminal(zeroOp.id), "applied"); const r = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [zeroOp.id])).rows[0].result; assert.equal(r.polled, 0); assert.equal(r.globalAlertHookRequested, false); await delay(350); assert.equal(engines, before) } + assert.equal(posts, 2); checks.push("empty/unknown/disabled-source groups return0 without native or global alert hook") + const token = await new SignJWT({ sub: owner.id, apps: owner.apps, permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime("1h").setIssuer(process.env.AUTH_ISSUER!).sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET)) + const rest = await app.inject({ method: "POST", url: "/api/uptime/probes/collect-group", headers: { authorization: "Bearer " + token }, payload: input }) + assert.equal(rest.statusCode, 200); assert.deepEqual(rest.json(), { ok: true, polled: 2 }); assert.equal(posts, 4); assert.equal(await samples(), 4); await until(() => engines >= 2) + checks.push("real signed-JWT REST parity uses same shared group operation") + hold = true; barrier = new Promise(resolve => { releaseHold = resolve }); busy = manualDataCollection.collectPingGroup([p]); await until(() => posts === 5) + const busyPlan = await approve({ probeIds: [p] }), before = engines, busyOp = await applyPlan(principal, busyPlan.planId, randomUUID()) + assert.equal(await terminal(busyOp.id), "unknown"); assert.equal(posts, 5); assert.equal(await samples(), 4); assert.equal(engines, before) + const busyRest = await app.inject({ method: "POST", url: "/api/uptime/probes/collect-group", headers: { authorization: "Bearer " + token }, payload: { probeIds: [p] } }); assert.equal(busyRest.statusCode, 409) + hold = false; assert(releaseHold); releaseHold(); await busy; busy = undefined; await until(() => engines >= 3); assert.equal(await samples(), 5) + checks.push("shared busy409 becomes no fake MCP success/retry, REST409; only original active group finishes") + assert.equal(fixtureFailed, false); assert.equal(engineFailed, false); assert.equal(Number((await pool.query("SELECT count(*) AS n FROM alert_outbox")).rows[0].n), 0) + console.log("Owned manual Ping group accepted:", JSON.stringify({ checks, pingPosts: posts, samples: await samples(), actualGlobalAlertRuns: engines, notifierDelivery: false })) + } finally { + hold = false; releaseHold?.(); await busy?.catch(() => {}); hooks.clearAlertCollectorHooksTimer() + await scheduler.stopSchedulerForDatabaseOperation(); await Promise.allSettled([...runningEngines]); hooks.clearAlertCollectorHooksTimer() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation(); await app?.close() + server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())); await closePool() + } +} +void main().catch(() => { console.error("Owned manual Ping group acceptance failed; no external RouterOS/notifier action"); process.exitCode = 1 }) diff --git a/backend/src/modules/mcp/manual-traffic-actions.test.ts b/backend/src/modules/mcp/manual-traffic-actions.test.ts new file mode 100644 index 00000000..8481ed1e --- /dev/null +++ b/backend/src/modules/mcp/manual-traffic-actions.test.ts @@ -0,0 +1,93 @@ +/** Actual shared extraction and MCP adapter; synthetic scheduler/SQL boundaries. + * No DB, RouterOS, alert execution or notifications in this unit profile. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { beforeEach, mock, test } from "node:test" +import type { add } from "./actions.js" +import type { SchedulerRunReceipt } from "../../services/scheduler-recorded-run.js" + +const permission = "mm:traffic:write", principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 60000).toISOString() } +const context = { principal, connection: { owner_id: "owned-traffic-owner" } } as Parameters[8]["prepare"]>>[1] +const forward = { phase: "forward" as const, operationId: randomUUID(), stepIndex: 0 } +let receipt: SchedulerRunReceipt, thrown: unknown, calls = 0, settingReadFails = false +let settings = { enabled: true, intervalSec: 30, retentionDays: 14, lastCollectedAt: "2026-10-29T00:00:00.000Z", lastDurationMs: 12, lastError: "" } +let connection = { owner_id: context.connection.owner_id, owner_permissions: [permission], policy: { version: 1, permissions: [permission], scope: { allowGlobal: true, serverIds: [1, 2, 3] } } } +let catalog = [1, 2, 3].map(id => ({ id, enabled: id !== 3, routingEndpointEpoch: randomUUID() })) +const events: Array<{ level: string; eventType: string; message?: string }> = [], queries: string[] = [] +mock.module("../../services/scheduler.js", { namedExports: { async runSchedulerJobNow(job: string) { assert.equal(job, "traffic"); calls++; if (thrown) throw thrown; return receipt } } }) +mock.module("../../services/traffic-collector.js", { namedExports: { async getTrafficSettings() { if (settingReadFails) throw Error("settings receipt failed"); return { ...settings } } } }) +mock.module("../events/service/events-service.js", { namedExports: { async appendEvent(value: typeof events[number]) { events.push(value) } } }) +mock.module("../../db/index.js", { namedExports: { pool: { async query(sql: string) { + queries.push(sql) + if (sql.startsWith("SELECT id,enabled,")) return { rows: structuredClone(catalog) } + assert.equal(sql, 'SELECT enabled,interval_sec AS "intervalSec",retention_days AS "retentionDays" FROM traffic_settings WHERE id=1') + return { rows: [{ enabled: settings.enabled, intervalSec: settings.intervalSec, retentionDays: settings.retentionDays }] } +} } } }) +mock.module("./store.js", { namedExports: { async effectiveMcpConnection() { return structuredClone(connection) } } }) +mock.module("../../services/config-mutation-policy.js", { namedExports: { async assertVerifiedConfigurationAccess() {} } }) +const { collectTrafficManually } = await import("../../services/manual-traffic-collection.js") +const { registerManualTrafficActions } = await import("./manual-traffic-actions.js"), { findMcpService } = await import("./service-registry.js") +let registration: Parameters | undefined +registerManualTrafficActions((...args: Parameters) => { registration = args }); assert(registration) +const [operation, tool, _title, _section, requiredPermission, effect, scope, schema, handlers] = registration, service = findMcpService("traffic.collect")! +const prepare = async () => { const evidence = await handlers.prepare!({}, context), step = evidence.steps[0]; assert(step.kind === "service"); return { evidence, sealed: step.input } } +beforeEach(() => { + calls = 0; thrown = undefined; settingReadFails = false; events.length = 0; queries.length = 0 + settings = { enabled: true, intervalSec: 30, retentionDays: 14, lastCollectedAt: "2026-10-29T00:00:00.000Z", lastDurationMs: 12, lastError: "" } + catalog = [1, 2, 3].map(id => ({ id, enabled: id !== 3, routingEndpointEpoch: randomUUID() })) + connection = { owner_id: context.connection.owner_id, owner_permissions: [permission], policy: { version: 1, permissions: [permission], scope: { allowGlobal: true, serverIds: [1, 2, 3] } } } + receipt = { status: "ok", error: null, result: { v: 1, job: "traffic", sampledAt: settings.lastCollectedAt, servers: [{ serverId: 1, name: "one", host: "private-host", ok: true }] } } +}) +test("shared success and warning retain exact REST receipt and manual event order", async () => { + assert(receipt.result?.job === "traffic"); receipt.result.servers[0].warning = "peer slice unavailable" + const result = await collectTrafficManually() + assert.deepEqual(result, { statusCode: 200, body: { ok: true, warnings: [{ serverId: 1, warning: "peer slice unavailable" }], lastCollectedAt: settings.lastCollectedAt, lastDurationMs: 12, lastError: null } }) + assert.equal(calls, 1); assert.equal(events.length, 1); assert.equal(events[0].level, "warning"); assert.equal(events[0].eventType, "traffic.collect.manual.ok") +}) +test("shared missing result and skipped snapshot preserve 502/409 without success events", async () => { + receipt.result = null; assert.equal((await collectTrafficManually()).statusCode, 502); assert.equal(events.length, 0) + receipt.result = { v: 1, job: "traffic", sampledAt: settings.lastCollectedAt, skipped: true, servers: [] } + assert.equal((await collectTrafficManually()).statusCode, 409); assert.equal(events.length, 0); assert.equal(calls, 2) +}) +test("shared partial/fatal snapshot preserves warning failure branch and successful IDs", async () => { + assert(receipt.result?.job === "traffic"); receipt.result.servers.push({ serverId: 2, name: "two", host: "private-host", ok: false, error: "owned native failure" }) + const partial = await collectTrafficManually(); assert.equal(partial.statusCode, 502); assert.deepEqual(partial.body.succeeded, [1]); assert.equal(partial.body.failures?.length, 1) + assert.equal(events[0].level, "warning"); assert.equal(events[0].eventType, "traffic.collect.manual.failed") + receipt.result.fatalError = "fatal owned collector"; assert.equal((await collectTrafficManually()).body.error, "fatal owned collector") +}) +test("shared thrown busy and thrown/receipt uncertainty retain 409 or critical 500, no retry", async () => { + thrown = Object.assign(Error("Job already running"), { statusCode: 409 }); assert.equal((await collectTrafficManually()).statusCode, 409); assert.equal(events.length, 0) + thrown = Error("uncertain scheduler journal"); assert.equal((await collectTrafficManually()).statusCode, 500); assert.equal(events[0].level, "critical") + thrown = undefined; settingReadFails = true; assert.equal((await collectTrafficManually()).statusCode, 500); assert.equal(calls, 3) +}) +test("GLOBAL human adapter advertises complete scheduler/retention/alerts footprint, plans only public config", async () => { + assert.equal(operation, "traffic.collect"); assert.equal(tool, "mm_traffic_collect_plan"); assert.equal(requiredPermission, permission); assert.equal(effect, "write"); assert.equal(scope, "global") + assert(schema.safeParse({}).success); assert(!schema.safeParse({ serverId: 1 }).success) + const { evidence } = await prepare(); assert.equal(evidence.requiresHuman, true); assert.equal(evidence.steps.length, 1); assert.equal(evidence.verificationScope, "acknowledgement") + assert.deepEqual(evidence.objects.filter(value => value.kind === "server").map(value => value.serverId), [1, 2, 3]) + for (const kind of ["traffic-samples", "scheduler-runs", "events", "partition-maintenance", "alert-engine", "alerts-outbox"]) assert(evidence.objects.some(value => value.kind === kind)) + assert.equal(calls, 0); assert.equal(queries.length, 2); assert(queries.every(sql => !/password|username|last_collected_at/.test(sql))) +}) +test("GLOBAL/all disabled-inclusive catalog, owner permission and stale configuration refuse before shared operation", async () => { + connection.policy.scope.allowGlobal = false; await assert.rejects(prepare()); connection.policy.scope.allowGlobal = true + connection.policy.scope.serverIds = [1, 2]; await assert.rejects(prepare()); connection.policy.scope.serverIds = [1, 2, 3] + connection.owner_permissions = []; await assert.rejects(prepare()); connection.owner_permissions = [permission] + const { sealed } = await prepare(); catalog[0].routingEndpointEpoch = randomUUID(); await assert.rejects(service.execute(sealed, forward)); assert.equal(calls, 0) +}) +test("stable settings ignore sampling timestamps; one actual shared call returns bounded summary without native warnings", async () => { + const { sealed, evidence } = await prepare(); settings.lastCollectedAt = "2026-10-29T00:01:00.000Z" + assert.deepEqual(await service.current(sealed), evidence.before) + assert(receipt.result?.job === "traffic"); receipt.result.servers[0].warning = "PRIVATE_NATIVE_SECRET"; settings.lastError = "PRIVATE_NATIVE_SECRET" + const result = await service.execute(sealed, forward) + assert.deepEqual(result, { acknowledged: true, warnings: 1, lastCollectedAt: settings.lastCollectedAt, lastDurationMs: 12, lastError: true, globalAlertHookRequested: true, alertDeliveryConfirmed: false }) + assert.equal(JSON.stringify(result).includes("PRIVATE_NATIVE_SECRET"), false); assert.equal(calls, 1); assert.equal(await service.verify!(sealed, result), true) + assert.deepEqual(await service.reconcile!(sealed, async () => {}), { matchesDesiredState: false, matchesOriginalState: false }); assert.equal(calls, 1) +}) +test("busy/partial/unknown shared outcomes cannot become MCP acknowledged success or trigger retries", async () => { + const { sealed } = await prepare() + for (const error of [Object.assign(Error("busy"), { statusCode: 409 }), Error("journal uncertain")]) { + thrown = error; await assert.rejects(service.execute(sealed, forward)); assert.equal(await service.verify!(sealed, { ok: true }), false) + } + assert.equal(calls, 2) + await assert.rejects(service.execute(sealed, { phase: "recovery", operationId: forward.operationId })); assert.equal(calls, 2) +}) diff --git a/backend/src/modules/mcp/manual-traffic-actions.ts b/backend/src/modules/mcp/manual-traffic-actions.ts new file mode 100644 index 00000000..2b181cee --- /dev/null +++ b/backend/src/modules/mcp/manual-traffic-actions.ts @@ -0,0 +1,70 @@ +/** Full ordinary manual traffic collection, not a scoped interface diagnostic. */ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { collectTrafficManually } from "../../services/manual-traffic-collection.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const permission = "mm:traffic:write" +const identitySchema = z.object({ principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict(), ownerId: z.string().min(1).max(200) }).strict() +const hash = z.string().regex(/^[a-f0-9]{64}$/), serviceSchema = identitySchema.extend({ policyProof: hash, beforeFingerprint: hash }).strict() +const serverSchema = z.object({ id: z.number().int().positive().safe(), enabled: z.boolean(), routingEndpointEpoch: z.string().uuid() }).strict() +const settingsSchema = z.object({ enabled: z.boolean(), intervalSec: z.number().int().positive().max(2147483647), retentionDays: z.number().int().positive().max(2147483647) }).strict() +async function source(input: z.infer & { policyProof?: string }) { + const connection = await effectiveMcpConnection(input.principal) + if (connection.owner_id !== input.ownerId || !connection.policy.scope.allowGlobal || !hasPermission(connection.policy.permissions, permission) || !hasPermission(connection.owner_permissions, permission)) + throw new McpError("PERMISSION_DENIED", "Полный сбор трафика требует глобального допуска", 403) + const policyProof = fingerprint(connection.policy) + if (input.policyProof && input.policyProof !== policyProof) throw new McpError("STATE_CONFLICT", "Допуск изменился; подготовьте новый план", 409) + const result = await pool.query('SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM servers ORDER BY id LIMIT 1001') + const catalog = z.array(serverSchema).max(1000).safeParse(result.rows) + if (!catalog.success || catalog.data.some((row, index) => index > 0 && row.id <= catalog.data[index - 1].id)) throw new McpError("STATE_UNAVAILABLE", "Каталог трафика отсутствует или превышает 1000 устройств", 409) + if (catalog.data.some(row => !connection.policy.scope.serverIds.includes(row.id))) throw new McpError("SERVER_DENIED", "Глобальные оповещения требуют всего каталога, включая отключённые устройства", 403) + const settings = await pool.query('SELECT enabled,interval_sec AS "intervalSec",retention_days AS "retentionDays" FROM traffic_settings WHERE id=1') + const parsed = settings.rows.length === 1 ? settingsSchema.safeParse(settings.rows[0]) : undefined + if (settings.rows.length > 1 || settings.rows.length === 1 && !parsed?.success) throw new McpError("STATE_UNAVAILABLE", "Настройки трафика не подтверждены", 409) + return { before: { servers: catalog.data, settings: parsed?.success ? parsed.data : null }, serverIds: catalog.data.map(row => row.id), policyProof } +} +function summary(result: Awaited>) { + if (result.statusCode !== 200) throw new McpError(result.statusCode === 409 ? "COLLECTOR_BUSY" : "COLLECTION_FAILED", + "Сбор трафика не подтверждён. Данные, журнал и оповещения уже могли измениться; автоматический повтор запрещён", result.statusCode) + const value = z.object({ ok: z.literal(true), warnings: z.array(z.unknown()).max(1000), lastCollectedAt: z.string().max(64).nullable(), lastDurationMs: z.number().nonnegative().nullable(), lastError: z.string().nullable() }).parse(result.body) + if (value.lastCollectedAt !== null && !Number.isFinite(Date.parse(value.lastCollectedAt))) throw new McpError("STATE_UNAVAILABLE", "Время сбора не подтверждено", 502) + return { acknowledged: true, warnings: value.warnings.length, lastCollectedAt: value.lastCollectedAt, lastDurationMs: value.lastDurationMs, + lastError: !!value.lastError, globalAlertHookRequested: true, alertDeliveryConfirmed: false } +} +export function registerManualTrafficActions(register: typeof add) { + registerMcpService("traffic.collect", { + current: async raw => (await source(serviceSchema.parse(raw))).before, + execute: async (raw, context) => { + if (context?.phase !== "forward" || !z.string().uuid().safeParse(context.operationId).success) throw new McpError("INVALID_EXECUTION_CONTEXT", "Сбор требует подтверждённого MCP плана", 409) + const input = serviceSchema.parse(raw) + await assertVerifiedConfigurationAccess(); const current = await source(input) + if (fingerprint(current.before) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "Каталог или настройки трафика изменились после подтверждения", 409) + await assertVerifiedConfigurationAccess() + return summary(await collectTrafficManually()) + }, + verify: async (_input, result) => !!result && typeof result === "object" && "acknowledged" in result && result.acknowledged === true, + reconcile: async (_input, authorize) => { await authorize(); return { matchesDesiredState: false, matchesOriginalState: false } }, + }) + register("traffic.collect", "mm_traffic_collect_plan", "Ручной сбор трафика и оповещения", "traffic", permission, "write", "global", z.object({}).strict(), { + version: 1, + prepare: async (_raw, context) => { + const identity = identitySchema.parse({ principal: context.principal, ownerId: context.connection.owner_id }), current = await source(identity) + return { target: { scope: "global" }, objects: [...current.serverIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), + { kind: "traffic-settings", id: "1" }, { kind: "traffic-samples", id: "global" }, { kind: "traffic-collection-state", id: "global" }, + { kind: "scheduler-runs", id: "global" }, { kind: "events", id: "global" }, { kind: "partition-maintenance", id: "global" }, + { kind: "alerts-configuration", id: "1" }, { kind: "alert-engine", id: "global" }, { kind: "alerts-outbox", id: "global" }], + snapshots: [], before: current.before, requiresHuman: true, mutationCost: 1, verificationScope: "acknowledgement", + steps: [{ kind: "service", service: "traffic.collect", input: { ...identity, policyProof: current.policyProof, beforeFingerprint: fingerprint(current.before) } }], + warnings: ["ГЛОБАЛЬНЫЙ ручной сбор: обычный scheduler job traffic, общий collection lock, GET интерфейсов/WireGuard peers включённых устройств, samples и настройки.", + "Сохраняются scheduler journal, retention, events, partitions и отложенный GLOBAL ALERT ENGINE/outbox всего приложения; возможны уведомления/Telegram, включая историю отключённых устройств.", + "При отсутствии настроек collector создаёт стандартные: enabled=true, intervalSec=30, retentionDays=14. Подготовка плана ничего не создаёт.", + "Busy, частичный результат и потеря подтверждения не считаются успешным сбором. Уже выполненные записи/оповещения не повторяются автоматически; receipt не подтверждает доставку уведомлений."] } + }, + }) +} diff --git a/backend/src/modules/mcp/manual-traffic-http.test.ts b/backend/src/modules/mcp/manual-traffic-http.test.ts new file mode 100644 index 00000000..a869d6da --- /dev/null +++ b/backend/src/modules/mcp/manual-traffic-http.test.ts @@ -0,0 +1,136 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +/** Ordinary complete traffic collect-now: owned app PostgreSQL and localhost + * RouterOS GETs, genuine MCP runtime, actual scheduler/alerts, blank notifier. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" + +async function main() { + assert.equal(process.env.MMAPP_OWN_MANUAL_TRAFFIC, "true") + const target = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(target.protocol)); assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") + assert(target.password); assert.equal(target.pathname, "/postgres"); assert.equal(target.search, ""); assert.equal(target.hash, "") + assert(Number.isSafeInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)); assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.AUTH_REQUIRED = "true"; process.env.AUTH_JWT_SECRET = "owned-manual-traffic-acceptance-secret"; process.env.AUTH_ISSUER = "https://owned-manual-traffic.test" + const { pool, closePool } = await import("../../db/index.js"), { applySqlMigrations } = await import("../../db/migrate.js") + const { ensurePartitionsAround } = await import("../../db/partitions.js"), { buildApp } = await import("../../index.js") + const { createMcpConnection, authenticateMcpToken, updateMcpSettings } = await import("./store.js"), { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const { collectTrafficManually } = await import("../../services/manual-traffic-collection.js"), hooks = await import("../../services/alert-collector-hooks.js") + const { executeSchedulerJob } = await import("../../services/scheduler.js"), { Client } = await import("pg") + const owner = { id: "owned-manual-traffic-owner", email: "owned-traffic@example.test", name: "Owned traffic", apps: ["mm"], permissions: ["mm:settings:admin", "mm:traffic:write"], expiresAt: Date.now() + 3600000 } + const counts = { interfaces: 0, peers: 0, disabled: 0, posts: 0 }, groups: string[] = [], engines = new Set>() + let mode: "normal" | "warning" | "partial" = "normal", hold = false, barrier: Promise | undefined, releaseHold: (() => void) | undefined + let fixtureFailed = false, alertRuns = 0, alertFailed = false, journalFault = false, faultInjected = false + const originalQuery = Client.prototype.query + Client.prototype.query = function(this: InstanceType, ...args: unknown[]) { + const config = args[0] as { text?: string; values?: unknown[] }, text = typeof args[0] === "string" ? args[0] : config.text ?? "" + const values = (typeof args[0] === "string" ? args[1] : config.values ?? args[1]) as unknown[] | undefined + const selected = journalFault && /insert\s+into\s+"?scheduler_runs"?/i.test(text) && Array.isArray(values) && values.includes("traffic") + const result = Reflect.apply(originalQuery, this, args) as unknown + if (selected && result && typeof (result as Promise).then === "function") return (result as Promise).then(value => { + void value; journalFault = false; faultInjected = true; throw Error("Owned scheduler journal ACK loss after actual INSERT") + }) + return result + } as typeof Client.prototype.query + const responders = [1, 2].map(index => createServer((request, response) => { + void (async () => { + if (request.method !== "GET") { counts.posts++; throw Error("Unexpected native mutation") } + if (request.headers.authorization === "Basic " + Buffer.from("owned-disabled:owned-password").toString("base64")) counts.disabled++ + else assert.equal(request.headers.authorization, "Basic " + Buffer.from(`owned-${index}:owned-password`).toString("base64")) + const path = request.url?.split("?")[0]; response.setHeader("content-type", "application/json") + if (path === "/rest/interface") { + counts.interfaces++; if (hold) await barrier + if (mode === "partial" && index === 2) { response.statusCode = 500; response.end('{"message":"owned failure"}'); return } + response.end(JSON.stringify([{ ".id": "*1", name: "ether1", running: "true", disabled: "false", "rx-byte": String(counts.interfaces * 1000), "tx-byte": String(counts.interfaces * 2000) }])); return + } + if (path === "/rest/interface/wireguard/peers") { + counts.peers++; if (mode === "warning" && index === 1) { response.statusCode = 404; response.end("{}"); return } + response.end("[]"); return + } + fixtureFailed = true; response.statusCode = 404; response.end("{}") + })().catch(() => { fixtureFailed = true; response.destroy() }) + })) + let app: Awaited> | undefined, busyRun: Promise | undefined + const count = async (table: "traffic_samples" | "scheduler_runs" | "events" | "traffic_collection_state") => Number((await pool.query(`SELECT count(*)::int AS n FROM ${table}`)).rows[0].n) + const until = async (check: () => Promise | boolean) => { const end = performance.now() + 20000; while (!await check()) { assert(performance.now() < end, "Owned traffic operation did not finish; never replay"); await delay(25) } } + const terminal = async (id: string) => { let state = ""; await until(async () => { state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; return !["pending", "running"].includes(state) }); return state } + try { + await applySqlMigrations(pool); assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0); await ensurePartitionsAround(pool) + await pool.query("INSERT INTO traffic_settings(id,enabled,interval_sec,retention_days) VALUES(1,false,30,14) ON CONFLICT(id) DO UPDATE SET enabled=false,interval_sec=30,retention_days=14") + await pool.query("INSERT INTO alert_telegram_settings(id,bot_token,chat_id) VALUES(1,'','') ON CONFLICT(id) DO UPDATE SET bot_token='',chat_id='' ") + await pool.query("INSERT INTO scheduler_runs(id,job_key,started_at,finished_at,status) VALUES('owned-retained-old','traffic',now()-interval '90 days',now()-interval '90 days','ok')") + await pool.query("INSERT INTO events(id,created_at,level,event_type,source_module,title,message) VALUES('owned-retained-old',now()-interval '90 days','info','owned.old','traffic','owned','owned')") + for (const responder of responders) await new Promise(resolve => responder.listen(0, "127.0.0.1", resolve)) + const ids: number[] = [] + for (let index = 0; index < 3; index++) ids.push(Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) VALUES($1,'127.0.0.1',$2,false,false,$3,'owned-password',$4) RETURNING id", + ["owned-traffic-" + index, (responders[index % 2].address() as { port: number }).port, index === 2 ? "owned-disabled" : "owned-" + (index + 1), index < 2])).rows[0].id)) + await updateMcpSettings({ enabled: true }, owner); app = await buildApp({ startScheduler: false }); await app.ready() + hooks.wireAlertEngineRunner(async () => { + const pending = (async () => { await executeSchedulerJob("alert_engine"); const row = (await pool.query("SELECT status,result_json FROM scheduler_runs WHERE job_key='alert_engine' ORDER BY finished_at DESC LIMIT 1")).rows[0]; assert.equal(row?.status, "ok"); assert.equal(row.result_json.skippedNoTelegram, true); alertRuns++ })() + engines.add(pending); try { await pending } catch { alertFailed = true; throw Error("Owned alert engine failed") } finally { engines.delete(pending) } + }) + const issue = async (serverIds: number[], allowGlobal: boolean) => authenticateMcpToken((await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned manual traffic", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:traffic:write"], scope: { serverIds, operationIds: ["traffic.collect"], allowGlobal }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner)).token) + const principal = await issue(ids, true), prepare = () => invokeMcpTool(principal, "mm_traffic_collect_plan", {}) as Promise<{ planId: string; state: string }> + const approved = async () => { const plan = await prepare(); await approvePlan(plan.planId, owner, true); return plan } + const token = await new SignJWT({ sub: owner.id, apps: owner.apps, permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime("1h").setIssuer(process.env.AUTH_ISSUER!).sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET)) + const rest = () => app!.inject({ method: "POST", url: "/api/traffic/collect-now", headers: { authorization: "Bearer " + token }, payload: {} }) + console.log("Owned manual traffic stage:", "scope-and-approval") + await assert.rejects(invokeMcpTool(await issue(ids, false), "mm_traffic_collect_plan", {})); await assert.rejects(invokeMcpTool(await issue(ids.slice(0, 2), true), "mm_traffic_collect_plan", {})) + const denied = await prepare(); assert.equal(denied.state, "awaiting-approval"); await assert.rejects(applyPlan(principal, denied.planId, randomUUID())); assert.equal(counts.interfaces, 0) + groups.push("GLOBAL/all catalog IDs including disabled and human approval deny before RouterOS GET") + + console.log("Owned manual traffic stage:", "complete-scheduler-path") + const plan = await approved(), key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await terminal(operation.id), "applied"); assert.equal(counts.interfaces, 2); assert.equal(counts.peers, 2); assert.equal(await count("traffic_samples"), 2); assert.equal(await count("traffic_collection_state"), 2) + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].result + assert.equal(receipt.acknowledged, true); assert.equal(receipt.warnings, 0); assert(receipt.lastCollectedAt); assert.equal(receipt.lastError, false); assert.equal(receipt.alertDeliveryConfirmed, false) + assert(Buffer.byteLength(JSON.stringify(receipt)) < 16384) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM scheduler_runs WHERE id='owned-retained-old'")).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM events WHERE id='owned-retained-old'")).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM events WHERE event_type='traffic.collect.manual.ok'")).rows[0].n), 1) + assert((await pool.query("SELECT last_collected_at,last_duration_ms FROM traffic_settings WHERE id=1")).rows[0].last_collected_at) + await until(() => alertRuns >= 1); groups.push("full scheduler traffic path persists samples/state/settings, journal/events/retention and real deferred global alert job") + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id); assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, operation.id); assert.equal(counts.interfaces, 2) + groups.push("reapplying original plan never repeats native collection") + + console.log("Owned manual traffic stage:", "rest-warning-and-partial") + mode = "warning"; const warning = await rest(); assert.equal(warning.statusCode, 200); assert.equal(warning.json().ok, true); assert.equal(warning.json().warnings.length, 1); assert.equal(await count("traffic_samples"), 4) + mode = "partial"; const partial = await rest(); assert.equal(partial.statusCode, 502); assert.equal(partial.json().ok, false); assert.equal(partial.json().failures.length, 1); assert.deepEqual(partial.json().succeeded, [ids[0]]) + assert.equal(await count("traffic_samples"), 5); groups.push("same REST service preserves success warnings and partial-failure 502 with real successful samples") + + console.log("Owned manual traffic stage:", "busy-shared-job") + mode = "normal"; hold = true; barrier = new Promise(resolve => { releaseHold = resolve }); const beforeBusy = counts.interfaces + busyRun = collectTrafficManually(); await until(() => counts.interfaces === beforeBusy + 1) + const busyPlan = await approved(), busyOperation = await applyPlan(principal, busyPlan.planId, randomUUID()); assert.equal(await terminal(busyOperation.id), "unknown") + assert.equal((await rest()).statusCode, 409); assert.equal(counts.interfaces, beforeBusy + 1) + hold = false; assert(releaseHold); releaseHold(); await busyRun; busyRun = undefined + groups.push("actual shared scheduler/collector busy state returns REST 409 and MCP UNKNOWN without duplicate GETs") + + console.log("Owned manual traffic stage:", "journal-ack-loss") + const unknownPlan = await approved(), beforeUnknown = counts.interfaces, samplesBefore = await count("traffic_samples") + journalFault = true; const unknown = await applyPlan(principal, unknownPlan.planId, randomUUID()); assert.equal(await terminal(unknown.id), "unknown"); assert(faultInjected) + assert.equal(counts.interfaces, beforeUnknown + 2); assert.equal(await count("traffic_samples"), samplesBefore + 2) + assert.equal((await applyPlan(principal, unknownPlan.planId, randomUUID())).id, unknown.id); assert.equal(counts.interfaces, beforeUnknown + 2) + journalFault = true; faultInjected = false; assert.equal((await rest()).statusCode, 500); assert(faultInjected) + assert(Number((await pool.query("SELECT count(*) AS n FROM events WHERE event_type='traffic.collect.manual.failed' AND level='critical'")).rows[0].n) >= 2) + groups.push("real scheduler journal INSERT acknowledgement loss preserves committed samples but yields MCP UNKNOWN/REST critical 500 without replay") + await until(() => alertRuns >= 2); assert.equal(alertFailed, false); assert.equal(fixtureFailed, false); assert.equal(counts.disabled, 0); assert.equal(counts.posts, 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM alert_outbox")).rows[0].n), 0) + console.log("Owned manual traffic accepted:", JSON.stringify({ groups: groups.length, checks: groups, nativeGets: counts.interfaces + counts.peers, sampleRows: await count("traffic_samples"), actualGlobalAlertRuns: alertRuns, notifierDelivery: false })) + } finally { + journalFault = false; Client.prototype.query = originalQuery; hold = false; releaseHold?.(); await busyRun?.catch(() => {}) + hooks.clearAlertCollectorHooksTimer(); await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation(); await Promise.allSettled([...engines]); hooks.clearAlertCollectorHooksTimer() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation(); await app?.close() + for (const responder of responders) { responder.closeAllConnections(); await new Promise(resolve => responder.close(() => resolve())) } + await closePool() + } +} +void main().catch(() => { console.error("Owned manual traffic acceptance failed; no real RouterOS/notifier action"); process.exitCode = 1 }) diff --git a/backend/src/modules/mcp/manual-uptime-actions.test.ts b/backend/src/modules/mcp/manual-uptime-actions.test.ts new file mode 100644 index 00000000..ce1c2168 --- /dev/null +++ b/backend/src/modules/mcp/manual-uptime-actions.test.ts @@ -0,0 +1,107 @@ +/** Ordinary MCP adapter + ACTUAL shared manual executor with inert collector + * and alert hook. No database, network, notifications or scheduler execution. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { beforeEach, mock, test } from "node:test" +import { createManualDataCollection } from "../../services/manual-data-collection-execution.js" +import type { add } from "./actions.js" + +const permission="mm:uptime:write",principal={connectionId:randomUUID(),credentialId:randomUUID(),credentialExpiresAt:new Date(Date.now()+60000).toISOString()} +const context={principal,connection:{owner_id:"owned-manual-actor"}} as Parameters[8]["prepare"]>>[1] +const forward={phase:"forward" as const,operationId:randomUUID(),stepIndex:0} +const epochs=[randomUUID(),randomUUID(),randomUUID()],queries:string[]=[],events:string[]=[] +let connection:{owner_id:string;owner_permissions:string[];policy:{version:number;permissions:string[];scope:{allowGlobal:boolean;serverIds:number[]}}} +let catalog:Array<{id:number;enabled:boolean;routingEndpointEpoch:string}>,settings:Record|undefined,collectorFailure:Error|undefined,readFailure=false,lastError="" +let probes:Array>=[],parts={resources:{servers:[{status:"online"}],skipped:false,fatalError:undefined as string|undefined},ping:{probes:[{ok:true}],skipped:false,skippedByInterval:2,fatalError:undefined as string|undefined}} +let lastCollectedAt="2026-10-29T00:00:00.000Z",collectCalls=0,alertCalls=0 +const shared=createManualDataCollection({ + async collectServersRestPing(){assert.fail("wrong shared operation")}, + async readServersRestPingSettings(){assert.fail("wrong settings operation")}, + async collectUptime(){collectCalls++;events.push("resources","ping");if(collectorFailure)throw collectorFailure;return parts},async readUptimeSettings(){events.push("receipt-settings");if(readFailure)throw Error("uncertain receipt");return {lastCollectedAt,lastDurationMs:23,lastError}},async collectPingGroup(){assert.fail("wrong shared operation")}, + scheduleGlobalAlerts(){alertCalls++;events.push("global-alert-hook")}, +}) +mock.module("../../db/index.js",{namedExports:{pool:{async query(sql:string){queries.push(sql) + if(sql.startsWith('SELECT id,enabled,routing_endpoint_epoch'))return {rows:structuredClone(catalog)} + if(sql.includes("FROM uptime_settings"))return {rows:settings?[structuredClone(settings)]:[]} + assert(sql.includes("FROM uptime_probes"));return {rows:structuredClone(probes)} +}}}}) +mock.module("./store.js",{namedExports:{async effectiveMcpConnection(){return structuredClone(connection)}}}) +mock.module("../../services/config-mutation-policy.js",{namedExports:{async assertVerifiedConfigurationAccess(){events.push("authorize")}}}) +mock.module("../../services/manual-data-collection.js",{namedExports:{manualDataCollection:shared}}) +const {registerManualUptimeActions}=await import("./manual-uptime-actions.js"),{findMcpService}=await import("./service-registry.js") +let registration:Parameters|undefined +registerManualUptimeActions((...args:Parameters)=>{registration=args});assert(registration) +const [operation,tool,_title,_section,requiredPermission,effect,scope,schema,handlers]=registration,service=findMcpService("uptime.collect")! +assert(service) +async function prepare(){const evidence=await handlers.prepare!({},context),step=evidence.steps[0];assert(step.kind==="service");return {evidence,sealed:step.input}} +beforeEach(()=>{ + queries.length=0;events.length=0;collectCalls=0;alertCalls=0;collectorFailure=undefined;readFailure=false;lastError="";lastCollectedAt="2026-10-29T00:00:00.000Z" + connection={owner_id:context.connection.owner_id,owner_permissions:[permission],policy:{version:1,permissions:[permission],scope:{allowGlobal:true,serverIds:[1,2,3]}}} + catalog=epochs.map((routingEndpointEpoch,index)=>({id:index+1,enabled:index!==2,routingEndpointEpoch}));settings={enabled:true,resourcesEnabled:true,pingEnabled:true,speedEnabled:false,intervalSec:15,probeIntervalSec:15,speedIntervalSec:60,retentionDays:14} + probes=[{id:"owned-probe",srcServerId:1,srcInterface:"",name:"owned",target:"192.0.2.1",enabled:true,intervalSec:0,sortOrder:0}] + parts={resources:{servers:[{status:"online"}],skipped:false,fatalError:undefined},ping:{probes:[{ok:true}],skipped:false,skippedByInterval:2,fatalError:undefined}} +}) + +test("global human uptime plan binds all devices/probes/settings with no credentials or changing receipt timestamps",async()=>{ + assert.equal(operation,"uptime.collect");assert.equal(tool,"mm_uptime_collect_plan");assert.equal(requiredPermission,permission);assert.equal(effect,"write");assert.equal(scope,"global") + assert.equal(schema.safeParse({serverId:1}).success,false) + const {evidence,sealed}=await prepare();assert.equal(evidence.requiresHuman,true);assert.deepEqual(evidence.target,{scope:"global"}) + assert.deepEqual(evidence.objects.filter(value=>value.kind==="server").map(value=>value.serverId),[1,2,3]);assert(evidence.objects.some(value=>value.kind==="ping-probe"&&value.id==="owned-probe")) + const warnings=evidence.warnings.join(" ");for(const text of ["GLOBAL ALERT ENGINE","outbox","Telegram","resourcesEnabled","Interval"])assert(warnings.includes(text)) + assert.equal(queries.length,3);assert(queries.every(query=>!query.includes("password")&&!query.includes("last_collected"))) + lastCollectedAt="2026-10-29T00:02:00Z";assert.deepEqual(await service.current(sealed),evidence.before);assert.equal(collectCalls,0) +}) +test("global/all-catalog permission gate rejects disabled-ID omission; missing settings planning is read-only",async()=>{ + connection.policy.scope.allowGlobal=false;await assert.rejects(prepare());connection.policy.scope.allowGlobal=true + connection.policy.scope.serverIds=[1,2];await assert.rejects(prepare());assert.equal(collectCalls,0);assert.equal(alertCalls,0) + connection.policy.scope.serverIds=[1,2,3];settings=undefined;const {sealed,evidence}=await prepare();assert.deepEqual(await service.current(sealed),evidence.before);assert.equal(settings,undefined) + catalog=Array.from({length:1001},(_,index)=>({id:index+1,enabled:true,routingEndpointEpoch:epochs[0]}));await assert.rejects(prepare());assert.equal(collectCalls,0) +}) +test("one full shared executor preserves resource→ping→global alert order and bounded truthful parts",async()=>{ + const {sealed}=await prepare();lastError="PRIVATE_ERROR_TEXT" + const output=await service.execute(sealed,forward) as {acknowledged:boolean;parts:{resources:{count:number};ping:{skippedByInterval:number}};lastError:boolean} + assert.deepEqual(events.slice(-4),["resources","ping","global-alert-hook","receipt-settings"]);assert.equal(collectCalls,1);assert.equal(alertCalls,1) + assert.equal(output.acknowledged,true);assert.equal(output.parts.resources.count,1);assert.equal(output.parts.ping.skippedByInterval,2);assert.equal(output.lastError,true) + assert(!JSON.stringify(output).includes(lastError));assert.equal(await service.verify!(sealed,output),true) + await service.reconcile!(sealed,async()=>{});assert.equal(collectCalls,1) +}) +test("busy/fatal resource or ping part cannot become silent full success, even when last settings error was cleared",async()=>{ + for(const mode of ["resources-skip","ping-skip","resources-fatal","ping-fatal"]){ + parts.resources.skipped=false;parts.ping.skipped=false;parts.resources.fatalError=undefined;parts.ping.fatalError=undefined + const {sealed}=await prepare() + if(mode==="resources-skip")parts.resources.skipped=true;if(mode==="ping-skip")parts.ping.skipped=true + if(mode==="resources-fatal")parts.resources.fatalError="PRIVATE_FAILURE";if(mode==="ping-fatal")parts.ping.fatalError="PRIVATE_FAILURE" + await assert.rejects(service.execute(sealed,forward));assert.equal(alertCalls,collectCalls) + } + assert.equal(collectCalls,4);assert.equal(alertCalls,4) +}) +test("normal per-target OFFLINE/down samples are valid observations, not collector partial failure",async()=>{ + parts.resources.servers=[{status:"offline"}];parts.ping.probes=[{ok:false}] + const {sealed}=await prepare();const result=await service.execute(sealed,forward);assert.equal(await service.verify!(sealed,result),true);assert.equal(collectCalls,1) +}) +test("probe/config/scope drift and collector/receipt errors never dispatch/replay a second full collector",async()=>{ + const {sealed}=await prepare();probes[0].target="192.0.2.2";await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,0) + probes[0].target="192.0.2.1";collectorFailure=Error("uncertain collector");await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,1);assert.equal(alertCalls,0) + collectorFailure=undefined;readFailure=true;await assert.rejects(service.execute(sealed,forward));assert.equal(collectCalls,2);assert.equal(alertCalls,1) +}) + +test("actual collector initial settings failures release resource/ping/group busy flags before next call (inert VM dependencies)",async()=>{ + const {readFile}=await import("node:fs/promises"),{runInNewContext}=await import("node:vm"),ts=await import("typescript") + const tables={uptimeSettings:{id:"settings"},uptimeResourceSamples:{},uptimeProbeSamples:{},uptimeProbes:{}} + let failRead=true + const builder=()=>{const value={from(){return value},where(){return value},orderBy(){return value},limit(){return value},groupBy(){return value},then(resolve:(rows:unknown[])=>unknown,reject?:(error:unknown)=>unknown){return Promise.resolve().then(()=>{if(failRead){failRead=false;throw Error("initial settings failed")}return [{id:1,enabled:true,intervalSec:15,probeIntervalSec:15}]}).then(resolve,reject)}};return value} + const db={select:builder,update:()=>({set:()=>({where:async()=>[]})})} + const modules:Record={"../db/index.js":{db,dbAll:async()=>[]},"../db/schema.js":tables,"drizzle-orm":Object.fromEntries(["and","asc","eq","gte","lte","inArray","max","sql"].map(name=>[name,()=>null])), + "./config-mutation-policy.js":{executeGuardedApplicationWrite:()=>assert.fail("no config mutation")},"./monitoring-server-catalog.js":{listEnabledMonitoringServers:async()=>[]},"./mikrotik.js":{MikrotikClient:class{}},"../types/scheduler-run-snapshot.js":{SCHEDULER_RUN_SNAPSHOT_VERSION:1},"../constants/ping-probe.js":{PING_PROBE_WARN_RTT_MS:100},"./ros-metric-parse.js":{},"./ping-probe-replacement.js":{},"./partition-write-transaction.js":{withPartitionWrite:()=>assert.fail("no targets"),isPartitionWriteOutcomeUnknown:()=>false,PartitionWriteError:class extends Error{}}} + const exports:RecordRecord|Promise>>={} + const code=ts.transpileModule(await readFile(new URL("../../services/uptime-collector.ts",import.meta.url),"utf8"),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText + runInNewContext(code,{exports,require:(name:string)=>{assert(name in modules,name);return modules[name]},Date,performance,console,Set,Map,Promise,Number,String,Math,Object,JSON,Error}) + for(const method of ["collectResourceSamplesOnce","collectPingProbesOnce","collectPingForProbeIds"]){ + failRead=true + if(method==="collectPingForProbeIds")await assert.rejects(async()=>exports[method](["probe"])) + else assert.equal(typeof (await exports[method]()).fatalError,"string") + const state=await exports.getUptimeCollectorsState();assert.equal(state.resources,false);assert.equal(state.ping,false) + const next=await exports[method](...(method==="collectPingForProbeIds"?[["probe"]]:[])) + assert.equal(next.skipped,undefined);assert.equal(next.fatalError,undefined) + } +}) diff --git a/backend/src/modules/mcp/manual-uptime-actions.ts b/backend/src/modules/mcp/manual-uptime-actions.ts new file mode 100644 index 00000000..732ea449 --- /dev/null +++ b/backend/src/modules/mcp/manual-uptime-actions.ts @@ -0,0 +1,65 @@ +/** Ordinary full manual uptime: resources THEN interval-aware ping, plus the + * existing deferred GLOBAL alert engine. No substitute scoped diagnostic. */ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { manualDataCollection } from "../../services/manual-data-collection.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const permission="mm:uptime:write",hash=z.string().regex(/^[a-f0-9]{64}$/) +const principal=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict() +const identity=z.object({principal,ownerId:z.string().min(1).max(200)}).strict(),serviceInput=identity.extend({policyProof:hash,beforeFingerprint:hash}) +const server=z.object({id:z.number().int().positive().safe(),enabled:z.boolean(),routingEndpointEpoch:z.string().uuid()}).strict() +const positive=z.number().int().positive().max(2147483647) +const settings=z.object({enabled:z.boolean(),resourcesEnabled:z.boolean(),pingEnabled:z.boolean(),speedEnabled:z.boolean(),intervalSec:positive,probeIntervalSec:positive,speedIntervalSec:positive,retentionDays:positive}).strict() +const probe=z.object({id:z.string().min(1).max(200),srcServerId:z.number().int().positive().safe(),srcInterface:z.string().max(200),name:z.string().max(1000),target:z.string().min(1).max(1000),enabled:z.boolean(),intervalSec:z.number().int().min(0).max(2147483647),sortOrder:z.number().int()}).strict() +async function source(input:z.infer&{policyProof?:string}){ + const connection=await effectiveMcpConnection(input.principal) + if(connection.owner_id!==input.ownerId||!connection.policy.scope.allowGlobal||!hasPermission(connection.policy.permissions,permission)||!hasPermission(connection.owner_permissions,permission))throw new McpError("PERMISSION_DENIED","Полный uptime требует глобального доступа",403) + const policyProof=fingerprint(connection.policy) + if(input.policyProof&&input.policyProof!==policyProof)throw new McpError("STATE_CONFLICT","Допуск изменился; подготовьте новый план",409) + const catalog=z.array(server).max(1000).safeParse((await pool.query('SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM servers ORDER BY id LIMIT 1001')).rows) + if(!catalog.success||catalog.data.some((row,index)=>index>0&&row.id<=catalog.data[index-1].id))throw new McpError("STATE_UNAVAILABLE","Каталог uptime превышает предел или не подтверждён",409) + const serverIds=catalog.data.map(row=>row.id) + if(serverIds.some(id=>!connection.policy.scope.serverIds.includes(id)))throw new McpError("SERVER_DENIED","Полный uptime требует весь каталог, включая отключённые устройства",403) + const rows=(await pool.query('SELECT enabled,resources_enabled AS "resourcesEnabled",ping_enabled AS "pingEnabled",speed_enabled AS "speedEnabled",interval_sec AS "intervalSec",probe_interval_sec AS "probeIntervalSec",speed_interval_sec AS "speedIntervalSec",retention_days AS "retentionDays" FROM uptime_settings WHERE id=1')).rows + const currentSettings=rows.length===1?settings.safeParse(rows[0]):undefined + if(rows.length>1||rows.length===1&&!currentSettings?.success)throw new McpError("STATE_UNAVAILABLE","Настройки uptime не подтверждены",409) + const probes=z.array(probe).max(1000).safeParse((await pool.query('SELECT id,src_server_id AS "srcServerId",src_interface AS "srcInterface",name,target,enabled,interval_sec AS "intervalSec",sort_order AS "sortOrder" FROM uptime_probes ORDER BY id LIMIT 1001')).rows) + if(!probes.success||new Set(probes.data.map(row=>row.id)).size!==probes.data.length||probes.data.some(row=>!serverIds.includes(row.srcServerId)))throw new McpError("STATE_UNAVAILABLE","Каталог ping-проб не подтверждён или превышает предел",409) + return {before:{servers:catalog.data,settings:currentSettings?.success?currentSettings.data:null,probes:probes.data},serverIds,probeIds:probes.data.map(row=>row.id),policyProof} +} +const part=z.object({reported:z.boolean(),skipped:z.boolean(),fatalError:z.boolean(),count:z.number().int().min(0),skippedByInterval:z.number().int().min(0).optional()}) +function summary(value:unknown){ + const receipt=z.object({ok:z.literal(true),skipped:z.boolean(),partial:z.boolean(),parts:z.object({resources:part,ping:part}),lastCollectedAt:z.unknown().optional(),lastDurationMs:z.unknown().optional(),lastError:z.unknown().optional()}).parse(value) + if(receipt.skipped||receipt.partial)throw new McpError(receipt.skipped?"COLLECTOR_BUSY":"COLLECTION_PARTIAL","Uptime завершился частично или пропущен; samples и global alert hook уже могли возникнуть. Автоматический повтор запрещён",409) + return {acknowledged:true,skipped:false,partial:false,parts:receipt.parts,lastCollectedAt:typeof receipt.lastCollectedAt==="string"&&receipt.lastCollectedAt.length<=64&&Number.isFinite(Date.parse(receipt.lastCollectedAt))?receipt.lastCollectedAt:null, + lastDurationMs:typeof receipt.lastDurationMs==="number"&&Number.isFinite(receipt.lastDurationMs)&&receipt.lastDurationMs>=0?receipt.lastDurationMs:null,lastError:!!receipt.lastError,globalAlertHookRequested:true,alertDeliveryConfirmed:false} +} +export function registerManualUptimeActions(register:typeof add){ + registerMcpService("uptime.collect",{ + current:async raw=>(await source(serviceInput.parse(raw))).before, + execute:async(raw,context)=>{ + if(context?.phase!=="forward"||!z.string().uuid().safeParse(context.operationId).success)throw new McpError("INVALID_EXECUTION_CONTEXT","Ручной uptime требует подтверждённого MCP плана",409) + const input=serviceInput.parse(raw);await assertVerifiedConfigurationAccess();const current=await source(input) + if(fingerprint(current.before)!==input.beforeFingerprint)throw new McpError("STATE_CONFLICT","Каталог, пробы или настройки uptime изменились",409) + await assertVerifiedConfigurationAccess();return summary(await manualDataCollection.collectUptime()) + }, + verify:async(_input,result)=>!!result&&typeof result==="object"&&"acknowledged"in result&&result.acknowledged===true&&"partial"in result&&result.partial===false, + reconcile:async(_input,authorize)=>{await authorize();return {matchesDesiredState:false,matchesOriginalState:false}}, + }) + register("uptime.collect","mm_uptime_collect_plan","Ручной uptime: ресурсы, ping и обработка оповещений","uptime",permission,"write","global",z.object({}).strict(),{ + version:1,prepare:async(_raw,context)=>{ + const original=identity.parse({principal:context.principal,ownerId:context.connection.owner_id}),current=await source(original) + return {target:{scope:"global"},objects:[...current.serverIds.map(serverId=>({kind:"server",id:String(serverId),serverId})),...current.before.probes.map(row=>({kind:"ping-probe",id:row.id,serverId:row.srcServerId})),{kind:"uptime-settings",id:"1"},{kind:"partition-maintenance",id:"global"},{kind:"alerts-configuration",id:"1"},{kind:"alert-engine",id:"global"},{kind:"alerts-outbox",id:"global"}],snapshots:[],before:current.before,requiresHuman:true,mutationCost:1,verificationScope:"acknowledgement", + steps:[{kind:"service",service:"uptime.collect",input:{...original,policyProof:current.policyProof,beforeFingerprint:fingerprint(current.before)}}], + warnings:["ГЛОБАЛЬНЫЙ ручной цикл: ресурсы включённых устройств, затем ping-пробы с действующими интервалами, samples, partitions и настройки. Автоматические resourcesEnabled/pingEnabled управляют расписанием; ручной REST-цикл запускает обе части независимо от этих флагов.", + "GLOBAL ALERT ENGINE/outbox вызывается отложенно для всего приложения, включая историю отключённых устройств; возможны уведомления/Telegram. Это не scoped read-only проверка.", + "Busy/partial не считается полным успехом; уже записанные samples и alert hook не повторяются автоматически. Interval skip не означает свежий sample. Receipt не подтверждает доставку уведомлений.","Отсутствующие настройки создаются обычным shared collector только при выполнении. Подготовка плана ничего не создаёт."]} + }, + }) +} diff --git a/backend/src/modules/mcp/manual-uptime-http.test.ts b/backend/src/modules/mcp/manual-uptime-http.test.ts new file mode 100644 index 00000000..cc600b20 --- /dev/null +++ b/backend/src/modules/mcp/manual-uptime-http.test.ts @@ -0,0 +1,140 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +/** Disposable ordinary MCP/REST manual collector acceptance. Real application + * SQL, localhost HTTP and deferred global alert engine; no notifier recipients. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" + +async function main() { + assert.equal(process.env.MMAPP_OWN_MANUAL_UPTIME, "true") + const target = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(target.protocol)); assert.equal(target.hostname, "127.0.0.1") + assert.equal(target.username, "mcp_test"); assert(target.password); assert.equal(target.pathname, "/postgres") + assert(Number.isSafeInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) + assert.equal(target.search, ""); assert.equal(target.hash, "") + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)); assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.AUTH_REQUIRED = "true"; process.env.AUTH_JWT_SECRET = "owned-manual-uptime-acceptance-secret"; process.env.AUTH_ISSUER = "https://owned-uptime.test" + const { pool, closePool } = await import("../../db/index.js"), { applySqlMigrations } = await import("../../db/migrate.js") + const { ensurePartitionsAround } = await import("../../db/partitions.js"), { buildApp } = await import("../../index.js") + const { createMcpConnection, authenticateMcpToken, updateMcpSettings } = await import("./store.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const hooks = await import("../../services/alert-collector-hooks.js"), { executeSchedulerJob } = await import("../../services/scheduler.js") + const { manualDataCollection } = await import("../../services/manual-data-collection.js") + const owner = { id: "owned-manual-uptime-owner", email: "owned-uptime@example.test", name: "Owned Uptime", apps: ["mm"], permissions: ["mm:settings:admin", "mm:uptime:write"], expiresAt: Date.now() + 3600000 } + let pingPosts = 0, gets = 0, disabledGets = 0, fixtureFailed = false, hold = false, releaseHold: (() => void) | undefined, barrier: Promise | undefined + let engineRuns = 0, engineFailed = false + const engines = new Set>(), groups: string[] = [] + const responders = [1, 2].map(index => createServer((request, response) => { + void (async () => { + if (request.headers.authorization === "Basic " + Buffer.from("owned-disabled:owned-disabled-password").toString("base64")) disabledGets++ + else assert.equal(request.headers.authorization, "Basic " + Buffer.from(`owned-${index}:owned-local-password`).toString("base64")) + const resource=request.method==="GET"&&request.url?.split("?")[0]==="/rest/system/resource" + if(resource)gets++;else{assert.equal(request.method,"POST");assert.equal(request.url,"/rest/tool/ping");pingPosts++} + if (hold&&resource) await barrier + response.setHeader("content-type", "application/json"); response.end(JSON.stringify(resource?{"cpu-load":"7","free-memory":"67108864","total-memory":"134217728","free-hdd-space":"67108864","total-hdd-space":"134217728",uptime:"1d2h",version:"7.fixture","board-name":"owned"}:[{sent:"4",received:"4","packet-loss":"0","avg-rtt":"2ms"}])) + })().catch(() => { fixtureFailed = true; response.destroy() }) + })) + let app: Awaited> | undefined, busyRun: Promise | undefined + const samples = async () => Number((await pool.query("SELECT count(*)::int AS n FROM uptime_resource_samples")).rows[0].n) + const settings = async () => (await pool.query("SELECT enabled,last_collected_at,last_duration_ms,last_error FROM uptime_settings WHERE id=1")).rows[0] + const until = async (check: () => Promise | boolean) => { const end = performance.now() + 20000; while (!await check()) { assert(performance.now() < end, "Owned manual collector did not finish; never replay"); await delay(25) } } + const terminal = async (id: string) => { let state = ""; await until(async () => { state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; return !["pending", "running"].includes(state) }); return state } + try { + await applySqlMigrations(pool); assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + await ensurePartitionsAround(pool) + // Deliberately exercise an absent singleton in this verified disposable DB; + // normal migrations may already seed defaults. Planning must stay read-only. + await pool.query("DELETE FROM uptime_settings WHERE id=1") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM uptime_settings")).rows[0].n),0,"Missing settings must be initialized by the shared executor, not planning") + await pool.query("INSERT INTO alert_telegram_settings(id,bot_token,chat_id) VALUES(1,'','') ON CONFLICT(id) DO UPDATE SET bot_token='',chat_id='' ") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM alert_outbox")).rows[0].n), 0) + for (const responder of responders) await new Promise(resolve => responder.listen(0, "127.0.0.1", resolve)) + const ids: number[] = [] + for (let index = 0; index < 3; index++) ids.push(Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) VALUES($1,'127.0.0.1',$2,false,false,$3,$4,$5) RETURNING id", + ["owned-uptime-" + index, (responders[index % 2].address() as { port: number }).port, index === 2 ? "owned-disabled" : "owned-" + (index + 1), index === 2 ? "owned-disabled-password" : "owned-local-password", index < 2])).rows[0].id)) + for(let index=0;index<2;index++)await pool.query("INSERT INTO uptime_probes(id,src_server_id,name,target,enabled,interval_sec) VALUES($1,$2,$3,$4,true,86400)",[randomUUID(),ids[index],"owned-uptime-probe-"+index,"192.0.2."+(index+1)]) + await pool.query("INSERT INTO alert_rules(id,name,type,target,condition,severity,enabled,rule_chat_id) VALUES($1,'owned-uptime-rule','server','owned-uptime-0','offline','warning',true,'')", [randomUUID()]) + await updateMcpSettings({ enabled: true }, owner); app = await buildApp({ startScheduler: false }); await app.ready() + hooks.wireAlertEngineRunner(async () => { + const pending = (async () => { + await executeSchedulerJob("alert_engine") + const row = (await pool.query("SELECT status,result_json FROM scheduler_runs WHERE job_key='alert_engine' ORDER BY finished_at DESC LIMIT 1")).rows[0] + assert(row); assert.equal(row.status, "ok"); assert.equal(row.result_json.skippedNoTelegram, true); engineRuns++ + })(); engines.add(pending) + try { await pending } + catch { engineFailed = true; throw Error("Owned actual alert engine failed") } + finally { engines.delete(pending) } + }) + const issue = async (serverIds: number[], allowGlobal: boolean) => authenticateMcpToken((await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned manual Uptime", mode: "approval", + expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:uptime:write"], scope: { serverIds, operationIds: ["uptime.collect"], allowGlobal }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner)).token) + const principal = await issue(ids, true) + const prepare = () => invokeMcpTool(principal, "mm_uptime_collect_plan", {}) as Promise<{ planId: string; state: string }> + const approve = async () => { const plan = await prepare(); await approvePlan(plan.planId, owner, true); return plan } + console.log("Owned manual Uptime stage:", "global-catalog-approval") + await assert.rejects(invokeMcpTool(await issue(ids, false), "mm_uptime_collect_plan", {})) + await assert.rejects(invokeMcpTool(await issue(ids.slice(0, 2), true), "mm_uptime_collect_plan", {})) + const unapproved = await prepare(); assert.equal(unapproved.state, "awaiting-approval"); await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())) + assert.equal(gets, 0); assert.equal(await samples(), 0) + groups.push("GLOBAL, all catalog IDs including disabled server and human approval are enforced before HTTP") + + console.log("Owned manual Uptime stage:", "full-mcp-collector") + const plan = await approve(), key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await terminal(operation.id), "applied"); assert.equal(gets, 2); assert.equal(disabledGets, 0); assert.equal(await samples(), 2) + const row = await settings(); assert.equal(row.enabled,true);assert(row.last_collected_at); assert(Number.isFinite(row.last_duration_ms)); assert.equal(row.last_error, "") + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].result + assert.equal(receipt.acknowledged, true); assert.equal(receipt.skipped, false); assert.equal(receipt.partial,false);assert.equal(receipt.parts.resources.count,2);assert.equal(receipt.parts.ping.count,2);assert.equal(pingPosts,2);assert.equal(Number((await pool.query("SELECT count(*) AS n FROM uptime_probe_samples")).rows[0].n),2); assert(receipt.lastCollectedAt); assert.equal(receipt.lastError, false) + assert.equal(receipt.globalAlertHookRequested, true); assert.equal(receipt.alertDeliveryConfirmed, false) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM uptime_probe_samples WHERE measurement_known=true AND status='up'")).rows[0].n),2) + assert(Buffer.byteLength(JSON.stringify(receipt)) < 16384) + await until(() => engineRuns >= 1); assert.equal(engineFailed, false) + groups.push("ordinary MCP collector resource GETs/ping POSTs enabled servers only, persists samples/settings and invokes actual deferred global alert engine without recipients") + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, operation.id) + assert.equal(gets, 2); assert.equal(await samples(), 2) + groups.push("reapplying the same plan returns its journal operation without fresh collection") + + console.log("Owned manual Uptime stage:", "rest-parity") + const token = await new SignJWT({ sub: owner.id, apps: owner.apps, permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime("1h").setIssuer(process.env.AUTH_ISSUER!).sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET)) + const response = await app.inject({ method: "POST", url: "/api/uptime/collect-now", headers: { authorization: "Bearer " + token }, payload: {} }) + assert.equal(response.statusCode, 200); const rest = response.json(); assert.equal(rest.ok, true); assert.equal(rest.skipped, false);assert.equal(rest.partial,false);assert.equal(rest.parts.resources.count,2);assert.equal(rest.parts.ping.skippedByInterval,2);assert.equal(pingPosts,2) + assert(rest.lastCollectedAt); assert.equal(typeof rest.lastDurationMs, "number"); assert.equal(rest.lastError, null) + assert.equal(gets, 4); assert.equal(disabledGets, 0); assert.equal(await samples(), 4) + await until(() => engineRuns >= 2) + groups.push("signed-JWT REST uses the same complete collector and receipt fields") + + console.log("Owned manual Uptime stage:", "busy-is-not-fresh") + hold = true; barrier = new Promise(resolve => { releaseHold = resolve }) + busyRun = manualDataCollection.collectUptime(); await until(() => gets === 5) + const busyPlan = await approve(), busyOperation = await applyPlan(principal, busyPlan.planId, randomUUID()) + assert.equal(await terminal(busyOperation.id), "unknown"); assert.equal(gets, 5); assert.equal(pingPosts,2); assert.equal(await samples(), 4) + const partialResponse=await app.inject({method:"POST",url:"/api/uptime/collect-now",headers:{authorization:"Bearer "+token},payload:{}}) + assert.equal(partialResponse.statusCode,200);const partial=partialResponse.json();assert.equal(partial.ok,true);assert.equal(partial.partial,true);assert.equal(partial.parts.resources.skipped,true);assert.equal(partial.parts.ping.skipped,false) + hold = false; assert(releaseHold); releaseHold(); await busyRun; busyRun = undefined + await until(() => engineRuns >= 3); assert.equal(await samples(), 6); assert.equal(disabledGets, 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM alert_outbox")).rows[0].n), 0) + assert.equal(fixtureFailed, false); assert.equal(engineFailed, false) + groups.push("shared busy collector is not reported as fresh MCP success and is not retried; existing active collector completes normally") + console.log("Owned manual Uptime accepted:", JSON.stringify({ groups: groups.length, checks: groups, resourceGets: gets,pingPosts, disabledGets, sampleRows: await samples(), actualGlobalAlertRuns: engineRuns, notifierDelivery: false })) + } finally { + hold = false; releaseHold?.(); await busyRun?.catch(() => {}) + hooks.clearAlertCollectorHooksTimer() + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await Promise.allSettled([...engines]); hooks.clearAlertCollectorHooksTimer() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close() + for (const responder of responders) { responder.closeAllConnections(); await new Promise(resolve => responder.close(() => resolve())) } + await closePool() + } +} +void main().catch(error => { + console.error("Owned manual Uptime acceptance failed; no real RouterOS/notifier action") + if (error instanceof assert.AssertionError) console.error({ code: error.code, actual: error.actual, expected: error.expected, stack: error.stack }) + process.exitCode = 1 +}) diff --git a/backend/src/modules/mcp/native-conservation-acceptance.ts b/backend/src/modules/mcp/native-conservation-acceptance.ts new file mode 100644 index 00000000..0748e4f4 --- /dev/null +++ b/backend/src/modules/mcp/native-conservation-acceptance.ts @@ -0,0 +1,568 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { managedComment } from "../../managed-markers.js" +import { updateServer } from "../servers/service/servers-service.js" +import type { ChangeEvidence } from "./action-types.js" +import { findAction } from "./actions.js" +import { readRosSnapshot } from "./resources.js" +import { approvePlan, applyPlan, invokeMcpTool } from "./runtime.js" +import { decryptSecret, encryptSecret } from "./security.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js" + +/** Owned SQL + loopback only. Models projected native configuration, not a CHR + * forwarding test, opaque-field proof, or proof of real native side effects. */ +export async function acceptNativeConservation(actor: AuthUser) { + const serverId = 198 + const filterPath = "/ip/firewall/filter", rulePath = "/routing/rule" + const routePaths = ["/ip/route", "/ipv6/route"], settingsPaths = ["/ip/settings", "/ipv6/settings"] + const base = () => ["a", "b", "c"].map(id => ({ ".id": `*${id}`, chain: "fixture", action: "accept", comment: id, disabled: "true" })) + const ruleBase = () => ["a", "b", "c"].map(id => ({ ".id": `*${id}`, action: "lookup", table: "main", comment: id, disabled: "true" })) + let catalogs: Record[]> = {}, mode = "normal", writes = 0, next = 0, polls = 0 + let connectionId: string | undefined + let afterIntent: (() => Promise) | undefined, afterVerified: (() => Promise) | undefined + let afterRouteRead: (() => Promise) | undefined + const query = pool.query.bind(pool) + const commands: Array<{ method: string; path: string }> = [] + const readPaths = new Set() + const reset = () => { + mode = "normal" + catalogs = { [filterPath]: base(), [rulePath]: ruleBase(), "/routing/table": [{ ".id": "*main", name: "main", fib: "true", disabled: "false" }], "/ip/settings": [{ "rp-filter": "no", "ipv4-multipath-hash-policy": "l3" }], "/ip/route": [] } + } + reset() + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")) + res.setHeader("content-type", "application/json") + if (req.method === "GET") { + polls++; readPaths.add(path) + const body = JSON.stringify(catalogs[path] ?? []) + if (path === "/ip/route" && afterRouteRead) { + const hook = afterRouteRead + afterRouteRead = undefined + await hook() + } + res.end(body) + return + } + const parent = [filterPath, rulePath, ...routePaths, ...settingsPaths].find(value => path === value || path.startsWith(`${value}/`)) + if (!parent) { res.statusCode = 404; res.end("{}"); return } + const singleton = settingsPaths.includes(parent) + const set = singleton && path === parent + "/set" && req.method === "POST" + const rowWrite = !singleton && (req.method === "PUT" ? path === parent : ["PATCH", "DELETE"].includes(req.method ?? "") && path.startsWith(parent + "/")) + if (!set && !rowWrite) { res.statusCode = 400; res.end("{}"); return } + let text = ""; for await (const chunk of req) text += chunk + const fields = text ? JSON.parse(text) as Record : {} + if (set) { + const keys = parent === "/ip/settings" ? ["rp-filter", "ipv4-multipath-hash-policy"] : ["multipath-hash-policy"] + if (!fields || !Object.keys(fields).length || Object.entries(fields).some(([key, value]) => !keys.includes(key) || typeof value !== "string")) { res.statusCode = 400; res.end("{}"); return } + } + const rows = catalogs[parent], id = req.method === "PUT" ? `*new${++next}` : path.slice(parent.length + 1) + writes++; commands.push({ method: req.method!, path }) + if (mode !== "ignored") { + if (req.method === "PUT") rows.push({ ".id": id, ...fields }) + if (set) Object.assign(rows[0], fields) + if (req.method === "PATCH") Object.assign(rows.find(row => row[".id"] === id)!, fields) + if (req.method === "DELETE") catalogs[parent] = rows.filter(row => row[".id"] !== id) + } + const after = catalogs[parent] + if (routePaths.includes(parent)) for (const row of after) row.active = Number(row.distance) === Math.min(...after.map(item => Number(item.distance))) ? "true" : "false" + if (mode === "two-new") after.push({ ".id": `*extra${next}`, ...fields }) + if (mode === "collateral-update") after.find(row => row[".id"] === "*b")!.comment = "external" + if (mode === "collateral-remove") catalogs[parent] = after.filter(row => row[".id"] !== "*b") + if (mode === "duplicate-id") after.push(structuredClone(after[0])) + if (mode === "missing-id") delete (after[0] as Record)[".id"] + if (mode === "reorder") after.reverse() + if (mode === "lost") { req.socket.destroy(); return } + // Model both acknowledged empty-body and RouterOS command responses. + if (set) { + if (parent === "/ip/settings") { res.writeHead(204); res.end() } + else res.end("[]") + return + } + res.end(JSON.stringify(req.method === "PUT" ? { ".id": mode === "ack-existing" ? "*a" : id, ...fields } : {})) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const wait = async (id: string) => { + for (const deadline = Date.now() + 45000; Date.now() < deadline;) { + const row = (await query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw Error("Conservation operation remains live; never replay") + } + // Hooks run AFTER durable intent / verified-step SQL, not at plan creation. + pool.query = (async (...args: unknown[]) => { + const value = await (query as (...values: unknown[]) => Promise)(...args) + const sql = args[0], params = args[1] as unknown[] | undefined + if (typeof sql === "string" && sql.startsWith("INSERT INTO mcp_audit") && params?.[1] === "configuration.intent" && String(params?.[3]).includes(`"serverId":${serverId}`) && afterIntent) { + const hook = afterIntent; afterIntent = undefined; await hook() + } + if (typeof sql === "string" && sql.startsWith("UPDATE mcp_operation_steps SET state=$3") && params?.[2] === "verified" && afterVerified) { + const hook = afterVerified; afterVerified = undefined; await hook() + } + return value + }) as typeof pool.query + try { + assert.equal((await query("SELECT count(*)::int AS n FROM servers WHERE id=$1", [serverId])).rows[0].n, 0) + await query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'native-conservation','127.0.0.1',$2,false,'fixture-only')", [serverId, (router.address() as { port: number }).port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Native conservation", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write"], scope: { serverIds: [serverId], operationIds: ["ros.ip_filter.create", "ros.ip_filter.update", "ros.ip_filter.delete", "ros.routing_rule.update", "ros.ipv4_route.update", "ros.ip_settings.update", "ros.ipv6_settings.update", "routing.ecmp.update", "routing.rpf.update", "operations.recover", "operations.reconcile", "operations.read"], allowGlobal: false }, limits: { callsPerMinute: 1000, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id + let principal = await authenticateMcpToken(access.token) + const prepare = (action: "create" | "update" | "delete", resource = "ip_filter") => invokeMcpTool(principal, `mm_${resource}_${action}_plan`, { serverId, ...(action === "create" ? {} : { id: "*a" }), fields: action === "create" ? { chain: "fixture", action: "accept", comment: "new", disabled: true } : action === "update" ? { comment: "changed" } : {} }) as Promise<{ planId: string }> + const evidence = async (id: string) => JSON.parse(await decryptSecret((await query("SELECT evidence FROM mcp_plans WHERE id=$1", [id])).rows[0].evidence.sealed)) as ChangeEvidence + const replace = async (id: string, value: ChangeEvidence) => { await query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [id, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(value)) })]) } + const apply = async (id: string) => { await approvePlan(id, actor, true); const key = randomUUID(), operation = await applyPlan(principal, id, key); return { key, operation, ...await wait(operation.id) } } + const noReplay = async (id: string, result: Awaited>, count: number) => { + const replay = await applyPlan(principal, id, result.key) + assert.equal(replay.id, result.operation.id); assert.equal(writes, count) + assert.equal((await applyPlan(principal, id, randomUUID())).id, result.operation.id) + assert.equal(writes, count) + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id })) + assert.equal(writes, count) + } + for (const action of ["create", "update", "delete"] as const) { + reset(); const plan = await prepare(action), before = writes, beforePolls = polls, result = await apply(plan.planId) + assert.equal(result.state, "applied", `normal ${action}`); assert.equal(result.verification, "passed"); assert.equal(writes, before + 1) + const sealed = await evidence(plan.planId) + assert.equal(polls - beforePolls, new Set(sealed.snapshots.map(snapshot => snapshot.resource)).size * 4 + 1, `normal ${action}: one sweep per execution boundary and changed-catalog readback`) + const observed = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean } + assert.equal(observed.automaticRetry, false); assert.equal(writes, before + 1) + if (action !== "delete") { + const inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id }) as { planId: string } + assert.equal((await apply(inverse.planId)).state, "applied") + assert.deepEqual(catalogs[filterPath], base(), `exact ${action} inverse`) + } else { + // Generic DELETE has no safe inverse: a new native ID is not the old identity. + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id })) + assert.deepEqual(catalogs[filterPath], base().slice(1)) + } + } + reset() + const finalDrift = await prepare("update"), beforeFinalDrift = writes + afterVerified = async () => { catalogs[filterPath][1].comment = "external-before-terminal" } + try { + const result = await apply(finalDrift.planId) + assert.equal(afterVerified, undefined) + assert.equal(result.error_code, "STATE_CONFLICT") + assert.notEqual(result.state, "applied") + assert.equal(writes, beforeFinalDrift + 1) + await noReplay(finalDrift.planId, result, writes) + } finally { + afterVerified = undefined + } + for (const action of ["create", "update", "delete"] as const) { + const faults = ["ignored", "lost", "collateral-update", "collateral-remove", "duplicate-id", "missing-id", "reorder", ...(action === "create" ? ["ack-existing", "two-new"] : [])] + for (const fault of faults) { + reset(); const plan = await prepare(action), before = writes; mode = fault + const result = await apply(plan.planId) + assert.notEqual(result.state, "applied", `${action}/${fault}`); assert.notEqual(result.verification, "passed", `${action}/${fault}`) + assert.equal(writes, before + 1, `${action}/${fault}: one diagnostic dispatch`) + await noReplay(plan.planId, result, writes) + assert.equal((await query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1 AND state='verified'", [result.operation.id])).rows[0].n, 0) + } + } + for (const fault of ["duplicate-id", "missing-id"]) { + reset(); const before = writes + if (fault === "duplicate-id") catalogs[filterPath].push(structuredClone(catalogs[filterPath][0])) + else delete (catalogs[filterPath][0] as Record)[".id"] + // Either preparation rejects an invalid catalog, or execution rejects + // the sealed invalid snapshot before its first native dispatch. + const plan = await prepare("update").catch(() => undefined) + if (plan) { const result = await apply(plan.planId); assert.notEqual(result.state, "applied"); assert.notEqual(result.verification, "passed") } + assert.equal(writes, before, `${fault} in original snapshot: zero writes`) + } + reset(); const reorderedRule = await prepare("update", "routing_rule"); mode = "reorder" + const ruleResult = await apply(reorderedRule.planId) + assert.notEqual(ruleResult.state, "applied"); assert.notEqual(ruleResult.verification, "passed") + await noReplay(reorderedRule.planId, ruleResult, writes) + + // Explicit sealed fixture: two valid native PATCHes share the real initial + // full-catalog snapshot. We do NOT fabricate an already-mutated prefix. + reset(); const multi = await prepare("update"), multiEvidence = await evidence(multi.planId) + const first = multiEvidence.steps[0]; assert.equal(first.kind, "ros") + if (first.kind !== "ros") throw Error("Expected native step") + multiEvidence.steps.push({ kind: "ros", step: { ...first.step, id: "*b", fields: { comment: "second" } } }) + multiEvidence.objects.push({ kind: "ip_filter", id: "*b", serverId }); multiEvidence.mutationCost = 2 + multiEvidence.warnings.push("Acceptance fixture: sealed two-step batch with full original snapshot") + await replace(multi.planId, multiEvidence) + const beforeMulti = writes, commandOffset = commands.length + afterVerified = async () => { catalogs[filterPath][1].comment = "external-before-second-intent" } + const stopped = await apply(multi.planId) + assert.equal(afterVerified, undefined, "first write must actually be verified before drift") + assert.notEqual(stopped.state, "applied"); assert.notEqual(stopped.verification, "passed"); assert.equal(writes, beforeMulti + 1) + assert.deepEqual(commands.slice(commandOffset), [{ method: "PATCH", path: `${filterPath}/*a` }]) + assert.deepEqual((await query("SELECT step_index,state FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [stopped.operation.id])).rows, [{ step_index: 0, state: "verified" }]) + await noReplay(multi.planId, stopped, writes) + + for (const drift of ["source", "epoch", "dependency", "protection", "policy"] as const) { + reset(); const plan = await prepare("update"), sealed = await evidence(plan.planId) + // A legitimate read dependency, sealed BEFORE dispatch. The unchanged + // table is part of the prefix and must be checked after native intent. + if (drift === "dependency") { + sealed.snapshots.push(await readRosSnapshot(serverId, "routing_table")) + sealed.objects.push({ kind: "routing_table", id: "*main", serverId }) + await replace(plan.planId, sealed) + } + const protection = randomUUID(), before = writes + let pollsAtIntent: number | undefined + afterIntent = async () => { + pollsAtIntent = polls + if (drift === "source") await query("UPDATE servers SET password='late-fixture-source' WHERE id=$1", [serverId]) + if (drift === "epoch") { + const previous = (await query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1", [serverId])).rows[0].routing_endpoint_epoch + await query("UPDATE servers SET password='epoch-aba-fixture' WHERE id=$1", [serverId]) + await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId]) + assert.notEqual((await query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1", [serverId])).rows[0].routing_endpoint_epoch, previous, "A -> B -> A must rotate the actual SQL endpoint epoch") + } + if (drift === "dependency") catalogs["/routing/table"][0].fib = "false" + if (drift === "protection") await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ip_filter", id: "*a", serverId, protection: "deny" }), actor.id]) + if (drift === "policy") await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{version}',to_jsonb((policy->>'version')::int+1)) WHERE id=$1", [connectionId]) + } + try { + const result = await apply(plan.planId) + console.log("Late native fence:", drift, { state: result.state, verification: result.verification, errorCode: result.error_code, dispatched: writes - before }) + assert.equal(afterIntent, undefined, `${drift}: durable native intent hook reached`) + assert.notEqual(result.state, "applied", `${drift}: late intent must stop execution`); assert.notEqual(result.verification, "passed"); assert.equal(writes, before, `${drift}: zero dispatches`) + if (drift === "source" || drift === "epoch") assert.equal(polls, pollsAtIntent, `${drift}: new source refused before native GET`) + await noReplay(plan.planId, result, writes) + } finally { + afterIntent = undefined + await query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId]) + principal = await authenticateMcpToken(access.token) + } + } + reset(); const obsolete = await prepare("update") + const currentVersion = findAction("mm_ip_filter_update_plan").operation.version + assert.ok(currentVersion > 1) + await query("UPDATE mcp_plans SET operation_version=$2 WHERE id=$1", [obsolete.planId, currentVersion - 1]) + await approvePlan(obsolete.planId, actor, true) + const beforeObsolete = writes + await assert.rejects(applyPlan(principal, obsolete.planId, randomUUID()), /устарели/) + assert.equal(writes, beforeObsolete) + reset() + const routeBaseline = [0, 1].map(index => ({ ".id": `*route${index}`, "dst-address": "192.0.2.0/24", "routing-table": "main", gateway: `198.18.0.${index + 1}`, "immediate-gw": `198.18.0.${index + 1}%fixture`, scope: "0", distance: String((index + 1) * 10), active: index === 0 ? "true" : "false", disabled: "false", dynamic: "false", comment: managedComment("conservation") })) + catalogs["/ip/route"] = structuredClone(routeBaseline) + const originalSettings = structuredClone(catalogs["/ip/settings"]) + const ecmpArgs = { serverId, family: "ipv4", routeIds: ["*route0", "*route1"], distance: 10, hashPolicy: "l4" } + const beforeEcmp = writes + const observer = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "ECMP interface-only preflight", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [serverId], operationIds: ["routing.ecmp.preflight.read"], allowGlobal: false } }), actor) + try { + const reader = await authenticateMcpToken(observer.token) + const { distance, hashPolicy, ...preflightInput } = ecmpArgs + catalogs["/ip/firewall/connection/tracking"] = [{ enabled: "auto" }] + catalogs["/ip/route"] = routeBaseline.map((row, index) => ({ ...row, gateway: `gre${index}`, "immediate-gw": `gre${index}` })) + catalogs["/interface"] = [0, 1].map(index => ({ ".id": `*gre${index}`, name: `gre${index}` })) + const before = polls + const result = await invokeMcpTool(reader, "mm_ecmp_preflight_get", preflightInput) as { configurationObserved: boolean; networkValidation: string; sources: { interfaces: { count: number } } } + assert.equal(result.configurationObserved, true) + assert.equal(result.networkValidation, "not-performed") + assert.equal(result.sources.interfaces.count, 2) + assert.equal(polls - before, 11) + assert.equal(writes, beforeEcmp) + } finally { + await query("DELETE FROM mcp_connections WHERE id=$1", [observer.connection.id]) + catalogs["/ip/route"] = structuredClone(routeBaseline) + } + catalogs["/ip/route"] = routeBaseline.map((row, index) => ({ ...row, gateway: `gre${index}`, "immediate-gw": `gre${index}` })) + const namedHopPlan = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string; changes: unknown } + assert.ok(!JSON.stringify(namedHopPlan.changes).includes("nextHopInterface")) + const inventory = structuredClone(catalogs["/interface"]) + afterIntent = async () => { catalogs["/interface"][0][".id"] = "*replacement" } + const replacedHop = await apply(namedHopPlan.planId) + assert.equal(afterIntent, undefined) + assert.equal(replacedHop.state, "rejected") + assert.equal(replacedHop.error_code, "STATE_CONFLICT") + assert.equal(writes, beforeEcmp) + catalogs["/interface"] = inventory + catalogs["/ip/route"] = structuredClone(routeBaseline) + console.log("ECMP interface-only next-hop: same-name replacement after intent refused before PATCH; native identity stays private") + afterRouteRead = async () => { catalogs["/ip/route"][1].distance = "25" } + await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" }) + assert.equal(afterRouteRead, undefined) + catalogs["/ip/route"] = structuredClone(routeBaseline) + afterRouteRead = async () => { + await query("UPDATE servers SET password='source-aba-fixture' WHERE id=$1", [serverId]) + await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId]) + } + await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" }) + assert.equal(afterRouteRead, undefined) + assert.equal(writes, beforeEcmp) + catalogs["/ip/route"][1]["immediate-gw"] = routeBaseline[0]["immediate-gw"] + await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" }) + assert.equal(writes, beforeEcmp) + catalogs["/ip/route"] = structuredClone(routeBaseline) + const fibTable = catalogs["/routing/table"] + for (const rows of [ + [{ ...fibTable[0], fib: "false" }], + [{ ...fibTable[0], fib: "" }], + [{ ".id": "*main", name: "main" }], + [{ ...fibTable[0], disabled: "true" }], + [{ ...fibTable[0], invalid: "true" }], + [...fibTable, { ...fibTable[0], ".id": "*duplicate" }], + ]) { + catalogs["/routing/table"] = rows + await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" }) + assert.equal(writes, beforeEcmp) + } + catalogs["/routing/table"] = fibTable + const staleFib = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + await approvePlan(staleFib.planId, actor, true) + catalogs["/routing/table"] = [{ ...fibTable[0], fib: "false" }] + assert.equal((await wait((await applyPlan(principal, staleFib.planId, randomUUID())).id)).state, "rejected") + assert.equal(writes, beforeEcmp) + catalogs["/routing/table"] = fibTable + console.log("ECMP FIB SQL/HTTP: inactive, unconfirmed and ambiguous tables refused; drift rejected before native write") + const equal = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + const beforeEqual: number = writes, equalResult = await apply(equal.planId) + assert.equal(equalResult.state, "applied"); assert.equal(writes, beforeEqual + 3) + assert.deepEqual(commands.at(-1), { method: "POST", path: "/ip/settings/set" }) + assert.equal(catalogs["/ip/settings"][0]["ipv4-multipath-hash-policy"], "l4") + const unequal = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: equalResult.operation.id }) as { planId: string } + assert.equal((await apply(unequal.planId)).state, "applied"); assert.equal(writes, beforeEqual + 6) + assert.deepEqual(commands[beforeEqual + 3], { method: "POST", path: "/ip/settings/set" }) + assert.deepEqual(catalogs["/ip/route"], routeBaseline); assert.deepEqual(catalogs["/ip/settings"], originalSettings) + const lateEcmp = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + afterIntent = async () => { catalogs["/ip/route"][1]["immediate-gw"] = "" } + const lateEcmpResult = await apply(lateEcmp.planId) + assert.equal(afterIntent, undefined) + assert.equal(lateEcmpResult.state, "rejected") + assert.equal(lateEcmpResult.error_code, "STATE_CONFLICT") + assert.equal(writes, beforeEqual + 6) + catalogs["/ip/route"] = structuredClone(routeBaseline) + const sourceChanged = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + let lateRouteReads = 0 + const rotateDuringHealthRead = async () => { + if (++lateRouteReads === 1) { + // The prefix reads the configuration first; the next GET checks the live next-hop. + afterRouteRead = rotateDuringHealthRead + return + } + await assert.rejects(updateServer(serverId, { password: "concurrent-update-fixture" }), { statusCode: 409 }) + await query("UPDATE servers SET password='health-source-aba-fixture' WHERE id=$1", [serverId]) + await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId]) + } + afterIntent = async () => { afterRouteRead = rotateDuringHealthRead } + const staleHealthSource = await apply(sourceChanged.planId) + assert.equal(lateRouteReads, 2) + assert.equal(staleHealthSource.state, "rejected") + assert.equal(staleHealthSource.error_code, "STATE_CONFLICT") + assert.equal(writes, beforeEqual + 6) + const beforeNextStep = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string } + afterVerified = async () => { catalogs["/ip/route"][1]["immediate-gw"] = "" } + const partialEcmp = await apply(beforeNextStep.planId) + assert.equal(partialEcmp.state, "partial") + assert.equal(writes, beforeEqual + 7) + for (const [result, confirmedWrites] of [[lateEcmpResult, 0], [partialEcmp, 1]] as const) { + const stored = await invokeMcpTool(principal, "mm_operation_get", { operationId: result.operation.id }) as { + state: string; result: { failedStepDispatched: boolean; confirmedWrites: number; needsReview: boolean } + } + assert.equal(stored.state, result.state) + assert.equal(stored.result.failedStepDispatched, false) + assert.equal(stored.result.confirmedWrites, confirmedWrites) + assert.equal(stored.result.needsReview, confirmedWrites > 0) + const { rows: [stop] } = await pool.query("SELECT payload FROM mcp_audit WHERE action='operation.stop' AND payload->>'operationId'=$1", [result.operation.id]) + assert.equal(stop.payload.failedStepDispatched, false) + } + catalogs["/ip/route"] = structuredClone(routeBaseline) + const collapsed = await invokeMcpTool(principal, "mm_ecmp_plan", { ...ecmpArgs, hashPolicy: undefined }) as { planId: string } + afterVerified = async () => { + afterVerified = async () => { catalogs["/ip/route"][1]["immediate-gw"] = routeBaseline[0]["immediate-gw"] } + } + const collapsedResult = await apply(collapsed.planId) + assert.equal(collapsedResult.state, "partial") + assert.equal(collapsedResult.verification, "failed") + assert.equal(writes, beforeEqual + 9) + assert.equal(afterVerified, undefined) + catalogs["/ip/route"] = structuredClone(routeBaseline) + console.log("ECMP SQL/HTTP: shared adjacency refuses preparation and post-write collapse fails verification") + const v6Baseline = routeBaseline.map((row, index) => ({ + ...row, "dst-address": "2001:db8::/64", gateway: `2001:db8:1::${index + 1}`, + "immediate-gw": `fe80::${index + 1}%fixture`, + })) + catalogs["/ipv6/route"] = structuredClone(v6Baseline) + const v6Settings = [{ "multipath-hash-policy": "l3" }] + catalogs["/ipv6/settings"] = structuredClone(v6Settings) + const v6Mangle = "/ipv6/firewall/mangle" + catalogs[v6Mangle] = [{ ".id": "*v6mark", action: "mark-routing", disabled: "false", "new-routing-mark": "NEVER_NATIVE_POLICY_MARK", bytes: "0", packets: "0" }] + const v6Args = { ...ecmpArgs, family: "ipv6" } + readPaths.clear() + for (const [path, field] of [[v6Mangle, "new-routing-mark"], [rulePath, "min-prefix"]]) { + const stale = await invokeMcpTool(principal, "mm_ecmp_plan", v6Args) as { planId: string; changes: { snapshots: Record[] } } + const policy = stale.changes.snapshots.find(snapshot => snapshot.resource === "routing_policy:ipv6_mangle") + assert.deepEqual(policy, { serverId, resource: "routing_policy:ipv6_mangle", rowCount: 1 }) + assert.ok(!JSON.stringify(stale).includes("NEVER_NATIVE_POLICY_MARK")) + await approvePlan(stale.planId, actor, true) + const original = catalogs[path], before = writes + catalogs[path] = [{ ...original[0], [field]: "17" }, ...original.slice(1)] + assert.equal((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "rejected") + assert.equal(writes, before) + catalogs[path] = original + } + const v6Plan = await invokeMcpTool(principal, "mm_ecmp_plan", v6Args) as { planId: string } + catalogs[v6Mangle][0].bytes = "1500" + catalogs[v6Mangle][0].packets = "1" + const v6Result = await apply(v6Plan.planId) + assert.equal(v6Result.state, "applied") + assert.deepEqual(commands.at(-1), { method: "POST", path: "/ipv6/settings/set" }) + assert.ok(catalogs["/ipv6/route"].every(row => row.active === "true" && row.distance === "10")) + assert.equal(catalogs["/ipv6/settings"][0]["multipath-hash-policy"], "l4") + const inverseArgs = { operationId: v6Result.operation.id } + catalogs[v6Mangle][0]["new-routing-mark"] = "external-mark" + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", inverseArgs), { code: "STATE_CONFLICT" }) + catalogs[v6Mangle][0]["new-routing-mark"] = "NEVER_NATIVE_POLICY_MARK" + const v6Inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseArgs) as { planId: string } + assert.ok(!JSON.stringify(v6Inverse).includes("NEVER_NATIVE_POLICY_MARK")) + const v6InverseOffset = commands.length + assert.equal((await apply(v6Inverse.planId)).state, "applied") + assert.deepEqual(commands[v6InverseOffset], { method: "POST", path: "/ipv6/settings/set" }) + assert.deepEqual(catalogs["/ipv6/route"], v6Baseline) + assert.deepEqual(catalogs["/ipv6/settings"], v6Settings) + assert.ok(![...readPaths].some(path => path === "/ip/settings" || path.startsWith("/ip/firewall/"))) + console.log("ECMP IPv6 SQL/HTTP: family-specific reads, policy drift refusal, route/hash apply and inverse") + const rpfArgs = { serverId, mode: "strict", acknowledgeAsymmetry: true } + const manglePath = "/ip/firewall/mangle" + const beforeRpf = writes, beforeIpv6 = polls + await assert.rejects(invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, family: "ipv6" }), { code: "UNSUPPORTED_CAPABILITY" }) + assert.equal(polls, beforeIpv6) + catalogs[rulePath][0].disabled = "false" + await assert.rejects(invokeMcpTool(principal, "mm_rpf_plan", rpfArgs), { code: "STATE_CONFLICT" }) + const strictSettings = { serverId, fields: { "rp-filter": "strict", "ipv4-multipath-hash-policy": "l4" } } + await assert.rejects(invokeMcpTool(principal, "mm_ip_settings_update_plan", strictSettings), { code: "STATE_CONFLICT" }) + catalogs[rulePath][0].disabled = "true" + catalogs[manglePath] = [{ ".id": "*mark", chain: "prerouting", action: "mark-routing", disabled: "false", "new-routing-mark": "fixture-mark" }] + await assert.rejects(invokeMcpTool(principal, "mm_rpf_plan", rpfArgs), { code: "STATE_CONFLICT" }) + const loose = await invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, mode: "loose" }) as { changes: { warnings: string[] } } + assert.ok(loose.changes.warnings.some(warning => warning.includes("policy routing"))) + catalogs[manglePath][0].disabled = "true" + const strict = await invokeMcpTool(principal, "mm_rpf_plan", rpfArgs) as { planId: string } + const genericStrict = await invokeMcpTool(principal, "mm_ip_settings_update_plan", strictSettings) as { planId: string } + await approvePlan(strict.planId, actor, true) + await approvePlan(genericStrict.planId, actor, true) + catalogs[manglePath][0]["new-routing-mark"] = "changed-after-approval" + const staleRpf = await applyPlan(principal, strict.planId, randomUUID()) + assert.equal((await wait(staleRpf.id)).state, "rejected") + assert.equal((await wait((await applyPlan(principal, genericStrict.planId, randomUUID())).id)).state, "rejected") + assert.equal(writes, beforeRpf) + catalogs[manglePath] = [] + catalogs["/ip/settings"][0]["rp-filter"] = "loose" + const disableRpf = await invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, mode: "disabled" }) as { planId: string } + assert.equal((await apply(disableRpf.planId)).state, "applied") + assert.deepEqual(catalogs["/ip/settings"], originalSettings) + catalogs["/ip/settings"][0]["rp-filter"] = "strict" + const leaveStrict = await invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, mode: "no" }) as { planId: string } + const leftStrict = await apply(leaveStrict.planId) + assert.equal(leftStrict.state, "applied") + const inverseRpfArgs = { operationId: leftStrict.operation.id } + const beforeInverseRpf = writes + catalogs[manglePath] = [{ ".id": "*late-mark", action: "mark-routing", disabled: "false" }] + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs), { code: "STATE_CONFLICT" }) + catalogs[manglePath] = [] + const legacyRpf = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs) as { planId: string } + const legacyRpfEvidence = await evidence(legacyRpf.planId) + legacyRpfEvidence.snapshots = legacyRpfEvidence.snapshots.filter(snapshot => !snapshot.resource.startsWith("routing_policy:")) + await replace(legacyRpf.planId, legacyRpfEvidence) + await approvePlan(legacyRpf.planId, actor, true) + await assert.rejects(applyPlan(principal, legacyRpf.planId, randomUUID()), { code: "PLAN_EXPIRED" }) + const staleInverseRpf = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs) as { planId: string } + await approvePlan(staleInverseRpf.planId, actor, true) + catalogs[manglePath] = [{ ".id": "*late-mark", action: "mark-routing", disabled: "false" }] + assert.equal((await wait((await applyPlan(principal, staleInverseRpf.planId, randomUUID())).id)).state, "rejected") + assert.equal(writes, beforeInverseRpf) + catalogs[manglePath] = [] + const safeInverseRpf = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs) as { planId: string } + assert.equal((await apply(safeInverseRpf.planId)).state, "applied") + assert.equal(catalogs["/ip/settings"][0]["rp-filter"], "strict") + catalogs["/ip/settings"] = structuredClone(originalSettings) + console.log("RPF inverse SQL/HTTP: active policy, legacy plans and post-approval drift refused; exact strict restoration under verified policy") + console.log("RPF SQL/HTTP: explicit IPv6 refusal, strict policy admission and drift, loose warning, disabled alias") + for (const [tool, args, path] of [ + ["mm_rpf_plan", { serverId, mode: "loose", acknowledgeAsymmetry: true }, "/ip/settings"], + ["mm_ip_settings_update_plan", { serverId, fields: { "rp-filter": "loose" } }, "/ip/settings"], + ["mm_ipv6_settings_update_plan", { serverId, fields: { "multipath-hash-policy": "l4" } }, "/ipv6/settings"], + ["mm_ipv4_route_update_plan", { serverId, id: "*route0", fields: { scope: 30 } }, "/ip/route"], + ] as const) { + const baseline = structuredClone(catalogs[path]), commandOffset = commands.length + const forward = await invokeMcpTool(principal, tool, args) as { planId: string }, result = await apply(forward.planId) + assert.equal(result.state, "applied", tool) + const inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id }) as { planId: string } + assert.equal((await apply(inverse.planId)).state, "applied", tool) + assert.deepEqual(catalogs[path], baseline, `${tool}: exact singleton/zero inverse`) + if (settingsPaths.includes(path)) assert.deepEqual(commands.slice(commandOffset), [ + { method: "POST", path: path + "/set" }, { method: "POST", path: path + "/set" }, + ]) + } + for (const fault of ["ignored", "lost"]) { + const forward = await invokeMcpTool(principal, "mm_ip_settings_update_plan", { serverId, fields: { "rp-filter": "loose" } }) as { planId: string } + const commandOffset = commands.length + mode = fault + try { + const result = await apply(forward.planId) + assert.notEqual(result.state, "applied") + assert.notEqual(result.verification, "passed") + assert.deepEqual(commands.slice(commandOffset), [{ method: "POST", path: "/ip/settings/set" }]) + const observed = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean } + assert.equal(observed.automaticRetry, false) + await noReplay(forward.planId, result, commandOffset + 1) + } finally { + mode = "normal" + catalogs["/ip/settings"] = structuredClone(originalSettings) + } + } + catalogs[filterPath] = base().map(row => ({ ...row, comment: "", log: "false" })) + const booleanBaseline = structuredClone(catalogs[filterPath]) + const booleanPlan = await invokeMcpTool(principal, "mm_ip_filter_update_plan", { serverId, id: "*a", fields: { comment: "changed", log: true } }) as { planId: string } + const booleanResult = await apply(booleanPlan.planId); assert.equal(booleanResult.state, "applied") + const booleanInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: booleanResult.operation.id }) as { planId: string } + assert.equal((await apply(booleanInverse.planId)).state, "applied"); assert.deepEqual(catalogs[filterPath], booleanBaseline) + catalogs["/ip/route"] = routeBaseline.map((row, index) => ({ ...row, gateway: `gre${index}`, "immediate-gw": `gre${index}` })) + const stableHopPlan = await invokeMcpTool(principal, "mm_ecmp_plan", { ...ecmpArgs, hashPolicy: undefined }) as { planId: string } + const beforeStableHops = writes + const stableHops = await apply(stableHopPlan.planId) + assert.equal(stableHops.state, "applied") + assert.equal(writes, beforeStableHops + 2) + assert.ok(catalogs["/ip/route"].every(row => row.distance === "10" && row.active === "true")) + console.log("ECMP interface-only next-hop: unchanged interface IDs verified before both writes and after apply") + const namedHopInverseArgs = { operationId: stableHops.operation.id } + const plansBeforeReplacement = (await query("SELECT count(*)::int AS count FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].count + catalogs["/interface"][0][".id"] = "*replaced-before-preparation" + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs), { code: "STATE_CONFLICT" }) + assert.equal((await query("SELECT count(*)::int AS count FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].count, plansBeforeReplacement) + catalogs["/interface"][0][".id"] = "*gre0" + const oldHopInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs) as { planId: string } + const oldHopEvidence = await evidence(oldHopInverse.planId) + delete oldHopEvidence.healthChecks + await replace(oldHopInverse.planId, oldHopEvidence) + const beforeInverseHops = writes + const oldHopResult = await apply(oldHopInverse.planId) + assert.equal(oldHopResult.state, "rejected") + assert.equal(oldHopResult.error_code, "PLAN_EXPIRED") + assert.equal(writes, beforeInverseHops) + const staleHopInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs) as { planId: string; changes: unknown } + assert.ok(!JSON.stringify(staleHopInverse.changes).includes("nextHopInterface")) + afterIntent = async () => { catalogs["/interface"][0][".id"] = "*inverse-replacement" } + const refusedInverse = await apply(staleHopInverse.planId) + assert.equal(afterIntent, undefined) + assert.equal(refusedInverse.state, "rejected") + assert.equal(refusedInverse.error_code, "STATE_CONFLICT") + assert.equal(writes, beforeInverseHops) + catalogs["/interface"][0][".id"] = "*gre0" + const safeHopInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs) as { planId: string } + assert.equal((await apply(safeHopInverse.planId)).state, "applied") + assert.equal(writes, beforeInverseHops + 2) + assert.deepEqual(catalogs["/ip/route"].map(row => row.distance), ["10", "20"]) + console.log("ECMP inverse: replaced interface refused before PATCH; intact interface restores unequal costs") + console.log("Native multi-catalog inverse SQL/HTTP: two route PATCHes plus singleton hash policy, exact reverse prefix, RPF/raw singleton, numeric zero, false and empty-comment originals preserved") + console.log("Native conservation SQL/HTTP: C/U/D and safe inverses; assigned-ID, membership, field and order failures never VERIFIED/replayed; honest two-step prefix stops before second intent; late source/dependency/protection/policy fence zero writes; stale operation revision refused. No CHR/opaque-field/forwarding claim.") + } finally { + pool.query = query as typeof pool.query + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await query("DELETE FROM mcp_protections WHERE object_ref->>'serverId'=$1", [String(serverId)]) + await query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/native-conservation-http.test.ts b/backend/src/modules/mcp/native-conservation-http.test.ts new file mode 100644 index 00000000..e1c24c97 --- /dev/null +++ b/backend/src/modules/mcp/native-conservation-http.test.ts @@ -0,0 +1,84 @@ +import assert from "node:assert/strict" +import { createServer, request } from "node:http" +import { pool } from "../../db/index.js" +import { configurationRows, resourceById, type RosSnapshot, type RosStep } from "./resources.js" +import { fingerprint } from "./security.js" +import { verifyRosStepSnapshots } from "./ros-step-verification.js" + +// Real loopback framing/response-loss test of the pure projected verifier. This +// deliberately does not invoke admission, SQL checkpoints or real RouterOS. +const originalQuery = pool.query, originalConnect = pool.connect +let sql = 0 +const forbidden = () => { sql++; throw Error("HTTP model must not access PostgreSQL") } +pool.query = forbidden as typeof pool.query; pool.connect = forbidden as typeof pool.connect +type Row = Record +let rows: Row[] = [], mode = "normal", writes = 0, next = 0 +const serverId = 198 +const base = (resource: string): Row[] => ["a", "b", "c"].map(id => ({ ".id": `*${id}`, ...(resource === "ip_filter" ? { chain: "fixture", action: "accept" } : { action: "lookup", table: "main" }), comment: id, disabled: "true" })) +const router = createServer(async (req, res) => { + res.setHeader("content-type", "application/json") + if (req.method === "GET") { res.end(JSON.stringify(rows)); return } + let body = ""; for await (const chunk of req) body += chunk + const fields = body ? JSON.parse(body) as Row : {}, id = req.method === "PUT" ? `*new${++next}` : decodeURIComponent((req.url ?? "").split("/").at(-1)!) + writes++ + if (mode !== "ignored") { + if (req.method === "PUT") rows.push({ ".id": id, ...fields }) + if (req.method === "PATCH") Object.assign(rows.find(row => row[".id"] === id)!, fields) + if (req.method === "DELETE") rows = rows.filter(row => row[".id"] !== id) + } + if (mode === "two-new") rows.push({ ".id": `*extra${next}`, ...fields }) + if (mode === "collateral") rows.find(row => row[".id"] === "*b")!.comment = "external" + if (mode === "unsupplied") rows.find(row => row[".id"] === id)!.disabled = "false" + if (mode === "removed-neighbor") rows = rows.filter(row => row[".id"] !== "*b") + if (mode === "duplicate") rows.push(structuredClone(rows[0])) + if (mode === "missing") delete (rows[0] as Record)[".id"] + if (mode === "reorder") rows.reverse() + if (mode === "lost") { req.socket.destroy(); return } + res.end(JSON.stringify(req.method === "PUT" ? { ".id": mode === "existing" ? "*a" : id } : {})) +}) +await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) +const port = (router.address() as { port: number }).port +const http = (method: string, path: string, body?: unknown) => new Promise((resolve, reject) => { + const payload = body === undefined ? undefined : JSON.stringify(body) + const req = request({ hostname: "127.0.0.1", port, method, path, agent: false, ...(payload === undefined ? {} : { headers: { "content-type": "application/json", "content-length": Buffer.byteLength(payload) } }) }, res => { + let text = ""; res.on("data", chunk => { text += chunk }); res.once("error", reject) + res.once("end", () => { try { resolve(JSON.parse(text)) } catch (error) { reject(error) } }) + }) + req.once("error", reject); req.setTimeout(3000, () => req.destroy(Error("Loopback timeout"))) + req.end(payload) +}) +const snapshot = async (resource: string): Promise => { + const projected = configurationRows(resourceById(resource), await http("GET", resourceById(resource).path)) + return { serverId, resource, rows: projected, fingerprint: fingerprint(projected) } +} +try { + let cases = 0 + for (const resource of ["ip_filter", "routing_rule"]) { + for (const action of ["create", "update", "delete"] as const) { + const faults = ["normal", "ignored", "lost", "collateral", "removed-neighbor", "duplicate", "missing", "reorder", ...(action === "create" ? ["existing", "two-new"] : action === "update" ? ["unsupplied"] : [])] + for (const fault of faults) { + rows = base(resource); mode = fault + const fields: Record = action === "create" ? { ...(resource === "ip_filter" ? { chain: "fixture", action: "accept" } : { action: "lookup", table: "main" }), comment: "new", disabled: "true" } : action === "update" ? { comment: "changed" } : {} + const step: RosStep = { serverId, resource, action, fields, ...(action === "create" ? {} : { id: "*a" }) } + const before = await snapshot(resource), count = writes + let acknowledged = false, result: unknown + try { result = await http(action === "create" ? "PUT" : action === "update" ? "PATCH" : "DELETE", `${resourceById(resource).path}${action === "create" ? "" : "/*a"}`, fields); acknowledged = true } + catch (error) { assert.equal(fault, "lost", `${resource}/${action}/${fault}: ${error instanceof Error ? error.message : "transport error"}`) } + const after = await snapshot(resource) + // Snapshot equality cannot manufacture an acknowledgement after loss. + const verified = acknowledged && verifyRosStepSnapshots(step, result, before, after) + assert.equal(verified, fault === "normal", `${resource}/${action}/${fault}`) + assert.equal(writes, count + 1) + // Diagnostic reads and repeated verification never dispatch a retry. + verifyRosStepSnapshots(step, result, before, await snapshot(resource)) + assert.equal(writes, count + 1); cases++ + } + } + } + assert.equal(sql, 0) + console.log(`Native conservation HTTP: ${cases} C/U/D cases, assigned IDs, exact membership/fields/order, lost ACK remains unconfirmed, no diagnostic replay; zero PostgreSQL access`) +} finally { + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + pool.query = originalQuery; pool.connect = originalConnect + await pool.end() +} diff --git a/backend/src/modules/mcp/native-conservation-sql.test.ts b/backend/src/modules/mcp/native-conservation-sql.test.ts new file mode 100644 index 00000000..23737238 --- /dev/null +++ b/backend/src/modules/mcp/native-conservation-sql.test.ts @@ -0,0 +1,19 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-native-conservation-acceptance-secret" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const actor = { id: "native-conservation-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await updateMcpSettings({ enabled: true }, actor) + await (await import("./native-conservation-acceptance.js")).acceptNativeConservation(actor) + await (await import("./native-create-id-acceptance.js")).acceptNativeCreateId(actor) +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/native-create-id-acceptance.ts b/backend/src/modules/mcp/native-create-id-acceptance.ts new file mode 100644 index 00000000..716f9564 --- /dev/null +++ b/backend/src/modules/mcp/native-create-id-acceptance.ts @@ -0,0 +1,112 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../../lib/permissions.js" +import { pool } from "../../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import { decryptSecret, encryptSecret } from "./security.js" +import type { ChangeEvidence } from "./action-types.js" + +/** Owned SQL and local HTTP only; no claim about RouterOS native behavior. */ +export async function acceptNativeCreateId(actor: AuthUser) { + const serverId = 199 + const old = { ".id": "*old", chain: "fixture", action: "accept", comment: "same", disabled: "true" } + let rows: Record[] = [structuredClone(old)], mode = "normal", reads = 0, writes = 0, next = 1 + const commands: { method: string; id?: string }[] = [] + const router = createServer(async (req, res) => { + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")) + res.setHeader("content-type", "application/json") + if (req.method === "GET" && path === "/ip/firewall/filter") { reads++; res.end(JSON.stringify(rows)); return } + if (req.method === "PUT" && path === "/ip/firewall/filter") { + let text = ""; for await (const chunk of req) text += chunk + const fields = JSON.parse(text) as Record, id = `*new${next++}` + writes++; commands.push({ method: "PUT", id }) + if (mode !== "ignored") rows.push({ ".id": id, ...fields }) + if (mode === "lost") { req.socket.destroy(); return } + res.end(JSON.stringify({ ".id": id, ...fields })); return + } + if (req.method === "DELETE" && path.startsWith("/ip/firewall/filter/")) { + const id = path.slice("/ip/firewall/filter/".length) + writes++; commands.push({ method: "DELETE", id }); rows = rows.filter(row => row[".id"] !== id) + res.end("{}"); return + } + res.statusCode = 404; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined + const wait = async (id: string) => { + for (const deadline = Date.now() + 45000; Date.now() < deadline;) { + const row = (await pool.query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw Error("Native create ID operation remains live; do not replay") + } + try { + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=$1", [serverId])).rows[0].n, 0) + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'native-create-id','127.0.0.1',$2,false,'fixture-only')", [serverId, (router.address() as { port: number }).port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Native CREATE ID", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: ["ros.ip_filter.create", "operations.recover", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 240, mutationsPerHour: 1000 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const input = { serverId, fields: { chain: "fixture", action: "accept", comment: "same", disabled: true } } + const prepare = () => invokeMcpTool(principal, "mm_ip_filter_create_plan", input) as Promise<{ planId: string; state: string }> + const evidence = async (id: string) => JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [id])).rows[0].evidence.sealed)) as ChangeEvidence + const replaceEvidence = async (id: string, value: ChangeEvidence) => pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [id, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(value)) })]) + const apply = async (id: string) => { await approvePlan(id, actor, true); const key = randomUUID(), operation = await applyPlan(principal, id, key); return { key, operation, ...await wait(operation.id) } } + const requestsBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_requests")).rows[0].n + for (const id of ["*old", "*forged", undefined]) await assert.rejects(invokeMcpTool(principal, "mm_ip_filter_create_plan", { ...input, id })) + assert.equal(reads, 0); assert.equal(writes, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests")).rows[0].n, requestsBefore) + + const plan = await prepare(), success = await apply(plan.planId) + assert.equal(success.state, "applied"); assert.equal(success.verification, "passed") + const createdId = rows.find(row => row[".id"] !== "*old")![".id"] + assert.deepEqual(rows[0], old); assert.equal(writes, 1) + const inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: success.operation.id }) as { planId: string } + const inverseEvidence = await evidence(inverse.planId) + assert.equal(inverseEvidence.steps.length, 1) + assert.deepEqual(inverseEvidence.steps[0], { kind: "ros", step: { serverId, resource: "ip_filter", action: "delete", id: createdId, fields: {} } }) + assert.equal((await apply(inverse.planId)).state, "applied") + assert.deepEqual(rows, [old]); assert.deepEqual(commands, [{ method: "PUT", id: createdId }, { method: "DELETE", id: createdId }]) + + const poisonedPlan = await prepare(), poisonedEvidence = await evidence(poisonedPlan.planId) + assert.equal(poisonedEvidence.steps[0].kind, "ros") + if (poisonedEvidence.steps[0].kind === "ros") poisonedEvidence.steps[0].step.id = "*old" + await replaceEvidence(poisonedPlan.planId, poisonedEvidence) + const beforePoison = writes, poisoned = await apply(poisonedPlan.planId) + assert.notEqual(poisoned.state, "applied"); assert.notEqual(poisoned.verification, "passed"); assert.equal(writes, beforePoison) + + const secondPlan = await prepare(), second = await apply(secondPlan.planId) + assert.equal(second.state, "applied") + const legacy = await evidence(secondPlan.planId) + if (legacy.steps[0].kind === "ros") legacy.steps[0].step.id = "*old" + await replaceEvidence(secondPlan.planId, legacy) + const readsBeforeRecovery = reads, writesBeforeRecovery = writes + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: second.operation.id })) + assert.equal(reads, readsBeforeRecovery); assert.equal(writes, writesBeforeRecovery) + assert.deepEqual(rows[0], old) + const reconcile = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: second.operation.id }) + assert.ok(reconcile); assert.equal(writes, writesBeforeRecovery) + + const obsolete = await prepare() + await pool.query("UPDATE mcp_plans SET operation_version=1 WHERE id=$1", [obsolete.planId]) + await approvePlan(obsolete.planId, actor, true) + await assert.rejects(applyPlan(principal, obsolete.planId, randomUUID()), /устарели/); assert.equal(writes, writesBeforeRecovery) + for (const fault of ["ignored", "lost"]) { + mode = fault + const faultPlan = await prepare(), before: number = writes, result = await apply(faultPlan.planId) + assert.equal(result.state, fault === "lost" ? "unknown" : "partial"); assert.equal(writes, before + 1) + await applyPlan(principal, faultPlan.planId, result.key); assert.equal(writes, before + 1) + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id })) + assert.deepEqual(rows[0], old) + } + console.log("Native CREATE ID SQL/HTTP: caller ID refused before request/SQL read/native poll; assigned ID verified and inverse deletes only new object, legacy poisoned apply/recovery blocked, old versions refused, read-only reconcile retained, ignored/lost no replay; projected conservation/order remain separate") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/native-create-id-http.test.ts b/backend/src/modules/mcp/native-create-id-http.test.ts new file mode 100644 index 00000000..db2826ef --- /dev/null +++ b/backend/src/modules/mcp/native-create-id-http.test.ts @@ -0,0 +1,18 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-native-create-id-acceptance-secret" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const actor = { id: "native-create-id-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await updateMcpSettings({ enabled: true }, actor) + await (await import("./native-create-id-acceptance.js")).acceptNativeCreateId(actor) +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/native-create-identity.test.ts b/backend/src/modules/mcp/native-create-identity.test.ts new file mode 100644 index 00000000..74903498 --- /dev/null +++ b/backend/src/modules/mcp/native-create-identity.test.ts @@ -0,0 +1,106 @@ +import assert from "node:assert/strict" +import { pool } from "../../db/index.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { findAction } from "./actions.js" +import { verifyStep } from "./runtime.js" +import { executeRosStep, resourceById, rosResources, validateRosStep, type RosStep } from "./resources.js" +import { acknowledgedCreateId, assertNoCallerCreateId, recoveryRosStepId } from "./native-create-identity.js" +import { McpError } from "./security.js" + +const creates = rosResources.filter(resource => !resource.readOnly && resource.create) +assert.equal(creates.length, 30) +for (const resource of creates) { + const action = findAction(`mm_${resource.id}_create_plan`) + assert.equal(action.operation.version, resource.id === "bfd_configuration" ? 8 : resource.id === "ospf_template" ? 11 : ["ipsec_peer", "vrf", "ospf_instance"].includes(resource.id) ? 5 : ["wireguard", "bridge", "bridge_port", "bridge_vlan"].includes(resource.id) ? 4 : 3) + assert.equal(action.schema.safeParse({ serverId: 1, fields: {} }).success, true) + for (const id of ["*OLD", undefined, null, ""]) { + const rejected = action.schema.safeParse({ serverId: 1, fields: {}, id }) + assert.equal(rejected.success, false, `${resource.id}: explicit id must fail even if undefined`) + } +} + +const invalid = (error: unknown) => error instanceof McpError && error.code === "INVALID_INPUT" +const bfdCreate = findAction("mm_bfd_configuration_create_plan").schema +const bfdConfig = resourceById("bfd_configuration") +assert.deepEqual(validateRosStep(bfdConfig, "create", { "address-list": "peers", vrf: "main" }), { "address-list": "peers", vrf: "main" }) +assert.throws(() => validateRosStep(bfdConfig, "create", { vrf: "main" }), invalid) +assert.deepEqual(validateRosStep(bfdConfig, "create", { interfaces: "bridge1", vrf: "main" }), { interfaces: "bridge1", vrf: "main" }) +for (const interfaces of ["all", "none", "bridge1,all", "bridge1,"]) assert.throws(() => validateRosStep(bfdConfig, "create", { interfaces, vrf: "main" }), invalid) +for (const [addresses, expected] of [["192.0.2.9/24", "192.0.2.0/24"], ["FD00::20/64", "fd00::/64"], ["192.0.2.9/32", "192.0.2.9/32"], ["fd00::20/128", "fd00::20/128"]]) { + assert.deepEqual(validateRosStep(bfdConfig, "update", { addresses }, "*A"), { addresses: expected }) +} +for (const addresses of ["0.0.0.0/0", "::/0", "192.0.2.1", "fd00::/129", "192.0.2.1/32/extra", "192.0.2.0/24,192.0.3.0/24", "fe80::%ether1/64"]) assert.equal(bfdConfig.fields.safeParse({ addresses }).success, false) +assert.deepEqual(validateRosStep(bfdConfig, "update", { "min-rx": "200ms", "min-tx": "1000ms" }, "*A"), { "min-rx": "200ms", "min-tx": "1s" }) +for (const interval of ["0ms", "60001ms", "61s", "100", "1ms;", "0.2s"]) assert.equal(bfdConfig.fields.safeParse({ "min-rx": interval }).success, false) +assert.equal(bfdCreate.safeParse({ serverId: 1, fields: {}, placeBeforeId: "*A" }).success, true) +assert.equal(bfdCreate.safeParse({ serverId: 1, fields: {}, placeBeforeId: "0" }).success, false) +assert.equal(findAction("mm_bfd_configuration_update_plan").schema.safeParse({ serverId: 1, id: "*B", fields: {}, placeBeforeId: "*A" }).success, false) +const base: RosStep = { serverId: 1, resource: "ip_filter", action: "create", fields: { chain: "forward", action: "accept" } } +for (const id of ["*OLD", "", null, 1]) { + assert.throws(() => assertNoCallerCreateId({ action: "create", id }), invalid) + assert.throws(() => validateRosStep(resourceById("ip_filter"), "create", base.fields, id as string), invalid) + assert.throws(() => recoveryRosStepId({ action: "create", id }, [], []), invalid) +} +assert.deepEqual(validateRosStep(resourceById("ip_filter"), "create", base.fields), base.fields) +const ospfInstance = resourceById("ospf_instance") +const filterBinding = findAction("mm_ospf_filter_binding_plan") +assert.deepEqual(filterBinding.operation.requiredPermissions, ["mm:network:write", "mm:filters:read"]) +assert.equal(filterBinding.schema.safeParse({ serverId: 1, id: "*A", fields: {} }).success, false) +assert.equal(findAction("mm_ospf_instance_update_plan").schema.safeParse({ serverId: 1, id: "*A", fields: { "out-filter-chain": "test" } }).success, false) +assert.deepEqual(validateRosStep(ospfInstance, "update", { "router-id": "Edge.Id" }, "*A"), { "router-id": "Edge.Id" }) +assert.deepEqual(validateRosStep(ospfInstance, "update", { "router-id": "192.0.2.1" }, "*A"), { "router-id": "192.0.2.1" }) +for (const id of ["2001:db8::1", "192.0.2.999", "*A"]) assert.equal(ospfInstance.fields.safeParse({ "router-id": id }).success, false) +assert.deepEqual(validateRosStep(ospfInstance, "update", { redistribute: "static, connected", "originate-default": "if-installed" }, "*A"), { redistribute: "connected,static", "originate-default": "if-installed" }) +assert.deepEqual(validateRosStep(ospfInstance, "update", { redistribute: "", "originate-default": "never" }, "*A"), { redistribute: "", "originate-default": "never" }) +for (const redistribute of ["static,static", "unknown", "static,", " "]) assert.equal(ospfInstance.fields.safeParse({ redistribute }).success, false) +const ospf = resourceById("ospf_template") +assert.deepEqual(validateRosStep(ospf, "create", { area: "lab", networks: "198.18.1.2/24, FD00:0:0:0::/64" }), { area: "lab", networks: "198.18.1.0/24,fd00::/64" }) +assert.deepEqual(validateRosStep(ospf, "create", { area: "lab", interfaces: "bridge1" }), { area: "lab", interfaces: "bridge1" }) +assert.deepEqual(validateRosStep(ospf, "create", { area: "lab", interfaces: "bridge1", type: "nbma", priority: 0 }), { area: "lab", interfaces: "bridge1", type: "nbma", priority: "0" }) +assert.deepEqual(validateRosStep(ospf, "update", { type: "ptmp-broadcast", priority: 255 }, "*A"), { type: "ptmp-broadcast", priority: "255" }) +assert.deepEqual(validateRosStep(ospf, "update", { "instance-id": 0 }, "*A"), { "instance-id": "0" }) +for (const invalidId of [-1, 256, 1.5, "0"]) assert.equal(ospf.fields.safeParse({ "instance-id": invalidId }).success, false) +assert.deepEqual(validateRosStep(ospf, "update", { "retransmit-interval": "1m", "transmit-delay": "00:00:01" }, "*A"), { "retransmit-interval": "60s", "transmit-delay": "1s" }) +for (const value of ["0s", "1ms", "5", "1s 2s"]) { + assert.throws(() => validateRosStep(ospf, "update", { "transmit-delay": value }, "*A")) +} +for (const priority of [-1, 256, 1.5, "0"]) assert.equal(ospf.fields.safeParse({ priority }).success, false) +assert.throws(() => validateRosStep(ospf, "create", { area: "lab" }), invalid) +for (const networks of ["", "bad/24", "192.0.2.1", "192.0.2.0/33", "fe80::%ether1/64", "192.0.2.0/24,192.0.2.1/24"]) { + assert.equal(ospf.fields.safeParse({ networks }).success, false) +} +assertNoCallerCreateId({ action: "update", id: "*OLD" }) +assert.equal(acknowledgedCreateId({ ".id": "*NEW" }), "*NEW") +for (const result of [undefined, null, {}, [], { id: "*OLD" }, { ".id": "" }, { ".id": " *NEW" }, { ".id": 7 }]) assert.equal(acknowledgedCreateId(result), undefined) +const old = { ".id": "*OLD", chain: "forward", action: "accept" } +const added = { ...old, ".id": "*NEW" } +assert.equal(recoveryRosStepId(base, [old], [old, added]), "*NEW") +assert.equal(recoveryRosStepId(base, [old], [old]), undefined) +assert.equal(recoveryRosStepId(base, [old], [old, added, { ...added, ".id": "*OTHER" }]), undefined) +assert.equal(recoveryRosStepId(base, [old], [old, { ...added, ".id": "" }]), undefined) +assert.equal(recoveryRosStepId({ action: "update", id: "*OLD" }, [old], [old]), "*OLD") + +// Exercise actual executor/verifier boundaries: a poisoned legacy step and a +// missing acknowledgement must terminate before SQL or RouterOS reads/writes. +const originalQuery = pool.query, originalConnect = pool.connect +const originalGet = MikrotikClient.prototype.get, originalPut = MikrotikClient.prototype.put +let effects = 0 +const forbidden = () => { effects++; throw new Error("Unexpected SQL/native access") } +pool.query = forbidden as typeof pool.query +pool.connect = forbidden as typeof pool.connect +MikrotikClient.prototype.get = forbidden as typeof originalGet +MikrotikClient.prototype.put = forbidden as typeof originalPut +try { + for (const id of ["*OLD", "", null, 1]) { + const poisoned = { ...base, id } as RosStep + await assert.rejects(() => executeRosStep(poisoned), invalid) + await assert.rejects(() => verifyStep({ kind: "ros", step: poisoned }, { ".id": "*NEW" }), invalid) + } + for (const result of [undefined, {}, { id: "*OLD" }, { ".id": "" }, { ".id": 7 }]) assert.equal(await verifyStep({ kind: "ros", step: base }, result), false) + assert.equal(effects, 0) +} finally { + pool.query = originalQuery; pool.connect = originalConnect + MikrotikClient.prototype.get = originalGet; MikrotikClient.prototype.put = originalPut + await pool.end() +} +console.log("Native CREATE identity: all 30 strict schemas/revisions, legacy poison rejected before SQL/native access, acknowledgement-only verification and one-added-ID recovery") diff --git a/backend/src/modules/mcp/native-create-identity.ts b/backend/src/modules/mcp/native-create-identity.ts new file mode 100644 index 00000000..56eb8749 --- /dev/null +++ b/backend/src/modules/mcp/native-create-identity.ts @@ -0,0 +1,26 @@ +import { McpError } from "./security.js" + +type IdentityStep = { action: string; id?: unknown } +type IdentityRow = Record + +/** Old sealed plans must obey the same identity rule as new public requests. */ +export function assertNoCallerCreateId(step: IdentityStep): void { + if (step.action === "create" && step.id !== undefined) throw new McpError("INVALID_INPUT", "ID создаваемого объекта назначает RouterOS", 400) +} + +function validId(value: unknown): value is string { + return typeof value === "string" && value.length > 0 && value.length <= 200 && value.trim() === value +} + +export function acknowledgedCreateId(result: unknown): string | undefined { + const id = result && typeof result === "object" && !Array.isArray(result) ? (result as IdentityRow)[".id"] : undefined + return validId(id) ? id : undefined +} + +/** Recovery may remove only the one new identity proven by the checkpoints. */ +export function recoveryRosStepId(step: IdentityStep, before: IdentityRow[], after: IdentityRow[]): string | undefined { + assertNoCallerCreateId(step) + if (step.action !== "create") return validId(step.id) ? step.id : undefined + const added = after.filter(row => !before.some(previous => previous[".id"] === row[".id"])) + return added.length === 1 && validId(added[0][".id"]) ? added[0][".id"] : undefined +} diff --git a/backend/src/modules/mcp/network-host-actions.ts b/backend/src/modules/mcp/network-host-actions.ts new file mode 100644 index 00000000..cefea885 --- /dev/null +++ b/backend/src/modules/mcp/network-host-actions.ts @@ -0,0 +1,23 @@ +import { hasPermission } from "../../lib/permissions.js" +import { resolveHostsSchema, resolveNetworkHosts } from "../../services/network-host-resolution.js" +import { fingerprint, McpError } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import type { add } from "./actions.js" + +export function registerNetworkHostActions(register: typeof add) { + // Match the existing POST /api/network/resolve-hosts permission boundary. + // Observer mode still prevents configuration writes using this grant. + register("network.hosts.resolve", "mm_network_hosts_resolve", "IPv4-резолв hostname системным resolver хоста менеджера", "network", "mm:network:write", "read", "global", resolveHostsSchema, { + read: async (input, context) => { + const assertReadable = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Резолв отменён", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Право сетевой диагностики отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Область сетевой диагностики изменилась", 409) + } + await assertReadable() + try { return await resolveNetworkHosts(input, { signal: context.signal, assertReadable }) } + catch (error) { await assertReadable(); if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Резолв не подтверждён", 409) } + }, + }) +} diff --git a/backend/src/modules/mcp/network-neighbor-actions.ts b/backend/src/modules/mcp/network-neighbor-actions.ts new file mode 100644 index 00000000..bb2ccd6b --- /dev/null +++ b/backend/src/modules/mcp/network-neighbor-actions.ts @@ -0,0 +1,133 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { hasPermission } from "../../lib/permissions.js" +import { observedBoolean } from "../../services/ospf-observed-values.js" +import { configurationRows, resourceById, routerClient } from "./resources.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { fingerprint, McpError } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import type { ActionContext } from "./action-types.js" +import type { add } from "./actions.js" + +const name = z.string().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f]/.test(value)) +const statuses = new Set(["delay", "failed", "incomplete", "permanent", "probe", "reachable", "stale", "noarp"]) +const baseFields = ".id,address,mac-address,interface,status,vrf,disabled,dynamic,invalid" +const paths = { + ipv4: `/ip/arp?.proplist=${baseFields},complete,dhcp,published`, + ipv6: `/ipv6/neighbor?.proplist=${baseFields},router`, +} +const ndBase = ".id,interface,disabled,invalid,dynamic" +const ndPaths = { + interfaces: `/ipv6/nd?.proplist=${ndBase},ra-interval,ra-delay,ra-lifetime,ra-preference,mtu,hop-limit,reachable-time,retransmit-interval,advertise-mac-address,advertise-dns,dns,pref64,managed-address-configuration,other-configuration`, + prefixes: `/ipv6/nd/prefix?.proplist=${ndBase},prefix,6to4-interface,on-link,autonomous,dhcp6-pd-preferred,valid-lifetime,preferred-lifetime`, +} +const pageSchema = { + serverId: z.number().int().positive().safe(), interface: name.optional(), + offset: z.number().int().min(0).max(10000).default(0), limit: z.number().int().min(1).max(200).default(100), +} + +function catalogRows(value: unknown, project: (text: (key: string) => string | null, flag: (key: string) => boolean | null) => T) { + if (!Array.isArray(value) || value.length > 10000) throw new Error("Network catalog unavailable") + const ids = new Set() + return value.map((row: unknown) => { + if (!row || typeof row !== "object" || Array.isArray(row)) throw new Error("Invalid network catalog row") + const native = row as Record + const text = (key: string) => { + const parsed = name.safeParse(native[key]) + return parsed.success ? parsed.data : null + } + const id = text(".id") + if (!id || !/^\*[a-z0-9]+$/i.test(id) || ids.has(id)) throw new Error("Unconfirmed network catalog identity") + ids.add(id) + const flag = (key: string) => observedBoolean(text(key) ?? undefined) + return { id, interface: text("interface"), disabled: flag("disabled"), dynamic: flag("dynamic"), invalid: flag("invalid"), ...project(text, flag) } + }).sort((left, right) => left.id.localeCompare(right.id)) +} + +async function readNetworkValue(serverId: number, context: ActionContext, path: string, project: (raw: unknown) => T) { + const authorize = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение сетевого каталога отменено", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:read") || !hasPermission(current.policy.permissions, "mm:network:read")) throw new McpError("PERMISSION_DENIED", "Право чтения сети отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Область чтения сети изменилась", 409) + } + await authorize() + const source = await createRoutingSourceContext(await readRoutingEndpoints([serverId])) + if (source.expected.length !== 1) throw new McpError("DEVICE_UNAVAILABLE", "Маршрутизатор отсутствует или выключен", 409) + const check = async () => { await authorize(); await source.assertCurrent(); await authorize() } + try { + const { client } = await routerClient(serverId) + const raw = await client.get(path, 10000, context.signal, 1048576, check) + const value = project(raw) + await check() + return value + } catch (error) { + await check() + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Сетевой каталог недоступен или не подтверждён", 409) + } +} + +async function readCatalog(input: Record, context: ActionContext, path: string, project: (raw: unknown) => T[], includeAll = false) { + const serverId = Number(input.serverId), observed = await readNetworkValue(serverId, context, path, project) + const rows = observed.filter(row => input.interface === undefined || row.interface === input.interface || (includeAll && row.interface === "all")) + const unknownInterfaces = input.interface === undefined ? 0 : observed.filter(row => row.interface === null).length + const offset = Number(input.offset), limit = Number(input.limit), items = rows.slice(offset, offset + limit) + return { serverId, items, total: rows.length, nextOffset: offset + limit < rows.length ? offset + limit : null, + incomplete: items.length < rows.length || unknownInterfaces > 0, unknownInterfaces, + observedAt: new Date().toISOString(), timestampKind: "backend-receipt", forwardingVerified: false } +} + +export function registerNetworkNeighborActions(register: typeof add) { + register("network.neighbors.list", "mm_network_neighbors_list", "Таблица ARP или кэш соседей IPv6 одного RouterOS", "network", "mm:network:read", "read", "servers", z.object({ + ...pageSchema, family: z.enum(["ipv4", "ipv6"]), + }).strict(), { read: async (input, context) => { + const family = input.family === "ipv4" ? "ipv4" : "ipv6" + const page = await readCatalog(input, context, paths[family], raw => catalogRows(raw, (text, flag) => { + const address = text("address"), status = text("status"), mac = text("mac-address") + if (!address || isIP(address) !== (family === "ipv4" ? 4 : 6)) throw new Error("Unconfirmed neighbor address") + return { + address, vrf: text("vrf"), status: status && statuses.has(status) ? status : null, + macAddress: mac && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(mac) && mac !== "00:00:00:00:00:00" ? mac : null, + ...(family === "ipv4" ? { complete: flag("complete"), dhcp: flag("dhcp"), published: flag("published") } : { router: flag("router") }), + } + })) + return { ...page, family } + } }) + register("network.ipv6.nd.list", "mm_ipv6_nd_list", "Наблюдаемые настройки IPv6 Router Advertisement или префиксы ND", "network", "mm:network:read", "read", "servers", z.object({ + ...pageSchema, collection: z.enum(["interfaces", "prefixes"]).default("interfaces"), + }).strict(), { read: async (input, context) => { + const collection = input.collection === "prefixes" ? "prefixes" : "interfaces" + const page = await readCatalog(input, context, ndPaths[collection], raw => catalogRows(raw, (text, flag) => { + if (collection === "prefixes") return { + prefix: text("prefix"), sixToFourInterface: text("6to4-interface"), onLink: flag("on-link"), autonomous: flag("autonomous"), + dhcp6PdPreferred: flag("dhcp6-pd-preferred"), validLifetime: text("valid-lifetime"), preferredLifetime: text("preferred-lifetime"), + } + const advertiseDns = text("advertise-dns"), raPreference = text("ra-preference") + return { + raInterval: text("ra-interval"), raDelay: text("ra-delay"), raLifetime: text("ra-lifetime"), + raPreference: raPreference && ["low", "medium", "high"].includes(raPreference) ? raPreference : null, + mtu: text("mtu"), hopLimit: text("hop-limit"), reachableTime: text("reachable-time"), retransmitInterval: text("retransmit-interval"), + advertiseMacAddress: flag("advertise-mac-address"), advertiseDns: advertiseDns && ["no", "yes", "self"].includes(advertiseDns) ? advertiseDns : null, + dns: text("dns"), pref64: text("pref64"), managedAddressConfiguration: flag("managed-address-configuration"), otherConfiguration: flag("other-configuration"), + } + }), true) + return { ...page, collection } + } }) + register("network.ipv6.nd.settings.get", "mm_ipv6_nd_settings_get", "Глобальные настройки приёма IPv6 Router Advertisement", "network", "mm:network:read", "read", "servers", z.object({ serverId: pageSchema.serverId }).strict(), { + read: async (input, context) => { + const serverId = Number(input.serverId), resource = resourceById("ipv6_nd_settings") + const distanceKey = "router-advertisement-route-distance", optionsKey = "router-advertisement-ignored-options" + const settings = await readNetworkValue(serverId, context, resource.path, raw => { + const rows = configurationRows(resource, raw) + if (rows.length !== 1 || rows[0][".id"] !== undefined) throw new Error("Unconfirmed ND settings singleton") + const distance = rows[0][distanceKey], options = resource.fields.shape[optionsKey].safeParse(rows[0][optionsKey]) + return { + routeDistance: typeof distance === "string" && /^\d{1,3}$/.test(distance) && Number(distance) <= 255 ? Number(distance) : null, + ignoredOptions: options.success && typeof options.data === "string" ? options.data ? options.data.split(",") : [] : null, + } + }) + return { serverId, settings, observedAt: new Date().toISOString(), timestampKind: "backend-receipt", forwardingVerified: false } + }, + }) +} diff --git a/backend/src/modules/mcp/network-neighbors-acceptance.ts b/backend/src/modules/mcp/network-neighbors-acceptance.ts new file mode 100644 index 00000000..ef54307d --- /dev/null +++ b/backend/src/modules/mcp/network-neighbors-acceptance.ts @@ -0,0 +1,152 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { invokeMcpTool } from "./runtime.js" +import { authenticateMcpToken, createMcpConnection } from "./store.js" + +type Family = "ipv4" | "ipv6" +type NeighborPage = { + serverId: number + family: Family + items: { id: string; address: string; interface: string; macAddress: string | null; status: string | null; vrf: string | null; dynamic: boolean | null }[] + total: number + nextOffset: number | null + incomplete: boolean +} +type NdPage = { + serverId: number + collection: "interfaces" | "prefixes" + items: { id: string; interface: string | null; raInterval?: string | null; advertiseDns?: string | null; disabled?: boolean | null; prefix?: string; validLifetime?: string | null; autonomous?: boolean | null; dynamic?: boolean | null }[] + total: number + nextOffset: number | null + incomplete: boolean + unknownInterfaces: number +} + +/** Runs only inside the disposable SQL/HTTP acceptance database. */ +export async function acceptNetworkNeighborsMcp(actor: AuthUser) { + const selection = { serverId: 105, family: "ipv4" as Family } + const arp = { ".id": "*A", address: "192.0.2.1", interface: "ether1", "mac-address": "02:00:00:00:00:01", status: "reachable", vrf: "lab-vrf", dynamic: "true", opaque: "NEVER_NEIGHBOR_PRIVATE" } + const incomplete = { ".id": "*C", address: "192.0.2.3", interface: "ether1", status: "incomplete" } + const nd = { ".id": "*D", address: "fe80::2", interface: "ether1", status: "stale", router: "true" } + let ipv4: unknown = [arp, { ...arp, ".id": "*B", address: "192.0.2.2", interface: "ether2" }, incomplete] + let ipv6: unknown = [nd] + const ndInterface = { ".id": "*1", interface: "all", "ra-interval": "3m20s-10m", "advertise-dns": "self", disabled: "false", opaque: "NEVER_NEIGHBOR_PRIVATE" } + const ndPrefix = { ".id": "*4", prefix: "2001:db8:1::/64", interface: "ether1", "valid-lifetime": "1d", autonomous: "true", dynamic: "false" } + let ndInterfaces: unknown = [ndInterface, { ".id": "*2", interface: "ether1" }, { ".id": "*3", interface: "ether2" }] + let ndPrefixes: unknown = [ndPrefix, { ...ndPrefix, ".id": "*5", prefix: "2001:db8:2::/64", interface: "ether2" }] + const menus = new Map([ + ["/rest/ip/arp", { fields: [".id", "address", "interface", "mac-address", "status"], rows: () => ipv4 }], + ["/rest/ipv6/neighbor", { fields: [".id", "address", "interface", "mac-address", "status"], rows: () => ipv6 }], + ["/rest/ipv6/nd", { fields: [".id", "interface", "ra-interval", "advertise-dns", "disabled"], rows: () => ndInterfaces }], + ["/rest/ipv6/nd/prefix", { fields: [".id", "prefix", "interface", "valid-lifetime", "autonomous", "dynamic"], rows: () => ndPrefixes }], + ]) + let nativeError = false + const requests: URL[] = [] + const router = createServer((req, res) => { + const request = new URL(req.url ?? "", "http://127.0.0.1") + requests.push(request) + res.setHeader("content-type", "application/json") + const fields = request.searchParams.get(".proplist")?.split(",") ?? [] + const menu = menus.get(request.pathname) + if (req.method !== "GET" || !menu || !menu.fields.every(field => fields.includes(field))) { + res.writeHead(400).end("{}") + return + } + if (nativeError) { res.writeHead(502).end('{"message":"NEVER_NEIGHBOR_PROVIDER"}'); return } + res.end(JSON.stringify(menu.rows())) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined + try { + const port = (router.address() as { port: number }).port + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES (105,'neighbor-in-scope','127.0.0.1',$1,false,'NEVER_NEIGHBOR_PASSWORD'),(106,'neighbor-outside-scope','127.0.0.1',$1,false,'NEVER_NEIGHBOR_PASSWORD')", [port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: "Network neighbors", mode: "observer", expiresAt: new Date(Date.now() + 1_800_000).toISOString(), + permissions: ["mm:network:read"], scope: { serverIds: [105], operationIds: ["network.neighbors.list", "network.ipv6.nd.list"], allowGlobal: false }, + }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const read = (input: Record = {}) => invokeMcpTool(principal, "mm_network_neighbors_list", { ...selection, ...input }) as Promise + const readNd = (input: Record = {}) => invokeMcpTool(principal, "mm_ipv6_nd_list", { serverId: selection.serverId, ...input }) as Promise + const first = await read({ interface: "ether1", limit: 1 }) + assert.equal(first.serverId, selection.serverId) + assert.equal(first.family, "ipv4") + assert.equal(first.total, 2); assert.equal(first.nextOffset, 1); assert.equal(first.incomplete, true) + assert.equal(first.items.length, 1) + assert.deepEqual( + first.items.map(({ id, address, interface: portName, macAddress, status, vrf, dynamic }) => ({ id, address, portName, macAddress, status, vrf, dynamic })), + [{ id: "*A", address: arp.address, portName: "ether1", macAddress: arp["mac-address"], status: "reachable", vrf: "lab-vrf", dynamic: true }], + ) + assert.equal(requests[0].pathname, "/rest/ip/arp") + assert.ok(!JSON.stringify(first).includes("NEVER_")) + const last = await read({ interface: "ether1", limit: 1, offset: 1 }) + assert.equal(last.total, 2); assert.equal(last.nextOffset, null) + assert.deepEqual(last.items.map(({ id, macAddress, status, vrf }) => ({ id, macAddress, status, vrf })), [{ id: "*C", macAddress: null, status: "incomplete", vrf: null }]) + + const v6 = await read({ family: "ipv6" }) + assert.equal(requests.at(-1)?.pathname, "/rest/ipv6/neighbor") + assert.equal(v6.family, "ipv6"); assert.equal(v6.total, 1); assert.equal(v6.incomplete, false) + assert.deepEqual(v6.items.map(({ id, address, macAddress, status, dynamic }) => ({ id, address, macAddress, status, dynamic })), + [{ id: "*D", address: nd.address, macAddress: null, status: "stale", dynamic: null }]) + ipv6 = [] + const empty = await read({ family: "ipv6" }) + assert.deepEqual(empty.items, []); assert.equal(empty.total, 0); assert.equal(empty.nextOffset, null); assert.equal(empty.incomplete, false) + + const ndFirst = await readNd({ interface: "ether1", limit: 1 }) + assert.equal(requests.at(-1)?.pathname, "/rest/ipv6/nd") + assert.equal(ndFirst.serverId, selection.serverId) + assert.equal(ndFirst.collection, "interfaces") + assert.equal(ndFirst.total, 2) + assert.equal(ndFirst.nextOffset, 1) + assert.equal(ndFirst.incomplete, true) + assert.equal(ndFirst.unknownInterfaces, 0) + assert.deepEqual(ndFirst.items.map(({ id, interface: portName, raInterval, advertiseDns, disabled }) => ({ id, portName, raInterval, advertiseDns, disabled })), + [{ id: "*1", portName: "all", raInterval: ndInterface["ra-interval"], advertiseDns: "self", disabled: false }]) + assert.ok(!JSON.stringify(ndFirst).includes("NEVER_")) + const ndLast = await readNd({ interface: "ether1", limit: 1, offset: 1 }) + assert.deepEqual(ndLast.items.map(({ id, interface: portName }) => ({ id, portName })), [{ id: "*2", portName: "ether1" }]) + assert.equal(ndLast.nextOffset, null) + + const prefixes = await readNd({ collection: "prefixes", interface: "ether1" }) + assert.equal(requests.at(-1)?.pathname, "/rest/ipv6/nd/prefix") + assert.equal(prefixes.collection, "prefixes") + assert.equal(prefixes.total, 1) + assert.equal(prefixes.incomplete, false) + assert.deepEqual(prefixes.items.map(({ id, prefix, validLifetime, autonomous, dynamic }) => ({ id, prefix, validLifetime, autonomous, dynamic })), + [{ id: "*4", prefix: ndPrefix.prefix, validLifetime: "1d", autonomous: true, dynamic: false }]) + + const previousReads = requests.length + await assert.rejects(read({ serverId: 106 }), { code: "SERVER_DENIED" }) + await assert.rejects(readNd({ serverId: 106 }), { code: "SERVER_DENIED" }) + await pool.query("UPDATE servers SET enabled=false WHERE id=105") + await assert.rejects(read(), { code: "DEVICE_UNAVAILABLE" }) + await assert.rejects(readNd(), { code: "DEVICE_UNAVAILABLE" }) + await pool.query("UPDATE servers SET enabled=true WHERE id=105") + assert.equal(requests.length, previousReads, "Denied and disabled devices stop before native polling") + + for (const bad of [{ items: [arp] }, [arp, { ...arp, address: "192.0.2.4" }]]) { + ipv4 = bad + await assert.rejects(read(), { code: "STATE_UNAVAILABLE" }) + } + ndInterfaces = [ndInterface, { ...ndInterface, interface: "ether1" }] + await assert.rejects(readNd(), { code: "STATE_UNAVAILABLE" }) + ndPrefixes = [] + const emptyPrefixes = await readNd({ collection: "prefixes" }) + assert.deepEqual(emptyPrefixes.items, []) + assert.equal(emptyPrefixes.total, 0) + assert.equal(emptyPrefixes.nextOffset, null) + assert.equal(emptyPrefixes.incomplete, false) + nativeError = true + await assert.rejects(read(), (error: Error & { code?: string }) => error.code === "STATE_UNAVAILABLE" && !error.message.includes("NEVER_")) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n, 0) + console.log("✓ Network neighbors and IPv6 ND MCP: native projection, interface fallback, pagination, empty collections, source scope and malformed/provider rejection") + } finally { + if (connectionId) await pool.query("DELETE FROM mcp_connections WHERE id=$1", [connectionId]) + await pool.query("DELETE FROM servers WHERE id IN (105,106)") + router.closeAllConnections() + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/operation-execution-origin.sql-helper.ts b/backend/src/modules/mcp/operation-execution-origin.sql-helper.ts new file mode 100644 index 00000000..8d024e17 --- /dev/null +++ b/backend/src/modules/mcp/operation-execution-origin.sql-helper.ts @@ -0,0 +1,211 @@ +/** Actual public fixture runner/admission/legacy locks, real SQL ACK faults. + * No native HTTP, production registration or default global-writer activation. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import { assertOwnedBandwidthSql } from "../../services/bandwidth-admission-process.sql-helper.js" + +export async function acceptOwnedMcpOperationRoot() { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_MCP_OPERATION_ORIGIN, "true") + const { pool } = await import("../../db/index.js"), { Client } = await import("pg"), { z } = await import("zod") + const { add } = await import("./actions.js"), { registerMcpService } = await import("./service-registry.js") + const { createMcpConnection, authenticateMcpToken } = await import("./store.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { invokeMcpTool, approvePlan, applyPlan, recoverMcpOperations } = await import("./runtime.js") + const root = await import("./operation-execution-origin.js") + const { readMcpTemporaryGrantOrigin } = await import("./temporary-grant-origin.js") + const { captureVerifiedMcpServiceExecution } = await import("../../services/config-mutation-policy.js") + const operationName = "fixture.operation-root." + randomUUID(), serviceName = "fixture.operation-root-service." + randomUUID(), tool = "mm_owned_operation_root_" + randomUUID().replaceAll("-", "") + const secret = "OWNED_PRIVATE_ROOT_PAYLOAD_" + randomUUID(), application = await pool.connect(), rival = await pool.connect() + const query = pool.query, clientQuery = Client.prototype.query, ids: number[] = [], checks: string[] = [] + const expected = new Map(), caps = new Map(), lockKinds = new Map>() + let sourceFaultApplied = false + let mode = "normal", serviceCalls = 0, preLockCaptures = 0, legacyEntries = 0, callbackContenders = 0 + let selectedOperation: string | undefined, heldRival: string | undefined, cleanupRows = -1, claimRows: number[] = [], rivalNonce: string | undefined + let reached!: () => void, proceed!: () => void, cleanupFinished!: () => void + let reachedGate = Promise.resolve(), proceedGate = Promise.resolve(), cleanupGate = Promise.resolve(), arrivals = 0 + const reset = (next: string) => { mode = next; sourceFaultApplied = false; selectedOperation = undefined; cleanupRows = -1; claimRows = []; rivalNonce = undefined; arrivals = 0 + reachedGate = new Promise(yes => { reached = yes }); proceedGate = new Promise(yes => { proceed = yes }); cleanupGate = new Promise(yes => { cleanupFinished = yes }) } + const bounded = async (promise: Promise): Promise => { + let timer: ReturnType | undefined + try { return await Promise.race([promise, new Promise((_yes, no) => { timer = setTimeout(() => no(Error("Owned operation root fixture deadline")), 20000) })]) } + finally { clearTimeout(timer) } + } + const terminal = async (id: string) => { + for (const until = Date.now() + 20000; Date.now() < until;) { + const row = (await application.query("SELECT state,result,error_code FROM public.mcp_operations WHERE id=$1", [id])).rows[0] + if (row && !["pending", "running"].includes(row.state)) return row + await delay(10) + } + throw Error("Owned operation did not reach terminal state") + } + try { + for (let i = 0; i < 2; i++) ids.push(Number((await application.query("INSERT INTO public.servers(name,host,enabled) VALUES('owned-operation-root','127.0.0.1',false) RETURNING id")).rows[0].id)) + await application.query("UPDATE public.mcp_settings SET enabled=true WHERE id=1") + registerMcpService(serviceName, { current: async () => ({ fixture: "before" }), verify: async (_input, result) => !!result && typeof result === "object" && "fixture" in result, + execute: async () => { serviceCalls++; const old = await captureVerifiedMcpServiceExecution(); assert(old); await assert.rejects(root.captureMcpOperationExecutionOrigin()); return { fixture: "done" } } }) + add(operationName, tool, "Owned operation root acceptance", "servers", "mm:servers:write", "write", "servers", z.object({ password: z.string() }).strict(), { version: 1, + target: () => ({ scope: "servers", serverIds: ids }), prepare: async input => ({ target: { scope: "servers", serverIds: ids }, objects: ids.map(serverId => ({ kind: "server", id: String(serverId), serverId })), snapshots: [], before: { fixture: "before" }, warnings: [], + steps: [0, 1].map(index => ({ kind: "service", service: serviceName, input: { index, password: input.password } })) }) }) + const cleanupPids = new Set() + Client.prototype.query = function(this: InstanceType, ...args: unknown[]) { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "", values = (Array.isArray(args[1]) ? args[1] : (args[0] as { values?: unknown[] })?.values) as unknown[] | undefined + const key = String(values?.[0] ?? ""), opLock = text.includes("pg_try_advisory_lock") && key.startsWith("mcp-operation:") + const legacy = opLock ? "operation" : text.includes("pg_try_advisory_lock") && text.includes("mm-config:application") ? "application" : text.includes("pg_try_advisory_lock") && /^mm-config:\d+$/.test(key) ? "server" : undefined + const physical = this as unknown as { processID: number } + const run = async () => { + if (opLock) { + const id = key.slice("mcp-operation:".length); selectedOperation = id; legacyEntries++ + const cap = await root.captureMcpOperationExecutionOrigin(), metadata = await root.readCurrentMcpOperationExecutionOrigin(cap), planned = expected.get(metadata.planId); assert(planned) + assert.equal(metadata.operationId, id); assert.equal(metadata.approved, planned.approved); assert.equal(metadata.ownerId, planned.ownerId) + assert.equal(metadata.evidence.steps.length, 2); assert.equal(metadata.privateInput.password, secret); assert.deepEqual(metadata.serverIds, ids); assert(!("stepIndex" in metadata)) + const held = await rival.query("SELECT count(*)::int AS n FROM pg_catalog.pg_locks WHERE pid=$1 AND locktype='advisory' AND granted", [physical.processID]); assert.equal(held.rows[0].n, 0) + caps.set(id, cap); preLockCaptures++ + if (mode === "hold-public") { reached(); await proceedGate } + if (mode === "rival-operation-lock") { await rival.query("SELECT pg_advisory_lock(hashtextextended($1,0))", [key]); heldRival = key } + } else if (legacy && selectedOperation) { + await assert.rejects(root.captureMcpOperationExecutionOrigin()); await root.readCurrentMcpOperationExecutionOrigin(caps.get(selectedOperation)) + } + if (legacy && selectedOperation) { const set = lockKinds.get(selectedOperation) ?? new Set(); set.add(legacy); lockKinds.set(selectedOperation, set) } + const result = await Reflect.apply(clientQuery, this, args) + if (opLock && mode === "callback-error") throw Error("Owned callback failure after actual operation lock ACK") + if (text.startsWith("UPDATE public.mcp_operations SET state='unknown'") && values?.length === 4) { cleanupRows = result.rowCount; cleanupPids.add(physical.processID) } + if (text === "COMMIT" && cleanupPids.delete(physical.processID)) cleanupFinished() + return result + } + // Only direct Promise driver calls are instrumented asynchronously. All + // pg-pool callback calls and their results stay completely unchanged. + if (legacy || text.startsWith("UPDATE public.mcp_operations SET state='unknown'") || text === "COMMIT" && cleanupPids.has(physical.processID)) return run() + return Reflect.apply(clientQuery, this, args) + } as typeof Client.prototype.query + pool.query = function(this: typeof pool, ...args: unknown[]) { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "", values = (Array.isArray(args[1]) ? args[1] : (args[0] as { values?: unknown[] })?.values) as unknown[] | undefined + if (text.startsWith("/* mcp-operation-root:source */") && mode === "preclaim-permission" && !sourceFaultApplied) return (async () => { + const result = await Reflect.apply(query, this, args); sourceFaultApplied = true + await application.query("UPDATE public.mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [values![1]]) + return result + })() + if (!text.startsWith("/* mcp-operation-root:claim */")) return Reflect.apply(query, this, args) + return (async () => { + if (mode === "dual-claim") arrivals++ + if (mode === "lost-zero") { rivalNonce = randomUUID(); const won = await application.query("UPDATE public.mcp_operations SET state='running',result=jsonb_build_object('__mmappMcpOperationClaimV1',$2::text) WHERE id=$1 AND state='pending'", [values![0], rivalNonce]); assert.equal(won.rowCount, 1) } + const result = await Reflect.apply(query, this, args); claimRows.push(result.rowCount) + if (mode === "lost-own") { assert.equal(result.rowCount, 1); throw Error("Owned actual one-row claim ACK lost") } + if (mode === "lost-zero") { assert.equal(result.rowCount, 0); throw Error("Owned actual zero-row claim ACK lost") } + return result + })() + } as typeof pool.query + async function prepare(approved: boolean) { + const actor = { id: randomUUID(), email: "owned-root@example.test", name: "Owned operation root", apps: ["mm"], permissions: ["mm:servers:write", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 300000 } + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned operation root", mode: approved ? "approval" : "autonomous", permissions: ["mm:servers:write"], expiresAt: new Date(actor.expiresAt).toISOString(), scope: { serverIds: ids, operationIds: [operationName, "operations.read"], allowGlobal: false }, + ...(approved ? {} : { dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: [operationName] }), limits: { callsPerMinute: 500, mutationsPerHour: 1000 } }), actor) + const principal = await authenticateMcpToken(access.token), plan = await invokeMcpTool(principal, tool, { password: secret }) as { planId: string } + if (approved) await approvePlan(plan.planId, actor, true) + expected.set(plan.planId, { approved, ownerId: actor.id }) + return { principal, planId: plan.planId } + } + for (const approved of [true, false]) { + reset("hold-public"); const prepared = await prepare(approved), before = serviceCalls + const operation = await applyPlan(prepared.principal, prepared.planId, randomUUID()); await bounded(reachedGate) + const privateRow = (await application.query("SELECT result FROM public.mcp_operations WHERE id=$1", [operation.id])).rows[0].result + const nonce = privateRow[root.MCP_OPERATION_PRIVATE_CLAIM_KEY]; assert.equal(typeof nonce, "string") + const model = await invokeMcpTool(prepared.principal, "mm_operation_get", { operationId: operation.id }), web = await applyPlan(prepared.principal, prepared.planId, randomUUID()) + for (const output of [model, web]) { assert(!JSON.stringify(output).includes(nonce)); assert(!JSON.stringify(output).includes(root.MCP_OPERATION_PRIVATE_CLAIM_KEY)) } + proceed(); const finished = await terminal(operation.id); assert.equal(finished.state, "applied"); assert.equal(serviceCalls - before, 2) + assert.deepEqual([...lockKinds.get(operation.id)!].sort(), ["application", "operation", "server"]) + await assert.rejects(root.readCurrentMcpOperationExecutionOrigin(caps.get(operation.id))) + } + checks.push("actual human/autonomous public fixture runner publishes full root before operation/app/server locks, retains per-step origins and hides nonce from real model/direct-web apply results") + { + const { withMcpDatabaseRequest } = await import("./database-request-lease.js") + const { SYSTEM_DATABASE_REQUEST_LOCK, withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js") + reset("normal") + const prepared = await prepare(true), before = serviceCalls, key = randomUUID() + let releaseMaintenance!: () => void, enteredMaintenance!: () => void + const release = new Promise(resolve => { releaseMaintenance = resolve }) + const entered = new Promise(resolve => { enteredMaintenance = resolve }) + let maintenance: Promise | undefined + try { + const admitted = await withMcpDatabaseRequest(async () => { + maintenance = withSystemDatabaseOperationLock(() => pool.connect(), async () => { + enteredMaintenance() + await release + }) + void maintenance.catch(() => {}) + for (const end = Date.now() + 2000; Date.now() < end;) { + const queued = await application.query( + "SELECT 1 FROM pg_locks WHERE locktype='advisory' AND classid=0 AND objid=$1 AND mode='ExclusiveLock' AND NOT granted", + [SYSTEM_DATABASE_REQUEST_LOCK]) + if (queued.rowCount) return applyPlan(prepared.principal, prepared.planId, key) + await delay(10) + } + throw Error("Owned maintenance lock did not queue") + }) + await bounded(entered) + await delay(350) + await application.query("UPDATE mcp_operations SET created_at=now()-interval '3 minutes' WHERE id=$1", [admitted.id]) + const orphanId = randomUUID(), orphanPlanId = randomUUID() + await application.query(`INSERT INTO mcp_plans + (id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) + SELECT $1,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,'executing',now()-interval '1 minute' + FROM mcp_plans WHERE id=$2`, [orphanPlanId, prepared.planId]) + await application.query(`INSERT INTO mcp_operations (id,plan_id,connection_id,idempotency_key,state,created_at) + VALUES($1,$2,$3,$1::uuid::text,'pending',now()-interval '3 minutes')`, [orphanId, orphanPlanId, prepared.principal.connectionId]) + // Exercise the recovery predicate while the real runner is held at admission. + await recoverMcpOperations() + assert.equal((await application.query("SELECT state FROM mcp_operations WHERE id=$1", [admitted.id])).rows[0].state, "pending") + assert.deepEqual((await application.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [orphanId])).rows[0], + { state: "unknown", error_code: "PROCESS_INTERRUPTED" }) + assert.equal(serviceCalls, before) + releaseMaintenance() + await maintenance + assert.equal((await terminal(admitted.id)).state, "applied") + assert.equal((await applyPlan(prepared.principal, prepared.planId, key)).id, admitted.id) + assert.equal(serviceCalls - before, 2) + checks.push("recovery preserves a queued operation under its original plan deadline, closes an expired orphan, and the queued operation executes both steps once") + } finally { + releaseMaintenance() + await maintenance?.catch(() => {}) + } + } + for (const fault of ["lost-own", "lost-zero", "rival-operation-lock", "callback-error", "preclaim-permission"] as const) { + reset(fault); const prepared = await prepare(false), before = serviceCalls, operation = await applyPlan(prepared.principal, prepared.planId, randomUUID()) + if (fault === "lost-zero") { + await bounded(cleanupGate); assert.equal(cleanupRows, 0) + const row = (await application.query("SELECT state,result FROM public.mcp_operations WHERE id=$1", [operation.id])).rows[0] + assert.equal(row.state, "running"); assert.equal(row.result[root.MCP_OPERATION_PRIVATE_CLAIM_KEY], rivalNonce) + await application.query("UPDATE public.mcp_operations SET state='unknown',result='{}'::jsonb,finished_at=now() WHERE id=$1 AND result->>'__mmappMcpOperationClaimV1'=$2", [operation.id, rivalNonce]) + } else { const row = await terminal(operation.id); assert.equal(row.state, "unknown"); assert.equal(row.error_code, "PROCESS_INTERRUPTED") } + if (heldRival) { await rival.query("SELECT pg_advisory_unlock(hashtextextended($1,0))", [heldRival]); heldRival = undefined } + assert.equal(serviceCalls, before) + } + checks.push("actual one-row/zero-row ACK loss, real rival operation lock refusal and post-real-lock callback error and pre-claim permission loss: no legacy service/native replay; only own nonce becomes unknown, rival winner preserved") + reset("dual-claim"); const prepared = await prepare(false), anchor = root.beginMcpOperationAdmission(prepared.principal), wall = Date.now(), mono = performance.now() + await application.query("BEGIN") + let issued: Awaited> + try { + const plan = (await application.query("SELECT * FROM public.mcp_plans WHERE id=$1 FOR UPDATE", [prepared.planId])).rows[0] + const grant = await readMcpTemporaryGrantOrigin(application, prepared.principal.connectionId, prepared.principal.credentialId), expires = Math.min(plan.expires_at.getTime(), Date.parse(prepared.principal.credentialExpiresAt)) + issued = await root.insertMcpOperationAdmission(application, anchor, prepared.planId, randomUUID(), { ...grant, monotonicEnd: grant.expiresAt === null ? Infinity : mono + grant.expiresAt - wall, admissionExpiresAt: expires, admissionMonotonicEnd: mono + expires - wall }) + await application.query("UPDATE public.mcp_plans SET state='executing' WHERE id=$1", [prepared.planId]); assert.equal((await application.query("COMMIT")).command, "COMMIT") + } catch (error) { await application.query("ROLLBACK"); throw error } + const beforeEntries = legacyEntries + const outcomes = await Promise.allSettled([0, 1].map(() => root.withMcpOperationExecutionOrigin(issued.admission, async () => { callbackContenders++ }))) + const finished = await terminal(issued.operation.id) + assert.equal(arrivals, 1); assert.deepEqual(claimRows, [1]); assert.equal(outcomes.filter(value => value.status === "rejected").length, 1) + assert.equal(legacyEntries - beforeEntries + callbackContenders, 1); assert.equal(finished.state, "unknown") + checks.push("actual creating-transaction INSERT owns private one-shot admission: two pending publishers enter one callback/one real CAS; separate rival case proves actual zero-row ACK isolation") + const later = await prepare(false), lateStart = root.beginMcpOperationAdmission(later.principal), latePlan = (await application.query("SELECT expires_at FROM public.mcp_plans WHERE id=$1", [later.planId])).rows[0] + const lateGrant = await readMcpTemporaryGrantOrigin(application, later.principal.connectionId, later.principal.credentialId), end = Math.min(latePlan.expires_at.getTime(), Date.parse(later.principal.credentialExpiresAt)) + await assert.rejects(root.insertMcpOperationAdmission(application, lateStart, later.planId, randomUUID(), { ...lateGrant, monotonicEnd: Infinity, admissionExpiresAt: end, admissionMonotonicEnd: performance.now() + end - Date.now() })) + assert.equal((await application.query("SELECT id FROM public.mcp_operations WHERE plan_id=$1", [later.planId])).rows.length, 0) + checks.push("later/autocommit caller cannot mint admission or orphan an INSERT; no existing-pending UPDATE mint API") + return { checks, actualPublicFixtureRunner: true, preLockCaptures, legacyEntries, serviceCalls, claimRows, actualSql: true, nativeCalls: 0, globalWriterActivated: false, + terminalRestoreRecoveryCovered: false, extraProductionPools: 0, extraProductionProcesses: 0, privateNonceExposed: false } + } finally { + proceed?.(); pool.query = query; Client.prototype.query = clientQuery + if (heldRival) await rival.query("SELECT pg_advisory_unlock(hashtextextended($1,0))", [heldRival]).catch(() => {}) + application.release(true); rival.release(true) + } +} diff --git a/backend/src/modules/mcp/operation-execution-origin.sql.test.ts b/backend/src/modules/mcp/operation-execution-origin.sql.test.ts new file mode 100644 index 00000000..63ee3e5e --- /dev/null +++ b/backend/src/modules/mcp/operation-execution-origin.sql.test.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState } from "../../services/bandwidth-admission-process.sql-helper.js" +assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_MCP_OPERATION_ORIGIN, "true") +await prepareOwnedBandwidthState() +const { pool, closePool } = await import("../../db/index.js") +try { + const { applySqlMigrations } = await import("../../db/migrate.js"); await applySqlMigrations(pool) + const { acceptOperationReplayTombstones } = await import("./operation-replay-tombstones.sql-helper.js") + await acceptOperationReplayTombstones() + const { acceptOwnedMcpOperationRoot } = await import("./operation-execution-origin.sql-helper.js") + console.log("Owned actual MCP operation root:", JSON.stringify(await acceptOwnedMcpOperationRoot())) +} finally { await closePool() } diff --git a/backend/src/modules/mcp/operation-execution-origin.test.ts b/backend/src/modules/mcp/operation-execution-origin.test.ts new file mode 100644 index 00000000..e969522e --- /dev/null +++ b/backend/src/modules/mcp/operation-execution-origin.test.ts @@ -0,0 +1,162 @@ +/** Real publisher/ALS/principal/policy/crypto with synthetic SQL boundaries. */ +import assert from "node:assert/strict" +import { test } from "node:test" +import { randomUUID } from "node:crypto" +import { mkdtemp, realpath, writeFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, dirname } from "node:path" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import type { McpPrincipal } from "./store.js" +import type { McpConnectionPolicy } from "@mmapp/contracts/mcp" +import { principalSourceRowsForTests } from "../../test/mcp-principal-source-rows.js" + +test("ordinary MCP operation publisher before legacy locks; actual private origin with synthetic SQL", async t => { + const parent = await realpath(tmpdir()), directory = await mkdtemp(join(parent, "mm-operation-root-")), prior = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = directory + const generation = Buffer.alloc(32, 91) + await writeFile(join(directory, "encryption-key"), Buffer.alloc(32, 83)); await writeFile(join(directory, "access-generation"), generation) + const { pool, closePool } = await import("../../db/index.js"), actualQuery = pool.query, actualConnect = pool.connect + const source = await import("./operation-execution-origin.js") + const { encryptSecret, fingerprint, redact } = await import("./security.js") + const { findActionByOperation } = await import("./actions.js") + const { withApplicationMutationLock, withConfigMutationLock } = await import("../../services/config-mutation-lock.js") + const { withOperationConfigurationLockClient } = await import("../../services/operation-config-lock-lease.js") + let row: Record & { id: string; plan_id: string; connection_id: string; operation_state: string; plan_state: string; result: Record | null; claim_nonce?: unknown; payload: { sealed: string }; owner_permissions: string[] } + let connection: Record & { owner_id: string; owner_permissions: string[]; owner_claims_issued_at: Date; owner_valid_until: Date; policy: McpConnectionPolicy } + let cleanups = 0, audits = 0, calls = 0 + let fault: "none" | "lost-own" | "lost-other" | "rows-only" = "none", claimGate: (() => Promise) | undefined + const clone = (value: T) => structuredClone(value) + const result = (command: string, rows: unknown[]) => ({ command, rowCount: rows.length, rows }) + async function query(raw: string | { text: string; values?: unknown[] }, values?: unknown[]) { + calls++; const text = typeof raw === "string" ? raw : raw.text, args = typeof raw === "string" ? values ?? [] : raw.values ?? [] + if (text.startsWith("/* mcp-principal:coherent-source-v1 */")) return result("SELECT", principalSourceRowsForTests(connection, + { credentialId: String(args[0]), credentialExpiresAt: connection.owner_valid_until.toISOString() }, { includeGrants: args[2] === true })) + if (text.startsWith("SELECT pg_current_xact_id_if_assigned")) return result("SELECT", [{ creating_xid: "123" }]) + if (text.startsWith("INSERT INTO mcp_operations")) { row.id = String(args[0]); row.plan_id = String(args[1]); row.connection_id = String(args[2]); return result("INSERT", [{ id: row.id, plan_id: row.plan_id, connection_id: row.connection_id, state: "pending", result: null }]) } + if (text.includes("AS creating_transaction")) return result("SELECT", [{ ...clone(row), creating_transaction: true }]) + if (text.startsWith("SELECT id FROM mcp_credentials")) return result("SELECT", [{ id: args[0] }]) + if (text.startsWith("SELECT * FROM mcp_connections")) return result("SELECT", [clone(connection)]) + if (text.startsWith("SELECT invalid_before FROM mcp_owner_revocations")) return result("SELECT", []) + if (text.startsWith("SELECT enabled FROM mcp_settings")) return result("SELECT", [{ enabled: true }]) + if (text.startsWith("/* mcp-operation-root:source */")) return result("SELECT", [clone(row)]) + if (text.includes("FROM public.mcp_grants")) return result("SELECT", []) + if (text.startsWith("/* mcp-operation-root:claim */")) { + await claimGate?.() + if (fault === "lost-other") { row.operation_state = "running"; row.claim_nonce = randomUUID(); row.result = { [source.MCP_OPERATION_PRIVATE_CLAIM_KEY]: row.claim_nonce }; throw Error("Real zero-row ACK lost after rival winner (synthetic driver)") } + if (row.operation_state !== "pending") return result("UPDATE", []) + row.operation_state = "running"; row.claim_nonce = args[3]; row.result = { [source.MCP_OPERATION_PRIVATE_CLAIM_KEY]: args[3] } + if (fault === "lost-own") throw Error("Applied own claim ACK lost (synthetic driver)") + const ack = result("UPDATE", [{ id: row.id, plan_id: row.plan_id, connection_id: row.connection_id, state: "running", result: clone(row.result) }]) + return fault === "rows-only" ? { rows: ack.rows } : ack + } + if (text.startsWith("UPDATE public.mcp_operations SET state='unknown'")) { + if (!(row.operation_state === "pending" && !row.claim_nonce || row.operation_state === "running" && row.claim_nonce === args[3])) return result("UPDATE", []) + cleanups++; row.operation_state = "unknown"; row.result = { needsReview: true, automaticRetry: false }; return result("UPDATE", [{ plan_id: row.plan_id }]) + } + if (text.startsWith("UPDATE public.mcp_plans")) { row.plan_state = "completed"; return result("UPDATE", []) } + if (text.startsWith("INSERT INTO mcp_audit")) { audits++; return result("INSERT", []) } + if (text === "BEGIN" || text === "COMMIT") return result(text, []) + if (text.includes("pg_try_advisory_lock")) return result("SELECT", [{ acquired: true }]) + if (text.includes("pg_advisory_unlock")) return result("SELECT", [{ released: true }]) + throw Error("Unexpected fixture SQL family") + } + pool.query = query as never + const client = { query, release() {}, on() {}, removeListener() {} } + pool.connect = (async () => client) as never + async function fixture(ttl = 30000) { + fault = "none"; claimGate = undefined + const principal: McpPrincipal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 60000).toISOString() } + const expires = Date.now() + ttl, id = randomUUID(), planId = randomUUID(), action = findActionByOperation("users.create") + const evidence = { target: { scope: "global" }, objects: [{ kind: "user", id: "owned-user" }], snapshots: [], warnings: [], steps: [0, 1].map(index => ({ kind: "service", service: "users.create", input: { index } })) } + const input = { user: { name: "owned private input" } }, payload = { sealed: await encryptSecret(JSON.stringify(input)) }, encryptedEvidence = { sealed: await encryptSecret(JSON.stringify(evidence)) } + connection = { id: principal.connectionId, name: "fixture", owner_id: "owned-root-owner", owner_permissions: ["mm:users:write"], generation: generation.toString("hex"), owner_claims_issued_at: new Date(Date.now() - 1000), owner_valid_until: new Date(expires + 1000), + policy: { id: principal.connectionId, version: 1, status: "active", expiresAt: new Date(expires + 1000).toISOString(), mode: "autonomous", permissions: ["mm:users:write"], scope: { serverIds: [], operationIds: ["users.create"], allowGlobal: true }, dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["users.create"], limits: { callsPerMinute: 60, concurrentReads: 4, mutationsPerHour: 10, serversPerPlan: 5, objectsPerPlan: 100 } } } + row = { id, plan_id: planId, connection_id: principal.connectionId, operation_state: "pending", plan_state: "executing", operation_id: "users.create", operation_version: action.operation.version, policy_version: 1, + generation: generation.toString("hex"), connection_generation: generation.toString("hex"), expires_at: new Date(expires), approved_by: null, owner_id: connection.owner_id, owner_permissions: connection.owner_permissions, owner_valid_until: connection.owner_valid_until, policy: connection.policy, + payload, evidence: encryptedEvidence, grants: [], protections: [], concurrent_reads: 0, result: null } + const grant = { proof: fingerprint({ credentialId: principal.credentialId, grant: null }), expiresAt: null, monotonicEnd: Infinity, admissionExpiresAt: expires, admissionMonotonicEnd: performance.now() + ttl } + const start = source.beginMcpOperationAdmission(principal), issued = await source.insertMcpOperationAdmission(client as never, start, planId, randomUUID(), grant) + return { id: issued.operation.id, principal, grant, input, evidence, admission: issued.admission } + } + const terminal = () => { row.operation_state = "applied"; row.plan_state = "completed"; row.result = { confirmedWrites: 2 } } + try { + await t.test("no ambient root, fake/copy tokens and unproved admission cannot mint", async () => { + await fixture(); const before = calls + await assert.rejects(source.captureMcpOperationExecutionOrigin()); await assert.rejects(source.readCurrentMcpOperationExecutionOrigin({})) + await assert.rejects(source.withMcpOperationExecutionOrigin({}, async () => { throw Error("must not enter") })); assert.equal(calls, before) + }) + await t.test("publisher exposes whole private operation, fixed lifetime and deeply frozen multi-step evidence", async () => { + const f = await fixture(); let saved: unknown, signal: AbortSignal | undefined + await source.withMcpOperationExecutionOrigin(f.admission, async () => { + const token = await source.captureMcpOperationExecutionOrigin(); saved = token + const metadata = await source.readCurrentMcpOperationExecutionOrigin(token), lifetime = await source.inspectMcpOperationExecutionLifetime(token); signal = lifetime.signal + assert.equal(metadata.operationId, f.id); assert.equal(metadata.evidence.steps.length, 2); assert.deepEqual(metadata.privateInput, f.input); assert.deepEqual(metadata.resources, f.evidence.objects) + assert(!("stepIndex" in metadata)); assert(Object.isFrozen(metadata.evidence.steps)); assert.equal(lifetime.monotonicDeadline, (await source.inspectMcpOperationExecutionLifetime(token)).monotonicDeadline) + const before = calls; for (const fake of [{}, { ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(source.readCurrentMcpOperationExecutionOrigin(fake)); assert.equal(calls, before) + terminal() + }) + assert.equal(signal?.aborted, true); await assert.rejects(source.readCurrentMcpOperationExecutionOrigin(saved)) + }) + await t.test("late capture denied under each actual legacy ALS, existing root remains inspectable", async () => { + const f = await fixture() + await source.withMcpOperationExecutionOrigin(f.admission, async () => { + const token = await source.captureMcpOperationExecutionOrigin() + await withApplicationMutationLock(async () => { await assert.rejects(source.captureMcpOperationExecutionOrigin()); await source.readCurrentMcpOperationExecutionOrigin(token) }) + await withConfigMutationLock([101, 102], async () => { await assert.rejects(source.captureMcpOperationExecutionOrigin()); await source.readCurrentMcpOperationExecutionOrigin(token) }) + await withOperationConfigurationLockClient(client as never, () => {}, new AbortController().signal, async () => { await assert.rejects(source.captureMcpOperationExecutionOrigin()); await source.readCurrentMcpOperationExecutionOrigin(token) }) + terminal() + }) + }) + await t.test("one-shot admitted dual workers cannot publish or enter legacy twice", async () => { + const f = await fixture(); let arrivals = 0, callbacks = 0 + claimGate = async () => { arrivals++ } + const outcomes = await Promise.allSettled([0, 1].map(() => source.withMcpOperationExecutionOrigin(f.admission, async () => { callbacks++; terminal() }))) + assert.equal(arrivals, 1); assert.equal(callbacks, 1); assert.equal(outcomes.filter(value => value.status === "rejected").length, 1); assert.equal(row.operation_state, "applied") + }) + for (const kind of ["lost-own", "lost-other", "rows-only"] as const) await t.test(kind + " claim refuses before legacy callback, exact nonce owns cleanup", async () => { + const f = await fixture(), before = cleanups; fault = kind; let entered = false + await assert.rejects(source.withMcpOperationExecutionOrigin(f.admission, async () => { entered = true })) + assert.equal(entered, false); assert.equal(cleanups - before, kind === "lost-other" ? 0 : 1); assert.equal(row.operation_state, kind === "lost-other" ? "running" : "unknown") + }) + await t.test("callback throw or nonterminal return closes only own running claim as unknown", async () => { + for (const fail of [false, true]) { const f = await fixture(), before = cleanups + const work = source.withMcpOperationExecutionOrigin(f.admission, async () => { if (fail) throw Error("Owned callback failed") }) + if (fail) await assert.rejects(work); else await work + assert.equal(row.operation_state, "unknown"); assert.equal(cleanups, before + 1) + } + assert(audits > 0) + }) + await t.test("same-byte directory clone and expired queued admission cannot revive or leave owned pending", async () => { + const f = await fixture(), cloneDirectory = await mkdtemp(join(parent, "mm-operation-root-clone-")) + try { + await writeFile(join(cloneDirectory, "access-generation"), generation); await writeFile(join(cloneDirectory, "encryption-key"), Buffer.alloc(32, 83)) + process.env.MCP_STATE_DIR = cloneDirectory + await assert.rejects(source.withMcpOperationExecutionOrigin(f.admission, async () => { throw Error("must not run from clone") })) + assert.equal(row.operation_state, "unknown") + } finally { process.env.MCP_STATE_DIR = directory; assert.equal(dirname(await realpath(cloneDirectory)), parent); await rm(cloneDirectory, { recursive: true, force: true }) } + const expired = await fixture(500); await delay(550) + await assert.rejects(source.withMcpOperationExecutionOrigin(expired.admission, async () => { throw Error("must not run expired") })); assert.equal(row.operation_state, "unknown") + }) + await t.test("current sealed source/claim nonce and current actor permission revoke retained root", async () => { + for (const fault of ["sealed", "nonce", "permission"] as const) { const f = await fixture() + await source.withMcpOperationExecutionOrigin(f.admission, async () => { + const token = await source.captureMcpOperationExecutionOrigin() + if (fault === "sealed") row.payload.sealed += "!" + if (fault === "nonce") { row.claim_nonce = randomUUID(); row.result = { [source.MCP_OPERATION_PRIVATE_CLAIM_KEY]: row.claim_nonce } } + if (fault === "permission") { connection.owner_permissions = []; row.owner_permissions = [] } + await assert.rejects(source.readCurrentMcpOperationExecutionOrigin(token)) + }) + } + }) + await t.test("private nonce stripped from direct result and nested model/UI redaction; terminal results unchanged", async () => { + const value = { confirmedWrites: 2, [source.MCP_OPERATION_PRIVATE_CLAIM_KEY]: "PRIVATE-NONCE" } + assert.deepEqual(source.stripMcpOperationExecutionPrivateResult(value), { confirmedWrites: 2 }) + assert.deepEqual(redact({ items: [{ result: value }], operation: { result: value } }), { items: [{ result: { confirmedWrites: 2 } }], operation: { result: { confirmedWrites: 2 } } }) + assert(!JSON.stringify(redact(value)).includes("PRIVATE-NONCE")); assert.deepEqual(source.stripMcpOperationExecutionPrivateResult({ confirmedWrites: 2 }), { confirmedWrites: 2 }) + }) + } finally { + pool.query = actualQuery; pool.connect = actualConnect; process.env.MCP_STATE_DIR = prior; await closePool() + assert.equal(dirname(await realpath(directory)), parent); await rm(directory, { recursive: true, force: true }) + } +}) diff --git a/backend/src/modules/mcp/operation-execution-origin.ts b/backend/src/modules/mcp/operation-execution-origin.ts new file mode 100644 index 00000000..0b134ad5 --- /dev/null +++ b/backend/src/modules/mcp/operation-execution-origin.ts @@ -0,0 +1,314 @@ +/** Ordinary runner root BEFORE operation/app/server configuration locks. This + * is provenance, never a global-writer permit or a public callback/JSON API. */ +import { AsyncLocalStorage } from "node:async_hooks" +import { createHash, randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import { readFileSync } from "node:fs" +import { join } from "node:path" +import { performance } from "node:perf_hooks" +import type { QueryConfig, PoolClient } from "pg" +import { mcpTemporaryGrantSchema, type McpObjectRef } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import { sharedDatabaseGate } from "../../services/shared-database-gate.js" +import { assertNoConfigurationLockContext } from "../../services/config-mutation-lock.js" +import { assertNoOperationConfigurationLockContext } from "../../services/operation-config-lock-lease.js" +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 { changeMutationCost } from "./change-cost.js" +import { assertOriginalMcpTemporaryGrant, type OriginalServiceGrant } from "./temporary-grant-origin.js" +import type { ChangeEvidence } from "./action-types.js" + +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 + readonly sealedEvidenceProof: string; readonly sealedPayloadProof: string + readonly originalTemporaryGrantProof: string; readonly originalTemporaryGrantExpiresAt: number | null; readonly originalExpiresAt: number + readonly serverIds: readonly number[]; readonly resources: readonly McpObjectRef[] + readonly privateInput: Readonly>; readonly evidence: Readonly +} +declare const operationOriginBrand: unique symbol +export type McpOperationExecutionOrigin = Readonly<{ [operationOriginBrand]: true }> +type Entry = { active: boolean; nonce: string; metadata: Readonly; grant: Readonly + end: number; controller: AbortController; timer?: ReturnType; directory: string; keyProof: string; keyEpoch: string } +const roots = new AsyncLocalStorage(), tokens = new WeakMap() +declare const admissionStartBrand: unique symbol +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; grant: Readonly + generation: string; directory: string; keyEpoch: string; operationName: string; operationVersion: number; policyVersion: number; evidenceProof: string; payloadProof: string } +const starts = new WeakMap; wall: number; mono: number; directory: string; generation: string; keyEpoch: string }>(), admissions = new WeakMap() +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" +const sha = (value: string) => createHash("sha256").update(value).digest("hex") +function existingMaterial(directory: string) { + if (mcpStateDirectory() !== directory) return deny() + const generation = readFileSync(join(directory, "access-generation")), key = readFileSync(join(directory, "encryption-key")) + try { + if (generation.length !== 32 || key.length !== 32 || mcpStateDirectory() !== directory) return deny() + return { generation: generation.toString("hex"), keyEpoch: createHash("sha256").update(key).digest("hex") } + } finally { key.fill(0) } +} +function frozen(value: T): T { if (value && typeof value === "object") { Object.values(value).forEach(frozen); Object.freeze(value) }; return value } +export function stripMcpOperationExecutionPrivateResult(value: unknown): unknown { + if (!value || typeof value !== "object" || Array.isArray(value)) return value + return Object.fromEntries(Object.entries(value).filter(([key]) => key !== MCP_OPERATION_PRIVATE_CLAIM_KEY)) +} +const SOURCE_SQL = `/* mcp-operation-root:source */ SELECT o.id,o.plan_id,o.connection_id,o.state AS operation_state, + o.result->>'__mmappMcpOperationClaimV1' AS claim_nonce,p.operation_id,p.operation_version,p.policy_version,p.generation, + p.state AS plan_state,p.expires_at,p.approved_by, + CASE WHEN octet_length(p.payload::text)+octet_length(p.evidence::text)<=8388608 THEN p.payload END AS payload, + CASE WHEN octet_length(p.payload::text)+octet_length(p.evidence::text)<=8388608 THEN p.evidence END AS evidence, + c.owner_id,c.owner_permissions,c.policy,c.owner_valid_until,c.generation AS connection_generation, + coalesce((SELECT jsonb_agg(g) FROM (SELECT id::text,CASE WHEN octet_length(payload::text)<=1048576 THEN payload END AS payload FROM public.mcp_grants WHERE connection_id=o.connection_id + AND revoked_at IS NULL AND payload->>'credentialId'=$3 LIMIT 2)g),'[]'::jsonb) AS grants, + (WITH limited AS MATERIALIZED (SELECT object_ref FROM public.mcp_protections LIMIT 1001), + budget AS (SELECT coalesce(sum(octet_length(object_ref::text)),0) AS bytes FROM limited) + SELECT CASE WHEN bytes<=1048576 THEN (SELECT coalesce(jsonb_agg(object_ref),'[]'::jsonb) FROM limited) END FROM budget) AS protections, + (SELECT count(*)::int FROM public.mcp_read_leases WHERE connection_id=o.connection_id AND expires_at>now()) AS concurrent_reads + FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_connections c ON c.id=o.connection_id WHERE o.id=$1 AND o.connection_id=$2 LIMIT 2` +const CLAIM_SQL = `/* mcp-operation-root:claim */ UPDATE public.mcp_operations o SET state='running', + result=coalesce(o.result,'{}'::jsonb)||jsonb_build_object('__mmappMcpOperationClaimV1',$4::text) + FROM public.mcp_plans p WHERE o.id=$1 AND o.plan_id=$2 AND o.connection_id=$3 AND o.state='pending' + AND (o.result IS NULL OR jsonb_typeof(o.result)='object' AND NOT o.result ? '__mmappMcpOperationClaimV1') + AND p.id=o.plan_id AND p.connection_id=o.connection_id AND p.state='executing' AND p.operation_version=$5 AND p.policy_version=$6 + AND p.generation=$7 AND p.expires_at>now() AND encode(sha256(convert_to(p.evidence->>'sealed','UTF8')),'hex')=$8 + AND encode(sha256(convert_to(p.payload->>'sealed','UTF8')),'hex')=$9 RETURNING o.id,o.plan_id,o.connection_id,o.state,o.result` +function life(entry: Entry) { if (entry.controller.signal.aborted || Date.now() >= entry.metadata.originalExpiresAt || performance.now() >= entry.end) deny() } +function owned(entry: Entry) { if (!entry.active || roots.getStore() !== entry) deny(); life(entry) } +async function files(entry: Entry) { + life(entry); if (mcpStateDirectory() !== entry.directory) deny() + const bytes = await readFile(join(entry.directory, "access-generation")); life(entry) + if (bytes.length !== 32 || bytes.toString("hex") !== entry.metadata.generation || mcpStateDirectory() !== entry.directory) deny() + if ((await privateFingerprinterExisting(purpose))(keyMessage) !== entry.keyProof || mcpStateDirectory() !== entry.directory) deny(); life(entry) +} +async function sql(text: string, values: unknown[], deadlineAt: number) { + if (Date.now() >= deadlineAt) return deny() + const config: QueryConfig & { query_timeout: number } = { text, values, query_timeout: Math.max(1, Math.min(5000, deadlineAt - Date.now())) } + return pool.query(config) +} +async function rowFor(operationId: string, principal: McpPrincipal, deadlineAt: number) { + const result = await sql(SOURCE_SQL, [operationId, principal.connectionId, principal.credentialId], deadlineAt) + if (result.command !== "SELECT" || result.rowCount !== 1 || result.rows.length !== 1) return deny() + return result.rows[0] +} +/** Timing anchor only, not authority. The fixed INSERT below is the only minter. */ +export function beginMcpOperationAdmission(principal: McpPrincipal): McpOperationAdmissionStart { + const wall = Date.now(), mono = performance.now(), directory = mcpStateDirectory(), material = existingMaterial(directory) + const token = Object.freeze(Object.create(null)) as McpOperationAdmissionStart + starts.set(token, { principal: frozen(structuredClone(principal)), wall, mono, directory, ...material }); return token +} +/** Trusted creating-transaction seam. This method owns the fresh INSERT and its + * real receipt; it cannot mint from an existing pending row or a later UPDATE. + * xmin is an additional same-transaction pin, not protection from a DB owner. */ +export async function insertMcpOperationAdmission(client: PoolClient, start: McpOperationAdmissionStart, planId: string, idempotencyKey: string, + original: OriginalServiceGrant): Promise<{ operation: StoredOperation; admission: McpOperationAdmission; originalGrant: Readonly }> { + const anchor = starts.get(start); starts.delete(start) + if (!anchor || !original || original.admissionExpiresAt <= Date.now()) return deny() + const material = existingMaterial(anchor.directory) + if (material.generation !== anchor.generation || material.keyEpoch !== anchor.keyEpoch) return deny() + const transaction = await client.query("SELECT pg_current_xact_id_if_assigned()::text AS creating_xid") + if (transaction.command !== "SELECT" || transaction.rowCount !== 1 || typeof transaction.rows[0]?.creating_xid !== "string") return deny() + const beforePrincipal = existingMaterial(anchor.directory) + if (beforePrincipal.generation !== anchor.generation || beforePrincipal.keyEpoch !== anchor.keyEpoch) return deny() + const principal = anchor.principal, current = await validatePrincipal(principal, client) + await assertOriginalMcpTemporaryGrant(client, principal.connectionId, principal.credentialId, original) + const beforeInsert = existingMaterial(anchor.directory) + if (beforeInsert.generation !== anchor.generation || beforeInsert.keyEpoch !== anchor.keyEpoch) return deny() + const id = randomUUID(), inserted = await client.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key) VALUES($1,$2,$3,$4) RETURNING *", [id, planId, principal.connectionId, idempotencyKey]) + if (inserted.command !== "INSERT" || inserted.rowCount !== 1 || inserted.rows.length !== 1 || inserted.rows[0].id !== id || inserted.rows[0].plan_id !== planId + || inserted.rows[0].connection_id !== principal.connectionId || inserted.rows[0].state !== "pending") return deny() + const pin = await client.query(`SELECT o.id,o.plan_id,o.connection_id,o.xmin::text=mod(pg_current_xact_id()::text::numeric,4294967296)::text AS creating_transaction, + p.operation_id,p.operation_version,p.policy_version,p.generation, + CASE WHEN octet_length(p.payload::text)+octet_length(p.evidence::text)<=8388608 THEN p.evidence END AS evidence, + CASE WHEN octet_length(p.payload::text)+octet_length(p.evidence::text)<=8388608 THEN p.payload END AS payload,p.expires_at + FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id WHERE o.id=$1`, [id]) + if (pin.command !== "SELECT" || pin.rowCount !== 1 || pin.rows.length !== 1) return deny() + const row = pin.rows[0] + if (row.creating_transaction !== true || row.id !== id || row.plan_id !== planId || row.connection_id !== principal.connectionId || row.generation !== current.generation + || row.generation !== anchor.generation || row.policy_version !== current.policy.version || !(row.expires_at instanceof Date) || typeof row.evidence?.sealed !== "string" || typeof row.payload?.sealed !== "string") return deny() + const finalMaterial = existingMaterial(anchor.directory) + if (finalMaterial.generation !== anchor.generation || finalMaterial.keyEpoch !== anchor.keyEpoch) return deny() + const expiresAt = Math.min(original.admissionExpiresAt, row.expires_at.getTime(), Date.parse(principal.credentialExpiresAt), Date.parse(current.policy.expiresAt), current.owner_valid_until.getTime()) + const grant = frozen({ ...original, admissionExpiresAt: expiresAt, + admissionMonotonicEnd: Math.min(original.admissionMonotonicEnd, anchor.mono + Math.max(0, expiresAt - anchor.wall)), + monotonicEnd: original.expiresAt === null ? Infinity : Math.min(original.monotonicEnd, anchor.mono + Math.max(0, original.expiresAt - anchor.wall)) }) + if (Date.now() >= expiresAt || performance.now() >= grant.admissionMonotonicEnd) return deny() + 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) }) + return { operation: inserted.rows[0], admission, originalGrant: grant } +} +function grantFrom(row: Record, principal: McpPrincipal) { + if (!Array.isArray(row.grants) || row.grants.length > 1) return deny() + if (!row.grants.length) return { proof: fingerprint({ credentialId: principal.credentialId, grant: null }), expiresAt: null, value: undefined } + const entry = row.grants[0] + if (!entry || typeof entry.id !== "string" || !entry.payload || entry.payload.credentialId !== principal.credentialId) return deny() + const { credentialId: _credential, ...raw } = entry.payload, value = mcpTemporaryGrantSchema.parse(raw) + if (Date.parse(value.startsAt) > Date.now() || Date.parse(value.expiresAt) <= Date.now()) return deny() + return { proof: fingerprint({ rowId: entry.id, credentialId: principal.credentialId, grant: value }), expiresAt: Date.parse(value.expiresAt), value } +} +function source(entry: Entry, row: Record, state: "pending" | "running") { + const m = entry.metadata + if (row.id !== m.operationId || row.plan_id !== m.planId || row.connection_id !== m.connectionId || row.operation_state !== state + || 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 + || !(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 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[0]["policy"] + const protections = row.protections as Parameters[0]["protections"] + if (policy.version !== m.policyVersion || !(row.owner_valid_until instanceof Date) || row.owner_valid_until.getTime() < m.originalExpiresAt) deny() + 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 }] : [])], + 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 })) : [])], + 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() +} +/** Two current joined snapshots around real principal/grant checks. Nothing is + * cached across calls; the final SQL validates exact source + policy + grant. */ +async function current(entry: Entry, state: "pending" | "running") { + await files(entry) + const initialMaterial = existingMaterial(entry.directory); if (initialMaterial.generation !== entry.metadata.generation || initialMaterial.keyEpoch !== entry.keyEpoch) deny() + const before = await validatePrincipal(entry.metadata.principal); life(entry) + if (before.owner_id !== entry.metadata.ownerId) deny() + source(entry, await rowFor(entry.metadata.operationId, entry.metadata.principal, entry.metadata.originalExpiresAt), state); life(entry) + await assertOriginalMcpTemporaryGrant(pool, entry.metadata.connectionId, entry.metadata.principal.credentialId, entry.grant); life(entry) + await files(entry) + const finalMaterial = existingMaterial(entry.directory); if (finalMaterial.generation !== entry.metadata.generation || finalMaterial.keyEpoch !== entry.keyEpoch) deny() + const after = await validatePrincipal(entry.metadata.principal); life(entry) + if (after.owner_id !== entry.metadata.ownerId) deny() + source(entry, await rowFor(entry.metadata.operationId, entry.metadata.principal, entry.metadata.originalExpiresAt), state); life(entry) +} +/** Private owner nonce prevents an uncertain zero-row claimant from closing a + * different winner. No pending reset, verified ACK fabrication or native retry. */ +async function interrupt(entry: Admission) { + const deadline = Date.now() + 2500; let client: PoolClient | undefined, safe = false, retired = false, timeout: ReturnType | undefined + let stage = "checkout" + const connect = pool.connect().then(value => { if (retired || Date.now() >= deadline) { value.release(true); throw Error("Late cleanup checkout") }; client = value; return value }) + const stopped = new Promise((_yes, no) => { timeout = setTimeout(() => { retired = true; client?.release(true); client = undefined; no(Error("Operation root cleanup deadline")) }, 2500) }) + const work = async () => { + const owned = await connect + const query = (text: string, values: unknown[] = []) => { + if (retired || Date.now() >= deadline || !sharedDatabaseGate.hasActiveAdmission()) throw Error("Late cleanup SQL") + return owned.query({ text, values, query_timeout: Math.max(1, Math.min(1000, deadline - Date.now())) } as QueryConfig) + } + stage = "begin" + if ((await query("BEGIN")).command !== "BEGIN") throw Error("No cleanup BEGIN ACK") + stage = "operation" + const result = await query(`UPDATE public.mcp_operations SET state='unknown',verification='inconclusive',error_code='PROCESS_INTERRUPTED', + result=jsonb_build_object('needsReview',true,'automaticRetry',false),finished_at=now() WHERE id=$1 AND plan_id=$2 AND connection_id=$3 + AND ((state='pending' AND result->>'__mmappMcpOperationClaimV1' IS NULL) + OR (state='running' AND result->>'__mmappMcpOperationClaimV1'=$4)) RETURNING plan_id`, [entry.operationId, entry.planId, entry.connectionId, entry.nonce]) + if (result.rows.length) { + stage = "plan" + await query("UPDATE public.mcp_plans SET state='completed' WHERE id=$1 AND connection_id=$2", [entry.planId, entry.connectionId]) + stage = "audit" + await audit({ query } as Pick, "mcp-runner", "operation.interrupted", entry.connectionId, + { operationId: entry.operationId, planId: entry.planId, origin: "operation-root-v1", retryAllowed: false, sealedEvidenceProof: entry.evidenceProof }) + } + stage = "commit" + if ((await query("COMMIT")).command !== "COMMIT") throw Error("No cleanup COMMIT ACK") + safe = true + } + try { await Promise.race([work(), stopped]) } catch (error) { + const sqlState = (error as { code?: unknown } | null)?.code + const category = retired ? "deadline" : error instanceof Error && error.message === "Query read timeout" ? "query-timeout" : "failure" + console.error("MCP operation root cleanup deferred", { operationId: entry.operationId, stage, category, sqlState: typeof sqlState === "string" && /^[0-9A-Z]{5}$/.test(sqlState) ? sqlState : undefined, retryAllowed: false }) + } + finally { retired = true; clearTimeout(timeout); if (client) { client.release(!safe); client = undefined } } +} +/** Cleanup SQL owns a live request/exclusive admission. Waiting for repair is + * detached from runner slots; only admission is retried, never native work. */ +async function interruptWithinDatabaseAdmission(entry: Admission): Promise { + if (sharedDatabaseGate.hasActiveAdmission()) return sharedDatabaseGate.withinAdmission(() => interrupt(entry)) + if (!sharedDatabaseGate.isPaused()) return sharedDatabaseGate.request(() => interrupt(entry)) + void (async () => { + while (sharedDatabaseGate.isPaused()) await sharedDatabaseGate.whenOpen() + await sharedDatabaseGate.request(() => interrupt(entry)) + })().catch(() => console.error("MCP operation root cleanup deferred", { operationId: entry.operationId, retryAllowed: false })) +} +/** Aborted admission/queue ownership only. A serialized/foreign cap is inert. */ +export async function retireMcpOperationAdmission(admission: unknown): Promise { + const entry = admission && typeof admission === "object" ? admissions.get(admission) : undefined + if (!entry) return + admissions.delete(admission as object); await interruptWithinDatabaseAdmission(entry) +} +/** Called exactly by the ordinary runtime before its first pool checkout/lock. + * The callback is fixed runner code; callers supply no metadata/authorizer. */ +export async function withMcpOperationExecutionOrigin(admission: unknown, work: () => Promise): Promise { + assertNoOperationConfigurationLockContext(); assertNoConfigurationLockContext() + const admitted = admission && typeof admission === "object" ? admissions.get(admission) : undefined + if (!admitted) return deny() + admissions.delete(admission as object) // One shot even after any failed attempt. + let entry: Entry | undefined, interruptOwn = true + try { + const { operationId, principal, grant: originalGrant } = admitted + if (roots.getStore() || originalGrant.admissionExpiresAt <= Date.now() || originalGrant.admissionMonotonicEnd <= performance.now()) return deny() + const capturedPrincipal = frozen(structuredClone(principal)), directory = admitted.directory, material = existingMaterial(directory) + if (material.generation !== admitted.generation || material.keyEpoch !== admitted.keyEpoch) return deny() + const bytes = await readFile(join(directory, "access-generation")); if (bytes.length !== 32) return deny() + const sign = await privateFingerprinterExisting(purpose), keyProof = sign(keyMessage), generation = bytes.toString("hex") + const beforeAuthorizer = existingMaterial(directory); if (beforeAuthorizer.generation !== admitted.generation || beforeAuthorizer.keyEpoch !== admitted.keyEpoch) return deny() + const connection = await validatePrincipal(capturedPrincipal) + const row = await rowFor(operationId, capturedPrincipal, originalGrant.admissionExpiresAt) + 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() + const evidence = JSON.parse(await decryptSecret(row.evidence.sealed)) as ChangeEvidence, privateInput = JSON.parse(await decryptSecret(row.payload.sealed)) as Record + 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), + 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 }) + entry = { active: false, nonce: admitted.nonce, metadata, grant: frozen({ ...originalGrant }), directory, keyProof, keyEpoch: admitted.keyEpoch, + end: Math.min(originalGrant.admissionMonotonicEnd, performance.now() + Math.max(0, expiresAt - Date.now())), controller: new AbortController() } + const active = entry + const tick = () => { const remaining = Math.min(expiresAt - Date.now(), active.end - performance.now()); if (!Number.isFinite(remaining) || remaining <= 0) active.controller.abort(); else { active.timer = setTimeout(tick, Math.max(1, Math.min(2147483647, remaining))); active.timer.unref() } } + tick() + await current(entry, "pending"); life(entry) + const result = await sql(CLAIM_SQL, [operationId, metadata.planId, metadata.connectionId, entry.nonce, metadata.operationVersion, metadata.policyVersion, generation, metadata.sealedEvidenceProof, metadata.sealedPayloadProof], expiresAt) + if (result.command !== "UPDATE" || !Number.isSafeInteger(result.rowCount) || !Array.isArray(result.rows)) throw Error("Operation claim ACK unavailable") + if (result.rowCount === 0 && result.rows.length === 0) return // No publication; cleanup can close only own still-pending creation, never a rival nonce. + if (result.rowCount !== 1 || result.rows.length !== 1 || result.rows[0].id !== operationId || result.rows[0].plan_id !== metadata.planId || result.rows[0].connection_id !== metadata.connectionId + || result.rows[0].state !== "running" || result.rows[0].result?.[MCP_OPERATION_PRIVATE_CLAIM_KEY] !== entry.nonce) throw Error("Operation claim ACK mismatch") + life(entry); await current(entry, "running"); life(entry) + entry.active = true + await roots.run(entry, work) + } finally { + if (entry) { entry.active = false; clearTimeout(entry.timer); entry.controller.abort() } + if (interruptOwn) await interruptWithinDatabaseAdmission(admitted) + } +} +export async function captureMcpOperationExecutionOrigin(): Promise { + assertNoOperationConfigurationLockContext(); assertNoConfigurationLockContext() + const entry = roots.getStore(); if (!entry) return deny(); owned(entry) + await current(entry, "running"); owned(entry) + const token = Object.freeze(Object.create(null)) as McpOperationExecutionOrigin; tokens.set(token, entry); return token +} +export async function readCurrentMcpOperationExecutionOrigin(token: unknown): Promise> { + const entry = token && typeof token === "object" ? tokens.get(token) : undefined + if (!entry) return deny(); owned(entry); await current(entry, "running"); owned(entry); return entry.metadata +} +export async function inspectMcpOperationExecutionLifetime(token: unknown): Promise> { + const entry = token && typeof token === "object" ? tokens.get(token) : undefined + if (!entry) return deny(); owned(entry); await current(entry, "running"); owned(entry) + return Object.freeze({ expiresAt: entry.metadata.originalExpiresAt, monotonicDeadline: entry.end, signal: entry.controller.signal }) +} diff --git a/backend/src/modules/mcp/operation-replay-tombstones.sql-helper.ts b/backend/src/modules/mcp/operation-replay-tombstones.sql-helper.ts new file mode 100644 index 00000000..e6dddc2f --- /dev/null +++ b/backend/src/modules/mcp/operation-replay-tombstones.sql-helper.ts @@ -0,0 +1,74 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { assertOwnedBandwidthSql } from "../../services/bandwidth-admission-process.sql-helper.js" + +export async function acceptOperationReplayTombstones() { + assertOwnedBandwidthSql() + assert.equal(process.env.MMAPP_OWN_MCP_OPERATION_ORIGIN, "true") + const { pool } = await import("../../db/index.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { createMcpConnection, authenticateMcpToken } = await import("./store.js") + const { applyPlan, invokeMcpTool } = await import("./runtime.js") + const { operationIdempotencyDigest } = await import("./operation-replay-tombstones.js") + const { encryptSecret, McpError } = await import("./security.js") + const { default: Fastify } = await import("fastify") + const { default: authPlugin } = await import("../../plugins/auth.js") + const { createMcpRoutes } = await import("../../routes/mcp.js") + const { env } = await import("../../config.js") + const { SignJWT } = await import("jose") + const makeConnection = async () => { + const actor = { id: randomUUID(), email: "owned-tombstone@example.test", name: "Owned replay fixture", apps: ["mm"], + permissions: ["mm:users:write", "mm:mcp:read"], issuedAt: Date.now()-1000, expiresAt: Date.now()+300000 } + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: actor.name, mode: "approval", + permissions: actor.permissions, expiresAt: new Date(actor.expiresAt).toISOString(), + scope: { serverIds: [], operationIds: ["users.create", "operations.read", "changes.apply"], allowGlobal: true }, + limits: { callsPerMinute: 100, mutationsPerHour: 10 } }), actor) + return { actor, principal: await authenticateMcpToken(issued.token) } + } + const app = Fastify() + const ids: string[] = [] + const matches = (code: string, status: number) => (error: unknown) => + error instanceof McpError && error.code === code && error.statusCode === status + await pool.query("UPDATE public.mcp_settings SET enabled=true WHERE id=1") + try { + const owner = await makeConnection(), foreign = await makeConnection() + // A real completed plan has already initialized the owned encryption state. + // The fixture seeds its tombstone directly, so exercise the same initializer. + await encryptSecret("owned replay fixture state") + const connectionId = owner.principal.connectionId + ids.push(connectionId, foreign.principal.connectionId) + const operationId = randomUUID(), planId = randomUUID(), key = "owned-replay-" + randomUUID() + await pool.query(`INSERT INTO public.mcp_operation_replay_tombstones + (operation_id,plan_id,connection_id,idempotency_digest,action_id,target_server_ids,target_global) + VALUES($1,$2,$3,$4,'users.create','{}',true)`, [operationId, planId, connectionId, operationIdempotencyDigest(connectionId,key)]) + await assert.rejects(applyPlan(owner.principal, planId, key), matches("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(applyPlan(owner.principal, planId, randomUUID()), matches("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(applyPlan(owner.principal, randomUUID(), key), matches("STATE_CONFLICT",409)) + await assert.rejects(applyPlan(foreign.principal, planId, key), matches("NOT_FOUND",404)) + await assert.rejects(invokeMcpTool(owner.principal,"mm_change_apply",{ planId,idempotencyKey:key }), matches("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(invokeMcpTool(owner.principal,"mm_operation_get",{ operationId }), matches("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(invokeMcpTool(foreign.principal,"mm_operation_get",{ operationId }), matches("NOT_FOUND",404)) + await app.register(authPlugin) + await app.register(createMcpRoutes(async () => {})) + for (const subject of [owner, foreign]) { + const token = await new SignJWT({ sub: subject.actor.id, email: subject.actor.email, name: subject.actor.name, + apps: subject.actor.apps, permissions: subject.actor.permissions }) + .setProtectedHeader({ alg:"HS256" }).setIssuer(env.authIssuer).setIssuedAt().setExpirationTime("4m") + .sign(new TextEncoder().encode(env.jwtSecret)) + const reply = await app.inject({ method: "POST", url: "/mcp/plans/"+planId+"/apply", headers: { authorization:"Bearer "+token } }) + assert.equal(reply.statusCode, subject === owner ? 410 : 404) + assert.equal(reply.json().code, subject === owner ? "OPERATION_HISTORY_EXPIRED" : "NOT_FOUND") + } + await pool.query("UPDATE public.mcp_connections SET policy=jsonb_set(policy,'{scope,allowGlobal}','false') WHERE id=$1", [connectionId]) + await assert.rejects(applyPlan(owner.principal, planId, key), matches("NOT_FOUND",404)) + await assert.rejects(invokeMcpTool(owner.principal,"mm_operation_get",{ operationId }), matches("NOT_FOUND",404)) + const { rows:[proof] } = await pool.query(`SELECT + NOT EXISTS(SELECT 1 FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[])) AS no_operation, + NOT EXISTS(SELECT 1 FROM public.mcp_budget_buckets WHERE connection_id=ANY($1::uuid[]) AND kind='mutations') AS no_write_budget`, [ids]) + assert.deepEqual(proof,{no_operation:true,no_write_budget:true}) + console.log("Owned operation tombstones: scoped MCP and Web 410, replay conflict and no dispatch") + } finally { + await app.close() + await pool.query("DELETE FROM public.mcp_operation_replay_tombstones WHERE connection_id=ANY($1::uuid[])", [ids]) + } +} diff --git a/backend/src/modules/mcp/operation-replay-tombstones.ts b/backend/src/modules/mcp/operation-replay-tombstones.ts new file mode 100644 index 00000000..651ab64b --- /dev/null +++ b/backend/src/modules/mcp/operation-replay-tombstones.ts @@ -0,0 +1,64 @@ +import { createHash } from "node:crypto" +import type { PoolClient } from "pg" +import { hasPermission } from "../../lib/permissions.js" +import { listActions } from "./actions.js" +import { McpError } from "./security.js" +import { effectiveMcpConnection, type ConnectionRow, type McpPrincipal } from "./store.js" + +type Query = Pick +export interface OperationReplayTombstone { + operation_id: string + plan_id: string + connection_id: string + action_id: string + target_server_ids: string[] + target_global: boolean +} + +// Idempotency tokens are caller input and already stored in plaintext on live +// operations. This domain-separated digest avoids copying that plaintext and +// survives DB restore and access-key rotation. It is not a keyed secret proof. +export function operationIdempotencyDigest(connectionId: string, key: string): string { + return createHash("sha256").update("mcp-operation-replay-v1\0").update(connectionId).update("\0").update(key).digest("hex") +} + +export function operationHistoryExpired(): never { + throw new McpError("OPERATION_HISTORY_EXPIRED", "Срок хранения завершённой операции истёк. Повторное применение запрещено.", 410) +} + +export function assertOperationReplayScope(row: OperationReplayTombstone, connection: ConnectionRow): void { + const scope = connection.policy.scope + if (row.connection_id !== connection.id || !scope.operationIds.includes(row.action_id)) throw operationReplayNotFound() + const action = listActions().find(candidate => candidate.operation.id === row.action_id) + if (!action || action.operation.requiredPermissions.some(permission => + !hasPermission(connection.policy.permissions, permission) || !hasPermission(connection.owner_permissions, permission))) throw operationReplayNotFound() + if (row.target_global && !scope.allowGlobal || row.target_server_ids.some(id => !scope.serverIds.some(server => String(server) === id))) throw operationReplayNotFound() +} + +function operationReplayNotFound(): McpError { + return new McpError("NOT_FOUND", "Операция не найдена", 404) +} + +const projection = "t.operation_id,t.plan_id,t.connection_id,t.action_id,t.target_server_ids::text[] AS target_server_ids,t.target_global" + +export async function findOperationReplayTombstone(db: Query, connectionId: string, operationId: string) { + return (await db.query(`SELECT ${projection} FROM mcp_operation_replay_tombstones t + WHERE t.connection_id=$1 AND t.operation_id=$2`, [connectionId, operationId])).rows[0] +} + +export async function findOwnerPlanReplayTombstone(db: Query, ownerId: string, planId: string) { + return (await db.query(`SELECT ${projection} FROM mcp_operation_replay_tombstones t + JOIN mcp_connections c ON c.id=t.connection_id WHERE c.owner_id=$1 AND t.plan_id=$2`, [ownerId, planId])).rows[0] +} + +export async function refuseExpiredOperationReplay(db: Query, principal: McpPrincipal, connectionId: string, planId: string, key: string): Promise { + const rows = (await db.query(`SELECT ${projection} FROM mcp_operation_replay_tombstones t + WHERE t.connection_id=$1 AND (t.plan_id=$2 OR t.idempotency_digest=$3)`, + [connectionId, planId, operationIdempotencyDigest(connectionId, key)])).rows + if (!rows.length) return + const conflict = rows.find(row => row.plan_id !== planId.toLowerCase()) + const current = await effectiveMcpConnection(principal, db) + assertOperationReplayScope(conflict ?? rows[0], current) + if (conflict) throw new McpError("STATE_CONFLICT", "Ключ уже используется другим планом", 409) + operationHistoryExpired() +} diff --git a/backend/src/modules/mcp/operation-revisions.test.ts b/backend/src/modules/mcp/operation-revisions.test.ts new file mode 100644 index 00000000..51dd8bdd --- /dev/null +++ b/backend/src/modules/mcp/operation-revisions.test.ts @@ -0,0 +1,72 @@ +import assert from "node:assert/strict" +import { findAction } from "./actions.js" +import { findMcpOperation } from "./registry.js" + +const action = findAction("mm_ospf_optimizer_plan") +const fullFlowSettings = findAction("mm_netflow_settings_update_plan") +assert.equal(fullFlowSettings.operation.id, "netflow.settings.update") +assert.equal(fullFlowSettings.operation.version, 3) +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, 2) +assert.equal(findAction("mm_ipsec_server_init_plan").operation.version, 4) +assert.equal(findAction("mm_ipsec_server_remove_plan").operation.version, 2) +assert.equal(findAction("mm_ipsec_revision_restore_plan").operation.version, 1) +assert.equal(findAction("mm_wireguard_revision_restore_plan").operation.id, "config.wireguard.revisions.restore") +assert.equal(findAction("mm_vrf_update_plan").schema.safeParse({ serverId: 1, id: "*1", fields: { comment: "reviewed" }, productionAdmission: true }).success, true) +assert.equal(findAction("mm_vrf_update_plan").schema.safeParse({ serverId: 1, id: "*1", fields: {}, productionAdmission: false }).success, false) +const rebuild = findAction("mm_netflow_facts_rebuild_plan") +assert.equal(rebuild.operation.id, "traffic.flow.facts.rebuild") +assert.equal(rebuild.operation.version, 1) +assert.equal(findMcpOperation(rebuild.operation.id)?.version, 1) +assert.equal(rebuild.operation.scope, "global") +assert.deepEqual(rebuild.operation.requiredPermissions, ["mm:traffic:write"]) +assert.deepEqual(rebuild.schema.parse({}), {}) +for (const input of [{ serverIds: [1] }, { jobId: "caller" }, { configProof: "forged" }, { serverIds: undefined }]) assert.throws(() => rebuild.schema.parse(input)) +const rebuildReconcile = findAction("mm_netflow_facts_rebuild_reconcile") +assert.equal(rebuildReconcile.operation.id, "traffic.flow.facts.rebuild.reconcile") +assert.equal(rebuildReconcile.operation.version, 1) +assert.equal(rebuildReconcile.operation.effect, "read") +assert.equal(rebuildReconcile.operation.scope, "global") +assert.deepEqual(rebuildReconcile.operation.requiredPermissions, ["mm:traffic:write"]) +assert.throws(() => rebuildReconcile.schema.parse({ jobId: "caller" })) +assert.equal(action.operation.version, 13) +assert.equal(findMcpOperation(action.operation.id)?.version, 13) +assert.throws(() => action.schema.parse({ serverId: 1, expectedServerProof: "forged-source" })) +assert.throws(() => action.schema.parse({ serverId: 1, version: 1 })) +assert.equal(action.schema.parse({ serverId: 1 }).family, "ipv4") +assert.equal(findAction("mm_routing_measurement_plan").operation.version, 2) +assert.equal(findAction("mm_ospf_template_create_plan").operation.version, 11) +assert.equal(findAction("mm_ospf_template_update_plan").operation.version, 11) +assert.equal(findAction("mm_ospf_template_delete_plan").operation.version, 9) +assert.equal(findAction("mm_wireguard_peer_delete_plan").operation.version, 2) +assert.equal(findAction("mm_wireguard_peer_update_plan").operation.version, 2) +assert.equal(findAction("mm_wireguard_delete_plan").operation.version, 2) +assert.equal(findAction("mm_wireguard_update_plan").operation.version, 3) +assert.equal(findAction("mm_wireguard_interface_edit_plan").operation.version, 3) +assert.equal(findAction("mm_wireguard_interface_create_plan").operation.version, 3) +assert.equal(findAction("mm_wireguard_create_plan").operation.version, 4) +assert.equal(findAction("mm_interfaces_update_plan").operation.version, 5) +assert.equal(findAction("mm_ipsec_peer_delete_plan").operation.version, 3) +assert.equal(findAction("mm_ipsec_peer_update_plan").operation.version, 3) +assert.equal(findAction("mm_certificate_delete_plan").operation.version, 3) +for (const [tool, version] of [["mm_route_optimizer_plan", 7], ["mm_route_profile_save_plan", 5], ["mm_route_path_verify", 2], ["mm_operation_recovery_plan", 8], ["mm_ecmp_plan", 8], ["mm_rpf_plan", 5], ["mm_ip_settings_update_plan", 4], ["mm_ipv6_settings_update_plan", 3], ["mm_vrf_plan", 4], ["mm_firewall_rule_update_plan", 2]] as const) { + const routed = findAction(tool) + assert.equal(routed.operation.version, version) + assert.equal(findMcpOperation(routed.operation.id)?.version, version) +} +assert.throws(() => findAction("mm_routing_measurement_plan").schema.parse({ measurement: { srcServerId: 1, dstServerId: 2, srcInterface: "gre", dstInterface: "gre", family: "ipv4", expectedEndpoints: [] } })) +console.log("MCP operation revisions: OSPF v13 and template order revisions agree; clients cannot inject revision or private source proof") + +assert.equal(findAction("mm_ipsec_peer_create_plan").operation.version, 5) +assert.equal(findAction("mm_certificate_update_plan").operation.version, 4) +assert.deepEqual(findAction("mm_certificate_update_plan").operation.requiredPermissions, ["mm:certificates:write", "mm:network:write"]) + +assert.equal(findAction("mm_ipsec_user_edit_plan").operation.version, 2) +assert.equal(findAction("mm_ipsec_user_edit_plan").operation.id, "network.ipsec.users.update") +assert.deepEqual(findAction("mm_ipsec_user_edit_plan").operation.requiredPermissions, ["mm:network:write"]) + +for (const tool of ["mm_ipsec_peer_remove_plan", "mm_ipsec_certificate_remove_plan", "mm_ipsec_user_remove_plan", "mm_ipsec_peer_edit_plan"]) assert.equal(findAction(tool).operation.version, 2) +assert.equal(findAction("mm_ipsec_server_remove_plan").operation.version, 2) diff --git a/backend/src/modules/mcp/ordinary-operation-retirement.owned.test.ts b/backend/src/modules/mcp/ordinary-operation-retirement.owned.test.ts new file mode 100644 index 00000000..69d094c4 --- /dev/null +++ b/backend/src/modules/mcp/ordinary-operation-retirement.owned.test.ts @@ -0,0 +1,319 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { assertOwnedBandwidthSql,prepareOwnedBandwidthState } from "../../services/bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +assert.equal(process.env.MMAPP_OWN_MCP_OPERATION_ORIGIN,"true") +await prepareOwnedBandwidthState() +const {pool,closePool}=await import("../../db/index.js") +const {applySqlMigrations}=await import("../../db/migrate.js") +const {mcpConnectionCreateSchema}=await import("@mmapp/contracts/mcp") +const {createMcpConnection,authenticateMcpToken,getMcpSettings}=await import("./store.js") +const {invokeMcpTool,approvePlan,applyPlan}=await import("./runtime.js") +const {OrdinaryRetirementCommitUnknown,retireOrdinaryMcpOperations}=await import("./ordinary-operation-retirement.js") +const {PARTITION_WRITER_ADVISORY_KEY}=await import("../../services/partition-write-transaction.js") +const {tickMcpWorkers}=await import("./workers.js") +const {pauseSchedulerJobsAndWait,resumeSchedulerJobs}=await import("../../services/scheduler-running.js") +const {Client}=await import("pg") +const {McpError}=await import("./security.js") +const {touchGeoipRunMeta}=await import("../../services/geoip-settings.js") +const {touchBackupScheduleRunMeta}=await import("../../services/backup-service.js") +const errorIs=(code:string,status:number)=>(error:unknown)=>error instanceof McpError&&error.code===code&&error.statusCode===status + +async function assertWorkerUsesCommittedRetention() { + const actorId=randomUUID() + const original=await getMcpSettings() + await pool.query("UPDATE mcp_settings SET requests_retention_days=1,audit_retention_days=30 WHERE id=1") + const settingsWriter=await pool.connect() + let tick:Promise|undefined,settled=false,transactionOpen=false + try{ + await pool.query(`INSERT INTO mcp_requests(id,actor_id,tool,status,created_at) VALUES + ($1,$5,'protected','completed',now()-interval '2 days'), + ($2,$5,'active','running',now()-interval '33 days'), + ($3,$5,'recent','completed',now()-interval '1 hour'), + ($4,$5,'expired','completed',now()-interval '33 days')`, + [randomUUID(),randomUUID(),randomUUID(),randomUUID(),actorId]) + await pool.query(`INSERT INTO mcp_audit(actor_id,action,created_at) VALUES + ($1,'protected',now()-interval '31 days'),($1,'recent',now()-interval '1 hour'), + ($1,'expired',now()-interval '183 days')`,[actorId]) + await settingsWriter.query("BEGIN") + transactionOpen=true + const {pid}=(await settingsWriter.query("SELECT pg_backend_pid() AS pid")).rows[0] + await settingsWriter.query("UPDATE mcp_settings SET requests_retention_days=30,audit_retention_days=180 WHERE id=1") + tick=tickMcpWorkers().finally(()=>{settled=true}) + void tick.catch(()=>{}) + let waiting=false + for(const deadline=performance.now()+3000;!settled&&!waiting&&performance.now(){}) + await pool.query("DELETE FROM mcp_requests WHERE actor_id=$1",[actorId]) + await pool.query("DELETE FROM mcp_audit WHERE actor_id=$1",[actorId]) + await pool.query("UPDATE mcp_settings SET requests_retention_days=$1,audit_retention_days=$2 WHERE id=1",[original.requestsRetentionDays,original.auditRetentionDays]) + } + console.log("Owned MCP retention: worker waits for committed policy, preserves protected and running rows, and deletes expired rows") +} + +try { + await applySqlMigrations(pool) + await pauseSchedulerJobsAndWait() + await assertWorkerUsesCommittedRetention() + await pool.query("UPDATE public.mcp_settings SET enabled=true WHERE id=1") + const serverId=Number((await pool.query("INSERT INTO servers(name,host,enabled) VALUES('owned logs retention','127.0.0.1',false) RETURNING id")).rows[0].id) + async function access(){ + const actor={id:randomUUID(),email:"owned-logs-retirement@example.test",name:"Owned settings retention",apps:["mm"], + permissions:["mm:settings:admin","mm:mcp:approve","mm:traffic:write","mm:uptime:write","mm:servers:write","mm:network:write","mm:backups:write"], + issuedAt:Date.now()-1000,expiresAt:Date.now()+300000} + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:actor.name,mode:"approval", + permissions:actor.permissions,expiresAt:new Date(actor.expiresAt).toISOString(), + scope:{serverIds:[serverId],operationIds:["logs.settings.read","logs.settings.update","traffic.settings.update","uptime.settings.update", + "servers.api-ping.settings.update","internet-path.settings.update","geoip.settings.update","backups.schedule.update","operations.read","changes.apply"],allowGlobal:true}, + limits:{callsPerMinute:100,mutationsPerHour:20}}),actor) + return {actor,principal:await authenticateMcpToken(issued.token)} + } + const {actor,principal}=await access(),foreign=(await access()).principal + const initialLogs=await invokeMcpTool(principal,"mm_logs_settings_get",{}) + assert.deepEqual(initialLogs,{enabled:false,intervalSec:10,retentionDays:14}) + const shorterLogs=await invokeMcpTool(principal,"mm_logs_settings_update_plan",{settings:{retentionDays:7}}) as {state:string;changes:{warnings:string[]}} + assert.equal(shorterLogs.state,"awaiting-approval") + assert.match(shorterLogs.changes.warnings.join(" "),/с 14 до 7 суток.*полученные более 7 суток назад/) + assert.deepEqual(await invokeMcpTool(principal,"mm_logs_settings_get",{}),initialLogs) + async function operation(retentionDays:number,ageMs:number,tool="mm_logs_settings_update_plan", + settings:Record={retentionDays}){ + const plan=await invokeMcpTool(principal,tool,{settings}) as {planId:string;state:string} + if(tool==="mm_geoip_settings_update_plan"||tool==="mm_backup_schedule_update_plan")assert.equal(plan.state,"awaiting-approval") + if(plan.state==="awaiting-approval")await approvePlan(plan.planId,actor,true) + const key=randomUUID(),applied=await applyPlan(principal,plan.planId,key) + let terminal + for(const deadline=Date.now()+15000;Date.now()(await pool.query(`SELECT i.id,o.state,p.state AS plan_state,s.state AS step_state, + k.operation_id AS checkpoint,t.operation_id AS tombstone,c.claim_token + FROM unnest($1::uuid[]) i(id) + LEFT JOIN mcp_operations o ON o.id=i.id LEFT JOIN mcp_plans p ON p.id=o.plan_id + LEFT JOIN mcp_operation_steps s ON s.operation_id=i.id LEFT JOIN mcp_operation_checkpoints k ON k.operation_id=i.id + LEFT JOIN mcp_operation_replay_tombstones t ON t.operation_id=i.id + LEFT JOIN mcp_ordinary_retirement_claims c ON c.operation_id=i.id ORDER BY i.id`,[ids])).rows + const beforeFence=await projection() + const exclusive=await pool.connect() + let blockedAttempt:ReturnType|undefined + try{ + await exclusive.query("SELECT pg_advisory_lock($1::bigint)",[PARTITION_WRITER_ADVISORY_KEY]) + await exclusive.query("BEGIN") + blockedAttempt=retireOrdinaryMcpOperations() + void blockedAttempt.catch(()=>{}) + let waiting=false + for(const deadline=Date.now()+3000;!waiting&&Date.now()0);assert.equal(blocked.retired,0);assert.equal(blocked.automaticReplay,false) + assert.deepEqual(await projection(),beforeFence) + }finally{ + try{await blockedAttempt}finally{ + try{ + await exclusive.query("ROLLBACK") + assert.equal((await exclusive.query("SELECT pg_advisory_unlock($1::bigint) AS unlocked",[PARTITION_WRITER_ADVISORY_KEY])).rows[0].unlocked,true) + }finally{exclusive.release(true)} + } + } + const originalQuery=Client.prototype.query + let commits=0 + try{ + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + if((args[0] as {text?:string})?.text==="COMMIT"&&++commits===3) + return Promise.reject(Error("owned ordinary destructive COMMIT transport loss")) + return Reflect.apply(originalQuery,this,args) + } as typeof Client.prototype.query + await assert.rejects(retireOrdinaryMcpOperations(), + (error:unknown)=>error instanceof OrdinaryRetirementCommitUnknown&&error.automaticReplay===false) + }finally{Client.prototype.query=originalQuery} + assert.equal(commits,3) + const before=await projection(),spentBefore=before.find(row=>row.id===spent.id) + assert.equal(spentBefore?.state,"applied");assert(spentBefore?.claim_token);assert.equal(spentBefore?.tombstone,null) + const { withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js") + await withSystemDatabaseOperationLock(() => pool.connect(), async () => { + await tickMcpWorkers() + assert.deepEqual(await projection(), before) + }) + await tickMcpWorkers() + const after=await projection() + assert.deepEqual(after.filter(row=>row.id!==due.id),before.filter(row=>row.id!==due.id)) + const retired=after.find(row=>row.id===due.id) + assert(retired?.claim_token);assert.equal(retired?.tombstone,due.id) + for(const field of ["state","plan_state","step_state","checkpoint"])assert.equal(retired[field],null) + assert.equal((await pool.query("SELECT 1 FROM mcp_plans WHERE id=$1",[due.planId])).rows.length,0) + assert.equal((await pool.query("SELECT retention_days FROM router_log_settings WHERE id=1")).rows[0].retention_days,54) + await tickMcpWorkers() + assert.deepEqual(await projection(),after) + await assert.rejects(applyPlan(principal,due.planId,due.key),errorIs("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(applyPlan(principal,randomUUID(),due.key),errorIs("STATE_CONFLICT",409)) + await assert.rejects(invokeMcpTool(foreign,"mm_operation_get",{operationId:due.id}),errorIs("NOT_FOUND",404)) + const telemetry=[ + {action:"traffic.settings.update",version:2,record:await operation(55,age+60000,"mm_traffic_settings_update_plan")}, + {action:"uptime.settings.update",version:2,record:await operation(56,age+60000,"mm_uptime_settings_update_plan",{enabled:false,retentionDays:56})}, + {action:"servers.api-ping.settings.update",version:2,record:await operation(0,age+60000,"mm_servers_api_ping_settings_update_plan",{intervalSec:123})}, + {action:"internet-path.settings.update",version:1,record:await operation(57,age+60000,"mm_internet_path_settings_update_plan")}, + ] + const telemetryRows=async()=>(await pool.query(`SELECT p.operation_id,p.operation_version,o.id + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=ANY($1::uuid[]) ORDER BY p.operation_id`, + [telemetry.map(item=>item.record.id)])).rows + assert.deepEqual((await telemetryRows()).map(row=>[row.operation_id,row.operation_version]), + telemetry.map(item=>[item.action,item.version]).sort((a,b)=>String(a[0]).localeCompare(String(b[0])))) + const telemetrySettings=async()=>(await pool.query(`SELECT to_jsonb(t) AS traffic,to_jsonb(u) AS uptime, + to_jsonb(a) AS api_ping,to_jsonb(i) AS internet FROM traffic_settings t,uptime_settings u, + servers_api_ping_settings a,internet_path_settings i WHERE t.id=1 AND u.id=1 AND a.id=1 AND i.id=1`)).rows[0] + const settingsBefore=await telemetrySettings() + const traffic=telemetry[0].record + const {rows:[originalStep]}=await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[traffic.id]) + await pool.query("UPDATE mcp_operation_steps SET result=jsonb_set(result,'{retentionDays}','999') WHERE operation_id=$1",[traffic.id]) + await tickMcpWorkers() + assert.equal((await pool.query("SELECT 1 FROM mcp_ordinary_retirement_claims WHERE operation_id=$1",[traffic.id])).rows.length,0) + assert.equal((await pool.query("SELECT 1 FROM mcp_operations WHERE id=$1",[traffic.id])).rows.length,1) + await pool.query("UPDATE mcp_operation_steps SET result=$2 WHERE operation_id=$1",[traffic.id,originalStep.result]) + await tickMcpWorkers() + await tickMcpWorkers() + // The keyset owner may need one tick to wrap after the rejected oldest row. + await tickMcpWorkers() + assert.deepEqual(await telemetryRows(),[]) + assert.deepEqual(await telemetrySettings(),settingsBefore) + for(const {action,record} of telemetry){ + const {rows:[proof]}=await pool.query(`SELECT t.action_id,t.target_global,t.target_server_ids::text[] AS server_ids, + NOT EXISTS(SELECT 1 FROM mcp_plans WHERE id=$2) AS plan_gone, + NOT EXISTS(SELECT 1 FROM mcp_operation_steps WHERE operation_id=$1) AS step_gone, + NOT EXISTS(SELECT 1 FROM mcp_operation_checkpoints WHERE operation_id=$1) AS checkpoint_gone + FROM mcp_operation_replay_tombstones t WHERE t.operation_id=$1 AND t.plan_id=$2`,[record.id,record.planId]) + assert.deepEqual(proof,{action_id:action,target_global:true,server_ids:[String(serverId)],plan_gone:true,step_gone:true,checkpoint_gone:true}) + await assert.rejects(applyPlan(principal,record.planId,record.key),errorIs("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(invokeMcpTool(foreign,"mm_operation_get",{operationId:record.id}),errorIs("NOT_FOUND",404)) + } + // Keep the owned fixture offline even if a product scheduler refresh occurs. + await pool.query("UPDATE geoip_settings SET enabled=false WHERE id=1") + await touchGeoipRunMeta({lastCheckAt:new Date().toISOString(),lastError:"owned GeoIP metadata",etags:{"owned-test":"unchanged"}}) + const geoip=await operation(0,age+60000,"mm_geoip_settings_update_plan",{enabled:false,updateIntervalSec:86400}) + const geoipState=async()=>(await pool.query("SELECT to_jsonb(g) AS row FROM geoip_settings g WHERE id=1")).rows[0].row + const geoipBefore=await geoipState() + assert.equal(geoipBefore.enabled,false);assert.equal(geoipBefore.update_interval_sec,86400) + assert.equal(geoipBefore.last_error,"owned GeoIP metadata");assert.deepEqual(geoipBefore.etags_json,{"owned-test":"unchanged"}) + const {rows:[geoipStep]}=await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[geoip.id]) + const assertGeoipUnclaimed=async()=>{ + assert.equal((await pool.query("SELECT 1 FROM mcp_ordinary_retirement_claims WHERE operation_id=$1",[geoip.id])).rows.length,0) + assert.equal((await pool.query("SELECT 1 FROM mcp_operations WHERE id=$1",[geoip.id])).rows.length,1) + assert.deepEqual(await geoipState(),geoipBefore) + } + await pool.query("UPDATE mcp_operation_steps SET result=jsonb_set(result,'{settings,updateIntervalSec}','172800') WHERE operation_id=$1",[geoip.id]) + await tickMcpWorkers();await tickMcpWorkers();await assertGeoipUnclaimed() + await pool.query("UPDATE mcp_operation_steps SET result=$2 WHERE operation_id=$1",[geoip.id,geoipStep.result]) + const geoipHold=randomUUID() + await pool.query("INSERT INTO mcp_drift(id,operation_id,expected,observed) VALUES($1,$2,'{}','{}')",[geoipHold,geoip.id]) + await tickMcpWorkers();await assertGeoipUnclaimed() + await pool.query("DELETE FROM mcp_drift WHERE id=$1",[geoipHold]) + await tickMcpWorkers();await tickMcpWorkers() + const {rows:[geoipTombstone]}=await pool.query(`SELECT t.action_id,t.target_global,t.target_server_ids::text[] AS server_ids, + NOT EXISTS(SELECT 1 FROM mcp_operations WHERE id=$1) AS operation_gone, + NOT EXISTS(SELECT 1 FROM mcp_plans WHERE id=$2) AS plan_gone, + NOT EXISTS(SELECT 1 FROM mcp_operation_steps WHERE operation_id=$1) AS step_gone, + NOT EXISTS(SELECT 1 FROM mcp_operation_checkpoints WHERE operation_id=$1) AS checkpoint_gone + FROM mcp_operation_replay_tombstones t WHERE t.operation_id=$1 AND t.plan_id=$2`,[geoip.id,geoip.planId]) + assert.deepEqual(geoipTombstone,{action_id:"geoip.settings.update",target_global:true,server_ids:[String(serverId)], + operation_gone:true,plan_gone:true,step_gone:true,checkpoint_gone:true}) + await assert.rejects(applyPlan(principal,geoip.planId,geoip.key),errorIs("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(applyPlan(principal,randomUUID(),geoip.key),errorIs("STATE_CONFLICT",409)) + await assert.rejects(invokeMcpTool(foreign,"mm_operation_get",{operationId:geoip.id}),errorIs("NOT_FOUND",404)) + assert.deepEqual(await geoipState(),geoipBefore) + await pool.query("UPDATE backup_schedule_settings SET enabled=false,server_ids_json='[]' WHERE id=1") + await touchBackupScheduleRunMeta({lastRunAt:new Date().toISOString(),lastDurationMs:123,lastError:"owned backup scheduler metadata"}) + await pool.query(`INSERT INTO backup_entries(id,server_id,server_name,filename,size_bytes,kind,notes,created_at) + VALUES($1,$2,'owned fixture','owned-retention.rsc',123,'auto','keep',clock_timestamp())`,[randomUUID(),serverId]) + const backupState=async()=>(await pool.query(`SELECT to_jsonb(s) AS schedule, + (SELECT jsonb_agg(to_jsonb(b) ORDER BY id) FROM backup_entries b) AS entries + FROM backup_schedule_settings s WHERE id=1`)).rows[0] + const backupOutside=await operation(0,age+120000,"mm_backup_schedule_update_plan",{enabled:false,serverIds:[String(serverId+1000)]}) + const outsideState=await backupState() + await tickMcpWorkers();await tickMcpWorkers() + assert.equal((await pool.query("SELECT 1 FROM mcp_ordinary_retirement_claims WHERE operation_id=$1",[backupOutside.id])).rows.length,0) + assert.equal((await pool.query("SELECT 1 FROM mcp_operations WHERE id=$1",[backupOutside.id])).rows.length,1) + assert.deepEqual(await backupState(),outsideState) + await pool.query("UPDATE backup_schedule_settings SET server_ids_json='[]' WHERE id=1") + const backup=await operation(0,age+60000,"mm_backup_schedule_update_plan",{enabled:false,keepCount:9,serverIds:[String(serverId)]}) + const backupBefore=await backupState() + assert.equal(backupBefore.schedule.enabled,false);assert.equal(backupBefore.schedule.keep_count,9) + assert.deepEqual(backupBefore.schedule.server_ids_json,[String(serverId)]) + assert.equal(backupBefore.schedule.last_duration_ms,123);assert.equal(backupBefore.schedule.last_error,"owned backup scheduler metadata") + assert.deepEqual(backupBefore.entries,outsideState.entries) + const {rows:[backupStep]}=await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[backup.id]) + await pool.query("UPDATE mcp_operation_steps SET result=jsonb_set(result,'{keepCount}','10') WHERE operation_id=$1",[backup.id]) + await tickMcpWorkers();await tickMcpWorkers() + assert.equal((await pool.query("SELECT 1 FROM mcp_ordinary_retirement_claims WHERE operation_id=$1",[backup.id])).rows.length,0) + assert.deepEqual(await backupState(),backupBefore) + await pool.query("UPDATE mcp_operation_steps SET result=$2 WHERE operation_id=$1",[backup.id,backupStep.result]) + await tickMcpWorkers();await tickMcpWorkers() + const {rows:[backupTombstone]}=await pool.query(`SELECT t.action_id,t.target_global,t.target_server_ids::text[] AS server_ids, + NOT EXISTS(SELECT 1 FROM mcp_operations WHERE id=$1) AS operation_gone, + NOT EXISTS(SELECT 1 FROM mcp_plans WHERE id=$2) AS plan_gone, + NOT EXISTS(SELECT 1 FROM mcp_operation_steps WHERE operation_id=$1) AS step_gone, + NOT EXISTS(SELECT 1 FROM mcp_operation_checkpoints WHERE operation_id=$1) AS checkpoint_gone + FROM mcp_operation_replay_tombstones t WHERE t.operation_id=$1 AND t.plan_id=$2`,[backup.id,backup.planId]) + assert.deepEqual(backupTombstone,{action_id:"backups.schedule.update",target_global:true,server_ids:[String(serverId)], + operation_gone:true,plan_gone:true,step_gone:true,checkpoint_gone:true}) + await assert.rejects(applyPlan(principal,backup.planId,backup.key),errorIs("OPERATION_HISTORY_EXPIRED",410)) + await assert.rejects(applyPlan(principal,randomUUID(),backup.key),errorIs("STATE_CONFLICT",409)) + await assert.rejects(invokeMcpTool(foreign,"mm_operation_get",{operationId:backup.id}),errorIs("NOT_FOUND",404)) + assert.deepEqual(await backupState(),backupBefore) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$1",[principal.connectionId]) + await assert.rejects(applyPlan(principal,due.planId,due.key),errorIs("NOT_FOUND",404)) + for(const {record} of telemetry)await assert.rejects(applyPlan(principal,record.planId,record.key),errorIs("NOT_FOUND",404)) + await assert.rejects(applyPlan(principal,geoip.planId,geoip.key),errorIs("NOT_FOUND",404)) + await assert.rejects(applyPlan(principal,backup.planId,backup.key),errorIs("NOT_FOUND",404)) + console.log("Owned settings operations: seven exact families, authenticated apply, physical 180-day purge, preserved settings and scoped replay") +}finally{resumeSchedulerJobs();await closePool()} diff --git a/backend/src/modules/mcp/ordinary-operation-retirement.ts b/backend/src/modules/mcp/ordinary-operation-retirement.ts new file mode 100644 index 00000000..b0c2b229 --- /dev/null +++ b/backend/src/modules/mcp/ordinary-operation-retirement.ts @@ -0,0 +1,224 @@ +import { randomUUID } from "node:crypto" +import { logSettingsPatchSchema, logSettingsSchema } from "@mmapp/contracts" +import { geoipSettingsDtoSchema, geoipSettingsPatchSchema } from "@mmapp/contracts/geoip" +import { backupScheduleSettingsDtoSchema, putBackupScheduleSettingsSchema } from "@mmapp/contracts/backups" +import { z } from "zod" +import { connectProbeStorage } from "../../services/server-probe-storage-connection.js" +import type { ProbeStorageSql } from "../../services/server-probe-journal-maintenance.js" +import { admitPartitionWriter } from "../../services/partition-write-transaction.js" +import { sharedDatabaseGate } from "../../services/shared-database-gate.js" +import { operationIdempotencyDigest } from "./operation-replay-tombstones.js" +import { decryptSecretExisting, fingerprint, privateFingerprinterExisting, redact } from "./security.js" + +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 ids = z.array(positive).max(1000) +const retention = z.number().int().min(1).max(365) +const bool = z.boolean() +const interval = (min:number,max:number)=>z.number().int().min(min).max(max) +const backupServerIds=z.array(z.string().regex(/^[1-9]\d{0,15}$/).refine(value=>Number.isSafeInteger(Number(value)))) + .max(1000).refine(values=>new Set(values).size===values.length) +const backupSchedule=backupScheduleSettingsDtoSchema.extend({serverIds:backupServerIds}).strict() +const backupBefore=z.object({value:backupSchedule,serverIds:ids}).strict() +// Drizzle's string-mode timestamptz keeps PostgreSQL's space and hour-only +// offset. Accept that exact wire form as well as an ISO timestamptz. +const timestamp = z.string().regex(/^\d{4}-\d\d-\d\d[ T]\d\d:\d\d:\d\d(?:\.\d{1,6})?(?:Z|[+-]\d\d(?::\d\d)?)$/) + .refine(value=>Number.isFinite(Date.parse(value.replace(" ","T").replace(/([+-]\d\d)$/,"$1:00")))) +const metadata = { + id:z.literal(1),createdAt:timestamp,updatedAt:timestamp, + lastCollectedAt:timestamp.nullable(),lastDurationMs:z.number().int().safe().nullable(), + lastError:z.string().max(16384).nullable(), +} +function telemetryFamily(version:number,domain:string,settings:z.ZodObject,hasEpoch:boolean,hasConfigurationProof:boolean){ + const originalSettings=hasConfigurationProof?settings.extend({configurationProof:z.string().regex(/^[0-9a-f]{64}$/)}):settings + const effect=settings.extend({...metadata,...(hasEpoch?{pollConfigurationEpoch:uuid}:{})}).strict() + return {version,domain,patch:settings.partial().strict().refine(value=>Object.keys(value).length>0), + before:z.object({value:originalSettings.strict(),serverIds:ids}).strict(),effect} +} +const families={ + "logs.settings.update":{version:1,domain:null,patch:logSettingsPatchSchema,before:logSettingsSchema.strict(),effect:logSettingsSchema.strict()}, + "traffic.settings.update":telemetryFamily(2,"traffic-settings",z.object({enabled:bool,intervalSec:interval(5,3600),retentionDays:retention}),true,true), + "uptime.settings.update":telemetryFamily(2,"uptime-settings",z.object({enabled:bool,resourcesEnabled:bool,pingEnabled:bool, + speedEnabled:bool,intervalSec:interval(5,86400),probeIntervalSec:interval(5,86400),speedIntervalSec:interval(10,86400),retentionDays:retention}),true,true), + "servers.api-ping.settings.update":telemetryFamily(2,"servers-api-ping-settings",z.object({enabled:bool,intervalSec:interval(10,86400)}),false,true), + "internet-path.settings.update":telemetryFamily(1,"internet-path-settings",z.object({enabled:bool,intervalSec:interval(30,86400),retentionDays:retention}),true,false), + "geoip.settings.update":{ + version:1,domain:"geoip-settings", + patch:geoipSettingsPatchSchema.strict().refine(value=>Object.keys(value).length>0), + before:z.object({value:z.object({enabled:bool,updateIntervalSec:positive}).strict(),serverIds:ids}).strict(), + effect:z.object({settings:geoipSettingsDtoSchema.strict(),verificationScope:z.literal("configuration-persistence")}) + .strict().transform(result=>result.settings), + }, + "backups.schedule.update":{ + version:1,domain:"backup-schedule", + patch:putBackupScheduleSettingsSchema.extend({serverIds:backupServerIds.optional()}).strict().refine(value=>Object.keys(value).length>0), + before:backupBefore,effect:backupSchedule, + }, +} as const +const planEvidence = z.object({ + target:z.object({scope:z.literal("global")}).strict(), + objects:z.array(z.object({kind:z.string(),id:z.string(),serverId:positive.optional()}).strict()).max(1001), + steps:z.tuple([z.object({kind:z.literal("service"),service:z.string(),input:z.object({settings:object}).strict()}).strict()]), + snapshots:z.tuple([]),before:object,warnings:z.array(z.string()).max(8), + verificationScope:z.literal("configuration").optional(), + requiresHuman:z.literal(true).optional(), +}).strict() + +class RetirementRefused extends Error {} +export class OrdinaryRetirementCommitUnknown extends Error { + readonly automaticReplay = false + constructor() { super("Ordinary operation retirement outcome unknown; automatic replay forbidden") } +} +const requireThat: (value: unknown) => asserts value = value => { + if (!value) throw new RetirementRefused() +} + +// Fixed reviewed settings families without artifact, historic receipt or +// inverse-plan consumers. Other action families need separate review. +const ELIGIBLE = `FROM public.mcp_operations o + JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + JOIN public.mcp_operation_checkpoints k ON k.operation_id=o.id AND k.step_index=0 + WHERE (p.operation_id,p.operation_version) IN ( + ('logs.settings.update',1),('traffic.settings.update',2),('uptime.settings.update',2), + ('servers.api-ping.settings.update',2),('internet-path.settings.update',1),('geoip.settings.update',1),('backups.schedule.update',1) + ) AND p.state='completed' + AND o.state='applied' AND o.verification='passed' AND o.error_code IS NULL + AND isfinite(o.finished_at) AND o.finished_at($1::timestamptz,$2::uuid)) + ORDER BY o.finished_at,o.id LIMIT 8` +const SOURCE = `SELECT to_jsonb(o) AS operation,to_jsonb(p) AS plan,to_jsonb(s) AS step,to_jsonb(k) AS checkpoint ${ELIGIBLE} + AND o.id=$1 AND o.connection_id=$2 + AND octet_length(to_jsonb(o)::text)+octet_length(to_jsonb(p)::text)+octet_length(to_jsonb(s)::text)+octet_length(to_jsonb(k)::text)<=1048576 + FOR UPDATE OF o,p,s,k NOWAIT` + +async function transaction(deadlineAt:number, mode:"scan"|"write", work:(sql:ProbeStorageSql)=>Promise):Promise { + const sql = await connectProbeStorage({deadlineAt}) + let committing=false,committed=false + try { + await sql.query("BEGIN") + // Preserve the one-second lock budget and admit before any target row or operation lock. + if(mode==="write")await admitPartitionWriter(sql,{deadlineAt:Math.min(deadlineAt,Date.now()+1000)}) + await sql.query("SELECT set_config('TimeZone','UTC',true),set_config('statement_timeout','5000ms',true),set_config('lock_timeout','1000ms',true)") + const result=await work(sql) + committing=true + await sql.query("COMMIT") + committed=true + return result + } catch(error) { + if(committing)throw new OrdinaryRetirementCommitUnknown() + try{await sql.query("ROLLBACK")}catch{/* Destroy this checkout. */} + throw error + } finally { + sql.release(!committed) + } +} + +async function prepare(sql:ProbeStorageSql, operationId:string, connectionId:string) { + requireThat((await sql.query("SELECT id FROM public.mcp_connections WHERE id=$1 FOR UPDATE NOWAIT",[connectionId])).rows.length===1) + requireThat((await sql.query("SELECT pg_try_advisory_xact_lock(hashtextextended($1,0)) AS locked",["mcp-operation:"+operationId])).rows[0]?.locked===true) + const rows=(await sql.query(SOURCE,[operationId,connectionId])).rows + requireThat(rows.length===1) + const raw=rows[0], operation=object.parse(raw.operation), plan=object.parse(raw.plan) + const step=object.parse(raw.step),checkpoint=object.parse(raw.checkpoint) + const open=async(value:unknown)=>JSON.parse(await decryptSecretExisting(sealed.parse(value).sealed)) + const original=planEvidence.parse(await open(plan.evidence)) + const action=z.enum(Object.keys(families) as [keyof typeof families,...(keyof typeof families)[]]).parse(plan.operation_id) + const family=families[action],service=original.steps[0] + requireThat(plan.operation_version===family.version&&service.service===action) + requireThat(fingerprint(await open(plan.payload))===fingerprint(service.input)) + requireThat(fingerprint(step.intended_state)===fingerprint(redact(service))) + const patch=object.parse(family.patch.parse(service.input.settings)) + const effect=object.parse(family.effect.parse(step.result)) + requireThat(Object.entries(patch).every(([key,value])=>fingerprint(effect[key])===fingerprint(value))) + requireThat(fingerprint(operation.result)===fingerprint({confirmedWrites:1,verificationScope:"configuration"})) + for(const value of [checkpoint.before_sealed,checkpoint.after_sealed]){ + requireThat(typeof value==="string"&&value.length<=1024&&fingerprint(await open({sealed:value}))===fingerprint([])) + } + const serverObjects=original.objects.filter(item=>item.kind==="server") + const serverIds=ids.parse(serverObjects.map(item=>item.serverId)) + requireThat(new Set(serverIds).size===serverIds.length&&serverObjects.every(item=>item.id===String(item.serverId))) + const domainObjects=original.objects.filter(item=>item.kind!=="server") + const before=family.before.parse(original.before) + if(family.domain===null){ + requireThat(domainObjects.length===0&&original.requiresHuman===undefined) + }else{ + requireThat(original.requiresHuman===true&&original.verificationScope==="configuration" + &&domainObjects.length===1&&domainObjects[0].kind===family.domain&&domainObjects[0].id==="1"&&domainObjects[0].serverId===undefined) + requireThat("serverIds" in before&&fingerprint(before.serverIds)===fingerprint(serverIds)) + } + if(action==="backups.schedule.update"){ + const scope=new Set(serverIds.map(String)) + requireThat([...backupBefore.parse(before).value.serverIds,...backupSchedule.parse(effect).serverIds].every(id=>scope.has(id))) + } + const signer=await privateFingerprinterExisting("mcp-ordinary-retirement-source-v1") + return {planId:uuid.parse(plan.id),action,serverIds,key:z.string().min(1).max(200).parse(operation.idempotency_key),proof:signer(fingerprint(raw))} +} + +async function retire(operationId:string,connectionId:string,deadlineAt:number) { + const token=randomUUID() + await transaction(deadlineAt,"write",async sql=>{ + const source=await prepare(sql,operationId,connectionId) + const claim=await sql.query(`INSERT INTO public.mcp_ordinary_retirement_claims(operation_id,plan_id,connection_id,source_proof,claim_token) + VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING RETURNING operation_id`,[operationId,source.planId,connectionId,source.proof,token]) + requireThat(claim.rows.length===1&&claim.rows[0].operation_id===operationId) + }) + await transaction(deadlineAt,"write",async sql=>{ + const claim=(await sql.query(`SELECT source_proof,plan_id FROM public.mcp_ordinary_retirement_claims + WHERE operation_id=$1 AND connection_id=$2 AND claim_token=$3 AND claim_xid<>pg_current_xact_id()`,[operationId,connectionId,token])).rows + requireThat(claim.length===1) + const source=await prepare(sql,operationId,connectionId) + requireThat(claim[0].source_proof===source.proof&&claim[0].plan_id===source.planId) + const tombstone=await sql.query(`INSERT INTO public.mcp_operation_replay_tombstones + (operation_id,plan_id,connection_id,idempotency_digest,action_id,target_server_ids,target_global) + VALUES($1,$2,$3,$4,$5,$6,true) RETURNING operation_id`, + [operationId,source.planId,connectionId,operationIdempotencyDigest(connectionId,source.key),source.action,source.serverIds]) + requireThat(tombstone.rows.length===1&&tombstone.rows[0].operation_id===operationId) + const checkpoint=await sql.query("DELETE FROM public.mcp_operation_checkpoints WHERE operation_id=$1 AND step_index=0 RETURNING operation_id",[operationId]) + const step=await sql.query("DELETE FROM public.mcp_operation_steps WHERE operation_id=$1 AND step_index=0 RETURNING operation_id",[operationId]) + const operation=await sql.query("DELETE FROM public.mcp_operations WHERE id=$1 AND connection_id=$2 AND plan_id=$3 RETURNING id AS operation_id",[operationId,connectionId,source.planId]) + const plan=await sql.query("DELETE FROM public.mcp_plans WHERE id=$1 AND connection_id=$2 RETURNING id AS plan_id",[source.planId,connectionId]) + requireThat([checkpoint,step,operation].every(result=>result.rows.length===1&&result.rows[0].operation_id===operationId) + &&plan.rows.length===1&&plan.rows[0].plan_id===source.planId) + }) +} + +let cursor:{finishedAt:string;operationId:string}|undefined +export function retireOrdinaryMcpOperations() { + return sharedDatabaseGate.request(async()=>{ + const deadlineAt=Date.now()+15000 + const rows=await transaction(deadlineAt,"scan",async sql=>(await sql.query(CANDIDATES,[cursor?.finishedAt??null,cursor?.operationId??null])).rows) + requireThat(rows.length<=8) + let scanned=0,retired=0 + for(const row of rows){ + if(Date.now()>=deadlineAt-2000||retired>=2)break + const id=uuid.parse(row.id),connectionId=uuid.parse(row.connection_id) + requireThat(typeof row.finished_at==="string"&&Number.isFinite(Date.parse(row.finished_at))) + cursor={finishedAt:row.finished_at,operationId:id} + scanned++ + try{await retire(id,connectionId,deadlineAt);retired++} + catch(error){if(error instanceof OrdinaryRetirementCommitUnknown)throw error} + } + if(scanned===rows.length&&rows.length<8)cursor=undefined + return {scanned,retired,automaticReplay:false as const} + }) +} diff --git a/backend/src/modules/mcp/ospf-health.test.ts b/backend/src/modules/mcp/ospf-health.test.ts new file mode 100644 index 00000000..7843440f --- /dev/null +++ b/backend/src/modules/mcp/ospf-health.test.ts @@ -0,0 +1,16 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { ospfTable, requireOspfBfdSession } from "./ospf-health.js" + +const session = { ".id": "*bfd", state: "up", "remote-address": "192.0.2.2%gre-lab@lab", "local-address": "192.0.2.1%gre-lab@lab", vrf: "lab" } +test("BFD proof is bound to exactly one neighbor, interface and VRF", () => { + assert.equal(requireOspfBfdSession([session], "192.0.2.2", "gre-lab", "lab")[".id"], "*bfd") + for (const rows of [[], [session, { ...session, ".id": "*duplicate" }], [{ ...session, state: "down" }], [{ ...session, vrf: "foreign" }], [{ ...session, "local-address": "192.0.2.1%foreign@lab" }]]) assert.throws(() => requireOspfBfdSession(rows, "192.0.2.2", "gre-lab", "lab")) + assert.throws(() => requireOspfBfdSession([{ ...session, "remote-address": "192.0.2.2@lab", "local-address": "192.0.2.1@lab" }], "192.0.2.2", "gre-lab", "lab")) +}) +test("BFD IPv6 compressed/scoped addresses match without crossing VRFs", () => { + const row = { ...session, "remote-address": "fe80::2%gre-lab@lab", "local-address": "fe80::1%gre-lab@lab" } + assert.equal(requireOspfBfdSession([row], "fe80:0:0:0:0:0:0:2%gre-lab", "gre-lab", "lab")[".id"], "*bfd") + assert.throws(() => requireOspfBfdSession([row], "fe80::2", "gre-lab", "main")) + assert.equal(ospfTable({ "routing-table": "", vrf: "lab" }), "lab") +}) diff --git a/backend/src/modules/mcp/ospf-health.ts b/backend/src/modules/mcp/ospf-health.ts new file mode 100644 index 00000000..bd88af9d --- /dev/null +++ b/backend/src/modules/mcp/ospf-health.ts @@ -0,0 +1,25 @@ +import { isIP } from "node:net" +import type { ConfigurationRow } from "./resources.js" +import { McpError } from "./security.js" + +export const ospfTable = (instance: { "routing-table"?: unknown; vrf?: unknown }) => String(instance["routing-table"] || instance.vrf || "main") +const endpoint = (value: unknown) => { + if (typeof value !== "string") return null + const [scoped, vrf, extra] = value.split("@"), [address, iface, extraIface] = scoped.split("%") + if (extra !== undefined || extraIface !== undefined || !isIP(address)) return null + const ip = isIP(address) === 6 ? new URL(`http://[${address}]/`).hostname.slice(1, -1) : address + return { ip, iface, vrf } +} +export function requireOspfBfdSession(sessions: ConfigurationRow[], neighborAddress: string, iface: string, table: string) { + const neighbor = endpoint(neighborAddress) + if (!neighbor || neighbor.iface && neighbor.iface !== iface || neighbor.vrf && neighbor.vrf !== table) throw new McpError("MEASUREMENT_UNKNOWN", "Не подтверждён адрес OSPF-соседа", 409) + const matching = sessions.filter(row => { + const remote = endpoint(row["remote-address"]), local = endpoint(row["local-address"]) + if (!remote || !local || remote.ip !== neighbor.ip) return false + const interfaces = [remote.iface, local.iface, row.interface].filter(value => typeof value === "string" && value.length) + const vrfs = [remote.vrf, local.vrf, row.vrf].filter(value => typeof value === "string" && value.length) + return interfaces.length > 0 && interfaces.every(value => value === iface) && (vrfs.length ? vrfs.every(value => value === table) : table === "main") + }) + if (matching.length !== 1 || typeof matching[0][".id"] !== "string" || String(matching[0].state).toLowerCase() !== "up") throw new McpError("MEASUREMENT_UNKNOWN", "Для выбранного OSPF-соседа нет единственной Up BFD-сессии нужного интерфейса/VRF", 409) + return matching[0] +} diff --git a/backend/src/modules/mcp/ospf-legacy-actions.ts b/backend/src/modules/mcp/ospf-legacy-actions.ts new file mode 100644 index 00000000..42c70518 --- /dev/null +++ b/backend/src/modules/mcp/ospf-legacy-actions.ts @@ -0,0 +1,123 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { readLegacyOspfSealedSources } from "./ospf-legacy-source.js" +import { executeSealedLegacyOspf, reconcileSealedLegacyOspf, LegacyOspfExecutionError, type SealedLegacyOspf } from "./ospf-legacy-execution.js" +import { registerMcpService } from "./service-registry.js" +import { findMcpOperation } from "./registry.js" +import { fingerprint, privateSecretFingerprint, decryptSecret, McpError } from "./security.js" +import type { add } from "./actions.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" + +const APPLY="routing.ospf.application.optimize",RESTORE="routing.ospf.application.restore" +const principalSchema=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict() +const fail=(message:string,code="STATE_CONFLICT")=>new McpError(code,message,409) +async function catalog(input:Record,signal?:AbortSignal,verified=false){ + signal?.throwIfAborted() + if(verified)await assertVerifiedConfigurationAccess() + const current=await effectiveMcpConnection(principalSchema.parse(input.principal)) + if(current.owner_id!==input.ownerId||current.policy.mode==="observer"||!hasPermission(current.policy.permissions,"mm:network:write")||!hasPermission(current.owner_permissions,"mm:network:write"))throw fail("Право оптимизации OSPF отозвано","PERMISSION_DENIED") + if(fingerprint(current.policy)!==input.policyProof)throw fail("Допуск OSPF изменился","RESET_REQUIRED") + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 11")).rows.map(row=>Number(row.id)) + if(ids.length>10||ids.some(id=>!current.policy.scope.serverIds.includes(id))||!ids.includes(Number(input.serverId)))throw fail("Legacy OSPF требует допуск ко всему каталогу, не более 10 серверов","SERVER_DENIED") + signal?.throwIfAborted() + const rows=(await pool.query("SELECT s.*,created_at::text AS created_at,updated_at::text AS updated_at FROM servers s WHERE id=ANY($1::bigint[]) ORDER BY id LIMIT 11",[ids])).rows + const protections=(await pool.query("SELECT id,object_ref FROM mcp_protections WHERE object_ref->>'serverId'=ANY($1::text[]) OR (object_ref->>'kind'='server' AND object_ref->>'id'=ANY($1::text[])) ORDER BY id LIMIT 1001",[ids.map(String)])).rows + if(rows.length!==ids.length||protections.length>1000||Buffer.byteLength(JSON.stringify({rows,protections}))>1048576)throw fail("Каталог или защиты OSPF превышают бюджет","STATE_UNAVAILABLE") + const proof=await privateSecretFingerprint(fingerprint({rows,protections}),"ospf-legacy-catalog-v1") + if(input.catalogProof!==undefined&&input.catalogProof!==proof)throw fail("Каталог, подключение или защиты изменились после подтверждения") + signal?.throwIfAborted() + return {ids,protections,proof} +} +async function read(input:Record,signal?:AbortSignal,verified=false){ + const source=await catalog(input,signal,verified) + const request={...input,catalogProof:source.proof} + const authorize=async()=>{await catalog(request,signal,verified)} + await authorize() + const {server}=await routerClient(Number(input.serverId)) + try{ + const sealed=await readLegacyOspfSealedSources(server,Number(input.pingWeight),{serverIds:source.ids,signal,authorize}) + await authorize() + return {source,sealed,server,authorize,publicState:{proof:sealed.proof,catalogProof:source.proof}} + }catch(error){if(error instanceof McpError)throw error;throw fail("Источники legacy OSPF недоступны","STATE_UNAVAILABLE")} +} +function request(input:Record,context:ActionContext){return{...input,principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy)}} +async function restoreSource(input:Record,context:ActionContext){ + const current=await effectiveMcpConnection(context.principal) + if(current.policy.mode==="observer"||!hasPermission(current.policy.permissions,"mm:network:write")||!hasPermission(current.owner_permissions,"mm:network:write"))throw fail("Нет прав восстановления OSPF","PERMISSION_DENIED") + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 11")).rows.map(row=>Number(row.id)) + if(ids.length>10||ids.some(id=>!current.policy.scope.serverIds.includes(id)))throw fail("Восстановление требует весь каталог OSPF","SERVER_DENIED") + context.signal?.throwIfAborted() + const row=(await pool.query("SELECT o.state,p.operation_id,p.operation_version,p.evidence FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.connection_id=$2",[input.operationId,current.id])).rows[0] + if(!row||![APPLY,RESTORE].includes(row.operation_id))throw fail("Исходная OSPF операция не найдена","NOT_FOUND") + if(!current.policy.scope.operationIds.includes(row.operation_id)||findMcpOperation(row.operation_id)?.version!==row.operation_version)throw fail("Допуск или версия исходной операции изменились","PERMISSION_DENIED") + if(!["applied","partial","unknown"].includes(row.state))throw fail("Исходная операция ещё не завершена") + const evidence=JSON.parse(await decryptSecret(row.evidence.sealed)) as ChangeEvidence + const step=evidence.steps[0] + if(evidence.steps.length!==1||step?.kind!=="service"||![APPLY,RESTORE].includes(step.service))throw fail("Не подтверждён исходный сервис OSPF") + if(step.input.ownerId!==current.owner_id)throw fail("Изменился владелец исходной операции OSPF","PERMISSION_DENIED") + return step.input +} +function targets(sealed:SealedLegacyOspf){return sealed.plan.interfaces.map(row=>({id:row.id,interface:row.interface,from:row.currentCost,to:sealed.targets?.[row.id]??row.optimalCost}))} +function state(sealed:SealedLegacyOspf,source:{proof:string;catalogProof:string}){return {...source,serverId:sealed.plan.serverId,pingWeight:sealed.plan.pingWeight,changes:targets(sealed),modelContainsAssumptions:true}} +async function evidenceFor(input:Record,context:ActionContext,service:string,restore?:Record):Promise{ + const req=request(input,context) + const before=await read(req,context.signal) + let desired:Record|undefined + if(restore){ + if(restore.catalogProof!==before.source.proof)throw fail("Каталог или защиты исходной операции изменились; требуется ручная сверка") + const original=restore.sealed as SealedLegacyOspf + const reconciled=await reconcileSealedLegacyOspf(before.server,original,{serverIds:before.source.ids,signal:context.signal,authorize:before.authorize},true) + if(reconciled.confirmedPrefix===null||reconciled.confirmedPrefix===0)throw fail("Нет подтверждённых изменений для обратного плана") + desired=Object.fromEntries(original.plan.interfaces.map(row=>[row.id,row.currentCost])) + if(before.sealed.plan.interfaces.length!==Object.keys(desired).length||before.sealed.plan.interfaces.some(row=>desired![row.id]===undefined))throw fail("Изменился набор шаблонов OSPF") + } + const sealed:SealedLegacyOspf={plan:before.sealed.plan,proof:before.sealed.proof,nativeProof:before.sealed.nativeProof,...(desired?{targets:desired}:{})} + const changes=targets(sealed),count=changes.filter(row=>row.from!==row.to).length + if(count>50)throw fail("План OSPF превышает 50 cost-записей","LIMIT_EXCEEDED") + const objects:ChangeEvidence["objects"]=[...before.source.ids.map(serverId=>({kind:"server",id:String(serverId),serverId})),...changes.map(row=>({kind:"ospf_template",id:row.id,serverId:before.server.id}))] + for(const row of before.source.protections)objects.push(row.object_ref) + await before.authorize() + return {target:{scope:"servers",serverIds:before.source.ids},objects,snapshots:[],before:state(sealed,before.publicState),requiresHuman:true,mutationCost:Math.max(1,count), + steps:[{kind:"service",service,input:{...req,catalogProof:before.source.proof,sealed}}], + warnings:["Legacy рейтинг приложения использует округления и fallback. Он не подтверждает свежесть bandwidth, family/VRF, Full OSPF, BFD или forwarding; автоматическое управление по нему запрещено.","Подтверждаются конкретные cost и весь каталог приложения. Обновление метрик, подключения или защит может потребовать нового плана.","Записи последовательные и не атомарные. Проверки выполняются после durable intent перед PATCH и после read-back; неизвестный ответ не повторяется.",restore?"Отдельный человеческий обратный план: исходные cost восстанавливаются после сверки подтверждённого prefix и неизменной native-конфигурации. Это не повтор операции.":"Для восстановления используется отдельный mm_ospf_application_restore_plan по ID завершённой операции.","Native writes в receipt считаются отдельно от сервисных шагов. Сверка без записи доступна через mm_operation_reconcile."]} +} +export function registerLegacyOspfActions(register:typeof add){ + for(const service of [APPLY,RESTORE])registerMcpService(service,{ + current:async input=>state(input.sealed as SealedLegacyOspf,(await read(input)).publicState), + execute:async input=>{ + const source=await read(input,undefined,true),sealed=input.sealed as SealedLegacyOspf + if(source.sealed.proof!==sealed.proof)throw fail("Рейтинг или источники OSPF изменились после подтверждения") + try{ + const receipt=await executeSealedLegacyOspf(source.server,sealed,{serverIds:source.source.ids,authorize:source.authorize,deadlineAt:Date.now()+120000}) + return {outcome:"applied",nativeWrites:receipt.applied.length,applied:receipt.applied,forwardingVerified:false} + }catch(error){ + if(error instanceof LegacyOspfExecutionError){ + if(error.state==="unknown")throw error + return {outcome:error.state,nativeWrites:error.applied.length,applied:error.applied,forwardingVerified:false} + } + throw fail("Применение OSPF не подтверждено; нужна сверка без повтора","MUTATION_OUTCOME_UNKNOWN") + } + }, + verify:async(input,result)=>{ + if((result as {outcome?:string}|null)?.outcome!=="applied")return false + const source=await read(input) + return (await reconcileSealedLegacyOspf(source.server,input.sealed as SealedLegacyOspf,{serverIds:source.source.ids,authorize:source.authorize})).matchesDesiredState + }, + reconcile:async(input,authorize)=>{ + await authorize();const source=await read(input) + return reconcileSealedLegacyOspf(source.server,input.sealed as SealedLegacyOspf,{serverIds:source.source.ids,authorize:async()=>{await authorize();await source.authorize()}}) + }, + }) + register(APPLY,"mm_ospf_application_optimize_plan","Применить legacy OSPF рейтинг приложения после подтверждения","network","mm:network:write","write","servers",z.object({serverId:z.number().int().positive().safe(),pingWeight:z.number().int().min(0).max(100).default(60)}).strict(),{ + target:async(input,context)=>({scope:"servers",serverIds:(await catalog(request(input,context),context.signal)).ids}), + prepare:(input,context)=>evidenceFor(input,context,APPLY), + }) + register(RESTORE,"mm_ospf_application_restore_plan","Отдельный обратный план legacy OSPF операции","network","mm:network:write","write","servers",z.object({operationId:z.string().uuid()}).strict(),{ + target:async(input,context)=>{const old=await restoreSource(input,context);return{scope:"servers",serverIds:(await catalog(request({serverId:old.serverId},context),context.signal)).ids}}, + prepare:async(input,context)=>{const old=await restoreSource(input,context);return evidenceFor({serverId:old.serverId,pingWeight:old.pingWeight,sourceOperationId:input.operationId},context,RESTORE,old)}, + }) +} diff --git a/backend/src/modules/mcp/ospf-legacy-execution.ts b/backend/src/modules/mcp/ospf-legacy-execution.ts new file mode 100644 index 00000000..9e95a5bb --- /dev/null +++ b/backend/src/modules/mcp/ospf-legacy-execution.ts @@ -0,0 +1,64 @@ +import { MikrotikClient } from "../../services/mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { ospfCostConnectionProof, OspfCostWriteError } from "../../services/ospf-template-cost-write.js" +import { applyLegacyOspfOptimization, LegacyOspfApplyError, type LegacyOspfPreparation, type LegacyOspfApplied } from "../../services/ospf-legacy-optimization.js" +import { readLegacyOspfSealedSources } from "./ospf-legacy-source.js" +import { McpError } from "./security.js" + +type Server = Parameters[0] +type Options = Parameters[2] +export interface SealedLegacyOspf { plan: LegacyOspfPreparation; proof: string; nativeProof?:string; targets?:Record } +export class LegacyOspfExecutionError extends McpError { + constructor(readonly state: "rejected"|"partial"|"unknown", readonly applied: readonly LegacyOspfApplied[]) { + super(state === "partial" ? "VERIFICATION_FAILED" : state === "unknown" ? "MUTATION_OUTCOME_UNKNOWN" : "STATE_CONFLICT", "Применение legacy OSPF остановлено; нужна сверка без повторной записи", 409) + } +} + +/** Internal executor. The MCP caller must supply its full catalog, policy, source and protection authorization. */ +export async function executeSealedLegacyOspf(server: Server, sealed: SealedLegacyOspf, options: Options) { + if (sealed.plan.serverId !== server.id || !/^[a-f0-9]{64}$/.test(sealed.proof)) throw new LegacyOspfExecutionError("rejected", []) + return withApplicationMutationLock(() => withConfigMutationLock([...options.serverIds], async () => { + const connectionProof = await ospfCostConnectionProof(server.id, server) + let completed = 0 + const check = async (count: number) => { + const observed = await readLegacyOspfSealedSources(server, sealed.plan.pingWeight, options, { baseline: sealed.plan, completedCount: count,targets:sealed.targets }) + if (observed.proof !== sealed.proof) throw new McpError("STATE_CONFLICT", "Источники legacy OSPF изменились", 409) + } + try { + await check(0) + const client = MikrotikClient.fromServer(server) + const desired={...sealed.plan,interfaces:sealed.plan.interfaces.map(row=>({...row,optimalCost:sealed.targets?.[row.id]??row.optimalCost}))} + return await applyLegacyOspfOptimization(desired, async item => { + await client.setOspfInterfaceTemplateCost(item.id, item.optimalCost, item.currentCost, connectionProof, () => check(completed)) + // A confirmed cost write is not proof that SQL/neighbor/configuration sources stayed unchanged. + try { await check(completed + 1) } catch { throw new OspfCostWriteError("Контекст после cost не подтверждён; повтор запрещён", true) } + completed++ + }, async (_item, index) => { await check(index) }) + } catch (error) { + if (error instanceof LegacyOspfApplyError) throw new LegacyOspfExecutionError(error.state, error.applied) + throw new LegacyOspfExecutionError(completed ? "unknown" : "rejected", []) + } + })) +} + +/** Read-only, bounded prefix reconciliation. Never invokes a writer or interprets unobserved state as success. */ +export async function reconcileSealedLegacyOspf(server: Server, sealed: SealedLegacyOspf, options: Options,nativeOnly=false) { + if (sealed.plan.serverId !== server.id || !/^[a-f0-9]{64}$/.test(sealed.proof)) throw new LegacyOspfExecutionError("rejected", []) + const scoped = { ...options, deadlineAt: Math.min(options.deadlineAt ?? Infinity, Date.now()+45000) } + const observed = await readLegacyOspfSealedSources(server, sealed.plan.pingWeight, scoped) + const current = new Map(observed.plan.interfaces.map(row => [row.id, row.currentCost])) + const desired=(row:LegacyOspfPreparation["interfaces"][number])=>sealed.targets?.[row.id]??row.optimalCost + const changed = sealed.plan.interfaces.filter(row => row.currentCost !== desired(row)) + let count = 0, pending = false, valid = current.size === sealed.plan.interfaces.length + for (const item of sealed.plan.interfaces) { + const cost = current.get(item.id) + if (item.currentCost === desired(item)) { if (cost !== item.currentCost) valid = false; continue } + if (cost === desired(item) && !pending) count++ + else if (cost === item.currentCost) pending = true + else valid = false + } + if (!valid) return { matchesOriginalState: false, matchesDesiredState: false, confirmedPrefix: null } + const proof = await readLegacyOspfSealedSources(server, sealed.plan.pingWeight, scoped, { baseline: sealed.plan, completedCount: count,targets:sealed.targets }) + const matches = nativeOnly ? typeof sealed.nativeProof==="string"&&proof.nativeProof===sealed.nativeProof : proof.proof === sealed.proof + return { matchesOriginalState: matches && count === 0, matchesDesiredState: matches && count === changed.length, confirmedPrefix: matches ? count : null } +} diff --git a/backend/src/modules/mcp/ospf-legacy-source.test.ts b/backend/src/modules/mcp/ospf-legacy-source.test.ts new file mode 100644 index 00000000..fa263150 --- /dev/null +++ b/backend/src/modules/mcp/ospf-legacy-source.test.ts @@ -0,0 +1,69 @@ +import assert from "node:assert/strict" +import { createHmac } from "node:crypto" +import { LegacyOspfSourceCapture, type LegacyOspfPrefix } from "./ospf-legacy-source.js" +import type { LegacyOspfPreparation } from "../../services/ospf-legacy-optimization.js" + +const key = Buffer.alloc(32, 7) +const digest = (value: string) => createHmac("sha256", key).update(value).digest("hex") +const plan: LegacyOspfPreparation = { serverId: 1, serverName: "fixture", pingWeight: 60, interfaces: [ + { id: "*1", interface: "gre-a", currentCost: 30, optimalCost: 10, score: 80, pingMs: 10, dlMbps: 100, ulMbps: 90 }, + { id: "*2", interface: "gre-b", currentCost: 40, optimalCost: 20, score: 70, pingMs: 20, dlMbps: 90, ulMbps: 80 }, +] } +const templates = [{ ".id": "*1", interfaces: "gre-a", cost: "30", disabled: "false", opaque: "native-kept" }, { ".id": "*2", interfaces: "gre-b", cost: "40", disabled: "false" }] +const neighbors = [{ ".id": "*n1", "router-id": "10.0.0.2", interface: "gre-a", state: "Full", uptime: "1m" }, { ".id": "*n2", "router-id": "10.0.0.3", interface: "gre-a", state: "Full", uptime: "2m" }] +const source = [{ id: 1, password: "NEVER_PUBLISH_SQL_PASSWORD", polledAt: new Date("2026-10-23T00:00:00Z") }] +function collect(prefix?: LegacyOspfPrefix, edits: { native?: typeof templates; neighbors?: typeof neighbors; source?: unknown; ranked?: LegacyOspfPreparation; routeMetric?: string } = {}) { + const capture = new LegacyOspfSourceCapture(digest, prefix) + capture.native(1, "/routing/ospf/interface-template", null, edits.native ?? templates) + capture.native(1, "/routing/ospf/neighbor", null, edits.neighbors ?? neighbors) + capture.native(1, "/ip/route", null, [{ ".id": "*r1", metric: edits.routeMetric ?? "10" }]) + for(const path of ["/routing/ospf/area", "/routing/ospf/instance", "/routing/bfd/session", "/ipv6/route"])capture.native(1,path,null,[]) + capture.native(1, "/routing/ospf/interface-template/get", { ".id": "*1", "value-name": "disabled" }, { ret: "false" }) + capture.source("enabled-servers", edits.source ?? source) + capture.source("latency:1", [{latencyMs:10,polledAt:new Date("2026-10-23T00:00:00Z")}]) + capture.source("traffic:1", []) + capture.source("speed-probes:1", []) + return capture.finish(edits.ranked ?? plan) +} +const before = collect() +assert.equal(collect(undefined, { routeMetric: "99", neighbors: neighbors.map(row => ({ ...row, uptime: "5m" })) }).proof, before.proof) +assert.equal(collect(undefined, { native: [...templates].reverse() }).proof, before.proof) +assert.notEqual(collect(undefined, { neighbors: [...neighbors].reverse() }).proof, before.proof, "Equal-state native order is a ranking input") +assert.notEqual(collect(undefined, { source: [{ ...source[0], password: "changed" }] }).proof, before.proof) +assert.equal(collect(undefined, { source: [{ ...source[0], password: "changed" }] }).nativeProof, before.nativeProof) +assert.notEqual(collect(undefined, { native: templates.map(row => ({ ...row, opaque: "changed" })) }).proof, before.proof) +assert.ok(!JSON.stringify(before).includes(source[0].password)) +const progressed = { ...plan, interfaces: plan.interfaces.map((row, index) => ({ ...row, currentCost: index === 0 ? row.optimalCost : row.currentCost })) } +const prefix = { baseline: plan, completedCount: 1 } +const nativeProgressed = templates.map((row, index) => ({ ...row, cost: index === 0 ? "10" : row.cost })) +assert.equal(collect(prefix, { native: nativeProgressed, ranked: progressed }).proof, before.proof) +assert.throws(() => collect(prefix), /не подтверждены/) +assert.throws(() => collect(prefix, { native: templates.map(row => ({ ...row, cost: String(plan.interfaces.find(item => item.id === row[".id"])!.optimalCost) })), ranked: progressed }), /не подтверждены/, "Future writes must not be accepted as completed prefix") +assert.throws(() => collect(prefix, { native: nativeProgressed, ranked: { ...progressed, interfaces: progressed.interfaces.slice(0, 1) } }), /не подтверждены/) +assert.notEqual(collect(prefix, { native: nativeProgressed.map(row => ({ ...row, opaque: "collateral" })), ranked: progressed }).proof, before.proof) +assert.throws(() => new LegacyOspfSourceCapture(digest, { baseline: plan, completedCount: 3 })) +const inverseBaseline={...plan,interfaces:plan.interfaces.map(row=>({...row,currentCost:row.optimalCost}))} +const inverseNative=templates.map((row,index)=>({...row,cost:String(plan.interfaces[index].optimalCost)})) +const inverseBefore=collect(undefined,{native:inverseNative,ranked:inverseBaseline}) +const inverseTargets=Object.fromEntries(plan.interfaces.map(row=>[row.id,row.currentCost])) +const inverseRank={...inverseBaseline,interfaces:inverseBaseline.interfaces.map((row,index)=>({...row,currentCost:index===0?plan.interfaces[0].currentCost:row.currentCost}))} +const inverseStep=collect({baseline:inverseBaseline,completedCount:1,targets:inverseTargets},{native:inverseNative.map((row,index)=>({...row,cost:index===0?"30":row.cost})),ranked:inverseRank}) +assert.equal(inverseStep.proof,inverseBefore.proof);assert.equal(inverseStep.nativeProof,inverseBefore.nativeProof) +assert.throws(()=>new LegacyOspfSourceCapture(digest,{baseline:inverseBaseline,completedCount:0,targets:{"*1":30}})) +for (const [path, payload] of [["/routing/ospf/interface-template", [templates[0], templates[0]]], ["/routing/ospf/interface-template", null], ["/unexpected", []], ["/ip/route", Array.from({ length: 10001 }, (_, index) => ({ ".id": String(index) }))]] as const) { + assert.throws(() => new LegacyOspfSourceCapture(digest).native(1, path, null, payload)) +} +const duplicate = new LegacyOspfSourceCapture(digest) +duplicate.source("latency:1", [{ latencyMs: 10 }]) +assert.throws(() => duplicate.source("latency:1", [{ latencyMs: 11 }]), /не подтверждены/, "Intra-capture drift must fail closed") +assert.throws(() => new LegacyOspfSourceCapture(digest).source("latency:1", [{}, {}])) +assert.throws(() => new LegacyOspfSourceCapture(digest).source("traffic:1", [{ data: "x".repeat(1048577) }])) +assert.throws(() => new LegacyOspfSourceCapture(digest).source("traffic:1", Array.from({length:2001},()=>({})))) +assert.throws(() => new LegacyOspfSourceCapture(digest).native(1, "/routing/ospf/interface-template/set", { cost: "10" }, {})) +const closed = new LegacyOspfSourceCapture(digest) +assert.throws(()=>closed.finish(plan), /не подтверждены/, "Missing source closure must fail") +for(const path of ["/routing/ospf/area", "/routing/ospf/instance", "/routing/bfd/session", "/ipv6/route", "/ip/route", "/routing/ospf/neighbor", "/routing/ospf/interface-template"])closed.native(1,path,null,[]) +closed.finish({...plan,interfaces:[]}) +assert.throws(() => closed.source("latency:1", [])) +assert.throws(() => closed.finish(plan)) +console.log("Legacy OSPF source proof: keyed SQL/native/rank capture, actual prefix costs, opaque configuration and neighbor tie order, bounded inputs, intra-read drift and no raw secrets") diff --git a/backend/src/modules/mcp/ospf-legacy-source.ts b/backend/src/modules/mcp/ospf-legacy-source.ts new file mode 100644 index 00000000..72620c71 --- /dev/null +++ b/backend/src/modules/mcp/ospf-legacy-source.ts @@ -0,0 +1,153 @@ +import { buildOspfOptimizationPlan, type OspfPreviewReadOptions } from "../../routes/ospf.js" +import { prepareLegacyOspfOptimization, type LegacyOspfPreparation } from "../../services/ospf-legacy-optimization.js" +import { validateOspfOptimizationPlan } from "../../services/ospf-optimizer-plan.js" +import { McpError, fingerprint, privateFingerprinter } from "./security.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" + +const templatePath = "/routing/ospf/interface-template" +const configPaths = new Set([templatePath, "/routing/ospf/area", "/routing/ospf/instance"]) +const unusedPaths = new Set(["/routing/bfd/session", "/ip/route", "/ipv6/route"]) +const neighborFields = [".id", "router-id", "interface", "state"] +const fail = () => new McpError("STATE_CONFLICT", "Источники legacy OSPF не подтверждены; подготовьте новый план", 409) +export interface LegacyOspfPrefix { baseline: LegacyOspfPreparation; completedCount: number; targets?:Record } + +/** Only keyed digests leave this collector. Native rows and SQL credentials stay in memory. */ +export class LegacyOspfSourceCapture { + private entries = new Map() + private inspected = new Set() + private bytes = 0 + private reads = 0 + private closed = false + private baseline?: LegacyOspfPreparation + private costs = new Map() + + constructor(private readonly digest: (value: string) => string, prefix?: LegacyOspfPrefix) { + if (!prefix) return + const interfaces = validateOspfOptimizationPlan(prefix.baseline.interfaces) + if(prefix.targets&&(Object.keys(prefix.targets).length!==interfaces.length||interfaces.some(row=>!Number.isInteger(prefix.targets![row.id])||prefix.targets![row.id]<1||prefix.targets![row.id]>65535)))throw fail() + const desired=(row:typeof interfaces[number])=>prefix.targets?.[row.id]??row.optimalCost + const changed = interfaces.filter(row => row.currentCost !== desired(row)) + if (!Number.isSafeInteger(prefix.completedCount) || prefix.completedCount < 0 || prefix.completedCount > changed.length || interfaces.length > 1000) throw fail() + this.baseline = { ...prefix.baseline, interfaces } + const completed = new Set(changed.slice(0, prefix.completedCount).map(row => row.id)) + for (const row of interfaces) this.costs.set(row.id, { initial: row.currentCost, expected: completed.has(row.id) ? desired(row) : row.currentCost }) + } + + private bounded(value: unknown): string { + if (this.closed || ++this.reads > 512) throw fail() + let serialized: string + try { serialized = JSON.stringify(value) } catch { throw fail() } + if (typeof serialized !== "string") throw fail() + const size = Buffer.byteLength(serialized) + this.bytes += size + if (size > 1048576 || this.bytes > 8388608 || Array.isArray(value) && value.length > 10000) throw fail() + return serialized + } + + private record(key: string, value: unknown) { + if (this.closed) throw fail() + const proof = this.digest(fingerprint(value)) + const previous = this.entries.get(key) + if (previous !== undefined && previous !== proof) throw fail() + if (this.entries.size >= 256 && previous === undefined) throw fail() + this.entries.set(key, proof) + } + + native(serverId: number, path: string, body: Record | null, value: unknown) { + this.bounded(value) + if (!Number.isSafeInteger(serverId) || serverId <= 0) throw fail() + if (body) { + const parent = path.replace(/\/(get|print)$/, "") + if (!configPaths.has(parent) || path === parent) throw fail() + this.bounded(body) + this.record(`native:${serverId}:${path}:${fingerprint(body)}`, value) + return + } + if (!Array.isArray(value) || value.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !row[".id"].trim())) throw fail() + if (new Set(value.map(row => row[".id"])).size !== value.length) throw fail() + this.inspected.add(`${serverId}:${path}`) + // The legacy ranker fetches these catalogs but does not consume their contents. + // Their successful bounded read is required; their runtime churn is not a configuration proof. + if (unusedPaths.has(path)) return + if (path === "/routing/ospf/neighbor") { + // Preserve native order: equal-state neighbors use the original stable tie order. + this.record(`native:${serverId}:${path}`, value.map(row => Object.fromEntries(neighborFields.filter(key => key in row).map(key => [key, row[key]])))) + return + } + if (!configPaths.has(path)) throw fail() + const rows = value.map(row => ({ ...row })) + if (path === templatePath && serverId === this.baseline?.serverId) { + for (const [id, cost] of this.costs) { + const row = rows.find(row => row[".id"] === id) + if (!row || ![cost.expected, String(cost.expected)].includes(row.cost)) throw fail() + row.cost = String(cost.initial) + } + } + this.record(`native:${serverId}:${path}`, rows.sort((a, b) => a[".id"].localeCompare(b[".id"]))) + } + + source(name: string, value: unknown) { + this.bounded(value) + if (!/^(enabled-servers|(?:latency|traffic|speed-probes):[1-9]\d*)$/.test(name)) throw fail() + if (!Array.isArray(value) || value.length > (name.startsWith("latency:") ? 1 : name === "enabled-servers" ? 1000 : 2000)) throw fail() + // Preserve SQL order because probe ties can depend on that order. + this.record(`sql:${name}`, value) + } + + finish(plan: LegacyOspfPreparation) { + if (this.closed) throw fail() + const ranked = validateOspfOptimizationPlan(plan.interfaces) + if (ranked.length > 1000) throw fail() + if ([...configPaths, ...unusedPaths, "/routing/ospf/neighbor"].some(path => !this.inspected.has(`${plan.serverId}:${path}`))) throw fail() + if (ranked.length && ["enabled-servers", `latency:${plan.serverId}`, `traffic:${plan.serverId}`, `speed-probes:${plan.serverId}`].some(name => !this.entries.has(`sql:${name}`))) throw fail() + const normalized = ranked.map(row => { + if (!this.baseline) return row + const cost = this.costs.get(row.id) + if (!cost || row.currentCost !== cost.expected) throw fail() + return { ...row, currentCost: cost.initial } + }) + if (this.baseline && normalized.length !== this.costs.size) throw fail() + this.bounded(normalized) + this.record("ranked", { ...plan, interfaces: normalized }) + const entries = [...this.entries].sort(([a], [b]) => a.localeCompare(b)) + const nativeProof=this.digest(fingerprint(entries.filter(([key])=>key.startsWith("native:")))) + this.closed = true + this.entries.clear() + this.inspected.clear() + return { proof: this.digest(fingerprint(entries)), nativeProof, sourceCount: entries.length, observedReads: this.reads } + } +} + +/** Uses the actual REST ranker; authorization surrounds every SQL and native source read. */ +export async function readLegacyOspfSealedSources( + server: Parameters[0], pingWeight: number, + options: OspfPreviewReadOptions & { serverIds: readonly number[]; authorize: () => Promise }, prefix?: LegacyOspfPrefix, +) { + if (options.serverIds.length > 10 || options.serverIds.some(id => !Number.isSafeInteger(id) || id <= 0) || !options.serverIds.includes(server.id) || new Set(options.serverIds).size !== options.serverIds.length) throw fail() + const deadlineAt = Math.min(options.deadlineAt ?? Infinity, Date.now() + 45000) + if (!Number.isFinite(deadlineAt) || deadlineAt <= Date.now()) throw fail() + const signal = AbortSignal.any([AbortSignal.timeout(Math.max(1, Math.ceil(deadlineAt-Date.now()))), ...(options.signal ? [options.signal] : [])]) + const authorize = async () => { signal.throwIfAborted(); await options.authorize(); signal.throwIfAborted() } + await authorize() + const capture = new LegacyOspfSourceCapture(await privateFingerprinter("ospf-legacy-application-sources-v1"), prefix) + const sources = new Map() + const verifySource = async (source: typeof server) => { + await authorize() + if (!options.serverIds.includes(source.id)) throw fail() + const fresh = await getServerRowById(source.id) + if (!fresh?.enabled || fingerprint(fresh) !== fingerprint(source)) throw fail() + sources.set(source.id, source) + await authorize() + } + const plan = await prepareLegacyOspfOptimization(server, pingWeight, () => buildOspfOptimizationPlan(server, pingWeight, { + ...options, + deadlineAt, signal, + beforeSourceRead: async () => { await authorize(); await options.beforeSourceRead?.() }, + onSourceRead: async (name, value) => { await authorize(); capture.source(name, value); await options.onSourceRead?.(name, value); await authorize() }, + beforeRequest: async source => { await verifySource(source); await options.beforeRequest?.(source); await authorize() }, + onNativeRead: async (source, path, body, value) => { await authorize(); capture.native(source.id, path, body, value); await options.onNativeRead?.(source, path, body, value); await authorize() }, + }), authorize) + for (const source of sources.values()) await verifySource(source) + await authorize() + return { plan, ...capture.finish(plan) } +} diff --git a/backend/src/modules/mcp/ospf-owner-graph.test.ts b/backend/src/modules/mcp/ospf-owner-graph.test.ts new file mode 100644 index 00000000..ce29db8c --- /dev/null +++ b/backend/src/modules/mcp/ospf-owner-graph.test.ts @@ -0,0 +1,93 @@ +import assert from "node:assert/strict" +import { createOspfOwnerSourceGuard, verifyOspfOwnerGraph, verifyOspfOwnerGraphAtPhase } from "./ospf-owner-graph.js" +import { fingerprint, McpError, redact } from "./security.js" +import { createRosPrefix } from "./ros-prefix.js" +import { rosSnapshotVersion, type RosSnapshot } from "./resources.js" + +const ids = [1, 2, 3] +const graph = { family: "ipv4" as const, sourceSecretProofs: ids.map(serverId => ({ serverId, proof: fingerprint({ serverId }) })), instances: ids.map(serverId => ({ serverId, fingerprint: fingerprint({ serverId, native: "instances" }) })) } +let reads: number[] = [], sourcesCurrent = true, revoked = false, changed = 0 +const authorize = async () => { if (revoked) throw new Error("Revoked") } +const dependencies = { + sources: async (selected: number[], expected: typeof graph.sourceSecretProofs) => { + assert.deepEqual(selected, ids); assert.deepEqual(expected, graph.sourceSecretProofs) + return async () => { if (!sourcesCurrent) throw new Error("Changed source") } + }, + instance: async (id: number) => { reads.push(id); return changed === id ? fingerprint("changed") : graph.instances.find(row => row.serverId === id)!.fingerprint }, +} +await verifyOspfOwnerGraph(graph, ids, authorize, dependencies) +assert.deepEqual(reads, ids) +changed = 3; reads = [] +await assert.rejects(verifyOspfOwnerGraph(graph, ids, authorize, dependencies), (error: { code?: string }) => error.code === "STATE_CONFLICT") +assert.deepEqual(reads, ids, "A previously unselected owner remains part of every dispatch check") +changed = 0; reads = []; sourcesCurrent = false +await assert.rejects(verifyOspfOwnerGraph(graph, ids, authorize, dependencies)); assert.deepEqual(reads, []) +sourcesCurrent = true +reads = [] +await assert.rejects(verifyOspfOwnerGraph(graph, ids, authorize, { ...dependencies, instance: async id => { const result = await dependencies.instance(id); if (id === 1) sourcesCurrent = false; return result } })) +assert.deepEqual(reads, [1], "Endpoint drift stops before polling another graph candidate") +sourcesCurrent = true +await assert.rejects(verifyOspfOwnerGraph(graph, ids, authorize, { ...dependencies, instance: async id => { const result = await dependencies.instance(id); if (id === 3) sourcesCurrent = false; return result } })) +sourcesCurrent = true; reads = [] +await assert.rejects(verifyOspfOwnerGraph(graph, ids, authorize, { ...dependencies, instance: async id => { const result = await dependencies.instance(id); if (id === 1) revoked = true; return result } })) +assert.deepEqual(reads, [1], "Revoked access stops before the next native read") +revoked = false +for (const invalid of [{ ...graph, instances: [...graph.instances, graph.instances[0]] }, { ...graph, sourceSecretProofs: graph.sourceSecretProofs.slice(1) }, { ...graph, instances: [{ serverId: 99, fingerprint: fingerprint("foreign") }] }]) { + reads = []; await assert.rejects(verifyOspfOwnerGraph(invalid, ids, authorize, dependencies)); assert.deepEqual(reads, []) +} +assert.equal((redact({ ospfOwnerGraph: graph }) as { ospfOwnerGraph: { sourceSecretProofs: unknown } }).ospfOwnerGraph.sourceSecretProofs, "[redacted]") +console.log("OSPF owner graph: full negative/positive owner set, endpoint checks before/after native reads, per-read live grant, scoped identities and private proofs") + +// The context is reused, but every catalog callback must revalidate its live +// epoch. A->B->A restores endpoint fields, never the original epoch/proof. +let contexts = 0, sourceChecks = 0, epoch = 0, grantRevoked = false +const lightSources = { + sources: async (selected: number[], expected: typeof graph.sourceSecretProofs) => { + contexts++; assert.deepEqual(selected, ids); assert.deepEqual(expected, graph.sourceSecretProofs) + const sealedEpoch = epoch + return async () => { sourceChecks++; if (epoch !== sealedEpoch) throw new McpError("STATE_CONFLICT", "Source epoch changed") } + }, +} +const sourceGuard = createOspfOwnerSourceGuard(graph, ids, lightSources) +const catalogAuthorize = async () => { + if (grantRevoked) throw new McpError("PERMISSION_DENIED", "Revoked") + await sourceGuard() +} +const catalogRows = [{ ".id": "*A", name: "keep", disabled: "false" }] +const catalog: RosSnapshot = { serverId: 1, resource: "bridge", rows: catalogRows, fingerprint: fingerprint(catalogRows), projectionVersion: rosSnapshotVersion("bridge"), sourceProof: "a".repeat(64) } +reads = [] +const catalogPrefix = createRosPrefix([catalog], async (_id, _resource, beforeRead = async () => {}) => { + // Match readRosSnapshot's normal three grant/source fences without polling a + // device; the prefix adds a fourth fence at the end of its catalog pass. + await beforeRead(); await beforeRead(); await beforeRead(); return structuredClone(catalog) +}) +for (let index = 0; index < 32; index++) await catalogPrefix.check(catalogAuthorize) +assert.equal(contexts, 1, "Operation-local source context must be initialized once") +assert.equal(sourceChecks, 128, "Live source checks must never be memoized away") +assert.deepEqual(reads, [], "Catalog checks must not invoke full graph/native polling") +epoch = 1; epoch = 2 +await assert.rejects(catalogPrefix.check(catalogAuthorize), error => error instanceof McpError && error.code === "STATE_CONFLICT") +assert.equal(contexts, 1, "ABA cannot rebind a memoized guard to a fresh epoch") +grantRevoked = true +const checksBeforeRevoke = sourceChecks +await assert.rejects(catalogPrefix.check(catalogAuthorize), error => error instanceof McpError && error.code === "PERMISSION_DENIED") +assert.equal(sourceChecks, checksBeforeRevoke) +for (const invalid of [{ ...graph, instances: [...graph.instances, graph.instances[0]] }, { ...graph, sourceSecretProofs: graph.sourceSecretProofs.slice(1) }, { ...graph, instances: [{ serverId: 99, fingerprint: fingerprint("foreign") }] }]) { + assert.throws(() => createOspfOwnerSourceGuard(invalid, ids, lightSources)) +} +assert.equal(contexts, 1, "Foreign/invalid graphs must fail before even initializing source access") + +// Only full graph conflicts after an acknowledged write become verification +// failure. Admission conflicts, grant failures and I/O errors keep their codes. +changed = 2; reads = []; sourcesCurrent = true; revoked = false +await assert.rejects(verifyOspfOwnerGraphAtPhase(graph, ids, authorize, "admission", dependencies), error => error instanceof McpError && error.code === "STATE_CONFLICT") +await assert.rejects(verifyOspfOwnerGraphAtPhase(graph, ids, authorize, "post-ack", dependencies), error => error instanceof McpError && error.code === "VERIFICATION_FAILED") +changed = 0 +const permission = new McpError("PERMISSION_DENIED", "Paused") +await assert.rejects(verifyOspfOwnerGraphAtPhase(graph, ids, async () => { throw permission }, "post-ack", dependencies), error => error === permission) +const io = new Error("Native unavailable") +await assert.rejects(verifyOspfOwnerGraphAtPhase(graph, ids, authorize, "post-ack", { ...dependencies, instance: async () => { throw io } }), error => error === io) +reads = [] +for (let boundary = 0; boundary < 5; boundary++) await verifyOspfOwnerGraphAtPhase(graph, ids, authorize, boundary < 3 ? "admission" : "post-ack", dependencies) +assert.equal(reads.length, 15, "Five explicit full-graph boundaries poll three owners each") +console.log("OSPF lightweight catalog guards: one bound source context, 128 live checks with zero graph polls, ABA/revocation/scope rejection, five full boundaries and post-ACK conflict classification") diff --git a/backend/src/modules/mcp/ospf-owner-graph.ts b/backend/src/modules/mcp/ospf-owner-graph.ts new file mode 100644 index 00000000..85141937 --- /dev/null +++ b/backend/src/modules/mcp/ospf-owner-graph.ts @@ -0,0 +1,74 @@ +import { z } from "zod" +import type { ChangeEvidence } from "./action-types.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { configurationRows, resourceById, routerClient } from "./resources.js" +import { readOspfLinkCatalog } from "../../services/ospf-link-catalog.js" +import { fingerprint, McpError } from "./security.js" + +const sid = z.number().int().positive().safe(), hash = z.string().regex(/^[a-f0-9]{64}$/) +const schema = z.object({ family: z.enum(["ipv4", "ipv6"]), sourceSecretProofs: z.array(z.object({ serverId: sid, proof: hash }).strict()).min(1).max(1000), instances: z.array(z.object({ serverId: sid, fingerprint: hash }).strict()).min(1).max(1000) }).strict() +type Graph = NonNullable +interface GraphDependencies { + sources: (ids: number[], expected: Graph["sourceSecretProofs"]) => Promise<() => Promise>; + instance: (id: number) => Promise; +} + +function validateGraph(graph: Graph, permittedIds: readonly number[]) { + const parsed = schema.safeParse(graph) + if (!parsed.success) throw new McpError("STATE_CONFLICT", "Недостоверный граф владельцев OSPF", 409) + const value = parsed.data, ids = value.instances.map(row => row.serverId) + const sourceIds = value.sourceSecretProofs.map(row => row.serverId) + if (new Set(ids).size !== ids.length || new Set(sourceIds).size !== sourceIds.length || ids.some(id => !permittedIds.includes(id)) || fingerprint([...ids].sort((a, b) => a - b)) !== fingerprint([...sourceIds].sort((a, b) => a - b))) throw new McpError("SERVER_DENIED", "Граф OSPF не совпадает с полной разрешённой областью источников", 409) + return { value, ids } +} + +/** Operation-local source guard: memoize the bound context, never the result of + * its live DB/HMAC/epoch check. This performs no native graph polling. */ +export function createOspfOwnerSourceGuard(graph: Graph | undefined, permittedIds: readonly number[], dependencies?: Pick): () => Promise { + if (!graph) return async () => {} + const { value, ids } = validateGraph(graph, permittedIds) + let context: Promise<() => Promise> | undefined + return async () => { + context ??= dependencies ? dependencies.sources(ids, value.sourceSecretProofs) : currentSources(ids, value) + await (await context)() + } +} + +export async function verifyOspfOwnerGraph(graph: Graph | undefined, permittedIds: readonly number[], authorize: () => Promise, dependencies?: GraphDependencies) { + if (!graph) return + const { value, ids } = validateGraph(graph, permittedIds) + await authorize() + const assertSources = dependencies ? await dependencies.sources(ids, value.sourceSecretProofs) : await currentSources(ids, value) + await assertSources() + let rows = 0, bytes = 0 + for (const expected of value.instances) { + await authorize() + await assertSources() + let actual: string + if (dependencies) actual = await dependencies.instance(expected.serverId) + else { + const { client } = await routerClient(expected.serverId) + const link = await readOspfLinkCatalog(client, value.family, async () => { await authorize(); await assertSources() }) + rows += link.rows; bytes += link.bytes + if (rows > 10000 || bytes > 1048576) throw new McpError("STATE_UNAVAILABLE", "Граф владельцев OSPF превышает допустимый объём", 409) + actual = fingerprint({ native: fingerprint(configurationRows(resourceById("ospf_instance"), link.identity.raw)), identity: link.fingerprint }) + } + if (actual !== expected.fingerprint) throw new McpError("STATE_CONFLICT", "После подготовки изменился граф владельцев Router ID OSPF", 409) + } + await assertSources() + await authorize() +} + +export async function verifyOspfOwnerGraphAtPhase(graph: Graph | undefined, permittedIds: readonly number[], authorize: () => Promise, phase: "admission" | "post-ack", dependencies?: GraphDependencies) { + try { await verifyOspfOwnerGraph(graph, permittedIds, authorize, dependencies) } + catch (error) { + if (phase === "post-ack" && error instanceof McpError && error.code === "STATE_CONFLICT") throw new McpError("VERIFICATION_FAILED", "После записи изменился подтверждённый контекст соседства OSPF; требуется сверка без повторной записи", 409) + throw error + } +} + +async function currentSources(ids: number[], expected: Graph) { + const context = await createRoutingSourceContext(await readRoutingEndpoints(ids)) + if (fingerprint(context.expected) !== fingerprint([...expected.sourceSecretProofs].sort((a, b) => a.serverId - b.serverId))) throw new McpError("STATE_CONFLICT", "Источник графа OSPF изменился после подготовки", 409) + return () => context.assertCurrent() +} diff --git a/backend/src/modules/mcp/ospf-route-proof-acceptance.ts b/backend/src/modules/mcp/ospf-route-proof-acceptance.ts new file mode 100644 index 00000000..433c61f1 --- /dev/null +++ b/backend/src/modules/mcp/ospf-route-proof-acceptance.ts @@ -0,0 +1,102 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, setConnectionStatus, authenticateMcpToken } from "./store.js" +import { invokeMcpTool } from "./runtime.js" + +/** Runs only inside the disposable SQL/HTTP acceptance database. */ +export async function acceptOspfRouteProofMcp(actor: AuthUser) { + const selection = { serverId: 104, prefix: "198.19.1.2/32", table: "local-vrf", interface: "owned-gre" } + const base: Record = { ".id": "*a", "dst-address": selection.prefix, "routing-table": selection.table, "immediate-gw": "198.18.1.2%owned-gre@local-vrf", opaque: "NEVER_RIB_PRIVATE" } + let rows = [base], reads = 0, nativeError = false + const tablePath = "/rest/routing/table?.proplist=.id,name,disabled,invalid" + let tables = [{ ".id": "*t", name: selection.table }] + let beforeReply: (() => Promise) | undefined, afterMetric: (() => Promise) | undefined + 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 } + 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 } + 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() } + res.end(JSON.stringify({ ret: ({ ospf: "true", active: "true", "ospf.metric": "0" } as Record)[key] })) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(104,'ospf-route-proof','127.0.0.1',$1,false,'NEVER_RIB_ENDPOINT_PASSWORD')", [(router.address() as { port: number }).port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "OSPF RIB proof", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [104], operationIds: ["routing.ospf.route.proof.read"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const read = (args = selection, signal?: AbortSignal) => invokeMcpTool(principal, "mm_ospf_route_proof_get", args, signal) + const result = await read() as { route: { metric: number }; forwardingVerified: boolean; independentPathsVerified: boolean } + assert.equal(result.route.metric, 0); assert.equal(result.forwardingVerified, false); assert.equal(result.independentPathsVerified, false) + assert.ok(!JSON.stringify(result).includes("NEVER_")); assert.equal(reads, 4) + rows = [base, { ...base, ".id": "*b", contribution: "best-candidate" }] + assert.equal((await read() as typeof result).route.metric, 0) + assert.equal(reads, 8, "Known inactive alternative needs no property reads") + const beforeHidden = reads + await assert.rejects(read({ ...selection, serverId: 1 })) + await assert.rejects(read({ ...selection, prefix: "not-a-prefix" })) + await pool.query("UPDATE servers SET enabled=false WHERE id=104") + await assert.rejects(read(), (error: { code?: string }) => error.code === "DEVICE_UNAVAILABLE") + await pool.query("UPDATE servers SET enabled=true WHERE id=104") + assert.equal(reads, beforeHidden) + rows = [base, { ...base, ".id": "*b" }] + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + rows = [{ ...base, "immediate-gw": "198.18.1.2%wrong-gre" }] + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + rows = [base]; nativeError = true + await assert.rejects(read(), (error: Error & { code?: string }) => error.code === "STATE_UNAVAILABLE" && !error.message.includes("NEVER_")) + nativeError = false + let beforeRace = reads + beforeReply = async () => { + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=104") + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=104") + } + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_CONFLICT") + assert.equal(reads, beforeRace + 1, "Endpoint ABA stops before all missing-field getters") + beforeRace = reads + beforeReply = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(read()); assert.equal(reads, beforeRace + 1) + await setConnectionStatus(access.connection.id, "active", actor) + beforeRace = reads + const cancel = new AbortController() + beforeReply = async () => cancel.abort() + await assert.rejects(read(selection, cancel.signal), (error: { code?: string }) => error.code === "READ_CANCELLED") + assert.equal(reads, beforeRace + 1) + afterMetric = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(read(), "Pause after the last getter must stop publication") + await setConnectionStatus(access.connection.id, "active", actor) + const absent = () => invokeMcpTool(principal, "mm_ospf_route_proof_get", { ...selection, expectAbsent: true }) + rows = [{ ...base, "routing-table": "foreign-vrf" }, + { ".id": "*b", "dst-address": "eth1", afi: "link" }, + { ".id": "*c", "dst-address": "fe80::%owned-gre/64", "routing-table": selection.table, afi: "ipv6" }] + const absentResult = await absent() as { route: null; absenceVerified: boolean; prefix: string; table: string } + assert.equal(absentResult.route, null); assert.equal(absentResult.absenceVerified, true) + assert.equal(absentResult.prefix, selection.prefix); assert.equal(absentResult.table, selection.table) + for (const row of [{ ...base, contribution: "best-candidate" }, { ...base, "routing-table": undefined }, { ...base, "dst-address": undefined }]) { + rows = [row] + await assert.rejects(absent(), { code: "STATE_UNAVAILABLE" }) + } + rows = []; nativeError = true + await assert.rejects(absent(), { code: "STATE_UNAVAILABLE" }) + nativeError = false; tables = [] + await assert.rejects(absent(), { code: "STATE_UNAVAILABLE" }) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n, 0) + const recorded = JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows) + assert.ok(!recorded.includes("NEVER_")) + console.log("✓ OSPF RIB MCP proof: active learned route, confirmed table-wide absence, rejection of hidden/ambiguous/failed RIB, source fences and no observer plans") + } finally { + if (connectionId) await pool.query("DELETE FROM mcp_connections WHERE id=$1", [connectionId]) + await pool.query("DELETE FROM servers WHERE id=104") + router.closeAllConnections() + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/ospf-route-proof-actions.ts b/backend/src/modules/mcp/ospf-route-proof-actions.ts new file mode 100644 index 00000000..c4586431 --- /dev/null +++ b/backend/src/modules/mcp/ospf-route-proof-actions.ts @@ -0,0 +1,43 @@ +import { z } from "zod" +import { hasPermission } from "../../lib/permissions.js" +import { canonicalOspfRouteProofPrefix, readOspfRouteAbsence, readOspfRouteProof } from "../../services/ospf-route-proof.js" +import { effectiveMcpConnection } from "./store.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError } from "./security.js" +import type { add } from "./actions.js" + +const name = z.string().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f]/.test(value)) +const prefix = z.string().max(64).refine(value => value === value.trim() && canonicalOspfRouteProofPrefix(value) !== null) + +export function registerOspfRouteProofActions(register: typeof add) { + register("routing.ospf.route.proof.read", "mm_ospf_route_proof_get", "Проверка активного OSPF-маршрута или отсутствия точного префикса во всей выбранной таблице", "network", "mm:network:read", "read", "servers", z.object({ serverId: z.number().int().positive().safe(), prefix, table: name, interface: name.describe("Выходной интерфейс для проверки активного маршрута"), expectAbsent: z.boolean().default(false) }).strict(), { + read: async (input, context) => { + const authorize = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение OSPF-маршрута отменено", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:read") || !hasPermission(current.policy.permissions, "mm:network:read")) throw new McpError("PERMISSION_DENIED", "Право чтения сети отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) throw new McpError("RESET_REQUIRED", "Допуск изменился; повторите чтение OSPF-маршрута", 409) + } + await authorize() + const serverId = Number(input.serverId), source = await createRoutingSourceContext(await readRoutingEndpoints([serverId])) + if (source.expected.length !== 1) throw new McpError("DEVICE_UNAVAILABLE", "Источник маршрута OSPF отсутствует или выключен", 409) + const beforeRead = async () => { await authorize(); await source.assertCurrent() } + try { + await beforeRead() + const { client } = await routerClient(serverId) + const selection = { prefix: String(input.prefix), table: String(input.table), interface: String(input.interface) } + const proof = input.expectAbsent === true + ? { route: null, ...await readOspfRouteAbsence(client, selection, beforeRead) } + : { route: await readOspfRouteProof(client, selection, beforeRead) } + return { serverId, ...proof, observedAt: new Date().toISOString(), timestampKind: "backend-receipt", forwardingVerified: false, independentPathsVerified: false } + } catch (error) { + await beforeRead() + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", input.expectAbsent === true + ? "Отсутствие точного префикса во всей выбранной таблице не подтверждено" + : "Активный OSPF-маршрут, его метрика или выбранный выход не подтверждены; предел источника 10000 строк и 1 МиБ", 409) + } + }, + }) +} diff --git a/backend/src/modules/mcp/ospf-state-actions.ts b/backend/src/modules/mcp/ospf-state-actions.ts new file mode 100644 index 00000000..c13c6ebb --- /dev/null +++ b/backend/src/modules/mcp/ospf-state-actions.ts @@ -0,0 +1,41 @@ +import { z } from "zod" +import { readOspfMcpStateForServer, readBfdStateForServer } from "../../services/ospf-state-read.js" +import type { ServerRow } from "../servers/repository/servers-repository.js" +import { catalogDevices, catalogPaging } from "./catalog-scope.js" +import type { ActionContext } from "./action-types.js" +import type { add } from "./actions.js" + +async function readState(input: Record, context: ActionContext, read: (server: ServerRow) => Promise>) { + const { selected, ...page } = await catalogDevices(input, context), offset = Number(input.offset), limit = Number(input.limit) + let incomplete = page.incomplete + const failures: { serverId: number; serverName: string; error: string }[] = [] + const servers = await Promise.all(selected.map(async server => { + try { + const state = await read(server) + const collections = Object.fromEntries(Object.entries(state).map(([key, rows]) => { + if (rows === null) { + incomplete = true + return [key, null] + } + const items = rows.slice(offset, offset + limit) + incomplete ||= items.length < rows.length + return [key, { items, total: rows.length, nextOffset: offset + limit < rows.length ? offset + limit : null }] + })) + return { serverId: server.id, collections } + } catch (error) { + incomplete = true + failures.push({ serverId: server.id, serverName: server.name, error: error instanceof Error ? error.message : "Ошибка опроса OSPF/BFD" }) + return { serverId: server.id, collections: null } + } + })) + return { ...page, servers, failures: failures.sort((a, b) => a.serverId - b.serverId), incomplete, observedAt: new Date().toISOString(), timestampKind: "backend-receipt", forwardingVerified: false } +} + +export function registerOspfStateActions(register: typeof add) { + register("routing.ospf.state.read", "mm_ospf_state_get", "Состояние OSPF: интерфейсы, соседства, шаблоны, instances, areas, BFD, маршруты и метаданные LSA по запросу", "network", "mm:network:read", "read", "servers", catalogPaging.extend({ includeLsas: z.boolean().default(false) }), { + read: (input, context) => readState(input, context, server => readOspfMcpStateForServer(server, context.signal, input.includeLsas === true)), + }) + register("routing.bfd.sessions.read", "mm_bfd_sessions_get", "Сессии BFD: только подтверждённые поля и явная неизвестность", "network", "mm:network:read", "read", "servers", catalogPaging, { + read: (input, context) => readState(input, context, async server => ({ sessions: (await readBfdStateForServer(server)).observed })), + }) +} diff --git a/backend/src/modules/mcp/path-proof-purpose.ts b/backend/src/modules/mcp/path-proof-purpose.ts new file mode 100644 index 00000000..fd1c891a --- /dev/null +++ b/backend/src/modules/mcp/path-proof-purpose.ts @@ -0,0 +1,10 @@ +import { z } from "zod" + +export const pathProofPurposeSchema = z.enum(["community", "control-path"]) +export type PathProofPurpose = z.infer +export const PATH_PROOF_VERSION = 2 +export const COMMUNITY_PATH_MAX_AGE_MS = 90000 +export const pathProofTag = (purpose: PathProofPurpose) => "path-check:" + purpose + ":v" + PATH_PROOF_VERSION + ":" +export function isCommunityPathEvidence(value: Record) { + return value.proofPurpose === "community" && value.proofVersion === PATH_PROOF_VERSION +} diff --git a/backend/src/modules/mcp/path-verification.test.ts b/backend/src/modules/mcp/path-verification.test.ts new file mode 100644 index 00000000..13d49053 --- /dev/null +++ b/backend/src/modules/mcp/path-verification.test.ts @@ -0,0 +1,96 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { inspectTrace, inspectControlPing } from "./path-verification.js" +const raw = [{ address: "192.0.2.1", sent: "1", loss: "0" }, { address: "192.0.2.2", sent: "1", loss: "0" }] + +test("control failure requires the exact completed probe and valid terminal counters", () => { + const failed = inspectControlPing([{ sent: "3", received: "0", "packet-loss": "100" }]) + assert.equal(failed.confirmedFailure, true) + assert.deepEqual(failed.packetCounts, { sent: 3, received: 0 }) + for (const rows of [[], [{ status: "timeout" }], [{ sent: "2", received: "0" }], [{ sent: "4", received: "0" }], [{ sent: "9".repeat(400), received: "0" }], [{ sent: "3", received: "4" }], [{ sent: "3", received: "0" }, { sent: "invalid", received: "0" }]]) assert.equal(inspectControlPing(rows).confirmedFailure, false) + assert.equal(inspectControlPing([{ sent: "3", received: "3", "packet-loss": "0", "avg-rtt": "1ms" }]).confirmedFailure, false) + assert.equal(inspectControlPing([{ sent: "3", received: "0", "packet-loss": "0" }]).ping.measured, false) +}) +test("control ping rejects malformed explicit loss and summaries split across rows", () => { + for (const [received, loss] of [["3", "0junk"], ["0", "100junk"], ["3", ""], ["3", "0e0"]]) { + const result = inspectControlPing([{ sent: "3", received, "packet-loss": loss, "avg-rtt": "1ms" }]) + assert.equal(result.ping.measured, false) + assert.equal(result.confirmedFailure, false) + } + assert.equal(inspectControlPing([{ sent: "3", received: "3" }, { "packet-loss": "0", "avg-rtt": "1ms" }]).ping.measured, false) + assert.equal(inspectControlPing([{ sent: "3", received: "0", "packet-loss": "100.0%" }]).confirmedFailure, true) + assert.equal(inspectControlPing([{ sent: "3", received: "3", "avg-rtt": "1ms" }]).ping.measured, true) +}) +test("path proof requires ordered expected hops and destination, never infers a missing hop", () => { + assert.equal(inspectTrace(raw, "192.0.2.2", ["192.0.2.1", "192.0.2.2"]).verified, true) + assert.equal(inspectTrace(raw, "192.0.2.2", ["192.0.2.2", "192.0.2.1"]).verified, false) + assert.equal(inspectTrace(raw, "192.0.2.3", ["192.0.2.1"]).verified, false) + assert.equal(inspectTrace([{ ...raw[0], status: "timeout" }, raw[1]], "192.0.2.2", ["192.0.2.1"]).verified, false) + assert.equal(inspectTrace([{ ...raw[0], sent: undefined }, raw[1]], "192.0.2.2", ["192.0.2.1"]).verified, false) + assert.equal(inspectTrace([{ ...raw[0], loss: "100" }, raw[1]], "192.0.2.2", ["192.0.2.1"]).verified, false) +}) + +test("traceroute requires explicit numeric loss for every observed hop", () => { + const expected = ["192.0.2.1", "192.0.2.2"] + for (const loss of [null, "", " ", false, [], "0x0"]) { + assert.equal(inspectTrace([{ ...raw[0], loss }, raw[1]], "192.0.2.2", expected).verified, false) + assert.equal(inspectTrace([raw[0], { ...raw[1], loss }], "192.0.2.2", expected).verified, false) + } + assert.equal(inspectTrace([{ ...raw[0], loss: "0.0" }, { ...raw[1], loss: 0 }], "192.0.2.2", expected).verified, true) +}) + +test("traceroute rejects coerced sent counters and section identifiers", () => { + const expected = ["192.0.2.1", "192.0.2.2"] + for (const sent of [true, [1], 1.5, "1.5", "1e0", "0x1", " 1", "9007199254740993"]) { + assert.equal(inspectTrace([{ ...raw[0], sent }, raw[1]], "192.0.2.2", expected).verified, false, JSON.stringify(sent)) + } + const partial = { ".section": "0", address: "", sent: "1", loss: "100", status: "timeout" } + const complete = raw.map(row => ({ ...row, sent: 1, ".section": 1 })) + assert.equal(inspectTrace([partial, ...complete], "192.0.2.2", expected).verified, true) + assert.equal(inspectTrace([partial, ...complete.map(row => ({ ...row, ".section": [1] }))], "192.0.2.2", expected).verified, false) + const timeout = { address: "", sent: 1, loss: "100", status: "timeout" } + const result = inspectTrace([raw[0], timeout, raw[1]], "192.0.2.2", expected) + assert.equal(result.verified, true) + assert.equal(result.completeTrace, false) +}) + +test("IPv6 traceroute matches required hops and destination across equivalent address spellings", () => { + const observed = [ + { ...raw[0], address: { address: "2001:db8::a" } }, + { ...raw[1], address: "2001:0DB8:0:0:0:0:0:B" }, + ] + const expected = ["2001:0DB8:0:0:0:0:0:A", "2001:db8::b"] + const trace = inspectTrace(observed, "2001:db8::b", expected) + assert.equal(trace.verified, true) + assert.deepEqual(trace.hops, ["2001:db8::a", "2001:0DB8:0:0:0:0:0:B"]) + assert.equal(inspectTrace(observed, "2001:db8::c", expected).verified, false) + assert.equal(inspectTrace(observed, "2001:db8::b", [...expected].reverse()).verified, false) +}) + +test("incomplete positive ping cannot attest the three-packet command; unknown reasons expose no raw payload", () => { + for (const [sent, received] of [["1", "1"], ["2", "2"], ["4", "4"], ["invalid", "3"], ["9".repeat(400), "3"]]) { + const result = inspectControlPing([{ sent, received, "packet-loss": "0", "avg-rtt": "1ms", status: "NEVER_NATIVE_BODY" }]) + assert.equal(result.ping.measured, false); assert.equal(result.packetCounts, null) + assert.equal(result.reason, "PING_COUNTERS_UNCONFIRMED"); assert.equal(result.confirmedFailure, false) + assert.ok(!JSON.stringify({ reason: result.reason, observation: result.observation }).includes("NEVER_")) + } + const complete = inspectControlPing([{ sent: "3", received: "3", "packet-loss": "0", "avg-rtt": "1ms" }]) + assert.equal(complete.ping.measured, true); assert.equal(complete.reason, null) + assert.deepEqual(complete.observation, { rows: 1, hasTerminalCounters: true, terminalSent: 3, terminalReceived: 3 }) + assert.equal(inspectControlPing([{ sent: "3", received: "2", "packet-loss": "33", "avg-rtt": "1ms" }]).reason, "PING_PARTIAL_LOSS") + assert.equal(inspectControlPing([{ sent: "3", received: "0", "packet-loss": "0" }]).reason, "PING_COUNTERS_INCONSISTENT") +}) +test("native REST traceroute uses the last complete section and never merges partial tables", () => { + const partial = [{ ".section": "0", address: "", sent: "1", loss: "0" }] + const complete = raw.map(row => ({ ...row, ".section": "1" })) + assert.equal(inspectTrace([...partial, ...complete], "192.0.2.2", ["192.0.2.1", "192.0.2.2"]).verified, true) + assert.equal(inspectTrace([...complete, { ...raw[0], ".section": "2" }], "192.0.2.2", ["192.0.2.1", "192.0.2.2"]).verified, false) + assert.equal(inspectTrace([...complete, { ...raw[0], ".section": "invalid" }], "192.0.2.2", ["192.0.2.1"]).verified, false) +}) +test("unrelated unresponsive hops stay unknown without hiding observed required nodes", () => { + const gap = { address: "", status: "timeout", sent: "1", loss: "100" } + const proof = inspectTrace([raw[0], gap, raw[1]], "192.0.2.2", ["192.0.2.1", "192.0.2.2"]) + assert.equal(proof.verified, true); assert.equal(proof.completeTrace, false) + assert.equal(inspectTrace([raw[0], gap, raw[1]], "192.0.2.2", ["192.0.2.3", "192.0.2.2"]).verified, false) + assert.equal(inspectTrace([...raw, gap], "192.0.2.2", ["192.0.2.1", "192.0.2.2"]).verified, false) +}) diff --git a/backend/src/modules/mcp/path-verification.ts b/backend/src/modules/mcp/path-verification.ts new file mode 100644 index 00000000..636286d7 --- /dev/null +++ b/backend/src/modules/mcp/path-verification.ts @@ -0,0 +1,176 @@ +import { randomUUID } from "node:crypto" +import { isIP } from "node:net" +import { z } from "zod" +import { routeAiProfileSchema, type RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { parsePing } from "../../services/uptime-collector.js" +import { ipv6ToInt } from "../../services/traffic-flow-ip.js" +import { fingerprint, McpError } from "./security.js" +import { readRouteCommunityContext, routeContextAccess, type RouteContextOptions } from "./routing-community-context.js" +import type { add } from "./actions.js" +import { readControlPathContext } from "./control-path-context.js" +import { COMMUNITY_PATH_MAX_AGE_MS, isCommunityPathEvidence, PATH_PROOF_VERSION, pathProofPurposeSchema, type PathProofPurpose } from "./path-proof-purpose.js" +import { routingMeasurementRefusal } from "./routing-community-predispatch-diagnostic.js" +import { connectionWrite } from "./store.js" + +export function inspectControlPing(rows: Parameters[0]) { + const parsed = parsePing(rows) + const terminal = [...rows].reverse().find(row => row.sent !== undefined || row.received !== undefined || row["packet-loss"] !== undefined || row["avg-rtt"] !== undefined) + const sent = Number(terminal?.sent), received = Number(terminal?.received) + const counts = terminal && /^\d+$/.test(terminal.sent ?? "") && /^\d+$/.test(terminal.received ?? "") && sent === 3 && Number.isSafeInteger(received) && received >= 0 && received <= sent ? { sent, received } : null + const loss = terminal?.["packet-loss"] + const malformedLoss = loss !== undefined && (!/^\d+(?:\.\d+)?%?$/.test(loss) || Number(loss.replace(/%$/, "")) > 100) + const inconsistent = !!counts && (malformedLoss || parsed.loss !== Math.round((counts.sent - counts.received) / counts.sent * 100)) + const ping = !counts || inconsistent ? { ...parsed, measured: false } : parsed + const reason = !counts ? "PING_COUNTERS_UNCONFIRMED" : inconsistent ? "PING_COUNTERS_INCONSISTENT" : !ping.measured ? "PING_MEASUREMENT_UNCONFIRMED" : received > 0 && received < sent ? "PING_PARTIAL_LOSS" : null + const observedCounter = (value: string | undefined) => value !== undefined && /^\d+$/.test(value) && Number(value) <= 16 ? Number(value) : null + const observation = { rows: rows.length, hasTerminalCounters: terminal?.sent !== undefined || terminal?.received !== undefined, terminalSent: observedCounter(terminal?.sent), terminalReceived: observedCounter(terminal?.received) } + // Count is fixed at three in the command. Malformed/oversized/incomplete + // counters cannot prove success or turn an unknown probe into confirmed loss. + return { ping, packetCounts: counts, confirmedFailure: ping.measured && counts?.received === 0, reason, observation } +} + +export function inspectTrace(raw: unknown, destination: string, expectedHops: string[]) { + if (!Array.isArray(raw) || !raw.length || raw.length > 256) return { verified: false, hops: [] as string[] } + let rows = raw + if (raw.some(row => row && typeof row === "object" && ".section" in row)) { + const invalidSection = raw.some(row => { + if (!row || typeof row !== "object") return true + const section = row[".section"] + if (typeof section === "number") return !Number.isInteger(section) || section < 0 || section > 99 + return typeof section !== "string" || !/^\d{1,2}$/.test(section) + }) + if (invalidSection) return { verified: false, hops: [] as string[] } + // RouterOS REST returns successive traceroute table snapshots. Earlier + // sections can contain unfinished empty hops; never merge them into proof. + const finalSection = Math.max(...raw.map(row => Number(row[".section"]))) + rows = raw.filter(row => Number(row[".section"]) === finalSection) + } + if (rows.length > 16) return { verified: false, hops: [] as string[] } + const hops: string[] = [] + let unobservedHops = 0 + for (const row of rows) { + if (!row || typeof row !== "object" || row.error || row.status && row.status !== "timeout") return { verified: false, hops } + const sent = typeof row.sent === "number" || typeof row.sent === "string" && /^\d+$/.test(row.sent) ? Number(row.sent) : NaN + if (!Number.isSafeInteger(sent) || sent < 1) return { verified: false, hops } + const loss = typeof row.loss === "number" || typeof row.loss === "string" && /^\d+(?:\.\d+)?$/.test(row.loss) ? Number(row.loss) : NaN + if (!Number.isFinite(loss) || loss < 0 || loss > 100) return { verified: false, hops } + const address = typeof row.address === "string" ? row.address : row.address?.address + if (!address || row.status === "timeout") { unobservedHops++; continue } + if (typeof address !== "string" || !isIP(address.split("%")[0]) || loss !== 0) return { verified: false, hops } + hops.push(address.split("%")[0]) + } + let cursor = -1 + const matchesAddress = (actual: string, expected: string) => { + if (actual === expected) return true + if (expected.includes("%")) return false + const expectedV6 = ipv6ToInt(expected) + return expectedV6 !== null && ipv6ToInt(actual) === expectedV6 + } + const ordered = expectedHops.every(address => { + const index = hops.findIndex((hop, index) => index > cursor && matchesAddress(hop, address)) + if (index < 0) return false + cursor = index + return true + }) + const last = rows.at(-1), finalAddress = typeof last?.address === "string" ? last.address : last?.address?.address + const reachedDestination = typeof finalAddress === "string" && matchesAddress(finalAddress.split("%")[0], destination) + return { verified: ordered && reachedDestination && Number(last?.loss) === 0 && !last?.status, hops, unobservedHops, completeTrace: unobservedHops === 0 } +} +export async function checkCandidatePath(profile: RouteAiProfile, candidateId: string, connectionId: string, options: RouteContextOptions & { proofPurpose?: PathProofPurpose } = {}, + communityContext?: { + before: () => ReturnType + after?: (options: RouteContextOptions) => ReturnType + }) { + const proofPurpose = pathProofPurposeSchema.parse(options.proofPurpose ?? "community") + if (options.managementTransport && proofPurpose !== "community") throw new McpError("UNSUPPORTED_CAPABILITY", "Management transport разрешён только для Community proof", 409) + if (communityContext && proofPurpose !== "community") throw new McpError("INVALID_INPUT", "Контекст применим только к Community proof", 400) + const acquire = (sourceOptions: RouteContextOptions) => proofPurpose === "control-path" ? readControlPathContext(profile, candidateId, sourceOptions) : readRouteCommunityContext(profile, { ...sourceOptions, selectedCandidateId: candidateId }) + const candidate = profile.candidates.find(candidate => candidate.id === candidateId) + if (!candidate?.controlProbe) throw new McpError("MEASUREMENT_UNKNOWN", "Для пути не настроена контрольная проверка", 409) + const probe = candidate.controlProbe, startedAt = new Date().toISOString(), id = randomUUID() + let status: "passed" | "failed" | "unknown" = "unknown", evidence: Record = { startedAt } + // A recovery caller may validate its initial context as part of acquisition. + // Preserve the independent post-probe read and both probe fences. + let context: Awaited> | undefined = await communityContext?.before() + try { + context ??= await acquire(options) + if (!context.before.contextVerified) throw new McpError("MEASUREMENT_UNKNOWN", "Контекст контрольной проверки не подтверждён", 409) + const probeEndpoint = context.probeEndpoints.get(candidateId) + if (!probeEndpoint) throw new McpError("STATE_UNAVAILABLE", "Контекст выбранного кандидата не подтверждён", 409) + const { destination, expected, source } = probeEndpoint + if (!probe.expectedHops.some(hop => hop.serverId === candidate.jumpHostId) || probe.expectedHops.at(-1)?.serverId !== candidate.exitNodeId) throw new McpError("INVALID_INPUT", "Контрольный путь должен включать JumpHost и завершаться ExitNode", 400) + const client = context.clients.get(profile.serverId)! + const args: Record = { address: destination, interface: candidate.wanInterface, vrf: profile.probeVrf, count: "3", interval: "0.3s" } + if (source) args["src-address"] = source + await context.fence() + const pingRaw = await client.post("/tool/ping", args, 10000) + const pingRows = z.array(z.record(z.string(), z.string())).max(256).parse(pingRaw), { ping, packetCounts, confirmedFailure, reason, observation } = inspectControlPing(pingRows) + evidence = { ...evidence, packetCounts, pingObservation: observation, ...(reason ? { reason } : {}) } + // Only explicit terminal sent/received proves a failed check. A timeout or + // unsupported command is unknown and can never count toward failover. + if (confirmedFailure) { status = "failed"; evidence = { ...evidence, destination, ping } } + else if (ping.measured && ping.loss === 0) { + const traceArgs: Record = { address: destination, interface: candidate.wanInterface, vrf: profile.probeVrf, count: "1", "max-hops": "16", timeout: "500ms", "use-dns": "no", protocol: "icmp" } + if (args["src-address"]) traceArgs["src-address"] = args["src-address"] + await context.fence() + const trace = inspectTrace(await client.post("/tool/traceroute", traceArgs, 20000), destination, expected) + status = trace.verified ? "passed" : "unknown" + if (!trace.verified) evidence = { ...evidence, reason: "TRACE_UNCONFIRMED" } + evidence = { ...evidence, destination, expected, ping, trace, proofScope: "observed-control-probes-only", returnPathVerified: false } + } + const current = await (communityContext?.after ?? acquire)({ ...options, expectedSources: context.before.sourceProofs }) + if (current.before.dependencyProof !== context.before.dependencyProof || current.before.targetRuleProof !== context.before.targetRuleProof) throw new McpError("STATE_CONFLICT", "Контекст пути изменился во время проверки", 409) + } catch (error) { status = "unknown"; evidence = { ...evidence, reason: error instanceof McpError ? error.code : "PROBE_UNAVAILABLE" } } + // A failed fence invalidates even an otherwise successful or failed probe. + if (["STATE_CONFLICT", "PERMISSION_DENIED", "RESET_REQUIRED", "READ_CANCELLED"].includes(String(evidence.reason))) status = "unknown" + const observedAt = new Date().toISOString() + const payload = { id, connectionId, profileHash: fingerprint(profile), candidateId, status, ...evidence, proofPurpose, proofVersion: PATH_PROOF_VERSION, proofScope: "observed-control-probes-only", returnPathVerified: false, observedAt, sourceProofs: context?.before.sourceProofs ?? [], dependencyProof: context?.before.dependencyProof ?? null } + const proof = context?.signEvidence(payload) ?? null + await connectionWrite(async client => { + await options.authorize?.() + await client.query("INSERT INTO mcp_path_checks(id,connection_id,profile_hash,candidate_id,status,evidence) VALUES($1,$2,$3,$4,$5,$6::jsonb)", [id, connectionId, payload.profileHash, candidateId, status, JSON.stringify({ ...payload, proof })]) + }) + return { id, candidateId, status, ...evidence, proofPurpose, proofVersion: PATH_PROOF_VERSION, proofScope: "observed-control-probes-only" as const, returnPathVerified: false, observedAt } +} +export function registerPathActions(register: typeof add) { + register("routing.path.verify", "mm_route_path_verify", "Контрольные ping и traceroute зарегистрированного пути", "network", "mm:network:read", "read", "servers", z.object({ profile: routeAiProfileSchema, candidateId: z.string().min(1), proofPurpose: pathProofPurposeSchema.default("community") }).strict(), { + target: input => { const profile = routeAiProfileSchema.parse(input.profile); return { scope: "servers", serverIds: [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])] } }, + read: async (input, context) => checkCandidatePath(routeAiProfileSchema.parse(input.profile), String(input.candidateId), context.connection.id, { authorize: routeContextAccess(context), signal: context.signal, proofPurpose: pathProofPurposeSchema.parse(input.proofPurpose) }), + version: 2, + }) +} +export async function requireRecentPathWithContext(profile: RouteAiProfile, candidateId: string, connectionId: string, options: RouteContextOptions = {}, + acquireContext?: (options: RouteContextOptions) => ReturnType) { + // Compare the database timestamp with the database clock. The signed + // observedAt below is independently checked against the application clock. + const queryStarted = performance.now() + const { rows: [row] } = await pool.query(`SELECT id,status,evidence, + EXTRACT(EPOCH FROM (clock_timestamp()-created_at))::double precision * 1000 AS database_age_ms + FROM mcp_path_checks WHERE connection_id=$1 AND profile_hash=$2 AND candidate_id=$3 + AND evidence->>'proofPurpose'='community' AND evidence->>'proofVersion'='2' ORDER BY created_at DESC,id DESC LIMIT 1`, [connectionId, fingerprint(profile), candidateId]) + if (!row || row.status !== "passed") throw routingMeasurementRefusal("latest-proof-unavailable", "Нет свежего подтверждения контрольного пути") + const { proof, ...evidence } = row.evidence ?? {} + if (!isCommunityPathEvidence(evidence) || evidence.id !== row.id || evidence.connectionId !== connectionId || evidence.profileHash !== fingerprint(profile) || evidence.candidateId !== candidateId || evidence.status !== "passed" || !Array.isArray(evidence.sourceProofs) || !evidence.sourceProofs.length) throw routingMeasurementRefusal("latest-proof-binding", "Подтверждение пути не связано с Community purpose, источником и контекстом") + const observedAt = Date.parse(evidence.observedAt) + const databaseAge = row.database_age_ms + const assertFresh = () => { + const signedAge = Date.now() - observedAt + // Charge the entire query duration conservatively. This also bounds the + // database clock's deadline after the last SQL read without another query. + if (!Number.isFinite(databaseAge) || databaseAge < 0 || databaseAge + performance.now() - queryStarted > COMMUNITY_PATH_MAX_AGE_MS || + !Number.isFinite(signedAge) || signedAge < 0 || signedAge > COMMUNITY_PATH_MAX_AGE_MS) + throw routingMeasurementRefusal("latest-proof-stale", "Истёк срок подтверждения контрольного пути") + } + assertFresh() + if (options.expectedSources && fingerprint(options.expectedSources) !== fingerprint(evidence.sourceProofs)) throw new McpError("STATE_CONFLICT", "Источник контрольного пути изменился", 409) + const contextOptions = { ...options, selectedCandidateId: candidateId, expectedSources: evidence.sourceProofs } + const current = await (acquireContext ? acquireContext(contextOptions) : readRouteCommunityContext(profile, contextOptions)) + if (!current.verifyEvidence(evidence, proof)) throw routingMeasurementRefusal("latest-proof-context", "Зависимости пути или сохранённое подтверждение изменились") + assertFresh() + return { id: row.id as string, context: current, assertFresh } +} + +export async function requireRecentPath(profile: RouteAiProfile, candidateId: string, connectionId: string, options: RouteContextOptions = {}) { + return (await requireRecentPathWithContext(profile, candidateId, connectionId, options)).id +} diff --git a/backend/src/modules/mcp/plan-expiry.test.ts b/backend/src/modules/mcp/plan-expiry.test.ts new file mode 100644 index 00000000..6a170bb0 --- /dev/null +++ b/backend/src/modules/mcp/plan-expiry.test.ts @@ -0,0 +1,69 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { preparedPlanExpiry } from "./plan-expiry.js" + +const iso = (milliseconds: number) => new Date(Date.now() + milliseconds).toISOString() +const poll = (validUntil?: string) => preparedPlanExpiry("servers.poll", "servers", { validUntil }) + +test("Community honors sealed validity but keeps the ordinary lifetime without a bound", () => { + const bound = iso(90000) + assert.equal(preparedPlanExpiry("routing.community.switch", "network", { validUntil: bound })?.toISOString(), bound) + assert.equal(preparedPlanExpiry("routing.community.switch", "network", { validUntil: iso(-1000) }), null) + const before = Date.now(), manual = preparedPlanExpiry("routing.community.switch", "network", {})!.getTime() + assert(manual >= before + 300000 && manual <= Date.now() + 300000) +}) + +test("Poll uses its existing sealed five-minute validity as the nominal window", () => { + const bound = iso(300000) + assert.equal(poll(bound)?.toISOString(), bound) +}) + +test("scoped inverse review expires at the sealed deadline and rejects missing or elapsed validity", () => { + const step = { kind: "service", service: "routing.community.scoped.recover", input: {} } as const + const bound = iso(45000) + assert.equal(preparedPlanExpiry("operations.recover", "network", { validUntil: bound, steps: [step] })?.toISOString(), bound) + assert.equal(preparedPlanExpiry("operations.recover", "network", { validUntil: iso(-1000), steps: [step] }), null) + assert.equal(preparedPlanExpiry("operations.recover", "network", { steps: [step] }), null) + const before = Date.now() + const generic = preparedPlanExpiry("operations.recover", "network", { + validUntil: iso(-1000), steps: [{ ...step, service: "routing.community.restore" }], + })!.getTime() + assert(generic >= before + 300000 && generic <= Date.now() + 300000) +}) +for (const [name, milliseconds] of [["credential", 220000], ["policy", 240000], ["owner", 260000]] as const) { + test(`Poll preserves a shorter original ${name} deadline`, () => { + const bound = iso(milliseconds) + assert.equal(poll(bound)?.toISOString(), bound) + }) +} +test("Poll cannot extend the old ordinary upper bound with longer evidence", () => { + const before = Date.now(), actual = poll(iso(1800000))!.getTime(), after = Date.now() + assert(actual >= before + 900000 && actual <= after + 900000) +}) +for (const [name, value] of [["missing", undefined], ["malformed", "invalid"], ["permissive Date.parse", "1"], ["non-datetime", "9999"], ["expired", new Date(0).toISOString()]] as const) { + test(`Poll ${name} validity refuses instead of minting a nominal deadline`, () => { + assert.equal(poll(value), null) + }) +} +for (const section of ["servers", "network"]) { + test(`generic ${section} plans keep the original lifetime even with expired evidence`, () => { + const before = Date.now(), actual = preparedPlanExpiry("generic.operation", section, { validUntil: new Date(0).toISOString() })!.getTime(), after = Date.now() + const lifetime = section === "network" ? 300000 : 900000 + assert(actual >= before + lifetime && actual <= after + lifetime) + }) +} +test("generic plans never read evidence validity, including a throwing getter", () => { + const evidence = { get validUntil(): string { throw Error("Generic evidence must stay unread") } } + assert(preparedPlanExpiry("generic.operation", "servers", evidence) instanceof Date) + assert(preparedPlanExpiry("generic.operation", "network", evidence) instanceof Date) +}) +test("Poll reads its trusted validity once and does not mutate the evidence", () => { + const bound = iso(300000); let reads = 0 + const evidence = Object.freeze({ get validUntil() { reads++; return bound } }) + assert.equal(preparedPlanExpiry("servers.poll", "servers", evidence)?.toISOString(), bound) + assert.equal(reads, 1) +}) +test("Poll accepts strict datetime offsets while preserving their absolute instant", () => { + const absolute = iso(300000), offset = absolute.replace(/Z$/, "+00:00") + assert.equal(poll(offset)?.toISOString(), absolute) +}) diff --git a/backend/src/modules/mcp/plan-expiry.ts b/backend/src/modules/mcp/plan-expiry.ts new file mode 100644 index 00000000..53e6cae1 --- /dev/null +++ b/backend/src/modules/mcp/plan-expiry.ts @@ -0,0 +1,18 @@ +import { z } from "zod" +import type { ChangeEvidence } from "./action-types.js" + +/** Internal value calculation only; evidence comes from action.prepare, never + * caller JSON. Generic plans may outlive their preparation access credential. + * A missing result refuses a time-bound plan before storage. */ +export function preparedPlanExpiry(operationId: string, section: string, evidence: Pick & Partial>): Date | null { + const now = Date.now(), ordinaryExpiry = now + (section === "network" ? 300000 : 900000) + const step = operationId === "operations.recover" && evidence.steps?.length === 1 ? evidence.steps[0] : undefined + const scopedRecovery = step?.kind === "service" && step.service === "routing.community.scoped.recover" + if (operationId !== "servers.poll" && operationId !== "routing.community.switch" && !scopedRecovery) return new Date(ordinaryExpiry) + const validUntil = evidence.validUntil + if (operationId === "routing.community.switch" && validUntil === undefined) return new Date(ordinaryExpiry) + const validity = z.string().datetime({ offset: true }).safeParse(validUntil) + const bound = validity.success ? Date.parse(validity.data) : NaN + if (!Number.isFinite(bound) || bound <= now) return null + return new Date(Math.min(ordinaryExpiry, bound)) +} diff --git a/backend/src/modules/mcp/policy.test.ts b/backend/src/modules/mcp/policy.test.ts new file mode 100644 index 00000000..7b8ae087 --- /dev/null +++ b/backend/src/modules/mcp/policy.test.ts @@ -0,0 +1,171 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { mcpOperationDescriptorSchema, mcpPolicyPreviewRequestSchema, mcpPolicyDecisionSchema, mcpTemporaryGrantSchema, type McpPolicyPreviewRequest } from "@mmapp/contracts/mcp" +import { evaluateMcpPolicy } from "./policy.js" +import { findMcpOperation, listMcpOperations } from "./registry.js" + +const now = new Date("2026-10-16T12:00:00Z") +test("operation revisions can advance independently of policy version and remain SQL-int bounded", () => { + const descriptor = findMcpOperation("routing.ospf.optimize")! + assert.equal(mcpOperationDescriptorSchema.parse({ ...descriptor, version: 2 }).version, 2) + assert.equal(mcpOperationDescriptorSchema.parse({ ...descriptor, version: 2147483647 }).version, 2147483647) + for (const version of [0, -1, 1.5, 2147483648, NaN, Infinity]) assert.equal(mcpOperationDescriptorSchema.safeParse({ ...descriptor, version }).success, false) +}) +function request(operationId = "routing.ospf.optimize"): McpPolicyPreviewRequest { + return mcpPolicyPreviewRequestSchema.parse({ + operationId, + policy: { + id: "connection-1", version: 1, status: "active", expiresAt: "2026-10-17T12:00:00Z", mode: "autonomous", + permissions: ["mm:network:write", "mm:servers:read", "mm:users:write", "mm:settings:admin"], + scope: { serverIds: [1, 2], operationIds: listMcpOperations().map(operation => operation.id), allowGlobal: true }, + }, + ownerPermissions: ["mm:network:write", "mm:servers:read", "mm:settings:admin"], + target: { scope: "servers", serverIds: [1] }, objectCount: 1, + }) +} +function denied(input: McpPolicyPreviewRequest, code: string) { + const result = mcpPolicyDecisionSchema.parse(evaluateMcpPolicy(input, now)) + assert.equal(result.decision, "denied") + assert.ok(result.reasons.some(reason => reason.code === code), JSON.stringify(result)) +} + +test("mode × dangerous policy matrix never grants autonomy implicitly", () => { + for (const mode of ["observer", "approval", "autonomous"] as const) { + for (const dangerousPolicy of ["approval", "explicit-allow"] as const) { + for (const explicitlyAllowed of [false, true]) { + const input = request() + Object.assign(input.policy, { mode, dangerousPolicy, autonomousDangerousOperationIds: explicitlyAllowed ? [input.operationId] : [] }) + const result = evaluateMcpPolicy(input, now) + assert.equal(result.decision, mode === "observer" ? "denied" : mode === "autonomous" && dangerousPolicy === "explicit-allow" && explicitlyAllowed ? "allowed" : "approval-required") + } + } + } +}) + +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") + const probe = request("diagnostics.bandwidth"); probe.policy.mode = "observer" + denied(probe, "OBSERVER_WRITE_DENIED") + const user = request("users.create"); user.policy.mode = "observer"; user.target = { scope: "global" } + denied(user, "OBSERVER_WRITE_DENIED") +}) + +test("owner and connection permissions intersect, including read/write inheritance", () => { + const input = request("logs.query") + input.ownerPermissions = ["mm:servers:write"] + assert.equal(evaluateMcpPolicy(input, now).decision, "allowed") + input.policy.permissions = [] + denied(input, "CONNECTION_PERMISSION_MISSING") + input.policy.permissions = ["mm:servers:read"]; input.ownerPermissions = [] + denied(input, "OWNER_PERMISSION_MISSING") + const roles = request("users.permissions.update"); roles.target = { scope: "global" }; roles.ownerPermissions = ["mm:users:write"] + denied(roles, "OWNER_PERMISSION_MISSING") +}) + +test("unknown, newly added, paused, revoked and expired access fail closed", () => { + denied(request("not.registered"), "UNKNOWN_OPERATION") + for (const status of ["paused", "revoked"] as const) { + const input = request(); input.policy.status = status; denied(input, "CONNECTION_INACTIVE") + } + const expired = request(); expired.policy.expiresAt = now.toISOString(); denied(expired, "CONNECTION_EXPIRED") + const absent = request(); absent.policy.scope.operationIds = []; denied(absent, "OPERATION_DENIED") +}) + +test("scope checks reject hidden dependency servers and global substitutions", () => { + const input = request(); input.target = { scope: "servers", serverIds: [1, 3] }; denied(input, "SERVER_DENIED") + input.target = { scope: "global" }; denied(input, "TARGET_SCOPE_MISMATCH") + const global = request("database.restore"); global.target = { scope: "global" }; global.policy.scope.allowGlobal = false + denied(global, "GLOBAL_DENIED") + const dependency = request(); dependency.objects = [{ kind: "interface", id: "*1", serverId: 2 }] + denied(dependency, "INVALID_OBJECT_SCOPE") + const serverAlias = request(); serverAlias.objects = [{ kind: "server", id: "2", serverId: 1 }] + denied(serverAlias, "INVALID_OBJECT_SCOPE") +}) + +test("temporary grants only narrow access and expire at the exact boundary", () => { + const input = request("logs.query") + input.temporaryGrant = { id: "g", startsAt: "2026-10-16T11:00:00Z", expiresAt: "2026-10-16T13:00:00Z", scope: { serverIds: [1], operationIds: ["logs.query"], allowGlobal: false } } + assert.equal(evaluateMcpPolicy(input, now).decision, "allowed") + input.target = { scope: "servers", serverIds: [2] }; denied(input, "GRANT_SCOPE_DENIED") + input.target = { scope: "servers", serverIds: [1] }; input.policy.scope.serverIds = [2]; denied(input, "SERVER_DENIED") + input.policy.scope.serverIds = [1]; input.temporaryGrant.expiresAt = now.toISOString(); denied(input, "GRANT_INACTIVE") + input.temporaryGrant.startsAt = "2026-10-16T12:01:00Z"; input.temporaryGrant.expiresAt = "2026-10-16T13:00:00Z"; denied(input, "GRANT_INACTIVE") + const global = request("database.restore"); global.target = { scope: "global" } + global.temporaryGrant = { id: "g", startsAt: "2026-10-16T11:00:00Z", expiresAt: "2026-10-16T13:00:00Z", scope: { serverIds: [1], operationIds: [global.operationId], allowGlobal: true } } + global.objects = [{ kind: "interface", id: "*1", serverId: 2 }] + denied(global, "GRANT_SCOPE_DENIED") +}) + +test("server and dependent object protections override explicit autonomous allowance", () => { + const input = request() + input.policy.dangerousPolicy = "explicit-allow"; input.policy.autonomousDangerousOperationIds = [input.operationId] + input.objects = [{ kind: "interface", id: "*1", serverId: 1 }] + input.protections = [{ kind: "interface", id: "*1", serverId: 1, protection: "human-approval-required" }] + assert.equal(evaluateMcpPolicy(input, now).decision, "approval-required") + input.protections[0].protection = "deny"; denied(input, "PROTECTED_OBJECT") + input.protections = [{ kind: "server", id: "1", protection: "deny" }]; denied(input, "PROTECTED_OBJECT") + input.protections = [{ kind: "interface", id: "*1", serverId: 2, protection: "deny" }] + assert.equal(evaluateMcpPolicy(input, now).decision, "allowed") + const global = request("database.restore"); global.target = { scope: "global" }; global.objects = input.objects + global.protections = [{ kind: "server", id: "1", protection: "deny" }]; denied(global, "PROTECTED_OBJECT") +}) + +test("globally unique probe protections also cover a server-scoped probe reference", () => { + const input = request() + for (const kind of ["ping-probe", "speed-probe"]) { + input.objects = [{ kind, id: "probe", serverId: 1 }] + input.protections = [{ kind, id: "probe", protection: "deny" }] + denied(input, "PROTECTED_OBJECT") + input.protections[0].serverId = 2 + assert.ok(!evaluateMcpPolicy(input, now).reasons.some(reason => reason.code === "PROTECTED_OBJECT")) + } +}) + +test("budgets, object counts and audit availability cannot be bypassed by approval", () => { + const input = request(); input.policy.mode = "approval" + input.auditAvailable = false; denied(input, "AUDIT_UNAVAILABLE") + input.auditAvailable = true; input.usage.mutationsInHour = 10; denied(input, "BUDGET_EXCEEDED") + input.usage.mutationsInHour = 0; input.objectCount = 101; denied(input, "PLAN_LIMIT_EXCEEDED") + input.objectCount = 0; input.objects = Array.from({ length: 101 }, (_, i) => ({ kind: "route", id: String(i), serverId: 1 })); denied(input, "PLAN_LIMIT_EXCEEDED") + input.objects = []; input.policy.limits.serversPerPlan = 1; input.target = { scope: "servers", serverIds: [1, 2] }; denied(input, "PLAN_LIMIT_EXCEEDED") + const global = request("database.restore"); global.target = { scope: "global" }; global.policy.limits.serversPerPlan = 1 + global.objects = [{ kind: "server", id: "1", serverId: 1 }, { kind: "server", id: "2", serverId: 2 }] + denied(global, "PLAN_LIMIT_EXCEEDED") + const read = request("logs.query"); read.usage.callsInMinute = 60; denied(read, "RATE_LIMITED") + read.usage.callsInMinute = 0; read.usage.concurrentReads = 4; denied(read, "RATE_LIMITED") +}) + +test("invalid contracts reject unknown fields, duplicate IDs and unbounded grants", () => { + assert.equal(mcpPolicyPreviewRequestSchema.safeParse({ ...request(), approved: true }).success, false) + const duplicate = request(); duplicate.policy.scope.serverIds = [1, 1] + assert.equal(mcpPolicyPreviewRequestSchema.safeParse(duplicate).success, false) + assert.equal(mcpTemporaryGrantSchema.safeParse({ id: "g", startsAt: now.toISOString(), expiresAt: "2026-10-18T12:00:00Z", scope: request().policy.scope }).success, false) + const negative = request(); negative.usage.callsInMinute = -1 + assert.equal(mcpPolicyPreviewRequestSchema.safeParse(negative).success, false) +}) + +test("simulation includes the entire batch in remaining budget, with an inclusive exact-fit boundary", () => { + const input = request() + input.policy.limits.mutationsPerHour = 10 + input.usage.mutationsInHour = 8 + input.objectCount = 2 + assert.equal(evaluateMcpPolicy(input, now).decision, "approval-required") + input.objectCount = 3 + denied(input, "BUDGET_EXCEEDED") + input.usage.mutationsInHour = 0; input.objectCount = 11 + denied(input, "BUDGET_EXCEEDED") + const read = request("logs.query") + read.usage.mutationsInHour = 10; read.objectCount = 100 + assert.equal(evaluateMcpPolicy(read, now).decision, "allowed") +}) + +test("simulation has no side effects and registry copies cannot change trusted metadata", () => { + const input = request(); const before = structuredClone(input) + assert.deepEqual(evaluateMcpPolicy(input, now), evaluateMcpPolicy(input, now)) + assert.deepEqual(input, before) + const descriptor = findMcpOperation(input.operationId)! + descriptor.requiredPermissions.length = 0 + assert.deepEqual(findMcpOperation(input.operationId)!.requiredPermissions, ["mm:network:write"]) + assert.ok(listMcpOperations().every(operation => operation.implementation === "planned")) +}) diff --git a/backend/src/modules/mcp/policy.ts b/backend/src/modules/mcp/policy.ts new file mode 100644 index 00000000..6adbd8e7 --- /dev/null +++ b/backend/src/modules/mcp/policy.ts @@ -0,0 +1,94 @@ +import { + mcpPolicyPreviewRequestSchema, + type McpPolicyDecision, + type McpPolicyPreviewRequest, + type McpPolicyReasonCode, +} from "@mmapp/contracts/mcp" +import { hasPermission } from "../../lib/permissions.js" +import { findMcpOperation } from "./registry.js" + +/** 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) + if (!Number.isFinite(now.getTime())) throw new Error("Некорректное время проверки MCP") + const { policy, target, usage } = input + const denied: McpPolicyDecision["reasons"] = [] + const approvals: McpPolicyDecision["reasons"] = [] + const deny = (code: McpPolicyReasonCode, message: string) => { denied.push({ code, message }) } + const approve = (message: string) => { approvals.push({ code: "APPROVAL_REQUIRED", message }) } + const result = (): McpPolicyDecision => ({ + decision: denied.length ? "denied" : approvals.length ? "approval-required" : "allowed", + reasons: [...denied, ...approvals], + limits: policy.limits, + }) + const operation = findMcpOperation(input.operationId) + if (!operation) { + deny("UNKNOWN_OPERATION", "Операция отсутствует в реестре MCP") + return result() + } + if (policy.status !== "active") deny("CONNECTION_INACTIVE", "Подключение приостановлено или отозвано") + if (Date.parse(policy.expiresAt) <= now.getTime()) deny("CONNECTION_EXPIRED", "Срок подключения истёк") + if (!policy.scope.operationIds.includes(operation.id)) deny("OPERATION_DENIED", "Операция не выдана подключению") + for (const permission of operation.requiredPermissions) { + if (!hasPermission(input.ownerPermissions, permission)) deny("OWNER_PERMISSION_MISSING", `У владельца нет права ${permission}`) + if (!hasPermission(policy.permissions, permission)) deny("CONNECTION_PERMISSION_MISSING", `Подключению не выдано право ${permission}`) + } + if (operation.scope !== target.scope) deny("TARGET_SCOPE_MISMATCH", "Область операции не совпадает с областью запроса") + if (target.scope === "servers" && target.serverIds.some(id => !policy.scope.serverIds.includes(id))) { + deny("SERVER_DENIED", "Запрос затрагивает неразрешённый сервер") + } + if (target.scope === "global" && !policy.scope.allowGlobal) deny("GLOBAL_DENIED", "Глобальные операции не разрешены") + + const grant = input.temporaryGrant + if (grant) { + if (Date.parse(grant.startsAt) > now.getTime() || Date.parse(grant.expiresAt) <= now.getTime()) deny("GRANT_INACTIVE", "Временный доступ ещё не начался или истёк") + if (!grant.scope.operationIds.includes(operation.id) || + target.scope === "servers" && target.serverIds.some(id => !grant.scope.serverIds.includes(id)) || + target.scope === "global" && !grant.scope.allowGlobal || + input.objects.some(object => object.serverId !== undefined && !grant.scope.serverIds.includes(object.serverId))) { + deny("GRANT_SCOPE_DENIED", "Запрос выходит за пределы временного доступа") + } + } + + const invalidObjectScope = input.objects.some(object => { + if (object.kind === "server" && (object.serverId === undefined || object.id !== String(object.serverId))) return true + if (target.scope === "servers") return object.serverId === undefined || !target.serverIds.includes(object.serverId) + if (target.scope === "connection") return true + return object.serverId !== undefined && !policy.scope.serverIds.includes(object.serverId) + }) + if (invalidObjectScope) { + deny("INVALID_OBJECT_SCOPE", "Объект или зависимость находится вне области запроса") + } + + if (usage.callsInMinute >= policy.limits.callsPerMinute) deny("RATE_LIMITED", "Исчерпан минутный лимит вызовов") + if (operation.effect === "read") { + if (usage.concurrentReads >= policy.limits.concurrentReads) deny("RATE_LIMITED", "Исчерпан лимит одновременного чтения") + return result() + } + + if (policy.mode === "observer") deny("OBSERVER_WRITE_DENIED", "Наблюдателю запрещены изменения и нагрузочные проверки") + if (!input.auditAvailable) deny("AUDIT_UNAVAILABLE", "До начала изменений требуется доступный аудит") + if (usage.mutationsInHour >= policy.limits.mutationsPerHour || usage.mutationsInHour + input.objectCount > policy.limits.mutationsPerHour) deny("BUDGET_EXCEEDED", "Недостаточно часового бюджета для всех изменений плана") + const affectedServers = new Set([ + ...(target.scope === "servers" ? target.serverIds : []), + ...input.objects.flatMap(object => object.serverId === undefined ? [] : [object.serverId]), + ]) + if (affectedServers.size > policy.limits.serversPerPlan) deny("PLAN_LIMIT_EXCEEDED", "Превышено число серверов на план") + if (Math.max(input.objectCount, input.objects.length) > policy.limits.objectsPerPlan) deny("PLAN_LIMIT_EXCEEDED", "Превышено число изменяемых объектов") + + for (const protection of input.protections) { + const matchesServer = protection.kind === "server" && [...affectedServers].some(id => + String(id) === protection.id && (protection.serverId === undefined || protection.serverId === id)) + // Probe IDs are global primary keys even in a server-scoped plan. Existing + // catalog protections may omit serverId; native RouterOS IDs are not global. + const matchesObject = input.objects.some(object => object.kind === protection.kind && object.id === protection.id && (object.serverId === protection.serverId || protection.serverId === undefined && ["ping-probe", "speed-probe"].includes(object.kind))) + if (!matchesServer && !matchesObject) continue + if (protection.protection === "deny") deny("PROTECTED_OBJECT", "Защищённый объект запрещает изменение") + else approve("Защищённый объект требует подтверждения человеком") + } + if (policy.mode === "approval") approve("Режим подключения требует подтверждения человеком") + if (operation.risk === "dangerous" && !(policy.dangerousPolicy === "explicit-allow" && policy.autonomousDangerousOperationIds.includes(operation.id))) { + approve("Опасная операция не разрешена для автономного выполнения") + } + return result() +} diff --git a/backend/src/modules/mcp/poll-policy-coordinator.test.ts b/backend/src/modules/mcp/poll-policy-coordinator.test.ts new file mode 100644 index 00000000..19c94e6f --- /dev/null +++ b/backend/src/modules/mcp/poll-policy-coordinator.test.ts @@ -0,0 +1,115 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { coordinatePollPolicy, PollPolicyCoordinationRefused, type PollPolicyTransaction } from "./poll-policy-coordinator.js" + +function deferred() { let resolve!: () => void; const promise = new Promise(yes => { resolve = yes }); return { promise, resolve } } +function fixture() { + const controller = new AbortController(), events: string[] = [] + const client = Object.freeze({ marker: "one readonly owner" }) + return { controller, events, client, options: { + signal: controller.signal, assertActive: () => { assert(!controller.signal.aborted) }, + retireUnsafeOwner: () => { events.push("owner-retired"); controller.abort() }, + runPolicy: async (work: (value: typeof client) => Promise) => { events.push("policy-enter"); await work(client); events.push("policy-leave") }, + runtime: async (transaction: PollPolicyTransaction) => { events.push("grant-before"); await transaction(async actual => { assert.equal(actual, client); events.push("policy-query") }); events.push("grant-after") }, + verify: async () => { events.push("metadata-verify"); return "verified" }, + } } +} +test("one policy loan precedes metadata; runtime postcheck follows metadata", async () => { + const f = fixture() + assert.equal(await coordinatePollPolicy(f.options), "verified") + assert.deepEqual(f.events, ["grant-before", "policy-enter", "policy-query", "policy-leave", "metadata-verify", "grant-after"]) +}) +test("metadata failure prevents runtime postcheck success and is not replayed", async () => { + const f = fixture(), error = Error("current protection changed") + await assert.rejects(coordinatePollPolicy({ ...f.options, verify: async () => { f.events.push("refused"); throw error } }), x => x === error) + assert(!f.events.includes("grant-after")); assert.equal(f.events.filter(x => x === "policy-enter").length, 1) +}) +test("original grant or policy failure prevents metadata I/O", async () => { + for (const at of ["grant", "policy"]) { + const f = fixture(), error = Error(at) + await assert.rejects(coordinatePollPolicy({ ...f.options, runtime: async tx => { + if (at === "grant") throw error + await tx(async () => { throw error }) + } }), x => x === error) + assert(!f.events.includes("metadata-verify")) + } +}) +test("postcheck failure discards a successfully verified result", async () => { + const f = fixture(), error = Error("original grant revoked after metadata") + await assert.rejects(coordinatePollPolicy({ ...f.options, runtime: async tx => { await tx(async () => {}); throw error } }), x => x === error) + assert(f.events.includes("metadata-verify")) +}) +test("unused, unawaited and replayed transaction protocols cannot return success", async () => { + for (const mode of ["unused", "unawaited", "replayed", "caught-replay"]) { + const f = fixture() + await assert.rejects(coordinatePollPolicy({ ...f.options, runtime: async tx => { + if (mode === "unused") return + if (mode === "unawaited") { void tx(async () => {}).catch(() => {}); return } + await tx(async () => {}) + if (mode === "caught-replay") await tx(async () => {}).catch(() => {}) + else await tx(async () => {}) + } }), PollPolicyCoordinationRefused) + } +}) +test("retained transaction rejects after completion without another policy query", async () => { + const f = fixture(); let retained!: PollPolicyTransaction + await coordinatePollPolicy({ ...f.options, runtime: async tx => { retained = tx; await tx(async () => {}) } }) + const before = f.events.length + await assert.rejects(retained(async () => { throw Error("late work") }), PollPolicyCoordinationRefused) + assert.equal(f.events.length, before) +}) +test("pre-aborted owner performs no callback", async () => { + const f = fixture(); f.controller.abort() + await assert.rejects(coordinatePollPolicy(f.options), PollPolicyCoordinationRefused) + assert.deepEqual(f.events, []) +}) +test("abort during policy or verification retires the coordinator, no late postcheck", async () => { + for (const at of ["policy", "verify"]) { + const f = fixture(), entered = deferred(), release = deferred() + const pending = coordinatePollPolicy({ ...f.options, + runPolicy: async work => { if (at === "policy") { entered.resolve(); await release.promise } await work(f.client) }, + verify: async () => { if (at === "verify") { entered.resolve(); await release.promise } return "verified" }, + }) + await entered.promise; f.controller.abort() + await assert.rejects(pending, PollPolicyCoordinationRefused) + release.resolve(); await new Promise(done => setImmediate(done)) + assert(!f.events.includes("grant-after")) + if (at === "policy") assert(!f.events.includes("policy-query")) + } +}) +test("a provider cannot omit, duplicate, abandon or swallow failure of the policy body", async () => { + for (const mode of ["omit", "duplicate", "abandon", "swallow"]) { + const f = fixture(), started = deferred(), release = deferred() + const pending = coordinatePollPolicy({ ...f.options, + runPolicy: async work => { + if (mode === "omit") return + if (mode === "abandon") { void work(f.client).catch(() => {}); return } + if (mode === "duplicate") { await work(f.client); await work(f.client).catch(() => {}); return } + await work(f.client).catch(() => {}) + }, + runtime: async tx => { await tx(async () => { + if (mode === "swallow") throw Error("policy denied") + if (mode === "abandon") { started.resolve(); await release.promise } + }) }, + }) + // Abandonment is rejected and retires the owner even with an unsettled body. + await assert.rejects(pending) + release.resolve(); await new Promise(done => setImmediate(done)) + assert(!f.events.includes("metadata-verify")); assert(!f.events.includes("grant-after")) + } +}) + +test("owner expiry during error drain does not wait for abandoned policy work", async () => { + const f = fixture(), entered = deferred(), release = deferred(), error = Error("runtime failed") + const pending = coordinatePollPolicy({ ...f.options, runtime: async tx => { + void tx(async () => { entered.resolve(); await release.promise }).catch(() => {}) + await entered.promise + throw error + } }) + await entered.promise + await new Promise(done => setImmediate(done)) + f.controller.abort() + await assert.rejects(pending, actual => actual === error) + release.resolve(); await new Promise(done => setImmediate(done)) + assert(!f.events.includes("metadata-verify")); assert(!f.events.includes("grant-after")) +}) diff --git a/backend/src/modules/mcp/poll-policy-coordinator.ts b/backend/src/modules/mcp/poll-policy-coordinator.ts new file mode 100644 index 00000000..323c70a2 --- /dev/null +++ b/backend/src/modules/mcp/poll-policy-coordinator.ts @@ -0,0 +1,100 @@ +/** Two-phase scheduling only: no database, authority, clock or cached decision. + * The caller owns a bounded readonly transaction and fences the client it loans. + * Policy reads precede verification; runtime postchecks follow verification. */ +export class PollPolicyCoordinationRefused extends Error { + readonly automaticReplay = false + constructor() { super("Poll policy coordination refused") } +} +export type PollPolicyWork = (client: Client) => Promise +export type PollPolicyTransaction = (work: PollPolicyWork) => Promise + +function deferred() { + let resolve!: (value: T | PromiseLike) => void, reject!: (error: unknown) => void + const promise = new Promise((yes, no) => { resolve = yes; reject = no }) + // A phase can fail before its consumer awaits it. Keep the original promise. + void promise.catch(() => {}) + return { promise, resolve, reject } +} + +export async function coordinatePollPolicy(options: { + signal: AbortSignal + assertActive(): void + retireUnsafeOwner(): void + runPolicy(work: PollPolicyWork): Promise + runtime(transaction: PollPolicyTransaction): Promise + verify(): Promise +}): Promise { + const ready = deferred(), verified = deferred(), stopped = deferred() + let active = true, used = false, resumed = false, runtimeEnded = false, violation = false + let policy: Promise | undefined + let body: Promise | undefined + const refuse = () => new PollPolicyCoordinationRefused() + const live = () => { + if (!active || options.signal.aborted) throw refuse() + options.assertActive() + } + const abort = () => { active = false; const error = refuse(); ready.reject(error); verified.reject(error); stopped.reject(error) } + options.signal.addEventListener("abort", abort, { once: true }) + const race = (work: Promise) => Promise.race([work, stopped.promise]) + let runtime: Promise | undefined + try { + live() + const transaction: PollPolicyTransaction = work => { + try { live(); if (used || runtimeEnded) throw refuse() } + catch (error) { violation = true; ready.reject(error); return Promise.reject(error) } + used = true + policy = (async () => { + try { + let calls = 0, completed = false, failed = false, failure: unknown + live() + await options.runPolicy(client => { + calls++ + if (calls !== 1) return Promise.reject(refuse()) + body = Promise.resolve().then(async () => { live(); await work(client); live(); completed = true }).catch(error => { failed = true; failure = error; throw error }) + void body.catch(() => {}) + return body + }) + live() + if (failed) throw failure + if (calls !== 1 || !completed) throw refuse() + ready.resolve() + await race(verified.promise); live() + resumed = true + } catch (error) { ready.reject(error); throw error } + })() + void policy.catch(() => {}) + return policy + } + runtime = Promise.resolve().then(() => { live(); return options.runtime(transaction) }).then(() => { + runtimeEnded = true + // Ignoring the transaction promise must never publish a successful result. + if (violation || !used || !resumed) throw refuse() + live() + }, error => { runtimeEnded = true; throw error }) + void runtime.catch(error => { ready.reject(error); verified.reject(error) }) + await race(ready.promise); live() + const result = await race(Promise.resolve().then(() => { live(); return options.verify() })) + live(); verified.resolve() + await race(runtime); live() + return result + } catch (error) { + verified.reject(error) + if (error instanceof PollPolicyCoordinationRefused && !options.signal.aborted) { + try { options.retireUnsafeOwner() } catch { /* Original refusal is retained. */ } + } + throw error + } finally { + active = false + verified.reject(refuse()) + // A revoked owner may have a deliberately stalled callback. Its outer + // transaction must be destroyed, never released healthy or replayed. + // Every borrowed client query must independently fence before/after I/O. + const drain = Promise.allSettled([...(runtime ? [runtime] : []), ...(policy ? [policy] : []), ...(body ? [body] : [])]) + try { + // Revocation can arrive while draining, after the error was observed. + // Retain the abort listener until that wait ends as well. + if (!options.signal.aborted) await Promise.race([drain, stopped.promise.catch(() => {})]) + else void drain.catch(() => {}) + } finally { options.signal.removeEventListener("abort", abort) } + } +} diff --git a/backend/src/modules/mcp/poll-policy-loan.owned-helper.test.ts b/backend/src/modules/mcp/poll-policy-loan.owned-helper.test.ts new file mode 100644 index 00000000..8a1c057e --- /dev/null +++ b/backend/src/modules/mcp/poll-policy-loan.owned-helper.test.ts @@ -0,0 +1,78 @@ +/** Invoked only inside the guarded disposable principal-source runner. + * Real SQL and origin bridge; this is not full public MCP acceptance. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import type { Pool, PoolClient } from "pg" +import { withVerifiedConfigurationChange, type VerifiedMcpServiceMetadata } from "../../services/config-mutation-policy.js" +import { connectProbeStorage } from "../../services/server-probe-storage-connection.js" +import { getConnection, fixedPrincipalBoundarySource, readSynchronousPrincipalBoundary, effectiveMcpConnectionSource, type McpPrincipal } from "./store.js" +import { withRuntimePollPrincipalSource, captureRuntimePollPrincipalBoundary, coordinateRuntimePollPrincipalBoundary, executeRuntimePollPolicyTransaction } from "./runtime-poll-principal-boundary.js" +import { assertOriginalMcpTemporaryGrant, readMcpTemporaryGrantOrigin, temporaryGrantOriginSql, type OriginalServiceGrant } from "./temporary-grant-origin.js" +import { pollRuntimePolicySql, readPollRuntimePolicySource } from "./poll-runtime-policy-source.js" + +export async function checkOwnedPollPolicyLoan(pool: Pool, fixture: () => Promise<{ principal: McpPrincipal }>) { + const accepted: string[] = [] + for (const mode of ["valid", "replaced-grant", "revoked-credential", "aborted"] as const) { + const { principal } = await fixture(), connection = await getConnection(principal.connectionId) + const grantId = randomUUID(), replacementId = randomUUID() + const payload = { credentialId: principal.credentialId, id: randomUUID(), startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(), scope: { serverIds: [], operationIds: ["servers.poll"], allowGlobal: true } } + const controller = new AbortController(), texts: string[] = [], events: string[] = [] + let active = true, released = false + const lease = await connectProbeStorage({ deadlineAt: Date.now() + 30000, signal: controller.signal }) + const retire = () => { active = false; controller.abort(); if (!released) { released = true; lease.release(true) } } + const client = { query: (async (text: string, values?: unknown[]) => { assert(active); texts.push(text); return lease.query(text, values) }) as PoolClient["query"] } + try { + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grantId, principal.connectionId, JSON.stringify(payload)]) + const origin = await readMcpTemporaryGrantOrigin(pool, principal.connectionId, principal.credentialId) + const original: OriginalServiceGrant = { ...origin, monotonicEnd: performance.now() + 60000, admissionExpiresAt: Date.now() + 30000, admissionMonotonicEnd: performance.now() + 30000 } + await client.query("BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY") + await client.query("SET LOCAL statement_timeout='10s'") + const info = (await client.query("SELECT pg_backend_pid() AS pid,current_setting('transaction_read_only') AS readonly,current_setting('transaction_isolation') AS isolation,now()::text AS clock")).rows[0] + assert.equal(info.readonly, "on"); assert.equal(info.isolation, "read committed") + const metadata: VerifiedMcpServiceMetadata = { operationId: randomUUID(), planId: randomUUID(), operationName: "servers.poll", serviceName: "servers.poll", stepIndex: 0, phase: "forward", operationVersion: 1, policyVersion: 1, generation: connection.generation, principal, originalExpiresAt: original.admissionExpiresAt, sealedEvidenceProof: "a".repeat(64), sealedPayloadProof: "b".repeat(64), originalTemporaryGrantProof: origin.proof, originalTemporaryGrantExpiresAt: origin.expiresAt, serviceInput: { serverId: 1 } } + const run = () => withVerifiedConfigurationChange({ actorId: connection.owner_id, connectionId: principal.connectionId, approved: true, serverIds: [], objects: [], serverWide: true, serviceExecution: metadata, authorize: async () => assert.fail("No hidden authority fallback") }, () => withRuntimePollPrincipalSource(principal, async (_raw, loan) => { + assert(loan) + await executeRuntimePollPolicyTransaction(loan, async sql => { + await assertOriginalMcpTemporaryGrant(sql, principal.connectionId, principal.credentialId, original) + events.push("original-before") + const policy = await readPollRuntimePolicySource(sql, principal.connectionId, principal.credentialId) + assert.equal(policy.temporaryGrant?.id, payload.id); events.push("policy") + }) + await assertOriginalMcpTemporaryGrant(pool, principal.connectionId, principal.credentialId, original) + events.push("original-after") + }, async () => { + const token = captureRuntimePollPrincipalBoundary(principal, fixedPrincipalBoundarySource); assert(token) + const bound = await readSynchronousPrincipalBoundary(principal, client, packet => ({ result: coordinateRuntimePollPrincipalBoundary(token, packet, { client, signal: controller.signal, assertActive() { assert(active && !controller.signal.aborted) }, retireUnsafeOwner: retire }, async () => { + events.push("verify") + if (mode === "replaced-grant") { + await pool.query("UPDATE mcp_grants SET revoked_at=clock_timestamp() WHERE id=$1", [grantId]) + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [replacementId, principal.connectionId, JSON.stringify(payload)]) + } + if (mode === "revoked-credential") await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [principal.credentialId]) + if (mode === "aborted") { retire(); return false } + const current = await effectiveMcpConnectionSource(principal, client) + assert.equal(current.connection.id, principal.connectionId) + return true + }) })) + return await bound.result + })) + if (mode === "valid") { assert.equal(await run(), true); assert.deepEqual(events, ["original-before", "policy", "verify", "original-after"]) } + else { await assert.rejects(run(), mode === "replaced-grant" ? { code: "GRANT_INACTIVE" } : undefined); assert(!events.includes("original-after")) } + assert.equal(texts.filter(text => text === temporaryGrantOriginSql).length, 1) + assert.equal(texts.filter(text => text === pollRuntimePolicySql).length, 1) + assert.equal(texts.filter(text => text.startsWith("BEGIN")).length, 1) + assert(!texts.some(text => text === "COMMIT")) + if (!released) { + assert.deepEqual((await client.query("SELECT pg_backend_pid() AS pid,current_setting('transaction_read_only') AS readonly,current_setting('transaction_isolation') AS isolation,now()::text AS clock")).rows[0], info) + await client.query("ROLLBACK") + } + accepted.push(mode) + } finally { + active = false + if (!released) { released = true; lease.release(true) } + await pool.query("DELETE FROM mcp_grants WHERE id=ANY($1::uuid[])", [[grantId, replacementId]]) + } + } + return { cases: accepted, actualReadonlyTransaction: true, oneBackendAndClock: true, originalGrantBeforeAndAfter: true, physicalGrantReplacementRefused: true, fullPublicMcpAcceptance: false } +} diff --git a/backend/src/modules/mcp/poll-runtime-policy-source.owned-helper.test.ts b/backend/src/modules/mcp/poll-runtime-policy-source.owned-helper.test.ts new file mode 100644 index 00000000..ff756b03 --- /dev/null +++ b/backend/src/modules/mcp/poll-runtime-policy-source.owned-helper.test.ts @@ -0,0 +1,64 @@ +/** Called only by the guarded disposable principal-source SQL entry. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { Pool } from "pg" +import { mcpTemporaryGrantSchema } from "@mmapp/contracts/mcp" +import { pollRuntimePolicySql, readPollRuntimePolicySource } from "./poll-runtime-policy-source.js" + +export async function checkOwnedPollPolicySource(pool: Pool, connectionId: string, credentialId: string) { + const protectionId = randomUUID(), grantId = randomUUID(), leaseId = randomUUID(), extraIds: string[] = [] + const protection = { kind: "界".repeat(200), id: "測".repeat(200), serverId: 1, protection: "deny" } + const grant = mcpTemporaryGrantSchema.parse({ id: grantId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 600000).toISOString(), + scope: { serverIds: [1], operationIds: Array.from({ length: 1000 }, (_, index) => String(index).padStart(4, "0") + "界".repeat(196)), allowGlobal: false } }) + const owner = await pool.connect() + try { + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'owned-policy-source')", [protectionId, JSON.stringify(protection)]) + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grantId, connectionId, JSON.stringify({ ...grant, credentialId })]) + await owner.query("BEGIN") + assert.equal((await owner.query("SHOW transaction_isolation")).rows[0].transaction_isolation, "read committed") + const clock = (await owner.query("SELECT now()::text AS clock,pg_backend_pid() AS pid")).rows[0] + await owner.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'calls',date_trunc('minute',now()),7),($1,'mutations',date_trunc('hour',now()),9)", [connectionId]) + await owner.query("INSERT INTO mcp_read_leases(id,connection_id,expires_at) VALUES($1,$2,now()+interval '1 hour')", [leaseId, connectionId]) + const first = await readPollRuntimePolicySource(owner, connectionId, credentialId) + assert.deepEqual(first.usage, { calls: 7, mutations: 9, reads: 1 }) + assert.deepEqual(first.protections, [{ object_ref: protection }]); assert.deepEqual(first.temporaryGrant, grant) + // Execute the old independent reads against the same transaction clock. + assert.deepEqual(first.protections, (await owner.query("SELECT object_ref FROM mcp_protections LIMIT 1001")).rows) + const raw = (await owner.query("SELECT payload FROM mcp_grants WHERE connection_id=$1 AND revoked_at IS NULL AND payload->>'credentialId'=$2", [connectionId, credentialId])).rows[0].payload + delete raw.credentialId; assert.deepEqual(first.temporaryGrant, mcpTemporaryGrantSchema.parse(raw)) + await pool.query("UPDATE mcp_protections SET object_ref=jsonb_set(object_ref,'{protection}','\"human-approval-required\"'::jsonb) WHERE id=$1", [protectionId]) + await pool.query("UPDATE mcp_grants SET revoked_at=clock_timestamp() WHERE id=$1", [grantId]) + const second = await readPollRuntimePolicySource(owner, connectionId, credentialId) + assert.equal(second.temporaryGrant, undefined); assert.equal(second.protections[0].object_ref.protection, "human-approval-required") + assert.deepEqual((await owner.query("SELECT now()::text AS clock,pg_backend_pid() AS pid")).rows[0], clock) + await owner.query("COMMIT") + // Separate owners and credentials cannot inherit another source's usage/grant. + const other = await readPollRuntimePolicySource(pool, randomUUID(), randomUUID()) + assert.deepEqual(other.usage, { calls: 0, mutations: 0, reads: 0 }); assert.equal(other.temporaryGrant, undefined) + await pool.query("UPDATE mcp_grants SET revoked_at=NULL WHERE id=$1", [grantId]) + const duplicate = randomUUID(); extraIds.push(duplicate) + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) SELECT $1,connection_id,payload FROM mcp_grants WHERE id=$2", [duplicate, grantId]) + await assert.rejects(readPollRuntimePolicySource(pool, connectionId, credentialId), { code: "GRANT_INACTIVE" }) + await pool.query("DELETE FROM mcp_grants WHERE id=$1", [duplicate]) + await pool.query("UPDATE mcp_grants SET payload=payload||jsonb_build_object('opaque',repeat('x',1048577)) WHERE id=$1", [grantId]) + const boundedGrant = (await pool.query(pollRuntimePolicySql, [connectionId, credentialId])).rows[0] + assert.equal(boundedGrant.grant_oversize, true); assert.deepEqual(boundedGrant.grants, [null]) + await assert.rejects(readPollRuntimePolicySource(pool, connectionId, credentialId), { code: "GRANT_INACTIVE" }) + await pool.query("UPDATE mcp_protections SET object_ref=jsonb_build_object('opaque',repeat('x',8193)) WHERE id=$1", [protectionId]) + const boundedProtection = (await pool.query(pollRuntimePolicySql, [connectionId, credentialId])).rows[0] + assert.equal(boundedProtection.protection_oversize, true); assert.deepEqual(boundedProtection.protections, [null]) + await assert.rejects(readPollRuntimePolicySource(pool, connectionId, credentialId), { code: "STATE_UNAVAILABLE" }) + await pool.query("UPDATE mcp_protections SET object_ref=$2::jsonb WHERE id=$1", [protectionId, JSON.stringify(protection)]) + for (let index = 0; index < 1000; index++) extraIds.push(randomUUID()) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) SELECT id,$2::jsonb,'owned-policy-source' FROM unnest($1::uuid[]) AS id", [extraIds.slice(1), JSON.stringify(protection)]) + await assert.rejects(readPollRuntimePolicySource(pool, connectionId, credentialId), { code: "STATE_UNAVAILABLE" }) + return { unicodeMaxProtectionAndGrant: true, actualTransactionClockAndBackendPreserved: true, independentCommittedChangesVisible: true, + noGrantOrUsageCrossConnection: true, ambiguityAndOversizeRefused: true, protectionOverflowRefused: true } + } finally { + await owner.query("ROLLBACK"); owner.release() + await pool.query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [[protectionId, ...extraIds]]) + await pool.query("DELETE FROM mcp_grants WHERE id=ANY($1::uuid[])", [[grantId, ...extraIds]]) + await pool.query("DELETE FROM mcp_read_leases WHERE id=$1", [leaseId]) + await pool.query("DELETE FROM mcp_budget_buckets WHERE connection_id=$1", [connectionId]) + } +} diff --git a/backend/src/modules/mcp/poll-runtime-policy-source.test.ts b/backend/src/modules/mcp/poll-runtime-policy-source.test.ts new file mode 100644 index 00000000..9bb5a9d2 --- /dev/null +++ b/backend/src/modules/mcp/poll-runtime-policy-source.test.ts @@ -0,0 +1,44 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import type { PoolClient } from "pg" +import { pollRuntimePolicySql, readPollRuntimePolicySource } from "./poll-runtime-policy-source.js" + +const empty = () => ({ protection_count: 0, protection_oversize: false, protections: [], calls: 3, mutations: 4, reads: 1, grant_oversize: false, grants: [] }) +const credential = "owned-credential" +const grant = { id: "grant", startsAt: "2026-10-29T00:00:00Z", expiresAt: "2026-10-29T01:00:00Z", scope: { serverIds: [1], operationIds: ["servers.poll"], allowGlobal: false } } +function client(rows: () => unknown[]) { + let queries = 0 + return { get queries() { return queries }, query: (async (text: string, values: unknown[]) => { + queries++; assert.equal(text, pollRuntimePolicySql); assert.deepEqual(values, ["connection", credential]); return { rows: rows() } + }) as PoolClient["query"] } +} +test("each invocation executes a fresh bound SELECT, including protection and current grant changes", async () => { + let row: Record = empty() + const sql = client(() => [row]) + assert.deepEqual(await readPollRuntimePolicySource(sql, "connection", credential), { protections: [], usage: { calls: 3, mutations: 4, reads: 1 }, temporaryGrant: undefined }) + const object = { kind: "server", id: "1", serverId: 1, protection: "deny" } + row = { ...empty(), protection_count: 1, protections: [object], grants: [{ ...grant, credentialId: credential }] } + assert.deepEqual(await readPollRuntimePolicySource(sql, "connection", credential), { protections: [{ object_ref: object }], usage: { calls: 3, mutations: 4, reads: 1 }, temporaryGrant: grant }) + row = empty(); assert.equal((await readPollRuntimePolicySource(sql, "connection", credential)).temporaryGrant, undefined) + assert.equal(sql.queries, 3) +}) +test("missing, duplicated, malformed, truncated and oversized source refuses", async () => { + for (const rows of [[], [empty(), empty()], [{ ...empty(), calls: -1 }], [{ ...empty(), reads: "1" }], [{ ...empty(), extra: true }], + [{ ...empty(), protection_count: 1 }], [{ ...empty(), protection_count: 1001, protections: Array(1001).fill({}) }], + [{ ...empty(), protection_oversize: true }]]) { + await assert.rejects(readPollRuntimePolicySource(client(() => rows), "connection", credential), { code: "STATE_UNAVAILABLE" }) + } +}) +test("ambiguous, oversized, malformed and different-credential grants refuse", async () => { + for (const patch of [{ grants: [{}, {}] }, { grant_oversize: true, grants: [null] }, + { grants: [{ ...grant, credentialId: "other" }] }]) { + await assert.rejects(readPollRuntimePolicySource(client(() => [{ ...empty(), ...patch }]), "connection", credential), { code: "GRANT_INACTIVE" }) + } + await assert.rejects(readPollRuntimePolicySource(client(() => [{ ...empty(), grants: [{ credentialId: credential }] }]), "connection", credential)) +}) +test("source errors propagate without retry or fallback", async () => { + const failure = Error("owned SQL failure"); let queries = 0 + const sql = { query: (async () => { queries++; throw failure }) as PoolClient["query"] } + await assert.rejects(readPollRuntimePolicySource(sql, "connection", credential), error => error === failure) + assert.equal(queries, 1) +}) diff --git a/backend/src/modules/mcp/poll-runtime-policy-source.ts b/backend/src/modules/mcp/poll-runtime-policy-source.ts new file mode 100644 index 00000000..70143099 --- /dev/null +++ b/backend/src/modules/mcp/poll-runtime-policy-source.ts @@ -0,0 +1,53 @@ +import type { PoolClient } from "pg" +import { z } from "zod" +import { mcpProtectedObjectSchema, mcpTemporaryGrantSchema } from "@mmapp/contracts/mcp" +import { McpError } from "./security.js" + +// Data only. Every invocation uses the caller's existing transaction and fresh +// bound values. Original grant checks and the policy decision remain in runtime. +export const pollRuntimePolicySql = `/* mcp-poll:current-policy-source-v1 */ +WITH protections AS MATERIALIZED ( + SELECT object_ref,octet_length(object_ref::text) AS bytes + FROM public.mcp_protections LIMIT 1001 +), grants AS MATERIALIZED ( + SELECT payload,octet_length(payload::text) AS bytes FROM public.mcp_grants + WHERE connection_id=$1 AND revoked_at IS NULL AND payload->>'credentialId'=$2 LIMIT 2 +) +SELECT (SELECT count(*)::int FROM protections) AS protection_count, + COALESCE((SELECT bool_or(bytes>8192) FROM protections),false) AS protection_oversize, + (SELECT COALESCE(jsonb_agg(CASE WHEN bytes<=8192 THEN object_ref ELSE NULL END),'[]'::jsonb) FROM protections) AS protections, + COALESCE((SELECT used FROM public.mcp_budget_buckets WHERE connection_id=$1 AND kind='calls' AND bucket=date_trunc('minute',now())),0) AS calls, + COALESCE((SELECT used FROM public.mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations' AND bucket=date_trunc('hour',now())),0) AS mutations, + (SELECT count(*)::int FROM public.mcp_read_leases WHERE connection_id=$1 AND expires_at>now()) AS reads, + COALESCE((SELECT bool_or(bytes>1048576) FROM grants),false) AS grant_oversize, + (SELECT COALESCE(jsonb_agg(CASE WHEN bytes<=1048576 THEN payload ELSE NULL END),'[]'::jsonb) FROM grants) AS grants` + +const counter = z.number().int().nonnegative().safe() +const sourceSchema = z.object({ + protection_count: counter.max(1001), protection_oversize: z.boolean(), protections: z.array(z.unknown()).max(1001), + calls: counter, mutations: counter, reads: counter, + grant_oversize: z.boolean(), grants: z.array(z.unknown()).max(2), +}).strict() + +export async function readPollRuntimePolicySource(client: Pick, connectionId: string, credentialId: string) { + const result = await client.query(pollRuntimePolicySql, [connectionId, credentialId]) + const parsed = result.rows.length === 1 && sourceSchema.safeParse(result.rows[0]) + if (!parsed || !parsed.success) throw new McpError("STATE_UNAVAILABLE", "Источник проверки политики недоступен", 503) + const source = parsed.data + if (source.protection_count > 1000 || source.protection_count !== source.protections.length || source.protection_oversize) { + throw new McpError("STATE_UNAVAILABLE", "Защиты превышают предел проверки; изменение заблокировано", 503) + } + const protections = source.protections.map(value => ({ object_ref: mcpProtectedObjectSchema.parse(value) })) + if (source.grants.length > 1) throw new McpError("GRANT_INACTIVE", "Неоднозначный временный доступ") + if (source.grant_oversize) throw new McpError("GRANT_INACTIVE", "Временный доступ превышает предел проверки") + let temporaryGrant: z.infer | undefined + if (source.grants.length) { + const payload = z.record(z.string(), z.unknown()).parse(source.grants[0]) + if (payload.credentialId !== credentialId) throw new McpError("GRANT_INACTIVE", "Временный доступ изменён") + const { credentialId: _credential, ...grant } = payload + temporaryGrant = mcpTemporaryGrantSchema.parse(grant) + } + return { protections, usage: { calls: source.calls, mutations: source.mutations, reads: source.reads }, temporaryGrant } +} + +export type PollRuntimePolicySource = Awaited> diff --git a/backend/src/modules/mcp/portal-revocation-event.test.ts b/backend/src/modules/mcp/portal-revocation-event.test.ts new file mode 100644 index 00000000..a64ad657 --- /dev/null +++ b/backend/src/modules/mcp/portal-revocation-event.test.ts @@ -0,0 +1,59 @@ +import assert from "node:assert/strict" +import { createHmac } from "node:crypto" +import { test } from "node:test" +import Fastify from "fastify" +import { portalRevocationEvent } from "./portal-revocation-event.js" +import { mcpPortal } from "../../routes/mcp-portal.js" + +const event = { eventId: "bf3e7529-a044-4dbf-a957-61dd207b61a8", userId: "synthetic-owner", type: "permissions.changed" } +const bytes = (value: unknown) => Buffer.from(JSON.stringify(value)) + +test("legacy events preserve the authenticated delivery-time cutoff", () => { + const result = portalRevocationEvent(bytes(event), "1700000000") + assert.deepEqual(result.event, event) + assert.equal(result.cutoff, 1700000000000) +}) + +test("v2 delayed delivery and re-signing never advance original revocation time", () => { + const original = { ...event, version: 2, eventOccurredAt: 1700000000123 } + const raw = bytes(original) + for (const delivery of ["1700000000", "1700000600", "1700086400"]) { + const result = portalRevocationEvent(raw, delivery) + assert.equal(result.cutoff, original.eventOccurredAt) + assert.deepEqual(result.event, original) + assert.deepEqual(raw, bytes(original)) + } +}) + +test("unrecognized versions, incomplete envelopes and future occurrence refuse", () => { + for (const value of [ + { ...event, version: 3, eventOccurredAt: 1700000000000 }, + { ...event, version: 2 }, + { ...event, eventOccurredAt: 1700000000000 }, + { ...event, version: 2, eventOccurredAt: 1700000001000 }, + { ...event, version: 2, eventOccurredAt: -1 }, + { ...event, version: 2, eventOccurredAt: 1.5 }, + { ...event, version: 2, eventOccurredAt: 1700000000000, authority: true }, + ]) assert.throws(() => portalRevocationEvent(bytes(value), "1700000000")) +}) + +test("a signed future occurrence is rejected as an invalid event over HTTP", async () => { + const previous = process.env.MCP_PORTAL_WEBHOOK_SECRET + const secret = "synthetic-revocation-endpoint-test-secret" + process.env.MCP_PORTAL_WEBHOOK_SECRET = secret + const app = Fastify() + try { + await app.register(mcpPortal) + const timestamp = String(Math.floor(Date.now() / 1000)) + const payload = bytes({ ...event, version: 2, eventOccurredAt: Number(timestamp) * 1000 + 1000 }) + const signature = createHmac("sha256", secret).update(timestamp + ".").update(payload).digest("hex") + const response = await app.inject({ method: "POST", url: "/_internal/mcp/portal/revocations", payload, + headers: { "content-type": "application/json", "x-mm-timestamp": timestamp, "x-mm-signature": `sha256=${signature}` } }) + assert.equal(response.statusCode, 400) + assert.equal(response.json().code, "INVALID_EVENT") + } finally { + await app.close() + if (previous === undefined) delete process.env.MCP_PORTAL_WEBHOOK_SECRET + else process.env.MCP_PORTAL_WEBHOOK_SECRET = previous + } +}) diff --git a/backend/src/modules/mcp/portal-revocation-event.ts b/backend/src/modules/mcp/portal-revocation-event.ts new file mode 100644 index 00000000..1350d74a --- /dev/null +++ b/backend/src/modules/mcp/portal-revocation-event.ts @@ -0,0 +1,16 @@ +import { z } from 'zod' + +export class InvalidPortalEventTime extends Error {} + +const fields = { eventId: z.string().uuid(), userId: z.string().min(1).max(200), type: z.enum(['permissions.changed', 'user.disabled', 'sessions.revoked']) } +const eventSchema = z.union([ + z.object(fields).strict(), + z.object({ ...fields, version: z.literal(2), eventOccurredAt: z.number().int().positive().safe() }).strict(), +]) +/** Only called after HMAC verification. Delivery timestamp is NOT v2 authority time. */ +export function portalRevocationEvent(raw: Buffer, deliveryTimestamp: string) { + const event = eventSchema.parse(JSON.parse(raw.toString('utf8'))) + const cutoff = 'version' in event ? event.eventOccurredAt : Number(deliveryTimestamp) * 1000 + if (cutoff > Number(deliveryTimestamp) * 1000 + 999) throw new InvalidPortalEventTime('PORTAL_EVENT_TIME_INVALID') + return { event, cutoff } +} diff --git a/backend/src/modules/mcp/portal-revocation.ts b/backend/src/modules/mcp/portal-revocation.ts new file mode 100644 index 00000000..6d42d48a --- /dev/null +++ b/backend/src/modules/mcp/portal-revocation.ts @@ -0,0 +1,174 @@ +import { createHash, createHmac, timingSafeEqual } from "node:crypto" +import { randomUUID } from "node:crypto" +import { mkdir, open, readFile, unlink } from "node:fs/promises" +import path from "node:path" +import type { PoolClient } from "pg" +import { principalBytes, sourceAliases, principalSourceSql } from "./principal-security-sql.js" +import { z } from "zod" +import { isDeepStrictEqual } from "node:util" +import { portalRevocationEvent } from "./portal-revocation-event.js" +import { mcpConnectionPolicySchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { McpError, mcpStateDirectory } from "./security.js" +import { publishStateFile } from "./state-file-publish.js" +import { audit, transaction } from "./store.js" +import type { ConnectionRow, McpPrincipal } from "./store.js" + +const ownerFile = (ownerId: string) => path.join(mcpStateDirectory(), "owner-revocations", createHash("sha256").update(ownerId).digest("hex") + ".json") +async function externalCutoff(ownerId: string): Promise { + try { return z.object({ invalidBefore: z.number().int().nonnegative() }).parse(JSON.parse(await readFile(ownerFile(ownerId), "utf8"))).invalidBefore } + catch (error) { + if (error instanceof Error && "code" in error && error.code === "ENOENT") return 0 + throw new McpError("STATE_UNAVAILABLE", "Внешнее состояние отзыва недоступно", 503) + } +} +async function persistCutoff(ownerId: string, at: number) { + const target = ownerFile(ownerId), value = Math.max(at, await externalCutoff(ownerId)) + const directory = path.dirname(target) + const created = await mkdir(directory, { recursive: true, mode: 0o700 }) + const temporary = target + "." + randomUUID() + ".tmp", handle = await open(temporary, "wx", 0o600) + try { + try { await handle.writeFile(JSON.stringify({ invalidBefore: value })); await handle.sync() } finally { await handle.close() } + await publishStateFile(temporary, target) + } catch (error) { await unlink(temporary).catch(() => {}); throw error } + if (process.platform !== "win32") { + // Persist the rename and any directories created for this state file. + const boundary = created ? path.dirname(created) : path.dirname(directory) + for (let current = directory; ; current = path.dirname(current)) { + const descriptor = await open(current, "r") + try { await descriptor.sync() } finally { await descriptor.close() } + if (current === boundary) break + } + } +} +function storedCutoff(value: Date | undefined): number { + const timestamp = value?.getTime() + if (timestamp === undefined || !Number.isSafeInteger(timestamp) || timestamp < 0) + throw new McpError("STATE_UNAVAILABLE", "Состояние отзыва владельца недоступно", 503) + return timestamp +} +export async function lockPortalOwner(client: Pick, ownerId: string) { + await client.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", ["mcp-owner:" + ownerId]) +} +export async function assertPortalClaimsCurrent(actor: Pick, client: Pick = pool) { + const { rows: [row] } = await client.query<{ invalid_before: Date }>("SELECT invalid_before FROM mcp_owner_revocations WHERE owner_id=$1", [actor.id]) + const cutoff = Math.max(row?.invalid_before.getTime() ?? 0, await externalCutoff(actor.id)) + if (cutoff && (!actor.issuedAt || actor.issuedAt <= cutoff)) throw new McpError("OWNER_CLAIMS_REVOKED", "Права портала изменены; войдите заново с актуальным JWT", 401) +} + +// Internal fixed SQL source only. These limits refuse a PRESENT oversized row; +// they never turn a narrower grant or a connection into an absent source. +// Identifier arrays at their contract maxima fit these generous envelopes. +// Unbounded future/opaque SQL fields still require explicit bounded refusal. + +const sourceUnavailable = (): never => { throw new McpError("STATE_UNAVAILABLE", "Текущий источник прав недоступен", 503) } +const utcTimestamp = (value: unknown, nullable = false) => { + if (nullable && value === null) return + if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$/.test(value) || !Number.isFinite(Date.parse(value))) sourceUnavailable() + if (new Date(value as string).toISOString() !== (value as string).slice(0, 23) + "Z") sourceUnavailable() +} + +/** Private data reader, not an authority/capability constructor. BOTH SQL reads + * and the restore-resilient owner file check are owned here. No supplied JSON, + * cutoff, source rows or fallback can replace either current read. */ +export async function readCurrentPrincipalSecuritySource(principal: McpPrincipal, client: Pick, includeGrants: boolean) { + const capture = async () => { + const { rows } = await client.query(principalSourceSql, [principal.credentialId, principal.connectionId, includeGrants]) + if (!Array.isArray(rows) || rows.length < 1 || rows.length > 2) sourceUnavailable() + const row = rows[0] + if (row.__ps_isolation !== "read committed" || row.__ps_exists !== true && row.__ps_exists !== false || row.__ps_oversize !== true && row.__ps_oversize !== false) sourceUnavailable() + if (row.__ps_oversize) sourceUnavailable() + if (!row.__ps_exists) throw new McpError("NOT_FOUND", "Подключение не найдено", 404) + if (row.__ps_guard !== true || !Number.isSafeInteger(row.__ps_bytes) || row.__ps_bytes <= 0 || row.__ps_bytes > principalBytes + || row.id !== principal.connectionId || typeof row.__ps_credential_current !== "boolean" + || typeof row.__ps_policy_sha256 !== "string" || !/^[a-f0-9]{64}$/.test(row.__ps_policy_sha256)) sourceUnavailable() + for (const field of ["__ps_owner_until", "__ps_owner_issued"]) utcTimestamp(row[field]) + for (const field of ["__ps_credential_expires", "__ps_credential_revoked", "__ps_cutoff_text"]) utcTimestamp(row[field], true) + if (row.__ps_credential !== null && row.__ps_credential !== principal.credentialId + || row.__ps_credential === null && (row.__ps_credential_current || row.__ps_credential_expires !== null || row.__ps_credential_revoked !== null) + || row.__ps_credential !== null && row.__ps_credential_expires === null + || row.__ps_credential_current && row.__ps_credential_revoked !== null) sourceUnavailable() + if (row.__ps_settings !== null && row.__ps_settings !== 1 || row.__ps_enabled !== null && typeof row.__ps_enabled !== "boolean") sourceUnavailable() + if (row.__ps_settings === null && row.__ps_enabled !== null) sourceUnavailable() + const connection = { ...row } + for (const key of sourceAliases) delete connection[key] + const rawPolicy = structuredClone(connection.policy) + connection.policy = mcpConnectionPolicySchema.parse(connection.policy) + if (!(connection.owner_valid_until instanceof Date) || !Number.isFinite(connection.owner_valid_until.getTime()) + || !(connection.owner_claims_issued_at instanceof Date) || !Number.isFinite(connection.owner_claims_issued_at.getTime())) sourceUnavailable() + const cutoff = row.__ps_cutoff + if (row.__ps_cutoff_owner === null ? cutoff !== null || row.__ps_cutoff_text !== null + : row.__ps_cutoff_owner !== connection.owner_id || !(cutoff instanceof Date) || !Number.isFinite(cutoff.getTime())) sourceUnavailable() + const common = (value: ConnectionRow) => { + const result = { ...value } + delete result.__ps_grant_id; delete result.__ps_grant_payload; delete result.__ps_grant_oversize; delete result.__ps_grant_sha256 + return result + } + for (const other of rows) if (!isDeepStrictEqual(common(other), common(row))) sourceUnavailable() + const grants: { payload: unknown }[] = [], grantIdentities: unknown[] = [], grantHashes: unknown[] = [], grantOversizes: boolean[] = [] + for (const item of rows) { + if (item.__ps_grant_id === null) { + if (rows.length !== 1 || item.__ps_grant_payload !== null || item.__ps_grant_oversize !== null || item.__ps_grant_sha256 !== null) sourceUnavailable() + } else { + if (!includeGrants || typeof item.__ps_grant_id !== "string" || typeof item.__ps_grant_oversize !== "boolean") sourceUnavailable() + if (item.__ps_grant_oversize ? item.__ps_grant_payload !== null || item.__ps_grant_sha256 !== null + : typeof item.__ps_grant_sha256 !== "string" || !/^[a-f0-9]{64}$/.test(item.__ps_grant_sha256)) sourceUnavailable() + grants.push({ payload: item.__ps_grant_payload }); grantIdentities.push(item.__ps_grant_id) + grantHashes.push(item.__ps_grant_sha256); grantOversizes.push(item.__ps_grant_oversize) + } + } + if (new Set(grantIdentities).size !== grantIdentities.length) sourceUnavailable() + const security = { id: connection.id, owner: connection.owner_id, permissions: structuredClone(connection.owner_permissions), rawPolicy, policyHash: row.__ps_policy_sha256, + generation: connection.generation, ownerUntil: row.__ps_owner_until, ownerIssued: row.__ps_owner_issued, + credential: row.__ps_credential, credentialCurrent: row.__ps_credential_current, credentialExpires: row.__ps_credential_expires, credentialRevoked: row.__ps_credential_revoked, + cutoffOwner: row.__ps_cutoff_owner, cutoff: row.__ps_cutoff_text, settings: row.__ps_settings, enabled: row.__ps_enabled, + grants: structuredClone(grants), grantIdentities, grantHashes, grantOversizes } + return { connection, grants, security, grantOversized: grantOversizes.includes(true), credentialCurrent: row.__ps_credential_current, enabled: row.__ps_enabled, cutoff: cutoff?.getTime() ?? 0 } + } + const before = await capture(), fileCutoff = await externalCutoff(before.connection.owner_id) + const portalCurrent = (value: typeof before) => { + const cutoff = Math.max(value.cutoff, fileCutoff) + if (cutoff && (!value.connection.owner_claims_issued_at.getTime() || value.connection.owner_claims_issued_at.getTime() <= cutoff)) + throw new McpError("OWNER_CLAIMS_REVOKED", "Права портала изменены; войдите заново с актуальным JWT", 401) + } + portalCurrent(before) + const after = await capture() + if (after.connection.owner_id !== before.connection.owner_id) throw new McpError("STATE_CONFLICT", "Текущий источник прав изменился", 409) + portalCurrent(after) + if (!isDeepStrictEqual(before.security, after.security)) throw new McpError("STATE_CONFLICT", "Текущий источник прав изменился", 409) + return { connection: after.connection, grantRows: after.grants, grantOversized: after.grantOversized, credentialCurrent: after.credentialCurrent, enabled: after.enabled, credentialExpiresAt: after.security.credentialExpires } +} +export async function acceptPortalRevocation(raw: Buffer, timestamp: unknown, signature: unknown, secret: string) { + if (secret.length < 32) throw new McpError("WEBHOOK_DISABLED", "Webhook портала не настроен", 503) + if (!Buffer.isBuffer(raw) || raw.length > 4096 || typeof timestamp !== "string" || !/^\d{10}$/.test(timestamp) || Math.abs(Date.now() - Number(timestamp) * 1000) > 300000 || typeof signature !== "string" || !/^sha256=[a-f0-9]{64}$/.test(signature)) throw new McpError("UNAUTHORIZED", "Подпись события недействительна", 401) + const expected = createHmac("sha256", secret).update(timestamp + ".").update(raw).digest() + if (!timingSafeEqual(expected, Buffer.from(signature.slice(7), "hex"))) throw new McpError("UNAUTHORIZED", "Подпись события недействительна", 401) + const { event, cutoff } = portalRevocationEvent(raw, timestamp) + const hash = createHash("sha256").update(raw).digest("hex") + return transaction(async client => { + await lockPortalOwner(client, event.userId) + const inserted = await client.query("INSERT INTO mcp_portal_events(id,owner_id,payload_hash) VALUES($1,$2,$3) ON CONFLICT DO NOTHING RETURNING id", [event.eventId, event.userId, hash]) + if (!inserted.rowCount) { + const { rows: [prior] } = await client.query("SELECT owner_id,payload_hash FROM mcp_portal_events WHERE id=$1", [event.eventId]) + if (prior.owner_id !== event.userId || prior.payload_hash !== hash) throw new McpError("STATE_CONFLICT", "ID события уже использован", 409) + const { rows: [current] } = await client.query<{ invalid_before: Date }>("SELECT invalid_before FROM mcp_owner_revocations WHERE owner_id=$1", [event.userId]) + // A legacy duplicate can be signed with a later delivery timestamp. + // Repair using the recorded authority, without advancing it on replay. + await persistCutoff(event.userId, storedCutoff(current?.invalid_before)) + return { ok: true, duplicate: true } + } + const { rows: [current] } = await client.query<{ invalid_before: Date }>("INSERT INTO mcp_owner_revocations(owner_id,invalid_before) VALUES($1,to_timestamp($2::double precision/1000)) ON CONFLICT(owner_id) DO UPDATE SET invalid_before=GREATEST(mcp_owner_revocations.invalid_before,EXCLUDED.invalid_before),updated_at=now() RETURNING invalid_before", [event.userId, cutoff]) + await client.query("SELECT id FROM mcp_connections WHERE owner_id=$1 AND owner_claims_issued_at<=to_timestamp($2::double precision/1000) ORDER BY id FOR UPDATE", [event.userId, cutoff]) + await client.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"revoked\"'),owner_valid_until=now(),updated_at=now() WHERE owner_id=$1 AND owner_claims_issued_at<=to_timestamp($2::double precision/1000)", [event.userId, cutoff]) + await client.query("UPDATE mcp_credentials SET revoked_at=now() WHERE connection_id IN (SELECT id FROM mcp_connections WHERE owner_id=$1 AND owner_claims_issued_at<=to_timestamp($2::double precision/1000)) AND revoked_at IS NULL", [event.userId, cutoff]) + await client.query("UPDATE mcp_grants SET revoked_at=now() WHERE connection_id IN (SELECT id FROM mcp_connections WHERE owner_id=$1 AND owner_claims_issued_at<=to_timestamp($2::double precision/1000)) AND revoked_at IS NULL", [event.userId, cutoff]) + await client.query("UPDATE mcp_plans SET state='invalidated' WHERE connection_id IN (SELECT id FROM mcp_connections WHERE owner_id=$1 AND owner_claims_issued_at<=to_timestamp($2::double precision/1000)) AND state IN ('draft','approved','awaiting-approval')", [event.userId, cutoff]) + await client.query("UPDATE mcp_routing_profiles SET enabled=false,state=state || '{\"pausedReason\":\"Права владельца отозваны порталом\"}'::jsonb WHERE connection_id IN (SELECT id FROM mcp_connections WHERE owner_id=$1 AND owner_claims_issued_at<=to_timestamp($2::double precision/1000))", [event.userId, cutoff]) + await audit(client, "auth-portal", "owner.revoke", null, { eventId: event.eventId, ownerId: event.userId, type: event.type, eventOccurredAt: cutoff }) + // The cutoff survives a later database restore and closes stale JWT regrant. + // If commit is lost after this write, access stays revoked and delivery retries. + await persistCutoff(event.userId, storedCutoff(current?.invalid_before)) + return { ok: true, duplicate: false } + }) +} diff --git a/backend/src/modules/mcp/principal-security-source.owned.test.ts b/backend/src/modules/mcp/principal-security-source.owned.test.ts new file mode 100644 index 00000000..e4854231 --- /dev/null +++ b/backend/src/modules/mcp/principal-security-source.owned.test.ts @@ -0,0 +1,261 @@ +/** Owned SQL source/refusal evidence only. Real connection/token/security-source + * factories, synthetic owners, no HTTP/native traffic or MCP mutation success. */ +import assert from "node:assert/strict" +import { createHash, createHmac, randomBytes, randomUUID } from "node:crypto" +import { readFile, unlink, writeFile } from "node:fs/promises" +import { basename, dirname, isAbsolute, join, resolve } from "node:path" +import type { PoolClient } from "pg" +import type { AuthUser } from "../../lib/permissions.js" +import { checkOwnedPollPolicySource } from "./poll-runtime-policy-source.owned-helper.test.js" +import { checkOwnedPollPolicyLoan } from "./poll-policy-loan.owned-helper.test.js" + +assert.equal(process.env.MMAPP_OWN_PRINCIPAL_SECURITY_SOURCE, "true") +assert.equal(process.env.NODE_ENV, "test") +const endpoint = new URL(process.env.DATABASE_URL ?? "") +assert(["postgres:", "postgresql:"].includes(endpoint.protocol)) +assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.username, "mcp_test"); assert(endpoint.password) +assert.equal(endpoint.pathname, "/postgres"); assert.equal(endpoint.search, ""); assert.equal(endpoint.hash, "") +assert(Number.isInteger(Number(endpoint.port)) && Number(endpoint.port) > 0 && Number(endpoint.port) <= 65535) +assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) +assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") +assert.match(basename(dirname(resolve(process.env.MCP_STATE_DIR))), /^mm-mcp-test-[A-Za-z0-9-]+$/) + +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 { 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") +const { ProbeStorageUnknown, ProbeStorageRefused } = await import("../../services/server-probe-journal-maintenance.js") +const connectionIds: string[] = [], groups: string[] = [] +let priorEnabled: boolean | undefined +const check = async (name: string, work: () => Promise) => { await work(); groups.push(name); console.log("Owned principal source group PASS:", name) } +async function fixture() { + const actor: AuthUser = { id: "owned-principal-source-" + randomUUID(), email: "owned@example.invalid", name: "Owned SQL principal source", + apps: ["mm"], permissions: ["mm:servers:read", "mm:servers:write", "mm:mcp:manage", "mm:mcp:admin", "mm:mcp:approve"], + issuedAt: Date.now() - 1000, expiresAt: Date.now() + 3600000 } + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned source", mode: "approval", dangerousPolicy: "explicit-allow", + permissions: ["mm:servers:read", "mm:servers:write"], expiresAt: new Date(actor.expiresAt!).toISOString(), + scope: { serverIds: [], operationIds: ["servers.poll"], allowGlobal: true }, + limits: { callsPerMinute: 10000, mutationsPerHour: 10000, serversPerPlan: 1, objectsPerPlan: 10000 } }), actor) + connectionIds.push(access.connection.id) + const principal = await authenticateMcpToken(access.token) + return { actor, principal } +} +type Fixture = Awaited> +async function grant(f: Fixture, oversized = false) { + const id = randomUUID(), value = mcpTemporaryGrantSchema.parse({ id, startsAt: new Date(Date.now() - 1000).toISOString(), + expiresAt: new Date(Date.now() + 600000).toISOString(), scope: { serverIds: [], operationIds: ["servers.poll"], allowGlobal: false } }) + const payload = { ...value, credentialId: f.principal.credentialId } + if (oversized) await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb||jsonb_build_object('opaque',repeat('x',2097153)))", [id, f.principal.connectionId, JSON.stringify(payload)]) + else await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [id, f.principal.connectionId, JSON.stringify(payload)]) + return payload +} +/** Transparent observer: every call executes original SQL/arguments and returns + * its exact actual result. The independent held writer cannot be reused by pool + * reads. No source row, proof, cutoff or permission decision is substituted. */ +async function between(hook: (writer: PoolClient) => Promise, work: (client: Pick) => Promise) { + const writer = await pool.connect(), texts: string[] = [], observedNumbers: unknown[] = [], observedGrantNumbers: unknown[] = [] + const client: Pick = { query: (async (...args: unknown[]) => { + const result = await Reflect.apply(pool.query, pool, args) + if (typeof args[0] === "string" && args[0].includes("mcp-principal:coherent-source-v1")) { + texts.push(args[0]); observedNumbers.push(result.rows[0]?.policy?.limits?.callsPerMinute) + observedGrantNumbers.push(result.rows[0]?.__ps_grant_payload?.scope?.serverIds?.[0]) + if (texts.length === 1) await hook(writer) + } + return result + }) as PoolClient["query"] } + try { return { result: await work(client), texts, observedNumbers, observedGrantNumbers } } + finally { writer.release(true) } +} +async function refusalBetween(f: Fixture, hook: (writer: PoolClient) => Promise, code: string, effective = false) { + let error: unknown + const observed = await between(hook, async client => { try { if (effective) await effectiveMcpConnectionSource(f.principal, client); else await validatePrincipal(f.principal, client) } catch (caught) { error = caught } }) + assert(error && typeof error === "object" && "code" in error && error.code === code, "Expected fixed source refusal code") + assert.equal(observed.texts.length, code === "OWNER_CLAIMS_REVOKED" ? 1 : 2) + assert(observed.texts.every(text => text === observed.texts[0]), "Both captures must execute the identical actual fixed SQL") + return observed +} +try { + await applySqlMigrations(pool) + 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("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))) + }) + await check("borrowed policy transaction: actual origin bridge and real original-grant/credential races", async () => { + console.log("Owned poll policy loan SQL:", JSON.stringify(await checkOwnedPollPolicyLoan(pool, fixture))) + }) + await check("actual factories, two fixed captures, no grant and exact connection DTO", async () => { + const f = await fixture(), expected = await getConnection(f.principal.connectionId) + const captured = await between(async () => {}, client => validatePrincipal(f.principal, client)) + assert.equal(captured.texts.length, 2); assert.equal(captured.texts[0], captured.texts[1]); assert.deepEqual(captured.result, expected) + assert(captured.result.owner_valid_until instanceof Date); assert(!Object.keys(captured.result).some(key => key.startsWith("__ps_"))) + const effective = await effectiveMcpConnectionSource(f.principal) + assert.deepEqual(effective.grantRows, []); assert.deepEqual(effective.connection, expected) + }) + await check("exact raw grantRows payload shape and excluded-grant base principal", async () => { + const f = await fixture(), payload = await grant(f) + const base = await validatePrincipal(f.principal); assert.equal(base.policy.scope.allowGlobal, true) + const effective = await effectiveMcpConnectionSource(f.principal) + assert.equal(effective.connection.policy.scope.allowGlobal, false) + assert.deepEqual(effective.grantRows, [{ payload }]); assert.deepEqual(Object.keys(effective.grantRows[0]), ["payload"]) + }) + await check("borrowed non-RC readonly transactions fail closed", async () => { + const f = await fixture() + for (const isolation of ["REPEATABLE READ", "SERIALIZABLE"]) { + const owner = await pool.connect() + try { await owner.query("BEGIN ISOLATION LEVEL " + isolation + " READ ONLY"); await assert.rejects(validatePrincipal(f.principal, owner), { code: "STATE_UNAVAILABLE" }) } + finally { await owner.query("ROLLBACK"); owner.release(true) } + } + }) + await check("actual reserved alias column refuses rather than silently replacing c star fields", async () => { + const f = await fixture(); let added = false + try { + await pool.query('ALTER TABLE mcp_connections ADD COLUMN "__ps_exists" boolean'); added = true + await assert.rejects(validatePrincipal(f.principal), { code: "STATE_UNAVAILABLE" }) + } finally { if (added) await pool.query('ALTER TABLE mcp_connections DROP COLUMN "__ps_exists"') } + assert.equal((await validatePrincipal(f.principal)).id, f.principal.connectionId) + }) + await check("independent SQL credential, settings and owner changes between captures refuse", async () => { + const credential = await fixture() + await refusalBetween(credential, async writer => { await writer.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1 AND connection_id=$2", [credential.principal.credentialId, credential.principal.connectionId]) }, "STATE_CONFLICT") + const settings = await fixture() + try { await refusalBetween(settings, async writer => { await writer.query("UPDATE mcp_settings SET enabled=false WHERE id=1") }, "STATE_CONFLICT") } + finally { await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") } + const owner = await fixture() + await refusalBetween(owner, async writer => { await writer.query("UPDATE mcp_connections SET owner_id=$2 WHERE id=$1", [owner.principal.connectionId, "owned-source-replacement-" + randomUUID()]) }, "STATE_CONFLICT") + }) + await check("actual HMAC portal revoke and private external cutoff interrupt source capture", async () => { + const f = await fixture(), secret = randomBytes(32).toString("hex") + 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 check("duplicate signed revocation repairs the external cutoff without moving recorded authority", async () => { + const f = await fixture(), secret = randomBytes(32).toString("hex") + const cutoffSeconds = Math.floor(Date.now() / 1000) - 10 + const eventId = randomUUID() + const raw = Buffer.from(JSON.stringify({ eventId, userId: f.actor.id, type: "sessions.revoked" })) + const deliver = (seconds: number, body = raw) => { + const timestamp = String(seconds) + const signature = "sha256=" + createHmac("sha256", secret).update(timestamp + ".").update(body).digest("hex") + return acceptPortalRevocation(body, timestamp, signature, secret) + } + assert.deepEqual(await deliver(cutoffSeconds), { ok: true, duplicate: false }) + const latest = cutoffSeconds * 1000 + 123 + const newer = Buffer.from(JSON.stringify({ version: 2, eventId: randomUUID(), userId: f.actor.id, + type: "sessions.revoked", eventOccurredAt: latest })) + assert.deepEqual(await deliver(cutoffSeconds, newer), { ok: true, duplicate: false }) + const file = join(resolve(process.env.MCP_STATE_DIR!), "owner-revocations", createHash("sha256").update(f.actor.id).digest("hex") + ".json") + await unlink(file) + const conflict = Buffer.from(JSON.stringify({ eventId, userId: f.actor.id, type: "user.disabled" })) + await assert.rejects(deliver(cutoffSeconds + 8, conflict), { code: "STATE_CONFLICT" }) + await assert.rejects(readFile(file), { code: "ENOENT" }) + assert.deepEqual(await deliver(cutoffSeconds + 8), { ok: true, duplicate: true }) + assert.deepEqual(JSON.parse(await readFile(file, "utf8")), { invalidBefore: latest }) + const externalLatest = latest + 1 + await writeFile(file, JSON.stringify({ invalidBefore: externalLatest })) + assert.deepEqual(await deliver(cutoffSeconds + 9), { ok: true, duplicate: true }) + assert.deepEqual(JSON.parse(await readFile(file, "utf8")), { invalidBefore: externalLatest }) + const { rows: [row] } = await pool.query<{ invalid_before: Date; audits: number }>( + `SELECT invalid_before, (SELECT count(*)::int FROM mcp_audit WHERE action='owner.revoke' AND payload->>'ownerId'=$1) AS audits + FROM mcp_owner_revocations WHERE owner_id=$1`, [f.actor.id]) + assert.equal(row.invalid_before.getTime(), latest) + assert.equal(row.audits, 2) + }) + await check("actual SQL numeric policy drift hidden by JS rounding refuses the SQL digest", async () => { + const f = await fixture() + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{limits,callsPerMinute}','50'::jsonb) WHERE id=$1", [f.principal.connectionId]) + const observed = await refusalBetween(f, async writer => { await writer.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{limits,callsPerMinute}','50.000000000000000001'::jsonb) WHERE id=$1", [f.principal.connectionId]) }, "STATE_CONFLICT") + assert.deepEqual(observed.observedNumbers, [50, 50], "Actual driver JSON values round alike while the fixed SQL hashes must differ") + }) + await check("oversized PRESENT grant preserves portal then principal then grant refusal order", async () => { + const valid = await fixture(); await grant(valid, true) + await assert.rejects(effectiveMcpConnectionSource(valid.principal), { code: "STATE_UNAVAILABLE" }) + const invalid = await fixture(); await grant(invalid, true) + await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1 AND connection_id=$2", [invalid.principal.credentialId, invalid.principal.connectionId]) + await assert.rejects(effectiveMcpConnectionSource(invalid.principal), { code: "UNAUTHORIZED" }) + const revoked = await fixture(); await grant(revoked, true) + await pool.query("INSERT INTO mcp_owner_revocations(owner_id,invalid_before) VALUES($1,clock_timestamp())", [revoked.actor.id]) + await assert.rejects(effectiveMcpConnectionSource(revoked.principal), { code: "OWNER_CLAIMS_REVOKED" }) + }) + await check("actual SQL numeric grant drift also refuses despite equal JS numbers", async () => { + const f = await fixture(); await grant(f) + await pool.query("UPDATE mcp_grants SET payload=jsonb_set(payload,'{scope,serverIds}','[50]'::jsonb) WHERE connection_id=$1", [f.principal.connectionId]) + const observed = await refusalBetween(f, async writer => { + await writer.query("UPDATE mcp_grants SET payload=jsonb_set(payload,'{scope,serverIds}','[50.000000000000000001]'::jsonb) WHERE connection_id=$1", [f.principal.connectionId]) + }, "STATE_CONFLICT", true) + assert.deepEqual(observed.observedGrantNumbers, [50, 50]) + }) + await check("oversized PRESENT connection is refused before its opaque payload crosses the reader", async () => { + const f = await fixture(); let added = false + try { + await pool.query('ALTER TABLE mcp_connections ADD COLUMN owned_source_padding text'); added = true + await pool.query("UPDATE mcp_connections SET owned_source_padding=repeat('x',8388609) WHERE id=$1", [f.principal.connectionId]) + await assert.rejects(validatePrincipal(f.principal), { code: "STATE_UNAVAILABLE" }) + assert.equal((await pool.query("SELECT id FROM mcp_connections WHERE id=$1", [f.principal.connectionId])).rowCount, 1) + } finally { if (added) await pool.query('ALTER TABLE mcp_connections DROP COLUMN owned_source_padding') } + }) + await check("bounded real driver reuses one protocol parse while binding different current principals and grants", async () => { + const f = await fixture(), other = await fixture(); await grant(f) + const lease = await connectProbeStorage({ deadlineAt: Date.now() + 30000 }) + const client = { query: ((text: string, values?: unknown[]) => lease.query(text, values)) as PoolClient["query"] } + try { + assert.equal((await effectiveMcpConnectionSource(f.principal, client)).connection.policy.scope.allowGlobal, false) + assert.equal((await validatePrincipal(other.principal, client)).id, other.principal.connectionId) + await pool.query("UPDATE mcp_grants SET revoked_at=clock_timestamp() WHERE connection_id=$1", [f.principal.connectionId]) + assert.equal((await effectiveMcpConnectionSource(f.principal, client)).connection.policy.scope.allowGlobal, true) + const prepared = (await lease.query("SELECT name,statement,from_sql,generic_plans,custom_plans FROM pg_prepared_statements WHERE name=$1", [principalSourceStatementName])).rows + assert.equal(prepared.length, 1); assert.equal(prepared[0].statement, principalSourceSql); assert.equal(prepared[0].from_sql, false) + assert(Number(prepared[0].generic_plans) + Number(prepared[0].custom_plans) >= 6) + await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [other.principal.credentialId]) + await assert.rejects(validatePrincipal(other.principal, client), { code: "UNAUTHORIZED" }) + } finally { lease.release() } + }) + await check("prepared source still rejects a real independent policy change BETWEEN captures", async () => { + const f = await fixture(), lease = await connectProbeStorage({ deadlineAt: Date.now() + 30000 }) + let captures = 0 + const client = { query: (async (text: string, values?: unknown[]) => { + const result = await lease.query(text, values) + if (text === principalSourceSql && ++captures === 1) await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{limits,callsPerMinute}','41'::jsonb) WHERE id=$1", [f.principal.connectionId]) + return result + }) as PoolClient["query"] } + try { await assert.rejects(validatePrincipal(f.principal, client), { code: "STATE_CONFLICT" }); assert.equal(captures, 2) } + finally { lease.release() } + }) + await check("actual cached result-shape DDL failure destroys bounded owner with no fallback and fresh checkout succeeds", async () => { + const f = await fixture(), lease = await connectProbeStorage({ deadlineAt: Date.now() + 30000 }) + const client = { query: ((text: string, values?: unknown[]) => lease.query(text, values)) as PoolClient["query"] } + let added = false + try { + await validatePrincipal(f.principal, client) + await pool.query('ALTER TABLE mcp_connections ADD COLUMN owned_prepared_shape text'); added = true + await assert.rejects(validatePrincipal(f.principal, client), error => error instanceof ProbeStorageUnknown) + assert.equal(lease.lifetimeSignal?.aborted, true) + await assert.rejects(lease.query(principalSourceSql, [f.principal.credentialId, f.principal.connectionId, false]), error => error instanceof ProbeStorageRefused) + const fresh = await connectProbeStorage({ deadlineAt: Date.now() + 30000 }) + try { + const current = { query: ((text: string, values?: unknown[]) => fresh.query(text, values)) as PoolClient["query"] } + assert.equal((await validatePrincipal(f.principal, current)).id, f.principal.connectionId) + } finally { fresh.release(true) } + } finally { lease.release(true); if (added) await pool.query('ALTER TABLE mcp_connections DROP COLUMN owned_prepared_shape') } + }) + console.log("Owned principal security source SQL:", JSON.stringify({ groups: groups.length, names: groups, currentMigrations: MIGRATION_MANIFEST.length, + actualConnectionAndTokenFactories: true, actualFixedSqlAndPrivateFilesystem: true, independentSqlWriterBetweenCaptures: true, + actualReservedAliasColumnRefused: true, sqlNumericDigestDriftRefused: true, oversizedPresentGrantPrecedence: true, + componentSqlOnly: true, actualReadonlyMetadataHandlerAccepted: false, actualFullMcpMutationAccepted: false, httpRequests: 0, nativeRequests: 0, + automaticReplay: false, productionDatabaseTouched: false })) +} finally { + try { + if (connectionIds.length) await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"revoked\"'::jsonb) WHERE id=ANY($1::uuid[])", [connectionIds]) + if (priorEnabled !== undefined) await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [priorEnabled]) + } finally { await closePool() } +} diff --git a/backend/src/modules/mcp/principal-security-source.test.ts b/backend/src/modules/mcp/principal-security-source.test.ts new file mode 100644 index 00000000..6b5f4da8 --- /dev/null +++ b/backend/src/modules/mcp/principal-security-source.test.ts @@ -0,0 +1,315 @@ +/** Actual fixed reader/store parser + isolated real filesystem, fake SQL only. + * This is component evidence, never PostgreSQL/MCP runtime acceptance. */ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises" +import { join } from "node:path" +import { fileURLToPath } from "node:url" +import { test } from "node:test" +import type { PoolClient } from "pg" +import { withRuntimePollPrincipalSource, captureRuntimePollPrincipalBoundary, beginRuntimePollPrincipalBoundary, coordinateRuntimePollPrincipalBoundary, executeRuntimePollPolicyTransaction, type RuntimePollBoundary, type RuntimePollPolicyLoan } from "./runtime-poll-principal-boundary.js" +import { temporaryGrantOriginSql } from "./temporary-grant-origin.js" +import { pollRuntimePolicySql } from "./poll-runtime-policy-source.js" +import { withVerifiedConfigurationChange, type VerifiedMcpServiceMetadata } from "../../services/config-mutation-policy.js" +import { pool, closePool } from "../../db/index.js" +import { accessGeneration, mcpStateDirectory, invalidateMcpAccess, fingerprint } from "./security.js" +import { readCurrentPrincipalSecuritySource } from "./portal-revocation.js" +import { fixedPrincipalBoundarySource, readSynchronousPrincipalBoundary, principalBoundaryProjection, type PrincipalBoundaryPacket, effectiveMcpConnectionSource, validatePrincipal, type McpPrincipal } from "./store.js" + +test("two current principal captures with real private cutoff filesystem; component only", async t => { + const state = await mkdtemp(join(fileURLToPath(new URL("../../../../tmp/", import.meta.url)), "mm-mcp-principal-source-")) + const previous = process.env.MCP_STATE_DIR, previousQuery = pool.query, previousConnect = pool.connect + process.env.MCP_STATE_DIR = state + pool.query = (() => { throw Error("No default component SQL") }) as never + pool.connect = (() => { throw Error("No component backend acquisition") }) as never + const utc = (value: number) => new Date(value).toISOString().replace(/(\.\d{3})Z$/, "$1000Z") + const cutoffFile = (owner: string) => join(state, "owner-revocations", createHash("sha256").update(owner).digest("hex") + ".json") + const cutoff = async (owner: string, value: unknown) => { await mkdir(join(state, "owner-revocations"), { recursive: true }); await writeFile(cutoffFile(owner), JSON.stringify(value)) } + try { + function fixture(generation: string) { + const now = Date.now(), principal: McpPrincipal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(now + 60000).toISOString() } + const connection = { id: principal.connectionId, name: "ordinary", owner_id: randomUUID(), owner_permissions: ["mm:servers:write"], generation, + created_at: new Date(now), updated_at: new Date(now), last_used_at: null as Date | null, owner_valid_until: new Date(now + 60000), owner_claims_issued_at: new Date(now - 1000), + opaque_config: { future: "preserve-private-unicode-Ж" }, + policy: { id: principal.connectionId, version: 1, status: "active", mode: "approval", dangerousPolicy: "approval", expiresAt: principal.credentialExpiresAt, + permissions: ["mm:servers:write"], scope: { serverIds: [1, 2], operationIds: ["servers.poll"], allowGlobal: true } } } + const base = () => ({ ...structuredClone(connection), __ps_exists: true, __ps_oversize: false, __ps_bytes: 4096, __ps_guard: true, __ps_isolation: "read committed", + __ps_credential: principal.credentialId as string | null, __ps_credential_current: true, __ps_credential_expires: utc(now + 60000) as string | null, + __ps_credential_revoked: null as string | null, __ps_owner_until: utc(now + 60000), __ps_owner_issued: utc(now - 1000), + __ps_cutoff_owner: null as string | null, __ps_cutoff: null as Date | null, __ps_cutoff_text: null as string | null, + __ps_settings: 1 as number | null, __ps_enabled: true as boolean | null, __ps_grant_id: null as string | null, + __ps_grant_oversize: null as boolean | null, __ps_grant_payload: null as unknown, + __ps_policy_sha256: fingerprint(connection.policy), __ps_grant_sha256: null as string | null }) + type Row = ReturnType + let before: Row[] = [base()], after: Row[] | undefined, hook: ((index: number) => Promise) | undefined + const commands: { text: string; values: unknown[] }[] = [] + const client = { async query(text: string, values: unknown[]) { + commands.push({ text, values }); assert(text.startsWith("/* mcp-principal:coherent-source-v1 */")) + assert.deepEqual(values.slice(0, 2), [principal.credentialId, principal.connectionId]) + await hook?.(commands.length) + return { rows: structuredClone(commands.length % 2 === 1 ? before : after ?? before) } + } } as unknown as Pick + return { principal, connection, client, commands, base, + set: (value: Row[]) => { before = value }, setAfter: (value: Row[]) => { after = value }, hook: (value: typeof hook) => { hook = value }, + source: (grants = false) => readCurrentPrincipalSecuritySource(principal, client, grants) } + } + await t.test("fixed ephemeral packet shares raw/effective projections only synchronously, with two real reader captures", async () => { + const f = fixture(await accessGeneration()), row = f.base(), now = Date.now() + row.__ps_grant_id=randomUUID();row.__ps_grant_oversize=false + row.__ps_grant_payload={credentialId:f.principal.credentialId,id:"owned",startsAt:new Date(now-1000).toISOString(),expiresAt:new Date(now+30000).toISOString(),scope:{serverIds:[1],operationIds:["servers.poll"],allowGlobal:false}} + row.__ps_grant_sha256=fingerprint(row.__ps_grant_payload);f.set([row]) + let held!: PrincipalBoundaryPacket + const result=await readSynchronousPrincipalBoundary(f.principal,f.client,packet=>{held=packet;return {raw:principalBoundaryProjection(packet,f.principal,mcpStateDirectory(),false),effective:principalBoundaryProjection(packet,f.principal,mcpStateDirectory(),true)}}) + assert.equal(f.commands.length,2);assert.deepEqual(f.commands.map(c=>c.values[2]),[true,true]) + assert.deepEqual(result.raw.connection.policy.scope.serverIds,[1,2]);assert.deepEqual(result.effective.connection.policy.scope.serverIds,[1]) + assert.notEqual(result.raw.connection,result.effective.connection);assert(Object.isFrozen(result.raw.connection.policy));assert(Object.isFrozen(result.effective.connection.policy.scope)) + assert.throws(()=>principalBoundaryProjection(held,f.principal,mcpStateDirectory(),false),{code:"PERMISSION_DENIED"}) + assert.throws(()=>principalBoundaryProjection({} as PrincipalBoundaryPacket,f.principal,mcpStateDirectory(),false),{code:"PERMISSION_DENIED"}) + }) + await t.test("packet refuses credential/expiry/directory mismatch and async consume without fallback", async () => { + const f=fixture(await accessGeneration()) + await readSynchronousPrincipalBoundary(f.principal,f.client,packet=>{ + for(const expected of [{...f.principal,credentialId:randomUUID()},{...f.principal,credentialExpiresAt:new Date(Date.now()+30000).toISOString()}])assert.throws(()=>principalBoundaryProjection(packet,expected,mcpStateDirectory(),false),{code:"PERMISSION_DENIED"}) + assert.throws(()=>principalBoundaryProjection(packet,f.principal,mcpStateDirectory()+"-different",false),{code:"PERMISSION_DENIED"}) + }) + await assert.rejects(readSynchronousPrincipalBoundary(f.principal,f.client,async packet=>{await Promise.resolve();return principalBoundaryProjection(packet,f.principal,mcpStateDirectory(),false)}),{code:"STATE_CONFLICT"}) + await new Promise(resolve=>setImmediate(resolve)) + }) + await t.test("fresh packet retains original between-capture policy and credential revocation refusal", async () => { + for(const mode of ["policy","credential"]){const f=fixture(await accessGeneration()),after=f.base();if(mode==="policy")after.policy.version++;else{after.__ps_credential_current=false;after.__ps_credential_revoked=utc(Date.now())}f.setAfter([after]);let consumed=false + await assert.rejects(readSynchronousPrincipalBoundary(f.principal,f.client,()=>{consumed=true}),{code:"STATE_CONFLICT"});assert.equal(consumed,false);assert.equal(f.commands.length,2)} + }) + await t.test("private origin capability: no mint authorization, mismatch fallback, forged/replayed/retired packets denied (trusted inert host only)", async () => { + const f=fixture(await accessGeneration());let fullChecks=0,finishes=0,held!:RuntimePollBoundary + const metadata:VerifiedMcpServiceMetadata={operationId:randomUUID(),planId:randomUUID(),operationName:"servers.poll",serviceName:"servers.poll",stepIndex:0,phase:"forward",operationVersion:1,policyVersion:1,generation:f.connection.generation,principal:f.principal,originalExpiresAt:Date.now()+30000,sealedEvidenceProof:"a".repeat(64),sealedPayloadProof:"b".repeat(64),originalTemporaryGrantProof:"c".repeat(64),originalTemporaryGrantExpiresAt:null,serviceInput:{serverId:1}} + await withVerifiedConfigurationChange({actorId:f.connection.owner_id,connectionId:f.principal.connectionId,approved:true,serverIds:[1],objects:[],serverWide:true,serviceExecution:metadata,authorize:async()=>{fullChecks++}},()=>withRuntimePollPrincipalSource(f.principal,async raw=>{finishes++;assert.equal(raw.id,f.principal.connectionId);assert(Object.isFrozen(raw.policy))},async()=>{ + held=captureRuntimePollPrincipalBoundary(f.principal,fixedPrincipalBoundarySource)! + assert(held);assert.equal(fullChecks,0,"Mint must not hide another full authority call") + for(const expected of [{...f.principal,connectionId:randomUUID()},{...f.principal,credentialId:randomUUID()},{...f.principal,credentialExpiresAt:new Date(Date.now()+30000).toISOString()}])assert.equal(captureRuntimePollPrincipalBoundary(expected,fixedPrincipalBoundarySource),undefined) + assert.equal(captureRuntimePollPrincipalBoundary(f.principal,{...fixedPrincipalBoundarySource}),undefined) + await readSynchronousPrincipalBoundary(f.principal,f.client,packet=>{ + assert.throws(()=>beginRuntimePollPrincipalBoundary({} as RuntimePollBoundary,packet),{code:"PERMISSION_DENIED"}) + const runtime=beginRuntimePollPrincipalBoundary(held,packet) + assert.throws(()=>beginRuntimePollPrincipalBoundary(held,packet),{code:"PERMISSION_DENIED"}) + return {runtime} + }).then(result=>result.runtime) + assert.equal(finishes,1) + })) + assert.throws(()=>beginRuntimePollPrincipalBoundary(held,{} as PrincipalBoundaryPacket),{code:"PERMISSION_DENIED"}) + assert.equal(finishes,1);assert.equal(fullChecks,0) + assert.equal(captureRuntimePollPrincipalBoundary(f.principal,fixedPrincipalBoundarySource),undefined) + }) + await t.test("policy transaction loan pins packet/client, exact SQL/bindings, phases and lifetime", async () => { + for (const mode of ["valid", "foreign-client", "forged-loan", "write", "transaction", "wrong-bindings", "postcheck", "aborted"] as const) { + const f = fixture(await accessGeneration()), controller = new AbortController(), events: string[] = [] + let heldClient: Pick | undefined, heldLoan: RuntimePollPolicyLoan | undefined, retired = 0 + const owner = { signal: controller.signal, assertActive() { assert.equal(controller.signal.aborted, false) }, retireUnsafeOwner() { retired++; controller.abort() } } + const client = { async query(text: string, values: unknown[]) { + if (text === temporaryGrantOriginSql || text === pollRuntimePolicySql) { events.push(text === temporaryGrantOriginSql ? "grant" : "policy"); return { rows: [] } } + return f.client.query(text, values) + } } as Pick + const metadata: VerifiedMcpServiceMetadata = { operationId: randomUUID(), planId: randomUUID(), operationName: "servers.poll", serviceName: "servers.poll", stepIndex: 0, phase: "forward", operationVersion: 1, policyVersion: 1, generation: f.connection.generation, principal: f.principal, originalExpiresAt: Date.now() + 30000, sealedEvidenceProof: "a".repeat(64), sealedPayloadProof: "b".repeat(64), originalTemporaryGrantProof: "c".repeat(64), originalTemporaryGrantExpiresAt: null, serviceInput: { serverId: 1 } } + const run = () => withVerifiedConfigurationChange({ actorId: f.connection.owner_id, connectionId: f.principal.connectionId, approved: true, serverIds: [1], objects: [], serverWide: true, serviceExecution: metadata, authorize: async () => assert.fail("No authority fallback") }, () => withRuntimePollPrincipalSource(f.principal, async (_raw, loan) => { + assert(loan); heldLoan = loan + await executeRuntimePollPolicyTransaction(mode === "forged-loan" ? {} as RuntimePollPolicyLoan : loan, async sql => { + heldClient = sql + if (mode === "aborted") controller.abort() + const bindings = [f.principal.connectionId, mode === "wrong-bindings" ? randomUUID() : f.principal.credentialId] + await sql.query(mode === "write" ? "DELETE FROM mcp_grants" : mode === "transaction" ? "BEGIN" : temporaryGrantOriginSql, bindings) + await sql.query(pollRuntimePolicySql, bindings) + }) + events.push("postcheck") + if (mode === "postcheck") throw Error("Original grant changed") + }, async () => { + const token = captureRuntimePollPrincipalBoundary(f.principal, fixedPrincipalBoundarySource); assert(token) + const bound = await readSynchronousPrincipalBoundary(f.principal, client, packet => { + if (mode === "foreign-client") assert.throws(() => coordinateRuntimePollPrincipalBoundary(token, packet, { ...owner, client: f.client }, async () => assert.fail("Foreign verify")), { code: "PERMISSION_DENIED" }) + return { result: coordinateRuntimePollPrincipalBoundary(token, packet, { ...owner, client }, async () => { events.push("verify"); return 42 }) } + }) + const value = await bound.result + assert.equal(value, 42) + assert.deepEqual(events, ["grant", "policy", "verify", "postcheck"]) + await assert.rejects(executeRuntimePollPolicyTransaction(heldLoan!, async () => assert.fail("Retired loan")), { code: "PERMISSION_DENIED" }) + })) + if (["valid", "foreign-client"].includes(mode)) await run() + else await assert.rejects(run()) + if (["write", "transaction", "wrong-bindings", "aborted", "forged-loan"].includes(mode)) assert.deepEqual(events, []) + if (["write", "transaction", "wrong-bindings"].includes(mode)) assert(retired > 0) + if (heldClient) { const before = events.length; await assert.rejects(heldClient.query(temporaryGrantOriginSql, [f.principal.connectionId, f.principal.credentialId])); assert.equal(events.length, before) } + assert.equal(f.commands.length, 2, "No fallback or hidden principal reread") + } + }) + await t.test("naturally expired active-origin capability refuses rather than choosing compatibility fallback", async () => { + const f=fixture(await accessGeneration()) + const metadata:VerifiedMcpServiceMetadata={operationId:randomUUID(),planId:randomUUID(),operationName:"servers.poll",serviceName:"servers.poll",stepIndex:0,phase:"forward",operationVersion:1,policyVersion:1,generation:f.connection.generation,principal:f.principal,originalExpiresAt:Date.now()+100,sealedEvidenceProof:"a".repeat(64),sealedPayloadProof:"b".repeat(64),originalTemporaryGrantProof:"c".repeat(64),originalTemporaryGrantExpiresAt:null,serviceInput:{serverId:1}} + await withVerifiedConfigurationChange({actorId:f.connection.owner_id,connectionId:f.principal.connectionId,approved:true,serverIds:[1],objects:[],serverWide:true,serviceExecution:metadata,authorize:async()=>assert.fail("Mint must not authorize")},()=>withRuntimePollPrincipalSource(f.principal,async()=>assert.fail("Expired origin must not continue"),async()=>{ + const token=captureRuntimePollPrincipalBoundary(f.principal,fixedPrincipalBoundarySource);assert(token) + await new Promise(resolve=>setTimeout(resolve,125)) + assert.throws(()=>captureRuntimePollPrincipalBoundary(f.principal,fixedPrincipalBoundarySource),{code:"PERMISSION_DENIED"}) + assert.throws(()=>beginRuntimePollPrincipalBoundary(token,{} as PrincipalBoundaryPacket),{code:"PERMISSION_DENIED"}) + })) + assert.equal(f.commands.length,0) + }) + await t.test("valid source makes exactly two identical parameterized reads, preserves DTO/opaque fields and source proof shape", async () => { + const f = fixture(await accessGeneration()), result = await effectiveMcpConnectionSource(f.principal, f.client) + assert.equal(f.commands.length, 2); assert.equal(f.commands[0].text, f.commands[1].text) + assert.deepEqual(f.commands.map(c => c.values[2]), [true, true]); assert.deepEqual(result.grantRows, []) + assert.deepEqual(result.connection.opaque_config, f.connection.opaque_config) + assert(result.connection.owner_valid_until instanceof Date) + assert(!Object.keys(result.connection).some(key => key.startsWith("__ps_"))) + assert.match(f.commands[0].text, /WITH candidate AS MATERIALIZED/) + assert.match(f.commands[0].text, /source_bytes<=8388608/) + assert.match(f.commands[0].text, /payload_bytes>2097152/) + assert.match(f.commands[0].text, /WITH raw_grants AS MATERIALIZED/) + assert.match(f.commands[0].text, /CASE WHEN payload_bytes<=2097152 THEN pg_catalog\.encode\(pg_catalog\.sha256/) + assert.match(f.commands[0].text, /NOT\(COALESCE\(to_jsonb\(c\) \?\| ARRAY\[/) + assert.match(f.commands[0].text, /current_setting\('transaction_isolation'\)/) + assert(!/FOR UPDATE|FOR SHARE|INSERT|DELETE|UPDATE\b|;/i.test(f.commands[0].text)) + assert.match(fingerprint({ grants: result.grantRows }), /^[a-f0-9]{64}$/) + }) + await t.test("missing connection remains NOT_FOUND; oversized PRESENT core/grant never becomes absence", async () => { + const f = fixture(await accessGeneration()), absent = f.base(); absent.__ps_exists = false + f.set([absent]); await assert.rejects(f.source(), { code: "NOT_FOUND" }) + for (const kind of ["core", "grant"] as const) { + const x = fixture(await accessGeneration()), row = x.base() + if (kind === "core") { row.__ps_oversize = true; row.__ps_bytes = 8388609 } + else { row.__ps_grant_id = randomUUID(); row.__ps_grant_oversize = true; row.__ps_grant_payload = null } + x.set([row]); await assert.rejects(effectiveMcpConnectionSource(x.principal, x.client), { code: "STATE_UNAVAILABLE" }); assert.equal(x.commands.length, kind === "core" ? 1 : 2) + } + }) + await t.test("RC required on both snapshots and alias collision cannot be ignored", async () => { + for (const at of [1, 2]) for (const isolation of ["repeatable read", "serializable", undefined]) { + const f = fixture(await accessGeneration()), row = f.base(); row.__ps_isolation = isolation as string + if (at === 1) f.set([row]); else f.setAfter([row]) + await assert.rejects(f.source(), { code: "STATE_UNAVAILABLE" }); assert.equal(f.commands.length, at) + } + const f = fixture(await accessGeneration()), row = f.base(); row.__ps_guard = false + f.set([row]); await assert.rejects(f.source(), { code: "STATE_UNAVAILABLE" }) + }) + await t.test("raw policy is compared before parser defaults, while decorative/name/usage changes are permitted", async () => { + const f = fixture(await accessGeneration()), after = f.base() + ;(after.policy as Record).autonomousDangerousOperationIds = [] + f.setAfter([after]); await assert.rejects(f.source(), { code: "STATE_CONFLICT" }) + const x = fixture(await accessGeneration()), decorative = x.base(); decorative.name = "new name"; decorative.last_used_at = new Date(); decorative.updated_at = new Date(Date.now() + 1) + x.setAfter([decorative]); const result = await x.source() + assert.equal(result.connection.name, "new name"); assert.deepEqual(result.connection.last_used_at, decorative.last_used_at) + }) + await t.test("permission, owner, credential, scope, settings, raw grant and exact microsecond drift all refuse", async () => { + const mutations: ((row: ReturnType["base"]>) => void)[] = [ + row => { row.owner_permissions = [] }, row => { row.owner_id = randomUUID() }, row => { row.generation = "changed" }, + row => { row.__ps_credential_current = false }, row => { row.__ps_enabled = false }, row => { row.policy.scope.serverIds = [1] }, + row => { row.__ps_owner_issued = row.__ps_owner_issued.replace(/000Z$/, "001Z") }, + row => { row.__ps_credential_expires = row.__ps_credential_expires!.replace(/000Z$/, "001Z") }, + row => { row.__ps_grant_id = randomUUID(); row.__ps_grant_payload = {}; row.__ps_grant_oversize = false; row.__ps_grant_sha256 = fingerprint({}) }, + ] + for (const mutate of mutations) { + const f = fixture(await accessGeneration()), after = f.base(); mutate(after); f.setAfter([after]) + await assert.rejects(f.source(true), { code: "STATE_CONFLICT" }); assert.equal(f.commands.length, 2) + } + }) + await t.test("actual external-only file revocation and malformed file refuse before second capture, even with invalid credential", async () => { + for (const value of [{ invalidBefore: Date.now() }, { invalidBefore: "malformed" }]) { + const f = fixture(await accessGeneration()), row = f.base(); row.__ps_credential_current = false; f.set([row]) + await cutoff(f.connection.owner_id, value) + await assert.rejects(validatePrincipal(f.principal, f.client), { code: typeof value.invalidBefore === "number" ? "OWNER_CLAIMS_REVOKED" : "STATE_UNAVAILABLE" }) + assert.equal(f.commands.length, 1) + } + }) + await t.test("stable policy/portal/principal/grant refusal precedence is preserved", async () => { + const malformed = fixture(await accessGeneration()), badPolicy = malformed.base() + badPolicy.policy.version = 0; badPolicy.__ps_credential_current = false; malformed.set([badPolicy]) + await cutoff(malformed.connection.owner_id, { invalidBefore: "unreadable-cutoff-shape" }) + await assert.rejects(validatePrincipal(malformed.principal, malformed.client), error => (error as { name?: string }).name === "ZodError") + assert.equal(malformed.commands.length, 1) + const invalid = fixture(await accessGeneration()), badGrant = invalid.base() + badGrant.__ps_credential_current = false; badGrant.__ps_grant_id = randomUUID(); badGrant.__ps_grant_oversize = false; badGrant.__ps_grant_payload = null; badGrant.__ps_grant_sha256 = fingerprint(null) + invalid.set([badGrant]); await assert.rejects(effectiveMcpConnectionSource(invalid.principal, invalid.client), { code: "UNAUTHORIZED" }) + assert.equal(invalid.commands.length, 2) + }) + await t.test("grant oversize defers until portal/principal eligibility, with duplicate count first", async () => { + for (const mode of ["valid", "invalid-credential", "owner-revoked", "duplicate"] as const) { + const f = fixture(await accessGeneration()), row = f.base() + row.__ps_grant_id = randomUUID(); row.__ps_grant_oversize = true; row.__ps_grant_payload = null; row.__ps_grant_sha256 = null + if (mode === "invalid-credential") row.__ps_credential_current = false + if (mode === "owner-revoked") await cutoff(f.connection.owner_id, { invalidBefore: Date.now() }) + f.set(mode === "duplicate" ? [row, { ...row, __ps_grant_id: randomUUID() }] : [row]) + const code = mode === "valid" ? "STATE_UNAVAILABLE" : mode === "invalid-credential" ? "UNAUTHORIZED" : mode === "owner-revoked" ? "OWNER_CLAIMS_REVOKED" : "GRANT_INACTIVE" + await assert.rejects(effectiveMcpConnectionSource(f.principal, f.client), { code }) + assert.equal(f.commands.length, mode === "owner-revoked" ? 1 : 2) + } + }) + await t.test("SQL hash metadata binds precision lost by JS parsing; synthetic producer rows", async () => { + for (const kind of ["policy", "grant"] as const) { + const f = fixture(await accessGeneration()), before = f.base(), after = f.base() + if (kind === "policy") { + before.policy.version = after.policy.version = 50 + before.__ps_policy_sha256 = createHash("sha256").update('{"version":50.000000000000000001}').digest("hex") + after.__ps_policy_sha256 = createHash("sha256").update('{"version":50.000000000000000002}').digest("hex") + assert.equal(JSON.parse('{"version":50.000000000000000001}').version, JSON.parse('{"version":50.000000000000000002}').version) + } else { + before.__ps_grant_id = after.__ps_grant_id = randomUUID() + before.__ps_grant_payload = after.__ps_grant_payload = { opaqueNumber: 9007199254740992 } + before.__ps_grant_oversize = after.__ps_grant_oversize = false + before.__ps_grant_sha256 = createHash("sha256").update('{"opaqueNumber":9007199254740992}').digest("hex") + after.__ps_grant_sha256 = createHash("sha256").update('{"opaqueNumber":9007199254740993}').digest("hex") + assert.deepEqual(JSON.parse('{"opaqueNumber":9007199254740992}'), JSON.parse('{"opaqueNumber":9007199254740993}')) + } + f.set([before]); f.setAfter([after]) + await assert.rejects(f.source(true), { code: "STATE_CONFLICT" }) + } + const f = fixture(await accessGeneration()), row = f.base(); row.__ps_policy_sha256 = "not-a-server-hash" + f.set([row]); await assert.rejects(f.source(), { code: "STATE_UNAVAILABLE" }) + }) + await t.test("new SQL owner cutoff after filesystem read refuses as revoked claims", async () => { + const f = fixture(await accessGeneration()), after = f.base(), now = Date.now() + after.__ps_cutoff_owner = f.connection.owner_id; after.__ps_cutoff = new Date(now); after.__ps_cutoff_text = utc(now) + f.setAfter([after]); await assert.rejects(f.source(), { code: "OWNER_CLAIMS_REVOKED" }); assert.equal(f.commands.length, 2) + }) + await t.test("generation rotates after second SQL: original final filesystem check still denies", async () => { + const f = fixture(await accessGeneration()); f.hook(async index => { if (index === 2) await invalidateMcpAccess() }) + await assert.rejects(validatePrincipal(f.principal, f.client), { code: "UNAUTHORIZED" }); assert.equal(f.commands.length, 2) + }) + await t.test("no grant parsing for base validator, exact payload-only effective source and duplicate/NULL grants refuse", async () => { + const f = fixture(await accessGeneration()), row = f.base(), now = Date.now() + row.__ps_grant_id = randomUUID(); row.__ps_grant_oversize = false + row.__ps_grant_payload = { credentialId: f.principal.credentialId, id: "temporary", startsAt: new Date(now - 1000).toISOString(), expiresAt: new Date(now + 30000).toISOString(), + scope: { serverIds: [1], operationIds: ["servers.poll"], allowGlobal: true } } + row.__ps_grant_sha256 = fingerprint(row.__ps_grant_payload) + f.set([row]); const result = await effectiveMcpConnectionSource(f.principal, f.client) + assert.deepEqual(result.grantRows, [{ payload: row.__ps_grant_payload }]); assert.deepEqual(result.connection.policy.scope.serverIds, [1]) + const x = fixture(await accessGeneration()), nullRow = x.base(); nullRow.__ps_grant_id = randomUUID(); nullRow.__ps_grant_oversize = false; nullRow.__ps_grant_sha256 = fingerprint(null) + x.set([nullRow]); await assert.rejects(effectiveMcpConnectionSource(x.principal, x.client)) + const y = fixture(await accessGeneration()), one = y.base(), two = y.base() + one.__ps_grant_id = randomUUID(); two.__ps_grant_id = randomUUID(); one.__ps_grant_oversize = two.__ps_grant_oversize = false + one.__ps_grant_sha256 = two.__ps_grant_sha256 = fingerprint(null) + y.set([one, two]); await assert.rejects(effectiveMcpConnectionSource(y.principal, y.client), { code: "GRANT_INACTIVE" }) + const z = fixture(await accessGeneration()); await validatePrincipal(z.principal, z.client); assert.deepEqual(z.commands.map(c => c.values[2]), [false, false]) + }) + await t.test("invalid bindings, malformed exact timestamps, source row overflow and absent settings fail closed", async () => { + for (const mutate of [ + (row: ReturnType["base"]>) => { row.__ps_credential = randomUUID() }, + (row: ReturnType["base"]>) => { row.__ps_owner_issued = "2026-02-30T00:00:00.000000Z" }, + ]) { const f = fixture(await accessGeneration()), row = f.base(); mutate(row); f.set([row]); await assert.rejects(f.source(), { code: "STATE_UNAVAILABLE" }) } + const f = fixture(await accessGeneration()); f.set([f.base(), f.base(), f.base()]); await assert.rejects(f.source(), { code: "STATE_UNAVAILABLE" }) + const x = fixture(await accessGeneration()), row = x.base(); row.__ps_settings = null; row.__ps_enabled = null; x.set([row]) + await assert.rejects(validatePrincipal(x.principal, x.client), { code: "UNAUTHORIZED" }) + }) + await t.test("failed or expired borrowed SQL facade never falls back or retries", async () => { + const f = fixture(await accessGeneration()); f.hook(async index => { if (index === 2) throw Error("OWNED_COMPONENT_FACADE_EXPIRED") }) + await assert.rejects(f.source(), /OWNED_COMPONENT_FACADE_EXPIRED/); assert.equal(f.commands.length, 2) + }) + await t.test("external-only cutoff after the file read is caught by next boundary, never cached", async () => { + const f = fixture(await accessGeneration()) + f.hook(async index => { if (index === 2) await cutoff(f.connection.owner_id, { invalidBefore: Date.now() }) }) + await validatePrincipal(f.principal, f.client) + f.hook(undefined) + await assert.rejects(validatePrincipal(f.principal, f.client), { code: "OWNER_CLAIMS_REVOKED" }) + assert.equal(f.commands.length, 3, "This preserves the original post-filesystem race boundary, not a linearizability claim") + }) + } finally { + pool.query = previousQuery; pool.connect = previousConnect + if (previous === undefined) delete process.env.MCP_STATE_DIR + else process.env.MCP_STATE_DIR = previous + await closePool(); await rm(state, { recursive: true, force: true }) + } +}) diff --git a/backend/src/modules/mcp/principal-security-sql.ts b/backend/src/modules/mcp/principal-security-sql.ts new file mode 100644 index 00000000..5be8b574 --- /dev/null +++ b/backend/src/modules/mcp/principal-security-sql.ts @@ -0,0 +1,34 @@ +/** Inert fixed source SQL. Both captures still execute current bound values. */ +import { createHash } from "node:crypto" + +export const principalBytes = 8 * 1024 * 1024, grantBytes = 2 * 1024 * 1024 +export const sourceAliases = ["__ps_exists", "__ps_oversize", "__ps_bytes", "__ps_guard", "__ps_isolation", "__ps_credential", "__ps_credential_current", + "__ps_credential_expires", "__ps_credential_revoked", "__ps_owner_until", "__ps_owner_issued", "__ps_cutoff_owner", "__ps_cutoff", "__ps_cutoff_text", + "__ps_settings", "__ps_enabled", "__ps_grant_id", "__ps_grant_oversize", "__ps_grant_payload", "__ps_policy_sha256", "__ps_grant_sha256"] as const +const utc = (column: string) => `to_char(${column} AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"')` +export const principalSourceSql = `/* mcp-principal:coherent-source-v1 */ WITH candidate AS MATERIALIZED ( + SELECT c AS source_row,octet_length(to_jsonb(c)::text) AS source_bytes FROM mcp_connections c WHERE c.id=$2::uuid +), eligible AS MATERIALIZED (SELECT (source_row).* FROM candidate WHERE source_bytes<=${principalBytes}) +SELECT c.*,flags.source_exists AS __ps_exists,flags.oversize AS __ps_oversize,flags.source_bytes AS __ps_bytes, + NOT(COALESCE(to_jsonb(c) ?| ARRAY[${sourceAliases.map(name => "'" + name + "'").join(",")}],false)) AS __ps_guard, + current_setting('transaction_isolation') AS __ps_isolation,k.id AS __ps_credential, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(c.policy::text,'UTF8')),'hex') AS __ps_policy_sha256, + (k.id IS NOT NULL AND k.revoked_at IS NULL AND k.expires_at>now()) AS __ps_credential_current, + ${utc("k.expires_at")} AS __ps_credential_expires,${utc("k.revoked_at")} AS __ps_credential_revoked, + ${utc("c.owner_valid_until")} AS __ps_owner_until,${utc("c.owner_claims_issued_at")} AS __ps_owner_issued, + r.owner_id AS __ps_cutoff_owner,r.invalid_before AS __ps_cutoff,${utc("r.invalid_before")} AS __ps_cutoff_text, + s.id AS __ps_settings,s.enabled AS __ps_enabled,g.id AS __ps_grant_id,g.oversize AS __ps_grant_oversize,g.payload AS __ps_grant_payload,g.payload_sha256 AS __ps_grant_sha256 +FROM (SELECT EXISTS(SELECT 1 FROM candidate) AS source_exists, + COALESCE((SELECT source_bytes>${principalBytes} FROM candidate),false) AS oversize,(SELECT source_bytes FROM candidate) AS source_bytes) flags +LEFT JOIN eligible c ON true LEFT JOIN mcp_credentials k ON k.id=$1::uuid AND k.connection_id=c.id +LEFT JOIN mcp_owner_revocations r ON r.owner_id=c.owner_id LEFT JOIN mcp_settings s ON s.id=1 +LEFT JOIN LATERAL (WITH raw_grants AS MATERIALIZED ( + SELECT id,payload,octet_length(payload::text) AS payload_bytes FROM mcp_grants + WHERE $3::boolean AND connection_id=c.id AND revoked_at IS NULL AND payload->>'credentialId'=$1::text ORDER BY id LIMIT 2) + SELECT id,payload_bytes>${grantBytes} AS oversize, + CASE WHEN payload_bytes<=${grantBytes} THEN payload ELSE NULL END AS payload, + CASE WHEN payload_bytes<=${grantBytes} THEN pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(payload::text,'UTF8')),'hex') ELSE NULL END AS payload_sha256 + FROM raw_grants ORDER BY id) g ON true` + +/** One bounded protocol statement per physical backend; never a result cache. */ +export const principalSourceStatementName = "mm_ps_v1_" + createHash("sha256").update(principalSourceSql).digest("base64url") diff --git a/backend/src/modules/mcp/probe-actions.ts b/backend/src/modules/mcp/probe-actions.ts new file mode 100644 index 00000000..dbab56be --- /dev/null +++ b/backend/src/modules/mcp/probe-actions.ts @@ -0,0 +1,70 @@ +import { z } from "zod" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { readSpeedHistoryPage } from "../../services/speed-history-read.js" +import { readProbeInterfacesForServer } from "../../services/probe-interface-read.js" +import { readPingProbeConfiguration } from "../../services/ping-probe-configuration.js" +import { updateProbeShowOnDashboard } from "../../services/uptime-collector.js" +import { registerMcpService } from "./service-registry.js" +import { McpError } from "./security.js" +import { fingerprint } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import { hasPermission } from "../../lib/permissions.js" +import { catalogDevices, catalogPaging } from "./catalog-scope.js" +import { projectUptimeOverview, readUptimeOverview } from "../../services/uptime-overview-read.js" +import type { add } from "./actions.js" + +const sid = z.number().int().positive().safe() +const paging = { limit: z.number().int().min(1).max(200).default(100), offset: z.number().int().min(0).max(100000).default(0) } +export function registerProbeActions(register: typeof add) { + register("uptime.overview.read", "mm_uptime_overview_get", "Сохранённая сводка мониторинга разрешённых серверов", "uptime", "mm:uptime:read", "read", "servers", catalogPaging.safeExtend({ range: z.enum(["5m", "15m", "1h", "4h", "24h"]).default("1h"), sampleLimit: z.number().int().min(1).max(200).default(40) }), { + read: async (input, context) => { + const assertAccess = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение мониторинга отменено", 409) + const fresh = await effectiveMcpConnection(context.principal) + if (!hasPermission(fresh.owner_permissions, "mm:uptime:read") || !hasPermission(fresh.policy.permissions, "mm:uptime:read")) throw new McpError("PERMISSION_DENIED", "Право чтения мониторинга отозвано", 403) + if (fingerprint(fresh.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Область мониторинга изменилась", 409) + } + await assertAccess() + const { selected, ...page } = await catalogDevices(input, context, { includeDisabled: true }) + const end = Date.now(), minutes = { "5m": 5, "15m": 15, "1h": 60, "4h": 240, "24h": 1440 } + try { + const view = await readUptimeOverview(selected.map(server => server.id), { startAt: new Date(end - minutes[input.range as keyof typeof minutes] * 60000).toISOString(), endAt: new Date(end).toISOString(), + maxRows: 20000, maxProbes: 1000, probeOffset: Number(input.offset), probeLimit: Number(input.limit) }) + for (const server of selected) if (fingerprint(await getServerRowById(server.id)) !== fingerprint(server)) throw new McpError("STATE_CONFLICT", "Источник мониторинга изменился во время чтения", 409) + await assertAccess() + const projected = projectUptimeOverview(view, Number(input.sampleLimit)) + return { ...page, ...projected, servers: selected.map(server => ({ serverId: server.id, enabled: server.enabled })), atomicSnapshot: false, nextOffset: Number(input.offset) + view.probes.length < view.totalProbes ? Number(input.offset) + view.probes.length : null, + incomplete: page.incomplete || view.probes.length < view.totalProbes || projected.probes.some(row => row.samplesTruncated) } + } catch (error) { await assertAccess(); if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Сводка недоступна или превышает 1000 проб/20000 samples; уменьшите область или диапазон", 409) } + }, + }) + register("uptime.speed.runs.read", "mm_speed_test_runs_get", "История скорости: оба участника в эффективной области доступа", "uptime", "mm:uptime:read", "read", "servers", z.object({ serverId: sid.optional(), ...paging }).strict(), { + read: (input, context) => readSpeedHistoryPage({ + serverIds: context.connection.policy.scope.serverIds, serverId: input.serverId as number | undefined, + limit: Number(input.limit), offset: Number(input.offset), + }), + }) + for (const addresses of [false, true]) { + register(addresses ? "uptime.speed.interfaces.read" : "uptime.ping.interfaces.read", addresses ? "mm_speed_test_interfaces_get" : "mm_ping_source_interfaces_get", addresses ? "Интерфейсы и IPv4 адреса endpoint speed-test" : "Активные интерфейсы источника Ping", "uptime", "mm:uptime:read", "read", "servers", z.object({ serverId: sid, ...paging }).strict(), { + read: async input => { + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const result = await readProbeInterfacesForServer(server, addresses), offset = Number(input.offset), limit = Number(input.limit) + const interfaces = result.interfaces.slice(offset, offset + limit).map(row => ({ ...row, ...(row.addresses ? { addresses: row.addresses.slice(0, 100), addressCandidates: row.addressCandidates?.slice(0, 100), addressesTruncated: row.addresses.length > 100 } : {}) })) + return { ...result, serverId: server.id, interfaces, unknownInterfaces: result.unknownInterfaces.slice(0, 100), unknownInterfacesTruncated: result.unknownInterfaces.length > 100, total: result.interfaces.length, + nextOffset: offset + limit < result.interfaces.length ? offset + limit : null, incomplete: result.incomplete || result.unknownInterfaces.length > 100 || interfaces.length < result.interfaces.length || interfaces.some(row => "addressesTruncated" in row && row.addressesTruncated), observedAt: new Date().toISOString() } + }, + }) + } + const id = "uptime.ping.visibility.update" + const current = (input: Record) => readPingProbeConfiguration(String(input.probeId), Number(input.serverId)) + registerMcpService(id, { current, execute: input => updateProbeShowOnDashboard(String(input.probeId), Boolean(input.showOnDashboard)), verify: async input => (await current(input))?.showOnDashboard === input.showOnDashboard }) + register(id, "mm_ping_probe_visibility_plan", "План отображения Ping-пробы на dashboard", "uptime", "mm:uptime:write", "write", "servers", z.object({ serverId: sid, probeId: z.string().trim().min(1).max(200), showOnDashboard: z.boolean() }).strict(), { + prepare: async input => { + const before = await current(input) + if (!before) throw new McpError("NOT_FOUND", "Ping-проба этого сервера не найдена", 404) + return { target: { scope: "servers", serverIds: [before.srcServerId] }, objects: [{ kind: "server", id: String(before.srcServerId), serverId: before.srcServerId }, { kind: "ping-probe", id: before.id, serverId: before.srcServerId }], snapshots: [], before, + steps: [{ kind: "service", service: id, input }], verificationScope: "configuration", requiresHuman: true, warnings: ["Меняется только отображение пробы. Сетевые проверки и расписание не запускаются."] } + }, + }) +} diff --git a/backend/src/modules/mcp/protection-requests.ts b/backend/src/modules/mcp/protection-requests.ts new file mode 100644 index 00000000..f624dd60 --- /dev/null +++ b/backend/src/modules/mcp/protection-requests.ts @@ -0,0 +1,13 @@ +import { z } from "zod" +import { mcpProtectedObjectSchema } from "@mmapp/contracts/mcp" + +/** Validate new registrations only: legacy rows must remain readable. RouterOS + * native IDs are local to one device, including when their kind is unfamiliar. */ +export const mcpProtectionCreateRequestSchema = z.object({ + object: mcpProtectedObjectSchema.superRefine((object, context) => { + if (/^\*[a-z0-9]+$/i.test(object.id) && object.serverId === undefined) { + context.addIssue({ code: "custom", path: ["serverId"], message: "Для native ID RouterOS укажите serverId" }) + } + }), + reason: z.string().max(1000).default(""), +}).strict() diff --git a/backend/src/modules/mcp/protection-scope-acceptance.ts b/backend/src/modules/mcp/protection-scope-acceptance.ts new file mode 100644 index 00000000..34d5cdc6 --- /dev/null +++ b/backend/src/modules/mcp/protection-scope-acceptance.ts @@ -0,0 +1,54 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import Fastify from "fastify" +import { mcpProtectedObjectSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import authPlugin from "../../plugins/auth.js" +import { createMcpRoutes } from "../../routes/mcp.js" + +/** Owned disposable SQL only. Exercises the actual JWT administration route; + * no RouterOS requests, downloads, alert runners or secret payloads. */ +export async function acceptProtectionRegistrationScope(actor: AuthUser) { + const app = Fastify(), ids: string[] = [], marker = randomUUID() + await app.register(authPlugin) + await app.register(createMcpRoutes(), { prefix: "/api" }) + const token = app.jwt.sign({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }, { iss: (await import("../../config.js")).env.authIssuer, expiresIn: "1h" }) + const headers = { authorization: "Bearer " + token } + const counts = async () => ({ + protections: (await pool.query("SELECT count(*)::int AS n FROM mcp_protections WHERE created_by=$1", [actor.id])).rows[0].n, + audit: (await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE actor_id=$1 AND action='protection.create'", [actor.id])).rows[0].n, + }) + try { + const before = await counts() + for (const kind of ["wireguard_peer", "ethernet", "future-native-kind"]) { + const denied = await app.inject({ method: "POST", url: "/api/mcp/protections", headers, payload: { object: { kind, id: "*3", protection: "deny" }, reason: marker } }) + assert.equal(denied.statusCode, 400, denied.body) + assert.equal(denied.json().code, "INVALID_INPUT") + } + assert.deepEqual(await counts(), before, "Invalid native scope creates neither a protection nor a creation audit") + for (const object of [ + { kind: "wireguard_peer", id: "*3", serverId: 141, protection: "deny" }, + { kind: "wireguard_peer", id: "*3", serverId: 142, protection: "deny" }, + { kind: "server", id: "141", protection: "deny" }, + { kind: "ping-probe", id: marker, protection: "human-approval-required" }, + { kind: "user", id: "scope-test-" + marker, protection: "deny" }, + ]) { + const response = await app.inject({ method: "POST", url: "/api/mcp/protections", headers, payload: { object, reason: marker } }) + assert.equal(response.statusCode, 200, response.body) + const id = response.json().id as string; ids.push(id) + assert.deepEqual((await pool.query("SELECT object_ref FROM mcp_protections WHERE id=$1", [id])).rows[0].object_ref, object) + } + const after = await counts() + assert.equal(after.protections, before.protections + 5); assert.equal(after.audit, before.audit + 5) + const scoped = (await pool.query("SELECT object_ref FROM mcp_protections WHERE id=ANY($1::uuid[])", [ids.slice(0, 2)])).rows.map(row => row.object_ref.serverId).sort() + assert.deepEqual(scoped, [141, 142], "Equal native IDs retain distinct device scopes") + // Do not change the shared persisted-row schema: old ambiguous records + // should not prevent unrelated plans from reading their protections. + assert.equal(mcpProtectedObjectSchema.safeParse({ kind: "wireguard_peer", id: "*3", protection: "deny" }).success, true) + console.log("MCP protection registration HTTP: unscoped native IDs rejected without INSERT/audit; scoped duplicate IDs and global app/probe/server references preserved; legacy read schema unchanged") + } finally { + await app.close() + await pool.query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [ids]) + } +} diff --git a/backend/src/modules/mcp/protocol-limits.owned.test.ts b/backend/src/modules/mcp/protocol-limits.owned.test.ts new file mode 100644 index 00000000..e99404cf --- /dev/null +++ b/backend/src/modules/mcp/protocol-limits.owned.test.ts @@ -0,0 +1,184 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" + +assert.equal(process.env.MMAPP_OWN_PROTOCOL_LIMITS, "true") +assert.equal(process.env.NODE_ENV, "test") +const endpoint = new URL(process.env.DATABASE_URL ?? "") +assert.equal(endpoint.protocol, "postgresql:") +assert.equal(endpoint.hostname, "127.0.0.1") +assert.equal(endpoint.username, "mcp_test") +assert.equal(endpoint.pathname, "/postgres") +assert(endpoint.port && endpoint.password && !endpoint.search && !endpoint.hash) +const state = process.env.MCP_STATE_DIR ?? "" +assert.equal(basename(state), "state") +assert.match(basename(dirname(state)), /^mm-mcp-test-/) + +const { pool, closePool } = await import("../../db/index.js") +const { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK, withOperationLock, withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js") +const { applySqlMigrations, applyStandaloneSqlMigrations } = await import("../../db/migrate.js") +const { PARTITION_WRITER_ADVISORY_KEY } = await import("../../services/partition-write-transaction.js") +const { authenticateMcpToken, createMcpConnection, updateMcpSettings } = await import("./store.js") +const { recordUnauthorizedProtocol, reserveProtocolEnvelope } = await import("./protocol-limits.js") +const { canonicalMcpUrl, encryptSecret, tokenDigest } = await import("./security.js") +const { buildApp } = await import("../../index.js") +const { sharedDatabaseGate } = await import("../../services/shared-database-gate.js") +const { invokeMcpTool } = await import("./runtime.js") + +const app = await buildApp({ startScheduler: false }) +try { + await withOperationLock(() => pool.connect(), SYSTEM_DATABASE_OPERATION_LOCK, async () => { + await assert.rejects(applyStandaloneSqlMigrations(pool), /Операция с базой приложения уже выполняется/) + const { rows: [before] } = await pool.query("SELECT to_regclass('public.schema_migrations') AS marker") + assert.equal(before.marker, null) + await applySqlMigrations(pool) + const { rows: [after] } = await pool.query("SELECT count(*)::int AS applied FROM schema_migrations") + assert(after.applied > 0) + }) + await applyStandaloneSqlMigrations(pool) + const actor = { id: "protocol-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:servers:write"], + email: "owned@example.invalid", name: "Owned protocol", expiresAt: Date.now() + 600_000 } + await updateMcpSettings({ enabled: true }, actor) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned protocol", mode: "approval", + permissions: ["mm:servers:write"], expiresAt: new Date(actor.expiresAt).toISOString(), + scope: { serverIds: [], operationIds: ["servers.poll", "operations.read"], allowGlobal: true }, limits: { callsPerMinute: 2 } }), actor) + const principal = await authenticateMcpToken(access.token) + const clientId = randomUUID(), family = randomUUID(), refresh = randomUUID(), resource = canonicalMcpUrl().href + await pool.query("INSERT INTO mcp_oauth_clients(id,name,redirect_uris) VALUES($1,'Owned OAuth fixture','[]')", [clientId]) + await pool.query(`INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at,family_id,client_id,audience) + VALUES($1,$2,$3,'refresh',now()+interval '5 minutes',$4,$5,$6)`, [randomUUID(), principal.connectionId, tokenDigest(refresh), family, clientId, resource]) + const exchange = () => app.inject({ method: "POST", url: "/oauth/token", + payload: { grant_type: "refresh_token", client_id: clientId, resource, refresh_token: refresh } }) + const revoke = (token: string) => app.inject({ method: "POST", url: "/oauth/revoke", payload: { client_id: clientId, token } }) + const readFamily = async () => (await pool.query("SELECT kind,revoked_at FROM mcp_credentials WHERE family_id=$1 ORDER BY id", [family])).rows + const beforeExchange = await readFamily() + await withSystemDatabaseOperationLock(() => pool.connect(), async () => { + for (const response of [await exchange(), await revoke(refresh)]) { + assert.equal(response.statusCode, 503) + assert.deepEqual(response.json(), { error: "temporarily_unavailable" }) + assert.equal(response.headers["cache-control"], "no-store") + } + assert.deepEqual(await readFamily(), beforeExchange) + }) + const rotated = await exchange() + assert.equal(rotated.statusCode, 200) + assert.equal((await readFamily()).length, 3) + assert.equal((await revoke(rotated.json().refresh_token)).statusCode, 200) + assert((await readFamily()).every(row => row.revoked_at !== null)) + const planId = randomUUID(), operationId = randomUUID() + const evidence = { sealed: await encryptSecret(JSON.stringify({ target: { scope: "global" }, objects: [] })) } + await pool.query(`INSERT INTO mcp_plans + (id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) + VALUES($1,$2,'servers.poll',1,1,'test','{}'::jsonb,$3,'approved',now()+interval '5 minutes')`, + [planId, principal.connectionId, evidence]) + await pool.query(`INSERT INTO mcp_operations (id,plan_id,connection_id,idempotency_key,state) + VALUES($1,$2,$3,$1::uuid::text,'running')`, [operationId, planId, principal.connectionId]) + const address = await app.listen({ host: "127.0.0.1", port: 0 }) + const stateSql = `SELECT (SELECT count(*)::int FROM mcp_budget_buckets) AS budgets, + (SELECT count(*)::int FROM mcp_unauthorized_buckets) AS unauthorized, + (SELECT count(*)::int FROM mcp_requests) AS requests` + const baseline = (await pool.query(stateSql)).rows[0] + const lock = await pool.connect() + try { + await lock.query("SELECT pg_advisory_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + const request = (token = access.token) => fetch(new URL("/mcp", address), { + method: "POST", + headers: { + authorization: `Bearer ${token}`, "content-type": "application/json", + accept: "application/json, text/event-stream", "mcp-protocol-version": "2025-06-18", + }, + signal: AbortSignal.timeout(5000), + body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", + params: { name: "mm_operation_get", arguments: { operationId } } }), + }) + const blocked = await sharedDatabaseGate.exclusive(() => request()) + assert.equal(blocked.status, 409) + assert.deepEqual((await pool.query(stateSql)).rows[0], baseline) + await withOperationLock(() => pool.connect(), SYSTEM_DATABASE_OPERATION_LOCK, async () => { + for (const token of [access.token, "not-a-valid-token"]) { + const response = await request(token) + assert.equal(response.status, 503) + assert.equal((await response.json()).code, "PROTOCOL_STORAGE_BUSY") + } + await assert.rejects(invokeMcpTool(principal, "mm_operation_get", { operationId }), { code: "PROTOCOL_STORAGE_BUSY" }) + assert.deepEqual((await pool.query(stateSql)).rows[0], baseline) + }) + const blocker = await pool.connect() + let pending: ReturnType | undefined + let maintenance: Promise | undefined + const waitForLease = async (mode: string, granted: boolean) => { + for (let attempt = 0; attempt < 100; attempt++) { + const { rowCount } = await pool.query( + "SELECT 1 FROM pg_locks WHERE locktype='advisory' AND classid=0 AND objid=$1 AND mode=$2 AND granted=$3", + [SYSTEM_DATABASE_REQUEST_LOCK, mode, granted]) + if (rowCount) return + await delay(15) + } + assert.fail("Expected request lifecycle lease") + } + let response: Response + try { + await blocker.query("BEGIN") + await blocker.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [principal.connectionId]) + pending = request() + void pending.catch(() => {}) + await waitForLease("ShareLock", true) + maintenance = withSystemDatabaseOperationLock(() => pool.connect(), async () => { + const { rows } = await pool.query( + "SELECT status,finished_at IS NOT NULL AS finished FROM mcp_requests WHERE connection_id=$1 AND tool IN ('mm_operation_get','protocol:tools/call')", + [principal.connectionId]) + assert.deepEqual(rows, [{ status: "completed", finished: true }, { status: "completed", finished: true }]) + }) + void maintenance.catch(() => {}) + await waitForLease("ExclusiveLock", false) + const refused = await request() + assert.equal(refused.status, 503) + assert.equal((await refused.json()).code, "PROTOCOL_STORAGE_BUSY") + await blocker.query("COMMIT") + response = await pending + await maintenance + } finally { + blocker.release(true) + await Promise.allSettled([pending, maintenance]) + } + assert.equal(response.status, 200) + const body = await response.text() + const message = JSON.parse(body.split(/\r?\n/).find(line => line.startsWith("data: "))?.slice(6) ?? body) + assert.equal(message.result?.isError ?? false, false, body) + const result = message.result?.structuredContent?.result + assert.equal(result?.id, operationId, body) + assert.equal(result?.plan_id, planId) + assert.equal(result?.state, "running") + assert.deepEqual((await pool.query( + "SELECT tool FROM mcp_requests WHERE connection_id=$1 AND tool IN ('mm_operation_get','protocol:tools/call') ORDER BY tool", + [principal.connectionId], + )).rows, [{ tool: "mm_operation_get" }, { tool: "protocol:tools/call" }]) + assert.equal((await pool.query("SELECT used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='protocol'", [principal.connectionId])).rows[0]?.used, 1) + await pool.query("DELETE FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='protocol'", [principal.connectionId]) + } finally { + await lock.query("SELECT pg_advisory_unlock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + lock.release() + } + // Leave room for the concurrency assertions before the next minute. + const minute = (await pool.query("SELECT date_trunc('minute',now()) = date_trunc('minute',now()+interval '5 seconds') AS safe")).rows[0].safe + if (!minute) await delay(5000) + const envelopes = await Promise.all(Array.from({ length: 16 }, () => reserveProtocolEnvelope(principal))) + assert.equal(envelopes.filter(envelope => envelope.allowed).length, 10) + const limited = await app.inject({ method: "POST", url: "/mcp", + headers: { authorization: `Bearer ${access.token}` }, payload: {} }) + assert.equal(limited.statusCode, 429) + assert.equal(limited.json().code, "RATE_LIMITED") + assert.equal(limited.headers["retry-after"], String(limited.json().retryAfterSec)) + for (let attempt = 0; attempt < 8; attempt++) await recordUnauthorizedProtocol("192.0.2.99", "UNAUTHORIZED") + const { rows: [counts] } = await pool.query(`SELECT + (SELECT count(*)::int FROM mcp_requests WHERE tool='protocol:rate-limit') AS denied, + (SELECT count(*)::int FROM mcp_requests WHERE tool='protocol:unauthorized') AS unauthorized, + (SELECT max(attempts)::int FROM mcp_unauthorized_buckets) AS attempts`) + assert.deepEqual(counts, { denied: 1, unauthorized: 1, attempts: 8 }) + console.log("MCP request accounting respects the backup/restore lock independently of the partition lease") +} finally { + await app.close() + await closePool() +} diff --git a/backend/src/modules/mcp/protocol-limits.ts b/backend/src/modules/mcp/protocol-limits.ts new file mode 100644 index 00000000..3a9e9d81 --- /dev/null +++ b/backend/src/modules/mcp/protocol-limits.ts @@ -0,0 +1,27 @@ +import { randomUUID } from "node:crypto" +import { requestAccountingWrite, validatePrincipal, type McpPrincipal } from "./store.js" +import { accessGeneration, tokenDigest } from "./security.js" + +export async function reserveProtocolEnvelope(principal: McpPrincipal) { + return requestAccountingWrite(async client => { + // Status reads also reserve protocol quota while POLL holds the partition lease. + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [principal.connectionId]) + const connection = await validatePrincipal(principal, client) + // Protocol negotiation and metadata have their own cap; tools retain their + // stricter policy budget. A single metadata endpoint cannot bypass limits. + const limit = Math.max(10, Math.min(3000, connection.policy.limits.callsPerMinute * 3)) + const { rows } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+1 WHERE mcp_budget_buckets.used<$2 RETURNING used", [connection.id, limit]) + if (!rows.length) { + const { rows: [denied] } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol-denied',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used+1,2147483647) RETURNING used", [connection.id]) + if (denied.used === 1) await client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,status,input,error_code) VALUES($1,$2,$3,'protocol:rate-limit','denied','{}','RATE_LIMITED')", [randomUUID(), connection.id, connection.owner_id]) + } + return { allowed: !!rows.length, limit, retryAfterSec: 60 - new Date().getUTCSeconds() } + }) +} +export async function recordUnauthorizedProtocol(ip: string, reason: string) { + const clientHash = tokenDigest(ip + ":" + await accessGeneration()) + await requestAccountingWrite(async client => { + const { rows: [bucket] } = await client.query("INSERT INTO mcp_unauthorized_buckets(bucket,client_hash,reason) VALUES(date_trunc('minute',now()),$1,$2) ON CONFLICT(bucket,client_hash,reason) DO UPDATE SET attempts=LEAST(mcp_unauthorized_buckets.attempts+1,2147483647) RETURNING attempts", [clientHash, reason]) + if (bucket.attempts === 1) await client.query("INSERT INTO mcp_requests(id,tool,status,input,error_code) VALUES($1,'protocol:unauthorized','denied',$2::jsonb,$3)", [randomUUID(), JSON.stringify({ clientHash, aggregation: "minute", counterSource: "mcp_unauthorized_buckets" }), reason]) + }) +} diff --git a/backend/src/modules/mcp/readiness-http.test.ts b/backend/src/modules/mcp/readiness-http.test.ts new file mode 100644 index 00000000..0292a8fa --- /dev/null +++ b/backend/src/modules/mcp/readiness-http.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createServer } from "node:http" +import { runMcpProtocolProbe } from "./readiness.js" + +test("failed/malformed/redirected and stalled protocol probes are bounded and never expose credentials", async () => { + let mode = "malformed", forwarded = 0, requests = 0 + const server = createServer((req, res) => { + requests++; req.resume() + if (req.url === "/target") { forwarded++; res.end("{}"); return } + assert.equal(req.headers.authorization, "Bearer private-readiness-fixture") + if (mode === "redirect") { res.writeHead(302, { Location: "/target" }); res.end(); return } + res.setHeader("content-type", "application/json") + if (mode === "stalled") { res.write("{"); return } + if (mode === "large") { res.end(JSON.stringify({ padding: "x".repeat(1024 * 1024 + 1) })); return } + if (mode === "announced") { res.writeHead(200, { "content-length": "2097152" }); res.write("{"); return } + res.end("private-readiness-fixture") + }) + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const endpoint = new URL(`http://127.0.0.1:${(server.address() as { port: number }).port}/mcp`) + try { + for (const state of ["malformed", "redirect", "large", "announced", "stalled"]) { + mode = state + const started = performance.now(), result = await runMcpProtocolProbe(endpoint, "private-readiness-fixture", { timeoutMs: 300 }) + assert.equal(result.status, "failed", state) + assert.ok(performance.now() - started < 3000, state) + assert.ok(!JSON.stringify(result).includes("private-readiness-fixture")) + } + assert.equal(forwarded, 0); assert.ok(requests >= 5) + } finally { server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) } +}) + +test("capabilities readiness refuses widened scope and checks the tool result", async () => { + const capability = { id: "capabilities.read", toolName: "mm_capabilities_get" } + const tool = { name: capability.toolName, inputSchema: { type: "object" } } + let mode = "extra-tool", calls = 0 + const server = createServer(async (req, res) => { + if (req.method !== "POST") { res.writeHead(405).end(); return } + const chunks: Buffer[] = [] + for await (const chunk of req) chunks.push(chunk) + const { id, method, params } = JSON.parse(Buffer.concat(chunks).toString()) + if (id === undefined) { res.writeHead(202).end(); return } + let result: unknown + if (method === "initialize") result = { protocolVersion: params.protocolVersion, capabilities: { tools: {} }, serverInfo: { name: "fixture", version: "1.0.0" } } + if (method === "tools/list") result = { tools: mode === "extra-tool" ? [tool, { ...tool, name: "mm_servers_list" }] : [tool] } + if (method === "tools/call") { + calls++ + assert.equal(params.name, capability.toolName) + if (mode === "timeout") { res.writeHead(200, { "content-type": "application/json" }).write("{"); return } + const operations = mode === "extra-operation" ? [capability, { id: "servers.list" }] : [capability] + result = { content: [], isError: mode === "tool-error", structuredContent: { result: { schemaVersion: 1, operations } } } + } + res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ jsonrpc: "2.0", id, result })) + }) + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const endpoint = new URL(`http://127.0.0.1:${(server.address() as { port: number }).port}/mcp`) + const probe = () => runMcpProtocolProbe(endpoint, "private-readiness-fixture", { timeoutMs: 400, readCapabilities: true }) + try { + const widened = await probe() + assert.equal(widened.reason, "SCOPE_NOT_ENFORCED") + assert.equal(widened.capabilitiesRead, "skipped") + assert.equal(calls, 0) + for (const state of ["tool-error", "extra-operation", "timeout"]) { + mode = state + const result = await probe() + assert.equal(result.status, "failed", state) + assert.equal(result.capabilitiesRead, "failed", state) + assert.equal(result.reason, state === "timeout" ? "TIMEOUT" : "CAPABILITIES_READ_FAILED", state) + } + mode = "valid" + const valid = await probe() + assert.equal(valid.status, "passed") + assert.equal(valid.capabilitiesRead, "passed") + assert.equal(valid.clientCredentialsVerified, false) + assert.equal(calls, 4) + } finally { server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) } +}) diff --git a/backend/src/modules/mcp/readiness.ts b/backend/src/modules/mcp/readiness.ts new file mode 100644 index 00000000..e8949c46 --- /dev/null +++ b/backend/src/modules/mcp/readiness.ts @@ -0,0 +1,97 @@ +import { randomUUID } from "node:crypto" +import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client" +import { mcpTemporaryGrantSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../../lib/permissions.js" +import { audit, assertConnectionVisible, connectionWrite, getConnection, validatePrincipal } from "./store.js" +import { canonicalMcpUrl, McpError, randomToken, tokenDigest } from "./security.js" + +export interface ProtocolProbe { + status: "passed" | "failed" | "skipped"; + elapsedMs: number; + reason?: string; + credentialType: "temporary-no-operations" | "temporary-capabilities-read"; + capabilitiesRead: "passed" | "failed" | "skipped"; + clientCredentialsVerified: false; +} + +const capabilitiesOperation = "capabilities.read" +const capabilitiesTool = "mm_capabilities_get" + +/** The probe may read its own narrowed capabilities when the connection grants + * that operation. The ordinary connection credentials are never recovered. */ +export async function withMcpProbeCredential(connectionId: string, actor: AuthUser, work: (token: string, readCapabilities: boolean) => Promise): Promise { + const credentialId = randomUUID(), grantId = randomUUID(), token = randomToken("access") + const readCapabilities = await connectionWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [connectionId]) + const connection = await getConnection(connectionId, client) + assertConnectionVisible(connection, actor) + const recent = await client.query("SELECT 1 FROM mcp_audit WHERE connection_id=$1 AND action='connection.handshake.start' AND created_at>now()-interval '30 seconds' LIMIT 1", [connectionId]) + if (recent.rows.length) throw new McpError("RATE_LIMITED", "Повторная проверка транспорта доступна через 30 секунд", 429) + // Recover expired leftovers from a crashed probe, only for this connection. + const expired = (await client.query("DELETE FROM mcp_credentials WHERE connection_id=$1 AND client_id='mcp-readiness-probe' AND expires_at<=now() RETURNING id", [connectionId])).rows.map(row => row.id) + if (expired.length) await client.query("DELETE FROM mcp_grants WHERE connection_id=$1 AND payload->>'credentialId'=ANY($2::text[])", [connectionId, expired]) + const expiresAt = new Date(Math.min(Date.now() + 20000, Date.parse(connection.policy.expiresAt), connection.owner_valid_until.getTime())).toISOString() + const allowed = connection.policy.scope.operationIds.includes(capabilitiesOperation) + const grant = mcpTemporaryGrantSchema.parse({ id: grantId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt, scope: { serverIds: [], operationIds: allowed ? [capabilitiesOperation] : [], allowGlobal: false } }) + await client.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at,audience,client_id) VALUES($1,$2,$3,'access',$4,$5,'mcp-readiness-probe')", [credentialId, connectionId, tokenDigest(token), expiresAt, canonicalMcpUrl().href]) + await client.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grantId, connectionId, JSON.stringify({ ...grant, credentialId })]) + await validatePrincipal({ connectionId, credentialId, credentialExpiresAt: expiresAt }, client) + await audit(client, actor.id, "connection.handshake.start", connectionId, { credentialId, expiresAt }) + return allowed + }) + let completed = false + try { const result = await work(token, readCapabilities); completed = true; return result } + finally { + await connectionWrite(async client => { + // Use the same connection → credential → grant lock order as revocation. + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [connectionId]) + await client.query("DELETE FROM mcp_credentials WHERE id=$1 AND connection_id=$2", [credentialId, connectionId]) + await client.query("DELETE FROM mcp_grants WHERE id=$1 AND connection_id=$2", [grantId, connectionId]) + await audit(client, actor.id, "connection.handshake.end", connectionId, { credentialId, completed }) + }) + } +} + +/** One deadline covers negotiation, metadata and HTTP response bodies. */ +export async function runMcpProtocolProbe(endpoint: URL, token: string, { timeoutMs = 8000, readCapabilities = false } = {}): Promise { + const started = performance.now(), controller = new AbortController() + const timer = setTimeout(() => controller.abort(), timeoutMs) + const client = new Client({ name: "MikrotikManager readiness", version: "1.0.0" }) + let capabilitiesRead: ProtocolProbe["capabilitiesRead"] = "skipped" + const result = (status: ProtocolProbe["status"], reason?: string): ProtocolProbe => ({ status, reason, elapsedMs: Math.round(performance.now() - started), credentialType: readCapabilities ? "temporary-capabilities-read" : "temporary-no-operations", capabilitiesRead, clientCredentialsVerified: false }) + try { + const transport = new StreamableHTTPClientTransport(endpoint, { requestInit: { headers: { Authorization: `Bearer ${token}` }, redirect: "error" }, redirectPolicy: "follow", + fetch: async (url, init) => { + const signal = init?.signal ? AbortSignal.any([init.signal, controller.signal]) : controller.signal + const response = await fetch(url, { ...init, redirect: "error", signal }) + if (!response.body) return response + if (Number(response.headers.get("content-length")) > 1024 * 1024) { await response.body.cancel(); throw new Error("Probe response limit") } + let bytes = 0 + const body = response.body.pipeThrough(new TransformStream({ transform(chunk, output) { + bytes += chunk.byteLength + if (bytes > 1024 * 1024) { output.error(new Error("Probe response limit")); return } + output.enqueue(chunk) + } }), { signal }) + return new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers }) + } }) + await client.connect(transport, { timeout: timeoutMs }) + const tools = await client.listTools({}, { timeout: timeoutMs }) + const expectedTools = readCapabilities ? [capabilitiesTool] : [] + if (tools.nextCursor || tools.tools.length !== expectedTools.length || tools.tools.some((tool, index) => tool.name !== expectedTools[index])) return result("failed", "SCOPE_NOT_ENFORCED") + if (readCapabilities) { + capabilitiesRead = "failed" + const call = await client.callTool({ name: capabilitiesTool, arguments: {} }, { timeout: timeoutMs }) + const output = (call.structuredContent as { result?: { schemaVersion?: unknown; operations?: unknown } } | undefined)?.result + const operations = output?.operations + if (call.isError || output?.schemaVersion !== 1 || !Array.isArray(operations) || operations.length !== 1 || operations[0]?.id !== capabilitiesOperation || operations[0]?.toolName !== capabilitiesTool) return result("failed", "CAPABILITIES_READ_FAILED") + capabilitiesRead = "passed" + } + return result("passed") + } catch { return result("failed", controller.signal.aborted ? "TIMEOUT" : capabilitiesRead === "failed" ? "CAPABILITIES_READ_FAILED" : "TRANSPORT_FAILED") } + finally { clearTimeout(timer); controller.abort(); await client.close().catch(() => {}) } +} + +export async function checkMcpProtocol(connectionId: string, actor: AuthUser) { + const endpoint = canonicalMcpUrl() + return withMcpProbeCredential(connectionId, actor, (token, readCapabilities) => runMcpProtocolProbe(endpoint, token, { readCapabilities })) +} diff --git a/backend/src/modules/mcp/recovery-actions.ts b/backend/src/modules/mcp/recovery-actions.ts new file mode 100644 index 00000000..60b7cafe --- /dev/null +++ b/backend/src/modules/mcp/recovery-actions.ts @@ -0,0 +1,186 @@ +import { assertNoCallerCreateId, recoveryRosStepId } from "./native-create-identity.js" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { decryptSecret, fingerprint, McpError } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import { findMcpService } from "./service-registry.js" +import { findMcpOperation } from "./registry.js" +import { readRosSnapshot, resourceById, routerClient, validateRosStep, splitOspfUnsetUpdate, rosSnapshotVersion, type RosSnapshot, type RosStep } from "./resources.js" +import { verifyRosStepSnapshots } from "./ros-step-verification.js" +import type { add } from "./actions.js" +import type { ActionContext, ChangeEvidence, ChangeStep } from "./action-types.js" +import { loadScopedRecoverySource } from "./routing-community-scoped-recovery-source.js" +import { prepareScopedRecovery, registerScopedRecoveryService } from "./routing-community-scoped-recovery.js" +import { reconcileVrfProductionStep } from "../../services/vrf-production-execution.js" +import { needsBridgeVlanRecovery, prepareBridgeVlanRecovery } from "./bridge-vlan-recovery.js" +import { readRpfDependencies } from "./routing-controls.js" +import { verifyHealthChecks } from "./routing-health-checks.js" + +async function source(operationId: string, context: ActionContext, allowServices = false) { + const { rows: [row] } = await pool.query("SELECT o.*,p.operation_id AS source_action,p.operation_version AS source_version,p.evidence FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.connection_id=$2", [operationId, context.connection.id]) + if (!row) throw new McpError("NOT_FOUND", "Операция не найдена", 404) + const action = findMcpOperation(row.source_action) + if (!action || !context.connection.policy.scope.operationIds.includes(action.id) || action.requiredPermissions.some(permission => !hasPermission(context.connection.owner_permissions, permission) || !hasPermission(context.connection.policy.permissions, permission))) throw new McpError("PERMISSION_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", [context.connection.id, context.principal.credentialId]) + if (grants.some(grant => !grant.payload.scope?.operationIds?.includes(action.id))) throw new McpError("PERMISSION_DENIED", "Временный допуск не разрешает исходное действие") + const evidence = JSON.parse(await decryptSecret(row.evidence.sealed)) as ChangeEvidence + if (!allowServices && evidence.steps.some(step => step.kind !== "ros")) throw new McpError("UNSUPPORTED_CAPABILITY", "Сервис восстанавливается через собственную ревизию; универсальный откат недоступен", 409) + return { row, evidence } +} +export function registerRecoveryActions(register: typeof add) { + registerScopedRecoveryService() + register("operations.reconcile", "mm_operation_reconcile", "Сверить результат сетевой операции без повторной записи", "mcp", "mm:network:read", "read", "servers", z.object({ operationId: z.string().uuid() }).strict(), { + target: async (input, context) => { + const { evidence } = await source(String(input.operationId), context, true) + if (evidence.target.scope !== "servers" || evidence.steps.some(step => step.kind === "service" && !findMcpService(step.service)?.reconcile)) throw new McpError("UNSUPPORTED_CAPABILITY", "Для сервиса нет безопасной read-only сверки", 409) + return evidence.target + }, + read: async (input, context) => { + const { row, evidence } = await source(String(input.operationId), context, true) + const authorize = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Сверка отменена", 409) + const current = await effectiveMcpConnection(context.principal) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) throw new McpError("RESET_REQUIRED", "Допуск сверки изменился", 409) + } + const results = [] + const sourceChecks: (() => Promise)[] = [] + for (const [index, step] of evidence.steps.entries()) { + if (step.kind === "service") { + const serverId = Number(step.input.serverId) + if (!Number.isSafeInteger(serverId) || !context.connection.policy.scope.serverIds.includes(serverId) || evidence.target.scope !== "servers" || !evidence.target.serverIds.includes(serverId)) throw new McpError("SERVER_DENIED", "Сервис вне допуска сверки") + if (["pending", "running"].includes(row.state)) throw new McpError("STATE_CONFLICT", "Операция ещё выполняется; повтор записи запрещён", 409) + const handler = findMcpService(step.service) + if (!handler?.reconcile) throw new McpError("UNSUPPORTED_CAPABILITY", "Для сервиса нет безопасной read-only сверки", 409) + await authorize() + results.push({ stepIndex: index, serverId, service: step.service, ...await handler.reconcile(step.input, authorize) }) + continue + } + if (!context.connection.policy.scope.serverIds.includes(step.step.serverId)) throw new McpError("SERVER_DENIED", "Сервер вне допуска") + await authorize() + const sourceProof = fingerprint((await routerClient(step.step.serverId)).server) + const beforeRead = async () => { + await authorize() + if (fingerprint((await routerClient(step.step.serverId)).server) !== sourceProof) throw new McpError("STATE_CONFLICT", "Источник сверки изменился во время чтения", 409) + } + sourceChecks.push(beforeRead) + const checkpoint = (await pool.query("SELECT after_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1 AND step_index=$2", [row.id, index])).rows[0] + if (step.step.resource === "vrf") { + if (["pending", "running"].includes(row.state)) throw new McpError("STATE_CONFLICT", "VRF ещё выполняется; сверка не повторяет запись", 409) + const original = evidence.snapshots.find(snapshot => snapshot.serverId === step.step.serverId && snapshot.resource === "vrf") + if (!original?.sourceProof) throw new McpError("STATE_CONFLICT", "Нет исходного источника VRF для сверки", 409) + const state = (await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=$2", [row.id, index])).rows[0] + const privateValue = checkpoint?.after_sealed ? JSON.parse(await decryptSecret(checkpoint.after_sealed)) as { kind?: string; sealed?: string } : null + const verified = state?.state === "verified" && state.result?.kind === "vrf-production-execution-v1" && privateValue?.kind === "vrf-production-private-checkpoint-v1" + const comparison = await reconcileVrfProductionStep({ step: step.step, ownerId: context.connection.owner_id, sourceProof: original.sourceProof, + oldMembers: original.rows.find(item => item[".id"] === step.step.id)?.interfaces, + ...(verified ? { verifiedNativeId: state.result.nativeId, privateCheckpoint: privateValue?.sealed } : {}), authorize: beforeRead }) + results.push({ stepIndex: index, serverId: step.step.serverId, resource: "vrf", ...comparison, comparisonScope: "whole-native-companion-configuration", sourceUnchangedDuringRead: true, originalSourceIdentityVerified: true, forwardingVerified: false }) + continue + } + const current = await readRosSnapshot(step.step.serverId, step.step.resource, beforeRead) + const expected = checkpoint?.after_sealed ? JSON.parse(await decryptSecret(checkpoint.after_sealed)) as RosSnapshot : null + results.push({ stepIndex: index, serverId: step.step.serverId, resource: step.step.resource, matchesVerifiedCheckpoint: expected ? expected.fingerprint === current.fingerprint : null, checkpointAvailable: !!expected, + comparisonScope: "visible-resource-configuration", sourceUnchangedDuringRead: true, originalSourceIdentityVerified: false }) + } + for (const check of sourceChecks) await check() + await authorize() + return { operationId: row.id, state: row.state, observedAt: new Date().toISOString(), results, automaticRetry: false } + }, + }) + register("operations.recover", "mm_operation_recovery_plan", "Подготовить отдельный обратный план сетевой операции", "network", "mm:network:write", "write", "servers", z.object({ operationId: z.string().uuid(), observation: z.literal("route-effects-v1").optional() }).strict(), { + version: 8, + target: async (input, context) => { + const { evidence } = await source(String(input.operationId), context, true) + if (evidence.steps.some(step => step.kind === "ros" && step.step.resource === "vrf")) throw new McpError("UNSUPPORTED_CAPABILITY", "VRF не имеет проверенной обратной ревизии; используйте read-only сверку и отдельный approved план", 409) + for (const step of evidence.steps) if (step.kind === "ros") assertNoCallerCreateId(step.step) + if (evidence.steps.length === 1 && evidence.steps[0].kind === "service" && evidence.steps[0].service === "routing.community.scoped") return { scope: "servers", serverIds: (await loadScopedRecoverySource(String(input.operationId), context)).ids } + if (evidence.steps.some(step => step.kind !== "ros")) throw new McpError("UNSUPPORTED_CAPABILITY", "У сервиса нет собственного обратного плана", 409) + return { scope: "servers", serverIds: [...new Set(evidence.snapshots.map(snapshot => snapshot.serverId))] } + }, + prepare: async (input, context) => { + const { row, evidence } = await source(String(input.operationId), context, true) + if (evidence.steps.some(step => step.kind === "ros" && step.step.resource === "vrf")) throw new McpError("UNSUPPORTED_CAPABILITY", "VRF не имеет проверенной обратной ревизии; автоматический inverse и повтор записи запрещены", 409) + for (const step of evidence.steps) if (step.kind === "ros") assertNoCallerCreateId(step.step) + if (evidence.steps.length === 1 && evidence.steps[0].kind === "service" && evidence.steps[0].service === "routing.community.scoped") { + return prepareScopedRecovery(String(input.operationId), context, input.observation === "route-effects-v1") + } + if (input.observation !== undefined) throw new McpError("UNSUPPORTED_CAPABILITY", "Наблюдения доступны только для обратного плана Route AI Community", 409) + if (evidence.steps.some(step => step.kind !== "ros")) throw new McpError("UNSUPPORTED_CAPABILITY", "Сервис восстанавливается через собственную ревизию; универсальный откат недоступен", 409) + if (row.source_version !== findMcpOperation(row.source_action)?.version) throw new McpError("PLAN_EXPIRED", "Версия исходного действия изменилась; требуется новая сверка вместо обратного плана", 409) + if (!["applied", "partial"].includes(row.state)) throw new McpError("STATE_CONFLICT", "Неопределённый или выполняющийся результат требует сверки, а не автоматического отката", 409) + const checkpoints = (await pool.query("SELECT step_index,before_sealed,after_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1 ORDER BY step_index DESC", [row.id])).rows + const intended = (await pool.query("SELECT step_index,state,result FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index DESC", [row.id])).rows + if (!intended.length || intended.some(step => step.state !== "verified") || intended.length !== checkpoints.length || checkpoints.some(checkpoint => !checkpoint.after_sealed)) throw new McpError("STATE_CONFLICT", "Нет полного набора подтверждённых снимков; требуется ручная сверка", 409) + if (needsBridgeVlanRecovery(row.source_action, evidence)) return prepareBridgeVlanRecovery({ operationId: row.id, evidence, checkpoints, intended }, context) + const snapshots = new Map(), steps: ChangeStep[] = [], objects: ChangeEvidence["objects"] = [...evidence.objects] + const warnings = [`Обратный план операции ${row.id}; не является повтором исходного действия.`, "Восстановление удалённых объектов и сервисов требует их собственного механизма ревизий.", "Сохраняются инварианты Full OSPF/Up BFD и привязка соседств к устройствам, VRF, адресам и интерфейсам; достижимость восстановленной сетевой конфигурации проверяется контрольными пробами."] + for (const checkpoint of checkpoints) { + const original = evidence.steps[checkpoint.step_index] + if (!original || original.kind !== "ros" || original.step.action === "delete") throw new McpError("UNSUPPORTED_CAPABILITY", "Удалённый объект может иметь новые ID и зависимости; используйте проверенную ревизию", 409) + const step = original.step, resource = resourceById(step.resource) + if (!context.connection.policy.scope.serverIds.includes(step.serverId)) throw new McpError("SERVER_DENIED", "Зависимость вне допуска") + const before = JSON.parse(await decryptSecret(checkpoint.before_sealed)) as RosSnapshot, after = JSON.parse(await decryptSecret(checkpoint.after_sealed)) as RosSnapshot + if (before.projectionVersion !== rosSnapshotVersion(step.resource) || after.projectionVersion !== rosSnapshotVersion(step.resource) || !before.sourceProof || before.sourceProof !== after.sourceProof || before.fingerprint !== fingerprint(before.rows) || after.fingerprint !== fingerprint(after.rows) || !verifyRosStepSnapshots(step, intended.find(item => item.step_index === checkpoint.step_index)?.result, before, after)) throw new McpError("STATE_CONFLICT", "Исходный переход каталога или источник не подтверждён; обратный план запрещён", 409) + const key = `${step.serverId}:${step.resource}` + if (!snapshots.has(key)) { + const current = await readRosSnapshot(step.serverId, step.resource, undefined, after.sourceProof) + if (current.fingerprint !== after.fingerprint || current.sourceProof !== after.sourceProof) throw new McpError("STATE_CONFLICT", "После операции обнаружено внешнее изменение или смена источника; откат запрещён", 409) + snapshots.set(key, current) + } + const id = recoveryRosStepId(step, before.rows, after.rows) + if (step.action === "create" && (!id || !resource.remove)) throw new McpError("UNSUPPORTED_CAPABILITY", "Нет однозначного ID созданного объекта", 409) + const old = step.id ? before.rows.find(row => row[".id"] === step.id) : before.rows[0] + const fields: Record = {} + if (step.action === "update") for (const key of Object.keys(step.fields)) { + const value = old?.[key] === undefined && old && step.resource === "ospf_template" && key === "prefix-list" && !Object.hasOwn(old, key) ? "" : old?.[key] + const schema = resource.fields.shape[key] + if (value === undefined || !schema) throw new McpError("UNSUPPORTED_CAPABILITY", "Исходное значение неизвестно; нельзя восстановить его по предположению", 409) + fields[key] = schema.safeParse(true).success && [true, false, "true", "false", "yes", "no"].includes(value as string | boolean) ? [true, "true", "yes"].includes(value as string | boolean) : schema.safeParse(1).success ? Number(value) : value + } + const action = step.action === "create" ? "delete" : "update" + const inverse: RosStep = { ...step, placeBeforeId: undefined, action, id: typeof id === "string" ? id : undefined, fields: validateRosStep(resource, action, fields, typeof id === "string" ? id : undefined) } + if (step.resource === "ospf_template" && action === "update" && Object.keys(inverse.fields).length === 1 && inverse.fields.cost !== undefined) { + const applied = after.rows.find(row => row[".id"] === id)?.cost + if (typeof applied !== "string" || !/^\d+$/.test(applied) || !Number.isInteger(Number(applied)) || Number(applied) < 1 || Number(applied) > 65535) throw new McpError("STATE_CONFLICT", "Не подтверждена применённая стоимость OSPF для обратного плана", 409) + inverse.expectedCost = Number(applied) + } + steps.push(...splitOspfUnsetUpdate(inverse).map(step => ({ kind: "ros" as const, step }))) + objects.push({ kind: step.resource, id: String(id ?? "singleton"), serverId: step.serverId }) + } + for (const dependency of evidence.snapshots) { + const key = `${dependency.serverId}:${dependency.resource}` + if (snapshots.has(key)) continue + const current = await readRosSnapshot(dependency.serverId, dependency.resource, undefined, dependency.sourceProof) + if (current.fingerprint !== dependency.fingerprint || current.sourceProof !== dependency.sourceProof || dependency.projectionVersion !== rosSnapshotVersion(dependency.resource)) throw new McpError("STATE_CONFLICT", "Зависимость или источник исходного плана изменились", 409) + snapshots.set(key, current) + } + for (const change of steps) { + if (change.kind !== "ros" || change.step.resource !== "ip_settings" || !change.step.fields["rp-filter"]) continue + const settings = snapshots.get(`${change.step.serverId}:ip_settings`) + const rpf = await readRpfDependencies(change.step.serverId, change.step.fields["rp-filter"], settings?.sourceProof) + for (const snapshot of rpf.snapshots) { + const key = `${snapshot.serverId}:${snapshot.resource}`, previous = snapshots.get(key) + if (previous && (previous.fingerprint !== snapshot.fingerprint || previous.sourceProof !== snapshot.sourceProof)) { + throw new McpError("STATE_CONFLICT", "Зависимость RPF изменилась при подготовке восстановления", 409) + } + snapshots.set(key, snapshot) + } + warnings.push(...rpf.warnings) + } + objects.push(...[...new Set([...snapshots.values()].map(snapshot => snapshot.serverId))].map(serverId => ({ kind: "server", id: String(serverId), serverId }))) + const healthChecks = evidence.healthChecks?.filter(check => check.field === "state" || check.field === "bfd-up" || check.field === "resolved") + const currentSnapshots = [...snapshots.values()] + const serverIds = [...new Set(currentSnapshots.map(snapshot => snapshot.serverId))] + const nextHopChecks = healthChecks?.filter(check => check.field === "resolved") + if (nextHopChecks?.length) await verifyHealthChecks({ healthChecks: nextHopChecks, snapshots: currentSnapshots }, serverIds, false, async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Подготовка обратного плана отменена", 409) + const current = await effectiveMcpConnection(context.principal) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) { + throw new McpError("RESET_REQUIRED", "Допуск восстановления изменился", 409) + } + }) + return { target: { scope: "servers", serverIds }, objects, snapshots: currentSnapshots, steps, ospfOwnerGraph: evidence.ospfOwnerGraph, healthChecks, requiresHuman: true, warnings } + }, + }) +} diff --git a/backend/src/modules/mcp/recovery-maintenance-http.test.ts b/backend/src/modules/mcp/recovery-maintenance-http.test.ts new file mode 100644 index 00000000..523d00da --- /dev/null +++ b/backend/src/modules/mcp/recovery-maintenance-http.test.ts @@ -0,0 +1,56 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-recovery-maintenance-acceptance-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const actor = { id: "recovery-maintenance-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:certificates:write", "mm:network:admin", "mm:filters:admin", "mm:servers:admin", "mm:traffic:write"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({ enabled: true }, actor) + const managementAdmissionOnly = process.env.MMAPP_OWN_MANAGEMENT_ADMISSION_ONLY === "true" + const administrationAdmissionOnly = process.env.MMAPP_OWN_ADMINISTRATION_ADMISSION_ONLY === "true" + const configurationAuditOnly = process.env.MMAPP_OWN_CONFIGURATION_AUDIT_ONLY === "true" + const routerRequestDrainOnly = process.env.MMAPP_OWN_ROUTER_REQUEST_DRAIN_ONLY === "true" + const restReplicaOnly = process.env.MMAPP_OWN_REST_REPLICA_ONLY === "true" + const restoreQuarantineOnly = process.env.MMAPP_OWN_RESTORE_QUARANTINE_ONLY === "true" + const logsSdkScenario = process.env.MMAPP_OWN_LOGS_SDK_SCENARIO + const sqliteImportCliOnly = process.env.MMAPP_OWN_SQLITE_IMPORT_CLI_ONLY === "true" + if (restoreQuarantineOnly) await (await import("../../test/database-restore-quarantine-owned.js")).verifyRestoreQuarantineWithArchive() + else if (restReplicaOnly) await (await import("../../test/database-rest-replica.js")).verifyDatabaseRestReplica() + else if (sqliteImportCliOnly) await (await import("../../test/sqlite-import-cli-admission.js")).verifySqliteImportCliAdmission() + else if (logsSdkScenario) { + await pool.query("INSERT INTO servers(id,name,host,port,enabled) VALUES(1,'owned-logs-lease','127.0.0.1',9,true)") + const app = await (await import("../../index.js")).buildApp({ startScheduler: false, logger: false }) + try { + const address = await app.listen({ host: "127.0.0.1", port: 0 }) + await (await import("./logs-follow-sdk-cancellation-acceptance.js")).verifyLogsFollowSdkCancellation(app, `${address}/mcp`, actor, logsSdkScenario === "lease-loss") + } finally { await app.close() } + } else if (routerRequestDrainOnly) await (await import("../../test/mcp-router-request-drain.js")).verifyRouterRequestDrain() + else if (configurationAuditOnly) { + await (await import("../../test/mcp-configuration-audit-admission.js")).verifyMcpConfigurationAuditAdmission() + await pool.query("INSERT INTO servers(id,name,host,port,enabled) VALUES(1,'owned-config-audit','127.0.0.1',9,false)") + await (await import("../../test/mcp-acme-test.js")).verifyMcpAcmeTest(actor) + await (await import("../../test/mcp-telegram-test.js")).verifyMcpTelegramTest(actor) + } else if (administrationAdmissionOnly) await (await import("./administration-admission-acceptance.js")).verifyAdministrationAdmission(actor) + else console.log("Independent management transport:", await (await import("./management-transport-acceptance.js")).acceptManagementTransport(actor, managementAdmissionOnly)) + if (!managementAdmissionOnly && !administrationAdmissionOnly && !configurationAuditOnly && !routerRequestDrainOnly && !restReplicaOnly && !restoreQuarantineOnly && !logsSdkScenario && !sqliteImportCliOnly) { + await (await import("./routing-community-scoped-acceptance.js")).acceptScopedRouteCommunitySwitch(actor) + await (await import("./ipsec-user-delete-acceptance.js")).acceptIpsecUserDeletion(actor) + await (await import("./ipsec-peer-delete-acceptance.js")).acceptIpsecPeerDeletion(actor) + await (await import("./ipsec-certificate-delete-acceptance.js")).acceptIpsecCertificateDeletion(actor) + await (await import("./ipsec-peer-update-acceptance.js")).acceptIpsecPeerUpdate(actor) + await (await import("../../test/mcp-flow-purge.js")).verifyMcpFlowPurge(actor) + } +} catch (error) { + console.error("Focused path-check diagnostics:", (await pool.query("SELECT id,status,candidate_id,created_at,now() AS database_now,evidence->>'observedAt' AS observed_at,evidence->>'proofVersion' AS proof_version FROM mcp_path_checks ORDER BY created_at DESC LIMIT 5")).rows, { nodeNow: new Date().toISOString() }) + throw error +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/recursive-recovery.test.ts b/backend/src/modules/mcp/recursive-recovery.test.ts new file mode 100644 index 00000000..9f3016a5 --- /dev/null +++ b/backend/src/modules/mcp/recursive-recovery.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { recursiveRestoreMatches } from "./recursive-recovery.js" +import { changeMutationCost } from "./change-cost.js" + +const route = { id: "old", dstAddress: "192.0.2.0/24", gateway: "192.0.2.1", distance: 0, scope: 0, targetScope: 0, routingTable: "main", checkGateway: "none" as const, country: "country", comment: "fixture", disabled: false } +test("restored native multiset ignores ID/country/order but preserves zero and multiplicity", () => { + assert.equal(recursiveRestoreMatches([route], [{ ...route, id: "new", country: "" }]), true) + assert.equal(recursiveRestoreMatches([route], [{ ...route, distance: 1 }]), false) + assert.equal(recursiveRestoreMatches([route], [{ ...route, scope: 30 }]), false) + assert.equal(recursiveRestoreMatches([route, route], [route]), false) + assert.equal(recursiveRestoreMatches([route], [route, route]), false) + assert.equal(recursiveRestoreMatches([{ ...route, scope: null, targetScope: null }], [{ ...route, scope: 30, targetScope: 10 }]), true) + assert.equal(recursiveRestoreMatches([], []), true) + assert.equal(recursiveRestoreMatches([{ ...route, dstAddress: "192.0.2.1" }], [{ ...route, dstAddress: "192.0.2.1/32" }]), true) + assert.equal(recursiveRestoreMatches([{ ...route, scope: null, targetScope: 0 }, { ...route, scope: 0, targetScope: null }], [{ ...route, scope: 0, targetScope: 0 }, { ...route, scope: 30, targetScope: 0 }]), true) +}) +test("batch cost cannot be reduced below service/native steps or made nonintegral", () => { + const step = { kind: "service" as const, service: "fixture", input: {} } + assert.equal(changeMutationCost({ steps: [step] }), 1) + assert.equal(changeMutationCost({ steps: [step], mutationCost: 7 }), 7) + assert.equal(changeMutationCost({ steps: [step, step] }), 2) + for (const mutationCost of [0, -1, 1.5, NaN, Infinity, 10001]) assert.throws(() => changeMutationCost({ steps: [step], mutationCost })) + assert.throws(() => changeMutationCost({ steps: [step, step], mutationCost: 1 })) +}) diff --git a/backend/src/modules/mcp/recursive-recovery.ts b/backend/src/modules/mcp/recursive-recovery.ts new file mode 100644 index 00000000..799ffb6f --- /dev/null +++ b/backend/src/modules/mcp/recursive-recovery.ts @@ -0,0 +1,146 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { canonicalRecursiveRoutes, fingerprintPayload, getRevisionById } from "../../services/config-revisions.js" +import { parseRecursiveCatalog } from "../../services/recursive-route-catalog.js" +import { recursiveInputRowsSchema } from "../../services/recursive-route-validation.js" +import { storeRecursiveRoutes } from "../../services/recursive-route-store.js" +import { RecursiveApplyError } from "../../services/recursive-apply.js" +import { userRecursiveComment } from "../../services/config-apply-plan.js" +import { applyRecursiveToServer, readRecursiveNativeRowsForServer, readCachedRecursiveRoutes } from "../../routes/recursive-routes.js" +import { fingerprint, McpError } from "./security.js" +import { readRosSnapshot, routerClient } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" + +type Routes = ReturnType +const boundedRoutes = recursiveInputRowsSchema.refine(rows => rows.length <= 50, "Полный набор MCP ограничен 50 маршрутами") +const schema = z.object({ serverId: z.number().int().positive().safe(), revisionId: z.string().min(1).max(100) }).strict() +const replaceSchema = z.object({ serverId: z.number().int().positive().safe(), routes: boundedRoutes, source: z.enum(["apply", "copy"]).default("apply") }).strict() +const canonical = (routes: Routes) => canonicalRecursiveRoutes(routes).map(row => { + const { country: _country, ...native } = row as Record + if (typeof native.dstAddress === "string" && !native.dstAddress.includes("/")) native.dstAddress += "/32" + return native +}).sort((a, b) => fingerprint(a).localeCompare(fingerprint(b))) + +async function revision(input: Record) { + const row = await getRevisionById(String(input.revisionId), { serverId: Number(input.serverId), section: "recursive-routes" }) + if (!row || row.serverId !== input.serverId || row.section !== "recursive-routes") throw new McpError("NOT_FOUND", "Ревизия выбранного сервера и раздела не найдена", 404) + if (row.fingerprint !== fingerprintPayload(row.payload)) throw new McpError("STATE_CONFLICT", "Содержимое ревизии не соответствует fingerprint", 409) + const parsed = boundedRoutes.safeParse(row.payload) + if (!parsed.success) throw new McpError("UNSUPPORTED_CAPABILITY", "Ревизия содержит неподдерживаемые маршруты или превышает предел 50", 409) + return { id: row.id, fingerprint: row.fingerprint, routes: parsed.data.map((route, index) => ({ ...route, id: `revision-${index}`, comment: userRecursiveComment(route.comment) })) } +} +async function selection(input: Record) { + if (input.replaceSet !== true) return revision(input) + const routes = boundedRoutes.parse(input.routes).map(route => ({ ...route, comment: userRecursiveComment(route.comment) })) + return { id: "request", fingerprint: fingerprint({ routes, source: input.source }), routes } +} +async function metadataFingerprint(serverId: number) { + const cache = await readCachedRecursiveRoutes(serverId) + const bindings = (await pool.query("SELECT id::text, gateway_tunnel_id FROM filter_rules WHERE server_id=$1 AND gateway_tunnel_id LIKE 'rec:%' ORDER BY id", [serverId])).rows + return fingerprint({ cache, bindings }) +} +async function current(input: Record) { + const saved = await selection(input), { server, client } = await routerClient(Number(input.serverId)) + if (input.replaceSet === true) await storeRecursiveRoutes(server.id, saved.routes, input.source === "copy" ? "copy" : "apply", true) + else await assertNoFilterBindings(server.id) + const observed = parseRecursiveCatalog(await readRecursiveNativeRowsForServer(server)) + if (observed.length > 50) throw new McpError("UNSUPPORTED_CAPABILITY", "Текущий управляемый набор превышает предел 50", 409) + const names = new Set([...saved.routes, ...observed].map(route => route.gateway.split("%")[1]).filter(Boolean)) + const inventory = z.array(z.object({ ".id": z.string().min(1), name: z.string().min(1), type: z.string().min(1), disabled: z.union([z.string(), z.boolean()]).optional() }).passthrough()).parse(await client.get("/interface")) + if (new Set(inventory.map(row => row[".id"])).size !== inventory.length) throw new McpError("STATE_CONFLICT", "Неоднозначные ID интерфейсов", 409) + const interfaces = [...names].sort().map(name => { + const rows = inventory.filter(row => row.name === name) + if (rows.length !== 1) throw new McpError("STATE_CONFLICT", "Не подтверждён интерфейс gateway: " + name, 409) + return { id: rows[0][".id"], name, type: rows[0].type, disabled: rows[0].disabled ?? null } + }) + return { revisionId: saved.id, revisionFingerprint: saved.fingerprint, observed: canonical(observed), interfaces, + ...(input.replaceSet === true ? { metadataFingerprint: await metadataFingerprint(server.id) } : {}) } +} +async function assertNoFilterBindings(serverId: number) { + const { rows } = await pool.query("SELECT f.id FROM filter_rules f JOIN recursive_routes r ON r.server_id=f.server_id AND f.gateway_tunnel_id='rec:' || r.id::text WHERE r.server_id=$1 LIMIT 1", [serverId]) + if (rows.length) throw new McpError("STATE_CONFLICT", "BGP-фильтры используют ID рекурсивных маршрутов. Сначала подготовьте явное изменение зависимостей", 409) +} +/** A multiset match: native IDs change; omitted optional scopes retain REST + * semantics (RouterOS defaults), while explicit zero is compared exactly. */ +export function recursiveRestoreMatches(expected: Routes, observed: Routes) { + if (expected.length !== observed.length) return false + const actual = canonical(observed), desired = canonical(expected) + const edges = desired.map(row => actual.flatMap((candidate, index) => Object.entries(row).every(([key, value]) => (["scope", "targetScope"].includes(key) && value === null) || fingerprint(candidate[key]) === fingerprint(value)) ? [index] : [])) + const matches = new Map() + const assign = (row: number, seen: Set): boolean => { + for (const index of edges[row]) { + if (seen.has(index)) continue + seen.add(index) + const prior = matches.get(index) + if (prior === undefined || assign(prior, seen)) { matches.set(index, row); return true } + } + return false + } + return desired.every((_row, index) => assign(index, new Set())) +} + +export function registerRecursiveRecovery(register: typeof add) { + const service = { + current, + execute: async (input: Record) => { + const saved = await selection(input) + if (saved.fingerprint !== input.revisionFingerprint || fingerprint(saved.routes) !== fingerprint(input.routes)) throw new McpError("STATE_CONFLICT", "Выбранный набор маршрутов или ревизия изменились после подтверждения", 409) + const { server } = await routerClient(Number(input.serverId)) + try { + return { ...await applyRecursiveToServer(server, saved.routes, input.replaceSet === true ? input.source === "copy" ? "copy" : "apply" : "rollback", async observed => { + if (input.replaceSet === true) { + if (await metadataFingerprint(server.id) !== input.metadataFingerprint) throw new McpError("STATE_CONFLICT", "Кеш маршрутов или BGP-ссылки изменились перед первой записью", 409) + } else await assertNoFilterBindings(server.id) + if (fingerprint(canonical(observed)) !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "Набор маршрутов изменился перед первой записью", 409) + }), verified: true, forwardingVerified: false } + } catch (error) { + if (error instanceof RecursiveApplyError && ["partial", "applied"].includes(error.receipt.state)) return { ...error.receipt, verified: false, forwardingVerified: false } + throw error + } + }, + verify: async (input: Record, result: unknown) => { + if ((result as { verified?: boolean } | null)?.verified !== true) return false + try { + const { server } = await routerClient(Number(input.serverId)) + const observed = parseRecursiveCatalog(await readRecursiveNativeRowsForServer(server)) + if (!recursiveRestoreMatches(boundedRoutes.parse(input.routes), observed)) return false + const cached = await readCachedRecursiveRoutes(server.id) + if (fingerprintPayload(canonicalRecursiveRoutes(cached)) !== fingerprintPayload(canonicalRecursiveRoutes(boundedRoutes.parse(input.routes)))) return false + const { rows } = await pool.query("SELECT payload FROM config_revisions WHERE server_id=$1 AND section='recursive-routes' ORDER BY created_at DESC LIMIT 1", [input.serverId]) + return rows.length === 1 && fingerprintPayload(rows[0].payload) === fingerprintPayload(canonicalRecursiveRoutes(boundedRoutes.parse(input.routes))) + } catch { return false } + }, + } + registerMcpService("routing.recursive.restore", service) + registerMcpService("routing.recursive.replace", service) + const prepare = async (input: Record): Promise => { + const saved = await selection(input), sid = Number(input.serverId) + const snapshots = await Promise.all(["ipv4_route", "routing_table", "routing_rule", "vrf", "ipv4_address"].map(resource => readRosSnapshot(sid, resource))) + const before = await current(input) + if (before.revisionFingerprint !== saved.fingerprint) throw new McpError("STATE_CONFLICT", "Ревизия изменилась во время подготовки", 409) + const observedRaw = parseRecursiveCatalog(await readRecursiveNativeRowsForServer((await routerClient(sid)).server)) + if (fingerprint(canonical(observedRaw)) !== fingerprint(before.observed)) throw new McpError("STATE_CONFLICT", "Маршруты изменились во время подготовки", 409) + const objects: ChangeEvidence["objects"] = observedRaw.map(route => ({ kind: "ipv4_route", id: route.id, serverId: sid })) + objects.push(...saved.routes.map((route, index) => ({ kind: "ipv4_route", id: route.id || `new-${index}`, serverId: sid }))) + for (const name of new Set([...saved.routes, ...observedRaw].map(route => route.routingTable))) { + const rows = snapshots.find(snapshot => snapshot.resource === "routing_table")!.rows.filter(row => row.name === name) + if (rows.length !== 1 || typeof rows[0][".id"] !== "string" || !rows[0][".id"] || [true, "true", "yes"].includes(rows[0].disabled as string | boolean)) throw new McpError("STATE_CONFLICT", "Не подтверждена таблица маршрутизации: " + name, 409) + objects.push({ kind: "routing_table", id: String(rows[0][".id"]), serverId: sid }) + } + for (const parent of before.interfaces) objects.push({ kind: "interface", id: parent.id, serverId: sid }, { kind: parent.type === "ether" ? "ethernet" : parent.type, id: parent.id, serverId: sid }) + // Full-set service writes use the executor's server-wide guard. Mirror + // every server protection here so denial happens before command dispatch. + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(sid)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел проверки защит", 503) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: sid }) + return { target: { scope: "servers", serverIds: [sid] }, objects, snapshots, before, mutationCost: saved.routes.length + observedRaw.length + 1, + steps: [{ kind: "service" as const, service: input.replaceSet === true ? "routing.recursive.replace" : "routing.recursive.restore", input: { ...input, routes: saved.routes, revisionFingerprint: saved.fingerprint, beforeFingerprint: fingerprint(before.observed), ...(input.replaceSet === true ? { metadataFingerprint: before.metadataFingerprint } : {}) } }], requiresHuman: true, + warnings: [`Полностью заменяется управляемый набор: создать ${saved.routes.length}, удалить ${observedRaw.length}, сохранить ревизию. Native ID изменятся.`, "Чужие маршруты не удаляются. Пустой список явно удаляет весь управляемый набор.", "NAT/RPF/таблицы/интерфейсы не меняются. Связность и обратный путь требуют самостоятельной проверки.", "При частичном/неизвестном исходе нет автоматического повтора или универсального inverse."] } + } + register("routing.recursive.revision.restore", "mm_recursive_revision_restore_plan", "Восстановление полной ревизии рекурсивных маршрутов", "network", "mm:network:write", "write", "servers", schema, { prepare }) + register("routing.recursive.routes.replace", "mm_recursive_routes_replace_plan", "Применение полного набора рекурсивных маршрутов приложения", "network", "mm:network:write", "write", "servers", replaceSchema, { + prepare: input => prepare({ ...input, replaceSet: true }), + }) +} diff --git a/backend/src/modules/mcp/registry.ts b/backend/src/modules/mcp/registry.ts new file mode 100644 index 00000000..73da575a --- /dev/null +++ b/backend/src/modules/mcp/registry.ts @@ -0,0 +1,59 @@ +import { mcpOperationDescriptorSchema, type McpOperationDescriptor } from "@mmapp/contracts/mcp" + +type Definition = Pick + +// Baseline planned definitions. Typed runtime actions promote implemented entries. +// Adding a definition never grants it to an existing connection. +const definitions: Definition[] = [ + { id: "capabilities.read", toolName: "mm_capabilities_get", title: "Доступные возможности", section: "mcp", effect: "read", risk: "ordinary", scope: "connection", requiredPermissions: [] }, + { id: "servers.list", toolName: "mm_servers_list", title: "Каталог серверов", section: "servers", effect: "read", risk: "ordinary", scope: "servers", requiredPermissions: ["mm:servers:read"] }, + { id: "users.list", toolName: "mm_users_list", title: "Каталог пользователей", section: "users", effect: "read", risk: "ordinary", scope: "global", requiredPermissions: ["mm:users:read"] }, + { id: "users.create", toolName: "mm_users_create_plan", title: "Создание пользователя", section: "users", effect: "write", risk: "ordinary", scope: "global", requiredPermissions: ["mm:users:write"] }, + { id: "users.permissions.update", toolName: "mm_users_permissions_update_plan", title: "Изменение прав пользователя", section: "users", effect: "write", risk: "dangerous", scope: "global", requiredPermissions: ["mm:settings:admin"] }, + { id: "logs.query", toolName: "mm_logs_query", title: "Поиск логов", section: "logs", effect: "read", risk: "ordinary", scope: "servers", requiredPermissions: ["mm:servers:read"] }, + { id: "logs.context", toolName: "mm_logs_context_get", title: "Контекст события", section: "logs", effect: "read", risk: "ordinary", scope: "servers", requiredPermissions: ["mm:servers:read"] }, + { id: "logs.follow", toolName: "mm_logs_follow", title: "Ожидание новых логов", section: "logs", effect: "read", risk: "ordinary", scope: "servers", requiredPermissions: ["mm:servers:read"] }, + { id: "logs.settings.update", toolName: "mm_logs_settings_update_plan", title: "Настройки сборщика логов", section: "logs", effect: "write", risk: "dangerous", scope: "global", requiredPermissions: ["mm:settings:admin"] }, + { id: "routing.analyze", toolName: "mm_route_optimizer_analyze", title: "Анализ маршрутизации", section: "network", effect: "read", risk: "ordinary", scope: "servers", requiredPermissions: ["mm:network:read"] }, + { id: "routing.ospf.optimize", toolName: "mm_ospf_optimizer_plan", title: "Оптимизация OSPF", section: "network", effect: "write", risk: "dangerous", scope: "servers", requiredPermissions: ["mm:network:write"] }, + { id: "routing.ecmp.update", toolName: "mm_ecmp_plan", title: "Настройка ECMP", section: "network", effect: "write", risk: "dangerous", scope: "servers", requiredPermissions: ["mm:network:write"] }, + { id: "routing.vrf.update", toolName: "mm_vrf_plan", title: "Настройка VRF", section: "network", effect: "write", risk: "dangerous", scope: "servers", requiredPermissions: ["mm:network:write"] }, + { id: "interfaces.update", toolName: "mm_interfaces_update_plan", title: "Настройка интерфейса", section: "network", effect: "write", risk: "dangerous", scope: "servers", requiredPermissions: ["mm:network:write"] }, + { id: "firewall.update", toolName: "mm_firewall_rule_update_plan", title: "Настройка firewall", section: "network", effect: "write", risk: "dangerous", scope: "servers", requiredPermissions: ["mm:network:write"] }, + { id: "diagnostics.bandwidth", toolName: "mm_routing_measurement_plan", title: "Нагрузочный bandwidth-тест", section: "network", effect: "diagnostic", risk: "dangerous", scope: "servers", requiredPermissions: ["mm:network:write"] }, + { id: "database.restore", toolName: "mm_database_restore_plan", title: "Восстановление базы приложения", section: "settings", effect: "write", risk: "dangerous", scope: "global", requiredPermissions: ["mm:settings:admin"] }, +] + +const registry = definitions.map(definition => mcpOperationDescriptorSchema.parse({ + ...definition, + version: 1, + implementation: "planned", + annotations: { + readOnlyHint: definition.effect === "read", + destructiveHint: definition.effect === "write", + idempotentHint: definition.effect === "read", + openWorldHint: true, + }, +})) +if (new Set(registry.map(operation => operation.id)).size !== registry.length || + new Set(registry.map(operation => operation.toolName)).size !== registry.length) { + throw new Error("Повторяющиеся операции MCP") +} + +// Return copies so callers cannot mutate trusted policy metadata. +export function listMcpOperations(): McpOperationDescriptor[] { + return registry.map(operation => mcpOperationDescriptorSchema.parse(operation)) +} + +export function findMcpOperation(id: string): McpOperationDescriptor | undefined { + const operation = registry.find(candidate => candidate.id === id) + return operation ? mcpOperationDescriptorSchema.parse(operation) : undefined +} + +export function registerMcpOperation(operation: McpOperationDescriptor): void { + const parsed = mcpOperationDescriptorSchema.parse(operation) + const existing = registry.findIndex(candidate => candidate.id === parsed.id) + if (registry.some((candidate, index) => index !== existing && candidate.toolName === parsed.toolName)) throw new Error("Повторяющееся имя инструмента MCP") + if (existing >= 0) registry[existing] = parsed + else registry.push(parsed) +} diff --git a/backend/src/modules/mcp/request-monitor-projection.ts b/backend/src/modules/mcp/request-monitor-projection.ts new file mode 100644 index 00000000..ad223db2 --- /dev/null +++ b/backend/src/modules/mcp/request-monitor-projection.ts @@ -0,0 +1,19 @@ +/** Display projection only. Never use it as an execution, approval or audit + * authority source. Original JSONB remains intact in mcp_requests. The logical + * UTF-8 bound is checked in PostgreSQL before node-postgres decodes the rows; + * physical/TOAST storage size is not a payload bound. */ +export const MCP_REQUEST_DISPLAY_FIELD_BYTES = 16384 +const displayField = (name: "input" | "result") => `CASE + WHEN r.${name} IS NULL OR pg_catalog.octet_length(r.${name}::pg_catalog.text)<=${MCP_REQUEST_DISPLAY_FIELD_BYTES} THEN r.${name} + ELSE pg_catalog.jsonb_build_object('detailsOmitted',true,'reason','DISPLAY_LIMIT', + 'encodedBytes',pg_catalog.octet_length(r.${name}::pg_catalog.text), + 'message','Подробности слишком велики для отображения. Исходная запись сохранена в журнале.') + END AS ${name}` + +// Explicit columns prevent sending the original large value alongside its +// display replacement. Owner/filter/cursor predicates remain in the caller. +export const MCP_REQUEST_MONITOR_PROJECTION = [ + "r.id", "r.connection_id", "r.actor_id", "r.tool", "r.server_ids", "r.status", + displayField("input"), displayField("result"), "r.error_code", "r.duration_ms", + "r.created_at", "r.finished_at", +].join(",") diff --git a/backend/src/modules/mcp/resources.ts b/backend/src/modules/mcp/resources.ts new file mode 100644 index 00000000..32ee8921 --- /dev/null +++ b/backend/src/modules/mcp/resources.ts @@ -0,0 +1,459 @@ +import { assertNoCallerCreateId } from "./native-create-identity.js" +import { z } from "zod" +import { isIP } from "node:net" +import { eq } from "drizzle-orm" +import { db } from "../../db/index.js" +import { servers } from "../../db/schema.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { readRouterOsSnapshotGet } from "../../services/routeros-snapshot-read.js" +import { McpError, fingerprint, privateFingerprinter, privateSecretFingerprint } from "./security.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { greNativeFieldsSchema } from "../../services/gre-mutation-input.js" +import { canonicalVrfMembers } from "../../services/vrf-members.js" +import { canonicalBridgeVlanValue } from "../../services/bridge-vlan-values.js" +import { canonicalBgpPolicyPrefix } from "../../services/bgp-community-policy-model.js" +import { observedBoolean, observedDurationMs } from "../../services/ospf-observed-values.js" +import { ipv6NdFields, ipv6NdPrefixFields, ipv6NdSettingsFields, normalizeIpv6NdRow } from "../../services/ipv6-nd-configuration.js" + +const text = z.string().max(1000) +const name = z.string().trim().min(1).max(100) +const bool = z.boolean() +const vlanIds = z.string().min(1).max(16384).refine(value => { + try { canonicalBridgeVlanValue("vlan-ids", value); return true } catch { return false } +}, "Требуется непересекающийся список VLAN ID/диапазонов 1..4094") +const integer = (min: number, max: number) => z.number().int().min(min).max(max) +const common = { name: name.optional(), comment: text.optional(), disabled: bool.optional() } +const ipv4 = name.refine(value => isIP(value) === 4, "Требуется IPv4 router/area ID") +const ospfRouterId = name.refine(value => isIP(value) === 4 || /^[a-z0-9_.-]+$/i.test(value) && !/^[\d.]+$/.test(value), "Требуется IPv4 или имя Router ID") +const ospfFilterName = z.string().max(100).refine(value => value.trim() === value && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(value)) +export const ospfFilterFields = z.object({ "in-filter-chain": ospfFilterName.optional(), "out-filter-chain": ospfFilterName.optional(), "out-filter-select": ospfFilterName.optional() }).strict() +const ospfRouteSources = z.enum(["connected", "static", "rip", "ospf", "isis", "bgp", "vpn", "dhcp", "fantasy", "modem", "bgp-mpls-vpn", "slaac"]) +const ospfRedistribute = text.transform((value, context) => { + if (value === "") return "" + const sources = value.split(",").map(source => source.trim()) + if (new Set(sources).size !== sources.length || sources.some(source => !ospfRouteSources.safeParse(source).success)) { + context.addIssue({ code: "custom", message: "Требуется список источников OSPF без повторов" }) + return z.NEVER + } + return sources.sort().join(",") +}) +const interval = z.string().regex(/^(?:[1-9]|[1-5][0-9]|60)s$/) +const ospfTimerFields = ["hello-interval", "dead-interval", "retransmit-interval", "transmit-delay"] as const +function canonicalOspfTimer(value: string): string | undefined { + const ms = observedDurationMs(value) + return ms !== null && ms > 0 && ms % 1000 === 0 ? `${ms / 1000}s` : undefined +} +const ospfLinkStateInterval = z.string().max(64).transform((value, context) => { + const canonical = canonicalOspfTimer(value) + if (canonical) return canonical + context.addIssue({ code: "custom", message: "Укажите положительный интервал OSPF в целых секундах с единицей времени" }) + return z.NEVER +}) +function canonicalBfdInterval(ms: number | null): string | null { + if (ms === null || ms < 1 || ms > 60_000) return null + const wholeMs = Math.round(ms) + if (Math.abs(ms - wholeMs) > 1e-9) return null + return wholeMs % 1000 === 0 ? `${wholeMs / 1000}s` : `${wholeMs}ms` +} +const bfdInterval = z.string().regex(/^[1-9]\d{0,4}(?:ms|s)$/).transform((value, context) => { + const normalized = canonicalBfdInterval(observedDurationMs(value)) + if (normalized !== null) return normalized + context.addIssue({ code: "custom", message: "Укажите интервал BFD от 1ms до 60s" }) + return z.NEVER +}) +const bfdAddressPrefix = name.transform((value, context) => { + try { + const prefix = canonicalBgpPolicyPrefix(value) + if (!prefix.endsWith("/0")) return prefix + } catch { /* The schema reports a common error for invalid prefixes. */ } + context.addIssue({ code: "custom", message: "Укажите явный IPv4/IPv6-префикс BFD с ненулевой длиной" }) + return z.NEVER +}) +const ospfNetworks = text.min(1).transform((value, context) => { + try { + const prefixes = value.split(",").map(canonicalBgpPolicyPrefix) + if (new Set(prefixes).size !== prefixes.length) throw new Error("Duplicate prefixes") + return prefixes.join(",") + } catch { + context.addIssue({ code: "custom", message: "Требуется список IPv4/IPv6-префиксов без повторов" }) + return z.NEVER + } +}) +const firewall = { + chain: name.optional(), action: name.optional(), protocol: text.optional(), "src-address": text.optional(), "dst-address": text.optional(), + "src-port": text.optional(), "dst-port": text.optional(), "in-interface": text.optional(), "out-interface": text.optional(), + "src-address-list": text.optional(), "dst-address-list": text.optional(), "connection-state": text.optional(), + comment: text.optional(), disabled: bool.optional(), log: bool.optional(), "log-prefix": text.optional(), +} +const route = { + "dst-address": name.optional(), gateway: name.optional(), distance: integer(1, 255).optional(), scope: integer(0, 255).optional(), + "target-scope": integer(0, 255).optional(), "routing-table": name.optional(), "check-gateway": z.enum(["none", "ping", "arp", "bfd"]).optional(), + disabled: bool.optional(), comment: text.optional(), +} +export interface RosResourceSpec { + id: string; path: string; title: string; permission: string; fields: z.ZodObject; + create: boolean; remove: boolean; requiredOnCreate?: string[]; readOnly?: boolean + /** Native configuration sealed for drift checks, never accepted as writes. */ + observeFields?: readonly string[] +} +export const rosResources: RosResourceSpec[] = [ + { id: "routing_filter", path: "/routing/filter/rule", title: "Фильтры маршрутизации", permission: "mm:filters", fields: z.object({ chain: text.optional(), rule: text.optional(), comment: text.optional(), disabled: bool.optional() }).strict(), create: false, remove: false, readOnly: true }, + { id: "ethernet", path: "/interface/ethernet", title: "Ethernet", permission: "mm:network", fields: z.object({ ...common, mtu: integer(68, 65535).optional(), "auto-negotiation": bool.optional() }).strict(), create: false, remove: false }, + { id: "bridge", path: "/interface/bridge", title: "Bridge", permission: "mm:network", fields: z.object({ ...common, mtu: integer(68, 65535).optional(), "vlan-filtering": bool.optional(), pvid: integer(1, 4094).optional(), "frame-types": z.enum(["admit-all", "admit-only-vlan-tagged", "admit-only-untagged-and-priority-tagged"]).optional(), "ingress-filtering": bool.optional(), "protocol-mode": z.enum(["none", "stp", "rstp", "mstp"]).optional() }).strict(), observeFields: ["mvrp"], create: true, remove: true, requiredOnCreate: ["name"] }, + { id: "bridge_port", path: "/interface/bridge/port", title: "Порты bridge", permission: "mm:network", fields: z.object({ bridge: name.optional(), interface: name.optional(), pvid: integer(1, 4094).optional(), "ingress-filtering": bool.optional(), "frame-types": z.enum(["admit-all", "admit-only-vlan-tagged", "admit-only-untagged-and-priority-tagged"]).optional(), disabled: bool.optional(), comment: text.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["bridge", "interface"] }, + { id: "bridge_vlan", path: "/interface/bridge/vlan", title: "Bridge VLAN membership", permission: "mm:network", fields: z.object({ bridge: name.optional(), "vlan-ids": vlanIds.optional(), tagged: text.optional(), untagged: text.optional(), disabled: bool.optional(), comment: text.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["bridge", "vlan-ids"] }, + { id: "vlan", path: "/interface/vlan", title: "VLAN", permission: "mm:network", fields: z.object({ ...common, interface: name.optional(), "vlan-id": integer(1, 4094).optional(), mtu: integer(68, 65535).optional(), "use-service-tag": bool.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name", "interface", "vlan-id"] }, + { id: "ipv4_address", path: "/ip/address", title: "IPv4-адреса", permission: "mm:network", fields: z.object({ address: name.optional(), interface: name.optional(), network: name.optional(), comment: text.optional(), disabled: bool.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["address", "interface"] }, + { id: "ipv6_address", path: "/ipv6/address", title: "IPv6-адреса", permission: "mm:network", fields: z.object({ address: name.optional(), interface: name.optional(), advertise: bool.optional(), comment: text.optional(), disabled: bool.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["address", "interface"] }, + { id: "ipv4_route", path: "/ip/route", title: "Маршруты IPv4", permission: "mm:network", fields: z.object(route).strict(), create: true, remove: true, requiredOnCreate: ["dst-address"] }, + { id: "ipv6_route", path: "/ipv6/route", title: "Маршруты IPv6", permission: "mm:network", fields: z.object(route).strict(), create: true, remove: true, requiredOnCreate: ["dst-address"] }, + { id: "routing_table", path: "/routing/table", title: "Таблицы маршрутизации", permission: "mm:network", fields: z.object({ name: name.optional(), fib: bool.optional(), disabled: bool.optional() }).strict(), observeFields: ["invalid"], create: true, remove: true, requiredOnCreate: ["name"] }, + { id: "routing_rule", path: "/routing/rule", title: "Правила маршрутизации", permission: "mm:network", fields: z.object({ "src-address": text.optional(), "dst-address": text.optional(), interface: text.optional(), action: z.enum(["lookup", "lookup-only-in-table", "drop", "unreachable"]).optional(), table: name.optional(), disabled: bool.optional(), comment: text.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["action"] }, + { id: "vrf", path: "/ip/vrf", title: "VRF", permission: "mm:network", fields: z.object({ name: name.optional(), interfaces: text.optional(), disabled: bool.optional(), comment: text.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name", "interfaces"] }, + { id: "ip_settings", path: "/ip/settings", title: "RPF и IPv4 hash-policy", permission: "mm:network", fields: z.object({ "rp-filter": z.enum(["no", "loose", "strict"]).optional(), "ipv4-multipath-hash-policy": z.enum(["l3", "l4", "l3-inner"]).optional() }).strict(), create: false, remove: false }, + { id: "ipv6_settings", path: "/ipv6/settings", title: "IPv6 hash-policy", permission: "mm:network", fields: z.object({ "multipath-hash-policy": z.enum(["l3", "l4", "l3-inner"]).optional() }).strict(), create: false, remove: false }, + ...(["ip", "ipv6"] as const).flatMap(family => (["filter", "nat", "mangle", "raw"] as const).map(table => ({ id: `${family}_${table}`, path: `/${family}/firewall/${table}`, title: `${family} firewall ${table}`, permission: "mm:network", fields: z.object(firewall).strict(), create: true, remove: true, requiredOnCreate: ["chain", "action"] }))), + { id: "gre", path: "/interface/gre", title: "GRE", permission: "mm:network", fields: greNativeFieldsSchema, create: true, remove: true, requiredOnCreate: ["name", "remote-address"] }, + { id: "wireguard", path: "/interface/wireguard", title: "WireGuard", permission: "mm:network", fields: z.object({ ...common, "listen-port": integer(1, 65535).optional(), mtu: integer(68, 65535).optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name"] }, + { id: "wireguard_peer", path: "/interface/wireguard/peers", title: "WireGuard peers", permission: "mm:network", fields: z.object({ interface: name.optional(), "public-key": name.optional(), "allowed-address": text.optional(), "endpoint-address": text.optional(), "endpoint-port": integer(0, 65535).optional(), "persistent-keepalive": text.optional(), disabled: bool.optional(), comment: text.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["interface", "public-key", "allowed-address"] }, + { id: "vxlan", path: "/interface/vxlan", title: "VXLAN", permission: "mm:network", fields: z.object({ ...common, vni: integer(1, 16777215).optional(), port: integer(1, 65535).optional(), "local-address": text.optional(), interface: text.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name", "vni"] }, + { id: "ospf_template", path: "/routing/ospf/interface-template", title: "OSPF templates", permission: "mm:network", fields: z.object({ interfaces: text.optional(), networks: ospfNetworks.optional(), "prefix-list": z.union([z.literal(""), name]).optional(), area: name.optional(), "instance-id": integer(0, 255).optional(), cost: integer(1, 65535).optional(), priority: integer(0, 255).optional(), type: z.enum(["broadcast", "nbma", "ptp", "ptmp", "ptmp-broadcast", "ptp-unnumbered"]).optional(), passive: bool.optional(), "use-bfd": bool.optional(), "hello-interval": interval.optional(), "dead-interval": interval.optional(), "retransmit-interval": ospfLinkStateInterval.optional(), "transmit-delay": ospfLinkStateInterval.optional(), disabled: bool.optional(), comment: text.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["area"] }, + { id: "ospf_neighbor", path: "/routing/ospf/neighbor", title: "Соседи OSPF", permission: "mm:network", fields: z.object({ "router-id": text.optional(), interface: text.optional(), state: text.optional(), address: text.optional(), area: text.optional(), instance: text.optional() }).strict(), create: false, remove: false, readOnly: true }, + { id: "ospf_instance", path: "/routing/ospf/instance", title: "OSPF instances", permission: "mm:network", fields: z.object({ ...common, "router-id": ospfRouterId.optional(), version: z.enum(["2", "3"]).optional(), "routing-table": name.optional(), vrf: name.optional(), redistribute: ospfRedistribute.optional(), "originate-default": z.enum(["never", "always", "if-installed"]).optional(), ...ospfFilterFields.shape }).strict(), create: true, remove: true, requiredOnCreate: ["name", "router-id", "version", "vrf"] }, + { id: "ospf_area", path: "/routing/ospf/area", title: "OSPF areas", permission: "mm:network", fields: z.object({ ...common, instance: name.optional(), "area-id": ipv4.optional(), type: z.enum(["default", "stub", "nssa"]).optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name", "instance", "area-id"] }, + { id: "bfd_configuration", path: "/routing/bfd/configuration", title: "Правила BFD", permission: "mm:network", fields: z.object({ interfaces: name.optional(), addresses: bfdAddressPrefix.optional(), "address-list": name.optional(), vrf: name.optional(), "forbid-bfd": bool.optional(), "min-rx": bfdInterval.optional(), "min-tx": bfdInterval.optional(), multiplier: integer(2, 50).optional(), disabled: bool.optional(), comment: text.optional() }).strict(), observeFields: ["inactive", "keyring"], create: true, remove: true, requiredOnCreate: ["vrf"] }, + { id: "bfd_session", path: "/routing/bfd/session", title: "Наблюдаемые сессии BFD", permission: "mm:network", fields: z.object({ "remote-address": text.optional(), "local-address": text.optional(), vrf: text.optional(), interface: text.optional(), state: text.optional() }).strict(), create: false, remove: false, readOnly: true }, + { id: "bgp_session", path: "/routing/bgp/session", title: "BGP sessions", permission: "mm:bgp", fields: z.object({}).strict(), create: false, remove: false, readOnly: true }, + { id: "bgp_instance", path: "/routing/bgp/instance", title: "BGP instances", permission: "mm:bgp", fields: z.object({ ...common, as: integer(1, 4294967295).optional(), "router-id": ipv4.optional(), vrf: name.optional(), "routing-table": name.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name", "as", "router-id", "vrf", "routing-table"] }, + { id: "bgp_connection", path: "/routing/bgp/connection", title: "BGP connections", permission: "mm:bgp", fields: z.object({ ...common, instance: name.optional(), "remote.address": name.refine(value => isIP(value) > 0, "Требуется конкретный адрес BGP-соседа").optional(), "local.address": name.refine(value => isIP(value) > 0).optional(), "remote.as": integer(1, 4294967295).optional(), "local.role": z.enum(["ibgp", "ibgp-rr", "ebgp", "ebgp-provider", "ebgp-customer", "ebgp-peer", "ebgp-rs", "ebgp-rs-client"]).optional(), connect: bool.optional(), listen: bool.optional(), afi: z.enum(["ip", "ipv6", "ip,ipv6"]).optional(), "use-bfd": bool.optional(), "routing-table": name.optional(), vrf: name.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name", "instance", "remote.address", "remote.as", "local.address", "local.role"] }, + { id: "ipsec_peer", path: "/ip/ipsec/peer", title: "IPsec peers", permission: "mm:network", fields: z.object({ ...common, address: text.optional(), "exchange-mode": z.enum(["ike2", "main", "aggressive"]).optional(), profile: name.optional(), passive: bool.optional() }).strict(), create: true, remove: true, requiredOnCreate: ["name", "address"] }, + { id: "container", path: "/container", title: "Контейнеры", permission: "mm:network", fields: z.object({ comment: text.optional(), "start-on-boot": bool.optional() }).strict(), create: false, remove: true }, + { id: "certificate", path: "/certificate", title: "Сертификаты", permission: "mm:certificates", fields: z.object({ name: name.optional(), trusted: bool.optional() }).strict(), create: false, remove: true }, +] + +const ospfRouterIdDependency: RosResourceSpec = { + id: "ospf_router_id", path: "/routing/id", title: "OSPF Router ID dependency", permission: "mm:network", + fields: z.object({}).strict(), observeFields: ["name", "id", "dynamic-id", "disabled", "inactive", "select-dynamic-id", "select-from-vrf"], + create: false, remove: false, readOnly: true, +} +const ospfFilterSelectDependency: RosResourceSpec = { + id: "routing_filter_select", path: "/routing/filter/select-rule", title: "Routing filter selection dependency", permission: "mm:filters", + fields: z.object({}).strict(), create: false, remove: false, readOnly: true, +} + +export type ConfigurationRow = Record +export function isStaticArpRow(row: ConfigurationRow): boolean { + const manual = row.dynamic === "false" && row.dhcp === "false" + const permanent = row.status === "permanent" && [undefined, "false"].includes(row.dynamic as string | undefined) && [undefined, "false"].includes(row.dhcp as string | undefined) + return (manual || permanent) && ![true, "true", "yes"].includes(row.builtin as string | boolean) && ![true, "true", "yes"].includes(row.default as string | boolean) +} +export interface RosStep { serverId: number; resource: string; action: "create" | "update" | "delete"; id?: string; fields: Record; placeBeforeId?: string; expectedCost?: number; expectedServerProof?: string } +export function supportsRosPlacement(step: Pick) { + return step.placeBeforeId === undefined || step.action === "create" && ["vrf", "bfd_configuration"].includes(step.resource) && /^\*[a-z0-9]+$/i.test(step.placeBeforeId) +} +export const ROS_SNAPSHOT_VERSION = 3 +// OSPF cost plans and archived inverses issued before prefix-list evidence cannot be reused. +export const isSingletonRosResource = (resource: string) => ["ip_settings", "ipv6_settings", "ipv6_nd_settings"].includes(resource) +export const rosSnapshotVersion = (resource: string): number => resource === "ospf_template" ? 10 : resource === "bfd_configuration" ? 7 : ["bridge", "bridge_port", "bridge_vlan", "routing_table", "ip_settings", "ipv6_settings", "ospf_instance", "ipv6_nd"].includes(resource) ? 4 : ROS_SNAPSHOT_VERSION +export interface RosSnapshot { serverId: number; resource: string; rows: ConfigurationRow[]; fingerprint: string; projectionVersion?: number; sourceProof?: string } +const firewallAddressListResources = ["ip_address_list", "ipv6_address_list"] +const routingPolicyResources = new Set(["routing_rule", "routing_filter", "routing_filter_select", "bfd_configuration", ...firewallAddressListResources, "ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw"]) +const routingPolicyPrefix = "routing_policy:" +const routingPolicyCounters = new Set(["bytes", "packets"]) +export function isRoutingPolicySnapshot(resource: string) { + return resource.startsWith(routingPolicyPrefix) && routingPolicyResources.has(resource.slice(routingPolicyPrefix.length)) +} +export function readRoutingPolicySnapshot(serverId: number, resource: string, expectedSourceProof?: string, beforeRead?: () => Promise) { + if (!routingPolicyResources.has(resource)) throw new McpError("UNSUPPORTED_CAPABILITY", "Неизвестный каталог политики маршрутизации", 409) + return readRosSnapshot(serverId, routingPolicyPrefix + resource, beforeRead, expectedSourceProof) +} + +async function routingPolicyRows(resource: string, payload: unknown): Promise { + if (!Array.isArray(payload)) throw new McpError("STATE_CONFLICT", "Каталог политики маршрутизации не подтверждён", 409) + const filterCatalog = resource === routingPolicyPrefix + "routing_filter" || resource === routingPolicyPrefix + "routing_filter_select" + const bfdCatalog = resource === routingPolicyPrefix + "bfd_configuration" + const addressList = firewallAddressListResources.includes(resource.slice(routingPolicyPrefix.length)) + if ((filterCatalog || bfdCatalog || addressList) && (payload.length > 1000 || Buffer.byteLength(JSON.stringify(payload)) > 1048576)) throw new McpError("STATE_UNAVAILABLE", "Каталог политики маршрутизации превышает лимит", 409) + const identify = await privateFingerprinter(`mcp-routing-policy-v1:${resource}`) + const ids = new Set() + const rows = payload.map(row => { + const id = row?.[".id"] + if (!row || typeof row !== "object" || Array.isArray(row) || typeof id !== "string" || !id.trim() || ids.has(id)) { + throw new McpError("STATE_CONFLICT", "Неоднозначные ID каталога политики маршрутизации", 409) + } + ids.add(id) + // Seal the native rule, including selectors outside the write schema. + const configuration = Object.fromEntries(Object.entries(row).filter(([name]) => bfdCatalog || resource === routingPolicyPrefix + "routing_rule" || !routingPolicyCounters.has(name))) + const timed = ![undefined, null, "", "0", "0s", "none-static"].includes(row.timeout) + if (addressList && timed) configuration.timeout = "non-permanent" + const proof = identify(fingerprint(configuration)) + if (bfdCatalog) delete configuration[".nextid"] + return { ".id": id, action: row.action ?? null, disabled: row.disabled ?? null, ...(filterCatalog ? { chain: row.chain ?? null, invalid: row.invalid ?? null, inactive: row.inactive ?? null } : {}), ...(bfdCatalog ? { dynamic: row.dynamic ?? null, builtin: row.builtin ?? null, default: row.default ?? null, ruleProof: identify(fingerprint(configuration)) } : {}), ...(addressList ? { list: row.list ?? null, address: row.address ?? null, dynamic: row.dynamic ?? null, timed } : {}), proof } + }) + return addressList ? rows.sort((a, b) => a[".id"].localeCompare(b[".id"])) : rows +} +const ipv6NdResources: Record = { + ipv6_nd: { id: "ipv6_nd", path: "/ipv6/nd", title: "IPv6 Router Advertisement", permission: "mm:network", fields: ipv6NdFields, observeFields: ["invalid"], create: true, remove: true, requiredOnCreate: ["interface"] }, + ipv6_nd_prefix: { id: "ipv6_nd_prefix", path: "/ipv6/nd/prefix", title: "Префиксы IPv6 ND", permission: "mm:network", fields: ipv6NdPrefixFields, observeFields: ["invalid"], create: true, remove: true, requiredOnCreate: ["interface", "prefix"] }, + ipv6_nd_settings: { id: "ipv6_nd_settings", path: "/ipv6/nd/settings", title: "Глобальные настройки IPv6 ND", permission: "mm:network", fields: ipv6NdSettingsFields, create: false, remove: false }, +} +const ipArpResource: RosResourceSpec = { + id: "ip_arp", path: "/ip/arp", title: "Статический ARP", permission: "mm:network", + fields: z.object({ + address: z.string().refine(value => isIP(value) === 4).optional(), interface: name.optional(), + "mac-address": z.string().regex(/^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i).transform(value => value.toLowerCase()).optional(), + published: bool.optional(), disabled: bool.optional(), + }).strict(), observeFields: ["dhcp", "vrf", "comment", "status"], create: true, remove: true, requiredOnCreate: ["address", "interface"], +} +export function resourceById(id: string): RosResourceSpec { + if (Object.hasOwn(ipv6NdResources, id)) return ipv6NdResources[id] + if (id === ipArpResource.id) return ipArpResource + if (id === ospfRouterIdDependency.id) return ospfRouterIdDependency + if (id === ospfFilterSelectDependency.id) return ospfFilterSelectDependency + if (id === "ospf_prefix_bindings") return { id, path: "/routing/ospf/interface-template?.proplist=.id,prefix-list", title: "OSPF prefix-list bindings", permission: "mm:network", fields: z.object({}).strict(), observeFields: ["prefix-list"], create: false, remove: false, readOnly: true } + const interfaceDependency = interfaceDependencies.get(id) + if (interfaceDependency) return { id, path: interfaceDependency.path, title: id, permission: "mm:network", fields: z.object({}).strict(), observeFields: interfaceDependency.fields, create: false, remove: false, readOnly: true } + if (firewallAddressListResources.includes(id)) return { id, path: `/${id === "ip_address_list" ? "ip" : "ipv6"}/firewall/address-list`, title: "Firewall address-list dependency", permission: "mm:network", fields: z.object({}).strict(), create: false, remove: false, readOnly: true } + const resource = rosResources.find(item => item.id === id) + if (!resource) throw new McpError("UNSUPPORTED_CAPABILITY", "Неизвестный тип сетевого объекта", 400) + return resource +} +const interfaceDependencies = new Map([ + ["interface_list", { path: "/interface/list", fields: ["name", "include", "exclude"] }], + ["interface_list_member", { path: "/interface/list/member", fields: ["list", "interface", "disabled"] }], + ["interface_inventory", { path: "/interface", fields: ["name", "type", "disabled"] }], +]) +export async function routerClient(serverId: number) { + const [server] = await db.select().from(servers).where(eq(servers.id, serverId)).limit(1) + if (!server || !server.enabled) throw new McpError("DEVICE_UNAVAILABLE", "Сервер отключён или не найден", 409) + return { server, client: MikrotikClient.fromServer(server) } +} +/** Preserve rule/member order and the observed OSPF template listing order. + * Sorting templates by ID would hide a native reordering from source proofs. */ +export function isOrderedRosResource(resourceId: string): boolean { + return ["ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw", "routing_rule", "vrf", "bfd_configuration", "ospf_template"].includes(resourceId) +} +export function configurationRows(resource: RosResourceSpec, payload: unknown): ConfigurationRow[] { + const rows = Array.isArray(payload) ? payload : payload && typeof payload === "object" ? [payload] : null + if (!rows || rows.some(row => !row || typeof row !== "object" || Array.isArray(row))) throw new McpError("STATE_CONFLICT", "Недостоверная конфигурация RouterOS", 409) + const fields = Object.keys(resource.fields.shape) + // RouterOS creates/removes IPv6 link-local rows with interface runtime state. + // They cannot be configured independently and must not invalidate a sealed + // inverse plan when its own GRE is disabled. Explicit addresses remain sealed. + const configuration = rows.filter(row => !(resource.id === "ipv6_address" && [true, "true"].includes((row as ConfigurationRow).dynamic as boolean | string) && /^fe80:/i.test(String((row as ConfigurationRow).address))) + // These rows are generated from bridge/port PVID, not independently + // configurable VLAN entries. Their inputs remain sealed in bridge/ports. + && !(resource.id === "bridge_vlan" && [true, "true", "yes"].includes((row as ConfigurationRow).dynamic as boolean | string))) + const selectedRows = configuration.map(row => { + const selected = Object.fromEntries(Object.entries(row as ConfigurationRow).filter(([key]) => [".id", "dynamic", "builtin", "default", ...fields, ...(resource.observeFields ?? [])].includes(key))) + // RouterOS 7.24.5 returns an explicit empty fib flag for non-FIB tables. + // Normalize only this observed field; missing values must remain unknown. + if (resource.id === "routing_table" && selected.fib === "") selected.fib = "false" + if (resource.id === "vrf" && selected.interfaces !== undefined) selected.interfaces = canonicalVrfMembers(selected.interfaces) + if (Object.hasOwn(ipv6NdResources, resource.id)) normalizeIpv6NdRow(resource.id, selected) + if (resource.id === "ip_arp") { + if (typeof selected["mac-address"] === "string") selected["mac-address"] = selected["mac-address"].toLowerCase() + for (const key of ["dynamic", "dhcp", "disabled", "published"]) { + const value = selected[key] + const flag = typeof value === "boolean" ? value : typeof value === "string" ? observedBoolean(value) : null + if (flag !== null) selected[key] = String(flag) + } + // Reachability status and learned MAC addresses are not static configuration. + if (selected.status !== "permanent" || selected.dynamic !== undefined && selected.dhcp !== undefined) delete selected.status + if (selected.dynamic === "true" || selected.dhcp === "true") delete selected["mac-address"] + } + if (resource.id === "ospf_template") { + if (selected.networks !== undefined) { + const networks = ospfNetworks.safeParse(selected.networks) + if (networks.success) selected.networks = networks.data + } + for (const field of ospfTimerFields) { + const observed = selected[field] + if (typeof observed !== "string") continue + selected[field] = canonicalOspfTimer(observed) ?? observed + } + } + if (resource.id === "ospf_instance" && selected.redistribute !== undefined) { + const sources = ospfRedistribute.safeParse(selected.redistribute) + if (sources.success) selected.redistribute = sources.data + } + if (resource.id === "bfd_configuration" && selected.addresses !== undefined) { + const prefix = bfdAddressPrefix.safeParse(selected.addresses) + if (prefix.success) selected.addresses = prefix.data + } + if (resource.id === "bfd_configuration") for (const field of ["min-rx", "min-tx"]) { + const observed = selected[field] + if (typeof observed !== "string") continue + selected[field] = canonicalBfdInterval(observedDurationMs(observed)) ?? observed + } + if (["bridge_port", "bridge_vlan"].includes(resource.id) && selected.comment === undefined) selected.comment = "" + if (resource.id === "bridge_vlan") { + try { for (const field of ["vlan-ids", "tagged", "untagged"]) selected[field] = canonicalBridgeVlanValue(field, selected[field]) } + catch { throw new McpError("STATE_CONFLICT", "Недостоверная конфигурация VLAN membership", 409) } + } + return selected + }) + // Reordering an ordered catalog changes configuration even with unchanged rows. + return isOrderedRosResource(resource.id) ? selectedRows : selectedRows.sort((a, b) => String(a[".id"] ?? a.name ?? "").localeCompare(String(b[".id"] ?? b.name ?? ""))) +} +export async function readRosSnapshot(serverId: number, resourceId: string, beforeRead: () => Promise = async () => {}, expectedSourceProof?: string): Promise { + await beforeRead() + const policy = isRoutingPolicySnapshot(resourceId) + const resource = resourceById(policy ? resourceId.slice(routingPolicyPrefix.length) : resourceId), { client, server } = await routerClient(serverId) + const sourceProof = await rosSnapshotSourceProof(server) + if (expectedSourceProof !== undefined && expectedSourceProof !== sourceProof) throw new McpError("STATE_CONFLICT", "Источник сетевого снимка изменился до опроса", 409) + const checkSource = async () => { + await beforeRead() + if (await rosSnapshotSourceProof((await routerClient(serverId)).server) !== sourceProof) throw new McpError("STATE_CONFLICT", "Источник сетевого снимка изменился во время чтения", 409) + } + await checkSource() + const payload = resourceId === "ospf_template" ? await client.getOspfInterfaceTemplates(checkSource) : ["ospf_instance", "ospf_area", "bgp_instance", "bgp_connection"].includes(resourceId) ? await readOspfEffectiveDisabled(await client.get(resource.path), async id => { + await checkSource() + return client.post(resource.path + "/get", { ".id": id, "value-name": "disabled" }) + }) : await readRouterOsSnapshotGet({ client, path: resource.path, assertCurrent: checkSource }) + await checkSource() + if (resourceId === "ip_arp" && (!Array.isArray(payload) || payload.length > 10000 || Buffer.byteLength(JSON.stringify(payload)) > 1048576)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён ограниченный каталог ARP", 409) + if (Object.hasOwn(ipv6NdResources, resourceId) && (resourceId !== "ipv6_nd_settings" && (!Array.isArray(payload) || payload.length > 1000) || Buffer.byteLength(JSON.stringify(payload) ?? "") > 1048576)) throw new McpError("STATE_UNAVAILABLE", "Каталог IPv6 ND превышает предел или недоступен", 409) + if (interfaceDependencies.has(resourceId) && (!Array.isArray(payload) || payload.length > 1000 || Buffer.byteLength(JSON.stringify(payload)) > 1048576)) { + throw new McpError("STATE_UNAVAILABLE", "Каталог интерфейсов OSPF превышает предел или недоступен", 409) + } + const rows = policy ? await routingPolicyRows(resourceId, payload) : configurationRows(resource, payload) + if (resourceId === "ip_arp") { + const ids = rows.map(row => row[".id"]) + if (ids.some(id => typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id)) || new Set(ids).size !== ids.length) throw new McpError("STATE_UNAVAILABLE", "Недостоверные ID каталога ARP", 409) + } + if (resourceId === "ipv6_nd_settings") { + if (rows.length !== 1 || rows[0][".id"] !== undefined) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены глобальные настройки IPv6 ND", 409) + } else if (Object.hasOwn(ipv6NdResources, resourceId)) { + const ids = rows.map(row => row[".id"]) + if (ids.some(id => typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id)) || new Set(ids).size !== ids.length) throw new McpError("STATE_UNAVAILABLE", "Недостоверные ID каталога IPv6 ND", 409) + } + if (resourceId === "ospf_prefix_bindings") { + const ids = rows.map(row => row[".id"]) + if (!Array.isArray(payload) || rows.length > 1000 || ids.some(id => typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id)) || new Set(ids).size !== ids.length || + rows.some(row => row["prefix-list"] !== undefined && typeof row["prefix-list"] !== "string")) { + throw new McpError("STATE_UNAVAILABLE", "Не подтверждены привязки prefix-list OSPF", 409) + } + } + if (interfaceDependencies.has(resourceId)) { + const ids = rows.map(row => row[".id"]) + if (ids.some(id => typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id)) || new Set(ids).size !== ids.length) { + throw new McpError("STATE_UNAVAILABLE", "Недостоверные ID каталога интерфейсов OSPF", 409) + } + } + if (resourceId === ospfRouterIdDependency.id) { + if (!Array.isArray(payload) || rows.length > 10000 || Buffer.byteLength(JSON.stringify(payload)) > 1048576 || rows.some(row => typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"])) || new Set(rows.map(row => row[".id"])).size !== rows.length) { + throw new McpError("STATE_UNAVAILABLE", "Недостоверный каталог Router ID", 409) + } + let getters = 0 + for (const row of rows) for (const flag of ["disabled", "inactive"]) { + if (row[flag] !== undefined) continue + if (++getters > 128) throw new McpError("STATE_UNAVAILABLE", "Превышен бюджет чтения Router ID", 409) + await checkSource() + const response = await client.post<{ ret?: string }>(resource.path + "/get", { ".id": String(row[".id"]), "value-name": flag }) + const value = typeof response?.ret === "string" ? observedBoolean(response.ret) : null + if (value === null) throw new McpError("STATE_UNAVAILABLE", "Не подтверждено состояние Router ID", 409) + row[flag] = String(value) + } + await checkSource() + } + return { serverId, resource: resourceId, rows, fingerprint: fingerprint(rows), projectionVersion: rosSnapshotVersion(resourceId), sourceProof } +} +export async function rosSnapshotSourceProof(server: Awaited>["server"]): Promise { + return privateSecretFingerprint(fingerprint({ id: server.id, enabled: server.enabled, host: server.host, port: server.port, useSsl: server.useSsl, verifySsl: server.verifySsl, username: server.username, password: server.password, epoch: server.routingEndpointEpoch }), "mcp-ros-snapshot-source-v3") +} +export function validateRosStep(resource: RosResourceSpec, action: RosStep["action"], fields: unknown, id?: string) { + assertNoCallerCreateId({ action, id }) + if (isSingletonRosResource(resource.id) && id !== undefined) throw new McpError("INVALID_INPUT", "Настройки устройства не имеют RouterOS ID строки", 400) + if (resource.readOnly || action === "create" && !resource.create || action === "delete" && !resource.remove) throw new McpError("UNSUPPORTED_CAPABILITY", "Действие не поддерживается для объекта", 400) + if (action !== "create" && !isSingletonRosResource(resource.id) && !id) throw new McpError("INVALID_INPUT", "Требуется стабильный RouterOS ID", 400) + const parsed = resource.fields.parse(fields ?? {}) as Record + if (action === "create" && resource.requiredOnCreate?.some(field => parsed[field] === undefined)) throw new McpError("INVALID_INPUT", "Не заполнены обязательные поля создания", 400) + if (resource.id === "bfd_configuration" && action === "create") { + if (!parsed.interfaces && !parsed["address-list"]) throw new McpError("INVALID_INPUT", "Для правила BFD укажите интерфейсы или address-list", 400) + if (parsed.interfaces && parsed.addresses === undefined && String(parsed.interfaces).split(",").some(value => !value.trim() || /^(?:all|none)$/i.test(value.trim()))) { + throw new McpError("INVALID_INPUT", "Для правила BFD без адреса укажите конкретные интерфейсы", 400) + } + } + if (resource.id === "ospf_template" && typeof parsed.interfaces === "string" && !parsed.interfaces.trim()) throw new McpError("INVALID_INPUT", "Пустой список интерфейсов OSPF", 400) + if (resource.id === "ospf_template" && action !== "update" && parsed["prefix-list"] === "") throw new McpError("INVALID_INPUT", "Снятие prefix-list выполняется обновлением шаблона OSPF", 400) + if (resource.id === "ospf_template" && action === "create" && !parsed.networks && !String(parsed.interfaces ?? "").trim()) throw new McpError("INVALID_INPUT", "Укажите interfaces или networks для шаблона OSPF", 400) + if (action !== "delete" && !Object.keys(parsed).length) throw new McpError("INVALID_INPUT", "Нет параметров для изменения", 400) + return Object.fromEntries(Object.entries(parsed).map(([key, value]) => [key, resource.id === "vrf" && key === "interfaces" ? canonicalVrfMembers(value) : String(value)])) +} +/** An unset is a separate native command and must have its own intent/checkpoint. */ +export function splitOspfUnsetUpdate(step: RosStep): RosStep[] { + if (step.resource !== "ospf_template" || step.action !== "update") return [step] + const fields = { ...step.fields } + const steps: RosStep[] = [] + // Detach the v3-only prefix list before any change of OSPF area. + if (fields["prefix-list"] === "") { + steps.push({ ...step, fields: { "prefix-list": "" } }) + delete fields["prefix-list"] + } + const unsetPassive = fields.passive === "false" + if (unsetPassive) delete fields.passive + if (Object.keys(fields).length) steps.push({ ...step, fields }) + if (unsetPassive) steps.push({ ...step, fields: { passive: "false" } }) + return steps.length ? steps : [step] +} +export async function executeRosStep(step: RosStep): Promise { + assertNoCallerCreateId(step) + const resource = resourceById(step.resource) + if (!supportsRosPlacement(step)) throw new McpError("INVALID_INPUT", "Недопустимая позиция создания записи", 400) + // These resources require full private dependency proofs. Retired generic + // steps (including previously approved inverses) must not bypass the shared + // services by reaching the raw native dispatcher. + if (["ipsec_peer", "certificate"].includes(resource.id)) throw new McpError("UNSUPPORTED_CAPABILITY", "IPsec peer и сертификаты изменяются только через сервис с полным proof зависимостей; старый native шаг недоступен", 409) + if (resource.id === "vrf") throw new McpError("UNSUPPORTED_CAPABILITY", "VRF изменяется только через production companion executor; raw/legacy шаг недоступен", 409) + const singletonSettings = isSingletonRosResource(resource.id) + if (singletonSettings && (step.action !== "update" || step.id !== undefined)) throw new McpError("INVALID_INPUT", "Настройки устройства поддерживают только изменение без RouterOS ID строки", 400) + const { client, server } = await routerClient(step.serverId) + if (singletonSettings) return client.post(resource.path + "/set", step.fields) + if (resource.id === "wireguard" && step.action === "delete") { + const { deleteWireguardInterfaceGuarded } = await import("../../services/wireguard-interface-delete.js") + const { assertVerifiedConfigurationAccess } = await import("../../services/config-mutation-policy.js") + return deleteWireguardInterfaceGuarded(server, step.id!, undefined, assertVerifiedConfigurationAccess, { cascade: false, recordRevision: false }) + } + if (resource.id === "wireguard_peer" && step.action === "update") { + const { updateWireguardPeer, wireguardPeerNativeUpdate } = await import("../../services/wireguard-peer-update.js") + const { assertVerifiedConfigurationAccess } = await import("../../services/config-mutation-policy.js") + return updateWireguardPeer(server, step.id!, wireguardPeerNativeUpdate(step.fields), undefined, assertVerifiedConfigurationAccess, { recordRevision: false }) + } + if (resource.id === "wireguard_peer" && step.action === "delete") { + const { deleteWireguardPeer } = await import("../../services/wireguard-peer-delete.js") + const { assertVerifiedConfigurationAccess } = await import("../../services/config-mutation-policy.js") + return deleteWireguardPeer(server, step.id!, undefined, assertVerifiedConfigurationAccess, { recordRevision: false }) + } + if (resource.id === "ospf_template" && step.action === "update" && Object.keys(step.fields).length === 1 && step.fields.cost !== undefined && step.id) { + await client.setOspfInterfaceTemplateCost(step.id, Number(step.fields.cost), step.expectedCost, step.expectedServerProof) + return { cost: step.fields.cost, readBackVerified: true } + } + if(resource.id==="gre"&&(step.action==="delete"||step.action==="update"&&step.fields.name!==undefined)){ + const {mutateGreNativeTunnel,greNativeMutationSchema}=await import("../../services/gre-native-mutation.js") + const fields=Object.fromEntries(Object.entries(step.fields).map(([key,value])=>[key,key==="disabled"?["true","yes"].includes(value)?true:["false","no"].includes(value)?false:value:key==="mtu"&&value!=="auto"?Number(value):value])) + const mutation=greNativeMutationSchema.parse({operation:step.action,id:step.id,...(step.action==="update"?{fields}:{})}) + const receipt=await mutateGreNativeTunnel(server,mutation,undefined,{recordRevision:false}) + if(!receipt.verified)throw new McpError("VERIFICATION_FAILED","Native GRE изменение не подтверждено; повтор запрещён",409) + return receipt + } + if (resource.id === "container" && step.action === "delete") { + const { removeStoppedContainer } = await import("../../services/container-removal.js") + await removeStoppedContainer(client, step.id!) + return { deleted: true } + } + if (resource.id === "ospf_template" && step.action === "update") { + const field = step.fields["prefix-list"] === "" ? "prefix-list" : step.fields.passive === "false" ? "passive" : undefined + if (field) { + if (!step.id || Object.keys(step.fields).length !== 1) throw new McpError("INVALID_INPUT", "Снятие свойства OSPF требует отдельного шага плана", 400) + return client.post(resource.path + "/unset", { ".id": step.id, "value-name": field }) + } + } + if (step.action === "create") { + const fields = resource.id === "ospf_template" && step.fields.passive === "false" ? Object.fromEntries(Object.entries(step.fields).filter(([key]) => key !== "passive")) : step.fields + return client.put(resource.path, { ...fields, ...(step.placeBeforeId ? { "place-before": step.placeBeforeId } : {}) }) + } + const destination = step.id ? `${resource.path}/${encodeURIComponent(step.id)}` : resource.path + if (step.action === "delete") return client.delete(destination) + return client.patch(destination, step.fields) +} diff --git a/backend/src/modules/mcp/result-budget.test.ts b/backend/src/modules/mcp/result-budget.test.ts new file mode 100644 index 00000000..4e0e5203 --- /dev/null +++ b/backend/src/modules/mcp/result-budget.test.ts @@ -0,0 +1,122 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { CallToolResultSchema } from "@modelcontextprotocol/core" +import type { CallToolResult } from "@modelcontextprotocol/server" +import { boundedToolError, boundedToolResult, MCP_CALL_RESULT_BYTES, RESULT_BUDGET_META } from "./result-budget.js" + +const id = "00000000-0000-4000-8000-000000000001", plan = "00000000-0000-4000-8000-000000000002" +const projected = (result: CallToolResult) => (result.structuredContent as { result: Record }).result +function accepted(output: unknown, projection: "read" | "operation" | "plan" = "read", meta?: Record) { + const decision = boundedToolResult(output, projection, meta) + assert.equal(decision.kind, "result") + if (decision.kind !== "result") throw Error("Missing result") + assert.equal(decision.bytes, Buffer.byteLength(JSON.stringify(decision.result))) + assert(decision.bytes <= MCP_CALL_RESULT_BYTES) + assert(CallToolResultSchema.safeParse(decision.result).success) + return decision.result +} +test("normal result keeps SDK content/result shape and metadata", () => { + const output = { items: [{ name: "узел 🛰️", quality: "unknown" }], nextOffset: 1 } + const result = accepted(output, "read", { trace: "ok" }) + assert.deepEqual(result.structuredContent, { result: output }) + assert.deepEqual(result._meta, { trace: "ok" }) + assert.equal(result.content[0].type === "text" && result.content[0].text, JSON.stringify(output)) +}) +test("UTF8, double encoding and metadata each contribute to the full limit", () => { + for (const value of ["a".repeat(530000), "🛰️".repeat(80000), '\\"'.repeat(140000)]) { + assert(Buffer.byteLength(JSON.stringify({ value })) < MCP_CALL_RESULT_BYTES) + assert.equal(accepted({ value }).isError, true) + } + assert.equal(accepted({ items: [] }, "read", { trace: "m".repeat(MCP_CALL_RESULT_BYTES) }).isError, true) +}) +test("exact inclusive full result boundary, no arbitrary JSON truncation", () => { + const overhead = Buffer.byteLength(JSON.stringify({ content: [{ type: "text", text: JSON.stringify({ x: "" }) }], structuredContent: { result: { x: "" } } })) + const length = Math.floor((MCP_CALL_RESULT_BYTES - overhead) / 2) + const fit = accepted({ x: "a".repeat(length) }) + assert.equal(fit.isError, undefined) + assert.equal(accepted({ x: "a".repeat(length + 1) }).isError, true) +}) +test("oversized applied/running/unknown outcomes preserve actual identity and verification without counts", () => { + for (const state of ["applied", "running", "unknown", "partial", "failed", "cancelled"]) { + const output = { id, plan_id: plan, state, verification: "inconclusive", error_code: null, + result: { details: "z".repeat(MCP_CALL_RESULT_BYTES), confirmedWrites: 123, secret: "do not project" } } + const result = accepted(output, "operation") + assert.equal(result.isError, undefined) + assert.deepEqual(projected(result), { id, plan_id: plan, state, verification: "inconclusive", error_code: null }) + assert.deepEqual(result._meta?.[RESULT_BUDGET_META], { version: 1, detailsOmitted: true, reason: "byte-budget", automaticReplay: false }) + assert(!JSON.stringify(result).includes("confirmedWrites")) + assert.equal(output.state, state) + } +}) +test("non-JSON detail cannot overwrite the already applied receipt", () => { + for (const detail of [1n, undefined, { toJSON: () => { throw Error("private failure") } }]) { + const output = { id, plan_id: plan, state: "applied", verification: "passed", result: detail } + // Undefined property is JSON-compatible; BigInt/throwing toJSON require receipt. + const result = accepted(output, "operation") + assert.equal(result.isError, undefined) + assert.equal(projected(result).state, "applied") + } + const output: Record = { id, plan_id: plan, state: "applied", verification: "passed" } + output.result = output + assert.equal(projected(accepted(output, "operation")).state, "applied") +}) +test("compact receipt keeps a known pre-dispatch stop and its verified prefix", () => { + for (const [state, confirmedWrites] of [["rejected", 0], ["partial", 2]] as const) { + const result = { failedStepDispatched: false, confirmedWrites } + const output = { id, plan_id: plan, state, verification: "inconclusive", error_code: "STATE_CONFLICT" } + assert.deepEqual(projected(accepted({ ...output, result: { ...result, detail: 1n } }, "operation")), { ...output, result }) + for (const invalid of [{ ...result, confirmedWrites: -1 }, { ...result, failedStepDispatched: true }]) { + assert.deepEqual(projected(accepted({ ...output, result: { ...invalid, detail: 1n } }, "operation")), output) + } + } +}) +test("prepare omission keeps real plan/state, requests review, never invents executionAvailable", () => { + const result = accepted({ planId: plan, state: "approved", expiresAt: "2026-10-31T12:00:00Z", executionAvailable: true, changes: "x".repeat(MCP_CALL_RESULT_BYTES) }, "plan") + assert.equal(result.isError, undefined) + assert.deepEqual(projected(result), { planId: plan, state: "approved", expiresAt: "2026-10-31T12:00:00Z" }) + assert.equal((result._meta?.[RESULT_BUDGET_META] as Record).reviewRequired, true) + assert(!JSON.stringify(result).includes("executionAvailable")) +}) +test("malformed effect receipt is explicit integration stop, never fabricated failed/unknown", () => { + assert.deepEqual(boundedToolResult({ result: "x".repeat(MCP_CALL_RESULT_BYTES) }, "operation"), { kind: "receipt-required", reason: "byte-budget" }) + const output = { get id() { throw Error("must not read getter") }, plan_id: plan, state: "applied", verification: "passed" } + assert.deepEqual(boundedToolResult(output, "operation"), { kind: "receipt-required", reason: "not-json" }) +}) +test("read unserializable output is bounded error with no empty healthy success", () => { + for (const output of [undefined, 1n, { toJSON: () => { throw Error("secret") } }]) { + const result = accepted(output) + assert.equal(result.isError, true) + assert.equal(result.structuredContent, undefined) + assert(!JSON.stringify(result).includes("secret")) + } +}) +test("snapshot prevents post-check mutation/toJSON from enlarging wire result", () => { + let calls = 0 + const result = accepted({ toJSON: () => ({ value: ++calls === 1 ? "short" : "x".repeat(MCP_CALL_RESULT_BYTES) }) }) + JSON.stringify(result) + assert.equal(calls, 1) +}) +test("bounded existing error preserves NetFlow error code, no metadata/raw error leakage", () => { + const error = boundedToolError("RATE_LIMITED", "Ресурс чтения NetFlow занят".repeat(10000)) + assert(CallToolResultSchema.safeParse(error).success) + assert(Buffer.byteLength(JSON.stringify(error)) < MCP_CALL_RESULT_BYTES) + assert.equal(error.isError, true) + assert.equal(error.content[0].type === "text" && JSON.parse(error.content[0].text).code, "RATE_LIMITED") + const quality = { quality: "unknown", complete: false, losses: { received: 7, dropped: 2 }, nextOffset: null } + assert.deepEqual(accepted(quality).structuredContent, { result: quality }) +}) +test("aggregate small fields overflow even when no individual string exceeds16k", () => { + const result = accepted({ items: Array.from({ length: 500 }, () => ({ name: "a".repeat(1200) })) }) + assert.equal(result.isError, true) +}) +test("unserializable metadata preserves an actual passed operation with actual error code", () => { + const metadata: Record = {}; metadata.self = metadata + const result = accepted({ id, plan_id: plan, state: "applied", verification: "passed", error_code: "REAL_CODE" }, "operation", metadata) + assert.deepEqual(projected(result), { id, plan_id: plan, state: "applied", verification: "passed", error_code: "REAL_CODE" }) + assert.equal(result.isError, undefined) + assert.equal((result._meta?.[RESULT_BUDGET_META] as Record).reason, "not-json") +}) +test("hostile proxy cannot escape into the generic tool-error catch", () => { + const proxy = new Proxy({}, { getOwnPropertyDescriptor: () => { throw Error("private") }, get: () => { throw Error("private") } }) + assert.deepEqual(boundedToolResult(proxy, "operation"), { kind: "receipt-required", reason: "not-json" }) +}) diff --git a/backend/src/modules/mcp/result-budget.ts b/backend/src/modules/mcp/result-budget.ts new file mode 100644 index 00000000..da5448ea --- /dev/null +++ b/backend/src/modules/mcp/result-budget.ts @@ -0,0 +1,105 @@ +import type { CallToolResult } from "@modelcontextprotocol/server" + +/** B1 bounds CallToolResult, NOT JSON-RPC/SSE framing, listTools or HTTP bytes. */ +export const MCP_CALL_RESULT_BYTES = 1024 * 1024 +export const RESULT_BUDGET_META = "io.mikrotikmanager/result-budget" +export type ResultProjection = "read" | "operation" | "plan" | "cancel-plan" | "cancel-probe" +type Reason = "byte-budget" | "not-json" +export type BudgetDecision = + | { kind: "result"; result: CallToolResult; bytes: number } + | { kind: "receipt-required"; reason: Reason } + +/** No getters/toJSON: a compact receipt is captured before serializing details. */ +function data(value: unknown, key: string): unknown { + if (!value || typeof value !== "object") return undefined + const descriptor = Object.getOwnPropertyDescriptor(value, key) + return descriptor && "value" in descriptor ? descriptor.value : undefined +} +function short(value: unknown, max = 96): value is string { + return typeof value === "string" && value.length > 0 && value.length <= max +} +function uuid(value: unknown): value is string { + return typeof value === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value) +} +function project(output: unknown, projection: ResultProjection): Record | undefined { + if (projection === "read") return undefined + if (projection === "cancel-plan") return data(output, "cancelled") === true ? { cancelled: true } : undefined + if (projection === "cancel-probe") { + const operationId = data(output, "operationId"), cancelRequested = data(output, "cancelRequested"), terminal = data(output, "terminal") + if (!uuid(operationId) || typeof cancelRequested !== "boolean" || typeof terminal !== "boolean" || data(output, "automaticReplay") !== false) return undefined + return { operationId, cancelRequested, terminal, automaticReplay: false } + } + if (projection === "operation") { + const id = data(output, "id"), planId = data(output, "plan_id") + const state = data(output, "state"), verification = data(output, "verification") + if (!uuid(id) || !uuid(planId) || !short(state) || !short(verification)) return undefined + const receipt: Record = { id, plan_id: planId, state, verification } + const errorCode = data(output, "error_code") + if (errorCode === null || short(errorCode)) receipt.error_code = errorCode + const outcome = data(output, "result") + const confirmedWrites = data(outcome, "confirmedWrites") + if ((state === "partial" || state === "rejected") && data(outcome, "failedStepDispatched") === false && + typeof confirmedWrites === "number" && Number.isSafeInteger(confirmedWrites) && + (state === "partial" ? confirmedWrites > 0 : confirmedWrites === 0)) { + receipt.result = { failedStepDispatched: false, confirmedWrites } + } + return receipt + } + const planId = data(output, "planId"), state = data(output, "state") + if (!uuid(planId) || !short(state)) return undefined + const receipt: Record = { planId, state } + const expiresAt = data(output, "expiresAt") + if (short(expiresAt)) receipt.expiresAt = expiresAt + return receipt +} +function encoded(result: CallToolResult): { result: CallToolResult; bytes: number } { + // Count ALL result fields, including duplicated text/structuredContent and _meta. + return { result, bytes: Buffer.byteLength(JSON.stringify(result), "utf8") } +} +function success(output: unknown, text: string, meta?: Record): CallToolResult { + return { content: [{ type: "text", text }], structuredContent: { result: output }, ...(meta ? { _meta: meta } : {}) } +} +function omission(reason: Reason, projection: ResultProjection): Record { + return { [RESULT_BUDGET_META]: { version: 1, detailsOmitted: true, reason, + automaticReplay: false, ...(projection === "plan" ? { reviewRequired: true } : {}) } } +} + +/** + * Input must already be authorized/redacted. Classify from the registered handler, + * never from caller arguments or shape guesses. Unknown effectful handlers must NOT + * be classified read. `receipt-required` is an integration defect, not tool failure: + * an effect may already have happened; do not feed it to a generic error catch/retry. + */ +export function boundedToolResult(output: unknown, projection: ResultProjection, meta?: Record, readByteLimit = MCP_CALL_RESULT_BYTES): BudgetDecision { + const byteLimit = projection === "read" ? readByteLimit : MCP_CALL_RESULT_BYTES + let receipt: Record | undefined + try { receipt = project(output, projection) } catch { /* Hostile proxy is not a receipt. */ } + let reason: Reason = "not-json" + try { + const text = JSON.stringify(output) + if (text === undefined) throw new TypeError("No JSON value") + // Snapshot once: SDK cannot later invoke changing getters/toJSON a second time. + const snapshot: unknown = JSON.parse(text) + const metaText = meta === undefined ? undefined : JSON.stringify(meta) + if (meta !== undefined && metaText === undefined) throw new TypeError("No JSON metadata") + const metaSnapshot = metaText === undefined ? undefined : JSON.parse(metaText) as Record + const full = encoded(success(snapshot, text, metaSnapshot)) + if (full.bytes <= byteLimit) return { kind: "result", ...full } + reason = "byte-budget" + } catch { /* Only detail delivery failed; the original operation outcome did not. */ } + if (projection !== "read" && !receipt) return { kind: "receipt-required", reason } + if (receipt) { + const result = success(receipt, JSON.stringify(receipt), omission(reason, projection)) + return { kind: "result", ...encoded(result) } + } + const error = { code: reason === "byte-budget" ? "RESULT_TOO_LARGE" : "RESULT_NOT_JSON", + message: "Результат не выдан полностью. Уменьшите страницу, период или область запроса. Автоматически не повторяйте изменение." } + return { kind: "result", ...encoded({ isError: true, content: [{ type: "text", text: JSON.stringify(error) }], + _meta: omission(reason, projection) }) } +} + +/** Error delivery also has a bounded full result; never echo raw exceptions/meta. */ +export function boundedToolError(code: string, publicMessage: string): CallToolResult { + const safeCode = /^[A-Z0-9_]{1,96}$/.test(code) ? code : "INTERNAL_ERROR" + return { isError: true, content: [{ type: "text", text: JSON.stringify({ code: safeCode, message: publicMessage.slice(0, 1000) }) }] } +} diff --git a/backend/src/modules/mcp/result-delivery.test.ts b/backend/src/modules/mcp/result-delivery.test.ts new file mode 100644 index 00000000..f2673dd9 --- /dev/null +++ b/backend/src/modules/mcp/result-delivery.test.ts @@ -0,0 +1,58 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { CallToolResultSchema } from "@modelcontextprotocol/core" +import { boundedToolError, MCP_CALL_RESULT_BYTES, RESULT_BUDGET_META } from "./result-budget.js" +import { deliverRegisteredToolResult, registeredResultProjection, type RegisteredResultContract } from "./result-delivery.js" + +const id = "00000000-0000-4000-8000-000000000001", planId = "00000000-0000-4000-8000-000000000002" +const contract = (id: string, effect: RegisteredResultContract["effect"], hasRead = true, hasPrepare = false) => ({ id, effect, hasRead, hasPrepare }) +test("registered classification follows runtime read/prepare priority and controls", () => { + assert.equal(registeredResultProjection(contract("any.write", "write", false, true)), "plan") + assert.equal(registeredResultProjection(contract("any.diagnostic", "diagnostic", false, true)), "plan") + assert.equal(registeredResultProjection(contract("changes.apply", "write", true, true)), "operation") + assert.equal(registeredResultProjection(contract("changes.cancel", "read")), "cancel-plan") + assert.equal(registeredResultProjection(contract("servers.probes.cancel", "write")), "cancel-probe") + assert.equal(registeredResultProjection(contract("operations.read", "read")), "operation") +}) +test("unknown direct write/diagnostic is refused before invoking, output cannot spoof classification", async () => { + for (const effect of ["write", "diagnostic"] as const) { + let invoked = 0 + const result = await deliverRegisteredToolResult(contract("new.direct", effect), async () => { invoked++; return { id, state: "applied" } }, () => { throw Error("unexpected catch") }) + assert.equal(invoked, 0) + assert.equal(result.isError, true) + } +}) +test("prepare denied/no-plan uses actual invocation error and creates no fake plan receipt", async () => { + let caught = 0 + const result = await deliverRegisteredToolResult(contract("any.write", "write", false, true), async () => { throw Error("denied before insert") }, () => { caught++; return boundedToolError("PERMISSION_DENIED", "Отказ") }) + assert.equal(caught, 1); assert.equal(result.isError, true); assert.equal(result.structuredContent, undefined) +}) +test("async apply preserves pending snapshot, never claims finished native execution", async () => { + let caught = 0 + const result = await deliverRegisteredToolResult(contract("changes.apply", "write"), async () => ({ id, plan_id: planId, state: "pending", verification: "inconclusive", result: "x".repeat(MCP_CALL_RESULT_BYTES) }), () => { caught++; return boundedToolError("INTERNAL_ERROR", "failed") }) + assert.equal(caught, 0); assert.equal(result.isError, undefined) + assert.deepEqual(result.structuredContent, { result: { id, plan_id: planId, state: "pending", verification: "inconclusive" } }) +}) +test("cancel plan/probe details preserve source booleans without a fake operation outcome", async () => { + for (const [registered, output, receipt] of [ + [contract("changes.cancel", "read"), { cancelled: true, details: 1n }, { cancelled: true }], + [contract("servers.probes.cancel", "write"), { operationId: id, cancelRequested: false, terminal: true, automaticReplay: false, details: 1n }, { operationId: id, cancelRequested: false, terminal: true, automaticReplay: false }], + ] as const) { + const result = await deliverRegisteredToolResult(registered, async () => output, () => { throw Error("No delivery error may reach invocation catch") }) + assert.equal(result.isError, undefined); assert.deepEqual(result.structuredContent, { result: receipt }) + assert(CallToolResultSchema.safeParse(result).success) + } +}) +test("invariant malformed effect receipt does not claim failed/unknown and never enters invocation error", async () => { + const result = await deliverRegisteredToolResult(contract("changes.apply", "write"), async () => ({ bogus: 1n }), () => { throw Error("must not reach generic error catch") }) + assert.equal(result.isError, undefined); assert.equal(result.structuredContent, undefined) + assert.equal((result._meta?.[RESULT_BUDGET_META] as Record).receiptUnavailable, true) + assert(!JSON.stringify(result).includes('"state"')) +}) +test("NetFlow owner lifecycle/legacy payload check/retained byte accounting stay composed", async () => { + const events: string[] = [], quality = { quality: "unknown", complete: false, received: 10, dropped: 2 } + const flowOwner = { run: async (body: () => Promise) => { events.push("owner:enter"); try { return await body() } finally { events.push("owner:exit") } }, retainOutput: (bytes: number) => { assert(bytes > 0); events.push("retain") } } + const result = await flowOwner.run(() => deliverRegisteredToolResult(contract("traffic.flow.catalog.read", "read"), async () => { events.push("invoke"); const output = quality; assert(Buffer.byteLength(JSON.stringify(output)) <= MCP_CALL_RESULT_BYTES); events.push("payload-check"); return output }, () => { throw Error("unexpected") }, bytes => flowOwner.retainOutput(bytes + 1024))) + assert.deepEqual((result as { structuredContent: unknown }).structuredContent, { result: quality }) + assert.deepEqual(events, ["owner:enter", "invoke", "payload-check", "retain", "owner:exit"]) +}) diff --git a/backend/src/modules/mcp/result-delivery.ts b/backend/src/modules/mcp/result-delivery.ts new file mode 100644 index 00000000..b9def95a --- /dev/null +++ b/backend/src/modules/mcp/result-delivery.ts @@ -0,0 +1,51 @@ +import type { CallToolResult } from "@modelcontextprotocol/server" +import { boundedToolError, boundedToolResult, MCP_CALL_RESULT_BYTES, RESULT_BUDGET_META, type ResultProjection } from "./result-budget.js" + +/** Constructed only from a registered action, never from request arguments. */ +export interface RegisteredResultContract { + id: string + effect: "read" | "write" | "diagnostic" + hasRead: boolean + hasPrepare: boolean +} +export function registeredResultProjection(contract: RegisteredResultContract): ResultProjection | undefined { + // Match runtime ordering: a read closure wins over prepare if both exist. + if (!contract.hasRead && contract.hasPrepare) return "plan" + if (!contract.hasRead) return undefined + if (contract.id === "changes.apply") return contract.effect === "write" ? "operation" : undefined + if (contract.id === "operations.read") return contract.effect === "read" ? "operation" : undefined + if (contract.id === "changes.cancel") return contract.effect === "read" ? "cancel-plan" : undefined + if (contract.id === "servers.probes.cancel") return contract.effect === "write" ? "cancel-probe" : undefined + return contract.effect === "read" ? "read" : undefined +} + +/** + * Only invocation errors enter invocationError. Delivery is outside that catch: + * serialization/size failure cannot turn a known applied result into a failed call. + * The impossible-for-current-source receipt-required branch is a delivery-only + * invariant notice, with no invented execution state, isError, counts or retry. + */ +export async function deliverRegisteredToolResult(contract: RegisteredResultContract, invoke: () => Promise, + invocationError: (error: unknown) => CallToolResult, onBytes?: (bytes: number) => void, readByteLimit?: number): Promise { + const projection = registeredResultProjection(contract) + if (!projection) return boundedToolError("UNSUPPORTED_CAPABILITY", "Для действия не определена безопасная форма квитанции; выполнение не начато") + // Only a trusted read may have a smaller delivery allowance. Fail before invoke. + if (readByteLimit !== undefined && (projection !== "read" || !Number.isSafeInteger(readByteLimit) || readByteLimit < 8192 || readByteLimit > MCP_CALL_RESULT_BYTES)) { + return boundedToolError("UNSUPPORTED_CAPABILITY", "Допуск размера ответа чтения не подтверждён; выполнение не начато") + } + let output: unknown + try { output = await invoke() } catch (error) { + const result = invocationError(error) + onBytes?.(Buffer.byteLength(JSON.stringify(result))) + return result + } + const decision = boundedToolResult(output, projection, undefined, readByteLimit) + const result: CallToolResult = decision.kind === "result" ? decision.result : { + content: [{ type: "text", text: "Квитанция не доставлена из-за нарушения внутреннего формата. Это не новый исход операции. Не повторяйте изменение; проверьте существующую операцию в приложении." }], + _meta: { [RESULT_BUDGET_META]: { version: 1, detailsOmitted: true, reason: decision.reason, automaticReplay: false, receiptUnavailable: true } }, + } + // Resource accounting callback is intentionally outside invocationError too. + // Existing response owner can reject a read; it cannot rewrite an effectful call. + onBytes?.(Buffer.byteLength(JSON.stringify(result))) + return result +} diff --git a/backend/src/modules/mcp/ros-prefix.test.ts b/backend/src/modules/mcp/ros-prefix.test.ts new file mode 100644 index 00000000..f7599280 --- /dev/null +++ b/backend/src/modules/mcp/ros-prefix.test.ts @@ -0,0 +1,52 @@ +import assert from "node:assert/strict" +import { createRosPrefix } from "./ros-prefix.js" +import { fingerprint, McpError } from "./security.js" +import { rosSnapshotVersion, type RosSnapshot, type RosStep, type ConfigurationRow } from "./resources.js" + +const snapshot = (resource: string, rows: ConfigurationRow[], sourceProof = "a".repeat(64)): RosSnapshot => ({ serverId: 1, resource, rows, fingerprint: fingerprint(rows), projectionVersion: rosSnapshotVersion(resource), sourceProof }) +const old = snapshot("ip_filter", [{ ".id": "*a", chain: "forward", action: "accept", comment: "old" }, { ".id": "*b", chain: "forward", action: "drop", comment: "keep" }]) +const dependency = snapshot("routing_table", [{ ".id": "*main", name: "main", fib: "true" }]) +const changed = snapshot("ip_filter", [{ ...old.rows[0], comment: "new" }, old.rows[1]]) +const step: RosStep = { serverId: 1, resource: "ip_filter", action: "update", id: "*a", fields: { comment: "new" } } +const actual = new Map([[old.resource, old], [dependency.resource, dependency]]) +const read = async (_serverId: number, resource: string, beforeRead: () => Promise = async () => {}) => { await beforeRead(); return structuredClone(actual.get(resource)!) } +const isConflict = (error: unknown) => error instanceof McpError && error.code === "STATE_CONFLICT" +const isFailed = (error: unknown) => error instanceof McpError && error.code === "VERIFICATION_FAILED" + +for (const invalid of [{ ...old, sourceProof: undefined }, { ...old, projectionVersion: 1 }, { ...old, projectionVersion: 2 }, { ...old, fingerprint: "forged" }]) assert.throws(() => createRosPrefix([invalid]), error => error instanceof McpError && error.code === "PLAN_EXPIRED") +assert.throws(() => createRosPrefix([old, changed]), isConflict) +assert.throws(() => createRosPrefix([{ ...dependency, projectionVersion: 3 }]), { code: "PLAN_EXPIRED" }) +const ospf = snapshot("ospf_template", [{ ".id": "*a", "prefix-list": "announced", cost: "80" }]) +assert.throws(() => createRosPrefix([{ ...ospf, projectionVersion: 8 }]), { code: "PLAN_EXPIRED" }) +for (const [resource, fields] of [["ip_settings", { "rp-filter": "loose" }], ["ipv6_settings", { "multipath-hash-policy": "l4" }]] as const) { + const current = snapshot(resource, [fields]) + assert.doesNotThrow(() => createRosPrefix([current])) + assert.throws(() => createRosPrefix([{ ...current, projectionVersion: 3 }]), { code: "PLAN_EXPIRED" }) +} +const prefix = createRosPrefix([old, dependency, structuredClone(old)], read) +await prefix.check() +actual.set(old.resource, changed) +await prefix.advance(step, {}, changed) +assert.equal(prefix.before(step).rows[0].comment, "new") +await prefix.check() +actual.set(old.resource, old) +await assert.rejects(prefix.check(), isConflict) +actual.set(old.resource, changed) +actual.set(dependency.resource, { ...dependency, sourceProof: "b".repeat(64) }) +await assert.rejects(prefix.check(), isConflict) + +actual.set(old.resource, old); actual.set(dependency.resource, dependency) +const unadvanced = createRosPrefix([old, dependency], read) +const collateral = snapshot("ip_filter", [changed.rows[0], { ...old.rows[1], action: "accept" }]) +actual.set(old.resource, collateral) +await assert.rejects(unadvanced.advance(step, {}, collateral), isFailed) +assert.equal(unadvanced.before(step).rows[0].comment, "old") +actual.set(old.resource, changed) +actual.set(dependency.resource, snapshot("routing_table", [{ ".id": "*main", name: "renamed", fib: "true" }])) +await assert.rejects(unadvanced.advance(step, {}, changed), isFailed) +assert.equal(unadvanced.before(step).rows[0].comment, "old", "A failed dependency check cannot advance the expected prefix") +actual.set(old.resource, old); actual.set(dependency.resource, dependency) +await unadvanced.check() +await assert.rejects(unadvanced.check(async () => { throw new McpError("PERMISSION_DENIED", "revoked") }), error => error instanceof McpError && error.code === "PERMISSION_DENIED") +assert.equal(unadvanced.before(step).rows[0].comment, "old") +console.log("ROS prefix: version/source/fingerprint and duplicate-baseline guards, verified advancement, inter-step drift/source changes, failed conservation/dependency do not advance, and live revocation") diff --git a/backend/src/modules/mcp/ros-prefix.ts b/backend/src/modules/mcp/ros-prefix.ts new file mode 100644 index 00000000..75e769d6 --- /dev/null +++ b/backend/src/modules/mcp/ros-prefix.ts @@ -0,0 +1,51 @@ +import { fingerprint, McpError } from "./security.js" +import { isSingletonRosResource, readRosSnapshot, rosSnapshotVersion, type RosSnapshot, type RosStep } from "./resources.js" +import { verifyRosStepSnapshots } from "./ros-step-verification.js" +import { acknowledgedCreateId } from "./native-create-identity.js" + +const key = (snapshot: Pick) => `${snapshot.serverId}:${snapshot.resource}` +function valid(snapshot: RosSnapshot): boolean { + if (snapshot.projectionVersion !== rosSnapshotVersion(snapshot.resource) || typeof snapshot.sourceProof !== "string" || !/^[a-f0-9]{64}$/.test(snapshot.sourceProof) || !Array.isArray(snapshot.rows) || snapshot.fingerprint !== fingerprint(snapshot.rows)) return false + if (isSingletonRosResource(snapshot.resource)) return snapshot.rows.length === 1 && !!snapshot.rows[0] && typeof snapshot.rows[0] === "object" && !Array.isArray(snapshot.rows[0]) + const ids = snapshot.rows.map(acknowledgedCreateId) + return ids.every(id => id !== undefined) && new Set(ids).size === ids.length +} +function matches(expected: RosSnapshot, current: RosSnapshot): boolean { + return valid(current) && key(expected) === key(current) && expected.sourceProof === current.sourceProof && expected.fingerprint === current.fingerprint +} + +/** The baseline advances only after a complete, acknowledged projected change. */ +export function createRosPrefix(snapshots: RosSnapshot[], read: typeof readRosSnapshot = readRosSnapshot) { + let expected = new Map() + for (const snapshot of snapshots) { + if (!valid(snapshot)) throw new McpError("PLAN_EXPIRED", "Сетевой снимок имеет устаревший контракт; подготовьте новый план", 409) + const previous = expected.get(key(snapshot)) + if (previous && !matches(previous, snapshot)) throw new McpError("STATE_CONFLICT", "Неоднозначный исходный сетевой снимок", 409) + expected.set(key(snapshot), structuredClone(snapshot)) + } + const checkMap = async (map: Map, beforeRead: () => Promise, code: string) => { + for (const snapshot of map.values()) { + const current = await read(snapshot.serverId, snapshot.resource, beforeRead, snapshot.sourceProof) + if (!matches(snapshot, current)) throw new McpError(code, "Сетевой источник или ожидаемая конфигурация изменились; повтор записи запрещён", 409) + } + await beforeRead() + } + return { + async check(beforeRead: () => Promise = async () => {}) { + await checkMap(expected, beforeRead, "STATE_CONFLICT") + }, + before(step: RosStep): RosSnapshot { + const snapshot = expected.get(key(step)) + if (!snapshot) throw new McpError("STATE_CONFLICT", "Нет исходного снимка сетевого шага", 409) + return structuredClone(snapshot) + }, + async advance(step: RosStep, result: unknown, after: RosSnapshot, beforeRead: () => Promise = async () => {}) { + const before = expected.get(key(step)) + if (!before || !valid(after) || before.sourceProof !== after.sourceProof || !verifyRosStepSnapshots(step, result, before, after)) throw new McpError("VERIFICATION_FAILED", "Изменение сетевого каталога не совпало с подтверждённым шагом", 409) + const next = new Map(expected) + next.set(key(after), structuredClone(after)) + await checkMap(next, beforeRead, "VERIFICATION_FAILED") + expected = next + }, + } +} diff --git a/backend/src/modules/mcp/ros-step-verification.test.ts b/backend/src/modules/mcp/ros-step-verification.test.ts new file mode 100644 index 00000000..a380d31d --- /dev/null +++ b/backend/src/modules/mcp/ros-step-verification.test.ts @@ -0,0 +1,226 @@ +import assert from "node:assert/strict" +import { configurationRows, isOrderedRosResource, resourceById, rosSnapshotVersion, splitOspfUnsetUpdate, type ConfigurationRow, type RosSnapshot, type RosStep } from "./resources.js" +import { fingerprint } from "./security.js" +import { proveRosDriftExpectation, verifyRosStepSnapshots } from "./ros-step-verification.js" +import { readOspfTemplateFlags } from "../../services/ospf-template-flags.js" + +function snapshot(resource: string, rows: ConfigurationRow[]): RosSnapshot { + return { serverId: 1, resource, rows, fingerprint: fingerprint(rows) } +} +function check(step: RosStep, result: unknown, before: ConfigurationRow[], after: ConfigurationRow[]): boolean { + return verifyRosStepSnapshots(step, result, snapshot(step.resource, before), snapshot(step.resource, after)) +} +const create: RosStep = { serverId: 1, resource: "ip_filter", action: "create", fields: { chain: "forward", action: "accept" } } +const a = { ".id": "*A", chain: "forward", action: "drop", comment: "keep", disabled: "false" } +const b = { ...a, ".id": "*B", chain: "input" } +const c = { ".id": "*C", chain: "forward", action: "accept", disabled: "no" } +const ack = { ".id": "*C" } +assert.equal(check(create, ack, [a, b], [a, b, c]), true) +assert.equal(check(create, { ".id": "*A" }, [a, b], [a, b, c]), false, "Existing assigned ID cannot acknowledge a new object") +assert.equal(check(create, ack, [a, b], [a, b, c, { ...c, ".id": "*D" }]), false, "Two added rows are not one CREATE") +assert.equal(check(create, ack, [a, b], [a, c]), false, "Replacement/removal cannot hide behind matching cardinality") +assert.equal(check(create, ack, [a, b], [{ ...a, comment: "collateral" }, b, c]), false) +assert.equal(check(create, ack, [a, b], [a, b, { ...c, action: "drop" }]), false) +assert.equal(check(create, undefined, [a], [a, c]), false, "Observed object never substitutes a lost acknowledgement") +assert.equal(check({ ...create, id: "*A" }, ack, [a], [a, c]), false) + +const update: RosStep = { ...create, action: "update", id: "*A", fields: { action: "accept" } } +const updated = { ...a, action: "accept" } +assert.equal(check(update, {}, [a, b], [updated, b]), true) +assert.equal(check(update, {}, [a, b], [{ ...updated, comment: "collateral" }, b]), false) +assert.equal(check(update, {}, [a, b], [updated, { ...b, action: "accept" }]), false) +assert.equal(check(update, {}, [a, b], [updated]), false) +assert.equal(check(update, {}, [a, b], [updated, b, c]), false) +assert.equal(check(update, {}, [a, b], [{ ...updated, ".id": "*X" }, b]), false) +assert.equal(check(update, {}, [a, b], [{ ...updated, comment: undefined }, b]), false, "Absent/unknown unsupplied properties cannot replace a known value") +assert.equal(check({ ...update, fields: { invented: "field" } }, {}, [a], [{ ...a, invented: "field" }]), false) +const remove: RosStep = { ...update, action: "delete", fields: {} } +assert.equal(check(remove, undefined, [a, b], [b]), true) +assert.equal(check(remove, undefined, [a, b], []), false) +assert.equal(check(remove, undefined, [a, b], [{ ...b, comment: "collateral" }]), false) +assert.equal(check(remove, undefined, [b], [b]), false, "Deleting an already absent ID does not prove the step") + +for (const rows of [[a, a], [{ ...a, ".id": undefined }], [{ ...a, ".id": "" }], [{ ...a, ".id": 4 }], [{ ...a, ".id": " *A" }]]) { + assert.equal(check(create, ack, rows, [...rows, c]), false) + assert.equal(check(create, ack, [], rows), false) +} +assert.equal(check(create, ack, [a], [a, c, c]), false, "Duplicate IDs after a mutation are unknown") + +const ordered = ["ip_filter", "ip_nat", "ip_mangle", "ip_raw", "ipv6_filter", "ipv6_nat", "ipv6_mangle", "ipv6_raw", "routing_rule", "vrf", "bfd_configuration", "ospf_template"] +for (const resource of ordered) { + assert.equal(isOrderedRosResource(resource), true) + const rows = [{ ".id": "*Z", comment: "first" }, { ".id": "*A", comment: "second" }] + assert.deepEqual(configurationRows(resourceById(resource), rows).map(row => row[".id"]), ["*Z", "*A"], `${resource} preserves the native order`) + const step = { ...update, resource, id: "*Z", fields: { comment: "changed" } } + assert.equal(check(step, {}, rows, [{ ...rows[0], comment: "changed" }, rows[1]]), true) + assert.equal(check(step, {}, rows, [rows[1], { ...rows[0], comment: "changed" }]), false, `${resource} rejects reordering`) +} +assert.equal(check(create, ack, [a, b], [a, c, b]), false, "Ordered CREATE must append without place-before") +assert.equal(check(remove, {}, [a, b, c], [c, b]), false, "DELETE must retain survivor order") +const vrf: RosStep = { ...create, resource: "vrf", fields: { name: "new", interfaces: "ether3" }, placeBeforeId: "*B" } +const va = { ".id": "*A", name: "first", interfaces: "ether1" }, vb = { ".id": "*B", name: "main", interfaces: "all" }, vc = { ".id": "*C", name: "new", interfaces: "ether3" } +assert.equal(check(vrf, ack, [va, vb], [va, vc, vb]), true) +assert.equal(check(vrf, ack, [va, vb], [vc, va, vb]), false, "place-before is exact insertion, not any earlier position") +assert.equal(check(vrf, ack, [va, vb], [va, vb, vc]), false) +assert.equal(check({ ...vrf, placeBeforeId: "*MISSING" }, ack, [va, vb], [va, vc, vb]), false) +assert.equal(check({ ...create, placeBeforeId: "*B" }, ack, [a, b], [a, c, b]), false, "Firewall creation does not accept place-before") +const bfd: RosStep = { ...create, resource: "bfd_configuration", fields: { interfaces: "ether1", addresses: "192.0.2.5/32", vrf: "main" }, placeBeforeId: "*B" } +const insertedBfd = { ".id": "*C", ...bfd.fields } +assert.equal(check(bfd, ack, [a, b], [a, insertedBfd, b]), true) +assert.equal(check(bfd, ack, [a, b], [a, b, insertedBfd]), false) +assert.equal(check(bfd, ack, [a, b], [a, insertedBfd, { ...b, comment: "changed" }]), false) +assert.equal(check({ ...bfd, placeBeforeId: "*D" }, ack, [a, b], [a, insertedBfd, b]), false) +const bfdRows = (rows: ConfigurationRow[]) => configurationRows(resourceById("bfd_configuration"), rows) +const interfaceBfd: RosStep = { ...bfd, fields: { interfaces: "ether1", vrf: "main" } } +assert.equal(check(interfaceBfd, ack, [a, b], [a, { ".id": "*C", ...interfaceBfd.fields }, b]), true) +assert.equal(check(interfaceBfd, ack, [a, b], bfdRows([a, { ".id": "*C", ...interfaceBfd.fields, "address-list": "unexpected" }, b])), false) +const bfdIntervalUpdate: RosStep = { ...bfd, action: "update", id: "*A", placeBeforeId: undefined, fields: { "min-rx": "200ms", "min-tx": "1s" } } +const originalBfd = { ...a, "min-rx": "500ms", "min-tx": "2s" } +const formattedBfd = { ...a, "min-rx": "00:00:00.200", "min-tx": "1000ms" } +assert.equal(check(bfdIntervalUpdate, {}, bfdRows([originalBfd]), bfdRows([formattedBfd])), true) +assert.equal(check(bfdIntervalUpdate, {}, bfdRows([originalBfd]), bfdRows([{ ...formattedBfd, "min-rx": "200001us" }])), false) +assert.equal(check(bfd, ack, bfdRows([a, { ...b, "address-list": "previous" }]), bfdRows([a, insertedBfd, { ...b, "address-list": "changed" }])), false) +const unordered = { ...update, resource: "bridge", fields: { name: "renamed" } } +const ua = { ".id": "*A", name: "one" }, ub = { ".id": "*B", name: "two" } +assert.equal(isOrderedRosResource("bridge"), false) +assert.equal(check(unordered, {}, [ua, ub], [ub, { ...ua, name: "renamed" }]), true) +assert.deepEqual(configurationRows(resourceById("bridge"), [ub, ua]).map(row => row[".id"]), ["*A", "*B"]) + +const booleanStep = { ...update, fields: { disabled: "false" } } +for (const value of [false, "false", "no"]) assert.equal(check(booleanStep, {}, [a, b], [{ ...a, disabled: value }, { ...b, disabled: false }]), true) +for (const value of [undefined, "", 0, null, "unknown", true]) assert.equal(check(booleanStep, {}, [a], [{ ...a, disabled: value }]), false) +assert.equal(check(update, {}, [{ ...a, comment: "yes" }], [{ ...updated, comment: true }]), false, "Boolean normalization applies only to schema boolean fields") +assert.equal(check(update, {}, [{ ...a, dynamic: "yes" }], [{ ...updated, dynamic: true }]), false, "Native metadata has no boolean schema to authorize equivalence") + +const settings: RosStep = { serverId: 1, resource: "ip_settings", action: "update", fields: { "rp-filter": "strict" } } +const s0 = { "rp-filter": "loose", "ipv4-multipath-hash-policy": "l3" }, s1 = { ...s0, "rp-filter": "strict" } +assert.equal(check(settings, {}, [s0], [s1]), true) +assert.equal(check(settings, {}, [], [s1]), false) +assert.equal(check(settings, {}, [s0, s0], [s1]), false) +assert.equal(check(settings, {}, [s0], [s1, s1]), false) +assert.equal(check({ ...settings, id: "singleton" }, {}, [s0], [s1]), false) +assert.equal(check(settings, {}, [s0], [{ ...s1, "ipv4-multipath-hash-policy": "l4" }]), false) +assert.equal(check(settings, {}, [s0], [{ ...s1, ".id": "fabricated" }]), false) +assert.equal(check({ ...settings, resource: "ipv6_settings", fields: { "multipath-hash-policy": "l4" } }, {}, [{ "multipath-hash-policy": "l3" }], [{ "multipath-hash-policy": "l4" }]), true) + +const ospf: RosStep = { serverId: 1, resource: "ospf_template", action: "update", id: "*A", fields: { passive: "false" } } +const redistribution: RosStep = { serverId: 1, resource: "ospf_instance", action: "update", id: "*I", fields: { redistribute: "connected,static", "originate-default": "if-installed" } } +const instanceRows = (row: ConfigurationRow) => configurationRows(resourceById("ospf_instance"), [row]) +const oldInstance = { ".id": "*I", redistribute: "", "originate-default": "never", "out-filter-chain": "ospf-out" } +const newInstance = { ...oldInstance, redistribute: "static,connected", "originate-default": "if-installed" } +assert.equal(check(redistribution, {}, instanceRows(oldInstance), instanceRows(newInstance)), true) +assert.equal(check(redistribution, {}, instanceRows(oldInstance), instanceRows({ ...newInstance, redistribute: "static" })), false) +assert.equal(check(redistribution, {}, instanceRows(oldInstance), instanceRows({ ...newInstance, "out-filter-chain": "other" })), false) +const networkTemplate: RosStep = { serverId: 1, resource: "ospf_template", action: "create", fields: { area: "backbone", networks: "fd00::/64" } } +const networkRows = (row: ConfigurationRow) => configurationRows(resourceById("ospf_template"), [row]) +const networkRow = { ".id": "*N", area: "backbone", networks: "FD00:0:0:0::/64" } +assert.equal(check(networkTemplate, { ".id": "*N" }, [], networkRows(networkRow)), true) +assert.equal(check(networkTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, "instance-id": "0" })), true) +assert.equal(check(networkTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, "instance-id": "7" })), false) +const instanceIdUpdate: RosStep = { ...ospf, id: "*N", fields: { "instance-id": "7" } } +const withDefaultId = networkRows({ ...networkRow, "instance-id": "0" }) +assert.equal(check(instanceIdUpdate, {}, withDefaultId, networkRows({ ...networkRow, "instance-id": "7" })), true) +assert.equal(check(instanceIdUpdate, {}, withDefaultId, networkRows(networkRow)), false) +assert.equal(check(networkTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, networks: "fd00::/48" })), false) +assert.equal(check(networkTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, interfaces: "ether1" })), false) +assert.equal(check(networkTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, "prefix-list": "unexpected" })), false) +const prefixTemplate = { ...networkTemplate, fields: { ...networkTemplate.fields, "prefix-list": "advertise" } } +assert.equal(check(prefixTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, "prefix-list": "advertise" })), true) +const detachPrefix: RosStep = { ...ospf, id: "*N", fields: { "prefix-list": "" } } +const attached = networkRows({ ...networkRow, "prefix-list": "advertise" }) +assert.equal(check(detachPrefix, [], attached, networkRows(networkRow)), true) +assert.equal(check(detachPrefix, [], attached, networkRows({ ...networkRow, "prefix-list": "" })), true) +assert.equal(check(detachPrefix, [], attached, networkRows({ ...networkRow, "prefix-list": null })), false) +assert.equal(check(detachPrefix, [], attached, attached), false) +assert.equal(check(detachPrefix, [], attached, networkRows({ ...networkRow, area: "foreign" })), false) +assert.deepEqual(splitOspfUnsetUpdate({ ...detachPrefix, fields: { area: "other", "prefix-list": "", passive: "false" } }).map(step => step.fields), + [{ "prefix-list": "" }, { area: "other" }, { passive: "false" }]) +assert.equal(check(networkTemplate, {}, [], networkRows(networkRow)), false) +const nbmaTemplate: RosStep = { ...networkTemplate, fields: { ...networkTemplate.fields, type: "nbma", priority: "0" } } +assert.equal(check(nbmaTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, type: "nbma", priority: "0" })), true) +assert.equal(check(nbmaTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, type: "nbma", priority: "128" })), false) +const priorityUpdate: RosStep = { ...ospf, id: "*N", fields: { type: "ptmp-broadcast", priority: "255" } } +const prior = networkRows({ ...networkRow, type: "nbma", priority: "0" }) +assert.equal(check(priorityUpdate, {}, prior, networkRows({ ...networkRow, type: "ptmp-broadcast", priority: "255" })), true) +assert.equal(check(priorityUpdate, {}, prior, networkRows({ ...networkRow, type: "nbma", priority: "255" })), false) +const timers = { "hello-interval": "10s", "dead-interval": "60s" } +const timedTemplate = { ...networkTemplate, fields: { ...networkTemplate.fields, ...timers } } +const clockTimers = networkRows({ ...networkRow, "hello-interval": "00:00:10", "dead-interval": "1m" }) +assert.deepEqual(clockTimers, networkRows({ ...networkRow, ...timers })) +assert.equal(check(timedTemplate, { ".id": "*N" }, [], clockTimers), true) +assert.equal(check(timedTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, "hello-interval": "10001ms", "dead-interval": "1m" })), false) +assert.equal(check(timedTemplate, { ".id": "*N" }, [], networkRows({ ...networkRow, ...timers, "dead-interval": "60" })), false) +const linkStateTimers: RosStep = { ...ospf, id: "*N", fields: { "retransmit-interval": "5s", "transmit-delay": "1s" } } +const linkStateBefore = networkRows({ ...networkRow, "retransmit-interval": "6s", "transmit-delay": "2s" }) +assert.equal(check(linkStateTimers, {}, linkStateBefore, networkRows({ ...networkRow, "retransmit-interval": "00:00:05", "transmit-delay": "1000ms" })), true) +assert.equal(check(linkStateTimers, {}, linkStateBefore, networkRows({ ...networkRow, "retransmit-interval": "5s", "transmit-delay": "1001ms" })), false) +const os = { ".id": "*A", passive: "true", disabled: "false", cost: "10" } +assert.equal(check(ospf, {}, [os], [{ ...os, passive: "false" }]), true) +const { passive: _passive, ...unset } = os +assert.equal(check(ospf, {}, [os], [unset]), false, "An omitted switch alone never proves false") +const effective = await readOspfTemplateFlags([unset], async () => ({ ret: "" }), undefined, async () => ({ present: [], absent: [{ ".id": "*A" }] })) +assert.equal(check(ospf, {}, [os], effective), true, "Existing complete complementary switch-query receipt proves unset passive=false") + +const frozenBefore = Object.freeze([Object.freeze(a), Object.freeze(b)]) as unknown as ConfigurationRow[] +const frozenAfter = Object.freeze([Object.freeze(updated), Object.freeze(b)]) as unknown as ConfigurationRow[] +const frozenStep = Object.freeze({ ...update, fields: Object.freeze(update.fields) }) +assert.equal(check(frozenStep, Object.freeze({}), frozenBefore, frozenAfter), true) +const raw = Object.freeze([Object.freeze({ ...a, opaque: "outside-projection" })]) +const projected = configurationRows(resourceById("ip_filter"), raw) +assert.equal(Object.hasOwn(projected[0], "opaque"), false) +assert.equal(raw[0].opaque, "outside-projection") +assert.equal(verifyRosStepSnapshots(update, {}, { ...snapshot("ip_filter", [a]), serverId: 2 }, snapshot("ip_filter", [updated])), false) +assert.equal(verifyRosStepSnapshots(update, {}, snapshot("bridge", [a]), snapshot("ip_filter", [updated])), false) +console.log("ROS projected whole-catalog verifier: acknowledged assigned ID, exact membership/field conservation, 12 ordered catalogs including OSPF templates, singleton settings, schema booleans, effective OSPF unset, immutable inputs") + +const sourceProof = "a".repeat(64) +function driftSnapshot(resource: string, rows: ConfigurationRow[]): RosSnapshot { + return { ...snapshot(resource, rows), projectionVersion: rosSnapshotVersion(resource), sourceProof } +} +function expectDrift(step: RosStep, receipt: unknown, before: ConfigurationRow[], after: ConfigurationRow[]) { + const expectation = proveRosDriftExpectation(step, receipt, driftSnapshot(step.resource, before), driftSnapshot(step.resource, after)) + assert.ok(expectation) + assert.equal(expectation.sourceProof, sourceProof) + return expectation +} + +const settingsDrift = expectDrift(settings, {}, [s0], [s1]) +assert.equal(settingsDrift.nativeId, undefined) +assert.equal(settingsDrift.matches(driftSnapshot("ip_settings", [{ ...s1, "ipv4-multipath-hash-policy": "l4" }])), true) +assert.equal(settingsDrift.matches(driftSnapshot("ip_settings", [s0])), false) +for (const rows of [[], [s1, s1], [{}], [{ "rp-filter": "unknown" }], [{ "rp-filter": null }], [{ "rp-filter": ["loose"] }]]) { + assert.equal(settingsDrift.matches(driftSnapshot("ip_settings", rows)), undefined) +} +const verifiedSettings = driftSnapshot("ip_settings", [s1]) +assert.equal(settingsDrift.matches({ ...verifiedSettings, sourceProof: "b".repeat(64) }), undefined) +assert.equal(settingsDrift.matches({ ...verifiedSettings, serverId: 2 }), undefined) +assert.equal(settingsDrift.matches({ ...verifiedSettings, fingerprint: "b".repeat(64) }), undefined) +assert.equal(proveRosDriftExpectation(settings, {}, snapshot("ip_settings", [s0]), verifiedSettings), undefined, "Legacy evidence without a source/version cannot prove drift") +assert.equal(proveRosDriftExpectation(settings, {}, driftSnapshot("ip_settings", [s0]), { kind: "vrf-production-private-checkpoint-v1" }), undefined) +assert.equal(proveRosDriftExpectation(settings, {}, driftSnapshot("ip_settings", [s0]), { ...verifiedSettings, sourceProof: "b".repeat(64) }), undefined) +assert.equal(proveRosDriftExpectation(settings, undefined, driftSnapshot("ip_settings", [s0]), verifiedSettings), undefined) + +const createDrift = expectDrift(create, ack, [a, b], [a, b, c]) +assert.equal(createDrift.nativeId, "*C") +assert.equal(createDrift.matches(driftSnapshot("ip_filter", [{ ...b, comment: "unrelated" }, c])), true) +assert.equal(createDrift.matches(driftSnapshot("ip_filter", [a, b])), false) +assert.equal(createDrift.matches(driftSnapshot("ip_filter", [a, b, c, c])), undefined) +assert.equal(proveRosDriftExpectation(create, {}, driftSnapshot("ip_filter", [a, b]), driftSnapshot("ip_filter", [a, b, c])), undefined) + +const updateDrift = expectDrift(update, {}, [a, b], [updated, b]) +assert.equal(updateDrift.nativeId, "*A") +assert.equal(updateDrift.matches(driftSnapshot("ip_filter", [{ ...updated, comment: "unrelated" }, c])), true) +assert.equal(updateDrift.matches(driftSnapshot("ip_filter", [a, c])), false) +assert.equal(updateDrift.matches(driftSnapshot("ip_filter", [b, c])), false) + +const booleanDrift = expectDrift(booleanStep, {}, [a], [a]) +assert.equal(booleanDrift.matches(driftSnapshot("ip_filter", [{ ...a, disabled: "no" }])), true) +assert.equal(booleanDrift.matches(driftSnapshot("ip_filter", [{ ...a, disabled: "yes" }])), false) +assert.equal(booleanDrift.matches(driftSnapshot("ip_filter", [{ ...a, disabled: "" }])), undefined) + +const deleteDrift = expectDrift(remove, null, [a, b], [b]) +assert.equal(deleteDrift.nativeId, "*A") +assert.equal(deleteDrift.matches(driftSnapshot("ip_filter", [{ ...b, comment: "unrelated" }, c])), true) +assert.equal(deleteDrift.matches(driftSnapshot("ip_filter", [{ ...a, disabled: "no" }, c])), false) +assert.equal(deleteDrift.matches(driftSnapshot("ip_filter", [{ ...a, chain: "output", comment: "a different rule reused this ID" }, b])), undefined) +console.log("ROS drift proof: bound source and verified transition, scoped typed fields, unknown observations and delete ID reuse") diff --git a/backend/src/modules/mcp/ros-step-verification.ts b/backend/src/modules/mcp/ros-step-verification.ts new file mode 100644 index 00000000..4dae1565 --- /dev/null +++ b/backend/src/modules/mcp/ros-step-verification.ts @@ -0,0 +1,169 @@ +import { isDeepStrictEqual } from "node:util" +import { fingerprint } from "./security.js" +import { acknowledgedCreateId } from "./native-create-identity.js" +import { canonicalBridgeVlanValue } from "../../services/bridge-vlan-values.js" +import { isOrderedRosResource, isSingletonRosResource, isStaticArpRow, resourceById, rosSnapshotVersion, supportsRosPlacement, type ConfigurationRow, type RosResourceSpec, type RosSnapshot, type RosStep } from "./resources.js" + +function booleanValue(value: unknown): unknown { + if ([true, "true", "yes"].includes(value as boolean | string)) return true + if ([false, "false", "no"].includes(value as boolean | string)) return false + return value +} + +function normalizedRow(resource: RosResourceSpec, row: ConfigurationRow, omit: Set): ConfigurationRow { + return Object.fromEntries(Object.entries(row).filter(([key]) => !omit.has(key)).map(([key, value]) => [key, + resource.fields.shape[key]?.safeParse(true).success ? booleanValue(value) : value, + ])) +} + +function conserved(resource: RosResourceSpec, before: ConfigurationRow, after: ConfigurationRow, omit = new Set()): boolean { + return isDeepStrictEqual(normalizedRow(resource, before, omit), normalizedRow(resource, after, omit)) +} + +function requested(resource: RosResourceSpec, fields: RosStep["fields"], row: ConfigurationRow): boolean { + return Object.entries(fields).every(([key, value]) => { + const schema = resource.fields.shape[key] + if (resource.id === "ospf_template" && key === "prefix-list" && value === "") return !Object.hasOwn(row, key) || row[key] === "" + if (!schema || typeof value !== "string" || !Object.hasOwn(row, key) || row[key] === undefined || row[key] === null) return false + if (schema.safeParse(true).success) return ["true", "false"].includes(value) && booleanValue(row[key]) === (value === "true") + if (resource.id === "bridge_vlan" && ["vlan-ids", "tagged", "untagged"].includes(key)) { + try { return canonicalBridgeVlanValue(key, row[key]) === canonicalBridgeVlanValue(key, value) } catch { return false } + } + return String(row[key]) === value + }) +} + +function hasUnexpectedOspfCreateFields(step: RosStep, row: ConfigurationRow): boolean { + if (step.resource !== "ospf_template" || step.action !== "create") return false + const unexpectedSelector = ["interfaces", "networks", "prefix-list"].some(key => + step.fields[key] === undefined && row[key] !== undefined && row[key] !== "") + const instanceId = row["instance-id"] + return unexpectedSelector || (step.fields["instance-id"] === undefined && instanceId !== undefined && instanceId !== "0") +} + +function indexed(rows: ConfigurationRow[]): Map | undefined { + const map = new Map() + for (const row of rows) { + if (!row || typeof row !== "object" || Array.isArray(row)) return undefined + const id = acknowledgedCreateId(row) + if (!id || map.has(id)) return undefined + map.set(id, row) + } + return map +} + +/** + * Compare only the supplied configurationRows projection, never opaque/private + * native fields or source identity. An acknowledgement is still required by + * the caller; matching snapshots cannot turn a lost response into success. + */ +export function verifyRosStepSnapshots(step: RosStep, result: unknown, before: RosSnapshot, after: RosSnapshot): boolean { + if (before.serverId !== step.serverId || after.serverId !== step.serverId || before.resource !== step.resource || after.resource !== step.resource || !Array.isArray(before.rows) || !Array.isArray(after.rows)) return false + let resource: RosResourceSpec + try { resource = resourceById(step.resource) } catch { return false } + if (resource.readOnly || !["create", "update", "delete"].includes(step.action) || step.action === "create" && (!resource.create || step.id !== undefined) || step.action === "delete" && !resource.remove) return false + if (!supportsRosPlacement(step)) return false + const changed = new Set(Object.keys(step.fields)) + if (step.action !== "delete" && !changed.size) return false + + // Settings are native singleton endpoints. Never invent a row ID for them. + if (isSingletonRosResource(step.resource)) { + if (step.action !== "update" || step.id !== undefined || before.rows.length !== 1 || after.rows.length !== 1) return false + const old = before.rows[0], current = after.rows[0] + if (!old || !current || typeof old !== "object" || typeof current !== "object" || Array.isArray(old) || Array.isArray(current)) return false + return requested(resource, step.fields, current) && conserved(resource, old, current, changed) + } + + const prior = indexed(before.rows), current = indexed(after.rows) + if (!prior || !current) return false + const id = step.action === "create" ? acknowledgedCreateId(result) : step.id + if (!id) return false + const old = prior.get(id), row = current.get(id) + if (step.resource === "ip_arp" && (old && !isStaticArpRow(old) || row && !isStaticArpRow(row))) return false + const expectedIds = [...prior.keys()] + if (step.action === "create") { + if (old || !row || current.size !== prior.size + 1 || !requested(resource, step.fields, row)) return false + if (hasUnexpectedOspfCreateFields(step, row)) return false + if (step.resource === "bfd_configuration" && ["interfaces", "addresses", "address-list"].some(key => step.fields[key] === undefined && row[key] !== undefined && row[key] !== "")) return false + if (step.placeBeforeId !== undefined) { + const position = expectedIds.indexOf(step.placeBeforeId) + if (position < 0) return false + expectedIds.splice(position, 0, id) + } else expectedIds.push(id) + } else if (step.action === "update") { + if (!old || !row || current.size !== prior.size || !requested(resource, step.fields, row) || !conserved(resource, old, row, changed)) return false + } else { + if (!old || row || current.size !== prior.size - 1) return false + expectedIds.splice(expectedIds.indexOf(id), 1) + } + for (const [otherId, previous] of prior) { + if (otherId === id && step.action !== "create") continue + const next = current.get(otherId) + if (!next || !conserved(resource, previous, next)) return false + } + // For unordered catalogs, the cardinality and conserved-ID checks above + // already prove exact membership. Ordered catalogs must also retain position. + return !isOrderedRosResource(step.resource) || isDeepStrictEqual(expectedIds, [...current.keys()]) +} + +function projectedSnapshot(value: unknown, step: RosStep): value is RosSnapshot & { sourceProof: string } { + if (!value || typeof value !== "object" || Array.isArray(value)) return false + const snapshot = value as RosSnapshot + if (snapshot.serverId !== step.serverId || snapshot.resource !== step.resource + || snapshot.projectionVersion !== rosSnapshotVersion(step.resource) + || typeof snapshot.sourceProof !== "string" || !/^[a-f0-9]{64}$/.test(snapshot.sourceProof) + || !Array.isArray(snapshot.rows) || snapshot.fingerprint !== fingerprint(snapshot.rows)) return false + if (!isSingletonRosResource(step.resource)) return indexed(snapshot.rows) !== undefined + const row = snapshot.rows[0] + return snapshot.rows.length === 1 && !!row && typeof row === "object" && !Array.isArray(row) +} + +interface RosDriftExpectation { + nativeId: string | undefined + sourceProof: string + matches(current: unknown): boolean | undefined +} + +/** Bind the drift expectation to an acknowledged and verified transition. + * The caller distinguishes a stored JSON null (native 204) from SQL NULL. */ +export function proveRosDriftExpectation(step: RosStep, receipt: unknown, before: unknown, after: unknown): RosDriftExpectation | undefined { + if (receipt === undefined || !projectedSnapshot(before, step) || !projectedSnapshot(after, step) + || before.sourceProof !== after.sourceProof || !verifyRosStepSnapshots(step, receipt, before, after)) return undefined + const resource = resourceById(step.resource) + const nativeId = step.action === "create" ? acknowledgedCreateId(receipt) : step.id + const singleton = isSingletonRosResource(step.resource) + const deleted = step.action === "delete" ? before.rows.find(row => row[".id"] === nativeId) : undefined + const sourceProof = after.sourceProof + return { + nativeId, + sourceProof, + matches(current) { + if (!projectedSnapshot(current, step) || current.sourceProof !== sourceProof) return undefined + const row = singleton ? current.rows[0] : current.rows.find(item => item[".id"] === nativeId) + if (deleted) { + if (!row) return true + // A different row at a reused ID does not prove the original row returned. + return conserved(resource, deleted, row) ? false : undefined + } + if (!row) return false + if (hasUnexpectedOspfCreateFields(step, row)) return false + let complete = true + for (const [key, expected] of Object.entries(step.fields)) { + const schema = resource.fields.shape[key] + const raw = row[key] + if (!schema || !Object.hasOwn(row, key) || raw === undefined || raw === null) { + complete = false + continue + } + const boolean = schema.safeParse(true).success + const valid = boolean ? typeof booleanValue(raw) === "boolean" + : (typeof raw === "string" || typeof raw === "number") && ( + schema.safeParse(raw).success || typeof raw === "string" && /^\d+$/.test(raw) && schema.safeParse(Number(raw)).success + ) + if (!valid) complete = false + else if (!requested(resource, { [key]: expected }, row)) return false + } + return complete ? true : undefined + }, + } +} diff --git a/backend/src/modules/mcp/routing-actions.ts b/backend/src/modules/mcp/routing-actions.ts new file mode 100644 index 00000000..9134e509 --- /dev/null +++ b/backend/src/modules/mcp/routing-actions.ts @@ -0,0 +1,279 @@ +import { prepareRoutingMainTransition, beginRoutingMainTransition, finishRoutingMainTransition, type RoutingMainTransitionAttempt } from "./routing-main-transition.js" +import { queryRoutingObservations, readRoutingObservations } from "./routing-observations.js" +import { isIP } from "node:net" +import { hasPermission } from "../../lib/permissions.js" +import { effectiveMcpConnection } from "./store.js" +import { z } from "zod" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { registerRoutingMeasurement } from "./routing-measurement.js" +import { buildOspfCostOptimizationEvidence, type OspfRank } from "../../services/ospf-cost-optimization.js" +import { planBgpInApply } from "../../services/config-apply-plan.js" +import { hasManagedCommentPrefix } from "../../managed-markers.js" +import { applyFiltersToServer, fetchServerFilters, toRouterRuleBody, type ApiFilterRule } from "../../routes/filters.js" +import { rankRoutingCandidates } from "./routing-model.js" +import { accessGeneration, fingerprint, McpError } from "./security.js" +import { readRosSnapshot, routerClient, type RosSnapshot } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import type { ChangeEvidence, ActionContext } from "./action-types.js" +import { managementEvidence } from "./management.js" +import { checkCandidatePath, requireRecentPath } from "./path-verification.js" +import { readRouteCommunityContext, routeContextAccess } from "./routing-community-context.js" +import { prepareScopedCommunitySwitch, registerScopedCommunityService } from "./routing-community-scoped-service.js" +import { resolveRoutingProfileAuthority, assertRoutingProfileAuthority, type RoutingProfileAuthorityBinding } from "./routing-profile-authority.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" + +const sid = z.number().int().positive().max(Number.MAX_SAFE_INTEGER) +const historySelector = z.string().trim().min(1).max(100) +const routingHistorySchema = z.object({ + serverId: sid.optional(), srcServerId: sid.optional(), dstServerId: sid.optional(), + srcInterface: historySelector.optional(), dstInterface: historySelector.optional(), + family: z.enum(["ipv4", "ipv6"]).optional(), table: historySelector.optional(), + limit: z.number().int().min(1).max(100).default(50), + cursor: z.object({ recordedAt: z.string().datetime({ precision: 6 }), id: z.string().uuid() }).strict().optional(), +}).strict() +const ospfOptimizerSchema = z.object({ serverId: sid, family: z.enum(["ipv4", "ipv6"]).default("ipv4"), table: z.string().min(1).default("main"), ecmpPaths: z.number().int().min(1).max(64).default(1), pingWeight: z.number().min(0).max(100).default(60), maxLossPct: z.number().min(0).max(100).default(1) }).strict() +export { readRoutingObservations } from "./routing-observations.js" + +const profileTarget = (profile: z.infer) => [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])] +const routeAnalysisSchema = z.object({ + profile: routeAiProfileSchema, + offset: z.number().int().min(0).max(100).default(0), + limit: z.number().int().min(1).max(100).default(20), + eligibility: z.enum(["all", "eligible", "ineligible"]).default("all"), +}).strict() +const routeComparisonSchema = z.object({ profile: routeAiProfileSchema, candidateIds: z.array(z.string().min(1).max(100)).min(2).max(8) }).strict().superRefine(({ profile, candidateIds }, context) => { + const known = new Set(profile.candidates.map(candidate => candidate.id)) + if (new Set(candidateIds).size !== candidateIds.length || candidateIds.some(id => !known.has(id))) { + context.addIssue({ code: "custom", path: ["candidateIds"], message: "Укажите разные ID путей из профиля" }) + } +}) + +async function analyzeCommunityProfile(profile: z.infer, context: ActionContext) { + const ids = profileTarget(profile) + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Профиль выходит за область доступа") + const authorize = routeContextAccess(context) + await authorize() + const ranking = rankRoutingCandidates(profile, await readRoutingObservations(ids)) + const routeContext = await readRouteCommunityContext(profile, { authorize, signal: context.signal }) + for (const row of ranking) if (routeContext.before.candidateDiagnostics.find(diagnostic => diagnostic.candidateId === row.candidate.id)?.status !== "available") { + row.eligible = false; row.reasons.push("CANDIDATE_CONTEXT_UNAVAILABLE") + } + ranking.sort((a, b) => Number(b.eligible) - Number(a.eligible)) + context.captureRoutingConfirmation?.(new Map(ranking.map(row => [row.candidate.id, row.confirmation]))) + const publicRanking = ranking.map(({ confirmation: _private, ...row }) => row) + return { profile, ranking: publicRanking, ...routeContext.before, observedPath: false as const, returnPathVerified: false as const, observedAt: new Date().toISOString() } +} + +async function assertCandidateEligible(profile: z.infer, candidateId: string) { + const candidate = rankRoutingCandidates(profile, await readRoutingObservations(profileTarget(profile))).find(row => row.candidate.id === candidateId) + if (!candidate?.eligible) throw new McpError("MEASUREMENT_UNKNOWN", "Выбранный путь не удовлетворяет свежести измерений, ограничениям и SLA: " + (candidate?.reasons.join("; ") ?? "путь отсутствует"), 409) + return candidate +} +function persistentRoutingAccess(input: Record) { + return async () => { + if (input.persistentProfileAuthority !== undefined) { + await assertRoutingProfileAuthority(input.persistentProfileAuthority as RoutingProfileAuthorityBinding, { + profile: routeAiProfileSchema.parse(input.profile), ownerId: z.string().parse(input.ownerId), + principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict().parse(input.principal), + transport: { contextSources: z.array(z.object({ serverId: z.number().int().positive(), proof: z.string().regex(/^[a-f0-9]{64}$/) }).strict()).parse(input.contextSources) }, + }) + } + } +} +async function filtersBefore(input: Record, afterWrite = false) { + const authorize = persistentRoutingAccess(input) + await authorize() + // Recheck freshness before the first write, without reranking the saved choice. + if (input.profile) { + const profile = routeAiProfileSchema.parse(input.profile), candidateId = String(input.candidateId) + const context = await readRouteCommunityContext(profile, { selectedCandidateId: candidateId, expectedSources: input.contextSources as Array<{ serverId: number; proof: string }> | undefined, authorize }) + if (!context.before.executionAvailable || input.contextProof !== context.before.dependencyProof) throw new McpError("STATE_CONFLICT", "Контекст Route AI изменился или scoped executor недоступен", 409) + if (!afterWrite && input.targetRuleProof !== context.before.targetRuleProof) throw new McpError("STATE_CONFLICT", "Owned Route AI policy изменилась", 409) + await assertCandidateEligible(profile, candidateId) + if (!afterWrite && profile.candidates.find(candidate => candidate.id === candidateId)?.controlProbe) await requireRecentPath(profile, candidateId, String(input.connectionId), { authorize }) + } + await authorize() + const { server } = await routerClient(Number(input.serverId)); const rules = (await fetchServerFilters(server)).rules + await authorize(); return rules +} + +export function registerRoutingActions(register: typeof add) { + registerRoutingMeasurement(register) + registerScopedCommunityService() + register("routing.measurements.read", "mm_routing_measurements_query", "История измерений со статусом проверки источника", "network", "mm:network:read", "read", "servers", routingHistorySchema, { + target: (_input, context) => ({ scope: "servers", serverIds: context.connection.policy.scope.serverIds }), + read: async (input, context) => queryRoutingObservations(context.connection.policy.scope.serverIds, routingHistorySchema.parse(input)), + }) + register("routing.analyze", "mm_route_optimizer_analyze", "Анализ профиля Community", "network", "mm:network:read", "read", "servers", routeAnalysisSchema, { + target: input => ({ scope: "servers", serverIds: profileTarget(routeAiProfileSchema.parse(input.profile)) }), + read: async (input, context) => { + const { profile, offset, limit, eligibility } = routeAnalysisSchema.parse(input) + const { ranking, candidateDiagnostics, ...analysis } = await analyzeCommunityProfile(profile, context) + const filtered = eligibility === "all" ? ranking : ranking.filter(row => row.eligible === (eligibility === "eligible")) + const page = filtered.slice(offset, offset + limit) + const candidateIds = new Set(page.map(row => row.candidate.id)) + return { ...analysis, ranking: page, candidateDiagnostics: candidateDiagnostics.filter(row => candidateIds.has(row.candidateId)), + total: filtered.length, offset, limit, nextOffset: offset + limit < filtered.length ? offset + limit : null } + }, + }) + register("routing.compare", "mm_route_optimizer_compare", "Сравнение выбранных путей Community", "network", "mm:network:read", "read", "servers", routeComparisonSchema, { + target: input => ({ scope: "servers", serverIds: profileTarget(routeAiProfileSchema.parse(input.profile)) }), + read: async (input, context) => { + const { profile, candidateIds } = routeComparisonSchema.parse(input) + const analysis = await analyzeCommunityProfile(profile, context) + const ranking = analysis.ranking.flatMap(({ candidate, ...row }, index) => candidateIds.includes(candidate.id) + ? [{ candidateId: candidate.id, profileRank: index + 1, ...row }] + : []) + return { serverId: profile.serverId, family: profile.family, table: profile.table, community: profile.community, + ranking, bestEligibleCandidateId: ranking.find(row => row.eligible)?.candidateId ?? null, + observedPath: analysis.observedPath, returnPathVerified: analysis.returnPathVerified, observedAt: analysis.observedAt } + }, + }) + + registerMcpService("routing.community", { + current: filtersBefore, + execute: async input => { + const authorize = persistentRoutingAccess(input) + await authorize() + const { server, client } = await routerClient(Number(input.serverId)) + const rules = z.array(z.object({ id: z.string(), community: z.string().regex(/^\d{1,10}:\d{1,10}$/), communityName: z.string().optional(), action: z.enum(["route", "blackhole"]).optional(), gateway: z.string(), gatewayTunnelId: z.string(), description: z.string() }).strict()).parse(input.rules) + await authorize() + const existing = await client.get>("/routing/filter/rule") + await authorize() + if (planBgpInApply(existing, rules.length).conflictIds.length) throw new McpError("STATE_CONFLICT", "Есть неуправляемые конфликтующие фильтры", 409) + const target = existing.filter(row => row.chain === "bgp-in" && hasManagedCommentPrefix(row.comment ?? "")) + if (input.profile && (target.length !== 1 || typeof target[0].comment !== "string")) throw new McpError("STATE_CONFLICT", "Owned target Route AI не подтверждён", 409) + let mainAttempt: RoutingMainTransitionAttempt | undefined + const result = await applyFiltersToServer(server, rules, "apply", { ...(input.profile ? { managedRuleComment: target[0].comment } : {}), beforeWrite: async (body, state) => { + await filtersBefore(input) + if (input.persistentProfileAuthority !== undefined) { + if (state.managed.length !== 1) throw new McpError("STATE_CONFLICT", "Main owned target отсутствует", 409) + mainAttempt = await beginRoutingMainTransition(input, body, state.managed[0][".id"], String(state.managed[0].rule)) + } + } }) + if (mainAttempt) await finishRoutingMainTransition(mainAttempt, result) + return result + }, + verify: async (input) => { + const observed = await filtersBefore(input, true) + const expected = input.rules as ApiFilterRule[] + if (!expected.every(rule => observed.some(actual => actual.community === rule.community && actual.gateway === rule.gateway && actual.action === rule.action))) return false + if (input.profile) { + const profile = routeAiProfileSchema.parse(input.profile), candidateId = String(input.candidateId) + if (profile.candidates.find(candidate => candidate.id === candidateId)?.controlProbe) return (await checkCandidatePath(profile, candidateId, String(input.connectionId), { authorize: persistentRoutingAccess(input) })).status === "passed" + } + return true + }, + }) + const canonicalRules = (rules: ApiFilterRule[]) => rules.map(({ community, action, gateway, gatewayTunnelId, description }) => ({ community, action: action ?? "route", gateway, gatewayTunnelId, description })).sort((a, b) => a.community.localeCompare(b.community)) + registerMcpService("routing.community.restore", { + current: async input => canonicalRules(await filtersBefore({ serverId: input.serverId })), + execute: async () => { throw new McpError("UNSUPPORTED_CAPABILITY", "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE", 409) }, + verify: async input => { + if (fingerprint(canonicalRules(await filtersBefore({ serverId: input.serverId }))) !== fingerprint(canonicalRules(input.rules as ApiFilterRule[]))) return false + return (await checkCandidatePath(routeAiProfileSchema.parse(input.profile), String(input.candidateId), String(input.connectionId))).status === "passed" + }, + }) + register("routing.community.switch", "mm_route_optimizer_plan", "Переключение Community на проверенный gateway", "network", "mm:filters:write", "write", "servers", z.object({ profile: routeAiProfileSchema, candidateId: z.string().min(1) }).strict(), { + target: input => ({ scope: "servers", serverIds: profileTarget(routeAiProfileSchema.parse(input.profile)) }), + prepare: async (input, context) => { + const profile = routeAiProfileSchema.parse(input.profile), ids = profileTarget(profile) + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Не разрешены зависимости пути") + if (profile.table !== "main" || profile.probeVrf !== "main" || profile.bgpContext && (profile.bgpContext.inputChain !== "bgp-in" || profile.bgpContext.vrf !== "main")) return prepareScopedCommunitySwitch(input, context) + let persistentProfileAuthority: RoutingProfileAuthorityBinding | undefined + const profileAuthorityContext = () => ({ profile, principal: context.principal, ownerId: context.connection.owner_id, transport: { contextSources: pinnedSources! }, signal: context.signal }) + let pinnedSources: Array<{ serverId: number; proof: string }> | undefined + const authorize = async () => { + await routeContextAccess(context)() + if (persistentProfileAuthority) await assertRoutingProfileAuthority(persistentProfileAuthority, profileAuthorityContext()) + } + if (context.persistentRoutingProfile) { + await routeContextAccess(context)() + pinnedSources = (await createRoutingSourceContext(await readRoutingEndpoints(ids))).expected + persistentProfileAuthority = await resolveRoutingProfileAuthority({ ...profileAuthorityContext(), profileId: context.persistentRoutingProfile.id, expectedVersion: context.persistentRoutingProfile.version }) + } + await authorize() + const routeContext = await readRouteCommunityContext(profile, { selectedCandidateId: String(input.candidateId), authorize, signal: context.signal, expectedSources: pinnedSources }) + if (!routeContext.before.executionAvailable) throw new McpError("UNSUPPORTED_CAPABILITY", String(routeContext.before.executionReason), 409) + const candidate = profile.candidates.find(candidate => candidate.id === input.candidateId) + if (!candidate || profile.excludedCandidateIds.includes(candidate.id) || profile.pinnedCandidateId && profile.pinnedCandidateId !== candidate.id) throw new McpError("PERMISSION_DENIED", "Путь исключён или закреплён другой") + await assertCandidateEligible(profile, candidate.id) + if (candidate.controlProbe) await requireRecentPath(profile, candidate.id, context.connection.id, { authorize, signal: context.signal }) + if (isIP(candidate.gateway.split("%")[0]) !== (profile.family === "ipv4" ? 4 : 6)) throw new McpError("INVALID_INPUT", "Семейство gateway не совпадает", 400) + const gateway = await readRosSnapshot(profile.serverId, candidate.gatewayRef.resource, authorize), row = gateway.rows.find(row => row[".id"] === candidate.gatewayRef.id) + if (!row || row.disabled === "true") throw new McpError("STATE_CONFLICT", "Gateway отсутствует или отключён", 409) + if (candidate.gatewayRef.resource !== "gre" && !hasManagedCommentPrefix(String(row.comment ?? ""))) throw new McpError("PROTECTED_OBJECT", "Рекурсивный gateway не принадлежит приложению") + await authorize() + const { server, client } = await routerClient(profile.serverId) + await authorize() + const observed = await fetchServerFilters(server) + await authorize() + if (planBgpInApply(await client.get("/routing/filter/rule"), observed.rules.length).conflictIds.length) throw new McpError("STATE_CONFLICT", "Конфликт с неуправляемыми фильтрами", 409) + await authorize() + if (!observed.rules.some(rule => rule.community === profile.community)) throw new McpError("STATE_CONFLICT", "Community отсутствует в управляемом наборе", 409) + if (candidate.gatewayRef.resource !== "gre" && String(row["dst-address"]).split("/")[0] !== candidate.gateway.split("%")[0]) throw new McpError("STATE_CONFLICT", "Gateway не соответствует адресу рекурсивного объекта", 409) + const rules = observed.rules.map(rule => rule.community === profile.community ? { ...rule, action: "route" as const, gateway: candidate.gateway, gatewayTunnelId: candidate.gatewayRef.resource === "gre" ? String(row.name) : "" } : rule) + const management = candidate.controlProbe ? await managementEvidence(ids) : { requiresHuman: true } + let recovery: ChangeEvidence["recovery"] + const recoverySnapshots: RosSnapshot[] = [] + const recoveryObjects: ChangeEvidence["objects"] = [] + if (profile.autoRecovery) { + const { rows: grants } = await pool.query("SELECT id,config,grant_expires_at FROM mcp_routing_profiles WHERE connection_id=$1 AND credential_id=$2 AND enabled AND grant_expires_at>now() AND policy_version=$3 AND generation=$4", [context.connection.id, context.principal.credentialId, context.connection.policy.version, await accessGeneration()]) + const grant = grants.find(grant => fingerprint(routeAiProfileSchema.parse(grant.config)) === fingerprint(profile)) + if (!grant || management.requiresHuman) throw new McpError("APPROVAL_REQUIRED", "Автовосстановление требует отдельного подтверждённого профиля и независимого управления", 409) + const currentRules = observed.rules.filter(rule => rule.community === profile.community) + const previous = profile.candidates.filter(path => path.id === routeContext.before.currentCandidateId) + if (previous.length === 1 && previous[0].controlProbe) { + // A failed original path is never an automatic recovery destination. + try { + await requireRecentPath(profile, previous[0].id, context.connection.id, { authorize, signal: context.signal }) + const prior = previous[0], priorSnapshot = await readRosSnapshot(profile.serverId, prior.gatewayRef.resource, authorize) + const priorRow = priorSnapshot.rows.find(value => value[".id"] === prior.gatewayRef.id) + if (!priorRow || priorRow.disabled === "true") throw new McpError("STATE_CONFLICT", "Исходный gateway недоступен", 409) + if (prior.gatewayRef.resource !== "gre" && (!hasManagedCommentPrefix(String(priorRow.comment ?? "")) || String(priorRow["dst-address"]).split("/")[0] !== prior.gateway.split("%")[0])) throw new McpError("STATE_CONFLICT", "Исходный gateway не подтверждён", 409) + if (prior.gatewayRef.resource === "gre" && String(priorRow.name) !== String(currentRules[0].gatewayTunnelId)) throw new McpError("STATE_CONFLICT", "Исходный GRE отличается от сохранённого пути", 409) + recoverySnapshots.push(priorSnapshot) + recoveryObjects.push({ kind: prior.gatewayRef.resource, id: prior.gatewayRef.id, serverId: profile.serverId }) + recovery = { authorityProfileId: grant.id, validUntil: new Date(Math.min(Date.now() + 120000, grant.grant_expires_at.getTime(), context.connection.owner_valid_until.getTime(), Date.parse(context.principal.credentialExpiresAt))).toISOString(), expected: canonicalRules(rules), step: { kind: "service", service: "routing.community.restore", input: { serverId: profile.serverId, rules: observed.rules, profile, candidateId: previous[0].id, connectionId: context.connection.id } } } + } catch { /* No confirmed safe original path: failover remains possible, automatic recovery is omitted. */ } + } + } + const mainInput: Record = { serverId: profile.serverId, rules, profile, candidateId: candidate.id, connectionId: context.connection.id, contextProof: routeContext.before.dependencyProof, targetRuleProof: routeContext.before.targetRuleProof, contextSources: routeContext.before.sourceProofs, ...(persistentProfileAuthority ? { persistentProfileAuthority, principal: context.principal, ownerId: context.connection.owner_id } : {}) } + if (persistentProfileAuthority) { + await authorize() + const interfaces = rules.some(rule => rule.action !== "blackhole" && rule.gatewayTunnelId && !rule.gatewayTunnelId.startsWith("rec:")) ? await client.get>("/interface/gre") : [] + await authorize() + mainInput.mainTransitionPreparation = await prepareRoutingMainTransition(mainInput, await toRouterRuleBody(profile.serverId, rules, interfaces)) + } + const filterSnapshot = await readRosSnapshot(profile.serverId, "routing_filter", authorize) + await authorize() + return { target: { scope: "servers", serverIds: ids }, objects: [{ kind: candidate.gatewayRef.resource, id: candidate.gatewayRef.id, serverId: profile.serverId }, ...recoveryObjects], snapshots: [gateway, ...recoverySnapshots, filterSnapshot], before: observed.rules, + steps: [{ kind: "service", service: "routing.community", input: mainInput }], ...management, recovery, + warnings: ["Конфигурация gateway подтверждена; фактический сквозной путь проверяется отдельно.", "Неуправляемые фильтры не удаляются."] } + }, + version: 7, + }) + + register("routing.ospf.analyze", "mm_ospf_optimizer_analyze", "Предварительный анализ OSPF cost по свежим измерениям без создания плана", "network", "mm:network:read", "read", "servers", ospfOptimizerSchema, { + target: (_input, context) => ({ scope: "servers", serverIds: context.connection.policy.scope.serverIds }), + read: async (input, context) => { + const evidence = await buildOspfOptimizationEvidence(input, context, "read") + return { serverId: input.serverId, family: input.family, table: input.table, serverIds: evidence.target.scope === "servers" ? evidence.target.serverIds : [], validUntil: evidence.validUntil, + ranking: evidence.before.ranking.slice(0, 50), eligibleInterfaces: evidence.before.ranking.length, changes: evidence.steps.length, incomplete: evidence.before.ranking.length > 50, + warnings: evidence.warnings, criteria: { maxLossPct: input.maxLossPct, pingWeight: input.pingWeight, ecmpPaths: input.ecmpPaths }, executionAvailable: false, proposalOnly: true, forwardingVerified: false, newApprovedPlanRequired: true } + }, + }) + register("routing.ospf.optimize", "mm_ospf_optimizer_plan", "Оптимизация стоимости OSPF без пересчёта при применении", "network", "mm:network:write", "write", "servers", ospfOptimizerSchema, { target: (_input, context) => ({ scope: "servers", serverIds: context.connection.policy.scope.serverIds }), prepare: buildOspfOptimizationEvidence, version: 13 }) +} + +async function buildOspfOptimizationEvidence(input: Record, context: ActionContext, mode: "read" | "write" = "write"): Promise { + const authorize = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Подготовка OSPF отменена", 409) + const current = await effectiveMcpConnection(context.principal), permission = "mm:network:" + mode + if (!hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission)) throw new McpError("PERMISSION_DENIED", "Право подготовки OSPF отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Допуск OSPF изменился; повторите подготовку", 409) + } + return buildOspfCostOptimizationEvidence(input, context.connection.policy.scope.serverIds, authorize) +} diff --git a/backend/src/modules/mcp/routing-bandwidth-original.test.ts b/backend/src/modules/mcp/routing-bandwidth-original.test.ts new file mode 100644 index 00000000..2029e894 --- /dev/null +++ b/backend/src/modules/mcp/routing-bandwidth-original.test.ts @@ -0,0 +1,211 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mkdtemp, writeFile, rm, unlink } from "node:fs/promises" +import path from "node:path" +import os from "node:os" +import { test } from "node:test" +import { pool, closePool } from "../../db/index.js" +import { withVerifiedConfigurationChange, type VerifiedMcpServiceMetadata } from "../../services/config-mutation-policy.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { fingerprint, privateFingerprinterExisting, redact } from "./security.js" +import { createRoutingBandwidthOriginalProvider } from "./routing-bandwidth-original.js" +import { createRoutingBandwidthPeerTrustedSource } from "./routing-bandwidth-peer-source.js" +import type { BandwidthPeerRef } from "../../services/bandwidth-peer-reference.js" +import { createBandwidthPeerObservationReader } from "../../services/bandwidth-peer-observation.js" + +test("routing bandwidth original production provider with fake SQL/native IO", async t => { + const directory = await mkdtemp(path.join(os.tmpdir(), "mm-routing-original-")), previousDirectory = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = directory + const key = Buffer.alloc(32, 31), generation = Buffer.alloc(32, 47) + const keyPath = path.join(directory, "encryption-key"), generationPath = path.join(directory, "access-generation") + await writeFile(keyPath, key); await writeFile(generationPath, generation) + const sign = await privateFingerprinterExisting("routing-observation-source-v1") + const originalQuery = pool.query, originalConnect = pool.connect, originalGet = MikrotikClient.prototype.get, originalPost = MikrotikClient.prototype.post + let row: Record | undefined, endpoints: Record[] = [], nativeReads = 0, credentialReads = 0 + let addresses: Record[] = [], nativeHook: (() => void) | undefined, queryHook: (() => void) | undefined + pool.query = (async (query: string | { text: string }) => { + const text = typeof query === "string" ? query : query.text + if (text.includes("FROM public.mcp_operations")) { queryHook?.(); return { rows: row ? [structuredClone(row)] : [] } } + assert.match(text, /WITH chosen AS MATERIALIZED/); credentialReads++ + return { rows: endpoints.map(endpoint => ({ id: endpoint.id, endpoint: structuredClone(endpoint), overflow: false })) } + }) as never + pool.connect = (() => { throw Error("Physical DB forbidden in unit") }) as never + MikrotikClient.prototype.get = (async (endpoint: string, _timeout: number, _signal: unknown, _budget: unknown, guard: () => Promise) => { + if (endpoint === "/system/license") { await guard(); nativeReads++; return { "system-id": "FAKE-SYSTEM-ID", "software-id": "FAKE-SOFTWARE-ID" } } + assert(["/ip/address", "/ipv6/address"].includes(endpoint)); await guard(); nativeReads++; nativeHook?.(); return structuredClone(addresses) + }) as never + MikrotikClient.prototype.post = (async (endpoint: string, body: Record, _timeout: number, _signal: unknown, guard: () => Promise) => { + assert(["/ip/address/get", "/ipv6/address/get"].includes(endpoint), "No bandwidth dispatch is allowed"); assert.equal(body["value-name"], "disabled") + await guard(); nativeReads++; return { ret: "false" } + }) as never + function fixture(expiry = Date.now() + 10000, ipv6 = false) { + endpoints = [101, 102].map(id => ({ id, host: "fake.invalid", port: 443, username: "fake-user-" + id, password: "fake-password-" + id, + useSsl: true, verifySsl: true, enabled: true, routingEndpointEpoch: randomUUID() })) + const input = { srcServerId: 101, dstServerId: 102, srcInterface: "gre-source", dstInterface: "gre-remote", family: ipv6 ? "ipv6" : "ipv4", vrf: ipv6 ? "blue" : "main", + bandwidth: true, durationSec: 5, maxMbps: 12, expectedEndpoints: endpoints.map(endpoint => ({ serverId: endpoint.id, + proof: sign("endpoint:" + fingerprint({ generation: generation.toString("hex"), endpoint })) })) } + const connectionId = randomUUID() + const metadata: VerifiedMcpServiceMetadata = { operationId: randomUUID(), planId: randomUUID(), operationName: "diagnostics.bandwidth", serviceName: "routing.measure", operationVersion: 2, + stepIndex: 0, phase: "forward", policyVersion: 1, generation: "original-generation", originalExpiresAt: expiry, sealedEvidenceProof: "e".repeat(64), sealedPayloadProof: "f".repeat(64), + originalTemporaryGrantProof: "a".repeat(64), originalTemporaryGrantExpiresAt: null, + principal: { connectionId, credentialId: randomUUID(), credentialExpiresAt: new Date(expiry + 1000).toISOString() }, serviceInput: input } + let authorized = true + const context = { actorId: "fake-original-owner", connectionId, approved: false, serverIds: [101, 102], objects: [101, 102].map(serverId => ({ kind: "server", serverId, id: String(serverId) })), serverWide: true, + serviceExecution: metadata, authorize: async () => { if (!authorized) throw Error("revoked original grant") } } + row = { operation_state: "running", plan_state: "executing", step_state: "intent", server_id: null, operation_id: metadata.operationName, + operation_version: 2, policy_version: 1, generation: metadata.generation, owner_id: context.actorId, approved_by: null, expires_at: new Date(expiry), evidence_proof: metadata.sealedEvidenceProof, + payload_proof: metadata.sealedPayloadProof, intended_state: redact({ kind: "service", service: metadata.serviceName, input }) } + addresses = [{ ".id": "*1", interface: "gre-remote", address: ipv6 ? "fe80::2/64" : "192.0.2.2/24" }]; nativeHook = undefined; queryHook = undefined + return { input, metadata, context, revoke: () => { authorized = false } } + } + const scope = () => ({ deadlineAt: Date.now() + 10000 }) + try { + await t.test("forged origin/context and foreign provider capability fail before credentials/native IO", async () => { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider(), reads = credentialReads, native = nativeReads + await assert.rejects(provider.capture(f.input, scope())) + await assert.rejects(provider.readOriginal({}, scope())) + assert.equal(credentialReads, reads); assert.equal(nativeReads, native) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()) + await assert.rejects(createRoutingBandwidthOriginalProvider().readOriginal(token, scope())) + }) + }) + await t.test("pins exact sealed request/body and distinct participant roles; freezes nested state", async () => { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider() + const token = await withVerifiedConfigurationChange(f.context, async () => { + const raw = structuredClone(f.input), token = await provider.capture(raw, scope()); raw.srcServerId = 999 + const intent = await provider.readOriginal(token, scope()) + assert.equal(intent.sourceServerId, 101); assert.equal(intent.credentialServerId, 102) + assert.deepEqual(intent.actualRemotePeer, { kind: "server", serverId: 102 }); assert(!("actualRemoteServerId" in intent)) + assert.deepEqual(intent.request, f.input); assert.equal(intent.nativeBody.address, "192.0.2.2") + assert.equal(intent.nativeBody.password, "fake-password-102"); assert.equal(intent.nativeBody.duration, "5s") + assert.equal(intent.nativeBody["local-tx-speed"], "12000000"); assert.equal(intent.socketTimeoutMs, 15000) + assert.equal(intent.expiresAt, f.metadata.originalExpiresAt); assert.equal(intent.callerKind, "routing-measurement") + assert(Object.isFrozen(intent)); assert(Object.isFrozen(intent.request)); assert(Object.isFrozen(intent.actualRemotePeer)); assert(Object.isFrozen(intent.nativeBody)) + assert.equal(await provider.readOriginal(token, scope()), intent) + return token + }) + await assert.rejects(provider.readOriginal(token, scope())) + }) + await t.test("native IPv6 selection binds source zone and VRF without accepting caller address", async () => { + const f = fixture(undefined, true), provider = createRoutingBandwidthOriginalProvider() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()) + assert.equal((await provider.readOriginal(token, scope())).nativeBody.address, "fe80::2%gre-source@blue") + }) + }) + await t.test("sealed endpoint proof drift and absent destination scope fail before native address selection", async () => { + for (const fault of ["proof", "scope", "object", "raw-native-body"] as const) { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider(), before = nativeReads + if (fault === "proof") f.input.expectedEndpoints[1].proof = "0".repeat(64) + if (fault === "scope") f.context.serverIds = [101] + if (fault === "object") f.context.objects = f.context.objects.slice(0, 1) + const raw = fault === "raw-native-body" ? { ...f.input, nativeBody: { address: "outside.invalid" } } : f.input + if (fault === "proof") row!.intended_state = redact({ kind: "service", service: "routing.measure", input: f.input }) + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(raw, scope())) }) + assert.equal(nativeReads, before) + } + }) + await t.test("source/destination credentials, endpoint epoch, disabled and SQL-origin drift refuse", async () => { + for (const fault of ["source-password", "destination-password", "epoch", "disabled", "sealed", "grant"] as const) { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()) + if (fault === "source-password") endpoints[0].password = "changed" + if (fault === "destination-password") endpoints[1].password = "changed" + if (fault === "epoch") endpoints[1].routingEndpointEpoch = randomUUID() + if (fault === "disabled") endpoints[1].enabled = false + if (fault === "sealed") row!.payload_proof = "0".repeat(64) + if (fault === "grant") f.revoke() + await assert.rejects(provider.readOriginal(token, scope())) + }) + } + }) + await t.test("missing generation/key refuses without state recreation", async () => { + for (const file of [keyPath, generationPath]) { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()); await unlink(file) + await assert.rejects(provider.readOriginal(token, scope())) + await assert.rejects(import("node:fs/promises").then(fs => fs.stat(file)), { code: "ENOENT" }) + }) + await writeFile(file, file === keyPath ? key : generation) + } + }) + await t.test("key/generation rotation invalidates pinned proof before credential transfer", async () => { + for (const file of [keyPath, generationPath]) { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), before = credentialReads + await writeFile(file, Buffer.alloc(32, 99)) + await assert.rejects(provider.readOriginal(token, scope())); assert.equal(credentialReads, before) + }) + await writeFile(file, file === keyPath ? key : generation) + } + }) + await t.test("managed peer source uses original pinned endpoints and rejects synthetic provider, forged context and unapproved refs", async () => { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider() + assert.throws(() => createRoutingBandwidthPeerTrustedSource({ readPeer: async () => { throw Error("substitute") } } as never, {} as never)) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), source = createRoutingBandwidthPeerTrustedSource(provider, token) + assert(Object.isFrozen(source)) + for (const id of [101, 102]) { + const peer = await source.read({ kind: "server", serverId: id }, scope()); assert(peer) + assert.deepEqual(peer.peerRef, { kind: "server", serverId: id }); assert.equal(peer.endpoint.password, "fake-password-" + id) + assert.equal(peer.sourceProof, f.input.expectedEndpoints.find(item => item.serverId === id)!.proof) + assert.equal(peer.authorityGeneration, f.metadata.generation); assert(Object.isFrozen(peer)); assert(Object.isFrozen(peer.endpoint)) + } + const before = credentialReads + for (const ref of [{ kind: "server", serverId: 999 }, { kind: "external", enrollmentId: randomUUID() }] as BandwidthPeerRef[]) await assert.rejects(source.read(ref, scope())) + await assert.rejects(createRoutingBandwidthPeerTrustedSource(provider, {} as never).read({ kind: "server", serverId: 101 }, scope())) + assert.equal(credentialReads, before) + f.revoke(); await assert.rejects(source.read({ kind: "server", serverId: 101 }, scope())); assert.equal(credentialReads, before) + }) + }) + await t.test("runtime revocation on fresh beforecredential origin read prevents first source credential lookup", async () => { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider(), before = credentialReads + let origins = 0; queryHook = () => { if (++origins === 3) f.revoke() } + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(f.input, scope())) }) + assert.equal(origins, 3); assert.equal(credentialReads, before) + }) + await t.test("v2 observer composes genuine original-backed source and loses authority after original source drift", async () => { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider() + await withVerifiedConfigurationChange(f.context, async () => { + const original = await provider.capture(f.input, scope()), source = createRoutingBandwidthPeerTrustedSource(provider, original) + const observer = createBandwidthPeerObservationReader(source) + try { + const token = await observer.observe({ kind: "server", serverId: 102 }, scope()), evidence = await observer.inspect(token, scope()) + assert.equal(evidence.version, 2); assert.deepEqual(evidence.peerRef, { kind: "server", serverId: 102 }) + assert.equal(evidence.sourceProof, f.input.expectedEndpoints[1].proof) + assert(!JSON.stringify(evidence).includes("fake-password")); assert(!JSON.stringify(evidence).includes("fake-user")) + endpoints[0].password = "revoked-source-credential" + await assert.rejects(observer.inspect(token, scope())) + } finally { observer.close() } + }) + }) + await t.test("revocation during native GET and ambiguous/unapproved native address never mint capability", async () => { + for (const fault of ["revoke", "ambiguous", "wrong-interface"] as const) { + const f = fixture(), provider = createRoutingBandwidthOriginalProvider() + if (fault === "revoke") nativeHook = f.revoke + if (fault === "ambiguous") addresses.push({ ...addresses[0], address: "192.0.2.3/24" }) + if (fault === "wrong-interface") addresses[0].interface = "outside-approved-scope" + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(f.input, scope())) }) + } + }) + await t.test("cancelled/deadline scope and original monotonic expiry cannot be renewed by wall rollback", async () => { + const f = fixture(Date.now() + 200), provider = createRoutingBandwidthOriginalProvider() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), controller = new AbortController(); controller.abort() + await assert.rejects(provider.readOriginal(token, { ...scope(), signal: controller.signal })) + await assert.rejects(provider.readOriginal(token, { deadlineAt: Date.now() - 1 })) + await new Promise(resolve => setTimeout(resolve, 220)) + const now = Date.now; Date.now = () => f.metadata.originalExpiresAt - 1000 + try { await assert.rejects(provider.readOriginal(token, scope())) } finally { Date.now = now } + }) + }) + } finally { + pool.query = originalQuery; pool.connect = originalConnect; MikrotikClient.prototype.get = originalGet; MikrotikClient.prototype.post = originalPost + if (previousDirectory === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previousDirectory + await rm(directory, { recursive: true, force: true }); await closePool() + } +}) diff --git a/backend/src/modules/mcp/routing-bandwidth-original.ts b/backend/src/modules/mcp/routing-bandwidth-original.ts new file mode 100644 index 00000000..bb6dbc7d --- /dev/null +++ b/backend/src/modules/mcp/routing-bandwidth-original.ts @@ -0,0 +1,152 @@ +/** Server-owned routing.measure origin. This authenticates the caller and exact + * native intent; independent remote-node policy must still authenticate the peer. + * No source enrollment, state bootstrap, registry mutation or dispatch occurs. */ +import { randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import path from "node:path" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { routeMeasurementSchema } from "@mmapp/contracts/mcp-routing" +import { captureVerifiedMcpServiceExecution } from "../../services/config-mutation-policy.js" +import type { BandwidthScope } from "../../services/bandwidth-admission.js" +import type { BandwidthPeerExecutionIntent } from "../../services/bandwidth-peer-execution-host.js" +import { parseBandwidthPeerRef, type BandwidthPeerRef } from "../../services/bandwidth-peer-reference.js" +import type { BandwidthPeerSourceSnapshot } from "../../services/bandwidth-peer-observation.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { createRoutingMeasurementOriginGuard } from "./routing-measurement-origin.js" +import { readCurrentMcpServiceExecution } from "./service-execution-origin.js" +import { readRoutingEndpoints } from "./routing-observation-source.js" +import { readMeasurementAddress } from "./routing-measurement-address.js" +import { fingerprint, McpError, mcpStateDirectory, privateFingerprinterExisting } from "./security.js" + +declare const originalBrand: unique symbol +export type RoutingBandwidthOriginal = Readonly<{ [originalBrand]: true }> +export type RoutingBandwidthExecutionIntent = BandwidthPeerExecutionIntent +const providers = new WeakSet() +export function assertRoutingBandwidthOriginalProvider(provider: unknown): asserts provider is ReturnType { + if (!provider || typeof provider !== "object" || !providers.has(provider)) deny() +} +const deny = (): never => { throw new McpError("PERMISSION_DENIED", "Исходный контекст bandwidth измерения изменён или недоступен", 403) } +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} +/** Reading an existing generation must never recreate deleted authority. */ +async function existingGeneration() { + const value = await readFile(path.join(mcpStateDirectory(), "access-generation")) + if (value.length !== 32) deny() + return value.toString("hex") +} +function scopeGuard(scope: Readonly, expiresAt: number, monotonicEnd: number) { + if (!scope || !Number.isSafeInteger(scope.deadlineAt) || scope.signal !== undefined && !(scope.signal instanceof AbortSignal)) deny() + const deadlineAt = Math.min(scope.deadlineAt, expiresAt), signal = scope.signal + const end = Math.min(monotonicEnd, performance.now() + Math.max(0, deadlineAt - Date.now())) + return () => { if (signal?.aborted || Date.now() >= deadlineAt || performance.now() >= end) deny() } +} +function captureScope(scope: Readonly): Readonly { + if (!scope || !Number.isSafeInteger(scope.deadlineAt) || scope.signal !== undefined && !(scope.signal instanceof AbortSignal)) deny() + return Object.freeze({ deadlineAt: scope.deadlineAt, signal: scope.signal }) +} + +export function createRoutingBandwidthOriginalProvider() { + type Entry = { intent: RoutingBandwidthExecutionIntent; peers: ReadonlyMap>; assertCurrent(scope: Readonly): Promise } + const contexts = new WeakMap() + const readOriginal = async (context: unknown, scope: Readonly): Promise => { + const entry = context && typeof context === "object" ? contexts.get(context) : undefined + if (!entry) return deny() + await entry.assertCurrent(captureScope(scope)) + return entry.intent + } + const provider = Object.freeze({ + /** Takes the genuine sealed service input, never an externally supplied + * native body/address, and selects the address using native read-only IO. */ + async capture(raw: Record, inputScope: Readonly): Promise { + const scope = captureScope(inputScope) + const entered = Date.now(), enteredMono = performance.now() + const live = scopeGuard(scope, scope?.deadlineAt, enteredMono + Math.max(0, scope?.deadlineAt - entered)) + live() + const origin = await createRoutingMeasurementOriginGuard(raw); live() + const token = await captureVerifiedMcpServiceExecution(); live() + const metadata = await readCurrentMcpServiceExecution(token, scope); live() + if (!isDeepStrictEqual(metadata.serviceInput, origin.input)) deny() + const { expectedEndpoints, ...measurement } = origin.input + const input = routeMeasurementSchema.parse(measurement) + if (!input.bandwidth || input.srcServerId === input.dstServerId) deny() + const originalEnd = enteredMono + Math.max(0, metadata.originalExpiresAt - entered) + const originalLive = scopeGuard(scope, metadata.originalExpiresAt, originalEnd) + originalLive() + const generation = await existingGeneration(); originalLive() + const sign = await privateFingerprinterExisting("routing-observation-source-v1"); originalLive() + const keyMessage = "routing-observation-source-purpose-key-epoch-v1", keyProof = sign(keyMessage) + const ids = [...new Set([input.srcServerId, input.dstServerId])].sort((a, b) => a - b) + if (!isDeepStrictEqual(await readCurrentMcpServiceExecution(token, scope), metadata)) deny() + originalLive() + const endpoints = freeze(await readRoutingEndpoints(ids)); originalLive() + if (endpoints.length !== ids.length || endpoints.some(endpoint => !endpoint.enabled) || !isDeepStrictEqual(endpoints.map(endpoint => endpoint.id), ids)) deny() + const expected = endpoints.map(endpoint => ({ serverId: endpoint.id, proof: sign("endpoint:" + fingerprint({ generation, endpoint })) })) + if (!isDeepStrictEqual(expectedEndpoints, expected)) deny() + const assertCurrent = async (currentScope: Readonly) => { + const currentLive = scopeGuard(currentScope, metadata.originalExpiresAt, originalEnd) + currentLive() + const current = await readCurrentMcpServiceExecution(token, currentScope); currentLive() + if (!isDeepStrictEqual(current, metadata) || generation !== await existingGeneration()) deny() + currentLive() + const currentSign = await privateFingerprinterExisting("routing-observation-source-v1"); currentLive() + if (currentSign(keyMessage) !== keyProof) deny() + if (!isDeepStrictEqual(await readCurrentMcpServiceExecution(token, currentScope), metadata)) deny() + currentLive() + const currentEndpoints = await readRoutingEndpoints(ids); currentLive() + if (!isDeepStrictEqual(currentEndpoints, endpoints) || generation !== await existingGeneration()) deny() + currentLive() + if ((await privateFingerprinterExisting("routing-observation-source-v1"))(keyMessage) !== keyProof) deny() + await origin.assertCurrent(); currentLive() + } + await assertCurrent(scope) + const destination = endpoints.find(endpoint => endpoint.id === input.dstServerId)! + const client = new MikrotikClient(destination, destination.id) + const nativeScope = Object.freeze({ deadlineAt: Math.min(scope.deadlineAt, metadata.originalExpiresAt), signal: scope.signal }) + const nativeGuard = () => assertCurrent(nativeScope) + const timeout = () => Math.max(1, Math.min(10000, nativeScope.deadlineAt - Date.now(), originalEnd - performance.now())) + const endpoint = input.family === "ipv4" ? "/ip/address" : "/ipv6/address" + const rows = z.array(z.record(z.string(), z.unknown())).max(10000).parse(await client.get(endpoint, timeout(), scope.signal, undefined, nativeGuard)) + await nativeGuard() + const selected = await readMeasurementAddress(rows, input, async id => client.post(endpoint + "/get", { ".id": id, "value-name": "disabled" }, timeout(), scope.signal, nativeGuard)) + await nativeGuard() + const address = selected.address + (selected.linkLocal ? "%" + input.srcInterface : "") + (input.vrf === "main" ? "" : "@" + input.vrf) + const nativeBody = { address, user: destination.username, password: destination.password, protocol: "tcp", direction: "both", duration: input.durationSec + "s", + "local-tx-speed": String(input.maxMbps! * 1000000), "remote-tx-speed": String(input.maxMbps! * 1000000) } + // The ciphertext proofs already bind the randomized sealed request. Never + // publish an unkeyed digest of the plaintext credential/native body. + const authorityProof = fingerprint({ domain: "mcp-routing-bandwidth-original-v1", operationId: metadata.operationId, planId: metadata.planId, + stepIndex: metadata.stepIndex, generation: metadata.generation, sealedEvidenceProof: metadata.sealedEvidenceProof, sealedPayloadProof: metadata.sealedPayloadProof, + originalTemporaryGrantProof: metadata.originalTemporaryGrantProof }) + const intent = freeze({ attemptId: randomUUID(), operationId: metadata.operationId, callerKind: "routing-measurement" as const, + sourceServerId: input.srcServerId, actualRemotePeer: { kind: "server" as const, serverId: input.dstServerId }, credentialServerId: input.dstServerId, + request: metadata.serviceInput as BandwidthPeerExecutionIntent["request"], nativeBody, durationSec: input.durationSec, socketTimeoutMs: (input.durationSec + 10) * 1000, + authorityProof, authorityGeneration: metadata.generation, expiresAt: metadata.originalExpiresAt }) + const context = Object.freeze(Object.create(null)) as RoutingBandwidthOriginal + const peers = new Map(endpoints.map(endpoint => [endpoint.id, freeze({ peerRef: { kind: "server" as const, serverId: endpoint.id }, enabled: endpoint.enabled, + sourceProof: expected.find(item => item.serverId === endpoint.id)!.proof, authorityGeneration: metadata.generation, + endpoint: { host: endpoint.host, port: endpoint.port, useSsl: endpoint.useSsl, verifySsl: endpoint.verifySsl, + username: endpoint.username, password: endpoint.password, routingEndpointEpoch: endpoint.routingEndpointEpoch } })])) + contexts.set(context, { intent, peers, assertCurrent }) + return context + }, + readOriginal, + /** The original service allows exactly its managed source and destination. + * Credential-provider identity never grants an external enrollment alias. */ + async readPeer(context: unknown, rawPeer: BandwidthPeerRef, scope: Readonly): Promise> { + const peer = parseBandwidthPeerRef(rawPeer) + const entry = context && typeof context === "object" ? contexts.get(context) : undefined + if (!entry) return deny() + if (peer.kind !== "server" || !entry.peers.has(peer.serverId)) return deny() + await readOriginal(context, scope) + const snapshot = entry.peers.get(peer.serverId) + if (!snapshot) return deny() + return snapshot + }, + }) + providers.add(provider) + return provider +} diff --git a/backend/src/modules/mcp/routing-bandwidth-peer-source.ts b/backend/src/modules/mcp/routing-bandwidth-peer-source.ts new file mode 100644 index 00000000..73ea2946 --- /dev/null +++ b/backend/src/modules/mcp/routing-bandwidth-peer-source.ts @@ -0,0 +1,11 @@ +import type { BandwidthPeerTrustedSource } from "../../services/bandwidth-peer-observation.js" +import { assertRoutingBandwidthOriginalProvider, type createRoutingBandwidthOriginalProvider, type RoutingBandwidthOriginal } from "./routing-bandwidth-original.js" + +/** Fixed production source for the v2 observer. Provider identity and its opaque + * original capability are checked at runtime; assembly cannot supply substitute + * authority/secret-reader callbacks. Every read rechecks the original first. */ +export function createRoutingBandwidthPeerTrustedSource(provider: ReturnType, original: RoutingBandwidthOriginal): Readonly { + assertRoutingBandwidthOriginalProvider(provider) + const read = provider.readPeer.bind(provider) + return Object.freeze({ read: (peerRef, scope) => read(original, peerRef, scope) } satisfies BandwidthPeerTrustedSource) +} diff --git a/backend/src/modules/mcp/routing-charge-audit.ts b/backend/src/modules/mcp/routing-charge-audit.ts new file mode 100644 index 00000000..fa6dfc84 --- /dev/null +++ b/backend/src/modules/mcp/routing-charge-audit.ts @@ -0,0 +1,57 @@ +import type { PoolClient } from "pg" +import { z } from "zod" +import { fingerprint, McpError, privateFingerprinterExisting } from "./security.js" +import type { RoutingSwitchLedgerEntry } from "./routing-switch-ledger-contract.js" +import { readRoutingChargeJournal } from "./routing-charge-journal.js" +import { stageRoutingChargeWitness } from "./routing-reservation-transaction.js" + +const action = "routing.charge.admitted" +const uuid = z.string().uuid(), digest = z.string().regex(/^[a-f0-9]{64}$/) +const anchor = z.object({ revision: z.literal(1), profileId: uuid, connectionId: uuid, ownerId: z.string().min(1).max(1000), + sourceOperationId: uuid, allocationId: uuid, kind: z.enum(["forward", "manual-inverse", "reserved"]), + bindingDigest: digest, createdAt: z.string().datetime() }).strict() +const envelope = z.object({ anchor, mac: digest }).strict() +type Anchor = z.infer +const signer = () => privateFingerprinterExisting("routing-charge-audit-v1") +const fail = () => new McpError("STATE_CONFLICT", "Учёт переключений не совпал с журналом допуска", 409) + +/** Run on the same transaction that creates the charge. No authority is issued. */ +export async function recordRoutingChargeAnchor(db: PoolClient, + input: Omit & { bindingProof: string }, deadline: number) { + const { bindingProof, ...fields } = input + const binding = anchor.parse({ ...fields, revision: 1, bindingDigest: fingerprint(bindingProof) }) + const mac = (await signer())(fingerprint(binding)) + const result = await db.query(`INSERT INTO mcp_audit (actor_id,action,connection_id,payload,created_at) + VALUES ($1,$2,$3,$4::jsonb,$5)`, [binding.ownerId, action, binding.connectionId, JSON.stringify({ anchor: binding, mac }), binding.createdAt]) + if (result.rowCount !== 1) throw fail() + stageRoutingChargeWitness(db, binding.profileId, { allocationId: binding.allocationId, createdAt: binding.createdAt, auditMac: mac }, deadline) +} + +/** The surviving audit is an independent witness of recent charges. Earlier + * charges remain readable without an audit anchor. */ +export async function assertRoutingChargeAudit(db: PoolClient, profileId: string, owner: { id: string; owner_id: string }, now: number, + entries: readonly RoutingSwitchLedgerEntry[]) { + const { rows } = await db.query(`SELECT actor_id,created_at, + CASE WHEN octet_length(payload::text)<=4096 THEN payload END AS payload + FROM mcp_audit WHERE action='${action}' AND connection_id=$1 AND payload->'anchor'->>'profileId'=$2 AND created_at>$3 + ORDER BY created_at,id LIMIT 12`, [owner.id, profileId, new Date(now - 3600000)]) + if (rows.length > 11) throw fail() + const witnesses = await readRoutingChargeJournal(profileId, now) + if (!rows.length && !witnesses.length) return + const sign = await signer(), seen = new Map() + for (const row of rows) { + const parsed = envelope.safeParse(row.payload) + if (!parsed.success) throw fail() + const { anchor: binding, mac } = parsed.data + if (binding.profileId !== profileId || binding.connectionId !== owner.id || binding.ownerId !== owner.owner_id || + row.actor_id !== binding.ownerId || !(row.created_at instanceof Date) || row.created_at.getTime() !== Date.parse(binding.createdAt) || + row.created_at.getTime() > now || mac !== sign(fingerprint(binding)) || seen.has(binding.allocationId)) throw fail() + seen.set(binding.allocationId, mac) + const expected = binding.kind === "reserved" ? ["forward", "inverse"] : [binding.kind === "forward" ? "forward" : "inverse"] + const matching = entries.filter(entry => entry.allocationId === binding.allocationId) + if (matching.length !== expected.length || expected.some(lane => !matching.some(entry => entry.lane === lane)) || + matching.some(entry => entry.profileId !== profileId || entry.sourceOperationId !== binding.sourceOperationId || + entry.createdAt !== binding.createdAt || fingerprint(entry.sourceIdentity) !== binding.bindingDigest)) throw fail() + } + if (witnesses.some(witness => seen.get(witness.allocationId) !== witness.auditMac)) throw fail() +} diff --git a/backend/src/modules/mcp/routing-charge-journal.test.ts b/backend/src/modules/mcp/routing-charge-journal.test.ts new file mode 100644 index 00000000..d26be6ce --- /dev/null +++ b/backend/src/modules/mcp/routing-charge-journal.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict" +import { randomBytes, randomUUID } from "node:crypto" +import { chmod, link, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import path from "node:path" +import { test } from "node:test" +import { appendRoutingChargeJournal, readRoutingChargeJournal } from "./routing-charge-journal.js" + +const hour = 3_600_000 +const now = Date.parse("2026-11-15T10:00:00.000Z") +const receipt = (time: number, mac = "a") => ({ + allocationId: randomUUID(), createdAt: new Date(time).toISOString(), auditMac: mac.repeat(64), +}) +const refused = { code: "STATE_CONFLICT" } + +async function owned(work: (root: string, profileId: string, filename: string) => Promise) { + const parent = await realpath(tmpdir()) + const root = await mkdtemp(path.join(parent, "mm-routing-charge-journal-")) + const previous = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = root + try { + await writeFile(path.join(root, "encryption-key"), randomBytes(32), { mode: 0o600 }) + const profileId = randomUUID() + await work(root, profileId, path.join(root, "routing-charge-journal", profileId + ".json")) + } finally { + if (previous === undefined) delete process.env.MCP_STATE_DIR + else process.env.MCP_STATE_DIR = previous + assert.equal(path.dirname(await realpath(root)), parent) + assert.match(path.basename(root), /^mm-routing-charge-journal-/) + await rm(root, { recursive: true, force: true }) + } +} + +test("host journal preserves exact receipts within the strict hourly window", () => owned(async (_root, profileId, filename) => { + assert.deepEqual(await readRoutingChargeJournal(profileId, now), []) + const start = receipt(now - hour), fresh = receipt(now - hour + 1), current = receipt(now, "b") + for (const item of [start, fresh, current]) await appendRoutingChargeJournal(profileId, item) + const persisted = await readFile(filename, "utf8") + assert.deepEqual(JSON.parse(persisted).body.witnesses, [fresh, current]) + assert.deepEqual(await readRoutingChargeJournal(profileId, now), [fresh, current]) + assert.deepEqual(await readRoutingChargeJournal(profileId, now + 1), [current]) + await appendRoutingChargeJournal(profileId, current) + await assert.rejects(appendRoutingChargeJournal(profileId, { ...current, auditMac: "c".repeat(64) }), refused) + assert.equal(await readFile(filename, "utf8"), persisted) +})) + +test("host journal refuses changed payload, identity, and signing material", () => owned(async (root, profileId, filename) => { + const item = receipt(now) + await appendRoutingChargeJournal(profileId, item) + const original = await readFile(filename, "utf8"), changed = JSON.parse(original) + changed.body.witnesses[0].auditMac = "c".repeat(64) + await writeFile(filename, JSON.stringify(changed)) + await assert.rejects(readRoutingChargeJournal(profileId, now), refused) + await assert.rejects(appendRoutingChargeJournal(profileId, receipt(now)), refused) + await writeFile(filename, original) + const other = randomUUID() + await writeFile(path.join(path.dirname(filename), other + ".json"), original, { mode: 0o600 }) + await assert.rejects(readRoutingChargeJournal(other, now), refused) + await assert.rejects(readRoutingChargeJournal("../" + profileId, now), refused) + await writeFile(path.join(root, "encryption-key"), randomBytes(32)) + await assert.rejects(readRoutingChargeJournal(profileId, now), refused) +})) + +test("host journal bounds and protects the snapshot file", () => owned(async (_root, profileId, filename) => { + for (let index = 0; index < 11; index++) await appendRoutingChargeJournal(profileId, receipt(now)) + await assert.rejects(appendRoutingChargeJournal(profileId, receipt(now)), refused) + assert.equal((await readRoutingChargeJournal(profileId, now)).length, 11) + const alias = path.join(path.dirname(filename), "alias.json") + await link(filename, alias) + await assert.rejects(readRoutingChargeJournal(profileId, now), refused) + await rm(alias) + if (process.platform !== "win32") { + await chmod(filename, 0o644) + await assert.rejects(readRoutingChargeJournal(profileId, now), refused) + await chmod(filename, 0o600) + } + await writeFile(filename, Buffer.alloc(4097)) + await assert.rejects(readRoutingChargeJournal(profileId, now), refused) +})) diff --git a/backend/src/modules/mcp/routing-charge-journal.ts b/backend/src/modules/mcp/routing-charge-journal.ts new file mode 100644 index 00000000..9c2f6895 --- /dev/null +++ b/backend/src/modules/mcp/routing-charge-journal.ts @@ -0,0 +1,151 @@ +import { randomUUID } from "node:crypto" +import { constants, type Stats } from "node:fs" +import { lstat, mkdir, open, realpath, unlink } from "node:fs/promises" +import path from "node:path" +import { z } from "zod" +import { constantEqual, fingerprint, McpError, mcpStateDirectory, privateFingerprinterExisting } from "./security.js" +import { publishStateFile } from "./state-file-publish.js" + +const windows = process.platform === "win32" +const maxBytes = 4096 +const maxWitnesses = 11 +const hour = 3_600_000 +const uuid = z.string().uuid().refine(value => value === value.toLowerCase()) +const hex = z.string().regex(/^[a-f0-9]{64}$/) +const canonicalTime = z.string().datetime().refine(value => { + const time = Date.parse(value) + return Number.isFinite(time) && new Date(time).toISOString() === value +}) +const witness = z.object({ allocationId: uuid, createdAt: canonicalTime, auditMac: hex }).strict() +const snapshot = z.object({ + body: z.object({ revision: z.literal(1), profileId: uuid, witnesses: z.array(witness).max(maxWitnesses) }).strict(), + mac: hex, +}).strict() + +export type RoutingChargeWitness = Readonly> + +const fail = () => new McpError("STATE_CONFLICT", "Локальный журнал списаний маршрутизации недоступен или повреждён", 409) +const missing = (error: unknown) => (error as NodeJS.ErrnoException).code === "ENOENT" +const noFollow = windows ? 0 : constants.O_NOFOLLOW +const privateOwner = (entry: Stats) => windows || (entry.uid === process.geteuid?.() && (entry.mode & 0o077) === 0) +const sign = () => privateFingerprinterExisting("routing-charge-host-journal-v1") +const signingEpoch = (signer: Awaited>) => signer("routing-charge-host-journal-key") +const comparable = (value: string) => windows ? value.toLowerCase() : value + +async function initialEntry(filename: string) { + try { return await lstat(filename) } catch (error) { + if (missing(error)) return undefined + throw error + } +} + +async function assertDirectory(directory: string, entry: Stats, privateMode: boolean) { + if (!entry.isDirectory() || (privateMode && !privateOwner(entry)) || + comparable(await realpath(directory)) !== comparable(directory)) throw fail() +} + +async function checkDirectory(directory: string, privateMode: boolean) { + await assertDirectory(directory, await lstat(directory), privateMode) +} + +async function syncDirectory(directory: string) { + if (windows) return + const handle = await open(directory, constants.O_RDONLY | constants.O_DIRECTORY | noFollow) + try { await handle.sync() } finally { await handle.close() } +} + +async function readSnapshot(filename: string, entry: Stats, profileId: string, signer: Awaited>) { + if (!entry.isFile() || entry.nlink !== 1 || entry.size === 0 || entry.size > maxBytes || !privateOwner(entry)) throw fail() + const handle = await open(filename, constants.O_RDONLY | noFollow | (windows ? 0 : constants.O_NONBLOCK)) + try { + const opened = await handle.stat() + if (!opened.isFile() || opened.dev !== entry.dev || opened.ino !== entry.ino || opened.nlink !== 1 || + opened.size !== entry.size || !privateOwner(opened)) throw fail() + const bytes = Buffer.alloc(maxBytes + 1) + let length = 0 + while (length < bytes.length) { + const { bytesRead } = await handle.read(bytes, length, bytes.length - length, length) + if (bytesRead === 0) break + length += bytesRead + } + if (length !== opened.size) throw fail() + const { body, mac } = snapshot.parse(JSON.parse(bytes.toString("utf8", 0, length))) + if (body.profileId !== profileId || !constantEqual(mac, signer(fingerprint(body))) || + new Set(body.witnesses.map(item => item.allocationId)).size !== body.witnesses.length) throw fail() + return body.witnesses + } finally { await handle.close() } +} + +async function storedWitnesses(directory: string, filename: string, profileId: string, + signer: Awaited>) { + const directoryEntry = await initialEntry(directory) + if (!directoryEntry) return [] + await assertDirectory(directory, directoryEntry, true) + const fileEntry = await initialEntry(filename) + const items = fileEntry ? await readSnapshot(filename, fileEntry, profileId, signer) : [] + // A directory that disappeared after the initial lookup is not first use. + const currentDirectory = await lstat(directory) + await assertDirectory(directory, currentDirectory, true) + if (currentDirectory.dev !== directoryEntry.dev || currentDirectory.ino !== directoryEntry.ino) throw fail() + return items +} + +async function assertSameSigner(root: string, signer: Awaited>) { + if (mcpStateDirectory() !== root || !constantEqual(signingEpoch(signer), signingEpoch(await sign()))) throw fail() +} + +export async function readRoutingChargeJournal(profileId: string, now: number): Promise { + try { + uuid.parse(profileId) + if (!Number.isSafeInteger(now)) throw fail() + const root = mcpStateDirectory(), directory = path.join(root, "routing-charge-journal") + await checkDirectory(root, false) + const signer = await sign() + const items = await storedWitnesses(directory, path.join(directory, profileId + ".json"), profileId, signer) + await assertSameSigner(root, signer) + return items.filter(item => Date.parse(item.createdAt) > now - hour) + } catch { throw fail() } +} + +/** The caller holds the global SQL admission lock until after publication. + * This host file survives SQL rollback, including an uncertain COMMIT. */ +export async function appendRoutingChargeJournal(profileId: string, value: RoutingChargeWitness): Promise { + try { + uuid.parse(profileId) + const item = witness.parse(value) + const root = mcpStateDirectory(), directory = path.join(root, "routing-charge-journal") + const filename = path.join(directory, profileId + ".json") + await checkDirectory(root, false) + const signer = await sign() + const items = await storedWitnesses(directory, filename, profileId, signer) + const existing = items.find(entry => entry.allocationId === item.allocationId) + if (existing && (existing.createdAt !== item.createdAt || existing.auditMac !== item.auditMac)) throw fail() + if (existing) { + await assertSameSigner(root, signer) + return + } + const witnesses = items.filter(entry => Date.parse(entry.createdAt) > Date.parse(item.createdAt) - hour) + witnesses.push(item) + const body = snapshot.shape.body.parse({ revision: 1, profileId, witnesses }) + const encoded = JSON.stringify({ body, mac: signer(fingerprint(body)) }) + if (Buffer.byteLength(encoded) > maxBytes) throw fail() + + await checkDirectory(root, false) + try { await mkdir(directory, { mode: 0o700 }) } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error + } + await checkDirectory(directory, true) + await syncDirectory(root) + const temporary = path.join(directory, profileId + "." + randomUUID() + ".tmp") + const handle = await open(temporary, "wx", 0o600) + try { + try { await handle.writeFile(encoded); await handle.sync() } finally { await handle.close() } + await assertSameSigner(root, signer) + await publishStateFile(temporary, filename) + await syncDirectory(directory) + await assertSameSigner(root, signer) + } finally { + await unlink(temporary).catch(() => {}) + } + } catch { throw fail() } +} diff --git a/backend/src/modules/mcp/routing-community-context-acceptance.ts b/backend/src/modules/mcp/routing-community-context-acceptance.ts new file mode 100644 index 00000000..4df7b60f --- /dev/null +++ b/backend/src/modules/mcp/routing-community-context-acceptance.ts @@ -0,0 +1,233 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { managedComment } from "../../managed-markers.js" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool } from "./runtime.js" +import { requireRecentPath } from "./path-verification.js" +import { attestRoutingFixture } from "../../test/mcp-routing-fixture.js" +import { approvePlan, applyPlan, loadPlan, publicPlan } from "./runtime.js" + +/** Diagnostic paths only; never print a matching private value or native row. */ +function privateMarkerPaths(value: unknown, path = "$", found: string[] = []): string[] { + if (typeof value === "string" && value.includes("NEVER_")) found.push(path) + else if (Array.isArray(value)) value.forEach((item, index) => privateMarkerPaths(item, path + "[" + index + "]", found)) + else if (value && typeof value === "object") for (const [key, item] of Object.entries(value)) privateMarkerPaths(item, path + "." + key, found) + return found +} + +/** Hook for the disposable SQL+HTTP integration suite; no real router writes. */ +export async function acceptRouteCommunityContextMcp(actor: AuthUser) { + const sid = 130 + const writerActor: AuthUser = { ...actor, permissions: [...new Set([...actor.permissions, "mm:filters:write"])] } + let writes = 0, requests = 0, secondConnection = false, counter = 0 + let afterRead: (() => Promise) | undefined + let table = "policy-only" + const inputChain = "bgp-in" + let inherited = false, parentChain = "bgp-in", oversized = false, afi = "ip", interfaceList = false + let separateProbeVrf = false, bgpVrfDisabled = false + const route: Record = { ".id": "*r", "dst-address": "192.0.2.1/32", "routing-table": "main", disabled: "false", comment: managedComment("recursive"), opaque: "NEVER_ROUTE_CONTEXT_NATIVE" } + const body = compileManagedFilterRules([{ community: "65000:10", gateway: "192.0.2.1", outIface: "", action: "route", description: "" }]) + let policyBody = body, changePolicyAfterTrace = false + let policyComment = managedComment("filter"), mainWriteEnabled = false + const reserveRoute = { ...route, ".id": "*r2", "dst-address": "192.0.2.9/32" } + const connection = () => ({ ".id": "*c", name: "peer", disabled: "false", instance: "default", vrf: "main", "routing-table": table, afi, ...(!inherited ? { "input.filter": inputChain } : {}), "output.filter-chain": "", templates: inherited ? "parent" : "" }) + const collections = (path: string): Record[] => ({ + "/routing/table": [{ ".id": "*tm", name: "main", fib: "true", dynamic: "true" }, { ".id": "*tp", name: "policy-only", fib: "true" }, ...(separateProbeVrf ? [{ ".id": "*td", name: "probe-only", fib: "true", dynamic: "true" }] : [])], + "/ip/vrf": [...(interfaceList ? [{ ".id": "*vl", name: "listed", interfaces: "WAN-LIST", disabled: "false" }] : []), ...(separateProbeVrf ? [{ ".id": "*vd", name: "probe-only", interfaces: "wan", disabled: "false" }] : []), { ".id": "*v", name: "main", interfaces: "all", disabled: String(bgpVrfDisabled) }], + "/routing/bgp/connection": [connection(), ...(secondConnection ? [{ ...connection(), ".id": "*c2", "routing-table": "main" }] : [])], + "/routing/bgp/instance": [{ ".id": "*i", name: "default", vrf: "main", "routing-table": "main", disabled: "false" }], + "/routing/bgp/template": inherited ? [{ ".id": "*t", name: "parent", disabled: "false", "input.filter": parentChain }] : [], + "/routing/filter/rule": [{ ".id": "*f", chain: "bgp-in", disabled: "false", rule: policyBody, comment: policyComment, opaque: "NEVER_ROUTE_CONTEXT_TARGET_OPAQUE" }], + "/ip/route": mainWriteEnabled ? [route, reserveRoute] : [route], + "/interface": ["wan", "hop", "target"].map((name, index) => ({ ".id": "*if" + index, name, disabled: "false", running: "true", "rx-byte": String(++counter) })), + "/ip/address": ["wan", "hop", "target"].map((name, index) => ({ ".id": "*a" + index, interface: name, address: "192.0.2." + (index + 1) + "/32", disabled: "false", invalid: "false" })), + } as Record[]>)[path] ?? [] + const probeVrfs: string[] = [] + const router = createServer(async (req, res) => { + requests++; res.setHeader("content-type", "application/json") + let raw = ""; for await (const chunk of req) raw += chunk + const args = raw ? JSON.parse(raw) : {}, path = (req.url ?? "").replace(/^\/rest/, "") + if (afterRead) { const hook = afterRead; afterRead = undefined; await hook() } + if (oversized) { res.end(JSON.stringify({ value: "я".repeat(600000) })); return } + if (mainWriteEnabled && req.method === "PATCH" && path === "/routing/filter/rule/*f") { + assert.equal(args.chain, "bgp-in"); assert.equal(args.disabled, "no") + writes++; policyBody = args.rule; policyComment = args.comment + res.end(JSON.stringify(collections("/routing/filter/rule")[0])); return + } + if (req.method === "GET") { res.end(JSON.stringify(collections(path))); return } + if (req.method === "POST" && path.endsWith("/get")) { + const row = collections(path.slice(0, -4)).find(row => row[".id"] === args[".id"]), value = row?.[args["value-name"]] + res.end(JSON.stringify(value === undefined ? [] : { ret: value })); return + } + if (req.method === "POST" && path === "/tool/ping") { probeVrfs.push(args.vrf); res.end(JSON.stringify([{ sent: "3", received: "3", "packet-loss": "0", "avg-rtt": "1ms" }])); return } + if (req.method === "POST" && path === "/tool/traceroute") { + probeVrfs.push(args.vrf) + if (changePolicyAfterTrace) { changePolicyAfterTrace = false; policyBody = compileManagedFilterRules([{ community: "65000:10", gateway: "192.0.2.9", outIface: "", action: "route", description: "" }]) } + res.end(JSON.stringify(["192.0.2.2", "192.0.2.3"].map(address => ({ address, sent: "1", loss: "0" })))); return + } + writes++; res.statusCode = 405; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined, writeConnectionId: string | undefined + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'route-context','127.0.0.1',$2,false,'NEVER_ROUTE_CONTEXT_PASSWORD')", [sid, (router.address() as { port: number }).port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Route context", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [sid], operationIds: ["routing.analyze", "routing.path.verify"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const base = routeAiProfileSchema.parse({ name: "table is not VRF", serverId: sid, community: "65000:10", family: "ipv4", table: "policy-only", probeVrf: "main", bgpContext: { connectionId: "*c", instance: "default", inputChain: "bgp-in" }, candidates: [{ id: "a", jumpHostId: sid, exitNodeId: sid, wanInterface: "wan", downstreamInterface: "hop", gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*r" }, controlProbe: { targetInterface: "target", expectedHops: [{ serverId: sid, interface: "hop" }, { serverId: sid, interface: "target" }] } }] }) + const analyze = async (profile = base) => await invokeMcpTool(principal, "mm_route_optimizer_analyze", { profile }) as { currentCandidateId: string | null; executionAvailable: boolean; contextVerified: boolean; sharedConnectionIds: string[]; bgpVrf: string | null; probeVrf: string } + const result = await analyze() + assert.equal(result.contextVerified, true); assert.equal(result.currentCandidateId, "a"); assert.equal(result.executionAvailable, false) + assert.ok(!JSON.stringify(result).includes("NEVER_")) + route["dst-address"] = "192.0.2.1/24" + assert.equal((await analyze()).currentCandidateId, null, "a covering route is not the registered recursive host route") + route["dst-address"] = "192.0.2.1/32" + afi = "ipv6" + assert.equal((await analyze()).currentCandidateId, null, "the selected BGP binding must carry the requested address family") + afi = "ip"; interfaceList = true + const unavailableProbe = await analyze() as Awaited> & { candidateDiagnostics: Array<{ candidateId: string; status: string }> } + assert.equal(unavailableProbe.candidateDiagnostics[0].status, "candidate-unavailable", "unknown earlier VRF interface-list membership cannot fall through to main all") + interfaceList = false + inherited = true + assert.equal((await analyze()).currentCandidateId, "a", "explicit binding may be inherited from a uniquely resolved template") + parentChain = "other" + assert.equal((await analyze()).currentCandidateId, null, "different inherited input-chain is not the selected profile") + parentChain = "bgp-in"; inherited = false; secondConnection = true + assert.deepEqual((await analyze()).sharedConnectionIds.sort(), ["*c", "*c2"]) + secondConnection = false + const wrong = routeAiProfileSchema.parse({ ...base, bgpContext: { ...base.bgpContext, instance: "other" } }) + assert.equal((await analyze(wrong)).currentCandidateId, null) + assert.equal((await analyze(routeAiProfileSchema.parse({ ...base, table: "main" }))).currentCandidateId, null) + const duplicate = routeAiProfileSchema.parse({ ...base, candidates: [...base.candidates, { ...base.candidates[0], id: "same-gateway" }] }) + assert.equal((await analyze(duplicate)).currentCandidateId, null, "same gateway does not identify a unique candidate") + const check = async (profile = base) => await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "a" }) as { id: string; status: string; returnPathVerified: boolean } + separateProbeVrf = true + const distinct = routeAiProfileSchema.parse({ ...base, probeVrf: "probe-only", bgpContext: { ...base.bgpContext, vrf: "main" } }) + const separated = await analyze(distinct) + assert.equal(separated.contextVerified, true); assert.equal(separated.currentCandidateId, "a"); assert.equal(separated.executionAvailable, false) + assert.equal(separated.bgpVrf, "main"); assert.equal(separated.probeVrf, "probe-only") + assert.equal((await analyze(routeAiProfileSchema.parse({ ...distinct, bgpContext: { ...distinct.bgpContext, vrf: "probe-only" } }))).currentCandidateId, null, "wrong explicit BGP transport VRF cannot match merely because the probe uses it") + const distinctCheck = await check(distinct) + assert.equal(distinctCheck.status, "passed"); assert.equal(distinctCheck.returnPathVerified, false) + assert.deepEqual(probeVrfs, ["probe-only", "probe-only"]) + assert.equal(await requireRecentPath(distinct, "a", connectionId), distinctCheck.id) + bgpVrfDisabled = true + await assert.rejects(analyze(distinct), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE", "BGP transport VRF is checked independently of the enabled probe VRF") + bgpVrfDisabled = false; separateProbeVrf = false; probeVrfs.length = 0 + changePolicyAfterTrace = true + assert.equal((await check()).status, "unknown", "An owned policy change during the probe cannot retain passed status") + policyBody = body; probeVrfs.length = 0 + const passed = await check() + assert.equal(passed.status, "passed"); assert.equal(passed.returnPathVerified, false); assert.deepEqual(probeVrfs, ["main", "main"]) + assert.equal(await requireRecentPath(base, "a", connectionId), passed.id) + route.opaque = "changed dependency" + await assert.rejects(requireRecentPath(base, "a", connectionId), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN") + route.opaque = "NEVER_ROUTE_CONTEXT_NATIVE" + await pool.query("UPDATE mcp_path_checks SET evidence=jsonb_set(evidence,'{status}','\"failed\"') WHERE id=$1", [passed.id]) + await assert.rejects(requireRecentPath(base, "a", connectionId)) + await check() + const beforeEndpoint = requests + await pool.query("UPDATE servers SET password='NEVER_CHANGED_PASSWORD' WHERE id=$1", [sid]) + await assert.rejects(requireRecentPath(base, "a", connectionId), (error: { code?: string }) => error.code === "STATE_CONFLICT") + assert.equal(requests, beforeEndpoint, "prior source proof rejects changed endpoint before any native request") + oversized = true + await assert.rejects(analyze(), (error: Error & { code?: string }) => ["LIMIT_EXCEEDED", "STATE_UNAVAILABLE"].includes(error.code ?? "") && !error.message.includes("я")) + oversized = false + afterRead = () => setConnectionStatus(connectionId!, "paused", actor) + await assert.rejects(analyze()) + assert.equal(writes, 0) + const saved = JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows) + assert.ok(!saved.includes("NEVER_")) + // Exercise the actual service through a human-approved runtime plan. The + // legacy owned comment intentionally differs from the current server name. + table = "main"; mainWriteEnabled = true; policyComment = managedComment("NEVER_LEGACY_OWNED_COMMENT") + const legacyComment = policyComment + const withoutProbe = (({ controlProbe: _probe, ...candidate }) => candidate)(base.candidates[0]) + const mainProfile = routeAiProfileSchema.parse({ ...base, table: "main", candidates: [withoutProbe, { ...withoutProbe, id: "b", gateway: "192.0.2.9", gatewayRef: { resource: "ipv4_route", id: "*r2" } }] }) + const compareAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Community comparison", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [sid], operationIds: ["routing.compare"], allowGlobal: false } }), actor) + const analyzeAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Community analysis only", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [sid], operationIds: ["routing.analyze"], allowGlobal: false } }), actor) + try { + const comparePrincipal = await authenticateMcpToken(compareAccess.token) + const analyzePrincipal = await authenticateMcpToken(analyzeAccess.token) + const compare = (candidateIds: string[], profile = mainProfile) => invokeMcpTool(comparePrincipal, "mm_route_optimizer_compare", { profile, candidateIds }) + const beforeDenied = requests + await assert.rejects(invokeMcpTool(analyzePrincipal, "mm_route_optimizer_compare", { profile: mainProfile, candidateIds: ["a", "b"] })) + await assert.rejects(compare(["a", "a"])) + await assert.rejects(compare(["a", "missing"])) + await assert.rejects(compare(["a", "b"], routeAiProfileSchema.parse({ ...mainProfile, candidates: [mainProfile.candidates[0], { ...mainProfile.candidates[1], exitNodeId: sid + 1 }] }))) + assert.equal(requests, beforeDenied) + const beforeWrites = writes + const analyzed = await invokeMcpTool(analyzePrincipal, "mm_route_optimizer_analyze", { profile: mainProfile }) as { ranking: Array<{ candidate: { id: string }; eligible: boolean; score: number | null }> } + const result = await compare(["b", "a"]) as { ranking: Array<{ candidateId: string; profileRank: number; eligible: boolean; score: number | null }>; observedPath: boolean; returnPathVerified: boolean } + assert.deepEqual(result.ranking.map(({ candidateId, profileRank, eligible, score }) => ({ candidateId, profileRank, eligible, score })), analyzed.ranking.map(({ candidate, eligible, score }, index) => ({ candidateId: candidate.id, profileRank: index + 1, eligible, score }))) + assert.equal(result.observedPath, false) + assert.equal(result.returnPathVerified, false) + const pagedProfile = routeAiProfileSchema.parse({ ...mainProfile, candidates: [...mainProfile.candidates, ...Array.from({ length: 20 }, (_, index) => ({ ...mainProfile.candidates[1], id: `missing-${String(index).padStart(2, "0")}`, gatewayRef: { resource: "ipv4_route", id: `*absent${index}` } }))] }) + type AnalysisPage = { ranking: Array<{ candidate: { id: string }; eligible: boolean }>; candidateDiagnostics: Array<{ candidateId: string }>; total: number; nextOffset: number | null; currentCandidateId: string | null } + const page = (options = {}) => invokeMcpTool(analyzePrincipal, "mm_route_optimizer_analyze", { profile: pagedProfile, ...options }) as Promise + const firstPage = await page(), secondPage = await page({ offset: 20 }), full = await page({ limit: 100 }) + assert.equal(firstPage.total, 22) + assert.equal(firstPage.ranking.length, 20) + assert.equal(firstPage.candidateDiagnostics.length, 20) + assert.equal(firstPage.nextOffset, 20) + assert.equal(secondPage.nextOffset, null) + assert.deepEqual([...firstPage.ranking, ...secondPage.ranking], full.ranking) + assert.equal(secondPage.currentCandidateId, full.currentCandidateId) + const eligible = await page({ eligibility: "eligible" }), ineligible = await page({ eligibility: "ineligible", limit: 100 }) + assert.deepEqual(eligible.ranking, full.ranking.filter(row => row.eligible)) + assert.deepEqual(ineligible.ranking, full.ranking.filter(row => !row.eligible)) + await assert.rejects(page({ limit: 101 })) + assert.equal(writes, beforeWrites) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [compareAccess.connection.id])).rows[0].n, 0) + assert.deepEqual(privateMarkerPaths(result), []) + console.log("✓ Route AI comparison: explicit observer scope, invalid IDs and hidden dependencies rejected before RouterOS, analysis parity and no plan or write") + } finally { + await pool.query("DELETE FROM mcp_connections WHERE id=ANY($1::uuid[])", [[compareAccess.connection.id, analyzeAccess.connection.id]]) + } + for (const iface of ["wan", "hop"]) { + const observationId = randomUUID() + await pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source) VALUES($1,$2,$2,$3,'peer','ipv4','main',1,0,100,100,now(),now(),true,'fixture')", [observationId, sid, iface]) + await attestRoutingFixture(observationId) + } + const writeAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Main Route context write", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write", "mm:filters:write"], scope: { serverIds: [sid], operationIds: ["routing.analyze", "routing.community.switch", "routing.profiles.save"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), writerActor) + writeConnectionId = writeAccess.connection.id + const writer = await authenticateMcpToken(writeAccess.token) + const automatic = routeAiProfileSchema.parse({ ...mainProfile, autoRecovery: true, candidates: mainProfile.candidates.map(candidate => ({ ...candidate, controlProbe: base.candidates[0].controlProbe })) }) + const unavailable = await invokeMcpTool(writer, "mm_route_optimizer_analyze", { profile: automatic }) as { executionAvailable: boolean; executionReason: string } + assert.equal(unavailable.executionAvailable, false); assert.equal(unavailable.executionReason, "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE") + await assert.rejects(invokeMcpTool(writer, "mm_route_optimizer_plan", { profile: automatic, candidateId: "b" }), (error: Error & { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY" && error.message.includes("ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE")) + await assert.rejects(invokeMcpTool(writer, "mm_route_profile_save_plan", { profile: automatic, enabled: true, expiresAt: new Date(Date.now() + 900000).toISOString() }), (error: Error & { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY" && error.message.includes("ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE")) + assert.equal(writes, 0) + const plan = await invokeMcpTool(writer, "mm_route_optimizer_plan", { profile: mainProfile, candidateId: "b" }) as { planId: string } + assert.deepEqual(privateMarkerPaths(plan), [], "initial plan response contains private native data") + assert.deepEqual(privateMarkerPaths(await publicPlan(await loadPlan(plan.planId))), [], "retrieved public plan contains private native data") + assert.equal((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0].evidence.recovery, undefined) + await approvePlan(plan.planId, writerActor, true) + const operation = await applyPlan(writer, plan.planId, "route-context-main-comment") + let applied: { state: string; error_code?: string } | undefined + for (let attempt = 0; attempt < 300; attempt++) { + applied = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (applied && !["pending", "running"].includes(applied.state)) break + await delay(30) + } + assert.equal(applied?.state, "applied", JSON.stringify(applied)); assert.equal(writes, 1); assert.equal(policyComment, legacyComment) + const afterSwitch = await invokeMcpTool(writer, "mm_route_optimizer_analyze", { profile: mainProfile }) as { currentCandidateId: string | null; executionAvailable: boolean } + assert.equal(afterSwitch.currentCandidateId, "b"); assert.equal(afterSwitch.executionAvailable, true) + const writerResults = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [writeConnectionId])).rows + assert.deepEqual(privateMarkerPaths(writerResults), [], "request result contains private native data at these JSON paths") + assert.ok(!JSON.stringify(writerResults).includes("NEVER_")) + console.log("✓ Route AI context SQL+HTTP: independent table/BGP/probe VRF, exact effective input binding/inheritance/family/candidate ambiguity, shared-chain dependencies, private signed path checks, endpoint/dependency/tamper drift, return-path unknown, UTF-8 byte cap, live revocation; read-only/non-main/recovery guards made zero writes; approved main switch made one PATCH with verified read-back and preserved legacy owned comment") + } finally { + if (writeConnectionId) await setConnectionStatus(writeConnectionId, "paused", writerActor) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=$1", [sid]) + router.closeAllConnections(); await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/routing-community-context-compatibility-acceptance.ts b/backend/src/modules/mcp/routing-community-context-compatibility-acceptance.ts new file mode 100644 index 00000000..ffa896c9 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-context-compatibility-acceptance.ts @@ -0,0 +1,191 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { managedComment } from "../../managed-markers.js" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js" +import { invokeMcpTool } from "./runtime.js" +import { readRouteCommunityContext } from "./routing-community-context.js" + +/** Disposable SQL/HTTP fixtures only. No RouterOS or production requests. */ +export async function acceptRouteCommunityContextCompatibilityMcp(actor: AuthUser, { legacyOnly = false } = {}) { + const sid = 139, reserveId = 140 + let disabledInput = "unrelated", disabledOutput = "", disabledNlri = "", disabledSelect = "", inherited = false, missingParent = false, cyclicParent = false + let disabledFlag: string | undefined = "true", disabledGetter: string | undefined, selectedDisabled = false, currentDisabled = false + let version = "7.21.1 (stable)", resourceFailure = false, instanceReads = 0, writes = 0, pings = 0, traces = 0, reserveReads = 0 + let fixtureTable = "main" + let reserveAddressUnavailable = false, reserveCatalogMalformed = false, reserveJsonMalformed = false, reserveStatus = 200, mutateReserveSource = false, disabledOpaque = "baseline" + const body = compileManagedFilterRules([{ community: "65000:10", action: "route", gateway: "192.0.2.1", outIface: "", description: "" }]) + const selected = () => ({ ".id": "*c", name: "selected", disabled: String(selectedDisabled), instance: "default", vrf: "main", "routing-table": "main", afi: "ip", "input.filter": "bgp-in", "output.filter-chain": "", templates: "" }) + const rows = (path: string, reserve: boolean): Record[] => ({ + "/routing/table": [{ ".id": "*t", name: "main", fib: "true" }, { ".id": "*t2", name: "tenant", fib: "true" }], + "/ip/vrf": [{ ".id": "*v2", name: "tenant", interfaces: fixtureTable === "tenant" ? "wan,reserve-wan" : "none", disabled: "false" }, { ".id": "*v", name: "main", interfaces: "all", disabled: "false" }], + "/routing/bgp/connection": [selected(), { ".id": "*d", name: "disabled-unrelated", ...(disabledFlag === undefined ? {} : { disabled: disabledFlag }), instance: "deleted-instance", opaque: disabledOpaque, templates: inherited ? "parent" : "", ...(!inherited ? { "input.filter": disabledInput, "output.filter-chain": disabledOutput, "input.filter-nlri": disabledNlri, "output.filter-select": disabledSelect } : {}) }], + "/routing/bgp/template": inherited && !missingParent ? [{ ".id": "*p", name: "parent", disabled: "false", templates: cyclicParent ? "parent" : "", "input.filter": disabledInput, "output.filter-chain": disabledOutput, "input.filter-nlri": disabledNlri, "output.filter-select": disabledSelect }] : [], + "/routing/bgp/instance": [{ ".id": "*i", name: "default", disabled: "false", vrf: "main", "routing-table": "main" }], + "/routing/filter/rule": [{ ".id": "*f", chain: "bgp-in", disabled: "false", comment: managedComment("compatibility"), rule: body }], + "/ip/route": [1, 2].map(index => ({ ".id": "*r" + index, "dst-address": "192.0.2." + index + "/32", "routing-table": fixtureTable, disabled: String(index === 1 && currentDisabled), comment: managedComment("recursive") })), + "/interface": ["wan", "hop", "target", "reserve-wan"].map((name, index) => ({ ".id": "*if" + index, name, disabled: "false" })), + "/ip/address": reserve && reserveAddressUnavailable ? [] : ["wan", "hop", "target", "reserve-wan"].map((name, index) => ({ ".id": "*a" + index, interface: name, address: (reserve ? "198.51.100." : "192.0.2.") + (index + 1) + "/32", disabled: "false" })), + } as Record[]>)[path] ?? [] + const router = createServer(async (req, res) => { + res.setHeader("content-type", "application/json") + const reserve = req.headers.authorization === "Basic " + Buffer.from("reserve:fixture").toString("base64") + if (reserve) reserveReads++ + let raw = ""; for await (const chunk of req) raw += chunk + const args = raw ? JSON.parse(raw) : {}, path = (req.url ?? "").replace(/^\/rest/, "") + if (reserve && reserveStatus !== 200) { res.statusCode = reserveStatus; res.end("NEVER_RESERVE_AUTH_PROVIDER_BODY"); return } + if (reserve && reserveJsonMalformed) { res.end("{NEVER_RESERVE_MALFORMED_JSON"); return } + if (mutateReserveSource && reserve) { mutateReserveSource = false; await pool.query("UPDATE servers SET password='changed-fixture' WHERE id=$1", [reserveId]) } + if (path === "/system/resource") { if (resourceFailure) res.statusCode = 404; res.end(JSON.stringify(resourceFailure ? { message: "unavailable" } : { version })); return } + if (path === "/routing/bgp/instance") instanceReads++ + if (req.method === "GET") { res.end(JSON.stringify(reserve && reserveCatalogMalformed ? { unexpected: "catalog" } : rows(path, reserve))); return } + if (req.method === "POST" && path.endsWith("/get")) { + const row = rows(path.slice(0, -4), reserve).find(row => row[".id"] === args[".id"]) + const value = args[".id"] === "*d" && args["value-name"] === "disabled" && disabledFlag === undefined ? disabledGetter : row?.[args["value-name"]] + res.end(JSON.stringify(value === undefined ? [] : { ret: value })); return + } + if (req.method === "POST" && path === "/tool/ping") { pings++; res.end(JSON.stringify([{ sent: "3", received: "3", "packet-loss": "0", "avg-rtt": "1ms" }])); return } + if (req.method === "POST" && path === "/tool/traceroute") { traces++; res.end(JSON.stringify(["192.0.2.2", "192.0.2.3"].map(address => ({ address, sent: "1", loss: "0" })))); return } + writes++; res.statusCode = 405; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined + try { + const port = (router.address() as { port: number }).port + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,'context-compatibility','127.0.0.1',$3,false,'primary','fixture'),($2,'context-reserve','127.0.0.1',$3,false,'reserve','fixture')", [sid, reserveId, port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Context compatibility", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [sid, reserveId], operationIds: ["routing.analyze", "routing.path.verify"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const primary = { id: "a", jumpHostId: sid, exitNodeId: sid, wanInterface: "wan", downstreamInterface: "hop", gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*r1" }, controlProbe: { targetInterface: "target", expectedHops: [{ serverId: sid, interface: "hop" }, { serverId: sid, interface: "target" }] } } + const base = routeAiProfileSchema.parse({ name: "compatibility", serverId: sid, community: "65000:10", family: "ipv4", candidates: [primary, { ...primary, id: "b", jumpHostId: reserveId, exitNodeId: reserveId, wanInterface: "reserve-wan", gateway: "192.0.2.2", gatewayRef: { resource: "ipv4_route", id: "*r2" }, controlProbe: { targetInterface: "target", expectedHops: [{ serverId: reserveId, interface: "hop" }, { serverId: reserveId, interface: "target" }] } }] }) + const read = (selectedCandidateId?: string) => readRouteCommunityContext(base, { selectedCandidateId }) + if (legacyOnly) { + version = "7.19.3 (stable)" + const writer = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Legacy BGP writer", mode: "approval", + expiresAt: new Date(Date.now() + 600000).toISOString(), permissions: ["mm:network:write", "mm:network:read", "mm:filters:write"], + scope: { serverIds: [sid, reserveId], operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "routing.profiles.save"], allowGlobal: false }, + }), actor) + try { + const writerPrincipal = await authenticateMcpToken(writer.token) + const analysis = await invokeMcpTool(writerPrincipal, "mm_route_optimizer_analyze", { profile: base }) as { + executionAvailable: boolean; executionReason: string + } + assert.equal(analysis.executionAvailable, false) + assert.equal(analysis.executionReason, "LEGACY_BGP_READ_ONLY") + const nativeBefore = { pings, traces, writes, instanceReads } + await assert.rejects(invokeMcpTool(writerPrincipal, "mm_route_optimizer_plan", { profile: base, candidateId: "b" }), + { code: "UNSUPPORTED_CAPABILITY", message: "LEGACY_BGP_READ_ONLY" }) + const enableController = (profile: typeof base) => invokeMcpTool(writerPrincipal, "mm_route_profile_save_plan", + { profile, enabled: true, expiresAt: new Date(Date.now() + 300000).toISOString() }) + const legacyRefusal = { code: "UNSUPPORTED_CAPABILITY", message: "LEGACY_BGP_READ_ONLY" } + await assert.rejects(enableController(base), legacyRefusal) + const scoped = routeAiProfileSchema.parse({ ...base, table: "tenant", probeVrf: "tenant", + bgpContext: { connectionId: "*c", instance: "default", inputChain: "bgp-in", vrf: "tenant" }, + candidates: base.candidates.map(candidate => ({ ...candidate, gateway: candidate.gateway + "@tenant", + controlProbe: { ...candidate.controlProbe, sourceAddress: candidate.id === "a" ? "192.0.2.1" : "192.0.2.4" } })) }) + fixtureTable = "tenant" + await assert.rejects(invokeMcpTool(writerPrincipal, "mm_route_optimizer_plan", { profile: scoped, candidateId: "b" }), + { code: "UNSUPPORTED_CAPABILITY", message: "LEGACY_BGP_READ_ONLY" }) + await assert.rejects(enableController(scoped), legacyRefusal) + assert.deepEqual({ pings, traces, writes, instanceReads }, nativeBefore) + for (const table of ["mcp_plans", "mcp_operations"] as const) { + assert.equal((await pool.query(`SELECT 1 FROM ${table} WHERE connection_id=$1`, [writer.connection.id])).rowCount, 0) + } + console.log("Legacy BGP MCP: analysis works and the write-enabled planner refuses before plan, probe or native write") + } finally { await setConnectionStatus(writer.connection.id, "paused", actor) } + return + } + const initial = await read("a") + assert.equal(initial.before.executionAvailable, true); assert.equal(initial.before.currentCandidateId, "a") + assert.deepEqual(initial.before.disabledConnectionIds, ["*d"]) + assert.ok(initial.before.candidateDiagnostics.every(row => row.status === "available")) + disabledOpaque = "drift" + assert.notEqual((await read()).before.dependencyProof, initial.before.dependencyProof, "ignored disabled rows remain sealed dependencies") + disabledOpaque = "baseline"; disabledInput = "bgp-in" + assert.equal((await read()).before.executionAvailable, false, "disabled peer input reference prevents a private write") + disabledInput = "unrelated"; disabledOutput = "bgp-in" + assert.equal((await read()).before.executionAvailable, false, "disabled peer output reference prevents a private write") + disabledOutput = ""; disabledNlri = "bgp-in" + assert.equal((await read()).before.executionAvailable, false, "disabled peer NLRI reference prevents a private write") + disabledNlri = ""; disabledSelect = "bgp-in" + assert.equal((await read()).before.executionAvailable, false, "disabled peer select reference prevents a private write") + disabledSelect = ""; inherited = true + assert.equal((await read()).before.executionAvailable, true) + disabledInput = "bgp-in" + assert.equal((await read()).before.executionAvailable, false, "inherited dormant input chain remains shared") + disabledInput = "unrelated"; disabledOutput = "bgp-in" + assert.equal((await read()).before.executionAvailable, false, "inherited dormant output chain remains shared") + disabledOutput = ""; missingParent = true + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + missingParent = false; cyclicParent = true + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + cyclicParent = false; inherited = false; disabledFlag = undefined + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE", "unknown disabled is never skipped") + disabledGetter = "true" + assert.equal((await read()).before.executionAvailable, true, "omitted disabled requires a positively observed effective true") + disabledGetter = undefined + disabledFlag = "true"; selectedDisabled = true + assert.equal((await read()).before.contextVerified, false, "disabled selected binding cannot verify context") + selectedDisabled = false; reserveAddressUnavailable = true + const unavailable = await read("a") + assert.equal(unavailable.before.executionAvailable, true) + assert.equal(unavailable.before.currentCandidateId, "a") + assert.equal(unavailable.before.candidateDiagnostics[1].status, "candidate-unavailable") + assert.equal(unavailable.probeEndpoints.has("b"), false) + await assert.rejects(read("b"), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + const failedProbe = await invokeMcpTool(principal, "mm_route_path_verify", { profile: base, candidateId: "b" }) as { status: string } + assert.equal(failedProbe.status, "unknown"); assert.equal(pings, 0, "unavailable candidate cannot send probes") + const primaryProbe = await invokeMcpTool(principal, "mm_route_path_verify", { profile: base, candidateId: "a" }) as { status: string } + assert.equal(primaryProbe.status, "passed") + reserveAddressUnavailable = false; currentDisabled = true + const away = await read("b") + assert.equal(away.before.currentCandidateId, "a", "configured failed path identity survives readiness loss") + assert.equal(away.before.executionAvailable, true); assert.equal(away.before.candidateDiagnostics[0].status, "candidate-unavailable") + currentDisabled = false + await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [reserveId]) + const beforeDisabledReads = reserveReads, disabledSource = await read("a") + assert.equal(disabledSource.before.candidateDiagnostics[1].status, "candidate-unavailable"); assert.equal(reserveReads, beforeDisabledReads) + await assert.rejects(read("b")) + await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [reserveId]) + await assert.rejects(disabledSource.fence(), (error: { code?: string }) => error.code === "STATE_CONFLICT", "unavailable source epoch is fenced") + reserveCatalogMalformed = true + await assert.rejects(read("a"), (error: { code?: string }) => error.code === "INVALID_NATIVE_RESPONSE", "malformed catalog is not silently skipped") + reserveCatalogMalformed = false + const beforeInvalidPings = pings + for (const status of [401, 403]) { + reserveStatus = status + await assert.rejects(read("a"), (error: Error & { code?: string }) => error.code === "SOURCE_AUTH_FAILED" && !error.message.includes("NEVER_"), "reserve authentication failure invalidates the entire context") + const probe = await invokeMcpTool(principal, "mm_route_path_verify", { profile: base, candidateId: "a" }) as { status: string; reason: string } + assert.equal(probe.status, "unknown"); assert.equal(probe.reason, "SOURCE_AUTH_FAILED") + } + reserveStatus = 200; reserveJsonMalformed = true + await assert.rejects(read("a"), (error: Error & { code?: string }) => error.code === "INVALID_NATIVE_RESPONSE" && !error.message.includes("NEVER_"), "malformed reserve JSON invalidates the entire context") + const malformedProbe = await invokeMcpTool(principal, "mm_route_path_verify", { profile: base, candidateId: "a" }) as { status: string; reason: string } + assert.equal(malformedProbe.status, "unknown"); assert.equal(malformedProbe.reason, "INVALID_NATIVE_RESPONSE") + assert.equal(pings, beforeInvalidPings, "fatal reserve source failures cannot send even primary probes") + reserveJsonMalformed = false; mutateReserveSource = true + await assert.rejects(read("a"), (error: { code?: string }) => error.code === "STATE_CONFLICT", "late source drift remains fatal") + await pool.query("UPDATE servers SET password='fixture' WHERE id=$1", [reserveId]) + version = "7.19.3 (stable)"; const beforeLegacyInstanceReads = instanceReads + const legacy = await read("a") + assert.equal(legacy.before.bgpContextMode, "legacy-read-only"); assert.equal(legacy.before.contextVerified, true) + assert.equal(legacy.before.instance, null); assert.equal(legacy.before.executionReason, "LEGACY_BGP_READ_ONLY") + assert.equal(legacy.before.scopedExecutionAvailable, false); assert.equal(instanceReads, beforeLegacyInstanceReads) + const named = routeAiProfileSchema.parse({ ...base, bgpContext: { connectionId: "*c", instance: "default", inputChain: "bgp-in" } }) + assert.equal((await readRouteCommunityContext(named)).before.contextVerified, false, "legacy never invents the requested named instance") + version = "unknown" + assert.equal((await read()).before.bgpContextMode, "explicit-instance", "unknown version requires a proven actual instance graph") + resourceFailure = true + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE", "404 never detects legacy") + assert.equal(writes, 0) + console.log("✓ Route context compatibility: disabled unrelated BGP references/inheritance/raw seals, per-candidate readiness with current identity, unavailable endpoint fencing/no probes, selected-candidate requirement, source drift/malformed catalog rejection, positively detected legacy read-only mode without invented instances or writes") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=ANY($1::bigint[])", [[sid, reserveId]]) + router.closeAllConnections(); await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/routing-community-context-model.test.ts b/backend/src/modules/mcp/routing-community-context-model.test.ts new file mode 100644 index 00000000..587f6579 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-context-model.test.ts @@ -0,0 +1,63 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { managedComment } from "../../managed-markers.js" +import { candidateMatchesGateway, candidateIdentifiesGateway, recognizedCommunityGateway, routeContextExecutionReason, routeGatewayContext, routeBgpContextMode, routeCandidateAvailabilityError } from "./routing-community-context-model.js" + +const profile = routeAiProfileSchema.parse({ name: "context", serverId: 1, community: "65000:10", family: "ipv4", candidates: [{ id: "a", wanInterface: "wan", downstreamInterface: "down", jumpHostId: 2, exitNodeId: 3, gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*a" } }] }) +const body = compileManagedFilterRules([{ community: profile.community, action: "route", gateway: "192.0.2.1", outIface: "", description: "" }]) +const native = { ".id": "*f", chain: "bgp-in", disabled: "false", comment: managedComment("fixture"), rule: body } + +test("legacy main remains explicit; route installation table never selects diagnostic VRF", () => { + assert.equal(profile.probeVrf, "main") + const nonmain = routeAiProfileSchema.parse({ ...profile, table: "policy-only", bgpContext: { connectionId: "*c", instance: "default", inputChain: "bgp-in" } }) + assert.equal(nonmain.probeVrf, "main") + assert.equal(routeContextExecutionReason(nonmain), "SCOPED_ROUTE_EXECUTOR_UNAVAILABLE") + assert.equal(routeContextExecutionReason(profile), null) + assert.equal(routeAiProfileSchema.safeParse({ ...profile, autoRecovery: true }).success, false, "recovery admission requires probes before the executor guard is evaluated") + const automatic = routeAiProfileSchema.parse({ ...profile, autoRecovery: true, candidates: profile.candidates.map(candidate => ({ ...candidate, controlProbe: { targetInterface: "target", expectedHops: [{ serverId: candidate.jumpHostId, interface: "down" }, { serverId: candidate.exitNodeId, interface: "target" }] } })) }) + assert.equal(routeContextExecutionReason(automatic), "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE") + const transport = routeAiProfileSchema.parse({ ...profile, bgpContext: { connectionId: "*c", instance: "default", inputChain: "bgp-in", vrf: "bgp-only" } }) + assert.equal(transport.probeVrf, "main") + assert.equal(routeContextExecutionReason(transport), "SCOPED_BINDING_EXECUTOR_UNAVAILABLE") +}) +test("current gateway requires exact owned compiler output and correct family; opaque flow never becomes proof", () => { + assert.deepEqual(recognizedCommunityGateway([native], "bgp-in", profile.community, "ipv4"), { gateway: "192.0.2.1", gatewayInterface: "" }) + assert.equal(recognizedCommunityGateway([native], "bgp-in", profile.community, "ipv6"), null) + for (const row of [{ ...native, rule: "accept;\n" + body }, { ...native, rule: body + "\nreject;" }, { ...native, disabled: undefined }, { ...native, comment: "external" }, { ...native, rule: body.replace("bgp-communities includes", "afi ipv6 && bgp-communities includes") }]) assert.equal(recognizedCommunityGateway([row], "bgp-in", profile.community, "ipv4"), null) + assert.equal(recognizedCommunityGateway([native, { ...native, ".id": "*other" }], "bgp-in", profile.community, "ipv4"), null) +}) +test("same address in another resolver/table/resource cannot identify the current candidate", () => { + const current = { gateway: "192.0.2.1", gatewayInterface: "" }, candidate = profile.candidates[0] + const route = { ".id": "*a", "dst-address": "192.0.2.1/32", "routing-table": "main", disabled: "false", comment: managedComment("recursive") } + assert.equal(candidateMatchesGateway(profile, candidate, current, route), true) + assert.equal(candidateMatchesGateway(profile, candidate, current, { ...route, "routing-table": "tenant" }), false) + assert.equal(candidateMatchesGateway(profile, { ...candidate, gatewayRef: { resource: "ipv6_route", id: "*a" } }, current, route), false) + assert.equal(candidateMatchesGateway(profile, candidate, current, { ...route, disabled: undefined }), false) + for (const destination of ["192.0.2.1/24", "192.0.2.1", "192.0.2.1/0", "192.0.2.1/32/extra"]) assert.equal(candidateMatchesGateway(profile, candidate, current, { ...route, "dst-address": destination }), false, "only an exact recursive host route proves this gateway") + assert.deepEqual(routeGatewayContext("192.0.2.1%gre-a@resolver"), { address: "192.0.2.1", interface: "gre-a", family: 4, resolverTable: "resolver" }) + assert.deepEqual(routeGatewayContext("2001:db8::1@resolver"), { address: "2001:db8::1", interface: null, family: 6, resolverTable: "resolver" }) + for (const gateway of ["192.0.2.1@tenant@main", "192.0.2.1,192.0.2.2", "host@tenant"]) assert.throws(() => routeGatewayContext(gateway)) +}) + +test("a failed configured path retains exact identity without claiming availability", () => { + const candidate = profile.candidates[0], current = { gateway: "192.0.2.1", gatewayInterface: "" } + const failed = { ".id": "*a", "dst-address": "192.0.2.1/32", "routing-table": "main", disabled: "true", invalid: "true", comment: managedComment("recursive") } + assert.equal(candidateIdentifiesGateway(profile, candidate, current, failed), true) + assert.equal(candidateMatchesGateway(profile, candidate, current, failed), false) + assert.equal(candidateIdentifiesGateway(profile, candidate, current, { ...failed, ".id": "*other" }), false) + assert.equal(candidateIdentifiesGateway(profile, candidate, current, { ...failed, "routing-table": "other" }), false) +}) + +test("legacy BGP mode requires an explicitly observed pre-7.20 stable version", () => { + for (const version of ["7.1", "7.19.3", "7.19.3 (stable)", "7.15.2 (long-term)"]) assert.equal(routeBgpContextMode({ version }), "legacy-read-only") + assert.equal(routeBgpContextMode([{ version: "7.19.3 (stable)" }]), "legacy-read-only") + for (const value of [[], null, {}, { version: "6.49.18" }, { version: "7.20" }, { version: "7.20rc1" }, { version: "7.19beta1" }, { version: "7.19garbage" }, [{ version: "7.19" }, { version: "7.19" }], { version: "8.0" }]) assert.equal(routeBgpContextMode(value), "explicit-instance") +}) + +test("only availability failures are local to a candidate; invalidation remains global", () => { + for (const code of ["STATE_UNAVAILABLE", "DEVICE_UNAVAILABLE", "MEASUREMENT_UNKNOWN", "ECONNREFUSED", "ETIMEDOUT"]) assert.equal(routeCandidateAvailabilityError({ code }), true) + for (const code of ["STATE_CONFLICT", "PERMISSION_DENIED", "SOURCE_AUTH_FAILED", "INVALID_NATIVE_RESPONSE", "RESET_REQUIRED", "READ_CANCELLED", "LIMIT_EXCEEDED", "EACCES"]) assert.equal(routeCandidateAvailabilityError({ code }), false) + assert.equal(routeCandidateAvailabilityError(new Error("opaque")), false) +}) diff --git a/backend/src/modules/mcp/routing-community-context-model.ts b/backend/src/modules/mcp/routing-community-context-model.ts new file mode 100644 index 00000000..1c69e50f --- /dev/null +++ b/backend/src/modules/mcp/routing-community-context-model.ts @@ -0,0 +1,67 @@ +import { isIP } from "node:net" +import type { RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { hasManagedCommentPrefix } from "../../managed-markers.js" +import { compileManagedFilterRules, filterRuleBranches, filterRuleToken } from "../../services/filter-rules-write-model.js" +import { canonicalRoutingFilterBody } from "../../services/routing-filter-body.js" + +/** Only a positive RouterOS v7 version observation selects the legacy shape. + * Missing resources, HTTP failures and malformed versions are never evidence. */ +export function routeBgpContextMode(resource: unknown): "legacy-read-only" | "explicit-instance" { + const row = Array.isArray(resource) && resource.length === 1 ? resource[0] : resource + const version = row && typeof row === "object" && "version" in row ? row.version : null + const match = typeof version === "string" ? /^7\.(\d+)(?:\.\d+)?(?:\s+\((?:stable|long-term|testing|development)\))?$/.exec(version) : null + return match && Number(match[1]) < 20 ? "legacy-read-only" : "explicit-instance" +} + +export function routeCandidateAvailabilityError(error: unknown): boolean { + const code = error && typeof error === "object" && "code" in error ? error.code : undefined + // Source/authorization/cancellation/budget failures must invalidate the batch. + return ["STATE_UNAVAILABLE", "DEVICE_UNAVAILABLE", "MEASUREMENT_UNKNOWN", "ECONNREFUSED", "ECONNRESET", "ENOTFOUND", "EHOSTUNREACH", "ENETUNREACH", "ETIMEDOUT"].includes(String(code)) +} + +export const routeContextExecutionReason = (profile: RouteAiProfile) => profile.autoRecovery ? "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE" : profile.table !== "main" || profile.probeVrf !== "main" + ? "SCOPED_ROUTE_EXECUTOR_UNAVAILABLE" : profile.bgpContext && (profile.bgpContext.inputChain !== "bgp-in" || profile.bgpContext.vrf !== "main") ? "SCOPED_BINDING_EXECUTOR_UNAVAILABLE" : null + +/** @table resolves the nexthop; it does not name the route installation table. */ +export function routeGatewayContext(gateway: string) { + const match = /^([^@%\s,]+)(?:%([^@%\s,]+))?(?:@([^@%\s,]+))?$/.exec(gateway) + if (!match || !isIP(match[1])) throw new Error("Gateway context unconfirmed") + return { address: match[1], family: isIP(match[1]), interface: match[2] ?? null, resolverTable: match[3] ?? "main" } +} + +/** Recognize only the application's complete generated filter language. Hints + * extracted from arbitrary RouterOS code are never current-path evidence. */ +export function recognizedCommunityGateway(rows: Record[], chain: string, community: string, family: "ipv4" | "ipv6") { + const selected = rows.filter(row => row.chain === chain) + if (selected.length !== 1 || !hasManagedCommentPrefix(String(selected[0].comment ?? "")) || !["false", "no", false].includes(selected[0].disabled as string | boolean) || ["true", "yes", true].includes(selected[0].invalid as string | boolean) || typeof selected[0].rule !== "string") return null + const body = selected[0].rule, branches = filterRuleBranches(body), rules: { community: string; action: "route" | "blackhole"; gateway: string; outIface: string; description: string }[] = [] + try { + for (const branch of branches) { + if (!/^\s*bgp-communities includes \d+:\d+(?:\s*\|\|\s*bgp-communities includes \d+:\d+)*\s*$/.test(branch.condition)) return null + const communities = [...branch.condition.matchAll(/bgp-communities includes (\d+:\d+)/g)].map(match => match[1]) + const blackhole = /^\s*set blackhole yes;\s*accept;\s*$/.test(branch.body) + for (const item of communities) rules.push({ community: item, action: blackhole ? "blackhole" : "route", gateway: blackhole ? "" : filterRuleToken(branch.body, "gw"), outIface: blackhole ? "" : filterRuleToken(branch.body, "interface"), description: "" }) + } + const canonical = (text: string) => canonicalRoutingFilterBody(text).replace(/\r\n/g, "\n").trim() + if (!rules.length || canonical(compileManagedFilterRules(rules)) !== canonical(body)) return null + const current = rules.filter(rule => rule.community === community && rule.action === "route") + if (current.length !== 1 || isIP(current[0].gateway) !== (family === "ipv4" ? 4 : 6)) return null + return { gateway: current[0].gateway, gatewayInterface: current[0].outIface } + } catch { return null } +} + +export function candidateMatchesGateway(profile: RouteAiProfile, candidate: RouteAiProfile["candidates"][number], current: { gateway: string; gatewayInterface: string }, row: Record | undefined) { + if (!row || !["false", "no", false].includes(row.disabled as string | boolean) || ["true", "yes", true].includes(row.invalid as string | boolean)) return false + return candidateIdentifiesGateway(profile, candidate, current, row) +} + +/** Policy identity does not assert that its currently configured route is usable. */ +export function candidateIdentifiesGateway(profile: RouteAiProfile, candidate: RouteAiProfile["candidates"][number], current: { gateway: string; gatewayInterface: string }, row: Record | undefined) { + if (!row || row[".id"] !== candidate.gatewayRef.id) return false + try { + const gateway = routeGatewayContext(candidate.gateway) + if (gateway.family !== (profile.family === "ipv4" ? 4 : 6) || candidate.gateway !== current.gateway) return false + if (candidate.gatewayRef.resource === "gre") return row.name === current.gatewayInterface && gateway.interface === null && gateway.resolverTable === "main" + return candidate.gatewayRef.resource === (profile.family === "ipv4" ? "ipv4_route" : "ipv6_route") && row["routing-table"] === gateway.resolverTable && row["dst-address"] === gateway.address + (gateway.family === 4 ? "/32" : "/128") && current.gatewayInterface === "" && hasManagedCommentPrefix(String(row.comment ?? "")) + } catch { return false } +} diff --git a/backend/src/modules/mcp/routing-community-context-native.test.ts b/backend/src/modules/mcp/routing-community-context-native.test.ts new file mode 100644 index 00000000..8d2bb300 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-context-native.test.ts @@ -0,0 +1,86 @@ +import assert from "node:assert/strict" +import { createServer, type RequestListener } from "node:http" +import { test } from "node:test" +import { routeContextClients } from "./routing-community-context-native.js" +import type { RoutingEndpoint } from "./routing-observation-source.js" + +async function fixture(handler: RequestListener) { + const server = createServer(handler) + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const endpoint: RoutingEndpoint = { id: 1, host: "127.0.0.1", port: (server.address() as { port: number }).port, username: "fixture", password: "private", useSsl: false, verifySsl: false, enabled: true, routingEndpointEpoch: "00000000-0000-4000-8000-000000000001" } + return { endpoint, close: async () => { server.closeAllConnections(); await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())) } } +} + +test("native context rejects UTF-8 bytes before JSON parse and bounds the whole batch", async () => { + let huge = true + const router = await fixture((_req, res) => { res.setHeader("content-type", "application/json"); const body = huge ? JSON.stringify("я".repeat(600000)) : JSON.stringify("x".repeat(900000)); res.write(body.slice(0, 1)); res.end(body.slice(1)) }) + try { + const client = routeContextClients([router.endpoint]).get(1)! + await assert.rejects(client.get("/routing/table"), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + huge = false + const bounded = routeContextClients([router.endpoint]).get(1)! + for (let index = 0; index < 9; index++) assert.equal((await bounded.get("/routing/table")).length, 900000) + await assert.rejects(bounded.get("/routing/table"), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + } finally { await router.close() } +}) + +test("native context cancellation drains an in-flight response; pre-cancel and writes make no request", async () => { + let requests = 0, began: (() => void) | undefined + const router = await fixture((_req, res) => { requests++; res.writeHead(200, { "content-type": "application/json" }); res.write("["); began?.() }) + try { + const controller = new AbortController(), client = routeContextClients([router.endpoint], controller.signal).get(1)! + for (const path of ["/routing/filter/rule/set?x=/print", "/routing/filter/rule/set#x=/get"]) { + assert.throws(() => client.post(path, { rule: "accept" }), { code: "PERMISSION_DENIED" }) + } + assert.equal(requests, 0) + const started = new Promise(resolve => { began = resolve }) + const pending = client.get("/routing/table") + const rejected = assert.rejects(pending) + await Promise.race([started, pending.then(() => { throw new Error("response completed before cancellation") })]) + controller.abort() + await rejected + await assert.rejects(client.get("/routing/table"), (error: { code?: string }) => error.code === "READ_CANCELLED") + assert.throws(() => client.post("/ip/route/add", { gateway: "192.0.2.1" }), (error: { code?: string }) => error.code === "PERMISSION_DENIED") + assert.equal(requests, 1) + } finally { await router.close() } +}) + +test("native context does not follow redirects or expose their response bodies", async () => { + let requests = 0 + const router = await fixture((_req, res) => { requests++; res.writeHead(302, { location: "/rest/secret" }); res.end("NEVER_NATIVE_CONTEXT_BODY") }) + try { + await assert.rejects(routeContextClients([router.endpoint]).get(1)!.get("/routing/table"), (error: Error & { code?: string }) => error.code === "INVALID_NATIVE_RESPONSE" && !error.message.includes("NEVER_")) + assert.equal(requests, 1) + } finally { await router.close() } +}) + +test("native context refuses an oversized declared frame without waiting for its body", async () => { + const router = await fixture((_req, res) => { res.writeHead(200, { "content-length": "1048577" }); res.flushHeaders() }) + try { + await assert.rejects(routeContextClients([router.endpoint]).get(1)!.get("/routing/table"), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + } finally { await router.close() } +}) + +test("a candidate request timeout is unavailable while cancellation stays global", async () => { + const router = await fixture((_req, res) => { res.writeHead(200); res.write("[") }) + try { + await assert.rejects(routeContextClients([router.endpoint]).get(1)!.get("/interface", 50), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + } finally { await router.close() } +}) + +test("source authentication and malformed responses remain fatal to the context batch", async () => { + let status = 401, malformed = false + const router = await fixture((_req, res) => { res.writeHead(status); res.end(malformed ? "{NEVER_MALFORMED_BODY" : "NEVER_AUTH_BODY") }) + try { + for (const value of [401, 403, 407]) { + status = value + await assert.rejects(routeContextClients([router.endpoint]).get(1)!.get("/interface"), (error: Error & { code?: string }) => error.code === "SOURCE_AUTH_FAILED" && !error.message.includes("NEVER_")) + } + status = 200; malformed = true + await assert.rejects(routeContextClients([router.endpoint]).get(1)!.get("/interface"), (error: Error & { code?: string }) => error.code === "INVALID_NATIVE_RESPONSE" && !error.message.includes("NEVER_")) + status = 400; malformed = false + await assert.rejects(routeContextClients([router.endpoint]).get(1)!.get("/interface"), (error: { code?: string }) => error.code === "INVALID_NATIVE_RESPONSE") + status = 503 + await assert.rejects(routeContextClients([router.endpoint]).get(1)!.get("/interface"), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + } finally { await router.close() } +}) diff --git a/backend/src/modules/mcp/routing-community-context-native.ts b/backend/src/modules/mcp/routing-community-context-native.ts new file mode 100644 index 00000000..de6e868a --- /dev/null +++ b/backend/src/modules/mcp/routing-community-context-native.ts @@ -0,0 +1,67 @@ +import http from "node:http" +import https from "node:https" +import type { RoutingEndpoint } from "./routing-observation-source.js" +import { McpError } from "./security.js" +import { isIP } from "node:net" +import { captureRoutingNetworkDestinationPins, type RoutingNetworkNativeDestinationPin } from "./routing-network-native-destination.js" + +/** Trusted internal execution limits, never an HTTP tool input. */ +export type RouteContextNativeSessionLimits = Readonly<{ deadline: number; maxCalls: number; maxBytes: number; pins?: readonly RoutingNetworkNativeDestinationPin[] }> + +/** One acquisition/probe batch: actual UTF-8 bytes before JSON.parse, one + * deadline, no redirects/retries, and a fixed authenticated destination. */ +export function routeContextClients(endpoints: readonly RoutingEndpoint[], signal?: AbortSignal, sessionLimits?: RouteContextNativeSessionLimits) { + const now = Date.now() + if (sessionLimits && (!Number.isFinite(sessionLimits.deadline) || sessionLimits.deadline <= now || sessionLimits.deadline > now + 120000 || + !Number.isSafeInteger(sessionLimits.maxCalls) || sessionLimits.maxCalls < 1 || sessionLimits.maxCalls > 256 || + !Number.isSafeInteger(sessionLimits.maxBytes) || sessionLimits.maxBytes < 1 || sessionLimits.maxBytes > 8 * 1048576)) throw new McpError("LIMIT_EXCEEDED", "Некорректный внутренний бюджет native session", 409) + const deadline = sessionLimits?.deadline ?? now + 60000, maxCalls = sessionLimits?.maxCalls ?? 2000, maxBytes = sessionLimits?.maxBytes ?? 8 * 1048576 + const capturedEndpoints = sessionLimits?.pins === undefined ? endpoints : endpoints.map(endpoint => Object.freeze({ ...endpoint })) + const pins = sessionLimits?.pins === undefined ? undefined : captureRoutingNetworkDestinationPins(capturedEndpoints, sessionLimits.pins) + let bytes = 0, calls = 0 + const read = (endpoint: RoutingEndpoint, path: string, body?: Record, timeoutMs = 10000): Promise => new Promise((resolve, reject) => { + if (signal?.aborted || Date.now() >= deadline || bytes >= maxBytes || calls >= maxCalls) { reject(new McpError("READ_CANCELLED", "Лимит времени/запросов контекста пути", 409)); return } + calls++ + const transport = endpoint.useSsl ? https : http + const pin = pins?.get(endpoint.id), host = isIP(endpoint.host) === 6 ? "[" + endpoint.host + "]" : endpoint.host + const req = transport.request({ hostname: pin?.address ?? endpoint.host, port: endpoint.port, path: "/rest" + path, method: body ? "POST" : "GET", signal, + ...(pin ? { family: pin.family, servername: isIP(endpoint.host) ? undefined : endpoint.host } : {}), + rejectUnauthorized: endpoint.useSsl ? endpoint.verifySsl : undefined, + headers: { Authorization: "Basic " + Buffer.from(endpoint.username + ":" + endpoint.password).toString("base64"), "Content-Type": "application/json", + ...(pin ? { Host: host + (endpoint.port === (endpoint.useSsl ? 443 : 80) ? "" : ":" + endpoint.port) } : {}) }, + }, res => { + const chunks: Buffer[] = []; let received = 0 + res.on("error", reject) + res.once("aborted", () => reject(new McpError("STATE_UNAVAILABLE", "Ответ контекста пути прерван", 409))) + if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) { + const code = [401, 403, 407].includes(res.statusCode ?? 0) ? "SOURCE_AUTH_FAILED" + : [404, 408, 429, 500, 502, 503, 504].includes(res.statusCode ?? 0) ? "STATE_UNAVAILABLE" : "INVALID_NATIVE_RESPONSE" + reject(new McpError(code, "Источник контекста пути вернул неподдерживаемый ответ", 409)); res.destroy(); req.destroy(); return + } + if (Number(res.headers["content-length"] ?? 0) > 1048576) { reject(new McpError("LIMIT_EXCEEDED", "Бюджет байтов контекста пути превышен", 409)); res.destroy(); req.destroy(); return } + res.on("data", (chunk: Buffer) => { + received += chunk.length; bytes += chunk.length + if (received > 1048576 || bytes > maxBytes) { reject(new McpError("LIMIT_EXCEEDED", "Бюджет байтов контекста пути превышен", 409)); res.destroy(); req.destroy(); return } + chunks.push(chunk) + }) + res.on("end", () => { + clearTimeout(timer) + try { resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")) as T) } + catch { reject(new McpError("INVALID_NATIVE_RESPONSE", "Источник контекста пути вернул некорректный JSON", 409)) } + }) + }) + const remaining = Math.max(1, deadline - Date.now()), batchDeadline = remaining <= timeoutMs + const timer = setTimeout(() => req.destroy(new McpError(batchDeadline ? "READ_CANCELLED" : "STATE_UNAVAILABLE", "Истёк срок чтения контекста пути", 409)), Math.min(timeoutMs, remaining)) + req.once("error", error => { clearTimeout(timer); reject(error) }) + req.once("close", () => clearTimeout(timer)) + req.end(body ? JSON.stringify(body) : undefined) + }) + const clients = new Map(capturedEndpoints.map(endpoint => [endpoint.id, { + get: (path: string, timeoutMs = 10000) => read(endpoint, path, undefined, timeoutMs), + post: (path: string, body: Record, timeoutMs = 10000) => { + if (/[?#]/.test(path) || !path.endsWith("/get") && !path.endsWith("/print") && !["/tool/ping", "/tool/traceroute"].includes(path)) throw new McpError("PERMISSION_DENIED", "Контекст пути допускает только чтение и ограниченные probes", 403) + return read(endpoint, path, body, timeoutMs) + }, + }])) + return Object.assign(clients, { readBudget: () => Object.freeze({ deadline, calls, bytes, maxCalls, maxBytes }) }) +} diff --git a/backend/src/modules/mcp/routing-community-context.ts b/backend/src/modules/mcp/routing-community-context.ts new file mode 100644 index 00000000..f706e360 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-context.ts @@ -0,0 +1,293 @@ +import { isConfirmedRoutingFibTable } from "../../services/routing-table-fib.js" +import { prepareOriginalEffectSelection } from "./routing-original-effects.js" +import type { RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { nativeBgpRows, bgpTemplateReferences, type NativeBgpRow } from "../../services/bgp-export-model.js" +import { bgpFilterName, effectiveBgpFilter, stableBgpPolicyRow } from "../../services/bgp-filter-binding-model.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { canonicalVrfMembers } from "../../services/vrf-members.js" +import { hasPermission } from "../../lib/permissions.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, McpError, privateFingerprinter, constantEqual } from "./security.js" +import { readRoutingEndpoints, createRoutingSourceContext, type RoutingEndpoint } from "./routing-observation-source.js" +import { candidateIdentifiesGateway, recognizedCommunityGateway, routeContextExecutionReason, routeGatewayContext, routeBgpContextMode, routeCandidateAvailabilityError } from "./routing-community-context-model.js" +import type { ActionContext } from "./action-types.js" +import { readMeasurementAddress } from "./routing-measurement-address.js" +import { routeContextClients } from "./routing-community-context-native.js" +import { pathProofTag } from "./path-proof-purpose.js" +import { assertManagementTransport, managementTransportClient, type ManagementTransportCapability } from "./management-transport.js" +import { assertRoutingNetworkSharedContextRead, inspectRoutingNetworkSharedContextRead, readRoutingNetworkSharedResource, routingNetworkSharedContextPublicationFence, type RoutingNetworkSharedContextRead } from "./routing-network-native-session.js" +import { scopedTargetRuleMatches } from "./routing-community-scoped-model.js" + +export function routeContextAccess(context: ActionContext) { + return async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение контекста пути отменено", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:read") || !hasPermission(current.policy.permissions, "mm:network:read")) throw new McpError("PERMISSION_DENIED", "Право чтения контекста пути отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Область контекста пути изменилась", 409) + } +} +export type RouteContextOptions = { authorize?: () => Promise; signal?: AbortSignal; expectedSources?: Array<{ serverId: number; proof: string }>; selectedCandidateId?: string; managementTransport?: ManagementTransportCapability } +export type OwnedRouteCommunityTarget = { nativeId: string; rawRow: NativeBgpRow; endpoint: Readonly; desiredTargetProof: (ruleBody: string) => string; matchesRuleProof: (ruleBody: string, proof: unknown) => boolean } + +/** Fixed endpoint clients and private proofs are retained in closures. Public + * results contain no credentials, native rules, arbitrary comments or SQL rows. */ +async function readContext(profile: RouteAiProfile, options: RouteContextOptions, sharedContext?: RoutingNetworkSharedContextRead) { + const ids = [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])].sort((a, b) => a - b) + await options.authorize?.() + const endpoints = await readRoutingEndpoints(ids) + if (!endpoints.some(endpoint => endpoint.id === profile.serverId && endpoint.enabled)) throw new McpError("DEVICE_UNAVAILABLE", "Основной источник контекста пути отсутствует или отключён", 409) + const sources = await createRoutingSourceContext(endpoints) + if (options.expectedSources && fingerprint(sources.expected) !== fingerprint(options.expectedSources)) throw new McpError("STATE_CONFLICT", "Источник контекста пути изменился; новый endpoint не опрашивается", 409) + const sign = await privateFingerprinter("route-ai-context-v1") + const clients: Map(path: string, timeoutMs?: number): Promise; post(path: string, body: Record, timeoutMs?: number): Promise }> = sharedContext ? new Map(endpoints.filter(endpoint => endpoint.enabled).map(endpoint => [endpoint.id, { + get: (path: string) => readRoutingNetworkSharedResource(sharedContext, endpoint.id, path) as Promise, + post: (path: string, body: Record) => readRoutingNetworkSharedResource(sharedContext, endpoint.id, path, body) as Promise, + }])) : routeContextClients(endpoints.filter(endpoint => endpoint.enabled), options.signal) + if (options.managementTransport) { + await assertManagementTransport(options.managementTransport) + const alternate = managementTransportClient(options.managementTransport, options.signal) + if (!endpoints.some(endpoint => endpoint.id === alternate.serverId && endpoint.enabled)) throw new McpError("SERVER_DENIED", "Management transport вне контекста пути", 409) + clients.set(alternate.serverId, alternate) + } + const unavailableIds = ids.filter(id => !endpoints.some(endpoint => endpoint.id === id && endpoint.enabled)) + const unavailableSourcesProof = sign("unavailable-sources:" + fingerprint(endpoints.filter(endpoint => unavailableIds.includes(endpoint.id)))) + const fence = async () => { + await options.authorize?.(); await sources.assertCurrent() + if (options.managementTransport) await assertManagementTransport(options.managementTransport) + if (unavailableIds.length && unavailableSourcesProof !== sign("unavailable-sources:" + fingerprint(await readRoutingEndpoints(unavailableIds)))) throw new McpError("STATE_CONFLICT", "Недоступный источник контекста пути изменился", 409) + } + const rawProof: unknown[] = [{ unavailableIds, unavailableSourcesProof }], caches = new Map(), failures = new Map() + let totalRows = 0, totalBytes = 0, calls = 0 + const budget = (value: unknown) => { + totalBytes += Buffer.byteLength(JSON.stringify(value), "utf8") + if (++calls > 2000 || totalBytes > 8 * 1024 * 1024) throw new McpError("LIMIT_EXCEEDED", "Превышен бюджет контекста пути", 409) + } + const get = async (serverId: number, path: string): Promise => { + const key = serverId + path, cached = caches.get(key) + if (cached) return cached + if (failures.has(key)) throw failures.get(key) + await fence() + const client = clients.get(serverId) + if (!client) throw new McpError("DEVICE_UNAVAILABLE", "Источник кандидата отсутствует или отключён", 409) + let value: unknown + try { value = await client.get(path) } + catch (error) { + if (routeCandidateAvailabilityError(error)) { failures.set(key, error); rawProof.push({ serverId, path, unavailable: true }) } + throw error + } + budget(value) + let rows: NativeBgpRow[] + try { rows = nativeBgpRows(value) } + catch { throw new McpError("INVALID_NATIVE_RESPONSE", "Источник контекста пути вернул недостоверный каталог", 409) } + if (rows.length > 1000 || (totalRows += rows.length) > 10000) throw new McpError("LIMIT_EXCEEDED", "Каталог контекста пути превышает лимит", 409) + const stable = (row: NativeBgpRow) => path === "/interface" ? Object.fromEntries(Object.entries(row).filter(([property]) => !/^(?:rx-|tx-|fp-|last-link-|link-downs$|driver-)/.test(property))) : stableBgpPolicyRow(row) + caches.set(key, rows); rawProof.push({ serverId, path, rows: rows.map(stable) }); return rows + } + const post = async (serverId: number, path: string, body: Record) => { + await fence(); const value = await clients.get(serverId)!.post(path, body); budget(value); rawProof.push({ serverId, path, body, value }); return value + } + const enabled = async (serverId: number, path: string, row: NativeBgpRow) => { + const [result] = await readOspfEffectiveDisabled([row], id => post(serverId, path + "/get", { ".id": id, "value-name": "disabled" })) + if (!["false", "no", false].includes(result.disabled as string | boolean) || ["true", "yes", true].includes(result.invalid as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Зависимость пути отключена или не подтверждена", 409) + return result + } + const unique = (rows: NativeBgpRow[], ref: unknown) => { + const found = rows.filter(row => row.name === ref || row[".id"] === ref) + if (found.length !== 1) throw new McpError("STATE_UNAVAILABLE", "Контекст пути отсутствует или неоднозначен", 409) + return found[0] + } + const sourceId = profile.serverId, tables = await get(sourceId, "/routing/table"), vrfs = await get(sourceId, "/ip/vrf") + const table = unique(tables, profile.table) + if (!isConfirmedRoutingFibTable(table)) throw new McpError("STATE_UNAVAILABLE", "FIB таблицы назначения не подтверждён", 409) + await enabled(sourceId, "/ip/vrf", unique(vrfs, profile.probeVrf)) + // Keep ordered VRF membership (first matching row wins), never equate an + // arbitrary policy table with the diagnostic VRF's same-name table. + for (const row of vrfs) canonicalVrfMembers(row.interfaces) + await fence() + const resource = await clients.get(sourceId)!.get("/system/resource") + budget(resource); rawProof.push({ serverId: sourceId, path: "/system/resource", mode: routeBgpContextMode(resource), version: resource && typeof resource === "object" ? (Array.isArray(resource) ? resource[0]?.version : (resource as Record).version) : null }) + const bgpContextMode = routeBgpContextMode(resource), legacy = bgpContextMode === "legacy-read-only" + const connections = await get(sourceId, "/routing/bgp/connection"), templates = await get(sourceId, "/routing/bgp/template"), instances = legacy ? [] : await get(sourceId, "/routing/bgp/instance") + if (connections.length > 100 || templates.length > 100) throw new McpError("LIMIT_EXCEEDED", "Граф BGP контекста превышает 100 подключений/шаблонов", 409) + const effectiveCache = new Map>() + const resolve = async (path: string, row: NativeBgpRow, stack: string[] = [], disabledConnection = false): Promise> => { + const key = path + row[".id"], cached = effectiveCache.get(key) + if (stack.includes(key) || stack.length >= 16) throw new McpError("STATE_UNAVAILABLE", "Цикл или глубина BGP templates не подтверждены", 409) + if (cached) return cached + if (!disabledConnection) await enabled(sourceId, path, row) + const refs = bgpTemplateReferences(await post(sourceId, path + "/get", { ".id": row[".id"], "value-name": "templates" })) + const parents: Record[] = [] + for (const ref of refs) parents.push(await resolve("/routing/bgp/template", unique(templates, ref), [...stack, key])) + const result: Record = {} + for (const property of [...(legacy ? [] : ["instance", "input.filter-nlri", "output.filter-select"]), "vrf", "routing-table", "afi", "input.filter", "output.filter-chain"]) { + const local = bgpFilterName(await post(sourceId, path + "/get", { ".id": row[".id"], "value-name": property })) + const inherited = parents.map(parent => parent[property]).filter((value): value is string => value !== null) + result[property] = local !== null ? local : inherited.length ? effectiveBgpFilter(null, inherited) : null + } + effectiveCache.set(key, result); return result + } + const bindings: Array<{ connectionId: string; instance: string | null; vrf: string; table: string; afi: string; inputChain: string; outputChain: string }> = [] + const disabledBindings: Array<{ connectionId: string; inputChain: string; outputChain: string }> = [] + const otherBgpReferences: Array<{ connectionId: string; chain: string }> = [] + for (const connection of connections) { + const [observed] = await readOspfEffectiveDisabled([connection], id => post(sourceId, "/routing/bgp/connection/get", { ".id": id, "value-name": "disabled" })) + const disabled = ["true", "yes", true].includes(observed.disabled as string | boolean) + const effective = await resolve("/routing/bgp/connection", observed, [], disabled) + for (const property of ["input.filter-nlri", "output.filter-select"]) if (effective[property]) otherBgpReferences.push({ connectionId: connection[".id"], chain: effective[property]! }) + // Disabled peers still resolve all inheritance and filter references. Their + // dormant chain references continue to block a supposedly private write. + if (disabled) { disabledBindings.push({ connectionId: connection[".id"], inputChain: effective["input.filter"] ?? "", outputChain: effective["output.filter-chain"] ?? "" }); continue } + const instance = legacy ? null : await enabled(sourceId, "/routing/bgp/instance", unique(instances, effective.instance)) + const values: Record = { ...effective } + for (const property of ["vrf", "routing-table"]) if (!values[property] && instance) values[property] = bgpFilterName(await post(sourceId, "/routing/bgp/instance/get", { ".id": instance[".id"], "value-name": property })) + if (!values.vrf || !values["routing-table"] || !values.afi || values["input.filter"] === null) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены effective BGP table/VRF/AFI/input binding", 409) + const boundTable = unique(tables, values["routing-table"]) + if (!isConfirmedRoutingFibTable(boundTable)) throw new McpError("STATE_UNAVAILABLE", "FIB BGP таблицы не подтверждён", 409) + await enabled(sourceId, "/ip/vrf", unique(vrfs, values.vrf)) + bindings.push({ connectionId: connection[".id"], instance: instance ? String(instance.name) : null, vrf: values.vrf, table: values["routing-table"]!, afi: values.afi, inputChain: values["input.filter"]!, outputChain: values["output.filter-chain"] ?? "" }) + } + const matching = bindings.filter(binding => binding.table === profile.table && binding.vrf === (profile.bgpContext?.vrf ?? "main") && binding.afi.split(",").includes(profile.family === "ipv4" ? "ip" : "ipv6") && (profile.bgpContext ? binding.connectionId === profile.bgpContext.connectionId && binding.instance === profile.bgpContext.instance && binding.inputChain === profile.bgpContext.inputChain : profile.table === "main" && binding.inputChain === "bgp-in")) + const selected = matching.length === 1 ? matching[0] : null + const filters = await get(sourceId, "/routing/filter/rule") + for (const row of filters.filter(row => row.chain === selected?.inputChain)) Object.assign(row, await enabled(sourceId, "/routing/filter/rule", row)) + const current = selected ? recognizedCommunityGateway(filters, selected.inputChain, profile.community, profile.family) : null + const externalReferences: Array<{ path: string; id: string }> = [] + for (const path of ["/routing/ospf/instance", "/routing/rip/instance", "/routing/isis/instance", "/routing/filter/select-rule", "/routing/bgp/vpn", "/routing/rule", "/ip/firewall/mangle", "/ipv6/firewall/mangle"]) { + const rows = await get(sourceId, path) + for (const row of rows) if (selected && Object.entries(row).some(([key, value]) => (key.includes("filter") || key === "do-where" || path === "/routing/filter/select-rule" && key === "chain") && (typeof value === "string" ? value.split(",").includes(selected.inputChain) : Array.isArray(value) && value.includes(selected.inputChain)))) externalReferences.push({ path, id: row[".id"] }) + } + for (const row of filters) if (selected && row.chain !== selected.inputChain && typeof row.rule === "string" && /\bjump\b/.test(row.rule)) { + // Opaque jump syntax cannot prove that the selected input chain is private. + externalReferences.push({ path: "/routing/filter/rule", id: row[".id"] }) + } + const candidateRows = new Map() + const candidateDiagnostics = profile.candidates.map(candidate => ({ candidateId: candidate.id, status: "available" as "available" | "candidate-unavailable", reason: null as string | null })) + const unavailableCandidate = (candidateId: string, error: unknown) => { + if (!routeCandidateAvailabilityError(error)) throw error + const diagnostic = candidateDiagnostics.find(row => row.candidateId === candidateId)! + diagnostic.status = "candidate-unavailable"; diagnostic.reason = "CANDIDATE_CONTEXT_UNAVAILABLE" + rawProof.push({ candidateId, status: diagnostic.status, reason: diagnostic.reason }) + } + for (const candidate of profile.candidates) { + try { + const path = { gre: "/interface/gre", ipv4_route: "/ip/route", ipv6_route: "/ipv6/route" }[candidate.gatewayRef.resource] + const row = (await get(sourceId, path)).find(row => row[".id"] === candidate.gatewayRef.id) + if (!row) throw new McpError("STATE_UNAVAILABLE", "Gateway кандидата отсутствует", 409) + candidateRows.set(candidate.id, row) + await enabled(sourceId, path, row) + if (!candidateIdentifiesGateway(profile, candidate, { gateway: candidate.gateway, gatewayInterface: candidate.gatewayRef.resource === "gre" ? String(row.name ?? "") : "" }, row)) throw new McpError("STATE_UNAVAILABLE", "Gateway кандидата не связан с точным owned объектом", 409) + let gateway: ReturnType + try { gateway = routeGatewayContext(candidate.gateway) } catch { throw new McpError("STATE_UNAVAILABLE", "Gateway кандидата не подтверждён", 409) } + const resolver = unique(tables, gateway.resolverTable) + if (!isConfirmedRoutingFibTable(resolver)) throw new McpError("STATE_UNAVAILABLE", "FIB resolver таблицы не подтверждён", 409) + if (unavailableIds.includes(candidate.jumpHostId) || unavailableIds.includes(candidate.exitNodeId)) throw new McpError("DEVICE_UNAVAILABLE", "Источник кандидата отсутствует или отключён", 409) + } catch (error) { unavailableCandidate(candidate.id, error) } + } + const probeEndpoints = new Map() + const address = async (serverId: number, iface: string, selected?: string) => { + const path = profile.family === "ipv4" ? "/ip/address" : "/ipv6/address" + const interfaces = await get(serverId, "/interface") + await enabled(serverId, "/interface", unique(interfaces, iface)) + const rows = await get(serverId, path) + const value = await readMeasurementAddress(rows, { dstInterface: iface, family: profile.family, dstAddress: selected }, id => post(serverId, path + "/get", { ".id": id, "value-name": "disabled" })) + if (value.linkLocal) throw new McpError("STATE_UNAVAILABLE", "Link-local control probe требует отдельного scoped исполнителя", 409) + return value.address + } + for (const candidate of profile.candidates.filter(candidate => candidate.controlProbe)) { + if (candidateDiagnostics.find(row => row.candidateId === candidate.id)!.status !== "available") continue + try { + const probe = candidate.controlProbe! + // RouterOS VRF rows are ordered. Interface lists and inherited membership + // are deliberately unknown instead of guessing the first matching VRF. + let owner: string | null = null + const interfaceNames = new Set((await get(sourceId, "/interface")).map(row => String(row.name))) + for (const row of vrfs) { + const members = canonicalVrfMembers(row.interfaces).split(",") + if (["true", "yes", true].includes(row.disabled as string | boolean)) continue + await enabled(sourceId, "/ip/vrf", row) + if (members.some(member => !["all", "none"].includes(member) && !interfaceNames.has(member))) throw new McpError("STATE_UNAVAILABLE", "VRF interface-list membership требует отдельного resolver", 409) + if (members.includes("all") || members.includes(candidate.wanInterface)) { owner = String(row.name); break } + } + if (owner !== profile.probeVrf) throw new McpError("STATE_UNAVAILABLE", "WAN интерфейс не принадлежит указанному probe VRF", 409) + await enabled(sourceId, "/interface", unique(await get(sourceId, "/interface"), candidate.wanInterface)) + const destination = await address(candidate.exitNodeId, probe.targetInterface, probe.targetAddress), expected: string[] = [] + for (const hop of probe.expectedHops) expected.push(await address(hop.serverId, hop.interface, hop.address)) + const source = probe.sourceAddress ? await address(sourceId, candidate.wanInterface, probe.sourceAddress) : undefined + probeEndpoints.set(candidate.id, { destination, expected, source }) + } catch (error) { unavailableCandidate(candidate.id, error) } + } + if (options.selectedCandidateId && candidateDiagnostics.find(row => row.candidateId === options.selectedCandidateId)?.status !== "available") throw new McpError("STATE_UNAVAILABLE", "CANDIDATE_CONTEXT_UNAVAILABLE", 409) + const currentCandidates = current && selected ? profile.candidates.filter(candidate => candidateIdentifiesGateway(profile, candidate, current, candidateRows.get(candidate.id))) : [] + await fence() + const protections = (await pool.query(`WITH chosen AS MATERIALIZED (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[])) LIMIT 1001), + budget AS (SELECT count(*) AS count,coalesce(sum(octet_length(object_ref::text)),0) AS bytes FROM chosen) + SELECT CASE WHEN budget.count<=1000 AND budget.bytes<=1048576 THEN (SELECT coalesce(jsonb_agg(object_ref),'[]') FROM chosen) END AS refs,budget.count>1000 OR budget.bytes>1048576 AS overflow FROM budget`, [ids.map(String)])).rows[0] + if (!protections || protections.overflow || !Array.isArray(protections.refs)) throw new McpError("LIMIT_EXCEEDED", "Не подтверждён ограниченный каталог защит пути", 409) + await fence() + const allChainBindings = [...bindings, ...disabledBindings] + const sharedConnectionIds = selected ? allChainBindings.filter(binding => binding.inputChain === selected.inputChain).map(binding => binding.connectionId) : [] + const dependencyReason = !selected ? "BGP_CONTEXT_UNCONFIRMED" : selected.afi !== (profile.family === "ipv4" ? "ip" : "ipv6") || sharedConnectionIds.length !== 1 || allChainBindings.some(binding => binding.outputChain === selected.inputChain) || otherBgpReferences.some(binding => binding.chain === selected.inputChain) || externalReferences.length ? "SHARED_BINDING_REQUIRES_SCOPED_EXECUTOR" : !current || currentCandidates.length !== 1 ? "CURRENT_GATEWAY_CONTEXT_UNCONFIRMED" : null + const reason = legacy ? "LEGACY_BGP_READ_ONLY" : routeContextExecutionReason(profile) ?? dependencyReason + const topologyReason = legacy ? "LEGACY_BGP_READ_ONLY" : !profile.bgpContext && (profile.table !== "main" || profile.probeVrf !== "main" || selected && (selected.vrf !== "main" || selected.inputChain !== "bgp-in")) ? "EXPLICIT_BGP_CONTEXT_REQUIRED" : dependencyReason + const scopedReason = legacy ? topologyReason : profile.autoRecovery ? "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE" : topologyReason + const targetRule = selected && current ? filters.find(row => row.chain === selected.inputChain)! : null + // The owned target body has a separate seal so a scoped executor can prove + // untouched dependencies after its deliberate, exact body replacement. + const targetRuleProof = targetRule ? sign("target-rule:" + fingerprint(stableBgpPolicyRow(targetRule))) : null + const immutableTarget = targetRule ? structuredClone(targetRule) : null + const ownedTarget: OwnedRouteCommunityTarget | undefined = immutableTarget ? (() => { + const desiredTargetProof = (ruleBody: string) => { + if (typeof ruleBody !== "string" || Buffer.byteLength(ruleBody, "utf8") > 65536) throw new McpError("LIMIT_EXCEEDED", "Тело scoped Community фильтра превышает лимит", 409) + return sign("target-rule:" + fingerprint(stableBgpPolicyRow({ ...immutableTarget, rule: ruleBody }))) + } + return { + nativeId: immutableTarget[".id"], rawRow: structuredClone(immutableTarget), endpoint: Object.freeze({ ...endpoints.find(endpoint => endpoint.id === sourceId)! }), + desiredTargetProof, matchesRuleProof: (ruleBody: string, proof: unknown) => scopedTargetRuleMatches({ rawRow: immutableTarget, desiredTargetProof }, ruleBody, proof), + } + })() : undefined + const dependencies = rawProof.map(value => { + const snapshot = value as { path?: string; rows?: Record[] } + return snapshot.path === "/routing/filter/rule" && snapshot.rows && targetRule ? { ...snapshot, rows: snapshot.rows.map(row => row[".id"] === targetRule[".id"] ? { ...row, rule: "[separately-sealed-owned-body]" } : row) } : value + }) + const dependencyProof = sign("dependencies:" + fingerprint({ profile, sources: sources.expected, rawProof: dependencies, protections: protections.refs })) + const before = { contextVerified: !!selected, bgpContextMode, candidateDiagnostics, table: profile.table, probeVrf: profile.probeVrf, family: profile.family, + bgpConnectionId: selected?.connectionId ?? null, bgpVrf: selected?.vrf ?? null, instance: selected?.instance ?? null, inputChain: selected?.inputChain ?? null, + currentCandidateId: currentCandidates.length === 1 ? currentCandidates[0].id : null, currentGateway: current?.gateway ?? null, + nativeRuleId: selected && current ? filters.find(row => row.chain === selected.inputChain)![".id"] : null, ownedCompilerRecognized: !!current, + sharedConnectionIds, bindings, disabledConnectionIds: disabledBindings.map(binding => binding.connectionId), externalReferences, executionAvailable: reason === null, executionReason: reason, scopedExecutionAvailable: scopedReason === null, scopedExecutionReason: scopedReason, targetRuleProof, dependencyProof, sourceProofs: sources.expected } + return { before, clients, fence, probeEndpoints, ownedTarget, scopedTopologyAvailable: topologyReason === null, + captureOriginalEffectSelection: async (candidateId: string) => { + try { + const routes = await get(sourceId, "/routing/route"), sessions = await get(sourceId, "/routing/bgp/session") + await fence() + return prepareOriginalEffectSelection(profile, candidateId, selected, { routes, connections, sessions, + candidate: candidateRows.get(candidateId), probe: probeEndpoints.get(candidateId) }) + } catch { await fence(); return { status: "unknown" as const, reason: "prepared-effect-catalogue-unavailable" } } + }, + signEvidence: (value: unknown) => sign(pathProofTag("community") + fingerprint({ dependencyProof, targetRuleProof, value })), + verifyEvidence: (value: unknown, proof: unknown) => typeof proof === "string" && constantEqual(sign(pathProofTag("community") + fingerprint({ dependencyProof, targetRuleProof, value })), proof), + } +} + +export async function readRouteCommunityContext(profile: RouteAiProfile, options: RouteContextOptions = {}) { + try { return await readContext(profile, options) } + catch (error) { + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Не подтверждены bounded BGP/routing dependencies пути", 409) + } +} + +/** Private token-only path. Profile, source identities, candidate, authority, + * signal and transport all come from the authenticated native session owner. + * It preserves every existing execution reason, including autoRecovery. */ +export async function readRouteCommunityContextWithSharedNativeSession(sharedContext: RoutingNetworkSharedContextRead) { + const binding = inspectRoutingNetworkSharedContextRead(sharedContext) + const result = await readContext(binding.profile, { selectedCandidateId: binding.candidateId, signal: binding.signal, + expectedSources: binding.contextSources, authorize: () => assertRoutingNetworkSharedContextRead(sharedContext) }, sharedContext) + await result.fence() + // The acquisition ends when this function returns. Later publication fences + // must use the owner rather than retain a completed transport-read permit. + return { before: result.before, scopedTopologyAvailable: result.scopedTopologyAvailable, fence: routingNetworkSharedContextPublicationFence(sharedContext), + ownedTarget: result.ownedTarget ? { nativeId: result.ownedTarget.nativeId, rawRow: structuredClone(result.ownedTarget.rawRow), matchesRuleProof: result.ownedTarget.matchesRuleProof } : undefined } +} diff --git a/backend/src/modules/mcp/routing-community-predispatch-diagnostic.ts b/backend/src/modules/mcp/routing-community-predispatch-diagnostic.ts new file mode 100644 index 00000000..a07331d3 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-predispatch-diagnostic.ts @@ -0,0 +1,17 @@ +import { McpError } from "./security.js" + +type MeasurementReason = "sla-unavailable" | "bound-proof-unavailable" | "bound-proof-stale" | "bound-proof-changed" | + "latest-proof-unavailable" | "latest-proof-binding" | "latest-proof-stale" | "latest-proof-context" + +const reasons = new WeakMap() + +export function routingMeasurementRefusal(reason: MeasurementReason, message: string) { + const error = new McpError("MEASUREMENT_UNKNOWN", message, 409) + reasons.set(error, reason) + return error +} + +export function manualRoutingPredispatchDiagnostic(error: unknown, operationId: string) { + if (!(error instanceof McpError) || error.code !== "MEASUREMENT_UNKNOWN") return undefined + return { event: "mcp.routing.manual-predispatch-refused", operationId, reason: reasons.get(error) ?? "unclassified" } +} diff --git a/backend/src/modules/mcp/routing-community-public.test.ts b/backend/src/modules/mcp/routing-community-public.test.ts new file mode 100644 index 00000000..770150f7 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-public.test.ts @@ -0,0 +1,45 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import type { ChangeEvidence } from "./action-types.js" +import { publicRoutingCommunityChanges, publicMcpChangeEvidence, publicRoutingControllerState } from "./routing-community-public.js" + +test("controller status keeps the comparison freshness window private", () => { + assert.deepEqual(publicRoutingControllerState({ consecutive: 2, confirmationWindow: { evaluatedAtMs: 123, validUntilMs: 456 } }), { consecutive: 2 }) +}) + +test("Route AI public review preserves exact route changes while hiding native snapshots and private seals", () => { + const before = { community: "65000:10", action: "route", gateway: "192.0.2.1", gatewayTunnelId: "", description: "NEVER_NATIVE_ANNOTATION" } + const after = { ...before, gateway: "192.0.2.9" } + const evidence: ChangeEvidence = { target: { scope: "servers", serverIds: [1] }, objects: [{ kind: "ipv4_route", serverId: 1, id: "*r" }], + snapshots: [{ serverId: 1, resource: "routing_filter", fingerprint: "NEVER_DICTIONARY_ORACLE", rows: [{ ".id": "*f", comment: "NEVER_NATIVE_COMMENT", rule: "NEVER_NATIVE_SCRIPT", opaque: "NEVER_OPAQUE_FIELD" }] }], + before: [before], steps: [{ kind: "service", service: "routing.community", input: { serverId: 1, candidateId: "b", rules: [after], profile: { community: before.community, table: "main", probeVrf: "main", name: "NEVER_PROFILE_ANNOTATION" }, contextProof: "NEVER_CONTEXT_SEAL", targetRuleProof: "NEVER_TARGET_SEAL", contextSources: [{ proof: "NEVER_SOURCE_SEAL" }] } }], requiresHuman: true, warnings: ["Review gateway change"] } + const projected = publicRoutingCommunityChanges(evidence) + assert.deepEqual(projected.before, [{ community: "65000:10", action: "route", gateway: "192.0.2.1", gatewayTunnelId: "" }]) + assert.deepEqual(projected.steps[0].input.rules, [{ community: "65000:10", action: "route", gateway: "192.0.2.9", gatewayTunnelId: "" }]) + assert.deepEqual(projected.snapshots, [{ serverId: 1, resource: "routing_filter", rowCount: 1 }]) + assert.equal(projected.requiresHuman, true); assert.ok(!JSON.stringify(projected).includes("NEVER_")) + assert.equal(evidence.snapshots[0].rows[0].comment, "NEVER_NATIVE_COMMENT", "projection must not alter sealed execution evidence") +}) + +test("scoped recovery uses the private Route AI projector through the generic recovery action", () => { + const before = { community: "65000:10", action: "route", gateway: "192.0.2.9", gatewayTunnelId: "" } + const evidence: ChangeEvidence = { target: { scope: "servers", serverIds: [1] }, objects: [], snapshots: [], before: [before], requiresHuman: true, validUntil: "2026-10-24T12:02:00.000Z", + steps: [{ kind: "service", service: "routing.community.scoped.recover", input: { serverId: 1, candidateId: "a", profile: { community: before.community, table: "tenant", probeVrf: "probe", name: "NEVER_PROFILE" }, rules: [{ ...before, gateway: "192.0.2.1" }], requestProof: "NEVER_SEAL", sourceProof: "NEVER_SOURCE", originalRule: "NEVER_NATIVE_SCRIPT", managementBindingCiphertext: "NEVER_MANAGEMENT" } }], warnings: [] } + const projection = publicMcpChangeEvidence("operations.recover", evidence) + assert.ok(!JSON.stringify(projection).includes("NEVER_")) + assert.equal((projection.steps[0] as { input: { rules: { gateway: string }[] } }).input.rules[0].gateway, "192.0.2.1") + assert.deepEqual(projection.before, [before]); assert.equal(projection.requiresHuman, true) + assert.equal(projection.validUntil, evidence.validUntil, "Human review must show the shorter inverse execution deadline") + const ros: ChangeEvidence = { ...evidence, steps: [{ kind: "ros", step: { serverId: 1, resource: "routing_filter", action: "update", id: "*f", fields: { disabled: false } } }] } + assert.equal(publicMcpChangeEvidence("operations.recover", ros), ros, "existing generic native recovery review remains unchanged") +}) + +test("scoped preview includes exact selected before/proposed and hides all private executor evidence", () => { + const before = { community: "65000:10", gateway: "192.0.2.1", action: "route", gatewayTunnelId: "" } + const evidence: ChangeEvidence = { target: { scope: "servers", serverIds: [1] }, objects: [], snapshots: [], before: [before], requiresHuman: true, + steps: [{ kind: "service", service: "routing.community.scoped", input: { principal: "NEVER_PRINCIPAL", requestProof: "NEVER_REQUEST", receiptId: "NEVER_RECEIPT", dependencyProof: "NEVER_DEPENDENCY", desiredTargetProof: "NEVER_DESIRED", originalTargetProof: "NEVER_ORIGINAL", profile: { community: before.community, table: "tenant", probeVrf: "probe" }, candidateId: "b", serverId: 1, rules: [{ ...before, gateway: "192.0.2.9" }] } }], warnings: [] } + const result = publicRoutingCommunityChanges(evidence) + assert.deepEqual(result.before, [before]); assert.equal(result.steps[0].input.rules[0].gateway, "192.0.2.9") + assert.equal(result.steps[0].input.table, "tenant"); assert.equal(result.steps[0].input.probeVrf, "probe") + assert.ok(!JSON.stringify(result).includes("NEVER_")) +}) diff --git a/backend/src/modules/mcp/routing-community-public.ts b/backend/src/modules/mcp/routing-community-public.ts new file mode 100644 index 00000000..efee18e1 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-public.ts @@ -0,0 +1,66 @@ +import type { ChangeEvidence } from "./action-types.js" +import { isRoutingPolicySnapshot } from "./resources.js" + +const record = (value: unknown): Record => value && typeof value === "object" && !Array.isArray(value) ? value as Record : {} +const rules = (value: unknown) => Array.isArray(value) ? value.map(item => { + const rule = record(item) + return { community: rule.community, action: rule.action ?? "route", gateway: rule.gateway, gatewayTunnelId: rule.gatewayTunnelId } +}) : [] + +/** Native snapshots remain encrypted execution evidence. Public review contains + * only the exact managed routing change, without scripts, comments or seals. + * The unkeyed snapshot fingerprint is also private: comments can contain low + * entropy confidential values that would otherwise permit offline guesses. */ +export function publicRoutingCommunityChanges(evidence: ChangeEvidence) { + return { + target: evidence.target, objects: evidence.objects, + snapshots: evidence.snapshots.map(snapshot => ({ serverId: snapshot.serverId, resource: snapshot.resource, rowCount: snapshot.rows.length })), + before: rules(evidence.before), + steps: evidence.steps.map(step => { + if (step.kind !== "service" || !["routing.community", "routing.community.scoped", "routing.community.scoped.recover"].includes(step.service)) throw new Error("Unsupported public Route AI change") + const profile = record(step.input.profile) + return { kind: step.kind, service: step.service, input: { + serverId: step.input.serverId, candidateId: step.input.candidateId, + community: profile.community, table: profile.table, probeVrf: profile.probeVrf, + rules: rules(step.input.rules), + ...(step.service === "routing.community.scoped.recover" && step.input.observation === "route-effects-v1" ? { observation: "route-effects-v1" } : {}), + } } + }), + requiresHuman: evidence.requiresHuman, mutationCost: evidence.mutationCost, validUntil: evidence.validUntil, + managementChannels: evidence.managementChannels?.map(channel => ({ channelId: channel.channelId, serverId: channel.serverId, expiresAt: channel.expiresAt })), + warnings: evidence.warnings, + } +} + +/** Service recovery has the same private native bodies as its forward plan. */ +export function publicMcpChangeEvidence(operationId: string, evidence: ChangeEvidence) { + if (operationId === "routing.bfd.configuration.move") { + const before = evidence.before as { order?: string[] } | undefined + return { ...evidence, before: { order: before?.order }, steps: evidence.steps.map(step => step.kind === "service" + ? { ...step, input: { move: step.input.move } } : step) } + } + const scopedRecovery = operationId === "operations.recover" && evidence.steps.length === 1 && evidence.steps[0].kind === "service" && evidence.steps[0].service === "routing.community.scoped.recover" + if (operationId === "routing.profiles.save" || operationId === "routing.controller.update") return {...evidence,steps:evidence.steps.map(step=>{if(step.kind!=="service")return step;const {baseline:_privateBaseline,...input}=step.input;return {...step,input}})} + if (operationId === "routing.community.switch" || scopedRecovery) return publicRoutingCommunityChanges(evidence) + if (evidence.healthChecks?.some(check => check.nextHopInterface)) evidence = { ...evidence, healthChecks: evidence.healthChecks.map(({ nextHopInterface: _privateInterface, ...check }) => check) } + if (!evidence.snapshots.some(snapshot => isRoutingPolicySnapshot(snapshot.resource))) return evidence + return { ...evidence, snapshots: evidence.snapshots.map(snapshot => isRoutingPolicySnapshot(snapshot.resource) + ? { serverId: snapshot.serverId, resource: snapshot.resource, rowCount: snapshot.rows.length } + : snapshot) } +} + +/** Public controller DTO is explicit. Private baseline/native proofs, seal + * lineage and future internal fields never leave profile list or plan review. */ +export function publicRoutingControllerState(raw:unknown) { + if(!raw||typeof raw!=="object"||Array.isArray(raw))return {} + const value=raw as Record,result:Record={} + for(const key of ["pendingCandidateId","observationKey","lastSwitchAt","operationId","pausedReason","failureKey","failingCandidateId", + "planId","candidateId","expectedCandidateId","reason","checkedAt","probedAt"]){ + if(typeof value[key]==="string")result[key]=value[key] + } + for(const key of ["consecutive","failureCount"]){ + if(typeof value[key]==="number"&&Number.isSafeInteger(value[key])&&Number(value[key])>=0)result[key]=value[key] + } + if(Array.isArray(value.switches)&&value.switches.length<=2048&&value.switches.every(at=>typeof at==="string"))result.switches=[...value.switches] + return result +} diff --git a/backend/src/modules/mcp/routing-community-reserved-recovery-schema.ts b/backend/src/modules/mcp/routing-community-reserved-recovery-schema.ts new file mode 100644 index 00000000..21698ac9 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-reserved-recovery-schema.ts @@ -0,0 +1,14 @@ +/** Historical proposal retained for review compatibility only. Numbered 0040 + * now owns these columns; this string is not an enrollment or activation API. */ +export const ROUTING_RESERVED_RECOVERY_PROPOSAL_DDL = ` +ALTER TABLE mcp_scoped_recovery_intents ADD COLUMN inverse_intent_sealed text; +ALTER TABLE mcp_scoped_recovery_intents ADD COLUMN inverse_intent_proof text; +ALTER TABLE mcp_scoped_recovery_intents ADD COLUMN inverse_configuration_sealed text; +ALTER TABLE mcp_scoped_recovery_intents ADD COLUMN inverse_configuration_proof text; +ALTER TABLE mcp_scoped_recovery_intents ADD CONSTRAINT reserved_inverse_evidence_pairs CHECK ( + (inverse_intent_sealed IS NULL)=(inverse_intent_proof IS NULL) + AND (inverse_configuration_sealed IS NULL)=(inverse_configuration_proof IS NULL) + AND (inverse_configuration_sealed IS NULL OR inverse_intent_sealed IS NOT NULL) + AND (inverse_intent_sealed IS NULL OR mode='reserved') +); +` diff --git a/backend/src/modules/mcp/routing-community-reserved-source.test.ts b/backend/src/modules/mcp/routing-community-reserved-source.test.ts new file mode 100644 index 00000000..ce4c8763 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-reserved-source.test.ts @@ -0,0 +1,138 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { test } from "node:test" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import type { ActionContext } from "./action-types.js" +import { loadReservedScopedRecoverySource } from "./routing-community-scoped-recovery-source.js" +import { executeReservedScopedRecovery, scopedRecoveryIntentFromCheckpoint, validateReservedInverseVerified } from "./routing-community-scoped-recovery.js" +import { fingerprint } from "./security.js" + +test("public JSON cannot authorize a running forward source read", async () => { + for (const capability of [{ kind: "routing-reserved-inverse-execution" }, Object.freeze({ kind: "routing-reserved-inverse-execution" })]) { + await assert.rejects(loadReservedScopedRecoverySource(capability as Parameters[0], {} as ActionContext), /capability/) + } +}) + +test("a serialized allocation cannot enter the shared inverse engine", async () => { + const forged = JSON.parse('{"kind":"routing-reserved-inverse-allocation"}') + await assert.rejects(executeReservedScopedRecovery(forged, {} as Parameters[1]), /сеанс/) +}) + +test("the reserved spend and recheck require the original running operation and untouched intent", async () => { + const source = ts.createSourceFile("routing-reservation-store.ts", + await readFile(new URL("./routing-reservation-store.ts", import.meta.url), "utf8"), ts.ScriptTarget.ES2022, true) + const guard = source.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "assertRunningReservedIntent") + const store = source.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "createRoutingReservationStore") + assert(guard && store) + for (const method of ["beginInverseAttempt", "inspectInverseExecution"]) { + const start = store.getText(source).indexOf(`${method}:`) + assert.notEqual(start, -1) + assert.match(store.getText(source).slice(start, store.getText(source).indexOf("\n },", start)), /await assertRunningReservedIntent\(db, value\)/) + } + const compiled = ts.transpileModule(`export ${guard.getText(source)}`, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + const exports: Record = {} + runInNewContext(compiled, { exports, fingerprint, redact: (value: unknown) => value, fail: (message: string) => new Error(message) }) + const check = exports.assertRunningReservedIntent as (db: unknown, value: unknown) => Promise + const input = { receiptId: "original" }, value = { row: { id: "operation", plan_id: "plan", connection_id: "connection" }, input } + const intent = { kind: "service", service: "routing.community.scoped", input } + const db = (rows: unknown[]) => ({ async query(sql: string, args: string[]) { + assert.match(sql, /o\.id=\$1 AND p\.id=\$2 AND o\.connection_id=\$3 AND o\.state='running' AND p\.state='executing'/) + assert.match(sql, /s\.step_index=0/) + assert.match(sql, /s\.state='intent' AND s\.result IS NULL/) + assert.deepEqual(Array.from(args), ["operation", "plan", "connection"]) + return { rows } + } }) + await check(db([{ intended_state: intent }]), value) + await assert.rejects(check(db([]), value), /No exact running reserved intent/) + await assert.rejects(check(db([{ intended_state: { ...intent, input: { receiptId: "changed" } } }]), value), /No exact running reserved intent/) +}) + +test("confirmed inverse spend quarantines the exact slot on post-spend refusal without replay", async () => { + // Execute the actual boundary body with only its I/O dependencies substituted. + // This does not mint a production capability or claim native/SQL acceptance. + const source = ts.createSourceFile("routing-community-scoped-recovery.ts", + await readFile(new URL("./routing-community-scoped-recovery.ts", import.meta.url), "utf8"), ts.ScriptTarget.ES2022) + const declaration = source.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "executeReservedScopedRecovery") + assert(declaration) + const compiled = ts.transpileModule(declaration.getText(source), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + const allocation = Object.freeze({ kind: "routing-reserved-inverse-allocation" }) + const execution = Object.freeze({ kind: "routing-reserved-inverse-execution" }) + const principal = { connectionId: "owned", credentialId: "original" } + const sharedContext = Object.freeze({}), networkSession = { sharedContext }, networkDeadline = Date.now() + 30_000 + const original = new Error("original authority failure"), result = Object.freeze({ outcome: "recovered" }) + const scenarios = { + spend: ["spend"], + inspect: ["spend", "inspect", "quarantine"], + authority: ["spend", "inspect", "authority", "quarantine"], + source: ["spend", "inspect", "authority", "source", "quarantine"], + proof: ["spend", "inspect", "authority", "source", "proof", "quarantine"], + engine: ["spend", "inspect", "authority", "source", "proof", "engine", "quarantine"], + success: ["spend", "inspect", "authority", "source", "proof", "engine"], + } as const + for (const [scenario, expected] of Object.entries(scenarios)) { + const events: string[] = [], exports: Record = {}, capabilities = new WeakMap() + let request: object | undefined + const at = (phase: string) => { events.push(phase); if (phase === scenario) throw original } + runInNewContext(compiled, { + exports, reservedExecutions: capabilities, fingerprint: JSON.stringify, + inspectRoutingNetworkSharedContext: (value: unknown) => { + assert.equal(value, sharedContext) + return { sourceOperationId: "source", principal, deadline: networkDeadline } + }, + createRoutingReservationStore: () => ({ + async beginInverseAttempt(value: unknown) { + assert.equal(value, allocation); at("spend"); return { reservationId: "spent-slot", execution } + }, + async inspectInverseExecution(value: unknown) { + assert.equal(value, execution); at("inspect") + return { principal, sourceOperationId: "source", deadline: new Date(networkDeadline + 5000).toISOString() } + }, + async markUnknown(id: string, lane: string) { + assert.equal(id, "spent-slot"); assert.equal(lane, "inverse"); at("quarantine") + throw new Error("quarantine persistence unavailable") + }, + }), + effectiveMcpConnection: async () => { at("authority"); return { owner_id: "owner", policy: {}, owner_permissions: [] } }, + loadReservedScopedRecoverySource: async () => { + at("source"); return { profile: { serverId: 1 }, input: {}, configuration: { packet: {} } } + }, + accessGeneration: async () => "generation", + deadline: () => networkDeadline + 10_000, + sealedProof: async () => { at("proof"); return "proof" }, + executeScopedRecovery: async (value: { validUntil: string }) => { + request = value; assert.deepEqual({ ...capabilities.get(value) }, { execution, network: networkSession }) + assert.equal(Date.parse(value.validUntil), networkDeadline) + at("engine"); return result + }, + }) + const execute = exports.executeReservedScopedRecovery as + (allocation: unknown, expected: { sourceOperationId: string; principal: typeof principal; networkSession: typeof networkSession }) => Promise + const outcome = execute(allocation, { sourceOperationId: "source", principal, networkSession }) + if (scenario === "success") assert.equal(await outcome, result) + else await assert.rejects(outcome, error => error === original) + assert.deepEqual(events, expected, scenario) + if (request) assert.equal(capabilities.has(request), false) + } +}) + +test("configuration equality or generic VERIFIED cannot replace reserved signed receipts", async () => { + const expected = {} as Parameters[1] + for (const row of [{}, { state: "verified", phase: "RECOVERY_VERIFIED", configurationVerified: true }, { inverse_intent_sealed: "x".repeat(524289) }]) { + assert.equal(await validateReservedInverseVerified(row, expected), false) + } +}) + +test("manual checkpoint projection preserves the exact original intent fingerprint after reserved schema extension", () => { + const id = "a1111111-1111-4111-8111-111111111111", proof = "a".repeat(64), stamp = "2026-10-25T12:00:00.000Z" + const original = { revision: 1, phase: "RECOVERY_INTENT", mode: "manual", sourceOperationId: id, sourceStepIndex: 0, + sourceReceiptId: id, sourceProof: proof, configurationProof: proof, operationId: id, stepIndex: 0, receiptId: id, + requestSeal: proof, connectionId: id, ownerId: "writer", validUntil: stamp, createdAt: stamp } + for (const intent of [original, { ...original, mode: "reserved", reservationId: id, networkProof: proof }]) { + const checkpoint = JSON.parse(JSON.stringify({ ...intent, phase: "RECOVERY_CONFIG_APPLIED", serverId: 1, nativeId: "*1", candidateId: "a", + originalRule: "exact original bytes", targetProof: proof, dependencyProof: proof, sourceEpochs: [{ serverId: 1, proof }], pathCheckId: null, observedAt: stamp })) + const projected = scopedRecoveryIntentFromCheckpoint(checkpoint) + assert.deepEqual(projected, intent) + assert.equal(fingerprint(projected), fingerprint(intent)) + } +}) diff --git a/backend/src/modules/mcp/routing-community-scoped-acceptance.ts b/backend/src/modules/mcp/routing-community-scoped-acceptance.ts new file mode 100644 index 00000000..9c22baf8 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-acceptance.ts @@ -0,0 +1,2002 @@ +import { createRoutingReservationStore, readHistoricalChargesWithinOwner } from "./routing-reservation-store.js" +import { observeCommittedScopedCheckpoint } from "../../test/committed-scoped-checkpoint-observer.js" +import { prepareRoutingControllerBaseline, advanceRoutingControllerBaseline, assertRoutingControllerBaseline } from "./routing-controller-baseline.js" +import { loadScopedConfigurationPacket, loadScopedNetworkCheckpoint } from "./routing-community-scoped-checkpoint.js" +import { assertScopedCommunityRequest, readScopedPathCheckpoint } from "./routing-community-scoped-request.js" +import { originalEffectReportSchema, originalEffectSelectionSchema } from "./routing-original-effects.js" +import { tickRoutingProfiles } from "./routing-profiles.js" +import { saveControllerProfileCas } from "./routing-controller-fence.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { performance } from "node:perf_hooks" +import assert from "node:assert/strict" +import { createServer, type RequestListener } from "node:http" +import { randomUUID } from "node:crypto" +import type { PoolClient } from "pg" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { managedComment } from "../../managed-markers.js" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { attestRoutingFixture } from "../../test/mcp-routing-fixture.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus, effectiveMcpConnection } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan, publicPlan, loadPlan } from "./runtime.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import type { McpPrincipal } from "./store.js" +import { loadScopedRecoverySource } from "./routing-community-scoped-recovery-source.js" +import { findMcpService } from "./service-registry.js" +import { prepareScopedCommunitySwitch } from "./routing-community-scoped-service.js" +import { resolveRoutingNetworkConfigurationContext, consumeRoutingNetworkConfigurationContext, closeRoutingNetworkConfigurationContext } from "./routing-network-configuration-bridge.js" +import { createRoutingNetworkNativeSession } from "./routing-network-native-session.js" +import { requireRecentPath } from "./path-verification.js" +import { registerManagementChannel } from "./management.js" +import { isSchedulerJobsPaused, pauseSchedulerJobsAndWait, resumeSchedulerJobs } from "../../services/scheduler-running.js" + +type BrowserRoutingTransport = { + call(tool: string, input: Record): Promise> + approve(plan: Awaited>): Promise + verifyOperation(operation: { id: string; planId: string }): Promise +} + +type BrowserRoutingJourney = (access: { token: string; connectionId: string; serverId: number }, + run: (transport: BrowserRoutingTransport) => Promise) => Promise + +/** Disposable SQL + loopback HTTP only. Does not access or configure CHR. */ +type ForwardCheckpoint = (approved: { token: string; connectionId: string; planId: string }) => Promise +type InverseToolCall = (token: string, name: string, input: Record) => Promise + +export async function acceptScopedRouteCommunitySwitch(actor: AuthUser, options: { originalEffectsOnly?: boolean; controllerFailoverOnly?: boolean; originalCredentialRevocationOnly?: boolean; forwardRoundsOnly?: boolean; manualForwardExpiry?: "before-intent" | "after-intent"; manualInverseChargeOnly?: boolean; manualInverseChargeCase?: "positive" | "quota" | "concurrent" | "unknown" | "revoked" | "nonpersistent" | "mixed-forward"; lateInverseAuthorityChange?: "edit" | "disable" | "grant" | "path-proof" | "path-age"; browserJourney?: BrowserRoutingJourney; bareReadbackOnly?: boolean; forwardCheckpoint?: ForwardCheckpoint; expireDuringInverseProbe?: boolean; expireDuringInversePostProbe?: boolean; inverseCheckpointDrift?: boolean; inverseRouteEffects?: boolean; inverseEffectsCheckpointDrift?: boolean; loseInverseAck?: boolean; inverseToolCall?: InverseToolCall } = {}) { + if (options.browserJourney || options.bareReadbackOnly || options.manualForwardExpiry) assert.equal(process.env.NODE_ENV, "test") + if (options.expireDuringInverseProbe || options.expireDuringInversePostProbe || options.inverseCheckpointDrift || options.inverseRouteEffects) assert(options.bareReadbackOnly) + if (options.inverseEffectsCheckpointDrift) assert(options.inverseRouteEffects) + if (options.loseInverseAck) assert(options.bareReadbackOnly && !options.expireDuringInverseProbe) + const sid = options.lateInverseAuthorityChange ? { edit: 131, disable: 132, grant: 133, "path-proof": 134, "path-age": 135 }[options.lateInverseAuthorityChange] : 131 + const writer: AuthUser = { ...actor, permissions: [...new Set([...actor.permissions, "mm:filters:write"])] } + const compileFixture = (outIface: string) => compileManagedFilterRules([{ community: "65000:10", gateway: "192.0.2.1", outIface, action: "route", description: "" }, { community: "65000:20", gateway: "192.0.2.5", outIface: "", action: "route", description: "" }]) + const initial = compileFixture(options.bareReadbackOnly ? "gre-a" : ""), foreign = compileFixture("") + const filters = ["a", "b"].map(key => ({ ".id": "*f" + key, chain: "custom-" + key, rule: key === "a" ? initial : foreign, disabled: "false", comment: managedComment("NEVER_SCOPED_COMMENT_" + key), opaque: "NEVER_SCOPED_OPAQUE" })) + const bareRule = (rule: string) => rule.replace(/^ set gw-interface "(gre-[ab])";$/gm, " set gw-interface $1;") + const patchBodies: string[] = [] + let mode: "normal" | "ignored" | "lost" | "collateral" | "post-failed" | "post-unknown" = "normal", writes = 0, requests = 0 + let controllerProbeUnknown = false + let controllerFailoverProbeSources: string[] | undefined + let controllerFailoverReserveHealthy = true + let pendingPostCheckpoint = false, postCheckpointsObserved = 0 + let afterForwardWrite: (() => Promise) | undefined + let afterConfigurationCheckpoint: ((operationId: string) => Promise) | undefined + let forwardRoundBulk = 0, afterRoundLogRead: (() => Promise) | undefined + let effectLogReads = 0 + let primaryDown = false, primaryControlRequests = 0, pendingInversePost = false, inversePostChecks = 0 + let inverseProbeHook: (() => void) | undefined + let afterInverseConfigurationCheckpoint: ((operationId: string) => Promise) | undefined + let delayNextContextRead = false, delayContextAfterProofQuery = false + const observedVrfs = new Set() + const paddedEffectResources = new Set(["/routing/route", "/routing/bgp/session", "/routing/bfd/session", "/log"]) + // Persistent fixtures need an original RIB selection. These are declared + // synthetic RouterOS responses, never actual CHR facts. + let originalEffectCase: "transition" | "no-op" | "ambiguous" = "transition" + let exposeEffectCatalogue = false + const originalEffectWire: Array<{ method: string; path: string; afterPatch: boolean }> = [] + const originalEffectChanged = () => originalEffectCase === "no-op" || (options.bareReadbackOnly ? bareRule(filters[0].rule) !== bareRule(initial) : filters[0].rule !== initial) + const effectRoutes = () => { + const changed = originalEffectChanged() + const row = { ".id": "*effect1", afi: "ip4", "routing-table": "tenant-a", "dst-address": "203.0.113.0/24", + bgp: "true", connect: "false", static: "false", active: "true", contribution: "active", ecmp: "false", + disabled: "false", filtered: "false", unreachable: "false", blackhole: "false", "bgp.communities": "65000:10", + gateway: changed ? "192.0.2.9" : "192.0.2.1", + "immediate-gw": options.bareReadbackOnly ? changed ? "192.0.2.9%gre-b" : "192.0.2.1%gre-a" : (changed ? "203.0.113.9" : "203.0.113.1") + "%wan-a" } + return originalEffectCase === "ambiguous" ? [row, { ...row, ".id": "*effect2" }] : [row] + } + const effectConnection = { ".id": "*ca", name: "peer-a", instance: "default", vrf: "main", "routing-table": "tenant-a", afi: "ip", + "input.filter": "custom-a", "output.filter-chain": "", templates: "", disabled: "false", "use-bfd": "false", + "local.address": "198.18.0.1", "remote.address": "198.18.0.2", "remote.as": "65001" } + const { vrf: effectTransportVrf, ...effectSessionFields } = effectConnection + const effectSession = { ...effectSessionFields, ".id": "*sa", "local.afi": "ip", "remote.afi": "ip", established: "true", + "local.address": `${effectConnection["local.address"]}@${effectTransportVrf}`, "remote.address": `${effectConnection["remote.address"]}@${effectTransportVrf}` } + const collections = (path: string): Record[] => ({ + "/system/resource": [{ version: "7.20.1" }], + "/routing/table": ["main", "tenant-a", "tenant-b", "probe-a", "probe-b"].map((name, index) => ({ ".id": "*t" + index, name, fib: "true", disabled: "false", invalid: "false" })), + "/ip/vrf": ["a", "b"].map(key => ({ ".id": "*v" + key, name: "probe-" + key, interfaces: "wan-" + key, disabled: "false" })).concat([{ ".id": "*vm", name: "main", interfaces: "all", disabled: "false" }]), + "/routing/bgp/connection": [effectConnection, { ...effectConnection, ".id": "*cb", name: "peer-b", "routing-table": "tenant-b", "input.filter": "custom-b" }], + "/routing/bgp/instance": [{ ".id": "*i", name: "default", vrf: "main", "routing-table": "main", disabled: "false" }], + "/routing/bgp/template": [], "/routing/filter/rule": filters, + "/ip/route": (controllerFailoverProbeSources ? [1, 9, 10, 11] : [1, 9]).map(value => ({ ".id": "*r" + value, "dst-address": "192.0.2." + value + "/32", "routing-table": "main", disabled: "false", + "immediate-gw": "203.0.113." + value + "%wan-a", comment: managedComment("recursive"), opaque: "NEVER_SCOPED_ROUTE" })), + "/interface": ["wan-a", "wan-b", "hop", "target"].map((name, index) => ({ ".id": "*if" + index, name, disabled: "false", running: "true" })), + ...(options.bareReadbackOnly ? { "/interface/gre": ["a", "b"].map(key => ({ ".id": "*e" + key, name: "gre-" + key, disabled: "false", running: "true" })) } : {}), + "/ip/address": [ + ...["wan-a", "wan-b", "hop", "target"].map((name, index) => ({ ".id": "*ad" + index, interface: name, address: "198.51.100." + (index + 1) + "/32", disabled: "false", invalid: "false" })), + ...(controllerFailoverProbeSources ? [11, 12, 13].map(index => ({ ".id": "*ad" + index, interface: "wan-a", address: "198.51.100." + index + "/32", disabled: "false", invalid: "false" })) : []), + ], + ...(options.originalEffectsOnly ? { + "/routing/table": ["main", "tenant-a", "tenant-b", "probe-a", "probe-b"].map((name, index) => ({ ".id": "*t" + index, name, fib: "", disabled: "false", invalid: "false" })), + "/routing/bgp/connection": [effectConnection], + "/log": [{ ".id": "*l1", buffer: "memory", time: "10:00:00", topics: "bgp,route,info", message: "Declared synthetic localhost effect observation; interval coverage unknown" }], + } : {}), + ...(exposeEffectCatalogue || options.originalEffectsOnly ? { + "/routing/route": effectRoutes(), + "/routing/bgp/session": [effectSession], + } : {}), + ...(options.inverseRouteEffects ? { + "/log": [{ ".id": "*i1", buffer: "memory", time: "10:00:00", topics: "bgp,route,info", message: "NEVER_INVERSE_LOG_MESSAGE" }], + } : {}), + } as Record[]>)[path] ?? [] + let requestFailure: { error: unknown } | undefined + const pendingInverseOperationId = async () => { + assert.ok(connectionId) + const { rows } = await pool.query<{ recovery_operation_id: string }>("SELECT i.recovery_operation_id FROM mcp_scoped_recovery_intents i JOIN mcp_operations o ON o.id=i.recovery_operation_id WHERE i.state='intent' AND o.connection_id=$1", [connectionId]) + assert.equal(rows.length, 1, "Post-inverse observation must follow one durable single-use inverse intent") + return rows[0].recovery_operation_id + } + const handler: RequestListener = async (req, res) => { + requests++; res.setHeader("content-type", "application/json") + if (options.originalEffectsOnly) { + assert.equal(req.socket.localAddress, "127.0.0.1") + assert.equal(req.headers.authorization, "Basic " + Buffer.from("admin:NEVER_SCOPED_PASSWORD").toString("base64")) + } + let raw = ""; for await (const chunk of req) raw += chunk + const args = raw ? JSON.parse(raw) : {}, target = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")) + const path = target === "/log?buffer=memory&.proplist=.id,buffer,time,topics,message" ? "/log" : target + if (options.originalEffectsOnly || options.forwardRoundsOnly) originalEffectWire.push({ method: req.method ?? "", path, afterPatch: filters[0].rule !== initial }) + if (req.socket.localAddress === "127.0.0.1") { + if (/^\/(routing|tool)\//.test(path) || path === "/system/license") primaryControlRequests++ + if (primaryDown) { req.socket.destroy(); return } + } + if (req.method === "GET" && path === "/system/license") { res.end(JSON.stringify([{ "software-id": "NEVER_SCOPED_LICENSE" }])); return } + if (req.method === "GET") { + if (path === "/log") effectLogReads++ + if (delayNextContextRead) { + delayNextContextRead = false + await delay(750) + } + const payload = collections(path) + if (path === "/log" && afterRoundLogRead) { const hook = afterRoundLogRead; afterRoundLogRead = undefined; await hook() } + if (path === "/log" && pendingInversePost && options.inverseEffectsCheckpointDrift && afterInverseConfigurationCheckpoint) { + const hook = afterInverseConfigurationCheckpoint + afterInverseConfigurationCheckpoint = undefined + await hook(await pendingInverseOperationId()) + } + // Force the cumulative 10,000-row budget without exceeding the native + // cap of 1,000 rows on any individual response. + const padding = forwardRoundBulk && paddedEffectResources.has(path) + ? Array.from({ length: 1000 - payload.length }, (_, index) => ({ ".id": "*padding" + index })) : [] + res.end(JSON.stringify(padding.length ? [...payload, ...padding] : payload)); return + } + if (req.method === "POST" && path.endsWith("/get")) { const row = collections(path.slice(0, -4)).find(row => row[".id"] === args[".id"]), value = row?.[args["value-name"]]; res.end(JSON.stringify(value === undefined ? [] : { ret: value })); return } + if (req.method === "POST" && path === "/tool/ping") { + if (inverseProbeHook && (!options.expireDuringInversePostProbe || pendingInversePost)) { + const hook = inverseProbeHook + inverseProbeHook = undefined + hook() + } + if (pendingInversePost) { + const operationId = await pendingInverseOperationId() + pendingInversePost = false; inversePostChecks++ + if (!options.inverseEffectsCheckpointDrift) { + const hook = afterInverseConfigurationCheckpoint + afterInverseConfigurationCheckpoint = undefined + await hook?.(operationId) + } + } + if (pendingPostCheckpoint) { + assert.ok(connectionId) + const checkpoint = (await pool.query("SELECT c.configuration_sealed,c.configuration_proof FROM mcp_operation_checkpoints c JOIN mcp_operations o ON o.id=c.operation_id WHERE o.state='running' AND c.configuration_sealed IS NOT NULL AND o.connection_id=$1", [connectionId])).rows + assert.equal(checkpoint.length, 1, "Exact CONFIG_APPLIED must commit before the post-change network probe") + const packet = JSON.parse(await decryptSecret(checkpoint[0].configuration_sealed)) + assert.equal(packet.phase, "CONFIG_APPLIED"); assert.equal(packet.originalRule, initial) + assert.equal(packet.originalCandidateId, "a"); assert.equal(packet.desiredCandidateId, controllerFailoverProbeSources ? "d" : "b") + assert.equal(options.bareReadbackOnly ? bareRule(packet.desiredRule) : packet.desiredRule, filters.find(row => row[".id"] === packet.nativeId)!.rule) + assert.ok(checkpoint[0].configuration_proof); pendingPostCheckpoint = false; postCheckpointsObserved++ + const sharedCheck = afterConfigurationCheckpoint; afterConfigurationCheckpoint = undefined + await sharedCheck?.(packet.operationId) + } + observedVrfs.add(args.vrf) + if (controllerFailoverProbeSources) controllerFailoverProbeSources.push(args["src-address"]) + const probeFailed = controllerFailoverProbeSources && (!controllerFailoverReserveHealthy || args["src-address"] !== "198.51.100.13") || mode === "post-failed" && writes + res.end(JSON.stringify(controllerProbeUnknown || mode === "post-unknown" && writes ? [{ message: "unsupported fixture output" }] : [{ sent: "3", received: probeFailed ? "0" : "3", "packet-loss": probeFailed ? "100" : "0", "avg-rtt": "1ms" }])) + return + } + if (req.method === "POST" && path === "/tool/traceroute") { observedVrfs.add(args.vrf); res.end(JSON.stringify([3, 4].map(index => ({ address: "198.51.100." + index, sent: "1", loss: "0" })))); return } + if (req.method === "PATCH") { + assert.deepEqual(Object.keys(args), ["rule"]); const target = filters.find(row => path === "/routing/filter/rule/" + row[".id"]); assert.ok(target) + if (options.bareReadbackOnly) { assert.equal(target, filters[0]); patchBodies.push(args.rule) } + writes++; if (mode !== "ignored") target.rule = options.bareReadbackOnly ? bareRule(args.rule) : args.rule + pendingPostCheckpoint = args.rule !== initial; pendingInversePost = args.rule === initial + if (mode === "collateral") filters.find(row => row !== target)!.opaque = "changed collateral" + if (mode === "lost") { req.socket.destroy(); return } + if (args.rule !== initial && afterForwardWrite) { const change = afterForwardWrite; afterForwardWrite = undefined; await change() } + res.end("{}"); return + } + assert.fail("Unsupported fixture request " + req.method + " " + path) + } + // EventEmitter does not await an async RequestListener. Preserve the first + // original hook/assertion error for the awaited fixture rather than crash. + const listener: RequestListener = (req,res) => { + void Promise.resolve().then(()=>handler(req,res)).catch(error=>{ + requestFailure??={error}; req.socket.destroy() + }) + } + const router = createServer(listener), alternate = createServer(listener) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + await new Promise(resolve => alternate.listen(0, "127.0.0.2", resolve)) + let connectionId: string | undefined + let insertedServer = false + const persistentProfileIds: string[] = [], persistentCases: string[] = [] + const schedulerWasPaused = isSchedulerJobsPaused() + let schedulerDrained = false + let workerFixtureLease:PoolClient|undefined, workerLocked=false, workerUnsafe=false + const originalQuery = pool.query.bind(pool) + let afterIntent: (() => Promise) | undefined + let afterRecoveryJournalRead: ((rows: Array<{ recovery_operation_id: string; configuration_proof: string }>) => Promise) | undefined + pool.query = (async (...args: unknown[]) => { + const result = await (originalQuery as (...args: unknown[]) => Promise)(...args) + const parameters = args[1] as unknown[] | undefined + if (typeof args[0] === "string" && args[0].includes("INSERT INTO mcp_audit") && parameters?.[1] === "configuration.intent" && afterIntent) { const hook = afterIntent; afterIntent = undefined; await hook() } + if (typeof args[0] === "string" && args[0].includes(" AS step_result") && afterRecoveryJournalRead) { + const hook = afterRecoveryJournalRead + afterRecoveryJournalRead = undefined + await hook((result as { rows: Array<{ recovery_operation_id: string; configuration_proof: string }> }).rows) + } + if (delayContextAfterProofQuery && typeof args[0] === "string" && args[0].includes("AS database_age_ms")) { + delayContextAfterProofQuery = false + delayNextContextRead = true + const { rows } = result as { rows: Array<{ database_age_ms: number }> } + assert.equal(rows.length, 1) + rows[0].database_age_ms = 89500 + } + return result + }) as typeof pool.query + try { + // Full integration may already have periodic GRE/BGP collection running. + // Drain it before registering this router: its reads must not be attributed + // to the inverse transport or race exact zero-request assertions. + await pauseSchedulerJobsAndWait(); schedulerDrained = true + // Manual non-main authority rows are intentionally unsupported controller + // profiles. Drain a real tick by owning its existing session key, then keep + // periodic workers out until all fixture rows are disabled/cleaned up. + workerFixtureLease=await pool.connect() + const workerDrainDeadline=performance.now()+60000 + do { + try { workerLocked=(await workerFixtureLease.query("SELECT pg_try_advisory_lock(hashtextextended('mcp-workers',0)) AS locked")).rows[0].locked===true } + catch(error){workerUnsafe=true;throw error} + if(workerLocked)break + assert(performance.now()) => options.inverseToolCall + ? options.inverseToolCall(access.token, name, input) + : invokeMcpTool(principal, name, input) + const profile = (key: string) => routeAiProfileSchema.parse({ name: "independent " + key, maxLossPct: 5, serverId: sid, family: "ipv4", community: "65000:10", table: "tenant-" + key, probeVrf: "probe-" + key, bgpContext: { connectionId: "*c" + key, instance: "default", vrf: "main", inputChain: "custom-" + key }, candidates: [1, 9].map(value => ({ id: value === 1 ? "a" : "b", jumpHostId: sid, exitNodeId: sid, wanInterface: "wan-" + key, downstreamInterface: "hop", gateway: "192.0.2." + value, gatewayRef: { resource: "ipv4_route", id: "*r" + value }, controlProbe: { targetInterface: "target", sourceAddress: key === "a" ? "198.51.100.1" : "198.51.100.2", expectedHops: [{ serverId: sid, interface: "hop" }, { serverId: sid, interface: "target" }] } })) }) + for (const key of ["a", "b"]) for (const iface of ["wan-" + key, "hop"]) { + const id = randomUUID(); await pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source) VALUES($1,$2,$2,$3,'peer','ipv4',$4,1,0,100,100,now(),now(),true,'fixture')", [id, sid, iface, "tenant-" + key]); await attestRoutingFixture(id) + } + await pool.query("INSERT INTO filter_rules(server_id,sort_order,community,community_name,action,gateway,gateway_tunnel_id,description) VALUES($1,77,'65000:10','NEVER_SCOPED_SQL_NAME','route','203.0.113.99','NEVER_SCOPED_SQL_TUNNEL','NEVER_SCOPED_SQL_DESCRIPTION')", [sid]) + const dbBefore = (await pool.query("SELECT * FROM filter_rules WHERE server_id=$1 ORDER BY id", [sid])).rows + const prepare = async (key = "a", persistent?: { id: string; version: number }, exactProfile?: ReturnType, assertApprovalRequired = false, approver: AuthUser | null = writer) => { + exposeEffectCatalogue = !!persistent + const current = exactProfile === undefined ? profile(key) : routeAiProfileSchema.parse(exactProfile) + assert.equal((await invokeMcpTool(principal, "mm_route_path_verify", { profile: current, candidateId: "b" }) as { status: string }).status, "passed") + const plan = await invokeMcpTool(principal, "mm_route_optimizer_plan", { profile: current, candidateId: "b" }, undefined, + persistent ? { persistentRoutingProfile: persistent } : undefined) as { planId: string } + const projection = await publicPlan(await loadPlan(plan.planId)); assert.ok(!JSON.stringify(projection).includes("NEVER_")) + const row = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0] + const evidence = JSON.parse(await decryptSecret(row.evidence.sealed)); const input = evidence.steps[0].input + assert.equal(input.proofRevision, persistent ? 3 : 2) + if (persistent) { + assert.equal(input.persistentProfileAuthority.packet.profileId, persistent.id) + assert.equal(input.persistentProfileAuthority.packet.profileVersion, persistent.version) + assert.equal(input.persistentProfileAuthority.packet.credentialId, principal.credentialId) + assert(!JSON.stringify(projection).includes("persistentProfileAuthority"), "Persistent authority remains private") + } + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 2); assert.ok(!JSON.stringify(input).includes("NEVER_")); assert.equal(input.compiledBody, undefined) + if (assertApprovalRequired) { + const beforeApproval = { writes, requests } + await assert.rejects(applyPlan(principal, plan.planId, randomUUID()), (error: unknown) => error instanceof McpError && error.code === "APPROVAL_REQUIRED") + assert.deepEqual({ writes, requests }, beforeApproval) + } + if (approver) await approvePlan(plan.planId, approver, true) + return { plan, input, evidence } + } + const waitForOperation = async (operationId: string) => { + let state = "" + const deadline = Date.now() + 140000 + do { state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operationId])).rows[0].state; if (!["pending", "running"].includes(state)) break; await delay(20) } while (Date.now() < deadline) + if(requestFailure)throw requestFailure.error + assert.ok(!["pending", "running"].includes(state), "The bounded executor must reach a terminal state before assertions or fixture cleanup") + return state + } + if (options.browserJourney) { + exposeEffectCatalogue = true + const originalFilters = structuredClone(filters) + const origin = new URL(`http://127.0.0.1:${(router.address() as { port: number }).port}`) + const checkGateway = async (expected: string) => { + const response = await fetch(new URL("/rest/routing/route", origin), { redirect: "error", signal: AbortSignal.timeout(5000) }) + assert.equal(response.status, 200) + const rows = await response.json() + assert(Array.isArray(rows)) + assert.equal(rows.length, 1) + assert.equal(rows[0]["routing-table"], "tenant-a") + assert.equal(rows[0].active, "true") + assert.equal(rows[0].gateway, expected) + } + return await options.browserJourney({ token: access.token, connectionId: access.connection.id, serverId: sid }, async transport => { + const approveAndApply = async (prepared: Record, operationId: string) => { + const planId = prepared.planId + assert.equal(typeof planId, "string") + assert.equal(prepared.state, "awaiting-approval") + const review = await publicPlan(await loadPlan(planId as string)) + assert.equal(review.operationId, operationId) + assert.equal(review.connectionId, connectionId) + assert.equal(review.approvedBy, null) + assert(!JSON.stringify(review).includes("NEVER_")) + const unchanged = { writes, requests } + await assert.rejects(transport.call("mm_change_apply", { planId, idempotencyKey: randomUUID() }), { code: "APPROVAL_REQUIRED" }) + assert.deepEqual({ writes, requests }, unchanged) + await transport.approve(review) + assert.equal((await publicPlan(await loadPlan(planId as string))).approvedBy, writer.id) + const accepted = await transport.call("mm_change_apply", { planId, idempotencyKey: randomUUID() }) + const id = accepted.id + assert.equal(typeof id, "string") + assert.equal(await waitForOperation(id as string), "applied") + const result = await transport.call("mm_operation_get", { operationId: id }) + assert.equal(result.id, id) + assert.equal(result.state, "applied") + assert.equal(result.verification, "passed") + await transport.verifyOperation({ id: id as string, planId: planId as string }) + return { id: id as string, planId: planId as string } + } + const current = profile("a") + await checkGateway("192.0.2.1") + assert.equal((await transport.call("mm_route_path_verify", { profile: current, candidateId: "b" })).status, "passed") + const forward = await approveAndApply(await transport.call("mm_route_optimizer_plan", { profile: current, candidateId: "b" }), "routing.community.switch") + assert.equal(writes, 1) + assert.notEqual(filters[0].rule, originalFilters[0].rule) + assert.deepEqual({ ...filters[0], rule: originalFilters[0].rule }, originalFilters[0]) + assert.deepEqual(filters[1], originalFilters[1]) + await checkGateway("192.0.2.9") + const inverse = await approveAndApply(await transport.call("mm_operation_recovery_plan", { operationId: forward.id }), "operations.recover") + assert.notEqual(inverse.planId, forward.planId) + assert.notEqual(inverse.id, forward.id) + await checkGateway("192.0.2.1") + assert.deepEqual(filters, originalFilters) + assert.deepEqual((await pool.query("SELECT * FROM filter_rules WHERE server_id=$1 ORDER BY id", [sid])).rows, dbBefore) + assert.equal(writes, 2) + assert.equal(postCheckpointsObserved, 1) + assert.equal(inversePostChecks, 1) + return { accepted: true, persistentAuthorityCases: 0, officialSdk: true, browserApprovals: 2, routerWrites: 2, syntheticGatewayRoundTrip: true, + originalFiltersRestored: true, externalNetwork: false, forwardingVerified: false } + }) + } + const execute = async (planId: string, executingPrincipal: McpPrincipal = principal) => { + const op = await applyPlan(executingPrincipal, planId, randomUUID()) + return { ...op, state: await waitForOperation(op.id) } + } + if (options.manualForwardExpiry) { + const config = profile("a") + const { plan, input, evidence } = await prepare("a", undefined, config, true) + const approved = await loadPlan(plan.planId) + assert.equal(approved.state, "approved") + assert.equal(input.proofRevision, 2) + assert.equal(evidence.validUntil, undefined) + const bound = await readScopedPathCheckpoint(pool, input.pathCheckId, access.connection.id) + assert.equal(bound.proof, input.pathCheckpointProof) + const expiry = bound.validUntil + if (options.manualForwardExpiry === "after-intent") { + const targetPath = "/routing/filter/rule/" + input.nativeId + const originalFilters = structuredClone(filters) + const writesBeforeApply = writes + const realNow = Date.now + let writeId: string | undefined + afterIntent = async () => { + const { rows: intents } = await pool.query<{ write_id: string }>(` + SELECT payload->>'writeId' AS write_id FROM mcp_audit + WHERE connection_id=$1 AND action='configuration.intent' + AND payload->>'method'='PATCH' AND payload->>'path'=$2`, [connectionId, targetPath]) + assert.equal(intents.length, 1, "The committed scoped PATCH intent must be visible before expiring its proof") + assert.ok(intents[0].write_id) + writeId = intents[0].write_id + assert.equal((await readScopedPathCheckpoint(pool, input.pathCheckId, access.connection.id)).proof, bound.proof) + Date.now = () => expiry + 1 + } + const operation = await (async () => { + try { return await execute(plan.planId) } finally { Date.now = realNow; afterIntent = undefined } + })() + assert.ok(writeId, "The post-intent hook did not run") + assert.equal(operation.state, "unknown") + const { rows: [terminal] } = await pool.query(` + SELECT error_code,verification,result FROM mcp_operations WHERE id=$1 AND connection_id=$2`, [operation.id, connectionId]) + assert.equal(terminal.error_code, "MUTATION_OUTCOME_UNKNOWN") + assert.equal(terminal.verification, "inconclusive") + assert.equal(terminal.result.confirmedWrites, 0) + assert.equal(terminal.result.needsReview, true) + const { rows: records } = await pool.query<{ action: string }>(` + SELECT action FROM mcp_audit WHERE connection_id=$1 AND payload->>'writeId'=$2 ORDER BY action`, [connectionId, writeId]) + assert.deepEqual(records.map(row => row.action), ["configuration.intent", "configuration.unconfirmed"]) + assert.equal(writes, writesBeforeApply, "The expired bound proof must block the RouterOS PATCH") + assert.deepEqual(filters, originalFilters) + return { accepted: true, realSql: true, manualRequestRevision: 2, durableConfigurationIntents: 1, + terminalState: operation.state, errorCode: terminal.error_code, nativeWrites: 0, nativeAcknowledgements: 0 } + } + const newer = await invokeMcpTool(principal, "mm_route_path_verify", { profile: config, candidateId: "b" }) as { id: string; status: string; observedAt: string } + assert.equal(newer.status, "passed") + assert.notEqual(newer.id, input.pathCheckId) + assert(Date.parse(newer.observedAt) + 90_000 > expiry + 1 && expiry + 1 < approved.expires_at.getTime()) + + const before = { writes, requests, filters: structuredClone(filters) } + const realNow = Date.now + const realErrorLog = console.error + const startedAt = realNow() + const diagnostics: unknown[] = [] + const operation = await (async () => { + Date.now = () => expiry + 1 + realNow() - startedAt + console.error = (...args: unknown[]) => { + if (args.length === 1 && typeof args[0] === "string" && args[0].startsWith('{"event":"mcp.routing.manual-predispatch-refused",')) { + diagnostics.push(JSON.parse(args[0])) + } else realErrorLog(...args) + } + try { return await execute(plan.planId) } finally { Date.now = realNow; console.error = realErrorLog } + })() + assert.equal(operation.state, "rejected") + assert.deepEqual(diagnostics, [{ event: "mcp.routing.manual-predispatch-refused", operationId: operation.id, reason: "bound-proof-stale" }]) + const { rows: [terminal] } = await pool.query(`SELECT o.error_code,o.verification,o.result, + (SELECT count(*)::int FROM mcp_operation_steps WHERE operation_id=o.id) AS steps, + (SELECT count(*)::int FROM mcp_operation_checkpoints WHERE operation_id=o.id) AS checkpoints + FROM mcp_operations o WHERE o.id=$1`, [operation.id]) + assert.equal(terminal.error_code, "MEASUREMENT_UNKNOWN") + assert.equal(terminal.verification, "inconclusive") + assert.equal(terminal.result.confirmedWrites, 0) + assert(!JSON.stringify(terminal.result).includes("bound-proof-stale")) + assert.equal(terminal.steps, 0) + assert.equal(terminal.checkpoints, 0) + assert.deepEqual({ writes, requests, filters }, before) + assert.deepEqual((await pool.query("SELECT * FROM filter_rules WHERE server_id=$1 ORDER BY id", [sid])).rows, dbBefore) + return { accepted: true, realSql: true, manualRequestRevision: 2, terminalState: operation.state, + errorCode: terminal.error_code, privateDiagnostic: "bound-proof-stale", newerProofCannotRenew: true, + nativeRequests: 0, nativeWrites: 0, operationSteps: 0 } + } + const reconcile = async (operationId: string) => (await callInverseTool("mm_operation_reconcile", { operationId }) as { + results: Array<{ matchesDesiredState: boolean; configurationMatchesDesired: boolean; configurationCheckpointAvailable: boolean; recoveryCheckpointAvailable?: boolean; controlProbeVerified?: boolean; observation?: unknown }> + }).results[0] + const reset = () => { filters.forEach(row => { row.rule = initial; row.opaque = "NEVER_SCOPED_OPAQUE" }); mode = "normal"; pendingPostCheckpoint = false; pendingInversePost = false; primaryDown = false } + const refresh = async () => { + await pool.query("UPDATE mcp_routing_observations SET ping_at=now(),bandwidth_at=now(),loss_pct=0 WHERE src_server_id=$1", [sid]) + for (const row of (await pool.query("SELECT id FROM mcp_routing_observations WHERE src_server_id=$1", [sid])).rows) await attestRoutingFixture(row.id) + } + // Seed authority for fault cases. The public save/approval path is + // exercised separately below without inserting a profile directly. + const persistentFixture = async (config = profile("a")) => { + const id = randomUUID(), current = await effectiveMcpConnection(principal) + const expiry = new Date(Math.min(Date.now() + 600000, current.owner_valid_until.getTime(), Date.parse(principal.credentialExpiresAt), Date.parse(current.policy.expiresAt))) + await withApplicationMutationLock(() => withConfigMutationLock([sid], async () => { + // Unsupported autoRecovery is a deliberate legacy negative fixture: it + // receives NO baseline and can never acquire native write authority. + const baseline=config.autoRecovery?undefined:await prepareRoutingControllerBaseline(id,1,config,expiry.toISOString(),{principal,connection:current}) + const saved=await saveControllerProfileCas(pool,{profileId:id,connectionId,expectedVersion:null,profile:config,enabled:true,expiresAt:expiry.toISOString(),credentialId:principal.credentialId,generation:await accessGeneration(),policyVersion:current.policy.version,baseline}) + assert.equal(saved?.version,1) + })) + persistentProfileIds.push(id) + return { id, version: 1 } + } + const disablePersistent = (id: string) => pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", [id]) + const executeWithCheckpointObservation = async (planId: string, check: (operationId: string) => Promise) => { + const observer = observeCommittedScopedCheckpoint(pool, check, { + databaseUrl: process.env.DATABASE_URL ?? "", stateDir: process.env.MCP_STATE_DIR ?? "", + }) + try { + const operation = await execute(planId) + observer.assertCompleted() + return operation + } finally { observer.close() } + } + const acceptControllerFailover = async () => { + reset(); await refresh(); exposeEffectCatalogue = true; originalEffectCase = "transition" + controllerFailoverProbeSources = [] + const profileId = randomUUID(), initialWrites = writes + persistentProfileIds.push(profileId) + try { + const configured = profile("a"), original = configured.candidates[0] + configured.candidates = [1, 10, 11, 9].map((gateway, index) => ({ ...original, + id: ["a", "b", "c", "d"][index], gateway: "192.0.2." + gateway, + gatewayRef: { resource: "ipv4_route", id: "*r" + gateway }, + controlProbe: { ...original.controlProbe!, sourceAddress: "198.51.100." + [1, 11, 12, 13][index] }, + })) + const analysis = await invokeMcpTool(principal, "mm_route_optimizer_analyze", { profile: configured }) as { currentCandidateId: string; ranking: Array<{ eligible: boolean; candidate: { id: string } }> } + assert.equal(analysis.currentCandidateId, "a") + assert.deepEqual(analysis.ranking.filter(row => row.eligible).map(row => row.candidate.id), ["a", "b", "c", "d"]) + const enrollment = await invokeMcpTool(principal, "mm_route_profile_save_plan", { + profileId, profile: configured, enabled: true, expiresAt: new Date(Date.now() + 600000).toISOString(), + }) as { planId: string } + await approvePlan(enrollment.planId, writer, true) + assert.equal((await execute(enrollment.planId)).state, "applied") + const readController = async () => (await pool.query("SELECT enabled,state FROM mcp_routing_profiles WHERE id=$1", [profileId])).rows[0] + const beforeQuotaRefusal = await readController() + const quotaLease = randomUUID() + await pool.query(`INSERT INTO mcp_read_leases(id,connection_id,expires_at) + VALUES($1,$2,now()+interval '2 minutes')`, [quotaLease, principal.connectionId]) + const { policy } = await effectiveMcpConnection(principal) + const extraQuotaLeases = Array.from({ length: policy.limits.concurrentReads - 1 }, () => randomUUID()) + try { + await pool.query(`INSERT INTO mcp_read_leases(id,connection_id,expires_at) + SELECT id,$1,now()+interval '2 minutes' FROM unnest($2::uuid[]) AS id`, [principal.connectionId, extraQuotaLeases]) + await tickRoutingProfiles() + assert.deepEqual(await readController(), beforeQuotaRefusal, "Transient read quota must preserve controller state") + assert.equal(writes, initialWrites) + assert.deepEqual(controllerFailoverProbeSources, []) + } finally { + await pool.query("DELETE FROM mcp_read_leases WHERE id=ANY($1::uuid[])", [[quotaLease, ...extraQuotaLeases]]) + } + const failures = new Set() + let switchPlanId: string | undefined + for (const [index, alternate] of [12, 13, 13, 12, 13].entries()) { + // Drive the normal scheduled entry point without waiting a minute. + await pool.query("UPDATE mcp_routing_profiles SET state=state-'probedAt' WHERE id=$1", [profileId]) + controllerFailoverProbeSources.length = 0 + // The previously passing reserve must be rechecked, and its prior + // pass cannot authorize failover if the current check fails. + controllerFailoverReserveHealthy = index !== 2 + await tickRoutingProfiles() + const row = await readController() + assert.equal(row.enabled, true, String(row.state.pausedReason)) + assert.deepEqual(controllerFailoverProbeSources, [1, 11, alternate].map(last => "198.51.100." + last)) + failures.add(row.state.failureKey) + assert.equal(failures.size, index + 1, "Each counted failure must come from a distinct path check") + assert.equal(writes, initialWrites) + if (index < 4) { + assert.equal(row.state.failureCount, index + 1) + assert.equal(row.state.planId, undefined, "Earlier healthy reserve evidence cannot replace a fresh passing probe") + } else { + assert.equal(row.state.candidateId, "d") + switchPlanId = row.state.planId + } + } + assert.ok(switchPlanId) + assert.equal((await loadPlan(switchPlanId)).state, "awaiting-approval") + await tickRoutingProfiles() + assert.equal(writes, initialWrites) + await approvePlan(switchPlanId, writer, true) + await tickRoutingProfiles() + const operationId: string = (await readController()).state.operationId + assert.ok(operationId) + assert.equal(await waitForOperation(operationId), "applied") + await tickRoutingProfiles() + const completed = await readController() + assert.equal(completed.enabled, true, String(completed.state.pausedReason)) + assert.equal(completed.state.baseline.packet.candidateId, "d") + assert.equal(completed.state.baseline.packet.transition.operationId, operationId) + for (const key of ["failureCount", "failureKey", "failureObservedAt", "failingCandidateId", "observationKey"]) { + assert.equal(completed.state[key], undefined, key + " must not carry over to the verified path") + } + assert.equal(completed.state.failoverReserveCursor, undefined) + assert.equal(writes, initialWrites + 1) + const result = { accepted: true, stableRanking: ["a", "b", "c", "d"], selectedReserve: "d", probeCallsPerTick: 3, + distinctFailuresBeforePlan: failures.size, priorPassedProbeNotReused: true, humanApproval: true, nativeWrites: 1 } + console.log("Bounded controller reserve SQL/HTTP:", JSON.stringify(result)) + return result + } finally { await disablePersistent(profileId); controllerFailoverProbeSources = undefined; controllerFailoverReserveHealthy = true } + } + if (options.controllerFailoverOnly) return { ...await acceptControllerFailover(), persistentAuthorityCases: 0 } + if (options.bareReadbackOnly) { + const config = profile("a") + config.candidates = config.candidates.map(candidate => ({ ...candidate, gatewayRef: { resource: "gre", id: "*e" + candidate.id } })) + const persistent = await persistentFixture(config), context = { principal, connection: await effectiveMcpConnection(principal) } + const readProfile = async () => (await pool.query("SELECT enabled,state,grant_expires_at FROM mcp_routing_profiles WHERE id=$1", [persistent.id])).rows[0] + const enrolled = await readProfile(), grant = enrolled.grant_expires_at.toISOString() + const previous = await assertRoutingControllerBaseline(enrolled.state.baseline, persistent.id, persistent.version, config, grant, context) + const { plan, input } = await prepare("a", persistent, config, true) + assert.equal(originalEffectSelectionSchema.parse(input.originalEffectSelection).status, "prepared") + const forward = options.forwardCheckpoint + ? { id: await options.forwardCheckpoint({ token: access.token, connectionId, planId: plan.planId }), state: "applied" } + : await execute(plan.planId) + assert.equal(forward.state, "applied") + const seal = await assertScopedCommunityRequest(input) + const checkpoint = await loadScopedConfigurationPacket(input, seal, { operationId: forward.id, stepIndex: 0 }) + const network = await loadScopedNetworkCheckpoint(input, seal) + assert.equal(network?.originalEffects?.status, "observed-expected") + assert.ok(checkpoint) + assert.match(checkpoint.packet.desiredRule, / set gw-interface "gre-b";/) + assert.equal(filters[0].rule, bareRule(checkpoint.packet.desiredRule)) + assert.deepEqual(patchBodies, [checkpoint.packet.desiredRule]) + + // Link the operation to the tick as for the existing effect fixtures. + // The worker still validates the signed plan, charge and native readback. + await pool.query("UPDATE mcp_routing_profiles SET state=state || $2::jsonb WHERE id=$1", [persistent.id, JSON.stringify({ planId: plan.planId, candidateId: "b" })]) + await tickRoutingProfiles() + const published = await readProfile() + assert.equal(published.enabled, true, String(published.state.pausedReason)) + assert.equal(published.state.planId, undefined) + const advanced = await assertRoutingControllerBaseline(published.state.baseline, persistent.id, persistent.version, config, grant, context) + assert.equal(advanced.packet.candidateId, "b") + assert.equal(advanced.packet.previousProof, previous.proof) + assert.notEqual(advanced.packet.targetProof, checkpoint.packet.desiredTargetProof) + assert.deepEqual(advanced.packet.transition, { planId: plan.planId, operationId: forward.id, checkpointProof: checkpoint.checkpointProof, + sourceDesiredTargetProof: checkpoint.packet.desiredTargetProof }) + + const beforeInversePreparation = requests + const inverse = await callInverseTool("mm_operation_recovery_plan", { + operationId: forward.id, ...(options.inverseRouteEffects ? { observation: "route-effects-v1" } : {}), + }) as { planId: string; expiresAt: string; changes: { steps: Array<{ input: { observation?: string } }> } } + const inversePreparationRequests = requests - beforeInversePreparation + assert.notEqual(inverse.planId, plan.planId) + const inversePlan = await loadPlan(inverse.planId) + const inverseEvidence = JSON.parse(await decryptSecret(inversePlan.evidence.sealed)) + assert.equal(inverse.expiresAt, inverseEvidence.validUntil) + const reviewedInverse = await publicPlan(inversePlan) + assert.equal(reviewedInverse.expiresAt, inverse.expiresAt) + if (options.inverseRouteEffects) { + assert.equal(inverse.changes.steps[0].input.observation, "route-effects-v1") + assert.equal((reviewedInverse.changes as typeof inverse.changes).steps[0].input.observation, "route-effects-v1") + const publicReview = JSON.stringify([inverse, reviewedInverse]) + assert.doesNotMatch(publicReview, /observationCiphertext|originalEffectSelection|routeSelectionScope|203\.0\.113\.0\/24|\*effect1/) + } + await assert.rejects(applyPlan(principal, inverse.planId, randomUUID()), { code: "APPROVAL_REQUIRED" }) + assert.equal(writes, 1) + await approvePlan(inverse.planId, writer, true) + if (options.inverseCheckpointDrift || options.inverseEffectsCheckpointDrift) { + const logReadsBeforeInverse = effectLogReads + let tamperedOperationId: string | undefined + afterInverseConfigurationCheckpoint = async operationId => { + const { rows } = await pool.query<{ configuration_sealed: string; configuration_proof: string }>(`SELECT c.configuration_sealed,c.configuration_proof + FROM mcp_operation_checkpoints c JOIN mcp_operations o ON o.id=c.operation_id + WHERE c.operation_id=$1 AND c.step_index=0 AND o.plan_id=$2 AND o.state='running'`, [operationId, inverse.planId]) + assert.equal(rows.length, 1) + const row = rows[0] + const checkpoint = JSON.parse(await decryptSecret(row.configuration_sealed)) + assert.equal(checkpoint.phase, "RECOVERY_CONFIG_APPLIED") + assert.equal(checkpoint.operationId, operationId) + assert.match(row.configuration_proof, /^[a-f0-9]{64}$/) + const tampered = await pool.query(`UPDATE mcp_operation_checkpoints SET configuration_proof=$2 + WHERE operation_id=$1 AND step_index=0 AND configuration_proof=$3`, [operationId, fingerprint(row.configuration_proof), row.configuration_proof]) + assert.equal(tampered.rowCount, 1) + tamperedOperationId = operationId + } + const key = randomUUID(), admitted = await applyPlan(principal, inverse.planId, key) + assert.equal(await waitForOperation(admitted.id), "unknown") + assert.equal(tamperedOperationId, admitted.id) + const terminal = await invokeMcpTool(principal, "mm_operation_get", { operationId: admitted.id }) as { + id: string; plan_id: string; state: string; error_code: string + } + assert.equal(terminal.id, admitted.id) + assert.equal(terminal.plan_id, inverse.planId) + assert.equal(terminal.state, "unknown") + assert.equal(terminal.error_code, "MUTATION_OUTCOME_UNKNOWN") + assert.deepEqual(patchBodies, [checkpoint.packet.desiredRule, initial]) + assert.equal(writes, 2) + assert.equal(filters[0].rule, bareRule(initial)) + const { rows: intents } = await pool.query(`SELECT state,recovery_operation_id,verified_sealed,verified_proof + FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1`, [forward.id]) + assert.deepEqual(intents, [{ state: "unknown", recovery_operation_id: admitted.id, verified_sealed: null, verified_proof: null }]) + const { rows: failures } = await pool.query(`SELECT payload->>'phase' AS phase,payload->>'code' AS code + FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.recovery-unconfirmed'`, [connectionId]) + assert.deepEqual(failures, [{ phase: options.inverseEffectsCheckpointDrift ? "route-effects" : "post-probe", code: "STATE_CONFLICT" }]) + const inverseLogReads = effectLogReads - logReadsBeforeInverse + if (options.inverseEffectsCheckpointDrift) { + assert.equal(inverseLogReads, 2) + assert.equal(inversePostChecks, 0, "Checkpoint drift must stop before the post-inverse control probe") + const { rows } = await pool.query("SELECT id FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.recovery-route-effects' AND payload->>'operationId'=$2", + [connectionId, admitted.id]) + assert.deepEqual(rows, []) + const checked = await reconcile(admitted.id) + assert.equal(checked.observation, undefined) + assert.equal(checked.matchesDesiredState, false) + } + assert.equal((await applyPlan(principal, inverse.planId, key)).id, admitted.id) + assert.equal(writes, 2) + return { accepted: true, realSql: true, loopbackHttp: true, terminalState: terminal.state, + terminalCode: terminal.error_code, failure: failures[0], inverseNativeWrites: 1, repeatedInverseWrites: 0, + ...(options.inverseEffectsCheckpointDrift ? { inverseLogReads, observationPublished: false } : {}) } + } + if (options.expireDuringInverseProbe || options.expireDuringInversePostProbe) { + const realNow = Date.now + let expiredDuringProbe = false + inverseProbeHook = () => { + expiredDuringProbe = true + Date.now = () => Date.parse(inverse.expiresAt) + 1 + } + let operationId: string + try { operationId = (await execute(inverse.planId)).id } + finally { Date.now = realNow; inverseProbeHook = undefined } + assert(expiredDuringProbe) + const terminal = await invokeMcpTool(principal, "mm_operation_get", { operationId }) as { + id: string; plan_id: string; state: string; error_code: string + } + assert.equal(terminal.id, operationId) + assert.equal(terminal.plan_id, inverse.planId) + assert.equal(terminal.state, "unknown") + if (options.expireDuringInversePostProbe) { + assert.equal(terminal.error_code, "MUTATION_OUTCOME_UNKNOWN") + assert.equal(writes, 2) + assert.equal(filters[0].rule, bareRule(initial)) + const { rows: intents } = await pool.query(`SELECT state, recovery_operation_id FROM mcp_scoped_recovery_intents + WHERE source_operation_id=$1`, [forward.id]) + assert.deepEqual(intents, [{ state: "unknown", recovery_operation_id: operationId }]) + const { rows: [failure] } = await pool.query(`SELECT payload->>'phase' AS phase,payload->>'code' AS code + FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.recovery-unconfirmed'`, [connectionId]) + assert.deepEqual(failure, { phase: "post-probe", code: "GRANT_INACTIVE" }) + assert.equal((await applyPlan(principal, inverse.planId, randomUUID())).id, operationId) + assert.equal(writes, 2) + return { accepted: true, realSql: true, loopbackHttp: true, terminalState: terminal.state, + terminalCode: terminal.error_code, failure, inverseNativeWrites: 1, repeatedInverseWrites: 0 } + } + assert.equal(terminal.error_code, "GRANT_INACTIVE") + assert.equal(writes, 1) + assert.equal(filters[0].rule, bareRule(checkpoint.packet.desiredRule)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE recovery_operation_id=$1", [operationId])).rows[0].n, 0) + const { rows: [failure] } = await pool.query(`SELECT payload->>'phase' AS phase,payload->>'code' AS code + FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.recovery-unconfirmed'`, [connectionId]) + assert.deepEqual(failure, { phase: "pre-dispatch-path", code: "GRANT_INACTIVE" }) + return { accepted: true, realSql: true, loopbackHttp: true, expireDuringInverseProbe: true, + terminalState: terminal.state, terminalCode: terminal.error_code, failure, inverseNativeWrites: 0, durableInverseIntents: 0 } + } + if (options.loseInverseAck) { + mode = "lost" + const key = randomUUID() + const admitted = await applyPlan(principal, inverse.planId, key) + assert.equal(await waitForOperation(admitted.id), "unknown") + mode = "normal" + const terminal = await invokeMcpTool(principal, "mm_operation_get", { operationId: admitted.id }) as { + id: string; plan_id: string; connection_id: string; state: string; error_code: string + } + assert.equal(terminal.id, admitted.id) + assert.equal(terminal.plan_id, inverse.planId) + assert.equal(terminal.connection_id, connectionId) + assert.equal(terminal.state, "unknown") + assert.equal(terminal.error_code, "MUTATION_OUTCOME_UNKNOWN") + assert.equal(filters[0].rule, bareRule(initial)) + assert.equal(filters[1].rule, foreign) + assert.deepEqual(patchBodies, [checkpoint.packet.desiredRule, initial]) + const { rows: intents } = await pool.query(`SELECT state, recovery_operation_id FROM mcp_scoped_recovery_intents + WHERE source_operation_id=$1`, [forward.id]) + assert.deepEqual(intents, [{ state: "unknown", recovery_operation_id: admitted.id }]) + assert.equal((await applyPlan(principal, inverse.planId, key)).id, admitted.id) + assert.equal((await applyPlan(principal, inverse.planId, randomUUID())).id, admitted.id) + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: forward.id }), + { code: "MUTATION_OUTCOME_UNKNOWN" }) + assert.equal(writes, 2) + return { accepted: true, realSql: true, loopbackHttp: true, terminalState: terminal.state, + terminalCode: terminal.error_code, inverseNativeWrites: 1, repeatedInverseWrites: 0 } + } + const beforeInverseExecution = requests, logReadsBeforeInverse = effectLogReads + const recovered = await execute(inverse.planId) + const inverseExecutionRequests = requests - beforeInverseExecution + assert.equal(recovered.state, "applied") + assert.equal(filters[0].rule, bareRule(initial)) + assert.equal(filters[1].rule, foreign) + assert.deepEqual(patchBodies, [checkpoint.packet.desiredRule, initial]) + assert.equal(writes, 2) + if (options.forwardCheckpoint) { + assert.equal(inversePreparationRequests, 74, "Prepare must reuse its independently read post-probe context") + assert.equal(inverseExecutionRequests, 257, "Apply must reuse post-probe context without dropping boundary reads") + } + const inverseIntents = (await pool.query("SELECT state,recovery_operation_id FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [forward.id])).rows + assert.deepEqual(inverseIntents, [{ state: "verified", recovery_operation_id: recovered.id }]) + assert.equal(inversePostChecks, 1) + const inverseLogReads = effectLogReads - logReadsBeforeInverse + if (options.inverseRouteEffects) { + assert.equal(inverseLogReads, 3, "One baseline and two observations read the fixed log menu at most once each") + const { rows } = await pool.query(`SELECT a.payload->'observation' AS observation, + a.payload->>'configurationProof' AS observed_proof,c.configuration_proof + FROM mcp_audit a JOIN mcp_operation_checkpoints c ON c.operation_id=$2 AND c.step_index=0 + WHERE a.connection_id=$1 AND a.action='routing.scoped.recovery-route-effects' AND a.payload->>'operationId'=c.operation_id::text`, [connectionId, recovered.id]) + assert.equal(rows.length, 1) + assert.equal(rows[0].observed_proof, rows[0].configuration_proof) + const observation = rows[0].observation + const { rounds, ...summary } = observation as { rounds: Array<{ observedAt: string }> } + const expectedRound = { routes: { expected: 1, other: 0, unknown: 0 }, bgp: "observed-expected", bfd: "not-configured", relatedTopicRows: 1 } + assert.deepEqual(rounds.map(({ observedAt, ...round }) => { + assert.ok(Number.isFinite(Date.parse(observedAt))) + return round + }), [expectedRound, expectedRound]) + assert.deepEqual(summary, { + status: "unknown", transitions: 1, + logCoverage: "unknown", forwardingVerified: false, autoRecoveryAllowed: false, + }) + const checked = await reconcile(recovered.id) + assert.equal(checked.matchesDesiredState, true) + assert.deepEqual(checked.observation, observation) + assert.equal(writes, 2, "Diagnostic reads and read-only reconciliation must not issue another PATCH") + } else if (!options.forwardCheckpoint) { + const beforeReconcile = writes + assert.equal((await reconcile(recovered.id)).matchesDesiredState, true) + const { rows } = await pool.query<{ configuration_sealed: string; configuration_proof: string }>( + "SELECT configuration_sealed,configuration_proof FROM mcp_operation_checkpoints WHERE operation_id=$1 AND step_index=0", + [recovered.id]) + assert.equal(rows.length, 1) + const { configuration_sealed, configuration_proof } = rows[0] + const config = JSON.parse(await decryptSecret(configuration_sealed)) + assert.equal(config.phase, "RECOVERY_CONFIG_APPLIED") + assert.equal(config.operationId, recovered.id) + const impossible = { ...config, observedAt: new Date(Date.parse(config.createdAt) - 1).toISOString() } + const impossibleSeal = await encryptSecret(JSON.stringify(impossible)) + const impossibleProof = await privateSecretFingerprint(fingerprint(impossible), "route-ai-scoped-recovery-configuration-v1") + try { + const changed = await pool.query("UPDATE mcp_operation_checkpoints SET configuration_sealed=$2,configuration_proof=$3 WHERE operation_id=$1 AND step_index=0", + [recovered.id, impossibleSeal, impossibleProof]) + assert.equal(changed.rowCount, 1) + const checked = await reconcile(recovered.id) + assert.equal(checked.configurationMatchesDesired, true) + assert.equal(checked.recoveryCheckpointAvailable, false) + assert.equal(checked.controlProbeVerified, false) + assert.equal(checked.matchesDesiredState, false) + } finally { + const restored = await pool.query("UPDATE mcp_operation_checkpoints SET configuration_sealed=$2,configuration_proof=$3 WHERE operation_id=$1 AND step_index=0", + [recovered.id, configuration_sealed, configuration_proof]) + assert.equal(restored.rowCount, 1) + } + assert.equal((await reconcile(recovered.id)).matchesDesiredState, true) + const driftedProof = fingerprint(configuration_proof) + let journalDriftInjected = false + afterRecoveryJournalRead = async journal => { + assert.equal(journal.length, 1) + assert.equal(journal[0].recovery_operation_id, recovered.id) + assert.equal(journal[0].configuration_proof, configuration_proof) + const changed = await originalQuery("UPDATE mcp_operation_checkpoints SET configuration_proof=$2 WHERE operation_id=$1 AND step_index=0 AND configuration_proof=$3", + [recovered.id, driftedProof, configuration_proof]) + assert.equal(changed.rowCount, 1) + journalDriftInjected = true + } + try { + await assert.rejects(reconcile(recovered.id), { code: "STATE_CONFLICT" }) + assert.equal(journalDriftInjected, true) + } finally { + afterRecoveryJournalRead = undefined + if (journalDriftInjected) { + const restored = await pool.query("UPDATE mcp_operation_checkpoints SET configuration_proof=$2 WHERE operation_id=$1 AND step_index=0 AND configuration_proof=$3", + [recovered.id, configuration_proof, driftedProof]) + assert.equal(restored.rowCount, 1) + } + } + assert.equal((await reconcile(recovered.id)).matchesDesiredState, true) + assert.equal(writes, beforeReconcile) + } + await tickRoutingProfiles() + const paused = await readProfile() + assert.equal(paused.enabled, false) + assert.equal(paused.state.pausedReason, "STATE_CONFLICT") + assert.deepEqual(paused.state.baseline, advanced) + assert.deepEqual((await pool.query("SELECT * FROM filter_rules WHERE server_id=$1 ORDER BY id", [sid])).rows, dbBefore) + return { accepted: true, realSql: true, loopbackHttp: true, signedBaselinePublished: true, + sameSourceApprovedInverseVerified: true, controllerPausedAfterInverse: true, nativeWrites: writes, + inversePreparationRequests, inverseExecutionRequests, ...(options.forwardCheckpoint ? { + resumedForwardOperationId: forward.id, inverseOperationId: recovered.id, + } : options.inverseRouteEffects ? { inverseRouteEffectsObserved: true, inverseLogReads } : + { impossibleInverseChronologyRejected: true, inverseJournalDriftRejected: true }) } + } + if (options.originalEffectsOnly) { + const cases: Array> = [] + let publicManualRevision2: Record | undefined + const delegatedAdmin: AuthUser = { ...writer, id: "scoped-delegated-approval-admin", permissions: [...new Set([...writer.permissions, "mm:mcp:admin", "mm:mcp:approve"])] } + const snapshotApprovalAndAdmission = async (planId: string) => (await pool.query(`SELECT + (SELECT count(*)::int FROM mcp_operations WHERE plan_id=$1) AS operations, + (SELECT count(*)::int FROM mcp_routing_forward_charges WHERE source_plan_id=$1) AS forward_charges, + (SELECT COALESCE(jsonb_object_agg(bucket,used),'{}'::jsonb) FROM mcp_budget_buckets WHERE connection_id=$2 AND kind='mutations') AS mutation_buckets, + (SELECT count(*)::int FROM mcp_audit WHERE connection_id=$2 AND action='plan.approve' AND payload->>'planId'=$1::text) AS approval_audit`, [planId, connectionId])).rows[0] + const assertOwnerApproval = async (planId: string) => { + const nativeBefore = { requests, writes } + const before = await snapshotApprovalAndAdmission(planId) + assert.equal(before.operations, 0); assert.equal(before.forward_charges, 0); assert.equal(before.approval_audit, 0) + await assert.rejects(approvePlan(planId, delegatedAdmin, true), (error: unknown) => error instanceof McpError && error.code === "PERMISSION_DENIED") + const denied = await loadPlan(planId) + assert.equal(denied.state, "awaiting-approval"); assert.equal(denied.approved_by, null) + assert.deepEqual(await snapshotApprovalAndAdmission(planId), before) + // Simulate a delegated approval stored by an older deployment. + await pool.query("UPDATE mcp_plans SET state='approved',approved_by=$2,approved_at=now() WHERE id=$1", [planId, delegatedAdmin.id]) + try { + await assert.rejects(applyPlan(principal, planId, randomUUID()), (error: unknown) => error instanceof McpError && error.code === "APPROVAL_REQUIRED") + const stale = await loadPlan(planId) + assert.equal(stale.state, "approved"); assert.equal(stale.approved_by, delegatedAdmin.id) + assert.deepEqual(await snapshotApprovalAndAdmission(planId), before) + assert.deepEqual({ requests, writes }, nativeBefore) + } finally { + await pool.query("UPDATE mcp_plans SET state='awaiting-approval',approved_by=NULL,approved_at=NULL WHERE id=$1", [planId]) + } + await approvePlan(planId, writer, true) + } + for (const scenario of ["transition", "no-op", "ambiguous", "baseline-drift", "public-manual-rev2"] as const) { + const manual = scenario === "public-manual-rev2" + const effectCase = manual || scenario === "baseline-drift" ? "transition" : scenario + reset(); await refresh(); originalEffectCase = effectCase + const config = profile("a") + assert.equal(config.autoRecovery, false) + // The baseline and persistent authority are issued by the SAME actual + // fixed readers/CAS used by existing tests, not hand-written proofs. + const persistent = manual ? undefined : await persistentFixture(config) + try { + const { plan, input, evidence } = await prepare("a", persistent, config, manual, manual ? delegatedAdmin : scenario === "transition" ? null : writer) + if (scenario === "transition") await assertOwnerApproval(plan.planId) + assert.equal(input.manualEffectPolicy, manual ? "observational-v1" : undefined) + if (manual) assert.equal(input.persistentProfileAuthority, undefined) + const sealedSelection = originalEffectSelectionSchema.parse(input.originalEffectSelection) + assert.equal(sealedSelection.status, effectCase === "ambiguous" ? "unknown" : "prepared") + const storedPlan = await loadPlan(plan.planId) + assert.equal(storedPlan.approved_by, manual ? delegatedAdmin.id : writer.id) + if (manual) assert.equal(evidence.validUntil, undefined, "Manual scoped plans keep their existing preparation lifetime") + if (scenario === "transition") { + type ProbeRow = { created_at: Date; evidence: { observedAt: string } } + const probe: ProbeRow = (await pool.query( + "SELECT created_at,evidence FROM mcp_path_checks WHERE id=$1 AND connection_id=$2", [input.pathCheckId, connectionId])).rows[0] + const probeDeadline = Math.min(probe.created_at.getTime(), Date.parse(probe.evidence.observedAt)) + 90_000 + const authorityDeadline = Date.parse(input.persistentProfileAuthority.packet.validUntil) + assert(probeDeadline < authorityDeadline, "The fixture must exercise the probe cap, not the authority cap") + assert.equal(evidence.validUntil, new Date(probeDeadline).toISOString()) + assert.equal(storedPlan.expires_at.getTime(), probeDeadline) + + // A newer successful observation cannot extend an already approved plan. + const newerProbe = await invokeMcpTool(principal, "mm_route_path_verify", { profile: config, candidateId: "b" }) as { id: string; status: string; observedAt: string } + assert.equal(newerProbe.status, "passed") + assert.notEqual(newerProbe.id, input.pathCheckId) + assert(Date.parse(newerProbe.observedAt) + 90_000 > probeDeadline + 1) + const nativeBefore = { requests, writes }, admissionBefore = await snapshotApprovalAndAdmission(plan.planId) + const refuseAfterProbeDeadline = async (code: string) => { + const realNow = Date.now + try { + Date.now = () => probeDeadline + 1 + await assert.rejects(applyPlan(principal, plan.planId, randomUUID()), (error: unknown) => error instanceof McpError && error.code === code) + } finally { Date.now = realNow } + assert.deepEqual(await snapshotApprovalAndAdmission(plan.planId), admissionBefore) + assert.deepEqual({ requests, writes }, nativeBefore) + assert.equal((await loadPlan(plan.planId)).state, "approved") + } + await refuseAfterProbeDeadline("PLAN_EXPIRED") + + // The old deployment stored only the authority deadline in evidence. + const legacyValidity = input.persistentProfileAuthority.packet.validUntil as string + const legacyEvidence = { sealed: await encryptSecret(JSON.stringify({ ...evidence, validUntil: legacyValidity })) } + await pool.query("UPDATE mcp_plans SET expires_at=$2,evidence=$3::jsonb WHERE id=$1", [plan.planId, legacyValidity, JSON.stringify(legacyEvidence)]) + try { + await refuseAfterProbeDeadline("MEASUREMENT_UNKNOWN") + } finally { + await pool.query("UPDATE mcp_plans SET expires_at=$2,evidence=$3::jsonb WHERE id=$1", [plan.planId, storedPlan.expires_at, JSON.stringify(storedPlan.evidence)]) + } + cases.push({ case: "expired-approved-probe", newerProbeCannotRenew: true, scopedPlanCode: "PLAN_EXPIRED", legacyPlanCode: "MEASUREMENT_UNKNOWN", nativeRequests: 0, nativeWrites: 0, admissions: 0 }) + } + const publicProjection = JSON.stringify(await publicPlan(storedPlan)) + assert(!publicProjection.includes("originalEffectSelection")) + assert(!publicProjection.includes("manualEffectPolicy")) + const beforeWrites = writes, wireStart = originalEffectWire.length + const configurationIntents = async () => (await pool.query(`SELECT count(*)::int AS n FROM mcp_audit + WHERE connection_id=$1 AND action='configuration.intent'`, [connectionId])).rows[0].n + const intentsBefore = await configurationIntents() + if (scenario === "baseline-drift") originalEffectCase = "no-op" + const operation = await execute(plan.planId) + if (scenario === "ambiguous" || scenario === "baseline-drift") { + assert.equal(operation.state, "unknown") + const { rows: [refusal] } = await pool.query("SELECT error_code FROM mcp_operations WHERE id=$1", [operation.id]) + assert.equal(refusal.error_code, "MEASUREMENT_UNKNOWN") + assert.equal(writes, beforeWrites) + assert.equal(filters[0].rule, initial) + assert.equal(await configurationIntents(), intentsBefore) + const receiptCount: number = (await pool.query<{ n: number }>(`SELECT count(*)::int AS n FROM mcp_audit + WHERE connection_id=$1 AND action='routing.scoped.attempt' AND payload->>'receiptId'=$2`, [connectionId, input.receiptId])).rows[0].n + assert.equal(receiptCount, 0) + const runningWire = originalEffectWire.slice(wireStart) + assert.equal(runningWire.filter(row => row.method === "GET" && row.path === "/routing/route").length, scenario === "baseline-drift" ? 1 : 0) + cases.push({ case: scenario, state: operation.state, errorCode: refusal.error_code, nativeWrites: 0, attemptReceipts: 0 }) + continue + } + assert.equal(operation.state, "applied") + assert.equal(writes - beforeWrites, 1) + const rows: Array<{ created_at: Date; state: string; operation_id: string; operation_version: number; step_state: string; + result: { configurationVerified: boolean; controlProbeVerified: boolean; originalEffects: unknown } }> = (await pool.query(`SELECT o.created_at,o.state,p.operation_id,p.operation_version,s.state AS step_state,s.result + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE o.id=$1 AND o.connection_id=$2 AND o.plan_id=$3 AND s.step_index=0`, [operation.id, connectionId, plan.planId])).rows + assert.equal(rows.length, 1); assert.equal(rows[0].operation_id, "routing.community.switch") + assert.equal(rows[0].state, "applied"); assert.equal(rows[0].step_state, "verified") + assert.equal(rows[0].result.configurationVerified, true); assert.equal(rows[0].result.controlProbeVerified, true) + const report = originalEffectReportSchema.parse(rows[0].result.originalEffects) + const requestSeal = await assertScopedCommunityRequest(input) + if (manual) { + const changedInput = { ...input, manualEffectPolicy: "unapproved-policy" } + await assert.rejects(assertScopedCommunityRequest(changedInput), (error: unknown) => error instanceof McpError && error.code === "STATE_CONFLICT") + } + const configuration = await loadScopedConfigurationPacket(input, requestSeal, { operationId: operation.id, stepIndex: 0 }) + assert(configuration); assert.equal(configuration.packet.operationId, operation.id) + assert.equal(configuration.packet.revision, manual ? 1 : 2); assert.equal(configuration.packet.phase, "CONFIG_APPLIED") + const network = await loadScopedNetworkCheckpoint(input, requestSeal) + assert(network); assert.equal(network.status, "passed"); assert.deepEqual(network.originalEffects, report) + const runningWire = originalEffectWire.slice(wireStart) + const ribBefore = runningWire.filter(row => row.method === "GET" && row.path === "/routing/route" && !row.afterPatch).length + const ribAfter = runningWire.filter(row => row.method === "GET" && row.path === "/routing/route" && row.afterPatch).length + console.log("Owned original effect diagnostic:", JSON.stringify({ case: scenario, state: operation.state, + status: report.status, reason: report.reason, rounds: report.rounds.length, budget: report.budget ?? null, + ribBefore, ribAfter, runtimeNativeRequests: runningWire.length, lastNativePaths: runningWire.slice(-8).map(row => row.path) })) + assert.equal(ribBefore, 1) + assert.equal(ribAfter, 2) + assert.equal(report.status, effectCase === "transition" ? "observed-expected" : "unknown") + assert.equal(report.switchEffectObserved, effectCase === "transition") + assert.equal(report.logCoverage, "unknown"); assert.equal(report.routeOriginVerified, false) + assert.equal(report.forwardingVerified, false); assert.equal(report.semanticProof, false); assert.equal(report.autoRecoveryAllowed, false) + if (sealedSelection.status === "prepared") { + assert.equal(report.selectionIdentity, fingerprint(sealedSelection)); assert.equal(report.rounds.length, 2) + assert(report.budget && report.budget.calls > 18 && report.budget.calls <= 256) + assert(report.budget.bytes > 0 && report.budget.bytes <= 8 * 1048576) + assert(report.budget.rows > 0 && report.budget.rows <= 10000) + assert(report.budget.deadline <= rows[0].created_at.getTime() + 105000, "Original120s horizon is narrowed by15s publication reserve") + assert(report.rounds.every(round => Date.parse(round.observedAt) < report.budget!.deadline && round.controlProbe === "passed" && round.bgp === "observed-expected" && round.bfd === "not-configured")) + assert.deepEqual(report.transition?.changedPrefixes, effectCase === "transition" ? ["203.0.113.0/24"] : []) + assert.equal(report.transitionAttribution, "unproven") + } + if (manual) { + const repeatReads = requests + const repeated = await applyPlan(principal, plan.planId, randomUUID()) + assert.equal(repeated.id, operation.id) + assert.equal(repeated.state, "applied") + assert.equal(requests, repeatReads) + assert.equal(writes - beforeWrites, 1) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_routing_profiles WHERE enabled")).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [operation.id])).rows[0].n), 0) + publicManualRevision2 = { accepted: true, humanApprovalRequired: true, proofRevision: input.proofRevision, + configurationCheckpointRevision: configuration.packet.revision, effectStatus: report.status, + rounds: report.rounds.length, nativeRibBefore: ribBefore, nativeRibAfter: ribAfter, sharedBudget: report.budget, + autoRecoveryAllowed: report.autoRecoveryAllowed, scopedControllerEnabled: false, nativeWrites: 1 } + continue + } + assert(persistent) + const row = (await pool.query("SELECT state,grant_expires_at FROM mcp_routing_profiles WHERE id=$1 AND version=1", [persistent.id])).rows[0] + assert(row?.state.baseline) + const advance = () => advanceRoutingControllerBaseline(row.state.baseline, persistent.id, 1, config, row.grant_expires_at.toISOString(), + { principal, connection: principalConnection }, plan.planId, operation.id) + const principalConnection = await effectiveMcpConnection(principal), beforeAdvanceReads = requests + if (effectCase === "transition") { + const advanced = await advance(); assert.equal(advanced.packet.candidateId, "b") + assert.equal(advanced.packet.transition?.operationId, operation.id); assert.equal(advanced.packet.transition?.checkpointProof, configuration.checkpointProof) + } else { + await assert.rejects(advance, (error: unknown) => error instanceof McpError && error.code === "STATE_CONFLICT") + assert.equal(requests, beforeAdvanceReads, "Unknown/no-transition effect gate refuses before another native context read") + } + // Check actual HMAC loading, not only a caller-visible result object. + if (effectCase === "transition") { + const audit: Array<{ id: string; payload: Record }> = (await pool.query("SELECT id,payload FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.network-check' AND payload->>'receiptId'=$2", [connectionId, input.receiptId])).rows + assert.equal(audit.length, 1) + try { + await pool.query("UPDATE mcp_audit SET payload=jsonb_set(payload,'{originalEffects,switchEffectObserved}','false'::jsonb) WHERE id=$1", [audit[0].id]) + assert.equal(await loadScopedNetworkCheckpoint(input, requestSeal), null, "Modified effect vector cannot reuse its original network HMAC") + } finally { await pool.query("UPDATE mcp_audit SET payload=$2::jsonb WHERE id=$1", [audit[0].id, JSON.stringify(audit[0].payload)]) } + assert.deepEqual((await loadScopedNetworkCheckpoint(input, requestSeal))?.originalEffects, report) + } + const repeatReads = requests + const repeated = await applyPlan(principal, plan.planId, randomUUID()) + assert.equal(repeated.id, operation.id, "Completed plan returns its original operation without replay") + assert.equal(repeated.state, "applied") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_operations WHERE plan_id=$1", [plan.planId])).rows[0].n), 1) + assert.equal(writes - beforeWrites, 1); assert.equal(requests, repeatReads, "Completed approved plan cannot repeat native work") + // Link the applied operation as the controller does when it issues + // a plan. This checks both outcomes at the real worker boundary. + await pool.query("UPDATE mcp_routing_profiles SET state=state || $2::jsonb WHERE id=$1", [persistent.id, JSON.stringify({ planId: plan.planId, candidateId: "b" })]) + await tickRoutingProfiles() + const completed = (await pool.query("SELECT enabled,state FROM mcp_routing_profiles WHERE id=$1", [persistent.id])).rows[0] + if (effectCase === "transition") { + assert.equal(completed.enabled, true) + assert.equal(completed.state.baseline.packet.transition.operationId, operation.id) + assert.equal(completed.state.expectedCandidateId, "b") + } else { + assert.equal(completed.enabled, false) + assert.equal(completed.state.pausedReason, "STATE_CONFLICT") + assert.deepEqual(completed.state.baseline, row.state.baseline) + } + cases.push({ case: effectCase, manualState: operation.state, configurationCheckpoint: true, actualPrivateExecutionOrigin: true, + effectStatus: report.status, observedFibTransition: report.switchEffectObserved, nativeRibBefore: ribBefore, nativeRibAfter: ribAfter, + sharedBudget: report.budget ?? null, effectBaselineAdvanceAccepted: effectCase === "transition", effectGateNativeReads: effectCase === "transition" ? "fresh-native-checked" : 0, + tickEffectPauseAccepted: effectCase !== "transition", publicManualRevision2EffectOwnerAccepted: false }) + } finally { if (persistent) await disablePersistent(persistent.id) } + } + assert(publicManualRevision2) + reset(); await refresh(); originalEffectCase = "transition"; exposeEffectCatalogue = true + const controllerConfig = profile("a") + controllerConfig.confirmationSamples = 1 + controllerConfig.weights = { ping: 100, speed: 0, loss: 0 } + controllerConfig.candidates[1].downstreamInterface = "target" + const fast = randomUUID() + await pool.query("UPDATE mcp_routing_observations SET ping_ms=100 WHERE src_server_id=$1 AND routing_table='tenant-a' AND src_interface='hop'", [sid]) + await pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source) VALUES($1,$2,$2,'target','peer','ipv4','tenant-a',1,0,100,100,now(),now(),true,'fixture')", [fast, sid]) + await refresh() + const activeProfileId = randomUUID(), beforeControllerWrites = writes + persistentProfileIds.push(activeProfileId) + const saveProfile = (config: typeof controllerConfig) => invokeMcpTool(principal, "mm_route_profile_save_plan", { + profileId: activeProfileId, profile: config, enabled: true, expiresAt: new Date(Date.now() + 600000).toISOString(), + }) as Promise<{ planId: string }> + for (const invalid of [ + { ...controllerConfig, bgpContext: undefined }, + { ...controllerConfig, autoRecovery: true }, + { ...controllerConfig, candidates: controllerConfig.candidates.map(candidate => ({ ...candidate, controlProbe: { ...candidate.controlProbe!, sourceAddress: undefined } })) }, + ]) { + await assert.rejects(saveProfile(invalid), (error: unknown) => error instanceof McpError && error.code === "UNSUPPORTED_CAPABILITY") + } + const savedProfile = await saveProfile(controllerConfig) + assert.equal((await loadPlan(savedProfile.planId)).state, "awaiting-approval") + await approvePlan(savedProfile.planId, writer, true) + assert.equal((await execute(savedProfile.planId)).state, "applied") + const readController = async () => (await pool.query("SELECT enabled,state FROM mcp_routing_profiles WHERE id=$1", [activeProfileId])).rows[0] + const enrolled = await readController() + assert.equal(enrolled.enabled, true); assert.equal(enrolled.state.baseline.packet.candidateId, "a") + controllerProbeUnknown = true + try { await tickRoutingProfiles() } finally { controllerProbeUnknown = false } + const waitingForProbe = await readController() + assert.equal(waitingForProbe.enabled, true, String(waitingForProbe.state.pausedReason)) + assert.equal(waitingForProbe.state.planId, undefined) + assert.equal(waitingForProbe.state.pendingCandidateId, "b", String(waitingForProbe.state.reason)) + assert.equal(writes, beforeControllerWrites) + // Let the next tick run without waiting for the production polling interval. + await pool.query("UPDATE mcp_routing_profiles SET state=state-'probedAt' WHERE id=$1", [activeProfileId]) + await tickRoutingProfiles() + const proposed = await readController() + assert.equal(proposed.enabled, true, String(proposed.state.pausedReason)) + assert.equal(proposed.state.candidateId, "b", String(proposed.state.reason)) + const switchPlanId: string = proposed.state.planId + assert.equal((await loadPlan(switchPlanId)).state, "awaiting-approval") + await tickRoutingProfiles() + assert.equal(writes, beforeControllerWrites) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE plan_id=$1", [switchPlanId])).rows[0].n, 0) + await approvePlan(switchPlanId, writer, true) + await tickRoutingProfiles() + const pendingSwitch = await readController() + const switchOperationId: string = pendingSwitch.state.operationId + assert.ok(switchOperationId, String(pendingSwitch.state.pausedReason)) + assert.equal(await waitForOperation(switchOperationId), "applied") + await tickRoutingProfiles() + const switched = await readController() + assert.equal(switched.enabled, true, String(switched.state.pausedReason)) + assert.equal(switched.state.planId, undefined) + assert.equal(switched.state.expectedCandidateId, "b") + assert.equal(switched.state.baseline.packet.candidateId, "b") + assert.equal(switched.state.baseline.packet.previousProof, enrolled.state.baseline.proof) + assert.equal(switched.state.baseline.packet.transition.operationId, switchOperationId) + assert.equal(switched.state.baseline.packet.transition.planId, switchPlanId) + assert.equal(writes - beforeControllerWrites, 1) + await disablePersistent(activeProfileId) + const boundedReserveFailover = await acceptControllerFailover() + assert.deepEqual((await pool.query("SELECT * FROM filter_rules WHERE server_id=$1 ORDER BY id", [sid])).rows, dbBefore) + const result = { accepted: true, cases, publicManualRevision2, boundedReserveFailover, persistentAuthorityCases: 0, realMcpDispatcher: true, realSql: true, routerOsTransport: "declared-synthetic-localhost-http", + genuineApprovedRunningOrigin: true, actualConfigurationCheckpoint: true, signedEffectTamperRejected: true, + originalAutoRecoveryEnabled: false, fullRoutingAccepted: false, scopedControllerEnabled: true, publicScopedControllerLifecycle: true, nativeChrAccepted: false, forwardingVerified: false } + console.log("Original effect SQL/runtime component:", JSON.stringify(result)) + return result + } + const assertOriginalCredentialRevocation = async () => { + console.log(JSON.stringify({stage:'original-credential-revocation:init'})) + reset(); await refresh() + console.log(JSON.stringify({stage:'original-credential-revocation:prepare-source'})) + const persistent = await persistentFixture(), { plan, input } = await prepare("a", persistent) + mode = "post-failed"; const source = await execute(plan.planId); assert.equal(source.state, "partial"); mode = "normal" + const newerId = randomUUID(), newerExpiry = principal.credentialExpiresAt + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [newerId, connectionId, randomUUID(), newerExpiry]) + const newer: McpPrincipal = { ...principal, credentialId: newerId } + const loaded = await loadScopedRecoverySource(source.id, { principal: newer, connection: await effectiveMcpConnection(newer) }) + assert.equal((loaded.input.principal as McpPrincipal).credentialId, principal.credentialId) + assert.equal(fingerprint(loaded.configuration.packet.revision === 2 && loaded.configuration.packet.persistentProfileAuthority), fingerprint(input.persistentProfileAuthority)) + const recovery = await invokeMcpTool(newer, "mm_operation_recovery_plan", { operationId: source.id }) as { planId: string } + await approvePlan(recovery.planId, writer, true) + const before = writes, beforeReads = requests + const snapshot = async () => (await pool.query(`SELECT + (SELECT state FROM mcp_plans WHERE id=$1) AS plan_state, + (SELECT count(*)::int FROM mcp_operations WHERE connection_id=$2) AS operations, + (SELECT count(*)::int FROM mcp_operations WHERE plan_id=$1) AS recovery_operations, + (SELECT COALESCE(jsonb_agg(jsonb_build_object('bucket',bucket,'used',used) ORDER BY bucket),'[]'::jsonb) FROM mcp_budget_buckets WHERE connection_id=$2 AND kind='mutations') AS mutation_buckets, + (SELECT count(*)::int FROM mcp_routing_manual_inverse_charges WHERE connection_id=$2) AS inverse_charges, + (SELECT count(*)::int FROM mcp_scoped_recovery_intents WHERE source_operation_id=$3) AS inverse_intents + `,[recovery.planId,connectionId,source.id])).rows[0] + const admittedBefore=await snapshot() + assert.equal(admittedBefore.plan_state,'approved');assert.equal(admittedBefore.recovery_operations,0);assert.equal(admittedBefore.inverse_intents,0) + try { + await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [principal.credentialId]) + const credentials=(await pool.query("SELECT id,revoked_at IS NOT NULL AS revoked,expires_at>now() AS unexpired FROM mcp_credentials WHERE id=ANY($1::uuid[])",[[principal.credentialId,newerId]])).rows + assert.equal(credentials.find(row=>row.id===principal.credentialId)?.revoked,true) + assert.equal(credentials.find(row=>row.id===newerId)?.revoked,false);assert(credentials.every(row=>row.unexpired)) + assert.equal((await effectiveMcpConnection(newer)).id,connectionId) + console.log(JSON.stringify({stage:'original-credential-revocation:expect-admission-refusal'})) + await assert.rejects(applyPlan(newer,recovery.planId,randomUUID()),(error:unknown)=>error instanceof McpError && error.code==='UNAUTHORIZED' && error.statusCode===401) + assert.deepEqual(await snapshot(),admittedBefore,"Revoked original authority must roll back operation, executing plan, mutation debit, charge and intent admission") + assert.equal(writes, before) + assert.equal(requests, beforeReads, "Newer recovery credential cannot replace revoked original authority before native reads") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [source.id])).rows[0].n, 0) + persistentCases.push("recovery-preserves-original-authority") + console.log(JSON.stringify({stage:'original-credential-revocation-complete',unauthorized401:true,fullAdmissionRollback:true,nativeReads:requests-beforeReads,nativeWrites:writes-before,wholeSuiteAccepted:false})) + } finally { + await pool.query("UPDATE mcp_credentials SET revoked_at=NULL WHERE id=$1", [principal.credentialId]) + await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [newerId]) + await disablePersistent(persistent.id) + } + } + if (options.originalCredentialRevocationOnly) { + await assertOriginalCredentialRevocation() + return {accepted:true,originalCredentialRevocation:true,unauthorized401:true,fullAdmissionRollback:true,nativeReads:0,nativeWrites:0,realSql:true,allWriterLedgerComplete:false} + } + if (options.manualInverseChargeOnly || options.lateInverseAuthorityChange) { + const chargeCase = options.manualInverseChargeCase ?? "positive" + if (options.lateInverseAuthorityChange) assert.equal(chargeCase, "positive") + const persistent = chargeCase !== "nonpersistent" + // Same genuine fixed-reader baseline/CAS used by the existing persistent + // acceptance below. No hand-written baseline, sealed plan or authority. + const id = randomUUID(), current = await effectiveMcpConnection(principal), config = profile("a") + assert.equal(config.autoRecovery, false) + if (chargeCase === "quota") config.maxSwitchesPerHour = 1 + if (chargeCase === "mixed-forward") config.maxSwitchesPerHour = 2 + const expiry = new Date(Math.min(Date.now() + 600000, current.owner_valid_until.getTime(), Date.parse(principal.credentialExpiresAt), Date.parse(current.policy.expiresAt))) + await withApplicationMutationLock(() => withConfigMutationLock([sid], async () => { + const baseline = await prepareRoutingControllerBaseline(id, 1, config, expiry.toISOString(), { principal, connection: current }) + const saved = await saveControllerProfileCas(pool, { profileId: id, connectionId, expectedVersion: null, profile: config, enabled: persistent, + expiresAt: expiry.toISOString(), credentialId: principal.credentialId, generation: await accessGeneration(), policyVersion: current.policy.version, baseline }) + assert.equal(saved?.version, 1) + })) + persistentProfileIds.push(id) + const ledger = createRoutingReservationStore() + let snapshotStage = "initial" + const snapshot = async () => { + console.log(JSON.stringify({stage:"manual-inverse:snapshot-hourly",chargeCase,snapshotStage})) + const hourly = await ledger.readHourlyCharges(id, principal) + console.log(JSON.stringify({stage:"manual-inverse:snapshot-generic",chargeCase,snapshotStage})) + const generic = Number((await pool.query("SELECT COALESCE(sum(used),0)::int AS used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations'", [connectionId])).rows[0].used) + console.log(JSON.stringify({stage:"manual-inverse:snapshot-complete",chargeCase,snapshotStage})) + return { forward: hourly.forwardCharges, manualInverse: hourly.manualInverseCharges, reserved: hourly.reservedCharges, legacy: hourly.legacyCharges, total: hourly.totalCharges, generic, writes } + } + console.log(JSON.stringify({ stage: "manual-inverse:before-forward" })) + const before = await snapshot() + assert.deepEqual(before, { forward: 0, manualInverse: 0, reserved: 0, legacy: 0, total: 0, generic: 0, writes: 0 }) + if (!persistent) { + const active = (await pool.query(`SELECT count(*)::int AS n FROM ( + SELECT 1 FROM mcp_routing_profiles WHERE enabled AND (config->>'serverId'=$1 OR EXISTS( + SELECT 1 FROM jsonb_array_elements(config->'candidates') AS candidate + WHERE candidate->>'jumpHostId'=$1 OR candidate->>'exitNodeId'=$1)) LIMIT 1 + ) exact_active_writer_guard`, [String(sid)])).rows[0].n + assert.equal(active,0,"A genuine nonpersistent request2 fixture must not compete with an enabled persistent server profile") + console.log(JSON.stringify({stage:"manual-inverse:nonpersistent-active-profile-zero",chargeCase,activeProfiles:active})) + } + console.log(JSON.stringify({stage:"manual-inverse:forward-prepare",chargeCase})) + const { plan, input } = await prepare("a", persistent ? { id, version: 1 } : undefined, config) + // Prepare the competing forward while the ORIGINAL native baseline is + // current. Admission must charge it even if execution later refuses the + // changed network state. No invented request/authority or reserved lane. + const competingForward = chargeCase === "mixed-forward" ? await prepare("a", { id, version: 1 }, config) : undefined + // Existing known partial configuration path: genuine committed CONFIG_APPLIED, + // then failed control probe, then genuine manual inverse positive to A. + mode = "post-failed" + console.log(JSON.stringify({stage:"manual-inverse:forward-apply",chargeCase})) + const source = await execute(plan.planId) + // Read-only terminal diagnostics for this exact operation/connection. + // Nothing private is logged; malformed/missing diagnostics never replace + // the original expected partial assertion or make unknown acceptable. + try { + const rows = (await pool.query(`SELECT + CASE WHEN o.state IN('pending','running','applied','partial','unknown','rejected') THEN o.state ELSE 'other' END AS operation_state, + CASE WHEN o.error_code IN('MUTATION_OUTCOME_UNKNOWN','VERIFICATION_FAILED','STATE_CONFLICT','READ_CANCELLED','PLAN_EXPIRED','PERMISSION_DENIED','DEVICE_UNAVAILABLE','RESET_REQUIRED','PROCESS_INTERRUPTED') THEN o.error_code ELSE 'other' END AS error_code, + CASE WHEN s.state IN('intent','applied','verified','unknown','failed') THEN s.state ELSE 'other' END AS step_state, + s.result IS NOT NULL AS step_result_present,c.configuration_sealed IS NOT NULL AS configuration_present, + c.configuration_proof IS NOT NULL AS configuration_proof_present, + CASE WHEN octet_length(c.configuration_sealed)<=524288 THEN c.configuration_sealed END AS private_configuration, + (SELECT count(*)::int FROM (SELECT 1 FROM mcp_audit a WHERE a.connection_id=o.connection_id AND a.action='routing.scoped.network-check' AND a.payload->>'receiptId'=$3 LIMIT 2) bounded_network) AS network_receipt_rows_at_most2 + FROM mcp_operations o LEFT JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + LEFT JOIN mcp_operation_checkpoints c ON c.operation_id=o.id AND c.step_index=0 + WHERE o.id=$1 AND o.connection_id=$2 LIMIT 2`, [source.id,connectionId,input.receiptId])).rows + let checkpointRevision: number | null = null, checkpointPhaseApplied = false, checkpointIdentityMatches = false + if (rows.length===1 && rows[0].private_configuration) { + try { + const packet = JSON.parse(await decryptSecret(rows[0].private_configuration)) + checkpointRevision = packet.revision===1 || packet.revision===2 ? packet.revision : null + checkpointPhaseApplied = packet.phase==="CONFIG_APPLIED" + checkpointIdentityMatches = packet.operationId===source.id && packet.stepIndex===0 && packet.connectionId===connectionId && packet.receiptId===input.receiptId + } catch { /* fixed unavailable flags below; no private exception */ } + } + console.log(JSON.stringify({stage:"manual-inverse:forward-terminal",chargeCase,exactOperationFound:rows.length===1, + operationState:rows[0]?.operation_state,errorCode:rows[0]?.error_code,stepState:rows[0]?.step_state, + stepResultPresent:rows[0]?.step_result_present,configurationPresent:rows[0]?.configuration_present, + configurationProofPresent:rows[0]?.configuration_proof_present,networkReceiptRowsAtMost2:rows[0]?.network_receipt_rows_at_most2, + requestRevision:input.proofRevision===2 ? 2 : input.proofRevision===3 ? 3 : null,checkpointRevision,checkpointPhaseApplied,checkpointIdentityMatches, + writes,postCheckpointsObserved,inversePostChecks})) + } catch { console.log(JSON.stringify({stage:"manual-inverse:forward-terminal-diagnostic-unavailable",chargeCase})) } + assert.equal(source.state, "partial"); mode = "normal" + console.log(JSON.stringify({ stage: "manual-inverse:after-forward" })) + snapshotStage = "after-forward" + const afterForward = await snapshot() + assert.equal(afterForward.forward, persistent ? 1 : 0); assert.equal(afterForward.reserved, 0); assert.equal(afterForward.legacy, 0); assert.equal(afterForward.total, persistent ? 1 : 0) + assert.equal(afterForward.writes, 1) + assert.equal(postCheckpointsObserved, 1) + console.log(JSON.stringify({ stage: "manual-inverse:reload-original-authority" })) + const loaded = await loadScopedRecoverySource(source.id, { principal, connection: await effectiveMcpConnection(principal) }) + assert.equal(fingerprint(loaded.input.persistentProfileAuthority), fingerprint(input.persistentProfileAuthority)) + assert.equal(loaded.configuration.packet.revision, persistent ? 2 : 1) + if (persistent) assert.equal(fingerprint(loaded.configuration.packet.revision === 2 && loaded.configuration.packet.persistentProfileAuthority), fingerprint(input.persistentProfileAuthority)) + console.log(JSON.stringify({ stage: "manual-inverse:human-recovery-prepare" })) + const recovery = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: source.id }) as { planId: string; state: string } + assert.equal(recovery.state, "awaiting-approval") + assert.equal((await loadPlan(recovery.planId)).operation_id, "operations.recover") + assert.equal((await loadPlan(recovery.planId)).operation_version, 8) + await assert.rejects(applyPlan(principal, recovery.planId, randomUUID())) + assert.deepEqual(await snapshot(), afterForward, "No approval means no debit/charge/native write") + await approvePlan(recovery.planId, writer, true) + if (chargeCase === "positive" && !options.lateInverseAuthorityChange) { + await pool.query("UPDATE mcp_plans SET operation_version=7 WHERE id=$1", [recovery.planId]) + try { + await assert.rejects(applyPlan(principal, recovery.planId, randomUUID()), { code: "PLAN_EXPIRED" }) + assert.equal((await pool.query("SELECT id FROM mcp_operations WHERE plan_id=$1", [recovery.planId])).rowCount, 0) + assert.deepEqual(await snapshot(), afterForward, "Superseded approved inverse cannot create an operation, debit or native write") + } finally { + await pool.query("UPDATE mcp_plans SET operation_version=8 WHERE id=$1", [recovery.planId]) + } + } + if (chargeCase === "mixed-forward") { + assert(competingForward) + const attempts = [ + { lane: "forward", planId: competingForward.plan.planId, key: randomUUID(), cost: 2 }, + { lane: "manual-inverse", planId: recovery.planId, key: randomUUID(), cost: 1 }, + ] as const + const initialPlans = await Promise.all(attempts.map(attempt => loadPlan(attempt.planId))) + assert(initialPlans.every(value => value.state === "approved")) + const admitted = await Promise.allSettled(attempts.map(attempt => applyPlan(principal, attempt.planId, attempt.key))) + const winners = admitted.flatMap((value, index) => value.status === "fulfilled" ? [index] : []) + assert.equal(winners.length, 1, "Two writer lanes must compete for the same last profile slot") + const winnerIndex = winners[0], loserIndex = 1 - winnerIndex + const winner = admitted[winnerIndex], loser = admitted[loserIndex] + assert(winner.status === "fulfilled" && loser.status === "rejected") + assert(loser.reason instanceof McpError && loser.reason.code === "STATE_CONFLICT", "The losing lane must fail profile admission") + assert.equal(fingerprint(await loadPlan(attempts[loserIndex].planId)), fingerprint(initialPlans[loserIndex]), "Losing plan is completely unchanged") + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE plan_id=$1", [attempts[loserIndex].planId])).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_forward_charges WHERE source_plan_id=$1", [attempts[loserIndex].planId])).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_manual_inverse_charges WHERE recovery_plan_id=$1", [attempts[loserIndex].planId])).rows[0].n), 0) + const deadline = Date.now() + 140000 + let terminalState = "pending" + while (["pending", "running"].includes(terminalState) && Date.now() < deadline) { + terminalState = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [winner.value.id])).rows[0].state + if (["pending", "running"].includes(terminalState)) await delay(20) + } + assert(!["pending", "running"].includes(terminalState), "Winner must retire before fixture cleanup") + if (requestFailure) throw requestFailure.error + snapshotStage = "mixed-writer-terminal" + const afterMixed = await snapshot() + assert.equal(afterMixed.total, 2) + assert.equal(afterMixed.forward, winnerIndex === 0 ? 2 : 1) + assert.equal(afterMixed.manualInverse, winnerIndex === 1 ? 1 : 0) + assert.equal(afterMixed.reserved, 0); assert.equal(afterMixed.legacy, 0) + assert.equal(afterMixed.generic, afterForward.generic + attempts[winnerIndex].cost, "Losing writer cannot debit generic quota") + if (winnerIndex === 1) { + const charges = (await pool.query("SELECT source_operation_id,source_step_index,recovery_operation_id FROM mcp_routing_manual_inverse_charges WHERE recovery_operation_id=$1", [winner.value.id])).rows + assert.equal(charges.length, 1); assert.equal(charges[0].source_operation_id, source.id) + assert.equal(charges[0].source_step_index, 0); assert.equal(charges[0].recovery_operation_id, winner.value.id) + assert.equal(terminalState, "applied"); assert.equal(afterMixed.writes, 2) + } + const beforeReplay = fingerprint(await snapshot()) + assert.equal((await applyPlan(principal, attempts[winnerIndex].planId, attempts[winnerIndex].key)).id, winner.value.id) + await delay(30) + assert.equal(fingerprint(await snapshot()), beforeReplay, "Same-key observation cannot recharge or replay either lane") + return { accepted: true, mixedWriterAdmission: true, winnerLane: attempts[winnerIndex].lane, before, afterForward, afterMixed, + exactlyOneAdmission: true, losingPlanOperationChargeAndGenericRollback: true, sameKeyReplayUnchanged: true, + realSql: true, allWriterLedgerComplete: false, reservedDispatchEnabled: false, autoRecoveryEnabled: false } + } + console.log(JSON.stringify({ stage: "manual-inverse:human-recovery-apply" })) + const lateChange = options.lateInverseAuthorityChange + if (lateChange) { + const priorIntent = (await pool.query("SELECT coalesce(max(id),0)::text AS id FROM mcp_audit WHERE connection_id=$1 AND action='configuration.intent'", [connectionId])).rows[0].id + let writeId: string | undefined + afterIntent = async () => { + const intents = (await pool.query(`SELECT payload FROM mcp_audit WHERE connection_id=$1 AND id>$2::bigint + AND action='configuration.intent' AND payload->>'path'=$3`, + [connectionId, priorIntent, "/routing/filter/rule/" + loaded.configuration.packet.nativeId])).rows + assert.equal(intents.length, 1) + assert.equal(intents[0].payload.method, "PATCH") + assert.equal(intents[0].payload.serverId, sid) + writeId = intents[0].payload.writeId + assert.match(writeId ?? "", /^[0-9a-f-]{36}$/) + if (lateChange === "path-age") { + delayContextAfterProofQuery = true + } else if (lateChange === "path-proof") { + const changed = await pool.query(`UPDATE mcp_path_checks SET evidence=jsonb_set(evidence,'{proof}',to_jsonb(repeat('0',64))) + WHERE id=(SELECT id FROM mcp_path_checks WHERE connection_id=$1 AND profile_hash=$2 AND candidate_id=$3 + AND evidence->>'proofPurpose'='community' AND evidence->>'proofVersion'='2' ORDER BY created_at DESC,id DESC LIMIT 1) + AND status='passed'`, [connectionId, fingerprint(config), loaded.configuration.packet.originalCandidateId]) + assert.equal(changed.rowCount, 1) + } else { + const sql = { + edit: "UPDATE mcp_routing_profiles SET version=version+1,config=jsonb_set(config,'{name}','\"late inverse edit\"') WHERE id=$1", + disable: "UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", + grant: "UPDATE mcp_routing_profiles SET grant_expires_at=clock_timestamp()-interval '1 second' WHERE id=$1", + }[lateChange] + assert.equal((await pool.query(sql, [id])).rowCount, 1) + } + } + const inverse = await execute(recovery.planId) + assert.equal(afterIntent, undefined) + assert.ok(writeId) + assert.notEqual(inverse.state, "applied") + assert.equal(writes, afterForward.writes) + assert.equal(inversePostChecks, 0) + const auditRows = (await pool.query(`SELECT action FROM mcp_audit WHERE connection_id=$1 AND payload->>'writeId'=$2 + ORDER BY id`, [connectionId, writeId])).rows + assert.deepEqual(auditRows.map(row => row.action), ["configuration.intent", "configuration.unconfirmed"]) + const refusal = (await pool.query(`SELECT payload->>'phase' AS phase, payload->>'code' AS code + FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.recovery-unconfirmed'`, [connectionId])).rows + assert.deepEqual(refusal, [{ phase: "before-dispatch-context", + code: lateChange === "path-proof" || lateChange === "path-age" ? "MEASUREMENT_UNKNOWN" : "STATE_CONFLICT" }]) + if (lateChange === "path-age") assert(!delayContextAfterProofQuery && !delayNextContextRead, "Context read must start with a still-fresh database proof") + assert.deepEqual((await pool.query(`SELECT state,verified_sealed FROM mcp_scoped_recovery_intents + WHERE source_operation_id=$1 AND recovery_operation_id=$2`, [source.id, inverse.id])).rows, + [{ state: "unknown", verified_sealed: null }]) + assert.equal((await pool.query(`SELECT count(*)::int AS n FROM mcp_operation_checkpoints + WHERE operation_id=$1 AND configuration_sealed IS NOT NULL`, [inverse.id])).rows[0].n, 0) + const terminal = await invokeMcpTool(principal, "mm_operation_get", { operationId: inverse.id }) as { + id: string; plan_id: string; connection_id: string; state: string; error_code: string + } + assert.equal(terminal.id, inverse.id) + assert.equal(terminal.plan_id, recovery.planId) + assert.equal(terminal.connection_id, connectionId) + assert.equal(terminal.state, "unknown") + assert.equal(terminal.error_code, "MUTATION_OUTCOME_UNKNOWN") + return { accepted: true, lateInverseAuthorityChange: lateChange, durableIntents: 1, inverseNativeWrites: 0, + publicTerminalState: terminal.state, publicTerminalCode: terminal.error_code, privateFailurePhase: refusal[0].phase, + realSql: true, realMcpDispatcher: true, fullInverseLifecycleRepeated: false } + } + if (chargeCase === "quota" || chargeCase === "revoked") { + if (chargeCase === "revoked") await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", [id]) + const beforeOperations = (await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE connection_id=$1", [connectionId])).rows[0].n + await assert.rejects(applyPlan(principal, recovery.planId, randomUUID())) + assert.deepEqual(await snapshot(), afterForward, "Refused inverse admission rolls back operation/plan/generic debit/profile charge before intent/PATCH") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE connection_id=$1", [connectionId])).rows[0].n, beforeOperations) + assert.equal((await loadPlan(recovery.planId)).state, "approved") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [source.id])).rows[0].n, 0) + return { accepted:true,manualInverseChargeFix:true,chargeCase,before,afterForward,afterInverse:afterForward, + admissionRefusedBeforeIntent:true,fullRollback:true,realSql:true,allWriterLedgerComplete:false } + } + let inverse: Awaited> + if (chargeCase === "concurrent") { + const competitor = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: source.id }) as {planId:string} + await approvePlan(competitor.planId, writer, true) + const results = await Promise.allSettled([execute(recovery.planId), execute(competitor.planId)]) + const winners = results.filter((result): result is PromiseFulfilledResult>> => result.status === "fulfilled") + assert.equal(winners.length, 1); assert.equal(results.filter(result => result.status === "rejected").length, 1) + inverse = winners[0].value + } else { + if (chargeCase === "unknown") mode = "lost" + inverse = await execute(recovery.planId); mode = "normal" + } + if (chargeCase !== "unknown") assert.equal(inverse.state, "applied") + else assert.notEqual(inverse.state, "applied") + console.log(JSON.stringify({ stage: "manual-inverse:after-inverse" })) + snapshotStage = "after-inverse" + const afterInverse = await snapshot() + assert.equal(afterInverse.generic, afterForward.generic + 1) + assert.equal(afterInverse.forward, persistent ? 1 : 0); assert.equal(afterInverse.manualInverse, persistent ? 1 : 0); assert.equal(afterInverse.total, persistent ? 2 : 0) + assert.equal(afterInverse.reserved, 0); assert.equal(afterInverse.legacy, 0) + assert.equal(afterInverse.writes, 2); if (chargeCase !== "unknown") assert.equal(inversePostChecks, 1) + const charges = (await pool.query("SELECT recovery_operation_id,recovery_plan_id,source_operation_id,profile_id,charge_units,binding_sealed FROM mcp_routing_manual_inverse_charges WHERE source_operation_id=$1", [source.id])).rows + assert.equal(charges.length, persistent ? 1 : 0); + if (persistent) { + assert.equal(charges[0].recovery_operation_id, inverse.id); assert.equal(charges[0].recovery_plan_id, inverse.plan_id); assert.equal(charges[0].profile_id,id); assert.equal(charges[0].charge_units,1) + const binding = JSON.parse(await decryptSecret(charges[0].binding_sealed)) + assert.equal(binding.operationVersion, 8) + assert.equal(binding.sourceVersion, 7) + } + const intent = (await pool.query("SELECT state,verified_sealed,recovery_operation_id,authority_profile_id,authority_profile_version,reservation_id FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1 AND source_step_index=0", [source.id])).rows + assert.equal(intent.length, 1); assert.equal(intent[0].state, chargeCase === "unknown" ? "unknown" : "verified"); assert.equal(intent[0].recovery_operation_id, inverse.id) + assert.equal(intent[0].authority_profile_id, null); assert.equal(intent[0].authority_profile_version, persistent ? 1 : null); assert.equal(intent[0].reservation_id, null) + if (chargeCase !== "unknown") { + if (persistent) { + for (const sql of ["UPDATE mcp_routing_manual_inverse_charges SET charge_units=1 WHERE source_operation_id=$1", + "DELETE FROM mcp_routing_manual_inverse_charges WHERE source_operation_id=$1"]) { + await assert.rejects(pool.query(sql, [source.id]), (error:unknown) => (error as {code?:string}).code === "P0001") + } + await assert.rejects(pool.query("TRUNCATE mcp_routing_manual_inverse_charges"), (error:unknown) => (error as {code?:string}).code === "P0001") + } + const verified = JSON.parse(await decryptSecret(intent[0].verified_sealed)) + assert.equal(verified.phase, "RECOVERY_VERIFIED"); assert.equal(verified.originalRule, initial) + assert.equal(filters[0].rule, initial); assert.equal(filters[1].rule, initial) + assert.equal((await reconcile(inverse.id)).matchesDesiredState, true) + } + assert.equal((await applyPlan(principal, inverse.plan_id, randomUUID())).id, inverse.id) + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: source.id })) + assert.deepEqual(await snapshot(), afterInverse, "Known terminal same-plan replay/new recovery plan cannot debit or dispatch again") + const erasedChargeFamilies: string[] = [] + if (process.env.MMAPP_OWN_ROUTING_CHARGE_ERASURE_ONLY === "true" && chargeCase === "positive" && persistent) { + const { assertOwnedRoutingReservationSql } = await import("./routing-reservation.sql-helper.js") + assertOwnedRoutingReservationSql() + const anchors = async () => (await pool.query(`SELECT id, xmin::text, payload FROM mcp_audit WHERE connection_id=$1 + AND action='routing.charge.admitted' AND payload->'anchor'->>'profileId'=$2 ORDER BY id`, [connectionId, id])).rows + const beforeAnchors = await anchors() + assert.deepEqual(beforeAnchors.map(row => row.payload.anchor.kind), ["forward", "manual-inverse"]) + for (const [family, table] of [["forward", "mcp_routing_forward_charges"], ["manual-inverse", "mcp_routing_manual_inverse_charges"]] as const) { + const db = await pool.connect() + let discard = false + try { + await db.query("BEGIN") + await db.query("SET LOCAL lock_timeout='5s'") + await db.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-runner-admission',0))") + // Deliberately bypass the triggers on this owned test database, then roll back every change. + await db.query(`ALTER TABLE ${table} DISABLE TRIGGER USER`) + assert.equal((await db.query(`DELETE FROM ${table} WHERE source_operation_id=$1 AND profile_id=$2`, [source.id, id])).rowCount, 1) + if (family === "manual-inverse") { + await db.query("ALTER TABLE mcp_scoped_recovery_intents DISABLE TRIGGER USER") + assert.equal((await db.query("DELETE FROM mcp_scoped_recovery_intents WHERE mode='manual' AND source_step_index=0 AND source_operation_id=$1 AND recovery_operation_id=$2", [source.id, inverse.id])).rowCount, 1) + } + const current = await effectiveMcpConnection(principal, db) + await assert.rejects(readHistoricalChargesWithinOwner(db, id, current, undefined, Date.now() + 10000), + { code: "STATE_CONFLICT", message: "Учёт переключений не совпал с журналом допуска" }) + assert.equal((await db.query(`DELETE FROM mcp_audit WHERE connection_id=$1 AND action='routing.charge.admitted' + AND payload->'anchor'->>'allocationId'=$2`, [connectionId, family === "forward" ? source.id : inverse.id])).rowCount, 1) + await assert.rejects(readHistoricalChargesWithinOwner(db, id, current, undefined, Date.now() + 10000), + { code: "STATE_CONFLICT", message: "Учёт переключений не совпал с журналом допуска" }) + erasedChargeFamilies.push(family) + } finally { + try { await db.query("ROLLBACK") } catch (error) { discard = true; throw error } finally { db.release(discard) } + } + } + assert.deepEqual(await anchors(), beforeAnchors) + assert.deepEqual(await snapshot(), afterInverse) + } + assert.deepEqual((await pool.query("SELECT * FROM filter_rules WHERE server_id=$1 ORDER BY id", [sid])).rows, dbBefore) + return { accepted: true, manualInverseChargeFix: true, chargeCase, before, afterForward, afterInverse, + genuineConfigApplied: true, originalPersistentAuthorityReloaded: true, genuineRecoveryVerified: chargeCase !== "unknown", humanApprovalRequired: true, + profileInverseChargeObserved: persistent ? 1 : 0, inverseGenericDebitObserved: 1, terminalReplayUnchanged: true, + ...(erasedChargeFamilies.length ? { erasedChargeFamilies, independentHostWitnessDetectedBoth: true, auditAnchorsUnchanged: true } : {}), + realSql: true, routerOsTransport: "loopback-http-fixture", forwardingVerified: false, allWriterLedgerComplete: false } + } + // Genuine fixed-reader baseline A plus signed persistent authority remain + // valid, while the external native body changes to B BEFORE preparation. + // Public ingress cannot set internal profile context; this trusted-host + // preparation case proves the native gate independently binds enrollment. + { + const driftId=randomUUID(),driftCurrent=await effectiveMcpConnection(principal),driftExpiry=new Date(Math.min(Date.now()+600000,driftCurrent.owner_valid_until.getTime(),Date.parse(principal.credentialExpiresAt),Date.parse(driftCurrent.policy.expiresAt))) + await withApplicationMutationLock(()=>withConfigMutationLock([sid],async()=>{ + const baseline=await prepareRoutingControllerBaseline(driftId,1,profile("a"),driftExpiry.toISOString(),{principal,connection:driftCurrent}) + assert.equal((await saveControllerProfileCas(pool,{profileId:driftId,connectionId,expectedVersion:null,profile:profile("a"),enabled:true,expiresAt:driftExpiry.toISOString(),credentialId:principal.credentialId,generation:await accessGeneration(),policyVersion:driftCurrent.policy.version,baseline}))?.version,1) + })) + persistentProfileIds.push(driftId) + try { + filters[0].rule=compileManagedFilterRules([{community:"65000:10",gateway:"192.0.2.9",outIface:"",action:"route",description:""},{community:"65000:20",gateway:"192.0.2.5",outIface:"",action:"route",description:""}]) + assert.equal((await invokeMcpTool(principal,"mm_route_path_verify",{profile:profile("a"),candidateId:"a"}) as {status:string}).status,"passed") + const driftPlan=await invokeMcpTool(principal,"mm_route_optimizer_plan",{profile:profile("a"),candidateId:"a"},undefined,{persistentRoutingProfile:{id:driftId,version:1}}) as {planId:string} + await approvePlan(driftPlan.planId,writer,true) + const beforeDrift=writes,operation=await execute(driftPlan.planId) + assert.notEqual(operation.state,"applied") + assert.equal(writes,beforeDrift,"Authenticated private scoped input prepared after external A->B drift must send0nativewrites") + assert.equal((await pool.query("SELECT configuration_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1",[operation.id])).rows[0].configuration_sealed,null) + console.log("TARGETED_SCOPED_PRIVATE_DRIFT_NATIVE0_PASS") + }finally{filters[0].rule=initial;pendingPostCheckpoint=false;pendingInversePost=false;await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1",[driftId])} + } + if (options.forwardRoundsOnly) { + const accepted: string[] = [] + for (const scenario of ["two-rounds", "native-drift", "checkpoint-tamper", "late-revoke", "row-budget"] as const) { + reset(); await refresh() + const persistent = await persistentFixture(), { plan, input } = await prepare("a", persistent) + const beforeWrites = writes, wireStart = originalEffectWire.length + let faultReached = false + try { + if (scenario === "row-budget") forwardRoundBulk = 1000 + const operation = await executeWithCheckpointObservation(plan.planId, async operationId => { + const reads = requests, connection = await effectiveMcpConnection(principal) + await assert.rejects(resolveRoutingNetworkConfigurationContext(operationId, { + principal: { ...principal, connectionId: randomUUID() }, connection, + })) + assert.equal(requests, reads, "A mismatched principal cannot read native state") + if (scenario !== "two-rounds") afterRoundLogRead = async () => { + faultReached = true + if (scenario === "native-drift") filters[0].rule = "reject;" + if (scenario === "checkpoint-tamper") await pool.query("UPDATE mcp_operation_checkpoints SET configuration_proof=$2 WHERE operation_id=$1", [operationId, "0".repeat(64)]) + if (scenario === "late-revoke") await disablePersistent(persistent.id) + } + }) + assert.equal(faultReached, scenario !== "two-rounds") + const wire = originalEffectWire.slice(wireStart) + const originalRib = wire.filter(row => row.method === "GET" && row.path === "/routing/route" && !row.afterPatch) + const forwardRib = wire.filter(row => row.method === "GET" && row.path === "/routing/route" && row.afterPatch) + assert.equal(originalRib.length, 1); assert.equal(writes, beforeWrites + 1) + const { rows: [terminal] } = await pool.query("SELECT o.error_code,s.result FROM mcp_operations o JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 WHERE o.id=$1", [operation.id]) + if (scenario === "two-rounds" || scenario === "row-budget") { + assert.equal(operation.state, "applied"); assert.equal(forwardRib.length, 2) + const report = originalEffectReportSchema.parse(terminal.result.originalEffects) + const network = await loadScopedNetworkCheckpoint(input, await assertScopedCommunityRequest(input)) + assert(network); assert.equal(network.status, "passed"); assert.deepEqual(network.originalEffects, report) + assert.equal(report.semanticProof, false); assert.equal(report.forwardingVerified, false); assert.equal(report.logCoverage, "unknown") + if (scenario === "two-rounds") { + assert.equal(report.status, "observed-expected"); assert.equal(report.rounds.length, 2) + assert(report.budget && report.budget.calls > 0 && report.budget.calls <= 256 && report.budget.rows <= 10000) + } else { + assert.equal(report.status, "unknown"); assert.equal(report.rounds.length, 0) + assert.equal(report.reason, "original-effect-rounds-unavailable:LIMIT_EXCEEDED") + assert.equal(report.budget, undefined) + } + } else { + assert.equal(operation.state, "unknown"); assert.equal(forwardRib.length, 1) + assert.equal(terminal.error_code, "MUTATION_OUTCOME_UNKNOWN") + } + const readsBeforeReplay = requests + const replay = await applyPlan(principal, plan.planId, randomUUID()) + assert.equal(replay.id, operation.id); assert.equal(requests, readsBeforeReplay); assert.equal(writes, beforeWrites + 1) + accepted.push(scenario) + console.log(JSON.stringify({ scenario, state: operation.state, errorCode: terminal.error_code, forwardRibReads: forwardRib.length, nativeWrites: 1 })) + } finally { + afterRoundLogRead = undefined; forwardRoundBulk = 0 + await disablePersistent(persistent.id) + } + } + console.log(JSON.stringify({ forwardRoundCases: accepted, actualGenuineConfigApplied: true, sharedBudget: true, + securityProof: false, logCoverage: "unknown", forwardingVerified: false })) + return { accepted: true, forwardRoundCases: accepted, securityProof: false, forwardingVerified: false } + } + for (const key of ["a", "b"]) { + const before = structuredClone(filters), { plan, input } = await prepare(key), count = writes + const op = await execute(plan.planId); assert.equal(op.state, "applied"); assert.equal(writes, count + 1) + assert.equal((await reconcile(op.id)).configurationCheckpointAvailable, true) + const privateCheckpoint = (await pool.query("SELECT configuration_sealed,configuration_proof FROM mcp_operation_checkpoints WHERE operation_id=$1", [op.id])).rows[0] + assert.ok(!JSON.stringify(privateCheckpoint).includes(initial), "Native bodies are encrypted at rest") + for (const tamper of ["proof", "ciphertext"]) { + await pool.query("UPDATE mcp_operation_checkpoints SET configuration_sealed=$2,configuration_proof=$3 WHERE operation_id=$1", [op.id, tamper === "ciphertext" ? privateCheckpoint.configuration_sealed.slice(0, -8) + "AAAAAAAA" : privateCheckpoint.configuration_sealed, tamper === "proof" ? "0".repeat(64) : privateCheckpoint.configuration_proof]) + const checked = await reconcile(op.id); assert.equal(checked.configurationCheckpointAvailable, false); assert.equal(checked.matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_checkpoints SET configuration_sealed=$2,configuration_proof=$3 WHERE operation_id=$1", [op.id, privateCheckpoint.configuration_sealed, privateCheckpoint.configuration_proof]) + } + const networkReceipt = (await pool.query("SELECT id,payload FROM mcp_audit WHERE action='routing.scoped.network-check' AND payload->>'receiptId'=$1", [input.receiptId])).rows[0] + assert.ok(networkReceipt) + await pool.query("UPDATE mcp_audit SET payload=jsonb_set(payload,'{check,status}','\"failed\"'::jsonb) WHERE id=$1", [networkReceipt.id]) + assert.equal((await reconcile(op.id)).matchesDesiredState, false, "A generic VERIFIED result cannot replace the signed post-check evidence") + await pool.query("UPDATE mcp_audit SET payload=$2::jsonb WHERE id=$1", [networkReceipt.id, JSON.stringify(networkReceipt.payload)]) + assert.equal((await reconcile(op.id)).matchesDesiredState, true) + const index = key === "a" ? 0 : 1; assert.deepEqual({ ...filters[index], rule: before[index].rule }, before[index]); assert.deepEqual(filters[1 - index], before[1 - index]) + assert.equal((await reconcile(op.id)).matchesDesiredState, true) + const checkpoint = (await pool.query("SELECT intended_state,result FROM mcp_operation_steps WHERE operation_id=$1", [op.id])).rows[0] + await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1", [op.id]); assert.equal((await reconcile(op.id)).matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_steps SET state='verified',intended_state=jsonb_set(intended_state,'{input,receiptId}',to_jsonb($2::text)) WHERE operation_id=$1", [op.id, randomUUID()]); assert.equal((await reconcile(op.id)).matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1", [op.id, JSON.stringify(checkpoint.intended_state)]) + assert.equal((await reconcile(op.id)).matchesDesiredState, true) + assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, op.id, "already applied plan returns its operation without replay") + await assert.rejects(findMcpService("routing.community.scoped")!.current({ ...input, receiptId: randomUUID() })); assert.equal(writes, count + 1) + } + assert.deepEqual([...observedVrfs].sort(), ["probe-a", "probe-b"]) + + for (const scenario of ["ignored", "lost", "collateral"] as const) { + reset(); const { plan } = await prepare(); mode = scenario; const count = writes + const op = await execute(plan.planId); assert.ok(["unknown", "partial", "failed"].includes(op.state), op.state); assert.equal(writes, count + 1) + assert.equal((await pool.query("SELECT configuration_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1", [op.id])).rows[0].configuration_sealed, null, "Lost/ignored/collateral forward must not produce CONFIG_APPLIED") + const beforeRecoveryRequests = requests + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: op.id })) + assert.equal(requests, beforeRecoveryRequests, "Unknown or unverified source is rejected before native recovery reads") + if (scenario !== "collateral") { const checked = await reconcile(op.id); assert.equal(checked.matchesDesiredState, false); if (scenario === "lost") assert.equal(checked.configurationMatchesDesired, true) } + assert.equal(writes, count + 1) + } + + for (const scenario of ["post-failed", "post-unknown"] as const) for (const slaChanged of ["none", "stale", "failed"]) { + reset(); await refresh(); const { plan } = await prepare(), count = writes + mode = scenario + if (slaChanged !== "none") afterForwardWrite = async () => { + await pool.query(slaChanged === "stale" ? "UPDATE mcp_routing_observations SET ping_at='2000-01-01' WHERE src_server_id=$1" : "UPDATE mcp_routing_observations SET loss_pct=100 WHERE src_server_id=$1", [sid]) + for (const row of (await pool.query("SELECT id FROM mcp_routing_observations WHERE src_server_id=$1", [sid])).rows) await attestRoutingFixture(row.id) + } + const op = await execute(plan.planId) + assert.equal(op.state, scenario === "post-failed" ? "partial" : "unknown", scenario) + assert.equal(writes, count + 1) + assert.equal(afterForwardWrite, undefined) + const checked = await reconcile(op.id) + assert.equal(checked.configurationMatchesDesired, true); assert.equal(checked.configurationCheckpointAvailable, true); assert.equal(checked.matchesDesiredState, false) + assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, op.id) + assert.equal(writes, count + 1, "Neither failed nor unknown post-check authorizes replay or inverse") + if (scenario === "post-failed") { + const result = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [op.id])).rows[0].result + assert.equal(result.postProbeStatus, "failed"); assert.equal(result.configurationVerified, true); assert.equal(result.controlProbeVerified, false) + } else { + const beforeRecoveryRequests = requests + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: op.id })) + assert.equal(requests, beforeRecoveryRequests, "Unknown network outcome does not authorize inverse") + } + } + reset(); await refresh() + { + const { plan } = await prepare(), count = writes + afterForwardWrite = async () => { + await pool.query("UPDATE mcp_routing_observations SET ping_at='2000-01-01' WHERE src_server_id=$1", [sid]) + for (const row of (await pool.query("SELECT id FROM mcp_routing_observations WHERE src_server_id=$1", [sid])).rows) await attestRoutingFixture(row.id) + } + const op = await execute(plan.planId); assert.equal(op.state, "applied"); assert.equal(writes, count + 1) + const checked = await reconcile(op.id) + assert.equal(checked.configurationMatchesDesired, true); assert.equal(checked.configurationCheckpointAvailable, true); assert.equal(checked.matchesDesiredState, true) + assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, op.id); assert.equal(writes, count + 1) + } + await refresh() + console.log("Scoped post-write SLA: fresh admission remains mandatory; signed SQL samples becoming stale after PATCH preserve CONFIG_APPLIED and failed/unknown probe receipts, read-only reconciliation and no replay") + for (const change of ["protection", "epoch", "pause", "path-proof"]) { + reset(); await refresh() + const {plan,input}=await prepare(), before=writes, protection=randomUUID() + afterForwardWrite=async()=>{ + if(change==="protection")await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"routing_filter",id:"*fa",serverId:sid,protection:"deny"}),writer.id]) + if(change==="epoch")await pool.query("UPDATE servers SET password='post-write-source-change' WHERE id=$1",[sid]) + if(change==="pause")await setConnectionStatus(connectionId!,"paused",writer) + if(change==="path-proof")await pool.query("UPDATE mcp_path_checks SET evidence=jsonb_set(evidence,'{untrustedChange}','true'::jsonb) WHERE id=$1",[input.pathCheckId]) + } + const op=await execute(plan.planId);assert.notEqual(op.state,"applied",change);assert.equal(writes,before+1) + assert.equal((await pool.query("SELECT configuration_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1",[op.id])).rows[0].configuration_sealed,null,"Post-write authority or immutable source drift cannot produce CONFIG_APPLIED") + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + if(change==="epoch")await pool.query("UPDATE servers SET password='NEVER_SCOPED_PASSWORD' WHERE id=$1",[sid]) + if(change==="pause")await setConnectionStatus(connectionId!,"active",writer) + } + console.log("Scoped post-write authority: protection, endpoint epoch, policy pause and immutable path-row tampering still stop configuration confirmation; exact one PATCH and no replay") + assert.ok(postCheckpointsObserved >= 4) + console.log("Scoped CONFIG_APPLIED SQL/HTTP: encrypted exact operation checkpoint precedes post-probe, native body/candidate/proofs bound, tamper blocked, failed control probe partial, unknown probe unknown, no inverse or replay") + for (const scenario of ["normal", "concurrent", "lost", "ignored", "collateral", "late-protection", "late-epoch", "primary-down"] as const) { + console.log("Scoped inverse scenario:", scenario) + reset(); await refresh() + if (scenario === "primary-down") await registerManagementChannel({ serverId: sid, host: "127.0.0.2", port: (alternate.address() as { port: number }).port, useSsl: false, verifySsl: true, username: "admin", password: "NEVER_SCOPED_ALTERNATE", attestation: "Owned loopback fixture: independent address with the same fixed license identity", expiresAt: new Date(Date.now() + 900000).toISOString() }, writer) + const oldPath = await invokeMcpTool(principal, "mm_route_path_verify", { profile: profile("a"), candidateId: "a" }) as { status: string; id: string } + assert.equal(oldPath.status, "passed") + if (scenario === "normal") { + const savedTime = (await pool.query("SELECT created_at::text AS value FROM mcp_path_checks WHERE id=$1", [oldPath.id])).rows[0].value + for (const shift of ["1 minute", "-91 seconds"]) { + await pool.query("UPDATE mcp_path_checks SET created_at=clock_timestamp()+$2::interval WHERE id=$1", [oldPath.id, shift]) + await assert.rejects(requireRecentPath(profile("a"), "a", connectionId!)) + } + await pool.query("UPDATE mcp_path_checks SET created_at=$2::timestamptz WHERE id=$1", [oldPath.id, savedTime]) + assert.equal(await requireRecentPath(profile("a"), "a", connectionId!), oldPath.id) + } + const { plan: forward } = await prepare() + mode = "post-failed" + const original = await execute(forward.planId) + assert.equal(original.state, "partial") + mode = "normal" + await assert.rejects(requireRecentPath(profile("a"), "a", connectionId!), "The pre-switch A proof must not authorize inverse in B context") + const beforeRecovery: number = writes, primaryBefore: number = primaryControlRequests + if (scenario === "primary-down") primaryDown = true + if (scenario === "normal") { + const saved = (await pool.query("SELECT configuration_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1", [original.id])).rows[0] + await pool.query("UPDATE mcp_operation_checkpoints SET configuration_sealed='invalid-checkpoint' WHERE operation_id=$1", [original.id]) + const count: number = requests + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: original.id })) + assert.equal(requests, count, "A tampered source packet is rejected before native reads") + await pool.query("UPDATE mcp_operation_checkpoints SET configuration_sealed=$2 WHERE operation_id=$1", [original.id, saved.configuration_sealed]) + } + const recovery = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: original.id }) as { planId: string; state: string } + const competitor = scenario === "concurrent" ? await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: original.id }) as { planId: string } : undefined + assert.equal(recovery.state, "awaiting-approval") + const projected = await publicPlan(await loadPlan(recovery.planId)) + assert.ok(!JSON.stringify(projected).includes("NEVER_")); assert.ok(!JSON.stringify(projected).includes("managementBindingCiphertext")); assert.ok(!JSON.stringify(projected).includes("requestProof")) + const beforeApproval: number = writes + await assert.rejects(applyPlan(principal, recovery.planId, randomUUID())); assert.equal(writes, beforeApproval) + await approvePlan(recovery.planId, writer, true) + if (competitor) await approvePlan(competitor.planId, writer, true) + const protection = randomUUID() + if (scenario === "lost" || scenario === "ignored" || scenario === "collateral") mode = scenario + if (scenario === "late-protection" || scenario === "late-epoch") afterIntent = async () => { + if (scenario === "late-protection") await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "routing_filter", id: "*fa", serverId: sid, protection: "deny" }), writer.id]) + else { await pool.query("UPDATE servers SET password='inverse-temp' WHERE id=$1", [sid]); await pool.query("UPDATE servers SET password='NEVER_SCOPED_PASSWORD' WHERE id=$1", [sid]) } + } + const competing = await Promise.all([execute(recovery.planId).then(result => ({ ...result, planId: recovery.planId })), ...(competitor ? [execute(competitor.planId).then(result => ({ ...result, planId: competitor.planId }))] : [])]) + if (competitor) assert.equal(competing.filter(result => result.state === "applied").length, 1, "Only one separately approved plan can acquire the source inverse slot") + const inverse = competing.find(result => result.state === "applied") ?? competing[0] + if (writes !== beforeRecovery + (["late-protection", "late-epoch"].includes(scenario) ? 0 : 1)) { + console.error("Scoped inverse refusal:", scenario, + (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [inverse.id])).rows, + (await pool.query("SELECT action,payload->>'phase' AS phase,payload->>'code' AS code FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.recovery-unconfirmed' ORDER BY created_at DESC LIMIT 3", [connectionId])).rows) + } + assert.equal(writes, beforeRecovery + (["late-protection", "late-epoch"].includes(scenario) ? 0 : 1), scenario) + if ((inverse.state === "applied") !== ["normal", "concurrent", "primary-down"].includes(scenario)) { + console.error("Scoped inverse terminal refusal:", scenario, + (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [inverse.id])).rows, + (await pool.query("SELECT action,payload->>'phase' AS phase,payload->>'code' AS code FROM mcp_audit WHERE connection_id=$1 AND action='routing.scoped.recovery-unconfirmed' ORDER BY created_at DESC LIMIT 3", [connectionId])).rows) + } + assert.equal(inverse.state === "applied", ["normal", "concurrent", "primary-down"].includes(scenario), scenario) + assert.equal((await applyPlan(principal, inverse.planId, randomUUID())).id, inverse.id) + const count: number = writes + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: original.id }), "A new inverse plan must be rejected after scenario " + scenario) + assert.equal(writes, count, "A new plan cannot acquire the same source inverse slot again") + const intent = (await pool.query("SELECT state,verified_sealed,intent_sealed,recovery_operation_id FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [original.id])).rows + assert.equal(intent.length, 1); assert.equal(intent[0].recovery_operation_id, inverse.id) + if (["normal", "concurrent", "primary-down"].includes(scenario)) { + assert.equal(intent[0].state, "verified"); assert.ok(intent[0].verified_sealed) + const packet = JSON.parse(await decryptSecret(intent[0].verified_sealed)) + assert.equal(packet.phase, "RECOVERY_VERIFIED"); assert.equal(packet.originalRule, initial) + assert.equal(filters[0].rule, initial); assert.equal(filters[1].rule, initial) + assert.equal((await reconcile(inverse.id)).matchesDesiredState, true) + await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1", [inverse.id]) + assert.equal((await reconcile(inverse.id)).matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_steps SET state='verified' WHERE operation_id=$1", [inverse.id]) + } else { + assert.notEqual(intent[0].state, "verified"); assert.equal(intent[0].verified_sealed, null) + } + if (scenario === "primary-down") assert.equal(primaryControlRequests, primaryBefore, "Sealed alternate recovery must not contact the lost primary") + primaryDown = false; mode = "normal"; pendingInversePost = false + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + if (scenario === "primary-down") await pool.query("DELETE FROM mcp_management_channels WHERE server_id=$1", [sid]) + } + for (const scenario of ["original-failed", "original-unknown", "deadline", "future-source-time"] as const) { + reset(); await refresh(); const { plan } = await prepare() + mode = "post-failed"; const source = await execute(plan.planId); assert.equal(source.state, "partial") + mode = scenario === "original-unknown" ? "post-unknown" : scenario === "original-failed" ? "post-failed" : "normal" + if (scenario === "deadline") await pool.query("UPDATE mcp_operations SET created_at=now()-interval '3 minutes' WHERE id=$1", [source.id]) + if (scenario === "future-source-time") await pool.query("UPDATE mcp_operations SET created_at=now()+interval '1 minute' WHERE id=$1", [source.id]) + const count: number = writes + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: source.id })) + assert.equal(writes, count) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [source.id])).rows[0].n, 0, "Failed/unknown original path or expired authority cannot claim inverse") + } + assert.ok(inversePostChecks >= 2) + console.log("Scoped inverse SQL/HTTP: exact encrypted source/op/step, fresh A proof in B context, one durable inverse slot, exact original bytes, RECOVERY_VERIFIED, lost/ignored/collateral no replay, late protection/ABA fences, primary-down alternate and generic VERIFIED separation") + for (const scenario of ["stale", "late-epoch-aba", "late-protection", "late-pause"] as const) { + reset(); await refresh(); const { plan } = await prepare(), before = writes, protection = randomUUID() + afterIntent = async () => { + if (scenario === "stale") await pool.query("UPDATE mcp_routing_observations SET ping_at='2000-01-01' WHERE src_server_id=$1", [sid]) + if (scenario === "late-epoch-aba") { await pool.query("UPDATE servers SET password='temporary' WHERE id=$1", [sid]); await pool.query("UPDATE servers SET password='NEVER_SCOPED_PASSWORD' WHERE id=$1", [sid]) } + if (scenario === "late-protection") await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "routing_filter", id: "*fa", serverId: sid, protection: "deny" }), writer.id]) + if (scenario === "late-pause") await setConnectionStatus(connectionId!, "paused", writer) + } + const op = await execute(plan.planId); assert.notEqual(op.state, "applied", scenario); assert.equal(writes, before, scenario + " must be fenced AFTER durable intent BEFORE native PATCH"); assert.equal(afterIntent, undefined) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]); if (scenario === "late-pause") await setConnectionStatus(connectionId!, "active", writer); await refresh() + } + { + reset(); await refresh() + const reservedProfile = routeAiProfileSchema.parse({ ...profile("a"), autoRecovery: true }) + const persistent = await persistentFixture(reservedProfile), beforeWrites = writes + try { + assert.equal((await invokeMcpTool(principal, "mm_route_path_verify", { profile: reservedProfile, candidateId: "b" }) as { status: string }).status, "passed") + const beforeSlots = Number((await pool.query("SELECT count(*) AS n FROM mcp_scoped_recovery_intents r JOIN mcp_operations o ON o.id=r.source_operation_id WHERE o.connection_id=$1", [connectionId])).rows[0].n) + await assert.rejects(prepareScopedCommunitySwitch({ profile: reservedProfile, candidateId: "b" }, { + principal, connection: await effectiveMcpConnection(principal), persistentRoutingProfile: persistent, + }), (error: unknown) => error instanceof Error && error.message.includes("ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE")) + assert.equal(writes, beforeWrites, "Reserved preparation guard must refuse before either forward or inverse PATCH") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_scoped_recovery_intents r JOIN mcp_operations o ON o.id=r.source_operation_id WHERE o.connection_id=$1", [connectionId])).rows[0].n), beforeSlots, "Preparation does not allocate the shared inverse slot") + persistentCases.push("reserved-context-guard-zero-patch") + } finally { await disablePersistent(persistent.id) } + } + for (const change of ["edited", "disabled", "version-aba", "expired"] as const) { + reset(); await refresh(); const persistent = await persistentFixture() + assert.equal((await invokeMcpTool(principal, "mm_route_path_verify", { profile: profile("a"), candidateId: "b" }) as { status: string }).status, "passed") + if (change === "edited") await pool.query("UPDATE mcp_routing_profiles SET config=jsonb_set(config,'{name}','\"changed before preparation\"') WHERE id=$1", [persistent.id]) + if (change === "disabled") await disablePersistent(persistent.id) + if (change === "version-aba") await pool.query("UPDATE mcp_routing_profiles SET version=version+2 WHERE id=$1", [persistent.id]) + if (change === "expired") await pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=now()-interval '1 second' WHERE id=$1", [persistent.id]) + const beforeReads = requests, beforeWrites = writes + await assert.rejects(invokeMcpTool(principal, "mm_route_optimizer_plan", { profile: profile("a"), candidateId: "b" }, undefined, { persistentRoutingProfile: persistent })) + assert.equal(requests, beforeReads, `${change}: stale authority must fail before first native GET`) + assert.equal(writes, beforeWrites) + await disablePersistent(persistent.id); persistentCases.push("before-native:" + change) + } + reset(); await refresh() + { + const persistent = await persistentFixture(), { plan, input } = await prepare("a", persistent), before = writes + const checkBridge = async (operationId: string) => { + const action = { principal, connection: await effectiveMcpConnection(principal) } + const bridge = await resolveRoutingNetworkConfigurationContext(operationId, action) + const current = await consumeRoutingNetworkConfigurationContext(bridge) + assert.equal(current.operationId, operationId); assert.equal(current.securityProof, false); assert.equal(current.sharedNetworkBudgetVerified, false) + assert.equal(current.configuration.nativeId, input.nativeId); assert.equal(current.configuration.dependencyIdentity, input.dependencyProof) + const afterReads = requests + await assert.rejects(consumeRoutingNetworkConfigurationContext(bridge)) + await assert.rejects(consumeRoutingNetworkConfigurationContext(JSON.parse(JSON.stringify(bridge)))) + await assert.rejects(resolveRoutingNetworkConfigurationContext(operationId, action), /source already issued/) + assert.equal(requests, afterReads); assert.equal(writes, before + 1) + } + // The production forward rounds use the same one-time bridge issuance. + // Exercise the independent bridge at the committed checkpoint first. + const op = await executeWithCheckpointObservation(plan.planId, checkBridge) + assert.equal(op.state, "applied"); assert.equal(writes, before + 1) + const row = (await pool.query("SELECT c.configuration_sealed,s.result FROM mcp_operation_checkpoints c JOIN mcp_operation_steps s USING(operation_id,step_index) WHERE c.operation_id=$1", [op.id])).rows[0] + const packet = JSON.parse(await decryptSecret(row.configuration_sealed)) + assert.equal(packet.revision, 2); assert.equal(row.result.proofRevision, 3) + assert.equal(fingerprint(packet.persistentProfileAuthority), fingerprint(input.persistentProfileAuthority)) + assert.equal(packet.persistentProfileAuthority.packet.credentialId, principal.credentialId) + assert.equal((await reconcile(op.id)).matchesDesiredState, true) + persistentCases.push("genuine-configuration-bridge-one-consume-no-clone-grant") + await disablePersistent(persistent.id); persistentCases.push("persistent-forward-request3-checkpoint2") + } + for (const change of ["closed", "disabled", "checkpoint-tamper"] as const) { + reset(); await refresh() + const persistent = await persistentFixture(), { plan } = await prepare("a", persistent), before = writes + const op = await executeWithCheckpointObservation(plan.planId, async operationId => { + const bridge = await resolveRoutingNetworkConfigurationContext(operationId, { principal, connection: await effectiveMcpConnection(principal) }) + const { rows: [checkpoint] } = await pool.query("SELECT configuration_proof FROM mcp_operation_checkpoints WHERE operation_id=$1", [operationId]) + try { + if (change === "closed") closeRoutingNetworkConfigurationContext(bridge) + if (change === "disabled") await disablePersistent(persistent.id) + if (change === "checkpoint-tamper") await pool.query("UPDATE mcp_operation_checkpoints SET configuration_proof=$2 WHERE operation_id=$1", [operationId, "f".repeat(64)]) + const beforeReads = requests + await assert.rejects(consumeRoutingNetworkConfigurationContext(bridge)) + assert.equal(requests, beforeReads); assert.equal(writes, before + 1) + } finally { + if (change === "disabled") await pool.query("UPDATE mcp_routing_profiles SET enabled=true WHERE id=$1", [persistent.id]) + if (change === "checkpoint-tamper") await pool.query("UPDATE mcp_operation_checkpoints SET configuration_proof=$2 WHERE operation_id=$1", [operationId, checkpoint.configuration_proof]) + closeRoutingNetworkConfigurationContext(bridge) + } + }) + assert.equal(op.state, "applied"); assert.equal(writes, before + 1) + await disablePersistent(persistent.id) + persistentCases.push("genuine-configuration-bridge-denies:" + change) + } + { + reset(); await refresh() + const persistent = await persistentFixture(), { plan } = await prepare("a", persistent), before = writes + // The production effect reader already owns this operation's native + // session. A second reader must fail before any native request. + const op = await executeWithCheckpointObservation(plan.planId, async operationId => { + assert.equal((await pool.query("SELECT plan_id FROM mcp_operations WHERE id=$1", [operationId])).rows[0]?.plan_id, plan.planId) + if(process.env.MMAPP_OWN_ROUTING_WORKER_REPRO==="true") { + await (await import("./workers.js")).tickMcpWorkers() + const profileRow=(await pool.query("SELECT enabled,state FROM mcp_routing_profiles WHERE id=$1",[persistent.id])).rows[0] + assert.equal(profileRow.enabled,true);assert.equal(profileRow.state.pausedReason,undefined) + console.log("Worker exclusion preserved exact running CONFIG_APPLIED profile") + } + const action = { principal, connection: await effectiveMcpConnection(principal) } + // Reuse the approved plan's original deadline: now +90s would extend + // its preparation-time probe horizon and mask the duplicate-owner guard. + const deadline = (await loadPlan(plan.planId)).expires_at.getTime() + const reads = requests + await assert.rejects(createRoutingNetworkNativeSession({ sourceOperationId: operationId, context: action, + authorize: async () => {}, deadline, manifest: { servers: [sid], probes: [] } }), /Source native session уже создавалась/) + assert.equal(requests, reads); assert.equal(writes, before + 1) + }) + assert.equal(op.state, "applied"); assert.equal(writes, before + 1) + const { rows: [step] } = await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [op.id]) + const effects = originalEffectReportSchema.parse(step.result.originalEffects) + assert.equal(effects.status, "observed-expected") + assert.equal(effects.rounds.length, 2) + assert.equal(effects.semanticProof, false) + assert(effects.budget && effects.budget.calls > 0 && effects.budget.calls <= 256) + assert(effects.budget.bytes > 0 && effects.budget.rows > 0) + await disablePersistent(persistent.id) + persistentCases.push("genuine-running-config-bridge-uses-one-shared-native-owner") + } + for (const change of ["edit", "pause", "credential", "grant", "channel-revoke"] as const) { + reset(); await refresh() + if (change === "channel-revoke") await registerManagementChannel({ serverId: sid, host: "127.0.0.2", port: (alternate.address() as { port: number }).port, useSsl: false, verifySsl: true, + username: "admin", password: "NEVER_SCOPED_ALTERNATE", attestation: "Synthetic persistent authority independent channel for late SQL revocation", expiresAt: new Date(Date.now() + 600000).toISOString() }, writer) + const persistent = await persistentFixture(), { plan, input } = await prepare("a", persistent), before = writes + if (change === "channel-revoke") assert.ok(input.managementBindingCiphertext) + try { + afterIntent = async () => { + if (change === "edit") await pool.query("UPDATE mcp_routing_profiles SET version=version+1,config=jsonb_set(config,'{name}','\"late edit\"') WHERE id=$1", [persistent.id]) + if (change === "pause") await pool.query("UPDATE mcp_routing_profiles SET enabled=false,state='{\"pausedReason\":\"late emergency\"}'::jsonb WHERE id=$1", [persistent.id]) + if (change === "credential") await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [principal.credentialId]) + if (change === "grant") await pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=now()-interval '1 second' WHERE id=$1", [persistent.id]) + if (change === "channel-revoke") await pool.query("UPDATE mcp_management_channels SET revoked_at=now() WHERE server_id=$1", [sid]) + } + const op = await execute(plan.planId) + assert.notEqual(op.state, "applied"); assert.equal(afterIntent, undefined) + assert.equal(writes, before, `${change}: authority revoked after durable intent must dispatch zero PATCH`) + assert.equal((await pool.query("SELECT configuration_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1", [op.id])).rows[0].configuration_sealed, null) + persistentCases.push("after-intent:" + change) + } finally { + afterIntent = undefined + if (change === "credential") await pool.query("UPDATE mcp_credentials SET revoked_at=NULL WHERE id=$1", [principal.credentialId]) + if (change === "channel-revoke") await pool.query("DELETE FROM mcp_management_channels WHERE server_id=$1", [sid]) + await disablePersistent(persistent.id) + } + } + await assertOriginalCredentialRevocation() + reset(); await refresh() + reset(); const { plan: drift } = await prepare(), count = writes, oldPassword = "NEVER_SCOPED_PASSWORD", beforeRequests = requests + await pool.query("UPDATE servers SET password='changed' WHERE id=$1", [sid]); await pool.query("UPDATE servers SET password=$2 WHERE id=$1", [sid, oldPassword]) + assert.notEqual((await execute(drift.planId)).state, "applied"); assert.equal(writes, count); assert.equal(requests, beforeRequests) + // Refresh signed observations after the source epoch changed; old signatures cannot authorize the next plan. + const observations = (await pool.query("SELECT id FROM mcp_routing_observations WHERE src_server_id=$1", [sid])).rows + for (const observation of observations) await attestRoutingFixture(observation.id) + const abort = new AbortController(); abort.abort() + await assert.rejects(prepareScopedCommunitySwitch({ profile: profile("a"), candidateId: "b" }, { principal, connection: await effectiveMcpConnection(principal), signal: abort.signal })) + reset(); const { plan: paused } = await prepare(); await setConnectionStatus(connectionId, "paused", writer) + await assert.rejects(applyPlan(principal, paused.planId, randomUUID())); assert.equal(writes, count) + assert.deepEqual((await pool.query("SELECT * FROM filter_rules WHERE server_id=$1 ORDER BY id", [sid])).rows, dbBefore) + assert.ok(!JSON.stringify((await pool.query("SELECT result,intended_state FROM mcp_operation_steps WHERE operation_id IN(SELECT id FROM mcp_operations WHERE connection_id=$1)", [connectionId])).rows).includes("NEVER_")) + console.log("✓ Scoped Route AI SQL+HTTP: two independent non-main tables/custom chains/probe VRFs, single-field one-PATCH, comment/opaque and SQL filters preserved, verified exact receipt/read-only reconcile, ignored/lost/collateral outcomes, endpoint ABA, late-intent stale SLA/epoch ABA/protection fences, pause/cancel and public privacy") + return { accepted: true, persistentAuthorityCases: persistentCases.length, persistentCases, manualRequestRevision: 2, persistentRequestRevision: 3, persistentCheckpointRevision: 2, + realMcpDispatcher: true, realSql: true, routerOsTransport: "loopback-http-fixture", scopedControllerEnabled: false, reservedInversePositiveAccepted: false, externalNetwork: false, forwardingVerified: false } + } catch(error) { + // Awaited assertion retains the original HTTP hook cause and stack. + if(requestFailure)throw requestFailure.error + throw error + } finally { + try { + pool.query = originalQuery + if (persistentProfileIds.length) await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=ANY($1::uuid[])", [persistentProfileIds]) + if (connectionId) await setConnectionStatus(connectionId, "paused", writer) + if (insertedServer) { await pool.query("DELETE FROM mcp_management_channels WHERE server_id=$1", [sid]); await pool.query("DELETE FROM servers WHERE id=$1", [sid]) } + router.closeAllConnections(); alternate.closeAllConnections() + await Promise.all([new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())), new Promise((resolve, reject) => alternate.close(error => error ? reject(error) : resolve()))]) + } finally { + try { + if(workerFixtureLease){ + try { if(workerLocked)workerUnsafe=(await workerFixtureLease.query("SELECT pg_advisory_unlock(hashtextextended('mcp-workers',0)) AS unlocked")).rows[0].unlocked!==true } + catch(error){workerUnsafe=true;throw error} + finally {workerFixtureLease.release(workerUnsafe)} + } + } finally { if(schedulerDrained&&!schedulerWasPaused)resumeSchedulerJobs() } + } + } +} diff --git a/backend/src/modules/mcp/routing-community-scoped-checkpoint.ts b/backend/src/modules/mcp/routing-community-scoped-checkpoint.ts new file mode 100644 index 00000000..bbfda884 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-checkpoint.ts @@ -0,0 +1,118 @@ +import { originalEffectReportSchema, type OriginalEffectReport } from "./routing-original-effects.js" +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { audit, transaction } from "./store.js" +import { decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint, redact } from "./security.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" + +const proof = z.string().regex(/^[a-f0-9]{64}$/) +const body = z.string().max(65536).refine(value => Buffer.byteLength(value, "utf8") <= 65536) +const manualCheckpointSchema = z.object({ + revision: z.literal(1), phase: z.literal("CONFIG_APPLIED"), operationId: z.string().uuid(), stepIndex: z.number().int().nonnegative(), + connectionId: z.string().uuid(), ownerId: z.string(), receiptId: z.string().uuid(), requestSeal: proof, + serverId: z.number().int().positive(), nativeId: z.string().regex(/^\*[a-z0-9]+$/i), + originalCandidateId: z.string(), desiredCandidateId: z.string(), profileProof: proof, + originalRule: body, desiredRule: body, dependencyProof: proof, originalTargetProof: proof, desiredTargetProof: proof, + contextSources: z.array(z.object({ serverId: z.number().int().positive(), proof })).max(1000), observedAt: z.string().datetime(), +}).strict() +const checkpointSchema = z.union([manualCheckpointSchema, manualCheckpointSchema.extend({ revision: z.literal(2), persistentProfileAuthority: routingProfileAuthorityBindingSchema }).strict()]) +export type ScopedConfigurationPacket = z.infer +type Checkpoint = ScopedConfigurationPacket +const networkSchema = z.object({ receiptId: z.string().uuid(), requestSeal: proof, configurationCheckpointProof: proof, + check: z.object({ id: z.string().uuid(), candidateId: z.string(), status: z.enum(["passed", "failed"]), observedAt: z.string().datetime() }).strict(), originalEffects: originalEffectReportSchema.optional(), networkSeal: proof }).strict() +const intended = (input: Record) => JSON.stringify(redact({ kind: "service", service: "routing.community.scoped", input })) +const connection = (input: Record) => z.object({ connectionId: z.string().uuid() }).parse(input.principal).connectionId +const seal = (value: Checkpoint) => privateSecretFingerprint(fingerprint(value), `route-ai-scoped-config-checkpoint-v${value.revision}`) + +/** The record belongs to the exact running operation/step, not just a matching + * device. One atomic commit includes the encrypted proof and its audit event. */ +export async function saveScopedConfigurationCheckpoint(input: Record, requestSeal: string) { + await assertPersistentScopedProfileAuthority(input) + const originalRule = await decryptSecret(String(input.originalRuleCiphertext)), desiredRule = await decryptSecret(String(input.desiredRuleCiphertext)) + return transaction(async db => { + const rows = (await db.query("SELECT o.id,s.step_index FROM mcp_operations o JOIN mcp_operation_steps s ON s.operation_id=o.id WHERE o.connection_id=$1 AND o.state='running' AND s.state='intent' AND s.intended_state=$2::jsonb LIMIT 2 FOR UPDATE OF o,s", [connection(input), intended(input)])).rows + if (rows.length !== 1) throw new McpError("STATE_CONFLICT", "Не подтверждён exact scoped operation intent", 409) + const packet = checkpointSchema.parse({ revision: input.persistentProfileAuthority === undefined ? 1 : 2, phase: "CONFIG_APPLIED", operationId: rows[0].id, stepIndex: rows[0].step_index, + connectionId: connection(input), ownerId: input.ownerId, receiptId: input.receiptId, requestSeal, serverId: input.serverId, nativeId: input.nativeId, + originalCandidateId: input.originalCandidateId, desiredCandidateId: input.candidateId, profileProof: fingerprint(input.profile), + originalRule, desiredRule, dependencyProof: input.dependencyProof, originalTargetProof: input.originalTargetProof, desiredTargetProof: input.desiredTargetProof, + contextSources: input.contextSources, observedAt: new Date().toISOString(), + ...(input.persistentProfileAuthority === undefined ? {} : { persistentProfileAuthority: input.persistentProfileAuthority }) }) + const checkpointProof = await seal(packet) + const ciphertext = await encryptSecret(JSON.stringify(packet)) + await assertPersistentScopedProfileAuthority(input) + const changed = await db.query("UPDATE mcp_operation_checkpoints SET configuration_sealed=$3,configuration_proof=$4 WHERE operation_id=$1 AND step_index=$2 AND configuration_sealed IS NULL AND configuration_proof IS NULL RETURNING operation_id", [packet.operationId, packet.stepIndex, ciphertext, checkpointProof]) + if (changed.rowCount !== 1) throw new McpError("STATE_CONFLICT", "Scoped configuration checkpoint уже существует или недоступен", 409) + await audit(db, packet.ownerId, "routing.scoped.config-applied", packet.connectionId, { operationId: packet.operationId, stepIndex: packet.stepIndex, receiptId: packet.receiptId, requestSeal, checkpointProof, phase: packet.phase }) + return { checkpointProof, observedAt: packet.observedAt } + }) +} + +/** Read-only. A matching configuration is never promoted to a missing record. */ +export async function loadScopedConfigurationPacket(input: Record, requestSeal: string, source?: { operationId: string; stepIndex: number }, db: Pick = pool) { + return readScopedConfigurationPacket(input, requestSeal, source, db, false) +} + +async function readScopedConfigurationPacket(input: Record, requestSeal: string, source: { operationId: string; stepIndex: number } | undefined, db: Pick, lock: boolean) { + const rows = (await db.query(`SELECT o.id,s.step_index,c.configuration_sealed,c.configuration_proof FROM mcp_operations o JOIN mcp_operation_steps s ON s.operation_id=o.id JOIN mcp_operation_checkpoints c ON c.operation_id=o.id AND c.step_index=s.step_index WHERE o.connection_id=$1 AND s.intended_state=$2::jsonb${source ? " AND o.id=$3 AND s.step_index=$4" : ""} LIMIT 2${lock ? " FOR NO KEY UPDATE OF c" : ""}`, [connection(input), intended(input), ...(source ? [source.operationId, source.stepIndex] : [])])).rows + if (rows.length !== 1 || !rows[0].configuration_sealed || !rows[0].configuration_proof) return null + const row = rows[0] + if (Buffer.byteLength(row.configuration_sealed) > 524288) throw new McpError("STATE_UNAVAILABLE", "Scoped checkpoint превышает предел", 409) + try { + const packet = checkpointSchema.parse(JSON.parse(await decryptSecret(row.configuration_sealed))) + const expected = { connectionId: connection(input), ownerId: input.ownerId, receiptId: input.receiptId, requestSeal, serverId: input.serverId, nativeId: input.nativeId, + originalCandidateId: input.originalCandidateId, desiredCandidateId: input.candidateId, profileProof: fingerprint(input.profile), dependencyProof: input.dependencyProof, + originalTargetProof: input.originalTargetProof, desiredTargetProof: input.desiredTargetProof, contextSources: input.contextSources } + if (packet.revision !== (input.persistentProfileAuthority === undefined ? 1 : 2) || + packet.revision === 2 && fingerprint(packet.persistentProfileAuthority) !== fingerprint(input.persistentProfileAuthority)) return null + if (packet.operationId !== row.id || packet.stepIndex !== row.step_index || row.configuration_proof !== await seal(packet) || Object.entries(expected).some(([key, value]) => fingerprint(packet[key as keyof Checkpoint]) !== fingerprint(value))) return null + if (packet.originalRule !== await decryptSecret(String(input.originalRuleCiphertext)) || packet.desiredRule !== await decryptSecret(String(input.desiredRuleCiphertext))) return null + return { checkpointProof: row.configuration_proof as string, observedAt: packet.observedAt, packet } + } catch { return null } +} + +export async function loadScopedConfigurationCheckpoint(input: Record, requestSeal: string, db: Pick = pool) { + const value = await loadScopedConfigurationPacket(input, requestSeal, undefined, db) + return value ? { checkpointProof: value.checkpointProof, observedAt: value.observedAt } : null +} + +export async function saveScopedNetworkCheckpoint(input: Record, requestSeal: string, check: { id: string; candidateId: string; status: "passed" | "failed"; observedAt: string }, expectedConfigurationProof: string, originalEffects?: OriginalEffectReport) { + await assertPersistentScopedProfileAuthority(input) + await transaction(async db => { + await db.query("SET LOCAL lock_timeout='5s'") + const configuration = await readScopedConfigurationPacket(input, requestSeal, undefined, db, true) + if (!configuration || configuration.checkpointProof !== expectedConfigurationProof) throw new McpError("STATE_CONFLICT", "Исходный CONFIG_APPLIED изменился", 409) + const observedAt = Date.parse(check.observedAt) + if (check.candidateId !== input.candidateId || !Number.isFinite(observedAt) || + observedAt < Date.parse(configuration.observedAt) || observedAt > Date.now()) { + throw new McpError("STATE_CONFLICT", "Контрольная проба не относится к подтверждённой конфигурации", 409) + } + const packet = { receiptId: input.receiptId, requestSeal, configurationCheckpointProof: configuration.checkpointProof, + check: { id: check.id, candidateId: check.candidateId, status: check.status, observedAt: check.observedAt }, + ...(originalEffects === undefined ? {} : { originalEffects: originalEffectReportSchema.parse(originalEffects) }) } + const networkSeal = await privateSecretFingerprint(fingerprint(packet), "route-ai-scoped-network-checkpoint-v1") + await assertPersistentScopedProfileAuthority(input, undefined, db) + const receipt = { ...packet, networkSeal } + const rows = await readScopedNetworkReceipts(input, requestSeal, db) + if (rows.length === 1 && fingerprint(rows[0].payload) === fingerprint(redact(receipt))) return + if (rows.length) throw new McpError("STATE_CONFLICT", "Сетевая квитанция уже записана с другим содержимым", 409) + await audit(db, String(input.ownerId), "routing.scoped.network-check", connection(input), receipt) + }) +} + +async function readScopedNetworkReceipts(input: Record, requestSeal: string, db: Pick) { + return (await db.query("SELECT payload FROM mcp_audit WHERE connection_id=$1 AND actor_id=$2 AND action='routing.scoped.network-check' AND payload->>'receiptId'=$3 AND payload->>'requestSeal'=$4 LIMIT 2", [connection(input), input.ownerId, input.receiptId, requestSeal])).rows +} + +export async function loadScopedNetworkCheckpoint(input: Record, requestSeal: string, db: Pick = pool) { + const configuration = await loadScopedConfigurationCheckpoint(input, requestSeal, db) + if (!configuration) return null + const rows = await readScopedNetworkReceipts(input, requestSeal, db) + if (rows.length !== 1) return null + const parsed = networkSchema.safeParse(rows[0].payload) + if (!parsed.success) return null + const { networkSeal, ...packet } = parsed.data + if (packet.configurationCheckpointProof !== configuration.checkpointProof || packet.check.candidateId !== input.candidateId || Date.parse(packet.check.observedAt) < Date.parse(configuration.observedAt) || Date.parse(packet.check.observedAt) > Date.now() || networkSeal !== await privateSecretFingerprint(fingerprint(packet), "route-ai-scoped-network-checkpoint-v1")) return null + return { status: packet.check.status, networkSeal, ...(packet.originalEffects === undefined ? {} : { originalEffects: packet.originalEffects }) } +} diff --git a/backend/src/modules/mcp/routing-community-scoped-model.test.ts b/backend/src/modules/mcp/routing-community-scoped-model.test.ts new file mode 100644 index 00000000..05d4194e --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-model.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { compileScopedCommunitySwitch, scopedConfigurationMatches, scopedTargetRuleMatches } from "./routing-community-scoped-model.js" + +const rules = [{ community: "65000:10", action: "route" as const, gateway: "192.0.2.1", outIface: "", description: "" }, + { community: "65000:20", action: "route" as const, gateway: "192.0.2.1", outIface: "", description: "" }, + { community: "65000:30", action: "blackhole" as const, gateway: "", outIface: "", description: "" }] +test("full generated compiler splits grouped communities while preserving every other branch", () => { + const original = compileManagedFilterRules(rules) + const result = compileScopedCommunitySwitch(original, "65000:10", "192.0.2.9", "", "ipv4") + assert.equal(result.compiledBody, compileManagedFilterRules(rules.map(rule => rule.community === "65000:10" ? { ...rule, gateway: "192.0.2.9" } : rule))) + assert.equal(result.before.gateway, "192.0.2.1"); assert.equal(result.proposed.gateway, "192.0.2.9") + assert.equal(rules[1].gateway, "192.0.2.1") + const inverse = compileScopedCommunitySwitch(result.compiledBody, "65000:10", "192.0.2.1", "", "ipv4") + assert.equal(inverse.compiledBody, original) +}) +test("full roundtrip rejects hidden script, unknown branches, comments, invalid gateways and blackhole targets", () => { + const body = compileManagedFilterRules(rules) + for (const altered of [body + "\naccept;", "# comment\n" + body, body.replace("accept;", "set distance 9; accept;"), body.replace("bgp-communities", "dst"), ""]) assert.throws(() => compileScopedCommunitySwitch(altered, "65000:10", "192.0.2.9", "", "ipv4")) + for (const gateway of ["192.0.2.9@tenant", "192.0.2.9%wan", "fe80::1", "2001:db8::1", "1.2.3.4;accept;"]) assert.throws(() => compileScopedCommunitySwitch(body, "65000:10", gateway, "", "ipv4")) + assert.throws(() => compileScopedCommunitySwitch(body, "65000:30", "192.0.2.9", "", "ipv4")) + assert.throws(() => compileScopedCommunitySwitch(body, "65000:10", "192.0.2.9", 'bad";accept;', "ipv4")) +}) +test("native optional quotes preserve exact managed body and existing proof format", () => { + const quoted = compileManagedFilterRules([{ ...rules[0], outIface: "marker-gre-a" }]) + const bare = quoted.replace('"marker-gre-a"', "marker-gre-a") + const change = (body: string) => compileScopedCommunitySwitch(body, "65000:10", "192.0.2.9", "marker-gre-b", "ipv4") + assert.deepEqual(change(bare), change(quoted)) + assert.match(change(bare).compiledBody, / set gw-interface "marker-gre-b";/) + const proof = (body: string) => JSON.stringify({ id: "*1", body }) + const target = (rule: string) => ({ rawRow: { rule }, desiredTargetProof: proof }) + assert.equal(scopedTargetRuleMatches(target(bare), quoted, proof(quoted)), true) + assert.equal(scopedTargetRuleMatches(target(quoted), bare, proof(bare)), true) + assert.equal(scopedTargetRuleMatches(target(bare), quoted, proof(bare)), false) + assert.equal(scopedTargetRuleMatches(target(bare), quoted, JSON.stringify({ id: "*2", body: quoted })), false) + for (const altered of [bare.replace("marker-gre-a", "marker-gre-x"), bare.replace("192.0.2.1", "192.0.2.2"), + bare.replace(" set gw-interface", " set gw-interface"), bare.replace("gw-interface", "out-interface"), bare + "\naccept;"]) { + assert.equal(scopedTargetRuleMatches(target(altered), quoted, proof(quoted)), false) + } + for (const name of ["marker:gre/a", "100", "marker gre a"]) { + const unusual = compileManagedFilterRules([{ ...rules[0], outIface: name }]) + assert.equal(scopedTargetRuleMatches(target(unusual.replace('"' + name + '"', name)), unusual, proof(unusual)), false) + } +}) +test("scoped configuration comparison needs dependency, native metadata and candidate identity conjunction", () => { + const input = { dependencyProof: "dependencies", originalTargetProof: "original", desiredTargetProof: "desired", candidateId: "b" } + const state = { dependencyProof: "dependencies", targetRuleProof: "desired", currentCandidateId: "b" } + assert.deepEqual(scopedConfigurationMatches(input, state), { configurationMatchesOriginal: false, configurationMatchesDesired: true }) + for (const changed of [{ dependencyProof: "collateral" }, { targetRuleProof: "opaque edited" }, { currentCandidateId: "foreign" }]) assert.equal(scopedConfigurationMatches(input, { ...state, ...changed }).configurationMatchesDesired, false) +}) diff --git a/backend/src/modules/mcp/routing-community-scoped-model.ts b/backend/src/modules/mcp/routing-community-scoped-model.ts new file mode 100644 index 00000000..34d244db --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-model.ts @@ -0,0 +1,33 @@ +import { isIP } from "node:net" +import { compileManagedFilterRules, filterRuleBranches, filterRuleToken } from "../../services/filter-rules-write-model.js" +import { managedRoutingFilterBodiesMatch } from "../../services/routing-filter-body.js" + +type Rule = { community: string; action: "route" | "blackhole"; gateway: string; outIface: string; description: string } +/** Deliberately accepts only the complete compiler output, never partial script parsing. */ +export function compileScopedCommunitySwitch(body: string, community: string, gateway: string, outIface: string, family: "ipv4" | "ipv6") { + if (isIP(gateway) !== (family === "ipv4" ? 4 : 6) || /[@%]/.test(gateway) || /^fe[89ab]/i.test(gateway)) throw new Error("Unsupported gateway") + const rules: Rule[] = [] + for (const branch of filterRuleBranches(body)) { + if (!/^\s*bgp-communities includes \d+:\d+(?:\s*\|\|\s*bgp-communities includes \d+:\d+)*\s*$/.test(branch.condition)) throw new Error("Unknown condition") + const blackhole = /^\s*set blackhole yes;\s*accept;\s*$/.test(branch.body) + for (const match of branch.condition.matchAll(/bgp-communities includes (\d+:\d+)/g)) rules.push({ community: match[1], action: blackhole ? "blackhole" : "route", gateway: blackhole ? "" : filterRuleToken(branch.body, "gw"), outIface: blackhole ? "" : filterRuleToken(branch.body, "interface"), description: "" }) + } + if (!rules.length || !managedRoutingFilterBodiesMatch(body, compileManagedFilterRules(rules)) || new Set(rules.map(rule => rule.community)).size !== rules.length) throw new Error("Full compiler roundtrip required") + const selected = rules.filter(rule => rule.community === community) + if (selected.length !== 1 || selected[0].action !== "route") throw new Error("Unique routed community required") + const before = { ...selected[0] } + const desired = rules.map(rule => rule.community === community ? { ...rule, gateway, outIface } : rule) + const compiledBody = compileManagedFilterRules(desired) + return { compiledBody, before, proposed: desired.find(rule => rule.community === community)! } +} + +export function scopedTargetRuleMatches(target: { rawRow: Record; desiredTargetProof: (body: string) => string }, expectedBody: string, expectedProof: unknown) { + return typeof expectedProof === "string" && managedRoutingFilterBodiesMatch(target.rawRow.rule, expectedBody) && + target.desiredTargetProof(expectedBody) === expectedProof +} + +export function scopedConfigurationMatches(input: { dependencyProof: unknown; originalTargetProof: unknown; desiredTargetProof: unknown; candidateId: unknown }, observed: { dependencyProof: unknown; targetRuleProof: unknown; currentCandidateId: unknown }) { + const dependencies = input.dependencyProof === observed.dependencyProof + return { configurationMatchesOriginal: dependencies && input.originalTargetProof === observed.targetRuleProof, + configurationMatchesDesired: dependencies && input.desiredTargetProof === observed.targetRuleProof && input.candidateId === observed.currentCandidateId } +} diff --git a/backend/src/modules/mcp/routing-community-scoped-native.test.ts b/backend/src/modules/mcp/routing-community-scoped-native.test.ts new file mode 100644 index 00000000..8bad35e4 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-native.test.ts @@ -0,0 +1,26 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { patchScopedCommunity } from "./routing-community-scoped-native.js" + +test("scoped transport fixes one PATCH/id/body, does not redirect/retry, bounds actual bytes and cancels", async () => { + let count = 0, behavior = "ok" + const router = createServer(async (req, res) => { + count++; assert.equal(req.method, "PATCH"); assert.equal(decodeURIComponent(req.url!), "/rest/routing/filter/rule/*abc") + let body = ""; for await (const chunk of req) body += chunk + assert.deepEqual(JSON.parse(body), { rule: "generated" }) + if (behavior === "redirect") { res.writeHead(307, { Location: "/other" }); res.end("{}"); return } + if (behavior === "lost") { req.socket.destroy(); return } + if (behavior === "huge") { res.end("x".repeat(1048577)); return } + res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const endpoint = { id: 1, host: "127.0.0.1", port: (router.address() as { port: number }).port, username: "user", password: "private", useSsl: false, verifySsl: true, enabled: true, routingEndpointEpoch: randomUUID() } + try { + await patchScopedCommunity(endpoint, "*abc", "generated"); assert.equal(count, 1) + for (const mode of ["redirect", "lost", "huge"]) { behavior = mode; await assert.rejects(patchScopedCommunity(endpoint, "*abc", "generated")); assert.equal(count, ["redirect", "lost", "huge"].indexOf(mode) + 2) } + const abort = new AbortController(); abort.abort(); await assert.rejects(patchScopedCommunity(endpoint, "*abc", "generated", abort.signal)); assert.equal(count, 4) + assert.throws(() => patchScopedCommunity(endpoint, "*abc/../other", "generated")); assert.equal(count, 4) + } finally { router.closeAllConnections(); await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) } +}) diff --git a/backend/src/modules/mcp/routing-community-scoped-native.ts b/backend/src/modules/mcp/routing-community-scoped-native.ts new file mode 100644 index 00000000..890e58a6 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-native.ts @@ -0,0 +1,22 @@ +import http from "node:http" +import https from "node:https" +import type { RoutingEndpoint } from "./routing-observation-source.js" +import { McpError } from "./security.js" + +/** Exactly one request, fixed menu/id and one allowed field. Never follows redirects or retries. */ +export function patchScopedCommunity(endpoint: Readonly, nativeId: string, rule: string, signal?: AbortSignal): Promise { + if (!/^\*[A-Za-z0-9]+$/.test(nativeId) || Buffer.byteLength(rule) > 65536) throw new McpError("INVALID_INPUT", "Некорректная цель scoped фильтра", 409) + return new Promise((resolve, reject) => { + const req = (endpoint.useSsl ? https : http).request({ hostname: endpoint.host, port: endpoint.port, path: "/rest/routing/filter/rule/" + encodeURIComponent(nativeId), method: "PATCH", signal, + rejectUnauthorized: endpoint.useSsl ? endpoint.verifySsl : undefined, + headers: { Authorization: "Basic " + Buffer.from(endpoint.username + ":" + endpoint.password).toString("base64"), "Content-Type": "application/json" } }, res => { + let bytes = 0 + res.on("data", (chunk: Buffer) => { if ((bytes += chunk.length) > 1048576) req.destroy(new Error("Response limit")) }) + res.once("error", reject); res.once("aborted", () => reject(new Error("Response interrupted"))) + res.once("end", () => { if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) resolve(); else reject(new Error("Unconfirmed PATCH")) }) + }) + const timer = setTimeout(() => req.destroy(new Error("PATCH deadline")), 10000) + req.once("error", reject); req.once("close", () => clearTimeout(timer)) + req.end(JSON.stringify({ rule })) + }) +} diff --git a/backend/src/modules/mcp/routing-community-scoped-recovery-source.ts b/backend/src/modules/mcp/routing-community-scoped-recovery-source.ts new file mode 100644 index 00000000..76e3a6fb --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-recovery-source.ts @@ -0,0 +1,81 @@ +import { z } from "zod" +import type { PoolClient } from "pg" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { effectiveMcpConnection } from "./store.js" +import { findMcpOperation } from "./registry.js" +import { accessGeneration, decryptSecret, fingerprint, McpError, privateSecretFingerprint, redact } from "./security.js" +import { assertScopedCommunityRequest } from "./routing-community-scoped-request.js" +import { loadScopedConfigurationPacket, loadScopedNetworkCheckpoint } from "./routing-community-scoped-checkpoint.js" +import { assertPersistentScopedProfileAuthority } from "./routing-profile-authority.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { RoutingReservedInverseExecution } from "./routing-reservation-store.js" + +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, 409) +export const scopedRecoveryPrincipalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict() + +/** The operation and encrypted plan are the authority. Never reconstruct a + * native command from a model-supplied receipt, native ID, rule or public JSON. */ +export async function loadScopedRecoverySource(operationId: string, context: ActionContext, db: Pick = pool) { + return loadSource(operationId, context, undefined, db) +} + +/** Only the reservation store can recognize this process-local capability. + * JSON input, checkpoint equality and a running operation alone confer no grant. */ +export async function loadReservedScopedRecoverySource(execution: RoutingReservedInverseExecution, context: ActionContext) { + const { createRoutingReservationStore } = await import("./routing-reservation-store.js") + const binding = await createRoutingReservationStore().inspectInverseExecution(execution) + if (fingerprint(context.principal) !== fingerprint(binding.principal)) throw fail("Reserved inverse principal changed") + const source = await loadSource(binding.sourceOperationId, context, binding) + // A source read never consumes or recreates an execution capability. + await createRoutingReservationStore().inspectInverseExecution(execution) + return source +} + +type ReservedBinding = Awaited["inspectInverseExecution"]>> +async function loadSource(operationId: string, context: ActionContext, reserved?: ReservedBinding, db: Pick = pool) { + z.string().uuid().parse(operationId) + if (context.signal?.aborted) throw fail("Восстановление отменено", "READ_CANCELLED") + const current = await effectiveMcpConnection(context.principal, db) + if (current.id !== context.connection.id || current.owner_id !== context.connection.owner_id || fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw fail("Допуск восстановления изменился", "RESET_REQUIRED") + for (const operation of ["operations.recover", "routing.community.switch", "routing.path.verify"]) if (!current.policy.scope.operationIds.includes(operation)) throw fail("Не выдано право исходного действия, проверки или восстановления", "OPERATION_DENIED") + for (const permission of ["mm:network:write", "mm:network:read", "mm:filters:write"]) if (!hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission)) throw fail("Утрачено право восстановления пути", "PERMISSION_DENIED") + const row = (await db.query("SELECT o.id,o.state,o.connection_id,o.created_at,p.id AS source_plan_id,p.operation_id AS source_action,p.operation_version AS source_version,CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence ELSE NULL END AS evidence FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.connection_id=$2", [operationId, current.id])).rows[0] + if (!row) throw fail("Исходная операция недоступна", "NOT_FOUND") + if (row.source_action !== "routing.community.switch" || row.source_version !== findMcpOperation(row.source_action)?.version) throw fail("Нет совместимого scoped исходного действия", "UNSUPPORTED_CAPABILITY") + if (reserved) { + if (row.state !== "running") throw fail("Исходная операция для зарезервированного восстановления больше не выполняется") + } else if (!["applied", "partial"].includes(row.state)) throw fail("Неопределённая или выполняющаяся запись не допускает inverse") + if (!row.evidence?.sealed) throw fail("Исходное доказательство недоступно") + let evidence: ChangeEvidence + try { evidence = JSON.parse(await decryptSecret(row.evidence.sealed)) } catch { throw fail("Исходное доказательство повреждено") } + if (evidence.steps?.length !== 1 || evidence.steps[0].kind !== "service" || evidence.steps[0].service !== "routing.community.scoped" || evidence.target?.scope !== "servers") throw fail("Требуется единственный exact scoped service", "UNSUPPORTED_CAPABILITY") + const step = evidence.steps[0], input = step.input, profile = routeAiProfileSchema.parse(input.profile) + const ids = [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])].sort((a,b) => a-b) + if (input.serverId !== profile.serverId || input.ownerId !== current.owner_id || ids.some(id => !current.policy.scope.serverIds.includes(id)) || fingerprint([...evidence.target.serverIds].sort((a,b) => a-b)) !== fingerprint(ids)) throw fail("Зависимость восстановления вне допуска", "SERVER_DENIED") + const originalPrincipal = scopedRecoveryPrincipalSchema.parse(input.principal), original = await effectiveMcpConnection(originalPrincipal, db) + if (original.id !== current.id || original.owner_id !== current.owner_id || fingerprint(original.policy) !== input.policyProof || original.generation !== input.connectionGeneration || await accessGeneration() !== input.generation) throw fail("Исходный допуск отозван или изменён", "RESET_REQUIRED") + for (const permission of ["mm:network:read", "mm:filters:write"]) if (!hasPermission(original.owner_permissions, permission) || !hasPermission(original.policy.permissions, permission)) throw fail("Исходное право отозвано", "PERMISSION_DENIED") + const requestSeal = await assertScopedCommunityRequest(input) + if (reserved && (reserved.sourceInputSeal !== requestSeal || reserved.sourceReceiptId !== input.receiptId || fingerprint(reserved.scope) !== fingerprint(ids) || !profile.autoRecovery || input.proofRevision !== 3)) throw fail("Reserved source binding changed") + // Preserve the original forward authority/principal. A newer credential + // approving this manual inverse cannot replace the original profile grant. + await assertPersistentScopedProfileAuthority(input, context.signal, db) + const configuration = await loadScopedConfigurationPacket(input, requestSeal, { operationId, stepIndex: 0 }, db) + const network = await loadScopedNetworkCheckpoint(input, requestSeal, db) + if (!configuration || !network || row.state === "applied" && network.status !== "passed" || row.state === "partial" && network.status !== "failed") throw fail("Нет подтверждённой конфигурации и terminal network receipt") + if (reserved && (network.status !== "failed" || configuration.checkpointProof !== reserved.configurationProof || network.networkSeal !== reserved.networkProof)) throw fail("Нет exact reserved failed network outcome") + if (row.created_at.getTime() > Date.parse(configuration.observedAt) || Date.parse(configuration.observedAt) > Date.now()) throw fail("Время исходной операции несовместимо с подписанным checkpoint") + const generic = (await db.query("SELECT state,intended_state,result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operationId])).rows[0] + const result = generic?.result + const expectedSeal = await privateSecretFingerprint(fingerprint({ request: requestSeal, dependency: input.dependencyProof, desired: input.desiredTargetProof, candidate: input.candidateId, configuration: configuration.checkpointProof, network: network.networkSeal }), `route-ai-scoped-verified-v${input.proofRevision}`) + if (!generic || fingerprint(generic.intended_state) !== fingerprint(redact(step))) throw fail("Исходный generic intent не подтверждён") + if (reserved) { + if (generic.state !== "intent" || generic.result != null) throw fail("Running reserved source is no longer stable") + } else if (generic.state !== (row.state === "applied" ? "verified" : "applied") || result?.nativeVerificationSeal !== expectedSeal || result.configurationCheckpointProof !== configuration.checkpointProof || result.networkCheckpointProof !== network.networkSeal || result.receiptId !== input.receiptId || result.proofRevision !== input.proofRevision || result.nativeWrites !== 1 || result.configurationVerified !== true || result.postProbeStatus !== network.status || result.controlProbeVerified !== (network.status === "passed")) throw fail("Исходный generic checkpoint не подтверждён") + if (!profile.candidates.some(candidate => candidate.id === configuration.packet.originalCandidateId && candidate.controlProbe)) throw fail("Исходный кандидат не имеет контрольной пробы", "MEASUREMENT_UNKNOWN") + const sourceProof = await privateSecretFingerprint(fingerprint({ operationId, state: row.state, createdAt: row.created_at.toISOString(), sourceAction: row.source_action, sourceVersion: row.source_version, inputSeal: requestSeal, configurationProof: configuration.checkpointProof, networkSeal: network.networkSeal, generic, ...(reserved ? { reservationId: reserved.reservationId, inverseReceiptId: reserved.inverseReceiptId } : {}) }), "route-ai-scoped-recovery-source-v1") + await assertPersistentScopedProfileAuthority(input, context.signal, db) + return { row, input, profile, ids, configuration, network, sourceProof, current } +} diff --git a/backend/src/modules/mcp/routing-community-scoped-recovery.ts b/backend/src/modules/mcp/routing-community-scoped-recovery.ts new file mode 100644 index 00000000..1fa13c95 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-recovery.ts @@ -0,0 +1,633 @@ +import { withBackgroundPartitionWrite } from "../../services/partition-write-transaction.js" +import { randomUUID } from "node:crypto" +import { z } from "zod" +import type { PoolClient, QueryConfig } from "pg" +import { pool } from "../../db/index.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { assertVerifiedConfigurationAccess, executeGuardedConfigurationWrite } from "../../services/config-mutation-policy.js" +import { hasPermission } from "../../lib/permissions.js" +import { accessGeneration, decryptSecret, decryptSecretExisting, encryptSecret, fingerprint, McpError, privateSecretFingerprint, privateFingerprinterExisting, redact } from "./security.js" +import { audit, effectiveMcpConnection, transaction } from "./store.js" +import { registerMcpService, type RoutingReservedNetworkSession } from "./service-registry.js" +import { inspectRoutingNetworkSharedContext } from "./routing-network-native-session.js" +import { loadReservedScopedRecoverySource, loadScopedRecoverySource, scopedRecoveryPrincipalSchema } from "./routing-community-scoped-recovery-source.js" +import { createRoutingReservationStore, type RoutingReservedInverseAllocation, type RoutingReservedInverseExecution } from "./routing-reservation-store.js" +import { routingReservationTransaction } from "./routing-reservation-transaction.js" +import type { McpPrincipal } from "./store.js" +import { readRouteCommunityContext, type RouteContextOptions } from "./routing-community-context.js" +import { rankRoutingCandidates } from "./routing-model.js" +import { readRoutingObservations } from "./routing-observations.js" +import { checkCandidatePath, inspectControlPing, inspectTrace, requireRecentPathWithContext } from "./path-verification.js" +import { patchScopedCommunity } from "./routing-community-scoped-native.js" +import { assertManagementTransport, patchScopedCommunityViaManagementTransport, resolveManagementTransport, type ManagementTransportBinding } from "./management-transport.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { createInverseEffectReader, inverseEffectSummarySchema, originalEffectBaselineMatches, summarizeInverseEffects } from "./routing-inverse-effects.js" +import { inspectOriginalEffectRounds, originalEffectSelectionSchema } from "./routing-original-effects.js" + +import { manualInverseReceiptSchema } from "./routing-manual-inverse-store.js" + +const service = "routing.community.scoped.recover" +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, 409) +const proof = z.string().regex(/^[a-f0-9]{64}$/) +type Source = Awaited> +const sealedProof = (value: unknown, kind: string) => privateSecretFingerprint(fingerprint(value), "route-ai-scoped-recovery-" + kind + "-v1") +const reservedExecutions = new WeakMap, { execution: RoutingReservedInverseExecution; network: RoutingReservedNetworkSession }>() +const legacyIntentSchema = z.object({ revision: z.literal(1), phase: z.literal("RECOVERY_INTENT"), mode: z.enum(["manual", "reserved"]), + reservationId: z.string().uuid().optional(), networkProof: proof.optional(), + sourceOperationId: z.string().uuid(), sourceStepIndex: z.literal(0), sourceReceiptId: z.string().uuid(), sourceProof: proof, configurationProof: proof, + operationId: z.string().uuid(), stepIndex: z.number().int().nonnegative(), receiptId: z.string().uuid(), requestSeal: proof, + connectionId: z.string().uuid(), ownerId: z.string(), validUntil: z.string().datetime(), createdAt: z.string().datetime() }).strict() +const currentIntentSchema = legacyIntentSchema.extend({ revision: z.literal(2), mode: z.literal("manual"), + manualInverseCharge: manualInverseReceiptSchema }).omit({ reservationId: true, networkProof: true }).strict() +const intentSchema = z.union([legacyIntentSchema, currentIntentSchema]) +const checkpointFields = { phase: z.enum(["RECOVERY_CONFIG_APPLIED", "RECOVERY_VERIFIED"]), + serverId: z.number().int().positive(), nativeId: z.string(), candidateId: z.string(), originalRule: z.string().max(65536), + targetProof: proof, dependencyProof: proof, sourceEpochs: z.array(z.object({ serverId: z.number().int().positive(), proof })), + pathCheckId: z.string().uuid().nullable(), observedAt: z.string().datetime(), observation: inverseEffectSummarySchema.optional() } +const checkpointSchema = z.union([legacyIntentSchema.omit({ phase: true }).extend(checkpointFields).strict(), currentIntentSchema.omit({ phase: true }).extend(checkpointFields).strict()]) +type Intent = z.infer +type Checkpoint = z.infer +const intended = (input: Record) => JSON.stringify(redact({ kind: "service", service, input })) + +function verifiedCheckpointFollowsConfiguration(config: Checkpoint, verified: Checkpoint) { + const { phase: configPhase, pathCheckId: configPath, observedAt: configAt, observation: configObservation, ...configIdentity } = config + const { phase: verifiedPhase, pathCheckId: verifiedPath, observedAt: verifiedAt, observation: verifiedObservation, ...verifiedIdentity } = verified + let observationsInWindow = verifiedObservation === undefined + if (config.mode === "manual" && verifiedObservation?.rounds.length === 2) { + const [first, second] = verifiedObservation.rounds.map(round => Date.parse(round.observedAt)) + observationsInWindow = Date.parse(configAt) <= first && first <= second && second <= Date.parse(verifiedAt) + } + return configPhase === "RECOVERY_CONFIG_APPLIED" && configPath === null && configObservation === undefined && + verifiedPhase === "RECOVERY_VERIFIED" && observationsInWindow && + verifiedPath !== null && fingerprint(configIdentity) === fingerprint(verifiedIdentity) && + Date.parse(configAt) >= Date.parse(config.createdAt) && Date.parse(verifiedAt) >= Date.parse(configAt) && + Date.parse(verifiedAt) <= Date.parse(config.validUntil) +} + +/** Preserve the serialized intent's exact key set. Adding absent optional + * reserved fields changes the canonical fingerprint of a legacy manual intent. */ +export function scopedRecoveryIntentFromCheckpoint(raw: unknown) { + const packet = checkpointSchema.parse(raw) + return intentSchema.parse(Object.fromEntries(Object.entries(packet) + .filter(([key]) => Object.hasOwn(packet.revision === 2 ? currentIntentSchema.shape : legacyIntentSchema.shape, key)) + .map(([key, value]) => [key, key === "phase" ? "RECOVERY_INTENT" : value]))) +} + +/** Capture the original signed forward authority once. Mutating an outer + * request or presenting a newer credential cannot replace this closure. */ +export function captureScopedRecoveryAuthority(input: Record) { + const captured = structuredClone(input) + const validUntil = captured.persistentProfileAuthority === undefined ? undefined + : routingProfileAuthorityBindingSchema.parse(captured.persistentProfileAuthority).packet.validUntil + return { validUntil, assert: (signal?: AbortSignal) => assertPersistentScopedProfileAuthority(captured, signal) } +} + +/** A new session/credential never extends the original mutation deadline. */ +export function scopedRecoveryDeadlineCeiling(existingDeadline: number, authorityValidUntil?: string) { + const ceiling = authorityValidUntil === undefined ? existingDeadline : Math.min(existingDeadline, Date.parse(authorityValidUntil)) + if (!Number.isFinite(ceiling)) throw fail("Срок исходного profile authority повреждён", "PLAN_EXPIRED") + return ceiling +} + +async function requestSeal(input: Record) { + const { requestProof, ...request } = input + const seal = await sealedProof(request, "request") + if (input.proofRevision !== 1 || requestProof !== seal || !(input.mode === "manual" || input.mode === "reserved" && reservedExecutions.has(input))) throw fail("Обратный запрос повреждён или несовместим") + z.string().uuid().parse(input.receiptId); z.string().datetime().parse(input.validUntil) + return seal +} + +async function sourceFor(input: Record, signal?: AbortSignal, db: Pick = pool) { + await requestSeal(input) + const principal = scopedRecoveryPrincipalSchema.parse(input.principal), current = await effectiveMcpConnection(principal, db) + if (current.owner_id !== input.ownerId || fingerprint(current.owner_permissions) !== input.ownerProof || fingerprint(current.policy) !== input.policyProof || current.generation !== input.connectionGeneration || await accessGeneration() !== input.generation) throw fail("Допуск обратного запроса изменён", "RESET_REQUIRED") + const context: ActionContext = { connection: current, principal, signal } + const execution = reservedExecutions.get(input)?.execution + const source = execution ? await loadReservedScopedRecoverySource(execution, context) : await loadScopedRecoverySource(String(input.sourceOperationId), context, db) + if (source.sourceProof !== input.sourceProof || source.configuration.checkpointProof !== input.configurationProof || source.input.receiptId !== input.sourceReceiptId || source.profile.serverId !== input.serverId || fingerprint(source.profile) !== fingerprint(input.profile) || source.configuration.packet.originalCandidateId !== input.candidateId || fingerprint(source.input.beforeRules) !== fingerprint(input.rules) || fingerprint(source.input.rules) !== fingerprint(input.beforeRules)) throw fail("Обратный запрос не соответствует исходной операции") + if ((source.input.managementBindingCiphertext ?? null) !== (input.managementBindingCiphertext ?? null)) throw fail("Обратный транспорт изменён") + return { source, context } +} + +/** Internal SQL-owner admission read; issues no transport capability. */ +export async function loadScopedManualRecoveryAdmissionWithinOwner(input: Record, principal: McpPrincipal, db: Pick) { + if (input.mode !== "manual" || fingerprint(input.principal) !== fingerprint(principal)) throw fail("Manual recovery principal/mode changed") + return sourceFor(input, undefined, db) +} + +function deadline(source: Source, context: ActionContext, channelExpiry?: string, allowElapsed = false) { + const original = scopedRecoveryPrincipalSchema.parse(source.input.principal) + const authorityExpiry = source.input.persistentProfileAuthority === undefined ? undefined + : routingProfileAuthorityBindingSchema.parse(source.input.persistentProfileAuthority).packet.validUntil + const value = scopedRecoveryDeadlineCeiling(Math.min(source.row.created_at.getTime() + 120000, Date.parse(source.configuration.observedAt) + 120000, context.connection.owner_valid_until.getTime(), + Date.parse(context.connection.policy.expiresAt), Date.parse(context.principal.credentialExpiresAt), Date.parse(original.credentialExpiresAt), + channelExpiry ? Date.parse(channelExpiry) : Infinity), authorityExpiry) + if (!Number.isFinite(value) || !allowElapsed && value <= Date.now()) throw fail("Срок обратного действия истёк", "PLAN_EXPIRED") + return value +} + +async function session(input: Record, verified = false, extra?: () => Promise, readOnly = false, externalSignal?: AbortSignal) { + const loaded = await sourceFor(input), { source, context } = loaded + const capturedAuthority = captureScopedRecoveryAuthority(source.input) + const requestedUntil = Date.parse(String(input.validUntil)), principal = context.principal + // A read-only reconciliation may outlive the mutation reservation, while its + // current and original credentials and pinned channel must still be valid. + const until = readOnly ? Math.min(Date.now() + 60000, context.connection.owner_valid_until.getTime(), Date.parse(context.connection.policy.expiresAt), Date.parse(principal.credentialExpiresAt), Date.parse(scopedRecoveryPrincipalSchema.parse(source.input.principal).credentialExpiresAt)) : requestedUntil + const timeoutSignal = AbortSignal.timeout(Math.max(1, Math.min(120000, until - Date.now()))) + const signal = externalSignal ? AbortSignal.any([timeoutSignal, externalSignal]) : timeoutSignal + const authorize = async () => { + await extra?.() + if (verified) await assertVerifiedConfigurationAccess() + if (signal.aborted || Date.now() >= until) throw fail("Обратное действие отменено или истекло", "READ_CANCELLED") + await capturedAuthority.assert(signal) + const execution = reservedExecutions.get(input)?.execution + if (execution) await createRoutingReservationStore().inspectInverseExecution(execution) + const current = await effectiveMcpConnection(principal) + if (current.owner_id !== input.ownerId || fingerprint(current.owner_permissions) !== input.ownerProof || current.generation !== input.connectionGeneration || fingerprint(current.policy) !== input.policyProof || await accessGeneration() !== input.generation) throw fail("Допуск восстановления отозван", "RESET_REQUIRED") + if (["mm:network:write", "mm:network:read", "mm:filters:write"].some(permission => !hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission))) throw fail("Право восстановления отозвано", "PERMISSION_DENIED") + // The original credential remains part of the authority, even if a newer + // human credential approved the inverse. Never check failed B's SLA here. + const original = await effectiveMcpConnection(scopedRecoveryPrincipalSchema.parse(source.input.principal)) + if (original.owner_id !== input.ownerId || fingerprint(original.policy) !== source.input.policyProof || original.generation !== source.input.connectionGeneration) throw fail("Исходный допуск отозван", "RESET_REQUIRED") + } + let binding: ManagementTransportBinding | undefined + if (input.managementBindingCiphertext != null) { + try { binding = JSON.parse(await decryptSecret(String(input.managementBindingCiphertext))) } + catch { throw fail("Management binding восстановления повреждён") } + } + if (requestedUntil > deadline(source, context, binding?.expiresAt, readOnly)) throw fail("Срок обратного запроса расширен") + await authorize() + const managementTransport = binding ? await resolveManagementTransport(binding, { authorize, deadline: until, signal }) : undefined + const options: RouteContextOptions = { authorize, signal, expectedSources: source.configuration.packet.contextSources, managementTransport, selectedCandidateId: String(input.candidateId) } + return { ...loaded, authorize, signal, options, managementTransport } +} +type Session = Awaited> + +async function observe(value: Session, phase: "before" | "after", options = value.options) { + await value.authorize() + const { source } = value, packet = source.configuration.packet, profile = source.profile + const candidate = profile.candidates.find(candidate => candidate.id === packet.originalCandidateId) + if (!candidate || profile.excludedCandidateIds.includes(candidate.id) || profile.pinnedCandidateId && profile.pinnedCandidateId !== candidate.id) throw fail("Исходный кандидат исключён", "PERMISSION_DENIED") + if (!rankRoutingCandidates(profile, await readRoutingObservations(source.ids)).find(row => row.candidate.id === candidate.id)?.eligible) throw fail("Нет свежего SLA исходного кандидата", "MEASUREMENT_UNKNOWN") + const context = await readRouteCommunityContext(profile, options) + if (!context.before.scopedExecutionAvailable || !context.ownedTarget || context.ownedTarget.nativeId !== packet.nativeId) throw fail("Owned цель восстановления не подтверждена") + const expectedBody = phase === "before" ? packet.desiredRule : packet.originalRule + const expectedProof = phase === "before" ? packet.desiredTargetProof : packet.originalTargetProof + const expectedCandidate = phase === "before" ? packet.desiredCandidateId : packet.originalCandidateId + if (context.before.dependencyProof !== packet.dependencyProof || context.before.currentCandidateId !== expectedCandidate || !context.ownedTarget.matchesRuleProof(expectedBody, expectedProof)) throw fail("Конфигурация или зависимости обратного действия изменены") + await context.fence(); await value.authorize() + return context +} + +async function originalPath(value: Session) { + let afterProbe: Awaited> | undefined + const check = await checkCandidatePath(value.source.profile, value.source.configuration.packet.originalCandidateId, + value.context.connection.id, value.options, { + before: () => observe(value, "before"), + after: async options => { + afterProbe = await observe(value, "before", options) + return afterProbe + }, + }) + if (check.status !== "passed") throw fail("Исходный кандидат не подтвердил after-state контрольную пробу", "MEASUREMENT_UNKNOWN") + const { id: freshPath } = await requireRecentPathWithContext(value.source.profile, check.candidateId, value.context.connection.id, value.options, + async options => { + if (!afterProbe || fingerprint(afterProbe.before.sourceProofs) !== fingerprint(options.expectedSources)) throw fail("Источник after-state пробы изменён") + await afterProbe.fence() + await value.authorize() + return afterProbe + }) + if (freshPath !== check.id) throw fail("After-state проба изменена") + return { id: check.id, context: afterProbe! } +} + +export async function prepareScopedRecovery(operationId: string, context: ActionContext, withObservation = false): Promise { + const source = await loadScopedRecoverySource(operationId, context) + const existing = await pool.query("SELECT state FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1 AND source_step_index=0", [operationId]) + if (existing.rowCount) throw fail("Обратное действие уже начиналось; повтор запрещён", "MUTATION_OUTCOME_UNKNOWN") + let binding: ManagementTransportBinding | undefined + if (source.input.managementBindingCiphertext != null) { + try { binding = JSON.parse(await decryptSecret(String(source.input.managementBindingCiphertext))) } + catch { throw fail("Management binding повреждён") } + } + if (withObservation && binding) throw fail("Диагностика возврата с резервным транспортом недоступна", "UNSUPPORTED_CAPABILITY") + const input: Record = { mode: "manual", serverId: source.profile.serverId, sourceOperationId: operationId, + sourceProof: source.sourceProof, configurationProof: source.configuration.checkpointProof, sourceReceiptId: source.input.receiptId, + profile: source.profile, candidateId: source.configuration.packet.originalCandidateId, rules: source.input.beforeRules, beforeRules: source.input.rules, + principal: context.principal, ownerId: context.connection.owner_id, ownerProof: fingerprint(context.connection.owner_permissions), policyProof: fingerprint(context.connection.policy), connectionGeneration: context.connection.generation, + generation: await accessGeneration(), receiptId: randomUUID(), proofRevision: 1, validUntil: new Date(deadline(source, context, binding?.expiresAt)).toISOString(), + managementBindingCiphertext: source.input.managementBindingCiphertext ?? null } + input.requestProof = await sealedProof(input, "request") + const value = await session(input, false, async () => { if (context.signal?.aborted) throw fail("Подготовка восстановления отменена", "READ_CANCELLED") }, false, context.signal) + const path = await originalPath(value) + if (withObservation) { + const selection = await path.context.captureOriginalEffectSelection(String(input.candidateId)) + if (selection.status !== "prepared") throw fail("Не подтверждена область наблюдений для исходного кандидата", "STATE_UNAVAILABLE") + input.observation = "route-effects-v1" + input.observationCiphertext = await encryptSecret(JSON.stringify(selection)) + const { requestProof: _proof, ...request } = input + input.requestProof = await sealedProof(request, "request") + } + const gatewayObjects = [source.configuration.packet.originalCandidateId, source.configuration.packet.desiredCandidateId].map(id => { + const candidate = source.profile.candidates.find(candidate => candidate.id === id) + if (!candidate) throw fail("Gateway зависимость восстановления отсутствует") + return { kind: candidate.gatewayRef.resource, id: candidate.gatewayRef.id, serverId: source.profile.serverId } + }) + return { target: { scope: "servers", serverIds: source.ids }, objects: [{ kind: "routing_filter", serverId: source.profile.serverId, id: source.configuration.packet.nativeId }, + ...gatewayObjects, + ...source.ids.map(serverId => ({ kind: "server" as const, serverId, id: String(serverId) }))], snapshots: [], before: input.beforeRules, + steps: [{ kind: "service", service, input }], requiresHuman: true, mutationCost: 1, validUntil: String(input.validUntil), verificationScope: "configuration", + warnings: ["Одно обратное действие для exact исходной операции и native ID; исходное тело восстанавливается без изменения метаданных.", "Неизвестный ответ не повторяется; сверка и перезапуск только читают состояние.", "Проверяются конфигурация и control probe; сквозной и обратный пользовательский трафик не подтверждаются.", + ...(withObservation ? ["Дополнительно: снимок до возврата и два после возврата для маршрутов, BGP и доступного буфера событий. Полнота событий неизвестна."] : [])] } +} + +async function claim(input: Record, value: Session) { + const execution = reservedExecutions.get(input)?.execution + if (execution) return routingReservationTransaction(async db => { + const binding = await createRoutingReservationStore().inspectInverseExecution(execution) + const packet = intentSchema.parse({ revision: 1, phase: "RECOVERY_INTENT", mode: "reserved", reservationId: binding.reservationId, networkProof: binding.networkProof, + sourceOperationId: binding.sourceOperationId, sourceStepIndex: 0, sourceReceiptId: binding.sourceReceiptId, sourceProof: input.sourceProof, + configurationProof: binding.configurationProof, operationId: binding.sourceOperationId, stepIndex: 0, receiptId: binding.inverseReceiptId, + requestSeal: await requestSeal(input), connectionId: value.context.connection.id, ownerId: binding.ownerId, validUntil: input.validUntil, createdAt: new Date().toISOString() }) + const intentProof = await sealedProof(packet, "intent") + const changed = await db.query("UPDATE mcp_scoped_recovery_intents SET inverse_intent_sealed=$4,inverse_intent_proof=$5 WHERE source_operation_id=$1 AND source_step_index=0 AND reservation_id=$2 AND recovery_receipt_id=$3 AND mode='reserved' AND slot_phase='reserved-attempt' AND state='intent' AND inverse_intent_sealed IS NULL AND inverse_intent_proof IS NULL AND deadline>now() RETURNING source_operation_id", + [binding.sourceOperationId, binding.reservationId, binding.inverseReceiptId, await encryptSecret(JSON.stringify(packet)), intentProof]) + if (changed.rowCount !== 1) throw fail("Reserved inverse already started or slot changed", "MUTATION_OUTCOME_UNKNOWN") + await audit(db, binding.ownerId, "routing.scoped.recovery-intent", value.context.connection.id, { sourceOperationId: binding.sourceOperationId, receiptId: binding.inverseReceiptId, intentProof, mode: "reserved" }) + return { packet, intentProof } + }) + return transaction(async db => { + await db.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-runner-admission',0))") + const rows = (await db.query("SELECT o.id,s.step_index FROM mcp_operations o JOIN mcp_operation_steps s ON s.operation_id=o.id WHERE o.connection_id=$1 AND o.state='running' AND s.state='intent' AND s.intended_state=$2::jsonb LIMIT 2 FOR UPDATE OF o,s", [value.context.connection.id, intended(input)])).rows + if (rows.length !== 1) throw fail("Не подтверждён exact running inverse operation intent") + const { assertManualInverseChargeWithinOwner } = await import("./routing-manual-inverse-store.js") + const charge = await assertManualInverseChargeWithinOwner(db, rows[0].id, input, value.context.principal) + const packet = intentSchema.parse({ revision: charge ? 2 : 1, ...(charge ? {manualInverseCharge:charge} : {}), phase: "RECOVERY_INTENT", mode: "manual", sourceOperationId: input.sourceOperationId, sourceStepIndex: 0, + sourceReceiptId: input.sourceReceiptId, sourceProof: input.sourceProof, configurationProof: input.configurationProof, operationId: rows[0].id, stepIndex: rows[0].step_index, + receiptId: input.receiptId, requestSeal: await requestSeal(input), connectionId: value.context.connection.id, ownerId: input.ownerId, validUntil: input.validUntil, createdAt: new Date().toISOString() }) + // Exact prospective encrypted size, before intent insert and any native PATCH. + // The longer phase and non-null UUID dominate both actual checkpoint forms. + if (charge) { + assertPrivateRecoverySize(packet, 262144) + assertPrivateRecoverySize(await checkpoint(value, packet, "RECOVERY_CONFIG_APPLIED", "00000000-0000-4000-8000-000000000000"), 524288) + } + const intentProof = await sealedProof(packet, "intent") + const inserted = await db.query("INSERT INTO mcp_scoped_recovery_intents(source_operation_id,source_step_index,source_receipt_id,source_configuration_proof,recovery_receipt_id,recovery_operation_id,mode,intent_sealed,intent_proof,state,deadline,authority_profile_version) VALUES($1,0,$2,$3,$4,$5,'manual',$6,$7,'intent',$8,$9) ON CONFLICT DO NOTHING RETURNING source_operation_id", + [input.sourceOperationId, input.sourceReceiptId, input.configurationProof, input.receiptId, packet.operationId, await encryptSecret(JSON.stringify(packet)), intentProof, input.validUntil, charge?.binding.profileVersion ?? null]) + if (inserted.rowCount !== 1) throw fail("Обратный intent уже существует; повтор запрещён", "MUTATION_OUTCOME_UNKNOWN") + await audit(db, packet.ownerId, "routing.scoped.recovery-intent", packet.connectionId, { sourceOperationId: packet.sourceOperationId, operationId: packet.operationId, receiptId: packet.receiptId, intentProof }) + return { packet, intentProof } + }) +} + +export function assertPrivateRecoverySize(packet: unknown, cap: 262144 | 524288) { + const cipherBytes = 4 * Math.ceil((Buffer.byteLength(JSON.stringify(packet), "utf8") + 28) / 3) + if (cipherBytes > cap) throw fail("Private recovery packet exceeds unchanged ciphertext cap") +} + +async function checkpoint(value: Session, intent: Intent, phase: Checkpoint["phase"], pathCheckId: string | null, observation?: Checkpoint["observation"]) { + const source = value.source.configuration.packet + return checkpointSchema.parse({ ...intent, phase, serverId: source.serverId, nativeId: source.nativeId, candidateId: source.originalCandidateId, + originalRule: source.originalRule, targetProof: source.originalTargetProof, dependencyProof: source.dependencyProof, sourceEpochs: source.contextSources, + pathCheckId, observedAt: new Date().toISOString(), ...(observation ? { observation } : {}) }) +} + +/** Internal capability entry, never registered as a public service/controller. + * Spending the allocation is not native permission: all engine gates still run. */ +export async function executeReservedScopedRecovery(allocation: RoutingReservedInverseAllocation, expected: { + sourceOperationId: string; principal: McpPrincipal; networkSession: RoutingReservedNetworkSession +}) { + if (!expected.networkSession) throw fail("Исходный сетевой сеанс возврата недоступен") + const network = inspectRoutingNetworkSharedContext(expected.networkSession.sharedContext) + if (network.sourceOperationId !== expected.sourceOperationId || fingerprint(network.principal) !== fingerprint(expected.principal)) { + throw fail("Исходный сетевой сеанс относится к другой операции") + } + const store = createRoutingReservationStore(), spent = await store.beginInverseAttempt(allocation) + const execution = spent.execution + try { + const binding = await store.inspectInverseExecution(execution) + if (binding.sourceOperationId !== expected.sourceOperationId || fingerprint(binding.principal) !== fingerprint(expected.principal)) throw fail("Reserved runner identity differs") + const current = await effectiveMcpConnection(binding.principal) + const context: ActionContext = { principal: binding.principal, connection: current } + const source = await loadReservedScopedRecoverySource(execution, context) + const channel: ManagementTransportBinding | undefined = source.input.managementBindingCiphertext == null ? undefined + : JSON.parse(await decryptSecret(String(source.input.managementBindingCiphertext))) + const input: Record = { mode: "reserved", serverId: source.profile.serverId, sourceOperationId: binding.sourceOperationId, + sourceProof: source.sourceProof, configurationProof: binding.configurationProof, sourceReceiptId: binding.sourceReceiptId, + profile: source.profile, candidateId: source.configuration.packet.originalCandidateId, rules: source.input.beforeRules, beforeRules: source.input.rules, + principal: binding.principal, ownerId: current.owner_id, ownerProof: fingerprint(current.owner_permissions), policyProof: fingerprint(current.policy), connectionGeneration: current.generation, + generation: await accessGeneration(), receiptId: binding.inverseReceiptId, proofRevision: 1, + validUntil: new Date(Math.min(network.deadline, Date.parse(binding.deadline), deadline(source, context, channel?.expiresAt))).toISOString(), managementBindingCiphertext: source.input.managementBindingCiphertext ?? null } + input.requestProof = await sealedProof(input, "request") + reservedExecutions.set(input, { execution, network: expected.networkSession }) + try { return await executeScopedRecovery(input) } + finally { reservedExecutions.delete(input) } + } catch (error) { + // Acknowledged spend owns this exact slot even when its authority can no + // longer be inspected. A refused or uncertain spend never enters here. + await store.markUnknown(spent.reservationId, "inverse").catch(() => {}) + throw error + } +} + +/** Fixed-domain receipt verifier used by the reservation store. It recognizes + * RECOVERY_VERIFIED only; it cannot authorize dispatch or generic VERIFIED. */ +export async function validateReservedInverseVerified(row: Record, expected: { reservationId: string; sourceOperationId: string; sourceReceiptId: string; inverseReceiptId: string; deadline: string; configurationProof: string; networkProof: string }, historical = false) { + const decrypt = historical ? decryptSecretExisting : decryptSecret + const proof = async (value: unknown, kind: string) => historical + ? (await privateFingerprinterExisting("route-ai-scoped-recovery-" + kind + "-v1"))(fingerprint(value)) : sealedProof(value, kind) + try { + for (const key of ["inverse_intent_sealed", "inverse_configuration_sealed", "verified_sealed"]) if (typeof row[key] !== "string" || Buffer.byteLength(row[key] as string) > 524288) return false + const intent = intentSchema.parse(JSON.parse(await decrypt(row.inverse_intent_sealed as string))) + const config = checkpointSchema.parse(JSON.parse(await decrypt(row.inverse_configuration_sealed as string))) + const verified = checkpointSchema.parse(JSON.parse(await decrypt(row.verified_sealed as string))) + if (intent.revision !== 1 || intent.mode !== "reserved" || intent.reservationId !== expected.reservationId || intent.sourceOperationId !== expected.sourceOperationId || intent.operationId !== expected.sourceOperationId || intent.stepIndex !== 0 || intent.sourceReceiptId !== expected.sourceReceiptId || intent.receiptId !== expected.inverseReceiptId || intent.configurationProof !== expected.configurationProof || intent.networkProof !== expected.networkProof || Date.parse(intent.validUntil) > Date.parse(expected.deadline)) return false + if (row.inverse_intent_proof !== await proof(intent, "intent") || row.inverse_configuration_proof !== await proof(config, "configuration") || row.verified_proof !== await proof(verified, "verified")) return false + return verifiedCheckpointFollowsConfiguration(config, verified) && + Object.entries(intent).every(([key, value]) => key === "phase" || fingerprint(value) === fingerprint(config[key as keyof Checkpoint])) + } catch { return false } +} + +/** One native engine. Reserved and manual lanes use the same 0031 source slot; + * reserved evidence never overwrites its independently signed budget packet. */ +export async function executeScopedRecovery(input: Record) { + const loaded = await sourceFor(input) + return withApplicationMutationLock(() => withConfigMutationLock(loaded.source.ids, async () => { + let claimStarted = false + let phase = "pre-dispatch-session" + try { + const value = await session(input, true) + phase = "pre-dispatch-path" + const path = await originalPath(value) + const packet = value.source.configuration.packet + const effects = input.observation === "route-effects-v1" ? await (async () => { + if (value.managementTransport || input.mode !== "manual" || typeof input.observationCiphertext !== "string" || + Buffer.byteLength(input.observationCiphertext) > 32768 || !path.context.ownedTarget) throw fail("Недействительная область наблюдений возврата") + const selection = originalEffectSelectionSchema.parse(JSON.parse(await decryptSecret(input.observationCiphertext))) + if (selection.status !== "prepared" || selection.serverId !== packet.serverId || + selection.candidateId !== packet.originalCandidateId) throw fail("Область наблюдений не совпадает с обратным планом") + let fence = path.context.fence + const reader = await createInverseEffectReader(path.context.ownedTarget.endpoint, { + signal: value.signal, deadline: Date.parse(String(input.validUntil)), guard: () => fence(), + }) + const baseline = await reader.capture() + if (!originalEffectBaselineMatches(selection, baseline)) throw fail("Исходный снимок наблюдений возврата изменился") + return { + async observeAfter(guard: () => Promise) { + fence = guard + const rounds = [await reader.capture(), await reader.capture()] + return summarizeInverseEffects(await inspectOriginalEffectRounds(selection, rounds, baseline)) + }, + } + })() : undefined + phase = "claim" + claimStarted = true + const intent = await claim(input, value) + const reservationId = intent.packet.revision === 1 ? intent.packet.reservationId : undefined + if (reservedExecutions.has(input) && !reservationId) throw fail("Исходный резерв обратного шага отсутствует") + phase = "configuration-gate" + await executeGuardedConfigurationWrite(packet.serverId, "PATCH", "/routing/filter/rule/" + packet.nativeId, async signal => { + phase = "before-dispatch-context" + const { id: proof, context: fresh, assertFresh } = await requireRecentPathWithContext(value.source.profile, packet.originalCandidateId, + value.context.connection.id, value.options, options => observe(value, "before", options)) + if (proof !== path.id) throw fail("After-state проба устарела после intent") + // Preserve the already committed manual allocation after claim/reads. + // A valid copied intent stamp never substitutes for a present, matching + // parent at dispatch. This creates no debit or recovery authority. + if (intent.packet.mode === "manual" && value.source.input.persistentProfileAuthority !== undefined) { + if (intent.packet.revision !== 2) throw fail("Persistent manual inverse requires its original charge stamp") + const capturedCharge = intent.packet.manualInverseCharge + const { assertManualInverseChargeWithinOwner } = await import("./routing-manual-inverse-store.js") + const currentCharge = await routingReservationTransaction(db => + assertManualInverseChargeWithinOwner(db, intent.packet.operationId, input, value.context.principal), undefined, true) + if (!currentCharge || fingerprint(currentCharge) !== fingerprint(capturedCharge)) throw fail("Manual inverse dispatch charge differs from original intent") + } + await fresh.fence(); await value.authorize() + phase = "native-dispatch" + if (value.managementTransport) { + await assertManagementTransport(value.managementTransport) + assertFresh() + await patchScopedCommunityViaManagementTransport(value.managementTransport, packet.nativeId, packet.originalRule, signal) + } else { + assertFresh() + signal?.throwIfAborted() + const nativeSignal = signal ? AbortSignal.any([value.signal, signal]) : value.signal + await patchScopedCommunity(fresh.ownedTarget!.endpoint, packet.nativeId, packet.originalRule, nativeSignal) + } + }) + phase = "configuration-readback" + const appliedContext = await observe(value, "after") + const config = await checkpoint(value, intent.packet, "RECOVERY_CONFIG_APPLIED", null) + const configProof = await sealedProof(config, "configuration") + const configSealed = await encryptSecret(JSON.stringify(config)) + await (reservedExecutions.has(input) ? routingReservationTransaction : transaction)(async db => { + const saved = reservedExecutions.has(input) + ? await db.query("UPDATE mcp_scoped_recovery_intents SET inverse_configuration_sealed=$4,inverse_configuration_proof=$5 WHERE source_operation_id=$1 AND source_step_index=0 AND recovery_receipt_id=$2 AND inverse_intent_proof=$3 AND reservation_id=$6 AND mode='reserved' AND slot_phase='reserved-attempt' AND state='intent' AND inverse_configuration_sealed IS NULL AND deadline>now() RETURNING source_operation_id", [input.sourceOperationId, input.receiptId, intent.intentProof, configSealed, configProof, reservationId]) + : await db.query("UPDATE mcp_operation_checkpoints SET configuration_sealed=$3,configuration_proof=$4 WHERE operation_id=$1 AND step_index=$2 AND configuration_sealed IS NULL AND configuration_proof IS NULL RETURNING operation_id", [intent.packet.operationId, intent.packet.stepIndex, configSealed, configProof]) + if (saved.rowCount !== 1) throw fail("Configuration checkpoint inverse недоступен") + await audit(db, String(input.ownerId), "routing.scoped.recovery-config-applied", value.context.connection.id, + { receiptId: input.receiptId, requestSeal: await requestSeal(input), configurationProof: configProof }) + }) + phase = "route-effects" + const observation = await effects?.observeAfter(async () => { + await appliedContext.fence() + const { rowCount } = await pool.query(` + SELECT c.operation_id FROM mcp_operation_checkpoints c + JOIN mcp_operations o ON o.id=c.operation_id + JOIN mcp_operation_steps s ON s.operation_id=c.operation_id AND s.step_index=c.step_index + JOIN mcp_scoped_recovery_intents r ON r.recovery_operation_id=c.operation_id + WHERE c.operation_id=$1 AND c.step_index=$2 AND c.configuration_sealed=$3 AND c.configuration_proof=$4 + AND o.connection_id=$5 AND o.state='running' AND s.state='intent' + AND r.source_operation_id=$6 AND r.source_step_index=0 AND r.recovery_receipt_id=$7 + AND r.mode='manual' AND r.intent_proof=$8 AND r.state='intent' AND r.deadline>clock_timestamp()`, + [intent.packet.operationId, intent.packet.stepIndex, configSealed, configProof, value.context.connection.id, + intent.packet.sourceOperationId, intent.packet.receiptId, intent.intentProof]) + if (rowCount !== 1) throw fail("Контрольная точка или допуск возврата изменились во время наблюдений") + }) + phase = "post-probe" + const check = await checkCandidatePath(value.source.profile, packet.originalCandidateId, value.context.connection.id, + value.options, { before: async () => appliedContext, after: options => observe(value, "after", options) }) + if (check.status !== "passed") throw fail("После inverse контрольная проба не подтверждена", "MUTATION_OUTCOME_UNKNOWN") + const reservedNetwork = reservedExecutions.get(input)?.network + if (reservedNetwork) { + phase = "reserved-network-rounds" + const assertCheckpoint = async () => { + await value.authorize() + const remaining = Math.min(Date.parse(String(input.validUntil)), reservedNetwork.readBudget().deadline) - Date.now() + if (remaining <= 0) throw fail("Срок обратного раунда истёк") + const query: QueryConfig & { query_timeout: number } = { + text: `SELECT 1 FROM mcp_scoped_recovery_intents r + JOIN mcp_operations o ON o.id=r.source_operation_id + JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=r.source_step_index + WHERE r.source_operation_id=$1 AND r.source_step_index=0 AND r.reservation_id=$2 + AND r.recovery_receipt_id=$3 AND r.inverse_intent_proof=$4 + AND r.inverse_configuration_sealed=$5 AND r.inverse_configuration_proof=$6 + AND r.mode='reserved' AND r.slot_phase='reserved-attempt' AND r.state='intent' + AND r.deadline>clock_timestamp() AND o.connection_id=$7 AND o.state='running' AND s.state='intent' LIMIT 2`, + values: [intent.packet.sourceOperationId, reservationId, intent.packet.receiptId, intent.intentProof, + configSealed, configProof, value.context.connection.id], + query_timeout: Math.min(10_000, remaining), + } + if ((await pool.query(query)).rowCount !== 1) throw fail("Контрольная точка обратного раунда изменилась") + await value.authorize() + } + const pingRows = z.array(z.record(z.string(), z.string())).max(256) + for (const round of [1, 2]) { + const observed = await reservedNetwork.readConfigurationBoundInverseSnapshot(assertCheckpoint) + const [probe] = observed.probes + if (observed.stage !== "after-inverse" || observed.round !== round || observed.probes.length !== 1 || probe?.key !== packet.originalCandidateId) { + throw fail("Раунд исходного пути A не подтверждён") + } + const { ping } = inspectControlPing(pingRows.parse(probe.pingPayload)) + const trace = inspectTrace(probe.tracePayload, probe.request.destination, probe.expectedHopAddresses.map(hop => hop.address)) + if (!ping.measured || ping.loss !== 0 || !trace.verified || !trace.completeTrace) throw fail("Трафик пути A после возврата не подтверждён") + } + } + await value.authorize() + const verified = await checkpoint(value, intent.packet, "RECOVERY_VERIFIED", check.id, observation) + const verifiedProof = await sealedProof(verified, "verified") + if (!verifiedCheckpointFollowsConfiguration(config, verified)) throw fail("Порядок контрольных точек восстановления не подтверждён") + await (reservedExecutions.has(input) ? routingReservationTransaction : transaction)(async db => { + await value.authorize() + const reserved = reservedExecutions.has(input) + if (!reserved) { + const retained = await db.query("SELECT operation_id FROM mcp_operation_checkpoints WHERE operation_id=$1 AND step_index=$2 AND configuration_sealed=$3 AND configuration_proof=$4 FOR SHARE", + [intent.packet.operationId, intent.packet.stepIndex, configSealed, configProof]) + if (retained.rowCount !== 1) throw fail("Configuration checkpoint inverse изменён после пробы") + } + const bindings = [input.sourceOperationId, input.receiptId, intent.intentProof, await encryptSecret(JSON.stringify(verified)), verifiedProof] + const changed = await db.query(reserved + ? "UPDATE mcp_scoped_recovery_intents SET state='verified',verified_sealed=$4,verified_proof=$5,finished_at=now() WHERE source_operation_id=$1 AND source_step_index=0 AND recovery_receipt_id=$2 AND inverse_intent_proof=$3 AND mode='reserved' AND slot_phase='reserved-attempt' AND state='intent' AND inverse_configuration_sealed=$6 AND inverse_configuration_proof=$7 AND reservation_id=$8 AND deadline>now() RETURNING source_operation_id" + : "UPDATE mcp_scoped_recovery_intents SET state='verified',verified_sealed=$4,verified_proof=$5,finished_at=now() WHERE source_operation_id=$1 AND source_step_index=0 AND recovery_receipt_id=$2 AND intent_proof=$3 AND state='intent' AND deadline>now() RETURNING source_operation_id", + reserved ? [...bindings, configSealed, configProof, reservationId] : bindings) + if (changed.rowCount !== 1) throw fail("Квитанция inverse не может быть подтверждена") + if (observation) await audit(db, String(input.ownerId), "routing.scoped.recovery-route-effects", value.context.connection.id, + { operationId: intent.packet.operationId, receiptId: input.receiptId, configurationProof: configProof, observation }) + await audit(db, String(input.ownerId), "routing.scoped.recovery-verified", value.context.connection.id, { sourceOperationId: input.sourceOperationId, receiptId: input.receiptId, verifiedProof }) + }) + return recoveryResult(input, verifiedProof, verified.observation) + } catch (error) { + await audit(pool, String(input.ownerId), "routing.scoped.recovery-unconfirmed", loaded.context.connection.id, + { receiptId: input.receiptId, phase, code: error instanceof McpError ? error.code : "STATE_UNAVAILABLE" }).catch(() => {}) + if (!claimStarted) throw error + // This is deliberately attempted even if COMMIT acknowledgement was lost. + // Matching receipt prevents changing somebody else's winning inverse. + await pool.query("UPDATE mcp_scoped_recovery_intents SET state='unknown',finished_at=now() WHERE source_operation_id=$1 AND source_step_index=0 AND recovery_receipt_id=$2 AND state='intent'", [input.sourceOperationId, input.receiptId]).catch(() => {}) + throw fail("Результат inverse не подтверждён; повтор запрещён, требуется сверка", "MUTATION_OUTCOME_UNKNOWN") + } + })) +} + +async function recoveryResult(input: Record, verifiedProof: string, observation?: Checkpoint["observation"]) { + return { outcome: "recovered", nativeWrites: 1, configurationVerified: true, controlProbeVerified: true, forwardingVerified: false, + sourceOperationId: input.sourceOperationId, receiptId: input.receiptId, proofRevision: 1, recoveryCheckpointProof: verifiedProof, + nativeVerificationSeal: await sealedProof({ request: await requestSeal(input), verifiedProof, source: input.sourceProof }, "result"), + ...(observation ? { observation } : {}) } +} + +type ManualRecoveryJournal = { + state: string; mode: string; deadline: Date; recovery_operation_id: string | null + source_receipt_id: string; source_configuration_proof: string; intent_proof: string; intent_sealed: string | null + verified_proof: string | null; verified_sealed: string | null + operation_state: string | null; step_index: number | null; step_state: string | null; step_result: unknown + configuration_proof: string | null; configuration_sealed: string | null +} + +async function readManualRecoveryJournal(input: Record, value: Session) { + const { rows } = await pool.query(` + SELECT r.state,r.mode,r.deadline,r.recovery_operation_id,r.source_receipt_id,r.source_configuration_proof, + r.intent_proof,r.verified_proof,o.state AS operation_state,s.step_index,s.state AS step_state,c.configuration_proof, + CASE WHEN octet_length(r.intent_sealed)<=524288 THEN r.intent_sealed END AS intent_sealed, + CASE WHEN octet_length(r.verified_sealed)<=524288 THEN r.verified_sealed END AS verified_sealed, + CASE WHEN octet_length(c.configuration_sealed)<=524288 THEN c.configuration_sealed END AS configuration_sealed, + CASE WHEN octet_length(s.result::text)<=524288 THEN s.result END AS step_result + FROM mcp_scoped_recovery_intents r + LEFT JOIN mcp_operations o ON o.id=r.recovery_operation_id AND o.connection_id=$3 + LEFT JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.intended_state=$4::jsonb + LEFT JOIN mcp_operation_checkpoints c ON c.operation_id=s.operation_id AND c.step_index=s.step_index + WHERE r.source_operation_id=$1 AND r.source_step_index=0 AND r.recovery_receipt_id=$2 LIMIT 2`, + [input.sourceOperationId, input.receiptId, value.context.connection.id, intended(input)]) + if (rows.length > 1) throw fail("Неоднозначный журнал обратной операции") + return rows[0] ?? null +} + +async function verifiedRecord(input: Record, value: Session, row: ManualRecoveryJournal | null) { + if (!row || row.state !== "verified" || !row.intent_sealed || !row.verified_sealed || !row.verified_proof) return null + try { + const intent = intentSchema.parse(JSON.parse(await decryptSecret(row.intent_sealed))), packet = checkpointSchema.parse(JSON.parse(await decryptSecret(row.verified_sealed))) + if ((packet.observation !== undefined) !== (input.observation === "route-effects-v1")) return null + if (row.mode !== "manual" || row.source_receipt_id !== input.sourceReceiptId || row.source_configuration_proof !== input.configurationProof || row.deadline.toISOString() !== input.validUntil || row.recovery_operation_id !== intent.operationId || row.intent_proof !== await sealedProof(intent, "intent") || row.verified_proof !== await sealedProof(packet, "verified") || packet.phase !== "RECOVERY_VERIFIED" || !packet.pathCheckId || intent.requestSeal !== await requestSeal(input) || intent.sourceOperationId !== input.sourceOperationId || intent.sourceProof !== input.sourceProof || intent.configurationProof !== input.configurationProof || intent.receiptId !== input.receiptId || intent.connectionId !== value.context.connection.id || intent.ownerId !== input.ownerId || intent.validUntil !== input.validUntil || Object.entries(intent).some(([key, value]) => fingerprint(value) !== fingerprint(key === "phase" ? "RECOVERY_INTENT" : packet[key as keyof Checkpoint]))) return null + const source = value.source.configuration.packet + if (packet.originalRule !== source.originalRule || packet.serverId !== source.serverId || packet.nativeId !== source.nativeId || packet.candidateId !== source.originalCandidateId || packet.targetProof !== source.originalTargetProof || packet.dependencyProof !== source.dependencyProof || fingerprint(packet.sourceEpochs) !== fingerprint(source.contextSources)) return null + return { packet, verifiedProof: row.verified_proof } + } catch { return null } +} + +async function configurationRecord(input: Record, value: Session, row: ManualRecoveryJournal | null) { + if (!row?.configuration_sealed) return null + try { + const seal = await requestSeal(input), packet = checkpointSchema.parse(JSON.parse(await decryptSecret(row.configuration_sealed))) + const intent = scopedRecoveryIntentFromCheckpoint(packet) + const original = value.source.configuration.packet + const matches = packet.phase === "RECOVERY_CONFIG_APPLIED" && packet.pathCheckId === null && packet.requestSeal === seal && packet.receiptId === input.receiptId && packet.sourceOperationId === input.sourceOperationId && packet.sourceProof === input.sourceProof && packet.configurationProof === input.configurationProof && packet.connectionId === value.context.connection.id && packet.ownerId === input.ownerId && packet.operationId === row.recovery_operation_id && packet.stepIndex === row.step_index && row.intent_proof === await sealedProof(intent, "intent") && row.configuration_proof === await sealedProof(packet, "configuration") && packet.originalRule === original.originalRule && packet.nativeId === original.nativeId && packet.serverId === original.serverId && packet.targetProof === original.originalTargetProof && packet.dependencyProof === original.dependencyProof && packet.candidateId === original.originalCandidateId && fingerprint(packet.sourceEpochs) === fingerprint(original.contextSources) + return matches ? packet : null + } catch { return null } +} + +export function registerScopedRecoveryService() { + registerMcpService(service, { + current: async input => { const value = await session(input); await observe(value, "before"); return value.source.input.rules }, + execute: executeScopedRecovery, + verify: async (input, result) => { + const value = await session(input), journal = await readManualRecoveryJournal(input, value) + const record = await verifiedRecord(input, value, journal) + const config = record && await configurationRecord(input, value, journal) + if (!record || !config || !verifiedCheckpointFollowsConfiguration(config, record.packet) || + fingerprint(result) !== fingerprint(await recoveryResult(input, record.verifiedProof, record.packet.observation))) return false + await observe(value, "after") + const sameJournal = fingerprint(journal) === fingerprint(await readManualRecoveryJournal(input, value)) + await value.authorize() + return sameJournal + }, + reconcile: async (input, authorize) => { + const value = await session(input, false, authorize, true), context = await readRouteCommunityContext(value.source.profile, value.options) + const packet = value.source.configuration.packet, dependencies = context.before.dependencyProof === packet.dependencyProof + const target = context.ownedTarget + const sameTarget = target?.nativeId === packet.nativeId + const configurationMatchesDesired = dependencies && sameTarget && context.before.currentCandidateId === packet.originalCandidateId && target.matchesRuleProof(packet.originalRule, packet.originalTargetProof) + const configurationMatchesOriginal = dependencies && sameTarget && context.before.currentCandidateId === packet.desiredCandidateId && target.matchesRuleProof(packet.desiredRule, packet.desiredTargetProof) + const journal = await readManualRecoveryJournal(input, value) + const record = await verifiedRecord(input, value, journal) + const config = await configurationRecord(input, value, journal) + const verified = !!record && !!config && verifiedCheckpointFollowsConfiguration(config, record.packet) + const expected = verified ? await recoveryResult(input, record.verifiedProof, record.packet.observation) : null + const generic = !!record && !!expected && journal?.step_index === record.packet.stepIndex && journal.step_state === "verified" && + fingerprint(journal.step_result) === fingerprint(expected) + await context.fence() + if (fingerprint(journal) !== fingerprint(await readManualRecoveryJournal(input, value))) throw fail("Журнал обратной операции изменился во время сверки") + await value.authorize() + const recoveryState = journal?.state === "intent" && (journal.deadline.getTime() <= Date.now() || journal.operation_state && !["pending", "running"].includes(journal.operation_state)) ? "unknown" : journal?.state ?? "not-started" + return { configurationMatchesDesired, configurationMatchesOriginal, configurationCheckpointAvailable: !!config, recoveryState, recoveryCheckpointAvailable: verified, controlProbeVerified: verified, + matchesOriginalState: configurationMatchesOriginal, matchesDesiredState: configurationMatchesDesired && generic, + ...(verified && record.packet.observation ? { observation: record.packet.observation } : {}) } + }, + }) +} + +/** Restart maintenance is read-only with respect to the network. */ +export async function recoverScopedRecoveryIntents() { + await withBackgroundPartitionWrite(({ client }) => client.query(`WITH interrupted AS ( + SELECT intent.source_operation_id,intent.source_step_index + FROM mcp_scoped_recovery_intents intent + WHERE intent.state='intent' AND (intent.deadline<=now() OR EXISTS ( + SELECT 1 FROM mcp_operations operation + WHERE operation.id=intent.recovery_operation_id AND operation.state NOT IN ('pending','running') + )) + ORDER BY intent.deadline,intent.source_operation_id,intent.source_step_index + LIMIT 1000 FOR UPDATE SKIP LOCKED + ) + UPDATE mcp_scoped_recovery_intents intent SET state='unknown',finished_at=now() + FROM interrupted WHERE intent.source_operation_id=interrupted.source_operation_id + AND intent.source_step_index=interrupted.source_step_index`)) +} diff --git a/backend/src/modules/mcp/routing-community-scoped-request.ts b/backend/src/modules/mcp/routing-community-scoped-request.ts new file mode 100644 index 00000000..9a0c55f0 --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-request.ts @@ -0,0 +1,28 @@ +import type { PoolClient } from "pg" +import { COMMUNITY_PATH_MAX_AGE_MS } from "./path-proof-purpose.js" +import { fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { routingMeasurementRefusal } from "./routing-community-predispatch-diagnostic.js" + +export async function readScopedPathCheckpoint(db: Pick, id: string, connectionId: string, phase: "admission" | "configuration" = "admission") { + const { rows: [row] } = await db.query("SELECT id,connection_id,profile_hash,candidate_id,status,created_at,evidence FROM mcp_path_checks WHERE id=$1 AND connection_id=$2", [id, connectionId]) + const now = Date.now(), createdAt = row?.created_at.getTime(), observedAt = Date.parse(row?.evidence?.observedAt) + const validUntil = Math.min(createdAt, observedAt) + COMMUNITY_PATH_MAX_AGE_MS + if (!row || row.status !== "passed") { + throw routingMeasurementRefusal("bound-proof-unavailable", "Контрольное подтверждение пути устарело") + } + if (createdAt > now || phase === "admission" && + (!Number.isFinite(validUntil) || observedAt > now || validUntil <= now)) { + throw routingMeasurementRefusal("bound-proof-stale", "Контрольное подтверждение пути устарело") + } + return { proof: await privateSecretFingerprint(fingerprint(row), "route-ai-scoped-path-checkpoint-v1"), validUntil } +} + +/** One request domain for forward execution and trusted recovery resolution. */ +export async function assertScopedCommunityRequest(input: Record) { + const { requestProof: _proof, ...request } = input + const revision = input.proofRevision + if (revision !== 2 && revision !== 3 || revision === 2 && input.persistentProfileAuthority !== undefined || revision === 3 && input.persistentProfileAuthority === undefined) throw new McpError("STATE_CONFLICT", "Несовместимая квитанция запроса", 409) + const proof = await privateSecretFingerprint(fingerprint(request), `route-ai-scoped-request-v${revision}`) + if (input.requestProof !== proof || typeof input.receiptId !== "string") throw new McpError("STATE_CONFLICT", "Квитанция запроса повреждена", 409) + return proof +} diff --git a/backend/src/modules/mcp/routing-community-scoped-service.ts b/backend/src/modules/mcp/routing-community-scoped-service.ts new file mode 100644 index 00000000..de1a63eb --- /dev/null +++ b/backend/src/modules/mcp/routing-community-scoped-service.ts @@ -0,0 +1,303 @@ +import { createRoutingNetworkSessionForCurrentExecution } from "./routing-network-execution-session.js" +import { readRoutingNetworkScopedCommunityContext } from "./routing-network-scoped-context.js" +import { resolveRoutingNetworkForwardRoundConfigurationContext, closeRoutingNetworkForwardRoundConfigurationContext } from "./routing-network-configuration-bridge.js" +import { originalEffectReportSchema, originalEffectSelectionSchema, unknownOriginalEffects, originalEffectBaselineMatches, inspectOriginalEffectRounds, type OriginalEffectReport } from "./routing-original-effects.js" +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess, executeGuardedConfigurationWrite } from "../../services/config-mutation-policy.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { managedRoutingFilterBodiesMatch } from "../../services/routing-filter-body.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint, redact } from "./security.js" +import { saveScopedConfigurationCheckpoint, loadScopedConfigurationCheckpoint, saveScopedNetworkCheckpoint, loadScopedNetworkCheckpoint } from "./routing-community-scoped-checkpoint.js" +import { assertScopedCommunityRequest as sealedInput, readScopedPathCheckpoint } from "./routing-community-scoped-request.js" +import { prepareManagementTransportBinding } from "./management-transport.js" +import { readRouteCommunityContext, type OwnedRouteCommunityTarget } from "./routing-community-context.js" +import { candidateMatchesGateway } from "./routing-community-context-model.js" +import { rankRoutingCandidates } from "./routing-model.js" +import { readRoutingObservations } from "./routing-observations.js" +import { requireRecentPath, requireRecentPathWithContext, checkCandidatePath } from "./path-verification.js" +import { compileScopedCommunitySwitch, scopedConfigurationMatches } from "./routing-community-scoped-model.js" +import { patchScopedCommunity } from "./routing-community-scoped-native.js" +import { registerMcpService } from "./service-registry.js" +import { assertPersistentScopedProfileAuthority, resolveRoutingProfileAuthority, routingProfileAuthorityBindingSchema, scopedProfileAuthorityTransport } from "./routing-profile-authority.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import { createRoutingReservationStore } from "./routing-reservation-store.js" +import { routingMeasurementRefusal } from "./routing-community-predispatch-diagnostic.js" + +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict() +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, 409) +const effectReadFailure = (stage: "original-effect-rounds-unavailable" | "effect-publication-revalidation-unavailable", error: unknown) => { + const code = error instanceof McpError && ["READ_CANCELLED", "PLAN_EXPIRED", "LIMIT_EXCEEDED", "STATE_CONFLICT", "RESET_REQUIRED", "STATE_UNAVAILABLE"].includes(error.code) + ? error.code : "STATE_UNAVAILABLE" + return unknownOriginalEffects(stage + ":" + code) +} +export const scopedProfileIds = (profile: z.infer) => [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])].sort((a, b) => a - b) +const publicRule = (rule: { community: string; gateway: string; outIface: string }) => ({ community: rule.community, action: "route", gateway: rule.gateway, gatewayTunnelId: rule.outIface }) +type ObservationPhase = "admission" | "configuration" +async function access(input: Record, signal?: AbortSignal, verified = false, phase: ObservationPhase = "admission") { + if (signal?.aborted) throw fail("Операция отменена", "READ_CANCELLED") + if (verified) await assertVerifiedConfigurationAccess() + const principal = principalSchema.parse(input.principal), current = await effectiveMcpConnection(principal) + const profile = routeAiProfileSchema.parse(input.profile), ids = scopedProfileIds(profile) + if (current.owner_id !== input.ownerId || current.policy.mode === "observer" || ["mm:filters:write", "mm:network:read"].some(permission => !hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission))) throw fail("Доступ к переключению пути отозван", "PERMISSION_DENIED") + if (fingerprint(current.policy) !== input.policyProof || current.generation !== input.connectionGeneration || await accessGeneration() !== input.generation) throw fail("Допуск изменился", "RESET_REQUIRED") + if (ids.some(id => !current.policy.scope.serverIds.includes(id))) throw fail("Зависимость вне области доступа", "SERVER_DENIED") + await assertPersistentScopedProfileAuthority(input, signal) + if (phase === "admission") { + // SLA and the preflight age authorize dispatch. Once PATCH is sent, native + // configuration evidence must survive a later stale/bad SLA measurement. + const ranked = rankRoutingCandidates(profile, await readRoutingObservations(ids)) + if (!ranked.find(row => row.candidate.id === input.candidateId)?.eligible) throw routingMeasurementRefusal("sla-unavailable", "Нет свежего SLA выбранного пути") + } + if (input.pathCheckId !== undefined && (await readScopedPathCheckpoint(pool, String(input.pathCheckId), principal.connectionId, phase)).proof !== input.pathCheckpointProof) throw routingMeasurementRefusal("bound-proof-changed", "Подтверждение пути изменилось") + return { principal, current, profile, ids } +} + +async function observe(input: Record, signal?: AbortSignal, verified = false, extra?: () => Promise, phase: ObservationPhase = "admission") { + let accessPhase: ObservationPhase = "configuration" + const authorize = async () => { await extra?.(); await access(input, signal, verified, accessPhase) } + const { profile } = await access(input, signal, verified, accessPhase) + const context = await readRouteCommunityContext(profile, { selectedCandidateId: String(input.candidateId), authorize, signal, expectedSources: input.contextSources as Array<{ serverId: number; proof: string }> | undefined }) + await authorize() + if (!context.before.scopedExecutionAvailable || !context.ownedTarget) throw fail(String(context.before.scopedExecutionReason), "UNSUPPORTED_CAPABILITY") + if (input.nativeId !== undefined && context.ownedTarget.nativeId !== input.nativeId) throw fail("Owned ID изменился") + const candidate = profile.candidates.find(row => row.id === input.candidateId) + if (!candidate || profile.excludedCandidateIds.includes(candidate.id) || profile.pinnedCandidateId && profile.pinnedCandidateId !== candidate.id) throw fail("Кандидат исключён", "PERMISSION_DENIED") + accessPhase = phase + await context.fence() + return { context, candidate, profile, authorize } +} + +async function receiptExists(input: Record, action: string) { + const seal = await sealedInput(input) + const { rows } = await pool.query("SELECT id FROM mcp_audit WHERE connection_id=$1 AND actor_id=$2 AND action=$3 AND payload->>'receiptId'=$4 AND payload->>'requestSeal'=$5 LIMIT 1", [principalSchema.parse(input.principal).connectionId, input.ownerId, action, input.receiptId, seal]) + return rows.length === 1 +} +async function receipt(input: Record, action: string) { + const requestSeal = await sealedInput(input) + await assertPersistentScopedProfileAuthority(input) + await audit(pool, String(input.ownerId), action, principalSchema.parse(input.principal).connectionId, { receiptId: input.receiptId, requestSeal, revision: input.proofRevision }) +} +const matches = (input: Record, context: { before: Parameters[1] }) => scopedConfigurationMatches({ dependencyProof: input.dependencyProof, originalTargetProof: input.originalTargetProof, desiredTargetProof: input.desiredTargetProof, candidateId: input.candidateId }, context.before) +async function matchesDesired(input: Record, context: { before: Parameters[1]; ownedTarget: Pick | undefined }) { + if (matches(input, context).configurationMatchesDesired) return true + const target = context.ownedTarget + return !!target && target.nativeId === input.nativeId && context.before.dependencyProof === input.dependencyProof && + context.before.currentCandidateId === input.candidateId && + target.matchesRuleProof(await decryptSecret(String(input.desiredRuleCiphertext)), input.desiredTargetProof) +} +async function original(input: Record, verified: boolean) { + await sealedInput(input) + const value = await observe(input, undefined, verified) + if (!matches(input, value.context).configurationMatchesOriginal) throw fail("Исходное состояние изменилось") + const rebuilt = compileScopedCommunitySwitch(String(value.context.ownedTarget!.rawRow.rule), value.profile.community, value.candidate.gateway, String(input.outIface), value.profile.family) + if (String(value.context.ownedTarget!.rawRow.rule) !== await decryptSecret(String(input.originalRuleCiphertext)) || rebuilt.compiledBody !== await decryptSecret(String(input.desiredRuleCiphertext)) || value.context.before.currentCandidateId !== input.originalCandidateId) throw fail("Исходное тело или кандидат изменились") + if (value.context.ownedTarget!.desiredTargetProof(rebuilt.compiledBody) !== input.desiredTargetProof) throw fail("Компилятор или цель изменились") + if (value.candidate.controlProbe) await requireRecentPath(value.profile, value.candidate.id, principalSchema.parse(input.principal).connectionId, { authorize: value.authorize }) + await value.context.fence() + return { ...value, compiledBody: rebuilt.compiledBody } +} + +export async function prepareScopedCommunitySwitch(input: Record, action: ActionContext): Promise { + const profile = routeAiProfileSchema.parse(input.profile) + const request: Record = { serverId: profile.serverId, profile: input.profile, candidateId: input.candidateId, principal: action.principal, ownerId: action.connection.owner_id, policyProof: fingerprint(action.connection.policy), connectionGeneration: action.connection.generation, generation: await accessGeneration() } + await access(request, action.signal, false, "configuration") + // This metadata is supplied only by the internal dispatcher. Never read a + // profile ID/version or an authority binding from the public input object. + const persistent = action.persistentRoutingProfile + if (persistent) { + const sources = await createRoutingSourceContext(await readRoutingEndpoints(scopedProfileIds(profile))) + request.contextSources = sources.expected + request.persistentProfileAuthority = await resolveRoutingProfileAuthority({ profileId: persistent.id, expectedVersion: persistent.version, + profile, principal: action.principal, ownerId: action.connection.owner_id, transport: { contextSources: sources.expected }, signal: action.signal }) + await access(request, action.signal, false, "configuration") + } + const value = await observe(request, action.signal), owned = value.context.ownedTarget! + const { id: pathCheckId, context: checkedContext, assertFresh } = await requireRecentPathWithContext(profile, value.candidate.id, + action.connection.id, { authorize: value.authorize, signal: action.signal, expectedSources: value.context.before.sourceProofs }) + if (checkedContext.before.dependencyProof !== value.context.before.dependencyProof || + checkedContext.before.targetRuleProof !== value.context.before.targetRuleProof) throw fail("Контекст контрольной пробы изменился") + const checkpoint = await readScopedPathCheckpoint(pool, pathCheckId, action.connection.id) + // The context fence closes over request, including the path proof once bound. + Object.assign(request, { pathCheckId, pathCheckpointProof: checkpoint.proof }) + if (!value.candidate.controlProbe) throw fail("Scoped переключение требует свежий control probe", "MEASUREMENT_UNKNOWN") + const client = value.context.clients.get(value.profile.serverId)! + await value.context.fence() + // Already validated exact candidate identity by the context; use that same bounded snapshot endpoint. + const rows = await client.get[]>(value.candidate.gatewayRef.resource === "gre" ? "/interface/gre" : value.profile.family === "ipv4" ? "/ip/route" : "/ipv6/route") + const target = rows.filter(row => row[".id"] === value.candidate.gatewayRef.id) + if (target.length !== 1) throw fail("Кандидат отсутствует") + const outIface = value.candidate.gatewayRef.resource === "gre" ? String(target[0].name) : "" + if (!candidateMatchesGateway(value.profile, value.candidate, { gateway: value.candidate.gateway, gatewayInterface: outIface }, target[0])) throw fail("Gateway кандидата не соответствует exact native identity") + const compiled = compileScopedCommunitySwitch(String(owned.rawRow.rule), value.profile.community, value.candidate.gateway, outIface, value.profile.family) + if (managedRoutingFilterBodiesMatch(owned.rawRow.rule, compiled.compiledBody)) throw fail("Путь уже выбран") + let managementBindingCiphertext: string | null = null + const hasChannel = (await pool.query("SELECT id FROM mcp_management_channels WHERE server_id=$1 AND revoked_at IS NULL AND expires_at>now()", [profile.serverId])).rows.length > 0 + if (hasChannel) { + try { managementBindingCiphertext = await encryptSecret(JSON.stringify(await prepareManagementTransportBinding(profile.serverId, { authorize: value.authorize, signal: action.signal, deadline: Math.min(Date.now() + 120000, Date.parse(action.principal.credentialExpiresAt), action.connection.owner_valid_until.getTime(), Date.parse(action.connection.policy.expiresAt)) }))) } + catch (error) { if (!(error instanceof McpError) || error.code !== "APPROVAL_REQUIRED") throw error } + } + const originalEffectSelection = await value.context.captureOriginalEffectSelection(String(input.candidateId)) + const sealed: Record = { ...request, originalEffectSelection, + ...(!persistent && !profile.autoRecovery && originalEffectSelection.status === "prepared" ? { manualEffectPolicy: "observational-v1" } : {}), + nativeId: owned.nativeId, outIface, contextSources: value.context.before.sourceProofs, dependencyProof: value.context.before.dependencyProof, + originalTargetProof: value.context.before.targetRuleProof, desiredTargetProof: owned.desiredTargetProof(compiled.compiledBody), receiptId: randomUUID(), proofRevision: persistent ? 3 : 2, + originalCandidateId: value.context.before.currentCandidateId, originalRuleCiphertext: await encryptSecret(String(owned.rawRow.rule)), desiredRuleCiphertext: await encryptSecret(compiled.compiledBody), managementBindingCiphertext, + rules: [publicRule(compiled.proposed)], beforeRules: [publicRule(compiled.before)] } + if (persistent) sealed.persistentProfileAuthority = await resolveRoutingProfileAuthority({ profileId: persistent.id, expectedVersion: persistent.version, + profile, principal: action.principal, ownerId: action.connection.owner_id, transport: await scopedProfileAuthorityTransport(sealed), signal: action.signal }) + sealed.requestProof = await privateSecretFingerprint(fingerprint(sealed), `route-ai-scoped-request-v${sealed.proofRevision}`) + await value.context.fence() + await access(sealed, action.signal) + assertFresh() + const authorityValidUntil = persistent ? routingProfileAuthorityBindingSchema.parse(sealed.persistentProfileAuthority).packet.validUntil : undefined + const validUntil = authorityValidUntil && !profile.autoRecovery + ? new Date(Math.min(Date.parse(authorityValidUntil), checkpoint.validUntil)).toISOString() : authorityValidUntil + return { target: { scope: "servers", serverIds: scopedProfileIds(value.profile) }, objects: [{ kind: "routing_filter", serverId: value.profile.serverId, id: owned.nativeId }, ...scopedProfileIds(value.profile).map(serverId => ({ kind: "server" as const, serverId, id: String(serverId) }))], snapshots: [], before: sealed.beforeRules, + steps: [{ kind: "service", service: "routing.community.scoped", input: sealed }], requiresHuman: true, mutationCost: 2, verificationScope: "configuration", + ...(validUntil ? { validUntil } : {}), + warnings: ["Одна запись owned фильтра; SQL метаданные фильтров сохраняются.", "SLA и свежая preflight-проба обязательны до отправки PATCH. После записи подтверждение конфигурации сохраняется независимо от новых SLA-измерений; контрольная проба и её результат проверяются отдельно.", "Подтверждается конфигурация и control probe; сквозной и обратный трафик требуют отдельной приёмки.", managementBindingCiphertext ? "Заранее закреплён независимый транспорт для отдельного обратного плана; автоматический inverse не включён." : "Независимый transport не закреплён; отдельный обратный план потребует доступного primary.", "Неизвестный результат не повторяется; сверка только читает состояние."] } +} + +export function registerScopedCommunityService() { + registerMcpService("routing.community.scoped", { + current: async input => { await original(input, false); return input.beforeRules }, + execute: async (input, context) => withApplicationMutationLock(() => withConfigMutationLock(scopedProfileIds(routeAiProfileSchema.parse(input.profile)), async () => { + await sealedInput(input) + if (input.proofRevision === 3 && routeAiProfileSchema.parse(input.profile).autoRecovery) { + if (context?.phase !== "forward" || !context.routingReservationForward) throw fail("Нет подтверждённого резервирования forward attempt") + await createRoutingReservationStore().beginForwardAttempt(context.routingReservationForward, { operationId: context.operationId, stepIndex: context.stepIndex, input }) + } + await original(input, true) + if (await receiptExists(input, "routing.scoped.attempt")) throw fail("Повтор PATCH запрещён", "MUTATION_OUTCOME_UNKNOWN") + const requiresEffectBaseline = input.proofRevision === 3 && !routeAiProfileSchema.parse(input.profile).autoRecovery + let attemptRecorded = !requiresEffectBaseline + if (attemptRecorded) await receipt(input, "routing.scoped.attempt") + const before = await original(input, true), owned = before.context.ownedTarget! + let effectSession: Awaited> | undefined + let retainedNetworkSession = false + let effectBaseline: Parameters[1] | undefined + let originalEffects: OriginalEffectReport | undefined + const effectSelection = originalEffectSelectionSchema.safeParse(input.originalEffectSelection) + if (effectSelection.success) originalEffects = unknownOriginalEffects(effectSelection.data.status === "unknown" ? effectSelection.data.reason : "effect-acquisition-unavailable") + try { + // The persistent controller needs a baseline it can advance after the + // write. Manual plans still treat the effect vector as observational. + if (context?.phase === "forward" && (input.proofRevision === 3 || input.proofRevision === 2 && input.manualEffectPolicy === "observational-v1") && effectSelection.success && effectSelection.data.status === "prepared") { + try { + const selected = effectSelection.data + effectSession = await createRoutingNetworkSessionForCurrentExecution({ servers: scopedProfileIds(before.profile), + probes: [{ key: selected.probe.key, serverId: selected.probe.serverId, destination: selected.probe.destination, + source: selected.probe.source, interface: selected.probe.interface, probeVrf: selected.probe.probeVrf }] }, context.routingReservationForward) + effectBaseline = await effectSession.readSnapshot("before") + if (!originalEffectBaselineMatches(selected, effectBaseline)) { + effectSession.close(); effectSession = undefined; originalEffects = unknownOriginalEffects("prepared-effect-baseline-drift") + } + } catch { effectSession?.close(); effectSession = undefined } + } + if (requiresEffectBaseline) { + if (!effectSession) throw fail("Исходное состояние изменяемого префикса не подтверждено", "MEASUREMENT_UNKNOWN") + await receipt(input, "routing.scoped.attempt") + attemptRecorded = true + } + await executeGuardedConfigurationWrite(before.profile.serverId, "PATCH", "/routing/filter/rule/" + owned.nativeId, async signal => { + // This runs AFTER the durable configuration.intent, immediately before transport. + const fresh = await original(input, true); await before.context.fence() + signal?.throwIfAborted() + await patchScopedCommunity(owned.endpoint, owned.nativeId, fresh.compiledBody, signal) + }) + const after = await observe(input, undefined, true, undefined, "configuration") + if (!await matchesDesired(input, after.context)) throw fail("Read-back не подтверждён") + await after.context.fence() + // This commit precedes the first post-change network probe. A failed + // probe cannot erase the exact, acknowledged configuration read-back. + const configuration = await saveScopedConfigurationCheckpoint(input, await sealedInput(input)) + const check = await checkCandidatePath(after.profile, after.candidate.id, principalSchema.parse(input.principal).connectionId, { authorize: after.authorize }) + if (check.status === "unknown") throw fail("Control probe неизвестен", "MUTATION_OUTCOME_UNKNOWN") + if (effectSession && effectBaseline && effectSelection.success && effectSelection.data.status === "prepared") { + let roundContext: Awaited> | undefined + try { + const principal = principalSchema.parse(input.principal), action = { principal, connection: await effectiveMcpConnection(principal) } + roundContext = await resolveRoutingNetworkForwardRoundConfigurationContext(effectSession.sharedContext, action) + const first = await effectSession.readConfigurationBoundForwardSnapshot(roundContext) + const second = await effectSession.readConfigurationBoundForwardSnapshot(roundContext) + const { sameConfigurationBoundForwardRounds } = await import("./routing-network-native-session.js") + originalEffects = first.configurationReadbacks.before.checkpointProof === configuration.checkpointProof && + sameConfigurationBoundForwardRounds(effectBaseline, [first, second]) + ? await inspectOriginalEffectRounds(effectSelection.data, [first, second], effectBaseline) + : unknownOriginalEffects("configuration-bound-rounds-unconfirmed") + retainedNetworkSession = originalEffects.status !== "unknown" + } catch (error) { originalEffects = effectReadFailure("original-effect-rounds-unavailable", error) } + finally { if (roundContext) closeRoutingNetworkForwardRoundConfigurationContext(roundContext) } + } + // Publication revalidation uses the existing native owner's fixed + // context reader and therefore its SAME counters, without a snapshot. + let sharedPublication = false + if (effectSession && !effectSession.readBudget().closed) { + try { + const published = await readRoutingNetworkScopedCommunityContext(effectSession.sharedContext) + if (!await matchesDesired(input, published)) throw fail("Состояние изменилось после effect-check") + await published.fence() + sharedPublication = true + } catch (error) { originalEffects = effectReadFailure("effect-publication-revalidation-unavailable", error) } + } + if (!sharedPublication) { + // This is the OLD manual final read, relocated after optional work, + // never duplicated or counted as a completed effect-owner read. + // Closed/exhausted effect owners cannot waive this fresh guard. + const final = await observe(input, undefined, true, undefined, "configuration") + if (!await matchesDesired(input, final.context)) throw fail("Состояние изменилось после probe") + await final.context.fence() + } + if (effectSession && originalEffects) { + const { deadline, calls, bytes, rows } = effectSession.readBudget() + const budget = originalEffectReportSchema.shape.budget.safeParse({ deadline, calls, bytes, rows }) + if (budget.success) originalEffects = { ...originalEffects, budget: budget.data } + } + await saveScopedNetworkCheckpoint(input, await sealedInput(input), { ...check, status: check.status }, configuration.checkpointProof, originalEffects) + const result = await verifiedResult(input) + if (effectSession && retainedNetworkSession && sharedPublication && context?.phase === "forward" && + context.routingReservationForward && context.retainRoutingNetworkSession && !effectSession.readBudget().closed) { + context.retainRoutingNetworkSession(effectSession) + effectSession = undefined + } + return result + } catch (error) { + if (!attemptRecorded) throw error + throw fail("Результат переключения не подтверждён; повтор запрещён, требуется сверка", "MUTATION_OUTCOME_UNKNOWN") + } + finally { effectSession?.close() } + })), + verify: async (input, result) => { + const expected = await verifiedResult(input) + if (!expected.configurationCheckpointAvailable || !expected.controlProbeVerified || fingerprint(result) !== fingerprint(expected)) return false + return matchesDesired(input, (await observe(input, undefined, false, undefined, "configuration")).context) + }, + reconcile: async (input, authorize) => { + await sealedInput(input); await authorize() + const observed = await observe(input, undefined, false, authorize, "configuration") + const config = { ...matches(input, observed.context), configurationMatchesDesired: await matchesDesired(input, observed.context) } + await observed.context.fence(); await authorize() + const expected = await verifiedResult(input) + const checkpoints = (await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.state='verified' AND s.intended_state=$2::jsonb AND s.result=$3::jsonb LIMIT 2", [principalSchema.parse(input.principal).connectionId, JSON.stringify(redact({ kind: "service", service: "routing.community.scoped", input })), JSON.stringify(expected)])).rows + return { ...config, configurationCheckpointAvailable: expected.configurationCheckpointAvailable, controlProbeVerified: expected.controlProbeVerified, matchesOriginalState: config.configurationMatchesOriginal, matchesDesiredState: config.configurationMatchesDesired && expected.configurationCheckpointAvailable && expected.controlProbeVerified && checkpoints.length === 1 } + }, + }) +} + +async function verifiedResult(input: Record) { + await assertPersistentScopedProfileAuthority(input) + const checkpoint = await loadScopedConfigurationCheckpoint(input, await sealedInput(input)) + const network = await loadScopedNetworkCheckpoint(input, await sealedInput(input)) + const result = { outcome: network?.status === "passed" ? "applied" : "configuration-applied-network-unverified", nativeWrites: 1, forwardingVerified: false, configurationVerified: true, receiptId: input.receiptId, proofRevision: input.proofRevision, + configurationCheckpointAvailable: !!checkpoint, configurationCheckpointProof: checkpoint?.checkpointProof ?? null, controlProbeVerified: network?.status === "passed", postProbeStatus: network?.status ?? "unknown", networkCheckpointProof: network?.networkSeal ?? null, + ...(network?.originalEffects === undefined ? {} : { originalEffects: network.originalEffects }), + nativeVerificationSeal: await privateSecretFingerprint(fingerprint({ request: await sealedInput(input), dependency: input.dependencyProof, desired: input.desiredTargetProof, candidate: input.candidateId, configuration: checkpoint?.checkpointProof ?? null, network: network?.networkSeal ?? null }), `route-ai-scoped-verified-v${input.proofRevision}`) } + await assertPersistentScopedProfileAuthority(input) + return result +} diff --git a/backend/src/modules/mcp/routing-controller-active-writer.test.ts b/backend/src/modules/mcp/routing-controller-active-writer.test.ts new file mode 100644 index 00000000..f88a4df7 --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-active-writer.test.ts @@ -0,0 +1,89 @@ +import assert from "node:assert/strict" +import test from "node:test" +import { readFile } from "node:fs/promises" +import ts from "typescript" +import { fingerprint, McpError } from "./security.js" + +/** Execute the actual native guard body. Only authority/SQL adapters are inert; + * these cases test identity comparison semantics, not signed inverse integration. */ +async function fixture() { + const enrolled: Record = { nativeId: "*1", candidateId: "a", targetProof: "target-a", dependencyProof: "dep", sourceProofs: [{ serverId: 1, proof: "source" }] } + const authority = { packet: { profileId: "profile", profileVersion: 1, connectionId: "connection" } } + const input: Record = { profile: { autoRecovery: false }, persistentProfileAuthority: authority, nativeId: "*1", originalCandidateId: "a", originalTargetProof: "target-a", dependencyProof: "dep", contextSources: enrolled.sourceProofs } + const prepared: Record = { ...input, desiredCandidateId: "b" } + const packet: Record = { nativeId: "*1", originalCandidateId: "a", desiredCandidateId: "b", originalTargetProof: "target-a", desiredTargetProof: "target-b", dependencyProof: "dep", contextSources: enrolled.sourceProofs } + const source = { row: { id: "source-op", source_plan_id: "source-plan" }, input, configuration: { checkpointProof: "configuration-proof", packet }, sourceProof: "source-proof" } + const row = { id: "profile", version: 1, connection_id: "connection", grant_expires_at: new Date(Date.now() + 600000), config: {}, state: { baseline: { proof: "inert-authenticated-baseline" } } } + const metadata = { operationName: "routing.community.switch", operationVersion: 7, serviceName: "routing.community.scoped", phase: "forward", stepIndex: 0, ownerId: "owner", serviceInput: input, principal: {} } + let nativeWrites = 0 + const providers: Record = { + "./routing-reservation-store.js": { assertPersistentRoutingForwardTransportCharge: async () => {} }, + "./routing-main-transition.js": { loadRoutingMainTransitionPreparation: async () => prepared }, + "./routing-community-scoped-request.js": { assertScopedCommunityRequest: async () => {} }, + "@mmapp/contracts/mcp-routing": { routeAiProfileSchema: { parse: (value: unknown) => value } }, + "../../db/index.js": { pool: { query: async () => ({ rows: [row] }) } }, + "../../services/config-mutation-policy.js": { captureVerifiedMcpServiceExecution: async () => metadata, inspectVerifiedMcpServiceExecution: async () => metadata }, + "./store.js": { effectiveMcpConnection: async () => ({}) }, + "./routing-profile-authority.js": { routingProfileAuthorityBindingSchema: { parse: (value: unknown) => { assert.ok(value); return value } }, assertPersistentScopedProfileAuthority: async () => {} }, + "./routing-community-scoped-recovery-source.js": { loadScopedRecoverySource: async () => source }, + "./routing-controller-baseline.js": { routingControllerBaselinePrincipal: async () => ({}), authenticateRoutingControllerBaseline: async () => ({ value: { packet: enrolled } }) }, + "./security.js": { fingerprint, McpError }, + } + const exports: { assertActiveRoutingProfileWriter?: (serverId: number) => Promise } = {} + const body = await readFile(new URL("./routing-controller-active-writer.ts", import.meta.url), "utf8") + const compiled = ts.transpileModule(body, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText + new Function("exports", "require", compiled)(exports, (name: string) => { assert.ok(providers[name], name); return providers[name] }) + assert.ok(exports.assertActiveRoutingProfileWriter) + const guard = exports.assertActiveRoutingProfileWriter + return { + enrolled, input, prepared, source, metadata, + run: async () => { await guard(1); nativeWrites++ }, + writes: () => nativeWrites, + recovery: () => { + metadata.operationName = "operations.recover"; metadata.serviceName = "routing.community.scoped.recover" + metadata.serviceInput = { sourceOperationId: "source-op", sourceProof: source.sourceProof, configurationProof: source.configuration.checkpointProof, sourceReceiptId: source.input.receiptId } + }, + } +} + +test("scoped original enrolled identity is admitted", async () => { const value = await fixture(); await value.run(); assert.equal(value.writes(), 1) }) +const drifts: Array<[string, string, unknown]> = [ + ["native", "nativeId", "*other"], ["candidate", "originalCandidateId", "c"], ["target", "originalTargetProof", "other-target"], + ["dependency", "dependencyProof", "other-dep"], ["sources", "contextSources", [{ serverId: 1, proof: "other-source" }]], +] +for (const [name, key, bad] of drifts) test(`scoped ${name} drift is refused before native`, async () => { const value = await fixture(); value.input[key] = bad; await assert.rejects(value.run); assert.equal(value.writes(), 0) }) +test("main fixed loaded original identity is admitted", async () => { const value = await fixture(); value.metadata.serviceName = "routing.community"; await value.run() }) +for (const [name, key, bad] of drifts) test(`main decrypted ${name} drift is refused before native`, async () => { const value = await fixture(); value.metadata.serviceName = "routing.community"; value.prepared[key] = bad; await assert.rejects(value.run); assert.equal(value.writes(), 0) }) +test("inverse with enrolled A awaiting its own B transition is admitted", async () => { const value = await fixture(); value.recovery(); await value.run() }) +const sourceTransition = { operationId: "source-op", planId: "source-plan", checkpointProof: "configuration-proof" } +test("inverse from this exact source advanced B baseline is admitted", async () => { const value = await fixture(); value.recovery(); Object.assign(value.enrolled, { candidateId: "b", targetProof: "target-b", transition: sourceTransition }); await value.run() }) +test("inverse from this source can bind a separately authenticated B readback seal", async () => { + const value = await fixture() + value.recovery() + Object.assign(value.enrolled, { candidateId: "b", targetProof: "observed-b", transition: { ...sourceTransition, sourceDesiredTargetProof: "target-b" } }) + await value.run() + assert.equal(value.writes(), 1) +}) +for (const [key, wrong] of Object.entries({ operationId: "other-source", planId: "other-plan", checkpointProof: "other-checkpoint", sourceDesiredTargetProof: "other-desired" })) { + test(`inverse from observed B refuses mismatched ${key}`, async () => { + const value = await fixture() + value.recovery() + Object.assign(value.enrolled, { candidateId: "b", targetProof: "observed-b", transition: { ...sourceTransition, sourceDesiredTargetProof: "target-b", [key]: wrong } }) + await assert.rejects(value.run) + assert.equal(value.writes(), 0) + }) +} +test("new forward source must match the enrolled observed seal", async () => { + const value = await fixture() + Object.assign(value.enrolled, { candidateId: "b", targetProof: "observed-b", transition: { ...sourceTransition, sourceDesiredTargetProof: "target-b" } }) + Object.assign(value.input, { originalCandidateId: "b", originalTargetProof: "target-b" }) + await assert.rejects(value.run) + assert.equal(value.writes(), 0) + value.input.originalTargetProof = "observed-b" + await value.run() +}) +test("inverse with B but no authenticated transition is refused", async () => { const value = await fixture(); value.recovery(); Object.assign(value.enrolled, { candidateId: "b", targetProof: "target-b" }); await assert.rejects(value.run) }) +test("inverse with another configuration checkpoint is refused", async () => { const value = await fixture(); value.recovery(); Object.assign(value.enrolled, { candidateId: "b", targetProof: "target-b", transition: { ...sourceTransition, checkpointProof: "other-checkpoint" } }); await assert.rejects(value.run) }) +test("inverse with external C baseline is refused", async () => { const value = await fixture(); value.recovery(); value.enrolled.candidateId = "c"; await assert.rejects(value.run) }) +test("inverse with another authenticated source identity is refused", async () => { const value = await fixture(); value.recovery(); value.metadata.serviceInput.sourceProof = "other"; await assert.rejects(value.run) }) +test("inverse with source dependency drift is refused", async () => { const value = await fixture(); value.recovery(); value.source.configuration.packet.dependencyProof = "other"; await assert.rejects(value.run) }) diff --git a/backend/src/modules/mcp/routing-controller-active-writer.ts b/backend/src/modules/mcp/routing-controller-active-writer.ts new file mode 100644 index 00000000..2bd11e60 --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-active-writer.ts @@ -0,0 +1,85 @@ +import { loadRoutingMainTransitionPreparation } from "./routing-main-transition.js" +import { assertScopedCommunityRequest } from "./routing-community-scoped-request.js" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { routingProfileAuthorityBindingSchema, assertPersistentScopedProfileAuthority } from "./routing-profile-authority.js" +import { loadScopedRecoverySource } from "./routing-community-scoped-recovery-source.js" +import { authenticateRoutingControllerBaseline, routingControllerBaselinePrincipal } from "./routing-controller-baseline.js" +import { fingerprint, McpError } from "./security.js" +import { assertPersistentRoutingForwardTransportCharge } from "./routing-reservation-store.js" + +/** Current native boundary only. Called under existing mm-config server locks. + * This denies an unrelated REST/MCP native mutation; it creates no debit, + * dispatch capability, READY, enrollment, inverse or log coverage proof. */ +export async function assertActiveRoutingProfileWriter(serverId:number) { + const deny=()=>new McpError("STATE_CONFLICT","Активный routing profile требует исходного проверенного шага; приостановите профиль перед ручным изменением",409) + if(!Number.isSafeInteger(serverId)||serverId<=0)throw deny() + const rows=(await pool.query(`SELECT id,version,connection_id,grant_expires_at, + CASE WHEN octet_length(config::text)<=262144 THEN config END AS config, + CASE WHEN octet_length(state::text)<=131072 THEN state END AS state, + (config IS NULL OR state IS NULL OR octet_length(config::text)>262144 OR octet_length(state::text)>131072) AS source_bounds_unavailable + FROM mcp_routing_profiles WHERE enabled AND (config->>'serverId'=$1 OR EXISTS( + SELECT 1 FROM jsonb_array_elements(config->'candidates') AS candidate + WHERE candidate->>'jumpHostId'=$1 OR candidate->>'exitNodeId'=$1)) ORDER BY id LIMIT 2`,[String(serverId)])).rows + // Distinct profile IDs cannot share the one original persistent authority + // below. Two rows therefore prove ambiguity; never hydrate another999 rows. + // At most2 * (256KiB config +128KiB state) leave SQL, plus fixed metadata. + if(rows.length>1||rows.some(row=>row.source_bounds_unavailable===true))throw deny() + if(!rows.length)return + let origin + try { origin=await captureVerifiedMcpServiceExecution() } catch { throw deny() } + const metadata=await inspectVerifiedMcpServiceExecution(origin) + if(metadata.phase!=="forward"||metadata.stepIndex!==0)throw deny() + let input:Record, recoverySource:Awaited>|undefined + if(metadata.operationName==="routing.community.switch"&&metadata.operationVersion===7&&["routing.community.scoped","routing.community"].includes(metadata.serviceName))input=metadata.serviceInput + else if(metadata.operationName==="operations.recover"&&metadata.serviceName==="routing.community.scoped.recover"){ + const principal=metadata.principal,connection=await effectiveMcpConnection(principal) + const source=await loadScopedRecoverySource(String(metadata.serviceInput.sourceOperationId),{principal,connection}) + recoverySource=source + if(metadata.serviceInput.sourceProof!==source.sourceProof || metadata.serviceInput.configurationProof!==source.configuration.checkpointProof || + metadata.serviceInput.sourceReceiptId!==source.input.receiptId)throw deny() + input=source.input + }else throw deny() + const authority=routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority) + await assertPersistentScopedProfileAuthority(input) + for(const row of rows){ + if(row.id!==authority.packet.profileId||row.version!==authority.packet.profileVersion||row.connection_id!==authority.packet.connectionId||!(row.grant_expires_at instanceof Date))throw deny() + const principal=await routingControllerBaselinePrincipal(row.state?.baseline),connection=await effectiveMcpConnection(principal) + const baseline=await authenticateRoutingControllerBaseline(row.state?.baseline,row.id,row.version,routeAiProfileSchema.parse(row.config),row.grant_expires_at.toISOString(),{principal,connection}) + const enrolled=baseline.value.packet + const matches=(identity:{nativeId:unknown;candidateId:unknown;targetProof:unknown;dependencyProof:unknown;sourceProofs:unknown}, targetProof=enrolled.targetProof) => + identity.nativeId===enrolled.nativeId && identity.candidateId===enrolled.candidateId && identity.targetProof===targetProof && + identity.dependencyProof===enrolled.dependencyProof && fingerprint(identity.sourceProofs)===fingerprint(enrolled.sourceProofs) + if(metadata.serviceName==="routing.community"){ + const prepared=await loadRoutingMainTransitionPreparation(input) + if(!matches({nativeId:prepared.nativeId,candidateId:prepared.originalCandidateId,targetProof:prepared.originalTargetProof, + dependencyProof:prepared.dependencyProof,sourceProofs:prepared.contextSources}))throw deny() + }else if(!recoverySource){ + await assertScopedCommunityRequest(input) + if(!matches({nativeId:input.nativeId,candidateId:input.originalCandidateId,targetProof:input.originalTargetProof, + dependencyProof:input.dependencyProof,sourceProofs:input.contextSources}))throw deny() + }else{ + // The inverse writes B->A. It is admitted either while the ORIGINAL A + // baseline awaits its own B transition, or from the EXACT signed B + // baseline advanced by this very source operation/checkpoint. Never + // compare inverse before-state B naively to forward original-state A. + const packet=recoverySource.configuration.packet + const common={nativeId:packet.nativeId,dependencyProof:packet.dependencyProof,sourceProofs:packet.contextSources} + const original=matches({...common,candidateId:packet.originalCandidateId,targetProof:packet.originalTargetProof}) + const transition=enrolled.transition + const ownDesired=!!transition && transition.operationId===recoverySource.row.id && transition.planId===recoverySource.row.source_plan_id && + transition.checkpointProof===recoverySource.configuration.checkpointProof && + matches({...common,candidateId:packet.desiredCandidateId,targetProof:packet.desiredTargetProof},transition.sourceDesiredTargetProof??enrolled.targetProof) + if(!original&&!ownDesired)throw deny() + } + } + // The live origin and baseline cannot replace a durable admission charge. + // Existing reserved forward capability and manual inverse stamp paths remain + // separate; this check grants nothing and never infers a legacy exemption. + if (!recoverySource && !routeAiProfileSchema.parse(input.profile).autoRecovery) { + await assertPersistentRoutingForwardTransportCharge(pool, metadata, metadata.ownerId) + } + await inspectVerifiedMcpServiceExecution(origin) +} diff --git a/backend/src/modules/mcp/routing-controller-baseline-acceptance.ts b/backend/src/modules/mcp/routing-controller-baseline-acceptance.ts new file mode 100644 index 00000000..4d9f842e --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-baseline-acceptance.ts @@ -0,0 +1,401 @@ +import { prepareRoutingControllerBaseline, advanceRoutingControllerBaseline } from "./routing-controller-baseline.js" +import { saveControllerProfileCas, publishControllerState } from "./routing-controller-fence.js" +import { withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { routerClient } from "./resources.js" +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { managedComment } from "../../managed-markers.js" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool } from "./runtime.js" +import { requireRecentPath } from "./path-verification.js" +import { attestRoutingFixture } from "../../test/mcp-routing-fixture.js" +import { approvePlan, applyPlan, loadPlan, publicPlan } from "./runtime.js" +import { tickRoutingProfiles } from "./routing-profiles.js" +import { assertIdentifiedRoutingCompletion } from "./routing-reservation-store.js" + +/** Diagnostic paths only; never print a matching private value or native row. */ +function privateMarkerPaths(value: unknown, path = "$", found: string[] = []): string[] { + if (typeof value === "string" && value.includes("NEVER_")) found.push(path) + else if (Array.isArray(value)) value.forEach((item, index) => privateMarkerPaths(item, path + "[" + index + "]", found)) + else if (value && typeof value === "object") for (const [key, item] of Object.entries(value)) privateMarkerPaths(item, path + "." + key, found) + return found +} + +/** Hook for the disposable SQL+HTTP integration suite; no real router writes. */ +export async function acceptMainBaselineRuntime(actor: AuthUser) { + const sid = 130 + const writerActor: AuthUser = { ...actor, permissions: [...new Set([...actor.permissions, "mm:filters:write"])] } + let writes = 0, requests = 0, secondConnection = false, counter = 0 + let afterRead: (() => Promise) | undefined + let table = "policy-only" + const inputChain = "bgp-in" + let inherited = false, parentChain = "bgp-in", oversized = false, afi = "ip", interfaceList = false + let separateProbeVrf = false, bgpVrfDisabled = false + const route: Record = { ".id": "*r", "dst-address": "192.0.2.1/32", "routing-table": "main", disabled: "false", comment: managedComment("recursive"), opaque: "NEVER_ROUTE_CONTEXT_NATIVE" } + const body = compileManagedFilterRules([{ community: "65000:10", gateway: "192.0.2.1", outIface: "", action: "route", description: "" }]) + let policyBody = body, changePolicyAfterTrace = false + let policyComment = managedComment("filter"), mainWriteEnabled = false + const reserveRoute = { ...route, ".id": "*r2", "dst-address": "192.0.2.9/32" } + const connection = () => ({ ".id": "*c", name: "peer", disabled: "false", instance: "default", vrf: "main", "routing-table": table, afi, ...(!inherited ? { "input.filter": inputChain } : {}), "output.filter-chain": "", templates: inherited ? "parent" : "" }) + const collections = (path: string): Record[] => ({ + "/routing/table": [{ ".id": "*tm", name: "main", fib: "true", dynamic: "true" }, { ".id": "*tp", name: "policy-only", fib: "true" }, ...(separateProbeVrf ? [{ ".id": "*td", name: "probe-only", fib: "true", dynamic: "true" }] : [])], + "/ip/vrf": [...(interfaceList ? [{ ".id": "*vl", name: "listed", interfaces: "WAN-LIST", disabled: "false" }] : []), ...(separateProbeVrf ? [{ ".id": "*vd", name: "probe-only", interfaces: "wan", disabled: "false" }] : []), { ".id": "*v", name: "main", interfaces: "all", disabled: String(bgpVrfDisabled) }], + "/routing/bgp/connection": [connection(), ...(secondConnection ? [{ ...connection(), ".id": "*c2", "routing-table": "main" }] : [])], + "/routing/bgp/instance": [{ ".id": "*i", name: "default", vrf: "main", "routing-table": "main", disabled: "false" }], + "/routing/bgp/template": inherited ? [{ ".id": "*t", name: "parent", disabled: "false", "input.filter": parentChain }] : [], + "/routing/filter/rule": [{ ".id": "*f", chain: "bgp-in", disabled: "false", rule: policyBody, comment: policyComment, opaque: "NEVER_ROUTE_CONTEXT_TARGET_OPAQUE" }], + "/ip/route": mainWriteEnabled ? [route, reserveRoute] : [route], + "/interface": ["wan", "hop", "target"].map((name, index) => ({ ".id": "*if" + index, name, disabled: "false", running: "true", "rx-byte": String(++counter) })), + "/ip/address": ["wan", "hop", "target"].map((name, index) => ({ ".id": "*a" + index, interface: name, address: "192.0.2." + (index + 1) + "/32", disabled: "false", invalid: "false" })), + } as Record[]>)[path] ?? [] + const probeVrfs: string[] = [] + const router = createServer(async (req, res) => { + requests++; res.setHeader("content-type", "application/json") + let raw = ""; for await (const chunk of req) raw += chunk + const args = raw ? JSON.parse(raw) : {}, path = (req.url ?? "").replace(/^\/rest/, "") + if (afterRead) { const hook = afterRead; afterRead = undefined; await hook() } + if (oversized) { res.end(JSON.stringify({ value: "я".repeat(600000) })); return } + if (mainWriteEnabled && req.method === "PATCH" && path === "/routing/filter/rule/*f") { + assert.equal(args.chain, "bgp-in"); assert.equal(args.disabled, "no") + writes++; policyBody = args.rule; policyComment = args.comment + res.end(JSON.stringify(collections("/routing/filter/rule")[0])); return + } + if (req.method === "GET") { res.end(JSON.stringify(collections(path))); return } + if (req.method === "POST" && path.endsWith("/get")) { + const row = collections(path.slice(0, -4)).find(row => row[".id"] === args[".id"]), value = row?.[args["value-name"]] + res.end(JSON.stringify(value === undefined ? [] : { ret: value })); return + } + if (req.method === "POST" && path === "/tool/ping") { probeVrfs.push(args.vrf); res.end(JSON.stringify([{ sent: "3", received: "3", "packet-loss": "0", "avg-rtt": "1ms" }])); return } + if (req.method === "POST" && path === "/tool/traceroute") { + probeVrfs.push(args.vrf) + if (changePolicyAfterTrace) { changePolicyAfterTrace = false; policyBody = compileManagedFilterRules([{ community: "65000:10", gateway: "192.0.2.9", outIface: "", action: "route", description: "" }]) } + res.end(JSON.stringify(["192.0.2.2", "192.0.2.3"].map(address => ({ address, sent: "1", loss: "0" })))); return + } + writes++; res.statusCode = 405; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + let connectionId: string | undefined, writeConnectionId: string | undefined + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'route-context','127.0.0.1',$2,false,'NEVER_ROUTE_CONTEXT_PASSWORD')", [sid, (router.address() as { port: number }).port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Route context", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [sid], operationIds: ["routing.analyze", "routing.path.verify"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const base = routeAiProfileSchema.parse({ name: "table is not VRF", serverId: sid, community: "65000:10", family: "ipv4", table: "policy-only", probeVrf: "main", bgpContext: { connectionId: "*c", instance: "default", inputChain: "bgp-in" }, candidates: [{ id: "a", jumpHostId: sid, exitNodeId: sid, wanInterface: "wan", downstreamInterface: "hop", gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*r" }, controlProbe: { targetInterface: "target", expectedHops: [{ serverId: sid, interface: "hop" }, { serverId: sid, interface: "target" }] } }] }) + const analyze = async (profile = base) => await invokeMcpTool(principal, "mm_route_optimizer_analyze", { profile }) as { currentCandidateId: string | null; executionAvailable: boolean; contextVerified: boolean; sharedConnectionIds: string[]; bgpVrf: string | null; probeVrf: string } + const result = await analyze() + assert.equal(result.contextVerified, true); assert.equal(result.currentCandidateId, "a"); assert.equal(result.executionAvailable, false) + assert.ok(!JSON.stringify(result).includes("NEVER_")) + route["dst-address"] = "192.0.2.1/24" + assert.equal((await analyze()).currentCandidateId, null, "a covering route is not the registered recursive host route") + route["dst-address"] = "192.0.2.1/32" + afi = "ipv6" + assert.equal((await analyze()).currentCandidateId, null, "the selected BGP binding must carry the requested address family") + afi = "ip"; interfaceList = true + const unavailableProbe = await analyze() as Awaited> & { candidateDiagnostics: Array<{ candidateId: string; status: string }> } + assert.equal(unavailableProbe.candidateDiagnostics[0].status, "candidate-unavailable", "unknown earlier VRF interface-list membership cannot fall through to main all") + interfaceList = false + inherited = true + assert.equal((await analyze()).currentCandidateId, "a", "explicit binding may be inherited from a uniquely resolved template") + parentChain = "other" + assert.equal((await analyze()).currentCandidateId, null, "different inherited input-chain is not the selected profile") + parentChain = "bgp-in"; inherited = false; secondConnection = true + assert.deepEqual((await analyze()).sharedConnectionIds.sort(), ["*c", "*c2"]) + secondConnection = false + const wrong = routeAiProfileSchema.parse({ ...base, bgpContext: { ...base.bgpContext, instance: "other" } }) + assert.equal((await analyze(wrong)).currentCandidateId, null) + assert.equal((await analyze(routeAiProfileSchema.parse({ ...base, table: "main" }))).currentCandidateId, null) + const duplicate = routeAiProfileSchema.parse({ ...base, candidates: [...base.candidates, { ...base.candidates[0], id: "same-gateway" }] }) + assert.equal((await analyze(duplicate)).currentCandidateId, null, "same gateway does not identify a unique candidate") + const check = async (profile = base) => await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "a" }) as { id: string; status: string; returnPathVerified: boolean } + separateProbeVrf = true + const distinct = routeAiProfileSchema.parse({ ...base, probeVrf: "probe-only", bgpContext: { ...base.bgpContext, vrf: "main" } }) + const separated = await analyze(distinct) + assert.equal(separated.contextVerified, true); assert.equal(separated.currentCandidateId, "a"); assert.equal(separated.executionAvailable, false) + assert.equal(separated.bgpVrf, "main"); assert.equal(separated.probeVrf, "probe-only") + assert.equal((await analyze(routeAiProfileSchema.parse({ ...distinct, bgpContext: { ...distinct.bgpContext, vrf: "probe-only" } }))).currentCandidateId, null, "wrong explicit BGP transport VRF cannot match merely because the probe uses it") + const distinctCheck = await check(distinct) + assert.equal(distinctCheck.status, "passed"); assert.equal(distinctCheck.returnPathVerified, false) + assert.deepEqual(probeVrfs, ["probe-only", "probe-only"]) + assert.equal(await requireRecentPath(distinct, "a", connectionId), distinctCheck.id) + bgpVrfDisabled = true + await assert.rejects(analyze(distinct), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE", "BGP transport VRF is checked independently of the enabled probe VRF") + bgpVrfDisabled = false; separateProbeVrf = false; probeVrfs.length = 0 + changePolicyAfterTrace = true + assert.equal((await check()).status, "unknown", "An owned policy change during the probe cannot retain passed status") + policyBody = body; probeVrfs.length = 0 + const passed = await check() + assert.equal(passed.status, "passed"); assert.equal(passed.returnPathVerified, false); assert.deepEqual(probeVrfs, ["main", "main"]) + assert.equal(await requireRecentPath(base, "a", connectionId), passed.id) + route.opaque = "changed dependency" + await assert.rejects(requireRecentPath(base, "a", connectionId), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN") + route.opaque = "NEVER_ROUTE_CONTEXT_NATIVE" + await pool.query("UPDATE mcp_path_checks SET evidence=jsonb_set(evidence,'{status}','\"failed\"') WHERE id=$1", [passed.id]) + await assert.rejects(requireRecentPath(base, "a", connectionId)) + await check() + const beforeEndpoint = requests + await pool.query("UPDATE servers SET password='NEVER_CHANGED_PASSWORD' WHERE id=$1", [sid]) + await assert.rejects(requireRecentPath(base, "a", connectionId), (error: { code?: string }) => error.code === "STATE_CONFLICT") + assert.equal(requests, beforeEndpoint, "prior source proof rejects changed endpoint before any native request") + oversized = true + await assert.rejects(analyze(), (error: Error & { code?: string }) => ["LIMIT_EXCEEDED", "STATE_UNAVAILABLE"].includes(error.code ?? "") && !error.message.includes("я")) + oversized = false + afterRead = () => setConnectionStatus(connectionId!, "paused", actor) + await assert.rejects(analyze()) + assert.equal(writes, 0) + const saved = JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows) + assert.ok(!saved.includes("NEVER_")) + // Exercise the actual service through a human-approved runtime plan. The + // legacy owned comment intentionally differs from the current server name. + table = "main"; mainWriteEnabled = true; policyComment = managedComment("NEVER_LEGACY_OWNED_COMMENT") + const legacyComment = policyComment + const withoutProbe = (({ controlProbe: _probe, ...candidate }) => candidate)(base.candidates[0]) + const mainProfile = routeAiProfileSchema.parse({ ...base, table: "main", candidates: [withoutProbe, { ...withoutProbe, id: "b", gateway: "192.0.2.9", gatewayRef: { resource: "ipv4_route", id: "*r2" } }] }) + for (const iface of ["wan", "hop"]) { + const observationId = randomUUID() + await pool.query("INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source) VALUES($1,$2,$2,$3,'peer','ipv4','main',1,0,100,100,now(),now(),true,'fixture')", [observationId, sid, iface]) + await attestRoutingFixture(observationId) + } + const writeAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Main Route context write", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write", "mm:filters:write"], scope: { serverIds: [sid], operationIds: ["routing.analyze", "routing.community.switch", "routing.profiles.save", "routing.profiles.read", "routing.controller.read", "routing.controller.update", "routing.path.verify"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), writerActor) + writeConnectionId = writeAccess.connection.id + const writer = await authenticateMcpToken(writeAccess.token) + const automatic = routeAiProfileSchema.parse({ ...mainProfile, autoRecovery: true, candidates: mainProfile.candidates.map(candidate => ({ ...candidate, controlProbe: base.candidates[0].controlProbe })) }) + const unavailable = await invokeMcpTool(writer, "mm_route_optimizer_analyze", { profile: automatic }) as { executionAvailable: boolean; executionReason: string } + assert.equal(unavailable.executionAvailable, false); assert.equal(unavailable.executionReason, "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE") + await assert.rejects(invokeMcpTool(writer, "mm_route_optimizer_plan", { profile: automatic, candidateId: "b" }), (error: Error & { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY" && error.message.includes("ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE")) + await assert.rejects(invokeMcpTool(writer, "mm_route_profile_save_plan", { profile: automatic, enabled: true, expiresAt: new Date(Date.now() + 900000).toISOString() }), (error: Error & { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY" && error.message.includes("ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE")) + assert.equal(writes, 0) + const waitOperation = async (id: string) => { + for (let attempt=0; attempt<400; attempt++) { + const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[id])).rows[0] + if(row && !["pending","running"].includes(row.state))return row + await delay(20) + } + throw Error("Targeted runtime operation timeout") + } + const activate = async (profileId?: string) => { + const grantExpiresAt=new Date(Date.now()+600000).toISOString() + const prepared=await invokeMcpTool(writer,"mm_route_profile_save_plan",{...(profileId?{profileId}:{}),profile:mainProfile,enabled:true,expiresAt:grantExpiresAt}) as {planId:string} + await approvePlan(prepared.planId,writerActor,true) + return {planId:prepared.planId,grantExpiresAt} + } + // Both distinct NEW profile plans are publicly prepared and approved while + // no profile is enabled. A later activation must recheck overlap at execute. + const firstProfileId=randomUUID(),secondProfileId=randomUUID() + const activation=await activate(firstProfileId),overlappingActivation=await activate(secondProfileId) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_routing_profiles WHERE connection_id=$1",[writeConnectionId])).rows[0].count,0) + const activationOperation=await applyPlan(writer,activation.planId,"runtime-main-baseline-enable") + assert.equal((await waitOperation(activationOperation.id)).state,"applied") + let controller=(await pool.query("SELECT * FROM mcp_routing_profiles WHERE connection_id=$1",[writeConnectionId])).rows[0] + assert.equal(controller.enabled,true);assert.equal(controller.version,1) + assert.equal(controller.state.baseline.packet.candidateId,"a") + assert.ok(controller.state.baseline.proof) + assert.equal(controller.id,firstProfileId) + const firstBaselineProof=controller.state.baseline.proof,beforeOverlapNative=writes + // Terminal state publication precedes lease-finally cleanup. Observe the + // REAL application lease release so rejection below proves overlap, not a + // transient lock collision. No native or authority deadline is extended. + const leaseProbe=await pool.connect();let acquired=false,unsafe=false + try { + const deadline=Date.now()+5000 + while(!(acquired=(await leaseProbe.query("SELECT pg_try_advisory_lock(hashtextextended('mm-config:application',0)) AS acquired")).rows[0].acquired)){ + assert(Date.now()error instanceof Error && "code" in error && error.code==="STATE_CONFLICT","A new overlapping prepare must refuse while the first profile is enabled") + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_routing_profiles WHERE connection_id=$1 AND enabled",[writeConnectionId])).rows[0].count,1) + assert.equal(writes,beforeOverlapNative) + console.log("TARGETED_PUBLIC_OVERLAP_TOCTOU_NATIVE0_PASS",JSON.stringify({newProfilesApprovedBeforeEitherEnabled:2,firstActivationApplied:true,secondTerminalCode:overlapTerminal.error_code,secondCasRows:0,enabledProfiles:1,newPrepareRefused:true,nativeWrites:0})) + const publicProfile=await invokeMcpTool(writer,"mm_route_profiles_list",{}) + assert.ok(!JSON.stringify(publicProfile).includes(controller.state.baseline.proof),"Public profile DTO must omit baseline proof") + const beforeControllerRead = requests + const controllerView = await invokeMcpTool(writer, "mm_route_controller_get", { profileId: controller.id }) as { id: string; enabled: boolean; state: Record } + assert.equal(controllerView.id, controller.id) + assert.equal(controllerView.enabled, true) + assert.equal(controllerView.state.baseline, undefined) + const hiddenConfig = { ...mainProfile, candidates: mainProfile.candidates.map(candidate => ({ ...candidate, exitNodeId: sid + 1000 })) } + await pool.query("UPDATE mcp_routing_profiles SET config=$2::jsonb WHERE id=$1", [controller.id, JSON.stringify(hiddenConfig)]) + try { + const notFound = (error: unknown) => error instanceof Error && "code" in error && error.code === "NOT_FOUND" + await assert.rejects(invokeMcpTool(writer, "mm_route_controller_get", { profileId: controller.id }), notFound) + await assert.rejects(invokeMcpTool(writer, "mm_route_profile_save_plan", { profileId: controller.id, profile: mainProfile, enabled: false, expiresAt: activation.grantExpiresAt }), notFound) + } finally { + await pool.query("UPDATE mcp_routing_profiles SET config=$2::jsonb WHERE id=$1", [controller.id, JSON.stringify(mainProfile)]) + } + assert.equal(requests, beforeControllerRead) + console.log("✓ Route AI controller: exact public state, hidden dependencies reject read and replacement before RouterOS") + // Explicit out-of-application drift after enrollment, before a trusted + // private host prepares another persistent plan. MAC/authority stay valid. + policyComment=legacyComment+"-external-drift" + const driftMainPlan=await invokeMcpTool(writer,"mm_route_optimizer_plan",{profile:mainProfile,candidateId:"b"},undefined,{persistentRoutingProfile:{id:controller.id,version:controller.version}}) as {planId:string} + await approvePlan(driftMainPlan.planId,writerActor,true) + const beforeMainDrift=writes,driftMainOperation=await applyPlan(writer,driftMainPlan.planId,"main-private-prepared-after-external-drift") + assert.notEqual((await waitOperation(driftMainOperation.id)).state,"applied") + assert.equal(writes,beforeMainDrift,"Authenticated private MAIN input prepared after external drift must send0nativewrites") + assert.equal((await pool.query("SELECT configuration_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1",[driftMainOperation.id])).rows[0].configuration_sealed,null) + policyComment=legacyComment + console.log("TARGETED_MAIN_PRIVATE_DRIFT_NATIVE0_PASS") + const plan = await invokeMcpTool(writer, "mm_route_optimizer_plan", { profile: mainProfile, candidateId: "b" },undefined,{persistentRoutingProfile:{id:controller.id,version:controller.version}}) as { planId: string } + + assert.deepEqual(privateMarkerPaths(plan), [], "initial plan response contains private native data") + assert.deepEqual(privateMarkerPaths(await publicPlan(await loadPlan(plan.planId))), [], "retrieved public plan contains private native data") + assert.equal((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0].evidence.recovery, undefined) + await approvePlan(plan.planId, writerActor, true) + // Actual human-approved MAIN source joins the common admission profile-row + // lock BEFORE its operation/intent/native write. No synthetic MAIN seal. + const profileEditor = await pool.connect() + await profileEditor.query("BEGIN") + const editorPid = Number((await profileEditor.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + await profileEditor.query("SELECT id FROM mcp_routing_profiles WHERE id=$1 FOR UPDATE", [controller.id]) + const admission = applyPlan(writer, plan.planId, "route-context-main-comment") + void admission.catch(() => {}) + let admissionError: unknown, operation: Awaited | undefined, blocked = false + try { + for (let attempt = 0; attempt < 150; attempt++) { + blocked = (await pool.query("SELECT pid FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND $1::int=ANY(pg_blocking_pids(pid)) AND query LIKE 'SELECT id FROM mcp_routing_profiles WHERE id=%FOR UPDATE'", [editorPid])).rowCount === 1 + if (blocked) break + await delay(20) + } + assert(blocked, "Actual MAIN admission must wait on the same profile owner lock") + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE plan_id=$1", [plan.planId])).rows[0].n), 0) + assert.equal(writes, beforeMainDrift, "Profile edit lock must precede operation intent/native dispatch") + } catch (error) { admissionError = error } + finally { + try { await profileEditor.query("ROLLBACK") } catch (error) { admissionError ??= error } + profileEditor.release() + try { operation = await admission } catch (error) { admissionError ??= error } + } + if (admissionError) throw admissionError + assert(operation) + console.log("TARGETED_MAIN_COMMON_PROFILE_ADMISSION_LOCK_PASS") + let applied: { state: string; error_code?: string } | undefined + for (let attempt = 0; attempt < 300; attempt++) { + applied = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (applied && !["pending", "running"].includes(applied.state)) break + await delay(30) + } + assert.equal(applied?.state, "applied", JSON.stringify(applied)); assert.equal(writes, 1); assert.equal(policyComment, legacyComment) + const checkpoint=(await pool.query("SELECT configuration_sealed,configuration_proof FROM mcp_operation_checkpoints WHERE operation_id=$1",[operation.id])).rows[0] + assert.ok(checkpoint.configuration_sealed);assert.ok(checkpoint.configuration_proof) + const principalConnection=await (await import("./store.js")).effectiveMcpConnection(writer) + const nextBaseline=await advanceRoutingControllerBaseline(controller.state.baseline,controller.id,controller.version,mainProfile,controller.grant_expires_at.toISOString(),{principal:writer,connection:principalConnection},plan.planId,operation.id) + assert.equal(nextBaseline.packet.candidateId,"b") + const forwardCharge=(await pool.query("SELECT charge_units,source_plan_id,profile_id FROM mcp_routing_forward_charges WHERE source_operation_id=$1",[operation.id])).rows + assert.deepEqual(forwardCharge,[{charge_units:1,source_plan_id:plan.planId,profile_id:controller.id}]) + const completionOwner=await pool.connect() + try { + await assertIdentifiedRoutingCompletion(completionOwner,controller.id,plan.planId,operation.id,writer) + await assert.rejects(assertIdentifiedRoutingCompletion(completionOwner,randomUUID(),plan.planId,operation.id,writer),(error:unknown)=>(error as {code?:string}).code==="STATE_CONFLICT") + } finally { completionOwner.release() } + const { rows: [{ finished_at: finishedAt }] } = await pool.query("SELECT finished_at FROM mcp_operations WHERE id=$1", [operation.id]) + const legacyAt=(await pool.query("SELECT clock_timestamp() AS at")).rows[0].at.toISOString() + assert(Date.parse(legacyAt) > finishedAt.getTime(), "Controller observes the completion after it was recorded") + assert.equal(await publishControllerState(pool,controller,{...controller.state,switches:[legacyAt],planId:plan.planId,candidateId:"a"}),true) + const { withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js") + const readController = async () => (await pool.query("SELECT * FROM mcp_routing_profiles WHERE id=$1", [controller.id])).rows[0] + const beforeMaintenance = await readController() + await withSystemDatabaseOperationLock(() => pool.connect(), async () => { + await tickRoutingProfiles() + assert.deepEqual(await readController(), beforeMaintenance) + }) + await tickRoutingProfiles() + controller=(await pool.query("SELECT * FROM mcp_routing_profiles WHERE id=$1",[controller.id])).rows[0] + assert.equal(controller.state.baseline.packet.candidateId,"b") + assert.equal(controller.state.expectedCandidateId,"b","Completion must use the signed baseline, even if the stored pending candidate is stale") + assert.deepEqual(controller.state.switches,[legacyAt],"Trusted charge completion must not append/dedup legacy timestamps") + assert.equal(controller.state.planId,undefined);assert.equal(controller.state.lastSwitchAt,finishedAt.toISOString()) + console.log("TARGETED_MAIN_FORWARD_CHARGE_COMPLETION_BRIDGE_PASS") + const {client}=await routerClient(sid) + const beforeDenied=writes + await assert.rejects(client.patch("/routing/filter/rule/*f",{rule:body})) + assert.equal(writes,beforeDenied,"Active controller refuses foreign native write before I/O") + // Disable through the same public human save/CAS service, not direct SQL. + await assert.rejects(invokeMcpTool(writer, "mm_route_controller_update_plan", { profileId: randomUUID(), profile: mainProfile, enabled: false, expiresAt: activation.grantExpiresAt }), (error: unknown) => error instanceof Error && "code" in error && error.code === "NOT_FOUND") + const disable=await invokeMcpTool(writer,"mm_route_controller_update_plan",{profileId:controller.id,profile:mainProfile,enabled:false,expiresAt:activation.grantExpiresAt}) as {planId:string} + await approvePlan(disable.planId,writerActor,true) + assert.equal((await waitOperation((await applyPlan(writer,disable.planId,"runtime-main-baseline-disable")).id)).state,"applied") + // Native writer owns the REAL server lock first. Approved activation cannot + // acquire its whole runtime lease or publish profile state through CAS. + const writerFirst=await activate(controller.id) + let writerEntered!:()=>void,releaseWriter!:()=>void + const writerReady=new Promise(r=>{writerEntered=r}),writerBarrier=new Promise(r=>{releaseWriter=r}) + const competingWriter=withConfigMutationLock([sid],async()=>{writerEntered();await writerBarrier;await client.patch("/routing/filter/rule/*f",{rule:body,chain:"bgp-in",disabled:"no",comment:legacyComment})}) + await writerReady + const writerFirstOperation=await applyPlan(writer,writerFirst.planId,"runtime-main-writer-first") + assert.notEqual((await waitOperation(writerFirstOperation.id)).state,"applied") + assert.equal((await pool.query("SELECT enabled FROM mcp_routing_profiles WHERE id=$1",[controller.id])).rows[0].enabled,false) + releaseWriter();await competingWriter + assert.equal(policyBody,body) + // Activation owns real application+server leases first. Park its fixed + // native baseline read, then prove foreign writer cannot enter the lease. + const activationFirst=await activate(controller.id) + let activationEntered!:()=>void,releaseActivation!:()=>void + const activationReady=new Promise(r=>{activationEntered=r}),activationBarrier=new Promise(r=>{releaseActivation=r}) + afterRead=async()=>{activationEntered();await activationBarrier} + const activeOperation=await applyPlan(writer,activationFirst.planId,"runtime-main-activation-first") + await activationReady + const beforeRacingWrite=writes + await assert.rejects(client.patch("/routing/filter/rule/*f",{rule:body})) + assert.equal(writes,beforeRacingWrite) + releaseActivation() + assert.equal((await waitOperation(activeOperation.id)).state,"applied") + controller=(await pool.query("SELECT * FROM mcp_routing_profiles WHERE id=$1",[controller.id])).rows[0] + assert.equal(controller.enabled,true);assert.equal(controller.state.baseline.packet.candidateId,"a") + await assert.rejects(client.patch("/routing/filter/rule/*f",{rule:body})) + assert.equal(writes,beforeRacingWrite) + // Two genuine production CAS calls on distinct SQL clients race the SAME + // expected version and prepared NEXT-version baseline. Exactly one wins. + const nextHumanBaseline=await prepareRoutingControllerBaseline(controller.id,controller.version+1,mainProfile,controller.grant_expires_at.toISOString(),{principal:writer,connection:principalConnection}) + const casInput={profileId:controller.id,connectionId:writeConnectionId,expectedVersion:controller.version,profile:mainProfile,enabled:true,expiresAt:controller.grant_expires_at.toISOString(),credentialId:writer.credentialId,generation:controller.generation,policyVersion:controller.policy_version,baseline:nextHumanBaseline} + const human1=await pool.connect(),human2=await pool.connect() + try { + const saved=await Promise.all([saveControllerProfileCas(human1,casInput),saveControllerProfileCas(human2,casInput)]) + assert.equal(saved.filter(Boolean).length,1) + assert.equal(saved.find(Boolean)!.version,controller.version+1) + assert.equal(await publishControllerState(human1,controller,{baseline:controller.state.baseline,reason:"stale-tick"}),false) + }finally{human1.release();human2.release()} + const last=(await pool.query("SELECT * FROM mcp_routing_profiles WHERE id=$1",[controller.id])).rows[0] + assert.equal(last.version,controller.version+1);assert.equal(last.state.baseline.packet.profileVersion,last.version) + assert.equal(last.state.baseline.packet.candidateId,"a") + console.log("TARGETED_MAIN_BASELINE_RUNTIME_PASS",JSON.stringify({humanActivation:true,realRuntime:true,realApplyFiltersWriter:true,mainWithoutProbe:true,ownSwitchAndAdvance:true,writerFirst:true,activationFirst:true,genuineTwoClientCas:true,publicOverlapTOCTOU:true,foreignNativeWrites:0,authorizedFixtureWrites:writes})) + const afterSwitch = await invokeMcpTool(writer, "mm_route_optimizer_analyze", { profile: mainProfile }) as { currentCandidateId: string | null; executionAvailable: boolean } + assert.equal(afterSwitch.currentCandidateId, "a"); assert.equal(afterSwitch.executionAvailable, true) + const writerResults = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [writeConnectionId])).rows + assert.deepEqual(privateMarkerPaths(writerResults), [], "request result contains private native data at these JSON paths") + assert.ok(!JSON.stringify(writerResults).includes("NEVER_")) + const beforeUnrecognizedState = writes + await pool.query("UPDATE mcp_operations SET state='fixture-unrecognized-terminal' WHERE id=$1", [driftMainOperation.id]) + assert.equal(await publishControllerState(pool, last, { ...last.state, planId: driftMainPlan.planId }), true) + await tickRoutingProfiles() + const stopped = await readController() + assert.equal(stopped.enabled, false) + assert.equal(stopped.state.pausedReason, "Изменение отклонено или требует разбора результата") + assert.equal(stopped.state.planId, driftMainPlan.planId) + assert.deepEqual(stopped.state.baseline, last.state.baseline) + assert.equal(writes, beforeUnrecognizedState) + console.log("✓ Targeted baseline extension plus existing Route AI context SQL+HTTP: independent table/BGP/probe VRF, exact effective input binding/inheritance/family/candidate ambiguity, shared-chain dependencies, private signed path checks, endpoint/dependency/tamper drift, return-path unknown, UTF-8 byte cap, live revocation; read-only/non-main/recovery guards made zero writes; approved main switch made one PATCH with verified read-back and preserved legacy owned comment") + } finally { + if (writeConnectionId) await setConnectionStatus(writeConnectionId, "paused", writerActor) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=$1", [sid]) + router.closeAllConnections(); await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/routing-controller-baseline.test.ts b/backend/src/modules/mcp/routing-controller-baseline.test.ts new file mode 100644 index 00000000..1d0641fb --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-baseline.test.ts @@ -0,0 +1,142 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, join, resolve } from "node:path" +import { test, type TestContext } from "node:test" +import ts from "typescript" +import { z } from "zod" +import { scopedTargetRuleMatches } from "./routing-community-scoped-model.js" +import * as security from "./security.js" +import type * as Baseline from "./routing-controller-baseline.js" + +// Run the actual baseline implementation and durable HMAC on a temporary key. +// Only the SQL/RouterOS/authority adapters supply synthetic snapshots. +async function fixture(t: TestContext) { + const root = resolve(tmpdir()), prefix = "mcp-controller-baseline-test-" + const state = await mkdtemp(join(root, prefix)), previous = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = state + t.after(async () => { + if (previous === undefined) delete process.env.MCP_STATE_DIR + else process.env.MCP_STATE_DIR = previous + assert.equal(dirname(resolve(state)), root) + assert(basename(state).startsWith(prefix)) + await rm(state, { recursive: true }) + }) + const { fingerprint, privateSecretFingerprint } = security + const now = Date.now(), profileId = randomUUID(), planId = randomUUID(), operationId = randomUUID(), connectionId = randomUUID() + const grant = new Date(now + 120000).toISOString() + const principal = { connectionId, credentialId: randomUUID(), credentialExpiresAt: grant } + const profile = { serverId: 1, table: "tenant", probeVrf: "tenant", candidates: ["a", "b"].map(id => ({ id, jumpHostId: 1, exitNodeId: 1 })) } + const permissions = ["mm:network:read", "mm:network:write", "mm:filters:write"] + const connection = { id: connectionId, owner_id: "owner", generation: await security.accessGeneration(), + owner_valid_until: new Date(grant), owner_claims_issued_at: new Date(now), owner_permissions: permissions, + policy: { status: "active", mode: "operator", expiresAt: grant, permissions, + scope: { serverIds: [1], operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify"] } } } + const context = { principal, connection } as Parameters[4] + const originalRule = 'if (bgp-communities includes 65000:10) {\n set gw 192.0.2.1;\n set gw-interface "marker-gre-a";\n accept;\n}' + const desiredRule = originalRule.replace("192.0.2.1", "192.0.2.9").replace("marker-gre-a", "marker-gre-b") + const bareRule = desiredRule.replace('"marker-gre-b"', "marker-gre-b") + const signTarget = await security.privateFingerprinter("baseline-test-native-target") + const stableTarget = { ".id": "*1", chain: "input", comment: "managed" } + let observed = { ...stableTarget, rule: originalRule }, candidate = "a", failAfterRead = false, driftSourceAtFence = false + const targetProof = (rule: string) => signTarget(fingerprint({ ...observed, rule })) + const desiredTargetProof = targetProof(desiredRule), dependencyProof = "d".repeat(64), checkpointProof = "c".repeat(64) + const sourceProofs = [{ serverId: 1, proof: "a".repeat(64) }] + const receiptId = randomUUID(), requestSeal = "request-proof", networkSeal = "network-proof" + const input = { proofRevision: 3, profile, principal, ownerId: connection.owner_id, nativeId: "*1", originalCandidateId: "a", + originalTargetProof: targetProof(originalRule), desiredTargetProof, dependencyProof, contextSources: sourceProofs, candidateId: "b", + persistentProfileAuthority: { packet: { profileId, profileVersion: 1 } }, receiptId } + const step = { kind: "service", service: "routing.community.scoped", input } + const configuration = { checkpointProof, observedAt: new Date(now-100).toISOString(), packet: { revision: 2, operationId, stepIndex: 0, + nativeId: "*1", originalCandidateId: "a", originalTargetProof: input.originalTargetProof, desiredCandidateId: "b", desiredTargetProof, + dependencyProof, contextSources: sourceProofs, desiredRule } } + const nativeVerificationSeal = await privateSecretFingerprint(fingerprint({ request: requestSeal, dependency: dependencyProof, desired: desiredTargetProof, + candidate: "b", configuration: checkpointProof, network: networkSeal }), "route-ai-scoped-verified-v3") + const row = { id: operationId, plan_id: planId, state: "applied", step_state: "verified", operation_id: "routing.community.switch", + operation_version: 7, approved_by: "owner", created_at: new Date(now-200), intended_state: security.redact(step), + evidence: { sealed: await security.encryptSecret(JSON.stringify({ steps: [step], target: { scope: "servers", serverIds: [1] } })) }, + result: { nativeVerificationSeal, configurationCheckpointProof: checkpointProof, networkCheckpointProof: networkSeal, receiptId, + proofRevision: 3, nativeWrites: 1, configurationVerified: true, postProbeStatus: "passed", controlProbeVerified: true } } + const providers: Record = { + "./routing-main-transition.js": {}, + "../../db/index.js": { pool: { query: async (_sql: string, ids: string[]) => { + assert.deepEqual(ids, [operationId, planId, connectionId]) + return { rows: [failAfterRead ? { ...row, step_state: "unknown" } : row] } + } } }, + "./registry.js": { findMcpOperation: () => ({ version: 7 }) }, + "./routing-community-scoped-request.js": { assertScopedCommunityRequest: async () => requestSeal }, + "./routing-profile-authority.js": { assertPersistentScopedProfileAuthority: async () => {}, + routingProfileAuthorityBindingSchema: { parse: (value: unknown) => value } }, + "./routing-community-scoped-checkpoint.js": { loadScopedConfigurationPacket: async () => configuration, + loadScopedNetworkCheckpoint: async () => ({ status: "passed", networkSeal }) }, + "./routing-original-effects.js": {}, + zod: { z }, "@mmapp/contracts/mcp-routing": { routeAiProfileSchema: { parse: (value: unknown) => value } }, + "../../lib/permissions.js": { hasPermission: (set: string[], permission: string) => set.includes(permission) }, + "./store.js": { effectiveMcpConnection: async () => connection }, + "./security.js": security, + "./routing-community-context.js": { readRouteCommunityContext: async (_profile: unknown, options: { authorize(): Promise }) => { + await options.authorize() + const target = { nativeId: observed[".id"], rawRow: observed, desiredTargetProof: targetProof } + return { before: { nativeRuleId: target.nativeId, scopedExecutionAvailable: true, currentCandidateId: candidate, + targetRuleProof: targetProof(observed.rule), dependencyProof, sourceProofs }, + ownedTarget: { ...target, matchesRuleProof: (body: string, proof: unknown) => scopedTargetRuleMatches(target, body, proof) }, + fence: async () => { await options.authorize(); if (driftSourceAtFence) failAfterRead = true } } + } }, + } + const source = await readFile(new URL("./routing-controller-baseline.ts", import.meta.url), "utf8") + const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + const baseline = {} as typeof Baseline + new Function("exports", "require", compiled)(baseline, (name: string) => { assert(providers[name], name); return providers[name] }) + const original = await baseline.prepareRoutingControllerBaseline(profileId, 1, profile, grant, context) + const links = { planId, operationId, checkpointProof } + return { original, links, desiredTargetProof, targetProof, originalRule, desiredRule, bareRule, baseline, + advance: () => baseline.advanceRoutingControllerBaseline(original, profileId, 1, profile, grant, context, planId, operationId), + assertCurrent: (value: unknown) => baseline.assertRoutingControllerBaseline(value, profileId, 1, profile, grant, context), + driftAfterNativeRead: () => { driftSourceAtFence = true }, + observe: (rule: string, metadata: Partial = {}, selected = "b") => { candidate = selected; observed = { ...stableTarget, ...metadata, rule } } } +} + +test("controller signs an exact legacy-shaped next baseline when native target proof matches", async t => { + const f = await fixture(t) + await f.assertCurrent(f.original) + f.observe(f.desiredRule) + const next = await f.advance() + assert.equal(next.packet.targetProof, f.desiredTargetProof) + assert.deepEqual(next.packet.transition, f.links) + assert.equal(next.packet.previousProof, f.original.proof) + assert.deepEqual(await f.assertCurrent(next), next) +}) + +test("controller authenticates the source but enrolls the exact bare RouterOS readback", async t => { + const f = await fixture(t) + f.observe(f.bareRule) + const next = await f.advance() + assert.equal(next.packet.targetProof, f.targetProof(f.bareRule)) + assert.notEqual(next.packet.targetProof, f.desiredTargetProof) + assert.deepEqual(next.packet.transition, { ...f.links, sourceDesiredTargetProof: f.desiredTargetProof }) + assert.deepEqual(await f.assertCurrent(next), next) + const tampered = structuredClone(next) + tampered.packet.transition!.sourceDesiredTargetProof = "e".repeat(64) + await assert.rejects(f.baseline.routingControllerBaselinePrincipal(tampered), { code: "STATE_CONFLICT" }) + f.observe(f.desiredRule) + await assert.rejects(f.assertCurrent(next), { code: "STATE_CONFLICT" }) + f.observe(f.originalRule, {}, "a") + await assert.rejects(f.assertCurrent(next), { code: "STATE_CONFLICT" }) + await f.assertCurrent(f.original) +}) + +test("controller refuses changed selected rule and drift in other native row fields", async t => { + const f = await fixture(t) + f.observe(f.bareRule.replace("marker-gre-b", "other")) + await assert.rejects(f.advance(), { code: "STATE_CONFLICT" }) + f.observe(f.bareRule, { comment: "changed" }) + await assert.rejects(f.advance(), { code: "STATE_CONFLICT" }) +}) + +test("controller refuses baseline publication if the source operation changes after readback", async t => { + const f = await fixture(t) + f.observe(f.bareRule) + f.driftAfterNativeRead() + await assert.rejects(f.advance(), { code: "STATE_CONFLICT" }) +}) diff --git a/backend/src/modules/mcp/routing-controller-baseline.ts b/backend/src/modules/mcp/routing-controller-baseline.ts new file mode 100644 index 00000000..e4d6a51c --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-baseline.ts @@ -0,0 +1,206 @@ +import { loadRoutingMainTransition, routingMainServiceScope } from "./routing-main-transition.js" +import { pool } from "../../db/index.js" +import { findMcpOperation } from "./registry.js" +import { assertScopedCommunityRequest } from "./routing-community-scoped-request.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { loadScopedConfigurationPacket, loadScopedNetworkCheckpoint } from "./routing-community-scoped-checkpoint.js" +import { originalEffectSelectionSchema } from "./routing-original-effects.js" +import { z } from "zod" +import { routeAiProfileSchema, type RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { hasPermission } from "../../lib/permissions.js" +import { effectiveMcpConnection } from "./store.js" +import { accessGeneration, constantEqual, decryptSecret, fingerprint, redact, McpError, privateSecretFingerprint } from "./security.js" +import { readRouteCommunityContext } from "./routing-community-context.js" +import type { ActionContext } from "./action-types.js" + +// DRAFT: activation/native identity only. No new ledger, scope contract or recovery authority. +// Fixed production reader; no caller observation/signing provider is accepted. +const proof = z.string().regex(/^[a-f0-9]{64}$/) +const packetSchema = z.object({ + revision: z.literal(1), profileId: z.string().uuid(), profileVersion: z.number().int().positive(), + profileProof: proof, connectionId: z.string().uuid(), credentialId: z.string().uuid(), + principalExpiresAt: z.string().datetime(), ownerId: z.string().min(1), authorityProof: proof, grantExpiresAt: z.string().datetime(), + nativeId: z.string().regex(/^\*[a-z0-9]+$/i), candidateId: z.string().min(1), + targetProof: proof, dependencyProof: proof, + sourceProofs: z.array(z.object({ serverId: z.number().int().positive(), proof }).strict()).min(1).max(1000), + observedAt: z.string().datetime(), previousProof: proof.optional(), + transition: z.object({ planId: z.string().uuid(), operationId: z.string().uuid(), checkpointProof: proof, sourceDesiredTargetProof: proof.optional() }).strict().optional(), +}).strict() +const baselineSchema = z.object({ packet: packetSchema, proof }).strict() +export type RoutingControllerBaseline = z.infer +const fail = () => new McpError("STATE_CONFLICT", "Подтверждённое исходное состояние контроллера отсутствует или изменилось", 409) +const sign = (packet: unknown) => privateSecretFingerprint(fingerprint(packet), "route-ai-controller-baseline-v1") +const supportedExecution = (profile: RouteAiProfile, before: { executionAvailable?:boolean;scopedExecutionAvailable?:boolean }) => profile.table === "main" && profile.probeVrf === "main" && (!profile.bgpContext || profile.bgpContext.inputChain === "bgp-in" && profile.bgpContext.vrf === "main") ? before.executionAvailable : before.scopedExecutionAvailable +export const routingControllerScope = (profile: RouteAiProfile) => [...new Set([profile.serverId, + ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId, + ...(candidate.controlProbe?.expectedHops.map(hop => hop.serverId) ?? [])])])].sort((a, b) => a - b) + +export async function routingControllerBaselinePrincipal(raw: unknown) { const value = baselineSchema.parse(raw); if (!constantEqual(value.proof, await sign(value.packet))) throw fail(); return { connectionId: value.packet.connectionId, credentialId: value.packet.credentialId, credentialExpiresAt: value.packet.principalExpiresAt } } + +async function authority(context: ActionContext, profile: RouteAiProfile, grantExpiresAt: string) { + if (context.signal?.aborted) throw fail() + const current = await effectiveMcpConnection(context.principal), generation = await accessGeneration() + const grant = Date.parse(grantExpiresAt) + if (!Number.isFinite(grant) || grant <= Date.now() || grant > Math.min(Date.parse(context.principal.credentialExpiresAt), + current.owner_valid_until.getTime(), Date.parse(current.policy.expiresAt)) || + current.id !== context.principal.connectionId || current.owner_id !== context.connection.owner_id || + current.policy.status !== "active" || current.policy.mode === "observer" || current.generation !== generation || + routingControllerScope(profile).some(id => !current.policy.scope.serverIds.includes(id))) throw fail() + for (const permission of ["mm:network:read", "mm:network:write", "mm:filters:write"]) { + if (!hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission)) throw fail() + } + for (const operation of ["routing.analyze", "routing.community.switch", "routing.path.verify"]) { + if (!current.policy.scope.operationIds.includes(operation)) throw fail() + } + return fingerprint({ connectionId: current.id, principal: context.principal, ownerId: current.owner_id, + ownerPermissions: current.owner_permissions, ownerValidUntil: current.owner_valid_until, + ownerClaimsIssuedAt: current.owner_claims_issued_at, policy: current.policy, generation, grantExpiresAt }) +} + +/** Human plan preparation enrolls a baseline. A tick never calls this issuer. + * The sealed service step fixes NEXT version and grant before human approval. */ +export async function prepareRoutingControllerBaseline(profileId: string, profileVersion: number, rawProfile: unknown, + grantExpiresAt: string, context: ActionContext): Promise { + z.string().uuid().parse(profileId); z.number().int().positive().parse(profileVersion) + const profile = routeAiProfileSchema.parse(rawProfile) + const authorityProof = await authority(context, profile, grantExpiresAt) + const authorize = async () => { if (await authority(context, profile, grantExpiresAt) !== authorityProof) throw fail() } + const native = await readRouteCommunityContext(profile, { authorize, signal: context.signal }) + const before = native.before + if (before.bgpContextMode === "legacy-read-only") throw new McpError("UNSUPPORTED_CAPABILITY", "LEGACY_BGP_READ_ONLY", 409) + if (!supportedExecution(profile,before) || !native.ownedTarget || !before.currentCandidateId || !before.targetRuleProof || + native.ownedTarget.nativeId !== before.nativeRuleId || !profile.candidates.some(row => row.id === before.currentCandidateId)) throw fail() + await native.fence(); await authorize() + const packet = packetSchema.parse({ revision: 1, profileId, profileVersion, profileProof: fingerprint(profile), + connectionId: context.principal.connectionId, credentialId: context.principal.credentialId, principalExpiresAt: context.principal.credentialExpiresAt, ownerId: context.connection.owner_id, + authorityProof, grantExpiresAt, nativeId: native.ownedTarget.nativeId, candidateId: before.currentCandidateId, + targetProof: before.targetRuleProof, dependencyProof: before.dependencyProof, sourceProofs: before.sourceProofs, + observedAt: new Date().toISOString() }) + return { packet, proof: await sign(packet) } +} + +/** Fixed live comparison for profile execute and every tick boundary. + * Original grant expiry is never renewed. No mutation capability is returned. */ +export async function authenticateRoutingControllerBaseline(rawBaseline: unknown, profileId: string, profileVersion: number, + rawProfile: unknown, grantExpiresAt: string, context: ActionContext) { + const parsed = baselineSchema.safeParse(rawBaseline) + if (!parsed.success) throw fail() + const value = parsed.data, packet = value.packet, profile = routeAiProfileSchema.parse(rawProfile) + if (!constantEqual(value.proof, await sign(packet)) || packet.profileId !== profileId || packet.profileVersion !== profileVersion || + packet.profileProof !== fingerprint(profile) || packet.grantExpiresAt !== grantExpiresAt || + packet.connectionId !== context.principal.connectionId || packet.credentialId !== context.principal.credentialId || + packet.principalExpiresAt !== context.principal.credentialExpiresAt || packet.ownerId !== context.connection.owner_id || Date.parse(packet.observedAt) > Date.now() || + await authority(context, profile, grantExpiresAt) !== packet.authorityProof) throw fail() + const authorize = async () => { if (await authority(context, profile, grantExpiresAt) !== packet.authorityProof) throw fail() } + return { value, profile, authorize } +} + +export async function assertRoutingControllerBaseline(rawBaseline: unknown, profileId: string, profileVersion: number, rawProfile: unknown, grantExpiresAt: string, context: ActionContext) { + const { value, profile, authorize } = await authenticateRoutingControllerBaseline(rawBaseline, profileId, profileVersion, rawProfile, grantExpiresAt, context) + const packet = value.packet + const native = await readRouteCommunityContext(profile, { authorize, signal: context.signal, expectedSources: packet.sourceProofs }) + if (!supportedExecution(profile,native.before) || native.ownedTarget?.nativeId !== packet.nativeId || + native.before.nativeRuleId !== packet.nativeId || native.before.currentCandidateId !== packet.candidateId || + native.before.targetRuleProof !== packet.targetProof || native.before.dependencyProof !== packet.dependencyProof || + fingerprint(native.before.sourceProofs) !== fingerprint(packet.sourceProofs)) throw fail() + await native.fence(); await authorize() + return value +} + + + + + +/** Source operation, sealed input and signed CONFIG_APPLIED authorize ONLY the + * expected baseline transition. Current readback alone can never enroll drift. + * Applied configuration/control probe is not full network verification. */ +export async function advanceRoutingControllerBaseline(rawBaseline: unknown, profileId: string, profileVersion: number, + rawProfile: unknown, grantExpiresAt: string, context: ActionContext, planId: string, operationId: string) { + const { value, profile, authorize } = await authenticateRoutingControllerBaseline(rawBaseline, profileId, profileVersion, rawProfile, grantExpiresAt, context) + const original = value.packet + z.string().uuid().parse(planId); z.string().uuid().parse(operationId) + const readSource = async () => { + await authorize() + const found = (await pool.query(`SELECT o.id,o.plan_id,o.state,o.created_at,p.operation_id,p.operation_version,p.approved_by, + CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence END AS evidence, + s.state AS step_state,CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END AS intended_state, + CASE WHEN octet_length(s.result::text)<=1048576 THEN s.result END AS result + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE o.id=$1 AND o.plan_id=$2 AND o.connection_id=$3 AND s.step_index=0 LIMIT 2`, [operationId, planId, context.principal.connectionId])).rows + if (found.length !== 1) throw fail() + const row = found[0] + if (row.id !== operationId || row.plan_id !== planId || row.state !== "applied" || row.step_state !== "verified" || + row.operation_id !== "routing.community.switch" || row.operation_version !== findMcpOperation(row.operation_id)?.version || + row.approved_by !== context.connection.owner_id || typeof row.evidence?.sealed !== "string" || !(row.created_at instanceof Date)) throw fail() + await authorize() + return row + } + const row = await readSource(), sourceIdentity = fingerprint(row) + const evidence = JSON.parse(await decryptSecret(row.evidence.sealed)) + if (evidence.steps?.length === 1 && evidence.steps[0].kind === "service" && evidence.steps[0].service === "routing.community") { + const input = evidence.steps[0].input + const witness = await loadRoutingMainTransition(input,operationId,planId), prepared = witness.packet.preparation + const binding = routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority) + if (binding.packet.profileId!==profileId || binding.packet.profileVersion!==profileVersion || fingerprint(input.profile)!==original.profileProof || + fingerprint(input.principal)!==fingerprint(context.principal) || input.ownerId!==original.ownerId || prepared.nativeId!==original.nativeId || + prepared.originalCandidateId!==original.candidateId || prepared.originalTargetProof!==original.targetProof || prepared.dependencyProof!==original.dependencyProof || + fingerprint(prepared.contextSources)!==fingerprint(original.sourceProofs) || fingerprint(prepared.scope)!==fingerprint(routingMainServiceScope(profile)))throw fail() + const native = await readRouteCommunityContext(profile,{authorize,signal:context.signal,expectedSources:original.sourceProofs,selectedCandidateId:prepared.desiredCandidateId}) + if(!native.before.executionAvailable || native.ownedTarget?.nativeId!==original.nativeId || native.ownedTarget.rawRow.rule!==prepared.desiredRule || + native.before.currentCandidateId!==prepared.desiredCandidateId || native.before.targetRuleProof!==prepared.desiredTargetProof || + native.before.dependencyProof!==original.dependencyProof || fingerprint(native.before.sourceProofs)!==fingerprint(original.sourceProofs))throw fail() + await native.fence();await authorize();await assertPersistentScopedProfileAuthority(input,context.signal) + if(fingerprint(await readSource())!==sourceIdentity)throw fail() + const next=packetSchema.parse({...original,candidateId:prepared.desiredCandidateId,targetProof:prepared.desiredTargetProof,observedAt:new Date().toISOString(), + previousProof:value.proof,transition:{planId,operationId,checkpointProof:witness.checkpointProof}}) + return {packet:next,proof:await sign(next)} + } + if (evidence.steps?.length !== 1 || evidence.steps[0].kind !== "service" || evidence.steps[0].service !== "routing.community.scoped" || evidence.target?.scope !== "servers") throw fail() + const step = evidence.steps[0], input = step.input + if (input.proofRevision !== 3 || fingerprint(input.profile) !== original.profileProof || + fingerprint(input.principal) !== fingerprint(context.principal) || input.ownerId !== original.ownerId || input.nativeId !== original.nativeId || + input.originalCandidateId !== original.candidateId || input.originalTargetProof !== original.targetProof || input.dependencyProof !== original.dependencyProof || + fingerprint(input.contextSources) !== fingerprint(original.sourceProofs) || fingerprint(row.intended_state) !== fingerprint(redact(step)) || + fingerprint([...evidence.target.serverIds].sort((a:number,b:number)=>a-b)) !== fingerprint(routingControllerScope(profile))) throw fail() + const authorityBinding = routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority) + if (authorityBinding.packet.profileId !== profileId || authorityBinding.packet.profileVersion !== profileVersion) throw fail() + await assertPersistentScopedProfileAuthority(input, context.signal) + const requestSeal = await assertScopedCommunityRequest(input) + const configuration = await loadScopedConfigurationPacket(input, requestSeal, { operationId, stepIndex: 0 }) + const network = await loadScopedNetworkCheckpoint(input, requestSeal) + if (!configuration || configuration.packet.revision !== 2 || !network || network.status !== "passed") throw fail() + // A genuinely prepared effect check may pause controller advancement without + // erasing the separate successful manual configuration/control-probe receipt. + if (Object.hasOwn(input, "originalEffectSelection")) { + const effects = originalEffectSelectionSchema.safeParse(input.originalEffectSelection) + if (!effects.success || effects.data.status !== "prepared" || network.originalEffects?.status !== "observed-expected" || + network.originalEffects.selectionIdentity !== fingerprint(effects.data) || network.originalEffects.switchEffectObserved !== true || !network.originalEffects.transition?.changedPrefixes.length) throw fail() + } + const packet = configuration.packet, result = row.result + if (packet.operationId !== operationId || packet.stepIndex !== 0 || packet.nativeId !== original.nativeId || + packet.originalCandidateId !== original.candidateId || packet.originalTargetProof !== original.targetProof || + packet.dependencyProof !== original.dependencyProof || fingerprint(packet.contextSources) !== fingerprint(original.sourceProofs) || + packet.desiredCandidateId !== input.candidateId || packet.desiredTargetProof !== input.desiredTargetProof || + Date.parse(configuration.observedAt) < row.created_at.getTime() || Date.parse(configuration.observedAt) > Date.now()) throw fail() + const expectedSeal = await privateSecretFingerprint(fingerprint({ request: requestSeal, dependency: input.dependencyProof, desired: input.desiredTargetProof, + candidate: input.candidateId, configuration: configuration.checkpointProof, network: network.networkSeal }), `route-ai-scoped-verified-v${input.proofRevision}`) + if (result?.nativeVerificationSeal !== expectedSeal || result.configurationCheckpointProof !== configuration.checkpointProof || + result.networkCheckpointProof !== network.networkSeal || result.receiptId !== input.receiptId || result.proofRevision !== input.proofRevision || + result.nativeWrites !== 1 || result.configurationVerified !== true || result.postProbeStatus !== "passed" || result.controlProbeVerified !== true) throw fail() + const native = await readRouteCommunityContext(profile, { authorize, signal: context.signal, expectedSources: original.sourceProofs, selectedCandidateId: packet.desiredCandidateId }) + const targetProof = native.before.targetRuleProof + if (!supportedExecution(profile,native.before) || native.ownedTarget?.nativeId !== original.nativeId || native.before.nativeRuleId !== original.nativeId || + !targetProof || !native.ownedTarget.matchesRuleProof(packet.desiredRule, packet.desiredTargetProof) || native.before.currentCandidateId !== packet.desiredCandidateId || + native.before.dependencyProof !== original.dependencyProof || fingerprint(native.before.sourceProofs) !== fingerprint(original.sourceProofs)) throw fail() + await native.fence(); await authorize() + await assertPersistentScopedProfileAuthority(input, context.signal) + if (fingerprint(await readSource()) !== sourceIdentity) throw fail() + // Enroll the exact observed native seal for the next tick. The source seal + // still identifies this operation and its inverse when RouterOS omits quotes. + const next = packetSchema.parse({ ...original, candidateId: packet.desiredCandidateId, targetProof, + observedAt: new Date().toISOString(), previousProof: value.proof, transition: { planId, operationId, checkpointProof: configuration.checkpointProof, + ...(targetProof !== packet.desiredTargetProof ? { sourceDesiredTargetProof: packet.desiredTargetProof } : {}) } }) + return { packet: next, proof: await sign(next) } +} + + diff --git a/backend/src/modules/mcp/routing-controller-cas-acceptance.ts b/backend/src/modules/mcp/routing-controller-cas-acceptance.ts new file mode 100644 index 00000000..9058c347 --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-cas-acceptance.ts @@ -0,0 +1,73 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { PoolClient } from "pg" +import { pool, createPool } from "../../db/index.js" +import { controllerProfileCurrent, publishControllerState, saveControllerProfileCas, withControllerProfileLease } from "./routing-controller-fence.js" + +/** Disposable SQL only; no RouterOS/native calls. Copies table constraints/defaults without foreign keys. */ +export async function acceptRoutingControllerCas() { + const schema = "controller_cas_" + randomUUID().replaceAll("-", "") + const database = createPool(4) + await pool.query(`CREATE SCHEMA ${schema}`) + let human: PoolClient | undefined + try { + await pool.query(`CREATE TABLE ${schema}.mcp_routing_profiles (LIKE public.mcp_routing_profiles INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES)`) + human = await database.connect() + await human.query(`SET search_path TO ${schema},public`) + const input = { profileId: randomUUID(), connectionId: randomUUID(), expectedVersion: null as number | null, + profile: { marker: "first-human-grant" }, enabled: true, expiresAt: new Date(Date.now() + 600000).toISOString(), + credentialId: randomUUID(), generation: "original-generation", policyVersion: 1 } + assert.equal((await saveControllerProfileCas(human, input))?.version, 1) + assert.equal(await saveControllerProfileCas(human, input), undefined, "same-owner create collision must not overwrite") + assert.equal(await saveControllerProfileCas(human, { ...input, connectionId: randomUUID() }), undefined) + const previousSwitch = new Date(Date.now() - 1000).toISOString() + await human.query("UPDATE mcp_routing_profiles SET state=$2::jsonb WHERE id=$1", [input.profileId, + JSON.stringify({ switches: [previousSwitch], lastSwitchAt: previousSwitch, marker: "old-controller-state" })]) + + const fence = { id: input.profileId, connection_id: input.connectionId, version: 1 } + let entered!: () => void, resume!: () => void + const started = new Promise(resolve => { entered = resolve }) + const blocked = new Promise(resolve => { resume = resolve }) + let competitorEntered = false + const tick = withControllerProfileLease(database, input.profileId, async lease => { + await lease.query(`SET search_path TO ${schema},public`) + assert.equal(await controllerProfileCurrent(lease, fence), true) + entered(); await blocked + assert.equal(await controllerProfileCurrent(lease, fence), false) + assert.equal(await publishControllerState(lease, fence, { marker: "stale-tick" }), false) + assert.equal(await publishControllerState(lease, fence, { pausedReason: "stale-stop" }, true), false) + }) + await started + try { + await withControllerProfileLease(database, input.profileId, async () => { competitorEntered = true }) + assert.equal(competitorEntered, false, "second tick cannot enter the same profile") + const changed = { ...input, expectedVersion: 1, profile: { marker: "new-human-grant" }, generation: "new-generation", credentialId: randomUUID() } + assert.equal((await saveControllerProfileCas(human, changed))?.version, 2) + assert.equal(await saveControllerProfileCas(human, changed), undefined, "stale human save must fail CAS") + } finally { resume(); await tick } + const saved = (await human.query("SELECT * FROM mcp_routing_profiles WHERE id=$1", [input.profileId])).rows[0] + assert.equal(saved.enabled, true); assert.equal(saved.version, 2) + assert.deepEqual(saved.config, { marker: "new-human-grant" }) + assert.deepEqual(saved.state, { switches: [previousSwitch], lastSwitchAt: previousSwitch }, "Human save preserves switch-rate history but replaces prior controller state") + assert.equal(saved.generation, "new-generation") + + const current = { ...fence, version: 2 } + assert.equal(await publishControllerState(human, current, { marker: "fresh-tick" }), true) + // Emergency/portal pauses currently disable directly, without incrementing version. + await human.query("UPDATE mcp_routing_profiles SET enabled=false,state=$2::jsonb WHERE id=$1", [input.profileId, JSON.stringify({ pausedReason: "human-emergency-pause" })]) + assert.equal(await controllerProfileCurrent(human, current), false) + assert.equal(await publishControllerState(human, current, { marker: "stale-after-pause" }), false) + assert.equal(await publishControllerState(human, current, { pausedReason: "stale-stop" }, true), false) + assert.deepEqual((await human.query("SELECT state FROM mcp_routing_profiles WHERE id=$1", [input.profileId])).rows[0].state, { pausedReason: "human-emergency-pause" }) + + await assert.rejects(withControllerProfileLease(database, input.profileId, async () => { throw new Error("fixture failure") }), /fixture failure/) + let released = false + await withControllerProfileLease(database, input.profileId, async () => { released = true }) + assert.equal(released, true, "throwing tick releases its session lock") + return { accepted: true, concurrentTicks: "one-owner", staleStateWrites: 0, staleSaveWrites: 0, createCollisionWrites: 0, nativeRequests: 0 } + } finally { + human?.release() + await database.end() + await pool.query(`DROP SCHEMA ${schema} CASCADE`) + } +} diff --git a/backend/src/modules/mcp/routing-controller-fence.test.ts b/backend/src/modules/mcp/routing-controller-fence.test.ts new file mode 100644 index 00000000..0da5de6b --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-fence.test.ts @@ -0,0 +1,64 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import type { Pool } from "pg" +import { withControllerProfileLease } from "./routing-controller-fence.js" + +function fixture(options: { locked?: boolean; unlocked?: boolean; unlockError?: boolean; acquireError?: boolean } = {}) { + const queries: Array<{ sql: string; values: unknown[] }> = [] + const releases: unknown[] = [] + const database = { connect: async () => ({ + query: async (sql: string, values: unknown[]) => { + queries.push({ sql, values }) + if (sql.includes("pg_try_advisory_lock")) { + if (options.acquireError) throw new Error("lock response lost") + return { rows: [{ locked: options.locked ?? true }] } + } + if (options.unlockError) throw new Error("connection lost during unlock") + return { rows: [{ unlocked: options.unlocked ?? true }] } + }, + release: (destroy: boolean) => { releases.push(destroy) }, + }) } as unknown as Pick + return { database, queries, releases } +} + +test("profile lease survives awaited work and unlocks exactly its own key", async () => { + const value = fixture() + const result = await withControllerProfileLease(value.database, "profile-a", async () => { + await Promise.resolve() + assert.equal(value.queries.length, 1); assert.deepEqual(value.releases, []) + return "completed" + }) + assert.equal(result, "completed") + assert.deepEqual(value.queries.map(query => query.values), [["mcp-routing-profile:profile-a"], ["mcp-routing-profile:profile-a"]]) + assert.deepEqual(value.releases, [false]) +}) + +test("busy profile is skipped without work or unlocking another session's lease", async () => { + const value = fixture({ locked: false }) + let called = false + assert.equal(await withControllerProfileLease(value.database, "profile-a", async () => { called = true }), undefined) + assert.equal(called, false); assert.equal(value.queries.length, 1) + assert.deepEqual(value.releases, [false]) +}) + +test("failed tick releases the lease and preserves its error", async () => { + const value = fixture() + const failure = new Error("tick failed") + await assert.rejects(withControllerProfileLease(value.database, "profile-a", async () => { throw failure }), error => error === failure) + assert.equal(value.queries.length, 2); assert.deepEqual(value.releases, [false]) +}) + +test("unconfirmed unlock destroys the checked-out connection", async () => { + for (const options of [{ unlocked: false }, { unlockError: true }]) { + const value = fixture(options) + await withControllerProfileLease(value.database, "profile-a", async () => true) + assert.deepEqual(value.releases, [true]) + } +}) + +test("uncertain lock acquisition destroys the connection without entering work", async () => { + const value = fixture({ acquireError: true }) + let called = false + await assert.rejects(withControllerProfileLease(value.database, "profile-a", async () => { called = true }), /lock response lost/) + assert.equal(called, false); assert.deepEqual(value.releases, [true]) +}) diff --git a/backend/src/modules/mcp/routing-controller-fence.ts b/backend/src/modules/mcp/routing-controller-fence.ts new file mode 100644 index 00000000..a2fc15a1 --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-fence.ts @@ -0,0 +1,45 @@ +import type { Pool, PoolClient } from "pg" + +export type ControllerProfileFence = { id: string; version: number; connection_id: string } +type Sql = Pick + +/** A prepared create may never become an update, even on a same-owner ID collision. */ +export async function saveControllerProfileCas(db: Sql, input: Record) { + const values = [input.profileId, input.connectionId, JSON.stringify(input.profile), input.enabled, input.expiresAt, input.credentialId, input.generation, input.policyVersion, JSON.stringify(input.enabled ? { baseline: input.baseline } : {})] + const result = input.expectedVersion === null + ? await db.query(`INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version,state) + VALUES($1,$2,$3::jsonb,$4,$5,$6,$7,$8,$9::jsonb) ON CONFLICT(id) DO NOTHING RETURNING id,version`, values) + : await db.query(`UPDATE mcp_routing_profiles SET config=$3::jsonb,enabled=$4,grant_expires_at=$5,credential_id=$6,generation=$7, + policy_version=$8,version=version+1,state=$9::jsonb || jsonb_build_object('switches',COALESCE(state->'switches','[]'::jsonb)) || CASE WHEN state ? 'lastSwitchAt' THEN jsonb_build_object('lastSwitchAt',state->'lastSwitchAt') ELSE '{}'::jsonb END,updated_at=now() WHERE id=$1 AND connection_id=$2 AND version=$10 RETURNING id,version`, [...values, input.expectedVersion]) + return result.rows[0] as { id: string; version: number } | undefined +} + +/** Human saves increment version; a direct emergency pause also invalidates enabled. */ +export async function controllerProfileCurrent(db: Sql, row: ControllerProfileFence) { + return (await db.query("SELECT id FROM mcp_routing_profiles WHERE id=$1 AND connection_id=$2 AND version=$3 AND enabled", [row.id, row.connection_id, row.version])).rowCount === 1 +} + +export async function publishControllerState(db: Sql, row: ControllerProfileFence, state: unknown, pause = false) { + return (await db.query(`UPDATE mcp_routing_profiles SET state=$4::jsonb,enabled=CASE WHEN $5 THEN false ELSE enabled END,updated_at=now() + WHERE id=$1 AND connection_id=$2 AND version=$3 AND enabled RETURNING id`, [row.id, row.connection_id, row.version, JSON.stringify(state), pause])).rowCount === 1 +} + +/** Session lock covers the whole tick, including awaits, and is never pooled while held. */ +export async function withControllerProfileLease(database: Pick, profileId: string, work: (db: PoolClient) => Promise): Promise { + const lease = await database.connect() + let locked = false, unsafe = false + try { + try { + locked = (await lease.query("SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS locked", ["mcp-routing-profile:" + profileId])).rows[0]?.locked === true + } catch (error) { unsafe = true; throw error } + if (!locked) return undefined + return await work(lease) + } finally { + if (locked) { + try { + unsafe = (await lease.query("SELECT pg_advisory_unlock(hashtextextended($1,0)) AS unlocked", ["mcp-routing-profile:" + profileId])).rows[0]?.unlocked !== true + } catch { unsafe = true } + } + lease.release(unsafe) + } +} diff --git a/backend/src/modules/mcp/routing-controller-overlap.test.ts b/backend/src/modules/mcp/routing-controller-overlap.test.ts new file mode 100644 index 00000000..653387ea --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-overlap.test.ts @@ -0,0 +1,73 @@ +import assert from "node:assert/strict" +import test from "node:test" +import { readFile } from "node:fs/promises" +import { randomUUID } from "node:crypto" +import ts from "typescript" +import { z } from "zod" +import { routeAiControllerBlockReason, routeAiProfileSchema, usesScopedRouteAiContext } from "@mmapp/contracts/mcp-routing" +import { fingerprint, McpError } from "./security.js" + +type Handler = { prepare?: (input: Record, context: unknown) => Promise>; execute?: (input: Record) => Promise } +async function load(name: string, providers: Record) { + const exports: Record = {} + const source = await readFile(new URL(name, import.meta.url), "utf8") + new Function("exports", "require", ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText)(exports, (key: string) => { assert.ok(providers[key], key); return providers[key] }) + return exports +} +/** Actual overlap and actual profile action bodies; baseline/authority/CAS are + * explicit inert adapters. SQL predicates separately run on owned PostgreSQL. */ +async function fixture() { + const profile = routeAiProfileSchema.parse({ name: "overlap", serverId: 1, family: "ipv4", community: "1:1", candidates: [{ id: "a", wanInterface: "wan", downstreamInterface: "gre", jumpHostId: 2, exitNodeId: 3, gateway: "192.0.2.1", gatewayRef: { resource: "gre", id: "*1" } }] }) + const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } + const connection = { id: principal.connectionId, owner_id: "owner", owner_valid_until: new Date(principal.credentialExpiresAt), policy: { version: 1, expiresAt: principal.credentialExpiresAt, scope: { operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify"] } } } + let conflict = false, overlapReads = 0, casWrites = 0, baselinePreparations = 0 + let lastParameters: unknown[] = [] + const grantTimes: string[] = [] + const pool = { query: async (sql: string, values: unknown[] = []) => { + if (sql.startsWith("SELECT id FROM mcp_routing_profiles WHERE enabled")) { overlapReads++; lastParameters = values; assert.ok(sql.includes("LIMIT 1")); assert.ok(sql.includes("id<>$1::uuid")); return { rows: conflict ? [{ id: randomUUID() }] : [] } } + if (sql.startsWith("SELECT id,connection_id,version")) return { rows: [] } + throw new Error("Unexpected unit SQL") + } } + const overlap = await load("./routing-controller-overlap.ts", { "../../db/index.js": { pool }, "@mmapp/contracts/mcp-routing": { routeAiProfileSchema }, "./security.js": { McpError } }) + const services = new Map(), operations = new Map() + const profileModule = await load("./routing-profiles.ts", { + "./routing-controller-overlap.js": overlap, "./security.js": { fingerprint, McpError, accessGeneration: async () => "g" }, + "./routing-controller-baseline.js": { + prepareRoutingControllerBaseline: async (_id: unknown, _version: unknown, _profile: unknown, expiresAt: string) => { + baselinePreparations++; grantTimes.push(z.string().datetime().parse(expiresAt)) + return { packet: {}, proof: "inert-baseline" } + }, + assertRoutingControllerBaseline: async (_baseline: unknown, _id: unknown, _version: unknown, _profile: unknown, expiresAt: string) => { grantTimes.push(expiresAt) }, + routingControllerBaselinePrincipal: async () => principal, advanceRoutingControllerBaseline: async () => {}, + }, + "./routing-community-public.js": { publicRoutingControllerState: () => ({}) }, "node:crypto": { randomUUID }, zod: { z }, "@mmapp/contracts/mcp-routing": { routeAiControllerBlockReason, routeAiProfileSchema, usesScopedRouteAiContext }, "../../db/index.js": { pool }, + "./service-registry.js": { registerMcpService: (name: string, handler: Handler) => services.set(name, handler) }, + "./database-request-lease.js": { withMcpDatabaseRequest: () => { throw new Error("Profile-only fixture must not tick the controller") } }, + "./store.js": { audit: async () => {}, validatePrincipal: async () => connection }, "./routing-model.js": { decideRoutingSwitch: () => ({}) }, + "./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") } }, + }) + const register = profileModule.registerRoutingProfileActions as (register: (...args: unknown[]) => void) => void + register((...args) => operations.set(String(args[0]), args.at(-1) as Handler)) + const input = { profileId: randomUUID(), profile, enabled: true, expiresAt: new Date(Date.now() + 120000).toISOString() } + const prepare = () => operations.get("routing.profiles.save")!.prepare!(input, { principal, connection }) + const execute = (value: Record) => services.get("routing.profile.save")!.execute!(value) + return { input, prepare, execute, conflict: (value: boolean) => { conflict = value }, reads: () => overlapReads, cas: () => casWrites, baselines: () => baselinePreparations, parameters: () => lastParameters, grantTimes } +} +function serviceInput(evidence: Record) { return (evidence.steps as Array<{ input: Record }>)[0].input } +test("enabled prepare reports role overlap before baseline issuance", async () => { const value = await fixture(); value.conflict(true); await assert.rejects(value.prepare, error => error instanceof McpError && error.code === "STATE_CONFLICT"); assert.equal(value.baselines(), 0); assert.equal(value.cas(), 0) }) +test("execute rechecks overlap committed after preparation and refuses before CAS", async () => { const value = await fixture(); const evidence = await value.prepare(); value.conflict(true); await assert.rejects(() => value.execute(serviceInput(evidence))); assert.equal(value.reads(), 2); assert.equal(value.cas(), 0) }) +test("disjoint activation prepares and executes with normal CAS", async () => { const value = await fixture(); const evidence = await value.prepare(); await value.execute(serviceInput(evidence)); assert.equal(value.reads(), 2); assert.equal(value.cas(), 1) }) +test("current profile is excluded and role IDs remain exactly home jump exit", async () => { const value = await fixture(); await value.prepare(); assert.equal(value.parameters()[0], value.input.profileId); assert.deepEqual(value.parameters()[1], ["1", "2", "3"]) }) +test("disabled saves do not acquire overlap admission or issue a baseline", async () => { const value = await fixture(); value.input.enabled = false; value.conflict(true); const evidence = await value.prepare(); await value.execute(serviceInput(evidence)); assert.equal(value.reads(), 0); assert.equal(value.baselines(), 0); assert.equal(value.cas(), 1) }) +test("enabled profiles bind offset grant expiry to the exact UTC instant before approval", async () => { + const value = await fixture() + const expiry = value.input.expiresAt + value.input.expiresAt = new Date(Date.parse(expiry) + 3 * 3600000).toISOString().replace(/Z$/, "+03:00") + const input = serviceInput(await value.prepare()) + assert.equal(input.expiresAt, expiry) + await value.execute(input) + assert.deepEqual(value.grantTimes, [expiry, expiry]) + assert.equal(value.cas(), 1) +}) diff --git a/backend/src/modules/mcp/routing-controller-overlap.ts b/backend/src/modules/mcp/routing-controller-overlap.ts new file mode 100644 index 00000000..2df8e27e --- /dev/null +++ b/backend/src/modules/mcp/routing-controller-overlap.ts @@ -0,0 +1,18 @@ +import { pool } from "../../db/index.js" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { McpError } from "./security.js" + +/** The common native guard supports only one enrolled profile per role server. + * Preparation reports overlap early; execute rechecks under its existing + * application+role-server locks immediately before profile CAS. No JSON payload + * is hydrated and no caller reader/authority provider is accepted. */ +export async function assertNoActiveRoutingProfileOverlap(profileId: string, rawProfile: unknown) { + const profile = routeAiProfileSchema.parse(rawProfile) + const ids = [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])].sort((a, b) => a - b).map(String) + const found = (await pool.query(`SELECT id FROM mcp_routing_profiles WHERE enabled AND id<>$1::uuid AND ( + config IS NULL OR jsonb_typeof(config) IS DISTINCT FROM 'object' OR jsonb_typeof(config->'candidates') IS DISTINCT FROM 'array' + OR config->>'serverId'=ANY($2::text[]) OR EXISTS( + SELECT 1 FROM jsonb_array_elements(CASE WHEN jsonb_typeof(config->'candidates')='array' THEN config->'candidates' ELSE '[]'::jsonb END) candidate + WHERE candidate->>'jumpHostId'=ANY($2::text[]) OR candidate->>'exitNodeId'=ANY($2::text[]))) LIMIT 1`, [profileId, ids])).rows + if (found.length) throw new McpError("STATE_CONFLICT", "Устройства пути уже заняты активным профилем маршрутизации; приостановите пересекающийся профиль", 409) +} diff --git a/backend/src/modules/mcp/routing-controls.ts b/backend/src/modules/mcp/routing-controls.ts new file mode 100644 index 00000000..c1cdf5a5 --- /dev/null +++ b/backend/src/modules/mcp/routing-controls.ts @@ -0,0 +1,349 @@ +import { readRoutingTopology } from "../../services/routing-topology-observation.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { z } from "zod" +import { dedupeVrfAliasSnapshots } from "../../services/vrf-production-action-binding.js" +import { pool } from "../../db/index.js" +import { hasManagedCommentPrefix } from "../../managed-markers.js" +import { configurationRows, readRosSnapshot, readRoutingPolicySnapshot, resourceById, routerClient, rosSnapshotSourceProof, type ConfigurationRow, type RosSnapshot } from "./resources.js" +import { readRouterOsSnapshotGet } from "../../services/routeros-snapshot-read.js" +import { McpError, fingerprint } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import { hasPermission } from "../../lib/permissions.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import type { add } from "./actions.js" +import type { ChangeEvidence } from "./action-types.js" +import { catalogPaging, catalogDevices } from "./catalog-scope.js" +import { readOspfCatalogForServer } from "../../services/ospf-state-read.js" +import { pollOspfCatalog } from "../../services/ospf-catalog-poll.js" +import { pollBgpCatalog } from "../../services/bgp-catalog-poll.js" +import { readBgpSessionsForServer, readBgpCatalogForServer, readRawBgpSessionsForServer } from "../../services/bgp-session-read.js" +import { pollGreCatalog } from "../../services/gre-catalog-poll.js" +import { fetchServerFilters, readFilterRulesCatalog } from "../../routes/filters.js" +import { readRecursiveCatalogForServer, readRecursiveGatewayCatalogForServer, readRecursiveRoutesForServer, readRecursiveGatewaysForServer } from "../../routes/recursive-routes.js" +import { analyzeEcmpPreflight, confirmedEcmpInterfaces, confirmedEcmpTable, duplicateEcmpAdjacency, ecmpAdjacency, preflightPaths, preflightObservation, type PreflightObservation, type PreflightSource } from "./ecmp-preflight.js" +import { readLatestInternetPathSnapshot } from "../../services/internet-path-read.js" +import { projectInternetPathObservation } from "../../services/internet-path-observation.js" +import { readOspfOptimizationPreview } from "../../routes/ospf.js" + +const serverId = z.number().int().positive(), objectId = z.string().min(1).max(100) +const text = z.string().min(1).max(100), family = z.enum(["ipv4", "ipv6"]) +const bool = (value: unknown) => value === true || value === "true" || value === "yes" + +export function checkRpfPolicy(serverId: number, snapshots: RosSnapshot[], mode: string) { + const find = (resource: string) => snapshots.find(snapshot => snapshot.serverId === serverId && snapshot.resource === resource) + const tables = find("routing_table"), vrfs = find("vrf"), rules = find("routing_policy:routing_rule"), mangle = find("routing_policy:ip_mangle") + if (!tables || !vrfs || !rules || !mangle || !find("ipv4_route")) { + throw new McpError("PLAN_EXPIRED", "План не содержит актуальные зависимости RPF; подготовьте его заново", 409) + } + const policyRouting = rules.rows.some(row => !bool(row.disabled)) + || mangle.rows.some(row => !bool(row.disabled) && (row.action === "mark-routing" || typeof row.action !== "string" || !row.action.trim())) + const separateTables = [...tables.rows, ...vrfs.rows].some(row => row.name !== "main") + if (mode === "strict" && (policyRouting || separateTables)) throw new McpError("STATE_CONFLICT", "Strict RPF требует main без отдельных таблиц, VRF и действующих правил policy routing", 409) + return policyRouting || separateTables +} + +export async function readRpfDependencies(serverId: number, mode: string, sourceProof: string | undefined) { + if (!sourceProof) throw new McpError("STATE_CONFLICT", "Источник IPv4-настроек RPF не подтверждён", 409) + const [tables, vrfs, routes] = await Promise.all( + ["routing_table", "vrf", "ipv4_route"].map(resource => readRosSnapshot(serverId, resource, undefined, sourceProof)), + ) + const snapshots = [tables, vrfs, routes] + const warnings = ["RPF является IPv4-настройкой. Изменение может затронуть асимметричный трафик; снижение защиты требует компенсирующих правил."] + if (mode !== "no") { + const [rules, mangle] = await Promise.all(["routing_rule", "ip_mangle"].map(resource => readRoutingPolicySnapshot(serverId, resource, sourceProof))) + snapshots.push(rules, mangle) + if (checkRpfPolicy(serverId, snapshots, mode)) warnings.push("Обнаружена policy routing или отдельные таблицы. Проверьте обратный путь для трафика в каждой затронутой таблице.") + } + return { snapshots, warnings } +} + +export function registerRoutingControls(register: typeof add) { + register("routing.ospf.application.preview","mm_ospf_application_preview","Модельный OSPF preview приложения с отдельными наблюдаемыми метриками","network","mm:network:read","read","servers",z.object({serverId:z.number().int().positive().safe(),pingWeight:z.number().int().min(0).max(100).default(60),peerServerIds:z.array(z.number().int().positive().safe()).max(10).refine(ids=>new Set(ids).size===ids.length).optional(),offset:z.number().int().min(0).max(100000).default(0),limit:z.number().int().min(1).max(200).default(100)}).strict(),{ + read:async(input,context)=>{ + const deadlineAt=Date.now()+45000,signal=AbortSignal.any([AbortSignal.timeout(45000),...(context.signal?[context.signal]:[])]) + const expectedPolicy=fingerprint(context.connection.policy) + const assertAccess=async()=>{ + if(signal.aborted)throw new McpError("READ_CANCELLED","Чтение OSPF отменено или превысило срок",409) + const current=await effectiveMcpConnection(context.principal) + if(fingerprint(current.policy)!==expectedPolicy||!hasPermission(current.owner_permissions,"mm:network:read")||!hasPermission(current.policy.permissions,"mm:network:read"))throw new McpError("STATE_CONFLICT","Доступ к OSPF изменился во время чтения",409) + if(signal.aborted)throw new McpError("READ_CANCELLED","Чтение OSPF отменено или превысило срок",409) + } + await assertAccess() + const allowed=context.connection.policy.scope.serverIds + const selected=[...new Set([Number(input.serverId),...(input.peerServerIds as number[]|undefined??allowed)])].sort((a,b)=>a-b) + if(selected.some(id=>!allowed.includes(id)))throw new McpError("SERVER_DENIED","Сосед OSPF вне разрешённой области",403) + if(selected.length>10)throw new McpError("LIMIT_EXCEEDED","Выберите через peerServerIds не более 10 устройств вместе с источником",409) + const devices=(await pool.query<{id:number;enabled:boolean}>("SELECT id,enabled FROM servers WHERE id=ANY($1::bigint[])",[selected])).rows + if(devices.length!==selected.length)throw new McpError("NOT_FOUND","Выбранное устройство OSPF отсутствует в каталоге",404) + const {server}=await routerClient(Number(input.serverId)) + try{ + const result=await readOspfOptimizationPreview(server,Number(input.pingWeight),{serverIds:selected,signal,deadlineAt,beforeRequest:async source=>{ + await assertAccess() + const fresh=await getServerRowById(source.id) + if(!fresh?.enabled||fingerprint(fresh)!==fingerprint(source))throw new McpError("STATE_CONFLICT","Источник OSPF изменился во время чтения",409) + if(signal.aborted)throw new McpError("READ_CANCELLED","Чтение OSPF отменено или превысило срок",409) + }}) + await assertAccess() + const offset=Number(input.offset),limit=Number(input.limit) + const view=(rows:typeof result.interfaces)=>{const items=rows.slice(offset,offset+limit).map(({id,interface:iface,currentCost,observed,...modeled})=>({id,interface:iface,currentCost,observed:observed??null,modeled}));return{items,total:rows.length,nextOffset:offset+limit!row.enabled).map(row=>row.id),peerSelectionCoversGrant:selected.length===allowed.length, + proposalOnly:true,modelContainsAssumptions:true,metricContextVerified:false,executionAvailable:false,newApprovedPlanRequired:true,forwardingVerified:false, + incomplete:interfaces.items.length { + const {selected,...page}=await catalogDevices(input,context,{includeDisabled:true}) + const row=await readLatestInternetPathSnapshot(selected.map(server=>server.id)) + if(!row)return {...page,snapshot:null,source:"stored-application-interpretation",live:false,forwardingVerified:false} + try { + const snapshot=projectInternetPathObservation(row,selected.map(server=>server.id),Number(input.offset),Number(input.limit),Number(input.maxAgeSec)) + return {...page,snapshot,observedAt:new Date().toISOString(),incomplete:page.incomplete||snapshot.incomplete} + } catch {throw new McpError("STATE_UNAVAILABLE","Сохранённый Internet path выбранной области не подтверждён",503)} + }, + }) + register("routing.filters.catalog.read", "mm_bgp_filter_rules_get", "BGP-in: сервисный каталог правил и явный cache fallback", "filters", "mm:filters:read", "read", "servers", catalogPaging.safeExtend({ source: z.enum(["live", "cache"]).default("live") }), { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context, { includeDisabled: input.source === "cache" }) + const rows = await readFilterRulesCatalog(selected, { source: input.source as "live" | "cache" }) + const offset = Number(input.offset), limit = Number(input.limit) + const itemsPage = (items: T[]) => ({ items: items.slice(offset, offset + limit), total: items.length, nextOffset: offset + limit < items.length ? offset + limit : null }) + const rulesets = rows.map(row => ({ serverId: row.serverId, live: row.live, stale: row.stale, source: row.source, error: row.error, + rules: itemsPage(row.rules), nativeRules: row.nativeRules === null ? null : itemsPage(row.nativeRules), + greTunnels: itemsPage(row.greTunnels.map(tunnel => ({ id: tunnel.id, name: tunnel.name, serverId: tunnel.serverId, localAddress: tunnel.localAddress, remoteAddress: tunnel.remoteAddress }))), + cacheFreshnessVerified: false, recursiveReferencesVerified: false, ruleEffectsVerified: false, + })) + return { ...page, rulesets, incomplete: page.incomplete || rulesets.some(row => row.error !== null || [row.rules, row.greTunnels, ...(row.nativeRules ? [row.nativeRules] : [])].some(value => value.items.length < value.total)), + observedAt: new Date().toISOString(), writesPerformed: false, annotationsSource: "manager-metadata-or-legacy-native-comments", parsedRulesMeaning: "heuristic business hints, including disabled/unknown native rows; not effective routing policy", + nativeOrderMeaning: "relative bgp-in order only; does not evaluate chain jumps", greDetailsTool: "mm_gre_state_get", forwardingVerified: false, prefixExchangeVerified: false } + }, + }) + register("routing.ecmp.preflight.read", "mm_ecmp_preflight_get", "Предварительная проверка ECMP: NAT/FastTrack/RPF/маркировки без записи", "network", "mm:network:read", "read", "servers", z.object({ serverId, family, routeIds: z.array(objectId).min(2).max(8).refine(ids => new Set(ids).size === ids.length) }).strict(), { + read: async input => { + const { client } = await routerClient(Number(input.serverId)), selectedFamily = input.family as "ipv4" | "ipv6" + const read = async (path: string, singleton = false): Promise => { + try { return preflightObservation(await client.get(path), singleton) } + catch { return { rows: [], error: "Устройство не подтвердило чтение ресурса" } } + } + const entries = await Promise.all(Object.entries(preflightPaths(selectedFamily)).map(async ([key, path]) => [key, await read(path, key === "settings" || key === "connectionTracking")] as const)) + const observed = Object.fromEntries(entries) as Record + const ids = input.routeIds as string[] + const needsInventory = observed.routes.rows.some(row => ids.includes(String(row[".id"])) && typeof row["immediate-gw"] === "string" && row["immediate-gw"].length > 0 && !ecmpAdjacency(row["immediate-gw"])) + const inventory = needsInventory ? await read("/interface") : undefined + return { serverId: input.serverId, observedAt: new Date().toISOString(), ...analyzeEcmpPreflight(selectedFamily, ids, observed, inventory) } + }, + }) + const recursivePaging = z.object({ serverId, offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(200).default(100) }).strict() + register("routing.recursive.routes.read", "mm_recursive_routes_get", "Рекурсивные маршруты приложения с явным признаком устаревшего кеша", "network", "mm:network:read", "read", "servers", recursivePaging, { + read: async input => { + const { server } = await routerClient(Number(input.serverId)) + const result = await readRecursiveRoutesForServer(server, undefined, { persistObserved: false }) + const offset = Number(input.offset), limit = Number(input.limit) + const routes = result.routes.slice(offset, offset + limit) + return { ...result, routes, total: result.routes.length, nextOffset: offset + limit < result.routes.length ? offset + limit : null, observedAt: new Date().toISOString(), incomplete: result.stale || routes.length < result.routes.length, forwardingVerified: false } + }, + }) + register("routing.recursive.gateways.read", "mm_recursive_gateways_get", "Шлюзы приложения с up/down/unknown без предположений о передаче пакетов", "network", "mm:network:read", "read", "servers", recursivePaging, { + read: async input => { + const { server } = await routerClient(Number(input.serverId)) + const result = await readRecursiveGatewaysForServer(server) + const offset = Number(input.offset), limit = Number(input.limit) + const gateways = result.gateways.slice(offset, offset + limit) + return { ...result, gateways, total: result.gateways.length, nextOffset: offset + limit < result.gateways.length ? offset + limit : null, observedAt: new Date().toISOString(), incomplete: gateways.length < result.gateways.length, forwardingVerified: false } + }, + }) + register("routing.recursive.catalog.read", "mm_recursive_routes_catalog_get", "Подтверждённый каталог управляемых рекурсивных маршрутов", "network", "mm:network:read", "read", "servers", recursivePaging, { + read: async input => { + const { server } = await routerClient(Number(input.serverId)) + const result = await readRecursiveCatalogForServer(server, undefined, { persistObserved: false }) + const offset = Number(input.offset), limit = Number(input.limit) + const routes = result.routes.slice(offset, offset + limit) + return { ...result, routes, total: result.routes.length, nextOffset: offset + limit < result.routes.length ? offset + limit : null, observedAt: new Date().toISOString(), incomplete: routes.length < result.routes.length } + }, + }) + register("routing.recursive.gateways.catalog.read", "mm_recursive_gateways_catalog_get", "Подтверждённые шлюзы с active/inactive/unknown", "network", "mm:network:read", "read", "servers", recursivePaging, { + read: async input => { + const { server } = await routerClient(Number(input.serverId)) + const result = await readRecursiveGatewayCatalogForServer(server) + const offset = Number(input.offset), limit = Number(input.limit) + const gateways = result.gateways.slice(offset, offset + limit) + return { ...result, gateways, total: result.gateways.length, nextOffset: offset + limit < result.gateways.length ? offset + limit : null, observedAt: new Date().toISOString(), incomplete: gateways.length < result.gateways.length } + }, + }) + register("routing.gre.catalog.read", "mm_gre_catalog_get", "Каталог GRE для выбора путей маршрутизации", "filters", "mm:filters:read", "read", "servers", catalogPaging, { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context) + const result = await pollGreCatalog(selected, fetchServerFilters) + const offset = Number(input.offset), limit = Number(input.limit) + const tunnels = result.tunnels.slice(offset, offset + limit) + return { ...page, tunnels, total: result.tunnels.length, nextOffset: offset + limit < result.tunnels.length ? offset + limit : null, failures: result.failures, observedAt: new Date().toISOString(), incomplete: page.incomplete || result.failures.length > 0 || tunnels.length < result.tunnels.length } + }, + }) + register("routing.bgp.catalog.read", "mm_bgp_catalog_get", "Каталог BGP приложения с ограничением серверов до опроса", "bgp", "mm:bgp:read", "read", "servers", catalogPaging, { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context) + const result = await pollBgpCatalog(selected, readBgpCatalogForServer) + const offset = Number(input.offset), limit = Number(input.limit) + const items = result.sessions.slice(offset, offset + limit) + return { ...page, items, total: result.sessions.length, nextOffset: offset + limit < result.sessions.length ? offset + limit : null, failures: result.failures, observedAt: new Date().toISOString(), incomplete: page.incomplete || result.failures.length > 0 || items.length < result.sessions.length } + }, + }) + register("routing.bgp.sessions.read", "mm_bgp_sessions_get", "BGP-сессии приложения: состояние и счётчики с сохранением ошибок", "bgp", "mm:bgp:read", "read", "servers", catalogPaging, { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context) + const result = await pollBgpCatalog(selected, readBgpSessionsForServer) + const offset = Number(input.offset), limit = Number(input.limit) + return { ...page, sessions: result.sessions.slice(offset, offset + limit), total: result.sessions.length, nextOffset: offset + limit < result.sessions.length ? offset + limit : null, failures: result.failures, + observedAt: new Date().toISOString(), incomplete: page.incomplete || result.failures.length > 0 || result.sessions.slice(offset, offset + limit).length < result.sessions.length } + }, + }) + register("routing.bgp.sessions.raw.read", "mm_bgp_sessions_raw_get", "Диагностический ответ BGP выбранного сервера с маскированием секретов", "bgp", "mm:bgp:read", "read", "servers", z.object({ serverId, offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(200).default(100) }).strict(), { + read: async input => { + const { server } = await routerClient(Number(input.serverId)) + const raw = await readRawBgpSessionsForServer(server), offset = Number(input.offset), limit = Number(input.limit) + return { server: { id: server.id, host: server.host, name: server.name }, raw: raw.slice(offset, offset + limit), total: raw.length, nextOffset: offset + limit < raw.length ? offset + limit : null, + incomplete: raw.slice(offset, offset + limit).length < raw.length, observedAt: new Date().toISOString(), forwardingVerified: false } + }, + }) + register("routing.ospf.catalog.read", "mm_ospf_catalog_get", "Каталог OSPF/BFD приложения с ограниченной областью опроса", "network", "mm:network:read", "read", "servers", catalogPaging, { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context) + const result = await pollOspfCatalog(selected, readOspfCatalogForServer) + const entries = ["neighbors", "interfaces", "instances", "bfdSessions", "routes"] as const + const offset = Number(input.offset), limit = Number(input.limit) + const collections = Object.fromEntries(entries.map(key => [key, { items: result[key].slice(offset, offset + limit), total: result[key].length, nextOffset: offset + limit < result[key].length ? offset + limit : null }])) + return { ...page, collections, failures: result.failures, observedAt: new Date().toISOString(), incomplete: result.failures.length > 0 || page.incomplete || Object.values(collections).some(collection => collection.items.length < collection.total) } + }, + }) + register("routing.topology.read", "mm_routing_topology_get", "Наблюдаемая конфигурация маршрутизации", "network", "mm:network:read", "read", "servers", z.object({ serverId }).strict(), { + read: async (input, context) => { + const sid = Number(input.serverId) + if (!context.connection.policy.scope.serverIds.includes(sid)) throw new McpError("SERVER_DENIED", "Источник топологии вне разрешённой области", 403) + const signal = AbortSignal.any([AbortSignal.timeout(30000), ...(context.signal ? [context.signal] : [])]) + const authorize = async () => { + if (signal.aborted) throw new McpError("READ_CANCELLED", "Чтение топологии отменено или превысило срок", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:read") || !hasPermission(current.policy.permissions, "mm:network:read")) throw new McpError("PERMISSION_DENIED", "Право чтения сети отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) throw new McpError("RESET_REQUIRED", "Допуск изменился; повторите чтение топологии", 409) + } + await authorize() + const source = await createRoutingSourceContext(await readRoutingEndpoints([sid])) + if (source.expected.length !== 1) throw new McpError("DEVICE_UNAVAILABLE", "Источник топологии отсутствует или выключен", 409) + const beforeRead = async () => { + await authorize(); await source.assertCurrent() + if (signal.aborted) throw new McpError("READ_CANCELLED", "Чтение топологии отменено или превысило срок", 409) + } + await beforeRead() + const { client } = await routerClient(sid) + return { serverId: sid, ...await readRoutingTopology(client, beforeRead, signal) } + }, + }) + register("routing.ecmp.update", "mm_ecmp_plan", "Равная стоимость маршрутов ECMP и hash policy", "network", "mm:network:write", "write", "servers", z.object({ serverId, family, routeIds: z.array(objectId).min(2).max(8).refine(ids => new Set(ids).size === ids.length), distance: z.number().int().min(1).max(255).default(1), hashPolicy: z.enum(["l3", "l4", "l3-inner"]).optional() }).strict(), { + version: 8, + prepare: async input => { + const sid = Number(input.serverId), ipv4 = input.family === "ipv4", resource = ipv4 ? "ipv4_route" : "ipv6_route" + const base = ipv4 ? "ip" : "ipv6", settingsResource = `${base}_settings` + const { client, server } = await routerClient(sid) + const sourceProof = await rosSnapshotSourceProof(server) + const assertSource = async () => { + if (await rosSnapshotSourceProof((await routerClient(sid)).server) !== sourceProof) { + throw new McpError("STATE_CONFLICT", "Источник маршрутов ECMP изменился при подготовке", 409) + } + } + const read = (resourceId: string) => readRosSnapshot(sid, resourceId, undefined, sourceProof) + const readNative = async (path: string) => { + const value = await readRouterOsSnapshotGet({ client, path, assertCurrent: assertSource }) + await assertSource() + return preflightObservation(value) + } + const snapshot = await read(resource), ids = input.routeIds as string[] + const observed = await readNative(`/${base}/route`) + if (observed.error) throw new McpError("MEASUREMENT_UNKNOWN", observed.error, 409) + if (fingerprint(configurationRows(resourceById(resource), observed.rows)) !== snapshot.fingerprint) { + throw new McpError("STATE_CONFLICT", "Каталог маршрутов ECMP изменился при подготовке", 409) + } + const raw = observed.rows + const rows = ids.map(id => raw.find(row => row[".id"] === id)) + if (rows.some(row => !row || bool(row.dynamic) || bool(row.disabled) || bool(row.invalid) || !hasManagedCommentPrefix(String(row.comment ?? "")))) throw new McpError("PROTECTED_OBJECT", "ECMP требует разрешаемые статические маршруты приложения") + const routes = rows as ConfigurationRow[], first = routes[0] + if (routes.some(row => row["dst-address"] !== first["dst-address"] || String(row["routing-table"] ?? "main") !== String(first["routing-table"] ?? "main"))) throw new McpError("STATE_CONFLICT", "ECMP требует одинаковые prefix и routing table", 409) + const gateways = routes.map(row => row.gateway) + if (gateways.some(gateway => typeof gateway !== "string" || !gateway.trim()) || new Set(gateways).size !== ids.length) throw new McpError("MEASUREMENT_UNKNOWN", "Нужны разные подтверждённые gateway", 409) + const immediate = routes.map(row => row["immediate-gw"]) + let hops = immediate.map(value => ecmpAdjacency(value)) + let interfaceIds: Map | undefined + if (hops.some(hop => hop === null)) { + const inventory = await readNative("/interface") + if (inventory.error) throw new McpError("MEASUREMENT_UNKNOWN", inventory.error, 409) + interfaceIds = confirmedEcmpInterfaces(inventory.rows) + const names = new Set(interfaceIds.keys()) + hops = immediate.map(value => ecmpAdjacency(value, names)) + } + if (hops.some(hop => hop === null)) throw new McpError("MEASUREMENT_UNKNOWN", "Не подтверждён одиночный immediate gateway каждого маршрута", 409) + if (duplicateEcmpAdjacency(hops)) throw new McpError("STATE_CONFLICT", "Маршруты ECMP разрешаются в один immediate gateway", 409) + const steps: ChangeEvidence["steps"] = ids.map(id => ({ kind: "ros", step: { serverId: sid, resource, id, action: "update", fields: { distance: String(input.distance) } } })) + const snapshots = [snapshot] + const tableSnapshot = await read("routing_table") + const table = confirmedEcmpTable(tableSnapshot.rows, String(first["routing-table"] ?? "main")) + if (!table) { + throw new McpError("STATE_CONFLICT", "Для ECMP нужна однозначно определённая активная таблица FIB", 409) + } + snapshots.push(tableSnapshot) + const objects: ChangeEvidence["objects"] = [...ids.map(id => ({ kind: resource, id, serverId: sid })), { kind: "routing_table", id: table.id, serverId: sid }] + const [settings, vrf, ...impacts] = await Promise.all([ + read(settingsResource), + read("vrf"), + ...[`${base}_filter`, `${base}_nat`, `${base}_mangle`, `${base}_raw`, "routing_rule"].map(resource => readRoutingPolicySnapshot(sid, resource, sourceProof)), + ]) + snapshots.push(settings, vrf, ...impacts) + if (input.hashPolicy) { + const key = ipv4 ? "ipv4-multipath-hash-policy" : "multipath-hash-policy" + if (!(key in (settings.rows[0] ?? {}))) throw new McpError("UNSUPPORTED_CAPABILITY", "Устройство не подтвердило выбранную hash policy", 409) + steps.push({ kind: "ros", step: { serverId: sid, resource: settingsResource, action: "update", fields: { [key]: String(input.hashPolicy) } } }) + objects.push({ kind: settingsResource, id: "singleton", serverId: sid }, { kind: settingsResource, id: "new", serverId: sid }) + } + await assertSource() + const healthChecks = routes.flatMap((row, index) => { + const hop = hops[index], name = hop?.address === null ? hop.interface : null + const interfaceId = name ? interfaceIds?.get(name) : undefined + const nextHopInterface = name && interfaceId ? { name, id: interfaceId } : undefined + return [{ serverId: sid, resource, id: String(row[".id"]), field: "resolved" as const, identity: { gateway: String(row.gateway), "immediate-gw": String(row["immediate-gw"]) }, nextHopInterface }, + { serverId: sid, resource, id: String(row[".id"]), field: "active" as const, phase: "after" as const }] + }) + return { target: { scope: "servers", serverIds: [sid] }, objects, snapshots, steps, requiresHuman: true, healthChecks, + warnings: ["Равные стоимости проверяются как конфигурация. Распределение реального трафика требует отдельного наблюдения.", ipv4 ? "Проверьте NAT, FastTrack, маркировки и асимметрию. Настройка RPF сохраняется." : "Проверьте IPv6 NAT, маркировки, правила маршрутизации и асимметрию."] } + }, + }) + register("routing.rpf.update", "mm_rpf_plan", "Явное изменение IPv4 reverse path filter", "network", "mm:network:write", "write", "servers", z.object({ serverId, family: family.default("ipv4"), mode: z.enum(["no", "disabled", "loose", "strict"]), acknowledgeAsymmetry: z.literal(true) }).strict(), { + version: 5, + prepare: async input => { + if (input.family === "ipv6") throw new McpError("UNSUPPORTED_CAPABILITY", "RouterOS rp-filter доступен только для IPv4", 409) + const sid = Number(input.serverId) + const mode = input.mode === "disabled" ? "no" : String(input.mode) + const settings = await readRosSnapshot(sid, "ip_settings") + if (!("rp-filter" in (settings.rows[0] ?? {}))) throw new McpError("UNSUPPORTED_CAPABILITY", "RPF не подтверждён устройством", 409) + const { snapshots, warnings } = await readRpfDependencies(sid, mode, settings.sourceProof) + return { target: { scope: "servers", serverIds: [sid] }, objects: [{ kind: "server", id: String(sid), serverId: sid }, { kind: "ip_settings", id: "singleton", serverId: sid }, { kind: "ip_settings", id: "new", serverId: sid }], snapshots: [settings, ...snapshots], steps: [{ kind: "ros", step: { serverId: sid, resource: "ip_settings", action: "update", fields: { "rp-filter": mode } } }], requiresHuman: true, warnings } + }, + }) + register("routing.vrf.update", "mm_vrf_plan", "Проверенный состав существующего VRF", "network", "mm:network:write", "write", "servers", z.object({ serverId, id: objectId, interfaces: z.array(text).min(1).max(100).refine(values => new Set(values).size === values.length) }).strict(), { + version: 4, + prepare: async (input, context) => { + const sid = Number(input.serverId), { client } = await routerClient(sid), snapshot = await readRosSnapshot(sid, "vrf"), current = snapshot.rows.find(row => row[".id"] === input.id) + if (!current || current.name === "main" || !hasManagedCommentPrefix(String(current.comment ?? ""))) throw new McpError("PROTECTED_OBJECT", "Изменять состав можно только у существующего VRF приложения") + const interfaces = input.interfaces as string[], inventory = await client.get("/interface") + if (interfaces.some(name => name === "all" || !inventory.some(row => row.name === name))) throw new McpError("NOT_FOUND", "Интерфейс VRF отсутствует или указан selector all", 400) + if (snapshot.rows.some(row => row[".id"] !== input.id && row.name !== "main" && String(row.interfaces).split(",").some(name => interfaces.includes(name)))) throw new McpError("STATE_CONFLICT", "Интерфейс уже назначен другому VRF", 409) + const dependencies = await Promise.all(["ipv4_address", "ipv6_address", "routing_rule", "routing_table", "ospf_template", "gre", "vlan", "bridge_port"].map(resource => readRosSnapshot(sid, resource))) + const bindings = await pool.query("SELECT count(*)::int AS count FROM user_interface_bindings WHERE server_id=$1", [sid]).catch(() => { throw new McpError("STATE_UNAVAILABLE", "Не удалось проверить пользовательские зависимости", 503) }) + const native = await (await import("./actions.js")).findAction("mm_vrf_update_plan").prepare!({ serverId: sid, id: String(input.id), productionAdmission: true, fields: { interfaces: interfaces.join(",") } }, context) + const affected = new Set([...interfaces, ...String(current.interfaces ?? "").split(",")]) + return { ...native, objects: [...native.objects, ...inventory.filter(row => affected.has(String(row.name))).map(row => ({ kind: "interface", id: String(row[".id"]), serverId: sid }))], snapshots: dedupeVrfAliasSnapshots([snapshot, ...native.snapshots, ...dependencies]), + warnings: [...native.warnings, `Привязок пользователей на устройстве: ${bindings.rows[0].count}. Проверьте их назначение.`, "Порядок VRF сохраняется. Показаны снимки адресов, правил, OSPF и туннелей; route leaking не добавляется.", "Независимый management-канал и проверки из обоих VRF требуют подтверждения человеком."] } + }, + }) +} diff --git a/backend/src/modules/mcp/routing-dependencies.ts b/backend/src/modules/mcp/routing-dependencies.ts new file mode 100644 index 00000000..1cb4a00b --- /dev/null +++ b/backend/src/modules/mcp/routing-dependencies.ts @@ -0,0 +1,278 @@ +import { isIP } from "node:net" +import { ospfFilterFields, readRosSnapshot, readRoutingPolicySnapshot, type ConfigurationRow, type RosSnapshot, type RosStep } from "./resources.js" +import { McpError } from "./security.js" +import type { ChangeEvidence } from "./action-types.js" +import { gatewayReferencesTable, isDynamicRouteInsideTable } from "../../services/routing-gateway-reference.js" +import { resolveOspfRouterIdRow } from "../../services/ospf-identity-catalog.js" +import { canonicalBgpPolicyPrefix } from "../../services/bgp-community-policy-model.js" +import type { McpObjectRef } from "@mmapp/contracts/mcp" + +export const ospfInterfaceResources = ["ethernet", "bridge", "vlan", "gre", "wireguard", "vxlan"] + +export async function addOspfPrefixListDependencies(serverId: number, oldList: unknown, desiredList: unknown, snapshots: RosSnapshot[], objects: ChangeEvidence["objects"], beforeRead?: () => Promise) { + if (desiredList !== undefined && (typeof desiredList !== "string" || desiredList !== desiredList.trim())) { + throw new McpError("STATE_UNAVAILABLE", "Не подтверждено имя prefix-list OSPFv3", 409) + } + const references = new Set([oldList, desiredList].filter(value => typeof value === "string" && value)) + if (!references.size) return + const kind = "ipv6_address_list" + let snapshot = snapshots.find(row => row.resource === "routing_policy:" + kind && row.serverId === serverId) + if (!snapshot) { + snapshot = await readRoutingPolicySnapshot(serverId, kind, snapshots[0]?.sourceProof, beforeRead) + snapshots.push(snapshot) + } + let found = false + for (const row of snapshot.rows) { + if (!references.has(row.list)) continue + const id = row[".id"] + if (typeof id !== "string" || objects.length >= 1000) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены записи prefix-list OSPFv3", 409) + objects.push({ kind, id, serverId }) + if (row.list !== desiredList || [true, "true", "yes"].includes(row.disabled as string | boolean)) continue + const address = String(row.address) + try { + if (isIP(address.split("/")[0]) !== 6) throw new Error("Expected IPv6") + canonicalBgpPolicyPrefix(address.includes("/") ? address : address + "/128") + } catch { + throw new McpError("STATE_CONFLICT", "Prefix-list OSPFv3 требует явных адресов или префиксов IPv6", 409) + } + if (![null, false, "false", "no"].includes(row.dynamic as string | boolean | null) || row.timed !== false) { + throw new McpError("STATE_CONFLICT", "Prefix-list OSPFv3 содержит временную запись", 409) + } + if (![null, false, "false", "no"].includes(row.disabled as string | boolean | null)) { + throw new McpError("STATE_UNAVAILABLE", "Не подтверждена активность записи prefix-list OSPFv3", 409) + } + found = true + } + if (desiredList && !found) throw new McpError("NOT_FOUND", "Не найдены активные IPv6-записи prefix-list OSPFv3", 404) +} + +export async function resolveOspfInterfaceSelectors(serverId: number, selectors: string[], knownInterfaces: Set, snapshots: RosSnapshot[], objects: McpObjectRef[], beforeRead?: () => Promise) { + const source = snapshots[0]?.sourceProof + const read = async (kind: string) => { + const existing = snapshots.find(row => row.serverId === serverId && row.resource === kind) + if (existing) return existing + const snapshot = await readRosSnapshot(serverId, kind, beforeRead, source) + snapshots.push(snapshot) + return snapshot + } + const lists = await read("interface_list") + const builtins = ["all", "none", "static", "dynamic"] + if (selectors.every(name => !builtins.includes(name) && knownInterfaces.has(name) && !lists.rows.some(row => row.name === name))) { + return new Set(selectors) + } + const [members, interfaces] = await Promise.all([read("interface_list_member"), read("interface_inventory")]) + const fail = () => new McpError("STATE_CONFLICT", "Не подтверждён состав списка интерфейсов OSPF", 409) + const refs = new Set(objects.map(object => `${object.kind}/${object.id}`)) + const add = (kind: string, row: ConfigurationRow) => { + const id = row[".id"] + if (typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id)) throw fail() + const key = `${kind}/${id}` + if (refs.has(key)) return + if (objects.length >= 1000) throw fail() + refs.add(key) + objects.push({ kind, id, serverId }) + } + const flag = (value: unknown) => { + if ([true, "true", "yes"].includes(value as boolean | string)) return true + if ([undefined, false, "false", "no"].includes(value as boolean | string | undefined)) return false + throw fail() + } + const lookup = (rows: ConfigurationRow[], key: string) => { + const matches = rows.filter(row => row.name === key || row[".id"] === key) + if (matches.length > 1) throw fail() + return matches[0] + } + const includeNames = (value: unknown) => { + if (value === undefined || value === "") return [] + if (typeof value !== "string") throw fail() + const names = value.split(",").map(name => name.trim()) + if (names.some(name => !name)) throw fail() + return names + } + const cache = new Map>() + const visiting = new Set() + const resolve = (key: string, nested: boolean): Set => { + if (!key) throw fail() + const list = lookup(lists.rows, key) + const direct = nested ? undefined : lookup(interfaces.rows, key) + if (direct && (list || builtins.includes(key))) throw fail() + if (direct) return new Set([direct]) + const name = String(list?.name ?? key) + if (list && builtins.includes(name)) add("interface_list", list) + if (name === "none") return new Set() + if (name === "all" || name === "static" || name === "dynamic") { + if ([...knownInterfaces].some(interfaceName => !lookup(interfaces.rows, interfaceName))) throw fail() + return new Set(interfaces.rows.filter(row => name === "all" || flag(row.dynamic) === (name === "dynamic"))) + } + if (!list || flag(list.dynamic) || visiting.has(list)) throw fail() + const id = String(list[".id"]) + const cached = cache.get(id) + if (cached) return cached + add("interface_list", list) + visiting.add(list) + const selection = new Set() + for (const reference of includeNames(list.include)) for (const row of resolve(reference, true)) selection.add(row) + for (const reference of includeNames(list.exclude)) for (const row of resolve(reference, true)) selection.delete(row) + for (const row of members.rows.filter(row => row.list === name || row.list === id)) { + add("interface_list_member", row) + if (flag(row.disabled)) continue + if (flag(row.dynamic)) throw fail() + const member = typeof row.interface === "string" ? lookup(interfaces.rows, row.interface) : undefined + if (!member) throw fail() + selection.add(member) + } + visiting.delete(list) + cache.set(id, selection) + return selection + } + const names = new Set() + for (const selector of selectors) for (const row of resolve(selector, false)) { + if (typeof row.name !== "string" || !row.name || lookup(interfaces.rows, row.name) !== row) throw fail() + add("interface", row) + names.add(row.name) + } + return names +} + +export async function addRoutingDependencies(serverId: number, resource: string, action: RosStep["action"], current: ConfigurationRow | undefined, fields: Record, snapshots: RosSnapshot[], objects: ChangeEvidence["objects"]) { + const read = async (id: string) => { + const existing = snapshots.find(snapshot => snapshot.resource === id) + if (existing) return existing + const snapshot = await readRosSnapshot(serverId, id); snapshots.push(snapshot); return snapshot + } + const parent = async (resource: string, reference: unknown) => { + const snapshot = await read(resource) + const row = snapshot.rows.find(row => row.name === reference || row[".id"] === reference) + if (!row || typeof row[".id"] !== "string" || action !== "delete" && row.disabled === "true") throw new McpError("STATE_CONFLICT", "Не подтверждена зависимость маршрутизации: " + resource, 409) + objects.push({ kind: resource, id: row[".id"], serverId }) + return row + } + const vrf = async (reference: unknown) => { + const row = await parent("vrf", reference ?? "main") + await parent("routing_table", row.name) + } + const instance = async (reference: unknown) => { + const row = await parent("ospf_instance", reference) + await vrf(row.vrf ?? "main") + if (row["routing-table"]) await parent("routing_table", row["routing-table"]) + return row + } + if (["ospf_instance", "bgp_instance", "bfd_configuration", "bgp_connection"].includes(resource)) { + for (const reference of new Set([current?.vrf, fields.vrf].filter(value => value !== undefined))) await vrf(reference) + if (["ospf_instance", "bgp_instance"].includes(resource)) for (const reference of new Set([current?.["routing-table"], fields["routing-table"]].filter(value => value !== undefined))) await parent("routing_table", reference) + } + if (resource === "bfd_configuration" && (current?.["address-list"] || fields["address-list"])) { + const desired = action === "delete" ? undefined : fields["address-list"] ?? current?.["address-list"] + const names = new Set([current?.["address-list"], desired].filter(value => typeof value === "string" && value)) + let foundDesired = false + for (const kind of ["ip_address_list", "ipv6_address_list"]) { + const snapshot = await readRoutingPolicySnapshot(serverId, kind, snapshots[0]?.sourceProof) + snapshots.push(snapshot) + for (const row of snapshot.rows.filter(row => names.has(row.list))) { + const id = row[".id"] + if (typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id) || objects.length >= 1000) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены записи address-list BFD", 409) + objects.push({ kind, id, serverId }) + if (row.list !== desired) continue + try { + const address = String(row.address), family = kind === "ip_address_list" ? 4 : 6 + if (isIP(address.split("/")[0]) !== family) throw new Error("Invalid address") + canonicalBgpPolicyPrefix(address.includes("/") ? address : `${address}/${family === 4 ? 32 : 128}`) + } catch { throw new McpError("STATE_CONFLICT", "Список BFD требует явных IP-адресов или префиксов", 409) } + if (![null, false, "false", "no"].includes(row.dynamic as string | boolean | null) || row.timed !== false) throw new McpError("STATE_CONFLICT", "Список BFD содержит временную запись", 409) + if ([null, false, "false", "no"].includes(row.disabled as string | boolean | null)) foundDesired = true + } + } + if (desired && !foundDesired) throw new McpError("NOT_FOUND", "Не найдены действующие записи address-list BFD", 404) + } + if (resource === "ospf_instance") { + const filterKeys = Object.keys(ospfFilterFields.shape) + if (filterKeys.some(key => fields[key] !== undefined)) { + const regular = await readRoutingPolicySnapshot(serverId, "routing_filter", snapshots[0]?.sourceProof) + const useSelection = Boolean(fields["out-filter-select"] ?? current?.["out-filter-select"]) || Boolean(current?.["out-filter-select"]) + const selection = useSelection ? await readRoutingPolicySnapshot(serverId, "routing_filter_select", snapshots[0]?.sourceProof) : undefined + for (const key of filterKeys) { + const desired = fields[key] + if (!desired) continue + const rows = (key === "out-filter-select" ? selection : regular)?.rows.filter(row => row.chain === desired) ?? [] + if (!rows.length) throw new McpError("NOT_FOUND", "Цепочка фильтра OSPF не найдена", 404) + if (rows.some(row => [true, "true", "yes"].some(flag => flag === row.invalid || flag === row.inactive))) throw new McpError("STATE_CONFLICT", "Цепочка OSPF содержит недействительное правило", 409) + } + for (const snapshot of [regular, ...(selection ? [selection] : [])]) { + snapshots.push(snapshot) + for (const row of snapshot.rows) { + if (objects.length >= 1000 || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"])) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён каталог зависимостей фильтра OSPF", 409) + objects.push({ kind: snapshot.resource.replace("routing_policy:", ""), id: row[".id"], serverId }) + } + } + } + const names = new Set([current?.["router-id"], fields["router-id"]].filter((value): value is string => typeof value === "string" && value !== "" && isIP(value) !== 4)) + if (names.size) { + const snapshot = await read("ospf_router_id") + for (const name of names) { + const matches = snapshot.rows.filter(row => row.name === name) + if (action === "delete" && matches.length === 0) continue + if (matches.length !== 1 || typeof matches[0][".id"] !== "string") throw new McpError("STATE_CONFLICT", "Не подтверждена единственная ссылка Router ID OSPF", 409) + if (action !== "delete" && name === (fields["router-id"] ?? current?.["router-id"])) { + try { resolveOspfRouterIdRow(matches[0]) } + catch { throw new McpError("STATE_CONFLICT", "Не подтверждён действующий Router ID OSPF", 409) } + } + objects.push({ kind: snapshot.resource, id: matches[0][".id"], serverId }) + } + } + } + if (resource === "bgp_connection") for (const reference of new Set([current?.instance, fields.instance].filter(value => value !== undefined))) { + const row = await parent("bgp_instance", reference) + await vrf(row.vrf ?? "main") + if (row["routing-table"]) await parent("routing_table", row["routing-table"]) + } + 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 (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 }) + } + if (resource === "bgp_instance" && current && (action === "delete" || fields.name && fields.name !== current.name || fields.vrf && fields.vrf !== current.vrf || fields["routing-table"] && fields["routing-table"] !== current["routing-table"])) { + if ((await read("bgp_connection")).rows.some(row => [current.name, current[".id"]].includes(row.instance))) throw new McpError("STATE_CONFLICT", "BGP instance используется подключением", 409) + } + if (resource === "ospf_area") for (const reference of new Set([current?.instance, fields.instance].filter(value => value !== undefined))) await instance(reference) + if (resource === "ospf_template") { + const oldList = current?.["prefix-list"] + const desiredList = action === "delete" ? undefined : fields["prefix-list"] ?? oldList + const desiredArea = fields.area ?? current?.area + for (const reference of new Set([current?.area, fields.area].filter(value => value !== undefined))) { + const area = await parent("ospf_area", reference) + const owner = await instance(area.instance) + if (desiredList && reference === desiredArea && owner.version !== "3" && owner.version !== 3) { + throw new McpError("INVALID_INPUT", "Prefix-list доступен только для шаблонов OSPFv3", 400) + } + } + if (desiredList && !desiredArea) throw new McpError("STATE_UNAVAILABLE", "Не подтверждена область prefix-list OSPFv3", 409) + await addOspfPrefixListDependencies(serverId, oldList, desiredList, snapshots, objects) + } + if (resource === "ospf_instance" && current && (action === "delete" || fields.name && fields.name !== current.name || fields.version && fields.version !== current.version || fields.vrf && fields.vrf !== current.vrf)) { + if ((await read("ospf_area")).rows.some(row => [current.name, current[".id"]].includes(row.instance))) throw new McpError("STATE_CONFLICT", "OSPF instance используется областью", 409) + } + if (resource === "ospf_area" && current && (action === "delete" || fields.name && fields.name !== current.name || fields.instance && fields.instance !== current.instance)) { + if ((await read("ospf_template")).rows.some(row => [current.name, current[".id"]].includes(row.area))) throw new McpError("STATE_CONFLICT", "Область OSPF используется шаблоном", 409) + } + if (resource === "vrf" && current && (action === "delete" || fields.name && fields.name !== current.name || fields.disabled === "true")) { + if (typeof current.name !== "string" || !current.name || typeof current[".id"] !== "string") throw new McpError("STATE_UNAVAILABLE", "Не подтверждено имя/ID VRF", 409) + for (const routeResource of ["ipv4_route", "ipv6_route"]) { + const routes = await read(routeResource) + try { + const names = [String(current.name), String(current[".id"])] + if (routes.rows.some(row => names.includes(String(row["routing-table"])) && !isDynamicRouteInsideTable(row, names))) throw new McpError("STATE_CONFLICT", "VRF используется настроенным маршрутом", 409) + // Kernel-derived routes inside this VRF disappear/move with its + // membership. They are not external gateway lookup references. + if (routes.rows.some(row => !isDynamicRouteInsideTable(row, names) && gatewayReferencesTable(row.gateway, names))) throw new McpError("STATE_CONFLICT", "VRF используется gateway маршрута, включая другую таблицу", 409) + } catch (error) { + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "Не подтверждены gateway dependencies VRF; изменение не выполняется", 409) + } + } + if ((await read("ospf_instance")).rows.some(row => [current.name, current[".id"]].includes(row.vrf))) throw new McpError("STATE_CONFLICT", "VRF используется OSPF instance", 409) + if ((await read("bfd_configuration")).rows.some(row => [current.name, current[".id"]].includes(row.vrf))) throw new McpError("STATE_CONFLICT", "VRF используется правилом BFD", 409) + if ((await read("bgp_instance")).rows.some(row => [current.name, current[".id"]].includes(row.vrf))) throw new McpError("STATE_CONFLICT", "VRF используется BGP instance", 409) + } +} diff --git a/backend/src/modules/mcp/routing-forward-transport-charge.test.cjs b/backend/src/modules/mcp/routing-forward-transport-charge.test.cjs new file mode 100644 index 00000000..9429b275 --- /dev/null +++ b/backend/src/modules/mcp/routing-forward-transport-charge.test.cjs @@ -0,0 +1,99 @@ +/* eslint-disable @typescript-eslint/no-require-imports -- CommonJS harness evaluates transpiled modules with explicit dependency adapters. */ +// MODEL regression: actual store/guard bodies with inert SQL/MAC adapters. +// Genuine applyPlan/runner/native-boundary cases have separate owned SQL receipts. +const assert = require('node:assert/strict'); +const test = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const { createRequire } = require('node:module'); +const { createHash, createHmac } = require('node:crypto'); +const root = path.resolve(__dirname, '../../../..'); +const dep = createRequire(path.join(root, 'backend/package.json')); +const ts = dep('typescript'); +const sha = value => createHash('sha256').update(value).digest('hex'); +const mac = value => createHmac('sha256', 'model-only-key-no-host-authority').update(value).digest('hex'); +const originalSecurity = fs.readFileSync(path.join(root, 'backend/src/modules/mcp/security.ts'), 'utf8'); +const securityAst = ts.createSourceFile('security.ts', originalSecurity, ts.ScriptTarget.Latest, true); +const selected = securityAst.statements.filter(node => ts.isFunctionDeclaration(node) && ['fingerprint', 'redact'].includes(node.name?.text)).map(node => node.getText(securityAst)).join('\n'); +const securityExports = {}; +const securityBody = ts.transpileModule('const sensitive = /password|passwd|secret|private.?key|preshared|pre.shared|authorization|auth.?key|authentication.?key|tcp.?md5.?key|api.?key|certificate.?contents|token|ciphertext/i; const MAX_MCP_PUBLIC_STRING_LENGTH=16000; const MAX_MCP_PUBLIC_ARRAY_ITEMS=500;\n' + selected, { compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText; +new Function('exports', 'tokenDigest', securityBody)(securityExports, sha); +const { fingerprint, redact } = securityExports; +class McpError extends Error { constructor(code, message) { super(message); this.code = code; } } +function load(file, providers) { + const output = {}; + const source = fs.readFileSync(path.join(__dirname, file), 'utf8'); + const compiled = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText; + new Function('exports', 'require', compiled)(output, name => { + if (name === 'node:crypto') return require(name); + if (name === 'zod') return dep(name); + assert.ok(Object.hasOwn(providers, name), `Unexpected import: ${name}`); return providers[name]; + }); + return output; +} +const id = n => `00000000-0000-4000-8000-${String(n).padStart(12,'0')}`; +function fixture(service='routing.community') { + const now = new Date('2026-11-02T12:00:00.000Z'); + const principal={connectionId:id(3), credentialId:id(4), credentialExpiresAt:'2026-11-02T13:00:00.000Z'}; + const authority={packet:{profileId:id(5),profileVersion:7,connectionId:id(3),credentialId:id(4),ownerId:'owner'}, proof:'a'.repeat(64)}; + const metadata={operationId:id(1),planId:id(2),operationName:'routing.community.switch',operationVersion:7,ownerId:'owner',phase:'forward',stepIndex:0,serviceName:service,principal,policyVersion:2,generation:'generation',originalExpiresAt:now.getTime()+60000,sealedEvidenceProof:sha('evidence'),sealedPayloadProof:sha('payload'),serviceInput:{profile:{autoRecovery:false},persistentProfileAuthority:authority}}; + const source={operation_state:'running',plan_state:'executing',policy_version:2,generation:'generation',approved_by:service==='routing.community.scoped'?'owner':'human',observed_at:now,evidence_sealed:'evidence',payload_sealed:'payload',step_state:'intent',intended_state:redact({kind:'service',service,input:metadata.serviceInput})}; + const binding={revision:1,operationId:id(1),planId:id(2),stepIndex:0,profileId:id(5),profileVersion:7,connectionId:id(3),ownerId:'owner',principal,service,operationVersion:7,authorityProof:authority.proof,evidenceProof:fingerprint('evidence'),payloadProof:fingerprint('payload'),scope:[1],createdAt:now.toISOString(),chargeUnits:1}; + const row={source_operation_id:id(1),source_plan_id:id(2),source_step_index:0,profile_id:id(5),connection_id:id(3),charge_units:1,binding_sealed:null,binding_proof:null,created_at:now}; + let chargeRows=[row], sourceRows=[source], reservedRows=[], decryptCalls=0, nativeWrites=0, keyUnavailable=false; + const queries=[]; + function seal() { row.binding_sealed=JSON.stringify(binding);row.binding_proof=mac(fingerprint(binding)); } + seal(); + const db={query:async(sql,params)=>{queries.push({sql,params});if(sql.includes('FROM mcp_operations'))return {rows:sourceRows};if(sql.includes('FROM mcp_routing_forward_charges'))return {rows:chargeRows};if(sql.includes('FROM mcp_routing_reservations'))return {rows:reservedRows};throw Error('Unexpected SQL');}}; + const providers={ + './routing-manual-inverse-history.js':{},'./routing-manual-inverse-store.js':{},'./routing-switch-ledger-contract.js':{},'./routing-charge-audit.js':{}, + '@mmapp/contracts/mcp-routing':{routeAiProfileSchema:{parse:value=>value}},'../../lib/permissions.js':{}, + './routing-profile-authority.js':{routingProfileAuthorityBindingSchema:{parse:value=>value}}, + './routing-community-scoped-request.js':{},'./routing-community-scoped-checkpoint.js':{},'./routing-community-scoped-recovery-source.js':{}, + './security.js':{fingerprint,redact,McpError,decryptSecretExisting:async value=>{decryptCalls++;return value;},privateFingerprinterExisting:async()=>{if(keyUnavailable)throw Error('key unavailable');return mac;}}, + './store.js':{},'./routing-reservation-contract.js':{},'./routing-reservation-transaction.js':{},'./routing-reservation-runtime.js':{} + }; + const verifier=load('routing-reservation-store.ts',providers).assertPersistentRoutingForwardTransportCharge; + return {metadata,source,binding,row,queries,seal, + noCharge:()=>{chargeRows=[];},duplicateCharge:()=>{chargeRows=[row,row];},ambiguousSource:()=>{sourceRows=[source,source];},reserved:()=>{reservedRows=[{id:id(8)}];},missingKey:()=>{keyUnavailable=true;}, + run:async()=>{await verifier(db,metadata,'owner');nativeWrites++;},writes:()=>nativeWrites,decryptCalls:()=>decryptCalls}; +} +for(const service of ['routing.community','routing.community.scoped'])test(`exact ${service} running charge passes without mutation`,async()=>{const f=fixture(service);await f.run();assert.equal(f.writes(),1);assert.equal(f.queries.length,3);assert.ok(f.queries.every(({sql})=>sql.startsWith('SELECT ')));assert.ok(f.queries[0].sql.includes('LIMIT 2'));}); +test('scoped transport refuses delegated approval before reading the charge',async()=>{const f=fixture('routing.community.scoped');f.source.approved_by='delegated-admin';await assert.rejects(f.run);assert.equal(f.writes(),0);assert.equal(f.queries.length,1);}); +const mutations=[ + ['missing charge',f=>f.noCharge()],['duplicate charge',f=>f.duplicateCharge()],['reserved overlap',f=>f.reserved()],['ambiguous source',f=>f.ambiguousSource()], + ['pending operation',f=>f.source.operation_state='pending'],['applied operation',f=>f.source.operation_state='applied'],['terminal step',f=>f.source.step_state='verified'],['draft plan',f=>f.source.plan_state='draft'],['missing approval',f=>f.source.approved_by=null],['policy drift',f=>f.source.policy_version++],['generation drift',f=>f.source.generation='other'], + ['intent drift',f=>f.source.intended_state={}],['bounded-out evidence',f=>f.source.evidence_sealed=null],['bounded-out payload',f=>f.source.payload_sealed=null],['evidence drift',f=>f.source.evidence_sealed='other'],['payload drift',f=>f.source.payload_sealed='other'],['expired original horizon',f=>f.metadata.originalExpiresAt=f.source.observed_at.getTime()], + ['missing key',f=>f.missingKey()],['MAC tamper',f=>f.row.binding_proof='0'.repeat(64)],['bounded-out binding',f=>f.row.binding_sealed=null],['SQL profile substitution',f=>f.row.profile_id=id(99)],['SQL units substitution',f=>f.row.charge_units=0], + ['signed different profile version',f=>{f.binding.profileVersion++;f.seal();}],['signed different owner',f=>{f.binding.ownerId='other';f.seal();}],['signed different authority',f=>{f.binding.authorityProof='b'.repeat(64);f.seal();}],['signed different credential',f=>{f.binding.principal={...f.binding.principal,credentialId:id(99)};f.seal();}],['signed wrong evidence',f=>{f.binding.evidenceProof='b'.repeat(64);f.seal();}],['future charge',f=>{f.binding.createdAt='2026-11-02T12:00:01.000Z';f.row.created_at=new Date(f.binding.createdAt);f.seal();}], + ['reserved profile',f=>f.metadata.serviceInput.profile.autoRecovery=true],['inverse lane',f=>f.metadata.operationName='operations.recover'],['later step',f=>f.metadata.stepIndex=1] +]; +for(const [name,mutate] of mutations)test(`${name} refuses before simulated native write`,async()=>{const f=fixture();mutate(f);await assert.rejects(f.run);assert.equal(f.writes(),0);}); +test('missing charge refuses before any private binding decryption',async()=>{const f=fixture();f.noCharge();await assert.rejects(f.run);assert.equal(f.decryptCalls(),0);}); +function guardFixture() { + const f=fixture(); + const input=f.metadata.serviceInput; + Object.assign(input,{nativeId:'*1',originalCandidateId:'a',originalTargetProof:'target-a',dependencyProof:'dep',contextSources:[{serverId:1,proof:'source'}]}); + const baseline={nativeId:'*1',candidateId:'a',targetProof:'target-a',dependencyProof:'dep',sourceProofs:input.contextSources}; + const row={id:id(5),version:7,connection_id:id(3),grant_expires_at:new Date('2026-11-02T13:00:00.000Z'),config:input.profile,state:{baseline:{}}}; + let active=true,chargeChecks=0,failCharge=false,inspections=0; + const providers={ + './routing-main-transition.js':{loadRoutingMainTransitionPreparation:async()=>input}, + './routing-community-scoped-request.js':{assertScopedCommunityRequest:async()=>{}}, + '@mmapp/contracts/mcp-routing':{routeAiProfileSchema:{parse:v=>v}}, + '../../db/index.js':{pool:{query:async()=>({rows:active?[row]:[]})}}, + '../../services/config-mutation-policy.js':{captureVerifiedMcpServiceExecution:async()=>({}),inspectVerifiedMcpServiceExecution:async()=>{inspections++;return f.metadata;}}, + './store.js':{effectiveMcpConnection:async()=>({})}, + './routing-profile-authority.js':{routingProfileAuthorityBindingSchema:{parse:v=>v},assertPersistentScopedProfileAuthority:async()=>{}}, + './routing-community-scoped-recovery-source.js':{}, + './routing-controller-baseline.js':{routingControllerBaselinePrincipal:async()=>({}),authenticateRoutingControllerBaseline:async()=>({value:{packet:baseline}})}, + './security.js':{fingerprint,McpError}, + './routing-reservation-store.js':{assertPersistentRoutingForwardTransportCharge:async(db,m,owner)=>{chargeChecks++;assert.equal(m,f.metadata);assert.equal(owner,'owner');if(failCharge)throw Error('charge absent');}} + }; + const guard=load('routing-controller-active-writer.ts',providers).assertActiveRoutingProfileWriter; + return {run:()=>guard(1),metadata:f.metadata,checks:()=>chargeChecks,inspections:()=>inspections,missingCharge:()=>{failCharge=true;},inactive:()=>{active=false;}}; +} +for(const service of ['routing.community','routing.community.scoped'])test(`native guard invokes charge verifier for ${service}`,async()=>{const f=guardFixture();f.metadata.serviceName=service;await f.run();assert.equal(f.checks(),1);assert.equal(f.inspections(),2);}); +test('charge refusal propagates through native writer guard',async()=>{const f=guardFixture();f.missingCharge();await assert.rejects(f.run);assert.equal(f.checks(),1);assert.equal(f.inspections(),1);}); +test('inactive server retains existing nonpersistent behavior',async()=>{const f=guardFixture();f.inactive();await f.run();assert.equal(f.checks(),0);assert.equal(f.inspections(),0);}); + diff --git a/backend/src/modules/mcp/routing-health-checks.ts b/backend/src/modules/mcp/routing-health-checks.ts new file mode 100644 index 00000000..cff21227 --- /dev/null +++ b/backend/src/modules/mcp/routing-health-checks.ts @@ -0,0 +1,60 @@ +import type { ChangeEvidence } from "./action-types.js" +import { confirmedEcmpInterfaces, preflightObservation } from "./ecmp-preflight.js" +import { resourceById, routerClient, rosSnapshotSourceProof } from "./resources.js" +import { McpError } from "./security.js" +import { readRouterOsSnapshotGet } from "../../services/routeros-snapshot-read.js" + +export async function verifyHealthChecks(evidence: Pick, serverIds: number[], afterWrite: boolean, authorize?: () => Promise) { + const cache = new Map() + const interfaces = new Map>() + for (const check of evidence.healthChecks ?? []) { + if (check.phase === "after" && !afterWrite) continue + if (!serverIds.includes(check.serverId)) throw new McpError("SERVER_DENIED", "Проверка находится вне плана") + if (check.field === "state" && check.resource !== "ospf_neighbor") throw new McpError("INVALID_INPUT", "Неизвестный критерий здоровья", 409) + if (check.field === "bfd-up" && check.resource !== "bfd_session") throw new McpError("INVALID_INPUT", "Неизвестный критерий BFD", 409) + if (check.field === "resolved" && !["ipv4_route", "ipv6_route"].includes(check.resource)) throw new McpError("INVALID_INPUT", "Неизвестный критерий next-hop", 409) + const key = `${check.serverId}:${check.resource}` + if (!cache.has(key)) { + await authorize?.() + const { client, server } = await routerClient(check.serverId) + const path = resourceById(check.resource).path + const isRoute = check.resource === "ipv4_route" || check.resource === "ipv6_route" + let raw: unknown + if (isRoute) { + const expected = evidence.snapshots.find(snapshot => snapshot.serverId === check.serverId && snapshot.resource === check.resource)?.sourceProof + if (!expected) throw new McpError("PLAN_EXPIRED", "Не подтверждён источник проверки next-hop", 409) + const compareSource = async (current: typeof server) => { + if (await rosSnapshotSourceProof(current) !== expected) { + throw new McpError("STATE_CONFLICT", "Источник проверки next-hop изменился", 409) + } + } + const assertCurrent = async () => { + await authorize?.() + await compareSource((await routerClient(check.serverId)).server) + } + await compareSource(server) + raw = await readRouterOsSnapshotGet({ client, path, assertCurrent }) + await assertCurrent() + if (!interfaces.has(check.serverId) && evidence.healthChecks?.some(item => item.serverId === check.serverId && item.resource === check.resource && item.nextHopInterface)) { + const inventory = preflightObservation(await readRouterOsSnapshotGet({ client, path: "/interface", assertCurrent })) + await assertCurrent() + if (inventory.error) throw new McpError(afterWrite ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "Каталог интерфейсов next-hop не подтверждён", 409) + interfaces.set(check.serverId, confirmedEcmpInterfaces(inventory.rows)) + } + } else { + raw = await client.get(path) + } + await authorize?.() + if (!Array.isArray(raw)) throw new McpError("STATE_UNAVAILABLE", "Ответ проверки здоровья не подтверждён", 503) + cache.set(key, raw) + } + const rows = cache.get(key)!.filter((value): value is Record => !!value && typeof value === "object" && (value as Record)[".id"] === check.id) + const row = rows.length === 1 ? rows[0] : undefined + const identityMatches = row && Object.entries(check.identity ?? {}).every(([key, value]) => row[key] === value) + if (check.nextHopInterface && interfaces.get(check.serverId)?.get(check.nextHopInterface.name) !== check.nextHopInterface.id) { + throw new McpError(afterWrite ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "Интерфейс next-hop изменился после подготовки", 409) + } + const healthy = identityMatches && row && (check.field === "resolved" ? typeof row["immediate-gw"] === "string" && row["immediate-gw"].trim().length > 0 && ![true, "true", "yes"].includes(row.invalid as boolean | string) && ![true, "true", "yes"].includes(row.disabled as boolean | string) : check.field === "bfd-up" ? String(row.state).toLowerCase() === "up" : check.field === "state" ? String(row.state).toLowerCase() === "full" : [true, "true", "yes"].includes(row[check.field] as boolean | string)) + if (!healthy) throw new McpError(afterWrite ? "VERIFICATION_FAILED" : "STATE_CONFLICT", afterWrite ? "После записи не подтверждён next-hop, Full OSPF или Up BFD" : "Работоспособность next-hop/OSPF/BFD изменилась после подготовки", 409) + } +} diff --git a/backend/src/modules/mcp/routing-inverse-effects.test.ts b/backend/src/modules/mcp/routing-inverse-effects.test.ts new file mode 100644 index 00000000..f7f8585f --- /dev/null +++ b/backend/src/modules/mcp/routing-inverse-effects.test.ts @@ -0,0 +1,70 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { test } from "node:test" +import { createInverseEffectReader, summarizeInverseEffects } from "./routing-inverse-effects.js" +import { unknownOriginalEffects } from "./routing-original-effects.js" + +test("inverse diagnostics use only the fixed read menu and stop at the shared round bound or first fault", async () => { + const trace: string[] = [] + let revokeAfterRequest = false, revoked = false, oversized = false + const router = createServer((request, response) => { + trace.push(request.method + " " + request.url) + if (revokeAfterRequest) revoked = true + response.end(JSON.stringify(oversized ? Array.from({ length: 1001 }, () => ({})) : [{ fixture: request.url }])) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const endpoint = { + id: 1, host: "127.0.0.1", port: (router.address() as { port: number }).port, username: "fixture", password: "fixture", + useSsl: false, verifySsl: true, enabled: true, routingEndpointEpoch: randomUUID(), + } + const reader = () => createInverseEffectReader(endpoint, { + signal: new AbortController().signal, deadline: Date.now() + 15_000, + guard: async () => { if (revoked) throw Object.assign(new Error("revoked fixture"), { code: "PERMISSION_DENIED" }) }, + }) + try { + const normal = await reader() + for (let round = 0; round < 3; round++) { + const sample = await normal.capture() + assert.deepEqual(sample.probes, []) + assert.deepEqual(sample.servers[0].routes.payload, [{ fixture: "/rest/routing/route" }]) + assert.deepEqual(sample.servers[0].bgp.payload, [{ fixture: "/rest/routing/bgp/session" }]) + } + await assert.rejects(normal.capture(), { code: "STATE_CONFLICT" }) + const menu = ["/routing/route", "/routing/bgp/connection", "/routing/bgp/session", "/log?buffer=memory&.proplist=.id,buffer,time,topics,message"].map(path => "GET /rest" + path) + assert.deepEqual(trace, [...menu, ...menu, ...menu]) + + const revokedReader = await reader() + revokeAfterRequest = true + await assert.rejects(revokedReader.capture(), { code: "PERMISSION_DENIED" }) + revoked = false + revokeAfterRequest = false + await assert.rejects(revokedReader.capture(), { code: "STATE_CONFLICT" }) + assert.equal(trace.length, 13) + + const oversizedReader = await reader() + oversized = true + await assert.rejects(oversizedReader.capture(), { code: "LIMIT_EXCEEDED" }) + await assert.rejects(oversizedReader.capture(), { code: "STATE_CONFLICT" }) + assert.equal(trace.length, 14) + } finally { + router.closeAllConnections() + await new Promise(resolve => router.close(() => resolve())) + } +}) + +test("inverse diagnostics summary discloses counts without prefixes, proof identity or raw logs", () => { + const observedAt = new Date().toISOString() + const report = { + ...unknownOriginalEffects("private reason"), selectionIdentity: "a".repeat(64), + transition: { baselineObservedAt: observedAt, changedPrefixes: ["private prefix"] }, + rounds: [{ + observedAt, routes: [{ prefix: "private prefix", status: "observed-other" as const }], + bgp: "observed-expected" as const, bfd: "not-configured" as const, controlProbe: "unknown" as const, relatedTopicRows: 8, + }], + } + assert.deepEqual(summarizeInverseEffects(report), { + status: "unknown", rounds: [{ observedAt, routes: { expected: 0, other: 1, unknown: 0 }, bgp: "observed-expected", bfd: "not-configured", relatedTopicRows: 8 }], + transitions: 1, logCoverage: "unknown", forwardingVerified: false, autoRecoveryAllowed: false, + }) +}) diff --git a/backend/src/modules/mcp/routing-inverse-effects.ts b/backend/src/modules/mcp/routing-inverse-effects.ts new file mode 100644 index 00000000..42c84e22 --- /dev/null +++ b/backend/src/modules/mcp/routing-inverse-effects.ts @@ -0,0 +1,112 @@ +import { z } from "zod" +import { routeContextClients } from "./routing-community-context-native.js" +import { resolveRoutingNetworkDestinationPins } from "./routing-network-native-destination.js" +import { ROUTING_NATIVE_LOG_BUFFER, ROUTING_NATIVE_LOG_PATH } from "./routing-network-native-parser.js" +import type { RoutingEndpoint } from "./routing-observation-source.js" +import type { OriginalEffectReport, inspectOriginalEffectRounds } from "./routing-original-effects.js" +import { McpError } from "./security.js" + +export { originalEffectBaselineMatches } from "./routing-original-effects.js" + +type Snapshot = Parameters[1][number] +type ReaderOptions = Readonly<{ signal: AbortSignal; deadline: number; guard: () => Promise }> + +export async function createInverseEffectReader(endpoint: RoutingEndpoint, { signal, deadline, guard }: ReaderOptions) { + const remaining = deadline - Date.now() + if (!Number.isFinite(remaining) || remaining <= 0 || remaining > 120_000 || !endpoint.enabled) { + throw new McpError("READ_CANCELLED", "Недоступен исходный допуск наблюдения восстановления", 409) + } + const source = Object.freeze({ ...endpoint }) + const expiresMonotonic = performance.now() + remaining + const lifetime = AbortSignal.any([signal, AbortSignal.timeout(Math.ceil(remaining))]) + const assertActive = () => { + if (lifetime.aborted || Date.now() >= deadline || performance.now() >= expiresMonotonic) { + throw new McpError("READ_CANCELLED", "Истёк исходный срок наблюдения восстановления", 409) + } + } + const fence = async () => { + assertActive() + await guard() + assertActive() + } + const pins = await resolveRoutingNetworkDestinationPins([source], { deadline, signal: lifetime, authorize: fence }) + const clients = routeContextClients([source], lifetime, { deadline, maxCalls: 12, maxBytes: 8 * 1048576, pins }) + const client = clients.get(source.id)! + let rounds = 0, rows = 0, busy = false, failed = false + const read = async (path: string): Promise[]> => { + await fence() + let result: unknown + try { result = await client.get(path) } + finally { await fence() } + if (!Array.isArray(result) || result.some(row => !row || typeof row !== "object" || Array.isArray(row))) { + throw new McpError("INVALID_NATIVE_RESPONSE", "Неподтверждённый ответ диагностики восстановления", 409) + } + rows += result.length + if (result.length > 1000 || rows > 10000) throw new McpError("LIMIT_EXCEEDED", "Превышен бюджет строк диагностики восстановления", 409) + return result + } + return { + async capture(): Promise { + if (busy || failed || rounds >= 3) throw new McpError("STATE_CONFLICT", "Раунд диагностики восстановления недоступен", 409) + busy = true + rounds++ + try { + const routes = await read("/routing/route") + const connectionsPayload = await read("/routing/bgp/connection") + const sessions = await read("/routing/bgp/session") + const logs = await read(ROUTING_NATIVE_LOG_PATH) + return { + observedAt: new Date().toISOString(), + servers: [{ serverId: source.id, routes: { payload: routes }, bgp: { payload: sessions, connectionsPayload }, logs: { buffer: ROUTING_NATIVE_LOG_BUFFER, payload: logs } }], + probes: [], + } + } catch (error) { + failed = true + throw error + } finally { + busy = false + } + }, + } +} + +const diagnosticState = z.enum(["observed-expected", "observed-other", "unknown"]) +const count = z.number().int().min(0).max(32) +export const inverseEffectSummarySchema = z.object({ + status: diagnosticState, + rounds: z.array(z.object({ + observedAt: z.string().datetime(), + routes: z.object({ expected: count, other: count, unknown: count }).strict() + .refine(value => value.expected + value.other + value.unknown <= 32), + bgp: diagnosticState, + bfd: z.enum(["not-configured", "unknown"]), + relatedTopicRows: z.number().int().min(0).max(1000), + }).strict()).max(2), + transitions: count, + logCoverage: z.literal("unknown"), + forwardingVerified: z.literal(false), + autoRecoveryAllowed: z.literal(false), +}).strict() +export type InverseEffectSummary = z.infer + +export function summarizeInverseEffects(report: OriginalEffectReport): InverseEffectSummary { + return inverseEffectSummarySchema.parse({ + status: report.status, + rounds: report.rounds.map(round => ({ + observedAt: round.observedAt, + routes: round.routes.reduce((counts, route) => { + if (route.status === "observed-expected") counts.expected++ + else if (route.status === "observed-other") counts.other++ + else counts.unknown++ + return counts + }, { expected: 0, other: 0, unknown: 0 }), + bgp: round.bgp, + bfd: round.bfd, + relatedTopicRows: round.relatedTopicRows, + })), + transitions: report.transition?.changedPrefixes.length ?? 0, + logCoverage: "unknown" as const, + forwardingVerified: false as const, + autoRecoveryAllowed: false as const, + }) +} diff --git a/backend/src/modules/mcp/routing-main-transition.ts b/backend/src/modules/mcp/routing-main-transition.ts new file mode 100644 index 00000000..efda1db4 --- /dev/null +++ b/backend/src/modules/mcp/routing-main-transition.ts @@ -0,0 +1,167 @@ +import { createHash } from "node:crypto" +import { z } from "zod" +import { routeAiProfileSchema, type RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { transaction } from "./store.js" +import { decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint, redact } from "./security.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { readRouteCommunityContext } from "./routing-community-context.js" +import { checkCandidatePath } from "./path-verification.js" +import { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution, type VerifiedMcpServiceExecution } from "../../services/config-mutation-policy.js" + +const proof = z.string().regex(/^[a-f0-9]{64}$/) +const boundedBody = z.string().refine(value => Buffer.byteLength(value, "utf8") <= 65536) +const preparationSchema = z.object({ revision: z.literal(1), service: z.literal("routing.community"), inputProof: proof, + nativeId: z.string().regex(/^\*[a-z0-9]+$/i), originalCandidateId: z.string(), desiredCandidateId: z.string(), + originalRule: boundedBody, desiredRule: boundedBody, originalTargetProof: proof, desiredTargetProof: proof, dependencyProof: proof, + contextSources: z.array(z.object({ serverId: z.number().int().positive(), proof }).strict()).max(1000), + scope: z.array(z.number().int().positive()).max(1000), authorityProof: proof, +}).strict() +const witnessSchema = z.object({ revision: z.literal(1), service: z.literal("routing.community"), phase: z.literal("MAIN_EXECUTED_VERIFIED"), + operationId: z.string().uuid(), planId: z.string().uuid(), stepIndex: z.literal(0), connectionId: z.string().uuid(), ownerId: z.string(), + operationVersion: z.literal(7), sealedEvidenceProof: proof, sealedPayloadProof: proof, + originalExpiresAt: z.number().int().safe(), originalTemporaryGrantProof: proof, originalTemporaryGrantExpiresAt: z.number().int().safe().nullable(), + principalProof: proof, policyVersion: z.number().int().positive(), generation: z.string(), + inputProof: proof, preparation: preparationSchema, observedAt: z.string().datetime(), + controlProbe: z.union([z.object({ status: z.literal("not-configured") }).strict(), z.object({ status: z.literal("passed"), id: z.string().uuid(), observedAt: z.string().datetime() }).strict()]), +}).strict() +export type RoutingMainTransitionWitness = z.infer +const fail = () => new McpError("STATE_CONFLICT", "Исходное выполнение main Route AI не подтверждено", 409) +const hashCiphertext = (value: string) => createHash("sha256").update(value).digest("hex") +const inputProof = (input: Record) => { const { mainTransitionPreparation: _preparation, ...base } = input; return fingerprint(base) } +const sign = (packet: unknown) => privateSecretFingerprint(fingerprint(packet), "route-ai-main-transition-witness-v1") +/** Existing MAIN target semantics; never relabel this as scoped or add probe hops. */ +export const routingMainServiceScope = (profile: RouteAiProfile) => [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])].sort((a,b) => a-b) +const isMain = (profile: RouteAiProfile) => profile.table === "main" && profile.probeVrf === "main" && (!profile.bgpContext || profile.bgpContext.inputChain === "bgp-in" && profile.bgpContext.vrf === "main") +async function authority(input: Record) { + const binding = routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority) + await assertPersistentScopedProfileAuthority(input) + return binding +} +async function preparation(input: Record) { + if (typeof input.mainTransitionPreparation !== "string" || Buffer.byteLength(input.mainTransitionPreparation) > 524288) throw fail() + const value = preparationSchema.parse(JSON.parse(await decryptSecret(input.mainTransitionPreparation))) + if (value.inputProof !== inputProof(input) || value.authorityProof !== fingerprint(input.persistentProfileAuthority)) throw fail() + return value +} +/** Called only by main plan preparation, with the body from existing toRouterRuleBody. + * Body is fixed in sealed evidence and checked against the actual writer callback. */ +export async function prepareRoutingMainTransition(input: Record, desiredRule: string) { + const profile = routeAiProfileSchema.parse(input.profile) + if (!isMain(profile)) throw fail() + await authority(input) + const authorize = async () => { await authority(input) } + const native = await readRouteCommunityContext(profile, { authorize, expectedSources: input.contextSources as Array<{serverId:number;proof:string}>, selectedCandidateId: String(input.candidateId) }) + if (!native.before.executionAvailable || !native.ownedTarget || !native.before.currentCandidateId || native.before.targetRuleProof !== input.targetRuleProof || native.before.dependencyProof !== input.contextProof) throw fail() + const value = preparationSchema.parse({ revision:1, service:"routing.community", inputProof:inputProof(input), nativeId:native.ownedTarget.nativeId, + originalCandidateId:native.before.currentCandidateId, desiredCandidateId:input.candidateId, originalRule:native.ownedTarget.rawRow.rule, desiredRule, + originalTargetProof:native.before.targetRuleProof, desiredTargetProof:native.ownedTarget.desiredTargetProof(desiredRule), dependencyProof:native.before.dependencyProof, + contextSources:native.before.sourceProofs, scope:routingMainServiceScope(profile), authorityProof:fingerprint(input.persistentProfileAuthority) }) + await native.fence(); await authorize() + return encryptSecret(JSON.stringify(value)) +} +/** Fixed private reader for the shared native gate. It cannot mint a request, + * checkpoint or authority. The enclosing genuine ALS metadata pins this input. */ +export async function loadRoutingMainTransitionPreparation(input: Record) { + const profile=routeAiProfileSchema.parse(input.profile) + if(!isMain(profile))throw fail() + await authority(input) + const prepared=await preparation(input) + if(fingerprint(prepared.scope)!==fingerprint(routingMainServiceScope(profile)) || prepared.desiredCandidateId!==input.candidateId || + prepared.originalTargetProof!==input.targetRuleProof || prepared.dependencyProof!==input.contextProof || + fingerprint(prepared.contextSources)!==fingerprint(input.contextSources))throw fail() + await authority(input) + return prepared +} +const attempts = new WeakMap; prepared: z.infer; startedAt:number }>() +export type RoutingMainTransitionAttempt = Readonly +async function inspect(input:Record, origin:VerifiedMcpServiceExecution) { + const metadata = await inspectVerifiedMcpServiceExecution(origin), profile=routeAiProfileSchema.parse(input.profile) + if (!isMain(profile) || metadata.operationName!=="routing.community.switch" || metadata.operationVersion!==7 || metadata.serviceName!=="routing.community" || metadata.phase!=="forward" || metadata.stepIndex!==0 || !metadata.serverWide || + fingerprint(metadata.serviceInput)!==fingerprint(input) || fingerprint([...metadata.serverIds].sort((a,b)=>a-b))!==fingerprint(routingMainServiceScope(profile)) || fingerprint(metadata.principal)!==fingerprint(input.principal) || metadata.ownerId!==input.ownerId) throw fail() + await authority(input) + return metadata +} +/** Called inside existing beforeWrite, after legacy fences and before native I/O. */ +export async function beginRoutingMainTransition(input:Record, actualBody:string, nativeId:string, originalRule:string):Promise { + const origin=await captureVerifiedMcpServiceExecution(), prepared=await preparation(input) + await inspect(input,origin) + if(prepared.desiredRule!==actualBody || prepared.nativeId!==nativeId || prepared.originalRule!==originalRule) throw fail() + const native=await readRouteCommunityContext(routeAiProfileSchema.parse(input.profile),{authorize:async()=>{await inspect(input,origin)},expectedSources:prepared.contextSources}) + if(!native.before.executionAvailable || native.ownedTarget?.nativeId!==prepared.nativeId || native.ownedTarget.rawRow.rule!==prepared.originalRule || native.before.currentCandidateId!==prepared.originalCandidateId || native.before.targetRuleProof!==prepared.originalTargetProof || native.before.dependencyProof!==prepared.dependencyProof || fingerprint(native.before.sourceProofs)!==fingerprint(prepared.contextSources))throw fail() + await native.fence();await inspect(input,origin) + const token=Object.freeze(Object.create(null));attempts.set(token,{origin,input:structuredClone(input),prepared,startedAt:Date.now()});return token +} +/** No signer/read provider parameter. Only the original live execute can mint. + * verify/reconcile use the loader; readback alone cannot synthesize a witness. */ +export async function finishRoutingMainTransition(token:RoutingMainTransitionAttempt,result:{action:string;conflictsRemoved:number}) { + const attempt=attempts.get(token);if(!attempt)throw fail();attempts.delete(token) + const {origin,input,prepared}=attempt, profile=routeAiProfileSchema.parse(input.profile) + const authorize=async()=>{await inspect(input,origin)} + if(result.action!=="patch" || result.conflictsRemoved!==0 || prepared.originalCandidateId===prepared.desiredCandidateId)throw fail() + await authorize() + let controlProbe:RoutingMainTransitionWitness["controlProbe"]={status:"not-configured"} + if(profile.candidates.find(row=>row.id===prepared.desiredCandidateId)?.controlProbe){ + const check=await checkCandidatePath(profile,prepared.desiredCandidateId,String(input.connectionId),{authorize,expectedSources:prepared.contextSources}) + if(check.status!=="passed" || Date.parse(check.observedAt)Date.now())throw fail() + controlProbe={status:"passed",id:check.id,observedAt:check.observedAt} + } + const native=await readRouteCommunityContext(profile,{authorize,expectedSources:prepared.contextSources,selectedCandidateId:prepared.desiredCandidateId}) + if(!native.before.executionAvailable || native.ownedTarget?.nativeId!==prepared.nativeId || native.ownedTarget.rawRow.rule!==prepared.desiredRule || native.before.currentCandidateId!==prepared.desiredCandidateId || native.before.targetRuleProof!==prepared.desiredTargetProof || native.before.dependencyProof!==prepared.dependencyProof || fingerprint(native.before.sourceProofs)!==fingerprint(prepared.contextSources))throw fail() + await native.fence();const metadata=await inspect(input,origin) + const packet=witnessSchema.parse({revision:1,service:"routing.community",phase:"MAIN_EXECUTED_VERIFIED",operationId:metadata.operationId,planId:metadata.planId,stepIndex:0, + connectionId:metadata.connectionId,ownerId:metadata.ownerId,operationVersion:7,sealedEvidenceProof:metadata.sealedEvidenceProof,sealedPayloadProof:metadata.sealedPayloadProof, + originalExpiresAt:metadata.originalExpiresAt,originalTemporaryGrantProof:metadata.originalTemporaryGrantProof,originalTemporaryGrantExpiresAt:metadata.originalTemporaryGrantExpiresAt, + principalProof:fingerprint(metadata.principal),policyVersion:metadata.policyVersion,generation:metadata.generation,inputProof:fingerprint(input),preparation:prepared, + observedAt:new Date().toISOString(),controlProbe}) + const ciphertext=await encryptSecret(JSON.stringify(packet)),checkpointProof=await sign(packet) + await authorize() + const originalSource=await source(packet.operationId,packet.planId) + if(hashCiphertext(originalSource.evidence.sealed)!==packet.sealedEvidenceProof || hashCiphertext(originalSource.payload.sealed)!==packet.sealedPayloadProof)throw fail() + await authorize() + await transaction(async db=>{ + const changed=await db.query(`UPDATE mcp_operation_checkpoints c SET configuration_sealed=$4,configuration_proof=$5 + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE c.operation_id=o.id AND c.step_index=s.step_index AND o.id=$1 AND o.plan_id=$2 AND o.connection_id=$3 + AND o.state='running' AND s.step_index=0 AND s.state='intent' AND s.intended_state=$6::jsonb + AND p.operation_id='routing.community.switch' AND p.operation_version=7 AND p.approved_by=$7 + AND p.evidence->>'sealed'=$8 AND p.payload->>'sealed'=$9 + AND c.configuration_sealed IS NULL AND c.configuration_proof IS NULL RETURNING c.operation_id`, + [packet.operationId,packet.planId,packet.connectionId,ciphertext,checkpointProof,JSON.stringify(redact({kind:"service",service:"routing.community",input})),packet.ownerId, + originalSource.evidence.sealed,originalSource.payload.sealed]) + if(changed.rowCount!==1)throw fail() + await authorize() + }) + await authorize() + return {checkpointProof,observedAt:packet.observedAt} +} +async function source(operationId:string,planId:string){ + const rows=(await pool.query(`SELECT o.id,o.plan_id,o.connection_id,o.state,o.created_at,p.operation_id,p.operation_version,p.approved_by,p.expires_at, + CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence END evidence, + CASE WHEN octet_length(p.payload::text)<=1048576 THEN p.payload END payload, + s.state step_state, CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END intended_state, + CASE WHEN octet_length(c.configuration_sealed)<=524288 THEN c.configuration_sealed END configuration_sealed,c.configuration_proof + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id JOIN mcp_operation_checkpoints c ON c.operation_id=o.id AND c.step_index=s.step_index + WHERE o.id=$1 AND o.plan_id=$2 AND s.step_index=0 LIMIT 2`,[operationId,planId])).rows + if(rows.length!==1 || typeof rows[0].evidence?.sealed!=="string" || typeof rows[0].payload?.sealed!=="string")throw fail() + return rows[0] +} +/** Read only: used by baseline advancement after authenticated own execution. + * Generic applied/verified rows are necessary but never sufficient authority. */ +export async function loadRoutingMainTransition(input:Record,operationId:string,planId:string){ + await authority(input) + const row=await source(operationId,planId),identity=fingerprint(row) + if(row.state!=="applied"||row.step_state!=="verified"||row.operation_id!=="routing.community.switch"||row.operation_version!==7||row.approved_by!==input.ownerId||!row.configuration_sealed||!row.configuration_proof)throw fail() + const packet=witnessSchema.parse(JSON.parse(await decryptSecret(row.configuration_sealed))),prepared=await preparation(input) + const evidence=JSON.parse(await decryptSecret(row.evidence.sealed)) + const step={kind:"service",service:"routing.community",input} + if(packet.operationId!==operationId||packet.planId!==planId||packet.connectionId!==row.connection_id||packet.connectionId!==String(input.connectionId)||packet.ownerId!==input.ownerId||packet.principalProof!==fingerprint(input.principal)||packet.inputProof!==fingerprint(input)||packet.preparation.authorityProof!==fingerprint(input.persistentProfileAuthority)||fingerprint(packet.preparation)!==fingerprint(prepared)||row.configuration_proof!==await sign(packet)|| + hashCiphertext(row.evidence.sealed)!==packet.sealedEvidenceProof||hashCiphertext(row.payload.sealed)!==packet.sealedPayloadProof|| + evidence.steps?.length!==1||fingerprint(evidence.steps[0])!==fingerprint(step)||fingerprint(row.intended_state)!==fingerprint(redact(step))||evidence.target?.scope!=="servers"||fingerprint([...evidence.target.serverIds].sort((a:number,b:number)=>a-b))!==fingerprint(prepared.scope)|| + !(row.created_at instanceof Date)||!(row.expires_at instanceof Date)||Date.parse(packet.observedAt)Date.now()||Date.parse(packet.observedAt)>=packet.originalExpiresAt||packet.originalExpiresAt>row.expires_at.getTime()||Date.now()>=packet.originalExpiresAt)throw fail() + const hasProbe=routeAiProfileSchema.parse(input.profile).candidates.find(row=>row.id===prepared.desiredCandidateId)?.controlProbe + if(hasProbe ? packet.controlProbe.status!=="passed" : packet.controlProbe.status!=="not-configured")throw fail() + await authority(input) + if(fingerprint(await source(operationId,planId))!==identity)throw fail() + return {packet,checkpointProof:row.configuration_proof as string} +} diff --git a/backend/src/modules/mcp/routing-manual-inverse-history.test.ts b/backend/src/modules/mcp/routing-manual-inverse-history.test.ts new file mode 100644 index 00000000..e77c21f8 --- /dev/null +++ b/backend/src/modules/mcp/routing-manual-inverse-history.test.ts @@ -0,0 +1,216 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mkdtemp, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { test } from "node:test" +import type { PoolClient } from "pg" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { decryptSecretExisting, encryptSecret, fingerprint, privateSecretFingerprint, redact } from "./security.js" +import { assertLegacyManualHistoryWithinOwner } from "./routing-manual-inverse-history.js" + +import { manualInverseReceiptSchema, retainManualInverseChargesWithinOwner } from "./routing-manual-inverse-store.js" +import { scopedRecoveryIntentFromCheckpoint, assertPrivateRecoverySize } from "./routing-community-scoped-recovery.js" +const at="2026-11-01T12:00:00.000Z", sourceAt="2026-11-01T11:59:00.000Z", until="2026-11-01T12:01:00.000Z" +const sign=(value:unknown,domain:string)=>privateSecretFingerprint(fingerprint(value),domain) +async function fixture(persistent=false,version=6) { + const connectionId=randomUUID(),credentialId=randomUUID(),profileId=randomUUID(),sourceId=randomUUID(),operationId=randomUUID(),ownerId="fixture-owner" + const principal={connectionId,credentialId,credentialExpiresAt:until} + const profile=routeAiProfileSchema.parse({name:"manual fixture",serverId:1,community:"65000:10",family:"ipv4",autoRecovery:false, + candidates:[{id:"a",jumpHostId:2,exitNodeId:3,wanInterface:"wan",downstreamInterface:"hop",gateway:"192.0.2.1", + gatewayRef:{resource:"ipv4_route",id:"*1"},controlProbe:{targetInterface:"exit",expectedHops:[{serverId:3,interface:"independent-hop"}]}}]}) + const input:Record={proofRevision:persistent?3:2,receiptId:randomUUID(),ownerId,principal,profile} + if(persistent) { + const packet={revision:1,profileId,profileVersion:1,connectionId,credentialId,ownerId,principalExpiresAt:until,credentialExpiresAt:until, + grantExpiresAt:until,ownerExpiresAt:until,policyExpiresAt:until,validUntil:until,generation:"fixture-generation",connectionGeneration:"fixture-generation", + policyVersion:1,policyProof:"a".repeat(64),ownerPermissionsProof:"b".repeat(64),profileProof:fingerprint(profile),scope:[1,2,3],transportProof:"c".repeat(64)} + input.persistentProfileAuthority={packet,proof:await sign(packet,"route-ai-persistent-profile-authority-v1")} + } + input.requestProof=await sign(input,`route-ai-scoped-request-v${input.proofRevision}`) + const step={kind:"service",service:"routing.community.scoped",input}, config={revision:persistent?2:1,phase:"CONFIG_APPLIED",operationId:sourceId,stepIndex:0, + connectionId,ownerId,receiptId:input.receiptId,requestSeal:input.requestProof,profileProof:fingerprint(profile),observedAt:sourceAt, + ...(persistent?{persistentProfileAuthority:input.persistentProfileAuthority}:{})} + const configurationProof=await sign(config,`route-ai-scoped-config-checkpoint-v${config.revision}`) + const generic={state:"applied",intended_state:redact(step),result:{configurationCheckpointProof:configurationProof,networkCheckpointProof:"d".repeat(64)}} + const sourceProof=await sign({operationId:sourceId,state:"partial",createdAt:sourceAt,sourceAction:"routing.community.switch",sourceVersion:7, + inputSeal:input.requestProof,configurationProof,networkSeal:generic.result.networkCheckpointProof,generic},"route-ai-scoped-recovery-source-v1") + const request:Record={proofRevision:1,mode:"manual",sourceOperationId:sourceId,sourceReceiptId:input.receiptId,sourceProof,configurationProof, + receiptId:randomUUID(),ownerId,principal,profile,validUntil:until} + request.requestProof=await sign(request,"route-ai-scoped-recovery-request-v1") + const intent={revision:1,phase:"RECOVERY_INTENT",mode:"manual",sourceOperationId:sourceId,sourceStepIndex:0,sourceReceiptId:input.receiptId,sourceProof,configurationProof, + operationId,stepIndex:0,receiptId:request.receiptId,requestSeal:request.requestProof,connectionId,ownerId,validUntil:until,createdAt:at} + const row={source_operation_id:sourceId,source_step_index:0,source_receipt_id:input.receiptId,source_configuration_proof:configurationProof,recovery_receipt_id:request.receiptId, + recovery_operation_id:operationId,intent_sealed:await encryptSecret(JSON.stringify(intent)),intent_proof:await sign(intent,"route-ai-scoped-recovery-intent-v1"), + deadline:new Date(until),reservation_id:null,source_plan_id:randomUUID(),source_connection_id:connectionId,source_state:"partial",source_created_at:new Date(sourceAt), + source_action:"routing.community.switch",source_version:7,source_evidence:await encryptSecret(JSON.stringify({steps:[step]})),recovery_plan_id:randomUUID(), + recovery_connection_id:connectionId,recovery_action:"operations.recover",recovery_version:version, + recovery_evidence:await encryptSecret(JSON.stringify({steps:[{kind:"service",service:"routing.community.scoped.recover",input:request}],validUntil:until})), + configuration_sealed:await encryptSecret(JSON.stringify(config)),configuration_proof:configurationProof,generic_state:generic.state,generic_intended:generic.intended_state,generic_result:generic.result} + return {row,profileId,input,intent} +} +function cursor(rows:unknown[],onFetch?:()=>void) { + let offset=0;const calls:string[]=[] + const db={query:async(sql:string)=>{calls.push(sql);if(sql.startsWith("DECLARE"))return{rows:[]};if(sql.startsWith("FETCH")){onFetch?.();const page=rows.slice(offset,offset+12);offset+=12;return{rows:page}}if(sql.startsWith("CLOSE"))return{rows:[]};if(sql.includes("FROM mcp_routing_manual_inverse_charges"))return{rows:[]};throw Error(`Unexpected SQL ${sql}`)}} as unknown as PoolClient + return {db,calls} +} + +test("historical cursor authenticates more than two pages of genuine nonpersistent classifications",async()=>{ + const dir=await mkdtemp(join(tmpdir(),"mm-manual-history-unit-")),previous=process.env.MCP_STATE_DIR;process.env.MCP_STATE_DIR=dir + try { + const rows=[];for(let n=0;n<25;n++)rows.push((await fixture(false,6+n%3)).row) + const f=cursor(rows) + await assertLegacyManualHistoryWithinOwner(f.db,randomUUID(),at,()=>{}) + assert.equal(f.calls.filter(sql=>sql.startsWith("FETCH")).length,3) + assert.equal(f.calls.at(-1),"CLOSE mcp_manual_history") + assert.match(f.calls[0],/^DECLARE .*NO SCROLL CURSOR WITHOUT HOLD FOR SELECT/) + assert.doesNotMatch(f.calls[0],/created_at\s*[<>]|LIMIT\s+12|schema_migrations/) + }finally{if(previous===undefined)delete process.env.MCP_STATE_DIR;else process.env.MCP_STATE_DIR=previous;await rm(dir,{recursive:true,force:true})} +}) + +test("historical authentication, active persistent refusal, future rejection and timeout never yield partial success",async()=>{ + const dir=await mkdtemp(join(tmpdir(),"mm-manual-history-negative-")),previous=process.env.MCP_STATE_DIR;process.env.MCP_STATE_DIR=dir + try { + const f=await fixture(true) + await assert.rejects(assertLegacyManualHistoryWithinOwner(cursor([f.row]).db,f.profileId,at,()=>{}),/hour incomplete/) + await assertLegacyManualHistoryWithinOwner(cursor([f.row]).db,randomUUID(),at,()=>{}) + await assertLegacyManualHistoryWithinOwner(cursor([f.row]).db,f.profileId,"2026-11-01T13:01:00.001Z",()=>{}) + const expiredTamper={...f.row,source_evidence:null} + await assert.rejects(assertLegacyManualHistoryWithinOwner(cursor([expiredTamper]).db,f.profileId,"2026-11-01T14:00:00.000Z",()=>{})) + for (const version of [7,8]) { + const current={...f.row,recovery_version:version} + await assert.rejects(assertLegacyManualHistoryWithinOwner(cursor([current]).db,f.profileId,"2026-11-01T14:00:00.000Z",()=>{}),/lacks durable charge/) + } + const stripped={...f.input};delete stripped.persistentProfileAuthority + await assert.rejects(assertLegacyManualHistoryWithinOwner(cursor([{...f.row,source_evidence:await encryptSecret(JSON.stringify({steps:[{kind:"service",service:"routing.community.scoped",input:stripped}]}))}]).db,f.profileId,at,()=>{})) + const future={...f.intent,createdAt:"2026-11-01T12:00:00.001Z"} + await assert.rejects(assertLegacyManualHistoryWithinOwner(cursor([{...f.row,intent_sealed:await encryptSecret(JSON.stringify(future)),intent_proof:await sign(future,"route-ai-scoped-recovery-intent-v1")}]).db,f.profileId,at,()=>{})) + const valid=await fixture(false);let expired=false;const c=cursor(Array(25).fill(valid.row),()=>{expired=true}) + await assert.rejects(assertLegacyManualHistoryWithinOwner(c.db,randomUUID(),at,()=>{if(expired)throw Error("original deadline expired")}),/original deadline/) + assert.equal(c.calls.at(-1),"CLOSE mcp_manual_history") + }finally{if(previous===undefined)delete process.env.MCP_STATE_DIR;else process.env.MCP_STATE_DIR=previous;await rm(dir,{recursive:true,force:true})} +}) + +test("state transfer cap MODEL refuses oversized or NULL history as a whole, with no truncation",async()=>{ + const dir=await mkdtemp(join(tmpdir(),"mm-manual-history-caps-unit-")),previous=process.env.MCP_STATE_DIR;process.env.MCP_STATE_DIR=dir + try { + // Models fixed PostgreSQL octet_length CASE only; no SQL is executed here. + const sqlStateCap=(raw:string|null)=>raw!==null&&Buffer.byteLength(raw,"utf8")<=32?raw:null + for(const value of ["partial","applied","verified"])assert.equal(sqlStateCap(value),value) + assert.equal(sqlStateCap("p".repeat(33)),null) + assert.equal(sqlStateCap("я".repeat(17)),null) + assert.equal(sqlStateCap("я".repeat(16)),"я".repeat(16)) + const valid=await fixture(false,7) + for(const field of ["source_state","generic_state"] as const){ + const oversized={...valid.row,[field]:sqlStateCap("partial"+"x".repeat(26))} + assert.equal(oversized[field],null) + const c=cursor([valid.row,oversized]) + await assert.rejects(assertLegacyManualHistoryWithinOwner(c.db,randomUUID(),at,()=>{})) + assert.equal(c.calls.at(-1),"CLOSE mcp_manual_history") + assert(c.calls[0].includes("CASE WHEN octet_length(o.state)<=32 THEN o.state END AS source_state")) + assert(c.calls[0].includes("CASE WHEN octet_length(s.state)<=32 THEN s.state END AS generic_state")) + } + // A short but noncanonical value is not made legitimate by the byte cap. + await assert.rejects(assertLegacyManualHistoryWithinOwner(cursor([{...valid.row,source_state:sqlStateCap("partial ")}]).db,randomUUID(),at,()=>{})) + }finally{if(previous===undefined)delete process.env.MCP_STATE_DIR;else process.env.MCP_STATE_DIR=previous;await rm(dir,{recursive:true,force:true})} +}) + +async function retentionFixture(version = 7) { + const f=await fixture(true,version), authority=f.input.persistentProfileAuthority as {packet:{profileVersion:number;scope:number[]};proof:string} + const payload=await encryptSecret(JSON.stringify({MODEL:true})) + const row={...f.row,recovery_payload:payload,authority_profile_version:1} + const binding=manualInverseReceiptSchema.shape.binding.parse({revision:1,recoveryOperationId:f.intent.operationId,recoveryPlanId:row.recovery_plan_id, + sourceOperationId:f.intent.sourceOperationId,sourcePlanId:row.source_plan_id,sourceStepIndex:0,profileId:f.profileId,profileVersion:1, + connectionId:f.intent.connectionId,ownerId:f.intent.ownerId,originalPrincipal:f.input.principal,recoveryPrincipal:f.input.principal, + operationVersion:version,sourceVersion:7,authorityProof:authority.proof,sourceProof:f.intent.sourceProof,sourceRequestProof:f.input.requestProof, + recoveryRequestProof:f.intent.requestSeal,sourceEvidenceProof:fingerprint(row.source_evidence),recoveryEvidenceProof:fingerprint(row.recovery_evidence), + recoveryPayloadProof:fingerprint(payload),scope:authority.packet.scope,createdAt:at,chargeUnits:1}) + const receipt={binding,proof:await sign(binding,"routing-manual-inverse-charge-v1")} + const intent={...f.intent,revision:2,manualInverseCharge:receipt} + row.intent_sealed=await encryptSecret(JSON.stringify(intent));row.intent_proof=await sign(intent,"route-ai-scoped-recovery-intent-v1") + const parent={recovery_operation_id:intent.operationId,recovery_plan_id:row.recovery_plan_id,source_operation_id:intent.sourceOperationId,source_plan_id:row.source_plan_id, + source_step_index:0,profile_id:f.profileId,profile_version:1,connection_id:intent.connectionId,owner_id:intent.ownerId,charge_units:1, + binding_sealed:await encryptSecret(JSON.stringify(binding)),binding_proof:receipt.proof,created_at:new Date(at)} + return {...f,row,intent,parent} +} +function retentionOwner(f:Awaited>,present=true) { + const calls:string[]=[], deleted:string[]=[];let parent=present + const db={query:async(sql:string)=>{ + calls.push(sql) + if(sql==='SHOW transaction_isolation')return{rows:[{transaction_isolation:'read committed'}]} + if(sql.includes('pg_advisory_xact_lock'))return{rows:[]} + if(sql.includes('FROM mcp_settings WHERE id=1 FOR SHARE'))return{rows:[{enabled:true,requestsRetentionDays:1,auditRetentionDays:180}]} + if(sql.includes('clock_timestamp'))return{rows:[{at:new Date('2026-11-03T12:00:00.000Z')}]} + if(sql.startsWith('DECLARE'))return{rows:[]} + if(sql.startsWith('FETCH'))return{rows:[f.row]} + if(sql.startsWith('CLOSE'))return{rows:[]} + if(sql.startsWith('SELECT c.recovery_operation_id'))return{rows:parent?[{recovery_operation_id:f.intent.operationId,source_operation_id:f.intent.sourceOperationId,profile_version:1}]:[]} + if(sql.startsWith('SELECT r.authority_profile_version'))return{rows:[f.row]} + if(sql.startsWith('SELECT CASE WHEN octet_length(payload'))return{rows:[{sealed:f.row.recovery_payload}]} + if(sql.startsWith('DELETE')){assert(parent);parent=false;deleted.push(f.intent.operationId);return{rowCount:1,rows:[]}} + if(sql.includes('FROM mcp_routing_manual_inverse_charges'))return{rows:parent?[f.parent]:[]} + throw Error('MODEL unexpected fixed query') + }} as unknown as PoolClient + return {db,calls,deleted} +} + +test('retention MODEL intent2 authenticates SQL clock after parent GC; unknown noReplay anchor survives',async()=>{ + const dir=await mkdtemp(join(tmpdir(),'mm-intent2-retention-')),previous=process.env.MCP_STATE_DIR;process.env.MCP_STATE_DIR=dir + try { + for (const version of [7,8]) { + const f=await retentionFixture(version),owner=retentionOwner(f),before=fingerprint(f.row) + // SQL aging and the transaction owner below are MODELS, never genuine SQL. + await assertLegacyManualHistoryWithinOwner(owner.db,f.profileId,'2026-11-03T12:00:00.000Z',()=>{}) + assert.deepEqual(await retainManualInverseChargesWithinOwner(owner.db),{deleted:1}) + assert.equal(owner.deleted.length,1);assert.equal(fingerprint(f.row),before) + for(const profile of [f.profileId,randomUUID()])await assertLegacyManualHistoryWithinOwner(owner.db,profile,'2026-11-03T12:00:00.000Z',()=>{}) + const admission=owner.calls.findIndex(s=>s.includes('pg_advisory_xact_lock')) + const settings=owner.calls.findIndex(s=>s.includes('FROM mcp_settings WHERE id=1 FOR SHARE')) + assert(admission>=0 && admissions.startsWith('SELECT c.recovery_operation_id'))) + const candidate=owner.calls.find(s=>s.startsWith('SELECT c.recovery_operation_id'))! + assert(candidate.indexOf("state IN('pending','running')"){ + const dir=await mkdtemp(join(tmpdir(),'mm-intent2-negative-')),previous=process.env.MCP_STATE_DIR;process.env.MCP_STATE_DIR=dir + try { + const f=await retentionFixture() + await assert.rejects(assertLegacyManualHistoryWithinOwner(retentionOwner(f,false).db,f.profileId,at,()=>{}),/recent/) + await assert.rejects(assertLegacyManualHistoryWithinOwner(retentionOwner(f,false).db,f.profileId,'2026-11-01T11:59:59.999Z',()=>{})) + f.parent.binding_proof='0'.repeat(64) + await assert.rejects(assertLegacyManualHistoryWithinOwner(retentionOwner(f).db,f.profileId,'2026-11-03T12:00:00.000Z',()=>{}),/MAC/) + const wrong=await retentionFixture();wrong.row.authority_profile_version=2;const o=retentionOwner(wrong) + await assert.rejects(retainManualInverseChargesWithinOwner(o.db),/hint/);assert.equal(o.deleted.length,0) + const tamper=await retentionFixture();tamper.intent.manualInverseCharge.proof='0'.repeat(64) + tamper.row.intent_sealed=await encryptSecret(JSON.stringify(tamper.intent));tamper.row.intent_proof=await sign(tamper.intent,'route-ai-scoped-recovery-intent-v1') + const t=retentionOwner(tamper);await assert.rejects(retainManualInverseChargesWithinOwner(t.db),/MAC/);assert.equal(t.deleted.length,0) + const legacy=await fixture(true,7) + await assert.rejects(assertLegacyManualHistoryWithinOwner(cursor([legacy.row]).db,legacy.profileId,'2026-11-03T12:00:00.000Z',()=>{}),/lacks durable/) + const wrongVersion=await retentionFixture(7);wrongVersion.row.recovery_version=8 + const mismatched=retentionOwner(wrongVersion) + await assert.rejects(assertLegacyManualHistoryWithinOwner(mismatched.db,wrongVersion.profileId,'2026-11-03T12:00:00.000Z',()=>{}),/signed allocation/) + await assert.rejects(retainManualInverseChargesWithinOwner(mismatched.db),/hint/) + assert.equal(mismatched.deleted.length,0) + // Revision2 payload proof cannot be replaced by an authentic stamp from a different payload. + const payload=await retentionFixture();payload.row.recovery_payload=await encryptSecret('{}') + await assert.rejects(assertLegacyManualHistoryWithinOwner(retentionOwner(payload,false).db,payload.profileId,'2026-11-03T12:00:00.000Z',()=>{}),/payload/) + }finally{if(previous===undefined)delete process.env.MCP_STATE_DIR;else process.env.MCP_STATE_DIR=previous;await rm(dir,{recursive:true,force:true})} +}) + +test('intent2 checkpoint extraction preserves copy and caps; reserved and nonpersistent legacy keysets remain exact',async()=>{ + const dir=await mkdtemp(join(tmpdir(),'mm-intent2-checkpoint-')),previous=process.env.MCP_STATE_DIR;process.env.MCP_STATE_DIR=dir + try { + const f=await retentionFixture(),extra={phase:'RECOVERY_VERIFIED',serverId:1,nativeId:'*1',candidateId:'a',originalRule:'rule',targetProof:'a'.repeat(64),dependencyProof:'b'.repeat(64),sourceEpochs:[],pathCheckId:randomUUID(),observedAt:at} + assert.deepEqual(scopedRecoveryIntentFromCheckpoint({...f.intent,...extra}),f.intent) + const legacy=(await fixture(false,7)).intent + assert.deepEqual(scopedRecoveryIntentFromCheckpoint({...legacy,...extra}),legacy) + const reserved={...legacy,mode:'reserved',reservationId:randomUUID(),networkProof:'c'.repeat(64)} + assert.deepEqual(scopedRecoveryIntentFromCheckpoint({...reserved,...extra}),reserved) + assertPrivateRecoverySize(f.intent,262144) + assert.throws(()=>assertPrivateRecoverySize({...f.intent,oversize:'x'.repeat(196581)},262144),/cap/) + assert.throws(()=>assertPrivateRecoverySize({...f.intent,...extra,originalRule:'\u0000'.repeat(65536)},524288),/cap/) + }finally{if(previous===undefined)delete process.env.MCP_STATE_DIR;else process.env.MCP_STATE_DIR=previous;await rm(dir,{recursive:true,force:true})} +}) diff --git a/backend/src/modules/mcp/routing-manual-inverse-history.ts b/backend/src/modules/mcp/routing-manual-inverse-history.ts new file mode 100644 index 00000000..c6d7dc13 --- /dev/null +++ b/backend/src/modules/mcp/routing-manual-inverse-history.ts @@ -0,0 +1,156 @@ +import type { PoolClient } from "pg" +import { z } from "zod" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { decryptSecretExisting, fingerprint, McpError, privateFingerprinterExisting, redact } from "./security.js" +import { routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import type { ChangeEvidence } from "./action-types.js" + +import { manualInverseReceiptSchema } from "./routing-manual-inverse-store.js" + +const fail=(message:string)=>new McpError("STATE_CONFLICT",message,409) +const uuid=z.string().uuid(), proof=z.string().regex(/^[a-f0-9]{64}$/), stamp=z.string().datetime() +const legacyIntentSchema=z.object({revision:z.literal(1),phase:z.literal("RECOVERY_INTENT"),mode:z.literal("manual"), + sourceOperationId:uuid,sourceStepIndex:z.literal(0),sourceReceiptId:uuid,sourceProof:proof,configurationProof:proof, + operationId:uuid,stepIndex:z.literal(0),receiptId:uuid,requestSeal:proof,connectionId:uuid,ownerId:z.string(),validUntil:stamp,createdAt:stamp}).strict() +const intentSchema=z.union([legacyIntentSchema,legacyIntentSchema.extend({revision:z.literal(2),manualInverseCharge:manualInverseReceiptSchema}).strict()]) +const sealedJson=async(raw:unknown)=>{ + if(typeof raw!=="string" || Buffer.byteLength(raw)>1048576) throw fail("Historical private evidence unavailable/oversized") + return JSON.parse(await decryptSecretExisting(raw)) as unknown +} +const mac=async(value:unknown,domain:string)=>(await privateFingerprinterExisting(domain))(fingerprint(value)) +const iso=(raw:unknown)=>{if(!(raw instanceof Date)||!Number.isFinite(raw.getTime()))throw fail("Historical SQL timestamp unavailable");return raw.toISOString()} +export interface HistoricalRow { + source_operation_id:string;source_step_index:number;source_receipt_id:string;source_configuration_proof:string; + recovery_receipt_id:string;recovery_operation_id:string;intent_sealed:string|null;intent_proof:string;deadline:Date; + reservation_id:string|null;source_plan_id:string|null;source_connection_id:string|null;source_state:string|null; + source_created_at:Date|null;source_action:string|null;source_version:number|null;source_evidence:string|null; + recovery_plan_id:string|null;recovery_connection_id:string|null;recovery_action:string|null;recovery_version:number|null; + recovery_evidence:string|null;recovery_payload?:string|null;configuration_sealed:string|null;configuration_proof:string|null; + generic_state:string|null;generic_intended:unknown;generic_result:Record|null; +} + +/** Returns only positively authenticated classification. It creates no charge, + * changes no timestamp and never applies current/renewed grants to old history. */ +export async function classifyManualInverseHistory(row:HistoricalRow,observedAt:string) { + const now=Date.parse(observedAt), intent=intentSchema.parse(await sealedJson(row.intent_sealed)) + if(Date.parse(intent.createdAt)>now || Date.parse(intent.createdAt)>Date.parse(intent.validUntil) || + row.intent_proof!==await mac(intent,"route-ai-scoped-recovery-intent-v1") || row.reservation_id!==null || + intent.sourceOperationId!==row.source_operation_id || row.source_step_index!==0 || intent.sourceReceiptId!==row.source_receipt_id || + intent.configurationProof!==row.source_configuration_proof || intent.operationId!==row.recovery_operation_id || + intent.receiptId!==row.recovery_receipt_id || intent.validUntil!==iso(row.deadline) || + row.source_connection_id!==intent.connectionId || row.recovery_connection_id!==intent.connectionId || + row.source_action!=="routing.community.switch" || row.source_version!==7 || + row.recovery_action!=="operations.recover" || ![6,7,8].includes(row.recovery_version??0) || + !["partial","applied"].includes(row.source_state??"")) throw fail("Historical manual intent/source identity differs") + const source=await sealedJson(row.source_evidence) as ChangeEvidence, recovery=await sealedJson(row.recovery_evidence) as ChangeEvidence + const step=source.steps?.[0], inverse=recovery.steps?.[0] + if(source.steps?.length!==1 || step?.kind!=="service" || step.service!=="routing.community.scoped" || + recovery.steps?.length!==1 || inverse?.kind!=="service" || inverse.service!=="routing.community.scoped.recover") throw fail("Historical service shape differs") + const input=step.input, request=inverse.input + const {requestProof:sourceRequestProof,...sourceRequest}=input + if(![2,3].includes(Number(input.proofRevision)) || sourceRequestProof!==await mac(sourceRequest,`route-ai-scoped-request-v${input.proofRevision}`) || + input.receiptId!==intent.sourceReceiptId || input.ownerId!==intent.ownerId || + (input.principal as {connectionId?:string})?.connectionId!==intent.connectionId) throw fail("Historical original request is unauthenticated") + const {requestProof:recoveryRequestProof,...recoveryRequest}=request + if(request.proofRevision!==1 || request.mode!=="manual" || recoveryRequestProof!==await mac(recoveryRequest,"route-ai-scoped-recovery-request-v1") || + request.sourceOperationId!==intent.sourceOperationId || request.sourceReceiptId!==intent.sourceReceiptId || request.sourceProof!==intent.sourceProof || + request.configurationProof!==intent.configurationProof || request.receiptId!==intent.receiptId || request.requestProof!==intent.requestSeal || + request.ownerId!==intent.ownerId || (request.principal as {connectionId?:string})?.connectionId!==intent.connectionId || + request.validUntil!==intent.validUntil || recovery.validUntil!==intent.validUntil || fingerprint(request.profile)!==fingerprint(input.profile)) throw fail("Historical recovery request/deadline is unauthenticated") + const config=await sealedJson(row.configuration_sealed) as Record + const configRevision=input.persistentProfileAuthority===undefined?1:2 + if(config.revision!==configRevision || config.phase!=="CONFIG_APPLIED" || config.operationId!==intent.sourceOperationId || config.stepIndex!==0 || + config.connectionId!==intent.connectionId || config.ownerId!==intent.ownerId || config.receiptId!==intent.sourceReceiptId || + config.requestSeal!==sourceRequestProof || config.profileProof!==fingerprint(input.profile) || + row.configuration_proof!==intent.configurationProof || row.configuration_proof!==await mac(config,`route-ai-scoped-config-checkpoint-v${configRevision}`) || + typeof config.observedAt!=="string" || !Number.isFinite(Date.parse(config.observedAt)) || Date.parse(config.observedAt)>now || + Date.parse(iso(row.source_created_at))>now || Date.parse(iso(row.source_created_at))>Date.parse(config.observedAt) || + fingerprint(config.persistentProfileAuthority??null)!==fingerprint(input.persistentProfileAuthority??null)) throw fail("Historical configuration/source seal differs") + const profile=routeAiProfileSchema.parse(input.profile) + if(profile.autoRecovery || fingerprint(row.generic_intended)!==fingerprint(redact(step)) || + row.generic_state!==(row.source_state==="applied"?"verified":"applied") || !row.generic_result || + row.generic_result.configurationCheckpointProof!==intent.configurationProof) throw fail("Historical original generic identity differs") + // sourceProof seals actual source identity/state/time and complete generic + // result. Network seal is independently in the signed verified result. + const generic={state:row.generic_state,intended_state:row.generic_intended,result:row.generic_result} + const sourceProof=await mac({operationId:intent.sourceOperationId,state:row.source_state,createdAt:iso(row.source_created_at), + sourceAction:row.source_action,sourceVersion:row.source_version,inputSeal:sourceRequestProof, + configurationProof:intent.configurationProof,networkSeal:row.generic_result.networkCheckpointProof,generic},"route-ai-scoped-recovery-source-v1") + if(sourceProof!==intent.sourceProof || typeof row.generic_result.networkCheckpointProof!=="string") throw fail("Historical exact original source proof differs") + if(intent.revision===1 && input.proofRevision===2 && input.persistentProfileAuthority===undefined && configRevision===1) return {kind:"nonpersistent" as const,intent} + if(input.proofRevision!==3 || input.persistentProfileAuthority===undefined) throw fail("Historical persistent presence contract differs") + const authority=routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority) + if(authority.proof!==await mac(authority.packet,"route-ai-persistent-profile-authority-v1") || + authority.packet.connectionId!==intent.connectionId || authority.packet.ownerId!==intent.ownerId || + authority.packet.credentialId!==(input.principal as {credentialId?:string})?.credentialId || + authority.packet.profileProof!==fingerprint(profile) || + Date.parse(intent.validUntil)>Date.parse(authority.packet.validUntil) || + Date.parse(intent.validUntil)>Date.parse(iso(row.source_created_at))+120000 || + Date.parse(intent.validUntil)>Date.parse(config.observedAt)+120000) throw fail("Historical persistent authority/deadline binding differs") + return {kind:"persistent" as const,intent,authority,sourceRequestProof:input.requestProof,sourcePrincipal:input.principal,recoveryPrincipal:request.principal} +} + +export const manualInverseHistoryProjection = `SELECT r.source_operation_id,r.source_step_index,r.source_receipt_id,CASE WHEN octet_length(r.source_configuration_proof)=64 THEN r.source_configuration_proof END AS source_configuration_proof, + r.recovery_receipt_id,r.recovery_operation_id,CASE WHEN octet_length(r.intent_sealed)<=262144 THEN r.intent_sealed END AS intent_sealed, + CASE WHEN octet_length(r.intent_proof)=64 THEN r.intent_proof END AS intent_proof,r.deadline,r.reservation_id,o.plan_id AS source_plan_id,o.connection_id AS source_connection_id, + CASE WHEN octet_length(o.state)<=32 THEN o.state END AS source_state,o.created_at AS source_created_at,CASE WHEN octet_length(p.operation_id)<=128 THEN p.operation_id END AS source_action,p.operation_version AS source_version, + CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence->>'sealed' END AS source_evidence, + ro.plan_id AS recovery_plan_id,ro.connection_id AS recovery_connection_id,CASE WHEN octet_length(rp.operation_id)<=128 THEN rp.operation_id END AS recovery_action,rp.operation_version AS recovery_version, + CASE WHEN octet_length(rp.evidence::text)<=1048576 THEN rp.evidence->>'sealed' END AS recovery_evidence, + CASE WHEN octet_length(c.configuration_sealed)<=524288 THEN c.configuration_sealed END AS configuration_sealed,CASE WHEN octet_length(c.configuration_proof)=64 THEN c.configuration_proof END AS configuration_proof, + CASE WHEN octet_length(s.state)<=32 THEN s.state END AS generic_state,CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END AS generic_intended, + CASE WHEN octet_length(s.result::text)<=262144 THEN s.result END AS generic_result + FROM mcp_scoped_recovery_intents r LEFT JOIN mcp_operations o ON o.id=r.source_operation_id + LEFT JOIN mcp_plans p ON p.id=o.plan_id LEFT JOIN mcp_operations ro ON ro.id=r.recovery_operation_id + LEFT JOIN mcp_plans rp ON rp.id=ro.plan_id LEFT JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + LEFT JOIN mcp_operation_checkpoints c ON c.operation_id=o.id AND c.step_index=0 + ` + +/** One fixed MVCC cursor, bounded PAGE size (not a lifetime quota). + * Admission callers own mcp-runner-admission; manual INSERT trigger enforces + * the same cut. Read-only callers use existing repeatable-read snapshots. + * Any failed page or original caller deadline refuses the WHOLE projection. */ +export async function assertLegacyManualHistoryWithinOwner(db:PoolClient,profileId:string,observedAt:string,assertLive:()=>void) { + assertLive() + await db.query(`DECLARE mcp_manual_history NO SCROLL CURSOR WITHOUT HOLD FOR SELECT r.source_operation_id,r.source_step_index,r.source_receipt_id,CASE WHEN octet_length(r.source_configuration_proof)=64 THEN r.source_configuration_proof END AS source_configuration_proof, + r.recovery_receipt_id,r.recovery_operation_id,CASE WHEN octet_length(r.intent_sealed)<=262144 THEN r.intent_sealed END AS intent_sealed, + CASE WHEN octet_length(r.intent_proof)=64 THEN r.intent_proof END AS intent_proof,r.deadline,r.reservation_id,o.plan_id AS source_plan_id,o.connection_id AS source_connection_id, + CASE WHEN octet_length(o.state)<=32 THEN o.state END AS source_state,o.created_at AS source_created_at,CASE WHEN octet_length(p.operation_id)<=128 THEN p.operation_id END AS source_action,p.operation_version AS source_version, + CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence->>'sealed' END AS source_evidence, + ro.plan_id AS recovery_plan_id,ro.connection_id AS recovery_connection_id,CASE WHEN octet_length(rp.operation_id)<=128 THEN rp.operation_id END AS recovery_action,rp.operation_version AS recovery_version, + CASE WHEN octet_length(rp.evidence::text)<=1048576 THEN rp.evidence->>'sealed' END AS recovery_evidence, + CASE WHEN octet_length(c.configuration_sealed)<=524288 THEN c.configuration_sealed END AS configuration_sealed,CASE WHEN octet_length(c.configuration_proof)=64 THEN c.configuration_proof END AS configuration_proof, + CASE WHEN octet_length(s.state)<=32 THEN s.state END AS generic_state,CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END AS generic_intended, + CASE WHEN octet_length(s.result::text)<=262144 THEN s.result END AS generic_result + FROM mcp_scoped_recovery_intents r LEFT JOIN mcp_operations o ON o.id=r.source_operation_id + LEFT JOIN mcp_plans p ON p.id=o.plan_id LEFT JOIN mcp_operations ro ON ro.id=r.recovery_operation_id + LEFT JOIN mcp_plans rp ON rp.id=ro.plan_id LEFT JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + LEFT JOIN mcp_operation_checkpoints c ON c.operation_id=o.id AND c.step_index=0 + WHERE r.mode='manual' ORDER BY r.source_operation_id,r.source_step_index`) + let failed=false + try { + for(;;) { + assertLive() + const rows=(await db.query('FETCH FORWARD 12 FROM mcp_manual_history')).rows + assertLive() + for(const row of rows) { + assertLive() + const classified=await classifyManualInverseHistory(row,observedAt) + assertLive() + if(classified.kind==="nonpersistent") continue + // Authenticate any durable inverse row before treating this old manual + // identity as represented by the normal profile ledger. + const {authenticateManualInverseHistoryWithinOwner}=await import("./routing-manual-inverse-store.js") + if(await authenticateManualInverseHistoryWithinOwner(db,classified.intent,classified.authority,observedAt,{sourceEvidenceProof:fingerprint(row.source_evidence),recoveryEvidenceProof:fingerprint(row.recovery_evidence),recoveryPlanId:row.recovery_plan_id,recoveryVersion:row.recovery_version,sourcePlanId:row.source_plan_id,sourceRequestProof:classified.sourceRequestProof,sourcePrincipal:classified.sourcePrincipal,recoveryPrincipal:classified.recoveryPrincipal})) continue + if(row.recovery_version!==6) throw fail("Current persistent manual inverse lacks durable charge") + if(classified.authority.packet.profileId!==profileId) continue + // Historical v6 sealed evidence.validUntil bounded admission; source and + // configuration ceilings are checked above. This is only an upper guard + // horizon; no inferred timestamp is inserted or added to the total. + if(Date.parse(observedAt)<=Date.parse(classified.intent.validUntil)+3600000) throw fail("Historical manual inverse makes current profile hour incomplete") + } + if(rows.length<12) return + } + } catch(error) { failed=true;throw error } + finally { try { await db.query("CLOSE mcp_manual_history") } catch(error) { if(!failed)throw error } } +} diff --git a/backend/src/modules/mcp/routing-manual-inverse-store.ts b/backend/src/modules/mcp/routing-manual-inverse-store.ts new file mode 100644 index 00000000..84fddf1e --- /dev/null +++ b/backend/src/modules/mcp/routing-manual-inverse-store.ts @@ -0,0 +1,223 @@ +import type { PoolClient } from "pg" +import { z } from "zod" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import type { ChangeEvidence } from "./action-types.js" +import type { McpPrincipal, StoredPlan } from "./store.js" +import { effectiveMcpConnection, getMcpSettings } from "./store.js" +import { decryptSecretExisting, encryptSecret, fingerprint, McpError, privateFingerprinterExisting, privateSecretFingerprint } from "./security.js" +import { routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import type { RoutingSwitchLedgerEntry } from "./routing-switch-ledger-contract.js" +import { recordRoutingChargeAnchor } from "./routing-charge-audit.js" +import { findMcpOperation } from "./registry.js" + +const fail = (message: string) => new McpError("STATE_CONFLICT", message, 409) +const uuid = z.string().uuid(), proof = z.string().regex(/^[a-f0-9]{64}$/), stamp = z.string().datetime() +const principal = z.object({ connectionId: uuid, credentialId: uuid, credentialExpiresAt: stamp }).strict() +export const manualInverseBindingSchema = z.object({ revision: z.literal(1), recoveryOperationId: uuid, recoveryPlanId: uuid, + sourceOperationId: uuid, sourcePlanId: uuid, sourceStepIndex: z.literal(0), profileId: uuid, profileVersion: z.number().int().positive(), + connectionId: uuid, ownerId: z.string().min(1).max(1000), originalPrincipal: principal, recoveryPrincipal: principal, + operationVersion: z.union([z.literal(7), z.literal(8)]), sourceVersion: z.literal(7), authorityProof: proof, sourceProof: proof, + sourceRequestProof: proof, recoveryRequestProof: proof, sourceEvidenceProof: proof, + recoveryEvidenceProof: proof, recoveryPayloadProof: proof, + scope: z.array(z.number().int().positive().safe()).min(1).max(1000), createdAt: stamp, chargeUnits: z.literal(1) }).strict() +export const manualInverseReceiptSchema = z.object({ binding: manualInverseBindingSchema, proof }).strict() +export type ManualInverseReceipt = z.infer +type Binding = z.infer +type ChargeRow = { recovery_operation_id: string; recovery_plan_id: string; source_operation_id: string; source_plan_id: string; source_step_index: number; + profile_id: string; profile_version: number; connection_id: string; owner_id: string; charge_units: number; + binding_sealed: string | null; binding_proof: string; created_at: Date } +const columns = `recovery_operation_id,recovery_plan_id,source_operation_id,source_plan_id,source_step_index,profile_id,profile_version, + connection_id,owner_id,charge_units,CASE WHEN octet_length(binding_sealed)<=262144 THEN binding_sealed END AS binding_sealed,binding_proof,created_at` +const iso = (raw: unknown) => { + if (!(raw instanceof Date) || !Number.isFinite(raw.getTime())) throw fail("Invalid manual inverse SQL time") + return raw.toISOString() +} +async function authenticate(row: ChargeRow, observedAt: string) { + if (!row.binding_sealed) throw fail("Manual inverse binding exceeds bound") + const value = manualInverseBindingSchema.parse(JSON.parse(await decryptSecretExisting(row.binding_sealed))) + const mac = await privateFingerprinterExisting("routing-manual-inverse-charge-v1") + if (mac(fingerprint(value)) !== row.binding_proof || value.createdAt !== iso(row.created_at) || Date.parse(value.createdAt) > Date.parse(observedAt) || + value.recoveryOperationId !== row.recovery_operation_id || value.recoveryPlanId !== row.recovery_plan_id || + value.sourceOperationId !== row.source_operation_id || value.sourcePlanId !== row.source_plan_id || value.sourceStepIndex !== row.source_step_index || + value.profileId !== row.profile_id || value.profileVersion !== row.profile_version || value.connectionId !== row.connection_id || + value.ownerId !== row.owner_id || row.charge_units !== 1 || value.originalPrincipal.connectionId !== value.connectionId || + value.recoveryPrincipal.connectionId !== value.connectionId) throw fail("Manual inverse charge identity/time/MAC differs") + return value +} + +async function authenticateCopy(raw: unknown, observedAt: string) { + const receipt = manualInverseReceiptSchema.parse(raw), value = receipt.binding + if ((await privateFingerprinterExisting("routing-manual-inverse-charge-v1"))(fingerprint(value)) !== receipt.proof || + value.originalPrincipal.connectionId !== value.connectionId || value.recoveryPrincipal.connectionId !== value.connectionId || + !Number.isFinite(Date.parse(observedAt)) || Date.parse(value.createdAt) > Date.parse(observedAt)) throw fail("Copied SQL admission stamp identity/time/MAC differs") + return receipt +} + +/** Historical immutable signature, independent of live old grant validity. */ +export async function readManualInverseChargesWithinOwner(db: PoolClient, profileId: string, + current: Awaited>, observedAt: string): Promise { + const rows = (await db.query(`SELECT ${columns} FROM mcp_routing_manual_inverse_charges + WHERE profile_id=$1 AND created_at>$2 ORDER BY created_at,recovery_operation_id LIMIT 12`, [profileId, new Date(Date.parse(observedAt)-3600000)])).rows + if (rows.length>11) throw fail("Manual inverse hourly count exceeds bound") + const entries: RoutingSwitchLedgerEntry[] = [] + for (const row of rows) { + const value = await authenticate(row, observedAt) + if (value.profileId!==profileId || value.connectionId!==current.id || value.ownerId!==current.owner_id || + value.scope.some(id=>!current.policy.scope.serverIds.includes(id))) throw fail("Manual inverse charge outside current profile/read scope") + entries.push({ profileId, sourceOperationId: value.sourceOperationId, sourceStepIndex: 0, + allocationId: value.recoveryOperationId, sourceIdentity: row.binding_proof, lane: "inverse", state: "held", createdAt: value.createdAt }) + } + return entries +} + +/** Sealed recovery evidence selects the original authority; absence in derived + * recovery input never means the original source was nonpersistent. */ +export async function selectManualInverseAdmissionWithinOwner(db: PoolClient, plan: StoredPlan, evidence: ChangeEvidence, actor: McpPrincipal) { + if (plan.operation_id!=="operations.recover") return undefined + const step=evidence.steps[0] + if (evidence.steps.length!==1 || step.kind!=="service" || step.service!=="routing.community.scoped.recover") return undefined + if (plan.operation_version!==findMcpOperation(plan.operation_id)?.version) throw new McpError("PLAN_EXPIRED", "Версия операции изменилась; подготовьте новый план", 409) + if (step.input.mode!=="manual") throw fail("Manual recovery mode differs") + const { loadScopedManualRecoveryAdmissionWithinOwner } = await import("./routing-community-scoped-recovery.js") + const loaded = await loadScopedManualRecoveryAdmissionWithinOwner(step.input, actor, db) + const { source } = loaded + if (source.input.persistentProfileAuthority===undefined) return undefined + const authority=routingProfileAuthorityBindingSchema.parse(source.input.persistentProfileAuthority) + if (source.profile.autoRecovery || source.row.source_version!==7 || source.row.id!==step.input.sourceOperationId || + authority.packet.connectionId!==actor.connectionId) throw fail("Manual inverse cannot replace reserved allocation") + return { input: source.input, authority, recoveryInput: step.input, source, + sourceEvidenceProof:fingerprint(source.row.evidence.sealed) } +} + +function expectedBinding(selected: NonNullable>>, plan: StoredPlan, + actor: McpPrincipal, operationId: string, observedAt: string): Binding { + const authority=selected.authority.packet + return manualInverseBindingSchema.parse({revision:1,recoveryOperationId:operationId,recoveryPlanId:plan.id, + sourceOperationId:selected.source.row.id,sourcePlanId:selected.source.row.source_plan_id,sourceStepIndex:0,profileId:authority.profileId,profileVersion:authority.profileVersion, + connectionId:actor.connectionId,ownerId:authority.ownerId,originalPrincipal:principal.parse(selected.input.principal),recoveryPrincipal:actor, + operationVersion:plan.operation_version,sourceVersion:selected.source.row.source_version,authorityProof:selected.authority.proof,sourceProof:selected.source.sourceProof, + sourceRequestProof:selected.input.requestProof,recoveryRequestProof:selected.recoveryInput.requestProof, + sourceEvidenceProof:selected.sourceEvidenceProof,recoveryEvidenceProof:fingerprint(plan.evidence.sealed), + recoveryPayloadProof:fingerprint(plan.payload.sealed),scope:authority.scope,createdAt:observedAt,chargeUnits:1}) +} + +/** Called only after actual operation insertion, under the original profile's + * admission lock. Generic debit belongs exclusively to runtime.applyPlan. */ +export async function admitManualInverseWithinOwner(db: PoolClient, operationId: string, plan: StoredPlan, evidence: ChangeEvidence, + actor: McpPrincipal, readHourly: (profileId:string,current:Awaited>,observedAt:string,deadlineAt:number)=>Promise<{totalCharges:number}>) { + const selected=await selectManualInverseAdmissionWithinOwner(db,plan,evidence,actor) + if(!selected) return + const actual=(await db.query("SELECT o.state,p.state AS plan_state FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND p.id=$2 AND o.connection_id=$3 FOR UPDATE OF o,p",[operationId,plan.id,actor.connectionId])).rows + if(actual.length!==1 || actual[0].state!=="pending" || actual[0].plan_state!=="executing") throw fail("Manual charge requires pending actual admission") + const observedAt=iso((await db.query("SELECT date_trunc('milliseconds',clock_timestamp()) AS at")).rows[0].at) + const existing=(await db.query(`SELECT ${columns} FROM mcp_routing_manual_inverse_charges WHERE recovery_operation_id=$1 OR source_operation_id=$2 LIMIT 3`,[operationId,selected.source.row.id])).rows + if(existing.length) { + if(existing.length!==1) throw fail("Competing manual inverse allocations") + const found=await authenticate(existing[0],observedAt) + if(fingerprint(found)!==fingerprint(expectedBinding(selected,plan,actor,operationId,found.createdAt))) throw fail("Manual inverse source already allocated or binding changed") + return + } + if((await db.query("SELECT id FROM mcp_routing_reservations WHERE source_operation_id=$1 AND source_step_index=0 LIMIT 2",[selected.source.row.id])).rowCount) throw fail("Reserved source already owns inverse allocation") + const current=await effectiveMcpConnection(actor,db) + const deadline = Math.min(plan.expires_at.getTime(), Date.parse(selected.authority.packet.validUntil), + Date.parse(actor.credentialExpiresAt), Date.parse(String(selected.recoveryInput.validUntil))) + const hourly=await readHourly(selected.authority.packet.profileId,current,observedAt,deadline) + if(hourly.totalCharges+1>routeAiProfileSchema.parse(selected.input.profile).maxSwitchesPerHour) throw fail("Insufficient common profile slots for manual inverse") + const value=expectedBinding(selected,plan,actor,operationId,observedAt) + const signature=await privateSecretFingerprint(fingerprint(value),"routing-manual-inverse-charge-v1") + const inserted=await db.query(`INSERT INTO mcp_routing_manual_inverse_charges(recovery_operation_id,recovery_plan_id, + source_operation_id,source_plan_id,source_step_index,profile_id,profile_version,connection_id,owner_id,charge_units,binding_sealed,binding_proof,created_at) + VALUES($1,$2,$3,$4,0,$5,$6,$7,$8,1,$9,$10,$11) RETURNING ${columns}`, + [operationId,plan.id,value.sourceOperationId,value.sourcePlanId,value.profileId,value.profileVersion,value.connectionId,value.ownerId, + await encryptSecret(JSON.stringify(value)),signature,observedAt]) + if(inserted.rowCount!==1 || fingerprint(await authenticate(inserted.rows[0],observedAt))!==fingerprint(value)) throw fail("Manual inverse allocation was not confirmed") + await recordRoutingChargeAnchor(db, { profileId: value.profileId, connectionId: value.connectionId, ownerId: value.ownerId, + sourceOperationId: value.sourceOperationId, allocationId: operationId, kind: "manual-inverse", bindingProof: signature, createdAt: observedAt }, deadline) + // Recheck complete original source/current authority on the same SQL owner. + const rechecked=await selectManualInverseAdmissionWithinOwner(db,plan,evidence,actor) + if(!rechecked || fingerprint(expectedBinding(rechecked,plan,actor,operationId,observedAt))!==fingerprint(value)) throw fail("Manual inverse source changed after charge") +} + +/** A committed charge is required before the existing manual native intent. + * This authenticates identity only; existing live/native authority still runs. */ +export async function assertManualInverseChargeWithinOwner(db:PoolClient,operationId:string,input:Record,actor:McpPrincipal) { + const rows=(await db.query("SELECT p.* FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.connection_id=$2 AND o.state='running' AND p.state='executing' AND p.operation_id='operations.recover' AND p.operation_version=$3",[operationId,actor.connectionId,findMcpOperation("operations.recover")?.version])).rows + if(rows.length!==1) throw fail("Manual native claim requires current running action version") + const plan=rows[0], evidence=JSON.parse(await decryptSecretExisting(plan.evidence.sealed)) as ChangeEvidence + const step=evidence.steps[0] + if(evidence.steps.length!==1 || step.kind!=="service" || step.service!=="routing.community.scoped.recover" || fingerprint(step.input)!==fingerprint(input)) throw fail("Manual native claim input differs") + const selected=await selectManualInverseAdmissionWithinOwner(db,plan,evidence,actor) + if(!selected) return + const observedAt=iso((await db.query("SELECT date_trunc('milliseconds',clock_timestamp()) AS at")).rows[0].at) + const charges=(await db.query(`SELECT ${columns} FROM mcp_routing_manual_inverse_charges WHERE recovery_operation_id=$1 LIMIT 2`,[operationId])).rows + if(charges.length!==1) throw fail("Missing committed manual inverse charge") + const found=await authenticate(charges[0],observedAt) + if(fingerprint(found)!==fingerprint(expectedBinding(selected,plan,actor,operationId,found.createdAt))) throw fail("Manual claim does not own its committed allocation") + return manualInverseReceiptSchema.parse({binding:found,proof:charges[0].binding_proof}) +} + +/** Verify representation of a historical manual intent without live-grant + * filtering. Mere presence of a SQL charge row never suppresses history. */ +export async function authenticateManualInverseHistoryWithinOwner(db:PoolClient, + intent:{revision?:number;manualInverseCharge?:ManualInverseReceipt;operationId:string;sourceOperationId:string;sourceReceiptId:string;sourceProof:string;requestSeal:string;connectionId:string;ownerId:string}, + authority:z.infer,observedAt:string, + identity:{sourceEvidenceProof:string;recoveryEvidenceProof:string;recoveryPlanId:string|null;recoveryVersion:number|null;sourcePlanId:string|null;sourceRequestProof:unknown;sourcePrincipal:unknown;recoveryPrincipal:unknown}) { + const rows=(await db.query(`SELECT ${columns} FROM mcp_routing_manual_inverse_charges WHERE recovery_operation_id=$1 OR source_operation_id=$2 LIMIT 3`,[intent.operationId,intent.sourceOperationId])).rows + const copy = intent.revision === 2 ? await authenticateCopy(intent.manualInverseCharge, observedAt) : undefined + if (!rows.length && !copy) return false + if (rows.length > 1) throw fail("Historical manual inverse has conflicting allocations") + // Present parents always authenticate; a valid copy never hides corruption. + const value = rows.length ? await authenticate(rows[0], observedAt) : copy!.binding + if (copy && (fingerprint(copy.binding) !== fingerprint(value) || rows.length && copy.proof !== rows[0].binding_proof)) throw fail("Copied SQL admission stamp differs from parent") + if(value.recoveryOperationId!==intent.operationId || value.sourceOperationId!==intent.sourceOperationId || + value.profileId!==authority.packet.profileId || value.profileVersion!==authority.packet.profileVersion || value.authorityProof!==authority.proof || + value.connectionId!==intent.connectionId || value.ownerId!==intent.ownerId || value.sourceProof!==intent.sourceProof || + value.recoveryRequestProof!==intent.requestSeal || value.sourceEvidenceProof!==identity.sourceEvidenceProof || + value.recoveryEvidenceProof!==identity.recoveryEvidenceProof || value.recoveryPlanId!==identity.recoveryPlanId || + value.operationVersion!==identity.recoveryVersion || value.sourcePlanId!==identity.sourcePlanId)throw fail("Historical inverse is not represented by its signed allocation") + // Only revision2 needs this extra one-row bounded projection; preserve the + // existing cursor page payload ceiling and legacy protocol untouched. + const payload = copy ? (await db.query<{sealed:string|null}>("SELECT CASE WHEN octet_length(payload::text)<=1048576 THEN payload->>'sealed' END AS sealed FROM mcp_plans WHERE id=$1 LIMIT 2",[identity.recoveryPlanId])).rows : [] + if (copy && (payload.length!==1 || !payload[0].sealed)) throw fail("Copied SQL admission payload unavailable/oversized") + if (copy && (value.sourceRequestProof !== identity.sourceRequestProof || fingerprint(value.originalPrincipal) !== fingerprint(identity.sourcePrincipal) || + fingerprint(value.recoveryPrincipal) !== fingerprint(identity.recoveryPrincipal) || fingerprint(value.scope) !== fingerprint(authority.packet.scope) || + value.recoveryPayloadProof !== fingerprint(payload[0].sealed))) throw fail("Copied SQL admission full request/payload identity differs") + if (!rows.length && Date.parse(value.createdAt) > Date.parse(observedAt) - 3600000) throw fail("Missing recent manual inverse parent") + return true +} + +/** Existing retention floor, same admission owner, bounded returned batch. + * The version column is only a candidate hint: all private evidence is checked + * before DELETE. Old revision1 remains pinned while its intent survives. */ +export async function retainManualInverseChargesWithinOwner(db: PoolClient) { + if ((await db.query("SHOW transaction_isolation")).rows[0]?.transaction_isolation !== "read committed") throw fail("Manual inverse GC requires fresh read-committed owner") + await db.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-runner-admission',0))") + const { requestsRetentionDays } = await getMcpSettings(db) + const observedAt=iso((await db.query("SELECT date_trunc('milliseconds',clock_timestamp()) AS at")).rows[0].at) + const candidates=(await db.query<{recovery_operation_id:string;source_operation_id:string;profile_version:number}>(`SELECT c.recovery_operation_id,c.source_operation_id,c.profile_version + FROM mcp_routing_manual_inverse_charges c JOIN mcp_scoped_recovery_intents r + ON r.source_operation_id=c.source_operation_id AND r.source_step_index=0 AND r.recovery_operation_id=c.recovery_operation_id + WHERE r.mode='manual' AND r.authority_profile_id IS NULL AND r.authority_profile_version IS NOT NULL + AND c.created_at < now()-make_interval(days=>$1) AND c.created_at < now()-interval '1 hour' + AND NOT EXISTS(SELECT 1 FROM mcp_operations o WHERE o.id=c.recovery_operation_id AND o.state IN('pending','running')) + ORDER BY c.created_at,c.recovery_operation_id LIMIT 100 FOR UPDATE OF c,r`,[requestsRetentionDays])).rows + const {manualInverseHistoryProjection,classifyManualInverseHistory}=await import("./routing-manual-inverse-history.js") + let deleted=0 + for(const candidate of candidates) { + const rows=(await db.query( + manualInverseHistoryProjection.replace("SELECT r.source_operation_id", "SELECT r.authority_profile_version,r.source_operation_id")+ + " WHERE r.mode='manual' AND r.source_operation_id=$1 AND r.source_step_index=0 AND r.recovery_operation_id=$2",[candidate.source_operation_id,candidate.recovery_operation_id])).rows + if(rows.length!==1)throw fail("Retention intent/source pair disappeared") + const row=rows[0],classified=await classifyManualInverseHistory(row,observedAt) + if(classified.kind!=="persistent" || classified.intent.revision!==2 || row.recovery_version!==classified.intent.manualInverseCharge.binding.operationVersion || + row.authority_profile_version!==candidate.profile_version || row.authority_profile_version!==classified.intent.manualInverseCharge.binding.profileVersion) + throw fail("Retention hint is not an authenticated persistent intent2") + if(!await authenticateManualInverseHistoryWithinOwner(db,classified.intent,classified.authority,observedAt,{ + sourceEvidenceProof:fingerprint(row.source_evidence),recoveryEvidenceProof:fingerprint(row.recovery_evidence),recoveryPlanId:row.recovery_plan_id,recoveryVersion:row.recovery_version,sourcePlanId:row.source_plan_id,sourceRequestProof:classified.sourceRequestProof,sourcePrincipal:classified.sourcePrincipal,recoveryPrincipal:classified.recoveryPrincipal})) + throw fail("Retention parent is not authenticated") + const result=await db.query("DELETE FROM mcp_routing_manual_inverse_charges WHERE recovery_operation_id=$1 AND source_operation_id=$2",[candidate.recovery_operation_id,candidate.source_operation_id]) + if(result.rowCount!==1)throw fail("Retention parent delete was not confirmed") + deleted++ + } + return {deleted} +} diff --git a/backend/src/modules/mcp/routing-measurement-address.test.ts b/backend/src/modules/mcp/routing-measurement-address.test.ts new file mode 100644 index 00000000..1450edc5 --- /dev/null +++ b/backend/src/modules/mcp/routing-measurement-address.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict" +import { readMeasurementAddress, selectMeasurementAddress } from "./routing-measurement-address.js" +import { routeMeasurementSchema } from "@mmapp/contracts/mcp-routing" + +const input = { dstInterface: "gre", family: "ipv6" as const } +const rows = ["fe80::2/64", "fd00::2/64"].map(address => ({ address, interface: "gre", disabled: "false" })) +assert.equal(selectMeasurementAddress(rows, input).address, "fd00::2") +assert.deepEqual(selectMeasurementAddress(rows, { ...input, dstAddress: "fe80:0:0:0:0:0:0:2" }), { address: "fe80::2", linkLocal: true }) +assert.throws(() => selectMeasurementAddress([...rows, { ...rows[1], address: "fd00::3/64" }], input)) +assert.throws(() => selectMeasurementAddress(rows, { ...input, dstAddress: "fd00::4" })) +for (const disabled of [undefined, "true", true, "unknown"]) assert.throws(() => selectMeasurementAddress([{ ...rows[1], disabled }], input)) +assert.throws(() => selectMeasurementAddress([{ ...rows[1], invalid: "true" }], input)) +assert.equal(selectMeasurementAddress([{ ...rows[0], address: "fe90::2/64" }], input).linkLocal, true) +assert.throws(() => routeMeasurementSchema.parse({ srcServerId: 1, dstServerId: 2, srcInterface: "gre", ...input, dstAddress: "not-an-ip" })) +let reads = 0 +assert.equal((await readMeasurementAddress([{ ...rows[1], ".id": "*A", disabled: undefined }], input, async id => { reads++; assert.equal(id, "*A"); return { ret: "false" } })).address, "fd00::2") +assert.equal(reads, 1) +await assert.rejects(readMeasurementAddress([{ ...rows[1], ".id": "*A", disabled: undefined }], input, async () => ({ ret: "unknown" }))) +await assert.rejects(readMeasurementAddress([{ ...rows[1], ".id": "bad/id", disabled: undefined }], input, async () => { throw new Error("Must not dispatch") })) +assert.throws(() => selectMeasurementAddress([...rows, { ...rows[1], address: "fd00::3/64", disabled: undefined }], input)) +console.log("Measurement destination: peer-owned addresses, IPv6 ULA plus link-local, explicit canonical IPv6, ambiguous/disabled/unknown rejected") diff --git a/backend/src/modules/mcp/routing-measurement-address.ts b/backend/src/modules/mcp/routing-measurement-address.ts new file mode 100644 index 00000000..23e2dd42 --- /dev/null +++ b/backend/src/modules/mcp/routing-measurement-address.ts @@ -0,0 +1,38 @@ +import { isIP } from "node:net" +import { ipv6ToInt } from "../../services/traffic-flow-ip.js" +import { observedBoolean } from "../../services/ospf-observed-values.js" +import { McpError } from "./security.js" + +function linkLocal(address: string) { + const value = ipv6ToInt(address) + return value !== null && value >> 118n === 0x3fan +} +type AddressInput = { dstInterface: string; family: "ipv4" | "ipv6"; dstAddress?: string } +const disabled = (row: Record) => observedBoolean(typeof row.disabled === "boolean" ? String(row.disabled) : typeof row.disabled === "string" ? row.disabled : undefined) +function candidate(rows: Record[], input: AddressInput) { + const family = input.family === "ipv4" ? 4 : 6 + const same = (a: string, b: string) => family === 6 ? ipv6ToInt(a) === ipv6ToInt(b) : a === b + let candidates = rows.filter(row => row.interface === input.dstInterface + && disabled(row) !== true + && row.invalid !== true && row.invalid !== "true" && row.invalid !== "yes") + .map(row => ({ row, address: String(row.address).split("/")[0] })).filter(item => isIP(item.address) === family) + if (input.dstAddress) candidates = candidates.filter(item => same(item.address, input.dstAddress!)) + else if (family === 6 && candidates.some(item => !linkLocal(item.address))) candidates = candidates.filter(item => !linkLocal(item.address)) + if (candidates.length !== 1) throw new McpError("MEASUREMENT_UNKNOWN", "Не подтверждён единственный адрес интерфейса; укажите dstAddress при нескольких адресах", 409) + return candidates[0] +} +export function selectMeasurementAddress(rows: Record[], input: AddressInput) { + const selected = candidate(rows, input) + if (disabled(selected.row) !== false) throw new McpError("MEASUREMENT_UNKNOWN", "Не подтверждено включённое состояние адреса интерфейса", 409) + return { address: selected.address, linkLocal: input.family === "ipv6" && linkLocal(selected.address) } +} +/** Resolve an omitted RouterOS default only for one already unambiguous row. */ +export async function readMeasurementAddress(rows: Record[], input: AddressInput, readDisabled: (id: string) => Promise) { + const selected = candidate(rows, input) + if (selected.row.disabled === undefined && typeof selected.row[".id"] === "string" && /^\*[a-z0-9]+$/i.test(selected.row[".id"])) { + const response = await readDisabled(selected.row[".id"]) + const value = response && typeof response === "object" && "ret" in response ? response.ret : undefined + return selectMeasurementAddress([{ ...selected.row, disabled: value }], input) + } + return selectMeasurementAddress([selected.row], input) +} diff --git a/backend/src/modules/mcp/routing-measurement-origin.test.ts b/backend/src/modules/mcp/routing-measurement-origin.test.ts new file mode 100644 index 00000000..55623976 --- /dev/null +++ b/backend/src/modules/mcp/routing-measurement-origin.test.ts @@ -0,0 +1,98 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { pool, closePool } from "../../db/index.js" +import { withVerifiedConfigurationChange, type VerifiedMcpServiceMetadata } from "../../services/config-mutation-policy.js" +import { createRoutingMeasurementOriginGuard } from "./routing-measurement-origin.js" +import { registerRoutingMeasurement } from "./routing-measurement.js" +import { findMcpService } from "./service-registry.js" +import { redact } from "./security.js" + +const command = () => ({ srcServerId: 101, dstServerId: 102, srcInterface: "gre-source", dstInterface: "gre-remote", family: "ipv4", vrf: "main", bandwidth: true, durationSec: 30, maxMbps: 100, + expectedEndpoints: [{ serverId: 101, proof: "a".repeat(64) }, { serverId: 102, proof: "b".repeat(64) }] }) +function fixture() { + const input = command(), connectionId = randomUUID(), expiry = Date.now() + 10000 + const metadata: VerifiedMcpServiceMetadata = { operationId: randomUUID(), planId: randomUUID(), operationName: "diagnostics.bandwidth", serviceName: "routing.measure", operationVersion: 2, + stepIndex: 0, phase: "forward", policyVersion: 1, generation: "owned-test-generation", originalExpiresAt: expiry, sealedEvidenceProof: "e".repeat(64), sealedPayloadProof: "f".repeat(64), + originalTemporaryGrantProof: "a".repeat(64), originalTemporaryGrantExpiresAt: null, principal: { connectionId, credentialId: randomUUID(), credentialExpiresAt: new Date(expiry + 1000).toISOString() }, serviceInput: input } + let authorized = true, calls = 0, hook: (() => void) | undefined + const context = { actorId: "unit-original-owner", connectionId, approved: false, serverIds: [101, 102], objects: [101, 102].map(serverId => ({ kind: "server", serverId, id: String(serverId) })), serverWide: true, serviceExecution: metadata, + authorize: async () => { calls++; hook?.(); if (!authorized) throw Error("Original grant revoked") } } + const row: Record = { operation_state: "running", plan_state: "executing", step_state: "intent", server_id: null, operation_id: metadata.operationName, + operation_version: 2, policy_version: 1, generation: metadata.generation, owner_id: context.actorId, approved_by: null, expires_at: new Date(expiry), evidence_proof: metadata.sealedEvidenceProof, + payload_proof: metadata.sealedPayloadProof, intended_state: redact({ kind: "service", service: metadata.serviceName, input }) } + return { input, metadata, context, row, calls: () => calls, revoke: () => { authorized = false }, onAuthorize: (value: () => void) => { hook = value } } +} + +test("routing.measure production origin guard, fake metadata SQL only", async t => { + const originalQuery = pool.query, originalConnect = pool.connect + let currentRow: Record | undefined, queries = 0, connects = 0 + pool.query = (async (query: { text?: string; values?: unknown[] }) => { + queries++; assert(query && typeof query.text === "string"); assert.match(query.text, /FROM public\.mcp_operations/); assert.match(query.text, /LIMIT 2/) + assert(!/FROM (?:public\.)?servers\b/i.test(query.text), "Origin checks must precede credential lookup") + return { rows: currentRow ? [currentRow] : [] } + }) as never + pool.connect = (() => { connects++; throw Error("Origin unit cannot acquire a physical backend") }) as never + try { + await t.test("existing service caller refuses outside genuine runtime before source credentials or native calls", async () => { + registerRoutingMeasurement((() => {}) as Parameters[0]) + const service = findMcpService("routing.measure"); assert(service) + const before = queries; await assert.rejects(service.execute(command())); assert.equal(queries, before); assert.equal(connects, 0) + }) + await t.test("genuine version2 step accepts autonomy and supplies frozen exact input independent of mutable caller raw", async () => { + const f = fixture(); currentRow = f.row; const raw = structuredClone(f.input) + await withVerifiedConfigurationChange(f.context, async () => { + f.onAuthorize(() => { raw.srcServerId = 999; raw.expectedEndpoints.splice(0) }) + const guard = await createRoutingMeasurementOriginGuard(raw) + assert(Object.isFrozen(guard)); assert(Object.isFrozen(guard.input)); assert(Object.isFrozen(guard.input.expectedEndpoints)) + assert.deepEqual(guard.input, f.input); assert.notEqual(guard.input.srcServerId, raw.srcServerId) + await guard.assertCurrent(); assert(f.calls() >= 4) + assert.equal(Object.hasOwn(guard, "ready"), false); assert.equal(Object.hasOwn(guard, "peerProof"), false) + }) + }) + await t.test("wrong operation/service/version or raw mismatch refuses before any origin SQL/credentials", async () => { + for (const kind of ["operation", "service", "version", "raw"] as const) { + const f = fixture(); currentRow = f.row + if (kind === "operation") f.metadata.operationName = "servers.probes.run" + if (kind === "service") f.metadata.serviceName = "other.service" + if (kind === "version") f.metadata.operationVersion = 1 + const raw = structuredClone(f.input); if (kind === "raw") raw.dstServerId = 999 + const before = queries + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(createRoutingMeasurementOriginGuard(raw)) }) + assert.equal(queries, before) + } + }) + await t.test("BOTH server IDs need original scope and matching server objects before credentials", async () => { + for (const fault of ["source-scope", "remote-scope", "source-object", "remote-object", "object-id"] as const) { + const f = fixture(); currentRow = f.row + if (fault === "source-scope") f.context.serverIds = [102] + if (fault === "remote-scope") f.context.serverIds = [101] + if (fault === "source-object") f.context.objects = f.context.objects.slice(1) + if (fault === "remote-object") f.context.objects = f.context.objects.slice(0, 1) + if (fault === "object-id") f.context.objects[1].id = "999" + const before = queries + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(createRoutingMeasurementOriginGuard(f.input)) }) + assert.equal(queries, before) + } + }) + await t.test("fresh SQL origin tamper/revoke and exited ALS invalidate an existing guard", async () => { + const f = fixture(); currentRow = f.row + const guard = await withVerifiedConfigurationChange(f.context, async () => { + const value = await createRoutingMeasurementOriginGuard(f.input) + currentRow = { ...f.row, evidence_proof: "0".repeat(64) }; await assert.rejects(value.assertCurrent()) + currentRow = f.row; await value.assertCurrent(); f.revoke(); await assert.rejects(value.assertCurrent(), /revoked/) + return value + }) + await assert.rejects(guard.assertCurrent()) + }) + await t.test("accessors or non-JSON caller objects refuse synchronously without evaluating getters", async () => { + let reads = 0; const raw = command(); Object.defineProperty(raw, "srcServerId", { enumerable: true, get() { reads++; return 101 } }) + await assert.rejects(createRoutingMeasurementOriginGuard(raw)); assert.equal(reads, 0) + const f = fixture(); currentRow = f.row; f.metadata.serviceInput = { ...f.input, injected: "unexpected" } + const before = queries + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(createRoutingMeasurementOriginGuard(f.metadata.serviceInput)) }); assert.equal(queries, before) + }) + assert.equal(connects, 0) + console.log("Routing measurement origin: 6 groups, actual caller fails before credentials outside genuine ALS; fake metadata SQL only, 0 physical DB/native calls; no broker/peer readiness") + } finally { pool.query = originalQuery; pool.connect = originalConnect; await closePool() } +}) diff --git a/backend/src/modules/mcp/routing-measurement-origin.ts b/backend/src/modules/mcp/routing-measurement-origin.ts new file mode 100644 index 00000000..5e19f299 --- /dev/null +++ b/backend/src/modules/mcp/routing-measurement-origin.ts @@ -0,0 +1,51 @@ +/** Fixed running-service origin guard. This authenticates the original routing + * measurement step; it is neither peer identity nor bandwidth/enrollment authority. */ +import { isDeepStrictEqual } from "node:util" +import { routeMeasurementSchema } from "@mmapp/contracts/mcp-routing" +import { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution } from "../../services/config-mutation-policy.js" +import { readCurrentMcpServiceExecution } from "./service-execution-origin.js" +import { McpError } from "./security.js" + +export interface RoutingMeasurementOriginGuard { readonly input: Readonly>; assertCurrent(): Promise } +function deny(): never { throw new McpError("PERMISSION_DENIED", "Исходный шаг измерения маршрутизации не подтверждён", 403) } +function snapshot(raw: unknown): Record { + const seen = new Set() + const check = (value: unknown): void => { + if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return + if (!value || typeof value !== "object" || seen.has(value)) deny() + seen.add(value) + const descriptors = Object.getOwnPropertyDescriptors(value) + if (Array.isArray(value)) { + if (Reflect.ownKeys(value).length !== value.length + 1) deny() + for (let index = 0; index < value.length; index++) { const entry = descriptors[String(index)]; if (!entry || !("value" in entry) || !entry.enumerable) deny(); check(entry.value) } + } else { + if (Object.getPrototypeOf(value) !== Object.prototype || Reflect.ownKeys(value).length !== Object.keys(descriptors).length) deny() + for (const entry of Object.values(descriptors)) { if (!("value" in entry) || !entry.enumerable) deny(); check(entry.value) } + } + seen.delete(value) + } + check(raw) + if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.getPrototypeOf(raw) !== Object.prototype) deny() + const result = structuredClone(raw) as Record + const freeze = (value: unknown): void => { if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } } + freeze(result); return result +} + +export async function createRoutingMeasurementOriginGuard(raw: Record): Promise> { + // Capture before the first asynchronous authorization. The caller cannot + // replace input while the genuine runner/SQL origin checks are pending. + const input = snapshot(raw), token = await captureVerifiedMcpServiceExecution(), original = await inspectVerifiedMcpServiceExecution(token) + if (original.operationName !== "diagnostics.bandwidth" || original.serviceName !== "routing.measure" || original.operationVersion !== 2 + || original.phase !== "forward" || !isDeepStrictEqual(input, original.serviceInput)) deny() + const { expectedEndpoints: _expectedEndpoints, ...measurement } = input + const parsed = routeMeasurementSchema.parse(measurement) + for (const id of new Set([parsed.srcServerId, parsed.dstServerId])) { + if (!original.serverIds.includes(id) || !original.objects.some(object => object.kind === "server" && object.serverId === id && object.id === String(id))) deny() + } + const assertCurrent = async () => { + const current = await readCurrentMcpServiceExecution(token, { deadlineAt: original.originalExpiresAt }) + if (!isDeepStrictEqual(current, original) || !isDeepStrictEqual(current.serviceInput, input)) deny() + } + await assertCurrent() + return Object.freeze({ input: original.serviceInput, assertCurrent }) +} diff --git a/backend/src/modules/mcp/routing-measurement-pool.sql-helper.ts b/backend/src/modules/mcp/routing-measurement-pool.sql-helper.ts new file mode 100644 index 00000000..547a5b7f --- /dev/null +++ b/backend/src/modules/mcp/routing-measurement-pool.sql-helper.ts @@ -0,0 +1,170 @@ +/** Disposable owned-local acceptance of the actual routing measurement runner. + * No CHR, source replacement, synthetic SQL results or production activation. */ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomBytes, randomUUID } from "node:crypto" +import { readFile, writeFile } from "node:fs/promises" +import { join } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import type { PoolClient } from "pg" +import { assertOwnedBandwidthSql } from "../../services/bandwidth-admission-process.sql-helper.js" + +export async function acceptRoutingMeasurementPoolSql(ownServerIds: number[]) { + assertOwnedBandwidthSql() + assert.equal(ownServerIds.length, 2); assert.equal(new Set(ownServerIds).size, 2); assert(ownServerIds.every(id => Number.isSafeInteger(id) && id > 0)) + const { pool } = await import("../../db/index.js"), { Client } = await import("pg") + const previousMax = pool.options.max; pool.options.max = 4 + const ids = [...ownServerIds].sort((a, b) => a - b), connections: string[] = [], operations = new Set(), observations = new Set() + const originalDriverQuery = Client.prototype.query + const username = "owned-routing-native", password = "OWNED_ROUTING_PRIVATE_" + randomUUID(), address = "192.0.2.2" + const native = { addresses: 0, ping: 0, bandwidth: 0 } + let nativeFailure: Error | undefined, rotateAfterInsert = false, rotationApplied = false, publisherPid = 0, publisherRollbacks = 0, publisherCommits = 0 + let patched = false, publisherPending=false, admin: PoolClient | undefined, originalEnabled: boolean | undefined + let originalServers: Record[] = [] + let restoreService: (() => void) | undefined, originalKey: Buffer | undefined + const keyPath = join(process.env.MCP_STATE_DIR!, "encryption-key"), rotatedKey = randomBytes(32) + const lockChecks: Array<{ keys: number; physicalLockOwners: number; separateFromAdmin: boolean }> = [] + const server = createServer(async (request, response) => { + try { + assert.equal(request.headers.authorization === "Basic " + Buffer.from(username + ":" + password).toString("base64"), true) + let text = ""; for await (const chunk of request) text += String(chunk) + if (request.method === "GET" && request.url === "/rest/ip/address") { + native.addresses++; response.setHeader("content-type", "application/json") + response.end(JSON.stringify([{ ".id": "*1", interface: "gre-remote", address: address + "/24", disabled: "false", invalid: "false" }])) + } else if (request.method === "POST" && request.url === "/rest/tool/ping") { + native.ping++; assert.deepEqual(JSON.parse(text), { address, interface: "gre-source", count: "3", interval: "0.3s" }) + response.setHeader("content-type", "application/json"); response.end(JSON.stringify([{ sent: "3", received: "3", "packet-loss": "0%", "avg-rtt": "1ms", "min-rtt": "1ms", "max-rtt": "1ms" }])) + } else if (request.method === "POST" && request.url === "/rest/tool/bandwidth-test") { + native.bandwidth++; const body = JSON.parse(text) + assert.equal(body.user === username && body.password === password, true) + const { user: _user, password: _password, ...publicBody } = body + assert.deepEqual(publicBody, { address, protocol: "tcp", direction: "both", duration: "3s", "local-tx-speed": "10000000", "remote-tx-speed": "10000000" }) + response.setHeader("content-type", "application/json"); response.end(JSON.stringify([{ status: "done testing", "tx-total-average": "2000000", "rx-total-average": "3000000" }])) + } else throw Error("Unexpected owned native request") + } catch { nativeFailure = Error("Owned localhost RouterOS contract failed"); response.statusCode = 500; response.end("Owned fixture refused") } + }) + try { + assert(pool.totalCount <= 4, "Dedicated owned runner must start with no more than four existing backends") + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const endpoint = server.address(); assert(endpoint && typeof endpoint !== "string") + // Hold one real app-pool client throughout both executions. All monitoring + // uses this client, leaving exactly three slots for the actual runner. + admin = await pool.connect(); const adminPid = Number((await admin.query("SELECT pg_catalog.pg_backend_pid() AS pid")).rows[0].pid) + originalServers = (await admin.query("SELECT id,host,port,username,password,use_ssl,verify_ssl,enabled FROM public.servers WHERE id=ANY($1::bigint[]) ORDER BY id", [ids])).rows + assert.equal(originalServers.length, 2) + originalEnabled = (await admin.query("SELECT enabled FROM public.mcp_settings WHERE id=1")).rows[0]?.enabled + assert.equal(typeof originalEnabled, "boolean") + await admin.query("UPDATE public.mcp_settings SET enabled=true WHERE id=1") + await admin.query("UPDATE public.servers SET host='127.0.0.1',port=$2,username=$3,password=$4,use_ssl=false,verify_ssl=true,enabled=true WHERE id=ANY($1::bigint[])", [ids, endpoint.port, username, password]) + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { createMcpConnection, authenticateMcpToken } = await import("./store.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const { findMcpService } = await import("./service-registry.js") + const service = findMcpService("routing.measure"); assert(service) + const execute = service.execute + service.execute = async (raw, context) => { + const result = await execute(raw, context) + if (context && context.phase === "forward" && operations.has(context.operationId)) { + const keys = ["mcp-operation:" + context.operationId, "mm-config:application", ...ids.map(id => "mm-config:" + id)] + const locks = (await admin!.query(`WITH wanted AS (SELECT k,pg_catalog.hashtextextended(k,0) AS h FROM pg_catalog.unnest($1::text[]) AS k) + SELECT w.k,l.pid,l.granted,l.mode FROM wanted w JOIN pg_catalog.pg_locks l + ON l.locktype='advisory' AND l.objsubid=1 + AND l.classid::bigint=((w.h>>32)&4294967295::bigint) AND l.objid::bigint=(w.h&4294967295::bigint) + WHERE l.granted ORDER BY w.k,l.pid`, [keys])).rows + assert.equal(locks.length, 4); assert.equal(new Set(locks.map(lock => lock.k)).size, 4) + assert(locks.every(lock => lock.granted === true && lock.mode === "ExclusiveLock")) + const owners = new Set(locks.map(lock => Number(lock.pid))) + assert.equal(owners.size, 1, "Operation/application/both server advisory locks must share one physical PID") + assert(!owners.has(adminPid)); lockChecks.push({ keys: 4, physicalLockOwners: owners.size, separateFromAdmin: true }) + assert(pool.totalCount <= 4) + } + return result + } + restoreService = () => { service.execute = execute } + Client.prototype.query = function (this: InstanceType, ...args: unknown[]) { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "" + const values = (typeof args[0] === "string" ? args[1] : (args[0] as { values?: unknown[] })?.values) as unknown[] | undefined + const pid = Number((this as unknown as { processID?: number }).processID) + const ownedInsert = /^INSERT INTO mcp_routing_observations\b/.test(text.trim()) && values?.[1] === ids[0] && values?.[2] === ids[1] + const result = Reflect.apply(originalDriverQuery, this, args) as unknown + if (!result || typeof (result as Promise).then !== "function") return result + return (result as Promise).then(async value => { + if (ownedInsert) { + publisherPid = pid;publisherPending=true; observations.add(String(values![0])) + if (rotateAfterInsert && !rotationApplied) { rotationApplied = true; await writeFile(keyPath, rotatedKey) } + } + if (publisherPending && pid === publisherPid && /^ROLLBACK\s*;?$/i.test(text.trim())) {publisherRollbacks++;publisherPending=false} + if (publisherPending && pid === publisherPid && /^COMMIT\s*;?$/i.test(text.trim())) {publisherCommits++;publisherPending=false} + return value + }) + } as typeof Client.prototype.query + patched = true + const run = async (negative: boolean) => { + const actor = { id: randomUUID(), email: "owned-routing@example.test", name: "Owned routing measurement actor", apps: ["mm"], + permissions: ["mm:network:write", "mm:network:read", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 180000 } + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned routing pool acceptance", mode: "approval", expiresAt: new Date(actor.expiresAt).toISOString(), + permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: ids, operationIds: ["diagnostics.bandwidth"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connections.push(access.connection.id) + const principal = await authenticateMcpToken(access.token) + const prepared = await invokeMcpTool(principal, "mm_routing_measurement_plan", { measurement: { srcServerId: ids[0], dstServerId: ids[1], srcInterface: "gre-source", dstInterface: "gre-remote", family: "ipv4", vrf: "main", bandwidth: true, durationSec: 3, maxMbps: 10 } }) as { planId: string } + await approvePlan(prepared.planId, actor, true) + const before = { ...native }; publisherPid = 0; publisherRollbacks = 0; publisherCommits = 0; rotateAfterInsert = negative; rotationApplied = false + const operation = await applyPlan(principal, prepared.planId, randomUUID()); operations.add(operation.id) + let terminal: Record | undefined + for (const deadline = Date.now() + 45000; Date.now() < deadline;) { + const current = (await admin!.query("SELECT state,result,error_code FROM public.mcp_operations WHERE id=$1", [operation.id])).rows[0] + assert(current) + if (!["pending", "running"].includes(current.state)) { terminal = current; break } + await delay(20) + } + assert(terminal, "Actual routing runner did not terminate with pool max4 and held admin client") + assert.equal(nativeFailure, undefined) + assert.deepEqual(native, { addresses: before.addresses + 1, ping: before.ping + 1, bandwidth: before.bandwidth + 1 }) + assert(publisherPid > 0 && publisherPid !== adminPid) + const steps = (await admin!.query("SELECT state,result FROM public.mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [operation.id])).rows + const safe = JSON.stringify({ terminal, steps }); assert(!safe.includes(password)); assert(!safe.includes(access.token)); assert(!safe.includes(username)) + if (negative) { + assert.equal(rotationApplied, true); assert(["partial", "unknown"].includes(String(terminal.state))); assert.notEqual(terminal.state, "applied") + assert.equal(publisherCommits, 0); assert(publisherRollbacks >= 1) + const rolledBack = (await admin!.query("SELECT id FROM public.mcp_routing_observations WHERE id=ANY($1::uuid[])", [[...observations]])).rows + assert.equal(rolledBack.length, 1, "Only the prior positive observation may remain after key-rotation rollback") + } else { + assert.equal(terminal.state, "applied"); assert.equal(steps.length, 1); assert.equal(steps[0].state, "verified"); assert.equal(publisherCommits, 1) + const result = steps[0].result as { observationId?: string }; assert(typeof result.observationId === "string") + const saved = (await admin!.query("SELECT id,src_server_id,dst_server_id,ping_ms,loss_pct,dl_mbps,ul_mbps,source_proof FROM public.mcp_routing_observations WHERE id=$1", [result.observationId])).rows + assert.equal(saved.length, 1); assert.equal(saved[0].src_server_id, ids[0]); assert.equal(saved[0].dst_server_id, ids[1]); assert.equal(Number(saved[0].ping_ms), 1); assert.equal(Number(saved[0].loss_pct), 0) + assert.equal(Number(saved[0].dl_mbps), 3); assert.equal(Number(saved[0].ul_mbps), 2); assert.match(saved[0].source_proof, /^[a-f0-9]{64}$/) + } + return String(terminal.state) + } + const positive = await run(false) + originalKey = await readFile(keyPath); assert.equal(originalKey.length, 32) + let negative: string + try { negative = await run(true) } finally { rotateAfterInsert = false; await writeFile(keyPath, originalKey) } + assert.equal(lockChecks.length, 1) + return { positive, negative, poolMax: 4, heldAdminClients: 1, lockChecks, nativeRequests: native, observationsPublished: 1, rotationAfterRealInsert: rotationApplied, + negativeCommitCount: publisherCommits, negativeRollbackCount: publisherRollbacks, realChrCalls: 0, syntheticOperatorActor: true, brokerActivation: false } + } finally { + rotateAfterInsert = false + if (patched) Client.prototype.query = originalDriverQuery + restoreService?.() + try { if (originalKey) await writeFile(keyPath, originalKey) } + finally { + try { + if (admin) { + if (observations.size) await admin.query("DELETE FROM public.mcp_routing_observations WHERE id=ANY($1::uuid[])", [[...observations]]) + if (connections.length) { + await admin.query("DELETE FROM public.mcp_operation_checkpoints WHERE operation_id IN (SELECT id FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[]))", [connections]) + await admin.query("DELETE FROM public.mcp_operation_steps WHERE operation_id IN (SELECT id FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[]))", [connections]) + await admin.query("DELETE FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[])", [connections]) + await admin.query("DELETE FROM public.mcp_plans WHERE connection_id=ANY($1::uuid[])", [connections]) + await admin.query("DELETE FROM public.mcp_requests WHERE connection_id=ANY($1::uuid[])", [connections]) + await admin.query("DELETE FROM public.mcp_connections WHERE id=ANY($1::uuid[])", [connections]) + } + for (const row of originalServers) await admin.query("UPDATE public.servers SET host=$2,port=$3,username=$4,password=$5,use_ssl=$6,verify_ssl=$7,enabled=$8 WHERE id=$1", [row.id, row.host, row.port, row.username, row.password, row.use_ssl, row.verify_ssl, row.enabled]) + if (originalEnabled !== undefined) await admin.query("UPDATE public.mcp_settings SET enabled=$1 WHERE id=1", [originalEnabled]) + } + } finally { admin?.release(); pool.options.max = previousMax; originalKey?.fill(0); rotatedKey.fill(0); server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) } + } + } +} diff --git a/backend/src/modules/mcp/routing-measurement.ts b/backend/src/modules/mcp/routing-measurement.ts new file mode 100644 index 00000000..85ef0fbb --- /dev/null +++ b/backend/src/modules/mcp/routing-measurement.ts @@ -0,0 +1,114 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { routeMeasurementSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { assertBandwidthTestCompleted } from "../../services/bandwidth-test-status.js" +import { readBandwidthTestRates } from "../../services/bandwidth-test-rates.js" +import { withBtestNodeLocks } from "../../services/btest-node-locks.js" +import { inspectControlPing } from "./path-verification.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import { createRoutingSourceContext, readRoutingEndpoints, type RoutingSourceRecord } from "./routing-observation-source.js" +import { readMeasurementAddress } from "./routing-measurement-address.js" +import { routingObservationColumns } from "./routing-observations.js" +import { createRoutingMeasurementOriginGuard, type RoutingMeasurementOriginGuard } from "./routing-measurement-origin.js" +import type { add } from "./actions.js" + +async function sourceContext(raw: Record) { + const { expectedEndpoints: _expected, ...measurement } = raw + const input = routeMeasurementSchema.parse(measurement), ids = [...new Set([input.srcServerId, input.dstServerId])] + const endpoints = await readRoutingEndpoints(ids) + if (endpoints.length !== ids.length || endpoints.some(row => !row.enabled)) throw new McpError("DEVICE_UNAVAILABLE", "Устройство измерения отключено или не найдено", 409) + return { input, endpoints, context: await createRoutingSourceContext(endpoints) } +} + +async function publish(record: RoutingSourceRecord, context: Awaited>, origin: Readonly) { + const authorize = () => origin.assertCurrent() + await authorize() + const client = await pool.connect() + try { + await authorize() + await client.query("BEGIN") + await context.assertCurrent(client, true) + await authorize() + const proof = context.signRecord(record) + const saved = await client.query(`INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,ping_ms,loss_pct,dl_mbps,ul_mbps,ping_at,bandwidth_at,context_verified,source,created_at,source_proof) + VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17) RETURNING id`, + [record.id, record.srcServerId, record.dstServerId, record.srcInterface, record.dstInterface, record.family, record.table, record.pingMs, record.lossPct, record.dlMbps, record.ulMbps, record.pingAt, record.bandwidthAt, record.contextVerified, record.source, record.createdAt, proof]) + if (saved.rowCount !== 1 || saved.rows[0].id !== record.id) throw new McpError("VERIFICATION_FAILED", "Сохранение измерения не подтверждено", 409) + await authorize() + const row = (await client.query(`SELECT ${routingObservationColumns} FROM mcp_routing_observations o WHERE o.id=$1`, [record.id])).rows[0] + if (!row || !context.verifyRecord(row as RoutingSourceRecord, row.sourceProof)) throw new McpError("VERIFICATION_FAILED", "Сохранённые значения измерения изменились; транзакция отменена", 409) + // Database row locks cannot pin the filesystem signing-key epoch. Recheck + // it after INSERT/read-back before publishing a verified transaction. + await context.assertCurrent(client, true) + await authorize() + await client.query("COMMIT") + } catch (error) { await client.query("ROLLBACK"); throw error } + finally { client.release() } +} + +export function registerRoutingMeasurement(register: typeof add) { + registerMcpService("routing.measure", { + current: async raw => ({ endpoints: (await sourceContext(raw)).context.expected }), + execute: async raw => { + const origin = await createRoutingMeasurementOriginGuard(raw) + await origin.assertCurrent() + const { input, endpoints, context } = await sourceContext(origin.input) + await origin.assertCurrent() + if (fingerprint(origin.input.expectedEndpoints) !== fingerprint(context.expected)) throw new McpError("STATE_CONFLICT", "Подключения измерения изменились после подготовки", 409) + const src = endpoints.find(row => row.id === input.srcServerId)!, dst = endpoints.find(row => row.id === input.dstServerId)! + const source = new MikrotikClient(src, src.id), destination = new MikrotikClient(dst, dst.id) + const nativeGuard = async () => { await origin.assertCurrent(); await context.assertCurrent(); await origin.assertCurrent() } + let pingAt: string | null = null, bandwidthAt: string | null = null + let pingMs: number | null = null, lossPct: number | null = null, dlMbps: number | null = null, ulMbps: number | null = null + let contextVerified = false, failed = false + try { + await nativeGuard() + const addresses = z.array(z.record(z.string(), z.unknown())).max(10000).parse(await destination.get(input.family === "ipv4" ? "/ip/address" : "/ipv6/address", 10000, undefined, undefined, nativeGuard)) + const selected = await readMeasurementAddress(addresses, input, async id => { + await nativeGuard() + return destination.post(input.family === "ipv4" ? "/ip/address/get" : "/ipv6/address/get", { ".id": id, "value-name": "disabled" }, 15000, undefined, nativeGuard) + }) + let target = selected.address + if (selected.linkLocal) target += "%" + input.srcInterface + const body: Record = { address: target, interface: input.srcInterface, count: "3", interval: "0.3s", ...(input.vrf !== "main" ? { vrf: input.vrf } : {}) } + await origin.assertCurrent() + const rows = z.array(z.record(z.string(), z.string())).max(256).parse(await source.post("/tool/ping", body, 10000, undefined, nativeGuard)) + pingAt = new Date().toISOString() + const { ping } = inspectControlPing(rows) + if (!ping.measured) throw new McpError("MEASUREMENT_UNKNOWN", "Нет завершённых счётчиков трёх пакетов", 409) + pingMs = ping.avgRtt; lossPct = ping.loss; contextVerified = !input.bandwidth + if (input.bandwidth) { + try { + await withBtestNodeLocks([src.id, dst.id], async () => { + await nativeGuard() + const result = await source.post("/tool/bandwidth-test", { address: target + (input.vrf === "main" ? "" : "@" + input.vrf), user: dst.username, password: dst.password, protocol: "tcp", direction: "both", duration: input.durationSec + "s", "local-tx-speed": String(input.maxMbps! * 1000000), "remote-tx-speed": String(input.maxMbps! * 1000000) }, (input.durationSec + 10) * 1000, undefined, nativeGuard) + assertBandwidthTestCompleted(result) + const rates = readBandwidthTestRates(result); dlMbps = rates.rxAvgMbps; ulMbps = rates.txAvgMbps + // A global destination alone still cannot attest the selected WAN. + contextVerified = selected.linkLocal + }) + } finally { bandwidthAt = new Date().toISOString() } + } + } catch { failed = true; contextVerified = false; pingAt ??= new Date().toISOString() } + const record: RoutingSourceRecord = { id: randomUUID(), srcServerId: src.id, dstServerId: dst.id, srcInterface: input.srcInterface, dstInterface: input.dstInterface, family: input.family, table: input.vrf, + pingMs, lossPct, dlMbps, ulMbps, pingAt, bandwidthAt, contextVerified, source: "routeros", createdAt: new Date().toISOString() } + await publish(record, context, origin) + if (failed) throw new McpError("MEASUREMENT_UNKNOWN", "Последняя попытка измерения не подтверждена и сохранена как unknown; старый успешный результат не используется вместо неё", 409) + return { observationId: record.id, pingMs, lossPct, dlMbps, ulMbps, pingAt, bandwidthAt, contextVerified, sourceVerified: true, warning: contextVerified ? null : "WAN bandwidth-test не подтверждён: показатель не используется для автономного выбора" } + }, + verify: async (_input, result) => !!result && typeof result === "object" && "observationId" in result, + }) + register("diagnostics.bandwidth", "mm_routing_measurement_plan", "План измерения между зарегистрированными устройствами", "network", "mm:network:write", "diagnostic", "servers", z.object({ measurement: routeMeasurementSchema }).strict(), { + version: 2, + target: input => { const measurement = routeMeasurementSchema.parse(input.measurement); return { scope: "servers", serverIds: [...new Set([measurement.srcServerId, measurement.dstServerId])] } }, + prepare: async input => { + const { input: measurement, context } = await sourceContext(input.measurement as Record) + const ids = [...new Set([measurement.srcServerId, measurement.dstServerId])] + return { target: { scope: "servers", serverIds: ids }, objects: ids.map(serverId => ({ kind: "server", id: String(serverId), serverId })), snapshots: [], before: { endpoints: context.expected }, + steps: [{ kind: "service", service: "routing.measure", input: { ...measurement, expectedEndpoints: context.expected } }], warnings: measurement.bandwidth ? ["Активная нагрузка ограничена скоростью и длительностью."] : [] } + }, + }) +} diff --git a/backend/src/modules/mcp/routing-model.test.ts b/backend/src/modules/mcp/routing-model.test.ts new file mode 100644 index 00000000..5ef4c9ea --- /dev/null +++ b/backend/src/modules/mcp/routing-model.test.ts @@ -0,0 +1,332 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { routeAiProfileSchema, type RouteObservation } from "@mmapp/contracts/mcp-routing" +import { rankRoutingCandidates, decideRoutingSwitch, selectFailoverReserveProbes, type ControllerState, type PathHealth } from "./routing-model.js" + +const now = Date.parse("2026-10-17T10:00:00Z"), at = new Date(now).toISOString() +const profile = routeAiProfileSchema.parse({ name: "Test", serverId: 1, community: "65000:10", family: "ipv4", candidates: [ + { id: "a", jumpHostId: 2, exitNodeId: 3, wanInterface: "wan-a", downstreamInterface: "exit-a", gateway: "10.0.0.1", gatewayRef: { resource: "ipv4_route", id: "*a" } }, + { id: "b", jumpHostId: 4, exitNodeId: 3, wanInterface: "wan-b", downstreamInterface: "exit-b", gateway: "10.0.0.2", gatewayRef: { resource: "ipv4_route", id: "*b" } }, +] }) +function sample(id: string, src: number, dst: number, iface: string, values: Partial = {}): RouteObservation { + return { id, srcServerId: src, dstServerId: dst, srcInterface: iface, dstInterface: "peer", family: "ipv4", table: "main", contextVerified: true, pingMs: 10, lossPct: 1, dlMbps: 100, ulMbps: 50, pingAt: at, bandwidthAt: at, ...values } +} +const a = [sample("a1", 1, 2, "wan-a"), sample("a2", 2, 3, "exit-a", { dlMbps: 80, ulMbps: 40 })] + +test("durable charge count limits a controller with empty legacy history without synthetic timestamps", () => { + const active = { ...profile, confirmationSamples: 1, cooldownSec: 0, switchThreshold: 0, hysteresis: 0 } + const ranking = rankRoutingCandidates(active, [...a.map(row => ({ ...row, pingMs: 100 })), sample("b1", 1, 4, "wan-b", { pingMs: 1 }), sample("b2", 4, 3, "exit-b", { pingMs: 1 })], now) + const allowed = decideRoutingSwitch(active, {}, ranking, "a", now, [], 0) + assert.equal(allowed.switchTo, "b") + const stopped = decideRoutingSwitch(active, {}, ranking, "a", now, [], active.maxSwitchesPerHour) + assert.equal(stopped.switchTo, null); assert.equal(stopped.state.pausedReason, "Лимит переключений") + assert.deepEqual(stopped.state.switches, []) + assert.throws(() => decideRoutingSwitch(active, { switches: [at] }, ranking, "a", now, [], 0)) +}) + +test("new unknown attempts block older successful data independently for each metric", () => { + const later = new Date(now + 1).toISOString() + const unknown = sample("unknown", 1, 2, "wan-a", { contextVerified: false, pingAt: later, bandwidthAt: null, pingMs: null, lossPct: null }) + let ranked = rankRoutingCandidates(profile, [...a, unknown], now + 1).find(row => row.candidate.id === "a")! + assert.equal(ranked.pingMs, null); assert.equal(ranked.eligible, false); assert.equal(ranked.dlMbps, 80) + const unknownBandwidth = { ...unknown, pingAt: null, bandwidthAt: later } + ranked = rankRoutingCandidates(profile, [...a, unknownBandwidth], now + 1).find(row => row.candidate.id === "a")! + assert.equal(ranked.pingMs, 20); assert.equal(ranked.dlMbps, null); assert.equal(ranked.eligible, false) + const pingOnly = routeAiProfileSchema.parse({ ...profile, weights: { ping: 100, speed: 0, loss: 0 } }) + assert.equal(rankRoutingCandidates(pingOnly, [...a, unknownBandwidth], now + 1).find(row => row.candidate.id === "a")!.eligible, true) + const tampered = { ...unknown, sourceVerified: false, recordedAt: later, pingAt: null, bandwidthAt: null } + ranked = rankRoutingCandidates(profile, [...a, tampered], now + 1).find(row => row.candidate.id === "a")! + assert.equal(ranked.pingMs, null); assert.equal(ranked.dlMbps, null) + ranked = rankRoutingCandidates(profile, [...a, { ...tampered, pingAt: new Date(now - 1).toISOString() }], now + 1).find(row => row.candidate.id === "a")! + assert.equal(ranked.pingMs, null, "Invalid latest publication cannot fall back by editing metric timestamps") +}) +test("failover requires three distinct failures and a freshly verified eligible standby", () => { + const ranking = rankRoutingCandidates(profile, [...a, sample("b1", 1, 4, "wan-b"), sample("b2", 4, 3, "exit-b")], now) + let state: ControllerState = {} + const health = (id: string, time = now, status: PathHealth["status"] = "failed"): PathHealth[] => { + const observedAt = new Date(time).toISOString() + return [{ id, candidateId: "a", status, observedAt }, { id: "reserve", candidateId: "b", status: "passed", observedAt }] + } + for (let i = 0; i < 5; i++) { const next = decideRoutingSwitch(profile, state, ranking, "a", now, health("one")); state = next.state; assert.equal(next.switchTo, null) } + assert.equal(state.failureCount, 1) + const secondAt = now + 60000, thirdAt = now + 120000 + const second = decideRoutingSwitch(profile, state, ranking, "a", secondAt, health("two", secondAt)); assert.equal(second.switchTo, null) + assert.equal(decideRoutingSwitch(profile, second.state, ranking, "a", thirdAt, health("three", thirdAt)).switchTo, "b") + const unknown = decideRoutingSwitch(profile, second.state, ranking, "a", thirdAt, health("three", thirdAt, "unknown")) + assert.equal(unknown.switchTo, null) + assert.equal(unknown.state.failureObservedAt, undefined) + assert.equal(decideRoutingSwitch(profile, { ...second.state, switches: [at, at, at] }, ranking, "a", thirdAt, health("three", thirdAt)).switchTo, null) + assert.equal(decideRoutingSwitch(profile, second.state, ranking, "a", thirdAt + 100000, health("three", thirdAt)).switchTo, null) +}) +test("failover starts a fresh series after downtime or legacy state and ignores reordered failures", () => { + const ranking = rankRoutingCandidates(profile, [...a, sample("b1", 1, 4, "wan-b"), sample("b2", 4, 3, "exit-b")], now) + const last = now + 60000 + const persisted: ControllerState = JSON.parse(JSON.stringify({ failureCount: 2, failureKey: "two", + failureObservedAt: new Date(last).toISOString(), failingCandidateId: "a" })) + const check = (state: ControllerState, id: string, time: number, clock = time) => { + const observedAt = new Date(time).toISOString() + return decideRoutingSwitch(profile, state, ranking, "a", clock, + [{ id, candidateId: "a", status: "failed", observedAt }, { id: "reserve", candidateId: "b", status: "passed", observedAt }]) + } + for (const result of [check(persisted, "two", last + 60000), check(persisted, "older", last - 1000, last), + check(persisted, "clock-rollback", last - 1000), check(persisted, "equal-time", last)]) { + assert.equal(result.switchTo, null) + assert.equal(result.state.failureCount, 2) + assert.equal(result.state.failureObservedAt, persisted.failureObservedAt) + } + for (const [state, time] of [[persisted, now + 4 * 3600000], [{ ...persisted, failureObservedAt: undefined }, last + 60000]] as const) { + const result = check(state, "next", time) + assert.equal(result.switchTo, null) + assert.equal(result.state.failureCount, 1) + assert.equal(result.state.failureObservedAt, new Date(time).toISOString()) + } +}) +test("bounded failover probing keeps the highest eligible reserve and eventually visits every other eligible probe", () => { + const controlProbe = { targetInterface: "exit-a", expectedHops: [{ serverId: 3, interface: "exit-a" }] } + const configured = routeAiProfileSchema.parse({ ...profile, excludedCandidateIds: ["05"], + candidates: Array.from({ length: 100 }, (_, index) => ({ ...profile.candidates[0], id: String(index).padStart(2, "0"), + controlProbe: index === 4 ? undefined : controlProbe })) }) + const ranking = rankRoutingCandidates(configured, a, now) + const expected = ranking.filter(row => row.eligible && row.candidate.controlProbe && !["00", "01"].includes(row.candidate.id)).map(row => row.candidate.id) + let cursor: string | undefined + for (const alternate of expected) { + const probes = selectFailoverReserveProbes(ranking, "00", cursor) + assert.deepEqual(probes.candidateIds, ["01", alternate]) + assert.equal(probes.cursor, alternate) + cursor = probes.cursor + } + assert.deepEqual(selectFailoverReserveProbes(ranking, "00", cursor).candidateIds, ["01", "02"]) + assert.deepEqual(selectFailoverReserveProbes(ranking, "00", "05").candidateIds, ["01", "02"]) + const pinned = rankRoutingCandidates({ ...configured, pinnedCandidateId: "09" }, a, now) + assert.deepEqual(selectFailoverReserveProbes(pinned, "00"), { candidateIds: ["09"], cursor: undefined }) + assert.deepEqual(selectFailoverReserveProbes(pinned, "09"), { candidateIds: [], cursor: undefined }) +}) +test("routing combines RTT, independent losses and bottleneck speed, never claims an observed path", () => { + const ranked = rankRoutingCandidates(profile, a, now).find(row => row.candidate.id === "a")! + assert.equal(ranked.pingMs, 20); assert.ok(Math.abs(ranked.lossPct! - 1.99) < 0.00001) + assert.equal(ranked.dlMbps, 80); assert.equal(ranked.ulMbps, 40); assert.equal(ranked.observedPath, false) +}) +test("fresh ping preserves an independent bandwidth sample for the exact same link", () => { + const ranked = rankRoutingCandidates(profile, [...a, sample("new-ping", 1, 2, "wan-a", { pingMs: 2, pingAt: new Date(now + 1).toISOString(), bandwidthAt: null, dlMbps: null, ulMbps: null })], now + 1).find(row => row.candidate.id === "a")! + assert.equal(ranked.pingMs, 12); assert.equal(ranked.dlMbps, 80) + assert.ok(ranked.observationIds.includes("a1")); assert.ok(ranked.observationIds.includes("new-ping")) +}) +test("reverse measurements swap download/upload relative to the analyzed source", () => { + const reverse = sample("reverse", 2, 1, "peer", { dstInterface: "wan-a", dlMbps: 30, ulMbps: 90 }) + const ranked = rankRoutingCandidates(profile, [reverse, a[1]], now).find(row => row.candidate.id === "a")! + assert.equal(ranked.dlMbps, 80); assert.equal(ranked.ulMbps, 30) +}) +test("unknown, stale, wrong VRF/family/interface and unverified samples do not rank", () => { + for (const values of [{ contextVerified: false }, { table: "other" }, { family: "ipv6" as const }, { srcInterface: "another" }, { pingAt: new Date(now - 301000).toISOString() }, { bandwidthAt: null }, { lossPct: null }]) { + const ranked = rankRoutingCandidates(profile, [{ ...a[0], ...values }, a[1]], now).find(row => row.candidate.id === "a")! + assert.equal(ranked.eligible, false); assert.equal(ranked.score, null) + } +}) +test("exclusions, pin and SLA prohibit an otherwise measurable candidate", () => { + for (const patch of [{ excludedCandidateIds: ["a"] }, { pinnedCandidateId: "b" }, { maxPingMs: 15 }, { minBandwidthMbps: 50 }]) { + assert.equal(rankRoutingCandidates({ ...profile, ...patch }, a, now).find(row => row.candidate.id === "a")!.eligible, false) + } +}) +test("controller does not count polling the same sample as consecutive confirmation", () => { + const observations = [...a.map(row => ({ ...row, pingMs: 100 })), sample("b1", 1, 4, "wan-b", { pingMs: 1 }), sample("b2", 4, 3, "exit-b", { pingMs: 1 })] + const ranks = rankRoutingCandidates(profile, observations, now) + let state: ControllerState = {} + for (let index = 0; index < 10; index++) { const next = decideRoutingSwitch(profile, state, ranks, "a", now); state = next.state; assert.equal(next.switchTo, null) } + assert.equal(state.consecutive, 1) + for (let index = 0; index < 2; index++) { + observations[2].id += "new"; const next = decideRoutingSwitch(profile, state, rankRoutingCandidates(profile, observations, now), "a", now); state = next.state + assert.equal(next.switchTo, index === 1 ? "b" : null) + } + assert.equal(decideRoutingSwitch(profile, { ...state, lastSwitchAt: at }, ranks, "a", now).switchTo, null) + assert.equal(decideRoutingSwitch(profile, { ...state, switches: [at, at, at] }, ranks, "a", now).switchTo, null) + assert.equal(decideRoutingSwitch(profile, state, ranks, null, now).switchTo, null) +}) + +test("controller counts only samples used for ranking or SLA and resets an old mixed streak", () => { + const slow = a.map(row => ({ ...row, pingMs: 100, bandwidthAt: null, dlMbps: null, ulMbps: null })) + const base = [...slow, sample("b1", 1, 4, "wan-b", { pingMs: 1, bandwidthAt: null, dlMbps: null, ulMbps: null }), + sample("b2", 4, 3, "exit-b", { pingMs: 1, bandwidthAt: null, dlMbps: null, ulMbps: null })] + const bandwidth = base.map(row => ({ ...row, id: `bw-${row.id}`, pingAt: null, pingMs: null, lossPct: null, + bandwidthAt: at, dlMbps: 100, ulMbps: 100 })) + const pingOnly = routeAiProfileSchema.parse({ ...profile, weights: { ping: 100, speed: 0, loss: 0 } }) + const decide = (configured: typeof profile, state: ControllerState, observations: RouteObservation[]) => { + const ranks = rankRoutingCandidates(configured, observations, now + 20) + const confirmation = new Map(ranks.map(row => [row.candidate.id, row.confirmation])) + return decideRoutingSwitch(configured, state, ranks, "a", now + 20, [], undefined, confirmation) + } + let state: ControllerState = { pendingCandidateId: "b", consecutive: 2, observationKey: "old-mixed-key" } + for (let i = 0; i < 3; i++) { + const observations = [...base, ...bandwidth, { ...bandwidth[2], id: `bw-new-${i}`, bandwidthAt: new Date(now + i + 1).toISOString() }] + const result = decide(pingOnly, state, observations) + assert.equal(result.switchTo, null) + assert.equal(result.state.consecutive, 1) + state = result.state + } + assert.equal(state.observationKey, "ping:a1:a2:b1:b2") + const fresh = (i: number) => [...base, ...bandwidth, { ...base[2], id: `ping-new-${i}`, pingAt: new Date(now + i + 1).toISOString() }] + const second = decide(pingOnly, state, fresh(1)) + assert.equal(second.state.consecutive, 2) + assert.equal(decide(pingOnly, second.state, fresh(1)).state.consecutive, 2) + assert.equal(decide(pingOnly, second.state, fresh(2)).switchTo, "b") + + const withBandwidthSla = routeAiProfileSchema.parse({ ...pingOnly, minBandwidthMbps: 10 }) + const initial = decide(withBandwidthSla, {}, [...base, ...bandwidth]) + const updated = decide(withBandwidthSla, initial.state, [...base, ...bandwidth, { + ...bandwidth[2], id: "new-sla-sample", bandwidthAt: new Date(now + 1).toISOString(), + }]) + assert.equal(initial.state.observationKey, "a1:a2:b1:b2:bw-a1:bw-a2:bw-b1:bw-b2") + assert.equal(updated.state.consecutive, 2) + + const bandwidthOnly = routeAiProfileSchema.parse({ ...profile, weights: { ping: 0, speed: 100, loss: 0 } }) + const unequalBandwidth = bandwidth.map((row, i) => ({ ...row, dlMbps: i < 2 ? 20 : 800, ulMbps: i < 2 ? 20 : 800 })) + const speedInitial = decide(bandwidthOnly, {}, [...base, ...unequalBandwidth]) + const speedWithNewPing = decide(bandwidthOnly, speedInitial.state, [...base, ...unequalBandwidth, fresh(1).at(-1)!]) + assert.equal(speedInitial.state.observationKey, "bandwidth:bw-a1:bw-a2:bw-b1:bw-b2") + assert.equal(speedWithNewPing.state.consecutive, 1) + assert.equal(speedWithNewPing.switchTo, null) +}) + +test("an unknown current path breaks the confirmation streak without making the last sample reusable", () => { + const configured = routeAiProfileSchema.parse({ ...profile, pingFreshnessSec: 10, weights: { ping: 100, speed: 0, loss: 0 } }) + const observations = [...a.map(row => ({ ...row, pingMs: 100 })), sample("b1", 1, 4, "wan-b", { pingMs: 1 }), sample("b2", 4, 3, "exit-b", { pingMs: 1 })] + const decide = (state: ControllerState, rows: RouteObservation[], time: number) => { + const ranks = rankRoutingCandidates(configured, rows, time) + const confirmation = new Map(ranks.map(row => [row.candidate.id, row.confirmation])) + return decideRoutingSwitch(configured, state, ranks, "a", time, [], undefined, confirmation) + } + const initial = decide({}, observations, now) + const newer = observations.map(row => ({ ...row, id: row.id + "-next", pingAt: new Date(now + 1000).toISOString() })) + const second = decide(initial.state, newer, now + 1000) + assert.equal(second.state.consecutive, 2) + const unavailable = decide(second.state, newer.slice(2), now + 1000) + assert.equal(decide(unavailable.state, newer, now + 1000).state.consecutive, 0) + const outageAt = now + 12_000 + const reserve = newer.slice(2).map(row => ({ ...row, id: row.id + "-outage", pingAt: new Date(outageAt).toISOString() })) + const outage = decide(second.state, [...newer, ...reserve], outageAt) + assert.equal(outage.state.consecutive, 0) + assert.equal(outage.state.pendingCandidateId, undefined) + assert.equal(outage.state.observationKey, second.state.observationKey) + assert.equal(outage.switchTo, null) + const restored = newer.map(row => ({ ...row, id: row.id + "-restored", pingAt: new Date(outageAt + 1000).toISOString() })) + const firstAfterOutage = decide(outage.state, restored, outageAt + 1000) + assert.equal(firstAfterOutage.state.consecutive, 1) + assert.equal(firstAfterOutage.switchTo, null) +}) + +test("missed ticks cannot carry expired or unbounded confirmations into a new comparison", () => { + const configured = routeAiProfileSchema.parse({ ...profile, pingFreshnessSec: 10, weights: { ping: 100, speed: 0, loss: 0 } }) + const rankAt = (time: number) => { + const pingAt = new Date(time).toISOString() + return rankRoutingCandidates(configured, [ + ...a.map(row => ({ ...row, pingMs: 100 })), + sample("b1", 1, 4, "wan-b", { pingMs: 1 }), sample("b2", 4, 3, "exit-b", { pingMs: 1 }), + ].map(row => ({ ...row, pingAt, id: `${row.id}-${time}` })), time) + } + const first = decideRoutingSwitch(configured, {}, rankAt(now), "a", now) + const secondAt = now + 1_000 + const second = decideRoutingSwitch(configured, first.state, rankAt(secondAt), "a", secondAt) + assert.equal(second.state.consecutive, 2) + const boundary = secondAt + 10_000 + const persisted: ControllerState = JSON.parse(JSON.stringify(second.state)) + assert.equal(decideRoutingSwitch(configured, persisted, rankAt(boundary), "a", boundary).switchTo, "b") + + for (const window of [persisted.confirmationWindow, undefined, { evaluatedAtMs: secondAt, validUntilMs: Infinity }]) { + const resumedAt = boundary + 1 + const resumed = decideRoutingSwitch(configured, { ...persisted, confirmationWindow: window }, rankAt(resumedAt), "a", resumedAt) + assert.equal(resumed.switchTo, null) + assert.equal(resumed.state.consecutive, 1) + } + const noBound = decideRoutingSwitch(configured, { ...persisted, confirmationWindow: undefined }, rankAt(secondAt), "a", secondAt) + assert.equal(noBound.state.consecutive, 0) + assert.equal(noBound.state.observationKey, persisted.observationKey) + const rolledBack = decideRoutingSwitch(configured, persisted, rankAt(now), "a", now) + assert.equal(rolledBack.state.consecutive, 1) + assert.equal(rolledBack.switchTo, null) +}) + +test("a decision delayed beyond the oldest relevant leg cannot use an earlier eligible ranking", () => { + const configured = routeAiProfileSchema.parse({ ...profile, pingFreshnessSec: 60, bandwidthFreshnessSec: 60, confirmationSamples: 1 }) + const ranks = rankRoutingCandidates(configured, [ + ...a.map(row => ({ ...row, pingMs: 100 })), sample("b1", 1, 4, "wan-b", { pingMs: 1 }), + sample("b2", 4, 3, "exit-b", { pingMs: 1, bandwidthAt: new Date(now - 59_000).toISOString() }), + ], now) + assert.equal(ranks.every(row => row.eligible), true) + const confirmation = new Map(ranks.map(row => [row.candidate.id, row.confirmation])) + const publicRanks = ranks.map(({ confirmation: _private, ...row }) => row) + assert.equal(decideRoutingSwitch(configured, {}, publicRanks, "a", now + 1_000, [], undefined, confirmation).switchTo, "b") + assert.equal(decideRoutingSwitch(configured, {}, publicRanks, "a", now + 1_001, [], undefined, confirmation).switchTo, null) + assert.equal(decideRoutingSwitch(configured, {}, publicRanks, "a", now, [], undefined, new Map()).switchTo, null) + + const beforeThirdFailure = { failureCount: 2, failureKey: "previous", failureObservedAt: at, failingCandidateId: "a" } + const failedOverAt = (time: number) => { + const observedAt = new Date(time).toISOString() + return decideRoutingSwitch(configured, beforeThirdFailure, publicRanks, "a", time, [ + { id: "third", candidateId: "a", status: "failed", observedAt }, + { id: "reserve", candidateId: "b", status: "passed", observedAt }, + ], undefined, confirmation) + } + assert.equal(failedOverAt(now + 1_000).switchTo, "b") + assert.equal(failedOverAt(now + 1_001).switchTo, null) + assert.equal(failedOverAt(now + 1_001).state.failureCount, 3) +}) + +test("hysteresis retains pending confirmation without counting sub-threshold observations or releasing cooldown", () => { + const ranked = rankRoutingCandidates(profile, [...a, sample("b1", 1, 4, "wan-b"), sample("b2", 4, 3, "exit-b")], now) + const compare = (state: ControllerState, gain: number, observation: string, time = now) => { + const ranks = ["b", "a"].map(id => ({ ...ranked.find(row => row.candidate.id === id)!, + score: id === "a" ? 50 : 50 + gain, observationIds: [id + observation] })) + return decideRoutingSwitch(profile, state, ranks, "a", time) + } + + const initial = compare({}, 15, "1").state + assert.equal(initial.consecutive, 1) + for (const [gain, observation] of [[6, "2"], [5, "3"]] as const) { + const result = compare(initial, gain, observation) + assert.equal(result.switchTo, null) + assert.deepEqual(result.state, initial) + } + const reset = compare(initial, 4, "4") + assert.equal(reset.state.pendingCandidateId, undefined) + assert.equal(reset.state.consecutive, 0) + + const second = compare(initial, 15, "5") + assert.equal(second.switchTo, null) + const afterCooldown = now + 100_000 + const lastSwitchAt = new Date(afterCooldown - profile.cooldownSec * 1000).toISOString() + const third = compare({ ...second.state, lastSwitchAt }, 15, "6") + assert.equal(third.state.consecutive, 3) + assert.equal(third.switchTo, null) + + assert.equal(compare(third.state, 6, "7", afterCooldown).switchTo, null) + assert.equal(compare(third.state, 15, "8", afterCooldown).switchTo, "b") +}) + +test("a different pending candidate cannot borrow confirmations from a shared observation key", () => { + const ranks = rankRoutingCandidates(profile, [...a, sample("b1", 1, 4, "wan-b"), sample("b2", 4, 3, "exit-b")], now) + .map(row => ({ ...row, score: row.candidate.id === "a" ? 50 : 75 })) + .sort((left, right) => right.score - left.score) + const observationKey = ranks.flatMap(row => row.observationIds).sort().join(":") + const persisted: ControllerState = { pendingCandidateId: "previous-candidate", consecutive: 3, observationKey } + const selected = decideRoutingSwitch(profile, persisted, ranks, "a", now) + assert.equal(selected.switchTo, null) + assert.equal(selected.state.pendingCandidateId, "b") + assert.equal(selected.state.consecutive, 0) + assert.equal(selected.state.observationKey, observationKey) + + const replay = decideRoutingSwitch(profile, selected.state, ranks, "a", now) + assert.equal(replay.switchTo, null) + assert.equal(replay.state.consecutive, 0) + + const freshRanks = [{ ...ranks[0], observationIds: ["new-b-1", "new-b-2"] }, ranks[1]] + const fresh = decideRoutingSwitch(profile, replay.state, freshRanks, "a", now) + assert.equal(fresh.state.consecutive, 1) + assert.equal(fresh.switchTo, null) + + const belowFloor = [{ ...freshRanks[0], score: 54 }, freshRanks[1]] + const reset = decideRoutingSwitch(profile, fresh.state, belowFloor, "a", now) + assert.equal(reset.state.pendingCandidateId, undefined) + assert.equal(reset.state.consecutive, 0) + const replayAfterReset = decideRoutingSwitch(profile, reset.state, freshRanks, "a", now) + assert.equal(replayAfterReset.switchTo, null) + assert.equal(replayAfterReset.state.consecutive, 0) +}) diff --git a/backend/src/modules/mcp/routing-model.ts b/backend/src/modules/mcp/routing-model.ts new file mode 100644 index 00000000..7c17e542 --- /dev/null +++ b/backend/src/modules/mcp/routing-model.ts @@ -0,0 +1,172 @@ +import type { RouteAiProfile, RouteObservation } from "@mmapp/contracts/mcp-routing" + +export function fresh(timestamp: string | null, seconds: number, now: number): boolean { + const measured = timestamp === null ? NaN : Date.parse(timestamp) + return Number.isFinite(measured) && measured <= now && now - measured <= seconds * 1000 +} +function confirmationMetrics(profile: RouteAiProfile) { + return { + ping: profile.weights.ping > 0 || profile.weights.loss > 0 || profile.maxPingMs !== null || profile.maxLossPct !== null, + bandwidth: profile.weights.speed > 0 || profile.minBandwidthMbps !== null, + } +} +export function rankRoutingCandidates(profile: RouteAiProfile, observations: RouteObservation[], now = Date.now()) { + const metrics = confirmationMetrics(profile) + function leg(src: number, dst: number, iface: string, remoteIface?: string) { + const possible = observations.flatMap(observation => { + if (observation.family !== profile.family || observation.table !== profile.table) return [] + if (observation.srcServerId === src && observation.dstServerId === dst && observation.srcInterface === iface && (!remoteIface || observation.dstInterface === remoteIface)) return [observation] + if (observation.srcServerId === dst && observation.dstServerId === src && observation.dstInterface === iface && (!remoteIface || observation.srcInterface === remoteIface)) return [{ ...observation, dlMbps: observation.ulMbps, ulMbps: observation.dlMbps }] + return [] + }) + const peerInterfaces = new Set(possible.map(value => value.srcServerId === src ? value.dstInterface : value.srcInterface)) + if (!remoteIface && peerInterfaces.size > 1) return undefined + // Collectors run independently. A new ping must not erase a still fresh + // bandwidth sample for the same exact directional link/family/table. + const latest = (field: "pingAt" | "bandwidthAt") => possible.filter(value => value[field] !== null || value.sourceVerified === false) + .sort((a, b) => Date.parse(b.recordedAt ?? b[field]!) - Date.parse(a.recordedAt ?? a[field]!) + || (b.recordedAt ?? b[field] ?? "").localeCompare(a.recordedAt ?? a[field] ?? ""))[0] + const ping = latest("pingAt"), bandwidth = latest("bandwidthAt"), base = ping ?? bandwidth + return base ? { ...base, pingMs: ping?.contextVerified ? ping.pingMs : null, lossPct: ping?.contextVerified ? ping.lossPct : null, pingAt: ping?.pingAt ?? null, + dlMbps: bandwidth?.contextVerified ? bandwidth.dlMbps : null, ulMbps: bandwidth?.contextVerified ? bandwidth.ulMbps : null, bandwidthAt: bandwidth?.bandwidthAt ?? null, + sampleIds: [...new Set([ping?.id, bandwidth?.id].filter((id): id is string => !!id))], + confirmationIds: [metrics.ping ? ping?.id : undefined, metrics.bandwidth ? bandwidth?.id : undefined].filter((id): id is string => !!id) } : undefined + } + return profile.candidates.map(candidate => { + const first = leg(profile.serverId, candidate.jumpHostId, candidate.wanInterface, candidate.wanRemoteInterface) + const second = leg(candidate.jumpHostId, candidate.exitNodeId, candidate.downstreamInterface, candidate.downstreamRemoteInterface) + const reasons: string[] = [] + if (profile.excludedCandidateIds.includes(candidate.id)) reasons.push("Путь исключён") + if (profile.pinnedCandidateId && profile.pinnedCandidateId !== candidate.id) reasons.push("Закреплён другой путь") + const legs = [first, second] + if (legs.some(value => !value)) reasons.push("Нет подтверждённых измерений обоих участков") + const pingKnown = legs.every(value => value?.pingMs !== null && value?.pingMs !== undefined && fresh(value.pingAt, profile.pingFreshnessSec, now)) + const lossKnown = legs.every(value => value?.lossPct !== null && value?.lossPct !== undefined && fresh(value.pingAt, profile.pingFreshnessSec, now)) + const speedKnown = legs.every(value => value?.dlMbps !== null && value?.dlMbps !== undefined && value.ulMbps !== null && fresh(value.bandwidthAt, profile.bandwidthFreshnessSec, now)) + const pingMs = pingKnown ? first!.pingMs! + second!.pingMs! : null + const lossPct = lossKnown ? 100 * (1 - (1 - first!.lossPct! / 100) * (1 - second!.lossPct! / 100)) : null + const dlMbps = speedKnown ? Math.min(first!.dlMbps!, second!.dlMbps!) : null + const ulMbps = speedKnown ? Math.min(first!.ulMbps!, second!.ulMbps!) : null + if ((profile.weights.ping > 0 || profile.maxPingMs !== null) && pingMs === null) reasons.push("RTT неизвестен или устарел") + if ((profile.weights.loss > 0 || profile.maxLossPct !== null) && lossPct === null) reasons.push("Потери неизвестны или устарели") + if ((profile.weights.speed > 0 || profile.minBandwidthMbps !== null) && dlMbps === null) reasons.push("Bandwidth неизвестен или устарел") + if (pingMs !== null && profile.maxPingMs !== null && pingMs > profile.maxPingMs) reasons.push("Превышен RTT") + if (lossPct !== null && profile.maxLossPct !== null && lossPct > profile.maxLossPct) reasons.push("Превышены потери") + if (dlMbps !== null && ulMbps !== null && profile.minBandwidthMbps !== null && Math.min(dlMbps, ulMbps) < profile.minBandwidthMbps) reasons.push("Недостаточна пропускная способность") + const score = reasons.length ? null : Math.round((profile.weights.ping * Math.max(0, 100 - (pingMs ?? 0) * 0.6) + profile.weights.speed * Math.min(100, ((dlMbps ?? 0) + (ulMbps ?? 0)) / 18) + profile.weights.loss * (100 - (lossPct ?? 0))) / 100) + const confirmation = reasons.length ? undefined : { + ids: legs.flatMap(value => value?.confirmationIds ?? []), + evaluatedAtMs: now, + validUntilMs: Math.min(...legs.flatMap(value => [ + ...(metrics.ping ? [Date.parse(value!.pingAt!) + profile.pingFreshnessSec * 1000] : []), + ...(metrics.bandwidth ? [Date.parse(value!.bandwidthAt!) + profile.bandwidthFreshnessSec * 1000] : []), + ])), + } + return { candidate, eligible: !reasons.length, score, reasons, pingMs, lossPct, dlMbps, ulMbps, + observationIds: legs.flatMap(value => value?.sampleIds ?? []), confirmation, observedPath: false as const } + }).sort((a, b) => (b.score ?? -1) - (a.score ?? -1) || a.candidate.id.localeCompare(b.candidate.id)) +} + +export interface PathHealth { id: string; candidateId: string; status: "passed" | "failed" | "unknown"; observedAt: string } +interface ConfirmationWindow { evaluatedAtMs: number; validUntilMs: number } +export interface ControllerState { pendingCandidateId?: string; consecutive?: number; observationKey?: string; confirmationWindow?: ConfirmationWindow; lastSwitchAt?: string; switches?: string[]; operationId?: string; pausedReason?: string; failureCount?: number; failureKey?: string; failureObservedAt?: string; failingCandidateId?: string } + +type RoutingRankingRow = ReturnType[number] +type ConfirmationObservation = NonNullable +export type PublicRoutingRanking = Array> +export type RoutingConfirmationObservations = ReadonlyMap + +export function selectFailoverReserveProbes(ranking: PublicRoutingRanking, currentCandidateId: string, previous?: string) { + const standbys = ranking.filter(({ candidate, eligible }) => eligible && candidate.id !== currentCandidateId && candidate.controlProbe) + .map(({ candidate }) => candidate.id) + const [preferred, ...remaining] = standbys + if (remaining.length === 0) return { candidateIds: standbys, cursor: undefined } + const index = remaining.indexOf(previous ?? "") + const cursor = remaining[(index + 1) % remaining.length] + return { candidateIds: [preferred, cursor], cursor } +} + +const pathHealthFreshnessSec = 90 +export function decideRoutingSwitch(profile: RouteAiProfile, state: ControllerState, ranking: Array>>, currentCandidateId: string | null, now = Date.now(), health: PathHealth[] = [], chargedSwitchesPerHour?: number, confirmation?: RoutingConfirmationObservations) { + const next: ControllerState = { ...state, switches: (state.switches ?? []).filter(at => now - Date.parse(at) < 3600000) } + const metrics = confirmationMetrics(profile) + const maxWindowMs = Math.min(metrics.ping ? profile.pingFreshnessSec * 1000 : Infinity, + metrics.bandwidth ? profile.bandwidthFreshnessSec * 1000 : Infinity) + const windowIsCurrent = (window: ConfirmationWindow | undefined) => !!window && + Number.isSafeInteger(window.evaluatedAtMs) && Number.isSafeInteger(window.validUntilMs) && + window.evaluatedAtMs <= now && now <= window.validUntilMs && window.validUntilMs - window.evaluatedAtMs <= maxWindowMs + const observationFor = (row: typeof ranking[number]) => confirmation ? confirmation.get(row.candidate.id) : row.confirmation + const resetConfirmation = () => { next.pendingCandidateId = undefined; next.consecutive = 0; next.confirmationWindow = undefined } + // The previous comparison must still be valid even when no controller tick ran during the gap. + // Keep its key so restarting a streak cannot count a replay. + if ((state.consecutive ?? 0) > 0 && !windowIsCurrent(state.confirmationWindow)) { + next.consecutive = 0 + next.confirmationWindow = undefined + } + if (chargedSwitchesPerHour !== undefined && (!Number.isSafeInteger(chargedSwitchesPerHour) || chargedSwitchesPerHour < next.switches!.length || chargedSwitchesPerHour > 4096)) throw Error("Invalid common routing charge count") + const switchCount = chargedSwitchesPerHour ?? next.switches!.length + const best = ranking.find(candidate => candidate.eligible), current = ranking.find(candidate => candidate.candidate.id === currentCandidateId) + const currentHealth = health.find(value => value.candidateId === currentCandidateId && fresh(value.observedAt, pathHealthFreshnessSec, now)) + if (currentHealth?.status === "failed" && currentCandidateId && !state.pausedReason) { + const observed = Date.parse(currentHealth.observedAt), previous = Date.parse(state.failureObservedAt ?? "") + const sameCandidate = state.failingCandidateId === currentCandidateId + if (state.failureKey === currentHealth.id) { + if (!sameCandidate || observed !== previous) return { state: next, switchTo: null, reason: "Ожидание новой проверки отказа" } + } else { + if (observed <= previous) return { state: next, switchTo: null, reason: "Ожидание новой проверки отказа" } + const continues = sameCandidate && fresh(state.failureObservedAt ?? null, pathHealthFreshnessSec, observed) + next.failureCount = continues ? (state.failureCount ?? 0) + 1 : 1 + next.failureKey = currentHealth.id; next.failureObservedAt = currentHealth.observedAt; next.failingCandidateId = currentCandidateId + } + const reserve = ranking.find(value => value.eligible && value.candidate.id !== currentCandidateId && windowIsCurrent(observationFor(value)) && health.some(check => check.candidateId === value.candidate.id && check.status === "passed" && fresh(check.observedAt, pathHealthFreshnessSec, now))) + if ((next.failureCount ?? 0) < 3 || !reserve) return { state: next, switchTo: null, reason: "Failover ожидает три отдельных отказа и подтверждённый резерв" } + if (switchCount >= profile.maxSwitchesPerHour) return { state: { ...next, pausedReason: "Лимит переключений" }, switchTo: null, reason: "Исчерпан часовой бюджет, включая failover" } + if (next.lastSwitchAt && now - Date.parse(next.lastSwitchAt) < profile.cooldownSec * 1000) return { state: next, switchTo: null, reason: "Failover ожидает окончания cooldown" } + return { state: { ...next, failureCount: 0 }, switchTo: reserve.candidate.id, reason: "Failover: три подтверждённых отказа и проверенный резерв" } + } + next.failureCount = 0; next.failureKey = undefined; next.failureObservedAt = undefined; next.failingCandidateId = undefined + if (state.pausedReason || !currentCandidateId || !best || best.candidate.id === currentCandidateId) { + resetConfirmation() + return { state: next, switchTo: null, reason: "Нет подтверждённого улучшения или текущего пути" } + } + if (!current?.eligible || current.score === null) { + resetConfirmation() + return { state: next, switchTo: null, reason: "Состояние текущего пути неизвестно; failover требует отдельных подтверждённых проверок отказа" } + } + const bestEvidence = observationFor(best), currentEvidence = observationFor(current) + if (!bestEvidence?.ids.length || !currentEvidence?.ids.length || !windowIsCurrent(bestEvidence) || !windowIsCurrent(currentEvidence)) { + resetConfirmation() + return { state: next, switchTo: null, reason: "Нет подтверждённых измерений для выбора" } + } + const gain = best.score! - current.score + if (gain < Math.max(0, profile.switchThreshold - profile.hysteresis)) { + resetConfirmation() + return { state: next, switchTo: null, reason: "Сработал гистерезис" } + } + if (gain < profile.switchThreshold) return { state: next, switchTo: null, reason: "Порог улучшения не достигнут" } + let observationKey: string + if (metrics.ping && metrics.bandwidth) { + observationKey = [...best.observationIds, ...current.observationIds].sort().join(":") + } else { + const prefix = metrics.ping ? "ping:" : "bandwidth:" + observationKey = prefix + [...new Set([...bestEvidence.ids, ...currentEvidence.ids])].sort().join(":") + // Old streaks may include samples irrelevant to the current policy. + if (!next.observationKey?.startsWith(prefix)) next.consecutive = 0 + } + if (next.pendingCandidateId !== undefined && next.pendingCandidateId !== best.candidate.id) { + resetConfirmation() + next.pendingCandidateId = best.candidate.id + } + if (observationKey !== next.observationKey) { + next.consecutive = next.pendingCandidateId === best.candidate.id ? (next.consecutive ?? 0) + 1 : 1 + next.pendingCandidateId = best.candidate.id; next.observationKey = observationKey + next.confirmationWindow = { + evaluatedAtMs: Math.max(bestEvidence.evaluatedAtMs, currentEvidence.evaluatedAtMs), + validUntilMs: Math.min(bestEvidence.validUntilMs, currentEvidence.validUntilMs), + } + } + if (next.pendingCandidateId !== best.candidate.id || (next.consecutive ?? 0) < profile.confirmationSamples) return { state: next, switchTo: null, reason: "Ожидание новых измерений" } + if (next.lastSwitchAt && now - Date.parse(next.lastSwitchAt) < profile.cooldownSec * 1000) return { state: next, switchTo: null, reason: "Cooldown" } + if (switchCount >= profile.maxSwitchesPerHour) return { state: { ...next, pausedReason: "Лимит переключений" }, switchTo: null, reason: "Исчерпан часовой бюджет переключений" } + return { state: next, switchTo: best.candidate.id, reason: "Улучшение подтверждено свежими наблюдениями" } +} diff --git a/backend/src/modules/mcp/routing-native-endpoint.ts b/backend/src/modules/mcp/routing-native-endpoint.ts new file mode 100644 index 00000000..aac1ac45 --- /dev/null +++ b/backend/src/modules/mcp/routing-native-endpoint.ts @@ -0,0 +1,28 @@ +import { isIP } from "node:net" + +export function parseRoutingEndpoint(value: unknown) { + if (typeof value !== "string" || !value.length || value.length > 256) throw Error("missing-or-unsupported-field") + const match = /^([^%@/,\s]+)(?:\/(\d+))?(?:%([^%@/,\s]+))?(?:@([^%@/,\s]+))?$/.exec(value) + if (!match) throw Error("unsupported-endpoint") + const [, address, prefix, interfaceName, vrf] = match + const family = isIP(address) + if (!family) throw Error("unsupported-address") + if (prefix !== undefined && prefix !== String(family === 4 ? 32 : 128)) throw Error("unsupported-peer-range") + return { address, interface: interfaceName, vrf } +} + +export function matchesBgpSessionContext( + row: Record, + expected: { table: string; vrf: string }, + local: ReturnType, + remote: ReturnType, +) { + if (!Object.hasOwn(row, "routing-table") || row["routing-table"] !== expected.table) return false + if (Object.hasOwn(row, "vrf")) { + return row.vrf === expected.vrf && (local.vrf === undefined || local.vrf === expected.vrf) && + (remote.vrf === undefined || remote.vrf === expected.vrf) + } + // RouterOS may report the transport VRF only in the two runtime addresses. + // The installation table alone cannot identify it. + return local.vrf === expected.vrf && remote.vrf === expected.vrf +} diff --git a/backend/src/modules/mcp/routing-network-configuration-bridge.test.ts b/backend/src/modules/mcp/routing-network-configuration-bridge.test.ts new file mode 100644 index 00000000..3d1da0bf --- /dev/null +++ b/backend/src/modules/mcp/routing-network-configuration-bridge.test.ts @@ -0,0 +1,316 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { z } from "zod" +import ts from "typescript" +import { scopedTargetRuleMatches } from "./routing-community-scoped-model.js" + +/** Inert dependency-boundary tests only. SQL/HTTP acceptance must create the + * real checkpoint through the existing manual service and production HMAC; + * these providers never establish genuine checkpoint acceptance. */ +async function fixture() { + let now = Date.now(), revoked = false, sourceChanged = false, checkpointMissing = false, checkpointChanged = false, guard = false + let nativeReads = 0, profileChecks = 0, sourceReads = 0, configChecks = 0 + let nativeBody = "desired", nativeDependency = "dependency", nativeTarget = "desired-target", nativeCandidate = "b", nativeId = "*1", generation = "generation" + let afterNative: (() => void) | undefined, afterCheckpoint: (() => void) | undefined, afterProfile: (() => void) | undefined + let sharedReads = 0, sharedClosed = false, manualOwner = false, reservedOwner = false, topologyAvailable = true + class ClockDate extends Date { static now() { return now } } + class BridgeError extends Error { constructor(readonly code: string, message: string) { super(message) } } + const fingerprint = (value: unknown) => JSON.stringify(value) + const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new ClockDate(now + 600000).toISOString() } + const connection = { id: principal.connectionId, owner_id: "owner", generation, owner_valid_until: new ClockDate(now + 600000), + owner_permissions: ["mm:network:read", "mm:filters:write"], policy: { mode: "operator", expiresAt: new ClockDate(now + 600000).toISOString(), + permissions: ["mm:network:read", "mm:filters:write"], scope: { serverIds: [1], operationIds: ["routing.community.switch"] } } } + const profile = { serverId: 1, table: "tenant", autoRecovery: false, candidates: [{ id: "a", jumpHostId: 1, exitNodeId: 1 }, { id: "b", jumpHostId: 1, exitNodeId: 1 }] } + const authority = { packet: { profileId: randomUUID(), profileVersion: 1, validUntil: new ClockDate(now + 600000).toISOString() }, proof: "authority" } + const input: Record = { serverId: 1, profile, principal, ownerId: "owner", connectionGeneration: generation, policyProof: fingerprint(connection.policy), generation, + proofRevision: 3, persistentProfileAuthority: authority, requestProof: "signed", receiptId: randomUUID(), candidateId: "b", originalCandidateId: "a", + nativeId: "*1", dependencyProof: "dependency", desiredTargetProof: "desired-target", contextSources: [{ serverId: 1, proof: "source" }] } + const step = { kind: "service", service: "routing.community.scoped", input } + const evidence = { target: { scope: "servers", serverIds: [1] }, steps: [step] } + const row = { id: randomUUID(), state: "applied", created_at: new ClockDate(now - 1000), plan_id: randomUUID(), operation_id: "routing.community.switch", + operation_version: 7, approved_by: "owner", expires_at: new ClockDate(now + 600000), evidence: { sealed: JSON.stringify(evidence) }, step_state: "verified", intended_state: step } + const packet = { revision: 2, phase: "CONFIG_APPLIED", operationId: row.id, stepIndex: 0, serverId: 1, nativeId: "*1", desiredRule: "desired", + dependencyProof: "dependency", desiredTargetProof: "desired-target", desiredCandidateId: "b", contextSources: input.contextSources, persistentProfileAuthority: authority } + const checkpoint = { checkpointProof: "persisted-checkpoint", observedAt: new ClockDate(now - 500).toISOString(), packet } + const sharedToken = Object.freeze({}), acquisition = Object.freeze({}), sharedBinding = { sourceOperationId: row.id, profile: structuredClone(profile), principal: structuredClone(principal), + deadline: now + 10000, contextSources: structuredClone(input.contextSources), candidateId: "b" } + let substitutedTarget: string | undefined + const ownedTarget = () => ({ nativeId, rawRow: { rule: nativeBody }, + matchesRuleProof: (body: string, proof: unknown) => scopedTargetRuleMatches({ rawRow: { rule: nativeBody }, + desiredTargetProof: observed => observed === nativeBody ? nativeTarget : observed === packet.desiredRule ? substitutedTarget ?? "" : "" }, body, proof) }) + const providers: Record = { + "./routing-network-execution-session.js": { isVerifiedManualEffectOwner: (owner: unknown) => owner === sharedToken && manualOwner, + assertVerifiedReservedEffectOwner: async (owner: unknown, operationId: string, actor: unknown) => { + assert.equal(operationId, row.id); assert.deepEqual(actor, principal) + if (owner !== sharedToken || !reservedOwner) throw Error("Reserved attempt unavailable") + return sharedBinding.deadline + } }, + zod: { z }, "@mmapp/contracts/mcp-routing": { routeAiProfileSchema: { parse: (value: unknown) => value } }, + "../../db/index.js": { pool: { query: async () => { sourceReads++; return { rows: [{ ...row, plan_id: sourceChanged ? "changed" : row.plan_id }] } } } }, + "../../lib/permissions.js": { hasPermission: (values: string[], permission: string) => values.includes(permission) }, + "./store.js": { effectiveMcpConnection: async () => { if (revoked) throw Error("revoked"); return connection } }, + "./registry.js": { findMcpOperation: () => ({ version: 7 }) }, + "./routing-network-scoped-context.js": { readRoutingNetworkScopedCommunityContext: async (token: unknown) => { + if (token !== sharedToken || sharedClosed) throw Error("Unknown shared context") + sharedReads++; nativeReads++; if (revoked) throw Error("revoked") + return { before: { scopedExecutionAvailable: !guard, scopedExecutionReason: guard ? "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE" : null, + dependencyProof: nativeDependency, targetRuleProof: nativeTarget, currentCandidateId: nativeCandidate, sourceProofs: input.contextSources }, + ownedTarget: ownedTarget(), fence: async () => { if (revoked || sharedClosed) throw Error("revoked") } } + } }, + "./routing-network-native-session.js": { inspectRoutingNetworkSharedContext: (token: unknown) => { if (token !== sharedToken || sharedClosed) throw Error("Unknown shared context"); return sharedBinding }, + assertRoutingNetworkSharedContextReadOwner: (owner: unknown, read: unknown) => { if (owner !== sharedToken || read !== acquisition || sharedClosed) throw Error("Unknown acquisition owner") }, + closeRoutingNetworkSharedContext: (token: unknown) => { if (token === sharedToken) sharedClosed = true } }, + "./security.js": { fingerprint, redact: (value: unknown) => value, McpError: BridgeError, accessGeneration: async () => generation, decryptSecret: async (value: string) => value }, + "./routing-community-scoped-request.js": { assertScopedCommunityRequest: async (value: Record) => { if (value.requestProof !== "signed") throw Error("request HMAC invalid"); return "request-seal" } }, + "./routing-profile-authority.js": { routingProfileAuthorityBindingSchema: { parse: (value: unknown) => value }, + assertPersistentScopedProfileAuthority: async () => { profileChecks++; afterProfile?.(); afterProfile = undefined; if (revoked) throw Error("persistent authority revoked") } }, + "./routing-community-scoped-checkpoint.js": { loadScopedConfigurationPacket: async (_input: unknown, seal: string, source: { operationId: string; stepIndex: number }) => { + configChecks++; assert.equal(seal, "request-seal"); assert.equal(source.operationId, row.id); assert.equal(source.stepIndex, 0) + afterCheckpoint?.(); afterCheckpoint = undefined + return checkpointMissing ? null : { ...checkpoint, checkpointProof: checkpointChanged ? "changed" : checkpoint.checkpointProof } + } }, + "./routing-community-context.js": { readRouteCommunityContextWithSharedNativeSession: async (token: unknown) => { + if (token !== acquisition || sharedClosed) throw Error("Unknown acquisition") + nativeReads++; sharedReads++; afterNative?.(); afterNative = undefined + return { scopedTopologyAvailable: topologyAvailable, before: { scopedExecutionAvailable: !guard, scopedExecutionReason: null, + dependencyProof: nativeDependency, targetRuleProof: nativeTarget, currentCandidateId: nativeCandidate, sourceProofs: input.contextSources }, + ownedTarget: ownedTarget(), fence: async () => { if (revoked || sharedClosed) throw Error("revoked") } } + }, readRouteCommunityContext: async (_profile: unknown, options: { authorize(): Promise; signal: AbortSignal; expectedSources: unknown }) => { + assert.equal(options.signal.aborted, false); assert.deepEqual(options.expectedSources, input.contextSources) + await options.authorize(); nativeReads++; afterNative?.(); afterNative = undefined + return { before: { scopedExecutionAvailable: !guard, scopedExecutionReason: guard ? "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE" : null, + dependencyProof: nativeDependency, targetRuleProof: nativeTarget, currentCandidateId: nativeCandidate, sourceProofs: input.contextSources }, + ownedTarget: ownedTarget(), fence: options.authorize } + } }, + } + const source = await readFile(new URL("./routing-network-configuration-bridge.ts", import.meta.url), "utf8") + const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + const exports: Record = {} + runInNewContext(compiled, { exports, require(name: string) { assert(name in providers, "Unexpected production dependency: " + name); return providers[name] }, + Date: ClockDate, AbortSignal, AbortController, structuredClone, Buffer }) + type Token = Record + type Observation = { operationId: string; phase: string; configuration: { bodyIdentity: string }; securityProof: boolean; forwardingVerified: boolean; sharedNetworkBudgetVerified: boolean } + const resolve = exports.resolveRoutingNetworkConfigurationContext as (id: string, action: unknown) => Promise + const consume = exports.consumeRoutingNetworkConfigurationContext as (token: Token) => Promise + const close = exports.closeRoutingNetworkConfigurationContext as (token: Token) => void + const resolveShared = exports.resolveRoutingNetworkConfigurationContextFromSharedContext as (token: object, action: unknown) => Promise + const resolveRound = exports.resolveRoutingNetworkForwardRoundConfigurationContext as (token: object, action: unknown) => Promise + const readRound = exports.readRoutingNetworkForwardRoundConfigurationContext as (token: object, acquisition: object) => Promise + const closeRound = exports.closeRoutingNetworkForwardRoundConfigurationContext as (token: object) => void + const action = { principal, connection } + const reseal = () => { row.evidence.sealed = JSON.stringify(evidence) } + return { resolve: () => resolve(row.id, action), resolveShared: (token = sharedToken) => resolveShared(token, action), sharedToken, sharedBinding, + consume, close, resolveRound: () => resolveRound(sharedToken, action), readRound: (token: object, read = acquisition) => readRound(token, read), closeRound, acquisition, + row, input, profile, checkpoint, packet, action, authority, reseal, brandManual: () => { manualOwner = true }, + reserve: (branded: boolean) => { + reservedOwner = branded; profile.autoRecovery = true; sharedBinding.profile.autoRecovery = true + row.state = "running"; row.step_state = "intent"; guard = true; reseal() + }, + revokeReservation: () => { reservedOwner = false }, breakTopology: () => { topologyAvailable = false }, + stats: () => ({ nativeReads, profileChecks, sourceReads, configChecks, sharedReads, sharedClosed }), advance: (ms: number) => { now += ms }, + revoke: () => { revoked = true }, sourceChanged: () => { sourceChanged = true }, checkpointMissing: () => { checkpointMissing = true }, + checkpointChanged: () => { checkpointChanged = true }, guard: () => { guard = true }, + bareNativeReadback: (proof: string) => { + packet.desiredRule = 'if (bgp-communities includes 65000:10) {\n set gw 192.0.2.9;\n set gw-interface "marker-gre-b";\n accept;\n}' + nativeBody = packet.desiredRule.replace('"marker-gre-b"', "marker-gre-b") + nativeTarget = "bare-target" + substitutedTarget = proof + }, + drift: (field: string) => { if (field === "body") nativeBody = "other"; if (field === "dependency") nativeDependency = "other"; if (field === "target") nativeTarget = "other"; if (field === "candidate") nativeCandidate = "a"; if (field === "nativeId") nativeId = "*2"; if (field === "generation") generation = "changed" }, + afterNative: (hook: () => void) => { afterNative = hook }, afterCheckpoint: (hook: () => void) => { afterCheckpoint = hook }, afterProfile: (hook: () => void) => { afterProfile = hook } } +} + +test("reserved B readback requires a live original spend, a round owner and valid topology", async () => { + const unbranded = await fixture() + unbranded.reserve(false) + await assert.rejects(unbranded.resolveRound(), /Reserved attempt unavailable/) + assert.equal(unbranded.stats().nativeReads, 0) + const oneShot = await fixture() + oneShot.reserve(true) + await assert.rejects(oneShot.resolveShared(), /Recovery guard remains closed/) + assert.equal(oneShot.stats().sharedClosed, true) + for (const mode of ["valid", "topology", "revoked"] as const) { + const f = await fixture() + f.reserve(true) + await assert.rejects(f.resolve(), /Recovery guard remains closed/) + const token = await f.resolveRound() + if (mode === "topology") f.breakTopology() + if (mode === "revoked") f.afterNative(f.revokeReservation) + if (mode === "valid") { + const observed = await f.readRound(token) + assert.equal(observed.phase, "forward") + assert.equal(observed.sharedNetworkBudgetVerified, true) + assert.equal(observed.securityProof, false) + } else { + await assert.rejects(f.readRound(token)) + assert.equal(f.stats().sharedClosed, true) + } + f.closeRound(token) + } +}) + +test("fixed resolver captures opaque context and consume rechecks configuration once without network proof", async () => { + const f = await fixture(), token = await f.resolve() + assert.equal(Object.keys(token).length, 0); assert(Object.isFrozen(token)); assert.equal(f.stats().nativeReads, 1) + const observed = await f.consume(token) + assert.equal(observed.operationId, f.row.id); assert.equal(observed.phase, "forward"); assert.equal(observed.configuration.bodyIdentity, JSON.stringify("desired")) + assert.equal(observed.securityProof, false); assert.equal(observed.forwardingVerified, false); assert.equal(observed.sharedNetworkBudgetVerified, false) + assert.equal(f.stats().nativeReads, 2); assert(f.stats().profileChecks > 5) + await assert.rejects(f.consume(token), /Unknown or consumed/); await assert.rejects(f.resolve(), /already issued/) +}) +test("optional RouterOS quotes do not change the sealed checkpoint identity", async () => { + const f = await fixture() + f.bareNativeReadback("desired-target") + const observed = await f.consume(await f.resolve()) + assert.equal(observed.configuration.bodyIdentity, JSON.stringify(f.packet.desiredRule)) + const drift = await fixture() + drift.bareNativeReadback("different-native-metadata") + await assert.rejects(drift.resolve(), /drifted/) +}) + +for (const mode of ["positive", "unbranded", "wrong-checkpoint", "terminal-source", "standalone"] as const) { + test(`manual revision-2 round ${mode}`, async () => { + const f = await fixture() + f.input.proofRevision = 2; f.input.manualEffectPolicy = "observational-v1"; delete f.input.persistentProfileAuthority + f.row.state = mode === "terminal-source" ? "applied" : "running"; f.row.step_state = "intent" + f.packet.revision = mode === "wrong-checkpoint" ? 2 : 1 + if (mode !== "unbranded") f.brandManual() + f.reseal() + if (mode !== "positive") { + await assert.rejects(mode === "standalone" ? f.resolve() : f.resolveRound()) + assert.equal(f.stats().nativeReads, 0) + return + } + const round = await f.resolveRound(), observation = await f.readRound(round) + assert.equal(observation.sharedNetworkBudgetVerified, true) + assert.equal(observation.forwardingVerified, false) + }) +} +test("caller objects, JSON, another resolver and closed contexts cannot be consumed", async () => { + const f = await fixture(), other = await fixture(), token = await f.resolve() + await assert.rejects(f.consume({}), /Unknown or consumed/); await assert.rejects(f.consume(JSON.parse(JSON.stringify(token))), /Unknown or consumed/) + await assert.rejects(other.consume(token), /Unknown or consumed/) + f.close(token); await assert.rejects(f.consume(token), /Unknown or consumed/) +}) +for (const missing of ["checkpoint", "persistent", "revision", "intent", "requestHMAC", "operationVersion", "unapproved", "futureCheckpoint", "expired", "autoRecovery"]) test(missing + " refuses before native read", async () => { + const f = await fixture() + if (missing === "checkpoint") f.checkpointMissing() + if (missing === "persistent") delete f.input.persistentProfileAuthority + if (missing === "revision") f.input.proofRevision = 2 + if (missing === "intent") f.row.intended_state = { ...f.row.intended_state, service: "unrelated" } + if (missing === "requestHMAC") f.input.requestProof = "forged" + if (missing === "operationVersion") f.row.operation_version = 6 + if (missing === "unapproved") f.row.approved_by = "" + if (missing === "futureCheckpoint") f.checkpoint.observedAt = new Date(Date.now() + 600000).toISOString() + if (missing === "expired") f.advance(120000) + if (missing === "autoRecovery") f.profile.autoRecovery = true + f.reseal(); await assert.rejects(f.resolve()); assert.equal(f.stats().nativeReads, 0) +}) +for (const field of ["body", "dependency", "target", "candidate", "nativeId"]) test("exact current " + field + " drift prevents capability mint", async () => { + const f = await fixture(); f.drift(field) + await assert.rejects(f.resolve(), /drifted/); assert.equal(f.stats().nativeReads, 1) + await assert.rejects(f.resolve(), /already issued/) +}) +test("existing scoped execution guard is retained", async () => { + const f = await fixture(); f.guard() + await assert.rejects(f.resolve(), /ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE/) +}) +for (const stage of ["native", "checkpoint", "profile"]) test("revocation after awaited " + stage + " refuses capture", async () => { + const f = await fixture() + if (stage === "native") f.afterNative(f.revoke) + if (stage === "checkpoint") f.afterCheckpoint(f.revoke) + if (stage === "profile") f.afterProfile(f.revoke) + await assert.rejects(f.resolve(), /revoked/) +}) +for (const drift of ["source", "checkpoint", "native", "authority", "deadline", "generation"]) test("consume " + drift + " drift revokes token permanently", async () => { + const f = await fixture(), token = await f.resolve() + if (drift === "source") f.sourceChanged() + if (drift === "checkpoint") f.checkpointChanged() + if (drift === "native") f.drift("body") + if (drift === "authority") f.revoke() + if (drift === "deadline") f.advance(120000) + if (drift === "generation") f.drift("generation") + await assert.rejects(f.consume(token)); await assert.rejects(f.consume(token), /Unknown or consumed/) +}) +test("caller cancellation between capture and consume prevents read-back", async () => { + const f = await fixture(), abort = new AbortController() + Object.assign(f.action, { signal: abort.signal }) + const token = await f.resolve(); abort.abort() + await assert.rejects(f.consume(token), /closed|expired/); assert.equal(f.stats().nativeReads, 1) +}) + +test("explicit close during an awaited read revokes in-flight consume", async () => { + const f = await fixture(), token = await f.resolve() + f.afterNative(() => f.close(token)) + await assert.rejects(f.consume(token), /closed|expired/) + await assert.rejects(f.consume(token), /Unknown or consumed/) +}) + +test("concurrent consume admits a single fixed read-back", async () => { + const f = await fixture(), token = await f.resolve() + const first = f.consume(token) + await assert.rejects(f.consume(token), /Unknown or consumed/) + await first; assert.equal(f.stats().nativeReads, 2) +}) + +test("shared bridge uses only the genuine owner reader and preserves its shorter original deadline", async () => { + const f = await fixture(), token = await f.resolveShared(), observed = await f.consume(token) + assert.equal(observed.sharedNetworkBudgetVerified, true); assert.equal(observed.securityProof, false) + assert.equal(f.stats().sharedReads, 2); assert.equal(f.stats().sharedClosed, false) + assert.equal((observed as unknown as { deadline: number }).deadline, f.sharedBinding.deadline) +}) +test("serialized shared metadata cannot select the shared bridge path", async () => { + const f = await fixture() + await assert.rejects(f.resolveShared(JSON.parse(JSON.stringify(f.sharedToken))), /Unknown shared context/) + assert.equal(f.stats().nativeReads, 0) +}) +test("shared source profile mismatch refuses before native I/O and closes its owner", async () => { + const f = await fixture(); f.profile.table = "changed"; f.reseal() + await assert.rejects(f.resolveShared(), /binding changed/) + assert.equal(f.stats().nativeReads, 0); assert.equal(f.stats().sharedClosed, true) +}) +test("missing genuine checkpoint closes shared owner without accepting equality", async () => { + const f = await fixture(); f.checkpointMissing() + await assert.rejects(f.resolveShared(), /CONFIG_APPLIED/) + assert.equal(f.stats().sharedReads, 0); assert.equal(f.stats().sharedClosed, true) +}) + +test("forward round context reuses one admitted source only through its exact ephemeral acquisition", async () => { + const f = await fixture(), token = await f.resolveRound() + assert.equal(f.stats().sharedReads, 0, "Round admission grants no native-success observation") + for (let round = 0; round < 2; round++) { + const before = await f.readRound(token), after = await f.readRound(token) + assert.equal(before.operationId, f.row.id); assert.equal(after.securityProof, false) + assert.equal(after.forwardingVerified, false); assert.equal(after.sharedNetworkBudgetVerified, true) + } + assert.equal(f.stats().sharedReads, 4) + await assert.rejects(f.resolveShared(), /already issued/) +}) + +for (const fault of ["clone", "owner", "body", "dependency", "checkpoint", "revoke", "close"] as const) + test("forward round capability refuses " + fault + " without reopening source admission", async () => { + const f = await fixture(), token = await f.resolveRound() + await f.readRound(token) + if (fault === "body" || fault === "dependency") f.drift(fault) + if (fault === "checkpoint") f.checkpointChanged() + if (fault === "revoke") f.revoke() + if (fault === "close") f.closeRound(token) + await assert.rejects(f.readRound(fault === "clone" ? JSON.parse(JSON.stringify(token)) : token, fault === "owner" ? {} : f.acquisition)) + assert.equal(f.stats().sharedClosed, !["clone", "close"].includes(fault)) + }) + + +for (const drift of ["body", "dependency", "checkpoint", "revoke"] as const) + test("first forward-round read detects " + drift + " after source-only admission", async () => { + const f = await fixture(), token = await f.resolveRound() + assert.equal(f.stats().nativeReads, 0) + if (drift === "body" || drift === "dependency") f.drift(drift) + if (drift === "checkpoint") f.checkpointChanged() + if (drift === "revoke") f.revoke() + await assert.rejects(f.readRound(token)) + assert.equal(f.stats().sharedClosed, true) + await assert.rejects(f.readRound(token)) + }) diff --git a/backend/src/modules/mcp/routing-network-configuration-bridge.ts b/backend/src/modules/mcp/routing-network-configuration-bridge.ts new file mode 100644 index 00000000..fd06af4b --- /dev/null +++ b/backend/src/modules/mcp/routing-network-configuration-bridge.ts @@ -0,0 +1,238 @@ +import { z } from "zod" +import type { QueryConfig } from "pg" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { effectiveMcpConnection } from "./store.js" +import { findMcpOperation } from "./registry.js" +import { accessGeneration, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import { assertScopedCommunityRequest } from "./routing-community-scoped-request.js" +import { loadScopedConfigurationPacket } from "./routing-community-scoped-checkpoint.js" +import { assertVerifiedReservedEffectOwner, isVerifiedManualEffectOwner } from "./routing-network-execution-session.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { readRouteCommunityContext, readRouteCommunityContextWithSharedNativeSession } from "./routing-community-context.js" +import { readRoutingNetworkScopedCommunityContext } from "./routing-network-scoped-context.js" +import { assertRoutingNetworkSharedContextReadOwner, closeRoutingNetworkSharedContext, inspectRoutingNetworkSharedContext, type RoutingNetworkSharedContext, type RoutingNetworkSharedContextRead } from "./routing-network-native-session.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { ConnectionRow } from "./store.js" + +declare const contextBrand: unique symbol +/** A process-local read capability, never a mutation or full network proof. */ +export type RoutingNetworkConfigurationContext = Readonly<{ [contextBrand]: true }> +const fail = (reason: string) => new McpError("STATE_CONFLICT", reason, 409) +const connectionAuthority = (value: ConnectionRow) => fingerprint({ id: value.id, ownerId: value.owner_id, permissions: value.owner_permissions, + policy: value.policy, generation: value.generation, ownerValidUntil: value.owner_valid_until, ownerClaimsIssuedAt: value.owner_claims_issued_at }) +const issued = new Set() +declare const roundContextBrand: unique symbol +/** Reusable read-only checkpoint binding on one already-admitted native owner. + * It grants no native write, inverse, READY or signed network authority. */ +export type RoutingNetworkForwardRoundConfigurationContext = Readonly<{ [roundContextBrand]: true }> +type ConfigurationObservation = Readonly<{ + operationId: string; phase: "forward"; checkpointProof: string; checkpointObservedAt: string; observedAt: string; deadline: number; + configuration: { serverId: number; nativeId: string; table: string; bodyIdentity: string; dependencyIdentity: string }; + securityProof: false; forwardingVerified: false; sharedNetworkBudgetVerified: boolean; +}> +const roundContexts = new WeakMap void; + read: (acquisition?: RoutingNetworkSharedContextRead) => Promise; +}>() +const contexts = new WeakMap void; consume: () => Promise>; read: (acquisition?: RoutingNetworkSharedContextRead) => Promise }>() +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} + +/** Fixed production resolver. Only an actual revision-3 approved source plan + * and its authenticated persisted CONFIG_APPLIED can reach native read-back. + * Existing context guards (including autoRecovery) remain mandatory. + * This bridge uses the existing bounded configuration reader; its acquisition + * counters are NOT the native observation session's shared 256-call budget. + * Do not use this independent bridge as per-round full verifier acceptance. */ +export async function resolveRoutingNetworkConfigurationContext(sourceOperationId: string, action: ActionContext): Promise { + return resolveConfigurationContext(sourceOperationId, action) +} + +/** Same genuine checkpoint bridge on an existing running native session. + * The opaque owner fixes source, principal and shared transport/budget; caller + * JSON, captured metadata and supplied clients cannot select this path. */ +export async function resolveRoutingNetworkConfigurationContextFromSharedContext(sharedContext: RoutingNetworkSharedContext, action: ActionContext): Promise { + const binding = inspectRoutingNetworkSharedContext(sharedContext) + if (fingerprint(binding.principal) !== fingerprint(action.principal)) throw fail("Shared configuration principal changed") + return resolveConfigurationContext(binding.sourceOperationId, action, sharedContext) +} + +/** Uses the SAME one-time source admission and genuine CONFIG_APPLIED checks + * as the existing bridge. Only its owning session can use it inside a queued + * round acquisition; JSON copies and caller-supplied observations cannot. */ +export async function resolveRoutingNetworkForwardRoundConfigurationContext(sharedContext: RoutingNetworkSharedContext, action: ActionContext): Promise { + const binding = inspectRoutingNetworkSharedContext(sharedContext) + if (fingerprint(binding.principal) !== fingerprint(action.principal)) throw fail("Shared configuration principal changed") + const initial = await resolveConfigurationContext(binding.sourceOperationId, action, sharedContext, true) + const entry = contexts.get(initial) + if (!entry) throw fail("Configuration bridge admission disappeared") + contexts.delete(initial) + const token = Object.freeze({}) as RoutingNetworkForwardRoundConfigurationContext + roundContexts.set(token, { owner: sharedContext, close: entry.close, read: entry.read }) + return token +} + +/** Fixed internal reader, called only with the session's ephemeral acquisition. + * This does not enqueue another shared-context task while the queue is held. */ +export async function readRoutingNetworkForwardRoundConfigurationContext(context: RoutingNetworkForwardRoundConfigurationContext, acquisition: RoutingNetworkSharedContextRead) { + const entry = roundContexts.get(context) + if (!entry) throw fail("Unknown or closed forward-round configuration context") + try { + assertRoutingNetworkSharedContextReadOwner(entry.owner, acquisition) + return await entry.read(acquisition) + } catch (error) { + roundContexts.delete(context); entry.close(); closeRoutingNetworkSharedContext(entry.owner) + throw error + } +} + +export function closeRoutingNetworkForwardRoundConfigurationContext(context: RoutingNetworkForwardRoundConfigurationContext) { + const entry = roundContexts.get(context) + roundContexts.delete(context); entry?.close() +} + +async function resolveConfigurationContext(sourceOperationId: string, action: ActionContext, sharedContext?: RoutingNetworkSharedContext, deferredRoundReadback = false): Promise { + if (deferredRoundReadback && !sharedContext) throw fail("Forward round requires its original shared owner") + z.string().uuid().parse(sourceOperationId) + const principal = freeze(structuredClone(action.principal)), connection = freeze(structuredClone(action.connection)) + const abort = new AbortController() + let closed = false, deadline = Infinity + let signal = AbortSignal.any([abort.signal, ...(action.signal ? [action.signal] : [])]) + const active = () => { if (closed || signal.aborted || Date.now() >= deadline) throw fail("Configuration bridge closed or original deadline expired") } + const close = () => { closed = true; abort.abort() } + const guarded = async (work: () => Promise) => { active(); const value = await work(); active(); return value } + const source = () => { + const query: QueryConfig & { query_timeout: number } = { text: `SELECT o.id,o.state,o.created_at,p.id AS plan_id,p.operation_id,p.operation_version,p.approved_by,p.expires_at, + CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence END AS evidence,s.state AS step_state, + CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END AS intended_state + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE o.id=$1 AND o.connection_id=$2 AND s.step_index=0 LIMIT 2`, values: [sourceOperationId, principal.connectionId], + query_timeout: Math.max(1, Math.min(10000, Number.isFinite(deadline) ? deadline - Date.now() : 10000)) } + return guarded(() => pool.query(query)) + } + try { + const initial = await guarded(() => effectiveMcpConnection(principal)) + if (connectionAuthority(initial) !== connectionAuthority(connection)) throw fail("Original connection changed") + const found = await source() + if (found.rows.length !== 1) throw fail("Exact configuration source unavailable") + const row = found.rows[0] + if (row.id !== sourceOperationId || !["running", "applied", "partial"].includes(row.state) || !["intent", "applied", "verified"].includes(row.step_state) || + row.operation_id !== "routing.community.switch" || row.operation_version !== findMcpOperation(row.operation_id)?.version || !row.approved_by || + !(row.created_at instanceof Date) || !(row.expires_at instanceof Date) || typeof row.evidence?.sealed !== "string") throw fail("No compatible approved source intent") + deadline = Math.min(row.created_at.getTime() + 120000, row.expires_at.getTime(), Date.parse(principal.credentialExpiresAt), connection.owner_valid_until.getTime(), Date.parse(connection.policy.expiresAt), + sharedContext ? inspectRoutingNetworkSharedContext(sharedContext).deadline : Infinity) + active() + const evidence = JSON.parse(await guarded(() => decryptSecret(row.evidence.sealed))) as ChangeEvidence + if (evidence.steps?.length !== 1 || evidence.steps[0].kind !== "service" || evidence.steps[0].service !== "routing.community.scoped" || evidence.target?.scope !== "servers") throw fail("Exact scoped service source required") + const step = evidence.steps[0], input = freeze(structuredClone(step.input)), profile = routeAiProfileSchema.parse(input.profile) + if (sharedContext) { + const binding = inspectRoutingNetworkSharedContext(sharedContext) + if (binding.sourceOperationId !== sourceOperationId || fingerprint(binding.profile) !== fingerprint(profile) || + fingerprint(binding.contextSources) !== fingerprint(input.contextSources) || binding.candidateId !== input.candidateId) throw fail("Shared configuration source binding changed") + } + const manual = !!sharedContext && deferredRoundReadback && isVerifiedManualEffectOwner(sharedContext) + const reservedOwner = profile.autoRecovery ? sharedContext : undefined + if (profile.autoRecovery && (!reservedOwner || !deferredRoundReadback || row.state !== "running" || + row.step_state !== "intent" || row.approved_by !== connection.owner_id)) throw fail("Recovery guard remains closed") + if (manual) { + if (row.state !== "running" || row.step_state !== "intent" || input.proofRevision !== 2 || + input.persistentProfileAuthority !== undefined || input.manualEffectPolicy !== "observational-v1") throw fail("Exact running manual effect source required") + } else { + if (input.proofRevision !== 3 || input.persistentProfileAuthority === undefined) throw fail("Persistent revision-3 source required") + const authority = routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority) + deadline = Math.min(deadline, Date.parse(authority.packet.validUntil)) + } + signal = AbortSignal.any([signal, AbortSignal.timeout(Math.max(1, deadline - Date.now()))]) + active() + const ids = [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])].sort((a,b) => a-b) + if (input.serverId !== profile.serverId || input.ownerId !== connection.owner_id || fingerprint(input.principal) !== fingerprint(principal) || + fingerprint(redact(step)) !== fingerprint(row.intended_state) || fingerprint([...evidence.target.serverIds].sort((a,b) => a-b)) !== fingerprint(ids)) throw fail("Source input or exact scope changed") + const requestSeal = await guarded(() => assertScopedCommunityRequest(input)), sourceProof = fingerprint(row) + const authorize = async () => { + const current = await guarded(() => effectiveMcpConnection(principal)) + if (connectionAuthority(current) !== connectionAuthority(connection) || current.id !== principal.connectionId || current.owner_id !== input.ownerId || current.policy.mode === "observer" || + current.generation !== input.connectionGeneration || fingerprint(current.policy) !== input.policyProof || + ids.some(id => !current.policy.scope.serverIds.includes(id)) || + ["mm:network:read", "mm:filters:write"].some(permission => !hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission)) || + !current.policy.scope.operationIds.includes("routing.community.switch") || await guarded(accessGeneration) !== input.generation) throw fail("Original authority revoked or changed") + await guarded(() => assertPersistentScopedProfileAuthority(input, signal)) + const currentSource = await source() + if (currentSource.rows.length !== 1 || fingerprint(currentSource.rows[0]) !== sourceProof || await guarded(() => assertScopedCommunityRequest(input)) !== requestSeal) throw fail("Configuration source changed") + } + const checkpoint = async () => { + await authorize() + if (reservedOwner && await guarded(() => assertVerifiedReservedEffectOwner(reservedOwner, sourceOperationId, principal)) < deadline) { + throw fail("Reserved forward deadline changed") + } + const result = await guarded(() => loadScopedConfigurationPacket(input, requestSeal, { operationId: sourceOperationId, stepIndex: 0 })) + if (!result || result.packet.revision !== (manual ? 1 : 2) || result.packet.operationId !== sourceOperationId || result.packet.stepIndex !== 0 || + result.packet.phase !== "CONFIG_APPLIED" || Date.parse(result.observedAt) < row.created_at.getTime() || Date.parse(result.observedAt) > Date.now() || + Date.parse(result.observedAt) >= deadline) throw fail("Genuine persistent CONFIG_APPLIED unavailable") + await authorize() + return result + } + const original = await checkpoint() + if (issued.has(sourceOperationId) || issued.size >= 1024) throw fail("Configuration bridge source already issued or process capacity exhausted") + issued.add(sourceOperationId) + const readback = async (acquisition?: RoutingNetworkSharedContextRead) => { + const before = await checkpoint() + if (before.checkpointProof !== original.checkpointProof || fingerprint(before.packet) !== fingerprint(original.packet)) throw fail("Configuration checkpoint changed") + if (acquisition) { + if (!sharedContext) throw fail("Round acquisition requires its original shared owner") + assertRoutingNetworkSharedContextReadOwner(sharedContext, acquisition) + } + const context = await guarded(() => acquisition ? readRouteCommunityContextWithSharedNativeSession(acquisition) : sharedContext ? readRoutingNetworkScopedCommunityContext(sharedContext) : readRouteCommunityContext(profile, { + selectedCandidateId: String(input.candidateId), authorize, signal, expectedSources: input.contextSources as Array<{ serverId: number; proof: string }> })) + if (!(reservedOwner ? context.scopedTopologyAvailable : context.before.scopedExecutionAvailable) || !context.ownedTarget) { + throw fail(String(context.before.scopedExecutionReason ?? "Owned context unavailable")) + } + const owned = context.ownedTarget + if (owned.nativeId !== original.packet.nativeId || owned.nativeId !== input.nativeId || + context.before.dependencyProof !== original.packet.dependencyProof || + !owned.matchesRuleProof(original.packet.desiredRule, original.packet.desiredTargetProof) || + context.before.currentCandidateId !== original.packet.desiredCandidateId || fingerprint(context.before.sourceProofs) !== fingerprint(original.packet.contextSources)) throw fail("Current owned rule or dependencies drifted from CONFIG_APPLIED") + await guarded(() => context.fence()) + const after = await checkpoint() + if (after.checkpointProof !== original.checkpointProof || fingerprint(after.packet) !== fingerprint(original.packet)) throw fail("Configuration checkpoint changed after read-back") + await guarded(() => context.fence()) + return freeze({ operationId: sourceOperationId, phase: "forward" as const, checkpointProof: original.checkpointProof, + checkpointObservedAt: original.observedAt, observedAt: new Date().toISOString(), deadline, + configuration: { serverId: original.packet.serverId, nativeId: owned.nativeId, table: profile.table, + bodyIdentity: fingerprint(original.packet.desiredRule), dependencyIdentity: original.packet.dependencyProof }, + securityProof: false as const, forwardingVerified: false as const, sharedNetworkBudgetVerified: !!sharedContext }) + } + // A forward-round token carries no native success observation. Its fixed + // owner must perform a fresh read immediately BEFORE every snapshot and + // another immediately AFTER. Avoid an unused eager native read here; + // independent/shared one-shot bridge semantics remain unchanged. + if (!deferredRoundReadback) await readback() + const token = Object.freeze({}) as RoutingNetworkConfigurationContext + contexts.set(token, { consumed: false, close, read: readback, consume: async () => { + try { return await readback() } + catch (error) { if (sharedContext) closeRoutingNetworkSharedContext(sharedContext); throw error } + finally { close() } + } }) + return token + } catch (error) { close(); if (sharedContext) closeRoutingNetworkSharedContext(sharedContext); throw error } +} + +/** Consume once, re-reading current rule/dependencies through the fixed reader. + * Serialized tokens and caller-created objects have no WeakMap membership. */ +export async function consumeRoutingNetworkConfigurationContext(context: RoutingNetworkConfigurationContext) { + const entry = contexts.get(context) + if (!entry || entry.consumed) throw fail("Unknown or consumed configuration context") + entry.consumed = true + try { return await entry.consume() } finally { contexts.delete(context) } +} +export function closeRoutingNetworkConfigurationContext(context: RoutingNetworkConfigurationContext) { + const entry = contexts.get(context) + contexts.delete(context); entry?.close() +} diff --git a/backend/src/modules/mcp/routing-network-execution-origin.test.mjs b/backend/src/modules/mcp/routing-network-execution-origin.test.mjs new file mode 100644 index 00000000..0fb1002d --- /dev/null +++ b/backend/src/modules/mcp/routing-network-execution-origin.test.mjs @@ -0,0 +1,115 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import test from 'node:test'; +import { createHash } from 'node:crypto'; +const require = createRequire(new URL('../../../package.json', import.meta.url)); +const ts = require('typescript'); +const code = ts.transpileModule(await readFile(new URL('./routing-network-execution-session.ts', import.meta.url), 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } +}).outputText; +function fixture() { + const now = Date.now(); + const metadata = { approved: true, phase: 'forward', stepIndex: 0, operationName: 'routing.community.switch', operationVersion: 7, + serviceName: 'routing.community.scoped', operationId: 'real-operation', planId: 'real-plan', principal: { connectionId: 'real-connection', credentialExpiresAt: new Date(now + 90000).toISOString() }, + serviceInput: { persistentProfileAuthority: { packet: { validUntil: new Date(now + 70000).toISOString() } } } }; + metadata.sealedEvidenceProof = createHash('sha256').update('evidence-ciphertext').digest('hex'); + metadata.sealedPayloadProof = createHash('sha256').update('payload-ciphertext').digest('hex'); + const original = structuredClone(metadata); + const row = { created_at: new Date(now - 40000), expires_at: new Date(now + 100000), evidence: { sealed: 'evidence-ciphertext' }, payload: { sealed: 'payload-ciphertext' }, + intended_state: { kind: 'service', service: metadata.serviceName, input: structuredClone(metadata.serviceInput) } }; + const lifetime = { expiresAt: now + 60000, monotonicDeadline: performance.now() + 60000, signal: new AbortController().signal }; + let calls = 0, inspections = 0, closed = 0, options, query, originAvailable = true, afterCreate = () => {}; + const connection = { owner_valid_until: new Date(now + 110000), policy: { expiresAt: new Date(now + 120000).toISOString() } }; + const token = Object.freeze({}), sharedContext = Object.freeze({}), allocation = Object.freeze({}); + let spendDeadline = now + 45_000; + const providers = { + 'node:crypto': { createHash }, + '../../db/index.js': { pool: { query: async value => { query = value; return { rows: [row] }; } } }, + '../../services/config-mutation-policy.js': { + captureVerifiedMcpServiceExecution: async () => { if (!originAvailable) throw Error('no-origin'); return token; }, + inspectVerifiedMcpServiceExecution: async value => { assert.equal(value, token); inspections++; return structuredClone(metadata); }, + inspectVerifiedMcpServiceExecutionLifetime: async value => { assert.equal(value, token); return lifetime; } + }, + './store.js': { effectiveMcpConnection: async principal => { assert.deepEqual(principal, original.principal); return connection; } }, + './registry.js': { findMcpOperation: () => ({ version: 7 }) }, + './security.js': { fingerprint: JSON.stringify, redact: value => value, McpError: Error }, + './routing-profile-authority.js': { routingProfileAuthorityBindingSchema: { parse: value => value } }, + './routing-reservation-store.js': { assertAcknowledgedRoutingForwardAttempt: async (value, operationId, principal) => { + assert.equal(value, allocation); assert.equal(operationId, metadata.operationId); assert.deepEqual(principal, metadata.principal); + if (!spendDeadline) throw Error('acknowledged spend no longer live'); + return spendDeadline; + } }, + './routing-network-native-session.js': { createRoutingNetworkNativeSession: async value => { calls++; options = value; afterCreate(); return { sharedContext, close() { closed++; } }; } } + }; + const exports = {}; + new Function('exports', 'require', code)(exports, name => { assert.ok(providers[name], name); return providers[name]; }); + const manifest = { servers: [1, 2], probes: [] }; + return { metadata, row, lifetime, connection, manifest, now, sharedContext, allocation, isManual: exports.isVerifiedManualEffectOwner, + assertReserved: owner => exports.assertVerifiedReservedEffectOwner(owner, metadata.operationId, metadata.principal), + revokeSpend: () => { spendDeadline = 0; }, + run: forwardAllocation => exports.createRoutingNetworkSessionForCurrentExecution(manifest, forwardAllocation), + revokeOrigin: () => { originAvailable = false; }, afterCreate: fn => { afterCreate = fn; }, + stats: () => ({ calls, closed, inspections, options, query }) }; +} + +test('reserved owner requires its acknowledged spend and inherits the shorter deadline', async () => { + const f = fixture(); + Object.assign(f.metadata.serviceInput, { proofRevision: 3, profile: { autoRecovery: true } }); + f.row.intended_state.input = structuredClone(f.metadata.serviceInput); + await assert.rejects(f.run()); + await assert.rejects(f.run({})); + assert.equal(f.stats().calls, 0); + await f.run(f.allocation); + assert.equal(f.stats().options.deadline, f.now + 30_000); + await f.assertReserved(f.sharedContext); + await assert.rejects(f.assertReserved({})); + f.revokeSpend(); + await assert.rejects(f.assertReserved(f.sharedContext), /no longer live/); +}); +for (const mode of ['positive', 'missing-policy', 'missing-selection', 'recovery', 'late-revocation']) { + test(`manual effect owner origin ${mode}`, async () => { + const f = fixture(); + f.metadata.serviceInput = { proofRevision: 2, manualEffectPolicy: 'observational-v1', profile: { autoRecovery: mode === 'recovery' }, originalEffectSelection: { status: 'prepared' } }; + if (mode === 'missing-policy') delete f.metadata.serviceInput.manualEffectPolicy; + if (mode === 'missing-selection') delete f.metadata.serviceInput.originalEffectSelection; + if (mode === 'late-revocation') f.afterCreate(() => { f.metadata.approved = false; }); + f.row.intended_state.input = structuredClone(f.metadata.serviceInput); + if (mode === 'positive') { + await f.run(); + assert.equal(f.isManual(f.sharedContext), true); + assert.equal(f.isManual(JSON.parse(JSON.stringify(f.sharedContext))), false); + } else { + await assert.rejects(f.run()); + assert.equal(f.isManual(f.sharedContext), false); + assert.equal(f.stats().calls, mode === 'late-revocation' ? 1 : 0); + assert.equal(f.stats().closed, mode === 'late-revocation' ? 1 : 0); + } + }); +} +test('real origin supplies operation, principal, signal and minimum immutable horizon', async () => { + const f = fixture(); await f.run(); const { options, query } = f.stats(); + assert.equal(options.sourceOperationId, f.metadata.operationId); assert.equal(options.context.signal, f.lifetime.signal); + assert.equal(options.deadline, f.lifetime.expiresAt - 15000); assert.equal(options.manifest, f.manifest); + assert.deepEqual(query.values, [f.metadata.operationId, f.metadata.principal.connectionId, f.metadata.planId]); + assert.match(query.text, /o.state='running'/); assert.match(query.text, /s.state='intent'/); +}); +test('elapsed original 120 seconds cannot be renewed from current time', async () => { + const f = fixture(); f.row.created_at = new Date(f.now - 120001); await assert.rejects(f.run); assert.equal(f.stats().calls, 0); +}); +for (const [name, change] of [ + ['plan', f => { f.row.expires_at = new Date(f.now + 30000); }], + ['owner', f => { f.connection.owner_valid_until = new Date(f.now + 30000); }], + ['policy', f => { f.connection.policy.expiresAt = new Date(f.now + 30000).toISOString(); }], + ['profile', f => { f.metadata.serviceInput.persistentProfileAuthority.packet.validUntil = new Date(f.now + 30000).toISOString(); f.row.intended_state.input = structuredClone(f.metadata.serviceInput); }], + ['operation', f => { f.row.created_at = new Date(f.now - 90000); }] +]) test(`minimum ${name} expiry is conserved with publication reserve`, async () => { const f = fixture(); change(f); await f.run(); assert.equal(f.stats().options.deadline, f.now + 15000); }); +for (const [key, value] of [['approved', false], ['phase', 'recovery'], ['stepIndex', 1], ['operationName', 'operations.recover'], ['operationVersion', 6], ['serviceName', 'routing.community']]) { + test(`wrong live origin ${key} is denied before native factory`, async () => { const f = fixture(); f.metadata[key] = value; await assert.rejects(f.run); assert.equal(f.stats().calls, 0); }); +} +test('outside verified origin cannot supply authority', async () => { const f = fixture(); f.revokeOrigin(); await assert.rejects(f.run); assert.equal(f.stats().calls, 0); }); +test('persisted intended state must match the live service input', async () => { const f = fixture(); f.row.intended_state.input = {}; await assert.rejects(f.run); assert.equal(f.stats().calls, 0); }); +for (const field of ['evidence', 'payload']) test(`changed randomized sealed ${field} is denied before native`, async () => { const f = fixture(); f.row[field].sealed = 'changed'; await assert.rejects(f.run); assert.equal(f.stats().calls, 0); }); +test('monotonic expiry denies open even with unexpired wall horizon', async () => { const f = fixture(); f.lifetime.monotonicDeadline = performance.now() - 1; await assert.rejects(f.run); assert.equal(f.stats().calls, 0); }); +test('late origin change closes the newly opened native owner', async () => { const f = fixture(); f.afterCreate(() => { f.metadata.planId = 'changed'; }); await assert.rejects(f.run); assert.equal(f.stats().closed, 1); }); +test('returned native authorize callback remains tied to original metadata', async () => { const f = fixture(); await f.run(); f.metadata.operationId = 'changed'; await assert.rejects(f.stats().options.authorize); }); diff --git a/backend/src/modules/mcp/routing-network-execution-session.ts b/backend/src/modules/mcp/routing-network-execution-session.ts new file mode 100644 index 00000000..87b0bde3 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-execution-session.ts @@ -0,0 +1,92 @@ +import { createHash } from "node:crypto" +import type { QueryConfig } from "pg" +import { pool } from "../../db/index.js" +import { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecutionLifetime } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { findMcpOperation } from "./registry.js" +import { fingerprint, McpError, redact } from "./security.js" +import { routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { assertAcknowledgedRoutingForwardAttempt, type RoutingReservedForwardAllocation } from "./routing-reservation-store.js" +import type { McpPrincipal } from "./store.js" +import { createRoutingNetworkNativeSession, type RoutingNetworkNativeManifest, type RoutingNetworkSharedContext } from "./routing-network-native-session.js" + +const manualEffectOwners = new WeakSet() +const reservedEffectOwners = new WeakMap() + +/** Only the verified execution factory can mark a genuine shared owner. */ +export function isVerifiedManualEffectOwner(owner: RoutingNetworkSharedContext): boolean { + return manualEffectOwners.has(owner) +} + +export async function assertVerifiedReservedEffectOwner(owner: RoutingNetworkSharedContext, operationId: string, principal: McpPrincipal) { + const allocation = reservedEffectOwners.get(owner) + if (!allocation) throw new McpError("STATE_CONFLICT", "Исходный зарезервированный сеанс отсутствует", 409) + return assertAcknowledgedRoutingForwardAttempt(allocation, operationId, principal) +} + +/** Private producer for the existing native/configuration-round reader. The + * manifest selects reads, never identity or authority. Its exact scope, probe + * ownership and whole-session budget are still checked by the native factory. + * Only the original live verified service step can open or use this session. + * No operation/principal/deadline/authorization callback is caller supplied. + * This grants no write, recovery or authenticated log-interval coverage. */ +export async function createRoutingNetworkSessionForCurrentExecution(manifest: RoutingNetworkNativeManifest, + forwardAllocation?: RoutingReservedForwardAllocation): Promise>> { + const fail = () => new McpError("STATE_CONFLICT", "Нет исходного running scoped execution для native acquisition", 409) + const origin = await captureVerifiedMcpServiceExecution() + const initial = await inspectVerifiedMcpServiceExecution(origin) + if (!initial.approved || initial.phase !== "forward" || initial.stepIndex !== 0 || + initial.operationName !== "routing.community.switch" || initial.operationVersion !== findMcpOperation(initial.operationName)?.version || + initial.serviceName !== "routing.community.scoped") throw fail() + const input = initial.serviceInput, manual = input.proofRevision === 2 + if (manual && (input.manualEffectPolicy !== "observational-v1" || input.persistentProfileAuthority !== undefined || + (input.profile as { autoRecovery?: unknown } | undefined)?.autoRecovery !== false || + (input.originalEffectSelection as { status?: unknown } | undefined)?.status !== "prepared")) throw fail() + const reserved = (input.profile as { autoRecovery?: unknown } | undefined)?.autoRecovery === true + if (reserved !== !!forwardAllocation || reserved && (input.proofRevision !== 3 || input.persistentProfileAuthority === undefined)) throw fail() + const lifetime = await inspectVerifiedMcpServiceExecutionLifetime(origin) + const authorize = async () => { + const current = await inspectVerifiedMcpServiceExecution(origin) + if (fingerprint(current) !== fingerprint(initial)) throw fail() + } + await authorize() + const reservationExpiry = forwardAllocation + ? await assertAcknowledgedRoutingForwardAttempt(forwardAllocation, initial.operationId, initial.principal) : Infinity + const connection = await effectiveMcpConnection(initial.principal) + await authorize() + // The service lifetime may be narrower than plan expiry, but it does not + // replace the operation-created-at +120s native horizon. Never renew at now. + const query: QueryConfig & { query_timeout: number } = { text: `SELECT o.created_at,p.expires_at, + CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence END AS evidence, + CASE WHEN octet_length(p.payload::text)<=1048576 THEN p.payload END AS payload, + CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END AS intended_state FROM mcp_operations o + JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE o.id=$1 AND o.connection_id=$2 AND o.plan_id=$3 AND o.state='running' + AND s.step_index=0 AND s.state='intent' LIMIT 2`, + values: [initial.operationId, initial.principal.connectionId, initial.planId], + query_timeout: Math.max(1, Math.min(10000, lifetime.expiresAt - Date.now())) } + const source = await pool.query(query) + await authorize() + if (source.rows.length !== 1 || !(source.rows[0].created_at instanceof Date) || !(source.rows[0].expires_at instanceof Date)) throw fail() + const sha = (value: string) => createHash("sha256").update(value).digest("hex") + if (typeof source.rows[0].evidence?.sealed !== "string" || typeof source.rows[0].payload?.sealed !== "string" || + sha(source.rows[0].evidence.sealed) !== initial.sealedEvidenceProof || sha(source.rows[0].payload.sealed) !== initial.sealedPayloadProof) throw fail() + if (fingerprint(source.rows[0].intended_state) !== fingerprint(redact({ kind: "service", service: initial.serviceName, input: initial.serviceInput }))) throw fail() + const authorityExpiry = initial.serviceInput.persistentProfileAuthority === undefined ? Infinity : + Date.parse(routingProfileAuthorityBindingSchema.parse(initial.serviceInput.persistentProfileAuthority).packet.validUntil) + const deadline = Math.min(source.rows[0].created_at.getTime() + 120000, source.rows[0].expires_at.getTime(), lifetime.expiresAt, + Date.parse(initial.principal.credentialExpiresAt), connection.owner_valid_until.getTime(), Date.parse(connection.policy.expiresAt), authorityExpiry, reservationExpiry) + // Reserve publication/current-authority time for the existing manual result. + // This only narrows the original horizon; it never creates a renewed grant. + const readDeadline = deadline - 15000 + if (!Number.isFinite(readDeadline) || Date.now() >= readDeadline || lifetime.signal.aborted || performance.now() >= lifetime.monotonicDeadline) throw fail() + const session = await createRoutingNetworkNativeSession({ sourceOperationId: initial.operationId, + context: { principal: initial.principal, connection, signal: lifetime.signal }, authorize, deadline: readDeadline, manifest }) + try { + await authorize() + if (manual) manualEffectOwners.add(session.sharedContext) + if (forwardAllocation) reservedEffectOwners.set(session.sharedContext, forwardAllocation) + return session + } + catch (error) { session.close(); throw error } +} diff --git a/backend/src/modules/mcp/routing-network-forward-rounds.owned.test.ts b/backend/src/modules/mcp/routing-network-forward-rounds.owned.test.ts new file mode 100644 index 00000000..333e3a90 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-forward-rounds.owned.test.ts @@ -0,0 +1,45 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +/** Exclusive owned SQL/localhost acceptance. Genuine scoped runtime produces + * CONFIG_APPLIED; only persistent profile authority is a synthetic fixture. */ +import assert from "node:assert/strict" +import { basename, dirname, isAbsolute, resolve } from "node:path" + +assert.equal(process.env.MMAPP_OWN_ROUTING_FORWARD_ROUNDS, "true") +assert.equal(process.env.NODE_ENV, "test") +const target = new URL(process.env.DATABASE_URL ?? "") +assert(["postgres:", "postgresql:"].includes(target.protocol)) +assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") +assert(target.password); assert.equal(target.pathname, "/postgres") +assert(Number.isInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) +assert.equal(target.search, ""); assert.equal(target.hash, "") +assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) +assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") +assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const originalEffects = process.env.MMAPP_OWN_ROUTING_ORIGINAL_EFFECTS === "true" +const controllerFailoverOnly = process.env.MMAPP_OWN_ROUTING_CONTROLLER_FAILOVER_ONLY === "true" +assert(!controllerFailoverOnly || originalEffects, "Focused controller failover requires the owned original-effects fixture") +assert(!(originalEffects && process.env.MMAPP_OWN_ROUTING_WORKER_REPRO === "true"), "Owned effect acceptance cannot mix with the worker repro profile") +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "owned-routing-forward-rounds-acceptance-secret" +process.env.AUTH_ISSUER = "https://owned-routing-forward-rounds.test" + +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const actor = { id: "owned-routing-forward-rounds-owner", email: "owned-routing@example.test", name: "Owned routing forward rounds", + apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:filters:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + await updateMcpSettings({ enabled: true }, actor) + if(process.env.MMAPP_OWN_ROUTING_WORKER_REPRO==="true") { + console.log("Worker fixture repro:",await (await import("./routing-worker-fixture-isolation.sql-helper.js")).acceptRoutingWorkerFixtureIsolation(actor)) + } + const result = await (await import("./routing-community-scoped-acceptance.js")).acceptScopedRouteCommunitySwitch(actor, + originalEffects ? { originalEffectsOnly: true, controllerFailoverOnly } : process.env.MMAPP_OWN_ROUTING_WORKER_REPRO==="true"?{}:{ forwardRoundsOnly: true }) + assert.equal(result.accepted, true) +} finally { + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/routing-network-native-destination.ts b/backend/src/modules/mcp/routing-network-native-destination.ts new file mode 100644 index 00000000..aa15214d --- /dev/null +++ b/backend/src/modules/mcp/routing-network-native-destination.ts @@ -0,0 +1,74 @@ +import { lookup } from "node:dns/promises" +import { isIP } from "node:net" +import { isAllowedServerConnectionAddress, serverConnectionHostSchema } from "./server-connection-destination-policy.js" +import { McpError } from "./security.js" +import type { RoutingEndpoint } from "./routing-observation-source.js" + +export type RoutingNetworkNativeDestinationPin = Readonly<{ serverId: number; host: string; address: string; family: 4 | 6 }> +let pendingLookups = 0 +const fail = (code: string, message: string) => new McpError(code, message, 409) + +/** Fixed production DNS provider. A timeout/cancel never releases the global + * slot of an uncancellable lookup; only settlement of that lookup releases it. + * No cache, caller resolver, fallback, native request or authority is exposed. */ +export async function resolveRoutingNetworkDestinationPins(endpoints: readonly RoutingEndpoint[], options: { + deadline: number; signal: AbortSignal; authorize: () => Promise; +}): Promise { + const deadline = options.deadline, signal = options.signal, authorize = options.authorize + if (!Number.isFinite(deadline) || deadline > Date.now() + 120000 || typeof authorize !== "function") throw fail("READ_CANCELLED", "Некорректный исходный DNS deadline") + const capturedEndpoints = endpoints.map(endpoint => Object.freeze({ id: endpoint.id, host: endpoint.host })) + const active = () => { if (signal.aborted || Date.now() >= deadline) throw fail("READ_CANCELLED", "DNS native session отменён или истёк") } + const pins: RoutingNetworkNativeDestinationPin[] = [] + for (const endpoint of capturedEndpoints) { + active(); await authorize(); active() + const host = serverConnectionHostSchema.parse(endpoint.host) + let rows: { address: string; family: number }[] + if (isIP(host)) rows = [{ address: host, family: isIP(host) }] + else { + if (pendingLookups >= 16) throw fail("LIMIT_EXCEEDED", "Исчерпана ёмкость незавершённых DNS lookup") + pendingLookups++ + const pending = Promise.resolve().then(() => lookup(host, { all: true, verbatim: true })) + // Register both settlement handlers immediately, including late rejection. + void pending.then(() => { pendingLookups-- }, () => { pendingLookups-- }) + let timer: ReturnType | undefined + let aborted: (() => void) | undefined + try { + rows = await Promise.race([pending, new Promise((_resolve, reject) => { + aborted = () => reject(fail("READ_CANCELLED", "DNS native session отменён")) + signal.addEventListener("abort", aborted, { once: true }) + if (signal.aborted) { aborted(); return } + timer = setTimeout(() => reject(fail("STATE_UNAVAILABLE", "Истёк исходный срок DNS lookup")), Math.max(1, Math.min(2000, deadline - Date.now()))) + })]) + } catch (error) { + if (error instanceof McpError) throw error + throw fail("STATE_UNAVAILABLE", "Назначение native session не разрешено") + } finally { + if (timer) clearTimeout(timer) + if (aborted) signal.removeEventListener("abort", aborted) + // Recheck real authority even when the provider rejects or times out. + active(); await authorize(); active() + } + } + active(); await authorize(); active() + if (!rows.length || rows.length > 16 || rows.some(row => ![4,6].includes(row.family) || row.family !== isIP(row.address) || !isAllowedServerConnectionAddress(row.address))) throw fail("DESTINATION_DENIED", "Не подтверждён безопасный DNS native manifest") + const selected = [...rows].sort((a,b) => a.family - b.family || a.address.localeCompare(b.address))[0] + pins.push(Object.freeze({ serverId: endpoint.id, host: endpoint.host, address: selected.address, family: selected.family as 4 | 6 })) + } + return Object.freeze(pins) +} + +/** Transport independently checks the exact captured pin set. JSON cannot add + * a URL, lookup callback, alternate host or a nonliteral connection target. */ +export function captureRoutingNetworkDestinationPins(endpoints: readonly RoutingEndpoint[], pins: readonly RoutingNetworkNativeDestinationPin[]) { + if (!Array.isArray(pins) || pins.length !== endpoints.length || new Set(endpoints.map(endpoint => endpoint.id)).size !== endpoints.length) throw fail("STATE_CONFLICT", "DNS pin set не соответствует endpoint manifest") + const captured = new Map() + for (const pin of pins) { + if (!pin || typeof pin !== "object" || Object.keys(pin).some(key => !["serverId", "host", "address", "family"].includes(key)) || captured.has(pin.serverId) || + !endpoints.some(endpoint => endpoint.id === pin.serverId && endpoint.host === pin.host) || ![4,6].includes(pin.family) || isIP(pin.address) !== pin.family || !isAllowedServerConnectionAddress(pin.address)) throw fail("DESTINATION_DENIED", "DNS pin повреждён или изменяет назначение") + const host = serverConnectionHostSchema.parse(pin.host) + const canonicalLiteral = (value: string) => isIP(value) === 6 ? new URL("http://[" + value + "]/").hostname : value + if (isIP(host) && (isIP(host) !== pin.family || canonicalLiteral(host) !== canonicalLiteral(pin.address))) throw fail("DESTINATION_DENIED", "DNS pin не может заменить исходный literal IP") + captured.set(pin.serverId, Object.freeze({ serverId: pin.serverId, host: pin.host, address: pin.address, family: pin.family })) + } + return captured +} diff --git a/backend/src/modules/mcp/routing-network-native-parser.test.ts b/backend/src/modules/mcp/routing-network-native-parser.test.ts new file mode 100644 index 00000000..fb476c52 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-native-parser.test.ts @@ -0,0 +1,335 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { captureRoutingNetworkNativeParser } from "./routing-network-native-parser.js" +import { classifyRoutingNetwork, createRoutingNetworkBudget, spendRoutingNetworkBudget, type RoutingNetworkManifest, type RoutingNetworkRound } from "./routing-network-verifier-model.js" +type RawRow = Record +type RawMutation = { routerOsVersion: string; resource: string; payload: RawRow[]; connectionsPayload: RawRow[]; sessionsPayload: RawRow[]; + request: RawRow; pingPayload: RawRow[]; tracePayload: RawRow[] } + +function fixture(ipv6 = false) { + const local = ipv6 ? "2001:db8::2" : "192.0.2.2", remote = ipv6 ? "2001:db8::1" : "192.0.2.1" + const destination = ipv6 ? "2001:db8:10::1" : "203.0.113.1", source = ipv6 ? "2001:db8:20::1" : "198.51.100.1" + const family = ipv6 ? "ipv6" : "ipv4", afi = ipv6 ? "ipv6" : "ip" + const manifest: RoutingNetworkManifest = { revision: 1, phase: "forward", identity: { operationId: "00000000-0000-4000-8000-000000000001", profileId: "00000000-0000-4000-8000-000000000002", profileVersion: 1, stepIndex: 0, sourceEpoch: "epoch1", authorityIdentity: "authority1" }, + sourceCreatedAt: 1800000000000, deadline: 1800000120000, observationStartAt: 1800000001000, maximumSampleAgeMs: 30000, serverIds: [1], + configuration: { serverId: 1, nativeId: "*F1", table: "tenant", bodyIdentity: "body1", dependencyIdentity: "deps1" }, + routes: [{ key: "route1", serverId: 1, family, prefix: ipv6 ? "2001:db8:10::/64" : "203.0.113.0/24", table: "tenant", sessionKey: "peer1", nextHop: remote, interface: "wan1", resolverTable: "main", resolutionChain: [{ address: remote, table: "main", interface: "wan1" }] }], + bgp: [{ key: "peer1", serverId: 1, family, connectionId: "*C1", instance: "default", localAddress: local, remoteAddress: remote, remoteAsn: 65001 }], + bfd: [{ key: "bfd1", serverId: 1, applicability: "required", configurationIdentity: "bfd-config", localAddress: local, remoteAddress: remote, interface: "wan1", vrf: "main" }], + probes: [{ key: "probe1", serverId: 1, family, destination, source, interface: "wan1", probeVrf: "diagnostic", expectedHops: [remote, destination] }], + logs: [{ key: "logs1", serverId: 1, generation: "boot1", sequenceMode: "contiguous-capture", afterSequence: 0, topics: ["route", "bgp", "bfd", "interface"], relatedKeys: ["route1", "peer1", "bfd1", "probe1"] }], + } + // Raw RouterOS REST field names/string values, not model observations. + const connections = [{ ".id": "*C1", name: "peer", instance: "default", "local.address": local, "remote.address": remote, "remote.as": "65001", afi, disabled: "false", templates: "", vrf: "main", "routing-table": "tenant" }] + const sessions = [{ ".id": "*B000002", name: "runtime-name-is-not-a-binding", instance: "default", "local.address": local, "remote.address": remote, + "remote.as": "65001", "local.afi": afi, "remote.afi": afi, established: "true", vrf: "main", "routing-table": "tenant", uptime: "1h" }] + const base = { serverId: 1, routerOsVersion: "7.20.4 (stable)" } + const bgp = { ...base, resource: "/routing/bgp/session", payload: sessions, connectionsPayload: connections } + const route = { ...base, resource: "/routing/route", payload: [{ ".id": "*R1", afi: ipv6 ? "ip6" : "ip4", "dst-address": manifest.routes[0].prefix, "routing-table": "tenant", bgp: "true", dynamic: "true", active: "true", contribution: "active", gateway: remote + "@main", "immediate-gw": remote + "%wan1", "bgp.peer-cache-id": "*B000002", "belongs-to": "untrusted descriptive text" }], sessionsPayload: sessions, connectionsPayload: connections } + const bfd = { ...base, resource: "/routing/bfd/session", payload: [{ ".id": "*D1", "local-address": local + "%wan1@main", "remote-address": remote + "%wan1@main", state: "Up" }] } + const probe = { ...base, request: { destination, source, interface: "wan1", probeVrf: "diagnostic", count: 3 as const, maxHops: 16 as const }, + pingPayload: [{ sent: "1", received: "1", "packet-loss": "0", "avg-rtt": "453us", host: destination }, { sent: "2", received: "2", "packet-loss": "0", "avg-rtt": "1ms", host: destination }, { sent: "3", received: "3", "packet-loss": "0", "avg-rtt": "53ms561us", host: destination }], + tracePayload: [{ address: remote, sent: "1", loss: "0" }, { address: destination, sent: "1", loss: "0" }], + } + return { manifest, parser: captureRoutingNetworkNativeParser(manifest), base, bgp, route, bfd, probe } +} + +for (const ipv6 of [false, true]) test("raw documented " + (ipv6 ? "IPv6" : "IPv4") + " BGP/BFD/probe observations do not establish route-origin namespace", () => { + const f = fixture(ipv6) + for (const result of [f.parser.parseBgp("peer1", f.bgp), f.parser.parseBfd("bfd1", f.bfd), f.parser.parseProbe("probe1", f.probe)]) { + assert.equal(result.status, "observed", JSON.stringify(result)); assert.equal(result.securityProof, false) + } + const route = f.parser.parseRoute("route1", f.route) + assert.equal(route.status, "unknown"); if (route.status === "unknown") assert.equal(route.reason, "unsupported-route-bgp-origin-namespace") + const probe = f.parser.parseProbe("probe1", f.probe) + if (probe.status === "observed") assert.equal(probe.observation.probeVrf, "diagnostic") +}) + +test("equal cache/session REST IDs collide without proving a shared namespace; aliases never repair it", () => { + const f = fixture() + // The old parser accepted this exact collision plus a genuine-looking BGP + // tuple. Equal strings from distinct native menus are not provenance. + assert.equal(f.route.payload[0]["bgp.peer-cache-id"], f.bgp.payload[0][".id"]) + for (const mode of ["cache", "session", "both-equal", "both-conflict", "absent"] as const) { + const raw = structuredClone(f.route) as unknown as RawMutation + if (mode !== "cache" && mode !== "absent") raw.payload[0]["bgp.session"] = mode === "both-conflict" ? "*OTHER" : "*B000002" + if (mode === "session" || mode === "absent") delete raw.payload[0]["bgp.peer-cache-id"] + const result = f.parser.parseRoute("route1", raw) + assert.equal(result.status, "unknown") + if (result.status === "unknown") { + assert.equal(result.reason, "unsupported-route-bgp-origin-namespace") + assert.deepEqual(result.diagnostic, { peerCacheIdPresent: mode !== "session" && mode !== "absent", sessionFieldPresent: mode !== "cache" && mode !== "absent", routeToSessionBinding: "unproven" }) + assert.equal(Object.hasOwn(result, "observation"), false) + } + } +}) + +for (const mutation of ["missing-peer-id", "conflicting-peer-id", "foreign-peer", "duplicate-route", "wrong-table", "wrong-afi", "hostbits", "missing-active", "contradictory-contribution", "multipath", "missing-resolver", "wrong-resource", "invented-success"] as const) test("route " + mutation + " remains unknown", () => { + const f = fixture(), raw = structuredClone(f.route) as unknown as RawMutation, row = raw.payload[0] + if (mutation === "missing-peer-id") delete row["bgp.peer-cache-id"] + if (mutation === "conflicting-peer-id") row["bgp.session"] = "*OTHER" + if (mutation === "foreign-peer") row["bgp.peer-cache-id"] = "*FOREIGN" + if (mutation === "duplicate-route") raw.payload.push({ ...row, ".id": "*R2" }) + if (mutation === "wrong-table") row["routing-table"] = "diagnostic" + if (mutation === "wrong-afi") row.afi = "ipv6" + if (mutation === "hostbits") row["dst-address"] = "203.0.113.1/24" + if (mutation === "missing-active") delete row.active + if (mutation === "contradictory-contribution") row.contribution = "filtered" + if (mutation === "multipath") row["immediate-gw"] = "192.0.2.1%wan1,192.0.2.2%wan2" + if (mutation === "missing-resolver") row.gateway = "192.0.2.1" + if (mutation === "wrong-resource") raw.resource = "/ip/route" + if (mutation === "invented-success") raw.payload = [{ ".id": "*R1", "dst-address": f.manifest.routes[0].prefix, "routing-table": "tenant", stagePassed: true, resolved: true, sessionKey: "peer1" }] + assert.equal(f.parser.parseRoute("route1", raw).status, "unknown") +}) + +test("recursive route does not copy manifest chain into observed evidence; raw nexthop link namespaces remain unsupported", () => { + const f = fixture(), raw = structuredClone(f.route) + raw.payload[0].gateway = "198.18.0.1@main" + const parsed = f.parser.parseRoute("route1", raw) + assert.equal(parsed.status, "unknown"); if (parsed.status === "unknown") assert.match(parsed.reason, /recursive/) +}) + +test("documented link and VPN rows do not obscure the exact recursive IPv4 or IPv6 diagnostic", () => { + for (const ipv6 of [false, true]) { + const f = fixture(ipv6), raw = structuredClone(f.route) as unknown as RawMutation + const [target] = raw.payload + target.gateway = ipv6 ? "2001:db8:ffff::1@main" : "198.18.0.1@main" + const table = target["routing-table"] + raw.payload.unshift( + { ".id": "*L1", afi: "link", "dst-address": "ether1", "routing-table": table }, + { ".id": "*V4", afi: "vpnv4", "dst-address": "203.0.113.0/24&65530:100", "routing-table": table }, + { ".id": "*V6", afi: "vpnv6", "dst-address": "2001:db8:10::/64&65530:100", "routing-table": table }, + ) + const result = f.parser.parseRoute("route1", raw) + assert.equal(result.status, "unknown") + assert.equal(result.reason, "unsupported-recursive-nexthop-link-proof") + assert.equal(result.securityProof, false) + raw.payload[0]["dst-address"] = target["dst-address"] + const collision = f.parser.parseRoute("route1", raw) + assert.equal(collision.status, "unknown") + assert.equal(collision.reason, "missing-or-ambiguous-installation-route") + } +}) + +for (const ipv6 of [false, true]) test("recursive " + (ipv6 ? "IPv6" : "IPv4") + " cannot infer numeric native joins from matching route/debug IDs or desired chain", () => { + const f = fixture(ipv6), raw = structuredClone(f.route) as unknown as RawMutation + const recursive = ipv6 ? "2001:db8:ffff::1" : "198.18.0.1" + f.manifest.routes[0].resolutionChain.unshift({ address: recursive, table: "main", interface: "wan1" }) + raw.payload[0].gateway = recursive + "@main" + raw.payload[0]["nexthop-id"] = "1" + raw.payload[0]["debug.route-ptr"] = "1" + raw.payload[0]["debug.fwp-ptr"] = "1" + // A host route with a consistent final endpoint still does not identify + // which numeric nexthop output link RouterOS actually used. + raw.payload.push({ ".id": "*1", afi: ipv6 ? "ip6" : "ip4", "dst-address": recursive + (ipv6 ? "/128" : "/32"), + "routing-table": "main", static: "true", active: "true", contribution: "active", scope: "10", "target-scope": "10", + gateway: raw.payload[0]["immediate-gw"], "immediate-gw": raw.payload[0]["immediate-gw"], "nexthop-id": "1" }) + const result = captureRoutingNetworkNativeParser(f.manifest).parseRoute("route1", raw) + assert.equal(result.status, "unknown") + if (result.status === "unknown") { + assert.equal(result.reason, "unsupported-recursive-nexthop-link-proof") + assert.deepEqual(result.diagnostic?.missingEvidence, ["numeric-nexthop-identity", "numeric-route-link-identity", "numeric-interface-index-identity", "complete-recursive-link-catalogue"]) + assert.equal(result.securityProof, false) + } +}) + +test("direct final endpoint cannot satisfy a longer desired recursive chain", () => { + const f = fixture() + f.manifest.routes[0].resolutionChain.unshift({ address: "198.18.0.1", table: "main", interface: "wan1" }) + const result = captureRoutingNetworkNativeParser(f.manifest).parseRoute("route1", f.route) + assert.equal(result.status, "unknown") + if (result.status === "unknown") assert.equal(result.reason, "unsupported-recursive-nexthop-link-proof") +}) + +test("route ID referencing another observed session and noncanonical duplicate prefix cannot pass attribution", () => { + const f = fixture(), raw = structuredClone(f.route) as unknown as RawMutation + raw.sessionsPayload.push({ ...raw.sessionsPayload[0], ".id": "*OTHER", "remote.address": "192.0.2.9" }) + raw.payload[0]["bgp.peer-cache-id"] = "*OTHER" + assert.equal(f.parser.parseRoute("route1", raw).status, "unknown") + const malformed = structuredClone(f.route) + malformed.payload.push({ ...malformed.payload[0], ".id": "*R2", "dst-address": "203.0.113.1/24" }) + assert.equal(f.parser.parseRoute("route1", malformed).status, "unknown") +}) + +test("a different direct nexthop cannot bypass the missing route-origin binding", () => { + const f = fixture(), raw = structuredClone(f.route) + raw.payload[0].gateway = "192.0.2.9@main"; raw.payload[0]["immediate-gw"] = "192.0.2.9%wan9" + const result = f.parser.parseRoute("route1", raw) + assert.equal(result.status, "unknown") + if (result.status === "unknown") assert.equal(result.reason, "unsupported-route-bgp-origin-namespace") +}) + +for (const mutation of ["duplicate-config", "wrong-config-id", "unresolved-template", "missing-endpoint", "duplicate-session", "wrong-asn", "missing-afi", "wrong-vrf", "disabled-config", "missing-established", "contradictory-state", "name-only"] as const) test("BGP " + mutation + " cannot establish an exact binding", () => { + const f = fixture(), raw = structuredClone(f.bgp) as unknown as RawMutation, session = raw.payload[0], config = raw.connectionsPayload[0] + if (mutation === "duplicate-config") raw.connectionsPayload.push({ ...config, ".id": "*C2" }) + if (mutation === "wrong-config-id") config[".id"] = "*C2" + if (mutation === "unresolved-template") config.templates = "default" + if (mutation === "missing-endpoint") delete config["local.address"] + if (mutation === "duplicate-session") raw.payload.push({ ...session, ".id": "*B3" }) + if (mutation === "wrong-asn") session["remote.as"] = "65002" + if (mutation === "missing-afi") delete session["remote.afi"] + if (mutation === "wrong-vrf") session.vrf = "diagnostic" + if (mutation === "disabled-config") config.disabled = "true" + if (mutation === "missing-established") delete session.established + if (mutation === "contradictory-state") session.state = "idle" + if (mutation === "name-only") { session.name = "peer-1"; delete session["remote.address"] } + assert.equal(f.parser.parseBgp("peer1", raw).status, "unknown") +}) + +for (const ipv6 of [false, true]) test("BGP runtime context can come from both exact qualified " + (ipv6 ? "IPv6" : "IPv4") + " endpoints", () => { + const f = fixture(ipv6), raw = structuredClone(f.bgp) as unknown as RawMutation, session = raw.payload[0] + const local = session["local.address"], remote = session["remote.address"] + delete session.vrf + session["local.address"] = `${local}@main` + session["remote.address"] = `${remote}@main` + const observation = f.parser.parseBgp("peer1", raw) + assert.equal(observation.status, "observed", JSON.stringify(observation)) + assert.equal(observation.securityProof, false) + for (const mutation of [ + { "remote.address": remote }, { "remote.address": `${remote}@tenant` }, + { "remote.address": `${remote}@main@main` }, { "routing-table": "main" }, + { vrf: "tenant" }, { vrf: null }, + ]) { + assert.equal(f.parser.parseBgp("peer1", { ...raw, payload: [{ ...session, ...mutation }] }).status, "unknown") + } + assert.equal(f.parser.parseBgp("peer1", { ...raw, payload: [session, { ...session, ".id": "*B3" }] }).status, "unknown") +}) + +test("native cached non-established BGP cannot invent a model FSM state from an undocumented field", () => { + const f = fixture(), raw = structuredClone(f.bgp) as unknown as RawMutation + raw.payload[0].established = "false" + const parsed = f.parser.parseBgp("peer1", raw); assert.equal(parsed.status, "observed") + if (parsed.status === "observed") assert.equal(parsed.observation.state, "not-established") + raw.payload[0].state = "idle" + assert.equal(f.parser.parseBgp("peer1", raw).status, "unknown") +}) + +test("cached session health stays observed while route attribution is unknown; explicit inert classifier model remains separate", () => { + const f = fixture(); f.bgp.payload[0].established = "false" + const route = f.parser.parseRoute("route1", f.route), bgp = f.parser.parseBgp("peer1", f.bgp), bfd = f.parser.parseBfd("bfd1", f.bfd), probe = f.parser.parseProbe("probe1", f.probe) + assert.equal(route.status, "unknown") + assert(bgp.status === "observed" && bfd.status === "observed" && probe.status === "observed") + // Explicit inert classifier input, NOT an observation returned by the native parser. + const modelRoute: RoutingNetworkRound["routes"][number] = { ...f.manifest.routes[0], active: true, resolved: true, protocol: "bgp", resolutionChain: f.manifest.routes[0].resolutionChain.map(link => ({ ...link, resolved: true })) } + const start = f.manifest.sourceCreatedAt, now = start + 10000 + const rounds: RoutingNetworkRound[] = [2000, 3000].map(offset => ({ identity: f.manifest.identity, startedAt: start + offset, observedAt: start + offset + 100, + configuration: f.manifest.configuration, routes: [modelRoute], bgp: [bgp.observation], bfd: [bfd.observation], probes: [probe.observation], + // Inert model coverage fixture only; parseLogs never claims this contract. + logs: f.manifest.logs.map(scope => ({ ...scope, coverageStartAt: f.manifest.observationStartAt, coveredUntil: start + offset + 100, capturedAt: start + offset + 100, + toSequence: 0, responseRowCount: 0, moreAvailable: false, rotationDetected: false, clockSynchronized: true, rows: [] })) })) + const budget = spendRoutingNetworkBudget(createRoutingNetworkBudget(start, [f.manifest.deadline], start), "forward", now, { requests: 20, bytes: 40000, rows: 40 }).budget + const result = classifyRoutingNetwork(f.manifest, rounds, now, budget) + assert.equal(result.verdict, "failed"); assert.equal(result.securityProof, false) + rounds[0].logs = []; assert.equal(classifyRoutingNetwork(f.manifest, rounds, now, budget).verdict, "unknown") +}) + +test("a second session missing its instance cannot be silently excluded from exact mapping", () => { + const f = fixture(), raw = structuredClone(f.bgp) as unknown as RawMutation + const row = { ...raw.payload[0], ".id": "*OTHER" }; delete row.instance + raw.payload.push(row) + assert.equal(f.parser.parseBgp("peer1", raw).status, "unknown") +}) + +for (const mutation of ["duplicate", "wrong-interface", "wrong-vrf", "missing-context", "wrong-family", "missing-state", "invented-success"] as const) test("BFD " + mutation + " is unknown", () => { + const f = fixture(), raw = structuredClone(f.bfd) as unknown as RawMutation + if (mutation === "duplicate") raw.payload.push({ ...raw.payload[0], ".id": "*D2" }) + if (mutation === "wrong-interface") raw.payload[0].interface = "other" + if (mutation === "wrong-vrf") raw.payload[0].vrf = "tenant" + if (mutation === "missing-context") { raw.payload[0]["local-address"] = "192.0.2.2"; raw.payload[0]["remote-address"] = "192.0.2.1" } + if (mutation === "wrong-family") raw.payload[0]["remote-address"] = "2001:db8::1%wan1@main" + if (mutation === "missing-state") delete raw.payload[0].state + if (mutation === "invented-success") raw.payload[0].state = "healthy" + assert.equal(f.parser.parseBfd("bfd1", raw).status, "unknown") +}) + +test("BFD canonicalizes actual IPv6 endpoint spelling and never infers not-configured from empty runtime", () => { + const f = fixture(true), raw = structuredClone(f.bfd) + raw.payload[0]["remote-address"] = "2001:0db8:0:0:0:0:0:1%wan1@main" + assert.equal(f.parser.parseBfd("bfd1", raw).status, "observed") + f.manifest.bfd[0] = { key: "bfd1", serverId: 1, applicability: "not-applicable", reason: "not-configured", configurationIdentity: "absent1" } + assert.equal(captureRoutingNetworkNativeParser(f.manifest).parseBfd("bfd1", { ...f.bfd, payload: [] }).status, "unknown") +}) + +test("documented terminal zero receive is observed failure data; partial loss/timeout is not promoted", () => { + const f = fixture(), raw = { ...f.probe, pingPayload: [{ sent: "3", received: "0", "packet-loss": "100" }], tracePayload: undefined } + const result = f.parser.parseProbe("probe1", raw); assert.equal(result.status, "observed") + if (result.status === "observed") { assert.equal(result.observation.received, 0); assert.equal(result.observation.trace.kind, "not-run") } + assert.equal(f.parser.parseProbe("probe1", { ...raw, pingPayload: [{ sent: "3", received: "1", "packet-loss": "67", "avg-rtt": "1ms" }] }).status, "unknown") + assert.equal(f.parser.parseProbe("probe1", { ...raw, pingPayload: [{ status: "timeout" }] }).status, "unknown") +}) + +for (const mutation of ["wrong-vrf", "wrong-source", "wrong-destination", "partial-count", "over-count", "contradictory-loss", "duplicate-summary", "trace-timeout", "missing-hop", "foreign-final", "false-loss", "invented-success"] as const) test("probe " + mutation + " remains unknown", () => { + const f = fixture(), raw = structuredClone(f.probe) as unknown as RawMutation + if (mutation === "wrong-vrf") raw.request.probeVrf = "tenant" + if (mutation === "wrong-source") raw.request.source = "198.51.100.2" + if (mutation === "wrong-destination") raw.pingPayload[0].host = "203.0.113.2" + if (mutation === "partial-count") raw.pingPayload.pop() + if (mutation === "over-count") raw.pingPayload[2].received = "4" + if (mutation === "contradictory-loss") raw.pingPayload[2]["packet-loss"] = "100" + if (mutation === "duplicate-summary") raw.pingPayload.push(structuredClone(raw.pingPayload[2])) + if (mutation === "trace-timeout") raw.tracePayload[0].status = "timeout" + if (mutation === "missing-hop") raw.tracePayload[0].address = "" + if (mutation === "foreign-final") raw.tracePayload[1].address = "203.0.113.2" + if (mutation === "false-loss") raw.tracePayload[0].loss = false + if (mutation === "invented-success") raw.pingPayload = [{ stagePassed: true, status: "passed" }] + assert.equal(f.parser.parseProbe("probe1", raw).status, "unknown") +}) + +test("traceroute final complete section is parsed separately; out-of-order snapshots are unknown", () => { + const f = fixture(), raw = structuredClone(f.probe) as unknown as RawMutation + raw.tracePayload = [{ ".section": "0", sent: "1", loss: "100", status: "timeout" }, ...raw.tracePayload.map(row => ({ ...row, ".section": "1" }))] + assert.equal(f.parser.parseProbe("probe1", raw).status, "observed") + raw.tracePayload.push({ ".section": "0", sent: "1", loss: "0", address: "203.0.113.1" }) + assert.equal(f.parser.parseProbe("probe1", raw).status, "unknown") +}) + +test("successful counters with a native terminal timeout status are contradictory and unknown", () => { + const f = fixture(), raw = structuredClone(f.probe) as unknown as RawMutation + raw.pingPayload[2].status = "timeout" + assert.equal(f.parser.parseProbe("probe1", raw).status, "unknown") +}) + +test("reader probe metadata selectors and derived hop addresses must agree with captured scope", () => { + const f = fixture(), raw = { ...f.probe, key: "probe1", expectedHopAddresses: [{ serverId: 1, interface: "wan1", address: "192.0.2.1" }, { serverId: 1, interface: "exit1", address: "203.0.113.1" }] } + assert.equal(f.parser.parseProbe("probe1", raw).status, "observed") + assert.equal(f.parser.parseProbe("probe1", { ...raw, key: "other" }).status, "unknown") + raw.expectedHopAddresses[0].serverId = 2; assert.equal(f.parser.parseProbe("probe1", raw).status, "unknown") + raw.expectedHopAddresses[0].serverId = 1; raw.expectedHopAddresses[0].address = "192.0.2.9"; assert.equal(f.parser.parseProbe("probe1", raw).status, "unknown") +}) + +test("logs raw shape can be bounded but empty ring and SQL history never establish interval coverage", () => { + const f = fixture() + const envelope = { ...f.base, resource: "/log", buffer: "memory" } + const row = { ".id": "*L1", buffer: "memory", time: "10:00:00", topics: "bgp,info", message: "session up" } + const rows = (count: number) => Array.from({ length: count }, (_, index) => ({ ...row, ".id": `*${index.toString(16)}` })) + for (const payload of [[], [row], rows(1000)]) { + const result = f.parser.parseLogs({ ...envelope, payload }) + assert.equal(result.status, "unknown"); if (result.status === "unknown") assert.equal(result.reason, "authenticated-log-interval-coverage-unsupported") + } + const { buffer: _omitted, ...withoutBuffer } = row + for (const second of [{ ...row, buffer: "other" }, withoutBuffer]) { + const result = f.parser.parseLogs({ ...envelope, payload: [row, { ...second, ".id": "*L2" }] }) + assert.equal(result.status, "unknown") + if (result.status === "unknown") assert.equal(result.reason, "native-log-buffer-unconfirmed") + } + assert.equal(f.parser.parseLogs({ ...envelope, payload: rows(1001) }).status, "unknown") + assert.equal(f.parser.parseLogs({ ...envelope, resource: "router_log_entries", payload: [] }).status, "unknown") +}) + +test("decoded byte/row/depth/id bounds and metadata drift reject malformed catalogues", () => { + const f = fixture() + const variants = [null, {}, [null], [{ ...f.bfd.payload[0], opaque: "x".repeat(1048577) }], Array.from({ length: 1001 }, (_, index) => ({ ...f.bfd.payload[0], ".id": "*D" + index })), [{ ...f.bfd.payload[0], ".id": undefined }], [f.bfd.payload[0], f.bfd.payload[0]]] + for (const payload of variants) assert.equal(f.parser.parseBfd("bfd1", { ...f.bfd, payload }).status, "unknown") + for (const routerOsVersion of ["6.49.10", "7.19.5", "7.99", "8.0", "7.20beta1", ""]) assert.equal(f.parser.parseBfd("bfd1", { ...f.bfd, routerOsVersion }).status, "unknown") + assert.equal(f.parser.parseBfd("bfd1", { ...f.bfd, serverId: 2 }).status, "unknown") + assert.equal(f.parser.parseBgp("unscoped", f.bgp).status, "unknown") +}) + +test("manifest and returned observations are deeply separated from external input mutation", () => { + const f = fixture() + f.manifest.bgp[0].remoteAsn = 65002 + const result = f.parser.parseBgp("peer1", f.bgp); assert.equal(result.status, "observed") + if (result.status === "observed") result.observation.remoteAsn = 65003 + const again = f.parser.parseBgp("peer1", f.bgp) + if (again.status === "observed") assert.equal(again.observation.remoteAsn, 65001) +}) diff --git a/backend/src/modules/mcp/routing-network-native-parser.ts b/backend/src/modules/mcp/routing-network-native-parser.ts new file mode 100644 index 00000000..79f0c28c --- /dev/null +++ b/backend/src/modules/mcp/routing-network-native-parser.ts @@ -0,0 +1,239 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { canonicalBgpPolicyPrefix } from "../../services/bgp-community-policy-model.js" +import { captureRoutingNetworkManifest, type RoutingNetworkRound } from "./routing-network-verifier-model.js" +import { matchesBgpSessionContext, parseRoutingEndpoint } from "./routing-native-endpoint.js" + +/** Pure decoded-response parsers, NOT source authentication or completeness + * proof. Only the explicit RouterOS7.20–7.26 field subset below is recognized. + * Actual wire limits/current source authority belong to the native reader. */ +// Native semantic sources (not a claim of live-device acceptance): +// https://help.mikrotik.com/docs/spaces/ROS/pages/59965493/routing+route +// https://manual.mikrotik.com/docs/cli-reference/routing/route/ +// https://manual.mikrotik.com/docs/cli-reference/routing/bgp/session/ +// https://manual.mikrotik.com/docs/cli-reference/routing/nexthop/ +// https://help.mikrotik.com/docs/spaces/ROS/pages/47579162/REST+API +type Row = Record +type Unknown = { status: "unknown"; reason: string; securityProof: false; diagnostic?: Record } +export type RoutingNativeParseResult = Unknown | { status: "observed"; observation: T; securityProof: false } +const unknown = (reason: string, diagnostic?: Record): Unknown => ({ status: "unknown", reason, securityProof: false, ...(diagnostic ? { diagnostic } : {}) }) +const observed = (observation: T): RoutingNativeParseResult => ({ status: "observed", observation: structuredClone(observation), securityProof: false }) +const fail = (reason: string): never => { throw Error(reason) } +const flag = (value: unknown) => value === true || value === "true" ? true : value === false || value === "false" ? false : undefined +const literal = (value: unknown): string => typeof value === "string" && value.length > 0 && value.length <= 256 ? value : fail("missing-or-unsupported-field") +const integer = (value: unknown, maximum: number): number => { + if (typeof value !== "number" && !(typeof value === "string" && /^(?:0|[1-9]\d*)$/.test(value))) return fail("malformed-counter") + const parsed = Number(value) + return Number.isSafeInteger(parsed) && parsed >= 0 && parsed <= maximum ? parsed : fail("malformed-counter") +} +const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b) +function canonicalAddress(value: unknown): string { + const text = literal(value), family = isIP(text) + if (!family || text.includes("%")) return fail("unsupported-address") + return canonicalBgpPolicyPrefix(text + "/" + (family === 4 ? 32 : 128)).split("/")[0] +} +function endpoint(value: unknown) { + const parsed = parseRoutingEndpoint(value) + return { ...parsed, address: canonicalAddress(parsed.address) } +} +function decodedRows(payload: unknown, maximum = 1000, ids = true): Row[] { + let nodes = 0 + const json = (value: unknown, depth = 0): boolean => { + if (++nodes > 30000 || depth > 8) return false + if (value === null || typeof value === "boolean" || typeof value === "string") return true + if (typeof value === "number") return Number.isFinite(value) + if (Array.isArray(value)) return value.every(item => json(item, depth + 1)) + return !!value && typeof value === "object" && [Object.prototype, null].includes(Object.getPrototypeOf(value)) && Object.values(value).every(item => json(item, depth + 1)) + } + if (!Array.isArray(payload) || payload.length > maximum || !json(payload) || Buffer.byteLength(JSON.stringify(payload), "utf8") > 1048576) return fail("decoded-response-limit-or-shape") + if (payload.some(value => !value || typeof value !== "object" || Array.isArray(value))) return fail("malformed-native-row") + const rows = payload as Row[] + if (ids && (rows.some(row => typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"])) || new Set(rows.map(row => row[".id"])).size !== rows.length)) return fail("missing-or-duplicate-native-id") + return rows +} +const envelope = z.object({ serverId: z.number().int().positive(), routerOsVersion: z.string().max(64), resource: z.string().max(64), payload: z.unknown() }).strict() +const bgpEnvelope = envelope.extend({ connectionsPayload: z.unknown() }).strict() +const routeEnvelope = envelope.extend({ connectionsPayload: z.unknown(), sessionsPayload: z.unknown() }).strict() +export const ROUTING_NATIVE_LOG_BUFFER = "memory" as const +export const ROUTING_NATIVE_LOG_PATH = `/log?buffer=${ROUTING_NATIVE_LOG_BUFFER}&.proplist=.id,buffer,time,topics,message` +const logEnvelope = envelope.extend({ buffer: z.literal(ROUTING_NATIVE_LOG_BUFFER), coverage: z.literal("unknown").optional() }).strict() +const probeEnvelope = z.object({ serverId: z.number().int().positive(), routerOsVersion: z.string().max(64), pingPayload: z.unknown(), tracePayload: z.unknown().optional(), + key: z.string().min(1).max(128).optional(), expectedHopAddresses: z.array(z.object({ serverId: z.number().int().positive(), interface: z.string().min(1).max(128), address: z.string().max(45) }).strict()).min(1).max(16).optional(), + request: z.object({ destination: z.string(), source: z.string(), interface: z.string(), probeVrf: z.string(), count: z.literal(3), maxHops: z.literal(16) }).strict() }).strict() +function metadata(value: { serverId: number; routerOsVersion: string }, serverId: number) { + if (value.serverId !== serverId) fail("server-identity-drift") + if (!/^7\.2[0-6](?:\.\d{1,3})?(?: \((?:stable|long-term)\))?$/.test(value.routerOsVersion)) fail("unsupported-routeros-version") +} +const attempt = (work: () => RoutingNativeParseResult): RoutingNativeParseResult => { + try { return work() } catch (error) { return unknown(error instanceof Error ? error.message : "malformed-or-unsupported-input") } +} +const afi = (value: unknown) => { + const entries = literal(value).split(",") + if (entries.some(value => !["ip", "ipv6"].includes(value)) || new Set(entries).size !== entries.length) return fail("unsupported-afi") + return entries +} + +/** Captures a validated deep-frozen manifest. Returned observations are data, + * never grants. Neither names nor community tags invent target/session scope. */ +export function captureRoutingNetworkNativeParser(rawManifest: unknown) { + const manifest = captureRoutingNetworkManifest(rawManifest) + return Object.freeze({ + parseRoute: (key: string, raw: unknown): RoutingNativeParseResult => attempt(() => { + const wanted = manifest.routes.find(value => value.key === key) + if (!wanted) return unknown("route-not-in-manifest") + const value = routeEnvelope.parse(raw); metadata(value, wanted.serverId) + if (value.resource !== "/routing/route") return unknown("unsupported-route-resource") + const rows = decodedRows(value.payload) + for (const candidate of rows.filter(row => row["routing-table"] === wanted.table)) { + const prefix = literal(candidate["dst-address"]) + // The shared RouterOS catalogue also carries interface names and VPN + // destinations with route distinguishers. An exact target collision + // still participates in the ambiguity check below. + if (candidate.afi === "link" || candidate.afi === "vpnv4" || candidate.afi === "vpnv6") continue + if (canonicalBgpPolicyPrefix(prefix) !== prefix) return unknown("noncanonical-installation-route-prefix") + } + const matching = rows.filter(row => row["routing-table"] === wanted.table && row["dst-address"] === wanted.prefix) + if (matching.length !== 1) return unknown("missing-or-ambiguous-installation-route") + const row = matching[0], nativePrefix = literal(row["dst-address"]) + if (canonicalBgpPolicyPrefix(nativePrefix) !== nativePrefix || isIP(nativePrefix.split("/")[0]) !== (wanted.family === "ipv4" ? 4 : 6)) return unknown("route-prefix-or-afi-drift") + if (flag(row.bgp) !== true || row.dynamic !== undefined && flag(row.dynamic) !== true || flag(row.active) === undefined || ["static", "connect", "ospf", "rip", "isis", "blackhole"].some(field => flag(row[field]) === true)) return unknown("unsupported-or-ambiguous-route-origin") + if (!(wanted.family === "ipv4" ? ["ip4", "ip"].includes(String(row.afi)) : ["ip6", "ipv6"].includes(String(row.afi)))) return unknown("route-afi-unconfirmed") + if (row.contribution !== (flag(row.active) ? "active" : "best-candidate") || ["disabled", "filtered", "unreachable", "ecmp"].some(field => row[field] !== undefined && flag(row[field]) !== false)) return unknown("route-contribution-or-flags-unconfirmed") + const gateway = endpoint(row.gateway), immediate = endpoint(row["immediate-gw"]) + const resolverTable = gateway.vrf ?? (row["routing-table"] === "main" ? "main" : undefined) + if (!immediate.interface || !resolverTable || resolverTable !== wanted.resolverTable || immediate.vrf !== undefined && immediate.vrf !== resolverTable || gateway.interface !== undefined && gateway.interface !== immediate.interface) return unknown("unconfirmed-route-resolution-context") + // The 7.26 CLI reference types nexthop-id, links.route-id/fwp-id and + // immediate-gw.interface-idx as numbers, but specifies no join between + // those namespaces and REST .id values. Scope/LPM reconstruction cannot + // supply that missing identity: RouterOS keeps separate nexthops for + // scope/check-gateway combinations and can correct scopes internally. + // immediate-gw documents the final forwarding endpoint, not every link. + // Keep the entire chain unknown until that wire contract is established. + if (wanted.resolutionChain.length !== 1 || gateway.address !== immediate.address) return unknown("unsupported-recursive-nexthop-link-proof", { + missingEvidence: ["numeric-nexthop-identity", "numeric-route-link-identity", "numeric-interface-index-identity", "complete-recursive-link-catalogue"], + }) + // peer-cache-id is a cache namespace, not a documented session REST .id. + // Matching opaque strings (or a caller-supplied bgp.session alias) do not + // prove route origin. No manufacturer/provenance-bound mapping producer + // is available here; keep raw field presence distinct from a valid link. + return unknown("unsupported-route-bgp-origin-namespace", { + peerCacheIdPresent: Object.hasOwn(row, "bgp.peer-cache-id"), + sessionFieldPresent: Object.hasOwn(row, "bgp.session"), + routeToSessionBinding: "unproven", + }) + }), + parseBgp: (key: string, raw: unknown): RoutingNativeParseResult => attempt(() => { + const wanted = manifest.bgp.find(value => value.key === key) + if (!wanted) return unknown("bgp-not-in-manifest") + const value = bgpEnvelope.parse(raw); metadata(value, wanted.serverId) + if (value.resource !== "/routing/bgp/session") return unknown("unsupported-bgp-resource") + const rows = decodedRows(value.payload), connections = decodedRows(value.connectionsPayload) + const expectedAfi = wanted.family === "ipv4" ? "ip" : "ipv6" + const bindings = connections.map(row => { + if (!(row.templates === "" || Array.isArray(row.templates) && row.templates.length === 0)) fail("unresolved-bgp-template-inheritance") + if (flag(row.disabled) === undefined) fail("unconfirmed-connection-disabled-state") + return { row, local: endpoint(row["local.address"]), remote: endpoint(row["remote.address"]), instance: literal(row.instance), + asn: integer(row["remote.as"], 4294967295), afi: afi(row.afi), vrf: literal(row.vrf), table: literal(row["routing-table"]) } + }) + const matches = bindings.filter(binding => binding.instance === wanted.instance && binding.local.address === wanted.localAddress && binding.remote.address === wanted.remoteAddress && binding.asn === wanted.remoteAsn && binding.afi.includes(expectedAfi)) + if (matches.length !== 1 || matches[0].row[".id"] !== wanted.connectionId) return unknown("missing-or-ambiguous-exact-bgp-connection") + const binding = matches[0] + if (flag(binding.row.disabled) !== false || [binding.local.vrf, binding.remote.vrf].some(vrf => vrf !== undefined && vrf !== binding.vrf)) return unknown("bgp-connection-disabled-or-context-drift") + const tables = [...new Set(manifest.routes.filter(route => route.sessionKey === wanted.key && route.serverId === wanted.serverId).map(route => route.table))] + if (tables.length !== 1 || tables[0] !== binding.table) return unknown("unconfirmed-bgp-installation-table") + const relevant: Row[] = [] + for (const row of rows) { + const instance = literal(row.instance) + const local = endpoint(row["local.address"]), remote = endpoint(row["remote.address"]) + if (instance !== wanted.instance) continue + if (local.address !== wanted.localAddress || remote.address !== wanted.remoteAddress) continue + if (integer(row["remote.as"], 4294967295) !== wanted.remoteAsn || !afi(row["remote.afi"]).includes(expectedAfi) || !afi(row["local.afi"]).includes(expectedAfi)) return unknown("bgp-session-asn-or-afi-drift") + if (!matchesBgpSessionContext(row, binding, local, remote) || local.interface !== binding.local.interface || remote.interface !== binding.remote.interface) return unknown("bgp-session-context-drift") + relevant.push(row) + } + if (relevant.length !== 1) return unknown("missing-or-ambiguous-bgp-session") + const row = relevant[0], established = flag(row.established) + // Identity and health are separate: RouterOS retains cached disconnected + // sessions. False proves only not-established, never idle/connect/etc. + if (established === undefined || row.state !== undefined) return unknown("unsupported-or-contradictory-bgp-state") + return observed({ ...wanted, state: established ? "established" : "not-established" }) + }), + parseBfd: (key: string, raw: unknown): RoutingNativeParseResult => attempt(() => { + const wanted = manifest.bfd.find(value => value.key === key) + if (!wanted) return unknown("bfd-not-in-manifest") + const value = envelope.parse(raw); metadata(value, wanted.serverId) + if (value.resource !== "/routing/bfd/session") return unknown("unsupported-bfd-resource") + const rows = decodedRows(value.payload) + if (wanted.applicability !== "required") return unknown("runtime-session-absence-cannot-prove-bfd-not-configured") + const relevant: Row[] = [] + for (const row of rows) { + const local = endpoint(row["local-address"]), remote = endpoint(row["remote-address"]) + if (local.address !== wanted.localAddress || remote.address !== wanted.remoteAddress) continue + if (isIP(local.address) !== isIP(remote.address)) return unknown("bfd-afi-drift") + const interfaces = [local.interface, remote.interface, row.interface].filter(value => value !== undefined) + const vrfs = [local.vrf, remote.vrf, row.vrf].filter(value => value !== undefined) + if (!interfaces.length || !vrfs.length || interfaces.some(value => value !== wanted.interface) || vrfs.some(value => value !== wanted.vrf)) return unknown("bfd-interface-or-vrf-drift") + if (row.disabled !== undefined && flag(row.disabled) !== false) return unknown("bfd-disabled-or-unsupported") + relevant.push(row) + } + if (relevant.length !== 1) return unknown("missing-or-ambiguous-bfd-session") + const state = literal(relevant[0].state).toLowerCase() + if (!["up", "down", "init", "admin-down"].includes(state)) return unknown("unsupported-bfd-state") + return observed({ ...wanted, state: state as "up" | "down" | "init" | "admin-down" }) + }), + parseProbe: (key: string, raw: unknown): RoutingNativeParseResult => attempt(() => { + const wanted = manifest.probes.find(value => value.key === key) + if (!wanted) return unknown("probe-not-in-manifest") + const value = probeEnvelope.parse(raw); metadata(value, wanted.serverId) + if (value.key !== undefined && value.key !== key) return unknown("probe-selector-drift") + if (value.expectedHopAddresses && (value.expectedHopAddresses.some(hop => !manifest.serverIds.includes(hop.serverId)) || !same(value.expectedHopAddresses.map(hop => canonicalAddress(hop.address)), wanted.expectedHops))) return unknown("probe-derived-hop-address-drift") + if (!same(value.request, { destination: wanted.destination, source: wanted.source, interface: wanted.interface, probeVrf: wanted.probeVrf, count: 3, maxHops: 16 })) return unknown("probe-request-context-drift") + const rows = decodedRows(value.pingPayload, 256, false) + if (!rows.length || rows.some(row => row.error !== undefined || row.status !== undefined && row.status !== "timeout")) return unknown("unsupported-ping-response") + if (rows.some(row => row.host !== undefined && canonicalAddress(row.host) !== wanted.destination)) return unknown("ping-destination-drift") + const terminal = rows.at(-1)!, sent = integer(terminal.sent, 3), received = integer(terminal.received, 3) + if (sent !== 3 || received > sent) return unknown("incomplete-ping-terminal-counts") + const summaries = rows.filter(row => row.sent !== undefined || row.received !== undefined) + if (summaries.some((row, index) => integer(row.sent, 3) < integer(row.received, 3) || index > 0 && integer(row.sent, 3) <= integer(summaries[index - 1].sent, 3))) return unknown("ambiguous-ping-summaries") + const loss = literal(terminal["packet-loss"]) + if (!/^(?:0|[1-9]\d*)(?:\.\d+)?%?$/.test(loss) || Math.abs(Number(loss.replace(/%$/, "")) - (sent - received) / sent * 100) > 0.51) return unknown("inconsistent-ping-loss") + if (received !== 0 && received !== 3) return unknown("partial-ping-loss-not-terminal-failure") + if (received > 0 && terminal.status !== undefined) return unknown("contradictory-ping-terminal-status") + if (received === 0) { + if (value.tracePayload !== undefined && value.tracePayload !== null) return unknown("unexpected-trace-after-zero-receive-ping") + return observed({ ...wanted, sent: 3, received: 0, terminal: true, trace: { kind: "not-run", reason: "terminal-ping-failure" } }) + } + if (!/^\d+(?:\.\d+)?(?:ms|us|s)(?:\d+us)?$/.test(literal(terminal["avg-rtt"]))) return unknown("unsupported-ping-rtt") + let traceRows = decodedRows(value.tracePayload, 256, false) + if (!traceRows.length) return unknown("missing-traceroute") + if (traceRows.some(row => row[".section"] !== undefined)) { + const sections = traceRows.map(row => integer(row[".section"], 255)) + if (sections.some((section, index) => index > 0 && section < sections[index - 1])) return unknown("unordered-trace-sections") + const last = Math.max(...sections); traceRows = traceRows.filter(row => integer(row[".section"], 255) === last) + } + if (traceRows.length > 16) return unknown("trace-hop-limit") + const hops = traceRows.map((row, index) => { + if (row.error !== undefined || row.status !== undefined && row.status !== "" || integer(row.sent, 3) < 1 || integer(row.loss, 100) !== 0) fail("incomplete-or-unsupported-trace") + const rawAddress = typeof row.address === "object" && row.address !== null ? (row.address as Row).address : row.address + const address = endpoint(rawAddress) + if (isIP(address.address) !== (wanted.family === "ipv4" ? 4 : 6) || address.vrf !== undefined || address.interface !== undefined && address.interface !== wanted.interface) fail("trace-address-context-drift") + if (row.hop !== undefined && integer(row.hop, 16) !== index + 1) fail("ambiguous-trace-hop-order") + return { hop: index + 1, address: address.address, state: "responded" as const } + }) + if (hops.at(-1)!.address !== wanted.destination) return unknown("trace-destination-unconfirmed") + return observed({ ...wanted, sent: 3, received: 3, terminal: true, trace: { kind: "observed", hops } }) + }), + parseLogs: (raw: unknown): RoutingNativeParseResult => attempt(() => { + const value = logEnvelope.parse(raw) + if (!manifest.serverIds.includes(value.serverId)) return unknown("logs-outside-manifest") + metadata(value, value.serverId) + if (value.resource !== "/log") return unknown("unsupported-log-resource") + const rows = decodedRows(value.payload) + if (rows.some(row => row.buffer !== value.buffer)) return unknown("native-log-buffer-unconfirmed") + if (rows.some(row => typeof row.time !== "string" || typeof row.topics !== "string" || typeof row.message !== "string")) return unknown("malformed-native-log-row") + // No mapping from raw RouterOS ring IDs/time strings or SQL-history to + // authenticated generation/contiguous capture interval is established. + return unknown("authenticated-log-interval-coverage-unsupported", { parsedRows: rows.length }) + }), + }) +} diff --git a/backend/src/modules/mcp/routing-network-native-session.test.ts b/backend/src/modules/mcp/routing-network-native-session.test.ts new file mode 100644 index 00000000..13f840fc --- /dev/null +++ b/backend/src/modules/mcp/routing-network-native-session.test.ts @@ -0,0 +1,460 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import { EventEmitter } from "node:events" +import { randomUUID } from "node:crypto" +import { isIP } from "node:net" +import { test } from "node:test" +import { z } from "zod" +import ts from "typescript" +import { canonicalVrfMembers } from "../../services/vrf-members.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { readMeasurementAddress } from "./routing-measurement-address.js" + +/** Actual source, inert module providers: no configured DB, sockets, signing + * keys, native grants or checkpoint writers are initialized by these tests. */ +async function loadSource(file: string, providers: Record, globals: Record = {}) { + const source = await readFile(new URL(file, import.meta.url), "utf8") + const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + const exports: Record = {} + runInNewContext(compiled, { exports, require(name: string) { assert(name in providers, "Unexpected production dependency: " + name); return providers[name] }, + Buffer, URL, AbortSignal, AbortController, setTimeout, clearTimeout, structuredClone, ...globals }) + return exports +} +class SessionError extends Error { constructor(readonly code: string, message: string) { super(message) } } +type Factory = (options: Record) => Promise<{ readSnapshot(stage: string): Promise>; + readConfigurationBoundInverseSnapshot(assertCheckpoint: () => Promise): Promise>; + readConfigurationBoundForwardSnapshot(context: object): Promise>; close(): void; readBudget(): Record }> +async function fixture(reserved = false) { + let clock = Date.now(), revoked = false, sourceChanged = false, endpointChanged = false, payloadRows = 1, wrongVrf = false, failedProbe = false + let originalAddress = "198.51.100.2" + let afterNative: (() => void) | undefined, pendingNative: (() => Promise) | undefined + let nativeCalls = 0, authorityCalls = 0, callbacks = 0, transports = 0, sourceChecks = 0 + const requests: { serverId: number; path: string; body?: unknown }[] = [] + class ClockDate extends Date { static now() { return clock } } + const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new ClockDate(clock + 600000).toISOString() } + const connection = { id: principal.connectionId, owner_id: "owner", generation: "generation", owner_permissions: ["mm:network:read"], owner_valid_until: new ClockDate(clock + 600000), + policy: { scope: { serverIds: [1] }, expiresAt: new ClockDate(clock + 600000).toISOString(), permissions: ["mm:network:read"] } } + const fingerprint = (value: unknown): string => JSON.stringify(value) + const candidate = (id: string, targetInterface: string) => ({ id, jumpHostId: 1, exitNodeId: 1, wanInterface: "wan", + controlProbe: { targetInterface, expectedHops: [{ serverId: 1, interface: "hop" }, { serverId: 1, interface: targetInterface }] } }) + const profile = { serverId: 1, family: "ipv4", probeVrf: "probe", autoRecovery: reserved, + candidates: reserved ? [candidate("a", "target"), candidate("b", "target-b")] : [candidate("a", "target")] } + const input = { profile, principal, ownerId: "owner", policyProof: fingerprint(connection.policy), connectionGeneration: connection.generation, generation: "access", + contextSources: [{ serverId: 1, proof: "source" }], ...(reserved ? { proofRevision: 3, originalCandidateId: "a", candidateId: "b", + dependencyProof: "dependency", originalTargetProof: "target", originalRuleCiphertext: "original body", nativeId: "*1" } : {}) } + const row = { id: randomUUID(), state: "running", connection_id: principal.connectionId, created_at: new ClockDate(clock - 1000), plan_id: randomUUID(), operation_id: "routing.community.switch", operation_version: 7, + approved_by: "owner", step_state: "intent", intended_state: { kind: "service", service: "routing.community.scoped", input }, expires_at: new ClockDate(clock + 600000), evidence: { sealed: JSON.stringify({ steps: [{ kind: "service", service: "routing.community.scoped", input }], target: { scope: "servers", serverIds: [1] } }) } } + const endpoints = [{ id: 1, enabled: true, host: "owned", password: "private", routingEndpointEpoch: "fixed" }] + const configurationToken = Object.freeze({}) + const exports = await loadSource("./routing-network-native-session.ts", { + "node:net": { isIP }, zod: { z }, "@mmapp/contracts/mcp-routing": { routeAiProfileSchema: { parse: (value: unknown) => value } }, + "../../db/index.js": { pool: { query: async () => { sourceChecks++; return { rows: [{ ...row, plan_id: sourceChanged ? "changed" : row.plan_id }] } } } }, + "../../lib/permissions.js": { hasPermission: (permissions: string[], permission: string) => permissions.includes(permission) }, + "../../services/vrf-members.js": { canonicalVrfMembers }, "../../services/ospf-effective-disabled.js": { readOspfEffectiveDisabled }, "./routing-measurement-address.js": { readMeasurementAddress }, + "./security.js": { fingerprint, redact: (value: unknown) => value, McpError: SessionError, accessGeneration: async () => "access", decryptSecret: async (value: string) => value }, + "./store.js": { effectiveMcpConnection: async () => { authorityCalls++; if (revoked) throw new SessionError("RESET_REQUIRED", "revoked"); return connection } }, + "./routing-community-context.js": { readRouteCommunityContextWithSharedNativeSession: async (token: object) => { + assert(reserved) + const inspect = exports.inspectRoutingNetworkSharedContextRead as (value: object) => { candidateId: string } + const read = exports.readRoutingNetworkSharedResource as (value: object, serverId: number, path: string) => Promise + assert.equal(inspect(token).candidateId, "a") + await read(token, 1, "/routing/filter/rule") + return { scopedTopologyAvailable: true, before: { currentCandidateId: "a", dependencyProof: "dependency" }, + ownedTarget: { nativeId: "*1", matchesRuleProof: (body: string, proof: string) => body === "original body" && proof === "target" }, + fence: async () => {} } + } }, + "./routing-network-configuration-bridge.js": { readRoutingNetworkForwardRoundConfigurationContext: async (token: object, acquisition: object) => { + assert.equal(token, configurationToken, "Only the fixed bridge capability is accepted by this inert provider") + const read = exports.readRoutingNetworkSharedResource as (token: object, serverId: number, path: string) => Promise + await read(acquisition, 1, "/system/resource") + return { operationId: row.id, phase: "forward", checkpointProof: "inert-boundary-only", configuration: { nativeId: "*1" }, securityProof: false, + deadline: clock + 119000, checkpointObservedAt: new ClockDate(clock - 1).toISOString(), observedAt: new ClockDate(clock).toISOString(), sharedNetworkBudgetVerified: true } + } }, + "./registry.js": { findMcpOperation: () => ({ version: 7 }) }, + "./routing-community-scoped-request.js": { assertScopedCommunityRequest: async () => "genuine-provider-seal" }, + "./routing-profile-authority.js": { assertPersistentScopedProfileAuthority: async () => { if (revoked) throw new SessionError("RESET_REQUIRED", "profile revoked") }, routingProfileAuthorityBindingSchema: { parse: () => { throw Error("No persistent binding fixture") } } }, + "./routing-observation-source.js": { readRoutingEndpoints: async () => endpoints, createRoutingSourceContext: async () => ({ expected: input.contextSources, assertCurrent: async () => { if (endpointChanged) throw new SessionError("STATE_CONFLICT", "source changed") } }) }, + "./routing-network-native-destination.js": { resolveRoutingNetworkDestinationPins: async (_endpoints: unknown, options: { authorize(): Promise }) => { await options.authorize(); return [{ serverId: 1, host: "owned", address: "127.0.0.1", family: 4 }] } }, + "./routing-network-native-parser.js": { ROUTING_NATIVE_LOG_BUFFER: "memory", ROUTING_NATIVE_LOG_PATH: "/log?buffer=memory&.proplist=.id,buffer,time,topics,message" }, + "./routing-community-context-native.js": { routeContextClients: (_endpoints: unknown, _signal: AbortSignal, limits: { deadline: number; maxCalls: number; maxBytes: number }) => { + transports++; let calls = 0, bytes = 0 + const read = async (path: string, body?: unknown) => { + calls++; nativeCalls++; requests.push({ serverId: 1, path, body }); await pendingNative?.() + const payload = path === "/tool/ping" ? [{ sent: "3", received: failedProbe ? "0" : "3", "packet-loss": failedProbe ? "100%" : "0%" }] + : path.endsWith("/get") ? { ret: "false" } : path === "/interface" ? ["wan", "hop", "target", ...(reserved ? ["target-b"] : [])].map((name,index) => ({ ".id": "*i"+index, name })) + : path === "/ip/address" ? ["wan", "hop", "target", ...(reserved ? ["target-b"] : [])].map((name,index) => ({ ".id": "*a"+index, interface: name, address: ["192.0.2.1/32", "198.51.100.1/32", originalAddress + "/32", "198.51.100.3/32"][index] })) + : path === "/ip/vrf" ? [{ ".id": "*v1", name: wrongVrf ? "other" : "probe", interfaces: "wan" }, { ".id": "*v2", name: "main", interfaces: "all" }] + : path === "/system/resource" ? [{ version: "7.20.1" }] : Array.from({ length: payloadRows }, () => ({ ".id": "*1" })) + bytes += Buffer.byteLength(JSON.stringify(payload)); afterNative?.(); afterNative = undefined; return payload + } + return Object.assign(new Map([[1, { get: read, post: read }]]), { readBudget: () => ({ ...limits, calls, bytes }) }) + } }, + }, { Date: ClockDate }) + const options = { sourceOperationId: row.id, context: { principal, connection }, authorize: async () => { callbacks++ }, deadline: clock + 119000, manifest: { servers: [1], probes: [] } } + return { create: exports.createRoutingNetworkNativeSession as Factory, + checkRounds: exports.sameConfigurationBoundForwardRounds as (baseline: object, rounds: object[]) => boolean, + configurationToken, options, requests, stats: () => ({ nativeCalls, authorityCalls, callbacks, transports, sourceChecks }), + advance: (ms: number) => { clock += ms }, revoke: () => { revoked = true }, changeSource: () => { sourceChanged = true }, changeEndpoint: () => { endpointChanged = true }, + rows: (value: number) => { payloadRows = value }, wrongVrf: () => { wrongVrf = true }, failProbe: () => { failedProbe = true }, + changeOriginalAddress: () => { originalAddress = "198.51.100.4" }, afterNative: (hook: () => void) => { afterNative = hook }, pendingNative: (hook: () => Promise) => { pendingNative = hook } } +} + +test("configuration-bound forward reads serialize around the native snapshot and charge both reads before publishing budget", async () => { + const f = await fixture(), session = await f.create(f.options) + await session.readSnapshot("before") + let release!: () => void, entered = false + const barrier = new Promise(done => { release = done }) + f.pendingNative(async () => { entered = true; await barrier }) + const round = session.readConfigurationBoundForwardSnapshot(f.configurationToken) + const concurrent = session.readSnapshot("after-forward") + try { + for (let i = 0; i < 50 && !entered; i++) await new Promise(done => setTimeout(done, 1)) + assert.equal(entered, true); assert.equal(f.stats().nativeCalls, 7, "Concurrent snapshot must stay behind the current acquisition") + } finally { f.pendingNative(async () => {}); release() } + const captured = await round + assert.equal((captured.budget as { calls: number }).calls, 14) + assert(captured.configurationReadbacks); assert.equal(captured.securityProof, false) + assert.equal(captured.forwardingVerified, false); assert.equal(captured.logCoverage, "unknown") + assert.equal((await concurrent).round, 2); assert.equal(session.readBudget().calls, 20) + assert.equal(f.stats().transports, 1) + session.close() +}) + +test("effect diagnostics require the original baseline and two ordered configuration-bound rounds", async () => { + const f = await fixture(), session = await f.create(f.options) + try { + const baseline = await session.readSnapshot("before") + const first = await session.readConfigurationBoundForwardSnapshot(f.configurationToken) + const second = await session.readConfigurationBoundForwardSnapshot(f.configurationToken) + assert(f.checkRounds(baseline, [first, second])) + assert(!f.checkRounds({ ...baseline }, [first, second])) + assert(!f.checkRounds(baseline, [first, structuredClone(second)])) + assert(!f.checkRounds(baseline, [second, first])) + } finally { session.close() } +}) + +for (const fault of ["caller-object", "endpoint", "revoke", "rows"] as const) + test("bound forward round closes the shared owner on " + fault, async () => { + const f = await fixture(), session = await f.create(f.options) + await session.readSnapshot("before") + if (fault === "endpoint") f.afterNative(f.changeEndpoint) + if (fault === "revoke") f.afterNative(f.revoke) + if (fault === "rows") f.rows(1000) + if (fault === "rows") await session.readConfigurationBoundForwardSnapshot(f.configurationToken) + await assert.rejects(session.readConfigurationBoundForwardSnapshot(fault === "caller-object" ? {} : f.configurationToken)) + assert.equal(session.readBudget().closed, true) + const stopped = f.stats().nativeCalls + await assert.rejects(session.readSnapshot("after-forward")); assert.equal(f.stats().nativeCalls, stopped) + }) + +test("one captured session retains deadline, manifest and cumulative counters across stable repeated phase rounds", async () => { + const f = await fixture(), session = await f.create(f.options) + f.options.authorize = async () => { throw Error("outer callback replaced") }; f.options.manifest.servers[0] = 999; f.options.deadline += 600000 + const first = await session.readSnapshot("before"), second = await session.readSnapshot("after-forward") + await session.readSnapshot("after-forward") + const third = await session.readSnapshot("after-inverse"); await session.readSnapshot("after-inverse") + assert.equal(first.securityProof, false); assert.equal(first.logCoverage, "unknown") + assert.equal((first.budget as { calls: number }).calls, 6); assert.equal((second.budget as { calls: number }).calls, 12); assert.equal((third.budget as { calls: number }).calls, 24) + assert.equal(session.readBudget().rows, 30); assert.equal(f.stats().transports, 1); assert(f.stats().authorityCalls > 30); assert(f.stats().callbacks > 60) + assert.equal(typeof first.startedAt, "string"); assert.equal(typeof first.observedAt, "string") + assert(f.requests.every(request => request.serverId === 1)); assert(Object.isFrozen(first)); assert(Object.isFrozen(first.servers)) + await assert.rejects(f.create({ ...f.options, deadline: Date.now() + 1000, manifest: { servers: [1], probes: [] }, authorize: async () => {} }), /уже создавалась/) +}) + +test("additive noop authorize cannot mask actual revoked authority, endpoint drift or source plan drift", async () => { + for (const change of ["revoke", "changeSource", "changeEndpoint"] as const) { + const f = await fixture(), session = await f.create(f.options) + f[change](); await assert.rejects(session.readSnapshot("before")); assert.equal(f.stats().nativeCalls, 0) + await assert.rejects(session.readSnapshot("before")); assert.equal(f.stats().nativeCalls, 0) + } +}) + +test("authority loss during a native await discards the entire response and permanently closes the session", async () => { + const f = await fixture(), session = await f.create(f.options) + f.afterNative(f.revoke); await assert.rejects(session.readSnapshot("before")); assert.equal(f.stats().nativeCalls, 1) + await assert.rejects(session.readSnapshot("after-forward")); assert.equal(f.stats().nativeCalls, 1) +}) + +test("row overflow, stage replay, explicit close and elapsed original deadline cannot reopen a session", async () => { + for (const failure of ["rows", "stage", "close", "deadline"] as const) { + const f = await fixture(), session = await f.create(f.options) + if (failure === "rows") f.rows(1001) + if (failure === "close") session.close() + if (failure === "deadline") f.advance(120000) + await assert.rejects(session.readSnapshot(failure === "stage" ? "after-forward" : "before")) + assert.equal(f.stats().nativeCalls, failure === "rows" ? 2 : 0) + await assert.rejects(session.readSnapshot("before")) + } +}) + +test("queued stages share cancellation and cannot dispatch after an in-flight close", async () => { + const f = await fixture(), session = await f.create(f.options) + let release!: () => void, began!: () => void + const pending = new Promise(resolve => { release = resolve }), started = new Promise(resolve => { began = resolve }) + f.pendingNative(async () => { began(); await pending }) + const first = assert.rejects(session.readSnapshot("before")), second = assert.rejects(session.readSnapshot("after-forward")) + await started; session.close(); release(); await Promise.all([first, second]); assert.equal(f.stats().nativeCalls, 1) +}) + +test("cumulative rows span snapshots rather than restarting for the inverse", async () => { + const f = await fixture(), session = await f.create(f.options) + f.rows(500) + await session.readSnapshot("before"); await session.readSnapshot("after-forward"); await session.readSnapshot("after-forward") + await assert.rejects(session.readSnapshot("after-inverse"), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + assert.equal(session.readBudget().rows, 10004); assert.equal(f.stats().transports, 1) +}) + +test("phase rounds reject a fourth sample, early inverse and backward reads without resetting their budget", async () => { + for (const scenario of ["forward-fourth", "inverse-early", "inverse-fourth", "backward"] as const) { + const f = await fixture(), session = await f.create(f.options) + await session.readSnapshot("before"); await session.readSnapshot("after-forward") + if (scenario !== "inverse-early") await session.readSnapshot("after-forward") + if (scenario === "forward-fourth") await session.readSnapshot("after-forward") + if (["inverse-fourth", "backward"].includes(scenario)) await session.readSnapshot("after-inverse") + if (scenario === "inverse-fourth") { await session.readSnapshot("after-inverse"); await session.readSnapshot("after-inverse") } + const before = f.stats().nativeCalls + await assert.rejects(session.readSnapshot(scenario === "inverse-early" || scenario === "inverse-fourth" ? "after-inverse" : "after-forward")) + assert.equal(f.stats().nativeCalls, before); assert.equal(f.stats().transports, 1) + } +}) + +test("an already aborted original signal performs no source SQL or native dispatch", async () => { + const f = await fixture(), controller = new AbortController(); controller.abort() + await assert.rejects(f.create({ ...f.options, context: { ...f.options.context, signal: controller.signal } })) + assert.equal(f.stats().sourceChecks, 0); assert.equal(f.stats().nativeCalls, 0) +}) + +test("invalid or reduced manifests fail before source SQL/native acquisition", async () => { + const f = await fixture() + for (const manifest of [{ servers: [1,1] }, { servers: [1], url: "https://arbitrary" }, { servers: [1], probes: [{ key: "x", serverId: 2 }] }]) await assert.rejects(f.create({ ...f.options, manifest })) + assert.equal(f.stats().sourceChecks, 0); assert.equal(f.stats().transports, 0) + await assert.rejects(f.create({ ...f.options, manifest: { servers: [2] } })) + assert.equal(f.stats().nativeCalls, 0) +}) + +test("probe commands use only original candidate owned addresses, disabled lookups and ordered VRF membership", async () => { + const f = await fixture() + const probe = { key: "a", serverId: 1, destination: "198.51.100.2", source: "192.0.2.1", interface: "wan", probeVrf: "probe" } + const session = await f.create({ ...f.options, manifest: { servers: [1], probes: [probe] } }) + const snapshot = await session.readSnapshot("before"), probes = snapshot.probes as { request: typeof probe; expectedHopAddresses: unknown[] }[] + assert.equal(probes.length, 1); assert.equal(probes[0].expectedHopAddresses.length, 2) + const commands = f.requests.filter(request => ["/tool/ping", "/tool/traceroute"].includes(request.path)) + assert.equal(commands.length, 2) + assert(commands.every(request => request.serverId === 1 && (request.body as { address: string })?.address === probe.destination && (request.body as { "src-address": string })["src-address"] === probe.source)) + assert(f.requests.some(request => request.path === "/interface/get")); assert(f.requests.some(request => request.path === "/ip/address/get")); assert(f.requests.some(request => request.path === "/ip/vrf/get")) + assert((snapshot.budget as { calls: number }).calls > 8) +}) + +test("reserved session pins the original A tuple and samples A after inverse under the same budget", async () => { + const probe = { key: "b", serverId: 1, destination: "198.51.100.3", source: "192.0.2.1", interface: "wan", probeVrf: "probe" } + for (const drift of [false, true]) { + const f = await fixture(true), session = await f.create({ ...f.options, manifest: { servers: [1], probes: [probe] } }) + const baseline = await session.readSnapshot("before") + const baselineKeys = (baseline.probes as { key: string }[]).map(row => row.key) + assert.deepEqual(Array.from(baselineKeys), ["b", "a"]) + for (let round = 0; round < 2; round++) { + const forward = await session.readSnapshot("after-forward") + assert.deepEqual(Array.from(forward.probes as { key: string }[], row => row.key), ["b"]) + } + const callsBefore = session.readBudget().calls + const sent = f.requests.filter(request => request.path === "/tool/ping").length + if (drift) { + f.changeOriginalAddress() + await assert.rejects(session.readSnapshot("after-inverse"), /Адреса исходного пути изменились/) + assert.equal(f.requests.filter(request => request.path === "/tool/ping").length, sent) + assert.equal(session.readBudget().closed, true) + } else { + const inverse = await session.readSnapshot("after-inverse") + assert.deepEqual(Array.from(inverse.probes as { key: string }[], row => row.key), ["a"]) + assert.equal((f.requests.filter(request => request.path === "/tool/ping").at(-1)?.body as { address: string }).address, "198.51.100.2") + assert.equal(inverse.securityProof, false) + } + assert(session.readBudget().calls > callsBefore) + assert.equal(session.readBudget().deadline, f.options.deadline) + assert.equal(f.stats().transports, 1) + session.close() + } +}) + +test("inverse snapshots bracket A with shared configuration reads and discard a lost recovery checkpoint", async () => { + const probe = { key: "b", serverId: 1, destination: "198.51.100.3", source: "192.0.2.1", interface: "wan", probeVrf: "probe" } + const f = await fixture(true), session = await f.create({ ...f.options, manifest: { servers: [1], probes: [probe] } }) + await session.readSnapshot("before") + await session.readSnapshot("after-forward") + await session.readSnapshot("after-forward") + const requestStart = f.requests.length + let checked = 0 + const first = await session.readConfigurationBoundInverseSnapshot(async () => { checked++ }) + const reads = f.requests.slice(requestStart).map(request => request.path) + assert.equal(reads[0], "/routing/filter/rule") + assert(reads.includes("/tool/ping")) + assert.equal(reads.at(-1), "/routing/filter/rule") + assert.equal(checked, 4) + assert.equal((first.budget as { calls: number }).calls, session.readBudget().calls) + assert.equal(first.securityProof, false) + await assert.rejects(session.readConfigurationBoundInverseSnapshot(async () => { + if (++checked === 7) throw Error("checkpoint disappeared after native observation") + })) + assert.equal(session.readBudget().closed, true) + assert.equal(f.stats().transports, 1) +}) + +test("arbitrary candidate identities, foreign addresses and earlier conflicting VRF rows cannot dispatch probes", async () => { + const valid = { key: "a", serverId: 1, destination: "198.51.100.2", source: "192.0.2.1", interface: "wan", probeVrf: "probe" } + for (const change of ["key", "source", "destination", "interface", "vrf-order"] as const) { + const f = await fixture(), probe = { ...valid } + if (change === "key") probe.key = "foreign" + if (change === "source") probe.source = "203.0.113.1" + if (change === "destination") probe.destination = "203.0.113.2" + if (change === "interface") probe.interface = "foreign" + if (change === "vrf-order") f.wrongVrf() + if (["key", "interface"].includes(change)) await assert.rejects(f.create({ ...f.options, manifest: { servers: [1], probes: [probe] } })) + else { const session = await f.create({ ...f.options, manifest: { servers: [1], probes: [probe] } }); await assert.rejects(session.readSnapshot("before")) } + assert.equal(f.requests.filter(request => request.path.startsWith("/tool/")).length, 0) + } +}) + +test("explicit terminal zero of three sends only ping and preserves absent trace for independent parser validation", async () => { + const f = await fixture(); f.failProbe() + const session = await f.create({ ...f.options, manifest: { servers: [1], probes: [{ key: "a", serverId: 1, destination: "198.51.100.2", source: "192.0.2.1", interface: "wan", probeVrf: "probe" }] } }) + const snapshot = await session.readSnapshot("before"), probes = snapshot.probes as Record[] + assert.equal(f.requests.filter(request => request.path === "/tool/ping").length, 1) + assert.equal(f.requests.filter(request => request.path === "/tool/traceroute").length, 0) + assert(!Object.hasOwn(probes[0], "tracePayload")); assert.equal(snapshot.securityProof, false) +}) + +test("actual native transport keeps wire whitespace bytes across calls, captures limits and exposes no reset", async () => { + let requests = 0 + const transport = { request: (_options: unknown, receive: (response: EventEmitter) => void) => { + requests++; const request = new EventEmitter() as EventEmitter & { end(): void; destroy(error?: Error): void } + request.destroy = error => { if (error) queueMicrotask(() => request.emit("error", error)); request.emit("close") } + request.end = () => queueMicrotask(() => { + const response = Object.assign(new EventEmitter(), { statusCode: 200, headers: {}, destroy() {} }) + receive(response); response.emit("data", Buffer.from(" ".repeat(600000) + "[]")); response.emit("end"); request.emit("close") + }); return request + } } + const exports = await loadSource("./routing-community-context-native.ts", { "node:http": { default: transport }, "node:https": { default: transport }, "node:net": { isIP }, "./routing-network-native-destination.js": { captureRoutingNetworkDestinationPins: () => { throw Error("Unexpected pins") } }, "./security.js": { McpError: SessionError } }) + const create = exports.routeContextClients as (endpoints: unknown[], signal: undefined, limits: { deadline: number; maxCalls: number; maxBytes: number }) => Map }> & { readBudget(): { bytes: number; maxCalls: number; deadline: number } } + const limits = { deadline: Date.now() + 119000, maxCalls: 2, maxBytes: 1000000 } + const clients = create([{ id: 1, host: "inert", username: "u", password: "p" }], undefined, limits), deadline = limits.deadline + limits.deadline += 500000; limits.maxCalls = 256; limits.maxBytes = 8000000 + const first = await clients.get(1)!.get("/routing/route"); assert(Array.isArray(first) && first.length === 0) + await assert.rejects(clients.get(1)!.get("/routing/route"), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + assert.equal(clients.readBudget().bytes, 1200004); assert.equal(clients.readBudget().deadline, deadline); assert.equal(clients.readBudget().maxCalls, 2) + await assert.rejects(clients.get(1)!.get("/routing/route")); assert.equal(requests, 2) + assert(!("reset" in clients)); assert(Object.isFrozen(clients.readBudget())) +}) + +type Pin = { serverId: number; host: string; address: string; family: number } +async function destinationFixture(lookup: (host: string) => Promise<{ address: string; family: number }[]>, globals: Record = {}) { + // Loading the actual shared policy proves its dependency graph is pure: + // any attempt to import configured DB/actions/services fails this provider. + const policy = await loadSource("./server-connection-destination-policy.ts", { "node:net": { isIP }, zod: { z } }) + const exports = await loadSource("./routing-network-native-destination.ts", { + "node:dns/promises": { lookup }, "node:net": { isIP }, "./server-connection-destination-policy.js": policy, "./security.js": { McpError: SessionError }, + }, globals) + return { + resolve: exports.resolveRoutingNetworkDestinationPins as (endpoints: { id: number; host: string }[], options: { deadline: number; signal: AbortSignal; authorize(): Promise }) => Promise, + capture: exports.captureRoutingNetworkDestinationPins as (endpoints: { id: number; host: string }[], pins: Pin[]) => Map, + } +} + +test("DNS resolves once per captured authenticated source, rejects mixed unsafe answers and never falls back", async () => { + const names: string[] = [], endpoints = [{ id: 1, host: "router.example" }] + const f = await destinationFixture(async host => { names.push(host); return [{ address: "192.168.88.1", family: 4 }, { address: "10.0.0.1", family: 4 }] }) + let checks = 0 + const pins = await f.resolve(endpoints, { deadline: Date.now() + 10000, signal: new AbortController().signal, authorize: async () => { if (++checks === 1) endpoints[0].host = "changed.example" } }) + assert.equal(names.join(","), "router.example"); assert.equal(pins[0].host, "router.example"); assert.equal(pins[0].address, "10.0.0.1"); assert(Object.isFrozen(pins[0])); assert(checks >= 3) + for (const address of ["169.254.169.254", "224.0.0.1", "168.63.129.16", "100.100.100.200", "fe80::1", "ff02::1", "::ffff:a9fe:a9fe", "fd00:ec2::254"]) { + const denied = await destinationFixture(async () => [{ address: "127.0.0.1", family: 4 }, { address, family: isIP(address) }]) + await assert.rejects(denied.resolve([{ id: 1, host: "router.example" }], { deadline: Date.now() + 10000, signal: new AbortController().signal, authorize: async () => {} }), (error: { code?: string }) => error.code === "DESTINATION_DENIED") + } +}) + +test("timed out uncancellable DNS lookups retain capacity until their real settlement", async () => { + const pending: ((rows: { address: string; family: number }[]) => void)[] = [] + const f = await destinationFixture(() => new Promise(resolve => pending.push(resolve)), { + setTimeout: (work: () => void) => { queueMicrotask(work); return 1 }, clearTimeout: () => {}, + }) + const options = { deadline: Date.now() + 10000, signal: new AbortController().signal, authorize: async () => {} } + for (let index = 0; index < 16; index++) await assert.rejects(f.resolve([{ id: index + 1, host: "router.example" }], options)) + assert.equal(pending.length, 16) + await assert.rejects(f.resolve([{ id: 17, host: "router.example" }], options), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + assert.equal(pending.length, 16) + pending[0]([{ address: "127.0.0.1", family: 4 }]); await Promise.resolve(); await Promise.resolve() + await assert.rejects(f.resolve([{ id: 18, host: "router.example" }], options)); assert.equal(pending.length, 17) + for (const resolve of pending.slice(1)) resolve([{ address: "127.0.0.1", family: 4 }]) +}) + +test("pin capture refuses reduced/foreign/unsafe sets and caller lookup injection", async () => { + const f = await destinationFixture(async () => { throw Error("DNS must not run") }), endpoint = { id: 1, host: "router.example" } + const pin = { serverId: 1, host: "router.example", address: "127.0.0.1", family: 4 } + for (const pins of [[], [pin,pin], [{ ...pin, serverId: 2 }], [{ ...pin, host: "foreign.example" }], [{ ...pin, address: "169.254.169.254" }], [{ ...pin, address: "router.example" }], [{ ...pin, lookup: () => {} }]]) assert.throws(() => f.capture([endpoint], pins)) + const captured = f.capture([endpoint], [pin]); pin.address = "203.0.113.1" + assert.equal(captured.get(1)!.address, "127.0.0.1"); assert(Object.isFrozen(captured.get(1))) + assert.throws(() => f.capture([{ id: 1, host: "127.0.0.1" }], [{ ...pin, host: "127.0.0.1", address: "10.0.0.1" }])) +}) + +test("pinned transport connects only the chosen literal and preserves original HTTP Host, TLS SNI and verification", async () => { + const destination = await destinationFixture(async () => { throw Error("No transport DNS lookup") }), requests: Record[] = [] + const transport = { request: (options: Record, receive: (response: EventEmitter) => void) => { + requests.push(options) + const request = Object.assign(new EventEmitter(), { end() { queueMicrotask(() => { const response = Object.assign(new EventEmitter(), { statusCode: 200, headers: {}, destroy() {} }); receive(response); response.emit("data", Buffer.from("[]")); response.emit("end"); request.emit("close") }) }, destroy() {} }) + return request + } } + const exports = await loadSource("./routing-community-context-native.ts", { + "node:http": { default: transport }, "node:https": { default: transport }, "node:net": { isIP }, + "./routing-network-native-destination.js": { captureRoutingNetworkDestinationPins: destination.capture }, "./security.js": { McpError: SessionError }, + }) + const create = exports.routeContextClients as (endpoints: Record[], signal: undefined, limits: Record) => Map }> + const endpoint = { id: 1, host: "router.example", port: 8443, useSsl: true, verifySsl: true, username: "u", password: "p" } + const pin = { serverId: 1, host: "router.example", address: "127.0.0.1", family: 4 } + const clients = create([endpoint], undefined, { deadline: Date.now() + 10000, maxCalls: 2, maxBytes: 10000, pins: [pin] }) + endpoint.host = "changed.example"; endpoint.verifySsl = false; pin.address = "203.0.113.1" + await clients.get(1)!.get("/routing/route"); await clients.get(1)!.get("/routing/bgp/session") + assert.equal(requests.length, 2) + for (const request of requests) { + assert.equal(request.hostname, "127.0.0.1"); assert.equal(request.servername, "router.example"); assert.equal(request.rejectUnauthorized, true) + assert.equal((request.headers as { Host: string }).Host, "router.example:8443"); assert(!("lookup" in request)) + } + const ip = create([{ ...endpoint, host: "::1", port: 8443, verifySsl: true }], undefined, { deadline: Date.now() + 10000, maxCalls: 1, maxBytes: 10000, pins: [{ serverId: 1, host: "::1", address: "::1", family: 6 }] }) + await ip.get(1)!.get("/routing/route") + assert.equal((requests[2].headers as { Host: string }).Host, "[::1]:8443"); assert.equal(requests[2].servername, undefined); assert.equal(requests[2].rejectUnauthorized, true) +}) + +test("DNS cancellation retains uncancellable lookup capacity and a late answer cannot bypass revoked authority", async () => { + const pending: ((rows: { address: string; family: number }[]) => void)[] = [] + const f = await destinationFixture(() => new Promise(resolve => pending.push(resolve))) + for (let index = 0; index < 16; index++) { + const controller = new AbortController() + const rejected = assert.rejects(f.resolve([{ id: index + 1, host: "router.example" }], { deadline: Date.now() + 10000, signal: controller.signal, authorize: async () => {} })) + for (let tick = 0; pending.length <= index && tick < 30; tick++) await Promise.resolve() + assert.equal(pending.length, index + 1); controller.abort(); await rejected + } + await assert.rejects(f.resolve([{ id: 17, host: "router.example" }], { deadline: Date.now() + 10000, signal: new AbortController().signal, authorize: async () => {} }), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + for (const resolve of pending) resolve([{ address: "127.0.0.1", family: 4 }]) + let checks = 0, lookups = 0 + const revoked = await destinationFixture(async () => { lookups++; return [{ address: "127.0.0.1", family: 4 }] }) + await assert.rejects(revoked.resolve([{ id: 1, host: "router.example" }], { deadline: Date.now() + 10000, signal: new AbortController().signal, + authorize: async () => { if (++checks > 1) throw new SessionError("RESET_REQUIRED", "revoked during DNS") } }), (error: { code?: string }) => error.code === "RESET_REQUIRED") + assert.equal(lookups, 1) +}) + + +test("complete authority fences bound repeated runtime authorization while preserving every native boundary", async () => { + const f = await fixture(), session = await f.create(f.options) + const before = f.stats() + await session.readSnapshot("before") + const after = f.stats(), native = after.nativeCalls - before.nativeCalls, callbacks = after.callbacks - before.callbacks + assert.equal(native, 6) + // Two full fences and the direct dispatch guard stay fresh for every call. + // SQL/file leaves inside those fences must not recursively multiply them. + assert(callbacks >= native * 4) + assert(callbacks <= 50, "Six native reads must fit a bounded number of complete runtime revalidations") + f.afterNative(f.revoke) + await assert.rejects(session.readSnapshot("after-forward")) + assert.equal(f.stats().nativeCalls, after.nativeCalls + 1) + await assert.rejects(session.readSnapshot("after-forward")) + assert.equal(f.stats().nativeCalls, after.nativeCalls + 1) +}) diff --git a/backend/src/modules/mcp/routing-network-native-session.ts b/backend/src/modules/mcp/routing-network-native-session.ts new file mode 100644 index 00000000..30044af8 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-native-session.ts @@ -0,0 +1,450 @@ +import { isIP } from "node:net" +import { z } from "zod" +import type { QueryConfig } from "pg" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { canonicalVrfMembers } from "../../services/vrf-members.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { accessGeneration, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import { findMcpOperation } from "./registry.js" +import { assertScopedCommunityRequest } from "./routing-community-scoped-request.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { routeContextClients } from "./routing-community-context-native.js" +import { resolveRoutingNetworkDestinationPins } from "./routing-network-native-destination.js" +import { ROUTING_NATIVE_LOG_BUFFER, ROUTING_NATIVE_LOG_PATH } from "./routing-network-native-parser.js" +import { readMeasurementAddress } from "./routing-measurement-address.js" +import { readRouteCommunityContextWithSharedNativeSession, type OwnedRouteCommunityTarget } from "./routing-community-context.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { RoutingNetworkForwardRoundConfigurationContext } from "./routing-network-configuration-bridge.js" + +const sid = z.number().int().positive().safe() +const address = z.string().min(1).max(45).refine(value => isIP(value) !== 0) +const nativeName = z.string().min(1).max(128).regex(/^[^\u0000-\u001f\u007f]+$/) +const manifestSchema = z.object({ servers: z.array(sid).min(1).max(40), probes: z.array(z.object({ + key: z.string().min(1).max(128), serverId: sid, destination: address, source: address, interface: nativeName, probeVrf: nativeName, +}).strict()).max(40).default([]) }).strict() +export type RoutingNetworkNativeManifest = z.input +export type RoutingNetworkNativeStage = "before" | "after-forward" | "after-inverse" +type Row = Record +export type RoutingNetworkNativeServerEnvelope = Readonly<{ serverId: number; routerOsVersion: string; + routes: { resource: "/routing/route"; payload: Row[]; sessionsPayload: Row[]; connectionsPayload: Row[] }; + bgp: { resource: "/routing/bgp/session"; payload: Row[]; connectionsPayload: Row[] }; + bfd: { resource: "/routing/bfd/session"; payload: Row[] }; + logs: { resource: "/log"; buffer: typeof ROUTING_NATIVE_LOG_BUFFER; payload: Row[]; coverage: "unknown" } }> +export type RoutingNetworkNativeProbeEnvelope = Readonly<{ key: string; serverId: number; routerOsVersion: string; pingPayload: Row[]; tracePayload?: Row[]; + expectedHopAddresses: { serverId: number; interface: string; address: string }[]; + request: { destination: string; source: string; interface: string; probeVrf: string; count: 3; maxHops: 16 } }> +const seenSources = new Set() +const snapshotOwners = new WeakMap() + +/** Only original snapshots from one native session can bind effect diagnostics. */ +export function sameConfigurationBoundForwardRounds(baseline: object, rounds: readonly object[]): boolean { + const initial = snapshotOwners.get(baseline) + if (!initial || initial.configuration !== undefined || rounds.length !== 2) return false + const first = snapshotOwners.get(rounds[0]), second = snapshotOwners.get(rounds[1]) + return first?.owner === initial.owner && second?.owner === initial.owner && + first.round === 1 && second.round === 2 && + first.startedAt! >= initial.finishedAt && second.startedAt! >= first.finishedAt && + first.configuration !== undefined && first.configuration === second.configuration +} +declare const sharedContextBrand: unique symbol +export type RoutingNetworkSharedContext = Readonly<{ [sharedContextBrand]: true }> +declare const sharedReadBrand: unique symbol +export type RoutingNetworkSharedContextRead = Readonly<{ [sharedReadBrand]: true }> +type SharedBinding = Readonly<{ sourceOperationId: string; profile: z.infer; principal: ActionContext["principal"]; + deadline: number; contextSources: Array<{ serverId: number; proof: string }>; candidateId: string; signal: AbortSignal }> +type SharedContextResult = { scopedTopologyAvailable: boolean; before: { scopedExecutionAvailable: boolean; scopedExecutionReason: string | null; dependencyProof: string; targetRuleProof: string | null; + currentCandidateId: string | null; sourceProofs: Array<{ serverId: number; proof: string }> }; fence(): Promise; + ownedTarget: Pick | undefined } +type SharedOwner = { binding: SharedBinding; active(): void; fence(): Promise; close(): void; + read(serverId: number, path: string, body?: Record): Promise; run(): Promise } +const sharedContexts = new WeakMap() +const sharedReads = new WeakMap() +const inverseReads = new WeakMap() +function sharedOwner(context: RoutingNetworkSharedContext): SharedOwner { + const owner = sharedContexts.get(context) + if (!owner) throw fail("STATE_CONFLICT", "Unknown native shared context") + owner.active() + return owner +} +/** Metadata alone cannot create or reconstruct a shared transport owner. */ +export function inspectRoutingNetworkSharedContext(context: RoutingNetworkSharedContext): SharedBinding { return sharedOwner(context).binding } +function readOwner(context: RoutingNetworkSharedContextRead): SharedOwner { + const owner = sharedReads.get(context) + if (!owner) throw fail("STATE_CONFLICT", "Unknown or completed shared context acquisition") + owner.active(); return owner +} +/** Capability membership and exact owner identity, not a metadata comparison. */ +export function assertRoutingNetworkSharedContextReadOwner(context: RoutingNetworkSharedContext, acquisition: RoutingNetworkSharedContextRead): void { + if (readOwner(acquisition) !== sharedOwner(context)) throw fail("STATE_CONFLICT", "Configuration acquisition belongs to another native owner") +} +export function inspectRoutingNetworkSharedContextRead(context: RoutingNetworkSharedContextRead): SharedBinding { + const binding = readOwner(context).binding, candidateId = inverseReads.get(context) + return candidateId ? Object.freeze({ ...binding, candidateId }) : binding +} +export async function assertRoutingNetworkSharedContextRead(context: RoutingNetworkSharedContextRead) { await readOwner(context).fence() } +export function routingNetworkSharedContextPublicationFence(context: RoutingNetworkSharedContextRead): () => Promise { + const owner = readOwner(context) + return async () => { + try { owner.active(); await owner.fence(); owner.active() } + catch (error) { owner.close(); throw error } + } +} +/** Internal fixed resource reader. No clients, callbacks or transport providers + * can be supplied. Only a genuine process-local session token has membership. */ +export async function readRoutingNetworkSharedResource(context: RoutingNetworkSharedContextRead, serverId: number, path: string, body?: Record): Promise { + return readOwner(context).read(serverId, path, body) +} +export async function readRoutingNetworkSharedCommunityContext(context: RoutingNetworkSharedContext) { return sharedOwner(context).run() } +export function closeRoutingNetworkSharedContext(context: RoutingNetworkSharedContext) { sharedContexts.get(context)?.close() } +const configurationResources = new Set(["/routing/table", "/ip/vrf", "/system/resource", "/routing/bgp/connection", "/routing/bgp/template", "/routing/bgp/instance", + "/routing/filter/rule", "/routing/ospf/instance", "/routing/rip/instance", "/routing/isis/instance", "/routing/filter/select-rule", "/routing/bgp/vpn", + "/routing/rule", "/ip/firewall/mangle", "/ipv6/firewall/mangle", "/interface/gre", "/ip/route", "/ipv6/route", "/interface", "/ip/address", "/ipv6/address"]) +const effectiveFields = new Set(["disabled", "templates", "instance", "input.filter-nlri", "output.filter-select", "vrf", "routing-table", "afi", "input.filter", "output.filter-chain"]) +const fail = (code: string, message: string) => new McpError(code, message, 409) +function freeze(value: T): T { + if (value && typeof value === "object") { for (const child of Object.values(value)) freeze(child); Object.freeze(value) } + return value +} + +/** Unregistered internal reader, not a native-write grant or a network proof. + * The actual encrypted operation/plan supplies authority and endpoint identity; + * the runner callback is an additional fence, never an authority replacement. + * One process allows at most1024 authenticated source sessions, without reopen + * or eviction. Cross-restart budget admission requires a future durable owner. */ +export async function createRoutingNetworkNativeSession(options: { + sourceOperationId: string; context: ActionContext; authorize: () => Promise; deadline: number; manifest: RoutingNetworkNativeManifest; +}) { + const sourceOperationId = z.string().uuid().parse(options.sourceOperationId) + const manifest = freeze(manifestSchema.parse(structuredClone(options.manifest))) + if (new Set(manifest.servers).size !== manifest.servers.length || new Set(manifest.probes.map(probe => probe.key)).size !== manifest.probes.length || + manifest.probes.some(probe => !manifest.servers.includes(probe.serverId))) throw fail("INVALID_INPUT", "Неоднозначный native manifest") + // One baseline plus at most three forward and three inverse rounds. + if ((manifest.servers.length * 6 + manifest.probes.length * 2) * 7 > 256) throw fail("LIMIT_EXCEEDED", "Полный native manifest превышает бюджет256 запросов") + const principal = structuredClone(options.context.principal), connection = structuredClone(options.context.connection) + const trustedAuthorize = options.authorize, deadline = options.deadline, startedAt = Date.now() + if (typeof trustedAuthorize !== "function" || !Number.isFinite(deadline) || deadline <= startedAt || deadline > startedAt + 120000) throw fail("READ_CANCELLED", "Некорректный исходный срок native session") + const abort = new AbortController(), signal = AbortSignal.any([abort.signal, AbortSignal.timeout(Math.max(1, deadline - startedAt)), ...(options.context.signal ? [options.context.signal] : [])]) + let closed = false, rows = 0, phase = 0, tail: Promise = Promise.resolve() + const rounds = { before: 0, "after-forward": 0, "after-inverse": 0 } + const active = () => { if (closed || signal.aborted || Date.now() >= deadline) throw fail("READ_CANCELLED", "Native session закрыта или истекла") } + const guarded = async (work: () => Promise) => { + active(); await trustedAuthorize(); active() + try { return await work() } + finally { active(); await trustedAuthorize(); active() } + } + // Primitive source reads keep the immutable lifetime checks. The complete + // authority fence surrounds their batch; native dispatch retains its own + // fresh before/after authorization. Avoid recursively re-running the full + // runtime authorization around every SQL/file leaf inside that same fence. + const bounded = async (work: () => Promise) => { + active() + try { return await work() } finally { active() } + } + const currentAuthority = async () => { + const current = await bounded(() => effectiveMcpConnection(principal)) + if (current.id !== connection.id || current.owner_id !== connection.owner_id || current.generation !== connection.generation || + fingerprint(current.policy) !== fingerprint(connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(connection.owner_permissions) || + !hasPermission(current.owner_permissions, "mm:network:read") || !hasPermission(current.policy.permissions, "mm:network:read") || + manifest.servers.some(id => !current.policy.scope.serverIds.includes(id))) throw fail("RESET_REQUIRED", "Допуск native session изменён") + } + const sourceRow = async () => { + const query: QueryConfig & { query_timeout: number } = { text: "SELECT o.id,o.state,o.connection_id,o.created_at,p.id AS plan_id,p.operation_id,p.operation_version,p.approved_by,p.expires_at,s.state AS step_state,CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END AS intended_state,CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence END AS evidence FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 WHERE o.id=$1 AND o.connection_id=$2 LIMIT 2", + values: [sourceOperationId, principal.connectionId], query_timeout: Math.max(1, Math.min(10000, deadline - Date.now())) } + const result = await bounded(() => pool.query(query)) + if (result.rows.length !== 1) throw fail("NOT_FOUND", "Exact source native session недоступен") + const row = result.rows[0] + if (!["running", "applied", "partial"].includes(row.state) || !["intent", "applied", "verified"].includes(row.step_state) || row.operation_id !== "routing.community.switch" || row.operation_version !== findMcpOperation(row.operation_id)?.version || !row.approved_by || !row.evidence?.sealed || !(row.created_at instanceof Date) || !(row.expires_at instanceof Date)) throw fail("STATE_CONFLICT", "Нет совместимого подтверждённого source intent") + return row + } + try { + await guarded(async () => {}) + await currentAuthority() + const row = await sourceRow() + if (row.state !== "running" || row.step_state !== "intent") throw fail("STATE_CONFLICT", "Native session требует исходный running intent") + const evidence = JSON.parse(await guarded(() => decryptSecret(row.evidence.sealed))) as ChangeEvidence + if (evidence.steps?.length !== 1 || evidence.steps[0].kind !== "service" || evidence.steps[0].service !== "routing.community.scoped" || evidence.target.scope !== "servers") throw fail("STATE_CONFLICT", "Требуется один исходный scoped intent") + const input = freeze(structuredClone(evidence.steps[0].input)), profile = routeAiProfileSchema.parse(input.profile) + if (fingerprint(row.intended_state) !== fingerprint(redact(evidence.steps[0]))) throw fail("STATE_CONFLICT", "Исходный service intent не соответствует sealed plan") + const fullScope = [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId])])].sort((a,b) => a-b) + if (fingerprint([...manifest.servers].sort((a,b) => a-b)) !== fingerprint(fullScope) || fingerprint([...evidence.target.serverIds].sort((a,b) => a-b)) !== fingerprint(fullScope) || + fingerprint(input.principal) !== fingerprint(principal) || input.ownerId !== connection.owner_id || input.policyProof !== fingerprint(connection.policy) || input.connectionGeneration !== connection.generation) throw fail("SERVER_DENIED", "Native manifest не соответствует полному исходному scope") + await guarded(() => assertScopedCommunityRequest(input)) + const generation = await guarded(() => accessGeneration()) + if (input.generation !== generation) throw fail("RESET_REQUIRED", "Исходный access generation отозван") + const authorityExpiry = input.persistentProfileAuthority === undefined ? Infinity : Date.parse(routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority).packet.validUntil) + if (deadline > Math.min(row.created_at.getTime() + 120000, row.expires_at.getTime(), authorityExpiry, Date.parse(principal.credentialExpiresAt), connection.owner_valid_until.getTime(), Date.parse(connection.policy.expiresAt))) throw fail("PLAN_EXPIRED", "Native session расширяет исходный срок") + await guarded(() => assertPersistentScopedProfileAuthority(input, signal)) + const original = profile.autoRecovery ? profile.candidates.find(candidate => candidate.id === input.originalCandidateId) : undefined + if (profile.autoRecovery && (input.proofRevision !== 3 || !original?.controlProbe || manifest.probes.length !== 1 || + manifest.probes[0].key !== input.candidateId || original.id === input.candidateId)) { + throw fail("STATE_CONFLICT", "Исходный путь возврата отсутствует в подписанном плане") + } + const originalProbe = original ? { key: original.id, serverId: profile.serverId, interface: original.wanInterface, probeVrf: profile.probeVrf } : undefined + const baselineProbes = originalProbe ? [...manifest.probes, originalProbe] : manifest.probes + const probesFor = (stage: RoutingNetworkNativeStage) => stage === "before" ? baselineProbes : + stage === "after-inverse" && originalProbe ? [originalProbe] : manifest.probes + for (const probe of baselineProbes) { + const candidate = profile.candidates.find(candidate => candidate.id === probe.key) + if (!candidate?.controlProbe || probe.serverId !== profile.serverId || probe.interface !== candidate.wanInterface || probe.probeVrf !== profile.probeVrf || + !candidate.controlProbe.expectedHops.some(hop => hop.serverId === candidate.jumpHostId) || candidate.controlProbe.expectedHops.at(-1)?.serverId !== candidate.exitNodeId) throw fail("SERVER_DENIED", "Probe manifest не соответствует исходному candidate") + } + const fixedRoundCalls = (stage: RoutingNetworkNativeStage) => { + const probes = probesFor(stage), addressServers = new Set() + for (const probe of probes) { + const candidate = profile.candidates.find(candidate => candidate.id === probe.key)!, configured = candidate.controlProbe! + addressServers.add(profile.serverId); addressServers.add(candidate.exitNodeId) + for (const hop of configured.expectedHops) addressServers.add(hop.serverId) + } + return manifest.servers.length * 6 + probes.length * 2 + addressServers.size * 2 + (probes.length ? 1 : 0) + } + // Reject only a known mandatory seven-round footprint. Conditional /get + // fallbacks consume the same measured budget at runtime; exhaustion closes + // the session, without truncating any catalogue or claiming complete proof. + if (fixedRoundCalls("before") + 3 * (fixedRoundCalls("after-forward") + fixedRoundCalls("after-inverse")) > 256) { + throw fail("LIMIT_EXCEEDED", "Обязательный семираундовый native manifest с address catalogues превышает256 запросов") + } + if (seenSources.has(sourceOperationId)) throw fail("STATE_CONFLICT", "Source native session уже создавалась; повтор запрещён") + if (seenSources.size >= 1024) throw fail("LIMIT_EXCEEDED", "Исчерпан process lifetime native sessions") + seenSources.add(sourceOperationId) + const sourceIdentity = fingerprint({ id: row.id, createdAt: row.created_at, planId: row.plan_id, evidence: row.evidence, expiresAt: row.expires_at, intendedState: row.intended_state }) + const endpoints = await guarded(() => readRoutingEndpoints(fullScope)) + if (endpoints.length !== fullScope.length || endpoints.some(endpoint => !endpoint.enabled) || fingerprint(endpoints.map(endpoint => endpoint.id).sort((a,b) => a-b)) !== fingerprint(fullScope)) throw fail("DEVICE_UNAVAILABLE", "Полный источник native manifest недоступен") + const sources = await guarded(() => createRoutingSourceContext(endpoints)) + if (fingerprint(sources.expected) !== fingerprint(input.contextSources)) throw fail("STATE_CONFLICT", "Исходный endpoint manifest изменился") + const fence = async () => guarded(async () => { + await currentAuthority() + if (await bounded(() => accessGeneration()) !== generation) throw fail("RESET_REQUIRED", "Access generation native session изменён") + await bounded(() => assertPersistentScopedProfileAuthority(input, signal)) + const current = await sourceRow() + if (fingerprint({ id: current.id, createdAt: current.created_at, planId: current.plan_id, evidence: current.evidence, expiresAt: current.expires_at, intendedState: current.intended_state }) !== sourceIdentity) throw fail("STATE_CONFLICT", "Исходный native intent изменён") + await bounded(() => sources.assertCurrent()) + }) + const pins = await guarded(() => resolveRoutingNetworkDestinationPins(endpoints, { deadline, signal, authorize: fence })) + await fence() + const clients = routeContextClients(freeze(structuredClone(endpoints)), signal, { deadline, maxCalls: 256, maxBytes: 8 * 1048576, pins }) + const readRaw = async (serverId: number, path: string, body?: Record) => { + await fence() + const client = clients.get(serverId) + if (!client) throw fail("SERVER_DENIED", "Shared native read вне исходного scope") + const timeout = Math.max(1, Math.min(10000, deadline - Date.now())) + const raw = await guarded(() => body ? client.post(path, body, timeout) : client.get(path, timeout)) + await fence() + const payload = Array.isArray(raw) ? raw : (path === "/system/resource" || path.endsWith("/get")) && raw && typeof raw === "object" ? [raw] : null + if (!payload || payload.length > 1000 || payload.some(value => !value || typeof value !== "object" || Array.isArray(value))) throw fail("INVALID_NATIVE_RESPONSE", "Native response не является bounded каталогом") + rows += payload.length + if (rows > 10000) throw fail("LIMIT_EXCEEDED", "Превышен общий бюджет10000 native rows") + return raw + } + const read = async (serverId: number, path: string, body?: Record) => { + const raw = await readRaw(serverId, path, body) + return (Array.isArray(raw) ? raw : [raw]) as Row[] + } + const sharedContext = Object.freeze({}) as RoutingNetworkSharedContext + const close = () => { closed = true; abort.abort(); sharedContexts.delete(sharedContext) } + sharedContexts.set(sharedContext, { + binding: Object.freeze({ sourceOperationId, profile: freeze(structuredClone(profile)), principal: freeze(structuredClone(principal)), deadline, + contextSources: freeze(structuredClone(input.contextSources as Array<{ serverId: number; proof: string }>)), candidateId: String(input.candidateId), signal }), + active, fence, close, + read: async (serverId, path, body) => { + try { + const resource = body === undefined ? path : path.endsWith("/get") ? path.slice(0, -4) : "" + if (!configurationResources.has(resource) || body !== undefined && (Object.keys(body).length !== 2 || + !Object.hasOwn(body, ".id") || !Object.hasOwn(body, "value-name") || !/^\*[a-z0-9]+$/i.test(body[".id"]) || !effectiveFields.has(body["value-name"]))) throw fail("PERMISSION_DENIED", "Shared context допускает только fixed read resources и effective fields") + return await readRaw(serverId, path, body) + } catch (error) { close(); throw error } + }, + run: () => { + const run = tail.then(async () => { + const acquisition = Object.freeze({}) as RoutingNetworkSharedContextRead + try { + active(); sharedReads.set(acquisition, sharedOwner(sharedContext)) + return await readRouteCommunityContextWithSharedNativeSession(acquisition) + } + catch (error) { close(); throw error } + finally { sharedReads.delete(acquisition) } + }) + tail = run.catch(() => {}) + return run + }, + }) + let originalProbeIdentity: string | undefined + const acquireSnapshot = async (stage: RoutingNetworkNativeStage) => { + try { + active() + if (stage === "before" && phase === 0) { rounds.before++; phase = 1 } + else if (stage === "after-forward" && phase === 1 && rounds[stage] < 3) rounds[stage]++ + else if (stage === "after-inverse" && phase >= 1 && rounds["after-forward"] >= 2 && rounds[stage] < 3) { rounds[stage]++; phase = 2 } + else throw fail("STATE_CONFLICT", "Native round нельзя повторить сверх лимита или переставить") + const startedAt = new Date(Date.now()).toISOString() + const servers: RoutingNetworkNativeServerEnvelope[] = [], probes: RoutingNetworkNativeProbeEnvelope[] = [] + const catalogues = new Map() + const catalogue = async (serverId: number, path: string) => { + const key = serverId + ":" + path + if (!catalogues.has(key)) catalogues.set(key, await read(serverId, path)) + return catalogues.get(key)! + } + const disabledValues = new Map() + const readDisabled = async (serverId: number, path: string, id: string) => { + if (!/^\*[a-z0-9]+$/i.test(id)) throw fail("INVALID_NATIVE_RESPONSE", "Не подтверждён native ID probe dependency") + const key = serverId + path + id + if (!disabledValues.has(key)) { + const values = await read(serverId, path + "/get", { ".id": id, "value-name": "disabled" }) + if (values.length !== 1) throw fail("INVALID_NATIVE_RESPONSE", "Не подтверждён disabled probe dependency") + disabledValues.set(key, values[0]) + } + return disabledValues.get(key) + } + const enabled = async (serverId: number, path: string, row: Row) => { + const [observed] = await readOspfEffectiveDisabled([row], id => readDisabled(serverId, path, id)) + if (![false, "false", "no"].includes(observed.disabled as string | boolean) || [true, "true", "yes"].includes(observed.invalid as string | boolean)) throw fail("MEASUREMENT_UNKNOWN", "Probe dependency не подтверждена включённой") + } + const addressOf = async (serverId: number, iface: string, selected?: string) => { + const interfaces = (await catalogue(serverId, "/interface")).filter(row => row.name === iface) + if (interfaces.length !== 1) throw fail("MEASUREMENT_UNKNOWN", "Не подтверждён exact probe interface") + await enabled(serverId, "/interface", interfaces[0]) + const path = profile.family === "ipv4" ? "/ip/address" : "/ipv6/address" + const selectedAddress = await readMeasurementAddress(await catalogue(serverId, path), { dstInterface: iface, family: profile.family, dstAddress: selected }, id => readDisabled(serverId, path, id)) + if (selectedAddress.linkLocal) throw fail("UNSUPPORTED_CAPABILITY", "Link-local probe требует отдельного контракта") + return selectedAddress.address + } + for (const serverId of manifest.servers) { + const resource = await read(serverId, "/system/resource"), version = resource.length === 1 ? resource[0].version : null + if (typeof version !== "string" || version.length > 128) throw fail("INVALID_NATIVE_RESPONSE", "RouterOS version не подтверждена") + const routes = await read(serverId, "/routing/route"), connectionsPayload = await read(serverId, "/routing/bgp/connection") + const bgp = await read(serverId, "/routing/bgp/session"), bfd = await read(serverId, "/routing/bfd/session") + const logs = await read(serverId, ROUTING_NATIVE_LOG_PATH) + servers.push({ serverId, routerOsVersion: version, routes: { resource: "/routing/route", payload: routes, sessionsPayload: bgp, connectionsPayload }, bgp: { resource: "/routing/bgp/session", payload: bgp, connectionsPayload }, bfd: { resource: "/routing/bfd/session", payload: bfd }, logs: { resource: "/log", buffer: ROUTING_NATIVE_LOG_BUFFER, payload: logs, coverage: "unknown" } }) + } + for (const probe of probesFor(stage)) { + const candidate = profile.candidates.find(candidate => candidate.id === probe.key)!, configured = candidate.controlProbe! + const interfaceNames = new Set((await catalogue(profile.serverId, "/interface")).map(row => row.name)) + let owner: unknown = null + const vrfs = await catalogue(profile.serverId, "/ip/vrf") + for (const row of vrfs) { + const members = canonicalVrfMembers(row.interfaces).split(",") + if ([true, "true", "yes"].includes(row.disabled as string | boolean)) continue + await enabled(profile.serverId, "/ip/vrf", row) + if (members.some(member => !["all", "none"].includes(member) && !interfaceNames.has(member))) throw fail("UNSUPPORTED_CAPABILITY", "VRF interface-list требует отдельного resolver") + if (members.includes("all") || members.includes(candidate.wanInterface)) { owner = row.name; break } + } + if (owner !== profile.probeVrf) throw fail("MEASUREMENT_UNKNOWN", "Probe WAN не принадлежит exact ordered VRF") + const source = await addressOf(profile.serverId, candidate.wanInterface, configured.sourceAddress) + const destination = await addressOf(candidate.exitNodeId, configured.targetInterface, configured.targetAddress) + if ("source" in probe && "destination" in probe && (source !== probe.source || destination !== probe.destination)) { + throw fail("STATE_CONFLICT", "Probe manifest addresses не соответствуют native ownership") + } + const expectedHopAddresses = [] + for (const hop of configured.expectedHops) expectedHopAddresses.push({ serverId: hop.serverId, interface: hop.interface, address: await addressOf(hop.serverId, hop.interface, hop.address) }) + const request = { destination, source, interface: probe.interface, probeVrf: probe.probeVrf, count: 3 as const, maxHops: 16 as const } + if (probe === originalProbe) { + const identity = fingerprint({ request, expectedHopAddresses }) + if (stage === "before") originalProbeIdentity = identity + else if (originalProbeIdentity !== identity) throw fail("STATE_CONFLICT", "Адреса исходного пути изменились после возврата") + } + const routerOsVersion = servers.find(server => server.serverId === probe.serverId)!.routerOsVersion + const pingRequest = { address: destination, "src-address": source, interface: probe.interface, vrf: probe.probeVrf, count: "3" } + const pingPayload = await read(probe.serverId, "/tool/ping", pingRequest) + // This is a command-selection check, never a failure proof. The + // parser must still validate every raw counter/row independently. + const terminal = pingPayload.at(-1), loss = terminal?.["packet-loss"] + const zeroReceived = [3,"3"].includes(terminal?.sent as string | number) && [0,"0"].includes(terminal?.received as string | number) && + (loss === undefined || loss === 100 || typeof loss === "string" && /^100(?:\.0+)?%?$/.test(loss)) + const tracePayload = zeroReceived ? undefined : await read(probe.serverId, "/tool/traceroute", { ...pingRequest, "max-hops": "16" }) + probes.push({ key: probe.key, serverId: probe.serverId, routerOsVersion, pingPayload, ...(tracePayload === undefined ? {} : { tracePayload }), expectedHopAddresses, request }) + } + await fence() + const observedAt = new Date(Date.now()).toISOString() + const snapshot = freeze({ sourceOperationId, stage, round: rounds[stage], startedAt, observedAt, servers, probes, securityProof: false as const, logCoverage: "unknown" as const, budget: { ...clients.readBudget(), rows } }) + if (stage === "before") snapshotOwners.set(snapshot, { owner: sharedContext, finishedAt: Date.parse(observedAt) }) + return snapshot + } catch (error) { + close() + if (error instanceof McpError) throw error + throw fail("STATE_UNAVAILABLE", "Native read session не подтверждена; повтор запрещён") + } + } + const readSnapshot = (stage: RoutingNetworkNativeStage) => { + const run = tail.then(() => acquireSnapshot(stage)) + tail = run.catch(() => {}) + return run + } + const readConfigurationBoundForwardSnapshot = (configuration: RoutingNetworkForwardRoundConfigurationContext) => { + const run = tail.then(async () => { + const acquisition = Object.freeze({}) as RoutingNetworkSharedContextRead + try { + active() + sharedReads.set(acquisition, sharedOwner(sharedContext)) + // Fixed lazy import avoids a bridge/session initialization cycle. + // No caller can supply a verifier, reader, proof JSON or client. + const { readRoutingNetworkForwardRoundConfigurationContext } = await import("./routing-network-configuration-bridge.js") + const before = await readRoutingNetworkForwardRoundConfigurationContext(configuration, acquisition) + const snapshot = await acquireSnapshot("after-forward") + const after = await readRoutingNetworkForwardRoundConfigurationContext(configuration, acquisition) + await fence() + const capturedAt = Date.now() + const ordered = [before.checkpointObservedAt, before.observedAt, snapshot.startedAt, + snapshot.observedAt, after.observedAt].map(value => Date.parse(value)) + ordered.push(capturedAt) + if (before.operationId !== sourceOperationId || after.operationId !== sourceOperationId || before.phase !== "forward" || after.phase !== "forward" || + !Number.isFinite(before.deadline) || before.deadline > deadline || before.deadline !== after.deadline || + !before.sharedNetworkBudgetVerified || !after.sharedNetworkBudgetVerified || + before.checkpointObservedAt !== after.checkpointObservedAt || before.checkpointProof !== after.checkpointProof || + ordered.some((time, index) => !Number.isFinite(time) || time >= before.deadline || index > 0 && time < ordered[index - 1]) || + fingerprint(before.configuration) !== fingerprint(after.configuration)) throw fail("STATE_CONFLICT", "Forward-round configuration changed") + // Both readbacks consume this owner's measured transport and row + // budget. Preserve raw observation times; publication is later. + const bound = freeze({ ...snapshot, configurationReadbacks: { before, after }, capturedAt: new Date(capturedAt).toISOString(), + budget: { ...clients.readBudget(), rows }, securityProof: false as const, forwardingVerified: false as const }) + snapshotOwners.set(bound, { owner: sharedContext, round: bound.round, startedAt: ordered[1], finishedAt: capturedAt, + configuration: fingerprint({ deadline: before.deadline, checkpointProof: before.checkpointProof, configuration: before.configuration }) }) + return bound + } catch (error) { close(); throw error } + finally { sharedReads.delete(acquisition) } + }) + tail = run.catch(() => {}) + return run + } + const readConfigurationBoundInverseSnapshot = (assertCheckpoint: () => Promise) => { + const run = tail.then(async () => { + const acquisition = Object.freeze({}) as RoutingNetworkSharedContextRead + try { + active() + if (!originalProbe || !originalProbeIdentity || rounds["after-forward"] < 2 || rounds["after-inverse"] >= 3) { + throw fail("STATE_CONFLICT", "Исходный раунд A для возврата не подтверждён") + } + sharedReads.set(acquisition, sharedOwner(sharedContext)) + inverseReads.set(acquisition, originalProbe.key) + const body = await guarded(() => decryptSecret(String(input.originalRuleCiphertext))) + const assertOriginalConfiguration = async () => { + await guarded(assertCheckpoint) + const current = await readRouteCommunityContextWithSharedNativeSession(acquisition) + if (!current.scopedTopologyAvailable || current.before.currentCandidateId !== originalProbe.key || + current.before.dependencyProof !== input.dependencyProof || current.ownedTarget?.nativeId !== input.nativeId || + !current.ownedTarget?.matchesRuleProof(body, input.originalTargetProof)) { + throw fail("STATE_CONFLICT", "Конфигурация A изменилась во время обратного раунда") + } + await current.fence() + await guarded(assertCheckpoint) + } + await assertOriginalConfiguration() + const snapshot = await acquireSnapshot("after-inverse") + await assertOriginalConfiguration() + return freeze({ ...snapshot, budget: { ...clients.readBudget(), rows } }) + } catch (error) { close(); throw error } + finally { inverseReads.delete(acquisition); sharedReads.delete(acquisition) } + }) + tail = run.catch(() => {}) + return run + } + return Object.freeze({ readSnapshot, readConfigurationBoundForwardSnapshot, readConfigurationBoundInverseSnapshot, + sharedContext, close, readBudget: () => Object.freeze({ ...clients.readBudget(), rows, closed }) }) + } catch (error) { + closed = true; abort.abort() + if (error instanceof McpError) throw error + throw fail("STATE_UNAVAILABLE", "Исходный native session не подтверждён") + } +} diff --git a/backend/src/modules/mcp/routing-network-native-tls.test.ts b/backend/src/modules/mcp/routing-network-native-tls.test.ts new file mode 100644 index 00000000..cd7f4829 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-native-tls.test.ts @@ -0,0 +1,75 @@ +import assert from "node:assert/strict" +import http from "node:http" +import https from "node:https" +import { execFileSync } from "node:child_process" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, isAbsolute, join, relative, resolve } from "node:path" +import type { TLSSocket } from "node:tls" +import { randomUUID } from "node:crypto" +import { routeContextClients } from "./routing-community-context-native.js" +import { resolveRoutingNetworkDestinationPins } from "./routing-network-native-destination.js" +import type { RoutingEndpoint } from "./routing-observation-source.js" + +// Actual local TLS; trust only this freshly generated test certificate for the +// exact bound fixture. No configured SQL pool, public endpoint or user key. +const openssl = process.env.MMAPP_TEST_OPENSSL +assert(openssl, "Set MMAPP_TEST_OPENSSL to an installed OpenSSL executable") +const directory = await mkdtemp(join(tmpdir(), "mm-mcp-tls-")) +let server: https.Server | undefined, trustedAgent: https.Agent | undefined, untrustedAgent: https.Agent | undefined +const originalRequest = https.request +try { + const keyFile = join(directory, "fixture.key"), certFile = join(directory, "fixture.crt") + execFileSync(openssl, ["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-subj", "/CN=localhost", "-addext", "subjectAltName=DNS:localhost", "-days", "2", "-keyout", keyFile, "-out", certFile], { stdio: "ignore", timeout: 20000 }) + const cert = await readFile(certFile), key = await readFile(keyFile) + const seen: { host: string; sni: string | false | undefined }[] = [] + const payload = JSON.stringify([{ version: "7.26" }]) + server = https.createServer({ key, cert }, (request, response) => { + assert.equal(request.method, "GET"); assert.equal(request.url, "/rest/system/resource") + assert.equal(request.headers.authorization, "Basic " + Buffer.from("fixture:inert-only").toString("base64")) + seen.push({ host: request.headers.host!, sni: (request.socket as TLSSocket).servername }) + response.setHeader("Content-Type", "application/json"); response.end(payload) + }) + server.on("tlsClientError", () => {}) + await new Promise(done => server!.listen(0, "127.0.0.1", done)) + const address = server.address(); assert(address && typeof address === "object" && address.address === "127.0.0.1") + trustedAgent = new https.Agent({ keepAlive: true, maxSockets: 2, maxTotalSockets: 2, ca: cert }) + untrustedAgent = new https.Agent({ keepAlive: false, ca: [] }) + let trust = true + https.request = ((...args: unknown[]) => { + const options = args[0] as https.RequestOptions + assert(options && options.hostname === "127.0.0.1" && Number(options.port) === address.port, "TLS fixture must never connect outside its owned loopback endpoint") + args[0] = { ...options, agent: trust ? trustedAgent : untrustedAgent } + return Reflect.apply(originalRequest, https, args) as http.ClientRequest + }) as typeof https.request + const endpoint: RoutingEndpoint = { id: 1, host: "localhost", port: address.port, useSsl: true, verifySsl: true, username: "fixture", password: "inert-only", enabled: true, routingEndpointEpoch: randomUUID() } + const deadline = Date.now() + 90000, abort = new AbortController() + let fences = 0 + const resolved = await resolveRoutingNetworkDestinationPins([endpoint], { deadline, signal: abort.signal, authorize: async () => { fences++ } }) + assert.equal(resolved.length, 1); assert.equal(resolved[0].address, "127.0.0.1"); assert.equal(resolved[0].host, "localhost") + const pins = resolved.map(pin => ({ ...pin })) + const clients = routeContextClients([endpoint], abort.signal, { deadline, maxCalls: 2, maxBytes: 1024, pins }) + pins[0].host = "outer-mutated.invalid"; pins[0].address = "127.0.0.2"; endpoint.host = "outer-mutated.invalid"; endpoint.password = "changed" + assert.deepEqual(await clients.get(1)!.get("/system/resource"), [{ version: "7.26" }]) + assert.deepEqual(await clients.get(1)!.get("/system/resource"), [{ version: "7.26" }]) + assert.deepEqual(seen, [{ host: `localhost:${address.port}`, sni: "localhost" }, { host: `localhost:${address.port}`, sni: "localhost" }]) + assert.equal(clients.readBudget().bytes, 2 * Buffer.byteLength(payload)); assert.equal(clients.readBudget().deadline, deadline) + await assert.rejects(clients.get(1)!.get("/system/resource")); assert.equal(seen.length, 2) + assert(fences >= 2) + const originalEndpoint = { ...endpoint, host: "localhost", password: "inert-only" } + const wrongName = { ...originalEndpoint, host: "127.0.0.1" } + const wrongClient = routeContextClients([wrongName], undefined, { deadline, maxCalls: 1, maxBytes: 1024, pins: [{ serverId: 1, host: "127.0.0.1", address: "127.0.0.1", family: 4 }] }) + await assert.rejects(wrongClient.get(1)!.get("/system/resource"), (error: unknown) => !!error && typeof error === "object" && Reflect.get(error, "code") === "ERR_TLS_CERT_ALTNAME_INVALID") + assert.equal(seen.length, 2, "A valid trusted certificate for another hostname must fail before HTTP/authentication") + trust = false + const untrusted = routeContextClients([originalEndpoint], undefined, { deadline, maxCalls: 1, maxBytes: 1024, pins: resolved }) + await assert.rejects(untrusted.get(1)!.get("/system/resource"), (error: unknown) => !!error && typeof error === "object" && ["DEPTH_ZERO_SELF_SIGNED_CERT", "SELF_SIGNED_CERT_IN_CHAIN"].includes(String(Reflect.get(error, "code")))) + assert.equal(seen.length, 2) + console.log("Actual native TLS fixture: localhost DNS pin, authenticated HTTPS, preserved Host/SNI, CA and hostname verification, captured immutable pin, actual wire budget and pre-HTTP request cap; no SQL/CHR/public network") +} finally { + https.request = originalRequest; trustedAgent?.destroy(); untrustedAgent?.destroy() + if (server) { server.closeAllConnections(); await new Promise(done => server!.close(() => done())) } + const within = relative(resolve(tmpdir()), resolve(directory)) + assert(within && !within.startsWith("..") && !isAbsolute(within) && basename(directory).startsWith("mm-mcp-tls-"), "Unsafe TLS fixture cleanup path") + await rm(directory, { recursive: true, force: true }) +} diff --git a/backend/src/modules/mcp/routing-network-native.sql.test.ts b/backend/src/modules/mcp/routing-network-native.sql.test.ts new file mode 100644 index 00000000..97b0373b --- /dev/null +++ b/backend/src/modules/mcp/routing-network-native.sql.test.ts @@ -0,0 +1,320 @@ +import assert from "node:assert/strict" +import http from "node:http" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" + +// Guard before imports which configure the database, authority or private keys. +assert.equal(process.env.MMAPP_OWN_ROUTING_NETWORK_NATIVE, "true") +const url = new URL(process.env.DATABASE_URL ?? "") +assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") +assert(["postgres:", "postgresql:"].includes(url.protocol)) +assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { managedComment } = await import("../../managed-markers.js") +const { installLoopbackFixtureAgent } = await import("../../test/loopback-http-agent.js") +const { mcpConnectionPolicySchema } = await import("@mmapp/contracts/mcp") +const { routeAiProfileSchema } = await import("@mmapp/contracts/mcp-routing") +const security = await import("./security.js") +const { effectiveMcpConnection, authenticateMcpToken } = await import("./store.js") +const { invokeMcpTool } = await import("./runtime.js") +const { createRoutingSourceContext, readRoutingEndpoints } = await import("./routing-observation-source.js") +const { resolveRoutingProfileAuthority } = await import("./routing-profile-authority.js") +await import("./actions.js") +const { findMcpOperation } = await import("./registry.js") +const { createRoutingNetworkNativeSession } = await import("./routing-network-native-session.js") +const { captureRoutingNetworkNativeParser } = await import("./routing-network-native-parser.js") +const { captureRoutingNetworkObservationAssembly } = await import("./routing-network-observation-assembly.js") +const { readRoutingNetworkScopedCommunityContext } = await import("./routing-network-scoped-context.js") +type Options = Parameters[0] + +type Mode = "topology-unknown" | "normal" | "zero-ping" | "redirect" | "auth" | "malformed" | "oversized" | "rows" | "total-bytes" | "total-rows" +let mode: Mode = "normal", onRequest: (() => Promise | void) | undefined +let logRows: Record[] = [] +const requests: { method: string; path: string; body: Record }[] = [] +let responseBytes = 0, writes = 0, sinks = 0 +const profiles: string[] = [], connections: string[] = [] +let serverId: number | undefined +const addressRows = [ + { ".id": "*a1", interface: "wan1", address: "198.51.100.1/24", disabled: "false", invalid: "false", dynamic: "false" }, + { ".id": "*a2", interface: "exit1", address: "203.0.113.9/24", disabled: "false", invalid: "false", dynamic: "false" }, +] +const collections = (path: string): Record[] => { + if (path === "/system/resource") return [{ version: "7.26", uptime: "1d", "board-name": "CHR" }] + if (path === "/interface") return ["wan1", "exit1"].map((name, i) => ({ ".id": "*i" + i, name, type: "ether", disabled: "false", running: "true" })) + if (path === "/ip/address") return addressRows + if (path === "/ipv6/address") return [] + if (path === "/ip/vrf") return [{ ".id": "*v1", name: "probe", interfaces: "wan1", disabled: "false" }, { ".id": "*v2", name: "main", interfaces: "all", disabled: "false" }] + if (path === "/routing/route") return [{ ".id": "*r1", afi: "ip", "dst-address": "203.0.113.0/24", "routing-table": "tenant", gateway: "192.0.2.1@main", "immediate-gw": "192.0.2.1%wan1", active: "true", dynamic: "true", contribution: "active", bgp: "true", "bgp.session": "*s1" }] + if (path === "/routing/bgp/connection") return [{ ".id": "*c1", name: "peer1", instance: "default", "remote.address": "192.0.2.1", "local.address": "192.0.2.2", "remote.as": "65001", afi: "ip", templates: "", vrf: "main", "routing-table": "tenant", disabled: "false" }] + if (path === "/routing/bgp/session") return [{ ".id": "*s1", name: "peer1-1", instance: "default", "remote.address": "192.0.2.1@main", "local.address": "192.0.2.2@main", "remote.as": "65001", "local.afi": "ip", "remote.afi": "ip", established: "true", "routing-table": "tenant" }] + if (path === "/routing/bfd/session") return [{ ".id": "*b1", "local-address": "192.0.2.2", "remote-address": "192.0.2.1", interface: "wan1", vrf: "main", state: "up" }] + if (path === "/log") return logRows + if (path === "/routing/table") return ["main", "tenant", "probe"].map((name, i) => ({ ".id": "*t" + i, name, fib: "true" })) + if (path === "/routing/bgp/instance") return [{ ".id": "*i1", name: "default", vrf: "main", "routing-table": "main", disabled: "false" }] + if (["/routing/bgp/template", "/routing/filter/rule", "/interface/gre", "/ipv6/route"].includes(path)) return [] + if (["/routing/ospf/instance", "/routing/rip/instance", "/routing/isis/instance", "/routing/filter/select-rule", "/routing/bgp/vpn", "/routing/rule", "/ip/firewall/mangle", "/ipv6/firewall/mangle"].includes(path)) return [] + if (path === "/ip/route") return [1, 2].map(i => ({ ".id": "*r" + i, "dst-address": "192.0.2." + i + "/32", "routing-table": "main", gateway: "198.51.100.254", disabled: "false", comment: managedComment("owned-read-fixture") })) + throw Error("Unapproved fixture path") +} +const router = http.createServer(async (req, res) => { + try { + const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(Buffer.from(chunk)) + const body = chunks.length ? JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record : {} + const url = new URL(req.url!, "http://127.0.0.1") + const path = url.pathname.replace(/^\/rest/, "") + if (path === "/log") assert.deepEqual([...url.searchParams], [["buffer", "memory"], [".proplist", ".id,buffer,time,topics,message"]]) + requests.push({ method: req.method!, path, body }) + assert.equal(req.headers.authorization, "Basic " + Buffer.from("fixture:inert-only").toString("base64")) + const callback = onRequest; onRequest = undefined; await callback?.() + if (path === "/sink") { sinks++; res.end("[]"); return } + if (mode === "redirect") { res.writeHead(302, { Location: "/rest/sink" }); res.end(); return } + if (mode === "auth") { res.writeHead(401); res.end("fixture denied"); return } + if (mode === "malformed") { res.end("["); return } + if (mode === "oversized") { res.setHeader("Content-Type", "application/json"); res.write('[{"version":"7.26","padding":"'); res.end("路".repeat(400000) + '"}]'); return } + let payload: unknown + if (req.method === "GET") payload = collections(path) + else if (req.method === "POST" && path.endsWith("/get")) { + const field = body["value-name"] + payload = { ret: field === "disabled" ? "false" : field === "instance" ? "default" : field === "vrf" ? "main" : field === "routing-table" ? (path === "/routing/bgp/instance/get" ? "main" : "tenant") : field === "afi" ? "ip" : "" } + } + else if (req.method === "POST" && path === "/tool/ping") { + assert.equal(body.address, "203.0.113.9"); assert.equal(body["src-address"], "198.51.100.1"); assert.equal(body.interface, "wan1"); assert.equal(body.vrf, "probe"); assert.equal(body.count, "3") + payload = mode === "zero-ping" ? [{ sent: "3", received: "0", "packet-loss": "100%" }] : [{ sent: "3", received: "3", "packet-loss": "0%", "avg-rtt": "1ms" }] + } else if (req.method === "POST" && path === "/tool/traceroute") { + assert.equal(body.address, "203.0.113.9"); assert.equal(body.interface, "wan1"); assert.equal(body.vrf, "probe"); assert.equal(body["max-hops"], "16") + payload = [{ ".section": "0", address: "203.0.113.9", sent: "3", loss: "0", last: "1ms" }] + } else { writes++; throw Error("Unexpected native mutation") } + if (mode === "topology-unknown" && path === "/routing/route") payload = [{ ...collections(path)[0], "immediate-gw": "17", "bgp.peer-cache-id": "*s1" }] + if (mode === "rows") payload = Array.from({ length: 1001 }, () => ({ version: "7.26" })) + if (mode === "total-rows" && path !== "/system/resource") payload = Array.from({ length: 1000 }, () => ({ value: "fixture" })) + if (mode === "total-bytes") payload = [{ version: "7.26", padding: "x".repeat(600000) }] + const encoded = JSON.stringify(payload); responseBytes += Buffer.byteLength(encoded) + res.setHeader("Content-Type", "application/json"); res.end(encoded) + } catch { if (!res.headersSent) res.writeHead(500); res.end("fixture failed") } +}) +await new Promise(done => router.listen(0, "127.0.0.1", done)) +const bound = router.address(); assert(bound && typeof bound === "object") +const restoreAgent = installLoopbackFixtureAgent(router) +const cases: string[] = [] +const run = async (name: string, work: () => Promise) => { mode = "normal"; logRows = []; onRequest = undefined; await work(); cases.push(name) } + +async function source(withProbes = true) { + assert(serverId) + const connectionId = randomUUID(), credentialId = randomUUID(), profileId = randomUUID(), ownerId = "native-reader-" + randomUUID() + const now = Date.now(), expiresAt = new Date(now + 600000).toISOString(), generation = await security.accessGeneration() + const permissions = ["mm:network:read", "mm:network:write", "mm:filters:write"] + const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt, + scope: { serverIds: [serverId], operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "operations.recover"], allowGlobal: false }, + limits: { callsPerMinute: 1000, mutationsPerHour: 10, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } }) + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned native reader',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), generation, expiresAt, new Date(now - 1000)]) + connections.push(connectionId) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), expiresAt]) + const profile = routeAiProfileSchema.parse({ name: "Owned native reader", serverId, community: "65000:10", family: "ipv4", table: "tenant", probeVrf: "probe", autoRecovery: true, + candidates: ["a", "b"].map((id, i) => ({ id, jumpHostId: serverId!, exitNodeId: serverId!, wanInterface: "wan1", downstreamInterface: "exit1", gateway: "192.0.2." + (i + 1), + gatewayRef: { resource: "ipv4_route", id: "*r" + (i + 1) }, controlProbe: { targetInterface: "exit1", targetAddress: "203.0.113.9", sourceAddress: "198.51.100.1", expectedHops: [{ serverId: serverId!, interface: "exit1", address: "203.0.113.9" }] } })) }) + await pool.query("INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version) VALUES($1,$2,$3::jsonb,true,$4,$5,$6,1)", [profileId, connectionId, JSON.stringify(profile), expiresAt, credentialId, generation]) + profiles.push(profileId) + const principal = { connectionId, credentialId, credentialExpiresAt: expiresAt } + const contextSources = (await createRoutingSourceContext(await readRoutingEndpoints([serverId]))).expected + const authority = await resolveRoutingProfileAuthority({ profileId, expectedVersion: 1, profile, principal, ownerId, transport: { contextSources } }) + const input: Record = { serverId, profile, principal, ownerId, candidateId: "b", originalCandidateId: "a", persistentProfileAuthority: authority, contextSources, + policyProof: security.fingerprint(policy), connectionGeneration: generation, generation, receiptId: randomUUID(), proofRevision: 3, + nativeId: "*r1", dependencyProof: "d".repeat(64), originalTargetProof: "a".repeat(64), desiredTargetProof: "b".repeat(64), + originalRuleCiphertext: await security.encryptSecret("synthetic source data only"), desiredRuleCiphertext: await security.encryptSecret("synthetic desired data only"), + rules: [{ community: profile.community, gateway: "192.0.2.2" }], beforeRules: [{ community: profile.community, gateway: "192.0.2.1" }] } + input.requestProof = await security.privateSecretFingerprint(security.fingerprint(input), "route-ai-scoped-request-v3") + const evidence = { target: { scope: "servers", serverIds: [serverId] }, objects: [], snapshots: [], before: input.beforeRules, steps: [{ kind: "service", service: "routing.community.scoped", input }], warnings: [], requiresHuman: true, mutationCost: 2 } + const planId = randomUUID(), operationId = randomUUID() + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by,approved_at) VALUES($1,$2,'routing.community.switch',$3,1,$4,$5::jsonb,$6::jsonb,'executing',$7,$8,now())", [planId, connectionId, findMcpOperation("routing.community.switch")!.version, generation, JSON.stringify({ sealed: await security.encryptSecret("{}") }), JSON.stringify({ sealed: await security.encryptSecret(JSON.stringify(evidence)) }), expiresAt, ownerId]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'running')", [operationId, planId, connectionId, randomUUID()]) + await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", [operationId, JSON.stringify(security.redact({ kind: "service", service: "routing.community.scoped", input }))]) + let fences = 0 + const options: Options = { sourceOperationId: operationId, context: { principal, connection: await effectiveMcpConnection(principal) }, authorize: async () => { fences++ }, deadline: Date.now() + 90000, + manifest: { servers: [serverId], probes: withProbes ? [{ key: "a", serverId, destination: "203.0.113.9", source: "198.51.100.1", interface: "wan1", probeVrf: "probe" }] : [] } } + return { options, operationId, profileId, planId, evidence, fences: () => fences } +} + +try { + await applySqlMigrations(pool) + // Other genuine phases may have already created authenticated checkpoints in + // this owned database. Read-only acceptance must preserve every existing row + // and add none; raw SQL JSON also preserves timestamp microseconds that + // conversion to a JavaScript Date would truncate. + const checkpointBaselineRows = (await pool.query("SELECT to_jsonb(c)::text AS inventory_row FROM mcp_operation_checkpoints c ORDER BY operation_id,step_index")).rows + const checkpointBaseline = security.fingerprint(checkpointBaselineRows) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0) + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES('owned-native-reader','127.0.0.1',$1,true,'fixture','inert-only',false,true) RETURNING id", [bound.port])).rows[0].id) + await run("actual fixed read-only reader preserves one captured authority/source and cumulative wire budget across three stages", async () => { + const f = await source(), originalAuthorize = f.options.authorize, session = await createRoutingNetworkNativeSession(f.options), before = responseBytes + const first = await session.readSnapshot("before") + assert.equal(first.securityProof, false); assert.equal(first.logCoverage, "unknown"); assert.equal(first.probes.length, 1) + const server = first.servers[0] + const manifest = { revision: 1, phase: "forward", identity: { operationId: f.operationId, stepIndex: 0, profileId: f.profileId, profileVersion: 1, sourceEpoch: "owned-fixture-epoch", authorityIdentity: "owned-fixture-authority" }, + // Bind the diagnostic fixture to the actual acquisition interval. A guessed + // lookback after SQL/HTTP completes can put its source after the first read. + sourceCreatedAt: Date.parse(first.startedAt), deadline: f.options.deadline, observationStartAt: Date.parse(first.observedAt), maximumSampleAgeMs: 30000, serverIds: [serverId], + configuration: { serverId, nativeId: "*r1", table: "tenant", bodyIdentity: "not-a-configuration-checkpoint", dependencyIdentity: "not-a-publication-proof" }, + routes: [{ key: "route1", serverId, family: "ipv4", prefix: "203.0.113.0/24", table: "tenant", sessionKey: "peer1", nextHop: "192.0.2.1", interface: "wan1", resolverTable: "main", resolutionChain: [{ address: "192.0.2.1", table: "main", interface: "wan1" }] }], + bgp: [{ key: "peer1", serverId, family: "ipv4", connectionId: "*c1", instance: "default", localAddress: "192.0.2.2", remoteAddress: "192.0.2.1", remoteAsn: 65001 }], + bfd: [{ key: "bfd1", serverId, applicability: "required", configurationIdentity: "fixture-bfd", localAddress: "192.0.2.2", remoteAddress: "192.0.2.1", interface: "wan1", vrf: "main" }], + probes: [{ key: "a", serverId, family: "ipv4", destination: "203.0.113.9", source: "198.51.100.1", interface: "wan1", probeVrf: "probe", expectedHops: ["203.0.113.9"] }], + logs: [{ key: "logs1", serverId, generation: "fixture-ring", sequenceMode: "contiguous-capture", afterSequence: 0, topics: ["route", "bgp", "bfd", "interface"], relatedKeys: ["route1", "peer1", "bfd1", "a"] }], + } + const parser = captureRoutingNetworkNativeParser(manifest) + const metadata = { serverId: server.serverId, routerOsVersion: server.routerOsVersion } + const routeOrigin = parser.parseRoute("route1", { ...metadata, ...server.routes }) + assert.equal(routeOrigin.status, "unknown"); if (routeOrigin.status === "unknown") assert.equal(routeOrigin.reason, "unsupported-route-bgp-origin-namespace") + for (const parsed of [parser.parseBgp("peer1", { ...metadata, ...server.bgp }), parser.parseBfd("bfd1", { ...metadata, ...server.bfd }), parser.parseProbe("a", first.probes[0])]) assert.equal(parsed.status, "observed", JSON.stringify(parsed)) + assert.equal(parser.parseLogs({ ...metadata, ...server.logs }).status, "unknown", "An empty actual log response cannot mint complete authenticated log coverage") + const budgetBeforeContext = session.readBudget(), requestsBeforeContext = requests.length + const shared = await readRoutingNetworkScopedCommunityContext(session.sharedContext) + assert.equal(shared.before.scopedExecutionAvailable, false, "Read-only shared acquisition must retain existing execution guards") + const budgetAfterContext = session.readBudget() + assert.equal(budgetAfterContext.calls - budgetBeforeContext.calls, requests.length - requestsBeforeContext) + assert(budgetAfterContext.bytes > budgetBeforeContext.bytes); assert(budgetAfterContext.rows > budgetBeforeContext.rows) + const beforeFake = requests.length + await assert.rejects(readRoutingNetworkScopedCommunityContext(JSON.parse(JSON.stringify(session.sharedContext)))) + assert.equal(requests.length, beforeFake) + f.options.manifest.servers.push(999999); f.options.authorize = async () => { throw Error("Outer provider must not replace captured fence") } + const logRow = (id: string, message: string) => ({ ".id": id, buffer: "memory", time: "10:00:00", topics: "bgp,info", message }) + const up = logRow("*1", "session up"), down = logRow("*2", "session down"), restored = logRow("*3", "session restored") + logRows = [up, down] + const second = await session.readSnapshot("after-forward") + assert.deepEqual(second.servers[0].logs.payload, [up, down]) + logRows = [down, restored] + const secondRound = await session.readSnapshot("after-forward") + logRows = [restored, logRow("*1", "session restarted")] + const third = await session.readSnapshot("after-inverse") + const thirdRound = await session.readSnapshot("after-inverse") + for (const snapshot of [second, secondRound, third, thirdRound]) { + assert.equal(snapshot.logCoverage, "unknown") + assert.equal(snapshot.servers[0].logs.payload.length, 2) + } + const assembledForward = captureRoutingNetworkObservationAssembly(manifest).assemble([first, second, secondRound], Date.now()) + const assembledInverse = captureRoutingNetworkObservationAssembly({ ...manifest, phase: "inverse", observationStartAt: Date.parse(third.startedAt) }).assemble([first, second, secondRound, third, thirdRound], Date.now()) + for (const assembled of [assembledForward, assembledInverse]) { + assert.equal(assembled.verdict, "unknown"); assert.equal(assembled.securityProof, false); assert.equal(assembled.completeRounds, 0) + assert.equal(assembled.diagnostics.length, 2, JSON.stringify(assembled.reasons)) + assert(assembled.reasons.includes("configuration-proof-unavailable")) + assert(assembled.reasons.includes("authenticated-log-interval-coverage-unsupported")) + assert(assembled.diagnostics.every(round => round.observations.filter(entry => entry.stage !== "logs" && entry.stage !== "routes").every(entry => entry.result.status === "observed"))) + assert(assembled.diagnostics.every(round => round.observations.filter(entry => entry.stage === "routes").every(entry => entry.result.status === "unknown" && entry.result.reason === "unsupported-route-bgp-origin-namespace"))) + } + assert(second.budget.calls > first.budget.calls); assert(third.budget.calls > second.budget.calls) + assert.equal(third.budget.deadline, f.options.deadline); assert.equal(session.readBudget().bytes, responseBytes - before) + assert(third.budget.rows > second.budget.rows); assert(f.fences() > 20) + assert(Object.isFrozen(first.servers[0].routes.payload)); session.close() + const count = requests.length; await assert.rejects(createRoutingNetworkNativeSession({ ...f.options, authorize: originalAuthorize, manifest: { servers: [serverId!], probes: [] } }), /уже создавалась/); assert.equal(requests.length, count) + }) + await run("actual terminal zero-received ping skips traceroute without inventing log coverage", async () => { + const f = await source(), session = await createRoutingNetworkNativeSession(f.options), count = requests.length + mode = "zero-ping" + const snapshot = await session.readSnapshot("before") + assert.equal(snapshot.probes.length, 1) + assert.deepEqual(snapshot.probes[0].pingPayload, [{ sent: "3", received: "0", "packet-loss": "100%" }]) + assert.equal(snapshot.probes[0].tracePayload, undefined) + assert.equal(snapshot.logCoverage, "unknown") + assert.equal(requests.slice(count).filter(request => request.path === "/tool/ping").length, 1) + assert.equal(requests.slice(count).filter(request => request.path === "/tool/traceroute").length, 0) + session.close() + }) + await run("reordered stage permanently closes session without native continuation", async () => { + const f = await source(false), session = await createRoutingNetworkNativeSession(f.options), count = requests.length + await assert.rejects(session.readSnapshot("after-forward")); await assert.rejects(session.readSnapshot("before")); assert.equal(requests.length, count) + }) + for (const scenario of ["profile", "endpoint-aba", "cancel"] as const) await run("late " + scenario + " after actual GET stops subsequent native requests", async () => { + const f = await source(false), abort = new AbortController(); f.options.context.signal = abort.signal + const session = await createRoutingNetworkNativeSession(f.options), count = requests.length + onRequest = async () => { + if (scenario === "profile") await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", [f.profileId]) + else if (scenario === "endpoint-aba") { await pool.query("UPDATE servers SET username='temporary' WHERE id=$1", [serverId]); await pool.query("UPDATE servers SET username='fixture' WHERE id=$1", [serverId]) } + else abort.abort() + } + await assert.rejects(session.readSnapshot("before")); assert.equal(requests.length, count + 1) + await assert.rejects(session.readSnapshot("after-forward")); assert.equal(requests.length, count + 1) + }) + await run("tampered source request and extended original deadline refuse before HTTP", async () => { + const f = await source(false), count = requests.length + f.evidence.steps[0].input.requestProof = "f".repeat(64) + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [f.planId, JSON.stringify({ sealed: await security.encryptSecret(JSON.stringify(f.evidence)) })]) + await assert.rejects(createRoutingNetworkNativeSession(f.options)); assert.equal(requests.length, count) + const g = await source(false); await pool.query("UPDATE mcp_operations SET created_at=now()-interval '60 seconds' WHERE id=$1", [g.operationId]) + await assert.rejects(createRoutingNetworkNativeSession(g.options)); assert.equal(requests.length, count) + }) + for (const fault of ["redirect", "auth", "malformed", "oversized", "rows"] as const) await run("actual " + fault + " response stops without retry or redirect", async () => { + const f = await source(false), session = await createRoutingNetworkNativeSession(f.options), count = requests.length + mode = fault; await assert.rejects(session.readSnapshot("before")); assert.equal(requests.length, count + 1) + await assert.rejects(session.readSnapshot("after-forward")); assert.equal(requests.length, count + 1); assert.equal(sinks, 0) + }) + await run("actual aggregate wire bytes remain charged across forward and inverse", async () => { + const f = await source(false), session = await createRoutingNetworkNativeSession(f.options); mode = "total-bytes" + await session.readSnapshot("before"); await session.readSnapshot("after-forward") + await assert.rejects(session.readSnapshot("after-forward")); assert(session.readBudget().bytes > 8 * 1048576) + }) + await run("actual row count remains charged across phases and rejects total10000 overflow", async () => { + const f = await source(false), session = await createRoutingNetworkNativeSession(f.options); mode = "total-rows" + await session.readSnapshot("before"); await assert.rejects(session.readSnapshot("after-forward")); assert(session.readBudget().rows > 10000) + }) + const topologyObserver = async () => { + const f = await source(false), { connectionId, credentialId } = f.options.context.principal + const policy = mcpConnectionPolicySchema.parse({ ...f.options.context.connection.policy, mode: "observer", permissions: ["mm:network:read"], + scope: { serverIds: [serverId!], operationIds: ["routing.topology.read"], allowGlobal: false } }) + await pool.query("UPDATE mcp_connections SET policy=$2::jsonb,owner_permissions=$3::jsonb WHERE id=$1", [connectionId, JSON.stringify(policy), JSON.stringify(["mm:network:read"])]) + const token = security.randomToken("bearer") + await pool.query("UPDATE mcp_credentials SET digest=$2 WHERE id=$1", [credentialId, security.tokenDigest(token)]) + return { principal: await authenticateMcpToken(token), connectionId } + } + await run("actual public topology bearer reader observes final gateway without operation binding or namespace joins", async () => { + const f = await topologyObserver(), count = requests.length, plans = Number((await pool.query("SELECT count(*) AS n FROM mcp_plans")).rows[0].n) + const result = await invokeMcpTool(f.principal, "mm_routing_topology_get", { serverId }) as { routeResolution: { status: string; items: Array<{ status: string; immediateGateway: { address: string; interface: string }; semanticProof: boolean; routeOriginVerified: boolean; recursiveChainVerified: boolean }> } } + assert.equal(requests.length - count, 11) + assert.equal(result.routeResolution.status, "observed") + const route = result.routeResolution.items[0] + assert.equal(route.status, "observed"); assert.deepEqual(route.immediateGateway, { address: "192.0.2.1", interface: "wan1" }) + assert.equal(route.semanticProof, false); assert.equal(route.routeOriginVerified, false); assert.equal(route.recursiveChainVerified, false) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_plans")).rows[0].n), plans) + assert(!JSON.stringify(result).includes("peer-cache-id")); assert(!JSON.stringify(result).includes("bgp.session")) + }) + await run("actual public topology preserves unknown numeric gateway despite matching BGP cache/session strings", async () => { + const f = await topologyObserver(); mode = "topology-unknown" + const result = await invokeMcpTool(f.principal, "mm_routing_topology_get", { serverId }) as { routeResolution: { items: Array<{ status: string; reason: string }> } } + assert.equal(result.routeResolution.items[0].status, "unknown") + assert.equal(result.routeResolution.items[0].reason, "unconfirmed-immediate-endpoint") + }) + await run("actual public topology bearer and server scope/disabled checks refuse before native I/O", async () => { + const f = await topologyObserver(), count = requests.length + await assert.rejects(authenticateMcpToken("not-a-token")) + await assert.rejects(invokeMcpTool(f.principal, "mm_routing_topology_get", { serverId: serverId! + 100000 })) + await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + await assert.rejects(invokeMcpTool(f.principal, "mm_routing_topology_get", { serverId })) + await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [serverId]) + assert.equal(requests.length, count) + }) + await run("actual public topology pause endpoint ABA and cancellation retire at first response without publication", async () => { + for (const fault of ["pause", "endpoint-aba", "cancel"] as const) { + const f = await topologyObserver(), count = requests.length, cancel = new AbortController() + onRequest = async () => { + if (fault === "pause") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"') WHERE id=$1", [f.connectionId]) + else if (fault === "endpoint-aba") { + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=$1", [serverId]) + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=$1", [serverId]) + } else cancel.abort() + } + await assert.rejects(invokeMcpTool(f.principal, "mm_routing_topology_get", { serverId }, cancel.signal)) + assert.equal(requests.length, count + 1) + } + }) + assert.equal(writes, 0) + assert.equal(security.fingerprint((await pool.query("SELECT to_jsonb(c)::text AS inventory_row FROM mcp_operation_checkpoints c ORDER BY operation_id,step_index")).rows), checkpointBaseline, + "Read-only acceptance must preserve existing checkpoints exactly and create none") + console.log("Owned routing native reader SQL/HTTP:", JSON.stringify({ cases: cases.length, names: cases, requests: requests.length, nativeWrites: writes, actualSql: true, actualHttp: true, syntheticEncryptedSourcePlans: true, genuinePrepareAccepted: false, configurationCheckpointFabricated: false, priorCheckpointsPreserved: checkpointBaselineRows.length, checkpointInventoryUnchanged: true, signedNetworkOutcomePublished: false, logCoverage: "unknown", reservedDispatchEnabled: false, realChr: false })) +} finally { + if (profiles.length) await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=ANY($1::uuid[])", [profiles]).catch(() => {}) + if (connections.length) await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"') WHERE id=ANY($1::uuid[])", [connections]).catch(() => {}) + if (serverId) await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]).catch(() => {}) + restoreAgent(); router.closeAllConnections(); await new Promise(done => router.close(() => done())) + await closePool() +} diff --git a/backend/src/modules/mcp/routing-network-observation-assembly.test.ts b/backend/src/modules/mcp/routing-network-observation-assembly.test.ts new file mode 100644 index 00000000..13cc84fe --- /dev/null +++ b/backend/src/modules/mcp/routing-network-observation-assembly.test.ts @@ -0,0 +1,140 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { captureRoutingNetworkNativeParser } from "./routing-network-native-parser.js" +import type { RoutingNetworkManifest } from "./routing-network-verifier-model.js" +import { captureRoutingNetworkObservationAssembly } from "./routing-network-observation-assembly.js" +// Synthetic raw reader data only; no configuration checkpoint is invented. +function fixture(ipv6 = false) { + const local = ipv6 ? "2001:db8::2" : "192.0.2.2", remote = ipv6 ? "2001:db8::1" : "192.0.2.1" + const destination = ipv6 ? "2001:db8:10::1" : "203.0.113.1", source = ipv6 ? "2001:db8:20::1" : "198.51.100.1" + const family = ipv6 ? "ipv6" : "ipv4", afi = ipv6 ? "ipv6" : "ip" + const manifest: RoutingNetworkManifest = { revision: 1, phase: "forward", identity: { operationId: "00000000-0000-4000-8000-000000000001", profileId: "00000000-0000-4000-8000-000000000002", profileVersion: 1, stepIndex: 0, sourceEpoch: "epoch1", authorityIdentity: "authority1" }, + sourceCreatedAt: 1800000000000, deadline: 1800000120000, observationStartAt: 1800000001000, maximumSampleAgeMs: 30000, serverIds: [1], + configuration: { serverId: 1, nativeId: "*F1", table: "tenant", bodyIdentity: "body1", dependencyIdentity: "deps1" }, + routes: [{ key: "route1", serverId: 1, family, prefix: ipv6 ? "2001:db8:10::/64" : "203.0.113.0/24", table: "tenant", sessionKey: "peer1", nextHop: remote, interface: "wan1", resolverTable: "main", resolutionChain: [{ address: remote, table: "main", interface: "wan1" }] }], + bgp: [{ key: "peer1", serverId: 1, family, connectionId: "*C1", instance: "default", localAddress: local, remoteAddress: remote, remoteAsn: 65001 }], + bfd: [{ key: "bfd1", serverId: 1, applicability: "required", configurationIdentity: "bfd-config", localAddress: local, remoteAddress: remote, interface: "wan1", vrf: "main" }], + probes: [{ key: "probe1", serverId: 1, family, destination, source, interface: "wan1", probeVrf: "diagnostic", expectedHops: [remote, destination] }], + logs: [{ key: "logs1", serverId: 1, generation: "boot1", sequenceMode: "contiguous-capture", afterSequence: 0, topics: ["route", "bgp", "bfd", "interface"], relatedKeys: ["route1", "peer1", "bfd1", "probe1"] }], + } + // Raw RouterOS REST field names/string values, not model observations. + const connections = [{ ".id": "*C1", name: "peer", instance: "default", "local.address": local, "remote.address": remote, "remote.as": "65001", afi, disabled: "false", templates: "", vrf: "main", "routing-table": "tenant" }] + const sessions = [{ ".id": "*B000002", name: "runtime-name-is-not-a-binding", instance: "default", "local.address": local, "remote.address": remote, + "remote.as": "65001", "local.afi": afi, "remote.afi": afi, established: "true", vrf: "main", "routing-table": "tenant", uptime: "1h" }] + const base = { serverId: 1, routerOsVersion: "7.20.4 (stable)" } + const bgp = { ...base, resource: "/routing/bgp/session", payload: sessions, connectionsPayload: connections } + const route = { ...base, resource: "/routing/route", payload: [{ ".id": "*R1", afi: ipv6 ? "ip6" : "ip4", "dst-address": manifest.routes[0].prefix, "routing-table": "tenant", bgp: "true", dynamic: "true", active: "true", contribution: "active", gateway: remote + "@main", "immediate-gw": remote + "%wan1", "bgp.peer-cache-id": "*B000002", "belongs-to": "untrusted descriptive text" }], sessionsPayload: sessions, connectionsPayload: connections } + const bfd = { ...base, resource: "/routing/bfd/session", payload: [{ ".id": "*D1", "local-address": local + "%wan1@main", "remote-address": remote + "%wan1@main", state: "Up" }] } + const probe = { ...base, request: { destination, source, interface: "wan1", probeVrf: "diagnostic", count: 3 as const, maxHops: 16 as const }, + pingPayload: [{ sent: "1", received: "1", "packet-loss": "0", "avg-rtt": "453us", host: destination }, { sent: "2", received: "2", "packet-loss": "0", "avg-rtt": "1ms", host: destination }, { sent: "3", received: "3", "packet-loss": "0", "avg-rtt": "53ms561us", host: destination }], + tracePayload: [{ address: remote, sent: "1", loss: "0" }, { address: destination, sent: "1", loss: "0" }], + } + return { manifest, parser: captureRoutingNetworkNativeParser(manifest), base, bgp, route, bfd, probe } +} + +function captured(ipv6 = false, inverse = false) { + const f = fixture(ipv6) + if (inverse) f.manifest.phase = "inverse" + const stages = inverse ? ["before", "after-forward", "after-forward", "after-inverse", "after-inverse"] : ["before", "after-forward", "after-forward"] + const seen: Record = {} + const snapshots = stages.map((stage, index) => { + const { serverId: _serverId, routerOsVersion: _version, ...routes } = f.route + const { serverId: _bgpId, routerOsVersion: _bgpVersion, ...bgp } = f.bgp + const { serverId: _bfdId, routerOsVersion: _bfdVersion, ...bfd } = f.bfd + return { sourceOperationId: f.manifest.identity.operationId, stage, round: seen[stage] = (seen[stage] ?? 0) + 1, + startedAt: new Date(f.manifest.sourceCreatedAt + index * 2000).toISOString(), observedAt: new Date(f.manifest.sourceCreatedAt + index * 2000 + 1000).toISOString(), + servers: [{ ...f.base, routes, bgp, bfd, logs: { resource: "/log", buffer: "memory", payload: [] as Record[], coverage: "unknown" } }], + probes: [{ ...f.probe, key: "probe1", expectedHopAddresses: f.manifest.probes[0].expectedHops.map(address => ({ serverId: 1, interface: "wan1", address })) }], + securityProof: false, logCoverage: "unknown", budget: { deadline: f.manifest.deadline, calls: (index + 1) * 10, bytes: (index + 1) * 5000, rows: (index + 1) * 20, maxCalls: 256, maxBytes: 8388608 } } + }) + return { ...f, snapshots, now: f.manifest.sourceCreatedAt + 10000 } +} + +for (const ipv6 of [false, true]) for (const inverse of [false, true]) test(`fixed raw composition IPv6=${ipv6} inverse=${inverse} preserves missing proof and log UNKNOWN`, () => { + const f = captured(ipv6, inverse), result = captureRoutingNetworkObservationAssembly(f.manifest).assemble(f.snapshots, f.now) + assert.equal(result.verdict, "unknown"); assert.equal(result.securityProof, false); assert.equal(result.forwardingVerified, false) + assert.equal(result.diagnostics.length, 2); assert.deepEqual(result.modelRounds, []); assert.equal(result.completeRounds, 0) + for (const round of result.diagnostics) { + assert.equal(round.observations.length, 5) + for (const entry of round.observations) { + assert.equal(entry.result.status, entry.stage === "logs" || entry.stage === "routes" ? "unknown" : "observed", JSON.stringify(entry)) + if (entry.stage === "routes" && entry.result.status === "unknown") assert.equal(entry.result.reason, "unsupported-route-bgp-origin-namespace") + } + } + assert(result.reasons.includes("configuration-proof-unavailable")); assert(Object.isFrozen(result.diagnostics)) +}) + +const invalid = { + "source-after-first-read": (f: ReturnType) => { f.manifest.sourceCreatedAt = Date.parse(f.snapshots[0].startedAt) + 1 }, + "foreign-operation": (f: ReturnType) => { f.snapshots[1].sourceOperationId = "00000000-0000-4000-8000-000000000099" }, + "duplicate-server": (f: ReturnType) => { f.snapshots[1].servers.push(f.snapshots[1].servers[0]) }, + "missing-probe": (f: ReturnType) => { f.snapshots[1].probes = [] }, + "wrong-probe-server": (f: ReturnType) => { f.snapshots[1].probes[0].serverId = 2 }, + "round-replay": (f: ReturnType) => { f.snapshots[2].round = 1 }, + "phase-replay": (f: ReturnType) => { f.snapshots[2].stage = "before" }, + "unexpected-inverse": (f: ReturnType) => { f.snapshots[2].stage = "after-inverse" }, + "future-time": (f: ReturnType) => { f.snapshots[2].observedAt = new Date(f.now + 1).toISOString() }, + "overlap": (f: ReturnType) => { f.snapshots[2].startedAt = f.snapshots[1].startedAt }, + "deadline-extension": (f: ReturnType) => { f.snapshots[1].budget.deadline++ }, + "budget-rewind": (f: ReturnType) => { f.snapshots[1].budget.bytes = 1 }, + "underreported-requests": (f: ReturnType) => { f.snapshots[1].budget.calls = 11 }, + "underreported-rows": (f: ReturnType) => { f.snapshots[1].budget.rows = 21 }, + "expired": (f: ReturnType) => { f.now = f.manifest.deadline }, + "stale": (f: ReturnType) => { f.now += 30000 }, +} +for (const [name, mutate] of Object.entries(invalid)) test(name + " cannot assemble a complete selected phase", () => { + const f = captured(); mutate(f) + const result = captureRoutingNetworkObservationAssembly(f.manifest).assemble(f.snapshots, f.now) + assert.equal(result.verdict, "unknown"); assert(result.reasons.length > 2, JSON.stringify(result)) +}) + +test("caller configuration receipt, signer and promoted coverage are rejected by strict snapshot shape", () => { + for (const extra of [{ configuration: { status: "CONFIG_APPLIED" } }, { signer: "caller" }, { logCoverage: "complete" }]) { + const f = captured(); Object.assign(f.snapshots[1], extra) + assert(captureRoutingNetworkObservationAssembly(f.manifest).assemble(f.snapshots, f.now).reasons.includes("malformed-or-unsupported-snapshots")) + } +}) + +test("overlapping RouterOS REST log snapshots with reused IDs still cannot establish continuity", () => { + const f = captured() + const entry = (id: string, message: string) => ({ ".id": id, buffer: "memory", time: "10:00:00", topics: "bgp,info", message }) + f.snapshots[0].servers[0].logs.payload = [entry("*1", "session up"), entry("*2", "session down")] + f.snapshots[1].servers[0].logs.payload = [entry("*2", "session down"), entry("*3", "session up")] + f.snapshots[2].servers[0].logs.payload = [entry("*3", "session up"), entry("*1", "session up")] + const result = captureRoutingNetworkObservationAssembly(f.manifest).assemble(f.snapshots, f.now) + assert.equal(result.verdict, "unknown") + assert.equal(result.completeRounds, 0) + assert.equal(result.securityProof, false) + assert.equal(result.forwardingVerified, false) + assert(result.reasons.includes("logs:logs1:authenticated-log-interval-coverage-unsupported")) +}) + +test("parser uncertainty and explicit unhealthy data survive simultaneous log uncertainty", () => { + const f = captured() + for (const snap of f.snapshots) { snap.servers[0].bgp.payload[0].established = "false"; snap.servers[0].bfd.payload[0].state = "down"; snap.probes[0].pingPayload[2].received = "2" } + const result = captureRoutingNetworkObservationAssembly(f.manifest).assemble(f.snapshots, f.now) + assert.equal(result.verdict, "unknown"); assert.equal(result.diagnostics.length, 2) + for (const round of result.diagnostics) { + const bgp = round.observations.find(entry => entry.stage === "bgp")!.result + assert.equal(bgp.status, "observed") + if (bgp.status === "observed") assert.equal((bgp.observation as { state: string }).state, "not-established") + assert.equal(round.observations.find(entry => entry.stage === "probes")!.result.status, "unknown") + } +}) + +test("captured manifest resists later caller mutation", () => { + const f = captured(), assembly = captureRoutingNetworkObservationAssembly(f.manifest) + f.manifest.serverIds.push(999) + assert.equal(assembly.assemble(f.snapshots, f.now).diagnostics.length, 2) +}) + +test("cyclic and non-JSON raw native fields refuse without throwing or promoting proof", () => { + for (const native of [new Date(), Number.NaN, () => true, undefined]) { + const f = captured(); Object.assign(f.snapshots[1].servers[0].routes.payload[0], { unsupported: native }) + assert(captureRoutingNetworkObservationAssembly(f.manifest).assemble(f.snapshots, f.now).reasons.includes("malformed-or-unbounded-json-snapshots")) + } + const f = captured(); Object.assign(f.snapshots[1].servers[0].routes.payload[0], { cycle: f.snapshots }) + assert(captureRoutingNetworkObservationAssembly(f.manifest).assemble(f.snapshots, f.now).reasons.includes("malformed-or-unbounded-json-snapshots")) +}) + + diff --git a/backend/src/modules/mcp/routing-network-observation-assembly.ts b/backend/src/modules/mcp/routing-network-observation-assembly.ts new file mode 100644 index 00000000..32981a22 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-observation-assembly.ts @@ -0,0 +1,104 @@ +import { z } from "zod" +import { captureRoutingNetworkNativeParser, ROUTING_NATIVE_LOG_BUFFER, type RoutingNativeParseResult } from "./routing-network-native-parser.js" +import { captureRoutingNetworkManifest, createRoutingNetworkBudget, spendRoutingNetworkBudget } from "./routing-network-verifier-model.js" + +const counter = z.number().int().safe().nonnegative() +const rows = z.array(z.record(z.string(), z.unknown())).max(1000) +const server = z.object({ serverId: z.number().int().positive(), routerOsVersion: z.string().max(128), + routes: z.object({ resource: z.literal("/routing/route"), payload: rows, sessionsPayload: rows, connectionsPayload: rows }).strict(), + bgp: z.object({ resource: z.literal("/routing/bgp/session"), payload: rows, connectionsPayload: rows }).strict(), + bfd: z.object({ resource: z.literal("/routing/bfd/session"), payload: rows }).strict(), + logs: z.object({ resource: z.literal("/log"), buffer: z.literal(ROUTING_NATIVE_LOG_BUFFER), payload: rows, coverage: z.literal("unknown") }).strict(), +}).strict() +const probe = z.object({ key: z.string().min(1).max(128), serverId: z.number().int().positive(), routerOsVersion: z.string().max(128), + pingPayload: rows, tracePayload: rows.optional(), + expectedHopAddresses: z.array(z.object({ serverId: z.number().int().positive(), interface: z.string(), address: z.string() }).strict()).min(1).max(16), + request: z.object({ destination: z.string(), source: z.string(), interface: z.string(), probeVrf: z.string(), count: z.literal(3), maxHops: z.literal(16) }).strict(), +}).strict() +const instant = z.string().datetime({ offset: true }).transform(value => Date.parse(value)) +const snapshot = z.object({ sourceOperationId: z.string().uuid(), stage: z.enum(["before", "after-forward", "after-inverse"]), + round: z.number().int().min(1).max(3), startedAt: instant, observedAt: instant, servers: z.array(server).min(1).max(32), probes: z.array(probe).max(8), + securityProof: z.literal(false), logCoverage: z.literal("unknown"), + budget: z.object({ deadline: counter, calls: counter.max(256), bytes: counter.max(8 * 1048576), rows: counter.max(10000), + maxCalls: z.literal(256), maxBytes: z.literal(8 * 1048576) }).strict(), +}).strict() +type StageDiagnostic = { stage: "routes" | "bgp" | "bfd" | "probes" | "logs"; key: string; result: RoutingNativeParseResult } +const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b) +function boundedJson(raw: unknown): boolean { + let nodes = 0 + const visit = (value: unknown, depth: number): boolean => { + if (++nodes > 300000 || depth > 16) return false + if (value === null || typeof value === "boolean") return true + if (typeof value === "string") return value.length <= 1048576 + if (typeof value === "number") return Number.isFinite(value) + if (Array.isArray(value)) return value.length <= 1000 && value.every(child => visit(child, depth + 1)) + return !!value && typeof value === "object" && [Object.prototype, null].includes(Object.getPrototypeOf(value)) && + Object.values(value).every(child => visit(child, depth + 1)) + } + return visit(raw, 0) +} +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} + +/** Inert diagnostic composition over captured data. No native I/O, authority, + * signer, configuration receipt or execution capability is accepted or minted. + * A native session snapshot is data even when its source was authenticated. + * Complete model rounds deliberately remain unavailable until an independent + * fixed production configuration-proof bridge and log-coverage bridge exist. */ +export function captureRoutingNetworkObservationAssembly(rawManifest: unknown) { + const manifest = captureRoutingNetworkManifest(rawManifest), parser = captureRoutingNetworkNativeParser(manifest) + return Object.freeze({ assemble(rawNativeSnapshots: unknown, now: number) { + const diagnostics: { stage: string; round: number; observations: StageDiagnostic[] }[] = [] + const reasons = new Set(["configuration-proof-unavailable", "authenticated-log-interval-coverage-unsupported"]) + const finish = () => freeze({ verdict: "unknown" as const, reasons: [...reasons], diagnostics, + modelRounds: [] as never[], completeRounds: 0, securityProof: false as const, forwardingVerified: false as const }) + if (!boundedJson(rawNativeSnapshots)) { reasons.add("malformed-or-unbounded-json-snapshots"); return finish() } + const parsed = z.array(snapshot).min(3).max(7).safeParse(rawNativeSnapshots) + if (!parsed.success || !Number.isSafeInteger(now) || now < manifest.sourceCreatedAt) { reasons.add("malformed-or-unsupported-snapshots"); return finish() } + if (now >= manifest.deadline) { reasons.add("original-window-expired"); return finish() } + let budget = createRoutingNetworkBudget(manifest.sourceCreatedAt, [manifest.deadline], manifest.sourceCreatedAt) + const counts = { before: 0, "after-forward": 0, "after-inverse": 0 } + let phase = "before", previousAt = manifest.sourceCreatedAt, previous = { calls: 0, bytes: 0, rows: 0 } + const target = manifest.phase === "forward" ? "after-forward" : "after-inverse" + for (const value of parsed.data) { + const reject = (reason: string) => { reasons.add(reason); return finish() } + if (value.sourceOperationId !== manifest.identity.operationId) return reject("snapshot-source-operation-drift") + if (value.startedAt < previousAt || value.observedAt < value.startedAt || value.observedAt > now || value.observedAt >= manifest.deadline) return reject("snapshot-time-window-or-order-drift") + previousAt = value.observedAt + if (value.stage === "before" ? phase !== "before" || counts.before !== 0 : value.stage === "after-forward" ? counts.before !== 1 || phase === "after-inverse" : counts["after-forward"] < 2 || manifest.phase !== "inverse") return reject("snapshot-phase-order-drift") + if (value.round !== ++counts[value.stage]) return reject("snapshot-round-sequence-drift") + phase = value.stage + const ids = value.servers.map(entry => entry.serverId).sort((a,b) => a-b) + if (!same(ids, manifest.serverIds) || !same(value.probes.map(entry => entry.key).sort(), manifest.probes.map(entry => entry.key).sort())) return reject("snapshot-scope-incomplete-or-duplicate") + if (value.probes.some(entry => entry.serverId !== manifest.probes.find(wanted => wanted.key === entry.key)?.serverId || entry.routerOsVersion !== value.servers.find(server => server.serverId === entry.serverId)?.routerOsVersion)) return reject("snapshot-probe-source-drift") + if (value.servers.some(entry => !same(entry.routes.sessionsPayload, entry.bgp.payload) || !same(entry.routes.connectionsPayload, entry.bgp.connectionsPayload))) return reject("snapshot-shared-catalogue-drift") + const delta = { requests: value.budget.calls - previous.calls, bytes: value.budget.bytes - previous.bytes, rows: value.budget.rows - previous.rows } + const minimumCalls = value.servers.length * 6 + value.probes.reduce((sum, entry) => sum + 1 + (entry.tracePayload ? 1 : 0), 0) + const minimumRows = value.servers.reduce((sum, entry) => sum + 1 + entry.routes.payload.length + entry.bgp.payload.length + entry.bgp.connectionsPayload.length + entry.bfd.payload.length + entry.logs.payload.length, 0) + value.probes.reduce((sum, entry) => sum + entry.pingPayload.length + (entry.tracePayload?.length ?? 0), 0) + if (value.budget.deadline !== manifest.deadline || delta.requests < minimumCalls || delta.rows < minimumRows || delta.bytes <= 0) return reject("snapshot-cumulative-budget-drift") + const spent = spendRoutingNetworkBudget(budget, value.stage === "after-inverse" ? "inverse" : "forward", value.observedAt, delta) + if (!spent.accepted) return reject("snapshot-budget-exhausted-or-rewound") + budget = spent.budget; previous = value.budget + if (value.stage !== target) continue + if (value.startedAt < manifest.observationStartAt || now - value.startedAt > manifest.maximumSampleAgeMs) return reject("snapshot-selected-phase-stale-or-before-observation") + const observations: StageDiagnostic[] = [] + for (const stage of ["routes", "bgp", "bfd"] as const) for (const wanted of manifest[stage]) { + const entry = value.servers.find(entry => entry.serverId === wanted.serverId)! + const raw = { serverId: entry.serverId, routerOsVersion: entry.routerOsVersion, ...entry[stage] } + const result = stage === "routes" ? parser.parseRoute(wanted.key, raw) : stage === "bgp" ? parser.parseBgp(wanted.key, raw) : parser.parseBfd(wanted.key, raw) + observations.push({ stage, key: wanted.key, result }) + } + for (const wanted of manifest.probes) observations.push({ stage: "probes", key: wanted.key, result: parser.parseProbe(wanted.key, value.probes.find(entry => entry.key === wanted.key)) }) + for (const wanted of manifest.logs) { + const entry = value.servers.find(entry => entry.serverId === wanted.serverId)! + observations.push({ stage: "logs", key: wanted.key, result: parser.parseLogs({ serverId: entry.serverId, routerOsVersion: entry.routerOsVersion, ...entry.logs }) }) + } + for (const entry of observations) if (entry.result.status === "unknown") reasons.add(entry.stage + ":" + entry.key + ":" + entry.result.reason) + diagnostics.push({ stage: value.stage, round: value.round, observations }) + } + if (counts[target] < 2 || counts[target] > 3) reasons.add("selected-phase-requires-two-or-three-rounds") + return finish() + } }) +} diff --git a/backend/src/modules/mcp/routing-network-scoped-context.test.ts b/backend/src/modules/mcp/routing-network-scoped-context.test.ts new file mode 100644 index 00000000..07781429 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-scoped-context.test.ts @@ -0,0 +1,214 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID, createHash } from "node:crypto" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import { isIP } from "node:net" +import { test } from "node:test" +import ts from "typescript" +import { z } from "zod" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { managedComment } from "../../managed-markers.js" +import { compileManagedFilterRules } from "../../services/filter-rules-write-model.js" +import { nativeBgpRows, bgpTemplateReferences } from "../../services/bgp-export-model.js" +import { bgpFilterName, effectiveBgpFilter, stableBgpPolicyRow } from "../../services/bgp-filter-binding-model.js" +import { canonicalVrfMembers } from "../../services/vrf-members.js" +import { readOspfEffectiveDisabled } from "../../services/ospf-effective-disabled.js" +import { isConfirmedRoutingFibTable } from "../../services/routing-table-fib.js" +import * as contextModel from "./routing-community-context-model.js" +import { scopedTargetRuleMatches } from "./routing-community-scoped-model.js" +import { prepareOriginalEffectSelection } from "./routing-original-effects.js" +import { readMeasurementAddress } from "./routing-measurement-address.js" +import { routeContextClients } from "./routing-community-context-native.js" +import { pathProofTag } from "./path-proof-purpose.js" +import { ROUTING_NATIVE_LOG_BUFFER, ROUTING_NATIVE_LOG_PATH } from "./routing-network-native-parser.js" + +async function loadSource(file: string, providers: Record, exports: Record) { + const source = await readFile(new URL(file, import.meta.url), "utf8") + const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + runInNewContext(compiled, { exports, require(name: string) { assert(name in providers, "Unexpected production dependency: " + name); return providers[name] }, + Buffer, AbortSignal, AbortController, structuredClone, Date }) +} +type Token = Record +type Session = { sharedContext: Token; readSnapshot(stage: string): Promise; readBudget(): { calls: number; bytes: number; rows: number; closed: boolean }; close(): void } +type Context = { before: { scopedExecutionAvailable: boolean; scopedExecutionReason: string | null }; ownedTarget?: { nativeId: string }; clients?: unknown; signEvidence?: unknown } + +/** Actual native session, context, private API and HTTP transport source, with + * owned loopback wire I/O. SQL/authority providers are inert fixtures. This is + * combined I/O accounting acceptance, not genuine SQL checkpoint acceptance. */ +async function fixture(autoRecovery = false) { + let requests = 0, wireBytes = 0, wireRows = 0, rawGetObjects = 0, bulk = 0, padding = 0, revoked = false + let afterHttp: (() => void) | undefined, afterPath: { path: string; run(): void } | undefined + const body = compileManagedFilterRules([{ community: "65000:10", gateway: "192.0.2.1", outIface: "", action: "route", description: "" }]) + const collections: Record[]> = { + "/system/resource": [{ version: "7.20.4" }], + "/routing/table": [{ ".id": "*t", name: "tenant", fib: "true" }, { ".id": "*tm", name: "main", fib: "true" }], + "/ip/vrf": [{ ".id": "*v", name: "main", interfaces: "all", disabled: "false" }], + "/routing/bgp/connection": [{ ".id": "*c1", name: "peer", instance: "default", vrf: "main", "routing-table": "tenant", afi: "ip", "input.filter": "custom", "output.filter-chain": "", templates: "", disabled: "false" }], + "/routing/bgp/instance": [{ ".id": "*i", name: "default", vrf: "main", "routing-table": "main", disabled: "false" }], + "/routing/filter/rule": [{ ".id": "*f", chain: "custom", rule: body, comment: managedComment("fixture") }], + "/ip/route": [1, 9].map(value => ({ ".id": "*r" + value, "dst-address": "192.0.2." + value + "/32", "routing-table": "main", disabled: "false", comment: managedComment("recursive") })), + "/interface": ["wan", "hop", "target"].map((name, index) => ({ ".id": "*if" + index, name, disabled: "false" })), + "/ip/address": ["wan", "hop", "target"].map((name, index) => ({ ".id": "*ad" + index, interface: name, address: "198.51.100." + (index + 1) + "/32", disabled: "false", invalid: "false" })), + } + const router = createServer(async (req, res) => { + let raw = ""; for await (const chunk of req) raw += chunk + const args = raw ? JSON.parse(raw) : {}, path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")) + assert(["GET", "POST"].includes(req.method ?? "")); requests++ + let response: unknown + if (req.method === "POST") { + assert(path.endsWith("/get")); const resource = path.slice(0, -4) + const row = collections[resource]?.find(row => row[".id"] === args[".id"]) + const value = args["value-name"] === "disabled" ? "false" : row?.[args["value-name"]] + response = value === undefined ? [] : { ret: value }; if (!Array.isArray(response)) rawGetObjects++ + } else if (["/routing/ospf/instance", "/routing/rip/instance"].includes(path)) { + response = Array.from({ length: bulk || (padding ? 1 : 0) }, (_, index) => ({ ".id": "*e" + index, fixture: padding ? "я".repeat(padding) : "row" })) + } else response = collections[path] ?? [] + const encoded = JSON.stringify(response); wireBytes += Buffer.byteLength(encoded); wireRows += Array.isArray(response) ? response.length : 1 + res.setHeader("content-type", "application/json"); res.end(encoded) + afterHttp?.(); afterHttp = undefined + if (afterPath?.path === path) { const run = afterPath.run; afterPath = undefined; run() } + }) + const now = Date.now(), fingerprint = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex") + const endpoint = { id: 1, host: "127.0.0.1", port: 0, username: "fixture", password: "private", useSsl: false, verifySsl: true, enabled: true, routingEndpointEpoch: randomUUID() } + const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(now + 600000).toISOString() } + const connection = { id: principal.connectionId, owner_id: "owner", generation: "generation", owner_permissions: ["mm:network:read"], + policy: { permissions: ["mm:network:read"], scope: { serverIds: [1] }, expiresAt: new Date(now + 600000).toISOString() }, owner_valid_until: new Date(now + 600000) } + const profile = routeAiProfileSchema.parse({ name: "shared fixture", serverId: 1, family: "ipv4", community: "65000:10", table: "tenant", probeVrf: "main", autoRecovery, + bgpContext: { connectionId: "*c1", instance: "default", vrf: "main", inputChain: "custom" }, + candidates: [1, 9].map(value => ({ id: value === 1 ? "a" : "b", jumpHostId: 1, exitNodeId: 1, wanInterface: "wan", downstreamInterface: "hop", gateway: "192.0.2." + value, gatewayRef: { resource: "ipv4_route", id: "*r" + value }, + ...(autoRecovery ? { controlProbe: { targetInterface: "target", expectedHops: [{ serverId: 1, interface: "hop" }, { serverId: 1, interface: "target" }] } } : {}) })) }) + const sources = [{ serverId: 1, proof: "source" }] + const input = { profile, principal, ownerId: "owner", candidateId: autoRecovery ? "b" : "a", contextSources: sources, + generation: "generation", connectionGeneration: "generation", policyProof: fingerprint(connection.policy), + ...(autoRecovery ? { originalCandidateId: "a", proofRevision: 3 } : {}) } + const step = { kind: "service", service: "routing.community.scoped", input } + const row = { id: randomUUID(), state: "running", step_state: "intent", created_at: new Date(now), plan_id: randomUUID(), operation_id: "routing.community.switch", operation_version: 7, + approved_by: "owner", expires_at: new Date(now + 600000), intended_state: step, evidence: { sealed: JSON.stringify({ target: { scope: "servers", serverIds: [1] }, steps: [step] }) } } + const security = { fingerprint, redact: (value: unknown) => value, constantEqual: (a: string, b: string) => a === b, + McpError: class extends Error { constructor(readonly code: string, message: string) { super(message) } }, accessGeneration: async () => "generation", decryptSecret: async (value: string) => value, + privateFingerprinter: async () => (value: string) => fingerprint(value) } + const authority = async () => { if (revoked) throw Error("revoked fixture authority") } + const observationSource = { readRoutingEndpoints: async () => [endpoint], createRoutingSourceContext: async () => ({ expected: sources, assertCurrent: authority }) } + const sql = { query: async (query: string | { text: string }) => (typeof query === "string" ? query : query.text).includes("mcp_protections") ? { rows: [{ refs: [], overflow: false }] } : { rows: [row] } } + const native: Record = {}, context: Record = {}, privateApi: Record = {} + const common = { "../../db/index.js": { pool: sql }, "./security.js": security, "./store.js": { effectiveMcpConnection: async () => { await authority(); return connection } }, + "../../lib/permissions.js": { hasPermission: (values: string[], value: string) => values.includes(value) }, "./routing-observation-source.js": observationSource, + "./routing-community-context-native.js": { routeContextClients }, "../../services/vrf-members.js": { canonicalVrfMembers }, "../../services/ospf-effective-disabled.js": { readOspfEffectiveDisabled }, + "./routing-measurement-address.js": { readMeasurementAddress } } + await loadSource("./routing-community-context.ts", { ...common, "./routing-network-native-session.js": native, + "../../services/routing-table-fib.js": { isConfirmedRoutingFibTable }, "./routing-original-effects.js": { prepareOriginalEffectSelection }, + "../../services/bgp-export-model.js": { nativeBgpRows, bgpTemplateReferences }, "../../services/bgp-filter-binding-model.js": { bgpFilterName, effectiveBgpFilter, stableBgpPolicyRow }, + "./routing-community-context-model.js": contextModel, "./routing-community-scoped-model.js": { scopedTargetRuleMatches }, + "./path-proof-purpose.js": { pathProofTag }, + "./management-transport.js": { assertManagementTransport: async () => { throw Error("No alternate management fixture") } } }, context) + await loadSource("./routing-network-native-session.ts", { ...common, "node:net": { isIP }, zod: { z }, "@mmapp/contracts/mcp-routing": { routeAiProfileSchema }, + "./routing-community-context.js": context, "./registry.js": { findMcpOperation: () => ({ version: 7 }) }, + "./routing-network-native-parser.js": { ROUTING_NATIVE_LOG_BUFFER, ROUTING_NATIVE_LOG_PATH }, + "./routing-community-scoped-request.js": { assertScopedCommunityRequest: async () => "inert request boundary" }, + "./routing-profile-authority.js": { assertPersistentScopedProfileAuthority: authority, routingProfileAuthorityBindingSchema: { parse: () => { throw Error("No persistent checkpoint claimed") } } }, + "./routing-network-native-destination.js": { resolveRoutingNetworkDestinationPins: async () => [{ serverId: 1, host: endpoint.host, address: "127.0.0.1", family: 4 }] } }, native) + await loadSource("./routing-network-scoped-context.ts", { "./routing-network-native-session.js": native }, privateApi) + const create = native.createRoutingNetworkNativeSession as (options: unknown) => Promise + const abort = new AbortController() + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + endpoint.port = (router.address() as { port: number }).port + const closeRouter = () => { + router.closeAllConnections() + return new Promise(resolve => router.close(() => resolve())) + } + const probes = autoRecovery ? [{ key: "b", serverId: 1, destination: "198.51.100.3", source: "198.51.100.1", interface: "wan", probeVrf: "main" }] : [] + const session = await create({ sourceOperationId: row.id, context: { principal, connection, signal: abort.signal }, authorize: authority, deadline: now + 119000, manifest: { servers: [1], probes } }).catch(async error => { + await closeRouter() + throw error + }) + const readContext = privateApi.readRoutingNetworkScopedCommunityContext as (token: Token) => Promise + return { session, readContext: () => readContext(session.sharedContext), forged: () => readContext(JSON.parse(JSON.stringify(session.sharedContext))), + readResource: native.readRoutingNetworkSharedResource as (token: Token, server: number, path: string, body?: Record) => Promise, + bypassTail: context.readRouteCommunityContextWithSharedNativeSession as (token: Token) => Promise, + counters: () => ({ requests, wireBytes, wireRows, rawGetObjects }), bulk: (value: number) => { bulk = value }, padding: (value: number) => { padding = value }, + revoke: () => { revoked = true }, abort: () => abort.abort(), afterHttp: (hook: () => void) => { afterHttp = hook }, afterPath: (path: string, hook: () => void) => { afterPath = { path, run: hook } }, + cleanup: async () => { session.close(); await closeRouter() } } +} + +test("actual source context and snapshots share one pinned HTTP request/byte/row budget including object /get", async () => { + const f = await fixture() + try { + await f.session.readSnapshot("before"); const before = f.session.readBudget(); assert.equal(before.calls, 6) + const context = await f.readContext(); assert.equal(context.before.scopedExecutionAvailable, true); assert.equal(context.ownedTarget?.nativeId, "*f") + assert.equal(context.clients, undefined); assert.equal(context.signEvidence, undefined) + const after = f.session.readBudget(); assert(after.calls > before.calls); assert(f.counters().rawGetObjects > 0) + await f.session.readSnapshot("after-forward"); const final = f.session.readBudget(), wire = f.counters() + assert.equal(final.calls, after.calls + 6); assert.equal(final.calls, wire.requests); assert.equal(final.bytes, wire.wireBytes); assert.equal(final.rows, wire.wireRows) + assert.equal(final.closed, false) + } finally { await f.cleanup() } +}) + +for (const dimension of ["requests", "bytes", "rows"]) test("combined " + dimension + " exhaustion closes both context and later snapshot I/O", async () => { + const f = await fixture() + try { + if (dimension === "bytes") f.padding(350000) + if (dimension === "rows") f.bulk(1000) + await f.session.readSnapshot("before") + let denied = false + for (let index = 0; index < 20; index++) { try { await f.readContext() } catch { denied = true; break } } + assert(denied); assert.equal(f.session.readBudget().closed, true) + const stopped = f.counters().requests + await assert.rejects(f.readContext()); await assert.rejects(f.session.readSnapshot("after-forward")); assert.equal(f.counters().requests, stopped) + if (dimension === "requests") assert.equal(f.session.readBudget().calls, 256) + if (dimension === "bytes") assert(f.session.readBudget().bytes > 8388608) + if (dimension === "rows") assert(f.session.readBudget().rows > 10000) + } finally { await f.cleanup() } +}) + +test("JSON metadata cannot reconstruct a shared transport owner", async () => { + const f = await fixture() + try { await assert.rejects(f.forged(), /Unknown native shared context/); assert.equal(f.counters().requests, 0) } finally { await f.cleanup() } +}) + +for (const request of ["foreign-server", "write-path", "probe", "wrong-field", "extra-field"]) test("owner metadata cannot issue direct " + request + " resource I/O outside acquisition", async () => { + const f = await fixture() + try { + const path = request === "write-path" ? "/routing/filter/rule/*f" : request === "probe" ? "/tool/ping" : "/routing/bgp/connection/get" + const body = request === "wrong-field" ? { ".id": "*c1", "value-name": "password" } : request === "extra-field" ? { ".id": "*c1", "value-name": "disabled", extra: "forged" } : undefined + await assert.rejects(f.readResource(f.session.sharedContext, request === "foreign-server" ? 2 : 1, path, body)) + assert.equal(f.counters().requests, 0); assert.equal(f.session.readBudget().closed, false) + } finally { await f.cleanup() } +}) + +test("the internal context entry point cannot bypass the serialized owner API", async () => { + const f = await fixture() + try { await assert.rejects(f.bypassTail(f.session.sharedContext), /Unknown or completed shared context acquisition/); assert.equal(f.counters().requests, 0) } + finally { await f.cleanup() } +}) + +test("revocation after a real shared HTTP response closes the original native session", async () => { + const f = await fixture() + try { f.afterHttp(f.revoke); await assert.rejects(f.readContext(), /revoked/); assert.equal(f.session.readBudget().closed, true); const stopped = f.counters().requests; await assert.rejects(f.session.readSnapshot("before")); assert.equal(f.counters().requests, stopped) } + finally { await f.cleanup() } +}) + +test("configuration and snapshot acquisitions serialize on the same tail", async () => { + const f = await fixture() + try { + const work = f.readContext(), snapshot = f.session.readSnapshot("before") + await work; const calls = f.session.readBudget().calls + await snapshot; assert.equal(f.session.readBudget().calls, calls + 6) + } finally { await f.cleanup() } +}) + +test("shared transport preserves autoRecovery execution guard in actual context source", async () => { + const f = await fixture(true) + try { const context = await f.readContext(); assert.equal(context.before.scopedExecutionAvailable, false); assert.equal(context.before.scopedExecutionReason, "ROUTE_RECOVERY_EXECUTOR_UNAVAILABLE") } + finally { await f.cleanup() } +}) + +test("original cancellation during shared context wire I/O closes queued snapshot acquisition", async () => { + const f = await fixture() + try { + f.afterHttp(f.abort) + const context = f.readContext(), snapshot = f.session.readSnapshot("before") + const results = await Promise.allSettled([context, snapshot]); assert(results.every(result => result.status === "rejected")) + assert.equal(f.session.readBudget().closed, true); assert.equal(f.counters().requests, 1) + } finally { await f.cleanup() } +}) diff --git a/backend/src/modules/mcp/routing-network-scoped-context.ts b/backend/src/modules/mcp/routing-network-scoped-context.ts new file mode 100644 index 00000000..3046dbd9 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-scoped-context.ts @@ -0,0 +1,9 @@ +import { readRoutingNetworkSharedCommunityContext, type RoutingNetworkSharedContext } from "./routing-network-native-session.js" + +/** Fixed private configuration acquisition on the native session's own tail, + * pinned clients, absolute deadline, byte/request counters and row budget. + * Serialized metadata or caller clients cannot create an owner. This is a + * read-only configuration observation; it supplies no write or network proof. */ +export async function readRoutingNetworkScopedCommunityContext(context: RoutingNetworkSharedContext) { + return readRoutingNetworkSharedCommunityContext(context) +} diff --git a/backend/src/modules/mcp/routing-network-verifier-model.test.ts b/backend/src/modules/mcp/routing-network-verifier-model.test.ts new file mode 100644 index 00000000..9659e862 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-verifier-model.test.ts @@ -0,0 +1,253 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { captureRoutingNetworkManifest, captureRoutingNetworkRounds, classifyRoutingNetwork, createRoutingNetworkBudget, createRoutingNetworkClassifier, + routingNetworkManifestSchema, spendRoutingNetworkBudget, type RoutingNetworkManifest, type RoutingNetworkRound } from "./routing-network-verifier-model.js" + +const start = 1800000000000, now = start + 10000 +function fixture() { + const manifest: RoutingNetworkManifest = { + revision: 1, phase: "forward", identity: { operationId: "00000000-0000-4000-8000-000000000001", stepIndex: 0, + profileId: "00000000-0000-4000-8000-000000000002", profileVersion: 1, sourceEpoch: "endpoint1", authorityIdentity: "original1" }, + sourceCreatedAt: start, deadline: start + 120000, observationStartAt: start + 1000, maximumSampleAgeMs: 30000, serverIds: [1], + configuration: { serverId: 1, nativeId: "*1", table: "tenant", bodyIdentity: "desired-body", dependencyIdentity: "original-dependencies" }, + routes: [{ key: "route1", serverId: 1, family: "ipv4", prefix: "203.0.113.0/24", table: "tenant", sessionKey: "peer1", nextHop: "192.0.2.1", interface: "wan1", resolverTable: "main", resolutionChain: [{ address: "192.0.2.1", table: "main", interface: "wan1" }] }], + bgp: [{ key: "peer1", serverId: 1, connectionId: "*c1", instance: "default", family: "ipv4", localAddress: "192.0.2.2", remoteAddress: "192.0.2.1", remoteAsn: 65001 }], + bfd: [{ key: "bfd1", serverId: 1, applicability: "required", configurationIdentity: "bfd-config1", localAddress: "192.0.2.2", remoteAddress: "192.0.2.1", interface: "wan1", vrf: "main" }], + probes: [{ key: "probe1", serverId: 1, family: "ipv4", destination: "203.0.113.1", source: "198.51.100.1", interface: "wan1", probeVrf: "diagnostic", expectedHops: ["192.0.2.1", "203.0.113.1"] }], + logs: [{ key: "logs1", serverId: 1, generation: "boot1", sequenceMode: "contiguous-capture", afterSequence: 100, topics: ["route", "bgp", "bfd", "interface"], relatedKeys: ["route1", "peer1", "bfd1", "probe1"] }], + } + const round = (offset: number): RoutingNetworkRound => ({ identity: structuredClone(manifest.identity), startedAt: start + offset, observedAt: start + offset + 100, + configuration: structuredClone(manifest.configuration), routes: manifest.routes.map(route => ({ ...structuredClone(route), active: true, resolved: true, protocol: "bgp", resolutionChain: route.resolutionChain.map(hop => ({ ...hop, resolved: true })) })), + bgp: manifest.bgp.map(peer => ({ ...peer, state: "established" })), bfd: manifest.bfd.map(session => session.applicability === "required" ? { ...session, state: "up" } : { ...session, configured: false }), + probes: manifest.probes.map(probe => ({ ...structuredClone(probe), sent: 3, received: 3, terminal: true, trace: { kind: "observed", hops: probe.expectedHops.map((address, index) => ({ hop: index + 1, address, state: "responded" })) } })), + logs: manifest.logs.map(scope => ({ ...structuredClone(scope), coverageStartAt: start + 1000, coveredUntil: start + offset + 100, capturedAt: start + offset + 100, toSequence: 100, responseRowCount: 0, moreAvailable: false, rotationDetected: false, clockSynchronized: true, rows: [] })), + }) + const rounds = [round(2000), round(3000)] + const budget = spendRoutingNetworkBudget(createRoutingNetworkBudget(start, [start + 120000], start), "forward", now, { requests: 20, rows: 40, bytes: 40000 }).budget + return { manifest, rounds, budget } +} +const classify = (f: ReturnType) => classifyRoutingNetwork(f.manifest, f.rounds, now, f.budget) + +test("complete stable raw vector passes without claiming authority or forwarding; probeVRF differs from installation table", () => { + const result = classify(fixture()) + assert.equal(result.verdict, "passed"); assert.equal(result.completeRounds, 2) + assert.equal(result.securityProof, false); assert.equal(result.forwardingVerified, false) + assert(Object.isFrozen(result)); assert(Object.isFrozen(result.reasons)) +}) + +test("contiguous log rounds preserve the prefix and may append events after the preceding interval", () => { + const f = fixture() + const initial = { sequence: 101, timestamp: start + 1500, topics: ["bgp" as const], relatedKey: "peer1", event: "informational" as const, severity: "info" as const } + const next = { ...initial, sequence: 102, timestamp: start + 2500 } + const setRows = (index: number, rows: typeof initial[]) => { + Object.assign(f.rounds[index].logs[0], { rows, toSequence: 100 + rows.length, responseRowCount: rows.length }) + } + setRows(0, [initial]) + setRows(1, [initial, next]) + assert.equal(classify(f).verdict, "passed") + + for (const rows of [[], [{ ...initial, timestamp: start + 1600 }, next], [initial, { ...next, timestamp: start + 2100 }]]) { + setRows(1, rows) + const result = classify(f) + assert.equal(result.verdict, "unknown") + assert.deepEqual(result.reasons, ["logs:inconsistent-across-rounds"]) + } + setRows(1, [initial, next]) + f.rounds[0].logs[0].coverageStartAt-- + assert.deepEqual(classify(f).reasons, ["logs:inconsistent-across-rounds"]) +}) + +for (const failure of ["route", "bgp", "bfd", "probe", "logs"] as const) test("complete stable explicit " + failure + " failure is classified failed", () => { + const f = fixture() + for (const round of f.rounds) { + if (failure === "route") round.routes[0].resolved = false + if (failure === "bgp") round.bgp[0].state = "idle" + if (failure === "bfd" && round.bfd[0].applicability === "required") round.bfd[0].state = "down" + if (failure === "probe") { round.probes[0].received = 0; round.probes[0].trace = { kind: "not-run", reason: "terminal-ping-failure" } } + if (failure === "logs") { round.logs[0].toSequence = 101; round.logs[0].responseRowCount = 1; round.logs[0].rows = [{ sequence: 101, timestamp: start + 1500, topics: ["bgp"], relatedKey: "peer1", event: "down", severity: "error" }] } + } + assert.equal(classify(f).verdict, "failed") +}) + +for (const missing of ["routes", "bgp", "bfd", "probes", "logs"] as const) test("missing " + missing + " overrides explicit terminal ping loss", () => { + const f = fixture() + for (const round of f.rounds) { round.probes[0].received = 0; round.probes[0].trace = { kind: "not-run", reason: "terminal-ping-failure" }; round[missing] = [] } + assert.equal(classify(f).verdict, "unknown") +}) + +for (const mutation of ["source", "authority", "profile", "table", "family", "prefix", "peer", "bfd-applicability", "probeVRF", "log-generation"] as const) test("mixed identity " + mutation + " is unknown", () => { + const f = fixture(), round = f.rounds[0] + if (mutation === "source") round.identity.sourceEpoch = "other" + if (mutation === "authority") round.identity.authorityIdentity = "new-credential" + if (mutation === "profile") round.identity.profileVersion++ + if (mutation === "table") round.routes[0].table = "diagnostic" + if (mutation === "family") round.routes[0].family = "ipv6" + if (mutation === "prefix") round.routes[0].prefix = "203.0.114.0/24" + if (mutation === "peer") round.bgp[0].connectionId = "*c2" + if (mutation === "bfd-applicability") round.bfd[0] = { key: "bfd1", serverId: 1, configurationIdentity: "bfd-config1", applicability: "not-applicable", reason: "not-configured", configured: false } + if (mutation === "probeVRF") round.probes[0].probeVrf = "tenant" + if (mutation === "log-generation") round.logs[0].generation = "boot2" + assert.equal(classify(f).verdict, "unknown") +}) + +test("explicit BFD not-configured is supported only with the exact configuration applicability", () => { + const f = fixture() + f.manifest.bfd[0] = { key: "bfd1", serverId: 1, configurationIdentity: "absent1", applicability: "not-applicable", reason: "not-configured" } + f.rounds.forEach(round => { round.bfd[0] = { ...f.manifest.bfd[0], configured: false } }) + assert.equal(classify(f).verdict, "passed") + f.rounds[0].bfd = []; assert.equal(classify(f).verdict, "unknown") +}) + +test("documented BGP not-established fact is failure without inventing an idle/connect FSM state", () => { + const f = fixture(); f.rounds.forEach(round => { round.bgp[0].state = "not-established" }) + assert.equal(classify(f).verdict, "failed") + f.rounds[0].logs = []; assert.equal(classify(f).verdict, "unknown") +}) + +for (const mutation of ["truncated", "rotation", "clock", "stale-coverage", "future-capture", "rowcount", "duplicate", "unparsed", "old-event", "unordered", "empty-advanced-watermark", "unsupported-severity"] as const) test("log coverage " + mutation + " is unknown", () => { + const f = fixture(), log = f.rounds[0].logs[0] + if (mutation === "truncated") log.moreAvailable = true + if (mutation === "rotation") log.rotationDetected = true + if (mutation === "clock") log.clockSynchronized = false + if (mutation === "stale-coverage") log.coveredUntil-- + if (mutation === "future-capture") log.capturedAt = now + 1 + if (mutation === "rowcount") log.responseRowCount++ + if (mutation === "empty-advanced-watermark") log.toSequence++ + if (mutation === "unsupported-severity") { log.toSequence++; log.responseRowCount = 1; log.rows = [{ sequence: 101, timestamp: start + 1500, topics: ["bgp"], relatedKey: "peer1", event: "informational", severity: "critical" }] } + if (["duplicate", "unparsed", "old-event", "unordered"].includes(mutation)) { + log.toSequence = 101; log.rows = [{ sequence: 101, timestamp: start + 1500, topics: ["bgp"], relatedKey: "peer1", event: mutation === "unparsed" ? "unparsed" : "informational", severity: "info" }] + if (mutation === "duplicate") log.rows.push(structuredClone(log.rows[0])) + if (mutation === "old-event") log.rows[0].timestamp = start + if (mutation === "unordered") log.rows = [{ ...log.rows[0], sequence: 102 }, log.rows[0]] + log.responseRowCount = log.rows.length + } + assert.equal(classify(f).verdict, "unknown") +}) + +test("unrelated keys, cross-server log scope, omitted stage keys and topics cannot pass empty log coverage", () => { + for (const mutation of ["unrelated", "cross-server", "missing-key", "missing-topic"]) { + const f = fixture() + if (mutation === "unrelated") f.manifest.logs[0].relatedKeys = ["unrelated"] + if (mutation === "cross-server") { f.manifest.serverIds.push(2); f.manifest.logs[0].serverId = 2 } + if (mutation === "missing-key") f.manifest.logs[0].relatedKeys.pop() + if (mutation === "missing-topic") f.manifest.logs[0].topics = ["bgp"] + assert.equal(classify(f).verdict, "unknown", mutation) + } +}) + +test("unsupported probe/BGP/BFD states and partial counters never become a terminal failed result", () => { + for (const mutation of ["bgp", "bfd", "trace-timeout", "partial-ping", "sent-zero", "received-over-sent"]) { + const f = fixture(), round = f.rounds[0] + if (mutation === "bgp") round.bgp[0].state = "unknown" + if (mutation === "bfd" && round.bfd[0].applicability === "required") round.bfd[0].state = "unknown" + if (mutation === "trace-timeout" && round.probes[0].trace.kind === "observed") round.probes[0].trace.hops[0].state = "timeout" + if (mutation === "partial-ping") round.probes[0].terminal = false + if (mutation === "sent-zero") round.probes[0].sent = 0 + if (mutation === "received-over-sent") round.probes[0].sent = 2 + assert.equal(classify(f).verdict, "unknown", mutation) + } +}) + +test("at least two complete nonoverlapping stable rounds are required; no cherry-picking unknown rounds", () => { + const f = fixture() + assert.equal(classifyRoutingNetwork(f.manifest, f.rounds.slice(0, 1), now, f.budget).verdict, "unknown") + f.rounds[1].routes[0].active = false; assert.equal(classify(f).verdict, "unknown") + const g = fixture(); g.rounds[1].startedAt = g.rounds[0].observedAt - 1; assert.equal(classify(g).verdict, "unknown") + const h = fixture(); h.rounds.push({ ...structuredClone(h.rounds[1]), startedAt: start + 4000, observedAt: start + 4100 }); h.rounds[0].bgp[0].state = "unknown" + assert.equal(classify(h).verdict, "unknown") +}) + +test("stale, future and reversed sample times fail closed", () => { + for (const mutation of ["future", "stale", "backwards", "before-intent", "deadline"]) { + const f = fixture() + if (mutation === "future") f.rounds[0].observedAt = now + 1 + if (mutation === "stale") f.manifest.maximumSampleAgeMs = 1 + if (mutation === "backwards") f.rounds[0].observedAt = f.rounds[0].startedAt - 1 + if (mutation === "before-intent") f.rounds[0].startedAt = start + if (mutation === "deadline") f.rounds[0].observedAt = f.manifest.deadline + assert.equal(classify(f).verdict, "unknown", mutation) + } +}) + +test("strict schema rejects supplied verdict flags, missing raw data, ambiguous keys and response bounds", () => { + const f = fixture() + for (const raw of [{ ...f.rounds[0], stagePassed: true }, { ...f.rounds[0], verdict: "passed" }, { ...f.rounds[0], routes: undefined }, { ...f.rounds[0], bgp: [...f.rounds[0].bgp, f.rounds[0].bgp[0]] }, { ...f.rounds[0], logs: [{ ...f.rounds[0].logs[0], rows: Array(201).fill({}) }] }]) { + assert.equal(classifyRoutingNetwork(f.manifest, [raw, f.rounds[1]], now, f.budget).verdict, "unknown") + } + f.manifest.routes.push({ ...f.manifest.routes[0], key: "duplicate-target" }); assert.equal(routingNetworkManifestSchema.safeParse(f.manifest).success, false) +}) + +test("CIDR must be canonical network address and canonical IPv6 prevents alias recursion cycles", () => { + for (const value of ["203.0.113.1/24", "203.0.113.0/024", "2001:0db8::/64", "2001:db8::1/64", "::ffff:192.0.2.0/120"]) { + const f = fixture(); f.manifest.routes[0].prefix = value; assert.equal(classify(f).verdict, "unknown", value) + } + const f = fixture(); f.manifest.routes[0].resolutionChain = [{ address: "192.0.2.1", table: "main", interface: "wan1" }, { address: "192.0.2.1", table: "main", interface: "wan1" }] + assert.equal(classify(f).verdict, "unknown") + const g = fixture(); g.manifest.routes[0].resolutionChain[0].address = "2001:0db8:0:0:0:0:0:1"; assert.equal(classify(g).verdict, "unknown") +}) + +test("AFI mismatches, missing BGP mapping and out-of-scope observations are unknown", () => { + const f = fixture(); f.manifest.routes[0].family = "ipv6"; assert.equal(classify(f).verdict, "unknown") + const g = fixture(); g.manifest.routes[0].sessionKey = "absent"; assert.equal(classify(g).verdict, "unknown") + const h = fixture(); h.rounds[0].routes[0].serverId = 2; assert.equal(classify(h).verdict, "unknown") + const b = fixture(); if (b.rounds[0].bfd[0].applicability === "required") b.rounds[0].bfd[0].remoteAddress = "2001:db8::1" + assert.equal(classify(b).verdict, "unknown", "raw BFD Up cannot accept a cross-family pair") +}) + +test("canonical IPv6 vector passes while compressed/expanded aliases and mismatched BFD AFI remain unsupported", () => { + const f = fixture() + for (const value of [f.manifest, ...f.rounds]) { + value.routes[0].family = "ipv6"; value.routes[0].prefix = "2001:db8:10::/64"; value.routes[0].nextHop = "2001:db8::1"; value.routes[0].resolutionChain[0].address = "2001:db8::1" + value.bgp[0].family = "ipv6"; value.bgp[0].localAddress = "2001:db8::2"; value.bgp[0].remoteAddress = "2001:db8::1" + if (value.bfd[0].applicability === "required") { value.bfd[0].localAddress = "2001:db8::2"; value.bfd[0].remoteAddress = "2001:db8::1" } + value.probes[0].family = "ipv6"; value.probes[0].destination = "2001:db8:10::1"; value.probes[0].source = "2001:db8:20::1"; value.probes[0].expectedHops = ["2001:db8::1", "2001:db8:10::1"] + } + f.rounds.forEach(round => { round.probes[0].trace = { kind: "observed", hops: [{ hop: 1, address: "2001:db8::1", state: "responded" }, { hop: 2, address: "2001:db8:10::1", state: "responded" }] } }) + assert.equal(classify(f).verdict, "passed") + const alias = structuredClone(f) + alias.rounds[0].routes[0].resolutionChain.push({ address: "2001:0db8:0:0:0:0:0:1", table: "main", interface: "wan1", resolved: true }) + assert.equal(classify(alias).verdict, "unknown") + if (f.manifest.bfd[0].applicability === "required") f.manifest.bfd[0].remoteAddress = "192.0.2.1" + assert.equal(classify(f).verdict, "unknown") +}) + +test("budget exact fit is cumulative across phases and cannot return to forward", () => { + let budget = createRoutingNetworkBudget(start, [start + 120000], start, { requests: 3, bytes: 30, rows: 3 }) + let spent = spendRoutingNetworkBudget(budget, "forward", start + 1, { requests: 2, bytes: 20, rows: 2 }); assert(spent.accepted); budget = spent.budget + spent = spendRoutingNetworkBudget(budget, "inverse", start + 2, { requests: 1, bytes: 10, rows: 1 }); assert(spent.accepted) + assert.deepEqual(spent.budget.used, { requests: 3, bytes: 30, rows: 3 }); assert.equal(spent.budget.deadline, start + 120000) + assert.equal(spendRoutingNetworkBudget(spent.budget, "inverse", start + 3, { requests: 1, bytes: 0, rows: 0 }).accepted, false) + assert.equal(spendRoutingNetworkBudget(spent.budget, "forward", start + 3, { requests: 0, bytes: 0, rows: 0 }).accepted, false) +}) + +test("short by one requests/bytes/rows, clock rollback, deadline and malformed spend permanently exhaust the budget value", () => { + for (const work of [{ requests: 2, bytes: 0, rows: 0 }, { requests: 0, bytes: 2, rows: 0 }, { requests: 0, bytes: 0, rows: 2 }, { requests: -1, bytes: 0, rows: 0 }]) { + const spent = spendRoutingNetworkBudget(createRoutingNetworkBudget(start, [start + 1000], start, { requests: 1, bytes: 1, rows: 1 }), "forward", start + 1, work) + assert.equal(spent.accepted, false); assert.equal(spent.budget.exhausted, true) + assert.equal(spendRoutingNetworkBudget(spent.budget, "inverse", start + 2, { requests: 0, bytes: 0, rows: 0 }).accepted, false) + } + const budget = createRoutingNetworkBudget(start, [start + 1000], start + 10) + assert.equal(spendRoutingNetworkBudget(budget, "forward", start + 9, { requests: 0, bytes: 0, rows: 0 }).accepted, false) + assert.equal(spendRoutingNetworkBudget(budget, "inverse", start + 1000, { requests: 0, bytes: 0, rows: 0 }).accepted, false) +}) + +test("all original deadlines cap120seconds; malformed/future initialization and expired classification fail closed", () => { + assert.equal(createRoutingNetworkBudget(start, [start + 300000], start).deadline, start + 120000) + assert.equal(createRoutingNetworkBudget(start, [start + 300000, start + 500], start).deadline, start + 500) + for (const deadlines of [[], [NaN], [Infinity], [start]]) assert.throws(() => createRoutingNetworkBudget(start, deadlines, start)) + assert.throws(() => createRoutingNetworkBudget(start, [start + 1000], start - 1)) + const f = fixture(); assert.equal(classifyRoutingNetwork(f.manifest, f.rounds, f.manifest.deadline, f.budget).verdict, "unknown") +}) + +test("deep capture isolates external manifest/round/limit/deadline mutation; no capability or security proof is minted", () => { + const f = fixture(), classifier = createRoutingNetworkClassifier(f.manifest), captured = captureRoutingNetworkRounds(f.rounds) + const limits = { requests: 4, bytes: 50, rows: 4 }, deadlines = [start + 1000] + const budget = createRoutingNetworkBudget(start, deadlines, start, limits) + f.manifest.configuration.bodyIdentity = "tampered"; f.rounds[0].routes[0].active = false + limits.requests = 400; deadlines[0] = start + 300000 + assert.equal(classifier.classify(captured, now, f.budget).verdict, "passed") + assert.equal(budget.limits.requests, 4); assert.equal(budget.deadline, start + 1000) + assert.throws(() => { captured[0].configuration.bodyIdentity = "tampered" }, TypeError) + assert.throws(() => { classifier.manifest.routes[0].resolutionChain.push({ address: "192.0.2.3", table: "main", interface: "wan1" }) }, TypeError) + assert(Object.isFrozen(captureRoutingNetworkManifest(fixture().manifest).logs[0].relatedKeys)) +}) diff --git a/backend/src/modules/mcp/routing-network-verifier-model.ts b/backend/src/modules/mcp/routing-network-verifier-model.ts new file mode 100644 index 00000000..0f31cc78 --- /dev/null +++ b/backend/src/modules/mcp/routing-network-verifier-model.ts @@ -0,0 +1,240 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { canonicalBgpPolicyPrefix } from "../../services/bgp-community-policy-model.js" + +/** Inert classification only. These values are not signed observations, + * authority, a native grant, or proof that a caller collected complete data. */ +const key = z.string().min(1).max(128).regex(/^[a-zA-Z0-9_.:*@/-]+$/) +const clock = z.number().int().safe().nonnegative() +const ip = z.string().max(45).refine(value => { + try { return !!isIP(value) && canonicalBgpPolicyPrefix(value + "/" + (isIP(value) === 4 ? 32 : 128)).split("/")[0] === value } catch { return false } +}) +const family = z.enum(["ipv4", "ipv6"]) +const named = z.string().min(1).max(128).regex(/^[^\s\x00-\x1f]+$/) +const identity = z.object({ operationId: z.string().uuid(), stepIndex: z.literal(0), profileId: z.string().uuid(), profileVersion: z.number().int().positive().safe(), sourceEpoch: key, authorityIdentity: key }).strict() +const prefix = z.string().max(49).refine(value => { + try { return canonicalBgpPolicyPrefix(value) === value } catch { return false } +}) +const hop = z.object({ address: ip, table: named, interface: named }).strict() +const route = z.object({ key, serverId: z.number().int().positive(), family, prefix, table: named, sessionKey: key, + nextHop: ip, interface: named, resolverTable: named, resolutionChain: z.array(hop).min(1).max(8) }).strict() +const bgp = z.object({ key, serverId: z.number().int().positive(), connectionId: key, instance: named, family, + localAddress: ip, remoteAddress: ip, remoteAsn: z.number().int().min(1).max(4294967295) }).strict() +const bfdCommon = { key, serverId: z.number().int().positive(), configurationIdentity: key } +const bfd = z.discriminatedUnion("applicability", [ + z.object({ ...bfdCommon, applicability: z.literal("required"), localAddress: ip, remoteAddress: ip, interface: named, vrf: named }).strict(), + z.object({ ...bfdCommon, applicability: z.literal("not-applicable"), reason: z.literal("not-configured") }).strict(), +]) +const probe = z.object({ key, serverId: z.number().int().positive(), family, destination: ip, source: ip, interface: named, probeVrf: named, + expectedHops: z.array(ip).min(1).max(16) }).strict() +const topic = z.enum(["bgp", "bfd", "route", "interface"]) +const logScope = z.object({ key, serverId: z.number().int().positive(), generation: key, sequenceMode: z.literal("contiguous-capture"), afterSequence: clock, + topics: z.array(topic).min(1).max(4), relatedKeys: z.array(key).min(1).max(64) }).strict() +const configuration = z.object({ serverId: z.number().int().positive(), nativeId: key, table: named, bodyIdentity: key, dependencyIdentity: key }).strict() +const unique = (values: T[], identityOf: (value: T) => string) => new Set(values.map(identityOf)).size === values.length +const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b) +const sameIp = (a: string, b: string) => isIP(a) === isIP(b) && a.toLowerCase() === b.toLowerCase() + +export const routingNetworkManifestSchema = z.object({ revision: z.literal(1), phase: z.enum(["forward", "inverse"]), identity, + sourceCreatedAt: clock, deadline: clock, observationStartAt: clock, maximumSampleAgeMs: z.number().int().min(1).max(30000), + serverIds: z.array(z.number().int().positive()).min(1).max(32), configuration, + routes: z.array(route).min(1).max(32), bgp: z.array(bgp).min(1).max(32), bfd: z.array(bfd).min(1).max(32), + probes: z.array(probe).min(1).max(8), logs: z.array(logScope).min(1).max(32), +}).strict().superRefine((value, ctx) => { + const reject = (message: string) => ctx.addIssue({ code: "custom", message }) + if (value.deadline <= value.sourceCreatedAt || value.deadline > value.sourceCreatedAt + 120000 || value.observationStartAt < value.sourceCreatedAt || value.observationStartAt >= value.deadline) reject("invalid original execution window") + if (!unique(value.serverIds, String) || !same(value.serverIds, [...value.serverIds].sort((a, b) => a - b))) reject("scope must be sorted and unique") + const groups: Array> = [value.routes, value.bgp, value.bfd, value.probes, value.logs] + if (groups.some(group => !unique(group, entry => entry.key))) reject("duplicate manifest keys") + if (!value.serverIds.includes(value.configuration.serverId) || groups.some(group => group.some(entry => !value.serverIds.includes(entry.serverId)))) reject("manifest outside scope") + if (!unique(groups.flat(), entry => entry.key)) reject("stage keys must be globally unique") + if (!unique(value.routes, entry => [entry.serverId, entry.family, entry.prefix, entry.table].join("|")) || !unique(value.bgp, entry => [entry.serverId, entry.connectionId, entry.instance, entry.family].join("|")) || !unique(value.logs, entry => String(entry.serverId))) reject("ambiguous target mapping") + const expectedKeys = new Set([...value.routes, ...value.bgp, ...value.bfd, ...value.probes].map(entry => entry.key)) + if (value.logs.some(scope => !unique(scope.topics, String) || !unique(scope.relatedKeys, String) || scope.relatedKeys.some(id => !expectedKeys.has(id)))) reject("log selectors are ambiguous") + if (value.serverIds.some(id => !value.logs.some(scope => scope.serverId === id))) reject("every source requires log coverage") + for (const scope of value.logs) { + const serverKeys = [...value.routes, ...value.bgp, ...value.bfd, ...value.probes].filter(entry => entry.serverId === scope.serverId).map(entry => entry.key).sort() + if (!same([...scope.relatedKeys].sort(), serverKeys)) reject("logs must cover exact same-server stage keys") + const requiredTopics = [value.routes.some(entry => entry.serverId === scope.serverId) ? "route" : null, + value.bgp.some(entry => entry.serverId === scope.serverId) ? "bgp" : null, + value.bfd.some(entry => entry.serverId === scope.serverId && entry.applicability === "required") ? "bfd" : null, + value.probes.some(entry => entry.serverId === scope.serverId) ? "interface" : null].filter(Boolean) + if (requiredTopics.some(required => !scope.topics.some(actual => actual === required))) reject("missing relevant log topics") + } + for (const entry of value.routes) { + if (isIP(entry.prefix.split("/")[0]) !== (entry.family === "ipv4" ? 4 : 6) || isIP(entry.nextHop) !== (entry.family === "ipv4" ? 4 : 6) || entry.resolutionChain.some(link => isIP(link.address) !== (entry.family === "ipv4" ? 4 : 6))) reject("route AFI mismatch") + if (!value.bgp.some(session => session.key === entry.sessionKey && session.serverId === entry.serverId && session.family === entry.family)) reject("route has no exact BGP session") + if (!unique(entry.resolutionChain, link => link.address.toLowerCase() + "/" + link.table)) reject("recursive cycle") + const last = entry.resolutionChain.at(-1)! + if (entry.resolutionChain[0].table !== entry.resolverTable || last.address !== entry.nextHop || last.interface !== entry.interface) reject("resolution chain does not terminate at exact nexthop") + } + for (const entry of value.bfd) if (entry.applicability === "required" && isIP(entry.localAddress) !== isIP(entry.remoteAddress)) reject("BFD AFI mismatch") + for (const entry of [...value.bgp, ...value.probes]) { + const addresses = "destination" in entry ? [entry.destination, entry.source, ...entry.expectedHops] : [entry.localAddress, entry.remoteAddress] + if (addresses.some(address => isIP(address) !== (entry.family === "ipv4" ? 4 : 6))) reject("probe/session AFI mismatch") + if ("destination" in entry && !sameIp(entry.expectedHops.at(-1)!, entry.destination)) reject("trace must end at exact destination") + if ("destination" in entry && !unique(entry.expectedHops, String)) reject("ambiguous expected probe path") + } +}) +export type RoutingNetworkManifest = z.infer + +const observedRoute = route.extend({ active: z.boolean(), resolved: z.boolean(), protocol: z.enum(["bgp", "static", "ospf", "connected"]), + resolutionChain: z.array(hop.extend({ resolved: z.boolean() }).strict()).min(1).max(8) }).strict() +const observedBgp = bgp.extend({ state: z.enum(["established", "not-established", "idle", "connect", "active", "opensent", "openconfirm", "disabled", "unknown"]) }).strict() +const observedBfd = z.discriminatedUnion("applicability", [ + z.object({ ...bfdCommon, applicability: z.literal("required"), localAddress: ip, remoteAddress: ip, interface: named, vrf: named, + state: z.enum(["up", "down", "init", "admin-down", "unknown"]) }).strict(), + z.object({ ...bfdCommon, applicability: z.literal("not-applicable"), reason: z.literal("not-configured"), configured: z.literal(false) }).strict(), +]).refine(value => value.applicability !== "required" || isIP(value.localAddress) === isIP(value.remoteAddress), "BFD AFI mismatch") +const trace = z.discriminatedUnion("kind", [ + z.object({ kind: z.literal("observed"), hops: z.array(z.object({ hop: z.number().int().min(1).max(16), address: ip.nullable(), state: z.enum(["responded", "timeout", "unsupported"]) }).strict()).min(1).max(16) }).strict(), + z.object({ kind: z.literal("not-run"), reason: z.literal("terminal-ping-failure") }).strict(), +]) +const observedProbe = probe.extend({ sent: z.number().int().min(0).max(3), received: z.number().int().min(0).max(3), terminal: z.boolean(), trace }).strict() +const logRow = z.object({ sequence: clock, timestamp: clock, topics: z.array(topic).min(1).max(4), relatedKey: key, + event: z.enum(["up", "down", "route-withdrawn", "route-unresolved", "authentication-failure", "informational", "unparsed"]), + severity: z.enum(["info", "warning", "error", "critical"]) }).strict() +const observedLogs = logScope.extend({ coverageStartAt: clock, coveredUntil: clock, capturedAt: clock, toSequence: clock, + responseRowCount: z.number().int().min(0).max(200), moreAvailable: z.boolean(), rotationDetected: z.boolean(), clockSynchronized: z.boolean(), + rows: z.array(logRow).max(200) }).strict() +export const routingNetworkRoundSchema = z.object({ identity, startedAt: clock, observedAt: clock, configuration, + routes: z.array(observedRoute).max(32), bgp: z.array(observedBgp).max(32), bfd: z.array(observedBfd).max(32), + probes: z.array(observedProbe).max(8), logs: z.array(observedLogs).max(32) }).strict() +export type RoutingNetworkRound = z.infer + +/** Aggregate counters only. Actual per-response wire-byte/page limits remain + * the future native adapter's responsibility and are not proved here. */ +export const routingNetworkBudgetLimits = Object.freeze({ requests: 256, bytes: 8 * 1048576, rows: 10000 }) +const workSchema = z.object({ requests: clock, bytes: clock, rows: clock }).strict() +const limitsSchema = z.object({ requests: z.number().int().min(1).max(256), bytes: z.number().int().min(1).max(8 * 1048576), rows: z.number().int().min(1).max(10000) }).strict() +const budgetSchema = z.object({ sourceCreatedAt: clock, deadline: clock, lastAt: clock, phase: z.enum(["forward", "inverse"]), limits: limitsSchema, + used: workSchema, exhausted: z.boolean() }).strict().refine(value => value.deadline > value.sourceCreatedAt && value.deadline <= value.sourceCreatedAt + 120000 && value.lastAt >= value.sourceCreatedAt && + value.used.requests <= value.limits.requests && value.used.bytes <= value.limits.bytes && value.used.rows <= value.limits.rows) +export type RoutingNetworkBudget = z.infer +const frozenBudget = (value: RoutingNetworkBudget): Readonly => Object.freeze({ ...value, used: Object.freeze({ ...value.used }), limits: Object.freeze({ ...value.limits }) }) +function deepFreeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(deepFreeze); Object.freeze(value) } + return value +} +export function captureRoutingNetworkManifest(raw: unknown): RoutingNetworkManifest { return deepFreeze(routingNetworkManifestSchema.parse(raw)) } +export function captureRoutingNetworkRounds(raw: unknown): RoutingNetworkRound[] { return deepFreeze(z.array(routingNetworkRoundSchema).min(2).max(3).parse(raw)) } +export function createRoutingNetworkClassifier(raw: unknown) { + const manifest = captureRoutingNetworkManifest(raw) + return Object.freeze({ manifest, classify: (rounds: unknown, now: number, budget: unknown) => classifyRoutingNetwork(manifest, rounds, now, budget) }) +} + +/** No restoration or grant semantics. Integration must retain this original + * state durably; constructing another pure value does not allocate work. */ +export function createRoutingNetworkBudget(sourceCreatedAt: number, originalDeadlines: number[], now: number, limits = routingNetworkBudgetLimits): Readonly { + clock.parse(sourceCreatedAt); clock.parse(now) + z.array(clock).min(1).max(16).parse(originalDeadlines) + const deadline = Math.min(sourceCreatedAt + 120000, ...originalDeadlines) + if (now >= deadline) throw Error("Original network budget expired") + return frozenBudget(budgetSchema.parse({ sourceCreatedAt, deadline, lastAt: now, phase: "forward", limits, used: { requests: 0, bytes: 0, rows: 0 }, exhausted: false })) +} + +/** Account before work. A denied spend permanently exhausts this value. */ +export function spendRoutingNetworkBudget(raw: RoutingNetworkBudget, phase: "forward" | "inverse", now: number, work: z.infer) { + const budget = budgetSchema.parse(raw), parsed = workSchema.safeParse(work) + const denied = !parsed.success || !Number.isSafeInteger(now) || now < budget.lastAt || now >= budget.deadline || budget.exhausted || budget.phase === "inverse" && phase !== "inverse" || !["forward", "inverse"].includes(phase) + const next = parsed.success ? { requests: budget.used.requests + parsed.data.requests, bytes: budget.used.bytes + parsed.data.bytes, rows: budget.used.rows + parsed.data.rows } : budget.used + const exceeded = Object.keys(budget.limits).some(key => next[key as keyof typeof next] > budget.limits[key as keyof typeof next] || !Number.isSafeInteger(next[key as keyof typeof next])) + if (denied || exceeded) return { accepted: false, budget: frozenBudget({ ...budget, exhausted: true }) } + return { accepted: true, budget: frozenBudget({ ...budget, phase, lastAt: now, used: next }) } +} + +type Verdict = "passed" | "failed" | "unknown" +type RoundResult = { verdict: Verdict; reasons: string[]; stability: string } +const byKey = (entries: T[]) => new Map(entries.map(entry => [entry.key, entry])) +function inspectRound(manifest: RoutingNetworkManifest, sample: RoutingNetworkRound, now: number): RoundResult { + const unknown: string[] = [], failed: string[] = [], stable: unknown[] = [] + const uncertain = (reason: string) => unknown.push(reason) + const bad = (reason: string) => failed.push(reason) + if (!same(sample.identity, manifest.identity)) uncertain("mixed-source-identity") + if (sample.startedAt < manifest.observationStartAt || sample.observedAt < sample.startedAt || sample.observedAt > now || sample.observedAt >= manifest.deadline || now - sample.startedAt > manifest.maximumSampleAgeMs) uncertain("stale-future-or-outside-window") + if (!same(sample.configuration, manifest.configuration)) uncertain("configuration-or-dependency-drift") + const compare = (expected: T[], actual: U[], stage: string, inspect: (wanted: T, got: U) => void) => { + if (expected.length !== actual.length || !unique(actual, entry => entry.key)) { uncertain(stage + ":incomplete-or-duplicate"); return } + const entries = byKey(actual) + for (const wanted of expected) { const got = entries.get(wanted.key); if (!got) uncertain(stage + ":missing"); else inspect(wanted, got) } + } + compare(manifest.routes, sample.routes, "routes", (wanted, got) => { + if (wanted.serverId !== got.serverId || wanted.family !== got.family || wanted.prefix !== got.prefix || wanted.table !== got.table) { uncertain("routes:mixed-identity"); return } + if (!unique(got.resolutionChain, link => link.address.toLowerCase() + "/" + link.table)) { uncertain("routes:recursive-cycle"); return } + const chain = got.resolutionChain.map(({ resolved: _resolved, ...link }) => link) + const healthy = got.active && got.resolved && got.protocol === "bgp" && got.sessionKey === wanted.sessionKey && sameIp(got.nextHop, wanted.nextHop) && got.interface === wanted.interface && got.resolverTable === wanted.resolverTable && same(chain, wanted.resolutionChain) && got.resolutionChain.every(link => link.resolved) + if (!healthy) bad("routes:unconverged") + stable.push(["route", wanted.key, healthy, got.nextHop, got.interface, got.active, got.resolved, got.protocol, got.sessionKey, got.resolverTable, got.resolutionChain]) + }) + compare(manifest.bgp, sample.bgp, "bgp", (wanted, got) => { + const { state, ...binding } = got + if (!same(wanted, binding)) { uncertain("bgp:mixed-identity"); return } + if (state === "unknown") uncertain("bgp:unsupported-state"); else if (state !== "established") bad("bgp:not-established") + stable.push(["bgp", wanted.key, state]) + }) + compare(manifest.bfd, sample.bfd, "bfd", (wanted, got) => { + const { state: _state, configured: _configured, ...binding } = got as typeof got & { state?: string; configured?: boolean } + if (!same(wanted, binding)) { uncertain("bfd:applicability-or-identity-drift"); return } + if (got.applicability === "required") { + if (got.state === "unknown") uncertain("bfd:unsupported-state"); else if (got.state !== "up") bad("bfd:not-up") + stable.push(["bfd", got.key, got.state]) + } else stable.push(["bfd", got.key, "not-configured", got.configurationIdentity]) + }) + compare(manifest.probes, sample.probes, "probes", (wanted, got) => { + const { sent, received, terminal, trace, ...binding } = got + if (!same(wanted, binding)) { uncertain("probe:mixed-identity"); return } + if (!terminal || sent !== 3 || received > sent) { uncertain("probe:incomplete-counts"); return } + if (received < sent) { + if (trace.kind !== "not-run") uncertain("probe:inconsistent-terminal-failure"); else bad("probe:terminal-loss") + stable.push(["probe", wanted.key, sent, received, "terminal-loss"]); return + } + if (trace.kind !== "observed" || trace.hops.some((hop, index) => hop.hop !== index + 1 || hop.state !== "responded" || !hop.address)) { uncertain("probe:incomplete-trace"); return } + const addresses = trace.hops.map(hop => hop.address) + if (!same(addresses, wanted.expectedHops)) bad("probe:unexpected-complete-path") + stable.push(["probe", wanted.key, sent, received, addresses]) + }) + compare(manifest.logs, sample.logs, "logs", (wanted, got) => { + const { coverageStartAt, coveredUntil, capturedAt, toSequence, responseRowCount, moreAvailable, rotationDetected, clockSynchronized, rows, ...binding } = got + if (!same(wanted, binding)) { uncertain("logs:mixed-identity-or-generation"); return } + if (coverageStartAt > manifest.observationStartAt || coveredUntil < sample.observedAt || capturedAt < coveredUntil || capturedAt > now || coveredUntil >= manifest.deadline || toSequence < wanted.afterSequence || moreAvailable || rotationDetected || !clockSynchronized || responseRowCount !== rows.length || !unique(rows, row => String(row.sequence))) { uncertain("logs:incomplete-interval"); return } + // This model supports only a declared contiguous unfiltered capture + // sequence. Raw RouterOS IDs/cursor gaps need an adapter, never guessing. + if (toSequence - wanted.afterSequence !== rows.length || rows.some((row, index) => row.sequence !== wanted.afterSequence + index + 1 || row.timestamp < coverageStartAt || row.timestamp > coveredUntil || !unique(row.topics, String))) { uncertain("logs:malformed-interval"); return } + const relevant = rows.filter(row => wanted.relatedKeys.includes(row.relatedKey) && row.topics.some(topic => wanted.topics.includes(topic))) + if (relevant.some(row => row.event === "unparsed")) uncertain("logs:unsupported-relevant-event") + if (relevant.some(row => ["error", "critical"].includes(row.severity) && ["up", "informational"].includes(row.event))) uncertain("logs:unsupported-event-severity") + const blocking = relevant.filter(row => ["down", "route-withdrawn", "route-unresolved", "authentication-failure"].includes(row.event)) + if (blocking.length) bad("logs:blocking-event") + stable.push(["logs", wanted.key, [...new Set(blocking.map(row => row.relatedKey + ":" + row.event))].sort()]) + }) + return { verdict: unknown.length ? "unknown" : failed.length ? "failed" : "passed", reasons: [...unknown, ...failed], stability: JSON.stringify(stable) } +} + +/** No stagePassed flags or caller verdict are accepted. Even a passed result + * is only a classification of supplied data, never authenticated evidence. */ +export function classifyRoutingNetwork(manifestRaw: unknown, roundsRaw: unknown, now: number, budgetRaw: unknown): { verdict: Verdict; reasons: string[]; completeRounds: number; securityProof: false; forwardingVerified: false } { + const result = (verdict: Verdict, reasons: string[], completeRounds = 0) => deepFreeze({ verdict, reasons: [...new Set(reasons)], completeRounds, securityProof: false as const, forwardingVerified: false as const }) + const manifest = routingNetworkManifestSchema.safeParse(manifestRaw), rounds = z.array(routingNetworkRoundSchema).min(2).max(3).safeParse(roundsRaw), budget = budgetSchema.safeParse(budgetRaw) + if (!manifest.success || !rounds.success || !budget.success || !Number.isSafeInteger(now)) return result("unknown", ["malformed-or-unsupported-input"]) + if (budget.data.exhausted || budget.data.sourceCreatedAt !== manifest.data.sourceCreatedAt || budget.data.deadline !== manifest.data.deadline || budget.data.phase !== manifest.data.phase || budget.data.lastAt > now || now >= manifest.data.deadline) return result("unknown", ["budget-or-original-window-unavailable"]) + if (rounds.data.some((round, index) => index > 0 && round.startedAt < rounds.data[index - 1].observedAt)) return result("unknown", ["overlapping-or-reordered-rounds"]) + const inspected = rounds.data.map(round => inspectRound(manifest.data, round, now)) + if (inspected.some(round => round.verdict === "unknown")) return result("unknown", inspected.flatMap(round => round.reasons), inspected.filter(round => round.verdict !== "unknown").length) + // Each round declares a contiguous interval from the same source cursor. + // Later rounds must preserve the events already observed in that interval. + for (let index = 1; index < rounds.data.length; index++) { + const previous = rounds.data[index - 1], current = rounds.data[index] + const currentLogs = new Map(current.logs.map(log => [log.key, log])) + for (const earlier of previous.logs) { + const later = currentLogs.get(earlier.key) + if (!later || later.toSequence < earlier.toSequence || later.coverageStartAt > earlier.coverageStartAt || + later.coveredUntil < earlier.coveredUntil || + earlier.rows.some((row, rowIndex) => !same(row, later.rows[rowIndex])) || + later.rows.slice(earlier.rows.length).some(row => row.timestamp <= earlier.coveredUntil)) { + return result("unknown", ["logs:inconsistent-across-rounds"]) + } + } + } + if (!inspected.every(round => round.stability === inspected[0].stability)) return result("unknown", ["unstable-complete-rounds"], inspected.length) + return result(inspected.some(round => round.verdict === "failed") ? "failed" : "passed", inspected.flatMap(round => round.reasons), inspected.length) +} diff --git a/backend/src/modules/mcp/routing-observation-source.test.ts b/backend/src/modules/mcp/routing-observation-source.test.ts new file mode 100644 index 00000000..1ba6c3d0 --- /dev/null +++ b/backend/src/modules/mcp/routing-observation-source.test.ts @@ -0,0 +1,86 @@ +import assert from "node:assert/strict" +import { mkdtemp, rm, readFile, writeFile, unlink } from "node:fs/promises" +import { join, resolve, dirname, basename } from "node:path" +import { tmpdir } from "node:os" +import { randomUUID, randomBytes } from "node:crypto" +import type { PoolClient } from "pg" +import { pool, closePool } from "../../db/index.js" +import { createRoutingSourceContext, type RoutingSourceRecord } from "./routing-observation-source.js" +import { invalidateMcpAccess, accessGeneration } from "./security.js" + +const directory = await mkdtemp(join(tmpdir(), "mm-routing-source-")), previous = process.env.MCP_STATE_DIR +process.env.MCP_STATE_DIR = directory +const originalQuery = pool.query, originalConnect = pool.connect +let realSqlCalls = 0, credentialQueries = 0, endpointQueryHook: (() => Promise) | undefined +pool.query = (() => { realSqlCalls++; throw Error("Routing source unit cannot use real database SQL") }) as never +pool.connect = (() => { realSqlCalls++; throw Error("Routing source unit cannot acquire a database connection") }) as never +let originalKey: Buffer | undefined, rotatedKey: Buffer | undefined +try { + const endpoint = { id: 1, host: "127.0.0.1", port: 80, username: "fixture", password: "PRIVATE_FIXTURE", useSsl: false, verifySsl: true, enabled: true, routingEndpointEpoch: randomUUID() } + const peer = { ...endpoint, id: 2, routingEndpointEpoch: randomUUID() } + const endpoints = [endpoint, peer], context = await createRoutingSourceContext(endpoints) + const row: RoutingSourceRecord = { id: randomUUID(), srcServerId: 1, dstServerId: 2, srcInterface: "gre", dstInterface: "gre", family: "ipv6", table: "main", pingMs: 1, lossPct: 0, dlMbps: null, ulMbps: null, pingAt: "2026-10-21T10:00:00.123Z", bandwidthAt: null, contextVerified: true, source: "routeros", createdAt: "2026-10-21T10:00:00.123456Z" } + const proof = context.signRecord(row) + assert.equal(context.verifyRecord(row, proof), true) + assert.equal((await createRoutingSourceContext([...endpoints].reverse())).verifyRecord(row, proof), true) + assert.equal(context.verifyRecord({ ...row, pingAt: "2026-10-21T10:00:00.123000Z" }, proof), true) + for (const changed of [{ pingMs: 0 }, { lossPct: 1 }, { dlMbps: 1 }, { srcInterface: "other" }, { dstInterface: "other" }, { family: "ipv4" }, { table: "other" }, { contextVerified: false }, { source: "fixture" }, { createdAt: "2026-10-21T10:00:00.123457Z" }, { pingMs: NaN }, { dlMbps: Infinity }]) { + assert.equal(context.verifyRecord({ ...row, ...changed } as RoutingSourceRecord, proof), false) + } + for (const id of [1, 2]) for (const changed of [{ password: "CHANGED" }, { host: "192.0.2.1" }, { enabled: false }, { routingEndpointEpoch: randomUUID() }]) { + const changedContext = await createRoutingSourceContext(endpoints.map(value => value.id === id ? { ...value, ...changed } : value)) + assert.equal(changedContext.verifyRecord(row, proof), false) + } + assert.equal(context.verifyRecord(row, null), false) + assert.ok(!JSON.stringify(context.expected).includes(endpoint.password)) + const fakeQuery = { async query(text: string, values: unknown[]) { + credentialQueries++; assert.match(text, /WITH chosen AS MATERIALIZED/); assert.match(text, /FROM servers WHERE id=ANY\(\$1::bigint\[\]\)/) + assert.deepEqual(values, [[1, 2]]); await endpointQueryHook?.() + return { rows: endpoints.map(value => ({ id: value.id, endpoint: structuredClone(value), overflow: false })) } + } } as unknown as Pick + const keyPath = join(directory, "encryption-key"), generationBefore = await accessGeneration() + originalKey = await readFile(keyPath); rotatedKey = randomBytes(32) + assert.equal(originalKey.length, 32); assert.equal(rotatedKey.length, 32) + await context.assertCurrent(fakeQuery) + try { + await writeFile(keyPath, rotatedKey) + assert.equal(await accessGeneration(), generationBefore, "Rotate encryption-key itself, never access-generation") + const fresh = await createRoutingSourceContext(endpoints) + assert.equal(fresh.verifyRecord(row, proof), false, "Positive control: new purpose key rejects original MAC") + const before = credentialQueries + await assert.rejects(context.assertCurrent(fakeQuery), error => !!error && typeof error === "object" && "code" in error && error.code === "STATE_CONFLICT") + assert.equal(credentialQueries, before, "Changed key must refuse BEFORE reading source credentials") + } finally { await writeFile(keyPath, originalKey) } + // Restoring exactly the original bytes is an isolated control, not a key + // migration or reset of the application generation/ongoing operation. + await context.assertCurrent(fakeQuery) + assert.equal((await createRoutingSourceContext(endpoints)).verifyRecord(row, proof), true) + try { + endpointQueryHook = async () => { await writeFile(keyPath, rotatedKey!) } + const before = credentialQueries + await assert.rejects(context.assertCurrent(fakeQuery), error => !!error && typeof error === "object" && "code" in error && error.code === "STATE_CONFLICT") + assert.equal(credentialQueries, before + 1, "Rotation occurs DURING the awaited endpoint SQL seam") + assert.equal(await accessGeneration(), generationBefore) + } finally { endpointQueryHook = undefined; await writeFile(keyPath, originalKey) } + await context.assertCurrent(fakeQuery) + try { + await unlink(keyPath) + const before = credentialQueries + await assert.rejects(context.assertCurrent(fakeQuery), error => !!error && typeof error === "object" && "code" in error && error.code === "STATE_UNAVAILABLE") + assert.equal(credentialQueries, before) + await assert.rejects(readFile(keyPath), error => !!error && typeof error === "object" && "code" in error && error.code === "ENOENT", "Read-only epoch check must not recreate a deleted key") + } finally { await writeFile(keyPath, originalKey) } + await context.assertCurrent(fakeQuery); assert.equal(context.verifyRecord(row, proof), true) + await invalidateMcpAccess() + assert.equal((await createRoutingSourceContext(endpoints)).verifyRecord(row, proof), false) + const beforeGenerationRefusal = credentialQueries + await assert.rejects(context.assertCurrent(fakeQuery)); assert.equal(credentialQueries, beforeGenerationRefusal) + assert.equal(realSqlCalls, 0) + console.log("Routing source attestation: original endpoint/MAC/microsecond/generation checks preserved; real isolated same-generation encryption-key rotation before/during fake credential SQL, deletion without recreation, original-byte restore controls; 0 real DB/native calls") +} finally { + endpointQueryHook = undefined; originalKey?.fill(0); rotatedKey?.fill(0) + pool.query = originalQuery; pool.connect = originalConnect; await closePool() + if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous + const target = resolve(directory); assert.equal(dirname(target), resolve(tmpdir())); assert.match(basename(target), /^mm-routing-source-/) + await rm(target, { recursive: true, force: true }) +} diff --git a/backend/src/modules/mcp/routing-observation-source.ts b/backend/src/modules/mcp/routing-observation-source.ts new file mode 100644 index 00000000..40664e4e --- /dev/null +++ b/backend/src/modules/mcp/routing-observation-source.ts @@ -0,0 +1,91 @@ +import { z } from "zod" +import type { PoolClient } from "pg" +import type { RouteObservation } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { accessGeneration, constantEqual, fingerprint, McpError, privateFingerprinter, privateFingerprinterExisting } from "./security.js" + +const sid = z.number().int().positive().safe(), text = z.string().min(1).max(200) +const endpointSchema = z.object({ id: sid, host: z.string().min(1).max(1000), port: z.number().int().min(1).max(65535), username: z.string().max(1000), password: z.string().max(65536), useSsl: z.boolean(), verifySsl: z.boolean(), enabled: z.boolean(), routingEndpointEpoch: z.string().uuid() }).strip() +export type RoutingEndpoint = z.infer +export type RoutingSourceRecord = RouteObservation & { source: string; createdAt: string } +type Query = Pick +const count = z.number().finite().nonnegative().nullable() +const recordSchema = z.object({ id: text, srcServerId: sid, dstServerId: sid, srcInterface: text, dstInterface: text, family: z.enum(["ipv4", "ipv6"]), table: text, + pingMs: count, lossPct: z.number().finite().min(0).max(100).nullable(), dlMbps: count, ulMbps: count, + pingAt: z.string().nullable(), bandwidthAt: z.string().nullable(), contextVerified: z.boolean(), source: text, createdAt: z.string() }).strip() + +/** Keep PostgreSQL microseconds in signed ordering fields; normalizing to a JS + * Date would let an edited sub-millisecond created_at reuse a signature. */ +function timestamp(value: string | null): string | null { + if (value === null) return null + const match = /^(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)(?:\.(\d{1,6}))?Z$/.exec(value) + if (!match || !Number.isFinite(Date.parse(value)) || new Date(value).toISOString().slice(0, 19) !== match[1]) throw new Error("Invalid observation timestamp") + return match[1] + "." + (match[2] ?? "").padEnd(6, "0") + "Z" +} + +export async function readRoutingEndpoints(ids: readonly number[], query: Query = pool, lock = false): Promise { + const selected = [...new Set(ids)].sort((a, b) => a - b) + if (selected.length > 1000 || selected.some(id => !Number.isSafeInteger(id) || id <= 0)) throw new McpError("STATE_UNAVAILABLE", "Некорректная область источников измерения", 409) + if (!selected.length) return [] + // Scope and byte budget precede transfer of any credential payload to Node. + const rows = (await query.query(`WITH chosen AS MATERIALIZED ( + SELECT id,jsonb_build_object('id',id,'host',host,'port',port,'username',username,'password',password,'useSsl',use_ssl,'verifySsl',verify_ssl,'enabled',enabled,'routingEndpointEpoch',routing_endpoint_epoch) AS endpoint + FROM servers WHERE id=ANY($1::bigint[]) ORDER BY id ${lock ? "FOR SHARE" : ""} + ), budget AS (SELECT coalesce(sum(octet_length(endpoint::text)),0) AS bytes FROM chosen) + SELECT id,CASE WHEN budget.bytes<=1048576 THEN endpoint END AS endpoint,budget.bytes>1048576 AS overflow FROM chosen CROSS JOIN budget ORDER BY id`, [selected])).rows + if (rows.some(row => row.overflow)) throw new McpError("STATE_UNAVAILABLE", "Источник параметров измерения превышает 1 МиБ", 409) + const result = endpointSchema.array().safeParse(rows.map(row => row.endpoint)) + if (!result.success) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены параметры источников измерения", 409) + return result.data +} + +export async function createRoutingSourceContext(endpoints: readonly RoutingEndpoint[]) { + const sign = await privateFingerprinter("routing-observation-source-v1"), generation = await accessGeneration() + // Private fixed-message proof detects an encryption-key epoch change without + // changing endpoint/record MAC bytes or exposing material outside this scope. + const keyMessage = "routing-observation-source-purpose-key-epoch-v1", keyProof = sign(keyMessage) + const assertPurposeKey = async () => { + const current = await privateFingerprinterExisting("routing-observation-source-v1") + if (!constantEqual(current(keyMessage), keyProof)) throw new McpError("STATE_CONFLICT", "Ключ источника изменился; нужен новый замер", 409) + } + const collect = (values: readonly RoutingEndpoint[]) => { + const signatures = new Map() + for (const value of values) { + const endpoint = endpointSchema.parse(value) + if (!endpoint.enabled) continue + const proof = sign("endpoint:" + fingerprint({ generation, endpoint })) + if (signatures.has(endpoint.id) && signatures.get(endpoint.id) !== proof) throw new McpError("STATE_CONFLICT", "Источник изменился при получении параметров измерения", 409) + signatures.set(endpoint.id, proof) + } + return [...signatures].sort((a, b) => a[0] - b[0]).map(([serverId, proof]) => ({ serverId, proof })) + } + const expected = collect(endpoints), signatures = new Map(expected.map(row => [row.serverId, row.proof])) + const recordProof = (raw: RoutingSourceRecord): string | null => { + const parsed = recordSchema.safeParse(raw) + if (!parsed.success) return null + const row = parsed.data, source = signatures.get(row.srcServerId), destination = signatures.get(row.dstServerId) + if (!source || !destination) return null + try { + return sign("record:" + fingerprint({ source, destination, record: { ...row, pingAt: timestamp(row.pingAt), bandwidthAt: timestamp(row.bandwidthAt), createdAt: timestamp(row.createdAt) } })) + } catch { return null } + } + return { + expected, + signRecord(record: RoutingSourceRecord) { + const proof = recordProof(record) + if (!proof) throw new McpError("MEASUREMENT_UNKNOWN", "Нельзя подтвердить источник или значения замера", 409) + return proof + }, + verifyRecord(record: RoutingSourceRecord, proof: unknown) { + const expectedProof = recordProof(record) + return typeof proof === "string" && /^[a-f0-9]{64}$/.test(proof) && expectedProof !== null && constantEqual(expectedProof, proof) + }, + async assertCurrent(query: Query = pool, lock = false) { + if (generation !== await accessGeneration()) throw new McpError("STATE_CONFLICT", "Подключение источника изменилось; нужен новый замер", 409) + await assertPurposeKey() // Fresh key check BEFORE reading credentials. + const current = collect(await readRoutingEndpoints(expected.map(row => row.serverId), query, lock)) + if (generation !== await accessGeneration() || fingerprint(current) !== fingerprint(expected)) throw new McpError("STATE_CONFLICT", "Подключение источника изменилось; нужен новый замер", 409) + await assertPurposeKey() // SQL and generation checks cannot return stale key authority. + }, + } +} diff --git a/backend/src/modules/mcp/routing-observations-model.test.ts b/backend/src/modules/mcp/routing-observations-model.test.ts new file mode 100644 index 00000000..2173b112 --- /dev/null +++ b/backend/src/modules/mcp/routing-observations-model.test.ts @@ -0,0 +1,30 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import type { RouteObservation } from "@mmapp/contracts/mcp-routing" +import { fingerprint } from "./security.js" +import { assertRoutingChecksFresh, assertRoutingObservationValues } from "./routing-observations-model.js" + +const now = Date.parse("2026-10-18T12:00:00Z") +const observation: RouteObservation = { id: "observed", srcServerId: 1, dstServerId: 2, srcInterface: "gre", dstInterface: "peer", family: "ipv4", table: "main", pingMs: 10, lossPct: 0, dlMbps: 100, ulMbps: 200, pingAt: new Date(now).toISOString(), bandwidthAt: new Date(now).toISOString(), contextVerified: true } +const check = { id: observation.id, srcServerId: 1, dstServerId: 2, srcInterface: "gre", family: "ipv4", table: "main", fingerprint: fingerprint(observation), validUntil: new Date(now + 1000).toISOString() } +test("the exact sealed observation remains valid up to its exclusive deadline", () => { + assertRoutingObservationValues([check], [{ ...observation }], now + 999) + assert.throws(() => assertRoutingObservationValues([check], [observation], now + 1000)) + assert.throws(() => assertRoutingObservationValues([{ ...check, validUntil: "invalid" }], [observation], now)) +}) +test("new identity or changed metrics/context cannot reuse the sealed evidence", () => { + const changes: Partial[] = [{ id: "newer" }, { lossPct: 5 }, { pingMs: null }, { dlMbps: 0 }, { contextVerified: false }, { family: "ipv6" }, { table: "isolated" }, { dstServerId: 3 }, { srcInterface: "other" }, { bandwidthAt: null }] + for (const patch of changes) assert.throws(() => assertRoutingObservationValues([check], [{ ...observation, ...patch }], now)) +}) +test("missing, extra or reordered query results fail without selecting an older alternative", () => { + assert.throws(() => assertRoutingObservationValues([check], [], now)) + assert.throws(() => assertRoutingObservationValues([check], [observation, observation], now)) + const peer = { ...observation, id: "other", srcInterface: "other" } + const peerCheck = { ...check, id: peer.id, srcInterface: peer.srcInterface, fingerprint: fingerprint(peer) } + assert.throws(() => assertRoutingObservationValues([check, peerCheck], [peer, observation], now)) + assertRoutingObservationValues([check, peerCheck], [observation, peer], now) +}) +test("bounds reject oversized checks before a database request; plans without checks stay compatible", () => { + assert.throws(() => assertRoutingChecksFresh(Array.from({ length: 1001 }, () => check), now)) + assertRoutingObservationValues([], [], now) +}) diff --git a/backend/src/modules/mcp/routing-observations-model.ts b/backend/src/modules/mcp/routing-observations-model.ts new file mode 100644 index 00000000..f11365fd --- /dev/null +++ b/backend/src/modules/mcp/routing-observations-model.ts @@ -0,0 +1,15 @@ +import type { RouteObservation } from "@mmapp/contracts/mcp-routing" +import type { ChangeEvidence } from "./action-types.js" +import { fingerprint, McpError } from "./security.js" + +type Checks = NonNullable +export function latestRoutingMetric(rows: readonly RouteObservation[], metric: "pingAt" | "bandwidthAt") { + return rows.find(row => row[metric] !== null || row.sourceVerified === false) +} +export function assertRoutingChecksFresh(checks: Checks, now = Date.now()) { + if (checks.length > 1000 || checks.some(check => !Number.isFinite(Date.parse(check.validUntil)) || Date.parse(check.validUntil) <= now)) throw new McpError("MEASUREMENT_UNKNOWN", "Измерения маршрутизации устарели; нужен новый план", 409) +} +export function assertRoutingObservationValues(checks: Checks, latest: RouteObservation[], now = Date.now()) { + assertRoutingChecksFresh(checks, now) + if (latest.length !== checks.length || checks.some((check, index) => latest[index]?.id !== check.id || fingerprint(latest[index]) !== check.fingerprint)) throw new McpError("MEASUREMENT_UNKNOWN", "После подтверждения изменились измерения маршрутизации; нужен новый план", 409) +} diff --git a/backend/src/modules/mcp/routing-observations.ts b/backend/src/modules/mcp/routing-observations.ts new file mode 100644 index 00000000..13427377 --- /dev/null +++ b/backend/src/modules/mcp/routing-observations.ts @@ -0,0 +1,89 @@ +import type { RouteObservation } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { McpError } from "./security.js" +import type { ChangeEvidence } from "./action-types.js" +import { assertRoutingChecksFresh, assertRoutingObservationValues, latestRoutingMetric } from "./routing-observations-model.js" +import { createRoutingSourceContext, readRoutingEndpoints, type RoutingSourceRecord } from "./routing-observation-source.js" + +export const routingObservationColumns = `o.id,o.src_server_id AS "srcServerId",o.dst_server_id AS "dstServerId",o.src_interface AS "srcInterface",o.dst_interface AS "dstInterface",o.family,o.routing_table AS "table",o.ping_ms AS "pingMs",o.loss_pct AS "lossPct",o.dl_mbps AS "dlMbps",o.ul_mbps AS "ulMbps",to_char(o.ping_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS "pingAt",to_char(o.bandwidth_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS "bandwidthAt",o.context_verified AS "contextVerified",o.source,to_char(o.created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS "createdAt",o.source_proof AS "sourceProof"` +async function observations(rows: Record[], serverIds: number[]): Promise { + if (!rows.length) return [] + const selected = [...new Set(rows.flatMap(row => [Number(row.srcServerId), Number(row.dstServerId)]))].filter(id => serverIds.includes(id)) + const context = await createRoutingSourceContext(await readRoutingEndpoints(selected)) + return rows.map(raw => { + const row = { ...raw, srcServerId: Number(raw.srcServerId), dstServerId: Number(raw.dstServerId) } as unknown as RoutingSourceRecord + const sourceVerified = context.verifyRecord(row, raw.sourceProof) + // Never expose the signature or endpoint credentials through history/audits. + const { source: _source, createdAt: _createdAt, sourceProof: _proof, ...publicRow } = raw + return { ...publicRow, srcServerId: row.srcServerId, dstServerId: row.dstServerId, contextVerified: sourceVerified && row.contextVerified === true, sourceVerified, recordedAt: row.createdAt } as unknown as RouteObservation + }) +} +export async function readRoutingObservations(serverIds: number[], serverId?: number): Promise { + if (serverId !== undefined && !serverIds.includes(serverId)) throw new McpError("SERVER_DENIED", "История измерений вне разрешённой области", 403) + return observations((await pool.query(`SELECT ${routingObservationColumns} FROM mcp_routing_observations o WHERE o.src_server_id=ANY($1::bigint[]) AND o.dst_server_id=ANY($1::bigint[]) AND ($2::bigint IS NULL OR o.src_server_id=$2 OR o.dst_server_id=$2) AND o.created_at>now()-interval '24 hours' ORDER BY o.created_at DESC,o.id DESC LIMIT 10000`, [serverIds, serverId ?? null])).rows, serverIds) +} + +interface RoutingObservationQuery { + serverId?: number + srcServerId?: number + dstServerId?: number + srcInterface?: string + dstInterface?: string + family?: "ipv4" | "ipv6" + table?: string + limit: number + cursor?: { recordedAt: string; id: string } +} + +export async function queryRoutingObservations(serverIds: number[], input: RoutingObservationQuery) { + if ([input.serverId, input.srcServerId, input.dstServerId].some(id => id !== undefined && !serverIds.includes(id))) { + throw new McpError("SERVER_DENIED", "История измерений вне разрешённой области", 403) + } + const { rows } = await pool.query(`SELECT ${routingObservationColumns} FROM mcp_routing_observations o + WHERE o.src_server_id=ANY($1::bigint[]) AND o.dst_server_id=ANY($1::bigint[]) + AND ($2::bigint IS NULL OR o.src_server_id=$2 OR o.dst_server_id=$2) + AND ($3::bigint IS NULL OR o.src_server_id=$3) AND ($4::bigint IS NULL OR o.dst_server_id=$4) + AND ($5::text IS NULL OR o.src_interface=$5) AND ($6::text IS NULL OR o.dst_interface=$6) + AND ($7::text IS NULL OR o.family=$7) AND ($8::text IS NULL OR o.routing_table=$8) + AND ($9::timestamptz IS NULL OR (o.created_at,o.id)<($9::timestamptz,$10::uuid)) + AND o.created_at>now()-interval '24 hours' + ORDER BY o.created_at DESC,o.id DESC LIMIT $11`, [serverIds, input.serverId ?? null, input.srcServerId ?? null, + input.dstServerId ?? null, input.srcInterface ?? null, input.dstInterface ?? null, input.family ?? null, input.table ?? null, + input.cursor?.recordedAt ?? null, input.cursor?.id ?? null, input.limit + 1]) + const page = await observations(rows.slice(0, input.limit), serverIds) + const last = page.at(-1) + return { observations: page, nextCursor: rows.length > input.limit && last?.recordedAt ? { recordedAt: last.recordedAt, id: last.id } : null } +} + +/** Match the sealed observation to the newest record for the exact context. + * No re-ranking and no replacement by an older healthy observation. */ +export async function verifyRoutingObservationChecks(checks: ChangeEvidence["observationChecks"]) { + if (!checks?.length) return + assertRoutingChecksFresh(checks) + const { rows } = await pool.query(`SELECT q.ordinality AS "checkOrdinal", latest.* FROM jsonb_array_elements($1::jsonb) WITH ORDINALITY AS q(value,ordinality) + LEFT JOIN LATERAL(SELECT ${routingObservationColumns} FROM mcp_routing_observations o + WHERE o.src_server_id=(q.value->>'srcServerId')::bigint AND o.dst_server_id=(q.value->>'dstServerId')::bigint + AND o.src_interface=q.value->>'srcInterface' AND o.family=q.value->>'family' AND o.routing_table=q.value->>'table' + AND (q.value->>'dstInterface' IS NULL OR o.dst_interface=q.value->>'dstInterface') + AND o.created_at>now()-interval '24 hours' + AND (q.value->>'metric' IS NULL OR (o.created_at,o.id)>=(SELECT sealed.created_at,sealed.id FROM mcp_routing_observations sealed WHERE sealed.id=(q.value->>'id')::uuid)) + ORDER BY o.created_at DESC,o.id DESC LIMIT (CASE WHEN q.value->>'metric' IS NULL THEN 1 ELSE 10001 END)) latest ON true + ORDER BY q.ordinality,latest."createdAt" DESC,latest.id DESC LIMIT 10001`, [JSON.stringify(checks)]) + if (rows.length > 10000) throw new McpError("MEASUREMENT_UNKNOWN", "Слишком много новых измерений маршрутизации; нужен новый план", 409) + const verified = await observations(rows.filter(row => row.id !== null).map(({ checkOrdinal: _, ...row }) => row), [...new Set(checks.flatMap(check => [check.srcServerId, check.dstServerId]))]) + const groups = new Map() + let index = 0 + for (const row of rows) { + if (row.id === null) continue + const ordinal = Number(row.checkOrdinal) + const group = groups.get(ordinal) ?? [] + group.push(verified[index++]) + groups.set(ordinal, group) + } + const latest = checks.flatMap((check, position) => { + const candidates = groups.get(position + 1) ?? [] + const match = check.metric ? latestRoutingMetric(candidates, check.metric) : candidates[0] + return match ? [match] : [] + }) + assertRoutingObservationValues(checks, latest) +} diff --git a/backend/src/modules/mcp/routing-original-effects-caller.test.mjs b/backend/src/modules/mcp/routing-original-effects-caller.test.mjs new file mode 100644 index 00000000..6caff2df --- /dev/null +++ b/backend/src/modules/mcp/routing-original-effects-caller.test.mjs @@ -0,0 +1,131 @@ +import assert from 'node:assert/strict'; +import { createRequire } from 'node:module'; +import { readFile } from 'node:fs/promises'; +import { randomUUID } from 'node:crypto'; +import test from 'node:test'; +const require=createRequire(new URL('../../../package.json',import.meta.url)),ts=require('typescript'),{z}=require('zod'); +const code=ts.transpileModule(await readFile(new URL('./routing-community-scoped-service.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText; +async function loadModel(path, resolveDependency = require) { + const source = await readFile(new URL(path, import.meta.url), 'utf8'); + const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText; + const exports = {}; + new Function('exports', 'require', code)(exports, resolveDependency); + return exports; +} +const filterBody = await loadModel('../../services/routing-filter-body.ts'); +const pathProofPurpose = await loadModel('./path-proof-purpose.ts'); +const nativeEndpoint = await loadModel('./routing-native-endpoint.ts'); +const { originalEffectReportSchema } = await loadModel('./routing-original-effects.ts', name => { + if (name === './routing-community-context-model.js' || name === './security.js') return {}; + if (name === './routing-native-endpoint.js') return nativeEndpoint; + return require(name); +}); +class FixtureMcpError extends Error { + constructor(code, message) { + super(message); + this.code = code; + } +} +async function fixture(mode='prepared',failure,{ proofRevision = 3, manualEffectPolicy } = {}){ + const calls=[],principal={connectionId:randomUUID(),credentialId:randomUUID(),credentialExpiresAt:new Date(Date.now()+120000).toISOString()},connection={id:principal.connectionId,owner_id:'owner',generation:'generation',owner_permissions:['mm:filters:write','mm:network:read'],policy:{mode:'copilot',permissions:['mm:filters:write','mm:network:read'],scope:{serverIds:[1]},expiresAt:new Date(Date.now()+120000).toISOString()},owner_valid_until:new Date(Date.now()+120000)}; + const profile={serverId:1,family:'ipv4',table:'tenant',probeVrf:'probe',community:'65000:10',autoRecovery:false,candidates:[{id:'a',jumpHostId:1,exitNodeId:1,gateway:'192.0.2.8',gatewayRef:{resource:'ipv4_route',id:'*r0'},controlProbe:{}},{id:'b',jumpHostId:1,exitNodeId:1,gateway:'192.0.2.9',gatewayRef:{resource:'ipv4_route',id:'*r'},controlProbe:{}}],excludedCandidateIds:[],pinnedCandidateId:null}; + const input={serverId:1,profile,candidateId:'b',principal,ownerId:'owner',policyProof:JSON.stringify(connection.policy),connectionGeneration:'generation',generation:'access',proofRevision,originalCandidateId:'a',nativeId:'*1',outIface:'',originalRuleCiphertext:'original',desiredRuleCiphertext:'desired',desiredTargetProof:'desired-proof',beforeRules:[],receiptId:randomUUID()}; + if (manualEffectPolicy) input.manualEffectPolicy = manualEffectPolicy; + if(mode!=='legacy')input.originalEffectSelection=mode==='prepared'?{status:'prepared',probe:{key:'b',serverId:1,destination:'198.51.100.20',source:'198.51.100.10',interface:'wan',probeVrf:'probe'}}:{status:'unknown',reason:'unsupported-source'}; + let wrote=false,handler,network,foreign=false,reads=0,rounds=0,closed=0,ownerClosed=false,nativeCalls=100; + const unknown=reason=>({scope:'prepared-route-fib-adjacency-control-effects',status:'unknown',reason,rounds:[],logCoverage:'unknown',semanticProof:false,forwardingVerified:false,autoRecoveryAllowed:false}); + const report={...unknown(null),status:'observed-expected',rounds:[{controlProbe:'passed'},{controlProbe:'passed'}]}; + const session={sharedContext:{},readSnapshot:async stage=>{calls.push('baseline:'+stage);return {};},readConfigurationBoundForwardSnapshot:async()=>{calls.push('round:'+(++rounds));if(failure==='round'||failure==='round-closed'){ownerClosed=failure==='round-closed';throw Error('unknown');}return {configurationReadbacks:{before:{checkpointProof:failure==='checkpoint-mismatch'?'other':'configuration'}}};},readBudget:()=>({deadline:Date.now()+10000,calls:nativeCalls,bytes:1000,rows:50,closed:ownerClosed}),close:()=>{closed++;ownerClosed=true;calls.push('close-session');}}; + const security = {accessGeneration:async()=>'access',decryptSecret:async x=>x,encryptSecret:async x=>x,fingerprint:JSON.stringify,McpError:FixtureMcpError,privateSecretFingerprint:async()=>'seal',redact:x=>x}; + const predispatchDiagnostic = await loadModel('./routing-community-predispatch-diagnostic.ts', name => { + if (name === './security.js') return security; + return require(name); + }); + const { readScopedPathCheckpoint } = await loadModel('./routing-community-scoped-request.ts', name => { + if (name === './path-proof-purpose.js') return pathProofPurpose; + if (name === './security.js') return security; + if (name === './routing-community-predispatch-diagnostic.js') return predispatchDiagnostic; + return require(name); + }); + const pathCheckId = randomUUID(), observedAt = new Date(); + const pathCheckpoint = { id: pathCheckId, connection_id: principal.connectionId, status: 'passed', created_at: observedAt, evidence: { observedAt: observedAt.toISOString() } }; + const providers={ + 'node:crypto':{randomUUID},zod:{z},'@mmapp/contracts/mcp-routing':{routeAiProfileSchema:{parse:x=>x}},'../../db/index.js':{pool:{query:async (sql, values)=>({rows:String(sql).includes('FROM mcp_path_checks')&&values?.[0]===pathCheckId&&values?.[1]===principal.connectionId?[pathCheckpoint]:[]})}}, + '../../lib/permissions.js':{hasPermission:(list,value)=>list.includes(value)}, + '../../services/config-mutation-policy.js':{assertVerifiedConfigurationAccess:async()=>{},executeGuardedConfigurationWrite:async(_sid,_method,_path,work)=>{calls.push('guarded-intent');return work();}}, + '../../services/config-mutation-lock.js':{withApplicationMutationLock:work=>work(),withConfigMutationLock:(_ids,work)=>work()}, + '../../services/routing-filter-body.js': filterBody, + './store.js':{effectiveMcpConnection:async()=>connection,audit:async(_db,_owner,action)=>{calls.push(action);}}, + './security.js':security, + './routing-community-predispatch-diagnostic.js':predispatchDiagnostic, + './routing-community-scoped-checkpoint.js':{saveScopedConfigurationCheckpoint:async()=>{calls.push('CONFIG_APPLIED');return {checkpointProof:'configuration'};},loadScopedConfigurationCheckpoint:async()=>({checkpointProof:'configuration'}),saveScopedNetworkCheckpoint:async(_input,_seal,check,expectedConfigurationProof,originalEffects)=>{assert.equal(expectedConfigurationProof,'configuration');calls.push('save-network');network={status:check.status,networkSeal:'network',...(originalEffects?{originalEffects}:{})};},loadScopedNetworkCheckpoint:async()=>network}, + './routing-community-scoped-request.js':{assertScopedCommunityRequest:async()=>'request',readScopedPathCheckpoint},'./management-transport.js':{prepareManagementTransportBinding:async()=>({})}, + './routing-community-context.js':{readRouteCommunityContext:async()=>{reads++;return {before:{scopedExecutionAvailable:true,currentCandidateId:wrote?'b':'a',dependencyProof:'dep',sourceProofs:[{serverId:1,proof:'source'}]},ownedTarget:{nativeId:'*1',rawRow:{rule:wrote?'desired':'original'},endpoint:{},desiredTargetProof:()=> 'desired-proof'},fence:async()=>{},clients:new Map([[1,{get:async()=>[{'.id':'*r'}]}]]),captureOriginalEffectSelection:async()=>{calls.push('capture-prepared-selection');return input.originalEffectSelection;}};}}, + './routing-community-context-model.js':{candidateMatchesGateway:()=>true},'./routing-model.js':{rankRoutingCandidates:()=>profile.candidates.map(candidate=>({candidate,eligible:true}))},'./routing-observations.js':{readRoutingObservations:async()=>[]}, + './path-verification.js':{requireRecentPath:async()=>pathCheckId,requireRecentPathWithContext:async()=>({id:pathCheckId,context:{before:{dependencyProof:'dep'}},assertFresh:()=>{}}),checkCandidatePath:async()=>{calls.push('legacy-probe');return {id:randomUUID(),candidateId:'b',status:'passed',observedAt:new Date().toISOString()};}}, + './routing-community-scoped-model.js':{compileScopedCommunitySwitch:()=>({compiledBody:'desired',proposed:{community:'65000:10',gateway:'192.0.2.9',outIface:''},before:{community:'65000:10',gateway:'192.0.2.8',outIface:''}}),scopedConfigurationMatches:()=>({configurationMatchesOriginal:!wrote,configurationMatchesDesired:wrote&&!foreign})}, + './routing-community-scoped-native.js':{patchScopedCommunity:async()=>{calls.push('PATCH');wrote=true;}},'./service-registry.js':{registerMcpService:(_name,value)=>{handler=value;}}, + './routing-profile-authority.js':{assertPersistentScopedProfileAuthority:async()=>{},resolveRoutingProfileAuthority:async()=>{},routingProfileAuthorityBindingSchema:{parse:x=>x},scopedProfileAuthorityTransport:async()=>({})}, + './routing-observation-source.js':{createRoutingSourceContext:async()=>({}),readRoutingEndpoints:async()=>[]},'./routing-reservation-store.js':{createRoutingReservationStore:()=>({})}, + './routing-network-execution-session.js':{createRoutingNetworkSessionForCurrentExecution:async options=>{calls.push('open-session');assert.deepEqual(Object.keys(options).sort(),['probes','servers']);if(failure==='open')throw Error('unknown');return session;}}, + './routing-network-configuration-bridge.js':{resolveRoutingNetworkForwardRoundConfigurationContext:async()=>{calls.push('configuration-round-owner');return {};},closeRoutingNetworkForwardRoundConfigurationContext:()=>{calls.push('close-round-owner');}}, + './routing-network-native-session.js':{sameConfigurationBoundForwardRounds:()=>failure!=='round-binding'}, + './routing-network-scoped-context.js':{readRoutingNetworkScopedCommunityContext:async()=>{calls.push('shared-publication');if(failure==='publication')throw Error('DO_NOT_EXPOSE_TEST_DETAIL');nativeCalls+=10;return{before:{dependencyProof:'dep',targetRuleProof:'desired-proof',currentCandidateId:'b'},fence:async()=>{}};}}, + './routing-original-effects.js':{originalEffectReportSchema,originalEffectSelectionSchema:{safeParse:x=>x?{success:true,data:x}:{success:false}},unknownOriginalEffects:unknown,originalEffectBaselineMatches:()=>failure!=='baseline',inspectOriginalEffectRounds:async()=>{calls.push('inspect-effects');if(failure==='foreign')foreign=true;return report;}} + }; + const exports={};new Function('exports','require',code)(exports,name=>{assert.ok(providers[name],name);return providers[name];});exports.registerScopedCommunityService(); + return {calls,input,run:()=>handler.execute(input,{operationId:randomUUID(),phase:'forward',stepIndex:0}),prepare:()=>exports.prepareScopedCommunitySwitch({profile,candidateId:'b'},{principal,connection}),stats:()=>({wrote,closed,reads}),network:()=>network}; +} +test('actual service consumer baselines before the single PATCH and persists separate effects after genuine CONFIG_APPLIED and manual probe',async()=>{const f=await fixture(),result=await f.run();const c=f.calls;assert(c.indexOf('baseline:before')x==='PATCH').length,1);assert.equal(result.configurationVerified,true);assert.equal(result.controlProbeVerified,true);assert.equal(result.forwardingVerified,false);assert.equal(result.originalEffects.status,'observed-expected');assert.equal(result.originalEffects.budget.calls,110);assert.equal(f.stats().closed,1);assert.deepEqual(result.originalEffects,f.network().originalEffects);}); +for (const mode of ['legacy', 'unknown']) test(`existing ${mode} manual path does not open native effect session`, async () => { + const f = await fixture(mode, undefined, { proofRevision: 2 }); + const result = await f.run(); + assert(!f.calls.includes('open-session')); + assert.equal(result.configurationVerified, true); + assert.equal(result.controlProbeVerified, true); + assert.equal(f.calls.filter(call => call === 'PATCH').length, 1); +}); +for (const failure of ['open', 'baseline', 'round', 'round-closed', 'publication', 'checkpoint-mismatch']) test(`optional ${failure} uncertainty preserves config/control receipt with effects unknown`, async () => { + const f = await fixture('prepared', failure, ['open', 'baseline'].includes(failure) + ? { proofRevision: 2, manualEffectPolicy: 'observational-v1' } + : { proofRevision: 3 }); + const result = await f.run(); + assert.equal(result.configurationVerified, true); + assert.equal(result.controlProbeVerified, true); + assert.equal(result.originalEffects.status, 'unknown'); + assert.equal(f.calls.filter(call => call === 'PATCH').length, 1); + assert(f.calls.includes('save-network')); +}); +for (const [description, mode, failure] of [ + ['missing selection', 'legacy'], + ['unknown selection', 'unknown'], + ['unavailable native session', 'prepared', 'open'], + ['baseline drift', 'prepared', 'baseline'], +]) { + test(`persistent revision 3 refuses ${description} before dispatch`, async () => { + const f = await fixture(mode, failure, { proofRevision: 3 }); + await assert.rejects(f.run, { code: 'MEASUREMENT_UNKNOWN' }); + for (const call of ['routing.scoped.attempt', 'guarded-intent', 'PATCH', 'CONFIG_APPLIED', 'save-network']) { + assert(!f.calls.includes(call), call); + } + }); +} +test('foreign configuration drift after optional effect observations still refuses publication, never repeats PATCH',async()=>{const f=await fixture('prepared','foreign');await assert.rejects(f.run);assert.equal(f.calls.filter(x=>x==='PATCH').length,1);assert(!f.calls.includes('save-network'));assert.equal(f.stats().closed,1);}); +test('revision2 remains on existing manual path even with a prepared-like field',async()=>{const f=await fixture();f.input.proofRevision=2;await f.run();assert(!f.calls.includes('open-session'));}); +test('sealed manual policy runs read-only rounds through the real scoped consumer',async()=>{const f=await fixture();f.input.proofRevision=2;f.input.manualEffectPolicy='observational-v1';const result=await f.run();assert.equal(f.calls.filter(x=>x==='PATCH').length,1);assert(f.calls.includes('round:2'));assert.equal(result.originalEffects.status,'observed-expected');}); +test('actual preparation consumes the required real-context selection method and includes it in sealed service input',async()=>{const f=await fixture(),evidence=await f.prepare();assert(f.calls.includes('capture-prepared-selection'));assert.deepEqual(evidence.steps[0].input.originalEffectSelection,f.input.originalEffectSelection);assert.equal(evidence.steps[0].input.requestProof,'seal');assert(!f.calls.includes('PATCH'));}); +test('closed effect owner falls back to exactly the old final context read without bypassing publication guard', async () => { + const legacy = await fixture('legacy', undefined, { proofRevision: 2 }); + await legacy.run(); + const f = await fixture('prepared', 'round-closed', { proofRevision: 2, manualEffectPolicy: 'observational-v1' }); + await f.run(); + assert(!f.calls.includes('shared-publication')); + assert.equal(f.stats().reads, legacy.stats().reads); +}); + +test('optional publication error exposes only a bounded failure category', async()=>{ + const f=await fixture('prepared','publication'), result=await f.run(); + assert.equal(result.originalEffects.reason,'effect-publication-revalidation-unavailable:STATE_UNAVAILABLE'); + assert(!JSON.stringify(result).includes('DO_NOT_EXPOSE_TEST_DETAIL')); + assert.equal(result.configurationVerified,true);assert.equal(result.controlProbeVerified,true); +}); diff --git a/backend/src/modules/mcp/routing-original-effects-controller.test.mjs b/backend/src/modules/mcp/routing-original-effects-controller.test.mjs new file mode 100644 index 00000000..3e687711 --- /dev/null +++ b/backend/src/modules/mcp/routing-original-effects-controller.test.mjs @@ -0,0 +1,71 @@ +import assert from 'node:assert/strict'; +import { createRequire } from 'node:module'; +import { readFile } from 'node:fs/promises'; +import { randomUUID, createHash } from 'node:crypto'; +import test from 'node:test'; +const require=createRequire(new URL('../../../package.json',import.meta.url)),ts=require('typescript'),{z}=require('zod'); +const hash=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex'),sign=async(value,purpose)=>hash([purpose,value]); +const code=ts.transpileModule(await readFile(new URL('./routing-controller-baseline.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText; +const effectCode=ts.transpileModule(await readFile(new URL('./routing-original-effects.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,effectExports={}; +const endpointCode=ts.transpileModule(await readFile(new URL('./routing-native-endpoint.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,endpointExports={}; +new Function('exports','require',endpointCode)(endpointExports,require); +new Function('exports','require',effectCode)(effectExports,name=>{if(name==='node:net')return require('node:net');if(name==='zod')return{z};if(name==='./security.js')return{fingerprint:hash};if(name==='./routing-community-context-model.js')return{};if(name==='./routing-native-endpoint.js')return endpointExports;throw Error(name);}); +async function loadModel(path, resolveDependency) { + const source = await readFile(new URL(path, import.meta.url), 'utf8'); + const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText; + const exports = {}; + new Function('exports', 'require', code)(exports, resolveDependency); + return exports; +} +const filterBody = await loadModel('../../services/routing-filter-body.ts', require); +const filterRules = await loadModel('../../services/filter-rules-write-model.ts', require); +const { scopedTargetRuleMatches } = await loadModel('./routing-community-scoped-model.ts', name => { + if (name === '../../services/routing-filter-body.js') return filterBody; + if (name === '../../services/filter-rules-write-model.js') return filterRules; + return require(name); +}); +const preparedValue={status:'prepared',revision:1,serverId:1,routeSelectionScope:'table-afi-community-observation',candidateId:'b',family:'ipv4',table:'tenant',community:'65000:10',expectedGateway:'192.0.2.9',expectedImmediate:{address:'198.51.100.2',interface:'ether2'},binding:{connectionId:'*c',sessionId:'*s',instance:'default',table:'tenant',vrf:'main',localAddress:'192.0.2.1',remoteAddress:'192.0.2.2',remoteAsn:65001,afi:'ip'},bfd:{applicability:'not-configured',evidence:'explicit-use-bfd-false'},routes:[{nativeId:'*r',prefix:'203.0.113.0/24',originalGateway:'192.0.2.8'}],probe:{key:'b',serverId:1,destination:'198.51.100.20',source:'198.51.100.10',interface:'wan',probeVrf:'probe',expectedHops:['198.51.100.20']}}; +async function fixture(selection='absent',effectStatus='observed-expected',switched=true){ + const principal={connectionId:randomUUID(),credentialId:randomUUID(),credentialExpiresAt:new Date(Date.now()+120000).toISOString()},profileId=randomUUID(),planId=randomUUID(),operationId=randomUUID(),grant=new Date(Date.now()+60000).toISOString(); + const profile={serverId:1,table:'tenant',probeVrf:'probe',candidates:[{id:'a',jumpHostId:1,exitNodeId:1},{id:'b',jumpHostId:1,exitNodeId:1}]},perms=['mm:network:read','mm:network:write','mm:filters:write']; + const connection={id:principal.connectionId,owner_id:'owner',owner_permissions:perms,owner_valid_until:new Date(Date.now()+120000),owner_claims_issued_at:new Date(Date.now()-1000),generation:'access',policy:{status:'active',mode:'copilot',permissions:perms,scope:{serverIds:[1],operationIds:['routing.analyze','routing.community.switch','routing.path.verify']},expiresAt:new Date(Date.now()+120000).toISOString()}}; + const context={principal,connection},sourceProofs=[{serverId:1,proof:'c'.repeat(64)}],dependency='d'.repeat(64),originalTarget='a'.repeat(64),desiredTarget='b'.repeat(64),checkpointProof='e'.repeat(64),networkSeal='f'.repeat(64),requestSeal='1'.repeat(64); + let advancing=false,nativeReads=0,row; + const input={proofRevision:3,profile,principal,ownerId:'owner',nativeId:'*1',originalCandidateId:'a',originalTargetProof:originalTarget,desiredTargetProof:desiredTarget,dependencyProof:dependency,contextSources:sourceProofs,candidateId:'b',receiptId:randomUUID(),persistentProfileAuthority:{packet:{profileId,profileVersion:1}}}; + if(selection!=='absent')input.originalEffectSelection=selection?.status==='prepared'&&Object.keys(selection).length===1?structuredClone(preparedValue):selection; + const packet={revision:2,operationId,stepIndex:0,nativeId:'*1',originalCandidateId:'a',originalTargetProof:originalTarget,dependencyProof:dependency,contextSources:sourceProofs,desiredCandidateId:'b',desiredTargetProof:desiredTarget,desiredRule:'desired'}; + const network={status:'passed',networkSeal,...(effectStatus==='missing'?{}:{originalEffects:{status:effectStatus,switchEffectObserved:switched,selectionIdentity:hash(input.originalEffectSelection??null),transition:{changedPrefixes:switched?['203.0.113.0/24']:[]}}})}; + const readNativeContext = async () => { + nativeReads++; + const rawRow = { rule: advancing ? 'desired' : 'original' }; + const targetRuleProof = advancing ? desiredTarget : originalTarget; + const target = { nativeId: '*1', rawRow, desiredTargetProof: body => body === rawRow.rule ? targetRuleProof : hash(body) }; + return { + before: { scopedExecutionAvailable: true, currentCandidateId: advancing ? 'b' : 'a', targetRuleProof, dependencyProof: dependency, sourceProofs, nativeRuleId: '*1' }, + ownedTarget: { ...target, matchesRuleProof: (body, proof) => scopedTargetRuleMatches(target, body, proof) }, + fence: async () => {}, + }; + }; + const providers={ + './routing-main-transition.js':{},'../../db/index.js':{pool:{query:async()=>({rows:[row]})}},'./registry.js':{findMcpOperation:()=>({version:7})}, + './routing-community-scoped-request.js':{assertScopedCommunityRequest:async()=>requestSeal},'./routing-profile-authority.js':{assertPersistentScopedProfileAuthority:async()=>{},routingProfileAuthorityBindingSchema:{parse:x=>x}}, + './routing-community-scoped-checkpoint.js':{loadScopedConfigurationPacket:async()=>({packet,checkpointProof,observedAt:new Date(Date.now()-500).toISOString()}),loadScopedNetworkCheckpoint:async()=>network}, + zod:{z},'@mmapp/contracts/mcp-routing':{routeAiProfileSchema:{parse:x=>x}},'../../lib/permissions.js':{hasPermission:(list,value)=>list.includes(value)},'./store.js':{effectiveMcpConnection:async()=>connection}, + './security.js':{accessGeneration:async()=>'access',constantEqual:(a,b)=>a===b,decryptSecret:async x=>x,fingerprint:hash,redact:x=>x,McpError:Error,privateSecretFingerprint:sign}, + './routing-community-context.js':{readRouteCommunityContext: readNativeContext}, + './routing-original-effects.js':{originalEffectSelectionSchema:effectExports.originalEffectSelectionSchema} + }; + const exports={};new Function('exports','require',code)(exports,name=>{assert.ok(providers[name],name);return providers[name];}); + const baseline=await exports.prepareRoutingControllerBaseline(profileId,1,profile,grant,context);advancing=true; + const step={kind:'service',service:'routing.community.scoped',input}; + row={id:operationId,plan_id:planId,state:'applied',step_state:'verified',operation_id:'routing.community.switch',operation_version:7,approved_by:'owner',created_at:new Date(Date.now()-1000),evidence:{sealed:JSON.stringify({steps:[step],target:{scope:'servers',serverIds:[1]}})},intended_state:step}; + row.result={nativeVerificationSeal:await sign(hash({request:requestSeal,dependency,desired:desiredTarget,candidate:'b',configuration:checkpointProof,network:networkSeal}),'route-ai-scoped-verified-v3'),configurationCheckpointProof:checkpointProof,networkCheckpointProof:networkSeal,receiptId:input.receiptId,proofRevision:3,nativeWrites:1,configurationVerified:true,postProbeStatus:'passed',controlProbeVerified:true}; + return {advance:()=>exports.advanceRoutingControllerBaseline(baseline,profileId,1,profile,grant,context,planId,operationId),nativeReads:()=>nativeReads,row,network}; +} +test('actual controller preserves old advancement only when new selection field is absent',async()=>{const f=await fixture();assert.equal((await f.advance()).packet.candidateId,'b');assert.equal(f.nativeReads(),2);}); +for(const [name,selection]of[['unknown',{status:'unknown',reason:'unsupported'}],['malformed',{}],['malformed prepared',{status:'prepared',revision:2}],['null',null],['array',[]]])test(`actual controller rejects present ${name} before desired native read`,async()=>{const f=await fixture(selection);await assert.rejects(f.advance);assert.equal(f.nativeReads(),1);assert.equal(f.row.result.configurationVerified,true);assert.equal(f.row.result.controlProbeVerified,true);}); +for(const status of ['unknown','observed-other','missing'])test(`actual controller rejects prepared selection with ${status} durable effect result`,async()=>{const f=await fixture({status:'prepared'},status);await assert.rejects(f.advance);assert.equal(f.nativeReads(),1);}); +test('actual controller rejects matched poststate without an observed selected FIB transition',async()=>{const f=await fixture({status:'prepared'},'observed-expected',false);await assert.rejects(f.advance);assert.equal(f.nativeReads(),1);}); +test('actual controller advances after durable expected selected FIB transition',async()=>{const f=await fixture({status:'prepared'});assert.equal((await f.advance()).packet.candidateId,'b');assert.equal(f.nativeReads(),2);}); +test('actual controller rejects effect vector belonging to a different prepared selection',async()=>{const f=await fixture({status:'prepared'});f.network.originalEffects.selectionIdentity='0'.repeat(64);await assert.rejects(f.advance);assert.equal(f.nativeReads(),1);}); +test('actual controller rejects transition flag without an actual changed selected prefix',async()=>{const f=await fixture({status:'prepared'});f.network.originalEffects.transition.changedPrefixes=[];await assert.rejects(f.advance);assert.equal(f.nativeReads(),1);}); diff --git a/backend/src/modules/mcp/routing-original-effects.test.mjs b/backend/src/modules/mcp/routing-original-effects.test.mjs new file mode 100644 index 00000000..bb04c853 --- /dev/null +++ b/backend/src/modules/mcp/routing-original-effects.test.mjs @@ -0,0 +1,75 @@ +import assert from 'node:assert/strict'; +import { createRequire } from 'node:module'; +import { readFile } from 'node:fs/promises'; +import test from 'node:test'; +import { isIP } from 'node:net'; +import { randomUUID, createHash } from 'node:crypto'; +const require=createRequire(new URL('../../../package.json',import.meta.url)), ts=require('typescript'),{z}=require('zod'); +const source=await readFile(new URL('./routing-original-effects.ts',import.meta.url),'utf8'); +const exports={}, endpointExports={}; +const endpointSource=await readFile(new URL('./routing-native-endpoint.ts',import.meta.url),'utf8'); +new Function('exports','require',ts.transpileModule(endpointSource,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText)(endpointExports,require); +const gateway=value=>{const match=/^([^@%\s,]+)(?:%([^@%\s,]+))?(?:@([^@%\s,]+))?$/.exec(value);if(!match||!isIP(match[1]))throw Error();return {address:match[1],family:isIP(match[1]),interface:match[2]??null,resolverTable:match[3]??'main'};}; +const providers={ 'node:net':{isIP},'node:crypto':{randomUUID},zod:{z},'./routing-community-context-model.js':{routeGatewayContext:gateway},'./routing-native-endpoint.js':endpointExports,'./security.js':{fingerprint:value=>createHash('sha256').update(JSON.stringify(value)).digest('hex')}, + './path-verification.js':{inspectControlPing:rows=>{const row=rows.at(-1),sent=Number(row.sent),received=Number(row.received);return {confirmedFailure:sent===3&&received===0,ping:{measured:sent===3&&Number.isInteger(received)&&received>=0&&received<=3,loss:(sent-received)/sent*100}};}, + inspectTrace:(rows,destination,expected)=>({verified:Array.isArray(rows)&&rows.at(-1)?.address===destination&&expected.every(x=>rows.some(r=>r.address===x))})}}; +new Function('exports','require',ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText)(exports,name=>{assert.ok(providers[name],name);return providers[name];}); +function fixture(){ + const profile={serverId:1,family:'ipv4',table:'tenant',probeVrf:'probe',community:'65000:10',candidates:[{id:'b',wanInterface:'wan',gateway:'192.0.2.9',gatewayRef:{resource:'ipv4_route',id:'*9'},controlProbe:{targetInterface:'target'}}]}; + const binding={connectionId:'*c',instance:'default',table:'tenant',vrf:'main'}; + const connection={'.id':'*c',instance:'default','routing-table':'tenant',vrf:'main','local.address':'192.0.2.1','remote.address':'192.0.2.2','remote.as':'65001',afi:'ip',templates:'',disabled:'false','use-bfd':'false'}; + const session={...connection,'.id':'*s','local.afi':'ip','remote.afi':'ip',established:'true'}; + const route={'.id':'*r',afi:'ip4','routing-table':'tenant','dst-address':'203.0.113.0/24',bgp:'true','bgp.communities':'65000:10',gateway:'192.0.2.8',active:'true',contribution:'active',ecmp:'false','immediate-gw':'198.51.100.1%ether1'}; + const raw={connections:[connection],sessions:[session],routes:[route],candidate:{'.id':'*9','immediate-gw':'198.51.100.2%ether2'},probe:{source:'198.51.100.10',destination:'198.51.100.20',expected:['198.51.100.20']}}; + const selection=exports.prepareOriginalEffectSelection(profile,'b',binding,raw); + const sample=()=>({observedAt:new Date().toISOString(),servers:[{serverId:1,routes:{payload:[{...route,gateway:'192.0.2.9','immediate-gw':'198.51.100.2%ether2'}]},bgp:{payload:[{...session}],connectionsPayload:[{...connection}]},logs:{buffer:'memory',payload:[{buffer:'memory',topics:'route,info',message:'bounded diagnostic'},{buffer:'other',topics:'route,info'},{topics:'route,info'}]}}],probes:[{key:'b',pingPayload:[{sent:'3',received:'3','packet-loss':'0'}],tracePayload:[{address:'198.51.100.20'}]}]}); + const baseline=sample();baseline.servers[0].routes.payload=structuredClone(raw.routes); + return {profile,binding,raw,selection,sample,baseline,prepare:()=>exports.prepareOriginalEffectSelection(profile,'b',binding,raw),inspect:samples=>exports.inspectOriginalEffectRounds(selection,samples,baseline)}; +} +test('actual captured predicate seals BGP destinations separately from static resolver ID',()=>{const f=fixture();assert.equal(f.selection.status,'prepared');assert.equal(f.selection.routes[0].nativeId,'*r');assert.notEqual(f.selection.routes[0].nativeId,f.raw.candidate['.id']);assert.equal(f.selection.binding.sessionId,'*s');assert.equal('sessionKey'in f.selection.routes[0],false);}); +test('two real effect captures can observe expected FIB/adjacency/probe while logcoverage and stronger claims stay false',async()=>{const f=fixture(),result=await f.inspect([f.sample(),f.sample()]);assert.equal(result.status,'observed-expected');assert.equal(result.logCoverage,'unknown');assert.equal(result.routeOriginVerified,false);assert.equal(result.recursiveChainVerified,false);assert.equal(result.forwardingVerified,false);assert.equal(result.autoRecoveryAllowed,false);assert.equal(result.rounds[0].relatedTopicRows,1);}); +function qualifyRuntimeSession(row) { + delete row.vrf; + row['local.address'] += '@main'; + row['remote.address'] += '@main'; +} +test('BGP session uses exact runtime endpoint qualifiers when VRF is absent and the installation table is separate',async()=>{ + const f=fixture(); + qualifyRuntimeSession(f.raw.sessions[0]); + const selection=f.prepare(); + assert.equal(selection.status,'prepared'); + assert.equal(selection.binding.vrf,'main'); + assert.equal(selection.binding.table,'tenant'); + const samples=[f.sample(),f.sample()]; + const report=await exports.inspectOriginalEffectRounds(selection,samples,f.baseline); + assert.equal(report.status,'observed-expected'); + assert.equal(report.routeOriginVerified,false); +}); +for(const [name,change]of[ + ['missing affected BGP route',f=>{f.raw.routes=[];}],['ambiguous prefix',f=>{f.raw.routes.push({...f.raw.routes[0],'.id':'*r2'});}], + ['connected resolver is not affected BGP route',f=>{f.raw.routes[0].bgp='false';f.raw.routes[0].connect='true';}], + ['unresolved inheritance',f=>{f.raw.connections[0].templates='template';}],['BFD not explicitly known',f=>{delete f.raw.connections[0]['use-bfd'];}], + ['router-selected source retained as unsupported effect subset',f=>{delete f.raw.probe.source;}],['mixed session identity',f=>{f.raw.sessions.push({...f.raw.sessions[0],'.id':'*s2'});}], + ['implicit VRF without both endpoint qualifiers',f=>{qualifyRuntimeSession(f.raw.sessions[0]);f.raw.sessions[0]['local.address']='192.0.2.1';}], + ['qualified endpoints in the installation table',f=>{qualifyRuntimeSession(f.raw.sessions[0]);f.raw.sessions[0]['remote.address']='192.0.2.2@tenant';}], + ['contradictory explicit VRF',f=>{qualifyRuntimeSession(f.raw.sessions[0]);f.raw.sessions[0].vrf='tenant';}], + ['malformed peer ASN',f=>{f.raw.sessions[0]['remote.as']='0xfde9';}], + ['duplicate native ID even with unrelated tuple',f=>{f.raw.sessions.push({...f.raw.sessions[0],instance:'unrelated'});}], + ['noncanonical AFI prefix',f=>{f.raw.routes[0]['dst-address']='2001:db8::/64';}] +])test(`prepared ambiguity/unsupported remains unknown: ${name}`,()=>{const f=fixture();change(f);assert.equal(f.prepare().status,'unknown');}); +test('baseline binds saved route IDs/prefix/gateway and does not infer operation UUID',()=>{const f=fixture(),base=f.sample();base.servers[0].routes.payload=structuredClone(f.raw.routes);assert.equal(exports.originalEffectBaselineMatches(f.selection,base),true);base.servers[0].routes.payload[0]['.id']='*different';assert.equal(exports.originalEffectBaselineMatches(f.selection,base),false);}); +for(const [name,change,expected]of[ + ['unexpected immediate next-hop',s=>{s.servers[0].routes.payload[0]['immediate-gw']='198.51.100.3%ether3';},'observed-other'], + ['unchanged connected route',s=>{s.servers[0].routes.payload[0].bgp='false';s.servers[0].routes.payload[0].connect='true';},'unknown'], + ['same prefix competition',s=>{s.servers[0].routes.payload.push({...s.servers[0].routes.payload[0],'.id':'*other'});},'unknown'], + ['new community target scope',s=>{s.servers[0].routes.payload.push({...s.servers[0].routes.payload[0],'.id':'*other','dst-address':'203.0.114.0/24'});},'unknown'], + ['reconnected session ID',s=>{s.servers[0].bgp.payload[0]['.id']='*new';},'unknown'], + ['observed adjacency down',s=>{s.servers[0].bgp.payload[0].established='false';},'observed-other'], + ['BFD applicability changed',s=>{s.servers[0].bgp.connectionsPayload[0]['use-bfd']='true';},'unknown'], + ['unreachable route',s=>{s.servers[0].routes.payload[0].unreachable='true';},'unknown'], + ['terminal control loss',s=>{s.probes[0].pingPayload=[{sent:'3',received:'0','packet-loss':'100'}];},'observed-other'] +])test(`effect observation: ${name}`,async()=>{const f=fixture(),s=f.sample();change(s);const result=await f.inspect([s,structuredClone(s)]);assert.equal(result.status,expected);assert.equal(result.forwardingVerified,false);}); +test('one round is not upgraded to two',async()=>{const f=fixture();assert.equal((await f.inspect([f.sample()])).status,'unknown');}); +test('unchanged expected poststate is not labelled a switch effect',async()=>{const f=fixture();f.baseline.servers[0].routes.payload[0].gateway=f.selection.expectedGateway;f.baseline.servers[0].routes.payload[0]['immediate-gw']='198.51.100.2%ether2';f.selection.routes[0].originalGateway=f.selection.expectedGateway;const result=await f.inspect([f.sample(),f.sample()]);assert.equal(result.status,'unknown');assert.equal(result.switchEffectObserved,false);assert.deepEqual(result.transition.changedPrefixes,[]);assert.equal(result.reason,'poststate-matched-without-selected-fib-transition');}); +test('actual selected destination gateway/final endpoint transition is recorded separately from origin',async()=>{const f=fixture(),result=await f.inspect([f.sample(),f.sample()]);assert.equal(result.status,'observed-expected');assert.equal(result.switchEffectObserved,true);assert.deepEqual(result.transition.changedPrefixes,['203.0.113.0/24']);assert.equal(result.transition.baselineObservedAt,f.baseline.observedAt);assert.equal(result.routeOriginVerified,false);}); +test('logical gateway change with unchanged final forwarding endpoint is not a FIB switch',async()=>{const f=fixture();f.baseline.servers[0].routes.payload[0]['immediate-gw']='198.51.100.2%ether2';const result=await f.inspect([f.sample(),f.sample()]);assert.equal(result.status,'unknown');assert.equal(result.switchEffectObserved,false);assert.equal(result.routeSelectionScope,'table-afi-community-observation');assert.equal(result.transitionAttribution,'unproven');assert.deepEqual(result.transition.changedPrefixes,[]);}); diff --git a/backend/src/modules/mcp/routing-original-effects.ts b/backend/src/modules/mcp/routing-original-effects.ts new file mode 100644 index 00000000..1163b188 --- /dev/null +++ b/backend/src/modules/mcp/routing-original-effects.ts @@ -0,0 +1,192 @@ +import { isIP } from "node:net" +import { z } from "zod" +import type { RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { routeGatewayContext } from "./routing-community-context-model.js" +import { matchesBgpSessionContext, parseRoutingEndpoint } from "./routing-native-endpoint.js" +import { fingerprint } from "./security.js" + +type Row = Record +const id = z.string().regex(/^\*[a-z0-9]+$/i).max(100) +const text = z.string().min(1).max(200) +const ip = text.refine(value => isIP(value) !== 0) +const flag = (value: unknown): boolean | undefined => [true, "true", "yes"].includes(value as string | boolean) ? true : [false, "false", "no"].includes(value as string | boolean) ? false : undefined +const communities = (row: Row) => Array.isArray(row["bgp.communities"]) ? row["bgp.communities"] as unknown[] : typeof row["bgp.communities"] === "string" ? row["bgp.communities"].split(",") : [] +const family = (row: Row) => ["ip", "ip4"].includes(String(row.afi)) ? "ipv4" : ["ipv6", "ip6"].includes(String(row.afi)) ? "ipv6" : null +const unique = (rows: Row[], nativeId: string) => rows.filter(row => row[".id"] === nativeId) +const tupleSchema = z.object({ connectionId: id, sessionId: id, instance: text, table: text, vrf: text, + localAddress: ip, remoteAddress: ip, remoteAsn: z.number().int().positive().max(4294967295), afi: z.enum(["ip", "ipv6"]) }).strict() +const prepared = z.object({ status: z.literal("prepared"), revision: z.literal(1), serverId: z.number().int().positive(), + routeSelectionScope: z.literal("table-afi-community-observation"), + candidateId: text, family: z.enum(["ipv4", "ipv6"]), table: text, community: text, expectedGateway: text, + expectedImmediate: z.object({ address: ip, interface: text }).strict(), binding: tupleSchema, + bfd: z.object({ applicability: z.literal("not-configured"), evidence: z.literal("explicit-use-bfd-false") }).strict(), + routes: z.array(z.object({ nativeId: id, prefix: text, originalGateway: text }).strict()).min(1).max(32), + probe: z.object({ key: text, serverId: z.number().int().positive(), destination: ip, source: ip, interface: text, probeVrf: text, + expectedHops: z.array(ip).min(1).max(8) }).strict(), +}).strict() +export const originalEffectSelectionSchema = z.union([prepared, z.object({ status: z.literal("unknown"), reason: text }).strict()]) +export type OriginalEffectSelection = z.infer +type Prepared = z.infer +const state = z.enum(["observed-expected", "observed-other", "unknown"]) +const roundSchema = z.object({ observedAt: z.string().datetime(), routes: z.array(z.object({ prefix: text, status: state }).strict()).max(32), + bgp: state, bfd: z.enum(["not-configured", "unknown"]), controlProbe: z.enum(["passed", "failed", "unknown"]), + relatedTopicRows: z.number().int().nonnegative().max(1000) }).strict() +export const originalEffectReportSchema = z.object({ scope: z.literal("prepared-route-fib-adjacency-control-effects"), status: state, + reason: text.nullable(), rounds: z.array(roundSchema).max(3), logSource: z.literal("routeros-rest-ring"), logCoverage: z.literal("unknown"), + selectionIdentity: z.string().regex(/^[a-f0-9]{64}$/).nullable(), + routeSelectionScope: z.literal("table-afi-community-observation"), transitionAttribution: z.literal("unproven"), + switchEffectObserved: z.boolean(), + transition: z.object({ baselineObservedAt: z.string().datetime(), changedPrefixes: z.array(text).max(32) }).strict().optional(), + budget: z.object({ deadline: z.number().int().positive(), calls: z.number().int().nonnegative().max(256), bytes: z.number().int().nonnegative().max(8 * 1048576), + rows: z.number().int().nonnegative().max(10000) }).strict().optional(), + semanticProof: z.literal(false), routeOriginVerified: z.literal(false), recursiveChainVerified: z.literal(false), forwardingVerified: z.literal(false), autoRecoveryAllowed: z.literal(false) }).strict() +export type OriginalEffectReport = z.infer +export const unknownOriginalEffects = (reason: string): OriginalEffectReport => ({ scope: "prepared-route-fib-adjacency-control-effects", status: "unknown", reason, + rounds: [], logSource: "routeros-rest-ring", logCoverage: "unknown", selectionIdentity: null, routeSelectionScope: "table-afi-community-observation", transitionAttribution: "unproven", switchEffectObserved: false, semanticProof: false, routeOriginVerified: false, recursiveChainVerified: false, forwardingVerified: false, autoRecoveryAllowed: false }) + +function connectionMatches(row: Row, expected: Prepared["binding"]) { + return row.instance === expected.instance && row["routing-table"] === expected.table && row.vrf === expected.vrf && + row["local.address"] === expected.localAddress && row["remote.address"] === expected.remoteAddress && + (row["remote.as"] === expected.remoteAsn || row["remote.as"] === String(expected.remoteAsn)) && + row.afi === expected.afi +} + +function selectBgpSession(rows: Row[], expected: Prepared["binding"]) { + try { + let match: Row | undefined + for (const row of rows) { + if (row.instance !== expected.instance) continue + const local = parseRoutingEndpoint(row["local.address"]), remote = parseRoutingEndpoint(row["remote.address"]) + if (local.address !== expected.localAddress || remote.address !== expected.remoteAddress) continue + if (match || local.interface !== undefined || remote.interface !== undefined || !matchesBgpSessionContext(row, expected, local, remote) || + (row["remote.as"] !== expected.remoteAsn && row["remote.as"] !== String(expected.remoteAsn)) || row["local.afi"] !== expected.afi || row["remote.afi"] !== expected.afi || + row.state !== undefined || flag(row.established) === undefined || !id.safeParse(row[".id"]).success) return undefined + match = row + } + return match && unique(rows, String(match[".id"])).length === 1 ? match : undefined + } catch { return undefined } +} +function selectedRows(rows: Row[], selection: Pick) { + return rows.filter(row => row["routing-table"] === selection.table && family(row) === selection.family && flag(row.bgp) === true && communities(row).includes(selection.community)) +} + +/** Called only by the real private context reader. This selects observed route + * effects by table/AFI/Community, never a route→session provenance relationship. + * The initial bounded subset requires explicit flat peer identity, explicit + * BFD-off and an already configured explicit probe source. Other manual paths + * keep their existing configuration/probe behavior, with effects unknown. */ +export function prepareOriginalEffectSelection(profile: RouteAiProfile, candidateId: string, binding: { connectionId: string; instance: string | null; vrf: string; table: string } | null, + raw: { routes: Row[]; connections: Row[]; sessions: Row[]; candidate: Row | undefined; probe: { destination: string; expected: string[]; source?: string } | undefined }): OriginalEffectSelection { + try { + if (!binding || !binding.instance) throw Error() + const candidate = profile.candidates.find(value => value.id === candidateId), probe = raw.probe + if (!candidate?.controlProbe || !probe?.source || !raw.candidate) throw Error() + const connection = unique(raw.connections, binding.connectionId) + if (connection.length !== 1 || !(connection[0].templates === "" || Array.isArray(connection[0].templates) && connection[0].templates.length === 0) || + flag(connection[0].disabled) !== false || flag(connection[0]["use-bfd"]) !== false) throw Error() + const peer = connection[0], afi = profile.family === "ipv4" ? "ip" : "ipv6" + const wanted = tupleSchema.parse({ connectionId: peer[".id"], sessionId: "*0", instance: binding.instance, table: binding.table, vrf: binding.vrf, + localAddress: peer["local.address"], remoteAddress: peer["remote.address"], remoteAsn: Number(peer["remote.as"]), afi }) + if (!connectionMatches(peer, wanted)) throw Error() + const session = selectBgpSession(raw.sessions, wanted) + if (!session) throw Error() + wanted.sessionId = id.parse(session[".id"]) + const affected = selectedRows(raw.routes, profile) + if (!affected.length || affected.length > 32 || new Set(affected.map(row => row["dst-address"])).size !== affected.length || new Set(affected.map(row => row[".id"])).size !== affected.length) throw Error() + for (const row of affected) { + const [address, mask, extra] = String(row["dst-address"]).split("/") + if (extra !== undefined || isIP(address) !== (profile.family === "ipv4" ? 4 : 6) || !/^\d+$/.test(mask ?? "") || Number(mask) > (profile.family === "ipv4" ? 32 : 128)) throw Error() + routeGatewayContext(String(row.gateway)) + } + // A selected destination cannot silently choose among competing origins. + if (affected.some(row => raw.routes.filter(other => other["routing-table"] === profile.table && other["dst-address"] === row["dst-address"]).length !== 1)) throw Error() + const endpoint = candidate.gatewayRef.resource === "gre" ? { address: routeGatewayContext(candidate.gateway).address, interface: String(raw.candidate.name) } : routeGatewayContext(String(raw.candidate["immediate-gw"])) + if (!endpoint.interface) throw Error() + return prepared.parse({ status: "prepared", revision: 1, serverId: profile.serverId, candidateId, family: profile.family, table: profile.table, + routeSelectionScope: "table-afi-community-observation", + community: profile.community, expectedGateway: candidate.gateway, expectedImmediate: { address: endpoint.address, interface: endpoint.interface }, binding: wanted, + bfd: { applicability: "not-configured", evidence: "explicit-use-bfd-false" }, + routes: affected.map(row => ({ nativeId: row[".id"], prefix: row["dst-address"], originalGateway: row.gateway })).sort((a, b) => String(a.prefix).localeCompare(String(b.prefix))), + probe: { key: candidateId, serverId: profile.serverId, destination: probe.destination, source: probe.source, interface: candidate.wanInterface, + probeVrf: profile.probeVrf, expectedHops: probe.expected } }) + } catch { return { status: "unknown", reason: "prepared-effect-selection-unsupported-or-ambiguous" } } +} + +type Snapshot = { observedAt: string; servers: Array<{ serverId: number; routes: { payload: Row[] }; bgp: { payload: Row[]; connectionsPayload: Row[] }; logs: { buffer: string; payload: Row[] } }>; + probes: Array<{ key: string; pingPayload: Row[]; tracePayload?: Row[] }> } +export function originalEffectBaselineMatches(selection: Prepared, sample: Snapshot) { + const source = sample.servers.find(row => row.serverId === selection.serverId) + if (!source) return false + const actual = selectedRows(source.routes.payload, selection) + return actual.length === selection.routes.length && selection.routes.every(wanted => actual.filter(row => row[".id"] === wanted.nativeId && row["dst-address"] === wanted.prefix && row.gateway === wanted.originalGateway).length === 1) +} + +/** Actual bounded native captures only; all stronger causal/whole-path claims + * remain false. Related log inspection is reported without completeness. */ +export async function inspectOriginalEffectRounds(rawSelection: unknown, samples: Snapshot[], baseline: Snapshot) { + const selection = prepared.parse(rawSelection), rounds: z.infer[] = [] + if (!originalEffectBaselineMatches(selection, baseline)) return unknownOriginalEffects("prepared-effect-baseline-drift") + const { inspectControlPing, inspectTrace } = await import("./path-verification.js") + for (const sample of samples) { + const source = sample.servers.find(row => row.serverId === selection.serverId) + if (!source) return unknownOriginalEffects("effect-source-missing") + const scope = selectedRows(source.routes.payload, selection) + const scopeChanged = fingerprint(scope.map(row => row["dst-address"]).sort()) !== fingerprint(selection.routes.map(row => row.prefix).sort()) + const routes = selection.routes.map(wanted => { + const matching = source.routes.payload.filter(row => row["routing-table"] === selection.table && row["dst-address"] === wanted.prefix && family(row) === selection.family) + let status: z.infer = "unknown" + if (!scopeChanged && matching.length === 1) { + const row = matching[0] + if (flag(row.bgp) === true && flag(row.connect) !== true && flag(row.static) !== true && flag(row.active) !== undefined && row.ecmp !== undefined && flag(row.ecmp) === false && + ["disabled", "filtered", "unreachable", "blackhole", "invalid"].every(key => row[key] === undefined || flag(row[key]) === false)) { + try { + const final = routeGatewayContext(String(row["immediate-gw"])) + status = flag(row.active) === true && row.contribution === "active" && row.gateway === selection.expectedGateway && final.address === selection.expectedImmediate.address && final.interface === selection.expectedImmediate.interface ? "observed-expected" : "observed-other" + } catch { /* no final resolution evidence */ } + } + } + return { prefix: wanted.prefix, status } + }) + const connections = unique(source.bgp.connectionsPayload, selection.binding.connectionId) + const session = selectBgpSession(source.bgp.payload, selection.binding) + let bgp: z.infer = "unknown" + if (connections.length === 1 && connectionMatches(connections[0], selection.binding) && flag(connections[0].disabled) === false && flag(connections[0]["use-bfd"]) === false && + session?.[".id"] === selection.binding.sessionId) bgp = flag(session.established) ? "observed-expected" : "observed-other" + const probe = sample.probes.find(row => row.key === selection.probe.key) + let controlProbe: "passed" | "failed" | "unknown" = "unknown" + if (probe) { + const ping = inspectControlPing(z.array(z.record(z.string(), z.string())).max(256).parse(probe.pingPayload)) + if (ping.confirmedFailure) controlProbe = "failed" + else if (ping.ping.measured && ping.ping.loss === 0 && inspectTrace(probe.tracePayload, selection.probe.destination, selection.probe.expectedHops).verified) controlProbe = "passed" + } + rounds.push(roundSchema.parse({ observedAt: sample.observedAt, routes, bgp, + bfd: connections.length === 1 && flag(connections[0]["use-bfd"]) === false ? "not-configured" : "unknown", controlProbe, + relatedTopicRows: source.logs.payload.filter(row => row.buffer === source.logs.buffer && typeof row.topics === "string" && row.topics.split(",").some(topic => ["bgp", "bfd", "route", "interface"].includes(topic))).length })) + } + if (rounds.length !== 2) return unknownOriginalEffects("two-effect-rounds-required") + const all = rounds.flatMap(round => [...round.routes.map(route => route.status), round.bgp, round.bfd === "unknown" ? "unknown" : "observed-expected", round.controlProbe === "passed" ? "observed-expected" : round.controlProbe === "failed" ? "observed-other" : "unknown"]) + const before = baseline.servers.find(row => row.serverId === selection.serverId)! + // Native IDs alone may churn; they never count as a forwarding transition. + // Require an actual selected active destination's final forwarding endpoint + // to differ from the expected post-state seen consistently in both rounds. + // A changed logical gateway resolving to the SAME endpoint is not a FIB switch. + const changedPrefixes = selection.routes.filter(wanted => { + if (!rounds.every(round => round.routes.find(row => row.prefix === wanted.prefix)?.status === "observed-expected")) return false + const row = before.routes.payload.find(row => row[".id"] === wanted.nativeId && row["dst-address"] === wanted.prefix)! + if (flag(row.bgp) !== true || flag(row.active) !== true || row.contribution !== "active" || flag(row.connect) === true || flag(row.static) === true || flag(row.ecmp) !== false || + ["disabled", "filtered", "unreachable", "blackhole", "invalid"].some(key => row[key] !== undefined && flag(row[key]) !== false)) return false + try { + const endpoint = routeGatewayContext(String(row["immediate-gw"])) + routeGatewayContext(String(row.gateway)) + if (!endpoint.interface) return false + return endpoint.address !== selection.expectedImmediate.address || endpoint.interface !== selection.expectedImmediate.interface + } catch { return false } + }).map(row => row.prefix) + const postState = all.includes("unknown") ? "unknown" : all.includes("observed-other") ? "observed-other" : "observed-expected" + return originalEffectReportSchema.parse({ ...unknownOriginalEffects(""), rounds, reason: null, + selectionIdentity: fingerprint(selection), + transition: { baselineObservedAt: baseline.observedAt, changedPrefixes }, switchEffectObserved: postState === "observed-expected" && changedPrefixes.length > 0, + status: postState === "observed-expected" && !changedPrefixes.length ? "unknown" : postState, + ...(postState === "observed-expected" && !changedPrefixes.length ? { reason: "poststate-matched-without-selected-fib-transition" } : {}) }) +} + diff --git a/backend/src/modules/mcp/routing-profile-authority-integration.sql-helper.ts b/backend/src/modules/mcp/routing-profile-authority-integration.sql-helper.ts new file mode 100644 index 00000000..fe1e60ff --- /dev/null +++ b/backend/src/modules/mcp/routing-profile-authority-integration.sql-helper.ts @@ -0,0 +1,38 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import type { AuthUser } from "../../lib/permissions.js" + +/** Reuses the COMPLETE existing scoped/manual/recovery and management SQL/HTTP + * acceptance, plus persistent authority cases. Not a narrowed snapshot runner. + * Invoke only after migrations in the owned disposable PostgreSQL harness. */ +export async function acceptRoutingProfileAuthorityIntegrationSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../../db/index.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + assert.equal((await pool.query("SELECT id FROM servers WHERE id=131")).rows.length, 0, "Existing scoped fixture ID must be unowned before this isolated run") + // Importing the action registry registers only the existing production tools; + // no test alias, new production route or collector is installed here. + await import("./actions.js") + const { acceptScopedRouteCommunitySwitch } = await import("./routing-community-scoped-acceptance.js") + const { acceptManagementTransport } = await import("./management-transport-acceptance.js") + const previousEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean + const now = Date.now() + const actor: AuthUser = { id: "routing-authority-integration-owner-" + randomUUID(), email: "synthetic@example.invalid", name: "Synthetic routing authority owner", + apps: ["mm"], permissions: ["mm:network:admin", "mm:filters:admin", "mm:mcp:admin", "mm:mcp:approve", "mm:servers:admin"], issuedAt: now - 1000, expiresAt: now + 3600000 } + try { + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + const scoped = await acceptScopedRouteCommunitySwitch(actor) + const management = await acceptManagementTransport(actor) + assert.equal(scoped.persistentAuthorityCases, 17, "Includes the six additional genuine configuration-bridge scenarios") + assert.equal(management.metadataAdmissionCases, 7) + assert.equal(management.metadataNativeRequests, 0) + return { scoped, management, realSql: true, transport: "loopback HTTP fixtures", externalNetwork: false, forwardingVerified: false, controllerEnabled: false } + } finally { + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousEnabled]) + } +} diff --git a/backend/src/modules/mcp/routing-profile-authority.sql-helper.ts b/backend/src/modules/mcp/routing-profile-authority.sql-helper.ts new file mode 100644 index 00000000..809d3a06 --- /dev/null +++ b/backend/src/modules/mcp/routing-profile-authority.sql-helper.ts @@ -0,0 +1,109 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import type { McpPrincipal } from "./store.js" + +/** Owned disposable PostgreSQL only. Real schema, principal validation, private + * HMAC and guarded configuration.intent; the PATCH callback is just a counter. + * No module registration, RouterOS/HTTP call, retention or deletion. */ +export async function acceptRoutingProfileAuthoritySql() { + assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + // All imports with runtime DB/key side effects follow the ownership guard. + const { pool } = await import("../../db/index.js") + const security = await import("./security.js") + const { mcpConnectionPolicySchema } = await import("@mmapp/contracts/mcp") + const { routeAiProfileSchema } = await import("@mmapp/contracts/mcp-routing") + const { resolveRoutingProfileAuthority, assertRoutingProfileAuthority } = await import("./routing-profile-authority.js") + const { readRoutingEndpoints, createRoutingSourceContext } = await import("./routing-observation-source.js") + const { withVerifiedConfigurationChange, executeGuardedConfigurationWrite } = await import("../../services/config-mutation-policy.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + const previousEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean + const ids: number[] = [], profileIds: string[] = [] + let cases = 0, simulatedPatchDispatches = 0 + const names: string[] = [] + try { + const marker = "authority-synthetic-" + randomUUID() + ids.push(...(await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES($1,'127.0.0.1',9,true,'SYNTHETIC','SYNTHETIC',false,true),($2,'127.0.0.1',9,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id", [marker + "-source", marker + "-exit"])).rows.map(row => Number(row.id))) + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + const profile = routeAiProfileSchema.parse({ name: marker, serverId: ids[0], community: "65000:10", family: "ipv4", autoRecovery: true, + candidates: [{ id: "a", jumpHostId: ids[0], exitNodeId: ids[1], wanInterface: "synthetic-wan", downstreamInterface: "synthetic-hop", + gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*1" }, + controlProbe: { targetInterface: "synthetic-exit", expectedHops: [{ serverId: ids[1], interface: "synthetic-exit" }] } }] }) + const source = await createRoutingSourceContext(await readRoutingEndpoints(ids)) + const transport = { contextSources: source.expected } + const fixture = async () => { + const connectionId = randomUUID(), credentialId = randomUUID(), profileId = randomUUID(), ownerId = marker + "-owner-" + randomUUID() + const now = Date.now(), expires = new Date(now + 600000).toISOString(), generation = await security.accessGeneration() + const permissions = ["mm:network:read", "mm:network:write", "mm:filters:write"] + const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt: expires, + scope: { serverIds: ids, operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "operations.recover"], allowGlobal: false }, + limits: { callsPerMinute: 100, mutationsPerHour: 100, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } }) + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'synthetic routing authority',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), generation, expires, new Date(now - 1000)]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), expires]) + await pool.query("INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version) VALUES($1,$2,$3::jsonb,true,$4,$5,$6,1)", [profileId, connectionId, JSON.stringify(profile), expires, credentialId, generation]) + profileIds.push(profileId) + const principal: McpPrincipal = { connectionId, credentialId, credentialExpiresAt: expires } + const context = { profile, principal, ownerId, transport } + const binding = await resolveRoutingProfileAuthority({ ...context, profileId, expectedVersion: 1 }) + return { context, binding, connectionId, credentialId, profileId, ownerId } + } + type Fixture = Awaited> + const check = (name: string) => { cases++; names.push(name) } + { + const f = await fixture() + assert.equal((await assertRoutingProfileAuthority(f.binding, f.context)).profileVersion, 1) + await assertRoutingProfileAuthority(f.binding, { ...f.context, use: "recovery" }) + await assert.rejects(resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId, expectedVersion: 2 })) + await assert.rejects(assertRoutingProfileAuthority({ ...f.binding, proof: "f".repeat(64) }, f.context)) + await assert.rejects(assertRoutingProfileAuthority(f.binding, { ...f.context, ownerId: "foreign" })) + await assert.rejects(assertRoutingProfileAuthority(f.binding, { ...f.context, transport: { contextSources: source.expected.map(row => ({ ...row, proof: "f".repeat(64) })) } })) + check("real resolver, recovery authority, version, tamper, owner and transport") + } + const changes: Array<[string, (f: Fixture) => Promise]> = [ + ["profile version", f => pool.query("UPDATE mcp_routing_profiles SET version=version+1 WHERE id=$1", [f.profileId])], + ["profile edit", f => pool.query("UPDATE mcp_routing_profiles SET config=jsonb_set(config,'{name}','\"edited\"') WHERE id=$1", [f.profileId])], + ["same-config ABA version", async f => { + await pool.query("UPDATE mcp_routing_profiles SET config=jsonb_set(config,'{name}','\"temporary\"'),version=version+1 WHERE id=$1", [f.profileId]) + await pool.query("UPDATE mcp_routing_profiles SET config=$2::jsonb,version=version+1 WHERE id=$1", [f.profileId, JSON.stringify(profile)]) + }], + ["disabled", f => pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", [f.profileId])], + ["emergency pause", f => pool.query("UPDATE mcp_routing_profiles SET state='{\"pausedReason\":\"synthetic emergency\"}'::jsonb WHERE id=$1", [f.profileId])], + ["profile grant expired", f => pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=clock_timestamp()-interval '1 second' WHERE id=$1", [f.profileId])], + ["profile grant extended", f => pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=grant_expires_at+interval '1 second' WHERE id=$1", [f.profileId])], + ["credential revoked", f => pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [f.credentialId])], + ["credential expired", f => pool.query("UPDATE mcp_credentials SET expires_at=clock_timestamp()-interval '1 second' WHERE id=$1", [f.credentialId])], + ["policy version", f => pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{version}','2') WHERE id=$1", [f.connectionId])], + ["policy paused", f => pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"') WHERE id=$1", [f.connectionId])], + ["server scope", f => pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}',$2::jsonb) WHERE id=$1", [f.connectionId, JSON.stringify([ids[0]])])], + ["owner identity", f => pool.query("UPDATE mcp_connections SET owner_id=$2 WHERE id=$1", [f.connectionId, marker + "-foreign"])], + ["owner expired", f => pool.query("UPDATE mcp_connections SET owner_valid_until=clock_timestamp()-interval '1 second' WHERE id=$1", [f.connectionId])], + ["owner permission revoked", f => pool.query("UPDATE mcp_connections SET owner_permissions='[\"mm:network:read\"]'::jsonb WHERE id=$1", [f.connectionId])], + ["portal owner claims revoked", f => pool.query("INSERT INTO mcp_owner_revocations(owner_id,invalid_before) VALUES($1,clock_timestamp())", [f.ownerId])], + ] + for (const [name, change] of changes) { + const f = await fixture(), path = "/routing/filter/rule/*1", before = simulatedPatchDispatches + await assert.rejects(withVerifiedConfigurationChange({ actorId: f.ownerId, connectionId: f.connectionId, approved: true, + serverIds: ids, objects: [], serverWide: true, authorize: async () => { await assertRoutingProfileAuthority(f.binding, f.context) }, + beforeDispatch: async () => { + const intent = (await pool.query("SELECT id FROM mcp_audit WHERE connection_id=$1 AND action='configuration.intent' AND payload->>'path'=$2 LIMIT 2", [f.connectionId, path])).rows + assert.equal(intent.length, 1, "Common guarded boundary must durably record intent before the mutation") + await change(f) + await assertRoutingProfileAuthority(f.binding, f.context) + }, + }, () => executeGuardedConfigurationWrite(ids[0], "PATCH", path, async () => { simulatedPatchDispatches++ })), /Постоянный допуск/) + assert.equal(simulatedPatchDispatches, before, `${name}: no simulated PATCH after lost authority`) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action='configuration.unconfirmed'", [f.connectionId])).rows[0].n, 1) + check(name + " after durable intent") + } + return { cases, names, simulatedPatchDispatches, realSql: true, productionHmac: true, realGuardedIntent: true, externalNetwork: false, controllerEnabled: false } + } finally { + // Retain evidence. Never delete fixture rows or touch pre-existing servers. + if (profileIds.length) await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=ANY($1::uuid[])", [profileIds]) + if (ids.length) await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [ids]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousEnabled]) + } +} diff --git a/backend/src/modules/mcp/routing-profile-authority.test.ts b/backend/src/modules/mcp/routing-profile-authority.test.ts new file mode 100644 index 00000000..6918f406 --- /dev/null +++ b/backend/src/modules/mcp/routing-profile-authority.test.ts @@ -0,0 +1,193 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { mkdtemp, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { dirname, join, resolve } from "node:path" +import { test } from "node:test" +import type { PoolClient } from "pg" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import type { ConnectionRow } from "./store.js" +import { assertRoutingProfileAuthority, resolveRoutingProfileAuthority, type RoutingProfileAuthorityContext, type RoutingProfileAuthorityDependencies } from "./routing-profile-authority.js" +import { assertPersistentScopedProfileAuthority, scopedProfileAuthorityTransport } from "./routing-profile-authority.js" +import { assertScopedCommunityRequest } from "./routing-community-scoped-request.js" +import { fingerprint, privateSecretFingerprint } from "./security.js" + +function fixture() { + let now = Date.parse("2026-10-25T00:00:00Z"), generation = "generation-a" + const expiry = (offset: number) => new Date(now + offset) + const profileId = randomUUID(), connectionId = randomUUID(), credentialId = randomUUID() + const profile = routeAiProfileSchema.parse({ name: "profile", serverId: 1, community: "65000:10", family: "ipv4", autoRecovery: true, + candidates: [{ id: "a", jumpHostId: 2, exitNodeId: 3, wanInterface: "wan", downstreamInterface: "hop", gateway: "192.0.2.1", + gatewayRef: { resource: "ipv4_route", id: "*1" }, controlProbe: { targetInterface: "exit", expectedHops: [{ serverId: 3, interface: "independent-hop" }] } }] }) + const connection = { id: connectionId, owner_id: "owner", owner_permissions: ["mm:network:read", "mm:network:write", "mm:filters:write"], + generation, owner_valid_until: expiry(600000), policy: { version: 1, status: "active", expiresAt: expiry(600000).toISOString(), + permissions: ["mm:network:read", "mm:network:write", "mm:filters:write"], scope: { serverIds: [1, 2, 3], + operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "operations.recover"] } } } as ConnectionRow + const row: Record = { id: profileId, connection_id: connectionId, version: 1, enabled: true, grant_expires_at: expiry(120000), + credential_id: credentialId, generation, policy_version: 1, config: profile, state: {}, credential_expires_at: expiry(600000), credential_revoked_at: null, credential_kind: "bearer" } + let rows = [row], failSql = false, failConnection = false, signDelay = 0 + let afterQuery: (() => void) | undefined + const queries: Array<{ sql: string; values: unknown[] }> = [] + const context: RoutingProfileAuthorityContext = { profile, ownerId: "owner", principal: { connectionId, credentialId, credentialExpiresAt: expiry(600000).toISOString() }, + transport: { contextSources: [1, 2, 3].map(serverId => ({ serverId, proof: "a".repeat(64) })), + management: { channelId: randomUUID(), proof: "b".repeat(64), expiresAt: expiry(300000).toISOString() } } } + const deps: RoutingProfileAuthorityDependencies = { + db: { query: async (sql: string, values: unknown[]) => { queries.push({ sql, values }); if (failSql) throw Error("private SQL detail"); afterQuery?.(); return { rows } } } as unknown as Pick, + currentConnection: async () => { if (failConnection) throw Error("revoked credential"); return connection }, + now: () => now, generation: async () => generation, + sign: async value => { now += signDelay; return createHmac("sha256", "private-test-key").update(value).digest("hex") }, + } + return { profileId, context, deps, row, connection, queries, setRows: (value: typeof rows) => { rows = value }, + advance: (ms: number) => { now += ms }, generation: (value: string) => { generation = value }, + afterQuery: (work: () => void) => { afterQuery = work }, + failSql: () => { failSql = true }, failConnection: () => { failConnection = true }, signDelay: (ms: number) => { signDelay = ms } } +} + +test("resolver uses bounded exact SQL and sealed full scope/transport; assertion re-reads", async () => { + const f = fixture(), binding = await resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId, expectedVersion: 1 }, f.deps) + assert.deepEqual(binding.packet.scope, [1, 2, 3]) + assert.equal(binding.packet.grantExpiresAt, binding.packet.validUntil) + assert.equal(JSON.stringify(binding).includes("192.0.2.1"), false) + await assertRoutingProfileAuthority(binding, f.context, f.deps) + await assertRoutingProfileAuthority(binding, { ...f.context, use: "recovery" }, f.deps) + assert.equal(f.queries.length, 3) + for (const query of f.queries) { + assert.match(query.sql, /WHERE p.id=\$1 AND p.connection_id=\$2 LIMIT 2/) + assert.match(query.sql, /octet_length\(p.config::text\)<=262144/) + assert.deepEqual(query.values, [f.profileId, f.context.principal.connectionId]) + } +}) + +const rowChanges: Record) => void> = { + version: f => { f.row.version = 2 }, disabled: f => { f.row.enabled = false }, paused: f => { f.row.state = { pausedReason: "emergency" } }, + malformedState: f => { f.row.state = [] }, overflowState: f => { f.row.state = null }, overflowConfig: f => { f.row.config = null }, + editedConfig: f => { f.row.config = { ...f.context.profile, name: "edited" } }, + shortenedGrant: f => { f.row.grant_expires_at = new Date(Date.parse(String(f.row.grant_expires_at)) - 1000) }, + extendedGrant: f => { f.row.grant_expires_at = new Date(Date.parse(String(f.row.grant_expires_at)) + 1000) }, + credentialExpiry: f => { f.row.credential_expires_at = new Date("2026-10-25T00:09:00Z") }, + ownerExpiry: f => { f.connection.owner_valid_until = new Date("2026-10-25T00:09:00Z") }, + credential: f => { f.row.credential_id = randomUUID() }, revokedCredential: f => { f.row.credential_revoked_at = new Date() }, + refreshCredential: f => { f.row.credential_kind = "refresh" }, profileConnection: f => { f.row.connection_id = randomUUID() }, + policyVersion: f => { f.row.policy_version = 2 }, generation: f => { f.row.generation = "other" }, + deletion: f => f.setRows([]), ambiguousRows: f => f.setRows([f.row, f.row]), + sqlFailure: f => f.failSql(), currentCredentialRevoked: f => f.failConnection(), globalGeneration: f => f.generation("other"), + owner: f => { f.connection.owner_id = "other" }, ownerPermission: f => { f.connection.owner_permissions = ["mm:network:read"] }, + connectionPaused: f => { f.connection.policy.status = "paused" }, serverScope: f => { f.connection.policy.scope.serverIds = [1, 2] }, + operationScope: f => { f.connection.policy.scope.operationIds = ["routing.analyze"] }, + expired: f => f.advance(120000), +} +for (const [name, change] of Object.entries(rowChanges)) test(`persistent authority rejects ${name} on fresh admission`, async () => { + const f = fixture(), binding = await resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps) + change(f) + await assert.rejects(assertRoutingProfileAuthority(binding, f.context, f.deps), /Постоянный допуск/) +}) + +test("binding cannot be edited, forged, or moved to another transport/principal", async () => { + const f = fixture(), binding = await resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps) + for (const altered of [{ ...binding, proof: "f".repeat(64) }, { ...binding, packet: { ...binding.packet, profileVersion: 2 } }, + { ...binding, unexpected: true }]) await assert.rejects(assertRoutingProfileAuthority(altered, f.context, f.deps)) + for (const context of [ + { ...f.context, principal: { ...f.context.principal, credentialId: randomUUID() } }, + { ...f.context, transport: { ...f.context.transport, management: { ...f.context.transport.management!, channelId: randomUUID() } } }, + { ...f.context, transport: { contextSources: f.context.transport.contextSources.map(value => ({ ...value, proof: "e".repeat(64) })) } }, + ]) await assert.rejects(assertRoutingProfileAuthority(binding, context, f.deps)) +}) + +test("source order is irrelevant but duplicate/missing/extra source identities refuse", async () => { + const f = fixture(), binding = await resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps) + await assertRoutingProfileAuthority(binding, { ...f.context, transport: { ...f.context.transport, contextSources: [...f.context.transport.contextSources].reverse() } }, f.deps) + for (const sources of [f.context.transport.contextSources.slice(1), [...f.context.transport.contextSources, f.context.transport.contextSources[0]], + [...f.context.transport.contextSources, { serverId: 5, proof: "a".repeat(64) }]]) { + await assert.rejects(resolveRoutingProfileAuthority({ ...f.context, transport: { contextSources: sources }, profileId: f.profileId }, f.deps)) + } +}) + +test("recovery needs unchanged original autoRecovery and recovery operation permission", async () => { + for (const missing of ["autoRecovery", "operation"]) { + const f = fixture() + if (missing === "autoRecovery") { f.context.profile.autoRecovery = false; f.row.config = f.context.profile } + else f.connection.policy.scope.operationIds = f.connection.policy.scope.operationIds.filter(value => value !== "operations.recover") + const binding = await resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps) + await assertRoutingProfileAuthority(binding, f.context, f.deps) + await assert.rejects(assertRoutingProfileAuthority(binding, { ...f.context, use: "recovery" }, f.deps)) + } +}) + +test("resolver rejects wrong expected version and cancellation; signing cannot extend expiry", async () => { + const f = fixture() + await assert.rejects(resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId, expectedVersion: 2 }, f.deps)) + const abort = new AbortController(); abort.abort() + await assert.rejects(resolveRoutingProfileAuthority({ ...f.context, signal: abort.signal, profileId: f.profileId }, f.deps)) + f.signDelay(120000) + await assert.rejects(resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps)) +}) + +test("each expiry independently limits admission even when other grants remain current", async () => { + for (const kind of ["profile", "credential", "owner", "policy", "principal", "channel"]) { + const f = fixture(), expired = new Date("2026-10-25T00:00:00Z") + if (kind === "profile") f.row.grant_expires_at = expired + if (kind === "credential") f.row.credential_expires_at = expired + if (kind === "owner") f.connection.owner_valid_until = expired + if (kind === "policy") f.connection.policy.expiresAt = expired.toISOString() + if (kind === "principal") f.context.principal.credentialExpiresAt = expired.toISOString() + if (kind === "channel") f.context.transport.management!.expiresAt = expired.toISOString() + await assert.rejects(resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps)) + } +}) + +test("late emergency pause invalidates the same sealed binding at the next transport admission", async () => { + const f = fixture(), binding = await resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps) + await assertRoutingProfileAuthority(binding, f.context, f.deps) + // Model the durable-intent boundary; no actual transport is used here. + f.row.enabled = false + let writes = 0 + await assert.rejects((async () => { await assertRoutingProfileAuthority(binding, f.context, f.deps); writes++ })()) + assert.equal(writes, 0) +}) + +test("connection authority is validated again after the bounded profile SQL await", async () => { + for (const change of ["owner", "policy", "credential", "generation"]) { + const f = fixture() + f.afterQuery(() => { + if (change === "owner") f.connection.owner_id = "changed-during-sql" + if (change === "policy") f.connection.policy.version++ + if (change === "credential") f.failConnection() + if (change === "generation") f.generation("changed-during-sql") + }) + await assert.rejects(resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps)) + } +}) + +test("scoped request uses real private HMAC domains for manual v2 and authority v3", async () => { + const directory = await mkdtemp(join(tmpdir(), "mm-routing-authority-proof-")), previous = process.env.MCP_STATE_DIR + assert.equal(dirname(directory), resolve(tmpdir())) + process.env.MCP_STATE_DIR = directory + try { + const f = fixture(), binding = await resolveRoutingProfileAuthority({ ...f.context, profileId: f.profileId }, f.deps) + for (const revision of [2, 3]) { + const request: Record = { profile: f.context.profile, receiptId: randomUUID(), proofRevision: revision, + ...(revision === 3 ? { persistentProfileAuthority: binding } : {}) } + const requestProof = await privateSecretFingerprint(fingerprint(request), `route-ai-scoped-request-v${revision}`) + assert.equal(await assertScopedCommunityRequest({ ...request, requestProof }), requestProof) + await assert.rejects(assertScopedCommunityRequest({ ...request, requestProof, profile: { ...f.context.profile, name: "tampered" } })) + if (revision === 3) { + const { persistentProfileAuthority: _authority, ...without } = request + await assert.rejects(assertScopedCommunityRequest({ ...without, requestProof })) + await assert.rejects(assertScopedCommunityRequest({ ...request, requestProof, proofRevision: 2 })) + } else await assert.rejects(assertScopedCommunityRequest({ ...request, persistentProfileAuthority: binding, requestProof })) + } + } finally { + if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous + await rm(directory, { recursive: true, force: true }) + } +}) + +test("manual private input remains compatible while malformed present authority fails closed", async () => { + assert.equal(await assertPersistentScopedProfileAuthority({}), undefined) + await assert.rejects(assertPersistentScopedProfileAuthority({ persistentProfileAuthority: null })) + await assert.rejects(assertPersistentScopedProfileAuthority({ persistentProfileAuthority: { packet: {}, proof: "f".repeat(64) } })) + const f = fixture() + assert.deepEqual(await scopedProfileAuthorityTransport({ contextSources: f.context.transport.contextSources }), { contextSources: f.context.transport.contextSources }) + await assert.rejects(scopedProfileAuthorityTransport({ contextSources: null })) +}) + diff --git a/backend/src/modules/mcp/routing-profile-authority.ts b/backend/src/modules/mcp/routing-profile-authority.ts new file mode 100644 index 00000000..e4866e68 --- /dev/null +++ b/backend/src/modules/mcp/routing-profile-authority.ts @@ -0,0 +1,180 @@ +import type { PoolClient } from "pg" +import { z } from "zod" +import { routeAiProfileSchema, type RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { accessGeneration, decryptSecret, fingerprint, McpError, privateSecretFingerprint } from "./security.js" +import { effectiveMcpConnection, type ConnectionRow, type McpPrincipal } from "./store.js" +import { assertManagementTransportBinding, type ManagementTransportBinding } from "./management-transport.js" + +const proof = z.string().regex(/^[a-f0-9]{64}$/) +const transportSchema = z.object({ + contextSources: z.array(z.object({ serverId: z.number().int().positive().safe(), proof }).strict()).min(1).max(1000), + management: z.object({ channelId: z.string().uuid(), proof, expiresAt: z.string().datetime() }).strict().optional(), +}).strict() +export type RoutingProfileAuthorityTransport = z.infer +const packetSchema = z.object({ + revision: z.literal(1), profileId: z.string().uuid(), profileVersion: z.number().int().positive().safe(), + connectionId: z.string().uuid(), credentialId: z.string().uuid(), ownerId: z.string().min(1).max(1000), + principalExpiresAt: z.string().datetime(), credentialExpiresAt: z.string().datetime(), grantExpiresAt: z.string().datetime(), + ownerExpiresAt: z.string().datetime(), policyExpiresAt: z.string().datetime(), validUntil: z.string().datetime(), + generation: z.string().min(1).max(1000), connectionGeneration: z.string().min(1).max(1000), policyVersion: z.number().int().positive(), + policyProof: proof, ownerPermissionsProof: proof, profileProof: proof, scope: z.array(z.number().int().positive().safe()).min(1).max(1000), + transportProof: proof, +}).strict() +export type RoutingProfileAuthorityPacket = z.infer +const bindingSchema = z.object({ packet: packetSchema, proof }).strict() +export const routingProfileAuthorityBindingSchema = bindingSchema +export type RoutingProfileAuthorityBinding = z.infer +type Sql = Pick +export interface RoutingProfileAuthorityDependencies { + db: Sql + currentConnection: (principal: McpPrincipal, db: Sql) => Promise + generation: () => Promise + sign: (value: string) => Promise + now: () => number +} +const production: RoutingProfileAuthorityDependencies = { + db: pool, currentConnection: effectiveMcpConnection, generation: accessGeneration, now: Date.now, + sign: value => privateSecretFingerprint(value, "route-ai-persistent-profile-authority-v1"), +} +export interface RoutingProfileAuthorityContext { + profile: RouteAiProfile + principal: McpPrincipal + ownerId: string + /** Already verified by the context/management reader. This module pins its + * exact identity; it does not replace that reader's live endpoint fences. */ + transport: RoutingProfileAuthorityTransport + signal?: AbortSignal +} +const fail = () => new McpError("STATE_CONFLICT", "Постоянный допуск профиля маршрутизации недоступен или изменён", 409) +const date = (value: unknown): string => { + if (!(value instanceof Date) || !Number.isFinite(value.getTime())) throw fail() + return value.toISOString() +} +function scope(profile: RouteAiProfile) { + return [...new Set([profile.serverId, ...profile.candidates.flatMap(candidate => [candidate.jumpHostId, candidate.exitNodeId, + ...(candidate.controlProbe?.expectedHops.map(hop => hop.serverId) ?? [])])])].sort((a, b) => a - b) +} +function transport(value: RoutingProfileAuthorityTransport, ids: number[]) { + const parsed = transportSchema.parse(value) + const sources = [...parsed.contextSources].sort((a, b) => a.serverId - b.serverId) + if (new Set(sources.map(source => source.serverId)).size !== sources.length || fingerprint(sources.map(source => source.serverId)) !== fingerprint(ids)) throw fail() + return { ...parsed, contextSources: sources } +} +function active(context: RoutingProfileAuthorityContext, deps: RoutingProfileAuthorityDependencies) { + if (context.signal?.aborted || !Number.isFinite(deps.now())) throw fail() +} + +/** Single exact profile row; byte caps are enforced in SQL before returning + * JSON to Node. No catalog scan or credentials/configuration is published. */ +async function snapshot(profileId: string, context: RoutingProfileAuthorityContext, deps: RoutingProfileAuthorityDependencies): Promise { + active(context, deps) + z.string().uuid().parse(profileId) + const profile = routeAiProfileSchema.parse(context.profile), ids = scope(profile), path = transport(context.transport, ids) + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict().parse(context.principal) + const connection = await deps.currentConnection(principal, deps.db) + active(context, deps) + const generation = await deps.generation() + if (connection.id !== principal.connectionId || connection.owner_id !== context.ownerId || connection.policy.status !== "active" || + connection.generation !== generation || ids.some(id => !connection.policy.scope.serverIds.includes(id))) throw fail() + for (const permission of ["mm:network:read", "mm:network:write", "mm:filters:write"]) { + if (!hasPermission(connection.owner_permissions, permission) || !hasPermission(connection.policy.permissions, permission)) throw fail() + } + for (const operation of ["routing.analyze", "routing.community.switch", "routing.path.verify"]) if (!connection.policy.scope.operationIds.includes(operation)) throw fail() + const connectionAuthority = (value: ConnectionRow) => fingerprint({ id: value.id, owner: value.owner_id, permissions: value.owner_permissions, + policy: value.policy, generation: value.generation, ownerExpiresAt: date(value.owner_valid_until), ownerClaimsIssuedAt: value.owner_claims_issued_at }) + const beforeConnectionProof = connectionAuthority(connection) + const { rows } = await deps.db.query(`SELECT p.id,p.connection_id,p.version,p.enabled,p.grant_expires_at,p.credential_id,p.generation,p.policy_version, + CASE WHEN octet_length(p.config::text)<=262144 THEN p.config END AS config, + CASE WHEN octet_length(p.state::text)<=65536 THEN p.state END AS state, + c.expires_at AS credential_expires_at,c.revoked_at AS credential_revoked_at,c.kind AS credential_kind + FROM mcp_routing_profiles p JOIN mcp_credentials c ON c.id=p.credential_id AND c.connection_id=p.connection_id + WHERE p.id=$1 AND p.connection_id=$2 LIMIT 2`, [profileId, principal.connectionId]) + active(context, deps) + // The bounded SQL read itself is an await boundary. A portal/credential or + // policy revocation committed while it ran must not receive a new proof. + const afterConnection = await deps.currentConnection(principal, deps.db) + if (beforeConnectionProof !== connectionAuthority(afterConnection) || generation !== await deps.generation()) throw fail() + active(context, deps) + if (rows.length !== 1) throw fail() + const row = rows[0] + if (row.id !== profileId || row.connection_id !== connection.id || row.enabled !== true || row.credential_id !== principal.credentialId || + row.generation !== generation || row.policy_version !== connection.policy.version || row.credential_revoked_at !== null || + !["bearer", "access"].includes(row.credential_kind) || !Number.isSafeInteger(row.version) || row.version < 1 || + !row.state || typeof row.state !== "object" || Array.isArray(row.state) || Object.hasOwn(row.state, "pausedReason") || + !row.config || fingerprint(routeAiProfileSchema.parse(row.config)) !== fingerprint(profile)) throw fail() + const grantExpiresAt = date(row.grant_expires_at), credentialExpiresAt = date(row.credential_expires_at), ownerExpiresAt = date(connection.owner_valid_until) + const policyExpiresAt = z.string().datetime().parse(connection.policy.expiresAt) + const until = Math.min(Date.parse(grantExpiresAt), Date.parse(credentialExpiresAt), Date.parse(principal.credentialExpiresAt), + Date.parse(ownerExpiresAt), Date.parse(policyExpiresAt), path.management ? Date.parse(path.management.expiresAt) : Infinity) + if (!Number.isFinite(until) || until <= deps.now()) throw fail() + return packetSchema.parse({ revision: 1, profileId, profileVersion: row.version, connectionId: connection.id, credentialId: principal.credentialId, + ownerId: connection.owner_id, principalExpiresAt: principal.credentialExpiresAt, credentialExpiresAt, grantExpiresAt, ownerExpiresAt, policyExpiresAt, + validUntil: new Date(until).toISOString(), generation, connectionGeneration: connection.generation, policyVersion: connection.policy.version, + policyProof: fingerprint(connection.policy), ownerPermissionsProof: fingerprint(connection.owner_permissions), profileProof: fingerprint(profile), scope: ids, + transportProof: fingerprint(path) }) +} + +/** Internal preparation only. Never expose this resolver as a model tool or + * treat the returned binding as a reservation of budget/recovery permission. */ +export async function resolveRoutingProfileAuthority(input: RoutingProfileAuthorityContext & { profileId: string; expectedVersion?: number }, deps = production): Promise { + try { + const packet = await snapshot(input.profileId, input, deps) + if (input.expectedVersion !== undefined && input.expectedVersion !== packet.profileVersion) throw fail() + const binding = { packet, proof: await deps.sign(fingerprint(packet)) } + active(input, deps) + if (Date.parse(packet.validUntil) <= deps.now()) throw fail() + return bindingSchema.parse(binding) + } catch { throw fail() } +} + +/** Call from the live forward/recovery authorize callback, including AFTER + * durable intent immediately before transport. Reconcile must not use this + * mutation admission to infer an absent checkpoint or replay a PATCH. */ +export async function assertRoutingProfileAuthority(raw: RoutingProfileAuthorityBinding, context: RoutingProfileAuthorityContext & { use?: "forward" | "recovery" }, deps = production): Promise { + try { + active(context, deps) + if (Buffer.byteLength(JSON.stringify(raw)) > 131072) throw fail() + const binding = bindingSchema.parse(raw) + if (binding.proof !== await deps.sign(fingerprint(binding.packet)) || Date.parse(binding.packet.validUntil) <= deps.now()) throw fail() + const current = await snapshot(binding.packet.profileId, context, deps) + if (fingerprint(binding.packet) !== fingerprint(current)) throw fail() + if (context.use === "recovery") { + const connection = await deps.currentConnection(context.principal, deps.db) + if (!routeAiProfileSchema.parse(context.profile).autoRecovery || !connection.policy.scope.operationIds.includes("operations.recover")) throw fail() + // Recovery still requires its separate reserved intent/budget and + // after-state original-candidate SLA/probe in the shared inverse engine. + if (fingerprint(connection.policy) !== current.policyProof || connection.owner_id !== current.ownerId) throw fail() + } + active(context, deps) + if (Date.parse(current.validUntil) <= deps.now()) throw fail() + return current + } catch { throw fail() } +} + +/** Extract only the exact already-verified transport identities from private + * service evidence. No caller JSON can create a signed profile authority. */ +export async function scopedProfileAuthorityTransport(input: Record, signal?: AbortSignal, db: Sql = pool): Promise { + const sources = transportSchema.shape.contextSources.parse(input.contextSources) + if (input.managementBindingCiphertext == null) return { contextSources: sources } + const ciphertext = z.string().min(1).max(262144).parse(input.managementBindingCiphertext) + const raw = JSON.parse(await decryptSecret(ciphertext)) + const inspected = await assertManagementTransportBinding(raw as ManagementTransportBinding, signal, db) + if (inspected.serverId !== input.serverId) throw fail() + const { serverId: _serverId, ...management } = inspected + return { contextSources: sources, management } +} + +/** The enclosing request signature supplies presence/absence integrity. Used + * by every service fence and by checkpoint publication after asynchronous SQL. */ +export async function assertPersistentScopedProfileAuthority(input: Record, signal?: AbortSignal, db: Sql = pool) { + if (input.persistentProfileAuthority === undefined) return + const binding = bindingSchema.parse(input.persistentProfileAuthority) + const profile = routeAiProfileSchema.parse(input.profile) + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict().parse(input.principal) + const ownerId = z.string().min(1).parse(input.ownerId), current = await effectiveMcpConnection(principal, db) + if (current.owner_id !== ownerId || current.id !== binding.packet.connectionId || current.owner_id !== binding.packet.ownerId || + fingerprint(current.policy) !== binding.packet.policyProof || scope(profile).some(id => !current.policy.scope.serverIds.includes(id))) throw fail() + return assertRoutingProfileAuthority(binding, { profile, principal, ownerId, transport: await scopedProfileAuthorityTransport(input, signal, db), signal }, { ...production, db }) +} diff --git a/backend/src/modules/mcp/routing-profiles.ts b/backend/src/modules/mcp/routing-profiles.ts new file mode 100644 index 00000000..d3bb791a --- /dev/null +++ b/backend/src/modules/mcp/routing-profiles.ts @@ -0,0 +1,214 @@ +import { assertNoActiveRoutingProfileOverlap } from "./routing-controller-overlap.js" +import { fingerprint } from "./security.js" +import { prepareRoutingControllerBaseline, assertRoutingControllerBaseline, routingControllerBaselinePrincipal, advanceRoutingControllerBaseline, type RoutingControllerBaseline } from "./routing-controller-baseline.js" +import { publicRoutingControllerState } from "./routing-community-public.js" +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { routeAiControllerBlockReason, routeAiProfileSchema, usesScopedRouteAiContext, type RouteAiProfile } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import { withMcpDatabaseRequest } from "./database-request-lease.js" +import { registerMcpService } from "./service-registry.js" +import { accessGeneration, McpError } from "./security.js" +import { audit, validatePrincipal, type McpPrincipal } from "./store.js" +import { decideRoutingSwitch, selectFailoverReserveProbes, type ControllerState, type PathHealth, type PublicRoutingRanking, type RoutingConfirmationObservations } from "./routing-model.js" +import type { add } from "./actions.js" +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" + +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" +const profileInScope = (config: unknown, serverIds: number[]) => targetIds(routeAiProfileSchema.parse(config)).every(id => serverIds.includes(id)) +const profileSaveSchema = z.object({ profileId: z.string().uuid().optional(), profile: routeAiProfileSchema, enabled: z.boolean().default(false), expiresAt: z.string().datetime({ offset: true }) }).strict() +function assertControllerCanBeEnabled(profile: RouteAiProfile) { + const reason = routeAiControllerBlockReason(profile) + if (reason) throw new McpError("UNSUPPORTED_CAPABILITY", reason, 409) +} +export function registerRoutingProfileActions(register: typeof add) { + register("routing.profiles.read", "mm_route_profiles_list", "Профили и состояние контроллера Route AI", "network", "mm:network:read", "read", "connection", z.object({}).strict(), { + read: async (_input, context) => ({ profiles: (await pool.query(`SELECT ${profilePublicColumns} FROM mcp_routing_profiles WHERE connection_id=$1 ORDER BY updated_at DESC LIMIT 200`, [context.connection.id])).rows.filter(row => profileInScope(row.config, context.connection.policy.scope.serverIds)).map(row => ({ ...row, state: publicRoutingControllerState(row.state) })) }), + }) + register("routing.controller.read", "mm_route_controller_get", "Состояние выбранного контроллера Route AI", "network", "mm:network:read", "read", "connection", z.object({ profileId: z.string().uuid() }).strict(), { + read: async (input, context) => { + const { rows: [row] } = await pool.query(`SELECT ${profilePublicColumns} FROM mcp_routing_profiles WHERE id=$1 AND connection_id=$2`, [input.profileId, context.connection.id]) + if (!row || !profileInScope(row.config, context.connection.policy.scope.serverIds)) throw new McpError("NOT_FOUND", "Профиль не найден", 404) + return { ...row, state: publicRoutingControllerState(row.state) } + }, + }) + registerMcpService("routing.profile.save", { + current: async input => (await pool.query("SELECT id,version,config,enabled,grant_expires_at FROM mcp_routing_profiles WHERE id=$1 AND connection_id=$2", [input.profileId, input.connectionId])).rows[0] ?? null, + execute: async input => { + if (input.enabled) assertControllerCanBeEnabled(routeAiProfileSchema.parse(input.profile)) + if (input.expectedVersion !== null && (!Number.isSafeInteger(input.expectedVersion) || Number(input.expectedVersion) < 1)) throw new McpError("PLAN_EXPIRED", "План не содержит ожидаемую версию профиля", 409) + if (input.enabled) { + const principal=await routingControllerBaselinePrincipal(input.baseline),connection=await validatePrincipal(principal) + await assertRoutingControllerBaseline(input.baseline,String(input.profileId),input.expectedVersion===null?1:Number(input.expectedVersion)+1,input.profile,String(input.expiresAt),{principal,connection}) + await assertNoActiveRoutingProfileOverlap(String(input.profileId),input.profile) + } + const row = await saveControllerProfileCas(pool, input) + if (!row) throw new McpError("STATE_CONFLICT", "Профиль изменён после подготовки плана; нужен новый план", 409) + return row + }, + verify: async (input, result) => !!result && (await pool.query("SELECT config,state,enabled,version,credential_id,generation,policy_version,grant_expires_at FROM mcp_routing_profiles WHERE id=$1 AND connection_id=$2", [input.profileId, input.connectionId])).rows.some(row => row.version === (result as { version: number }).version && row.version === (input.expectedVersion === null ? 1 : Number(input.expectedVersion) + 1) && JSON.stringify(routeAiProfileSchema.parse(row.config)) === JSON.stringify(routeAiProfileSchema.parse(input.profile)) && row.enabled === input.enabled && (!input.enabled || fingerprint(row.state.baseline)===fingerprint(input.baseline)) && row.credential_id === input.credentialId && row.generation === input.generation && row.policy_version === input.policyVersion && row.grant_expires_at.getTime() === Date.parse(String(input.expiresAt))), + }) + const profileTarget = (input: Record) => ({ scope: "servers" as const, serverIds: targetIds(routeAiProfileSchema.parse(input.profile)) }) + const prepareProfileSave = (updateOnly: boolean) => async (input: Record, context: ActionContext) => { + const profile = routeAiProfileSchema.parse(input.profile), ids = targetIds(profile), profileId = input.profileId ?? randomUUID() + if (input.enabled) assertControllerCanBeEnabled(profile) + const expires = Date.parse(String(input.expiresAt)), credentialExpires = Date.parse(context.principal.credentialExpiresAt) + if (expires <= Date.now() || expires > Math.min(Date.now() + 86400000, credentialExpires, Date.parse(context.connection.policy.expiresAt), context.connection.owner_valid_until.getTime())) throw new McpError("INVALID_EXPIRATION", "Допуск контроллера ограничен 24 часами и сроком подтверждения прав владельца", 400) + const expiresAt = new Date(expires).toISOString() + const existing = (await pool.query("SELECT id,connection_id,version,config,enabled,grant_expires_at FROM mcp_routing_profiles WHERE id=$1", [profileId])).rows[0] + if (existing ? existing.connection_id !== context.connection.id || !profileInScope(existing.config, context.connection.policy.scope.serverIds) : updateOnly) throw new McpError("NOT_FOUND", "Профиль не найден", 404) + const affectedIds = existing ? [...new Set([...ids, ...targetIds(routeAiProfileSchema.parse(existing.config))])] : ids + if (input.enabled && !["routing.analyze", "routing.community.switch"].every(id => context.connection.policy.scope.operationIds.includes(id))) throw new McpError("OPERATION_DENIED", "Для контроллера нужны отдельные разрешения анализа и переключения") + if(input.enabled)await assertNoActiveRoutingProfileOverlap(String(profileId),profile) + const baseline=input.enabled?await prepareRoutingControllerBaseline(String(profileId),(existing?.version??0)+1,profile,expiresAt,context):undefined + const { connection_id: _connection, ...before } = existing ?? {} + return { target: { scope: "servers" as const, serverIds: affectedIds }, objects: affectedIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), snapshots: [], before: existing ? before : null, + steps: [{ kind: "service" as const, service: "routing.profile.save", input: { ...input, expiresAt, profileId, expectedVersion: existing?.version ?? null, connectionId: context.connection.id, baseline, credentialId: context.principal.credentialId, generation: await accessGeneration(), policyVersion: context.connection.policy.version } }], requiresHuman: true, + warnings: ["Контроллер использует исходный доступ, лимиты и подтверждения. Отзыв токена или изменение политики останавливает его.", "При неизвестном пути или результате записи контроллер приостанавливается."] } + } + register("routing.profiles.save", "mm_route_profile_save_plan", "Сохранить профиль и временный допуск контроллера", "network", "mm:network:write", "write", "servers", profileSaveSchema, { + target: profileTarget, prepare: prepareProfileSave(false), version: 5, + }) + register("routing.controller.update", "mm_route_controller_update_plan", "Обновить профиль и допуск контроллера Route AI", "network", "mm:network:write", "write", "servers", profileSaveSchema.extend({ profileId: z.string().uuid(), enabled: z.boolean() }), { + target: profileTarget, prepare: prepareProfileSave(true), + }) +} + +interface PersistentController extends ControllerState { baseline?:RoutingControllerBaseline; planId?: string; candidateId?: string; expectedCandidateId?: string; reason?: string; checkedAt?: string; probedAt?: string; failoverReserveCursor?: string } +/** Also fenced per profile so direct/concurrent ticks cannot publish stale state. */ +export async function tickRoutingProfiles() { + let entered = false + try { + await withMcpDatabaseRequest(() => { + entered = true + return tickAdmittedRoutingProfiles() + }) + } catch (error) { + if (entered || !(error instanceof McpError) || !["PROTOCOL_STORAGE_BUSY", "RATE_LIMITED"].includes(error.code)) throw error + // A closed maintenance gate delays this tick; it is not a profile fault. + } +} + +async function tickAdmittedRoutingProfiles() { + const { invokeMcpTool, applyPlan } = await import("./runtime.js") + const { rows } = await pool.query("SELECT * FROM mcp_routing_profiles WHERE enabled ORDER BY updated_at LIMIT 100") + for (const listed of rows) { + await withControllerProfileLease(pool, listed.id, async lease => { + const { rows: [row] } = await lease.query("SELECT * FROM mcp_routing_profiles WHERE id=$1 AND enabled", [listed.id]) + if (!row) return + const profile = routeAiProfileSchema.parse(row.config), state: PersistentController = row.state + const next: PersistentController = { ...state, checkedAt: new Date().toISOString() } + const stop = async (reason: string) => { + next.pausedReason = reason; next.reason = reason + if (await publishControllerState(lease, row, next, true)) await audit(lease, "route-controller", "controller.pause", row.connection_id, { profileId: row.id, reason }) + } + const current = async () => { if (!await controllerProfileCurrent(lease, row)) throw new McpError("STATE_CONFLICT", "Допуск профиля изменился во время цикла", 409) } + try { + const controllerBlock = routeAiControllerBlockReason(profile) + if (controllerBlock) { await stop(controllerBlock); return } + if (row.grant_expires_at.getTime() <= Date.now() || row.generation !== await accessGeneration() || !row.credential_id) { await stop("Допуск контроллера истёк или отозван"); return } + const principal: McpPrincipal = await routingControllerBaselinePrincipal(state.baseline) + const connection = await validatePrincipal(principal) + if (connection.policy.version !== row.policy_version) { await stop("Политика изменилась; требуется новый допуск"); return } + if (state.pausedReason) { await stop(state.pausedReason); return } + if (state.planId) { + const { rows: [plan] } = await pool.query(`SELECT p.state,p.expires_at,o.id AS operation_id,o.state AS operation_state, + CASE WHEN o.finished_at>=o.created_at AND o.finished_at<=clock_timestamp() THEN o.finished_at END AS operation_finished_at + FROM mcp_plans p LEFT JOIN mcp_operations o ON o.plan_id=p.id WHERE p.id=$1 AND p.connection_id=$2`, [state.planId, connection.id]) + if (!plan || (plan.operation_id + ? !["applied", "pending", "running"].includes(plan.operation_state) + : !["draft", "approved", "awaiting-approval"].includes(plan.state))) { + await stop("Изменение отклонено или требует разбора результата") + return + } + if (plan.operation_state === "applied") { + if (!(plan.operation_finished_at instanceof Date)) { await stop("Время окончания переключения не подтверждено"); return } + await assertIdentifiedRoutingCompletion(lease, row.id, state.planId, plan.operation_id, principal) + next.baseline=await advanceRoutingControllerBaseline(state.baseline,row.id,row.version,profile,row.grant_expires_at.toISOString(),{principal,connection},state.planId,plan.operation_id) + next.lastSwitchAt = plan.operation_finished_at.toISOString() + next.switches = [...(state.switches ?? [])]; next.consecutive = 0 + next.expectedCandidateId = next.baseline.packet.candidateId + delete next.planId; delete next.candidateId; delete next.pendingCandidateId; delete next.operationId; delete next.confirmationWindow + delete next.observationKey + delete next.failureCount + delete next.failureKey + delete next.failureObservedAt + delete next.failingCandidateId + delete next.failoverReserveCursor + next.reason = "Изменение конфигурации проверено; ожидаются новые измерения сети" + } else if (plan.operation_id) next.reason = "Операция выполняется" + else if (plan.expires_at.getTime() <= Date.now()) { await stop("План истёк без подтверждения"); return } + 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}) + const operation = await applyPlan(principal, state.planId, `controller:${row.id}:${state.planId}`) + next.operationId = operation.id; next.reason = "Применение через общий исполнитель" + } else next.reason = "Ожидается подтверждение человеком" + } else { + await assertRoutingControllerBaseline(state.baseline,row.id,row.version,profile,row.grant_expires_at.toISOString(),{principal,connection}) + if (state.probedAt && Date.now() - Date.parse(state.probedAt) < 55000) return + await current() + let confirmation: RoutingConfirmationObservations | undefined + const analysis = await invokeMcpTool(principal, "mm_route_optimizer_analyze", { profile, limit: 100 }, undefined, { + captureRoutingConfirmation: observations => { confirmation = observations }, + }) as { ranking: PublicRoutingRanking; currentCandidateId: string | null } + if (state.expectedCandidateId && analysis.currentCandidateId !== state.expectedCandidateId) { await stop("Наблюдаемый путь изменился вне ожидаемого переключения; требуется проверка вмешательства или дрейфа"); return } + if (analysis.currentCandidateId) next.expectedCandidateId = analysis.currentCandidateId + if (profile.candidates.some(candidate => candidate.controlProbe) && !connection.policy.scope.operationIds.includes("routing.path.verify")) { await stop("Не выдано право контрольной проверки пути"); return } + const probeCandidate = async (candidateId: string): Promise => { + if (!profile.candidates.find(candidate => candidate.id === candidateId)?.controlProbe) return + await current() + return await invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId }) as PathHealth + } + const currentHealth = analysis.currentCandidateId ? await probeCandidate(analysis.currentCandidateId) : undefined + const health = currentHealth ? [currentHealth] : [] + const failoverProbes = analysis.currentCandidateId && currentHealth?.status === "failed" + ? selectFailoverReserveProbes(analysis.ranking, analysis.currentCandidateId, state.failoverReserveCursor) : undefined + const candidates = failoverProbes?.candidateIds ?? analysis.ranking.filter(value => value.eligible).slice(0, 2) + .map(value => value.candidate.id).filter(id => id !== analysis.currentCandidateId) + for (const candidateId of candidates) { + const observed = await probeCandidate(candidateId) + if (observed) health.push(observed) + } + await assertRoutingControllerBaseline(state.baseline,row.id,row.version,profile,row.grant_expires_at.toISOString(),{principal,connection}) + const charges = await createRoutingReservationStore().readHourlyCharges(row.id, principal) + await current() + const decisionAt = Date.now() + const hasFreshPassedProbe = (candidateId: string | null) => health.some(probe => { + if (probe.candidateId !== candidateId || probe.status !== "passed") return false + const age = decisionAt - Date.parse(probe.observedAt) + return age >= 0 && age < COMMUNITY_PATH_MAX_AGE_MS + }) + const decision = decideRoutingSwitch(profile, state, analysis.ranking, analysis.currentCandidateId, decisionAt, health, charges.totalCharges, confirmation) + Object.assign(next, decision.state, { reason: decision.reason }) + if (!failoverProbes?.cursor) delete next.failoverReserveCursor + else if (!hasFreshPassedProbe(failoverProbes.cursor)) { + // Advance past failed/unknown reserves. Keep rechecking a passing + // reserve while the current path accumulates independent failures. + next.failoverReserveCursor = failoverProbes.cursor + } + next.probedAt = new Date().toISOString() + if (decision.state.pausedReason) { await stop(decision.state.pausedReason); return } + if (decision.switchTo && usesScopedRouteAiContext(profile) && !hasFreshPassedProbe(decision.switchTo)) { + next.reason = "Ожидается успешная контрольная проба выбранного пути" + } else if (decision.switchTo) { + await current() + await assertRoutingControllerBaseline(state.baseline,row.id,row.version,profile,row.grant_expires_at.toISOString(),{principal,connection}) + const prepared = await invokeMcpTool(principal, "mm_route_optimizer_plan", { profile, candidateId: decision.switchTo }, undefined, { persistentRoutingProfile: { id: row.id, version: row.version } }) as { planId: string } + next.planId = prepared.planId; next.candidateId = decision.switchTo; next.reason = "Подготовлен план переключения" + await audit(pool, "route-controller", "controller.plan", connection.id, { profileId: row.id, planId: prepared.planId, candidateId: decision.switchTo }) + } + } + await publishControllerState(lease, row, next) + } catch (error) { + // An exhausted read or call quota clears independently of the profile. + if (error instanceof McpError && error.code === "RATE_LIMITED") return + await stop(error instanceof McpError ? error.code : "Проверка состояния недоступна") + } + }) + } +} diff --git a/backend/src/modules/mcp/routing-recovery-authority.sql-helper.ts b/backend/src/modules/mcp/routing-recovery-authority.sql-helper.ts new file mode 100644 index 00000000..9621b123 --- /dev/null +++ b/backend/src/modules/mcp/routing-recovery-authority.sql-helper.ts @@ -0,0 +1,93 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer, request } from "node:http" +import { basename, dirname, resolve } from "node:path" + +export function assertOwnedRoutingRecoveryAuthoritySql() { + assert.equal(process.env.MMAPP_OWN_ROUTING_RECOVERY_AUTHORITY, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +} + +/** Focused acceptance of the exact captured-authority fence used by production + * recovery sessions. Not a repeat of the whole inverse lifecycle. The guarded + * callback would PATCH a synthetic loopback listener if admission regressed. */ +export async function acceptRoutingRecoveryAuthoritySql() { + assertOwnedRoutingRecoveryAuthoritySql() + const { pool } = await import("../../db/index.js") + const { mcpConnectionPolicySchema } = await import("@mmapp/contracts/mcp") + const { routeAiProfileSchema } = await import("@mmapp/contracts/mcp-routing") + const { accessGeneration, McpError } = await import("./security.js") + const { readRoutingEndpoints, createRoutingSourceContext } = await import("./routing-observation-source.js") + const { resolveRoutingProfileAuthority } = await import("./routing-profile-authority.js") + const { captureScopedRecoveryAuthority, scopedRecoveryDeadlineCeiling } = await import("./routing-community-scoped-recovery.js") + const { withVerifiedConfigurationChange, executeGuardedConfigurationWrite } = await import("../../services/config-mutation-policy.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + const previousEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean + const profileIds: string[] = [], ids: number[] = [] + let nativeHttpRequests = 0, callbackDispatches = 0, dispatchFences = 0 + const listener = createServer((req, res) => { nativeHttpRequests++; req.resume(); res.setHeader("content-type", "application/json"); res.end("{}") }) + try { + await new Promise((resolve, reject) => { listener.once("error", reject); listener.listen(0, "127.0.0.1", () => { listener.off("error", reject); resolve() }) }) + const port = (listener.address() as { port: number }).port + const marker = "captured-recovery-" + randomUUID() + ids.push(...(await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES($1,'127.0.0.1',$2,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id", [marker, port])).rows.map(row => Number(row.id))) + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + const profile = routeAiProfileSchema.parse({ name: marker, serverId: ids[0], community: "65000:10", family: "ipv4", table: "tenant", probeVrf: "probe", + candidates: [{ id: "original", jumpHostId: ids[0], exitNodeId: ids[0], wanInterface: "synthetic-wan", downstreamInterface: "synthetic-hop", gateway: "192.0.2.1", + gatewayRef: { resource: "ipv4_route", id: "*1" }, controlProbe: { targetInterface: "synthetic-exit", expectedHops: [{ serverId: ids[0], interface: "synthetic-exit" }] } }] }) + const transport = { contextSources: (await createRoutingSourceContext(await readRoutingEndpoints(ids))).expected } + for (const change of ["edit", "disable", "grant"] as const) { + const now = Date.now(), expiresAt = new Date(now + 600000).toISOString(), grantExpiry = new Date(now + 60000).toISOString() + const connectionId = randomUUID(), credentialId = randomUUID(), profileId = randomUUID(), ownerId = marker + "-owner-" + change + const permissions = ["mm:network:read", "mm:network:write", "mm:filters:write"] + const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt, + scope: { serverIds: ids, operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "operations.recover"], allowGlobal: false }, + limits: { callsPerMinute: 100, mutationsPerHour: 100, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } }) + const generation = await accessGeneration() + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'synthetic captured recovery',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), generation, expiresAt, new Date(now - 1000)]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), expiresAt]) + await pool.query("INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version) VALUES($1,$2,$3::jsonb,true,$4,$5,$6,1)", [profileId, connectionId, JSON.stringify(profile), grantExpiry, credentialId, generation]) + profileIds.push(profileId) + const principal = { connectionId, credentialId, credentialExpiresAt: expiresAt } + const binding = await resolveRoutingProfileAuthority({ profileId, expectedVersion: 1, profile, principal, ownerId, transport }) + const input = { serverId: ids[0], profile: structuredClone(profile), principal: { ...principal }, ownerId, contextSources: structuredClone(transport.contextSources), persistentProfileAuthority: structuredClone(binding) } + const captured = captureScopedRecoveryAuthority(input) + // Alter every mutable outer authority component after session capture. + // The production closure must still validate its original signed input. + input.principal.credentialId = randomUUID(); input.profile.name = "caller mutated" + input.persistentProfileAuthority.packet.profileVersion = 999; input.contextSources[0].proof = "f".repeat(64) + await captured.assert() + assert.equal(captured.validUntil, binding.packet.validUntil) + assert.equal(scopedRecoveryDeadlineCeiling(now + 120000, captured.validUntil), Date.parse(grantExpiry)) + const path = "/routing/filter/rule/*1" + await assert.rejects(withVerifiedConfigurationChange({ actorId: ownerId, connectionId, approved: true, serverIds: ids, serverWide: true, objects: [], + authorize: async () => { await captured.assert() }, beforeDispatch: async () => { + dispatchFences++ + }, + }, () => executeGuardedConfigurationWrite(ids[0], "PATCH", path, async () => { + callbackDispatches++ + await new Promise((resolve, reject) => { + const req = request({ hostname: "127.0.0.1", port, path: "/rest" + path, method: "PATCH", timeout: 1000 }, res => { res.resume(); res.once("end", resolve) }) + req.once("error", reject); req.once("timeout", () => req.destroy(new Error("Synthetic loopback timeout"))); req.end("{}") + }) + })), (error: unknown) => error instanceof McpError && error.code === "STATE_CONFLICT" && /Активный routing profile/.test(error.message)) + assert.equal(callbackDispatches, 0); assert.equal(nativeHttpRequests, 0); assert.equal(dispatchFences, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action IN ('configuration.intent','configuration.unconfirmed','configuration.acknowledged')", [connectionId])).rows[0].n, 0) + + if (change === "edit") await pool.query("UPDATE mcp_routing_profiles SET version=version+1,config=jsonb_set(config,'{name}','\"late edit\"') WHERE id=$1", [profileId]) + if (change === "disable") await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", [profileId]) + if (change === "grant") await pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=clock_timestamp()-interval '1 second' WHERE id=$1", [profileId]) + await assert.rejects(captured.assert(), /Постоянный допуск/) + } + return { cases: 3, deepCaptureCases: 3, directGuardBeforeIntent: true, dispatchFences, callbackDispatches, nativeHttpRequests, + capturedAuthorityChanges: ["edit", "disable", "grant"], realSql: true, productionHmac: true, controllerEnabled: false, externalNetwork: false } + } finally { + if (profileIds.length) await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=ANY($1::uuid[])", [profileIds]) + if (ids.length) await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [ids]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousEnabled]) + listener.closeAllConnections(); await new Promise(resolve => listener.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/routing-recovery-authority.sql.test.ts b/backend/src/modules/mcp/routing-recovery-authority.sql.test.ts new file mode 100644 index 00000000..2b97341b --- /dev/null +++ b/backend/src/modules/mcp/routing-recovery-authority.sql.test.ts @@ -0,0 +1,27 @@ +import { assertOwnedRoutingRecoveryAuthoritySql, acceptRoutingRecoveryAuthoritySql } from "./routing-recovery-authority.sql-helper.js" + +// Guard precedes imports that configure a pool or load private MCP state. +assertOwnedRoutingRecoveryAuthoritySql() +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "owned-routing-recovery-authority-acceptance-secret" +process.env.AUTH_ISSUER = "https://owned-routing-recovery-authority.test" +const { pool, closePool } = await import("../../db/index.js") +try { + const { applySqlMigrations } = await import("../../db/migrate.js") + await applySqlMigrations(pool) + console.log("captured recovery authority SQL acceptance:", JSON.stringify(await acceptRoutingRecoveryAuthoritySql())) + const { updateMcpSettings } = await import("./store.js") + await import("./actions.js") + const { acceptScopedRouteCommunitySwitch } = await import("./routing-community-scoped-acceptance.js") + const actor = { id: "owned-routing-recovery-authority", email: "routing-recovery@example.invalid", name: "Owned recovery authority", + apps: ["mm"], permissions: ["mm:settings:admin", "mm:mcp:admin", "mm:mcp:approve", "mm:network:read", "mm:network:write", "mm:filters:write", "mm:servers:admin"], + issuedAt: Date.now() - 1000, expiresAt: Date.now() + 900000 } + await updateMcpSettings({ enabled: true }, actor) + for (const lateInverseAuthorityChange of ["path-age", "edit", "disable", "grant", "path-proof"] as const) { + const evidence = await acceptScopedRouteCommunitySwitch(actor, { lateInverseAuthorityChange }) + console.log("genuine late inverse authority SQL acceptance:", JSON.stringify(evidence)) + } +} finally { + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/routing-recovery-authority.test.ts b/backend/src/modules/mcp/routing-recovery-authority.test.ts new file mode 100644 index 00000000..ce0116bf --- /dev/null +++ b/backend/src/modules/mcp/routing-recovery-authority.test.ts @@ -0,0 +1,20 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { captureScopedRecoveryAuthority, scopedRecoveryDeadlineCeiling } from "./routing-community-scoped-recovery.js" + +test("recovery authority deadline is finite and never extends either ceiling", () => { + const now = Date.parse("2026-10-25T00:00:00Z") + assert.equal(scopedRecoveryDeadlineCeiling(now + 120000), now + 120000) + assert.equal(scopedRecoveryDeadlineCeiling(now + 120000, new Date(now + 30000).toISOString()), now + 30000) + assert.equal(scopedRecoveryDeadlineCeiling(now + 120000, new Date(now + 600000).toISOString()), now + 120000) + for (const bad of [NaN, Infinity, -Infinity]) assert.throws(() => scopedRecoveryDeadlineCeiling(bad)) + for (const bad of ["invalid", "", "Infinity"]) assert.throws(() => scopedRecoveryDeadlineCeiling(now + 120000, bad)) +}) + +test("manual sessions without a persistent binding preserve their existing authority path", async () => { + const manual = captureScopedRecoveryAuthority({ principal: { credentialId: "manual" }, profile: { name: "manual" } }) + assert.equal(manual.validUntil, undefined) + assert.equal(await manual.assert(), undefined) + assert.throws(() => captureScopedRecoveryAuthority({ persistentProfileAuthority: null })) +}) + diff --git a/backend/src/modules/mcp/routing-reservation-contract.ts b/backend/src/modules/mcp/routing-reservation-contract.ts new file mode 100644 index 00000000..49e86b50 --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation-contract.ts @@ -0,0 +1,39 @@ +export type RoutingReservationLane = "forward" | "inverse" +export type RoutingReservationLaneState = "held" | "spent" | "proven-unused" | "unknown" +export interface RoutingReservationState { + revision: number + forward: RoutingReservationLaneState + inverse: RoutingReservationLaneState + outcome?: { status: "passed" | "failed"; configurationProof: string; networkProof: string } +} +/** Until controller admission adopts this ledger, count both histories without + * timestamp deduplication. Conservative overlap remains charged. */ +export function countLegacyRoutingSwitches(state: unknown, now: number): number { + if (!Number.isFinite(now) || !state || typeof state !== "object" || Array.isArray(state)) throw Error("Invalid legacy profile switch history") + const switches = (state as Record).switches + if (switches === undefined) return 0 + if (!Array.isArray(switches) || switches.length > 2048) throw Error("Invalid legacy profile switch history") + let count = 0 + for (const value of switches) { + if (typeof value !== "string" || value.length !== 24) throw Error("Invalid legacy profile switch history") + const at = Date.parse(value) + if (!Number.isFinite(at) || new Date(at).toISOString() !== value || at > now) throw Error("Invalid legacy profile switch history") + if (at > now - 3600000) count++ + } + return count +} +export function routingReservationDeadline(sourceCreatedAt: number, planExpiresAt: number, authorityExpiresAt: number, channelExpiresAt = Infinity) { + if (![sourceCreatedAt, planExpiresAt, authorityExpiresAt].every(Number.isFinite) || !(Number.isFinite(channelExpiresAt) || channelExpiresAt === Infinity)) throw Error("Invalid reservation deadline") + const result = Math.min(sourceCreatedAt + 120000, planExpiresAt, authorityExpiresAt, channelExpiresAt) + if (!Number.isFinite(result)) throw Error("Invalid reservation deadline") + return result +} +/** Every allocated slot remains charged in this version, including a proved + * unused inverse. There is deliberately no refund/delete API. */ +export function transitionRoutingReservation(state: RoutingReservationState, lane: RoutingReservationLane, event: "attempt" | "unknown" | "prove-unused"): RoutingReservationState { + const current = state[lane] + if (event === "attempt" && (current !== "held" || lane === "inverse" && (state.forward !== "spent" || state.outcome?.status !== "failed"))) throw Error("Reservation attempt unavailable; replay forbidden") + if (event === "unknown" && current !== "spent") throw Error("Only a spent attempt can become unknown") + if (event === "prove-unused" && (lane !== "inverse" || current !== "held" || state.forward !== "spent" || state.outcome?.status !== "passed")) throw Error("No server-proven unused inverse") + return { ...state, revision: state.revision + 1, [lane]: event === "attempt" ? "spent" : event === "unknown" ? "unknown" : "proven-unused" } +} diff --git a/backend/src/modules/mcp/routing-reservation-runtime.sql.test.ts b/backend/src/modules/mcp/routing-reservation-runtime.sql.test.ts new file mode 100644 index 00000000..7192ac45 --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation-runtime.sql.test.ts @@ -0,0 +1,28 @@ +import { assertOwnedRoutingReservationSql, acceptRoutingReservationSql } from "./routing-reservation.sql-helper.js" + +assertOwnedRoutingReservationSql() +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "owned-routing-mixed-writer-acceptance-secret" +process.env.AUTH_ISSUER = "https://owned-routing-mixed-writer.test" +const { pool, closePool } = await import("../../db/index.js") +const chargeErasureOnly = process.env.MMAPP_OWN_ROUTING_CHARGE_ERASURE_ONLY === "true" +try { + const { applySqlMigrations } = await import("../../db/migrate.js") + await applySqlMigrations(pool) + if (!chargeErasureOnly) console.log("private routing reservation runtime admission SQL acceptance:", JSON.stringify(await acceptRoutingReservationSql({ runtimeAdmission: true }))) + const actor = { id: "owned-routing-mixed-writer", email: "routing-mixed@example.invalid", name: "Owned mixed writer", + apps: ["mm"], permissions: ["mm:settings:admin", "mm:mcp:admin", "mm:mcp:approve", "mm:network:read", "mm:network:write", "mm:filters:write", "mm:servers:admin"], + issuedAt: Date.now() - 1000, expiresAt: Date.now() + 900000 } + await (await import("./store.js")).updateMcpSettings({ enabled: true }, actor) + await import("./actions.js") + const { acceptScopedRouteCommunitySwitch } = await import("./routing-community-scoped-acceptance.js") + const inverse = await acceptScopedRouteCommunitySwitch(actor, { manualInverseChargeOnly: true }) + console.log("private routing v8 manual inverse charge and intent SQL acceptance:", JSON.stringify(inverse)) + if (!chargeErasureOnly) { + const mixed = await acceptScopedRouteCommunitySwitch(actor, { manualInverseChargeOnly: true, manualInverseChargeCase: "mixed-forward" }) + console.log("private routing mixed forward/manual inverse admission SQL acceptance:", JSON.stringify(mixed)) + } +} finally { + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/routing-reservation-runtime.ts b/backend/src/modules/mcp/routing-reservation-runtime.ts new file mode 100644 index 00000000..f23c4677 --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation-runtime.ts @@ -0,0 +1,86 @@ +import type { PoolClient } from "pg" +import { selectManualInverseAdmissionWithinOwner } from "./routing-manual-inverse-store.js" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import type { ChangeEvidence } from "./action-types.js" +import type { McpPrincipal, StoredPlan } from "./store.js" +import { decryptSecret, fingerprint, McpError } from "./security.js" +import { assertScopedCommunityRequest, readScopedPathCheckpoint } from "./routing-community-scoped-request.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { loadRoutingMainTransitionPreparation } from "./routing-main-transition.js" + +export function requiresScopedRoutingOwnerApproval(evidence: ChangeEvidence) { + const step = evidence.steps[0] + return evidence.steps.length === 1 && step.kind === "service" && + step.service === "routing.community.scoped" && step.input.proofRevision === 3 +} + +export async function readScopedRoutingPathAdmissionDeadline(db: PoolClient, evidence: ChangeEvidence, connectionId: string) { + const step = evidence.steps[0] + if (step.kind !== "service" || step.service !== "routing.community.scoped" || step.input.proofRevision !== 3 || + routeAiProfileSchema.parse(step.input.profile).autoRecovery) return + const { pathCheckId, pathCheckpointProof } = step.input + if (typeof pathCheckId !== "string" || typeof pathCheckpointProof !== "string") { + throw new McpError("MEASUREMENT_UNKNOWN", "Подтверждение пути изменилось", 409) + } + const checkpoint = await readScopedPathCheckpoint(db, pathCheckId, connectionId) + if (checkpoint.proof !== pathCheckpointProof) throw new McpError("MEASUREMENT_UNKNOWN", "Подтверждение пути изменилось", 409) + return checkpoint.validUntil +} + +/** Only authenticated encrypted evidence can select this admission lane. */ +export async function routingReservationEvidence(plan: Pick, evidence: ChangeEvidence) { + if (plan.operation_id !== "routing.community.switch" || evidence.steps.length !== 1) return undefined + const step = evidence.steps[0] + if (step.kind !== "service" || step.service !== "routing.community.scoped" || step.input.proofRevision !== 3) return undefined + await assertScopedCommunityRequest(step.input) + const profile = routeAiProfileSchema.parse(step.input.profile) + if (!profile.autoRecovery) return undefined + if (plan.operation_version !== 7 || evidence.recovery) throw new McpError("STATE_CONFLICT", "Incompatible reserved routing plan", 409) + const authority = routingProfileAuthorityBindingSchema.parse(step.input.persistentProfileAuthority) + await assertPersistentScopedProfileAuthority(step.input) + return { input: step.input, authority } +} + +/** Same profile-row owner for every existing persistent forward lane. This + * selects locks only; it creates no slot, debit or dispatch authority. */ +async function nonReservedPersistentAdmissionEvidence(plan: Pick, evidence: ChangeEvidence) { + if (plan.operation_id !== "routing.community.switch" || evidence.steps.length !== 1) return undefined + const step = evidence.steps[0] + if (step.kind !== "service" || step.input.persistentProfileAuthority === undefined) return undefined + if (step.service === "routing.community.scoped" && step.input.proofRevision === 3) { + await assertScopedCommunityRequest(step.input) + if (routeAiProfileSchema.parse(step.input.profile).autoRecovery) throw new McpError("STATE_CONFLICT", "Reserved profile must use its original admission", 409) + await assertPersistentScopedProfileAuthority(step.input) + } else if (step.service === "routing.community") await loadRoutingMainTransitionPreparation(step.input) + else throw new McpError("STATE_CONFLICT", "Persistent profile admission source is incompatible", 409) + if (plan.operation_version !== 7) throw new McpError("STATE_CONFLICT", "Persistent routing operation version changed", 409) + return { input: step.input, authority: routingProfileAuthorityBindingSchema.parse(step.input.persistentProfileAuthority) } +} + +/** Connection and global runner admission are already locked by applyPlan. + * Lock profile/credential BEFORE the plan and operation, on the same owner. */ +export async function lockRoutingReservationAdmissionSource(db: PoolClient, initial: StoredPlan, principal: McpPrincipal) { + const initialEvidence = JSON.parse(await decryptSecret(initial.evidence.sealed)) as ChangeEvidence + const reserved = await routingReservationEvidence(initial, initialEvidence) + const manual = await selectManualInverseAdmissionWithinOwner(db, initial, initialEvidence, principal) + const selected = reserved ?? await nonReservedPersistentAdmissionEvidence(initial, initialEvidence) ?? manual + if (selected) { + const packet = selected.authority.packet + if (packet.connectionId !== principal.connectionId || !manual && packet.credentialId !== principal.credentialId) throw new McpError("STATE_CONFLICT", "Original routing principal differs", 409) + await db.query("SELECT id FROM mcp_routing_profiles WHERE id=$1 FOR UPDATE", [packet.profileId]) + for (const credentialId of [...new Set([packet.credentialId, principal.credentialId])].sort()) { + await db.query("SELECT id FROM mcp_credentials WHERE id=$1 AND connection_id=$2 FOR SHARE", [credentialId, principal.connectionId]) + } + } + const rows = (await db.query("SELECT * FROM mcp_plans WHERE id=$1 FOR UPDATE", [initial.id])).rows + if (rows.length !== 1) throw new McpError("NOT_FOUND", "План не найден", 404) + const plan = rows[0] + if (fingerprint(plan.evidence) !== fingerprint(initial.evidence) || plan.operation_id !== initial.operation_id || plan.operation_version !== initial.operation_version || plan.connection_id !== initial.connection_id) throw new McpError("STATE_CONFLICT", "Plan changed before ordered admission locks", 409) + if (manual) { + await db.query("SELECT o.id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.connection_id=$2 FOR UPDATE OF o,p", [manual.source.row.id, principal.connectionId]) + const after = await selectManualInverseAdmissionWithinOwner(db, plan, initialEvidence, principal) + if (!after || fingerprint(after) !== fingerprint(manual)) throw new McpError("STATE_CONFLICT", "Manual inverse source changed before ordered admission", 409) + } + if (selected) await assertPersistentScopedProfileAuthority(selected.input, undefined, db) + return { plan, evidence: initialEvidence, reserved: !!reserved } +} diff --git a/backend/src/modules/mcp/routing-reservation-schema.ts b/backend/src/modules/mcp/routing-reservation-schema.ts new file mode 100644 index 00000000..2884568d --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation-schema.ts @@ -0,0 +1,33 @@ +/** Historical proposal retained for review compatibility only. Production and + * current owned acceptance use numbered 0040; never execute this after it. */ +export const ROUTING_RESERVATION_PROPOSAL_DDL = ` +CREATE TABLE mcp_routing_reservations ( + id uuid PRIMARY KEY, + source_plan_id uuid NOT NULL REFERENCES mcp_plans(id), + source_operation_id uuid NOT NULL REFERENCES mcp_operations(id), + source_step_index integer NOT NULL CHECK(source_step_index=0), + profile_id uuid NOT NULL REFERENCES mcp_routing_profiles(id), + connection_id uuid NOT NULL REFERENCES mcp_connections(id), + binding_sealed text NOT NULL,binding_proof text NOT NULL, + state_sealed text NOT NULL,state_proof text NOT NULL,revision integer NOT NULL CHECK(revision>=1), + deadline timestamptz NOT NULL,created_at timestamptz NOT NULL DEFAULT now(), + UNIQUE(source_operation_id,source_step_index) +); +CREATE TABLE mcp_routing_reservation_slots ( + reservation_id uuid NOT NULL REFERENCES mcp_routing_reservations(id), + profile_id uuid NOT NULL REFERENCES mcp_routing_profiles(id), + lane text NOT NULL CHECK(lane IN ('forward','inverse')), + state text NOT NULL CHECK(state IN ('held','spent','proven-unused','unknown')), + created_at timestamptz NOT NULL DEFAULT now(),PRIMARY KEY(reservation_id,lane) +); +CREATE INDEX mcp_routing_reservation_slots_window ON mcp_routing_reservation_slots(profile_id,created_at); +ALTER TABLE mcp_scoped_recovery_intents ADD COLUMN reservation_id uuid REFERENCES mcp_routing_reservations(id); +ALTER TABLE mcp_scoped_recovery_intents ADD COLUMN slot_phase text NOT NULL DEFAULT 'recovery-intent'; +ALTER TABLE mcp_scoped_recovery_intents ALTER COLUMN source_configuration_proof DROP NOT NULL; +ALTER TABLE mcp_scoped_recovery_intents ADD CONSTRAINT scoped_reservation_slot_shape CHECK ( + (mode='manual' AND reservation_id IS NULL AND slot_phase='recovery-intent' AND source_configuration_proof IS NOT NULL) + OR (mode='reserved' AND reservation_id IS NOT NULL AND + ((slot_phase='reservation-held' AND source_configuration_proof IS NULL) + OR (slot_phase IN ('reservation-bound','reserved-attempt') AND source_configuration_proof IS NOT NULL))) +); +` diff --git a/backend/src/modules/mcp/routing-reservation-store.ts b/backend/src/modules/mcp/routing-reservation-store.ts new file mode 100644 index 00000000..5cdf522b --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation-store.ts @@ -0,0 +1,566 @@ +import { assertLegacyManualHistoryWithinOwner } from "./routing-manual-inverse-history.js" +import { readManualInverseChargesWithinOwner } from "./routing-manual-inverse-store.js" +import { projectRoutingSwitchLedgerModel } from "./routing-switch-ledger-contract.js" +import { assertRoutingChargeAudit, recordRoutingChargeAnchor } from "./routing-charge-audit.js" +import { createHash, randomUUID } from "node:crypto" +import type { VerifiedMcpServiceMetadata } from "../../services/config-mutation-policy.js" +import type { PoolClient } from "pg" +import { z } from "zod" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { hasPermission } from "../../lib/permissions.js" +import { assertPersistentScopedProfileAuthority, routingProfileAuthorityBindingSchema } from "./routing-profile-authority.js" +import { assertScopedCommunityRequest } from "./routing-community-scoped-request.js" +import { loadScopedConfigurationPacket, loadScopedNetworkCheckpoint } from "./routing-community-scoped-checkpoint.js" +import { loadScopedRecoverySource } from "./routing-community-scoped-recovery-source.js" +import { decryptSecret, decryptSecretExisting, encryptSecret, fingerprint, McpError, privateSecretFingerprint, privateFingerprinterExisting, redact } from "./security.js" +import { effectiveMcpConnection, type McpPrincipal } from "./store.js" +import { routingReservationDeadline, transitionRoutingReservation, type RoutingReservationLane } from "./routing-reservation-contract.js" +import { routingReservationTransaction, type RoutingReservationConnect } from "./routing-reservation-transaction.js" +import { lockRoutingReservationAdmissionSource } from "./routing-reservation-runtime.js" +import type { StoredPlan } from "./store.js" + +const uuid = z.string().uuid(), proof = z.string().regex(/^[a-f0-9]{64}$/), stamp = z.string().datetime() +const principalSchema = z.object({ connectionId: uuid, credentialId: uuid, credentialExpiresAt: stamp }).strict() +const forwardChargeSchema = z.object({ revision: z.literal(1), operationId: uuid, planId: uuid, stepIndex: z.literal(0), + profileId: uuid, profileVersion: z.number().int().positive(), connectionId: uuid, ownerId: z.string().min(1), + principal: principalSchema, service: z.enum(["routing.community", "routing.community.scoped"]), operationVersion: z.literal(7), + authorityProof: proof, evidenceProof: proof, payloadProof: proof, scope: z.array(z.number().int().positive().safe()).min(1).max(1000), + createdAt: stamp, chargeUnits: z.literal(1) }).strict() +type ForwardCharge = z.infer +const forwardChargeMac = (value: ForwardCharge) => privateSecretFingerprint(fingerprint(value), "routing-forward-charge-v1") +const bindingSchema = z.object({ revision: z.literal(1), id: uuid, planId: uuid, operationId: uuid, stepIndex: z.literal(0), + sourceReceiptId: uuid, inverseReceiptId: uuid, profileId: uuid, profileVersion: z.number().int().positive(), + principal: principalSchema, ownerId: z.string().min(1), sourceInputSeal: proof, sourceEvidenceProof: proof, authorityProof: proof, + scope: z.array(z.number().int().positive().safe()).min(1).max(1000), createdAt: stamp, deadline: stamp, budgetBucket: stamp, + mutationUnits: z.literal(2), profileSlots: z.literal(2), maxProfileSlots: z.number().int().positive().max(10) }).strict() +const laneState = z.enum(["held", "spent", "proven-unused", "unknown"]) +const stateSchema = z.object({ revision: z.number().int().positive(), forward: laneState, inverse: laneState, + outcome: z.object({ status: z.enum(["passed", "failed"]), configurationProof: proof, networkProof: proof }).strict().optional() }).strict() +type Binding = z.infer +type State = z.infer +type RecordValue = { binding: Binding; state: State; bindingProof: string; stateProof: string; sharedQuarantined?: boolean } +export interface RoutingReservedInverseAllocation { readonly kind: "routing-reserved-inverse-allocation" } +export interface RoutingReservedForwardAllocation { readonly kind: "routing-reserved-forward-allocation" } +export interface RoutingReservedInverseExecution { readonly kind: "routing-reserved-inverse-execution" } +const forwardAllocations = new WeakMap() +const forwardAttempts = new WeakMap() +const inverseExecutions = new WeakMap() +const inverseAllocations = new WeakMap() +const sign = (value: unknown, domain: string) => privateSecretFingerprint(fingerprint(value), "routing-reservation-" + domain + "-v1") +const fail = (message: string) => new McpError("STATE_CONFLICT", message, 409) +const iso = (value: unknown) => { if (!(value instanceof Date) || !Number.isFinite(value.getTime())) throw fail("Invalid reservation SQL timestamp"); return value.toISOString() } + +type ChargeSqlRow = { source_operation_id: string; source_plan_id: string; source_step_index: number; profile_id: string; + connection_id: string; charge_units: number; binding_sealed: string | null; binding_proof: string; created_at: Date } +const forwardColumns = `source_operation_id,source_plan_id,source_step_index,profile_id,connection_id,charge_units, + CASE WHEN octet_length(binding_sealed)<=262144 THEN binding_sealed END AS binding_sealed,binding_proof,created_at` +async function authenticateForwardCharge(row: ChargeSqlRow, observedAt: string) { + if (!row.binding_sealed) throw fail("Forward charge byte bound exceeded") + const value = forwardChargeSchema.parse(JSON.parse(await decryptSecretExisting(row.binding_sealed))) + const mac = await privateFingerprinterExisting("routing-forward-charge-v1") + if (row.binding_proof !== mac(fingerprint(value)) || value.operationId !== row.source_operation_id || value.planId !== row.source_plan_id || + value.stepIndex !== row.source_step_index || value.profileId !== row.profile_id || value.connectionId !== row.connection_id || + row.charge_units !== 1 || value.createdAt !== iso(row.created_at) || Date.parse(value.createdAt) > Date.parse(observedAt) || + value.principal.connectionId !== value.connectionId) throw fail("Forward charge identity/time/MAC differs") + return value +} +async function readForwardChargesWithinOwner(db: PoolClient, profileId: string, current: Awaited>, observedAt: string) { + const rows = (await db.query(`SELECT ${forwardColumns} FROM mcp_routing_forward_charges + WHERE profile_id=$1 AND created_at>$2 ORDER BY created_at,source_operation_id LIMIT 12`, [profileId, new Date(Date.parse(observedAt) - 3600000)])).rows + if (rows.length > 11) throw fail("Forward charge count bound exceeded") + const entries = [] + for (const row of rows) { + const value = await authenticateForwardCharge(row, observedAt) + if (value.profileId !== profileId || value.connectionId !== current.id || value.ownerId !== current.owner_id || + value.scope.some(id => !current.policy.scope.serverIds.includes(id))) throw fail("Forward charge outside current profile/read scope") + // Historical signature, not live old permission or an execution token. + entries.push({ profileId, sourceOperationId: value.operationId, sourceStepIndex: 0, allocationId: value.operationId, + sourceIdentity: row.binding_proof, lane: "forward" as const, state: "held" as const, createdAt: value.createdAt }) + } + return entries +} + +async function source(db: PoolClient, operationId: string, lock = false) { + uuid.parse(operationId) + const rows = (await db.query(`SELECT o.id,o.plan_id,o.connection_id,o.created_at,o.state AS operation_state, + p.operation_id AS action,p.operation_version,p.policy_version,p.generation,p.state AS plan_state,p.approved_by,p.expires_at, + CASE WHEN octet_length(p.evidence::text)<=1048576 THEN p.evidence END AS evidence + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 LIMIT 2${lock ? " FOR UPDATE OF o,p" : ""}`, [operationId])).rows + if (rows.length !== 1) throw fail("Exact reservation source unavailable") + const row = rows[0] + if (row.action !== "routing.community.switch" || row.operation_version !== 7 || !row.approved_by || !row.evidence?.sealed) throw fail("No approved compatible scoped source") + const evidence = JSON.parse(await decryptSecret(row.evidence.sealed)) + if (evidence.steps?.length !== 1 || evidence.steps[0].kind !== "service" || evidence.steps[0].service !== "routing.community.scoped" || evidence.target?.scope !== "servers") throw fail("Reservation requires one scoped source step") + const input = evidence.steps[0].input as Record, profile = routeAiProfileSchema.parse(input.profile) + const authority = routingProfileAuthorityBindingSchema.parse(input.persistentProfileAuthority), principal = principalSchema.parse(input.principal) + if (input.proofRevision !== 3 || !profile.autoRecovery || row.connection_id !== principal.connectionId || row.approved_by !== input.ownerId || + authority.packet.connectionId !== principal.connectionId || authority.packet.credentialId !== principal.credentialId || + fingerprint([...evidence.target.serverIds].sort((a: number, b: number) => a - b)) !== fingerprint(authority.packet.scope)) throw fail("Source does not carry the original reserved authority") + const inputSeal = await assertScopedCommunityRequest(input) + return { row, input, profile, authority, principal, inputSeal, evidenceProof: await sign({ evidence, planId: row.plan_id, operationId, action: row.action, + operationVersion: row.operation_version, policyVersion: row.policy_version, generation: row.generation, approvedBy: row.approved_by, expiresAt: iso(row.expires_at) }, "source-evidence") } +} + +async function load(db: PoolClient, id: string, lock = false, historical = false): Promise { + const decrypt = historical ? decryptSecretExisting : decryptSecret + const mac = async (value: unknown, domain: string) => historical + ? (await privateFingerprinterExisting("routing-reservation-" + domain + "-v1"))(fingerprint(value)) : sign(value, domain) + uuid.parse(id) + const rows = (await db.query(`SELECT id,source_plan_id,source_operation_id,source_step_index,profile_id,connection_id,revision,deadline,created_at, + CASE WHEN octet_length(binding_sealed)<=1048576 THEN binding_sealed END AS binding_sealed,binding_proof, + CASE WHEN octet_length(state_sealed)<=131072 THEN state_sealed END AS state_sealed,state_proof + FROM mcp_routing_reservations WHERE id=$1 LIMIT 2${lock ? " FOR UPDATE" : ""}`, [id])).rows + if (rows.length !== 1) throw fail("Reservation not found") + const row = rows[0] + if (!row.binding_sealed || !row.state_sealed) throw fail("Reservation size bound exceeded") + const binding = bindingSchema.parse(JSON.parse(await decrypt(row.binding_sealed))), state = stateSchema.parse(JSON.parse(await decrypt(row.state_sealed))) + if (row.binding_proof !== await mac(binding, "binding") || row.state_proof !== await mac({ bindingProof: row.binding_proof, state }, "state") || + binding.id !== row.id || binding.planId !== row.source_plan_id || binding.operationId !== row.source_operation_id || binding.stepIndex !== row.source_step_index || + binding.profileId !== row.profile_id || binding.principal.connectionId !== row.connection_id || state.revision !== row.revision || + binding.deadline !== iso(row.deadline) || binding.createdAt !== iso(row.created_at)) throw fail("Reservation tampering detected") + const slots = (await db.query("SELECT lane,state,profile_id,created_at FROM mcp_routing_reservation_slots WHERE reservation_id=$1 ORDER BY lane LIMIT 3", [id])).rows + if (slots.length !== 2 || slots[0].lane !== "forward" || slots[1].lane !== "inverse" || slots.some(slot => slot.profile_id !== binding.profileId || slot.state !== state[slot.lane as RoutingReservationLane] || iso(slot.created_at) !== binding.createdAt)) throw fail("Profile allocation ledger differs from signed reservation") + const shared = (await db.query("SELECT * FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1 AND source_step_index=$2 AND octet_length(row_to_json(mcp_scoped_recovery_intents)::text)<=1048576 LIMIT 2", [binding.operationId, binding.stepIndex])).rows + if (shared.length !== 1 || shared[0].reservation_id !== id || shared[0].mode !== "reserved" || shared[0].source_receipt_id !== binding.sourceReceiptId || + shared[0].recovery_receipt_id !== binding.inverseReceiptId || shared[0].authority_profile_id !== binding.profileId || shared[0].authority_profile_version !== binding.profileVersion || iso(shared[0].deadline) !== binding.deadline) throw fail("Shared manual/reserved inverse slot differs") + const sharedPacket = JSON.parse(await decrypt(shared[0].intent_sealed)) + const expectedSharedState = state.forward === "unknown" || state.inverse === "unknown" ? "unknown" : "intent" + const sharedQuarantined = expectedSharedState === "intent" && shared[0].state === "unknown" + let sharedVerified = false + if (shared[0].state === "verified" && state.inverse === "spent" && state.outcome?.status === "failed") { + const { validateReservedInverseVerified } = await import("./routing-community-scoped-recovery.js") + sharedVerified = await validateReservedInverseVerified(shared[0], { reservationId: binding.id, sourceOperationId: binding.operationId, + sourceReceiptId: binding.sourceReceiptId, inverseReceiptId: binding.inverseReceiptId, deadline: binding.deadline, + configurationProof: state.outcome.configurationProof, networkProof: state.outcome.networkProof }, historical) + } + if (shared[0].intent_proof !== await mac(sharedPacket, "shared-slot") || sharedPacket.reservationId !== id || sharedPacket.bindingProof !== row.binding_proof || + sharedPacket.phase !== shared[0].slot_phase || sharedPacket.stateProof !== row.state_proof || + shared[0].state !== expectedSharedState && !sharedQuarantined && !sharedVerified || sharedPacket.phase !== (state.inverse === "spent" || state.inverse === "unknown" ? "reserved-attempt" : state.outcome ? "reservation-bound" : "reservation-held") || + (state.outcome?.configurationProof ?? null) !== shared[0].source_configuration_proof) throw fail("Shared inverse allocation proof differs") + return { binding, state, bindingProof: row.binding_proof, stateProof: row.state_proof, sharedQuarantined } +} + +async function live(db: PoolClient, record: RecordValue) { + if (record.sharedQuarantined) throw fail("Shared inverse slot quarantined; read-only reconciliation only") + if (Date.parse(record.binding.deadline) <= Date.now()) throw fail("Original reservation deadline expired") + const value = await source(db, record.binding.operationId) + if (!["executing", "completed"].includes(value.row.plan_state)) throw fail("Original reservation plan is no longer executable") + if (value.row.plan_id !== record.binding.planId || value.inputSeal !== record.binding.sourceInputSeal || value.evidenceProof !== record.binding.sourceEvidenceProof || + value.authority.proof !== record.binding.authorityProof || value.authority.packet.profileId !== record.binding.profileId || value.authority.packet.profileVersion !== record.binding.profileVersion || + fingerprint(value.principal) !== fingerprint(record.binding.principal)) throw fail("Reservation original source changed") + await assertPersistentScopedProfileAuthority(value.input) + if (Date.parse(record.binding.deadline) <= Date.now()) throw fail("Original reservation deadline expired during validation") + return value +} + +async function assertRunningReservedIntent(db: PoolClient, value: Awaited>) { + const { rows } = await db.query(`SELECT s.intended_state FROM mcp_operations o + JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + WHERE o.id=$1 AND p.id=$2 AND o.connection_id=$3 AND o.state='running' AND p.state='executing' + AND s.state='intent' AND s.result IS NULL`, [value.row.id, value.row.plan_id, value.row.connection_id]) + if (rows.length !== 1 || fingerprint(rows[0].intended_state) !== fingerprint(redact({ kind: "service", service: "routing.community.scoped", input: value.input }))) { + throw fail("No exact running reserved intent") + } +} + +async function saveState(db: PoolClient, record: RecordValue, state: State, quarantineOnly = false) { + const stateProof = await sign({ bindingProof: record.bindingProof, state }, "state") + const phase = state.inverse === "spent" || state.inverse === "unknown" ? "reserved-attempt" : state.outcome ? "reservation-bound" : "reservation-held" + const sharedPacket = { phase, reservationId: record.binding.id, bindingProof: record.bindingProof, stateProof } + const result = await db.query("UPDATE mcp_routing_reservations SET state_sealed=$2,state_proof=$3,revision=$4 WHERE id=$1 AND revision=$5 RETURNING id", [record.binding.id, await encryptSecret(JSON.stringify(state)), stateProof, state.revision, record.state.revision]) + if (result.rowCount !== 1) throw fail("Reservation state changed") + for (const lane of ["forward", "inverse"] as const) await db.query("UPDATE mcp_routing_reservation_slots SET state=$3 WHERE reservation_id=$1 AND lane=$2", [record.binding.id, lane, state[lane]]) + const shared = await db.query("UPDATE mcp_scoped_recovery_intents SET slot_phase=$3,source_configuration_proof=$4,intent_sealed=$5,intent_proof=$6,state=$7 WHERE source_operation_id=$1 AND source_step_index=$2 AND reservation_id=$8 RETURNING source_operation_id", [record.binding.operationId, record.binding.stepIndex, phase, state.outcome?.configurationProof ?? null, await encryptSecret(JSON.stringify(sharedPacket)), await sign(sharedPacket, "shared-slot"), state.forward === "unknown" || state.inverse === "unknown" ? "unknown" : "intent", record.binding.id]) + if (shared.rowCount !== 1) throw fail("Reserved source slot was lost") + if (!quarantineOnly) await live(db, { ...record, state, stateProof }) + return { ...record, state, stateProof } +} +const receipt = (value: RecordValue, created = false) => ({ reservationId: value.binding.id, sourceOperationId: value.binding.operationId, sourceStepIndex: value.binding.stepIndex, + profileId: value.binding.profileId, profileVersion: value.binding.profileVersion, deadline: value.binding.deadline, state: value.state, created, + chargedMutationUnits: 2, chargedProfileSlots: 2, refundsAvailable: false, sharedSlotQuarantined: !!value.sharedQuarantined, nativeDispatchAvailable: false }) + +/** Reuse only within the original trusted SQL owner. Current execution + * authority/connection/profile locks stay with reserveWithinOwner. This is NOT + * the public historical READ lane and never opens another transaction/client. + * Historical rows remain charged without live() or original-grant filtering. + * Recent admissions are checked against the surviving signed audit. */ +export async function readHistoricalChargesWithinOwner(db: PoolClient, profileId: string, + current: Awaited>, observedAt?: string, deadlineAt?: number) { + const epoch = (await privateFingerprinterExisting("routing-owner-charge-epoch-v1"))("existing-material") + // Hold one admission lock before reading the SQL catalogue and the host + // witness. A racing read may fail closed, never accept a torn cross-store cut. + const cut = (await db.query("SELECT current_setting('transaction_isolation') AS isolation,pg_try_advisory_xact_lock(hashtextextended('mcp-runner-admission',0)) AS locked")).rows[0] + if (cut.isolation !== "repeatable read" && cut.isolation !== "read committed") throw fail("Unsupported hourly identity snapshot isolation") + if (cut.locked !== true) throw fail("Hourly manual identity cut is busy") + const profiles = (await db.query(`SELECT id,connection_id, + CASE WHEN octet_length(config::text)<=262144 THEN config END AS config, + CASE WHEN octet_length(state::text)<=131072 THEN state END AS state, + clock_timestamp() AS observed_at FROM mcp_routing_profiles WHERE id=$1 AND connection_id=$2 LIMIT 2`, [profileId, current.id])).rows + if (profiles.length !== 1 || !profiles[0].config || !profiles[0].state) throw fail("Bounded hourly profile unavailable") + const row = profiles[0], profile = routeAiProfileSchema.parse(row.config), now = Date.parse(observedAt ?? iso(row.observed_at)) + const callerDeadline = Math.min(deadlineAt ?? Infinity, current.owner_valid_until.getTime(), Date.parse(current.policy.expiresAt)) + const assertLive = () => { if (!Number.isFinite(callerDeadline) || Date.now() >= callerDeadline) throw fail("Original hourly admission/read deadline expired") } + await assertLegacyManualHistoryWithinOwner(db, profileId, new Date(now).toISOString(), assertLive) + assertLive() + const scope = [...new Set([profile.serverId, ...profile.candidates.flatMap(value => [value.jumpHostId, value.exitNodeId])])] + if (scope.some(id => !current.policy.scope.serverIds.includes(id))) throw fail("Hourly profile outside read scope") + // UNION includes surviving slot/shared references: a moved, missing or + // conflicting parent cannot silently disappear from this profile read. + // LIMIT sentinel refuses the WHOLE snapshot, never truncates its charge. + const ids = (await db.query(`SELECT id FROM ( + SELECT id FROM mcp_routing_reservations WHERE profile_id=$1 AND created_at>$2 + UNION SELECT reservation_id AS id FROM mcp_routing_reservation_slots WHERE profile_id=$1 AND created_at>$2 + UNION SELECT reservation_id AS id FROM mcp_scoped_recovery_intents WHERE authority_profile_id=$1 AND created_at>$2 + AND (mode='reserved' OR reservation_id IS NOT NULL)) AS wanted ORDER BY id LIMIT 12`, [profileId, new Date(now - 3600000)])).rows + if (ids.length > 11) throw fail("Hourly ledger count bound exceeded") + const entries = [] + for (const id of ids) { + const value = await load(db, String(id.id), false, true) + if (value.binding.profileId !== profileId || value.binding.principal.connectionId !== current.id || + value.binding.ownerId !== current.owner_id || value.binding.scope.some(serverId => !current.policy.scope.serverIds.includes(serverId))) throw fail("Hourly ledger source outside profile/read scope") + for (const lane of ["forward", "inverse"] as const) entries.push({ profileId, sourceOperationId: value.binding.operationId, + sourceStepIndex: value.binding.stepIndex, allocationId: value.binding.id, sourceIdentity: value.bindingProof, + lane, createdAt: value.binding.createdAt, state: value.state[lane] }) + } + const reservedCharges = entries.filter(value => Date.parse(value.createdAt) > now - 3600000).length + const forwardEntries = await readForwardChargesWithinOwner(db, profileId, current, new Date(now).toISOString()) + const manualInverseEntries = await readManualInverseChargesWithinOwner(db, profileId, current, new Date(now).toISOString()) + entries.push(...forwardEntries, ...manualInverseEntries) + await assertRoutingChargeAudit(db, profileId, current, now, entries) + assertLive() + const projection = projectRoutingSwitchLedgerModel({ profileId, now, maxSwitchesPerHour: profile.maxSwitchesPerHour, + complete: true, legacyState: row.state, entries }) + const result = Object.freeze({ observedAt: new Date(now).toISOString(), legacyCharges: projection.legacyCharges, + reservedCharges, forwardCharges: forwardEntries.length, manualInverseCharges: manualInverseEntries.length, totalCharges: projection.totalCharges, availableSlots: projection.availableSlots, + budgetFits: projection.budgetFits, securityProof: false as const, dispatchAllowed: false as const, + allWriterAdmissionAccepted: false as const, scope: "observed-owner-catalog" as const }) + + if ((await privateFingerprinterExisting("routing-owner-charge-epoch-v1"))("existing-material") !== epoch) throw fail("Historical charge key changed") + return result +} + +/** Private, unregistered store. This component owns its two-unit debit; future + * runtime admission must delegate instead of ALSO charging generic mutationCost. + * Allocation/capability is never sufficient authorization for native transport. */ +async function reserveWithinOwner(db: PoolClient, operationId: string) { + const before = await source(db, operationId) + // Fixed admission order: connection -> profile -> credential -> plan -> + // operation -> reservation -> shared inverse slot. No lock retry/fallback. + // Future applyPlan integration must use this SAME owner/transaction, + // rather than call this factory while another backend holds connection. + await db.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [before.principal.connectionId]) + await db.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-runner-admission',0))") + await db.query("SELECT id FROM mcp_routing_profiles WHERE id=$1 FOR UPDATE", [before.authority.packet.profileId]) + await db.query("SELECT id FROM mcp_credentials WHERE id=$1 AND connection_id=$2 FOR SHARE", [before.principal.credentialId, before.principal.connectionId]) + await db.query("SELECT id FROM mcp_plans WHERE id=$1 FOR UPDATE", [before.row.plan_id]) + await db.query("SELECT id FROM mcp_operations WHERE id=$1 FOR UPDATE", [operationId]) + const value = await source(db, operationId) + if (value.evidenceProof !== before.evidenceProof || value.authority.packet.profileId !== before.authority.packet.profileId || value.principal.connectionId !== before.principal.connectionId) throw fail("Source changed before ordered admission locks") + await assertPersistentScopedProfileAuthority(value.input) + const existing = (await db.query("SELECT id FROM mcp_routing_reservations WHERE source_operation_id=$1 AND source_step_index=0 LIMIT 2", [operationId])).rows + if (existing.length) { if (existing.length !== 1) throw fail("Ambiguous source reservation"); const known = await load(db, existing[0].id, true); await live(db, known); return { ...receipt(known), bindingProof: known.bindingProof } } + if (value.row.operation_state !== "pending" || value.row.plan_state !== "executing") throw fail("Reservation must precede forward execution") + const connection = await effectiveMcpConnection(value.principal, db) + const time = (await db.query("SELECT clock_timestamp() AS at,date_trunc('hour',now()) AS bucket")).rows[0] + const createdAt = iso(time.at), budgetBucket = iso(time.bucket) + const deadline = routingReservationDeadline(value.row.created_at.getTime(), value.row.expires_at.getTime(), Date.parse(value.authority.packet.validUntil)) + if (deadline <= Date.parse(createdAt)) throw fail("No original reservation lifetime remains") + // Existing connection/profile locks and authority remain the source of + // admission. MAC/slot/shared integrity is now checked before any debit. + // Legacy overlap stays charged, including allocations predating audit anchors. + const charges = await readHistoricalChargesWithinOwner(db, value.authority.packet.profileId, connection, createdAt, deadline) + if (charges.totalCharges + 2 > value.profile.maxSwitchesPerHour) throw fail("Insufficient durable profile switch slots") + const budget = await db.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) SELECT $1,'mutations',$2,2 WHERE $3::int>=2 ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+2 WHERE mcp_budget_buckets.used<=$3::int-2 RETURNING used", [connection.id, budgetBucket, connection.policy.limits.mutationsPerHour]) + if (budget.rowCount !== 1) throw fail("Insufficient connection mutation budget for forward and inverse") + const binding = bindingSchema.parse({ revision: 1, id: randomUUID(), planId: value.row.plan_id, operationId, stepIndex: 0, sourceReceiptId: value.input.receiptId, + inverseReceiptId: randomUUID(), profileId: value.authority.packet.profileId, profileVersion: value.authority.packet.profileVersion, principal: value.principal, + ownerId: value.input.ownerId, sourceInputSeal: value.inputSeal, sourceEvidenceProof: value.evidenceProof, authorityProof: value.authority.proof, scope: value.authority.packet.scope, + createdAt, deadline: new Date(deadline).toISOString(), budgetBucket, mutationUnits: 2, profileSlots: 2, maxProfileSlots: value.profile.maxSwitchesPerHour }) + const state: State = { revision: 1, forward: "held", inverse: "held" }, bindingProof = await sign(binding, "binding"), stateProof = await sign({ bindingProof, state }, "state") + await db.query("INSERT INTO mcp_routing_reservations(id,source_plan_id,source_operation_id,source_step_index,profile_id,connection_id,binding_sealed,binding_proof,state_sealed,state_proof,revision,deadline,created_at) VALUES($1,$2,$3,0,$4,$5,$6,$7,$8,$9,1,$10,$11)", [binding.id, binding.planId, operationId, binding.profileId, connection.id, await encryptSecret(JSON.stringify(binding)), bindingProof, await encryptSecret(JSON.stringify(state)), stateProof, binding.deadline, createdAt]) + for (const lane of ["forward", "inverse"]) await db.query("INSERT INTO mcp_routing_reservation_slots(reservation_id,profile_id,lane,state,created_at) VALUES($1,$2,$3,'held',$4)", [binding.id, binding.profileId, lane, createdAt]) + const sharedPacket = { phase: "reservation-held", reservationId: binding.id, bindingProof, stateProof } + const slot = await db.query("INSERT INTO mcp_scoped_recovery_intents(source_operation_id,source_step_index,source_receipt_id,source_configuration_proof,recovery_receipt_id,mode,authority_profile_id,authority_profile_version,intent_sealed,intent_proof,state,deadline,reservation_id,slot_phase) VALUES($1,0,$2,NULL,$3,'reserved',$4,$5,$6,$7,'intent',$8,$9,'reservation-held') ON CONFLICT DO NOTHING RETURNING source_operation_id", [operationId, binding.sourceReceiptId, binding.inverseReceiptId, binding.profileId, binding.profileVersion, await encryptSecret(JSON.stringify(sharedPacket)), await sign(sharedPacket, "shared-slot"), binding.deadline, binding.id]) + if (slot.rowCount !== 1) throw fail("Manual or reserved inverse already owns this source slot") + await recordRoutingChargeAnchor(db, { profileId: binding.profileId, connectionId: connection.id, ownerId: binding.ownerId, + sourceOperationId: operationId, allocationId: binding.id, kind: "reserved", bindingProof, createdAt }, deadline) + await assertPersistentScopedProfileAuthority(value.input) + if (deadline <= Date.now()) throw fail("Original reservation deadline expired during admission") + return { ...receipt({ binding, state, bindingProof, stateProof }, true), bindingProof } +} +/** Fixed same-owner admission, never a charge-ID capability or caller evidence. + * Generic mutation debit remains in applyPlan, in this SAME transaction. */ +export async function admitPersistentRoutingForwardWithinOwner(db: PoolClient, operationId: string) { + uuid.parse(operationId) + const operation = (await db.query("SELECT id,plan_id,connection_id,state FROM mcp_operations WHERE id=$1", [operationId])).rows[0] + if (!operation || operation.state !== "pending") throw fail("Forward charge requires actual pending admission") + const initial = (await db.query("SELECT * FROM mcp_plans WHERE id=$1", [operation.plan_id])).rows[0] + if (!initial) throw fail("Forward source plan unavailable") + if (initial.operation_id !== "routing.community.switch") return + const initialEvidence = JSON.parse(await decryptSecret(initial.evidence.sealed)) as import("./action-types.js").ChangeEvidence + const first = initialEvidence.steps[0] + if (first?.kind !== "service" || first.input.persistentProfileAuthority === undefined) return + const principal = principalSchema.parse(first.input.principal) + if (principal.connectionId !== operation.connection_id || initial.connection_id !== operation.connection_id) throw fail("Forward source connection differs") + const selected = await lockRoutingReservationAdmissionSource(db, initial, principal) + if (selected.reserved || selected.plan.state !== "executing") throw fail("Forward charge must not replace reserved admission") + const row = (await db.query("SELECT state FROM mcp_operations WHERE id=$1 AND plan_id=$2 AND connection_id=$3 FOR UPDATE", [operationId, initial.id, principal.connectionId])).rows[0] + if (row?.state !== "pending") throw fail("Forward admission changed before charge") + const step = selected.evidence.steps[0] + if (selected.evidence.steps.length !== 1 || step.kind !== "service" || !["routing.community", "routing.community.scoped"].includes(step.service)) throw fail("Forward source step differs") + const authority = routingProfileAuthorityBindingSchema.parse(step.input.persistentProfileAuthority) + const current = await effectiveMcpConnection(principal, db) + // Define the protocol's millisecond precision IN SQL before observation. + // No caller Date or client-side window/time adjustment can create a charge. + const observedAt = iso((await db.query("SELECT date_trunc('milliseconds',clock_timestamp()) AS at")).rows[0].at) + const existing = (await db.query(`SELECT ${forwardColumns} FROM mcp_routing_forward_charges WHERE source_operation_id=$1 LIMIT 2`, [operationId])).rows + if (existing.length) { + const value = await authenticateForwardCharge(existing[0], observedAt) + if (existing.length !== 1 || value.planId !== initial.id || value.profileId !== authority.packet.profileId || value.connectionId !== current.id || + value.ownerId !== current.owner_id || value.authorityProof !== authority.proof || value.profileVersion !== authority.packet.profileVersion || + fingerprint(value.principal) !== fingerprint(principal) || value.service !== step.service || + value.evidenceProof !== fingerprint(selected.plan.evidence.sealed) || value.payloadProof !== fingerprint(selected.plan.payload.sealed)) throw fail("Repeated forward identity differs") + return + } + const deadline = Math.min(initial.expires_at.getTime(), Date.parse(authority.packet.validUntil), Date.parse(principal.credentialExpiresAt)) + const charges = await readHistoricalChargesWithinOwner(db, authority.packet.profileId, current, observedAt, deadline) + if (charges.totalCharges + 1 > routeAiProfileSchema.parse(step.input.profile).maxSwitchesPerHour) throw fail("Insufficient common profile switch slots") + if ((await db.query("SELECT id FROM mcp_routing_reservations WHERE source_operation_id=$1 AND source_step_index=0 LIMIT 2", [operationId])).rowCount) throw fail("Forward already has reserved allocation") + const value = forwardChargeSchema.parse({ revision: 1, operationId, planId: initial.id, stepIndex: 0, + profileId: authority.packet.profileId, profileVersion: authority.packet.profileVersion, connectionId: current.id, ownerId: current.owner_id, + principal, service: step.service, operationVersion: 7, authorityProof: authority.proof, + evidenceProof: fingerprint(selected.plan.evidence.sealed), payloadProof: fingerprint(selected.plan.payload.sealed), scope: authority.packet.scope, + createdAt: observedAt, chargeUnits: 1 }) + await assertPersistentScopedProfileAuthority(step.input) + const proof = await forwardChargeMac(value) + const inserted = await db.query(`INSERT INTO mcp_routing_forward_charges(source_operation_id,source_plan_id,source_step_index,profile_id,connection_id, + charge_units,binding_sealed,binding_proof,created_at) VALUES($1,$2,0,$3,$4,1,$5,$6,$7) RETURNING created_at,binding_proof`, + [operationId, initial.id, value.profileId, current.id, await encryptSecret(JSON.stringify(value)), proof, observedAt]) + if (inserted.rowCount !== 1 || iso(inserted.rows[0].created_at) !== observedAt || inserted.rows[0].binding_proof !== proof) throw fail("Forward charge insertion/time was not confirmed") + await recordRoutingChargeAnchor(db, { profileId: value.profileId, connectionId: value.connectionId, ownerId: value.ownerId, + sourceOperationId: operationId, allocationId: operationId, kind: "forward", bindingProof: proof, createdAt: observedAt }, deadline) + await assertPersistentScopedProfileAuthority(step.input) +} + +/** Exact completed source identity, not timestamp dedup or absence-as-legacy. + * Existing native baseline/witness verification remains independently required. */ +export async function assertIdentifiedRoutingCompletion(db: PoolClient, profileId: string, planId: string, operationId: string, principal: McpPrincipal) { + const current = await effectiveMcpConnection(principal, db) + const source = (await db.query(`SELECT o.state, + CASE WHEN octet_length(p.evidence->>'sealed')<=8388608 THEN p.evidence->>'sealed' END AS evidence_sealed, + CASE WHEN octet_length(p.payload->>'sealed')<=8388608 THEN p.payload->>'sealed' END AS payload_sealed + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id WHERE o.id=$1 AND p.id=$2 AND o.connection_id=$3 AND p.operation_id='routing.community.switch' AND p.operation_version=7`, [operationId, planId, current.id])).rows + if (source.length !== 1 || source[0].state !== "applied" || typeof source[0].evidence_sealed !== "string" || typeof source[0].payload_sealed !== "string") throw fail("Completion source is not actually applied/bounded") + const observedAt = iso((await db.query("SELECT clock_timestamp() AS at")).rows[0].at) + const forward = (await db.query(`SELECT ${forwardColumns} FROM mcp_routing_forward_charges WHERE source_operation_id=$1 LIMIT 2`, [operationId])).rows + const reserved = (await db.query("SELECT id FROM mcp_routing_reservations WHERE source_operation_id=$1 AND source_step_index=0 LIMIT 2", [operationId])).rows + if (forward.length + reserved.length !== 1) throw fail("Completion charge absent or ambiguous; human baseline review required") + if (forward.length) { + const value = await authenticateForwardCharge(forward[0], observedAt) + if (value.planId !== planId || value.profileId !== profileId || value.connectionId !== current.id || value.ownerId !== current.owner_id || + value.evidenceProof !== fingerprint(source[0].evidence_sealed) || value.payloadProof !== fingerprint(source[0].payload_sealed) || + value.scope.some(id => !current.policy.scope.serverIds.includes(id))) throw fail("Completion forward charge differs") + } else { + const value = await load(db, reserved[0].id, false, true) + if (value.binding.planId !== planId || value.binding.profileId !== profileId || value.binding.operationId !== operationId || + value.binding.principal.connectionId !== current.id || value.binding.ownerId !== current.owner_id || value.state.forward !== "spent") throw fail("Completion reserved charge differs") + } +} + +function confirmForward(result: Awaited>) { + // Only a confirmed NEW COMMIT can mint this process-local allocation. + // An unknown ACK, repeated reserve, read or restart cannot reconstruct it. + let forwardAllocation: RoutingReservedForwardAllocation | undefined + if (result.created) { + forwardAllocation = Object.freeze({ kind: "routing-reserved-forward-allocation" }) + forwardAllocations.set(forwardAllocation, { reservationId: result.reservationId, bindingProof: result.bindingProof }) + } + const { bindingProof: _bindingProof, ...publicReceipt } = result + return { ...publicReceipt, forwardAllocation } +} + +/** The wrapper owns BEGIN/COMMIT and emits allocations only after its own ACK. + * The callback can create the runtime operation on this exact backend; it + * cannot attest a COMMIT or inject authority/signing/native permissions. */ +export async function withRoutingReservationAdmission(work: (db: PoolClient, reserve: (operationId: string) => Promise) => Promise, connect?: RoutingReservationConnect) { + let admitted: Awaited> | undefined + const value = await routingReservationTransaction(async db => { + const result = await work(db, async operationId => { + if (admitted) throw fail("Only one reservation may be admitted per plan transaction") + admitted = await reserveWithinOwner(db, operationId) + }) + if (admitted) await live(db, await load(db, admitted.reservationId)) + return result + }, connect) + return { value, reservation: admitted ? confirmForward(admitted) : undefined } +} + +export function createRoutingReservationStore(connect?: RoutingReservationConnect) { + const tx = (work: (db: PoolClient) => Promise, readOnly = false) => routingReservationTransaction(work, connect, readOnly) + return { + /** Private read-only historical snapshot, never admission or native authority. + * Expired original grants/credentials remain charged; current READ access is + * checked separately before/after the snapshot. No live() filtering. */ + readHourlyCharges: async (profileId: string, supplied: McpPrincipal) => { + uuid.parse(profileId) + const principal = Object.freeze(structuredClone(supplied)), started = Date.now() + const authorize = async () => { + if (Date.now() - started >= 10000) throw fail("Hourly ledger read expired") + const current = await effectiveMcpConnection(principal) + if (Date.now() - started >= 10000) throw fail("Hourly ledger read expired") + if (!hasPermission(current.owner_permissions, "mm:network:read") || !hasPermission(current.policy.permissions, "mm:network:read")) throw fail("Hourly ledger read denied") + return current + } + const initial = await authorize() + const accessProof = (value: typeof initial) => fingerprint({ id: value.id, owner: value.owner_id, + policy: value.policy, generation: value.generation, permissions: value.owner_permissions, validUntil: value.owner_valid_until }) + const keyEpoch = await privateFingerprinterExisting("routing-hourly-read-key-epoch-v1") + const epoch = keyEpoch("existing-material") + const result = await tx(async db => { + await db.query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ") + return Object.freeze({ ...await readHistoricalChargesWithinOwner(db, profileId, initial, undefined, started + 10000), scope: "observed-repeatable-read-snapshot" as const }) + }, true) + if (accessProof(await authorize()) !== accessProof(initial) || + (await privateFingerprinterExisting("routing-hourly-read-key-epoch-v1"))("existing-material") !== epoch) throw fail("Hourly read authority/key changed") + if (Date.now() - started >= 10000) throw fail("Hourly ledger read expired") + return result + }, + reserve: (operationId: string) => tx(db => reserveWithinOwner(db, operationId)).then(confirmForward), + read: (id: string, principal: McpPrincipal) => tx(async db => { + const value = await load(db, id), current = await effectiveMcpConnection(principal, db) + if (current.id !== value.binding.principal.connectionId || current.owner_id !== value.binding.ownerId || !hasPermission(current.owner_permissions, "mm:network:read") || !hasPermission(current.policy.permissions, "mm:network:read") || value.binding.scope.some(serverId => !current.policy.scope.serverIds.includes(serverId))) throw fail("Reservation read denied") + return receipt(value) + }, true), + beginForwardAttempt: (allocation: RoutingReservedForwardAllocation, expected?: { operationId: string; stepIndex: number; input: Record }) => { + const captured = forwardAllocations.get(allocation) + if (!captured) return Promise.reject(fail("No internal reserved forward allocation capability")) + forwardAllocations.delete(allocation) + return tx(async db => { + const record = await load(db, captured.reservationId, true), value = await live(db, record) + if (record.bindingProof !== captured.bindingProof) throw fail("Forward allocation source changed") + if (expected && (expected.operationId !== record.binding.operationId || expected.stepIndex !== record.binding.stepIndex || await assertScopedCommunityRequest(expected.input) !== record.binding.sourceInputSeal)) throw fail("Forward allocation runner context differs") + await assertRunningReservedIntent(db, value) + return saveState(db, record, transitionRoutingReservation(record.state, "forward", "attempt")) + }).then(saved => { + forwardAttempts.set(allocation, { ...captured, stateProof: saved.stateProof }) + return receipt(saved) + }) + }, + recordForwardOutcome: (id: string): Promise<{ receipt: ReturnType; allocation?: RoutingReservedInverseAllocation }> => tx(async db => { + const record = await load(db, id, true), value = await live(db, record) + if (record.state.forward !== "spent" || record.state.outcome || !["running", "applied", "partial"].includes(value.row.operation_state)) throw fail("Forward outcome cannot be republished") + const config = await loadScopedConfigurationPacket(value.input, value.inputSeal, { operationId: record.binding.operationId, stepIndex: 0 }, db) + const network = await loadScopedNetworkCheckpoint(value.input, value.inputSeal, db) + if (!config || !network) throw fail("No actual CONFIG_APPLIED and signed terminal network result") + // The signed control probe and REST observations do not attest the event interval. + if (network.status === "failed") throw fail("Authenticated network evidence for automatic recovery unavailable") + const next: State = { ...record.state, revision: record.state.revision + 1, outcome: { status: network.status, configurationProof: config.checkpointProof, networkProof: network.networkSeal } } + const saved = await saveState(db, record, next) + return { receipt: receipt(saved) } + }), + beginInverseAttempt: (allocation: RoutingReservedInverseAllocation) => { + const captured = inverseAllocations.get(allocation) + if (!captured) return Promise.reject(fail("No internal reserved inverse allocation capability")) + inverseAllocations.delete(allocation) + return tx(async db => { + const record = await load(db, captured.reservationId, true), value = await live(db, record) + if (record.stateProof !== captured.outcomeProof) throw fail("Inverse allocation state changed") + await assertRunningReservedIntent(db, value) + const saved = await saveState(db, record, transitionRoutingReservation(record.state, "inverse", "attempt")) + return { receipt: receipt(saved), bindingProof: saved.bindingProof, stateProof: saved.stateProof } + }).then(result => { + const execution: RoutingReservedInverseExecution = Object.freeze({ kind: "routing-reserved-inverse-execution" }) + inverseExecutions.set(execution, { reservationId: captured.reservationId, bindingProof: result.bindingProof, stateProof: result.stateProof }) + return { ...result.receipt, execution } + }) + }, + inspectInverseExecution: (execution: RoutingReservedInverseExecution) => { + const captured = inverseExecutions.get(execution) + if (!captured) return Promise.reject(fail("No internal reserved inverse execution capability")) + return tx(async db => { + const record = await load(db, captured.reservationId), value = await live(db, record) + if (record.bindingProof !== captured.bindingProof || record.stateProof !== captured.stateProof || record.state.forward !== "spent" || record.state.inverse !== "spent" || record.state.outcome?.status !== "failed") throw fail("Reserved inverse execution state differs") + await assertRunningReservedIntent(db, value) + const binding = record.binding + return { reservationId: binding.id, sourceOperationId: binding.operationId, sourceStepIndex: binding.stepIndex, sourceReceiptId: binding.sourceReceiptId, + inverseReceiptId: binding.inverseReceiptId, deadline: binding.deadline, profileId: binding.profileId, profileVersion: binding.profileVersion, + principal: structuredClone(binding.principal), ownerId: binding.ownerId, scope: [...binding.scope], sourceInputSeal: binding.sourceInputSeal, + configurationProof: record.state.outcome.configurationProof, networkProof: record.state.outcome.networkProof } + }, true) + }, + proveUnusedInverse: (id: string) => tx(async db => { + const record = await load(db, id, true), value = await live(db, record) + if (value.row.operation_state !== "applied") throw fail("Unused inverse needs a terminal applied forward") + const verified = await loadScopedRecoverySource(record.binding.operationId, { principal: value.principal, connection: await effectiveMcpConnection(value.principal, db) }) + if (verified.network.status !== "passed" || verified.configuration.checkpointProof !== record.state.outcome?.configurationProof || verified.network.networkSeal !== record.state.outcome?.networkProof) throw fail("No generic verified proof for unused inverse") + return receipt(await saveState(db, record, transitionRoutingReservation(record.state, "inverse", "prove-unused"))) + }), + markUnknown: (id: string, lane: RoutingReservationLane) => tx(async db => { + const record = await load(db, id, true) + // Conservative quarantine does not authorize a native request, release + // budget, create a capability or extend the original deadline. + return receipt(await saveState(db, record, transitionRoutingReservation(record.state, lane, "unknown"), true)) + }), + } +} + +/** Read-only validation of the original process-local, acknowledged forward spend. */ +export function assertAcknowledgedRoutingForwardAttempt(allocation: RoutingReservedForwardAllocation, + operationId: string, principal: McpPrincipal) { + const captured = forwardAttempts.get(allocation) + if (!captured) return Promise.reject(fail("No acknowledged reserved forward attempt")) + return routingReservationTransaction(async db => { + const record = await load(db, captured.reservationId) + if (record.bindingProof !== captured.bindingProof || record.stateProof !== captured.stateProof || + record.binding.operationId !== operationId || fingerprint(record.binding.principal) !== fingerprint(principal) || + record.state.forward !== "spent" || record.state.inverse !== "held" || record.state.outcome) { + throw fail("Original reserved forward attempt changed") + } + const value = await live(db, record) + if (value.row.operation_state !== "running" || value.row.plan_state !== "executing") throw fail("Reserved forward source no longer running") + return Date.parse(record.binding.deadline) + }, undefined, true) +} + +/** Transport-side conservation check for an already admitted nonreserved forward. + * Called only with inspected private service origin by the active-profile guard. + * No debit, grant, allocation capability, replay or legacy-absence fallback. + * Reserved attempts retain their separate beginForwardAttempt capability path. */ +export async function assertPersistentRoutingForwardTransportCharge(db: Pick, + metadata: Readonly, ownerId: string) { + const authority = routingProfileAuthorityBindingSchema.parse(metadata.serviceInput.persistentProfileAuthority) + const profile = routeAiProfileSchema.parse(metadata.serviceInput.profile) + if (metadata.operationName !== "routing.community.switch" || metadata.operationVersion !== 7 || metadata.phase !== "forward" || + metadata.stepIndex !== 0 || !["routing.community", "routing.community.scoped"].includes(metadata.serviceName) || profile.autoRecovery || + authority.packet.connectionId !== metadata.principal.connectionId || authority.packet.credentialId !== metadata.principal.credentialId || + authority.packet.ownerId !== ownerId) throw fail("Nonreserved transport charge lane differs") + uuid.parse(metadata.operationId); uuid.parse(metadata.planId) + const source = (await db.query(`SELECT o.state AS operation_state,p.state AS plan_state,p.policy_version,p.generation,p.approved_by, + date_trunc('milliseconds',clock_timestamp()) AS observed_at, + CASE WHEN octet_length(p.evidence->>'sealed')<=8388608 THEN p.evidence->>'sealed' END AS evidence_sealed, + CASE WHEN octet_length(p.payload->>'sealed')<=8388608 THEN p.payload->>'sealed' END AS payload_sealed, + s.state AS step_state,CASE WHEN octet_length(s.intended_state::text)<=1048576 THEN s.intended_state END AS intended_state + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + WHERE o.id=$1 AND p.id=$2 AND o.connection_id=$3 AND p.operation_id='routing.community.switch' AND p.operation_version=7 LIMIT 2`, + [metadata.operationId, metadata.planId, metadata.principal.connectionId])).rows + if (source.length !== 1) throw fail("Exact transport source unavailable") + const row = source[0], observedAt = iso(row.observed_at) + if (row.operation_state !== "running" || row.plan_state !== "executing" || row.step_state !== "intent" || !row.approved_by || + metadata.serviceName === "routing.community.scoped" && row.approved_by !== ownerId || + row.policy_version !== metadata.policyVersion || row.generation !== metadata.generation || + typeof row.evidence_sealed !== "string" || typeof row.payload_sealed !== "string" || + fingerprint(row.intended_state) !== fingerprint(redact({ kind: "service", service: metadata.serviceName, input: metadata.serviceInput })) || + createHash("sha256").update(row.evidence_sealed).digest("hex") !== metadata.sealedEvidenceProof || + createHash("sha256").update(row.payload_sealed).digest("hex") !== metadata.sealedPayloadProof || + !Number.isSafeInteger(metadata.originalExpiresAt) || Date.parse(observedAt) >= metadata.originalExpiresAt) throw fail("Running transport source differs or expired") + const charges = (await db.query(`SELECT ${forwardColumns} FROM mcp_routing_forward_charges WHERE source_operation_id=$1 LIMIT 2`, [metadata.operationId])).rows + const reserved = (await db.query("SELECT id FROM mcp_routing_reservations WHERE source_operation_id=$1 AND source_step_index=0 LIMIT 2", [metadata.operationId])).rows + if (charges.length !== 1 || reserved.length !== 0) throw fail("Transport forward charge absent or ambiguous") + const value = await authenticateForwardCharge(charges[0], observedAt) + if (value.planId !== metadata.planId || value.profileId !== authority.packet.profileId || value.profileVersion !== authority.packet.profileVersion || + value.connectionId !== metadata.principal.connectionId || value.ownerId !== ownerId || value.authorityProof !== authority.proof || + fingerprint(value.principal) !== fingerprint(metadata.principal) || value.service !== metadata.serviceName || + value.evidenceProof !== fingerprint(row.evidence_sealed) || value.payloadProof !== fingerprint(row.payload_sealed)) throw fail("Transport original forward charge differs") +} diff --git a/backend/src/modules/mcp/routing-reservation-transaction.ts b/backend/src/modules/mcp/routing-reservation-transaction.ts new file mode 100644 index 00000000..7d06e29d --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation-transaction.ts @@ -0,0 +1,59 @@ +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { appendRoutingChargeJournal, type RoutingChargeWitness } from "./routing-charge-journal.js" +import { McpError } from "./security.js" +import { assertOriginalMcpTemporaryGrant, assertOriginalMcpTemporaryGrantLive, type OriginalServiceGrant } from "./temporary-grant-origin.js" + +export class RoutingReservationCommitUnknown extends Error { readonly automaticReplay = false; constructor() { super("Routing reservation COMMIT outcome unknown; replay forbidden") } } +export type RoutingReservationConnect = () => Promise +const admissions = new WeakMap() + +export function bindRoutingChargeAdmission(db: PoolClient, connectionId: string, credentialId: string, grant: OriginalServiceGrant) { + const admission = admissions.get(db) + if (!admission || admission.origin || admission.charge) throw new Error("Routing charge origin requires an unstaged admission") + admission.origin = { connectionId, credentialId, grant: Object.freeze({ ...grant }) } +} + +function assertChargeDeadline(deadline: number) { + if (!Number.isFinite(deadline) || deadline <= Date.now()) { + throw new McpError("STATE_CONFLICT", "Исходный срок допуска переключения истёк", 409) + } +} + +export function stageRoutingChargeWitness(db: PoolClient, profileId: string, witness: RoutingChargeWitness, deadline: number) { + const admission = admissions.get(db) + if (!admission || admission.charge) throw new Error("Routing charge requires one owned admission transaction") + assertChargeDeadline(deadline) + admission.charge = { profileId, witness: Object.freeze({ ...witness }), deadline } +} +/** Only connection ownership is injectable for transport fault tests. Signing + * and all authority validation remain fixed production functions in the store. */ +export async function routingReservationTransaction(work: (db: PoolClient) => Promise, connect: RoutingReservationConnect = () => pool.connect(), readOnly = false): Promise { + const db = await connect(); let commitSent = false, destroy = false + try { + await db.query(readOnly ? "BEGIN READ ONLY" : "BEGIN") + await db.query("SET LOCAL lock_timeout='5s'"); await db.query("SET LOCAL statement_timeout='10s'") + if (!readOnly) admissions.set(db, {}) + const result = await work(db) + const { charge, origin } = admissions.get(db) ?? {} + // SQL corroborates this witness after a confirmed COMMIT. An interrupted + // flush/COMMIT retains a restrictive witness, never a dispatch capability. + if (charge) { + assertChargeDeadline(charge.deadline) + if (origin) assertOriginalMcpTemporaryGrantLive(origin.grant) + await appendRoutingChargeJournal(charge.profileId, charge.witness) + if (origin) await assertOriginalMcpTemporaryGrant(db, origin.connectionId, origin.credentialId, origin.grant) + assertChargeDeadline(charge.deadline) + } + commitSent = true + if ((await db.query("COMMIT")).command !== "COMMIT") throw new Error("Routing admission COMMIT acknowledgement unavailable") + return result + } catch (error) { + if (commitSent) { destroy = true; throw new RoutingReservationCommitUnknown() } + try { await db.query("ROLLBACK") } catch { destroy = true } + throw error + } finally { admissions.delete(db); db.release(destroy) } +} diff --git a/backend/src/modules/mcp/routing-reservation.sql-helper.ts b/backend/src/modules/mcp/routing-reservation.sql-helper.ts new file mode 100644 index 00000000..2b3903b9 --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation.sql-helper.ts @@ -0,0 +1,829 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, join, resolve } from "node:path" +import { readFile, writeFile } from "node:fs/promises" +import type { PoolClient } from "pg" + +/** Actual SQL/crypto numbered-schema acceptance only; no public tool registration, + * native callback, controller tick, reservation refund or fixture DELETE. */ +export function assertOwnedRoutingReservationSql() { + assert.equal(process.env.MMAPP_OWN_ROUTING_RESERVATIONS, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +} + +export async function acceptRoutingReservationSql(options: { runtimeAdmission?: boolean; commonAdmissionOnly?: boolean; forwardChargeOnly?: boolean } = {}) { + assertOwnedRoutingReservationSql() + const { pool } = await import("../../db/index.js") + const { mcpConnectionPolicySchema } = await import("@mmapp/contracts/mcp") + const { routeAiProfileSchema } = await import("@mmapp/contracts/mcp-routing") + const security = await import("./security.js") + const { createRoutingSourceContext, readRoutingEndpoints } = await import("./routing-observation-source.js") + const { resolveRoutingProfileAuthority } = await import("./routing-profile-authority.js") + const { assertAcknowledgedRoutingForwardAttempt, createRoutingReservationStore, withRoutingReservationAdmission, readHistoricalChargesWithinOwner } = await import("./routing-reservation-store.js") + const { RoutingReservationCommitUnknown } = await import("./routing-reservation-transaction.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0040_mcp_routing_reservations'")).rows[0].n, 1, "Numbered routing reservation enrollment required") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservations")).rows[0].n, 0, "Acceptance must start with empty enrolled ledgers") + const previousEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean + const ids: number[] = [], profileIds: string[] = [], names: string[] = [] + const store = createRoutingReservationStore() + // Positive historical fixtures explicitly reconstruct every durable record. + const replaceFixtureAnchor = async (id: string, bindingProof: string, createdAt: string) => { + const rows = (await pool.query("SELECT id,payload FROM mcp_audit WHERE action='routing.charge.admitted' AND payload->'anchor'->>'allocationId'=$1", [id])).rows + assert.equal(rows.length, 1) + const anchor = { ...rows[0].payload.anchor, bindingDigest: security.fingerprint(bindingProof), createdAt } + const mac = await security.privateSecretFingerprint(security.fingerprint(anchor), "routing-charge-audit-v1") + await pool.query("UPDATE mcp_audit SET payload=$2::jsonb,created_at=$3 WHERE id=$1", [rows[0].id, JSON.stringify({ anchor, mac }), createdAt]) + const file = join(security.mcpStateDirectory(), "routing-charge-journal", anchor.profileId + ".json") + const { body } = JSON.parse(await readFile(file, "utf8")) + const witness = body.witnesses.find((value: { allocationId: string }) => value.allocationId === id) + assert(witness) + Object.assign(witness, { createdAt, auditMac: mac }) + const hostMac = await security.privateSecretFingerprint(security.fingerprint(body), "routing-charge-host-journal-v1") + await writeFile(file, JSON.stringify({ body, mac: hostMac }) + "\n") + } + try { + ids.push(...(await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES($1,'127.0.0.1',9,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id", ["reservation-synthetic-" + randomUUID()])).rows.map(row => Number(row.id))) + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + const contextSources = (await createRoutingSourceContext(await readRoutingEndpoints(ids))).expected + const fixture = async (mutations = 10, maxSwitches = 3, autoRecovery = true) => { + const connectionId = randomUUID(), credentialId = randomUUID(), profileId = randomUUID(), ownerId = "reservation-owner-" + randomUUID() + const now = Date.now(), expiresAt = new Date(now + 600000).toISOString(), generation = await security.accessGeneration() + const permissions = ["mm:network:read", "mm:network:write", "mm:filters:write"] + const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt, + scope: { serverIds: ids, operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "operations.recover"], allowGlobal: false }, + limits: { callsPerMinute: 100, mutationsPerHour: mutations, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } }) + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'synthetic reservation',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), generation, expiresAt, new Date(now - 1000)]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), expiresAt]) + let profile = routeAiProfileSchema.parse({ name: "Synthetic reserved profile", serverId: ids[0], community: "65000:10", family: "ipv4", table: "tenant", probeVrf: "probe", autoRecovery, maxSwitchesPerHour: maxSwitches, + candidates: ["a", "b"].map((id, index) => ({ id, jumpHostId: ids[0], exitNodeId: ids[0], wanInterface: "synthetic-wan", downstreamInterface: "synthetic-hop", gateway: "192.0.2." + (index + 1), + gatewayRef: { resource: "ipv4_route", id: "*" + (index + 1) }, controlProbe: { targetInterface: "synthetic-exit", expectedHops: [{ serverId: ids[0], interface: "synthetic-exit" }] } })) }) + await pool.query("INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version) VALUES($1,$2,$3::jsonb,true,$4,$5,$6,1)", [profileId, connectionId, JSON.stringify(profile), expiresAt, credentialId, generation]) + profileIds.push(profileId) + const principal = { connectionId, credentialId, credentialExpiresAt: expiresAt } + let authority = await resolveRoutingProfileAuthority({ profileId, expectedVersion: 1, profile, principal, ownerId, transport: { contextSources } }) + const makeSource = async (createOperation = true) => { + const operationId = randomUUID(), planId = randomUUID() + const input: Record = { serverId: ids[0], profile, principal, ownerId, candidateId: "b", originalCandidateId: "a", persistentProfileAuthority: authority, + contextSources, policyProof: security.fingerprint(policy), connectionGeneration: generation, generation, receiptId: randomUUID(), proofRevision: 3, + nativeId: "*1", dependencyProof: "d".repeat(64), originalTargetProof: "a".repeat(64), desiredTargetProof: "b".repeat(64), + originalRuleCiphertext: await security.encryptSecret("synthetic original command; no native request made"), desiredRuleCiphertext: await security.encryptSecret("synthetic desired command; no native request made"), + rules: [{ community: profile.community, gateway: "192.0.2.2" }], beforeRules: [{ community: profile.community, gateway: "192.0.2.1" }] } + input.requestProof = await security.privateSecretFingerprint(security.fingerprint(input), "route-ai-scoped-request-v3") + const evidence = { target: { scope: "servers", serverIds: ids }, objects: [], snapshots: [], before: input.beforeRules, steps: [{ kind: "service", service: "routing.community.scoped", input }], warnings: [], requiresHuman: true, mutationCost: 2 } + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by,approved_at) VALUES($1,$2,'routing.community.switch',7,1,$3,$4::jsonb,$5::jsonb,$8,$6,$7,now())", [planId, connectionId, generation, JSON.stringify({ sealed: await security.encryptSecret(JSON.stringify({ profile, candidateId: "b" })) }), JSON.stringify({ sealed: await security.encryptSecret(JSON.stringify(evidence)) }), expiresAt, ownerId, createOperation ? "executing" : "approved"]) + if (createOperation) await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'pending')", [operationId, planId, connectionId, randomUUID()]) + return { operationId, planId, input } + } + const renew = async () => { + const row = (await pool.query("SELECT config,version FROM mcp_routing_profiles WHERE id=$1", [profileId])).rows[0] + profile = routeAiProfileSchema.parse(row.config) + authority = await resolveRoutingProfileAuthority({ profileId, expectedVersion: row.version, profile, principal, ownerId, transport: { contextSources } }) + } + return { connectionId, credentialId, profileId, principal, makeSource, renew } + } + const used = async (connectionId: string) => Number((await pool.query("SELECT COALESCE(sum(used),0)::int AS used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations'", [connectionId])).rows[0].used) + if (!options.commonAdmissionOnly && !options.forwardChargeOnly) { + { + const f = await fixture(2, 2), source = await f.makeSource() + const concurrent = await Promise.all([store.reserve(source.operationId), store.reserve(source.operationId)]) + assert.equal(concurrent.filter(value => value.created).length, 1) + assert.equal(concurrent[0].reservationId, concurrent[1].reservationId) + assert.equal(concurrent.filter(value => !!value.forwardAllocation).length, 1) + assert.equal((await store.reserve(source.operationId)).forwardAllocation, undefined) + assert.equal((await createRoutingReservationStore().reserve(source.operationId)).forwardAllocation, undefined) + await assert.rejects(store.beginForwardAttempt(JSON.parse(JSON.stringify({ kind: "routing-reserved-forward-allocation" }))), /capability/) + assert.equal(await used(f.connectionId), 2) + const shared = (await pool.query("SELECT mode,slot_phase,source_configuration_proof,reservation_id FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [source.operationId])).rows + assert.equal(shared.length, 1); assert.equal(shared[0].mode, "reserved"); assert.equal(shared[0].slot_phase, "reservation-held"); assert.equal(shared[0].source_configuration_proof, null) + assert.equal((await store.read(concurrent[0].reservationId, f.principal)).nativeDispatchAvailable, false) + const newerId = randomUUID() + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [newerId, f.connectionId, randomUUID(), f.principal.credentialExpiresAt]) + await store.read(concurrent[0].reservationId, { ...f.principal, credentialId: newerId }) + assert.equal(await used(f.connectionId), 2, "Historical read cannot allocate fresh budget") + // OwnPG-only production-crypto hourly reader acceptance. No native request, + // CONFIG_APPLIED or network receipt is fabricated by these fixtures. + const historicalPrincipal = { ...f.principal, credentialId: newerId } + const charged = () => store.readHourlyCharges(f.profileId, historicalPrincipal) + assert.equal((await charged()).totalCharges, 2) + await pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=now()-interval '1 second',enabled=false,version=version+1 WHERE id=$1", [f.profileId]) + await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [f.credentialId]) + assert.equal((await charged()).totalCharges, 2, "Old grant/credential/profile version never erases charge") + const at = new Date().toISOString() + await pool.query("UPDATE mcp_routing_profiles SET state=$2::jsonb WHERE id=$1", [f.profileId, JSON.stringify({ switches: [at, at] })]) + const projected = await charged() + assert.equal(projected.legacyCharges, 2); assert.equal(projected.reservedCharges, 2); assert.equal(projected.totalCharges, 4) + assert.equal(projected.budgetFits, false); assert.equal(projected.dispatchAllowed, false); assert.equal(projected.securityProof, false) + const reservationId = concurrent[0].reservationId + const original = (await pool.query("SELECT * FROM mcp_routing_reservations WHERE id=$1", [reservationId])).rows[0] + const originalShared = (await pool.query("SELECT * FROM mcp_scoped_recovery_intents WHERE reservation_id=$1", [reservationId])).rows[0] + const originalSlots = (await pool.query("SELECT * FROM mcp_routing_reservation_slots WHERE reservation_id=$1 ORDER BY lane", [reservationId])).rows + const restore = async () => { + await pool.query("UPDATE mcp_routing_reservations SET binding_sealed=$2,binding_proof=$3,state_sealed=$4,state_proof=$5,revision=$6,profile_id=$7,deadline=$8,created_at=$9 WHERE id=$1", [reservationId, original.binding_sealed, original.binding_proof, original.state_sealed, original.state_proof, original.revision, original.profile_id, original.deadline, original.created_at]) + for (const slot of originalSlots) await pool.query("INSERT INTO mcp_routing_reservation_slots(reservation_id,profile_id,lane,state,created_at) VALUES($1,$2,$3,$4,$5) ON CONFLICT(reservation_id,lane) DO UPDATE SET state=excluded.state,profile_id=excluded.profile_id,created_at=excluded.created_at", [reservationId, slot.profile_id, slot.lane, slot.state, slot.created_at]) + await pool.query("UPDATE mcp_scoped_recovery_intents SET intent_sealed=$2,intent_proof=$3,state=$4,slot_phase=$5,source_configuration_proof=$6,recovery_receipt_id=$7,deadline=$8,created_at=$9,inverse_intent_sealed=$10,inverse_intent_proof=$11,inverse_configuration_sealed=$12,inverse_configuration_proof=$13,verified_sealed=$14,verified_proof=$15 WHERE reservation_id=$1", [reservationId, originalShared.intent_sealed, originalShared.intent_proof, originalShared.state, originalShared.slot_phase, originalShared.source_configuration_proof, originalShared.recovery_receipt_id, originalShared.deadline, originalShared.created_at, originalShared.inverse_intent_sealed, originalShared.inverse_intent_proof, originalShared.inverse_configuration_sealed, originalShared.inverse_configuration_proof, originalShared.verified_sealed, originalShared.verified_proof]) + await replaceFixtureAnchor(reservationId, original.binding_proof, original.created_at.toISOString()) + } + const invalid = async (sql: string, values: unknown[]) => { + try { await pool.query(sql, values); await assert.rejects(charged) } finally { await restore() } + } + await invalid("UPDATE mcp_routing_reservations SET binding_proof=$2 WHERE id=$1", [reservationId, "0".repeat(64)]) + await invalid("UPDATE mcp_routing_reservations SET state_proof=$2 WHERE id=$1", [reservationId, "0".repeat(64)]) + await invalid("UPDATE mcp_routing_reservation_slots SET state='spent' WHERE reservation_id=$1 AND lane='inverse'", [reservationId]) + await invalid("DELETE FROM mcp_routing_reservation_slots WHERE reservation_id=$1 AND lane='inverse'", [reservationId]) + await invalid("UPDATE mcp_scoped_recovery_intents SET recovery_receipt_id=$2 WHERE reservation_id=$1", [reservationId, randomUUID()]) + await invalid("UPDATE mcp_routing_reservations SET state_sealed=$2 WHERE id=$1", [reservationId, "x".repeat(131073)]) + await invalid("UPDATE mcp_routing_reservations SET created_at=clock_timestamp()+interval '1 minute' WHERE id=$1", [reservationId]) + const foreign = await fixture() + await invalid("UPDATE mcp_routing_reservations SET profile_id=$2 WHERE id=$1", [reservationId, foreign.profileId]) + const overflow: string[] = [] + try { + for (let index = 0; index < 11; index++) { + const extra = await f.makeSource(), id = randomUUID(); overflow.push(id) + // Deliberately invalid identity clones: bound refusal must happen + // before loading any ciphertext, not after returning a partial count. + await pool.query(`INSERT INTO mcp_routing_reservations(id,source_plan_id,source_operation_id,source_step_index,profile_id,connection_id,binding_sealed,binding_proof,state_sealed,state_proof,revision,deadline,created_at) + SELECT $2,$3,$4,source_step_index,profile_id,connection_id,binding_sealed,binding_proof,state_sealed,state_proof,revision,deadline,created_at FROM mcp_routing_reservations WHERE id=$1`, [reservationId, id, extra.planId, extra.operationId]) + } + await assert.rejects(charged, /count bound/) + } finally { await pool.query("DELETE FROM mcp_routing_reservations WHERE id=ANY($1::uuid[])", [overflow]) } + // Readable old state remains charged under original source inverse slot. + // Signed historical state fixtures exercise integrity ONLY, not success + // of a real forward/inverse or production fault/network verification. + for (const [forward, inverse] of [["held", "held"], ["spent", "spent"], ["spent", "unknown"], ["spent", "proven-unused"]]) { + try { + const outcome = forward === "held" ? undefined : { status: inverse === "proven-unused" ? "passed" : "failed", configurationProof: "c".repeat(64), networkProof: "d".repeat(64) } + const state = { revision: original.revision + 1, forward, inverse, ...(outcome ? { outcome } : {}) } + const stateProof = await security.privateSecretFingerprint(security.fingerprint({ bindingProof: original.binding_proof, state }), "routing-reservation-state-v1") + const phase = inverse === "spent" || inverse === "unknown" ? "reserved-attempt" : outcome ? "reservation-bound" : "reservation-held" + const sharedPacket = { phase, reservationId, bindingProof: original.binding_proof, stateProof } + await pool.query("UPDATE mcp_routing_reservations SET state_sealed=$2,state_proof=$3,revision=$4 WHERE id=$1", [reservationId, await security.encryptSecret(JSON.stringify(state)), stateProof, state.revision]) + await pool.query("UPDATE mcp_routing_reservation_slots SET state=CASE lane WHEN 'forward' THEN $2 ELSE $3 END WHERE reservation_id=$1", [reservationId, forward, inverse]) + await pool.query("UPDATE mcp_scoped_recovery_intents SET intent_sealed=$2,intent_proof=$3,state=$4,slot_phase=$5,source_configuration_proof=$6 WHERE reservation_id=$1", [reservationId, await security.encryptSecret(JSON.stringify(sharedPacket)), await security.privateSecretFingerprint(security.fingerprint(sharedPacket), "routing-reservation-shared-slot-v1"), inverse === "unknown" ? "unknown" : "intent", phase, outcome?.configurationProof ?? null]) + assert.equal((await charged()).reservedCharges, 2) + } finally { await restore() } + } + // Positive historical RECOVERY_VERIFIED verification: real ownSQL rows, + // fixed production encryption/MAC/domains and actual schema verifier. + // Synthetic stored receipt fixture; NOT a completed native recovery. + try { + const start = Date.now() - 3000, until = new Date(start + 1500).toISOString() + const binding = { ...JSON.parse(await security.decryptSecret(original.binding_sealed)), createdAt: new Date(start).toISOString(), deadline: until } + const bindingProof = await security.privateSecretFingerprint(security.fingerprint(binding), "routing-reservation-binding-v1") + const state = { revision: original.revision + 1, forward: "spent", inverse: "spent", outcome: { status: "failed", configurationProof: "c".repeat(64), networkProof: "d".repeat(64) } } + const stateProof = await security.privateSecretFingerprint(security.fingerprint({ bindingProof, state }), "routing-reservation-state-v1") + const packet = { phase: "reserved-attempt", reservationId, bindingProof, stateProof } + const intent = { revision: 1, phase: "RECOVERY_INTENT", mode: "reserved", reservationId, networkProof: state.outcome.networkProof, + sourceOperationId: binding.operationId, sourceStepIndex: 0, sourceReceiptId: binding.sourceReceiptId, sourceProof: binding.sourceEvidenceProof, + configurationProof: state.outcome.configurationProof, operationId: binding.operationId, stepIndex: 0, receiptId: binding.inverseReceiptId, + requestSeal: "e".repeat(64), connectionId: binding.principal.connectionId, ownerId: binding.ownerId, validUntil: until, createdAt: binding.createdAt } + const config = { ...intent, phase: "RECOVERY_CONFIG_APPLIED", serverId: ids[0], nativeId: "*1", candidateId: "a", + originalRule: "synthetic ownSQL historical receipt", targetProof: "a".repeat(64), dependencyProof: "b".repeat(64), + sourceEpochs: [{ serverId: ids[0], proof: "f".repeat(64) }], pathCheckId: null, observedAt: new Date(start + 100).toISOString() } + const verified = { ...config, phase: "RECOVERY_VERIFIED", pathCheckId: randomUUID(), observedAt: new Date(start + 200).toISOString() } + const mac = (value: unknown, kind: string) => security.privateSecretFingerprint(security.fingerprint(value), "route-ai-scoped-recovery-" + kind + "-v1") + await pool.query("UPDATE mcp_routing_reservations SET binding_sealed=$2,binding_proof=$3,state_sealed=$4,state_proof=$5,revision=$6,deadline=$7,created_at=$8 WHERE id=$1", [reservationId, await security.encryptSecret(JSON.stringify(binding)), bindingProof, await security.encryptSecret(JSON.stringify(state)), stateProof, state.revision, until, binding.createdAt]) + await pool.query("UPDATE mcp_routing_reservation_slots SET state='spent',created_at=$2 WHERE reservation_id=$1", [reservationId, binding.createdAt]) + await pool.query(`UPDATE mcp_scoped_recovery_intents SET intent_sealed=$2,intent_proof=$3,state='verified',slot_phase='reserved-attempt',source_configuration_proof=$4,deadline=$5,created_at=$6, + inverse_intent_sealed=$7,inverse_intent_proof=$8,inverse_configuration_sealed=$9,inverse_configuration_proof=$10,verified_sealed=$11,verified_proof=$12 WHERE reservation_id=$1`, + [reservationId, await security.encryptSecret(JSON.stringify(packet)), await security.privateSecretFingerprint(security.fingerprint(packet), "routing-reservation-shared-slot-v1"), state.outcome.configurationProof, until, binding.createdAt, + await security.encryptSecret(JSON.stringify(intent)), await mac(intent, "intent"), await security.encryptSecret(JSON.stringify(config)), await mac(config, "configuration"), await security.encryptSecret(JSON.stringify(verified)), await mac(verified, "verified")]) + assert(Date.parse(until) < Date.now()) + await replaceFixtureAnchor(reservationId, bindingProof, binding.createdAt) + assert.equal((await charged()).reservedCharges, 2) + await pool.query("UPDATE mcp_scoped_recovery_intents SET verified_proof=$2 WHERE reservation_id=$1", [reservationId, "0".repeat(64)]) + await assert.rejects(charged) + } finally { await restore() } + // RR sees the original coherent catalog despite a real concurrent COMMIT + // moving its raw parent after profile snapshot establishment. + const rr = await fixture(), rrSource = await rr.makeSource(), rrAllocation = await store.reserve(rrSource.operationId) + let committed = false + const rrStore = createRoutingReservationStore(async () => { + const db = await pool.connect(), query = db.query.bind(db) + return { query: async (...args: unknown[]) => { + const result = await (query as (...values: unknown[]) => Promise)(...args) + if (String(args[0]).includes("clock_timestamp() AS observed_at FROM mcp_routing_profiles")) { + await pool.query("UPDATE mcp_routing_reservations SET profile_id=$2 WHERE id=$1", [rrAllocation.reservationId, foreign.profileId]); committed = true + } + return result + }, release: (destroy?: boolean) => db.release(destroy) } as unknown as PoolClient + }) + try { + assert.equal((await rrStore.readHourlyCharges(rr.profileId, rr.principal)).reservedCharges, 2) + assert.equal(committed, true) + await assert.rejects(store.readHourlyCharges(rr.profileId, rr.principal)) + } finally { await pool.query("UPDATE mcp_routing_reservations SET profile_id=$2 WHERE id=$1", [rrAllocation.reservationId, rr.profileId]) } + // Historical boundary uses real stored timestamps/signed binding. Only + // rows older than the complete hour window (all references) disappear. + try { + const old = new Date(Date.now() - 3601000).toISOString(), binding = { ...JSON.parse(await security.decryptSecret(original.binding_sealed)), createdAt: old } + const bindingProof = await security.privateSecretFingerprint(security.fingerprint(binding), "routing-reservation-binding-v1") + const state = JSON.parse(await security.decryptSecret(original.state_sealed)) + const stateProof = await security.privateSecretFingerprint(security.fingerprint({ bindingProof, state }), "routing-reservation-state-v1") + const packet = { phase: "reservation-held", reservationId, bindingProof, stateProof } + await pool.query("UPDATE mcp_routing_reservations SET binding_sealed=$2,binding_proof=$3,state_proof=$4,created_at=$5 WHERE id=$1", [reservationId, await security.encryptSecret(JSON.stringify(binding)), bindingProof, stateProof, old]) + await pool.query("UPDATE mcp_routing_reservation_slots SET created_at=$2 WHERE reservation_id=$1", [reservationId, old]) + await pool.query("UPDATE mcp_scoped_recovery_intents SET intent_sealed=$2,intent_proof=$3,created_at=$4 WHERE reservation_id=$1", [reservationId, await security.encryptSecret(JSON.stringify(packet)), await security.privateSecretFingerprint(security.fingerprint(packet), "routing-reservation-shared-slot-v1"), old]) + await replaceFixtureAnchor(reservationId, bindingProof, old) + assert.equal((await charged()).reservedCharges, 0) + assert.equal((await charged()).legacyCharges, 2) + } finally { await restore() } + const beforeRead = (await pool.query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1", [source.operationId])).rows[0].n + await charged() + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1", [source.operationId])).rows[0].n, beforeRead) + assert.equal(await used(f.connectionId), 2, "Read-only projection never allocates/debits/refunds") + await assert.rejects(store.readHourlyCharges(randomUUID(), historicalPrincipal)) + // Actual access change after snapshot COMMIT; proxy observes actual SQL, + // never substitutes a row, signer, authority or crypto result. + const lateStore = createRoutingReservationStore(async () => { + const db = await pool.connect(), query = db.query.bind(db) + return { query: async (...args: unknown[]) => { + const result = await (query as (...values: unknown[]) => Promise)(...args) + if (args[0] === "COMMIT") await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [newerId]) + return result + }, release: (destroy?: boolean) => db.release(destroy) } as unknown as PoolClient + }) + try { await assert.rejects(lateStore.readHourlyCharges(f.profileId, historicalPrincipal)) } + finally { await pool.query("UPDATE mcp_credentials SET revoked_at=NULL WHERE id=$1", [newerId]) } + const files = await import("node:fs/promises"), paths = await import("node:path") + const keyPath = paths.join(security.mcpStateDirectory(), "encryption-key"), heldKey = keyPath + ".owned-hourly-" + randomUUID() + let keyHeld = false + const missingKeyStore = createRoutingReservationStore(async () => { + const db = await pool.connect(), query = db.query.bind(db) + return { query: async (...args: unknown[]) => { + const result = await (query as (...values: unknown[]) => Promise)(...args) + if (args[0] === "COMMIT") { await files.rename(keyPath, heldKey); keyHeld = true } + return result + }, release: (destroy?: boolean) => db.release(destroy) } as unknown as PoolClient + }) + try { + await assert.rejects(missingKeyStore.readHourlyCharges(f.profileId, historicalPrincipal)) + await assert.rejects(files.access(keyPath), "Historical read must not bootstrap missing encryption key") + } finally { + if (keyHeld) { await files.rm(keyPath, { force: true }); await files.rename(heldKey, keyPath) } + } + const deadlineStore = createRoutingReservationStore(async () => { + const db = await pool.connect(), query = db.query.bind(db) + return { query: async (...args: unknown[]) => { + const result = await (query as (...values: unknown[]) => Promise)(...args) + if (args[0] === "COMMIT") await new Promise(done => setTimeout(done, 10050)) + return result + }, release: (destroy?: boolean) => db.release(destroy) } as unknown as PoolClient + }) + await assert.rejects(deadlineStore.readHourlyCharges(f.profileId, historicalPrincipal), /Hourly ledger read expired/) + names.push("hourly positive historical RECOVERY_VERIFIED production-crypto fixture expired but charged; tampered verified proof refused", + "hourly real parallel COMMIT cannot tear repeatable-read integrity snapshot", + "hourly old complete history window excluded; future drift and actual 10s publication expiry refused") + names.push("hourly authenticated legacy overlap/expired grant/revoked original credential remains charged", "hourly MAC/slot/shared/orphan/size tamper whole refusal", "hourly all charged states and original source inverse slot; no debit or native receipt") + names.push("concurrent exact-fit single reservation/shared source-step slot", "new-credential read never allocates") + } + { + const f = await fixture(2, 2), source = await f.makeSource() + await pool.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'mutations',date_trunc('hour',now()),1)", [f.connectionId]) + await assert.rejects(store.reserve(source.operationId), /mutation budget/) + assert.equal(await used(f.connectionId), 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservations WHERE source_operation_id=$1", [source.operationId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [source.operationId])).rows[0].n, 0) + names.push("short by one connection mutation rolls back all allocations") + } + { + const f = await fixture(10, 3), first = await f.makeSource(), second = await f.makeSource() + await store.reserve(first.operationId); await assert.rejects(store.reserve(second.operationId), /profile switch slots/) + assert.equal(await used(f.connectionId), 2) + names.push("short by one rolling-hour profile slot across source operations") + } + { + const f = await fixture(10, 3), source = await f.makeSource() + await pool.query("UPDATE mcp_routing_profiles SET state=jsonb_build_object('switches',jsonb_build_array(to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"'),to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"'))) WHERE id=$1", [f.profileId]) + await assert.rejects(store.reserve(source.operationId), /profile switch slots/) + assert.equal(await used(f.connectionId), 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservations WHERE source_operation_id=$1", [source.operationId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservation_slots WHERE profile_id=$1", [f.profileId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [source.operationId])).rows[0].n, 0) + names.push("legacy recent two plus held two exceeds max three without debit") + } + { + const f = await fixture(), source = await f.makeSource() + for (const history of [{ switches: ["invalid"] }, { switches: [new Date(Date.now() + 3600000).toISOString()] }, { switches: Array(2049).fill(new Date().toISOString()) }, { unrelated: "x".repeat(131073) }]) { + await pool.query("UPDATE mcp_routing_profiles SET state=$2::jsonb WHERE id=$1", [f.profileId, JSON.stringify(history)]) + if ("unrelated" in history) { + await assert.rejects(store.reserve(source.operationId), (error: unknown) => error instanceof security.McpError && error.code === "STATE_CONFLICT" && error.statusCode === 409 && error.message === "Постоянный допуск профиля маршрутизации недоступен или изменён") + } else await assert.rejects(store.reserve(source.operationId), /legacy profile switch history/i) + assert.equal(await used(f.connectionId), 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservation_slots WHERE profile_id=$1", [f.profileId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservations WHERE source_operation_id=$1", [source.operationId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [source.operationId])).rows[0].n, 0) + } + names.push("malformed future oversized legacy histories refuse with zero allocations") + } + { + const f = await fixture(), source = await f.makeSource(); let injected = false + const lateStore = createRoutingReservationStore(async () => { + const client = await pool.connect(), original = client.query.bind(client) + return { query: async (...args: unknown[]) => { + if (!injected && typeof args[0] === "string" && args[0].includes("SELECT id FROM mcp_routing_profiles")) { injected = true; await pool.query("UPDATE mcp_routing_profiles SET version=version+1 WHERE id=$1", [f.profileId]) } + return (original as (...values: unknown[]) => Promise)(...args) + }, release: (destroy: boolean) => client.release(destroy) } as unknown as PoolClient + }) + await assert.rejects(lateStore.reserve(source.operationId)); assert.equal(injected, true); assert.equal(await used(f.connectionId), 0) + names.push("late persistent authority edit before admission leaves zero charge") + } + { + const f = await fixture(), source = await f.makeSource(), destroyed: boolean[] = []; let commitSent = false + const uncertain = createRoutingReservationStore(async () => { + const client = await pool.connect(), original = client.query.bind(client) + return { query: async (...args: unknown[]) => { + assert(!(commitSent && args[0] === "ROLLBACK"), "No rollback after uncertain COMMIT") + const result = await (original as (...values: unknown[]) => Promise)(...args) + if (args[0] === "COMMIT") { commitSent = true; throw Error("Owned synthetic lost COMMIT ACK") } + return result + }, release: (destroy: boolean) => { destroyed.push(destroy); client.release(destroy) } } as unknown as PoolClient + }) + await assert.rejects(uncertain.reserve(source.operationId), RoutingReservationCommitUnknown) + assert.deepEqual(destroyed, [true]); assert.equal(await used(f.connectionId), 2) + const id = (await pool.query("SELECT id FROM mcp_routing_reservations WHERE source_operation_id=$1", [source.operationId])).rows[0].id + assert.equal((await store.read(id, f.principal)).state.forward, "held") + const restarted = createRoutingReservationStore() + assert.equal((await restarted.reserve(source.operationId)).forwardAllocation, undefined) + await assert.rejects(restarted.beginForwardAttempt(JSON.parse(JSON.stringify(id))), /capability/) + await assert.rejects(restarted.beginForwardAttempt(JSON.parse('{"kind":"routing-reserved-forward-allocation"}')), /capability/) + assert.equal((await store.read(id, f.principal)).state.forward, "held") + assert.equal(await used(f.connectionId), 2) + names.push("lost COMMIT ACK destroys owner; no forward capability after read or repeated reserve") + } + { + const f = await fixture(), source = await f.makeSource(), reservation = await store.reserve(source.operationId) + await pool.query("UPDATE mcp_operations SET state='running' WHERE id=$1", [source.operationId]) + await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", [source.operationId, JSON.stringify(security.redact({ kind: "service", service: "routing.community.scoped", input: source.input }))]) + assert(reservation.forwardAllocation) + await assert.rejects(assertAcknowledgedRoutingForwardAttempt(reservation.forwardAllocation, source.operationId, f.principal), /No acknowledged/) + await store.beginForwardAttempt(reservation.forwardAllocation) + assert.equal(await assertAcknowledgedRoutingForwardAttempt(reservation.forwardAllocation, source.operationId, f.principal), Date.parse(reservation.deadline)) + await assert.rejects(assertAcknowledgedRoutingForwardAttempt(JSON.parse(JSON.stringify(reservation.forwardAllocation)), source.operationId, f.principal), /No acknowledged/) + await assert.rejects(assertAcknowledgedRoutingForwardAttempt(reservation.forwardAllocation, randomUUID(), f.principal), /attempt changed/) + await assert.rejects(store.beginForwardAttempt(reservation.forwardAllocation), /capability/) + await assert.rejects(store.recordForwardOutcome(reservation.reservationId), /CONFIG_APPLIED/) + await assert.rejects(store.proveUnusedInverse(reservation.reservationId), /terminal applied/) + assert.equal((await store.read(reservation.reservationId, f.principal)).state.outcome, undefined) + assert.equal((await store.read(reservation.reservationId, f.principal)).state.inverse, "held") + await store.markUnknown(reservation.reservationId, "forward") + await assert.rejects(assertAcknowledgedRoutingForwardAttempt(reservation.forwardAllocation, source.operationId, f.principal), /attempt changed/) + assert.equal((await store.read(reservation.reservationId, f.principal)).state.forward, "unknown") + assert.equal(await used(f.connectionId), 2) + await assert.rejects(store.beginForwardAttempt(reservation.forwardAllocation), /capability/) + names.push("spent forward/no genuine checkpoint and preterminal unused-proof refuse; unknown never replays or refunds") + } + if (options.runtimeAdmission) { + const f = await fixture(), source = await f.makeSource(), reservation = await store.reserve(source.operationId) + assert(reservation.forwardAllocation) + await pool.query("UPDATE mcp_operations SET state='running' WHERE id=$1", [source.operationId]) + const changedIntent = security.redact({ kind: "service", service: "routing.community.scoped", input: { ...source.input, receiptId: randomUUID() } }) + await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", + [source.operationId, JSON.stringify(changedIntent)]) + await assert.rejects(store.beginForwardAttempt(reservation.forwardAllocation), /No exact running reserved intent/) + assert.deepEqual((await store.read(reservation.reservationId, f.principal)).state, reservation.state) + assert.equal(await used(f.connectionId), 2) + names.push("running operation with changed generic intent refuses reserved spend without changing held slots or charge") + } + { + const f = await fixture(), source = await f.makeSource(), reservation = await store.reserve(source.operationId) + const { loadScopedNetworkCheckpoint, saveScopedConfigurationCheckpoint, saveScopedNetworkCheckpoint } = await import("./routing-community-scoped-checkpoint.js") + const { unknownOriginalEffects } = await import("./routing-original-effects.js") + await pool.query("UPDATE mcp_operations SET state='running' WHERE id=$1", [source.operationId]) + const step = { kind: "service", service: "routing.community.scoped", input: source.input } + await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", + [source.operationId, JSON.stringify(security.redact(step))]) + await pool.query("INSERT INTO mcp_operation_checkpoints(operation_id,step_index,before_sealed) VALUES($1,0,$2)", + [source.operationId, await security.encryptSecret(JSON.stringify(source.input.beforeRules))]) + assert(reservation.forwardAllocation) + await store.beginForwardAttempt(reservation.forwardAllocation) + // Use the actual checkpoint writers for production crypto and SQL, but + // deliberately seed a synthetic failed-forward observation. No PATCH + // or genuine network observation is part of this owned component case. + const requestSeal = String(source.input.requestProof) + const configuration = await saveScopedConfigurationCheckpoint(source.input, requestSeal) + const effects = unknownOriginalEffects("authenticated-log-interval-coverage-unsupported") + await assert.rejects(saveScopedNetworkCheckpoint(source.input, requestSeal, { id: randomUUID(), candidateId: "b", + status: "failed", observedAt: configuration.observedAt }, "0".repeat(64), effects), { code: "STATE_CONFLICT" }) + const editor = await pool.connect() + const check = { id: randomUUID(), candidateId: "b", status: "failed" as const, observedAt: configuration.observedAt } + let publication: Promise | undefined + try { + await editor.query("BEGIN") + await editor.query("UPDATE mcp_operation_checkpoints SET configuration_proof=$2 WHERE operation_id=$1", + [source.operationId, "0".repeat(64)]) + publication = saveScopedNetworkCheckpoint(source.input, requestSeal, check, configuration.checkpointProof, effects) + void publication.catch(() => {}) + const { setTimeout: delay } = await import("node:timers/promises") + const { rows: [{ pid }] } = await editor.query("SELECT pg_backend_pid() AS pid") + let waiting = false + for (let attempt = 0; attempt < 100 && !waiting; attempt++) { + const result = await pool.query("SELECT 1 FROM pg_stat_activity WHERE $1 = ANY(pg_blocking_pids(pid)) AND query LIKE '%FOR NO KEY UPDATE OF c'", + [pid]) + waiting = Boolean(result.rowCount) + if (!waiting) await delay(15) + } + assert(waiting, "Network checkpoint publication must wait for the configuration row") + await editor.query("COMMIT") + await assert.rejects(publication, { code: "STATE_CONFLICT" }) + } finally { + editor.release(true) + await publication?.catch(() => {}) + } + assert.equal((await pool.query("SELECT 1 FROM mcp_audit WHERE action='routing.scoped.network-check' AND payload->>'receiptId'=$1", + [source.input.receiptId])).rowCount, 0) + await pool.query("UPDATE mcp_operation_checkpoints SET configuration_proof=$2 WHERE operation_id=$1", + [source.operationId, configuration.checkpointProof]) + const publish = () => saveScopedNetworkCheckpoint(source.input, requestSeal, check, configuration.checkpointProof, effects) + await Promise.all([publish(), publish()]) + await assert.rejects(saveScopedNetworkCheckpoint(source.input, requestSeal, { ...check, status: "passed" }, + configuration.checkpointProof, effects), { code: "STATE_CONFLICT" }) + assert.equal((await pool.query("SELECT 1 FROM mcp_audit WHERE action='routing.scoped.network-check' AND payload->>'receiptId'=$1", + [source.input.receiptId])).rowCount, 1) + const network = await loadScopedNetworkCheckpoint(source.input, requestSeal) + assert.equal(network?.status, "failed") + assert.deepEqual(network?.originalEffects, effects) + const before = await store.read(reservation.reservationId, f.principal) + assert.equal(before.state.forward, "spent") + assert.equal(before.state.inverse, "held") + assert.equal(before.state.outcome, undefined) + assert.equal(before.chargedMutationUnits, 2) + assert.equal(before.chargedProfileSlots, 2) + assert.equal(before.refundsAvailable, false) + const sharedSlot = async () => (await pool.query(`SELECT state,slot_phase,inverse_intent_sealed,inverse_configuration_sealed,verified_sealed + FROM mcp_scoped_recovery_intents WHERE reservation_id=$1`, [reservation.reservationId])).rows + const shared = await sharedSlot() + assert.deepEqual(shared, [{ state: "intent", slot_phase: "reservation-held", inverse_intent_sealed: null, + inverse_configuration_sealed: null, verified_sealed: null }]) + for (let attempt = 0; attempt < 2; attempt++) { + await assert.rejects(store.recordForwardOutcome(reservation.reservationId), /Authenticated network evidence for automatic recovery unavailable/) + assert.deepEqual((await store.read(reservation.reservationId, f.principal)).state, before.state) + assert.deepEqual(await sharedSlot(), shared) + } + await assert.rejects(store.proveUnusedInverse(reservation.reservationId), /terminal applied/) + assert.equal(await used(f.connectionId), 2) + assert.equal((await store.readHourlyCharges(f.profileId, f.principal)).reservedCharges, 2) + names.push("signed synthetic failed-control checkpoint with REST-only unknown log coverage cannot create an inverse allocation or outcome; original shared slot and both charges stay held without refund") + } + { + const f = await fixture(), source = await f.makeSource(), reservation = await store.reserve(source.operationId) + const saved = (await pool.query("SELECT state_proof FROM mcp_routing_reservations WHERE id=$1", [reservation.reservationId])).rows[0].state_proof + await pool.query("UPDATE mcp_routing_reservations SET state_proof=$2 WHERE id=$1", [reservation.reservationId, "f".repeat(64)]) + await assert.rejects(store.read(reservation.reservationId, f.principal), /tampering/) + await pool.query("UPDATE mcp_routing_reservations SET state_proof=$2 WHERE id=$1", [reservation.reservationId, saved]) + await pool.query("UPDATE mcp_scoped_recovery_intents SET state='unknown' WHERE source_operation_id=$1", [source.operationId]) + assert.equal((await store.read(reservation.reservationId, f.principal)).sharedSlotQuarantined, true) + assert(reservation.forwardAllocation) + await assert.rejects(store.beginForwardAttempt(reservation.forwardAllocation), /quarantined/) + names.push("signed ledger tamper and external shared-slot quarantine fail closed") + } + if (options.runtimeAdmission) { + const { applyPlan, loadPlan } = await import("./runtime.js") + const { withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js") + const { lockRoutingReservationAdmissionSource } = await import("./routing-reservation-runtime.js") + const emptyAllocations = async (f: Awaited>, planId: string) => { + assert.equal(await used(f.connectionId), 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE plan_id=$1", [planId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservations WHERE profile_id=$1", [f.profileId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservation_slots WHERE profile_id=$1", [f.profileId])).rows[0].n, 0) + } + { + const f = await fixture(2, 2), source = await f.makeSource(false), key = randomUUID() + await withSystemDatabaseOperationLock(() => pool.connect(), async () => { + await assert.rejects(applyPlan(f.principal, source.planId, key), { code: "PROTOCOL_STORAGE_BUSY" }) + await emptyAllocations(f, source.planId) + }) + const operation = await applyPlan(f.principal, source.planId, key) + await new Promise(resolve => setTimeout(resolve, 30)) + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0].state, "pending", "Reserved native dispatch remains gated") + assert.equal(await used(f.connectionId), 2, "Runtime must not double-charge the store debit") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservation_slots WHERE profile_id=$1", [f.profileId])).rows[0].n, 2) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [operation.id])).rows[0].n, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].n, 0) + assert.equal((await applyPlan(f.principal, source.planId, key)).id, operation.id) + assert.equal(await used(f.connectionId), 2) + assert.equal((await store.readHourlyCharges(f.profileId, f.principal)).reservedCharges, 2) + const auditCount = await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action='routing.charge.admitted'", [f.connectionId]) + assert.equal(auditCount.rows[0].n, 1, "Idempotent replay retains one audit witness for both slots") + const owner = await pool.connect() + try { + await owner.query("BEGIN") + // An owned disposable database only. Simulate erasure outside the ordinary trigger protections. + for (const table of ["mcp_scoped_recovery_intents", "mcp_routing_reservation_slots", "mcp_routing_reservations"]) + await owner.query(`ALTER TABLE ${table} DISABLE TRIGGER USER`) + await owner.query("DELETE FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1", [operation.id]) + await owner.query("DELETE FROM mcp_routing_reservation_slots WHERE profile_id=$1", [f.profileId]) + await owner.query("DELETE FROM mcp_routing_reservations WHERE source_operation_id=$1", [operation.id]) + const current = await (await import("./store.js")).effectiveMcpConnection(f.principal, owner) + await assert.rejects(readHistoricalChargesWithinOwner(owner, f.profileId, current, undefined, Date.now() + 10000), /не совпал с журналом допуска/) + } finally { + try { await owner.query("ROLLBACK") } finally { owner.release() } + } + assert.equal((await store.readHourlyCharges(f.profileId, f.principal)).reservedCharges, 2) + names.push("surviving signed audit refuses an erased reservation family and rollback restores both original slots") + names.push("actual reserved apply refuses during database maintenance without allocating, then records one two-unit hold and returns the same ID on replay without dispatch") + } + for (const scenario of ["short-budget", "cancelled", "authority-disabled"] as const) { + const f = await fixture(scenario === "short-budget" ? 1 : 10), source = await f.makeSource(false) + if (scenario === "cancelled") await pool.query("UPDATE mcp_plans SET state='cancelled' WHERE id=$1", [source.planId]) + if (scenario === "authority-disabled") await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=$1", [f.profileId]) + await assert.rejects(applyPlan(f.principal, source.planId, randomUUID())) + await emptyAllocations(f, source.planId) + names.push("actual applyPlan " + scenario + " refuses before operation/budget/profile/shared allocations") + } + { + const f = await fixture(2, 2), source = await f.makeSource(false), released: boolean[] = [], statements: string[] = [] + const connect = async () => { + const client = await pool.connect(), original = client.query.bind(client) + let commitSent = false + return { query: async (...args: unknown[]) => { + const sql = String(args[0]); statements.push(sql) + assert(!(commitSent && sql === "ROLLBACK"), "Unknown outer COMMIT must never roll back") + const result = await (original as (...values: unknown[]) => Promise)(...args) + if (sql === "COMMIT") { commitSent = true; throw Error("Owned outer admission COMMIT ACK lost") } + return result + }, release: (destroy: boolean) => { released.push(destroy); client.release(destroy) } } as unknown as PoolClient + } + await assert.rejects(withRoutingReservationAdmission(async (db, reserve) => { + await db.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [f.connectionId]) + await lockRoutingReservationAdmissionSource(db, await loadPlan(source.planId, db), f.principal) + await db.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'pending')", [source.operationId, source.planId, f.connectionId, randomUUID()]) + await db.query("UPDATE mcp_plans SET state='executing' WHERE id=$1", [source.planId]) + await reserve(source.operationId) + }, connect), RoutingReservationCommitUnknown) + assert.deepEqual(released, [true]); assert.equal(statements.filter(sql => sql === "BEGIN").length, 1); assert.equal(statements.filter(sql => sql === "COMMIT").length, 1) + assert.equal(statements.filter(sql => sql.startsWith("INSERT INTO mcp_budget_buckets")).length, 1) + assert.equal(await used(f.connectionId), 2) + const restarted = createRoutingReservationStore(), repeated = await restarted.reserve(source.operationId) + assert.equal(repeated.forwardAllocation, undefined) + assert.equal((await restarted.read(repeated.reservationId, f.principal)).state.forward, "held") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1", [source.operationId])).rows[0].n, 0) + names.push("owned outer admission lost actual COMMIT ACK retains atomic operation/hold and emits no forward capability") + } + } + // Same-owner admission consumes the authenticated OBSERVED historical rows, + // not public read access or a second client/transaction. No full-catalog + // deletion anchor, new writer registration or native recovery is claimed. + { + const f = await fixture(10, 4), first = await f.makeSource(), old = await store.reserve(first.operationId), next = await f.makeSource() + const row = (await pool.query("SELECT * FROM mcp_routing_reservations WHERE id=$1", [old.reservationId])).rows[0] + const inverse = (await pool.query("SELECT * FROM mcp_routing_reservation_slots WHERE reservation_id=$1 AND lane='inverse'", [old.reservationId])).rows[0] + const snapshot = async () => ({ + used: await used(f.connectionId), + allocations: security.fingerprint((await pool.query("SELECT COALESCE(jsonb_agg(to_jsonb(r) ORDER BY r.id),'[]'::jsonb) AS rows FROM mcp_routing_reservations r WHERE connection_id=$1", [f.connectionId])).rows[0].rows), + slots: security.fingerprint((await pool.query("SELECT COALESCE(jsonb_agg(to_jsonb(s) ORDER BY s.reservation_id,s.lane),'[]'::jsonb) AS rows FROM mcp_routing_reservation_slots s WHERE profile_id=$1", [f.profileId])).rows[0].rows), + shared: security.fingerprint((await pool.query("SELECT COALESCE(jsonb_agg(to_jsonb(s) ORDER BY s.source_operation_id,s.source_step_index),'[]'::jsonb) AS rows FROM mcp_scoped_recovery_intents s WHERE authority_profile_id=$1", [f.profileId])).rows[0].rows), + }) + const denials: { before: Awaited>; after: Awaited>; unchanged: true }[] = [] + const unchangedRefusal = async (operationId: string, pattern?: RegExp) => { + const before = await snapshot() + if (pattern) await assert.rejects(store.reserve(operationId), pattern) + else await assert.rejects(store.reserve(operationId)) + const after = await snapshot() + assert.deepEqual(after, before, "Denied admission must preserve full allocation/slot/shared rows and mutation debit") + denials.push({ before, after, unchanged: true }) + } + await pool.query("UPDATE mcp_routing_reservations SET binding_proof=$2 WHERE id=$1", [old.reservationId, "0".repeat(64)]) + try { await unchangedRefusal(next.operationId, /tampering/) } + finally { await pool.query("UPDATE mcp_routing_reservations SET binding_proof=$2 WHERE id=$1", [old.reservationId, row.binding_proof]) } + await pool.query("DELETE FROM mcp_routing_reservation_slots WHERE reservation_id=$1 AND lane='inverse'", [old.reservationId]) + try { await unchangedRefusal(next.operationId, /allocation ledger/) } + finally { await pool.query("INSERT INTO mcp_routing_reservation_slots(reservation_id,profile_id,lane,state,created_at) VALUES($1,$2,$3,$4,$5)", [inverse.reservation_id, inverse.profile_id, inverse.lane, inverse.state, inverse.created_at]) } + const shared = (await pool.query("SELECT intent_proof FROM mcp_scoped_recovery_intents WHERE reservation_id=$1", [old.reservationId])).rows[0] + await pool.query("UPDATE mcp_scoped_recovery_intents SET intent_proof=$2 WHERE reservation_id=$1", [old.reservationId, "0".repeat(64)]) + try { await unchangedRefusal(next.operationId, /allocation proof/) } + finally { await pool.query("UPDATE mcp_scoped_recovery_intents SET intent_proof=$2 WHERE reservation_id=$1", [old.reservationId, shared.intent_proof]) } + await pool.query("UPDATE mcp_routing_profiles SET state=$2::jsonb WHERE id=$1", [f.profileId, JSON.stringify({ switches: [row.created_at.toISOString()] })]) + try { await unchangedRefusal(next.operationId, /profile switch slots/) } + finally { await pool.query("UPDATE mcp_routing_profiles SET state='{}'::jsonb WHERE id=$1", [f.profileId]) } + // A signed historical expired deadline remains charged. This does not + // grant execution and never calls live() for historical allocations. + const binding = { ...JSON.parse(await security.decryptSecret(row.binding_sealed)), deadline: new Date(Date.now() - 1000).toISOString() } + const bindingProof = await security.privateSecretFingerprint(security.fingerprint(binding), "routing-reservation-binding-v1") + const state = JSON.parse(await security.decryptSecret(row.state_sealed)) + const stateProof = await security.privateSecretFingerprint(security.fingerprint({ bindingProof, state }), "routing-reservation-state-v1") + const packet = { phase: "reservation-held", reservationId: old.reservationId, bindingProof, stateProof } + await pool.query("UPDATE mcp_routing_reservations SET binding_sealed=$2,binding_proof=$3,state_proof=$4,deadline=$5 WHERE id=$1", [old.reservationId, await security.encryptSecret(JSON.stringify(binding)), bindingProof, stateProof, binding.deadline]) + await pool.query("UPDATE mcp_scoped_recovery_intents SET intent_sealed=$2,intent_proof=$3,deadline=$4 WHERE reservation_id=$1", [old.reservationId, await security.encryptSecret(JSON.stringify(packet)), await security.privateSecretFingerprint(security.fingerprint(packet), "routing-reservation-shared-slot-v1"), binding.deadline]) + await replaceFixtureAnchor(old.reservationId, bindingProof, binding.createdAt) + // Renew actual current profile/credential authority. Old credential and + // original grant/version do not reset the charge of the old allocation. + const newCredential = randomUUID(), expiry = new Date(Date.now() + 540000).toISOString() + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [newCredential, f.connectionId, randomUUID(), expiry]) + await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [f.credentialId]) + await pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=now()-interval '1 second',enabled=false WHERE id=$1", [f.profileId]) + await pool.query("UPDATE mcp_routing_profiles SET grant_expires_at=$2,credential_id=$3,version=version+1,enabled=true WHERE id=$1", [f.profileId, expiry, newCredential]) + const currentProfile = (await pool.query("SELECT config,version FROM mcp_routing_profiles WHERE id=$1", [f.profileId])).rows[0] + const owner = (await pool.query("SELECT owner_id FROM mcp_connections WHERE id=$1", [f.connectionId])).rows[0].owner_id + const principal = { ...f.principal, credentialId: newCredential, credentialExpiresAt: expiry } + const authority = await resolveRoutingProfileAuthority({ profileId: f.profileId, expectedVersion: currentProfile.version, profile: routeAiProfileSchema.parse(currentProfile.config), principal, ownerId: owner, transport: { contextSources } }) + const plan = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [next.planId])).rows[0] + const evidence = JSON.parse(await security.decryptSecret(plan.evidence.sealed)) + const { requestProof: _oldRequest, ...input } = evidence.steps[0].input + Object.assign(input, { principal, persistentProfileAuthority: authority }) + input.requestProof = await security.privateSecretFingerprint(security.fingerprint(input), "route-ai-scoped-request-v3") + evidence.steps[0].input = input + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [next.planId, JSON.stringify({ sealed: await security.encryptSecret(JSON.stringify(evidence)) })]) + const statements: string[] = [], pids = new Set() + const traced = createRoutingReservationStore(async () => { + const db = await pool.connect(), query = db.query.bind(db); pids.add(Number((await db.query("SELECT pg_backend_pid() AS pid")).rows[0].pid)) + return { query: async (...args: unknown[]) => { statements.push(String(args[0])); return (query as (...values: unknown[]) => Promise)(...args) }, + release: (destroy?: boolean) => db.release(destroy) } as unknown as PoolClient + }) + await traced.reserve(next.operationId) + assert.equal(pids.size, 1) + assert.equal(statements.filter(sql => sql === "BEGIN").length, 1); assert.equal(statements.filter(sql => sql === "COMMIT").length, 1) + const profileLock = statements.findIndex(sql => sql === "SELECT id FROM mcp_routing_profiles WHERE id=$1 FOR UPDATE") + const authenticatedRead = statements.findIndex(sql => sql.includes("AS wanted ORDER BY id LIMIT 12")) + const debit = statements.findIndex(sql => sql.startsWith("INSERT INTO mcp_budget_buckets")) + assert(profileLock >= 0 && authenticatedRead > profileLock && debit > authenticatedRead) + assert.equal(await used(f.connectionId), 4) + assert.equal((await store.readHourlyCharges(f.profileId, principal)).reservedCharges, 4) + const over = await f.makeSource() // Original credential is revoked: still no bypass. + await unchangedRefusal(over.operationId) + assert.equal(await used(f.connectionId), 4) + const traceFiles = await import("node:fs/promises") + await traceFiles.writeFile(resolve(process.env.MCP_STATE_DIR!, "..", "owner-admission-query-trace.json"), JSON.stringify({ + pids: [...pids], statements, profileLock, authenticatedRead, debit, + beginCount: statements.filter(sql => sql === "BEGIN").length, commitCount: statements.filter(sql => sql === "COMMIT").length, + denials, sourceOwnerOnly: true, publicReadLaneUsedForAdmission: false, nativeRequests: 0, + }, null, 2)) + names.push("same-owner admission rejects prior MAC/slot/shared tamper before new debit", "same-owner legacy overlap charged without timestamp dedup", + "same-owner renewed authority and expired historical allocation retain four charges under one transaction/profile lock") + } + } + if (options.commonAdmissionOnly && !options.forwardChargeOnly) { + // Non-reserved persistent forward must join the SAME existing owner lock + // order without acquiring the reserved debit/capability or native rights. + const { lockRoutingReservationAdmissionSource } = await import("./routing-reservation-runtime.js") + const { loadPlan } = await import("./runtime.js") + const f = await fixture(10, 3, false), source = await f.makeSource(false) + await withRoutingReservationAdmission(async db => { + const ownerPid = Number((await db.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + await db.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [f.connectionId]) + const selected = await lockRoutingReservationAdmissionSource(db, await loadPlan(source.planId, db), f.principal) + assert.equal(selected.reserved, false) + const editor = await pool.connect() + try { + assert.notEqual(Number((await editor.query("SELECT pg_backend_pid() AS pid")).rows[0].pid), ownerPid) + await editor.query("SET lock_timeout='100ms'") + await assert.rejects(editor.query("UPDATE mcp_routing_profiles SET version=version+1 WHERE id=$1", [f.profileId]), (error: unknown) => (error as { code?: string }).code === "55P03") + } finally { await editor.query("RESET lock_timeout"); editor.release() } + const other = await fixture(10, 3, false), otherSource = await other.makeSource(false) + await withRoutingReservationAdmission(async separateOwner => { + await separateOwner.query("SET LOCAL lock_timeout='100ms'") + await separateOwner.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [other.connectionId]) + assert.equal((await lockRoutingReservationAdmissionSource(separateOwner, await loadPlan(otherSource.planId, separateOwner), other.principal)).reserved, false) + }) + assert.equal(await used(other.connectionId), 0) + }) + assert.equal(await used(f.connectionId), 0) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservations WHERE profile_id=$1", [f.profileId])).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE plan_id=$1", [source.planId])).rows[0].n), 0) + names.push("non-reserved scoped persistent admission holds existing profile lock against concurrent editor without reserved debit/capability") + names.push("different authenticated profile and connection admit without false profile-lock contention") + await pool.query("UPDATE mcp_routing_profiles SET version=version+1 WHERE id=$1", [f.profileId]) + await assert.rejects(withRoutingReservationAdmission(async db => { + await db.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [f.connectionId]) + await lockRoutingReservationAdmissionSource(db, await loadPlan(source.planId, db), f.principal) + })) + assert.equal(await used(f.connectionId), 0) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservation_slots WHERE profile_id=$1", [f.profileId])).rows[0].n), 0) + names.push("non-reserved scoped admission rejects genuinely changed profile authority with zero allocation/debit") + const renewed = await fixture(10, 4, false), oldSource = await renewed.makeSource(false) + const editor = await pool.connect() + await editor.query("BEGIN"); await editor.query("SET LOCAL lock_timeout='1s'"); await editor.query("SET LOCAL statement_timeout='5s'") + const editorPid = Number((await editor.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + await editor.query("SELECT id FROM mcp_routing_profiles WHERE id=$1 FOR UPDATE", [renewed.profileId]) + const pending = withRoutingReservationAdmission(async db => { + await db.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [renewed.connectionId]) + await lockRoutingReservationAdmissionSource(db, await loadPlan(oldSource.planId, db), renewed.principal) + }) + let pendingError: unknown, raceError: unknown + void pending.catch(() => {}) + try { + let waiting = false + for (let attempt = 0; attempt < 150; attempt++) { + waiting = (await pool.query("SELECT pid FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND $1::int=ANY(pg_blocking_pids(pid)) AND query LIKE 'SELECT id FROM mcp_routing_profiles WHERE id=%FOR UPDATE'", [editorPid])).rowCount === 1 + if (waiting) break + await new Promise(resolve => setTimeout(resolve, 10)) + } + assert(waiting, "The old false-plan must actually wait before current profile renewal") + // Genuine newly signed authority is resolved only AFTER this current + // metadata renewal. False/true are never simultaneously authoritative. + await editor.query("UPDATE mcp_routing_profiles SET config=jsonb_set(config,'{autoRecovery}','true'),version=version+1 WHERE id=$1", [renewed.profileId]) + await editor.query("COMMIT") + } catch (error) { raceError = error; try { await editor.query("ROLLBACK") } catch { /* preserve first error */ } } + finally { editor.release(); try { await pending } catch (error) { pendingError = error } } + if (raceError) throw raceError + assert(pendingError, "Changed actual authority must refuse after the profile lock is acquired") + assert.equal(await used(renewed.connectionId), 0) + await renewed.renew() + const currentSource = await renewed.makeSource(), admitted = await store.reserve(currentSource.operationId) + assert.equal(admitted.created, true); assert(admitted.forwardAllocation) + assert.equal(await used(renewed.connectionId), 2) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_reservation_slots WHERE profile_id=$1", [renewed.profileId])).rows[0].n), 2) + names.push("same-profile stale false-plan refuses after real owner wait and current renewal; newly authenticated auto authority retains original two-slot debit") + } + if (options.forwardChargeOnly) { + console.log(JSON.stringify({stage:"forward-runtime-created"})) + const { applyPlan } = await import("./runtime.js") + const terminal = async (id: string) => { + for (let attempt = 0; attempt < 250; attempt++) { + const value = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (value && !["pending", "running"].includes(value.state)) return value.state as string + await new Promise(resolve => setTimeout(resolve, 20)) + } + throw Error("Owned forward fixture operation did not terminate") + } + const count = async (profileId: string) => Number((await pool.query("SELECT count(*)::int AS n FROM mcp_routing_forward_charges WHERE profile_id=$1", [profileId])).rows[0].n) + const f = await fixture(100, 4, false), source = await f.makeSource(false), key = randomUUID() + const operation = await applyPlan(f.principal, source.planId, key) + await terminal(operation.id) // Synthetic request intentionally has no native SLA witness. + const charge = (await pool.query("SELECT * FROM mcp_routing_forward_charges WHERE source_operation_id=$1", [operation.id])).rows[0] + assert(charge); assert.equal(charge.source_plan_id, source.planId); assert.equal(charge.profile_id, f.profileId) + assert.equal(charge.source_step_index, 0); assert.equal(charge.charge_units, 1) + const signed = JSON.parse(await security.decryptSecretExisting(charge.binding_sealed)) + assert.equal(signed.createdAt, charge.created_at.toISOString(), "Signed charge uses the exact observed SQL time") + assert.equal(await used(f.connectionId), 2); assert.equal(await count(f.profileId), 1) + assert.equal((await applyPlan(f.principal, source.planId, key)).id, operation.id) + assert.equal(await used(f.connectionId), 2); assert.equal(await count(f.profileId), 1) + assert.equal((await store.readHourlyCharges(f.profileId, f.principal)).forwardCharges, 1) + names.push("actual runtime-created non-reserved operation atomically owns one signed SQL-time forward charge; same-key read never recharges/replays") + console.log(JSON.stringify({stage:"forward-renewed-reserved"})) + const legacyAt = (await pool.query("SELECT clock_timestamp() AS at")).rows[0].at.toISOString() + await pool.query("UPDATE mcp_routing_profiles SET config=jsonb_set(config,'{autoRecovery}','true'),version=version+1,state=jsonb_build_object('switches',jsonb_build_array($2::text)) WHERE id=$1", [f.profileId, legacyAt]) + await f.renew() + const autoSource = await f.makeSource(false) + const automatic = await applyPlan(f.principal, autoSource.planId, randomUUID()) + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [automatic.id])).rows[0].state, "pending", "Existing reserved native dispatch stays closed") + const total = await store.readHourlyCharges(f.profileId, f.principal) + assert.equal(total.forwardCharges, 1); assert.equal(total.reservedCharges, 2); assert.equal(total.legacyCharges, 1); assert.equal(total.totalCharges, 4) + assert.equal(await used(f.connectionId), 4) + names.push("current profile renewal keeps historical forward1 plus original reserved2 plus conservative legacy1; no weak timestamp dedup") + console.log(JSON.stringify({stage:"forward-quota-and-guards"})) + const snapshot = async () => (await pool.query("SELECT jsonb_build_object('charges',(SELECT coalesce(jsonb_agg(to_jsonb(c) ORDER BY c.source_operation_id),'[]') FROM mcp_routing_forward_charges c WHERE profile_id=$1),'slots',(SELECT coalesce(jsonb_agg(to_jsonb(s) ORDER BY s.reservation_id,s.lane),'[]') FROM mcp_routing_reservation_slots s WHERE profile_id=$1),'used',(SELECT coalesce(sum(used),0) FROM mcp_budget_buckets WHERE connection_id=$2 AND kind='mutations')) AS value", [f.profileId, f.connectionId])).rows[0].value + const before = security.fingerprint(await snapshot()), deniedSource = await f.makeSource(false) + await assert.rejects(applyPlan(f.principal, deniedSource.planId, randomUUID()), (error: unknown) => error instanceof security.McpError && error.code === "STATE_CONFLICT") + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE plan_id=$1", [deniedSource.planId])).rows[0].n), 0) + assert.equal(security.fingerprint(await snapshot()), before) + for (const sql of ["DELETE FROM mcp_routing_forward_charges WHERE source_operation_id=$1", "UPDATE mcp_routing_forward_charges SET created_at=clock_timestamp() WHERE source_operation_id=$1"]) + await assert.rejects(pool.query(sql, [operation.id]), (error: unknown) => (error as { code?: string }).code === "P0001") + await assert.rejects(pool.query("TRUNCATE mcp_routing_forward_charges"), (error: unknown) => (error as { code?: string }).code === "P0001") + assert.equal(security.fingerprint(await snapshot()), before) + names.push("over-budget runtime admission rolls back operation/debit/all rows; recent deletion/time edits/truncate cannot refund a charge") + console.log(JSON.stringify({stage:"forward-profile-delete"})) + const orphan = await fixture(100, 4, false), orphanSource = await orphan.makeSource(false) + const retained = await applyPlan(orphan.principal, orphanSource.planId, randomUUID()); await terminal(retained.id) + assert.equal(await count(orphan.profileId), 1) + await pool.query("DELETE FROM mcp_routing_profiles WHERE id=$1", [orphan.profileId]) + assert.equal(await count(orphan.profileId), 1, "Profile-family deletion cannot erase retained hourly charge") + names.push("ordinary profile removal has no new lifetime FK blockage and leaves its actual operation charge retained") + console.log(JSON.stringify({stage:"forward-lost-return"})) + const lost = await fixture(100, 4, false), lostSource = await lost.makeSource(false), lostKey = randomUUID() + const observedClient = await pool.connect(), prototype = Object.getPrototypeOf(observedClient) as PoolClient + const originalQuery = prototype.query; observedClient.release() + const chargedOwners = new WeakSet(); let committedReturnLost = false + // Transparent fault after the REAL charge INSERT and REAL COMMIT on its + // exact owner. No invented COMMIT, rows, operation, crypto or authority. + prototype.query = function (this: PoolClient, ...args: unknown[]) { + const sql = typeof args[0] === "string" ? args[0] : String((args[0] as { text?: unknown })?.text) + const result = (originalQuery as (...values: unknown[]) => unknown).apply(this, args) + if (!(result instanceof Promise)) return result + return result.then(value => { + if (sql.startsWith("INSERT INTO mcp_routing_forward_charges") && (args[1] as unknown[] | undefined)?.[2] === lost.profileId) chargedOwners.add(this) + if (sql === "COMMIT" && chargedOwners.has(this)) { chargedOwners.delete(this); committedReturnLost = true; throw Error("Owned actual forward admission COMMIT return lost") } + return value + }) + } as PoolClient["query"] + try { await assert.rejects(applyPlan(lost.principal, lostSource.planId, lostKey), RoutingReservationCommitUnknown) } + finally { prototype.query = originalQuery } + assert(committedReturnLost) + const saved = (await pool.query("SELECT id,state,error_code,verification,result FROM mcp_operations WHERE plan_id=$1", [lostSource.planId])).rows[0] + assert(saved) + console.log(JSON.stringify({stage:"forward-lost-persistent-observation",realCommitReturnLost:committedReturnLost, + state:saved.state,errorCode:saved.error_code,verification:saved.verification,chargeCount:await count(lost.profileId),mutationUsed:await used(lost.connectionId)})) + // The REAL runtime retires its original admission on an uncertain COMMIT. + // Lower-level store-only fixtures have no such owner to quarantine them. + assert.equal(saved.state, "unknown"); assert.equal(saved.error_code, "PROCESS_INTERRUPTED"); assert.equal(saved.verification, "inconclusive") + assert.equal(saved.result.automaticRetry, false); assert.equal(saved.result.needsReview, true) + assert.equal(await count(lost.profileId), 1); assert.equal(await used(lost.connectionId), 2) + assert.equal((await applyPlan(lost.principal, lostSource.planId, lostKey)).id, saved.id) + await new Promise(resolve => setTimeout(resolve, 30)) + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [saved.id])).rows[0].state, "unknown") + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1", [saved.id])).rows[0].n), 0) + assert.equal(await count(lost.profileId), 1); assert.equal(await used(lost.connectionId), 2) + names.push("real forward admission COMMIT plus injected lost application return leaves one charge and no dispatch; same-key observation cannot replay") + } + return { cases: names.length, names, actualSql: true, productionHmac: true, sourcePlans: "synthetic encrypted source and historical recovery receipt fixtures; no native recovery or genuine network checkpoint claimed", + numberedSchemaEnrollment: true, schemaMigration: "0040_mcp_routing_reservations", proposalDdlOnly: false, runtimeRegistered: false, runtimeAdmissionAccepted: !!options.runtimeAdmission, reservedNativeDispatchEnabled: false, + nativeRequests: 0, refundsAvailable: false, fullReservedInverseLifecycleAccepted: false } + } finally { + if (profileIds.length) await pool.query("UPDATE mcp_routing_profiles SET enabled=false WHERE id=ANY($1::uuid[])", [profileIds]) + if (ids.length) await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [ids]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousEnabled]) + } +} diff --git a/backend/src/modules/mcp/routing-reservation.sql.test.ts b/backend/src/modules/mcp/routing-reservation.sql.test.ts new file mode 100644 index 00000000..fa98b44e --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation.sql.test.ts @@ -0,0 +1,10 @@ +import { assertOwnedRoutingReservationSql, acceptRoutingReservationSql } from "./routing-reservation.sql-helper.js" + +// Guard precedes imports that configure SQL or load private MCP state. +assertOwnedRoutingReservationSql() +const { pool, closePool } = await import("../../db/index.js") +try { + const { applySqlMigrations } = await import("../../db/migrate.js") + await applySqlMigrations(pool) + console.log("private routing reservation SQL acceptance:", JSON.stringify(await acceptRoutingReservationSql())) +} finally { await closePool() } diff --git a/backend/src/modules/mcp/routing-reservation.test.ts b/backend/src/modules/mcp/routing-reservation.test.ts new file mode 100644 index 00000000..90cede3f --- /dev/null +++ b/backend/src/modules/mcp/routing-reservation.test.ts @@ -0,0 +1,209 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { randomUUID } from "node:crypto" +import { mkdtemp, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import type { PoolClient } from "pg" +import { countLegacyRoutingSwitches, routingReservationDeadline, transitionRoutingReservation, type RoutingReservationState } from "./routing-reservation-contract.js" +import { bindRoutingChargeAdmission, routingReservationTransaction, RoutingReservationCommitUnknown, stageRoutingChargeWitness } from "./routing-reservation-transaction.js" +import { readRoutingChargeJournal } from "./routing-charge-journal.js" +import { fingerprint, privateSecretFingerprint } from "./security.js" +import { temporaryGrantOriginSql } from "./temporary-grant-origin.js" +import { createRoutingReservationStore, withRoutingReservationAdmission } from "./routing-reservation-store.js" + +test("legacy switch history counts each recent entry and fails closed on malformed/future/unbounded data", () => { + const now = Date.parse("2026-10-25T12:00:00.000Z"), recent = "2026-10-25T11:59:00.000Z" + assert.equal(countLegacyRoutingSwitches({}, now), 0) + assert.equal(countLegacyRoutingSwitches({ switches: [recent, recent, "2026-10-25T11:00:00.000Z"] }, now), 2) + for (const state of [null, [], { switches: null }, { switches: ["invalid"] }, { switches: ["2026-10-25T12:00:00.001Z"] }, { switches: Array(2049).fill(recent) }]) assert.throws(() => countLegacyRoutingSwitches(state, now), /legacy/) +}) + +test("reservation deadline never restarts its original120-second/grant/channel clocks", () => { + const start = 1000000 + assert.equal(routingReservationDeadline(start, start + 600000, start + 500000), start + 120000) + assert.equal(routingReservationDeadline(start, start + 600000, start + 90000), start + 90000) + assert.equal(routingReservationDeadline(start, start + 600000, start + 90000, start + 30000), start + 30000) + assert.equal(routingReservationDeadline(start, start + 10000, start + 90000), start + 10000) + for (const bad of [NaN, Infinity, -Infinity]) { + assert.throws(() => routingReservationDeadline(bad, start, start)) + assert.throws(() => routingReservationDeadline(start, bad, start)) + assert.throws(() => routingReservationDeadline(start, start, bad)) + } +}) + +test("held allocation consumes once; unknown cannot be retried or refunded", () => { + const held: RoutingReservationState = { revision: 1, forward: "held", inverse: "held" } + const spent = transitionRoutingReservation(held, "forward", "attempt") + assert.equal(held.forward, "held"); assert.equal(spent.forward, "spent") + assert.throws(() => transitionRoutingReservation(spent, "forward", "attempt")) + const unknown = transitionRoutingReservation(spent, "forward", "unknown") + assert.equal(unknown.forward, "unknown") + for (const event of ["attempt", "prove-unused", "unknown"] as const) assert.throws(() => transitionRoutingReservation(unknown, "forward", event)) + assert.throws(() => transitionRoutingReservation(held, "inverse", "attempt")) + assert.throws(() => transitionRoutingReservation(held, "inverse", "prove-unused")) +}) + +test("server-proven passed versus failed outcomes authorize distinct inverse allocation transitions", () => { + const base: RoutingReservationState = { revision: 2, forward: "spent", inverse: "held" } + const passed: RoutingReservationState = { ...base, outcome: { status: "passed", configurationProof: "proof", networkProof: "proof" } } + assert.equal(transitionRoutingReservation(passed, "inverse", "prove-unused").inverse, "proven-unused") + assert.throws(() => transitionRoutingReservation(passed, "inverse", "attempt")) + const failed: RoutingReservationState = { ...base, outcome: { ...passed.outcome!, status: "failed" } } + assert.equal(transitionRoutingReservation(failed, "inverse", "attempt").inverse, "spent") + assert.throws(() => transitionRoutingReservation(failed, "inverse", "prove-unused")) +}) + +function sqlFixture(failure?: "commit" | "work" | "rollback") { + const calls: string[] = [], released: boolean[] = [] + const db = { query: async (sql: string) => { calls.push(sql); if (sql === "COMMIT" && failure === "commit" || sql === "ROLLBACK" && failure === "rollback") throw Error("transport lost"); return { rows: [], command: sql } }, + release: (destroy: boolean) => { released.push(destroy) } } as unknown as PoolClient + return { db, calls, released, connect: async () => db } +} + +test("uncertain COMMIT destroys its owner without rollback or automatic retry", async () => { + const f = sqlFixture("commit") + await assert.rejects(routingReservationTransaction(async () => "value", f.connect), RoutingReservationCommitUnknown) + assert.equal(f.calls.filter(sql => sql === "COMMIT").length, 1) + assert.equal(f.calls.includes("ROLLBACK"), false); assert.deepEqual(f.released, [true]) +}) + +test("host charge witness is durable before COMMIT and survives loss of its acknowledgement", async () => { + const root = await mkdtemp(join(tmpdir(), "mmapp-routing-witness-")), saved = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = root + const profileId = randomUUID(), now = Date.now() + const witness = { allocationId: randomUUID(), createdAt: new Date(now).toISOString(), auditMac: "a".repeat(64) } + const aborted = { ...witness, allocationId: randomUUID() } + try { + await privateSecretFingerprint("owned fixture", "owned fixture") + const beforeCommit = sqlFixture() + await assert.rejects(routingReservationTransaction(async db => { + stageRoutingChargeWitness(db, profileId, aborted, now + 60_000) + throw new Error("refused before COMMIT") + }, beforeCommit.connect), /refused before COMMIT/) + assert.deepEqual(await readRoutingChargeJournal(profileId, now), []) + const lost = sqlFixture("commit"), query = lost.db.query.bind(lost.db) + lost.db.query = (async (sql: string) => { + if (sql === "COMMIT") assert.deepEqual(await readRoutingChargeJournal(profileId, now), [witness]) + return query(sql) + }) as PoolClient["query"] + await assert.rejects(routingReservationTransaction(async db => { + stageRoutingChargeWitness(db, profileId, witness, now + 60_000) + }, lost.connect), RoutingReservationCommitUnknown) + assert.deepEqual(await readRoutingChargeJournal(profileId, now), [witness]) + assert.deepEqual(lost.released, [true]) + } finally { + if (saved === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = saved + await rm(root, { recursive: true, force: true }) + } +}) + +test("expiry before or during publication rolls back without sending COMMIT", async t => { + const root = await mkdtemp(join(tmpdir(), "mmapp-routing-expired-witness-")), saved = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = root + const now = Date.now(), deadline = now + 1_000 + const witness = { allocationId: randomUUID(), createdAt: new Date(now).toISOString(), auditMac: "b".repeat(64) } + try { + await privateSecretFingerprint("owned fixture", "owned fixture") + for (const passes of [1, 2]) { + const profileId = randomUUID(), fixture = sqlFixture() + let checked = 0 + const clock = t.mock.method(Date, "now", () => ++checked <= passes ? now : deadline) + try { + await assert.rejects(routingReservationTransaction(async db => { + stageRoutingChargeWitness(db, profileId, witness, deadline) + }, fixture.connect), { code: "STATE_CONFLICT", message: "Исходный срок допуска переключения истёк" }) + } finally { + clock.mock.restore() + } + assert.equal(fixture.calls.includes("COMMIT"), false) + assert.equal(fixture.calls.at(-1), "ROLLBACK") + assert.deepEqual(await readRoutingChargeJournal(profileId, now), passes === 2 ? [witness] : []) + } + } finally { + if (saved === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = saved + await rm(root, { recursive: true, force: true }) + } +}) + +test("charge publication keeps the original monotonic lifetime and rechecks the grant before COMMIT", async () => { + const root = await mkdtemp(join(tmpdir(), "mmapp-routing-grant-witness-")), saved = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = root + const now = Date.now(), connectionId = randomUUID(), credentialId = randomUUID() + const witness = { allocationId: randomUUID(), createdAt: new Date(now).toISOString(), auditMac: "c".repeat(64) } + const grant = { proof: fingerprint({ credentialId, grant: null }), expiresAt: null, monotonicEnd: Infinity, + admissionExpiresAt: now + 60_000, admissionMonotonicEnd: performance.now() + 60_000 } + try { + await privateSecretFingerprint("owned fixture", "owned fixture") + for (const mode of ["expired", "changed", "current"] as const) { + const profileId = randomUUID(), fixture = sqlFixture() + const query = fixture.db.query.bind(fixture.db) + fixture.db.query = (async (sql: string, values?: unknown[]) => { + if (sql === temporaryGrantOriginSql) { + assert.deepEqual(values, [connectionId, credentialId]) + assert.deepEqual(await readRoutingChargeJournal(profileId, now), [witness]) + } + return query(sql) + }) as PoolClient["query"] + const tx = routingReservationTransaction(async db => { + bindRoutingChargeAdmission(db, connectionId, credentialId, { ...grant, + proof: mode === "changed" ? "old grant proof" : grant.proof, + admissionMonotonicEnd: mode === "expired" ? performance.now() - 1 : grant.admissionMonotonicEnd }) + stageRoutingChargeWitness(db, profileId, witness, now + 60_000) + }, fixture.connect) + if (mode === "current") await tx + else await assert.rejects(tx, { code: "GRANT_INACTIVE" }) + assert.equal(fixture.calls.includes(temporaryGrantOriginSql), mode !== "expired") + assert.equal(fixture.calls.at(-1), mode === "current" ? "COMMIT" : "ROLLBACK") + assert.deepEqual(await readRoutingChargeJournal(profileId, now), mode === "expired" ? [] : [witness]) + } + } finally { + if (saved === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = saved + await rm(root, { recursive: true, force: true }) + } +}) + +test("precommit error rolls back; failed rollback destroys connection; readonly uses same owner", async () => { + for (const failure of ["work", "rollback"] as const) { + const f = sqlFixture(failure) + await assert.rejects(routingReservationTransaction(async () => { throw Error("precommit") }, f.connect), /precommit/) + assert.equal(f.calls.includes("COMMIT"), false); assert.equal(f.calls.filter(sql => sql === "ROLLBACK").length, 1) + assert.deepEqual(f.released, [failure === "rollback"]) + } + const f = sqlFixture() + assert.equal(await routingReservationTransaction(async db => { await db.query("SELECT 1"); return "readonly" }, f.connect, true), "readonly") + assert.equal(f.calls[0], "BEGIN READ ONLY"); assert.deepEqual(f.released, [false]) +}) + +test("a serialized object cannot forge the internal inverse allocation capability", async () => { + const store = createRoutingReservationStore() + await assert.rejects(store.beginInverseAttempt(JSON.parse('{"kind":"routing-reserved-inverse-allocation"}')), /capability/) +}) + +test("forward allocation rejects caller IDs and serialized lookalikes before acquiring SQL ownership", async () => { + let connects = 0 + const store = createRoutingReservationStore(async () => { connects++; throw Error("must not connect") }) + for (const forged of ["00000000-0000-4000-8000-000000000000", { kind: "routing-reserved-forward-allocation" }]) { + await assert.rejects(store.beginForwardAttempt(JSON.parse(JSON.stringify(forged))), /capability/) + } + assert.equal(connects, 0) +}) + +test("outer runtime admission owns exactly one transaction and cannot return a value after lost ACK", async () => { + const confirmed = sqlFixture() + const result = await withRoutingReservationAdmission(async db => { await db.query("owned operation admission"); return { operationId: "synthetic" } }, confirmed.connect) + assert.deepEqual(result, { value: { operationId: "synthetic" }, reservation: undefined }) + assert.equal(confirmed.calls.filter(sql => sql === "BEGIN").length, 1) + assert.equal(confirmed.calls.filter(sql => sql === "COMMIT").length, 1) + const lost = sqlFixture("commit") + await assert.rejects(withRoutingReservationAdmission(async db => { await db.query("owned operation admission"); return "must not escape" }, lost.connect), RoutingReservationCommitUnknown) + assert.equal(lost.calls.includes("ROLLBACK"), false) + assert.deepEqual(lost.released, [true]) +}) + +test("inverse execution capability cannot be forged to read a running source", async () => { + let connects = 0 + const store = createRoutingReservationStore(async () => { connects++; throw Error("must not connect") }) + await assert.rejects(store.inspectInverseExecution(JSON.parse('{"kind":"routing-reserved-inverse-execution"}')), /capability/) + assert.equal(connects, 0) +}) diff --git a/backend/src/modules/mcp/routing-reserved-step.test.ts b/backend/src/modules/mcp/routing-reserved-step.test.ts new file mode 100644 index 00000000..d535bcb9 --- /dev/null +++ b/backend/src/modules/mcp/routing-reserved-step.test.ts @@ -0,0 +1,155 @@ +/** Component orchestration tests with import doubles, not full RT-2 acceptance. */ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { test } from "node:test" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import type { executeReservedRoutingStep } from "./routing-reserved-step.js" +import type { RoutingReservedNetworkSession } from "./service-registry.js" + +const source = await readFile(new URL("./routing-reserved-step.ts", import.meta.url), "utf8") +const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + +function fixture(mode: "passed" | "passed-publish-revoked" | "failed" | "forward-error" | "forward-unknown" | "forward-quarantine-error" | "outcome-error" | "inverse-error" | "revoked" | "wrong-origin" | "bad-receipt" | "missing-network" = "failed") { + const events: string[] = [], allocation = Object.freeze({ kind: "routing-reserved-inverse-allocation" }) + const principal = { connectionId: "owned-connection", credentialId: "owned-credential", credentialExpiresAt: "2026-10-29T01:00:00Z" } + const forwardResult = { configurationVerified: true, postProbeStatus: mode === "passed" ? "passed" : "failed", secret: "private-forward" } + const recoveryResult = { outcome: "recovered", nativeWrites: 1, configurationVerified: true, controlProbeVerified: true, receiptId: "inverse-receipt", recoveryCheckpointProof: "verified-proof" } + const counts = { forward: 0, outcome: 0, inverse: 0, access: 0 } + let active = true + let sessionCloses = 0 + const session = { readBudget: () => ({ closed: sessionCloses > 0 }), close: () => { sessionCloses++ } } as RoutingReservedNetworkSession + const origin = Object.freeze({}), metadata = { operationId: "owned-source", operationName: "routing.community.switch", operationVersion: 7, + serviceName: "routing.community.scoped", stepIndex: 0, phase: "forward", principal, serviceInput: { receiptId: "source-receipt" } } + class McpError extends Error { + constructor(readonly code: string, message: string, readonly statusCode: number) { super(message) } + } + const original = mode === "forward-unknown" || mode === "forward-quarantine-error" + ? new McpError("MUTATION_OUTCOME_UNKNOWN", "owned forward outcome unknown", 409) : Error("owned failure") + const modules: Record = { + "../../services/config-mutation-policy.js": { captureVerifiedMcpServiceExecution: async () => origin, inspectVerifiedMcpServiceExecution: async (token: unknown) => { + assert.equal(token, origin) + events.push("access"); counts.access++ + if (!active || mode === "revoked" && counts.access === 3) throw original + return mode === "wrong-origin" ? { ...metadata, stepIndex: 1 } : metadata + } }, + "./routing-reservation-store.js": { createRoutingReservationStore: () => ({ recordForwardOutcome: async (id: string) => { + events.push("outcome"); counts.outcome++; assert.equal(active, true); assert.equal(id, "owned-reservation") + if (mode === "outcome-error") throw original + if (mode === "passed-publish-revoked") active = false + return { allocation: mode === "passed" || mode === "passed-publish-revoked" ? undefined : allocation } + }, markUnknown: async (id: string, lane: string) => { + assert.equal(id, "owned-reservation"); assert.equal(lane, "forward"); events.push("quarantine") + if (mode === "forward-quarantine-error") throw Error("owned quarantine acknowledgement lost") + }, read: async (id: string, actor: unknown) => { + assert.equal(id, "owned-reservation"); assert.equal(actor, principal); events.push("ledger") + return { sourceOperationId: "owned-source", state: { forward: "spent", inverse: "spent", outcome: { status: "failed", configurationProof: "config-proof", networkProof: "network-proof" } }, deadline: "deadline", sharedSlotQuarantined: false } + } }) }, + "./routing-community-scoped-recovery.js": { executeReservedScopedRecovery: async (capability: unknown, expected: { sourceOperationId: string; principal: unknown; networkSession: unknown }) => { + events.push("inverse"); counts.inverse++; assert.equal(active, true) + assert.equal(capability, allocation, "Pass the exact process-private allocation object without cloning/JSON") + assert.equal(expected.sourceOperationId, "owned-source"); assert.equal(expected.principal, principal) + assert.equal(expected.networkSession, session); assert.equal(sessionCloses, 0) + if (mode === "inverse-error") throw original + return recoveryResult + }, validateReservedInverseVerified: async (_row: unknown, expected: unknown) => { + events.push("receipt") + assert.deepEqual(JSON.parse(JSON.stringify(expected)), { reservationId: "owned-reservation", sourceOperationId: "owned-source", sourceReceiptId: "source-receipt", inverseReceiptId: "inverse-receipt", deadline: "deadline", configurationProof: "config-proof", networkProof: "network-proof" }) + return mode !== "bad-receipt" + } }, + "../../db/index.js": { pool: { query: async (_sql: string, args: unknown[]) => { + assert.deepEqual(Array.from(args), ["owned-source", "owned-reservation"]); events.push("slot") + return { rows: [{ state: "verified", verified_proof: "verified-proof" }] } + } } }, + "./security.js": { McpError, fingerprint: (value: unknown) => JSON.stringify(value) }, + } + const exports: Record = {} + runInNewContext(compiled, { exports, require: (id: string) => { assert(id in modules, id); return modules[id] } }) + const execute = exports.executeReservedRoutingStep as typeof executeReservedRoutingStep + const branded = exports.hasConfirmedReservedRoutingForward as (error: unknown) => boolean + const run = () => execute({ reservationId: "owned-reservation", sourceOperationId: "owned-source", principal, + forward: async retain => { events.push("forward"); counts.forward++; assert.equal(active, true); if (mode === "forward-error" || mode === "forward-unknown" || mode === "forward-quarantine-error") throw original; if (mode !== "missing-network") retain(session); return forwardResult } }) + return { run, branded, events, counts, forwardResult, recoveryResult, original, sessionCloses: () => sessionCloses, close: () => { active = false } } +} + +test("component: successful forward publishes once inside original scope and never invokes inverse", async () => { + const f = fixture("passed"), result = await f.run() + assert.equal(result.kind, "forwardPassed"); assert.equal(result.forwardResult, f.forwardResult) + assert.deepEqual(f.events, ["access", "forward", "access", "outcome", "access"]) + assert.deepEqual(f.counts, { forward: 1, outcome: 1, inverse: 0, access: 3 }) + assert.equal(f.sessionCloses(), 1) +}) + +test("component: failed signed outcome hands exact private capability to inverse before caller receives result", async () => { + const f = fixture(), result = await f.run() + assert.equal(result.kind, "recovered"); assert.equal(result.forwardResult, f.forwardResult) + if (result.kind === "recovered") assert.equal(result.recoveryResult, f.recoveryResult) + assert.equal(result.forwardResult.postProbeStatus, "failed", "Recovery must not relabel forward as verified") + assert.deepEqual(f.events, ["access", "forward", "access", "outcome", "access", "inverse", "access", "ledger", "slot", "receipt", "access"]) + assert.equal(f.sessionCloses(), 1) +}) + +test("component: inverse cannot start without the original session", async () => { + const f = fixture("missing-network") + await assert.rejects(f.run(), error => f.branded(error)) + assert.equal(f.counts.inverse, 0) +}) + +test("component: completed original verified scope refuses before forward", async () => { + const f = fixture(); f.close() + await assert.rejects(f.run(), error => error === f.original) + assert.deepEqual(f.counts, { forward: 0, outcome: 0, inverse: 0, access: 1 }) +}) + +test("component: unclassified forward error propagates without changing the reservation", async () => { + const f = fixture("forward-error") + await assert.rejects(f.run(), error => error === f.original) + assert.deepEqual(f.counts, { forward: 1, outcome: 0, inverse: 0, access: 1 }) +}) + +test("component: unknown forward attempts quarantine once and preserves the original failure", async () => { + for (const mode of ["forward-unknown", "forward-quarantine-error"] as const) { + const f = fixture(mode) + await assert.rejects(f.run(), error => error === f.original) + assert.deepEqual(f.events, ["access", "forward", "quarantine"]) + assert.deepEqual(f.counts, { forward: 1, outcome: 0, inverse: 0, access: 1 }) + } +}) + +for (const mode of ["outcome-error", "inverse-error", "revoked"] as const) test(`component: ${mode} preserves private forward progress, fails unknown and never retries`, async () => { + const f = fixture(mode) + await assert.rejects(f.run(), (error: unknown) => { + const value = error as { code: string } + assert.equal(value.code, "MUTATION_OUTCOME_UNKNOWN") + assert.equal(f.branded(error), true) + assert.equal(f.branded(JSON.parse(JSON.stringify(error))), false) + assert.equal(JSON.stringify(error).includes("private-forward"), false) + return true + }) + assert.equal(f.counts.forward, 1); assert.equal(f.counts.outcome, 1) + assert.equal(f.counts.inverse, mode === "inverse-error" ? 1 : 0) + assert.equal(f.sessionCloses(), 1) +}) + +test("component: another operation/service step cannot use an active verified scope", async () => { + const f = fixture("wrong-origin") + await assert.rejects(f.run(), /Исходный service step/) + assert.equal(f.counts.forward, 0); assert.equal(f.counts.outcome, 0) +}) + +test("component: returned recovery object cannot replace authenticated durable receipts", async () => { + const f = fixture("bad-receipt") + await assert.rejects(f.run(), error => f.branded(error)) + assert.equal(f.counts.inverse, 1); assert.equal(f.counts.forward, 1) +}) + +test("component: passed outcome publication cannot outlive original scope or return forwardPassed", async () => { + const f = fixture("passed-publish-revoked") + await assert.rejects(f.run(), error => { + assert.equal(f.branded(error), true, "Actual forward progress survives late scope revocation privately") + assert.equal((error as { code: string }).code, "MUTATION_OUTCOME_UNKNOWN") + return true + }) + assert.deepEqual(f.events, ["access", "forward", "access", "outcome", "access"]) + assert.deepEqual(f.counts, { forward: 1, outcome: 1, inverse: 0, access: 3 }) +}) diff --git a/backend/src/modules/mcp/routing-reserved-step.ts b/backend/src/modules/mcp/routing-reserved-step.ts new file mode 100644 index 00000000..3493c876 --- /dev/null +++ b/backend/src/modules/mcp/routing-reserved-step.ts @@ -0,0 +1,92 @@ +import { pool } from "../../db/index.js" +import { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution } from "../../services/config-mutation-policy.js" +import { executeReservedScopedRecovery, validateReservedInverseVerified } from "./routing-community-scoped-recovery.js" +import { createRoutingReservationStore } from "./routing-reservation-store.js" +import { fingerprint, McpError } from "./security.js" +import type { McpPrincipal } from "./store.js" +import type { RoutingReservedNetworkSession } from "./service-registry.js" + +export type ReservedRoutingStepOutcome = + | { kind: "forwardPassed"; forwardResult: T } + | { kind: "recovered"; forwardResult: T; recoveryResult: Awaited> } + +/** Private runner progress, never serialize this error or its forward result. + * A confirmed forward configuration does not prove a verified network outcome. + * An unconfirmed handoff must not re-enter forward or reconstruct a capability. */ +const unconfirmed = new WeakMap() +class ReservedRoutingHandoffUnknown extends McpError { + constructor(forwardResult: unknown, originalError: unknown) { + super("MUTATION_OUTCOME_UNKNOWN", "Результат зарезервированного восстановления не подтверждён; повтор запрещён", 409) + unconfirmed.set(this, { forwardResult, originalError }) + } +} +/** Process-local progress only; public JSON and matching error codes confer none. */ +export function hasConfirmedReservedRoutingForward(error: unknown): boolean { + return typeof error === "object" && error !== null && unconfirmed.has(error) +} + +/** Internal composition only; call INSIDE the original runner's active + * withVerifiedConfigurationChange callback, before changing generic intent. + * This does not activate applyPlan's reserved dispatch gate or close RT-1. */ +export async function executeReservedRoutingStep(input: { + reservationId: string; sourceOperationId: string; principal: McpPrincipal; + forward: (retainNetworkSession: (session: RoutingReservedNetworkSession) => void) => Promise +}): Promise> { + const origin = await captureVerifiedMcpServiceExecution() + const inspect = async () => { + const metadata = await inspectVerifiedMcpServiceExecution(origin) + if (metadata.operationId !== input.sourceOperationId || metadata.operationName !== "routing.community.switch" || metadata.operationVersion !== 7 || + metadata.serviceName !== "routing.community.scoped" || metadata.stepIndex !== 0 || metadata.phase !== "forward" || + fingerprint(metadata.principal) !== fingerprint(input.principal)) throw new McpError("STATE_CONFLICT", "Исходный service step восстановления изменился", 409) + return metadata + } + await inspect() + const retained: { session?: RoutingReservedNetworkSession } = {} + let acceptingSession = true + try { + let forwardResult: T + try { + forwardResult = await input.forward(session => { + if (!acceptingSession || retained.session || session.readBudget().closed) { + throw new McpError("STATE_CONFLICT", "Исходный сетевой сеанс уже передан или закрыт", 409) + } + retained.session = session + }) + } catch (error) { + if (error instanceof McpError && error.code === "MUTATION_OUTCOME_UNKNOWN") { + // Preserve the spent attempt even if quarantine itself cannot be confirmed. + await createRoutingReservationStore().markUnknown(input.reservationId, "forward").catch(() => {}) + } + throw error + } finally { acceptingSession = false } + try { + await inspect() + const store = createRoutingReservationStore() + const published = await store.recordForwardOutcome(input.reservationId) + await inspect() + if (!published.allocation) return { kind: "forwardPassed", forwardResult } + if (!retained.session) throw new McpError("STATE_CONFLICT", "Исходный сетевой сеанс возврата недоступен", 409) + const recoveryResult = await executeReservedScopedRecovery(published.allocation, { + sourceOperationId: input.sourceOperationId, principal: input.principal, networkSession: retained.session, + }) + const metadata = await inspect() + // A returned object alone cannot promote recovery to verified. Re-read the + // signed ledger and authenticate all three receipts in the same source slot. + const record = await store.read(input.reservationId, input.principal) + const row = (await pool.query("SELECT * FROM mcp_scoped_recovery_intents WHERE source_operation_id=$1 AND source_step_index=0 AND reservation_id=$2 AND mode='reserved'", [input.sourceOperationId, input.reservationId])).rows + if (record.sourceOperationId !== input.sourceOperationId || record.state.forward !== "spent" || record.state.inverse !== "spent" || record.state.outcome?.status !== "failed" || record.sharedSlotQuarantined || + row.length !== 1 || row[0].state !== "verified" || row[0].verified_proof !== recoveryResult.recoveryCheckpointProof || + recoveryResult.outcome !== "recovered" || recoveryResult.configurationVerified !== true || recoveryResult.controlProbeVerified !== true || recoveryResult.nativeWrites !== 1 || + !await validateReservedInverseVerified(row[0], { reservationId: input.reservationId, sourceOperationId: input.sourceOperationId, + sourceReceiptId: String(metadata.serviceInput.receiptId), inverseReceiptId: String(recoveryResult.receiptId), deadline: record.deadline, + configurationProof: record.state.outcome.configurationProof, networkProof: record.state.outcome.networkProof })) { + throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Подписанная квитанция inverse не подтверждена", 409) + } + await inspect() + return { kind: "recovered", forwardResult, recoveryResult } + } catch (error) { + // No retry, refund, replacement allocation or synthetic source checkpoint. + throw new ReservedRoutingHandoffUnknown(forwardResult, error) + } + } finally { retained.session?.close() } +} diff --git a/backend/src/modules/mcp/routing-switch-ledger-contract.test.ts b/backend/src/modules/mcp/routing-switch-ledger-contract.test.ts new file mode 100644 index 00000000..458c9020 --- /dev/null +++ b/backend/src/modules/mcp/routing-switch-ledger-contract.test.ts @@ -0,0 +1,92 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { projectRoutingSwitchLedgerModel, type RoutingSwitchLedgerEntry } from "./routing-switch-ledger-contract.js" + +const profileId = "11111111-1111-4111-8111-111111111111" +const sourceOperationId = "22222222-2222-4222-8222-222222222222" +const allocationId = "33333333-3333-4333-8333-333333333333" +const other = "44444444-4444-4444-8444-444444444444" +const now = Date.parse("2026-10-31T12:00:00.000Z") +const recent = "2026-10-31T11:59:00.000Z" +const base: RoutingSwitchLedgerEntry = { profileId, sourceOperationId, sourceStepIndex: 0, allocationId, + sourceIdentity: "a".repeat(64), lane: "forward", state: "held", createdAt: recent } +const input = (entries: unknown[] = [base], legacyState: unknown = {}) => ({ profileId, now, maxSwitchesPerHour: 3, complete: true, entries, legacyState }) + +test("forward/inverse reserve two slots; every lane state remains charged", () => { + for (const state of ["held", "spent", "unknown"] as const) { + const result = projectRoutingSwitchLedgerModel(input([{ ...base, state }, { ...base, lane: "inverse", state }])) + assert.equal(result.totalCharges, 2); assert.equal(result.availableSlots, 1) + assert.equal(result.securityProof, false); assert.equal(result.dispatchAllowed, false) + } + assert.equal(projectRoutingSwitchLedgerModel(input([base, { ...base, lane: "inverse", state: "proven-unused" }])).totalCharges, 2) + assert.throws(() => projectRoutingSwitchLedgerModel(input([{ ...base, state: "proven-unused" }]))) +}) +test("same immutable allocation counts once across reads and charged state transitions", () => { + assert.equal(projectRoutingSwitchLedgerModel(input([base, { ...base }, { ...base, state: "spent" }, { ...base, state: "unknown" }])).totalCharges, 1) +}) +test("same timestamp in distinct operations never deduplicates", () => { + assert.equal(projectRoutingSwitchLedgerModel(input([base, { ...base, sourceOperationId: other, allocationId: other }])).totalCharges, 2) +}) +test("different source steps are distinct only with their own allocation", () => { + assert.equal(projectRoutingSwitchLedgerModel(input([base, { ...base, sourceStepIndex: 1, allocationId: other }])).totalCharges, 2) + assert.throws(() => projectRoutingSwitchLedgerModel(input([base, { ...base, sourceStepIndex: 1 }]))) +}) +test("immutable operation/lane binding conflicts fail closed", () => { + for (const change of [{ allocationId: other }, { sourceIdentity: "b".repeat(64) }, { createdAt: "2026-10-31T11:58:00.000Z" }]) { + assert.throws(() => projectRoutingSwitchLedgerModel(input([base, { ...base, ...change }]))) + } +}) +test("allocation cannot be relabelled as another operation", () => { + assert.throws(() => projectRoutingSwitchLedgerModel(input([base, { ...base, sourceOperationId: other }]))) + assert.throws(() => projectRoutingSwitchLedgerModel(input([base, { ...base, lane: "inverse", sourceOperationId: other }]))) + assert.throws(() => projectRoutingSwitchLedgerModel(input([base, { ...base, lane: "inverse", sourceIdentity: "b".repeat(64) }]))) +}) +test("foreign profile is rejected rather than hidden", () => { + assert.throws(() => projectRoutingSwitchLedgerModel(input([{ ...base, profileId: other }]))) +}) +test("manual inverse shares original inverse slot; recovery operation cannot rebind it", () => { + const inverse = { ...base, lane: "inverse" } + assert.equal(projectRoutingSwitchLedgerModel(input([base, inverse, { ...inverse }])).totalCharges, 2) + assert.throws(() => projectRoutingSwitchLedgerModel(input([base, inverse, { ...inverse, sourceOperationId: other }]))) +}) +test("legacy overlap stays charged even with identical timestamp", () => { + const result = projectRoutingSwitchLedgerModel(input([base, { ...base, lane: "inverse" }], { switches: [recent, recent] })) + assert.equal(result.legacyCharges, 2); assert.equal(result.totalCharges, 4) + assert.equal(result.availableSlots, 0); assert.equal(result.budgetFits, false) +}) +test("hour boundary follows existing strict greater-than rule", () => { + const boundary = "2026-10-31T11:00:00.000Z" + assert.equal(projectRoutingSwitchLedgerModel(input([{ ...base, createdAt: boundary }], { switches: [boundary] })).totalCharges, 0) + assert.equal(projectRoutingSwitchLedgerModel(input([{ ...base, createdAt: "2026-10-31T11:00:00.001Z" }])).totalCharges, 1) +}) +test("incomplete/oversized input cannot become a smaller successful budget", () => { + assert.throws(() => projectRoutingSwitchLedgerModel({ ...input(), complete: false })) + assert.throws(() => projectRoutingSwitchLedgerModel(input(Array(2049).fill(base)))) + assert.throws(() => projectRoutingSwitchLedgerModel(input([base, null]))) +}) +test("malformed/future history and untrusted extra metadata are rejected", () => { + for (const entry of [{ ...base, createdAt: "2026-10-31T12:00:00.001Z" }, { ...base, sourceIdentity: "caller-assertion" }, + { ...base, sourceStepIndex: -1 }, { ...base, state: "refunded" }, { ...base, credentialId: other }]) { + assert.throws(() => projectRoutingSwitchLedgerModel(input([entry]))) + } + for (const legacy of [null, { switches: ["invalid"] }, { switches: ["2026-10-31T12:00:00.001Z"] }, { switches: Array(2049).fill(recent) }]) { + assert.throws(() => projectRoutingSwitchLedgerModel(input([], legacy))) + } +}) +test("limits and clock must be finite bounded integers", () => { + for (const maxSwitchesPerHour of [0, 11, 1.5, NaN, Infinity]) assert.throws(() => projectRoutingSwitchLedgerModel({ ...input(), maxSwitchesPerHour })) + for (const clock of [0, -1, NaN, Infinity, now + 0.5]) assert.throws(() => projectRoutingSwitchLedgerModel({ ...input(), now: clock })) +}) +test("three charged slots fit; fourth lane cannot fit regardless of writer kind", () => { + const pair = [base, { ...base, lane: "inverse" }] + const manual = { ...base, sourceOperationId: other, allocationId: other } + assert.equal(projectRoutingSwitchLedgerModel(input([...pair, manual])).availableSlots, 0) + assert.equal(projectRoutingSwitchLedgerModel(input([...pair, manual, { ...manual, lane: "inverse" }])).budgetFits, false) +}) +test("snapshot result is frozen and retains history across simulated restart/human save", () => { + const rows = [{ ...base }], result = projectRoutingSwitchLedgerModel(input(rows)) + rows[0].state = "unknown" + assert.equal(result.totalCharges, 1); assert.equal(Object.isFrozen(result), true) + assert.deepEqual(projectRoutingSwitchLedgerModel(input(JSON.parse(JSON.stringify(rows)), { switches: [recent] })), + projectRoutingSwitchLedgerModel(input(rows, { switches: [recent], unrelatedHumanState: "new grant" }))) +}) diff --git a/backend/src/modules/mcp/routing-switch-ledger-contract.ts b/backend/src/modules/mcp/routing-switch-ledger-contract.ts new file mode 100644 index 00000000..5fe6c25e --- /dev/null +++ b/backend/src/modules/mcp/routing-switch-ledger-contract.ts @@ -0,0 +1,88 @@ +import { countLegacyRoutingSwitches } from "./routing-reservation-contract.js" + +/** Inert arithmetic contract, deliberately NOT an admission API. + * Completeness, sourceIdentity and SQL ownership are assertions in this model; + * this module cannot authenticate them. No runtime caller may use budgetFits + * as authority. RT3.B2 must acquire and verify the complete ledger itself under + * the shared transaction/profile lock and the original execution authority. + * Manual inverse uses its ORIGINAL source operation/step and inverse lane, + * never a new recovery-operation identity to escape the common allocation. */ +export interface RoutingSwitchLedgerEntry { + readonly profileId: string + readonly sourceOperationId: string + readonly sourceStepIndex: number + readonly allocationId: string + readonly sourceIdentity: string + readonly lane: "forward" | "inverse" + readonly createdAt: string + readonly state: "held" | "spent" | "unknown" | "proven-unused" +} + +const entryKeys = ["allocationId", "createdAt", "lane", "profileId", "sourceIdentity", "sourceOperationId", "sourceStepIndex", "state"].sort() +const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ +const fail = (): never => { throw Error("Invalid or incomplete routing switch ledger model") } +function plain(raw: unknown): raw is Record { + return !!raw && typeof raw === "object" && [Object.prototype, null].includes(Object.getPrototypeOf(raw)) +} +function timestamp(raw: unknown, now: number): number { + if (typeof raw !== "string" || raw.length !== 24) return fail() + const at = Date.parse(raw) + if (!Number.isSafeInteger(at) || at < 0 || at > now || new Date(at).toISOString() !== raw) return fail() + return at +} +function entry(raw: unknown, profileId: string, now: number): RoutingSwitchLedgerEntry { + if (!plain(raw) || Object.keys(raw).sort().join("|") !== entryKeys.join("|")) return fail() + if (raw.profileId !== profileId || typeof raw.sourceOperationId !== "string" || !uuid.test(raw.sourceOperationId) || + typeof raw.allocationId !== "string" || !uuid.test(raw.allocationId) || + typeof raw.sourceIdentity !== "string" || !/^[0-9a-f]{64}$/.test(raw.sourceIdentity) || + !Number.isSafeInteger(raw.sourceStepIndex) || (raw.sourceStepIndex as number) < 0 || (raw.sourceStepIndex as number) > 49 || + !["forward", "inverse"].includes(raw.lane as string) || !["held", "spent", "unknown", "proven-unused"].includes(raw.state as string) || + raw.lane === "forward" && raw.state === "proven-unused") return fail() + timestamp(raw.createdAt, now) + // Copy immutable scalar fields; later mutation of input cannot change output. + return Object.freeze({ ...raw }) as unknown as RoutingSwitchLedgerEntry +} + +/** Both histories are a COMPLETE, bounded model snapshot, not paginated input. + * Same allocation/operation/step/lane with matching immutable source binding is + * counted once, irrespective of charged state. Conflicting immutable bindings + * fail closed. Matching timestamps, versions or credentials never deduplicate. + * Legacy entries always remain separately charged: they have no proven link. + * No refund, persisted write, dispatch, authority token or signature is emitted. */ +export function projectRoutingSwitchLedgerModel(input: { + readonly profileId: string + readonly now: number + readonly maxSwitchesPerHour: number + readonly complete: boolean + readonly legacyState: unknown + readonly entries: readonly unknown[] +}) { + const { profileId, now, maxSwitchesPerHour } = input + if (typeof profileId !== "string" || !uuid.test(profileId) || !Number.isSafeInteger(now) || now < 3600000 || + !Number.isSafeInteger(maxSwitchesPerHour) || maxSwitchesPerHour < 1 || maxSwitchesPerHour > 10 || input.complete !== true || + !Array.isArray(input.entries) || input.entries.length > 2048) return fail() + const legacyCharges = countLegacyRoutingSwitches(input.legacyState, now) + const operations = new Map(), allocations = new Map(), allocationOwners = new Map() + let ledgerCharges = 0 + for (const raw of input.entries) { + const value = entry(raw, profileId, now) + const operationKey = `${value.sourceOperationId}:${value.sourceStepIndex}:${value.lane}` + const allocationKey = `${value.allocationId}:${value.lane}` + const binding = JSON.stringify([value.allocationId, value.sourceIdentity, value.createdAt]) + const owner = JSON.stringify([value.sourceOperationId, value.sourceStepIndex, value.sourceIdentity, value.createdAt]) + if (allocationOwners.has(value.allocationId) && allocationOwners.get(value.allocationId) !== owner) return fail() + allocationOwners.set(value.allocationId, owner) + if (operations.has(operationKey)) { + if (operations.get(operationKey) !== binding || allocations.get(allocationKey) !== operationKey) return fail() + continue + } + if (allocations.has(allocationKey)) return fail() + operations.set(operationKey, binding); allocations.set(allocationKey, operationKey) + if (Date.parse(value.createdAt) > now - 3600000) ledgerCharges++ + } + const totalCharges = legacyCharges + ledgerCharges + return Object.freeze({ legacyCharges, ledgerCharges, totalCharges, + availableSlots: Math.max(0, maxSwitchesPerHour - totalCharges), + budgetFits: totalCharges <= maxSwitchesPerHour, + securityProof: false as const, dispatchAllowed: false as const }) +} diff --git a/backend/src/modules/mcp/routing-worker-fixture-isolation.sql-helper.ts b/backend/src/modules/mcp/routing-worker-fixture-isolation.sql-helper.ts new file mode 100644 index 00000000..3efd2404 --- /dev/null +++ b/backend/src/modules/mcp/routing-worker-fixture-isolation.sql-helper.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createMcpConnection, authenticateMcpToken, effectiveMcpConnection, setConnectionStatus } from "./store.js" +import { accessGeneration } from "./security.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "./routing-observation-source.js" +import { resolveRoutingProfileAuthority, assertRoutingProfileAuthority } from "./routing-profile-authority.js" +import { tickMcpWorkers } from "./workers.js" + +/** OWNED SQL/localhost repro only. The real worker and real private authority + * are used; no clock/TTL/policy/authority provider is replaced. */ +export async function acceptRoutingWorkerFixtureIsolation(actor:AuthUser) { + assert.equal(process.env.MMAPP_OWN_ROUTING_FORWARD_ROUNDS,"true") + assert.equal(process.env.MMAPP_OWN_ROUTING_WORKER_REPRO,"true") + assert.equal(process.env.NODE_ENV,"test") + const target=new URL(process.env.DATABASE_URL??"") + assert(["postgres:","postgresql:"].includes(target.protocol));assert.equal(target.hostname,"127.0.0.1") + assert.equal(target.username,"mcp_test");assert.equal(target.pathname,"/postgres");assert(target.password) + assert.equal(target.search,"");assert.equal(target.hash,"") + assert(process.env.MCP_STATE_DIR&&isAbsolute(process.env.MCP_STATE_DIR)) + assert.equal(basename(resolve(process.env.MCP_STATE_DIR)),"state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n),0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_operations")).rows[0].n),0) + assert.equal((await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled,true) + const sid=132,id=randomUUID();let connectionId:string|undefined,requests=0 + const router=createServer((_req,res)=>{requests++;res.statusCode=500;res.end("{}");}) + await new Promise(done=>router.listen(0,"127.0.0.1",done)) + const exclusion=await pool.connect();let locked=false,unsafe=false + try { + locked=(await exclusion.query("SELECT pg_try_advisory_lock(hashtextextended('mcp-workers',0)) AS locked")).rows[0].locked===true + assert(locked,"Fresh owned repro must exclusively own worker key before fixture creation") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'owned-worker-repro','127.0.0.1',$2,false,'NEVER_WORKER_REPRO_PASSWORD')",[sid,(router.address() as {port:number}).port]) + const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"owned worker race",mode:"approval",expiresAt:new Date(Date.now()+600000).toISOString(), + permissions:["mm:network:read","mm:network:write","mm:filters:write"],scope:{serverIds:[sid],operationIds:["routing.analyze","routing.community.switch","routing.path.verify"],allowGlobal:false}}),actor) + connectionId=access.connection.id + const principal=await authenticateMcpToken(access.token),current=await effectiveMcpConnection(principal) + const profile=routeAiProfileSchema.parse({name:"synthetic non-main authority",serverId:sid,community:"65000:10",family:"ipv4",table:"tenant-a",probeVrf:"main",candidates:[ + {id:"a",jumpHostId:sid,exitNodeId:sid,wanInterface:"wan",downstreamInterface:"hop",gateway:"192.0.2.1",gatewayRef:{resource:"ipv4_route",id:"*r"}}, + ]}) + const expiry=new Date(Math.min(Date.now()+300000,current.owner_valid_until.getTime(),Date.parse(principal.credentialExpiresAt),Date.parse(current.policy.expiresAt))) + await pool.query("INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version) VALUES($1,$2,$3::jsonb,true,$4,$5,$6,$7)",[id,connectionId,JSON.stringify(profile),expiry,principal.credentialId,await accessGeneration(),current.policy.version]) + const source=await createRoutingSourceContext(await readRoutingEndpoints([sid])) + const context={profile,principal,ownerId:current.owner_id,transport:{contextSources:source.expected}} + const authority=await resolveRoutingProfileAuthority({profileId:id,expectedVersion:1,...context}) + await assertRoutingProfileAuthority(authority,context) + await tickMcpWorkers() // real periodic entry point must skip its busy key + const held=(await pool.query("SELECT enabled,state,version,grant_expires_at FROM mcp_routing_profiles WHERE id=$1",[id])).rows[0] + assert.equal(held.enabled,true);assert.deepEqual(held.state,{});assert.equal(held.version,1) + assert.equal(held.grant_expires_at.toISOString(),expiry.toISOString()) + await assertRoutingProfileAuthority(authority,context) + assert.equal((await exclusion.query("SELECT pg_advisory_unlock(hashtextextended('mcp-workers',0)) AS unlocked")).rows[0].unlocked,true) + locked=false + await tickMcpWorkers() // no sleep/race: deterministic real controller pause + const changed=(await pool.query("SELECT enabled,state,version,grant_expires_at FROM mcp_routing_profiles WHERE id=$1",[id])).rows[0] + assert.equal(changed.enabled,false);assert.equal(changed.version,1) + assert.equal(changed.grant_expires_at.toISOString(),expiry.toISOString()) + assert.match(changed.state.pausedReason,/Укажите явную BGP-привязку/) + const pauses=(await pool.query("SELECT id FROM mcp_audit WHERE action='controller.pause' AND payload->>'profileId'=$1",[id])).rows + assert.equal(pauses.length,1) + await assert.rejects(assertRoutingProfileAuthority(authority,context),(error:{code?:string})=>error.code==="STATE_CONFLICT") + assert.equal(requests,0,"Non-main refusal precedes all native requests") + return {realWorkerTick:true,heldKeyPreservedAuthority:true,releasedKeyPausedProfile:true,originalAuthorityRejected:true,profileVersionUnchanged:true,grantExpiryUnchanged:true,nativeRequests:requests,productionWorkerChanged:false} + } finally { + if(locked)try {unsafe=(await exclusion.query("SELECT pg_advisory_unlock(hashtextextended('mcp-workers',0)) AS unlocked")).rows[0].unlocked!==true} catch {unsafe=true} + exclusion.release(unsafe) + await pool.query("DELETE FROM mcp_routing_profiles WHERE id=$1",[id]) + if(connectionId)await setConnectionStatus(connectionId,"paused",actor) + await pool.query("DELETE FROM servers WHERE id=$1",[sid]) + router.closeAllConnections();await new Promise((done,fail)=>router.close(error=>error?fail(error):done())) + } +} diff --git a/backend/src/modules/mcp/runtime-poll-principal-boundary.ts b/backend/src/modules/mcp/runtime-poll-principal-boundary.ts new file mode 100644 index 00000000..d3a4857d --- /dev/null +++ b/backend/src/modules/mcp/runtime-poll-principal-boundary.ts @@ -0,0 +1,95 @@ +/** Private active-origin bridge. No authority data on scope or tokens. */ +import { AsyncLocalStorage } from "node:async_hooks" +import type { PoolClient } from "pg" +import { McpError, mcpStateDirectory } from "./security.js" +import { fixedPrincipalBoundarySource, principalBoundaryProjection, assertPrincipalBoundaryClient, type PrincipalBoundaryPacket, type McpPrincipal, type ConnectionRow } from "./store.js" +import { inspectCurrentVerifiedServiceOrigin } from "../../services/config-mutation-policy.js" +import { coordinatePollPolicy, PollPolicyCoordinationRefused, type PollPolicyTransaction, type PollPolicyWork } from "./poll-policy-coordinator.js" +import { temporaryGrantOriginSql } from "./temporary-grant-origin.js" +import { pollRuntimePolicySql } from "./poll-runtime-policy-source.js" + +declare const runtimePollBoundaryBrand: unique symbol +export type RuntimePollBoundary = Readonly<{ [runtimePollBoundaryBrand]: true }> +declare const policyLoanBrand: unique symbol +export type RuntimePollPolicyLoan = Readonly<{ [policyLoanBrand]: true }> +type Client = Pick +type Scope = { active: boolean; principal: Readonly; directory: string; finish: (connection: ConnectionRow, loan?: RuntimePollPolicyLoan) => Promise } +const scopes = new AsyncLocalStorage(), tokens = new WeakMap(), spent = new WeakSet() +const policyLoans = new WeakMap }>() +const deny = (): never => { throw new McpError("PERMISSION_DENIED", "Исходная граница runtime MCP недействительна", 403) } +const equal = (a: McpPrincipal, b: McpPrincipal) => a.connectionId === b.connectionId && a.credentialId === b.credentialId && a.credentialExpiresAt === b.credentialExpiresAt +function current(scope: Scope) { + if (!scope.active || scopes.getStore() !== scope) return deny() + const origin = inspectCurrentVerifiedServiceOrigin() + if (origin.serviceName !== "servers.poll" || origin.phase !== "forward" || !equal(origin.principal, scope.principal)) return deny() +} +/** Fixed runtime dispatch only, after original verified service entry. */ +export async function withRuntimePollPrincipalSource(principal: McpPrincipal, finish: Scope["finish"], work: () => Promise): Promise { + const scope: Scope = { active: true, principal: Object.freeze({ ...principal }), directory: mcpStateDirectory(), finish } + try { return await scopes.run(scope, work) } finally { scope.active = false } +} +export function captureRuntimePollPrincipalBoundary(expected: McpPrincipal, source: typeof fixedPrincipalBoundarySource): RuntimePollBoundary | undefined { + const scope = scopes.getStore() + if (!scope) return undefined + current(scope) + if (!equal(expected, scope.principal) || source !== fixedPrincipalBoundarySource || scope.directory !== mcpStateDirectory()) return undefined + const token = Object.freeze(Object.create(null)) as RuntimePollBoundary + tokens.set(token, scope) + return token +} +export function beginRuntimePollPrincipalBoundary(token: RuntimePollBoundary, packet: PrincipalBoundaryPacket): Promise { + const scope = token && tokens.get(token) + if (!scope) return deny() + current(scope) + if (spent.has(packet)) return deny() + const raw = principalBoundaryProjection(packet, scope.principal, scope.directory, false) + spent.add(packet) + return scope.finish(raw.connection) +} + +/** Borrow only the same fixed source client's live readonly transaction. + * No BEGIN/COMMIT, arbitrary SQL, cache, pool fallback or authority mint. */ +export function coordinateRuntimePollPrincipalBoundary(token: RuntimePollBoundary, packet: PrincipalBoundaryPacket, owner: { + client: Client; signal: AbortSignal; assertActive(): void; retireUnsafeOwner(): void +}, verify: () => Promise): Promise { + const scope = token && tokens.get(token) + if (!scope) return deny() + current(scope) + if (spent.has(packet) || inspectCurrentVerifiedServiceOrigin().operationName !== "servers.poll") return deny() + const raw = principalBoundaryProjection(packet, scope.principal, scope.directory, false) + assertPrincipalBoundaryClient(packet, owner.client) + spent.add(packet) + const live = () => { current(scope); if (scope.directory !== mcpStateDirectory()) deny(); owner.assertActive() } + return coordinatePollPolicy({ signal: owner.signal, assertActive: live, retireUnsafeOwner: owner.retireUnsafeOwner, + runPolicy: async work => { + let borrowed = true + const refuse = (): never => { owner.retireUnsafeOwner(); throw new PollPolicyCoordinationRefused() } + const query = async (text: string, values?: unknown[]) => { + if (!borrowed) return refuse() + live() + if (![temporaryGrantOriginSql, pollRuntimePolicySql].includes(text) || !Array.isArray(values) || values.length !== 2 + || values[0] !== scope.principal.connectionId || values[1] !== scope.principal.credentialId) return refuse() + const result = await owner.client.query(text, values) + if (!borrowed) return refuse() + live(); return result + } + try { live(); await work(Object.freeze({ query: query as PoolClient["query"] })); live() } + finally { borrowed = false } + }, + runtime: async run => { + live() + const loan = Object.freeze(Object.create(null)) as RuntimePollPolicyLoan + policyLoans.set(loan, { scope, run }) + try { await scope.finish(raw.connection, loan) } + finally { policyLoans.delete(loan) } + }, verify, + }) +} + +export async function executeRuntimePollPolicyTransaction(loan: RuntimePollPolicyLoan, work: PollPolicyWork): Promise { + const entry = loan && policyLoans.get(loan) + if (!entry) return deny() + current(entry.scope) + await entry.run(work) + current(entry.scope) +} diff --git a/backend/src/modules/mcp/runtime.ts b/backend/src/modules/mcp/runtime.ts new file mode 100644 index 00000000..583611d8 --- /dev/null +++ b/backend/src/modules/mcp/runtime.ts @@ -0,0 +1,894 @@ +import { partitionWriteFailureDiagnostic, withBackgroundPartitionWrite } from "../../services/partition-write-transaction.js" +import { pollErrorLog } from "../../services/server-poll-error-provenance.js" +import { manualRoutingPredispatchDiagnostic } from "./routing-community-predispatch-diagnostic.js" +import { vrfPreparationDiagnostic } from "./vrf-preparation-diagnostic.js" +import { createVrfRuntimeDiagnosticBoundary } from "../../services/vrf-execution-diagnostic.js" +import { capturePublishedServerProbeTerminalProof } from "../../services/server-probe-operation-retirement.js" +import { SERVER_PROBE_OPERATION } from "../../services/server-probe-authority.js" +import { preparedPlanExpiry } from "./plan-expiry.js" +import { acknowledgedCreateId, assertNoCallerCreateId } from "./native-create-identity.js" +import { randomUUID, createHash } from "node:crypto" +import { performance } from "node:perf_hooks" +import { readMcpTemporaryGrantOrigin,assertOriginalMcpTemporaryGrant,type OriginalServiceGrant } from "./temporary-grant-origin.js" +import type { PoolClient } from "pg" +import { mcpProtectedObjectSchema, mcpTemporaryGrantSchema, type McpPolicyPreviewRequest } from "@mmapp/contracts/mcp" +import { appUserCreateSchema, appUserUpdateSchema, userBindingCreateSchema } from "@mmapp/contracts/users" +import { logSettingsPatchSchema } from "@mmapp/contracts" +import { pool } from "../../db/index.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { createUser, updateUser, deleteUser, addBinding, removeBinding, getUserById } from "../users/service/users-service.js" +import { normalizeBindingPeer } from "../users/peer-bind.js" +import { getLogSettings, updateLogSettings } from "../../services/router-logs-collector.js" +import { refreshScheduler } from "../../services/scheduler.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { withOperationConfigurationLockClient, assertOperationConfigurationLockScope, assertOperationConfigurationLockCheckpoint } from "../../services/operation-config-lock-lease.js" +import { sharedDatabaseGate } from "../../services/shared-database-gate.js" +import { mcpDatabaseRequestSignal, withMcpDatabaseRequest, withMcpRunnerDatabaseRequest } from "./database-request-lease.js" +import { setTimeout as waitForRunnerAdmission } from "node:timers/promises" +import { withRuntimePollPrincipalSource, executeRuntimePollPolicyTransaction, type RuntimePollPolicyLoan } from "./runtime-poll-principal-boundary.js" +import { readPollRuntimePolicySource, type PollRuntimePolicySource } from "./poll-runtime-policy-source.js" +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 { findAction, findActionByOperation } from "./actions.js" +import { refuseExpiredOperationReplay } from "./operation-replay-tombstones.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, privateSecretFingerprint, McpError, redact } from "./security.js" +import { audit, getConnection, transaction, requestAccountingWrite, validatePrincipal, effectiveMcpConnection, assertConnectionVisible, type McpPrincipal, type ConnectionRow, type StoredPlan, type StoredOperation } from "./store.js" +import { executeRosStep, readRosSnapshot, routerClient, type RosSnapshot } from "./resources.js" +import { verifyHealthChecks } from "./routing-health-checks.js" +import { verifyRosStepSnapshots } from "./ros-step-verification.js" +import { createRosPrefix } from "./ros-prefix.js" +import { checkRpfPolicy } from "./routing-controls.js" +import type { ActionContext, ChangeEvidence, ChangeStep } from "./action-types.js" +import { logsFollowCancellation } from "./logs-follow-cancellation.js" +import { findMcpService, type McpServiceExecutionContext, type RoutingReservedNetworkSession } from "./service-registry.js" +import { withDiagnosticCancellation } from "./diagnostic-cancellation.js" +import { z } from "zod" +import { verifyManagementChannel } from "./management.js" +import { intersectGrant } from "./store.js" +import { changeMutationCost } from "./change-cost.js" +import { verifyRoutingObservationChecks } from "./routing-observations.js" +import { publicMcpChangeEvidence } from "./routing-community-public.js" +import { prepareRuntimeVrfTicket } from "./vrf-ticket-runtime.js" +import { executeVrfProductionStep, isVrfCompanionResource, VrfProductionAcknowledgedFailure } from "../../services/vrf-production-execution.js" +import { VrfProductionClaimOutcomeUnknown } from "../../services/vrf-production-ticket-store.js" +import { serviceNativeProgress } from "./service-native-progress.js" +import { serviceDiagnosticProgress, serviceDiagnosticReceipt } from "./service-diagnostic-progress.js" +import { admitManualInverseWithinOwner } from "./routing-manual-inverse-store.js" +import { bindRoutingChargeAdmission } from "./routing-reservation-transaction.js" +import { withRoutingReservationAdmission, createRoutingReservationStore, readHistoricalChargesWithinOwner, admitPersistentRoutingForwardWithinOwner, type RoutingReservedForwardAllocation } from "./routing-reservation-store.js" +import { lockRoutingReservationAdmissionSource, readScopedRoutingPathAdmissionDeadline, requiresScopedRoutingOwnerApproval, routingReservationEvidence } from "./routing-reservation-runtime.js" +import { executeReservedRoutingStep, hasConfirmedReservedRoutingForward, type ReservedRoutingStepOutcome } from "./routing-reserved-step.js" + +let activeRunners = 0 +const runnerQueue: Array<() => void> = [] +async function runBounded(work: () => Promise) { + if (activeRunners >= 2) await new Promise(resolve => runnerQueue.push(resolve)) + else activeRunners++ + try { await work() } + finally { const next = runnerQueue.shift(); if (next) next(); else activeRunners-- } +} + +async function runOrdinaryOperation(work: () => Promise): Promise { + for (;;) { + await sharedDatabaseGate.whenOpen() + let local = false, entered = false + try { + return await runBounded(() => sharedDatabaseGate.request(() => { + local = true + return withMcpRunnerDatabaseRequest(() => { + entered = true + return work() + }) + })) + } catch (error) { + if (entered || (local + ? !(error instanceof McpError && ["PROTOCOL_STORAGE_BUSY", "RATE_LIMITED"].includes(error.code)) + : !sharedDatabaseGate.isPaused())) throw error + await waitForRunnerAdmission(250) + } + } +} + +async function policyInput(client: Pick, connection: ConnectionRow, operationId: string, target: ChangeEvidence["target"], evidence?: ChangeEvidence, source?: PollRuntimePolicySource): Promise { + if (operationId === "operations.recover" && evidence?.snapshots.some(snapshot => ["routing_policy:routing_filter", "routing_policy:routing_filter_select"].includes(snapshot.resource)) && + (!hasPermission(connection.policy.permissions, "mm:filters:read") || !hasPermission(connection.owner_permissions, "mm:filters:read"))) { + throw new McpError("PERMISSION_DENIED", "Для восстановления привязки OSPF требуется право чтения фильтров", 403) + } + const { rows: protections } = source ? { rows: source.protections } : await client.query("SELECT object_ref FROM mcp_protections LIMIT 1001") + if (protections.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Число защит превышает предел проверки; изменение заблокировано", 503) + const { rows: [usage] } = source ? { rows: [source.usage] } : await client.query<{ calls: number; mutations: number; reads: number }>(` + 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]) + 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 }] : []), + ], + usage: { callsInMinute: Number(usage.calls), mutationsInHour: Number(usage.mutations), concurrentReads: Number(usage.reads) }, auditAvailable: true, + } +} + +export async function readMcpPolicyPreviewInput(connection: ConnectionRow, input: Pick): Promise { + const current = await policyInput(pool, connection, input.operationId, input.target) + return { ...current, objects: input.objects, objectCount: input.objectCount } +} + +async function temporaryScope(client: Pick, connection: ConnectionRow, credentialId: string) { + const { rows } = await client.query("SELECT payload FROM mcp_grants WHERE connection_id=$1 AND revoked_at IS NULL AND payload->>'credentialId'=$2", [connection.id, credentialId]) + if (!rows.length) return undefined + if (rows.length !== 1) throw new McpError("GRANT_INACTIVE", "Неоднозначный временный доступ") + const { credentialId: _credential, ...grant } = rows[0].payload + return mcpTemporaryGrantSchema.parse(grant) +} +async function reserveCall(principal: McpPrincipal, toolName: string, input: Record, signal?: AbortSignal) { + const action = findAction(toolName), requestId = randomUUID() + const reservation = await requestAccountingWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [principal.connectionId]) + const connection = await validatePrincipal(principal, client) + const grant = await temporaryScope(client, connection, principal.credentialId) + const context: ActionContext = { principal, connection: intersectGrant(connection, grant), signal } + const evidence = await policyInput(client, connection, action.operation.id, await action.target(input, { ...context, transactionClient: client })) + evidence.temporaryGrant = grant + const result = evaluateMcpPolicy(evidence) + await client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,server_ids,status,input,error_code) VALUES($1,$2,$3,$4,$5,$6,$7::jsonb,$8)", [requestId, connection.id, connection.owner_id, toolName, evidence.target.scope === "servers" ? evidence.target.serverIds : [], result.decision === "denied" ? "denied" : "running", JSON.stringify(redact(input)), result.decision === "denied" ? result.reasons[0]?.code : null]) + if (result.decision !== "denied") { + await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'calls',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+1", [connection.id]) + if (action.operation.effect === "read") await client.query("INSERT INTO mcp_read_leases(id,connection_id,expires_at) VALUES($1,$2,now()+interval '2 minutes')", [requestId, connection.id]) + await client.query("UPDATE mcp_connections SET last_used_at=now() WHERE id=$1", [connection.id]) + } + return { context, result } + }) + if (reservation.result.decision === "denied") throw new McpError(reservation.result.reasons[0]?.code ?? "PERMISSION_DENIED", reservation.result.reasons.map(reason => reason.message).join("; ")) + return { action, requestId, context: reservation.context } +} +async function completeRequest(id: string, started: number, result: unknown, error?: unknown, assertDeliveryAccess?: (client: PoolClient) => Promise) { + await requestAccountingWrite(async client => { + await client.query("UPDATE mcp_requests SET status=$2,result=$3::jsonb,error_code=$4,duration_ms=$5,finished_at=now() WHERE id=$1", [id, error ? "error" : "completed", JSON.stringify(redact(error ? { message: error instanceof McpError ? error.message : "Внутренняя ошибка операции" } : result)), error instanceof McpError ? error.code : error ? "INTERNAL_ERROR" : null, Math.min(2147483647, Date.now() - started)]) + await client.query("DELETE FROM mcp_read_leases WHERE id=$1", [id]) + await assertDeliveryAccess?.(client) + }) +} +type InternalMcpCall = { persistentRoutingProfile: NonNullable } + | { captureRoutingConfirmation: NonNullable } + +export async function invokeMcpTool(principal: McpPrincipal, toolName: string, raw: unknown, signal?: AbortSignal, internal?: InternalMcpCall): Promise { + if (toolName === "mm_logs_follow") { + const maintenance = new AbortController() + return sharedDatabaseGate.request( + () => withMcpDatabaseRequest(() => invokeMcpToolInner(principal, toolName, raw, signal, internal, maintenance.signal)), + () => maintenance.abort(new McpError("RESET_REQUIRED", "База данных перешла в режим обслуживания; начните новое чтение", 409)), + ) + } + return sharedDatabaseGate.request(() => withMcpDatabaseRequest(() => invokeMcpToolInner(principal, toolName, raw, signal, internal))) +} +async function invokeMcpToolInner(principal: McpPrincipal, toolName: string, raw: unknown, signal?: AbortSignal, internal?: InternalMcpCall, logsFollowMaintenance?: AbortSignal): Promise { + const action = findAction(toolName), input = action.schema.parse(raw) as Record, started = Date.now() + const leaseSignal = mcpDatabaseRequestSignal() + leaseSignal?.throwIfAborted() + const actionSignal = leaseSignal && signal ? AbortSignal.any([leaseSignal, signal]) : leaseSignal ?? signal + const call = await reserveCall(principal, toolName, input, actionSignal) + const cancelled = (): McpError | undefined => leaseSignal?.aborted ? leaseSignal.reason : logsFollowMaintenance && logsFollowCancellation(logsFollowMaintenance, signal) + const assertActive = () => { const failure = cancelled(); if (failure) throw failure } + let vrfPreparing = false + let routingConfirmation: Parameters>[0] | undefined + try { + assertActive() + if (logsFollowMaintenance) call.context.logsFollowMaintenance = leaseSignal ? AbortSignal.any([logsFollowMaintenance, leaseSignal]) : logsFollowMaintenance + if (internal && "persistentRoutingProfile" in internal) { + if (action.operation.id !== "routing.community.switch") throw new McpError("INVALID_INPUT", "Контекст профиля не относится к этой операции", 400) + call.context.persistentRoutingProfile = z.object({ id: z.string().uuid(), version: z.number().int().positive().safe() }).strict().parse(internal.persistentRoutingProfile) + } + if (internal && "captureRoutingConfirmation" in internal) { + if (action.operation.id !== "routing.analyze") throw new McpError("INVALID_INPUT", "Измерения контроллера не относятся к этой операции", 400) + call.context.captureRoutingConfirmation = observations => { routingConfirmation = observations } + } + let result: unknown + if (action.read) result = await action.read(input, call.context) + else if (action.prepare) { + vrfPreparing = ["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete"].includes(action.operation.id) + const evidence = await action.prepare(input, call.context) + result = await transaction(async client => { + assertActive() + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [principal.connectionId]) + const connection = await validatePrincipal(principal, client) + const check = await policyInput(client, connection, action.operation.id, evidence.target, evidence) + check.temporaryGrant = await temporaryScope(client, connection, principal.credentialId) + // The call already reserved one call slot. Preparation does not reserve a mutation. + check.usage.callsInMinute = Math.max(0, check.usage.callsInMinute - 1) + let decision = evaluateMcpPolicy(check) + if (decision.decision === "denied") throw new McpError(decision.reasons[0].code, decision.reasons.map(reason => reason.message).join("; ")) + 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]) + 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) + const current = await validatePrincipal(principal, client) + if (current.owner_id !== connection.owner_id || fingerprint(current.policy) !== fingerprint(connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(connection.owner_permissions)) throw new McpError("STATE_CONFLICT", "Доступ изменился во время подготовки VRF", 409) + const fresh = await policyInput(client, current, action.operation.id, evidence.target, evidence) + fresh.temporaryGrant = await temporaryScope(client, current, principal.credentialId) + fresh.usage.callsInMinute = Math.max(0, fresh.usage.callsInMinute - 1) + const reviewed = evaluateMcpPolicy(fresh) + if (reviewed.decision === "denied") throw new McpError(reviewed.reasons[0].code, reviewed.reasons.map(reason => reason.message).join("; ")) + if (reviewed.decision !== decision.decision) throw new McpError("STATE_CONFLICT", "Политика подготовки VRF изменилась", 409) + }, + assertCurrentSource: async (serverId, proof) => { + const { server } = await routerClient(serverId) + const current = await privateSecretFingerprint(fingerprint({ id: server.id, enabled: server.enabled, host: server.host, port: server.port, useSsl: server.useSsl, verifySsl: server.verifySsl, username: server.username, password: server.password, epoch: server.routingEndpointEpoch }), "mcp-ros-snapshot-source-v3") + if (!server.enabled || current !== proof) throw new McpError("STATE_CONFLICT", "Источник VRF изменился", 409) + }, + }) + if (vrfTicket) { + // Admission closes physical/VLAN parents and implicit native objects. + // Recheck the existing object policy/budget before publishing the + // final encrypted plan; the whole transaction rolls back on refusal. + const finalConnection = await validatePrincipal(principal, client) + const finalCheck = await policyInput(client, finalConnection, action.operation.id, evidence.target, evidence) + finalCheck.temporaryGrant = await temporaryScope(client, finalConnection, principal.credentialId) + 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"]) + } + 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 } + }) + vrfPreparing = false + } else throw new McpError("UNSUPPORTED_CAPABILITY", "Нет обработчика операции", 409) + assertActive() + const assertCurrentResultAccess = async (client?: PoolClient) => { + const current = await effectiveMcpConnection(principal, client) + assertActive() + if (fingerprint(current.policy) !== fingerprint(call.context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(call.context.connection.owner_permissions)) throw new McpError("STATE_CONFLICT", "Доступ изменился во время вызова; результат не выдан", 409) + } + await assertCurrentResultAccess() + const output = redact(result) + const checkDelivery = action.operation.section === "logs" && action.operation.effect === "read" ? assertCurrentResultAccess : leaseSignal ? async () => leaseSignal.throwIfAborted() : undefined + await completeRequest(call.requestId, started, output, undefined, checkDelivery) + await checkDelivery?.() + assertActive() + if (routingConfirmation && internal && "captureRoutingConfirmation" in internal) internal.captureRoutingConfirmation(routingConfirmation) + return output + } catch (error) { + if (vrfPreparing) { + try { + const diagnostic = vrfPreparationDiagnostic(call.requestId, action.operation.id, error) + if (diagnostic) console.error(JSON.stringify(diagnostic)) + } catch { /* Diagnostics must never replace the original failure. */ } + } + const failure = cancelled() ?? error + await completeRequest(call.requestId, started, null, failure) + throw failure + } +} + +export async function loadPlan(id: string, client: Pick = pool): Promise { + const { rows: [plan] } = await client.query("SELECT * FROM mcp_plans WHERE id=$1", [id]) + if (!plan) throw new McpError("NOT_FOUND", "План не найден", 404) + return plan +} +export async function publicPlan(plan: StoredPlan) { + 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, + 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) { + return transaction(async client => { + await client.query("SELECT id FROM mcp_plans WHERE id=$1 FOR UPDATE", [id]) + const plan = await loadPlan(id, client), connection = await getConnection(plan.connection_id, client) + assertConnectionVisible(connection, actor) + if (!["awaiting-approval", "draft"].includes(plan.state) || plan.expires_at.getTime() <= Date.now() || plan.generation !== await accessGeneration()) throw new McpError("PLAN_EXPIRED", "План устарел или уже запущен", 409) + const action = findActionByOperation(plan.operation_id) + if (!hasPermission(actor.permissions, "mm:mcp:approve") || action.operation.requiredPermissions.some(permission => !hasPermission(actor.permissions, permission))) throw new McpError("PERMISSION_DENIED", "Нет прав на подтверждение действия") + if (approve && actor.id !== connection.owner_id && plan.operation_id === "routing.community.switch" && + requiresScopedRoutingOwnerApproval(JSON.parse(await decryptSecret(plan.evidence.sealed)) as ChangeEvidence)) { + throw new McpError("PERMISSION_DENIED", "Переключение постоянного профиля подтверждает владелец подключения", 403) + } + await client.query("UPDATE mcp_plans SET state=$2,approved_by=$3,approved_at=now() WHERE id=$1", [id, approve ? "approved" : "cancelled", actor.id]) + await audit(client, actor.id, approve ? "plan.approve" : "plan.reject", connection.id, { planId: id }) + }) +} +async function serviceCurrent(service: string, input: Record): Promise { + const custom = findMcpService(service) + if (custom) return custom.current(input) + if (service === "logs.settings.update") return getLogSettings() + if (service.startsWith("users.")) return input.id ? getUserById(String(input.id)) : null + throw new McpError("UNSUPPORTED_CAPABILITY", "Неизвестный сервис", 409) +} +async function serviceExecute(service: string, input: Record, context: McpServiceExecutionContext): Promise { + const custom = findMcpService(service) + if (custom) { + if (!custom.cancellableDiagnostic) return custom.execute(input, context) + let entered = false + try { + return await withDiagnosticCancellation(context, signal => { + entered = true + return custom.execute(input, context.phase === "forward" ? { ...context, signal } : context) + }) + } catch (error) { + if (!entered && context.phase === "forward" && error instanceof McpError && error.code === "READ_CANCELLED" && custom.diagnosticIdentity) { + // Only the watcher knows execute was never entered. The handler still + // proves the exact encrypted plan/operation before supplying identity. + try { + const identity = await custom.diagnosticIdentity(input, context) + const progress = serviceDiagnosticProgress(Object.assign(new McpError("READ_CANCELLED", "Диагностика отменена до начала"), { + diagnosticProgress: { ...identity, operationId: context.operationId, state: "cancelled", attemptedStages: 0, completedStages: 0, automaticReplay: false }, + }), true, context.operationId, identity.stageBudget) + if (progress) Object.assign(error, { diagnosticNotStarted: true, diagnosticProgress: progress }) + } catch { /* Failure to prove ownership keeps the conservative unknown. */ } + } + throw error + } + } + if (service === "users.create") return createUser(appUserCreateSchema.parse(input.user)) + if (service === "users.update") return updateUser(String(input.id), appUserUpdateSchema.parse(input.user)) + if (service === "users.delete") return deleteUser(String(input.id)) + if (service === "users.binding_add") return addBinding(String(input.id), userBindingCreateSchema.parse(input.binding), { assertCurrent: assertVerifiedConfigurationAccess }) + if (service === "users.binding_remove") return removeBinding(String(input.id), String(input.bindingId)) + if (service === "logs.settings.update") { const result = await updateLogSettings(logSettingsPatchSchema.parse(input.settings)); await refreshScheduler(); return result } + throw new McpError("UNSUPPORTED_CAPABILITY", "Неизвестный сервис", 409) +} +export async function verifyStep(step: ChangeStep, result: unknown, before?: RosSnapshot, after?: RosSnapshot): Promise { + if (step.kind === "service") { + const custom = findMcpService(step.service) + if (custom?.verify) return custom.verify(step.input, result) + if (step.service === "users.delete") return !await getUserById(String(step.input.id)) + if (step.service === "users.create" || step.service === "users.update") { + const id = step.service === "users.create" && result && typeof result === "object" ? String((result as { id?: string }).id ?? "") : String(step.input.id) + const current = await getUserById(id) + if (!current) return false + const expected = step.service === "users.create" ? appUserCreateSchema.parse(step.input.user) : appUserUpdateSchema.parse(step.input.user) + return Object.entries(expected).every(([key, value]) => { + if (key === "avatar" && typeof value === "string" && !value.trim()) return true // Existing service derives initials or retains the prior avatar. + const normalized = ["name", "login", "email", "avatar"].includes(key) && typeof value === "string" ? value.trim() : value + return fingerprint((current as unknown as Record)[key]) === fingerprint(normalized) + }) + } + if (step.service === "users.binding_remove") { const current = await getUserById(String(step.input.id)); return !!current && !current.bindings.some(binding => binding.id === step.input.bindingId) } + if (step.service === "users.binding_add") { + const binding = userBindingCreateSchema.parse(step.input.binding), current = await getUserById(String(step.input.id)) + const createdId = result && typeof result === "object" ? (result as { id?: unknown }).id : undefined + return typeof createdId === "string" && !!current?.bindings.some(item => item.id === createdId + && item.serverId === binding.serverId && item.interfaceName === binding.interfaceName.trim() + && (binding.interfaceType === undefined || item.interfaceType === binding.interfaceType) + && item.peerPublicKey === normalizeBindingPeer(item.interfaceType, binding.peerPublicKey)) + } + if (step.service === "logs.settings.update") { + const current = await getLogSettings() as unknown as Record + return Object.entries(logSettingsPatchSchema.parse(step.input.settings)).every(([key, value]) => current[key] === value) + } + // Service methods confirm their own DB changes; the receipt remains inspectable. + return result !== undefined || step.service === "users.binding_remove" + } + assertNoCallerCreateId(step.step) + const id = step.step.action === "create" ? acknowledgedCreateId(result) : step.step.id + if (step.step.action === "create" && !id) return false + 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 { + return withMcpDatabaseRequest(() => admitPlan(principal, planId, idempotencyKey)) +} +async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey: string): Promise { + const operationAdmissionStart = beginMcpOperationAdmission(principal) + let reservedRouting = false + let originalServiceGrant:OriginalServiceGrant|undefined + let operationAdmission: McpOperationAdmission | undefined + const admitted = await withRoutingReservationAdmission(async (client, reserveRouting) => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [principal.connectionId]) + const connection = await validatePrincipal(principal, client) + const { rows: [existing] } = await client.query("SELECT * FROM mcp_operations WHERE connection_id=$1 AND (idempotency_key=$2 OR plan_id=$3) ORDER BY created_at LIMIT 1", [connection.id, idempotencyKey, planId]) + if (existing) { + if (existing.plan_id !== planId) throw new McpError("STATE_CONFLICT", "Ключ уже используется другим планом", 409) + const original = await loadPlan(planId, client) + const originalEvidence=JSON.parse(await decryptSecret(original.evidence.sealed)) as ChangeEvidence + reservedRouting = !!await routingReservationEvidence(original, originalEvidence) + return existing + } + await refuseExpiredOperationReplay(client, principal, connection.id, planId, idempotencyKey) + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-runner-admission',0))") + const pending = await client.query<{ count: number }>("SELECT count(*)::int AS count FROM mcp_operations WHERE state IN ('pending','running')") + if (pending.rows[0].count >= 100) throw new McpError("BUDGET_EXCEEDED", "Очередь исполнителя заполнена; повторите подготовленное применение позже", 429) + const selected = await lockRoutingReservationAdmissionSource(client, await loadPlan(planId, client), principal) + const { plan, evidence } = selected + reservedRouting = selected.reserved + if (plan.connection_id !== connection.id) throw new McpError("NOT_FOUND", "План не найден", 404) + 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) + 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") + } + } + 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) + scopedPathExpiresAt = await readScopedRoutingPathAdmissionDeadline(client, evidence, connection.id) + } + if (evidence.validUntil && (!Number.isFinite(Date.parse(evidence.validUntil)) || Date.now() >= Date.parse(evidence.validUntil))) { + throw new McpError("MEASUREMENT_UNKNOWN", "Измерения плана устарели; подготовьте новый план", 409) + } + if (evidence.recovery) { + if (Date.now() >= Date.parse(evidence.recovery.validUntil)) throw new McpError("PLAN_EXPIRED", "Срок зарезервированного восстановления истёк", 409) + const { rows: [grant] } = await client.query("SELECT config FROM mcp_routing_profiles WHERE id=$1 AND connection_id=$2 AND credential_id=$3 AND enabled AND grant_expires_at>now() AND policy_version=$4 AND generation=$5", [evidence.recovery.authorityProfileId, connection.id, principal.credentialId, connection.policy.version, await accessGeneration()]) + const step = evidence.recovery.step + if (!grant?.config?.autoRecovery || step.kind !== "service" || fingerprint(grant.config) !== fingerprint(step.input.profile)) throw new McpError("PERMISSION_DENIED", "Допуск восстановления изменён или отозван") + } + const input = await policyInput(client, connection, action.operation.id, evidence.target, evidence) + input.temporaryGrant = await temporaryScope(client, connection, principal.credentialId) + // tools/call already reserved its slot. A full minute bucket must not reject + // the very call that legitimately consumed the last slot. + input.usage.callsInMinute = Math.max(0, input.usage.callsInMinute - 1) + const decision = evaluateMcpPolicy(input) + if (decision.decision === "denied") throw new McpError(decision.reasons[0].code, decision.reasons.map(reason => reason.message).join("; ")) + if (decision.decision === "approval-required" && plan.state !== "approved") throw new McpError("APPROVAL_REQUIRED", "Требуется подтверждение человеком", 409) + // Whole-operation provenance pins the ORIGINAL admission grant for ROS and + // service plans alike; a later runner must never adopt a replacement grant. + { + const grantWall=Date.now(),grantMonotonic=performance.now() + const grant=await readMcpTemporaryGrantOrigin(client,connection.id,principal.credentialId) + const admissionExpiresAt = Math.min(plan.expires_at.getTime(), evidence.validUntil ? Date.parse(evidence.validUntil) : Infinity, + evidence.recovery ? Date.parse(evidence.recovery.validUntil) : Infinity, Date.parse(principal.credentialExpiresAt), + Date.parse(connection.policy.expiresAt), connection.owner_valid_until.getTime(), grant.expiresAt ?? Infinity, scopedPathExpiresAt ?? Infinity) + originalServiceGrant=Object.freeze({...grant,monotonicEnd:grant.expiresAt===null?Infinity:grantMonotonic+Math.max(0,grant.expiresAt-grantWall),admissionExpiresAt,admissionMonotonicEnd:grantMonotonic+Math.max(0,admissionExpiresAt-grantWall)}) + await assertOriginalMcpTemporaryGrant(client,connection.id,principal.credentialId,originalServiceGrant) + } + const mutationCost = reservedRouting ? 2 : changeMutationCost(evidence) + (evidence.recovery ? 1 : 0) + if (input.usage.mutationsInHour + mutationCost > connection.policy.limits.mutationsPerHour) throw new McpError("BUDGET_EXCEEDED", "Недостаточно бюджета для всех шагов плана", 429) + if (!reservedRouting) await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'mutations',date_trunc('hour',now()),$2) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+EXCLUDED.used", [connection.id, mutationCost]) + 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 }) + return row + }).catch(async error => { await retireMcpOperationAdmission(operationAdmission); throw error }) + const operation = admitted.value + // Reserved native dispatch remains closed until the common engine's bounded + // network verifier and complete positive/negative lifecycle are accepted. + // The atomic hold is durable; neither read nor another apply mints its cap. + if (operation.state === "pending" && !reservedRouting && originalServiceGrant) { + const operationKind = (await loadPlan(operation.plan_id)).operation_id + const terminal = operationKind === "database.restore" + setImmediate(() => { void (terminal || operationKind === "database.backup" ? runBounded(() => terminal + ? import("./database-restore.js").then(module => module.executeMcpDatabaseRestore(operation.id, principal,originalServiceGrant)) + : import("../../services/system-database-backup.js").then(module => module.withSystemDatabaseBackupAdmission(() => executeOperation(operation.id, principal,undefined,undefined,originalServiceGrant,operationAdmission)))) + : runOrdinaryOperation(() => executeOperation(operation.id, principal, admitted.reservation?.forwardAllocation, admitted.reservation?.reservationId,originalServiceGrant,operationAdmission))).catch(async () => { if (!terminal) await retireMcpOperationAdmission(operationAdmission) /* no retry; interrupted intents retain explicit UNKNOWN */ }) }) + } + return { ...operation, result: stripMcpOperationExecutionPrivateResult(operation.result) } +} +async function executeOperation(operationId: string, principal: McpPrincipal, routingReservationForward?: RoutingReservedForwardAllocation, routingReservationId?: string,originalServiceGrant?:OriginalServiceGrant, operationAdmission?: McpOperationAdmission): Promise { + return withMcpOperationExecutionOrigin(operationAdmission, async () => { + const lease = await pool.connect() + let terminalProbe: Awaited> + let locked = false, released = false + const ownerLifetime = new AbortController() + // This closure is the SINGLE owner of physical destruction/release. + const retire = () => { + if (released) return + released = true + ownerLifetime.abort() + try { lease.release(true) } catch { /* Never reuse an uncertain lock session. */ } + } + const onError = () => retire() + lease.on("error", onError) + try { + try { + locked = (await lease.query<{ locked: boolean }>("SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS locked", ["mcp-operation:" + operationId])).rows[0].locked + } catch (error) { + // The backend may hold the lock despite a rejected/lost acquire ACK. + // An error event is not required to make healthy reuse unsafe. + retire() + throw error + } + if (released || !locked) return + terminalProbe = await withOperationConfigurationLockClient(lease, retire, ownerLifetime.signal, + () => executeOperationWithLease(operationId, principal, routingReservationForward, routingReservationId, originalServiceGrant), mcpDatabaseRequestSignal()) + } finally { + if (!released && locked) { + try { await lease.query("SELECT pg_advisory_unlock(hashtextextended($1,0))", ["mcp-operation:" + operationId]) } + catch { retire() } + } + if (!released) { + // Keep the absorber on a destroyed client; remove only before a healthy + // release after EVERY borrowed lock facade/query has settled. + lease.removeListener("error", onError) + released = true + lease.release() + } + } + // Capture has its own bounded checkout and may fail without rewriting the + // acknowledged terminal outcome or redispatching the diagnostic. + if (terminalProbe) { + try { await capturePublishedServerProbeTerminalProof(terminalProbe) } catch { /* Existing retention remains authoritative. */ } + } + }) +} +async function executeOperationWithLease(operationId: string, principal: McpPrincipal, routingReservationForward?: RoutingReservedForwardAllocation, routingReservationId?: string,originalServiceGrant?:OriginalServiceGrant) { + const { rows: [operation] } = await pool.query("SELECT * FROM mcp_operations WHERE id=$1 AND state='running'", [operationId]) + if (!operation) return + const plan = await loadPlan(operation.plan_id), evidence = JSON.parse(await decryptSecret(plan.evidence.sealed)) as ChangeEvidence + const serverIds = evidence.target.scope === "servers" ? evidence.target.serverIds : evidence.objects.flatMap(object => object.serverId ? [object.serverId] : []) + let attempted = false, confirmed = 0, activeServiceStep = false + let ecmpStoppedBeforeDispatch = false + let vrfDiagnosticFallback: ReturnType | undefined + let reservedForwardPassed = false + let reservedRecoverySummary: Record | undefined + const diagnosticOnly = evidence.steps.length === 1 && evidence.steps.every(step => step.kind === "service" && findMcpService(step.service)?.cancellableDiagnostic === true) + const productionVrf = evidence.vrfProductionAdmission === true + let diagnosticReceipt: ReturnType + const authorizeGrant = async () => { + assertOperationConfigurationLockScope() + if (plan.operation_version !== findActionByOperation(plan.operation_id).operation.version) throw new McpError("PLAN_EXPIRED", "Версия операции изменилась; подготовьте новый план", 409) + const connection = await validatePrincipal(principal) + assertOperationConfigurationLockScope() + if (connection.policy.version !== plan.policy_version) throw new McpError("STATE_CONFLICT", "Политика изменилась во время операции", 409) + return authorizeAfterPrincipal(connection) + } + const authorizeAfterPrincipal = async (connection: ConnectionRow, policyLoan?: RuntimePollPolicyLoan) => { + assertOperationConfigurationLockScope() + if (plan.operation_version !== findActionByOperation(plan.operation_id).operation.version) throw new McpError("PLAN_EXPIRED", "Версия операции изменилась; подготовьте новый план", 409) + if (connection.policy.version !== plan.policy_version) throw new McpError("STATE_CONFLICT", "Политика изменилась во время операции", 409) + const checkPolicy = async (client: Pick) => { + assertOperationConfigurationLockScope() + if(evidence.steps.some(step=>step.kind==="service")){ + await assertOriginalMcpTemporaryGrant(client,connection.id,principal.credentialId,originalServiceGrant) + assertOperationConfigurationLockScope() + } + const pollSource = plan.operation_id === "servers.poll" + ? await readPollRuntimePolicySource(client, connection.id, principal.credentialId) : undefined + assertOperationConfigurationLockScope() + const policy = await policyInput(client, connection, plan.operation_id, evidence.target, evidence, pollSource) + assertOperationConfigurationLockScope() + policy.temporaryGrant = pollSource ? pollSource.temporaryGrant : await temporaryScope(client, connection, principal.credentialId) + assertOperationConfigurationLockScope() + // Budget was reserved for the complete batch at admission. + 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 (policyLoan) await executeRuntimePollPolicyTransaction(policyLoan, checkPolicy) + else await transaction(checkPolicy) + assertOperationConfigurationLockScope() + await verifyRoutingObservationChecks(evidence.observationChecks) + assertOperationConfigurationLockScope() + if(evidence.steps.some(step=>step.kind==="service"))await assertOriginalMcpTemporaryGrant(pool,connection.id,principal.credentialId,originalServiceGrant) + assertOperationConfigurationLockScope() + return connection + } + const authorize = async () => { + assertOperationConfigurationLockScope() + const connection = await authorizeGrant() + assertOperationConfigurationLockScope() + await verifyOspfOwnerGraph(evidence.ospfOwnerGraph, serverIds, authorizeGrant) + assertOperationConfigurationLockScope() + return connection + } + let ownerSources: (() => Promise) | undefined + const authorizeCatalog = async () => { + assertOperationConfigurationLockScope() + const connection = await authorizeGrant() + assertOperationConfigurationLockScope() + ownerSources ??= createOspfOwnerSourceGuard(evidence.ospfOwnerGraph, serverIds) + await ownerSources() + assertOperationConfigurationLockScope() + return connection + } + try { + if (productionVrf && (evidence.steps.length !== 1 || evidence.steps[0].kind !== "ros" || evidence.steps[0].step.resource !== "vrf")) throw new McpError("STATE_CONFLICT", "VRF execution требует exact single typed step", 409) + if (await routingReservationEvidence(plan, evidence) && (!routingReservationForward || !routingReservationId)) throw new McpError("STATE_CONFLICT", "No confirmed reserved forward admission", 409) + if (plan.operation_version !== findActionByOperation(plan.operation_id).operation.version) throw new McpError("PLAN_EXPIRED", "Версия операции изменилась до запуска", 409) + const checksResolvedNextHop = evidence.healthChecks?.some(check => check.field === "resolved") === true + if (plan.operation_id === "operations.recover" && !checksResolvedNextHop && evidence.steps.some(change => change.kind === "ros" && ["ipv4_route", "ipv6_route"].includes(change.step.resource))) { + const { operationId: sourceId } = JSON.parse(await decryptSecret(plan.payload.sealed)) as { operationId: string } + const { rows: [source] } = await pool.query("SELECT p.operation_id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.connection_id=$2", [sourceId, plan.connection_id]) + if (!source || source.operation_id === "routing.ecmp.update") throw new McpError("PLAN_EXPIRED", "Подготовьте новый обратный план с проверкой next-hop", 409) + } + const applyWithLocks = () => withConfigMutationLock(serverIds, async () => { + await authorize() + const prefix = evidence.steps.some(step => step.kind === "ros") ? createRosPrefix(productionVrf ? evidence.snapshots.filter(snapshot => !isVrfCompanionResource(snapshot.resource)) : evidence.snapshots) : undefined + 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") + } + } + if (prefix) { + // An ordinary single-step plan has no device observation before its pre-intent check. + const needsEntrySnapshot = productionVrf || evidence.steps.length !== 1 || + evidence.healthChecks?.length || evidence.managementChannels?.length || + evidence.observationChecks?.length || evidence.ospfOwnerGraph || evidence.recovery + if (needsEntrySnapshot) await prefix.check(authorizeCatalog) + } else for (const snapshot of evidence.snapshots) { + if ((await readRosSnapshot(snapshot.serverId, snapshot.resource)).fingerprint !== snapshot.fingerprint) throw new McpError("STATE_CONFLICT", "Конфигурация изменилась после подготовки", 409) + } + await verifyHealthChecks(evidence, serverIds, false, authorizeCatalog) + for (const step of evidence.steps) { + if (step.kind === "service" && fingerprint(await serviceCurrent(step.service, step.input)) !== fingerprint(evidence.before)) throw new McpError("STATE_CONFLICT", "Данные сервиса изменились", 409) + } + // Deterministic one-server stages. Each successful write is verified before the next. + for (let index = 0; index < evidence.steps.length; index++) { + if (evidence.recovery && Date.now() >= Date.parse(evidence.recovery.validUntil)) throw new McpError("PLAN_EXPIRED", "Срок гарантированного восстановления истёк до записи", 409) + for (const channel of evidence.managementChannels ?? []) { + if (!serverIds.includes(channel.serverId)) throw new McpError("SERVER_DENIED", "Management-проверка вне плана") + await verifyManagementChannel(channel.serverId, channel.channelId) + } + if (evidence.validUntil && (!Number.isFinite(Date.parse(evidence.validUntil)) || Date.now() >= Date.parse(evidence.validUntil))) throw new McpError("MEASUREMENT_UNKNOWN", "Измерения плана устарели; подготовьте новый план", 409) + const connection = await authorize() + const step = evidence.steps[index] + let vrfBeforeDispatch: (() => Promise) | undefined + if (step.kind === "ros") { + await prefix!.check(authorizeCatalog) + const before = productionVrf ? evidence.snapshots.find(snapshot => snapshot.resource === "vrf" && snapshot.serverId === step.step.serverId) : prefix!.before(step.step) + if (!before) throw new McpError("STATE_CONFLICT", "Нет исходного VRF source snapshot", 409) + await pool.query("INSERT INTO mcp_operation_checkpoints(operation_id,step_index,before_sealed) VALUES($1,$2,$3)", [operationId, index, await encryptSecret(JSON.stringify(before))]) + } else { + await pool.query("INSERT INTO mcp_operation_checkpoints(operation_id,step_index,before_sealed) VALUES($1,$2,$3)", [operationId, index, await encryptSecret(JSON.stringify(evidence.snapshots))]) + } + await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,server_id,state,intended_state) VALUES($1,$2,$3,'intent',$4::jsonb)", [operationId, index, step.kind === "ros" ? step.step.serverId : null, JSON.stringify(redact(step))]) + attempted = true + activeServiceStep = step.kind === "service" + const serviceExecution=step.kind==="service"?{operationId,planId:plan.id,operationName:plan.operation_id,serviceName:step.service,stepIndex:index,phase:"forward" as const, + operationVersion:plan.operation_version,policyVersion:plan.policy_version,generation:plan.generation,principal:{...principal}, + originalTemporaryGrantProof:originalServiceGrant!.proof,originalTemporaryGrantExpiresAt:originalServiceGrant!.expiresAt, + sealedEvidenceProof:createHash("sha256").update(plan.evidence.sealed).digest("hex"),sealedPayloadProof:createHash("sha256").update(plan.payload.sealed).digest("hex"), + originalExpiresAt:Math.min(plan.expires_at.getTime(),evidence.validUntil?Date.parse(evidence.validUntil):Infinity,Date.parse(principal.credentialExpiresAt),Date.parse(connection.policy.expiresAt),connection.owner_valid_until.getTime(),originalServiceGrant!.admissionExpiresAt), + serviceInput:step.input}:undefined + const reservedStep: { outcome?: ReservedRoutingStepOutcome } = {} + vrfDiagnosticFallback = productionVrf && step.kind === "ros" && step.step.resource === "vrf" && ["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete", "routing.vrf.update"].includes(plan.operation_id) + ? createVrfRuntimeDiagnosticBoundary(operationId, step.step.action === "update" ? "patch" : step.step.action, diagnostic => { console.error(JSON.stringify(diagnostic)) }) : undefined + const beforeDispatch = step.kind === "ros" ? async () => { + await prefix!.check(authorizeCatalog) + if (checksResolvedNextHop) { + try { + await verifyHealthChecks(evidence, serverIds, false, authorizeCatalog) + } catch (error) { + ecmpStoppedBeforeDispatch = true + throw error + } + } + await authorize() + await authorizeCatalog() + if (productionVrf) { + if (!vrfBeforeDispatch) throw new McpError("STATE_CONFLICT", "VRF command guard отсутствует", 409) + await vrfBeforeDispatch() + } + } : 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()) }, beforeDispatch }, + async () => { + vrfDiagnosticFallback?.executorEntered() + if (step.kind === "ros") { + if (!productionVrf) return executeRosStep(step.step) + const before = evidence.snapshots.find(snapshot => snapshot.resource === "vrf" && snapshot.serverId === step.step.serverId) + if (!before?.sourceProof) throw new McpError("STATE_CONFLICT", "Нет VRF source-v3 proof", 409) + if (!["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete", "routing.vrf.update"].includes(plan.operation_id)) throw new McpError("STATE_CONFLICT", "VRF action binding изменён", 409) + const receipt = await executeVrfProductionStep({ operationId, planId: plan.id, connectionId: connection.id, ownerId: connection.owner_id, + operationName: plan.operation_id as "ros.vrf.create" | "ros.vrf.update" | "ros.vrf.delete" | "routing.vrf.update", operationVersion: plan.operation_version, + stepIndex: index, step: step.step, sourceProof: before.sourceProof, originalSnapshots: evidence.snapshots, authorize: authorizeCatalog, + assertDependencies: () => prefix!.check(authorizeCatalog), installBeforeDispatch: guard => { vrfBeforeDispatch = guard }, + onDiagnostic: diagnostic => { vrfDiagnosticFallback?.observed(diagnostic) } }) + vrfDiagnosticFallback?.executorReturned() + return receipt + } + const forward = (retainRoutingNetworkSession?: (session: RoutingReservedNetworkSession) => void) => + serviceExecute(step.service, step.input, { operationId, phase: "forward", stepIndex: index, + ...(routingReservationForward ? { routingReservationForward, retainRoutingNetworkSession } : {}) }) + if (!routingReservationId) return step.service === "servers.poll" ? withRuntimePollPrincipalSource(principal, async (current, policyLoan) => { + assertOperationConfigurationLockScope() + await authorizeAfterPrincipal(current, policyLoan) + assertOperationConfigurationLockScope() + await verifyOspfOwnerGraph(evidence.ospfOwnerGraph, serverIds, authorizeGrant) + assertOperationConfigurationLockScope() + }, forward) : forward() + reservedStep.outcome = await executeReservedRoutingStep({ reservationId: routingReservationId, sourceOperationId: operationId, principal, forward }) + return reservedStep.outcome.forwardResult + }) + vrfDiagnosticFallback = undefined + if (reservedStep.outcome?.kind === "recovered") { + // The generic source remains intent/result=null until the private + // inverse has finished. Record the forward ACK, never B VERIFIED + // after restoring A. No second operation or mutation debit is added. + confirmed++ + reservedRecoverySummary = { state: "verified", mode: "same-slot", outcome: "recovered", nativeWrites: 1, + configurationVerified: true, controlProbeVerified: true, forwardingVerified: false, + recoveryCheckpointProof: reservedStep.outcome.recoveryResult.recoveryCheckpointProof } + await pool.query("UPDATE mcp_operation_steps SET state='applied',result=$3::jsonb,finished_at=now() WHERE operation_id=$1 AND step_index=$2", [operationId, index, JSON.stringify(redact(result ?? null))]) + throw new McpError("VERIFICATION_FAILED", "Forward не прошёл сетевую проверку; исходная конфигурация восстановлена и проверена", 409) + } + if (reservedStep.outcome?.kind === "forwardPassed") reservedForwardPassed = true + if (diagnosticOnly) { + diagnosticReceipt = serviceDiagnosticReceipt(result, true, operationId, evidence.mutationCost) + if (!diagnosticReceipt) throw new McpError("VERIFICATION_FAILED", "Квитанция этапов диагностики не подтверждена", 409) + } + confirmed++ + await pool.query("UPDATE mcp_operation_steps SET state='applied',result=$3::jsonb,finished_at=now() WHERE operation_id=$1 AND step_index=$2", [operationId, index, JSON.stringify(redact(result ?? null))]) + if (step.kind === "service") { + const after = await Promise.all(evidence.snapshots.map(snapshot => readRosSnapshot(snapshot.serverId, snapshot.resource))) + await pool.query("UPDATE mcp_operation_checkpoints SET after_sealed=$3 WHERE operation_id=$1 AND step_index=$2", [operationId, index, await encryptSecret(JSON.stringify(after))]) + } + if (step.kind === "ros" && productionVrf) { + await prefix!.check(authorizeCatalog) + } else if (step.kind === "ros") { + const after = await readRosSnapshot(step.step.serverId, step.step.resource, authorizeCatalog, prefix!.before(step.step).sourceProof) + await prefix!.advance(step.step, result, after, authorizeCatalog) + await verifyOspfOwnerGraphAtPhase(evidence.ospfOwnerGraph, serverIds, authorizeGrant, "post-ack") + await authorizeCatalog() + await pool.query("UPDATE mcp_operation_checkpoints SET after_sealed=$3 WHERE operation_id=$1 AND step_index=$2", [operationId, index, await encryptSecret(JSON.stringify(after))]) + } else if (!await verifyStep(step, result)) throw new McpError("VERIFICATION_FAILED", "Запись подтверждена, но проверка результата не прошла", 409) + await pool.query("UPDATE mcp_operation_steps SET state=$3 WHERE operation_id=$1 AND step_index=$2", [operationId, index, evidence.verificationScope === "acknowledgement" ? "acknowledged" : "verified"]) + } + await verifyHealthChecks(evidence, serverIds, true, authorizeCatalog) + await prefix?.check(authorizeCatalog) + if (evidence.ospfOwnerGraph) { + await authorizeGrant() + await verifyOspfOwnerGraphAtPhase(evidence.ospfOwnerGraph, serverIds, authorizeGrant, confirmed > 0 ? "post-ack" : "admission") + await authorizeCatalog() + } + }) + await (evidence.steps.some(step => step.kind === "service") ? withApplicationMutationLock(applyWithLocks) : applyWithLocks()) + assertOperationConfigurationLockCheckpoint() + const receiptId = plan.operation_id === SERVER_PROBE_OPERATION ? diagnosticReceipt?.receiptId : undefined + const finishedAt = await transaction(async client => { + assertOperationConfigurationLockCheckpoint() + const terminal = await client.query<{ finished_at: string }>("UPDATE mcp_operations SET state='applied',verification=$3,result=$2::jsonb,finished_at=now() WHERE id=$1" + + (receiptId ? ` RETURNING to_char(finished_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS finished_at` : ""), + [operationId, JSON.stringify({ confirmedWrites: diagnosticOnly ? 0 : confirmed, verificationScope: evidence.verificationScope ?? "configuration", ...(diagnosticOnly ? { completedServiceSteps: confirmed, diagnosticReceipt } : {}) }), evidence.verificationScope === "acknowledgement" ? "inconclusive" : "passed"]) + assertOperationConfigurationLockCheckpoint() + await client.query("UPDATE mcp_plans SET state='completed' WHERE id=$1", [plan.id]) + assertOperationConfigurationLockCheckpoint() + await audit(client, "mcp-runner", "operation.complete", operation.connection_id, { operationId, confirmedWrites: diagnosticOnly ? 0 : confirmed, ...(diagnosticOnly ? { completedServiceSteps: confirmed, diagnosticReceipt } : {}) }) + assertOperationConfigurationLockCheckpoint() + return receiptId ? terminal.rows[0]?.finished_at : undefined + }) + assertOperationConfigurationLockCheckpoint() + if (routingReservationId && reservedForwardPassed) { + // Requires the real terminal applied + generic VERIFIED source. Failure + // conservatively retains the held inverse and both original charges. + await createRoutingReservationStore().proveUnusedInverse(routingReservationId).catch(() => {}) + } + assertOperationConfigurationLockCheckpoint() + if (receiptId && finishedAt) return { receiptId, operationId, connectionId:operation.connection_id, finishedAt } + } catch (error) { + vrfDiagnosticFallback?.failure(error) + if (routingReservationId && hasConfirmedReservedRoutingForward(error)) confirmed = Math.max(confirmed, 1) + if (error instanceof VrfProductionAcknowledgedFailure) confirmed = Math.max(confirmed, error.acknowledgedWrites) + const code = error instanceof VrfProductionClaimOutcomeUnknown ? "VRF_CLAIM_OUTCOME_UNKNOWN" : error instanceof McpError ? error.code : "DEVICE_UNAVAILABLE" + const nativeProgress = serviceNativeProgress(error, activeServiceStep, evidence.mutationCost) + const diagnosticProgress = serviceDiagnosticProgress(error, diagnosticOnly && activeServiceStep, operationId, evidence.mutationCost) + const diagnosticNotStarted = diagnosticOnly && confirmed === 0 && code === "READ_CANCELLED" && + (error as { diagnosticNotStarted?: unknown } | null)?.diagnosticNotStarted === true && + diagnosticProgress?.state === "cancelled" && diagnosticProgress.attemptedStages === 0 && diagnosticProgress.completedStages === 0 + const state = ecmpStoppedBeforeDispatch ? confirmed === 0 ? "rejected" : "partial" + : !attempted || diagnosticNotStarted ? "rejected" : code === "VERIFICATION_FAILED" ? "partial" : "unknown" + const dispatchOutcome = ecmpStoppedBeforeDispatch ? { failedStepDispatched: false } : {} + // Private operator STDERR only; never HTTP/result/audit payload. + try { + const [step] = evidence.steps + const manualRouting = !attempted && plan.operation_id === "routing.community.switch" && evidence.steps.length === 1 && + step?.kind === "service" && step.service === "routing.community.scoped" && step.input.proofRevision === 2 + const diagnostic = manualRouting ? manualRoutingPredispatchDiagnostic(error, operationId) : pollErrorLog(error, operationId, plan.operation_id) + if (diagnostic) console.error(JSON.stringify(diagnostic)) + } catch { /* Logging cannot replace operation outcome. */ } + const recovery = reservedRecoverySummary ?? (code === "VERIFICATION_FAILED" && confirmed === evidence.steps.length && evidence.recovery + ? await performReservedRecovery(operation, plan, evidence, principal, serverIds) + : undefined) + const persistFailure = mcpDatabaseRequestSignal()?.aborted ? requestAccountingWrite : transaction + await persistFailure(async client => { + await client.query("UPDATE mcp_operations SET state=$2,verification=$3,error_code=$4,result=$5::jsonb,finished_at=now() WHERE id=$1", [operationId, state, code === "VERIFICATION_FAILED" ? "failed" : "inconclusive", code, JSON.stringify({ confirmedWrites: diagnosticOnly ? 0 : confirmed, needsReview: state !== "rejected", ...dispatchOutcome, recovery, ...(nativeProgress ? { nativeProgress } : {}), ...(diagnosticProgress ? { diagnosticProgress } : {}), ...(diagnosticOnly ? { completedServiceSteps: confirmed } : {}) })]) + await client.query("UPDATE mcp_plans SET state='completed' WHERE id=$1", [plan.id]) + await audit(client, "mcp-runner", "operation.stop", operation.connection_id, { operationId, state, code, confirmedWrites: diagnosticOnly ? 0 : confirmed, ...dispatchOutcome, ...(nativeProgress ? { nativeProgress } : {}), ...(diagnosticProgress ? { diagnosticProgress } : {}) }) + }) + } +} +async function performReservedRecovery(operation: StoredOperation, plan: StoredPlan, evidence: ChangeEvidence, principal: McpPrincipal, serverIds: number[]) { + const recovery = evidence.recovery! + let attempted = false + try { + return await withApplicationMutationLock(() => withConfigMutationLock(serverIds, async () => { + if (Date.now() >= Date.parse(recovery.validUntil)) throw new McpError("PLAN_EXPIRED", "Зарезервированный срок восстановления истёк", 409) + const connection = await validatePrincipal(principal) + if (connection.policy.version !== plan.policy_version) throw new McpError("STATE_CONFLICT", "Политика изменилась; допуск восстановления недействителен", 409) + const { rows: [grant] } = await pool.query("SELECT config FROM mcp_routing_profiles WHERE id=$1 AND connection_id=$2 AND credential_id=$3 AND enabled AND grant_expires_at>now() AND policy_version=$4 AND generation=$5", [recovery.authorityProfileId, connection.id, principal.credentialId, connection.policy.version, await accessGeneration()]) + if (!grant?.config?.autoRecovery || recovery.step.kind !== "service" || recovery.step.service !== "routing.community.restore") throw new McpError("PERMISSION_DENIED", "Нет действующего допуска этого восстановления") + const recoveryProfile = recovery.step.input.profile + if (fingerprint(grant.config) !== fingerprint(recoveryProfile)) throw new McpError("STATE_CONFLICT", "Профиль восстановления изменился", 409) + await transaction(async client => { + const input = await policyInput(client, connection, plan.operation_id, evidence.target, evidence) + 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", "Текущая защита или допуск запрещает восстановление") + }) + for (const channel of evidence.managementChannels ?? []) await verifyManagementChannel(channel.serverId, channel.channelId) + if (!evidence.managementChannels?.length) throw new McpError("APPROVAL_REQUIRED", "Восстановление требует независимого канала") + for (const [index, step] of evidence.steps.entries()) { + const { rows: [checkpoint] } = await pool.query("SELECT after_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1 AND step_index=$2", [operation.id, index]) + if (!checkpoint?.after_sealed) throw new McpError("STATE_CONFLICT", "Нет подтверждённого снимка после исходного шага", 409) + const raw = JSON.parse(await decryptSecret(checkpoint.after_sealed)) + const snapshots = step.kind === "ros" ? [raw] : raw + for (const snapshot of snapshots) if ((await readRosSnapshot(snapshot.serverId, snapshot.resource)).fingerprint !== snapshot.fingerprint) throw new McpError("STATE_CONFLICT", "Зависимость изменилась после записи; восстановление запрещено", 409) + } + if (fingerprint(await serviceCurrent(recovery.step.service, recovery.step.input)) !== fingerprint(recovery.expected)) throw new McpError("STATE_CONFLICT", "Конфигурация изменилась извне; восстановление остановлено", 409) + const intent = await pool.query("INSERT INTO mcp_recovery_intents(operation_id,state,authority_profile_id) VALUES($1,'intent',$2) ON CONFLICT DO NOTHING RETURNING operation_id", [operation.id, recovery.authorityProfileId]) + if (!intent.rowCount) throw new McpError("STATE_CONFLICT", "Восстановление уже начиналось и не повторяется", 409) + 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" })) + 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 }) + return { state: "verified", authorityProfileId: recovery.authorityProfileId } + })) + } catch (error) { + if (attempted) await pool.query("UPDATE mcp_recovery_intents SET state='unknown',finished_at=now() WHERE operation_id=$1 AND state='intent'", [operation.id]).catch(() => {}) + return { state: attempted ? "unknown" : "blocked", errorCode: error instanceof McpError ? error.code : "STATE_UNAVAILABLE", automaticRetry: false } + } +} +export async function recoverMcpOperations() { + // A second backend must not interrupt a runner still holding its session lease. + // A pending operation can still be queued behind maintenance. Its original + // plan expiry bounds that wait across processes without granting a new lease. + const interrupted = `o.created_at { + const { rows: [lock] } = await client.query("SELECT pg_try_advisory_xact_lock(hashtextextended($1,0)) AS locked", ["mcp-operation:" + row.id]) + if (!lock.locked) return + // Defer the whole transition when either row is busy. + const { rows: [target] } = await client.query(`SELECT o.plan_id,o.connection_id + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id + WHERE o.id=$1 AND ${interrupted} + FOR UPDATE OF o,p SKIP LOCKED`, [row.id]) + if (!target) return + await client.query("UPDATE mcp_operations SET state='unknown',verification='inconclusive',error_code='PROCESS_INTERRUPTED',finished_at=now() WHERE id=$1", [row.id]) + await client.query("UPDATE mcp_plans SET state='completed' WHERE id=$1", [target.plan_id]) + await audit(client, "mcp-recovery", "operation.interrupted", target.connection_id, { operationId: row.id, retryAllowed: false }) + }) + } + await withBackgroundPartitionWrite(({ client }) => client.query(`WITH expired AS ( + SELECT id FROM mcp_plans + WHERE expires_at { + const diagnostic = partitionWriteFailureDiagnostic(error) + if (diagnostic) { + try { console.error(JSON.stringify({ event: "mcp.worker.plan-expiry-failed", ...diagnostic })) } + catch { /* Preserve the original transaction failure. */ } + } + throw error + }) + await withBackgroundPartitionWrite(({ client }) => client.query(`WITH expired AS ( + SELECT id FROM mcp_read_leases WHERE expires_at client.query(`WITH interrupted AS ( + SELECT intent.operation_id FROM mcp_recovery_intents intent + JOIN mcp_operations operation ON operation.id=intent.operation_id + WHERE intent.state='intent' AND operation.state NOT IN ('pending','running') + ORDER BY intent.created_at,intent.operation_id LIMIT 1000 FOR UPDATE OF intent SKIP LOCKED + ) + UPDATE mcp_recovery_intents SET state='unknown',finished_at=now() FROM interrupted + WHERE mcp_recovery_intents.operation_id=interrupted.operation_id`)) +} diff --git a/backend/src/modules/mcp/scoped-checkpoint-adapters-http.test.ts b/backend/src/modules/mcp/scoped-checkpoint-adapters-http.test.ts new file mode 100644 index 00000000..b0edbf68 --- /dev/null +++ b/backend/src/modules/mcp/scoped-checkpoint-adapters-http.test.ts @@ -0,0 +1,22 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-scoped-checkpoint-acceptance-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const actor = { id: "scoped-checkpoint-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:filters:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({ enabled: true }, actor) + console.log("Routing controller CAS SQL:", await (await import("./routing-controller-cas-acceptance.js")).acceptRoutingControllerCas()) + await (await import("./routing-community-scoped-acceptance.js")).acceptScopedRouteCommunitySwitch(actor) + await (await import("../../test/mcp-server-connection.js")).verifyMcpServerConnection(actor) + await (await import("./ipsec-peer-delete-acceptance.js")).acceptIpsecPeerDeletion(actor) +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await closePool() +} diff --git a/backend/src/modules/mcp/scoped-routing-diagnostics-http.test.ts b/backend/src/modules/mcp/scoped-routing-diagnostics-http.test.ts new file mode 100644 index 00000000..64116f39 --- /dev/null +++ b/backend/src/modules/mcp/scoped-routing-diagnostics-http.test.ts @@ -0,0 +1,28 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-scoped-routing-acceptance-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const { buildApp } = await import("../../index.js") +const actor = { id: "scoped-routing-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:filters:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +let app: Awaited> | undefined +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,enabled) VALUES(1,'owned-fixture','127.0.0.1',9,false,false)") + await pool.query("INSERT INTO geoip_settings(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false }); await app.ready() + await (await import("./routing-community-context-acceptance.js")).acceptRouteCommunityContextMcp(actor) + await (await import("./control-path-context-acceptance.js")).verifyMcpControlPathContext(actor) + await (await import("./routing-community-scoped-acceptance.js")).acceptScopedRouteCommunitySwitch(actor) + await (await import("../../test/mcp-terminal-diagnostics.js")).verifyMcpTerminalDiagnostics(actor, app) +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close(); await closePool() +} diff --git a/backend/src/modules/mcp/sdk-terminal-budget.test.ts b/backend/src/modules/mcp/sdk-terminal-budget.test.ts new file mode 100644 index 00000000..9ba27e2b --- /dev/null +++ b/backend/src/modules/mcp/sdk-terminal-budget.test.ts @@ -0,0 +1,55 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createSdkTerminalBudget } from "./sdk-terminal-budget.js" + +const info = { name: "MikrotikManager", version: "1.0.0" } +type Message = Parameters["beforeSend"]>[0] + +test("SDK-only terminals preserve ordinary messages and refuse a whole oversized error with its protocol code", () => { + const budget = createSdkTerminalBudget(info, { method: "tools/list", id: 3 }) + const ordinary = { jsonrpc: "2.0" as const, id: 3, result: { tools: [] } } + assert.equal(budget.beforeSend(ordinary), ordinary) + assert.equal(budget.auditNote(), undefined) + const excessive = { jsonrpc: "2.0" as const, id: 3, error: { code: -32602, message: "я".repeat(budget.plan.maximumJsonRpcBytes) } } + const refusal = budget.beforeSend(excessive) + assert("error" in refusal) + assert.equal(refusal.id, 3) + assert.equal(refusal.error.code, -32602) + assert.equal((refusal.error.data as { code: string }).code, "RESULT_TOO_LARGE") + assert(Buffer.byteLength(JSON.stringify(refusal)) < budget.plan.maximumJsonRpcBytes) + assert.equal(budget.auditNote()?.source, "connected-sdk-terminal") +}) + +test("entering any registered callback protects the original receipt without inspecting or serializing it", () => { + const budget = createSdkTerminalBudget(info, {}) + budget.registeredCallbackEntered() + const receipt = new Proxy({}, { get() { throw Error("Receipt inspected after callback") }, has() { throw Error("Receipt classified after callback") }, ownKeys() { throw Error("Receipt serialized after callback") } }) as Message + assert.equal(budget.beforeSend(receipt), receipt) + assert.equal(budget.auditNote(), undefined) +}) + +test("factoryless refusal preserves HTTP status and correlation; in-budget rejection bytes stay exact", async () => { + const budget = createSdkTerminalBudget(info, { method: "tools/call", id: "owned-id" }) + const original = '{"jsonrpc":"2.0","id":"owned-id","error":{"code":-32600,"message":"invalid"}}' + const bounded = await budget.factorylessJson(new Response(original, { status: 400, headers: { "content-type": "application/json", "x-owned": "preserved" } })) + assert.equal(await bounded.text(), original) + assert.equal(bounded.status, 400) + assert.equal(bounded.headers.get("x-owned"), "preserved") + const huge = new Response(JSON.stringify({ error: "x".repeat(budget.plan.maximumJsonRpcBytes) }), { status: 400, headers: { "content-type": "application/json", "content-length": "9999999", "x-owned": "preserved" } }) + const refusal = await budget.factorylessJson(huge) + assert.equal(refusal.status, 400) + assert.equal(refusal.headers.get("content-length"), null) + assert.equal(refusal.headers.get("x-owned"), "preserved") + const frame = await refusal.json() as { id: string; error: { data: { code: string } } } + assert.equal(frame.id, "owned-id") + assert.equal(frame.error.data.code, "RESULT_TOO_LARGE") +}) + +test("a factory-created response is returned untouched without consuming its stream", async () => { + const budget = createSdkTerminalBudget(info, {}) + budget.factoryEntered() + const response = new Response('{"result":"factual-receipt"}', { headers: { "content-type": "application/json" } }) + assert.equal(await budget.factorylessJson(response), response) + assert.equal(response.bodyUsed, false) + assert.equal(budget.auditNote(), undefined) +}) diff --git a/backend/src/modules/mcp/sdk-terminal-budget.ts b/backend/src/modules/mcp/sdk-terminal-budget.ts new file mode 100644 index 00000000..fe0f67c7 --- /dev/null +++ b/backend/src/modules/mcp/sdk-terminal-budget.ts @@ -0,0 +1,108 @@ +import { McpServer, type createMcpHandler } from "@modelcontextprotocol/server" +import { MCP_CALL_RESULT_BYTES } from "./result-budget.js" +import { MCP_REQUEST_BODY_BYTES } from "./wire-admission.js" +import { planMcpTerminalEnvelope } from "./flow-envelope-allowance.js" + +type SdkInfo = ConstructorParameters[0] +type SdkTransport = Parameters[0] +type SdkMessage = Parameters[0] +type SdkEntry = ReturnType +type Decision = Readonly<{ + source: "connected-sdk-terminal" | "factoryless-json" + reason: "byte-budget" | "not-json" + encodedBytesMeasured?: number + observedBytesLowerBound?: number +}> +/** Same echo policy as installed entry/Node adapter, NOT an ID acceptance rule. */ +function echoId(body: unknown): string | number | null { + if (!body || typeof body !== "object" || Array.isArray(body)) return null + const value = body as { method?: unknown; id?: unknown } + if (typeof value.method !== "string") return null + return typeof value.id === "string" || typeof value.id === "number" ? value.id : null +} +export function boundedSdkTerminalRefusal(id: Id, code = -32603, reason: Decision["reason"] = "byte-budget") { + return { jsonrpc: "2.0" as const, id, error: { code, message: reason === "not-json" ? "MCP reply cannot be encoded; narrow the granted operation scope or input" : "MCP reply exceeds its budget; narrow the granted operation scope or input", data: { code: reason === "not-json" ? "STATE_UNAVAILABLE" : "RESULT_TOO_LARGE", detailsOmitted: true, reason } } } +} +/** One admitted HTTP exchange only. No queue, timeout, keepalive change or STDIO + * request map. The constructor runs in guarded setup, BEFORE callbacks/effects. + */ +export function createSdkTerminalBudget(serverInfo: SdkInfo, admittedBody: unknown) { + const legacy = planMcpTerminalEnvelope(MCP_REQUEST_BODY_BYTES, "legacy", serverInfo) + const modern = planMcpTerminalEnvelope(MCP_REQUEST_BODY_BYTES, "modern", serverInfo) + const maximumJsonRpcBytes = MCP_CALL_RESULT_BYTES + Math.max(legacy.jsonRpcEnvelopeBytes, modern.jsonRpcEnvelopeBytes) + const plan = Object.freeze({ b1CallResultBytes: MCP_CALL_RESULT_BYTES, encodedIdBoundaryBytes: MCP_REQUEST_BODY_BYTES, + maximumJsonRpcBytes, maximumTerminalSseFrameBytes: maximumJsonRpcBytes + 23, modernStampBytes: modern.modernStamp }) + const fallbackId = echoId(admittedBody) + let factoryEntered = false, callbackEntered = false, decision: Decision | undefined + const note = (value: Decision) => { decision ??= Object.freeze(value) } + return { + serverInfo, plan, + factoryEntered() { factoryEntered = true }, + registeredCallbackEntered() { callbackEntered = true }, + auditNote: () => decision, + /** Already codec-encoded, before the actual SDK transport serialization. + * Protected branch MUST remain the first branch: no new stringify, shape + * inspection, byte rejection or replacement after ANY callback entered. + */ + beforeSend(message: SdkMessage): SdkMessage { + if (callbackEntered) return message + if (!("result" in message || "error" in message)) return message // notifications unchanged + try { + const encoded = JSON.stringify(message) + if (encoded === undefined) throw Error("SDK terminal is not JSON") + const bytes = Buffer.byteLength(encoded, "utf8") + if (bytes <= plan.maximumJsonRpcBytes) return message + note({ source: "connected-sdk-terminal", reason: "byte-budget", encodedBytesMeasured: bytes }) + } catch { note({ source: "connected-sdk-terminal", reason: "not-json" }) } + // SDK-only refusal, never a guessed outcome of a registered operation. + return boundedSdkTerminalRefusal(message.id, "error" in message ? message.error.code : -32603, decision?.reason) + }, + /** Only entry rejections where the factory NEVER ran. Never consume, clone, + * cancel or reserialize a factory-created response, especially effect SSE. + */ + async factorylessJson(response: Response): Promise { + if (factoryEntered || response.body === null || !response.headers.get("content-type")?.toLowerCase().startsWith("application/json")) return response + const reader = response.body.getReader(), chunks: Uint8Array[] = [] + let bytes = 0, over = false + try { + while (true) { + const next = await reader.read() + if (next.done) break + bytes += next.value.byteLength + if (bytes > plan.maximumJsonRpcBytes) { over = true; note({ source: "factoryless-json", reason: "byte-budget", observedBytesLowerBound: bytes }); try { await reader.cancel() } catch { /* no invocation; reporting remains bounded */ }; break } + chunks.push(next.value) + } + } finally { reader.releaseLock() } + if (!over) { + const body = new Uint8Array(bytes); let offset = 0 + for (const chunk of chunks) { body.set(chunk, offset); offset += chunk.byteLength } + return new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers }) + } + const headers = new Headers(response.headers) + headers.delete("content-length"); headers.delete("content-encoding"); headers.set("content-type", "application/json") + // Original rejection HTTP status/header semantics retained; ordinary + // in-budget SDK diagnostic bodies are byte-for-byte unchanged. + return new Response(JSON.stringify(boundedSdkTerminalRefusal(fallbackId)), { status: response.status, statusText: response.statusText, headers }) + }, + } +} +export type SdkTerminalBudget = ReturnType + +/** Public connect/send only. Keep transport identity and all duck-typed methods + * and callbacks intact; no SDK registry/codec/private field access. + */ +export class SdkBudgetedMcpServer extends McpServer { + private readonly guarded = new WeakSet() + constructor(private readonly budget: SdkTerminalBudget) { super(budget.serverInfo) } + override async connect(transport: SdkTransport): Promise { + if (!this.guarded.has(transport)) { + const send = transport.send.bind(transport) + transport.send = (message, options) => send(this.budget.beforeSend(message), options) + this.guarded.add(transport) + } + return super.connect(transport) + } +} +export function withFactorylessSdkJsonGuard(entry: SdkEntry, budget: SdkTerminalBudget): SdkEntry { + return { ...entry, fetch: async (request, options) => budget.factorylessJson(await entry.fetch(request, options)) } +} diff --git a/backend/src/modules/mcp/security.test.ts b/backend/src/modules/mcp/security.test.ts new file mode 100644 index 00000000..339104e5 --- /dev/null +++ b/backend/src/modules/mcp/security.test.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { redact, privateSecretFingerprint, tokenDigest, accessGeneration } from "./security.js" +import { mkdtemp, unlink, rmdir, writeFile, readFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" + +test("fast existing-state reads retain immediate revocation and corruption refusal", async () => { + const directory = await mkdtemp(join(tmpdir(), "mcp-current-state-")), previous = process.env.MCP_STATE_DIR + try { + process.env.MCP_STATE_DIR = directory + const original = await accessGeneration() + await writeFile(join(directory, "access-generation"), Buffer.from(original, "hex").map(value => value ^ 1)) + assert.notEqual(await accessGeneration(), original) + const proof = await privateSecretFingerprint("value", "fresh-key") + await writeFile(join(directory, "encryption-key"), Buffer.alloc(32, 7)) + assert.notEqual(await privateSecretFingerprint("value", "fresh-key"), proof) + await writeFile(join(directory, "access-generation"), Buffer.from("broken")) + await assert.rejects(accessGeneration(), { code: "STATE_UNAVAILABLE" }) + assert.equal((await readFile(join(directory, "access-generation"))).toString(), "broken") + } finally { + if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous + await unlink(join(directory, "access-generation")); await unlink(join(directory, "encryption-key")); await rmdir(directory) + } +}) + +test("routing authentication keys are masked in structured and textual results", () => { + const result = redact({ name: "ospf-lab", "auth-key": "fixture-ospf-key", nested: [{ "tcp-md5-key": "fixture-bgp-key", authenticationKey: "fixture-auth-key", authKeyRef: "reference-id" }], message: 'auth-key="fixture-text-key" tcp-md5-key=fixture-text-md5' }) as { name: string; nested: Record[]; message: string } + assert.equal(result.name, "ospf-lab") + assert.equal(result.nested[0].authKeyRef, "reference-id") + for (const secret of ["fixture-ospf-key", "fixture-bgp-key", "fixture-auth-key", "fixture-text-key", "fixture-text-md5"]) assert.ok(!JSON.stringify(result).includes(secret)) +}) + +test("credential proofs are stable on one installation and separated by secret, domain and installation", async () => { + const first=await mkdtemp(join(tmpdir(),"mcp-secret-proof-")),second=await mkdtemp(join(tmpdir(),"mcp-secret-proof-")),previous=process.env.MCP_STATE_DIR + try{ + process.env.MCP_STATE_DIR=first + const value=await privateSecretFingerprint("human-chosen-value","backup-storage") + assert.equal(await privateSecretFingerprint("human-chosen-value","backup-storage"),value) + assert.notEqual(value,tokenDigest("human-chosen-value")) + assert.notEqual(await privateSecretFingerprint("different-value","backup-storage"),value) + assert.notEqual(await privateSecretFingerprint("human-chosen-value","different-domain"),value) + process.env.MCP_STATE_DIR=second + assert.notEqual(await privateSecretFingerprint("human-chosen-value","backup-storage"),value) + }finally{ + if(previous===undefined)delete process.env.MCP_STATE_DIR;else process.env.MCP_STATE_DIR=previous + for(const directory of [first,second]){await unlink(join(directory,"encryption-key"));await rmdir(directory)} + } +}) diff --git a/backend/src/modules/mcp/security.ts b/backend/src/modules/mcp/security.ts new file mode 100644 index 00000000..06af6b69 --- /dev/null +++ b/backend/src/modules/mcp/security.ts @@ -0,0 +1,139 @@ +import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto" +import { mkdir, open, readFile, rename } from "node:fs/promises" +import path from "node:path" +import { env } from "../../config.js" + +export class McpError extends Error { + constructor(public readonly code: string, message: string, public readonly statusCode = 403) { super(message) } +} +export const tokenDigest = (token: string) => createHash("sha256").update(token).digest("hex") +export const randomToken = (kind: string) => `mmcp_${kind}_${randomBytes(32).toString("base64url")}` +export function canonicalMcpUrl(): URL { + const url = new URL(process.env.MCP_PUBLIC_URL || `http://localhost:${env.PORT}/mcp`) + if (url.username || url.password || url.search || url.hash || url.pathname !== "/mcp") throw new McpError("INVALID_CONFIGURATION", "MCP_PUBLIC_URL должен указывать на /mcp без query и credentials", 503) + if (url.protocol !== "https:" && !(url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname))) throw new McpError("INVALID_CONFIGURATION", "Удалённый MCP требует HTTPS", 503) + return url +} +export function constantEqual(a: string, b: string): boolean { + const left = Buffer.from(a), right = Buffer.from(b) + return left.length === right.length && timingSafeEqual(left, right) +} +export function mcpStateDirectory() { + return path.resolve(process.env.MCP_STATE_DIR || path.join(path.dirname(env.DATABASE_PATH), ".mcp-state")) +} +async function durableValue(name: string, bytes: number): Promise { + const directory = mcpStateDirectory(), filename = path.join(directory, name) + const checked = (value: Buffer) => { + if (value.length !== bytes) throw new McpError("STATE_UNAVAILABLE", "Повреждено состояние доступа MCP", 503) + return value + } + // Re-read current material on EVERY check. Existing installations need no + // mkdir/open-wx/EEXIST round trip; rotation/revocation is never cached. + try { return checked(await readFile(filename)) } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error } + await mkdir(directory, { recursive: true, mode: 0o700 }) + try { + const handle = await open(filename, "wx", 0o600) + try { await handle.writeFile(randomBytes(bytes)); await handle.sync() } finally { await handle.close() } + } catch (error) { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error } + return checked(await readFile(filename)) +} +export async function accessGeneration(): Promise { return (await durableValue("access-generation", 32)).toString("hex") } +export async function invalidateMcpAccess(): Promise { + await accessGeneration() + const filename = path.join(mcpStateDirectory(), "access-generation"), temporary = filename + "." + randomBytes(8).toString("hex") + const handle = await open(temporary, "wx", 0o600) + try { await handle.writeFile(randomBytes(32)); await handle.sync() } finally { await handle.close() } + await rename(temporary, filename) +} +export async function encryptSecret(value: string): Promise { + return seal(value, await durableValue("encryption-key", 32)) +} +/** Private configuration proofs must not turn a public outer fingerprint into + * an offline dictionary oracle for a human-chosen credential. */ +export async function privateSecretFingerprint(value: string, domain: string): Promise { + return (await privateFingerprinter(domain))(value) +} +/** One scoped batch derives its purpose key once; no key is returned to callers. */ +export async function privateFingerprinter(domain: string): Promise<(value: string) => string> { + const key = createHmac("sha256", await durableValue("encryption-key", 32)).update("MCP private fingerprint:" + domain).digest() + return value => createHmac("sha256", key).update(value).digest("hex") +} +/** Read-only check of an already active purpose-key epoch. Unlike initial + * bootstrap, a missing/deleted key must refuse without recreating state. */ +export async function privateFingerprinterExisting(domain: string): Promise<(value: string) => string> { + let material: Buffer + try { material = await readFile(path.join(mcpStateDirectory(), "encryption-key")) } + catch { throw new McpError("STATE_UNAVAILABLE", "Действующий ключ MCP недоступен", 503) } + try { + if (material.length !== 32) throw new McpError("STATE_UNAVAILABLE", "Действующий ключ MCP недоступен", 503) + const key = createHmac("sha256", material).update("MCP private fingerprint:" + domain).digest() + return value => createHmac("sha256", key).update(value).digest("hex") + } finally { material.fill(0) } +} +/** Purpose-separated durable material stays inside a server-owned callback; + * it must never be serialized into evidence, tool inputs or responses. */ +export async function withVrfProductionSealingKey(work: (secret: Uint8Array) => Promise): Promise { + const key = createHmac("sha256", await durableValue("encryption-key", 32)).update("MCP VRF production admission sealing:v1").digest() + try { return await work(key) } finally { key.fill(0) } +} +export function seal(value: string, key: Buffer): string { + const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv) + const encrypted = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]) + return Buffer.concat([iv, cipher.getAuthTag(), encrypted]).toString("base64") +} +/** Historical readers must never bootstrap/recreate missing encryption state. */ +export async function decryptSecretExisting(value: string): Promise { + let key: Buffer + try { key = await readFile(path.join(mcpStateDirectory(), "encryption-key")) } + catch { throw new McpError("STATE_UNAVAILABLE", "Действующий ключ MCP недоступен", 503) } + try { + if (key.length !== 32) throw new McpError("STATE_UNAVAILABLE", "Действующий ключ MCP недоступен", 503) + const data = Buffer.from(value, "base64") + if (data.length < 28) throw new McpError("SECRET_UNAVAILABLE", "Секрет недоступен", 503) + const cipher = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)) + cipher.setAuthTag(data.subarray(12, 28)) + return Buffer.concat([cipher.update(data.subarray(28)), cipher.final()]).toString("utf8") + } finally { key.fill(0) } +} +export async function decryptSecret(value: string): Promise { + const data = Buffer.from(value, "base64") + if (data.length < 28) throw new McpError("SECRET_UNAVAILABLE", "Секрет недоступен", 503) + const decipher = createDecipheriv("aes-256-gcm", await durableValue("encryption-key", 32), data.subarray(0, 12)) + decipher.setAuthTag(data.subarray(12, 28)) + return Buffer.concat([decipher.update(data.subarray(28)), decipher.final()]).toString("utf8") +} +export async function encryptBinary(value: Buffer): Promise { + const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", await durableValue("encryption-key", 32), iv) + const data = Buffer.concat([cipher.update(value), cipher.final()]) + return Buffer.concat([iv, cipher.getAuthTag(), data]) +} +export async function decryptBinary(value: Buffer): Promise { + if (value.length < 28) throw new McpError("SECRET_UNAVAILABLE", "Зашифрованный архив недоступен", 503) + const cipher = createDecipheriv("aes-256-gcm", await durableValue("encryption-key", 32), value.subarray(0, 12)) + cipher.setAuthTag(value.subarray(12, 28)) + return Buffer.concat([cipher.update(value.subarray(28)), cipher.final()]) +} +const sensitive = /password|passwd|secret|private.?key|preshared|pre.shared|authorization|auth.?key|authentication.?key|tcp.?md5.?key|api.?key|certificate.?contents|token|ciphertext/i +export const MAX_MCP_PUBLIC_STRING_LENGTH = 16000 +export const MAX_MCP_PUBLIC_ARRAY_ITEMS = 500 +export function redact(value: unknown, depth = 0): unknown { + if (depth > 12) return "[truncated]" + if (value instanceof Date) return value.toISOString() + if (typeof value === "string") return value.slice(0, MAX_MCP_PUBLIC_STRING_LENGTH) + .replace(/\bBearer\s+[^\s,;]+/gi, "Bearer [redacted]") + .replace(/\bmmcp_[a-z]+_[A-Za-z0-9_-]+/g, "[redacted]") + .replace(/((?:password|secret|private-key|preshared-key|auth-key|authentication-key|tcp-md5-key|api-key|token)\s*[=:]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)/gi, "$1[redacted]") + if (Array.isArray(value)) return value.slice(0, MAX_MCP_PUBLIC_ARRAY_ITEMS).map(item => redact(item, depth + 1)) + if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).filter(([key]) => key !== "__mmappMcpOperationClaimV1").slice(0, 200).map(([key, item]) => [key, sensitive.test(key) && !/Ref$|Configured$/i.test(key) ? "[redacted]" : redact(item, depth + 1)])) + return value +} +export function fingerprint(value: unknown): string { + const canonical = (item: unknown): string => { + if (item instanceof Date) return JSON.stringify(item.toISOString()) + if (Array.isArray(item)) return "[" + item.map(canonical).join(",") + "]" + if (item && typeof item === "object") return "{" + Object.entries(item).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, entry]) => JSON.stringify(key) + ":" + canonical(entry)).join(",") + "}" + return JSON.stringify(item) ?? "null" + } + return tokenDigest(canonical(value)) +} diff --git a/backend/src/modules/mcp/server-connection-actions.ts b/backend/src/modules/mcp/server-connection-actions.ts new file mode 100644 index 00000000..4ad80050 --- /dev/null +++ b/backend/src/modules/mcp/server-connection-actions.ts @@ -0,0 +1,105 @@ +import { lookup } from "node:dns/promises" +import { isIP } from "node:net" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { performServerConnectionTest } from "../../services/server-connection-test.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { decryptSecret, fingerprint, privateSecretFingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import { isAllowedServerConnectionAddress, serverConnectionHostSchema } from "./server-connection-destination-policy.js" +export { isAllowedServerConnectionAddress } from "./server-connection-destination-policy.js" + +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, 409) +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict() +export const serverConnectionActionSchema = z.object({ + host: serverConnectionHostSchema, + port: z.number().int().min(1).max(65535).default(443), useSsl: z.boolean().default(true), verifySsl: z.boolean().default(false), + apiPath: z.string().max(256).regex(/^\/(?:[a-zA-Z0-9_-]+(?:\/[a-zA-Z0-9_-]+)*)?$/).default("/rest"), + username: z.string().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f:]/.test(value)).default("admin"), passwordRef: z.string().uuid(), +}).strict() +/** Issuance UI must bind all destination and authentication parameters, including + * TLS verification; an old host:port-only server-login reference is insufficient. */ +export function serverConnectionSecretPurpose(value: Omit, "passwordRef">) { + const { passwordRef: _reference, ...endpoint } = serverConnectionActionSchema.parse({ ...value, passwordRef: "00000000-0000-4000-8000-000000000001" }) + return `server-connection-test:${fingerprint(endpoint)}` +} +async function resolveDestination(host: string) { + if (isIP(host)) { + if (!isAllowedServerConnectionAddress(host)) throw fail("Назначение RouterOS запрещено", "DESTINATION_DENIED") + return [{ address: host, family: isIP(host) }] + } + let timer: ReturnType | undefined + try { + const rows = await Promise.race([lookup(host, { all: true, verbatim: true }), new Promise((_resolve,reject) => { timer = setTimeout(() => reject(fail("Истекло время разрешения адреса", "STATE_UNAVAILABLE")), 2000) })]) + if (!rows.length || rows.length > 16 || rows.some(row => !isAllowedServerConnectionAddress(row.address))) throw fail("Не подтверждено безопасное назначение RouterOS", "DESTINATION_DENIED") + return rows.sort((a,b) => a.family - b.family || a.address.localeCompare(b.address)) + } catch (error) { if (error instanceof McpError) throw error; throw fail("Адрес RouterOS не разрешён", "STATE_UNAVAILABLE") } + finally { if (timer) clearTimeout(timer) } +} +export async function readServerConnectionSource(input: Record, signal?: AbortSignal) { + const request = serverConnectionActionSchema.parse(Object.fromEntries(Object.keys(serverConnectionActionSchema.shape).map(key => [key,input[key]]))) + const authorize = async () => { + if (signal?.aborted) throw fail("Проверка отменена", "READ_CANCELLED") + const connection = await effectiveMcpConnection(principalSchema.parse(input.principal)) + if (connection.owner_id !== input.ownerId || !connection.policy.scope.allowGlobal || !connection.policy.scope.operationIds.includes("servers.test-connection") || !hasPermission(connection.policy.permissions,"mm:servers:write") || !hasPermission(connection.owner_permissions,"mm:servers:write")) throw fail("Право проверки подключения отозвано", "PERMISSION_DENIED") + if (fingerprint(connection.policy) !== input.policyProof) throw fail("Допуск изменился", "RESET_REQUIRED") + const ids = (await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => row.id) + if (ids.length > 1000 || ids.some(id => !connection.policy.scope.serverIds.includes(id))) throw fail("Требуется полный каталог серверов", "SERVER_DENIED") + return ids + } + const ids = await authorize() + const reference = (await pool.query("SELECT CASE WHEN octet_length(purpose)<=512 THEN purpose ELSE NULL END AS purpose,server_id,created_by,xmin::text AS revision,CASE WHEN octet_length(ciphertext)<=16384 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2", [request.passwordRef,input.ownerId])).rows[0] + if (!reference || reference.server_id !== null || reference.purpose !== serverConnectionSecretPurpose(request) || typeof reference.ciphertext !== "string") throw fail("Ссылка на пароль не выдана этому назначению", "SECRET_UNAVAILABLE") + let password: string + try { password = await decryptSecret(reference.ciphertext) } catch { throw fail("Ссылка на пароль недоступна", "SECRET_UNAVAILABLE") } + if (Buffer.byteLength(password) > 4096 || /[\u0000\r\n]/.test(password)) throw fail("Пароль не соответствует допустимому формату", "SECRET_UNAVAILABLE") + const destinations = await resolveDestination(request.host) + if (fingerprint(ids) !== fingerprint(await authorize())) throw fail("Каталог изменился") + const { passwordRef: _reference, ...endpoint } = request + return { ids, endpoint, password, destinations, publicState: { endpoint, destinations, passwordConfigured: true, proof: await privateSecretFingerprint(fingerprint({ reference, ids, destinations }),"server-connection-test-v1") } } +} +export function registerServerConnectionActions(register: typeof add) { + const current = async (input: Record) => (await readServerConnectionSource(input)).publicState + registerMcpService("servers.test-connection", { + current, + execute: async input => { + const source = await readServerConnectionSource(input) + const fence = async () => { + await assertVerifiedConfigurationAccess() + if (fingerprint(await current(input)) !== input.beforeFingerprint) throw fail("Назначение, допуск или ссылка изменились после подтверждения") + } + await fence() + const pinned = source.destinations[0] + const result = await performServerConnectionTest({ ...source.endpoint, password: source.password, + lookup: ((_host: string, options: unknown, callback: (...args: unknown[]) => void) => { + if ((options as {all?:boolean})?.all) callback(null,[pinned]); else callback(null,pinned.address,pinned.family) + }) as typeof import("node:dns").lookup, + servername: isIP(source.endpoint.host) ? undefined : source.endpoint.host, + }, { beforeDispatch: fence, afterDispatch: fence }) + if (result.outcome === "unknown") throw new McpError("MUTATION_OUTCOME_UNKNOWN","Результат проверки подключения не подтверждён; автоматический повтор запрещён",502) + return { ...result, attempts: 1, requests: 2, configurationChanged: false, forwardingVerified: false } + }, + verify: async (input,result) => { + if (fingerprint(await current(input)) !== input.beforeFingerprint) throw fail("Источник или допуск изменились перед подтверждением результата") + return (result as {success?:boolean}|null)?.success === true + }, + reconcile: async (_input,authorize) => { await authorize(); return {matchesDesiredState:false,matchesOriginalState:false} }, + }) + register("servers.test-connection","mm_server_test_connection_plan","Проверить подключение RouterOS до сохранения","servers","mm:servers:write","write","global",serverConnectionActionSchema, { + prepare: async (input,context) => { + const request = {...input,principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy)} + const source = await readServerConnectionSource(request,context.signal) + const objects = [{kind:"server-connection-test",id:"global"},...source.ids.map(serverId => ({kind:"server",id:String(serverId),serverId}))] + const protections = 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'='server-connection-test' LIMIT 1001",[source.ids.map(String)]) + if (protections.rows.length > 1000) throw fail("Превышен предел защит", "STATE_UNAVAILABLE") + for (const row of protections.rows) objects.push(row.object_ref) + if (fingerprint(await current(request)) !== fingerprint(source.publicState)) throw fail("Источник изменился при подготовке") + return {target:{scope:"global"},objects,snapshots:[],before:source.publicState,requiresHuman:true,mutationCost:1,verificationScope:"acknowledgement", + steps:[{kind:"service",service:"servers.test-connection",input:{...request,beforeFingerprint:fingerprint(source.publicState)}}], + warnings:["Два ограниченных GET /system/identity и /system/resource с указанными параметрами. Настройки и сохранённые серверы не изменяются; сетевой запрос расходует один допуск операции.","Пароль принимается только через owner/purpose ссылку без server scope, связанную с полным назначением. DNS ограничен, адрес закрепляется без redirect; изменение DNS блокирует результат.","Проверка требует человеческого подтверждения. Ответ не доказывает forwarding. Неопределённый результат не повторяется и не сверяется новым сетевым запросом."]} + }, + }) +} diff --git a/backend/src/modules/mcp/server-connection-destination-policy.ts b/backend/src/modules/mcp/server-connection-destination-policy.ts new file mode 100644 index 00000000..2f0c4c3e --- /dev/null +++ b/backend/src/modules/mcp/server-connection-destination-policy.ts @@ -0,0 +1,24 @@ +import { isIP } from "node:net" +import { z } from "zod" + +/** Pure shared destination policy: no configured pool, actions or services. */ +export const serverConnectionHostSchema = z.string().trim().min(1).max(253).transform(value => value.toLowerCase()).refine(value => isIP(value) !== 0 || /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(value) && value.split(".").every(part => part.length > 0 && part.length <= 63), "Некорректный адрес RouterOS") + +export function isAllowedServerConnectionAddress(address: string): boolean { + const family = isIP(address) + if (family === 4) { + const [a,b] = address.split(".").map(Number) + // RFC1918 and loopback are intentional managed RouterOS destinations. Link + // local/metadata, unspecified and multicast destinations are never eligible. + return a !== 0 && a < 224 && !(a === 169 && b === 254) && !["168.63.129.16","100.100.100.200"].includes(address) + } + if (family !== 6) return false + const normalized = new URL(`http://[${address}]/`).hostname.slice(1,-1).toLowerCase() + if (normalized.startsWith("::ffff:")) { + const hex = normalized.slice(7).split(":") + if (hex.length !== 2) return false + const bytes = hex.flatMap(part => [parseInt(part,16) >> 8, parseInt(part,16) & 255]) + return isAllowedServerConnectionAddress(bytes.join(".")) + } + return (normalized === "::1" || !normalized.startsWith("::")) && normalized !== "fd00:ec2::254" && !normalized.startsWith("ff") && !/^fe[89abcdef]/.test(normalized) && !normalized.startsWith("64:ff9b:") && !normalized.startsWith("2002:") +} diff --git a/backend/src/modules/mcp/server-poll-actions.test.ts b/backend/src/modules/mcp/server-poll-actions.test.ts new file mode 100644 index 00000000..92469437 --- /dev/null +++ b/backend/src/modules/mcp/server-poll-actions.test.ts @@ -0,0 +1,213 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { test } from "node:test" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import { createServerPollMcpAdapterForTests, type ServerPollMcpDependencies } from "./server-poll-actions.js" +import { fingerprint, redact } from "./security.js" +import type { DurableServerPollSessionInput } from "../../services/server-poll-durable-session.js" +import type { ServerPollReceipt } from "../../services/server-poll-execution.js" + +function fixture(hooks: { verified?: () => Promise; owned?: () => Promise; authority?: () => Promise; readArchived?:ServerPollMcpDependencies["readArchived"] } = {}) { + let now = Date.now(), revoked = false, verified = true, stateProof = "a".repeat(64), authorityProof = "b".repeat(64), executions = 0, reads = 0, authCalls = 0 + const operationId = randomUUID(), sessions: DurableServerPollSessionInput[] = [], queries: unknown[][] = [] + const context = { principal: { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(now + 600000).toISOString() }, + connection: { owner_id: "owner", owner_claims_issued_at: new Date(now - 1000), owner_valid_until: new Date(now + 600000), policy: { expiresAt: new Date(now + 600000).toISOString() } } } as ActionContext + const encode = (value: string) => Buffer.from(value).toString("base64"), decode = (value: string) => Buffer.from(value, "base64").toString() + let evidence!: ChangeEvidence + const row: Record = { operation_id: operationId, operation_state: "running", plan_state: "executing", approved_by: "human", step_state: "intent", step_index: 0, result: null } + let stored: ServerPollReceipt | undefined + let artifactOpens = 0, authorityPasses = 1, onPass: (index: number) => void = () => {} + const projections: boolean[] = [] + const deps: ServerPollMcpDependencies = { + ...(hooks.readArchived?{readArchived:hooks.readArchived}:{}), + now: () => now, seal: async value => encode(value), open: async value => { const text = decode(value); if (text.includes('"steps":')) artifactOpens++; return text }, proof: async value => fingerprint(value), + verified: async () => { await hooks.verified?.(); if (!verified) throw Error("PRIVATE_PROVIDER missing inherited guard") }, + authority: () => async (_request, ids, signal) => { signal?.throwIfAborted(); await hooks.authority?.(); authCalls++; if (revoked || ids.some(id => ![1, 2].includes(id))) throw Error("PRIVATE_PROVIDER denied"); return { global: true, serverIds: [1, 2], authorityProof } }, + query: async (sql, values) => { + await hooks.owned?.() + queries.push(values) + if (sql.startsWith("SELECT o.id FROM")) return [{ id: operationId }] + assert.equal(values[0], operationId); assert.equal(values[1], context.principal.connectionId); assert.equal(values[2], "owner") + const full = sql.includes("p.evidence AS evidence"), artifact = { sealed: encode(JSON.stringify(evidence)) }; projections.push(full) + return [{ ...row, intended_state: row.intended_state ?? redact(evidence.steps[0]), evidence: full ? artifact : null, + evidence_sha256: createHash("sha256").update(JSON.stringify(artifact)).digest("hex") }] + }, + session: input => { + sessions.push(structuredClone({ ...input, authorize: undefined }) as unknown as DurableServerPollSessionInput) + return { + prepare: async signal => { await input.authorize(input.request, [1, 2], signal); return { version: 1, requiresHuman: true, scope: "global", request: { ...input.request }, around: new Date(now).toISOString(), + beforeProof: stateProof, authorityProof, bindingProof: "c".repeat(64), maintenance: [], affectedServerIds: [1, 2], mutationCost: 8 } }, + execute: async (plan, signal) => { for (let index = 0; index < authorityPasses; index++) { onPass(index); await input.authorize(input.request, plan.affectedServerIds, signal) }; executions++ + stored = { receiptId: input.request.receiptId, complete: true, maintenanceComplete: true, configurationVerified: true, nativeReads: 4, confirmedPublications: 2, completedPublications: 2, + afterProof: "d".repeat(64), observation: "online", snapshot: { id: 7, serverId: 1, polledAt: new Date(now).toISOString(), status: "online", identityName: "router" } } + return structuredClone(stored) + }, + readExisting: async (plan, signal) => { await input.authorize(input.request, plan.affectedServerIds, signal); reads++; return stored ? structuredClone(stored) : undefined }, + } + }, + } + const adapter = createServerPollMcpAdapterForTests(deps) + const prepare = async () => { evidence = await adapter.prepare({ serverId: 1 }, context); return evidence.steps[0].kind === "service" ? evidence.steps[0].input : assert.fail("Expected service") } + const execute = (input: Record) => adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0 }) + return { adapter, context, deps, row, operationId, sessions, queries, prepare, execute, evidence: () => evidence, decode, encode, + revoke: () => { revoked = true }, unverify: () => { verified = false }, advance: (ms: number) => { now += ms }, drift: () => { stateProof = "e".repeat(64) }, grantDrift: () => { authorityProof = "f".repeat(64) }, + executions: () => executions, reads: () => reads, authCalls: () => authCalls, artifactOpens: () => artifactOpens, projections, + passes: (count: number, callback: (index: number) => void = () => {}) => { authorityPasses = count; onPass = callback } } +} + +test("each dispatch boundary waits for all independent live sources, including a late refusal", async () => { + for (const refused of [false, true]) { + let active = false, reached!: () => void + const started = new Set(), releases = new Map void>() + const allStarted = new Promise(resolve => { reached = resolve }) + const check = (name: string) => async () => { + if (!active) return + const waiting = new Promise(resolve => { releases.set(name, resolve) }) + started.add(name); if (started.size === 3) reached() + await waiting + if (refused && name === "authority") throw Error("late revocation") + } + const f = fixture({ verified: check("verified"), owned: check("owned"), authority: check("authority") }) + const input = await f.prepare(); f.passes(1, () => { active = true }) + const execution = f.execute(input) + // Observe rejection immediately, even while the other sources are pending. + const outcome = execution.then(() => true, () => false) + await allStarted + releases.get("verified")!(); releases.get("owned")!() + await Promise.resolve(); assert.equal(f.executions(), 0) + active = false; releases.get("authority")!() + assert.equal(await outcome, !refused); assert.equal(f.executions(), refused ? 0 : 1) + } +}) + +test("public input accepts only serverId; human global evidence seals original identity and no placeholder operation", async () => { + const f = fixture(), input = await f.prepare(), evidence = f.evidence() + assert(!f.adapter.schema.safeParse({ serverId: 1, operationId: randomUUID(), receiptId: randomUUID(), principal: {} }).success) + assert.deepEqual(evidence.target, { scope: "global" }); assert.equal(evidence.requiresHuman, true); assert.equal(evidence.mutationCost, 8) + assert.equal(evidence.verificationScope, "configuration"); assert.deepEqual(evidence.objects.map(object => object.serverId), [1, 2]) + const binding = JSON.parse(f.decode(String(input.bindingCiphertext))) + assert.deepEqual(binding.principal, f.context.principal); assert.equal(binding.ownerId, "owner"); assert.equal(binding.expiresAt, Date.parse(evidence.validUntil!)) + assert(!("executionId" in binding)); assert(!("identity" in binding)); assert.equal(f.executions(), 0) + assert(f.sessions.every(session => session.identity.executionId !== f.operationId)) +}) + +test("apply uses actual runner op ID, inherited guard and original sealed expiry/principal", async () => { + const f = fixture(), input = await f.prepare(), expiry = Date.parse(f.evidence().validUntil!) + const result = await f.execute(input) as Record, execution = f.sessions.at(-1)! + assert.equal(execution.identity.executionId, f.operationId); assert.equal(execution.identity.expiresAt, expiry) + assert.equal(execution.identity.kind, "mcp"); assert.equal(f.executions(), 1); assert(f.authCalls() > 3) + assert.equal(result.operationId, f.operationId); assert.equal(result.nativeReads, 4); assert(!JSON.stringify(result).includes("bindingProof")); assert(!JSON.stringify(result).includes("rawInterfaces")) +}) + +test("service refuses missing real context, recovery, wrong step, absent guard and human approval", async () => { + for (const mode of ["context", "recovery", "step", "guard", "human"]) { + const f = fixture(), input = await f.prepare() + if (mode === "guard") f.unverify(); if (mode === "human") f.row.approved_by = null + await assert.rejects(mode === "context" ? f.adapter.service.execute(input) : mode === "recovery" ? f.adapter.service.execute(input, { operationId: f.operationId, phase: "recovery" }) + : mode === "step" ? f.adapter.service.execute(input, { operationId: f.operationId, phase: "forward", stepIndex: 1 }) : f.execute(input)) + assert.equal(f.executions(), 0) + } +}) + +test("sealed input/evidence tamper and an expired original plan refuse despite another live credential", async () => { + for (const mode of ["digest", "receipt", "evidence", "expired"]) { + const f = fixture(), input = await f.prepare() + if (mode === "digest") input.inputDigest = "0".repeat(64) + if (mode === "receipt") input.receiptId = randomUUID() + if (mode === "evidence") f.evidence().requiresHuman = false + if (mode === "expired") { f.advance(300001); f.context.principal.credentialId = randomUUID(); f.context.principal.credentialExpiresAt = new Date(Date.now() + 3600000).toISOString() } + await assert.rejects(f.execute(input)); assert.equal(f.executions(), 0) + } +}) + +test("current is journal-free and refuses original source/policy drift; revoked scope cannot prepare", async () => { + const f = fixture(), input = await f.prepare() + assert.deepEqual(await f.adapter.service.current(input), f.evidence().before); assert.equal(f.executions(), 0); assert.equal(f.reads(), 0) + f.drift(); await assert.rejects(f.adapter.service.current(input)) + const g = fixture(), other = await g.prepare(); g.grantDrift(); await assert.rejects(g.adapter.service.current(other)) + const h = fixture(); h.revoke(); await assert.rejects(h.prepare()); assert.equal(h.executions(), 0) +}) + +test("verify/reconcile use only authenticated persisted read, never execute/claim replay", async () => { + const f = fixture(), input = await f.prepare() + assert.equal(await f.adapter.service.verify!(input, {}), false) + const result = await f.execute(input); f.row.result = result; f.row.step_state = "applied" + assert.equal(await f.adapter.service.verify!(input, result), true); assert.equal(f.executions(), 1); assert.equal(f.reads(), 1) + assert.deepEqual(await f.adapter.service.reconcile!(input, async () => {}), { matchesDesiredState: true, matchesOriginalState: false }) + assert.equal(f.executions(), 1); assert.equal(f.reads(), 2) + f.row.result = { forged: true }; assert.equal(await f.adapter.service.verify!(input, result), false) + f.revoke(); assert.equal(await f.adapter.service.verify!(input, result), false) +}) + +test("expired binding reconciles through optional historical reader with current authority and no old session/grant",async()=>{ + let archiveCalls=0,currentChecks=0 + const f=fixture({readArchived:async(_raw,authorize)=>{archiveCalls++;await authorize();return true}}),input=await f.prepare() + f.advance(300001);f.revoke() + const before={sessions:f.sessions.length,auth:f.authCalls(),queries:f.queries.length} + assert.deepEqual(await f.adapter.service.reconcile!(input,async()=>{currentChecks++}),{matchesDesiredState:true,matchesOriginalState:false}) + assert.equal(archiveCalls,1);assert(currentChecks>=4) + assert.deepEqual({sessions:f.sessions.length,auth:f.authCalls(),queries:f.queries.length},before) + const g=fixture(),other=await g.prepare();g.advance(300001) + assert.deepEqual(await g.adapter.service.reconcile!(other,async()=>{}),{matchesDesiredState:false,matchesOriginalState:false}) +}) + +test("expired historical reconcile propagates current permission denial instead of publishing a safe-looking false",async()=>{ + const denied=Error("current permission denied") + const f=fixture({readArchived:async(_raw,authorize)=>{await authorize();throw denied}}),input=await f.prepare() + f.advance(300001);let checks=0 + await assert.rejects(f.adapter.service.reconcile!(input,async()=>{if(++checks===4)throw denied}),error=>error===denied) +}) + +test("missing persisted receipt cannot verify a plausible result", async () => { + const f = fixture(), input = await f.prepare() + const plausible = { operationId: f.operationId, receiptId: input.receiptId, nativeReads: 4, confirmedPublications: 2, completedPublications: 2, complete: true, maintenanceComplete: true, configurationVerified: true, automaticReplay: false, + snapshot: { id: 7, serverId: 1, polledAt: new Date().toISOString(), status: "online" } } + f.row.result = plausible + assert.equal(await f.adapter.service.verify!(input, plausible), false); assert.equal(f.executions(), 0); assert.equal(f.reads(), 1) +}) + +test("same-window artifact decrypt occurs once while ownership and authority SQL remain fresh at every boundary", async () => { + const f = fixture(), input = await f.prepare(); f.passes(5) + await f.execute(input) + assert.equal(f.artifactOpens(), 1); assert.equal(f.queries.length, 7); assert.deepEqual(f.projections, [true, false, false, false, false, false, false]) + assert(f.authCalls() >= 8) + await f.execute(input) + assert.equal(f.artifactOpens(), 2); assert.equal(f.projections.filter(Boolean).length, 2, "A new window cannot reuse artifact validation") +}) + +test("fresh SHA drift refetches full artifact and rejects tamper instead of using cached approval", async () => { + const f = fixture(), input = await f.prepare() + f.passes(2, index => { if (index === 1) f.evidence().requiresHuman = false }) + await assert.rejects(f.execute(input)); assert.equal(f.executions(), 0) + assert.deepEqual(f.projections, [true, false, false, true]); assert.equal(f.artifactOpens(), 2) +}) + +test("cached artifact never caches revocation, human approval, operation ID or original expiry", async () => { + for (const mode of ["revocation", "approval", "expiry"]) { + const f = fixture(), input = await f.prepare() + f.passes(2, index => { if (index !== 1) return; if (mode === "revocation") f.revoke(); if (mode === "approval") f.row.approved_by = null; if (mode === "expiry") f.advance(300001) }) + await assert.rejects(f.execute(input)); assert.equal(f.executions(), 0) + assert.equal(f.artifactOpens(), 1) + } + const f = fixture(), input = await f.prepare(); await f.execute(input) + await assert.rejects(f.adapter.service.execute(input, { operationId: randomUUID(), phase: "forward", stepIndex: 0 })) + assert.equal(f.executions(), 1); assert.equal(f.artifactOpens(), 1) +}) + +test("derived expected input never substitutes for a changed current intended-state row", async () => { + const f=fixture(),input=await f.prepare() + f.passes(2,index=>{if(index===1)f.row.intended_state={kind:"service",service:"servers.poll",input:{serverId:2}}}) + await assert.rejects(f.execute(input));assert.equal(f.executions(),0) + assert.equal(f.artifactOpens(),1);assert(f.queries.length>=3) + assert(f.queries.every(values=>JSON.parse(String(values[4])).input.serverId===1)) +}) + + +test("unbranded combined-authority injection cannot replace the original verified runtime branch", async () => { + const f=fixture(),input=await f.prepare();let forgedCalls=0 + const forged=async()=>{forgedCalls++;return {global:true as const,serverIds:[1,2],authorityProof:"b".repeat(64)}} + const adapter=createServerPollMcpAdapterForTests({...f.deps,combinedAuthority:()=>forged}) + f.passes(1,()=>f.unverify()) + await assert.rejects(adapter.service.execute(input,{operationId:f.operationId,phase:"forward",stepIndex:0})) + assert.equal(forgedCalls,0);assert.equal(f.executions(),0) +}) diff --git a/backend/src/modules/mcp/server-poll-actions.ts b/backend/src/modules/mcp/server-poll-actions.ts new file mode 100644 index 00000000..5ddf8bcc --- /dev/null +++ b/backend/src/modules/mcp/server-poll-actions.ts @@ -0,0 +1,239 @@ +import { carryPollError } from "../../services/server-poll-error-provenance.js" +import { publicationLayout } from "../../services/server-poll-maintenance-groups.js" +import { randomUUID } from "node:crypto" +import { z } from "zod" +import type { DurableServerPollSessionInput } from "../../services/server-poll-durable-session.js" +import type { ServerPollPlan, ServerPollReceipt } from "../../services/server-poll-execution.js" +import { ServerPollProblem } from "../../services/server-poll-execution-model.js" +import { SERVER_POLL_OPERATION, isCombinedRuntimeMcpServerPollAuthority, type createMcpServerPollAuthority } from "../../services/server-poll-authority.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { McpServiceHandler } from "./service-registry.js" +import { fingerprint, McpError, redact } from "./security.js" + +export const SERVER_POLL_MCP_OPERATION_VERSION = 1 +export const serverPollActionSchema = z.object({ serverId: z.number().int().positive().safe() }).strict() +const hash = z.string().regex(/^[a-f0-9]{64}$/), sid = z.number().int().positive().safe() +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict() +const partition = { parent: z.string().min(1).max(100), name: z.string().min(1).max(100) } +const maintenanceSchema = z.discriminatedUnion("kind", [ + z.object({ kind: z.literal("partition.drop"), ...partition }).strict(), + z.object({ kind: z.literal("partition.ensure"), ...partition, from: z.string().datetime(), to: z.string().datetime() }).strict(), + z.object({ kind: z.literal("partition.storage"), ...partition, settings: z.record(z.string(), z.string()) }).strict(), +]) +const planSchema = z.object({ version: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]), approvalProof: hash.optional(), maintenanceGroupSize: z.literal(8).optional(), requiresHuman: z.literal(true), scope: z.literal("global"), request: z.object({ serverId: sid, receiptId: z.string().uuid() }).strict(), + around: z.string().datetime(), beforeProof: hash, authorityProof: hash, bindingProof: hash, maintenance: z.array(maintenanceSchema).max(2000), + affectedServerIds: z.array(sid).min(1).max(1000), mutationCost: z.number().int().min(8).max(2008) }).strict().refine(plan => ((plan.version === 3 || plan.version === 4) ? plan.approvalProof !== undefined : !Object.hasOwn(plan, "approvalProof")) && (plan.version === 4 ? plan.maintenanceGroupSize === 8 : !Object.hasOwn(plan, "maintenanceGroupSize"))) +const bindingSchema = z.object({ version: z.literal(1), operation: z.literal(SERVER_POLL_OPERATION), principal: principalSchema, + ownerId: z.string().min(1).max(200), issuedAt: z.number().int().nonnegative().safe(), expiresAt: z.number().int().positive().safe(), plan: planSchema }).strict() +type Binding = z.infer +const inputSchema = z.object({ serverId: sid, receiptId: z.string().uuid(), inputDigest: hash, bindingCiphertext: z.string().min(1).max(2097152) }).strict() +type Input = z.infer +const snapshotSchema = z.object({ id: sid, serverId: sid, polledAt: z.string().datetime(), status: z.enum(["online", "offline"]), + latencyMs: z.number().nonnegative().nullable().optional(), rosVersion: z.string().nullable().optional(), boardName: z.string().nullable().optional(), uptime: z.string().nullable().optional(), + cpuLoad: z.number().nullable().optional(), freeMemory: z.number().nonnegative().nullable().optional(), totalMemory: z.number().nonnegative().nullable().optional(), identityName: z.string().nullable().optional() }).strict() +const resultSchema = z.object({ operationId: z.string().uuid(), receiptId: z.string().uuid(), nativeReads: z.literal(4), confirmedPublications: z.number().int().nonnegative(), completedPublications: z.number().int().nonnegative(), + complete: z.literal(true), maintenanceComplete: z.literal(true), configurationVerified: z.literal(true), automaticReplay: z.literal(false), snapshot: snapshotSchema }).strict() +type Window = { deadlineAt: number; signal?: AbortSignal } +export interface ServerPollMcpDependencies { + authority: typeof createMcpServerPollAuthority + combinedAuthority?(principal: Parameters[0], options: Parameters[1]): ReturnType | undefined + session(input: DurableServerPollSessionInput): { prepare(signal?: AbortSignal): Promise; execute(plan: ServerPollPlan, signal?: AbortSignal): Promise; readExisting(plan: ServerPollPlan, signal?: AbortSignal): Promise } + query(sql: string, values: unknown[], options: Window): Promise[]> + /** Current authorized global reconciliation only; never the old live grant. */ + readArchived?(raw:Record,authorize:()=>Promise,options:Window):Promise + seal(value: string): Promise + open(value: string): Promise + proof(value: unknown): Promise + verified(): Promise + now(): number +} +const denied = (): never => { throw new McpError("STATE_CONFLICT", "Привязка полного опроса к подтверждённой операции не подтверждена", 409) } +const safe = (error: unknown): McpError => { + if (error instanceof McpError) return carryPollError(new McpError(error.code, "Полный опрос не получил подтверждённый результат; автоматический повтор запрещён", error.statusCode), error, "poll.safe") + if (error instanceof ServerPollProblem) return carryPollError(new McpError(error.code, "Полный опрос не получил подтверждённый результат; автоматический повтор запрещён", 409), error, "poll.safe") + return carryPollError(new McpError("STATE_UNAVAILABLE", "Состояние полного опроса не подтверждено; автоматический повтор запрещён", 503), error, "poll.safe") +} +/** Trusted composition only. Model JSON cannot provide a principal, receipt, + * journal, clock, connector, authority or prepared plan. */ +export function createServerPollMcpAdapterForTests(rawDeps: ServerPollMcpDependencies) { + const deps = Object.freeze({ ...rawDeps }) + // Only parsed-artifact authentication is reused within ONE owned window. + // The fresh SQL row, SHA-256, ownership, approval and live authority are not. + const artifacts = new WeakMap>() + // Derived constants only: parsed input is immutable and local to this window. + // No database row, policy, grant, approval or current digest is stored here. + type Subject = Readonly<{ key: string; intendedJson: string; intendedProof: string }> + const subjects = new WeakMap>>() + const subject = (input: Input, operationId: string, options: Window): Subject => { + if (!Object.isFrozen(input)) return denied() + let inputs = subjects.get(options) + if (!inputs) { inputs = new WeakMap(); subjects.set(options, inputs) } + let operations = inputs.get(input) + if (!operations) { operations = new Map(); inputs.set(input, operations) } + let value = operations.get(operationId) + if (!value) { + const intended = redact({ kind: "service", service: SERVER_POLL_OPERATION, input }) + value = Object.freeze({ key: fingerprint({ operationId, input }), intendedJson: JSON.stringify(intended), intendedProof: fingerprint(intended) }) + operations.set(operationId, value) + } + return value + } + const live = (binding: Binding, signal?: AbortSignal) => { if (signal?.aborted || deps.now() < binding.issuedAt || deps.now() >= binding.expiresAt) return denied() } + const window = async (expiresAt: number, signal: AbortSignal | undefined, work: (options: Window) => Promise): Promise => { + const deadlineAt = Math.min(expiresAt, deps.now() + 180000), controller = new AbortController() + const combined = signal ? AbortSignal.any([signal, controller.signal]) : controller.signal + let timer: ReturnType | undefined, abort!: () => void + const stopped = new Promise((_yes, reject) => { abort = () => { controller.abort(); reject(deniedError()) }; timer = setTimeout(abort, Math.max(1, deadlineAt - deps.now())) }) + combined.addEventListener("abort", abort, { once: true }) + try { if (deadlineAt <= deps.now() || combined.aborted) abort(); return await Promise.race([Promise.resolve().then(() => { combined.throwIfAborted(); return work({ deadlineAt, signal: combined }) }), stopped]) } + catch (error) { throw safe(error) } + finally { clearTimeout(timer); combined.removeEventListener("abort", abort); controller.abort() } + } + const open = async (raw: Record, historical=false) => { + const input = Object.freeze(inputSchema.parse(raw)) + const text = await deps.open(input.bindingCiphertext) + if (Buffer.byteLength(text) > 1048576) return denied() + const binding = bindingSchema.parse(JSON.parse(text)); if(!historical)live(binding) + if (input.inputDigest !== await deps.proof(binding) || input.receiptId !== binding.plan.request.receiptId || input.serverId !== binding.plan.request.serverId + || binding.plan.mutationCost !== binding.plan.maintenance.length + 8 || !binding.plan.affectedServerIds.includes(input.serverId) + || new Set(binding.plan.affectedServerIds).size !== binding.plan.affectedServerIds.length || binding.expiresAt > Date.parse(binding.principal.credentialExpiresAt)) return denied() + if(binding.expiresAt<=binding.issuedAt||binding.issuedAt>deps.now())return denied() + if(!historical)live(binding); return { input, binding } + } + const session = (binding: Binding, options: Window, executionId: string, authorize?: DurableServerPollSessionInput["authorize"]) => deps.session({ request: binding.plan.request, + identity: { kind: "mcp", ownerId: binding.ownerId, executionId, connectionId: binding.principal.connectionId, credentialId: binding.principal.credentialId, + issuedAt: binding.issuedAt, expiresAt: binding.expiresAt, operationName: SERVER_POLL_OPERATION }, authorize: authorize ?? deps.authority(binding.principal, options) }) + const preflight = async (binding: Binding, options: Window) => { + live(binding, options.signal) + const fresh = await session(binding, options, randomUUID()).prepare(options.signal) + live(binding, options.signal) + const epochPlan = binding.plan.version === 3 || binding.plan.version === 4 + if (epochPlan && fresh.version !== 3 && fresh.version !== 4) return denied() + if (binding.plan.version === 4 && (fresh.version !== 4 || fresh.maintenanceGroupSize !== binding.plan.maintenanceGroupSize)) return denied() + for (const key of [epochPlan ? "approvalProof" : "beforeProof", "authorityProof", "maintenance", "affectedServerIds", "mutationCost"] as const) if (fingerprint(fresh[key]) !== fingerprint(binding.plan[key])) return denied() + return { proof: binding.plan.beforeProof } + } + const expectedEvidence = (binding: Binding, input: Input) => ({ target: { scope: "global" }, objects: binding.plan.affectedServerIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), + snapshots: [], steps: [{ kind: "service", service: SERVER_POLL_OPERATION, input }], before: { proof: binding.plan.beforeProof }, mutationCost: binding.plan.mutationCost, + requiresHuman: true, verificationScope: "configuration", validUntil: new Date(binding.expiresAt).toISOString(), warnings: ["Полный опрос читает устройство и изменяет общие снимки и разделы истории; требуется подтверждение человеком. Автоматический повтор запрещён."] }) + const owned = async (binding: Binding, input: Input, operationId: string, executing: boolean, options: Window) => { + live(binding, options.signal) + if (deps.now() >= options.deadlineAt) return denied() + let cache = artifacts.get(options) + if (!cache) { cache = new Map(); artifacts.set(options, cache) } + const { key, intendedJson, intendedProof } = subject(input, operationId, options), previous = cache.get(key) + const rows = await deps.query(`SELECT o.id AS operation_id,o.state AS operation_state,p.state AS plan_state,p.approved_by,s.state AS step_state,s.step_index,s.intended_state,s.result, + ${previous ? "NULL AS evidence" : "p.evidence AS evidence"},pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(p.evidence::text,'UTF8')),'hex') AS evidence_sha256 + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_connections c ON c.id=o.connection_id JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE o.id=$1 AND o.connection_id=$2 AND c.owner_id=$3 AND p.operation_id=$4 AND p.operation_version=1 + AND p.approved_by IS NOT NULL AND s.step_index=0 AND s.intended_state=$5::jsonb AND octet_length(p.evidence::text)<=4194304 LIMIT 2`, + [operationId, binding.principal.connectionId, binding.ownerId, SERVER_POLL_OPERATION, intendedJson], options) + live(binding, options.signal) + if (rows.length !== 1) return denied() + const row = rows[0] + if (row.operation_id !== operationId || row.step_index !== 0 || !row.approved_by || fingerprint(row.intended_state) !== intendedProof) return denied() + if (executing ? row.operation_state !== "running" || row.plan_state !== "executing" || row.step_state !== "intent" + : !["running", "applied", "unknown", "failed"].includes(String(row.operation_state)) || !["intent", "applied", "verified", "acknowledged", "unknown", "failed"].includes(String(row.step_state))) return denied() + if (!hash.safeParse(row.evidence_sha256).success) return denied() + if (previous && previous === row.evidence_sha256) return row + if (previous) { + // A changed server digest cannot inherit the old artifact validation. + // Refetch the full sealed source and repeat all fresh row predicates. + cache.delete(key) + return owned(binding, input, operationId, executing, options) + } + const evidence = row.evidence as { sealed?: unknown } + if (typeof evidence?.sealed !== "string") return denied() + const text = await deps.open(evidence.sealed); live(binding, options.signal) + if (Buffer.byteLength(text) > 2097152 || fingerprint(JSON.parse(text)) !== fingerprint(expectedEvidence(binding, input))) return denied() + cache.set(key, String(row.evidence_sha256)) + return row + } + const result = (receipt: ServerPollReceipt, binding: Binding, operationId: string) => { + if (receipt.receiptId !== binding.plan.request.receiptId || !receipt.complete || !receipt.maintenanceComplete || !receipt.configurationVerified + || receipt.confirmedPublications !== receipt.completedPublications || receipt.confirmedPublications < publicationLayout(binding.plan).maintenancePublications + 2 || receipt.confirmedPublications > publicationLayout(binding.plan).maintenancePublications + 4) return denied() + const parsed = snapshotSchema.parse(receipt.snapshot) + if (parsed.serverId !== binding.plan.request.serverId) return denied() + const snapshot = { id: parsed.id, serverId: parsed.serverId, polledAt: parsed.polledAt, status: parsed.status, + latencyMs: parsed.latencyMs ?? null, rosVersion: parsed.rosVersion ?? null, boardName: parsed.boardName ?? null, uptime: parsed.uptime ?? null, + cpuLoad: parsed.cpuLoad ?? null, freeMemory: parsed.freeMemory ?? null, totalMemory: parsed.totalMemory ?? null, identityName: parsed.identityName ?? null } + return resultSchema.parse({ operationId, receiptId: receipt.receiptId, nativeReads: receipt.nativeReads, confirmedPublications: receipt.confirmedPublications, completedPublications: receipt.completedPublications, + complete: true, maintenanceComplete: true, configurationVerified: true, automaticReplay: false, snapshot }) + } + const persisted = async (input: Input, binding: Binding, operationId: string, options: Window) => { + const authority = deps.authority(binding.principal, options) + const authorize: typeof authority = async (...args) => { live(binding, options.signal); await owned(binding, input, operationId, false, options); const grant = await authority(...args); live(binding, options.signal); return grant } + await authorize(binding.plan.request, binding.plan.affectedServerIds, options.signal) + const receipt = await session(binding, options, operationId, authorize).readExisting(binding.plan, options.signal) + if (!receipt) return undefined + const row = await owned(binding, input, operationId, false, options) + await authorize(binding.plan.request, binding.plan.affectedServerIds, options.signal) + return { row, result: result(receipt, binding, operationId) } + } + const prepare = async (raw: Record, context: ActionContext): Promise => window(Date.parse(context.principal.credentialExpiresAt), context.signal, async options => { + const input = serverPollActionSchema.parse(raw), principal = principalSchema.parse(structuredClone(context.principal)), connection = context.connection + const expiresAt = Math.min(deps.now() + 300000, Date.parse(principal.credentialExpiresAt), connection.owner_valid_until.getTime(), Date.parse(connection.policy.expiresAt)) + const seed = { version: 1 as const, operation: SERVER_POLL_OPERATION, principal, ownerId: connection.owner_id, issuedAt: connection.owner_claims_issued_at.getTime(), expiresAt } + const request = { serverId: input.serverId, receiptId: randomUUID() }, authorize = deps.authority(principal, { ...options, deadlineAt: Math.min(options.deadlineAt, expiresAt) }) + await authorize(request, [input.serverId], options.signal) + const plan = await deps.session({ request, identity: { kind: "mcp", executionId: randomUUID(), ownerId: seed.ownerId, issuedAt: seed.issuedAt, expiresAt, + connectionId: principal.connectionId, credentialId: principal.credentialId, operationName: SERVER_POLL_OPERATION }, authorize }).prepare(options.signal) + const binding = bindingSchema.parse({ ...seed, plan }); live(binding, options.signal) + if (plan.request.serverId !== request.serverId || plan.request.receiptId !== request.receiptId || plan.mutationCost !== plan.maintenance.length + 8) return denied() + const serviceInput: Input = { ...request, inputDigest: await deps.proof(binding), bindingCiphertext: await deps.seal(JSON.stringify(binding)) } + await preflight(binding, options) + return expectedEvidence(binding, serviceInput) as ChangeEvidence + }) + const service: McpServiceHandler = { + current: raw => window(deps.now() + 180000, undefined, async options => { const { binding } = await open(raw); return preflight(binding, { ...options, deadlineAt: Math.min(options.deadlineAt, binding.expiresAt) }) }), + execute: (raw, context) => window(deps.now() + 180000, context?.phase === "forward" ? context.signal : undefined, async options => { + if (!context || context.phase !== "forward" || context.stepIndex !== 0 || !z.string().uuid().safeParse(context.operationId).success) return denied() + await deps.verified() + const { input, binding } = await open(raw), bounded = { ...options, deadlineAt: Math.min(options.deadlineAt, binding.expiresAt) } + await owned(binding, input, context.operationId, true, bounded) + const candidateAuthority = deps.combinedAuthority?.(binding.principal, bounded) + const combinedAuthority = isCombinedRuntimeMcpServerPollAuthority(candidateAuthority) ? candidateAuthority : undefined + const authority = combinedAuthority ?? deps.authority(binding.principal, bounded) + const authorize: typeof authority = async (...args) => { + live(binding, bounded.signal) + // Independent read-only sources must all be current at this boundary. + // Run them together; retain no policy/approval decision for later I/O. + const verification = combinedAuthority ? [] : [deps.verified()] + const ownership = owned(binding, input, context.operationId, true, bounded), granted = authority(...args) + const checks = await Promise.allSettled([...verification, ownership, granted]) + live(binding, bounded.signal) + for (const check of checks) if (check.status === "rejected") throw check.reason + return granted + } + const receipt = await session(binding, bounded, context.operationId, authorize).execute(binding.plan, bounded.signal) + await authorize(binding.plan.request, binding.plan.affectedServerIds, bounded.signal) + return result(receipt, binding, context.operationId) + }), + verify: async (raw, value) => { try { return await window(deps.now() + 180000, undefined, async options => { + const parsed = resultSchema.parse(value), { input, binding } = await open(raw), actual = await persisted(input, binding, parsed.operationId, { ...options, deadlineAt: Math.min(options.deadlineAt, binding.expiresAt) }) + return !!actual && fingerprint(actual.result) === fingerprint(parsed) && fingerprint(actual.row.result) === fingerprint(parsed) + }) } catch { return false } }, + reconcile: async (raw, authorize) => { await authorize(); let authFailed=false,authError:unknown + const current=async()=>{try{await authorize()}catch(error){authFailed=true;authError=error;throw error}} + try { return await window(deps.now() + 180000, undefined, async options => { + await current();const { input, binding } = await open(raw,true);await current() + if(deps.now()>=binding.expiresAt){ + if(!deps.readArchived)return {matchesDesiredState:false,matchesOriginalState:false} + const matches=await deps.readArchived(input,current,options);await current() + return {matchesDesiredState:matches===true,matchesOriginalState:false} + } + // A read-only lookup resolves the actual op ID from this exact sealed step. + const rows = await deps.query("SELECT o.id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id WHERE o.connection_id=$1 AND p.operation_id=$2 AND s.step_index=0 AND s.intended_state=$3::jsonb LIMIT 2", [binding.principal.connectionId, SERVER_POLL_OPERATION, JSON.stringify(redact({ kind: "service", service: SERVER_POLL_OPERATION, input }))], options) + if (rows.length !== 1 || !z.string().uuid().safeParse(rows[0].id).success) return { matchesDesiredState: false, matchesOriginalState: false } + const actual = await persisted(input, binding, String(rows[0].id), { ...options, deadlineAt: Math.min(options.deadlineAt, binding.expiresAt) }); await current() + return { matchesDesiredState: !!actual && fingerprint(actual.row.result) === fingerprint(actual.result), matchesOriginalState: false } + }) } catch { if(authFailed)throw authError;return { matchesDesiredState: false, matchesOriginalState: false } } }, + } + return { schema: serverPollActionSchema, target: async (raw: Record, context: ActionContext) => { + const { serverId } = serverPollActionSchema.parse(raw), request = { serverId, receiptId: randomUUID() } + await window(Date.parse(context.principal.credentialExpiresAt), context.signal, async options => { await deps.authority(context.principal, options)(request, [serverId], options.signal) }) + return { scope: "global" as const } + }, prepare, service } +} +function deniedError() { return new McpError("STATE_CONFLICT", "Полный опрос отменён или срок исходного плана истёк", 409) } diff --git a/backend/src/modules/mcp/server-poll-http.sql-helper.ts b/backend/src/modules/mcp/server-poll-http.sql-helper.ts new file mode 100644 index 00000000..271c5701 --- /dev/null +++ b/backend/src/modules/mcp/server-poll-http.sql-helper.ts @@ -0,0 +1,323 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +import { publicationLayout } from "../../services/server-poll-maintenance-groups.js" +/** Actual default registry/runner acceptance in a disposable fresh PostgreSQL + * profile. Only the RouterOS HTTP endpoint is an owned loopback fixture. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { mkdir, realpath } from "node:fs/promises" +import { basename, dirname, resolve } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { performance } from "node:perf_hooks" +import type { AuthUser } from "../../lib/permissions.js" +import type { ChangeEvidence } from "./action-types.js" +import type { ServerPollPlan } from "../../services/server-poll-execution.js" + +export async function acceptServerPollPublicRuntimeSql(options: { cleanupStatus?: "paused"; concurrentApplicationReads?: true; beforeHumanApproval?: () => Promise; delayedApproval?: "runtime" | "configuration-aba" | "guard" } = {}) { + // Refuse before importing configured pools, security keys or production code. + assert.equal(process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH, "true") + if(options.delayedApproval){assert.equal(process.env.MMAPP_OWN_SERVER_POLL_DELAYED_APPROVAL,"true");assert.equal(process.env.NODE_ENV,"test")} + let guardDisabled=false + const concurrentReads = options.concurrentApplicationReads === true + if (concurrentReads) { assert.equal(process.env.MMAPP_OWN_INSTALLATION_COLOAD,"true"); assert.equal(process.env.NODE_ENV,"test") } + if (options.beforeHumanApproval) assert(concurrentReads, "Approval-delay hook requires the isolated application co-load fixture") + const expiryDiagnostics = process.env.MMAPP_OWN_POLL_EXPIRY_DIAGNOSTICS === "true" && process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH === "true" + if (options.cleanupStatus === "paused") { + assert.equal(process.env.MMAPP_OWN_POLL_TERMINAL_ARCHIVE, "true") + assert.equal(process.env.MMAPP_OWN_POLL_ARCHIVE_HISTORY, "true") + } + const url = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(url.protocol)); assert.equal(url.hostname, "127.0.0.1") + assert.equal(decodeURIComponent(url.username), "mcp_test"); assert.equal(url.pathname, "/postgres") + assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres"); assert.equal(url.search, ""); assert.equal(url.hash, "") + assert(process.env.MCP_STATE_DIR) + const state = resolve(process.env.MCP_STATE_DIR) + const parent = dirname(state) + assert.equal(basename(state), "state"); assert.match(basename(parent), /^mm-mcp-test-[A-Za-z0-9-]+$/) + assert.equal(resolve(await realpath(parent)).toLowerCase(), parent.toLowerCase()) + await mkdir(resolve(parent, "state"), { mode: 0o700 }).catch(error => { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error }) + assert.equal(resolve(await realpath(state)).toLowerCase(), state.toLowerCase()) + const { pool } = await import("../../db/index.js") + const { ensurePartitionFor } = await import("../../db/partitions.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { createMcpConnection, authenticateMcpToken } = await import("./store.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const { findAction } = await import("./actions.js") + const { findMcpService } = await import("./service-registry.js") + const { decryptSecret, fingerprint } = await import("./security.js") + const { createStorageDurableServerPollSession } = await import("../../services/server-poll-durable-session.js") + const { createMcpServerPollAuthority } = await import("../../services/server-poll-authority.js") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length) + const action = findAction("mm_server_poll_plan"), service = findMcpService("servers.poll") + assert(service); assert.equal(action.operation.effect, "write"); assert.equal(action.operation.scope, "global") + assert.equal(service.cancellableDiagnostic, undefined) + const actor: AuthUser = { id: "owned-public-poll-" + randomUUID(), email: "poll@example.test", name: "Owned public poll", apps: ["mm"], + permissions: ["mm:servers:write", "mm:servers:read", "mm:mcp:manage", "mm:mcp:admin", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 3600000 } + const username = "NEVER_PUBLIC_POLL_USER", password = "NEVER_PUBLIC_POLL_PASSWORD", secrets = [username, password] + const hidden = (value: unknown) => { assert.equal(secrets.some(secret => JSON.stringify(value).includes(secret)), false, "Private endpoint or credential escaped") } + const connectionIds: string[] = [], paths: string[] = [] + let phase = "fixture", serverId: number | undefined, operationId: string | undefined, requests = 0, nativeGets = 0, backgroundReads = 0, checkedAdmissions = 0, protocolErrors = 0 + const started = Date.now(), priorEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled + const monoStarted = performance.now() + let diagnosticPlanId: string | undefined, diagnosticCredentialId: string | undefined + let diagnosticBinding: { expiresAt: number; credentialExpiresAt: number; evidenceExpiresAt: number } | undefined + let originalExecute: typeof service.execute | undefined + const clocks = () => ({ wallElapsedMs: Date.now() - started, monotonicElapsedMs: performance.now() - monoStarted }) + const diagnosticError = (error: unknown) => error && typeof error === "object" && "code" in error && typeof error.code === "string" && /^(?:[A-Z_]{1,80}|[0-9A-Z]{5})$/.test(error.code) ? error.code : "UNAVAILABLE" + const expiryDiagnostic = async (stage: string) => { + if (!expiryDiagnostics) return + try { + const { SERVER_POLL_WALL_BUDGET_MS } = await import("../../services/server-poll-limits.js") + const row = diagnosticPlanId && diagnosticCredentialId ? (await pool.query(`SELECT + extract(epoch FROM p.expires_at)*1000 AS plan_expiry, + extract(epoch FROM c.owner_valid_until)*1000 AS owner_expiry, + extract(epoch FROM (c.policy->>'expiresAt')::timestamptz)*1000 AS policy_expiry, + extract(epoch FROM k.expires_at)*1000 AS credential_expiry, + (SELECT count(*)::int FROM mcp_grants g WHERE g.connection_id=c.id AND g.revoked_at IS NULL AND g.payload->>'credentialId'=$2::text) AS grant_count, + (SELECT min(extract(epoch FROM (g.payload->>'expiresAt')::timestamptz)*1000) FROM mcp_grants g WHERE g.connection_id=c.id AND g.revoked_at IS NULL AND g.payload->>'credentialId'=$2::text) AS grant_expiry, + (SELECT extract(epoch FROM a.authority_expires_at)*1000 FROM server_poll_storage_admissions a WHERE a.execution_id=$3::uuid AND a.identity_kind='mcp') AS admission_expiry, + (SELECT extract(epoch FROM o.created_at)*1000 FROM mcp_operations o WHERE o.id=$3::uuid) AS operation_created, + (SELECT extract(epoch FROM o.finished_at)*1000 FROM mcp_operations o WHERE o.id=$3::uuid) AS operation_finished + FROM mcp_plans p JOIN mcp_connections c ON c.id=p.connection_id JOIN mcp_credentials k ON k.connection_id=c.id AND k.id=$2::uuid WHERE p.id=$1::uuid`, + [diagnosticPlanId, diagnosticCredentialId, operationId ?? null])).rows[0] as Record | undefined : undefined + const observedAt = Date.now(), remaining = (value: unknown) => value === null || value === undefined || !Number.isFinite(Number(value)) ? null : Number(value) - observedAt + console.log("Owned poll EXPIRY DIAGNOSTIC ONLY:", JSON.stringify({ stage, ...clocks(), wallBudgetMs: SERVER_POLL_WALL_BUDGET_MS, + actorRemainingMs: remaining(actor.expiresAt), bindingRemainingMs: remaining(diagnosticBinding?.expiresAt), + evidenceRemainingMs: remaining(diagnosticBinding?.evidenceExpiresAt), boundCredentialRemainingMs: remaining(diagnosticBinding?.credentialExpiresAt), + planRemainingMs: remaining(row?.plan_expiry), ownerRemainingMs: remaining(row?.owner_expiry), policyRemainingMs: remaining(row?.policy_expiry), + credentialRemainingMs: remaining(row?.credential_expiry), grantCount: row?.grant_count ?? null, minimumGrantRemainingMs: remaining(row?.grant_expiry), + admissionRemainingMs: remaining(row?.admission_expiry), operationAgeMs: row?.operation_created ? observedAt - Number(row.operation_created) : null, + operationFinishedAgoMs: row?.operation_finished ? observedAt - Number(row.operation_finished) : null })) + } catch (error) { console.log("Owned poll EXPIRY DIAGNOSTIC ONLY:", JSON.stringify({ stage, ...clocks(), code: diagnosticError(error) })) } + } + if (expiryDiagnostics) console.log("Owned poll EXPIRY DIAGNOSTIC ONLY: transparent service observer enabled; this run is not unwrapped public acceptance") + const router = createServer(async (request, response) => { + requests++; response.setHeader("content-type", "application/json") + try { + assert.equal(request.socket.remoteAddress, "127.0.0.1") + assert.equal(request.headers.authorization === "Basic " + Buffer.from(`${username}:${password}`).toString("base64"), true) + const path = request.url ?? "" + if(concurrentReads && request.method==="POST" && ["/rest/ping","/rest/tool/ping"].includes(path)) { + backgroundReads++; response.end(JSON.stringify([{sent:"4",received:"4","packet-loss":"0","avg-rtt":"1ms"}])); return + } + assert.equal(request.method, "GET"); nativeGets++ + const pollPath=["/rest/system/identity", "/rest/system/resource", "/rest/interface", "/rest/ip/address"].includes(path) + if(concurrentReads&&!pollPath){assert(/^\/rest\/[a-z0-9/_-]{1,180}$/.test(path));backgroundReads++;response.end("[]");return} + assert(pollPath); paths.push(path) + if(!concurrentReads)assert(operationId) + const rows = operationId ? (await pool.query(`SELECT a.execution_id,a.claim_token,a.claim_attempted_at,a.used_rows,e.execution_id AS anchor_execution + FROM server_poll_storage_admissions a JOIN server_poll_executions e USING(receipt_id) + WHERE a.identity_kind='mcp' AND a.execution_id=$1 AND a.owner_id=$2`, [operationId, actor.id])).rows : [] + if(concurrentReads&&!rows.length)backgroundReads++ + else { + assert.equal(rows.length, 1); assert.equal(rows[0].execution_id, operationId); assert.equal(rows[0].anchor_execution, operationId) + assert.equal(rows[0].claim_token, null); assert(rows[0].claim_attempted_at); assert(Number(rows[0].used_rows) >= 1); checkedAdmissions++ + } + const value = path === "/rest/system/identity" ? { name: "owned-public-poll-after" } : path === "/rest/system/resource" + ? { version: "7.owned", "board-name": password, uptime: "1h", "cpu-load": "2", "free-memory": "1024", "total-memory": "2048" } + // Deliberately differ from PostgreSQL JSONB key ordering. + : path === "/rest/interface" ? [{ type: "ether", name: "owned-interface" }] : [{ interface: "owned-interface", address: "192.0.2.1/24" }] + response.end(JSON.stringify(value)) + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned public poll protocol or admission mismatch"}') } + }) + const ledger = async () => { + const rows: unknown[] = [] + for (const table of ["server_poll_storage_admissions", "server_poll_executions", "server_poll_intents", "server_poll_acks", "server_poll_receipts", "server_poll_quarantines", "server_poll_storage_usage"]) + rows.push((await pool.query(`SELECT to_jsonb(t) AS value FROM ${table} t ORDER BY to_jsonb(t)::text`)).rows) + return fingerprint(rows) + } + const wait = async (id: string) => { + let nextProgressAt = Date.now() + 15000 + for (const deadline = Date.now() + 210000; Date.now() < deadline;) { + const row = (await pool.query("SELECT id,state,error_code,verification,result FROM mcp_operations WHERE id=$1", [id])).rows[0] + assert(row) + if (!["pending", "running"].includes(row.state)) return row + if (Date.now() >= nextProgressAt) { + await expiryDiagnostic("progress") + const progress = (await pool.query(`SELECT a.used_rows,a.max_stages, + (SELECT i.stage FROM server_poll_intents i WHERE i.receipt_id=a.receipt_id ORDER BY i.created_at DESC,i.stage DESC LIMIT 1) AS last_intent, + EXISTS(SELECT 1 FROM server_poll_receipts r WHERE r.receipt_id=a.receipt_id) AS has_receipt + FROM server_poll_storage_admissions a WHERE a.execution_id=$1 AND a.identity_kind='mcp'`, [id])).rows[0] + console.log("Owned public poll progress:", JSON.stringify({ state: row.state, nativeGets: requests, elapsedMs: Date.now() - started, + usedRows: progress?.used_rows ?? 0, maxStages: progress?.max_stages ?? 0, lastIntent: progress?.last_intent ?? null, hasReceipt: progress?.has_receipt ?? false })) + nextProgressAt = Date.now() + 15000 + } + await delay(50) + } + throw Error("Owned public poll did not settle; no retry permitted") + } + try { + await new Promise((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", () => { router.off("error", no); yes() }) }) + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES('owned-public-poll-before','127.0.0.1',$1,false,false,true,$2,$3) RETURNING id", [(router.address() as { port: number }).port, username, password])).rows[0].id) + await ensurePartitionFor(pool, "server_snapshots", "week", new Date()) + const ids = (await pool.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row => Number(row.id)); assert(ids.length <= 1000) + const connect = async (mode: "autonomous" | "observer", allowGlobal: boolean) => { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned public poll", mode, dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["servers.poll"], expiresAt: new Date(actor.expiresAt!).toISOString(), + permissions: ["mm:servers:write"], scope: { serverIds: ids, operationIds: ["servers.poll"], allowGlobal }, + limits: { callsPerMinute: 10000, mutationsPerHour: 10000, serversPerPlan: Math.max(1, ids.length), objectsPerPlan: 10000 } }), actor) + connectionIds.push(access.connection.id); secrets.push(access.token); return authenticateMcpToken(access.token) + } + phase = "negative-admission" + const principal = await connect("autonomous", true), observer = await connect("observer", true), scoped = await connect("autonomous", false) + const emptyLedger = await ledger() + await assert.rejects(invokeMcpTool(observer, "mm_server_poll_plan", { serverId })) + await assert.rejects(invokeMcpTool(scoped, "mm_server_poll_plan", { serverId })) + await assert.rejects(invokeMcpTool(principal, "mm_server_poll_plan", { serverId, receiptId: randomUUID(), sql: "SELECT 1" })) + assert.equal(await ledger() === emptyLedger, true); if(!concurrentReads)assert.equal(requests, 0) + phase = "prepare" + const plan = await invokeMcpTool(principal, "mm_server_poll_plan", { serverId }) as { planId: string; state: string } + hidden(plan); assert.equal(plan.state, "awaiting-approval", "Human approval remains mandatory in autonomous mode") + if(!concurrentReads)assert.equal(requests, 0); assert.equal(await ledger() === emptyLedger, true) + const planRow = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0] + const evidence = JSON.parse(await decryptSecret(planRow.evidence.sealed)) as ChangeEvidence + assert.equal(evidence.steps.length, 1); const step = evidence.steps[0]; assert.equal(step.kind, "service"); if (step.kind !== "service") throw Error("Expected actual poll service") + const binding = JSON.parse(await decryptSecret(String(step.input.bindingCiphertext))) as { ownerId: string; issuedAt: number; expiresAt: number; principal: typeof principal; plan: ServerPollPlan } + assert.equal(binding.plan.version,4);assert.equal(binding.plan.maintenanceGroupSize,8) + assert.equal(evidence.mutationCost,binding.plan.maintenance.length+8) + const mutationUsed=async()=>Number((await pool.query("SELECT COALESCE(sum(used),0)::text AS used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations'",[principal.connectionId])).rows[0].used) + if (expiryDiagnostics) { + diagnosticPlanId = plan.planId; diagnosticCredentialId = principal.credentialId + diagnosticBinding = { expiresAt: binding.expiresAt, credentialExpiresAt: Date.parse(binding.principal.credentialExpiresAt), evidenceExpiresAt: Date.parse(evidence.validUntil!) } + } + assert.equal(binding.plan.request.receiptId, step.input.receiptId); assert.equal(binding.ownerId, actor.id); assert.equal(binding.expiresAt, Date.parse(evidence.validUntil!)) + assert.equal(Object.hasOwn(binding, "executionId"), false) + await assert.rejects(service.execute(step.input, { operationId: randomUUID(), phase: "forward", stepIndex: 0 }), { code: "PERMISSION_DENIED" }) + await assert.rejects(applyPlan(principal, plan.planId, randomUUID())) + if(!concurrentReads)assert.equal(requests, 0); assert.equal(await ledger() === emptyLedger, true) + await options.beforeHumanApproval?.() + if(options.delayedApproval){ + assert.equal(binding.plan.version,4) + const source=async()=>(await pool.query("SELECT retention_days,poll_configuration_epoch,to_jsonb(s)-'poll_configuration_epoch' AS complete FROM uptime_settings s WHERE id=1")).rows[0] + const before=await source() + if(options.delayedApproval==="runtime"){ + await delay(20000) + await pool.query("UPDATE uptime_settings SET last_collected_at=clock_timestamp(),last_duration_ms=37,last_error='synthetic approval wait',updated_at=clock_timestamp() WHERE id=1") + const after=await source();assert.equal(after.poll_configuration_epoch,before.poll_configuration_epoch);assert.notDeepEqual(after.complete,before.complete) + }else if(options.delayedApproval==="configuration-aba"){ + await pool.query("UPDATE uptime_settings SET retention_days=retention_days+1 WHERE id=1") + await pool.query("UPDATE uptime_settings SET retention_days=$1 WHERE id=1",[before.retention_days]) + const after=await source();assert.deepEqual(after.complete,before.complete);assert.notEqual(after.poll_configuration_epoch,before.poll_configuration_epoch) + }else{ + await pool.query("ALTER TABLE public.uptime_settings DISABLE TRIGGER server_poll_settings_epoch");guardDisabled=true + } + } + phase = "approved-apply" + await approvePlan(plan.planId, actor, true) + await expiryDiagnostic("before-actual-apply") + if (expiryDiagnostics) { + const { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecutionLifetime } = await import("../../services/config-mutation-policy.js") + originalExecute = service.execute + const execute = originalExecute + service.execute = async function (...args) { + let removeObserver: (() => void) | undefined + try { + // Observe only the actual inherited service scope. Never create a + // verified scope, grant, signer or replacement execution result. + const lifetime = await inspectVerifiedMcpServiceExecutionLifetime(await captureVerifiedMcpServiceExecution()) + const observe = (stage: string) => console.log("Owned poll EXPIRY DIAGNOSTIC ONLY:", JSON.stringify({ stage, ...clocks(), + actualServiceRemainingMs: lifetime.expiresAt - Date.now(), actualServiceMonotonicRemainingMs: lifetime.monotonicDeadline - performance.now(), signalAborted: lifetime.signal.aborted })) + observe("actual-service-entry") + const aborted = () => observe("actual-service-signal-abort") + lifetime.signal.addEventListener("abort", aborted, { once: true }) + removeObserver = () => lifetime.signal.removeEventListener("abort", aborted) + } catch (error) { console.log("Owned poll EXPIRY DIAGNOSTIC ONLY:", JSON.stringify({ stage: "actual-service-observer-unavailable", ...clocks(), code: diagnosticError(error) })) } + try { return await Reflect.apply(execute, this, args) } finally { removeObserver?.() } + } + } + const mutationBefore=await mutationUsed() + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key); operationId = operation.id + await expiryDiagnostic("after-actual-apply") + console.log("Owned public poll apply:", JSON.stringify({ phase, elapsedMs: Date.now() - started, maintenanceEffects: binding.plan.maintenance.length })) + const terminal = await wait(operation.id); hidden(terminal) + if (options.delayedApproval==="configuration-aba" || options.delayedApproval==="guard") { + assert.equal(terminal.state,"rejected"); assert.equal(terminal.error_code,options.delayedApproval==="guard"?"STATE_UNAVAILABLE":"STATE_CONFLICT") + assert.equal(checkedAdmissions,0,"Stale plan must never dispatch a native request under an MCP admission") + const effects = (await pool.query(`SELECT + (SELECT count(*)::int FROM server_poll_storage_admissions WHERE execution_id=$1) AS admissions, + (SELECT count(*)::int FROM server_poll_executions WHERE execution_id=$1) AS executions, + (SELECT count(*)::int FROM server_poll_receipts WHERE receipt_id=$2) AS receipts, + (SELECT count(*)::int FROM server_poll_intents WHERE receipt_id=$2) AS intents`,[operation.id,binding.plan.request.receiptId])).rows[0] + assert.deepEqual(effects,{admissions:0,executions:0,receipts:0,intents:0}) + const result={scenario:options.delayedApproval??"legacy-runtime",staleHumanApprovalRefused:true,terminalState:terminal.state,errorCode:terminal.error_code, + mcpNativeJournalReads:0,mcpStorageEffects:effects,automaticRetry:false,delayedApprovalUsabilityAccepted:false, + backgroundNativeRequests:requests,elapsedMs:Date.now()-started} + console.log("Owned public poll stale approval:",JSON.stringify(result)) + return result + } + assert.equal(terminal.state, "applied", "Actual public poll terminal: " + String(terminal.error_code ?? "none")) + assert.equal(terminal.verification, "passed"); assert.equal(terminal.result.confirmedWrites, 1); assert.equal(terminal.result.verificationScope, "configuration") + if(concurrentReads){assert(requests>=4);assert(checkedAdmissions>=4)}else{assert.equal(requests,4);assert.equal(checkedAdmissions,4)} + assert.equal(protocolErrors, 0); assert.equal(new Set(paths).size, 4) + const saved = (await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0] + hidden(saved.result); assert.equal(saved.state, "verified"); assert.equal(saved.result.operationId, operation.id); assert.equal(saved.result.nativeReads, 4) + assert.equal(saved.result.complete, true); assert.equal(saved.result.maintenanceComplete, true); assert.equal(saved.result.snapshot.serverId, serverId) + for (const field of ["context", "plan", "bindingCiphertext", "rawInterfaces", "rawIpAddresses", "afterProof"]) assert.equal(Object.hasOwn(saved.result, field), false) + assert.equal(saved.result.snapshot.boardName, "[redacted]") + const admission = (await pool.query("SELECT a.*,e.execution_id AS anchor_execution FROM server_poll_storage_admissions a JOIN server_poll_executions e USING(receipt_id) WHERE a.receipt_id=$1", [saved.result.receiptId])).rows + assert.equal(admission.length, 1); assert.equal(admission[0].execution_id, operation.id); assert.equal(admission[0].anchor_execution, operation.id) + assert.equal(admission[0].owner_id, actor.id); assert.equal(admission[0].claim_token, null); assert(admission[0].claim_attempted_at) + assert.equal(admission[0].max_stages, publicationLayout(binding.plan).journalMaxStages) + assert.equal(await mutationUsed()-mutationBefore,binding.plan.mutationCost) + const ownerUsage=(await pool.query("SELECT reserved_rows,reserved_bytes FROM server_poll_storage_usage WHERE scope=$1",["owner:"+actor.id])).rows[0] + assert.equal(ownerUsage.reserved_rows,4019);assert.equal(Number(ownerUsage.reserved_bytes),8421376) + const journalRows={ + execution:(await pool.query("SELECT * FROM server_poll_executions WHERE receipt_id=$1",[saved.result.receiptId])).rows[0], + intents:(await pool.query("SELECT * FROM server_poll_intents WHERE receipt_id=$1 ORDER BY stage",[saved.result.receiptId])).rows, + acks:(await pool.query("SELECT * FROM server_poll_acks WHERE receipt_id=$1 ORDER BY stage",[saved.result.receiptId])).rows, + receipt:(await pool.query("SELECT * FROM server_poll_receipts WHERE receipt_id=$1",[saved.result.receiptId])).rows[0], + quarantines:(await pool.query("SELECT * FROM server_poll_quarantines WHERE receipt_id=$1",[saved.result.receiptId])).rows, + } + const authenticatedHistory=await(await import("../../services/server-poll-history-verification.js")).verifyServerPollHistory(journalRows) + assert.equal(authenticatedHistory.context.plan.version,binding.plan.version) + if(binding.plan.version===4){ + const anchor=JSON.parse(await decryptSecret(String(journalRows.execution.context_ciphertext))) + assert.equal(anchor.executionStart.approvalProof,binding.plan.approvalProof) + assert.equal(fingerprint(anchor.plan),fingerprint(binding.plan)) + if(options.delayedApproval==="runtime"||options.beforeHumanApproval)assert.notEqual(anchor.executionStart.fullProof,binding.plan.beforeProof) + } + phase = "persisted-read-and-idempotence" + const beforeRead = await ledger() + const beforeReadCost=await mutationUsed() + const session = createStorageDurableServerPollSession({ request: binding.plan.request, identity: { kind: "mcp", executionId: operation.id, ownerId: binding.ownerId, + issuedAt: binding.issuedAt, expiresAt: binding.expiresAt, connectionId: binding.principal.connectionId, credentialId: binding.principal.credentialId, operationName: "servers.poll" }, + authorize: createMcpServerPollAuthority(binding.principal, { deadlineAt: Math.min(binding.expiresAt, Date.now() + 180000) }) }) + const persisted = await session.readExisting(binding.plan); assert(persisted); hidden(persisted) + assert.equal(persisted.receiptId, saved.result.receiptId); assert.equal(persisted.complete, true) + assert.equal(await service.verify!(step.input, saved.result), true) + assert.equal(await ledger() === beforeRead, true, "Persisted verification must not mutate quota or journal") + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + if(!concurrentReads)assert.equal(requests, 4); assert.equal(await ledger() === beforeRead, true) + assert.equal(await mutationUsed(),beforeReadCost) + const result = { actualDefaultPublicRegistry: true, actualRuntimeHumanApprovalRequiredInAutonomousMode: true, originalSealedExpiryPreserved: true, + authenticatedCompleteHistory:true,immutableFreshStartAuthenticates:binding.plan.version===4,delayedApprovalUsabilityAccepted:binding.plan.version===4&&!!(options.beforeHumanApproval||options.delayedApproval==="runtime"),scenario:options.delayedApproval??(options.beforeHumanApproval?"actual-collector-wait":"normal"), + actualOperationIdentityBound: true, actualNativeGets: nativeGets, dispatchesAfterConsumedAdmission: concurrentReads?undefined:checkedAdmissions, innerPlanVersion: binding.plan.version, maintenanceEffects: binding.plan.maintenance.length, + completedPublications: saved.result.completedPublications, journalRows: admission[0].used_rows, persistedReadAndVerifyNoMutation: true, idempotentApplyNoNewNativeOrCharge: !concurrentReads, + actualLogicalMutationDebit:await mutationUsed()-mutationBefore,expectedFullMutationCost:binding.plan.mutationCost,unchangedReservedRows:4019,unchangedReservedBytes:8421376, + readOnlyAndScopeAndJsonDeniedBeforeNative: !concurrentReads, directServiceRequiresInheritedGuard: true, safeSnapshotOnly: true, elapsedMs: Date.now() - started, + ...(concurrentReads?{nativeRequestAttribution:"aggregate-scheduler-and-MCP-not-isolated",aggregateNativeRequests:requests,otherBackgroundReadRequests:backgroundReads,mcpNativeJournalReads:saved.result.nativeReads,deniedCallsProducedNoMcpAdmission:true,idempotentApplyNoNewMcpIntentOrCharge:true}:{}), } + console.log(expiryDiagnostics ? "Owned poll EXPIRY DIAGNOSTIC ONLY runtime:" : "Owned public poll runtime:", JSON.stringify(result)); return result + } catch (error) { + await expiryDiagnostic("failure") + let progress: Record = {} + try { + if (operationId) progress = (await pool.query(`SELECT a.used_rows, + (SELECT count(*)::int FROM server_poll_receipts r WHERE r.receipt_id=a.receipt_id) AS receipt_rows, + (SELECT count(*)::int FROM server_poll_quarantines q WHERE q.receipt_id=a.receipt_id) AS quarantine_rows, + (SELECT i.stage FROM server_poll_intents i WHERE i.receipt_id=a.receipt_id ORDER BY i.created_at DESC,i.stage DESC LIMIT 1) AS last_stage, + (SELECT count(*)::int FROM server_poll_acks k WHERE k.receipt_id=a.receipt_id) AS ack_rows + FROM server_poll_storage_admissions a WHERE a.execution_id=$1 AND a.identity_kind='mcp'`, [operationId])).rows[0] ?? {} + } catch { /* Diagnostic failure must not create a second runner attempt. */ } + console.log("Owned public poll failure:", JSON.stringify({ phase, elapsedMs: Date.now() - started, nativeGets: requests, admissionChecked: checkedAdmissions, protocolErrors, + progress, + code: error && typeof error === "object" && "code" in error ? String(error.code).slice(0, 80) : "ASSERTION_OR_UNAVAILABLE" })) + throw error + } finally { + if(guardDisabled)await pool.query("ALTER TABLE public.uptime_settings ENABLE ALWAYS TRIGGER server_poll_settings_epoch") + if (originalExecute) service.execute = originalExecute + router.closeAllConnections(); if (router.listening) await new Promise(yes => router.close(() => yes())) + for (const connectionId of connectionIds) await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}',to_jsonb($2::text)) WHERE id=$1", [connectionId, options.cleanupStatus ?? "revoked"]) + if (serverId !== undefined) await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [priorEnabled]) + } +} diff --git a/backend/src/modules/mcp/server-poll-production.test.ts b/backend/src/modules/mcp/server-poll-production.test.ts new file mode 100644 index 00000000..5e55eef3 --- /dev/null +++ b/backend/src/modules/mcp/server-poll-production.test.ts @@ -0,0 +1,56 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { pool } from "../../db/index.js" +import { withVerifiedConfigurationChange } from "../../services/config-mutation-policy.js" +import { createProductionServerPollMcpAdapter, queryServerPollMcpMetadata } from "./server-poll-production.js" + +test("fixed production construction is inert and direct execution needs the real inherited verified guard", async () => { + const priorQuery = pool.query, priorConnect = pool.connect + let io = 0, inheritedChecks = 0 + pool.query = (() => { io++; throw Error("Unexpected production I/O") }) as never + pool.connect = (() => { io++; throw Error("Unexpected production checkout") }) as never + try { + const adapter = createProductionServerPollMcpAdapter(), context = { operationId: randomUUID(), phase: "forward" as const, stepIndex: 0 } + assert.equal(adapter.service.cancellableDiagnostic, undefined) + await assert.rejects(adapter.service.execute({}, context), { code: "PERMISSION_DENIED" }) + await withVerifiedConfigurationChange({ actorId: "owner", connectionId: randomUUID(), approved: true, serverIds: [1], objects: [], serverWide: true, + authorize: async () => { inheritedChecks++ } }, async () => { await assert.rejects(adapter.service.execute({}, context)) }) + assert.equal(inheritedChecks, 1); assert.equal(io, 0) + } finally { pool.query = priorQuery; pool.connect = priorConnect } +}) + +test("metadata keeps its read-only transaction, bounded local settings and cleanup in one setup query", async () => { + const prior = pool.connect, commands: string[] = [], releases: boolean[] = [] + pool.connect = (async () => ({ + async query(config: { text: string; values?: unknown[]; query_timeout: number }) { + commands.push(config.text) + assert(config.query_timeout > 0 && config.query_timeout <= 10000) + return { rows: config.text === "SELECT $1::int AS value" ? [{ value: config.values?.[0] }] : [], command: "SELECT" } + }, + release(unsafe: boolean) { releases.push(unsafe) }, + })) as never + try { + const rows = await queryServerPollMcpMetadata("SELECT $1::int AS value", [7], { deadlineAt: Date.now() + 5000 }) + assert.deepEqual(rows, [{ value: 7 }]) + assert.deepEqual(commands, ["BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY", "SELECT set_config('statement_timeout','10s',true),set_config('TimeZone','UTC',true)", "SELECT $1::int AS value", "ROLLBACK"]) + assert.deepEqual(releases, [false]) + } finally { pool.connect = prior } +}) + +test("abort during local setup never reaches metadata or a cleanup command on the destroyed lease", async () => { + const prior = pool.connect, controller = new AbortController(), commands: string[] = [], releases: boolean[] = [] + pool.connect = (async () => ({ + async query(config: { text: string }) { + commands.push(config.text) + if (config.text.startsWith("SELECT set_config")) controller.abort() + return { rows: [], command: "SELECT" } + }, + release(unsafe: boolean) { releases.push(unsafe) }, + })) as never + try { + await assert.rejects(queryServerPollMcpMetadata("SELECT forbidden_after_abort", [], { deadlineAt: Date.now() + 5000, signal: controller.signal }), { code: "STATE_UNAVAILABLE" }) + assert.equal(commands.length, 2) + assert.deepEqual(releases, [true]) + } finally { pool.connect = prior } +}) diff --git a/backend/src/modules/mcp/server-poll-production.ts b/backend/src/modules/mcp/server-poll-production.ts new file mode 100644 index 00000000..73fe6074 --- /dev/null +++ b/backend/src/modules/mcp/server-poll-production.ts @@ -0,0 +1,43 @@ +import { carryPollError } from "../../services/server-poll-error-provenance.js" +/** Fixed production assembly. Importing or registering performs no I/O. */ +import { createAtomicStorageDurableServerPollSession } from "../../services/server-poll-durable-session.js" +import { createMcpServerPollAuthority, createCombinedRuntimeMcpServerPollAuthority } from "../../services/server-poll-authority.js" +import { connectProbeStorage } from "../../services/server-probe-storage-connection.js" +import type { ProbeStorageConnection } from "../../services/server-probe-journal-maintenance.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { decryptSecret, encryptSecret, privateSecretFingerprint, fingerprint, McpError } from "./security.js" +import { createServerPollMcpAdapterForTests, type ServerPollMcpDependencies } from "./server-poll-actions.js" +import { readServerPollArchivedHistory } from "../../services/server-poll-history-reader.js" + +/** Metadata checks do not acquire the poll global quota lock: authority is + * called from inside that lock and must never wait for itself on another lease. */ +export const queryServerPollMcpMetadata: ServerPollMcpDependencies["query"] = async (text, values, options) => { + const deadlineAt = Math.min(options.deadlineAt, Date.now() + 10000) + let connection: ProbeStorageConnection | undefined, active = true, released = false, busy = false, begun = false + const fail = () => new McpError("STATE_UNAVAILABLE", "Источник подтверждённой операции недоступен", 503) + const close = (unsafe: boolean) => { if (connection && !released) { released = true; try { connection.release(unsafe) } catch { /* No driver details. */ } } } + const live = () => { if (!active || released || options.signal?.aborted || Date.now() >= deadlineAt) throw fail() } + let stop!: () => void + const stopped = new Promise((_yes, reject) => { stop = () => { active = false; close(true); reject(fail()) } }) + const timer = setTimeout(stop, Math.max(1, deadlineAt - Date.now())); options.signal?.addEventListener("abort", stop, { once: true }) + const execute = async (sql: string, parameters: unknown[] = []) => { + live(); if (!connection || busy) throw fail(); busy = true + try { const result = await connection.query(sql, parameters, { signal: options.signal, timeoutMs: Math.max(1, deadlineAt - Date.now()) }); live(); return result } + finally { busy = false } + } + const work = Promise.resolve().then(async () => { + live(); connection = await connectProbeStorage({ ...options, deadlineAt }); if (!active) { close(true); throw fail() } + await execute("BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY"); begun = true + await execute("SELECT set_config('statement_timeout','10s',true),set_config('TimeZone','UTC',true)") + const result = await execute(text, values); await execute("ROLLBACK"); begun = false; return result.rows + }) + try { return await Promise.race([work, stopped]) } + catch (error) { throw carryPollError(fail(), error, "poll.meta") } + finally { active = false; clearTimeout(timer); options.signal?.removeEventListener("abort", stop); close(begun || busy) } +} + +export function createProductionServerPollMcpAdapter() { + return createServerPollMcpAdapterForTests({ authority: createMcpServerPollAuthority, combinedAuthority: createCombinedRuntimeMcpServerPollAuthority, session: createAtomicStorageDurableServerPollSession, query:queryServerPollMcpMetadata,readArchived:readServerPollArchivedHistory, + seal: encryptSecret, open: decryptSecret, proof: value => privateSecretFingerprint(fingerprint(value), "server-poll-mcp-binding-v1"), + verified: assertVerifiedConfigurationAccess, now: Date.now }) +} diff --git a/backend/src/modules/mcp/server-poll-registration.test.ts b/backend/src/modules/mcp/server-poll-registration.test.ts new file mode 100644 index 00000000..3165b196 --- /dev/null +++ b/backend/src/modules/mcp/server-poll-registration.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { mcpOperationDescriptorSchema } from "@mmapp/contracts/mcp" +import type { add } from "./actions.js" +import { registerServerPollAction } from "./server-poll-registration.js" +import { findMcpService } from "./service-registry.js" + +test("registration exposes one actual global write plan and fixed nondiagnostic service", async () => { + const calls: Parameters[] = [] + registerServerPollAction(((...args: Parameters) => { calls.push(args) }) as typeof add) + assert.equal(calls.length, 1) + const [id, tool, description, section, permission, effect, scope, schema, handlers] = calls[0] + assert.match(description, /8 на владельца, 128 на приложение/) + assert.match(description, /Архивирование сохраняет аудит/) + assert.match(description, /pollStorageCapacity/) + assert.equal(mcpOperationDescriptorSchema.shape.title.safeParse(description).success, true) + assert.equal(id, "servers.poll"); assert.equal(tool, "mm_server_poll_plan"); assert.equal(section, "servers") + assert.equal(permission, "mm:servers:write"); assert.equal(effect, "write"); assert.equal(scope, "global"); assert.equal(handlers.version, 1) + assert(handlers.prepare); assert(handlers.target); assert.equal(handlers.read, undefined) + assert(schema.safeParse({ serverId: 1 }).success) + for (const input of [{ serverId: 1, identity: {} }, { serverId: 1, sql: "SELECT 1" }, { serverId: 1, receiptId: "forged" }, { serverId: 0 }]) assert(!schema.safeParse(input).success) + const service = findMcpService(id); assert(service); assert.equal(service.cancellableDiagnostic, undefined) + for (const method of ["current", "execute", "verify", "reconcile"] as const) assert.equal(typeof service[method], "function") + assert.equal(await service.verify!({}, {}), false) +}) diff --git a/backend/src/modules/mcp/server-poll-registration.ts b/backend/src/modules/mcp/server-poll-registration.ts new file mode 100644 index 00000000..aa6ed6a5 --- /dev/null +++ b/backend/src/modules/mcp/server-poll-registration.ts @@ -0,0 +1,14 @@ +import type { add } from "./actions.js" +import { SERVER_POLL_OPERATION } from "../../services/server-poll-authority.js" +import { SERVER_POLL_MCP_OPERATION_VERSION } from "./server-poll-actions.js" +import { createProductionServerPollMcpAdapter } from "./server-poll-production.js" +import { registerMcpService } from "./service-registry.js" +import { SERVER_POLL_STORAGE_POLICY } from "../../services/server-poll-storage-model.js" + +/** Normal startup enrolls quota-bound storage before public tools are served. */ +export function registerServerPollAction(register: typeof add) { + const adapter = createProductionServerPollMcpAdapter() + registerMcpService(SERVER_POLL_OPERATION, adapter.service) + register(SERVER_POLL_OPERATION, "mm_server_poll_plan", `Полный опрос и история. Резерв: ${SERVER_POLL_STORAGE_POLICY.ownerAttempts} на владельца, ${SERVER_POLL_STORAGE_POLICY.globalAttempts} на приложение. Архивирование сохраняет аудит; остаток зависит от объёма истории. Статус: pollStorageCapacity в MCP overview.`, "servers", "mm:servers:write", "write", "global", adapter.schema, + { version: SERVER_POLL_MCP_OPERATION_VERSION, target: adapter.target, prepare: adapter.prepare }) +} diff --git a/backend/src/modules/mcp/server-probe-actions.test.ts b/backend/src/modules/mcp/server-probe-actions.test.ts new file mode 100644 index 00000000..3d07a16b --- /dev/null +++ b/backend/src/modules/mcp/server-probe-actions.test.ts @@ -0,0 +1,186 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { createServerProbeMcpAdapter, serverProbeActionSchema, serverProbeSafeMcpError, type ServerProbeMcpDependencies } from "./server-probe-actions.js" +import { ServerProbeExecutionError, type PreparedServerProbe, type ServerProbeAuthority, type ServerProbeReceipt, type ServerProbeJournal } from "../../services/server-probe-execution.js" +import type { ServerProbeJournalContext } from "../../services/server-probe-journal.js" +import { createServerProbeJournalHistoryReader } from "../../services/server-probe-journal-history.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import { fingerprint, redact } from "./security.js" + +const now = 1800000000000, operationId = randomUUID(), connectionId = randomUUID(), credentialId = randomUUID(), receiptId = randomUUID() +const principal = { connectionId, credentialId, credentialExpiresAt: new Date(now + 600000).toISOString() } +const proof = async (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex") +const context = { principal, connection: { id: connectionId, owner_id: "owner", owner_claims_issued_at: new Date(now - 1000) } } as ActionContext +let assertions = 0 +function fixture(retiredSource?: ServerProbeMcpDependencies["retiredSource"]) { + const events: string[] = [], receipts = new Map(), contexts: ServerProbeJournalContext[] = [], sealed = new Map() + let evidence: ChangeEvidence, result: unknown = null, stepState = "intent", operationState = "running", scope = [2, 9], revoked = false, sqlTamper: string | undefined, dispatchFailure: unknown, preparedTool: PreparedServerProbe["tool"] = "ping", mismatchDestination = false, nextCipher = 0, revokeOnQuery = false, expiredPrepared = false, clock = now, liveHistory = true + const access: ServerProbeAuthority = { transport: "mcp", ownerId: "owner", connectionId, credentialId, expiresAt: now + 600000, generation: "generation", policyProof: "1".repeat(64), ownerProof: "2".repeat(64), active: true, operationAllowed: true, scopeServerIds: scope, ownerPermissions: ["mm:servers:write"], policyPermissions: ["mm:servers:write"] } + const readAuthority = async () => { events.push("authority"); if (revoked) throw new ServerProbeExecutionError("PERMISSION_DENIED", "PRIVATE_PROVIDER_SECRET", 403); return { ...access, expiresAt: clock + 600000, scopeServerIds: [...scope] } } + const assertProtections = async (ids: number[]) => { events.push("protections"); if (ids.some(id => !scope.includes(id))) throw new Error("out of scope") } + const seal = async (value: string) => { const id = `opaque-${++nextCipher}`; sealed.set(id, value); return id } + const open = async (value: string) => { const clear = sealed.get(value); if (!clear) throw new Error("bad cipher"); return clear } + const prepared = (): PreparedServerProbe => ({ receiptId, serverId: 9, tool: preparedTool, expiresAt: new Date(now + 300000).toISOString(), bindingCiphertext: "inner-private", ...(preparedTool === "bandwidth" ? { destinationServerId: mismatchDestination ? 3 : 2 } : {}) }) + const preflight = async (value: PreparedServerProbe, signal?: AbortSignal) => { events.push("preflight"); if (signal?.aborted || expiredPrepared) throw new ServerProbeExecutionError("CANCELLED", "cancelled", 502); await readAuthority(); await assertProtections([value.serverId, ...(value.destinationServerId ? [value.destinationServerId] : [])]); return { receiptId: value.receiptId, bindingProof: "3".repeat(64), tool: value.tool, serverIds: [value.serverId, ...(value.destinationServerId ? [value.destinationServerId] : [])], expiresAt: value.expiresAt } } + const journal: ServerProbeJournal = { + claim: async () => { events.push("claim"); return "claimed" }, intent: async () => { events.push("intent") }, received: async () => {}, quarantine: async () => {}, + finish: async value => { receipts.set(value.receiptId, value) }, + read: async id => { events.push("journal.read"); return receipts.get(id) }, + } + const deps: ServerProbeMcpDependencies = { + retiredSource, + authority: (() => ({ readAuthority, assertProtections })) as ServerProbeMcpDependencies["authority"], + historicalAuthority: ((currentPrincipal: typeof principal) => ({ readAuthority: async () => { events.push("read-authority"); return { ...await readAuthority(), connectionId: currentPrincipal.connectionId, credentialId: currentPrincipal.credentialId } }, assertProtections })) as ServerProbeMcpDependencies["historicalAuthority"], + history: dependencies => createServerProbeJournalHistoryReader({ ...dependencies, loadArchive: async binding => { + events.push("history.read"); const receipt = receipts.get(binding.receiptId); if (!receipt) return [] + const identity = binding.context.identity; assert.equal(identity.kind, "mcp"); if (identity.kind !== "mcp") return [] + assert.equal(identity.credentialId, credentialId) + const plaintext = JSON.stringify({ version: 1, context: binding.context, bindingProof: binding.bindingProof, receipt }) + const ciphertext = "archive-" + await proof(plaintext); sealed.set(ciphertext, plaintext) + return [{ receipt_id: binding.receiptId, binding_proof: binding.bindingProof, identity_kind: "mcp", execution_id: operationId, actor_id: "owner", authentication: "mcp", mcp_connection_id: connectionId, mcp_credential_id: credentialId, request_id: null, authority_issued_at: now - 1000, authority_expires_at: new Date(now + 600000), source_server_id: receipt.serverId, destination_server_id: receipt.destinationServerId ?? null, tool: receipt.tool, state: receipt.state, attempted_stages: receipt.attemptedStages, completed_stages: receipt.completedStages, receipt_ciphertext: ciphertext, receipt_proof: await proof(plaintext), quarantine_count: "0" }] + }, crypto: { decrypt: open, proof: (_domain, value) => proof(value) } }), + executor: ((options: { journal?: ServerProbeJournal }) => ({ + prepare: async (_id: number, request: { tool: PreparedServerProbe["tool"] }) => { events.push("prepare"); preparedTool = request.tool; return prepared() }, preflight, + execute: async (value: PreparedServerProbe, signal?: AbortSignal) => { + if (signal) events.push("trusted-signal") + events.push("execute"); if (dispatchFailure) throw dispatchFailure + assert.ok(options.journal); await options.journal.claim({ receiptId, bindingProof: "3".repeat(64), transport: "mcp", ownerId: "owner", connectionId, serverIds: [9, ...(value.destinationServerId ? [value.destinationServerId] : [])], tool: value.tool }) + const receipt: ServerProbeReceipt = { receiptId, serverId: value.serverId, ...(value.destinationServerId ? { destinationServerId: value.destinationServerId } : {}), tool: value.tool, state: "completed", httpStatus: 200, output: "PUBLIC_MEASUREMENT".repeat(900), data: { kind: value.tool, reply: true }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } + await options.journal.finish(receipt); return receipt + }, + })) as ServerProbeMcpDependencies["executor"], + journal: (ctx, dependencies, execution) => { assert.equal(execution?.deadlineAt, Date.parse(prepared().expiresAt)); contexts.push(ctx); return { ...journal, read: async (...args) => { await dependencies.assertAuthority(ctx, args[1]); return journal.read(...args) }, claim: async binding => { await dependencies.assertAuthority(ctx, binding.bindingProof); return journal.claim(binding) } } }, + query: async (sql, values) => { + events.push("sql"); if (revokeOnQuery) revoked = true + if (sql.includes("probe-history:scope")) { assert.equal(values[2], connectionId); assert.equal(values[3], "owner"); return liveHistory ? [{ source_server_id: 9, destination_server_id: preparedTool === "bandwidth" ? 2 : null }] : [] } + if (sql.includes("FROM servers")) { assert.ok(sql.includes("ANY($1::int[])")); assert.deepEqual(values[0], scope); return sql.includes("AND id=$2") ? [] : [{ id: 2, host: "peer", enabled: true }] } + if (!sql.includes("mcp_operation_steps")) return [{ evidence: { sealed: await seal(JSON.stringify(evidence)) } }] + assert.equal(values[0], connectionId); assert.equal(values[1], "owner"); assert.equal(values[2], "servers.probes.run") + const intended = redact(evidence.steps[0]) + assert.equal(fingerprint(JSON.parse(String(values[4]))), fingerprint(intended)) + if (sqlTamper === "none") return [] + const row = { operation_id: operationId, operation_state: operationState, plan_state: operationState === "running" ? "executing" : "completed", approved_by: "approver", step_state: stepState, step_index: 0, intended_state: intended, result, evidence: { sealed: await seal(JSON.stringify(evidence)) } } + if (sqlTamper === "digest") row.intended_state = { mismatch: true } + if (sqlTamper === "unapproved") row.approved_by = "" + if (sqlTamper === "step") row.step_index = 1 + if (sqlTamper === "operation") row.operation_id = randomUUID() + if (sqlTamper === "evidence") row.evidence.sealed = await seal(JSON.stringify({ ...evidence, requiresHuman: false })) + return sqlTamper === "duplicate" ? [row, row] : [row] + }, seal, open, proof, now: () => clock, + } + const adapter = createServerProbeMcpAdapter(deps) + const retireHistory = () => { liveHistory = false } + const prepare = async (tool: PreparedServerProbe["tool"] = "ping") => { evidence = await adapter.prepare({ serverId: 9, request: tool === "bandwidth" ? { tool, bwRemoteAddress: "peer", dstServerId: 8 } : { tool, target: "192.0.2.1" } }, context); const step = evidence.steps[0]; assert.equal(step.kind, "service"); return step.kind === "service" ? step.input : assert.fail() } + return { adapter, events, receipts, contexts, prepare, retireHistory, setResult: (value: unknown) => { result = value; stepState = "applied" }, finishOperation: () => { operationState = "applied"; stepState = "acknowledged" }, setTamper: (value?: string) => { sqlTamper = value }, revoke: () => { revoked = true }, setScope: (value: number[]) => { scope = value }, fail: (value: unknown) => { dispatchFailure = value }, revokeOnQuery: () => { revokeOnQuery = true }, evidence: () => evidence, mismatchDestination: () => { mismatchDestination = true }, expirePrepared: () => { expiredPrepared = true; clock = now + 3600000 } } +} + +for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"] as const) { + const f = fixture(), input = await f.prepare(tool), evidence = f.evidence() + assert.equal(evidence.requiresHuman, true); assert.equal(evidence.verificationScope, "acknowledgement"); assert.equal(evidence.mutationCost, tool === "mtu" ? 11 : tool === "ping" || tool === "traceroute" ? 2 : 1) + assert.deepEqual(evidence.target, { scope: "servers", serverIds: tool === "bandwidth" ? [2, 9] : [9] }); assert.ok(!f.events.includes("sql") && !f.events.includes("claim")) + if (tool === "bandwidth") assert.ok(evidence.warnings.some(text => text.includes("2 (запрошен 8)"))) + const before = await f.adapter.service.current(input); assert.deepEqual(before, evidence.before); assert.equal(f.contexts.length, 0) + const output = await f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0 }) as Record + assert.equal(output.operationId, operationId); assert.equal(output.status, "completed"); assert.ok(!JSON.stringify(output).includes("PUBLIC_MEASUREMENT")); assert.ok(!("confirmedWrites" in output)); assert.ok(Number(output.totalPages) > 1) + assert.equal(f.contexts[0].identity.executionId, operationId); assert.equal(f.contexts[0].identity.issuedAt, now - 1000) + f.setResult(output); assert.equal(await f.adapter.service.verify!(input, output), true) + assert.equal(await f.adapter.service.verify!(input, { ...output, receiptId: randomUUID() }), false) + f.finishOperation(); const executions = f.events.filter(event => event === "execute").length + assert.deepEqual(await f.adapter.service.reconcile!(input, async () => {}), { matchesDesiredState: true, matchesOriginalState: false }) + const page = await f.adapter.readPage({ operationId, receiptId, page: 0 }, context) + assert.equal(page.sha256, output.sha256); assert.ok(Buffer.from(page.contentB64, "base64").toString().includes("PUBLIC_MEASUREMENT")); assert.equal(f.events.filter(event => event === "execute").length, executions) + await assert.rejects(f.adapter.readPage({ operationId, receiptId: randomUUID(), page: 0 }, context)); assertions += 18 +} +for (const tamper of ["none", "duplicate", "digest", "step", "unapproved", "operation", "evidence"]) { + const f = fixture(), input = await f.prepare(); f.setTamper(tamper) + await assert.rejects(f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0 })); assert.equal(f.events.includes("execute"), false); assertions++ +} +for (const context of [undefined, { operationId, phase: "recovery" as const }, { operationId, phase: "forward" as const, stepIndex: 1 }]) { + const f = fixture(), input = await f.prepare(); await assert.rejects(f.adapter.service.execute(input, context)); assert.equal(f.events.includes("execute"), false); assertions++ +} +{ + const f = fixture(), input = await f.prepare() + for (const changed of [{ ...input, operationId }, { ...input, inputDigest: "0".repeat(64) }, { ...input, receiptId: randomUUID() }, { ...input, bindingCiphertext: "foreign" }]) { await assert.rejects(f.adapter.service.current(changed)); assertions++ } + f.revoke(); await assert.rejects(f.adapter.service.current(input)); assert.equal(f.events.includes("journal.read"), false); assertions++ +} +{ + const f = fixture(); assert.deepEqual(await f.adapter.target({ serverId: 9, request: { tool: "bandwidth", bwRemoteAddress: "peer", dstServerId: 8 } }, context), { scope: "servers", serverIds: [2, 9] }); assertions++ + f.setScope([2]); await assert.rejects(f.adapter.target({ serverId: 9, request: { tool: "ping", target: "192.0.2.1" } }, context)); assertions++ +} +{ + const f = fixture(); f.revokeOnQuery(); await assert.rejects(f.adapter.target({ serverId: 9, request: { tool: "bandwidth", bwRemoteAddress: "peer", dstServerId: 8 } }, context)); assert.equal(f.events.filter(event => event === "sql").length, 1); assertions++ +} +{ + const f = fixture(), input = await f.prepare(); f.fail(new ServerProbeExecutionError("JOURNAL_UNKNOWN", "PRIVATE_PROVIDER_SECRET", 503, "unknown", 1, 0)) + const error = await f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0 }).catch(error => error) + assert.ok(error && typeof error === "object" && "code" in error && "diagnosticProgress" in error) + const progress = error.diagnosticProgress; assert.ok(progress && typeof progress === "object" && "attemptedStages" in progress) + assert.equal(error.code, "JOURNAL_UNKNOWN"); assert.equal(progress.attemptedStages, 1); assert.ok(!JSON.stringify(error).includes("PRIVATE_PROVIDER_SECRET")); assert.ok(!("nativeProgress" in error)); assertions += 3 +} +{ + const f = fixture(), input = await f.prepare(), output = await f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0 }); f.setResult(output) + f.receipts.set(receiptId, { ...f.receipts.get(receiptId)!, serverId: 2 }); assert.equal(await f.adapter.service.verify!(input, output), false); await assert.rejects(f.adapter.readPage({ operationId, receiptId, page: 0 }, context)); assertions += 2 +} +for (const input of [{ serverId: 9, request: { tool: "ping", target: "x", credential: "secret" } }, { serverId: 9, operationId, request: { tool: "route", target: "192.0.2.1" } }, { serverId: 9, request: { tool: "route", target: "host" } }]) { assert.equal(serverProbeActionSchema.safeParse(input).success, false); assertions++ } +{ + const f = fixture(), input = await f.prepare(), controller = new AbortController() + assert.equal(f.adapter.service.cancellableDiagnostic, true) + await f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0, signal: controller.signal }); assert.ok(f.events.includes("trusted-signal")) + controller.abort(); const attempted = f.events.filter(event => event === "execute").length + await assert.rejects(f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0, signal: controller.signal })); assert.equal(f.events.filter(event => event === "execute").length, attempted); assertions += 3 +} +assert.ok(!("diagnosticProgress" in serverProbeSafeMcpError(new ServerProbeExecutionError("X", "secret", 500, "unknown", 99), { receiptId, operationId, stageBudget: 1 }))); assertions++ +{ + const hash = "3".repeat(64), date = (value: number) => new Date(value).toISOString().replace("Z", "000Z") + const retired = { source: { version: 1 as const, operation: "servers.probes.run" as const, operationVersion: 1 as const, tool: "ping" as const, + receiptId, executionId: operationId, connectionId, ownerId: "owner", planId: randomUUID(), originalCredentialId: credentialId, + sourceServerId: 9, serverIds: [9], issuedAt: now - 1000, authorityExpiresAt: date(now + 600000), terminalAt: date(now + 600000), + activityAt: date(now + 600000), gcNotBefore: date(now + 87060000), retentionDays: 1, safetyMs: 60000, + bindingProof: hash, sourceDigest: hash, terminalDigest: hash, stageSnapshotDigest: hash, receiptProof: hash, ciphertextSha256: hash, + }, payloadPurged: false } + let lookups = 0, changeOnRecheck = false + const f = fixture(async lookup => { + lookups++ + assert.deepEqual(lookup, { receiptId, operationId, connectionId, ownerId: "owner", scopeServerIds: [2, 9] }) + return changeOnRecheck && lookups % 2 === 0 ? undefined : retired + }) + const input = await f.prepare(), output = await f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0 }) + f.setResult(output); f.finishOperation() + const request = { operationId, receiptId, page: 0 } + const live = await f.adapter.readPage(request, context) + assert.equal(lookups, 0) + f.retireHistory() + const rotated = { ...context, principal: { ...principal, credentialId: randomUUID() } } + assert.deepEqual(await f.adapter.readPage(request, rotated), live) + assert.deepEqual(await f.adapter.receiptTarget(request, rotated), { scope: "servers", serverIds: [9] }) + const beforeForeign = lookups + await assert.rejects(f.adapter.readPage(request, { ...rotated, connection: { ...context.connection, owner_id: "foreign" } })) + assert.equal(lookups, beforeForeign) + lookups = 0; changeOnRecheck = true + const privateReads = f.events.filter(event => event === "history.read").length + await assert.rejects(f.adapter.readPage(request, rotated)) + assert.equal(f.events.filter(event => event === "history.read").length, privateReads) + assertions += 7 +} +{ + const f = fixture(), input = await f.prepare("bandwidth"), output = await f.adapter.service.execute(input, { operationId, phase: "forward", stepIndex: 0 }); f.setResult(output); f.finishOperation(); f.expirePrepared() + await assert.rejects(f.adapter.service.current(input)) + const rotatedContext = { ...context, principal: { ...principal, credentialId: randomUUID(), credentialExpiresAt: new Date(now + 7200000).toISOString() } } + const preflights = f.events.filter(event => event === "preflight").length, claims = f.events.filter(event => event === "claim").length + const page = await f.adapter.readPage({ operationId, receiptId, page: 0 }, rotatedContext) + assert.equal(page.receiptId, receiptId); assert.deepEqual(await f.adapter.receiptTarget({ operationId, receiptId, page: 0 }, rotatedContext), { scope: "servers", serverIds: [2, 9] }) + assert.equal(f.events.filter(event => event === "preflight").length, preflights); assert.equal(f.events.filter(event => event === "claim").length, claims) + f.setScope([9]); await assert.rejects(f.adapter.readPage({ operationId, receiptId, page: 0 }, rotatedContext)); f.setScope([2, 9]) + await assert.rejects(f.adapter.readPage({ operationId, receiptId, page: 0 }, { ...rotatedContext, principal: { ...rotatedContext.principal, connectionId: randomUUID() } })) + await assert.rejects(f.adapter.readPage({ operationId, receiptId, page: 0 }, { ...rotatedContext, connection: { ...context.connection, owner_id: "foreign" } })) + f.revoke(); await assert.rejects(f.adapter.readPage({ operationId, receiptId, page: 0 }, rotatedContext)); assertions += 8 +} +{ + const f = fixture(), input = await f.prepare("bandwidth") + assert.deepEqual(await f.adapter.service.diagnosticIdentity!(input, { operationId, phase: "forward", stepIndex: 0 }), { receiptId, stageBudget: 1 }) + assert.equal(f.events.includes("execute"), false); assert.equal(f.events.includes("claim"), false) + await assert.rejects(f.adapter.service.diagnosticIdentity!(input, { operationId, phase: "recovery" })) + await assert.rejects(f.adapter.service.diagnosticIdentity!({ ...input, inputDigest: "0".repeat(64) }, { operationId, phase: "forward", stepIndex: 0 })) + f.revoke(); await assert.rejects(f.adapter.service.diagnosticIdentity!(input, { operationId, phase: "forward", stepIndex: 0 })); assertions += 6 +} +console.log(`server-probe MCP adapter: ${assertions} inert assertions passed`) diff --git a/backend/src/modules/mcp/server-probe-actions.ts b/backend/src/modules/mcp/server-probe-actions.ts new file mode 100644 index 00000000..d5c2d3aa --- /dev/null +++ b/backend/src/modules/mcp/server-probe-actions.ts @@ -0,0 +1,265 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { createProductionServerProbeExecutor, ServerProbeExecutionError, type PreparedServerProbe, type ServerProbeJournal, type ServerProbeReceipt } from "../../services/server-probe-execution.js" +import { serverProbeExecutionRequest } from "../../services/server-probe-execution-model.js" +import { createMcpServerProbeAuthority, createMcpServerProbeReadAuthority, SERVER_PROBE_OPERATION } from "../../services/server-probe-authority.js" +import { createServerProbeJournalHistoryReader } from "../../services/server-probe-journal-history.js" +import { createSqlServerProbeJournal, type ServerProbeJournalContext, type ServerProbeJournalDependencies } from "../../services/server-probe-journal.js" +import { resolveServerProbeRestBandwidthPeer } from "../../services/server-probe-rest-contract.js" +import { serverProbeReceiptPage } from "../../services/server-probe-public-result.js" +import { decryptSecret, encryptSecret, fingerprint, privateSecretFingerprint, McpError, redact } from "./security.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { McpServiceHandler } from "./service-registry.js" +import type { findRetiredServerProbeHistory, ServerProbeRetiredLookup } from "../../services/server-probe-retired-history.js" + +/** Foundations only: importing this module registers no tools or services. */ +export const serverProbeActionSchema = z.object({ serverId: z.number().int().positive().safe(), request: serverProbeExecutionRequest }).strict() +export const SERVER_PROBE_MCP_OPERATION_VERSION = 1 +export const serverProbeReceiptReadSchema = z.object({ operationId: z.string().uuid(), receiptId: z.string().uuid(), page: z.number().int().min(0).max(15) }).strict() +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const serviceInputSchema = z.object({ serverId: z.number().int().positive().safe(), receiptId: z.string().uuid(), inputDigest: hash, bindingCiphertext: z.string().min(1).max(1048576) }).strict() +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict() +const preparedSchema = z.object({ receiptId: z.string().uuid(), tool: z.enum(["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]), serverId: z.number().int().positive().safe(), destinationServerId: z.number().int().positive().safe().optional(), expiresAt: z.string().datetime(), bindingCiphertext: z.string().min(1).max(524288) }).strict() +const bindingSchema = z.object({ version: z.literal(1), operation: z.literal(SERVER_PROBE_OPERATION), principal: principalSchema, ownerId: z.string().min(1).max(200), issuedAt: z.number().int().nonnegative().safe(), authorityExpiresAt: z.number().int().positive().safe(), prepared: preparedSchema, bindingProof: hash, stageBudget: z.number().int().min(1).max(11) }).strict() +export type ServerProbeMcpBinding = z.infer +type Binding = ServerProbeMcpBinding +type Input = z.infer +type OwnedStep = { operation_id: string; operation_state: string; plan_state: string; approved_by: string | null; step_state: string; step_index: number; intended_state: unknown; result: unknown; evidence: { sealed: string } } +type Query = (sql: string, values: unknown[]) => Promise[]> +export interface ServerProbeMcpDependencies { + authority: typeof createMcpServerProbeAuthority + historicalAuthority: typeof createMcpServerProbeReadAuthority + history: (dependencies: Parameters[0], execution?: { signal?: AbortSignal }) => ReturnType + executor: typeof createProductionServerProbeExecutor + journal: (context: ServerProbeJournalContext, dependencies: ServerProbeJournalDependencies, execution?: { signal?: AbortSignal; deadlineAt?: number }) => ServerProbeJournal + query: Query + retiredSource?: (lookup: ServerProbeRetiredLookup) => ReturnType + seal: (value: string) => Promise + open: (value: string) => Promise + proof: (value: unknown) => Promise + now: () => number +} +const production: ServerProbeMcpDependencies = { + authority: createMcpServerProbeAuthority, historicalAuthority: createMcpServerProbeReadAuthority, history: createServerProbeJournalHistoryReader, executor: createProductionServerProbeExecutor, journal: createSqlServerProbeJournal, + query: async (sql, values) => (await pool.query(sql, values)).rows, + seal: encryptSecret, open: decryptSecret, proof: value => privateSecretFingerprint(fingerprint(value), "server-probe-mcp-binding-v1"), now: Date.now, +} +const denied = (): never => { throw new McpError("STATE_CONFLICT", "Привязка диагностики к операции не подтверждена", 409) } +/** Shared strict source decoding; deliberately performs no authority/native preflight. */ +export async function openServerProbeMcpBinding(raw: Record, crypto: Pick) { + const input = serviceInputSchema.parse(raw) + let binding: Binding + try { binding = bindingSchema.parse(JSON.parse(await crypto.open(input.bindingCiphertext))) } catch { return denied() } + if (await crypto.proof(binding) !== input.inputDigest || input.receiptId !== binding.prepared.receiptId || input.serverId !== binding.prepared.serverId) return denied() + return { input, binding } +} +const ids = (prepared: PreparedServerProbe) => [...new Set([prepared.serverId, ...(prepared.destinationServerId ? [prepared.destinationServerId] : [])])].sort((a, b) => a - b) +export function serverProbeReceiptMetadata(receipt: ServerProbeReceipt, operationId: string, stageBudget: number) { + const page = serverProbeReceiptPage(receipt, 0) + if (!Number.isSafeInteger(receipt.attemptedStages) || !Number.isSafeInteger(receipt.completedStages) || receipt.completedStages < 0 || receipt.completedStages > receipt.attemptedStages || receipt.attemptedStages > stageBudget) return denied() + return { operationId, receiptId: receipt.receiptId, tool: receipt.tool, status: receipt.state, httpStatus: receipt.httpStatus, stageBudget, attemptedStages: receipt.attemptedStages, completedStages: receipt.completedStages, sha256: page.sha256, totalPages: page.totalPages, bytes: page.bytes, automaticReplay: false, forwardingVerified: false } +} +/** No native/provider message is copied into a model/audit error. Progress is + * diagnostic attempts, never confirmed configuration writes. */ +export function serverProbeSafeMcpError(error: unknown, input?: { receiptId: string; operationId: string; stageBudget: number }): McpError { + if (error instanceof ServerProbeExecutionError) { + const safe = new McpError(error.code, "Диагностика не получила подтверждённый результат; автоматический повтор запрещён", error.statusCode) + if (input && error.attemptedStages >= 0 && Number.isSafeInteger(error.attemptedStages) && error.attemptedStages <= input.stageBudget && Number.isSafeInteger(error.completedStages) && error.completedStages >= 0 && error.completedStages <= error.attemptedStages) Object.assign(safe, { diagnosticProgress: { ...input, state: error.state, attemptedStages: error.attemptedStages, completedStages: error.completedStages, automaticReplay: false } }) + return safe + } + if (error instanceof McpError) return error + return new McpError("STATE_UNAVAILABLE", "Состояние диагностики не подтверждено; автоматический повтор запрещён", 503) +} + +export function createServerProbeMcpAdapter(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + const open = (raw: Record) => openServerProbeMcpBinding(raw, deps) + const authority = (binding: Binding, executing = false, signal?: AbortSignal) => deps.authority(binding.principal, { executing, signal }) + const preflight = async (binding: Binding, signal?: AbortSignal) => { + const result = await deps.executor(authority(binding, false, signal)).preflight(binding.prepared, signal) + if (result.bindingProof !== binding.bindingProof || result.receiptId !== binding.prepared.receiptId || fingerprint([...result.serverIds].sort((a, b) => a - b)) !== fingerprint(ids(binding.prepared))) return denied() + return result + } + // Full exact redacted step equality includes the keyed digest of the private + // ciphertext. SQL caller input never supplies owner/connection/credential. + const owned = async (input: Input, binding: Binding, operationId?: string, executing = false, readFence?: () => Promise): Promise => { + await readFence?.() + const rows = await deps.query(`SELECT o.id AS operation_id,o.state AS operation_state,p.state AS plan_state,p.approved_by,s.state AS step_state,s.step_index,s.intended_state,s.result,p.evidence + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_connections c ON c.id=o.connection_id JOIN mcp_operation_steps s ON s.operation_id=o.id + WHERE o.connection_id=$1 AND c.owner_id=$2 AND p.operation_id=$3 AND p.operation_version=1 + AND ($4::uuid IS NULL OR o.id=$4) AND s.step_index=0 AND p.approved_by IS NOT NULL + AND s.intended_state=$5::jsonb AND octet_length(p.evidence::text)<=2097152 LIMIT 2`, + [binding.principal.connectionId, binding.ownerId, SERVER_PROBE_OPERATION, operationId ?? null, JSON.stringify(redact({ kind: "service", service: SERVER_PROBE_OPERATION, input }))]) + await readFence?.() + if (rows.length !== 1) return denied() + const row = rows[0] as unknown as OwnedStep + if (!z.string().uuid().safeParse(row.operation_id).success || row.step_index !== 0 || !row.approved_by || fingerprint(row.intended_state) !== fingerprint(redact({ kind: "service", service: SERVER_PROBE_OPERATION, input })) || operationId && row.operation_id !== operationId) return denied() + if (executing ? row.operation_state !== "running" || row.plan_state !== "executing" || row.step_state !== "intent" : !["running", "applied", "unknown", "failed"].includes(row.operation_state) || !["intent", "applied", "verified", "acknowledged", "unknown", "failed"].includes(row.step_state)) return denied() + // Do not rely on public step digests alone: bind the encrypted actual plan. + let evidence: ChangeEvidence + try { evidence = JSON.parse(await deps.open(row.evidence.sealed)) as ChangeEvidence } catch { return denied() } + await readFence?.() + if (evidence.steps?.length !== 1 || fingerprint(evidence.steps[0]) !== fingerprint({ kind: "service", service: SERVER_PROBE_OPERATION, input }) || evidence.requiresHuman !== true || evidence.verificationScope !== "acknowledgement" || evidence.mutationCost !== binding.stageBudget || evidence.validUntil !== binding.prepared.expiresAt || fingerprint(evidence.target) !== fingerprint({ scope: "servers", serverIds: ids(binding.prepared) })) return denied() + return row + } + const execution = (input: Input, binding: Binding, row: OwnedStep, executing: boolean, signal?: AbortSignal) => { + const guard = async (_context: Readonly, bindingProof: string) => { + if (bindingProof !== binding.bindingProof) return denied() + await preflight(binding, signal) + await owned(input, binding, row.operation_id, executing) + await preflight(binding, signal) + } + const journal = deps.journal({ identity: { kind: "mcp", ownerId: binding.ownerId, executionId: row.operation_id, connectionId: binding.principal.connectionId, credentialId: binding.principal.credentialId, issuedAt: binding.issuedAt, expiresAt: binding.authorityExpiresAt, operationName: SERVER_PROBE_OPERATION }, sourceServerId: binding.prepared.serverId, ...(binding.prepared.destinationServerId ? { destinationServerId: binding.prepared.destinationServerId } : {}), tool: binding.prepared.tool }, { assertAuthority: guard }, { signal, deadlineAt: Date.parse(binding.prepared.expiresAt) }) + return { journal, executor: deps.executor({ ...authority(binding, executing, signal), journal }), guard } + } + const read = async (input: Input, binding: Binding, operationId?: string, signal?: AbortSignal) => { + await preflight(binding, signal) + const row = await owned(input, binding, operationId) + const { journal } = execution(input, binding, row, false, signal) + const receipt = await journal.read(input.receiptId, binding.bindingProof) + await owned(input, binding, row.operation_id) + await preflight(binding, signal) + if (!receipt || receipt.receiptId !== input.receiptId || receipt.serverId !== binding.prepared.serverId || receipt.destinationServerId !== binding.prepared.destinationServerId || receipt.tool !== binding.prepared.tool) return denied() + return { row, receipt, result: serverProbeReceiptMetadata(receipt, row.operation_id, binding.stageBudget) } + } + const target = async (raw: Record, context: ActionContext): Promise => { + const input = serverProbeActionSchema.parse(raw), access = deps.authority(context.principal, { signal: context.signal }) + const initial = await access.readAuthority() + if (!initial.scopeServerIds.includes(input.serverId)) return denied() + await access.assertProtections([input.serverId], initial) + let destinationId: number | undefined + if (input.request.tool === "bandwidth") { + const query: Query = context.transactionClient ? async (sql, values) => (await context.transactionClient!.query(sql, values)).rows : deps.query + const fence = async () => { const fresh = await access.readAuthority(); if (fingerprint(fresh) !== fingerprint(initial)) return denied() } + const peer = await resolveServerProbeRestBandwidthPeer(input.request.bwRemoteAddress!, input.request.dstServerId, initial.scopeServerIds, { + byId: async (id, allowed) => { await fence(); if (!allowed.includes(id)) return undefined; const rows = await query("SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND id=$2 LIMIT 1", [[...allowed], id]); await fence(); return rows[0] as { id: number; host: string; enabled: boolean } | undefined }, + enabledByHost: async (host, allowed) => { await fence(); const rows = await query("SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND enabled=true AND lower(btrim(host))=lower(btrim($2)) LIMIT 1001", [[...allowed], host]); await fence(); return rows as Array<{ id: number; host: string; enabled: boolean }> }, + }) + destinationId = peer.id + } + const serverIds = [...new Set([input.serverId, ...(destinationId ? [destinationId] : [])])].sort((a, b) => a - b) + const fresh = await access.readAuthority(); if (fingerprint(fresh) !== fingerprint(initial)) return denied() + await access.assertProtections(serverIds, fresh) + return { scope: "servers", serverIds } + } + const prepare = async (raw: Record, context: ActionContext): Promise => { + try { + const input = serverProbeActionSchema.parse(raw), access = deps.authority(context.principal, { signal: context.signal }), executor = deps.executor(access) + const prepared = await executor.prepare(input.serverId, input.request, context.signal), checked = await executor.preflight(prepared, context.signal) + const current = await access.readAuthority() + if (current.ownerId !== context.connection.owner_id || current.connectionId !== context.principal.connectionId || current.credentialId !== context.principal.credentialId || current.transport === "rest") return denied() + const stageBudget = prepared.tool === "mtu" ? 11 : ["ping", "traceroute"].includes(prepared.tool) ? 2 : 1 + if (Date.parse(prepared.expiresAt) > deps.now() + 300000 || Date.parse(prepared.expiresAt) <= deps.now()) return denied() + const binding: Binding = bindingSchema.parse({ version: 1, operation: SERVER_PROBE_OPERATION, principal: context.principal, ownerId: current.ownerId, issuedAt: context.connection.owner_claims_issued_at.getTime(), authorityExpiresAt: current.expiresAt, prepared, bindingProof: checked.bindingProof, stageBudget }) + const serviceInput: Input = { serverId: input.serverId, receiptId: prepared.receiptId, inputDigest: await deps.proof(binding), bindingCiphertext: await deps.seal(JSON.stringify(binding)) } + await executor.preflight(prepared, context.signal) + return { target: { scope: "servers", serverIds: ids(prepared) }, objects: ids(prepared).map(serverId => ({ kind: "server", id: String(serverId), serverId })), snapshots: [], steps: [{ kind: "service", service: SERVER_PROBE_OPERATION, input: serviceInput }], before: { proof: checked.bindingProof }, mutationCost: stageBudget, requiresHuman: true, verificationScope: "acknowledgement", validUntil: prepared.expiresAt, warnings: ["Диагностика создаёт сетевой трафик; требуется явное подтверждение. Квитанция подтверждает измерение, но не доставку пользовательского трафика.", `Лимит этапов диагностики: ${stageBudget}; автоматический повтор запрещён.`, "Проверки допуска и сохранение квитанции имеют резерв до 30 с; сетевые лимиты команд не увеличиваются.", ...(prepared.destinationServerId ? [`Фактический сервер назначения BW: ${prepared.destinationServerId}${input.request.dstServerId !== undefined && input.request.dstServerId !== prepared.destinationServerId ? ` (запрошен ${input.request.dstServerId})` : ""}.`] : [])] } + } catch (error) { throw serverProbeSafeMcpError(error) } + } + const service: McpServiceHandler = { + cancellableDiagnostic: true, + diagnosticIdentity: async (raw, context) => { + if (context.phase !== "forward" || context.stepIndex !== 0 || !z.string().uuid().safeParse(context.operationId).success) return denied() + const { input, binding } = await open(raw) + await preflight(binding) + await owned(input, binding, context.operationId, true) + await preflight(binding) + return { receiptId: input.receiptId, stageBudget: binding.stageBudget } + }, + current: async raw => { try { const { binding } = await open(raw); await preflight(binding); return { proof: binding.bindingProof } } catch (error) { throw serverProbeSafeMcpError(error) } }, + execute: async (raw, context) => { + let progress: { receiptId: string; operationId: string; stageBudget: number } | undefined + try { + if (!context || context.phase !== "forward" || context.stepIndex !== 0 || !z.string().uuid().safeParse(context.operationId).success) return denied() + if (context.signal?.aborted) throw new ServerProbeExecutionError("CANCELLED", "Диагностика отменена", 502, "cancelled") + const { input, binding } = await open(raw); progress = { receiptId: input.receiptId, operationId: context.operationId, stageBudget: binding.stageBudget } + await preflight(binding, context.signal) + const row = await owned(input, binding, context.operationId, true) + const { executor } = execution(input, binding, row, true, context.signal) + const receipt = await executor.execute(binding.prepared, context.signal) + await preflight(binding, context.signal); await owned(input, binding, context.operationId, true); await preflight(binding, context.signal) + return serverProbeReceiptMetadata(receipt, context.operationId, binding.stageBudget) + } catch (error) { throw serverProbeSafeMcpError(error, progress) } + }, + verify: async (raw, result) => { + try { const { input, binding } = await open(raw); const operationId = z.object({ operationId: z.string().uuid() }).parse(result).operationId; const actual = await read(input, binding, operationId); return actual.receipt.state === "completed" && fingerprint(actual.result) === fingerprint(result) && fingerprint(actual.row.result) === fingerprint(result) } catch { return false } + }, + reconcile: async (raw, authorize) => { + await authorize() + try { const { input, binding } = await open(raw), actual = await read(input, binding); await authorize(); return { matchesDesiredState: actual.receipt.state === "completed" && fingerprint(actual.row.result) === fingerprint(actual.result), matchesOriginalState: false } } catch { return { matchesDesiredState: false, matchesOriginalState: false } } + }, + } + const historicalAccess = async (request: z.infer, context: ActionContext) => { + const access = deps.historicalAuthority(context.principal, { signal: context.signal }), initial = await access.readAuthority() + if (initial.ownerId !== context.connection.owner_id || initial.connectionId !== context.principal.connectionId || initial.credentialId !== context.principal.credentialId) return denied() + const fresh = async () => { const current = await access.readAuthority(); if (fingerprint(initial) !== fingerprint(current)) return denied(); return current } + await fresh() + const rows = await deps.query(`/* probe-history:scope */ SELECT j.source_server_id,j.destination_server_id FROM server_probe_executions j + JOIN mcp_operations o ON o.id=j.execution_id AND j.identity_kind='mcp' JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_connections c ON c.id=o.connection_id WHERE j.receipt_id=$1 AND o.id=$2 AND o.connection_id=$3 AND c.owner_id=$4 + AND j.mcp_connection_id=c.id AND j.actor_id=c.owner_id AND p.operation_id=$5 AND p.operation_version=1 + AND j.source_server_id=ANY($6::int[]) AND (j.destination_server_id IS NULL OR j.destination_server_id=ANY($6::int[])) LIMIT 2`, + [request.receiptId, request.operationId, context.principal.connectionId, initial.ownerId, SERVER_PROBE_OPERATION, initial.scopeServerIds]) + await fresh() + const loadRetired = deps.retiredSource + const readRetired = rows.length === 0 && loadRetired ? () => loadRetired({ + receiptId: request.receiptId, operationId: request.operationId, connectionId: context.principal.connectionId, + ownerId: initial.ownerId, scopeServerIds: initial.scopeServerIds, + }) : undefined + const retired = readRetired ? await readRetired() : undefined + if (readRetired) await fresh() + if (rows.length !== 1 && !retired) return denied() + const row = rows[0] + const serverIds = retired ? retired.source.serverIds : [...new Set([z.number().int().positive().safe().parse(row.source_server_id), ...(row.destination_server_id === null ? [] : [z.number().int().positive().safe().parse(row.destination_server_id)])])].sort((a, b) => a - b) + const fence = async () => { const current = await fresh(); if (serverIds.some(id => !current.scopeServerIds.includes(id))) return denied(); await access.assertProtections(serverIds, current); await fresh() } + await fence() + const retiredFence = retired && readRetired ? async () => { + await fence() + const current = await readRetired() + await fence() + if (!current || fingerprint(current) !== fingerprint(retired)) return denied() + } : undefined + return { initial, serverIds, fence, retired, retiredFence } + } + const receiptTarget = async (raw: Record, context: ActionContext): Promise => { + const request = serverProbeReceiptReadSchema.parse(raw), { serverIds, fence, retiredFence } = await historicalAccess(request, context) + await (retiredFence ?? fence)(); return { scope: "servers", serverIds } + } + const readPage = async (raw: Record, context: ActionContext) => { + try { + const request = serverProbeReceiptReadSchema.parse(raw), { initial, serverIds, fence, retired, retiredFence } = await historicalAccess(request, context) + if (retired && retiredFence) { + const source = retired.source + const originalContext: ServerProbeJournalContext = { + identity: { kind: "mcp", ownerId: source.ownerId, executionId: source.executionId, connectionId: source.connectionId, credentialId: source.originalCredentialId, + issuedAt: source.issuedAt, expiresAt: Date.parse(source.authorityExpiresAt), operationName: SERVER_PROBE_OPERATION }, + sourceServerId: source.sourceServerId, ...(source.destinationServerId !== undefined ? { destinationServerId: source.destinationServerId } : {}), tool: source.tool, + } + await retiredFence() + const reader = deps.history({ assertReadAuthority: retiredFence }, { signal: context.signal }) + const receipt = await reader.read({ context: originalContext, receiptId: source.receiptId, bindingProof: source.bindingProof }) + const page = serverProbeReceiptPage(receipt, request.page) + await retiredFence() + return page + } + await fence() + const rows = await deps.query(`SELECT p.evidence FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id JOIN mcp_connections c ON c.id=o.connection_id WHERE o.id=$1 AND o.connection_id=$2 AND c.owner_id=$3 AND p.operation_id=$4 AND p.operation_version=1 AND octet_length(p.evidence::text)<=2097152 LIMIT 2`, [request.operationId, context.principal.connectionId, initial.ownerId, SERVER_PROBE_OPERATION]) + await fence(); if (rows.length !== 1) return denied() + const evidence = JSON.parse(await deps.open((rows[0].evidence as { sealed: string }).sealed)) as ChangeEvidence + await fence() + const step = evidence.steps?.[0]; if (evidence.steps?.length !== 1 || step?.kind !== "service" || step.service !== SERVER_PROBE_OPERATION) return denied() + const { input, binding } = await open(step.input) + await fence() + if (input.receiptId !== request.receiptId || binding.principal.connectionId !== context.principal.connectionId || binding.ownerId !== initial.ownerId || fingerprint(serverIds) !== fingerprint(ids(binding.prepared))) return denied() + const originalContext: ServerProbeJournalContext = { identity: { kind: "mcp", ownerId: binding.ownerId, executionId: request.operationId, connectionId: binding.principal.connectionId, credentialId: binding.principal.credentialId, issuedAt: binding.issuedAt, expiresAt: binding.authorityExpiresAt, operationName: SERVER_PROBE_OPERATION }, sourceServerId: binding.prepared.serverId, ...(binding.prepared.destinationServerId !== undefined ? { destinationServerId: binding.prepared.destinationServerId } : {}), tool: binding.prepared.tool } + const reader = deps.history({ assertReadAuthority: async () => { await fence(); await owned(input, binding, request.operationId, false, fence); await fence() } }, { signal: context.signal }) + const receipt = await reader.read({ context: originalContext, receiptId: input.receiptId, bindingProof: binding.bindingProof }) + await fence() + const page = serverProbeReceiptPage(receipt, request.page) + await fence(); await owned(input, binding, request.operationId, false, fence); await fence() + return page + } catch (error) { throw serverProbeSafeMcpError(error) } + } + return { schema: serverProbeActionSchema, target, prepare, service, receiptSchema: serverProbeReceiptReadSchema, receiptTarget, readPage } +} diff --git a/backend/src/modules/mcp/server-probe-all-tools.sql-helper.ts b/backend/src/modules/mcp/server-probe-all-tools.sql-helper.ts new file mode 100644 index 00000000..49055095 --- /dev/null +++ b/backend/src/modules/mcp/server-probe-all-tools.sql-helper.ts @@ -0,0 +1,307 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { createSocket } from "node:dgram" +import { createServer as httpServer } from "node:http" +import { createServer as tcpServer, type Socket } from "node:net" +import { createRequire, syncBuiltinESMExports } from "node:module" +import { basename, dirname, resolve } from "node:path" +import { realpath } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import type { ServerProbeRequest } from "../../services/server-probe-execution-model.js" + +const gate = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +async function bounded(value: Promise, label: string) { + let timer: ReturnType | undefined + try { return await Promise.race([value, new Promise((_yes, no) => { timer = setTimeout(() => no(Error(label)), 15000) })]) } + finally { clearTimeout(timer) } +} + +/** All six public tools through genuine production registry/runner/storage. + * Only owned HTTP/DNS endpoints and one real-SQL pending-run barrier are used; + * no service, authority, source, resolver method or query result is replaced. */ +export async function acceptServerProbeAllToolsSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const routeOnly = process.env.MMAPP_OWN_ALL_TOOLS_ROUTE_ONLY === "true" + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres"); assert.equal(decodeURIComponent(url.username), "mcp_test") + assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres"); assert.equal(url.search, ""); assert.equal(url.hash, "") + assert(["postgres:", "postgresql:"].includes(url.protocol)); assert(process.env.MCP_STATE_DIR) + const state = resolve(process.env.MCP_STATE_DIR) + assert.equal(basename(state), "state"); assert.match(basename(dirname(state)), /^mm-mcp-test-[A-Za-z0-9-]+$/) + assert.equal(resolve(await realpath(state)).toLowerCase(), state.toLowerCase()) + const { pool } = await import("../../db/index.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const { createMcpConnection, authenticateMcpToken, rotateConnectionToken } = await import("./store.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { findAction } = await import("./actions.js") + const { SERVER_PROBE_CANCEL_OPERATION } = await import("./server-probe-registration.js") + const { SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION } = await import("../../services/server-probe-authority.js") + assert.equal(findAction("mm_server_probe_plan").operation.id, SERVER_PROBE_OPERATION) + assert.equal((await pool.query("SELECT to_regclass('server_probe_storage_admissions') IS NOT NULL AS ready")).rows[0].ready, true) + + const dnsPromises = createRequire(import.meta.url)("node:dns/promises") as typeof import("node:dns/promises") + const originalResolver = dnsPromises.Resolver, originalQuery = pool.query + const connections: string[] = [], servers: number[] = [], cases: string[] = [], sockets = new Set() + const username = "OWNED_ALLTOOLS_PRIVATE_USER", password = "OWNED_ALLTOOLS_PRIVATE_PASSWORD", privateValues = [username, password] + let requests = 0, dnsUdp = 0, dnsTcp = 0, resolvers = 0, protocolErrors = 0 + let routerListening = false, dnsListening = false, tcpListening = false + const previousEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled + const hidden = (value: unknown) => { const content = JSON.stringify(value); assert(!privateValues.some(secret => content.includes(secret)), "Private endpoint material escaped") } + const router = httpServer(async (request, response) => { + response.setHeader("content-type", "application/json") + try { + requests++; assert.equal(request.socket.remoteAddress, "127.0.0.1") + assert.equal(request.headers.authorization, "Basic " + Buffer.from(`${username}:${password}`).toString("base64")) + if (request.method === "GET") { + assert.equal(request.url, "/rest/ip/route") + response.end(JSON.stringify([{ active: "true", "dst-address": "0.0.0.0/0", gateway: "127.0.0.1", distance: "1" }])) + return + } + assert.equal(request.method, "POST") + let raw = ""; for await (const chunk of request) raw += chunk + const body = JSON.parse(raw) as Record + assert.equal(body.address, "127.0.0.1") + if (request.url === "/rest/tool/bandwidth-test") { + assert.equal(body.user, username); assert.equal(body.password, password) + response.end(JSON.stringify([{ "tx-current": "1000", "rx-current": "2000" }])) + } else if (request.url === "/rest/tool/traceroute") response.end(JSON.stringify([{ address: "127.0.0.1", loss: "0%", last: "1ms", avg: "1ms" }])) + else { + assert.equal(request.url, "/rest/tool/ping") + if (body["do-not-fragment"] === "true" && body.size === "1500") response.end(JSON.stringify([{ seq: "0", status: "timeout" }])) + else response.end(JSON.stringify([{ seq: "0", ttl: "64", time: "1ms", received: "1" }])) + } + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned all-tools protocol mismatch"}') } + }) + const udp = createSocket("udp4") + const question = (packet: Buffer) => { + assert(packet.length >= 17); assert.equal(packet.readUInt16BE(4), 1) + let offset = 12; const labels: string[] = [] + while (packet[offset]) { const length = packet[offset++]; assert(length <= 63 && offset + length < packet.length); labels.push(packet.subarray(offset, offset + length).toString("ascii")); offset += length } + offset++; assert.equal(labels.join("."), "owned-alltools.invalid"); assert.equal(packet.readUInt16BE(offset), 16); assert.equal(packet.readUInt16BE(offset + 2), 1) + return packet.subarray(12, offset + 4) + } + const answer = (packet: Buffer, truncate: boolean) => { + const selected = question(packet), header = Buffer.alloc(12) + header.writeUInt16BE(packet.readUInt16BE(0), 0); header.writeUInt16BE(truncate ? 0x8380 : 0x8180, 2); header.writeUInt16BE(1, 4) + if (truncate) return Buffer.concat([header, selected]) + header.writeUInt16BE(2, 6) + const records = ["FIRST_PUBLIC_DNS_RECORD_", "SECOND_PUBLIC_DNS_RECORD_"].map(prefix => { + const chunks = Array.from({ length: 16 }, (_, index) => Buffer.from((prefix + index + "_").padEnd(200, "A"), "ascii")) + const data = Buffer.concat(chunks.flatMap(chunk => [Buffer.from([chunk.length]), chunk])), rr = Buffer.alloc(12) + rr.writeUInt16BE(0xc00c, 0); rr.writeUInt16BE(16, 2); rr.writeUInt16BE(1, 4); rr.writeUInt32BE(60, 6); rr.writeUInt16BE(data.length, 10) + return Buffer.concat([rr, data]) + }) + return Buffer.concat([header, selected, ...records]) + } + udp.on("message", (packet, remote) => { + try { dnsUdp++; assert.equal(remote.address, "127.0.0.1"); udp.send(answer(packet, true), remote.port, remote.address) } + catch { protocolErrors++ } + }) + const tcp = tcpServer(socket => { + sockets.add(socket); socket.once("close", () => sockets.delete(socket)); socket.on("error", () => {}) + let received = Buffer.alloc(0) + socket.on("data", chunk => { + try { + received = Buffer.concat([received, chunk]); if (received.length < 2 || received.length < received.readUInt16BE(0) + 2) return + assert.equal(socket.remoteAddress, "127.0.0.1"); dnsTcp++ + const response = answer(received.subarray(2, received.readUInt16BE(0) + 2), false), size = Buffer.alloc(2); size.writeUInt16BE(response.length) + socket.end(Buffer.concat([size, response])); received = Buffer.alloc(0) + } catch { protocolErrors++; socket.destroy() } + }) + }) + let releasePending: (() => void) | undefined + try { + await new Promise((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) }); routerListening = true + await new Promise((yes, no) => { udp.once("error", no); udp.bind(0, "127.0.0.1", yes) }); dnsListening = true + const dnsPort = udp.address().port + await new Promise((yes, no) => { tcp.once("error", no); tcp.listen(dnsPort, "127.0.0.1", yes) }); tcpListening = true + // Constructor endpoint wrapper only: every resolve operation, wire parser, + // production beforeDispatch fence and cancellation method remains genuine. + dnsPromises.Resolver = class extends originalResolver { + constructor(...args: ConstructorParameters) { + super(...args); this.setServers([`127.0.0.1:${dnsPort}`]); resolvers++ + assert.deepEqual(this.getServers(), [`127.0.0.1:${dnsPort}`]) + } + } + syncBuiltinESMExports() + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + servers.push(...(await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES('owned-alltools-source','127.0.0.1',$1,false,false,true,$2,$3),('owned-alltools-destination','127.0.0.1',$1,false,false,true,$2,$3) RETURNING id", [(router.address() as { port: number }).port, username, password])).rows.map(row => Number(row.id))) + const fixture = async () => { + const actor = { id: "owned-alltools-" + randomUUID(), email: "fixture@example.test", name: "Owned all tools", apps: ["mm"], permissions: ["mm:servers:write", "mm:servers:read", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 3600000 } + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned all-tools production registry", mode: "approval", expiresAt: new Date(actor.expiresAt).toISOString(), permissions: ["mm:servers:write", "mm:servers:read"], + scope: { serverIds: servers, operationIds: [SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION, SERVER_PROBE_CANCEL_OPERATION], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connections.push(access.connection.id); privateValues.push(access.token) + return { actor, principal: await authenticateMcpToken(access.token) } + } + const terminal = async (operationId: string) => { + for (const until = Date.now() + 90000; Date.now() < until;) { + const row = (await pool.query("SELECT id,state,result,error_code FROM mcp_operations WHERE id=$1", [operationId])).rows[0] + assert(row); if (!["pending", "running"].includes(row.state)) return row + await delay(20) + } + throw Error("Owned all-tools operation did not reach terminal state") + } + const prepare = async (f: Awaited>, request: ServerProbeRequest) => { + const before = requests + dnsUdp + dnsTcp + const result = await invokeMcpTool(f.principal, "mm_server_probe_plan", { serverId: servers[0], request }) as { planId: string; state: string } + hidden(result); assert.equal(result.state, "awaiting-approval"); assert.equal(requests + dnsUdp + dnsTcp, before) + await assert.rejects(applyPlan(f.principal, result.planId, randomUUID())); assert.equal(requests + dnsUdp + dnsTcp, before) + await approvePlan(result.planId, f.actor, true) + return result.planId + } + const selectedTools: readonly ServerProbeRequest["tool"][] = routeOnly ? ["route"] : ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"] + for (const tool of selectedTools) { + const f = await fixture() + const request: ServerProbeRequest = tool === "bandwidth" ? { tool, bwRemoteAddress: "127.0.0.1", dstServerId: servers[1] } + : tool === "dns" ? { tool, target: "owned-alltools.invalid", dnsType: "TXT" } : { tool, target: "127.0.0.1", ...(tool === "ping" ? { pingCount: 1 } : {}) } + const startedAt = Date.now(), requestStart = requests, udpStart = dnsUdp, tcpStart = dnsTcp + const planId = await prepare(f, request), key = randomUUID(), operation = await applyPlan(f.principal, planId, key), done = await terminal(operation.id) + const progress = done.result?.diagnosticProgress + const stageTimes = (await pool.query(`SELECT s.stage_index,s.kind,s.state,s.created_at,s.received_at + FROM server_probe_execution_stages s JOIN server_probe_executions e USING(receipt_id) + WHERE e.execution_id=$1 ORDER BY s.stage_index`, [operation.id])).rows + // Emit bounded, selected facts BEFORE success assertions. No IDs, source + // fields, raw provider data, private bindings, credentials or error text. + console.log("Owned all-tools stage trace:", JSON.stringify({ tool, elapsedMs: Date.now() - startedAt, + state: done.state, errorCode: done.error_code ?? null, httpRequests: requests - requestStart, + dnsUdpQueries: dnsUdp - udpStart, dnsTcpQueries: dnsTcp - tcpStart, + ...(progress ? { progress: { state: progress.state, attemptedStages: progress.attemptedStages, completedStages: progress.completedStages } } : {}), + stages: stageTimes.map(row => ({ index: row.stage_index, kind: row.kind, state: row.state, createdAt: row.created_at, receivedAt: row.received_at })) })) + assert.equal(done.state, "applied", tool + ": " + String(done.error_code)); hidden(done.result) + assert.equal(done.result.confirmedWrites, 0); assert.equal(done.result.completedServiceSteps, 1); assert.equal(done.result.verificationScope, "acknowledgement") + const receipt = done.result.diagnosticReceipt as { receiptId: string; stageBudget: number; attemptedStages: number; completedStages: number; totalPages: number; sha256: string } + assert(receipt); assert.equal(receipt.attemptedStages, receipt.completedStages); assert(receipt.attemptedStages > 0 && receipt.attemptedStages <= receipt.stageBudget) + let captured = false + for (const until = Date.now()+2000; Date.now()>'terminalAt'=to_char(GREATEST(o.finished_at,p.expires_at) AT TIME ZONE 'UTC', + 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS terminal_matches FROM server_probe_terminal_proofs f + JOIN mcp_operations o ON o.id=f.operation_id JOIN mcp_plans p ON p.id=o.plan_id + WHERE f.operation_id=$1 AND f.receipt_id=$2`, [done.id,receipt.receiptId])).rows[0] + if (proof) { assert.equal(proof.terminal_matches,true); captured = true; break } + await delay(20) + } + assert.equal(captured,true,"Owned successful diagnostic did not publish its terminal proof") + const step = (await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1", [done.id])).rows + assert.equal(step.length, 1); assert.equal(step[0].state, "acknowledged") + for (const field of ["output", "data", "contentB64", "bindingCiphertext"]) assert(!Object.hasOwn(step[0].result, field)) + const ledger = (await pool.query("SELECT stage_index,kind,state FROM server_probe_execution_stages WHERE receipt_id=$1 ORDER BY stage_index", [receipt.receiptId])).rows + assert.equal(ledger.length, receipt.attemptedStages); assert(ledger.every((row, index) => row.stage_index === index + 1 && row.state === "received")) + if (tool === "mtu") assert.deepEqual(ledger.map(row => row.kind), ["native-mtu-1500", "native-mtu-1492"]) + const beforeRead = requests + dnsUdp + dnsTcp, chunks: Buffer[] = [] + for (let page = 0; page < receipt.totalPages; page++) { + const part = await invokeMcpTool(f.principal, "mm_server_probe_receipt_get", { operationId: done.id, receiptId: receipt.receiptId, page }) as { sha256: string; contentB64: string } + assert.equal(part.sha256, receipt.sha256); chunks.push(Buffer.from(part.contentB64, "base64")); hidden(part) + } + const complete = Buffer.concat(chunks); assert.equal(createHash("sha256").update(complete).digest("hex"), receipt.sha256) + const measurement = JSON.parse(complete.toString("utf8")); hidden(measurement); assert.equal(measurement.tool, tool); assert.equal(measurement.forwardingVerified, false) + if (tool === "dns") { + assert(receipt.totalPages > 1); assert.equal(dnsUdp, 1); assert.equal(dnsTcp, 1); assert.equal(resolvers, 1) + const rotated = await rotateConnectionToken(f.principal.connectionId, f.actor); privateValues.push(rotated.token) + await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb,policy=jsonb_set(jsonb_set(jsonb_set(policy,'{permissions}',$2::jsonb),'{scope,operationIds}',$3::jsonb),'{mode}','\"observer\"'::jsonb) WHERE id=$1", [f.principal.connectionId, JSON.stringify(["mm:servers:read"]), JSON.stringify([SERVER_PROBE_RECEIPT_READ_OPERATION])]) + const reader = await authenticateMcpToken(rotated.token) + for (let page = 0; page < receipt.totalPages; page++) await invokeMcpTool(reader, "mm_server_probe_receipt_get", { operationId: done.id, receiptId: receipt.receiptId, page }) + await assert.rejects(invokeMcpTool(f.principal, "mm_server_probe_receipt_get", { operationId: done.id, receiptId: receipt.receiptId, page: 0 })) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$1", [reader.connectionId]) + await assert.rejects(invokeMcpTool(reader, "mm_server_probe_receipt_get", { operationId: done.id, receiptId: receipt.receiptId, page: 1 })) + } + if (tool !== "dns") assert.equal((await applyPlan(f.principal, planId, key)).id, done.id) + assert.equal(requests + dnsUdp + dnsTcp, beforeRead) + const audit = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1 AND action='operation.complete'", [f.principal.connectionId])).rows + assert.equal(audit.length, 1); assert.equal(audit[0].payload.confirmedWrites, 0) + cases.push(tool + ": actual public plan/approval/apply, receipt metadata, full stage ledger and checksum pages without redispatch") + } + + // Force a genuine capture SQL failure after the real diagnostic has + // committed. A sequence records the rejected attempt despite rollback. + const captureFault = await fixture() + const faultPlan = await prepare(captureFault, { tool: "route", target: "127.0.0.1" }) + await pool.query("CREATE SEQUENCE public.owned_probe_capture_failure") + try { + await pool.query(`CREATE FUNCTION public.owned_probe_capture_failure() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN + IF NEW.source->>'connectionId'=TG_ARGV[0] THEN + PERFORM nextval('public.owned_probe_capture_failure'); + RAISE EXCEPTION 'owned terminal proof insertion refused'; + END IF; + RETURN NEW; + END $$`) + try { + assert.match(captureFault.principal.connectionId,/^[a-f0-9-]{36}$/) + await pool.query(`CREATE TRIGGER owned_probe_capture_failure BEFORE INSERT ON public.server_probe_terminal_proofs + FOR EACH ROW EXECUTE FUNCTION public.owned_probe_capture_failure('${captureFault.principal.connectionId}')`) + try { + const beforeFault = requests+dnsUdp+dnsTcp, faultKey=randomUUID() + const applied = await applyPlan(captureFault.principal,faultPlan,faultKey), done=await terminal(applied.id) + let attempted = false + for (const until=Date.now()+2500;Date.now()running SQL BEFORE sending it. This + // leaves a real pending approved source, allowing genuine durable cancel. + // A server-table lock after 'running' would have no intent step yet and the + // source loader correctly refuses that incomplete running source. + const f = await fixture(), planId = await prepare(f, { tool: "ping", target: "127.0.0.1", pingCount: 1 }) + const entered = gate(), resume = gate(); releasePending = resume.resolve + let heldOperation = "", held = false + pool.query = (async function (...args: unknown[]) { + const query = args[0] as { text?: string; values?: unknown[] } | undefined + if (!held && query?.text?.startsWith("/* mcp-operation-root:claim */") && typeof query.values?.[0] === "string") { + held = true; heldOperation = query.values[0]; entered.resolve(); await resume.promise + } + return Reflect.apply(originalQuery, pool, args) + }) as typeof pool.query + const beforeCancel = requests + dnsUdp + dnsTcp, key = randomUUID(), operation = await applyPlan(f.principal, planId, key) + await bounded(entered.promise, "Runner did not reach real SQL pending barrier"); assert.equal(heldOperation, operation.id) + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0].state, "pending") + const stopped = await invokeMcpTool(f.principal, "mm_server_probe_cancel", { operationId: operation.id }) as { cancelRequested: boolean; terminal: boolean } + assert.equal(stopped.cancelRequested, true); assert.equal(stopped.terminal, false) + pool.query = originalQuery; resume.resolve(); releasePending = undefined + const done = await terminal(operation.id); assert.equal(done.state, "rejected"); assert.equal(done.result.confirmedWrites, 0) + assert.deepEqual(done.result.diagnosticProgress && { attempted: done.result.diagnosticProgress.attemptedStages, completed: done.result.diagnosticProgress.completedStages, state: done.result.diagnosticProgress.state }, { attempted: 0, completed: 0, state: "cancelled" }) + assert.equal(done.result.needsReview, false); assert.equal(requests + dnsUdp + dnsTcp, beforeCancel) + const untouched = (await pool.query(`SELECT + (SELECT count(*)::int FROM server_probe_executions WHERE execution_id=$1) AS anchors, + (SELECT count(*)::int FROM server_probe_execution_stages s JOIN server_probe_executions e USING(receipt_id) WHERE e.execution_id=$1) AS stages, + (SELECT count(*)::int FROM server_probe_execution_payloads p JOIN server_probe_executions e USING(receipt_id) WHERE e.execution_id=$1) AS payloads, + (SELECT count(*)::int FROM mcp_diagnostic_cancellations WHERE operation_id=$1) AS stops, + (SELECT count(*)::int FROM mcp_audit WHERE action='diagnostic.cancel-requested' AND payload->>'operationId'=$1::text) AS audits`, [operation.id])).rows[0] + assert.deepEqual(untouched, { anchors: 0, stages: 0, payloads: 0, stops: 1, audits: 1 }) + assert.equal((await applyPlan(f.principal, planId, key)).id, operation.id); assert.equal(requests + dnsUdp + dnsTcp, beforeCancel) + cases.push("pre-first-send: actual pending SQL barrier, public durable cancellation, rejected zero-stage/no-anchor/no-payload and idempotent replay without provider I/O") + assert.equal(protocolErrors, 0) + return { cases, actualProductionRegistry: true, allSixTools: !routeOnly, isolatedRouteOnly: routeOnly, productionAuthorityAndStorage: true, + loopbackHttpRequests: requests, localDnsUdpQueries: dnsUdp, localDnsTcpQueries: dnsTcp, + actualDnsWireParsingAndTcpFallback: !routeOnly, dnsResolverConstructorEndpointWrapperOnly: true, + dnsResolutionOrServiceOverrides: false, largeDnsPagesWithChecksum: !routeOnly, currentReadonlyCredentialPages: !routeOnly, + genuinePendingSqlBarrierBeforeFirstDispatch: true, preCancelledZeroStageNoAnchor: true, + confirmedConfigurationWrites: 0, productionOrMachineDnsChanges: false, externalNetwork: false } + } finally { + pool.query = originalQuery; releasePending?.() + dnsPromises.Resolver = originalResolver; syncBuiltinESMExports(); assert.equal(dnsPromises.Resolver, originalResolver) + if (connections.length) await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"revoked\"'::jsonb) WHERE id=ANY($1::uuid[])", [connections]) + if (servers.length) await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [servers]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousEnabled]) + router.closeAllConnections(); for (const socket of sockets) socket.destroy() + if (routerListening) await new Promise(yes => router.close(() => yes())) + if (tcpListening) await new Promise(yes => tcp.close(() => yes())) + if (dnsListening) await new Promise(yes => udp.close(() => yes())) + } +} diff --git a/backend/src/modules/mcp/server-probe-registration.test.ts b/backend/src/modules/mcp/server-probe-registration.test.ts new file mode 100644 index 00000000..67c205b0 --- /dev/null +++ b/backend/src/modules/mcp/server-probe-registration.test.ts @@ -0,0 +1,55 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import type { ActionContext } from "./action-types.js" + +const priorQuery = pool.query, priorConnect = pool.connect, priorNative = MikrotikClient.fromServer, priorFetch = globalThis.fetch +let sql = 0, native = 0, network = 0 +pool.query = (() => { sql++; throw Error("Registration must not query SQL") }) as never +pool.connect = (() => { sql++; throw Error("Registration must not acquire a backend") }) as never +MikrotikClient.fromServer = (() => { native++; throw Error("Registration must not open a router") }) as never +globalThis.fetch = (async () => { network++; throw Error("Registration must not perform network I/O") }) as typeof fetch +try { + const { findAction, findActionByOperation, listActions } = await import("./actions.js") + const { findMcpService } = await import("./service-registry.js") + const { listMcpOperations } = await import("./registry.js") + const { SERVER_PROBE_MCP_OPERATION_VERSION } = await import("./server-probe-actions.js") + const { SERVER_PROBE_CANCEL_OPERATION } = await import("./server-probe-registration.js") + const run = findAction("mm_server_probe_plan"), read = findAction("mm_server_probe_receipt_get"), cancel = findAction("mm_server_probe_cancel") + assert.equal(run.operation.id, "servers.probes.run"); assert.equal(run.operation.version, SERVER_PROBE_MCP_OPERATION_VERSION) + assert.equal(run.operation.effect, "diagnostic"); assert.equal(run.operation.scope, "servers"); assert.equal(run.operation.implementation, "available") + assert.deepEqual(run.operation.requiredPermissions, ["mm:servers:write"]); assert.equal(run.operation.annotations.readOnlyHint, false) + assert(run.prepare); assert(!run.read); assert.equal(findActionByOperation(run.operation.id), run) + assert.equal(read.operation.id, "servers.probes.receipt.read"); assert.equal(read.operation.effect, "read"); assert.equal(read.operation.scope, "servers") + assert.deepEqual(read.operation.requiredPermissions, ["mm:servers:read"]); assert(read.read); assert(!read.prepare) + assert.equal(cancel.operation.id, SERVER_PROBE_CANCEL_OPERATION); assert.equal(cancel.operation.effect, "write") + assert.equal(cancel.operation.scope, "connection"); assert.deepEqual(cancel.operation.requiredPermissions, ["mm:servers:write"]) + assert.equal(cancel.operation.annotations.readOnlyHint, false); assert(cancel.read); assert(!cancel.prepare, "Stop requests must execute immediately without another approval plan") + const service = findMcpService(run.operation.id) + assert(service); assert.equal(service.cancellableDiagnostic, true) + for (const method of ["current", "execute", "verify", "reconcile", "diagnosticIdentity"] as const) assert.equal(typeof service[method], "function") + assert.equal(findMcpService(read.operation.id), undefined); assert.equal(findMcpService(cancel.operation.id), undefined) + const ids = [run.operation.id, read.operation.id, cancel.operation.id] + assert.equal(listActions().filter(action => ids.includes(action.operation.id)).length, 3) + assert.equal(listMcpOperations().filter(operation => ids.includes(operation.id)).length, 3) + for (const action of [run, read, cancel]) assert.equal(listMcpOperations().find(value => value.id === action.operation.id)?.implementation, "available") + + assert(run.schema.safeParse({ serverId: 1, request: { tool: "ping", target: "127.0.0.1", pingCount: 1 } }).success) + assert(!run.schema.safeParse({ serverId: 1, request: { tool: "ping", target: "127.0.0.1" }, journal: {}, sql: "SELECT 1" }).success) + assert(!run.schema.safeParse({ serverId: 1, request: { tool: "command", command: "/system/reboot" } }).success) + assert(read.schema.safeParse({ operationId: randomUUID(), receiptId: randomUUID(), page: 0 }).success) + assert(!read.schema.safeParse({ operationId: randomUUID(), receiptId: randomUUID(), page: 16 }).success) + assert(!read.schema.safeParse({ operationId: randomUUID(), receiptId: randomUUID(), page: 0, credentialId: randomUUID() }).success) + assert(cancel.schema.safeParse({ operationId: randomUUID() }).success) + assert(!cancel.schema.safeParse({ operationId: randomUUID(), ownerId: "forged", source: {}, principal: {} }).success) + assert(!cancel.schema.safeParse({ operationId: "not-an-operation" }).success) + const context = { principal: { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 60000).toISOString() }, connection: {} } as ActionContext + await assert.rejects(run.prepare({}, context)); await assert.rejects(read.read({}, context)); await assert.rejects(cancel.read({}, context)) + await assert.rejects(service.current({})); await assert.rejects(service.execute({}, { operationId: randomUUID(), phase: "recovery" })) + assert.equal(await service.verify!({}, {}), false) + const controller = new AbortController(); controller.abort() + await assert.rejects(async () => run.target({ serverId: 1, request: { tool: "ping", target: "127.0.0.1" } }, { ...context, signal: controller.signal }), { code: "READ_CANCELLED" }) + assert.deepEqual({ sql, native, network }, { sql: 0, native: 0, network: 0 }) + console.log("Public server-probe registration: three real descriptors and fixed storage service; immediate durable stop has write metadata; strict source/native escape rejection; inert construction and invalid/pre-aborted input perform no I/O") +} finally { pool.query = priorQuery; pool.connect = priorConnect; MikrotikClient.fromServer = priorNative; globalThis.fetch = priorFetch } diff --git a/backend/src/modules/mcp/server-probe-registration.ts b/backend/src/modules/mcp/server-probe-registration.ts new file mode 100644 index 00000000..4125c5bb --- /dev/null +++ b/backend/src/modules/mcp/server-probe-registration.ts @@ -0,0 +1,26 @@ +import { z } from "zod" +import type { add } from "./actions.js" +import { SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION } from "../../services/server-probe-authority.js" +import { SERVER_PROBE_MCP_OPERATION_VERSION } from "./server-probe-actions.js" +import { createStorageServerProbeMcpAdapter } from "./server-probe-storage-host.js" +import { registerMcpService } from "./service-registry.js" + +export const SERVER_PROBE_CANCEL_OPERATION = "servers.probes.cancel" +const cancelSchema = z.object({ operationId: z.string().uuid() }).strict() + +/** Fixed production composition. Construction is inert; normal startup enrolls + * schema 0037 before serving tools. No request may select storage or callbacks. */ +export function registerServerProbeActions(register: typeof add) { + const adapter = createStorageServerProbeMcpAdapter() + registerMcpService(SERVER_PROBE_OPERATION, adapter.service) + register(SERVER_PROBE_OPERATION, "mm_server_probe_plan", "Серверная диагностика с подтверждением человеком и без автоматического повтора", "servers", "mm:servers:write", "diagnostic", "servers", adapter.schema, + { version: SERVER_PROBE_MCP_OPERATION_VERSION, target: adapter.target, prepare: adapter.prepare }) + register(SERVER_PROBE_RECEIPT_READ_OPERATION, "mm_server_probe_receipt_get", "Постраничная квитанция диагностики с актуальным правом чтения", "servers", "mm:servers:read", "read", "servers", adapter.receiptSchema, + { version: 1, target: adapter.receiptTarget, read: adapter.readPage }) + // Immediate stop for an already human-approved owned run. The action uses the + // direct executor hook, but its descriptor remains a write: durable stop and + // audit records are created. The storage host rechecks owner/run/write/scope + // grants and exact approved source; cancellation cannot execute another run. + register(SERVER_PROBE_CANCEL_OPERATION, "mm_server_probe_cancel", "Остановить свою подтверждённую диагностику: записать запрос отмены и аудит", "servers", "mm:servers:write", "write", "connection", cancelSchema, + { version: 1, read: (input, context) => adapter.cancel(context.principal, String(input.operationId), context.signal) }) +} diff --git a/backend/src/modules/mcp/server-probe-storage-host.sql-helper.ts b/backend/src/modules/mcp/server-probe-storage-host.sql-helper.ts new file mode 100644 index 00000000..baf3fd1a --- /dev/null +++ b/backend/src/modules/mcp/server-probe-storage-host.sql-helper.ts @@ -0,0 +1,238 @@ +/** Owned disposable-PG/loopback acceptance of the public production registry and + * storage host. No executor/authority overrides or real router. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, resolve } from "node:path" +import { realpath } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" +import type { PoolClient } from "pg" +import type { AuthUser } from "../../lib/permissions.js" + +export async function acceptServerProbeStorageHostSql() { + // Guard BEFORE loading configured pools, crypto, runtime, or the host. + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") + assert(["postgres:", "postgresql:"].includes(url.protocol)); assert.equal(url.hostname, "127.0.0.1") + assert.equal(decodeURIComponent(url.username), "mcp_test"); assert.equal(url.pathname, "/postgres") + assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres"); assert.equal(url.search, ""); assert.equal(url.hash, "") + assert(process.env.MCP_STATE_DIR) + const state = resolve(process.env.MCP_STATE_DIR) + assert.equal(basename(state), "state"); assert.match(basename(dirname(state)), /^mm-mcp-test-[A-Za-z0-9-]+$/) + assert.equal(resolve(await realpath(state)).toLowerCase(), state.toLowerCase()) + const { pool } = await import("../../db/index.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { createStorageServerProbeMcpAdapter } = await import("./server-probe-storage-host.js") + const { findAction, findActionByOperation } = await import("./actions.js") + const { findMcpService } = await import("./service-registry.js") + const { SERVER_PROBE_CANCEL_OPERATION } = await import("./server-probe-registration.js") + const { SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION } = await import("../../services/server-probe-authority.js") + const { createMcpConnection, authenticateMcpToken, getConnection, rotateConnectionToken } = await import("./store.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const { decryptSecret } = await import("./security.js") + const { McpError } = await import("./security.js") + const { connectProbeStorage } = await import("../../services/server-probe-storage-connection.js") + assert.equal((await pool.query("SELECT to_regclass('server_probe_storage_admissions') IS NOT NULL AS ready")).rows[0].ready, true) + assert(findMcpService(SERVER_PROBE_OPERATION)) + assert.equal(findActionByOperation(SERVER_PROBE_OPERATION).operation.toolName, "mm_server_probe_plan") + assert.equal(findAction("mm_server_probe_receipt_get").operation.id, SERVER_PROBE_RECEIPT_READ_OPERATION) + assert.equal(findAction("mm_server_probe_cancel").operation.id, SERVER_PROBE_CANCEL_OPERATION) + const host = createStorageServerProbeMcpAdapter() + const actor: AuthUser = { id: "owned-probe-host-" + randomUUID(), email: "fixture@example.test", name: "Owned host", apps: ["mm"], + permissions: ["mm:servers:write", "mm:servers:read", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 3600000 } + const username = "NEVER_STORAGE_SOURCE_USER", password = "NEVER_STORAGE_SOURCE_PASSWORD" + const privateValues = [username, password] + const hidden = (value: unknown) => { const text = JSON.stringify(value); assert.equal(privateValues.some(secret => text.includes(secret)), false, "Private source or credential data leaked") } + let requests = 0, protocolErrors = 0, serverId: number | undefined + let secondValidated!: () => void, secondClosed!: () => void, releaseSecondResponse!: () => void + const secondRequest = new Promise(yes => { secondValidated = yes }) + const secondSocketClosed = new Promise(yes => { secondClosed = yes }) + const secondResponseGate = new Promise(yes => { releaseSecondResponse = yes }) + const boundedBarrier = async (pending: Promise, message: string) => { + let timer: ReturnType | undefined + try { await Promise.race([pending, new Promise((_yes, no) => { timer = setTimeout(() => no(Error(message)), 15000) })]) } + finally { if (timer) clearTimeout(timer) } + } + const connectionIds: string[] = [] + const router = createServer(async (request, response) => { + response.setHeader("content-type", "application/json") + try { + requests++ + assert.equal(request.socket.remoteAddress, "127.0.0.1") + assert.equal(request.method, "POST"); assert.equal(request.url, "/rest/tool/ping") + assert.equal(request.headers.authorization === "Basic " + Buffer.from(`${username}:${password}`).toString("base64"), true) + let raw = ""; for await (const chunk of request) raw += chunk + const body = JSON.parse(raw) as Record + assert.equal(body.address, "127.0.0.1"); assert.equal(body.count, "1") + if (requests === 2) { + response.once("close", secondClosed) + secondValidated() + await secondResponseGate + if (response.destroyed) return + } + response.end(JSON.stringify([{ seq: "0", ttl: "64", time: "1ms", status: `${username}:${password}` }, { sent: "1", received: "1", "packet-loss": "0%" }])) + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned loopback protocol mismatch"}') } + }) + await new Promise((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) }) + const priorSettings = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0] + assert(priorSettings) + const wait = async (operationId: string) => { + for (const deadline = Date.now() + 90000; Date.now() < deadline;) { + const row = (await pool.query("SELECT id,state,verification,error_code,result FROM mcp_operations WHERE id=$1", [operationId])).rows[0] + assert(row) + if (!["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw Error("Owned host operation remains active; no retry permitted") + } + try { + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES('owned-storage-host','127.0.0.1',$1,false,false,true,$2,$3) RETURNING id", [(router.address() as { port: number }).port, username, password])).rows[0].id) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned storage host", mode: "approval", expiresAt: new Date(actor.expiresAt!).toISOString(), + permissions: ["mm:servers:write", "mm:servers:read"], scope: { serverIds: [serverId], operationIds: [SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION, SERVER_PROBE_CANCEL_OPERATION], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connectionIds.push(access.connection.id); privateValues.push(access.token) + const principal = await authenticateMcpToken(access.token), raw = { serverId, request: { tool: "ping", target: "127.0.0.1", pingCount: 1 } } + const cancel = (operationId: string) => invokeMcpTool(principal, "mm_server_probe_cancel", { operationId }) as ReturnType + + // Actual planner transactionClient authority/source reads share one backend; + // a detached ALS child must refuse instead of falling back to the pool. + const borrowed = await pool.connect(), originalPoolQuery = pool.query, originalQuery = borrowed.query.bind(borrowed) + const plannerPids = new Set() + let releaseChild!: () => void, child: Promise | undefined + const gate = new Promise(yes => { releaseChild = yes }) + const context = { principal, connection: await getConnection(principal.connectionId) } + const plannerClient: Pick = { query: (async (text: string, values?: unknown[]) => { + if (!child) child = gate.then(() => host.target(raw, context)) + plannerPids.add(Number((await originalQuery("SELECT pg_backend_pid() AS pid")).rows[0].pid)) + return originalQuery(text, values) + }) as PoolClient["query"] } + try { + await originalQuery("BEGIN") + pool.query = (() => { throw Error("Pool query forbidden during actual planner lease") }) as typeof pool.query + const target = await host.target(raw, { ...context, transactionClient: plannerClient }) + assert.deepEqual(target, { scope: "servers", serverIds: [serverId] }); assert.equal(plannerPids.size, 1) + assert(child) + const stale = assert.rejects(child, error => error instanceof McpError && error.code === "STATE_CONFLICT") + releaseChild(); await stale + await originalQuery("COMMIT") + } finally { pool.query = originalPoolQuery; releaseChild(); borrowed.release(true) } + + const plan = await invokeMcpTool(principal, "mm_server_probe_plan", raw) as { planId: string; state: string } + hidden(plan); assert.equal(plan.state, "awaiting-approval"); assert.equal(requests, 0) + await assert.rejects(applyPlan(principal, plan.planId, randomUUID())); assert.equal(requests, 0) + await approvePlan(plan.planId, actor, true) + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key), terminal = await wait(operation.id) + assert.equal(terminal.state, "applied", "Owned host did not apply: " + String(terminal.error_code ?? "none")); hidden(terminal) + assert.equal(terminal.result.confirmedWrites, 0); assert.equal(terminal.result.verificationScope, "acknowledgement") + assert.equal(requests, 1); assert.equal(protocolErrors, 0) + const step = (await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0] + assert.equal(step.state, "acknowledged"); hidden(step.result) + assert.equal(step.result.status, "completed"); assert.equal(step.result.attemptedStages, 1); assert.equal(step.result.completedStages, 1) + for (const field of ["output", "data", "contentB64", "bindingCiphertext"]) assert.equal(Object.hasOwn(step.result, field), false) + const receiptId = String(step.result.receiptId) + const stored = (await pool.query(`SELECT j.*,p.receipt_ciphertext,p.receipt_proof AS payload_proof, + to_jsonb(j) ? 'receipt_ciphertext' AS anchor_has_ciphertext,a.owner_id,a.claim_token,a.claim_attempted_at + FROM server_probe_executions j JOIN server_probe_execution_payloads p USING(receipt_id) + JOIN server_probe_storage_admissions a USING(receipt_id) WHERE j.receipt_id=$1`, [receiptId])).rows + assert.equal(stored.length, 1); const row = stored[0] + assert.equal(row.anchor_has_ciphertext, false); assert.equal(row.owner_id, actor.id); assert.equal(row.execution_id, operation.id) + assert.equal(row.state, "completed"); assert.equal(row.claim_token, null); assert(row.claim_attempted_at) + assert.equal(row.receipt_proof === row.payload_proof, true) + const envelope = JSON.parse(await decryptSecret(row.receipt_ciphertext)); hidden(envelope) + assert.equal(envelope.receipt.state, "completed"); assert.equal(envelope.receipt.forwardingVerified, false) + const usage = (await pool.query("SELECT admissions,reserved_files,reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + actor.id])).rows[0] + assert.equal(Number(usage.admissions), 1); assert.equal(Number(usage.reserved_files), 1); assert.equal(Number(usage.reserved_bytes), 294912) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id); assert.equal(requests, 1) + const readRequest = { operationId: operation.id, receiptId, page: 0 } + const firstPage = await invokeMcpTool(principal, "mm_server_probe_receipt_get", readRequest) as { contentB64: string; sha256: string } + hidden(firstPage); hidden(JSON.parse(Buffer.from(firstPage.contentB64, "base64").toString("utf8"))) + + // A SECOND real runner operation is cancelled only after native dispatch. + // The owned HTTP fixture withholds its response; the production watcher + // must observe durable cancellation and abort the actual request socket. + const cancelPlan = await invokeMcpTool(principal, "mm_server_probe_plan", raw) as { planId: string; state: string } + hidden(cancelPlan); assert.equal(cancelPlan.state, "awaiting-approval") + await approvePlan(cancelPlan.planId, actor, true) + const cancelKey = randomUUID(), cancelOperation = await applyPlan(principal, cancelPlan.planId, cancelKey) + await boundedBarrier(secondRequest, "Second native request did not reach the owned HTTP barrier") + assert.equal(requests, 2); assert.equal(protocolErrors, 0) + // Queue both actual cancellation requests before either can commit. This + // prevents the watcher/terminal transition racing the duplicate assertion, + // without replacing or delaying any production authority/watcher callback. + const blocker = await connectProbeStorage({ deadlineAt: Date.now() + 20000 }) + let cancellationResults: PromiseSettledResult>>[] | undefined + try { + await blocker.query("BEGIN") + await blocker.query("SELECT pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0))") + const cancellations = Promise.allSettled([cancel(cancelOperation.id), cancel(cancelOperation.id)]) + let queued = false + for (const deadline = Date.now() + 12000; Date.now() < deadline;) { + const count = Number((await pool.query("SELECT count(*)::int AS n FROM pg_stat_activity WHERE datname=current_database() AND pid<>pg_backend_pid() AND wait_event='advisory' AND query LIKE '%probe-storage:lock%'")).rows[0].n) + if (count >= 2) { queued = true; break } + await delay(10) + } + assert.equal(queued, true, "Both real cancellation calls must queue before releasing the fixture barrier") + await blocker.query("COMMIT") + cancellationResults = await cancellations + } finally { blocker.release(true) } + assert(cancellationResults) + for (const result of cancellationResults) { + assert.equal(result.status, "fulfilled") + if (result.status !== "fulfilled") assert.fail("Owned cancellation was not acknowledged") + hidden(result.value); assert.equal(result.value.cancelRequested, true); assert.equal(result.value.terminal, false); assert.equal(result.value.automaticReplay, false) + } + const cancelled = await wait(cancelOperation.id); hidden(cancelled) + assert.equal(cancelled.state, "unknown"); assert.equal(cancelled.verification, "inconclusive") + assert.equal(cancelled.result.confirmedWrites, 0); assert.equal(cancelled.result.needsReview, true) + const progress = cancelled.result.diagnosticProgress + assert(progress); assert.equal(progress.operationId, cancelOperation.id); assert.equal(progress.state, "unknown") + assert.equal(progress.attemptedStages, 1); assert.equal(progress.completedStages, 0); assert.equal(progress.automaticReplay, false) + await boundedBarrier(secondSocketClosed, "Durable cancellation did not close the pending owned native socket") + const cancelledReceiptId = String(progress.receiptId) + const stopped = (await pool.query(`SELECT j.state,a.owner_id,a.claim_token,a.claim_attempted_at,a.cancelled_at, + (SELECT count(*)::int FROM server_probe_execution_quarantines q WHERE q.receipt_id=j.receipt_id AND q.execution_id=j.execution_id AND q.attempted_stages=1 AND q.completed_stages=0) AS quarantines, + (SELECT count(*)::int FROM server_probe_execution_payloads p WHERE p.receipt_id=j.receipt_id) AS payloads, + (SELECT count(*)::int FROM server_probe_execution_stages s WHERE s.receipt_id=j.receipt_id AND s.state='intent') AS pending_stages + FROM server_probe_executions j JOIN server_probe_storage_admissions a USING(receipt_id) + WHERE j.receipt_id=$1 AND j.execution_id=$2`, [cancelledReceiptId, cancelOperation.id])).rows + assert.equal(stopped.length, 1); assert.equal(stopped[0].state, "claimed"); assert.equal(stopped[0].owner_id, actor.id) + assert.equal(stopped[0].claim_token, null); assert(stopped[0].claim_attempted_at); assert(stopped[0].cancelled_at) + assert.equal(stopped[0].quarantines, 1); assert.equal(stopped[0].payloads, 0); assert.equal(stopped[0].pending_stages, 1) + const cancellationCount = async () => (await pool.query(`SELECT + (SELECT count(*)::int FROM mcp_diagnostic_cancellations WHERE operation_id=$1) AS records, + (SELECT count(*)::int FROM mcp_audit WHERE action='diagnostic.cancel-requested' AND actor_id=$2 AND payload->>'operationId'=$1::text) AS audits`, [cancelOperation.id, actor.id])).rows[0] + assert.deepEqual(await cancellationCount(), { records: 1, audits: 1 }) + const afterCancellation = (await pool.query("SELECT admissions,reserved_files,reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + actor.id])).rows[0] + assert.equal(Number(afterCancellation.admissions), 2); assert.equal(Number(afterCancellation.reserved_files), 2); assert.equal(Number(afterCancellation.reserved_bytes), 2 * 294912) + await assert.rejects(invokeMcpTool(principal, "mm_server_probe_receipt_get", { operationId: cancelOperation.id, receiptId: cancelledReceiptId, page: 0 })) + assert.equal((await applyPlan(principal, cancelPlan.planId, cancelKey)).id, cancelOperation.id) + const terminalCancellation = await cancel(cancelOperation.id) + assert.equal(terminalCancellation.terminal, true); assert.equal(terminalCancellation.cancelRequested, false) + assert.deepEqual(await cancellationCount(), { records: 1, audits: 1 }); assert.equal(requests, 2) + + const rotated = await rotateConnectionToken(principal.connectionId, actor); privateValues.push(rotated.token) + await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb,policy=jsonb_set(jsonb_set(jsonb_set(policy,'{permissions}',$2::jsonb),'{scope,operationIds}',$3::jsonb),'{mode}','\"observer\"'::jsonb) WHERE id=$1", [principal.connectionId, JSON.stringify(["mm:servers:read"]), JSON.stringify([SERVER_PROBE_RECEIPT_READ_OPERATION])]) + const currentReader = await authenticateMcpToken(rotated.token) + assert.notEqual(currentReader.credentialId, principal.credentialId) + await assert.rejects(invokeMcpTool(principal, "mm_server_probe_receipt_get", readRequest)) + const currentPage = await invokeMcpTool(currentReader, "mm_server_probe_receipt_get", readRequest) as { contentB64: string; sha256: string } + hidden(currentPage); hidden(JSON.parse(Buffer.from(currentPage.contentB64, "base64").toString("utf8"))) + assert.equal(currentPage.sha256, firstPage.sha256) + await assert.rejects(invokeMcpTool(currentReader, "mm_server_probe_plan", raw)); assert.equal(requests, 2) + return { actualRuntimeInvokeApproveApply: true, realInheritedVerifiedGuard: true, ownedLoopbackNativeRequests: requests, + modelMetadataOnly: true, sanitizedPublicHistory: true, separateEncryptedPayload: true, quotaChargedOnce: true, + idempotentOperationReplayWithoutNative: true, currentReadCredentialOriginalEnvelope: true, readOnlyCannotExecute: true, + actualRunningCancellation: true, realWatcherAbortedNativeSocket: true, duplicateCancellationAuditOnce: true, + cancelledAttemptSpentWithoutPayload: true, quarantineBlocksHistory: true, chargedAdmissions: 2, + plannerTransactionBackends: plannerPids.size, stalePlannerChildRefused: true, productionAuthorityOverrides: false, + fullRunnerPoolMaxOneClaimed: false, productionDdlApplied: false, realRouterOrDns: false, + actualPublicRegistry: true, actualPublicRunReceiptAndCancellationTools: true, fixtureRegistrationOnly: false } + } finally { + releaseSecondResponse() + if (connectionIds.length) await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"revoked\"'::jsonb) WHERE id=ANY($1::uuid[])", [connectionIds]) + if (serverId !== undefined) await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [priorSettings.enabled]) + router.closeAllConnections(); await new Promise(yes => router.close(() => yes())) + } +} + diff --git a/backend/src/modules/mcp/server-probe-storage-host.test.ts b/backend/src/modules/mcp/server-probe-storage-host.test.ts new file mode 100644 index 00000000..3de64f36 --- /dev/null +++ b/backend/src/modules/mcp/server-probe-storage-host.test.ts @@ -0,0 +1,64 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { createStorageServerProbeMcpAdapter, serverProbeStorageJournalDeadline } from "./server-probe-storage-host.js" +import { planServerProbeExecution } from "../../services/server-probe-execution-model.js" +import { PROBE_STORAGE_LIMITS } from "./diagnostic-cancellation-storage.js" +import { findMcpService } from "./service-registry.js" +import type { ActionContext } from "./action-types.js" + +const context = (): ActionContext => ({ principal: { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 60000).toISOString() }, connection: {} } as ActionContext) +const input = { serverId: 1, request: { tool: "ping", target: "127.0.0.1" } } +test("journal lifetime covers every supported executor budget and cannot outlive private plan or authority", () => { + const now = 1800000000000, authority = now + 3600000, prepared = now + 300000 + for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) { + const plan = planServerProbeExecution({ tool, target: "127.0.0.1", bwRemoteAddress: "127.0.0.1" }) + assert(serverProbeStorageJournalDeadline(authority, prepared, now) - now >= plan.wallBudgetMs, tool + " is cut off before its supported wall budget") + } + assert.equal(serverProbeStorageJournalDeadline(authority, now + 90000, now), now + 90000) + assert.equal(serverProbeStorageJournalDeadline(now + 60000, prepared, now), now + 60000) + assert.equal(serverProbeStorageJournalDeadline(authority, prepared, now + 150000), prepared) + for (const expiry of [NaN, Infinity, now, now - 1]) assert.throws(() => serverProbeStorageJournalDeadline(authority, expiry, now)) +}) +async function withoutPool(work: () => Promise | void) { + const originalQuery = pool.query, originalConnect = pool.connect + pool.query = (() => { assert.fail("Unexpected pool query") }) as typeof pool.query + pool.connect = (() => { assert.fail("Unexpected checkout") }) as typeof pool.connect + try { await work() } finally { pool.query = originalQuery; pool.connect = originalConnect } +} + +test("opt-in host construction is inert, validates trusted quota and registers no service", async () => { + await withoutPool(() => { + assert.equal(findMcpService("servers.probes.run"), undefined) + createStorageServerProbeMcpAdapter() + assert.equal(findMcpService("servers.probes.run"), undefined) + assert.throws(() => createStorageServerProbeMcpAdapter({ ...PROBE_STORAGE_LIMITS, globalFiles: 0 })) + }) +}) +test("pre-aborted target refuses before pool or borrowed planner SQL", async () => { + await withoutPool(async () => { + const host = createStorageServerProbeMcpAdapter(), controller = new AbortController(); controller.abort() + await assert.rejects(host.target(input, { ...context(), signal: controller.signal }), { code: "READ_CANCELLED" }) + const transactionClient = { query: (() => assert.fail("Unexpected borrowed planner SQL")) as PoolClient["query"] } + await assert.rejects(host.target(input, { ...context(), signal: controller.signal, transactionClient }), { code: "READ_CANCELLED" }) + }) +}) +test("detached child of failed planner context cannot fall back to the pool", async () => { + await withoutPool(async () => { + const host = createStorageServerProbeMcpAdapter(), actor = context() + let release!: () => void, child: Promise | undefined + const gate = new Promise(resolve => { release = resolve }) + const transactionClient = { query: (async () => { + child ??= gate.then(() => host.target(input, actor)) + return { rows: [] } + }) as unknown as PoolClient["query"] } + try { + await assert.rejects(host.target(input, { ...actor, transactionClient })) + assert(child) + const rejected = assert.rejects(child, { code: "STATE_CONFLICT" }) + release(); await rejected + } finally { release() } + }) +}) diff --git a/backend/src/modules/mcp/server-probe-storage-host.ts b/backend/src/modules/mcp/server-probe-storage-host.ts new file mode 100644 index 00000000..9cb78f0a --- /dev/null +++ b/backend/src/modules/mcp/server-probe-storage-host.ts @@ -0,0 +1,59 @@ +/** Shared production v2 MCP assembly; normal startup installs schema0037. */ +import { createMcpServerProbeAuthority, createMcpServerProbeReadAuthority } from "../../services/server-probe-authority.js" +import { createProductionServerProbeExecutor } from "../../services/server-probe-execution.js" +import { createServerProbeStorageHistoryReader } from "../../services/server-probe-storage-history.js" +import { findRetiredServerProbeHistory } from "../../services/server-probe-retired-history.js" +import { createSqlServerProbeSourceReaders } from "../../services/server-probe-storage-source.js" +import { createProbeStorageHostIo } from "../../services/server-probe-storage-host-io.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { createServerProbeMcpAdapter } from "./server-probe-actions.js" +import { createStorageDiagnosticCancellationRequest, PROBE_STORAGE_LIMITS, type ProbeStorageLimits } from "./diagnostic-cancellation-storage.js" +import { effectiveMcpConnection } from "./store.js" +import type { ActionContext } from "./action-types.js" +import type { McpServiceHandler } from "./service-registry.js" +export { serverProbeStorageJournalDeadline } from "../../services/server-probe-storage-host-io.js" + +export function createStorageServerProbeMcpAdapter(limits: ProbeStorageLimits = PROBE_STORAGE_LIMITS) { + const configured = Object.freeze({ ...limits }), io = createProbeStorageHostIo(configured, "mcp") + const authorityDependencies = { + query: io.rows, + connection: ((principal, client) => { + const current = io.currentClient() + if (current) return effectiveMcpConnection(principal, current) + return client ? io.withBorrowed(client, () => effectiveMcpConnection(principal, client)) : io.withClient(sql => effectiveMcpConnection(principal, sql), { deadlineAt: Date.parse(principal.credentialExpiresAt) }) + }) as typeof effectiveMcpConnection, + verified: async () => { + // Executing checks run between journal methods, never under storage locks. + io.assertOutside() + await assertVerifiedConfigurationAccess() + }, + } + const adapter = createServerProbeMcpAdapter({ query: io.rows, journal: io.journal, + retiredSource: lookup => findRetiredServerProbeHistory(io.rows, lookup), + authority: (principal, options) => createMcpServerProbeAuthority(principal, options, authorityDependencies), + historicalAuthority: (principal, options) => createMcpServerProbeReadAuthority(principal, options, authorityDependencies), + history: (dependencies, execution) => createServerProbeStorageHistoryReader({ assertReadAuthority: dependencies.assertReadAuthority, + options: { deadlineAt: Date.now() + 120000, signal: execution?.signal } }), + executor: input => createProductionServerProbeExecutor(input, { + getServer: async id => createSqlServerProbeSourceReaders({ query: io.query }, (await input.readAuthority()).scopeServerIds).getServer(id), + findDestination: async (remote, explicit, scope) => createSqlServerProbeSourceReaders({ query: io.query }, scope).findDestination(remote, explicit, scope), + }), + }) + const window = (work: () => Promise, signal?: AbortSignal, deadlineAt = Date.now() + 300000) => io.runRequest({ deadlineAt, signal }, work) + const actionWindow = (context: ActionContext, work: () => Promise) => window(work, context.signal, Math.min(Date.now() + 300000, Date.parse(context.principal.credentialExpiresAt))) + const service: McpServiceHandler = { ...adapter.service, + current: raw => window(() => adapter.service.current(raw)), + diagnosticIdentity: (raw, context) => window(() => adapter.service.diagnosticIdentity!(raw, context), context.phase === "forward" ? context.signal : undefined), + execute: (raw, context) => window(() => adapter.service.execute(raw, context), context?.phase === "forward" ? context.signal : undefined), + verify: (raw, result) => window(() => adapter.service.verify!(raw, result)), + reconcile: (raw, authorize) => window(() => adapter.service.reconcile!(raw, authorize)), + } + return { ...adapter, service, + target: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => context.transactionClient + ? io.withBorrowed(context.transactionClient, () => adapter.target(raw, { ...context, transactionClient: io.currentClient() })) : adapter.target(raw, context)), + prepare: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => adapter.prepare(raw, context)), + receiptTarget: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => adapter.receiptTarget(raw, context)), + readPage: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => adapter.readPage(raw, context)), + cancel: createStorageDiagnosticCancellationRequest(configured), + } +} diff --git a/backend/src/modules/mcp/service-diagnostic-progress.test.ts b/backend/src/modules/mcp/service-diagnostic-progress.test.ts new file mode 100644 index 00000000..5711a87b --- /dev/null +++ b/backend/src/modules/mcp/service-diagnostic-progress.test.ts @@ -0,0 +1,16 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { McpError } from "./security.js" +import { serviceDiagnosticProgress, serviceDiagnosticReceipt } from "./service-diagnostic-progress.js" +const operationId = randomUUID(), receiptId = randomUUID(), base = { operationId, receiptId, stageBudget: 11, attemptedStages: 4, completedStages: 3, automaticReplay: false as const } +const error = Object.assign(new McpError("CANCELLED", "NEVER_PROVIDER_BODY"), { diagnosticProgress: { ...base, state: "unknown" } }) +assert.deepEqual(serviceDiagnosticProgress(error, true, operationId, 11), { ...base, state: "unknown" }) +assert.equal(serviceDiagnosticProgress(error, false, operationId, 11), undefined) +assert.equal(serviceDiagnosticProgress(error, true, randomUUID(), 11), undefined) +assert.equal(serviceDiagnosticProgress(error, true, operationId, 10), undefined) +for (const change of [{ attemptedStages: 12 }, { completedStages: 5 }, { stageBudget: Infinity }, { state: "VERIFIED" }, { providerBody: "NEVER_PROVIDER_BODY" }, { receiptId: "invalid" }, { automaticReplay: true }]) assert.equal(serviceDiagnosticProgress(Object.assign(new McpError("CANCELLED", "private"), { diagnosticProgress: { ...base, state: "unknown", ...change } }), true, operationId, 11), undefined) +const metadata = { ...base, attemptedStages: 4, completedStages: 4, tool: "mtu", status: "completed", httpStatus: 200, sha256: "a".repeat(64), totalPages: 2, bytes: 11000, forwardingVerified: false } +assert.deepEqual(serviceDiagnosticReceipt(metadata, true, operationId, 11), metadata) +for (const change of [{ output: "NEVER_PROVIDER_BODY" }, { status: "unknown" }, { completedStages: 3 }, { bytes: 131073 }, { forwardingVerified: true }, { totalPages: 16 }]) assert.equal(serviceDiagnosticReceipt({ ...metadata, ...change }, true, operationId, 11), undefined) +assert.equal(serviceDiagnosticReceipt(metadata, false, operationId, 11), undefined) +console.log("Diagnostic progress/metadata: exact operation and budget binding, bounded stage counts, no native configuration-write claims, no private payload or replay passed") diff --git a/backend/src/modules/mcp/service-diagnostic-progress.ts b/backend/src/modules/mcp/service-diagnostic-progress.ts new file mode 100644 index 00000000..9c5ad6d6 --- /dev/null +++ b/backend/src/modules/mcp/service-diagnostic-progress.ts @@ -0,0 +1,19 @@ +import { z } from "zod" +import { McpError } from "./security.js" + +const base = { operationId: z.string().uuid(), receiptId: z.string().uuid(), stageBudget: z.number().int().min(1).max(11), attemptedStages: z.number().int().min(0).max(11), completedStages: z.number().int().min(0).max(11), automaticReplay: z.literal(false) } +const progress = z.object({ ...base, state: z.enum(["completed", "failed", "cancelled", "unknown"]) }).strict() +const receipt = z.object({ ...base, tool: z.enum(["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]), status: z.literal("completed"), httpStatus: z.literal(200), sha256: z.string().regex(/^[a-f0-9]{64}$/), totalPages: z.number().int().min(1).max(15), bytes: z.number().int().min(1).max(131072), forwardingVerified: z.literal(false) }).strict() +const bound = (value: z.infer, operationId: string, budget: number | undefined) => value.operationId === operationId && Number.isSafeInteger(budget) && value.stageBudget <= budget! && value.attemptedStages <= value.stageBudget && value.completedStages <= value.attemptedStages + +export function serviceDiagnosticProgress(error: unknown, diagnostic: boolean, operationId: string, budget: number | undefined) { + if (!diagnostic || !(error instanceof McpError)) return undefined + const parsed = progress.safeParse((error as McpError & { diagnosticProgress?: unknown }).diagnosticProgress) + return parsed.success && bound(parsed.data, operationId, budget) ? parsed.data : undefined +} +export function serviceDiagnosticReceipt(value: unknown, diagnostic: boolean, operationId: string, budget: number | undefined) { + if (!diagnostic) return undefined + const parsed = receipt.safeParse(value) + if (!parsed.success || !bound({ ...parsed.data, state: "completed" }, operationId, budget) || parsed.data.attemptedStages < 1 || parsed.data.completedStages !== parsed.data.attemptedStages) return undefined + return parsed.data +} diff --git a/backend/src/modules/mcp/service-execution-origin.sql.test.ts b/backend/src/modules/mcp/service-execution-origin.sql.test.ts new file mode 100644 index 00000000..73a2e6a7 --- /dev/null +++ b/backend/src/modules/mcp/service-execution-origin.sql.test.ts @@ -0,0 +1,18 @@ +import assert from "node:assert/strict" +import {assertOwnedBandwidthSql,prepareOwnedBandwidthState} from "../../services/bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql();await prepareOwnedBandwidthState() +const {pool,closePool}=await import("../../db/index.js") +const {applySqlMigrations}=await import("../../db/migrate.js") +const ownIds:number[]=[] +try{ + await applySqlMigrations(pool) + for(let index=0;index<3;index++)ownIds.push(Number((await pool.query("INSERT INTO public.servers(name,host,enabled) VALUES('owned-service-origin','127.0.0.1',false) RETURNING id")).rows[0].id)) + const {acceptBandwidthServiceContextSql}=await import("./bandwidth-service-context.sql-helper.js") + const result=await acceptBandwidthServiceContextSql(ownIds) + assert.equal(result.flows,2);assert.equal(result.verifiedServiceSteps,4);assert.equal(result.currentPlanChecks,12);assert.equal(result.rejectedPlanChanges,8) + console.log("Owned MCP service origin: "+JSON.stringify(result)) +}finally{ + try{if(ownIds.length)await pool.query("DELETE FROM public.servers WHERE id=ANY($1::bigint[])",[ownIds])} + finally{await closePool()} +} diff --git a/backend/src/modules/mcp/service-execution-origin.ts b/backend/src/modules/mcp/service-execution-origin.ts new file mode 100644 index 00000000..08f57f36 --- /dev/null +++ b/backend/src/modules/mcp/service-execution-origin.ts @@ -0,0 +1,44 @@ +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import type { QueryConfig } from "pg" +import { pool } from "../../db/index.js" +import { inspectVerifiedMcpServiceExecution } from "../../services/config-mutation-policy.js" +import type { BandwidthScope } from "../../services/bandwidth-admission.js" +import { McpError, redact } from "./security.js" + +const deny=():never=>{throw new McpError("PERMISSION_DENIED","Исходный сервисный шаг MCP изменён или недоступен",403)} +/** Current database proof for the genuine runtime origin. This is not an + * operation allowlist, peer identity or bandwidth dispatch permission. No lease + * is retained while the final runner authorizer acquires its own transaction. */ +export async function readCurrentMcpServiceExecution(token:unknown,input:Readonly){ + if(!input||!Number.isSafeInteger(input.deadlineAt)||input.signal!==undefined&&!(input.signal instanceof AbortSignal))return deny() + const scope=Object.freeze({...input}),end=performance.now()+Math.max(0,scope.deadlineAt-Date.now()) + const live=()=>{if(scope.signal?.aborted||Date.now()>=scope.deadlineAt||performance.now()>=end)deny()} + live();const original=await inspectVerifiedMcpServiceExecution(token);live() + const expectedStep=redact({kind:"service",service:original.serviceName,input:original.serviceInput}) + // pg supports per-query read timeout; its installed declaration omits it. + const query:QueryConfig&{query_timeout:number}={text:`SELECT o.state AS operation_state,p.state AS plan_state,s.state AS step_state, + p.operation_id,p.operation_version,p.policy_version,p.generation,p.expires_at,p.approved_by,c.owner_id, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(p.evidence->>'sealed','UTF8')),'hex') AS evidence_proof, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(p.payload->>'sealed','UTF8')),'hex') AS payload_proof, + s.intended_state,s.server_id + FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_connections c ON c.id=o.connection_id + JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=$4 + WHERE o.id=$1 AND p.id=$2 AND o.connection_id=$3 LIMIT 2`, + values:[original.operationId,original.planId,original.connectionId,original.stepIndex], + query_timeout:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))} + const result=await pool.query(query) + live() + if(result.rows.length!==1)return deny() + const row=result.rows[0] + if(row.operation_state!=="running"||row.plan_state!=="executing"||row.step_state!=="intent"||row.server_id!==null + ||row.operation_id!==original.operationName||row.operation_version!==original.operationVersion||row.policy_version!==original.policyVersion + ||row.generation!==original.generation||row.owner_id!==original.ownerId||!!row.approved_by!==original.approved + ||!(row.expires_at instanceof Date)||!Number.isFinite(row.expires_at.getTime())||row.expires_at.getTime() Number.isSafeInteger(value) && value! >= 0) || commandCount! > Math.min(mutationCost!, 10000) || confirmedWrites! > commandCount! || completedCommands! > confirmedWrites!) return undefined + return { commandCount: commandCount!, acknowledgedWrites: confirmedWrites!, verifiedCommands: completedCommands!, automaticReplay: false } +} diff --git a/backend/src/modules/mcp/service-registry.ts b/backend/src/modules/mcp/service-registry.ts new file mode 100644 index 00000000..37ceace0 --- /dev/null +++ b/backend/src/modules/mcp/service-registry.ts @@ -0,0 +1,28 @@ +import type { RoutingReservedForwardAllocation } from "./routing-reservation-store.js" +import type { createRoutingNetworkNativeSession } from "./routing-network-native-session.js" + +export type RoutingReservedNetworkSession = Awaited> + +/** Trusted runner metadata. Never deserialized from service input JSON. */ +export type McpServiceExecutionContext = + | { operationId: string; phase: "forward"; stepIndex: number; signal?: AbortSignal; routingReservationForward?: RoutingReservedForwardAllocation; + retainRoutingNetworkSession?: (session: RoutingReservedNetworkSession) => void } + | { operationId: string; phase: "recovery" }; +export interface McpServiceHandler { + /** Explicit diagnostic opt-in; configuration writes are never cancelled by this lane. */ + cancellableDiagnostic?: true; + /** Read-only exact owned-plan identity, used only when the durable watcher + * refuses before execute is entered. Must not claim a journal or send I/O. */ + diagnosticIdentity?: (input: Record, context: McpServiceExecutionContext) => Promise<{ receiptId: string; stageBudget: number }>; + current: (input: Record) => Promise; + execute: (input: Record, context?: McpServiceExecutionContext) => Promise; + verify?: (input: Record, result: unknown) => Promise; + /** Read-only comparison with server-sealed original/desired state; never retries a mutation. */ + reconcile?: (input: Record, authorize: () => Promise) => Promise<{ matchesDesiredState: boolean; matchesOriginalState: boolean }>; +} +const services = new Map() +export function registerMcpService(name: string, handler: McpServiceHandler) { + if (services.has(name)) throw new Error("Повторяющийся MCP service: " + name) + services.set(name, handler) +} +export function findMcpService(name: string) { return services.get(name) } diff --git a/backend/src/modules/mcp/sidebar-counts-actions.ts b/backend/src/modules/mcp/sidebar-counts-actions.ts new file mode 100644 index 00000000..edca99cf --- /dev/null +++ b/backend/src/modules/mcp/sidebar-counts-actions.ts @@ -0,0 +1,34 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { readSidebarCounts } from "../../services/sidebar-counts.js" +import { McpError } from "./security.js" +import type { add } from "./actions.js" + +// The response covers every section and every server, not a selected server page. +export const sidebarCountsPermissions = ["mm:settings:admin", "mm:servers:read", "mm:users:read", "mm:filters:read", "mm:uptime:read", "mm:network:read", "mm:certificates:read", "mm:bgp:read", "mm:containers:read"] as const +type Counts = Awaited> +export function sidebarCountsMcpView(result: Counts) { + const unknownCounters = Object.entries(result).filter(([, value]) => value === undefined).map(([key]) => key) + return { counts: Object.fromEntries(Object.entries(result).map(([key, value]) => [key, value ?? null])), unknownCounters, incomplete: unknownCounters.length > 0 } +} +export function registerSidebarCountsActions(register: typeof add, collect = readSidebarCounts, catalogSize: () => Promise = async () => (await pool.query("SELECT count(*)::text AS total FROM servers")).rows[0]?.total) { + let pending: Promise<{ counts: Counts; startedAt: string; observedAt: string }> | undefined + register("system.sidebar.counts.read", "mm_sidebar_counts_get", "Глобальные счётчики разделов: неизвестное значение отличается от нуля", "settings", sidebarCountsPermissions, "read", "global", z.object({}).strict(), { + read: async () => { + try { + const total = await catalogSize() + if (!/^\d+$/.test(total) || BigInt(total) > 100n) throw new McpError("LIMIT_EXCEEDED", "Глобальные live-счётчики MCP ограничены 100 серверами", 409) + // Concurrent callers share the same bounded collection, including after + // a caller disconnects; no new native work is started for that caller. + if (!pending) { + const startedAt = new Date().toISOString() + const collection = collect().then(counts => ({ counts, startedAt, observedAt: new Date().toISOString() })) + pending = collection + void collection.finally(() => { if (pending === collection) pending = undefined }).catch(() => {}) + } + const result = await pending + return { ...sidebarCountsMcpView(result.counts), startedAt: result.startedAt, observedAt: result.observedAt, atomicSnapshot: false, scope: "global-all-servers", forwardingVerified: false } + } catch (error) { if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Основные глобальные счётчики не подтверждены", 503) } + }, + }) +} diff --git a/backend/src/modules/mcp/simple-speed-chr.test.ts b/backend/src/modules/mcp/simple-speed-chr.test.ts new file mode 100644 index 00000000..141e69aa --- /dev/null +++ b/backend/src/modules/mcp/simple-speed-chr.test.ts @@ -0,0 +1,173 @@ +/** Opt-in, one human-approved ordinary RouterOS speed run on the two owned + * CHRs. The exclusive owned-PG runner supplies credentials; never replay an + * uncertain run. No registry/Q/key/control-source setup or router reconfigure. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { Agent } from "node:http" +import { basename,dirname,isAbsolute,resolve } from "node:path" +import { mock } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool, closePool } from "../../db/index.js" +import { applySqlMigrations } from "../../db/migrate.js" +import type { AuthUser } from "../../lib/permissions.js" +import { MikrotikClient, MikrotikError } from "../../services/mikrotik.js" +import { createMcpConnection, authenticateMcpToken, updateMcpSettings } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan, loadPlan } from "./runtime.js" +import { decryptSecret } from "./security.js" +import { routerClient } from "./resources.js" + +assert.equal(process.env.MMAPP_OWN_SIMPLE_SPEED_CHR, "true", "Exclusive owned CHR runner required") +const ownedDatabase=new URL(process.env.DATABASE_URL??"") +assert.equal(ownedDatabase.hostname,"127.0.0.1");assert.equal(ownedDatabase.username,"mcp_test");assert.equal(ownedDatabase.pathname,"/postgres") +assert(process.env.MCP_STATE_DIR&&isAbsolute(process.env.MCP_STATE_DIR));assert.equal(basename(resolve(process.env.MCP_STATE_DIR)),"state") +assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const hosts = ["194.226.121.150", "104.165.244.7"] as const +if (process.env.MMAPP_TEST_CHR_HOSTS) assert.deepEqual(process.env.MMAPP_TEST_CHR_HOSTS.split(",").map(value => value.trim()), hosts) +const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) +if (ports) assert(ports.length === 2 && ports.every(port => Number.isSafeInteger(port) && port >= 1024 && port <= 65535)) +assert(process.env.MMAPP_TEST_CHR_USER && process.env.MMAPP_TEST_CHR_PASSWORD, "CHR credentials required") +const actor: AuthUser = { id: "simple-speed-chr-owner", email: "simple-speed-owned@example.invalid", name: "Owned CHR acceptance", apps: ["mm"], permissions: ["mm:settings:admin", "mm:uptime:write"], expiresAt: Date.now() + 1800000 } +const counts = { bandwidthPosts: 0, pingPosts: 0, routerConfigurationMutations: 0 } +type NativeTelemetry = { outcome: "fulfilled"; responseShape: "array" | "object" | "other"; finalNativeStatus: string | null } + | { outcome: "rejected"; errorClass: "routeros-http" | "abort" | "error" | "other"; httpStatus: number | null; reasonCategory: "login-limit" | "authentication" | "permission" | "invalid-argument" | "device-mode" | "network" | "other" } +const report = { startedAt: new Date().toISOString(), finishedAt: "", complete: false, outcome: "preflight" as string, + elapsedMs: 0, peers: hosts, protocol: "tcp", direction: "both", durationSec: 3, maxMbps: 1, + preflight: [] as Array<{ serverId: number; resourceRead: boolean; bandwidthServerEnabled?: boolean }>, + planId: null as string | null, operationId: null as string | null, sealedRunId: null as string | null, + operationState: null as string | null, operationErrorCode: null as string | null, phase: "setup", nativeTelemetry: null as NativeTelemetry | null, + nativeValidation: null as null | { addressMatches: boolean; protocolMatches: boolean; durationMatches: boolean; rateMatches: boolean; timeoutMatches: boolean }, + nativeForwardEntered: false, + historyCount: 0, rates: null as null | { txAvgMbps: number | null; rxAvgMbps: number | null }, counts } +const save = () => writeFile(new URL("../../../../docs/mcp-simple-speed-chr-acceptance.json", import.meta.url), JSON.stringify(report, null, 2) + "\n") +const started = performance.now() +let phase = "setup" +// Preserve public server identity/HTTP Host and redirect only the TCP socket +// of these two plain-HTTP endpoints through operator-provided SSH forwards. +const createConnection = Agent.prototype.createConnection +if (ports) mock.method(Agent.prototype, "createConnection", function (this: Agent, ...args: Parameters) { + const [options, ...rest] = args + const index = hosts.findIndex(host => options.host === host || options.hostname === host) + if (index < 0 || Number(options.port) !== 80) return Reflect.apply(createConnection, this, args) as ReturnType + return Reflect.apply(createConnection, this, [{ ...options, host: "127.0.0.1", hostname: "127.0.0.1", port: ports[index] }, ...rest]) as ReturnType +}) +// Forward the actual client. Count/refuse all configuration methods and any +// unexpected POST; no mock response, credentials/body capture or extra pool. +const nativePost = MikrotikClient.prototype.post +mock.method(MikrotikClient.prototype, "post", function (this: MikrotikClient, ...args: Parameters) { + const [path, body, timeout] = args + if (path === "/tool/bandwidth-test") { + counts.bandwidthPosts++ + report.nativeValidation = { addressMatches: body.address === hosts[1], protocolMatches: body.protocol === "tcp" && body.direction === "both", + durationMatches: body.duration === "3s", rateMatches: body["local-tx-speed"] === "1000000" && body["remote-tx-speed"] === "1000000", timeoutMatches: timeout === 30000 } + assert.equal(counts.bandwidthPosts, 1, "Never replay native speed test") + assert.equal(body.address, hosts[1]); assert.equal(body.protocol, "tcp"); assert.equal(body.direction, "both") + assert.equal(body.duration, "3s"); assert.equal(body["local-tx-speed"], "1000000"); assert.equal(body["remote-tx-speed"], "1000000") + assert.equal(timeout, 30000) + } else { + assert.equal(path, "/tool/ping", "Only the normal post-test ping is permitted") + counts.pingPosts++; assert.equal(counts.pingPosts, 1); assert.equal(body.address, hosts[1]); assert.equal(body.count, "4"); assert.equal(body.interval, "1s"); assert.equal(timeout, 20000) + } + if (path === "/tool/bandwidth-test") report.nativeForwardEntered = true + const forwarded = Reflect.apply(nativePost, this, args) as ReturnType + if (path !== "/tool/bandwidth-test") return forwarded + return forwarded.then(value => { + const final = Array.isArray(value) ? value.at(-1) : value + const status: unknown = final && typeof final === "object" ? Object.getOwnPropertyDescriptor(final, "status")?.value : undefined + const normalized = typeof status === "string" ? status.trim().toLowerCase() : "" + const allowed = ["done testing", "connecting", "running", "testing", "authentication failed", "can not connect", "can not start test", "remote is busy", "test unsupported", "disconnected"] + report.nativeTelemetry = { outcome: "fulfilled", responseShape: Array.isArray(value) ? "array" : value !== null && typeof value === "object" ? "object" : "other", + finalNativeStatus: allowed.includes(normalized) ? normalized : null } + return value + }, (error: unknown) => { + const httpStatus = error instanceof MikrotikError && Number.isInteger(error.statusCode) && error.statusCode >= 100 && error.statusCode <= 599 ? error.statusCode : null + // Inspect transient private text only to classify; never retain/log it. + const text = error instanceof MikrotikError ? error.body.slice(0, 4096).toLowerCase() : error instanceof Error ? error.message.slice(0, 4096).toLowerCase() : "" + const reasonCategory: Extract["reasonCategory"] = /too many concurrent logins/.test(text) ? "login-limit" + : httpStatus === 401 || /authentication failed|invalid user|invalid password/.test(text) ? "authentication" + : httpStatus === 403 || /not enough permissions|permission denied/.test(text) ? "permission" + : /device.mode|not allowed by device/.test(text) ? "device-mode" + : /invalid argument|invalid value|unknown parameter|no such command/.test(text) ? "invalid-argument" + : /econn|enet|ehost|timed? ?out|socket|connection reset|can not connect/.test(text) ? "network" : "other" + report.nativeTelemetry = { outcome: "rejected", errorClass: error instanceof MikrotikError ? "routeros-http" : error instanceof Error && error.name === "AbortError" ? "abort" : error instanceof Error ? "error" : "other", httpStatus, reasonCategory } + throw error + }) +}) +for (const method of ["put", "patch", "delete"] as const) mock.method(MikrotikClient.prototype, method, () => { + counts.routerConfigurationMutations++; throw new Error("Router configuration writes prohibited in simple speed acceptance") +}) +try { + await save(); await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Owned empty test database required") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_speed_test_runs")).rows[0].n, 0) + await updateMcpSettings({ enabled: true }, actor) + for (const [index, host] of hosts.entries()) await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES($1,$2,$3,$4,false,$5,$6)", + [index + 1, "Owned simple speed CHR", host, 80, process.env.MMAPP_TEST_CHR_USER, process.env.MMAPP_TEST_CHR_PASSWORD]) + phase = "read-only-preflight" + for (const [index, host] of hosts.entries()) { + const { client } = await routerClient(index + 1) + phase=`preflight-resource-${index+1}` + const resource = await client.getResource() + assert(resource && typeof resource.version === "string", "RouterOS resource response required") + phase=`preflight-addresses-${index+1}` + const addresses = await client.get>>("/ip/address") + assert(Array.isArray(addresses)) + const matching = addresses.filter(row => row.address?.split("/")[0] === host && row.disabled !== "true" && row.invalid !== "true") + assert.equal(matching.length, 1, "Fixed CHR public address must have one eligible interface") + assert.equal(typeof matching[0].interface, "string"); assert(matching[0].interface.length > 0) + const item: typeof report.preflight[number] = { serverId: index + 1, resourceRead: true } + if (index === 1) { + phase="preflight-bandwidth-server" + const raw = await client.get | Array>>("/tool/bandwidth-server") + const settings = Array.isArray(raw) ? (assert.equal(raw.length, 1), raw[0]) : raw + assert(settings && [true, "true", "yes"].includes(settings.enabled as string | boolean), "Destination bandwidth-server must already be enabled") + item.bandwidthServerEnabled = true + } + report.preflight.push(item) + } + assert.equal(counts.bandwidthPosts, 0); await save() + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned simple speed CHR", mode: "approval", + expiresAt: new Date(Date.now() + 900000).toISOString(), permissions: ["mm:uptime:write"], scope: { serverIds: [1, 2], operationIds: ["uptime.speed.test"], allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + phase = "plan" + const plan = await invokeMcpTool(principal, "mm_speed_test_plan", { srcServerId: 1, dstServerId: 2, protocol: "tcp", direction: "both", durationSec: 3, maxMbps: 1 }) as { planId: string } + report.planId = plan.planId + const stored = await loadPlan(plan.planId) + const evidence = JSON.parse(await decryptSecret(stored.evidence.sealed)) as { steps: Array<{ kind: string; service: string; input: { runId: string } }> } + assert.equal(evidence.steps.length, 1); assert.equal(evidence.steps[0].service, "uptime.speed.test") + const runId = evidence.steps[0].input.runId; assert.match(runId, /^[a-f0-9-]{36}$/); report.sealedRunId = runId + await approvePlan(plan.planId, actor, true) + phase = "one-approved-native-run"; report.outcome = "unknown"; await save() + const operation = await applyPlan(principal, plan.planId, randomUUID()); report.operationId = operation.id + let terminal = false + for (let i = 0; i < 360; i++) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + report.operationState = row?.state ?? null + report.operationErrorCode = typeof row?.error_code === "string" && /^[A-Z0-9_]{1,80}$/.test(row.error_code) ? row.error_code : null + if (row && !["pending", "running"].includes(row.state)) { terminal = true; break } + await delay(250) + } + assert(terminal, "Operation still unknown; do not replay") + const steps = await pool.query("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [operation.id]) + const history = await pool.query("SELECT id,src_server_id::int AS src_server_id,dst_server_id::int AS dst_server_id,tx_avg_mbps,rx_avg_mbps,status,duration_sec,protocol,direction FROM uptime_speed_test_runs WHERE id=$1", [runId]) + report.historyCount = history.rowCount ?? 0 + assert.equal(report.operationState, "applied", "Operation not confirmed; do not replay") + assert.equal(steps.rows.length, 1); const receipt = steps.rows[0].result + assert.equal(receipt.completed, true); assert.equal(receipt.runId, runId) + assert.equal(history.rows.length, 1); assert.equal(history.rows[0].src_server_id, 1); assert.equal(history.rows[0].dst_server_id, 2) + assert.equal(history.rows[0].status, "done"); assert.equal(history.rows[0].duration_sec, 3) + assert.equal(history.rows[0].protocol, "tcp"); assert.equal(history.rows[0].direction, "both") + assert.equal(history.rows[0].tx_avg_mbps, receipt.txAvgMbps); assert.equal(history.rows[0].rx_avg_mbps, receipt.rxAvgMbps) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_speed_test_runs")).rows[0].n, 1) + assert.equal(counts.bandwidthPosts, 1); assert.equal(counts.pingPosts, 1); assert.equal(counts.routerConfigurationMutations, 0) + const finite = (value: unknown) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : null + report.rates = { txAvgMbps: finite(receipt.txAvgMbps), rxAvgMbps: finite(receipt.rxAvgMbps) } + report.complete = true; report.outcome = "confirmed-native-receipt-and-history" +} catch { + report.outcome = counts.bandwidthPosts > 0 || phase === "one-approved-native-run" ? "unknown-no-replay" : "refused-before-native" + throw new Error("SIMPLE_SPEED_CHR_ACCEPTANCE_FAILED: " + phase + "; consult sanitized report; never replay automatically") +} finally { + report.phase = phase; report.finishedAt = new Date().toISOString(); report.elapsedMs = Math.round(performance.now() - started) + try { await save() } finally { mock.restoreAll(); await closePool() } +} diff --git a/backend/src/modules/mcp/simple-speed-http.test.ts b/backend/src/modules/mcp/simple-speed-http.test.ts new file mode 100644 index 00000000..f2091f1f --- /dev/null +++ b/backend/src/modules/mcp/simple-speed-http.test.ts @@ -0,0 +1,145 @@ +import { MIGRATION_MANIFEST } from "../../db/migration-manifest.js" +/** Ordinary MCP/REST speed acceptance: disposable app PG and two localhost + * RouterOS HTTP responders. No control registry, Q or enrollment components. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { performance } from "node:perf_hooks" +import { SignJWT } from "jose" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" + +async function main() { + assert.equal(process.env.MMAPP_OWN_SIMPLE_SPEED, "true") + const target = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(target.protocol)); assert.equal(target.hostname, "127.0.0.1") + assert.equal(target.username, "mcp_test"); assert(target.password); assert.equal(target.pathname, "/postgres") + assert(Number.isSafeInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) + assert.equal(target.search, ""); assert.equal(target.hash, "") + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) + assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.AUTH_REQUIRED = "true"; process.env.AUTH_JWT_SECRET = "owned-simple-speed-acceptance-secret" + process.env.AUTH_ISSUER = "https://owned-simple-speed.test" + const { pool, closePool } = await import("../../db/index.js") + const { applySqlMigrations } = await import("../../db/migrate.js") + const { buildApp } = await import("../../index.js") + const { createMcpConnection, authenticateMcpToken, updateMcpSettings } = await import("./store.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("./runtime.js") + const owner = { id: "owned-simple-speed-owner", email: "owned-speed@example.test", name: "Owned speed fixture", apps: ["mm"], permissions: ["mm:settings:admin", "mm:uptime:write"], expiresAt: Date.now() + 3600000 } + const secret = "owned-destination-password", counts = { bandwidth: 0, ping: 0, addresses: 0 }, groups: string[] = [] + let mode: "success" | "drop" = "success", fixtureFailed = false + const nativeBodies: Array> = [] + const responders = [1, 2].map(index => createServer((request, response) => { + void (async () => { + assert.equal(request.headers.authorization, "Basic " + Buffer.from(`owned-${index}:${index === 1 ? "owned-source-password" : secret}`).toString("base64")) + response.setHeader("content-type", "application/json") + if (request.method === "GET" && request.url?.split("?")[0] === "/rest/ip/address") { + counts.addresses++; response.end(JSON.stringify([{ ".id": "*1", address: `198.51.100.${index}/24`, interface: `ether-${index}`, disabled: "false", invalid: "false" }])); return + } + const parts: Buffer[] = []; let bytes = 0 + for await (const part of request) { const chunk = Buffer.from(part); bytes += chunk.length; assert(bytes <= 16384); parts.push(chunk) } + const body = JSON.parse(Buffer.concat(parts).toString("utf8")) as Record + if (index === 1 && request.method === "POST" && request.url === "/rest/tool/bandwidth-test") { + counts.bandwidth++; nativeBodies.push(body) + if (mode === "drop") { response.destroy(); return } + response.end(JSON.stringify([{ status: "done testing", "tx-total-average": "10000000", "rx-total-average": "9000000" }])); return + } + if (index === 1 && request.method === "POST" && request.url === "/rest/tool/ping") { + counts.ping++; assert.equal(body.address, "198.51.100.2") + response.end(JSON.stringify([{ sent: "4", received: "4", "packet-loss": "0", "avg-rtt": "1ms" }])); return + } + fixtureFailed = true; response.statusCode = 404; response.end("{}") + })().catch(() => { fixtureFailed = true; response.destroy() }) + })) + let app: Awaited> | undefined + const history = async () => Number((await pool.query("SELECT count(*)::int AS n FROM uptime_speed_test_runs")).rows[0].n) + const wait = async (id: string) => { + const end = performance.now() + 20000 + for (;;) { + const row = (await pool.query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + assert(row); if (!["pending", "running"].includes(row.state)) return row + assert(performance.now() < end, "Owned speed operation did not finish; never replay"); await delay(25) + } + } + try { + await applySqlMigrations(pool); assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0, "Owned empty database required") + for (const server of responders) await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const ids: number[] = [] + for (const [index, server] of responders.entries()) { + const row = (await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) VALUES($1,'127.0.0.1',$2,false,false,$3,$4,true) RETURNING id", ["owned-simple-speed-" + index, (server.address() as { port: number }).port, "owned-" + (index + 1), index === 0 ? "owned-source-password" : secret])).rows[0] + ids.push(Number(row.id)) + } + await updateMcpSettings({ enabled: true }, owner) + app = await buildApp({ startScheduler: false }); await app.ready() + const issue = async (serverIds: number[]) => authenticateMcpToken((await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned ordinary speed", mode: "approval", + expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:uptime:write"], scope: { serverIds, operationIds: ["uptime.speed.test"], allowGlobal: false }, + limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner)).token) + const principal = await issue(ids) + const command = { srcServerId: ids[0], dstServerId: ids[1], srcInterface: "ether-1", dstInterface: "ether-2", protocol: "tcp", direction: "both", durationSec: 5, maxMbps: 10 } + const plan = () => invokeMcpTool(principal, "mm_speed_test_plan", command) as Promise<{ planId: string; state: string }> + console.log("Owned simple speed stage:", "scope-and-approval") + const unapproved = await plan(); assert.equal(unapproved.state, "awaiting-approval") + await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())) + await assert.rejects(invokeMcpTool(await issue([ids[0]]), "mm_speed_test_plan", command)) + assert.equal(counts.bandwidth, 0); assert.equal(await history(), 0) + groups.push("public MCP destination scope and human approval deny before native POST") + + console.log("Owned simple speed stage:", "approved-public-speed") + const approved = await plan(); await approvePlan(approved.planId, owner, true) + const key = randomUUID(), operation = await applyPlan(principal, approved.planId, key) + assert.equal((await wait(operation.id)).state, "applied") + assert.equal(counts.bandwidth, 1); assert.equal(counts.ping, 1); assert.equal(await history(), 1) + const body = nativeBodies[0] + assert.equal(body.address, "198.51.100.2"); assert.equal(body.duration, "5s") + assert.equal(body["local-tx-speed"], "10000000"); assert.equal(body["remote-tx-speed"], "10000000") + assert.equal(body.protocol, "tcp"); assert.equal(body.direction, "both"); assert(body.user === "owned-2" && body.password === secret) + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].result + assert.equal(receipt.completed, true); assert.equal(receipt.txAvgMbps, 10); assert.equal(receipt.rxAvgMbps, 9) + assert.equal(receipt.afterBtPing.rttMs, 1); assert.equal(receipt.afterBtPing.lossPct, 0) + const serialized = JSON.stringify(receipt); assert(Buffer.byteLength(serialized) < 16384) + assert(!serialized.includes(secret) && !serialized.includes("owned-2") && !serialized.includes('"raw"')) + assert.equal((await pool.query("SELECT status FROM uptime_speed_test_runs WHERE id=$1", [receipt.runId])).rows[0].status, "done") + groups.push("approved public MCP action sends one ordinary capped RouterOS POST, after-ping and one standard history row with bounded receipt") + assert.equal((await applyPlan(principal, approved.planId, key)).id, operation.id) + assert.equal((await applyPlan(principal, approved.planId, randomUUID())).id, operation.id) + assert.equal(counts.bandwidth, 1); assert.equal(await history(), 1) + groups.push("same approved plan returns original journal operation without a second native POST") + + console.log("Owned simple speed stage:", "native-drop-unknown") + mode = "drop" + const uncertain = await plan(); await approvePlan(uncertain.planId, owner, true) + const uncertainKey = randomUUID(), unknown = await applyPlan(principal, uncertain.planId, uncertainKey) + assert.equal((await wait(unknown.id)).state, "unknown") + assert.equal(counts.bandwidth, 2); assert.equal(counts.ping, 1); assert.equal(await history(), 1) + assert.equal((await applyPlan(principal, uncertain.planId, uncertainKey)).id, unknown.id) + assert.equal((await applyPlan(principal, uncertain.planId, randomUUID())).id, unknown.id) + assert.equal(counts.bandwidth, 2) + groups.push("actual native connection loss stays UNKNOWN with no automatic POST replay or fabricated success history") + + console.log("Owned simple speed stage:", "ordinary-rest") + mode = "success" + const token = await new SignJWT({ sub: owner.id, apps: owner.apps, permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }) + .setIssuedAt(Math.floor(Date.now() / 1000) - 1).setExpirationTime("1h").setIssuer(process.env.AUTH_ISSUER!).sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET)) + const headers = { authorization: "Bearer " + token } + assert.equal((await app.inject({ method: "POST", url: "/api/uptime/speed-test", headers, payload: { ...command, dstServerId: 999999 } })).statusCode, 404) + assert.equal(counts.bandwidth, 2); assert.equal(await history(), 1) + const rest = await app.inject({ method: "POST", url: "/api/uptime/speed-test", headers, payload: command }) + assert.equal(rest.statusCode, 200); assert.equal(rest.json().ok, true) + assert.equal(nativeBodies[2]["local-tx-speed"],"10000000");assert.equal(nativeBodies[2]["remote-tx-speed"],"10000000") + assert.equal(counts.bandwidth, 3); assert.equal(counts.ping, 2); assert.equal(await history(), 2) + groups.push("ordinary signed-JWT REST missing endpoint returns 404; valid request preserves full speed/ping/history workflow") + assert.equal(fixtureFailed, false) + console.log("Owned simple speed accepted:", JSON.stringify({ groups: groups.length, checks: groups, nativePosts: counts.bandwidth, afterPingPosts: counts.ping, historyRows: await history(), registryOrQRequired: false, realChr: false })) + } finally { + await pool.query("UPDATE servers SET enabled=false WHERE name LIKE 'owned-simple-speed-%'").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close() + for (const server of responders) { server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) } + await closePool() + } +} +void main().catch(() => { console.error("Owned simple speed acceptance failed; no real RouterOS or deployment action"); process.exitCode = 1 }) diff --git a/backend/src/modules/mcp/speed-bandwidth-original.sql-helper.ts b/backend/src/modules/mcp/speed-bandwidth-original.sql-helper.ts new file mode 100644 index 00000000..b37d264a --- /dev/null +++ b/backend/src/modules/mcp/speed-bandwidth-original.sql-helper.ts @@ -0,0 +1,287 @@ +/** Actual owned application/control SQL + localhost HTTP + private MCP origin, + * host receipt and workflow. Trusted verified-change COMPONENT entry only: + * uptime.speed-test has deliberately NOT been registered as public action/service. + * Enrollment quiescence and wire mapping remain explicitly synthetic fixtures. */ +import assert from "node:assert/strict" +import { randomBytes, randomUUID, createHash } from "node:crypto" +import { ClientRequest, createServer } from "node:http" +import { writeFile } from "node:fs/promises" +import { join } from "node:path" +import { performance } from "node:perf_hooks" +import { assertOwnedBandwidthSql } from "../../services/bandwidth-admission-process.sql-helper.js" +import type { VerifiedMcpServiceMetadata } from "../../services/config-mutation-policy.js" +import type { McpSpeedBandwidthInput } from "./speed-bandwidth-original.js" + +export async function acceptOwnedMcpSpeedWorkflow() { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_MCP_SPEED_ORIGINAL, "true") + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + const directory = process.env.MCP_STATE_DIR! + await writeFile(join(directory, "encryption-key"), randomBytes(32), { flag: "wx", mode: 0o600 }) + await writeFile(join(directory, "access-generation"), randomBytes(32), { flag: "wx", mode: 0o600 }) + const { createOwnedExternalRegistryHarness } = await import("../../services/bandwidth-external-node-registry.sql-helper.js") + const { pool } = await import("../../db/index.js") + const { Client: PgClient } = await import("pg") + const { speedTestCommandSchema, mcpConnectionCreateSchema } = await import("@mmapp/contracts") + const { hasPermission } = await import("../../lib/permissions.js") + const { createMcpConnection, authenticateMcpToken, effectiveMcpConnection } = await import("./store.js") + const { readMcpTemporaryGrantOrigin, assertOriginalMcpTemporaryGrant } = await import("./temporary-grant-origin.js") + const { withVerifiedConfigurationChange } = await import("../../services/config-mutation-policy.js") + const { accessGeneration, encryptSecret, decryptSecret, fingerprint, privateFingerprinterExisting, redact } = await import("./security.js") + const { createMcpSpeedBandwidthOriginalProvider } = await import("./speed-bandwidth-original.js") + const { findMcpService } = await import("./service-registry.js") + const { findActionByOperation } = await import("./actions.js") + assert.equal(findMcpService("uptime.speed-test"), undefined) + assert.throws(() => findActionByOperation("uptime.speed-test")) + const { createBandwidthPeerObservationReader } = await import("../../services/bandwidth-peer-observation.js") + const { createBandwidthPeerExecutionHost, readBandwidthPeerCompletion } = await import("../../services/bandwidth-peer-execution-host.js") + const { createBandwidthSpeedWorkflow } = await import("../../services/bandwidth-speed-workflow.js") + const { createBandwidthSpeedRunIdentity } = await import("../../services/bandwidth-speed-run-identity.js") + const f = await createOwnedExternalRegistryHarness(), application = await pool.connect().catch(async error => { await f.close(); throw error }) + const provider = createMcpSpeedBandwidthOriginalProvider(), query = pool.query, clientQuery = PgClient.prototype.query, setTimeoutOriginal = ClientRequest.prototype.setTimeout + const ids: number[] = [], users: string[] = [], receipts: Array<{ canonicalNodeId: string }> = [], checks: string[] = [], metrics: Record[] = [] + const probeId = "owned-mcp-speed-probe-" + randomUUID(), generation = await accessGeneration(), sign = await privateFingerprinterExisting("routing-observation-source-v1") + const claims = [0, 1].map(index => ({ "system-id": "OWNED-MCP-FULL-SPEED-" + index + "-" + randomUUID(), "software-id": "OWNED-MCP-FULL-SPEED-" + index })) + let nativeGets = 0, nativePosts = 0, pingPosts = 0, protocolErrors = 0, credentialQueries = 0, credentialPayloads = 0, authorizerChecks = 0 + let phase = "construction", mode = "positive", expectedBody: unknown, nativeBody: unknown, lastNativeFinished = 0, pingDelay = 0 + let beforeResponse: (() => Promise) | undefined, beforeCredentials: (() => Promise) | undefined + let rawRows: Array> = [], completionReads = 0 + type Latency = { completed: number; errors: number; totalMs: number; maxMs: number } + type Measurement = { began: number; appSql: number; controlSql: number; authorizersAtStart: number; families: Record; latency: Record; failures: Record[] } + let measurement: Measurement = { began: performance.now(), appSql: 0, controlSql: 0, authorizersAtStart: 0, families: {}, latency: {}, failures: [] } + const beginMeasurement = () => { measurement = { began: performance.now(), appSql: 0, controlSql: 0, authorizersAtStart: authorizerChecks, families: {}, latency: {}, failures: [] } } + const latencyReport = () => Object.fromEntries(Object.entries(measurement.latency).map(([family, value]) => [family, { + completed: value.completed, errors: value.errors, totalMs: Math.round(value.totalMs * 1000) / 1000, maxMs: Math.round(value.maxMs * 1000) / 1000, + }])) + const sqlFamily = (text: string) => text.startsWith("/* mcp-speed-original:") ? /^\/\* mcp-speed-original:([a-z-]+)/.exec(text)?.[1] ?? "speed-source" : text.includes("FROM public.mcp_operations") ? "sealed-origin" + : text.includes("mcp_credentials") ? "credential-current" : text.includes("mcp_connections") ? "connection-current" : text.includes("mcp_grants") ? "grant-current" + : text.includes("mcp_owner_revocations") ? "portal-current" : text.includes("mcp_settings") ? "settings-current" : "other" + function failure(label: string, error: unknown, scope?: { deadlineAt: number; signal?: AbortSignal }) { + if (measurement.failures.length >= 12) return + const value = error as { name?: unknown; code?: unknown; phase?: unknown } + measurement.failures.push({ boundary: label, elapsedMs: Math.round(performance.now() - measurement.began), + remainingMs: scope ? Math.round(scope.deadlineAt - Date.now()) : undefined, aborted: scope?.signal?.aborted, + name: typeof value?.name === "string" && /^[A-Za-z]{1,80}$/.test(value.name) ? value.name : undefined, + code: typeof value?.code === "string" && /^[A-Z_0-9]{1,80}$/.test(value.code) ? value.code : undefined, + phase: typeof value?.phase === "string" && /^[a-z-]{1,80}$/.test(value.phase) ? value.phase : undefined }) + } + async function observe(label: string, scope: { deadlineAt: number; signal?: AbortSignal }, work: () => Promise): Promise { + try { return await work() } catch (error) { failure(label, error, scope); throw error } + } + const timeouts: Array<{ path: string; timeout: number }> = [] + const routers = claims.map((license, index) => createServer(async (request, response) => { + try { + assert.equal(request.socket.remoteAddress, "127.0.0.1") + assert.equal(request.headers.authorization === "Basic " + Buffer.from(users[index] + ":owned-localhost-only").toString("base64"), true) + response.setHeader("content-type", "application/json") + if (request.method === "GET" && request.url === "/rest/system/license") { nativeGets++; response.end(JSON.stringify(license)); return } + if (request.method === "GET" && request.url === "/rest/ip/address") { + nativeGets++; response.end(JSON.stringify(index === 0 ? [{ interface: "source-if", address: "198.51.100.1/30" }] : [ + { interface: "dest-if", address: "198.51.100.99/30", dynamic: "true" }, { interface: "dest-if", address: "198.51.100.77/30", disabled: "true" }, + { interface: "dest-if", address: "198.51.100.66/30", invalid: "true" }, { interface: "dest-if", address: "198.51.100.2/30" }, + ])); return + } + assert.equal(request.method, "POST"); assert.equal(index, 0) + let text = ""; for await (const chunk of request) { text += String(chunk); assert(Buffer.byteLength(text) <= 65536) } + const body = JSON.parse(text) + if (request.url === "/rest/tool/bandwidth-test") { + nativePosts++; nativeBody = body; assert.deepEqual(body, expectedBody); await beforeResponse?.() + rawRows = [{ status: "testing", "tx-current": "1000000", retained: "owned-sample" }, { status: "done testing", "tx-total-average": "3000000", "rx-total-average": "4M", retained: "owned-terminal" }] + lastNativeFinished = performance.now(); response.end(JSON.stringify(rawRows)); return + } + assert.equal(request.url, "/rest/tool/ping"); pingPosts++ + assert.deepEqual(body, { address: "198.51.100.2", count: "4", interface: "source-if", interval: "1s" }) + pingDelay = performance.now() - lastNativeFinished; assert(pingDelay >= 2950) + response.end(JSON.stringify([{ sent: "4", received: "3", "packet-loss": "25%", "avg-rtt": "2ms" }])) + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned MCP speed HTTP protocol mismatch"}') } + })) + const restoreProbe = async () => { + await application.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) + await application.query("INSERT INTO public.uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,dst_interface,protocol,direction,duration_sec,enabled) VALUES($1,$2,$3,'source-if','dest-if','udp','receive',3,true)", [probeId, ids[0], ids[1]]) + } + const historyCounts = async () => (await application.query("SELECT (SELECT count(*)::int FROM public.uptime_speed_test_runs) AS speed,(SELECT count(*)::int FROM public.scheduler_runs) AS scheduler,(SELECT count(*)::int FROM public.server_probe_executions) AS diagnostic")).rows[0] + try { + for (const claim of claims) ids.push(await f.managedEndpoint(claim)) + users.push(...ids.map(id => "managed-" + id)) + for (const id of ids) receipts.push(await f.managedHost.register(await f.managedObservation(id), f.scope())) + assert.notEqual(receipts[0].canonicalNodeId, receipts[1].canonicalNodeId) + for (let index = 0; index < routers.length; index++) { + await new Promise((yes, no) => { routers[index].once("error", no); routers[index].listen(0, "127.0.0.1", yes) }) + await application.query("UPDATE public.servers SET port=$2 WHERE id=$1", [ids[index], (routers[index].address() as { port: number }).port]) + } + await application.query("UPDATE public.mcp_settings SET enabled=true WHERE id=1") + await restoreProbe(); const signed = await f.createSignedSource() + // Physical driver counts include callback-based pg-pool calls. All original + // SQL/config/params/result objects and ACKs are forwarded unchanged. + PgClient.prototype.query = function(this: InstanceType, ...args: unknown[]) { + const connection = this as unknown as { connectionParameters: { database: string; user: string } } + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "" + const core = connection.connectionParameters.database === f.databaseName + if (core) measurement.controlSql++; else if (connection.connectionParameters.database === "postgres" && connection.connectionParameters.user === "mcp_test") measurement.appSql++ + const family = (core ? "control:" : "application:") + sqlFamily(text); measurement.families[family] = (measurement.families[family] ?? 0) + 1 + const measured = measurement, began = performance.now(); let completed = false + // Observe the real driver's completion only. No SQL text, parameters, + // result rows or credentials enter these fixed-family numeric aggregates. + // Capture the measurement object so a late ACK cannot pollute a new case. + const finish = (error?: unknown) => { + if (completed) return; completed = true + const elapsed = performance.now() - began + const value = measured.latency[family] ??= { completed: 0, errors: 0, totalMs: 0, maxMs: 0 } + value.completed++; if (error) value.errors++; value.totalMs += elapsed; value.maxMs = Math.max(value.maxMs, elapsed) + } + const callback = args.at(-1) + if (typeof callback === "function") args[args.length - 1] = function(this: unknown, error: unknown, result: unknown) { + finish(error) + if (error) failure(family, error) + return Reflect.apply(callback, this, [error, result]) + } + let result: unknown + try { result = Reflect.apply(clientQuery, this, args) } catch (error) { finish(error); failure(family, error); throw error } + if (!result || typeof (result as Promise).then !== "function") return result + return (result as Promise).then(value => { finish(); return value }, error => { finish(error); failure(family, error); throw error }) + } as typeof PgClient.prototype.query + pool.query = function(this: typeof pool, ...args: unknown[]) { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "" + const credentials = text.startsWith("/* mcp-speed-original:credentials */") + return (async () => { + if (credentials) { credentialQueries++; const hook = beforeCredentials; beforeCredentials = undefined; await hook?.() } + let result + try { result = await Reflect.apply(query, this, args) } catch (error) { failure("pool:" + sqlFamily(text), error); throw error } + if (credentials) credentialPayloads += result.rows.length + return result // Original driver result/rows/ACK, never substituted. + })() + } as typeof pool.query + ClientRequest.prototype.setTimeout = function(this: ClientRequest, ...args: Parameters) { + if (this.path === "/rest/tool/bandwidth-test" || this.path === "/rest/tool/ping") timeouts.push({ path: this.path, timeout: args[0] }) + return Reflect.apply(setTimeoutOriginal, this, args) + } + async function prepare(approved: boolean) { + phase = "prepare-actual-origin" + const runId = "owned-mcp-full-speed-" + randomUUID(), command = speedTestCommandSchema.parse({ runId, probeId, srcServerId: ids[0], dstServerId: ids[1], srcInterface: "source-if", dstInterface: "dest-if", protocol: "udp", direction: "receive", durationSec: 3 }) + const endpointRows = (await application.query('SELECT id,host,port,username,password,use_ssl AS "useSsl",verify_ssl AS "verifySsl",enabled,routing_endpoint_epoch::text AS "routingEndpointEpoch" FROM public.servers WHERE id=ANY($1::bigint[]) ORDER BY id', [ids])).rows + const probe = (await application.query('SELECT id,src_server_id AS "srcServerId",dst_server_id AS "dstServerId",src_interface AS "srcInterface",dst_interface AS "dstInterface",protocol,direction,duration_sec AS "durationSec",enabled FROM public.uptime_speed_probes WHERE id=$1', [probeId])).rows[0] + const input = { command, expectedEndpoints: endpointRows.map(endpoint => ({ serverId: endpoint.id, proof: sign("endpoint:" + fingerprint({ generation, endpoint })) })), expectedProbe: probe } as McpSpeedBandwidthInput + const actor = { id: randomUUID(), email: "owned-mcp-speed@example.test", name: "Owned MCP speed component", apps: ["mm"], permissions: ["mm:uptime:write", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 300000 } + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned MCP speed component", mode: approved ? "approval" : "autonomous", expiresAt: new Date(actor.expiresAt).toISOString(), permissions: ["mm:uptime:write"], + scope: { serverIds: ids, operationIds: ["uptime.speed-test"], allowGlobal: false }, ...(approved ? {} : { dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["uptime.speed-test"] }) }), actor) + const principal = await authenticateMcpToken(access.token) + let grantId: string | undefined + if (approved) { grantId = randomUUID(); await application.query("INSERT INTO public.mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grantId, principal.connectionId, JSON.stringify({ credentialId: principal.credentialId, id: randomUUID(), startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 120000).toISOString(), scope: { serverIds: ids, operationIds: ["uptime.speed-test"], allowGlobal: false } })]) } + const originalGrant = await readMcpTemporaryGrantOrigin(pool, principal.connectionId, principal.credentialId) + const expiry = Math.min(Date.now() + 120000, Date.parse(principal.credentialExpiresAt), originalGrant.expiresAt ?? Infinity), began = Date.now(), mono = performance.now() + const grant = { ...originalGrant, monotonicEnd: originalGrant.expiresAt === null ? Infinity : mono + originalGrant.expiresAt - began, admissionExpiresAt: expiry, admissionMonotonicEnd: mono + expiry - began } + const planId = randomUUID(), operationId = randomUUID(), objects = ids.map(serverId => ({ kind: "server", id: String(serverId), serverId })) + const step = { kind: "service", service: "uptime.speed-test", input }, payload = { sealed: await encryptSecret(JSON.stringify({ command })) }, evidence = { sealed: await encryptSecret(JSON.stringify({ objects, steps: [step] })) } + await application.query("INSERT INTO public.mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by,approved_at) VALUES($1,$2,'uptime.speed-test',1,1,$3,$4::jsonb,$5::jsonb,'executing',$6,$7,$8)", [planId, principal.connectionId, generation, JSON.stringify(payload), JSON.stringify(evidence), new Date(expiry), approved ? actor.id : null, approved ? new Date() : null]) + await application.query("INSERT INTO public.mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'running')", [operationId, planId, principal.connectionId, randomUUID()]) + await application.query("INSERT INTO public.mcp_operation_steps(operation_id,step_index,server_id,state,intended_state) VALUES($1,0,NULL,'intent',$2::jsonb)", [operationId, JSON.stringify(redact(step))]) + const row = (await application.query("SELECT * FROM public.mcp_plans WHERE id=$1", [planId])).rows[0] + const decrypted = JSON.parse(await decryptSecret(row.evidence.sealed)); assert.deepEqual(decrypted.steps[0].input, input) + assert.deepEqual(JSON.parse(await decryptSecret(row.payload.sealed)), { command }) + const metadata: VerifiedMcpServiceMetadata = { operationId, planId, operationName: row.operation_id, serviceName: decrypted.steps[0].service, operationVersion: row.operation_version, policyVersion: row.policy_version, + generation: row.generation, principal, stepIndex: 0, phase: "forward", originalExpiresAt: row.expires_at.getTime(), originalTemporaryGrantProof: originalGrant.proof, originalTemporaryGrantExpiresAt: originalGrant.expiresAt, + sealedEvidenceProof: createHash("sha256").update(row.evidence.sealed).digest("hex"), sealedPayloadProof: createHash("sha256").update(row.payload.sealed).digest("hex"), serviceInput: decrypted.steps[0].input } + const context = { actorId: actor.id, connectionId: principal.connectionId, serverIds: ids, objects, approved: !!row.approved_by, serverWide: true, serviceExecution: metadata, + authorize: async () => { + authorizerChecks++; const current = await effectiveMcpConnection(principal) + assert.equal(current.owner_id, actor.id); assert.equal(current.policy.version, metadata.policyVersion) + assert(hasPermission(current.owner_permissions, "mm:uptime:write") && hasPermission(current.policy.permissions, "mm:uptime:write")) + assert(ids.every(id => current.policy.scope.serverIds.includes(id))); assert(current.policy.scope.operationIds.includes("uptime.speed-test")) + if (!approved) assert(current.policy.mode === "autonomous" && current.policy.dangerousPolicy === "explicit-allow" && current.policy.autonomousDangerousOperationIds.includes("uptime.speed-test")) + await assertOriginalMcpTemporaryGrant(pool, principal.connectionId, principal.credentialId, grant) + } } + return { input, command, context, metadata, grantId, principal } + } + const compose = (token: unknown) => { + const observer = createBandwidthPeerObservationReader({ read: (peer, current) => observe("observer-original", current, () => provider.readPeer(token, peer, current)) }) + const host = createBandwidthPeerExecutionHost({ coreSource: signed.core, nodeSource: signed.node, observer, readOriginal: (original, current) => observe("host-original", current, () => provider.readOriginal(original, current)), + verifyRemote: async (original, intent, evidence, current) => { + assert.equal(original, token); assert.deepEqual(intent.actualRemotePeer, { kind: "server", serverId: ids[1] }) + assert.equal(evidence.sourceReceipt.canonicalNodeId, receipts[0].canonicalNodeId); assert.equal(evidence.remoteReceipt.canonicalNodeId, receipts[1].canonicalNodeId) + assert.equal(intent.nativeBody.address, (await observe("wire-context", current, () => provider.readSpeedContext(token, current))).address) + return { proof: f.mac("SYNTHETIC-OWNED-MCP-WIRE-MAPPING", { source: receipts[0].canonicalNodeId, remote: receipts[1].canonicalNodeId, address: intent.nativeBody.address, credentialServerId: intent.credentialServerId }), expiresAt: intent.expiresAt } + } }) + return { host, observer } + } + for (const next of [{ name: "human", approved: true }, { name: "autonomous-probe-edit", approved: false }, { name: "human-probe-delete", approved: true }]) { + mode = next.name; await restoreProbe(); const owned = await prepare(next.approved), before = { nativePosts, pingPosts }, began = performance.now() + beginMeasurement() + expectedBody = { address: "198.51.100.2", user: users[1], password: "owned-localhost-only", protocol: "udp", direction: "receive", duration: "3s" } + beforeResponse = next.name.includes("edit") ? async () => { await application.query("UPDATE public.uptime_speed_probes SET duration_sec=4 WHERE id=$1", [probeId]) } + : next.name.includes("delete") ? async () => { await application.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) } : undefined + await withVerifiedConfigurationChange(owned.context, async () => { + const scope = { deadlineAt: Date.now() + 60000 }; phase = "capture"; const token = await provider.capture(owned.input, scope) + phase = "compose"; const { host, observer } = compose(token) + try { + const beforeForgery = credentialPayloads + await assert.rejects(provider.readPeerAfterCompletion(token, { outcome: "complete" }, { kind: "server", serverId: ids[0] }, scope)); assert.equal(credentialPayloads, beforeForgery) + phase = "workflow"; const result = await createBandwidthSpeedWorkflow({ originals: provider, peerHost: host }).run(token, scope) + assert.deepEqual(result.raw, rawRows); assert.equal(result.txAvgMbps, 3); assert.equal(result.rxAvgMbps, 4) + assert.equal(result.afterBtPing.rttMs, 2); assert.equal(result.afterBtPing.lossPct, 25); assert.equal(result.afterBtPing.error, null) + } finally { observer.close() } + }) + assert.equal(nativePosts, before.nativePosts + 1); assert.equal(pingPosts, before.pingPosts + 1); assert(pingDelay >= 2950); assert.deepEqual(nativeBody, expectedBody) + const history = (await application.query("SELECT status,probe_id,tx_avg_mbps,rx_avg_mbps,ping_rtt_ms,ping_loss_pct FROM public.uptime_speed_test_runs WHERE id=$1", [owned.command.runId])).rows + assert.equal(history.length, 1); assert.equal(history[0].status, "done"); assert.equal(history[0].probe_id, next.name === "human" ? probeId : null); assert.equal(Number(history[0].tx_avg_mbps), 3); assert.equal(history[0].ping_rtt_ms, 2) + const identity = createBandwidthSpeedRunIdentity(owned.command.runId), attempt = (await f.owner.query("SELECT caller_kind,state,resolution FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [identity.attemptId])).rows + assert.equal(attempt.length, 1); assert.equal(attempt[0].caller_kind, "mcp-speed"); assert.equal(attempt[0].state, "released"); assert.equal(attempt[0].resolution, "remote_completion") + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy")).rows[0].n), 0) + metrics.push({ case: mode, approved: next.approved, elapsedMs: Math.round(performance.now() - began), nativePosts: nativePosts - before.nativePosts, pingPosts: pingPosts - before.pingPosts, historyRows: history.length, + applicationSql: measurement.appSql, controlSql: measurement.controlSql, authorizerChecks: authorizerChecks - measurement.authorizersAtStart, sqlFamilies: measurement.families, sqlLatency: latencyReport() }) + checks.push(mode + ": real sealed/current MCP component + actual v2 private host completion + native HTTP/3-second pause/ping/history; probe drift only clears association") + } + const historyAfterWorkflows = await historyCounts() + assert.equal(historyAfterWorkflows.speed, 3) + mode = "exact-private-receipt"; beforeResponse = undefined; await restoreProbe(); const receiptCase = await prepare(false) + beginMeasurement() + expectedBody = { address: "198.51.100.2", user: users[1], password: "owned-localhost-only", protocol: "udp", direction: "receive", duration: "3s" } + await withVerifiedConfigurationChange(receiptCase.context, async () => { + const scope = { deadlineAt: Date.now() + 60000 }, token = await provider.capture(receiptCase.input, scope), { host, observer } = compose(token) + try { + phase = "exact-host-receipt"; const receipt = await host.execute(token, scope) + assert.deepEqual(readBandwidthPeerCompletion(receipt, token), await provider.readOriginal(token, scope)) + await application.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) + await assert.rejects(provider.readOriginal(token, scope)) + assert.equal((await provider.readPeerAfterCompletion(token, receipt, { kind: "server", serverId: ids[0] }, scope)).endpoint.username, users[0]); completionReads++ + const before = credentialPayloads + for (const copied of [{ ...receipt }, JSON.parse(JSON.stringify(receipt))]) await assert.rejects(provider.readPeerAfterCompletion(token, copied, { kind: "server", serverId: ids[0] }, scope)) + await assert.rejects(provider.readPeerAfterCompletion({ ...token }, receipt, { kind: "server", serverId: ids[0] }, scope)); assert.equal(credentialPayloads, before) + } finally { observer.close() } + }) + checks.push("actual exact private native+durable completion receipt permits probe-deleted post-completion source; copying/serializing receipt or changing private original refuses") + mode = "initial-probe-drift"; await restoreProbe(); const initial = await prepare(false), initialCounts = { credentialPayloads, nativeGets, nativePosts, pingPosts } + beginMeasurement() + beforeCredentials = async () => { await application.query("UPDATE public.uptime_speed_probes SET direction='both' WHERE id=$1", [probeId]) } + await withVerifiedConfigurationChange(initial.context, async () => { await assert.rejects(provider.capture(initial.input, { deadlineAt: Date.now() + 30000 })) }) + assert.deepEqual({ credentialPayloads, nativeGets, nativePosts, pingPosts }, initialCounts) + checks.push("actual probe UPDATE after initial probe SELECT but at credential command returns zero projected payload before GET/POST") + for (const fault of ["grant", "owner-permission", "sealed-step"] as const) { + mode = fault; await restoreProbe(); const owned = await prepare(true) + beginMeasurement() + await withVerifiedConfigurationChange(owned.context, async () => { + const scope = { deadlineAt: Date.now() + 30000 }, token = await provider.capture(owned.input, scope), before = { credentialPayloads, nativeGets, nativePosts, pingPosts } + if (fault === "grant") { assert(owned.grantId); await application.query("UPDATE public.mcp_grants SET revoked_at=now() WHERE id=$1", [owned.grantId]) } + if (fault === "owner-permission") await application.query("UPDATE public.mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [owned.principal.connectionId]) + if (fault === "sealed-step") await application.query("UPDATE public.mcp_operation_steps SET intended_state='{}'::jsonb WHERE operation_id=$1 AND step_index=0", [owned.metadata.operationId]) + await assert.rejects(provider.readOriginal(token, scope)); assert.deepEqual({ credentialPayloads, nativeGets, nativePosts, pingPosts }, before) + }) + } + checks.push("retained genuine component revalidates actual original grant revocation, owner permission loss and current sealed-step SQL drift before further credentials/native") + assert.deepEqual(await historyCounts(), historyAfterWorkflows) + assert.equal(protocolErrors, 0); assert(timeouts.some(value => value.path === "/rest/tool/bandwidth-test" && value.timeout === 30000)); assert(timeouts.some(value => value.path === "/rest/tool/ping" && value.timeout === 20000)) + return { checks, metrics, nativeGets, nativePosts, pingPosts, credentialQueries, credentialPayloadRows: credentialPayloads, authorizerChecks, exactPositiveCompletionReads: completionReads, + actualApplicationAndControlSql: true, actualSealedPayloads: true, actualMcpConnectionTokenGrantPortalAuthorizer: true, genuineVerifiedChangeComponent: true, + actualPrivateCompletionReceipt: true, actualWorkflowHistory: true, publicMcpActionRegistered: false, publicMcpRunnerAcceptance: false, + syntheticEnrollmentQuiescence: true, syntheticWireMapping: true, productionMappingVerified: false, runtimeAllCallersActivated: false, realChrCalls: 0, automaticReplay: false } + } catch (error) { + const cause = error instanceof Error && error.cause ? error.cause : error + console.error("Owned MCP speed phase:", JSON.stringify({ phase, mode, nativeGets, nativePosts, pingPosts, credentialQueries, credentialPayloads, authorizerChecks, + elapsedMs: Math.round(performance.now() - measurement.began), applicationSql: measurement.appSql, controlSql: measurement.controlSql, sqlFamilies: measurement.families, sqlLatency: latencyReport(), underlyingFailures: measurement.failures, + name: cause instanceof Error ? cause.name : "unclassified", code: cause && typeof cause === "object" && "code" in cause ? String(cause.code).slice(0, 80) : undefined, + peerPhase: cause && typeof cause === "object" && "phase" in cause && typeof cause.phase === "string" && /^[a-z-]{1,80}$/.test(cause.phase) ? cause.phase : undefined })) + throw error + } finally { + beforeCredentials = undefined; pool.query = query; PgClient.prototype.query = clientQuery; ClientRequest.prototype.setTimeout = setTimeoutOriginal + for (const router of routers) { router.closeAllConnections(); if (router.listening) await new Promise(yes => router.close(() => yes())) } + application.release(true); await f.close() + } +} diff --git a/backend/src/modules/mcp/speed-bandwidth-original.sql.test.ts b/backend/src/modules/mcp/speed-bandwidth-original.sql.test.ts new file mode 100644 index 00000000..ec40a9fc --- /dev/null +++ b/backend/src/modules/mcp/speed-bandwidth-original.sql.test.ts @@ -0,0 +1,15 @@ +/** Exclusive owned component acceptance. No public MCP action registration. */ +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState } from "../../services/bandwidth-admission-process.sql-helper.js" +async function main() { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_MCP_SPEED_ORIGINAL, "true") + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + await prepareOwnedBandwidthState() + const { acceptOwnedMcpSpeedWorkflow } = await import("./speed-bandwidth-original.sql-helper.js") + console.log("Owned genuine MCP full speed component:", JSON.stringify(await acceptOwnedMcpSpeedWorkflow())) +} +void main().catch(error => { + // Assertions may contain owned private credentials. Emit sanitized markers. + console.error("Owned MCP speed component acceptance failed", { name: error?.name, code: error?.code }) + process.exitCode = 1 +}) diff --git a/backend/src/modules/mcp/speed-bandwidth-original.test.ts b/backend/src/modules/mcp/speed-bandwidth-original.test.ts new file mode 100644 index 00000000..9aca3ff9 --- /dev/null +++ b/backend/src/modules/mcp/speed-bandwidth-original.test.ts @@ -0,0 +1,409 @@ +/** Actual verified-change ALS, private MCP tokens, current-origin reader, + * validatePrincipal/effective grant and existing key files. SQL/native boundaries + * alone are synthetic. No module/realm/equality/origin mocks or service activation. */ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createHash, randomUUID } from "node:crypto" +import { mkdir, mkdtemp, realpath, writeFile, readFile, unlink, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, dirname, basename } from "node:path" +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { unlinkSync, writeFileSync } from "node:fs" +import type { ConnectionRow } from "./store.js" +import { principalSourceRowsForTests } from "../../test/mcp-principal-source-rows.js" +import type { VerifiedMcpServiceMetadata } from "../../services/config-mutation-policy.js" +import { mcpTemporaryGrantSchema } from "@mmapp/contracts/mcp" + +test("full MCP speed original with genuine verifier/current authorizer and synthetic SQL/native boundaries", async t => { + process.env.NODE_ENV = "test" + const parent = await realpath(tmpdir()), directory = await mkdtemp(join(parent, "mm-mcp-speed-original-")), previous = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = directory + const { pool, closePool } = await import("../../db/index.js") + const { effectiveMcpConnection } = await import("./store.js") + const { hasPermission } = await import("../../lib/permissions.js") + const { withVerifiedConfigurationChange, captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecutionLifetime } = await import("../../services/config-mutation-policy.js") + const { createMcpSpeedBandwidthOriginalProvider, assertMcpSpeedBandwidthOriginalProvider, assertMcpSpeedBandwidthWorkflowRequest } = await import("./speed-bandwidth-original.js") + const { createBandwidthSpeedRunIdentity } = await import("../../services/bandwidth-speed-run-identity.js") + const { fingerprint, privateFingerprinterExisting, redact } = await import("./security.js") + const { MikrotikClient } = await import("../../services/mikrotik.js") + const key = Buffer.alloc(32, 63), generation = Buffer.alloc(32, 71), keyPath = join(directory, "encryption-key"), generationPath = join(directory, "access-generation") + await writeFile(keyPath, key); await writeFile(generationPath, generation) + const sign = await privateFingerprinterExisting("routing-observation-source-v1") + const actualQuery = pool.query, actualConnect = pool.connect, actualGet = MikrotikClient.prototype.get, actualPost = MikrotikClient.prototype.post + let connection: ConnectionRow, row: Record, endpoints: Array> = [], probe: Record | undefined + let credentialActive = true, mcpEnabled = true, portalRevoked = false, nativeReads = 0, credentials = 0, credentialPayloads = 0, posts = 0 + let historyIds = new Set(), nativeHook: (() => void) | undefined, sqlHook: ((text: string) => void) | undefined + let authorizerHook: (() => Promise) | undefined, grants: Array<{ id: string; payload: Record }> = [] + let pending = false, aborted = false, nativeStarted: (() => void) | undefined, sourceFails = false + let nativeOrder: number[] = [], snapshotOverride: unknown, authorizerCalls = 0, originReads = 0 + const snapshotConfigs: Array<{ name: string; text: string; values: unknown[]; query_timeout: number }> = [] + const scope = (milliseconds = 10000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + milliseconds, ...(signal ? { signal } : {}) }) + pool.connect = (() => { throw Error("No physical PostgreSQL in unit") }) as never + pool.query = (async (raw: string | { text: string; values: unknown[]; query_timeout: number; name?: string }, values?: unknown[]) => { + const text = typeof raw === "string" ? raw : raw.text, parameters = typeof raw === "string" ? values : raw.values + sqlHook?.(text) + if (text.startsWith("/* mcp-principal:coherent-source-v1 */")) { + assert.equal(parameters!.length, 3) + assert.equal(parameters![1], connection.id) + assert.equal(typeof parameters![2], "boolean") + const rows = principalSourceRowsForTests(connection, + { credentialId: String(parameters![0]), credentialExpiresAt: connection.owner_valid_until.toISOString() }, + { includeGrants: parameters![2] === true, credentialActive, enabled: mcpEnabled, + cutoff: portalRevoked ? new Date() : null, grants }) + return { command: "SELECT", rowCount: rows.length, rows } + } + if (text.startsWith("SELECT id FROM mcp_credentials")) return { rows: credentialActive ? [{ id: parameters![0] }] : [] } + if (text.startsWith("SELECT * FROM mcp_connections")) return { rows: [structuredClone(connection)] } + if (text.startsWith("SELECT invalid_before FROM mcp_owner_revocations")) return { rows: portalRevoked ? [{ invalid_before: new Date() }] : [] } + if (text.startsWith("SELECT enabled FROM mcp_settings")) return { rows: [{ enabled: mcpEnabled }] } + if (text.startsWith("SELECT payload FROM mcp_grants")) return { rows: structuredClone(grants.map(({ payload }) => ({ payload }))) } + assert(typeof raw !== "string"); assert(raw.query_timeout > 0 && raw.query_timeout <= 5000) + const combined = text.startsWith("/* mcp-speed-original:current-snapshot */") || text.startsWith("/* mcp-speed-original:completion-snapshot */") + if (combined) { + const expectedName = "mcp-speed-" + createHash("sha256").update(text).digest("hex").slice(0, 48) + assert.equal(raw.name, expectedName) + snapshotConfigs.push({ ...raw, name: raw.name! }) + } else assert.equal(raw.name, undefined) + if (!text.startsWith("/* mcp-speed-original:") && text.includes("FROM public.mcp_operations")) { originReads++; return { rows: [structuredClone(row)] } } + if (text.startsWith("/* bandwidth-speed-run:history */")) { const rows = historyIds.has(String(parameters![0])) ? [{ id: parameters![0] }] : []; return { command: "SELECT", rowCount: rows.length, rows } } + if (text.startsWith("/* mcp-speed-original:metadata */")) return { rows: endpoints.filter(endpoint => (parameters![0] as number[]).includes(Number(endpoint.id))).map(endpoint => ({ id: endpoint.id, enabled: endpoint.enabled, routingEndpointEpoch: endpoint.routingEndpointEpoch })) } + if (text.startsWith("/* mcp-speed-original:probe */")) return { rows: probe?.id === parameters![0] ? [structuredClone(probe)] : [] } + if (text.startsWith("/* mcp-speed-original:credentials */")) { + credentials++; assert(text.includes("WHERE a.complete")); assert(text.includes("budget.bytes<=1048576")) + const expected = JSON.parse(String(parameters![0])) as Array<{ id: number; epoch: string }> + const matches = expected.map(value => endpoints.find(endpoint => endpoint.id === value.id && endpoint.enabled === true && endpoint.routingEndpointEpoch === value.epoch)) + assert(text.includes("public.uptime_speed_probes")); assert(!text.includes("p.enabled=true")) + const expectedProbe = parameters![1] === null ? undefined : JSON.parse(String(parameters![1])) + const selected = !expectedProbe || !!probe && probe.id === parameters![2] && isDeepStrictEqual(probe, expectedProbe) + const rows = matches.every(Boolean) && selected ? matches.map(endpoint => ({ id: endpoint!.id, endpoint: structuredClone(endpoint), overflow: false })) : [] + credentialPayloads += rows.length; return { rows } + } + const strict = text.startsWith("/* mcp-speed-original:current-snapshot */") + assert(strict || text.startsWith("/* mcp-speed-original:completion-snapshot */")); assert.equal(text.includes("public.uptime_speed_probes"), strict) + const expected = JSON.parse(String(parameters![0])) as Array> + const pair = expected.length === 2 && new Set(expected.map(endpoint => endpoint.id)).size === 2 && expected.every(endpoint => endpoints.some(current => current.enabled === true && isDeepStrictEqual(current, endpoint))) + const expectedProbe = strict && parameters![1] !== null ? JSON.parse(String(parameters![1])) : undefined + const source = JSON.parse(String(parameters![3])) + assert(text.includes("mcp_credentials") && text.includes("mcp_owner_revocations") && text.includes("mcp_grants") && text.includes("sealed_source")) + const sealed = row.operation_state === "running" && row.plan_state === "executing" && row.step_state === "intent" && row.server_id === null + && row.operation_id === source.operationName && row.operation_version === source.operationVersion && row.policy_version === source.policyVersion + && row.generation === source.generation && row.owner_id === source.ownerId && !!row.approved_by === source.approved + && row.expires_at instanceof Date && row.expires_at.getTime() >= source.originalExpiresAt + && row.evidence_proof === source.sealedEvidenceProof && row.payload_proof === source.sealedPayloadProof && isDeepStrictEqual(row.intended_state, source.intendedState) + const current = pair && sealed && credentialActive && mcpEnabled && !portalRevoked + && (!expectedProbe || !!probe && probe.id === parameters![2] && isDeepStrictEqual(probe, expectedProbe)) + const authority = { id: connection.id, ownerId: connection.owner_id, ownerPermissions: structuredClone(connection.owner_permissions), policy: structuredClone(connection.policy), + generation: connection.generation, ownerValidUntil: connection.owner_valid_until.toISOString(), ownerClaimsIssuedAt: connection.owner_claims_issued_at.toISOString(), grants: structuredClone(grants) } + return snapshotOverride ?? { command: "SELECT", rowCount: 1, rows: [{ current, authority }] } + }) as never + MikrotikClient.prototype.get = (async function(this: InstanceType, path: string, timeout: number, signal: AbortSignal, _budget: unknown, guard: () => Promise) { + assert.equal(path, "/ip/address"); assert(timeout > 0 && timeout <= 10000); signal.throwIfAborted(); await guard() + const id = Number((this as unknown as { serverId: number }).serverId); nativeReads++; nativeOrder.push(id); nativeHook?.() + if (pending) return new Promise((_yes, no) => { signal.addEventListener("abort", () => { aborted = true; no(Error("Genuine original lifecycle cancelled native GET")) }, { once: true }); nativeStarted?.() }) + if (sourceFails && id === 101) throw Error("Synthetic source GET failure") + return id === 101 ? [{ interface: "gre-source", address: "198.51.100.1/30" }] : [ + { interface: "gre-destination", address: "198.51.100.99/30", dynamic: "true" }, { interface: "gre-destination", address: "198.51.100.77/30", disabled: "true" }, + { interface: "gre-destination", address: "198.51.100.66/30", invalid: "true" }, { interface: "gre-destination", address: "198.51.100.2/30" }, + ] + }) as never + MikrotikClient.prototype.post = (async () => { posts++; throw Error("No bandwidth or ping POST") }) as never + const provider = createMcpSpeedBandwidthOriginalProvider() + const assertProvider: (value: unknown) => void = assertMcpSpeedBandwidthOriginalProvider + function fixture(interfaces = true, selectedProbe = true, lifetimeMs = 10000) { + credentialActive = true; mcpEnabled = true; portalRevoked = false; historyIds = new Set(); nativeHook = undefined; sqlHook = undefined; authorizerHook = undefined; grants = [] + pending = false; aborted = false; sourceFails = false; nativeOrder = []; snapshotOverride = undefined + endpoints = [101, 102].map(id => ({ id, enabled: true, host: "192.0.2." + (id - 100), port: 443, useSsl: true, verifySsl: true, username: "fixture-user-" + id, password: "fixture-secret-" + id, routingEndpointEpoch: randomUUID() })) + const command = { runId: "owned-mcp-speed-" + randomUUID(), ...(selectedProbe ? { probeId: "probe-a" } : {}), srcServerId: 101, dstServerId: 102, + srcInterface: interfaces ? "gre-source" : "", dstInterface: interfaces ? "gre-destination" : "", protocol: "udp" as const, direction: "receive" as const, durationSec: 3 } + probe = selectedProbe ? { id: "probe-a", srcServerId: 101, dstServerId: 102, srcInterface: command.srcInterface, dstInterface: command.dstInterface, protocol: command.protocol, direction: command.direction, durationSec: command.durationSec, enabled: true } : undefined + const input = { command, expectedEndpoints: endpoints.map(endpoint => ({ serverId: Number(endpoint.id), proof: sign("endpoint:" + fingerprint({ generation: generation.toString("hex"), endpoint })) })), expectedProbe: probe ? structuredClone(probe) : null } + const connectionId = randomUUID(), expiry = Date.now() + lifetimeMs + const metadata: VerifiedMcpServiceMetadata = { operationId: randomUUID(), planId: randomUUID(), operationName: "uptime.speed-test", serviceName: "uptime.speed-test", operationVersion: 1, + stepIndex: 0, phase: "forward", policyVersion: 1, generation: generation.toString("hex"), originalExpiresAt: expiry, sealedEvidenceProof: "a".repeat(64), sealedPayloadProof: "b".repeat(64), originalTemporaryGrantProof: "c".repeat(64), originalTemporaryGrantExpiresAt: null, + principal: { connectionId, credentialId: randomUUID(), credentialExpiresAt: new Date(expiry + 1000).toISOString() }, serviceInput: input } + metadata.originalTemporaryGrantProof = fingerprint({ credentialId: metadata.principal.credentialId, grant: null }) + connection = { id: connectionId, name: "owned fixture", owner_id: "owned-mcp-owner", owner_permissions: ["mm:uptime:write"], generation: generation.toString("hex"), created_at: new Date(), updated_at: new Date(), last_used_at: null, + owner_valid_until: new Date(expiry + 1000), owner_claims_issued_at: new Date(Date.now() - 1000), policy: { id: connectionId, version: 1, status: "active", expiresAt: new Date(expiry + 1000).toISOString(), mode: "autonomous", permissions: ["mm:uptime:write"], scope: { serverIds: [101, 102], operationIds: ["uptime.speed-test"], allowGlobal: false }, dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["uptime.speed-test"], limits: { callsPerMinute: 60, concurrentReads: 4, mutationsPerHour: 10, serversPerPlan: 5, objectsPerPlan: 100 } } } + const context = { actorId: connection.owner_id, connectionId, approved: false, serverIds: [101, 102], objects: [101, 102].map(serverId => ({ kind: "server", serverId, id: String(serverId) })), serverWide: true, serviceExecution: metadata, + // Actual current-principal/portal/credential/grant path, not return true. + authorize: async (): Promise => { authorizerCalls++; const current = await effectiveMcpConnection(metadata.principal) + assert.equal(current.owner_id, context.actorId); assert(hasPermission(current.owner_permissions, "mm:uptime:write")); assert(hasPermission(current.policy.permissions, "mm:uptime:write")) + assert(current.policy.scope.operationIds.includes("uptime.speed-test")); assert([101, 102].every(id => current.policy.scope.serverIds.includes(id))) + assert(current.policy.mode === "autonomous" && current.policy.dangerousPolicy === "explicit-allow" && current.policy.autonomousDangerousOperationIds.includes("uptime.speed-test") || context.approved) + await authorizerHook?.() + } } + row = { operation_state: "running", plan_state: "executing", step_state: "intent", operation_id: metadata.operationName, operation_version: 1, policy_version: 1, + generation: metadata.generation, expires_at: new Date(expiry), approved_by: null, owner_id: context.actorId, evidence_proof: metadata.sealedEvidenceProof, payload_proof: metadata.sealedPayloadProof, + intended_state: redact({ kind: "service", service: metadata.serviceName, input }), server_id: null } + return { input, metadata, context } + } + try { + await t.test("outside/fake/copy raw input cannot mint authority or read credentials", async () => { + const f = fixture(), before = credentials + assertProvider(provider); assert.throws(() => assertProvider({ ...provider })) + await assert.rejects(provider.capture(f.input, scope())); await assert.rejects(provider.readOriginal({}, scope())); assert.equal(credentials, before) + let called = false; const getter = { ...f.input, get command() { called = true; return f.input.command } } + await assert.rejects(provider.capture(getter, scope())); assert.equal(called, false) + }) + await t.test("actual autonomous sealed origin, full baseline, stable durable run identity and original lifetime", async () => { + const f = fixture(); let saved: unknown, signal: AbortSignal | undefined + await withVerifiedConfigurationChange(f.context, async () => { + const firstScope = scope(), raw = structuredClone(f.input), token = await provider.capture(raw, firstScope); saved = token; raw.command.srcServerId = 999 + const intent = await provider.readOriginal(token, scope()), context = await provider.readSpeedContext(token, scope()), identity = createBandwidthSpeedRunIdentity(f.input.command.runId) + assert.equal(intent.callerKind, "mcp-speed"); assert.equal(intent.socketTimeoutMs, 30000); assert.equal(intent.attemptId, identity.attemptId); assert.equal(intent.operationId, identity.operationId) + assert.deepEqual(intent.nativeBody, { address: "198.51.100.2", user: "fixture-user-102", password: "fixture-secret-102", protocol: "udp", direction: "receive", duration: "3s" }) + assert.deepEqual(nativeOrder, [102, 101]); assert.equal(context.srcAddress, "198.51.100.1"); assert.equal(context.dstAddress, "198.51.100.2"); assert(Object.isFrozen(intent.request)) + assertMcpSpeedBandwidthWorkflowRequest(intent.request, context.command, intent.expiresAt) + const lifetime = await provider.readLifetime(token, scope()); signal = lifetime.signal + assert.equal(lifetime.expiresAt, f.metadata.originalExpiresAt); assert(lifetime.monotonicDeadline <= performance.now() + 10000); assert.equal((await provider.readLifetime(token, scope())).signal, signal) + const before = credentials + for (const fake of [{}, { ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(provider.readOriginal(fake, scope())) + await assert.rejects(createMcpSpeedBandwidthOriginalProvider().readOriginal(token, scope())); await assert.rejects(provider.readPeer(token, { kind: "server", serverId: 103 }, scope())) + assert.equal(credentials, before) + const again = await provider.capture(f.input, firstScope); assert.equal((await provider.readOriginal(again, scope())).attemptId, identity.attemptId) + await assert.rejects(createMcpSpeedBandwidthOriginalProvider().capture(f.input, { deadlineAt: firstScope.deadlineAt + 1 })) + }) + assert.equal(signal?.aborted, true); await assert.rejects(provider.readOriginal(saved, scope())) + }) + await t.test("wrong service/version/sealed request/scope/probe/proof and absent run ID refuse before native", async () => { + for (const fault of ["service", "operation", "version", "scope", "object", "probe", "proof", "run-id", "raw-body"] as const) { + const f = fixture(), before = nativeReads + if (fault === "service") f.metadata.serviceName = "routing.measure" + if (fault === "operation") f.metadata.operationName = "diagnostics.bandwidth" + if (fault === "version") f.metadata.operationVersion = 2 + if (fault === "scope") f.context.serverIds = [101] + if (fault === "object") f.context.objects = f.context.objects.slice(0, 1) + if (fault === "probe") probe!.direction = "both" + if (fault === "proof") f.input.expectedEndpoints[1].proof = "0".repeat(64) + if (fault === "run-id") f.input.command.runId = "" + row.operation_id = f.metadata.operationName; row.operation_version = f.metadata.operationVersion; row.intended_state = redact({ kind: "service", service: f.metadata.serviceName, input: f.input }) + const raw = fault === "raw-body" ? { ...f.input, nativeBody: { address: "outside.invalid" } } : f.input + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(raw, scope())) }); assert.equal(nativeReads, before) + } + }) + await t.test("current real authorizer rejects credential/portal/permission/scope before credentials", async () => { + for (const fault of ["credential", "portal", "owner-permission", "connection-permission", "target", "operation", "mcp-disabled"] as const) { + const f = fixture(false), before = credentials + if (fault === "credential") credentialActive = false + if (fault === "portal") portalRevoked = true + if (fault === "owner-permission") connection.owner_permissions = [] + if (fault === "connection-permission") connection.policy.permissions = [] + if (fault === "target") connection.policy.scope.serverIds = [101] + if (fault === "operation") connection.policy.scope.operationIds = [] + if (fault === "mcp-disabled") mcpEnabled = false + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(f.input, scope())) }); assert.equal(credentials, before) + } + }) + await t.test("retained original revalidates sealed SQL, endpoint/probe/key/gen and actor", async () => { + for (const fault of ["sealed", "state", "endpoint", "probe", "key", "generation", "directory", "actor"] as const) { + const f = fixture(false) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), before = credentials + if (fault === "sealed") row.payload_proof = "0".repeat(64) + if (fault === "state") row.operation_state = "failed" + if (fault === "endpoint") endpoints[1].password = "changed" + if (fault === "probe") probe = undefined + if (fault === "key") await writeFile(keyPath, Buffer.alloc(32, 89)) + if (fault === "generation") await writeFile(generationPath, Buffer.alloc(32, 97)) + if (fault === "directory") process.env.MCP_STATE_DIR = join(directory, "foreign") + if (fault === "actor") connection.policy.permissions = [] + await assert.rejects(provider.readOriginal(token, scope())); assert.equal(credentials, before) + }) + process.env.MCP_STATE_DIR = directory; await writeFile(keyPath, key); await writeFile(generationPath, generation) + } + }) + await t.test("missing material/history used ID and malformed Boolean snapshot fail closed", async () => { + for (const path of [keyPath, generationPath]) { + const f = fixture(false), before = credentials; await unlink(path) + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(f.input, scope())) }) + await assert.rejects(readFile(path)); assert.equal(credentials, before); await writeFile(path, path === keyPath ? key : generation) + } + const f = fixture(false); historyIds.add(f.input.command.runId) + await withVerifiedConfigurationChange(f.context, async () => { const before = credentials; await assert.rejects(provider.capture(f.input, scope())); assert.equal(credentials, before) }) + for (const result of [{ rows: [{ current: true }] }, { command: "SELECT", rowCount: 1, rows: [{ current: "true" }] }, { command: "SELECT", rowCount: 1, rows: [{ current: true, extra: true }] }]) { + const f = fixture(false) + await withVerifiedConfigurationChange(f.context, async () => { const token = await provider.capture(f.input, scope()); snapshotOverride = result; await assert.rejects(provider.readOriginal(token, scope())) }) + } + }) + await t.test("genuine host reader rejects forged/copied/wrong-cap completion before SQL", async () => { + const f = fixture(false) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), before = credentials + for (const result of [{ outcome: "complete" }, Object.freeze({ rows: [] }), {}]) await assert.rejects(provider.readPeerAfterCompletion(token, result, { kind: "server", serverId: 101 }, scope())) + await assert.rejects(provider.readPeerAfterCompletion({ ...token }, {}, { kind: "server", serverId: 101 }, scope())); assert.equal(credentials, before) + }) + }) + await t.test("current rights and sealed origin drift around native/SQL awaits refuse", async () => { + const f = fixture(), before = nativeReads + nativeHook = () => { row.payload_proof = "0".repeat(64) } + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(f.input, scope())) }) + assert.equal(nativeReads, before + 1) + const g = fixture(false), beforeCredentials = credentials + sqlHook = text => { if (text.startsWith("/* mcp-speed-original:metadata */")) connection.policy.permissions = [] } + await withVerifiedConfigurationChange(g.context, async () => { await assert.rejects(provider.capture(g.input, scope())) }) + assert.equal(credentials, beforeCredentials) + }) + await t.test("one current guard uses two joined snapshots/two genuine authorizers and a closing portal fence", async () => { + const f = fixture(false) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), before = { origins: originReads, authorizers: authorizerCalls } + await provider.readOriginal(token, scope()) + assert.equal(originReads - before.origins, 0); assert.equal(authorizerCalls - before.authorizers, 2) + let reads = 0 + sqlHook = text => { if (text.includes("FROM public.mcp_operations") && ++reads === 2) row.operation_state = "failed" } + await assert.rejects(provider.readOriginal(token, scope())); assert.equal(reads, 2) + }) + }) + await t.test("prepared name binds fixed SQL only; every read forwards new current parameters and deadline", async () => { + const captured: typeof snapshotConfigs = [] + for (let i = 0; i < 2; i++) { + const f = fixture(false) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), before = snapshotConfigs.length + await provider.readOriginal(token, scope()) + assert.equal(snapshotConfigs.length - before, 2, "both actual current queries still execute") + captured.push(...snapshotConfigs.slice(before)) + }) + } + assert(captured.every(config => config.name === captured[0].name && config.text === captured[0].text)) + assert.notEqual(captured[0].values, captured[1].values) + assert.notEqual(captured[0].values[3], captured[2].values[3], "new genuine original supplies its own current source parameters") + assert(captured.every(config => config.values.length === 4 && config.query_timeout > 0 && config.query_timeout <= 5000)) + }) + await t.test("revoke/portal/key/sealed drift exactly at joined SQL and after last authorizer refuses", async () => { + for (const phase of ["joined-sql", "last-authorizer"] as const) for (const fault of ["credential", "portal", "key", "sealed", "permissions"] as const) { + const f = fixture(false) + try { + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), beforeCredentials = credentials, beforeNative = nativeReads + const drift = () => { + if (fault === "credential") credentialActive = false + else if (fault === "portal") portalRevoked = true + else if (fault === "key") writeFileSync(keyPath, Buffer.alloc(32, 99)) + else if (fault === "sealed") row.payload_proof = "0".repeat(64) + else connection.policy.permissions = [] + } + let seen = 0 + if (phase === "joined-sql") sqlHook = text => { if (text.startsWith("/* mcp-speed-original:current-snapshot */") && ++seen === 1) drift() } + else authorizerHook = async () => { if (++seen === 2) drift() } + await assert.rejects(provider.readOriginal(token, scope())) + assert.equal(credentials, beforeCredentials); assert.equal(nativeReads, beforeNative) + }) + } finally { await writeFile(keyPath, key) } + } + }) + await t.test("joined authority retains normalized grant value AND physical row identity; absence/duplicate cannot widen", async () => { + for (const fault of ["row", "value", "absent", "duplicate"] as const) { + const f = fixture(false), now = Date.now(), rowId = randomUUID() + const raw = { id: " owned-grant ", startsAt: new Date(now - 1000).toISOString(), expiresAt: new Date(now + 20000).toISOString(), + scope: { serverIds: [101, 102], operationIds: ["uptime.speed-test"] } } + const parsed = mcpTemporaryGrantSchema.parse(raw) + grants = [{ id: rowId, payload: { ...raw, credentialId: f.metadata.principal.credentialId } }] + f.metadata.originalTemporaryGrantProof = fingerprint({ rowId, credentialId: f.metadata.principal.credentialId, grant: parsed }) + f.metadata.originalTemporaryGrantExpiresAt = Date.parse(parsed.expiresAt) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()) + await provider.readOriginal(token, scope()) + if (fault === "row") grants[0].id = randomUUID() + else if (fault === "value") grants[0].payload.id = "changed" + else if (fault === "absent") grants = [] + else grants.push(structuredClone(grants[0])) + await assert.rejects(provider.readOriginal(token, scope())) + }) + } + }) + await t.test("external durable portal cutoff written during final authorizer is caught by closing portal fence", async () => { + const f = fixture(false), ownerDirectory = join(directory, "owner-revocations") + await mkdir(ownerDirectory) + const file = join(ownerDirectory, createHash("sha256").update(connection.owner_id).digest("hex") + ".json") + try { + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()); let calls = 0 + authorizerHook = async () => { if (++calls === 2) writeFileSync(file, JSON.stringify({ invalidBefore: Date.now() })) } + await assert.rejects(provider.readOriginal(token, scope()), error => !!error && typeof error === "object" && "code" in error && error.code === "OWNER_CLAIMS_REVOKED") + assert.equal(calls, 2) + }) + } finally { await unlink(file) } + }) + await t.test("three file fences reject missing existing material before either authorizer and at final return", async () => { + for (const phase of ["before-first", "before-second", "final"] as const) for (const path of [keyPath, generationPath]) { + const f = fixture(false) + try { + await withVerifiedConfigurationChange(f.context, async () => { + const token = await provider.capture(f.input, scope()), before = authorizerCalls, beforeNative = nativeReads + let removed = false + const remove = () => { if (!removed) { removed = true; unlinkSync(path) } } + if (phase === "before-first") remove() + else if (phase === "before-second") sqlHook = text => { if (text.startsWith("/* mcp-speed-original:current-snapshot */")) remove() } + else authorizerHook = async () => { if (authorizerCalls - before === 2) remove() } + await assert.rejects(provider.readOriginal(token, scope())) + assert(removed); assert.equal(authorizerCalls - before, phase === "before-first" ? 0 : phase === "before-second" ? 1 : 2) + await assert.rejects(readFile(path), { code: "ENOENT" }); assert.equal(nativeReads, beforeNative) + }) + } finally { await writeFile(path, path === keyPath ? key : generation) } + } + }) + await t.test("initial credential SQL includes exact probe on same command; manual disabled probe remains allowed", async () => { + const f = fixture(false), beforePayloads = credentialPayloads, beforeNative = nativeReads + sqlHook = text => { if (text.startsWith("/* mcp-speed-original:credentials */")) probe!.direction = "both" } + await withVerifiedConfigurationChange(f.context, async () => { await assert.rejects(provider.capture(f.input, scope())) }) + assert.equal(credentialPayloads, beforePayloads); assert.equal(nativeReads, beforeNative) + const g = fixture(false) + probe!.enabled = false; g.input.expectedProbe!.enabled = false + row.intended_state = redact({ kind: "service", service: g.metadata.serviceName, input: g.input }) + await withVerifiedConfigurationChange(g.context, async () => { const token = await provider.capture(g.input, scope()); assert.equal((await provider.readOriginal(token, scope())).nativeBody.duration, "3s") }) + }) + await t.test("genuine service finally cancels pending transport on success and failure", async () => { + for (const fail of [false, true]) { + const f = fixture(), started = new Promise(yes => { nativeStarted = yes }); pending = true + let capture: Promise | undefined + const run = withVerifiedConfigurationChange(f.context, async () => { capture = provider.capture(f.input, scope()); capture.catch(() => {}); await started; if (fail) throw Error("Owned genuine service failed") }) + if (fail) await assert.rejects(run); else await run + await assert.rejects(capture!); assert.equal(aborted, true) + } + }) + await t.test("first consumer cancellation/deadline cannot be renewed by another provider factory", async () => { + const f = fixture(false), owner = new AbortController() + await withVerifiedConfigurationChange(f.context, async () => { + const first = scope(10000, owner.signal), token = await provider.capture(f.input, first), lifetime = await provider.readLifetime(token, scope()), before = credentials + owner.abort(); assert(lifetime.signal.aborted) + await assert.rejects(provider.readOriginal(token, scope())) + await assert.rejects(createMcpSpeedBandwidthOriginalProvider().capture(f.input, { deadlineAt: first.deadlineAt, signal: new AbortController().signal })) + assert.equal(credentials, before) + }) + }) + await t.test("service lifetime inspector retains metadata shape, original signal and expiry", async () => { + const f = fixture(false, false, 10000) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await captureVerifiedMcpServiceExecution(), first = await inspectVerifiedMcpServiceExecution(token), lifetime = await inspectVerifiedMcpServiceExecutionLifetime(token) + assert.equal(lifetime.signal, (await inspectVerifiedMcpServiceExecutionLifetime(token)).signal) + assert.equal(lifetime.monotonicDeadline, (await inspectVerifiedMcpServiceExecutionLifetime(token)).monotonicDeadline) + assert.deepEqual(await inspectVerifiedMcpServiceExecution(token), first); assert(!("signal" in first)); assert(!("monotonicDeadline" in first)) + }) + const g = fixture(true); sourceFails = true + await withVerifiedConfigurationChange(g.context, async () => { const token = await provider.capture(g.input, scope()); assert.equal((await provider.readSpeedContext(token, scope())).srcAddress, ""); assert.equal((await provider.readOriginal(token, scope())).nativeBody.address, "198.51.100.2") }) + }) + await t.test("private original monotonic expiry aborts despite wall-clock rollback", async () => { + const f = fixture(false, false, 500), now = Date.now + try { + await withVerifiedConfigurationChange(f.context, async () => { + const token = await captureVerifiedMcpServiceExecution(), lifetime = await inspectVerifiedMcpServiceExecutionLifetime(token), began = performance.now() + Date.now = () => now() - 60000 + await new Promise((yes, no) => { + const timer = setTimeout(() => no(Error("Original monotonic expiry did not abort")), 2000) + const done = () => { clearTimeout(timer); yes() } + if (lifetime.signal.aborted) done(); else lifetime.signal.addEventListener("abort", done, { once: true }) + }) + assert(lifetime.signal.aborted); assert(performance.now() - began < 2000) + await assert.rejects(inspectVerifiedMcpServiceExecution(token)) + }) + } finally { Date.now = now } + }) + assert(authorizerCalls > 0); assert.equal(posts, 0) + } finally { + pool.query = actualQuery; pool.connect = actualConnect; MikrotikClient.prototype.get = actualGet; MikrotikClient.prototype.post = actualPost + process.env.MCP_STATE_DIR = previous; await closePool() + assert.equal(dirname(await realpath(directory)), parent); assert.match(basename(directory), /^mm-mcp-speed-original-/); await rm(directory, { recursive: true, force: true }) + } +}) diff --git a/backend/src/modules/mcp/speed-bandwidth-original.ts b/backend/src/modules/mcp/speed-bandwidth-original.ts new file mode 100644 index 00000000..7d2c23eb --- /dev/null +++ b/backend/src/modules/mcp/speed-bandwidth-original.ts @@ -0,0 +1,430 @@ +/** Sealed MCP full uptime speed command only. No bandwidth dispatch, enrollment, + * runtime wiring, history publication or substitute diagnostic workflow. */ +import type { QueryConfig } from "pg" +import { createHash } from "node:crypto" +import { readFile } from "node:fs/promises" +import { join } from "node:path" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { speedTestCommandSchema, type SpeedTestCommand } from "@mmapp/contracts" +import { mcpConnectionPolicySchema, mcpTemporaryGrantSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecutionLifetime } from "../../services/config-mutation-policy.js" +import { readCurrentMcpServiceExecution } from "./service-execution-origin.js" +import { effectiveMcpConnection } from "./store.js" +import { assertPortalClaimsCurrent } from "./portal-revocation.js" +import { hasPermission } from "../../lib/permissions.js" +import { fingerprint, McpError, mcpStateDirectory, privateFingerprinterExisting, redact } from "./security.js" +import type { RoutingEndpoint } from "./routing-observation-source.js" +import type { BandwidthScope } from "../../services/bandwidth-admission.js" +import type { BandwidthPeerExecutionIntent } from "../../services/bandwidth-peer-execution-host.js" +import { readBandwidthPeerCompletion } from "../../services/bandwidth-peer-execution-host.js" +import type { BandwidthPeerSourceSnapshot } from "../../services/bandwidth-peer-observation.js" +import { parseBandwidthPeerRef, type BandwidthPeerRef } from "../../services/bandwidth-peer-reference.js" +import { assertSpeedProbeContext } from "../../services/speed-run-persistence.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { createBandwidthSpeedRunIdentity, BANDWIDTH_SPEED_RUN_HISTORY_SQL, assertBandwidthSpeedRunHistoryAbsent, BandwidthSpeedRunIdUnavailable } from "../../services/bandwidth-speed-run-identity.js" + +declare const mcpSpeedBrand: unique symbol +export type McpSpeedBandwidthOriginal = Readonly<{ [mcpSpeedBrand]: true }> +export interface McpSpeedBandwidthContext { + readonly command: Readonly; readonly srcAddress: string; readonly dstAddress: string + readonly address: string; readonly srcInterfaceAddress: string | null; readonly dstInterfaceAddress: string | null +} +const providers = new WeakSet() +// All private tokens from the SAME genuine service scope expose this one +// controller signal. Another provider factory cannot renew its first window. +const serviceCaptures = new WeakMap>() +const deny = (): never => { throw new McpError("PERMISSION_DENIED", "Исходный MCP speed-контекст изменён или недоступен", 403) } +export function assertMcpSpeedBandwidthOriginalProvider(value: unknown): asserts value is ReturnType { + if (!value || typeof value !== "object" || !providers.has(value)) deny() +} +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} +const commandFields = new Set(["runId", "probeId", "srcServerId", "dstServerId", "srcInterface", "dstInterface", "protocol", "direction", "durationSec"]) +function commandSnapshot(raw: unknown): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype) return deny() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== Object.keys(fields).length || Object.keys(fields).some(key => !commandFields.has(key)) + || Object.values(fields).some(field => !("value" in field) || !field.enumerable || field.value !== undefined && !["string", "number"].includes(typeof field.value))) deny() + const parsed = speedTestCommandSchema.parse(Object.fromEntries(Object.entries(fields).map(([key, field]) => [key, field.value]))) + // Keep optional absent IDs out of admission JSON while preserving the exact + // shared contract's coercion/defaults/trim behavior. + return freeze(Object.fromEntries(Object.entries(parsed).filter(([, value]) => value !== undefined)) as SpeedTestCommand) +} +function scopeSnapshot(raw: Readonly): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype || Reflect.ownKeys(raw).length !== Object.keys(raw).length) deny() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Object.keys(fields).some(key => !["deadlineAt", "signal"].includes(key)) || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) deny() + const deadlineAt = fields.deadlineAt?.value, signal = fields.signal?.value + if (typeof deadlineAt !== "number" || !Number.isSafeInteger(deadlineAt) || signal !== undefined && !(signal instanceof AbortSignal)) return deny() + return Object.freeze({ deadlineAt, signal }) +} +const metadataSchema = z.array(z.object({ id: z.number().int().positive().safe(), enabled: z.literal(true), routingEndpointEpoch: z.string().uuid() }).strict()).length(2) +const endpointSchema = z.object({ id: z.number().int().positive().safe(), enabled: z.literal(true), routingEndpointEpoch: z.string().uuid(), host: z.string().min(1).max(1000), + port: z.number().int().min(1).max(65535), useSsl: z.boolean(), verifySsl: z.boolean(), username: z.string().max(1000), password: z.string().max(65536) }).strict() +const probeSchema = z.object({ id: z.string(), srcServerId: z.number().int().positive().safe(), dstServerId: z.number().int().positive().safe(), + srcInterface: z.string(), dstInterface: z.string(), protocol: z.enum(["tcp", "udp"]), direction: z.enum(["both", "transmit", "receive"]), durationSec: z.number().int().min(3).max(2147483647), enabled: z.boolean() }).strict() +type Metadata = z.infer +type Probe = z.infer | undefined +const currentActorSchema = z.object({ id: z.string().uuid(), ownerId: z.string(), ownerPermissions: z.array(z.string()).max(1000), policy: mcpConnectionPolicySchema, + generation: z.string(), ownerValidUntil: z.string().datetime({ offset: true }), ownerClaimsIssuedAt: z.string().datetime({ offset: true }), + grants: z.array(z.object({ id: z.string().uuid(), payload: z.record(z.string(), z.unknown()) }).strict()).max(1) }).strict() +export interface McpSpeedBandwidthInput { + readonly command: Readonly + readonly expectedEndpoints: ReadonlyArray> + readonly expectedProbe: z.infer | null +} +function inputSnapshot(raw: unknown): Readonly { + let bytes = 0; const seen = new Set() + const visit = (value: unknown, depth: number): void => { + if (depth > 10) deny() + if (value === null || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return + if (typeof value === "string") { bytes += Buffer.byteLength(value); if (bytes > 131072) deny(); return } + if (!value || typeof value !== "object" || seen.has(value)) return deny(); seen.add(value) + const fields = Object.getOwnPropertyDescriptors(value) + if (Array.isArray(value)) { + if (Reflect.ownKeys(value).length !== value.length + 1) deny() + for (let i = 0; i < value.length; i++) { const field = fields[String(i)]; if (!field || !("value" in field) || !field.enumerable) deny(); visit(field.value, depth + 1) } + } else { + if (Object.getPrototypeOf(value) !== Object.prototype || Reflect.ownKeys(value).length !== Object.keys(fields).length) deny() + for (const field of Object.values(fields)) { if (!("value" in field) || !field.enumerable) deny(); visit(field.value, depth + 1) } + } + } + visit(raw, 0) + const value = z.object({ command: z.unknown(), expectedEndpoints: z.array(z.object({ serverId: z.number().int().positive().safe(), proof: z.string().regex(/^[a-f0-9]{64}$/) }).strict()).length(2), expectedProbe: probeSchema.nullable() }).strict().parse(raw) + const command = commandSnapshot(value.command) + if (!command.runId || !isDeepStrictEqual(command, value.command) || !!command.probeId !== !!value.expectedProbe || value.expectedProbe && value.expectedProbe.id !== command.probeId + || !isDeepStrictEqual(value.expectedEndpoints.map(row => row.serverId), [command.srcServerId, command.dstServerId].sort((a, b) => a - b))) deny() + return freeze(structuredClone({ command, expectedEndpoints: value.expectedEndpoints, expectedProbe: value.expectedProbe })) +} +/** Pure workflow consistency only. This never grants authority or mints a cap; + * the caller must already hold this provider's genuine private original. */ +export function assertMcpSpeedBandwidthWorkflowRequest(request: unknown, command: Readonly, expiresAt: number): void { + if (!request || typeof request !== "object" || Array.isArray(request) || Reflect.ownKeys(request).length !== 2) deny() + const fields = Object.getOwnPropertyDescriptors(request), commandField = fields.command, originField = fields.mcp + if (!commandField || !("value" in commandField) || !originField || !("value" in originField) || !isDeepStrictEqual(commandField.value, command)) deny() + const origin = originField.value as Awaited> + if (!origin || origin.operationName !== "uptime.speed-test" || origin.serviceName !== "uptime.speed-test" || origin.operationVersion !== 1 + || origin.phase !== "forward" || origin.originalExpiresAt !== expiresAt || !isDeepStrictEqual(inputSnapshot(origin.serviceInput).command, command) + || [command.srcServerId, command.dstServerId].some(id => !origin.serverIds?.includes(id) || !origin.objects?.some(object => object.kind === "server" && object.serverId === id && object.id === String(id)))) deny() +} +const purpose = "routing-observation-source-v1", keyMessage = "routing-observation-source-purpose-key-epoch-v1" +async function existingGeneration(directory: string) { + const bytes = await readFile(join(directory, "access-generation")) + if (bytes.length !== 32) return deny() + return bytes.toString("hex") +} +/** Fixed current-facts projection. Captured credentials are private parameters + * compared INSIDE PostgreSQL. Bounded authority metadata stays private; the + * same contract schemas normalize policy/grant rather than SQL approximations. + * Completion uses a separate fixed query omitting ONLY the selected probe. */ +function currentSnapshotSql(includeProbe: boolean): string { + return `/* mcp-speed-original:${includeProbe ? "current-snapshot" : "completion-snapshot"} */ WITH input AS MATERIALIZED (SELECT $2::jsonb AS probe,$3::text AS id), expected AS MATERIALIZED ( + SELECT * FROM pg_catalog.jsonb_to_recordset($1::jsonb) AS e(id bigint,enabled boolean,"routingEndpointEpoch" uuid, + host text,port integer,"useSsl" boolean,"verifySsl" boolean,username text,password text)), + matched AS MATERIALIZED ( + SELECT s.id FROM public.servers s JOIN expected e ON e.id=s.id WHERE s.enabled=true + AND pg_catalog.jsonb_build_object('id',s.id,'enabled',s.enabled,'routingEndpointEpoch',s.routing_endpoint_epoch, + 'host',s.host,'port',s.port,'useSsl',s.use_ssl,'verifySsl',s.verify_ssl,'username',s.username,'password',s.password)=pg_catalog.to_jsonb(e)), + endpoints_current AS MATERIALIZED (SELECT count(*)=2 AND count(DISTINCT id)=2 + AND (SELECT count(*) FROM matched)=2 AS current FROM expected)${includeProbe ? `, + chosen_probe AS MATERIALIZED (SELECT pg_catalog.jsonb_build_object('id',p.id,'srcServerId',p.src_server_id,'dstServerId',p.dst_server_id, + 'srcInterface',p.src_interface,'dstInterface',p.dst_interface,'protocol',p.protocol,'direction',p.direction,'durationSec',p.duration_sec,'enabled',p.enabled) AS projection + FROM public.uptime_speed_probes p WHERE p.id=$3::text LIMIT 2)` : ""} + , current_actor AS MATERIALIZED (SELECT c.id,c.owner_id,c.owner_permissions,c.policy,c.generation,c.owner_valid_until,c.owner_claims_issued_at, + coalesce((SELECT jsonb_agg(g) FROM (SELECT id::text,CASE WHEN octet_length(payload::text)<=1048576 THEN payload END AS payload + FROM public.mcp_grants WHERE connection_id=c.id AND revoked_at IS NULL AND payload->>'credentialId'=$4::jsonb->>'credentialId' LIMIT 2)g),'[]'::jsonb) AS grants, + EXISTS(SELECT 1 FROM public.mcp_credentials k WHERE k.id=($4::jsonb->>'credentialId')::uuid AND k.connection_id=c.id AND k.revoked_at IS NULL AND k.expires_at>now()) AS credential_current, + EXISTS(SELECT 1 FROM public.mcp_settings WHERE id=1 AND enabled=true) AS settings_current, + NOT EXISTS(SELECT 1 FROM public.mcp_owner_revocations r WHERE r.owner_id=c.owner_id AND r.invalid_before>to_timestamp(0) + AND (c.owner_claims_issued_at IS NULL OR c.owner_claims_issued_at<=r.invalid_before)) AS portal_current + FROM public.mcp_connections c WHERE c.id=($4::jsonb->>'connectionId')::uuid LIMIT 2), + actor_projection AS MATERIALIZED (SELECT jsonb_build_object('id',id,'ownerId',owner_id,'ownerPermissions',owner_permissions, + 'policy',policy,'generation',generation,'ownerValidUntil',owner_valid_until,'ownerClaimsIssuedAt',owner_claims_issued_at,'grants',grants) AS authority, + credential_current AND settings_current AND portal_current AS current FROM current_actor), + sealed_source AS MATERIALIZED (SELECT o.state AS operation_state,p.state AS plan_state,s.state AS step_state,p.operation_id,p.operation_version,p.policy_version, + p.generation,p.expires_at,p.approved_by,c.owner_id,s.server_id,s.intended_state, + encode(sha256(convert_to(p.evidence->>'sealed','UTF8')),'hex') AS evidence_proof, + encode(sha256(convert_to(p.payload->>'sealed','UTF8')),'hex') AS payload_proof + FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_connections c ON c.id=o.connection_id JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=($4::jsonb->>'stepIndex')::integer + WHERE o.id=($4::jsonb->>'operationId')::uuid AND p.id=($4::jsonb->>'planId')::uuid AND o.connection_id=($4::jsonb->>'connectionId')::uuid LIMIT 2) + SELECT (e.current${includeProbe ? ` AND ($2::jsonb IS NULL OR ((SELECT count(*) FROM chosen_probe)=1 + AND (SELECT projection FROM chosen_probe LIMIT 1)=$2::jsonb))` : ""} + AND (SELECT count(*) FROM actor_projection)=1 AND (SELECT current FROM actor_projection LIMIT 1) + AND (SELECT count(*) FROM sealed_source)=1 AND EXISTS(SELECT 1 FROM sealed_source s WHERE s.operation_state='running' AND s.plan_state='executing' AND s.step_state='intent' + AND s.server_id IS NULL AND s.operation_id=$4::jsonb->>'operationName' AND s.operation_version=($4::jsonb->>'operationVersion')::integer + AND s.policy_version=($4::jsonb->>'policyVersion')::integer AND s.generation=$4::jsonb->>'generation' AND s.owner_id=$4::jsonb->>'ownerId' + AND (s.approved_by IS NOT NULL)=($4::jsonb->>'approved')::boolean AND s.expires_at>=to_timestamp(($4::jsonb->>'originalExpiresAt')::double precision/1000) + AND s.evidence_proof=$4::jsonb->>'sealedEvidenceProof' AND s.payload_proof=$4::jsonb->>'sealedPayloadProof' AND s.intended_state=$4::jsonb->'intendedState')) IS TRUE AS current, + (SELECT CASE WHEN octet_length(authority::text)<=1048576 THEN authority END FROM actor_projection LIMIT 1) AS authority FROM endpoints_current e` +} +const CURRENT_SNAPSHOT_SQL = currentSnapshotSql(true), COMPLETION_SNAPSHOT_SQL = currentSnapshotSql(false) +// Per-physical-connection pg statement names cache parsing/planning only. All +// values, SQL execution, live facts and authority checks remain fresh per call. +const snapshotStatementName = (sql: string) => "mcp-speed-" + createHash("sha256").update(sql).digest("hex").slice(0, 48) +const CURRENT_STATEMENT = snapshotStatementName(CURRENT_SNAPSHOT_SQL), COMPLETION_STATEMENT = snapshotStatementName(COMPLETION_SNAPSHOT_SQL) +/** Same static-first/filter/order selection as executeBandwidthSpeedTestInner. */ +function firstUsableIp(rows: Record[], iface: string): string { + const candidates = rows.filter(row => String(row.interface ?? "").trim() === iface.trim() + && String(row.disabled ?? "false").toLowerCase() !== "true" && String(row.invalid ?? "false").toLowerCase() !== "true" && String(row.address ?? "").trim().includes("/")) + candidates.sort((a, b) => (String(a.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0) - (String(b.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0)) + return candidates[0]?.address ? String(candidates[0].address).split("/")[0]?.trim() ?? "" : "" +} +async function window(input: Readonly, authority: Readonly<{ expiresAt: number; monotonicDeadline: number; signal: AbortSignal }>, originalEnd: number, originalSignal: AbortSignal | undefined, work: (scope: Readonly, live: () => void) => Promise): Promise { + const controller = new AbortController() + const scope = Object.freeze({ deadlineAt: Math.min(input.deadlineAt, authority.expiresAt), signal: AbortSignal.any([controller.signal, authority.signal, ...(input.signal ? [input.signal] : []), ...(originalSignal ? [originalSignal] : [])]) }) + const end = Math.min(originalEnd, performance.now() + Math.max(0, scope.deadlineAt - Date.now())) + const signals = [...new Set([scope.signal, originalSignal, authority.signal].filter((signal): signal is AbortSignal => !!signal))] + let active = true, rejectStopped!: (error: Error) => void + const live = () => { if (!active || Date.now() >= scope.deadlineAt || performance.now() >= end || signals.some(signal => signal.aborted)) deny() } + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + const stop = () => { if (!active) return; active = false; controller.abort(); rejectStopped(new McpError("PERMISSION_DENIED", "MCP speed-контекст завершён", 403)) } + const timer = setTimeout(stop, Math.max(1, Math.min(scope.deadlineAt - Date.now(), end - performance.now()))) + for (const signal of signals) signal.addEventListener("abort", stop, { once: true }) + try { live(); return await Promise.race([Promise.resolve().then(async () => { live(); const result = await work(scope, live); live(); return result }), stopped]) } + finally { active = false; clearTimeout(timer); for (const signal of signals) signal.removeEventListener("abort", stop); controller.abort() } +} + +/** Fixed production dependencies only. Constructor does no I/O and accepts no + * actor/client/SQL/proof callbacks. Capabilities remain bound to the live MCP + * service; they are not background job or independent wire-mapping authority. */ +export function createMcpSpeedBandwidthOriginalProvider() { + type Entry = { intent: BandwidthPeerExecutionIntent; context: Readonly; peers: ReadonlyMap> + authority: Readonly<{ expiresAt: number; monotonicDeadline: number; signal: AbortSignal }>; end: number; signal?: AbortSignal; assertCurrent(scope: Readonly, live: () => void): Promise + assertAuthorityCurrent(scope: Readonly, live: () => void): Promise } + const entries = new WeakMap() + const read = async (token: unknown, rawScope: Readonly) => { + const scope = scopeSnapshot(rawScope), entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry) return deny() + await window(scope, entry.authority, entry.end, entry.signal, (current, live) => entry.assertCurrent(current, live)) + return entry + } + const provider = Object.freeze({ + async capture(raw: unknown, rawScope: Readonly): Promise { + const input = inputSnapshot(raw), command = input.command, scope = scopeSnapshot(rawScope), entered = Date.now(), enteredMono = performance.now() + const provisional = Object.freeze({ expiresAt: scope.deadlineAt, monotonicDeadline: enteredMono + Math.max(0, scope.deadlineAt - entered), signal: scope.signal ?? new AbortController().signal }) + return window(scope, provisional, provisional.monotonicDeadline, scope.signal, async (initial, initialLive) => { + // Existing private material is checked before entering a generic runtime + // authorizer. This provider never bootstraps a missing key/generation. + const sourceDirectory = mcpStateDirectory(), generation = await existingGeneration(sourceDirectory); initialLive() + const sign = await privateFingerprinterExisting(purpose); initialLive(); const keyProof = sign(keyMessage) + const original = await captureVerifiedMcpServiceExecution(); initialLive() + const metadata = await readCurrentMcpServiceExecution(original, initial); initialLive() + if (metadata.operationName !== "uptime.speed-test" || metadata.serviceName !== "uptime.speed-test" || metadata.operationVersion !== 1 || metadata.phase !== "forward" + || !isDeepStrictEqual(metadata.serviceInput, input)) return deny() + const ids = [command.srcServerId, command.dstServerId].sort((a, b) => a - b) + if (ids.some(id => !metadata.serverIds.includes(id) || !metadata.objects.some(object => object.kind === "server" && object.serverId === id && object.id === String(id)))) deny() + const lifetime = await inspectVerifiedMcpServiceExecutionLifetime(original); initialLive() + let captured = serviceCaptures.get(lifetime.signal) + if (captured) { + if (captured.deadlineAt !== scope.deadlineAt || captured.inputSignal !== scope.signal || captured.lifecycleSignal.aborted) deny() + } else { + captured = Object.freeze({ deadlineAt: scope.deadlineAt, inputSignal: scope.signal, end: Math.min(lifetime.monotonicDeadline, provisional.monotonicDeadline), + lifecycleSignal: scope.signal ? AbortSignal.any([lifetime.signal, scope.signal]) : lifetime.signal }) + serviceCaptures.set(lifetime.signal, captured) + } + const authority = Object.freeze({ ...lifetime, monotonicDeadline: captured.end, signal: captured.lifecycleSignal }) + const run = createBandwidthSpeedRunIdentity(command.runId), sealedCommand = command + const end = captured.end + return window(initial, authority, end, scope.signal, async (current, live) => { + if (mcpStateDirectory() !== sourceDirectory || generation !== metadata.generation || await existingGeneration(sourceDirectory) !== generation) deny(); live() + const actorCurrent = async (next: Readonly, checkLive: () => void) => { + checkLive(); const connection = await effectiveMcpConnection(metadata.principal); checkLive() + if (connection.id !== metadata.connectionId || connection.owner_id !== metadata.ownerId || connection.generation !== metadata.generation + || !hasPermission(connection.owner_permissions, "mm:uptime:write") || !hasPermission(connection.policy.permissions, "mm:uptime:write") + || !connection.policy.scope.operationIds.includes("uptime.speed-test") || ids.some(id => !connection.policy.scope.serverIds.includes(id)) + || next.signal?.aborted) deny() + if (!isDeepStrictEqual(await readCurrentMcpServiceExecution(original, next), metadata)) deny(); checkLive() + } + const basic = async (next: Readonly, checkLive: () => void) => { + checkLive(); if (mcpStateDirectory() !== sourceDirectory) deny() + // This guard already reads effective current rights AND the sealed + // SQL origin, with fresh private authorization on both sides. + await actorCurrent(next, checkLive) + if (await existingGeneration(sourceDirectory) !== generation || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + if ((await privateFingerprinterExisting(purpose))(keyMessage) !== keyProof || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + await actorCurrent(next, checkLive) + } + const query = async (text: string, values: unknown[], next: Readonly, checkLive: () => void) => { + const name = text === CURRENT_SNAPSHOT_SQL ? CURRENT_STATEMENT : text === COMPLETION_SNAPSHOT_SQL ? COMPLETION_STATEMENT : undefined + checkLive(); const config: QueryConfig & { query_timeout: number } = { text, values, query_timeout: Math.max(1, Math.min(5000, next.deadlineAt - Date.now())), ...(name ? { name } : {}) } + const result = await pool.query(config); checkLive(); return result + } + const endpointMetadata = async (next: Readonly, checkLive: () => void): Promise => { + await basic(next, checkLive) + const result = await query('/* mcp-speed-original:metadata */ SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM public.servers WHERE id=ANY($1::bigint[]) ORDER BY id LIMIT 3', [ids], next, checkLive) + await basic(next, checkLive) + const rows = metadataSchema.parse(result.rows) + if (!isDeepStrictEqual(rows.map(row => row.id), ids)) deny() + return rows + } + const probe = async (next: Readonly, checkLive: () => void): Promise => { + if (!command.probeId) return undefined + await basic(next, checkLive) + const rows = (await query('/* mcp-speed-original:probe */ SELECT id,src_server_id AS "srcServerId",dst_server_id AS "dstServerId",src_interface AS "srcInterface",dst_interface AS "dstInterface",protocol,direction,duration_sec AS "durationSec",enabled FROM public.uptime_speed_probes WHERE id=$1 LIMIT 2', [command.probeId], next, checkLive)).rows + await basic(next, checkLive); if (rows.length !== 1) return deny() + const value = probeSchema.parse(rows[0]); assertSpeedProbeContext(value, { ...command, id: command.probeId }); return value + } + const endpoints = async (next: Readonly, expected: Metadata, checkLive: () => void) => { + await basic(next, checkLive) + // Enabled/epoch completeness gates credential projection on the SAME + // command snapshot, including a change after the earlier metadata IO. + const result = await query(`/* mcp-speed-original:credentials */ WITH expected AS MATERIALIZED ( + SELECT * FROM pg_catalog.jsonb_to_recordset($1::jsonb) AS e(id bigint,epoch uuid)), + matched AS MATERIALIZED (SELECT s.id FROM public.servers s JOIN expected e ON e.id=s.id AND e.epoch=s.routing_endpoint_epoch WHERE s.enabled=true), + allowed AS MATERIALIZED (SELECT count(*)=2 AND ($2::jsonb IS NULL OR EXISTS ( + SELECT 1 FROM public.uptime_speed_probes p WHERE p.id=$3::text + AND pg_catalog.jsonb_build_object('id',p.id,'srcServerId',p.src_server_id,'dstServerId',p.dst_server_id, + 'srcInterface',p.src_interface,'dstInterface',p.dst_interface,'protocol',p.protocol,'direction',p.direction,'durationSec',p.duration_sec,'enabled',p.enabled)=$2::jsonb)) AS complete FROM matched), + chosen AS MATERIALIZED (SELECT s.id,jsonb_build_object('id',s.id,'host',s.host,'port',s.port,'username',s.username,'password',s.password, + 'useSsl',s.use_ssl,'verifySsl',s.verify_ssl,'enabled',s.enabled,'routingEndpointEpoch',s.routing_endpoint_epoch) AS endpoint + FROM public.servers s JOIN matched m ON m.id=s.id CROSS JOIN allowed a WHERE a.complete), + budget AS (SELECT coalesce(sum(octet_length(endpoint::text)),0) AS bytes FROM chosen) + SELECT id,CASE WHEN budget.bytes<=1048576 THEN endpoint END AS endpoint,budget.bytes>1048576 AS overflow FROM chosen CROSS JOIN budget ORDER BY id LIMIT 3`, + [JSON.stringify(expected.map(row => ({ id: row.id, epoch: row.routingEndpointEpoch }))), originalProbe ? JSON.stringify(originalProbe) : null, originalProbe?.id ?? null], next, checkLive) + await basic(next, checkLive) + if (result.rows.some(row => row.overflow)) deny() + const rows = z.array(endpointSchema).length(2).parse(result.rows.map(row => row.endpoint)) + if (rows.length !== 2 || !isDeepStrictEqual(rows.map(row => ({ id: row.id, enabled: row.enabled, routingEndpointEpoch: row.routingEndpointEpoch })), expected)) deny() + return freeze(rows) + } + const originalMetadata = freeze(await endpointMetadata(current, live)), originalProbe = freeze(await probe(current, live)) + if (!isDeepStrictEqual(originalProbe ?? null, input.expectedProbe)) deny() + await basic(current, live) + let history: unknown + try { history = await query(BANDWIDTH_SPEED_RUN_HISTORY_SQL, [run.runId], current, live) } + catch { throw new BandwidthSpeedRunIdUnavailable(run.runId) } + assertBandwidthSpeedRunHistoryAbsent(history, run.runId) + await basic(current, live) + const originalEndpoints = await endpoints(current, originalMetadata, live) + if (!isDeepStrictEqual(input.expectedEndpoints, originalEndpoints.map(endpoint => ({ serverId: endpoint.id, proof: sign("endpoint:" + fingerprint({ generation, endpoint })) })))) deny() + const expectedEndpoints = JSON.stringify(originalEndpoints), expectedProbe = originalProbe ? JSON.stringify(originalProbe) : null + const expectedSource = JSON.stringify({ ...metadata, credentialId: metadata.principal.credentialId, + intendedState: redact({ kind: "service", service: metadata.serviceName, input: metadata.serviceInput }) }) + const filesCurrent = async (checkLive: () => void) => { + checkLive(); if (mcpStateDirectory() !== sourceDirectory) deny() + const [currentGeneration, currentSign] = await Promise.all([existingGeneration(sourceDirectory), privateFingerprinterExisting(purpose)]) + checkLive(); if (mcpStateDirectory() !== sourceDirectory || currentGeneration !== generation || currentSign(keyMessage) !== keyProof) deny() + } + const assertSnapshot = async (next: Readonly, checkLive: () => void, includeProbe: boolean) => { + const originCurrent = async () => { + checkLive(); if (mcpStateDirectory() !== sourceDirectory) deny() + if (!isDeepStrictEqual(await inspectVerifiedMcpServiceExecution(original), metadata)) deny() + checkLive(); if (mcpStateDirectory() !== sourceDirectory) deny() + } + const joinedSnapshot = async () => { + const result = await query(includeProbe ? CURRENT_SNAPSHOT_SQL : COMPLETION_SNAPSHOT_SQL, + [expectedEndpoints, includeProbe ? expectedProbe : null, includeProbe ? originalProbe?.id ?? null : null, expectedSource], next, checkLive) + if (!result || typeof result !== "object" || result.command !== "SELECT" || result.rowCount !== 1 || !Array.isArray(result.rows) || result.rows.length !== 1) deny() + const row = result.rows[0] + if (!row || typeof row !== "object" || Array.isArray(row) || Reflect.ownKeys(row).length !== 2) deny() + const fields = Object.getOwnPropertyDescriptors(row) + if (!fields.current || !("value" in fields.current) || !fields.current.enumerable || fields.current.value !== true + || !fields.authority || !("value" in fields.authority) || !fields.authority.enumerable) deny() + const actor = currentActorSchema.parse(fields.authority.value) + if (Buffer.byteLength(JSON.stringify(fields.authority.value)) > 1048576 || actor.id !== metadata.connectionId || actor.ownerId !== metadata.ownerId + || actor.generation !== generation || actor.policy.status !== "active" || Date.parse(actor.policy.expiresAt) <= Date.now() || Date.parse(actor.ownerValidUntil) <= Date.now() + || !hasPermission(actor.ownerPermissions, "mm:uptime:write") || !hasPermission(actor.policy.permissions, "mm:uptime:write")) deny() + let grant: z.infer | undefined + let grantProof = fingerprint({ credentialId: metadata.principal.credentialId, grant: null }), grantExpiresAt: number | null = null + if (actor.grants.length) { + const held = actor.grants[0] + if (held.payload.credentialId !== metadata.principal.credentialId) deny() + const { credentialId: _credential, ...raw } = held.payload + grant = mcpTemporaryGrantSchema.parse(raw); grantExpiresAt = Date.parse(grant.expiresAt) + if (Date.parse(grant.startsAt) > Date.now() || grantExpiresAt <= Date.now()) deny() + grantProof = fingerprint({ rowId: held.id, credentialId: metadata.principal.credentialId, grant }) + } + if (grantProof !== metadata.originalTemporaryGrantProof || grantExpiresAt !== metadata.originalTemporaryGrantExpiresAt + || !actor.policy.scope.operationIds.includes("uptime.speed-test") || ids.some(id => !actor.policy.scope.serverIds.includes(id)) + || grant && (!grant.scope.operationIds.includes("uptime.speed-test") || ids.some(id => !grant!.scope.serverIds.includes(id)))) deny() + return actor + } + // No cached authority: fresh private authorizers bracket files and + // the fixed SQL. A closing SQL/files fence also catches source or + // credential drift caused DURING the last authorizer's own await. + // Three file fences: BEFORE each bootstrap-capable authorizer and + // AFTER all closing read-only SQL/portal work. Intervening fixed + // queries neither project credentials nor create MCP material. + await filesCurrent(checkLive) + await originCurrent() + await joinedSnapshot(); await filesCurrent(checkLive) + await originCurrent() + const closingActor = await joinedSnapshot() + // The portal cutoff also has a durable external file; SQL alone + // cannot see a revocation written during the last authorizer. + await assertPortalClaimsCurrent({ id: closingActor.ownerId, issuedAt: Date.parse(closingActor.ownerClaimsIssuedAt) }); checkLive() + await filesCurrent(checkLive) + } + const assertCurrent = (next: Readonly, checkLive: () => void) => assertSnapshot(next, checkLive, true) + const assertAuthorityCurrent = (next: Readonly, checkLive: () => void) => assertSnapshot(next, checkLive, false) + const source = originalEndpoints.find(endpoint => endpoint.id === command.srcServerId)!, destination = originalEndpoints.find(endpoint => endpoint.id === command.dstServerId)! + const addresses = async (endpoint: RoutingEndpoint): Promise[]> => { + let guardFailed = false + const guard = async () => { try { await assertCurrent(current, live) } catch (error) { guardFailed = true; throw error } } + await guard() + let result: unknown + const signal = current.signal ? AbortSignal.any([current.signal, authority.signal]) : authority.signal + try { result = await new MikrotikClient(endpoint, endpoint.id).get("/ip/address", Math.max(1, Math.min(10000, current.deadlineAt - Date.now())), signal, undefined, guard) } + catch { if (guardFailed) return deny(); await guard(); return [] } + await guard(); return z.array(z.record(z.string(), z.unknown())).max(10000).parse(result) + } + // Preserve existing order and fallbacks: destination first, then source. + const destinationRows = command.dstInterface ? await addresses(destination) : [] + const sourceRows = command.srcInterface ? await addresses(source) : [] + const dstAddress = command.dstInterface ? firstUsableIp(destinationRows, command.dstInterface) : destination.host + if (command.dstInterface && !dstAddress) throw new Error(`На интерфейсе назначения '${command.dstInterface}' нет активного IP (проверьте /ip/address — не disabled).`) + const srcAddress = command.srcInterface ? firstUsableIp(sourceRows, command.srcInterface) : source.host + const context = freeze({ command: sealedCommand, srcAddress, dstAddress, address: dstAddress || destination.host, + srcInterfaceAddress: command.srcInterface ? srcAddress || null : null, dstInterfaceAddress: command.dstInterface ? dstAddress || null : null }) + const nativeBody = { address: context.address, user: destination.username, password: destination.password, protocol: command.protocol, direction: command.direction, duration: command.durationSec + "s" } + const authorityGeneration = generation + ":" + metadata.operationVersion + ":" + metadata.policyVersion + const intent = freeze({ attemptId: run.attemptId, operationId: run.operationId, callerKind: "mcp-speed" as const, sourceServerId: source.id, + actualRemotePeer: { kind: "server" as const, serverId: destination.id }, credentialServerId: destination.id, + // Genuine metadata's JSON payload is checked by the private verifier; + // McpPrincipal's nominal TS interface alone lacks an index signature. + request: { command: sealedCommand, mcp: metadata } as unknown as BandwidthPeerExecutionIntent["request"], nativeBody, durationSec: command.durationSec, + // MikrotikClient.bandwidthTest's existing fixed timeout is 30_000ms. + socketTimeoutMs: 30000, authorityProof: sign("mcp-speed-authority:" + fingerprint({ metadata, command, run, generation })), authorityGeneration, expiresAt: authority.expiresAt }) + await assertCurrent(current, live) + const peers = new Map(originalEndpoints.map(endpoint => [endpoint.id, freeze({ peerRef: { kind: "server" as const, serverId: endpoint.id }, enabled: endpoint.enabled, + sourceProof: sign("endpoint:" + fingerprint({ generation, endpoint })), authorityGeneration, + endpoint: { host: endpoint.host, port: endpoint.port, useSsl: endpoint.useSsl, verifySsl: endpoint.verifySsl, username: endpoint.username, password: endpoint.password, routingEndpointEpoch: endpoint.routingEndpointEpoch } })])) + live(); const token = Object.freeze(Object.create(null)) as McpSpeedBandwidthOriginal + entries.set(token, { intent, context, peers, authority, end, signal: scope.signal, assertCurrent, assertAuthorityCurrent }); return token + }) + }) + }, + async readOriginal(token: unknown, scope: Readonly): Promise { return (await read(token, scope)).intent }, + /** Internal transport lifetime only. The genuine context is checked before + * exposing its fixed horizon and cancellation; this data cannot mint a cap. */ + async readLifetime(token: unknown, scope: Readonly): Promise> { + const entry = await read(token, scope) + return Object.freeze({ expiresAt: entry.intent.expiresAt, monotonicDeadline: entry.end, signal: entry.authority.signal }) + }, + /** Internal full speed workflow metadata only; no credentials/public DTO. */ + async readSpeedContext(token: unknown, scope: Readonly): Promise> { return (await read(token, scope)).context }, + async readPeer(token: unknown, rawPeer: BandwidthPeerRef, scope: Readonly): Promise> { + const peer = parseBandwidthPeerRef(rawPeer) + const entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry || peer.kind !== "server" || !entry.peers.has(peer.serverId)) return deny() + await read(token, scope); return entry.peers.get(peer.serverId)! + }, + /** Probe edits after a genuinely completed run affect history association, + * not the already sealed command. Actor/key/endpoint checks remain current. */ + async readPeerAfterCompletion(token: unknown, result: unknown, rawPeer: BandwidthPeerRef, rawScope: Readonly): Promise> { + const scope = scopeSnapshot(rawScope), peer = parseBandwidthPeerRef(rawPeer) + const entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry || peer.kind !== "server" || !entry.peers.has(peer.serverId)) return deny() + if (!isDeepStrictEqual(readBandwidthPeerCompletion(result, token), entry.intent)) return deny() + await window(scope, entry.authority, entry.end, entry.signal, (current, live) => entry.assertAuthorityCurrent(current, live)) + return entry.peers.get(peer.serverId)! + }, + }) + providers.add(provider); return provider +} diff --git a/backend/src/modules/mcp/speed-test-actions.test.ts b/backend/src/modules/mcp/speed-test-actions.test.ts new file mode 100644 index 00000000..b7e7b9c3 --- /dev/null +++ b/backend/src/modules/mcp/speed-test-actions.test.ts @@ -0,0 +1,83 @@ +/** Thin adapter only: mocked existing speed service and metadata query. No + * PostgreSQL, RouterOS, new runtime, authority registry or network effects. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { beforeEach, mock, test } from "node:test" +import type { add } from "./actions.js" +import type { SpeedTestRunParams, SpeedTestRunResult } from "../../services/uptime-speed-service.js" + +const epochs=[randomUUID(),randomUUID()],queries:Array<{sql:string;values:unknown}>=[],calls:SpeedTestRunParams[]=[] +let rows:unknown[],failure:Error|undefined,returned:SpeedTestRunResult|undefined +mock.module("../../db/index.js",{namedExports:{pool:{async query(sql:string,values:unknown){queries.push({sql,values});return {rows:structuredClone(rows)}}}}}) +mock.module("../../services/uptime-speed-service.js",{namedExports:{async runBandwidthSpeedTest(input:SpeedTestRunParams){calls.push(structuredClone(input));if(failure)throw failure;return returned??{runId:input.runId,txAvgMbps:9,rxAvgMbps:8,txSeries:[1,2,3],rxSeries:[4,5],afterBtPing:{rttMs:2,lossPct:0,error:null}}}}}) +const {registerSpeedTestActions,mcpSpeedTestSchema}=await import("./speed-test-actions.js"),{findMcpService}=await import("./service-registry.js") +let registration:Parameters|undefined +registerSpeedTestActions((...args:Parameters)=>{registration=args}) +assert(registration) +const [operation,tool,_title,_section,permission,effect,targetScope,schema,handlers]=registration +const service=findMcpService("uptime.speed.test")!;assert(service) +const context={phase:"forward" as const,operationId:randomUUID(),stepIndex:0} +const input=()=>({srcServerId:2,dstServerId:1}) +async function prepare(raw:Record=input()){ + const evidence=await handlers.prepare!(schema.parse(raw),{} as never) + const step=evidence.steps[0];assert.equal(step.kind,"service");assert(step.kind==="service") + return {evidence,sealed:step.input} +} +beforeEach(()=>{queries.length=0;calls.length=0;rows=[{id:1,enabled:true,routingEndpointEpoch:epochs[0]},{id:2,enabled:true,routingEndpointEpoch:epochs[1]}];failure=undefined;returned=undefined}) + +test("public schema and ordinary registration bound duration/rate/pair, no caller run/probe/credentials",()=>{ + assert.equal(operation,"uptime.speed.test");assert.equal(tool,"mm_speed_test_plan");assert.equal(permission,"mm:uptime:write");assert.equal(effect,"diagnostic");assert.equal(targetScope,"servers") + assert.deepEqual(mcpSpeedTestSchema.parse(input()),{...input(),srcInterface:"",dstInterface:"",protocol:"tcp",direction:"both",durationSec:5,maxMbps:10}) + for(const bad of [{srcServerId:1,dstServerId:1},{...input(),srcServerId:0},{...input(),durationSec:2},{...input(),durationSec:11},{...input(),maxMbps:0},{...input(),maxMbps:1001},{...input(),maxMbps:1.5},{...input(),protocol:"sctp"},{...input(),direction:"up"},{...input(),srcInterface:"x".repeat(201)},{...input(),runId:randomUUID()},{...input(),probeId:"probe"},{...input(),password:"PRIVATE"}])assert.equal(schema.safeParse(bad).success,false) + assert.equal(queries.length,0);assert.equal(calls.length,0);assert.equal(service.cancellableDiagnostic,undefined) +}) + +test("prepare binds both server scopes/objects and epoch in one public pair query, human approval and sealed UUID",async()=>{ + assert.deepEqual(handlers.target!(input(),{} as never),{scope:"servers",serverIds:[1,2]}) + const {evidence,sealed}=await prepare() + assert.deepEqual(evidence.target,{scope:"servers",serverIds:[1,2]});assert.deepEqual(evidence.objects,[{kind:"server",id:"1",serverId:1},{kind:"server",id:"2",serverId:2}]);assert.deepEqual(evidence.snapshots,[]) + assert.equal(evidence.requiresHuman,true);assert.equal(evidence.steps.length,1);assert.equal(evidence.steps[0].kind,"service");assert.equal(calls.length,0) + assert.match(String(sealed.runId),/^[a-f0-9-]{36}$/);assert.deepEqual(sealed.expectedEndpoints,rows);assert.equal(queries.length,1) + assert.equal(queries[0].sql,'SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM servers WHERE id=ANY($1::int[]) ORDER BY id');assert.deepEqual(queries[0].values,[[1,2]]) + assert(!queries[0].sql.includes("password"));assert.equal(evidence.warnings?.length,2) + const next=await prepare();assert.notEqual(next.sealed.runId,sealed.runId) +}) + +test("prepare/current reject missing or disabled peers, and current repeats only one paired metadata read",async()=>{ + for(const invalid of [[{id:1,enabled:true,routingEndpointEpoch:epochs[0]}],[{id:1,enabled:true,routingEndpointEpoch:epochs[0]},{id:2,enabled:false,routingEndpointEpoch:epochs[1]}]]){rows=invalid;await assert.rejects(prepare(),(error:unknown)=>!!error&&typeof error==="object"&&"code"in error&&error.code==="DEVICE_UNAVAILABLE")} + rows=[{id:1,enabled:true,routingEndpointEpoch:epochs[0]},{id:2,enabled:true,routingEndpointEpoch:epochs[1]}] + const {sealed,evidence}=await prepare(),before=queries.length + assert.deepEqual(await service.current(sealed),evidence.before);assert.equal(queries.length-before,1);assert.equal(calls.length,0) +}) + +test("forward execute dispatches existing service exactly once with sealed runId and caps; verification and reconcile never replay",async()=>{ + const {sealed}=await prepare(),before=queries.length + const output=await service.execute(sealed,context) + assert.equal(calls.length,1);assert.deepEqual(calls[0],{runId:sealed.runId,srcServerId:2,dstServerId:1,srcInterface:"",dstInterface:"",protocol:"tcp",direction:"both",durationSec:5,maxMbps:10}) + assert.equal(queries.length-before,1);assert.equal(await service.verify!(sealed,output),true) + assert.equal(await service.verify!(sealed,{completed:true,runId:randomUUID()}),false);assert.equal(await service.verify!(sealed,{completed:false,runId:sealed.runId}),false) + assert.deepEqual(await service.reconcile!(sealed,async()=>{}),{matchesDesiredState:false,matchesOriginalState:false});assert.equal(calls.length,1) +}) + +test("execute refuses missing/recovery context, unsealed runId and changed endpoint epoch before native service",async()=>{ + const {sealed}=await prepare() + await assert.rejects(service.execute(sealed));await assert.rejects(service.execute(sealed,{phase:"recovery",operationId:context.operationId}));await assert.rejects(service.execute({...sealed,runId:"model-id"},context)) + rows=[{id:1,enabled:true,routingEndpointEpoch:randomUUID()},{id:2,enabled:true,routingEndpointEpoch:epochs[1]}] + await assert.rejects(service.execute(sealed,context),(error:unknown)=>!!error&&typeof error==="object"&&"code"in error&&error.code==="STATE_CONFLICT");assert.equal(calls.length,0) +}) + +test("public result is bounded and strips raw RouterOS/endpoint/credentials/errors; wrong history ID is refused",async()=>{ + const {sealed}=await prepare() + returned={createdAt:new Date().toISOString(),runId:String(sealed.runId),srcInterface:null,dstInterface:null,address:"PRIVATE_HOST",srcAddress:"PRIVATE_HOST",dstAddress:"PRIVATE_HOST",srcInterfaceAddress:null,dstInterfaceAddress:null,protocol:"tcp",direction:"both",durationSec:5, + raw:[{password:"PRIVATE_PASSWORD",address:"PRIVATE_HOST"}],txAvgMbps:Infinity,rxAvgMbps:7,txSeries:Array(1000).fill(5),rxSeries:[NaN,-1,4],afterBtPing:{rttMs:NaN,lossPct:150,error:"PRIVATE_ERROR_STACK"}} + const result=await service.execute(sealed,context) as {txAvgMbps:null;txSeries:number[];rxSeries:number[];afterBtPing:{error:boolean;rttMs:null;lossPct:null}} + assert.equal(result.txAvgMbps,null);assert.equal(result.txSeries.length,100);assert.deepEqual(result.rxSeries,[4]);assert.deepEqual(result.afterBtPing,{rttMs:null,lossPct:null,error:true}) + const text=JSON.stringify(result);for(const secret of ["PRIVATE_PASSWORD","PRIVATE_HOST","PRIVATE_ERROR_STACK","raw","password","stack"])assert.equal(text.includes(secret),false) + returned={...returned,runId:randomUUID()};await assert.rejects(service.execute(sealed,context));assert.equal(calls.length,2) +}) + +test("persistence UNKNOWN propagates after one service call without fallback, retry or optimistic completion",async()=>{ + const {sealed}=await prepare();failure=Object.assign(Error("persistence acknowledgement uncertain"),{code:"SPEED_RUN_PERSISTENCE_UNKNOWN"}) + await assert.rejects(service.execute(sealed,context),error=>error===failure);assert.equal(calls.length,1) + assert.deepEqual(await service.reconcile!(sealed,async()=>{}),{matchesDesiredState:false,matchesOriginalState:false});assert.equal(calls.length,1) +}) diff --git a/backend/src/modules/mcp/speed-test-actions.ts b/backend/src/modules/mcp/speed-test-actions.ts new file mode 100644 index 00000000..f9b2a880 --- /dev/null +++ b/backend/src/modules/mcp/speed-test-actions.ts @@ -0,0 +1,62 @@ +/** Ordinary human-planned RouterOS bandwidth-test. Common MCP runtime owns + * authorization, sealed input, locks, idempotency, audit and unknown outcomes. */ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { runBandwidthSpeedTest, type SpeedTestRunResult } from "../../services/uptime-speed-service.js" +import { registerMcpService } from "./service-registry.js" +import { McpError } from "./security.js" +import type { add } from "./actions.js" + +const sid=z.number().int().positive().safe() +export const mcpSpeedTestSchema=z.object({ + srcServerId:sid,dstServerId:sid, + srcInterface:z.string().trim().max(200).refine(value=>!/[\x00-\x1f\x7f]/.test(value)).default(""), + dstInterface:z.string().trim().max(200).refine(value=>!/[\x00-\x1f\x7f]/.test(value)).default(""), + protocol:z.enum(["tcp","udp"]).default("tcp"),direction:z.enum(["both","transmit","receive"]).default("both"), + durationSec:z.number().int().min(3).max(10).default(5),maxMbps:z.number().int().min(1).max(1000).default(10), +}).strict().refine(value=>value.srcServerId!==value.dstServerId,"Источник и назначение должны отличаться") +const endpointSchema=z.object({id:sid,enabled:z.literal(true),routingEndpointEpoch:z.string().uuid()}).strict() +const serviceSchema=mcpSpeedTestSchema.safeExtend({runId:z.string().uuid(),expectedEndpoints:z.array(endpointSchema).length(2)}) +const ids=(input:{srcServerId:number;dstServerId:number})=>[input.srcServerId,input.dstServerId].sort((a,b)=>a-b) +async function currentPair(input:{srcServerId:number;dstServerId:number}){ + const wanted=ids(input) + const result=await pool.query('SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM servers WHERE id=ANY($1::int[]) ORDER BY id',[wanted]) + const parsed=z.array(endpointSchema).length(2).safeParse(result.rows) + if(!parsed.success||parsed.data.some((row,index)=>row.id!==wanted[index]))throw new McpError("DEVICE_UNAVAILABLE","Оба устройства должны существовать и быть включены",409) + return {servers:parsed.data} +} +const number=(value:unknown,max=Number.MAX_VALUE)=>typeof value==="number"&&Number.isFinite(value)&&value>=0&&value<=max?value:null +const samples=(values:unknown)=>Array.isArray(values)?values.slice(0,100).map(value=>number(value)).filter((value):value is number=>value!==null):[] +function summary(result:SpeedTestRunResult,input:z.infer){ + if(result.runId!==input.runId)throw new McpError("MEASUREMENT_UNKNOWN","Завершённый запуск скорости не подтверждён",409) + return {completed:true,runId:input.runId,srcServerId:input.srcServerId,dstServerId:input.dstServerId,protocol:input.protocol,direction:input.direction,durationSec:input.durationSec,maxMbps:input.maxMbps, + txAvgMbps:number(result.txAvgMbps),rxAvgMbps:number(result.rxAvgMbps),txSeries:samples(result.txSeries),rxSeries:samples(result.rxSeries), + afterBtPing:{rttMs:number(result.afterBtPing?.rttMs),lossPct:number(result.afterBtPing?.lossPct,100),error:!!result.afterBtPing?.error}} +} +export function registerSpeedTestActions(register:typeof add){ + registerMcpService("uptime.speed.test",{ + current:async raw=>currentPair(serviceSchema.parse(raw)), + execute:async(raw,context)=>{ + if(context?.phase!=="forward"||!z.string().uuid().safeParse(context.operationId).success)throw new McpError("INVALID_EXECUTION_CONTEXT","Запуск скорости требует подтверждённого MCP плана",409) + const input=serviceSchema.parse(raw),current=await currentPair(input) + if(JSON.stringify(current.servers)!==JSON.stringify(input.expectedEndpoints))throw new McpError("STATE_CONFLICT","Подключения устройств изменились; подготовьте новый план",409) + const params={runId:input.runId,srcServerId:input.srcServerId,dstServerId:input.dstServerId,srcInterface:input.srcInterface,dstInterface:input.dstInterface,protocol:input.protocol,direction:input.direction,durationSec:input.durationSec,maxMbps:input.maxMbps} + // Exactly one call. Persistence uncertainty propagates to the common + // runtime's UNKNOWN path; neither this adapter nor reconcile replays it. + return summary(await runBandwidthSpeedTest(params),input) + }, + verify:async(raw,result)=>{const input=serviceSchema.parse(raw);return !!result&&typeof result==="object"&&"completed"in result&&result.completed===true&&"runId"in result&&result.runId===input.runId}, + reconcile:async()=>({matchesDesiredState:false,matchesOriginalState:false}), + }) + register("uptime.speed.test","mm_speed_test_plan","Проверка скорости между MikroTik","uptime","mm:uptime:write","diagnostic","servers",mcpSpeedTestSchema,{ + version:1, + target:input=>({scope:"servers",serverIds:ids(mcpSpeedTestSchema.parse(input))}), + prepare:async raw=>{ + const input=mcpSpeedTestSchema.parse(raw),serverIds=ids(input),before=await currentPair(input) + return {target:{scope:"servers",serverIds},objects:serverIds.map(serverId=>({kind:"server",id:String(serverId),serverId})),snapshots:[],before,requiresHuman:true,mutationCost:1,verificationScope:"acknowledgement", + steps:[{kind:"service",service:"uptime.speed.test",input:{...input,runId:randomUUID(),expectedEndpoints:before.servers}}], + warnings:[`RouterOS bandwidth-test: ${input.durationSec} с, лимит ${input.maxMbps} Мбит/с в каждом направлении.`,"Тест создаёт нагрузку на канал и CPU обоих устройств. Повтор после неопределённого результата выполняется только новым подтверждённым запуском."]} + }, + }) +} diff --git a/backend/src/modules/mcp/state-file-publish.test.ts b/backend/src/modules/mcp/state-file-publish.test.ts new file mode 100644 index 00000000..803a4b62 --- /dev/null +++ b/backend/src/modules/mcp/state-file-publish.test.ts @@ -0,0 +1,54 @@ +import assert from "node:assert/strict" +import { mkdtemp, readFile, writeFile, rename, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import path from "node:path" +import { spawn } from "node:child_process" +import { publishStateFile } from "./state-file-publish.js" + +const directory = await mkdtemp(path.join(tmpdir(), "mcp-state-publish-")), target = path.join(directory, "receipt.json"), temporary = path.join(directory, "receipt.tmp") +const busy = Object.assign(Error("owned sharing violation"), { code: "EPERM" }) +try { + await writeFile(target, '{"state":"running"}'); await writeFile(temporary, '{"state":"applied"}') + let calls = 0 + const sleeps: number[] = [] + await publishStateFile(temporary, target, { platform: "win32", sleep: async ms => { sleeps.push(ms); assert.equal(JSON.parse(await readFile(target, "utf8")).state, "running") }, rename: async (from, to) => { calls++; if (calls < 3) throw busy; await rename(from, to) } }) + assert.equal(calls, 3); assert.deepEqual(sleeps, [10, 20]); assert.equal(JSON.parse(await readFile(target, "utf8")).state, "applied") + await writeFile(temporary, '{"state":"unknown"}') + calls = 0; sleeps.length = 0 + await assert.rejects(publishStateFile(temporary, target, { platform: "win32", rename: async () => { calls++; throw busy }, sleep: async ms => { sleeps.push(ms) } }), error => error === busy) + assert.equal(calls, 8); assert.equal(sleeps.reduce((sum, ms) => sum + ms, 0), 710) + assert.equal(JSON.parse(await readFile(target, "utf8")).state, "applied"); assert.equal(JSON.parse(await readFile(temporary, "utf8")).state, "unknown") + for (const [platform, code] of [["linux", "EPERM"], ["win32", "ENOSPC"], ["win32", "ENOENT"], ["win32", "EEXIST"]] as const) { + calls = 0 + await assert.rejects(publishStateFile(temporary, target, { platform, rename: async () => { calls++; throw Object.assign(Error("owned terminal failure"), { code }) }, sleep: async () => { assert.fail("No retries for a terminal error") } })) + assert.equal(calls, 1) + } + if (process.platform === "win32") { + // A real Windows handle which allows reads/writes but denies replacement. + // Node's own fs.open uses FILE_SHARE_DELETE and cannot create this case. + const script = path.join(directory, "hold-receipt.ps1") + await writeFile(script, `param([string]$Target) +$ErrorActionPreference='Stop' +$held=[IO.File]::Open($Target,[IO.FileMode]::Open,[IO.FileAccess]::Read,[IO.FileShare]::ReadWrite) +try { [Console]::WriteLine('locked'); [Console]::Out.Flush(); [Threading.Thread]::Sleep(300) } finally { $held.Dispose() } +`) + await writeFile(temporary, '{"state":"published-after-sharing-lock"}') + const child = spawn(path.join(process.env.SystemRoot ?? "C:\\Windows", "System32/WindowsPowerShell/v1.0/powershell.exe"), ["-NoProfile", "-NonInteractive", "-File", script, target], { windowsHide: true, stdio: ["ignore", "pipe", "pipe"] }) + const terminal = new Promise(resolve => { child.once("close", code => resolve(code ?? -1)); child.once("error", () => resolve(-1)) }) + try { + await new Promise((resolve, reject) => { + let output = "" + const timer = setTimeout(() => { child.kill(); reject(Error("Windows sharing fixture did not become ready")) }, 5000) + child.stdout.on("data", data => { output += data; if (output.includes("locked")) { clearTimeout(timer); resolve() } }) + child.once("error", error => { clearTimeout(timer); reject(error) }) + child.once("exit", code => { clearTimeout(timer); if (!output.includes("locked")) reject(Error("Windows sharing fixture stopped: " + code)) }) + }) + assert.equal(JSON.parse(await readFile(target, "utf8")).state, "applied") + const started = Date.now() + await publishStateFile(temporary, target) + assert.ok(Date.now() - started >= 100, "Publication waited for the actual file-sharing lock") + assert.equal(JSON.parse(await readFile(target, "utf8")).state, "published-after-sharing-lock") + } finally { assert.equal(await terminal, 0) } + } +} finally { await rm(directory, { recursive: true, force: true }) } +console.log("State publication: atomic replacement only, bounded Windows sharing retries, old receipt retained on failure, no retry of terminal errors or exclusive claims") diff --git a/backend/src/modules/mcp/state-file-publish.ts b/backend/src/modules/mcp/state-file-publish.ts new file mode 100644 index 00000000..3b10cfb5 --- /dev/null +++ b/backend/src/modules/mcp/state-file-publish.ts @@ -0,0 +1,19 @@ +import { rename } from "node:fs/promises" +import { setTimeout as delay } from "node:timers/promises" + +/** Publish an already fsynced state file. Windows readers/scanners can briefly + * deny replacement of the destination. Retry only this atomic rename: never + * remove the old receipt, rewrite the payload, or repeat the guarded operation. */ +export async function publishStateFile(temporary: string, target: string, options: { + rename?: typeof rename; sleep?: (ms: number) => Promise; platform?: NodeJS.Platform +} = {}) { + const pauses = [10, 20, 40, 80, 160, 200, 200] + for (let attempt = 0; ; attempt++) { + try { await (options.rename ?? rename)(temporary, target); return } + catch (error) { + const code = error && typeof error === "object" && "code" in error ? error.code : null + if ((options.platform ?? process.platform) !== "win32" || !["EPERM", "EACCES", "EBUSY"].includes(String(code)) || attempt >= pauses.length) throw error + await (options.sleep ?? delay)(pauses[attempt]) + } + } +} diff --git a/backend/src/modules/mcp/stdio-terminal-budget.ts b/backend/src/modules/mcp/stdio-terminal-budget.ts new file mode 100644 index 00000000..969a4461 --- /dev/null +++ b/backend/src/modules/mcp/stdio-terminal-budget.ts @@ -0,0 +1,122 @@ +import { AsyncLocalStorage } from "node:async_hooks" +import { McpServer, STDIO_DEFAULT_MAX_BUFFER_SIZE, isJSONRPCRequest } from "@modelcontextprotocol/server" +import { MCP_CALL_RESULT_BYTES } from "./result-budget.js" +import { measureMcpTerminalEnvelope } from "./flow-envelope-allowance.js" +import { boundedSdkTerminalRefusal } from "./sdk-terminal-budget.js" + +type Transport = Parameters[0] +type Message = Parameters[0] +type Info = ConstructorParameters[0] +type RequestScope = { kind: "request"; id: string | number; idBytes?: number; callbackEntered: boolean } +type Scope = RequestScope | { readonly kind: "forwarded" } +type Decision = Readonly<{ source: "connected-stdio" | "entry-stdio"; reason: "byte-budget" | "not-json"; encodedBytesMeasured?: number }> + +function safeAdd(a: number, b: number) { + const value = a + b + if (!Number.isSafeInteger(a) || !Number.isSafeInteger(b) || a < 0 || b < 0 || !Number.isSafeInteger(value)) throw Error("Unproved STDIO arithmetic") + return value +} +/** Current bridge/installed-SDK terminal component only. No request registry, + * notification cap, input limit reduction, operation retry or heap guarantee. */ +export function createStdioSdkTerminalGuard(serverInfo: Info) { + const context = new AsyncLocalStorage() + const forwarded = Object.freeze({ kind: "forwarded" as const }) + const channels = new WeakSet(), wires = new WeakSet() + // ReadBuffer enforces raw bytes before UTF8 decoding. One invalid raw byte + // can become U+FFFD (3 UTF8 bytes); keep the existing 10MiB input contract. + const maxEncodedIdBytes = safeAdd(safeAdd(STDIO_DEFAULT_MAX_BUFFER_SIZE, STDIO_DEFAULT_MAX_BUFFER_SIZE), STDIO_DEFAULT_MAX_BUFFER_SIZE + 2) + const largestEnvelope = measureMcpTerminalEnvelope(maxEncodedIdBytes, "modern", serverInfo) + const maximumJsonRpcBytes = safeAdd(MCP_CALL_RESULT_BYTES, largestEnvelope.jsonRpcEnvelopeBytes) + const plan = Object.freeze({ inputBufferBytes: STDIO_DEFAULT_MAX_BUFFER_SIZE, maxEncodedIdBytes, + b1BodyAllowanceBytes: MCP_CALL_RESULT_BYTES, maximumJsonRpcBytes, maximumStdioLineBytes: safeAdd(maximumJsonRpcBytes, 1), modernStampBytes: largestEnvelope.modernStamp }) + let lastDecision: Decision | undefined + let decisions = 0, unmeasuredChannelPasses = 0, mismatchedCallbacks = 0 + const increment = (value: number) => Math.min(2147483647, value + 1) + const note = (decision: Decision) => { decisions = increment(decisions); lastDecision = Object.freeze(decision) } + const idBytes = (id: unknown): number | undefined => { + // This measures admitted IDs, never introduces a new validity rule. + try { + const encoded = JSON.stringify(id) + if (encoded === undefined) return undefined + const bytes = Buffer.byteLength(encoded, "utf8") + return bytes > 0 && bytes <= maxEncodedIdBytes ? bytes : undefined + } catch { return undefined } + } + const boundTerminal = (message: Message, bytes: number, source: Decision["source"]): Message => { + // Never classify notifications or server-originated requests by ID alone. + if ("method" in message || !("result" in message || "error" in message)) return message + // Conservative maximum of the two actual codecs (modern adds the stamp). + // Legacy gets the same 122-byte identity headroom; HTTP/Flow stays separate. + const maximum = safeAdd(MCP_CALL_RESULT_BYTES, measureMcpTerminalEnvelope(bytes, "modern", serverInfo).jsonRpcEnvelopeBytes) + let reason: Decision["reason"] = "byte-budget" + try { + const encoded = JSON.stringify(message) + if (encoded === undefined) throw Error("Not a JSON message") + const measured = Buffer.byteLength(encoded, "utf8") + if (measured <= maximum) return message + note({ source, reason, encodedBytesMeasured: measured }) + } catch { reason = "not-json"; note({ source, reason }) } + return boundedSdkTerminalRefusal(message.id, "error" in message ? message.error.code : -32603, reason) + } + const channelMessage = (message: Message): Message => { + const scope = context.getStore() + // ORIGINAL first: no message inspection/serialization after callback or if + // correlation is unknown. No actual result is turned into budget failure. + if (!scope || scope.kind !== "request" || scope.callbackEntered || scope.idBytes === undefined) { + unmeasuredChannelPasses = increment(unmeasuredChannelPasses) + return message + } + if ("method" in message || message.id !== scope.id) return message + return boundTerminal(message, scope.idBytes, "connected-stdio") + } + return { + serverInfo, plan, + snapshot: () => ({ decisions, unmeasuredChannelPasses, mismatchedCallbacks, lastDecision }), + protectCallback(id: unknown) { + const scope = context.getStore() + // Protect BEFORE comparing IDs or issuing remote IO. A lost context is + // original-pass in channelMessage, never a guessed no-effect outcome. + if (scope?.kind === "request") scope.callbackEntered = true + if (!scope || scope.kind !== "request" || scope.id !== id) mismatchedCallbacks = increment(mismatchedCallbacks) + }, + decorateWire(wire: Transport) { + if (wires.has(wire)) return + const send = wire.send.bind(wire) + wire.send = (message, options) => { + // Newly established by EVERY channel send, even if its upstream + // request context was lost. Installed channel forwards synchronously. + if (context.getStore() !== undefined) return send(message, options) + if ("method" in message || !("result" in message || "error" in message)) return send(message, options) + const bytes = idBytes(message.id) + return send(bytes === undefined ? message : boundTerminal(message, bytes, "entry-stdio"), options) + } + wires.add(wire) + }, + decorateConnectedChannel(channel: Transport) { + if (channels.has(channel)) return + const receive = channel.onmessage, send = channel.send.bind(channel) + if (receive) channel.onmessage = (message, extra) => { + if (!isJSONRPCRequest(message)) return receive(message, extra) + return context.run({ kind: "request", id: message.id, idBytes: idBytes(message.id), callbackEntered: false }, () => receive(message, extra)) + } + channel.send = (message, options) => { + const originalOrBounded = channelMessage(message) + // Keep all channel output outside entry-owned classification, including + // unknown/protected output and modern subscription notifications. + return context.run(forwarded, () => send(originalOrBounded, options)) + } + channels.add(channel) + }, + } +} +export type StdioSdkTerminalGuard = ReturnType + +export class StdioSdkBudgetedMcpServer extends McpServer { + constructor(private readonly guard: StdioSdkTerminalGuard) { super(guard.serverInfo) } + override async connect(transport: Transport): Promise { + // SDK installs onmessage inside connect. STDIO channel.start is a no-op; + // serveStdio delivers only after this awaited connection returns. + await super.connect(transport) + this.guard.decorateConnectedChannel(transport) + } +} diff --git a/backend/src/modules/mcp/store.ts b/backend/src/modules/mcp/store.ts new file mode 100644 index 00000000..11c09a43 --- /dev/null +++ b/backend/src/modules/mcp/store.ts @@ -0,0 +1,255 @@ +import { randomUUID } from "node:crypto" +import type { PoolClient, QueryResultRow } from "pg" +import { z } from "zod" +import { + mcpConnectionPolicySchema, mcpConnectionSchema, mcpSettingsSchema, mcpTemporaryGrantSchema, + type McpConnection, type McpConnectionCreate, type McpConnectionPolicy, +} from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import { SYSTEM_DATABASE_OPERATION_LOCK } from "../../db/operation-lock.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { admitPartitionWriter, withPartitionWrite } from "../../services/partition-write-transaction.js" +import { accessGeneration, mcpStateDirectory, canonicalMcpUrl, McpError, randomToken, redact, tokenDigest } from "./security.js" +import { assertPortalClaimsCurrent, lockPortalOwner, readCurrentPrincipalSecuritySource } from "./portal-revocation.js" + +export interface ConnectionRow extends QueryResultRow { + id: string; name: string; owner_id: string; owner_permissions: string[]; policy: McpConnectionPolicy; + generation: string; created_at: Date; updated_at: Date; last_used_at: Date | null + owner_valid_until: Date + owner_claims_issued_at: Date +} +function ownerValidUntil(actor: AuthUser) { return new Date(Math.min(actor.expiresAt ?? Date.now() + 900000, Date.now() + 86400000)) } +export interface McpPrincipal { connectionId: string; credentialId: string; credentialExpiresAt: string } +export function intersectGrant(connection: ConnectionRow, grant?: z.infer): ConnectionRow { + if (!grant) return connection + return { ...connection, policy: { ...connection.policy, scope: { + serverIds: connection.policy.scope.serverIds.filter(id => grant.scope.serverIds.includes(id)), + operationIds: connection.policy.scope.operationIds.filter(id => grant.scope.operationIds.includes(id)), + allowGlobal: connection.policy.scope.allowGlobal && grant.scope.allowGlobal, + } } } +} +/** One current principal/grant read. The raw source is private metadata for + * same-invocation proof binding, never a reusable authority or public DTO. */ +export async function effectiveMcpConnectionSource(principal: McpPrincipal, client: Pick = pool) { + const { connection, grantRows: rows, grantOversized } = await currentPrincipalSource(principal, client, true) + if (!rows.length) return { connection, grantRows: rows } + if (rows.length !== 1) throw new McpError("GRANT_INACTIVE", "Неоднозначный временный доступ") + if (grantOversized) throw new McpError("STATE_UNAVAILABLE", "Текущий источник прав недоступен", 503) + const { credentialId: _credential, ...payload } = rows[0].payload as Record, grant = mcpTemporaryGrantSchema.parse(payload) + if (Date.parse(grant.startsAt) > Date.now() || Date.parse(grant.expiresAt) <= Date.now()) throw new McpError("GRANT_INACTIVE", "Временный доступ не действует", 403) + return { connection: intersectGrant(connection, grant), grantRows: rows } +} +export async function effectiveMcpConnection(principal: McpPrincipal, client: Pick = pool) { + return (await effectiveMcpConnectionSource(principal, client)).connection +} +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; + expires_at: Date; approved_by: string | null; created_at: Date +} +export interface StoredOperation extends QueryResultRow { + id: string; plan_id: string; connection_id: string; state: string; verification: string; + result: unknown; error_code: string | null; created_at: Date +} +export async function transaction(work: (client: PoolClient) => Promise): Promise { + const client = await pool.connect() + try { await client.query("BEGIN"); const result = await work(client); await client.query("COMMIT"); return result } + catch (error) { await client.query("ROLLBACK"); throw error } finally { client.release() } +} +export function connectionWrite(work: (client: PoolClient) => Promise): Promise { + // Keep domain errors from connection validation visible to the HTTP handler. + return transaction(async client => { + await admitPartitionWriter(client) + return work(client) + }) +} +export function requestAccountingWrite(work: (client: PoolClient) => Promise): Promise { + return transaction(async client => { + // POLL owns the partition key, so its status reads must use the database-operation key. + const { rows: [lock] } = await client.query<{ acquired: boolean }>( + "SELECT pg_catalog.pg_try_advisory_xact_lock_shared($1::bigint) AS acquired", [SYSTEM_DATABASE_OPERATION_LOCK]) + if (lock?.acquired !== true) throw new McpError("PROTOCOL_STORAGE_BUSY", "Выполняется операция с базой приложения", 503) + return work(client) + }) +} +export async function audit(client: Pick, actorId: string, action: string, connectionId: string | null, payload: unknown): Promise { + await client.query("INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)", [actorId, action, connectionId, JSON.stringify(redact(payload))]) +} +export async function getMcpSettings(retentionOwner?: PoolClient) { + const { rows: [row] } = await (retentionOwner ?? pool).query(`SELECT enabled,requests_retention_days AS "requestsRetentionDays", + audit_retention_days AS "auditRetentionDays" FROM mcp_settings WHERE id=1${retentionOwner ? " FOR SHARE" : ""}`) + if (!row) throw new McpError("STATE_UNAVAILABLE", "Настройки MCP отсутствуют; выполните миграции", 503) + return mcpSettingsSchema.parse(row) +} +export async function updateMcpSettings(patch: Partial>, actor: AuthUser) { + return withPartitionWrite(async ({ client }) => { + const { rows: [before] } = await client.query("SELECT * FROM mcp_settings WHERE id=1 FOR UPDATE") + await client.query("UPDATE mcp_settings SET enabled=COALESCE($1,enabled),requests_retention_days=COALESCE($2,requests_retention_days),audit_retention_days=COALESCE($3,audit_retention_days),updated_at=now() WHERE id=1", [patch.enabled, patch.requestsRetentionDays, patch.auditRetentionDays]) + await audit(client, actor.id, "settings.update", null, { before, patch }) + return { enabled: patch.enabled ?? before.enabled, requestsRetentionDays: patch.requestsRetentionDays ?? before.requests_retention_days, auditRetentionDays: patch.auditRetentionDays ?? before.audit_retention_days } + }) +} +export function publicConnection(row: ConnectionRow, generation: string): McpConnection { + return mcpConnectionSchema.parse({ + id: row.id, name: row.name, ownerId: row.owner_id, policy: row.policy, + createdAt: row.created_at.toISOString(), updatedAt: row.updated_at.toISOString(), + lastUsedAt: row.last_used_at?.toISOString() ?? null, generationValid: row.generation === generation, + ownerValidUntil: row.owner_valid_until.toISOString(), + }) +} +export async function listMcpConnections(actor: AuthUser): Promise { + const admin = hasPermission(actor.permissions, "mm:mcp:admin") + const { rows } = await pool.query("SELECT * FROM mcp_connections WHERE ($1::boolean OR owner_id=$2) ORDER BY created_at DESC LIMIT 500", [admin, actor.id]) + const generation = await accessGeneration() + return rows.map(row => publicConnection(row, generation)) +} +export async function getConnection(id: string, client: Pick = pool): Promise { + const { rows: [row] } = await client.query("SELECT * FROM mcp_connections WHERE id=$1", [id]) + if (!row) throw new McpError("NOT_FOUND", "Подключение не найдено", 404) + row.policy = mcpConnectionPolicySchema.parse(row.policy) + return row +} +export function assertConnectionVisible(row: ConnectionRow, actor: AuthUser): void { + 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) + 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 token = randomToken("bearer") + return connectionWrite(async client => { + await lockPortalOwner(client, actor.id) + await assertPortalClaimsCurrent(actor, client) + const { rows: [row] } = await client.query("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]) + 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) { + 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 => { + 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("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)]) + 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()) + }) +} +export async function setConnectionStatus(id: string, status: "active" | "paused" | "revoked", actor: AuthUser) { + return connectionWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [id]) + const row = await getConnection(id, client) + if (row.policy.status === "revoked" && status !== "revoked") throw new McpError("CONNECTION_REVOKED", "Отозванное подключение нельзя активировать", 409) + const policy = { ...row.policy, status, version: row.policy.version + 1 } + await client.query("UPDATE mcp_connections SET policy=$2::jsonb,updated_at=now() WHERE id=$1", [id, JSON.stringify(policy)]) + if (status !== "active") await client.query("UPDATE mcp_plans SET state='invalidated' WHERE connection_id=$1 AND state IN ('draft','awaiting-approval','approved')", [id]) + if (status === "revoked") await client.query("UPDATE mcp_credentials SET revoked_at=now() WHERE connection_id=$1 AND revoked_at IS NULL", [id]) + await audit(client, actor.id, "connection." + status, id, {}) + }) +} +export async function revalidateMcpOwner(id: string, actor: AuthUser) { + return connectionWrite(async client => { + await lockPortalOwner(client, actor.id) + await assertPortalClaimsCurrent(actor, client) + 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", "Права подтверждает владелец подключения") + const policy = { ...connection.policy, version: connection.policy.version + 1 } + const until = ownerValidUntil(actor) + const { rows: [row] } = await client.query("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)]) + await client.query("UPDATE mcp_plans SET state='invalidated' WHERE connection_id=$1 AND state IN ('draft','approved','awaiting-approval')", [id]) + await client.query("UPDATE mcp_routing_profiles SET enabled=false,state=state || '{\"pausedReason\":\"Требуется новый допуск после подтверждения прав владельца\"}'::jsonb WHERE connection_id=$1 AND enabled", [id]) + await audit(client, actor.id, "owner.revalidate", id, { permissions: actor.permissions, validUntil: until }) + return publicConnection(row, await accessGeneration()) + }) +} +export async function rotateConnectionToken(id: string, actor: AuthUser) { + const token = randomToken("bearer") + return connectionWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [id]) + const row = await getConnection(id, client) + if (row.policy.status === "revoked") throw new McpError("CONNECTION_REVOKED", "Доступ отозван", 409) + await client.query("UPDATE mcp_credentials SET revoked_at=now() WHERE connection_id=$1 AND kind='bearer' AND revoked_at IS NULL", [id]) + await client.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [randomUUID(), id, tokenDigest(token), row.policy.expiresAt]) + await audit(client, actor.id, "connection.token.rotate", id, {}) + return { token } + }) +} +export async function authenticateMcpToken(token: string): Promise { + if (!/^mmcp_(bearer|access)_[A-Za-z0-9_-]{43}$/.test(token)) throw new McpError("UNAUTHORIZED", "Требуется токен MCP", 401) + const { rows: [credential] } = await pool.query<{ id: string; connection_id: string; expires_at: Date; kind: string; audience: string | null }>("SELECT id,connection_id,expires_at,kind,audience FROM mcp_credentials WHERE digest=$1 AND revoked_at IS NULL AND expires_at>now() AND kind IN ('bearer','access')", [tokenDigest(token)]) + if (!credential) throw new McpError("UNAUTHORIZED", "Токен недействителен или отозван", 401) + if (credential.kind === "access" && credential.audience !== canonicalMcpUrl().href) throw new McpError("UNAUTHORIZED", "Токен выдан другому MCP resource", 401) + const row = await getConnection(credential.connection_id) + await assertPortalClaimsCurrent({ id: row.owner_id, issuedAt: row.owner_claims_issued_at.getTime() }) + if (row.generation !== await accessGeneration() || row.policy.status !== "active" || Date.parse(row.policy.expiresAt) <= Date.now() || row.owner_valid_until.getTime() <= Date.now()) throw new McpError("UNAUTHORIZED", "Подключение недействительно или требуется подтверждение прав владельца", 401) + if (!(await getMcpSettings()).enabled) throw new McpError("MCP_DISABLED", "MCP выключен", 503) + return { connectionId: row.id, credentialId: credential.id, credentialExpiresAt: credential.expires_at.toISOString() } +} +async function currentPrincipalSource(principal: McpPrincipal, client: Pick, includeGrants: boolean) { + const source = await readCurrentPrincipalSecuritySource(principal, client, includeGrants), { connection } = source + // Original final short-circuit order and fresh generation/clock checks remain. + // SQL credential expiry deliberately retains PostgreSQL now() semantics. + if (!source.credentialCurrent || !source.enabled || connection.generation !== await accessGeneration() || connection.policy.status !== "active" || Date.parse(connection.policy.expiresAt) <= Date.now() || connection.owner_valid_until.getTime() <= Date.now()) throw new McpError("UNAUTHORIZED", "Доступ MCP прекращён или требуется подтверждение прав владельца", 401) + return source +} +export async function validatePrincipal(principal: McpPrincipal, client: Pick = pool): Promise { + return (await currentPrincipalSource(principal, client, false)).connection +} + + +/** Fixed synchronous source exchange. Packets retire before any callback await. */ +declare const principalBoundaryBrand: unique symbol +export type PrincipalBoundaryPacket = Readonly<{ [principalBoundaryBrand]: true }> +export const fixedPrincipalBoundarySource = Object.freeze({ reader: readCurrentPrincipalSecuritySource, generation: accessGeneration, directory: mcpStateDirectory }) +type PrincipalBoundarySource = Awaited> +function freezePrincipalProjection(value: T): T { + if (value && typeof value === "object") { for (const child of Object.values(value)) freezePrincipalProjection(child); Object.freeze(value) } + return value +} +const principalBoundaryPackets = new WeakMap }>() +/** Only within the original synchronous packet callback. Exposes no client. */ +export function assertPrincipalBoundaryClient(packet: PrincipalBoundaryPacket, client: Pick) { + const entry = principalBoundaryPackets.get(packet) + if (!entry || entry.client !== client) throw new McpError("PERMISSION_DENIED", "Чужой или завершённый SQL источник MCP", 403) +} +export function principalBoundaryProjection(packet: PrincipalBoundaryPacket, expected: McpPrincipal, directory: string, effective: boolean) { + const entry = principalBoundaryPackets.get(packet) + if (!entry || entry.directory !== directory || entry.principal.connectionId !== expected.connectionId || entry.principal.credentialId !== expected.credentialId || entry.principal.credentialExpiresAt !== expected.credentialExpiresAt) throw new McpError("PERMISSION_DENIED", "Чужая или завершённая граница MCP", 403) + const source = entry.source + if (!source.credentialCurrent || !source.enabled || source.connection.generation !== entry.generation || source.connection.policy.status !== "active" || Date.parse(source.connection.policy.expiresAt) <= Date.now() || source.connection.owner_valid_until.getTime() <= Date.now() + || typeof source.credentialExpiresAt !== "string" || Date.parse(source.credentialExpiresAt) <= Date.now() || Date.parse(expected.credentialExpiresAt) <= Date.now()) throw new McpError("UNAUTHORIZED", "Источник MCP истёк", 401) + const raw = structuredClone(source.connection), rows = structuredClone(source.grantRows) + if (!effective || !rows.length) return freezePrincipalProjection({ connection: raw, grantRows: rows, generation: entry.generation }) + if (rows.length !== 1) throw new McpError("GRANT_INACTIVE", "Неоднозначный временный доступ") + if (source.grantOversized) throw new McpError("STATE_UNAVAILABLE", "Текущий источник прав недоступен", 503) + const { credentialId: _credential, ...payload } = rows[0].payload as Record, grant = mcpTemporaryGrantSchema.parse(payload) + if (Date.parse(grant.startsAt) > Date.now() || Date.parse(grant.expiresAt) <= Date.now()) throw new McpError("GRANT_INACTIVE", "Временный доступ не действует", 403) + return freezePrincipalProjection({ connection: intersectGrant(raw, grant), grantRows: rows, generation: entry.generation }) +} +export async function readSynchronousPrincipalBoundary(principal: McpPrincipal, client: Pick, consume: (packet: PrincipalBoundaryPacket) => T): Promise { + const directory = mcpStateDirectory(), source = await currentPrincipalSource(principal, client, true), generation = await accessGeneration() + if (directory !== mcpStateDirectory()) throw new McpError("STATE_CONFLICT", "Источник файлов MCP изменился", 409) + const packet = Object.freeze(Object.create(null)) as PrincipalBoundaryPacket + principalBoundaryPackets.set(packet, { principal: Object.freeze({ ...principal }), directory, source, generation, client }) + try { + const result = consume(packet) + if (result && typeof result === "object" && "then" in result) { void Promise.resolve(result).catch(() => {}); throw new McpError("STATE_CONFLICT", "Нужна синхронная граница MCP", 409) } + return result + } finally { principalBoundaryPackets.delete(packet) } +} diff --git a/backend/src/modules/mcp/telegram-settings-actions.ts b/backend/src/modules/mcp/telegram-settings-actions.ts new file mode 100644 index 00000000..2b48f5a6 --- /dev/null +++ b/backend/src/modules/mcp/telegram-settings-actions.ts @@ -0,0 +1,89 @@ +import { z } from "zod" +import { putTelegramSchema } from "@mmapp/contracts/alerts" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { updateTelegramSettings } from "../../services/alerts-service.js" +import { withApplicationMutationLock } from "../../services/config-mutation-lock.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, decryptSecret, privateSecretFingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +export const TELEGRAM_SECRET_PURPOSE="alerts-telegram-bot" +const fields=putTelegramSchema.omit({token:true}).extend({chatId:z.string().trim().max(200).refine(v=>!/[\u0000-\u001f\u007f]/.test(v)).optional()}).strict() +const schema=z.object({settings:fields.default({}),secretRef:z.string().uuid().nullable().optional()}).strict().refine(v=>Object.keys(v.settings).length>0||v.secretRef!==undefined,"Нет изменённых параметров Telegram") +type Row={botToken:string;chatId:string;messageThreadId:number|null;updatedAt:unknown} +const fail=(message:string,code="STATE_CONFLICT")=>new McpError(code,message,409) +async function authorize(input:Record,signal?:AbortSignal){ + if(signal?.aborted)throw fail("Подготовка отменена","READ_CANCELLED") + const principal=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict().parse(input.principal) + const current=await effectiveMcpConnection(principal) + if(current.owner_id!==input.ownerId || !current.policy.scope.allowGlobal || !hasPermission(current.policy.permissions,"mm:alerts:write") || !hasPermission(current.owner_permissions,"mm:alerts:write"))throw fail("Право настройки Telegram отозвано","PERMISSION_DENIED") + if(fingerprint(current.policy)!==input.policyProof)throw fail("Допуск изменился; подготовьте новый план","RESET_REQUIRED") + if(signal?.aborted)throw fail("Подготовка отменена","READ_CANCELLED") + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(r=>r.id) + if(ids.length>1000||ids.some(id=>!current.policy.scope.serverIds.includes(id)))throw fail("Настройка Telegram требует все серверы приложения","SERVER_DENIED") + if(signal?.aborted)throw fail("Подготовка отменена","READ_CANCELLED") + return ids +} +export { readSource as readTelegramMcpSource } +async function readSource(input:Record,signal?:AbortSignal){ + const ids=await authorize(input,signal) + const row=(await pool.query(`SELECT CASE WHEN octet_length(bot_token)<=4096 THEN bot_token ELSE NULL END AS "botToken",chat_id AS "chatId",message_thread_id AS "messageThreadId",updated_at AS "updatedAt" FROM alert_telegram_settings WHERE id=1`)).rows[0] + if(row&&(!z.string().max(4096).safeParse(row.botToken).success||!z.string().max(200).safeParse(row.chatId).success||!z.number().int().positive().max(2147483647).nullable().safeParse(row.messageThreadId).success))throw fail("Параметры Telegram не подтверждены","STATE_UNAVAILABLE") + let reference:{token:string;proof:string}|undefined + if(typeof input.secretRef==="string"){ + await authorize(input,signal) + const secret=(await pool.query("SELECT purpose,server_id,created_by,CASE WHEN octet_length(ciphertext)<=16384 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2",[input.secretRef,input.ownerId])).rows[0] + if(!secret||secret.server_id!==null||secret.purpose!==TELEGRAM_SECRET_PURPOSE||typeof secret.ciphertext!=="string")throw fail("Ссылка не выдана владельцу для токена Telegram","SECRET_UNAVAILABLE") + await authorize(input,signal) + let token:unknown;try{token=await decryptSecret(secret.ciphertext)}catch{throw fail("Секрет Telegram недоступен","SECRET_UNAVAILABLE")} + const parsed=z.string().trim().min(1).max(4096).refine(v=>!/[\s\u0000-\u001f\u007f]/.test(v)).safeParse(token) + if(!parsed.success)throw fail("Токен Telegram имеет неподдерживаемый формат","SECRET_UNAVAILABLE") + reference={token:parsed.data,proof:await privateSecretFingerprint(fingerprint(secret),"telegram-reference-v1")} + } + await authorize(input,signal) + return {row,ids,reference,publicState:{proof:await privateSecretFingerprint(fingerprint({row:row??null,serverIds:ids}),"telegram-state-v1"),referenceProof:reference?.proof??null}} +} +export function registerTelegramSettingsActions(register:typeof add){ + const current=async(input:Record)=>(await readSource(input)).publicState + registerMcpService("alerts.telegram.update",{current, + execute:async input=>withApplicationMutationLock(async()=>{ + const before=await readSource(input) + if(fingerprint(before.publicState)!==input.beforeFingerprint)throw fail("Telegram, секрет или каталог изменились после подтверждения") + const settings=fields.parse(input.settings), token=input.secretRef===null?"":before.reference?.token + const expected={botToken:token??before.row?.botToken??"",chatId:settings.chatId??before.row?.chatId??"",messageThreadId:settings.messageThreadId!==undefined?settings.messageThreadId:before.row?.messageThreadId??null} + let checks=0 + await updateTelegramSettings({...settings,...(token!==undefined?{token}:{})},async()=>{ + await assertVerifiedConfigurationAccess() + const latest=await readSource(input) + if(latest.publicState.referenceProof!==before.publicState.referenceProof || fingerprint(latest.ids)!==fingerprint(before.ids))throw fail("Ссылка на токен или каталог изменились перед записью") + if(before.row||checks===0){if(latest.publicState.proof!==before.publicState.proof)throw fail("Telegram изменился перед записью")} + else if(!latest.row||latest.row.botToken!==""||latest.row.chatId!==""||latest.row.messageThreadId!==null)throw fail("Начальная строка Telegram изменилась перед записью") + checks++ + }) + const after=await readSource(input) + if(!after.row||["botToken","chatId","messageThreadId"].some(key=>after.row![key as keyof Row]!==expected[key as keyof typeof expected]))throw fail("Сохранение Telegram не подтверждено; повтор запрещён","VERIFICATION_FAILED") + return {saved:true,proof:after.publicState.proof,telegram:{chatId:after.row.chatId,messageThreadId:after.row.messageThreadId,tokenConfigured:Boolean(after.row.botToken.trim())},messageSent:false} + }), + verify:async(input,result)=>{const receipt=result as {saved?:boolean;proof?:string}|null;return receipt?.saved===true&&(await current(input)).proof===receipt.proof}, + }) + register("alerts.telegram.update","mm_alerts_telegram_update_plan","Изменить Telegram: адресат, тема и отдельная ссылка на токен","alerts","mm:alerts:write","write","global",schema,{ + prepare:async(input,context)=>{ + if(context.signal?.aborted)throw fail("Подготовка отменена","READ_CANCELLED") + const request={...input,principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy)} + const source=await readSource(request,context.signal) + if(context.signal?.aborted)throw fail("Подготовка отменена","READ_CANCELLED") + const objects=[{kind:"alerts-telegram",id:"1"},...source.ids.map(serverId=>({kind:"server",id:String(serverId),serverId}))] + const protections=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'='alerts-telegram' AND object_ref->>'id'='1') LIMIT 1001",[source.ids.map(String)]) + if(protections.rows.length>1000)throw fail("Превышен предел защит Telegram","STATE_UNAVAILABLE") + for(const {object_ref:ref} of protections.rows)objects.push(ref) + if(context.signal?.aborted)throw fail("Подготовка отменена","READ_CANCELLED") + await authorize(request,context.signal) + return {target:{scope:"global"},objects,snapshots:[],before:source.publicState,requiresHuman:true,mutationCost:1, + steps:[{kind:"service",service:"alerts.telegram.update",input:{...request,beforeFingerprint:fingerprint(source.publicState)}}], + warnings:["Изменяются глобальные параметры будущих уведомлений. Настройка сама не отправляет сообщений.","Токен принимается только через выданную владельцу ссылку с purpose alerts-telegram-bot; secretRef=null очищает токен. Пропущенные поля сохраняются.","Изменение адресата или темы влияет на будущую отправку; требуется человеческое подтверждение."]} + }, + }) +} diff --git a/backend/src/modules/mcp/telegram-test-actions.ts b/backend/src/modules/mcp/telegram-test-actions.ts new file mode 100644 index 00000000..409ae37d --- /dev/null +++ b/backend/src/modules/mcp/telegram-test-actions.ts @@ -0,0 +1,59 @@ +import { z } from "zod" +import { testTelegramSchema } from "@mmapp/contracts/alerts" +import { pool } from "../../db/index.js" +import { performTelegramTest, resolveTelegramTest } from "../../services/alerts-telegram-test.js" +import { sendTelegramRequest } from "../../services/alerts-telegram-send.js" +import { assertVerifiedConfigurationAccess, executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { readTelegramMcpSource } from "./telegram-settings-actions.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +const preview=testTelegramSchema.shape.rulePreview.unwrap().extend({name:z.string().min(1).max(200),targets:z.array(z.string().min(1).max(200)).min(1).max(100),conditionLine:z.string().min(1).max(1000)}).strict() +const schema=testTelegramSchema.omit({token:true}).extend({chatId:z.string().trim().max(200).refine(v=>!/[\u0000-\u001f\u007f]/.test(v)).optional(),rulePreview:preview.optional(),secretRef:z.string().uuid().optional()}).strict() +function payload(input:Record,source:Awaited>){ + return resolveTelegramTest({...schema.parse({chatId:input.chatId,messageThreadId:input.messageThreadId,rulePreview:input.rulePreview,secretRef:input.secretRef}),...(source.reference?{token:source.reference.token}:{})},source.row??{botToken:"",chatId:"",messageThreadId:null}) +} +export function registerTelegramTestActions(register:typeof add){ + const current=async(input:Record)=>(await readTelegramMcpSource(input)).publicState + registerMcpService("alerts.telegram.test",{current, + execute:async input=>{ + const before=await readTelegramMcpSource(input),resolved=payload(input,before) + if(fingerprint(before.publicState)!==input.beforeFingerprint)throw new McpError("STATE_CONFLICT","Telegram или ссылка на токен изменились после подтверждения",409) + if(resolved.text!==input.messagePreview || fingerprint({chatId:resolved.chatId,messageThreadId:resolved.messageThreadId??null})!==fingerprint(input.destination))throw new McpError("STATE_CONFLICT","Текст или адресат не совпали с подтверждённым preview; подготовьте новый план",409) + const fence=async()=>{ + await assertVerifiedConfigurationAccess() + if(fingerprint((await readTelegramMcpSource(input)).publicState)!==input.beforeFingerprint)throw new McpError("STATE_CONFLICT","Telegram изменился перед отправкой",409) + } + return executeGuardedApplicationWrite({kind:"alerts-telegram-test",id:"1"},before.ids,async()=>{ + const result=await performTelegramTest({token:resolved.token,chatId:resolved.chatId,...(resolved.messageThreadId!==undefined?{messageThreadId:resolved.messageThreadId}:{}),...(input.rulePreview?{rulePreview:input.rulePreview}:{})},{ + readStored:async()=>{await fence();return before.row??{botToken:"",chatId:"",messageThreadId:null}}, + send:async request=>{ + const sent=await sendTelegramRequest(request.token,{chat_id:request.chatId,text:request.text,...(request.messageThreadId!==undefined?{message_thread_id:request.messageThreadId}:{})}) + if(!sent.ok&&sent.outcome==="unknown")throw new McpError("MUTATION_OUTCOME_UNKNOWN","Ответ Telegram не подтверждён; сообщение могло быть принято, повтор запрещён",502) + return sent + }, + },fence) + await assertVerifiedConfigurationAccess() + return {providerAccepted:result.ok,outcome:result.ok?"accepted":"rejected",deliveryVerified:false,attempts:1,configurationChanged:false} + }) + }, + verify:async(_input,result)=>(result as {providerAccepted?:boolean}|null)?.providerAccepted===true, + }) + register("alerts.telegram.test","mm_alerts_telegram_test_plan","Тестовая отправка Telegram одним запросом после подтверждения","alerts","mm:alerts:write","write","global",schema,{ + prepare:async(input,context)=>{ + const request={...input,principal:context.principal,ownerId:context.connection.owner_id,policyProof:fingerprint(context.connection.policy)} + const source=await readTelegramMcpSource(request,context.signal),resolved=payload(request,source) + if(resolved.text.length>4096||Buffer.byteLength(resolved.text)>16384)throw new McpError("INVALID_INPUT","Текст теста превышает предел Telegram",400) + const objects=[{kind:"alerts-telegram",id:"1"},{kind:"alerts-telegram-test",id:"1"},...source.ids.map(serverId=>({kind:"server",id:String(serverId),serverId}))] + const protections=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' IN ('alerts-telegram','alerts-telegram-test') LIMIT 1001",[source.ids.map(String)]) + if(protections.rows.length>1000)throw new McpError("STATE_UNAVAILABLE","Превышен предел защит Telegram",409) + for(const {object_ref:ref}of protections.rows)objects.push(ref) + if(context.signal?.aborted)throw new McpError("READ_CANCELLED","Подготовка отправки отменена",409) + if(fingerprint((await readTelegramMcpSource(request,context.signal)).publicState)!==fingerprint(source.publicState))throw new McpError("STATE_CONFLICT","Telegram изменился во время подготовки",409) + return {target:{scope:"global"},objects,snapshots:[],before:source.publicState,requiresHuman:true,mutationCost:1,verificationScope:"acknowledgement", + steps:[{kind:"service",service:"alerts.telegram.test",input:{...request,beforeFingerprint:fingerprint(source.publicState),destination:{chatId:resolved.chatId,messageThreadId:resolved.messageThreadId??null},messagePreview:resolved.text}}], + warnings:["Будет отправлено одно реальное тестовое сообщение указанному адресату; текст и адресат показаны в плане. Настройки и история срабатываний не изменяются.","Положительный ответ API подтверждает только приём Telegram, не доставку получателю. Потеря ответа оставляет unknown; автоматического повтора нет.","Новый подтверждённый тест после unknown может создать дубликат. Токен доступен только через сохранённую настройку или owner/purpose secretRef."]} + }, + }) +} diff --git a/backend/src/modules/mcp/telemetry-settings-configuration.owned.test.ts b/backend/src/modules/mcp/telemetry-settings-configuration.owned.test.ts new file mode 100644 index 00000000..8cf1e7fb --- /dev/null +++ b/backend/src/modules/mcp/telemetry-settings-configuration.owned.test.ts @@ -0,0 +1,119 @@ +/** Actual MCP settings admission/current/execute/verification against owned SQL. + * No native fixtures, mocked getters/writers, forged operation or origin state. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { setTimeout as delay } from "node:timers/promises" + +assert.equal(process.env.MMAPP_OWN_TELEMETRY_SETTINGS_CONFIGURATION, "true") +assert.equal(process.env.NODE_ENV, "test") +const target = new URL(process.env.DATABASE_URL ?? "") +assert(["postgres:", "postgresql:"].includes(target.protocol)) +assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") +assert(target.password); assert.equal(target.pathname, "/postgres") +assert(Number.isInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) +assert.equal(target.search, ""); assert.equal(target.hash, "") +assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) +assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") +assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "owned-telemetry-configuration-acceptance-secret" +process.env.AUTH_ISSUER = "https://owned-telemetry.test" + +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 { buildApp } = await import("../../index.js") +const { createMcpConnection, authenticateMcpToken, updateMcpSettings } = await import("./store.js") +const { invokeMcpTool, approvePlan, applyPlan, publicPlan, loadPlan } = await import("./runtime.js") +const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") +const actor = { id: "owned-telemetry-config-owner", email: "owned-telemetry@example.test", name: "Owned telemetry settings", + apps: ["mm"], permissions: ["mm:settings:admin", "mm:traffic:admin", "mm:uptime:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +const cases = [ + { table: "traffic_settings", operation: "traffic.settings.update", tool: "mm_traffic_settings_update_plan", settings: { enabled: false, intervalSec: 37 } }, + { table: "uptime_settings", operation: "uptime.settings.update", tool: "mm_uptime_settings_update_plan", settings: { enabled: false, resourcesEnabled: false, pingEnabled: false, speedEnabled: false, intervalSec: 317 } }, + { table: "servers_api_ping_settings", operation: "servers.api-ping.settings.update", tool: "mm_servers_api_ping_settings_update_plan", settings: { enabled: false, intervalSec: 131 } }, +] as const +let app: Awaited> | undefined +const accepted: string[] = [] +const configuration = async (table: typeof cases[number]["table"]) => (await pool.query(`SELECT to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'] AS value FROM ${table} s WHERE id=1`)).rows[0].value +const terminal = async (id: string) => { + const deadline = Date.now() + 30000 + while (Date.now() < deadline) { + const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(20) + } + throw Error("Owned telemetry operation did not reach a terminal state") +} +try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + for (const item of cases) { + await pool.query(`INSERT INTO ${item.table}(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false`) + await pool.query(`ALTER TABLE ${item.table} ADD COLUMN owned_opaque_configuration text NOT NULL DEFAULT 'NEVER_TELEMETRY_OPAQUE_A'`) + } + await pool.query("UPDATE uptime_settings SET resources_enabled=false,ping_enabled=false,speed_enabled=false WHERE id=1") + // Legitimate initial settings in the disposable installation prevent unrelated + // external automatic sources. Target services still run real refreshScheduler. + for (const table of ["geoip_settings", "backup_schedule_settings", "certificate_renew_settings", "internet_path_settings", "router_log_settings", "traffic_flow_settings"]) + await pool.query(`INSERT INTO ${table}(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false`) + await updateMcpSettings({ enabled: true }, actor) + app = await buildApp({ startScheduler: false, logger: false }); await app.ready() + const connection = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned telemetry configuration", mode: "approval", + expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:traffic:write", "mm:uptime:write", "mm:servers:write"], + scope: { allowGlobal: true, serverIds: [], operationIds: cases.map(item => item.operation) }, + // Exactly three successful writes plus six genuine admitted stale plans. + // This fixture allocation does not change production policy defaults. + limits: { callsPerMinute: 120, mutationsPerHour: 9 } }), actor) + const principal = await authenticateMcpToken(connection.token) + const prepare = async (item: typeof cases[number], settings: Record) => { + const plan = await invokeMcpTool(principal, item.tool, { settings }) as { planId: string } + const visible = JSON.stringify(await publicPlan(await loadPlan(plan.planId))) + assert(!visible.includes("NEVER_TELEMETRY_OPAQUE")); assert(!visible.includes("owned_opaque_configuration")) + assert.equal((await pool.query("SELECT operation_version FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0].operation_version, 2) + return plan + } + for (const item of cases) { + console.log("Owned telemetry configuration stage:", item.operation, "metadata") + const positive = await prepare(item, item.settings) + await pool.query(`UPDATE ${item.table} SET last_collected_at=now(),last_duration_ms=123,last_error=repeat('synthetic collector status',6000),updated_at=now() WHERE id=1`) + assert.equal(Number((await pool.query(`SELECT octet_length(last_error) AS bytes FROM ${item.table} WHERE id=1`)).rows[0].bytes), 156000, + "Actual PostgreSQL metadata subtraction must precede the 64KiB configuration bound") + await approvePlan(positive.planId, actor, true) + const applied = await applyPlan(principal, positive.planId, randomUUID()) + assert.equal((await terminal(applied.id)).state, "applied") + const saved = await configuration(item.table) + assert.equal(saved.interval_sec, item.settings.intervalSec); assert.equal(saved.enabled, false) + assert.equal(saved.owned_opaque_configuration, "NEVER_TELEMETRY_OPAQUE_A") + if (item.table === "uptime_settings") { + assert.equal(saved.resources_enabled, false); assert.equal(saved.ping_enabled, false); assert.equal(saved.speed_enabled, false) + } + accepted.push(item.operation + ":metadata-tolerated") + for (const fault of ["configuration", "opaque"] as const) { + console.log("Owned telemetry configuration stage:", item.operation, fault) + const stale = await prepare(item, { ...item.settings, intervalSec: item.settings.intervalSec + 10 }) + await approvePlan(stale.planId, actor, true) + if (fault === "configuration") await pool.query(`UPDATE ${item.table} SET interval_sec=interval_sec+1 WHERE id=1`) + else await pool.query(`UPDATE ${item.table} SET owned_opaque_configuration='NEVER_TELEMETRY_OPAQUE_B' WHERE id=1`) + const changed = await configuration(item.table) + const rejected = await applyPlan(principal, stale.planId, randomUUID()) + assert.deepEqual(await terminal(rejected.id), { state: "rejected", error_code: "STATE_CONFLICT" }) + assert.deepEqual(await configuration(item.table), changed, "Rejected stale plan must not overwrite any current configuration") + accepted.push(item.operation + ":reject-" + fault) + } + } + const visibleRows = (await pool.query(`SELECT result AS value FROM mcp_requests WHERE connection_id=$1 + UNION ALL SELECT result FROM mcp_operations WHERE connection_id=$1 + UNION ALL SELECT payload FROM mcp_audit WHERE connection_id=$1`, [connection.connection.id])).rows + assert(!JSON.stringify(visibleRows).includes("NEVER_TELEMETRY_OPAQUE")) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM alert_outbox")).rows[0].n), 0) + assert.equal(accepted.length, 9) + console.log(JSON.stringify({ accepted: accepted.length, cases: accepted, genuineMcpRuntime: true, + ordinarySettingsWritersAndSchedulerRefresh: true, collectorMetadataErrorBytes: 156000, nativeNetworkRequired: false, opaqueConfigurationLeaked: false })) +} finally { + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await app?.close() + await closePool() +} diff --git a/backend/src/modules/mcp/telemetry-settings-configuration.test.ts b/backend/src/modules/mcp/telemetry-settings-configuration.test.ts new file mode 100644 index 00000000..d5d22dbf --- /dev/null +++ b/backend/src/modules/mcp/telemetry-settings-configuration.test.ts @@ -0,0 +1,94 @@ +/** Fixed-source reader component tests; SQL/runtime approval acceptance is separate. */ +import assert from "node:assert/strict" +import { createHash, createHmac } from "node:crypto" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import { test } from "node:test" +import ts from "typescript" + +const source = await readFile(new URL("./telemetry-settings-configuration.ts", import.meta.url), "utf8") +const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText +const excluded = ["last_collected_at", "last_duration_ms", "last_error", "updated_at"] +const tables = ["traffic_settings", "uptime_settings", "servers_api_ping_settings"] as const +type Table = typeof tables[number] +type Row = Record +const canonical = (value: unknown): string => value && typeof value === "object" && !Array.isArray(value) + ? "{" + Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value) + +function fixture() { + const defaults: Record = { + traffic_settings: { id: 1, enabled: true, interval_sec: 30, retention_days: 14, created_at: "2026-10-29T00:00:00Z" }, + uptime_settings: { id: 1, enabled: true, resources_enabled: true, ping_enabled: true, speed_enabled: false, interval_sec: 300, probe_interval_sec: 15, speed_interval_sec: 60, retention_days: 14, created_at: "2026-10-29T00:00:00Z" }, + servers_api_ping_settings: { id: 1, enabled: false, interval_sec: 120, created_at: "2026-10-29T00:00:00Z" }, + } + const rows: Partial> = structuredClone(defaults), events: string[] = [], domains: string[] = [] + let malformed = false + const initialize = async (table: Table) => { events.push("initialize:" + table); rows[table] ??= structuredClone(defaults[table]); return { ignoredGetterResult: true } } + const modules: Record = { + "../../db/index.js": { pool: { query: async (sql: string) => { + const table = tables.find(table => sql.includes(`FROM ${table} s WHERE id=1 LIMIT 2`)); assert(table) + events.push("query:" + table) + const lists = [...sql.matchAll(/ARRAY\[([^\]]+)\]/g)].map(match => match[1].split(",").map(item => item.replaceAll("'", ""))) + assert.deepEqual(lists, [excluded, excluded], "Only the exact four collector fields may be excluded") + assert(sql.includes("octet_length((to_jsonb(s)-ARRAY["), "SQL byte bound must apply after telemetry subtraction") + const configuration = Object.fromEntries(Object.entries(rows[table]!).filter(([key]) => !excluded.includes(key))) + return { rows: malformed ? [] : [{ configuration: Buffer.byteLength(JSON.stringify(configuration)) > 65536 ? null : configuration }] } + } } }, + "../../services/traffic-collector.js": { getTrafficSettings: () => initialize("traffic_settings") }, + "../../services/uptime-collector.js": { readUptimeSettings: () => initialize("uptime_settings") }, + "../../services/servers-rest-ping-collector.js": { getServersApiPingSettings: () => initialize("servers_api_ping_settings") }, + "./security.js": { McpError: Error, fingerprint: (value: unknown) => createHash("sha256").update(canonical(value)).digest("hex"), + privateSecretFingerprint: async (value: string, domain: string) => { domains.push(domain); return createHmac("sha256", "component-private-key:" + domain).update(value).digest("hex") } }, + } + const exports: Record Promise> = {} + runInNewContext(compiled, { exports, require: (name: string) => { assert(name in modules, name); return modules[name] }, Buffer }) + const readers: Record Promise> = { traffic_settings: exports.readTrafficConfiguration, uptime_settings: exports.readUptimeConfiguration, servers_api_ping_settings: exports.readServersApiPingConfiguration } + return { rows, readers, events, domains, malformed: () => { malformed = true } } +} + +for (const table of tables) test(`${table}: collection metadata and >64KiB last_error cannot invalidate the approved configuration`, async () => { + const f = fixture(), before = await f.readers[table]() + Object.assign(f.rows[table]!, { last_collected_at: "2026-10-29T01:00:00Z", last_duration_ms: 199, last_error: "collector-error".repeat(10000), updated_at: "2026-10-29T01:00:01Z" }) + assert.deepEqual(await f.readers[table](), before) + assert.deepEqual(f.events, ["initialize:" + table, "query:" + table, "initialize:" + table, "query:" + table]) +}) + +for (const table of tables) test(`${table}: known settings, opaque future configuration and created_at remain approval dependencies without disclosure`, async () => { + const f = fixture(), before = await f.readers[table]() + f.rows[table]!.interval_sec = Number(f.rows[table]!.interval_sec) + 1 + const changedKnown = await f.readers[table]() + assert.notEqual(changedKnown.configurationProof, before.configurationProof) + assert.equal(changedKnown.intervalSec, f.rows[table]!.interval_sec) + f.rows[table]!.opaque_future_secret = { token: "DO-NOT-EXPOSE-private-settings" } + const opaque = await f.readers[table]() + assert.notEqual(opaque.configurationProof, changedKnown.configurationProof) + assert.equal(JSON.stringify(opaque).includes("DO-NOT-EXPOSE"), false) + assert.equal(Object.hasOwn(opaque, "opaque_future_secret"), false) + f.rows[table]!.created_at = "2026-10-28T00:00:00Z" + assert.notEqual((await f.readers[table]()).configurationProof, opaque.configurationProof) + assert.match(String(opaque.configurationProof), /^[a-f0-9]{64}$/) +}) + +test("configuration proof uses separate private purposes for the three fixed sources", async () => { + const f = fixture() + const result = await Promise.all(tables.map(table => f.readers[table]())) + assert.equal(new Set(f.domains).size, 3) + assert(f.domains.every(domain => domain.startsWith("mcp-telemetry-settings-configuration-") && domain.endsWith("-v1"))) + assert.equal(new Set(result.map(item => item.configurationProof)).size, 3) +}) + +test("existing singleton initializers run before the raw source read; getter DTO is not an authority snapshot", async () => { + const f = fixture() + delete f.rows.traffic_settings + const result = await f.readers.traffic_settings() + assert.equal(result.intervalSec, 30); assert.equal(Object.hasOwn(result, "ignoredGetterResult"), false) + assert.deepEqual(f.events, ["initialize:traffic_settings", "query:traffic_settings"]) +}) + +test("oversized opaque configuration, malformed singleton and invalid known fields refuse", async () => { + const oversized = fixture(); oversized.rows.traffic_settings!.opaque = "x".repeat(65537) + await assert.rejects(oversized.readers.traffic_settings()) + const missing = fixture(); missing.malformed(); await assert.rejects(missing.readers.uptime_settings()) + const invalid = fixture(); invalid.rows.servers_api_ping_settings!.enabled = "false" + await assert.rejects(invalid.readers.servers_api_ping_settings()) +}) diff --git a/backend/src/modules/mcp/telemetry-settings-configuration.ts b/backend/src/modules/mcp/telemetry-settings-configuration.ts new file mode 100644 index 00000000..447308e9 --- /dev/null +++ b/backend/src/modules/mcp/telemetry-settings-configuration.ts @@ -0,0 +1,49 @@ +import { pool } from "../../db/index.js" +import { getTrafficSettings } from "../../services/traffic-collector.js" +import { readUptimeSettings } from "../../services/uptime-collector.js" +import { getServersApiPingSettings } from "../../services/servers-rest-ping-collector.js" +import { fingerprint, McpError, privateSecretFingerprint } from "./security.js" + +// Fixed sources only. Bound the configuration AFTER removing the four fields +// collectors update; an arbitrarily long last_error is not configuration drift. +const trafficSql = `SELECT CASE WHEN octet_length((to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'])::text)<=65536 + THEN to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'] ELSE NULL END AS configuration FROM traffic_settings s WHERE id=1 LIMIT 2` +const uptimeSql = `SELECT CASE WHEN octet_length((to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'])::text)<=65536 + THEN to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'] ELSE NULL END AS configuration FROM uptime_settings s WHERE id=1 LIMIT 2` +const apiPingSql = `SELECT CASE WHEN octet_length((to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'])::text)<=65536 + THEN to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'] ELSE NULL END AS configuration FROM servers_api_ping_settings s WHERE id=1 LIMIT 2` + +const fail = (): never => { throw new McpError("STATE_UNAVAILABLE", "Конфигурация расписания недоступна или превышает предел", 409) } +async function read(sql: string, domain: string, fields: readonly (readonly [string, string, "boolean" | "integer"])[]) { + const rows = (await pool.query(sql)).rows + const raw: unknown = rows[0]?.configuration + if (rows.length !== 1 || !raw || typeof raw !== "object" || Array.isArray(raw) || Buffer.byteLength(JSON.stringify(raw)) > 65536) return fail() + const configuration = raw as Record + if (configuration.id !== 1) return fail() + const value: Record = {} + for (const [column, property, kind] of fields) { + const item = configuration[column] + if (kind === "boolean" ? typeof item !== "boolean" : !Number.isSafeInteger(item) || Number(item) <= 0) return fail() + value[property] = item + } + // Unknown/future raw columns remain sealed in the approval proof, including + // opaque configuration secrets. Only known fields and a purpose-keyed HMAC + // leave this reader; a public outer fingerprint cannot expose those values. + const configurationProof = await privateSecretFingerprint(fingerprint(configuration), "mcp-telemetry-settings-configuration-" + domain + "-v1") + return { ...value, configurationProof } +} + +export async function readTrafficConfiguration() { + await getTrafficSettings() // Preserve the existing singleton initializer. + return read(trafficSql, "traffic", [["enabled", "enabled", "boolean"], ["interval_sec", "intervalSec", "integer"], ["retention_days", "retentionDays", "integer"]]) +} +export async function readUptimeConfiguration() { + await readUptimeSettings() + return read(uptimeSql, "uptime", [["enabled", "enabled", "boolean"], ["resources_enabled", "resourcesEnabled", "boolean"], + ["ping_enabled", "pingEnabled", "boolean"], ["speed_enabled", "speedEnabled", "boolean"], ["interval_sec", "intervalSec", "integer"], + ["probe_interval_sec", "probeIntervalSec", "integer"], ["speed_interval_sec", "speedIntervalSec", "integer"], ["retention_days", "retentionDays", "integer"]]) +} +export async function readServersApiPingConfiguration() { + await getServersApiPingSettings() + return read(apiPingSql, "servers-api-ping", [["enabled", "enabled", "boolean"], ["interval_sec", "intervalSec", "integer"]]) +} diff --git a/backend/src/modules/mcp/temporary-grant-origin.test.ts b/backend/src/modules/mcp/temporary-grant-origin.test.ts new file mode 100644 index 00000000..5ade9ecf --- /dev/null +++ b/backend/src/modules/mcp/temporary-grant-origin.test.ts @@ -0,0 +1,138 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import type { PoolClient } from "pg" +import { pool, closePool } from "../../db/index.js" +import { readMcpTemporaryGrantOrigin as read, assertOriginalMcpTemporaryGrant as check, type OriginalServiceGrant } from "./temporary-grant-origin.js" + +type Sql = Pick +const connectionId = randomUUID(), credentialId = randomUUID() +const denied = (error: unknown) => !!error && typeof error === "object" && "code" in error && error.code === "GRANT_INACTIVE" +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function row() { + return { id: randomUUID(), payload: { credentialId, id: "original-grant", startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 10000).toISOString(), scope: { serverIds: [101, 102], operationIds: ["diagnostics.bandwidth"], allowGlobal: false } } } +} +function fixture(initial: unknown[] = [row()]) { + let rows = initial, hook: (() => Promise) | undefined + const calls: Array<{ text: string; values: unknown[] }> = [] + const sql = { async query(text: string, values: unknown[]) { calls.push({ text, values }); if (hook) await hook(); return { rows } } } as unknown as Sql + return { sql, calls, replace: (next: unknown[]) => { rows = next }, stall: (work: () => Promise) => { hook = work } } +} +const pin = (value: Awaited>, admissionBudgetMs = 5000): OriginalServiceGrant => { + const wall = Date.now(), monotonic = performance.now(), admissionExpiresAt = Math.min(wall + admissionBudgetMs, value.expiresAt ?? Infinity) + return Object.freeze({ ...value, monotonicEnd: value.expiresAt === null ? Infinity : monotonic + Math.max(0, value.expiresAt - wall), + admissionExpiresAt, admissionMonotonicEnd: monotonic + Math.max(0, admissionExpiresAt - wall) }) +} + +test("temporary grant origin read/check contract, fake read-only SQL only", async t => { + const originalQuery = pool.query, originalConnect = pool.connect + let realSqlCalls = 0 + pool.query = (() => { realSqlCalls++; throw Error("Grant-origin unit cannot use default SQL") }) as never + pool.connect = (() => { realSqlCalls++; throw Error("Grant-origin unit cannot acquire a backend") }) as never + try { + await t.test("bounded parameterized read authenticates physical row plus validated descriptor", async () => { + const original = row(), f = fixture([original]), first = await read(f.sql, connectionId, credentialId) + assert(Object.isFrozen(first)); assert.match(first.proof, /^[a-f0-9]{64}$/); assert.equal(first.expiresAt, Date.parse(original.payload.expiresAt)) + assert.deepEqual(await read(f.sql, connectionId, credentialId), first) + const call = f.calls[0]; assert.deepEqual(call.values, [connectionId, credentialId]); assert(!call.text.includes(connectionId)); assert(!call.text.includes(credentialId)) + assert.match(call.text, /^SELECT\b/); assert.match(call.text, /FROM public\.mcp_grants\b/); assert.match(call.text, /revoked_at IS NULL/); assert.match(call.text, /LIMIT 2$/) + assert.match(call.text, /octet_length\(payload::text\)<=1048576/); assert(!/\b(?:UPDATE|INSERT|DELETE|LOCK|FOR|CALL)\b|;/i.test(call.text)) + f.replace([{ ...original, id: randomUUID() }]); assert.notEqual((await read(f.sql, connectionId, credentialId)).proof, first.proof, "new physical row cannot reuse descriptor proof") + for (const payload of [{ ...original.payload, id: "replacement-descriptor" }, { ...original.payload, expiresAt: new Date(Date.parse(original.payload.expiresAt) + 1000).toISOString() }, { ...original.payload, scope: { ...original.payload.scope, serverIds: [101] } }]) { + f.replace([{ ...original, payload }]); assert.notEqual((await read(f.sql, connectionId, credentialId)).proof, first.proof) + } + }) + await t.test("absence is an immutable credential-bound original state, never an active grant", async () => { + const f = fixture([]), absent = await read(f.sql, connectionId, credentialId), original = pin(absent) + assert(Object.isFrozen(absent)); assert.equal(absent.expiresAt, null); assert.match(absent.proof, /^[a-f0-9]{64}$/) + await check(f.sql, connectionId, credentialId, original) + assert.notEqual((await read(f.sql, connectionId, randomUUID())).proof, absent.proof) + f.replace([row()]); await assert.rejects(check(f.sql, connectionId, credentialId, original), denied) + const active = pin(await read(f.sql, connectionId, credentialId)); f.replace([]); await assert.rejects(check(f.sql, connectionId, credentialId, active), denied) + }) + await t.test("expired, future, ambiguous, oversized-null and malformed records refuse", async () => { + const original = row() + const invalid: unknown[][] = [ + [original, original], [{ ...original, payload: null }], [{ ...original, payload: undefined }], [{ ...original, id: "chosen-row" }], + [{ ...original, payload: { ...original.payload, credentialId: randomUUID() } }], + [{ ...original, payload: { ...original.payload, startsAt: new Date(Date.now() - 2000).toISOString(), expiresAt: new Date(Date.now() - 1).toISOString() } }], + [{ ...original, payload: { ...original.payload, startsAt: new Date(Date.now() + 1000).toISOString() } }], + [{ ...original, payload: { ...original.payload, expiresAt: "invalid" } }], + [{ ...original, payload: { ...original.payload, expiresAt: new Date(Date.now() + 25 * 3600000).toISOString() } }], + [{ ...original, payload: { ...original.payload, unexpected: true } }], + [{ ...original, payload: { ...original.payload, scope: { ...original.payload.scope, serverIds: [101, 101] } } }], + [{ ...original, payload: { credentialId } }], [{ ...original, payload: [] }], [null], + ] + // A null row is not a PostgreSQL row, so its generic adapter rejection is + // also checked without asserting the database-record error code. + for (const rows of invalid.slice(0, -1)) await assert.rejects(read(fixture(rows).sql, connectionId, credentialId), denied) + await assert.rejects(read(fixture([null]).sql, connectionId, credentialId)) + }) + await t.test("captured proof/expiry cannot be extended by later SQL payload mutation or row replacement", async () => { + const original = row(), f = fixture([original]), value = await read(f.sql, connectionId, credentialId), originalPin = pin(value), expected = structuredClone(value) + original.payload.expiresAt = new Date(Date.now() + 100000).toISOString(); original.payload.scope.serverIds.push(999) + assert.deepEqual(value, expected); assert.equal(originalPin.expiresAt, expected.expiresAt); assert.throws(() => { (value as { expiresAt: number }).expiresAt += 100000 }) + await assert.rejects(check(f.sql, connectionId, credentialId, originalPin), denied) + f.replace([{ ...row(), payload: { ...original.payload, expiresAt: new Date(expected.expiresAt!).toISOString(), scope: { ...original.payload.scope, serverIds: [101, 102] } } }]) + await assert.rejects(check(f.sql, connectionId, credentialId, originalPin), denied) + }) + await t.test("missing or already expired original pin refuses before any SQL; valid exact original passes", async () => { + const f = fixture(), original = pin(await read(f.sql, connectionId, credentialId)), count = f.calls.length + await assert.rejects(check(f.sql, connectionId, credentialId, undefined), denied) + await assert.rejects(check(f.sql, connectionId, credentialId, Object.freeze({ ...original, expiresAt: Date.now() - 1 })), denied) + await assert.rejects(check(f.sql, connectionId, credentialId, Object.freeze({ ...original, monotonicEnd: performance.now() - 1 })), denied) + await assert.rejects(check(f.sql, connectionId, credentialId, Object.freeze({ ...original, admissionExpiresAt: Date.now() - 1 })), denied) + await assert.rejects(check(f.sql, connectionId, credentialId, Object.freeze({ ...original, admissionMonotonicEnd: performance.now() - 1 })), denied) + assert.equal(f.calls.length, count); await check(f.sql, connectionId, credentialId, original); assert.equal(f.calls.length, count + 1) + }) + await t.test("monotonic expiry DURING stalled SQL refuses even when the wall clock rolls back", async () => { + const originalRow = row() + originalRow.payload.startsAt = new Date(Date.now() - 120000).toISOString() + originalRow.payload.expiresAt = new Date(Date.now() + 100).toISOString() + const f = fixture([originalRow]), captured = await read(f.sql, connectionId, credentialId), entered = deferred(), release = deferred() + // The real original grant expires while SQL is stalled. startsAt is older + // than the rollback too, so wall checks alone still accept the grant and + // cannot accidentally make this monotonic regression pass as "future". + const original = pin(captured), originalNow = Date.now, baseline = Date.now() + f.stall(async () => { entered.resolve(); await release.promise }) + const rejected = assert.rejects(check(f.sql, connectionId, credentialId, original), denied) + try { + await entered.promise; Date.now = () => baseline - 60000; await delay(125) + assert.deepEqual(await read(fixture([originalRow]).sql, connectionId, credentialId), captured, "rolled-back wall clock alone still accepts the exact grant") + release.resolve(); await rejected + } + finally { Date.now = originalNow; release.resolve() } + assert.equal(f.calls.length, 2) + }) + await t.test("absent grant still has finite admission expiry during stalled SQL; rollback and caller pin mutation cannot renew it", async () => { + const f = fixture([]), absent = await read(f.sql, connectionId, credentialId), original = { ...pin(absent, 100) }, entered = deferred(), release = deferred() + assert.equal(original.expiresAt, null); assert.equal(original.monotonicEnd, Infinity); assert(Number.isFinite(original.admissionMonotonicEnd)) + const originalNow = Date.now, baseline = Date.now() + f.stall(async () => { entered.resolve(); await release.promise }) + const rejected = assert.rejects(check(f.sql, connectionId, credentialId, original), denied) + try { + await entered.promise + // The helper must have captured the original admission before SQL. + // Mutating this caller-owned object cannot extend the pending check. + original.admissionExpiresAt += 600000; original.admissionMonotonicEnd += 600000 + Date.now = () => baseline - 60000; await delay(125) + assert.deepEqual(await read(fixture([]).sql, connectionId, credentialId), absent, "absence still matches, so refusal must come from original admission lifetime") + release.resolve(); await rejected + } finally { Date.now = originalNow; release.resolve() } + assert.equal(f.calls.length, 2) + }) + await t.test("wall expiry during SQL and descriptor drift after a stalled read refuse", async () => { + for (const fault of ["wall", "descriptor"] as const) { + const f = fixture(), original = pin(await read(f.sql, connectionId, credentialId)), entered = deferred(), release = deferred(), originalNow = Date.now + f.stall(async () => { entered.resolve(); await release.promise }) + const rejected = assert.rejects(check(f.sql, connectionId, credentialId, original), denied) + try { await entered.promise; if (fault === "wall") Date.now = () => original.expiresAt! + 1; else f.replace([row()]); release.resolve(); await rejected } + finally { Date.now = originalNow; release.resolve() } + } + }) + assert.equal(realSqlCalls, 0) + console.log("Temporary grant origin: 8 groups, fake read-only SQL only, 0 real DB/network calls") + } finally { pool.query = originalQuery; pool.connect = originalConnect; await closePool() } +}) diff --git a/backend/src/modules/mcp/temporary-grant-origin.ts b/backend/src/modules/mcp/temporary-grant-origin.ts new file mode 100644 index 00000000..1f7c164a --- /dev/null +++ b/backend/src/modules/mcp/temporary-grant-origin.ts @@ -0,0 +1,40 @@ +import type { PoolClient } from "pg" +import { performance } from "node:perf_hooks" +import { mcpTemporaryGrantSchema } from "@mmapp/contracts/mcp" +import { fingerprint, McpError } from "./security.js" + +export type OriginalServiceGrant=Readonly<{proof:string;expiresAt:number|null;monotonicEnd:number;admissionExpiresAt:number;admissionMonotonicEnd:number}> +export function assertOriginalMcpTemporaryGrantLive(original: OriginalServiceGrant | undefined): asserts original is OriginalServiceGrant { + if (!original || !Number.isSafeInteger(original.admissionExpiresAt) || !Number.isFinite(original.admissionMonotonicEnd) || + Date.now() >= original.admissionExpiresAt || performance.now() >= original.admissionMonotonicEnd || + (original.expiresAt !== null && (Date.now() >= original.expiresAt || performance.now() >= original.monotonicEnd))) { + throw new McpError("GRANT_INACTIVE", "Срок исходного временного доступа истёк", 403) + } +} + +export async function assertOriginalMcpTemporaryGrant(sql:Pick,connectionId:string,credentialId:string,original:OriginalServiceGrant|undefined){ + const captured=original&&Object.freeze({...original}) + assertOriginalMcpTemporaryGrantLive(captured) + const current=await readMcpTemporaryGrantOrigin(sql,connectionId,credentialId) + assertOriginalMcpTemporaryGrantLive(captured) + if(current.proof!==captured.proof||current.expiresAt!==captured.expiresAt)throw new McpError("GRANT_INACTIVE","Исходный временный доступ изменён",403) +} + +/** Metadata only: no credentials or capability secrets. Pin the physical grant + * row as well as its validated descriptor; absence is an original state too. */ +export const temporaryGrantOriginSql = `SELECT id::text,CASE WHEN pg_catalog.octet_length(payload::text)<=1048576 THEN payload END AS payload + FROM public.mcp_grants WHERE connection_id=$1 AND revoked_at IS NULL AND payload->>'credentialId'=$2 LIMIT 2` +export async function readMcpTemporaryGrantOrigin(sql:Pick,connectionId:string,credentialId:string){ + const rows=(await sql.query(temporaryGrantOriginSql,[connectionId,credentialId])).rows + const deny=():never=>{throw new McpError("GRANT_INACTIVE","Исходный временный доступ изменён или недоступен",403)} + if(rows.length>1)return deny() + if(!rows.length)return Object.freeze({proof:fingerprint({credentialId,grant:null}),expiresAt:null}) + const row=rows[0] + if(typeof row.id!=="string"||!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(row.id)||!row.payload||typeof row.payload!=="object"||row.payload.credentialId!==credentialId)return deny() + const {credentialId:_credential,...raw}=row.payload + const parsed=mcpTemporaryGrantSchema.safeParse(raw) + if(!parsed.success)return deny() + const grant=parsed.data,startsAt=Date.parse(grant.startsAt),expiresAt=Date.parse(grant.expiresAt) + if(startsAt>Date.now()||expiresAt<=Date.now())return deny() + return Object.freeze({proof:fingerprint({rowId:row.id,credentialId,grant}),expiresAt}) +} diff --git a/backend/src/modules/mcp/terminal-diagnostic-actions.ts b/backend/src/modules/mcp/terminal-diagnostic-actions.ts new file mode 100644 index 00000000..dd81837e --- /dev/null +++ b/backend/src/modules/mcp/terminal-diagnostic-actions.ts @@ -0,0 +1,52 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { executeTerminalDiagnostic, terminalDiagnosticSchema, type TerminalDiagnosticClient } from "../../services/terminal-diagnostics.js" +import { withTerminalDiagnosticClient } from "../../services/terminal-diagnostic-client.js" +import { readRoutingEndpoints } from "./routing-observation-source.js" +import { effectiveMcpConnection } from "./store.js" +import { accessGeneration, fingerprint, McpError, redact, MAX_MCP_PUBLIC_STRING_LENGTH } from "./security.js" +import type { add } from "./actions.js" + +export const TERMINAL_DIAGNOSTIC_MAX_OUTPUT_BYTES = 12000 +export function registerTerminalDiagnosticActions(register: typeof add) { + const schema = terminalDiagnosticSchema.safeExtend({ serverId: z.number().int().positive().safe() }) + register("servers.terminal.read", "mm_server_terminal_diagnostic", "Разрешённая типизированная диагностика REST-терминала", "servers", "mm:servers:write", "read", "servers", schema, { + read: async (input, context) => { + const serverId = Number(input.serverId), { serverId: _id, ...args } = input + const command = terminalDiagnosticSchema.parse(args), generation = await accessGeneration() + const initial = (await pool.query("SELECT id,enabled,routing_endpoint_epoch FROM servers WHERE id=$1", [serverId])).rows[0] + const authorize = async () => { + context.signal?.throwIfAborted() + const fresh = await effectiveMcpConnection(context.principal) + if (!fresh.policy.scope.serverIds.includes(serverId) || !fresh.policy.scope.operationIds.includes("servers.terminal.read") || !hasPermission(fresh.policy.permissions, "mm:servers:write") || !hasPermission(fresh.owner_permissions, "mm:servers:write")) throw new McpError("PERMISSION_DENIED", "Нет допуска к терминальной диагностике", 403) + if (fingerprint(fresh.policy) !== fingerprint(context.connection.policy) || await accessGeneration() !== generation) throw new McpError("RESET_REQUIRED", "Допуск к диагностике изменился", 409) + const row = (await pool.query("SELECT id,enabled,routing_endpoint_epoch FROM servers WHERE id=$1", [serverId])).rows[0] + if (!initial || !z.string().uuid().safeParse(initial.routing_endpoint_epoch).success || !row || row.enabled !== true || row.routing_endpoint_epoch !== initial.routing_endpoint_epoch) throw new McpError("STATE_CONFLICT", "Подключение сервера изменилось или отключено", 409) + context.signal?.throwIfAborted() + } + await authorize() + let output: string + if (command.diagnostic === "help") output = await executeTerminalDiagnostic({} as TerminalDiagnosticClient, command) + else { + const endpoints = await readRoutingEndpoints([serverId]) + await authorize() + if (endpoints.length !== 1 || endpoints[0].routingEndpointEpoch !== initial.routing_endpoint_epoch || !endpoints[0].enabled) throw new McpError("STATE_CONFLICT", "Источник диагностики изменился", 409) + const endpoint = endpoints[0] + try { output = await withTerminalDiagnosticClient(endpoint, { signal: context.signal, authorize }, client => executeTerminalDiagnostic(client, command)) } + catch (error) { if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Терминальная диагностика не вернула подтверждённый результат", 409) } + // Free-form comments and log text may echo stored credentials. Mask exact + // credentials as well as labelled secrets before returning or auditing. + for (const value of [endpoint.password, Buffer.from(`${endpoint.username}:${endpoint.password}`).toString("base64")].filter(Boolean)) output = output.split(value).join("[redacted]") + } + await authorize() + const redactionTruncated = output.length > MAX_MCP_PUBLIC_STRING_LENGTH + output = String(redact(output)) + const encoded = Buffer.from(output), truncated = encoded.length > TERMINAL_DIAGNOSTIC_MAX_OUTPUT_BYTES + if (truncated) { let end = TERMINAL_DIAGNOSTIC_MAX_OUTPUT_BYTES; while (end > 0 && (encoded[end] & 0xc0) === 0x80) end--; output = encoded.subarray(0, end).toString("utf8") } + await authorize() + return { serverId, diagnostic: command.diagnostic, output, truncated: truncated || redactionTruncated, redactionTruncated, outputBytes: encoded.length, returnedBytes: Buffer.byteLength(output), maxOutputBytes: TERMINAL_DIAGNOSTIC_MAX_OUTPUT_BYTES, + activity: command.diagnostic === "ping" ? { kind: "ping", count: command.count ?? 4, interval: "0.2s", configurationApplied: false } : { kind: "read", configurationApplied: false } } + }, + }) +} diff --git a/backend/src/modules/mcp/traffic-history-actions.ts b/backend/src/modules/mcp/traffic-history-actions.ts new file mode 100644 index 00000000..4dd1fce4 --- /dev/null +++ b/backend/src/modules/mcp/traffic-history-actions.ts @@ -0,0 +1,85 @@ +import { z } from "zod" +import { hasPermission } from "../../lib/permissions.js" +import { readServerTrafficHistory, readServerTrafficInterfaces } from "../../services/traffic-history-read.js" +import { readTrafficCollectionContext } from "../../services/traffic-collection-state.js" +import { projectTrafficHistory } from "../../services/traffic-history-model.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { catalogDevices, catalogPaging } from "./catalog-scope.js" +import { effectiveMcpConnection } from "./store.js" +import { fingerprint, McpError } from "./security.js" +import type { ActionContext } from "./action-types.js" +import type { add } from "./actions.js" + +const range = z.enum(["5m", "15m", "1h", "4h", "24h"]).default("1h") +const minutes = { "5m": 5, "15m": 15, "1h": 60, "4h": 240, "24h": 1440 } +const serverId = z.number().int().positive().safe() +const onlyInterface = z.string().trim().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f]/.test(value) && value !== "__all__").optional() +export { assertAccess as assertTrafficHistoryAccess, assertSource as assertTrafficHistorySource } + +async function assertAccess(context: ActionContext) { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Чтение истории трафика отменено", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:traffic:read") || !hasPermission(current.policy.permissions, "mm:traffic:read")) throw new McpError("PERMISSION_DENIED", "Право чтения трафика отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy)) throw new McpError("RESET_REQUIRED", "Права изменились; повторите чтение истории", 409) +} +async function assertSource(context: ActionContext, server: NonNullable>>) { + await assertAccess(context) + const fresh = await getServerRowById(server.id) + if (!fresh || fingerprint(fresh) !== fingerprint(server)) throw new McpError("STATE_CONFLICT", "Конфигурация источника истории изменилась во время чтения", 409) +} +export function registerTrafficHistoryActions(register: typeof add) { + register("traffic.servers.history.read", "mm_traffic_servers_history_get", "Сохранённая история трафика разрешённых серверов", "traffic", "mm:traffic:read", "read", "servers", + catalogPaging.omit({ offset: true, limit: true }).safeExtend({ range }), { + read: async (input, context) => { + await assertAccess(context) + const { selected, ...page } = await catalogDevices(input, context) + const end = Date.now(), start = end - minutes[input.range as keyof typeof minutes] * 60000 + const collection = await readTrafficCollectionContext(selected.map(server => server.id)) + try { + const items = [] + for (const server of selected) { + await assertAccess(context) + items.push(projectTrafficHistory(await readServerTrafficHistory(server, start, end, undefined, { untilIso: new Date(end).toISOString(), maxRows: 20000 }, collection))) + await assertSource(context, server) + } + for (const server of selected) await assertSource(context, server) + await assertAccess(context) + return { ...page, servers: items, from: new Date(start).toISOString(), to: new Date(end).toISOString(), live: false, forwardingVerified: false } + } catch (error) { await assertAccess(context); if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "История недоступна или превышает 20000 строк на сервер; выберите меньший диапазон", 409) } + }, + }) + register("traffic.server.history.read", "mm_traffic_server_history_get", "История трафика сервера или интерфейса", "traffic", "mm:traffic:read", "read", "servers", z.object({ serverId, range, interface: onlyInterface }).strict(), { + read: async (input, context) => { + await assertAccess(context) + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const end = Date.now(), start = end - minutes[input.range as keyof typeof minutes] * 60000 + try { + const history = await readServerTrafficHistory(server, start, end, input.interface as string | undefined, { untilIso: new Date(end).toISOString(), maxRows: 20000 }) + await assertSource(context, server) + return { ...projectTrafficHistory(history), interface: input.interface ?? null, from: new Date(start).toISOString(), to: new Date(end).toISOString() } + } catch (error) { await assertAccess(context); if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "История недоступна или превышает 20000 строк; выберите меньший диапазон", 409) } + }, + }) + register("traffic.server.interfaces.read", "mm_traffic_server_interfaces_get", "Интерфейсы из сохранённой истории за 24 часа", "traffic", "mm:traffic:read", "read", "servers", + z.object({ serverId, offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(200).default(100) }).strict(), { + read: async (input, context) => { + await assertAccess(context) + const server = await getServerRowById(Number(input.serverId)) + if (!server) throw new McpError("NOT_FOUND", "Сервер не найден", 404) + const end = Date.now() + try { + const { interfaces, evidence } = await readServerTrafficInterfaces(server, end, { untilIso: new Date(end).toISOString(), maxRows: 100000 }) + await assertSource(context, server) + const offset = Number(input.offset), limit = Number(input.limit) + const items = interfaces.slice(offset, offset + limit).map(row => { + const observed = evidence.get(row.name)! + return { name: row.name, running: observed.stateKnown ? row.running : null, disabled: observed.stateKnown ? row.disabled : null, + rxNowMbps: row.rxNow, txNowMbps: row.txNow, sampledAt: observed.sampledAt, currentState: observed.currentState ?? "unknown", stateMeaning: "last-retained-interface-sample" } + }) + return { serverId: server.id, interfaces: items, total: interfaces.length, nextOffset: offset + limit < interfaces.length ? offset + limit : null, + incomplete: items.length < interfaces.length, from: new Date(end - 86400000).toISOString(), to: new Date(end).toISOString(), source: "stored-aggregate-interface-samples", live: false, currentInterfaceStateVerified: false, forwardingVerified: false } + } catch (error) { await assertAccess(context); if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Каталог истории недоступен или превышает 100000 строк", 409) } + }, + }) +} diff --git a/backend/src/modules/mcp/traffic-live-actions.ts b/backend/src/modules/mcp/traffic-live-actions.ts new file mode 100644 index 00000000..67bc453d --- /dev/null +++ b/backend/src/modules/mcp/traffic-live-actions.ts @@ -0,0 +1,38 @@ +import { setTimeout as delay } from "node:timers/promises" +import { z } from "zod" +import { readTrafficLiveSample } from "../../services/traffic-live.js" +import type { MonitorLiveSample } from "../../services/traffic-rate.js" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError } from "./security.js" +import { effectiveMcpConnection } from "./store.js" +import type { add } from "./actions.js" + +export function registerTrafficLiveActions(register: typeof add) { + register("traffic.live.read", "mm_traffic_live_get", "Ограниченное live-чтение трафика RouterOS", "traffic", "mm:traffic:read", "read", "servers", z.object({ + serverId: z.number().int().positive().safe(), + interface: z.string().trim().min(1).max(200).refine(value => !/[,\u0000-\u001f\u007f]/.test(value) && value !== "__all__", "Выберите один интерфейс или опустите поле").optional(), + samples: z.number().int().min(1).max(3).default(1), + }).strict(), { + read: async (input, context) => { + const { server, client } = await routerClient(Number(input.serverId)) + const source = fingerprint(server), policy = fingerprint(context.connection.policy) + const signal = AbortSignal.any([AbortSignal.timeout(30000), ...(context.signal ? [context.signal] : [])]) + const assertSource = async () => { + signal.throwIfAborted() + const current = await effectiveMcpConnection(context.principal) + if (fingerprint(current.policy) !== policy) throw new McpError("RESET_REQUIRED", "Права изменились; начните новое чтение", 409) + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || fingerprint(fresh) !== source) throw new McpError("STATE_CONFLICT", "Конфигурация источника трафика изменилась", 409) + signal.throwIfAborted() + } + const samples: MonitorLiveSample[] = [], cache = { names: [] as string[], joinedFailed: false } + for (let index = 0; index < Number(input.samples); index++) { + if (index) await delay(1000, undefined, { signal }) + samples.push(await readTrafficLiveSample(client, input.interface as string | undefined, cache, signal, assertSource)) + } + await assertSource() + return { serverId: server.id, interface: input.interface ?? null, samples, timestampKind: "backend-receipt", direction: "router-interface-rx-tx", aggregation: input.interface ? "single-interface" : "active-non-loopback-interfaces", forwardingVerified: false } + }, + }) +} diff --git a/backend/src/modules/mcp/traffic-user-history-actions.ts b/backend/src/modules/mcp/traffic-user-history-actions.ts new file mode 100644 index 00000000..f585b5a6 --- /dev/null +++ b/backend/src/modules/mcp/traffic-user-history-actions.ts @@ -0,0 +1,81 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { buildUserTrafficList, type UserTrafficDto, type BoundIfaceTrafficDto } from "../../services/traffic-users.js" +import { projectStoredTrafficMetrics } from "../../services/traffic-history-model.js" +import { readScopedTrafficUsers, type ScopedTrafficUserQuery } from "../../services/traffic-user-history-read.js" +import { catalogDevices, catalogPaging } from "./catalog-scope.js" +import { assertTrafficHistoryAccess, assertTrafficHistorySource } from "./traffic-history-actions.js" +import { fingerprint, McpError } from "./security.js" +import type { ActionContext } from "./action-types.js" +import type { add } from "./actions.js" + +const range = z.enum(["5m", "15m", "1h", "4h", "24h"]).default("1h") +const minutes = { "5m": 5, "15m": 15, "1h": 60, "4h": 240, "24h": 1440 } +const userId = z.string().trim().min(1).max(200) +const offset = z.number().int().min(0).max(100000).default(0) +const bindingLimit = z.number().int().min(1).max(200).default(100) +const userLimit = z.number().int().min(1).max(50).default(20) + +async function assertUnboundScope(context: ActionContext) { + if (!context.connection.policy.scope.allowGlobal) throw new McpError("GLOBAL_DENIED", "Пользователи без привязок требуют глобального доступа", 403) + const ids = (await pool.query<{ id: number }>("SELECT id FROM servers")).rows.map(row => Number(row.id)) + if (ids.some(id => !context.connection.policy.scope.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Для пользователей без привязок нужен весь каталог серверов", 403) +} +function bindingView(binding: BoundIfaceTrafficDto) { + return { bindingId: binding.bindingId, userId: binding.userId, serverId: Number(binding.serverId), interface: binding.interfaceName, + interfaceType: binding.interfaceType, peerSpecific: !!binding.peerPublicKey, status: binding.status, + statusObservedAt: binding.statusObservedAt, statusExpiresAt: binding.statusExpiresAt, ...projectStoredTrafficMetrics(binding) } +} +async function read(input: Record, context: ActionContext, mode: "users" | "user" | "bindings") { + await assertTrafficHistoryAccess(context) + if (input.includeUnbound) await assertUnboundScope(context) + const { selected, ...serverPage } = await catalogDevices(input, context, { includeDisabled: true }) + const query: ScopedTrafficUserQuery = { allowedServerIds: context.connection.policy.scope.serverIds, selectedServerIds: selected.map(server => server.id), + userId: input.userId as string | undefined, offset: mode === "user" ? 0 : Number(mode === "bindings" ? input.userOffset : input.offset), + limit: mode === "user" ? 1 : Number(mode === "bindings" ? input.userLimit : input.limit), includeUnbound: input.includeUnbound === true } + const end = Date.now(), start = end - minutes[input.range as keyof typeof minutes] * 60000 + try { + const catalog = await readScopedTrafficUsers(query) + if (mode === "user" && !catalog.users.length) throw new McpError("NOT_FOUND", "Пользователь с доступными привязками не найден в выбранной странице серверов", 404) + const result = await buildUserTrafficList(start, end, { users: catalog.users, serverIds: query.selectedServerIds, untilIso: new Date(end).toISOString(), maxRows: 20000 }) + await assertTrafficHistoryAccess(context) + for (const server of selected) await assertTrafficHistorySource(context, server) + if (fingerprint(await readScopedTrafficUsers(query)) !== fingerprint(catalog)) throw new McpError("STATE_CONFLICT", "Пользователи или привязки изменились во время чтения", 409) + if (input.includeUnbound) await assertUnboundScope(context) + await assertTrafficHistoryAccess(context) + const bindingStart = Number(mode === "bindings" ? input.offset : input.bindingOffset), pageSize = Number(mode === "bindings" ? input.limit : input.bindingLimit) + const view = (user: UserTrafficDto, index: number) => { + const source = catalog.users[index], bindings = user.interfaces.slice(bindingStart, bindingStart + pageSize).map(bindingView) + return { id: user.id, login: user.login, displayName: user.displayName, accountEnabled: user.active, + ...projectStoredTrafficMetrics(user), bindingCount: source.bindingCount, selectedBindingCount: source.selectedBindingCount, + completeSourceSet: source.bindingCount === source.selectedBindingCount, metricsScope: "selected-bindings", historicalOwnershipVerified: false, attribution: "current-bindings; overlapping bindings are not deduplicated", + bindings, nextBindingOffset: bindingStart + pageSize < user.interfaces.length ? bindingStart + pageSize : null, + incomplete: source.bindingCount !== source.selectedBindingCount || bindings.length < user.interfaces.length } + } + const envelope = { ...serverPage, from: new Date(start).toISOString(), to: new Date(end).toISOString(), live: false, forwardingVerified: false, historicalOwnershipVerified: false } + if (mode === "bindings") { + const bindings = result.flatMap(user => user.interfaces).map(bindingView), items = bindings.slice(bindingStart, bindingStart + pageSize) + return { ...envelope, interfaces: items, totalBindingsInUserPage: bindings.length, nextOffset: bindingStart + pageSize < bindings.length ? bindingStart + pageSize : null, + totalUsers: catalog.total, nextUserOffset: query.offset + query.limit < catalog.total ? query.offset + query.limit : null, + incomplete: serverPage.incomplete || items.length < bindings.length || result.length < catalog.total || catalog.users.some(user => user.bindingCount !== user.selectedBindingCount), source: "current authorized bindings and stored traffic samples" } + } + if (mode === "user") { const user = view(result[0], 0); return { ...envelope, user, incomplete: serverPage.incomplete || user.incomplete } } + const users = result.map(view) + return { ...envelope, users, total: catalog.total, nextOffset: query.offset + query.limit < catalog.total ? query.offset + query.limit : null, + incomplete: serverPage.incomplete || users.length < catalog.total || users.some(user => user.incomplete) } + } catch (error) { + await assertTrafficHistoryAccess(context) + if (error instanceof McpError) throw error + throw new McpError("STATE_UNAVAILABLE", "История пользователей недоступна или превышает 200 выбранных привязок/20000 samples на сервер; уменьшите выбранную область или диапазон", 409) + } +} + +export function registerTrafficUserHistoryActions(register: typeof add) { + const common = catalogPaging.safeExtend({ range, includeUnbound: z.boolean().default(false), bindingOffset: offset, bindingLimit }) + register("traffic.users.history.read", "mm_traffic_users_history_get", "История трафика пользователей с доступными привязками", "traffic", "mm:traffic:read", "read", "servers", + common.safeExtend({ limit: userLimit }), { read: (input, context) => read(input, context, "users") }) + register("traffic.user.history.read", "mm_traffic_user_history_get", "История трафика выбранного пользователя", "traffic", "mm:traffic:read", "read", "servers", + common.omit({ offset: true, limit: true }).safeExtend({ userId }), { read: (input, context) => read(input, context, "user") }) + register("traffic.bindings.history.read", "mm_traffic_bound_interfaces_get", "История трафика привязанных интерфейсов", "traffic", "mm:traffic:read", "read", "servers", + catalogPaging.safeExtend({ range, userId: userId.optional(), userOffset: offset, userLimit }), { read: (input, context) => read(input, context, "bindings") }) +} diff --git a/backend/src/modules/mcp/transport-lifecycle.ts b/backend/src/modules/mcp/transport-lifecycle.ts new file mode 100644 index 00000000..a887ff6d --- /dev/null +++ b/backend/src/modules/mcp/transport-lifecycle.ts @@ -0,0 +1,104 @@ +export interface McpTransportTerminal { + setupCompleted: boolean + dispatchStarted: boolean + failureStage: "setup" | "dispatch" | "close" | null + closeFailed: boolean +} +/** Before headers/end, Node's default 200 is not an observed wire status. */ +export function observeMcpResponseStatus(response: { headersSent: boolean; writableFinished: boolean; statusCode: number }) { + const statusCodeObserved = response.headersSent || response.writableFinished + return { statusCode: statusCodeObserved ? response.statusCode : null, statusCodeObserved } +} +interface ObservedResponse { + headersSent: boolean; writableFinished: boolean; destroyed: boolean; statusCode: number + once(event: "finish" | "close", listener: () => void): unknown + off(event: "finish" | "close", listener: () => void): unknown +} +export interface McpResponseObservation { + statusCodeObserved: boolean; statusCode: number | null + writableFinished: boolean | null; destroyed: boolean | null + terminalEvent: "finish" | "close" | null +} +export const MCP_RESPONSE_TERMINAL_WAIT_MS = 1000 +/** Raw ServerResponse flags may mutate when SDK attempts a late write AFTER + * close. Latch first ACTUAL finish/close; later flags cannot invent wire facts. + * No terminal observed at audit => explicit unknown, including already-closed + * responses whose terminal event predates attachment. No guessed past event. + */ +export function createMcpResponseObservation(response: ObservedResponse) { + const unknown: Readonly = Object.freeze({ statusCodeObserved: false, statusCode: null, writableFinished: null, destroyed: null, terminalEvent: null }) + let terminal: Readonly | undefined + let sealed = response.writableFinished || response.destroyed + let pending: Promise> | undefined + let resolvePending: ((value: Readonly) => void) | undefined + let timer: ReturnType | undefined + const detach = () => { response.off("finish", finished); response.off("close", closed) } + const capture = (event: "finish" | "close") => { + if (sealed) return + terminal = Object.freeze({ ...observeMcpResponseStatus(response), writableFinished: response.writableFinished, destroyed: response.destroyed, terminalEvent: event }) + sealed = true + detach() + clearTimeout(timer) + resolvePending?.(terminal) + resolvePending = undefined + } + const finished = () => capture("finish"), closed = () => capture("close") + // These flags cannot identify an already missed event. Do not install + // listeners that could never retire on an already terminal response. + if (!sealed) { response.once("finish", finished); response.once("close", closed) } + return { + snapshot: (): Readonly => terminal ?? unknown, + // Only audit waits. No raw flag fallback, synthetic event or indefinite hold. + // At the cap seal unknown and detach, so a late event cannot backfill facts. + settle: (): Promise> => { + if (sealed) return Promise.resolve(terminal ?? unknown) + pending ??= new Promise(resolve => { + resolvePending = resolve + timer = setTimeout(() => { + sealed = true + detach() + resolvePending = undefined + resolve(unknown) + }, MCP_RESPONSE_TERMINAL_WAIT_MS) + }) + return pending + }, + } +} +/** Protocol bookkeeping, not an operation outcome. Setup failure still attempts + * close and terminal audit; close failure cannot skip audit. Preserve the FIRST + * actual thrown value, including null/undefined. Never retry SDK/effects or + * fabricate a replacement receipt. Audit failure means persistence unconfirmed. + */ +export async function runMcpTransportLifecycle(hooks: { + setup(): void | Promise + dispatch(): Promise + close(): void | Promise + audit(terminal: Readonly): Promise + auditUnavailable?(terminal: Readonly, secondary: boolean): void +}): Promise { + let failed = false, firstError: unknown, result!: T + const terminal: McpTransportTerminal = { setupCompleted: false, dispatchStarted: false, failureStage: null, closeFailed: false } + const remember = (error: unknown, stage: McpTransportTerminal["failureStage"]) => { + if (!failed) { failed = true; firstError = error; terminal.failureStage = stage } + } + try { + await hooks.setup(); terminal.setupCompleted = true + terminal.dispatchStarted = true; result = await hooks.dispatch() + } catch (error) { remember(error, terminal.setupCompleted ? "dispatch" : "setup") } + finally { + try { await hooks.close() } + catch (error) { terminal.closeFailed = true; remember(error, "close") } + finally { + try { await hooks.audit(Object.freeze({ ...terminal })) } + catch (error) { + const secondary = failed + remember(error, null) + // Reporting only, bounded terminal metadata; no raw exception/IDs/body. + try { hooks.auditUnavailable?.(Object.freeze({ ...terminal }), secondary) } catch { /* Reporting cannot replace the actual failure. */ } + } + } + } + if (failed) throw firstError + return result +} diff --git a/backend/src/modules/mcp/users-bindings-owned.test.ts b/backend/src/modules/mcp/users-bindings-owned.test.ts new file mode 100644 index 00000000..6105c419 --- /dev/null +++ b/backend/src/modules/mcp/users-bindings-owned.test.ts @@ -0,0 +1,15 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "users-bindings-owned-acceptance-secret" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const actor = { id: "users-bindings-owner", email: "users@example.invalid", name: "User binding owner", apps: ["mm"], permissions: ["mm:settings:admin"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await updateMcpSettings({ enabled: true }, actor) + await (await import("../../test/mcp-users-bindings.js")).verifyMcpUserBindings(actor) +} finally { + await closePool() +} diff --git a/backend/src/modules/mcp/vpn-catalog-actions.ts b/backend/src/modules/mcp/vpn-catalog-actions.ts new file mode 100644 index 00000000..d8078129 --- /dev/null +++ b/backend/src/modules/mcp/vpn-catalog-actions.ts @@ -0,0 +1,58 @@ +import { z } from "zod" +import { pollWireGuardServers } from "../../services/wireguard-live.js" +import { pollIpsecServers } from "../../services/ipsec-live.js" +import { pollVxlanServers } from "../../services/vxlan-live.js" +import { pollGreServers } from "../../services/gre-live.js" +import { catalogDevices, catalogPaging } from "./catalog-scope.js" +import type { add } from "./actions.js" + +export function registerVpnCatalogActions(register: typeof add) { + register("network.gre.state.read", "mm_gre_state_get", "Каталог GRE с адресами и метаданными без PSK", "network", "mm:network:read", "read", "servers", catalogPaging.safeExtend({ addressOffset: z.number().int().min(0).max(100000).default(0), addressLimit: z.number().int().min(1).max(100).default(50) }), { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context), result = await pollGreServers(selected) + const offset = Number(input.offset), limit = Number(input.limit), addressOffset = Number(input.addressOffset), addressLimit = Number(input.addressLimit) + const tunnels = result.observed.sort((a, b) => a.id.localeCompare(b.id)).slice(offset, offset + limit).map(row => ({ ...row, + localAddresses: row.localAddresses.slice(addressOffset, addressOffset + addressLimit), + addressesPage: { total: row.localAddresses.length, nextOffset: addressOffset + addressLimit < row.localAddresses.length ? addressOffset + addressLimit : null } })) + return { ...page, tunnels, total: result.observed.length, nextOffset: offset + limit < result.observed.length ? offset + limit : null, failures: result.failures, + incomplete: page.incomplete || result.failures.length > 0 || tunnels.length < result.observed.length || tunnels.some(row => row.localAddresses.length < row.addressesPage.total), keyMaterialIncluded: false, forwardingVerified: false, configurationHistoryUpdated: false } + }, + }) + register("network.vxlan.state.read", "mm_vxlan_state_get", "Каталог VXLAN и статических VTEP", "network", "mm:network:read", "read", "servers", catalogPaging.safeExtend({ vtepOffset: z.number().int().min(0).max(100000).default(0), vtepLimit: z.number().int().min(1).max(100).default(50) }), { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context), result = await pollVxlanServers(selected) + const offset = Number(input.offset), limit = Number(input.limit) + const vtepOffset = Number(input.vtepOffset), vtepLimit = Number(input.vtepLimit) + const tunnels = result.tunnels.sort((a, b) => a.id.localeCompare(b.id)).slice(offset, offset + limit).map(row => { + const { port: _legacyPort, ...tunnel } = row + return { ...tunnel, l2mtu: row.l2mtu !== null && row.l2mtu > 0 ? row.l2mtu : null, remoteVteps: row.remoteVteps.slice(vtepOffset, vtepOffset + vtepLimit), vtepsPage: { total: row.remoteVteps.length, nextOffset: vtepOffset + vtepLimit < row.remoteVteps.length ? vtepOffset + vtepLimit : null } } + }) + return { ...page, tunnels, total: result.tunnels.length, nextOffset: offset + limit < result.tunnels.length ? offset + limit : null, failures: result.failures, + incomplete: page.incomplete || result.failures.length > 0 || tunnels.length < result.tunnels.length || tunnels.some(row => row.remoteVteps.length < row.vtepsPage.total), forwardingVerified: false } + }, + }) + register("network.wireguard.state.read", "mm_wireguard_state_get", "WireGuard интерфейсы и peers без приватных ключей", "network", "mm:network:read", "read", "servers", catalogPaging.safeExtend({ + limit: z.number().int().min(1).max(50).default(20), peerOffset: z.number().int().min(0).max(100000).default(0), peerLimit: z.number().int().min(1).max(50).default(20), + allowedOffset: z.number().int().min(0).max(100000).default(0), allowedLimit: z.number().int().min(1).max(200).default(100), + }), { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context), result = await pollWireGuardServers(selected, false) + const offset = Number(input.offset), limit = Number(input.limit), peerOffset = Number(input.peerOffset), peerLimit = Number(input.peerLimit) + const allowedOffset = Number(input.allowedOffset), allowedLimit = Number(input.allowedLimit) + const interfaces = result.observed.sort((a, b) => a.id.localeCompare(b.id)).slice(offset, offset + limit).map(row => ({ ...row, peers: row.peers.slice(peerOffset, peerOffset + peerLimit).map(peer => ({ ...peer, allowedIps: peer.allowedIps.slice(allowedOffset, allowedOffset + allowedLimit), allowedIpsPage: { total: peer.allowedIps.length, nextOffset: allowedOffset + allowedLimit < peer.allowedIps.length ? allowedOffset + allowedLimit : null } })), peersPage: { total: row.peers.length, nextOffset: peerOffset + peerLimit < row.peers.length ? peerOffset + peerLimit : null } })) + return { ...page, interfaces, total: result.observed.length, nextOffset: offset + limit < result.observed.length ? offset + limit : null, failures: result.failures, + incomplete: page.incomplete || result.failures.length > 0 || interfaces.length < result.observed.length || interfaces.some(row => row.peers.length < row.peersPage.total || row.peers.some(peer => peer.allowedIps.length < peer.allowedIpsPage.total)), keyMaterialIncluded: false, connectionVerified: false, configurationHistoryUpdated: false } + }, + }) + register("network.ipsec.state.read", "mm_ipsec_state_get", "Каталог IKEv2: серверы, сертификаты и клиенты без PSK", "network", "mm:network:read", "read", "servers", catalogPaging.safeExtend({ detailOffset: z.number().int().min(0).max(100000).default(0), detailLimit: z.number().int().min(1).max(100).default(50) }), { + read: async (input, context) => { + const { selected, ...page } = await catalogDevices(input, context), result = await pollIpsecServers(selected) + const offset = Number(input.offset), limit = Number(input.limit) + const detailOffset = Number(input.detailOffset), detailLimit = Number(input.detailLimit) + const servers = result.observed.servers.map(row => ({ ...row, peers: row.peers?.slice(detailOffset, detailOffset + detailLimit), peersPage: { total: row.peers?.length ?? 0, nextOffset: detailOffset + detailLimit < (row.peers?.length ?? 0) ? detailOffset + detailLimit : null }, certs: row.certs.slice(detailOffset, detailOffset + detailLimit), certificatesPage: { total: row.certs.length, nextOffset: detailOffset + detailLimit < row.certs.length ? detailOffset + detailLimit : null } })) + const clients = result.observed.clients.slice(offset, offset + limit) + return { ...page, servers, clients, totalClients: result.observed.clients.length, nextOffset: offset + limit < result.observed.clients.length ? offset + limit : null, failures: result.failures, + incomplete: page.incomplete || result.failures.length > 0 || clients.length < result.observed.clients.length || servers.some(row => (row.peers?.length ?? 0) < row.peersPage.total || row.certs.length < row.certificatesPage.total), dataPlaneVerified: false, configurationHistoryUpdated: false } + }, + }) +} diff --git a/backend/src/modules/mcp/vrf-preparation-diagnostic.test.ts b/backend/src/modules/mcp/vrf-preparation-diagnostic.test.ts new file mode 100644 index 00000000..9367e36e --- /dev/null +++ b/backend/src/modules/mcp/vrf-preparation-diagnostic.test.ts @@ -0,0 +1,97 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { vrfPreparationDiagnostic } from "./vrf-preparation-diagnostic.js" + +const requestId = "12345678-1234-4123-8123-123456789abc" +const operationId = "ros.vrf.update" +function fixture(message = "VRF production transition refused: static address provenance/context", stack?: string) { + const error = new Error(message) + if (stack !== undefined) Object.defineProperty(error, "stack", { value: stack, configurable: true, writable: true }) + return error +} +const stack = "Error: SECRET-PASSWORD 192.0.2.9 native-body\n at must (C:\\private\\backend\\src\\services\\vrf-production-transition.ts:80:82)\n at wrapped (C:\\private\\backend\\src\\modules\\mcp\\vrf-ticket-runtime.ts:76:9)" + +test("plain Error at known refusal produces bounded first owned source, stage and constant reason", () => { + const diagnostic = vrfPreparationDiagnostic(requestId, operationId, fixture(undefined, stack)) + assert.deepEqual(diagnostic, { event: "mcp.vrf.preparation-failed", requestId, operationId, stage: "transition", errorClass: "Error", firstOwnedFrame: { source: "vrf-production-transition", line: 80 }, reason: "static-address-context" }) + for (const secret of ["SECRET-PASSWORD", "192.0.2.9", "private", "native-body", "must", "stack", "message"]) assert.ok(!JSON.stringify(diagnostic).includes(secret), secret) +}) +test("real constructed Error with native stack never returns function names or raw text", () => { + const diagnostic = vrfPreparationDiagnostic(requestId, "ros.vrf.create", new Error("SECRET native payload"))! + assert.equal(diagnostic.errorClass, "Error"); assert.equal(diagnostic.reason, "other") + assert.ok(!JSON.stringify(diagnostic).includes("SECRET")) + const known = new Error("VRF production transition refused: dependency connected route identity") + const result = vrfPreparationDiagnostic(requestId, operationId, known)! + assert.equal(result.reason, "connected-route-identity"); assert.equal(result.stage, "transition") + // Native lazy stack accessors stay unmaterialized. No prepareStackTrace hook + // is needed to classify the actual own-data refusal message. + assert.equal(typeof Object.getOwnPropertyDescriptor(known, "stack")?.get, "function") + assert.equal(result.firstOwnedFrame, undefined) +}) +test("exact known messages map to enums; appended secrets remain other", () => { + for (const [message, reason] of [ + ["VRF production ticket refused: original dependency projection changed during preparation", "original-dependency-projection"], + ["VRF production transition refused: dependency connected route identity", "connected-route-identity"], + ["VRF production transition refused: missing ordered main VRF", "main-vrf-context"], + ["VRF production transition refused: main VRF shape", "main-vrf-context"], + ]) { + assert.equal(vrfPreparationDiagnostic(requestId, operationId, fixture(message))!.reason, reason) + assert.equal(vrfPreparationDiagnostic(requestId, operationId, fixture(message + " SECRET"))!.reason, "other") + } +}) +test("actual native Error unknown refusal suffix maps only reviewed stage without exposing secrets", () => { + for (const [prefix, stage] of [["VRF production read refused: ", "read"], ["VRF production admission refused: ", "admission"], ["VRF production transition refused: ", "transition"], ["VRF production ticket refused: ", "ticket"]]) { + const error = new Error(prefix + "SECRET-PASSWORD 192.0.2.9 native-body") + Object.defineProperty(error, "name", { get() { throw Error("SECRET name getter invoked") } }) + const result = vrfPreparationDiagnostic(requestId, operationId, error)! + assert.equal(result.stage, stage); assert.equal(result.reason, "other"); assert.equal(result.firstOwnedFrame, undefined) + for (const secret of ["SECRET-PASSWORD", "192.0.2.9", "native-body", prefix]) assert.ok(!JSON.stringify(result).includes(secret)) + } + const tooLong = fixture("VRF production read refused: " + "S".repeat(512)) + assert.equal(vrfPreparationDiagnostic(requestId, operationId, tooLong)!.stage, "other") +}) +test("proxy, prototype and toStringTag spoofing reject without triggering traps", () => { + let traps = 0 + const fail = () => { traps++; throw Error("SECRET getter called") } + const proxy = new Proxy(fixture(), { get: fail, getPrototypeOf: fail, getOwnPropertyDescriptor: fail, ownKeys: fail }) + const spoof = Object.create(Error.prototype) as object + Object.defineProperties(spoof, { message: { get: fail }, stack: { get: fail }, [Symbol.toStringTag]: { get: fail } }) + for (const error of [proxy, spoof, { name: "Error", message: "SECRET", stack }]) assert.deepEqual(vrfPreparationDiagnostic(requestId, operationId, error), { event: "mcp.vrf.preparation-failed", requestId, operationId, stage: "other", errorClass: "NonError", reason: "other" }) + assert.equal(traps, 0) +}) +test("actual Error own accessors and constructor/name getters are never invoked", () => { + for (const field of ["message", "stack"]) { + const error = fixture(undefined, stack), calls = { count: 0 } + Object.defineProperty(error, field, { get() { calls.count++; throw Error("SECRET") } }) + Object.defineProperty(error, "name", { get() { calls.count++; throw Error("SECRET") } }) + Object.defineProperty(error, "constructor", { get() { calls.count++; throw Error("SECRET") } }) + const diagnostic = vrfPreparationDiagnostic(requestId, operationId, error)! + assert.equal(diagnostic.reason, field === "stack" ? "static-address-context" : "other"); assert.equal(diagnostic.stage, field === "stack" ? "transition" : "other"); assert.equal(diagnostic.firstOwnedFrame, undefined) + assert.equal(calls.count, 0) + } +}) +test("built-in error classes use prototype identity and subclasses remain a fixed enum", () => { + for (const [error, expected] of [[new TypeError("SECRET"), "TypeError"], [new RangeError("SECRET"), "RangeError"], [new SyntaxError("SECRET"), "SyntaxError"], [new (class SecretError extends Error {})("SECRET"), "OtherNativeError"]] as const) assert.equal(vrfPreparationDiagnostic(requestId, operationId, error)!.errorClass, expected) +}) +test("nested Linux/Windows/compiled paths return source enums with bounded line only", () => { + for (const path of ["/app/dist/services/vrf-production-read.js", "file:///app/dist/services/vrf-production-read.js", "file:///C:/workspace/backend/src/services/vrf-production-read.ts", "C:\\workspace\\backend\\src\\services\\vrf-production-read.ts"]) { + const diagnostic = vrfPreparationDiagnostic(requestId, operationId, fixture("unexpected", `Error: SECRET\n at external (/tmp/node_modules/native.js:99:2)\n at closure (${path}:203:12)`))! + assert.equal(diagnostic.stage, "read"); assert.deepEqual(diagnostic.firstOwnedFrame, { source: "vrf-production-read", line: 203 }) + assert.ok(!JSON.stringify(diagnostic).includes(path)) + } +}) +test("non-owned paths, wrong directories and invalid line numbers never pass through", () => { + for (const path of ["/app/dist/services/private-secret.ts:1:1", "/app/dist/other/vrf-production-read.ts:1:1", "/app/dist/services/vrf-production-read.ts:0:1", "/app/dist/services/vrf-production-read.ts:1000001:1", "https://secret.example/src/services/vrf-production-read.ts:1:1"]) assert.equal(vrfPreparationDiagnostic(requestId, operationId, fixture("unexpected", `Error: SECRET\n at run (${path})`))!.firstOwnedFrame, undefined) +}) +test("stack scan has fixed byte/line bounds and never inspects error causes", () => { + const error = fixture("SECRET", "Error: SECRET\n" + " at external (/tmp/external.js:1:1)\n".repeat(70) + " at internal (/app/dist/services/vrf-production-transition.js:80:1)") + Object.defineProperty(error, "cause", { get() { throw Error("SECRET cause accessed") } }) + assert.equal(vrfPreparationDiagnostic(requestId, operationId, error)!.firstOwnedFrame, undefined) + error.stack = "Error: " + "S".repeat(17000) + "\n at internal (/app/dist/services/vrf-production-transition.js:80:1)" + assert.equal(vrfPreparationDiagnostic(requestId, operationId, error)!.firstOwnedFrame, undefined) +}) +test("nonerrors have no copied fields; non-VRF/non-UUID calls have no diagnostic", () => { + for (const error of [null, undefined, "SECRET", 42, Symbol("SECRET"), () => { throw Error("SECRET") }]) assert.equal(vrfPreparationDiagnostic(requestId, operationId, error)!.errorClass, "NonError") + for (const operation of ["ros.vrf.list", "routing.vrf.update", "mcp.operation.apply", "ros.bridge.update", "ros.vrf.update SECRET"]) assert.equal(vrfPreparationDiagnostic(requestId, operation, fixture()), undefined) + assert.equal(vrfPreparationDiagnostic("SECRET-not-a-uuid", operationId, fixture()), undefined) +}) diff --git a/backend/src/modules/mcp/vrf-preparation-diagnostic.ts b/backend/src/modules/mcp/vrf-preparation-diagnostic.ts new file mode 100644 index 00000000..89cfaf29 --- /dev/null +++ b/backend/src/modules/mcp/vrf-preparation-diagnostic.ts @@ -0,0 +1,86 @@ +import { types } from "node:util" + +const ownedSources = { + "vrf-production-read": { directory: "services", stage: "read" }, + "vrf-production-admission": { directory: "services", stage: "admission" }, + "vrf-production-transition": { directory: "services", stage: "transition" }, + "vrf-production-ticket-store": { directory: "services", stage: "ticket" }, + "vrf-production-action-binding": { directory: "services", stage: "admission" }, + "vrf-members": { directory: "services", stage: "read" }, + "vrf-ticket-runtime": { directory: "modules/mcp", stage: "ticket" }, + "resources": { directory: "modules/mcp", stage: "read" }, + "actions": { directory: "modules/mcp", stage: "other" }, + "runtime": { directory: "modules/mcp", stage: "runtime" }, +} as const +type Source = keyof typeof ownedSources +type Stage = typeof ownedSources[Source]["stage"] +type Reason = "original-dependency-projection" | "static-address-context" | "connected-route-identity" | "main-vrf-context" | "other" +type ErrorClass = "Error" | "TypeError" | "RangeError" | "SyntaxError" | "ReferenceError" | "URIError" | "EvalError" | "OtherNativeError" | "NonError" +type Operation = "ros.vrf.create" | "ros.vrf.update" | "ros.vrf.delete" +export interface VrfPreparationDiagnostic { + event: "mcp.vrf.preparation-failed"; requestId: string; operationId: Operation; + stage: Stage; errorClass: ErrorClass; + firstOwnedFrame?: { source: Source; line: number }; + reason: Reason; +} + +// Only complete, reviewed constant refusals. No user/native text is copied and +// appending any text (including secrets) intentionally maps back to "other". +const reasons = new Map([ + ["VRF production ticket refused: original dependency projection changed during preparation", "original-dependency-projection"], + ["VRF production transition refused: static address provenance/context", "static-address-context"], + ["VRF production transition refused: dependency connected route identity", "connected-route-identity"], + ["VRF production transition refused: missing ordered main VRF", "main-vrf-context"], + ["VRF production transition refused: main VRF shape", "main-vrf-context"], +]) +const refusalStages: ReadonlyArray = [ + ["VRF production read refused: ", "read"], + ["VRF production admission refused: ", "admission"], + ["VRF production transition refused: ", "transition"], + ["VRF production ticket refused: ", "ticket"], +] +const classes: Array<[object, ErrorClass]> = [ + [Error.prototype, "Error"], [TypeError.prototype, "TypeError"], [RangeError.prototype, "RangeError"], [SyntaxError.prototype, "SyntaxError"], + [ReferenceError.prototype, "ReferenceError"], [URIError.prototype, "URIError"], [EvalError.prototype, "EvalError"], +] +const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i + +function dataString(error: object, key: "message" | "stack"): string | undefined { + const descriptor = Object.getOwnPropertyDescriptor(error, key) + return descriptor && Object.hasOwn(descriptor, "value") && typeof descriptor.value === "string" ? descriptor.value : undefined +} +function ownedFrame(stack: string | undefined): VrfPreparationDiagnostic["firstOwnedFrame"] { + if (!stack) return undefined + // Bound work before splitting, and ignore the first error-message line. + for (const line of stack.slice(0, 16384).split(/\r?\n/).slice(1, 65)) { + if (line.length > 2048 || !/^\s+at\s/.test(line)) continue + const match = /(?:^|[ (])(?:file:\/\/)?(?:[A-Za-z]:[\\/]|\/)[^\r\n]*?[\\/](?:src|dist)[\\/](services|modules[\\/]mcp)[\\/]([a-z-]+)\.(?:ts|js):([1-9]\d{0,6}):[1-9]\d{0,6}\)?$/.exec(line) + if (!match || !Object.hasOwn(ownedSources, match[2])) continue + const source = match[2] as Source, lineNumber = Number(match[3]) + if (match[1].replaceAll("\\", "/") !== ownedSources[source].directory || lineNumber > 1000000) continue + return { source, line: lineNumber } + } + return undefined +} + +/** Pure bounded diagnostic. Never inspect arbitrary error getters/proxies, + * retain original objects, or expose messages, paths, addresses or stack text. */ +export function vrfPreparationDiagnostic(requestId: string, operationId: string, error: unknown): VrfPreparationDiagnostic | undefined { + if (typeof requestId !== "string" || !uuid.test(requestId) || !["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete"].includes(operationId)) return undefined + const result: VrfPreparationDiagnostic = { event: "mcp.vrf.preparation-failed", requestId, operationId: operationId as Operation, stage: "other", errorClass: "NonError", reason: "other" } + // isNativeError is an internal-slot check, unlike instanceof/toString. It + // rejects Proxy(Error) and prototype/toStringTag imitations without traps. + if (!types.isNativeError(error)) return result + result.errorClass = classes.find(([prototype]) => Object.getPrototypeOf(error) === prototype)?.[1] ?? "OtherNativeError" + const message = dataString(error, "message"), stack = dataString(error, "stack") + if (message !== undefined && message.length <= 512) { + result.reason = reasons.get(message) ?? "other" + result.stage = refusalStages.find(([prefix]) => message.startsWith(prefix))?.[1] ?? "other" + } + // Node 22 uses a native lazy stack accessor. We deliberately do not invoke + // it (nor Error.prepareStackTrace); exact own-message reasons still work. + if (message === undefined || stack === undefined) return result + const frame = ownedFrame(stack) + if (frame) { result.firstOwnedFrame = frame; result.stage = ownedSources[frame.source].stage } + return result +} diff --git a/backend/src/modules/mcp/vrf-ticket-runtime-sql.test.ts b/backend/src/modules/mcp/vrf-ticket-runtime-sql.test.ts new file mode 100644 index 00000000..b8291edc --- /dev/null +++ b/backend/src/modules/mcp/vrf-ticket-runtime-sql.test.ts @@ -0,0 +1,101 @@ +import assert from "node:assert/strict" +import { installLoopbackFixtureAgent } from "../../test/loopback-http-agent.js" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import type { AuthUser } from "../../lib/permissions.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import { createMcpConnection, authenticateMcpToken } from "./store.js" +import { invokeMcpTool, approvePlan } from "./runtime.js" +import { decryptSecret, fingerprint, privateSecretFingerprint } from "./security.js" +import { VrfProductionClaimOutcomeUnknown } from "../../services/vrf-production-ticket-store.js" +import { withVrfClaimQuarantine } from "./vrf-ticket-runtime.js" + +/** Explicit helper for the disposable migrated SQL database only. Uses the + * actual registry/runtime and production raw reader against loopback HTTP. + * Caller enables MCP/settings and supplies its isolated test actor. */ +export async function runVrfTicketRuntimeSqlAcceptance(actor: AuthUser): Promise<{ passed: true; cases: number }> { + let rawUnavailable = false, nativeWrites = 0, rawReads = 0 + const catalogs: Record = { + "/ip/vrf": [{ ".id": "*1", name: "tenant", interfaces: "none", disabled: "false" }, { ".id": "*2", name: "main", interfaces: "all", builtin: "true" }], + "/ip/settings": { "rp-filter": "no" }, "/ipv6/settings": { "disable-ipv6": "false" }, + "/routing/table": [{ ".id": "*a", name: "main", fib: "true", dynamic: "true" }], + } + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json") + if (req.method !== "GET") { nativeWrites++; res.statusCode = 405; res.end("{}"); return } + const endpoint = (req.url ?? "").replace(/^\/rest/, "") + if (endpoint === "/interface") { rawReads++; if (rawUnavailable) { res.statusCode = 400; res.end("{}"); return } } + res.end(JSON.stringify(catalogs[endpoint] ?? [])) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const closeFixtureAgent = installLoopbackFixtureAgent(router) + let connectionId: string | undefined, serverId: number | undefined, cases = 0 + try { + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,password,enabled) VALUES('vrf-runtime-acceptance','127.0.0.1',$1,false,'NEVER_VRF_PASSWORD',true) RETURNING id", [(router.address() as { port: number }).port])).rows[0].id) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "VRF ticket runtime", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [serverId], operationIds: ["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete"], allowGlobal: false }, limits: { callsPerMinute: 120, objectsPerPlan: 100, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + type Prepared = { planId: string; executionAvailable: boolean; vrfTicket?: { ticketId: string; executionAvailable: false } } + const prepare = async (optIn: boolean, action = "update") => invokeMcpTool(principal, `mm_vrf_${action}_plan`, { serverId, ...(action === "create" ? {} : { id: "*1" }), fields: action === "create" ? { name: "tenant-new", interfaces: "none" } : action === "delete" ? {} : { comment: "reviewed" }, ...(optIn ? { productionAdmission: true } : {}) }) as Promise + // Missing explicit admission must not mint a legacy unusable plan. + rawUnavailable = true + const plansBeforeLegacy = Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n) + await assert.rejects(prepare(false), /productionAdmission:true/) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n), plansBeforeLegacy) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM vrf_production_tickets WHERE connection_id=$1", [connectionId])).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT coalesce(sum(used),0)::int AS n FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations'", [connectionId])).rows[0].n), 0) + assert.equal(rawReads, 0); cases++ + const countPlans = async () => Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n) + const before = await countPlans() + await assert.rejects(prepare(true)) + assert.equal(await countPlans(), before) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM vrf_production_tickets WHERE connection_id=$1", [connectionId])).rows[0].n), 0); cases++ + rawUnavailable = false + for (const action of ["create", "update", "delete"]) { + const result = await prepare(true, action) + assert.ok(result.vrfTicket); assert.equal(result.vrfTicket.executionAvailable, false) + const stored = (await pool.query("SELECT t.*,p.evidence,p.operation_version,c.owner_id AS actual_owner FROM vrf_production_tickets t JOIN mcp_plans p ON p.id=t.plan_id JOIN mcp_connections c ON c.id=p.connection_id WHERE t.ticket_id=$1", [result.vrfTicket.ticketId])).rows[0] + assert.equal(stored.plan_id, result.planId); assert.equal(stored.owner_id, stored.actual_owner) + assert.equal(stored.action_id, `ros.vrf.${action}`); assert.equal(stored.action_version, stored.operation_version) + const evidence = JSON.parse(await decryptSecret(stored.evidence.sealed)) + assert.equal(evidence.vrfProductionAdmission, true) + const snapshot = evidence.snapshots.find((row: { resource: string }) => row.resource === "vrf") + assert.equal(snapshot.sourceProof, stored.source_proof) + // Reconstruct exactly the production source-v3 domain used by both + // configuration snapshots and the production raw adapter. + const { routerClient } = await import("./resources.js") + const { server } = await routerClient(serverId) + assert.equal(stored.source_proof, await privateSecretFingerprint(fingerprint({ id: server.id, enabled: server.enabled, host: server.host, port: server.port, useSsl: server.useSsl, verifySsl: server.verifySsl, username: server.username, password: server.password, epoch: server.routingEndpointEpoch }), "mcp-ros-snapshot-source-v3")) + assert.ok(new Date(stored.expires_at).getTime() <= Date.now() + 300000) + assert.ok(!JSON.stringify(result).includes("NEVER_VRF_PASSWORD")) + await approvePlan(result.planId, actor, false) + assert.equal((await pool.query("SELECT state FROM mcp_plans WHERE id=$1", [result.planId])).rows[0].state, "cancelled") + cases++ + } + const quarantinePlan = await prepare(true) + const operationId = randomUUID() + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key) VALUES($1,$2,$3,$4)", [operationId, quarantinePlan.planId, connectionId, randomUUID()]) + await assert.rejects(withVrfClaimQuarantine({ operationId, planId: quarantinePlan.planId, connectionId, ownerId: actor.id, journal: pool, async claim() { throw new VrfProductionClaimOutcomeUnknown(quarantinePlan.vrfTicket!.ticketId) } }), /claim outcome unknown/) + const journal = (await pool.query("SELECT state,error_code,result FROM mcp_operations WHERE id=$1", [operationId])).rows[0] + assert.equal(journal.state, "unknown"); assert.equal(journal.error_code, "VRF_CLAIM_OUTCOME_UNKNOWN"); assert.equal(journal.result.claimQuarantined, true); cases++ + const completedId = randomUUID(), completedPlan = await prepare(true) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,verification) VALUES($1,$2,$3,$4,'applied','passed')", [completedId, completedPlan.planId, connectionId, randomUUID()]) + await assert.rejects(withVrfClaimQuarantine({ operationId: completedId, planId: completedPlan.planId, connectionId, ownerId: actor.id, journal: pool, async claim() { throw new VrfProductionClaimOutcomeUnknown(completedPlan.vrfTicket!.ticketId) } }), /quarantine unavailable; execution forbidden/) + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [completedId])).rows[0].state, "applied"); cases++ + assert.equal(nativeWrites, 0) + return { passed: true, cases } + } finally { + closeFixtureAgent() + if (connectionId) { + await pool.query("DELETE FROM mcp_operation_steps WHERE operation_id IN (SELECT id FROM mcp_operations WHERE connection_id=$1)", [connectionId]) + await pool.query("DELETE FROM mcp_operations WHERE connection_id=$1", [connectionId]) + await pool.query("DELETE FROM vrf_production_tickets WHERE connection_id=$1", [connectionId]) + await pool.query("DELETE FROM mcp_plans WHERE connection_id=$1", [connectionId]) + await pool.query("DELETE FROM mcp_requests WHERE connection_id=$1", [connectionId]) + await pool.query("DELETE FROM mcp_connections WHERE id=$1", [connectionId]) + } + if (serverId) await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/vrf-ticket-runtime.test.ts b/backend/src/modules/mcp/vrf-ticket-runtime.test.ts new file mode 100644 index 00000000..bc371674 --- /dev/null +++ b/backend/src/modules/mcp/vrf-ticket-runtime.test.ts @@ -0,0 +1,98 @@ +import assert from "node:assert/strict" +import { createHmac, randomBytes, randomUUID } from "node:crypto" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { prepareRuntimeVrfTicket, withVrfClaimQuarantine, type VrfRuntimeTicketInput } from "./vrf-ticket-runtime.js" +import { VrfProductionClaimOutcomeUnknown, type PrepareVrfProductionTicketInput } from "../../services/vrf-production-ticket-store.js" + +const sourceProof = "a".repeat(64) +let issued: PrepareVrfProductionTicketInput | undefined, keyUses = 0, authority = 0, sources = 0 +const sql = { async query() { return { rows: [] } } } +const input: VrfRuntimeTicketInput = { + operationId: "ros.vrf.update", operationVersion: 4, planId: randomUUID(), connectionId: randomUUID(), ownerId: "owner", + evidence: { vrfProductionAdmission: true, target: { scope: "servers", serverIds: [7] }, objects: [], warnings: [], snapshots: [{ serverId: 7, resource: "vrf", rows: [], fingerprint: "snapshot", projectionVersion: 3, sourceProof }], steps: [{ kind: "ros", step: { serverId: 7, resource: "vrf", action: "update", id: "*1", fields: { name: "tenant" } } }] }, + sql, async assertAuthority() { authority++ }, async assertCurrentSource(serverId, proof) { assert.equal(serverId, 7); assert.equal(proof, sourceProof); sources++ }, +} +const dependencies = { + async withKey(work: (key: Uint8Array) => Promise) { keyUses++; return work(randomBytes(32)) }, + async prepare(value: PrepareVrfProductionTicketInput) { + issued = value + await value.assertAuthority(); await value.assertCurrentSource() + await value.transaction(async transaction => { assert.equal(transaction, sql) }) + return { ticketId: randomUUID(), expiresAt: new Date().toISOString(), executionAvailable: false as const } + }, +} +const ticket = await prepareRuntimeVrfTicket(input, dependencies) +assert.equal(ticket?.executionAvailable, false) +assert.equal(keyUses, 1); assert.equal(authority, 1); assert.equal(sources, 1) +assert.equal(issued?.context.binding.planId, input.planId) +assert.equal(issued?.context.binding.actorId, input.ownerId) +assert.equal(issued?.context.connectionId, input.connectionId) +assert.equal(issued?.context.actionVersion, 4) +assert.deepEqual(issued?.request, { action: "patch", id: "*1", fields: { name: "tenant" } }) +assert.deepEqual(Object.keys(ticket!).sort(), ["executionAvailable", "expiresAt", "ticketId"]) + +// A mislabeled, combined, cross-target or projection-v2 plan must never reach +// the raw reader/key provider, even if it resembles a VRF action. +for (const [mutate, refusal] of [ + [(value: VrfRuntimeTicketInput) => { value.operationId = "ros.ethernet.update" }, /VRF production ticket refused: untyped VRF plan/], + [(value: VrfRuntimeTicketInput) => { value.operationId = "ros.vrf.delete" }, /VRF action binding refused: unmapped public\/native action/], + [(value: VrfRuntimeTicketInput) => { value.evidence.steps.push(structuredClone(value.evidence.steps[0])) }, /VRF production ticket refused: ambiguous typed plan/], + [(value: VrfRuntimeTicketInput) => { value.evidence.target = { scope: "servers", serverIds: [8] } }, /VRF production ticket refused: ambiguous typed plan/], + [(value: VrfRuntimeTicketInput) => { value.evidence.snapshots[0].projectionVersion = 2 }, /VRF production ticket refused: missing trusted source-v3 snapshot/], + [(value: VrfRuntimeTicketInput) => { delete value.evidence.snapshots[0].sourceProof }, /VRF production ticket refused: missing trusted source-v3 snapshot/], +] as const) { + const changed = { ...input, evidence: structuredClone(input.evidence) } + mutate(changed) + await assert.rejects(prepareRuntimeVrfTicket(changed, dependencies), refusal) + assert.equal(keyUses, 1) +} +assert.equal(await prepareRuntimeVrfTicket({ ...input, evidence: { ...input.evidence, vrfProductionAdmission: undefined } }, dependencies), undefined) + +// Lost claim ACK must be quarantined in the permanent operation journal +// before control returns. It cannot return a mutation grant or retry claim. +let claims = 0, quarantines = 0 +const unknown = new VrfProductionClaimOutcomeUnknown(randomUUID()) +const journalInput = { operationId: randomUUID(), planId: input.planId, connectionId: input.connectionId, ownerId: input.ownerId, + journal: { async query(text: string, values?: unknown[]) { quarantines++; assert.match(text, /state='unknown'/); assert.match(text, /VRF_CLAIM_OUTCOME_UNKNOWN/); assert.deepEqual(values, [journalInput.operationId, input.planId, input.connectionId, input.ownerId]); return { rows: [{ id: journalInput.operationId }] } } }, + async claim(): Promise { claims++; throw unknown }, +} +await assert.rejects(withVrfClaimQuarantine(journalInput), error => error === unknown) +assert.equal(claims, 1); assert.equal(quarantines, 1) +await assert.rejects(withVrfClaimQuarantine({ ...journalInput, journal: { async query() { return { rows: [] } } } }), /quarantine unavailable; execution forbidden/) +const ordinary = new Error("revoked") +await assert.rejects(withVrfClaimQuarantine({ ...journalInput, async claim() { throw ordinary } }), error => error === ordinary) +assert.equal(quarantines, 1) + +// Wiring boundary: actual encrypted INSERT precedes ticket creation and audit +// within the existing transaction. It does not bypass action preparation or +// replace existing native companion refusal / simple VRF execution admission. +const runtime = await readFile(new URL("./runtime.ts", import.meta.url), "utf8") +const insert = runtime.indexOf('await client.query("INSERT INTO mcp_plans(') +const prepare = runtime.indexOf("const vrfTicket = await prepareRuntimeVrfTicket(") +const audit = runtime.indexOf('await audit(client, connection.owner_id, "plan.create"') +assert.ok(insert > 0 && prepare > insert && audit > prepare) +assert.ok(runtime.indexOf("const evidence = await action.prepare(input, call.context)") < insert) +assert.match(runtime, /\(vrfTicket \? \{ vrfTicket \} : \{\}\), executionAvailable: true/) +// Repeated server callbacks derive identical purpose material from durable +// storage; a different server state directory cannot validate these tickets. +const directory = await mkdtemp(join(tmpdir(), "mm-vrf-sealing-")), other = await mkdtemp(join(tmpdir(), "mm-vrf-sealing-")), previous = process.env.MCP_STATE_DIR +try { + const { withVrfProductionSealingKey } = await import("./security.js") + process.env.MCP_STATE_DIR = directory + let retained: Uint8Array | undefined + const digest = async () => withVrfProductionSealingKey(async key => { retained = key; return createHmac("sha256", key).update("private-test-vector").digest("hex") }) + const first = await digest() + assert.ok(retained!.every(byte => byte === 0), "purpose key cleared after callback") + assert.equal(await digest(), first) + process.env.MCP_STATE_DIR = other + assert.notEqual(await digest(), first) + process.env.MCP_STATE_DIR = directory + const encryptionKey = await readFile(join(directory, "encryption-key")) + assert.notEqual(createHmac("sha256", encryptionKey).update("private-test-vector").digest("hex"), first) +} finally { + if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous + await rm(directory, { recursive: true, force: true }); await rm(other, { recursive: true, force: true }) +} +console.log("VRF ticket runtime: transaction wiring, typed scope, private callback and claim quarantine passed") diff --git a/backend/src/modules/mcp/vrf-ticket-runtime.ts b/backend/src/modules/mcp/vrf-ticket-runtime.ts new file mode 100644 index 00000000..30516149 --- /dev/null +++ b/backend/src/modules/mcp/vrf-ticket-runtime.ts @@ -0,0 +1,101 @@ +import type { ChangeEvidence } from "./action-types.js" +import { VrfProductionClaimOutcomeUnknown, prepareVrfProductionTicket, type PrepareVrfProductionTicketInput, type VrfTicketSql } from "../../services/vrf-production-ticket-store.js" +import { assertVrfProductionHistoryProvenance, type VrfProductionTransitionCheckpoint } from "../../services/vrf-production-transition.js" +import { assertVrfActionContext, assertVrfAliasEvidence, vrfNativeActionFor, vrfActionContractsCurrent } from "../../services/vrf-production-action-binding.js" +import { findMcpOperation } from "./registry.js" + +export const isVrfTicketOperation = (operationId: string): boolean => /^ros\.vrf\.(create|update|delete)$/.test(operationId) || operationId === "routing.vrf.update" +export interface VrfRuntimeTicketInput { + operationId: string + operationVersion: number + planId: string + connectionId: string + ownerId: string + evidence: ChangeEvidence + /** The connection that has just inserted the real encrypted plan. */ + sql: VrfTicketSql + assertAuthority(): Promise + assertCurrentSource(serverId: number, proof: string): Promise +} +interface Dependencies { + withKey(work: (secret: Uint8Array) => Promise): Promise + prepare: typeof prepareVrfProductionTicket + history?(input: VrfRuntimeTicketInput, nativeId: string | undefined, sourceProof: string, secret: Uint8Array): Promise +} +const production: Dependencies = { + async withKey(work) { return (await import("./security.js")).withVrfProductionSealingKey(work) }, + prepare: prepareVrfProductionTicket, + async history(input, nativeId, sourceProof, secret) { + const { loadVerifiedVrfProductionHistory } = await import("../../services/vrf-production-execution.js") + return loadVerifiedVrfProductionHistory({ sql: input.sql, ownerId: input.ownerId, serverId: input.evidence.target.scope === "servers" ? input.evidence.target.serverIds[0] : 0, sourceProof, nativeId, secret }) + }, +} +/** Called only after encrypted plan INSERT, on that exact transaction. The + * store re-reads/decrypts the actual row and validates owner/action/request. + * There is intentionally no public preflight tool or native mutation grant. */ +export async function prepareRuntimeVrfTicket(input: VrfRuntimeTicketInput, dependencies: Dependencies = production): Promise<{ ticketId: string; expiresAt: string; executionAvailable: false } | undefined> { + if (input.evidence.vrfProductionAdmission !== true) return undefined + if (!isVrfTicketOperation(input.operationId)) { + throw new Error("VRF production ticket refused: untyped VRF plan") + } + const { evidence } = input + const step = evidence.steps[0] + if (evidence.target.scope !== "servers" || evidence.target.serverIds.length !== 1 || evidence.steps.length !== 1 || step?.kind !== "ros" || step.step.resource !== "vrf" || step.step.serverId !== evidence.target.serverIds[0]) throw new Error("VRF production ticket refused: ambiguous typed plan") + const native = step.step + const snapshots = evidence.snapshots.filter(snapshot => snapshot.serverId === native.serverId && snapshot.resource === "vrf") + if (snapshots.length !== 1 || snapshots[0].projectionVersion !== 3 || !/^[a-f0-9]{64}$/.test(snapshots[0].sourceProof ?? "")) throw new Error("VRF production ticket refused: missing trusted source-v3 snapshot") + const sourceProof = snapshots[0].sourceProof! + const request: PrepareVrfProductionTicketInput["request"] = { action: native.action === "update" ? "patch" : native.action, ...(native.id !== undefined ? { id: native.id } : {}), fields: native.fields, ...(native.placeBeforeId !== undefined ? { placeBeforeId: native.placeBeforeId } : {}) } + const actionId = vrfNativeActionFor(input.operationId, request) + const context: PrepareVrfProductionTicketInput["context"] = { binding: { planId: input.planId, actorId: input.ownerId, serverId: native.serverId, sourceProof }, connectionId: input.connectionId, actionId, + actionVersion: input.operationId === "routing.vrf.update" ? Number(findMcpOperation(actionId)?.version) : input.operationVersion, + ...(input.operationId === "routing.vrf.update" ? { authorityAction: { id: "routing.vrf.update" as const, version: input.operationVersion as 4 } } : {}) } + assertVrfActionContext(context, request); assertVrfAliasEvidence(context, request, evidence) + const authority = async () => { + await input.assertAuthority() + if (context.authorityAction && !vrfActionContractsCurrent(context)) throw new Error("VRF production ticket refused: stale native/public alias contract") + } + return dependencies.withKey(async secret => { + const previous = await dependencies.history?.(input, native.id, sourceProof, secret) + return dependencies.prepare({ + context, request, secret, assertAuthority: authority, + onAdmission: async (admission, snapshot) => { + const { vrfProductionDependencyObjects } = await import("../../services/vrf-production-transition.js") + const { McpError } = await import("./security.js") + const objects = new Map(input.evidence.objects.map(object => [`${object.serverId}/${object.kind}/${object.id}`, object])) + for (const object of vrfProductionDependencyObjects(snapshot, admission)) objects.set(`${object.serverId}/${object.kind}/${object.id}`, object) + if (objects.size > 1000) throw new McpError("BUDGET_EXCEEDED", "VRF dependency closure превышает существующий предел объектов плана", 409) + input.evidence.objects = [...objects.values()] + }, + assertPreparedSnapshot: async snapshot => { + if (previous) assertVrfProductionHistoryProvenance(previous, snapshot) + const { configurationRows, resourceById } = await import("./resources.js") + const { fingerprint } = await import("./security.js") + for (const original of input.evidence.snapshots) { + if (snapshot.catalogs[original.resource] === undefined) continue + if (original.serverId !== snapshot.serverId || original.sourceProof !== snapshot.sourceProof || fingerprint(configurationRows(resourceById(original.resource), snapshot.catalogs[original.resource])) !== original.fingerprint) throw new Error("VRF production ticket refused: original dependency projection changed during preparation") + } + await input.assertAuthority() + }, + assertCurrentSource: () => input.assertCurrentSource(native.serverId, sourceProof), + transaction: async work => work(input.sql), + }) }) +} + +/** Future native admission must wrap its claim in this boundary, using the + * independent durable operation journal connection, never the plan/claim + * transaction. Unknown claim acknowledgements are terminal and never retried. */ +export async function withVrfClaimQuarantine(input: { operationId: string; planId: string; connectionId: string; ownerId: string; journal: VrfTicketSql; claim(): Promise }): Promise { + try { return await input.claim() } + catch (error) { + if (!(error instanceof VrfProductionClaimOutcomeUnknown)) throw error + const { rows } = await input.journal.query(`UPDATE mcp_operations o SET state='unknown',verification='inconclusive',error_code='VRF_CLAIM_OUTCOME_UNKNOWN',result=jsonb_build_object('needsReview',true,'claimQuarantined',true),finished_at=clock_timestamp() + FROM mcp_plans p,mcp_connections c WHERE o.id=$1 AND o.plan_id=$2 AND o.connection_id=$3 + AND o.state IN ('pending','running') + AND p.id=o.plan_id AND p.connection_id=o.connection_id AND c.id=p.connection_id AND c.owner_id=$4 + AND p.operation_id IN ('ros.vrf.create','ros.vrf.update','ros.vrf.delete','routing.vrf.update') + RETURNING o.id`, [input.operationId, input.planId, input.connectionId, input.ownerId]) + if (rows.length !== 1) throw new Error("VRF claim outcome unknown; durable operation quarantine unavailable; execution forbidden", { cause: error }) + throw error + } +} diff --git a/backend/src/modules/mcp/wire-admission.test.ts b/backend/src/modules/mcp/wire-admission.test.ts new file mode 100644 index 00000000..c81cc2c3 --- /dev/null +++ b/backend/src/modules/mcp/wire-admission.test.ts @@ -0,0 +1,41 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { admitMcpRequestBody, MCP_REQUEST_BODY_BYTES } from "./wire-admission.js" +/** Test-only legacy wire fixture; not part of the production admission API. */ +function legacySuccessWireBytes(result: unknown, id: unknown) { + const idJson = JSON.stringify(id), resultJson = JSON.stringify(result) + if (idJson === undefined || resultJson === undefined) throw Error("JSON value required") + const message = JSON.stringify({ jsonrpc: "2.0", id, result }) + return { resultBytes: Buffer.byteLength(resultJson), idBytes: Buffer.byteLength(idJson), + rpcBytes: Buffer.byteLength(message), sseEntityBytes: Buffer.byteLength(`event: message\ndata: ${message}\n\n`) } +} +test("multi and empty batches refused without reflecting ids or inspecting per-message bodies", () => { + for (const body of [[], [{ id: "private-a" }, { id: "private-b" }], Array.from({ length: 100 }, () => ({ id: "x" }))]) { + const decision = admitMcpRequestBody(body) + assert.equal(decision.allowed, false) + if (decision.allowed) throw Error("expected refusal") + assert.equal(decision.error.id, null); assert.equal(decision.metrics.messageCount, body.length) + assert.equal(decision.metrics.encodedIdBytes, null) + assert(!JSON.stringify(decision).includes("private")) + } +}) +test("one-element batch is not unwrapped or rewritten and long valid ids remain valid", () => { + const body = [{ id: "я".repeat(32768), method: "ping" }] + assert(admitMcpRequestBody(body).allowed); assert.equal(body.length, 1) + for (const id of [0, -1, 0.5, null, "", "a".repeat(32768)]) assert(admitMcpRequestBody({ id }).allowed) + assert(admitMcpRequestBody({ method: "notifications/initialized" }).allowed) +}) +test("encoded-id bound inclusive at existing1MiB boundary, no new128 policy", () => { + assert(admitMcpRequestBody({ id: "a".repeat(MCP_REQUEST_BODY_BYTES - 2) }).allowed) + const denied = admitMcpRequestBody({ id: "a".repeat(MCP_REQUEST_BODY_BYTES - 1) }) + assert(!denied.allowed); if (!denied.allowed) assert.equal(denied.reason, "REQUEST_ID_TOO_LARGE") + const bad = admitMcpRequestBody({ id: 1n }); assert(!bad.allowed) +}) +test("legacy arithmetic counts escaping, UTF8, metadata, entire actual message and SSE prefix", () => { + for (const id of [1, -1, "я\\\"\n".repeat(100)]) { + const result = { content: [{ type: "text", text: JSON.stringify({ value: "🛰️\\\"" }) }], structuredContent: { result: { value: "🛰️\\\"" } }, _meta: { fixed: "metadata" } } + const bytes = legacySuccessWireBytes(result, id) + assert.equal(bytes.rpcBytes, bytes.resultBytes + bytes.idBytes + 33) + assert.equal(bytes.sseEntityBytes, bytes.rpcBytes + 23) + } +}) diff --git a/backend/src/modules/mcp/wire-admission.ts b/backend/src/modules/mcp/wire-admission.ts new file mode 100644 index 00000000..4a82d98a --- /dev/null +++ b/backend/src/modules/mcp/wire-admission.ts @@ -0,0 +1,32 @@ +/** Reviewed candidate only: no production integration in this stage. */ +export const MCP_REQUEST_BODY_BYTES = 1024 * 1024 +type Metrics = { messageCount: number; encodedIdBytes: number | null } +export type WireAdmission = + | { allowed: true; metrics: Metrics } + | { allowed: false; reason: "SINGLE_MESSAGE_REQUIRED" | "REQUEST_ID_TOO_LARGE" | "REQUEST_ID_NOT_JSON"; + metrics: Metrics; status: 400; error: { jsonrpc: "2.0"; id: null; error: { code: -32600; message: string } } } +function denied(reason: Extract["reason"], metrics: Metrics): WireAdmission { + return { allowed: false, reason, metrics, status: 400, + error: { jsonrpc: "2.0", id: null, error: { code: -32600, + message: reason === "SINGLE_MESSAGE_REQUIRED" ? "One JSON-RPC message per POST is required" : "Request id exceeds the request body budget" } } } +} +/** + * Caller MUST retain existing authentication, protocol rate reservation and + * bounded denial audit. Input is the existing body-limit-protected parsed body. + * Do not unwrap/alter a length-one array or reinterpret SDK method/id validity. + */ +export function admitMcpRequestBody(body: unknown): WireAdmission { + const count = Array.isArray(body) ? body.length : 1 + const metrics: Metrics = { messageCount: count, encodedIdBytes: null } + if (Array.isArray(body) && count !== 1) return denied("SINGLE_MESSAGE_REQUIRED", metrics) + const envelope: unknown = Array.isArray(body) ? body[0] : body + try { + if (envelope && typeof envelope === "object" && Object.hasOwn(envelope, "id")) { + const encoded = JSON.stringify((envelope as Record).id) + if (encoded === undefined) return denied("REQUEST_ID_NOT_JSON", metrics) + metrics.encodedIdBytes = Buffer.byteLength(encoded, "utf8") + if (metrics.encodedIdBytes > MCP_REQUEST_BODY_BYTES) return denied("REQUEST_ID_TOO_LARGE", metrics) + } + } catch { return denied("REQUEST_ID_NOT_JSON", metrics) } + return { allowed: true, metrics } +} diff --git a/backend/src/modules/mcp/wireguard-export-acceptance.ts b/backend/src/modules/mcp/wireguard-export-acceptance.ts new file mode 100644 index 00000000..483b0cd9 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-export-acceptance.ts @@ -0,0 +1,138 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import type { FastifyInstance } from "fastify" +import { SignJWT } from "jose" +import { env } from "../../config.js" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan, loadPlan, publicPlan } from "./runtime.js" +import { humanWireguardExportMetadata, readHumanWireguardExportArtifact, readWireguardExportSource, verifyWireguardExportArtifact } from "./wireguard-export-artifact.js" + +/** Synthetic SQL/loopback fixture and authenticated owner portal downloads. */ +export async function acceptWireguardExportMcp(actor: AuthUser, app: FastifyInstance) { + const serverId=143, owner={...actor,id:"wireguard-export-owner",permissions:[...new Set([...actor.permissions,"mm:network:write"])]} + const serverPrivate=Buffer.alloc(32,71).toString("base64"), serverPublic=Buffer.alloc(32,72).toString("base64"), peerPrivate=Buffer.alloc(32,73).toString("base64"), peerPublic=Buffer.alloc(32,74).toString("base64") + let writes=0,reads=0,peerDisabled=false,keepalive="0",comment="synthetic export",oversized=false,malformed=false,afterRead:(()=>Promise)|undefined + const rows=(path:string):Record[]=>({ + "/interface/wireguard":[{".id":"*wg6",name:"wg-export","listen-port":"13231",mtu:"1420",disabled:"false","private-key":serverPrivate,"public-key":serverPublic,comment}], + "/interface":[{".id":"*if3",name:"wg-export",type:"wg",disabled:"false",running:"true"}], + "/interface/wireguard/peers":[{".id":"*p1",interface:"*if3","public-key":peerPublic,"private-key":peerPrivate,"allowed-address":"192.0.2.2/32","persistent-keepalive":keepalive,disabled:String(peerDisabled),"client-address":"192.0.2.2/32","client-dns":"1.1.1.1"}], + "/ip/address":[{".id":"*a1",interface:"*if3",address:"192.0.2.1/32",disabled:"false",dynamic:"false"}],"/ipv6/address":[], + } as Record[]>)[path]??[] + const router=createServer(async(req,res)=>{ + res.setHeader("content-type","application/json");reads++ + if(afterRead){const run=afterRead;afterRead=undefined;await run()} + if(req.method!=="GET"){writes++;res.statusCode=405;res.end("{}");return} + if(oversized){res.end(JSON.stringify("я".repeat(600000)));return} + if(malformed){res.end("{NEVER_PROVIDER_BODY");return} + res.end(JSON.stringify(rows((req.url??"").replace(/^\/rest/,"")))) + }) + await new Promise(resolve=>router.listen(0,"127.0.0.1",resolve)) + let connectionId:string|undefined + try{ + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'wg-export-fixture','127.0.0.1',$2,false,'NEVER_ROUTER_PASSWORD')",[serverId,(router.address() as {port:number}).port]) + const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"WG export fixture",mode:"autonomous",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[serverId],operationIds:["network.wireguard.export"],allowGlobal:false},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + connectionId=access.connection.id + const principal=await authenticateMcpToken(access.token),request={serverId,interfaceName:"wg-export",format:"conf" as "conf"|"rsc"|"peer-conf",includePrivateKey:true} + const prepare=(body=request)=>invokeMcpTool(principal,"mm_wireguard_export_plan",body) as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{for(let n=0;n<600;n++){const row=(await pool.query("SELECT state,result,error_code FROM mcp_operations WHERE id=$1",[id])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("Synthetic export remains active; do not replay")} + const hidden=(value:unknown)=>{const serialized=JSON.stringify(value);for(const marker of [serverPrivate,peerPrivate,"NEVER_ROUTER_PASSWORD","NEVER_PROVIDER_BODY"])assert.ok(!serialized.includes(marker),"private material must not appear in model/audit data")} + const plan=await prepare();assert.equal(plan.state,"awaiting-approval");hidden(plan);hidden(await publicPlan(await loadPlan(plan.planId))) + await assert.rejects(applyPlan(principal,plan.planId,randomUUID())) + await approvePlan(plan.planId,owner,true) + const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key),done=await wait(operation.id) + assert.equal(done.state,"applied",JSON.stringify(done));hidden(done) + const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[operation.id])).rows[0].result + assert.equal(receipt.humanDownloadOnly,true);assert.equal(receipt.wireguardContentReturned,false) + const stored=(await pool.query("SELECT * FROM mcp_artifacts WHERE id=$1",[receipt.artifactId])).rows[0] + hidden(stored);assert.equal(stored.access_scope.kind,"wireguard-export");assert.ok(stored.expires_at.getTime()<=Date.now()+900000) + const authority=stored.access_scope.authority,proof=stored.access_scope.sourceProof + assert.equal(await verifyWireguardExportArtifact(receipt.artifactId,authority,proof),true) + const token=async(id=owner.id,permissions=owner.permissions)=>new SignJWT({sub:id,apps:["mm"],permissions}).setProtectedHeader({alg:"HS256"}).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + const headers={authorization:"Bearer "+await token()} + const beforeInvalid=reads + for(const invalid of [{serverId:"abc"},{serverId:0},{serverId:1.5},{interfaceName:"wg\nunsafe"},{interfaceName:"x".repeat(201)},{peerId:""},{includePrivateKey:"true"},{unknown:"value"}]){ + const rejected=await app.inject({method:"POST",url:"/api/wireguard/export",headers,payload:{...request,...invalid}}) + assert.equal(rejected.statusCode,400,rejected.body);hidden(rejected.body) + } + assert.equal(reads,beforeInvalid,"invalid REST inputs fail at the shared boundary before RouterOS reads") + const download=()=>app.inject({url:receipt.downloadPath,headers}) + const response=await download();assert.equal(response.statusCode,200,response.body) + assert.equal(response.headers["cache-control"],"private, no-store") + assert.equal(response.headers["content-disposition"],'attachment; filename="wg-export.conf"') + assert.ok(response.body.includes(serverPrivate));assert.ok(!response.body.includes(peerPrivate)) + assert.deepEqual(response.rawPayload,(await readWireguardExportSource(authority)).content) + const listing=await app.inject({url:`/api/mcp/operations?connectionId=${connectionId}`,headers}) + assert.equal(listing.statusCode,200,listing.body);hidden(listing.json()) + assert.equal(listing.json().items.find((item:{id:string})=>item.id===operation.id).artifactReceipts[0].artifactId,receipt.artifactId) + const beforeDenied=reads + for(const deniedHeaders of [{},{authorization:"Bearer "+access.token},{authorization:"Bearer "+await token("foreign-admin")},{authorization:"Bearer "+await token(owner.id,["mm:mcp:read"])}]){ + const denied=await app.inject({url:receipt.downloadPath,headers:deniedHeaders});assert.ok([401,403].includes(denied.statusCode));hidden(denied.body) + } + assert.equal(reads,beforeDenied,"unauthorized downloads never read native source") + for(const [field,value] of [["filename","tampered.conf"],["ciphertext",stored.ciphertext+"x"],["access_scope",{...stored.access_scope,kind:"logs"}]] as const){ + await pool.query(`UPDATE mcp_artifacts SET ${field}=$2${field==="access_scope"?"::jsonb":""} WHERE id=$1`,[receipt.artifactId,field==="access_scope"?JSON.stringify(value):value]) + const denied=await download();assert.equal(denied.statusCode,409);hidden(denied.body) + await pool.query("UPDATE mcp_artifacts SET filename=$2,ciphertext=$3,access_scope=$4::jsonb WHERE id=$1",[receipt.artifactId,stored.filename,stored.ciphertext,JSON.stringify(stored.access_scope)]) + } + await pool.query("UPDATE mcp_artifacts SET expires_at=now()-interval '1 second' WHERE id=$1",[receipt.artifactId]);assert.equal((await download()).statusCode,404) + await pool.query("UPDATE mcp_artifacts SET expires_at=$2 WHERE id=$1",[receipt.artifactId,stored.expires_at]) + await assert.rejects(humanWireguardExportMetadata(receipt.artifactId,{...owner,id:"foreign-admin"})) + await assert.rejects(humanWireguardExportMetadata(receipt.artifactId,{...owner,permissions:[]})) + await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1",[operation.id]) + await assert.rejects(humanWireguardExportMetadata(receipt.artifactId,owner),"metadata requires exact generic VERIFIED checkpoint") + assert.equal((await download()).statusCode,409) + await pool.query("UPDATE mcp_operation_steps SET state='verified' WHERE operation_id=$1",[operation.id]) + const beforeList=reads;await humanWireguardExportMetadata(receipt.artifactId,owner);assert.equal(reads,beforeList,"receipt metadata never reads source or ciphertext") + await applyPlan(principal,plan.planId,key) + assert.equal((await pool.query("SELECT count(*)::int AS count FROM mcp_artifacts WHERE connection_id=$1",[connectionId])).rows[0].count,1) + for(const format of ["conf","rsc","peer-conf"] as const)for(const includePrivateKey of [false,true]){ + keepalive=format==="conf"&&!includePrivateKey?"00:00:25":"0" + const next=await prepare({...request,format,includePrivateKey});await approvePlan(next.planId,owner,true) + const op=await applyPlan(principal,next.planId,randomUUID());assert.equal((await wait(op.id)).state,"applied") + const fileReceipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[op.id])).rows[0].result + const file=await app.inject({url:fileReceipt.downloadPath,headers});assert.equal(file.statusCode,200,file.body) + assert.equal(file.body.includes(serverPrivate),includePrivateKey&&format!=="peer-conf");assert.ok(!file.body.includes(peerPrivate)) + if(keepalive!=="0")assert.ok(file.body.includes("PersistentKeepalive = 25")) + const rest=await app.inject({method:"POST",url:"/api/wireguard/export",headers,payload:{...request,serverId:String(serverId),format,includePrivateKey,...(format==="peer-conf"?{peerId:`${serverId}:*p1`}:{})}}) + assert.equal(rest.statusCode,200,rest.body);assert.equal(rest.headers["cache-control"],"private, no-store") + assert.equal(rest.json().content,file.body);assert.equal(rest.json().filename,format==="rsc"?"wg-export.rsc":format==="conf"?"wg-export.conf":"wg-export-peer.conf") + } + keepalive="0" + const omitted=await app.inject({method:"POST",url:"/api/wireguard/export",headers,payload:{serverId:String(serverId),interfaceName:"wg-export",format:"rsc"}}) + assert.equal(omitted.statusCode,200,omitted.body);assert.ok(!omitted.json().content.includes(serverPrivate)) + for(const duration of ["25s","00:00:25"]){ + keepalive=duration + for(const format of ["rsc","conf","peer-conf"] as const){const rest=await app.inject({method:"POST",url:"/api/wireguard/export",headers,payload:{...request,format,includePrivateKey:false}});assert.equal(rest.statusCode,200,rest.body);assert.ok(rest.json().content.includes(format==="rsc"?"persistent-keepalive=25":"PersistentKeepalive = 25"))} + } + keepalive="0" + console.log("✓ WG export contract delta: malformed REST inputs return 400 before native reads; numeric/unit/clock keepalives share peer codec, all REST formats and approved MCP owner download preserve 25 seconds") + const cancelledDownload=new AbortController();cancelledDownload.abort();await assert.rejects(readHumanWireguardExportArtifact(receipt.artifactId,owner,undefined,cancelledDownload.signal)) + comment="changed source";assert.equal(await verifyWireguardExportArtifact(receipt.artifactId,authority,proof),false);comment="synthetic export" + await pool.query("UPDATE mcp_artifacts SET ciphertext=ciphertext || 'x' WHERE id=$1",[receipt.artifactId]);assert.equal(await verifyWireguardExportArtifact(receipt.artifactId,authority,proof),false);await pool.query("UPDATE mcp_artifacts SET ciphertext=$2 WHERE id=$1",[receipt.artifactId,stored.ciphertext]) + const protection=randomUUID();await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"wireguard-export",id:"wg-export",serverId,protection:"deny"}),owner.id]);await assert.rejects(humanWireguardExportMetadata(receipt.artifactId,owner));await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + peerDisabled=true;await assert.rejects(prepare(),"conf cannot silently enable disabled peers");hidden(await prepare({...request,format:"rsc"}));peerDisabled=false + oversized=true;await assert.rejects(prepare());oversized=false;malformed=true;await assert.rejects(prepare());malformed=false + const controller=new AbortController();controller.abort();await assert.rejects(readWireguardExportSource(authority,async()=>{},controller.signal)) + afterRead=()=>pool.query("UPDATE servers SET password='changed fixture endpoint' WHERE id=$1",[serverId]);await assert.rejects(prepare(),(error:{code?:string})=>error.code==="STATE_CONFLICT") + await pool.query("UPDATE servers SET password='NEVER_ROUTER_PASSWORD' WHERE id=$1",[serverId]) + assert.equal(await verifyWireguardExportArtifact(receipt.artifactId,authority,proof),false,"endpoint ABA invalidates source epoch") + const fresh=await prepare();await approvePlan(fresh.planId,owner,true) + const freshOp=await applyPlan(principal,fresh.planId,randomUUID());assert.equal((await wait(freshOp.id)).state,"applied") + const freshReceipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[freshOp.id])).rows[0].result + const originalQuery=pool.query.bind(pool);let lateRevocation=false + pool.query=((...args:unknown[])=>{const pending=(originalQuery as (...args:unknown[])=>Promise)(...args);if(!lateRevocation&&String(args[0]).includes("MCP WireGuard artifact payload")){lateRevocation=true;return pending.then(async row=>{await setConnectionStatus(connectionId!,"paused",owner);return row})}return pending}) as typeof pool.query + try{await assert.rejects(readHumanWireguardExportArtifact(freshReceipt.artifactId,owner));assert.equal(lateRevocation,true)}finally{pool.query=originalQuery as typeof pool.query} + await setConnectionStatus(connectionId,"active",owner) + assert.equal((await app.inject({url:freshReceipt.downloadPath,headers})).statusCode,409,"policy ABA invalidates old packages") + hidden((await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[connectionId])).rows);assert.equal(writes,0) + console.log("✓ Synthetic WG export portal: explicit private-key opt-in for conf/rsc only, human approval, encrypted 15-minute package, owner JWT download/no model or audit keys, no-store, anonymous/MCP/foreign/permission denial before native reads, exact VERIFIED metadata, tamper/expiry/cancel/late revoke/source/protection/ABA; no native writes") + }finally{ + if(connectionId){await setConnectionStatus(connectionId,"paused",owner);await pool.query("DELETE FROM mcp_artifacts WHERE connection_id=$1",[connectionId])} + await pool.query("DELETE FROM servers WHERE id=$1",[serverId]);router.closeAllConnections();await new Promise((resolve,reject)=>router.close(error=>error?reject(error):resolve())) + } +} diff --git a/backend/src/modules/mcp/wireguard-export-actions.ts b/backend/src/modules/mcp/wireguard-export-actions.ts new file mode 100644 index 00000000..dcf3b981 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-export-actions.ts @@ -0,0 +1,39 @@ +import { fingerprint, accessGeneration, McpError } from "./security.js" +import { pool } from "../../db/index.js" +import { wireguardExportRequestSchema } from "../../services/wireguard-export.js" +import { effectiveMcpConnection } from "./store.js" +import { registerMcpService } from "./service-registry.js" +import { wireguardExportAuthority, wireguardExportAuthoritySchema, createWireguardExportArtifact, readWireguardExportSource, verifyWireguardExportArtifact } from "./wireguard-export-artifact.js" +import type { add } from "./actions.js" + +export function registerWireguardExportActions(register: typeof add) { + const authority=(input:Record)=>wireguardExportAuthoritySchema.parse(input.authority) + registerMcpService("network.wireguard.export", { + current: async input=>({proof:(await readWireguardExportSource(authority(input))).proof}), + execute: async input=>createWireguardExportArtifact(authority(input),String(input.expectedProof)), + verify: async(input,result)=>{ + const receipt=result as {artifactId?:string;humanDownloadOnly?:boolean;wireguardContentReturned?:boolean}|null + return receipt?.humanDownloadOnly===true && receipt.wireguardContentReturned===false && !!receipt.artifactId && await verifyWireguardExportArtifact(receipt.artifactId,authority(input),String(input.expectedProof)) + }, + }) + register("network.wireguard.export","mm_wireguard_export_plan","Подготовить WireGuard экспорт для скачивания владельцем после человеческого подтверждения","network","mm:network:write","write","servers",wireguardExportRequestSchema,{ + target:input=>({scope:"servers",serverIds:[wireguardExportRequestSchema.parse(input).serverId]}), + prepare:async(input,context)=>{ + const request=wireguardExportRequestSchema.parse(input) + const authorize=async()=>{ + if(context.signal?.aborted)throw new McpError("READ_CANCELLED","Экспорт WireGuard отменён",409) + const fresh=await effectiveMcpConnection(context.principal) + if(fingerprint(fresh.policy)!==fingerprint(context.connection.policy))throw new McpError("RESET_REQUIRED","Допуск экспорта изменился",409) + } + await authorize() + const access=wireguardExportAuthority(context.principal,context.connection.owner_id,context.connection.policy,await accessGeneration(),request) + const source=await readWireguardExportSource(access,authorize,context.signal) + const protections=(await pool.query("SELECT CASE WHEN octet_length(object_ref::text)<=32768 THEN object_ref ELSE NULL END AS object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) OR (object_ref->>'kind' IN ('wireguard','wireguard-export') AND object_ref->>'id'=$2) LIMIT 1001",[String(request.serverId),request.interfaceName])).rows + if(protections.length>1000 || protections.some(row=>!row.object_ref))throw new McpError("LIMIT_EXCEEDED","Каталог защит экспорта превышает предел",409) + await authorize() + return {target:{scope:"servers",serverIds:[request.serverId]},objects:[{kind:"server",id:String(request.serverId),serverId:request.serverId},{kind:"wireguard-export",id:request.interfaceName,serverId:request.serverId},...protections.map(row=>row.object_ref)],snapshots:[],before:{proof:source.proof},requiresHuman:true,mutationCost:1, + steps:[{kind:"service",service:"network.wireguard.export",input:{authority:access,expectedProof:source.proof,beforeFingerprint:fingerprint({proof:source.proof})}}], + warnings:[request.includePrivateKey && request.format!=="peer-conf" ? "Этот пакет включает приватный ключ интерфейса WireGuard. Скачивание разрешено только вошедшему владельцу на 15 минут." : "Приватные ключи в этот пакет не включаются. Файл доступен только вошедшему владельцу на 15 минут.","Модель получает только квитанцию и ссылку; содержимое, native ключи и комментарии в MCP не возвращаются. Предел файла — 1 МиБ, чтения — 45 секунд.","Изменение источника, прав, подключений или защит блокирует выдачу. Формат conf не воспроизводит отключённые интерфейсы/пиры; peer-conf не раскрывает ключи клиента.",request.format==="conf"?"Формат conf переносит параметры туннеля; имя интерфейса, имена пиров и клиентские подсказки RouterOS в нём не восстанавливаются.":request.format==="peer-conf"?"peer-conf — шаблон клиента: приватный ключ и AllowedIPs заполняются человеком; готовность клиентского подключения не подтверждается.":"RSC сохраняет поддержанные native поля и отключённые состояния; фактический путь проверяется отдельно."]} + }, + }) +} diff --git a/backend/src/modules/mcp/wireguard-export-artifact.ts b/backend/src/modules/mcp/wireguard-export-artifact.ts new file mode 100644 index 00000000..dea81fdf --- /dev/null +++ b/backend/src/modules/mcp/wireguard-export-artifact.ts @@ -0,0 +1,140 @@ +import { createHash, randomUUID, timingSafeEqual } from "node:crypto" +import { z } from "zod" +import type { PoolClient } from "pg" +import { pool } from "../../db/index.js" +import { hasPermission, type AuthUser } from "../../lib/permissions.js" +import { readWireguardExport, wireguardExportRequestSchema } from "../../services/wireguard-export.js" +import { assertVerifiedConfigurationAccess, executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js" +import { assertLogArtifactBudget } from "./log-artifacts.js" +import { assertPortalClaimsCurrent } from "./portal-revocation.js" +import { connectionWrite, effectiveMcpConnection, type McpPrincipal } from "./store.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint, McpError, privateSecretFingerprint, redact } from "./security.js" + +const MAX_BYTES = 1048576, MAX_CIPHER = 2 * 1048576 +const digest = z.string().regex(/^[a-f0-9]{64}$/) +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.iso.datetime() }).strict() +export const wireguardExportAuthoritySchema = z.object({ principal: principalSchema, ownerId: z.string().min(1).max(200), policyProof: digest, generation: digest, request: wireguardExportRequestSchema }).strict() +export type WireguardExportAuthority = z.infer +const scopeSchema = z.object({ version: z.literal(1), kind: z.literal("wireguard-export"), authority: wireguardExportAuthoritySchema, sourceProof: digest, + bytes: z.number().int().min(0).max(MAX_BYTES), sha256: digest, encryptedSha256: digest, proof: digest }).strict() +type Header = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date } +type Meta = Header & { access_scope: z.infer; encrypted_bytes: number } +const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "wireguard-export-artifact-v1") +const signed = (header: Header, scope: Omit, "proof">) => ({ id:header.id,connection_id:header.connection_id,filename:header.filename,media_type:header.media_type,expires_at:header.expires_at.toISOString(),scope }) +const fail = (message: string, code = "ARTIFACT_INVALID") => new McpError(code, message, 409) + +export async function assertWireguardExportAccess(raw: unknown, client: Pick = pool) { + const authority = wireguardExportAuthoritySchema.parse(raw), connection = await effectiveMcpConnection(authority.principal, client) + if (connection.owner_id !== authority.ownerId || !hasPermission(connection.owner_permissions,"mm:network:write") || !hasPermission(connection.policy.permissions,"mm:network:write") || !connection.policy.scope.operationIds.includes("network.wireguard.export") || !connection.policy.scope.serverIds.includes(authority.request.serverId)) throw new McpError("PERMISSION_DENIED","Нет допуска к экспорту WireGuard",403) + if (fingerprint(connection.policy) !== authority.policyProof || await accessGeneration() !== authority.generation) throw fail("Допуск изменился; подготовьте скачивание заново","RESET_REQUIRED") + const denied=(await client.query("SELECT 1 FROM mcp_protections WHERE object_ref->>'protection'='deny' AND (object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) OR (object_ref->>'kind' IN ('wireguard','wireguard-export') AND object_ref->>'id'=$2)) LIMIT 1",[String(authority.request.serverId),authority.request.interfaceName])).rows[0] + if(denied)throw fail("Экспорт WireGuard заблокирован защитой объекта","PROTECTED_OBJECT") + return { connection, serverIds: [authority.request.serverId] } +} + +async function protectionProof(authority: WireguardExportAuthority, client: Pick = pool) { + const rows = (await client.query("SELECT id,xmin::text AS revision,CASE WHEN octet_length(object_ref::text)<=32768 THEN object_ref ELSE NULL END AS object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) OR (object_ref->>'kind' IN ('wireguard','wireguard-export') AND object_ref->>'id'=$2) ORDER BY id LIMIT 1001", [String(authority.request.serverId),authority.request.interfaceName])).rows + if (rows.length>1000 || rows.some(row=>!row.object_ref)) throw fail("Каталог защит экспорта превышает предел", "LIMIT_EXCEEDED") + return sign({protections:rows}) +} +export async function readWireguardExportSource(authority: WireguardExportAuthority, authorize: () => Promise = async()=>{}, signal?: AbortSignal) { + await authorize(); await assertWireguardExportAccess(authority) + const protections=await protectionProof(authority) + const source=await readWireguardExport(authority.request, { signal, authorize: async()=>{ + await authorize(); await assertWireguardExportAccess(authority) + if (await protectionProof(authority)!==protections) throw fail("Защиты экспорта изменились", "STATE_CONFLICT") + } }) + return {...source,protectionProof:protections,proof:await sign({nativeSource:source.proof,protections})} +} + +const receipt = (header: Header, scope: Pick, "bytes" | "sha256">) => ({artifactId:header.id,downloadPath:`/api/mcp/artifacts/${header.id}`,expiresInSeconds:900,bytes:scope.bytes,sha256:scope.sha256,humanDownloadOnly:true,wireguardContentReturned:false,configurationApplied:false}) + +export async function createWireguardExportArtifact(authority: WireguardExportAuthority, expectedProof: string) { + const authorize = assertVerifiedConfigurationAccess + return executeGuardedApplicationWrite({kind:"wireguard-export",id:authority.request.interfaceName},(await assertWireguardExportAccess(authority)).serverIds,async()=>{ + const source = await readWireguardExportSource(authority,authorize) + if (source.proof!==expectedProof) throw fail("Экспорт WireGuard изменился после подтверждения","STATE_CONFLICT") + const ciphertext = await encryptSecret(source.content.toString("base64")) + if(Buffer.byteLength(ciphertext)>MAX_CIPHER) throw fail("Зашифрованный пакет превышает предел","EXPORT_LIMIT") + const header: Header = { id: randomUUID(), connection_id: authority.principal.connectionId, filename: source.filename, media_type: "application/octet-stream", expires_at: new Date(Date.now()+900000) } + const access = { version: 1 as const, kind: "wireguard-export" as const, authority, sourceProof: source.proof, bytes: source.content.length, sha256: hash(source.content), encryptedSha256: hash(ciphertext) } + const scope = scopeSchema.parse({...access,proof:await sign(signed(header,access))}) + await authorize() + if ((await readWireguardExportSource(authority,authorize)).proof!==source.proof) throw fail("Конфигурация изменилась во время формирования пакета","STATE_CONFLICT") + await connectionWrite(async client=>{ + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE",[header.connection_id]) + await assertWireguardExportAccess(authority,client) + if(await protectionProof(authority,client)!==source.protectionProof) throw fail("Защиты экспорта изменились перед сохранением", "STATE_CONFLICT") + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))") + await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()") + const usage = (await client.query(`SELECT count(*) FILTER(WHERE connection_id=$1)::text AS files,COALESCE(sum(octet_length(ciphertext)) FILTER(WHERE connection_id=$1),0)::text AS own_bytes,COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts`,[header.connection_id])).rows[0] + assertLogArtifactBudget(BigInt(usage.files),BigInt(usage.own_bytes),BigInt(usage.total_bytes),Buffer.byteLength(ciphertext)) + await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)",[header.id,header.connection_id,header.filename,header.media_type,ciphertext,JSON.stringify(scope),header.expires_at]) + }) + await authorize();await assertWireguardExportAccess(authority) + return receipt(header,access) + }) +} + +async function loadMeta(id: string): Promise { + const row = (await pool.query("/* MCP WireGuard artifact metadata */ SELECT id,connection_id,filename,media_type,expires_at,CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope,octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND expires_at>now() AND octet_length(filename)<=1024 AND octet_length(media_type)<=200",[id])).rows[0] + if (!row) throw new McpError("NOT_FOUND","Пакет отсутствует или срок истёк",404) + const scope = scopeSchema.safeParse(row.access_scope) + if (!scope.success || !(row.expires_at instanceof Date) || !Number.isFinite(row.expires_at.getTime()) || !Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes>MAX_CIPHER) throw fail("Метаданные пакета не подтверждены") + const {proof,...access} = scope.data, expected = await sign(signed(row,access)) + if (!timingSafeEqual(Buffer.from(proof,"hex"),Buffer.from(expected,"hex")) || row.connection_id!==access.authority.principal.connectionId) throw fail("Подпись пакета не совпала") + return {...row,access_scope:scope.data} +} +async function assertVerifiedCheckpoint(meta: Meta) { + const scope=meta.access_scope + const step={kind:"service",service:"network.wireguard.export",input:{authority:scope.authority,expectedProof:scope.sourceProof,beforeFingerprint:fingerprint({proof:scope.sourceProof})}} + const matches=(await pool.query("SELECT s.operation_id FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id JOIN mcp_plans p ON p.id=o.plan_id WHERE o.connection_id=$1 AND o.state='applied' AND p.operation_id='network.wireguard.export' AND p.approved_by IS NOT NULL AND s.state='verified' AND s.intended_state=$2::jsonb AND s.result=$3::jsonb LIMIT 2",[meta.connection_id,JSON.stringify(redact(step)),JSON.stringify(receipt(meta,scope))])).rows + if(matches.length!==1)throw fail("Нет точной проверенной квитанции человеческого экспорта", "STATE_CONFLICT") +} +export async function verifyWireguardExportArtifact(id: string, authority: WireguardExportAuthority, sourceProof: string) { + const meta=await loadMeta(id) + await assertWireguardExportAccess(authority) + if(fingerprint(meta.access_scope.authority)!==fingerprint(authority) || meta.access_scope.sourceProof!==sourceProof)return false + const payload=(await pool.query<{ciphertext:string}>("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152",[id,meta.access_scope.proof])).rows[0] + await assertWireguardExportAccess(authority) + return !!payload && hash(payload.ciphertext)===meta.access_scope.encryptedSha256 && (await readWireguardExportSource(authority)).proof===sourceProof +} +/** Receipt listing validates sealed metadata and access without reading source or artifact payload. */ +export async function humanWireguardExportMetadata(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent) { + const meta = await loadMeta(id), scope = meta.access_scope + await checkClaims(actor) + if (actor.id !== scope.authority.ownerId || !hasPermission(actor.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Файл доступен только владельцу", 403) + await assertWireguardExportAccess(scope.authority) + await assertVerifiedCheckpoint(meta) + return scope +} +export async function readHumanWireguardExportArtifact(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent, signal?:AbortSignal) { + const deadline=AbortSignal.timeout(45000),bounded=signal?AbortSignal.any([signal,deadline]):deadline + bounded.throwIfAborted() + const meta=await loadMeta(id), scope=meta.access_scope + const authorize=async()=>{ + bounded.throwIfAborted() + await checkClaims(actor) + if(actor.id!==scope.authority.ownerId || !hasPermission(actor.permissions,"mm:network:write")) throw new McpError("PERMISSION_DENIED","Экспорт WireGuard доступен только владельцу с правом управления сетью",403) + await assertWireguardExportAccess(scope.authority) + await assertVerifiedCheckpoint(meta) + if(meta.expires_at.getTime()<=Date.now()) throw fail("Срок пакета истёк") + } + await authorize() + if((await readWireguardExportSource(scope.authority,authorize,bounded)).proof!==scope.sourceProof) throw fail("Экспорт WireGuard изменился; подготовьте новый пакет","STATE_CONFLICT") + const payload=(await pool.query<{ciphertext:string}>("/* MCP WireGuard artifact payload */ SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152",[id,scope.proof])).rows[0] + await authorize() + if(!payload || hash(payload.ciphertext)!==scope.encryptedSha256) throw fail("Зашифрованный пакет изменился") + let content: Buffer + try{content=Buffer.from(await decryptSecret(payload.ciphertext),"base64")}catch{throw fail("Пакет не расшифрован")} + await authorize() + if(Buffer.byteLength(content)!==scope.bytes || hash(content)!==scope.sha256) throw fail("Размер или SHA-256 пакета не совпал") + if((await readWireguardExportSource(scope.authority,authorize,bounded)).proof!==scope.sourceProof) throw fail("Конфигурация изменилась во время выдачи пакета","STATE_CONFLICT") + await authorize() + return {filename:meta.filename,mediaType:meta.media_type,content} +} + +export function wireguardExportAuthority(principal: McpPrincipal, ownerId: string, policy: unknown, generation: string, request: unknown) { + return wireguardExportAuthoritySchema.parse({principal,ownerId,policyProof:fingerprint(policy),generation,request}) +} diff --git a/backend/src/modules/mcp/wireguard-import-acceptance.ts b/backend/src/modules/mcp/wireguard-import-acceptance.ts new file mode 100644 index 00000000..5f40c4d5 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-import-acceptance.ts @@ -0,0 +1,317 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { wgImportPreviewSchema, wgImportResponseSchema } from "@mmapp/contracts/wireguard" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { wireguardInterfaceDependencyPaths } from "../../services/wireguard-interface-delete.js" +import { wireguardPublicKey } from "../../services/wireguard-interface-create.js" +import { WIREGUARD_IMPORT_MAX_BYTES } from "../../services/wireguard-import-plan.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { encryptSecret, fingerprint } from "./security.js" +import { WIREGUARD_IMPORT_CONTENT_PURPOSE } from "./wireguard-import-actions.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" +import wireguardRoutes from "../../routes/wireguard.js" + +type Rows = Record[]> +/** Explicitly invoked by the isolated SQL acceptance harness. Network traffic + * is restricted to this loopback RouterOS double; never run against a router. */ +export async function acceptWireguardImport(actor: AuthUser) { + const key = (byte: number) => Buffer.alloc(32, byte).toString("base64") + const privateKey = key(71), peerOne = key(72), peerTwo = key(73), generated = key(74) + const content = `/interface wireguard add name=wg-import private-key="${privateKey}" listen-port=54321 mtu=1300 comment="imported\\nsecond\\tline"\n/ipv6 address add interface=wg-import address=2001:db8:141::1/64 advertise=no\n/interface wireguard peers add interface=wg-import public-key="${peerOne}" allowed-address=192.0.2.2/32 endpoint-address=vpn.example.org endpoint-port=54321 private-key=auto client-address=10.0.0.2 client-dns=1.1.1.1 client-endpoint=vpn.example.org name=first persistent-keepalive=25 comment="first\\r\\npeer"\n/interface wireguard peers add interface=wg-import public-key="${peerTwo}" allowed-address=2001:db8:141::2/128 private-key=none name=second disabled=yes` + const initial: Rows = Object.fromEntries([...wireguardInterfaceDependencyPaths, "/interface/wireguard", "/interface/wireguard/peers", "/ip/address", "/ipv6/address"].map(path => [path, []])) + initial["/interface/wireguard"] = [{ ".id": "*f", name: "wg-foreign", disabled: "false", "public-key": wireguardPublicKey(generated), "private-key": "NEVER_IMPORT_FOREIGN_KEY", opaque: "NEVER_IMPORT_FOREIGN_OPAQUE" }] + initial["/interface"] = [{ ".id": "*f", name: "wg-foreign", type: "wg" }] + let rows = structuredClone(initial), reads = 0, commands: string[] = [], commandBodies: Record[] = [], mode = "normal", number = 0, insertedServers = false + const reset = () => { rows = structuredClone(initial); commands = []; commandBodies = []; mode = "normal"; number = 0 } + const assertPublic = (value: unknown) => { + const text = typeof value === "string" ? value : JSON.stringify(value) + 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/, "")) + 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 (!["PUT", "PATCH"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return } + let raw = "" + req.on("data", chunk => { raw += chunk }); req.on("end", () => { + const body = JSON.parse(raw) as Record + commands.push(req.method + " " + path) + commandBodies.push(structuredClone(body)) + if (mode === "reject" && commands.length === 4) { res.statusCode = 422; res.end('{"message":"fixture reject"}'); return } + let receipt: Record = {} + if (req.method === "PUT") { + if (!Object.hasOwn(rows, path)) { res.statusCode = 400; res.end("{}"); return } + const row: Record = { ".id": "*" + (++number).toString(16), ...body, dynamic: "false", opaque: "NEVER_IMPORT_NEW_DEFAULT" } + if (path === "/interface/wireguard") { Object.assign(row, { "public-key": wireguardPublicKey(privateKey) }); rows["/interface"].push({ ".id": row[".id"], name: row.name, type: "wg" }) } + if (row["private-key"] === "auto") row["private-key"] = generated + rows[path].push(row); receipt = { ".id": row[".id"] } + } else { + const id = path.split("/").at(-1)!, base = path.slice(0, path.lastIndexOf("/")) + const row = rows[base]?.find(row => row[".id"] === id) + if (!row) { res.statusCode = 404; res.end("{}"); return }; Object.assign(row, body) + if (base === "/interface/wireguard" && row.name === "wg-import" && body.disabled === "false") { + // Real RouterOS normalizes ignored X25519 bits when enabling the + // selected interface. Keep the observed public key corroboration. + const scalar = Buffer.from(row["private-key"], "base64") + scalar[0] &= 248; scalar[31] = (scalar[31] & 127) | 64 + row["private-key"] = scalar.toString("base64") + row["public-key"] = wireguardPublicKey(row["private-key"]) + } + } + if (mode === "collateral" && commands.length === 3) rows["/interface/wireguard"][0].opaque = "NEVER_IMPORT_CHANGED_FOREIGN" + if (mode === "lost" && commands.length === 4) { req.socket.destroy(); return } + res.end(JSON.stringify(receipt)) + }) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const secretIds: string[] = [], connectionIds: string[] = [] + const secret = async (purpose: string, serverId: number, owner = actor.id, text = content) => { + const id = randomUUID(); secretIds.push(id) + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, purpose, serverId, await encryptSecret(text), owner]); return id + } + const wait = async (id: string) => { + for (let i = 0; i < 3000; i++) { const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) } + throw Error("WireGuard import remains live; never replay") + } + try { + assert.equal((await pool.query("SELECT id FROM servers WHERE id IN (141,142)")).rows.length, 0, "Import fixture IDs must be unused") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(141,'wg-import-fixture','127.0.0.1',$1,false,'NEVER_IMPORT_ENDPOINT'),(142,'wg-import-outside','127.0.0.1',$1,false,'NEVER_IMPORT_OUTSIDE')", [(router.address() as { port: number }).port]) + insertedServers = true + await rest.register(wireguardRoutes, { prefix: "/api" }) + console.log("WireGuard import fixture: local REST/MCP router and owned SQL rows prepared") + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "WireGuard import", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [141], operationIds: ["network.wireguard.import.preview", "network.wireguard.import", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 200 } }), actor) + connectionIds.push(issued.connection.id) + const principal = await authenticateMcpToken(issued.token), contentRef = await secret(WIREGUARD_IMPORT_CONTENT_PURPOSE, 141) + const full = { serverId: 141, contentRef, format: "auto" } + const prepare = (input: unknown = full) => invokeMcpTool(principal, "mm_wireguard_import_plan", input) as Promise<{ planId: string; state: string; changes: { mutationCost: number; objects: { kind: string; id: string; serverId?: number }[] } }> + const beforeInvalid = reads + for (const input of [{ ...full, content }, { ...full, serverId: 142 }, { ...full, format: "script" }]) await assert.rejects(prepare(input)) + for (const ref of [await secret("wrong-purpose", 141), await secret(WIREGUARD_IMPORT_CONTENT_PURPOSE, 142), await secret(WIREGUARD_IMPORT_CONTENT_PURPOSE, 141, "wrong-owner"), await secret(WIREGUARD_IMPORT_CONTENT_PURPOSE, 141, actor.id, "x".repeat(WIREGUARD_IMPORT_MAX_BYTES + 1))]) await assert.rejects(prepare({ ...full, contentRef: ref })) + assert.equal(reads, beforeInvalid); assert.equal(commands.length, 0) + const preview = await invokeMcpTool(principal, "mm_wireguard_import_preview", full) + for (const secret of [privateKey, peerOne, peerTwo]) assert.equal(JSON.stringify(preview).includes(secret), false) + assert.equal(reads, beforeInvalid, "Parser preview does not read native state") + const restInput = { serverId: 141, content, format: "auto" } + const sql = pool.query.bind(pool) + let invalidSql = 0 + pool.query = ((...args: unknown[]) => { invalidSql++; return (sql as (...args: unknown[]) => unknown)(...args) }) as typeof pool.query + const readsBeforeInvalidRest = reads + try { + for (const serverId of ["", "141x", "141.5", "0", "-1", "9007199254740992", 0, -1, 1.5, null, false]) { + for (const dryRun of [true, false]) { + const response = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: { ...restInput, serverId, dryRun } }) + assert.equal(response.statusCode, 400); assertPublic(response.body) + } + } + assert.equal(invalidSql, 0, "Invalid REST server IDs must be rejected before any SQL") + } finally { pool.query = sql as typeof pool.query } + assert.equal(reads, readsBeforeInvalidRest); assert.equal(commands.length, 0) + const dry = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: { ...restInput, dryRun: true } }) + assert.equal(dry.statusCode, 200); assertPublic(dry.body) + const dryBody = wgImportResponseSchema.parse(dry.json()) + assert.equal(dryBody.dryRun, true); assert.equal(dryBody.applied, undefined) + assert.deepEqual(dryBody.preview.interface, { name: "wg-import", listenPort: 54321, mtu: 1300, comment: "imported\nsecond\tline", address: "2001:db8:141::1/64" }) + assert.equal(dryBody.preview.peers.length, 2) + assert.deepEqual(dryBody.preview.peers[0], { publicKey: "[KEY]", allowedAddresses: ["192.0.2.2/32"], endpointAddress: "vpn.example.org", endpointPort: 54321, persistentKeepalive: 25, comment: "first\r\npeer", name: "first", clientAddress: "10.0.0.2", clientDns: "1.1.1.1", clientEndpoint: "vpn.example.org" }) + assert.equal(dryBody.preview.peers[1].disabled, true) + const mcpPreview = (preview as { preview: { peers: Record[] } }).preview + assert.deepEqual(dryBody.preview, wgImportPreviewSchema.parse({ ...mcpPreview, peers: mcpPreview.peers.map(peer => ({ ...peer, publicKey: "[KEY]" })) }), "REST retains UI preview shape while carrying the same complete safe metadata") + assert.equal(reads, readsBeforeInvalidRest); assert.equal(commands.length, 0) + const failedParse = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: { ...restInput, content: content + `\n/system script add source="${privateKey}"`, dryRun: false } }) + assert.equal(failedParse.statusCode, 400); assertPublic(failedParse.body) + assert.equal(reads, readsBeforeInvalidRest) + reset() + const appliedRest = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: restInput }) + assert.equal(appliedRest.statusCode, 200, appliedRest.body); assertPublic(appliedRest.body) + const appliedRestBody = appliedRest.json() as { dryRun: boolean; preview: unknown; applied: { interfaceName: string; peersCreated: number; verified: boolean; completedCommands: number; commandCount: number } } + assert.equal(wgImportResponseSchema.safeParse(appliedRestBody).success, true) + assert.equal(appliedRestBody.dryRun, false); assert.deepEqual(appliedRestBody.preview, dryBody.preview) + assert.deepEqual(appliedRestBody.applied, { interfaceName: "wg-import", peersCreated: 2, verified: true, completedCommands: 7, commandCount: 7 }) + assert.equal(commands.length, 7) + assert.deepEqual(commandBodies.slice(0, 4).map(body => body.disabled), ["true", "true", "true", "true"]) + assert.equal(rows["/interface/wireguard"][1].disabled, "false"); assert.equal(rows["/interface/wireguard"][1].comment, "imported\nsecond\tline") + assert.equal(rows["/interface/wireguard/peers"].length, 2); assert.equal(rows["/interface/wireguard/peers"][0].disabled, "false"); assert.equal(rows["/interface/wireguard/peers"][1].disabled, "true") + assert.equal(rows["/interface/wireguard/peers"][0].comment, "first\r\npeer") + assert.equal(rows["/ipv6/address"][0].address, "2001:db8:141::1/64"); assert.equal(rows["/ipv6/address"][0].disabled, "false"); assert.equal(rows["/ipv6/address"][0].advertise, "false") + assert.equal(fingerprint(rows["/interface/wireguard"][0]), fingerprint(initial["/interface/wireguard"][0]), "Foreign native/opaque properties must survive REST import") + const restNative = { commands: [...commands], bodyProof: fingerprint(commandBodies) } + const revisions = (await pool.query("SELECT source,payload FROM config_revisions WHERE server_id=141 AND section='wireguard' ORDER BY created_at DESC,id DESC")).rows + assert.ok(revisions.length > 0); assert.equal(revisions[0].source, "copy") + const saved = (revisions[0].payload as { interfaces: { name: string; peers: unknown[]; innerAddresses: { address: string }[] }[] }).interfaces.find(iface => iface.name === "wg-import") + assert.ok(saved); assert.equal(saved.peers.length, 2); assert.equal(saved.innerAddresses[0].address, "2001:db8:141::1/64") + for (const fault of ["reject", "lost"]) { + reset(); mode = fault + const beforeRevisions = (await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE server_id=141 AND section='wireguard'")).rows[0].n + const response = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: restInput }) + assert.equal(response.statusCode, 502); assertPublic(response.body) + const failure = response.json() as { state: string; verifiedPrefixCommands: number; commandCount: number; automaticReplay: boolean; preview: unknown } + assert.equal(failure.state, fault === "reject" ? "partial" : "needs-review"); assert.equal(failure.verifiedPrefixCommands, 3); assert.equal(failure.commandCount, 7); assert.equal(failure.automaticReplay, false) + assert.deepEqual(failure.preview, dryBody.preview); assert.equal(commands.length, 4) + assert.equal(rows["/interface/wireguard/peers"].length, fault === "reject" ? 1 : 2) + assert.equal(rows["/interface/wireguard"][1].disabled, "true"); assert.equal(rows["/ipv6/address"][0].disabled, "true") + const repeat = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: restInput }) + assert.notEqual(repeat.statusCode, 200); assertPublic(repeat.body); assert.equal(commands.length, 4, "REST does not automatically replay or clean up a partially created interface") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE server_id=141 AND section='wireguard'")).rows[0].n, beforeRevisions) + } + reset() + console.log("WireGuard import REST parity: strict IDs before SQL/HTTP, full redacted preview, seven shared stages/two peers/IPv6, copy revision, partial/unknown no replay") + const limited = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "WireGuard import object budget", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [141], operationIds: ["network.wireguard.import"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 200, objectsPerPlan: 2 } }), actor) + connectionIds.push(limited.connection.id) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(limited.token), "mm_wireguard_import_plan", full), (error: { code?: string }) => error.code === "PLAN_LIMIT_EXCEEDED") + assert.equal(commands.length, 0) + let plan = await prepare(); assert.equal(plan.state, "awaiting-approval"); assert.equal(plan.changes.mutationCost, 8) + await assert.rejects(applyPlan(principal, plan.planId, randomUUID())); assert.equal(commands.length, 0) + for (const fault of ["normal", "lost", "reject", "collateral"]) { + reset(); plan = await prepare(); await approvePlan(plan.planId, actor, true); mode = fault + const idempotency = randomUUID(), operation = await applyPlan(principal, plan.planId, idempotency), completed = await wait(operation.id) + assert.equal(completed.state, fault === "normal" ? "applied" : fault === "reject" ? "partial" : "unknown", JSON.stringify({ fault, ...completed })) + const sent: number = commands.length + assert.equal(sent, fault === "normal" ? 7 : fault === "collateral" ? 3 : 4) + assert.equal((await applyPlan(principal, plan.planId, idempotency)).id, operation.id); assert.equal(commands.length, sent) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; verifiedPrefixCommands: number | null }[] } + assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results[0].matchesDesiredState, fault === "normal") + assert.equal(reconciliation.results[0].verifiedPrefixCommands, fault === "normal" ? 7 : fault === "reject" ? 3 : null) + assert.equal(commands.length, sent) + if (fault === "normal") { + assert.deepEqual(commands, restNative.commands); assert.equal(fingerprint(commandBodies), restNative.bodyProof, "REST and MCP dispatch exactly the same complete native payloads") + const step = (await pool.query("SELECT intended_state,result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0] + for (const secret of [privateKey, peerOne, peerTwo, "NEVER_IMPORT_"]) assert.equal(JSON.stringify(step).includes(secret), false) + assert.equal(step.result.peersCreated, 2); assert.equal(step.result.completedCommands, 7) + assert.equal(rows["/interface/wireguard/peers"][0].disabled, "false"); assert.equal(rows["/interface/wireguard/peers"][1].disabled, "true") + const intended = step.intended_state, savedResult = step.result + for (const state of ["intent", "applied", "sent"]) { + await pool.query("UPDATE mcp_operation_steps SET state=$2 WHERE operation_id=$1", [operation.id, state]) + const lostFinal = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { results: { matchesDesiredState: boolean; configurationMatchesDesiredState: boolean }[] } + assert.equal(lostFinal.results[0].matchesDesiredState, false) + assert.equal(lostFinal.results[0].configurationMatchesDesiredState, true, "Final audit prefix is native proof, not a successful operation acknowledgement") + } + await pool.query("UPDATE mcp_operation_steps SET state='verified',intended_state=$2::jsonb,result=$3::jsonb WHERE operation_id=$1", [operation.id, JSON.stringify({ ...intended, service: "other" }), JSON.stringify(savedResult)]) + assert.equal((await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { results: { matchesDesiredState: boolean }[] }).results[0].matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_steps SET state='verified',intended_state=$2::jsonb,result=$3::jsonb WHERE operation_id=$1", [operation.id, JSON.stringify(intended), JSON.stringify(savedResult)]) + } + } + reset(); plan = await prepare(); await approvePlan(plan.planId, actor, true) + // Persist the final native-prefix proof, then lose its acknowledgement to + // the service. Native configuration is complete, generic VERIFIED is absent. + const checkpointQuery = pool.query.bind(pool) + let finalCheckpointAckLost = false + pool.query = ((...args: unknown[]) => { + const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text + const params = args[1] as unknown[] | undefined + const pending = (checkpointQuery as (...args: unknown[]) => Promise)(...args) + if (!finalCheckpointAckLost && sql?.startsWith("INSERT INTO mcp_audit") && params?.[1] === "wireguard.import.checkpoint" && String(params[3]).includes('"completedCommands":7')) { + finalCheckpointAckLost = true + return pending.then(() => { throw Error("Fixture final checkpoint acknowledgement lost") }) + } + return pending + }) as typeof pool.query + try { + const replayKey = randomUUID(), unknown = await applyPlan(principal, plan.planId, replayKey) + assert.equal((await wait(unknown.id)).state, "unknown"); assert.equal(finalCheckpointAckLost, true); assert.equal(commands.length, 7) + const step = (await checkpointQuery("SELECT state,result FROM mcp_operation_steps WHERE operation_id=$1", [unknown.id])).rows[0] + assert.equal(step.state, "intent"); assert.equal(step.result, null) + const lost = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: unknown.id }) as { results: { matchesDesiredState: boolean; configurationMatchesDesiredState: boolean; verifiedFinalCheckpointAvailable: boolean; verifiedPrefixCommands: number | null }[] } + assert.equal(lost.results[0].configurationMatchesDesiredState, true); assert.equal(lost.results[0].verifiedPrefixCommands, 7) + assert.equal(lost.results[0].matchesDesiredState, false); assert.equal(lost.results[0].verifiedFinalCheckpointAvailable, false) + assert.equal((await applyPlan(principal, plan.planId, replayKey)).id, unknown.id); assert.equal(commands.length, 7) + } finally { pool.query = checkpointQuery as typeof pool.query } + reset(); plan = await prepare(); await approvePlan(plan.planId, actor, true) + console.log("WireGuard import MCP phases: human-only native outcomes and exact generic VERIFIED checkpoint variants checked") + const individual = plan.changes.objects.filter(object => object.kind === "wireguard_peer")[1] + assert.ok(individual) + const lateProtection = randomUUID(), originalQuery = pool.query.bind(pool) + let protectedAfterIntent = false + pool.query = ((...args: unknown[]) => { + const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text + const params = args[1] as unknown[] | undefined + const pending = (originalQuery as (...args: unknown[]) => Promise)(...args) + if (!protectedAfterIntent && sql?.startsWith("INSERT INTO mcp_audit") && params?.[1] === "configuration.intent" && String(params[3]).includes('"path":"/interface/wireguard"')) { + protectedAfterIntent = true + return pending.then(async value => { + // Omit serverId deliberately: the logical-object closure must catch + // this exact protected import peer after the durable native intent. + await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [lateProtection, { kind: individual.kind, id: individual.id, protection: "deny" }, actor.id]) + return value + }) + } + return pending + }) as typeof pool.query + try { + assert.equal((await wait((await applyPlan(principal, plan.planId, randomUUID())).id)).state, "unknown") + assert.equal(protectedAfterIntent, true); assert.equal(commands.length, 0) + } finally { pool.query = originalQuery as typeof pool.query; await pool.query("DELETE FROM mcp_protections WHERE id=$1", [lateProtection]) } + // A native ID is device-local. A different server's *3 protection must not + // block this import; a new same-server protection after durable activation + // intent must stop before the first PATCH, with the disabled prefix intact. + for (const transport of ["rest", "mcp"]) { + reset() + const outsideProtection = randomUUID(), nativeProtection = randomUUID(), query = pool.query.bind(pool) + let insertedAfterPeerIntent = false + await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [outsideProtection, { kind: "wireguard_peer", id: "*3", serverId: 142, protection: "deny" }, actor.id]) + try { + const nativePlan = transport === "mcp" ? await prepare() : undefined + if (nativePlan) await approvePlan(nativePlan.planId, actor, true) + pool.query = ((...args: unknown[]) => { + const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text + const params = args[1] as unknown[] | undefined + const pending = (query as (...args: unknown[]) => Promise)(...args) + if (!insertedAfterPeerIntent && sql?.startsWith("INSERT INTO mcp_audit") && params?.[1] === "configuration.intent" && String(params[3]).includes('"path":"/interface/wireguard/peers/*3"')) { + insertedAfterPeerIntent = true + return pending.then(async value => { + await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [nativeProtection, { kind: "wireguard_peer", id: "*3", serverId: 141, protection: "deny" }, actor.id]) + return value + }) + } + return pending + }) as typeof pool.query + if (nativePlan) { + const idempotency = randomUUID(), operation = await applyPlan(principal, nativePlan.planId, idempotency) + assert.equal((await wait(operation.id)).state, "unknown") + assert.equal((await applyPlan(principal, nativePlan.planId, idempotency)).id, operation.id) + } else { + const response = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: restInput }) + assert.equal(response.statusCode, 502, response.body); assertPublic(response.body) + assert.equal(response.json().state, "needs-review"); assert.equal(response.json().verifiedPrefixCommands, 4) + assert.equal(response.json().automaticReplay, false) + assert.notEqual((await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: restInput })).statusCode, 200) + } + assert.equal(insertedAfterPeerIntent, true, transport) + assert.equal(commands.length, 4, "Four creations, no activation or replay after late native protection") + assert.ok(commands.every(command => command.startsWith("PUT "))) + assert.equal(rows["/interface/wireguard"][1].disabled, "true") + assert.ok(rows["/interface/wireguard/peers"].every(row => row.disabled === "true")) + assert.equal(rows["/ipv6/address"][0].disabled, "true") + } finally { + pool.query = query as typeof pool.query + await query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [[outsideProtection, nativeProtection]]) + } + } + console.log("WireGuard import native protection scope: other-server duplicate ID allowed; late same-server peer protection after durable intent stops REST/MCP before PATCH, prefix four disabled, no replay") + reset(); plan = await prepare(); await approvePlan(plan.planId, actor, true) + await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [contentRef, await encryptSecret(content + "\n# changed revision")]) + assert.equal((await wait((await applyPlan(principal, plan.planId, randomUUID())).id)).state, "rejected"); assert.equal(commands.length, 0) + await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [contentRef, await encryptSecret(content)]) + const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [protection, { kind: "wireguard-import", id: "wg-import", serverId: 141, protection: "deny" }, actor.id]) + try { await assert.rejects(prepare()); assert.equal(commands.length, 0) } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + const audited = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1", [issued.connection.id])).rows + for (const secret of [privateKey, peerOne, peerTwo, "NEVER_IMPORT_"]) assert.equal(JSON.stringify(audited).includes(secret), false) + console.log("WireGuard import SQL/loopback helper: contentRef owner/purpose/server/revision, strict preview, human approval, multi-peer disabled staging, checkpoints, partial/unknown no replay, reconciliation and privacy") + } finally { + await rest.close() + for (const id of connectionIds) await setConnectionStatus(id, "paused", actor) + await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])", [secretIds]) + if (insertedServers) { + await pool.query("DELETE FROM config_revisions WHERE server_id IN (141,142)") + await pool.query("DELETE FROM servers WHERE id IN (141,142)") + } + router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/modules/mcp/wireguard-import-actions.test.ts b/backend/src/modules/mcp/wireguard-import-actions.test.ts new file mode 100644 index 00000000..52bee74b --- /dev/null +++ b/backend/src/modules/mcp/wireguard-import-actions.test.ts @@ -0,0 +1,47 @@ +import assert from "node:assert/strict" +import { wireguardImportReferenceSchema, WIREGUARD_IMPORT_CONTENT_PURPOSE, hasVerifiedWireguardImportCheckpoint } from "./wireguard-import-actions.js" +import { redact } from "./security.js" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { pool } from "../../db/index.js" +import wireguardRoutes from "../../routes/wireguard.js" + +const full = { serverId: 141, contentRef: "d31deace-6412-4b5f-bd87-2d3af9ea51f9" } +assert.equal(WIREGUARD_IMPORT_CONTENT_PURPOSE, "wireguard-import-config") +assert.deepEqual(wireguardImportReferenceSchema.parse(full), { ...full, format: "auto" }) +for (const value of [{ ...full, content: "[Interface]" }, { ...full, privateKey: "key" }, { ...full, parsed: {} }, { ...full, format: "script" }, { ...full, serverId: 0 }, { ...full, contentRef: "raw-config" }]) assert.equal(wireguardImportReferenceSchema.safeParse(value).success, false) +console.log("WireGuard import MCP schema: only owner-bound content reference/server/format, no plaintext config or keys") +const input = { principal: { connectionId: "connection" }, receiptId: "unique", requestProof: "source-proof", proofRevision: 1 } +const checkpoint = { completedCommands: 7, commandCount: 7, afterProof: "after", interfaceId: "*1", peerIds: ["*3", "*4"] } +const candidate = { connection_id: "connection", state: "verified", intended_state: redact({ kind: "service", service: "network.wireguard.import", input }), result: { verified: true, completedCommands: 7, commandCount: 7, afterProof: "after", interfaceId: "*1", peerIds: ["*4", "*3"], peersCreated: 2 } } +assert.equal(hasVerifiedWireguardImportCheckpoint(candidate, input, checkpoint, 2), true) +for (const changed of [null, { ...candidate, state: "applied" }, { ...candidate, state: "intent" }, { ...candidate, state: "sent" }, { ...candidate, connection_id: "another" }, { ...candidate, intended_state: { ...candidate.intended_state as object, service: "other" } }, { ...candidate, intended_state: redact({ kind: "service", service: "network.wireguard.import", input: { ...input, receiptId: "tampered" } }) }, { ...candidate, result: null }, { ...candidate, result: { ...candidate.result, afterProof: "different" } }, { ...candidate, result: { ...candidate.result, completedCommands: 6 } }, { ...candidate, result: { ...candidate.result, peerIds: ["*3", "*foreign"] } }]) assert.equal(hasVerifiedWireguardImportCheckpoint(changed, input, checkpoint, 2), false) +assert.equal(hasVerifiedWireguardImportCheckpoint(undefined, input, checkpoint, 2), false, "Lost acknowledgement after final audit-prefix checkpoint is not operation success") +console.log("WireGuard import reconciliation: final native audit-prefix alone is insufficient; exact VERIFIED service step/result/connection required") + +// Exercise the real REST dry-run adapter with a query trap. No SQL server or +// RouterOS double is necessary: malformed IDs and preview stop before either. +const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) +await rest.register(wireguardRoutes, { prefix: "/api" }) +const originalQuery = pool.query +let queries = 0 +pool.query = (() => { queries++; throw Error("Dry-run/invalid input must not query SQL") }) as typeof pool.query +try { + const key = (byte: number) => Buffer.alloc(32, byte).toString("base64") + const body = { serverId: 141, dryRun: true, format: "rsc", content: `/interface wireguard add name=wg-preview private-key="${key(31)}" comment="first\\nsecond\\tline"\n/ipv6 address add interface=wg-preview address=2001:db8::1/64 advertise=no\n/interface wireguard peers add interface=wg-preview public-key="${key(32)}" allowed-address=192.0.2.0/24 client-address=10.0.0.2 client-dns=1.1.1.1 client-endpoint=vpn.example.org comment="peer\\r\\nline"\n/interface wireguard peers add interface=wg-preview public-key="${key(33)}" allowed-address=2001:db8:1::/64 disabled=yes` } + const response = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: body }) + assert.equal(response.statusCode, 200, response.body) + const receipt = response.json() as { dryRun: boolean; preview: { interface: Record; peers: Record[] } } + assert.equal(receipt.dryRun, true); assert.equal(receipt.preview.peers.length, 2) + assert.equal(receipt.preview.interface.comment, "first\nsecond\tline"); assert.equal(receipt.preview.interface.address, "2001:db8::1/64") + assert.equal(receipt.preview.peers[0].comment, "peer\r\nline"); assert.equal(receipt.preview.peers[0].clientDns, "1.1.1.1"); assert.equal(receipt.preview.peers[1].disabled, true) + assert.equal(receipt.preview.peers.every(peer => peer.publicKey === "[KEY]" && peer.privateKey === undefined), true) + for (const secret of [key(31), key(32), key(33)]) assert.equal(response.body.includes(secret), false) + for (const serverId of ["", "141x", "141.5", "0", "-1", "9007199254740992", 0, -1, 1.5, null, false]) for (const dryRun of [false, true]) { + const invalid = await rest.inject({ method: "POST", url: "/api/wireguard/import", payload: { ...body, serverId, dryRun } }) + assert.equal(invalid.statusCode, 400) + for (const secret of [key(31), key(32), key(33)]) assert.equal(invalid.body.includes(secret), false) + } + assert.equal(queries, 0) + console.log("WireGuard import real REST adapter: complete redacted multiline/IPv6/multi-peer preview and strict IDs, zero SQL and zero native HTTP") +} finally { pool.query = originalQuery; await rest.close() } diff --git a/backend/src/modules/mcp/wireguard-import-actions.ts b/backend/src/modules/mcp/wireguard-import-actions.ts new file mode 100644 index 00000000..71b587ff --- /dev/null +++ b/backend/src/modules/mcp/wireguard-import-actions.ts @@ -0,0 +1,137 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { parseWireguardImportPlan, wireguardImportPreview, wireguardImportStages, wireguardImportObjects, assertWireguardImportAllowed, WIREGUARD_IMPORT_MAX_BYTES } from "../../services/wireguard-import-plan.js" +import { executeWireguardImport, wireguardImportPrefix, wireguardImportProtectionProof, type WireguardImportCheckpoint } from "../../services/wireguard-import-execution.js" +import { readWireguardInterfaceCreateState } from "../../services/wireguard-interface-create.js" +import { effectiveMcpConnection, audit } from "./store.js" +import { routerClient } from "./resources.js" +import { accessGeneration, fingerprint, McpError, decryptSecret, privateSecretFingerprint, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" +import type { ActionContext } from "./action-types.js" + +export const WIREGUARD_IMPORT_CONTENT_PURPOSE = "wireguard-import-config" +export const wireguardImportReferenceSchema = z.object({ serverId: z.number().int().positive().safe(), contentRef: z.string().uuid(), format: z.enum(["auto", "rsc", "conf"]).default("auto") }).strict() +const operation = "network.wireguard.import" +const nativeId = z.string().regex(/^\*[a-z0-9]+$/i).max(100) +const checkpointSchema = z.object({ completedCommands: z.number().int().min(1).max(204), commandCount: z.number().int().min(1).max(204), afterProof: z.string().regex(/^[a-f0-9]{64}$/), interfaceId: nativeId, peerIds: z.array(nativeId).max(100) }) +/** Audit prefix proofs establish native configuration only. Success also needs + * the exact generic VERIFIED step, whose acknowledgement may have been lost. */ +export function hasVerifiedWireguardImportCheckpoint(candidate: unknown, input: Record, checkpoint: WireguardImportCheckpoint, peerCount: number): boolean { + if (!candidate || typeof candidate !== "object") return false + const row = candidate as Record, result = row.result as Record | null + if (row.connection_id !== (input.principal as { connectionId?: string } | undefined)?.connectionId || row.state !== "verified" || fingerprint(row.intended_state) !== fingerprint(redact({ kind: "service", service: operation, input }))) return false + return !!result && result.verified === true && result.afterProof === checkpoint.afterProof && result.completedCommands === checkpoint.commandCount && checkpoint.completedCommands === checkpoint.commandCount && result.commandCount === checkpoint.commandCount && result.interfaceId === checkpoint.interfaceId && result.peersCreated === peerCount && Array.isArray(result.peerIds) && result.peerIds.every(id => typeof id === "string") && fingerprint([...result.peerIds].sort()) === fingerprint([...checkpoint.peerIds].sort()) +} +const fail = (code: string, message: string) => new McpError(code, message, code === "INVALID_INPUT" ? 400 : 409) +async function access(input: Record, signal?: AbortSignal) { + signal?.throwIfAborted() + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const connection = await effectiveMcpConnection(principal) + if (connection.owner_id !== input.ownerId || !hasPermission(connection.owner_permissions, "mm:network:write") || !hasPermission(connection.policy.permissions, "mm:network:write") || !connection.policy.scope.operationIds.includes(String(input.operationId))) throw fail("PERMISSION_DENIED", "Допуск импорта WireGuard отозван") + if (!connection.policy.scope.serverIds.includes(Number(input.serverId))) throw fail("SERVER_DENIED", "Сервер импорта вне области допуска") + if (fingerprint(connection.policy) !== input.policyProof || fingerprint(connection.owner_permissions) !== input.ownerProof || await accessGeneration() !== input.generation) throw fail("RESET_REQUIRED", "Допуск импорта изменился; подготовьте новый запрос") + signal?.throwIfAborted() +} +async function material(input: Record, signal?: AbortSignal) { + await access(input, signal) + const request = wireguardImportReferenceSchema.parse(input.request) + if (request.serverId !== input.serverId || fingerprint(request) !== input.requestProof) throw fail("STATE_CONFLICT", "Ссылка импорта не соответствует сохранённому запросу") + const row = (await pool.query("SELECT purpose,server_id,created_by,created_at,xmin::text AS revision,CASE WHEN octet_length(ciphertext)<=524288 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2", [request.contentRef, input.ownerId])).rows[0] + if (!row || row.server_id !== request.serverId || row.purpose !== WIREGUARD_IMPORT_CONTENT_PURPOSE || typeof row.ciphertext !== "string") throw fail("SECRET_UNAVAILABLE", "Ссылка не выдана владельцу для импорта WireGuard на этот сервер") + let content: string + try { content = await decryptSecret(row.ciphertext) } catch { throw fail("SECRET_UNAVAILABLE", "Конфигурация WireGuard недоступна") } + if (Buffer.byteLength(content) > WIREGUARD_IMPORT_MAX_BYTES) throw fail("INVALID_INPUT", "Конфигурация импорта превышает 256 КиБ") + const plan = parseWireguardImportPlan(content, request.format, request.serverId) + const referenceProof = await privateSecretFingerprint(fingerprint({ id: request.contentRef, ...row }), "wireguard-import-content-reference-v1") + await access(input, signal) + return { plan, referenceProof } +} +export function registerWireguardImportActions(register: typeof add) { + const source = async (input: Record, signal?: AbortSignal) => { + const value = await material(input, signal), objects = wireguardImportObjects(value.plan, String(input.receiptId)), protectionProof = await wireguardImportProtectionProof(Number(input.serverId), objects) + const authorize = async () => { + if ((await material(input, signal)).referenceProof !== value.referenceProof) throw fail("STATE_CONFLICT", "Содержимое ссылки WireGuard изменилось") + if (await wireguardImportProtectionProof(Number(input.serverId), objects) !== protectionProof) throw fail("STATE_CONFLICT", "Защиты WireGuard изменились") + } + await authorize() + const { server } = await routerClient(Number(input.serverId)) + const state = await readWireguardInterfaceCreateState(server, authorize) + await authorize() + return { ...value, server, state, authorize, publicState: { proof: state.proof, referenceProof: value.referenceProof, protectionProof, preview: wireguardImportPreview(value.plan) } } + } + const internal = async (input: Record, context: ActionContext, operationId: string) => { + const request = wireguardImportReferenceSchema.parse(JSON.parse(JSON.stringify(input))) + return { request, requestProof: fingerprint(request), serverId: request.serverId, principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions), generation: await accessGeneration(), operationId, receiptId: randomUUID() } + } + register("network.wireguard.import.preview", "mm_wireguard_import_preview", "Проверить .rsc/.conf из защищённой ссылки без раскрытия ключей", "network", "mm:network:write", "read", "servers", wireguardImportReferenceSchema, { + read: async (input, context) => { + const request = await internal(input, context, "network.wireguard.import.preview"), before = await material(request, context.signal) + const preview = wireguardImportPreview(before.plan) + if ((await material(request, context.signal)).referenceProof !== before.referenceProof) throw fail("STATE_CONFLICT", "Ссылка изменилась во время preview") + return { serverId: request.serverId, preview, referenceProof: before.referenceProof, nativeChecked: false, configurationApplied: false } + }, + }) + registerMcpService(operation, { + current: async input => { if (input.operationId !== operation) throw fail("STATE_CONFLICT", "Неверная операция импорта"); return (await source(input)).publicState }, + execute: async input => { + if (input.operationId !== operation) throw fail("STATE_CONFLICT", "Неверная операция импорта") + const before = await source(input) + if (fingerprint(before.publicState) !== input.beforeFingerprint) throw fail("STATE_CONFLICT", "WireGuard, ссылка или зависимости изменились после подтверждения") + const principal = input.principal as { connectionId: string } + return executeWireguardImport(before.server, before.plan, before.state.proof, async () => { await assertVerifiedConfigurationAccess(); await before.authorize() }, { + receiptId: String(input.receiptId), + checkpoint: async checkpoint => { + await assertVerifiedConfigurationAccess(); await before.authorize() + await audit(pool, String(input.ownerId), "wireguard.import.checkpoint", principal.connectionId, { receiptId: input.receiptId, inputProof: fingerprint(redact(input)), ...checkpoint }) + }, + }) + }, + verify: async (input, result) => { + const receipt = result as { verified?: boolean; afterProof?: string; completedCommands?: number } | null + const current = await source(input) + return receipt?.verified === true && receipt.completedCommands === wireguardImportStages(current.plan).length && receipt.afterProof === current.state.proof + }, + reconcile: async (input, allow) => { + if (input.operationId !== operation || input.proofRevision !== 1 || !z.string().uuid().safeParse(input.receiptId).success || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof)) throw fail("UNSUPPORTED_CAPABILITY", "Нет совместимого checkpoint импорта WireGuard") + await allow() + const latest = await source(input) + if (latest.referenceProof !== input.expectedReferenceProof) throw fail("STATE_CONFLICT", "Ссылка импорта изменилась после подготовки") + const rows = (await pool.query("SELECT payload FROM mcp_audit WHERE action='wireguard.import.checkpoint' AND actor_id=$1 AND connection_id=$2 AND payload->>'receiptId'=$3 AND payload->>'inputProof'=$4 ORDER BY id DESC LIMIT 1", [input.ownerId, (input.principal as { connectionId: string }).connectionId, input.receiptId, fingerprint(redact(input))])).rows + const parsedCheckpoint = checkpointSchema.safeParse(rows[0]?.payload) + const checkpoint = parsedCheckpoint.success ? parsedCheckpoint.data : undefined + const stages = wireguardImportStages(latest.plan) + let prefix: number | null = latest.state.proof === input.expectedProof ? 0 : null + if (checkpoint && Number.isInteger(checkpoint.completedCommands) && checkpoint.completedCommands > 0 && checkpoint.completedCommands <= stages.length && checkpoint.commandCount === stages.length && checkpoint.afterProof === latest.state.proof && await wireguardImportPrefix(latest.state, latest.plan, input.expectedProof, checkpoint.completedCommands)) { + const iface = latest.state.base.interfaces.find(row => row.name === latest.plan.interface.name) + const peerIds = latest.state.base.peers.filter(row => iface && [iface.name, iface[".id"]].includes(row.interface)).map(row => row[".id"]).sort() + if (iface?.[".id"] === checkpoint.interfaceId && fingerprint(peerIds) === fingerprint([...checkpoint.peerIds].sort())) prefix = checkpoint.completedCommands + } + const generic = (await pool.query("SELECT o.connection_id,s.state,s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.intended_state->'input'->>'receiptId'=$2 LIMIT 2", [(input.principal as { connectionId: string }).connectionId, input.receiptId])).rows + const verifiedFinal = prefix === stages.length && checkpoint !== undefined && generic.length === 1 && hasVerifiedWireguardImportCheckpoint(generic[0], input, checkpoint, latest.plan.peers.length) + await allow() + if (fingerprint((await source(input)).publicState) !== fingerprint(latest.publicState)) throw fail("STATE_CONFLICT", "WireGuard изменился при сверке") + return { matchesOriginalState: prefix === 0, configurationMatchesDesiredState: prefix === stages.length, matchesDesiredState: verifiedFinal, matchesExpectedPrefixState: prefix !== null, verifiedPrefixCommands: prefix, verifiedFinalCheckpointAvailable: verifiedFinal, forwardingVerified: false, automaticReplay: false } + }, + }) + register(operation, "mm_wireguard_import_plan", "Импортировать интерфейс, адрес и несколько пиров WireGuard с подтверждением", "network", "mm:network:write", "write", "servers", wireguardImportReferenceSchema, { + prepare: async (input, context) => { + const request = await internal(input, context, operation), before = await source(request, context.signal) + assertWireguardImportAllowed(before.state, before.plan) + // Imported names can themselves echo a key. The fixed application domain + // plus every existing server protection carry policy dependencies without + // placing a raw imported identifier into a public plan or audit record. + const objects = wireguardImportObjects(before.plan, request.receiptId) + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) OR EXISTS(SELECT 1 FROM jsonb_array_elements($2::jsonb) target WHERE target->>'kind'=object_ref->>'kind' AND target->>'id'=object_ref->>'id') LIMIT 1001", [String(request.serverId), JSON.stringify(objects)]) + if (protections.rows.length > 1000) throw fail("STATE_UNAVAILABLE", "Превышен предел защит WireGuard") + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: request.serverId }) + await before.authorize() + return { target: { scope: "servers", serverIds: [request.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: wireguardImportStages(before.plan).length + 1, before: before.publicState, + steps: [{ kind: "service", service: operation, input: { ...request, proofRevision: 1, expectedProof: before.state.proof, expectedReferenceProof: before.referenceProof, beforeFingerprint: fingerprint(before.publicState) } }], + warnings: ["Импорт создаёт один интерфейс, необязательный IPv4/IPv6 адрес и все пиры из .rsc/.conf. Сначала все объекты отключены, затем отдельно включаются запрошенные; операция не атомарна.", "Конфигурация и ключи доступны только через owner/purpose/server contentRef wireguard-import-config. Preview и квитанции не содержат ключей. Пределы: 256 КиБ и 100 пиров; полный расход команд учитывается политикой.", "Каждая команда требует свежий полный proof, durable intent и read-back. Проверенный префикс сохраняется отдельно; потеря ответа запрещает повтор. Автоматического отката нет, явный отказ оставляет созданный префикс для отдельного решения об очистке.", "Ошибка сохранения copy-ревизии не отменяет применённый native результат и не разрешает повтор. Handshake, трафик, маршрутизация и firewall не подтверждаются импортом."] } + }, + }) +} diff --git a/backend/src/modules/mcp/wireguard-interface-create-acceptance.ts b/backend/src/modules/mcp/wireguard-interface-create-acceptance.ts new file mode 100644 index 00000000..1c8b45fa --- /dev/null +++ b/backend/src/modules/mcp/wireguard-interface-create-acceptance.ts @@ -0,0 +1,163 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import wireguardRoutes from "../../routes/wireguard.js" +import { wireguardInterfaceDependencyPaths } from "../../services/wireguard-interface-delete.js" +import { wireguardPublicKey } from "../../services/wireguard-interface-create.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { encryptSecret } from "./security.js" +import { WIREGUARD_INTERFACE_KEY_PURPOSE, WIREGUARD_PEER_KEY_PURPOSE, wireguardInterfaceCreatePlanSchema } from "./wireguard-interface-create-actions.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +type Rows = Record[]> +export async function acceptWireguardInterfaceCreation(actor: AuthUser) { + const privateKey = Buffer.alloc(32, 51).toString("base64"), peerPrivateKey = Buffer.alloc(32, 52).toString("base64"), generatedKey = Buffer.alloc(32, 53).toString("base64") + const initial: Rows = Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])) + initial["/interface/wireguard"] = [{ ".id": "*af", name: "wg-foreign", disabled: "true", "public-key": wireguardPublicKey(generatedKey), "private-key": "NEVER_FOREIGN_PRIVATE" }] + initial["/interface/wireguard/peers"] = [{ ".id": "*pf", interface: "wg-foreign", "public-key": "foreign", opaque: "NEVER_FOREIGN_OPAQUE", rx: "0" }] + 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"] = [] + let rows = structuredClone(initial), mode = "normal", reads = 0, commands: string[] = [], counter = 0 + let afterWrite: (() => Promise) | 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)) + 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 + } + 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 } + let raw = "" + req.on("data", chunk => { raw += chunk }); req.on("end", async () => { + const body = JSON.parse(raw) as Record + commands.push(req.method + " " + path) + if (mode !== "ignored") { + if (req.method === "PUT") { + const id = "*c" + ++counter, row: Record = { ".id": id, ...body } + if (base === "/interface/wireguard") { row["private-key"] ??= generatedKey; row["public-key"] = wireguardPublicKey(row["private-key"]); rows["/interface"].push({ ".id": id, name: row.name, type: "wg" }) } + if (base === "/ip/address" || base === "/ipv6/address") row.dynamic = "false" + if (row["private-key"] === "auto") row["private-key"] = generatedKey + rows[base].push(row) + } else Object.assign(rows[base].find(row => row[".id"] === path.slice(base.length + 1))!, body) + } + if (mode === "collateral") rows["/ip/address"][0].comment = "NEVER_COLLATERAL" + if (mode === "staged-collateral" && commands.length === 2) rows["/interface/wireguard"].find(row => row.name === "wg-created")!.opaque = "NEVER_CHANGED_STAGED" + if (afterWrite) { const hook = afterWrite; afterWrite = undefined; await hook() } + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}") + }) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const secretIds: string[] = [] + const secret = async (purpose: string, value: string, serverId: number | null = 111, owner = actor.id) => { + const id = randomUUID(); secretIds.push(id) + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, purpose, serverId, await encryptSecret(value), owner]); return id + } + const wait = async (id: string) => { + for (let index = 0; index < 3000; index++) { const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; if (!["pending", "running"].includes(state)) return state; await delay(25) } + throw Error("WireGuard creation remains live; never replay") + } + let connectionId: string | undefined + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(111,'wg-interface-create','127.0.0.1',$1,false,'NEVER_CREATE_ENDPOINT_PASSWORD')", [(router.address() as { port: number }).port]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('wg-create-user','wg-create-user','Create fixture')") + await rest.register(wireguardRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "WireGuard creation", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [111], operationIds: ["network.wireguard.interfaces.create", "operations.reconcile", "operations.recover"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const privateKeyRef = await secret(WIREGUARD_INTERFACE_KEY_PURPOSE, privateKey), peerKeyRef = await secret(WIREGUARD_PEER_KEY_PURPOSE, peerPrivateKey) + const full = { serverId: 111, name: "wg-created", listenPort: 14321, mtu: 1380, comment: "owned interface", privateKeyRef, address: "2001:db8:251::1/64", peer: { publicKey: wireguardPublicKey(peerPrivateKey), allowedAddresses: ["192.0.2.2/32", "2001:db8:251::2/128"], endpointAddress: "vpn.example.org", endpointPort: 24321, persistentKeepalive: 25, comment: "owned peer", name: "owned-peer", privateKeyRef: peerKeyRef, clientAddress: "192.0.2.2", clientDns: "1.1.1.1", clientEndpoint: "client.example.org" } } + const prepare = async (input: unknown = full) => { + const plan = await invokeMcpTool(principal, "mm_wireguard_interface_create_plan", input) as { planId: string; state: string; changes: { mutationCost: number } } + assert.equal(plan.state, "awaiting-approval") + for (const value of [privateKey, peerPrivateKey, "NEVER_"]) assert.ok(!JSON.stringify(plan).includes(value)) + return plan + } + const beforeInvalid = reads + for (const input of [{ ...full, serverId: 1 }, { ...full, privateKey }, { ...full, name: "\u0000bad" }, { ...full, peer: { ...full.peer, privateKey } }]) await assert.rejects(prepare(input)) + assert.equal(wireguardInterfaceCreatePlanSchema.safeParse({ ...full, privateKey }).success, false) + assert.equal(reads, beforeInvalid) + for (const ref of [await secret("wrong-purpose", privateKey), await secret(WIREGUARD_INTERFACE_KEY_PURPOSE, privateKey, null), await secret(WIREGUARD_INTERFACE_KEY_PURPOSE, privateKey, 111, "wg-create-user")]) { await assert.rejects(prepare({ ...full, privateKeyRef: ref })); assert.equal(commands.length, 0) } + for (const [path, row] of [["/interface/vlan", { interface: full.name }], ["/routing/filter/rule", { rule: 'set gw "wg-created";' }], ["/routing/ospf/interface-template", { networks: "2001:db8:251::/64" }], ["/ip/vrf", { name: "other", interfaces: "all" }]] as [string, Record][]) { reset(); rows[path].push({ ".id": "*ref", ...row }); await assert.rejects(prepare()); assert.equal(commands.length, 0) } + reset() + const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [protection, { kind: "wireguard", id: "*af", serverId: 111, protection: "deny" }, actor.id]) + try { await assert.rejects(prepare()); const response = await rest.inject({ method: "POST", url: "/api/wireguard/interfaces", payload: { serverId: 111, name: "wg-created" } }); assert.equal(response.statusCode, 409); assert.equal(commands.length, 0) } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + for (const fault of ["normal", "ignored", "collateral", "lost", "staged-collateral"]) { + reset(); const plan = await prepare(); assert.equal(plan.changes.mutationCost, 7); await approvePlan(plan.planId, actor, true); mode = fault + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + const expected = fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial" + assert.equal(await wait(operation.id), expected, JSON.stringify({ fault, ...(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] })) + assert.equal(commands.length, fault === "normal" ? 6 : fault === "staged-collateral" ? 2 : 1) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + const count: number = commands.length + const reconciled = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesOriginalState: boolean; matchesExpectedPrefixState: boolean; verifiedPrefixCommands: number | null }[] } + assert.equal(reconciled.automaticRetry, false); assert.equal(reconciled.results[0].matchesDesiredState, fault === "normal"); assert.equal(reconciled.results[0].matchesOriginalState, fault === "ignored"); assert.equal(commands.length, count) + if (fault === "normal") { + const saved = (await pool.query("SELECT state,intended_state FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0] + for (const changed of [ + { state: "applied", intended: saved.intended_state }, + { state: "verified", intended: { ...saved.intended_state, service: "network.wireguard.peers.create" } }, + { state: "verified", intended: { ...saved.intended_state, input: { ...saved.intended_state.input, receiptId: randomUUID() } } }, + { state: "verified", intended: { ...saved.intended_state, input: { ...saved.intended_state.input, principal: { ...saved.intended_state.input.principal, credentialId: randomUUID() } } } }, + { state: "verified", intended: { ...saved.intended_state, input: { ...saved.intended_state.input, ownerId: "foreign-owner" } } }, + { state: "verified", intended: { ...saved.intended_state, input: { ...saved.intended_state.input, requestProof: "0".repeat(64) } } }, + ]) { + await pool.query("UPDATE mcp_operation_steps SET state=$2,intended_state=$3::jsonb WHERE operation_id=$1 AND step_index=0", [operation.id, changed.state, JSON.stringify(changed.intended)]) + const untrusted = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { results: { matchesDesiredState: boolean }[] } + assert.equal(untrusted.results[0].matchesDesiredState, false, "An unverified or foreign checkpoint cannot prove creation") + assert.equal(commands.length, count, "Checkpoint rejection must remain read-only") + } + await pool.query("UPDATE mcp_operation_steps SET state=$2,intended_state=$3::jsonb WHERE operation_id=$1 AND step_index=0", [operation.id, saved.state, JSON.stringify(saved.intended_state)]) + const trusted = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { results: { matchesDesiredState: boolean }[] } + assert.equal(trusted.results[0].matchesDesiredState, true) + assert.equal(commands.length, count) + } + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: operation.id }), (error: { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY") + if (fault === "normal") { assert.equal(rows["/interface/wireguard"].find(row => row.name === full.name)?.disabled, "false"); assert.equal(rows["/ipv6/address"][0].advertise, "false"); assert.equal(rows["/interface/wireguard/peers"].length, 2) } + } + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); rows["/interface/wireguard/peers"][0].opaque = "NEVER_LATE_DRIFT" + assert.equal(await wait((await applyPlan(principal, stale.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0) + reset(); const rotated = await prepare(); await approvePlan(rotated.planId, actor, true); await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [privateKeyRef, await encryptSecret(generatedKey)]) + assert.equal(await wait((await applyPlan(principal, rotated.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0) + await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [privateKeyRef, await encryptSecret(privateKey)]) + reset(); const paused = await prepare(); await approvePlan(paused.planId, actor, true); afterWrite = () => setConnectionStatus(connectionId!, "paused", actor) + assert.equal(await wait((await applyPlan(principal, paused.planId, randomUUID())).id), "unknown"); assert.equal(commands.length, 1) + await setConnectionStatus(connectionId, "active", actor) + for (const address of ["192.0.2.1/24", "2001:db8:252::1/64"]) { + reset(); const plan = await prepare({ serverId: 111, name: "wg-created", address, disabled: true, peer: { publicKey: full.peer.publicKey, allowedAddresses: full.peer.allowedAddresses, disabled: true, privateKeyMode: "auto" } }); await approvePlan(plan.planId, actor, true) + assert.equal(await wait((await applyPlan(principal, plan.planId, randomUUID())).id), "applied"); assert.equal(commands.length, 4) + } + reset(); mode = "provider-error"; await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + assert.ok(!(await rest.inject({ method: "POST", url: "/api/wireguard/interfaces", payload: { serverId: 111, name: "wg-created" } })).body.includes("NEVER_")) + reset() + const response = await rest.inject({ method: "POST", url: "/api/wireguard/interfaces", payload: { ...full, privateKeyRef: undefined, privateKey, peer: { ...full.peer, privateKeyRef: undefined, privateKey: peerPrivateKey } } }) + assert.equal(response.statusCode, 201, response.body); assert.equal(response.json().name, full.name); assert.equal(response.json().privateKey, privateKey); assert.equal(commands.length, 6) + reset() + const namedResponse = await rest.inject({ method: "POST", url: "/api/wireguard/interfaces", payload: { serverId: 111, name: 'WG тест "edge" [x]' } }) + assert.equal(namedResponse.statusCode, 201, namedResponse.body); assert.equal(namedResponse.json().name, 'WG тест "edge" [x]'); assert.equal(commands.length, 2) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND (result::text LIKE '%NEVER_%' OR result::text LIKE $2 OR result::text LIKE $3)", [connectionId, "%" + privateKey + "%", "%" + peerPrivateKey + "%"])).rows[0].n, 0) + const intents = await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent' AND payload->>'serverId'='111'") + assert.ok(intents.rows[0].n >= commands.length) + console.log("✓ WireGuard create shared REST/MCP: full optional address/peer/key fields, secret purpose/owner/server/rotation, disabled preparation and activation, whole-source/protection/selector checks, prefix/read-back/opaque preservation, lost/partial/revoke/no replay, IPv4/IPv6 and private REST DTO") + } finally { + await rest.close(); await new Promise(resolve => router.close(() => resolve())) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])", [secretIds]) + await pool.query("DELETE FROM app_users WHERE id='wg-create-user'") + await pool.query("DELETE FROM servers WHERE id=111") + } +} diff --git a/backend/src/modules/mcp/wireguard-interface-create-actions.ts b/backend/src/modules/mcp/wireguard-interface-create-actions.ts new file mode 100644 index 00000000..02fdc92c --- /dev/null +++ b/backend/src/modules/mcp/wireguard-interface-create-actions.ts @@ -0,0 +1,105 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { wireguardInterfaceCreateSchema, wireguardCreateKey, assertWireguardCreateAllowed, readWireguardInterfaceCreateState, createWireguardInterface, wireguardCreateStages, wireguardCreateProtectionProof } from "../../services/wireguard-interface-create.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError, decryptSecret, privateSecretFingerprint, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +export const WIREGUARD_INTERFACE_KEY_PURPOSE = "wireguard-interface-private-key" +export const WIREGUARD_PEER_KEY_PURPOSE = "wireguard-peer-private-key" +const peer = wireguardInterfaceCreateSchema.shape.peer.unwrap().omit({ privateKey: true }).extend({ privateKeyRef: z.string().uuid().optional(), privateKeyMode: z.enum(["auto", "none"]).optional() }).strict().refine(value => !(value.privateKeyRef && value.privateKeyMode), "Выберите ссылку или режим ключа") +export const wireguardInterfaceCreatePlanSchema = wireguardInterfaceCreateSchema.omit({ privateKey: true, peer: true }).extend({ privateKeyRef: z.string().uuid().optional(), peer: peer.optional() }).strict() +const fail = (code: string, message: string) => new McpError(code, message, 409) +async function access(input: Record, signal?: AbortSignal) { + if (signal?.aborted) throw fail("READ_CANCELLED", "Подготовка WireGuard отменена") + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const current = await effectiveMcpConnection(principal) + if (current.owner_id !== input.ownerId || !hasPermission(current.policy.permissions, "mm:network:write") || !hasPermission(current.owner_permissions, "mm:network:write")) throw fail("PERMISSION_DENIED", "Допуск WireGuard отозван") + if (!current.policy.scope.serverIds.includes(Number(input.serverId))) throw fail("SERVER_DENIED", "Сервер вне области допуска") + if (fingerprint(current.policy) !== input.policyProof || fingerprint(current.owner_permissions) !== input.ownerProof) throw fail("RESET_REQUIRED", "Допуск изменился; подготовьте новый план") +} +async function material(input: Record, signal?: AbortSignal) { + await access(input, signal) + const request = wireguardInterfaceCreatePlanSchema.parse(input.request), references: string[] = [] + if (input.requestProof !== fingerprint(request)) throw fail("STATE_CONFLICT", "Параметры WireGuard не соответствуют сохранённому proof") + const secret = async (id: string | undefined, purpose: string) => { + if (!id) return undefined + await access(input, signal) + const row = (await pool.query("SELECT purpose,server_id,created_by,CASE WHEN octet_length(ciphertext)<=16384 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2", [id, input.ownerId])).rows[0] + if (!row || row.server_id !== request.serverId || row.purpose !== purpose || typeof row.ciphertext !== "string") throw fail("SECRET_UNAVAILABLE", "Ссылка не выдана владельцу для этого ключа и сервера WireGuard") + let value: unknown + try { value = await decryptSecret(row.ciphertext) } catch { throw fail("SECRET_UNAVAILABLE", "Ключ WireGuard недоступен") } + const parsed = wireguardCreateKey.safeParse(value) + if (!parsed.success) throw fail("SECRET_UNAVAILABLE", "Неподдерживаемый формат ключа WireGuard") + references.push(await privateSecretFingerprint(fingerprint({ id, ...row }), "wireguard-create-reference-v1")) + return parsed.data + } + const privateKey = await secret(request.privateKeyRef, WIREGUARD_INTERFACE_KEY_PURPOSE), peerKey = await secret(request.peer?.privateKeyRef, WIREGUARD_PEER_KEY_PURPOSE) + const { privateKeyRef: _privateKeyRef, peer: requestedPeer, ...fields } = request + let nativePeer + if (requestedPeer) { const { privateKeyRef: _peerRef, privateKeyMode, ...publicFields } = requestedPeer; nativePeer = { ...publicFields, privateKey: peerKey ?? privateKeyMode } } + await access(input, signal) + return { body: wireguardInterfaceCreateSchema.parse({ ...fields, privateKey, peer: nativePeer }), referenceProof: fingerprint(references) } +} +export function registerWireguardInterfaceCreateActions(register: typeof add) { + const source = async (input: Record, signal?: AbortSignal) => { + const keys = await material(input, signal) + const protectionProof = await wireguardCreateProtectionProof(Number(input.serverId)) + const authorize = async () => { + const current = await material(input, signal) + if (current.referenceProof !== keys.referenceProof) throw fail("STATE_CONFLICT", "Ссылка на ключ изменилась") + if (await wireguardCreateProtectionProof(Number(input.serverId)) !== protectionProof) throw fail("STATE_CONFLICT", "Защиты WireGuard изменились") + } + const { server } = await routerClient(Number(input.serverId)), state = await readWireguardInterfaceCreateState(server, authorize) + await authorize() + return { ...keys, state, server, authorize, publicState: { proof: state.proof, referenceProof: keys.referenceProof, protectionProof } } + } + registerMcpService("network.wireguard.interfaces.create", { + current: async input => (await source(input)).publicState, + execute: async input => { + const before = await source(input) + if (fingerprint(before.publicState) !== input.beforeFingerprint) throw fail("STATE_CONFLICT", "Источник, ключи или зависимости WireGuard изменились") + return createWireguardInterface(before.server, before.body, before.state.proof, async () => { await assertVerifiedConfigurationAccess(); await before.authorize() }) + }, + verify: async (input, result) => { const receipt = result as { verified?: boolean; afterProof?: string } | null; return receipt?.verified === true && (await source(input)).state.proof === receipt.afterProof }, + reconcile: async (input, authorize) => { + if (input.proofRevision !== 1 || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof) || input.requestProof !== fingerprint(input.request)) throw fail("UNSUPPORTED_CAPABILITY", "Нет совместимого proof создания WireGuard") + await authorize() + const latest = await source(input) + if (latest.referenceProof !== input.expectedReferenceProof) throw fail("STATE_CONFLICT", "Ссылка на ключ изменилась после подготовки") + const stages = wireguardCreateStages(latest.body) + if (!z.string().uuid().safeParse(input.receiptId).success) throw fail("UNSUPPORTED_CAPABILITY", "Нет идентификатора проверенного checkpoint WireGuard") + const principal = input.principal as { connectionId: string } + const receipts = (await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.state='verified' AND s.intended_state=$2::jsonb AND s.result->>'verified'='true' LIMIT 2", [principal.connectionId, JSON.stringify(redact({ kind: "service", service: "network.wireguard.interfaces.create", input }))])).rows + await authorize() + const fresh = await source(input) + if (fingerprint(fresh.publicState) !== fingerprint(latest.publicState)) throw fail("STATE_CONFLICT", "WireGuard изменился во время сверки") + const afterProof = receipts.length === 1 && /^[a-f0-9]{64}$/.test(receipts[0].result?.afterProof ?? "") ? receipts[0].result.afterProof as string : undefined + const original = latest.state.proof === input.expectedProof, desired = afterProof !== undefined && latest.state.proof === afterProof + await authorize() + return { matchesOriginalState: original, matchesDesiredState: desired, matchesExpectedPrefixState: original || desired, verifiedPrefixCommands: desired ? stages.length : original ? 0 : null, verifiedFinalCheckpointAvailable: afterProof !== undefined, forwardingVerified: false } + }, + }) + register("network.wireguard.interfaces.create", "mm_wireguard_interface_create_plan", "Создать WireGuard, необязательный адрес и peer с отключённой подготовкой", "network", "mm:network:write", "write", "servers", wireguardInterfaceCreatePlanSchema, { + version: 3, + prepare: async (input, context) => { + // Seal the same JSON representation that survives durable plan storage. + const request = wireguardInterfaceCreatePlanSchema.parse(JSON.parse(JSON.stringify(input))), internal = { request, requestProof: fingerprint(request), serverId: request.serverId, principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions) } + const before = await source(internal, context.signal) + assertWireguardCreateAllowed(before.state, before.body) + const objects = [{ kind: "server", id: String(request.serverId), serverId: request.serverId }, { kind: "wireguard", id: request.name, serverId: request.serverId }] + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(request.serverId)]) + if (protections.rows.length > 1000) throw fail("STATE_UNAVAILABLE", "Превышен предел защит WireGuard") + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: request.serverId }) + await before.authorize() + return { target: { scope: "servers", serverIds: [request.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: wireguardCreateStages(before.body).length + 1, before: before.publicState, + steps: [{ kind: "service", service: "network.wireguard.interfaces.create", input: { ...internal, receiptId: randomUUID(), expectedProof: before.state.proof, expectedReferenceProof: before.referenceProof, proofRevision: 1, beforeFingerprint: fingerprint(before.publicState) } }], + warnings: ["Интерфейс, необязательные адрес и peer создаются отключёнными. Затем отдельными командами включаются запрошенные объекты; операция не атомарна и может остановиться в промежуточном состоянии.", "Ключи принимаются только через owner/purpose/server secret reference; без ключа интерфейс генерирует RouterOS. Приватные ключи не возвращаются модели.", "Каждая команда требует свежий источник и read-back; потеря ответа останавливает выполнение без повтора. Сверка подтверждает исходный proof или сохранённый проверенный конечный checkpoint; без checkpoint достижение результата не объявляется. Автоматического отката нет.", "Передача трафика, handshake, firewall и маршрутизация требуют отдельной приёмки."] } + }, + }) +} diff --git a/backend/src/modules/mcp/wireguard-interface-delete-acceptance.ts b/backend/src/modules/mcp/wireguard-interface-delete-acceptance.ts new file mode 100644 index 00000000..39b3c8aa --- /dev/null +++ b/backend/src/modules/mcp/wireguard-interface-delete-acceptance.ts @@ -0,0 +1,177 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import wireguardRoutes from "../../routes/wireguard.js" +import { wireguardInterfaceDependencyPaths } from "../../services/wireguard-interface-delete.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +type Rows = Record[]> +export async function acceptWireguardInterfaceDeletion(actor: AuthUser) { + const privateKey = Buffer.alloc(32, 31).toString("base64"), psk = Buffer.alloc(32, 32).toString("base64") + const initial: Rows = Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])) + initial["/interface/wireguard"] = [{ ".id": "*a1", name: "wg-cascade", disabled: "false", "private-key": privateKey, "listen-port": "13231", mtu: "1420", comment: "owned" }, { ".id": "*af", name: "wg-foreign", disabled: "true", "private-key": "NEVER_FOREIGN_PRIVATE" }] + initial["/interface/wireguard/peers"] = [{ ".id": "*p1", interface: "wg-cascade", "public-key": Buffer.alloc(32, 33).toString("base64"), "preshared-key": psk, "allowed-address": "192.0.2.2/32", disabled: "true" }, { ".id": "*p2", interface: "*a1", "public-key": Buffer.alloc(32, 34).toString("base64"), "allowed-address": "192.0.2.3/32", disabled: "true" }, { ".id": "*pf", interface: "wg-foreign", "public-key": "foreign", rx: "0", opaque: "NEVER_FOREIGN_OPAQUE" }] + initial["/interface"] = [{ ".id": "*a1", name: "wg-cascade", type: "wg" }, { ".id": "*af", name: "wg-foreign", type: "wg" }] + initial["/ip/address"] = [{ ".id": "*v1", address: "198.19.244.1/30", interface: "wg-cascade", disabled: "true", dynamic: "false", invalid: "true" }, { ".id": "*vf", address: "203.0.113.2/24", interface: "ether1", dynamic: "false", disabled: "false" }] + initial["/ipv6/address"] = [{ ".id": "*v2", address: "2001:db8:244::1/64", interface: "*a1", disabled: "true", dynamic: "false", invalid: "true" }, { ".id": "*ll", address: "fe80::1/64", interface: "wg-cascade", dynamic: "true" }] + initial["/ip/route"] = [{ ".id": "*dc", dynamic: "true", connect: "true", gateway: "wg-cascade", "pref-src": "198.19.244.1" }] + let rows = structuredClone(initial), mode = "normal", gets = 0, commands: string[] = [], afterDelete: (() => Promise) | undefined + let duringRead: (() => Promise) | undefined + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json") + const path = decodeURIComponent((req.url ?? "").slice(5)) + if (req.method === "GET") { + gets++ + const respond = () => { + if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_CASCADE_PROVIDER_PASSWORD"}'); return } + 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])) + } + if (duringRead) { const hook = duringRead; duringRead = undefined; void hook().then(respond, () => { res.statusCode = 503; res.end("{}"); }); return } + respond(); return + } + const bases = ["/interface/wireguard/peers", "/interface/wireguard", "/ip/address", "/ipv6/address"] + const base = bases.find(base => path.startsWith(base + "/")) + if (!base || !["PATCH", "DELETE"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return } + const id = path.slice(base.length + 1) + let body = "" + req.on("data", chunk => { body += chunk }); req.on("end", async () => { + commands.push(req.method + " " + path) + if (req.method === "PATCH") { + Object.assign(rows[base].find(row => row[".id"] === id)!, JSON.parse(body)) + rows["/ipv6/address"] = rows["/ipv6/address"].filter(row => row[".id"] !== "*ll") + rows["/ip/route"] = rows["/ip/route"].filter(row => row[".id"] !== "*dc") + } else { + if (mode !== "ignored") rows[base] = rows[base].filter(row => row[".id"] !== id) + if (base === "/interface/wireguard" && mode !== "ignored") rows["/interface"] = rows["/interface"].filter(row => row[".id"] !== id) + if (mode === "collateral") rows["/ip/address"].find(row => row[".id"] === "*vf")!.comment = "foreign drift" + if (afterDelete) { const hook = afterDelete; afterDelete = undefined; await hook() } + if (mode === "lost") { req.socket.destroy(); return } + } + res.end("{}") + }) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const reset = () => { rows = structuredClone(initial); commands = []; mode = "normal"; afterDelete = undefined } + const wait = async (id: string) => { + for (let n = 0; n < 3000; n++) { const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; if (!["pending", "running"].includes(state)) return state; await delay(25) } + throw Error("WireGuard cascade still live; never replay") + } + let connectionId: string | undefined + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(109,'wg-interface-delete','127.0.0.1',$1,false,'NEVER_CASCADE_ENDPOINT_PASSWORD')", [(router.address() as { port: number }).port]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('wg-cascade-user','wg-cascade-user','Cascade fixture')") + await rest.register(wireguardRoutes, { prefix: "/api" }) + const connectionInput = { name: "WireGuard cascade deletion", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [109], operationIds: ["network.wireguard.interfaces.delete", "ros.wireguard.delete", "operations.reconcile", "operations.recover"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } } + const access = await createMcpConnection(mcpConnectionCreateSchema.parse(connectionInput), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = async (native = false) => { + const result = await invokeMcpTool(principal, native ? "mm_wireguard_delete_plan" : "mm_wireguard_interface_remove_plan", native ? { serverId: 109, id: "*a1" } : { serverId: 109, interfaceId: "*a1" }) as { planId: string; state: string; changes: { mutationCost: number } } + assert.equal(result.state, "awaiting-approval") + for (const secret of [privateKey, psk, "NEVER_"]) assert.ok(!JSON.stringify(result).includes(secret)) + return result + } + const hidden = gets + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_interface_remove_plan", { serverId: 1, interfaceId: "*a1" })) + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_interface_remove_plan", { serverId: 109, interfaceId: "../bad" })) + assert.equal(gets, hidden) + 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" }], + ["/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" }], + ["/routing/ospf/interface-template", { networks: "198.19.244.0/30" }], ["/routing/ospf/neighbor", { interface: "wg-cascade", state: "Full" }], + ] as [string, Record][]) { + reset(); rows[path].push({ ".id": "*ref", ...row }) + await assert.rejects(prepare()); assert.equal(commands.length, 0) + assert.equal((await rest.inject({ method: "DELETE", url: "/api/wireguard/interfaces/109/*a1" })).statusCode, 409) + assert.equal(commands.length, 0) + } + reset() + const bindingId = randomUUID() + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,'wg-cascade-user',109,'*a1','')", [bindingId]) + await assert.rejects(prepare()); assert.equal((await rest.inject({ method: "DELETE", url: "/api/wireguard/interfaces/109/*a1" })).statusCode, 409) + await pool.query("DELETE FROM user_interface_bindings WHERE id=$1", [bindingId]) + const protectionId = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protectionId, JSON.stringify({ kind: "wireguard_peer", id: "*p2", serverId: 109, protection: "deny" })]) + await assert.rejects(prepare()); assert.equal((await rest.inject({ method: "DELETE", url: "/api/wireguard/interfaces/109/*a1" })).statusCode, 409) + assert.equal(commands.length, 0); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) + rows["/interface/wireguard"][0].comment = "MikrotikManager traffic-flow overlay" + await assert.rejects(prepare(), /NetFlow/); reset() + rows["/ip/address"][0].dynamic = "true" + await assert.rejects(prepare()); reset() + const low = await createMcpConnection(mcpConnectionCreateSchema.parse({ ...connectionInput, name: "Insufficient cascade budget", limits: { callsPerMinute: 120, mutationsPerHour: 3 } }), actor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(low.token), "mm_wireguard_interface_remove_plan", { serverId: 109, interfaceId: "*a1" }), (error: { code?: string }) => error.code === "BUDGET_EXCEEDED") + assert.equal(commands.length, 0) + for (const fault of ["normal", "ignored", "collateral", "lost"]) { + reset() + const plan = await prepare(); assert.equal(plan.changes.mutationCost, 7); await approvePlan(plan.planId, actor, true) + mode = fault + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + const expected = fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial" + assert.equal(await wait(operation.id), expected) + assert.equal(commands.length, fault === "normal" ? 6 : 2) + assert.ok(commands[0].startsWith("PATCH /interface/wireguard/")) + const count: number = commands.length + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id); assert.equal(commands.length, count) + const reconciled = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { state: string; automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesExpectedPrefixState: boolean; verifiedPrefixCommands: number | null }[] } + assert.equal(reconciled.state, expected); assert.equal(reconciled.automaticRetry, false) + assert.equal(reconciled.results[0].matchesDesiredState, fault === "normal") + assert.equal(reconciled.results[0].matchesExpectedPrefixState, fault !== "collateral") + assert.equal(reconciled.results[0].verifiedPrefixCommands, fault === "normal" ? 6 : fault === "ignored" ? 1 : fault === "lost" ? 2 : null) + assert.equal(commands.length, count) + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: operation.id }), (error: { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY") + if (fault === "normal") { + assert.deepEqual(rows["/interface/wireguard"], [initial["/interface/wireguard"][1]]) + assert.deepEqual(rows["/ip/address"], [initial["/ip/address"][1]]); assert.deepEqual(rows["/ipv6/address"], []) + } + } + reset() + const paused = await prepare(); await approvePlan(paused.planId, actor, true) + afterDelete = () => setConnectionStatus(access.connection.id, "paused", actor) + const stopped = await applyPlan(principal, paused.planId, randomUUID()) + assert.equal(await wait(stopped.id), "unknown"); assert.equal(commands.length, 2) + await setConnectionStatus(access.connection.id, "active", actor) + reset() + const stale = await prepare(); await approvePlan(stale.planId, actor, true) + rows["/interface/wireguard/peers"][2].opaque = "NEVER_LATE_CHANGE" + assert.equal(await wait((await applyPlan(principal, stale.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0) + reset() + const beforePause = gets + duringRead = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(prepare()); assert.equal(gets, beforePause + 1) + await setConnectionStatus(access.connection.id, "active", actor) + reset(); mode = "provider-error" + await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + assert.ok(!(await rest.inject({ method: "DELETE", url: "/api/wireguard/interfaces/109/*a1" })).body.includes("NEVER_")) + reset() + const restResult = await rest.inject({ method: "DELETE", url: "/api/wireguard/interfaces/109/*a1" }) + assert.equal(restResult.statusCode, 200, restResult.body); assert.equal(restResult.json().verified, true); assert.equal(restResult.json().revisionRecorded, true); assert.equal(restResult.json().afterProof, undefined); assert.equal(commands.length, 6) + reset(); rows["/interface/wireguard/peers"] = rows["/interface/wireguard/peers"].filter(row => row.interface === "wg-foreign"); rows["/ip/address"] = rows["/ip/address"].filter(row => row.interface !== "wg-cascade"); rows["/ipv6/address"] = []; rows["/ip/route"] = [] + const legacy = await prepare(true); await approvePlan(legacy.planId, actor, true); await pool.query("UPDATE mcp_plans SET operation_version=1 WHERE id=$1", [legacy.planId]) + await assert.rejects(applyPlan(principal, legacy.planId, randomUUID()), (error: { code?: string }) => error.code === "PLAN_EXPIRED") + const empty = await prepare(true); await approvePlan(empty.planId, actor, true) + assert.equal(await wait((await applyPlan(principal, empty.planId, randomUUID())).id), "applied"); assert.equal(commands.length, 1); assert.ok(commands[0].startsWith("DELETE /interface/wireguard/")) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND (result::text LIKE '%NEVER_%' OR result::text LIKE $2 OR result::text LIKE $3)", [connectionId, "%" + privateKey + "%", "%" + psk + "%"])).rows[0].n, 0) + console.log("✓ WireGuard interface cascade shares REST/MCP: explicit children and native single-object alias, pre-disable/order/budget, protocol/IP/user/NetFlow/protection guards, private prefix proofs, per-command read-back, partial/lost/revoke/no replay, revision receipt and preserved foreign configuration") + } finally { + await rest.close(); await new Promise(resolve => router.close(() => resolve())) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM user_interface_bindings WHERE user_id='wg-cascade-user'") + await pool.query("DELETE FROM app_users WHERE id='wg-cascade-user'") + await pool.query("DELETE FROM servers WHERE id=109") + } +} diff --git a/backend/src/modules/mcp/wireguard-interface-delete-actions.ts b/backend/src/modules/mcp/wireguard-interface-delete-actions.ts new file mode 100644 index 00000000..946204fc --- /dev/null +++ b/backend/src/modules/mcp/wireguard-interface-delete-actions.ts @@ -0,0 +1,55 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { readWireguardInterfaceDeleteState, planWireguardInterfaceDelete, projectWireguardInterfaceDeleteCommand, wireguardInterfaceDeleteProof, deleteWireguardInterfaceGuarded } from "../../services/wireguard-interface-delete.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const schema = z.object({ serverId: z.number().int().positive().safe(), interfaceId: z.string().regex(/^\*[a-z0-9]+$/i).max(100) }).strict() +export async function prepareWireguardInterfaceDeletion(serverId: number, interfaceId: string, context: ActionContext, cascade = true): Promise { + schema.parse({ serverId, interfaceId }) + const authorize = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Подготовка удаления WireGuard отменена", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Право изменения сети отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) throw new McpError("RESET_REQUIRED", "Допуск изменился; подготовьте план заново", 409) + } + await authorize() + const { server } = await routerClient(serverId), state = await readWireguardInterfaceDeleteState(server, interfaceId, authorize), commands = planWireguardInterfaceDelete(state, interfaceId, cascade) + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(serverId), serverId }, ...commands.map(command => ({ kind: ({ interfaces: "wireguard", peers: "wireguard_peer", ipv4: "ipv4_address", ipv6: "ipv6_address" })[command.resource], id: command.id, serverId }))] + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(serverId)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел защит WireGuard", 409) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId }) + const prefixes = [state.proof] + let projected = state + for (const command of commands) { projected = projectWireguardInterfaceDeleteCommand(projected, command); prefixes.push(await wireguardInterfaceDeleteProof(projected)) } + await authorize() + return { target: { scope: "servers", serverIds: [serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: commands.length + 1, + before: { proof: state.proof }, steps: [{ kind: "service", service: "network.wireguard.interfaces.delete", input: { serverId, interfaceId, cascade, commandSummary: commands.map(({ method, path, id }) => ({ method, path, id })), expectedProof: state.proof, expectedStageProofs: prefixes, proofRevision: 1, beforeFingerprint: fingerprint({ proof: state.proof }) } }], + warnings: [cascade ? "Явный каскад: сначала отключение интерфейса при необходимости, затем его peers и статические IPv4/IPv6 адреса, интерфейс удаляется последним. Передача трафика будет прервана." : "Native DELETE удаляет только пустой интерфейс. Каскадного удаления peers и адресов этот alias не разрешает.", `Подготовлено ${commands.length} native-команд и отдельная запись ревизии. Команды выполняются последовательно с проверкой состояния; операция не атомарна.`, "Маршруты, протоколы, firewall, привязки и NetFlow не изменяются каскадно: используемый ими интерфейс требует отдельных предварительных планов.", "Потеря ответа останавливает каскад без повтора. Доступна read-only сверка исходного, промежуточного и желаемого состояния; универсального обратного плана нет."] } +} +export function registerWireguardInterfaceDeleteActions(register: typeof add) { + const current = async (input: Record) => ({ proof: (await readWireguardInterfaceDeleteState((await routerClient(Number(input.serverId))).server, String(input.interfaceId))).proof }) + registerMcpService("network.wireguard.interfaces.delete", { current, + execute: async input => deleteWireguardInterfaceGuarded((await routerClient(Number(input.serverId))).server, String(input.interfaceId), String(input.expectedProof), assertVerifiedConfigurationAccess, { cascade: input.cascade === true }), + verify: async (input, result) => { + const receipt = result as { verified?: boolean; afterProof?: string } | null + return receipt?.verified === true && (await current(input)).proof === receipt.afterProof + }, + reconcile: async (input, authorize) => { + if (input.proofRevision !== 1 || !Array.isArray(input.expectedStageProofs) || input.expectedStageProofs.length < 2 || input.expectedStageProofs.length > 51 || input.expectedStageProofs.some(value => typeof value !== "string" || !/^[a-f0-9]{64}$/.test(value))) throw new McpError("UNSUPPORTED_CAPABILITY", "Нет совместимых prefix proofs для сверки каскада WireGuard", 409) + const state = await readWireguardInterfaceDeleteState((await routerClient(Number(input.serverId))).server, String(input.interfaceId), authorize) + await authorize() + const prefix = input.expectedStageProofs.indexOf(state.proof) + return { matchesOriginalState: prefix === 0, matchesDesiredState: prefix === input.expectedStageProofs.length - 1, matchesExpectedPrefixState: prefix >= 0, verifiedPrefixCommands: prefix < 0 ? null : prefix } + }, + }) + register("network.wireguard.interfaces.delete", "mm_wireguard_interface_remove_plan", "Удалить WireGuard с явным каскадом peers и статических адресов", "network", "mm:network:write", "write", "servers", schema, { + prepare: (input, context) => prepareWireguardInterfaceDeletion(Number(input.serverId), String(input.interfaceId), context), + }) +} diff --git a/backend/src/modules/mcp/wireguard-interface-update-acceptance.ts b/backend/src/modules/mcp/wireguard-interface-update-acceptance.ts new file mode 100644 index 00000000..6cb1ca70 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-interface-update-acceptance.ts @@ -0,0 +1,123 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import wireguardRoutes from "../../routes/wireguard.js" +import { wireguardInterfaceDependencyPaths } from "../../services/wireguard-interface-delete.js" +import { wireguardPublicKey } from "../../services/wireguard-interface-create.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { encryptSecret } from "./security.js" +import { WIREGUARD_INTERFACE_KEY_PURPOSE } from "./wireguard-interface-update-actions.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +/** Dedicated SQL/loopback fixture; caller owns serial PostgreSQL acceptance. */ +export async function acceptWireguardInterfaceUpdate(actor: AuthUser) { + const privateKey = Buffer.alloc(32, 71).toString("base64"), replacement = Buffer.alloc(32, 72).toString("base64") + type Rows = Record[]> + const initial: Rows = Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])) + initial["/interface/wireguard"] = [{ ".id": "*a", name: "wg-update", disabled: "false", mtu: "1420", "listen-port": "13231", "public-key": wireguardPublicKey(privateKey), "private-key": privateKey, opaque: "NEVER_NATIVE_OPAQUE" }] + initial["/interface/wireguard/peers"] = [{ ".id": "*b", interface: "wg-update", disabled: "false", "public-key": wireguardPublicKey(replacement), "allowed-address": "192.0.2.2/32", rx: "0" }] + initial["/interface"] = [{ ".id": "*a", name: "wg-update", type: "wg" }] + initial["/interface/wireguard"].push({ ".id": "*f", name: "wg-foreign", disabled: "true", mtu: "1420", opaque: "NEVER_FOREIGN_CONFIGURATION" }) + initial["/interface"].push({ ".id": "*f", name: "wg-foreign", type: "wg", opaque: "NEVER_FOREIGN_GENERIC" }) + initial["/ip/address"] = [{ ".id": "*c", interface: "wg-update", address: "192.0.2.1/24", disabled: "false", dynamic: "false" }, { ".id": "*d", interface: "wg-update", address: "198.51.100.1/24", disabled: "false", dynamic: "false", comment: "secondary" }] + initial["/ipv6/address"] = [] + let rows = structuredClone(initial), commands: string[] = [], mode = "normal", reads = 0, afterWrite: (() => Promise) | undefined + 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)) + 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 + } + 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 } + let raw = "" + req.on("data", chunk => { raw += chunk }); req.on("end", async () => { + const body = raw ? JSON.parse(raw) as Record : {}, id = path.slice(base.length + 1) + commands.push(req.method + " " + path) + if (mode !== "ignored") { + if (req.method === "DELETE") rows[base] = rows[base].filter(row => row[".id"] !== id) + else if (req.method === "PUT") rows[base].push({ ".id": "*e", dynamic: "false", ...body }) + else { + const selected = rows[base].find(row => row[".id"] === id)!, oldName = selected.name + Object.assign(selected, body) + if (body["private-key"]) selected["public-key"] = wireguardPublicKey(body["private-key"]) + if (body.name && base === "/interface/wireguard") { rows["/interface"][0].name = body.name; for (const row of [...rows["/interface/wireguard/peers"], ...rows["/ip/address"], ...rows["/ipv6/address"]]) if (row.interface === oldName) row.interface = body.name } + } + } + if (mode === "collateral") rows["/interface/wireguard"][0].opaque = "NEVER_COLLATERAL" + if (mode === "generic-collateral") rows["/interface"][1].opaque = "NEVER_GENERIC_COLLATERAL" + if (afterWrite) { const hook = afterWrite; afterWrite = undefined; await hook() } + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}") + }) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const secretIds: string[] = [] + const secret = async (purpose: string, value: string, serverId: number | null = 136, owner = actor.id) => { const id = randomUUID(); secretIds.push(id); await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, purpose, serverId, await encryptSecret(value), owner]); return id } + const wait = async (id: string) => { for (let index = 0; index < 3000; index++) { const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; if (!["pending", "running"].includes(state)) return state; await delay(25) } throw Error("WireGuard update remains live; never replay") } + let connectionId: string | undefined + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(136,'wg-interface-update','127.0.0.1',$1,false,'NEVER_UPDATE_ENDPOINT_PASSWORD')", [(router.address() as { port: number }).port]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('wg-update-user','wg-update-user','Update fixture')") + await rest.register(wireguardRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "WireGuard update", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [136], operationIds: ["network.wireguard.interfaces.update", "ros.wireguard.update", "operations.reconcile", "operations.recover"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token), privateKeyRef = await secret(WIREGUARD_INTERFACE_KEY_PURPOSE, replacement) + const full = { serverId: 136, interfaceId: "*a", patch: { name: "wg-renamed", listenPort: 23456, mtu: 1380, comment: "updated", disabled: true, privateKeyRef } } + const prepare = async (input: unknown = full) => { const plan = await invokeMcpTool(principal, "mm_wireguard_interface_edit_plan", input) as { planId: string; state: string; changes: { mutationCost: number } }; assert.equal(plan.state, "awaiting-approval"); for (const value of [privateKey, replacement, "NEVER_"]) assert.ok(!JSON.stringify(plan).includes(value)); return plan } + const beforeReads = reads + for (const input of [{ ...full, serverId: 1 }, { ...full, patch: { privateKey: replacement } }, { ...full, patch: { name: "\u0000bad" } }, { ...full, patch: {} }]) await assert.rejects(prepare(input)) + assert.equal(reads, beforeReads) + for (const ref of [await secret("wrong-purpose", replacement), await secret(WIREGUARD_INTERFACE_KEY_PURPOSE, replacement, null), await secret(WIREGUARD_INTERFACE_KEY_PURPOSE, replacement, 136, "wg-update-user")]) { await assert.rejects(prepare({ ...full, patch: { privateKeyRef: ref } })); assert.equal(commands.length, 0) } + for (const [path, row] of [["/interface/vlan", { interface: "wg-update" }], ["/routing/filter/rule", { rule: 'set gw "wg-update";' }], ["/routing/ospf/interface-template", { networks: "192.0.2.0/24" }]] as [string, Record][]) { reset(); rows[path].push({ ".id": "*f", ...row }); await assert.rejects(prepare()); assert.equal(commands.length, 0) } + reset() + const protection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [protection, { kind: "wireguard_peer", id: "*b", serverId: 136, protection: "deny" }, actor.id]) + try { await assert.rejects(prepare()); assert.equal((await rest.inject({ method: "PATCH", url: "/api/wireguard/interfaces/136/*a", payload: { disabled: true } })).statusCode, 409); assert.equal(commands.length, 0) } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + for (const fault of ["normal", "ignored", "collateral", "generic-collateral", "lost"]) { + reset(); const plan = await prepare(); await approvePlan(plan.planId, actor, true); mode = fault + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await wait(operation.id), fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial", fault) + assert.equal(commands.length, 1); assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + const reconciled = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesOriginalState: boolean }[] } + assert.equal(reconciled.automaticRetry, false); assert.equal(reconciled.results[0].matchesDesiredState, fault === "normal"); assert.equal(reconciled.results[0].matchesOriginalState, fault === "ignored"); assert.equal(commands.length, 1) + if (fault === "normal") { + const saved = (await pool.query("SELECT state,intended_state FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0] + for (const changed of [{ state: "applied", intended: saved.intended_state }, { state: "verified", intended: { ...saved.intended_state, service: "network.wireguard.interfaces.create" } }, { state: "verified", intended: { ...saved.intended_state, input: { ...saved.intended_state.input, receiptId: randomUUID() } } }, { state: "verified", intended: { ...saved.intended_state, input: { ...saved.intended_state.input, ownerId: "foreign-owner" } } }]) { + await pool.query("UPDATE mcp_operation_steps SET state=$2,intended_state=$3::jsonb WHERE operation_id=$1 AND step_index=0", [operation.id, changed.state, JSON.stringify(changed.intended)]) + const untrusted = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { results: { matchesDesiredState: boolean }[] }; assert.equal(untrusted.results[0].matchesDesiredState, false); assert.equal(commands.length, 1) + } + await pool.query("UPDATE mcp_operation_steps SET state=$2,intended_state=$3::jsonb WHERE operation_id=$1 AND step_index=0", [operation.id, saved.state, JSON.stringify(saved.intended_state)]) + } + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: operation.id }), (error: { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY") + } + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); rows["/interface/wireguard"][0].opaque = "NEVER_LATE_DRIFT"; assert.equal(await wait((await applyPlan(principal, stale.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0) + reset(); const metadata = await prepare(); await approvePlan(metadata.planId, actor, true); await pool.query("UPDATE servers SET name='updated metadata',comment='display-only',updated_at=now() WHERE id=136"); assert.equal(await wait((await applyPlan(principal, metadata.planId, randomUUID())).id), "applied"); assert.equal(commands.length, 1) + reset(); const epoch = await prepare(); await approvePlan(epoch.planId, actor, true); await pool.query("UPDATE servers SET port=port+1 WHERE id=136"); await pool.query("UPDATE servers SET port=port-1 WHERE id=136"); assert.equal(await wait((await applyPlan(principal, epoch.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0) + reset(); const rotated = await prepare(); await approvePlan(rotated.planId, actor, true); await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [privateKeyRef, await encryptSecret(privateKey)]); assert.equal(await wait((await applyPlan(principal, rotated.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0); await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [privateKeyRef, await encryptSecret(replacement)]) + reset(); const paused = await prepare({ serverId: 136, interfaceId: "*a", patch: { address: "2001:db8::1/64" } }); await approvePlan(paused.planId, actor, true); afterWrite = () => setConnectionStatus(connectionId!, "paused", actor); assert.equal(await wait((await applyPlan(principal, paused.planId, randomUUID())).id), "unknown"); assert.equal(commands.length, 1); await setConnectionStatus(connectionId, "active", actor) + for (const address of ["", "2001:db8::1/64", "192.0.2.3/24", "198.51.100.1/24"]) { + reset(); const plan = await prepare({ serverId: 136, interfaceId: "*a", patch: { address } }); await approvePlan(plan.planId, actor, true); assert.equal(await wait((await applyPlan(principal, plan.planId, randomUUID())).id), "applied"); assert.equal(rows["/ip/address"].find(row => row[".id"] === "*d")?.comment, "secondary") + } + reset(); const native = await invokeMcpTool(principal, "mm_wireguard_update_plan", { serverId: 136, id: "*a", fields: { "listen-port": 23456, mtu: 1380, disabled: true, comment: "native" } }) as { planId: string }; await approvePlan(native.planId, actor, true); assert.equal(await wait((await applyPlan(principal, native.planId, randomUUID())).id), "applied"); assert.equal(commands.length, 1) + reset(); mode = "provider-error"; await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")); assert.ok(!(await rest.inject({ method: "PATCH", url: "/api/wireguard/interfaces/136/*a", payload: { comment: "changed" } })).body.includes("NEVER_")) + reset(); const response = await rest.inject({ method: "PATCH", url: "/api/wireguard/interfaces/136/*a", payload: { name: "rest-renamed", privateKey: replacement, disabled: true, listenPort: 34567, mtu: 1380, comment: "rest" } }); assert.equal(response.statusCode, 200, response.body); assert.equal(commands.length, 1); assert.equal(rows["/interface/wireguard"][0]["public-key"], wireguardPublicKey(replacement)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND (result::text LIKE '%NEVER_%' OR result::text LIKE $2 OR result::text LIKE $3)", [connectionId, "%" + privateKey + "%", "%" + replacement + "%"])).rows[0].n, 0) + console.log("✓ WireGuard interface update shared REST/MCP: full fields/key refs, rename children, primary/secondary address semantics, native alias, permissions/protections/source epoch, readback/lost/partial/revoke/no replay, exact verified checkpoint, private diagnostics") + } finally { + await rest.close(); await new Promise(resolve => router.close(() => resolve())) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])", [secretIds]); await pool.query("DELETE FROM app_users WHERE id='wg-update-user'"); await pool.query("DELETE FROM servers WHERE id=136") + } +} diff --git a/backend/src/modules/mcp/wireguard-interface-update-actions.ts b/backend/src/modules/mcp/wireguard-interface-update-actions.ts new file mode 100644 index 00000000..ad169c33 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-interface-update-actions.ts @@ -0,0 +1,103 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { wireguardCreateKey, wireguardCreateProtectionProof } from "../../services/wireguard-interface-create.js" +import { wireguardInterfaceUpdateSchema, readWireguardInterfaceUpdateState, updateWireguardInterface, planWireguardInterfaceUpdate } from "../../services/wireguard-interface-update.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError, decryptSecret, privateSecretFingerprint, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +export const WIREGUARD_INTERFACE_KEY_PURPOSE = "wireguard-interface-private-key" +export const wireguardInterfaceUpdatePlanSchema = z.object({ serverId: z.number().int().positive().safe(), interfaceId: z.string().regex(/^\*[a-z0-9]+$/i).max(100), patch: z.object(wireguardInterfaceUpdateSchema.shape).omit({ privateKey: true }).extend({ privateKeyRef: z.string().uuid().optional() }).strict().refine(value => Object.values(value).some(item => item !== undefined)) }).strict() +const fail = (code: string, message: string) => new McpError(code, message, 409) +async function access(input: Record, signal?: AbortSignal) { + if (signal?.aborted) throw fail("READ_CANCELLED", "Подготовка WireGuard отменена") + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const current = await effectiveMcpConnection(principal) + if (current.owner_id !== input.ownerId || !hasPermission(current.policy.permissions, "mm:network:write") || !hasPermission(current.owner_permissions, "mm:network:write")) throw fail("PERMISSION_DENIED", "Допуск WireGuard отозван") + if (!current.policy.scope.serverIds.includes(Number(input.serverId))) throw fail("SERVER_DENIED", "Сервер вне области допуска") + if (fingerprint(current.policy) !== input.policyProof || fingerprint(current.owner_permissions) !== input.ownerProof) throw fail("RESET_REQUIRED", "Допуск изменился; подготовьте новый план") +} +async function material(input: Record, signal?: AbortSignal) { + await access(input, signal) + const request = wireguardInterfaceUpdatePlanSchema.parse(input.request), references: string[] = [] + if (input.requestProof !== fingerprint(request)) throw fail("STATE_CONFLICT", "Параметры WireGuard не соответствуют сохранённому proof") + const secret = async (id: string | undefined, purpose: string) => { + if (!id) return undefined + await access(input, signal) + const row = (await pool.query("SELECT purpose,server_id,created_by,CASE WHEN octet_length(ciphertext)<=16384 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2", [id, input.ownerId])).rows[0] + if (!row || row.server_id !== request.serverId || row.purpose !== purpose || typeof row.ciphertext !== "string") throw fail("SECRET_UNAVAILABLE", "Ссылка не выдана владельцу для этого ключа и сервера WireGuard") + let value: unknown + try { value = await decryptSecret(row.ciphertext) } catch { throw fail("SECRET_UNAVAILABLE", "Ключ WireGuard недоступен") } + const parsed = wireguardCreateKey.safeParse(value) + if (!parsed.success) throw fail("SECRET_UNAVAILABLE", "Неподдерживаемый формат ключа WireGuard") + references.push(await privateSecretFingerprint(fingerprint({ id, ...row }), "wireguard-update-reference-v1")) + return parsed.data + } + const privateKey = await secret(request.patch.privateKeyRef, WIREGUARD_INTERFACE_KEY_PURPOSE) + const { privateKeyRef: _privateKeyRef, ...fields } = request.patch + await access(input, signal) + return { body: wireguardInterfaceUpdateSchema.parse({ ...fields, privateKey }), referenceProof: fingerprint(references) } +} +export function registerWireguardInterfaceUpdateActions(register: typeof add) { + const source = async (input: Record, signal?: AbortSignal) => { + const keys = await material(input, signal) + const protectionProof = await wireguardCreateProtectionProof(Number(input.serverId)) + const authorize = async () => { + const current = await material(input, signal) + if (current.referenceProof !== keys.referenceProof) throw fail("STATE_CONFLICT", "Ссылка на ключ изменилась") + if (await wireguardCreateProtectionProof(Number(input.serverId)) !== protectionProof) throw fail("STATE_CONFLICT", "Защиты WireGuard изменились") + } + const { server } = await routerClient(Number(input.serverId)), state = await readWireguardInterfaceUpdateState(server, String((input.request as { interfaceId: string }).interfaceId), authorize) + await authorize() + return { ...keys, state, server, authorize, publicState: { proof: state.proof, referenceProof: keys.referenceProof, protectionProof } } + } + registerMcpService("network.wireguard.interfaces.update", { + current: async input => (await source(input)).publicState, + execute: async input => { + const before = await source(input) + if (fingerprint(before.publicState) !== input.beforeFingerprint) throw fail("STATE_CONFLICT", "Источник, ключи или зависимости WireGuard изменились") + return updateWireguardInterface(before.server, String((input.request as { interfaceId: string }).interfaceId), before.body, before.state.proof, async () => { await assertVerifiedConfigurationAccess(); await before.authorize() }) + }, + verify: async (input, result) => { const receipt = result as { verified?: boolean; afterProof?: string } | null; return receipt?.verified === true && (await source(input)).state.proof === receipt.afterProof }, + reconcile: async (input, authorize) => { + if (!Number.isInteger(input.commandCount) || Number(input.commandCount) < 0 || Number(input.commandCount) > 50 || input.proofRevision !== 2 || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof) || input.requestProof !== fingerprint(input.request)) throw fail("UNSUPPORTED_CAPABILITY", "Нет совместимого proof изменения WireGuard") + await authorize() + const latest = await source(input) + if (latest.referenceProof !== input.expectedReferenceProof) throw fail("STATE_CONFLICT", "Ссылка на ключ изменилась после подготовки") + const stages = Number(input.commandCount) + if (!z.string().uuid().safeParse(input.receiptId).success) throw fail("UNSUPPORTED_CAPABILITY", "Нет идентификатора проверенного checkpoint WireGuard") + const principal = input.principal as { connectionId: string } + const receipts = (await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.state='verified' AND s.intended_state=$2::jsonb AND s.result->>'verified'='true' LIMIT 2", [principal.connectionId, JSON.stringify(redact({ kind: "service", service: "network.wireguard.interfaces.update", input }))])).rows + await authorize() + const fresh = await source(input) + if (fingerprint(fresh.publicState) !== fingerprint(latest.publicState)) throw fail("STATE_CONFLICT", "WireGuard изменился во время сверки") + const afterProof = receipts.length === 1 && /^[a-f0-9]{64}$/.test(receipts[0].result?.afterProof ?? "") ? receipts[0].result.afterProof as string : undefined + const original = latest.state.proof === input.expectedProof, desired = afterProof !== undefined && latest.state.proof === afterProof + await authorize() + return { matchesOriginalState: original, matchesDesiredState: desired, matchesExpectedPrefixState: original || desired, verifiedPrefixCommands: desired ? stages : original ? 0 : null, verifiedFinalCheckpointAvailable: afterProof !== undefined, forwardingVerified: false } + }, + }) + register("network.wireguard.interfaces.update", "mm_wireguard_interface_edit_plan", "Изменить интерфейс WireGuard и основной адрес с проверкой зависимостей", "network", "mm:network:write", "write", "servers", wireguardInterfaceUpdatePlanSchema, { + version: 3, + prepare: async (input, context) => { + // Seal the same JSON representation that survives durable plan storage. + const request = wireguardInterfaceUpdatePlanSchema.parse(JSON.parse(JSON.stringify(input))), internal = { request, requestProof: fingerprint(request), serverId: request.serverId, principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions) } + const before = await source(internal, context.signal) + const commands = planWireguardInterfaceUpdate(before.state, request.interfaceId, before.body) + const objects = [{ kind: "server", id: String(request.serverId), serverId: request.serverId }, { kind: "wireguard", id: request.interfaceId, serverId: request.serverId }] + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(request.serverId)]) + if (protections.rows.length > 1000) throw fail("STATE_UNAVAILABLE", "Превышен предел защит WireGuard") + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: request.serverId }) + await before.authorize() + return { target: { scope: "servers", serverIds: [request.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: commands.length + 1, before: before.publicState, + steps: [{ kind: "service", service: "network.wireguard.interfaces.update", input: { ...internal, receiptId: randomUUID(), commandCount: commands.length, expectedProof: before.state.proof, expectedReferenceProof: before.referenceProof, proofRevision: 2, beforeFingerprint: fingerprint(before.publicState) } }], + warnings: ["Changing interface identity, key, address, port or disabled can interrupt traffic. Primary address replacement preserves secondary static addresses; rename and address must be separate plans.", "Keys require owner/purpose/server secret references. Every command requires intent, fresh source and read-back. Lost responses stop execution without replay; changes are not atomic and have no automatic rollback.", "Reconciliation proves original state or the exact durable verified final checkpoint. Handshake and forwarding require separate verification."] } + + }, + }) +} diff --git a/backend/src/modules/mcp/wireguard-peer-create-acceptance.ts b/backend/src/modules/mcp/wireguard-peer-create-acceptance.ts new file mode 100644 index 00000000..5bf3046a --- /dev/null +++ b/backend/src/modules/mcp/wireguard-peer-create-acceptance.ts @@ -0,0 +1,213 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import wireguardRoutes from "../../routes/wireguard.js" +import { wireguardInterfaceDependencyPaths } from "../../services/wireguard-interface-delete.js" +import { wireguardPublicKey } from "../../services/wireguard-interface-create.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { encryptSecret } from "./security.js" +import { WIREGUARD_PEER_KEY_PURPOSE } from "./wireguard-interface-create-actions.js" +import { wireguardPeerCreatePlanSchema } from "./wireguard-peer-create-actions.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +type Rows = Record[]> +/** Run only from the PostgreSQL acceptance harness; never uses a real router. */ +export async function acceptWireguardPeerCreation(actor: AuthUser) { + const privateKey = Buffer.alloc(32, 61).toString("base64"), generatedKey = Buffer.alloc(32, 62).toString("base64"), publicKey = wireguardPublicKey(privateKey) + const initial: Rows = Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])) + initial["/interface/wireguard"] = [{ ".id": "*a", name: "wg-parent", disabled: "false", "public-key": wireguardPublicKey(generatedKey), "private-key": "NEVER_PARENT_PRIVATE", opaque: "NEVER_PARENT_OPAQUE" }] + initial["/interface/wireguard/peers"] = [{ ".id": "*f", interface: "wg-parent", "public-key": wireguardPublicKey(Buffer.alloc(32, 63).toString("base64")), disabled: "false", opaque: "NEVER_FOREIGN_PEER", rx: "0" }] + initial["/interface"] = [{ ".id": "*a", name: "wg-parent", type: "wg" }] + initial["/ip/address"] = [{ ".id": "*ip", address: "192.0.2.1/24", interface: "wg-parent", disabled: "false", dynamic: "false" }] + initial["/ipv6/address"] = [{ ".id": "*v6", address: "2001:db8:112::1/64", interface: "wg-parent", disabled: "false", dynamic: "false", advertise: "false" }] + initial["/routing/ospf/interface-template"] = [{ ".id": "*ospf", interfaces: "wg-parent", networks: "192.0.2.0/24,2001:db8:112::/64" }] + initial["/ip/vrf"] = [{ ".id": "*vrf", name: "owned", interfaces: "wg-parent" }] + initial["/ip/firewall/filter"] = [{ ".id": "*fw", "in-interface": "wg-parent", action: "accept" }] + let rows = structuredClone(initial), mode = "normal", reads = 0, commands: string[] = [], afterWrite: (() => Promise) | undefined + 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" + 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 + } + if (!["PUT", "PATCH"].includes(req.method ?? "") || !(path === base || path === base + "/*c")) { res.statusCode = 400; res.end("{}"); return } + let raw = "" + req.on("data", chunk => { raw += chunk }); req.on("end", async () => { + const body = JSON.parse(raw) as Record + commands.push(req.method + " " + path) + if (mode !== "ignored") { + if (req.method === "PUT") { + const row: Record = { ".id": "*c", ...body, opaque: "NEVER_NEW_NATIVE_DEFAULT" } + if (row["private-key"] === "auto") row["private-key"] = generatedKey + rows[base].push(row) + } else Object.assign(rows[base].find(row => row[".id"] === "*c")!, body) + } + if (mode === "collateral") rows["/ip/address"][0].comment = "NEVER_COLLATERAL" + if (mode === "parent-drift") rows["/interface/wireguard"][0].opaque = "NEVER_CHANGED_PARENT" + if (mode === "staged-collateral" && commands.length === 2) rows[base][1].opaque = "NEVER_CHANGED_STAGED" + if (afterWrite) { const hook = afterWrite; afterWrite = undefined; await hook() } + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}") + }) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + const secretIds: string[] = [] + const secret = async (purpose: string, value: string, serverId: number | null = 112, owner = actor.id) => { + const id = randomUUID(); secretIds.push(id) + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, purpose, serverId, await encryptSecret(value), owner]); return id + } + const wait = async (id: string) => { + for (let index = 0; index < 3000; index++) { const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state; if (!["pending", "running"].includes(state)) return state; await delay(25) } + throw Error("WireGuard peer creation remains live; never replay") + } + let connectionId: string | undefined + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(112,'wg-peer-create','127.0.0.1',$1,false,'NEVER_PEER_ENDPOINT_PASSWORD')", [(router.address() as { port: number }).port]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('wg-peer-create-user','wg-peer-create-user','Peer fixture')") + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type,peer_public_key) VALUES('wg-peer-create-binding','wg-peer-create-user',112,'wg-parent','wg',$1)", [initial["/interface/wireguard/peers"][0]["public-key"]]) + await rest.register(wireguardRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "WireGuard peer creation", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [112], operationIds: ["network.wireguard.peers.create", "ros.wireguard_peer.create", "operations.reconcile", "operations.recover"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token), privateKeyRef = await secret(WIREGUARD_PEER_KEY_PURPOSE, privateKey) + const full = { serverId: 112, interfaceName: "wg-parent", publicKey, allowedAddresses: ["192.0.2.2/32", "2001:db8:112::2/128"], endpointAddress: "vpn.example.org", endpointPort: 24321, persistentKeepalive: 25, comment: "owned peer", name: "owned-peer", privateKeyRef, clientAddress: "192.0.2.2,2001:db8:112::2", clientDns: "1.1.1.1", clientEndpoint: "client.example.org" } + const prepare = async (input: unknown = full) => { + const plan = await invokeMcpTool(principal, "mm_wireguard_peer_add_plan", input) as { planId: string; state: string; changes: { mutationCost: number } } + assert.equal(plan.state, "awaiting-approval") + assert.ok(!JSON.stringify(plan).includes(privateKey)); assert.ok(!JSON.stringify(plan).includes("NEVER_")) + return plan + } + const beforeInvalid = reads + for (const input of [{ ...full, serverId: 1 }, { ...full, privateKey }, { ...full, privateKeyMode: "none" }, { ...full, allowedAddresses: ["invalid"] }]) await assert.rejects(prepare(input)) + assert.equal(wireguardPeerCreatePlanSchema.safeParse({ ...full, privateKey }).success, false); assert.equal(reads, beforeInvalid) + for (const ref of [await secret("wrong-purpose", privateKey), await secret(WIREGUARD_PEER_KEY_PURPOSE, privateKey, null), await secret(WIREGUARD_PEER_KEY_PURPOSE, privateKey, 112, "wg-peer-create-user")]) { await assert.rejects(prepare({ ...full, privateKeyRef: ref })); assert.equal(commands.length, 0) } + for (const change of [() => { rows["/interface/wireguard"][0].comment = "MikrotikManager traffic-flow overlay" }, () => { rows["/interface"][0][".id"] = "*mismatch" }, () => { rows["/interface/wireguard/peers"][0]["public-key"] = publicKey }]) { reset(); change(); await assert.rejects(prepare()); assert.equal(commands.length, 0) } + reset() + const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2,$3)", [protection, { kind: "wireguard", id: "*a", serverId: 112, protection: "deny" }, actor.id]) + try { await assert.rejects(prepare()); assert.equal((await rest.inject({ method: "POST", url: "/api/wireguard/peers", payload: { ...full, privateKeyRef: undefined } })).statusCode, 409); assert.equal(commands.length, 0) } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + reset() + const native = await invokeMcpTool(principal, "mm_wireguard_peer_create_plan", { serverId: 112, fields: { interface: "*a", "public-key": publicKey, "allowed-address": "192.0.2.2/32,2001:db8:112::2/128", "persistent-keepalive": "25s", disabled: false } }) as { planId: string; state: string; changes: { steps: { kind: string; service?: string }[]; mutationCost: number } } + assert.equal(native.state, "awaiting-approval"); assert.equal(native.changes.mutationCost, 3) + assert.equal(native.changes.steps[0].kind, "service"); assert.equal(native.changes.steps[0].service, "network.wireguard.peers.create") + await assert.rejects(applyPlan(principal, native.planId, randomUUID())) + await approvePlan(native.planId, actor, true) + assert.equal(await wait((await applyPlan(principal, native.planId, randomUUID())).id), "applied") + assert.equal(commands.length, 2); assert.equal(rows["/interface/wireguard/peers"][1]["persistent-keepalive"], "25") + reset() + const dependencyFailure = await prepare() + await approvePlan(dependencyFailure.planId, actor, true) + mode = "dependency-error" + const diagnosticLines: string[] = [] + const previousError = console.error + console.error = (...args: unknown[]) => { + if (typeof args[0] === "string" && args[0].includes('"event":"mcp.wireguard.peer-create-source-failed"')) diagnosticLines.push(args[0]) + else previousError(...args) + } + try { + const operation = await applyPlan(principal, dependencyFailure.planId, randomUUID()) + assert.equal(await wait(operation.id), "rejected") + const { rows: [terminal] } = await pool.query("SELECT error_code,result FROM mcp_operations WHERE id=$1", [operation.id]) + assert.equal(terminal.error_code, "STATE_UNAVAILABLE") + assert.doesNotMatch(JSON.stringify(terminal), /NEVER_PROVIDER_SECRET/) + assert.equal(commands.length, 0) + assert.equal(diagnosticLines.length, 1) + assert.deepEqual(JSON.parse(diagnosticLines[0]), { + event: "mcp.wireguard.peer-create-source-failed", phase: "current", + read: { catalogue: "/ip/address", attempt: 1, category: "http-refusal", httpStatus: 400 }, + }) + reset() + diagnosticLines.length = 0 + const readBackFailure = await prepare() + await approvePlan(readBackFailure.planId, actor, true) + afterWrite = async () => { mode = "dependency-error" } + const key = randomUUID() + const uncertain = await applyPlan(principal, readBackFailure.planId, key) + assert.equal(await wait(uncertain.id), "unknown") + const { rows: [unverified] } = await pool.query("SELECT error_code,result FROM mcp_operations WHERE id=$1", [uncertain.id]) + assert.equal(unverified.error_code, "MUTATION_OUTCOME_UNKNOWN") + assert.doesNotMatch(JSON.stringify(unverified), /NEVER_PROVIDER_SECRET/) + assert.deepEqual(commands, ["PUT /interface/wireguard/peers"]) + assert.equal(rows["/interface/wireguard/peers"][1].disabled, "true") + assert.equal((await applyPlan(principal, readBackFailure.planId, key)).id, uncertain.id) + assert.equal(commands.length, 1) + assert.deepEqual(diagnosticLines.map(line => JSON.parse(line)), [{ + event: "mcp.wireguard.peer-create-source-failed", phase: "apply-executor", + read: { catalogue: "/ip/address", attempt: 1, category: "http-refusal", httpStatus: 400, stage: "read-back" }, + }]) + } finally { console.error = previousError } + for (const fault of ["normal", "ignored", "collateral", "parent-drift", "lost", "staged-collateral"]) { + reset(); const plan = await prepare(); assert.equal(plan.changes.mutationCost, 3); await approvePlan(plan.planId, actor, true); mode = fault + const key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await wait(operation.id), fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial", JSON.stringify({ fault, ...(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] })) + assert.equal(commands.length, ["normal", "staged-collateral"].includes(fault) ? 2 : 1) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id) + const count: number = commands.length + type Reconciled = { automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesOriginalState: boolean; verifiedFinalCheckpointAvailable: boolean }[] } + const reconcile = () => invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as Promise + const reconciled = await reconcile() + assert.equal(reconciled.automaticRetry, false); assert.equal(reconciled.results[0].matchesDesiredState, fault === "normal"); assert.equal(reconciled.results[0].matchesOriginalState, fault === "ignored"); assert.equal(commands.length, count) + if (fault === "normal") { + assert.equal(rows["/interface/wireguard/peers"][1].disabled, "false"); assert.equal(rows["/interface/wireguard/peers"][1]["private-key"], privateKey) + const step = (await pool.query("SELECT intended_state,result FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0] + const variants = [ + { state: "sent", intended: step.intended_state, result: step.result }, + { state: "verified", intended: { ...step.intended_state, kind: "ros" }, result: step.result }, + { state: "verified", intended: { ...step.intended_state, service: "network.wireguard.interfaces.create" }, result: step.result }, + ...["credentialId", "connectionId"].map(field => ({ state: "verified", intended: { ...step.intended_state, input: { ...step.intended_state.input, principal: { ...step.intended_state.input.principal, [field]: randomUUID() } } }, result: step.result })), + ...["ownerId", "expectedProof", "expectedReferenceProof", "requestProof", "receiptId", "proofRevision"].map(field => ({ state: "verified", intended: { ...step.intended_state, input: { ...step.intended_state.input, [field]: field === "proofRevision" ? 2 : randomUUID() } }, result: step.result })), + { state: "verified", intended: { ...step.intended_state, input: { ...step.intended_state.input, request: { ...step.intended_state.input.request, name: "other" } } }, result: step.result }, + { state: "verified", intended: step.intended_state, result: null }, + { state: "verified", intended: step.intended_state, result: { ...step.result, peerId: "*foreign" } }, + ] + for (const variant of variants) { + await pool.query("UPDATE mcp_operation_steps SET state=$2,intended_state=$3::jsonb,result=$4::jsonb WHERE operation_id=$1", [operation.id, variant.state, JSON.stringify(variant.intended), JSON.stringify(variant.result)]) + assert.equal((await reconcile()).results[0].matchesDesiredState, false) + } + await pool.query("UPDATE mcp_operation_steps SET state='verified',intended_state=$2::jsonb,result=$3::jsonb WHERE operation_id=$1", [operation.id, JSON.stringify(step.intended_state), JSON.stringify(step.result)]) + assert.equal((await reconcile()).results[0].matchesDesiredState, true); assert.equal(commands.length, count) + } + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: operation.id }), (error: { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY") + } + reset(); const stale = await prepare(); await approvePlan(stale.planId, actor, true); rows["/interface/wireguard"][0].opaque = "NEVER_LATE_PARENT_DRIFT" + assert.equal(await wait((await applyPlan(principal, stale.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0) + reset(); const rotated = await prepare(); await approvePlan(rotated.planId, actor, true); await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [privateKeyRef, await encryptSecret(generatedKey)]) + assert.equal(await wait((await applyPlan(principal, rotated.planId, randomUUID())).id), "rejected"); assert.equal(commands.length, 0) + await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1", [privateKeyRef, await encryptSecret(privateKey)]) + reset(); const paused = await prepare(); await approvePlan(paused.planId, actor, true); afterWrite = () => setConnectionStatus(connectionId!, "paused", actor) + assert.equal(await wait((await applyPlan(principal, paused.planId, randomUUID())).id), "unknown"); assert.equal(commands.length, 1) + await setConnectionStatus(connectionId, "active", actor) + for (const privateKeyMode of ["auto", "none", undefined]) { + reset(); const plan = await prepare({ ...full, privateKeyRef: undefined, privateKeyMode, disabled: true }); await approvePlan(plan.planId, actor, true) + const operation = await applyPlan(principal, plan.planId, randomUUID()) + assert.equal(await wait(operation.id), "applied", JSON.stringify((await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0])); assert.equal(commands.length, 1) + } + reset(); mode = "provider-error"; await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + assert.ok(!(await rest.inject({ method: "POST", url: "/api/wireguard/peers", payload: { ...full, privateKeyRef: undefined, privateKey } })).body.includes("NEVER_")) + reset() + const response = await rest.inject({ method: "POST", url: "/api/wireguard/peers", payload: { ...full, privateKeyRef: undefined, privateKey } }) + assert.equal(response.statusCode, 201, response.body); assert.deepEqual(response.json(), { ok: true }); assert.equal(commands.length, 2) + assert.equal(rows["/interface/wireguard/peers"][1]["client-address"], full.clientAddress) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND (result::text LIKE '%NEVER_%' OR result::text LIKE $2)", [connectionId, "%" + privateKey + "%"])).rows[0].n, 0) + assert.ok((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent' AND payload->>'serverId'='112'")).rows[0].n >= commands.length) + console.log("✓ WireGuard peer create REST/MCP: all fields, IPv4/IPv6, key reference/modes, supported existing parent users/address/OSPF/VRF/firewall, exact checkpoints, per-stage proofs, lost/partial/revoke/drift/idempotency/privacy") + } finally { + await rest.close(); await new Promise(resolve => router.close(() => resolve())) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])", [secretIds]) + await pool.query("DELETE FROM user_interface_bindings WHERE id='wg-peer-create-binding'") + await pool.query("DELETE FROM app_users WHERE id='wg-peer-create-user'") + await pool.query("DELETE FROM servers WHERE id=112") + } +} diff --git a/backend/src/modules/mcp/wireguard-peer-create-actions.ts b/backend/src/modules/mcp/wireguard-peer-create-actions.ts new file mode 100644 index 00000000..b71e78eb --- /dev/null +++ b/backend/src/modules/mcp/wireguard-peer-create-actions.ts @@ -0,0 +1,116 @@ +import { z } from "zod" +import { randomUUID } from "node:crypto" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { wireguardCreateKey, wireguardCreateProtectionProof, readWireguardInterfaceCreateState } from "../../services/wireguard-interface-create.js" +import { wireguardPeerCreateSchema, assertWireguardPeerCreateAllowed, createWireguardPeer, wireguardPeerCreateStages } from "../../services/wireguard-peer-create.js" +import { wireguardReadDiagnostic } from "../../services/wireguard-read-diagnostic.js" +import { WIREGUARD_PEER_KEY_PURPOSE } from "./wireguard-interface-create-actions.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError, decryptSecret, privateSecretFingerprint, redact } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { add } from "./actions.js" + +export const wireguardPeerCreatePlanSchema = wireguardPeerCreateSchema.omit({ privateKey: true }).extend({ privateKeyRef: z.string().uuid().optional(), privateKeyMode: z.enum(["auto", "none"]).optional() }).strict().refine(value => !(value.privateKeyRef && value.privateKeyMode), "Выберите ссылку или режим ключа") +const fail = (code: string, message: string) => new McpError(code, message, 409) +function reportPeerSourceFailure(phase: "current" | "apply-source" | "apply-executor", error: unknown) { + const read = wireguardReadDiagnostic(error) + if (read || error instanceof McpError && error.code === "STATE_UNAVAILABLE") { + try { + console.error(JSON.stringify({ event: "mcp.wireguard.peer-create-source-failed", phase, read })) + } catch { /* A diagnostic must not replace the failure. */ } + } +} +async function access(input: Record, signal?: AbortSignal) { + if (signal?.aborted) throw fail("READ_CANCELLED", "Подготовка peer отменена") + const principal = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string() }).strict().parse(input.principal) + const current = await effectiveMcpConnection(principal) + if (current.owner_id !== input.ownerId || !hasPermission(current.policy.permissions, "mm:network:write") || !hasPermission(current.owner_permissions, "mm:network:write")) throw fail("PERMISSION_DENIED", "Допуск WireGuard отозван") + if (!current.policy.scope.serverIds.includes(Number(input.serverId))) throw fail("SERVER_DENIED", "Сервер вне области допуска") + if (fingerprint(current.policy) !== input.policyProof || fingerprint(current.owner_permissions) !== input.ownerProof) throw fail("RESET_REQUIRED", "Допуск изменился; подготовьте новый план") +} +async function material(input: Record, signal?: AbortSignal) { + await access(input, signal) + const request = wireguardPeerCreatePlanSchema.parse(input.request) + if (fingerprint(request) !== input.requestProof) throw fail("STATE_CONFLICT", "Параметры peer не соответствуют сохранённому proof") + let privateKey: string | undefined, referenceProof = fingerprint([]) + if (request.privateKeyRef) { + await access(input, signal) + const id = request.privateKeyRef + const row = (await pool.query("SELECT purpose,server_id,created_by,CASE WHEN octet_length(ciphertext)<=16384 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2", [id, input.ownerId])).rows[0] + if (!row || row.server_id !== request.serverId || row.purpose !== WIREGUARD_PEER_KEY_PURPOSE || typeof row.ciphertext !== "string") throw fail("SECRET_UNAVAILABLE", "Ссылка не выдана владельцу для ключа peer и этого сервера") + let value: unknown + try { value = await decryptSecret(row.ciphertext) } catch { throw fail("SECRET_UNAVAILABLE", "Ключ peer недоступен") } + const parsed = wireguardCreateKey.safeParse(value) + if (!parsed.success) throw fail("SECRET_UNAVAILABLE", "Неподдерживаемый формат ключа peer") + privateKey = parsed.data + referenceProof = await privateSecretFingerprint(fingerprint({ id, ...row }), "wireguard-peer-create-reference-v1") + } + const { privateKeyRef: _ref, privateKeyMode, ...fields } = request + await access(input, signal) + return { body: wireguardPeerCreateSchema.parse({ ...fields, privateKey: privateKey ?? privateKeyMode }), referenceProof } +} +export function registerWireguardPeerCreateActions(register: typeof add) { + const source = async (input: Record, signal?: AbortSignal) => { + const keys = await material(input, signal), protectionProof = await wireguardCreateProtectionProof(Number(input.serverId)) + const authorize = async () => { + if ((await material(input, signal)).referenceProof !== keys.referenceProof) throw fail("STATE_CONFLICT", "Ссылка на ключ peer изменилась") + if (await wireguardCreateProtectionProof(Number(input.serverId)) !== protectionProof) throw fail("STATE_CONFLICT", "Защиты WireGuard изменились") + } + const { server } = await routerClient(Number(input.serverId)), state = await readWireguardInterfaceCreateState(server, authorize) + await authorize() + return { ...keys, state, server, authorize, publicState: { proof: state.proof, referenceProof: keys.referenceProof, protectionProof } } + } + registerMcpService("network.wireguard.peers.create", { + current: async input => { + try { return (await source(input)).publicState } + catch (error) { reportPeerSourceFailure("current", error); throw error } + }, + execute: async input => { + let phase: "apply-source" | "apply-executor" = "apply-source" + try { + const before = await source(input) + if (fingerprint(before.publicState) !== input.beforeFingerprint) throw fail("STATE_CONFLICT", "Источник, ключи или зависимости peer изменились") + phase = "apply-executor" + return await createWireguardPeer(before.server, before.body, before.state.proof, async () => { await assertVerifiedConfigurationAccess(); await before.authorize() }) + } catch (error) { + reportPeerSourceFailure(phase, error) + throw error + } + }, + verify: async (input, result) => { const receipt = result as { verified?: boolean; afterProof?: string } | null; return receipt?.verified === true && (await source(input)).state.proof === receipt.afterProof }, + reconcile: async (input, authorize) => { + if (input.proofRevision !== 1 || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof) || !z.string().uuid().safeParse(input.receiptId).success) throw fail("UNSUPPORTED_CAPABILITY", "Нет совместимого proof и checkpoint создания peer") + await authorize() + const latest = await source(input) + if (latest.referenceProof !== input.expectedReferenceProof) throw fail("STATE_CONFLICT", "Ссылка на ключ изменилась после подготовки") + const principal = input.principal as { connectionId: string } + const receipts = (await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.state='verified' AND s.intended_state->>'kind'='service' AND s.intended_state->>'service'=$2 AND s.intended_state->'input'->>'receiptId'=$3 AND s.intended_state->'input'=$4::jsonb AND s.result->>'verified'='true' LIMIT 2", [principal.connectionId, "network.wireguard.peers.create", input.receiptId, JSON.stringify(redact(input))])).rows + await authorize() + const fresh = await source(input) + if (fingerprint(fresh.publicState) !== fingerprint(latest.publicState)) throw fail("STATE_CONFLICT", "WireGuard изменился во время сверки") + const receipt = receipts.length === 1 ? receipts[0].result : undefined + const afterProof = /^[a-f0-9]{64}$/.test(receipt?.afterProof ?? "") && /^\*[a-z0-9]+$/i.test(receipt?.peerId ?? "") ? receipt.afterProof as string : undefined + const original = latest.state.proof === input.expectedProof, desired = afterProof !== undefined && latest.state.proof === afterProof && latest.state.base.peers.some(row => row[".id"] === receipt.peerId && row["public-key"] === latest.body.publicKey) + await authorize() + return { matchesOriginalState: original, matchesDesiredState: desired, matchesExpectedPrefixState: original || desired, verifiedPrefixCommands: desired ? wireguardPeerCreateStages(latest.body).length : original ? 0 : null, verifiedFinalCheckpointAvailable: afterProof !== undefined, forwardingVerified: false } + }, + }) + register("network.wireguard.peers.create", "mm_wireguard_peer_add_plan", "Создать peer WireGuard с отключённой подготовкой и проверкой родителя", "network", "mm:network:write", "write", "servers", wireguardPeerCreatePlanSchema, { + prepare: async (input, context) => { + // Seal the same JSON representation that survives durable plan storage. + const request = wireguardPeerCreatePlanSchema.parse(JSON.parse(JSON.stringify(input))), internal = { request, requestProof: fingerprint(request), serverId: request.serverId, principal: context.principal, ownerId: context.connection.owner_id, policyProof: fingerprint(context.connection.policy), ownerProof: fingerprint(context.connection.owner_permissions) } + const before = await source(internal, context.signal), parent = assertWireguardPeerCreateAllowed(before.state, before.body) + const objects = [{ kind: "server", id: String(request.serverId), serverId: request.serverId }, { kind: "wireguard", id: parent[".id"], serverId: request.serverId }, { kind: "wireguard_peer", id: request.publicKey, serverId: request.serverId }] + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(request.serverId)]) + if (protections.rows.length > 1000) throw fail("STATE_UNAVAILABLE", "Превышен предел защит WireGuard") + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId: request.serverId }) + await before.authorize() + return { target: { scope: "servers", serverIds: [request.serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: wireguardPeerCreateStages(before.body).length + 1, before: before.publicState, + steps: [{ kind: "service", service: "network.wireguard.peers.create", input: { ...internal, receiptId: randomUUID(), expectedProof: before.state.proof, expectedReferenceProof: before.referenceProof, proofRevision: 1, beforeFingerprint: fingerprint(before.publicState) } }], + warnings: ["Peer создаётся отключённым и затем включается отдельной командой, если это запрошено; возможна остановка в промежуточном состоянии.", "Приватный ключ принимается через owner/purpose/server secret reference либо режим auto/none; plaintext не возвращается модели.", "Каждая команда требует свежий источник и read-back; при потере ответа повтор запрещён. Сверка признаёт только исходный proof или сохранённый проверенный checkpoint с native ID. Автоматического отката нет.", "Handshake, передача трафика, маршрутизация и firewall требуют отдельной приёмки."] } + }, + }) +} diff --git a/backend/src/modules/mcp/wireguard-peer-delete-acceptance.ts b/backend/src/modules/mcp/wireguard-peer-delete-acceptance.ts new file mode 100644 index 00000000..16158912 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-peer-delete-acceptance.ts @@ -0,0 +1,191 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import wireguardRoutes from "../../routes/wireguard.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +export async function acceptWireguardPeerDeletion(actor: AuthUser) { + const privateKey = Buffer.alloc(32, 7).toString("base64"), psk = Buffer.alloc(32, 8).toString("base64") + const publicKey = Buffer.alloc(32, 1).toString("base64"), otherKey = Buffer.alloc(32, 2).toString("base64") + const baseInterfaces = [{ ".id": "*a1", name: "wg-proof", comment: "", "private-key": privateKey, "listen-port": "13231", mtu: "1420", disabled: "false", opaque: "NEVER_WG_OPAQUE" }] + const basePeers: Record[] = [{ ".id": "*b1", interface: "wg-proof", "public-key": publicKey, "preshared-key": psk, "allowed-address": "10.1.0.2/32", disabled: "false", comment: "selected" }, { ".id": "*b2", interface: "wg-proof", "public-key": otherKey, "allowed-address": "10.1.0.3/32", disabled: "false", comment: "preserve", rx: "0" }] + let interfaces = structuredClone(baseInterfaces), peers = structuredClone(basePeers), mode = "normal", deletes = 0, reads = 0 + let duringRead: (() => Promise) | undefined + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json") + if (req.method === "GET") { + reads++ + if (duringRead) { + const hook = duringRead; duringRead = undefined + void hook().then(() => res.end(JSON.stringify(req.url === "/rest/interface/wireguard" ? interfaces : peers)), () => { res.statusCode = 503; res.end("{}") }) + return + } + if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_WG_PROVIDER_PASSWORD"}'); return } + res.end(JSON.stringify(req.url === "/rest/interface/wireguard" ? interfaces : req.url === "/rest/interface/wireguard/peers" ? peers : [])); return + } + if (req.method === "DELETE" && req.url?.startsWith("/rest/interface/wireguard/peers/")) { + deletes++ + const id = decodeURIComponent(req.url.split("/").at(-1)!) + if (mode !== "ignored") peers = peers.filter(row => row[".id"] !== id) + if (mode === "collateral") peers[0].comment = "unexpected-change" + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}"); return + } + res.statusCode = 400; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + let connectionId: string | undefined + const originalFlow = (await pool.query("SELECT host_public_key,peers_json FROM traffic_flow_settings WHERE id=1")).rows[0] + const reset = () => { mode = "normal"; interfaces = structuredClone(baseInterfaces); peers = structuredClone(basePeers) } + const wait = async (id: string) => { + for (let n = 0; n < 250; n++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state + if (!["pending", "running"].includes(state)) return state + await delay(25) + } + throw Error("WireGuard peer operation remains live; never replay") + } + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(105,'wg-peer-proof','127.0.0.1',$1,false,'NEVER_WG_ENDPOINT_PASSWORD')", [(router.address() as { port: number }).port]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('wg-peer-proof-user','wg-peer-proof-user','WG peer binding fixture')") + await rest.register(wireguardRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "WireGuard peer deletion", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [105], operationIds: ["network.wireguard.peers.delete", "ros.wireguard_peer.delete", "operations.reconcile"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = async (native = false) => { + const plan = await invokeMcpTool(principal, native ? "mm_wireguard_peer_delete_plan" : "mm_wireguard_peer_remove_plan", native ? { serverId: 105, id: "*b1" } : { serverId: 105, peerId: "*b1" }) as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval") + const preview = JSON.stringify(plan) + for (const value of [privateKey, psk, "NEVER_"]) assert.ok(!preview.includes(value)) + return plan + } + const beforeHidden = reads + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_peer_remove_plan", { serverId: 1, peerId: "*b1" })) + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_peer_remove_plan", { serverId: 105, peerId: "../bad" })) + assert.equal(reads, beforeHidden) + interfaces[0].name = "wg-flow"; peers.forEach(row => { row.interface = "wg-flow" }) + await prepare() // An ordinary interface name alone is not an ownership marker. + interfaces[0].comment = "MikrotikManager traffic-flow overlay"; peers[0].comment = "MM traffic-flow collector" + const beforeManaged = deletes + await assert.rejects(prepare(), /NetFlow/); await assert.rejects(prepare(true), /NetFlow/) + assert.equal((await rest.inject({ method: "DELETE", url: "/api/wireguard/peers/105/*b1" })).statusCode, 409) + assert.equal(deletes, beforeManaged); reset() + if (!originalFlow) await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1)") + try { + await pool.query("UPDATE traffic_flow_settings SET host_public_key=$1,peers_json=$2::jsonb WHERE id=1", [publicKey, JSON.stringify([{ serverId: 105 }])]) + const beforeRegistered = deletes + await assert.rejects(prepare(), /NetFlow/); await assert.rejects(prepare(true), /NetFlow/) + assert.equal((await rest.inject({ method: "DELETE", url: "/api/wireguard/peers/105/*b1" })).statusCode, 409) + assert.equal(deletes, beforeRegistered) + await pool.query("UPDATE traffic_flow_settings SET peers_json=$1::jsonb WHERE id=1", [JSON.stringify([{ serverId: 106 }])]) + await prepare() // A different server's registration grants no ownership here. + await pool.query("UPDATE traffic_flow_settings SET peers_json='{}'::jsonb WHERE id=1") + await assert.rejects(prepare(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + } finally { + if (originalFlow) await pool.query("UPDATE traffic_flow_settings SET host_public_key=$1,peers_json=$2::jsonb WHERE id=1", [originalFlow.host_public_key, JSON.stringify(originalFlow.peers_json)]) + else await pool.query("DELETE FROM traffic_flow_settings WHERE id=1") + } + const legacy = await prepare(true) + await approvePlan(legacy.planId, actor, true) + await pool.query("UPDATE mcp_plans SET operation_version=1 WHERE id=$1", [legacy.planId]) + const beforeLegacy = deletes + await assert.rejects(applyPlan(principal, legacy.planId, randomUUID()), (error: { code?: string }) => error.code === "PLAN_EXPIRED") + assert.equal(deletes, beforeLegacy) + const beforePause = reads + duringRead = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(prepare()); assert.equal(reads, beforePause + 1) + await setConnectionStatus(access.connection.id, "active", actor) + for (const key of [publicKey, "", " "]) { + const binding = randomUUID() + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,'wg-peer-proof-user',105,'wg-proof',$2)", [binding, key]) + const before = deletes + await assert.rejects(prepare(), /привязкой/); await assert.rejects(prepare(true), /привязкой/) + assert.equal((await rest.inject({ method: "DELETE", url: "/api/wireguard/peers/105/*b1" })).statusCode, 409) + assert.equal(deletes, before) + await pool.query("DELETE FROM user_interface_bindings WHERE id=$1", [binding]) + } + const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protection, JSON.stringify({ kind: "wireguard", id: "*a1", serverId: 105, protection: "deny" })]) + const beforeProtected = deletes + await assert.rejects(prepare()); await assert.rejects(prepare(true)) + assert.equal((await rest.inject({ method: "DELETE", url: "/api/wireguard/peers/105/*b1" })).statusCode, 409) + assert.equal(deletes, beforeProtected) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + let lostOperationId = "" + for (const fault of ["normal", "ignored", "collateral", "lost"]) { + reset() + const plan = await prepare(fault === "normal") + await approvePlan(plan.planId, actor, true) + mode = fault + const before = deletes, key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + assert.equal(await wait(operation.id), fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial") + assert.equal(deletes, before + 1) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id); assert.equal(deletes, before + 1) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { state: string; automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesOriginalState: boolean }[] } + assert.equal(reconciliation.automaticRetry, false); assert.equal(reconciliation.results.length, 1) + assert.equal(reconciliation.results[0].matchesDesiredState, fault === "normal" || fault === "lost") + assert.equal(reconciliation.results[0].matchesOriginalState, fault === "ignored") + assert.equal(reconciliation.state, fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial") + assert.equal(deletes, before + 1, "Reconciliation must never reissue DELETE or infer an acknowledgement") + if (fault === "lost") lostOperationId = operation.id + if (fault === "normal") { assert.deepEqual(peers, [basePeers[1]]); assert.deepEqual(interfaces, baseInterfaces) } + } + const beforeReconcilePause = reads, deletesBeforeReconcilePause = deletes + duringRead = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(invokeMcpTool(principal, "mm_operation_reconcile", { operationId: lostOperationId })) + assert.equal(reads, beforeReconcilePause + 1); assert.equal(deletes, deletesBeforeReconcilePause) + await setConnectionStatus(access.connection.id, "active", actor) + const cancelled = new AbortController(), beforeCancelledRead = reads + duringRead = async () => cancelled.abort() + await assert.rejects(invokeMcpTool(principal, "mm_operation_reconcile", { operationId: lostOperationId }, cancelled.signal), (error: { code?: string }) => error.code === "READ_CANCELLED") + assert.equal(reads, beforeCancelledRead + 1); assert.equal(deletes, deletesBeforeReconcilePause) + reset() + const stale = await prepare(); await approvePlan(stale.planId, actor, true) + peers[1].rx = "9000" // Runtime counters alone do not invalidate configuration. + const beforeCounter = deletes, counterOp = await applyPlan(principal, stale.planId, randomUUID()) + assert.equal(await wait(counterOp.id), "applied"); assert.equal(deletes, beforeCounter + 1) + reset() + const lateBinding = await prepare(); await approvePlan(lateBinding.planId, actor, true) + const bindingId = randomUUID() + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,'wg-peer-proof-user',105,'wg-proof',$2)", [bindingId, publicKey]) + const beforeBinding = deletes, bindingOp = await applyPlan(principal, lateBinding.planId, randomUUID()) + assert.equal(await wait(bindingOp.id), "rejected"); assert.equal(deletes, beforeBinding) + await pool.query("DELETE FROM user_interface_bindings WHERE id=$1", [bindingId]) + reset() + const secretDrift = await prepare(); await approvePlan(secretDrift.planId, actor, true) + peers[1]["preshared-key"] = Buffer.alloc(32, 9).toString("base64") + const beforeDrift = deletes, driftOp = await applyPlan(principal, secretDrift.planId, randomUUID()) + assert.equal(await wait(driftOp.id), "rejected"); assert.equal(deletes, beforeDrift) + reset() + const aba = await prepare(); await approvePlan(aba.planId, actor, true) + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=105"); await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=105") + const beforeAba = deletes, abaOp = await applyPlan(principal, aba.planId, randomUUID()) + assert.equal(await wait(abaOp.id), "rejected"); assert.equal(deletes, beforeAba) + reset(); mode = "provider-error" + await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + const failedRest = await rest.inject({ method: "DELETE", url: "/api/wireguard/peers/105/*b1" }) + assert.equal(failedRest.statusCode, 502); assert.ok(!failedRest.body.includes("NEVER_")) + reset() + const restBefore = deletes, success = await rest.inject({ method: "DELETE", url: "/api/wireguard/peers/105/*b1" }) + assert.equal(success.statusCode, 200); assert.equal(success.json().verified, true); assert.equal(deletes, restBefore + 1) + assert.deepEqual(peers, [basePeers[1]]); assert.deepEqual(interfaces, baseInterfaces) + const recorded = JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId])).rows) + for (const value of [privateKey, psk, "NEVER_"]) assert.ok(!recorded.includes(value)) + console.log("✓ Shared WireGuard peer deletion REST/MCP/native alias: bindings/whole interface/parent protection, private opaque proof, counter tolerance, secret/endpoint ABA drift, exact one DELETE, ignored/collateral partial, lost unknown, revision receipt, privacy and no replay") + } finally { + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM mcp_protections WHERE object_ref->>'serverId'='105'") + await pool.query("DELETE FROM app_users WHERE id='wg-peer-proof-user'") + await pool.query("DELETE FROM servers WHERE id=105") + await rest.close(); router.closeAllConnections() + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/modules/mcp/wireguard-peer-delete-actions.ts b/backend/src/modules/mcp/wireguard-peer-delete-actions.ts new file mode 100644 index 00000000..bf2aedb4 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-peer-delete-actions.ts @@ -0,0 +1,53 @@ +import { z } from "zod" +import { hasPermission } from "../../lib/permissions.js" +import { pool } from "../../db/index.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { deleteWireguardPeer, readWireguardPeerDeletionState, wireguardPeerDeletionTarget, expectedWireguardPeerDeletionProof } from "../../services/wireguard-peer-delete.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const schema = z.object({ serverId: z.number().int().positive().safe(), peerId: z.string().regex(/^\*[a-z0-9]+$/i).max(100) }).strict() +export async function prepareWireguardPeerDeletion(serverId: number, peerId: string, context: ActionContext): Promise { + schema.parse({ serverId, peerId }) + const authorize = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Подготовка удаления peer отменена", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Право изменения сети отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) throw new McpError("RESET_REQUIRED", "Допуск изменился; подготовьте план заново", 409) + } + await authorize() + const { server } = await routerClient(serverId), state = await readWireguardPeerDeletionState(server, authorize), { peer, parent } = wireguardPeerDeletionTarget(state, peerId) + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(serverId), serverId }, { kind: "wireguard", id: parent[".id"], serverId }, { kind: "wireguard_peer", id: peerId, serverId }] + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(serverId)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел защит WireGuard", 409) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId }) + await authorize() + return { target: { scope: "servers", serverIds: [serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: 2, + before: { proof: state.proof }, + steps: [{ kind: "service", service: "network.wireguard.peers.delete", input: { serverId, peerId, interface: parent.name, publicKey: peer["public-key"], expectedProof: state.proof, expectedAfterProof: await expectedWireguardPeerDeletionProof(state, peerId), proofRevision: 1, beforeFingerprint: fingerprint({ proof: state.proof }) } }], + warnings: ["Удаляется только выбранный peer; родительский интерфейс, адреса и другие peers сохраняются. Используемая привязка пользователя должна быть изменена отдельным планом.", "Единственный DELETE не повторяется при потере ответа. Универсального обратного плана удаления нет; состояние ревизии приложения возвращается отдельно."] } +} + +export function registerWireguardPeerDeleteActions(register: typeof add) { + const current = async (input: Record) => ({ proof: (await readWireguardPeerDeletionState((await routerClient(Number(input.serverId))).server)).proof }) + registerMcpService("network.wireguard.peers.delete", { current, + execute: async input => deleteWireguardPeer((await routerClient(Number(input.serverId))).server, String(input.peerId), String(input.expectedProof), assertVerifiedConfigurationAccess), + verify: async (input, result) => { + const receipt = result as { verified?: boolean; afterProof?: string } | null + return receipt?.verified === true && (await current(input)).proof === receipt.afterProof + }, + reconcile: async (input, authorize) => { + if (input.proofRevision !== 1 || typeof input.expectedAfterProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedAfterProof) || typeof input.expectedProof !== "string" || !/^[a-f0-9]{64}$/.test(input.expectedProof)) throw new McpError("UNSUPPORTED_CAPABILITY", "В исходном плане нет совместимого desired proof; требуется ручная сверка", 409) + const { server } = await routerClient(Number(input.serverId)), state = await readWireguardPeerDeletionState(server, authorize) + await authorize() + return { matchesDesiredState: state.proof === input.expectedAfterProof, matchesOriginalState: state.proof === input.expectedProof } + }, + }) + register("network.wireguard.peers.delete", "mm_wireguard_peer_remove_plan", "Удалить peer WireGuard с проверкой привязок и сохранности конфигурации", "network", "mm:network:write", "write", "servers", schema, { + prepare: (input, context) => prepareWireguardPeerDeletion(Number(input.serverId), String(input.peerId), context), + }) +} diff --git a/backend/src/modules/mcp/wireguard-peer-update-acceptance.ts b/backend/src/modules/mcp/wireguard-peer-update-acceptance.ts new file mode 100644 index 00000000..3a814ff0 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-peer-update-acceptance.ts @@ -0,0 +1,216 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import wireguardRoutes from "../../routes/wireguard.js" +import { wireguardPeerUpdateSchema, wireguardPeerNativeUpdate } from "../../services/wireguard-peer-update.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "./store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "./runtime.js" + +export async function acceptWireguardPeerUpdate(actor: AuthUser) { + const publicKey = Buffer.alloc(32, 11).toString("base64"), otherKey = Buffer.alloc(32, 12).toString("base64"), newKey = Buffer.alloc(32, 13).toString("base64") + const privateKey = Buffer.alloc(32, 14).toString("base64"), psk = Buffer.alloc(32, 15).toString("base64") + const baseInterfaces = [{ ".id": "*a1", name: "wg-update", disabled: "false", "private-key": privateKey, "listen-port": "13231", mtu: "1420", comment: "" }] + const basePeers: Record[] = [{ ".id": "*b1", interface: "wg-update", "public-key": publicKey, "preshared-key": psk, "allowed-address": "192.0.2.2/32", disabled: "false", comment: "before", opaque: "NEVER_UPDATE_OPAQUE" }, { ".id": "*b2", interface: "wg-update", "public-key": otherKey, "allowed-address": "192.0.2.3/32", disabled: "false", comment: "other", rx: "0" }] + let interfaces = structuredClone(baseInterfaces), peers = structuredClone(basePeers), mode = "normal", patches = 0, reads = 0 + let duringRead: (() => Promise) | undefined + let duringAuxRead: (() => Promise) | undefined, auxReads = 0 + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json") + if (req.method === "POST" && req.url?.startsWith("/rest/interface/wireguard/peers/")) { + let body = "" + req.on("data", chunk => { body += chunk }); req.on("end", async () => { + auxReads++ + if (duringAuxRead) { + const hook = duringAuxRead; duringAuxRead = undefined + try { await hook() } catch { res.statusCode = 503; res.end("{}"); return } + } + const data = JSON.parse(body) + if (req.url?.endsWith("/get")) { + if (mode === "unset-get-unknown") { res.end('{"ret":"NEVER_UNKNOWN_KEEPALIVE"}'); return } + const value = peers.find(row => row[".id"] === data[".id"])?.[data["value-name"]] + res.end(JSON.stringify(value === undefined ? [] : { ret: value })); return + } + if (req.url?.endsWith("/print")) { + const negative = data[".query"]?.includes("#!") + if (mode === "unset-partial") { res.end("[]"); return } + if (mode === "unset-overlap") { res.end(JSON.stringify(peers.map(row => ({ ".id": row[".id"] })))); return } + if (mode === "unset-foreign") { res.end('[{".id":"*fff"}]'); return } + res.end(JSON.stringify(peers.filter(row => (row["persistent-keepalive"] !== undefined) !== negative).map(row => ({ ".id": row[".id"] })))); return + } + res.statusCode = 400; res.end("{}") + }); return + } + if (req.method === "GET") { + reads++ + const respond = () => res.end(JSON.stringify(req.url === "/rest/interface/wireguard" ? interfaces : req.url === "/rest/interface/wireguard/peers" ? peers : [])) + if (duringRead) { const hook = duringRead; duringRead = undefined; void hook().then(respond, () => { res.statusCode = 503; res.end("{}") }); return } + if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_UPDATE_PROVIDER_PASSWORD"}'); return } + respond(); return + } + if (req.method === "PATCH" && req.url === "/rest/interface/wireguard/peers/*b1") { + let body = "" + req.on("data", chunk => { body += chunk }); req.on("end", () => { + patches++ + const data = JSON.parse(body) as Record + if (mode !== "ignored") { + Object.assign(peers[0], data) + if (data.disabled !== undefined) peers[0].disabled = data.disabled === "true" ? "yes" : "no" + if (data["persistent-keepalive"] === "60") peers[0]["persistent-keepalive"] = "1m" + if (data["persistent-keepalive"] === "0") delete peers[0]["persistent-keepalive"] + for (const key of ["comment", "name", "endpoint-address", "client-address", "client-dns", "client-endpoint"]) if (data[key] === "") delete peers[0][key] + } + if (mode === "collateral") peers[1].comment = "foreign changed" + if (mode === "lost") { req.socket.destroy(); return } + res.end("{}") + }); return + } + res.statusCode = 400; res.end("{}") + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + let connectionId: string | undefined + const reset = () => { mode = "normal"; interfaces = structuredClone(baseInterfaces); peers = structuredClone(basePeers) } + const wait = async (id: string) => { + for (let attempt = 0; attempt < 250; attempt++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0].state + if (!["pending", "running"].includes(state)) return state + await delay(25) + } + throw Error("WireGuard update remains live; never replay") + } + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(108,'wg-peer-update','127.0.0.1',$1,false,'NEVER_UPDATE_ENDPOINT_PASSWORD')", [(router.address() as { port: number }).port]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('wg-update-user','wg-update-user','Update fixture')") + await rest.register(wireguardRoutes, { prefix: "/api" }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "WireGuard peer update", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [108], operationIds: ["network.wireguard.peers.update", "ros.wireguard_peer.update", "operations.reconcile", "operations.recover"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const prepare = async (patch: Record = { comment: "after" }, native = false) => { + const plan = await invokeMcpTool(principal, native ? "mm_wireguard_peer_update_plan" : "mm_wireguard_peer_edit_plan", native ? { serverId: 108, id: "*b1", fields: patch } : { serverId: 108, peerId: "*b1", patch }) as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval") + for (const value of [privateKey, psk, "NEVER_"]) assert.ok(!JSON.stringify(plan).includes(value)) + return plan + } + const beforeInvalid = reads + for (const patch of [{}, { privateKey }, { endpointPort: 65536 }, { allowedAddresses: ["bad"] }, { allowedAddresses: ["192.0.2.1/24", "192.0.2.0/24"] }, { publicKey: "bad" }]) { + assert.equal(wireguardPeerUpdateSchema.safeParse(patch).success, false) + await assert.rejects(prepare(patch)) + assert.equal((await rest.inject({ method: "PATCH", url: "/api/wireguard/peers/108/*b1", payload: patch })).statusCode, 400) + } + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_peer_edit_plan", { serverId: 1, peerId: "*b1", patch: { comment: "after" } })) + await assert.rejects(prepare({ interface: "another" }, true)) + assert.equal(reads, beforeInvalid) + for (const fault of ["unset-get-unknown", "unset-partial", "unset-overlap", "unset-foreign"]) { + mode = fault + const before = patches + await assert.rejects(prepare(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + assert.equal(patches, before) + } + reset() + assert.equal(wireguardPeerNativeUpdate({ "persistent-keepalive": "1m", "endpoint-port": "123", disabled: "false" }).persistentKeepalive, 60) + assert.throws(() => wireguardPeerNativeUpdate({ "persistent-keepalive": "unknown" })) + const legacy = await prepare({ comment: "after" }, true); await approvePlan(legacy.planId, actor, true) + await pool.query("UPDATE mcp_plans SET operation_version=1 WHERE id=$1", [legacy.planId]) + await assert.rejects(applyPlan(principal, legacy.planId, randomUUID()), (error: { code?: string }) => error.code === "PLAN_EXPIRED") + assert.equal(patches, 0) + await assert.rejects(prepare({ publicKey: otherKey })) + const bindingId = randomUUID() + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,'wg-update-user',108,'wg-update',$2)", [bindingId, publicKey]) + const boundPlan = await prepare({ comment: "allowed for bound peer" }); await approvePlan(boundPlan.planId, actor, true) + assert.equal(await wait((await applyPlan(principal, boundPlan.planId, randomUUID())).id), "applied") + await assert.rejects(prepare({ publicKey: newKey }), /привязкой/) + assert.equal((await rest.inject({ method: "PATCH", url: "/api/wireguard/peers/108/*b1", payload: { publicKey: newKey } })).statusCode, 409) + await pool.query("DELETE FROM user_interface_bindings WHERE id=$1", [bindingId]) + reset() + const beforeProtection = patches + const protectionId = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [protectionId, JSON.stringify({ kind: "wireguard", id: "*a1", serverId: 108, protection: "deny" })]) + await assert.rejects(prepare()); await assert.rejects(prepare({ comment: "after" }, true)) + assert.equal((await rest.inject({ method: "PATCH", url: "/api/wireguard/peers/108/*b1", payload: { comment: "after" } })).statusCode, 409) + assert.equal(patches, beforeProtection) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) + peers[0].comment = "MM traffic-flow collector" + await assert.rejects(prepare(), /NetFlow/); await assert.rejects(prepare({ comment: "after" }, true), /NetFlow/) + reset() + const lateBindingPlan = await prepare(); await approvePlan(lateBindingPlan.planId, actor, true) + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,'wg-update-user',108,'wg-update',$2)", [bindingId, publicKey]) + const beforeLateBinding = patches + assert.equal(await wait((await applyPlan(principal, lateBindingPlan.planId, randomUUID())).id), "rejected") + assert.equal(patches, beforeLateBinding) + await pool.query("DELETE FROM user_interface_bindings WHERE id=$1", [bindingId]) + const counters = await prepare(); await approvePlan(counters.planId, actor, true) + peers[1].rx = "9000" + assert.equal(await wait((await applyPlan(principal, counters.planId, randomUUID())).id), "applied") + reset() + for (const fault of ["normal", "ignored", "collateral", "lost"]) { + reset() + const plan = await prepare({ comment: "after" }, fault === "normal"); await approvePlan(plan.planId, actor, true) + mode = fault + const before: number = patches, key = randomUUID(), operation = await applyPlan(principal, plan.planId, key) + const expected = fault === "normal" ? "applied" : fault === "lost" ? "unknown" : "partial" + assert.equal(await wait(operation.id), expected); assert.equal(patches, before + 1) + assert.equal((await applyPlan(principal, plan.planId, key)).id, operation.id); assert.equal(patches, before + 1) + const reconciliation = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: operation.id }) as { state: string; automaticRetry: boolean; results: { matchesDesiredState: boolean; matchesOriginalState: boolean }[] } + assert.equal(reconciliation.state, expected); assert.equal(reconciliation.automaticRetry, false) + assert.equal(reconciliation.results[0].matchesDesiredState, fault === "normal" || fault === "lost") + assert.equal(reconciliation.results[0].matchesOriginalState, fault === "ignored") + assert.equal(patches, before + 1) + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: operation.id }), (error: { code?: string }) => error.code === "UNSUPPORTED_CAPABILITY") + } + reset() + const drift = await prepare(); await approvePlan(drift.planId, actor, true) + peers[1]["preshared-key"] = "NEVER_LATE_PSK" + const beforeDrift = patches + assert.equal(await wait((await applyPlan(principal, drift.planId, randomUUID())).id), "rejected"); assert.equal(patches, beforeDrift) + reset() + const aba = await prepare(); await approvePlan(aba.planId, actor, true) + await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=108"); await pool.query("UPDATE servers SET verify_ssl=NOT verify_ssl WHERE id=108") + assert.equal(await wait((await applyPlan(principal, aba.planId, randomUUID())).id), "rejected"); assert.equal(patches, beforeDrift) + const pausedRead = reads + duringRead = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(prepare()); assert.equal(reads, pausedRead + 1) + await setConnectionStatus(access.connection.id, "active", actor) + reset() + const beforeAuxPause = auxReads, beforeAuxPauseGets = reads + duringAuxRead = () => setConnectionStatus(access.connection.id, "paused", actor) + await assert.rejects(prepare()); assert.equal(auxReads, beforeAuxPause + 1); assert.equal(reads, beforeAuxPauseGets + 2) + await setConnectionStatus(access.connection.id, "active", actor) + const auxCancelled = new AbortController(), beforeAuxCancel = auxReads + duringAuxRead = async () => auxCancelled.abort() + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_peer_edit_plan", { serverId: 108, peerId: "*b1", patch: { comment: "after" } }, auxCancelled.signal), (error: { code?: string }) => error.code === "READ_CANCELLED") + assert.equal(auxReads, beforeAuxCancel + 1) + const cancelled = new AbortController(), cancelledRead = reads + duringRead = async () => cancelled.abort() + await assert.rejects(invokeMcpTool(principal, "mm_wireguard_peer_edit_plan", { serverId: 108, peerId: "*b1", patch: { comment: "after" } }, cancelled.signal), (error: { code?: string }) => error.code === "READ_CANCELLED") + assert.equal(reads, cancelledRead + 1) + reset(); mode = "provider-error" + await assert.rejects(prepare(), (error: Error) => !error.message.includes("NEVER_")) + const provider = await rest.inject({ method: "PATCH", url: "/api/wireguard/peers/108/*b1", payload: { comment: "after" } }) + assert.equal(provider.statusCode, 502); assert.ok(!provider.body.includes("NEVER_")) + reset() + const fullPatch = { publicKey: newKey, allowedAddresses: ["2001:db8::abcd/64", "192.0.2.89/24"], endpointAddress: "vpn.example.invalid", endpointPort: 13232, persistentKeepalive: 60, comment: "", name: "updated-peer", disabled: true, clientAddress: "198.51.100.2/32", clientDns: "192.0.2.53", clientEndpoint: "client.example.invalid" } + const beforeRest = patches, restResult = await rest.inject({ method: "PATCH", url: "/api/wireguard/peers/108/*b1", payload: fullPatch }) + assert.equal(restResult.statusCode, 200, restResult.body); assert.equal(restResult.json().verified, true); assert.equal(restResult.json().revisionRecorded, true) + assert.equal(restResult.json().afterProof, undefined); assert.equal(patches, beforeRest + 1) + assert.equal(peers[0]["public-key"], newKey); assert.equal(peers[0]["persistent-keepalive"], "1m"); assert.equal(peers[0].disabled, "yes") + assert.equal(peers[0]["allowed-address"], "192.0.2.0/24,2001:db8::/64"); assert.equal(peers[0].comment, undefined) + assert.equal(peers[0]["preshared-key"], psk); assert.deepEqual(peers[1], basePeers[1]); assert.deepEqual(interfaces, baseInterfaces) + const clear = await prepare({ persistentKeepalive: 0, endpointAddress: "", endpointPort: 0 }); await approvePlan(clear.planId, actor, true) + assert.equal(await wait((await applyPlan(principal, clear.planId, randomUUID())).id), "applied") + assert.equal(peers[0]["persistent-keepalive"], undefined) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_requests WHERE connection_id=$1 AND (result::text LIKE '%NEVER_%' OR result::text LIKE $2 OR result::text LIKE $3)", [connectionId, "%" + psk + "%", "%" + privateKey + "%"])).rows[0].n, 0) + console.log("✓ WireGuard peer PATCH shares REST/MCP/native service: strict fields, bound identity/NetFlow/protections, private source proof, aliases/version fence, canonical read-back, preserved opaque/foreign peers, partial/lost/idempotency and read-only reconciliation; one PATCH and no forwarding claim") + } finally { + await rest.close(); await new Promise(resolve => router.close(() => resolve())) + if (connectionId) await setConnectionStatus(connectionId, "paused", actor) + await pool.query("DELETE FROM user_interface_bindings WHERE user_id='wg-update-user'") + await pool.query("DELETE FROM app_users WHERE id='wg-update-user'") + await pool.query("DELETE FROM servers WHERE id=108") + } +} diff --git a/backend/src/modules/mcp/wireguard-peer-update-actions.ts b/backend/src/modules/mcp/wireguard-peer-update-actions.ts new file mode 100644 index 00000000..94a41d61 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-peer-update-actions.ts @@ -0,0 +1,60 @@ +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { wireguardPeerDeletionTarget } from "../../services/wireguard-peer-delete.js" +import { wireguardPeerUpdateSchema, readWireguardPeerUpdateState, updateWireguardPeer, projectWireguardPeerUpdate, wireguardPeerUpdateProof } from "../../services/wireguard-peer-update.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { fingerprint, McpError } from "./security.js" +import { registerMcpService } from "./service-registry.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" +import type { add } from "./actions.js" + +const schema = z.object({ serverId: z.number().int().positive().safe(), peerId: z.string().regex(/^\*[a-z0-9]+$/i).max(100), patch: wireguardPeerUpdateSchema }).strict() +export async function prepareWireguardPeerUpdate(serverId: number, peerId: string, patch: unknown, context: ActionContext): Promise { + const parsed = schema.parse({ serverId, peerId, patch }) + const authorize = async () => { + if (context.signal?.aborted) throw new McpError("READ_CANCELLED", "Подготовка изменения peer отменена", 409) + const current = await effectiveMcpConnection(context.principal) + if (!hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Право изменения сети отозвано", 403) + if (fingerprint(current.policy) !== fingerprint(context.connection.policy) || fingerprint(current.owner_permissions) !== fingerprint(context.connection.owner_permissions)) throw new McpError("RESET_REQUIRED", "Допуск изменился; подготовьте план заново", 409) + } + await authorize() + const { server } = await routerClient(serverId), state = await readWireguardPeerUpdateState(server, peerId, authorize) + const projected = projectWireguardPeerUpdate(state, peerId, parsed.patch), { parent } = wireguardPeerDeletionTarget(state, peerId, true) + const objects: ChangeEvidence["objects"] = [{ kind: "server", id: String(serverId), serverId }, { kind: "wireguard", id: parent[".id"], serverId }, { kind: "wireguard_peer", id: peerId, serverId }] + const protections = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) LIMIT 1001", [String(serverId)]) + if (protections.rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел защит WireGuard", 409) + for (const { object_ref: ref } of protections.rows) objects.push({ kind: ref.kind, id: ref.id, serverId }) + await authorize() + return { target: { scope: "servers", serverIds: [serverId] }, objects, snapshots: [], requiresHuman: true, mutationCost: 2, + before: { proof: state.proof, semanticProof: await wireguardPeerUpdateProof(state) }, + steps: [{ kind: "service", service: "network.wireguard.peers.update", input: { serverId, peerId, patch: parsed.patch, expectedProof: state.proof, expectedOriginalProof: await wireguardPeerUpdateProof(state), expectedAfterProof: await wireguardPeerUpdateProof(projected), proofRevision: 1, beforeFingerprint: fingerprint({ proof: state.proof, semanticProof: await wireguardPeerUpdateProof(state) }) } }], + warnings: ["Изменение endpoint, allowed addresses и disabled может прервать трафик выбранного peer; handshake и forwarding требуют отдельной проверки.", "Отправляется единственный PATCH. При потере ответа запись не повторяется; используйте сверку. Универсального обратного плана сервиса нет."] } +} +export function registerWireguardPeerUpdateActions(register: typeof add) { + const current = async (input: Record) => { + const state = await readWireguardPeerUpdateState((await routerClient(Number(input.serverId))).server, String(input.peerId)) + return { proof: state.proof, semanticProof: await wireguardPeerUpdateProof(state) } + } + registerMcpService("network.wireguard.peers.update", { current, + execute: async input => updateWireguardPeer((await routerClient(Number(input.serverId))).server, String(input.peerId), input.patch, String(input.expectedProof), assertVerifiedConfigurationAccess, { expectedOriginalProof: String(input.expectedOriginalProof) }), + verify: async (input, result) => { + const receipt = result as { verified?: boolean; afterProof?: string; afterSemanticProof?: string } | null + if (receipt?.verified !== true) return false + const state = await current(input) + return state.proof === receipt.afterProof && state.semanticProof === receipt.afterSemanticProof + }, + reconcile: async (input, authorize) => { + if (input.proofRevision !== 1 || [input.expectedAfterProof, input.expectedOriginalProof].some(value => typeof value !== "string" || !/^[a-f0-9]{64}$/.test(value))) throw new McpError("UNSUPPORTED_CAPABILITY", "Нет совместимого proof для сверки изменения peer", 409) + const state = await readWireguardPeerUpdateState((await routerClient(Number(input.serverId))).server, String(input.peerId), authorize) + const proof = await wireguardPeerUpdateProof(state) + await authorize() + return { matchesDesiredState: proof === input.expectedAfterProof, matchesOriginalState: proof === input.expectedOriginalProof } + }, + }) + register("network.wireguard.peers.update", "mm_wireguard_peer_edit_plan", "Изменить peer WireGuard с проверкой зависимостей и сохранности конфигурации", "network", "mm:network:write", "write", "servers", schema, { + prepare: (input, context) => prepareWireguardPeerUpdate(Number(input.serverId), String(input.peerId), input.patch, context), + }) +} diff --git a/backend/src/modules/mcp/wireguard-revision-acceptance.ts b/backend/src/modules/mcp/wireguard-revision-acceptance.ts new file mode 100644 index 00000000..832d3610 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-revision-acceptance.ts @@ -0,0 +1,125 @@ +import assert from "node:assert/strict" +import { installLoopbackFixtureAgent } from "../../test/loopback-http-agent.js" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler,validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../../db/index.js" +import type { AuthUser } from "../../lib/permissions.js" +import { wireguardInterfaceDependencyPaths } from "../../services/wireguard-interface-delete.js" +import { wireguardPublicKey } from "../../services/wireguard-interface-create.js" +import { canonicalWireguardSnapshot } from "../../services/entity-snapshots.js" +import { fingerprintPayload } from "../../services/config-revisions.js" +import wireguardRoutes from "../../routes/wireguard.js" +import { createMcpConnection,authenticateMcpToken,setConnectionStatus } from "./store.js" +import { invokeMcpTool,approvePlan,applyPlan } from "./runtime.js" +import { WIREGUARD_REVISION_RESTORE_SERVICE as service } from "./wireguard-revision-actions.js" + +/** Run only from the explicitly authorized, serial, isolated PostgreSQL harness. + * Owns server 149, one connection, and a loopback RouterOS double. No live router. */ +export async function acceptWireguardRevisionRestore(actor:AuthUser){ + type Rows=Record[]> + const serverId=149,key=Buffer.alloc(32,91).toString("base64"),peerKey=Buffer.alloc(32,92).toString("base64") + const initial:Rows=Object.fromEntries([...wireguardInterfaceDependencyPaths,"/interface/wireguard","/interface/wireguard/peers","/ip/address","/ipv6/address"].map(path=>[path,[]])) + initial["/interface/wireguard"]=[{".id":"*a",name:"wg-revision",disabled:"false",mtu:"1420","listen-port":"13231","public-key":wireguardPublicKey(key),"private-key":key,opaque:"NEVER_RESTORE_INTERFACE_OPAQUE"}] + initial["/interface"]=[{".id":"*a",name:"wg-revision",type:"wg",disabled:"false"},{".id":"*e",name:"ether1",type:"ether",comment:"NEVER_RESTORE_FOREIGN"}] + initial["/interface/wireguard/peers"]=[{".id":"*b",interface:"wg-revision","public-key":peerKey,"allowed-address":"192.0.2.2/32",disabled:"false","persistent-keepalive":"25s","endpoint-address":"old.example","endpoint-port":"54321",comment:"old",name:"old","client-address":"old","client-dns":"old","client-endpoint":"old","preshared-key":"NEVER_RESTORE_PSK"}] + initial["/ip/address"]=[{".id":"*c",interface:"wg-revision",address:"192.0.2.1/24",disabled:"false",dynamic:"false",comment:"old"}] + const payload=()=>canonicalWireguardSnapshot({interfaces:[{name:"wg-revision",privateKey:"*****",listenPort:13232,mtu:1380,comment:"",disabled:false,address:"2001:db8:149::1/64",innerAddresses:[{address:"2001:db8:149::1/64",disabled:false,advertise:false,noDad:true,comment:""}],peers:[{publicKey:peerKey,allowedAddresses:["2001:db8:149::2/128"],endpointAddress:"",endpointPort:"",persistentKeepalive:null,comment:"",name:"",disabled:false,privateKey:"*****",clientAddress:"",clientDns:"",clientEndpoint:""}]}]}) + let rows=structuredClone(initial),commands:string[]=[],mode="normal",nextId=100,reads=0,afterWrite:(()=>Promise)|undefined + 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} + let raw="";for await(const chunk of req)raw+=chunk + const body=raw?JSON.parse(raw)as Record:{},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} + commands.push(req.method+" "+path) + const id=path.slice(base.length+1) + if(mode!=="ignored"){ + if(req.method==="DELETE"){rows[base]=rows[base].filter(row=>row[".id"]!==id);if(base==="/interface/wireguard")rows["/interface"]=rows["/interface"].filter(row=>row[".id"]!==id)} + else if(req.method==="PATCH"){ + const selected=rows[base].find(row=>row[".id"]===id)!;Object.assign(selected,body) + if(base==="/interface/wireguard"){if(body["private-key"])selected["public-key"]=wireguardPublicKey(body["private-key"]);const generic=rows["/interface"].find(row=>row[".id"]===id)!;for(const k of Object.keys(body))if(generic[k]!==undefined)generic[k]=selected[k]} + }else{ + const row:Record={".id":"*"+(++nextId).toString(16),dynamic:"false",...body} + rows[base].push(row) + if(base==="/interface/wireguard"){row["public-key"]=wireguardPublicKey(body["private-key"]);rows["/interface"].push({".id":row[".id"],name:row.name,type:"wg",disabled:row.disabled})} + } + } + if(mode==="collateral")rows["/interface/wireguard/peers"][0]["preshared-key"]="NEVER_RESTORE_CHANGED_PSK" + if(afterWrite){const hook=afterWrite;afterWrite=undefined;await hook()} + if(mode==="lost"){req.socket.destroy();return} + res.end("{}") + }) + await new Promise(resolve=>router.listen(0,"127.0.0.1",resolve)) + const closeFixtureAgent=installLoopbackFixtureAgent(router) + const rest=Fastify();rest.setValidatorCompiler(validatorCompiler);rest.setSerializerCompiler(serializerCompiler) + let connectionId:string|undefined,inserted=false + const revisionIds:string[]=[] + const revision=async(value:unknown,section="wireguard",sid=serverId)=>{const id=randomUUID();revisionIds.push(id);await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload) VALUES($1,$2,$3,'manual',$4,$5::jsonb)",[id,sid,section,fingerprintPayload(value),JSON.stringify(value)]);return id} + const wait=async(id:string)=>{for(let attempt=0;attempt<3000;attempt++){const row=(await pool.query("SELECT state FROM mcp_operations WHERE id=$1",[id])).rows[0];if(row&&!['pending','running'].includes(row.state))return String(row.state);await delay(25)}throw Error("WireGuard revision remains live; never replay")} + try{ + assert.equal((await pool.query("SELECT id FROM servers WHERE id=$1",[serverId])).rows.length,0,"WireGuard revision fixture ID must be unused") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'wg-revision','127.0.0.1',$2,false,'NEVER_RESTORE_ENDPOINT')",[serverId,(router.address()as{port:number}).port]);inserted=true + await rest.register(wireguardRoutes,{prefix:"/api"}) + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"WireGuard revision",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[serverId],operationIds:[service,"operations.reconcile"],allowGlobal:false},limits:{callsPerMinute:240,mutationsPerHour:1000}}),actor) + connectionId=issued.connection.id;const principal=await authenticateMcpToken(issued.token) + const prepare=async(revisionId:string)=>{ + const plan=await invokeMcpTool(principal,"mm_wireguard_revision_restore_plan",{serverId,revisionId})as{planId:string;state:string} + assert.equal(plan.state,"awaiting-approval");for(const hidden of [key,peerKey,"NEVER_RESTORE_"])assert.ok(!JSON.stringify(plan).includes(hidden));return plan + } + const run=async(planId:string)=>{await approvePlan(planId,actor,true);const idempotency=randomUUID(),op=await applyPlan(principal,planId,idempotency);return{op,idempotency,state:await wait(op.id)}} + for(const bad of [null,{},[],{interfaces:[{}]}]){ + const id=await revision(bad),before=reads;await assert.rejects(prepare(id));assert.equal(reads,before) + assert.ok((await rest.inject({method:"POST",url:`/api/wireguard/revisions/${id}/restore`,payload:{serverId}})).statusCode>=400);assert.equal(commands.length,0) + } + const selected=await revision(payload()) + for(const fault of ["normal","ignored","collateral","lost"]){ + reset();const plan=await prepare(selected);mode=fault;const outcome=await run(plan.planId) + if(outcome.state!==(fault==="normal"?"applied":fault==="lost"?"unknown":"partial")){ + const operation=(await pool.query("SELECT state,error_code,result FROM mcp_operations WHERE id=$1",[outcome.op.id])).rows[0] + console.log("WireGuard revision sanitized diagnostic",JSON.stringify({fault,operation,commands,interfaces:rows["/interface/wireguard"].map(row=>({id:row[".id"],name:row.name,disabled:row.disabled,mtu:row.mtu,listenPort:row["listen-port"]})),generic:rows["/interface"].map(row=>({id:row[".id"],name:row.name,disabled:row.disabled})),addresses:{v4:rows["/ip/address"].length,v6:rows["/ipv6/address"].length},peers:rows["/interface/wireguard/peers"].map(row=>({id:row[".id"],disabled:row.disabled,endpointConfigured:!!row["endpoint-address"],keepalive:row["persistent-keepalive"],opaquePskPreserved:row["preshared-key"]==="NEVER_RESTORE_PSK"}))})) + } + assert.equal(outcome.state,fault==="normal"?"applied":fault==="lost"?"unknown":"partial",fault) + assert.equal((await applyPlan(principal,plan.planId,outcome.idempotency)).id,outcome.op.id) + const count=commands.length,reconcile=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:outcome.op.id})as{automaticRetry:boolean;results:{matchesOriginalState:boolean;matchesDesiredState:boolean}[]} + assert.equal(reconcile.automaticRetry,false);assert.equal(reconcile.results[0].matchesDesiredState,fault==="normal");assert.equal(commands.length,count) + if(fault!=="normal"){ + assert.equal(count,1) + const stored=(await pool.query("SELECT result FROM mcp_operations WHERE id=$1",[outcome.op.id])).rows[0].result + assert.equal(stored.confirmedWrites,0,"Top-level service step did not return") + assert.equal(stored.nativeProgress.acknowledgedWrites,fault==="lost"?0:1) + assert.equal(stored.nativeProgress.verifiedCommands,0) + assert.ok(Number.isSafeInteger(stored.nativeProgress.commandCount)&&stored.nativeProgress.commandCount>=1) + assert.equal(stored.nativeProgress.automaticReplay,false) + } + if(fault==="normal"){ + assert.equal(rows["/interface/wireguard"][0]["private-key"],key);assert.equal(rows["/interface/wireguard/peers"][0]["preshared-key"],"NEVER_RESTORE_PSK");assert.equal(rows["/interface/wireguard/peers"][0]["endpoint-address"],"");assert.equal(rows["/ip/address"].length,0);assert.equal(rows["/ipv6/address"][0]["no-dad"],"true") + const step=(await pool.query("SELECT intended_state FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0",[outcome.op.id])).rows[0] + await pool.query("UPDATE mcp_operation_steps SET intended_state=jsonb_set(intended_state,'{input,receiptId}',to_jsonb($2::text)) WHERE operation_id=$1 AND step_index=0",[outcome.op.id,randomUUID()]) + assert.equal((await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:outcome.op.id})as{results:{matchesDesiredState:boolean}[]}).results[0].matchesDesiredState,false) + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1 AND step_index=0",[outcome.op.id,JSON.stringify(step.intended_state)]) + } + } + reset();rows["/interface/vlan"].push({".id":"*d",interface:"wg-revision"});await assert.rejects(prepare(selected));assert.equal(commands.length,0) + reset();const history=await prepare(selected) + await pool.query("DELETE FROM config_revisions WHERE server_id=$1 AND source='rollback'",[serverId]) + await pool.query("CREATE FUNCTION mcp_wg_revision_history_fixture() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.server_id=149 AND NEW.source='rollback' THEN RAISE EXCEPTION 'fixture history unavailable'; END IF; RETURN NEW; END $$") + try{ + await pool.query("CREATE TRIGGER mcp_wg_revision_history_fixture BEFORE INSERT ON config_revisions FOR EACH ROW EXECUTE FUNCTION mcp_wg_revision_history_fixture()") + assert.equal((await run(history.planId)).state,"partial");assert.ok(commands.length>1) + }finally{await pool.query("DROP TRIGGER IF EXISTS mcp_wg_revision_history_fixture ON config_revisions");await pool.query("DROP FUNCTION IF EXISTS mcp_wg_revision_history_fixture()")} + reset();const revoked=await prepare(selected);afterWrite=()=>setConnectionStatus(connectionId!,"revoked",actor);assert.equal((await run(revoked.planId)).state,"unknown");assert.equal(commands.length,1) + console.log("WireGuard revision SQL/loopback: strict REST/MCP revisions, clears/IPv6/private conservation, ignored/collateral/lost ACK, no replay/checkpoint tamper and post-ACK revocation passed") + }finally{ + closeFixtureAgent() + await rest.close();await new Promise((resolve,reject)=>router.close(error=>error?reject(error):resolve())) + if(connectionId)await setConnectionStatus(connectionId,"revoked",actor) + if(revisionIds.length)await pool.query("DELETE FROM config_revisions WHERE id=ANY($1::text[])",[revisionIds]) + if(inserted){await pool.query("DELETE FROM config_revisions WHERE server_id=$1",[serverId]);await pool.query("DELETE FROM servers WHERE id=$1",[serverId])} + } +} diff --git a/backend/src/modules/mcp/wireguard-revision-actions.ts b/backend/src/modules/mcp/wireguard-revision-actions.ts new file mode 100644 index 00000000..0deaae71 --- /dev/null +++ b/backend/src/modules/mcp/wireguard-revision-actions.ts @@ -0,0 +1,77 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../../db/index.js" +import { hasPermission } from "../../lib/permissions.js" +import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" +import { readWireguardRestoreRevision, readWireguardRestoreState, materializeWireguardRestore, validateWireguardRestoreSnapshot, planWireguardRevisionRestore, restoreWireguardRevision } from "../../services/wireguard-revision-restore.js" +import { wireguardCreateProtectionProof } from "../../services/wireguard-interface-create.js" +import { effectiveMcpConnection } from "./store.js" +import { routerClient } from "./resources.js" +import { registerMcpService } from "./service-registry.js" +import { encryptSecret, decryptSecret, fingerprint, McpError, redact } from "./security.js" +import type { add } from "./actions.js" +import type { ActionContext, ChangeEvidence } from "./action-types.js" + +export const WIREGUARD_REVISION_RESTORE_SERVICE="config.wireguard.revisions.restore" +const service=WIREGUARD_REVISION_RESTORE_SERVICE +const principalSchema=z.object({connectionId:z.string().uuid(),credentialId:z.string().uuid(),credentialExpiresAt:z.string()}).strict() +const fail=(message:string,code="STATE_CONFLICT")=>new McpError(code,message,409) +function request(input:Record,context:ActionContext){return {...input,principal:context.principal,ownerId:context.connection.owner_id,ownerPermissionsProof:fingerprint(context.connection.owner_permissions),policyProof:fingerprint(context.connection.policy)}} +async function authorize(input:Record,signal?:AbortSignal,verified=false){ + signal?.throwIfAborted();if(verified)await assertVerifiedConfigurationAccess() + const current=await effectiveMcpConnection(principalSchema.parse(input.principal)),serverId=Number(input.serverId) + if(current.owner_id!==input.ownerId||current.policy.mode==="observer"||!hasPermission(current.policy.permissions,"mm:network:write")||!hasPermission(current.owner_permissions,"mm:network:write"))throw fail("Право восстановления WireGuard отозвано","PERMISSION_DENIED") + if(!current.policy.scope.serverIds.includes(serverId)||!current.policy.scope.operationIds.includes(service))throw fail("WireGuard restore вне разрешённой области","SERVER_DENIED") + if(fingerprint(current.policy)!==input.policyProof||fingerprint(current.owner_permissions)!==input.ownerPermissionsProof)throw fail("Допуск WireGuard restore изменился","RESET_REQUIRED") + signal?.throwIfAborted() +} +async function readState(input:Record,signal?:AbortSignal,verified=false){ + await authorize(input,signal,verified) + const protectionProof=await wireguardCreateProtectionProof(Number(input.serverId)) + const access=async()=>{await authorize(input,signal,verified);if(await wireguardCreateProtectionProof(Number(input.serverId))!==protectionProof)throw fail("Защиты WireGuard изменились")} + await access();const {server}=await routerClient(Number(input.serverId)),state=await readWireguardRestoreState(server,access) + await access();return {server,state,access,stateProof:state.proof,protectionProof} +} +async function read(input:Record,signal?:AbortSignal,verified=false){ + await authorize(input,signal,verified) + const revision=await readWireguardRestoreRevision(Number(input.serverId),String(input.revisionId),()=>authorize(input,signal,verified)),source=await readState(input,signal,verified) + return {...source,revision,publicState:{stateProof:source.stateProof,revisionProof:revision.revisionProof,protectionProof:source.protectionProof}} +} +export function registerWireguardRevisionActions(register:typeof add){ + registerMcpService(service,{ + current:async input=>(await read(input)).publicState, + execute:async input=>{ + const source=await read(input,undefined,true) + if(source.stateProof!==input.beforeProof||source.revision.revisionProof!==input.revisionProof||source.protectionProof!==input.protectionProof)throw fail("WireGuard, защиты или ревизия изменились после подтверждения") + try{ + const desired=validateWireguardRestoreSnapshot(JSON.parse(await decryptSecret(z.object({ciphertext:z.string()}).strict().parse(input.desired).ciphertext))) + return await restoreWireguardRevision(source.server,String(input.revisionId),{beforeProof:String(input.beforeProof),revisionProof:String(input.revisionProof),desired,authorize:source.access}) + }catch(error){if(error instanceof McpError)throw error;throw fail("Результат WireGuard restore неизвестен; нужна сверка без повторной записи","MUTATION_OUTCOME_UNKNOWN")} + }, + verify:async(input,result)=>{const receipt=result as {verified?:boolean;afterFingerprint?:string}|null;return receipt?.verified===true&&(await readState(input)).stateProof===receipt.afterFingerprint}, + reconcile:async(input,allowed)=>{ + await allowed();const source=await readState(input) + if(!z.string().uuid().safeParse(input.receiptId).success)throw fail("Нет идентификатора checkpoint WireGuard restore") + const rows=(await pool.query("SELECT s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 AND s.state='verified' AND s.intended_state=$2::jsonb AND s.result->>'verified'='true' LIMIT 2",[principalSchema.parse(input.principal).connectionId,JSON.stringify(redact({kind:"service",service,input}))])).rows + await allowed();await source.access();const fresh=await readState(input) + if(fresh.stateProof!==source.stateProof)throw fail("WireGuard изменился во время сверки") + const after=rows.length===1?rows[0].result?.afterFingerprint:undefined + return {matchesOriginalState:fresh.stateProof===input.beforeProof,matchesDesiredState:typeof after==="string"&&fresh.stateProof===after,verifiedFinalCheckpointAvailable:typeof after==="string",forwardingVerified:false} + }, + }) + register(service,"mm_wireguard_revision_restore_plan","Восстановить весь раздел WireGuard из проверенной ревизии после подтверждения","network","mm:network:write","write","servers",z.object({serverId:z.number().int().positive().safe(),revisionId:z.string().uuid()}).strict(),{ + prepare:async(input,context)=>{ + const req=request(input,context),source=await read(req,context.signal),desired=materializeWireguardRestore(source.revision.desired,source.state),commands=planWireguardRevisionRestore(desired,source.state) + const objects:ChangeEvidence["objects"]=[{kind:"server",id:String(source.server.id),serverId:source.server.id}] + for(const command of commands)if(command.id)objects.push({kind:({interfaces:"wireguard",peers:"wireguard_peer",ipv4:"ipv4_address",ipv6:"ipv6_address"})[command.resource],id:command.id,serverId:source.server.id}) + await source.access();const protections=(await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) ORDER BY id LIMIT 1001",[String(source.server.id)])).rows + if(protections.length>1000)throw fail("Превышен бюджет защит WireGuard restore") + for(const {object_ref:ref}of protections)objects.push({kind:ref.kind,id:ref.id,serverId:source.server.id}) + await source.access() + const review={nativeCommands:commands.length,inventory:commands.map(({method,path,id,resource})=>({method,path,id,resource})),remove:source.state.base.interfaces.filter(row=>!desired.interfaces.some(i=>i.name===row.name)).map(row=>({id:row[".id"],name:row.name})),restore:desired.interfaces.map(i=>({name:i.name,create:!source.state.base.interfaces.some(r=>r.name===i.name),enabled:!i.disabled,listenPort:i.listenPort,mtu:i.mtu,addressCount:i.innerAddresses?.length??0,peerCount:i.peers.length}))} + return {target:{scope:"servers",serverIds:[source.server.id]},objects,snapshots:[],before:source.publicState,requiresHuman:true,mutationCost:commands.length+1, + steps:[{kind:"service",service,input:{...req,review,beforeProof:source.stateProof,revisionProof:source.revision.revisionProof,protectionProof:source.protectionProof,receiptId:randomUUID(),desired:{ciphertext:await encryptSecret(JSON.stringify(desired))}}}], + warnings:["Восстанавливается весь раздел WireGuard: отсутствующие интерфейсы, peers и статические IPv4/IPv6 адреса удаляются. Inventory включает каждую native-команду.","Затронутые интерфейсы отключаются, включение выполняется последним. Используемые объектами приложения, NetFlow или протоколами интерфейсы требуют отдельных предварительных планов.","Пустые/маскированные ключи старой ревизии сохраняют существующие ключи. Неизвестный ключ нового интерфейса запрещён. Настроенный PSK сохраняется на существующем peer; его удаление или воссоздание через ревизию запрещено.","Каждая команда проверяет источник, endpoint epoch, владельца, полномочия, защиты, revision и всё состояние после durable intent и ACK. Потеря ответа останавливает выполнение без повтора и без автоматического отката.","Сверка без записи сравнивает исходное состояние и проверенный конечный checkpoint; отсутствие checkpoint не означает достижение результата. Forwarding и IPv6 readiness не проверяются; сохранение rollback history отражено отдельно."]} + }, + }) +} diff --git a/backend/src/modules/mcp/wireguard-transfer-http.test.ts b/backend/src/modules/mcp/wireguard-transfer-http.test.ts new file mode 100644 index 00000000..9469f00a --- /dev/null +++ b/backend/src/modules/mcp/wireguard-transfer-http.test.ts @@ -0,0 +1,28 @@ +import assert from "node:assert/strict" +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-owned-wg-transfer-acceptance-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +const { pool, closePool } = await import("../../db/index.js") +const { applySqlMigrations } = await import("../../db/migrate.js") +const { updateMcpSettings } = await import("./store.js") +const { buildApp } = await import("../../index.js") +let app: Awaited> | undefined +const actor = { id: "wg-transfer-acceptance-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 3600000 } +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Disposable empty database required") + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + await updateMcpSettings({ enabled: true }, actor) + if (process.env.MMAPP_TEST_WG_EXPORT_ONLY !== "true") { + await (await import("./protection-scope-acceptance.js")).acceptProtectionRegistrationScope(actor) + await (await import("./wireguard-import-acceptance.js")).acceptWireguardImport(actor) + } + app = await buildApp({ startScheduler: false }); await app.ready() + await (await import("./wireguard-export-acceptance.js")).acceptWireguardExportMcp(actor, app) +} finally { + await pool.query("UPDATE servers SET enabled=false").catch(() => {}) + await (await import("../../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await app?.close() + await closePool() +} diff --git a/backend/src/modules/mcp/workers.ts b/backend/src/modules/mcp/workers.ts new file mode 100644 index 00000000..59dfb523 --- /dev/null +++ b/backend/src/modules/mcp/workers.ts @@ -0,0 +1,190 @@ +import { isDeepStrictEqual } from "node:util" +import { withConfigMutationLock } from "../../services/config-mutation-lock.js" +import { acknowledgedCreateId } from "./native-create-identity.js" +import { readRosSnapshot } from "./resources.js" +import { proveRosDriftExpectation } from "./ros-step-verification.js" +import { randomUUID } from "node:crypto" +import { retainServerProbeStorage } from "../../services/server-probe-retention.js" +import { maintainServerProbeOperationRetirement } from "../../services/server-probe-operation-retirement.js" +import { collectExpiredServerPollStorage } from "../../services/server-poll-storage-retention.js" +import { compactCompletedServerPollStorage } from "../../services/server-poll-terminal-compaction.js" +import { retireCompletedServerPollOperations } from "../../services/server-poll-operation-retirement.js" +import { pool } from "../../db/index.js" +import { sharedDatabaseGate } from "../../services/shared-database-gate.js" +import { recoverMcpOperations } from "./runtime.js" +import { retireOrdinaryMcpOperations } from "./ordinary-operation-retirement.js" +import { audit, getMcpSettings } from "./store.js" +import { accessGeneration, decryptSecretExisting, McpError, redact } from "./security.js" +import { withMcpDatabaseRequest } from "./database-request-lease.js" +import { tickRoutingProfiles } from "./routing-profiles.js" +import type { ChangeEvidence } from "./action-types.js" +import { PartitionWriterBusy, withBackgroundPartitionWrite } from "../../services/partition-write-transaction.js" +import { retainManualInverseChargesWithinOwner } from "./routing-manual-inverse-store.js" +import { recoverScopedRecoveryIntents } from "./routing-community-scoped-recovery.js" + +type DriftCheckpoint = { before_sealed: string; after_sealed: string; result: unknown; endpoint_epoch: string } + +const driftCheckpointSql = `SELECT k.before_sealed,k.after_sealed,s.result,device.routing_endpoint_epoch AS endpoint_epoch + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_connections c ON c.id=o.connection_id + JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=$2 + JOIN mcp_operation_checkpoints k ON k.operation_id=s.operation_id AND k.step_index=s.step_index + JOIN servers device ON device.id=s.server_id + WHERE o.id=$1 AND c.id=$7 AND p.connection_id=c.id + AND o.state='applied' AND o.verification='passed' AND o.finished_at>now()-interval '7 days' + AND c.policy->>'status'='active' AND (c.policy->>'expiresAt')::timestamptz>now() AND c.generation=$3 + AND p.evidence->>'sealed'=$4 AND s.server_id=$5 AND s.intended_state=$6::jsonb AND device.enabled + AND s.state='verified' AND s.result IS NOT NULL AND k.after_sealed IS NOT NULL + AND NOT EXISTS(SELECT 1 FROM mcp_operations newer JOIN mcp_operation_steps ns ON ns.operation_id=newer.id + WHERE newer.created_at>o.created_at AND ns.server_id=s.server_id)` + +export async function detectMcpDrift() { + const generation = await accessGeneration() + const { rows } = await pool.query<{ id: string; connection_id: string; evidence: { sealed: string } }>(`SELECT o.id,o.connection_id,p.evidence + FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_connections c ON c.id=o.connection_id + WHERE o.state='applied' AND o.verification='passed' AND o.finished_at>now()-interval '7 days' + AND c.policy->>'status'='active' AND (c.policy->>'expiresAt')::timestamptz>now() AND c.generation=$1 + ORDER BY o.finished_at DESC LIMIT 50`, [generation]) + const seen = new Set() + for (const row of rows) { + let steps: ChangeEvidence['steps'] + try { + const evidence = JSON.parse(await decryptSecretExisting(row.evidence.sealed)) as ChangeEvidence + if (!Array.isArray(evidence.steps)) continue + steps = evidence.steps + } catch { continue } + + const uncertainResources = new Set() + for (let index = steps.length - 1; index >= 0; index--) { + const step = steps[index] + if (step?.kind !== "ros" || !step.step) continue + const { serverId, resource, action, id } = step.step + const resourceKey = `${serverId}:${resource}` + if (uncertainResources.has(resourceKey)) continue + try { + const result = action === "create" + ? (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=$2 AND server_id=$3", [row.id, index, serverId])).rows[0]?.result + : undefined + const nativeId = action === "create" ? acknowledgedCreateId(result) : id + if (action === "create" && !nativeId) { + // A later create without a durable native identity can shadow an older target. + uncertainResources.add(resourceKey) + continue + } + const targetKey = JSON.stringify([serverId, resource, nativeId ?? null]) + if (seen.has(targetKey)) continue + seen.add(targetKey) + + await withConfigMutationLock([serverId], async () => { + const expected = JSON.stringify(redact(step)) + const args = [row.id, index, generation, row.evidence.sealed, serverId, expected, row.connection_id] + const { rows: [checkpoint] } = await pool.query(driftCheckpointSql, args) + if (!checkpoint || await accessGeneration() !== generation) return + const before = JSON.parse(await decryptSecretExisting(checkpoint.before_sealed)) + const after = JSON.parse(await decryptSecretExisting(checkpoint.after_sealed)) + const expectation = proveRosDriftExpectation(step.step, checkpoint.result, before, after) + if (!expectation || expectation.nativeId !== nativeId) return + const current = await readRosSnapshot(serverId, resource, undefined, expectation.sourceProof) + if (expectation.matches(current) !== false) return + const observedAt = new Date().toISOString() + + await withBackgroundPartitionWrite(async ({ client }) => { + const { rows: [locked] } = await client.query( + `${driftCheckpointSql} FOR SHARE OF o,p,c,s,k,device`, args) + if (!isDeepStrictEqual(locked, checkpoint) || await accessGeneration() !== generation) return + const inserted = await client.query(`INSERT INTO mcp_drift(id,operation_id,server_id,expected,observed) + VALUES($1,$2,$3,$4::jsonb,$5::jsonb) ON CONFLICT DO NOTHING RETURNING id`, [randomUUID(), row.id, serverId, + JSON.stringify(redact({ stepIndex: index, step })), JSON.stringify({ matchesExpected: false, observedAt })]) + if (inserted.rows.length) { + await audit(client, "mcp-drift", "drift.detected", row.connection_id, { operationId: row.id, stepIndex: index, serverId }) + await client.query("UPDATE mcp_routing_profiles SET enabled=false,state=state || $2::jsonb WHERE connection_id=$1 AND enabled", [row.connection_id, JSON.stringify({ pausedReason: "Обнаружен дрейф конфигурации" })]) + } + }) + }) + } catch { /* Unavailable proof or persistence leaves the next candidate eligible. */ } + } + } +} + +const mcpRetentionBatchSize = 1000 + +async function retainMcpData() { + await withBackgroundPartitionWrite(async ({ client }) => { + const { requestsRetentionDays } = await getMcpSettings(client) + await client.query(`WITH expired AS ( + SELECT id FROM mcp_requests WHERE created_at$1) AND status NOT IN ('running') + ORDER BY created_at,id LIMIT $2 FOR UPDATE SKIP LOCKED + ) DELETE FROM mcp_requests USING expired WHERE mcp_requests.id=expired.id`, [requestsRetentionDays, mcpRetentionBatchSize]) + }) + await withBackgroundPartitionWrite(async ({ client }) => { + const { auditRetentionDays } = await getMcpSettings(client) + await client.query(`WITH expired AS ( + SELECT id FROM mcp_audit WHERE created_at$1) + ORDER BY created_at,id LIMIT $2 FOR UPDATE SKIP LOCKED + ) DELETE FROM mcp_audit USING expired WHERE mcp_audit.id=expired.id`, [auditRetentionDays, mcpRetentionBatchSize]) + }) + await withBackgroundPartitionWrite(({ client }) => client.query(`WITH expired AS ( + SELECT id FROM mcp_artifacts WHERE expires_at client.query("DELETE FROM mcp_oauth_requests WHERE expires_at client.query("DELETE FROM mcp_budget_buckets WHERE bucket retainManualInverseChargesWithinOwner(client)) + await withBackgroundPartitionWrite(async ({ client }) => { + const { requestsRetentionDays } = await getMcpSettings(client) + await client.query(`WITH candidates AS MATERIALIZED (SELECT source_operation_id FROM mcp_routing_forward_charges + WHERE created_at$1) ORDER BY created_at,source_operation_id LIMIT 1000), + expired AS (SELECT c.source_operation_id FROM candidates c WHERE NOT EXISTS(SELECT 1 FROM mcp_operations o + WHERE o.id=c.source_operation_id AND o.state IN('pending','running'))) + DELETE FROM mcp_routing_forward_charges c USING expired e WHERE c.source_operation_id=e.source_operation_id`, [requestsRetentionDays]) + }) + await withBackgroundPartitionWrite(({ client }) => client.query("DELETE FROM mcp_routing_observations WHERE created_at client.query("DELETE FROM mcp_path_checks WHERE created_at client.query("DELETE FROM mcp_unauthorized_buckets WHERE bucket client.query("DELETE FROM mcp_portal_events WHERE created_at withMcpDatabaseRequest(async () => { + entered = true + await tickAdmittedMcpWorkers() + })) + } catch (error) { + if (entered || !(error instanceof McpError && ["PROTOCOL_STORAGE_BUSY", "RATE_LIMITED"].includes(error.code))) throw error + } +} + +async function tickAdmittedMcpWorkers() { + const lease = await pool.connect() + let locked = false, unsafe = false + try { + locked = (await lease.query("SELECT pg_try_advisory_lock(hashtextextended('mcp-workers',0)) AS locked")).rows[0].locked + if (!locked) return + await recoverMcpOperations() + await recoverScopedRecoveryIntents() + await retainMcpData() + await retainServerProbeStorage() + await compactCompletedServerPollStorage() + await retireCompletedServerPollOperations() + await collectExpiredServerPollStorage() + await retireOrdinaryMcpOperations() + if ((await getMcpSettings()).enabled) { await detectMcpDrift(); await tickRoutingProfiles() } + await maintainServerProbeOperationRetirement() + } catch (error) { + if (!(error instanceof PartitionWriterBusy)) throw error + } finally { + if (locked) try { await lease.query("SELECT pg_advisory_unlock(hashtextextended('mcp-workers',0))") } catch { unsafe = true } + lease.release(unsafe) + } +} + +export function startMcpWorkers(onError: (error: unknown) => void) { + let stopped = false, running: Promise | undefined + const tick = () => { + if (stopped || running) return + running = tickMcpWorkers().catch(onError).finally(() => { running = undefined }) + } + const timer = setInterval(tick, 30_000); timer.unref(); tick() + return async () => { stopped = true; clearInterval(timer); await running } +} diff --git a/backend/src/modules/servers/repository/servers-repository.ts b/backend/src/modules/servers/repository/servers-repository.ts index ab5db23d..82749de3 100644 --- a/backend/src/modules/servers/repository/servers-repository.ts +++ b/backend/src/modules/servers/repository/servers-repository.ts @@ -2,6 +2,7 @@ import { desc, eq } from "drizzle-orm" import { db } from "../../../db/index.js" import { serverSnapshots, servers } from "../../../db/schema.js" import { invalidateFlowCatalogCache } from "../../../services/traffic-flow-topology.js" +import { withPartitionWrite, type PartitionWriteOptions } from "../../../services/partition-write-transaction.js" export type ServerRow = typeof servers.$inferSelect export type SnapshotRow = typeof serverSnapshots.$inferSelect @@ -32,8 +33,10 @@ export async function updateServerRowById( return updated } -export async function deleteServerRowById(id: number): Promise { - await db.delete(servers).where(eq(servers.id, id)) +export async function deleteServerRowById(id: number,partitionWriteOptions:PartitionWriteOptions={}): Promise { + // The outer server deletion cascades into partitioned telemetry tables. + // Admission must precede even this parent DELETE on its own backend. + await withPartitionWrite(({ db: writer }) => writer.delete(servers).where(eq(servers.id, id)),partitionWriteOptions) invalidateFlowCatalogCache() } diff --git a/backend/src/modules/servers/service/servers-service.ts b/backend/src/modules/servers/service/servers-service.ts index d9ecb4c1..c82c9860 100644 --- a/backend/src/modules/servers/service/servers-service.ts +++ b/backend/src/modules/servers/service/servers-service.ts @@ -10,6 +10,8 @@ import { } from "../repository/servers-repository.js" import { toServerRead } from "../mapper/servers-mapper.js" import type { ServerCreate, ServerRead, ServerUpdate, SnapshotRead } from "../../../types/server.js" +import { executeGuardedApplicationWrite } from "../../../services/config-mutation-policy.js" +import { withConfigMutationLock } from "../../../services/config-mutation-lock.js" export async function listServersRead(): Promise { const rows = await listServerRows() @@ -22,7 +24,8 @@ export async function getServerReadById(id: number): Promise { +export function createServer(input: ServerCreate): Promise { return executeGuardedApplicationWrite({ kind: "server", id: "new" }, [], () => createServerImpl(input)) } +async function createServerImpl(input: ServerCreate): Promise { const now = new Date().toISOString() const { wanUplinks, ...rest } = input const inserted = await createServerRow({ @@ -34,7 +37,10 @@ export async function createServer(input: ServerCreate): Promise { return toServerRead(inserted, undefined) } -export async function updateServer(id: number, input: ServerUpdate): Promise { +export function updateServer(id: number, input: ServerUpdate): Promise { + return executeGuardedApplicationWrite({ kind: "server", id: String(id) }, [id], () => withConfigMutationLock([id], () => updateServerImpl(id, input))) +} +async function updateServerImpl(id: number, input: ServerUpdate): Promise { const { wanUplinks, ...rest } = input const setPayload: Record = { updatedAt: new Date().toISOString() } for (const [k, v] of Object.entries(rest)) { @@ -47,7 +53,10 @@ export async function updateServer(id: number, input: ServerUpdate): Promise { +export function deleteServer(id: number): Promise { + return executeGuardedApplicationWrite({ kind: "server", id: String(id) }, [id], () => withConfigMutationLock([id], () => deleteServerImpl(id))) +} +async function deleteServerImpl(id: number): Promise { await deleteServerRowById(id) } diff --git a/backend/src/modules/users/binding-native-read.ts b/backend/src/modules/users/binding-native-read.ts new file mode 100644 index 00000000..224e3aa9 --- /dev/null +++ b/backend/src/modules/users/binding-native-read.ts @@ -0,0 +1,26 @@ +import type { UserBindingCreate } from "@mmapp/contracts/users" +import { getServerRowById } from "../servers/repository/servers-repository.js" +import { MikrotikClient } from "../../services/mikrotik.js" +import { sameWireguardEndpoint } from "../../services/wireguard-source.js" +import { ipsecClientBindingKeys, type IpsecServerState } from "../../services/ipsec-live.js" +import { bindingNativeRows, readBindingTarget, UserBindingTargetError } from "./binding-target.js" + +export function readNativeUserBindingTarget(input: UserBindingCreate, options: { signal?: AbortSignal; assertCurrent?: () => Promise } = {}) { + return readBindingTarget(input, async () => { + const server = await getServerRowById(input.serverId) + if (!server?.enabled) throw new UserBindingTargetError("Сервер отключён или не найден", 404) + const client = MikrotikClient.fromServer(server) + return { + async assertCurrent() { + const fresh = await getServerRowById(input.serverId) + if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new UserBindingTargetError("Источник привязки изменился", 409) + }, + get: (path, timeout, signal, maximum) => client.get(path, timeout, signal, maximum), + ipsecKeys: (certs, peers, identities) => ipsecClientBindingKeys({ server, + certs: bindingNativeRows(certs, [".id", "name"]) as IpsecServerState["certs"], + peers: bindingNativeRows(peers, [".id"]) as IpsecServerState["peers"], + identities: bindingNativeRows(identities, [".id"]) as IpsecServerState["identities"], + }), + } + }, options) +} diff --git a/backend/src/modules/users/binding-target.test.ts b/backend/src/modules/users/binding-target.test.ts new file mode 100644 index 00000000..3645a1ee --- /dev/null +++ b/backend/src/modules/users/binding-target.test.ts @@ -0,0 +1,61 @@ +import assert from "node:assert/strict" +import { resolveBindingInterface, resolveBindingPeer, readBindingTarget, UserBindingTargetError } from "./binding-target.js" + +const row = (name: string, type: string, id = "*1") => ({ ".id": id, name, type }) +const refused = (status: number) => (error: unknown) => error instanceof UserBindingTargetError && error.status === status +let cases = 0 +for (const [native, expected] of [["ether", "ether"], ["bridge", "bridge"], ["vlan", "vlan"], ["vxlan", "vxlan"], ["gre-tunnel", "gre"], ["wg", "wg"], ["wireguard", "wg"], ["dummy", "other"]]) { + const iface = resolveBindingInterface({ serverId: 1, interfaceName: "actual" }, [row("actual", native)]) + assert.equal(iface.type, expected); cases++ +} +for (const raw of [undefined, {}, [{ ".id": "*1", name: "ether-guess" }], [row("actual", "")], [row("actual", "ether", "*G1")], [row("actual", "ether"), row("duplicate-id", "ether")]]) { + assert.throws(() => resolveBindingInterface({ serverId: 1, interfaceName: "actual" }, raw), refused(502)); cases++ +} +assert.throws(() => resolveBindingInterface({ serverId: 1, interfaceName: "missing" }, []), refused(404)); cases++ +assert.throws(() => resolveBindingInterface({ serverId: 1, interfaceName: "actual" }, [row("actual", "ether"), row("actual", "ether", "*2")]), refused(409)); cases++ +assert.throws(() => resolveBindingInterface({ serverId: 1, interfaceName: "actual", interfaceType: "ether" }, [row("actual", "wg")]), refused(409)); cases++ +const wgInput = { serverId: 1, interfaceName: "wg-real", peerPublicKey: "actual-key" } +const wg = resolveBindingInterface(wgInput, [row("wg-real", "wg")]) +const peer = { ".id": "*2", interface: "wg-real", "public-key": "actual-key", "preshared-key": "PRIVATE_VALUE" } +assert.equal(resolveBindingPeer(wgInput, wg, [peer]).peerPublicKey, "actual-key"); cases++ +assert.equal(JSON.stringify(resolveBindingPeer(wgInput, wg, [peer])).includes("PRIVATE_VALUE"), false); cases++ +for (const peers of [[], [{ ...peer, interface: "foreign" }], [{ ...peer, "public-key": "foreign" }]]) { + assert.throws(() => resolveBindingPeer(wgInput, wg, peers), refused(404)); cases++ +} +assert.throws(() => resolveBindingPeer(wgInput, wg, [peer, { ...peer, ".id": "*3" }]), refused(409)); cases++ +assert.throws(() => resolveBindingPeer({ ...wgInput, peerPublicKey: "" }, wg, [peer]), refused(400)); cases++ +for (const key of ["client-cn", "managed-psk-name"]) { + const input = { serverId: 1, interfaceName: "ipsec-vpn", peerPublicKey: key } + const iface = resolveBindingInterface(input) + assert.equal(resolveBindingPeer(input, iface, undefined, ["client-cn", "managed-psk-name"]).peerPublicKey, key); cases++ + assert.throws(() => resolveBindingPeer(input, iface, undefined, [key, key]), refused(409)); cases++ + assert.throws(() => resolveBindingPeer(input, iface, undefined, []), refused(404)); cases++ +} +let current = true, authority = true, nativeCalls = 0 +const open = async () => ({ + async assertCurrent() { if (!current) throw new UserBindingTargetError("Источник привязки изменился", 409) }, + async get(path: string, timeout: number, signal: AbortSignal, maximum: number) { + assert(timeout > 0 && timeout <= 10000); assert.equal(maximum, 1048576); assert(signal instanceof AbortSignal) + nativeCalls++ + return path === "/interface" ? [row("wg-real", "wg")] : [peer] + }, + ipsecKeys() { return ["client-cn", "managed-psk-name"] }, +}) +const selected = await readBindingTarget(wgInput, open, { assertCurrent: async () => { assert(authority, "current authority") } }) +assert.equal(nativeCalls, 2); cases++ +current = false; await assert.rejects(selected.assertCurrent(), refused(409)); cases++ +current = true; authority = false; await assert.rejects(selected.assertCurrent(), /current authority/); cases++ +authority = true +await assert.rejects(readBindingTarget(wgInput, async () => ({ ...await open(), async get() { current = false; return [row("wg-real", "wg")] } })), refused(409)); cases++ +current = true +const cancelled = new AbortController(); cancelled.abort() +await assert.rejects(readBindingTarget(wgInput, open, { signal: cancelled.signal })); cases++ +const ipsecPaths: string[] = [] +const psk = await readBindingTarget({ serverId: 1, interfaceName: "ipsec-vpn", peerPublicKey: "managed-psk-name" }, async () => ({ + ...await open(), async get(path) { ipsecPaths.push(path); return [] }, +})) +assert.equal(psk.peerPublicKey, "managed-psk-name") +assert.deepEqual(ipsecPaths, ["/certificate", "/ip/ipsec/peer", "/ip/ipsec/identity"]); cases++ +await assert.rejects(readBindingTarget(wgInput, async () => ({ ...await open(), async get() { throw Error("PRIVATE_UPSTREAM_BODY") } })), error => + refused(502)(error) && !(error as Error).message.includes("PRIVATE_UPSTREAM_BODY")); cases++ +console.log("User binding native selectors, eight types, peer keys, scope/source/authority and privacy cases:", cases) diff --git a/backend/src/modules/users/binding-target.ts b/backend/src/modules/users/binding-target.ts new file mode 100644 index 00000000..a757901f --- /dev/null +++ b/backend/src/modules/users/binding-target.ts @@ -0,0 +1,90 @@ +import type { InterfaceType, UserBindingCreate } from "@mmapp/contracts/users" +import { mapRosInterfaceType } from "./iface-type.js" +import { normalizeBindingPeer, PeerBindError, peerDisplayName } from "./peer-bind.js" +import { nativeNetworkRows } from "../../services/network-observed-read.js" + +export class UserBindingTargetError extends Error { + constructor(message: string, readonly status: 400 | 404 | 409 | 502) { super(message) } +} +export interface BindingInterface { ifaceName: string; type: InterfaceType; nativeId?: string } +export interface BindingTarget extends BindingInterface { peerPublicKey: string; peerName: string } +export function bindingNativeRows(raw: unknown, required: string[]) { + try { + const rows = nativeNetworkRows(raw, required) + if (rows.some(row => !/^\*[a-f0-9]+$/i.test(row[".id"]!))) throw new Error("native ID") + return rows + } + catch { throw new UserBindingTargetError("Не подтверждён native-каталог привязки.", 502) } +} +export function resolveBindingInterface(input: UserBindingCreate, raw?: unknown): BindingInterface { + const ifaceName = input.interfaceName.trim() + if (!ifaceName) throw new UserBindingTargetError("Имя интерфейса обязательно", 400) + let type: InterfaceType, nativeId: string | undefined + if (ifaceName === "ipsec-vpn") type = "ipsec" + else { + const rows = bindingNativeRows(raw, [".id", "name", "type"]) + const matches = rows.filter(row => row.name === ifaceName) + if (!matches.length) throw new UserBindingTargetError("Интерфейс не найден", 404) + if (matches.length !== 1) throw new UserBindingTargetError("Неоднозначное имя интерфейса", 409) + type = mapRosInterfaceType(matches[0].type) + nativeId = matches[0][".id"] + if (type === "ipsec") throw new UserBindingTargetError("Для IPsec выберите каталог ipsec-vpn", 409) + } + if (input.interfaceType !== undefined && input.interfaceType !== type) throw new UserBindingTargetError("Тип интерфейса изменился или не соответствует устройству", 409) + return { ifaceName, type, ...(nativeId ? { nativeId } : {}) } +} +export function resolveBindingPeer(input: UserBindingCreate, iface: BindingInterface, rawPeers?: unknown, ipsecKeys?: string[]): BindingTarget { + let peerPublicKey: string + try { peerPublicKey = normalizeBindingPeer(iface.type, input.peerPublicKey) } + catch (error) { if (error instanceof PeerBindError) throw new UserBindingTargetError(error.message, 400); throw error } + if (iface.type === "wg") { + const matches = bindingNativeRows(rawPeers, [".id", "interface", "public-key"]).filter(row => + [iface.ifaceName, iface.nativeId].includes(row.interface) && row["public-key"] === peerPublicKey) + if (!matches.length) throw new UserBindingTargetError("Пир WireGuard не найден на выбранном интерфейсе", 404) + if (matches.length !== 1) throw new UserBindingTargetError("Неоднозначный пир WireGuard", 409) + } + if (iface.type === "ipsec") { + if (!Array.isArray(ipsecKeys) || ipsecKeys.length > 10000 || ipsecKeys.some(key => typeof key !== "string" || !key.trim())) throw new UserBindingTargetError("Не подтверждён native-каталог клиентов IPsec", 502) + const matches = ipsecKeys.filter(key => key === peerPublicKey) + if (!matches.length) throw new UserBindingTargetError("Клиент IPsec не найден", 404) + if (matches.length !== 1) throw new UserBindingTargetError("Неоднозначный ключ клиента IPsec", 409) + } + return { ...iface, peerPublicKey, peerName: iface.type === "wg" ? peerDisplayName({ publicKey: peerPublicKey, name: input.peerName }) : "" } +} + +export interface BindingReadSession { + assertCurrent(): Promise + get(path: string, timeoutMs: number, signal: AbortSignal, maxResponseBytes: number): Promise + ipsecKeys(certs: unknown, peers: unknown, identities: unknown): string[] +} +/** Fixed catalogues only; no UI snapshot/history, writes, retries or authority token. */ +export async function readBindingTarget(input: UserBindingCreate, open: () => Promise, options: { signal?: AbortSignal; assertCurrent?: () => Promise } = {}) { + const end = performance.now() + 10000, signal = options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(10000)]) : AbortSignal.timeout(10000) + const session = await open() + const assertCurrent = async () => { + signal.throwIfAborted() + if (performance.now() >= end) throw new UserBindingTargetError("Истёк срок проверки привязки", 502) + await options.assertCurrent?.() + await session.assertCurrent() + signal.throwIfAborted() + if (performance.now() >= end) throw new UserBindingTargetError("Истёк срок проверки привязки", 502) + } + const read = async (path: string) => { + await assertCurrent() + let value: unknown + try { value = await session.get(path, Math.max(1, Math.ceil(end - performance.now())), signal, 1048576) } + catch { throw new UserBindingTargetError("Native-каталог привязки недоступен", 502) } + await assertCurrent() + return value + } + const iface = resolveBindingInterface(input, input.interfaceName.trim() === "ipsec-vpn" ? undefined : await read("/interface")) + let rawPeers: unknown, ipsecKeys: string[] | undefined + if (iface.type === "wg") rawPeers = await read("/interface/wireguard/peers") + if (iface.type === "ipsec") { + const certs = await read("/certificate"), peers = await read("/ip/ipsec/peer"), identities = await read("/ip/ipsec/identity") + ipsecKeys = session.ipsecKeys(certs, peers, identities) + } + const target = resolveBindingPeer(input, iface, rawPeers, ipsecKeys) + await assertCurrent() + return { ...target, assertCurrent } +} diff --git a/backend/src/modules/users/iface-type.test.ts b/backend/src/modules/users/iface-type.test.ts index a455a46b..65b70680 100644 --- a/backend/src/modules/users/iface-type.test.ts +++ b/backend/src/modules/users/iface-type.test.ts @@ -8,7 +8,10 @@ assert.equal(mapRosInterfaceType("gre-tunnel"), "gre") assert.equal(mapRosInterfaceType("gre6-tunnel"), "gre") assert.equal(mapRosInterfaceType("wg"), "wg") assert.equal(mapRosInterfaceType("wireguard"), "wg") -assert.equal(mapRosInterfaceType("vlan"), "other") +assert.equal(mapRosInterfaceType("vlan"), "vlan") +assert.equal(mapRosInterfaceType("bridge", "ether1"), "bridge") +assert.equal(mapRosInterfaceType("vxlan"), "vxlan") +assert.equal(mapRosInterfaceType("unknown-native-type", "ether1"), "other") assert.equal(mapRosInterfaceType(""), "other") assert.equal(mapRosInterfaceType("", "gre-tunnel1"), "gre") assert.equal(mapRosInterfaceType("", "MSK-DC"), "other") diff --git a/backend/src/modules/users/iface-type.ts b/backend/src/modules/users/iface-type.ts index dc05e29d..06884f2f 100644 --- a/backend/src/modules/users/iface-type.ts +++ b/backend/src/modules/users/iface-type.ts @@ -1,4 +1,5 @@ -export type InterfaceType = "ether" | "gre" | "wg" | "ipsec" | "other" +import type { InterfaceType } from "@mmapp/contracts/users" +export type { InterfaceType } from "@mmapp/contracts/users" export function mapRosInterfaceType(raw: string | undefined | null, name?: string): InterfaceType { const t = String(raw ?? "").trim().toLowerCase() @@ -6,6 +7,8 @@ export function mapRosInterfaceType(raw: string | undefined | null, name?: strin // RouterOS /interface type for GRE is "gre-tunnel" (also gre, gre6, gre6-tunnel) if (t === "gre" || t.startsWith("gre-") || t.startsWith("gre6")) return "gre" if (t === "wg" || t === "wireguard") return "wg" + if (t === "bridge" || t === "vlan" || t === "vxlan" || t === "ipsec") return t + if (t) return "other" const n = String(name ?? "").trim().toLowerCase() if (n.startsWith("gre") || n.includes("gre-tunnel")) return "gre" diff --git a/backend/src/modules/users/service/users-service.ts b/backend/src/modules/users/service/users-service.ts index b2a03aed..4a31929c 100644 --- a/backend/src/modules/users/service/users-service.ts +++ b/backend/src/modules/users/service/users-service.ts @@ -1,4 +1,6 @@ import { randomUUID } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { executeGuardedApplicationWrite } from "../../../services/config-mutation-policy.js" import { and, desc, eq } from "drizzle-orm" import type { AppUserCreate, @@ -40,10 +42,10 @@ import { parseRawInterfaces, } from "../iface-type.js" import { - normalizeBindingPeer, - PeerBindError, peerDisplayName, } from "../peer-bind.js" +import { readNativeUserBindingTarget } from "../binding-native-read.js" +import { UserBindingTargetError } from "../binding-target.js" import { listWireGuardPeersForCatalog } from "../../../services/wireguard-live.js" import { listIpsecClientsForCatalog } from "../../../services/ipsec-live.js" @@ -132,7 +134,20 @@ export async function getUserById(id: string): Promise return await toUserDto(row, await listBindingRowsByUser(id)) } -export async function createUser(input: AppUserCreate): Promise { +async function userMutationDependencies(id: string, added: number[] = []) { + const user = await getUserById(id) + return [...new Set([...added, ...(user?.servers ?? []).map(server => Number(server.serverId)), ...(user?.bindings ?? []).map(binding => binding.serverId)])] +} +type UserWriteOptions = { canManagePermissions?: boolean } +const permissionChangeDenied = () => new UsersServiceError("Для изменения роли и прав требуется mm:settings:admin", 403) + +export function createUser(input: AppUserCreate, { canManagePermissions = true }: UserWriteOptions = {}): Promise { + if (!canManagePermissions && ((input.role ?? "viewer") !== "viewer" || input.sections?.length || input.servers?.length)) { + return Promise.reject(permissionChangeDenied()) + } + return executeGuardedApplicationWrite({ kind: "user", id: "new" }, (input.servers ?? []).map(server => Number(server.serverId)), () => createUserImpl(input)) +} +async function createUserImpl(input: AppUserCreate): Promise { const login = input.login.trim() if (await getUserRowByLogin(login)) { throw new UsersServiceError("Логин уже занят", 409) @@ -155,9 +170,17 @@ export async function createUser(input: AppUserCreate): Promise { return await toUserDto(row, []) } -export async function updateUser(id: string, input: AppUserUpdate): Promise { +export function updateUser(id: string, input: AppUserUpdate, { canManagePermissions = true }: UserWriteOptions = {}): Promise { + return executeGuardedApplicationWrite({ kind: "user", id }, () => userMutationDependencies(id, (input.servers ?? []).map(server => Number(server.serverId))), () => updateUserImpl(id, input, canManagePermissions)) +} +async function updateUserImpl(id: string, input: AppUserUpdate, canManagePermissions: boolean): Promise { const existing = await getUserRowById(id) if (!existing) throw new UsersServiceError("Пользователь не найден", 404) + if (!canManagePermissions && ( + (input.role != null && input.role !== existing.role) + || (input.sections != null && !isDeepStrictEqual(input.sections, asPermArray(existing.sectionsJson, []))) + || (input.servers != null && !isDeepStrictEqual(input.servers, asPermArray(existing.serversJson, []))) + )) throw permissionChangeDenied() if (input.login != null) { const other = await getUserRowByLogin(input.login.trim()) if (other && other.id !== id) throw new UsersServiceError("Логин уже занят", 409) @@ -166,43 +189,41 @@ export async function updateUser(id: string, input: AppUserUpdate): Promise { +export function deleteUser(id: string): Promise { return executeGuardedApplicationWrite({ kind: "user", id }, () => userMutationDependencies(id), () => deleteUserImpl(id)) } +async function deleteUserImpl(id: string): Promise { const existing = await getUserRowById(id) if (!existing) throw new UsersServiceError("Пользователь не найден", 404) await deleteUserRowById(id) } -export async function addBinding(userId: string, input: UserBindingCreate): Promise { +type BindingReadOptions = { signal?: AbortSignal; assertCurrent?: () => Promise } +export function addBinding(userId: string, input: UserBindingCreate, options: BindingReadOptions = {}): Promise { return executeGuardedApplicationWrite({ kind: "user", id: userId }, () => userMutationDependencies(userId, [input.serverId]), () => addBindingImpl(userId, input, options)) } +/** Read-only validation shared by plan preparation and the guarded write. */ +export async function validateUserBindingCreate(userId: string, input: UserBindingCreate, options: BindingReadOptions = {}): Promise<{ ifaceName: string; type: InterfaceType; peerPublicKey: string; peerName: string; assertCurrent(): Promise }> { const user = await getUserRowById(userId) if (!user) throw new UsersServiceError("Пользователь не найден", 404) const serverRows = await db.select().from(servers).where(eq(servers.id, input.serverId)).limit(1) if (!serverRows[0]) throw new UsersServiceError("Сервер не найден", 404) - const ifaceName = input.interfaceName.trim() - if (!ifaceName) throw new UsersServiceError("Имя интерфейса обязательно", 400) - const type: InterfaceType = input.interfaceType ?? await inferIfaceType(input.serverId, ifaceName) - let peerPublicKey = "" + let target: Awaited> try { - peerPublicKey = normalizeBindingPeer(type, input.peerPublicKey) + target = await readNativeUserBindingTarget(input, options) } catch (err) { - if (err instanceof PeerBindError) throw new UsersServiceError(err.message, err.status) + if (err instanceof UserBindingTargetError) throw new UsersServiceError(err.message, err.status) throw err } - const peerName = type === "wg" - ? peerDisplayName({ - publicKey: peerPublicKey, - name: input.peerName, - }) - : "" + const { ifaceName, type, peerPublicKey, peerName, assertCurrent } = target const taken = await getBindingByServerIfacePeer(input.serverId, ifaceName, peerPublicKey) if (taken) { throw new UsersServiceError( @@ -212,8 +233,14 @@ export async function addBinding(userId: string, input: UserBindingCreate): Prom 409, ) } + return { ifaceName, type, peerPublicKey, peerName, assertCurrent } +} + +async function addBindingImpl(userId: string, input: UserBindingCreate, options: BindingReadOptions): Promise { + const { ifaceName, type, peerPublicKey, peerName, assertCurrent } = await validateUserBindingCreate(userId, input, options) const now = new Date().toISOString() try { + await assertCurrent() const row = await createBindingRow({ id: randomUUID(), userId, @@ -228,6 +255,7 @@ export async function addBinding(userId: string, input: UserBindingCreate): Prom }) return await toBindingDto(row) } catch (err) { + if (err instanceof UserBindingTargetError) throw new UsersServiceError(err.message, err.status) if (isUniqueConstraintError(err)) { throw new UsersServiceError( type === "wg" @@ -240,7 +268,8 @@ export async function addBinding(userId: string, input: UserBindingCreate): Prom } } -export async function removeBinding(userId: string, bindingId: string): Promise { +export function removeBinding(userId: string, bindingId: string): Promise { return executeGuardedApplicationWrite({ kind: "user", id: userId }, () => userMutationDependencies(userId), () => removeBindingImpl(userId, bindingId)) } +async function removeBindingImpl(userId: string, bindingId: string): Promise { const row = await getBindingRowById(bindingId) if (!row || row.userId !== userId) { throw new UsersServiceError("Привязка не найдена", 404) @@ -248,13 +277,6 @@ export async function removeBinding(userId: string, bindingId: string): Promise< await deleteBindingRowById(bindingId) } -async function inferIfaceType(serverId: number, ifaceName: string): Promise { - const snap = await getLatestSnapshot(serverId) - const parsed = parseRawInterfaces(snap?.rawInterfaces) - const found = parsed.find((i) => i.name === ifaceName) - return found?.type ?? "other" -} - export async function getInterfaceCatalogState(serverId: number): Promise { const serverRows = await db.select().from(servers).where(eq(servers.id, serverId)).limit(1) if (!serverRows[0]) throw new UsersServiceError("Сервер не найден", 404) @@ -365,9 +387,12 @@ export async function getInterfaceCatalogState(serverId: number): Promise a.name.localeCompare(b.name)), failures } } -export async function listInterfaceCatalog(serverId: number): Promise { - const state = await getInterfaceCatalogState(serverId) +export function projectLegacyInterfaceCatalog(state: InterfaceCatalogState): CatalogInterface[] { return state.interfaces.filter(iface => !(iface.type === "ipsec" && iface.name === "ipsec-vpn" && !iface.peers?.length)).map(iface => ({ ...iface, running: iface.running ?? false, disabled: iface.disabled ?? false })) } +export async function listInterfaceCatalog(serverId: number): Promise { + return projectLegacyInterfaceCatalog(await getInterfaceCatalogState(serverId)) +} + export { parseRawInterfaces, mapRosInterfaceType } diff --git a/backend/src/plugins/auth-provenance.test.ts b/backend/src/plugins/auth-provenance.test.ts new file mode 100644 index 00000000..968194f5 --- /dev/null +++ b/backend/src/plugins/auth-provenance.test.ts @@ -0,0 +1,189 @@ +import assert from "node:assert/strict" +import Fastify, { type FastifyInstance, type FastifyRequest } from "fastify" +import { SignJWT } from "jose" +import { mkdtemp, realpath, mkdir, writeFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, dirname, basename } from "node:path" +import { createHash } from "node:crypto" +import { spawn } from "node:child_process" +import { fileURLToPath } from "node:url" + +const local = process.env.MM_TEST_AUTH_PROVENANCE_LOCAL === "true" +process.env.AUTH_REQUIRED = local ? "false" : "true" +process.env.AUTH_JWT_SECRET = "provenance-test-secret-at-least-8" +process.env.AUTH_ISSUER = "https://provenance.example.invalid" +process.env.NODE_ENV = "test" +const temporaryParent = await realpath(tmpdir()), ownedRoot = await mkdtemp(join(temporaryParent, "mm-auth-provenance-")) +process.env.MCP_STATE_DIR = ownedRoot +const { default: authPlugin, requireAuth, captureVerifiedRestRequest, inspectVerifiedRestRequest, RestRequestProvenanceError } = await import("./auth.js") +const { pool, closePool } = await import("../db/index.js") +const originalQuery = pool.query, cutoff = new Map(), blocked = new Set() +let portalReads = 0, onPortal: (() => void) | undefined +pool.query = (async (text: string, values: unknown[]) => { + assert.equal(text, "SELECT invalid_before FROM mcp_owner_revocations WHERE owner_id=$1") + portalReads++; onPortal?.() + const owner = String(values[0]) + if (blocked.has(owner)) return new Promise(() => {}) + return { rows: cutoff.has(owner) ? [{ invalid_before: new Date(cutoff.get(owner)!) }] : [] } +}) as never +const denied = (error: unknown) => error instanceof RestRequestProvenanceError || error instanceof Error && "code" in error && error.code === "OWNER_CLAIMS_REVOKED" +const scope = (milliseconds = 5000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + milliseconds, ...(signal ? { signal } : {}) }) +const secret = new TextEncoder().encode(process.env.AUTH_JWT_SECRET) +const mint = (owner = "original-owner", permissions = ["mm:uptime:write"], extras: Record = {}) => new SignJWT({ sub: owner, apps: ["mm"], permissions, iat: Math.floor(Date.now() / 1000) - 10, exp: Math.floor(Date.now() / 1000) + 3600, ...extras }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(process.env.AUTH_ISSUER!).sign(secret) +const headers = (token: string) => ({ authorization: "Bearer " + token }) +const apps: FastifyInstance[] = [] +let saved: unknown, heldRequest: FastifyRequest | undefined, signalCaptured: AbortController | undefined +let releaseHold!: () => void, holdReady!: () => void, lateStart!: () => void +const held = new Promise(resolve => { holdReady = resolve }), holdRelease = new Promise(resolve => { releaseHold = resolve }) +let lateResult: Promise | undefined +async function makeApp() { + const app = Fastify(); apps.push(app) + await app.register(authPlugin) + // Even an overwritten request method cannot bypass the plugin's captured + // actual JWT verifier when the private provenance is captured. + app.addHook("preHandler", async request => { + if (request.url === "/api/uptime/fake-verifier") request.jwtVerify = (async () => { request.user = { sub: "forged", apps: ["mm"], permissions: ["mm:uptime:write"], iat: 1, exp: 9999999999 }; return request.user }) as never + }) + app.addHook("preHandler", requireAuth) + app.setErrorHandler((error, _request, reply) => { reply.code(denied(error) ? 403 : 500).send({ error: denied(error) ? "refused" : "unexpected" }) }) + app.post("/api/uptime/ordinary", async () => ({ ok: true })) + app.post("/api/uptime/provenance", async request => { + saved = await captureVerifiedRestRequest(request, scope()) + return inspectVerifiedRestRequest(saved, scope(), ["mm:uptime:write"]) + }) + app.post("/api/uptime/fake-verifier", async request => captureVerifiedRestRequest(request, scope())) + app.post("/api/uptime/mutation", async request => { + const token = await captureVerifiedRestRequest(request, scope()) + request.user.permissions!.push("mm:servers:write"); request.user.sub = "replacement" + request.authUser!.permissions.push("mm:servers:write"); request.authUser!.id = "replacement" + await assert.rejects(inspectVerifiedRestRequest(token, scope(), "mm:servers:write"), denied) + const original = await inspectVerifiedRestRequest(token, scope(), "mm:uptime:write") + assert.equal(original.ownerId, "original-owner"); assert.deepEqual(original.permissions, ["mm:uptime:write"]) + assert(Object.isFrozen(original)); assert(Object.isFrozen(original.permissions)) + for (const fake of [{}, { ...token }, JSON.parse(JSON.stringify(token)), { authRequired: false, actor: request.authUser }]) await assert.rejects(inspectVerifiedRestRequest(fake, scope(), "mm:uptime:write"), denied) + return { ok: true } + }) + app.post("/api/uptime/requirements", async request => { + const token = await captureVerifiedRestRequest(request, scope()), required = ["mm:uptime:write"] + onPortal = () => { required[0] = "mm:servers:write" } + try { await inspectVerifiedRestRequest(token, scope(), required) } finally { onPortal = undefined } + const controller = new AbortController(), mutableScope = scope(5000, controller.signal) + onPortal = () => { mutableScope.deadlineAt = Date.now() - 1 } + try { await inspectVerifiedRestRequest(token, mutableScope, "mm:uptime:write") } finally { onPortal = undefined } + let getterCalls = 0 + const hostile = Object.defineProperty({}, "deadlineAt", { get() { getterCalls++; return Date.now() + 5000 }, enumerable: true }) + await assert.rejects(captureVerifiedRestRequest(request, hostile as never), denied); assert.equal(getterCalls, 0) + return { ok: true } + }) + app.post("/api/uptime/revoke", async request => { + const token = await captureVerifiedRestRequest(request, scope()) + cutoff.set("original-owner", Date.now()) + try { await assert.rejects(inspectVerifiedRestRequest(token, scope(), "mm:uptime:write"), denied) } finally { cutoff.delete("original-owner") } + return { ok: true } + }) + app.post("/api/uptime/external-revoke", async request => { + const token = await captureVerifiedRestRequest(request, scope()), directory = join(ownedRoot, "owner-revocations") + await mkdir(directory, { recursive: true }) + const file = join(directory, createHash("sha256").update("original-owner").digest("hex") + ".json") + await writeFile(file, JSON.stringify({ invalidBefore: Date.now() })) + try { await assert.rejects(inspectVerifiedRestRequest(token, scope(), "mm:uptime:write"), denied) } finally { await rm(file) } + return { ok: true } + }) + app.post("/api/uptime/signal", async request => { + signalCaptured = new AbortController() + const firstScope = scope(5000, signalCaptured.signal), token = await captureVerifiedRestRequest(request, firstScope), before = portalReads + const authority = await inspectVerifiedRestRequest(token, scope(), "mm:uptime:write") + assert.equal((await inspectVerifiedRestRequest(token, scope(), "mm:uptime:write")).signal, authority.signal) + const reads = portalReads + signalCaptured.abort() + assert.equal(authority.signal.aborted, true) + await assert.rejects(inspectVerifiedRestRequest(token, scope(), "mm:uptime:write"), denied) + await assert.rejects(captureVerifiedRestRequest(request, firstScope), denied) + await assert.rejects(captureVerifiedRestRequest(request, scope(5000, new AbortController().signal)), denied) + await assert.rejects(captureVerifiedRestRequest(request, scope()), denied) + assert.equal(portalReads, reads); assert(reads > before || local) + return { ok: true } + }) + app.post("/api/uptime/raw-abort", async request => { + const token = await captureVerifiedRestRequest(request, scope()), before = portalReads + const authority = await inspectVerifiedRestRequest(token, scope(), "mm:uptime:write") + assert.equal(authority.signal.aborted, false) + const reads = portalReads + request.raw.emit("aborted") + assert.equal(authority.signal.aborted, true) + await assert.rejects(inspectVerifiedRestRequest(token, scope(), "mm:uptime:write"), denied); assert.equal(portalReads, reads); assert(reads > before || local) + return { ok: true } + }) + app.post("/api/uptime/timeout", async request => { + const bounded = scope(50) + await assert.rejects(captureVerifiedRestRequest(request, bounded), denied) + return { ok: true } + }) + app.post("/api/uptime/expiry", async request => { + const originalScope = scope(1000), token = await captureVerifiedRestRequest(request, originalScope), original = await inspectVerifiedRestRequest(token, scope(60000), "mm:uptime:write") + assert.equal(original.expiresAt, originalScope.deadlineAt) + const read = Date.now + Date.now = () => original.expiresAt + try { await assert.rejects(inspectVerifiedRestRequest(token, scope(60000), "mm:uptime:write"), denied) } finally { Date.now = read } + return { ok: true } + }) + app.post("/api/uptime/hold", async request => { + saved = await captureVerifiedRestRequest(request, scope()); heldRequest = request; holdReady() + await holdRelease; await inspectVerifiedRestRequest(saved, scope(), "mm:uptime:write"); return { ok: true } + }) + app.post("/api/uptime/foreign", async () => { + await assert.rejects(inspectVerifiedRestRequest(saved, scope(), "mm:uptime:write"), denied) + await assert.rejects(captureVerifiedRestRequest(heldRequest!, scope()), denied) + return { ok: true } + }) + app.post("/api/uptime/late", async request => { + const token = await captureVerifiedRestRequest(request, scope()) + const authority = await inspectVerifiedRestRequest(token, scope(), "mm:uptime:write") + assert.equal(authority.signal.aborted, false) + lateResult = (async () => { await new Promise(resolve => { lateStart = resolve }); assert.equal(authority.signal.aborted, true); await assert.rejects(inspectVerifiedRestRequest(token, scope(), "mm:uptime:write"), denied) })() + return { ok: true } + }) + return app +} +try { + const app = await makeApp(), jwt = await mint() + const post = (url: string, token = jwt) => app.inject({ method: "POST", url: "/api/uptime/" + url, headers: headers(token), payload: { authRequired: false, permissions: ["mm:servers:write"] } }) + if (local) { + const result = await post("provenance", "not-a-jwt") + assert.equal(result.statusCode, 200); assert.equal(result.json().authentication, "local"); assert.equal(result.json().ownerId, "local-operator"); assert.equal(portalReads, 0) + console.log("REST local provenance: actual server configuration only, no JWT/portal widening") + } else { + await assert.rejects(captureVerifiedRestRequest({ jwtVerify: async () => ({ sub: "forged" }), authUser: { id: "forged" } } as never, scope()), denied) + assert.equal((await post("ordinary")).statusCode, 200); assert.equal(portalReads, 0) + assert.equal((await post("ordinary", await mint("no-issued", ["mm:uptime:write"], { iat: undefined }))).statusCode, 200) + assert.equal((await post("provenance", await mint("no-issued", ["mm:uptime:write"], { iat: undefined }))).statusCode, 403) + assert.equal((await post("provenance", await mint("expired", ["mm:uptime:write"], { exp: Math.floor(Date.now() / 1000) - 1 }))).statusCode, 401) + assert.equal((await post("provenance", await mint("other-app", ["mm:uptime:write"], { apps: ["other"] }))).statusCode, 403) + assert.equal((await post("provenance", await mint("read-only", ["mm:uptime:read"]))).statusCode, 403) + assert.equal((await post("fake-verifier", "not-a-signed-jwt")).statusCode, 403) + const result = await post("provenance") + assert.equal(result.statusCode, 200); assert.equal(result.json().authentication, "jwt"); assert.equal(result.json().ownerId, "original-owner") + for (const url of ["mutation", "requirements", "revoke", "external-revoke", "signal", "raw-abort", "expiry"]) assert.equal((await post(url)).statusCode, 200, url) + blocked.add("blocked-owner") + try { assert.equal((await post("timeout", await mint("blocked-owner"))).statusCode, 200) } finally { blocked.delete("blocked-owner") } + const holding = post("hold"); void holding.then(() => {}) + await held + assert.equal((await post("foreign")).statusCode, 200) + const other = await makeApp() + assert.equal((await other.inject({ method: "POST", url: "/api/uptime/foreign", headers: headers(jwt) })).statusCode, 200) + releaseHold(); assert.equal((await holding).statusCode, 200) + assert.equal((await post("late")).statusCode, 200); lateStart(); await lateResult + await assert.rejects(inspectVerifiedRestRequest(saved, scope(), "mm:uptime:write"), denied) + await new Promise((resolve, reject) => { + const child = spawn(process.execPath, [...process.execArgv, fileURLToPath(import.meta.url)], { env: { ...process.env, MM_TEST_AUTH_PROVENANCE_LOCAL: "true" }, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] }) + let output = ""; child.stdout.on("data", chunk => { output += String(chunk) }); child.stderr.on("data", chunk => { output += String(chunk) }) + child.once("error", reject); child.once("exit", code => { if (code === 0) resolve(); else reject(Error("Local-mode isolated provenance test failed: " + output.slice(-4000))) }) + }) + console.log("REST provenance: genuine Fastify lifecycle+signed JWT; immutable claims/scopes/permissions, foreign/copy denial, local config, revocation, cancellation, expiry and bounded portal wait; no bandwidth activation") + } +} finally { + releaseHold?.(); for (const app of apps) await app.close() + pool.query = originalQuery; await closePool() + assert.equal(dirname(ownedRoot), temporaryParent); assert(basename(ownedRoot).startsWith("mm-auth-provenance-")); await rm(ownedRoot, { recursive: true, force: true }) +} diff --git a/backend/src/plugins/auth.logs.test.ts b/backend/src/plugins/auth.logs.test.ts index 81353807..2294f79a 100644 --- a/backend/src/plugins/auth.logs.test.ts +++ b/backend/src/plugins/auth.logs.test.ts @@ -1,6 +1,7 @@ import assert from "node:assert/strict" import Fastify from "fastify" import { SignJWT } from "jose" +import { logPageQuerySchema, logSequenceInputSchema } from "@mmapp/contracts" process.env.AUTH_REQUIRED = "true" process.env.AUTH_JWT_SECRET = "logs-test-secret" process.env.AUTH_ISSUER = "https://auth.test.local" @@ -20,6 +21,22 @@ for (const permission of ["mm:servers:read", "mm:servers:write", "mm:servers:adm assert.equal((await app.inject({ url: "/api/logs?levels=invalid", headers: { authorization: "Bearer " + await token(permission) } })).statusCode, 400) assert.equal((await app.inject({ url: "/api/logs/settings", headers: { authorization: "Bearer " + await token(permission) } })).statusCode, 403) } +const overflow = "9223372036854775808" +const valid = "09223372036854775807" +assert.equal(logSequenceInputSchema.parse(valid), valid) +assert.equal(logSequenceInputSchema.parse("0"), "0") +assert.equal(logSequenceInputSchema.safeParse(overflow).success, false) +assert.equal(logPageQuerySchema.parse({ watermark: "000" + valid }).watermark, "000" + valid) +const cursor = (id: string | number) => Buffer.from(JSON.stringify(["2026-10-10T00:00:00Z", id])).toString("base64url") +const authorization = "Bearer " + await token("mm:servers:read") +for (const url of [ + `/api/logs?watermark=${overflow}`, + `/api/logs?cursor=${cursor(overflow)}`, + `/api/logs?cursor=${cursor(7)}`, + `/api/logs/live?serverId=1&cursor=${overflow}`, + `/api/logs/${overflow}`, + `/api/logs/${overflow}/context`, +]) assert.equal((await app.inject({ url, headers: { authorization } })).statusCode, 400, url) assert.equal((await app.inject({ method: "PUT", url: "/api/logs/settings", payload: { retentionDays: 0 }, headers: { authorization: "Bearer " + await token("mm:settings:admin") } })).statusCode, 400) await app.close() console.log("PASS logs JWT: history/live/export/context require servers:read, settings require settings:admin before DB access") diff --git a/backend/src/plugins/auth.ts b/backend/src/plugins/auth.ts index 27c3765f..90676931 100644 --- a/backend/src/plugins/auth.ts +++ b/backend/src/plugins/auth.ts @@ -1,5 +1,8 @@ import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify" import fp from "fastify-plugin" +import { AsyncLocalStorage } from "node:async_hooks" +import { randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" import { env } from "../config.js" import { hasPermission, @@ -7,6 +10,130 @@ import { type AuthUser, } from "../lib/permissions.js" +declare const restRequestBrand: unique symbol +export type VerifiedRestRequest = Readonly<{ [restRequestBrand]: true }> +export interface RestRequestScope { readonly deadlineAt: number; readonly signal?: AbortSignal } +export interface VerifiedRestRequestAuthority { + readonly requestId: string; readonly authentication: "jwt" | "local"; readonly ownerId: string + readonly method: string; readonly path: string; readonly acceptedAt: number; readonly issuedAt: number; readonly expiresAt: number + /** Original process-local horizon; consumers must not reconstruct it after a wall-clock change. */ + readonly monotonicDeadline: number + readonly apps: readonly string[]; readonly permissions: readonly string[] + /** Fixed private request/first-capture lifetime, for trusted native readers. */ + readonly signal: AbortSignal +} +export class RestRequestProvenanceError extends Error { + readonly code = "REST_REQUEST_PROVENANCE_REFUSED" + constructor() { super("Исходный REST-контекст не подтверждён или завершён") } +} +type RequestEntry = { + active: boolean; authenticated: boolean; readonly realm: object; readonly request: FastifyRequest + readonly requestId: string; readonly acceptedAt: number; readonly monotonicAt: number; readonly method: string; readonly path: string + readonly authRequired: boolean; bearer?: string; readonly verify: (token: string) => unknown; readonly controller: AbortController + claims?: Readonly & { readonly apps: readonly string[]; readonly permissions: readonly string[] }> + captureScope?: Readonly; captureDeadline?: number; authoritySignal?: AbortSignal; cleanup(): void +} +type RestToken = { readonly entry: RequestEntry; readonly authority: Readonly; readonly signal?: AbortSignal; readonly monotonicEnd: number } +const restRequests = new WeakMap(), restTokens = new WeakMap() +const activeRestRequest = new AsyncLocalStorage() +const refuseProvenance = (): never => { throw new RestRequestProvenanceError() } +function scopeSnapshot(input: Readonly): Readonly { + if (!input || Object.getPrototypeOf(input) !== Object.prototype || Reflect.ownKeys(input).length !== Object.keys(input).length) refuseProvenance() + const fields = Object.getOwnPropertyDescriptors(input) + if (Object.keys(fields).some(key => !["deadlineAt", "signal"].includes(key)) || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) refuseProvenance() + const deadlineAt = fields.deadlineAt?.value, signal = fields.signal?.value + if (typeof deadlineAt !== "number" || !Number.isSafeInteger(deadlineAt) || signal !== undefined && !(signal instanceof AbortSignal)) return refuseProvenance() + return Object.freeze({ deadlineAt, signal }) +} +function permissionsSnapshot(input: string | readonly string[]): readonly string[] { + if (typeof input === "string") { if (!input || input.length > 200 || input.trim() !== input) refuseProvenance(); return Object.freeze([input]) } + if (!Array.isArray(input) || input.length < 1 || input.length > 1000 || Reflect.ownKeys(input).length !== input.length + 1) refuseProvenance() + const fields = Object.getOwnPropertyDescriptors(input), result: string[] = [] + for (let index = 0; index < input.length; index++) { + const field = fields[String(index)] + if (!field || !("value" in field) || !field.enumerable || typeof field.value !== "string" || !field.value || field.value.length > 200 || field.value.trim() !== field.value) refuseProvenance() + result.push(field.value) + } + return Object.freeze(result) +} +function requestLive(entry: RequestEntry): void { + if (!entry.active || entry.controller.signal.aborted || activeRestRequest.getStore() !== entry || restRequests.get(entry.request) !== entry + || entry.request.server.server !== entry.realm || Date.now() < entry.acceptedAt || Date.now() >= entry.acceptedAt + 300000 || performance.now() >= entry.monotonicAt + 300000) refuseProvenance() +} +function claimsSnapshot(value: unknown): NonNullable { + if (!value || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype) return refuseProvenance() + const fields = Object.getOwnPropertyDescriptors(value) + if (Reflect.ownKeys(value).length !== Object.keys(fields).length || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) refuseProvenance() + if (typeof fields.iat?.value !== "number" || typeof fields.exp?.value !== "number") refuseProvenance() + const id = fields.sub?.value, issuedAt = fields.iat.value * 1000, expiresAt = fields.exp.value * 1000 + if (typeof id !== "string" || !id.trim() || id.length > 200 || !Number.isSafeInteger(issuedAt) || issuedAt <= 0 || !Number.isSafeInteger(expiresAt) || expiresAt <= issuedAt) refuseProvenance() + const apps = permissionsSnapshot(fields.apps?.value), permissions = permissionsSnapshot(fields.permissions?.value) + if (!apps.includes("mm")) refuseProvenance() + return Object.freeze({ id, issuedAt, expiresAt, apps, permissions }) +} +/** Bound current portal I/O too. Late continuations cannot mint or inspect a + * capability after timeout, cancellation or retirement of its actual request. */ +async function provenanceWindow(entry: RequestEntry, scope: Readonly, expiresAt: number, monotonicEnd: number, originalSignal: AbortSignal | undefined, work: (live: () => void) => Promise): Promise { + const deadlineAt = Math.min(scope.deadlineAt, expiresAt, entry.acceptedAt + 300000) + const end = Math.min(monotonicEnd, performance.now() + Math.max(0, deadlineAt - Date.now())) + const signals = [...new Set([entry.controller.signal, scope.signal, originalSignal].filter((signal): signal is AbortSignal => !!signal))] + let active = true, rejectStopped!: (error: RestRequestProvenanceError) => void + const live = () => { requestLive(entry); if (!active || Date.now() >= deadlineAt || performance.now() >= end || signals.some(signal => signal.aborted)) refuseProvenance() } + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + const stop = () => { active = false; rejectStopped(new RestRequestProvenanceError()) } + const timer = setTimeout(stop, Math.max(1, Math.min(deadlineAt - Date.now(), end - performance.now()))) + for (const signal of signals) signal.addEventListener("abort", stop, { once: true }) + try { live(); return await Promise.race([Promise.resolve().then(async () => { live(); const result = await work(live); live(); return result }), stopped]) } + finally { active = false; clearTimeout(timer); for (const signal of signals) signal.removeEventListener("abort", stop) } +} +async function portalCurrent(entry: RequestEntry, live: () => void): Promise { + live() + if (entry.authRequired) { + const claims = entry.claims ?? refuseProvenance() + const { assertPortalClaimsCurrent } = await import("../modules/mcp/portal-revocation.js"); live() + await assertPortalClaimsCurrent(claims); live() + } +} +/** Actual plugin hook + successful real authentication are prerequisites. Neither + * AuthUser JSON nor a request carrying its own jwtVerify function is a source. */ +export async function captureVerifiedRestRequest(request: FastifyRequest, inputScope: Readonly): Promise { + const scope = scopeSnapshot(inputScope), entry = request && typeof request === "object" ? restRequests.get(request) : undefined + if (!entry) return refuseProvenance() + requestLive(entry) + if (entry.authRequired && (!entry.authenticated || !entry.bearer)) refuseProvenance() + // This is one original request authority, not a new grant per capture. In + // particular replacing an aborted first signal cannot revive its request. + if (entry.captureScope && (scope.deadlineAt !== entry.captureScope.deadlineAt || scope.signal !== entry.captureScope.signal)) refuseProvenance() + entry.captureScope ??= scope + entry.authoritySignal ??= scope.signal ? AbortSignal.any([entry.controller.signal, scope.signal]) : entry.controller.signal + if (entry.authRequired && !entry.claims) { + try { entry.claims = claimsSnapshot(entry.verify(entry.bearer!)) } catch { return refuseProvenance() } + } + const claims = entry.claims + if (claims && claims.issuedAt! > Date.now()) refuseProvenance() + entry.captureDeadline ??= Math.min(scope.deadlineAt, claims?.expiresAt ?? Infinity, entry.acceptedAt + 300000) + const expiresAt = entry.captureDeadline, monotonicEnd = entry.monotonicAt + Math.max(0, expiresAt - entry.acceptedAt) + return provenanceWindow(entry, scope, expiresAt, monotonicEnd, entry.captureScope.signal, async live => { + await portalCurrent(entry, live) + const required = permissionForRequest(entry.method, entry.path) + if (entry.authRequired && (!claims?.apps.includes("mm") || required && !hasPermission(claims.permissions, required))) refuseProvenance() + const authority = Object.freeze({ requestId: entry.requestId, authentication: entry.authRequired ? "jwt" as const : "local" as const, + ownerId: claims?.id ?? "local-operator", method: entry.method, path: entry.path, acceptedAt: entry.acceptedAt, + issuedAt: claims?.issuedAt ?? entry.acceptedAt, expiresAt, monotonicDeadline: monotonicEnd, apps: claims?.apps ?? Object.freeze([]), permissions: claims?.permissions ?? Object.freeze([]), signal: entry.authoritySignal! }) + live(); const token = Object.freeze(Object.create(null)) as VerifiedRestRequest + restTokens.set(token, Object.freeze({ entry, authority, signal: scope.signal, monotonicEnd })); return token + }) +} +export async function inspectVerifiedRestRequest(token: unknown, inputScope: Readonly, requiredPermissions: string | readonly string[]): Promise> { + const scope = scopeSnapshot(inputScope), required = permissionsSnapshot(requiredPermissions) + const captured = token && typeof token === "object" ? restTokens.get(token) : undefined + if (!captured) return refuseProvenance() + return provenanceWindow(captured.entry, scope, captured.authority.expiresAt, captured.monotonicEnd, captured.signal, async live => { + const current = () => { live(); if (captured.authority.authentication === "jwt" && (!captured.authority.apps.includes("mm") || required.some(permission => !hasPermission(captured.authority.permissions, permission)))) refuseProvenance() } + current(); await portalCurrent(captured.entry, live); current(); return captured.authority + }) +} + declare module "fastify" { interface FastifyRequest { authUser?: AuthUser @@ -24,6 +151,7 @@ declare module "@fastify/jwt" { is_admin?: boolean iss?: string exp?: number + iat?: number } user: { sub: string @@ -34,6 +162,7 @@ declare module "@fastify/jwt" { is_admin?: boolean iss?: string exp?: number + iat?: number } } } @@ -54,6 +183,29 @@ async function authPlugin(app: FastifyInstance) { : {}), }) + // Callback continuation propagates this actual request scope through Fastify + // hooks and handler. A preHandler-only ALS.run would retire too early. + const authRequired = env.authRequired, realm = app.server, verifyJwt = app.jwt.verify.bind(app.jwt), entries = new Set() + const retire = (request: FastifyRequest) => { + const entry = restRequests.get(request) + if (!entry || entry.realm !== realm) return + entry.active = false; entry.authenticated = false; entry.bearer = undefined; entry.controller.abort(); entry.cleanup(); entries.delete(entry); restRequests.delete(request) + } + app.addHook("onRequest", (request, reply, done) => { + const authorization = request.headers.authorization, bearer = typeof authorization === "string" && authorization.startsWith("Bearer ") ? authorization.slice(7) : undefined + const aborted = () => retire(request), closed = () => { if (!reply.raw.writableEnded) retire(request) } + const entry: RequestEntry = { active: true, authenticated: false, realm, request, requestId: randomUUID(), acceptedAt: Date.now(), monotonicAt: performance.now(), + method: request.method, path: request.routeOptions.url ?? request.url.split("?")[0], authRequired, bearer, verify: verifyJwt, controller: new AbortController(), + cleanup() { request.raw.off("aborted", aborted); reply.raw.off("close", closed) } } + restRequests.set(request, entry); entries.add(entry) + request.raw.once("aborted", aborted); reply.raw.once("close", closed) + if (request.raw.aborted || reply.raw.destroyed) retire(request) + activeRestRequest.run(entry, done) + }) + app.addHook("onResponse", async request => { retire(request) }) + app.addHook("onError", async request => { retire(request) }) + app.addHook("onClose", async () => { for (const entry of entries) retire(entry.request) }) + if (env.authRequired) { app.log.info( { issuer: env.authIssuer, portal: env.authPortalUrl }, @@ -90,6 +242,7 @@ export async function requireAuth( } const payload = request.user + if (typeof payload.sub !== "string" || !payload.sub.trim()) return reply.code(401).send({ error: "Unauthorized" }) const apps = Array.isArray(payload.apps) ? payload.apps.map(String) : [] const permissions = Array.isArray(payload.permissions) ? payload.permissions.map(String) @@ -108,12 +261,16 @@ export async function requireAuth( apps, permissions, isAdmin: Boolean(payload.is_admin), + expiresAt: typeof payload.exp === "number" ? payload.exp * 1000 : undefined, + issuedAt: typeof payload.iat === "number" ? payload.iat * 1000 : undefined, } const required = permissionForRequest(request.method, pathname) if (required && !hasPermission(permissions, required)) { return reply.code(403).send({ error: `Недостаточно прав: ${required}` }) } + const entry = restRequests.get(request) + if (entry && entry.active && activeRestRequest.getStore() === entry && entry.authRequired && entry.bearer === token) entry.authenticated = true } /** Gate the matched route, including encoded static segments in the raw URL. */ diff --git a/backend/src/plugins/database-request-gate.ts b/backend/src/plugins/database-request-gate.ts index 0262426f..168d60d6 100644 --- a/backend/src/plugins/database-request-gate.ts +++ b/backend/src/plugins/database-request-gate.ts @@ -1,5 +1,7 @@ import type { FastifyInstance, FastifyRequest } from "fastify" -import { DatabaseRequestGate } from "../services/database-request-gate.js" +import { sharedDatabaseGate } from "../services/shared-database-gate.js" +import { withConfigurationActor } from "../services/config-mutation-policy.js" +import { withDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" const streams = new WeakMap void }>() export function registerDatabaseStreamAbort(request: FastifyRequest, abort: () => void): void { @@ -9,8 +11,8 @@ export function registerDatabaseStreamAbort(request: FastifyRequest, abort: () = if (state.cancelled) abort() } -export function installDatabaseRequestGate(app: FastifyInstance): void { - const gate = new DatabaseRequestGate() +export function installDatabaseRequestGate(app: FastifyInstance, admit = withDatabaseApiRequest): void { + const gate = sharedDatabaseGate app.addHook("onRoute", options => { const path = options.url if (!path.startsWith("/api/") || path === "/api/auth/config") return @@ -18,9 +20,10 @@ export function installDatabaseRequestGate(app: FastifyInstance): void { const operation = path === "/api/system/database/backup" || path === "/api/system/database/restore" const stream = path === "/api/logs/live" || path === "/api/traffic/flow/live" || /^\/api\/traffic\/servers\/:id\/live$/.test(path) options.handler = async function (request, reply) { - const run = async () => await original.call(this, request, reply) - if (operation) return gate.exclusive(run) - if (!stream) return gate.request(run) + const run = async () => withConfigurationActor(request.authUser?.id ?? "system", async () => await original.call(this, request, reply)) + // The service owns exclusivity, including calls that do not originate in HTTP. + if (operation) return run() + if (!stream) return gate.request(() => admit(run)) const state = { cancelled: false, abort: undefined as (() => void) | undefined } streams.set(request, state) try { diff --git a/backend/src/routes/alerts.ts b/backend/src/routes/alerts.ts index 1c6e60c6..eb534979 100644 --- a/backend/src/routes/alerts.ts +++ b/backend/src/routes/alerts.ts @@ -1,11 +1,12 @@ import {getAlertsOutboxOverview,retryUnknownOutbox} from "../services/alert-engine/outbox.js" import {sendTelegramRequest} from "../services/alerts-telegram-send.js" +import {performTelegramTest,TelegramTestInputError} from "../services/alerts-telegram-test.js" +import {executeGuardedApplicationWrite} from "../services/config-mutation-policy.js" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { getAlertsOverview, getTelegramSettingsRow, replaceAlertsConfig, - formatAlertRuleTestTelegramText, updateTelegramSettings, } from "../services/alerts-service.js" import { @@ -102,22 +103,9 @@ export function createAlertsRoutes(deps:Dependencies):FastifyPluginAsyncZod {ret if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } - const stored = await deps.readTelegram() - const token = (parsed.data.token?.trim() || stored.botToken).trim() - const chatId = (parsed.data.chatId?.trim() || stored.chatId || "").trim() - if (!token) return reply.status(400).send({error:"Не задан Bot Token (сохраните в БД или передайте в запросе)"}) - if (!chatId) return reply.status(400).send({error:"Не задан Chat ID"}) - const threadId = parsed.data.messageThreadId ?? stored.messageThreadId ?? undefined - if (threadId !== undefined && (!Number.isInteger(threadId) || threadId < 1 || threadId > 2147483647)) return reply.status(400).send({error:"ID темы Telegram не подтверждён"}) - const text = parsed.data.rulePreview - ? formatAlertRuleTestTelegramText(parsed.data.rulePreview) - : "MikroTik Manager — тест оповещений (Telegram)." - const send = await deps.send({ - text, - token, - chatId, - messageThreadId: threadId, - }) + let send + try{send=await performTelegramTest(parsed.data,{readStored:deps.readTelegram,send:deps.send})} + catch(error){if(error instanceof TelegramTestInputError)return reply.status(400).send({error:error.message});throw error} if (send.ok !== true) { return reply.status(502).send({ ok: false, error: send.error }) } @@ -126,5 +114,5 @@ export function createAlertsRoutes(deps:Dependencies):FastifyPluginAsyncZod {ret } } -export const defaultAlertsDependencies:Dependencies={retryOutbox:retryUnknownOutbox,outbox:getAlertsOutboxOverview,overview:getAlertsOverview,replace:replaceAlertsConfig,writeTelegram:updateTelegramSettings,readTelegram:getTelegramSettingsRow,send:input=>sendTelegramRequest(input.token,{chat_id:input.chatId,text:input.text,...(input.messageThreadId!==undefined?{message_thread_id:input.messageThreadId}:{})})} +export const defaultAlertsDependencies:Dependencies={retryOutbox:retryUnknownOutbox,outbox:getAlertsOutboxOverview,overview:getAlertsOverview,replace:replaceAlertsConfig,writeTelegram:updateTelegramSettings,readTelegram:getTelegramSettingsRow,send:input=>executeGuardedApplicationWrite({kind:"alerts-telegram-test",id:"1"},"all",()=>sendTelegramRequest(input.token,{chat_id:input.chatId,text:input.text,...(input.messageThreadId!==undefined?{message_thread_id:input.messageThreadId}:{})}))} export default createAlertsRoutes(defaultAlertsDependencies) diff --git a/backend/src/routes/backups-operations.test.ts b/backend/src/routes/backups-operations.test.ts index f9455ef8..929ad8e0 100644 --- a/backend/src/routes/backups-operations.test.ts +++ b/backend/src/routes/backups-operations.test.ts @@ -9,7 +9,12 @@ const app = Fastify(); app.setValidatorCompiler(validatorCompiler); app.setSeria let unknownRestore = false let deleteOutcome = "success", deleted = 0 let created = 0, restored = 0, eventCalls = 0, restoreFail = false -await app.register(createBackupsRoutes({ delete: async () => { deleted++; if (deleteOutcome === "error") throw new Error("S3 unavailable"); if (deleteOutcome === "missing") return null; return { id: "backup-1", serverId: 1, serverName: "router", filename: "config.rsc", sizeBytes: 0, createdAt: "2026-10-01T00:00:00Z", kind: "manual", storage: "both" } }, listServers: async () => [{ id: 1 }, { id: 2 }], create: async id => { created++; if (id === "2") throw new Error("RouterOS unavailable"); return { id: "backup-1", serverId: 1, serverName: "router", filename: "config.rsc", sizeBytes: 0, createdAt: "2026-10-01T00:00:00Z", kind: "manual", storage: "local" } }, restore: async () => { restored++; if (unknownRestore) throw new BackupRestoreUnknownError(); if (restoreFail) throw new Error("import failed"); return { backupId: "backup-1", serverId: 1, filename: "config.rsc", serverName: "router" } }, appendEvent: async () => { eventCalls++; throw new Error("event DB unavailable") } }), { prefix: "/api" }) +let denyDetached = false +const admitDetached = async (work: () => Promise): Promise => { + if (denyDetached) throw new Error("database admission denied") + return work() +} +await app.register(createBackupsRoutes({ admitDetached, delete: async () => { deleted++; if (deleteOutcome === "error") throw new Error("S3 unavailable"); if (deleteOutcome === "missing") return null; return { id: "backup-1", serverId: 1, serverName: "router", filename: "config.rsc", sizeBytes: 0, createdAt: "2026-10-01T00:00:00Z", kind: "manual", storage: "both" } }, listServers: async () => [{ id: 1 }, { id: 2 }], create: async id => { created++; if (id === "2") throw new Error("RouterOS unavailable"); return { id: "backup-1", serverId: 1, serverName: "router", filename: "config.rsc", sizeBytes: 0, createdAt: "2026-10-01T00:00:00Z", kind: "manual", storage: "local" } }, restore: async () => { restored++; if (unknownRestore) throw new BackupRestoreUnknownError(); if (restoreFail) throw new Error("import failed"); return { backupId: "backup-1", serverId: 1, filename: "config.rsc", serverName: "router" } }, appendEvent: async () => { eventCalls++; throw new Error("event DB unavailable") } }), { prefix: "/api" }) const post = (payload: unknown) => app.inject({ method: "POST", url: "/api/backups/create", payload: payload as Record }) for (const serverIds of [[], ["1", "missing"], ["1", "3"], ["1", "1"], ["0"], ["1junk"], [9007199254740992]]) assert.equal((await post({ serverIds })).statusCode, 400) assert.equal(created, 0) @@ -29,6 +34,14 @@ await new Promise(resolve => setImmediate(resolve)); assert.equal(created, befor assert.equal((await post({ requestId, serverIds: ["1"], notes: "manual" })).statusCode, 202); assert.equal(created, before + 1) assert.equal((await post({ requestId, serverIds: ["2"], notes: "manual" })).statusCode, 409) assert.equal((await post({ requestId, serverIds: ["1"], notes: "other" })).statusCode, 409) +denyDetached = true +const beforeDenial = { created, eventCalls } +const denied = await post({ serverIds: ["1"] }) +assert.equal(denied.statusCode, 202) +const deniedJob = await app.inject({ method: "GET", url: "/api/backups/jobs/" + denied.json().jobId }) +assert.equal(deniedJob.json().status, "failed") +assert.deepEqual({ created, eventCalls }, beforeDenial) +denyDetached = false const success = await app.inject({ method: "POST", url: "/api/backups/backup-1/restore" }); assert.equal(success.statusCode, 200); assert.deepEqual(success.json(), { backupId: "backup-1", serverId: 1, filename: "config.rsc", serverName: "router" }); assert.equal(restored, 1) restoreFail = true; const failure = await app.inject({ method: "POST", url: "/api/backups/backup-1/restore" }); assert.equal(failure.statusCode, 400); assert.equal(failure.json().error, "import failed") unknownRestore = true; const unknown = await app.inject({ method: "POST", url: "/api/backups/backup-1/restore" }); assert.equal(unknown.statusCode, 502); assert.equal(unknown.json().outcome, "unknown") diff --git a/backend/src/routes/backups.ts b/backend/src/routes/backups.ts index 7e3f21b1..e865cee9 100644 --- a/backend/src/routes/backups.ts +++ b/backend/src/routes/backups.ts @@ -29,6 +29,8 @@ import { import { BackupRestoreUnknownError } from "../services/backup-restore.js" import { BackupStorageConflictError } from "../services/backup-storage-operation.js" import { processBackupJob, recordBackupEvent } from "../services/backup-job-execution.js" +import { createBackupJobStore, readBackupJob } from "../services/backup-job-store.js" +import { mcpDatabaseRequestSignal, withDetachedDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" type BackupJob = BackupJobStatusResponse @@ -39,9 +41,10 @@ const BackupJobIdParamSchema = z.object({ jobId: z.string().min(1), }) -export function createBackupsRoutes(deps: { listServers?: () => Promise>; create?: typeof runBackupForServer; restore?: typeof restoreBackupToDevice; delete?: typeof deleteBackupRecord; appendEvent?: typeof appendEvent } = {}): FastifyPluginAsyncZod { +export function createBackupsRoutes(deps: { listServers?: () => Promise>; create?: typeof runBackupForServer; restore?: typeof restoreBackupToDevice; delete?: typeof deleteBackupRecord; appendEvent?: typeof appendEvent; admitDetached?: typeof withDetachedDatabaseApiRequest } = {}): FastifyPluginAsyncZod { return async (app) => { - const backupJobs = new Map() + const { jobs: backupJobs, close: closeJobs } = createBackupJobStore() + app.addHook("onClose", async () => closeJobs()) const event = (input: Parameters[0]) => recordBackupEvent(input, deps.appendEvent ?? appendEvent, error => app.log.warn({ err: error }, "Не удалось записать событие резервных копий")) app.get("/backups", async (_req, reply) => { return reply.send(await listBackups()) @@ -129,28 +132,32 @@ export function createBackupsRoutes(deps: { listServers?: () => Promise {}) - void databaseBackgroundWork.track(Promise.resolve().then(async () => { - await processBackupJob(job, ids, notes, { create: deps.create ?? runBackupForServer, event }).catch(async (err) => { - job.status = "failed" - job.finishedAt = new Date().toISOString() - const failure = { - serverId: "job", - error: err instanceof Error ? err.message : String(err), - } - job.failures.push(failure) + const fail = (error: unknown) => { + job.status = "failed" + job.finishedAt = new Date().toISOString() + const failure = { serverId: "job", error: error instanceof Error ? error.message : String(error) } + job.failures.push(failure) + return failure + } + const admit = deps.admitDetached ?? withDetachedDatabaseApiRequest + void databaseBackgroundWork.track(admit(async () => { + const signal = mcpDatabaseRequestSignal() + await event({ + level: "info", + eventType: "backups.job.started", + sourceModule: "backups", + title: "Запущен бэкап", + message: `Серверов в очереди: ${ids.length}`, + entityType: "backup_job", + entityId: jobId, + payload: { + serverIds: ids, + notes: notes ?? null, + }, + }) + await processBackupJob(job, ids, notes, { create: deps.create ?? runBackupForServer, event, signal }).catch(async (err) => { + const failure = fail(err) + if (signal?.aborted) return await event({ level: "critical", eventType: "backups.job.failed", @@ -165,7 +172,7 @@ export function createBackupsRoutes(deps: { listServers?: () => Promise {}) + })).catch(fail) return reply.status(202).send({ jobId, status: job.status, @@ -176,7 +183,7 @@ export function createBackupsRoutes(deps: { listServers?: () => Promise { - const job = backupJobs.get(req.params.jobId) + const job = readBackupJob(req.params.jobId, backupJobs) if (!job) return reply.status(404).send({ error: "Job не найден" }) return reply.send(job) }) @@ -197,7 +204,7 @@ export function createBackupsRoutes(deps: { listServers?: () => Promise { try { const result = await (deps.restore ?? restoreBackupToDevice)(req.params.id) - void databaseBackgroundWork.track(event({ + await event({ level: "warning", eventType: "backups.restore", sourceModule: "backups", @@ -205,7 +212,7 @@ export function createBackupsRoutes(deps: { listServers?: () => Promise {}) + }) return reply.send(result) } catch (err) { return reply.status(err instanceof BackupRestoreUnknownError ? 502 : 400).send({ diff --git a/backend/src/routes/bgp.ts b/backend/src/routes/bgp.ts index 2fcf2c1a..1e256467 100644 --- a/backend/src/routes/bgp.ts +++ b/backend/src/routes/bgp.ts @@ -2,8 +2,8 @@ import { eq } from "drizzle-orm" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { db } from "../db/index.js" import { servers } from "../db/schema.js" -import { parseBgpSessions, parseBgpCatalogSessions, type BgpCatalogSessionRead } from "../services/bgp-parse-sessions.js" -import { MikrotikClient } from "../services/mikrotik.js" +import { type BgpCatalogSessionRead } from "../services/bgp-parse-sessions.js" +import { readBgpSessionsForServer, readBgpCatalogForServer, readRawBgpSessionsForServer } from "../services/bgp-session-read.js" import { ServerIdParamSchema, type ServerIdParams } from "../types/server.js" import type { BgpSessionRead } from "../types/server.js" @@ -20,8 +20,8 @@ interface BgpDependencies { const defaults: BgpDependencies = { listEnabledServers: () => db.select().from(servers).where(eq(servers.enabled, true)), findServer: async id => (await db.select().from(servers).where(eq(servers.id, id)).limit(1))[0], - readCatalogSessions: async server => parseBgpCatalogSessions(server, await MikrotikClient.fromServer(server).getBgpSessions()), - readSessions: async server => parseBgpSessions(server, await MikrotikClient.fromServer(server).getBgpSessions()), + readCatalogSessions: readBgpCatalogForServer, + readSessions: readBgpSessionsForServer, } export function createBgpRoutes(dependencies: BgpDependencies = defaults): FastifyPluginAsyncZod { return async (app) => { @@ -38,7 +38,7 @@ export function createBgpRoutes(dependencies: BgpDependencies = defaults): Fasti if (!server) return reply.status(404).send({ error: "Server not found" }) if (server.enabled === false) return reply.status(409).send({ error: "Сервер выключен. Опрос BGP не выполнялся" }) try { - const raw = await (dependencies.readRawSessions ?? (s => MikrotikClient.fromServer(s).getBgpSessionsRaw()))(server) + const raw = await (dependencies.readRawSessions ?? readRawBgpSessionsForServer)(server) if (!Array.isArray(raw)) throw new Error("Некорректный список BGP sessions") app.log.info({ serverId: server.id, sessionCount: raw.length }, "BGP raw response") return reply.send({ server: { id: server.id, host: server.host, name: server.name }, raw }) diff --git a/backend/src/routes/certificates.ts b/backend/src/routes/certificates.ts index 24f3fb0b..c13d0c50 100644 --- a/backend/src/routes/certificates.ts +++ b/backend/src/routes/certificates.ts @@ -8,11 +8,11 @@ import { } from "@mmapp/contracts/certificates" import { acceptCertificateIssueRequest, IssueRequestError } from "../services/certificate-issue-request.js" import { refreshScheduler } from "../services/scheduler.js" -import { testCloudflareToken } from "../services/acme-cloudflare.js" +import { performAcmeSettingsTest } from "../services/acme-settings-test.js" +import { performAcmeSettingsUpdate } from "../services/acme-settings-update.js" import { queueCertificateIssueJob } from "../services/certificate-issue-runner.js" import { createIssueJobRecordIfAbsent, - getAcmeCloudflareToken, getAcmeSettingsPublic, getCertificateRenewSettings, getIssueJobRecord, @@ -20,7 +20,6 @@ import { listCertificatesFromServers, listCertificatesState, toIssueJobDto, - updateAcmeSettings, updateCertificateRenewSettings, } from "../services/certificates-service.js" @@ -48,7 +47,7 @@ export function createCertificatesRoutes(deps: { getState?: typeof listCertifica if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } - return reply.send(await updateAcmeSettings(parsed.data)) + return reply.send((await performAcmeSettingsUpdate(parsed.data)).publicSettings) }) app.get("/certificates/renew-settings", async (_req, reply) => { @@ -70,18 +69,16 @@ export function createCertificatesRoutes(deps: { getState?: typeof listCertifica if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } - const effective = parsed.data.cloudflareApiToken?.trim() || await getAcmeCloudflareToken() - if (!effective) { - return reply.status(400).send({ error: "Не задан Cloudflare API token" }) - } + const abort = new AbortController() + const disconnected = () => { if (!reply.raw.writableEnded) abort.abort() } + req.raw.once("aborted", disconnected) + reply.raw.once("close", disconnected) try { - await testCloudflareToken(effective) - return reply.send({ ok: true, message: "Cloudflare API доступен" }) - } catch (e) { - return reply.status(400).send({ - ok: false, - message: e instanceof Error ? e.message : String(e), - }) + const result = await performAcmeSettingsTest(parsed.data, { signal: abort.signal }) + return reply.status(result.ok ? 200 : 400).send(result) + } finally { + req.raw.off("aborted", disconnected) + reply.raw.off("close", disconnected) } }) diff --git a/backend/src/routes/events.ts b/backend/src/routes/events.ts index a562eee0..8badc1a2 100644 --- a/backend/src/routes/events.ts +++ b/backend/src/routes/events.ts @@ -1,6 +1,6 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { appendEventsSchema, listEventsQuerySchema } from "@mmapp/contracts/events" -import { appendEvents, readEvents } from "../modules/events/service/events-service.js" +import { appendClientEventRows, prepareEventRows, readEvents } from "../modules/events/service/events-service.js" const eventsRoutes: FastifyPluginAsyncZod = async (app) => { app.get("/events", async (req, reply) => { @@ -16,7 +16,7 @@ const eventsRoutes: FastifyPluginAsyncZod = async (app) => { if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } - await appendEvents(parsed.data.events) + await appendClientEventRows(prepareEventRows(parsed.data.events)) return reply.status(201).send({ ok: true, inserted: parsed.data.events.length }) }) } diff --git a/backend/src/routes/evobgp-catalog.ts b/backend/src/routes/evobgp-catalog.ts index 8654f559..191d3d6e 100644 --- a/backend/src/routes/evobgp-catalog.ts +++ b/backend/src/routes/evobgp-catalog.ts @@ -1,6 +1,7 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { normalizeEvoBgpBaseUrl as normalizeBaseUrl, normalizeEvoBgpApiKey as normalizeApiKey } from "@mmapp/contracts/evobgp" -import { parseEvoCatalogRaw, aggregateCommunityRouteCounts } from "../services/evobgp-catalog.js" +import { parseEvoCatalogRaw } from "../services/evobgp-catalog.js" +import { formatEvoBgpCatalog } from "../services/evobgp-catalog-model.js" interface Dependencies { read: () => Promise<{enabled:boolean;baseUrl:string;apiKey:string}> fetchCatalog: (root:string,key:string) => Promise @@ -22,101 +23,7 @@ export function createEvoBgpCatalogRoutes(deps:Dependencies):FastifyPluginAsyncZ } try { const catalog = parseEvoCatalogRaw(await deps.fetchCatalog(cred.root, cred.apiKey)) - const modules = catalog.modules.items - const modMap = new Map(modules.map((m) => [m.id, m])) - const commItems = catalog.communities.items - const commLabel = new Map() - for (const c of commItems) { - const label = (c.community && c.title) - ? `${c.title} (${c.community})` - : (c.title ?? c.community ?? c.id) - commLabel.set(c.id, label) - } - - const now = deps.now?.() ?? new Date() - const fmtTime = now.toLocaleString("ru-RU") - const fetchedAt = now.toISOString() - - const domains = catalog.domains.items.map((rowItem) => { - const mod = modMap.get(rowItem.module_id) - const cid = rowItem.entry.community_id - return { - id: `${rowItem.module_id}:${rowItem.entry.id}`, - domain: rowItem.entry.fqdn, - resolvedIp: "—", - asn: "—", - purpose: mod?.name ?? "—", - filter: cid ? (commLabel.get(cid) ?? cid) : "—", - updated: fmtTime, - enabled: true, - } - }) - - const ipRanges = catalog.ip_ranges.items.map((rowItem) => { - const mod = modMap.get(rowItem.module_id) - const cid = rowItem.entry.community_id - return { - id: `${rowItem.module_id}:${rowItem.entry.id}`, - cidr: rowItem.entry.prefix, - asn: "—", - country: "—", - purpose: mod?.name ?? "—", - filter: commLabel.get(cid) ?? cid, - updated: fmtTime, - enabled: true, - } - }) - - const asns = catalog.asns.items.map((rowItem) => { - const cid = rowItem.entry.community_id - const e = rowItem.entry - const name = (e.asn_name ?? "").trim() - const prefixes = e.prefix_count ?? null - let updated = fmtTime - const rawResolved = (e.asn_resolved_at ?? "").trim() - if (rawResolved) { - const d = new Date(rawResolved) - if (!Number.isNaN(d.getTime())) updated = d.toLocaleString("ru-RU") - } - return { - id: `${rowItem.module_id}:${e.id}`, - asn: `AS${e.asn}`, - org: name || "—", - country: "—", - prefixes, - filter: cid ? (commLabel.get(cid) ?? cid) : "—", - updated, - enabled: true, - } - }) - - const routeCountsByCommUuid = aggregateCommunityRouteCounts(catalog) - const serversByCommunityStr = await deps.serversByCommunity() - - const communities = commItems.map((c) => { - const valueStr = (c.community ?? "").trim() - return { - id: c.id, - value: c.community ?? c.id, - name: c.title ?? c.community ?? c.id, - description: "", - type: "custom" as const, - filterIds: [] as string[], - serverCount: valueStr ? (serversByCommunityStr.get(valueStr) ?? 0) : 0, - prefixCount: routeCountsByCommUuid.has(c.id) ? routeCountsByCommUuid.get(c.id)! : 0, - action: "permit" as const, - enabled: true, - } - }) - - return reply.send({ - fetchedAt, - modules: modules.map((m) => ({ id: m.id, name: m.name, type: m.type })), - domains, - ipRanges, - asns, - communities, - }) + return reply.send(formatEvoBgpCatalog(catalog, await deps.serversByCommunity(), deps.now?.())) } catch (e) { const msg = e instanceof Error ? e.message : "Ошибка EvoBGP" return reply.status(502).send({ error: msg }) diff --git a/backend/src/routes/evobgp-settings.ts b/backend/src/routes/evobgp-settings.ts index b27fdb3b..72c2a6e0 100644 --- a/backend/src/routes/evobgp-settings.ts +++ b/backend/src/routes/evobgp-settings.ts @@ -1,11 +1,10 @@ import type {FastifyPluginAsyncZod} from "@fastify/type-provider-zod" import {z} from "zod" -import {evoBgpTestDraftSchema,normalizeEvoBgpBaseUrl,evoBgpSettingsPatchSchema,evoBgpSettingsSchema,normalizeEvoBgpApiKey,type EvoBgpSavePayload} from "@mmapp/contracts/evobgp" +import {evoBgpTestDraftSchema,normalizeEvoBgpBaseUrl,evoBgpSettingsPatchSchema,normalizeEvoBgpApiKey,type EvoBgpSavePayload} from "@mmapp/contracts/evobgp" +import { publicEvoBgpSettings } from "../services/evobgp-settings-model.js" type SettingsRow = {baseUrl:string;enabled:boolean;apiKey:string} function publicSettings(row:SettingsRow){ - const parsed=evoBgpSettingsSchema.safeParse({baseUrl:row.baseUrl,enabled:row.enabled,secretConfigured:Boolean(normalizeEvoBgpApiKey(row.apiKey))}) - if(!parsed.success)throw new Error("Настройки EvoBGP не подтверждены сервером") - return parsed.data + return publicEvoBgpSettings(row) } export function createEvoBgpSettingsRoutes(deps:{read:()=>Promise;write:(patch:EvoBgpSavePayload)=>Promise;probe:(root:string,key:string)=>Promise}):FastifyPluginAsyncZod { return async app=>{ diff --git a/backend/src/routes/evobgp.ts b/backend/src/routes/evobgp.ts index 15f19054..92544908 100644 --- a/backend/src/routes/evobgp.ts +++ b/backend/src/routes/evobgp.ts @@ -1,42 +1,9 @@ -import { eq, sql } from "drizzle-orm" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { createEvoBgpCatalogRoutes } from "./evobgp-catalog.js" import { createEvoBgpSettingsRoutes } from "./evobgp-settings.js" -import { db } from "../db/index.js" -import { evobgpSettings, filterRules } from "../db/schema.js" - -async function ensureEvobgpRow() { - let row = (await db.select().from(evobgpSettings).where(eq(evobgpSettings.id, 1)).limit(1))[0] - if (!row) { - await db.insert(evobgpSettings).values({ id: 1 }).onConflictDoNothing() - row = (await db.select().from(evobgpSettings).where(eq(evobgpSettings.id, 1)).limit(1))[0] - } - return row! -} - -/** - * Уникальные серверы из локальной БД, у которых в BGP-фильтре указан community (строка AS:NNN). - * Ключ — значение `filter_rules.community` после trim (как в EvoBGP `community`). - */ -async function distinctServersByFilterCommunityValue(): Promise> { - const rows = await db - .select({ serverId: filterRules.serverId, community: filterRules.community }) - .from(filterRules) - const byVal = new Map>() - for (const r of rows) { - const v = (r.community ?? "").trim() - if (!v) continue - let set = byVal.get(v) - if (!set) { - set = new Set() - byVal.set(v, set) - } - set.add(r.serverId) - } - const out = new Map() - for (const [v, set] of byVal) out.set(v, set.size) - return out -} +import { readEvoBgpSettings, writeEvoBgpSettings } from "../services/evobgp-settings.js" +import { distinctServersByFilterCommunityValue } from "../services/evobgp-community-counts.js" +import { probeEvoBgp } from "../services/evobgp-probe.js" function problemMessage(text: string, status: number): string { try { @@ -72,22 +39,13 @@ async function fetchEvoJson(root: string, path: string, token: string): Promi const evobgpRoutes: FastifyPluginAsyncZod = async (app) => { await app.register(createEvoBgpSettingsRoutes({ - read: ensureEvobgpRow, - probe: (root,key) => fetchEvoJson(root,"/v1/modules?limit=1",key), - write: async patch => { - await ensureEvobgpRow() - const values: {baseUrl?:string;enabled?:boolean;apiKey?:string} = {} - if(patch.baseUrl!==undefined) values.baseUrl=patch.baseUrl - if(patch.enabled!==undefined) values.enabled=patch.enabled - if(patch.apiKey!==undefined) values.apiKey=patch.apiKey??"" - const [row] = await db.update(evobgpSettings).set({...values,updatedAt:sql`now()`}).where(eq(evobgpSettings.id,1)).returning() - if(!row) throw new Error("Сохранение EvoBGP не подтверждено сервером") - return row - }, + read: readEvoBgpSettings, + probe: probeEvoBgp, + write: writeEvoBgpSettings, })) await app.register(createEvoBgpCatalogRoutes({ - read: ensureEvobgpRow, + read: readEvoBgpSettings, fetchCatalog: (root,key) => fetchEvoJson(root,"/v1/router-lists/catalog",key), serversByCommunity: distinctServersByFilterCommunityValue, })) diff --git a/backend/src/routes/exec.ts b/backend/src/routes/exec.ts index 80ea1d51..16c8668b 100644 --- a/backend/src/routes/exec.ts +++ b/backend/src/routes/exec.ts @@ -4,315 +4,15 @@ import { z } from "zod" import { db } from "../db/index.js" import { servers } from "../db/schema.js" import { MikrotikClient } from "../services/mikrotik.js" -import { observedDurationMs, observedInteger } from "../services/ospf-observed-values.js" -import { ServerIdParamSchema, type ServerIdParams } from "../types/server.js" -import type { - RosIpAddress, RosInterface, RosResource, RosIdentity, RosIpRoute, - RosFirewallFilter, RosLogEntry, RosBgpSession, RosPingResult, - RosOspfNeighbor, RosOspfArea, RosOspfInstance, RosOspfInterfaceTemplate, - RosBfdSession, -} from "../types/server.js" - -// ── string helpers ───────────────────────────────────────────────────────────── - -function p(s: string | undefined, len: number): string { - return (s?.trim() || "?").padEnd(len) -} -function flag(val: string | undefined, char: string): string { - return val === "true" ? char : val === "false" ? " " : "?" -} - -// ── formatters ───────────────────────────────────────────────────────────────── - -function fmtIpAddresses(addrs: RosIpAddress[]): string { - const hdr = [ - "Flags: X - disabled, I - invalid, D - dynamic", - " # ADDRESS NETWORK INTERFACE", - ] - const rows = addrs.map((a, i) => { - const f = `${flag(a.disabled,"X")}${flag(a.invalid,"I")}${flag(a.dynamic,"D")}` - return ` ${String(i).padStart(2)} ${f} ${p(a.address,18)} ${p(a.network,15)} ${a.interface}${a.comment ? ` ; ${a.comment}` : ""}` - }) - return [...hdr, ...rows].join("\n") -} - -function fmtInterfaces(ifaces: RosInterface[]): string { - const hdr = [ - "Flags: X - disabled, D - dynamic, R - running", - " # NAME TYPE MTU MAC-ADDRESS", - ] - const rows = ifaces.map((f, i) => { - const flags = `${flag(f.disabled,"X")} ${flag(f.running,"R")}` - const name = p(f.name, 20) - const type = p(f.type, 12) - const mtu = p(f["actual-mtu"] ?? f.mtu, 6) - const mac = f["mac-address"] ?? "" - return ` ${String(i).padStart(2)} ${flags} ${name} ${type} ${mtu} ${mac}${f.comment ? ` ; ${f.comment}` : ""}` - }) - return [...hdr, ...rows].join("\n") -} - -function fmtResource(r: RosResource, id: RosIdentity): string { - const kv = (k: string, v: string) => `${k.padStart(30)}: ${v}` - const mib = (b: string | undefined) => b && /^\d+$/.test(b) && Number.isSafeInteger(Number(b)) ? `${(parseInt(b) / 1024 / 1024).toFixed(1)} MiB` : "?" - return [ - kv("uptime", r["uptime"] ?? "?"), - kv("version", r["version"] ?? "?"), - kv("build-time", r["build-time"] ?? "?"), - kv("free-memory", mib(r["free-memory"])), - kv("total-memory", mib(r["total-memory"])), - kv("cpu", r["cpu"] ?? "?"), - kv("cpu-count", r["cpu-count"] ?? "?"), - kv("cpu-frequency", r["cpu-frequency"] ? `${r["cpu-frequency"]} MHz` : "?"), - kv("cpu-load", r["cpu-load"] ? `${r["cpu-load"]}%` : "?"), - kv("free-hdd-space", mib(r["free-hdd-space"])), - kv("total-hdd-space", mib(r["total-hdd-space"])), - kv("architecture-name",r["architecture-name"] ?? "?"), - kv("board-name", r["board-name"] ?? "?"), - kv("platform", r["platform"] ?? "?"), - kv("identity", id.name ?? "?"), - ].join("\n") -} - -function fmtIpRoutes(routes: RosIpRoute[]): string { - const hdr = [ - "Flags: X - disabled, A - active, D - dynamic, C - connect, S - static,", - " r - rip, b - bgp, o - ospf, B - blackhole, U - unreachable, P - prohibit", - " # DST-ADDRESS PREF-SRC GATEWAY DIST", - ] - const rows = routes.map((r, i) => { - const f = [ - flag(r.active, "A"), - flag(r.dynamic, "D"), - flag(r.connect, "C"), - flag(r.static, "S"), - flag(r.bgp, "b"), - flag(r.ospf, "o"), - flag(r.blackhole, "B"), - flag(r.unreachable, "U"), - flag(r.prohibit, "P"), - ].join("") - const dst = p(r["dst-address"] ?? "", 18) - const pref = p(r["pref-src"] ?? "", 15) - const gw = p(r["gateway"] ?? r["interface"] ?? "", 16) - const dist = r["distance"] ?? "?" - return ` ${String(i).padStart(2)} ${f} ${dst} ${pref} ${gw} ${dist}` - }) - return [...hdr, ...rows].join("\n") -} - -function fmtBgpSessions(sessions: RosBgpSession[]): string { - const hdr = [ - "Flags: E - established", - " # NAME REMOTE-AS REMOTE-ADDRESS STATE UPTIME", - ] - const rows = sessions.map((s, i) => { - const est = s["established"] === "true" || s["state"] === "established" - const fl = est ? "E" : " " - const name = p(s["name"] ?? s[".id"], 23) - const remAs = p(s["remote.as"], 10) - const remIp = p((s["remote.address"] ?? "").split("/")[0], 22) - const state = est ? "established" : (s["state"] ?? "?") - const up = s["uptime"] ?? "—" - return ` ${String(i).padStart(2)} ${fl} ${name} ${remAs} ${remIp} ${p(state, 12)} ${up}` - }) - return [...hdr, ...rows].join("\n") -} - -function fmtOspfNeighbors(neighbors: RosOspfNeighbor[]): string { - const hdr = [ - "Flags: V - virtual", - " # ROUTER-ID STATE CHANGES ADJACENCY INTERFACE", - ] - const rows = neighbors.map((n, i) => { - return ` ${String(i).padStart(2)} ${p(n["router-id"],16)} ${p(n["state"],8)} ${p(n["state-changes"] ?? "?",8)} ${p(n["adjacency"] ?? "—",15)} ${n["interface"] ?? ""}` - }) - return [...hdr, ...rows].join("\n") -} - -function fmtOspfAreas(areas: RosOspfArea[]): string { - const hdr = [" # NAME AREA-ID TYPE INSTANCE"] - const rows = areas.map((a, i) => - ` ${String(i).padStart(2)} ${p(a.name,20)} ${p(a["area-id"] ?? "?",15)} ${p(a.type ?? "?",12)} ${a.instance}` - ) - return [...hdr, ...rows].join("\n") -} - -function fmtOspfInstances(insts: RosOspfInstance[]): string { - const hdr = [" # NAME ROUTER-ID VER DISTRIBUTE"] - const rows = insts.map((inst, i) => - ` ${String(i).padStart(2)} ${p(inst.name,15)} ${p(inst["router-id"],18)} ${p(inst.version ?? "?",4)} ${inst["redistribute"] ?? "—"}` - ) - return [...hdr, ...rows].join("\n") -} - -function fmtOspfIfaceTemplates(templates: RosOspfInterfaceTemplate[]): string { - const hdr = [" # INTERFACES AREA COST TYPE BFD"] - const rows = templates.map((t, i) => - ` ${String(i).padStart(2)} ${p(t.interfaces ?? "—",21)} ${p(t.area,19)} ${p(t.cost ?? "?",5)} ${p(t.type ?? "?",10)} ${t["use-bfd"] === "true" ? "yes" : t["use-bfd"] === "false" ? "no" : "?"}` - ) - return [...hdr, ...rows].join("\n") -} - -function fmtBfdSessions(sessions: RosBfdSession[]): string { - const hdr = [" # LOCAL-ADDRESS REMOTE-ADDRESS STATE UPTIME"] - const rows = sessions.map((s, i) => - ` ${String(i).padStart(2)} ${p(s["local-address"],27)} ${p(s["remote-address"],27)} ${p(s["state"] ?? "?",8)} ${s["uptime"] ?? "—"}` - ) - return [...hdr, ...rows].join("\n") -} - -function fmtFirewallFilter(rules: RosFirewallFilter[]): string { - const lines: string[] = ["Flags: X - disabled, I - invalid, D - dynamic"] - rules.forEach((r, i) => { - const f = `${flag(r.disabled,"X")}${flag(r.invalid,"I")}${flag(r.dynamic,"D")}` - lines.push(``) - lines.push(` ${String(i).padStart(2)} ${f}`) - if (r.comment) lines.push(` ;;; ${r.comment}`) - const parts = [`chain=${r.chain}`, `action=${r.action}`] - if (r.protocol) parts.push(`protocol=${r.protocol}`) - if (r["connection-state"])parts.push(`connection-state=${r["connection-state"]}`) - if (r["src-address"]) parts.push(`src-address=${r["src-address"]}`) - if (r["dst-address"]) parts.push(`dst-address=${r["dst-address"]}`) - if (r["src-address-list"])parts.push(`src-address-list=${r["src-address-list"]}`) - if (r["dst-address-list"])parts.push(`dst-address-list=${r["dst-address-list"]}`) - if (r["src-port"]) parts.push(`src-port=${r["src-port"]}`) - if (r["dst-port"]) parts.push(`dst-port=${r["dst-port"]}`) - if (r["in-interface"]) parts.push(`in-interface=${r["in-interface"]}`) - if (r["out-interface"]) parts.push(`out-interface=${r["out-interface"]}`) - if (r["tls-host"]) parts.push(`tls-host=${r["tls-host"]}`) - lines.push(` ${parts.join(" ")}`) - if (r.packets != null || r.bytes != null) lines.push(` packets=${r.packets ?? "?"} bytes=${r.bytes ?? "?"}`) - }) - return lines.join("\n") -} - -function fmtLogs(entries: RosLogEntry[]): string { - if (entries.length === 0) return "(no log entries)" - return entries.map(e => - `${(e.time ?? "").padEnd(20)} ${(e.topics ?? "").padEnd(28)} ${e.message ?? ""}` - ).join("\n") -} - -function fmtPing(results: RosPingResult[], host: string): string { - const lines = [`PING ${host}`] - // individual replies - for (const r of results) { - if (r.status === "timeout") { - lines.push(` seq=${r.seq??"?"} timeout`) - } else if(r.time!=null||r.ttl!=null) { - lines.push(` seq=${r.seq??"?"} ttl=${r.ttl ?? "?"} time=${r.time ?? "?"}`) - } - } - // summary — last item with sent/received fields - const sum = [...results].reverse().find(r => r.sent != null) - if (sum) { - const loss = sum["packet-loss"] ?? "?" - lines.push(` sent=${sum.sent} received=${sum.received??"?"} packet-loss=${loss}`) - const average = sum["avg-rtt"] - const received = typeof sum.received === "string" ? observedInteger(sum.received, 1) : null - lines.push(` avg-rtt=${received !== null && typeof average === "string" && observedDurationMs(average) !== null ? average : "?"}`) - } - return lines.join("\n") -} - -// ── help text ────────────────────────────────────────────────────────────────── - -const HELP_TEXT = [ - "Доступные команды (RouterOS REST API):", - " ip address print — IP-адреса", - " ip route print — таблица маршрутов", - " interface print — интерфейсы", - " system resource print — ресурсы системы", - " system identity print — имя роутера", - " ip firewall filter print — правила firewall", - " routing bgp session print — BGP-сессии", - " routing ospf neighbor print — OSPF-соседи", - " routing ospf area print — OSPF-зоны", - " routing ospf instance print — OSPF-инстансы", - " routing ospf interface print — OSPF-интерфейсы", - " routing bfd session print — BFD-сессии", - " log print [limit=N] — системный лог", - " ping [count=N] — пинг", -].join("\n") - -// ── command dispatcher ───────────────────────────────────────────────────────── - -type ExecClient=Pick -class ExecValidationError extends Error { readonly statusCode=400 } - -async function dispatch(client: ExecClient, raw: string): Promise { - // strip leading slashes, normalize whitespace - const cmd = raw.trim().replace(/^\/+/, "") - const lower = cmd.toLowerCase().replace(/\s+/g, " ") - - if (lower === "?" || lower === "help") return HELP_TEXT - - if (lower === "ip address print") - return fmtIpAddresses(await client.getIpAddresses()) - - if (lower === "interface print") - return fmtInterfaces(await client.getInterfaces()) - - if (lower === "system resource print") { - const [res, id] = await Promise.all([client.getResource(), client.getIdentity()]) - return fmtResource(res, id) - } - - if (lower === "system identity print") - return ` name: ${(await client.getIdentity()).name}` - - if (lower === "ip route print") - return fmtIpRoutes(await client.getIpRoutes()) - - if (lower === "ip firewall filter print") - return fmtFirewallFilter(await client.getFirewallFilters()) - - if (lower === "routing bgp session print" || lower === "bgp session print" || lower === "bgp peer print") - return fmtBgpSessions(await client.getBgpSessions()) - - if (lower === "routing ospf neighbor print" || lower === "ospf neighbor print") - return fmtOspfNeighbors(await client.getOspfNeighbors()) - - if (lower === "routing ospf area print" || lower === "ospf area print") - return fmtOspfAreas(await client.getOspfAreas()) - - if (lower === "routing ospf instance print" || lower === "ospf instance print") - return fmtOspfInstances(await client.getOspfInstances()) - - if (lower === "routing ospf interface print" || lower === "routing ospf interface-template print") - return fmtOspfIfaceTemplates(await client.getOspfInterfaceTemplates()) - - if (lower === "routing bfd session print" || lower === "bfd session print") - return fmtBfdSessions(await client.getBfdSessions()) - - if (/^log print(?:\s|$)/.test(lower)) { - const match=lower.match(/^log print(?: limit=(\d+))?$/) - if(!match)throw new ExecValidationError("Используйте log print [limit=N].") - const limit=match[1]?Number(match[1]):50 - if(!Number.isSafeInteger(limit)||limit<1)throw new ExecValidationError("limit должен быть положительным целым числом.") - return fmtLogs(await client.getLogs(Math.min(limit,200))) - } - if(lower.split(" ")[0]==="ping") { - const parts=cmd.replace(/\s+/g," ").split(" ") - if(parts.length<2||parts.length>3)throw new ExecValidationError("Используйте ping [count=N].") - const match=parts[2]?.match(/^count=(\d+)$/i) - if(parts[2]&&!match)throw new ExecValidationError("count должен быть положительным целым числом.") - const count=match?Number(match[1]):4 - if(!Number.isSafeInteger(count)||count<1)throw new ExecValidationError("count должен быть положительным целым числом.") - return fmtPing(await client.ping(parts[1],Math.min(count,20)),parts[1]) - } - throw new ExecValidationError("Команда не поддерживается REST терминалом. Введите help для списка.") -} - -// ── route plugin ─────────────────────────────────────────────────────────────── +import { ServerIdParamSchema, type ServerIdParams } from "../types/server.js" +import { dispatchTerminalCommand, ExecValidationError, type TerminalDiagnosticClient } from "../services/terminal-diagnostics.js" const ExecBodySchema = z.object({ command: z.string().min(1).max(1024), }) type ExecServer=typeof servers.$inferSelect -export interface ExecDependencies {getServer:(id:number)=>Promise;createClient:(server:ExecServer)=>ExecClient} +export interface ExecDependencies {getServer:(id:number)=>Promise;createClient:(server:ExecServer)=>TerminalDiagnosticClient} const defaults:ExecDependencies={getServer:async(id)=>(await db.select().from(servers).where(eq(servers.id,id)).limit(1))[0],createClient:server=>MikrotikClient.fromServer(server)} export function createExecRoutes(dependencies:ExecDependencies=defaults):FastifyPluginAsyncZod { return async app=>{ @@ -321,7 +21,7 @@ export function createExecRoutes(dependencies:ExecDependencies=defaults):Fastify if(!server)return reply.status(404).send({error:"Устройство не найдено"}) if(!server.enabled)return reply.status(409).send({error:"Устройство отключено: выполнение команд недоступно"}) try { - const output=await dispatch(dependencies.createClient(server),req.body.command) + const output=await dispatchTerminalCommand(dependencies.createClient(server),req.body.command) return reply.send({output}) } catch(err) { const error=err instanceof Error?err.message:"Не удалось получить вывод команды." diff --git a/backend/src/routes/filters.ts b/backend/src/routes/filters.ts index d0d230fb..c40a1951 100644 --- a/backend/src/routes/filters.ts +++ b/backend/src/routes/filters.ts @@ -1,4 +1,4 @@ -import { and, asc, eq } from "drizzle-orm" +import { and, asc, eq, inArray } from "drizzle-orm" import { z } from "zod" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { db } from "../db/index.js" @@ -7,9 +7,15 @@ import { MikrotikClient } from "../services/mikrotik.js" import { pollGreCatalog } from "../services/gre-catalog-poll.js" import { parseKeepalive } from "../services/gre-live.js" import { parseDbServerId } from "../utils/server-id.js" +import { observeBgpFilterRules } from "../services/filter-rules-observed.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../services/config-mutation-lock.js" import { appendEvent } from "../modules/events/service/events-service.js" -import { managedComment } from "../managed-markers.js" -import { planBgpInApply } from "../services/config-apply-plan.js" +import { managedComment, hasManagedCommentPrefix } from "../managed-markers.js" +import { compileManagedFilterRules, filterRuleBranches, filterRuleDescriptions, filterRuleToken, validatedFilterRules } from "../services/filter-rules-write-model.js" +import { mutateManagedFilter, type readManagedFilterState } from "../services/filter-rules-mutation.js" +import { McpError } from "../modules/mcp/security.js" +import { executeGuardedApplicationWrite } from "../services/config-mutation-policy.js" +import { nativeBgpRows } from "../services/bgp-export-model.js" import { appendRevisionIfChanged, canonicalFilterRules, @@ -65,7 +71,7 @@ interface LiveGreTunnel { status: "up" | "down" | "degraded" } -interface ApiFilterRule { +export interface ApiFilterRule { id: string community: string communityName?: string @@ -89,7 +95,8 @@ function parseInnerIps(rule: string | undefined): { localInnerIp: string; remote } function parseFilterRule(raw: RosFilterRule): ApiFilterRule[] { - const text = raw.rule ?? "" + const originalText = raw.rule ?? "" + const text = originalText.replace(/^\s*#[^\r\n]*(?:\r?\n|$)/gm, "") const out: ApiFilterRule[] = [] const extractCommunities = (src: string): string[] => { @@ -106,11 +113,7 @@ function parseFilterRule(raw: RosFilterRule): ApiFilterRule[] { * членов сгруппированного блока в одну общую подпись. */ const extractDescMap = (src: string): Map => { - const m = new Map() - for (const cm of src.matchAll(/#\s*(\d+:\d+)\s*:\s*([^\n]+)/g)) { - m.set(cm[1], cm[2].trim()) - } - return m + return filterRuleDescriptions(src) } /** Первый произвольный `#` комментарий — fallback, если нет per-community. */ @@ -120,22 +123,22 @@ function parseFilterRule(raw: RosFilterRule): ApiFilterRule[] { const rawComment = raw.comment?.trim() ?? "" // Парсим по веткам if/else if, чтобы action применялся к "своим" communities. - const branches = [...text.matchAll(/(?:if|else\s+if)\s*\(([\s\S]*?)\)\s*\{([\s\S]*?)\}/gi)] + const branches = filterRuleBranches(originalText) if (branches.length > 0) { for (const b of branches) { - const condition = b[1] ?? "" - const body = b[2] ?? "" + const condition = b.condition + const body = b.body const comms = extractCommunities(condition) if (comms.length === 0) continue const isBlackhole = /set\s+type\s+blackhole/i.test(body) || /set\s+blackhole\s+(?:yes|true)/i.test(body) - const gwToken = body.match(/set\s+gw(?:ateway)?\s+([^\s;]+)/i)?.[1] ?? "" - const outIface = body.match(/set\s+out-interface\s+([^\s;]+)/i)?.[1] ?? "" + const gwToken = filterRuleToken(body, "gw") + const outIface = filterRuleToken(body, "interface") - const descMap = extractDescMap(body) - const fallbackInline = firstInlineComment(body) + const descMap = extractDescMap(originalText) + const fallbackInline = firstInlineComment(originalText) for (const community of comms) { const perComm = descMap.get(community)?.trim() ?? "" @@ -145,7 +148,7 @@ function parseFilterRule(raw: RosFilterRule): ApiFilterRule[] { action: isBlackhole ? "blackhole" : "route", gateway: isBlackhole ? "" : gwToken, gatewayTunnelId: isBlackhole ? "" : outIface, - description: perComm || fallbackInline || rawComment, + description: descMap.has(community) ? perComm : fallbackInline || rawComment, }) } } @@ -158,10 +161,10 @@ function parseFilterRule(raw: RosFilterRule): ApiFilterRule[] { const isBlackhole = /set\s+type\s+blackhole/i.test(text) || /set\s+blackhole\s+(?:yes|true)/i.test(text) - const gwToken = text.match(/set\s+gw(?:ateway)?\s+([^\s;]+)/i)?.[1] ?? "" - const outIface = text.match(/set\s+out-interface\s+([^\s;]+)/i)?.[1] ?? "" - const flatDescMap = extractDescMap(text) - const flatFallback = firstInlineComment(text) + const gwToken = filterRuleToken(text, "gw") + const outIface = filterRuleToken(text, "interface") + const flatDescMap = extractDescMap(originalText) + const flatFallback = firstInlineComment(originalText) return communities.map((community, idx) => { const perComm = flatDescMap.get(community)?.trim() ?? "" return { @@ -170,7 +173,7 @@ function parseFilterRule(raw: RosFilterRule): ApiFilterRule[] { action: isBlackhole ? "blackhole" : "route", gateway: isBlackhole ? "" : gwToken, gatewayTunnelId: isBlackhole ? "" : outIface, - description: perComm || flatFallback || rawComment, + description: flatDescMap.has(community) ? perComm : flatFallback || rawComment, } }) } @@ -184,13 +187,24 @@ function gatewayFromRecursiveDst(dstAddress: string): string { * Импорт с роутера даёт только `set gateway` без `set out-interface` — в таком виде не отличить от «поломанного» GRE. * Сопоставляем hop с локальной таблицей recursive_routes и восстанавливаем gatewayTunnelId = rec:. */ -async function enrichRulesWithRecursiveGateway(serverId: number, rules: ApiFilterRule[]): Promise { +async function enrichRulesWithRecursiveGateway(serverId: number, rules: ApiFilterRule[], cachedDescriptions: ReadonlySet): Promise { const rows = await db .select() .from(recursiveRoutes) .where(and(eq(recursiveRoutes.serverId, serverId), eq(recursiveRoutes.disabled, false))) + const cachedNames = await db.select({ community: filterRules.community, name: filterRules.communityName, description: filterRules.description }).from(filterRules).where(eq(filterRules.serverId, serverId)) + const labels = new Map>() + const descriptions = new Map>() + for (const row of cachedNames) { + const names = labels.get(row.community) ?? new Set(); names.add(row.name); labels.set(row.community, names) + const notes = descriptions.get(row.community) ?? new Set(); notes.add(row.description); descriptions.set(row.community, notes) + } return rules.map(rule => { + const names = labels.get(rule.community), name = names?.size === 1 ? [...names][0] : null + if (name !== null && name !== undefined) rule = { ...rule, communityName: name } + const notes = descriptions.get(rule.community) + if (cachedDescriptions.has(rule.id) && notes?.size === 1) rule = { ...rule, description: [...notes][0] } if (rule.action === "blackhole") return rule const tid = (rule.gatewayTunnelId ?? "").trim() if (tid) return rule @@ -207,12 +221,14 @@ async function enrichRulesWithRecursiveGateway(serverId: number, rules: ApiFilte }) } -async function fetchServerFilters(server: ServerRow) { +export async function fetchServerFilters(server: ServerRow) { + if (!server.enabled) throw new Error("Сервер выключен. Опрос фильтров недоступен.") const client = MikrotikClient.fromServer(server) const [greRaw, filterRaw] = await Promise.all([ client.get("/interface/gre"), client.get("/routing/filter/rule"), ]) + const nativeRules = observeBgpFilterRules(filterRaw) const tunnels: LiveGreTunnel[] = greRaw.map((g, idx) => { const keepalive = parseKeepalive(g["keepalive"]) @@ -243,21 +259,31 @@ async function fetchServerFilters(server: ServerRow) { } }) + const cachedDescriptions = new Set() const rulesRaw = filterRaw - .filter(r => (r.chain ?? "").trim().toLowerCase() === "bgp-in") - .flatMap(parseFilterRule) + .filter(r => r.chain === "bgp-in") + .flatMap(row => { + const hints = parseFilterRule(row), descriptions = filterRuleDescriptions(row.rule ?? "") + if (hasManagedCommentPrefix(row.comment ?? "")) for (const rule of hints) if (!descriptions.has(rule.community)) cachedDescriptions.add(rule.id) + return hints + }) + .map(rule => { + const candidates = greRaw.filter(row => row[".id"] === rule.gatewayTunnelId && typeof row.name === "string") + return candidates.length === 1 ? { ...rule, gatewayTunnelId: candidates[0].name! } : rule + }) - const rules = await enrichRulesWithRecursiveGateway(server.id, rulesRaw) + const rules = await enrichRulesWithRecursiveGateway(server.id, rulesRaw, cachedDescriptions) return { serverId: String(server.id), rules, tunnels, + nativeRules, } } async function toApiRulesets(serverRows: ServerRow[]) { - const dbRules = await db.select().from(filterRules).orderBy(asc(filterRules.serverId), asc(filterRules.sortOrder)) + const dbRules = serverRows.length ? await db.select().from(filterRules).where(inArray(filterRules.serverId, serverRows.map(server => server.id))).orderBy(asc(filterRules.serverId), asc(filterRules.sortOrder), asc(filterRules.id)) : [] return serverRows.map(s => ({ serverId: String(s.id), rules: dbRules @@ -279,91 +305,42 @@ async function resolveRouteTargets(serverId: number, rule: ApiFilterRule): Promi if (rule.action === "blackhole") return { gateway: "", outIface: "" } const tid = (rule.gatewayTunnelId ?? "").trim() if (tid.startsWith("rec:")) { - const rid = Number.parseInt(tid.slice(4), 10) - if (!Number.isFinite(rid)) { - return { gateway: rule.gateway, outIface: "" } - } + const rid = Number(tid.slice(4)) + if (!/^rec:[1-9]\d*$/.test(tid) || !Number.isSafeInteger(rid)) throw new Error("Некорректная ссылка на рекурсивный маршрут") const row = (await db .select() .from(recursiveRoutes) .where(and(eq(recursiveRoutes.serverId, serverId), eq(recursiveRoutes.id, rid))) .limit(1))[0] - if (!row) return { gateway: rule.gateway, outIface: "" } + if (!row) throw new Error("Рекурсивный маршрут фильтра отсутствует на выбранном сервере") const gw = gatewayFromRecursiveDst(row.dstAddress) + if (!gw || rule.gateway !== gw) throw new McpError("STATE_CONFLICT", "Recursive reference и gateway изменились; перечитайте выбранный маршрут", 409) return { gateway: gw || rule.gateway, outIface: "" } } return { gateway: rule.gateway, outIface: tid } } -async function toRouterRuleBody(serverId: number, rules: ApiFilterRule[]): Promise { - if (rules.length === 0) return "" - // Группируем по эффекту (action + gateway + out-interface). Communities с одним и тем же - // `set gw` объединяются через `||` в один if-блок — компактнее и ближе к привычному - // синтаксису bgp-in на MikroTik. - // RouterOS routing filter parser НЕ поддерживает `else if` (см. error - // «expected '{' instead of 'if'»), поэтому между группами — независимые `if`-блоки; - // после `accept;` обработка правила завершается, следующие `if`-ы не запускаются. - // Эталон тела: `bgp-communities includes `, `set gw `. - type Group = { - isBlackhole: boolean - gateway: string - outIface: string - items: ApiFilterRule[] - } - const groups: Group[] = [] - const indexByKey = new Map() - - for (const rule of rules) { - const isBlackhole = rule.action === "blackhole" - const { gateway, outIface } = isBlackhole - ? { gateway: "", outIface: "" } - : await resolveRouteTargets(serverId, rule) - const key = isBlackhole ? "bh" : `rt:${gateway}:${outIface}` - let idx = indexByKey.get(key) - if (idx === undefined) { - idx = groups.length - indexByKey.set(key, idx) - groups.push({ isBlackhole, gateway, outIface, items: [] }) +export async function toRouterRuleBody(serverId: number, rules: ApiFilterRule[], interfaces: readonly { ".id"?: unknown; name?: unknown }[]): Promise { + const validated = validatedFilterRules(rules) + const resolved = [] + for (const rule of validated) { + const target = await resolveRouteTargets(serverId, rule) + if (target.outIface) { + const matching = interfaces.filter(row => row.name === target.outIface && typeof row[".id"] === "string" && /^\*[A-Za-z0-9]+$/.test(row[".id"])) + if (matching.length !== 1) throw new McpError("STATE_CONFLICT", "Не подтверждён уникальный native ID GRE gateway", 409) + target.outIface = String(matching[0].name) } - groups[idx].items.push(rule) + resolved.push({ ...rule, ...target }) } - - return groups.map(g => { - const cond = g.items - .map(r => `bgp-communities includes ${r.community}`) - .join(" || ") - // Per-community подпись `# 65001:130: Torrents` — на роутере человеко-читаемо, - // и parseFilterRule умеет извлечь её обратно в description конкретного правила. - const commentLines = g.items - .filter(r => r.description?.trim()) - .map(r => ` # ${r.community}: ${r.description.trim()}`) - - if (g.isBlackhole) { - return [ - `if (${cond}) {`, - ...commentLines, - " set type blackhole;", - " accept;", - "}", - ].join("\n") - } - const lines = [ - `if (${cond}) {`, - ...commentLines, - ` set gw ${g.gateway};`, - ] - if (g.outIface) lines.push(` set out-interface ${g.outIface};`) - lines.push(" accept;") - lines.push("}") - return lines.join("\n") - }).join("\n") + return compileManagedFilterRules(resolved) } async function replaceDbRules(serverId: number, rules: ApiFilterRule[]) { - await db.delete(filterRules).where(eq(filterRules.serverId, serverId)) + await db.transaction(async transaction => { + await transaction.delete(filterRules).where(eq(filterRules.serverId, serverId)) if (rules.length === 0) return const now = new Date().toISOString() - await db.insert(filterRules).values( + await transaction.insert(filterRules).values( rules.map((r, i) => ({ serverId, sortOrder: i, @@ -377,9 +354,10 @@ async function replaceDbRules(serverId: number, rules: ApiFilterRule[]) { updatedAt: now, })), ) + }) } -async function cacheRulesetsForServer(serverId: number): Promise { +export async function cacheRulesetsForServer(serverId: number): Promise { const rows = await db .select() .from(filterRules) @@ -396,44 +374,43 @@ async function cacheRulesetsForServer(serverId: number): Promise ({ ...ruleset, greTunnels: [] as LiveGreTunnel[], nativeRules: null as ReturnType | null, live: false, stale: false, source: "cache" as const, error: null as string | null })) + } + const readSelected = () => Promise.all(selected.map(async server => { + try { + const remote = await fetchServerFilters(server) + if (options.persistObserved) { + await replaceDbRules(server.id, remote.rules) + await appendRevisionIfChanged({ serverId: server.id, section: "filters", source: "observed", payload: canonicalFilterRules(remote.rules) }) + } + return { serverId: String(server.id), rules: options.persistObserved ? await cacheRulesetsForServer(server.id) : remote.rules, greTunnels: remote.tunnels, nativeRules: remote.nativeRules, live: true, stale: false, source: "device" as const, error: null as string | null } + } catch { + return { serverId: String(server.id), rules: await cacheRulesetsForServer(server.id), greTunnels: [] as LiveGreTunnel[], nativeRules: null, live: false, stale: true, source: "cache" as const, error: server.enabled ? "Не подтверждено чтение фильтров выбранного сервера" : "Сервер выключен. Опрос фильтров недоступен." } + } + })) + return options.persistObserved + ? withApplicationMutationLock(() => withConfigMutationLock(selected.map(server => server.id), readSelected)) : readSelected() +} + +export async function applyFiltersToServer( server: ServerRow, rules: ApiFilterRule[], source: ConfigRevisionSource, + options: { beforeWrite?: (body: string, state: Awaited>) => Promise; managedRuleComment?: string } = {}, ): Promise<{ pushed: number; action: string; conflictsRemoved: number }> { + if (!server.enabled) throw new McpError("DEVICE_UNAVAILABLE", "Сервер выключен; изменение фильтров запрещено", 409) + if (options.managedRuleComment !== undefined && !hasManagedCommentPrefix(options.managedRuleComment)) throw new McpError("PROTECTED_OBJECT", "Сохранённый comment фильтра не принадлежит приложению", 409) + rules = validatedFilterRules(rules) + return executeGuardedApplicationWrite({ kind: "filters", id: "managed-bgp-in" }, [server.id], () => withConfigMutationLock([server.id], async () => { const client = MikrotikClient.fromServer(server) - const existing = await client.get("/routing/filter/rule") - const plan = planBgpInApply(existing, rules.length) - const managedCommentValue = managedComment(server.name || server.host) - - if (plan.action === "patch" && plan.managedId) { - const ruleBody = await toRouterRuleBody(server.id, rules) - await client.patch( - `/routing/filter/rule/${encodeURIComponent(plan.managedId)}`, - { - chain: "bgp-in", - comment: managedCommentValue, - rule: ruleBody, - disabled: "no", - }, - ) - } else if (plan.action === "create") { - const ruleBody = await toRouterRuleBody(server.id, rules) - await client.post("/routing/filter/rule/add", { - chain: "bgp-in", - comment: managedCommentValue, - rule: ruleBody, - }) - } else if (plan.action === "delete" && plan.managedId) { - await client.delete( - `/routing/filter/rule/${encodeURIComponent(plan.managedId)}`, - ) - } - - for (const id of plan.conflictIds) { - await client.delete(`/routing/filter/rule/${encodeURIComponent(id)}`) - } - + const interfaces = rules.some(rule => rule.action !== "blackhole" && rule.gatewayTunnelId && !rule.gatewayTunnelId.startsWith("rec:")) ? nativeBgpRows(await client.get("/interface/gre")) : [] + const ruleBody = await toRouterRuleBody(server.id, rules, interfaces) + const result = await mutateManagedFilter(client, ruleBody, options.managedRuleComment ?? managedComment(server.name || server.host), state => options.beforeWrite?.(ruleBody, state) ?? Promise.resolve()) await replaceDbRules(server.id, rules) await appendRevisionIfChanged({ serverId: server.id, @@ -444,9 +421,10 @@ async function applyFiltersToServer( return { pushed: rules.length, - action: plan.action, - conflictsRemoved: plan.conflictIds.length, + action: result.action, + conflictsRemoved: result.conflictsRemoved, } + })) } const RevisionIdParamSchema = z.object({ id: z.string().min(1) }) @@ -479,12 +457,13 @@ const filtersRoutes: FastifyPluginAsyncZod = async (app) => { app.get("/filters/rules", async (req, reply) => { const q = req.query as { serverId?: string | number } const serverId = parseDbServerId(q.serverId) + if (q.serverId !== undefined && serverId === null) return reply.status(400).send({ error: "Некорректный serverId каталога фильтров" }) const allServers = await db.select().from(servers).where(eq(servers.enabled, true)) if (serverId === null) { - const dbRulesets = await toApiRulesets(allServers) + const cached = await readFilterRulesCatalog(allServers, { source: "cache" }) return reply.send({ - rulesets: dbRulesets, + rulesets: cached.map(row => ({ serverId: row.serverId, rules: row.rules })), greTunnels: [] as LiveGreTunnel[], live: false, stale: false, @@ -495,33 +474,8 @@ const filtersRoutes: FastifyPluginAsyncZod = async (app) => { ?? (await db.select().from(servers).where(eq(servers.id, serverId)).limit(1))[0] if (!server) return reply.status(404).send({ error: "Server not found" }) - try { - const remote = await fetchServerFilters(server) - await replaceDbRules(server.id, remote.rules) - await appendRevisionIfChanged({ - serverId: server.id, - section: "filters", - source: "observed", - payload: canonicalFilterRules(remote.rules), - }) - const cached = await cacheRulesetsForServer(server.id) - return reply.send({ - rulesets: [{ serverId: String(server.id), rules: cached }], - greTunnels: remote.tunnels, - live: true, - stale: false, - }) - } catch (err) { - app.log.warn({ serverId, err: String(err) }, "filters live GET failed, serving cache") - const cached = await cacheRulesetsForServer(server.id) - return reply.send({ - rulesets: [{ serverId: String(server.id), rules: cached }], - greTunnels: [] as LiveGreTunnel[], - live: false, - stale: true, - error: String(err), - }) - } + const [result] = await readFilterRulesCatalog([server], { source: "live", persistObserved: true }) + return reply.send({ rulesets: [{ serverId: result.serverId, rules: result.rules }], greTunnels: result.greTunnels, live: result.live, stale: result.stale, ...(result.error ? { error: result.error } : {}) }) }) app.put("/filters/rules", async (req, reply) => { @@ -535,7 +489,10 @@ const filtersRoutes: FastifyPluginAsyncZod = async (app) => { const server = (await db.select().from(servers).where(eq(servers.id, serverId)).limit(1))[0] if (!server) return reply.status(404).send({ error: "Server not found" }) - const rules = body.rules ?? [] + let rules: ApiFilterRule[] + try { rules = validatedFilterRules(body.rules ?? []) } + catch { return reply.status(400).send({ error: "Некорректный набор Community-фильтров: проверьте пары 0–65535, gateway, ссылки и повторяющиеся Community" }) } + if (!server.enabled) return reply.status(409).send({ error: "Сервер выключен; изменение фильтров запрещено" }) const source: ConfigRevisionSource = body.source === "copy" ? "copy" : "apply" try { await appendEvent({ @@ -576,7 +533,7 @@ const filtersRoutes: FastifyPluginAsyncZod = async (app) => { entityType: "server", entityId: String(serverId), }) - return reply.status(500).send({ error: String(err) }) + return reply.status(err instanceof McpError ? err.statusCode : 500).send({ error: err instanceof McpError ? err.message : "Не удалось подтвердить применение фильтров; проверьте состояние перед повтором" }) } }) diff --git a/backend/src/routes/firewall.ts b/backend/src/routes/firewall.ts index 2c044bc4..6b8b5756 100644 --- a/backend/src/routes/firewall.ts +++ b/backend/src/routes/firewall.ts @@ -1,127 +1,24 @@ +import { restoreFirewallRevision } from "../services/firewall-revision-restore.js" +import { AddressKeySchema, AddressWriteSchema, AddressPatchSchema } from "../services/firewall-address-input.js" +import { writeFirewallAddress } from "../services/firewall-address-write.js" +import { RuleWriteSchema, RulePatchSchema, RuleKeySchema } from "../services/firewall-rule-input.js" +import { writeFirewallRule } from "../services/firewall-rule-write.js" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { z } from "zod" -import { MikrotikClient, MikrotikError, encodeRosId, firewallRestPath } from "../services/mikrotik.js" +import { MikrotikError } from "../services/mikrotik.js" import { getEnabledServerById } from "../services/wireguard-live.js" import { - captureFirewallSnapshot, - fetchFirewallState, listFirewallAll, } from "../services/firewall-live.js" import { - captureAndAppendRevision, listRevisions, loadRevisionForRestore, - type ConfigRevisionSource, } from "../services/config-revisions.js" -import { parseFirewallSnapshot, planFirewallRestore } from "../services/entity-snapshots.js" -import { executeRosOps } from "../services/ros-ops.js" import { parseDbServerId } from "../utils/server-id.js" -import type { FirewallFamily, FirewallTable } from "../types/server.js" +import { moveFirewallRule, firewallMoveSchema } from "../services/firewall-rule-move.js" -const FamilySchema = z.enum(["ip", "ip6"]) -const TableSchema = z.enum(["filter", "nat", "mangle", "raw"]) -const RuleKeySchema = z.object({ - serverId: z.string().min(1), - family: FamilySchema, - table: TableSchema, - rosId: z.string().min(1), -}) - -const RuleWriteSchema = z.object({ - serverId: z.string().min(1), - family: FamilySchema, - table: TableSchema, - rosId: z.string().min(1).optional(), - chain: z.string().min(1), - action: z.string().min(1), - protocol: z.string().optional(), - srcAddress: z.string().optional(), - dstAddress: z.string().optional(), - srcAddressList: z.string().optional(), - dstAddressList: z.string().optional(), - srcPort: z.string().optional(), - dstPort: z.string().optional(), - inInterface: z.string().optional(), - outInterface: z.string().optional(), - connectionState: z.string().optional(), - comment: z.string().optional(), - disabled: z.boolean().optional(), - log: z.boolean().optional(), - logPrefix: z.string().optional(), - tlsHost: z.string().optional(), - layer7Proto: z.string().optional(), -}) - -const RulePatchSchema = RuleKeySchema.extend({ - disabled: z.boolean(), -}) - -const RuleMoveSchema = RuleKeySchema.extend({ - destinationRosId: z.string().min(1).optional(), -}) - -const AddressKeySchema = z.object({ - serverId: z.string().min(1), - family: FamilySchema, - rosId: z.string().min(1), -}) - -const AddressWriteSchema = z.object({ - serverId: z.string().min(1), - family: FamilySchema, - rosId: z.string().min(1).optional(), - list: z.string().min(1), - address: z.string().min(1), - comment: z.string().optional(), - timeout: z.string().optional(), - disabled: z.boolean().optional(), -}) - -const AddressPatchSchema = AddressKeySchema.extend({ - disabled: z.boolean(), -}) - -function toRosBody(obj: Record): Record { - const out: Record = {} - for (const [k, v] of Object.entries(obj)) { - if (v !== undefined && v !== "") out[k] = v - } - return out -} - -function ruleToRos(d: z.infer): Record { - return toRosBody({ - chain: d.chain, - action: d.action, - protocol: d.protocol && d.protocol !== "all" ? d.protocol : undefined, - "src-address": d.srcAddress, - "dst-address": d.dstAddress, - "src-address-list": d.srcAddressList, - "dst-address-list": d.dstAddressList, - "src-port": d.srcPort, - "dst-port": d.dstPort, - "in-interface": d.inInterface, - "out-interface": d.outInterface, - "connection-state": d.connectionState, - comment: d.comment, - disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined, - log: d.log === true ? "yes" : d.log === false ? "no" : undefined, - "log-prefix": d.logPrefix, - "tls-host": d.tlsHost, - "layer7-protocol": d.layer7Proto, - }) -} - -function addressToRos(d: z.infer): Record { - return toRosBody({ - list: d.list, - address: d.address, - comment: d.comment, - timeout: d.timeout, - disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined, - }) -} +const RuleMoveSchema = firewallMoveSchema.safeExtend({ serverId: z.string().min(1) }) function rosErr(e: unknown): string { if (e instanceof MikrotikError) return e.message @@ -133,18 +30,6 @@ async function requireServer(serverId: string) { return await getEnabledServerById(serverId) } -async function recordFirewall( - server: NonNullable>>, - source: ConfigRevisionSource, -) { - await captureAndAppendRevision({ - serverId: server.id, - section: "firewall", - source, - capture: () => captureFirewallSnapshot(server), - }) -} - const RevisionIdParamSchema = z.object({ id: z.string().min(1) }) const firewallRoutes: FastifyPluginAsyncZod = async (app) => { @@ -161,31 +46,29 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = firewallRestPath(body.family as FirewallFamily, body.table as FirewallTable) try { - await client.put(path, ruleToRos(body)) - await recordFirewall(server, "apply") - return reply.status(201).send({ ok: true }) + const { serverId: _serverId, family: _family, table: _table, rosId: _rosId, ...fields } = body + const result = await writeFirewallRule(server, { operation: "create", family: body.family, table: body.table, fields }) + if (!result.verified) return reply.status(502).send({ error: "Запись Firewall отправлена, но результат не подтверждён", needsReview: true }) + return reply.status(201).send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } }) app.put("/firewall/rules", async (req, reply) => { - const parsed = RuleWriteSchema.extend({ rosId: z.string().min(1) }).safeParse(req.body ?? {}) + const parsed = RuleWriteSchema.extend({ rosId: RuleKeySchema.shape.rosId }).safeParse(req.body ?? {}) if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = `${firewallRestPath(body.family as FirewallFamily, body.table as FirewallTable)}/${encodeRosId(body.rosId)}` try { - await client.patch(path, ruleToRos(body)) - await recordFirewall(server, "apply") - return reply.send({ ok: true }) + const { serverId: _serverId, family: _family, table: _table, rosId: _rosId, ...fields } = body + const result = await writeFirewallRule(server, { operation: "update", family: body.family, table: body.table, rosId: body.rosId, fields }) + if (!result.verified) return reply.status(502).send({ error: "Запись Firewall отправлена, но результат не подтверждён", needsReview: true }) + return reply.send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } @@ -199,12 +82,10 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = `${firewallRestPath(body.family, body.table)}/${encodeRosId(body.rosId)}` try { - await client.patch(path, { disabled: body.disabled ? "yes" : "no" }) - await recordFirewall(server, "apply") - return reply.send({ ok: true }) + const result = await writeFirewallRule(server, { operation: "toggle", family: body.family, table: body.table, rosId: body.rosId, disabled: body.disabled }) + if (!result.verified) return reply.status(502).send({ error: "Запись Firewall отправлена, но результат не подтверждён", needsReview: true }) + return reply.send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } @@ -218,12 +99,10 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = `${firewallRestPath(body.family, body.table)}/${encodeRosId(body.rosId)}` try { - await client.delete(path) - await recordFirewall(server, "apply") - return reply.send({ ok: true }) + const result = await writeFirewallRule(server, { operation: "delete", family: body.family, table: body.table, rosId: body.rosId }) + if (!result.verified) return reply.status(502).send({ error: "Запись Firewall отправлена, но результат не подтверждён", needsReview: true }) + return reply.send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } @@ -237,15 +116,11 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = `${firewallRestPath(body.family, body.table)}/move` try { - await client.post(path, { - numbers: body.rosId, - ...(body.destinationRosId ? { destination: body.destinationRosId } : {}), - }) - await recordFirewall(server, "apply") - return reply.send({ ok: true }) + const { serverId: _serverId, ...move } = body + const result = await moveFirewallRule(server, move) + if (!result.verified) return reply.status(502).send({ error: "Перемещение отправлено, но порядок Firewall не подтверждён", needsReview: true }) + return reply.send({ ok: true, changed: result.changed, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } @@ -259,30 +134,29 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) try { - await client.put(firewallRestPath(body.family, "address-list"), addressToRos(body)) - await recordFirewall(server, "apply") - return reply.status(201).send({ ok: true }) + const { serverId: _serverId, family: _family, rosId: _rosId, ...fields } = body + const result = await writeFirewallAddress(server, { operation: "create", family: body.family, fields }) + if (!result.verified) return reply.status(502).send({ error: "Запись address-list отправлена, но результат не подтверждён", needsReview: true }) + return reply.status(201).send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } }) app.put("/firewall/address-lists", async (req, reply) => { - const parsed = AddressWriteSchema.extend({ rosId: z.string().min(1) }).safeParse(req.body ?? {}) + const parsed = AddressWriteSchema.extend({ rosId: AddressKeySchema.shape.rosId }).safeParse(req.body ?? {}) if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = `${firewallRestPath(body.family, "address-list")}/${encodeRosId(body.rosId)}` try { - await client.patch(path, addressToRos(body)) - await recordFirewall(server, "apply") - return reply.send({ ok: true }) + const { serverId: _serverId, family: _family, rosId: _rosId, ...fields } = body + const result = await writeFirewallAddress(server, { operation: "update", family: body.family, rosId: body.rosId, fields }) + if (!result.verified) return reply.status(502).send({ error: "Запись address-list отправлена, но результат не подтверждён", needsReview: true }) + return reply.send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } @@ -296,12 +170,10 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = `${firewallRestPath(body.family, "address-list")}/${encodeRosId(body.rosId)}` try { - await client.patch(path, { disabled: body.disabled ? "yes" : "no" }) - await recordFirewall(server, "apply") - return reply.send({ ok: true }) + const result = await writeFirewallAddress(server, { operation: "toggle", family: body.family, rosId: body.rosId, disabled: body.disabled }) + if (!result.verified) return reply.status(502).send({ error: "Запись address-list отправлена, но результат не подтверждён", needsReview: true }) + return reply.send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } @@ -315,12 +187,10 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await requireServer(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const path = `${firewallRestPath(body.family, "address-list")}/${encodeRosId(body.rosId)}` try { - await client.delete(path) - await recordFirewall(server, "apply") - return reply.send({ ok: true }) + const result = await writeFirewallAddress(server, { operation: "delete", family: body.family, rosId: body.rosId, }) + if (!result.verified) return reply.status(502).send({ error: "Запись address-list отправлена, но результат не подтверждён", needsReview: true }) + return reply.send({ ok: true, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } @@ -345,18 +215,10 @@ const firewallRoutes: FastifyPluginAsyncZod = async (app) => { requestedServerId: parseDbServerId(body?.serverId), }) if (!loaded.ok) return reply.status(loaded.status).send({ error: loaded.error }) - const client = MikrotikClient.fromServer(loaded.server) try { - const desired = parseFirewallSnapshot(loaded.row.payload) - const state = await fetchFirewallState(loaded.server) - const ops = planFirewallRestore(desired, { - rules: state.liveRules, - addressLists: state.liveLists, - }) - await executeRosOps(client, ops) - await recordFirewall(loaded.server, "rollback") - const next = await fetchFirewallState(loaded.server) - return reply.send({ ok: true, rules: next.rules, addressLists: next.addressLists }) + const result = await restoreFirewallRevision(loaded.server, id) + if (!result.verified || !result.catalog) return reply.status(502).send({ error: "Восстановление Firewall не подтверждено; требуется сверка", needsReview: true }) + return reply.send({ ok: true, ...result.catalog, revisionRecorded: result.revisionRecorded }) } catch (e) { return reply.status(502).send({ error: `RouterOS: ${rosErr(e)}` }) } diff --git a/backend/src/routes/geoip.ts b/backend/src/routes/geoip.ts index 9cb93ea5..4caac710 100644 --- a/backend/src/routes/geoip.ts +++ b/backend/src/routes/geoip.ts @@ -1,29 +1,9 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { geoipSettingsPatchSchema } from "@mmapp/contracts/geoip" import { refreshScheduler } from "../services/scheduler.js" -import { getGeoipSettings, updateGeoipSettings } from "../services/geoip-settings.js" -import { - GEOIP_ASN_FILE, - GEOIP_COUNTRY_FILE, - geoipReadersStatus, - initGeoip, -} from "../services/traffic-flow-geoip.js" -import { collectGeoipUpdateOnce, getGeoipUpdateState } from "../services/geoip-update-collector.js" - -async function buildGeoipStatus() { - await initGeoip() - const readers = geoipReadersStatus() - return { - ready: readers.countryLoaded && readers.asnLoaded, - countryLoaded: readers.countryLoaded, - asnLoaded: readers.asnLoaded, - countryFile: GEOIP_COUNTRY_FILE, - asnFile: GEOIP_ASN_FILE, - dir: readers.dir, - running: getGeoipUpdateState().running, - settings: await getGeoipSettings(), - } -} +import { updateGeoipSettings } from "../services/geoip-settings.js" +import { buildGeoipStatus } from "../services/geoip-status.js" +import { updateGeoipManually } from "../services/geoip-manual-update.js" const geoipRoutes: FastifyPluginAsyncZod = async (app) => { app.get("/geoip", async (_req, reply) => { @@ -44,8 +24,7 @@ const geoipRoutes: FastifyPluginAsyncZod = async (app) => { app.post("/geoip/update", async (_req, reply) => { try { - const snapshot = await collectGeoipUpdateOnce({ force: true }) - return reply.send({ ok: !snapshot.fatalError && snapshot.errors.length === 0, snapshot }) + return reply.send(await updateGeoipManually()) } catch (e) { const status = (e as { statusCode?: number }).statusCode ?? 502 const msg = e instanceof Error ? e.message : String(e) diff --git a/backend/src/routes/gre.ts b/backend/src/routes/gre.ts index 7b655ab7..6cc7d52f 100644 --- a/backend/src/routes/gre.ts +++ b/backend/src/routes/gre.ts @@ -1,62 +1,22 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { z } from "zod" +import { pool } from "../db/index.js" import { greReadForUser } from "../services/gre-read.js" -import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" +import { MikrotikError } from "../services/mikrotik.js" import { getEnabledServerById } from "../services/wireguard-live.js" import { - captureGreSnapshot, - fetchGreState, - formatKeepalive, listGreTunnels, - parseKeepalive, } from "../services/gre-live.js" import { - canonicalGreSnapshot, - parseGreSnapshot, - planGreCreate, - planGreDelete, - planGreRestore, - greAddressPath, GreRestoreConflict, } from "../services/entity-snapshots.js" -import { executeRosOps } from "../services/ros-ops.js" import { - captureAndAppendRevision, listRevisions, - loadRevisionForRestore, - type ConfigRevisionSource, } from "../services/config-revisions.js" import { parseDbServerId } from "../utils/server-id.js" -import { readGreComment, writeGreComment } from "../services/gre-comment.js" - -const TunnelWriteSchema = z.object({ - serverId: z.string().min(1), - name: z.string().min(1), - rosId: z.string().optional(), - localAddress: z.string().optional(), - remoteAddress: z.string().min(1), - localInnerIp: z.string().refine(value => { - if (!value) return true - try { greAddressPath(value); return true } catch { return false } - }, "Требуется IPv4/IPv6 с префиксом").optional(), - remoteInnerIp: z.string().optional(), - comment: z.string().optional(), - enabled: z.boolean().optional(), - mtu: z.number().optional(), - keepaliveInterval: z.number().int().min(1).max(4294967295).optional(), - keepaliveRetries: z.number().int().min(1).max(4294967295).optional(), - dscp: z.union([z.literal("inherit"), z.number(), z.string()]).optional(), - clampTcpMss: z.boolean().optional(), - allowFastPath: z.boolean().optional(), - ipsecSecret: z.string().optional(), -}) - -const TunnelKeySchema = z.object({ - serverId: z.string().min(1), - rosId: z.string().optional(), - name: z.string().optional(), - disabled: z.boolean().optional(), -}) +import { GreTunnelWriteSchema, GreTunnelKeySchema, greMutationSchema } from "../services/gre-mutation-input.js" +import { mutateGreTunnel } from "../services/gre-mutation.js" +import { restoreGreRevision } from "../services/gre-revision-restore.js" const RevisionIdParamSchema = z.object({ id: z.string().min(1) }) @@ -66,44 +26,6 @@ function rosErr(e: unknown): string { return String(e) } -async function recordGre( - server: NonNullable>>, - source: ConfigRevisionSource, -) { - await captureAndAppendRevision({ - serverId: server.id, - section: "gre", - source, - capture: () => captureGreSnapshot(server), - }) -} - -function tunnelFromBody(body: z.infer & { keepaliveInterval?: number; keepaliveRetries?: number }) { - const dscp = body.dscp == null - ? "inherit" - : typeof body.dscp === "number" - ? String(body.dscp) - : body.dscp - const keepalive = formatKeepalive(body.keepaliveInterval ?? 10, body.keepaliveRetries ?? 10) - return canonicalGreSnapshot({ - tunnels: [{ - name: body.name, - localAddress: body.localAddress ?? "", - remoteAddress: body.remoteAddress, - localInnerIp: body.localInnerIp ?? "", - remoteInnerIp: body.remoteInnerIp ?? readGreComment(body.comment).remoteInnerIp, - comment: body.remoteInnerIp === undefined ? body.comment ?? "" : writeGreComment(body.comment ?? "", body.remoteInnerIp), - disabled: body.enabled === false, - mtu: body.mtu ?? 1476, - keepalive, - dscp, - clampTcpMss: body.clampTcpMss, - allowFastPath: body.allowFastPath ?? !body.ipsecSecret?.trim(), - ipsecSecret: body.ipsecSecret ?? "", - }], - }).tunnels[0]! -} - const greRoutes: FastifyPluginAsyncZod = async (app) => { app.get("/gre/tunnels", async (req, reply) => { const q = req.query as { serverId?: string | number } @@ -116,113 +38,28 @@ const greRoutes: FastifyPluginAsyncZod = async (app) => { }) }) - app.post("/gre/tunnels", async (req, reply) => { - const parsed = TunnelWriteSchema.safeParse(req.body ?? {}) - if (!parsed.success) { - return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) - } - const body = parsed.data - if (body.ipsecSecret?.trim() && body.allowFastPath === true) { - return reply.status(400).send({ error: "IPsec несовместим с FastPath. Отключите FastPath для GRE с PSK." }) - } - const server = await getEnabledServerById(body.serverId) - if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - try { - const tunnel = tunnelFromBody(body) - await executeRosOps(client, planGreCreate(tunnel)) - await recordGre(server, "apply") - const state = await fetchGreState(server) - const created = state.tunnels.find((t) => t.name === tunnel.name) - return reply.status(201).send(created ?? { ok: true, name: tunnel.name }) - } catch (e) { - return reply.status(e instanceof GreRestoreConflict ? 409 : 502).send({ error: `RouterOS: ${rosErr(e)}` }) - } - }) - - app.patch("/gre/tunnels", async (req, reply) => { - const parsed = TunnelWriteSchema.partial().required({ serverId: true }).safeParse(req.body ?? {}) - if (!parsed.success) { - return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) - } - const body = parsed.data - const server = await getEnabledServerById(body.serverId) - if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - try { - const state = await fetchGreState(server) - const live = state.gre.find((g) => - (body.rosId && g.rosId === body.rosId) || (body.name && g.name === body.name), - ) - if (!live) return reply.status(404).send({ error: "Туннель не найден" }) - if (body.rosId && body.rosId !== live.rosId) return reply.status(409).send({ error: "ID и имя GRE указывают на разные объекты" }) - if (body.name && body.name !== live.name) return reply.status(409).send({ error: "Переименование GRE требует отдельной операции" }) - if ((body.ipsecSecret ?? live.ipsecSecret).trim() && body.allowFastPath === true) { - return reply.status(400).send({ error: "IPsec несовместим с FastPath. Отключите FastPath для GRE с PSK." }) + for (const [method, operation] of [["post", "create"], ["patch", "update"], ["delete", "delete"]] as const) { + app[method]("/gre/tunnels", async (req, reply) => { + const schema = operation === "create" ? GreTunnelWriteSchema : operation === "update" ? GreTunnelWriteSchema.partial().required({ serverId: true }) : GreTunnelKeySchema + const parsed = schema.safeParse(req.body ?? {}) + if (!parsed.success) return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) + const body = parsed.data as Record + const { serverId, name, rosId, ...fields } = body + const mutation = greMutationSchema.safeParse({ operation, ...(operation === "create" ? { fields: { name, ...fields } } : { ...(name === undefined ? {} : { name }), ...(rosId === undefined ? {} : { rosId }), ...(operation === "update" ? { fields } : {}) }) }) + if (!mutation.success) return reply.status(400).send({ error: "Некорректная операция GRE", details: mutation.error.flatten() }) + const server = await getEnabledServerById(String(serverId)) + if (!server) return reply.status(404).send({ error: "Сервер не найден" }) + try { + const { tunnel, ...receipt } = await mutateGreTunnel(server, mutation.data) + if (!receipt.verified) return reply.status(502).send({ ok: false, error: "Изменение GRE не подтверждено; повторять команду нельзя", mutation: receipt }) + if (operation === "create" && tunnel) return reply.status(201).send({ ...greReadForUser(tunnel, req.authUser), mutation: receipt }) + return reply.send({ ok: true, mutation: receipt }) + } catch (error) { + const status = error && typeof error === "object" && "statusCode" in error && typeof error.statusCode === "number" ? error.statusCode : error instanceof GreRestoreConflict ? 409 : 502 + return reply.status(status).send({ error: status === 409 || status === 400 ? rosErr(error) : "Результат операции GRE не подтверждён; требуется сверка состояния" }) } - const original = state.snapshot.tunnels.find(t => t.name === live.name)! - const merged = tunnelFromBody({ - serverId: body.serverId, - name: body.name || live.name, - localAddress: body.localAddress ?? live.localAddress, - remoteAddress: body.remoteAddress || live.remoteAddress, - localInnerIp: body.localInnerIp ?? original.localInnerIp, - remoteInnerIp: body.remoteInnerIp ?? state.tunnels.find(t => t.name === live.name)?.remoteInnerIp ?? "", - comment: body.comment ?? live.comment, - enabled: body.enabled ?? !live.disabled, - mtu: body.mtu ?? live.mtu, - keepaliveInterval: body.keepaliveInterval ?? parseKeepalive(live.keepalive).interval, - keepaliveRetries: body.keepaliveRetries ?? parseKeepalive(live.keepalive).retries, - dscp: body.dscp ?? live.dscp, - clampTcpMss: body.clampTcpMss ?? live.clampTcpMss, - allowFastPath: body.allowFastPath ?? (body.ipsecSecret?.trim() ? false : live.allowFastPath), - ipsecSecret: body.ipsecSecret ?? live.ipsecSecret, - }) - // A patch to another field must not reformat an observed native timer. - if (body.keepaliveInterval === undefined && body.keepaliveRetries === undefined) { - merged.keepalive = original.keepalive - } - merged.innerAddresses = original.innerAddresses?.map(a => ({ ...a })) - if (body.localInnerIp !== undefined && body.localInnerIp !== original.localInnerIp) { - merged.innerAddresses = (merged.innerAddresses ?? []).filter(a => a.address !== original.localInnerIp) - if (body.localInnerIp && !merged.innerAddresses.some(a => a.address === body.localInnerIp)) { - merged.innerAddresses.push({ address: body.localInnerIp, disabled: false, ...(greAddressPath(body.localInnerIp) === "/ipv6/address" ? { advertise: false } : {}) }) - } - } - const ops = planGreRestore( - { tunnels: [merged] }, - { gre: [live], addrs: state.addrs.filter(a => a.interfaceName === live.name) }, - ) - await executeRosOps(state.client, ops) - await recordGre(server, "apply") - return reply.send({ ok: true }) - } catch (e) { - return reply.status(e instanceof GreRestoreConflict ? 409 : 502).send({ error: `RouterOS: ${rosErr(e)}` }) - } - }) - - app.delete("/gre/tunnels", async (req, reply) => { - const parsed = TunnelKeySchema.safeParse(req.body ?? {}) - if (!parsed.success) { - return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) - } - const body = parsed.data - const server = await getEnabledServerById(body.serverId) - if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - try { - const state = await fetchGreState(server) - const live = state.gre.find((g) => - (body.rosId && g.rosId === body.rosId) || (body.name && g.name === body.name), - ) - if (!live) return reply.status(404).send({ error: "Туннель не найден" }) - if ((body.rosId && body.rosId !== live.rosId) || (body.name && body.name !== live.name)) return reply.status(409).send({ error: "ID и имя GRE указывают на разные объекты" }) - await executeRosOps(state.client, planGreDelete(live.name, { gre: state.gre, addrs: state.addrs })) - await recordGre(server, "apply") - return reply.send({ ok: true }) - } catch (e) { - return reply.status(e instanceof GreRestoreConflict ? 409 : 502).send({ error: `RouterOS: ${rosErr(e)}` }) - } - }) - + }) + } app.get("/gre/revisions", async (req, reply) => { const q = req.query as { serverId?: string | number } const serverId = parseDbServerId(q.serverId) @@ -236,22 +73,21 @@ const greRoutes: FastifyPluginAsyncZod = async (app) => { }, async (req, reply) => { const { id } = req.params const body = req.body as { serverId?: string | number } | undefined - const loaded = await loadRevisionForRestore({ - id, - section: "gre", - requestedServerId: parseDbServerId(body?.serverId), - }) - if (!loaded.ok) return reply.status(loaded.status).send({ error: loaded.error }) + const requestedServerId = parseDbServerId(body?.serverId) + if(body?.serverId!==undefined&&requestedServerId===null)return reply.status(400).send({error:"Некорректный serverId GRE"}) + // Resolve metadata first: never fetch an unbounded/foreign revision payload. + const row=(await pool.query("SELECT server_id,section FROM config_revisions WHERE id=$1",[id])).rows[0] + if(!row||row.section!=="gre")return reply.status(404).send({error:"Ревизия GRE не найдена"}) + if(requestedServerId!==null&&requestedServerId!==Number(row.server_id))return reply.status(409).send({error:"Ревизия принадлежит другому серверу"}) + const server=await getEnabledServerById(String(row.server_id)) + if(!server)return reply.status(404).send({error:"Сервер не найден или отключён"}) try { - const desired = parseGreSnapshot(loaded.row.payload) - const state = await fetchGreState(loaded.server) - const ops = planGreRestore(desired, { gre: state.gre, addrs: state.addrs }) - await executeRosOps(state.client, ops) - await recordGre(loaded.server, "rollback") - const next = await fetchGreState(loaded.server) - return reply.send({ ok: true, tunnels: next.tunnels }) + const {tunnels,...receipt}=await restoreGreRevision(server,id) + if(!receipt.verified)return reply.status(502).send({ok:false,error:"Восстановление GRE не подтверждено; повторять команды нельзя",mutation:receipt}) + return reply.send({ ok: true, tunnels:tunnels.map(tunnel=>greReadForUser(tunnel,req.authUser)),mutation:receipt }) } catch (e) { - return reply.status(e instanceof GreRestoreConflict ? 409 : 502).send({ error: `RouterOS: ${rosErr(e)}` }) + const conflict=e instanceof GreRestoreConflict||e&&typeof e==="object"&&"statusCode"in e&&e.statusCode===409 + return reply.status(conflict?409:502).send({ error:conflict?rosErr(e):"Результат восстановления GRE не подтверждён; требуется сверка состояния" }) } }) } diff --git a/backend/src/routes/ipsec-group.test.ts b/backend/src/routes/ipsec-group.test.ts index 9cf972e4..d10f5d89 100644 --- a/backend/src/routes/ipsec-group.test.ts +++ b/backend/src/routes/ipsec-group.test.ts @@ -7,6 +7,8 @@ import { fetchIpsecState } from "../services/ipsec-live.js" import { IPSEC_POLICY_GROUP } from "../services/ipsec-config.js" import { ipsecPolicyGroupExists, putIdentity, ensurePolicyTemplate } from "../services/ipsec-ros.js" import { removeManagedIpsecServer } from "../services/ipsec-cleanup.js" +import { db, pool } from "../db/index.js" +import { servers } from "../db/schema.js" const ownComment="MikrotikManager:ipsec test" const groups=[{".id":"*DEFAULT",name:"default"},{".id":"*GROUP",name:IPSEC_POLICY_GROUP}] for(const foreign of [false,true]){const removed:string[]=[];const client={get:async(p:string)=>p==="/ip/ipsec/policy/group"?groups:p==="/ip/ipsec/policy"?[{".id":"*POLICY",group:IPSEC_POLICY_GROUP,comment:ownComment}]:p==="/ip/ipsec/identity"&&foreign?[{".id":"*F",comment:"foreign","policy-template-group":IPSEC_POLICY_GROUP}]:[],delete:async(p:string)=>{removed.push(p)}} as unknown as MikrotikClient @@ -17,6 +19,11 @@ assert.equal(identityBody?.["policy-template-group"],IPSEC_POLICY_GROUP) await ensurePolicyTemplate({get:async()=>[],put:async(_p:string,b:Record)=>{templateBody=b}} as unknown as MikrotikClient,"198.18.82.0/24","ipsec-vpn",ownComment) assert.equal(templateBody?.group,IPSEC_POLICY_GROUP) const original=MikrotikClient.fromServer;let writes=0;MikrotikClient.fromServer=()=>({get:async(p:string)=>p==="/ip/ipsec/policy/group"?groups:[],put:async()=>{writes++},patch:async()=>{writes++},delete:async()=>{writes++},post:async()=>{writes++}}) as unknown as MikrotikClient +const originalSelect=db.select,originalQuery=pool.query,originalConnect=pool.connect +const server={id:1,enabled:true,routingEndpointEpoch:"00000000-0000-4000-8000-000000000001"} as Parameters[0] +const fixtureQuery=async(sql:string)=>{if(sql.startsWith("SELECT pg_try_advisory_lock"))return{rows:[{acquired:true}]};if(sql.startsWith("SELECT pg_advisory_unlock"))return{rows:[{pg_advisory_unlock:true}]};if(sql.startsWith("SELECT object_ref FROM mcp_protections")||sql.startsWith("INSERT INTO mcp_audit("))return{rows:[]};throw Error("Unexpected SQL in standalone init group fixture: "+sql)} +pool.query=fixtureQuery as typeof pool.query;pool.connect=(async()=>({query:fixtureQuery,release:()=>{}}))as unknown as typeof pool.connect +db.select=(()=>({from:(table:unknown)=>{assert.equal(table,servers);return{where:()=>({limit:async()=>[server]})}}}))as unknown as typeof db.select const app=Fastify();app.setValidatorCompiler(validatorCompiler);app.setSerializerCompiler(serializerCompiler) -try{await app.register(createIpsecRoutes({findServer:async()=>({id:1,enabled:true} as Parameters[0]),recordRevision:async()=>{throw Error("must not record")}}),{prefix:"/api"});const r=await app.inject({method:"POST",url:"/api/ipsec/server/init",payload:{serverId:1,serverEndpoint:"192.0.2.1",poolCidr:"198.18.82.0/24",createNatRule:false}});assert.equal(r.statusCode,409);assert.equal(writes,0)}finally{await app.close();MikrotikClient.fromServer=original} +try{await app.register(createIpsecRoutes({findServer:async()=>server,recordRevision:async()=>{throw Error("must not record")}}),{prefix:"/api"});const r=await app.inject({method:"POST",url:"/api/ipsec/server/init",payload:{serverId:1,serverEndpoint:"192.0.2.1",poolCidr:"198.18.82.0/24",createNatRule:false}});assert.equal(r.statusCode,409);assert.equal(writes,0);assert.match(r.json().error,/чужим объектом/)}finally{await app.close();MikrotikClient.fromServer=original;db.select=originalSelect;pool.query=originalQuery;pool.connect=originalConnect} console.log("IPsec groups: template/client binding, foreign reuse/reference no-write guards, default retained and owned group cleanup") diff --git a/backend/src/routes/ipsec-history.test.ts b/backend/src/routes/ipsec-history.test.ts index 3836666e..992a4de8 100644 --- a/backend/src/routes/ipsec-history.test.ts +++ b/backend/src/routes/ipsec-history.test.ts @@ -4,23 +4,57 @@ import { validatorCompiler, serializerCompiler } from "@fastify/type-provider-zo import { createIpsecRoutes } from "./ipsec.js" import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" import { fetchIpsecState } from "../services/ipsec-live.js" +import { db, pool } from "../db/index.js" +import { servers } from "../db/schema.js" +import { executeGuardedConfigurationWrite } from "../services/config-mutation-policy.js" +import { configurationAuditTransactionFixture, emptyActiveRoutingProfilesSqlFixture } from "../test/fixtures/managed-mutation-sql.js" const original = MikrotikClient.fromServer -const server = { id: 1, name: "test", host: "192.0.2.1", enabled: true } as Parameters[0] -let patches = 0, recordings = 0, nativeDisabled = "false", rejectWrite = false -MikrotikClient.fromServer = () => ({ get: async (path: string) => path === "/ip/ipsec/peer" ? [{ ".id": "*P", name: "ike", "exchange-mode": "ike2", disabled: nativeDisabled }] : [], put: async () => {}, post: async () => {}, delete: async () => {}, patch: async (_path: string, body: Record) => { if (rejectWrite) throw new MikrotikError(403, "/ip/ipsec/peer", "write rejected"); patches++; nativeDisabled = body.disabled! } }) as unknown as MikrotikClient +const originalSelect=db.select,originalQuery=pool.query,originalConnect=pool.connect +const server = { id: 1, name: "test", host: "192.0.2.1", enabled: true, routingEndpointEpoch:"00000000-0000-4000-8000-000000000001" } as Parameters[0] +let patches = 0, recordings = 0, nativeDisabled = "false", rejectWrite = false,dispatches=0,reads=0 +const auditActions:string[]=[] +// Own only the shared executor's repository/lock/protection/audit boundary. +// Any other SQL fails locally rather than contacting PostgreSQL. +const fixtureQuery=async(sql:string,params?:unknown[])=>{ + const admission = configurationAuditTransactionFixture(sql, params) + if (admission) return admission + const routing=emptyActiveRoutingProfilesSqlFixture(sql,params,server.id) + if(routing)return routing + if(sql.startsWith("SELECT pg_try_advisory_lock"))return{rows:[{acquired:true}]} + if(sql.startsWith("SELECT pg_advisory_unlock"))return{rows:[{pg_advisory_unlock:true}]} + if(sql.startsWith("SELECT object_ref FROM mcp_protections"))return{rows:[]} + if(sql.startsWith("INSERT INTO mcp_audit(")){auditActions.push(String(params?.[1]));return{rows:[]}} + throw Error("Unexpected SQL in standalone IPsec history fixture: "+sql) +} +pool.query=fixtureQuery as typeof pool.query +pool.connect=(async()=>({query:fixtureQuery,release:()=>{}}))as unknown as typeof pool.connect +db.select=(()=>({from:(table:unknown)=>{assert.equal(table,servers);return{where:()=>({limit:async(count:number)=>{assert.equal(count,1);return[server]}})}}}))as unknown as typeof db.select +MikrotikClient.fromServer = () => ({ + get: async (path: string) => {reads++;return path === "/ip/ipsec/peer" ? [{ ".id": "*P", name: "ike", "exchange-mode": "ike2", disabled: nativeDisabled,comment:"MikrotikManager:ipsec" }] : []}, + patch: async (path:string,body:Record,_timeout?:number,beforeDispatch?:()=>Promise) => executeGuardedConfigurationWrite(server.id,"PATCH",path,async()=>{ + assert.equal(auditActions.at(-1),"configuration.intent","PATCH requires its durable native intent") + await beforeDispatch?.() + dispatches++ + if(rejectWrite)throw new MikrotikError(403,path,"write rejected") + patches++;nativeDisabled=body.disabled! + }), +}) as unknown as MikrotikClient try { for (const historySaved of [false, true]) { - patches = recordings = 0; nativeDisabled = "false" + patches = recordings = dispatches = reads = 0; auditActions.length=0; nativeDisabled = "false" const app = Fastify(); app.setValidatorCompiler(validatorCompiler); app.setSerializerCompiler(serializerCompiler) - await app.register(createIpsecRoutes({ findServer: async () => server, recordRevision: async () => { recordings++; return historySaved ? { created: false, revision: {} as never } : null } }), { prefix: "/api" }) + await app.register(createIpsecRoutes({ findServer: async () => server, recordRevision: async input => { recordings++;const snapshot=await input.capture() as{peers:{disabled:boolean}[]};assert.equal(snapshot.peers[0].disabled,true);return historySaved ? { created: false, revision: {} as never } : null } }), { prefix: "/api" }) const response = await app.inject({ method: "PATCH", url: "/api/ipsec/peers/1/*P", payload: { disabled: true } }) assert.equal(response.statusCode, historySaved ? 200 : 502) assert.equal(patches, 1); assert.equal(recordings, 1); assert.equal(nativeDisabled, "yes") + assert.equal(dispatches,1);assert.ok(reads>=33,"full before/dispatch/read-back catalog must be observed") if (!historySaved) { assert.equal(response.json().state, "applied"); assert.equal(response.json().historyRecorded, false); assert.match(response.json().error, /Обновите данные перед повтором/) } else assert.deepEqual(response.json(), { ok: true }) rejectWrite = true; const rejected = await app.inject({ method: "PATCH", url: "/api/ipsec/peers/1/*P", payload: { disabled: false } }); rejectWrite = false - assert.equal(rejected.statusCode, 502); assert.equal(rejected.json().state, undefined); assert.equal(recordings, 1) + assert.equal(rejected.statusCode, 502); assert.equal(rejected.json().state, "unknown"); assert.equal(recordings, 1) + assert.equal(dispatches,2);assert.equal(patches,1);assert.equal(nativeDisabled,"yes") + assert.ok(auditActions.includes("configuration.acknowledged"));assert.ok(auditActions.includes("configuration.unconfirmed")) await app.close() } -} finally { MikrotikClient.fromServer = original } -console.log("IPsec HTTP: confirmed mutation + failed/saved history, rejected write does not claim applied") +} finally { MikrotikClient.fromServer = original;db.select=originalSelect;pool.query=originalQuery;pool.connect=originalConnect } +console.log("IPsec HTTP: guarded PATCH/read-back with failed/saved history, dispatched rejection remains unknown without history or replay; standalone no-DB fixture") diff --git a/backend/src/routes/ipsec-mutation.test.ts b/backend/src/routes/ipsec-mutation.test.ts index 5ea608b3..538e279a 100644 --- a/backend/src/routes/ipsec-mutation.test.ts +++ b/backend/src/routes/ipsec-mutation.test.ts @@ -4,17 +4,65 @@ import { validatorCompiler, serializerCompiler } from "@fastify/type-provider-zo import { createIpsecRoutes } from "./ipsec.js" import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" import { fetchIpsecState } from "../services/ipsec-live.js" -const server = { id: 1, name: "test", host: "192.0.2.1", enabled: true } as Parameters[0] +import { db, pool } from "../db/index.js" +import { servers } from "../db/schema.js" +import { executeGuardedConfigurationWrite } from "../services/config-mutation-policy.js" +import { configurationAuditTransactionFixture, emptyActiveRoutingProfilesSqlFixture } from "../test/fixtures/managed-mutation-sql.js" +const server = { id: 1, name: "test", host: "192.0.2.1", enabled: true, routingEndpointEpoch: "00000000-0000-4000-8000-000000000001" } as Parameters[0] const original = MikrotikClient.fromServer -let pkiMode = "" +const originalSelect = db.select, originalQuery = pool.query, originalConnect = pool.connect +const auditActions: string[] = [] +// Standalone fixture: model only the guarded executor's repository/lock/audit SQL. +// Unexpected SQL fails instead of contacting a real PostgreSQL instance. +const fixtureQuery = async (sql: string, params?: unknown[]) => { + const admission = configurationAuditTransactionFixture(sql, params) + if (admission) return admission + const routing = emptyActiveRoutingProfilesSqlFixture(sql, params, server.id) + if (routing) return routing + if (sql.startsWith("SELECT pg_try_advisory_lock")) return { rows: [{ acquired: true }] } + if (sql.startsWith("SELECT pg_advisory_unlock")) return { rows: [{ pg_advisory_unlock: true }] } + if (sql.startsWith("SELECT object_ref FROM mcp_protections")) return { rows: [] } + if (sql.startsWith("INSERT INTO mcp_audit(")) { auditActions.push(String(params?.[1])); return { rows: [] } } + throw Error("Unexpected SQL in standalone IPsec mutation fixture: " + sql) +} +pool.query = fixtureQuery as typeof pool.query +pool.connect = (async () => ({ query: fixtureQuery, release: () => {} })) as unknown as typeof pool.connect +db.select = (() => ({ from: (table: unknown) => { assert.equal(table, servers); return { where: () => ({ limit: async (count: number) => { assert.equal(count, 1); return [server] } }) } } })) as unknown as typeof db.select +let pkiMode = "", certificateRemoved = false let writes = 0, records = 0, fault = "", certificateFixture = false, removedCertificates = 0, exports = 0, exportedName = "" const identities: Record[] = [{ ".id": "*I", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:alice", "mode-config": "mc-ipsec-alice", comment: "MikrotikManager:ipsec user=Alice" }, { ".id": "*OTHER", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:other", "mode-config": "mc-ipsec-other", comment: "MikrotikManager:ipsec user=Other" }] -function certificateRows(): Record[] {const leaf:Record={".id":"*CERT",name:"ipsec-user-shared","common-name":"shared","key-usage":"tls-client"};if(!pkiMode)return [leaf];leaf.ca="test-crl-ca";if(pkiMode==="issued")leaf.issued="true";if(pkiMode==="revoked")leaf.revoked="true";if(pkiMode==="imported")leaf.issued="false";return [leaf,{".id":"*CA",name:"test-crl-ca","common-name":"test-crl-ca","key-usage":"key-cert-sign,crl-sign",crl:"true"}];} -MikrotikClient.fromServer = () => ({ get: async (p: string) => certificateFixture && p === "/certificate" ? certificateRows() : p === "/ip/ipsec/peer" ? [{ ".id": "*P", name: "ike", "exchange-mode": "ike2" }] : p === "/ip/ipsec/identity" ? identities : p === "/ip/ipsec/mode-config" ? [{ ".id": "*MC", name: "mc-ipsec-alice", address: "198.18.81.250" }, { ".id": "*OTHERMC", name: "mc-ipsec-other", address: "198.18.81.249" }] : [], getCertificates: async () => certificateFixture ? [{name:"ipsec-user-shared"}] : [], exportCertificatePkcs12: async (args: {name:string}) => {exports++;exportedName=args.name;return {fileName:args.name+".p12",content:Buffer.from("fixture-only-archive"),passphrase:"fixture-only-pass"}}, removeCertificate: async () => { writes++; removedCertificates++ }, put: async () => { writes++ }, post: async () => { writes++ }, delete: async () => { writes++ }, patch: async (path: string, body: Record) => { writes++; if (fault === "partial" && writes === 2) throw new MikrotikError(403, path, "rejected"); Object.assign(identities.find(i => path.endsWith(i[".id"]))!, body); if (fault === "lost-ack") throw Error("socket closed after application") } }) as unknown as MikrotikClient +function certificateRows(): Record[] {const leaf:Record={".id":"*CERT",name:"ipsec-user-shared","common-name":"shared","key-usage":"tls-client"};if(!pkiMode)return certificateRemoved ? [] : [leaf];leaf.ca="test-crl-ca";if(pkiMode==="issued")leaf.issued="true";if(pkiMode==="revoked")leaf.revoked="true";if(pkiMode==="imported")leaf.issued="false";return [...(certificateRemoved ? [] : [leaf]),{".id":"*CA",name:"test-crl-ca","common-name":"test-crl-ca","key-usage":"key-cert-sign,crl-sign",crl:"true"}];} +const modeConfigs: Record[] = [{ ".id": "*MC", name: "mc-ipsec-alice", address: "198.18.81.250" }, { ".id": "*OTHERMC", name: "mc-ipsec-other", address: "198.18.81.249" }] +MikrotikClient.fromServer = () => ({ + get: async (path: string) => structuredClone(certificateFixture && path === "/certificate" ? certificateRows() : path === "/ip/ipsec/peer" ? [{ ".id": "*P", name: "ike", "exchange-mode": "ike2" }] : path === "/ip/ipsec/identity" ? identities : path === "/ip/ipsec/mode-config" ? modeConfigs : []), + getCertificates: async () => certificateFixture ? certificateRows() : [], + exportCertificatePkcs12: async (args: { name: string }) => { exports++; exportedName = args.name; return { fileName: args.name + ".p12", content: Buffer.from("fixture-only-archive"), passphrase: "fixture-only-pass" } }, + removeCertificate: async () => { writes++; removedCertificates++; certificateRemoved = true }, + put: async () => { writes++ }, post: async () => { writes++ }, + delete: async (path: string, _timeout?: number, beforeDispatch?: () => Promise) => executeGuardedConfigurationWrite(server.id, "DELETE", path, async () => { + assert.equal(auditActions.at(-1), "configuration.intent", "native DELETE must follow its durable intent") + await beforeDispatch?.() + writes++ + const split = path.lastIndexOf("/"), collection = path.slice(0, split), id = decodeURIComponent(path.slice(split + 1)) + if (collection === "/ip/ipsec/identity") { const index = identities.findIndex(row => row[".id"] === id); assert.ok(index >= 0); identities.splice(index, 1) } + else if (collection === "/ip/ipsec/mode-config") { const index = modeConfigs.findIndex(row => row[".id"] === id); assert.ok(index >= 0); modeConfigs.splice(index, 1) } + else if (collection === "/certificate") { assert.ok(certificateRows().some(row => row[".id"] === id)); removedCertificates++; certificateRemoved = true } + else assert.fail("Unexpected native DELETE in IPsec fixture: " + path) + }), + patch: async (path: string, body: Record, _timeout?: number, beforeDispatch?: () => Promise) => executeGuardedConfigurationWrite(server.id, "PATCH", path, async () => { + assert.equal(auditActions.at(-1), "configuration.intent") + await beforeDispatch?.() + writes++ + if (fault === "partial" && writes === 2) throw new MikrotikError(403, path, "rejected") + Object.assign(identities.find(row => path.endsWith(row[".id"]))!, body) + if (fault === "lost-ack") throw Error("socket closed after application") + }), +}) as unknown as MikrotikClient const app = Fastify(); app.setValidatorCompiler(validatorCompiler); app.setSerializerCompiler(serializerCompiler) await app.register(createIpsecRoutes({ findServer: async () => server, recordRevision: async () => { records++; return {created:false,revision:{} as never} } }), { prefix: "/api" }) const patch = (payload: Record) => app.inject({ method: "PATCH", url: "/api/ipsec/users/1/identity:*I", payload }) try { +writes=records=0; const invalidStatic = await patch({name:"Untouched",staticIp:""}); assert.equal(invalidStatic.statusCode,400); assert.equal(writes,0); assert.equal(records,0); assert.equal(identities[0]!.comment,"MikrotikManager:ipsec user=Alice"); for (const body of [{name:"New",staticIp:"198.18.81.249"},{name:"Other"},{remoteId:"key-id:other"}]) { writes=0; const r=await patch(body); assert.equal(r.statusCode,409); assert.equal(writes,0); assert.equal(identities[0]!.comment,"MikrotikManager:ipsec user=Alice") } writes=records=0; fault="partial"; let r=await patch({name:"New",psk:"new-test-secret"}); assert.equal(r.statusCode,502); assert.equal(r.json().state,"partial"); assert.equal(r.json().confirmedWrites,1); assert.equal(records,0); assert.equal(identities[0]!.comment,"MikrotikManager:ipsec user=New") writes=records=0; fault="lost-ack"; r=await patch({name:"Unknown"}); assert.equal(r.statusCode,502); assert.equal(r.json().state,"unknown"); assert.equal(r.json().confirmedWrites,0); assert.equal(identities[0]!.comment,"MikrotikManager:ipsec user=Unknown"); assert.equal(records,0) @@ -22,21 +70,21 @@ writes=records=0; fault=""; r=await patch({remoteId:"fresh"}); assert.equal(r.st -// A same-named certificate is a separate object, not a binding inferred from a PSK label. -certificateFixture=true;identities.splice(0,identities.length,{".id":"*UNBOUND",peer:"ike","auth-method":"pre-shared-key","remote-id":"key-id:unbound",comment:"MikrotikManager:ipsec user=shared"}); -exports=0;let exported=await app.inject({method:"POST",url:"/api/ipsec/users/1/identity:*UNBOUND/cert",payload:{passphrase:"fixture-pass"}});assert.equal(exported.statusCode,409);assert.equal(exports,0);assert.equal(exported.json().contentB64,undefined); +// Unauthenticated legacy callers cannot reach either private export path. Selector/label parity is covered by the authenticated shared SQL/HTTP profile. +certificateFixture=true;certificateRemoved=false;identities.splice(0,identities.length,{".id":"*UNBOUND",peer:"ike","auth-method":"pre-shared-key","remote-id":"key-id:unbound",comment:"MikrotikManager:ipsec user=shared"}); +exports=0;let exported=await app.inject({method:"POST",url:"/api/ipsec/users/1/identity:*UNBOUND/cert",payload:{passphrase:"fixture-pass"}});assert.equal(exported.statusCode,401);assert.equal(exports,0);assert.equal(exported.json().contentB64,undefined); writes=records=removedCertificates=0; let deletion=await app.inject({method:"DELETE",url:"/api/ipsec/users/1/identity:*UNBOUND"});assert.equal(deletion.statusCode,200);assert.equal(writes,1);assert.equal(removedCertificates,0);assert.equal(records,1); // Explicit certificate binding still permits deletion when no other references exist. -identities.splice(0,identities.length,{".id":"*BOUND",peer:"ike","auth-method":"digital-signature","remote-certificate":"ipsec-user-shared",comment:"MikrotikManager:ipsec user=shared"}); -identities[0]!.comment="MikrotikManager:ipsec user=Renamed label";exports=0;exported=await app.inject({method:"POST",url:"/api/ipsec/users/1/identity:*BOUND/cert",payload:{passphrase:"fixture-pass"}});assert.equal(exported.statusCode,200);assert.equal(exports,1);assert.equal(exportedName,"ipsec-user-shared");assert.equal(exported.json().user,"Renamed label"); +certificateRemoved=false;identities.splice(0,identities.length,{".id":"*BOUND",peer:"ike","auth-method":"digital-signature","remote-certificate":"ipsec-user-shared",comment:"MikrotikManager:ipsec user=shared"}); +identities[0]!.comment="MikrotikManager:ipsec user=Renamed label";exports=0;exported=await app.inject({method:"POST",url:"/api/ipsec/users/1/identity:*BOUND/cert",payload:{passphrase:"fixture-pass"}});assert.equal(exported.statusCode,401);assert.equal(exports,0);assert.equal(exportedName,""); writes=records=removedCertificates=0; deletion=await app.inject({method:"DELETE",url:"/api/ipsec/users/1/identity:*BOUND"});assert.equal(deletion.statusCode,200);assert.equal(writes,2);assert.equal(removedCertificates,1);assert.equal(records,1); -certificateFixture=true;identities.splice(0,identities.length,...["*FIRST","*SECOND"].map((id,n)=>({".id":id,peer:"ike","auth-method":"digital-signature","remote-certificate":"ipsec-user-shared",comment:"MikrotikManager:ipsec user=Shared"+n}))); +certificateFixture=true;certificateRemoved=false;identities.splice(0,identities.length,...["*FIRST","*SECOND"].map((id,n)=>({".id":id,peer:"ike","auth-method":"digital-signature","remote-certificate":"ipsec-user-shared",comment:"MikrotikManager:ipsec user=Shared"+n}))); for(const method of ["PATCH","DELETE"] as const){writes=records=0;const r=await app.inject({method,url:"/api/ipsec/users/1/cert:*CERT",...(method==="PATCH"?{payload:{name:"Wrong"}}:{})});assert.equal(r.statusCode,409);assert.equal(writes,0);assert.equal(records,0)} writes=records=0;r=await app.inject({method:"DELETE",url:"/api/ipsec/users/1/identity:*FIRST"});assert.equal(r.statusCode,409);assert.equal(writes,0);assert.equal(records,0); writes=records=0;r=await app.inject({method:"PATCH",url:"/api/ipsec/users/1/identity:*FIRST",payload:{name:"Selected"}});assert.equal(r.statusCode,200);assert.equal(writes,1);assert.equal(identities[0]!.comment,"MikrotikManager:ipsec user=Selected");assert.equal(identities[1]!.comment,"MikrotikManager:ipsec user=Shared1"); @@ -44,13 +92,14 @@ writes=records=0;r=await app.inject({method:"DELETE",url:"/api/ipsec/users/1/ide // A CRL issuer protects both issued and already-revoked native certificates. for(const mode of ["issued","revoked"]) { - pkiMode=mode;identities.splice(0,identities.length,{".id":"*CRL-BOUND",peer:"ike","auth-method":"digital-signature","remote-certificate":"ipsec-user-shared",comment:"MikrotikManager:ipsec user=CRL client"}); - for(const url of ["/api/ipsec/users/1/identity:*CRL-BOUND","/api/ipsec/users/1/cert:*CERT"]) {writes=records=removedCertificates=0;const r=await app.inject({method:"DELETE",url});assert.equal(r.statusCode,409);assert.match(r.json().error,/CA с CRL/);assert.equal(writes,0);assert.equal(records,0);} + pkiMode=mode;identities.splice(0,identities.length,{".id":"*CRLBOUND",peer:"ike","auth-method":"digital-signature","remote-certificate":"ipsec-user-shared",comment:"MikrotikManager:ipsec user=CRL client"}); + for(const url of ["/api/ipsec/users/1/identity:*CRLBOUND","/api/ipsec/users/1/cert:*CERT"]) {writes=records=removedCertificates=0;const r=await app.inject({method:"DELETE",url});assert.equal(r.statusCode,409);assert.match(r.json().error,/CA с CRL/);assert.equal(writes,0);assert.equal(records,0);} for(const force of ["","?force=true"]) {writes=records=0;const r=await app.inject({method:"DELETE",url:"/api/ipsec/certs/1"+force,payload:{name:"ipsec-user-shared"}});assert.equal(r.statusCode,409);assert.equal(writes,0);assert.equal(records,0);} - writes=records=removedCertificates=0;const r=await app.inject({method:"DELETE",url:"/api/ipsec/users/1/identity:*CRL-BOUND?removeCertificate=false"});assert.equal(r.statusCode,200);assert.equal(writes,1);assert.equal(removedCertificates,0);assert.equal(records,1); + writes=records=removedCertificates=0;const r=await app.inject({method:"DELETE",url:"/api/ipsec/users/1/identity:*CRLBOUND?removeCertificate=false"});assert.equal(r.statusCode,200);assert.equal(writes,1);assert.equal(removedCertificates,0);assert.equal(records,1); } for(const force of ["","?force=true"]) {writes=records=0;const r=await app.inject({method:"DELETE",url:"/api/ipsec/certs/1"+force,payload:{name:"test-crl-ca"}});assert.equal(r.statusCode,409);assert.match(r.json().error,/всю цепочку/);assert.equal(writes,0);assert.equal(records,0);} pkiMode="imported";identities.splice(0,identities.length);writes=records=0;r=await app.inject({method:"DELETE",url:"/api/ipsec/certs/1",payload:{name:"ipsec-user-shared"}});assert.equal(r.statusCode,200);assert.equal(writes,1);assert.equal(records,1);pkiMode=""; -} finally { await app.close(); MikrotikClient.fromServer=original } +assert.ok(auditActions.includes("configuration.intent")); assert.ok(auditActions.includes("configuration.acknowledged")); +} finally { await app.close(); MikrotikClient.fromServer=original; db.select=originalSelect; pool.query=originalQuery; pool.connect=originalConnect } console.log("IPsec HTTP mutations: no-write conflicts, acknowledged partial failure, applied-but-lost ack and remote ID normalization") diff --git a/backend/src/routes/ipsec-nat.test.ts b/backend/src/routes/ipsec-nat.test.ts index bbc8215d..d5dea0f0 100644 --- a/backend/src/routes/ipsec-nat.test.ts +++ b/backend/src/routes/ipsec-nat.test.ts @@ -5,6 +5,8 @@ import { createIpsecRoutes } from "./ipsec.js" import { MikrotikClient } from "../services/mikrotik.js" import { fetchIpsecState } from "../services/ipsec-live.js" import { ensureNatRule, selectIpsecNatInterface } from "../services/ipsec-ros.js" +import { db, pool } from "../db/index.js" +import { servers } from "../db/schema.js" const interfaces=[{".id":"*E",name:"ether1"},{".id":"*E2",name:"ether2"},{".id":"*PPP",name:"pppoe-out1"}] const route={".id":"*R","dst-address":"0.0.0.0/0",active:"true","routing-table":"main","immediate-gw":"192.0.2.1%ether1"} assert.equal(selectIpsecNatInterface([route,{...route,".id":"*R2"}],interfaces),"ether1") @@ -12,10 +14,14 @@ assert.equal(selectIpsecNatInterface([{...route,"immediate-gw":"pppoe-out1"}],in for(const routes of [[],[{...route,active:"false"}],[{...route,"routing-table":"other"}],[route,{...route,"immediate-gw":"192.0.2.2%ether2"}]])assert.throws(()=>selectIpsecNatInterface(routes,interfaces),/однозначно/) let writes=0 const original=MikrotikClient.fromServer -const server={id:1,enabled:true,host:"192.0.2.1"} as Parameters[0] -MikrotikClient.fromServer=()=>({get:async(p:string)=>p==="/ip/route"?[]:interfaces,put:async()=>{writes++},patch:async()=>{writes++},post:async()=>{writes++},delete:async()=>{writes++}}) as unknown as MikrotikClient +const originalSelect=db.select,originalQuery=pool.query,originalConnect=pool.connect +const server={id:1,enabled:true,host:"192.0.2.1",routingEndpointEpoch:"00000000-0000-4000-8000-000000000001"} as Parameters[0] +const fixtureQuery=async(sql:string)=>{if(sql.startsWith("SELECT pg_try_advisory_lock"))return{rows:[{acquired:true}]};if(sql.startsWith("SELECT pg_advisory_unlock"))return{rows:[{pg_advisory_unlock:true}]};if(sql.startsWith("SELECT object_ref FROM mcp_protections")||sql.startsWith("INSERT INTO mcp_audit("))return{rows:[]};throw Error("Unexpected SQL in standalone init NAT fixture: "+sql)} +pool.query=fixtureQuery as typeof pool.query;pool.connect=(async()=>({query:fixtureQuery,release:()=>{}}))as unknown as typeof pool.connect +db.select=(()=>({from:(table:unknown)=>{assert.equal(table,servers);return{where:()=>({limit:async()=>[server]})}}}))as unknown as typeof db.select +MikrotikClient.fromServer=()=>({get:async(p:string)=>p==="/interface"?interfaces:[],put:async()=>{writes++},patch:async()=>{writes++},post:async()=>{writes++},delete:async()=>{writes++}}) as unknown as MikrotikClient const app=Fastify();app.setValidatorCompiler(validatorCompiler);app.setSerializerCompiler(serializerCompiler) -try {await app.register(createIpsecRoutes({findServer:async()=>server,recordRevision:async()=>{throw Error("must not record")}}),{prefix:"/api"});const r=await app.inject({method:"POST",url:"/api/ipsec/server/init",payload:{serverId:1,serverEndpoint:"192.0.2.1",poolCidr:"198.18.82.0/24",createNatRule:true}});assert.equal(r.statusCode,409);assert.equal(writes,0);assert.match(r.json().error,/Изменений не было/)}finally{await app.close();MikrotikClient.fromServer=original} +try {await app.register(createIpsecRoutes({findServer:async()=>server,recordRevision:async()=>{throw Error("must not record")}}),{prefix:"/api"});const r=await app.inject({method:"POST",url:"/api/ipsec/server/init",payload:{serverId:1,serverEndpoint:"192.0.2.1",poolCidr:"198.18.82.0/24",createNatRule:true}});assert.equal(r.statusCode,409);assert.equal(writes,0);assert.match(r.json().error,/Изменений не было/)}finally{await app.close();MikrotikClient.fromServer=original;db.select=originalSelect;pool.query=originalQuery;pool.connect=originalConnect} for(const existing of [false,true]){let body:Record|undefined;let method="";await ensureNatRule({get:async()=>existing?[{".id":"*N",chain:"srcnat",comment:"owned","out-interface-list":"WAN"}]:[],put:async(_p:string,b:Record)=>{method="put";body=b},patch:async(_p:string,b:Record)=>{method="patch";body=b}} as unknown as MikrotikClient,"198.18.82.0/24","owned","ether1");assert.equal(method,existing?"patch":"put");assert.equal(body?.["out-interface"],"ether1");assert.equal(body?.["ipsec-policy"],"out,none");assert.equal(body?.["out-interface-list"],undefined)} console.log("IPsec NAT: verified egress, PPP/duplicates, missing/ambiguous/VRF no-write preflight and scoped create/update") diff --git a/backend/src/routes/ipsec.ts b/backend/src/routes/ipsec.ts index 50c1354c..bd9a65f8 100644 --- a/backend/src/routes/ipsec.ts +++ b/backend/src/routes/ipsec.ts @@ -1,77 +1,34 @@ +import { restoreIpsecRevisionGuarded, IpsecRevisionRestoreExecutionError } from "../services/ipsec-revision-restore.js" +import { initIpsecServerGuarded, IpsecServerInitExecutionError } from "../services/ipsec-server-init.js" +import { createIpsecUserGuarded, IpsecUserCreateExecutionError } from "../services/ipsec-user-create.js" +import { deleteIpsecServerGuarded, IpsecServerDeleteExecutionError } from "../services/ipsec-server-delete.js" +import { updateIpsecUserGuarded, IpsecUserUpdateExecutionError } from "../services/ipsec-user-update.js" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" -import type { FastifyReply } from "fastify" +import type { FastifyReply, FastifyRequest } from "fastify" +import { createIpsecExistingCertificateExportRestHttpHandlers } from "../services/ipsec-existing-certificate-export-rest-http.js" +import { createIpsecExistingCertificateExportRestProductionAdapter } from "../services/ipsec-existing-certificate-export-rest-production.js" +import { IpsecExistingCertificateExportExecutionError } from "../services/ipsec-existing-certificate-export.js" import { IPSEC_MIN_PASSPHRASE, ipsecCertDeleteRequestSchema, - ipsecCertExportByNameRequestSchema, - ipsecCertExportRequestSchema, ipsecInitRequestSchema, ipsecPeerPatchSchema, ipsecUserCreateRequestSchema, ipsecUserPatchSchema, type IpsecCertBundle, - type IpsecClientDto, } from "@mmapp/contracts/ipsec" -import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" +import { MikrotikError } from "../services/mikrotik.js" import { - IPSEC_COMMON_NAME, - IPSEC_POLICY_GROUP, - IPSEC_SERVER_CERT, - buildClientInstructions, - buildSswanConfig, - clientCertName, - identityDisplayName, - ipsecManagedComment, - ipsecUserComment, - isIke2RemoteAccessIdentity, - isIpsecManagedComment, - parseIpsecUserComment, poolRangesFromCidr, - resolveIke2CaName, - resolveIke2ServerCert, - selectIke2Peers, - selectSharedIke2Identity, - userModeConfigName, - normalizeIpsecRemoteId, } from "../services/ipsec-config.js" import { - ensureIpsecPeer, - ensureIpsecPool, - ensureIpsecProfile, - ensureIpsecProposal, - ensureNatRule, - resolveIpsecNatInterface, IpsecNatInterfaceConflict, IpsecPolicyGroupConflict, - ipsecPolicyGroupExists, - listByPath, - ensurePolicyTemplate, - ensureSharedModeConfig, - putUserModeConfig, - deleteUserModeConfig, - putIdentity, - patchIdentity, - deleteIdentity, - patchPeer, - deletePeer, - ensureSharedIke2Identity, } from "../services/ipsec-ros.js" -import { - ensureCaCertificate, - ensureServerCertificate, - exportCertificateP12ByName, - findCertificate, - issueClientCertificate, -} from "../services/ipsec-ca.js" import { captureIpsecSnapshot, - fetchIpsecRestoreState, - fetchIpsecState, getEnabledIpsecServerById, listIpsec, - mapClients, - resolveIpsecClientTarget, - mapServerSummary, } from "../services/ipsec-live.js" import { captureAndAppendRevision, @@ -79,20 +36,18 @@ import { loadRevisionForRestore, type ConfigRevisionSource, } from "../services/config-revisions.js" -import { IpsecRestoreConflict, parseIpsecSnapshot, planIpsecRestore } from "../services/entity-snapshots.js" -import { executeRosOps } from "../services/ros-ops.js" -import { removeManagedIpsecServer } from "../services/ipsec-cleanup.js" +import { IpsecRestoreConflict } from "../services/entity-snapshots.js" import { parseDbServerId } from "../utils/server-id.js" import { isIP } from "node:net" -import { randomBytes } from "node:crypto" -import { trackIpsecMutation, type IpsecMutationProgress } from "../services/ipsec-operation.js" +import type { IpsecMutationProgress } from "../services/ipsec-operation.js" import { z } from "zod" +import { deleteIpsecUserGuarded, IpsecUserDeleteExecutionError } from "../services/ipsec-user-delete.js" +import { deleteIpsecCertificateGuarded, IpsecCertificateUsedConflict } from "../services/ipsec-certificate-delete.js" +import { ipsecPeerUpdateBody, updateIpsecPeerGuarded } from "../services/ipsec-peer-update.js" +import { deleteIpsecPeerGuarded, IpsecPeerLinkedConflict } from "../services/ipsec-peer-delete.js" +import { McpError } from "../modules/mcp/security.js" -function generatePassphrase(): string { - return randomBytes(9).toString("base64url") -} - function serverIdParam(v: string): string { return decodeURIComponent(v) } @@ -101,21 +56,6 @@ function rosIdParam(v: string): string { return decodeURIComponent(v) } -function certificateDeleteConflict(state: Awaited>, name: string): string | undefined { - const cert = state.certs.find(c => (c.name ?? "").trim() === name) - if (!cert) return undefined - // Removing a CA cascades to its issued children, including foreign clients. - if (state.certs.some(c => (c.ca ?? "").trim() === name)) { - return "CA содержит выданные сертификаты. Его удаление затронет всю цепочку; отдельное удаление CA здесь недоступно" - } - const issuer = state.certs.find(c => (c.name ?? "").trim() === (cert.ca ?? "").trim()) - const revoked = cert.revoked && !["false", "no", "0"].includes(cert.revoked) - if (issuer?.crl === "true" && (cert.issued === "true" || revoked)) { - return "Сертификат выдан CA с CRL и не удаляется отдельно в RouterOS. Удалите только identity, сохранив сертификат. Отзыв выполняется отдельно" - } - return undefined -} - function errReply(reply: FastifyReply, e: unknown, progress?: IpsecMutationProgress) { if (e instanceof IpsecNatInterfaceConflict) return reply.status(409).send({ error: `${e.message}. Изменений не было` }) if (e instanceof IpsecPolicyGroupConflict) return reply.status(409).send({ error: `${e.message}. Изменений не было` }) @@ -164,51 +104,25 @@ async function recordIpsecDefault( if (!result && source !== "observed") throw new IpsecHistoryError() } -/** Существующие статические IP клиентов (персональные mode-config). */ -function takenStaticIps(modeConfigs: Array<{ name?: string; address?: string; "address-prefix"?: string }>): string[] { - return modeConfigs - .filter((m) => (m.name ?? "").startsWith("mc-ipsec-")) - .map((m) => String(m.address ?? m["address-prefix"] ?? "").replace(/\/\d+$/, "").trim()) - .filter(Boolean) -} - -async function buildCertBundle( - client: MikrotikClient, - args: { userName: string; certName?: string; serverEndpoint: string; passphrase: string; dns?: string }, -): Promise { - const certName = args.certName?.trim() || clientCertName(args.userName) - const { fileName, content, passphrase } = await exportCertificateP12ByName(client, certName, args.passphrase) - const p12B64 = content.toString("base64") - return { - user: args.userName, - serverEndpoint: args.serverEndpoint, - filename: fileName, - contentB64: p12B64, - mime: "application/x-pkcs12", - passphrase, - sswanFilename: `${certName}.sswan`, - sswanContent: buildSswanConfig({ - name: `IKEv2 ${args.serverEndpoint}`, - serverEndpoint: args.serverEndpoint, - serverId: args.serverEndpoint, - p12B64, - }), - instructions: buildClientInstructions({ - userName: args.userName, - serverEndpoint: args.serverEndpoint, - p12Filename: fileName, - passphrase, - dns: args.dns, - }), - } -} - const RevisionIdParamSchema = z.object({ id: z.string().min(1) }) const ipsecDefaults = { findServer: getEnabledIpsecServerById, recordRevision: captureAndAppendRevision } -export function createIpsecRoutes(dependencies = ipsecDefaults): FastifyPluginAsyncZod { +async function existingCertificateReply(request: FastifyRequest, reply: FastifyReply, work: (signal: AbortSignal) => Promise) { + const cancellation = new AbortController(), abort = () => { if (!reply.raw.writableFinished) cancellation.abort() } + request.raw.once("aborted", abort); reply.raw.once("close", abort) + try { return await work(cancellation.signal) } + catch (error) { + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, + ...(error instanceof IpsecExistingCertificateExportExecutionError ? { state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", needsReview: true, confirmedWrites: error.confirmedWrites, completedCommands: error.completedCommands } : {}) }) + return reply.status(502).send({ error: "Приватный экспорт не подтверждён; автоматический повтор запрещён", state: "unknown", needsReview: true }) + } finally { request.raw.off("aborted", abort); reply.raw.off("close", abort) } +} + +export function createIpsecRoutes(dependencies: typeof ipsecDefaults & { existingCertificateExport?: ReturnType } = ipsecDefaults): FastifyPluginAsyncZod { const getEnabledIpsecServerById = dependencies.findServer + // Trusted assembly seam; every production export uses the same guarded path. + const existingCertificateExport = dependencies.existingCertificateExport ?? createIpsecExistingCertificateExportRestHttpHandlers(createIpsecExistingCertificateExportRestProductionAdapter()) const recordIpsec = (server: NonNullable>>, source: ConfigRevisionSource) => recordIpsecDefault(server, source, dependencies.recordRevision) return async (app) => { app.get("/ipsec", async (req, reply) => { @@ -223,369 +137,84 @@ export function createIpsecRoutes(dependencies = ipsecDefaults): FastifyPluginAs }) app.post("/ipsec/server/init", async (req, reply) => { - const parsed = ipsecInitRequestSchema.safeParse(req.body ?? {}) - if (!parsed.success) { - return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) - } - const body = parsed.data - const ranges = poolRangesFromCidr(body.poolCidr) - if (!ranges) { - return reply.status(400).send({ error: `Некорректная подсеть пула: ${body.poolCidr}` }) - } - const server = await getEnabledIpsecServerById(body.serverId) + const parsed = ipsecInitRequestSchema.strict().safeParse(req.body ?? {}) + if (!parsed.success) return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) + if (!poolRangesFromCidr(parsed.data.poolCidr)) return reply.status(400).send({ error: `Некорректная подсеть пула: ${parsed.data.poolCidr}` }) + const server = await getEnabledIpsecServerById(parsed.data.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - const natInterface = body.createNatRule ? await resolveIpsecNatInterface(client) : undefined - const groupExists = await ipsecPolicyGroupExists(client) - const caCert = await ensureCaCertificate(client, body.caDaysValid) - const serverCert = await ensureServerCertificate(client, { - serverEndpoint: body.serverEndpoint, - caCertName: caCert, - daysValid: body.serverDaysValid, - }) - const comment = ipsecManagedComment("IKEv2 road-warrior") - await ensureIpsecProfile(client, IPSEC_COMMON_NAME, comment) - await ensureIpsecProposal(client, IPSEC_COMMON_NAME, comment) - await ensureIpsecPool(client, IPSEC_COMMON_NAME, ranges, comment) - await ensureSharedModeConfig(client, IPSEC_COMMON_NAME, IPSEC_COMMON_NAME, body.dns, comment) - if (!groupExists) await client.put("/ip/ipsec/policy/group", { name: IPSEC_POLICY_GROUP }) - await ensurePolicyTemplate(client, body.poolCidr, IPSEC_COMMON_NAME, comment) - await ensureIpsecPeer(client, IPSEC_COMMON_NAME, serverCert, IPSEC_COMMON_NAME, comment) - await ensureSharedIke2Identity(client, { peerName: IPSEC_COMMON_NAME, serverCertName: serverCert, modeConfigName: IPSEC_COMMON_NAME, comment: ipsecManagedComment("IKEv2 listener"), policyTemplateGroup: IPSEC_POLICY_GROUP }) - for (const identity of await listByPath(client, "/ip/ipsec/identity")) { - if (identity.peer === IPSEC_COMMON_NAME && isIpsecManagedComment(identity.comment) && (!identity["policy-template-group"] || identity["policy-template-group"] === "default")) await patchIdentity(client, identity[".id"]!, { "policy-template-group": IPSEC_POLICY_GROUP }) - } - if (body.createNatRule) { - await ensureNatRule(client, body.poolCidr, ipsecManagedComment("интернет клиентам VPN"), natInterface!) - } - progress.complete() - await recordIpsec(server, "apply") - const state = await fetchIpsecState(server) - return reply.status(201).send(mapServerSummary(state, mapClients(state))) - } catch (e) { - return errReply(reply, e, progress) + const result = await initIpsecServerGuarded(server, parsed.data, undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "IPsec применён, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) + return reply.status(201).send(result.summary) + } catch (error) { + if (error instanceof IpsecServerInitExecutionError) return reply.status(502).send({ error: error.message, code: error.code, state: error.confirmedWrites ? "partial" : "unknown", confirmedWrites: error.confirmedWrites, completedCommands: error.completedCommands, commandCount: error.commandCount, needsReview: true }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: "unknown", needsReview: true } : {}) }) + return errReply(reply, error) } }) app.delete("/ipsec/server/:serverId", async (req, reply) => { const { serverId } = req.params as { serverId: string } - const q = req.query as { removeCertificates?: string } - const removeCertificates = q.removeCertificates !== "false" + const removeCertificates = (req.query as { removeCertificates?: string }).removeCertificates !== "false" const server = await getEnabledIpsecServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - await removeManagedIpsecServer(client, removeCertificates) - progress.complete() - await recordIpsec(server, "apply") + const result = await deleteIpsecServerGuarded(server, removeCertificates, undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Операция выполнена на устройстве, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) return reply.send({ ok: true }) - } catch (e) { - return errReply(reply, e, progress) + } catch (error) { + if (error instanceof IpsecServerDeleteExecutionError) return reply.status(502).send({ error: error.message, code: error.code, state: error.confirmedWrites ? "partial" : "unknown", confirmedWrites: error.confirmedWrites, completedCommands: error.completedCommands, commandCount: error.commandCount, needsReview: true }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", needsReview: true } : {}) }) + return errReply(reply, error) } }) app.post("/ipsec/users", async (req, reply) => { - const parsed = ipsecUserCreateRequestSchema.safeParse(req.body ?? {}) - if (!parsed.success) { - return badBodyReply(reply, parsed.error) - } - const body = parsed.data - const server = await getEnabledIpsecServerById(body.serverId) + const parsed = ipsecUserCreateRequestSchema.strict().safeParse(req.body ?? {}) + if (!parsed.success) return badBodyReply(reply, parsed.error) + const server = await getEnabledIpsecServerById(parsed.data.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - const state = await fetchIpsecState(server) - const ike2Peers = selectIke2Peers(state.peers) - const peer = (body.peerName - ? state.peers.find((p) => (p.name ?? "").trim() === body.peerName!.trim()) - : undefined) - ?? ike2Peers.find((p) => isIpsecManagedComment(p.comment)) - ?? ike2Peers.find((p) => (p.name ?? "").trim() === IPSEC_COMMON_NAME) - ?? ike2Peers[0] - if (!peer) { - return reply.status(400).send({ error: "На роутере нет ни одного IPsec peer — создайте peer на вкладке «Сервер»" }) - } - const ike2PeerNames = ike2Peers.map((p) => (p.name ?? "").trim()).filter(Boolean) - const ike2Identities = state.identities.filter( - (i) => isIpsecManagedComment(i.comment) || isIke2RemoteAccessIdentity(i, ike2PeerNames), - ) - const idMcName = ike2Identities - .map((i) => (i["mode-config"] ?? "").trim()) - .find((n) => n && !n.startsWith("mc-ipsec-")) - const sharedMc = (idMcName ? state.modeConfigs.find((m) => (m.name ?? "").trim() === idMcName) : undefined) - ?? state.modeConfigs.find((m) => (m.name ?? "").trim() === IPSEC_COMMON_NAME) - const serverCertRow = resolveIke2ServerCert(state.certs, ike2Peers, state.identities) - const caName = resolveIke2CaName(state.certs, serverCertRow) - if (body.authMethod === "certificate" && (!caName || !serverCertRow)) { - return reply.status(400).send({ - error: "Нет CA/серверного сертификата — для сертификатного клиента запустите мастер или используйте PSK", - }) - } - if (body.peerName && (peer.name ?? "").trim() !== body.peerName.trim()) return reply.status(404).send({ error: "Выбранный peer не найден" }) - if (mapClients(state).some(c => c.name === body.name.trim()) || (body.authMethod === "certificate" && state.certs.some(c => c.name === clientCertName(body.name)))) return reply.status(409).send({ error: "Клиент уже существует" }) - const peerName = (peer.name ?? "").trim() - if (body.authMethod === "pre-shared-key" && state.identities.some(i => i.peer === peerName && i["auth-method"] === "pre-shared-key" && i["remote-id"] === normalizeIpsecRemoteId(body.remoteId ?? body.name))) return reply.status(409).send({ error: "Remote ID уже используется клиентом этого peer" }) - const serverEndpoint = String(serverCertRow?.["common-name"] ?? "").trim() || server.host - const dns = sharedMc?.["static-dns"]?.trim() || undefined - - if (body.authMethod === "pre-shared-key" && !body.psk) { - return reply.status(400).send({ error: "Для PSK-клиента укажите secret (psk)" }) - } - - const sharedIdentity = selectSharedIke2Identity(state.identities, ike2PeerNames) - const sharedIdentityId = sharedIdentity?.[".id"] ? String(sharedIdentity[".id"]) : undefined - - let modeConfig = (sharedMc?.name ?? "").trim() - if (body.staticIp) { - const ip = body.staticIp.trim() - if (isIP(ip) !== 4) return reply.status(400).send({ error: `Некорректный IP: ${ip}` }) - const taken = takenStaticIps(state.modeConfigs) - if (taken.includes(ip)) { - return reply.status(409).send({ error: `IP ${ip} уже назначен другому клиенту` }) - } - modeConfig = userModeConfigName(body.name) - await putUserModeConfig(client, modeConfig, ip, ipsecManagedComment(`клиент ${body.name.trim()}`)) - } - - let certName: string | undefined - if (body.authMethod === "certificate") { - const issued = await issueClientCertificate(client, { - userName: body.name, - caCertName: caName!, - daysValid: body.daysValid ?? 1825, - }) - if (issued.existed) { - const receipt = progress.receipt() - return reply.status(409).send({ error: `Клиент с сертификатом ${issued.certName} уже существует${receipt ? ". Часть изменений применена; обновите данные перед повтором" : ""}`, ...receipt }) - } - certName = issued.certName - } - - if (body.authMethod === "certificate" && !body.staticIp) { - // Гибрид: клиент «вешается» на общую identity peer — отдельная identity не нужна, - // RouterOS сам принимает любой клиентский серт, подписанный доверенным CA. - await ensureSharedIke2Identity(client, { - peerName, - serverCertName: String(serverCertRow?.name ?? IPSEC_SERVER_CERT), - modeConfigName: modeConfig, - comment: ipsecManagedComment("IKEv2 listener"), - }) - } else { - // PSK или персональный статический IP: отдельная identity (match-by=certificate для cert). - // place-before гарантирует матчинг раньше общей listener-identity. - await putIdentity(client, { - peerName, - modeConfig, - comment: ipsecUserComment(body.name), - authMethod: body.authMethod, - certificate: body.authMethod === "certificate" ? String(serverCertRow?.name ?? IPSEC_SERVER_CERT) : undefined, - remoteCertificate: certName, - secret: body.psk, - remoteId: body.authMethod === "pre-shared-key" ? (body.remoteId ?? body.name) : undefined, - placeBefore: sharedIdentityId, - }) - } - - let bundle: IpsecCertBundle | undefined - if (body.authMethod === "certificate" && certName) { - const passphrase = body.passphrase?.trim() || generatePassphrase() - bundle = await buildCertBundle(client, { - userName: body.name, - serverEndpoint, - passphrase, - dns, - }) - } - - progress.complete() - await recordIpsec(server, "apply") - const fresh = await fetchIpsecState(server) - const clients = mapClients(fresh) - const created: IpsecClientDto | undefined = clients.find( - (c) => c.name === body.name.trim(), - ) ?? clients.find((c) => c.certificateName === certName) - return reply.status(201).send({ client: created ?? null, bundle }) - } catch (e) { - return errReply(reply, e, progress) + const result = await createIpsecUserGuarded(server, parsed.data, undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Клиент создан на устройстве, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) + return reply.status(201).send({ client: result.client, bundle: result.bundle }) + } catch (error) { + if (error instanceof IpsecUserCreateExecutionError) return reply.status(502).send({ error: error.message, code: error.code, state: error.confirmedWrites ? "partial" : "unknown", confirmedWrites: error.confirmedWrites, completedCommands: error.completedCommands, commandCount: error.commandCount, needsReview: true }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: "unknown", needsReview: true } : {}) }) + return errReply(reply, error) } }) - app.patch("/ipsec/users/:serverId/:rosId", async (req, reply) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } - const parsed = ipsecUserPatchSchema.safeParse(req.body ?? {}) - if (!parsed.success) { - return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) - } - const d = parsed.data - if ("staticIp" in d && d.staticIp && isIP(d.staticIp.trim()) !== 4) return reply.status(400).send({ error: "Некорректный статический IPv4 адрес" }) + const parsed = ipsecUserPatchSchema.strict().safeParse(req.body ?? {}) + if (!parsed.success) return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) + // Reject invalid/empty static IPv4 before even a display-name mutation. + if (parsed.data.staticIp != null && isIP(parsed.data.staticIp.trim()) !== 4) return reply.status(400).send({ error: "Некорректный статический IPv4 адрес" }) const server = await getEnabledIpsecServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - const state = await fetchIpsecState(server) - const targetRosId = rosIdParam(rosId) - const target = resolveIpsecClientTarget(state, targetRosId) - if (target.ambiguous) return reply.status(409).send({ error: "Неоднозначный ID клиента. Обновите список IPsec" }) - let { identity, certRow } = target - if (certRow) { - identity = state.identities.find(i => i["remote-certificate"] === certRow?.name) - if (identity) certRow = undefined - } - if (!identity && !certRow) return reply.status(404).send({ error: "Клиент не найден" }) - - const ike2Peers = selectIke2Peers(state.peers) - const ike2PeerNames = ike2Peers.map((p) => (p.name ?? "").trim()).filter(Boolean) - const serverCertRow = resolveIke2ServerCert(state.certs, ike2Peers, state.identities) - const managedIdentity = identity - ? isIpsecManagedComment(identity.comment) - : String(certRow?.name ?? "").startsWith("ipsec-user-") - const oldName = identity - ? (parseIpsecUserComment(identity.comment) ?? identity.comment ?? "") - : String(certRow?.["common-name"] ?? certRow?.name ?? "").trim() - const sharedMc = state.modeConfigs.find((m) => (m.name ?? "").trim() === IPSEC_COMMON_NAME) - const sharedMcName = (sharedMc?.name ?? "").trim() - const newName = d.name?.trim() || oldName - const normalizedRemoteId = d.remoteId === undefined ? undefined : normalizeIpsecRemoteId(d.remoteId) - - if (d.name && newName !== oldName && mapClients(state).some(c => c.name === newName && (identity ? c.rosId !== `identity:${identity[".id"]}` : c.certName !== certRow?.name))) return reply.status(409).send({ error: "Имя уже используется другим клиентом" }) - if (identity && normalizedRemoteId !== undefined && state.identities.some(i => i[".id"] !== identity[".id"] && i.peer === identity.peer && i["auth-method"] === "pre-shared-key" && i["remote-id"] === normalizedRemoteId)) return reply.status(409).send({ error: "Remote ID уже используется клиентом этого peer" }) - - // Validate conflicts before changing even the display name. - if (d.staticIp) { - const currentMc = (identity?.["mode-config"] ?? "").trim() - if (takenStaticIps(state.modeConfigs.filter(m => !currentMc || m.name !== currentMc)).includes(d.staticIp.trim())) return reply.status(409).send({ error: "IP уже назначен другому клиенту" }) - } - - if (d.name && d.name !== oldName) { - if (identity) { - // managed: user=; существующий RouterOS identity: обычный comment - await patchIdentity(client, identity[".id"]!, { - comment: managedIdentity ? ipsecUserComment(d.name) : d.name.trim(), - }) - } - // Preserve the signed certificate CN/key/name; keep the editable display name in an identity. - if (certRow && !d.staticIp) { - const peer = ike2Peers.find(p => isIpsecManagedComment(p.comment)) ?? ike2Peers[0] - if (!peer || !serverCertRow) return reply.status(400).send({ error: "Нет IKEv2 peer или серверного сертификата" }) - await putIdentity(client, { peerName: peer.name ?? "", modeConfig: sharedMcName, comment: ipsecUserComment(d.name), authMethod: "certificate", certificate: serverCertRow.name, remoteCertificate: certRow.name }) - } - } - - if (identity && d.staticIp !== undefined) { - const currentMc = (identity["mode-config"] ?? "").trim() - if (d.staticIp == null) { - // вернуть выдачу из пула - if (sharedMcName) await patchIdentity(client, identity[".id"]!, { "mode-config": sharedMcName }) - const personal = currentMc && currentMc !== sharedMcName ? currentMc : "" - if (personal) await deleteUserModeConfig(client, personal) - } else { - const ip = d.staticIp.trim() - if (isIP(ip) !== 4) return reply.status(400).send({ error: `Некорректный IP: ${ip}` }) - const others = takenStaticIps( - state.modeConfigs.filter((m) => (m.name ?? "").trim() !== currentMc), - ) - if (others.includes(ip)) return reply.status(409).send({ error: `IP ${ip} уже назначен другому клиенту` }) - const name = userModeConfigName(newName) - await putUserModeConfig(client, name, ip, ipsecManagedComment(`клиент ${newName.trim()}`)) - await patchIdentity(client, identity[".id"]!, { "mode-config": name }) - if (currentMc && currentMc !== sharedMcName && currentMc !== name) await deleteUserModeConfig(client, currentMc) - } - } else if (!identity && certRow && d.staticIp) { - // cert-клиент без identity: персональная identity (match-by=certificate) + статический mode-config - const ip = d.staticIp.trim() - if (isIP(ip) !== 4) return reply.status(400).send({ error: `Некорректный IP: ${ip}` }) - const taken = takenStaticIps(state.modeConfigs) - if (taken.includes(ip)) return reply.status(409).send({ error: `IP ${ip} уже назначен другому клиенту` }) - const peer = ike2Peers.find((p) => isIpsecManagedComment(p.comment)) ?? ike2Peers[0] - if (!peer) return reply.status(400).send({ error: "На роутере нет IKEv2 peer для привязки клиента" }) - const shared = selectSharedIke2Identity(state.identities, ike2PeerNames) - const mcName = userModeConfigName(newName) - await putUserModeConfig(client, mcName, ip, ipsecManagedComment(`клиент ${newName.trim()}`)) - await putIdentity(client, { - peerName: (peer.name ?? "").trim(), - modeConfig: mcName, - comment: ipsecUserComment(newName), - authMethod: "certificate", - certificate: String(serverCertRow?.name ?? IPSEC_SERVER_CERT), - remoteCertificate: String(certRow.name ?? "").trim(), - placeBefore: shared?.[".id"] ? String(shared[".id"]) : undefined, - }) - } - - if (identity) { - const patch: Record = {} - if (d.psk) patch.secret = d.psk - if (d.remoteId !== undefined) patch["remote-id"] = normalizedRemoteId! - if (d.disabled === true) patch.disabled = "yes" - if (d.disabled === false) patch.disabled = "no" - if (Object.keys(patch).length) await patchIdentity(client, identity[".id"]!, patch) - } - - progress.complete() - await recordIpsec(server, "apply") + const result = await updateIpsecUserGuarded(server, rosIdParam(rosId), parsed.data, undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Операция выполнена на устройстве, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) return reply.send({ ok: true }) - } catch (e) { - return errReply(reply, e, progress) + } catch (error) { + if (error instanceof IpsecUserUpdateExecutionError) return reply.status(502).send({ error: error.message, code: error.code, state: error.confirmedWrites ? "partial" : "unknown", confirmedWrites: error.confirmedWrites, completedCommands: error.completedCommands, commandCount: error.commandCount, needsReview: true }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", needsReview: true } : {}) }) + return errReply(reply, error) } }) app.delete("/ipsec/users/:serverId/:rosId", async (req, reply) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } - const q = req.query as { removeCertificate?: string } - const removeCertificate = q.removeCertificate !== "false" const server = await getEnabledIpsecServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - const state = await fetchIpsecState(server) - const targetRosId = rosIdParam(rosId) - const target = resolveIpsecClientTarget(state, targetRosId) - if (target.ambiguous) return reply.status(409).send({ error: "Неоднозначный ID клиента. Обновите список IPsec" }) - const identity = target.identity ?? (target.certRow ? state.identities.find(i => i["remote-certificate"] === target.certRow?.name) : undefined) - const certRow = identity ? undefined : target.certRow - if (!identity && !certRow) return reply.status(404).send({ error: "Клиент не найден" }) - const sharedMcName = (state.modeConfigs.find((m) => (m.name ?? "").trim() === IPSEC_COMMON_NAME)?.name ?? "").trim() - const personal = identity ? (identity["mode-config"] ?? "").trim() : "" - const remoteCert = identity - ? (identity["remote-certificate"] ?? "").trim() - : String(certRow?.name ?? "").trim() - - // Refuse before deleting the identity: a shared certificate must survive. - // A display name does not establish ownership of a certificate. - const deletionCertName = remoteCert - if (removeCertificate && deletionCertName && ( - state.identities.some(i => i[".id"] !== identity?.[".id"] && ((i["remote-certificate"] ?? "").trim() === deletionCertName || (i.certificate ?? "").trim() === deletionCertName)) - || state.peers.some(p => (p.certificate ?? "").trim() === deletionCertName) - )) return reply.status(409).send({ error: "Сертификат используется другой identity или peer. Удалите только выбранную identity, сохранив сертификат" }) - - const certificateConflict = removeCertificate && deletionCertName ? certificateDeleteConflict(state, deletionCertName) : undefined - if (certificateConflict) return reply.status(409).send({ error: certificateConflict }) - - if (identity) { - await deleteIdentity(client, identity[".id"]!) - if (personal && personal !== sharedMcName) await deleteUserModeConfig(client, personal) - } - if (removeCertificate) { - // удаляем только клиентский серт (managed / выбранный явно); CA и серверный не трогаем - const ike2Peers = selectIke2Peers(state.peers) - const caName = resolveIke2CaName(state.certs, resolveIke2ServerCert(state.certs, ike2Peers, state.identities)) - const serverCertName = String(resolveIke2ServerCert(state.certs, ike2Peers, state.identities)?.name ?? "").trim() - const certName = remoteCert - if (certName && certName !== caName && certName !== serverCertName) { - await client.removeCertificate(certName) - } - } - progress.complete() - await recordIpsec(server, "apply") - return reply.send({ ok: true }) - } catch (e) { - return errReply(reply, e, progress) + const result = await deleteIpsecUserGuarded(server, rosIdParam(rosId), (req.query as { removeCertificate?: string }).removeCertificate !== "false", undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Операция выполнена на устройстве, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) + return reply.send(result) + } catch (error) { + if (error instanceof IpsecUserDeleteExecutionError) return reply.status(502).send({ error: error.message, code: error.code, state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", confirmedWrites: error.confirmedWrites, completedCommands: error.completedCommands, commandCount: error.commandCount, needsReview: true }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", needsReview: true } : {}) }) + return reply.status(502).send({ error: "Операция IPsec не подтверждена; обновите данные перед повтором", state: "unknown", needsReview: true }) } }) @@ -595,59 +224,31 @@ export function createIpsecRoutes(dependencies = ipsecDefaults): FastifyPluginAs if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } - const d = parsed.data - const server = await getEnabledIpsecServerById(serverIdParam(serverId)) - if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - const state = await fetchIpsecState(server) - const peer = state.peers.find((p) => String(p[".id"] ?? "") === rosIdParam(rosId)) - if (!peer) return reply.status(404).send({ error: "Peer не найден" }) - const body: Record = {} - if (d.name !== undefined) body.name = d.name - if (d.address !== undefined) body.address = d.address - if (d.exchangeMode !== undefined) body["exchange-mode"] = d.exchangeMode - if (d.passive !== undefined) body.passive = d.passive ? "yes" : "no" - if (d.certificate !== undefined) return reply.status(400).send({ error: "Сертификат задаётся в IPsec identity, а не в peer" }) - if (d.profile !== undefined) body.profile = d.profile - if (d.disabled !== undefined) body.disabled = d.disabled ? "yes" : "no" - if (Object.keys(body).length) await patchPeer(client, rosIdParam(rosId), body) - progress.complete() - await recordIpsec(server, "apply") + ipsecPeerUpdateBody(parsed.data) + const server = await getEnabledIpsecServerById(serverIdParam(serverId)) + if (!server) return reply.status(404).send({ error: "Сервер не найден" }) + const result = await updateIpsecPeerGuarded(server, rosIdParam(rosId), parsed.data, undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Операция выполнена на устройстве, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) return reply.send({ ok: true }) - } catch (e) { - return errReply(reply, e, progress) + } catch (error) { + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", needsReview: true } : {}) }) + return reply.status(502).send({ error: "Операция IPsec не подтверждена; обновите данные перед повтором", state: "unknown", needsReview: true }) } }) app.delete("/ipsec/peers/:serverId/:rosId", async (req, reply) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } - const q = req.query as { force?: string } const server = await getEnabledIpsecServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - const state = await fetchIpsecState(server) - const peer = state.peers.find((p) => String(p[".id"] ?? "") === rosIdParam(rosId)) - if (!peer) return reply.status(404).send({ error: "Peer не найден" }) - const peerName = (peer.name ?? "").trim() - const linked = state.identities - .filter((i) => (i.peer ?? "").trim() === peerName) - .map((i) => identityDisplayName(i, state.certs)) - if (linked.length > 0 && q.force !== "true") { - return reply.status(409).send({ - error: `На peer «${peerName}» ссылаются identity: ${linked.join(", ")}. Удаление разорвёт их.`, - identities: linked, - }) - } - await deletePeer(client, rosIdParam(rosId)) - progress.complete() - await recordIpsec(server, "apply") - return reply.send({ ok: true }) - } catch (e) { - return errReply(reply, e, progress) + const result = await deleteIpsecPeerGuarded(server, rosIdParam(rosId), (req.query as { force?: string }).force === "true", undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Операция выполнена на устройстве, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) + return reply.send(result) + } catch (error) { + if (error instanceof IpsecPeerLinkedConflict) return reply.status(409).send({ error: error.message, identities: error.identities }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", needsReview: true } : {}) }) + return reply.status(502).send({ error: "Операция IPsec не подтверждена; обновите данные перед повтором", state: "unknown", needsReview: true }) } }) @@ -660,132 +261,29 @@ export function createIpsecRoutes(dependencies = ipsecDefaults): FastifyPluginAs const q = req.query as { force?: string } const server = await getEnabledIpsecServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client try { - const state = await fetchIpsecState(server) - const name = parsed.data.name - const cert = state.certs.find((c) => String(c.name ?? "").trim() === name) - if (!cert?.[".id"]) return reply.status(404).send({ error: `Сертификат ${name} не найден` }) - const certificateConflict = certificateDeleteConflict(state, name) - if (certificateConflict) return reply.status(409).send({ error: certificateConflict }) - const usedBy = [ - ...state.peers.filter((p) => (p.certificate ?? "").trim() === name).map((p) => `peer ${(p.name ?? "").trim()}`), - ...state.identities - .filter((i) => (i.certificate ?? "").trim() === name || (i["remote-certificate"] ?? "").trim() === name) - .map((i) => `identity ${identityDisplayName(i, state.certs)}`), - ] - if (usedBy.length > 0 && q.force !== "true") { - return reply.status(409).send({ - error: `Сертификат «${name}» используется: ${usedBy.join(", ")}.`, - usedBy, - }) - } - await client.delete(`/certificate/${encodeURIComponent(cert[".id"])}`) - progress.complete() - await recordIpsec(server, "apply") + const result = await deleteIpsecCertificateGuarded(server, parsed.data.name, q.force === "true", undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Операция выполнена на устройстве, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) return reply.send({ ok: true }) - } catch (e) { - return errReply(reply, e, progress) + } catch (error) { + if (error instanceof IpsecCertificateUsedConflict) return reply.status(409).send({ error: error.message, usedBy: error.usedBy }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: error.code === "MUTATION_OUTCOME_UNKNOWN" ? "unknown" : "partial", needsReview: true } : {}) }) + return reply.status(502).send({ error: "Операция IPsec не подтверждена; обновите данные перед повтором", state: "unknown", needsReview: true }) } }) app.post("/ipsec/users/:serverId/:rosId/cert", async (req, reply) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } - const parsed = ipsecCertExportRequestSchema.safeParse({ ...(req.body as object), serverId, clientId: rosId }) - if (!parsed.success) { - return badBodyReply(reply, parsed.error) - } - const body = parsed.data - const server = await getEnabledIpsecServerById(serverIdParam(serverId)) - if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client - try { - const state = await fetchIpsecState(server) - const target = resolveIpsecClientTarget(state, rosIdParam(rosId)) - if (target.ambiguous) return reply.status(409).send({ error: "Неоднозначный ID клиента. Обновите список IPsec" }) - const identity = target.identity - if (!identity) return reply.status(404).send({ error: "Клиент не найден" }) - const userName = parseIpsecUserComment(identity.comment) ?? "" - if (!userName) return reply.status(400).send({ error: "Не managed-клиент" }) - const certName = identity["remote-certificate"]?.trim() - if (!certName || identity["auth-method"]?.trim().toLowerCase() === "pre-shared-key") { - return reply.status(409).send({ error: "У выбранного клиента нет привязанного клиентского сертификата. Для отдельного сертификата используйте экспорт по его имени" }) - } - if (!(await findCertificate(client, certName))) { - return reply.status(404).send({ error: `Сертификат ${certName} не найден на роутере` }) - } - const serverCert = state.certs.find((c) => String(c.name ?? "") === IPSEC_SERVER_CERT) - const serverEndpoint = String(serverCert?.["common-name"] ?? "").trim() || server.host - const sharedMc = state.modeConfigs.find((m) => (m.name ?? "").trim() === IPSEC_COMMON_NAME) - const dns = sharedMc?.["static-dns"]?.trim() || undefined - // export работает по имени сертификата (certName), не по userName - const { fileName, content, passphrase } = await client.exportCertificatePkcs12({ - name: certName, - passphrase: body.passphrase, - }) - const p12B64 = content.toString("base64") - const bundle: IpsecCertBundle = { - user: userName, - serverEndpoint, - filename: fileName, - contentB64: p12B64, - mime: "application/x-pkcs12", - passphrase, - sswanFilename: `${certName}.sswan`, - sswanContent: buildSswanConfig({ - name: `IKEv2 ${serverEndpoint}`, - serverEndpoint, - serverId: serverEndpoint, - p12B64, - }), - instructions: buildClientInstructions({ - userName, - serverEndpoint, - p12Filename: fileName, - passphrase, - dns, - }), - } - return reply.send(bundle) - } catch (e) { - return errReply(reply, e, progress) - } + const actor = req.authUser + if (!actor) return reply.status(401).send({ error: "Требуется вход через портал", code: "UNAUTHORIZED" }) + return existingCertificateReply(req, reply, signal => existingCertificateExport.client({ actor, serverId, clientId: rosId, body: req.body, requestKey: typeof req.headers["idempotency-key"] === "string" ? req.headers["idempotency-key"] : undefined, signal })) }) - /** Экспорт .p12 существующего клиентского сертификата по имени (client1/anakondra и т.п.). */ app.post("/ipsec/certs/:serverId/export", async (req, reply) => { const { serverId } = req.params as { serverId: string } - const parsed = ipsecCertExportByNameRequestSchema.safeParse(req.body ?? {}) - if (!parsed.success) { - return badBodyReply(reply, parsed.error) - } - const { name, passphrase } = parsed.data - const server = await getEnabledIpsecServerById(serverIdParam(serverId)) - if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const progress = trackIpsecMutation(MikrotikClient.fromServer(server)) - const client = progress.client - try { - const state = await fetchIpsecState(server) - const cert = state.certs.find((c) => String(c.name ?? "").trim() === name) - if (!cert?.[".id"]) return reply.status(404).send({ error: `Сертификат ${name} не найден` }) - const serverCertRow = resolveIke2ServerCert(state.certs, selectIke2Peers(state.peers)) - const serverEndpoint = String(serverCertRow?.["common-name"] ?? "").trim() || server.host - const userName = String(cert["common-name"] ?? "").trim() || name - const sharedMc = state.modeConfigs.find((m) => (m.name ?? "").trim() === IPSEC_COMMON_NAME) - const dns = sharedMc?.["static-dns"]?.trim() || undefined - const bundle = await buildCertBundle(client, { - userName, - certName: name, - serverEndpoint, - passphrase, - dns, - }) - return reply.send(bundle) - } catch (e) { - return errReply(reply, e, progress) - } + const actor = req.authUser + if (!actor) return reply.status(401).send({ error: "Требуется вход через портал", code: "UNAUTHORIZED" }) + return existingCertificateReply(req, reply, signal => existingCertificateExport.certificate({ actor, serverId, body: req.body, requestKey: typeof req.headers["idempotency-key"] === "string" ? req.headers["idempotency-key"] : undefined, signal })) }) app.get("/ipsec/revisions", async (req, reply) => { @@ -807,36 +305,14 @@ export function createIpsecRoutes(dependencies = ipsecDefaults): FastifyPluginAs requestedServerId: parseDbServerId(body?.serverId), }) if (!loaded.ok) return reply.status(loaded.status).send({ error: loaded.error }) - let progress: IpsecMutationProgress | undefined try { - const desired = parseIpsecSnapshot(loaded.row.payload) - const state = await fetchIpsecRestoreState(loaded.server) - const missingCertificates = desired.identities.flatMap(i => [i.certificate, i.remoteCertificate]).filter(n => n && !state.certificateNames.includes(n)) - if (missingCertificates.length) return reply.status(409).send({ error: "На устройстве отсутствуют сертификаты ревизии. Откат не восстанавливает ключи PKI; изменений не было" }) - const profiles = await state.client.get("/ip/ipsec/profile") - if (!Array.isArray(profiles) || profiles.some(p => !p || typeof p.name !== "string")) throw new Error("Некорректный список IPsec profiles; откат не начат") - if (desired.peers.some(p => p.profile && !profiles.some(live => live.name === p.profile))) return reply.status(409).send({ error: "На устройстве отсутствует profile ревизии. Восстановите profile перед откатом; изменений не было" }) - if (desired.identities.some(i => i.policyTemplateGroup)) { - const groups = await state.client.get("/ip/ipsec/policy/group") - if (!Array.isArray(groups) || groups.some(g => !g || typeof g.name !== "string")) throw new Error("Некорректный список групп политик; откат не начат") - if (desired.identities.some(i => i.policyTemplateGroup && !groups.some(g => g.name === i.policyTemplateGroup))) return reply.status(409).send({ error: "Группа политик ревизии отсутствует. Восстановите её перед откатом; изменений не было" }) - } - const ops = planIpsecRestore(desired, { - peers: state.peers, - identities: state.identities, - modeConfigs: state.modeConfigs, - pools: state.pools, - nat: state.nat, - policies: state.policies, - }) - progress = trackIpsecMutation(state.client) - await executeRosOps(progress.client, ops) - progress.complete() - await recordIpsec(loaded.server, "rollback") - const result = await listIpsec({ serverId: String(loaded.server.id) }) - return reply.send({ ok: true, ...result }) - } catch (e) { - return errReply(reply, e, progress) + const result = await restoreIpsecRevisionGuarded(loaded.server, id, undefined, undefined, { recordRevision: dependencies.recordRevision }) + if (!result.historyRecorded) return reply.status(502).send({ error: "Откат применён, но сохранение истории не подтверждено. Обновите данные перед повтором", state: "applied", historyRecorded: false }) + return reply.send({ ok: true, ...await listIpsec({ serverId: String(loaded.server.id) }) }) + } catch (error) { + if (error instanceof IpsecRevisionRestoreExecutionError) return reply.status(502).send({ error: error.message, code: error.code, state: error.confirmedWrites ? "partial" : "unknown", confirmedWrites: error.confirmedWrites, completedCommands: error.completedCommands, commandCount: error.commandCount, needsReview: true }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code, ...(["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) ? { state: "unknown", needsReview: true } : {}) }) + return errReply(reply, error) } }) } diff --git a/backend/src/routes/logs.ts b/backend/src/routes/logs.ts index b4d96c5b..28bfd83f 100644 --- a/backend/src/routes/logs.ts +++ b/backend/src/routes/logs.ts @@ -1,13 +1,16 @@ import type { FastifyPluginAsync } from "fastify" import { setTimeout as sleep } from "node:timers/promises" import { once } from "node:events" -import { logFiltersSchema, logPageQuerySchema, logSettingsPatchSchema } from "@mmapp/contracts" +import { logFiltersSchema, logPageQuerySchema, logSequenceInputSchema, logSettingsPatchSchema } from "@mmapp/contracts" import { pool } from "../db/index.js" import { env } from "../config.js" +import { mcpDatabaseRequestSignal, withDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" +import { McpError } from "../modules/mcp/security.js" import { registerDatabaseStreamAbort } from "../plugins/database-request-gate.js" import { getLogSettings, subscribeLogDevice, updateLogSettings } from "../services/router-logs-collector.js" -import { readLogPage, readLogSummary, readLogEntry, readLogContext, readLogStream, logColumns } from "../services/router-logs-query.js" -import { csvCell, logWhere } from "../services/router-logs-model.js" +import { readLogPage, readLogSummary, readLogEntry, readLogContext, readLogStream } from "../services/router-logs-query.js" +import { logWhere } from "../services/router-logs-model.js" +import { readBoundedLogExportRows, serializeLogExport, withLogExportSlot, LogExportLimitError, LogExportBusyError } from "../services/router-logs-export.js" import { refreshScheduler } from "../services/scheduler.js" import { isSchedulerJobsPaused } from "../services/scheduler-running.js" @@ -37,30 +40,37 @@ const logsRoutes: FastifyPluginAsync = async app => { const format = (req.query as { format?: string }).format ?? "csv" if (!parsed.success || !["csv", "json"].includes(format)) return reply.code(400).send({ error: "Некорректный экспорт" }) const where = logWhere(parsed.data) - const rows = (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE ${where.sql} ORDER BY effective_at DESC,id DESC LIMIT 50001`, where.values)).rows - if (rows.length > 50000) return reply.code(422).send({ error: "Найдено более 50 000 событий. Сузьте период или фильтры для экспорта." }) - reply.header("Content-Disposition", `attachment; filename="router-logs.${format}"`) - if (format === "json") return reply.type("application/json; charset=utf-8").send(JSON.stringify(rows)) - const columns = ["effectiveAt", "serverName", "host", "level", "topics", "message", "rawTime", "receivedAt", "timeKind"] - const csv = [columns.map(csvCell).join(","), ...rows.map(row => columns.map(key => csvCell(Array.isArray(row[key]) ? row[key].join(",") : row[key] instanceof Date ? row[key].toISOString() : row[key])).join(","))].join("\r\n") - return reply.type("text/csv; charset=utf-8").send("\uFEFF" + csv) + try { + return await withLogExportSlot(async () => { + const content = serializeLogExport(await readBoundedLogExportRows(where), format as "json" | "csv") + return reply.header("Content-Disposition", `attachment; filename="router-logs.${format}"`).type(format === "json" ? "application/json; charset=utf-8" : "text/csv; charset=utf-8").send(content) + }) + } catch (error) { if (error instanceof LogExportLimitError || error instanceof LogExportBusyError) return reply.code(error.statusCode).send({ error: error.message }); throw error } }) app.get("/logs/live", async (req, reply) => { const parsed = logFiltersSchema.safeParse(req.query) - const cursor = (req.query as { cursor?: string }).cursor - if (!parsed.success || !parsed.data.serverId || !cursor || !/^\d{1,20}$/.test(cursor)) return reply.code(400).send({ error: "Выберите устройство и передайте курсор истории" }) + const cursor = logSequenceInputSchema.safeParse((req.query as { cursor?: string }).cursor) + if (!parsed.success || !parsed.data.serverId || !cursor.success) return reply.code(400).send({ error: "Выберите устройство и передайте курсор истории" }) const filters = parsed.data - const available = await pool.query("SELECT id FROM servers WHERE id=$1 AND enabled=true", [filters.serverId]) + const readAvailability = () => pool.query("SELECT id FROM servers WHERE id=$1 AND enabled=true", [filters.serverId]) + const available = await withDatabaseApiRequest(async () => { + const result = await readAvailability() + mcpDatabaseRequestSignal()?.throwIfAborted() + return result + }) if (!available.rowCount) return reply.code(404).send({ error: "Устройство отключено или удалено" }) if (isSchedulerJobsPaused()) return reply.code(503).send({ error: "Сбор приостановлен для обслуживания БД" }) const controller = new AbortController() - streams.add(controller) const onClose = () => controller.abort() - registerDatabaseStreamAbort(req, () => { - if (reply.raw.headersSent && !reply.raw.destroyed) reply.raw.write('event: reset\ndata: {"reason":"database-maintenance"}\n\n') + const resetForMaintenance = () => { + if (!controller.signal.aborted && reply.raw.headersSent && !reply.raw.destroyed) reply.raw.write('event: reset\ndata: {"reason":"database-maintenance"}\n\n') controller.abort() - }) - req.raw.on("close", onClose) + } + registerDatabaseStreamAbort(req, resetForMaintenance) + if (controller.signal.aborted) return reply.code(503).send({ error: "Сбор приостановлен для обслуживания БД" }) + if (reply.raw.destroyed) return + streams.add(controller) + reply.raw.on("close", onClose) const unsubscribe = subscribeLogDevice(filters.serverId!) reply.hijack() req.raw.setTimeout(0); reply.raw.setTimeout(0) @@ -68,32 +78,40 @@ const logsRoutes: FastifyPluginAsync = async app => { const origin = req.headers.origin if (origin && (env.CORS_ORIGIN === "*" || env.CORS_ORIGIN === origin)) { headers["Access-Control-Allow-Origin"] = origin; headers["Access-Control-Allow-Credentials"] = "true"; headers.Vary = "Origin" } reply.raw.writeHead(200, headers) - let after = cursor + let after = cursor.data try { while (!controller.signal.aborted && !isSchedulerJobsPaused()) { // Long connections must not outlive the JWT they were authenticated with. if (env.authRequired && req.user.exp && Date.now() >= req.user.exp * 1000) break - const exists = await pool.query("SELECT id FROM servers WHERE id=$1 AND enabled=true", [filters.serverId]) - if (!exists.rowCount) { reply.raw.write(`event: error\ndata: ${JSON.stringify({ error: "Устройство отключено или удалено" })}\n\n`); break } - const batch = await readLogStream(filters, after) + const batch = await withDatabaseApiRequest(async () => { + const leaseSignal = mcpDatabaseRequestSignal() + const signal = leaseSignal ? AbortSignal.any([controller.signal, leaseSignal]) : controller.signal + signal.throwIfAborted() + const exists = await readAvailability() + signal.throwIfAborted() + return exists.rowCount ? readLogStream(filters, after, undefined, signal) : null + }) + controller.signal.throwIfAborted() + if (!batch) { reply.raw.write(`event: error\ndata: ${JSON.stringify({ error: "Устройство отключено или удалено" })}\n\n`); break } after = batch.cursor if (!reply.raw.write(`event: batch\nid: ${after}\ndata: ${JSON.stringify(batch)}\n\n`)) await once(reply.raw, "drain", { signal: controller.signal }) if (batch.entries.length < 200) await sleep(1000, undefined, { signal: controller.signal }) } } catch (error) { - if (!controller.signal.aborted && !reply.raw.destroyed) reply.raw.write(`event: error\ndata: ${JSON.stringify({ error: "Поток прерван; выполняется переподключение" })}\n\n`) + if (error instanceof McpError && error.code === "PROTOCOL_STORAGE_BUSY") resetForMaintenance() + else if (!controller.signal.aborted && !reply.raw.destroyed) reply.raw.write(`event: error\ndata: ${JSON.stringify({ error: "Поток прерван; выполняется переподключение" })}\n\n`) req.log.debug({ err: error }, "Logs SSE closed") - } finally { streams.delete(controller); unsubscribe(); req.raw.off("close", onClose); reply.raw.end() } + } finally { streams.delete(controller); unsubscribe(); reply.raw.off("close", onClose); reply.raw.end() } }) app.get("/logs/:id", async (req, reply) => { const id = String((req.params as { id: string }).id) - if (!/^\d{1,20}$/.test(id)) return reply.code(400).send({ error: "Некорректный ID" }) + if (!logSequenceInputSchema.safeParse(id).success) return reply.code(400).send({ error: "Некорректный ID" }) const entry = await readLogEntry(id) return entry ?? reply.code(404).send({ error: "Событие не найдено или срок хранения истёк" }) }) app.get("/logs/:id/context", async (req, reply) => { const id = String((req.params as { id: string }).id) - if (!/^\d{1,20}$/.test(id)) return reply.code(400).send({ error: "Некорректный ID" }) + if (!logSequenceInputSchema.safeParse(id).success) return reply.code(400).send({ error: "Некорректный ID" }) return await readLogContext(id) ?? reply.code(404).send({ error: "Событие не найдено или срок хранения истёк" }) }) } diff --git a/backend/src/routes/manual-data-collection.test.ts b/backend/src/routes/manual-data-collection.test.ts new file mode 100644 index 00000000..3337c2e4 --- /dev/null +++ b/backend/src/routes/manual-data-collection.test.ts @@ -0,0 +1,50 @@ +import assert from "node:assert/strict" +import Fastify from "fastify" +import { createServersManualCollectionRoute, createUptimeManualCollectionRoutes } from "./manual-data-collection.js" +import { createManualDataCollection } from "../services/manual-data-collection-execution.js" + +const calls: string[] = [] +let groupError: Error | undefined +const operations = createManualDataCollection({ + collectServersRestPing: async () => { calls.push("rest") }, + collectUptime: async () => { calls.push("uptime");return {resources:{servers:[]},ping:{probes:[]}} }, + collectPingGroup: async ids => { calls.push(JSON.stringify(ids)); if (groupError) throw groupError; return { polled: ids.length } }, + readServersRestPingSettings: async () => ({ lastError: "sample failure" }), + readUptimeSettings: async () => ({}), + scheduleGlobalAlerts: () => { calls.push("global-alert-engine") }, +}) +const app = Fastify() +await app.register(createServersManualCollectionRoute(operations.collectServersRestPing), { prefix: "/api" }) +await app.register(createUptimeManualCollectionRoutes(operations), { prefix: "/api" }) +try { + const rest = await app.inject({ method: "POST", url: "/api/servers-api-ping/collect-now", payload: { enabled: false } }) + assert.equal(rest.statusCode, 200) + assert.deepEqual(rest.json(), { ok: true, lastCollectedAt: null, lastDurationMs: null, lastError: "sample failure",skipped:false }) + assert.deepEqual(calls.splice(0), ["rest", "global-alert-engine"]) + const uptime = await app.inject({ method: "POST", url: "/api/uptime/collect-now" }) + assert.equal(uptime.statusCode, 200) + assert.deepEqual(uptime.json(), { ok: true, lastCollectedAt: null, lastDurationMs: null, lastError: null,skipped:false,partial:false, + parts:{resources:{reported:true,skipped:false,fatalError:false,count:0},ping:{reported:true,skipped:false,fatalError:false,count:0,skippedByInterval:0}} }) + assert.deepEqual(calls.splice(0), ["uptime", "global-alert-engine"]) + const group = await app.inject({ method: "POST", url: "/api/uptime/probes/collect-group", payload: { probeIds: ["a", 12, null, "a", " ", true] } }) + assert.equal(group.statusCode, 200) + assert.deepEqual(group.json(), { ok: true, polled: 3 }) + assert.deepEqual(calls.splice(0), ['["a","a"," "]', "global-alert-engine"]) + const empty = await app.inject({ method: "POST", url: "/api/uptime/probes/collect-group", payload: { probeIds: false } }) + assert.deepEqual(empty.json(), { ok: true, polled: 0 }) + assert.deepEqual(calls.splice(0), ["[]"]) + const missingBody = await app.inject({ method: "POST", url: "/api/uptime/probes/collect-group" }) + assert.equal(missingBody.statusCode, 200) + assert.deepEqual(missingBody.json(), { ok: true, polled: 0 }) + assert.deepEqual(calls.splice(0), ["[]"]) + groupError = Object.assign(Error("Сбор ping уже выполняется, подождите"), { statusCode: 409 }) + const busy = await app.inject({ method: "POST", url: "/api/uptime/probes/collect-group", payload: { probeIds: ["a"] } }) + assert.equal(busy.statusCode, 409) + assert.deepEqual(busy.json(), { error: groupError.message }) + assert.deepEqual(calls.splice(0), ['["a"]']) + groupError = Error("database unavailable") + const failed = await app.inject({ method: "POST", url: "/api/uptime/probes/collect-group", payload: { probeIds: ["a"] } }) + assert.equal(failed.statusCode, 500) + assert.deepEqual(calls.splice(0), ['["a"]']) + console.log("Manual collector HTTP adapters: all three paths preserve full shared operations, response contracts, group string filtering and 409 behavior") +} finally { await app.close() } diff --git a/backend/src/routes/manual-data-collection.ts b/backend/src/routes/manual-data-collection.ts new file mode 100644 index 00000000..b079c02d --- /dev/null +++ b/backend/src/routes/manual-data-collection.ts @@ -0,0 +1,24 @@ +import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" +import type { ManualDataCollection } from "../services/manual-data-collection-execution.js" + +export function createServersManualCollectionRoute(operation: ManualDataCollection["collectServersRestPing"]): FastifyPluginAsyncZod { + return async app => { + app.post("/servers-api-ping/collect-now", async (_req, reply) => reply.send(await operation())) + } +} +export function createUptimeManualCollectionRoutes(operations: Pick): FastifyPluginAsyncZod { + return async app => { + app.post("/uptime/collect-now", async (_req, reply) => reply.send(await operations.collectUptime())) + app.post("/uptime/probes/collect-group", async (req, reply) => { + const raw = (req.body as { probeIds?: unknown } | undefined)?.probeIds + const probeIds = Array.isArray(raw) ? raw.filter((x): x is string => typeof x === "string") : [] + try { return reply.send(await operations.collectPingGroup(probeIds)) } + catch (error) { + if ((error as Error & { statusCode?: number }).statusCode === 409) { + return reply.status(409).send({ error: error instanceof Error ? error.message : "busy" }) + } + throw error + } + }) + } +} diff --git a/backend/src/routes/mcp-oauth-database-admission.test.ts b/backend/src/routes/mcp-oauth-database-admission.test.ts new file mode 100644 index 00000000..b23cae81 --- /dev/null +++ b/backend/src/routes/mcp-oauth-database-admission.test.ts @@ -0,0 +1,177 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import Fastify, { type FastifyInstance, type FastifyPluginAsync } from "fastify" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import * as crypto from "node:crypto" +import { z } from "zod" +import ts from "typescript" +import { DatabaseRequestGate } from "../services/database-request-gate.js" +import * as operation from "../services/system-database-operation.js" + +// Real Fastify + plugin and shared gate/readiness wiring; SQL is inert and +// counted. No configured pool, credentials, native process or network is opened. +async function fixture(authRequired = true) { + async function load(file: URL, modules: Record): Promise { + const exports: Record = {} + const compiled = ts.transpileModule(await readFile(file, "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, esModuleInterop: true } }).outputText + runInNewContext(compiled, { exports, require: (name: string) => { assert(name in modules, `Unapproved import ${name}`); return modules[name] }, URL, URLSearchParams, Buffer, Date, Promise, Error, process: { env: {} } }) + return exports as T + } + const shared = await load<{ sharedDatabaseGate: DatabaseRequestGate; databaseSchemaReadiness: ReturnType }>(new URL("../services/shared-database-gate.ts", import.meta.url), { + "./database-request-gate.js": { DatabaseRequestGate }, "./system-database-operation.js": operation, + }) + const gate = shared.sharedDatabaseGate as DatabaseRequestGate + const readiness = shared.databaseSchemaReadiness as ReturnType + let admissions = 0, sqlCalls = 0, transactions = 0, audits = 0 + let holdClientRead: (() => Promise) | undefined + const events: string[] = [], resource = "http://localhost:8000/mcp", generation = "current-owned-generation" + const query = async (sql: string) => { + sqlCalls++; events.push(sql.startsWith("INSERT INTO mcp_credentials") ? "credential-write" : "sql") + if (sql.startsWith("SELECT id FROM mcp_oauth_clients")) { + await holdClientRead?.(); return { rows: [{ id: "owned-client" }] } + } + if (sql.startsWith("SELECT * FROM mcp_oauth_clients")) return { rows: [{ id: "owned-client", redirect_uris: ["http://localhost/callback"] }] } + if (sql.startsWith("SELECT * FROM mcp_credentials")) return { rows: [{ id: "refresh-id", client_id: "owned-client", audience: resource, expires_at: new Date(Date.now() + 600000), revoked_at: null, connection_id: "connection-id", family_id: "family-id" }] } + if (sql.startsWith("SELECT * FROM mcp_oauth_clients ORDER")) return { rows: [] } + return { rows: [], rowCount: 1 } + } + const request = gate.request.bind(gate) + Object.defineProperty(gate, "request", { value: function(run: () => Promise, cancel?: () => void) { admissions++; return request(run, cancel) } }) + class McpError extends Error { constructor(readonly code: string, message: string, readonly statusCode = 403) { super(message) } } + const oauth = await load<{ mcpOauth: FastifyPluginAsync }>(new URL("./mcp-oauth.ts", import.meta.url), { + "node:crypto": crypto, zod: { z }, "../db/index.js": { pool: { query } }, + "../services/shared-database-gate.js": shared, + "../modules/mcp/database-request-lease.js": { + withMcpDatabaseRequest: (work: () => Promise) => work(), + withDatabaseApiRequest: (work: () => Promise) => work(), + }, + "../config.js": { env: { authRequired, CORS_ORIGIN: "http://localhost:3000" } }, + "../plugins/auth.js": { requireAuth: async (req: { authUser?: { id: string } }) => { req.authUser = { id: "owned-admin" } } }, + "../modules/mcp/store.js": { + audit: async () => { audits++ }, getConnection: async () => ({ generation, policy: { status: "active", expiresAt: new Date(Date.now() + 600000).toISOString() }, owner_valid_until: new Date(Date.now() + 600000) }), + transaction: async (work: (db: { query: typeof query }) => Promise) => { transactions++; events.push("transaction-start"); const result = await work({ query }); events.push("transaction-settled"); return result }, + }, + "../modules/mcp/security.js": { McpError, accessGeneration: async () => generation, constantEqual: (a: string, b: string) => a === b, randomToken: (kind: string) => kind + "-owned-inert", tokenDigest: (token: string) => crypto.createHash("sha256").update(token).digest("hex") }, + "./mcp-transport.js": { mcpPublicUrl: () => new URL(resource) }, + "../modules/mcp/portal-revocation.js": { assertPortalClaimsCurrent: async () => { await query("SELECT invalid_before FROM mcp_owner_revocations") } }, + }) + const api = await load<{ installDatabaseRequestGate(app: FastifyInstance): void }>(new URL("../plugins/database-request-gate.ts", import.meta.url), { + "../services/shared-database-gate.js": shared, + "../services/config-mutation-policy.js": { withConfigurationActor: (_actor: string, work: () => unknown) => work() }, + "../modules/mcp/database-request-lease.js": { withDatabaseApiRequest: (work: () => Promise) => work() }, + }) + const app = Fastify() + api.installDatabaseRequestGate(app) + await app.register(oauth.mcpOauth) + const publicRequests = () => [ + { method: "GET" as const, url: "/oauth/authorize?" + new URLSearchParams({ client_id: "owned-client", redirect_uri: "http://localhost/callback", response_type: "code", code_challenge: "a".repeat(43), code_challenge_method: "S256", resource }).toString() }, + { method: "POST" as const, url: "/oauth/token", payload: { grant_type: "refresh_token", client_id: "owned-client", resource, refresh_token: "owned-refresh" } }, + { method: "POST" as const, url: "/oauth/revoke", payload: { token: "owned-refresh", client_id: "owned-client" } }, + ] + return { app, gate, readiness, events, publicRequests, counts: () => ({ admissions, sqlCalls, transactions, audits }), hold: (work?: () => Promise) => { holdClientRead = work } } +} +function deferred() { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +async function unavailable(f: Awaited>) { + const before = f.counts() + for (const options of f.publicRequests()) { + const response = await f.app.inject(options) + assert.equal(response.statusCode, 503); assert.deepEqual(response.json(), { error: "temporarily_unavailable" }); assert.equal(response.headers["cache-control"], "no-store") + } + assert.equal(f.counts().sqlCalls, before.sqlCalls); assert.equal(f.counts().transactions, before.transactions); assert.equal(f.counts().audits, before.audits) +} + +test("public OAuth authorize/token/revoke refuse before SQL while exclusive maintenance runs", async () => { + const f = await fixture(), entered = deferred(), release = deferred() + const maintenance = f.gate.exclusive(async () => { entered.resolve(); await release.promise }) + try { await entered.promise; await unavailable(f) } + finally { release.resolve(); await maintenance; await f.app.close() } +}) +test("in-flight actual token transaction settles before exclusive replacement begins", async () => { + const f = await fixture(), entered = deferred(), release = deferred() + f.hold(async () => { entered.resolve(); await release.promise }) + const token = f.app.inject(f.publicRequests()[1]).then(response => response) + await entered.promise + let replacements = 0 + const maintenance = f.gate.exclusive(async () => { replacements++; f.events.push("replacement"); f.readiness.beginReplacement() }) + try { + await unavailable(f); assert.equal(replacements, 0) + release.resolve(); const response = await token; assert.equal(response.statusCode, 200); assert.equal(response.json().access_token, "access-owned-inert") + await maintenance; assert.equal(replacements, 1) + assert(f.events.indexOf("transaction-settled") < f.events.indexOf("replacement")); assert.equal(f.events.filter(event => event === "credential-write").length, 2) + await unavailable(f) + } finally { release.resolve(); await token; await maintenance; await f.app.close() } +}) +test("sticky replacement refusal survives exclusive finally and only explicit repair reopens OAuth SQL", async () => { + const f = await fixture() + try { + await f.gate.exclusive(async () => { f.readiness.beginReplacement() }) + await unavailable(f) + await f.gate.exclusive(async () => {}) + await unavailable(f) + await f.gate.exclusive(async () => { f.readiness.confirmSchema() }) + const responses = [] + for (const options of f.publicRequests()) responses.push(await f.app.inject(options)) + assert.deepEqual(responses.map(response => response.statusCode), [302, 200, 200]); assert(f.counts().sqlCalls > 0) + } finally { await f.app.close() } +}) +test("both pure discovery endpoints remain200 with no SQL or admission while maintenance is sticky-closed", async () => { + const f = await fixture() + try { + f.readiness.beginReplacement(); const before = f.counts() + for (const url of ["/.well-known/oauth-protected-resource", "/.well-known/oauth-authorization-server"]) assert.equal((await f.app.inject(url)).statusCode, 200) + assert.deepEqual(f.counts(), before) + } finally { await f.app.close() } +}) +test("OAuth admits public SQL once and protects both API admin SQL stages", async () => { + const f = await fixture() + try { + const before = f.counts().admissions + assert.equal((await f.app.inject(f.publicRequests()[0])).statusCode, 302) + assert.equal(f.counts().admissions - before, 1) + const middle = f.counts().admissions + assert.equal((await f.app.inject("/api/mcp/oauth/clients")).statusCode, 200) + assert.equal(f.counts().admissions - middle, 2) + await f.gate.exclusive(async () => { + const sqlBefore = f.counts().sqlCalls + const response = await f.app.inject("/api/mcp/oauth/clients") + assert.equal(response.statusCode, 503) + assert.equal(f.counts().sqlCalls, sqlBefore) + }) + } finally { await f.app.close() } +}) +test("ordinary invalid OAuth input preserves its existing protocol error and no-store behavior", async () => { + const f = await fixture() + try { + const response = await f.app.inject({ method: "POST", url: "/oauth/token", payload: {} }) + assert.equal(response.statusCode, 400); assert.deepEqual(response.json(), { error: "invalid_request" }); assert.equal(response.headers["cache-control"], "no-store"); assert.equal(f.counts().sqlCalls, 0) + } finally { await f.app.close() } +}) +test("disabled Portal auth prevents token exchange and refresh before SQL but permits revocation", async () => { + const f = await fixture(false) + try { + const [, refresh, revoke] = f.publicRequests() + const code = { + ...refresh, + payload: { + grant_type: "authorization_code", client_id: refresh.payload.client_id, + resource: "http://localhost:8000/mcp", redirect_uri: "http://localhost/callback", + code: "owned-code", code_verifier: "a".repeat(43), + }, + } + const before = f.counts() + for (const request of [code, refresh]) { + const response = await f.app.inject(request) + assert.equal(response.statusCode, 503) + assert.deepEqual(response.json(), { error: "temporarily_unavailable" }) + assert.equal(response.headers["cache-control"], "no-store") + assert.equal(response.headers.pragma, "no-cache") + } + const after = f.counts() + assert.equal(after.sqlCalls, before.sqlCalls) + assert.equal(after.transactions, before.transactions) + assert.equal(after.audits, before.audits) + assert.equal((await f.app.inject(revoke)).statusCode, 200) + assert.equal(f.counts().sqlCalls, before.sqlCalls + 1) + } finally { await f.app.close() } +}) diff --git a/backend/src/routes/mcp-oauth.ts b/backend/src/routes/mcp-oauth.ts new file mode 100644 index 00000000..621141e4 --- /dev/null +++ b/backend/src/routes/mcp-oauth.ts @@ -0,0 +1,172 @@ +import { createHash, randomUUID } from "node:crypto" +import type { FastifyPluginAsync } from "fastify" +import { z } from "zod" +import { pool } from "../db/index.js" +import { sharedDatabaseGate } from "../services/shared-database-gate.js" +import { withDatabaseApiRequest, withMcpDatabaseRequest } from "../modules/mcp/database-request-lease.js" +import { env } from "../config.js" +import { requireAuth } from "../plugins/auth.js" +import { audit, getConnection, transaction } from "../modules/mcp/store.js" +import { accessGeneration, constantEqual, McpError, randomToken, tokenDigest } from "../modules/mcp/security.js" +import { mcpPublicUrl } from "./mcp-transport.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" + +const clientInput = z.object({ name: z.string().trim().min(1).max(100), redirectUris: z.array(z.string().url()).min(1).max(10) }).strict() +const consentInput = z.discriminatedUnion("approve", [ + z.object({ requestId: z.string().uuid(), approve: z.literal(true), connectionId: z.string().uuid() }).strict(), + z.object({ requestId: z.string().uuid(), approve: z.literal(false), connectionId: z.string().uuid().optional() }).strict(), +]) +function safeRedirect(value: string): boolean { + const url = new URL(value) + return !url.username && !url.password && !url.hash && (url.protocol === "https:" || url.protocol === "http:" && ["127.0.0.1", "localhost", "[::1]"].includes(url.hostname)) +} +export function verifyPkce(verifier: string, challenge: string): boolean { + return /^[A-Za-z0-9._~-]{43,128}$/.test(verifier) && constantEqual(createHash("sha256").update(verifier).digest("base64url"), challenge) +} +export const mcpOauth: FastifyPluginAsync = async app => { + // Public OAuth SQL is outside /api: own its full handler admission here. + // Pure discovery stays available. The distributed lease covers SQL through handler completion. + app.addHook("onRoute", options => { + const publicOAuth = options.url.startsWith("/oauth/") + if (!publicOAuth && !options.url.startsWith("/api/mcp/oauth/")) return + const original = options.handler + options.handler = async function (request, reply) { + const run = () => withMcpDatabaseRequest(async () => await original.call(this, request, reply)) + return publicOAuth ? sharedDatabaseGate.request(run) : run() + } + }) + app.setErrorHandler((error, _request, reply) => { + reply.header("Cache-Control", "no-store") + if (error instanceof z.ZodError) return reply.code(400).send({ error: "invalid_request" }) + if (error instanceof McpError && error.code === "PROTOCOL_STORAGE_BUSY") return reply.code(503).send({ error: "temporarily_unavailable" }) + if (error instanceof McpError) return reply.code(error.statusCode).send({ error: error.code, error_description: error.message }) + return reply.code(503).send({ error: "temporarily_unavailable" }) + }) + const resource = mcpPublicUrl(), issuer = resource.origin + app.addContentTypeParser("application/x-www-form-urlencoded", { parseAs: "string", bodyLimit: 16384 }, (_request, body, done) => { + const params = new URLSearchParams(String(body)), object: Record = {} + for (const [key, value] of params) { + if (key in object) return done(Object.assign(new Error("Повторяющийся OAuth-параметр"), { statusCode: 400 })) + object[key] = value + } + done(null, object) + }) + app.get("/.well-known/oauth-protected-resource", async () => ({ resource: resource.href, authorization_servers: [issuer], scopes_supported: ["mcp"], bearer_methods_supported: ["header"] })) + app.get("/.well-known/oauth-authorization-server", async () => ({ + issuer, authorization_endpoint: issuer + "/oauth/authorize", token_endpoint: issuer + "/oauth/token", revocation_endpoint: issuer + "/oauth/revoke", + response_types_supported: ["code"], grant_types_supported: ["authorization_code", "refresh_token"], + token_endpoint_auth_methods_supported: ["none"], code_challenge_methods_supported: ["S256"], scopes_supported: ["mcp"], authorization_response_iss_parameter_supported: true, + })) + app.get("/oauth/authorize", async (request, reply) => { + if (!env.authRequired) return reply.code(503).send({ error: "temporarily_unavailable" }) + const input = z.object({ client_id: z.string().min(1).max(200), redirect_uri: z.string().url(), response_type: z.literal("code"), code_challenge: z.string().regex(/^[A-Za-z0-9_-]{43}$/), code_challenge_method: z.literal("S256"), state: z.string().max(1000).optional(), resource: z.literal(resource.href), scope: z.literal("mcp").default("mcp") }).strict().parse(request.query) + const { rows: [client] } = await pool.query("SELECT * FROM mcp_oauth_clients WHERE id=$1 AND enabled=true", [input.client_id]) + if (!client || !(client.redirect_uris as string[]).includes(input.redirect_uri)) return reply.code(400).send({ error: "invalid_request", error_description: "Незарегистрированный client_id или redirect_uri" }) + const id = randomUUID() + await pool.query("INSERT INTO mcp_oauth_requests(id,client_id,redirect_uri,challenge,state,resource,scope,expires_at) VALUES($1,$2,$3,$4,$5,$6,$7,now()+interval '5 minutes')", [id, input.client_id, input.redirect_uri, input.code_challenge, input.state ?? null, input.resource, input.scope]) + const ui = new URL("/mcp", process.env.MCP_UI_URL || env.CORS_ORIGIN) + ui.searchParams.set("oauthRequest", id) + return reply.redirect(ui.href) + }) + app.post("/oauth/token", async (request, reply) => { + reply.header("Cache-Control", "no-store").header("Pragma", "no-cache") + if (!env.authRequired) return reply.code(503).send({ error: "temporarily_unavailable" }) + const input = z.object({ grant_type: z.enum(["authorization_code", "refresh_token"]), client_id: z.string().min(1).max(200), resource: z.literal(resource.href), code: z.string().max(200).optional(), redirect_uri: z.string().url().optional(), code_verifier: z.string().max(128).optional(), refresh_token: z.string().max(200).optional() }).strict().parse(request.body) + const result = await transaction(async db => { + const { rows: [client] } = await db.query("SELECT id FROM mcp_oauth_clients WHERE id=$1 AND enabled=true", [input.client_id]) + if (!client) return { error: "invalid_client" } + let connectionId: string, familyId = randomUUID(), consumedId: string + if (input.grant_type === "authorization_code") { + if (!input.code || !input.code_verifier || !input.redirect_uri) return { error: "invalid_request" } + const { rows: [authorization] } = await db.query("SELECT * FROM mcp_oauth_requests WHERE code_digest=$1 FOR UPDATE", [tokenDigest(input.code)]) + if (!authorization || authorization.consumed_at || authorization.expires_at.getTime() <= Date.now() || authorization.client_id !== input.client_id || authorization.redirect_uri !== input.redirect_uri || authorization.resource !== input.resource || !authorization.connection_id || !verifyPkce(input.code_verifier, authorization.challenge)) return { error: "invalid_grant" } + connectionId = authorization.connection_id; consumedId = authorization.id + await db.query("UPDATE mcp_oauth_requests SET consumed_at=now() WHERE id=$1", [consumedId]) + } else { + if (!input.refresh_token) return { error: "invalid_request" } + const { rows: [refresh] } = await db.query("SELECT * FROM mcp_credentials WHERE digest=$1 AND kind='refresh' FOR UPDATE", [tokenDigest(input.refresh_token)]) + if (!refresh || refresh.client_id !== input.client_id || refresh.audience !== input.resource || refresh.expires_at.getTime() <= Date.now()) return { error: "invalid_grant" } + if (refresh.revoked_at) { + await db.query("UPDATE mcp_credentials SET revoked_at=now() WHERE family_id=$1", [refresh.family_id]) + await audit(db, "oauth", "refresh.reuse", refresh.connection_id, { clientId: input.client_id }) + return { error: "invalid_grant" } + } + connectionId = refresh.connection_id; familyId = refresh.family_id; consumedId = refresh.id + await db.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [consumedId]) + } + const connection = await getConnection(connectionId, db) + if (connection.generation !== await accessGeneration() || connection.policy.status !== "active" || Date.parse(connection.policy.expiresAt) <= Date.now() || connection.owner_valid_until.getTime() <= Date.now()) return { error: "invalid_grant" } + const access = randomToken("access"), refresh = randomToken("refresh") + const expires = Math.min(Date.now() + 900000, Date.parse(connection.policy.expiresAt)), refreshExpires = Math.min(Date.now() + 30 * 86400000, Date.parse(connection.policy.expiresAt)) + for (const credential of [{ token: access, kind: "access", expires }, { token: refresh, kind: "refresh", expires: refreshExpires }]) { + await db.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at,family_id,client_id,audience) VALUES($1,$2,$3,$4,$5,$6,$7,$8)", [randomUUID(), connectionId, tokenDigest(credential.token), credential.kind, new Date(credential.expires), familyId, input.client_id, input.resource]) + } + await audit(db, "oauth", "token.issue", connectionId, { clientId: input.client_id, familyId }) + return { access_token: access, token_type: "Bearer", expires_in: Math.floor((expires - Date.now()) / 1000), refresh_token: refresh, scope: "mcp" } + }) + return reply.code("error" in result ? 400 : 200).send(result) + }) + app.post("/oauth/revoke", async (request, reply) => { + const input = z.object({ token: z.string().max(200), client_id: z.string().max(200), token_type_hint: z.string().max(50).optional() }).strict().parse(request.body) + await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE family_id IN (SELECT family_id FROM mcp_credentials WHERE digest=$1 AND client_id=$2)", [tokenDigest(input.token), input.client_id]) + return reply.code(200).send({}) + }) + + // Portal-authenticated consent and client administration, isolated from agent credentials. + await app.register(async admin => { + admin.addHook("onRequest", async (request, reply) => { + if (!env.authRequired) return reply.code(503).send({ error: "Требуется авторизация портала" }) + await requireAuth(request, reply) + if (reply.sent) return + // The generic API gate admits the handler, not this SQL-bearing hook. + await sharedDatabaseGate.request(() => withDatabaseApiRequest(() => assertPortalClaimsCurrent(request.authUser!))) + if (request.headers.origin && request.headers.origin !== env.CORS_ORIGIN) return reply.code(403).send({ error: "Недопустимый Origin" }) + }) + admin.get("/api/mcp/oauth/clients", async () => ({ clients: (await pool.query("SELECT * FROM mcp_oauth_clients ORDER BY created_at DESC LIMIT 100")).rows })) + admin.post("/api/mcp/oauth/clients", async request => { + const input = clientInput.parse(request.body) + if (input.redirectUris.some(uri => !safeRedirect(uri))) throw new McpError("INVALID_REDIRECT", "Redirect требует HTTPS либо localhost без fragment", 400) + const id = randomUUID() + await transaction(async db => { await db.query("INSERT INTO mcp_oauth_clients(id,name,redirect_uris) VALUES($1,$2,$3::jsonb)", [id, input.name, JSON.stringify(input.redirectUris)]); await audit(db, request.authUser!.id, "oauth.client.create", null, { id, ...input }) }) + return { clientId: id } + }) + admin.delete("/api/mcp/oauth/clients/:id", async request => { + const id = z.object({ id: z.string().uuid() }).parse(request.params).id + await transaction(async db => { await db.query("UPDATE mcp_oauth_clients SET enabled=false WHERE id=$1", [id]); await db.query("UPDATE mcp_credentials SET revoked_at=now() WHERE client_id=$1", [id]); await audit(db, request.authUser!.id, "oauth.client.disable", null, { id }) }) + return { ok: true } + }) + admin.get("/api/mcp/oauth/requests/:id", async request => { + const id = z.object({ id: z.string().uuid() }).parse(request.params).id + const { rows: [authorization] } = await pool.query("SELECT r.id,c.name,r.redirect_uri,r.scope FROM mcp_oauth_requests r JOIN mcp_oauth_clients c ON c.id=r.client_id WHERE r.id=$1 AND r.expires_at>now() AND r.code_digest IS NULL AND r.consumed_at IS NULL AND c.enabled=true", [id]) + if (!authorization) throw new McpError("NOT_FOUND", "Запрос согласия устарел", 404) + return authorization + }) + admin.post("/api/mcp/oauth/consent", async request => { + const input = consentInput.parse(request.body) + return transaction(async db => { + const { rows: [authorization] } = await db.query("SELECT r.* FROM mcp_oauth_requests r JOIN mcp_oauth_clients c ON c.id=r.client_id WHERE r.id=$1 AND r.expires_at>now() AND r.code_digest IS NULL AND r.consumed_at IS NULL AND c.enabled=true FOR UPDATE OF r", [input.requestId]) + if (!authorization) throw new McpError("NOT_FOUND", "Запрос согласия устарел", 404) + const redirect = new URL(authorization.redirect_uri) + if (authorization.state) redirect.searchParams.set("state", authorization.state) + redirect.searchParams.set("iss", issuer) + let connectionId: string | null = null + if (input.approve) { + const connection = await getConnection(input.connectionId, db) + const generation = await accessGeneration(), now = Date.now() + if (connection.owner_id !== request.authUser!.id || connection.policy.status !== "active" || connection.generation !== generation || + Date.parse(connection.policy.expiresAt) <= now || connection.owner_valid_until.getTime() <= now) { + throw new McpError("PERMISSION_DENIED", "Выберите своё действующее подключение с подтверждёнными правами владельца") + } + connectionId = connection.id + const code = randomToken("code"); redirect.searchParams.set("code", code) + await db.query("UPDATE mcp_oauth_requests SET code_digest=$2,connection_id=$3 WHERE id=$1", [authorization.id, tokenDigest(code), connectionId]) + } else { + redirect.searchParams.set("error", "access_denied") + await db.query("UPDATE mcp_oauth_requests SET consumed_at=now() WHERE id=$1", [authorization.id]) + } + await audit(db, request.authUser!.id, "oauth.consent", connectionId, { clientId: authorization.client_id, approved: input.approve }) + return { redirectUrl: redirect.href } + }) + }) + }) +} diff --git a/backend/src/routes/mcp-portal.ts b/backend/src/routes/mcp-portal.ts new file mode 100644 index 00000000..d54fe078 --- /dev/null +++ b/backend/src/routes/mcp-portal.ts @@ -0,0 +1,21 @@ +import type { FastifyPluginAsync } from "fastify" +import { z } from "zod" +import { sharedDatabaseGate } from "../services/shared-database-gate.js" +import { acceptPortalRevocation } from "../modules/mcp/portal-revocation.js" +import { InvalidPortalEventTime } from "../modules/mcp/portal-revocation-event.js" +import { McpError } from "../modules/mcp/security.js" + +/** Machine-to-machine endpoint; portal HMAC is separate from all JWT/MCP secrets. */ +export const mcpPortal: FastifyPluginAsync = async app => { + app.removeContentTypeParser("application/json") + app.addContentTypeParser("application/json", { parseAs: "buffer", bodyLimit: 4096 }, (_request, body, done) => done(null, body)) + app.post("/_internal/mcp/portal/revocations", { bodyLimit: 4096 }, async (request, reply) => { + try { + return await sharedDatabaseGate.request(() => acceptPortalRevocation(request.body as Buffer, request.headers["x-mm-timestamp"], request.headers["x-mm-signature"], process.env.MCP_PORTAL_WEBHOOK_SECRET ?? "")) + } catch (error) { + if (error instanceof McpError) return reply.code(error.statusCode).send({ code: error.code, error: error.message }) + if (error instanceof z.ZodError || error instanceof SyntaxError || error instanceof InvalidPortalEventTime) return reply.code(400).send({ code: "INVALID_EVENT", error: "Некорректное событие портала" }) + return reply.code(503).send({ code: "AUDIT_UNAVAILABLE", error: "Событие не подтверждено; повторите с тем же eventId" }) + } + }) +} diff --git a/backend/src/routes/mcp-transport.ts b/backend/src/routes/mcp-transport.ts new file mode 100644 index 00000000..c5bdc411 --- /dev/null +++ b/backend/src/routes/mcp-transport.ts @@ -0,0 +1,217 @@ +import { admitMcpRequestBody } from "../modules/mcp/wire-admission.js" +import { SdkBudgetedMcpServer, createSdkTerminalBudget, withFactorylessSdkJsonGuard, type SdkTerminalBudget } from "../modules/mcp/sdk-terminal-budget.js" +import { boundedToolError } from "../modules/mcp/result-budget.js" +import { deliverRegisteredToolResult } from "../modules/mcp/result-delivery.js" +import { planFlowEnvelope } from "../modules/mcp/flow-envelope-allowance.js" +import { getFlowReadResourceProfile } from "../services/traffic-flow-read-resource-profile.js" +import { createFlowReadResponseOwner, type FlowReadResponseOwner } from "../services/traffic-flow-read-budget.js" +import { flowReadBudget, flowResponseReadLimits, FLOW_READ_RESERVATION_BYTES } from "../services/traffic-flow-read-policy.js" +import type { FastifyPluginAsync } from "fastify" +import { recordUnauthorizedProtocol, reserveProtocolEnvelope } from "../modules/mcp/protocol-limits.js" +import { randomUUID } from "node:crypto" +import { McpServer, createMcpHandler } from "@modelcontextprotocol/server" +import { toNodeHandler } from "@modelcontextprotocol/node" +import { hostHeaderValidation, originValidation } from "@modelcontextprotocol/fastify" +import { env } from "../config.js" +import { authorizedMcpBindings } from "../modules/mcp/authorized-bindings.js" +import { classifyMcpLane } from "../modules/mcp/control-lane.js" +import { runMcpTransportLifecycle, createMcpResponseObservation } from "../modules/mcp/transport-lifecycle.js" +import { createMcpInFlightBudget, McpInFlightRefusal } from "../modules/mcp/inflight-owner.js" +import { listActions } from "../modules/mcp/actions.js" +import { authenticateMcpToken, effectiveMcpConnection, requestAccountingWrite, type McpPrincipal } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" +import { canonicalMcpUrl, McpError, redact } from "../modules/mcp/security.js" +import { sharedDatabaseGate } from "../services/shared-database-gate.js" +import { withMcpDatabaseRequest } from "../modules/mcp/database-request-lease.js" + +export function mcpPublicUrl(): URL { + return canonicalMcpUrl() +} +const MCP_SERVER_INFO = Object.freeze({ name: "MikrotikManager", version: "1.0.0" }) +type McpInFlightOwner = Extract["acquire"]>, { allowed: true }>["owner"] +let inFlightBudget: ReturnType | undefined +const getMcpInFlightBudget = () => inFlightBudget ??= createMcpInFlightBudget(getFlowReadResourceProfile()) +export async function makeMcpServer(principal: McpPrincipal, signal?: AbortSignal, flowOwner?: FlowReadResponseOwner, wireEra?: "legacy" | "modern"): Promise { + return makeMcpServerWithBindings(principal, authorizedMcpBindings(listActions(), await effectiveMcpConnection(principal)), signal, flowOwner, wireEra) +} +/** Private binding snapshot: routing priority and actual SDK registration share + * these same authorized action objects; runtime still revalidates live policy. */ +function makeMcpServerWithBindings(principal: McpPrincipal, bindings: ReturnType, signal?: AbortSignal, flowOwner?: FlowReadResponseOwner, wireEra?: "legacy" | "modern", genericOwner?: McpInFlightOwner, sdkBudget?: SdkTerminalBudget): McpServer { + const server = sdkBudget ? new SdkBudgetedMcpServer(sdkBudget) : new McpServer(MCP_SERVER_INFO) + if (bindings.length === 0) { + server.server.registerCapabilities({ tools: {} }) + server.server.setRequestHandler("tools/list", () => ({ tools: [] })) + } + for (const action of bindings) { + const descriptor = action.operation + server.registerTool(descriptor.toolName, { + title: descriptor.title, description: `${descriptor.title}. ${action.prepare ? "Подготавливает неизменяемый план; применение отдельным вызовом после проверки политики." : "Возвращает данные только из разрешённой области."}`, + inputSchema: action.schema, annotations: descriptor.annotations, + }, async (input, sdkContext) => { + const execute = async () => { + sdkBudget?.registeredCallbackEntered() + const contract = { id: descriptor.id, effect: descriptor.effect, hasRead: !!action.read, hasPrepare: !!action.prepare } + const invocationError = (error: unknown) => { + const code = error instanceof McpError ? error.code : "INTERNAL_ERROR" + const message = error instanceof McpError ? String(redact(error.message)) : "Операция не выполнена; подробности доступны администратору" + return boundedToolError(code, message) + } + if (descriptor.effect === "read" && descriptor.id.startsWith("traffic.flow.")) { + if (!flowOwner) return invocationError(new McpError("STATE_UNAVAILABLE", "Ответ NetFlow не имеет владельца ресурса", 503)) + try { + const contextEra = sdkContext.mcpReq.envelope ? "modern" : "legacy" + if (wireEra !== undefined && wireEra !== contextEra) throw new McpError("STATE_UNAVAILABLE", "Протокол ответа NetFlow не подтверждён", 503) + const allowance = planFlowEnvelope(sdkContext.mcpReq.id, wireEra ?? contextEra, getFlowReadResourceProfile().responseOutputBytes, MCP_SERVER_INFO) + flowOwner.retainOutput(allowance.reservedEnvelopeBytes) + return await flowOwner.run(FLOW_READ_RESERVATION_BYTES, () => deliverRegisteredToolResult(contract, async () => { + const output = await invokeMcpTool(principal, descriptor.toolName, input, signal) + if (Buffer.byteLength(JSON.stringify(output)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Ответ NetFlow превышает 1 МиБ; уменьшите страницу", 409) + return output + }, invocationError, bytes => flowOwner.retainOutput(bytes), allowance.readByteLimit)) + } catch (error) { + return invocationError(error instanceof Error && "code" in error && error.code === "RATE_LIMITED" + ? new McpError("RATE_LIMITED", "Ресурс чтения NetFlow занят; повторите запрос позже", 429) : error) + } + } + // Only invoke errors are mapped to tool failure. Budget/serialization delivery + // is handled separately, preserving real effects and prohibiting auto replay. + const toolSignal = descriptor.id === "logs.follow" + ? signal ? AbortSignal.any([signal, sdkContext.mcpReq.signal]) : sdkContext.mcpReq.signal + : signal + return deliverRegisteredToolResult(contract, () => invokeMcpTool(principal, descriptor.toolName, input, toolSignal), invocationError) + } + // Ownership never inspects/rejects an actual result after invocation. + return genericOwner ? genericOwner.callback(execute) : execute() + }) + } + return server +} +const mcpTransport: FastifyPluginAsync = async app => { + const publicUrl = mcpPublicUrl() + app.addHook("onRequest", hostHeaderValidation([publicUrl.hostname, "localhost", "127.0.0.1", "[::1]"])) + app.addHook("onRequest", originValidation([publicUrl.hostname, ...([env.CORS_ORIGIN].filter(origin => origin !== "*").map(origin => new URL(origin).hostname))])) + app.route({ + method: ["POST", "GET", "DELETE"], url: "/mcp", bodyLimit: 1024 * 1024, + handler: async (request, reply) => { + return sharedDatabaseGate.request(async () => { + if (!env.authRequired) return reply.code(503).send({ error: "MCP требует включённой авторизации портала" }) + const accountingFailure = (error: unknown) => error instanceof McpError && error.code === "PROTOCOL_STORAGE_BUSY" + ? reply.code(503).send({ error: error.message, code: error.code }) + : reply.code(503).send({ error: "Аудит MCP недоступен", code: "AUDIT_UNAVAILABLE" }) + let principal: McpPrincipal + try { principal = await authenticateMcpToken((request.headers.authorization ?? "").replace(/^Bearer /, "")) } + catch (error) { + if (error instanceof McpError) { + try { await recordUnauthorizedProtocol(request.ip, error.code) } catch (failure) { return accountingFailure(failure) } + if (error.statusCode === 401) reply.header("WWW-Authenticate", `Bearer resource_metadata="${publicUrl.origin}/.well-known/oauth-protected-resource"`) + return reply.code(error.statusCode).send({ error: error.message, code: error.code }) + } + request.log.error({ err: error }, "MCP authentication storage unavailable") + return reply.code(503).send({ error: "Хранилище MCP недоступно" }) + } + let requestLeaseEntered = false + try { return await withMcpDatabaseRequest(async () => { + requestLeaseEntered = true + let envelope: Awaited> + try { envelope = await reserveProtocolEnvelope(principal) } + catch (error) { + if (!(error instanceof McpError) || error.code !== "PROTOCOL_STORAGE_BUSY") throw error + return accountingFailure(error) + } + if (!envelope.allowed) return reply.header("Retry-After", String(envelope.retryAfterSec)).code(429).send({ code: "RATE_LIMITED", error: "Превышен лимит протокольных запросов", retryAfterSec: envelope.retryAfterSec }) + // Authentication and the existing protocol meter remain ahead of admission. + // Rejections are audited with bounded counts; raw batch bodies/ids are omitted. + if (request.method === "POST") { + const admission = admitMcpRequestBody(request.body) + if (!admission.allowed) { + try { + await requestAccountingWrite(client => client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,status,input,error_code,duration_ms,finished_at) VALUES($1,$2,NULL,'protocol:request-admission','denied',$3::jsonb,$4,0,now())", [randomUUID(), principal.connectionId, JSON.stringify({ reason: admission.reason, ...admission.metrics }), "REQUEST_ADMISSION_DENIED"])) + } catch (error) { return accountingFailure(error) } + return reply.code(admission.status).send(admission.error) + } + } + const controller = new AbortController() + const requestId = randomUUID(), started = Date.now() + const body = request.body && typeof request.body === "object" ? request.body as Record : {} + const method = typeof body.method === "string" ? body.method.slice(0, 100) : request.method + try { + await requestAccountingWrite(client => client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,status,input) VALUES($1,$2,NULL,$3,'running',$4::jsonb)", [requestId, principal.connectionId, "protocol:" + method, JSON.stringify(redact(body))])) + } catch (error) { return accountingFailure(error) } + let bindings: ReturnType + try { bindings = authorizedMcpBindings(listActions(), await effectiveMcpConnection(principal)) } + catch { + try { await requestAccountingWrite(client => client.query("UPDATE mcp_requests SET status='error',error_code='REGISTRATION_UNAVAILABLE',duration_ms=$2,finished_at=now(),result='{}'::jsonb WHERE id=$1", [requestId, Math.min(2147483647, Date.now() - started)])) } + catch { if (reply.raw.destroyed) return reply.hijack(); return reply.code(503).send({ code: "AUDIT_UNAVAILABLE", error: "Аудит MCP недоступен" }) } + if (reply.raw.destroyed) return reply.hijack() + return reply.code(503).send({ code: "STATE_UNAVAILABLE", error: "Регистрация MCP недоступна" }) + } + const lane = classifyMcpLane(request.body, bindings.map(action => action.operation)) + const budget = getMcpInFlightBudget(), acquired = budget.acquire(reply.raw, lane) + if (!acquired.allowed) { + try { await requestAccountingWrite(client => client.query("UPDATE mcp_requests SET status='denied',error_code=$2,duration_ms=$3,finished_at=now(),result=$4::jsonb WHERE id=$1", [requestId, acquired.code, Math.min(2147483647, Date.now() - started), JSON.stringify({ lane, reason: acquired.reason, ...budget.stats() })])) } + catch { if (reply.raw.destroyed) return reply.hijack(); return reply.code(503).send({ code: "AUDIT_UNAVAILABLE", error: "Аудит MCP недоступен" }) } + if (acquired.reason === "response-closed") return reply.hijack() + return reply.code(429).send({ code: "RATE_LIMITED", error: "Ресурс запросов MCP занят; повторите запрос позже" }) + } + let dispatchEntered = false + try { return await acquired.owner.dispatch(async () => { + dispatchEntered = true + const responseObservation = createMcpResponseObservation(reply.raw) + const close = () => controller.abort() + let handler: ReturnType | undefined + let sdkBudget: SdkTerminalBudget | undefined + return runMcpTransportLifecycle({ + setup: () => { + reply.raw.once("close", close) + const flowOwner = createFlowReadResponseOwner(reply.raw, flowReadBudget, flowResponseReadLimits) + if (reply.raw.writableFinished || reply.raw.destroyed) { + controller.abort() + throw new McpError("READ_CANCELLED", "Ответ закрыт до выполнения", 409) + } + const budget = createSdkTerminalBudget(MCP_SERVER_INFO, request.body) + sdkBudget = budget + handler = withFactorylessSdkJsonGuard(createMcpHandler(context => { + budget.factoryEntered() + return makeMcpServerWithBindings(principal, bindings, controller.signal, flowOwner, context.era, acquired.owner, budget) + }, { legacy: "stateless", maxRequestBodySize: 1024 * 1024, maxSubscriptions: 0 }), budget) + }, + dispatch: async () => { + reply.hijack() + await toNodeHandler(handler!)(request.raw, reply.raw, request.body) + }, + close: async () => { + reply.raw.off("close", close) + if (handler) await handler.close() + }, + audit: async terminal => { + const responseTerminal = await responseObservation.settle() + const aborted = responseTerminal.terminalEvent === "close" && responseTerminal.writableFinished === false + const unconfirmed = responseTerminal.terminalEvent === null || !responseTerminal.statusCodeObserved + const sdkDecision = sdkBudget?.auditNote() + const errorCode = aborted ? "READ_CANCELLED" : terminal.failureStage ? "MCP_TRANSPORT_FAILED" : sdkDecision ? sdkDecision.reason === "not-json" ? "SDK_RESPONSE_UNSERIALIZABLE" : "SDK_RESPONSE_TOO_LARGE" : unconfirmed ? "MCP_RESPONSE_UNCONFIRMED" : null + const status = aborted || terminal.failureStage || sdkDecision || unconfirmed || responseTerminal.statusCodeObserved && responseTerminal.statusCode !== null && responseTerminal.statusCode >= 400 ? "error" : "completed" + await requestAccountingWrite(client => client.query("UPDATE mcp_requests SET status=$2,error_code=$3,duration_ms=$4,finished_at=now(),result=$5::jsonb WHERE id=$1", [requestId, status, errorCode, Math.min(2147483647, Date.now() - started), JSON.stringify({ ...responseTerminal, transport: { ...terminal, aborted }, ...(sdkDecision ? { sdkTerminalBudget: sdkDecision } : {}) })])) + }, + auditUnavailable: (terminal, secondary) => { + request.log.error({ code: "AUDIT_UNAVAILABLE", phase: terminal.failureStage ?? "audit", secondary }, "MCP terminal protocol persistence unconfirmed") + }, + }) + }) + } catch (error) { + // Only a proven pre-entry response-close refusal gets a denial audit. + // Exceptions after SDK dispatch are never rewritten as operation failure. + if (dispatchEntered || !(error instanceof McpInFlightRefusal) || error.reason !== "response-closed") throw error + try { await requestAccountingWrite(client => client.query("UPDATE mcp_requests SET status='denied',error_code='READ_CANCELLED',duration_ms=$2,finished_at=now(),result=$3::jsonb WHERE id=$1", [requestId, Math.min(2147483647, Date.now() - started), JSON.stringify({ lane, reason: "response-closed", ...budget.stats() })])) } + catch { request.log.error({ code: "AUDIT_UNAVAILABLE", phase: "pre-dispatch", secondary: true }, "MCP terminal protocol persistence unconfirmed"); return reply.hijack() } + return reply.hijack() + } + }) } catch (error) { + if (requestLeaseEntered) throw error + if (error instanceof McpError) return reply.code(error.statusCode).send({ code: error.code, error: error.message }) + return accountingFailure(error) + } + }) + }, + }) +} +export default mcpTransport diff --git a/backend/src/routes/mcp.test.ts b/backend/src/routes/mcp.test.ts new file mode 100644 index 00000000..5af9a652 --- /dev/null +++ b/backend/src/routes/mcp.test.ts @@ -0,0 +1,226 @@ +import assert from "node:assert/strict" +import Fastify from "fastify" +import { SignJWT } from "jose" + +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "mcp-foundation-test-secret" +process.env.AUTH_ISSUER = "https://auth.test.local" +process.env.NODE_ENV = "test" +const { default: authPlugin } = await import("../plugins/auth.js") +const { createMcpRoutes, mcpDevicePlanBodySchema, mcpDevicePlanArguments } = await import("./mcp.js") +const { findAction } = await import("../modules/mcp/actions.js") +const { pool } = await import("../db/index.js") +const { sharedDatabaseGate } = await import("../services/shared-database-gate.js") +const { env } = await import("../config.js") +const { mcpPolicyPreviewRequestSchema, mcpCatalogResponseSchema, mcpPolicyPreviewResponseSchema } = await import("@mmapp/contracts/mcp") +const { listMcpOperations } = await import("../modules/mcp/registry.js") +const { coverageSchema } = await import("../modules/mcp/coverage.js") + +const app = Fastify() +await app.register(authPlugin) +// Deliberately omit the global requireApiAuth hook: the module must protect itself. +let claimChecks = 0 +await app.register(createMcpRoutes(async () => { claimChecks++ }), { prefix: "/api" }) +async function token(permissions: string[], options: { issuer?: string; apps?: string[]; secret?: string; expiration?: string } = {}) { + return new SignJWT({ sub: "human-admin", apps: options.apps ?? ["mm"], permissions }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(options.issuer ?? env.authIssuer) + .setExpirationTime(options.expiration ?? "1h") + .sign(new TextEncoder().encode(options.secret ?? env.jwtSecret)) +} +const admin = { authorization: "Bearer " + await token(["mm:settings:admin"]) } +const payload = mcpPolicyPreviewRequestSchema.parse({ + operationId: "logs.query", + ownerPermissions: ["mm:servers:read"], + policy: { + id: "simulation", version: 1, status: "active", expiresAt: "2099-01-01T00:00:00Z", mode: "observer", + permissions: ["mm:servers:read"], scope: { serverIds: [1], operationIds: ["logs.query"] }, + }, + target: { scope: "servers", serverIds: [1] }, +}) +try { + await sharedDatabaseGate.exclusive(async () => { + const before = claimChecks + const response = await app.inject({ url: "/api/mcp/catalog", headers: admin }) + assert.equal(response.statusCode, 503) + assert.equal(claimChecks, before, "Maintenance stops the claims hook before SQL") + }) + for (const request of [{ method: "GET" as const, url: "/api/mcp/catalog" }, { method: "POST" as const, url: "/api/mcp/policy/preview", payload }]) { + assert.equal((await app.inject(request)).statusCode, 401) + assert.equal((await app.inject({ ...request, headers: { authorization: "Bearer arbitrary-agent-token" } })).statusCode, 401) + assert.equal((await app.inject({ ...request, headers: { authorization: "Bearer " + await token(["mm:network:write"]) } })).statusCode, 403) + for (const options of [{ issuer: "https://wrong.test" }, { secret: "different-mcp-only-signing-key" }, { expiration: "-1h" }]) { + assert.equal((await app.inject({ ...request, headers: { authorization: "Bearer " + await token(["mm:mcp:admin"], options) } })).statusCode, 401) + } + assert.equal((await app.inject({ ...request, headers: { authorization: "Bearer " + await token(["mm:mcp:admin"], { apps: ["other"] }) } })).statusCode, 403) + } + const noSubject = await new SignJWT({ apps: ["mm"], permissions: ["mm:settings:admin"] }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + assert.equal((await app.inject({ url: "/api/mcp/catalog", headers: { authorization: "Bearer " + noSubject } })).statusCode, 401) + for (const permission of ["mm:mcp:read", "mm:mcp:admin", "mm:settings:admin"]) { + const response = await app.inject({ url: "/api/mcp/catalog", headers: { authorization: "Bearer " + await token([permission]) } }) + assert.equal(response.statusCode, 200) + const catalog = mcpCatalogResponseSchema.parse(response.json()) + assert.equal(catalog.executionAvailable, true) + assert.equal(catalog.operations.length, listMcpOperations().length) + } + for (const permission of ["mm:mcp:read", "mm:mcp:approve", "mm:mcp:pause"]) { + assert.equal((await app.inject({ method: "POST", url: "/api/mcp/policy/preview", payload, headers: { authorization: "Bearer " + await token([permission]) } })).statusCode, 403) + } + const response = await app.inject({ method: "POST", url: "/api/mcp/policy/preview", payload, headers: admin }) + assert.equal(response.statusCode, 200) + const preview = mcpPolicyPreviewResponseSchema.parse(response.json()) + assert.equal(preview.simulation, true) + assert.equal(preview.executionAvailable, false) + assert.equal(preview.result.decision, "allowed") + const coverage = await app.inject({ url: "/api/mcp/coverage", headers: admin }) + assert.equal(coverage.statusCode, 200) + const routeCoverage = coverageSchema.parse(coverage.json()) + assert.ok(routeCoverage.routes.some(route => route.status === "mediated")) + assert.ok(routeCoverage.routes.some(route => route.status === "excluded")) + assert.equal(routeCoverage.routes.filter(route => route.status === "pending").length, 0, + "Completed adapter inventory must not expose pending business routes") + assert.equal((await app.inject({ url: "/api/mcp/coverage", headers: { authorization: "Bearer " + await token(["mm:mcp:read"]) } })).statusCode, 403) + assert.equal((await app.inject({ method: "POST", url: "/api/mcp/policy/preview", payload: { ...payload, approved: true }, headers: admin })).statusCode, 400) + assert.equal((await app.inject({ method: "POST", url: "/api/mcp/policy/preview", payload: { ...payload, operationId: "unknown" }, headers: admin })).json().result.decision, "denied") + const createBody = { connectionId: "66f196c9-4b8a-4884-bccd-ec3236c3d3f6", action: "create" as const, fields: { name: "web-created-bridge" } } + for (const id of ["*OLD", undefined, null, ""]) { + const body = { ...createBody, id } + assert.equal(Object.hasOwn(body, "id"), true) + assert.equal(mcpDevicePlanBodySchema.safeParse(body).success, false, "Explicit CREATE id is forbidden even when undefined") + } + const createArguments = mcpDevicePlanArguments(1, mcpDevicePlanBodySchema.parse(createBody)) + assert.equal(Object.hasOwn(createArguments, "id"), false, "In-process MCP invocation must omit the id key, not only JSON serialization") + assert.deepEqual(createArguments, { serverId: 1, fields: createBody.fields }) + assert.deepEqual(JSON.parse(JSON.stringify(createArguments)), createArguments) + assert.equal(findAction("mm_bridge_create_plan").schema.safeParse(createArguments).success, true, "Real strict CREATE action accepts web adapter arguments") + for (const action of ["update", "delete"] as const) { + const fields = action === "update" ? { comment: "edited" } : {} + const arguments_ = mcpDevicePlanArguments(1, mcpDevicePlanBodySchema.parse({ ...createBody, action, id: "*STABLE", fields })) + assert.deepEqual(arguments_, { serverId: 1, id: "*STABLE", fields }) + } + const originalQuery = pool.query, originalConnect = pool.connect + let sqlCalls = 0 + const forbiddenSql = () => { sqlCalls++; throw new Error("Unexpected owner/SQL access for invalid CREATE identity") } + pool.query = forbiddenSql as typeof pool.query; pool.connect = forbiddenSql as typeof pool.connect + try { + for (const id of ["*OLD", null, ""]) { + const rejected = await app.inject({ method: "POST", url: "/api/mcp/devices/1/bridge/plan", headers: admin, payload: { ...createBody, id } }) + assert.equal(rejected.statusCode, 400) + assert.equal(rejected.json().code, "INVALID_INPUT") + } + assert.equal(sqlCalls, 0, "Malformed CREATE must stop before ownerPrincipal, audit and native dispatch") + } 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 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` + let ownerQueries = 0, credentialQueries = 0, downstreamQueries = 0 + pool.query = (async (sql: string, values?: unknown[]) => { + if (sql === connectionSql) { + ownerQueries++; assert.deepEqual(values, [createBody.connectionId]) + return { rows: [{ id: createBody.connectionId, owner_id: "human-admin", policy: payload.policy }] } + } + if (sql === credentialSql) { + credentialQueries++; assert.deepEqual(values, [createBody.connectionId]); return { rows: [] } + } + downstreamQueries++; throw Error("No-current-credential owner request must not query plans, audit or downstream state") + }) as typeof pool.query + pool.connect = (() => { downstreamQueries++; throw Error("No-current-credential owner request must not start a transaction") }) as typeof pool.connect + try { + const ownerHeaders = { authorization: "Bearer " + await token(["mm:mcp:admin", "mm:network:read", "mm:network:write"]) } + const requests = [ + { method: "GET" as const, url: "/api/mcp/devices/1/bridge?connectionId=" + createBody.connectionId }, + { method: "POST" as const, url: "/api/mcp/devices/1/bridge/plan", payload: createBody }, + ] + for (const request of requests) { + const claimsBefore = claimChecks, ownersBefore = ownerQueries, credentialsBefore = credentialQueries + const rejected = await app.inject({ ...request, headers: ownerHeaders }) + assert.equal(rejected.statusCode, 403); assert.equal(rejected.json().code, "PERMISSION_DENIED") + assert.equal(claimChecks, claimsBefore + 1, "Valid human claims are checked outside delegated MCP denial") + assert.equal(ownerQueries, ownersBefore + 1); assert.equal(credentialQueries, credentialsBefore + 1) + assert.equal(downstreamQueries, 0, "Missing delegated credential must stop before plans, audit, transactions and native dispatch") + const invalid = await app.inject({ ...request, headers: { authorization: "Bearer arbitrary-agent-token" } }) + assert.equal(invalid.statusCode, 401) + assert.equal(claimChecks, claimsBefore + 1, "Invalid human JWT stops before the claims and owner SQL hooks") + assert.equal(ownerQueries, ownersBefore + 1); assert.equal(credentialQueries, credentialsBefore + 1); assert.equal(downstreamQueries, 0) + } + assert.equal(ownerQueries, 2); assert.equal(credentialQueries, 2) + } finally { pool.query = originalQuery; pool.connect = originalConnect } + // The recognized kind must validate its signed metadata, rather than fall + // through to generic log payloads or accept a forged kind-only record. + const artifactId = "5b79128d-286a-491b-b9fa-41bd02120e46" + let artifactQueries = 0 + pool.query = (async (sql: string) => { + artifactQueries++ + assert.match(sql, /^SELECT id,connection_id,filename,media_type,/) + return { rows: [{ id: artifactId, connection_id: "private-connection", filename: "NEVER_PRIVATE_NAME", media_type: "application/json", access_scope: { kind: "ipsec-existing-certificate-bundle" } }] } + }) as typeof pool.query + pool.connect = (() => { throw Error("Malformed artifact may not open SQL transactions") }) as typeof pool.connect + try { + const response = await app.inject({ url: `/api/mcp/artifacts/${artifactId}`, headers: { authorization: "Bearer " + await token(["mm:mcp:read", "mm:network:write"]) } }) + assert.equal(response.statusCode, 409) + assert.equal(response.json().code, "ARTIFACT_INVALID") + assert.ok(!response.body.includes("NEVER_")) + assert.equal(artifactQueries, 2, "malformed artifact stops after bounded metadata before ciphertext, owner, source or native access") + } finally { pool.query = originalQuery; pool.connect = originalConnect } + const profile = { + id: createBody.connectionId, connection_id: createBody.connectionId, version: 2, + config: { name: "Route AI test" }, enabled: true, grant_expires_at: "2099-01-01T00:00:00Z", updated_at: "2026-11-01T00:00:00Z", + state: { reason: "План ожидает подтверждения", candidateId: "wan-b", failureCount: 3, + baseline: { nativeRule: "NEVER_PRIVATE_BASELINE" }, failoverReserveCursor: "NEVER_PRIVATE_CURSOR" }, + } + pool.query = (async (sql: string, values: unknown[]) => { + assert.match(sql, /FROM mcp_routing_profiles p JOIN mcp_connections c/) + assert.deepEqual(values, [true, "human-admin"]) + return { rows: [profile] } + }) as typeof pool.query + try { + const listed = await app.inject({ url: "/api/mcp/routing-profiles", headers: admin }) + assert.equal(listed.statusCode, 200, listed.body) + assert.deepEqual(listed.json().profiles, [{ ...profile, state: { reason: profile.state.reason, candidateId: "wan-b", failureCount: 3 } }]) + assert.ok(!listed.body.includes("NEVER_PRIVATE_")) + } finally { pool.query = originalQuery } + const monitoredOperation = "58d8e404-7209-4fd3-88b3-91f9bed9425a" + const monitoringHeaders = { authorization: "Bearer " + await token(["mm:mcp:read"]) } + const monitoringUrl = `/api/mcp/operations?operationId=${monitoredOperation}&limit=1` + assert.equal((await app.inject({ url: monitoringUrl })).statusCode, 401) + let operationVisible = true + let stepReads = 0 + pool.query = (async (sql: string, values: unknown[]) => { + if (sql.includes("FROM mcp_operation_steps")) { + stepReads++ + assert.match(sql, /SELECT step_index,server_id,state,finished_at/) + assert.match(sql, /LIMIT 101$/) + assert.deepEqual(values, [monitoredOperation]) + return { rows: Array.from({ length: 101 }, (_, step_index) => ({ step_index, state: "verified" })) } + } + assert.match(sql, /c\.owner_id=\$2/) + assert.match(sql, /r\.id=\$3::uuid/) + if (sql.startsWith("SELECT count")) { + assert.deepEqual(values.slice(0, 3), [false, "human-admin", monitoredOperation]) + return { rows: [{ total: operationVisible ? 1 : 0 }] } + } + assert.match(sql, /FROM mcp_operations r/) + assert.deepEqual(values, [false, "human-admin", monitoredOperation, 2]) + return { rows: operationVisible ? [{ id: monitoredOperation, state: "partial" }] : [] } + }) as typeof pool.query + try { + const result = await app.inject({ url: monitoringUrl, headers: monitoringHeaders }) + assert.equal(result.statusCode, 200, result.body) + assert.equal(result.json().items[0].steps.length, 100) + assert.equal(result.json().items[0].stepsTruncated, true) + assert.equal(result.json().items[0].state, "partial") + operationVisible = false + assert.deepEqual((await app.inject({ url: monitoringUrl, headers: monitoringHeaders })).json(), { items: [], total: 0, nextCursor: null }) + assert.equal(stepReads, 1, "An inaccessible operation must not load its steps") + assert.equal((await app.inject({ url: monitoringUrl.replace("/operations", "/plans"), headers: monitoringHeaders })).statusCode, 400) + } finally { pool.query = originalQuery } + env.authRequired = false + assert.equal((await app.inject({ url: "/api/mcp/catalog" })).statusCode, 503) + assert.equal((await app.inject({ method: "POST", url: "/api/mcp/policy/preview", payload, headers: admin })).statusCode, 503) + assert.ok(claimChecks > 0) + console.log("mcp.test.ts: protected catalog/simulation and strict web CREATE identity pass; omitted ID survives in-process MCP args, explicit ID rejected before owner SQL, stable update/delete retained") +} finally { + env.authRequired = true + await app.close() +} diff --git a/backend/src/routes/mcp.ts b/backend/src/routes/mcp.ts new file mode 100644 index 00000000..da43699c --- /dev/null +++ b/backend/src/routes/mcp.ts @@ -0,0 +1,447 @@ +import { MCP_REQUEST_MONITOR_PROJECTION } from "../modules/mcp/request-monitor-projection.js" +import { humanIpsecUserBundleMetadata, readHumanIpsecUserBundleArtifact } from "../modules/mcp/ipsec-user-create-artifact.js" +import { humanIpsecExistingCertificateExportMetadata, readHumanIpsecExistingCertificateExportArtifact } from "../services/ipsec-existing-certificate-export-artifact.js" +import type { createIpsecExistingCertificateExportArtifactStore } from "../services/ipsec-existing-certificate-export-artifact.js" +import type { FastifyPluginAsync } from "fastify" +import { z } from "zod" +import { bridgeVlanConfigurationRequestSchema } from "@mmapp/contracts/bridge-vlan" +import { randomUUID, createHash } from "node:crypto" +import { mcpCatalogResponseSchema, mcpConnectionCreateSchema, mcpPageQuerySchema, mcpPolicyPreviewRequestSchema, mcpPolicyPreviewResponseSchema, mcpSettingsPatchSchema, mcpTemporaryGrantSchema } from "@mmapp/contracts/mcp" +import { mcpProtectionCreateRequestSchema } from "../modules/mcp/protection-requests.js" +import { env } from "../config.js" +import { requireAuth } from "../plugins/auth.js" +import { listMcpOperations, findMcpOperation } from "../modules/mcp/registry.js" +import { evaluateMcpPolicy } from "../modules/mcp/policy.js" +import { listActions } from "../modules/mcp/actions.js" +import { pool } from "../db/index.js" +import { hasPermission, type AuthUser } from "../lib/permissions.js" +import { accessGeneration, decryptSecret, encryptSecret, McpError, randomToken, redact, tokenDigest } from "../modules/mcp/security.js" +import { createMcpConnection, listMcpConnections, getMcpSettings, updateMcpSettings, getConnection, assertConnectionVisible, rotateConnectionToken, setConnectionStatus, updateMcpConnection, connectionWrite, requestAccountingWrite, audit } from "../modules/mcp/store.js" +import { approvePlan, loadPlan, publicPlan, readMcpPolicyPreviewInput } from "../modules/mcp/runtime.js" +import { applyPlan, invokeMcpTool } from "../modules/mcp/runtime.js" +import { findOwnerPlanReplayTombstone } from "../modules/mcp/operation-replay-tombstones.js" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { revalidateMcpOwner } from "../modules/mcp/store.js" +import { resourceById } from "../modules/mcp/resources.js" +import { registerManagementChannel } from "../modules/mcp/management.js" +import { MAX_RESTORE_ARCHIVE_BYTES, uploadRestoreArchive, readRestoreReceipt, readRestoreReceiptAuditStatus } from "../modules/mcp/database-restore.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { sharedDatabaseGate } from "../services/shared-database-gate.js" +import { withDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" +import { readFile } from "node:fs/promises" +import { checkMcpProtocol, type ProtocolProbe } from "../modules/mcp/readiness.js" +import { publicRoutingControllerState } from "../modules/mcp/routing-community-public.js" +import { humanBackupDownloadMetadata, readHumanBackupDownloadArtifact } from "../modules/mcp/backup-download-artifact.js" +import { humanDatabaseBackupMetadata, readHumanDatabaseBackupArtifact } from "../modules/mcp/database-backup-artifact.js" +import { humanWireguardExportMetadata, readHumanWireguardExportArtifact } from "../modules/mcp/wireguard-export-artifact.js" +import { humanArtifactReceipts } from "../modules/mcp/human-artifact-receipts.js" +import { readHumanHostFilesArtifact } from "../modules/mcp/host-files-artifact.js" +import { loadLogExportArtifactMetadata } from "../modules/mcp/log-artifacts.js" +import { readServerPollCapacity } from "../services/server-poll-capacity.js" +import { connectProbeStorage } from "../services/server-probe-storage-connection.js" + +const uuid = z.string().uuid() +const selectedPolicyPreviewSchema = mcpPolicyPreviewRequestSchema.pick({ operationId: true, target: true, objects: true, objectCount: true }).extend({ connectionId: uuid }) +const deviceResource = z.enum(["ethernet", "bridge", "bridge_port", "bridge_vlan", "vlan", "ipv4_address", "ipv6_address", "vrf"]) +const devicePlanCommon = { connectionId: uuid, fields: z.record(z.string(), z.unknown()).default({}) } +export const mcpDevicePlanBodySchema = z.discriminatedUnion("action", [ + z.object({ ...devicePlanCommon, action: z.literal("create") }).strict(), + z.object({ ...devicePlanCommon, action: z.literal("update"), id: z.string().min(1).max(200).optional() }).strict(), + z.object({ ...devicePlanCommon, action: z.literal("delete"), id: z.string().min(1).max(200).optional() }).strict(), +]) +export function mcpDevicePlanArguments(serverId: number, input: z.infer) { + return { serverId, ...(input.action === "create" ? {} : { id: input.id }), fields: input.fields } +} +const idOf = (params: unknown) => z.object({ id: uuid }).parse(params).id +const actorOf = (actor?: AuthUser) => { if (!actor) throw new McpError("UNAUTHORIZED", "Требуется вход", 401); return actor } +async function pollCapacityForPortal(ownerId:string,admin:boolean){ + const options={deadlineAt:Date.now()+3000} + let connection:Awaited>|undefined + try{ + connection=await connectProbeStorage(options) + return await readServerPollCapacity({query:(text,values)=>connection!.query(text,values,{timeoutMs:Math.max(1,options.deadlineAt-Date.now())})},ownerId,admin) + }catch{return {status:"unavailable" as const}} + finally{connection?.release()} +} +/** Only the delegated MCP identity failed; the human JWT/claims hooks ran outside. + * Never translate OWNER_CLAIMS_REVOKED or another authentication failure. */ +function auditOwnerRequest(actorId: string, action: string, connectionId: string, details: unknown) { + return requestAccountingWrite(client => audit(client, actorId, action, connectionId, details)) +} + +async function ownerDelegate(work: () => Promise): Promise { + try { return await work() } + catch (error) { + if (error instanceof McpError && error.code === "UNAUTHORIZED" && error.statusCode === 401) + throw new McpError("PERMISSION_DENIED", "Допуск MCP подключения недоступен", 403) + throw error + } +} +async function ownerPrincipal(connectionId: string, actor: AuthUser, operationId: string) { + const connection = await getConnection(connectionId) + if (connection.owner_id !== actor.id) throw new McpError("PERMISSION_DENIED", "Выполнение из web UI доступно владельцу подключения") + const action = listActions().find(action => action.operation.id === operationId) + if (!action || action.operation.requiredPermissions.some(permission => !hasPermission(actor.permissions, permission))) throw new McpError("PERMISSION_DENIED", "Текущий пользователь не имеет права выполнить операцию") + // Fixed UI actions use the connection's existing access without exposing credentials. + const { rows: [credential] } = await pool.query(`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`, [connectionId]) + if (!credential) throw new McpError("PERMISSION_DENIED", "У подключения нет действующего основного допуска", 403) + return { connectionId, credentialId: credential.id, credentialExpiresAt: credential.expires_at.toISOString() } +} + +/** Human administration and fixed owner UI actions; never a generic RouterOS proxy. */ +export function createMcpRoutes(checkClaims: typeof assertPortalClaimsCurrent = assertPortalClaimsCurrent, options: { existingCertificateArtifact?: Pick, "metadata" | "read"> } = {}): FastifyPluginAsync { + const existingCertificateArtifact = options.existingCertificateArtifact ?? { metadata: humanIpsecExistingCertificateExportMetadata, read: readHumanIpsecExistingCertificateExportArtifact } + return async app => { + app.addContentTypeParser("application/octet-stream", { parseAs: "buffer", bodyLimit: MAX_RESTORE_ARCHIVE_BYTES }, (_request, body, done) => done(null, body)) + app.setErrorHandler((error, request, reply) => { + if (error instanceof z.ZodError) return reply.code(400).send({ error: "Некорректные параметры MCP", code: "INVALID_INPUT", details: error.flatten() }) + if (error instanceof McpError) return reply.code(error.statusCode).send({ error: error.message, code: error.code }) + request.log.error({ error: redact(error instanceof Error ? error.message : error) }, "MCP administration failed") + return reply.code(503).send({ error: "Хранилище MCP или аудит недоступны", code: "AUDIT_UNAVAILABLE" }) + }) + app.addHook("onRequest", async (request, reply) => { + if (!env.authRequired) return reply.code(503).send({ error: "Для управления MCP включите проверку входа через портал" }) + // Explicit guard: these routes must stay protected if the global API hook changes. + await requireAuth(request, reply) + if (reply.sent) return + // This hook runs before the API handler's database admission. + const actor = actorOf(request.authUser) + await sharedDatabaseGate.request(() => checkClaims === assertPortalClaimsCurrent + ? withDatabaseApiRequest(() => checkClaims(actor)) + : checkClaims(actor)) + if (request.headers.origin && request.headers.origin !== env.CORS_ORIGIN) return reply.code(403).send({ error: "Недопустимый Origin" }) + }) + + app.post("/mcp/database/archives", { bodyLimit: MAX_RESTORE_ARCHIVE_BYTES }, async request => uploadRestoreArchive(request.body as Buffer, actorOf(request.authUser))) + app.get("/mcp/database/receipts/:id", async request => readRestoreReceipt(idOf(request.params), actorOf(request.authUser))) + app.get("/mcp/database/receipts/:id/audit-status", async request => readRestoreReceiptAuditStatus(idOf(request.params), actorOf(request.authUser))) + app.post("/mcp/database/plan", async request => { + const { connectionId, archiveRef, expectedSha256 } = z.object({ connectionId: uuid, archiveRef: uuid, expectedSha256: z.string().regex(/^[a-f0-9]{64}$/) }).strict().parse(request.body) + const actor = actorOf(request.authUser) + return ownerDelegate(async () => invokeMcpTool(await ownerPrincipal(connectionId, actor, "database.restore"), "mm_database_restore_plan", { archiveRef, expectedSha256 })) + }) + app.get("/mcp/catalog", async () => mcpCatalogResponseSchema.parse({ + stage: "runtime", executionAvailable: true, operations: listMcpOperations(), + })) + app.get("/mcp/coverage", async () => JSON.parse(await readFile(new URL("../../mcp-coverage.json", import.meta.url), "utf8"))) + app.get("/mcp/management-channels", async () => ({ channels: (await pool.query("SELECT id,server_id,created_by,attestation,expires_at,verified_at,revoked_at FROM mcp_management_channels ORDER BY server_id")).rows })) + app.post("/mcp/management-channels", async request => registerManagementChannel(request.body, actorOf(request.authUser))) + app.delete("/mcp/management-channels/:id", async request => { + const actor = actorOf(request.authUser), id = idOf(request.params) + await connectionWrite(async db => { + await db.query("UPDATE mcp_management_channels SET revoked_at=now() WHERE id=$1", [id]) + await audit(db, actor.id, "management.revoke", null, { channelId: id }) + }); return { ok: true } + }) + + app.post("/mcp/policy/preview", async (request, reply) => { + const parsed = z.union([selectedPolicyPreviewSchema, mcpPolicyPreviewRequestSchema]).safeParse(request.body) + if (!parsed.success) return reply.code(400).send({ error: "Некорректные параметры симуляции политики MCP", details: parsed.error.flatten() }) + const now = new Date() + let input + if ("connectionId" in parsed.data) { + const connection = await getConnection(parsed.data.connectionId) + assertConnectionVisible(connection, actorOf(request.authUser)) + const [generation, settings, current] = await Promise.all([ + accessGeneration(), getMcpSettings(), readMcpPolicyPreviewInput(connection, parsed.data), + ]) + const inactive = !settings.enabled || connection.generation !== generation || connection.owner_valid_until <= now + input = inactive && current.policy.status === "active" ? { ...current, policy: { ...current.policy, status: "paused" as const } } : current + } else input = parsed.data + return mcpPolicyPreviewResponseSchema.parse({ + simulation: true, + executionAvailable: false, + evaluatedAt: now.toISOString(), + policyVersion: input.policy.version, + result: evaluateMcpPolicy(input, now), + }) + }) + + app.get("/mcp/connections", async request => ({ connections: await listMcpConnections(actorOf(request.authUser)) })) + app.post("/mcp/connections", async request => { + const input = mcpConnectionCreateSchema.parse(request.body) + const available = new Set(listActions().map(action => action.operation.id)) + if (input.scope.operationIds.some(id => !available.has(id))) throw new McpError("UNKNOWN_OPERATION", "Можно выдать только реализованные операции", 400) + return createMcpConnection(input, actorOf(request.authUser)) + }) + app.put("/mcp/connections/:id", async request => { + const input = mcpConnectionCreateSchema.parse(request.body), available = new Set(listActions().map(action => action.operation.id)) + if (input.scope.operationIds.some(id => !available.has(id))) throw new McpError("UNKNOWN_OPERATION", "Можно выдать только реализованные операции", 400) + return updateMcpConnection(idOf(request.params), input, actorOf(request.authUser)) + }) + app.post("/mcp/connections/:id/status", async request => { + await setConnectionStatus(idOf(request.params), z.object({ status: z.enum(["active", "paused", "revoked"]) }).strict().parse(request.body).status, actorOf(request.authUser)); return { ok: true } + }) + app.post("/mcp/connections/:id/rotate", async request => rotateConnectionToken(idOf(request.params), actorOf(request.authUser))) + app.post("/mcp/connections/:id/revalidate", async request => revalidateMcpOwner(idOf(request.params), actorOf(request.authUser))) + app.get("/mcp/devices/catalog", async request => { + const { connectionId } = z.object({ connectionId: uuid }).strict().parse(request.query), actor = actorOf(request.authUser), connection = await getConnection(connectionId) + if (connection.owner_id !== actor.id) throw new McpError("PERMISSION_DENIED", "Выберите своё подключение") + const servers = (await pool.query("SELECT id,name,host,enabled FROM servers WHERE id=ANY($1::bigint[]) ORDER BY name LIMIT 1000", [connection.policy.scope.serverIds])).rows + const resources = deviceResource.options.map(id => { + const resource = resourceById(id), json = z.toJSONSchema(resource.fields, { io: "input" }) + const allowed = (action: string) => connection.policy.scope.operationIds.includes(`ros.${id}.${action}`) && hasPermission(actor.permissions, resource.permission + (action === "list" ? ":read" : ":write")) && (action === "list" || connection.policy.mode !== "observer") + const fields = Object.entries(json.properties ?? {}).map(([key, property]) => { + const schema = property as { type?: string; enum?: string[]; minimum?: number; maximum?: number; minLength?: number; maxLength?: number } + return { key, type: schema.type === "boolean" ? "boolean" : ["number", "integer"].includes(schema.type ?? "") ? "number" : "string", ...(schema.enum ? { options: schema.enum } : {}), min: schema.minimum, max: schema.maximum, maxLength: schema.maxLength, requiredOnCreate: resource.requiredOnCreate?.includes(key) ?? false } + }) + return { id, title: resource.title, fields, read: allowed("list"), create: resource.create && allowed("create"), update: allowed("update"), delete: resource.remove && allowed("delete") } + }).filter(resource => resource.read || resource.create || resource.update || resource.delete) + return { servers, resources } + }) + app.get("/mcp/devices/:serverId/:resource", async request => { + const params = z.object({ serverId: z.coerce.number().int().positive(), resource: deviceResource }).parse(request.params) + const query = z.object({ connectionId: uuid, offset: z.coerce.number().int().min(0).max(1000000).default(0) }).strict().parse(request.query) + const principal = await ownerPrincipal(query.connectionId, actorOf(request.authUser), `ros.${params.resource}.list`) + return ownerDelegate(async () => invokeMcpTool(principal, `mm_${params.resource}_list`, { serverId: params.serverId, limit: 100, offset: query.offset })) + }) + app.post("/mcp/devices/:serverId/:resource/plan", async request => { + const params = z.object({ serverId: z.coerce.number().int().positive(), resource: deviceResource }).parse(request.params) + const input = mcpDevicePlanBodySchema.parse(request.body) + const actor = actorOf(request.authUser), principal = await ownerPrincipal(input.connectionId, actor, `ros.${params.resource}.${input.action}`) + await auditOwnerRequest(actor.id, "device.web.plan", input.connectionId, { serverId: params.serverId, resource: params.resource, action: input.action, objectId: input.action === "create" ? null : input.id ?? null }) + return ownerDelegate(async () => invokeMcpTool(principal, `mm_${params.resource}_${input.action}_plan`, mcpDevicePlanArguments(params.serverId, input))) + }) + app.get("/mcp/devices/:serverId/bridge-vlan/configuration", async request => { + const { serverId } = z.object({ serverId: z.coerce.number().int().positive() }).parse(request.params) + const { connectionId } = z.object({ connectionId: uuid }).strict().parse(request.query) + const principal = await ownerPrincipal(connectionId, actorOf(request.authUser), "network.bridge-vlan.read") + return ownerDelegate(() => invokeMcpTool(principal, "mm_bridge_vlan_configuration_get", { serverId })) + }) + app.post("/mcp/devices/:serverId/bridge-vlan/configuration/plan", async request => { + const { serverId } = z.object({ serverId: z.coerce.number().int().positive() }).parse(request.params) + const { connectionId, ...input } = bridgeVlanConfigurationRequestSchema.omit({ serverId: true }).extend({ connectionId: uuid }).strict().parse(request.body) + const actor = actorOf(request.authUser) + const principal = await ownerPrincipal(connectionId, actor, "network.bridge-vlan.configure") + await auditOwnerRequest(actor.id, "device.web.vlan-plan", connectionId, { serverId, bridgeId: input.bridgeId }) + return ownerDelegate(() => invokeMcpTool(principal, "mm_bridge_vlan_configuration_plan", { serverId, ...input })) + }) + app.get("/mcp/settings", async () => getMcpSettings()) + app.put("/mcp/settings", async request => updateMcpSettings(mcpSettingsPatchSchema.parse(request.body), actorOf(request.authUser))) + app.post("/mcp/pause", async request => { await updateMcpSettings({ enabled: false }, actorOf(request.authUser)); return { ok: true } }) + app.post("/mcp/stop-automation", async request => { + const actor = actorOf(request.authUser) + await requestAccountingWrite(async client => { + await client.query("UPDATE mcp_settings SET enabled=false,updated_at=now() WHERE id=1") + await client.query("UPDATE mcp_routing_profiles SET enabled=false,state=state || '{\"pausedReason\":\"Аварийная остановка автоматических изменений\"}'::jsonb,updated_at=now() WHERE enabled") + await audit(client, actor.id, "automation.emergency-stop", null, {}) + }); return { ok: true } + }) + app.get("/mcp/overview", async request => { + const actor = actorOf(request.authUser), admin = hasPermission(actor.permissions, "mm:mcp:admin") + const { rows: [summary] } = await pool.query(`SELECT + (SELECT count(*)::int FROM mcp_connections WHERE ($1::boolean OR owner_id=$2) AND policy->>'status'='active') AS connections, + (SELECT count(*)::int FROM mcp_requests r JOIN mcp_connections c ON c.id=r.connection_id WHERE ($1::boolean OR c.owner_id=$2) AND r.created_at>now()-interval '24 hours') AS calls, + (SELECT count(*)::int FROM mcp_requests r JOIN mcp_connections c ON c.id=r.connection_id WHERE ($1::boolean OR c.owner_id=$2) AND r.created_at>now()-interval '24 hours' AND r.status IN ('error','denied')) AS errors, + (SELECT count(*)::int FROM mcp_plans p JOIN mcp_connections c ON c.id=p.connection_id WHERE ($1::boolean OR c.owner_id=$2) AND p.state='awaiting-approval' AND p.expires_at>now()) AS approvals, + (SELECT count(*)::int FROM mcp_operations o JOIN mcp_connections c ON c.id=o.connection_id WHERE ($1::boolean OR c.owner_id=$2) AND o.state IN ('running','pending')) AS running`, [admin, actor.id]) + return { ...summary, pollStorageCapacity:await pollCapacityForPortal(actor.id,admin), enabled: (await getMcpSettings()).enabled, observedAt: new Date().toISOString() } + }) + for (const table of ["requests", "plans", "operations", "audit", "drift"] as const) { + app.get(`/mcp/${table}`, async request => { + const actor = actorOf(request.authUser), query = mcpPageQuerySchema.parse(request.query), admin = hasPermission(actor.permissions, "mm:mcp:admin") + const relation = "mcp_" + table, values: unknown[] = [admin, actor.id] + const owner = table === "drift" ? "EXISTS(SELECT 1 FROM mcp_operations o JOIN mcp_connections c ON c.id=o.connection_id WHERE o.id=r.operation_id AND c.owner_id=$2)" : "EXISTS(SELECT 1 FROM mcp_connections c WHERE c.id=r.connection_id AND c.owner_id=$2)" + const clauses = [`($1::boolean OR ${owner})`] + const bind = (value: unknown) => { values.push(value); return "$" + values.length } + if (query.operationId) { + if (table !== "operations") throw new McpError("INVALID_TARGET", "ID операции используется только в журнале операций", 400) + clauses.push(`r.id=${bind(query.operationId)}::uuid`) + } + if (query.connectionId && table !== "drift") clauses.push(`r.connection_id=${bind(query.connectionId)}::uuid`) + if (query.status && ["requests", "plans", "operations"].includes(table)) clauses.push(`r.${table === "requests" ? "status" : "state"}=${bind(query.status)}`) + if (query.tool && table === "requests") clauses.push(`r.tool=${bind(query.tool)}`) + if (query.userId && ["requests", "audit"].includes(table)) clauses.push(`r.actor_id=${bind(query.userId)}`) + if (query.serverId && table === "requests") clauses.push(`${bind(query.serverId)}::bigint=ANY(r.server_ids)`) + const dateField = table === "drift" ? "detected_at" : "created_at" + if (query.from) clauses.push(`r.${dateField}>=${bind(query.from)}::timestamptz`) + if (query.to) clauses.push(`r.${dateField}<=${bind(query.to)}::timestamptz`) + if (query.cursor) { + const valid = table === "audit" ? /^[1-9]\d{0,18}$/.test(query.cursor) : z.string().uuid().safeParse(query.cursor).success + if (!valid) throw new McpError("INVALID_CURSOR", "Некорректный курсор страницы", 400) + clauses.push(`(r.${dateField},r.id)<(SELECT ${dateField},id FROM ${relation} WHERE id=${bind(query.cursor)}::${table === "audit" ? "bigint" : "uuid"})`) + } + const count = await pool.query<{ total: number }>(`SELECT count(*)::int AS total FROM ${relation} r WHERE ${clauses.join(" AND ")}`, values) + const { rows } = await pool.query(`SELECT ${table === "requests" ? MCP_REQUEST_MONITOR_PROJECTION : "r.*"} FROM ${relation} r WHERE ${clauses.join(" AND ")} ORDER BY r.${dateField} DESC,r.id DESC LIMIT ${bind(query.limit + 1)}`, values) + const entries = rows.slice(0, query.limit) + const steps = table === "operations" && query.operationId && entries.length > 0 + ? (await pool.query(`SELECT step_index,server_id,state,finished_at FROM mcp_operation_steps + WHERE operation_id=$1 ORDER BY step_index LIMIT 101`, [query.operationId])).rows + : undefined + const receipts = table === "operations" ? await humanArtifactReceipts(entries, actor, { query: (sql, values) => pool.query(sql, values), authorize: (id, actor, kind) => kind === "ipsec-existing-certificate-bundle" ? existingCertificateArtifact.metadata(id, actor, checkClaims) : kind === "ipsec-user-bundle" ? humanIpsecUserBundleMetadata(id, actor, checkClaims) : kind === "wireguard-export" ? humanWireguardExportMetadata(id, actor, checkClaims) : kind === "database-backup" ? humanDatabaseBackupMetadata(id, actor, checkClaims) : humanBackupDownloadMetadata(id, actor, checkClaims), checkClaims }) : new Map() + return { items: table === "plans" ? await Promise.all(entries.map(row => publicPlan(row as Awaited>))) : entries.map(row => table === "operations" ? { ...redact(row) as Record, ...(steps && { steps: steps.slice(0, 100), stepsTruncated: steps.length > 100 }), artifactReceipts: (receipts.get(row.id) ?? []).map((receipt: { artifactId: string }) => ({ ...receipt, downloadPath: `/api/mcp/artifacts/${receipt.artifactId}` })) } : redact(row)), total: count.rows[0].total, nextCursor: rows.length > query.limit ? String(entries.at(-1)?.id) : null } + }) + } + app.get("/mcp/plans/:id", async request => { + const plan = await loadPlan(idOf(request.params)); assertConnectionVisible(await getConnection(plan.connection_id), actorOf(request.authUser)); return publicPlan(plan) + }) + app.post("/mcp/plans/:id/decision", async request => { await approvePlan(idOf(request.params), actorOf(request.authUser), z.object({ approve: z.boolean() }).strict().parse(request.body).approve); return { ok: true } }) + for (const action of ["recover", "reconcile"] as const) app.post(`/mcp/operations/:id/${action}`, async request => { + const id = idOf(request.params), actor = actorOf(request.authUser) + const { rows: [operation] } = await pool.query("SELECT o.connection_id,p.operation_id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1", [id]) + if (!operation) throw new McpError("NOT_FOUND", "Операция не найдена", 404) + const globalReconcile = action === "reconcile" && findMcpOperation(operation.operation_id)?.scope === "global" + const principal = await ownerPrincipal(operation.connection_id, actor, action === "recover" ? "operations.recover" : globalReconcile ? "operations.global.reconcile" : "operations.reconcile") + return ownerDelegate(async () => invokeMcpTool(principal, action === "recover" ? "mm_operation_recovery_plan" : globalReconcile ? "mm_global_operation_reconcile" : "mm_operation_reconcile", { operationId: id })) + }) + app.post("/mcp/plans/:id/apply", async request => { + const id = idOf(request.params), actor = actorOf(request.authUser) + let plan: { id: string; connection_id: string; operation_id: string } + try { plan = await loadPlan(id) } + catch (error) { + if (!(error instanceof McpError) || error.code !== "NOT_FOUND") throw error + const tombstone = await findOwnerPlanReplayTombstone(pool, actor.id, id) + if (!tombstone) throw error + const principal = await ownerPrincipal(tombstone.connection_id, actor, tombstone.action_id) + return ownerDelegate(async () => applyPlan(principal, tombstone.plan_id, `web:${tombstone.plan_id}`)) + } + const principal = await ownerPrincipal(plan.connection_id, actor, plan.operation_id) + await auditOwnerRequest(actor.id, "plan.web.apply", plan.connection_id, { planId: plan.id }) + return ownerDelegate(async () => applyPlan(principal, plan.id, `web:${plan.id}`)) + }) + app.get("/mcp/protections", async request => { + const actor = actorOf(request.authUser) + return { protections: (await pool.query(`SELECT p.* FROM mcp_protections p WHERE $1::boolean OR p.created_by=$2 OR EXISTS( + SELECT 1 FROM mcp_connections c WHERE c.owner_id=$2 AND c.policy->'scope'->'serverIds' @> jsonb_build_array((p.object_ref->>'serverId')::bigint)) ORDER BY p.created_at DESC LIMIT 1000`, [hasPermission(actor.permissions, "mm:mcp:admin"), actor.id])).rows } + }) + app.post("/mcp/protections", async request => { + const input = mcpProtectionCreateRequestSchema.parse(request.body), actor = actorOf(request.authUser), id = randomUUID() + await connectionWrite(async client => { await client.query("INSERT INTO mcp_protections(id,object_ref,reason,created_by) VALUES($1,$2::jsonb,$3,$4)", [id, JSON.stringify(input.object), input.reason, actor.id]); await audit(client, actor.id, "protection.create", null, input) }) + return { id } + }) + app.delete("/mcp/protections/:id", async request => { const id = idOf(request.params); await connectionWrite(async client => { await client.query("DELETE FROM mcp_protections WHERE id=$1", [id]); await audit(client, actorOf(request.authUser).id, "protection.delete", null, { id }) }); return { ok: true } }) + app.post("/mcp/connections/:id/grants", async request => { + const connectionId = idOf(request.params) + const input = mcpTemporaryGrantSchema.parse(request.body), actor = actorOf(request.authUser) + const credentialId = randomUUID(), token = randomToken("bearer") + await connectionWrite(async client => { + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [connectionId]) + const connection = await getConnection(connectionId, client) + assertConnectionVisible(connection, actor) + if (Date.parse(input.expiresAt) <= Date.now() || connection.policy.status !== "active" || connection.generation !== await accessGeneration()) throw new McpError("GRANT_INACTIVE", "Подключение или временный доступ недействительны", 400) + if (input.scope.operationIds.some(id => !connection.policy.scope.operationIds.includes(id)) || input.scope.serverIds.some(id => !connection.policy.scope.serverIds.includes(id)) || input.scope.allowGlobal && !connection.policy.scope.allowGlobal || Date.parse(input.expiresAt) > Math.min(Date.parse(connection.policy.expiresAt), connection.owner_valid_until.getTime())) throw new McpError("PERMISSION_DENIED", "Временный доступ не может расширять основной доступ и срок подтверждённых прав владельца") + await client.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, tokenDigest(token), input.expiresAt]) + await client.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [randomUUID(), connectionId, JSON.stringify({ ...input, credentialId })]) + await audit(client, actor.id, "grant.create", connectionId, input) + }) + return { token, expiresAt: input.expiresAt } + }) + app.get("/mcp/connections/:id/grants", async request => { + const connection = await getConnection(idOf(request.params)); assertConnectionVisible(connection, actorOf(request.authUser)) + return { grants: (await pool.query("SELECT id,payload-'credentialId' AS grant,revoked_at FROM mcp_grants WHERE connection_id=$1 ORDER BY payload->>'expiresAt' DESC LIMIT 200", [connection.id])).rows } + }) + app.delete("/mcp/grants/:id", async request => { + const id = idOf(request.params), actor = actorOf(request.authUser) + await connectionWrite(async client => { + const { rows: [parent] } = await client.query("SELECT connection_id FROM mcp_grants WHERE id=$1", [id]) + if (!parent) throw new McpError("NOT_FOUND", "Допуск не найден", 404) + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [parent.connection_id]) + const grant = (await client.query("SELECT payload FROM mcp_grants WHERE id=$1 AND connection_id=$2", [id, parent.connection_id])).rows[0] + if (!grant) throw new McpError("NOT_FOUND", "Допуск не найден", 404) + assertConnectionVisible(await getConnection(parent.connection_id, client), actor) + await client.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1 AND connection_id=$2", [grant.payload.credentialId, parent.connection_id]) + const updated = await client.query("UPDATE mcp_grants SET revoked_at=now() WHERE id=$1 AND connection_id=$2", [id, parent.connection_id]) + if (!updated.rowCount) throw new McpError("NOT_FOUND", "Допуск не найден", 404) + await audit(client, actor.id, "grant.revoke", parent.connection_id, { grantId: id }) + }) + return { ok: true } + }) + app.post("/mcp/connections/:id/check", async request => { + const connection = await getConnection(idOf(request.params)); assertConnectionVisible(connection, actorOf(request.authUser)) + const actions = listActions(), checks = { + enabled: (await getMcpSettings()).enabled, + generation: connection.generation === await accessGeneration(), active: connection.policy.status === "active", + expires: Date.parse(connection.policy.expiresAt) > Date.now(), + ownerPermissionsFresh: connection.owner_valid_until.getTime() > Date.now(), + operations: connection.policy.scope.operationIds.every(id => actions.some(action => action.operation.id === id)), + permissions: actions.filter(action => connection.policy.scope.operationIds.includes(action.operation.id)).every(action => action.operation.requiredPermissions.every(permission => hasPermission(connection.owner_permissions, permission) && hasPermission(connection.policy.permissions, permission))), + credentials: (await pool.query("SELECT 1 FROM mcp_credentials WHERE connection_id=$1 AND revoked_at IS NULL AND expires_at>now() AND kind IN ('bearer','access') LIMIT 1", [connection.id])).rows.length > 0, + } + const protocolProbe: ProtocolProbe = Object.values(checks).every(Boolean) ? await checkMcpProtocol(connection.id, actorOf(request.authUser)) : { status: "skipped", reason: "PRECHECK_FAILED", elapsedMs: 0, credentialType: "temporary-no-operations", capabilitiesRead: "skipped", clientCredentialsVerified: false } + const result = { ...checks, protocolHandshake: protocolProbe.status === "passed" } + await auditOwnerRequest(actorOf(request.authUser).id, "connection.check", connection.id, { checks: result, protocolProbe }) + return { ok: Object.values(result).every(Boolean), checks: result, protocolProbe, checkedAt: new Date().toISOString(), transport: "streamable-http", endpointPath: "/mcp" } + }) + app.get("/mcp/routing-profiles", async request => { + const actor = actorOf(request.authUser) + const { rows } = await pool.query("SELECT p.id,p.connection_id,p.version,p.config,p.state,p.enabled,p.grant_expires_at,p.updated_at FROM mcp_routing_profiles p JOIN mcp_connections c ON c.id=p.connection_id WHERE ($1::boolean OR c.owner_id=$2) ORDER BY p.updated_at DESC LIMIT 200", [hasPermission(actor.permissions, "mm:mcp:admin"), actor.id]) + return { profiles: rows.map(row => ({ ...row, state: publicRoutingControllerState(row.state) })) } + }) + app.post("/mcp/routing-profiles/:id/pause", async request => { + const id = idOf(request.params), actor = actorOf(request.authUser) + await requestAccountingWrite(async client => { + const profile = (await client.query("SELECT connection_id FROM mcp_routing_profiles WHERE id=$1 FOR UPDATE", [id])).rows[0] + if (!profile) throw new McpError("NOT_FOUND", "Профиль не найден", 404) + assertConnectionVisible(await getConnection(profile.connection_id, client), actor) + await client.query("UPDATE mcp_routing_profiles SET enabled=false,state=state || '{\"pausedReason\":\"Остановлен администратором\"}'::jsonb WHERE id=$1", [id]) + await audit(client, actor.id, "controller.pause", profile.connection_id, { profileId: id }) + }) + return { ok: true } + }) + app.post("/mcp/routing-profiles/:id/check", async request => { + const id = idOf(request.params), actor = actorOf(request.authUser) + const { candidateId, proofPurpose } = z.object({ candidateId: z.string().min(1).max(100), proofPurpose: z.enum(["community", "control-path"]).default("community") }).strict().parse(request.body) + const { rows: [row] } = await pool.query("SELECT connection_id,config FROM mcp_routing_profiles WHERE id=$1", [id]) + if (!row) throw new McpError("NOT_FOUND", "Профиль не найден", 404) + const principal = await ownerPrincipal(row.connection_id, actor, "routing.path.verify") + return ownerDelegate(async () => invokeMcpTool(principal, "mm_route_path_verify", { profile: routeAiProfileSchema.parse(row.config), candidateId, proofPurpose })) + }) + app.post("/mcp/routing-profiles/plan", async request => { + const input = z.object({ connectionId: uuid, profileId: uuid.optional(), profile: routeAiProfileSchema, enabled: z.boolean(), expiresAt: z.string().datetime({ offset: true }) }).strict().parse(request.body) + const actor = actorOf(request.authUser), principal = await ownerPrincipal(input.connectionId, actor, "routing.profiles.save") + if (input.enabled && !hasPermission(actor.permissions, "mm:filters:write")) throw new McpError("PERMISSION_DENIED", "Контроллер Community требует право изменения фильтров") + const { connectionId: _connection, ...payload } = input + await auditOwnerRequest(actor.id, "controller.web.plan", input.connectionId, { profileId: input.profileId ?? null }) + return ownerDelegate(async () => invokeMcpTool(principal, "mm_route_profile_save_plan", payload)) + }) + app.post("/mcp/secrets", async request => { + const input = z.object({ purpose: z.string().min(1).max(512), serverId: z.number().int().positive().optional(), value: z.string().min(1).max(1000000) }).strict().parse(request.body), id = randomUUID(), actor = actorOf(request.authUser) + await connectionWrite(async client => { await client.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id, input.purpose, input.serverId ?? null, await encryptSecret(input.value), actor.id]); await audit(client, actor.id, "secret.create", null, { secretRef: id, purpose: input.purpose, serverId: input.serverId }) }) + return { secretRef: id } + }) + app.get("/mcp/artifacts/:id", async (request, reply) => { + // The portal owner is authenticated by onRequest. A sealed artifact's + // delegated credential can expire or be revoked without ending that login. + const readForOwner = async (read: () => Promise): Promise => { + try { return await read() } + catch (error) { + if (error instanceof McpError && error.code === "UNAUTHORIZED" && error.statusCode === 401) throw new McpError("PERMISSION_DENIED", "Доступ к файлу отозван", 403) + throw error + } + } + const { rows: [artifact] } = await pool.query("SELECT id,connection_id,filename,media_type,CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope FROM mcp_artifacts WHERE id=$1 AND expires_at>now() AND octet_length(filename)<=1024 AND octet_length(media_type)<=200", [idOf(request.params)]) + if (!artifact) throw new McpError("NOT_FOUND", "Файл отсутствует или срок истёк", 404) + if(artifact.access_scope?.kind==="backup-download" || artifact.access_scope?.kind==="database-backup" || artifact.access_scope?.kind==="wireguard-export" || artifact.access_scope?.kind==="ipsec-user-bundle" || artifact.access_scope?.kind==="ipsec-existing-certificate-bundle"){ + const cancellation=new AbortController(),abort=()=>{if(!reply.raw.writableFinished)cancellation.abort()} + request.raw.once("aborted",abort);reply.raw.once("close",abort) + try { + const read = artifact.access_scope.kind === "ipsec-existing-certificate-bundle" ? existingCertificateArtifact.read : artifact.access_scope.kind === "ipsec-user-bundle" ? readHumanIpsecUserBundleArtifact : artifact.access_scope.kind === "wireguard-export" ? readHumanWireguardExportArtifact : artifact.access_scope.kind === "database-backup" ? readHumanDatabaseBackupArtifact : readHumanBackupDownloadArtifact + const file=await readForOwner(() => read(artifact.id,actorOf(request.authUser),checkClaims,cancellation.signal)) + return reply.type(file.mediaType).header("Cache-Control","private, no-store").header("Content-Disposition",`attachment; filename="${file.filename}"`).send(file.content) + } finally {request.raw.off("aborted",abort);reply.raw.off("close",abort)} + } + if(artifact.access_scope?.kind==="netflow-host-files"){ + const file=await readForOwner(() => readHumanHostFilesArtifact(artifact.id,actorOf(request.authUser))) + return reply.type(file.mediaType).header("Content-Disposition",`attachment; filename="${file.filename.replace(/[^a-zA-Z0-9._-]/g,"_")}"`).send(file.content) + } + const connection = await getConnection(artifact.connection_id); assertConnectionVisible(connection, actorOf(request.authUser)) + if (connection.generation !== await accessGeneration() || connection.policy.status !== "active") throw new McpError("PERMISSION_DENIED", "Доступ к файлу отозван") + const metadata=await loadLogExportArtifactMetadata(artifact.id,connection.id) + if(!hasPermission(actorOf(request.authUser).permissions,"mm:servers:read") || !hasPermission(connection.policy.permissions,"mm:servers:read") || metadata.access_scope.policyVersion!==connection.policy.version || metadata.access_scope.generation!==await accessGeneration() || metadata.access_scope.serverIds.some(id=>!connection.policy.scope.serverIds.includes(id))) throw new McpError("PERMISSION_DENIED","Доступ к области выгрузки отозван") + const payload=(await pool.query("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND connection_id=$2 AND access_scope->>'proof'=$3 AND expires_at>now() AND octet_length(ciphertext)<=12582912",[artifact.id,artifact.connection_id,metadata.access_scope.proof])).rows[0] + if(!payload)throw new McpError("NOT_FOUND","Файл отсутствует или срок истёк",404) + if(createHash("sha256").update(payload.ciphertext).digest("hex")!==metadata.access_scope.encryptedSha256)throw new McpError("ARTIFACT_INVALID","Зашифрованная выгрузка изменилась",409) + const content=await decryptSecret(payload.ciphertext) + if(Buffer.byteLength(content)!==metadata.access_scope.bytes || createHash("sha256").update(content).digest("hex")!==metadata.access_scope.sha256)throw new McpError("ARTIFACT_INVALID","Размер или SHA-256 выгрузки не совпал",409) + await checkClaims(actorOf(request.authUser)) + const fresh=await getConnection(connection.id) + if(fresh.generation!==await accessGeneration() || fresh.policy.status!=="active" || fresh.policy.version!==metadata.access_scope.policyVersion || !hasPermission(fresh.policy.permissions,"mm:servers:read") || metadata.access_scope.serverIds.some(id=>!fresh.policy.scope.serverIds.includes(id)))throw new McpError("PERMISSION_DENIED","Доступ к файлу отозван") + return reply.type(artifact.media_type).header("Cache-Control", "private, no-store") + .header("Content-Disposition", `attachment; filename="${artifact.filename.replace(/[^a-zA-Z0-9._-]/g, "_")}"`).send(content) + }) +} + +} +export default createMcpRoutes() diff --git a/backend/src/routes/network.ts b/backend/src/routes/network.ts index a4b9de52..aff23a64 100644 --- a/backend/src/routes/network.ts +++ b/backend/src/routes/network.ts @@ -1,67 +1,25 @@ -import { promises as dns } from "node:dns" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" -import { z } from "zod" +import { resolveHostsSchema, resolveNetworkHosts } from "../services/network-host-resolution.js" -const IPV4 = - /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ - -function isIpv4(s: string): boolean { - return IPV4.test(s.trim()) -} - -/** Кэш DNS на процесс (GRE outer меняется редко). */ -const resolveCache = new Map() -const TTL_MS = 5 * 60 * 1000 - -async function resolveHostToIpv4(hostname: string): Promise { - const key = hostname.trim().toLowerCase() - if (!key || isIpv4(key)) return isIpv4(key) ? key.trim() : null - - const hit = resolveCache.get(key) - if (hit && hit.expires > Date.now()) return hit.ip - - try { - const { address } = await dns.lookup(key, { family: 4 }) - const ip = isIpv4(address) ? address : null - resolveCache.set(key, { ip, expires: Date.now() + TTL_MS }) - return ip - } catch { - resolveCache.set(key, { ip: null, expires: Date.now() + TTL_MS }) - return null +export function createNetworkRoutes(resolve = resolveNetworkHosts): FastifyPluginAsyncZod { + return async app => { + app.post("/network/resolve-hosts", async (req, reply) => { + const parsed = resolveHostsSchema.safeParse(req.body) + if (!parsed.success) return reply.status(400).send({ error: "Invalid body", details: parsed.error.flatten() }) + const abort = new AbortController() + const onAborted = () => abort.abort() + const onClose = () => { if (!reply.raw.writableEnded) abort.abort() } + req.raw.once("aborted", onAborted); reply.raw.once("close", onClose) + if (req.raw.aborted || reply.raw.destroyed) abort.abort() + try { + const resolved = await resolve(parsed.data, { signal: abort.signal }) + if (abort.signal.aborted) return + return reply.send({ results: resolved.results }) + } catch { + if (abort.signal.aborted) return + return reply.status(503).send({ error: "Не удалось подтвердить резолв" }) + } finally { req.raw.off("aborted", onAborted); reply.raw.off("close", onClose) } + }) } } - -const ResolveHostsBodySchema = z.object({ - hosts: z.array(z.string().max(253)).max(32), -}) - -const networkRoutes: FastifyPluginAsyncZod = async (app) => { - /** - * Резолв FQDN из GRE outer (remote-address / local-address) в IPv4 для сопоставления с WAN в каталоге. - */ - app.post("/network/resolve-hosts", async (req, reply) => { - const parsed = ResolveHostsBodySchema.safeParse(req.body) - if (!parsed.success) { - return reply.status(400).send({ error: "Invalid body", details: parsed.error.flatten() }) - } - - const seen = new Set() - const unique: string[] = [] - for (const h of parsed.data.hosts) { - const k = h.trim().toLowerCase() - if (!k || seen.has(k)) continue - seen.add(k) - unique.push(h.trim()) - } - - const results: Record = {} - for (const host of unique) { - const key = host.toLowerCase() - results[key] = await resolveHostToIpv4(host) - } - - return reply.send({ results }) - }) -} - -export default networkRoutes +export default createNetworkRoutes() diff --git a/backend/src/routes/ospf-catalog.test.ts b/backend/src/routes/ospf-catalog.test.ts index 5b79d83a..273099fe 100644 --- a/backend/src/routes/ospf-catalog.test.ts +++ b/backend/src/routes/ospf-catalog.test.ts @@ -2,6 +2,7 @@ import assert from "node:assert/strict" import Fastify from "fastify" import { validatorCompiler, serializerCompiler } from "@fastify/type-provider-zod" import { createOspfRoutes, parseBfdSessions, parseBfdCatalogSessions, parseCatalogNeighbors, parseCatalogInterfaces, parseCatalogInstances, parseCatalogRoutes } from "./ospf.js" +import { parseInterfaces } from "../services/ospf-read-model.js" import { emptyOspfCatalog, pollOspfCatalog, readOspfCatalogRaw, type OspfCatalogCollections } from "../services/ospf-catalog-poll.js" import { servers } from "../db/schema.js" const devices = [{ id: 1, name: "Первый" }, { id: 2, name: "Второй" }] @@ -64,6 +65,15 @@ const zeroIface = parseCatalogInterfaces(bfdServer, [{ ...rawIface, cost: "0", p assert.equal(zeroIface.cost, 0); assert.equal(zeroIface.priority, 0); assert.equal(zeroIface.helloInterval, 0); assert.equal(zeroIface.deadInterval, 1.5); assert.equal(zeroIface.useBfd, false) assert.equal(parseCatalogInterfaces(bfdServer, [{ ...rawIface, disabled: "junk", cost: "10x" }], [], [], new Map())[0].disabled, null) const rawInstance = { ".id": "*1", name: "default", disabled: "false", inactive: "false" } +const owners = [rawInstance, { ...rawInstance, ".id": "*2", name: "actual-owner" }] +const area = { ".id": "*A", name: "backbone", instance: "*2", disabled: "false", inactive: "false", type: "default" } +const rawMultiInstance = { ...rawIface, "instance-id": "0" } +assert.equal(parseInterfaces(bfdServer, [rawMultiInstance], [area], owners, new Map())[0].instance, "actual-owner") +const multiInstance = parseCatalogInterfaces(bfdServer, [{ ...rawMultiInstance, area: "*A" }], [area], owners, new Map())[0] +assert.equal(multiInstance.instance, "actual-owner") +assert.equal(multiInstance.instanceId, 0) +assert.equal(parseCatalogInterfaces(bfdServer, [{ ...rawMultiInstance, "instance-id": "256" }], [area], owners, new Map())[0].instanceId, null) +assert.equal(parseCatalogInterfaces(bfdServer, [rawMultiInstance], [area, { ...area, ".id": "*B", instance: "default" }], owners, new Map())[0].instance, null) const missingInstance = parseCatalogInstances(bfdServer, [rawInstance])[0] assert.equal(missingInstance.version, null); assert.equal(missingInstance.routerId, null) assert.equal(parseCatalogInstances(bfdServer, [{ ...rawInstance, version: "3", "router-id": "main" }])[0].version, 3) diff --git a/backend/src/routes/ospf-optimizer.test.ts b/backend/src/routes/ospf-optimizer.test.ts index a27918c0..8cb346c1 100644 --- a/backend/src/routes/ospf-optimizer.test.ts +++ b/backend/src/routes/ospf-optimizer.test.ts @@ -1,9 +1,10 @@ import assert from "node:assert/strict" import Fastify from "fastify" import {validatorCompiler,serializerCompiler} from "@fastify/type-provider-zod" -import {createOspfRoutes} from "./ospf.js" +import {createOspfRoutes,readOspfOptimizationPreview} from "./ospf.js" import type {servers} from "../db/schema.js" import {MikrotikError} from "../services/mikrotik.js" +import {OspfCostWriteError} from "../services/ospf-template-cost-write.js" const row={id:"*1",interface:"ether1",currentCost:10,optimalCost:20,pingMs:0,dlMbps:0,ulMbps:0,score:0} let plan:unknown=[row],writes=0,enabled=true,exists=true,failWrite=false let failAt:number|null=null,writeError:Error=new Error("Ответ записи потерян") @@ -16,6 +17,16 @@ await app.register(createOspfRoutes({ }),{prefix:"/api"}) const send=(preview=false,body:unknown={pingWeight:60})=>app.inject({method:"POST",url:"/api/servers/1/ospf/optimize"+(preview?"/preview":""),payload:body}) const preview=await send(true);assert.equal(preview.statusCode,200);assert.equal(preview.json().changedCount,1);assert.equal(preview.json().pingWeight,60);assert.equal(writes,0) +let previewBuilds=0 +const scopedServer={id:1,name:"Роутер",enabled:true} as typeof servers.$inferSelect +const scopedBuild=async()=>{previewBuilds++;return[row]} +const sharedPreview=await readOspfOptimizationPreview(scopedServer,60,{serverIds:[1],buildPlan:scopedBuild}) +assert.deepEqual(sharedPreview.interfaces,preview.json().interfaces) +for(const target of [{...scopedServer,enabled:false},scopedServer]){ + const before=previewBuilds + await assert.rejects(readOspfOptimizationPreview(target,60,{serverIds:target.enabled?[2]:[1],buildPlan:scopedBuild})) + assert.equal(previewBuilds,before) +} const unknown={value:null,source:"unknown",sourceId:null,measuredAt:null}; plan=[{...row,observed:{ping:{value:0,source:"speed-probe",sourceId:"p",measuredAt:"2020-01-01T00:00:00Z"},dl:unknown,ul:unknown}}]; const withObserved=await send(true);assert.equal(withObserved.json().interfaces[0].observed.ping.value,0);assert.equal(withObserved.json().changes[0].observed.ping.source,"speed-probe"); @@ -37,6 +48,10 @@ failAt=writes+2;writeError=new Error("Ответ второго PATCH потер const uncertain=await send();assert.equal(uncertain.json().state,"unknown");assert.equal(uncertain.json().confirmedWrites,1);assert.equal(uncertain.json().applied.length,1);assert.equal(writes,failAt) failAt=writes+1;writeError=new MikrotikError(401,"/routing/ospf/interface-template/*1","unauthorized") const rejected=await send();assert.equal(rejected.json().state,"rejected");assert.equal(rejected.json().confirmedWrites,0);assert.equal(rejected.json().needsReview,false);assert.equal(writes,failAt) +failAt=writes+1;writeError=new OspfCostWriteError("Cost изменился до записи",false) +const staleCost=await send();assert.equal(staleCost.json().state,"rejected");assert.equal(staleCost.json().confirmedWrites,0);assert.equal(staleCost.json().needsReview,false) +failAt=writes+1;writeError=new OspfCostWriteError("Read-back не подтверждён после записи",true) +const unconfirmedRead=await send();assert.equal(unconfirmedRead.json().state,"unknown");assert.equal(unconfirmedRead.json().confirmedWrites,0);assert.equal(unconfirmedRead.json().needsReview,true) failAt=null failWrite=false;plan=[];assert.equal((await send()).json().optimizedCount,0) await app.close();console.log("OSPF optimizer HTTP: invalid before write, readonly preview, disabled/missing/bad weight, network error and known empty; no DB/RouterOS") diff --git a/backend/src/routes/ospf.ts b/backend/src/routes/ospf.ts index 46aa5aa4..58b1cbb0 100644 --- a/backend/src/routes/ospf.ts +++ b/backend/src/routes/ospf.ts @@ -1,255 +1,56 @@ -import { and, desc, eq } from "drizzle-orm" +import { readOspfStateForServer, readBfdStateForServer, readOspfCatalogForServer } from "../services/ospf-state-read.js" +export { readOspfCatalogForServer } from "../services/ospf-state-read.js" +export { parseBfdSessions, parseBfdCatalogSessions, parseCatalogNeighbors, parseCatalogInterfaces, parseCatalogInstances, parseCatalogRoutes } from "../services/ospf-read-model.js" +import { and, asc, desc, eq, inArray } from "drizzle-orm" +import { decodeTrafficSampleFlags, trafficCounterContinuity } from "../db/traffic-flags.js" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { db } from "../db/index.js" import { serverSnapshots, servers, trafficSamples, uptimeSpeedProbes } from "../db/schema.js" -import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" +import { MikrotikClient } from "../services/mikrotik.js" import { ServerIdParamSchema, type ServerIdParams } from "../types/server.js" -import type { - RosOspfNeighbor, RosOspfArea, RosOspfInterfaceTemplate, RosOspfInstance, - RosBfdSession, RosIpRoute, - OspfNeighborRead, OspfInterfaceRead, OspfInstanceRead, OspfRouteRead, BfdSessionRead, -} from "../types/server.js" -import { parseOspfGateway, parseOspfRouteType, parseOspfCatalogRouteType } from "../services/ospf-route-parse.js" import { z } from "zod" import {resolveOptimizerRouterOwners} from "../services/ospf-optimizer-identity.js" import {readOspfOptimizerObserved,readOspfOptimizerModelSpeeds} from "../services/ospf-optimizer-metrics.js" import type {OspfOptimizerObserved} from "@mmapp/contracts" -import {readEditableOspfTemplates,validateOspfOptimizationPlan} from "../services/ospf-optimizer-plan.js" -import { observedInteger, observedDurationMs, observedBoolean } from "../services/ospf-observed-values.js" +import {readEditableOspfTemplates} from "../services/ospf-optimizer-plan.js" +import { prepareLegacyOspfOptimization,applyLegacyOspfOptimization,LegacyOspfApplyError } from "../services/ospf-legacy-optimization.js" import { pollOspfCatalog, readOspfCatalogRaw, type OspfCatalogCollections } from "../services/ospf-catalog-poll.js" import { pollLegacyOspf } from "../services/ospf-legacy-poll.js" type ServerRow = typeof servers.$inferSelect -type TrafficSampleRow = typeof trafficSamples.$inferSelect -type SpeedProbeRow = typeof uptimeSpeedProbes.$inferSelect +type TrafficSampleRow = Pick +type SpeedProbeRow = Pick const OspfOptimizeBodySchema = z.object({ pingWeight: z.number().int().min(0).max(100).default(60), }) -// ── helpers ─────────────────────────────────────────────────────────────────── - -/** Parse RouterOS duration string like "1h30m17s", "40s", "10m" → seconds */ -function parseDuration(s: string | undefined): number { - if (!s) return 0 - let total = 0 - for (const m of s.matchAll(/(\d+)(w|d|h|m(?!s)|s)/g)) { - const n = parseInt(m[1]) - switch (m[2]) { - case "w": total += n * 604800; break - case "d": total += n * 86400; break - case "h": total += n * 3600; break - case "m": total += n * 60; break - case "s": total += n; break - } +export interface OspfPreviewReadOptions { + serverIds?:readonly number[];beforeRequest?:(server:ServerRow)=>Promise;deadlineAt?:number;signal?:AbortSignal; + onNativeRead?:(server:ServerRow,path:string,body:Record|null,value:unknown)=>Promise; + onSourceRead?:(name:string,value:unknown)=>Promise; + beforeSourceRead?:()=>Promise; +} +function optimizationClient(server:ServerRow,options:OspfPreviewReadOptions) { + const client=MikrotikClient.fromServer(server) + if(!options.beforeRequest&&!options.signal&&!options.deadlineAt&&!options.onNativeRead)return client + const failure=new AbortController(),signal=AbortSignal.any([failure.signal,...(options.signal?[options.signal]:[])]) + const get=client.get.bind(client),post=client.post.bind(client) + const authorize=async()=>{signal.throwIfAborted();await options.beforeRequest?.(server);signal.throwIfAborted()} + const timeout=(ms:number)=>{ + const remaining=options.deadlineAt===undefined?ms:options.deadlineAt-Date.now() + if(remaining<=0)throw new Error("Истёк срок чтения OSPF") + return Math.max(1,Math.min(ms,remaining)) } - return total -} - -/** Build area-name → area-id lookup from a list of raw areas */ -function buildAreaMap(areas: RosOspfArea[]): Map { - const map = new Map() - for (const a of areas) { - map.set(a.name, a["area-id"] ?? "0.0.0.0") - } - return map -} - -/** Parse "200ms" | "1s" | "500ms" → milliseconds */ -function parseMs(s: string | undefined): number { - if (!s) return 0 - const ms = s.match(/^(\d+)ms$/) - if (ms) return parseInt(ms[1]) - const sec = s.match(/^(\d+)s$/) - if (sec) return parseInt(sec[1]) * 1000 - return parseInt(s) || 0 -} - -/** Normalize BFD state: "up" → "Up", "down" → "Down", etc. */ -function normalizeBfdState(state: string | undefined): string { - const map: Record = { - up: "Up", down: "Down", init: "Init", admindown: "AdminDown", - } - return map[(state ?? "").toLowerCase()] ?? (state ?? "Down") -} - -/** Extract IP and interface from RouterOS address format "10.0.0.1%eth0" */ -function parseAddrIface(addr: string): { ip: string; iface: string } { - const [ip, iface = ""] = addr.split("%") - return { ip, iface } -} - -/** Fetch all OSPF + BFD data for one server */ -async function fetchServerOspf(server: ServerRow) { - return readOspfCatalogRaw(MikrotikClient.fromServer(server)) -} - -// ── BFD parser ──────────────────────────────────────────────────────────────── - -export function parseBfdSessions(server: ServerRow, raw: RosBfdSession[], strictState = false): BfdSessionRead[] { - return raw.map((s, idx) => { - const local = parseAddrIface(s["local-address"]) - const remote = parseAddrIface(s["remote-address"]) - // Prefer interface from local-address; fallback to remote-address suffix - const iface = local.iface || remote.iface - - return { - id: s[".id"] ?? String(idx), - serverId: server.id, - serverName: server.name || server.host, - serverSite: server.site, - serverCountry: server.country, - localAddr: local.ip, - remoteAddr: remote.ip, - interface: iface, - state: strictState && !s["state"]?.trim() ? "Unknown" : normalizeBfdState(s["state"]), - uptime: s["uptime"] ?? null, - multihop: s["multihop"] === "true", - multiplier: parseInt(s["multiplier"] ?? "3") || 3, - txInterval: parseMs(s["actual-tx-interval"] ?? s["desired-tx-interval"]), - rxInterval: parseMs(s["required-min-rx"]), - holdTime: parseMs(s["hold-time"]), - packetsRx: parseInt(s["packets-rx"] ?? "0") || 0, - packetsTx: parseInt(s["packets-tx"] ?? "0") || 0, - stateChanges: parseInt(s["state-changes"] ?? "0") || 0, - } - }) -} - -export function parseBfdCatalogSessions(server: ServerRow, raw: RosBfdSession[]) { - return parseBfdSessions(server, raw, true).map((row, index) => { - const source = raw[index] - return { ...row, multihop: observedBoolean(source.multihop), multiplier: observedInteger(source.multiplier, 0, 255), txInterval: observedDurationMs(source["actual-tx-interval"]), rxInterval: observedDurationMs(source["required-min-rx"]), holdTime: observedDurationMs(source["hold-time"]), packetsRx: observedInteger(source["packets-rx"]), packetsTx: observedInteger(source["packets-tx"]), stateChanges: observedInteger(source["state-changes"]) } - }) -} -export function parseCatalogNeighbors(server: ServerRow, raw: RosOspfNeighbor[], areaMap: Map) { - return parseNeighbors(server, raw, areaMap).map((row, index) => ({ ...row, priority: observedInteger(raw[index].priority, 0, 255), stateChanges: observedInteger(raw[index]["state-changes"]) })) -} - -// ── parsers ─────────────────────────────────────────────────────────────────── - -function parseNeighbors( - server: ServerRow, - neighbors: RosOspfNeighbor[], - areaMap: Map, -): OspfNeighborRead[] { - return neighbors.map((n, idx) => ({ - id: n[".id"] ?? String(idx), - serverId: server.id, - serverName: server.name || server.host, - serverSite: server.site, - serverCountry: server.country, - address: n.address, - routerId: n["router-id"], - instance: n.instance, - area: n.area, - areaId: areaMap.get(n.area) ?? n.area, - interface: n.interface, - state: n.state, - uptime: n.adjacency ?? null, - stateChanges: parseInt(n["state-changes"] ?? "0") || 0, - priority: parseInt(n.priority ?? "1") || 1, - })) -} - -function parseInterfaces( - server: ServerRow, - templates: RosOspfInterfaceTemplate[], - areas: RosOspfArea[], - instances: RosOspfInstance[], - areaMap: Map, -): OspfInterfaceRead[] { - // Build instance-id → instance name map (for interface-template instance-id field) - const instanceIdMap = new Map() - instances.forEach((inst, i) => { instanceIdMap.set(String(i), inst.name) }) - - return templates.map((t, idx) => { - // Resolve interface name: may be a "*ID" reference (RouterOS internal ID) - const ifaceName = (t.interfaces ?? "").startsWith("*") - ? `(ref ${t.interfaces})` - : (t.interfaces ?? "—") - - return { - id: t[".id"] ?? String(idx), - serverId: server.id, - serverName: server.name || server.host, - serverSite: server.site, - serverCountry: server.country, - instance: instanceIdMap.get(t["instance-id"] ?? "") ?? t["instance-id"] ?? "", - area: t.area, - areaId: areaMap.get(t.area) ?? t.area, - interface: ifaceName, - cost: parseInt(t.cost ?? "10") || 10, - type: t.type ?? "broadcast", - disabled: t.disabled === "true", - inactive: t.inactive === "true", - priority: parseInt(t.priority ?? "1") || 1, - helloInterval: parseDuration(t["hello-interval"]), - deadInterval: parseDuration(t["dead-interval"]), - useBfd: t["use-bfd"] === "true", - } - }) -} - -export function parseCatalogInterfaces(server: ServerRow, raw: RosOspfInterfaceTemplate[], areas: RosOspfArea[], instances: RosOspfInstance[], areaMap: Map) { - return parseInterfaces(server, raw, areas, instances, areaMap).map((row, index) => { - const source = raw[index] - const helloMs = observedDurationMs(source["hello-interval"]), deadMs = observedDurationMs(source["dead-interval"]) - return { ...row, cost: observedInteger(source.cost, 0, 65535), priority: observedInteger(source.priority, 0, 255), type: source.type?.trim() || null, disabled: observedBoolean(source.disabled), inactive: observedBoolean(source.inactive), useBfd: observedBoolean(source["use-bfd"]), helloInterval: helloMs === null ? null : helloMs / 1000, deadInterval: deadMs === null ? null : deadMs / 1000 } - }) -} -export function parseCatalogInstances(server: ServerRow, raw: RosOspfInstance[]) { - return parseInstances(server, raw).map((row, index) => ({ ...row, version: ["2", "3"].includes(raw[index].version) ? Number(raw[index].version) : null, routerId: raw[index]["router-id"]?.trim() || null, disabled: observedBoolean(raw[index].disabled), inactive: observedBoolean(raw[index].inactive) })) -} -export function parseCatalogRoutes(server: ServerRow, raw: RosIpRoute[]) { - return raw.flatMap((source, index) => { - const rows = parseOspfRoutes(server, [{ ...source, ".id": source[".id"] ?? String(index) }]) - return rows.map(row => ({ ...row, type: parseOspfCatalogRouteType(source) ?? "Unknown", cost: observedInteger(source["ospf-metric"], 0, 4294967295) })) - }) -} - -function parseInstances( - server: ServerRow, - instances: RosOspfInstance[], -): OspfInstanceRead[] { - return instances.map((inst, idx) => ({ - id: inst[".id"] ?? String(idx), - serverId: server.id, - serverName: server.name || server.host, - serverSite: server.site, - serverCountry: server.country, - name: inst.name, - routerId: inst["router-id"], - version: parseInt(inst.version ?? "2") || 2, - disabled: inst.disabled === "true", - inactive: inst.inactive === "true", - redistribute: inst.redistribute ?? "", - })) -} - -function parseOspfRoutes(server: ServerRow, routes: RosIpRoute[]): OspfRouteRead[] { - const out: OspfRouteRead[] = [] - for (const [idx, r] of routes.entries()) { - const type = parseOspfRouteType(r) - if (!type) continue - const { nextHop, via } = parseOspfGateway(r) - const metric = parseInt(r["ospf-metric"] ?? r.distance ?? "0") || 0 - out.push({ - id: r[".id"] ?? String(idx), - serverId: server.id, - serverName: server.name || server.host, - serverSite: server.site, - destination: r["dst-address"] ?? "", - type, - cost: metric, - nextHop, - via, - area: r["ospf-area"] ?? "", - }) - } - return out + client.get=async(path:string,ms=10000,requestSignal?:AbortSignal)=>{try{await authorize();const value=await get(path,timeout(ms),requestSignal?AbortSignal.any([signal,requestSignal]):signal);await options.onNativeRead?.(server,path,null,value);await authorize();return value}catch(error){failure.abort();throw error}} + client.post=async(path:string,body:Record,ms=15000,requestSignal?:AbortSignal)=>{try{ + if(!path.endsWith("/get")&&!path.endsWith("/print"))throw new Error("В OSPF preview разрешены только read-команды") + await authorize();const value=await post(path,body,timeout(ms),requestSignal?AbortSignal.any([signal,requestSignal]):signal);await options.onNativeRead?.(server,path,body,value);await authorize();return value + }catch(error){failure.abort();throw error}} + return client } +async function fetchServerOspf(server: ServerRow,options:OspfPreviewReadOptions={}) { return readOspfCatalogRaw(optimizationClient(server,options)) } function calcRouteScore(pingMs: number, dlMbps: number, ulMbps: number, pingWeight: number) { const pingScore = Math.max(0, 100 - pingMs * 0.6) @@ -258,26 +59,36 @@ function calcRouteScore(pingMs: number, dlMbps: number, ulMbps: number, pingWeig return Math.round(w * pingScore + (1 - w) * speedScore) } -async function getLatestSnapshotLatencyMs(serverId: number): Promise { +async function getLatestSnapshotLatencyMs(serverId: number,options:OspfPreviewReadOptions={}): Promise { + await options.beforeSourceRead?.() const latest = await db - .select() + .select({latencyMs:serverSnapshots.latencyMs,polledAt:serverSnapshots.polledAt}) .from(serverSnapshots) .where(eq(serverSnapshots.serverId, serverId)) .orderBy(desc(serverSnapshots.polledAt)) .limit(1) + await options.onSourceRead?.(`latency:${serverId}`,latest) return latest.length > 0 ? Math.max(1, Math.round(latest[0].latencyMs ?? 100)) : 100 } -async function latestTrafficByInterface(serverId: number): Promise> { - const rows = await db - .select() +async function latestTrafficByInterface(serverId: number,options:OspfPreviewReadOptions={}): Promise> { + await options.beforeSourceRead?.() + const query = db + .selectDistinctOn([trafficSamples.interfaceName],{interfaceName:trafficSamples.interfaceName,rxBps:trafficSamples.rxBps,txBps:trafficSamples.txBps,sampledAt:trafficSamples.sampledAt,flags:trafficSamples.flags}) .from(trafficSamples) - .where(eq(trafficSamples.serverId, serverId)) + .where(and(eq(trafficSamples.serverId, serverId),eq(trafficSamples.peerPublicKey,""))) + .orderBy(asc(trafficSamples.interfaceName),desc(trafficSamples.sampledAt)) + const rows = await (options.onSourceRead ? query.limit(2001) : query) + if(options.onSourceRead && rows.length>2000)throw new Error("Превышен бюджет источников трафика legacy OSPF") const map = new Map() for (const row of rows) { + // A known counter is not proof of a measured delta. First/failed samples + // and individual WG peers must not masquerade as interface throughput. + if(!decodeTrafficSampleFlags(row.flags).countersKnown||trafficCounterContinuity(row.flags)!==true)continue const prev = map.get(row.interfaceName) if (!prev || row.sampledAt > prev.sampledAt) map.set(row.interfaceName, row) } + await options.onSourceRead?.(`traffic:${serverId}`,[...map.values()]) return map } @@ -290,50 +101,7 @@ function normalizeSpeedMbps(row: TrafficSampleRow | undefined): { dlMbps: number -function parsePingTimeMs(raw: string | undefined): number | null { - if (!raw) return null - const s = String(raw).trim().toLowerCase().replace(",", ".") - const us = s.match(/^(\d+(?:\.\d+)?)\s*us$/) - if (us) return Number.parseFloat(us[1]) / 1000 - const ms = s.match(/^(\d+(?:\.\d+)?)\s*ms$/) - if (ms) return Number.parseFloat(ms[1]) - const sec = s.match(/^(\d+(?:\.\d+)?)\s*s$/) - if (sec) return Number.parseFloat(sec[1]) * 1000 - const clock = s.match(/^(\d+):(\d+):(\d+(?:\.\d+)?)$/) - if (clock) { - const h = Number.parseFloat(clock[1]) - const m = Number.parseFloat(clock[2]) - const sc = Number.parseFloat(clock[3]) - return (h * 3600 + m * 60 + sc) * 1000 - } - return null -} - -function avgPingMsFromResults(rows: Array<{ time?: string; status?: string }>): number | null { - const ok = rows.filter((r) => r.time && String(r.status ?? "").toLowerCase() !== "timeout") - const vals = ok - .map((r) => parsePingTimeMs(r.time)) - .filter((v): v is number => v != null && Number.isFinite(v)) - if (!vals.length) return null - return Math.max(1, Math.round(vals.reduce((a, b) => a + b, 0) / vals.length)) -} - -async function measurePingByInterface( - client: MikrotikClient, - ifaceName: string, - targetAddr: string | undefined, - fallbackMs: number, -): Promise { - if (!targetAddr) return fallbackMs - try { - const rows = await client.ping(targetAddr, 2, ifaceName, { interval: "0.2s" }) - return avgPingMsFromResults(rows) ?? fallbackMs - } catch { - return fallbackMs - } -} - -type OspfRankedInterface = { +export type OspfRankedInterface = { observed?: OspfOptimizerObserved id: string interface: string @@ -345,20 +113,26 @@ type OspfRankedInterface = { optimalCost: number } -async function buildOspfOptimizationPlan(server: ServerRow, pingWeight: number): Promise { - const { ifaceTemplates, neighbors } = await fetchServerOspf(server) +export async function buildOspfOptimizationPlan(server: ServerRow, pingWeight: number, options:OspfPreviewReadOptions={}): Promise { + if(!server.enabled)throw new Error("Устройство OSPF отключено") + if(options.serverIds&&!options.serverIds.includes(server.id))throw new Error("Устройство OSPF вне выбранной области") + const { ifaceTemplates, neighbors } = await fetchServerOspf(server,options) const editable = readEditableOspfTemplates(ifaceTemplates) - const localLatencyMs = await getLatestSnapshotLatencyMs(server.id) - const latestTraffic = await latestTrafficByInterface(server.id) + if(!editable.length)return [] + const localLatencyMs = await getLatestSnapshotLatencyMs(server.id,options) + const latestTraffic = await latestTrafficByInterface(server.id,options) // 1) Сопоставляем remote OSPF router-id -> сервер из каталога (чтобы взять унифицированный ping как в карте /route-optimizer). const neededRouterIds = new Set(neighbors.map((n) => String(n["router-id"] ?? "")).filter((x) => x.length > 0)) async function buildRouterIdToServerMap(needed: Set): Promise> { - const enabledServers = await db.select().from(servers).where(eq(servers.enabled, true)) + await options.beforeSourceRead?.() + const query = db.select().from(servers).where(and(eq(servers.enabled, true),...(options.serverIds?[inArray(servers.id,[...options.serverIds])]:[]))).orderBy(asc(servers.id)) + const enabledServers = await (options.onSourceRead ? query.limit(1001) : query) + await options.onSourceRead?.("enabled-servers",enabledServers) return resolveOptimizerRouterOwners(enabledServers,needed,async id=>{ const target=enabledServers.find(server=>server.id===id)! - return MikrotikClient.fromServer(target).getOspfInstances() + return optimizationClient(target,options).getOspfInstances() }) } @@ -385,11 +159,13 @@ async function buildOspfOptimizationPlan(server: ServerRow, pingWeight: number): } // 3) Предгружаем uptimeSpeedProbes: оттуда берём и ping, и скорость (единый источник как у карты). - const allSourceProbes = (await db - .select() + await options.beforeSourceRead?.() + const probesQuery = db + .select({id:uptimeSpeedProbes.id,srcServerId:uptimeSpeedProbes.srcServerId,dstServerId:uptimeSpeedProbes.dstServerId,srcInterface:uptimeSpeedProbes.srcInterface,lastPingAt:uptimeSpeedProbes.lastPingAt,lastPingRttMs:uptimeSpeedProbes.lastPingRttMs,lastTxAvgMbps:uptimeSpeedProbes.lastTxAvgMbps,lastRxAvgMbps:uptimeSpeedProbes.lastRxAvgMbps,enabled:uptimeSpeedProbes.enabled}) .from(uptimeSpeedProbes) - .where(eq(uptimeSpeedProbes.srcServerId, server.id))) - .filter((r: SpeedProbeRow) => r.enabled !== false) + .where(and(eq(uptimeSpeedProbes.srcServerId, server.id),eq(uptimeSpeedProbes.enabled,true),...(options.serverIds?[inArray(uptimeSpeedProbes.dstServerId,[...options.serverIds])]:[]))) + const allSourceProbes = await (options.onSourceRead ? probesQuery.limit(2001) : probesQuery) + await options.onSourceRead?.(`speed-probes:${server.id}`,allSourceProbes) const speedProbesByDest = new Map() for (const p of allSourceProbes) { const arr = speedProbesByDest.get(p.dstServerId) ?? [] @@ -521,7 +297,7 @@ async function buildOspfOptimizationPlan(server: ServerRow, pingWeight: number): bestProbe?.lastPingRttMs != null ? Math.max(1, Math.round(bestProbe.lastPingRttMs)) : dstServerId != null - ? Math.min(995, Math.round(localLatencyMs + await getLatestSnapshotLatencyMs(dstServerId))) + ? Math.min(995, Math.round(localLatencyMs + await getLatestSnapshotLatencyMs(dstServerId,options))) : localLatencyMs const {dlMbps,ulMbps} = readOspfOptimizerModelSpeeds(bestProbe,speed) @@ -541,6 +317,15 @@ async function buildOspfOptimizationPlan(server: ServerRow, pingWeight: number): .map((row, idx) => ({ ...row, optimalCost: (idx + 1) * 10 })) } +export async function readOspfOptimizationPreview(server:ServerRow,pingWeight:number,options:OspfPreviewReadOptions&{buildPlan?:(server:ServerRow,weight:number)=>Promise}={}) { + if(!server.enabled)throw new Error("Устройство OSPF отключено") + if(options.serverIds&&!options.serverIds.includes(server.id))throw new Error("Устройство OSPF вне выбранной области") + z.number().int().min(0).max(100).parse(pingWeight) + const {interfaces:ranked}=await prepareLegacyOspfOptimization(server,pingWeight,()=>options.buildPlan?options.buildPlan(server,pingWeight):buildOspfOptimizationPlan(server,pingWeight,options)) + const changed=ranked.filter(row=>row.currentCost!==row.optimalCost) + return {serverId:server.id,serverName:server.name||server.host,pingWeight,interfacesTotal:ranked.length,changedCount:changed.length,unchangedCount:ranked.length-changed.length,interfaces:ranked,changes:changed} +} + // ── route plugin ────────────────────────────────────────────────────────────── interface OspfCatalogDependencies { @@ -553,11 +338,7 @@ interface OspfCatalogDependencies { } const catalogDefaults: OspfCatalogDependencies = { listEnabledServers: () => db.select().from(servers).where(eq(servers.enabled, true)), - readCatalog: async server => { - const { neighbors, areas, ifaceTemplates, instances, bfdSessions, ipRoutes } = await readOspfCatalogRaw(MikrotikClient.fromServer(server)) - const areaMap = buildAreaMap(areas) - return { neighbors: parseCatalogNeighbors(server, neighbors, areaMap), interfaces: parseCatalogInterfaces(server, ifaceTemplates, areas, instances, areaMap), instances: parseCatalogInstances(server, instances), bfdSessions: parseBfdCatalogSessions(server, bfdSessions), routes: parseCatalogRoutes(server, ipRoutes) } - }, + readCatalog: readOspfCatalogForServer, } export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalogDefaults): FastifyPluginAsyncZod { return async (app) => { @@ -569,8 +350,7 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog }) app.get("/ospf/neighbors", async (_req, reply) => { const result = await pollLegacyOspf(await dependencies.listEnabledServers(), async server => { - const { neighbors, areas } = await fetchServerOspf(server) - return parseNeighbors(server, neighbors, buildAreaMap(areas)) + return (await readOspfStateForServer(server)).legacy.neighbors }) if (result.failures.length) return reply.status(503).send(legacyFailure(result.failures)) return reply.send(result.values.flat()) @@ -578,8 +358,7 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog app.get("/ospf/interfaces", async (_req, reply) => { const result = await pollLegacyOspf(await dependencies.listEnabledServers(), async server => { - const { ifaceTemplates, areas, instances } = await fetchServerOspf(server) - return parseInterfaces(server, ifaceTemplates, areas, instances, buildAreaMap(areas)) + return (await readOspfStateForServer(server)).legacy.interfaces }) if (result.failures.length) return reply.status(503).send(legacyFailure(result.failures)) return reply.send(result.values.flat()) @@ -587,8 +366,7 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog app.get("/ospf/instances", async (_req, reply) => { const result = await pollLegacyOspf(await dependencies.listEnabledServers(), async server => { - const { instances } = await fetchServerOspf(server) - return parseInstances(server, instances) + return (await readOspfStateForServer(server)).legacy.instances }) if (result.failures.length) return reply.status(503).send(legacyFailure(result.failures)) return reply.send(result.values.flat()) @@ -596,15 +374,8 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog app.get("/ospf/all", async (_req, reply) => { const result = await pollLegacyOspf(await dependencies.listEnabledServers(), async server => { - const { neighbors, areas, ifaceTemplates, instances, bfdSessions, ipRoutes } = await fetchServerOspf(server) - const areaMap = buildAreaMap(areas) - return { - neighbors: parseNeighbors(server, neighbors, areaMap), - interfaces: parseInterfaces(server, ifaceTemplates, areas, instances, areaMap), - instances: parseInstances(server, instances), - bfdSessions: parseBfdSessions(server, bfdSessions), - routes: parseOspfRoutes(server, ipRoutes), - } + const { areas: _areas, ...collections } = (await readOspfStateForServer(server)).legacy + return collections }) if (result.failures.length) return reply.status(503).send(legacyFailure(result.failures)) return reply.send({ @@ -618,8 +389,7 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog app.get("/bfd/sessions", async (_req, reply) => { const result = await pollLegacyOspf(await dependencies.listEnabledServers(), async server => { - const raw = await MikrotikClient.fromServer(server).getBfdSessions() - return parseBfdSessions(server, raw) + return (await readBfdStateForServer(server)).legacy }) if (result.failures.length) return reply.status(503).send(legacyFailure(result.failures)) return reply.send(result.values.flat()) @@ -637,16 +407,7 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog if (server.enabled === false) return reply.status(409).send({ error: "Сервер выключен. Опрос OSPF не выполнялся" }) try { - const { neighbors, areas, ifaceTemplates, instances, bfdSessions, ipRoutes } = await readOspfCatalogRaw(MikrotikClient.fromServer(server)) - const areaMap = buildAreaMap(areas) - return reply.send({ - neighbors: parseNeighbors(server, neighbors, areaMap), - interfaces: parseInterfaces(server, ifaceTemplates, areas, instances, areaMap), - instances: parseInstances(server, instances), - bfdSessions: parseBfdSessions(server, bfdSessions), - routes: parseOspfRoutes(server, ipRoutes), - areas: areas.map(a => ({ name: a.name, areaId: a["area-id"] ?? "0.0.0.0", type: a.type, disabled: a.disabled === "true", inactive: a.inactive === "true", instance: a.instance })), - }) + return reply.send((await readOspfStateForServer(server)).legacy) } catch (err) { return reply.status(503).send({ error: String(err) }) } @@ -664,19 +425,7 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog if (!server.enabled) return reply.status(409).send({error:"Устройство OSPF отключено"}) try { - const pingWeight = req.body.pingWeight - const ranked = validateOspfOptimizationPlan(await (dependencies.buildOptimizationPlan ?? buildOspfOptimizationPlan)(server, pingWeight)) - const changed = ranked.filter((r) => r.currentCost !== r.optimalCost) - return reply.send({ - serverId: server.id, - serverName: server.name || server.host, - pingWeight, - interfacesTotal: ranked.length, - changedCount: changed.length, - unchangedCount: ranked.length - changed.length, - interfaces: ranked, - changes: changed, - }) + return reply.send(await readOspfOptimizationPreview(server,req.body.pingWeight,{buildPlan:dependencies.buildOptimizationPlan})) } catch (err) { return reply.status(500).send({ error: String(err) }) } @@ -693,42 +442,26 @@ export function createOspfRoutes(dependencies: OspfCatalogDependencies = catalog if (!server) return reply.status(404).send({ error: "Server not found" }) if (!server.enabled) return reply.status(409).send({error:"Устройство OSPF отключено"}) - const applied: Array<{ interface: string; from: number; to: number }> = [] - let pending: OspfRankedInterface | undefined try { const pingWeight = req.body.pingWeight - const ranked = validateOspfOptimizationPlan(await (dependencies.buildOptimizationPlan ?? buildOspfOptimizationPlan)(server, pingWeight)) - + const plan=await prepareLegacyOspfOptimization(server,pingWeight,()=>(dependencies.buildOptimizationPlan??buildOspfOptimizationPlan)(server,pingWeight)) const client=dependencies.setTemplateCost?null:MikrotikClient.fromServer(server) - const changed = ranked.filter((r) => r.currentCost !== r.optimalCost) - for (const item of changed) { - pending = item + const result=await applyLegacyOspfOptimization(plan,async item=>{ if(dependencies.setTemplateCost) await dependencies.setTemplateCost(server,item.id,item.optimalCost) - else await client!.setOspfInterfaceTemplateCost(item.id,item.optimalCost) - applied.push({ interface: item.interface, from: item.currentCost, to: item.optimalCost }) - pending = undefined - } - - return reply.send({ - serverId: server.id, - serverName: server.name || server.host, - pingWeight, - optimizedCount: changed.length, - applied, - interfaces: ranked, + else await client!.setOspfInterfaceTemplateCost(item.id,item.optimalCost,item.currentCost) }) + return reply.send(result) } catch (err) { // A lost acknowledgement does not prove that RouterOS rejected the write. - if (pending) { - const rejected = err instanceof MikrotikError && err.statusCode >= 400 && err.statusCode < 500 + if (err instanceof LegacyOspfApplyError) { return reply.status(502).send({ - error: `API не подтвердил завершение оптимизации OSPF. Обновите данные перед повторной попыткой. ${String(err)}`, + error: `API не подтвердил завершение оптимизации OSPF. Обновите данные перед повторной попыткой. ${String(err.original)}`, serverId: server.id, - state: rejected ? (applied.length ? "partial" : "rejected") : "unknown", - confirmedWrites: applied.length, - applied, - failedInterface: pending.interface, - needsReview: !rejected || applied.length > 0, + state: err.state, + confirmedWrites: err.applied.length, + applied: err.applied, + failedInterface: err.failedInterface, + needsReview: err.state!=="rejected", }) } return reply.status(500).send({ error: String(err) }) diff --git a/backend/src/routes/probe-storage-handler.ts b/backend/src/routes/probe-storage-handler.ts new file mode 100644 index 00000000..9c0d3e81 --- /dev/null +++ b/backend/src/routes/probe-storage-handler.ts @@ -0,0 +1,32 @@ +/** Application HTTP diagnostics use the shared v2 journal installed by0037. */ +import { randomUUID } from "node:crypto" +import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" +import { env } from "../config.js" +import { executeStorageServerProbeRest } from "../services/server-probe-storage-rest.js" + +export function createStorageProbeRoutes(): FastifyPluginAsyncZod { + return async app => { + app.post("/servers/:id/probes/run", async (request, reply) => { + const controller = new AbortController() + const requestAborted = () => controller.abort() + const responseClosed = () => { if (!reply.raw.writableEnded) controller.abort() } + // Attach before asynchronous preparation/authority/source lookups, not + // merely before native dispatch. Closing a completed response is normal. + request.raw.once("aborted", requestAborted) + reply.raw.once("close", responseClosed) + if (request.raw.aborted || reply.raw.destroyed) controller.abort() + try { + const result = await executeStorageServerProbeRest(String((request.params as { id?: string }).id ?? ""), request.body, { + actor: request.authUser, authRequired: env.authRequired, + // Neither JSON nor forwarded request headers choose durable identity. + requestId: randomUUID(), acceptedAt: Date.now(), signal: controller.signal, + }) + if (request.raw.aborted || reply.raw.destroyed) return reply + return reply.status(result.statusCode).send(result.body) + } finally { + request.raw.off("aborted", requestAborted) + reply.raw.off("close", responseClosed) + } + }) + } +} diff --git a/backend/src/routes/probes.cancel.test.ts b/backend/src/routes/probes.cancel.test.ts index 64a28685..f97aaa0e 100644 --- a/backend/src/routes/probes.cancel.test.ts +++ b/backend/src/routes/probes.cancel.test.ts @@ -29,16 +29,18 @@ console.log("Probes local HTTP: normal trace body does not abort; browser discon const {createServer}=await import("node:http") const {MikrotikClient}=await import("../services/mikrotik.js") +const {installManagedMutationSqlFixture}=await import("../test/fixtures/managed-mutation-sql.js") +const managed=installManagedMutationSqlFixture(71) let arrived!:()=>void,disconnected!:()=>void const arrival=new Promise(r=>arrived=r),disconnection=new Promise(r=>disconnected=r) const ros=createServer((req,res)=>{req.resume();req.once("end",arrived);res.once("close",disconnected)}) await new Promise(r=>ros.listen(0,"127.0.0.1",r)) try{ const addr=ros.address();assert.ok(addr&&typeof addr!=="string") - const liveClient=new MikrotikClient({host:"127.0.0.1",port:addr.port,useSsl:false,verifySsl:false,username:"fixture",password:"fixture"}) + const liveClient=new MikrotikClient({host:"127.0.0.1",port:addr.port,useSsl:false,verifySsl:false,username:"fixture",password:"fixture"},71) const controller=new AbortController() const pending=liveClient.bandwidthTest({address:"192.0.2.2",user:"fixture",password:"fixture"},controller.signal).then(()=>{throw new Error("BW не должен завершаться успехом")},error=>assert.match(error.message,/abort/i)) await arrival;controller.abort();await pending await Promise.race([disconnection,new Promise((_r,reject)=>setTimeout(()=>reject(new Error("RouterOS HTTP не закрыт")),2000))]) -}finally{await new Promise((resolve,reject)=>ros.close(error=>error?reject(error):resolve()))} +}finally{managed.restore();await new Promise((resolve,reject)=>ros.close(error=>error?reject(error):resolve()))} console.log("BW transport AbortSignal: real MikrotikClient POST closed against local fake RouterOS") diff --git a/backend/src/routes/recursive-routes.test.ts b/backend/src/routes/recursive-routes.test.ts index 2f5b5e39..39b2b708 100644 --- a/backend/src/routes/recursive-routes.test.ts +++ b/backend/src/routes/recursive-routes.test.ts @@ -97,6 +97,9 @@ assert.equal(failedProperty.statusCode,503);assert.equal(failedProperty.json().r propertyFailure=false rawPayload=[{".id":"one",gateway:"192.0.2.1"}] assert.equal((await app.inject({method:"GET",url:"/api/recursive-routes/gateways/catalog?serverId=1"})).json().gateways[0].status,"unknown") +rawPayload=[{".id":"one",gateway:"192.0.2.1",inactive:"true"}] +assert.equal((await app.inject({method:"GET",url:"/api/recursive-routes/gateways/catalog?serverId=1"})).json().gateways[0].status,"inactive") +assert.equal((await app.inject({method:"GET",url:"/api/recursive-routes/gateways?serverId=1"})).json().gateways[0].status,"down") for (const type of ["home-router", "exit-node"] as const) { server.enabled=false;server.type=type const counts={writes,rawReads,savedSnapshots} @@ -113,6 +116,14 @@ for(const state of ["partial","unknown","applied"] as const){ applyError=undefined;lateReadFailure=true const late=await app.inject({method:"PUT",url:"/api/recursive-routes",payload:{serverId:1,routes:[row]}});assert.equal(late.statusCode,502);assert.equal(late.json().state,"applied");assert.equal(late.json().metadataRecorded,true) lateReadFailure=false +rawPayload=[observed] +const legacyLive=await app.inject({method:"GET",url:"/api/recursive-routes?serverId=1"}) +assert.equal(legacyLive.statusCode,200);assert.equal(legacyLive.json().live,true);assert.equal(legacyLive.json().routes[0].distance,0) +rawPayload=[{...observed,distance:undefined}] +const legacyStale=await app.inject({method:"GET",url:"/api/recursive-routes?serverId=1"}) +assert.equal(legacyStale.json().stale,true);assert.equal(legacyStale.json().live,false) +rawPayload=[{".id":"*gw",gateway:"192.0.2.1%ether1"}] +assert.equal((await app.inject({method:"GET",url:"/api/recursive-routes/gateways?serverId=1"})).json().gateways[0].status,"unknown") await app.close() console.log("recursive routes HTTP: invalid/explicit-empty/zero/copy/error/restore ownership and payload passed without DB/RouterOS") diff --git a/backend/src/routes/recursive-routes.ts b/backend/src/routes/recursive-routes.ts index 283118f4..4bc1d04f 100644 --- a/backend/src/routes/recursive-routes.ts +++ b/backend/src/routes/recursive-routes.ts @@ -1,6 +1,9 @@ import { recursiveApplyBodySchema, recursiveTargetSchema, recursiveInputRowsSchema } from '../services/recursive-route-validation.js' import { parseRecursiveCatalog, parseRecursiveGatewayCatalog, readRecursiveEffectiveProperties, type RecursiveObservedProperty } from "../services/recursive-route-catalog.js" import { executeRecursiveApply, RecursiveApplyError } from "../services/recursive-apply.js" +import { storeRecursiveRoutes } from "../services/recursive-route-store.js" +import { planRecursiveIdentities } from "../services/recursive-route-identity.js" +import { withApplicationMutationLock, withConfigMutationLock } from "../services/config-mutation-lock.js" import { asc, eq } from "drizzle-orm" import { z } from "zod" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" @@ -9,10 +12,7 @@ import { recursiveRoutes, servers } from "../db/schema.js" import { MikrotikClient } from "../services/mikrotik.js" import { parseDbServerId } from "../utils/server-id.js" import { managedRecursiveComment } from "../managed-markers.js" -import { - mapRosManagedRoutes, - userRecursiveComment, -} from "../services/config-apply-plan.js" +import { userRecursiveComment } from "../services/config-apply-plan.js" import { appendRevisionIfChanged, canonicalRecursiveRoutes, @@ -23,32 +23,6 @@ import { type ServerRow = typeof servers.$inferSelect -interface RosRoute { - ".id"?: string - "dst-address"?: string - gateway?: string - distance?: string - scope?: string - "target-scope"?: string - "routing-table"?: string - "check-gateway"?: string - comment?: string - disabled?: string - static?: string - dynamic?: string - blackhole?: string - unreachable?: string - prohibit?: string - active?: string -} - -interface GatewayOptionDto { - id: string - name: string - ip: string - status: "up" | "down" -} - interface RecursiveRouteDto { id: string dstAddress: string @@ -63,14 +37,6 @@ interface RecursiveRouteDto { disabled: boolean } -function splitGateway(raw: string): { ip: string; name: string } | null { - const v = raw.trim() - if (!v) return null - const [ip, name] = v.split("%") - if (!ip || !/^\d{1,3}(\.\d{1,3}){3}$/.test(ip)) return null - return { ip, name: name || ip } -} - async function mapDbRoutes(serverId: number): Promise { const rows = await db .select() @@ -92,6 +58,7 @@ async function mapDbRoutes(serverId: number): Promise { disabled: r.disabled, })) } +export { mapDbRoutes as readCachedRecursiveRoutes } function mergeCachedCountry( live: RecursiveRouteDto[], @@ -99,11 +66,12 @@ function mergeCachedCountry( ): RecursiveRouteDto[] { return live.map((row) => { if (row.country) return row - const match = cached.find((c) => + const matches = cached.filter((c) => c.dstAddress === row.dstAddress && c.gateway === row.gateway && - c.distance === row.distance, + c.distance === row.distance && c.routingTable === row.routingTable, ) + const match = matches.length === 1 ? matches[0] : undefined return match?.country ? { ...row, country: match.country } : row }) } @@ -123,46 +91,32 @@ function toRouterPayload(route: RecursiveRouteDto): Record { } async function replaceDbRoutes(serverId: number, routes: RecursiveRouteDto[]) { - await db.transaction(async tx => { - await tx.delete(recursiveRoutes).where(eq(recursiveRoutes.serverId, serverId)) - if (routes.length === 0) return - const now = new Date().toISOString() - await tx.insert(recursiveRoutes).values( - routes.map((r, i) => ({ - serverId, - sortOrder: i, - dstAddress: r.dstAddress, - gateway: r.gateway, - distance: r.distance, - scope: r.scope, - targetScope: r.targetScope, - routingTable: r.routingTable || "main", - checkGateway: r.checkGateway ?? "", - country: r.country ?? "", - comment: userRecursiveComment(r.comment), - disabled: r.disabled, - createdAt: now, - updatedAt: now, - })), - ) - }) + return storeRecursiveRoutes(serverId, routes, "observed") } -async function applyRecursiveToServer( +export async function applyRecursiveToServer( server: ServerRow, routes: RecursiveRouteDto[], source: ConfigRevisionSource, + validateExisting?: (rows: ReturnType) => void | Promise, ): Promise<{ pushed: number; deleted: number }> { - const client = MikrotikClient.fromServer(server) - return executeRecursiveApply(client, routes.map(toRouterPayload), async () => { - await replaceDbRoutes(server.id, routes) - await appendRevisionIfChanged({ - serverId: server.id, - section: "recursive-routes", - source, - payload: canonicalRecursiveRoutes(routes), - }) - }) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], async () => { + await storeRecursiveRoutes(server.id, routes, source, true) + const client = MikrotikClient.fromServer(server) + return executeRecursiveApply(client, routes.map(toRouterPayload), async () => { + await storeRecursiveRoutes(server.id, routes, source) + await appendRevisionIfChanged({ + serverId: server.id, + section: "recursive-routes", + source, + payload: canonicalRecursiveRoutes(routes), + }) + }, async () => { + const observed = await readRecursiveNativeRowsForServer(server) + await validateExisting?.(parseRecursiveCatalog(observed)) + return observed + }, () => readRecursiveNativeRowsForServer(server)) + })) } const RevisionIdParamSchema = z.object({ id: z.string().min(1) }) @@ -184,8 +138,7 @@ export interface RecursiveRouteDependencies { getRevision: typeof getRevisionById } -export function createRecursiveRoutes(overrides: Partial = {}): FastifyPluginAsyncZod { - const deps: RecursiveRouteDependencies = { +const recursiveDefaults: RecursiveRouteDependencies = { getServer: async id => (await db.select().from(servers).where(eq(servers.id, id)).limit(1))[0], getRoutes: mapDbRoutes, apply: applyRecursiveToServer, getRevision: getRevisionById, readRaw: (server, purpose) => { @@ -193,16 +146,67 @@ export function createRecursiveRoutes(overrides: Partial("/ip/route?.proplist=.id,gateway,active") + ? client.get("/ip/route?.proplist=.id,gateway,active,inactive") : client.post("/ip/route/print", { ".query": ["comment"] }) }, readProperty: (server, id, property) => MikrotikClient.fromServer(server).post("/ip/route/get", { ".id": id, "value-name": property }), saveObserved: async (serverId, routes) => { - await replaceDbRoutes(serverId, routes) - await appendRevisionIfChanged({ serverId, section: "recursive-routes", source: "observed", payload: canonicalRecursiveRoutes(routes) }) + const stored = await replaceDbRoutes(serverId, routes) + await appendRevisionIfChanged({ serverId, section: "recursive-routes", source: "observed", payload: canonicalRecursiveRoutes(stored) }) }, - ...overrides, } + +/** Strict observation shared by both transports. REST can persist metadata; + * observers opt out. An unavailable source always fails this catalog read. */ +export async function readRecursiveCatalogForServer(server: ServerRow, deps: Pick = recursiveDefaults, options: { persistObserved?: boolean } = {}) { + const read = async () => { + if (!server.enabled) throw new Error("Устройство отключено: каталог маршрутов недоступен") + const observed = await readRecursiveEffectiveProperties(await deps.readRaw(server), (id, property) => deps.readProperty(server, id, property)) + const routes = mergeCachedCountry(parseRecursiveCatalog(observed), await deps.getRoutes(server.id)) + if (options.persistObserved !== false) await deps.saveObserved(server.id, routes) + return { serverId: server.id, routes, live: true, stale: false } + } + return deps === recursiveDefaults && options.persistObserved !== false + ? withApplicationMutationLock(() => withConfigMutationLock([server.id], read)) : read() +} + +export async function readRecursiveNativeRowsForServer(server: ServerRow) { + if (!server.enabled) throw new Error("Устройство отключено: опрос маршрутов недоступен") + return readRecursiveEffectiveProperties(await recursiveDefaults.readRaw(server), (id, property) => recursiveDefaults.readProperty(server, id, property)) +} + +export async function readRecursiveGatewayCatalogForServer(server: ServerRow, deps: Pick = recursiveDefaults) { + if (!server.enabled) throw new Error("Устройство отключено: каталог шлюзов недоступен") + return { serverId: server.id, gateways: parseRecursiveGatewayCatalog(await deps.readRaw(server, "gateways")) } +} + +export async function readRecursiveGatewaysForServer(server: ServerRow, deps: Pick = recursiveDefaults) { + const result = await readRecursiveGatewayCatalogForServer(server, deps) + return { ...result, gateways: result.gateways.map(row => ({ ...row, status: row.status === "active" ? "up" : row.status === "inactive" ? "down" : "unknown" })) } +} + +/** Legacy application read, including its explicit stale fallback. MCP observers + * share the source/parser while leaving cache and revision history untouched. */ +export async function readRecursiveRoutesForServer(server: ServerRow, deps: Pick = recursiveDefaults, options: { persistObserved?: boolean } = {}) { + if (!server.enabled) throw new Error("Устройство отключено: опрос маршрутов недоступен") + const read = async () => { + const cached = await deps.getRoutes(server.id) + try { + const result = await readRecursiveCatalogForServer(server, deps, options) + const routes = options.persistObserved === false + ? planRecursiveIdentities(cached, result.routes, new Set(), "observed").rows.map((row, index) => ({ ...row, id: row.id || result.routes[index].id })) + : await deps.getRoutes(server.id) + return { ...result, routes } + } catch { + return { serverId: server.id, routes: cached, live: false, stale: true, error: "Не удалось подтвердить живой каталог; возвращена сохранённая конфигурация" } + } + } + return deps === recursiveDefaults && options.persistObserved !== false + ? withApplicationMutationLock(() => withConfigMutationLock([server.id], read)) : read() +} + +export function createRecursiveRoutes(overrides: Partial = {}): FastifyPluginAsyncZod { + const deps: RecursiveRouteDependencies = Object.keys(overrides).length ? { ...recursiveDefaults, ...overrides } : recursiveDefaults return async (app) => { app.get("/recursive-routes/catalog", async (req, reply) => { @@ -212,10 +216,7 @@ export function createRecursiveRoutes(overrides: Partial deps.readProperty(server, id, property)) - const routes = mergeCachedCountry(parseRecursiveCatalog(observed), await deps.getRoutes(serverId)) - await deps.saveObserved(serverId, routes) - return reply.send({ serverId, routes, live: true, stale: false }) + return reply.send(await readRecursiveCatalogForServer(server, deps)) } catch (error) { return reply.status(503).send({ error: error instanceof Error ? error.message : "Не удалось прочитать каталог маршрутов" }) } @@ -227,7 +228,7 @@ export function createRecursiveRoutes(overrides: Partial("/ip/route") - const map = new Map() - for (const r of rosRoutes) { - const g = splitGateway(r.gateway ?? "") - if (!g) continue - const id = `${g.ip}%${g.name}` - if (!map.has(id)) { - map.set(id, { - id, - name: g.name, - ip: g.ip, - status: r.active === "true" ? "up" : "down", - }) - } - } - return reply.send({ serverId, gateways: [...map.values()] }) + return reply.send(await readRecursiveGatewaysForServer(server, deps)) } catch (err) { return reply.status(500).send({ error: String(err) }) } @@ -272,30 +257,7 @@ export function createRecursiveRoutes(overrides: Partial("/ip/route") - const live = mergeCachedCountry(mapRosManagedRoutes(rosRoutes), cached) - await replaceDbRoutes(serverId, live) - await appendRevisionIfChanged({ - serverId, - section: "recursive-routes", - source: "observed", - payload: canonicalRecursiveRoutes(live), - }) - const stored = await deps.getRoutes(serverId) - return reply.send({ serverId, routes: stored, live: true, stale: false }) - } catch (err) { - app.log.warn({ serverId, err: String(err) }, "recursive live GET failed, serving cache") - return reply.send({ - serverId, - routes: cached, - live: false, - stale: true, - error: String(err), - }) - } + return reply.send(await readRecursiveRoutesForServer(server, deps)) }) app.put("/recursive-routes", async (req, reply) => { diff --git a/backend/src/routes/servers-api-ping.ts b/backend/src/routes/servers-api-ping.ts index 9a5f88c9..00d223c5 100644 --- a/backend/src/routes/servers-api-ping.ts +++ b/backend/src/routes/servers-api-ping.ts @@ -1,8 +1,8 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" -import { scheduleAlertEngineAfterDataCollectors } from "../services/alert-collector-hooks.js" +import { manualDataCollection } from "../services/manual-data-collection.js" +import { createServersManualCollectionRoute } from "./manual-data-collection.js" import { refreshScheduler } from "../services/scheduler.js" import { - collectServersRestPingOnce, getServersApiPingSettings, getServersRestPingCollectorState, updateServersApiPingSettings, @@ -46,17 +46,7 @@ const serversApiPingRoutes: FastifyPluginAsyncZod = async (app) => { }) }) - app.post("/servers-api-ping/collect-now", async (_req, reply) => { - await collectServersRestPingOnce() - scheduleAlertEngineAfterDataCollectors() - const row = await getServersApiPingSettings() - return reply.send({ - ok: true, - lastCollectedAt: row.lastCollectedAt ?? null, - lastDurationMs: row.lastDurationMs ?? null, - lastError: row.lastError || null, - }) - }) + await app.register(createServersManualCollectionRoute(manualDataCollection.collectServersRestPing)) } export default serversApiPingRoutes diff --git a/backend/src/routes/servers.ts b/backend/src/routes/servers.ts index 78930339..2f493c9f 100644 --- a/backend/src/routes/servers.ts +++ b/backend/src/routes/servers.ts @@ -11,8 +11,8 @@ import { type SnapshotsQuery, type TestConnectionRequest, } from "../types/server.js" -import { pollServer } from "../services/poller.js" -import { MikrotikClient, MikrotikError, routerOsAccessHint } from "../services/mikrotik.js" +import { handleDurableServerPollHttp } from "../services/server-poll-http.js" +import { performServerConnectionTest } from "../services/server-connection-test.js" import { resolveRosSrcIpv4 } from "../utils/ros-src-address.js" import { createServer, @@ -24,7 +24,7 @@ import { } from "../modules/servers/service/servers-service.js" import { getServerRowById } from "../modules/servers/repository/servers-repository.js" import {serverReadForUser} from "../modules/servers/mapper/servers-mapper.js" -import { readInternetPathRows } from "../services/internet-path-snapshot.js" +import { readWanRuntimeForServer } from "../services/wan-runtime.js" // ── plugin ───────────────────────────────────────────────────────────────────── @@ -32,49 +32,9 @@ const serversRoutes: FastifyPluginAsyncZod = async (app) => { // POST /api/servers/test-connection — check credentials before saving app.post("/test-connection", { schema: { body: TestConnectionSchema } }, async (req, reply) => { - const { host, port, useSsl, verifySsl, apiPath, username, password } = req.body as TestConnectionRequest - const client = new MikrotikClient({ host, port, useSsl, verifySsl, apiPath, username, password }) - - try { - const t0 = performance.now() - const [identity, resource] = await Promise.all([ - client.getIdentity(), - client.getResource(), - ]) - const latencyMs = Math.round(performance.now() - t0) - - return reply.send({ - success: true, - latencyMs, - identity: identity.name, - version: resource["version"], - boardName: resource["board-name"], - uptime: resource["uptime"], - message: `Подключено · RouterOS ${resource["version"]} · ${resource["board-name"]}`, - }) - } catch (err) { - let message = "Не удалось подключиться" - - if (err instanceof MikrotikError) { - message = routerOsAccessHint(err.statusCode, err.body) ?? `Ошибка RouterOS API: ${err.statusCode}` - } else if (err instanceof Error) { - const msg = err.message.toLowerCase() - if (msg.includes("timeout") || msg.includes("abort")) - message = `Нет ответа от ${host}:${port} — проверьте IP и что сервис www/www-ssl включён` - else if (msg.includes("econnrefused")) - message = `Соединение отклонено — порт ${port} закрыт` - else if (msg.includes("cert") || msg.includes("ssl") || msg.includes("tls")) - message = "Ошибка SSL-сертификата — отключите «Проверять SSL» для self-signed" - else if (msg.includes("enotfound") || msg.includes("getaddrinfo")) - message = `Хост не найден: ${host}` - else - message = err.message - } - - return reply.status(502).send({ success: false, message }) - } + const { outcome: _outcome, ...result } = await performServerConnectionTest(req.body as TestConnectionRequest) + return reply.status(result.success ? 200 : 502).send(result) }) - // GET /api/servers app.get("/", async (req, reply) => { return reply.send((await listServersRead()).map(server=>serverReadForUser(server,req.authUser))) @@ -90,143 +50,13 @@ const serversRoutes: FastifyPluginAsyncZod = async (app) => { return reply.send({ host: server.host, ipv4 }) }) - // GET /api/servers/:id/wan-runtime — DHCP lease + active default route for HomeRouter WAN uplinks + // Shared with MCP; unknown/ambiguous routing evidence remains explicit. app.get("/:id/wan-runtime", { schema: { params: ServerIdParamSchema } }, async (req, reply) => { - const params = req.params as ServerIdParams - const server = await getServerReadById(params.id) + const server = await getServerRowById((req.params as ServerIdParams).id) if (!server) return reply.status(404).send({ error: "Server not found" }) - if (server.enabled === false) return reply.status(409).send({ error: "Сервер выключен. Опрос WAN не выполнялся" }) - - const toIp = (raw: string | null | undefined): string | null => { - const v = String(raw ?? "").trim() - if (!v) return null - return v.split("/")[0]?.trim() ?? null - } - const ipv4ToUint = (ip: string): number | null => { - const p = ip.split(".").map((x) => Number.parseInt(x, 10)) - if (p.length !== 4 || p.some((x) => !Number.isFinite(x) || x < 0 || x > 255)) return null - return (((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]) >>> 0) - } - const maskFromLen = (len: number): number => { - if (len <= 0) return 0 - if (len >= 32) return 0xffffffff - return (~((1 << (32 - len)) - 1)) >>> 0 - } - const parseDstRoute = (dst: string): { net: number; maskBits: number } | null => { - const t = dst.trim() - if (!t) return null - if (!t.includes("/")) { - const ip = ipv4ToUint(t) - return ip == null ? null : { net: ip, maskBits: 32 } - } - const [addr, mb] = t.split("/") - const ip = ipv4ToUint(addr.trim()) - const maskBits = Number.parseInt((mb ?? "").trim(), 10) - if (ip == null || !Number.isFinite(maskBits) || maskBits < 0 || maskBits > 32) return null - const mask = maskFromLen(maskBits) - return { net: ip & mask, maskBits } - } - const routeHasIp = (routeDst: string, ip: string): boolean => { - const ipu = ipv4ToUint(ip) - const cidr = parseDstRoute(routeDst) - if (ipu == null || !cidr) return false - const mask = maskFromLen(cidr.maskBits) - return (ipu & mask) === (cidr.net & mask) - } - const gatewayIface = (gw: string | null | undefined): string | null => { - const v = String(gw ?? "").trim() - if (!v) return null - const idx = v.indexOf("%") - if (idx < 0) return null - return v.slice(idx + 1).trim() || null - } - - try { - const row = await getServerRowById(params.id) - if (!row) return reply.status(404).send({ error: "Server not found" }) - if (row.enabled === false) return reply.status(409).send({ error: "Сервер выключен. Опрос WAN не выполнялся" }) - const client = MikrotikClient.fromServer(row) - const isTrue = (v: unknown) => { - const s = String(v ?? "").trim().toLowerCase() - return s === "true" || s === "yes" - } - const [dhcpRaw, ipAddrs, routes] = await Promise.all([ - client.get("/ip/dhcp-client").then(raw => readInternetPathRows(raw, "WAN DHCP", ["interface"])), - client.get("/ip/address").then(raw => readInternetPathRows(raw, "WAN addresses", ["address", "interface"])), - client.get("/ip/route").then(raw => readInternetPathRows(raw, "WAN routes", ["dst-address"])), - ]) - - const defaultRoute = routes - .filter((r) => String(r["dst-address"] ?? "").trim() === "0.0.0.0/0") - // Только реально ACTIVE default routes. - .filter((r) => isTrue((r as unknown as Record).active)) - // Эквивалент CLI: routing-table=main - .filter((r) => { - const rt = String((r as unknown as Record)["routing-table"] ?? "").trim().toLowerCase() - return rt === "" || rt === "main" - }) - .filter((r) => String((r as unknown as Record).disabled ?? "false").toLowerCase() !== "true") - .filter((r) => String((r as unknown as Record).inactive ?? "false").toLowerCase() !== "true") - .sort((a, b) => Number(a.distance ?? 255) - Number(b.distance ?? 255))[0] - - const defaultGateway = String(defaultRoute?.gateway ?? "").trim() || null - const immediateGw = String((defaultRoute as unknown as Record | undefined)?.["immediate-gw"] ?? "").trim() || null - const gwFromImmediate = immediateGw ? immediateGw.split("%")[0]?.trim() ?? null : null - const gwIp = toIp(defaultGateway) ?? gwFromImmediate - const dhcpIfaceByGateway = gwIp == null - ? null - : ( - dhcpRaw.find((d) => { - const status = String(d.status ?? "").trim().toLowerCase() - if (status && status !== "bound") return false - return toIp(d.gateway) === gwIp - }) - ) - const directIfaceByGwSubnet = - gwIp == null - ? null - : ( - routes - .filter((r) => String((r as unknown as Record).active ?? "").toLowerCase() === "true") - .filter((r) => String((r as unknown as Record)["dst-address"] ?? "").trim() !== "0.0.0.0/0") - .filter((r) => String((r as unknown as Record).disabled ?? "false").toLowerCase() !== "true") - .filter((r) => String((r as unknown as Record).inactive ?? "false").toLowerCase() !== "true") - .find((r) => routeHasIp(String((r as unknown as Record)["dst-address"] ?? ""), gwIp)) - ) - const defaultInterface = - gatewayIface(immediateGw) - || gatewayIface(defaultGateway) - || String((dhcpIfaceByGateway as unknown as Record | undefined)?.interface ?? "").trim() - || String(defaultRoute?.interface ?? "").trim() - || String((directIfaceByGwSubnet as unknown as Record | undefined)?.interface ?? "").trim() - || null - - const uplinks = (server.wanUplinks ?? []).map((w) => { - const iface = String(w.iface ?? "").trim() - const dhcp = dhcpRaw.find((d) => String(d.interface ?? "").trim() === iface) - const fromDhcp = toIp(dhcp?.address) - const fromIpAddr = toIp(ipAddrs.find((a) => String(a.interface ?? "").trim() === iface)?.address) - const leasedIp = fromDhcp ?? fromIpAddr - return { - id: w.id, - iface, - name: w.name, - isp: w.isp, - configuredIp: w.ip, - leasedIp, - dhcpStatus: String(dhcp?.status ?? "").trim() || null, - isDefault: defaultInterface != null && defaultInterface === iface, - } - }) - - return reply.send({ - defaultGateway, - immediateGateway: immediateGw, - defaultInterface, - uplinks, - }) - } catch (err) { - return reply.status(502).send({ error: err instanceof Error ? `WAN: ${err.message}` : "Не удалось прочитать состояние WAN" }) + try { return reply.send(await readWanRuntimeForServer(server)) } + catch (error) { + return reply.status((error as { statusCode?: number }).statusCode ?? 502).send({ error: error instanceof Error ? error.message : "Не удалось прочитать состояние WAN" }) } }) @@ -265,18 +95,7 @@ const serversRoutes: FastifyPluginAsyncZod = async (app) => { }) // POST /api/servers/:id/poll - app.post("/:id/poll", { schema: { params: ServerIdParamSchema } }, async (req, reply) => { - const params = req.params as ServerIdParams - const existing = await getServerReadById(params.id) - if (!existing) return reply.status(404).send({ error: "Server not found" }) - - try { - const snap = await pollServer(params.id) - return reply.send(snap) - } catch (err) { - return reply.status(500).send({ error: (err as Error).message }) - } - }) + app.post("/:id/poll", { schema: { params: ServerIdParamSchema } }, handleDurableServerPollHttp) // GET /api/servers/:id/snapshots app.get( diff --git a/backend/src/routes/sidebar-counts.ts b/backend/src/routes/sidebar-counts.ts index c3b94ac7..45177145 100644 --- a/backend/src/routes/sidebar-counts.ts +++ b/backend/src/routes/sidebar-counts.ts @@ -1,42 +1,13 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" -import {z} from "zod" -import {boundedCompleteCount,completeCollectionCount} from "../services/complete-count.js" -import { count } from "drizzle-orm" -import { listCertificatesFromServers } from "../services/certificates-service.js" -import { countWireGuardInterfaces } from "../services/wireguard-live.js" -import { countIpsecClients } from "../services/ipsec-live.js" -import { countVxlanTunnels } from "../services/vxlan-live.js" -import { countContainers } from "../services/containers-live.js" -import { countBgpSessions } from "../services/bgp-peers-live.js" -import { db } from "../db/index.js" -import { - filterRules, - recursiveRoutes, - servers, - uptimeProbes, - uptimeSpeedProbes, -} from "../db/schema.js" -import { listUsers } from "../modules/users/service/users-service.js" +import { defaultSidebarCountsDependencies, readSidebarCounts, type SidebarCountsDependencies } from "../services/sidebar-counts.js" +export { defaultSidebarCountsDependencies } from "../services/sidebar-counts.js" -async function tableCount(table: typeof servers | typeof filterRules | typeof uptimeProbes | typeof uptimeSpeedProbes | typeof recursiveRoutes): Promise { - const rows = await db.select({ n: count() }).from(table) - const value=rows[0]?.n - if(typeof value!=="number"||!Number.isSafeInteger(value)||value<0)throw Error("Счётчик БД не подтверждён") - return value +export function createSidebarCountsRoutes(deps: SidebarCountsDependencies = defaultSidebarCountsDependencies): FastifyPluginAsyncZod { + return async app => { + app.get("/sidebar-counts", async (_req, reply) => { + try { return reply.header("Cache-Control", "no-store").send(await readSidebarCounts(deps)) } + catch { return reply.status(500).send({ error: "Основные счётчики не подтверждены" }) } + }) + } } - -const countSchema=z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER) -const coreSchema=z.object({servers:countSchema,filterRules:countSchema,uptimeProbes:countSchema,uptimeSpeedProbes:countSchema,monitoringItems:countSchema,recursiveRoutes:countSchema,users:countSchema}).strict().refine(value=>value.monitoringItems===value.uptimeProbes+value.uptimeSpeedProbes) -interface Dependencies{core:()=>Promise;certificates:()=>Promise;wireguard:()=>Promise;ipsec:()=>Promise;bgp:()=>Promise;vxlan:()=>Promise;containers:()=>Promise} -export const defaultSidebarCountsDependencies:Dependencies={ - core:async()=>{const [serversTotal,filterRulesTotal,uptimeProbesTotal,uptimeSpeedProbesTotal,recursiveRoutesTotal,users]=await Promise.all([tableCount(servers),tableCount(filterRules),tableCount(uptimeProbes),tableCount(uptimeSpeedProbes),tableCount(recursiveRoutes),listUsers()]);return {servers:serversTotal,filterRules:filterRulesTotal,uptimeProbes:uptimeProbesTotal,uptimeSpeedProbes:uptimeSpeedProbesTotal,monitoringItems:uptimeProbesTotal+uptimeSpeedProbesTotal,recursiveRoutes:recursiveRoutesTotal,users:users.length}}, - certificates:()=>boundedCompleteCount(listCertificatesFromServers,value=>completeCollectionCount(value,"certificates")),wireguard:countWireGuardInterfaces,ipsec:countIpsecClients,bgp:countBgpSessions,vxlan:countVxlanTunnels,containers:countContainers, -} -export function createSidebarCountsRoutes(deps:Dependencies=defaultSidebarCountsDependencies):FastifyPluginAsyncZod{return async app=>{ - app.get("/sidebar-counts",async(_req,reply)=>{ - const [core,certificates,wireguard,ipsec,bgpSessions,vxlan,containers]=await Promise.all([deps.core(),boundedCompleteCount(deps.certificates,x=>x),boundedCompleteCount(deps.wireguard,x=>x),boundedCompleteCount(deps.ipsec,x=>x),boundedCompleteCount(deps.bgp,x=>x),boundedCompleteCount(deps.vxlan,x=>x),boundedCompleteCount(deps.containers,x=>x)]) - const parsed=coreSchema.safeParse(core);if(!parsed.success)return reply.status(500).send({error:"Основные счётчики не подтверждены"}) - return reply.header("Cache-Control","no-store").send({...parsed.data,certificates,wireguard,ipsec,bgpSessions,vxlan,containers}) - }) -}} export default createSidebarCountsRoutes() diff --git a/backend/src/routes/speed-test.test.ts b/backend/src/routes/speed-test.test.ts index 7ab6e391..9a2ed0d7 100644 --- a/backend/src/routes/speed-test.test.ts +++ b/backend/src/routes/speed-test.test.ts @@ -3,12 +3,11 @@ import Fastify from "fastify" import { SignJWT } from "jose" import { speedTestCommandSchema, type SpeedTestCommand } from "@mmapp/contracts" import { createSpeedTestRoute } from "./speed-test.js" -import { SpeedProbeEndpointsChanged, SpeedProbeContextChanged, SpeedRunPersistenceUnknown } from "../services/speed-run-persistence.js" +import { SpeedProbeEndpointsChanged, SpeedProbeContextChanged, SpeedRunPersistenceUnknown, SpeedTestServerNotFound } from "../services/speed-run-persistence.js" const commands: SpeedTestCommand[] = [], failures: Array = [] -let lookups = 0, missing = false, problem: Error | null = null +let problem: Error | null = null const result = (c: SpeedTestCommand) => ({ createdAt: "2026-10-07T00:00:00Z", runId: c.runId ?? "ad-hoc", srcInterface: c.srcInterface || null, dstInterface: c.dstInterface || null, address: "192.0.2.2", srcAddress: "192.0.2.1", dstAddress: "192.0.2.2", srcInterfaceAddress: null, dstInterfaceAddress: null, protocol: c.protocol, direction: c.direction, durationSec: c.durationSec, txAvgMbps: null, rxAvgMbps: 0, txSeries: [], rxSeries: [0], raw: [{ status: "done testing" }], afterBtPing: { rttMs: null, lossPct: null, error: null } }) const deps = { - serverExists: async (_id: number) => { lookups++; return !missing }, run: async (c: SpeedTestCommand) => { commands.push(c); if (problem) throw problem; return result(c) }, failure: async (c: SpeedTestCommand & { message: string }) => { failures.push(c) }, } @@ -16,15 +15,17 @@ const app = Fastify(); await app.register(createSpeedTestRoute(deps), { prefix: const send = (body: unknown) => app.inject({ method: "POST", url: "/api/uptime/speed-test", headers: { "content-type": "application/json" }, payload: JSON.stringify(body) }) const base = { srcServerId: 1, dstServerId: 2 } const bad = [null, [], {}, { ...base, srcServerId: "1junk" }, { ...base, srcServerId: "1.0" }, { ...base, srcServerId: "01" }, { ...base, srcServerId: true }, { ...base, srcServerId: 0 }, { ...base, srcServerId: -1 }, { ...base, srcServerId: 1.5 }, { ...base, srcServerId: 9007199254740992 }, { ...base, dstServerId: "1" }, { ...base, protocol: "TCP" }, { ...base, protocol: null }, { ...base, direction: "other" }, { ...base, durationSec: "10junk" }, { ...base, durationSec: "3.5" }, { ...base, durationSec: 2 }, { ...base, durationSec: 2147483648 }, { ...base, srcInterface: null }, { ...base, runId: 123 }, { ...base, probeId: false }, ...["runId", "probeId", "srcInterface", "dstInterface"].map(key => ({ ...base, [key]: "bad" + String.fromCharCode(0) }))] +for(const maxMbps of [0,1001,1.5,"10"])bad.push({...base,maxMbps}) for (const body of bad) assert.equal((await send(body)).statusCode, 400, JSON.stringify(body)) -assert.equal(lookups, 0); assert.equal(commands.length, 0); assert.equal(failures.length, 0) +assert.equal(commands.length, 0); assert.equal(failures.length, 0) assert.equal(speedTestCommandSchema.safeParse({ ...base, durationSec: Infinity }).success, false) const first = await send(base); assert.equal(first.statusCode, 200); assert.equal(first.json().result.txAvgMbps, null); assert.equal(first.json().result.rxAvgMbps, 0); assert.equal(first.json().result.createdAt, "2026-10-07T00:00:00Z") assert.deepEqual(commands[0], { ...base, srcInterface: "", dstInterface: "", protocol: "tcp", direction: "both", durationSec: 10 }) -const explicit = await send({ srcServerId: "1", dstServerId: "2", runId: " run-2 ", probeId: " probe ", srcInterface: " ether1 ", protocol: "udp", direction: "receive", durationSec: "3", lastTxAvgMbps: 999 }) +const explicit = await send({ srcServerId: "1", dstServerId: "2", runId: " run-2 ", probeId: " probe ", srcInterface: " ether1 ", protocol: "udp", direction: "receive", durationSec: "3", maxMbps:10,lastTxAvgMbps: 999 }) +assert.equal(commands[1].maxMbps,10) assert.equal(explicit.statusCode, 200); assert.equal(commands[1].runId, "run-2"); assert.equal(commands[1].probeId, "probe"); assert.equal(commands[1].srcInterface, "ether1"); assert.equal(commands[1].protocol, "udp"); assert.equal(commands[1].direction, "receive"); assert.equal(commands[1].durationSec, 3); assert.equal("lastTxAvgMbps" in commands[1], false) assert.equal(speedTestCommandSchema.parse({ ...base, runId: " ", probeId: "" }).probeId, undefined) -missing = true; assert.equal((await send(base)).statusCode, 404); assert.equal(commands.length, 2); assert.equal(failures.length, 0); missing = false +problem = new SpeedTestServerNotFound(); assert.equal((await send(base)).statusCode, 404); assert.equal(commands.length, 3); assert.equal(failures.length, 0); problem = null for (const [error, code] of [[new SpeedProbeEndpointsChanged(), 409], [new SpeedProbeContextChanged(), 409], [Error("На интерфейсе назначения нет активного IP"), 400], [Error("Server not found"), 400], [Error("authentication failed"), 502], [Error("socket hang up"), 502]] as const) { problem = error; const response = await send({ ...base, runId: "failed", direction: "transmit", durationSec: 20 }); assert.equal(response.statusCode, code); assert.equal(failures.at(-1)?.runId, "failed"); assert.equal(failures.at(-1)?.durationSec, 20); assert.equal(failures.at(-1)?.direction, "transmit") } @@ -44,11 +45,11 @@ const unknown = await send(base) assert.equal(unknown.statusCode, 502); assert.equal(unknown.json().outcome, "unknown") assert.equal(unknown.json().runId, "actual-run-id"); assert.match(unknown.json().error, /сохранение результата не подтверждено/) assert.equal(failures.length, beforePersistenceUnknown.failures); assert.equal(commands.length, beforePersistenceUnknown.runs + 1) -await app.close(); console.log("Speed-test HTTP:26 invalid inputs; explicit persistence unknown/runId skips failure write and command retry") +await app.close(); console.log(`Speed-test HTTP:${bad.length} invalid inputs; explicit persistence unknown/runId skips failure write and command retry`) process.env.AUTH_REQUIRED = "true"; process.env.AUTH_JWT_SECRET = "speed-command-test-secret"; process.env.AUTH_ISSUER = "https://auth.test.local"; process.env.NODE_ENV = "test" const { default: authPlugin, requireAuth } = await import("../plugins/auth.js") const secured = Fastify(); await secured.register(authPlugin); secured.addHook("preHandler", requireAuth) -problem = null; const before = { lookups, runs: commands.length } +problem = null; const before = { runs: commands.length } await secured.register(createSpeedTestRoute(deps), { prefix: "/api" }) const authorized = (token?: string) => secured.inject({ method: "POST", url: "/api/uptime/speed-test", payload: base, headers: token ? { authorization: "Bearer " + token } : {} }) assert.equal((await authorized()).statusCode, 401) @@ -56,5 +57,5 @@ for (const permission of ["mm:uptime:read", "mm:servers:write", "mm:uptime:write const token = await new SignJWT({ sub: "fixture", apps: ["mm"], permissions: [permission] }).setProtectedHeader({ alg: "HS256" }).setIssuer("https://auth.test.local").setExpirationTime("1h").sign(new TextEncoder().encode("speed-command-test-secret")) assert.equal((await authorized(token)).statusCode, permission === "mm:uptime:write" ? 200 : 403) } -assert.equal(lookups, before.lookups + 2); assert.equal(commands.length, before.runs + 1) +assert.equal(commands.length, before.runs + 1) await secured.close(); console.log("Speed-test RBAC:401/403 before dependencies, uptime writer accepted; no PostgreSQL or RouterOS") diff --git a/backend/src/routes/speed-test.ts b/backend/src/routes/speed-test.ts index 1f74cbe6..307ef271 100644 --- a/backend/src/routes/speed-test.ts +++ b/backend/src/routes/speed-test.ts @@ -1,11 +1,10 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { speedTestCommandSchema, type SpeedTestCommand } from "@mmapp/contracts" import type { SpeedTestRunResult } from "../services/uptime-speed-service.js" -import { SpeedProbeContextChanged, SpeedRunPersistenceUnknown } from "../services/speed-run-persistence.js" +import { SpeedProbeContextChanged, SpeedRunPersistenceUnknown, SpeedTestServerNotFound } from "../services/speed-run-persistence.js" import { speedRunErrorMessage } from "../services/speed-run-error-message.js" export function createSpeedTestRoute(deps: { - serverExists: (id: number) => Promise run: (command: SpeedTestCommand) => Promise failure: (command: SpeedTestCommand & { message: string }) => Promise }): FastifyPluginAsyncZod { @@ -14,11 +13,10 @@ export function createSpeedTestRoute(deps: { const parsed = speedTestCommandSchema.safeParse(req.body) if (!parsed.success) return reply.code(400).send({ error: "Проверьте источник, назначение, протокол, направление и целую длительность теста от 3 секунд." }) const command = parsed.data - const exists = await Promise.all([deps.serverExists(command.srcServerId), deps.serverExists(command.dstServerId)]) - if (exists.some(value => !value)) return reply.code(404).send({ error: "Server not found" }) try { return reply.send({ ok: true, result: await deps.run(command) }) } catch (error) { + if (error instanceof SpeedTestServerNotFound) return reply.code(404).send({ error: error.message }) if (error instanceof SpeedRunPersistenceUnknown) { return reply.code(502).send({ error: error.message, outcome: "unknown", runId: error.runId }) } diff --git a/backend/src/routes/system-database.test.ts b/backend/src/routes/system-database.test.ts index 35644cd0..fae48455 100644 --- a/backend/src/routes/system-database.test.ts +++ b/backend/src/routes/system-database.test.ts @@ -4,14 +4,34 @@ import {createSystemDatabaseRoutes} from "./system-database.js" import {withOperationLock} from "../db/operation-lock.js" const app=Fastify(),dump=Buffer.from("PGDMPfixture") let exports=0,restores=0,events=0,eventFails=true,coreFails=false,held=false +let unavailableCode: string | undefined +let maintenanceError: string | undefined, lastEventTitle: string | undefined let gate:Promise|undefined let entered:(()=>void)|undefined const lock=async()=>({query:async(sql:string)=>{if(sql.includes("pg_try")){const acquired=!held;if(acquired)held=true;return {rows:[{acquired}]}}held=false;return {rows:[{released:true}]}},release:()=>{}}) -await app.register(createSystemDatabaseRoutes({exportBackup:async()=>{exports++;if(coreFails)throw Error("dump failed");return {filename:"fixture.dump",buffer:dump,listenerError:"listener warning"}},restoreBackup:async()=>withOperationLock(lock,1,async()=>{restores++;entered?.();if(gate)await gate;if(coreFails)throw Error("restore failed");return {schedulerError:"scheduler warning",cleanupError:"cleanup warning"}},"Операция с базой приложения уже выполняется"),event:async()=>{events++;if(eventFails)throw Error("event fixture secret");return undefined as never}}),{prefix:"/api"}) +const refuseUnavailable = () => { + if (unavailableCode) throw Object.assign(new Error("Состояние базы не подтверждено"), { code: unavailableCode }) +} +await app.register(createSystemDatabaseRoutes({exportBackup:async()=>{exports++;refuseUnavailable();if(coreFails)throw Error("dump failed");return {filename:"fixture.dump",buffer:dump,listenerError:"listener warning"}},restoreBackup:async()=>withOperationLock(lock,1,async()=>{restores++;entered?.();if(gate)await gate;refuseUnavailable();if(coreFails)throw Error("restore failed");return {schedulerError:"scheduler warning",cleanupError:"cleanup warning",maintenanceError}},"Операция с базой приложения уже выполняется"),event:async input=>{events++;lastEventTitle=input.title;if(eventFails)throw Error("event fixture secret");return undefined as never}}),{prefix:"/api"}) const restore=()=>app.inject({method:"POST",url:"/api/system/database/restore",headers:{"content-type":"application/octet-stream"},payload:dump}) let response=await app.inject({method:"GET",url:"/api/system/database/backup"});assert.equal(response.statusCode,200);assert.deepEqual(response.rawPayload,dump);let warnings=JSON.parse(decodeURIComponent(response.headers["x-system-database-outcome"] as string));assert.equal(warnings.listenerError,"listener warning");assert.match(warnings.eventError,/событие/);assert.equal(response.body.includes("secret"),false) response=await restore();assert.equal(response.statusCode,200);assert.equal(response.json().ok,true);assert.equal(response.json().cleanupError,"cleanup warning");assert.match(response.json().eventError,/событие/) coreFails=true;const before=events;response=await restore();assert.equal(response.statusCode,400);assert.equal(response.json().ok,undefined);assert.equal(events,before);response=await app.inject({method:"GET",url:"/api/system/database/backup"});assert.equal(response.statusCode,500);assert.equal(events,before) coreFails=false;eventFails=false;let release!:()=>void;gate=new Promise(resolve=>release=resolve);const started=new Promise(resolve=>entered=resolve);const first=restore();await started;const runs=restores;response=await restore();assert.equal(response.statusCode,409);assert.equal(restores,runs);release();assert.equal((await first).statusCode,200);gate=undefined response=await app.inject({method:"POST",url:"/api/system/database/restore",headers:{"content-type":"application/octet-stream"},payload:Buffer.alloc(0)});assert.equal(response.statusCode,400);assert.equal(restores,runs) -assert.equal(exports,2);await app.close();console.log("System DB HTTP: confirmed binary/restore, separate audit warnings, failures, nonqueued conflict and no retry") +assert.equal(exports,2) +maintenanceError = "Карантин сохранён" +response = await restore() +assert.equal(response.statusCode, 200) +assert.equal(response.json().maintenanceError, maintenanceError) +assert.equal(lastEventTitle, "Завершение восстановления базы требует проверки") +const beforeRefusal = events +for (const code of ["DATABASE_RESTORE_CHECKPOINT_REFUSED", "DATABASE_RESTORE_DATA_REFUSED", "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE", "DATABASE_RUNTIME_SOURCE_REFUSED"]) { + unavailableCode = code + for (const result of [await app.inject({ method: "GET", url: "/api/system/database/backup" }), await restore()]) { + assert.equal(result.statusCode, 503) + assert.deepEqual(result.json(), { error: "Состояние базы не подтверждено", code }) + } +} +assert.equal(events, beforeRefusal) +await app.close();console.log("System DB HTTP: confirmed binary/restore, separate audit warnings, protective 503 without audit, nonqueued conflict and no retry") diff --git a/backend/src/routes/system-database.ts b/backend/src/routes/system-database.ts index e6e7b81b..aa2ee539 100644 --- a/backend/src/routes/system-database.ts +++ b/backend/src/routes/system-database.ts @@ -2,6 +2,7 @@ import type { FastifyPluginAsync } from "fastify" import { appendEvent } from "../modules/events/service/events-service.js" import { exportSystemDatabaseBackup, + exportSystemDatabaseBackupWithEvent, restoreSystemDatabaseBackup, } from "../services/system-database-backup.js" @@ -10,6 +11,22 @@ interface Dependencies { restoreBackup: typeof restoreSystemDatabaseBackup event: typeof appendEvent } + +const unavailableCodes = new Set([ + "DATABASE_RESTORE_CHECKPOINT_REFUSED", + "DATABASE_RESTORE_DATA_REFUSED", + "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE", + "DATABASE_RUNTIME_SOURCE_REFUSED", +]) + +function databaseOperationFailure(error: unknown, fallbackStatus: 400 | 500) { + const message = error instanceof Error ? error.message : String(error) + if (error instanceof Error && "code" in error && typeof error.code === "string" && unavailableCodes.has(error.code)) { + return { status: 503, body: { error: message, code: error.code } } + } + return { status: message.includes("уже выполняется") ? 409 : fallbackStatus, body: { error: message } } +} + export function createSystemDatabaseRoutes(deps: Dependencies): FastifyPluginAsync { return async (app) => { app.addContentTypeParser( "application/octet-stream", @@ -21,31 +38,14 @@ export function createSystemDatabaseRoutes(deps: Dependencies): FastifyPluginAsy app.get("/system/database/backup", async (_req, reply) => { try { - const { filename, buffer, ...warnings } = await deps.exportBackup() - let eventError: string | undefined - try { - await deps.event({ - level: "info", - eventType: "system.database.backup", - sourceModule: "system", - title: "Создан бэкап базы приложения", - message: filename, - entityType: "system_database", - entityId: filename, - payload: { - filename, - sizeBytes: buffer.length, - }, - }) - } catch { eventError = "Не удалось записать событие резервного копирования" } - const outcome = { ...warnings, ...(eventError ? { eventError } : {}) } + const { filename, buffer, ...outcome } = await exportSystemDatabaseBackupWithEvent(undefined, { exportBackup: deps.exportBackup, event: deps.event }) reply.header("X-System-Database-Outcome", encodeURIComponent(JSON.stringify(outcome))) reply.header("Content-Type", "application/octet-stream") reply.header("Content-Disposition", `attachment; filename="${filename}"`) return reply.send(buffer) } catch (err) { - const message = err instanceof Error ? err.message : String(err) - return reply.status(message.includes("уже выполняется") ? 409 : 500).send({ error: message }) + const { status, body } = databaseOperationFailure(err, 500) + return reply.status(status).send(body) } }) @@ -56,14 +56,17 @@ export function createSystemDatabaseRoutes(deps: Dependencies): FastifyPluginAsy } try { const outcome = await deps.restoreBackup(body) + const completionPending = Boolean(outcome.maintenanceError || outcome.migrationError) let eventError: string | undefined try { await deps.event({ level: "warning", eventType: "system.database.restore", sourceModule: "system", - title: "Восстановлена база приложения", - message: "Данные PostgreSQL заменены из загруженного дампа", + title: completionPending ? "Завершение восстановления базы требует проверки" : "Восстановлена база приложения", + message: completionPending + ? "Данные PostgreSQL заменены из дампа. Завершение обслуживания требует проверки." + : "Данные PostgreSQL заменены из загруженного дампа", entityType: "system_database", entityId: "restore", payload: { @@ -73,9 +76,8 @@ export function createSystemDatabaseRoutes(deps: Dependencies): FastifyPluginAsy } catch { eventError = "Не удалось записать событие восстановления" } return reply.send({ ok: true, ...outcome, ...(eventError ? { eventError } : {}) }) } catch (err) { - const message = err instanceof Error ? err.message : String(err) - const status = message.includes("уже выполняется") ? 409 : 400 - return reply.status(status).send({ error: message }) + const { status, body } = databaseOperationFailure(err, 400) + return reply.status(status).send(body) } }) } diff --git a/backend/src/routes/traffic-flow-purge.ts b/backend/src/routes/traffic-flow-purge.ts index 7251cf77..7fd2f0dd 100644 --- a/backend/src/routes/traffic-flow-purge.ts +++ b/backend/src/routes/traffic-flow-purge.ts @@ -1,17 +1,15 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import type { FlowPurgeDto } from "@mmapp/contracts/traffic-flow" +import { completeTrafficFlowPurge } from "../services/traffic-flow-purge-execution.js" export function createTrafficFlowPurgeRoutes(deps: { purge: () => Promise; record: (result: FlowPurgeDto) => Promise }): FastifyPluginAsyncZod { return async app => { app.post("/traffic/flow/purge", async (_req, reply) => { try { - const result = await deps.purge() - try { await deps.record(result) } catch (error) { - return { ...result, eventError: error instanceof Error ? error.message : String(error) } - } - return result + return await completeTrafficFlowPurge(deps) } catch (error) { const message = error instanceof Error ? error.message : String(error) - return reply.status(message.includes("уже выполняется") ? 409 : 500).send({ error: message }) + const statusCode=(error as{statusCode?:number}|null)?.statusCode + return reply.status(typeof statusCode==="number"&&statusCode>=400&&statusCode<=599?statusCode:message.includes("уже выполняется") ? 409 : 500).send({ error: message }) } }) } diff --git a/backend/src/routes/traffic-flow-rebuild.test.ts b/backend/src/routes/traffic-flow-rebuild.test.ts new file mode 100644 index 00000000..fdaed9e6 --- /dev/null +++ b/backend/src/routes/traffic-flow-rebuild.test.ts @@ -0,0 +1,87 @@ +import assert from "node:assert/strict" +import Fastify from "fastify" +import { SignJWT } from "jose" +import { createTrafficFlowRebuildRoutes } from "./traffic-flow-rebuild.js" + +process.env.AUTH_REQUIRED="true" +process.env.AUTH_JWT_SECRET="flow-rebuild-route-test-secret" +process.env.AUTH_ISSUER="https://auth.test.local" +process.env.NODE_ENV="test" +const {default:authPlugin}=await import("../plugins/auth.js") +const app=Fastify() +await app.register(authPlugin) +let mode="normal",rebuilds=0,drains=0,catalogReads=0,claimsChecks=0,disposed=0 +let controller=new AbortController(),injectedError:unknown +let serviceReturned=false +const order:string[]=[] +await app.register(createTrafficFlowRebuildRoutes({ + authRequired:()=>true, + verifyJwt:async request=>{await request.jwtVerify();return request.user}, + assertClaimsCurrent:async actor=>{ + assert.equal(actor.id,"fixture");claimsChecks++ + if(mode==="post-commit-auth"&&serviceReturned)throw Object.assign(Error("NEVER_PORTAL_PROVIDER_TEXT"),{code:"OWNER_CLAIMS_REVOKED"}) + if(mode==="late-auth"&&claimsChecks>=3)throw Object.assign(Error("NEVER_PORTAL_PROVIDER_TEXT"),{code:"OWNER_CLAIMS_REVOKED"}) + }, + catalog:async()=>{catalogReads++;order.push("catalog");return mode==="bad-scope"?[1,1]:[1,2]}, + cancellation:()=>{controller=new AbortController();return {signal:controller.signal,dispose:()=>{disposed++}}}, + drainExisting:async(authorize,ids)=>{assert.deepEqual([...ids],[1,2]);await authorize();order.push("drain");drains++}, + rebuild:async authority=>{ + rebuilds++;order.push("service");assert.deepEqual([...authority.serverIds],[1,2]) + if(mode==="cancel")controller.abort() + await authority.authorize() + if(mode==="error")throw injectedError + await authority.drainExisting(()=>authority.authorize(),authority.serverIds) + if(mode==="late-cancel")controller.abort() + serviceReturned=true + return {ok:true,buckets:3,facts:2,days:["2026-10-24"],jobId:"NEVER_PRIVATE_JOB",configurationProof:"NEVER_PRIVATE_PROOF"} + }, +}),{prefix:"/api"}) +const token=async(permission="mm:traffic:write",apps=["mm"],expiration="1h")=>new SignJWT({sub:"fixture",apps,permissions:[permission]}).setProtectedHeader({alg:"HS256"}).setIssuer("https://auth.test.local").setIssuedAt().setExpirationTime(expiration).sign(new TextEncoder().encode("flow-rebuild-route-test-secret")) +const headers={authorization:"Bearer "+await token()} +const request=async()=>{claimsChecks=0;serviceReturned=false;return app.inject({method:"POST",url:"/api/traffic/flow/rebuild-facts",headers})} +try{ + const success=await request() + assert.equal(success.statusCode,200) + assert.deepEqual(success.json(),{ok:true,buckets:3,facts:2,days:["2026-10-24"]}) + assert.equal(rebuilds,1);assert.equal(drains,1);assert.deepEqual(order,["catalog","service","drain"]) + assert.ok(claimsChecks>=5,"current portal claims are checked through drain and before publication") + assert.equal(disposed,1) + for(const denied of [undefined,await token("mm:traffic:read"),await token("mm:traffic:write",["other"]),await token("mm:traffic:write",["mm"],"-1s")]){ + const before={rebuilds,drains,catalogReads} + const response=await app.inject({method:"POST",url:"/api/traffic/flow/rebuild-facts",headers:denied?{authorization:"Bearer "+denied}:{}}) + assert.ok([401,403].includes(response.statusCode));assert.equal(rebuilds,before.rebuilds);assert.equal(drains,before.drains);assert.equal(catalogReads,before.catalogReads) + } + mode="bad-scope";let before={rebuilds,drains} + assert.equal((await request()).statusCode,409);assert.equal(rebuilds,before.rebuilds);assert.equal(drains,before.drains) + for(const failure of ["late-auth","cancel"]){ + mode=failure;before={rebuilds,drains};const response=await request() + assert.equal(response.statusCode,failure==="late-auth"?401:499);assert.equal(rebuilds,before.rebuilds+1);assert.equal(drains,before.drains) + assert.ok(!response.body.includes("NEVER_")) + } + for(const failure of ["late-cancel","post-commit-auth"]){ + mode=failure;before={rebuilds,drains};const late=await request() + assert.equal(late.statusCode,503);assert.equal(late.json().code,"FLOW_COMMIT_UNKNOWN") + assert.equal(rebuilds,before.rebuilds+1);assert.equal(drains,before.drains+1) + assert.ok(!/NEVER_|buckets|facts|days|configurationProof/.test(late.body),"post-commit authority failure publishes no counts/private proofs and must not encourage replay") + assert.match(late.json().error,/Не повторяйте/) + } + for(const [error,status,code] of [ + [Object.assign(Error("NEVER_SQL_PROVIDER password=secret"),{code:"FLOW_COMMIT_UNKNOWN",configurationProof:"NEVER_PRIVATE_PROOF"}),503,"FLOW_COMMIT_UNKNOWN"], + [Object.assign(Error("NEVER_SQL_PROVIDER"),{code:"PROTECTED_OBJECT"}),409,"PROTECTED_OBJECT"], + [Object.assign(Error("NEVER_SQL_PROVIDER"),{code:"APPROVAL_REQUIRED"}),409,"APPROVAL_REQUIRED"], + [Error("Пересчёт или сброс NetFlow уже выполняется"),409,"FLOW_BUSY"], + [Error("Flow writers are busy; rebuild was not started"),409,"FLOW_BUSY"], + [Error("Strict drain refuses a previously captured batch with global prune; resolve it through normal owned writer first"),409,"FLOW_BUSY"], + [Error("Owned flow batch cannot dispatch: unknown"),503,"FLOW_COMMIT_UNKNOWN"], + [Error("Worker drain outcome is unconfirmed"),503,"FLOW_COMMIT_UNKNOWN"], + [Error("Fact rebuild configuration changed"),409,"SOURCE_CHANGED"], + [Object.assign(Error("NEVER_SQL_PROVIDER"),{code:"STATE_UNAVAILABLE"}),503,"STATE_UNAVAILABLE"], + [Error("NEVER_SQL_PROVIDER password=secret"),500,"FLOW_REBUILD_FAILED"], + ] as const){ + mode="error";injectedError=error;const response=await request() + assert.equal(response.statusCode,status);assert.equal(response.json().code,code) + assert.ok(!/NEVER_|password|secret/.test(response.body),"only fixed public errors may leave the route") + } + assert.equal(disposed,1+4+1+2+2+11,"all request paths dispose cancellation ownership") + console.log("REST rebuild factory: actual JWT verification, full catalog before single shared executor/drain, public DTO only/no event writer, repeated portal checks, late auth/cancel gates and safe unknown/busy/protection/internal errors; Fastify injection only, no SQL/network/collectors") +}finally{await app.close()} diff --git a/backend/src/routes/traffic-flow-rebuild.ts b/backend/src/routes/traffic-flow-rebuild.ts new file mode 100644 index 00000000..cb9388c4 --- /dev/null +++ b/backend/src/routes/traffic-flow-rebuild.ts @@ -0,0 +1,80 @@ +import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" +import type { FastifyReply,FastifyRequest } from "fastify" +import type { PoolClient } from "pg" +import { hasPermission,type AuthUser } from "../lib/permissions.js" +import type { FactRebuildAuthority } from "../services/traffic-flow-facts-rebuild-service.js" + +type Claims={sub?:unknown;apps?:unknown;permissions?:unknown;exp?:unknown;iat?:unknown} +type PublicResult={ok:true;buckets:number;facts:number;days:string[]} +type Cancellation={signal:AbortSignal;dispose:()=>void} +export interface TrafficFlowRebuildRouteDependencies { + authRequired:()=>boolean + verifyJwt:(request:FastifyRequest)=>Promise + assertClaimsCurrent:(actor:AuthUser,client?:PoolClient)=>Promise + catalog:()=>Promise + rebuild:(authority:FactRebuildAuthority)=>Promise + drainExisting:FactRebuildAuthority["drainExisting"] + cancellation?:(request:FastifyRequest,reply:FastifyReply)=>Cancellation +} +const failure=(code:string)=>Object.assign(new Error(code),{code}) +function requestCancellation(request:FastifyRequest,reply:FastifyReply):Cancellation { + const controller=new AbortController() + const abort=()=>controller.abort() + const close=()=>{if(!reply.raw.writableEnded)abort()} + request.raw.on("aborted",abort);reply.raw.on("close",close) + if(request.raw.aborted)abort() + return {signal:controller.signal,dispose:()=>{request.raw.off("aborted",abort);reply.raw.off("close",close)}} +} +function safeError(error:unknown):{status:number;body:{error:string;code:string}} { + const code=(error as {code?:unknown}|null)?.code + const message=error instanceof Error?error.message:"" + if(code==="FLOW_COMMIT_UNKNOWN"||code==="MUTATION_OUTCOME_UNKNOWN"||message==="Owned flow batch cannot dispatch: unknown"||message==="Worker drain outcome is unconfirmed"||message==="Worker drain deadline exceeded; no collector restart"||message==="Existing worker exited before drain acknowledgement")return {status:503,body:{code:"FLOW_COMMIT_UNKNOWN",error:"Результат пересчёта не подтверждён. Не повторяйте запрос; требуется сверка."}} + if(code==="READ_CANCELLED")return {status:499,body:{code:"READ_CANCELLED",error:"Запрос пересчёта отменён."}} + if(code==="UNAUTHORIZED"||code==="OWNER_CLAIMS_REVOKED")return {status:401,body:{code:"UNAUTHORIZED",error:"Требуется актуальная авторизация."}} + if(code==="PERMISSION_DENIED"||code==="SERVER_DENIED")return {status:403,body:{code:"PERMISSION_DENIED",error:"Недостаточно прав для пересчёта трафика."}} + if(code==="PROTECTED_OBJECT"||code==="APPROVAL_REQUIRED")return {status:409,body:{code:String(code),error:"Пересчёт затрагивает защищённый объект; требуется проверенный план."}} + if(message==="Пересчёт или сброс NetFlow уже выполняется"||message==="Flow writers are busy; rebuild was not started"||message==="Strict drain refuses a previously captured batch with global prune; resolve it through normal owned writer first")return {status:409,body:{code:"FLOW_BUSY",error:"Пересчёт или сброс NetFlow уже выполняется."}} + if(code==="SOURCE_CHANGED"||message==="Fact rebuild configuration changed"||message==="Rebuild configuration requires the exact current server catalog")return {status:409,body:{code:"SOURCE_CHANGED",error:"Каталог или настройки изменились. Подготовьте пересчёт заново."}} + if(code==="STATE_UNAVAILABLE")return {status:503,body:{code:"STATE_UNAVAILABLE",error:"Проверка доступа или состояния пересчёта недоступна."}} + return {status:500,body:{code:"FLOW_REBUILD_FAILED",error:"Не удалось подтвердить пересчёт фактов трафика."}} +} + +/** Existing REST endpoint delegates the complete write/event/receipt scenario. + * This factory has no event writer or collector lifecycle dependency. */ +export function createTrafficFlowRebuildRoutes(deps:TrafficFlowRebuildRouteDependencies):FastifyPluginAsyncZod { + return async app=>{ + app.post("/traffic/flow/rebuild-facts",async(request,reply)=>{ + const cancellation=(deps.cancellation??requestCancellation)(request,reply) + let mayHaveCommitted=false + let principal:string|undefined + const checkCancelled=()=>{if(cancellation.signal.aborted)throw failure("READ_CANCELLED")} + const authorize=async(client?:PoolClient)=>{ + checkCancelled() + if(deps.authRequired()){ + let claims:Claims + try{claims=await deps.verifyJwt(request)}catch{throw failure("UNAUTHORIZED")} + checkCancelled() + const id=typeof claims.sub==="string"?claims.sub.trim():"" + if(!id||id.length>200||(principal!==undefined&&principal!==id)||(typeof claims.exp==="number"&&(!Number.isFinite(claims.exp)||claims.exp*1000<=Date.now())))throw failure("UNAUTHORIZED") + const apps=Array.isArray(claims.apps)?claims.apps.map(String):[],permissions=Array.isArray(claims.permissions)?claims.permissions.map(String):[] + if(!apps.includes("mm")||!hasPermission(permissions,"mm:traffic:write"))throw failure("PERMISSION_DENIED") + principal=id + await deps.assertClaimsCurrent({id,email:"",name:"",apps,permissions,expiresAt:typeof claims.exp==="number"?claims.exp*1000:undefined,issuedAt:typeof claims.iat==="number"?claims.iat*1000:undefined},client) + checkCancelled() + } + } + try{ + await authorize() + const serverIds=[...await deps.catalog()] + if(serverIds.length>1000||new Set(serverIds).size!==serverIds.length||serverIds.some(id=>!Number.isSafeInteger(id)||id<=0))throw failure("SOURCE_CHANGED") + await authorize() + const result=await deps.rebuild({serverIds,authorize,drainExisting:deps.drainExisting}) + mayHaveCommitted=true + await authorize() + // Explicit public DTO excludes job IDs, source/classifier proofs and receipts. + return reply.send({ok:result.ok,buckets:result.buckets,facts:result.facts,days:[...result.days]}) + }catch(error){const safe=safeError(mayHaveCommitted?failure("FLOW_COMMIT_UNKNOWN"):error);return reply.status(safe.status).send(safe.body)} + finally{cancellation.dispose()} + }) + } +} diff --git a/backend/src/routes/traffic-flow-settings-shared.test.ts b/backend/src/routes/traffic-flow-settings-shared.test.ts new file mode 100644 index 00000000..369472d9 --- /dev/null +++ b/backend/src/routes/traffic-flow-settings-shared.test.ts @@ -0,0 +1,31 @@ +import assert from "node:assert/strict" +import Fastify from "fastify" +import { createTrafficFlowSettingsRoutes } from "./traffic-flow-settings.js" +import { createTrafficFlowSettingsService } from "../services/traffic-flow-settings-service.js" +import type { FlowSettingsConfiguration } from "../services/traffic-flow-settings-execution.js" +import type { TrafficFlowSettingsDto } from "@mmapp/contracts/traffic-flow" +let current: FlowSettingsConfiguration = {id:1,enabled:false,topN:200,hostPublicKey:"public",hostPrivateKey:"PRIVATE",future:"preserved"} +let starts=0,writes=0,fail=false,legacy=0 +const phases:string[]=[] +const execute = createTrafficFlowSettingsService({ + exclusive:async work=>work(),guard:async work=>work(),agentPinned:()=>false,proof:async()=>"a".repeat(64), + store:()=>({read:async()=>structuredClone(current),claim:async(_id,phase)=>{phases.push(phase)},compareAndSet:async(_id,_proof,_before,desired)=>{writes++;current=structuredClone(desired);return true},complete:async()=>{}}), + startListener:async fence=>{await fence?.();starts++;if(fail)throw Error("PRIVATE unexpected raw error")}, +}) +const app=Fastify() +await app.register(createTrafficFlowSettingsRoutes({ + read:async()=>({enabled:current.enabled,topN:current.topN} as TrafficFlowSettingsDto),updateWithListener:execute, + update:async()=>{legacy++},startListener:async()=>{legacy++},ensureKeys:async()=>({publicKey:"public",created:false}),hasPrivateKey:async()=>true,hostFiles:async()=>[], +})) +for(const payload of [{enabled:false},{topN:200},{mapServiceMinSharePct:0}]){ + const response=await app.inject({method:"PUT",url:"/traffic/flow/settings",payload}) + assert.equal(response.statusCode,200);assert.equal(response.json().ok,true) +} +fail=true +const partial=await app.inject({method:"PUT",url:"/traffic/flow/settings",payload:{topN:100}}) +assert.equal(partial.statusCode,200);assert.equal(partial.json().settings.topN,100);assert.ok(partial.json().listenerError) +assert.ok(!partial.body.includes("PRIVATE"));assert.equal(current.future,"preserved") +assert.equal(starts,4);assert.equal(writes,4);assert.equal(legacy,0) +assert.deepEqual(phases,["configuration","listener","configuration","listener","configuration","listener","configuration","listener"]) +await app.close() +console.log("NetFlow PUT uses shared full settings executor once, no-op/display restart and saved configuration HTTP200 sanitized listenerError; injected no worker/SQL") diff --git a/backend/src/routes/traffic-flow-settings.ts b/backend/src/routes/traffic-flow-settings.ts index 49c9eefc..44fcb43e 100644 --- a/backend/src/routes/traffic-flow-settings.ts +++ b/backend/src/routes/traffic-flow-settings.ts @@ -3,8 +3,9 @@ import { trafficFlowSettingsPatchSchema, type TrafficFlowSettingsDto, type Traff type Dependencies = { read: () => Promise - update: (patch: TrafficFlowSettingsPatch) => Promise - startListener: () => Promise + update?: (patch: TrafficFlowSettingsPatch) => Promise + startListener?: () => Promise + updateWithListener?: (patch: TrafficFlowSettingsPatch) => Promise<{ listenerError?: string }> ensureKeys: () => Promise<{ publicKey: string; created: boolean }> hasPrivateKey: () => Promise hostFiles: () => Promise @@ -17,10 +18,15 @@ export function createTrafficFlowSettingsRoutes(deps: Dependencies): FastifyPlug app.put("/traffic/flow/settings", async (req, reply) => { const parsed = patchSchema.safeParse(req.body) if (!parsed.success) return reply.status(400).send({ error: "Укажите допустимые настройки NetFlow", details: parsed.error.flatten() }) - await deps.update(parsed.data) let listenerError: string | undefined - try { await deps.startListener() } catch (error) { - listenerError = error instanceof Error ? error.message : String(error) + if (deps.updateWithListener) { + listenerError = (await deps.updateWithListener(parsed.data)).listenerError + } else { + if (!deps.update || !deps.startListener) throw Error("NetFlow settings executor is missing") + await deps.update(parsed.data) + try { await deps.startListener() } catch (error) { + listenerError = error instanceof Error ? error.message : String(error) + } } // The listener starts asynchronously. This receipt confirms persistence, not UDP readiness. return { ok: true, settings: await deps.read(), ...(listenerError !== undefined ? { listenerError } : {}) } diff --git a/backend/src/routes/traffic-flow.ts b/backend/src/routes/traffic-flow.ts index f2eba30e..f0e3b72e 100644 --- a/backend/src/routes/traffic-flow.ts +++ b/backend/src/routes/traffic-flow.ts @@ -1,4 +1,10 @@ +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 { 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" import { registerDatabaseStreamAbort } from "../plugins/database-request-gate.js" import type { FastifyReply, FastifyRequest } from "fastify" import { env } from "../config.js" @@ -7,21 +13,24 @@ import { trafficFlowOverlayRequestSchema, } from "@mmapp/contracts/traffic-flow" import { createTrafficFlowPurgeRoutes } from "./traffic-flow-purge.js" +import { createTrafficFlowRebuildRoutes } from "./traffic-flow-rebuild.js" +import { pool } from "../db/index.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { rebuildFlowFactsSafely } from "../services/traffic-flow-facts-rebuild-service.js" +import { recordTrafficFlowPurge } from "../services/flow-purge-service.js" import { createTrafficFlowSettingsRoutes } from "./traffic-flow-settings.js" import { createTrafficFlowHostRoutes } from "./traffic-flow-host.js" import { getFlowHostStatus, reconcileFlowHost, withFlowConfigurationLock } from "../services/traffic-flow-host-agent.js" -import { agentSocket } from "../services/traffic-flow-agent-client.js" import { ensureHostKeys, getTrafficFlowSettingsRow, toTrafficFlowSettingsDto, - updateTrafficFlowSettings, } from "../services/traffic-flow-settings.js" import { getFlowListenerState, + drainExistingTrafficFlowWriters, getFlowRuntimeCounters, purgeTrafficFlowStore, - startTrafficFlowListener, listFlowTalkers, } from "../services/traffic-flow-ingest.js" import { @@ -34,26 +43,12 @@ import { import { buildFlowMapHops } from "../services/traffic-flow-map-hops.js" import { applyFlowOverlay } from "../services/traffic-flow-overlay.js" import { listTrafficFlowHostFiles } from "../services/traffic-flow-host-files.js" -import { rebuildFlowFactsFromBuckets } from "../services/traffic-flow-facts-rebuild.js" -import { appendEvent } from "../modules/events/service/events-service.js" -const LIVE_TICK_MS = 2000 -export const MAX_FLOW_LIVE_SUBSCRIBERS = 4 -let liveSubscribers = 0 +import { FLOW_LIVE_TICK_MS as LIVE_TICK_MS, tryAcquireFlowLiveSlot, releaseFlowLiveSlot } from "../services/traffic-flow-live-slots.js" +export { MAX_FLOW_LIVE_SUBSCRIBERS, tryAcquireFlowLiveSlot, releaseFlowLiveSlot, resetFlowLiveSlotsForTests } from "../services/traffic-flow-live-slots.js" -export function tryAcquireFlowLiveSlot(): boolean { - if (liveSubscribers >= MAX_FLOW_LIVE_SUBSCRIBERS) return false - liveSubscribers += 1 - return true -} - -export function releaseFlowLiveSlot(): void { - liveSubscribers = Math.max(0, liveSubscribers - 1) -} - -export function resetFlowLiveSlotsForTests(): void { - liveSubscribers = 0 -} +export const flowSseDiagnostics = channel("mmapp.netflow.sse") +type FlowSseStage = "open" | "build-start" | "build-ready" | "write-start" | "write-complete" | "closed" function rangeToMinutes(range: string | undefined): number { switch ((range ?? "5m").toLowerCase()) { @@ -131,9 +126,6 @@ async function applyOverlayHandler(req: FastifyRequest, reply: FastifyReply) { } } -function writeSse(raw: NodeJS.WritableStream, event: string, data: unknown) { - raw.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`) -} function sleep(ms: number, signal: AbortSignal): Promise { return new Promise((resolve, reject) => { @@ -154,6 +146,22 @@ function sleep(ms: number, signal: AbortSignal): Promise { } const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => { + const snapshotReads=new Set(["/traffic/flow","/traffic/flows","/traffic/flow/exporters","/traffic/flow/clients","/traffic/flow/analytics","/traffic/flow/map-hops","/traffic/flow/monthly","/traffic/flow/live"]) + app.addHook("onRoute",options=>{ + // Prefix-independent exact path suffix; only this plugin's snapshot GETs. + if(![options.method].flat().includes("GET") || ![...snapshotReads].some(path=>options.url.endsWith(path)))return + const original=options.handler + options.handler=async function(request,reply){ + const owner=createFlowReadResponseOwner(reply.raw,flowReadBudget,flowResponseReadLimits) + return owner.run(FLOW_READ_RESERVATION_BYTES,async()=>await original.call(this,request,reply)) + } + }) + app.addHook("onSend",async(request,_reply,payload)=>{ + if(request.method==="GET" && [...snapshotReads].some(path=>request.routeOptions.url?.endsWith(path)) && + (typeof payload==="string"||Buffer.isBuffer(payload)) && Buffer.byteLength(payload)>FLOW_SSE_MAX_FRAME_BYTES) + throw Object.assign(Error("Ответ NetFlow превышает допустимый объём; сократите область или окно"),{statusCode:409}) + return payload + }) await app.register(createTrafficFlowHostRoutes({ status: getFlowHostStatus, reconcile: () => withFlowConfigurationLock(reconcileFlowHost), @@ -161,66 +169,28 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => { await app.register(createTrafficFlowSettingsRoutes({ read: async () => { const settings = await toTrafficFlowSettingsDto(getFlowListenerState()) - const { packetsReceived, lastExporterIp, lastError, lastDatagramAt } = await getFlowRuntimeCounters() - return { ...settings, packetsReceived, lastExporterIp, lastError, lastDatagramAt } + const { packetsReceived, lastExporterIp, lastError, lastDatagramAt,dataQuality } = await getFlowRuntimeCounters() + return { ...settings, packetsReceived, lastExporterIp, lastError, lastDatagramAt,dataQuality } }, - update: async patch => { await withFlowConfigurationLock(async () => { - const current = await getTrafficFlowSettingsRow() - if (agentSocket() && (["collectorIp", "prefix", "flowListenPort", "wgListenPort"] as const).some(key => patch[key] !== undefined && patch[key] !== current[key])) { - throw Object.assign(new Error("Адрес, префикс и порты закреплены установкой агента; повторите установщик хоста"), { statusCode: 409 }) - } - await updateTrafficFlowSettings(patch) - }) }, - startListener: startTrafficFlowListener, - ensureKeys: () => withFlowConfigurationLock(ensureHostKeys), + updateWithListener: updateTrafficFlowSettingsWithListener, + ensureKeys: ensureHostKeys, hasPrivateKey: async () => Boolean((await getTrafficFlowSettingsRow()).hostPrivateKey), hostFiles: listTrafficFlowHostFiles, })) await app.register(createTrafficFlowPurgeRoutes({ purge: purgeTrafficFlowStore, - record: async result => { - await appendEvent({ - level: "warning", - eventType: "traffic.flow.purge", - sourceModule: "traffic", - title: "Сброшены данные NetFlow", - message: `Удалены сессии ${result.deleted.buckets}, minute ${result.deleted.minuteStats}, daily ${result.deleted.dailyDims}`, - entityType: "traffic_flow", - entityId: "purge", - payload: { - buckets: result.deleted.buckets, - minuteStats: result.deleted.minuteStats, - minuteDims: result.deleted.minuteDims, - dailyDims: result.deleted.dailyDims, - fileBytesBefore: result.fileBytesBefore, - fileBytesAfter: result.fileBytesAfter, - vacuumed: result.vacuumed, - }, - }) - }, + record: recordTrafficFlowPurge, })) - app.post("/traffic/flow/rebuild-facts", async (_req, reply) => { - try { - const result = await rebuildFlowFactsFromBuckets() - await appendEvent({ - level: "info", - eventType: "traffic.flow.rebuild_facts", - sourceModule: "traffic", - title: "Пересчитан куб NetFlow", - message: `Факты ${result.facts} из ${result.buckets} сессий, дней ${result.days.length}`, - entityType: "traffic_flow", - entityId: "rebuild-facts", - payload: { buckets: result.buckets, facts: result.facts, days: result.days }, - }) - return reply.send(result) - } catch (err) { - const message = err instanceof Error ? err.message : String(err) - const status = message.includes("уже выполняется") ? 409 : 500 - return reply.status(status).send({ error: message }) - } - }) + await app.register(createTrafficFlowRebuildRoutes({ + authRequired:()=>env.authRequired, + verifyJwt:async request=>{await request.jwtVerify();return request.user}, + assertClaimsCurrent:assertPortalClaimsCurrent, + catalog:async()=>(await pool.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>Number(row.id)), + rebuild:rebuildFlowFactsSafely, + drainExisting:drainExistingTrafficFlowWriters, + })) app.post("/traffic/flow/overlay", applyOverlayHandler) app.post("/traffic/flow-overlay", applyOverlayHandler) @@ -257,9 +227,6 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => { app.get("/traffic/flow/live", async (req, reply) => { const query = analyticsQuery(req) - if (!tryAcquireFlowLiveSlot()) { - return reply.status(429).send({ error: "Слишком много live-подписок" }) - } const liveQuery = { serverId: query.serverId, userId: query.userId, @@ -270,8 +237,16 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => { } const abort = new AbortController() const onClose = () => abort.abort() - registerDatabaseStreamAbort(req, onClose) - req.raw.on("close", onClose) + registerDatabaseStreamAbort(req, () => { + onClose() + if (reply.raw.headersSent) reply.raw.destroy() + }) + if (abort.signal.aborted) return reply.code(503).send({ error: "Выполняется операция с базой приложения" }) + if (reply.raw.destroyed) return + if (!tryAcquireFlowLiveSlot()) { + return reply.status(429).send({ error: "Слишком много live-подписок" }) + } + reply.raw.on("close", onClose) reply.hijack() req.raw.setTimeout(0) @@ -284,6 +259,12 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => { Connection: "keep-alive", "X-Accel-Buffering": "no", } + // A diagnostic subscriber can correlate its timestamps with the same stream in Edge. + const diagnosticId = flowSseDiagnostics.hasSubscribers ? randomUUID() : undefined + const trace = diagnosticId === undefined ? undefined : (stage: FlowSseStage) => { + flowSseDiagnostics.publish({ streamId: diagnosticId, stage, atUnixMs: Date.now(), monotonicMs: performance.now() }) + } + if (diagnosticId !== undefined) sseHeaders["X-MM-NetFlow-Stream"] = diagnosticId if (origin && (allowed === "*" || allowed === origin)) { sseHeaders["Access-Control-Allow-Origin"] = origin sseHeaders["Access-Control-Allow-Credentials"] = "true" @@ -291,19 +272,33 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => { sseHeaders.Vary = "Origin" } reply.raw.writeHead(200, sseHeaders) + trace?.("open") reply.raw.write(":\n\n") try { while (!abort.signal.aborted) { - const payload = await safeBuildLiveFlowSample(liveQuery) - writeSse(reply.raw, payload.event, payload.data) + await withFlowReadSourceWindow(async () => { + trace?.("build-start") + const payload = await withDatabaseApiRequest(async () => { + abort.signal.throwIfAborted() + const sample = await safeBuildLiveFlowSample(liveQuery) + mcpDatabaseRequestSignal()?.throwIfAborted() + return sample + }) + abort.signal.throwIfAborted() + trace?.("build-ready") + trace?.("write-start") + await writeBoundedFlowSse(reply.raw, payload.event, payload.data, FLOW_SSE_MAX_FRAME_BYTES) + trace?.("write-complete") + }) await sleep(LIVE_TICK_MS, abort.signal) } } catch { /* abort / disconnect */ } finally { + trace?.("closed") releaseFlowLiveSlot() - req.raw.off("close", onClose) + reply.raw.off("close", onClose) try { reply.raw.end() } catch { diff --git a/backend/src/routes/traffic.ts b/backend/src/routes/traffic.ts index a31079b4..7c2a78ae 100644 --- a/backend/src/routes/traffic.ts +++ b/backend/src/routes/traffic.ts @@ -1,70 +1,30 @@ import { env } from "../config.js" +import { mcpDatabaseRequestSignal, withDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" +import { McpError } from "../modules/mcp/security.js" import { registerDatabaseStreamAbort } from "../plugins/database-request-gate.js" -import { desc, eq } from "drizzle-orm" +import { eq } from "drizzle-orm" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { db } from "../db/index.js" -import { serverSnapshots, servers } from "../db/schema.js" +import { servers } from "../db/schema.js" import { - getTrafficCollectorState, - getTrafficSettings, - readServerSamplesInRange, + readPublicTrafficSettings, + toTrafficSettingsDto, updateTrafficSettings, } from "../services/traffic-collector.js" -import { refreshScheduler, runSchedulerJobNow } from "../services/scheduler.js" -import { appendEvent } from "../modules/events/service/events-service.js" +import { refreshScheduler } from "../services/scheduler.js" +import { collectTrafficManually } from "../services/manual-traffic-collection.js" import { readTrafficLiveSample, TrafficLiveSourceChanged } from "../services/traffic-live.js" import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" import { getEnabledServerById } from "../services/wireguard-live.js" import { readTrafficCollectionContext } from "../services/traffic-collection-state.js" -import { applyTrafficFreshness } from "../services/traffic-freshness.js" -import { - buildTrafficFromSamples, - type TrafficObservations, - isLoopbackName, -} from "../services/traffic-rate.js" +import { readServerTrafficHistory, readServerTrafficInterfaces } from "../services/traffic-history-read.js" import { buildBoundInterfaceTraffic, buildUserTrafficList, } from "../services/traffic-users.js" -interface TrafficServerDto { - volumeUnit: "GiB" - observations?:TrafficObservations - id: string - name: string - site: string - country: string - status: "online" | "offline" | "degraded" - rxNow: number - txNow: number - rxPeak: number - txPeak: number - rxTotal: number - txTotal: number - sessions: number - rxSeries: number[] - txSeries: number[] -} - -interface TrafficInterfaceDto { - name: string - running: boolean - disabled: boolean - rxNow: number | null - txNow: number | null -} - const LIVE_TICK_MS = 1500 -async function latestSnapshot(serverId: number) { - return (await db - .select() - .from(serverSnapshots) - .where(eq(serverSnapshots.serverId, serverId)) - .orderBy(desc(serverSnapshots.polledAt)) - .limit(1))[0] -} - function rangeToMinutes(range: string | undefined): number { switch ((range ?? "1h").toLowerCase()) { case "5m": return 5 @@ -76,50 +36,6 @@ function rangeToMinutes(range: string | undefined): number { } } -function buildServerTraffic( - s: typeof servers.$inferSelect, - status: TrafficServerDto["status"], - rows: Array<{ - interfaceName: string - sampledAt: string - rxBps: number - txBps: number - rxBytes: number - txBytes: number - running: boolean - disabled: boolean - }>, - rangeStartMs: number, - rangeEndMs: number, - onlyInterface?: string, - context?: Awaited>, -): TrafficServerDto { - const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, onlyInterface,undefined,true) - return { - volumeUnit: "GiB", - id: String(s.id), - name: s.name || s.host, - site: s.site || "—", - country: s.country || "UN", - status, - observations:built.observations && applyTrafficFreshness(built.observations, s, context?.settings, context?.states.get(s.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, - } -} - -async function snapshotStatus(serverId: number): Promise { - const snap = await latestSnapshot(serverId) - return snap?.status === "offline" ? "offline" : (snap?.status === "online" ? "online" : "degraded") -} - function writeSse(raw: NodeJS.WritableStream, event: string, data: unknown) { raw.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`) } @@ -144,17 +60,7 @@ function sleep(ms: number, signal: AbortSignal): Promise { const trafficRoutes: FastifyPluginAsyncZod = async (app) => { app.get("/traffic/settings", async (_req, reply) => { - const settings = await getTrafficSettings() - const state = await getTrafficCollectorState() - return reply.send({ - enabled: settings.enabled, - intervalSec: settings.intervalSec, - retentionDays: settings.retentionDays, - lastCollectedAt: settings.lastCollectedAt ?? null, - lastDurationMs: settings.lastDurationMs ?? null, - lastError: settings.lastError || null, - collectorRunning: state.running, - }) + return reply.send(await readPublicTrafficSettings()) }) app.put("/traffic/settings", async (req, reply) => { @@ -173,91 +79,42 @@ const trafficRoutes: FastifyPluginAsyncZod = async (app) => { await refreshScheduler() return reply.send({ ok: true, - settings: { - enabled: updated.enabled, - intervalSec: updated.intervalSec, - retentionDays: updated.retentionDays, - lastCollectedAt: updated.lastCollectedAt ?? null, - lastDurationMs: updated.lastDurationMs ?? null, - lastError: updated.lastError || null, - }, + settings: toTrafficSettingsDto(updated), }) }) app.post("/traffic/collect-now", async (_req, reply) => { - try { - const receipt = await runSchedulerJobNow("traffic") - if (!receipt.result || receipt.result.job !== "traffic") return reply.status(502).send({ ok: false, error: receipt.error ?? "Нет подтверждённого результата сбора" }) - const result = receipt.result - if (result.skipped) return reply.status(409).send({ error: "Сбор трафика уже выполняется" }) - const failures = result.servers.filter(s => !s.ok) - if (result.fatalError || failures.length) { - const error = result.fatalError ?? failures.map(s => `${s.name}: ${s.error ?? "Ошибка опроса"}`).join("; ") - await appendEvent({ level: "warning", eventType: "traffic.collect.manual.failed", sourceModule: "traffic", - title: "Сбор трафика завершён с ошибками", message: error }) - return reply.status(502).send({ ok: false, error, failures, succeeded: result.servers.filter(s => s.ok).map(s => s.serverId) }) - } - const updated = await getTrafficSettings() - const warnings = result.servers.filter(s => s.warning).map(s => ({ serverId: s.serverId, warning: s.warning })) - await appendEvent({ - level: warnings.length ? "warning" : "info", - eventType: "traffic.collect.manual.ok", - sourceModule: "traffic", - title: warnings.length ? "Ручной сбор трафика завершён с ограничениями" : "Ручной сбор трафика завершен", - message: `Длительность: ${updated.lastDurationMs ?? 0} мс`, - payload: { - lastCollectedAt: updated.lastCollectedAt ?? null, - }, - }) - return reply.send({ - ok: true, - warnings, - lastCollectedAt: updated.lastCollectedAt ?? null, - lastDurationMs: updated.lastDurationMs ?? null, - lastError: updated.lastError || null, - }) - } catch (error) { - if (typeof error === "object" && error && "statusCode" in error && error.statusCode === 409) - return reply.status(409).send({ error: error instanceof Error ? error.message : "Сбор трафика уже выполняется" }) - const msg = error instanceof Error ? error.message : String(error) - await appendEvent({ - level: "critical", - eventType: "traffic.collect.manual.failed", - sourceModule: "traffic", - title: "Ошибка ручного сбора трафика", - message: msg, - }) - return reply.status(500).send({ error: msg }) - } + const result = await collectTrafficManually() + return reply.status(result.statusCode).send(result.body) }) app.get("/traffic/servers", async (req, reply) => { const q = req.query as { range?: string } - const minutes = rangeToMinutes(q.range) - const rangeEndMs = Date.now() - const rangeStartMs = rangeEndMs - minutes * 60_000 - const sinceIso = new Date(rangeStartMs).toISOString() + 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() - const data = await Promise.all(allServers.map(async (s): Promise => { - const rows = await readServerSamplesInRange(s.id, sinceIso) - return buildServerTraffic(s, await snapshotStatus(s.id), rows, rangeStartMs, rangeEndMs, undefined, context) - })) - + 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 }) }) - app.get("/traffic/servers/:id/live", async (req, reply) => { const p = req.params as { id?: string | number } const q = req.query as { iface?: string } - const server = await getEnabledServerById(p.id ?? "") + const readServer = (id: string | number, signal?: AbortSignal) => withDatabaseApiRequest(async () => { + signal?.throwIfAborted() + const row = await getEnabledServerById(id) + mcpDatabaseRequestSignal()?.throwIfAborted() + return row + }) + const server = await readServer(p.id ?? "") if (!server || !server.enabled) return reply.status(404).send({ error: "Server not found" }) const onlyInterface = q.iface && q.iface !== "__all__" ? q.iface : undefined const abort = new AbortController() const onClose = () => abort.abort() registerDatabaseStreamAbort(req, onClose) - req.raw.on("close", onClose) + if (abort.signal.aborted) return reply.code(503).send({ error: "Выполняется операция с базой приложения" }) + if (reply.raw.destroyed) return + reply.raw.on("close", onClose) reply.hijack() req.raw.setTimeout(0) @@ -286,23 +143,24 @@ const trafficRoutes: FastifyPluginAsyncZod = async (app) => { while (!abort.signal.aborted) { try { const sample = await readTrafficLiveSample(client, onlyInterface, cache, abort.signal, async () => { - const current = await getEnabledServerById(server.id) + const current = await readServer(server.id, abort.signal) if (!current) throw new TrafficLiveSourceChanged("Устройство выключено или удалено. Live-поток остановлен") if (Date.parse(current.updatedAt) !== Date.parse(server.updatedAt)) throw new TrafficLiveSourceChanged("Конфигурация устройства изменилась. Переподключите live-поток") }) + if (abort.signal.aborted) break writeSse(reply.raw, "sample", sample) } catch (error) { if (abort.signal.aborted) break const msg = error instanceof Error ? error.message : String(error) writeSse(reply.raw, "error", { error: msg.slice(0,1024) }) - if (error instanceof TrafficLiveSourceChanged || error instanceof MikrotikError && [401,403].includes(error.statusCode)) break + if (error instanceof TrafficLiveSourceChanged || error instanceof McpError || error instanceof MikrotikError && [401,403].includes(error.statusCode)) break } await sleep(LIVE_TICK_MS, abort.signal) } } catch { /* abort / disconnect */ } finally { - req.raw.off("close", onClose) + reply.raw.off("close", onClose) try { reply.raw.end() } catch { @@ -315,41 +173,10 @@ const trafficRoutes: FastifyPluginAsyncZod = async (app) => { const p = req.params as { id?: string | number } const serverId = Number.parseInt(String(p.id ?? ""), 10) if (!Number.isFinite(serverId)) return reply.status(400).send({ error: "id is required" }) - - const allRows = await db.select().from(servers).where(eq(servers.id, serverId)).limit(1) - const server = allRows[0] + const server = (await db.select().from(servers).where(eq(servers.id, serverId)).limit(1))[0] if (!server) return reply.status(404).send({ error: "Server not found" }) - - const sinceIso = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString() - const rows = await readServerSamplesInRange(serverId, sinceIso) - const context = await readTrafficCollectionContext(), end = Date.now() - const byIface = new Map() - for (const r of rows) { - if (isLoopbackName(r.interfaceName) || r.peerPublicKey) continue - const arr = byIface.get(r.interfaceName) ?? [] - arr.push(r) - byIface.set(r.interfaceName, arr) - } - const interfaces: TrafficInterfaceDto[] = [...byIface.entries()].map(([name, arr]) => { - const sorted = arr.sort((a, b) => a.sampledAt.localeCompare(b.sampledAt)) - const last = sorted[sorted.length - 1] - if (!last) { - return { name, running: false, disabled: true, rxNow: 0, txNow: 0 } - } - const built = buildTrafficFromSamples(sorted, Date.parse(sinceIso), end, name, undefined, true) - const observed = applyTrafficFreshness(built.observations!, server, context.settings, context.states.get(server.id), end) - return { - name, - running: Boolean(last.running), - disabled: Boolean(last.disabled), - rxNow: observed.rxNow ? built.rxNow : null, - txNow: observed.txNow ? built.txNow : null, - } - }).sort((a, b) => ((b.rxNow ?? -1) + (b.txNow ?? -1)) - ((a.rxNow ?? -1) + (a.txNow ?? -1))) - - return reply.send({ interfaces }) + return reply.send({ interfaces: (await readServerTrafficInterfaces(server)).interfaces }) }) - app.get("/traffic/servers/:id", async (req, reply) => { const p = req.params as { id?: string | number } const q = req.query as { range?: string; iface?: string } @@ -357,24 +184,9 @@ const trafficRoutes: FastifyPluginAsyncZod = async (app) => { if (!Number.isFinite(serverId)) return reply.status(400).send({ error: "id is required" }) const server = (await db.select().from(servers).where(eq(servers.id, serverId)).limit(1))[0] if (!server) return reply.status(404).send({ error: "Server not found" }) - - const minutes = rangeToMinutes(q.range) - const rangeEndMs = Date.now() - const rangeStartMs = rangeEndMs - minutes * 60_000 - const sinceIso = new Date(rangeStartMs).toISOString() - const rows = await readServerSamplesInRange(server.id, sinceIso) - const data = buildServerTraffic( - server, - await snapshotStatus(server.id), - rows, - rangeStartMs, - rangeEndMs, - q.iface && q.iface !== "__all__" ? q.iface : undefined, - await readTrafficCollectionContext(), - ) - return reply.send({ server: data }) + const end = Date.now() + return reply.send({ server: await readServerTrafficHistory(server, end - rangeToMinutes(q.range) * 60000, end, q.iface && q.iface !== "__all__" ? q.iface : undefined) }) }) - app.get("/traffic/users", async (req, reply) => { const q = req.query as { range?: string } const minutes = rangeToMinutes(q.range) diff --git a/backend/src/routes/uptime.ts b/backend/src/routes/uptime.ts index 61169894..f7eb71fd 100644 --- a/backend/src/routes/uptime.ts +++ b/backend/src/routes/uptime.ts @@ -1,29 +1,27 @@ -import { emptySpeedProbeObservations } from "@mmapp/contracts" import { createSpeedTestRoute } from "./speed-test.js" import { createPingProbesWriteRoute } from "./ping-probes-write.js" import { createSpeedProbesWriteRoute } from "./speed-probes-write.js" -import { replaceSpeedProbes } from "../services/speed-probe-replacement.js" +import { replaceSpeedProbeCatalog } from "../services/speed-probe-catalog.js" import { mapSpeedProbeRead } from "../services/speed-probe-dto.js" -import { mapSpeedHistoryRun } from "../services/speed-history-dto.js" +import { readSpeedHistoryRuns } from "../services/speed-history-read.js" +import { readProbeInterfacesForServer } from "../services/probe-interface-read.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { z } from "zod" import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" -import { asc, desc, eq, sql } from "drizzle-orm" +import { asc, eq } from "drizzle-orm" import { db } from "../db/index.js" -import { servers, serverSnapshots, uptimeSpeedProbes, uptimeSpeedTestRuns } from "../db/schema.js" -import { MikrotikClient } from "../services/mikrotik.js" -import { scheduleAlertEngineAfterDataCollectors } from "../services/alert-collector-hooks.js" +import { servers, uptimeSpeedProbes } from "../db/schema.js" +import { manualDataCollection } from "../services/manual-data-collection.js" +import { createUptimeManualCollectionRoutes } from "./manual-data-collection.js" import { refreshScheduler, getSchedulerStatus } from "../services/scheduler.js" import { - collectPingForProbeIds, - collectUptimeOnce, readProbeRows, - readProbeSamplesSince, - readResourceSamplesSince, readUptimeSettings, replaceProbes, updateProbeShowOnDashboard, updateUptimeSettings, } from "../services/uptime-collector.js" -import {buildUptimeProbeObservations} from "../services/uptime-probe-observations.js" +import { readUptimeOverview } from "../services/uptime-overview-read.js" import { recordSpeedTestFailure, runBandwidthSpeedTest } from "../services/uptime-speed-service.js" function rangeToMinutes(range: string | undefined): number { @@ -37,42 +35,6 @@ function rangeToMinutes(range: string | undefined): number { } } -function toSeries(values: number[], target = 40): number[] { - if (values.length === 0) return Array(target).fill(0) - if (values.length === target) return values - if (values.length > target) return values.slice(values.length - target) - return [...Array(target - values.length).fill(values[0] ?? 0), ...values] -} - -/** Сэмпл ресурсов из PostgreSQL (readResourceSamplesSince). */ -type ResourceSampleRow = Awaited>[number] - -/** - * Последний сэмпл по времени может быть offline после сбоя опроса — тогда UI «пустой». - * Берём последний успешный (online + RAM), только если он не слишком старый относительно последней попытки, - * иначе снова показываем устаревший uptime/метрики как раньше. - */ -function pickResourceDisplayRow(rows: ResourceSampleRow[], maxGapMs: number): { - row: ResourceSampleRow | undefined - hasData: boolean -} { - if (rows.length === 0) return { row: undefined, hasData: false } - const last = rows[rows.length - 1]! - const lastTs = Date.parse(last.sampledAt) - const validOnline = (r: ResourceSampleRow) => r.status === "online" && r.totalMemory > 0 - - if (validOnline(last)) { - return { row: last, hasData: true } - } - - const fallback = [...rows].reverse().find(validOnline) - if (!fallback) return { row: undefined, hasData: false } - - const fbTs = Date.parse(fallback.sampledAt) - if (lastTs - fbTs <= maxGapMs) return { row: fallback, hasData: true } - return { row: undefined, hasData: false } -} - const uptimeRoutes: FastifyPluginAsyncZod = async (app) => { app.get("/uptime/settings", async (_req, reply) => { const s = await readUptimeSettings() @@ -135,35 +97,7 @@ const uptimeRoutes: FastifyPluginAsyncZod = async (app) => { }) }) - app.post("/uptime/collect-now", async (_req, reply) => { - await collectUptimeOnce() - scheduleAlertEngineAfterDataCollectors() - const s = await readUptimeSettings() - return reply.send({ - ok: true, - lastCollectedAt: s.lastCollectedAt ?? null, - lastDurationMs: s.lastDurationMs ?? null, - lastError: s.lastError || null, - }) - }) - - /** Ручной ping по списку id проб (группа «сервер + назначение» на UI); без ожидания interval_sec. */ - app.post("/uptime/probes/collect-group", async (req, reply) => { - const body = req.body as { probeIds?: unknown } - const raw = body.probeIds - const probeIds = Array.isArray(raw) ? raw.filter((x): x is string => typeof x === "string") : [] - try { - const { polled } = await collectPingForProbeIds(probeIds) - if (polled > 0) scheduleAlertEngineAfterDataCollectors() - return reply.send({ ok: true, polled }) - } catch (e) { - const status = (e as Error & { statusCode?: number }).statusCode - if (status === 409) { - return reply.status(409).send({ error: e instanceof Error ? e.message : "busy" }) - } - throw e - } - }) + await app.register(createUptimeManualCollectionRoutes(manualDataCollection)) app.get("/uptime/probes", async (_req, reply) => { const rows = await readProbeRows() @@ -195,28 +129,12 @@ const uptimeRoutes: FastifyPluginAsyncZod = async (app) => { }) app.get("/uptime/speed-test/runs", async (_req, reply) => { - const rows = await db.select().from(uptimeSpeedTestRuns).orderBy(desc(uptimeSpeedTestRuns.createdAt)).limit(200) - try { - return reply.send({ runs: rows.map(mapSpeedHistoryRun) }) - } catch { - return reply.code(502).send({ error: "История скорости содержит некорректную запись. Результаты не подтверждены." }) - } + try { return reply.send({ runs: await readSpeedHistoryRuns() }) } + catch { return reply.code(502).send({ error: "История скорости содержит некорректную запись. Результаты не подтверждены." }) } }) await app.register(createSpeedProbesWriteRoute({ - replace: probes => replaceSpeedProbes({ transaction: run => db.transaction(async tx => { - // Serialize complete replacements from all backend processes, including an empty catalog. - await tx.execute(sql`select pg_advisory_xact_lock(hashtext('uptime-speed-probes'))`) - await run({ - listProbes: async () => tx.select().from(uptimeSpeedProbes).for("update"), - remove: async id => { await tx.delete(uptimeSpeedProbes).where(eq(uptimeSpeedProbes.id, id)) }, - upsert: async (row, createdAt, resetObservations) => { - const { id, ...patch } = row - await tx.insert(uptimeSpeedProbes).values({ id, ...patch, createdAt }) - .onConflictDoUpdate({ target: uptimeSpeedProbes.id, set: { ...patch, ...(resetObservations ? emptySpeedProbeObservations : {}) } }) - }, - }) - }) }, probes), + replace: replaceSpeedProbeCatalog, refreshScheduler, })) @@ -236,77 +154,26 @@ const uptimeRoutes: FastifyPluginAsyncZod = async (app) => { }) app.get("/uptime/sources/:id/interfaces", async (req, reply) => { - const params = req.params as { id?: string | number } - const serverId = Number.parseInt(String(params.id ?? ""), 10) - if (!Number.isFinite(serverId)) return reply.status(400).send({ error: "Invalid server id" }) - const srv = (await db.select().from(servers).where(eq(servers.id, serverId)).limit(1))[0] - if (!srv) return reply.status(404).send({ error: "Server not found" }) - if (!srv.enabled) return reply.status(409).send({ error: "Сервер отключён: включите его перед опросом интерфейсов" }) - try { - const rows = await MikrotikClient.fromServer(srv).getInterfaces() - const interfaces = rows - .map((r) => ({ - name: String(r.name ?? "").trim(), - running: String(r.running ?? "false").toLowerCase() === "true", - disabled: String(r.disabled ?? "false").toLowerCase() === "true", - })) - .filter((r) => r.name.length > 0) - .filter((r) => r.running && !r.disabled) - .sort((a, b) => a.name.localeCompare(b.name)) - return reply.send({ interfaces }) - } catch (e) { - const message = e instanceof Error ? e.message : "Failed to fetch interfaces" - return reply.status(502).send({ error: message }) - } + const parsed = z.number().int().positive().safe().safeParse(Number((req.params as { id: string }).id)) + if (!parsed.success) return reply.status(400).send({ error: "Invalid server id" }) + const server = await getServerRowById(parsed.data) + if (!server) return reply.status(404).send({ error: "Server not found" }) + try { return reply.send(await readProbeInterfacesForServer(server, false)) } + catch (error) { return reply.status((error as { statusCode?: number }).statusCode ?? 502).send({ error: error instanceof Error ? error.message : "Не удалось прочитать интерфейсы проб" }) } }) await app.register(createSpeedTestRoute({ - serverExists: async id => Boolean((await db.select({ id: servers.id }).from(servers).where(eq(servers.id, id)).limit(1))[0]), run: runBandwidthSpeedTest, failure: recordSpeedTestFailure, })) app.get("/uptime/speed-test/endpoints/:id/interfaces", async (req, reply) => { - const params = req.params as { id?: string | number } - const serverId = Number.parseInt(String(params.id ?? ""), 10) - if (!Number.isFinite(serverId)) return reply.status(400).send({ error: "Invalid server id" }) - const srv = (await db.select().from(servers).where(eq(servers.id, serverId)).limit(1))[0] - if (!srv) return reply.status(404).send({ error: "Server not found" }) - if (!srv.enabled) return reply.status(409).send({ error: "Сервер отключён: включите его перед опросом интерфейсов" }) - try { - const client = MikrotikClient.fromServer(srv) - const [ifaces, ips] = await Promise.all([ - client.getInterfaces(), - client.getIpAddresses(), - ]) - const interfaces = ifaces - .map((i) => { - const name = String(i.name ?? "").trim() - const addresses = ips - .filter((a) => String(a.interface ?? "") === name) - .filter((a) => String(a.disabled ?? "false").toLowerCase() !== "true") - .filter((a) => String(a.invalid ?? "false").toLowerCase() !== "true") - .sort((a, b) => { - const ad = String(a.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0 - const bd = String(b.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0 - return ad - bd - }) - .map((a) => String(a.address ?? "").split("/")[0]) - .filter((x) => x.length > 0) - return { - name, - running: String(i.running ?? "false").toLowerCase() === "true", - disabled: String(i.disabled ?? "false").toLowerCase() === "true", - addresses, - } - }) - .filter((i) => i.name.length > 0) - .filter((i) => i.running && !i.disabled) - return reply.send({ interfaces }) - } catch (e) { - const message = e instanceof Error ? e.message : "Failed to fetch speed-test interfaces" - return reply.status(502).send({ error: message }) - } + const parsed = z.number().int().positive().safe().safeParse(Number((req.params as { id: string }).id)) + if (!parsed.success) return reply.status(400).send({ error: "Invalid server id" }) + const server = await getServerRowById(parsed.data) + if (!server) return reply.status(404).send({ error: "Server not found" }) + try { return reply.send(await readProbeInterfacesForServer(server, true)) } + catch (error) { return reply.status((error as { statusCode?: number }).statusCode ?? 502).send({ error: error instanceof Error ? error.message : "Не удалось прочитать интерфейсы проб" }) } }) app.get("/uptime/overview", async (req, reply) => { @@ -314,63 +181,8 @@ const uptimeRoutes: FastifyPluginAsyncZod = async (app) => { const minutes = rangeToMinutes(q.range) const rangeEndAt=new Date().toISOString() const sinceIso = new Date(Date.parse(rangeEndAt) - minutes * 60_000).toISOString() - const allServers = await db.select().from(servers) - const probeRows = await readProbeRows() - const probeSamples = await readProbeSamplesSince(sinceIso) - const uptimeCfg = await readUptimeSettings() - const intervalSec = Math.max(15, uptimeCfg.intervalSec ?? 300) - /** Допустимый разрыв между последней попыткой и последним «хорошим» сэмплом (не показывать данные часовой давности). */ - const resourceFallbackMaxGapMs = Math.min(2 * 3600 * 1000, Math.max(10 * 60 * 1000, intervalSec * 6 * 1000)) - - const probes = probeRows.map((p) => { - const rows = probeSamples.filter((r) => r.probeId === p.id) - const observations=buildUptimeProbeObservations(rows.filter(r=>Date.parse(r.sampledAt)<=Date.parse(rangeEndAt))) - const last = observations.samples.at(-1) - const series = observations.samples.map(r => r.rtt) - return { - id: p.id, - srcServerId: String(p.srcServerId), - srcInterface: p.srcInterface || "", - name: p.name, - target: p.target, - filter: p.probeFilter || "—", - rtt: last?.rtt ?? null, - loss: last?.loss ?? null, - status: last?.status ?? "unknown", - series, - enabled: p.enabled, - showOnDashboard: p.showOnDashboard, - observations, - } - }) - - const resources = await Promise.all(allServers.map(async (s) => { - const [rows, snap] = await Promise.all([ - readResourceSamplesSince(sinceIso, s.id), - db.select({ boardName: serverSnapshots.boardName }) - .from(serverSnapshots) - .where(eq(serverSnapshots.serverId, s.id)) - .orderBy(desc(serverSnapshots.polledAt)) - .limit(1), - ]) - const { row: pick, hasData } = pickResourceDisplayRow(rows, resourceFallbackMaxGapMs) - const cpuHistory = toSeries(rows.map((r) => r.cpuLoad), 40) - return { - serverId: String(s.id), - hasData, - cpu: pick?.cpuLoad ?? 0, - cpuHistory, - ramUsed: Math.max(0, ((pick?.totalMemory ?? 0) - (pick?.freeMemory ?? 0)) / (1024 * 1024)), - ramTotal: Math.max(0, (pick?.totalMemory ?? 0) / (1024 * 1024)), - hddUsed: Math.max(0, ((pick?.totalHddSpace ?? 0) - (pick?.freeHddSpace ?? 0)) / (1024 * 1024)), - hddTotal: Math.max(0, (pick?.totalHddSpace ?? 0) / (1024 * 1024)), - uptimeSeconds: pick?.uptimeSeconds ?? 0, - boardName: snap[0]?.boardName || "RouterBOARD", - temp: undefined as number | undefined, - } - })) - - return reply.send({ probes, resources,rangeStartAt:sinceIso,rangeEndAt }) + const view = await readUptimeOverview(undefined, { startAt: sinceIso, endAt: rangeEndAt }) + return reply.send({ probes: view.probes, resources: view.resources, rangeStartAt: sinceIso, rangeEndAt }) }) } diff --git a/backend/src/routes/users.ts b/backend/src/routes/users.ts index 51700f54..1c48c697 100644 --- a/backend/src/routes/users.ts +++ b/backend/src/routes/users.ts @@ -1,4 +1,5 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" +import { hasPermission } from "../lib/permissions.js" import { interfaceCatalogStateSchema, type InterfaceCatalogState, @@ -74,7 +75,9 @@ export function createUsersRoutes(deps: { getCatalogState?: (serverId: number) = schema: { body: appUserCreateSchema }, }, async (req, reply) => { try { - const user = await createUser(req.body as AppUserCreate) + const user = await createUser(req.body as AppUserCreate, { + canManagePermissions: !req.authUser || hasPermission(req.authUser.permissions, "mm:settings:admin"), + }) return reply.status(201).send({ user }) } catch (err) { return sendServiceError(reply, err) @@ -86,7 +89,9 @@ export function createUsersRoutes(deps: { getCatalogState?: (serverId: number) = }, async (req, reply) => { const { id } = req.params as { id: string } try { - const user = await updateUser(id, req.body as AppUserUpdate) + const user = await updateUser(id, req.body as AppUserUpdate, { + canManagePermissions: !req.authUser || hasPermission(req.authUser.permissions, "mm:settings:admin"), + }) return reply.send({ user }) } catch (err) { return sendServiceError(reply, err) diff --git a/backend/src/routes/wireguard-import.test.ts b/backend/src/routes/wireguard-import.test.ts index 6dde2c2e..3baf615f 100644 --- a/backend/src/routes/wireguard-import.test.ts +++ b/backend/src/routes/wireguard-import.test.ts @@ -16,10 +16,18 @@ try { for (const content of ["[Interface]\n# PrivateKey = placeholder", "[Peer]\nPublicKey = placeholder"]) { const response = await app.inject({ method: "POST", url: "/api/wireguard/import", payload: { serverId: "1", content, format: "conf", dryRun } }) assert.equal(response.statusCode, 400) - assert.match(response.json().error, /PrivateKey|Interface/) + assert.match(response.json().error, /PrivateKey|Interface|Конфигурация WireGuard не поддерживается/) } } assert.equal(mutations, 0) + const key = Buffer.alloc(32, 7).toString("base64"), peerKey = Buffer.alloc(32, 8).toString("base64") + const content = `[Interface]\nPrivateKey = ${key}\nAddress = 192.0.2.1/24\n[Peer]\nPublicKey = ${peerKey}\nAllowedIPs = 192.0.2.2/32\n` + const response = await app.inject({ method: "POST", url: "/api/wireguard/import", payload: { serverId: "1", content, format: "conf", dryRun: true } }) + assert.equal(response.statusCode, 200) + assert.equal(response.json().preview.peers.length, 1) + assert.equal(response.json().preview.peers[0].publicKey, "[KEY]") + assert.ok(!response.body.includes(key)); assert.ok(!response.body.includes(peerKey)) + assert.equal(mutations, 0) } finally { MikrotikClient.prototype.put = originalPut await app.close() diff --git a/backend/src/routes/wireguard.ts b/backend/src/routes/wireguard.ts index d1e9615c..071ca4e6 100644 --- a/backend/src/routes/wireguard.ts +++ b/backend/src/routes/wireguard.ts @@ -2,45 +2,33 @@ import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod" import { wgCreateInterfaceSchema, wgCreatePeerRequestSchema, - wgExportRequestSchema, wgImportRequestSchema, - wgPatchInterfaceSchema, - wgPatchPeerSchema, - resolveWgClientEndpoint, - type WgCreatePeerRequest, - type WgIfaceDto, + wgImportPreviewSchema, } from "@mmapp/contracts/wireguard" -import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" -import { deleteWireguardInterface } from "../services/wireguard-delete.js" -import { applyWireguardImport, WireguardImportApplyError } from "../services/wireguard-import-apply.js" -import { confirmWireguardImportRevision } from "../services/wireguard-import-revision.js" -import { parseWireguardImport } from "../services/wireguard-import.js" -import { - generateMikrotikRsc, - generateNativeConf, - generatePeerClientConf, - type WgParsedConfig, -} from "../services/wireguard-config.js" +import { deleteWireguardInterfaceGuarded } from "../services/wireguard-interface-delete.js" +import { updateWireguardInterface, wireguardInterfaceUpdateSchema } from "../services/wireguard-interface-update.js" +import { createWireguardInterface } from "../services/wireguard-interface-create.js" +import { createWireguardPeer } from "../services/wireguard-peer-create.js" +import { wireguardPeerUpdateSchema, updateWireguardPeer } from "../services/wireguard-peer-update.js" +import { deleteWireguardPeer } from "../services/wireguard-peer-delete.js" +import { McpError } from "../modules/mcp/security.js" +import { parseWireguardImportPlan, wireguardImportPreview } from "../services/wireguard-import-plan.js" +import { executeWireguardImport, WireguardImportExecutionError } from "../services/wireguard-import-execution.js" +import { readWireguardInterfaceCreateState } from "../services/wireguard-interface-create.js" +import { readWireguardExport, wireguardExportRequestSchema } from "../services/wireguard-export.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" import { captureWireguardSnapshot, - fetchWireguardRestoreState, getEnabledServerById, listWireGuardInterfaces, } from "../services/wireguard-live.js" -import { - putIpAddress, - putWireguardInterface, - putWireguardPeer, - toRosBody, -} from "../services/wireguard-ros.js" import { captureAndAppendRevision, listRevisions, loadRevisionForRestore, type ConfigRevisionSource, } from "../services/config-revisions.js" -import { parseWireguardSnapshot, planWireguardRestore, planStaticInterfaceAddresses, interfaceAddressPath, InterfaceAddressConflict } from "../services/entity-snapshots.js" -import { executeRosOps } from "../services/ros-ops.js" +import { restoreWireguardRevision } from "../services/wireguard-revision-restore.js" import { parseDbServerId } from "../utils/server-id.js" import { z } from "zod" import { hasPermission } from "../lib/permissions.js" @@ -53,62 +41,6 @@ function rosIdParam(v: string): string { return decodeURIComponent(v) } -function peerToRosBody(p: Omit & { interfaceName: string }) { - return toRosBody({ - interface: p.interfaceName, - "public-key": p.publicKey, - "allowed-address": p.allowedAddresses.join(","), - "endpoint-address": p.endpointAddress, - "endpoint-port": p.endpointPort != null ? String(p.endpointPort) : undefined, - "persistent-keepalive": - p.persistentKeepalive != null ? String(p.persistentKeepalive) : undefined, - comment: p.comment, - name: p.name, - "private-key": typeof p.privateKey === "string" ? p.privateKey : undefined, - "client-address": p.clientAddress, - "client-dns": p.clientDns, - "client-endpoint": p.clientEndpoint, - disabled: p.disabled === true ? "yes" : p.disabled === false ? "no" : undefined, - }) -} - -function previewFromParsed(parsed: WgParsedConfig) { - return { - format: parsed.format, - interface: { - name: parsed.interface.name, - listenPort: parsed.interface.listenPort, - mtu: parsed.interface.mtu, - privateKey: parsed.interface.privateKey, - comment: parsed.interface.comment, - address: parsed.interface.address, - disabled: parsed.interface.disabled, - }, - peers: parsed.peers.map((p) => ({ - publicKey: p.publicKey, - allowedAddresses: p.allowedAddresses, - endpointAddress: p.endpointAddress, - endpointPort: p.endpointPort, - persistentKeepalive: p.persistentKeepalive, - comment: p.comment, - name: p.name, - privateKey: p.privateKey, - clientAddress: p.clientAddress, - clientDns: p.clientDns, - clientEndpoint: p.clientEndpoint, - disabled: p.disabled, - })), - } -} - -function findIface( - list: WgIfaceDto[], - serverId: string, - interfaceName: string, -): WgIfaceDto | undefined { - return list.find((i) => i.serverId === serverId && i.name === interfaceName) -} - async function recordWireguard( server: NonNullable>>, source: ConfigRevisionSource, @@ -151,77 +83,33 @@ const wireguardRoutes: FastifyPluginAsyncZod = async (app) => { const server = await getEnabledServerById(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) try { - await putWireguardInterface(client, { - name: body.name, - "listen-port": String(body.listenPort), - mtu: String(body.mtu), - comment: body.comment, - "private-key": body.privateKey, - disabled: body.disabled ? "yes" : undefined, - }) - - if (body.address) { - await putIpAddress(client, body.address, body.name) - } - - if (body.peer) { - await putWireguardPeer(client, peerToRosBody({ ...body.peer, interfaceName: body.name })) - } + await createWireguardInterface(server, { ...body, serverId: server.id }) const list = await listWireGuardInterfaces({ serverId: String(server.id), includePrivateKey: true, }) const created = list.interfaces.find((i) => i.name === body.name) - await recordWireguard(server, "apply") return reply.status(201).send(created ?? { ok: true, name: body.name }) } catch (e) { - const msg = e instanceof MikrotikError ? e.message : e instanceof Error ? e.message : String(e) - return reply.status(502).send({ error: `RouterOS: ${msg}` }) + return reply.status(e instanceof McpError ? e.statusCode : 502).send({ error: e instanceof McpError ? e.message : "Создание WireGuard не подтверждено; выполнение остановлено без повтора" }) } }) app.patch("/wireguard/interfaces/:serverId/:rosId", async (req, reply) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } - const parsed = wgPatchInterfaceSchema.safeParse(req.body ?? {}) + const parsed = wireguardInterfaceUpdateSchema.safeParse(req.body ?? {}) if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } const server = await getEnabledServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) - const d = parsed.data try { - let addressOps: Parameters[1] = [] - if (d.address !== undefined) { - const state = await fetchWireguardRestoreState(server) - const live = state.ifaces.find(i => i.rosId === rosIdParam(rosId)) - if (!live) return reply.status(404).send({ error: "Интерфейс не найден" }) - if (d.name && d.name !== live.name) return reply.status(409).send({ error: "Изменяйте имя и адрес WireGuard отдельными операциями" }) - const original = state.snapshot.interfaces.find(i => i.name === live.name)! - const desired = original.innerAddresses!.filter(a => a.address !== original.address).map(a => ({ ...a })) - if (d.address && !desired.some(a => a.address === d.address)) desired.push({ address: d.address, disabled: false, ...(interfaceAddressPath(d.address) === "/ipv6/address" ? { advertise: false } : {}) }) - addressOps = planStaticInterfaceAddresses(live.name, d.address, desired, state.addrs) - } - await client.patch( - `/interface/wireguard/${encodeURIComponent(rosIdParam(rosId))}`, - toRosBody({ - name: d.name, - "listen-port": d.listenPort != null ? String(d.listenPort) : undefined, - mtu: d.mtu != null ? String(d.mtu) : undefined, - comment: d.comment, - "private-key": d.privateKey, - disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined, - }), - ) - await executeRosOps(client, addressOps) - await recordWireguard(server, "apply") + await updateWireguardInterface(server, rosIdParam(rosId), parsed.data) return reply.send({ ok: true }) } catch (e) { - const msg = e instanceof Error ? e.message : String(e) - return reply.status(e instanceof InterfaceAddressConflict ? 409 : 502).send({ error: `RouterOS: ${msg}` }) + return reply.status(e instanceof McpError ? e.statusCode : 502).send({ error: e instanceof McpError ? e.message : "WireGuard update unconfirmed; do not replay" }) } }) @@ -229,14 +117,11 @@ const wireguardRoutes: FastifyPluginAsyncZod = async (app) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } const server = await getEnabledServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) try { - await deleteWireguardInterface(client, rosIdParam(rosId)) - await recordWireguard(server, "apply") - return reply.send({ ok: true }) + const { afterProof: _afterProof, ...receipt } = await deleteWireguardInterfaceGuarded(server, rosIdParam(rosId)) + return reply.send(receipt) } catch (e) { - const msg = e instanceof Error ? e.message : String(e) - return reply.status(e instanceof InterfaceAddressConflict ? 409 : 502).send({ error: `RouterOS: ${msg}` }) + return reply.status(e instanceof McpError ? e.statusCode : 502).send({ error: e instanceof McpError ? e.message : "Удаление интерфейса WireGuard не подтверждено; каскад остановлен без повтора" }) } }) @@ -248,50 +133,27 @@ const wireguardRoutes: FastifyPluginAsyncZod = async (app) => { const body = parsed.data const server = await getEnabledServerById(body.serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) try { - await putWireguardPeer(client, peerToRosBody(body)) - await recordWireguard(server, "apply") + await createWireguardPeer(server, { ...body, serverId: server.id }) return reply.status(201).send({ ok: true }) } catch (e) { - const msg = e instanceof Error ? e.message : String(e) - return reply.status(502).send({ error: `RouterOS: ${msg}` }) + return reply.status(e instanceof McpError ? e.statusCode : 502).send({ error: e instanceof McpError ? e.message : "Создание peer WireGuard не подтверждено; выполнение остановлено без повтора" }) } }) app.patch("/wireguard/peers/:serverId/:rosId", async (req, reply) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } - const parsed = wgPatchPeerSchema.safeParse(req.body ?? {}) + const parsed = wireguardPeerUpdateSchema.safeParse(req.body ?? {}) if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } const server = await getEnabledServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const d = parsed.data - const client = MikrotikClient.fromServer(server) try { - await client.patch( - `/interface/wireguard/peers/${encodeURIComponent(rosIdParam(rosId))}`, - toRosBody({ - "public-key": d.publicKey, - "allowed-address": d.allowedAddresses?.join(","), - "endpoint-address": d.endpointAddress, - "endpoint-port": d.endpointPort != null ? String(d.endpointPort) : undefined, - "persistent-keepalive": - d.persistentKeepalive != null ? String(d.persistentKeepalive) : undefined, - comment: d.comment, - name: d.name, - "client-address": d.clientAddress, - "client-dns": d.clientDns, - "client-endpoint": d.clientEndpoint, - disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined, - }), - ) - await recordWireguard(server, "apply") - return reply.send({ ok: true }) + const { afterProof: _afterProof, afterSemanticProof: _afterSemanticProof, ...receipt } = await updateWireguardPeer(server, rosIdParam(rosId), parsed.data) + return reply.send(receipt) } catch (e) { - const msg = e instanceof Error ? e.message : String(e) - return reply.status(502).send({ error: `RouterOS: ${msg}` }) + return reply.status(e instanceof McpError ? e.statusCode : 502).send({ error: e instanceof McpError ? e.message : "Изменение peer WireGuard не подтверждено; повтор запрещён до сверки" }) } }) @@ -299,14 +161,11 @@ const wireguardRoutes: FastifyPluginAsyncZod = async (app) => { const { serverId, rosId } = req.params as { serverId: string; rosId: string } const server = await getEnabledServerById(serverIdParam(serverId)) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) try { - await client.delete(`/interface/wireguard/peers/${encodeURIComponent(rosIdParam(rosId))}`) - await recordWireguard(server, "apply") - return reply.send({ ok: true }) + const { afterProof: _afterProof, ...receipt } = await deleteWireguardPeer(server, rosIdParam(rosId)) + return reply.send(receipt) } catch (e) { - const msg = e instanceof Error ? e.message : String(e) - return reply.status(502).send({ error: `RouterOS: ${msg}` }) + return reply.status(e instanceof McpError ? e.statusCode : 502).send({ error: e instanceof McpError ? e.message : "Удаление peer WireGuard не подтверждено; повтор запрещён до сверки" }) } }) @@ -316,143 +175,54 @@ const wireguardRoutes: FastifyPluginAsyncZod = async (app) => { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } const body = parsed.data - let config: WgParsedConfig + const serverId = parseDbServerId(body.serverId) + if (serverId === null) return reply.status(400).send({ error: "Некорректный serverId" }) + let config: ReturnType try { - config = parseWireguardImport(body.content, body.format) + config = parseWireguardImportPlan(body.content, body.format, serverId) } catch (e) { return reply.status(400).send({ error: e instanceof Error ? e.message : "Ошибка разбора конфига" }) } - const preview = previewFromParsed(config) + const safe = wireguardImportPreview(config) as { format: "rsc" | "conf"; interface: Record; peers: Record[] } + const preview = wgImportPreviewSchema.parse({ ...safe, peers: safe.peers.map(peer => ({ ...peer, publicKey: "[KEY]" })) }) if (body.dryRun) { return reply.send({ dryRun: true, preview }) } - const server = await getEnabledServerById(body.serverId) + const server = await getEnabledServerById(serverId) if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - const client = MikrotikClient.fromServer(server) try { - const applied = await applyWireguardImport(client, config, (peer, interfaceName) => peerToRosBody({ ...peer, interfaceName })) - if (!(await confirmWireguardImportRevision(() => recordWireguard(server, "copy")))) { + const before = await readWireguardInterfaceCreateState(server) + const receipt = await executeWireguardImport(server, config, before.proof) + const applied = { interfaceName: preview.interface.name, peersCreated: receipt.peersCreated, verified: receipt.verified, completedCommands: receipt.completedCommands, commandCount: receipt.commandCount } + if (!receipt.revisionRecorded) { return reply.status(502).send({ error: "Конфигурация применена, но сохранение ревизии не подтверждено. Обновите данные перед повтором", state: "applied", preview }) } return reply.send({ dryRun: false, preview, applied }) } catch (error) { - if (error instanceof WireguardImportApplyError) { - return reply.status(error.state === "conflict" ? 409 : 502).send({ error: error.message, state: error.state, preview }) + if (error instanceof WireguardImportExecutionError) { + return reply.status(error.state === "conflict" ? 409 : 502).send({ error: error.message, state: error.state, verifiedPrefixCommands: error.verifiedPrefixCommands, commandCount: error.commandCount, automaticReplay: false, preview }) } - return reply.status(502).send({ error: "Не удалось проверить состояние WireGuard перед импортом; применение не начато", preview }) + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, state: "needs-review", automaticReplay: false, preview }) + return reply.status(502).send({ error: "Результат импорта WireGuard не подтверждён; обновите состояние перед повтором", state: "needs-review", automaticReplay: false, preview }) } }) app.post("/wireguard/export", async (req, reply) => { - const parsed = wgExportRequestSchema.safeParse(req.body ?? {}) + const parsed = wireguardExportRequestSchema.safeParse(req.body ?? {}) if (!parsed.success) { return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() }) } - const body = parsed.data - const server = await getEnabledServerById(body.serverId) - if (!server) return reply.status(404).send({ error: "Сервер не найден" }) - - const list = await listWireGuardInterfaces({ - serverId: String(server.id), - includePrivateKey: body.includePrivateKey === true, - }) - const iface = await findIface(list.interfaces, String(server.id), body.interfaceName) - if (!iface) return reply.status(404).send({ error: "Интерфейс не найден" }) - - if (body.format === "rsc" || body.format === "conf") { - const state = await fetchWireguardRestoreState(server) - const addresses = state.snapshot.interfaces.find(i => i.name === iface.name)?.innerAddresses - if (!addresses) return reply.status(409).send({ error: "Интерфейс изменился. Обновите список перед экспортом" }) - if (addresses.length > 1 || addresses.some(a => a.disabled || a.advertise || a.noDad || a.fromPool || a.eui64)) { - return reply.status(409).send({ error: "Экспорт не воспроизводит несколько адресов или специальные параметры. Используйте экспорт RouterOS" }) - } - if ((addresses[0]?.address ?? "") !== (iface.address ?? "")) return reply.status(409).send({ error: "Адрес изменился. Обновите список перед экспортом" }) - } - - if (body.format === "rsc") { - const content = generateMikrotikRsc({ - name: iface.name, - listenPort: iface.listenPort, - mtu: iface.mtu, - comment: iface.comment, - enabled: iface.enabled, - privateKey: body.includePrivateKey ? iface.privateKey : undefined, - publicKey: iface.publicKey, - address: iface.address, - serverName: iface.serverName, - peers: iface.peers.map((p) => ({ - publicKey: p.publicKey, - allowedIps: p.allowedIps, - endpoint: p.endpoint, - persistentKeepalive: p.persistentKeepalive, - persistent: p.persistent, - comment: p.comment, - name: p.name, - clientAddress: p.clientAddress, - clientDns: p.clientDns, - clientEndpoint: p.clientEndpoint, - })), - }) - return reply.send({ - format: "rsc", - filename: `${iface.name}.rsc`, - content, - }) - } - - if (body.format === "conf") { - const content = generateNativeConf( - { - name: iface.name, - listenPort: iface.listenPort, - mtu: iface.mtu, - comment: iface.comment, - enabled: iface.enabled, - privateKey: iface.privateKey, - publicKey: iface.publicKey, - address: iface.address, - serverName: iface.serverName, - peers: iface.peers.map((p) => ({ - publicKey: p.publicKey, - allowedIps: p.allowedIps, - endpoint: p.endpoint, - persistentKeepalive: p.persistentKeepalive, - persistent: p.persistent, - comment: p.comment, - })), - }, - { includePrivateKey: body.includePrivateKey === true }, - ) - return reply.send({ - format: "conf", - filename: `${iface.name}.conf`, - content, - }) - } - - // peer-conf - const peer = body.peerId - ? iface.peers.find((p) => p.id === body.peerId || p.rosId === body.peerId) - : iface.peers[0] - if (!peer) return reply.status(404).send({ error: "Пир не найден" }) - if (!iface.publicKey) { - return reply.status(400).send({ error: "У интерфейса нет public-key" }) - } - const endpoint = resolveWgClientEndpoint(peer.clientEndpoint, server.host, iface.listenPort) - const content = generatePeerClientConf({ - peerAddress: peer.clientAddress, - peerDns: peer.clientDns, - serverPublicKey: iface.publicKey, - // Server allowed-address describes the remote peer, not the client's routes. - endpoint, - persistentKeepalive: peer.persistentKeepalive ?? 25, - }) - return reply.send({ - format: "peer-conf", - filename: `${iface.name}-peer.conf`, - content, - }) + const cancellation = new AbortController() + const abort = () => { if (!reply.raw.writableFinished) cancellation.abort() } + req.raw.once("aborted", abort); reply.raw.once("close", abort) + try { + const file = await readWireguardExport(parsed.data, { signal: cancellation.signal, authorize: async () => { if (req.authUser) await assertPortalClaimsCurrent(req.authUser) } }) + return reply.header("Cache-Control", "private, no-store").send({ format: file.format, filename: file.filename, content: file.content.toString("utf8") }) + } catch (error) { + if (error instanceof McpError) return reply.status(error.statusCode).send({ error: error.message, code: error.code }) + throw error + } finally { req.raw.off("aborted", abort); reply.raw.off("close", abort) } }) app.get("/wireguard/revisions", async (req, reply) => { @@ -475,23 +245,15 @@ const wireguardRoutes: FastifyPluginAsyncZod = async (app) => { }) if (!loaded.ok) return reply.status(loaded.status).send({ error: loaded.error }) try { - const desired = parseWireguardSnapshot(loaded.row.payload) - const state = await fetchWireguardRestoreState(loaded.server) - const ops = planWireguardRestore(desired, { - ifaces: state.ifaces, - peers: state.peers, - addrs: state.addrs, - }) - await executeRosOps(state.client, ops) - await recordWireguard(loaded.server, "rollback") + const receipt = await restoreWireguardRevision(loaded.server, id, { authorize: async () => { if (req.authUser) await assertPortalClaimsCurrent(req.authUser) } }) + if (!receipt.revisionRecorded) return reply.status(502).send({ error: "Конфигурация WireGuard проверена, но сохранение rollback-ревизии не подтверждено; повтор запрещён до сверки", configurationVerified: true, revisionRecorded: false, automaticReplay: false }) const list = await listWireGuardInterfaces({ serverId: String(loaded.server.id), includePrivateKey: true, }) - return reply.send({ ok: true, interfaces: list.interfaces }) + return reply.send({ ok: true, interfaces: list.interfaces, verified: receipt.verified, revisionRecorded: receipt.revisionRecorded }) } catch (e) { - const msg = e instanceof Error ? e.message : String(e) - return reply.status(e instanceof InterfaceAddressConflict ? 409 : 502).send({ error: `RouterOS: ${msg}` }) + return reply.status(e instanceof McpError ? e.statusCode : 502).send({ error: e instanceof McpError ? e.message : "Восстановление WireGuard не подтверждено; повтор запрещён до сверки", automaticReplay: false }) } }) } diff --git a/backend/src/scripts/complete-database-restore.ts b/backend/src/scripts/complete-database-restore.ts new file mode 100644 index 00000000..a9c37235 --- /dev/null +++ b/backend/src/scripts/complete-database-restore.ts @@ -0,0 +1,14 @@ +import { closePool } from "../db/index.js" +import { completePendingSystemDatabaseRestore } from "../services/system-database-restore-recovery.js" + +try { + const [, , operationId, appliedJournalSha256, extra] = process.argv + if (!operationId || !appliedJournalSha256 || extra !== undefined) { + throw new Error("Использование: db:complete-restore ") + } + await completePendingSystemDatabaseRestore(operationId, appliedJournalSha256) + console.log("Карантин PostgreSQL снят для подтверждённого восстановления") +} catch (error) { + console.error(error instanceof Error ? error.message : "Снятие карантина PostgreSQL не подтверждено") + process.exitCode = 1 +} finally { await closePool() } diff --git a/backend/src/scripts/inspect-database-restore-checkpoint.ts b/backend/src/scripts/inspect-database-restore-checkpoint.ts new file mode 100644 index 00000000..ce41332b --- /dev/null +++ b/backend/src/scripts/inspect-database-restore-checkpoint.ts @@ -0,0 +1,10 @@ +import { closePool } from "../db/index.js" +import { inspectSystemDatabaseRestoreCheckpoint } from "../services/system-database-restore-inspection.js" + +try { + if (process.argv.length !== 2) throw new Error() + console.log(JSON.stringify(await inspectSystemDatabaseRestoreCheckpoint())) +} catch { + console.error("Не удалось сверить контрольную точку PostgreSQL. Сохраните журнал и исходные файлы для проверки.") + process.exitCode = 1 +} finally { await closePool() } diff --git a/backend/src/scripts/inspect-database-schema.ts b/backend/src/scripts/inspect-database-schema.ts new file mode 100644 index 00000000..41a14dc7 --- /dev/null +++ b/backend/src/scripts/inspect-database-schema.ts @@ -0,0 +1,34 @@ +import { env } from "../config.js" +import { assertRuntimeDatabaseActivationCurrent, closePool, pool } from "../db/index.js" +import { captureRuntimeDatabaseSchema } from "../db/database-runtime-schema.js" +import { captureRuntimeRestoreData } from "../db/database-restore-data.js" +import { withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" +import { mcpStateDirectory } from "../modules/mcp/security.js" +import { assertDatabaseMaintenanceStartup } from "../services/database-maintenance-startup.js" + +try { + if (!process.env.DATABASE_RUNTIME_REQUEST_FILE) throw new Error("Для сверки схемы требуется утверждённый источник PostgreSQL") + const directory = mcpStateDirectory() + await assertDatabaseMaintenanceStartup(directory, env.DATABASE_URL) + const result = await withSystemDatabaseOperationLock(() => pool.connect(), async (client, discard) => { + await assertDatabaseMaintenanceStartup(directory, env.DATABASE_URL) + if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client) + assertRuntimeDatabaseActivationCurrent() + if (process.argv.includes("--data")) { + const passwordFile = process.env.DATABASE_ARCHIVE_PASSWORD_FILE + if (process.env.DATABASE_RUNTIME_WRITER_APPROVAL_FILE !== undefined && !passwordFile) { + throw new Error("Для сверки данных требуется файл пароля роли архивирования PostgreSQL") + } + const snapshot = passwordFile + ? await (await import("../services/database-capture-inspection.js")).inspectProtectedDatabaseData(client, discard, passwordFile) + : await captureRuntimeRestoreData(client, discard) + assertRuntimeDatabaseActivationCurrent() + return { format: snapshot.format, schemaSha256: snapshot.schemaSha256, dataSha256: snapshot.sha256 } + } + const snapshot = await captureRuntimeDatabaseSchema(client, discard) + assertRuntimeDatabaseActivationCurrent() + return { format: snapshot.format, schemaSha256: snapshot.sha256, serverVersion: snapshot.serverVersion } + }) + console.log(JSON.stringify(result)) +} finally { await closePool() } diff --git a/backend/src/scripts/mcp-call-inventory.ts b/backend/src/scripts/mcp-call-inventory.ts new file mode 100644 index 00000000..ab920509 --- /dev/null +++ b/backend/src/scripts/mcp-call-inventory.ts @@ -0,0 +1,45 @@ +import assert from "node:assert/strict" +import { readFile, readdir } from "node:fs/promises" +import { listActions } from "../modules/mcp/actions.js" +import { coverageSchema, routeKey } from "../modules/mcp/coverage.js" + +const coverage = coverageSchema.parse(JSON.parse(await readFile(new URL("../../mcp-coverage.json", import.meta.url), "utf8"))) +const descriptors = listActions().map(action => action.operation) +assert.equal(new Set(descriptors.map(action => action.toolName)).size, descriptors.length, "Duplicate tool name") +const routesByOperation = new Map() +for (const route of coverage.routes) { + for (const id of route.operations) { + const routes = routesByOperation.get(id) ?? [] + routes.push(routeKey(route)) + routesByOperation.set(id, routes) + } +} +const auditDirectory = new URL("../../../docs/", import.meta.url) +const auditFiles = (await readdir(auditDirectory)).filter(name => /^mcp-.*-call-audit-\d{8}\.md$/.test(name)).sort() +const auditsByTool = new Map() +for (const name of auditFiles) { + const source = await readFile(new URL(name, auditDirectory), "utf8") + const tools = new Set() + for (const line of source.split(/\r?\n/)) { + if (!line.startsWith("|")) continue + // The owning call must occur in the first column or follow the step number. + const [, first, second] = line.split("|") + const toolColumn = /^\s*\d+\s*$/.test(first) ? second : first + const match = /\x60(mm_[a-z0-9_]+)(?=[({\x60])/.exec(toolColumn ?? "") + if (match) tools.add(match[1]) + } + for (const tool of tools) { + const files = auditsByTool.get(tool) ?? [] + files.push(name) + auditsByTool.set(tool, files) + } +} +const registeredTools = new Set(descriptors.map(action => action.toolName)) +for (const tool of auditsByTool.keys()) assert(registeredTools.has(tool), `Audit refers to an unregistered tool: ${tool}`) +const rows = descriptors.map(({ id, toolName, version, section, effect, implementation }) => ({ + id, toolName, version, section, effect, implementation, managerRestRoutes: (routesByOperation.get(id) ?? []).sort(), + callAuditReferences: auditsByTool.get(toolName) ?? [], +})).sort((left, right) => left.section.localeCompare(right.section) || left.toolName.localeCompare(right.toolName)) + +// Output is a work list, not evidence of native RouterOS compatibility or a successful invocation. +console.log(JSON.stringify(rows, null, 2)) diff --git a/backend/src/scripts/mcp-coverage.ts b/backend/src/scripts/mcp-coverage.ts new file mode 100644 index 00000000..85a25669 --- /dev/null +++ b/backend/src/scripts/mcp-coverage.ts @@ -0,0 +1,14 @@ +import { readFile, writeFile } from "node:fs/promises" +import { collectApplicationRoutes, coverageSchema, routeKey, type CoverageEntry } from "../modules/mcp/coverage.js" + +if (process.argv[2] !== "--update") throw new Error("Explicit --update required; newly discovered routes remain pending") +const file = new URL("../../mcp-coverage.json", import.meta.url) +const previous = await readFile(file, "utf8").then(text => coverageSchema.parse(JSON.parse(text)).routes, error => { if (error.code === "ENOENT") return []; throw error }) +const known = new Map(previous.map(entry => [routeKey(entry), entry])) +const routes: CoverageEntry[] = (await collectApplicationRoutes()).map(route => known.get(routeKey(route)) ?? { + ...route, operations: [], + status: /^(?:\/api\/(?:mcp(?:\/|$)|auth\/config$)|\/mcp$|\/oauth\/|\/\.well-known\/|\/_internal\/mcp\/|\/health$)/.test(route.path) ? "excluded" : "pending", + reason: /^(?:\/api\/mcp|\/mcp$|\/oauth\/|\/\.well-known\/|\/_internal\/mcp\/)/.test(route.path) ? "Управление самим MCP, транспорт или машинная авторизация: отдельного AI-инструмента нет." : ["/health", "/api/auth/config"].includes(route.path) ? "Bootstrap/health авторизации интерфейса; бизнес-операция MCP отсутствует." : "Нужен точный сервисный MCP-адаптер и проверка зависимостей; native-инструменты не объявляются эквивалентом этого REST-сценария.", +}) +await writeFile(file, JSON.stringify(coverageSchema.parse({ version: 1, routes }), null, 2) + "\n") +console.log(routes.map(routeKey).join("\n")) diff --git a/backend/src/scripts/mcp-stdio.ts b/backend/src/scripts/mcp-stdio.ts new file mode 100644 index 00000000..c1ccf486 --- /dev/null +++ b/backend/src/scripts/mcp-stdio.ts @@ -0,0 +1,43 @@ +import { AsyncLocalStorage } from "node:async_hooks" +import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client" +import { serveStdio, StdioServerTransport } from "@modelcontextprotocol/server/stdio" +import { createStdioSdkTerminalGuard, StdioSdkBudgetedMcpServer } from "../modules/mcp/stdio-terminal-budget.js" + +// Deliberately does not import dotenv/config: stdout belongs exclusively to MCP. +const endpoint = process.env.MCP_URL, token = process.env.MCP_TOKEN +if (!endpoint || !token) { process.stderr.write("Укажите MCP_URL и MCP_TOKEN\n"); process.exit(1) } +const url = new URL(endpoint) +if (url.protocol !== "https:" && !["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)) throw new Error("Удалённый MCP требует HTTPS") +const serverInfo = Object.freeze({ name: "MikrotikManager-stdio", version: "1.0.0" }) +const remote = new Client(serverInfo) +const followSignal = new AsyncLocalStorage() +await remote.connect(new StreamableHTTPClientTransport(url, { + requestInit: { headers: { authorization: "Bearer " + token } }, + fetch: (input, init) => { + const signal = followSignal.getStore() + if (!signal) return fetch(input, init) + return fetch(input, { ...init, signal: init?.signal ? AbortSignal.any([init.signal, signal]) : signal }) + }, +})) +const guard = createStdioSdkTerminalGuard(serverInfo) +const wire = new StdioServerTransport() +guard.decorateWire(wire) +const handle = serveStdio(async () => { + const server = new StdioSdkBudgetedMcpServer(guard) + server.server.registerCapabilities({ tools: {} }) + // The remote server supplies current policy and validates tool arguments. + server.server.setRequestHandler("tools/list", ({ params }, context) => + remote.listTools({ cursor: params?.cursor }, { signal: context.mcpReq.signal })) + server.server.setRequestHandler("tools/call", ({ params }, context) => { + guard.protectCallback(context.mcpReq.id) + const request = { name: params.name, arguments: params.arguments } + // Follow owns a cancellable upstream read. Mutation outcomes still need their receipt. + return params.name === "mm_logs_follow" + ? followSignal.run(context.mcpReq.signal, () => remote.callTool(request)) + : remote.callTool(request) + }) + return server +}, { transport: wire, onerror: () => process.stderr.write("Ошибка связи с MCP; проверьте доступ и журнал обращений\n") }) +process.once("SIGTERM", () => { void Promise.all([handle.close(), remote.close()]) }) +process.once("SIGINT", () => { void Promise.all([handle.close(), remote.close()]) }) +process.stdin.once("end", () => { void remote.close() }) diff --git a/backend/src/scripts/migrate-sqlite-to-pg.ts b/backend/src/scripts/migrate-sqlite-to-pg.ts index ade8c3e5..37e80da2 100644 --- a/backend/src/scripts/migrate-sqlite-to-pg.ts +++ b/backend/src/scripts/migrate-sqlite-to-pg.ts @@ -1,20 +1,34 @@ import { env } from "../config.js" -import { closePool, pool } from "../db/index.js" import { applySqlMigrations } from "../db/migrate.js" +import { withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" import { ensurePartitionsAround } from "../db/partitions.js" import { importSqliteToPostgres } from "../db/sqlite-import.js" +import { mcpStateDirectory } from "../modules/mcp/security.js" +import { assertDatabaseMaintenanceStartup } from "../services/database-maintenance-startup.js" const sqlitePath = process.argv.find((a) => a.startsWith("--sqlite="))?.slice(9) ?? env.DATABASE_PATH const dryRun = process.argv.includes("--dry-run") const fullHistory = process.argv.includes("--full-history") const strict = process.argv.includes("--strict") || !dryRun -await applySqlMigrations(pool) -await ensurePartitionsAround(pool) -const report = await importSqliteToPostgres(pool, sqlitePath, { dryRun, strict, fullHistory }) -console.log(JSON.stringify(report, null, 2)) -if (!dryRun && report.rejects.length > 0 && strict) { +if (process.env.DATABASE_RUNTIME_REQUEST_FILE !== undefined) { + throw new Error("Импорт SQLite требует отдельного процесса оператора PostgreSQL") +} +const { closePool, pool } = await import("../db/index.js") + +try { + const stateDirectory = mcpStateDirectory() + await assertDatabaseMaintenanceStartup(stateDirectory, env.DATABASE_URL) + const report = await withSystemDatabaseOperationLock(() => pool.connect(), async client => { + await assertDatabaseMaintenanceStartup(stateDirectory, env.DATABASE_URL) + if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client) + await applySqlMigrations(pool) + await ensurePartitionsAround(pool) + return importSqliteToPostgres(pool, sqlitePath, { dryRun, strict, fullHistory }) + }) + console.log(JSON.stringify(report, null, 2)) + if (!dryRun && report.rejects.length > 0 && strict) process.exitCode = 1 +} finally { await closePool() - process.exit(1) } -await closePool() diff --git a/backend/src/scripts/prepare-database.ts b/backend/src/scripts/prepare-database.ts new file mode 100644 index 00000000..876601f2 --- /dev/null +++ b/backend/src/scripts/prepare-database.ts @@ -0,0 +1,18 @@ +import "../config.js" + +const args = process.argv.slice(2) +if (args.length > 0 && (args.length !== 1 || args[0] !== "--partitions-only")) { + throw new Error("Допустимый параметр подготовки БД: --partitions-only") +} +if (!process.env.DATABASE_URL || process.env.DATABASE_RUNTIME_REQUEST_FILE !== undefined) { + throw new Error("Подготовка БД требует отдельного процесса оператора с явно заданным DATABASE_URL") +} + +const { initDatabase } = await import("../db/bootstrap.js") +const { closePool } = await import("../db/index.js") + +try { + await initDatabase(args.length === 1 ? "operator-partitions" : "startup") +} finally { + await closePool() +} diff --git a/backend/src/scripts/seal-bandwidth-control-source.ts b/backend/src/scripts/seal-bandwidth-control-source.ts new file mode 100644 index 00000000..0d16c460 --- /dev/null +++ b/backend/src/scripts/seal-bandwidth-control-source.ts @@ -0,0 +1,25 @@ +/** Offline owner command only; credentials and issuer private key never become + * MCP inputs. No schema/key/role creation, native calls or automatic recovery. */ +import { resolve } from "node:path" +import { readBandwidthControlFile } from "../services/bandwidth-control-source.js" +import { sealEmptyBandwidthControlSource } from "../services/bandwidth-control-provision.js" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "../services/bandwidth-admission-schema.js" +import { BANDWIDTH_CONTROL_SOURCE_DDL } from "../services/bandwidth-control-source-schema.js" + +const args=process.argv.slice(2) +if(args.length===1&&args[0]==="--print-schema"){ + console.log("BEGIN;\nCREATE SCHEMA mmapp_control;\n"+BANDWIDTH_ADMISSION_PROPOSAL_DDL+"\n"+BANDWIDTH_CONTROL_SOURCE_DDL+"\nCOMMIT;") +}else if(args.length!==2||args[0]!=="--config"){ + console.error("Использование: npm run bandwidth:control:seal -- --config <абсолютный путь к операторскому JSON>") + process.exitCode=1 +}else{ + try{ + const deadline=Date.now()+5000 + const config=JSON.parse((await readBandwidthControlFile(resolve(args[1]),32768,()=>{if(Date.now()>=deadline)throw new Error()})).toString("utf8")) + const record=await sealEmptyBandwidthControlSource(config) + console.log(JSON.stringify({installationId:record.installationId,controlGeneration:record.controlGeneration,sourceSealed:true,applicationSchemaReady:false,nativeDispatchEnabled:false})) + }catch{ + console.error("Привязка источника не подтверждена. Проверьте исходный pending-файл и immutable identity; не создавайте новое поколение для обхода отказа.") + process.exitCode=1 + } +} diff --git a/backend/src/scripts/seal-database-restore-checkpoint.ts b/backend/src/scripts/seal-database-restore-checkpoint.ts new file mode 100644 index 00000000..d858e1a8 --- /dev/null +++ b/backend/src/scripts/seal-database-restore-checkpoint.ts @@ -0,0 +1,14 @@ +import { closePool } from "../db/index.js" +import { sealPendingSystemDatabaseRestore } from "../services/system-database-restore-recovery.js" + +try { + const [, , operationId, appliedJournalSha256, extra] = process.argv + if (!operationId || !appliedJournalSha256 || extra !== undefined) { + throw new Error("Использование: db:seal-restore-checkpoint ") + } + await sealPendingSystemDatabaseRestore(operationId, appliedJournalSha256) + console.log("Контрольная точка PostgreSQL опубликована") +} catch (error) { + console.error(error instanceof Error ? error.message : "Публикация контрольной точки PostgreSQL не подтверждена") + process.exitCode = 1 +} finally { await closePool() } diff --git a/backend/src/scripts/serve-restore-checkpoint-claims.ts b/backend/src/scripts/serve-restore-checkpoint-claims.ts new file mode 100644 index 00000000..4a31fd3c --- /dev/null +++ b/backend/src/scripts/serve-restore-checkpoint-claims.ts @@ -0,0 +1,13 @@ +import { listenForRestoreCheckpointClaims } from "../services/database-restore-checkpoint-claim.js" + +try { + const [, , file, socket, extra] = process.argv + if (!file || !socket || extra !== undefined) throw new Error("Использование: db:serve-restore-claims <файл> <локальный-сокет>") + if (process.platform !== "win32") process.umask(0o007) + const server = await listenForRestoreCheckpointClaims(file, socket) + for (const signal of ["SIGINT", "SIGTERM"] as const) process.once(signal, () => server.close()) + console.log("Операторский процесс готов подтверждать одноразовые допуски восстановления PostgreSQL") +} catch { + console.error("Не удалось запустить операторскую службу допуска восстановления PostgreSQL") + process.exitCode = 1 +} diff --git a/backend/src/scripts/stage-database-restore-checkpoint.ts b/backend/src/scripts/stage-database-restore-checkpoint.ts new file mode 100644 index 00000000..e6a84a15 --- /dev/null +++ b/backend/src/scripts/stage-database-restore-checkpoint.ts @@ -0,0 +1,12 @@ +import { closePool } from "../db/index.js" +import { stageSystemDatabaseRestoreCheckpoint } from "../services/system-database-restore-staging.js" + +try { + const [, , currentFileSha256, extra] = process.argv + if (!currentFileSha256 || extra !== undefined) throw new Error("Использование: db:stage-restore-checkpoint ") + const operationId = await stageSystemDatabaseRestoreCheckpoint(currentFileSha256) + console.log(JSON.stringify({ operationId, status: "pending" })) +} catch (error) { + console.error(error instanceof Error ? error.message : "Допуск PostgreSQL не опубликован") + process.exitCode = 1 +} finally { await closePool() } diff --git a/backend/src/services/acme-cloudflare.ts b/backend/src/services/acme-cloudflare.ts index ce7582ec..f51a9972 100644 --- a/backend/src/services/acme-cloudflare.ts +++ b/backend/src/services/acme-cloudflare.ts @@ -48,7 +48,7 @@ async function cloudflareRequest( } export async function testCloudflareToken(token: string): Promise { - await cloudflareRequest>(token, "/zones?per_page=1") + await cloudflareRequest>(token, "/zones?per_page=5") } async function resolveZoneId(token: string, domain: string, defaultZoneId?: string): Promise { diff --git a/backend/src/services/acme-settings-test.test.ts b/backend/src/services/acme-settings-test.test.ts new file mode 100644 index 00000000..bb726165 --- /dev/null +++ b/backend/src/services/acme-settings-test.test.ts @@ -0,0 +1,52 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { performAcmeSettingsTest, probeAcmeCloudflareToken } from "./acme-settings-test.js" + +const token = "test-only-sensitive-token" +let requests = 0, mode = "accept" +const server = createServer((req, res) => { + requests++ + assert.equal(req.method, "GET") + assert.equal(req.url, "/client/v4/zones?per_page=5") + assert.equal(req.headers.authorization, `Bearer ${token}`) + if (mode === "redirect") { res.writeHead(302, { location: "/unapproved" }); res.end(); return } + if (mode === "large") { res.end("x".repeat(65537)); return } + if (mode === "malformed") { res.end("invalid"); return } + res.writeHead(mode === "reject" ? 403 : 200, { "content-type": "application/json" }) + res.end(JSON.stringify(mode === "reject" ? { success: false, errors: [{ message: token }] } : { success: true, result: [{ id: "private-zone", name: "private.example" }] })) +}) +await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) +const address = server.address() +assert.ok(address && typeof address !== "string") +const fetcher: typeof fetch = (url, init) => { + assert.equal(String(url), "https://api.cloudflare.com/client/v4/zones?per_page=5") + assert.equal(init?.redirect, "error") + return fetch(`http://127.0.0.1:${address.port}/client/v4/zones?per_page=5`, init) +} +try { + for (const [scenario, outcome] of [["accept", "accepted"], ["reject", "rejected"], ["large", "unknown"], ["malformed", "unknown"], ["redirect", "unknown"]] as const) { + mode = scenario + const before = requests, result = await probeAcmeCloudflareToken(token, undefined, fetcher) + assert.equal(result.outcome, outcome) + assert.equal(requests, before + 1) + assert.ok(!JSON.stringify(result).includes(token)) + assert.ok(!JSON.stringify(result).includes("private-zone")) + } + let dispatches = 0, checks = 0 + const probe: typeof probeAcmeCloudflareToken = async value => { assert.equal(value, token); dispatches++; return { ok: true, outcome: "accepted", message: "ok" } } + const guard = async (work: () => Promise) => work() + const dependencies = { readStored: async () => token, probe, guard } + await performAcmeSettingsTest({}, { ...dependencies, beforeDispatch: async () => { checks++ }, afterDispatch: async () => { checks++ } }) + assert.equal(dispatches, 1); assert.equal(checks, 2) + for (const reason of ["revoke", "source drift", "protection", "pause"]) { + await assert.rejects(performAcmeSettingsTest({}, { ...dependencies, beforeDispatch: async () => { throw Error(reason) } }), new RegExp(reason)) + } + const abort = new AbortController(); abort.abort() + await assert.rejects(performAcmeSettingsTest({}, { ...dependencies, signal: abort.signal })) + assert.equal(dispatches, 1) + await assert.rejects(performAcmeSettingsTest({}, { ...dependencies, afterDispatch: async () => { throw Error("late revoke") } }), /late revoke/) + assert.equal(dispatches, 2) + await assert.rejects(performAcmeSettingsTest({ endpoint: "https://other.example" }, dependencies)) + assert.equal(dispatches, 2) + console.log("ACME token test: local HTTP, safe output, one dispatch, redirect/size/malformed, late/pre-dispatch fences and cancel passed") +} finally { await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); server.closeAllConnections() } diff --git a/backend/src/services/acme-settings-test.ts b/backend/src/services/acme-settings-test.ts new file mode 100644 index 00000000..c4f64741 --- /dev/null +++ b/backend/src/services/acme-settings-test.ts @@ -0,0 +1,50 @@ +import { testAcmeCloudflareSettingsSchema } from "@mmapp/contracts/certificates" +import { getAcmeCloudflareToken } from "./certificates-service.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" + +export type AcmeTokenTestResult = { ok: boolean; outcome: "accepted" | "rejected" | "unknown"; message: string } +export const cloudflareTokenTestUrl = "https://api.cloudflare.com/client/v4/zones?per_page=5" +/** Fixed read-only provider endpoint. No ACME account, DNS record or certificate is created. */ +export async function probeAcmeCloudflareToken(token: string, signal?: AbortSignal, fetcher: typeof fetch = fetch): Promise { + const unknown: AcmeTokenTestResult = { ok: false, outcome: "unknown", message: "Ответ Cloudflare не подтверждён" } + try { + const response = await fetcher(cloudflareTokenTestUrl, { + method: "GET", redirect: "error", headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }, + signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(15000)]) : AbortSignal.timeout(15000), + }) + const reader = response.body?.getReader(), chunks: Uint8Array[] = [] + let size = 0 + if (reader) try { + for (;;) { + const chunk = await reader.read() + if (chunk.done) break + size += chunk.value.byteLength + if (size > 65536) { await reader.cancel(); return unknown } + chunks.push(chunk.value) + } + } finally { reader.releaseLock() } + const raw: unknown = JSON.parse(Buffer.concat(chunks).toString("utf8")) + if (!raw || typeof raw !== "object" || !("success" in raw)) return unknown + if (response.ok && raw.success === true && "result" in raw && Array.isArray(raw.result)) return { ok: true, outcome: "accepted", message: "Cloudflare API доступен" } + if (raw.success === false) return { ok: false, outcome: "rejected", message: "Cloudflare отклонил проверку токена" } + return unknown + } catch { return unknown } +} + +export async function performAcmeSettingsTest(raw: unknown, options: { + readStored?: () => Promise; probe?: typeof probeAcmeCloudflareToken; + beforeDispatch?: () => Promise; afterDispatch?: () => Promise; signal?: AbortSignal; + guard?: (work: () => Promise) => Promise; +} = {}) { + const input = testAcmeCloudflareSettingsSchema.strict().parse(raw) + const token = input.cloudflareApiToken?.trim() || await (options.readStored ?? getAcmeCloudflareToken)() + if (!token || token.length > 4096 || /[\r\n\u0000]/.test(token)) throw Object.assign(new Error("Не задан корректный Cloudflare API token"), { statusCode: 400 }) + const work = async () => { + await options.beforeDispatch?.() + options.signal?.throwIfAborted() + const result = await (options.probe ?? probeAcmeCloudflareToken)(token, options.signal) + await options.afterDispatch?.() + return result + } + return options.guard ? options.guard(work) : executeGuardedApplicationWrite({ kind: "acme-settings-test", id: "1" }, "all", work, async () => [{ kind: "acme-settings", id: "1" }]) +} diff --git a/backend/src/services/acme-settings-update-model.test.ts b/backend/src/services/acme-settings-update-model.test.ts new file mode 100644 index 00000000..5e44f786 --- /dev/null +++ b/backend/src/services/acme-settings-update-model.test.ts @@ -0,0 +1,22 @@ +import assert from "node:assert/strict" +import { planAcmeSettingsUpdate, publicAcmeSettings, DEFAULT_ACME_DIRECTORY } from "./acme-settings-update-model.js" + +const previous = { directoryUrl: "https://directory.example/acme", defaultZoneId: "saved-zone", cloudflareApiToken: "PRIVATE_SAVED_TOKEN", accountPrivateKey: "PRIVATE_ACCOUNT_KEY", updatedAt: "old" } +const now = "2026-10-24T00:00:00.000Z" +assert.deepEqual(planAcmeSettingsUpdate(previous, {}, now), { ...previous, updatedAt: now }) +assert.deepEqual(planAcmeSettingsUpdate(undefined, {}, now), { directoryUrl: DEFAULT_ACME_DIRECTORY, defaultZoneId: "", cloudflareApiToken: "", accountPrivateKey: "", updatedAt: now }) +for (const clear of [null, "", " "]) { + const cleared = planAcmeSettingsUpdate(previous, { cloudflareApiToken: clear, defaultZoneId: clear }, now) + assert.equal(cleared.cloudflareApiToken, ""); assert.equal(cleared.defaultZoneId, "") + assert.equal(cleared.accountPrivateKey, previous.accountPrivateKey) + assert.equal(publicAcmeSettings(cleared).tokenConfigured, false) +} +const replaced = planAcmeSettingsUpdate(previous, { directoryUrl: " https://new.example/directory ", defaultZoneId: " zone ", cloudflareApiToken: " NEW_PRIVATE_TOKEN " }, now) +assert.equal(replaced.directoryUrl, "https://new.example/directory"); assert.equal(replaced.defaultZoneId, "zone"); assert.equal(replaced.cloudflareApiToken, "NEW_PRIVATE_TOKEN") +assert.equal(replaced.accountPrivateKey, previous.accountPrivateKey) +assert.equal(planAcmeSettingsUpdate(previous, { directoryUrl: " " }, now).directoryUrl, previous.directoryUrl) +assert.deepEqual(publicAcmeSettings(replaced), { directoryUrl: "https://new.example/directory", defaultZoneId: "zone", tokenConfigured: true, updatedAt: now }) +assert.deepEqual(publicAcmeSettings(), { directoryUrl: DEFAULT_ACME_DIRECTORY, defaultZoneId: "", tokenConfigured: false, updatedAt: undefined }) +assert.equal(publicAcmeSettings({ ...previous, defaultZoneId: "" }).defaultZoneId, undefined) +assert.ok(!JSON.stringify(publicAcmeSettings(replaced)).includes("PRIVATE")) +console.log("✓ ACME PUT model: complete fields, retain/replace/clear, trim/fallback, account preservation and safe public DTO") diff --git a/backend/src/services/acme-settings-update-model.ts b/backend/src/services/acme-settings-update-model.ts new file mode 100644 index 00000000..d303b3f4 --- /dev/null +++ b/backend/src/services/acme-settings-update-model.ts @@ -0,0 +1,18 @@ +export const DEFAULT_ACME_DIRECTORY = "https://acme-v02.api.letsencrypt.org/directory" +export interface AcmeSettingsRecord { + directoryUrl: string; defaultZoneId: string; cloudflareApiToken: string; accountPrivateKey: string; updatedAt: string; revision?: string +} +export interface AcmeSettingsPatch { directoryUrl?: string; defaultZoneId?: string | null; cloudflareApiToken?: string | null } +/** Keep PUT semantics: omission retains, null/empty clear token and zone; directory falls back. */ +export function planAcmeSettingsUpdate(row: AcmeSettingsRecord | undefined, input: AcmeSettingsPatch, updatedAt: string): AcmeSettingsRecord { + return { + directoryUrl: input.directoryUrl?.trim() || row?.directoryUrl || DEFAULT_ACME_DIRECTORY, + defaultZoneId: input.defaultZoneId === null ? "" : input.defaultZoneId?.trim() ?? row?.defaultZoneId ?? "", + cloudflareApiToken: input.cloudflareApiToken === null ? "" : input.cloudflareApiToken?.trim() ?? row?.cloudflareApiToken ?? "", + accountPrivateKey: row?.accountPrivateKey ?? "", updatedAt, + } +} +export function publicAcmeSettings(row?: AcmeSettingsRecord) { + return row ? { directoryUrl: row.directoryUrl, defaultZoneId: row.defaultZoneId || undefined, tokenConfigured: Boolean(row.cloudflareApiToken?.trim()), updatedAt: row.updatedAt } + : { directoryUrl: DEFAULT_ACME_DIRECTORY, defaultZoneId: "", tokenConfigured: false, updatedAt: undefined as string | undefined } +} diff --git a/backend/src/services/acme-settings-update.ts b/backend/src/services/acme-settings-update.ts new file mode 100644 index 00000000..935deaf7 --- /dev/null +++ b/backend/src/services/acme-settings-update.ts @@ -0,0 +1,43 @@ +import { putAcmeCloudflareSettingsSchema } from "@mmapp/contracts/certificates" +import { pool } from "../db/index.js" +import { McpError } from "../modules/mcp/security.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { planAcmeSettingsUpdate, publicAcmeSettings, type AcmeSettingsRecord } from "./acme-settings-update-model.js" + +export async function readAcmeSettingsRecord(bounded = true): Promise { + if (!bounded) return (await pool.query(`SELECT directory_url AS "directoryUrl",default_zone_id AS "defaultZoneId",cloudflare_api_token AS "cloudflareApiToken",account_private_key AS "accountPrivateKey",to_char(updated_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS "updatedAt",xmin::text AS revision FROM acme_settings WHERE id=1`)).rows[0] + const row = (await pool.query(`SELECT + CASE WHEN octet_length(directory_url)<=8192 THEN directory_url ELSE NULL END AS "directoryUrl", + CASE WHEN octet_length(default_zone_id)<=16384 THEN default_zone_id ELSE NULL END AS "defaultZoneId", + CASE WHEN octet_length(cloudflare_api_token)<=4096 THEN cloudflare_api_token ELSE NULL END AS "cloudflareApiToken", + CASE WHEN octet_length(account_private_key)<=65536 THEN account_private_key ELSE NULL END AS "accountPrivateKey", + to_char(updated_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS "updatedAt",xmin::text AS revision FROM acme_settings WHERE id=1`)).rows[0] + if (row && [row.directoryUrl,row.defaultZoneId,row.cloudflareApiToken,row.accountPrivateKey].some(value=>typeof value!=="string")) throw new McpError("STATE_UNAVAILABLE", "Настройки ACME превышают предел безопасного чтения", 503) + return row +} +/** REST and MCP use one guarded mutation, preserving the existing PUT field semantics. */ +export async function performAcmeSettingsUpdate(raw: unknown, options: { + beforeWrite?: () => Promise; afterWrite?: () => Promise; expected?: AcmeSettingsRecord | null +} = {}) { + const input = putAcmeCloudflareSettingsSchema.strict().parse(raw) + return executeGuardedApplicationWrite({ kind: "acme-settings", id: "1" }, "all", async () => { + // Human REST retains its existing unbounded string contract. MCP supplies an + // expected source and uses SQL-side bounds before reading credential bytes. + const bounded = options.expected !== undefined + const row = await readAcmeSettingsRecord(bounded), next = planAcmeSettingsUpdate(row, input, new Date().toISOString().replace(/Z$/, "000Z")) + await options.beforeWrite?.() + // A compare-and-set also closes the interval between the last fence and SQL. + let result + if (options.expected === null) { + result = await pool.query('INSERT INTO acme_settings(id,directory_url,default_zone_id,cloudflare_api_token,account_private_key,updated_at) VALUES(1,$1,$2,$3,$4,$5) ON CONFLICT(id) DO NOTHING RETURNING id,xmin::text AS revision', [next.directoryUrl,next.defaultZoneId,next.cloudflareApiToken,next.accountPrivateKey,next.updatedAt]) + } else if (options.expected) { + const expected = options.expected + result = await pool.query('UPDATE acme_settings SET directory_url=$1,default_zone_id=$2,cloudflare_api_token=$3,updated_at=$4 WHERE id=1 AND directory_url=$5 AND default_zone_id=$6 AND cloudflare_api_token=$7 AND account_private_key=$8 AND updated_at=$9 AND xmin::text=$10 RETURNING id,xmin::text AS revision', [next.directoryUrl,next.defaultZoneId,next.cloudflareApiToken,next.updatedAt,expected.directoryUrl,expected.defaultZoneId,expected.cloudflareApiToken,expected.accountPrivateKey,expected.updatedAt,expected.revision]) + } else { + result = await pool.query('INSERT INTO acme_settings(id,directory_url,default_zone_id,cloudflare_api_token,account_private_key,updated_at) VALUES(1,$1,$2,$3,$4,$5) ON CONFLICT(id) DO UPDATE SET directory_url=EXCLUDED.directory_url,default_zone_id=EXCLUDED.default_zone_id,cloudflare_api_token=EXCLUDED.cloudflare_api_token,updated_at=EXCLUDED.updated_at RETURNING id,xmin::text AS revision', [next.directoryUrl,next.defaultZoneId,next.cloudflareApiToken,next.accountPrivateKey,next.updatedAt]) + } + if (result.rowCount !== 1) throw new McpError("STATE_CONFLICT", "Настройки ACME изменились перед записью", 409) + await options.afterWrite?.() + return { saved: { ...next, revision: result.rows[0]?.revision as string | undefined }, publicSettings: publicAcmeSettings(await readAcmeSettingsRecord(bounded)) } + }) +} diff --git a/backend/src/services/alert-collector-hooks.ts b/backend/src/services/alert-collector-hooks.ts index 1821d751..d7a29869 100644 --- a/backend/src/services/alert-collector-hooks.ts +++ b/backend/src/services/alert-collector-hooks.ts @@ -27,9 +27,7 @@ export function clearAlertCollectorHooksTimer(): void { } } -async function runAlertEngineWhenIdle(): Promise { - const run = alertEngineRunner - if (!run) return +async function runAlertEngineWhenIdle(run: () => Promise): Promise { while (isSchedulerJobRunning("alert_engine")) { if (isSchedulerJobsPaused()) return await new Promise((resolve) => setTimeout(resolve, ALERT_AFTER_COLLECT_WAIT_MS)) @@ -41,11 +39,13 @@ async function runAlertEngineWhenIdle(): Promise { /** После успешной записи сигналов в БД коллекторами (не из `alert_engine`). */ export function scheduleAlertEngineAfterDataCollectors(): void { if (isSchedulerJobsPaused()) return + const run = alertEngineRunner + if (!run) return if (alertAfterCollectTimer != null) clearTimeout(alertAfterCollectTimer) alertAfterCollectTimer = setTimeout(async () => { alertAfterCollectTimer = null alertAfterCollectChain = alertAfterCollectChain - .then(() => runAlertEngineWhenIdle()) + .then(() => runAlertEngineWhenIdle(run)) .catch(() => {}) }, ALERT_AFTER_COLLECT_DEBOUNCE_MS) } diff --git a/backend/src/services/alert-engine/confirm-stability.ts b/backend/src/services/alert-engine/confirm-stability.ts index f6ab69ab..e093287f 100644 --- a/backend/src/services/alert-engine/confirm-stability.ts +++ b/backend/src/services/alert-engine/confirm-stability.ts @@ -3,8 +3,10 @@ import { db } from "../../db/index.js" import { alertEngineConfirmPending } from "../../db/schema.js" import type { ApiAlertRule } from "../alerts-service.js" import type { RuleEvalHit } from "./types.js" +import { assertAlertEngineAccess } from "./execution-access.js" export async function deleteConfirmPending(ruleId: string) { + await assertAlertEngineAccess() await db.delete(alertEngineConfirmPending).where(eq(alertEngineConfirmPending.ruleId, ruleId)) } @@ -36,6 +38,7 @@ export async function stabilityReadyForSend( .limit(1))[0] if (!existing || existing.payloadHash !== hit.payloadHash) { + await assertAlertEngineAccess() if (existing) { await db.update(alertEngineConfirmPending) .set({ payloadHash: hit.payloadHash, sinceAt: nowIso }) diff --git a/backend/src/services/alert-engine/execution-access.test.ts b/backend/src/services/alert-engine/execution-access.test.ts new file mode 100644 index 00000000..a6021da5 --- /dev/null +++ b/backend/src/services/alert-engine/execution-access.test.ts @@ -0,0 +1,87 @@ +import assert from "node:assert/strict" +import { setImmediate as nextTurn } from "node:timers/promises" +import { assertAlertEngineAccess, withAlertEngineAccess } from "./execution-access.js" +import { sendTelegramRequest } from "../alerts-telegram-send.js" +import { enqueueTelegramOutbox } from "./outbox.js" + +const denial = Error("fixture grant revoked") +let revoked = false, dispatches = 0 +const authorize = async () => { if (revoked) throw denial } +const signal = AbortSignal.timeout(5000) + +// No context preserves legacy behavior; the fetcher is entirely local memory. +assert.deepEqual(await sendTelegramRequest("fixture-token", { chat_id: "fixture-chat", text: "fixture" }, async () => { + dispatches++; return new Response('{"ok":true}', { status: 200 }) +}), { ok: true }) +assert.equal(dispatches, 1) + +await assert.rejects(withAlertEngineAccess(authorize, signal, async () => { + revoked = true + await assert.rejects(enqueueTelegramOutbox({ id: "fixture", dedupeKey: "fixture", payload: { text: "fixture", history: { id: "fixture-history", ruleName: "fixture", severity: "info", message: "fixture", firedAt: new Date().toISOString() } } }), error => error === denial) + // enqueue's fence runs before DB insertion; this test requires no SQL server. +}), error => error === denial) +revoked = false +await assert.rejects(withAlertEngineAccess(authorize, signal, async () => { + revoked = true + await assert.rejects(sendTelegramRequest("fixture-token", { chat_id: "fixture-chat", text: "fixture" }, async () => { + dispatches++; throw Error("must not dispatch") + }), error => error === denial) +}), error => error === denial) +assert.equal(dispatches, 1) + +// A body held by a fixed in-memory fetch double must close on live revocation. +revoked = false +let started: () => void = () => {}, aborted = false +const didStart = new Promise(resolve => { started = resolve }) +const pending = withAlertEngineAccess(authorize, signal, async () => sendTelegramRequest("fixture-token", { chat_id: "fixture-chat", text: "fixture" }, async (_url, options) => { + dispatches++; started() + return await new Promise((_resolve, reject) => { + const fail = () => { aborted = true; reject(options?.signal?.reason) } + options?.signal?.addEventListener("abort", fail, { once: true }) + if (options?.signal?.aborted) fail() + }) +})) +await didStart +revoked = true +await assert.rejects(pending, error => error === denial) +assert.equal(aborted, true) +assert.equal(dispatches, 2) + +revoked = false +let release: () => void = () => {}, detached: Promise | undefined +const gate = new Promise(resolve => { release = resolve }) +await withAlertEngineAccess(authorize, signal, async () => { detached = gate.then(() => assertAlertEngineAccess()) }) +release() +await assert.rejects(detached!, /Контекст запуска алертов завершён/) + +// A deferred authorization read belongs to the operation until it settles, +// even when its main work and final access check have already finished. +let reads = 0, returned = false +let pollStarted: () => void = () => {}, releasePoll: () => void = () => {}, finalChecked: () => void = () => {}, releaseClosedContext: () => void = () => {} +const didPoll = new Promise(resolve => { pollStarted = resolve }) +const pollGate = new Promise(resolve => { releasePoll = resolve }) +const didFinalCheck = new Promise(resolve => { finalChecked = resolve }) +const closedGate = new Promise(resolve => { releaseClosedContext = resolve }) +let closedChild: Promise | undefined +const draining = withAlertEngineAccess(async () => { + reads++ + if (reads === 2) { pollStarted(); await pollGate } + if (reads === 3) finalChecked() +}, AbortSignal.timeout(5000), async () => { + closedChild = closedGate.then(() => assertAlertEngineAccess()) + await didPoll + return "finished" +}).then(value => { returned = true; return value }) +await didFinalCheck +await nextTurn() +assert.equal(returned, false, "Do not return while the owned poll is still reading authorization") +releaseClosedContext() +await assert.rejects(closedChild!, /Контекст запуска алертов завершён/) +assert.equal(reads, 3, "Closed inherited work must not start another authorization read") +releasePoll() +assert.equal(await draining, "finished") +assert.equal(returned, true) +await nextTurn() +assert.equal(reads, 3, "No detached authorization poll survives operation settlement") +console.log("Optional alert execution fences: deny before outbox SQL/send, cancel mock in-flight send on revoke, and reject inherited closed context; no external HTTP/SQL") +console.log("Alert access lifecycle: operation drains a deferred authorization poll before returning and closed inherited work cannot read again") diff --git a/backend/src/services/alert-engine/execution-access.ts b/backend/src/services/alert-engine/execution-access.ts new file mode 100644 index 00000000..8b62eb88 --- /dev/null +++ b/backend/src/services/alert-engine/execution-access.ts @@ -0,0 +1,58 @@ +import { AsyncLocalStorage } from "node:async_hooks" + +interface Access { active: boolean; authorize: () => Promise; signal: AbortSignal } +const access = new AsyncLocalStorage() + +/** Optional operation fence. Legacy scheduler/REST runs have no context. + * Detached callbacks inherit a closed context and must fail closed. */ +export async function assertAlertEngineAccess(): Promise { + const current = access.getStore() + if (!current) return + if (!current.active) throw new Error("Контекст запуска алертов завершён") + current.signal.throwIfAborted() + await current.authorize() + if (!current.active) throw new Error("Контекст запуска алертов завершён") + current.signal.throwIfAborted() +} +export function alertEngineAbortSignal(): AbortSignal | undefined { return access.getStore()?.signal } + +export async function withAlertEngineAccess(authorize: () => Promise, signal: AbortSignal, work: () => Promise): Promise { + // This also bounds in-flight Telegram requests. It cannot retract an already + // committed SQL statement or prove a provider did not receive an aborted POST. + signal = AbortSignal.any([signal, AbortSignal.timeout(120_000)]) + const cancellation = new AbortController() + const abort = () => cancellation.abort(signal.reason) + signal.addEventListener("abort", abort, { once: true }) + if (signal.aborted) abort() + let pendingPoll: Promise | undefined + const check = async () => { + cancellation.signal.throwIfAborted() + try { await authorize() } + catch (error) { cancellation.abort(error); throw error } + cancellation.signal.throwIfAborted() + } + const current: Access = { active: true, authorize: check, signal: cancellation.signal } + // An external revoke must close an in-flight HTTP response even while the + // engine is awaiting it. Polling never starts a second overlapping poll. + const timer = setInterval(() => { + if (pendingPoll || !current.active) return + pendingPoll = check().catch(() => {}).finally(() => { pendingPoll = undefined }) + }, 100) + return access.run(current, async () => { + try { + await assertAlertEngineAccess() + const value = await work() + await assertAlertEngineAccess() + return value + } + finally { + current.active = false + clearInterval(timer) + signal.removeEventListener("abort", abort) + cancellation.abort() + // Authorization can be awaiting SQL when work settles. Drain that owned + // read before callers close their pool or treat this operation as idle. + await pendingPoll + } + }) +} diff --git a/backend/src/services/alert-engine/outbox.ts b/backend/src/services/alert-engine/outbox.ts index 6f880465..88bc89ce 100644 --- a/backend/src/services/alert-engine/outbox.ts +++ b/backend/src/services/alert-engine/outbox.ts @@ -1,13 +1,17 @@ import {executeOutboxRetry} from "../alerts-outbox-retry.js" +import {executeGuardedApplicationWrite} from "../config-mutation-policy.js" +import {isDeepStrictEqual} from "node:util" import {readOutboxOverview} from "../alerts-outbox-overview.js" import {and,asc,desc,eq,inArray,lte,sql} from "drizzle-orm" import {db} from "../../db/index.js" import {alertOutbox,alertHistory} from "../../db/schema.js" import {sendTelegramAlertMessage} from "../alerts-service.js" import {executeOutboxDelivery,type AlertOutboxPayload} from "./outbox-execution.js" +import {assertAlertEngineAccess} from "./execution-access.js" export type {AlertOutboxPayload} from "./outbox-execution.js" export async function enqueueTelegramOutbox(input:{id:string;dedupeKey:string;payload:AlertOutboxPayload;maxRetries?:number}):Promise{ + await assertAlertEngineAccess() const [receipt]=await db.insert(alertOutbox).values({id:input.id,dedupeKey:input.dedupeKey,channel:"telegram",status:"pending",retryCount:0,maxRetries:Math.max(1,Math.floor(input.maxRetries??3)),nextAttemptAt:new Date().toISOString(),payloadJson:input.payload}).onConflictDoNothing({target:alertOutbox.dedupeKey}).returning({id:alertOutbox.id}) if(!receipt)return false if(receipt.id!==input.id)throw Error("Запись сообщения в очередь не подтверждена") @@ -19,20 +23,24 @@ export async function dispatchPendingOutbox(limit=25):Promise<{sent:number;faile const rows=await db.select({id:alertOutbox.id}).from(alertOutbox).where(and(eq(alertOutbox.status,"pending"),lte(alertOutbox.nextAttemptAt,nowIso))).orderBy(asc(alertOutbox.createdAt)).limit(Math.max(1,limit)) let sent=0,failed=0,held=0;const errors:string[]=[] for(const row of rows){ + await assertAlertEngineAccess() const result=await executeOutboxDelivery(row.id,{ now:Date.now, - claim:async id=>{const [claimed]=await db.update(alertOutbox).set({status:"processing",nextAttemptAt:new Date().toISOString()}).where(and(eq(alertOutbox.id,id),eq(alertOutbox.status,"pending"),lte(alertOutbox.nextAttemptAt,new Date().toISOString()))).returning();if(claimed&&(claimed.id!==id||claimed.status!=="processing"))throw Error("Захват сообщения не подтверждён");return claimed}, - send:payload=>sendTelegramAlertMessage({text:payload.text,chatId:payload.chatId}), + claim:async id=>{await assertAlertEngineAccess();const [claimed]=await db.update(alertOutbox).set({status:"processing",nextAttemptAt:new Date().toISOString()}).where(and(eq(alertOutbox.id,id),eq(alertOutbox.status,"pending"),lte(alertOutbox.nextAttemptAt,new Date().toISOString()))).returning();if(claimed&&(claimed.id!==id||claimed.status!=="processing"))throw Error("Захват сообщения не подтверждён");return claimed}, + send:async payload=>{await assertAlertEngineAccess();return sendTelegramAlertMessage({text:payload.text,chatId:payload.chatId})}, settle:(id,value)=>db.transaction(async tx=>{ const {history,...patch}=value + await assertAlertEngineAccess() const [receipt]=await tx.update(alertOutbox).set(patch).where(and(eq(alertOutbox.id,id),eq(alertOutbox.status,"processing"))).returning({id:alertOutbox.id,status:alertOutbox.status}) if(!receipt||receipt.id!==id||receipt.status!==value.status)throw Error("Статус очереди не подтверждён") if(history){ + await assertAlertEngineAccess() const [saved]=await tx.insert(alertHistory).values({...history,ruleId:history.ruleId??null,groupId:history.groupId??null}).returning() if(!saved||saved.id!==history.id||saved.sentOk!==history.sentOk||saved.ruleName!==history.ruleName||saved.severity!==history.severity||saved.message!==history.message||saved.ruleId!==(history.ruleId??null)||saved.groupId!==(history.groupId??null)||Date.parse(saved.firedAt)!==Date.parse(history.firedAt))throw Error("Журнал результата очереди не подтверждён") } }), }) + await assertAlertEngineAccess() if(result.apiAccepted)sent++ if(result.kind==="failed")failed++ if(result.kind==="held")held++ @@ -49,9 +57,36 @@ export async function getAlertsOutboxOverview(){ }) } -export async function retryUnknownOutbox(id:string,input:unknown){ +export class OutboxRetrySourceUnavailable extends Error { + constructor(message="Источник записи очереди недоступен или превышает 1 МиБ"){super(message)} +} +export type OutboxRetrySource = Record +async function readRetrySource(context:Pick,id:string,lock:boolean):Promise{ + const result=await context.execute(sql`SELECT CASE WHEN octet_length(to_jsonb(o)::text)<=1048576 THEN to_jsonb(o) END AS source FROM alert_outbox o WHERE id=${id} ${lock?sql`FOR UPDATE OF o`:sql``}`) + const row=result.rows[0] + if(!row)return null + if(!row.source||typeof row.source!=="object"||Array.isArray(row.source))throw new OutboxRetrySourceUnavailable() + return row.source as OutboxRetrySource +} +/** A bounded source for approved adapters; message/recipient fields stay private. */ +export async function readOutboxRetrySource(id:string){return readRetrySource(db,id,false)} + +export async function retryUnknownOutbox(id:string,input:unknown,guard?:(source:OutboxRetrySource)=>Promise){ return executeOutboxRetry[0]>[0]>(id,input,{ transaction:work=>db.transaction(work), - update:async(tx,messageId,expectedVersion)=>{const [receipt]=await tx.update(alertOutbox).set({status:"pending",resolutionVersion:sql`resolution_version + 1`,nextAttemptAt:new Date().toISOString(),lastError:null}).where(and(eq(alertOutbox.id,messageId),eq(alertOutbox.status,"unknown"),eq(alertOutbox.channel,"telegram"),eq(alertOutbox.resolutionVersion,expectedVersion))).returning({id:alertOutbox.id,status:alertOutbox.status,resolutionVersion:alertOutbox.resolutionVersion});return receipt}, + update:async(tx,messageId,expectedVersion)=>{ + let source:OutboxRetrySource|null=null + if(guard){source=await readRetrySource(tx,messageId,true);if(!source)throw new OutboxRetrySourceUnavailable();await guard(source)} + return executeGuardedApplicationWrite({kind:"alerts-outbox",id:messageId},"all",async()=>{ + const nextAttemptAt=new Date().toISOString() + const [receipt]=await tx.update(alertOutbox).set({status:"pending",resolutionVersion:sql`resolution_version + 1`,nextAttemptAt,lastError:null}).where(and(eq(alertOutbox.id,messageId),eq(alertOutbox.status,"unknown"),eq(alertOutbox.channel,"telegram"),eq(alertOutbox.resolutionVersion,expectedVersion))).returning({id:alertOutbox.id,status:alertOutbox.status,resolutionVersion:alertOutbox.resolutionVersion}) + if(source&&receipt){ + const after=await readRetrySource(tx,messageId,true) + const stable=(value:OutboxRetrySource)=>Object.fromEntries(Object.entries(value).filter(([key])=>!["status","resolution_version","next_attempt_at","last_error"].includes(key))) + if(!after||after.id!==messageId||after.status!=="pending"||after.resolution_version!==expectedVersion+1||after.last_error!==null||typeof after.next_attempt_at!=="string"||Date.parse(after.next_attempt_at)!==Date.parse(nextAttemptAt)||!isDeepStrictEqual(stable(source),stable(after)))throw new OutboxRetrySourceUnavailable("Не подтверждена неизменность уведомления при постановке в очередь; транзакция отменена") + } + return receipt + }) + }, }) } diff --git a/backend/src/services/alert-engine/run-once.ts b/backend/src/services/alert-engine/run-once.ts index 6536a63a..2e1b19ac 100644 --- a/backend/src/services/alert-engine/run-once.ts +++ b/backend/src/services/alert-engine/run-once.ts @@ -21,6 +21,7 @@ import { getLatestSourceFinishedAt, getSourceWatermark, updateSourceWatermark } import { pickTelegramAlertEmoji } from "./telegram-emoji.js" import type { AlertEngineRunResult, RuleEvalHit } from "./types.js" import { appendEvent } from "../../modules/events/service/events-service.js" +import { assertAlertEngineAccess } from "./execution-access.js" const SNAPSHOT_SOURCE_WAIT_MS = 30_000 const SNAPSHOT_SOURCE_POLL_MS = 20 @@ -32,6 +33,7 @@ const SNAPSHOT_SOURCE_POLL_MS = 20 async function awaitSnapshotSourcesIdle(): Promise { const t0 = Date.now() while (Date.now() - t0 < SNAPSHOT_SOURCE_WAIT_MS) { + await assertAlertEngineAccess() const busy = isAnyAlertSnapshotSourceJobRunning() || getServersRestPingCollectorState().running || @@ -64,10 +66,12 @@ async function loadEngineStateMap(): Promise { + await assertAlertEngineAccess() const errors: string[] = [] const sampledAt = new Date().toISOString() const latestSourceFinishedAt = await getLatestSourceFinishedAt() @@ -183,6 +188,7 @@ export async function runAlertEngineOnce(): Promise { errors.push(...outbox.errors) if (standaloneFires > 0 || groupFires > 0) { + await assertAlertEngineAccess() await appendEvent({ level: "info", eventType: "alerts.engine.fired", @@ -196,6 +202,7 @@ export async function runAlertEngineOnce(): Promise { }) } if (errors.length > 0) { + await assertAlertEngineAccess() await appendEvent({ level: "warning", eventType: "alerts.engine.errors", diff --git a/backend/src/services/alert-engine/source-watermark.ts b/backend/src/services/alert-engine/source-watermark.ts index 0ff9a641..db20c58e 100644 --- a/backend/src/services/alert-engine/source-watermark.ts +++ b/backend/src/services/alert-engine/source-watermark.ts @@ -1,6 +1,7 @@ import { desc, eq, inArray } from "drizzle-orm" import { db } from "../../db/index.js" import { alertEngineCursor, schedulerRuns } from "../../db/schema.js" +import { assertAlertEngineAccess } from "./execution-access.js" const SOURCE_JOBS = [ "traffic", @@ -33,6 +34,7 @@ export async function updateSourceWatermark(lastSourceFinishedAt: string | null) .where(eq(alertEngineCursor.id, 1)) .limit(1))[0] if (existing) { + await assertAlertEngineAccess() await db.update(alertEngineCursor) .set({ lastSourceFinishedAt, @@ -41,6 +43,7 @@ export async function updateSourceWatermark(lastSourceFinishedAt: string | null) .where(eq(alertEngineCursor.id, 1)) return } + await assertAlertEngineAccess() await db.insert(alertEngineCursor) .values({ id: 1, diff --git a/backend/src/services/alerts-service.ts b/backend/src/services/alerts-service.ts index 8ee810bb..5d0c5099 100644 --- a/backend/src/services/alerts-service.ts +++ b/backend/src/services/alerts-service.ts @@ -1,3 +1,4 @@ +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" import {deliverTelegramAlert,type TelegramAlertInput} from "./alerts-telegram-delivery.js" import {readCollectorSourceSnapshot} from "./alert-engine/signals-from-collectors.js" import {createCollectorContextReader} from "./alert-engine/collector-context-reader.js" @@ -213,7 +214,8 @@ export type ReplaceAlertGroupInput = { /** Атомарно заменяет группы, правила и строки targets. */ type AlertsWriteContext=Parameters[0]>[0] -export async function replaceAlertsConfig(payload: { groups?: ReplaceAlertGroupInput[]; rules: ReplaceAlertRuleInput[];expectedRevision?:string }) { +export function replaceAlertsConfig(payload: Parameters[0]) { return executeGuardedApplicationWrite({ kind: "alerts-configuration", id: "1" }, "all", () => replaceAlertsConfigImpl(payload)) } +async function replaceAlertsConfigImpl(payload: { groups?: ReplaceAlertGroupInput[]; rules: ReplaceAlertRuleInput[];expectedRevision?:string }) { const now=sql`now()` return executeAlertsConfigReplacement(payload, { transaction:work=>db.transaction(work), @@ -322,14 +324,15 @@ export async function updateTelegramSettings(patch: { chatId?: string | undefined /** undefined — не менять; null — сбросить тему */ messageThreadId?: number | null -}) { - return executeTelegramSettingsUpdate(patch, { - ensure: ensureTelegramRow, +}, beforeWrite: () => Promise = async()=>{}) { + return executeGuardedApplicationWrite({kind:"alerts-telegram",id:"1"},"all",()=>executeTelegramSettingsUpdate(patch, { + ensure: async()=>{await beforeWrite();return ensureTelegramRow()}, update: async values => { + await beforeWrite() const [row] = await db.update(alertTelegramSettings).set({...values, updatedAt:sql`now()`}).where(eq(alertTelegramSettings.id,1)).returning() return row }, - }) + })) } /** Токен только для внутреннего вызова Telegram API (не отдаётся клиенту). */ diff --git a/backend/src/services/alerts-telegram-send.test.ts b/backend/src/services/alerts-telegram-send.test.ts index a5db5ca2..1024677f 100644 --- a/backend/src/services/alerts-telegram-send.test.ts +++ b/backend/src/services/alerts-telegram-send.test.ts @@ -18,3 +18,9 @@ test("Telegram rejects explicitly only with a valid negative API receipt; ambigu const rejected=await sendTelegramRequest("fixture-secret",{chat_id:"123",text:"fixture"},async()=>Response.json({ok:false,error_code:429,description:"retry later"},{status:429}));assert.equal(rejected.ok,false);if(!rejected.ok)assert.equal(rejected.outcome,"rejected") for(const body of [{ok:false,description:"proxy failure"},{ok:false,error_code:"429",description:"bad type"},{ok:true}]){const result=await sendTelegramRequest("fixture-secret",{chat_id:"123",text:"fixture"},async()=>Response.json(body,{status:503}));assert.equal(result.ok,false);if(!result.ok)assert.equal(result.outcome,"unknown")} }) + +test("Telegram response is bounded and redirects never forward the credential",async()=>{ + let calls=0 + const result=await sendTelegramRequest("fixture-secret",{chat_id:"123",text:"fixture"},async(_url,init)=>{calls++;assert.equal(init?.redirect,"error");return new Response('{'+' '.repeat(65537)+'"ok":true}')}) + assert.equal(calls,1);assert.equal(result.ok,false);if(!result.ok)assert.equal(result.outcome,"unknown") +}) diff --git a/backend/src/services/alerts-telegram-send.ts b/backend/src/services/alerts-telegram-send.ts index a89bf92f..68824054 100644 --- a/backend/src/services/alerts-telegram-send.ts +++ b/backend/src/services/alerts-telegram-send.ts @@ -1,10 +1,16 @@ +import {assertAlertEngineAccess,alertEngineAbortSignal} from "./alert-engine/execution-access.js" type Payload={chat_id:string;text:string;message_thread_id?:number} export type TelegramSendResult={ok:true}|{ok:false;outcome:"rejected"|"unknown";error:string} export async function sendTelegramRequest(token:string,payload:Payload,fetcher:typeof fetch=fetch):Promise{ + await assertAlertEngineAccess() const redact=(message:string)=>message.replaceAll(token,"[TOKEN]").replaceAll(encodeURIComponent(token),"[TOKEN]") try{ - const response=await fetcher("https://api.telegram.org/bot"+encodeURIComponent(token)+"/sendMessage",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(payload),signal:AbortSignal.timeout(30_000)}) - const raw:unknown=await response.json().catch(()=>undefined) + const contextSignal=alertEngineAbortSignal(),timeoutSignal=AbortSignal.timeout(30_000) + const response=await fetcher("https://api.telegram.org/bot"+encodeURIComponent(token)+"/sendMessage",{method:"POST",redirect:"error",headers:{"Content-Type":"application/json"},body:JSON.stringify(payload),signal:contextSignal?AbortSignal.any([contextSignal,timeoutSignal]):timeoutSignal}) + const reader=response.body?.getReader(),chunks:Uint8Array[]=[] + let size=0,raw:unknown + if(reader){try{for(;;){const chunk=await reader.read();if(chunk.done)break;size+=chunk.value.byteLength;if(size>65536){await reader.cancel();throw Error("Telegram response limit")};chunks.push(chunk.value)}}finally{reader.releaseLock()}} + try{raw=JSON.parse(Buffer.concat(chunks).toString("utf8"))}catch{raw=undefined} if(response.ok&&typeof raw==="object"&&raw!==null&&"ok" in raw&&raw.ok===true)return {ok:true} const description=typeof raw==="object"&&raw!==null&&"description" in raw&&typeof raw.description==="string"?raw.description:"Telegram не подтвердил приём сообщения" const rejected=typeof raw==="object"&&raw!==null&&"ok" in raw&&raw.ok===false&&"error_code" in raw&&typeof raw.error_code==="number"&&Number.isInteger(raw.error_code)&&raw.error_code>=400&&raw.error_code<600&&"description" in raw&&typeof raw.description==="string" diff --git a/backend/src/services/alerts-telegram-test.ts b/backend/src/services/alerts-telegram-test.ts new file mode 100644 index 00000000..a9f60327 --- /dev/null +++ b/backend/src/services/alerts-telegram-test.ts @@ -0,0 +1,22 @@ +import { testTelegramSchema, type TestTelegramBody } from "@mmapp/contracts/alerts" +import { formatAlertRuleTestTelegramText } from "./alerts-service.js" + +export type TelegramTestDependencies={ + readStored:()=>Promise<{botToken:string;chatId:string;messageThreadId:number|null}>; + send:(input:{token:string;chatId:string;text:string;messageThreadId?:number})=>Promise<{ok:true}|{ok:false;error:string;outcome?:"rejected"|"unknown"}>; +} +export class TelegramTestInputError extends Error {readonly statusCode=400} +export function resolveTelegramTest(input:TestTelegramBody,stored:Awaited>){ + const token=(input.token?.trim()||stored.botToken).trim(),chatId=(input.chatId?.trim()||stored.chatId||"").trim() + if(!token)throw new TelegramTestInputError("Не задан Bot Token (сохраните в БД или передайте в запросе)") + if(!chatId)throw new TelegramTestInputError("Не задан Chat ID") + const messageThreadId=input.messageThreadId??stored.messageThreadId??undefined + if(messageThreadId!==undefined&&(!Number.isInteger(messageThreadId)||messageThreadId<1||messageThreadId>2147483647))throw new TelegramTestInputError("ID темы Telegram не подтверждён") + return {token,chatId,messageThreadId,text:input.rulePreview?formatAlertRuleTestTelegramText(input.rulePreview):"MikroTik Manager — тест оповещений (Telegram)."} +} +/** One dispatch; only caller-supplied fences run between resolving and sending. */ +export async function performTelegramTest(raw:unknown,deps:TelegramTestDependencies,beforeDispatch:()=>Promise=async()=>{}){ + const input=testTelegramSchema.strict().parse(raw),payload=resolveTelegramTest(input,await deps.readStored()) + await beforeDispatch() + return deps.send(payload) +} diff --git a/backend/src/services/backup-content-bounds.test.ts b/backend/src/services/backup-content-bounds.test.ts new file mode 100644 index 00000000..a0ed6bb6 --- /dev/null +++ b/backend/src/services/backup-content-bounds.test.ts @@ -0,0 +1,29 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { S3Client } from "@aws-sdk/client-s3" +import { BackupContentLimitError, BackupReadGuardError, collectBackupBytes } from "./backup-content-bounds.js" +import { s3GetObject } from "./s3-backup-client.js" + +async function* bytes() { yield Buffer.from([0,255]); yield Buffer.from([128,1]) } +assert.deepEqual(await collectBackupBytes(bytes(),{maxBytes:4}),Buffer.from([0,255,128,1])) +await assert.rejects(collectBackupBytes(bytes(),{maxBytes:3}),BackupContentLimitError) +await assert.rejects(collectBackupBytes(bytes(),{maxBytes:4,authorize:async()=>{throw Error("private authorization detail")}}),BackupReadGuardError) +const cancelled=new AbortController();cancelled.abort() +await assert.rejects(collectBackupBytes(bytes(),{maxBytes:4,signal:cancelled.signal})) +let mode="normal",reads=0 +const server=createServer((_req,res)=>{ + reads++ + if(mode==="length")res.setHeader("Content-Length","99999") + res.write(Buffer.from([0,255])) + res.end(Buffer.from([128,1])) +}) +await new Promise(resolve=>server.listen(0,"127.0.0.1",resolve)) +const address=server.address();assert.ok(address&&typeof address!=="string") +const client=new S3Client({endpoint:`http://127.0.0.1:${address.port}`,region:"us-east-1",forcePathStyle:true,maxAttempts:1,credentials:{accessKeyId:"fixture",secretAccessKey:"fixture"}}) +try { + assert.deepEqual(await s3GetObject(client,"fixture","binary",{maxBytes:4}),Buffer.from([0,255,128,1])) + await assert.rejects(s3GetObject(client,"fixture","binary",{maxBytes:3}),BackupContentLimitError) + mode="length";await assert.rejects(s3GetObject(client,"fixture","binary",{maxBytes:4}),BackupContentLimitError) + const before=reads;await assert.rejects(s3GetObject(client,"fixture","binary",{maxBytes:4,signal:cancelled.signal}));assert.equal(reads,before) +} finally {client.destroy();await new Promise((resolve,reject)=>server.close(error=>error?reject(error):resolve()))} +console.log("✓ Bounded backup bytes: binary parity, stream/header limits, cancellation before HTTP and safe authorization refusal") diff --git a/backend/src/services/backup-content-bounds.ts b/backend/src/services/backup-content-bounds.ts new file mode 100644 index 00000000..1a518e57 --- /dev/null +++ b/backend/src/services/backup-content-bounds.ts @@ -0,0 +1,26 @@ +/** Optional limits for the shared REST/MCP backup reader. No truncation is accepted. */ +export interface BackupReadOptions { + maxBytes?: number + signal?: AbortSignal + authorize?: () => Promise + sourceRead?: (source: "s3" | "local") => void +} +export class BackupContentLimitError extends Error {} +export class BackupReadGuardError extends Error {} +export async function checkBackupRead(options: BackupReadOptions, bytes = 0) { + options.signal?.throwIfAborted() + if (options.maxBytes !== undefined && (!Number.isSafeInteger(options.maxBytes) || options.maxBytes < 1 || bytes > options.maxBytes)) throw new BackupContentLimitError("Backup exceeds download limit") + try { await options.authorize?.() } catch { throw new BackupReadGuardError("Backup download authorization changed") } + options.signal?.throwIfAborted() +} +export async function collectBackupBytes(source: AsyncIterable, options: BackupReadOptions): Promise { + const chunks: Buffer[] = [] + let bytes = 0 + for await (const chunk of source) { + bytes += chunk.byteLength + await checkBackupRead(options, bytes) + chunks.push(Buffer.from(chunk)) + } + await checkBackupRead(options, bytes) + return Buffer.concat(chunks, bytes) +} diff --git a/backend/src/services/backup-job-execution.ts b/backend/src/services/backup-job-execution.ts index e1f14fca..7c426347 100644 --- a/backend/src/services/backup-job-execution.ts +++ b/backend/src/services/backup-job-execution.ts @@ -8,14 +8,17 @@ export async function recordBackupEvent(input: AppendEventBody, append: (input: export async function processBackupJob(job: BackupJobStatusResponse, ids: string[], notes: string | undefined, deps: { create: (id: string, kind: "manual", notes?: string) => Promise event: (input: AppendEventBody) => Promise + signal?: AbortSignal }) { job.status = "running" job.startedAt = new Date().toISOString() for (const id of ids) { + deps.signal?.throwIfAborted() try { job.created.push(await deps.create(id, "manual", notes)) } catch (error) { job.failures.push({ serverId: id, error: error instanceof Error ? error.message : String(error) }) } finally { job.completed += 1 } } + deps.signal?.throwIfAborted() job.status = "done" job.finishedAt = new Date().toISOString() const uploadFailures = job.created.filter(item => item.uploadError) diff --git a/backend/src/services/backup-job-read.test.ts b/backend/src/services/backup-job-read.test.ts new file mode 100644 index 00000000..523ee3b3 --- /dev/null +++ b/backend/src/services/backup-job-read.test.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import type { BackupJobStatusResponse } from "@mmapp/contracts/backups" +import { projectScopedBackupJob as project } from "./backup-job-read.js" +import { createBackupJobStore, readBackupJob, invalidateBackupJobs } from "./backup-job-store.js" +const job: BackupJobStatusResponse = { id: "job", serverIds: ["1"], status: "queued", requestedAt: "2026-10-19T00:00:00Z", total: 1, completed: 0, created: [], failures: [], notes: "NEVER_JOB_SECRET" } +assert.equal(project(job,new Set([2]),0,1),undefined) +assert.equal(project({...job,serverIds:["1","2"],created:"bad"},new Set([1]),0,1),undefined) +assert.equal(project(job,new Set([1]),0,1)?.completed,0) +assert.equal(project(job,new Set([1]),0,1)?.terminal,false) +const complete = { ...job, status: "done" as const, completed: 1, finishedAt: "2026-10-19T00:01:00Z", created: [{ id: "backup", serverId: 1, serverName: "NEVER_JOB_SECRET", filename: "NEVER_JOB_SECRET", sizeBytes: 0, createdAt: job.requestedAt, kind: "manual" as const, storage: "both" as const, notes: "NEVER_JOB_SECRET", s3Key: "NEVER_JOB_SECRET", uploadError: "NEVER_JOB_SECRET" }] } +const safe=project(complete,new Set([1]),0,1)! +assert.equal(safe.hasUploadErrors,true);assert.equal(safe.hasFailures,false);assert.equal(safe.created.items[0].sizeBytes,0) +assert.equal(safe.created.items[0].localFileStatus,"unknown");assert.equal(JSON.stringify(safe).includes("NEVER_JOB_SECRET"),false) +assert.equal(project(complete,new Set([1]),1,1)?.created.items.length,0) +for (const invalid of [{...complete,completed:2},{...job,status:"done"},{...job,total:2},{...complete,created:[{...complete.created[0],serverId:2}]}])assert.throws(()=>project(invalid,new Set([1]),0,1)) +const first=createBackupJobStore(),second=createBackupJobStore() +try { + first.jobs.set(job.id,job);assert.equal(readBackupJob(job.id),job) + second.jobs.set(job.id,{...job});assert.equal(readBackupJob(job.id),undefined) + assert.equal(readBackupJob(job.id,first.jobs),job) + second.close();assert.equal(readBackupJob(job.id),job) + invalidateBackupJobs();assert.equal(first.jobs.size,0);assert.equal(readBackupJob(job.id),undefined) +}finally{first.close();second.close()} +console.log("Backup jobs: whole-job scope before parsing, zero/pending and malformed progress, secret-free pages, unknown local file state, ambiguous IDs and lifecycle invalidation passed") diff --git a/backend/src/services/backup-job-read.ts b/backend/src/services/backup-job-read.ts new file mode 100644 index 00000000..bccc4332 --- /dev/null +++ b/backend/src/services/backup-job-read.ts @@ -0,0 +1,19 @@ +import { backupJobResponseSchema } from "@mmapp/contracts/backups" + +/** Gate the entire job before parsing any results. A mixed job is not a partial + * view: its total/progress would disclose work on hidden servers. */ +export function projectScopedBackupJob(raw: unknown, allowed: ReadonlySet, offset: number, limit: number) { + if (!raw || typeof raw !== "object" || !("serverIds" in raw) || !Array.isArray(raw.serverIds) + || !raw.serverIds.length || raw.serverIds.some(id => typeof id !== "string" || !/^[1-9]\d*$/.test(id) || !Number.isSafeInteger(Number(id)) || !allowed.has(Number(id)))) return undefined + const job = backupJobResponseSchema.parse(raw) + if (job.serverIds.length !== job.total || new Set(job.serverIds).size !== job.total) throw new Error("Цели backup-задачи не соответствуют прогрессу") + const page = (items: T[]) => ({ items: items.slice(offset, offset + limit), total: items.length, nextOffset: offset + limit < items.length ? offset + limit : null }) + return { jobId: job.id, serverIds: job.serverIds.map(Number), status: job.status, total: job.total, completed: job.completed, + requestedAt: job.requestedAt, startedAt: job.startedAt ?? null, finishedAt: job.finishedAt ?? null, + terminal: job.status === "done" || job.status === "failed", hasFailures: job.failures.length > 0, hasUploadErrors: job.created.some(item => Boolean(item.uploadError)), + created: page(job.created.map(item => ({ id: item.id, serverId: item.serverId, createdAt: item.createdAt, sizeBytes: item.sizeBytes, kind: item.kind, storage: item.storage, localFileStatus: item.localFileStatus ?? "unknown", uploadErrorPresent: Boolean(item.uploadError) }))), + failures: page(job.failures.map(item => ({ serverId: item.serverId, error: "Резервное копирование не подтверждено" }))), + incomplete: job.created.length > limit || job.failures.length > limit, + source: "process-memory" as const, ephemeral: true, + } +} diff --git a/backend/src/services/backup-job-store.ts b/backend/src/services/backup-job-store.ts new file mode 100644 index 00000000..16d6f45b --- /dev/null +++ b/backend/src/services/backup-job-store.ts @@ -0,0 +1,18 @@ +import type { BackupJobStatusResponse } from "@mmapp/contracts/backups" + +// Jobs are process-local, just as the REST queue. Each app retains its own +// idempotency map; shared readers refuse IDs that are ambiguous across apps. +const stores = new Set>() +export function createBackupJobStore() { + const jobs = new Map() + stores.add(jobs) + return { jobs, close: () => { stores.delete(jobs); jobs.clear() } } +} +export function readBackupJob(jobId: string, store?: Map) { + if (store) return store.get(jobId) + const matches = [...stores].flatMap(jobs => jobs.has(jobId) ? [jobs.get(jobId)!] : []) + return matches.length === 1 ? matches[0] : undefined +} +export function invalidateBackupJobs() { + for (const jobs of stores) jobs.clear() +} diff --git a/backend/src/services/backup-restore.test.ts b/backend/src/services/backup-restore.test.ts index ad4b6ad4..3ef63b4b 100644 --- a/backend/src/services/backup-restore.test.ts +++ b/backend/src/services/backup-restore.test.ts @@ -1,8 +1,71 @@ import assert from "node:assert/strict" +import { createServer } from "node:http" import { test } from "node:test" import { MikrotikClient, MikrotikError } from "./mikrotik.js" import { assertRestorableBackupScript, BackupRestoreUnknownError, importBackupOnce } from "./backup-restore.js" +import { installManagedMutationSqlFixture } from "../test/fixtures/managed-mutation-sql.js" const makeClient = () => new MikrotikClient({ host: "router.invalid", port: 80, useSsl: false, verifySsl: false, username: "test", password: "test" }) + +test("restore imports only the exact verified upload, even if a similar file has the same size", async t => { + const routerId = 71 + const sql = installManagedMutationSqlFixture(routerId) + t.after(() => sql.restore()) + const script = "# копия\n/system identity\nset name=valid" + for (const mode of ["verified", "wrong-bytes", "wrong-directory", "duplicate-name"] as const) { + let stored = Buffer.alloc(0), writes = 0, reads = 0, imports = 0 + const name = "flash/restore.rsc" + const server = createServer(async (req, res) => { + const chunks: Buffer[] = [] + for await (const chunk of req) chunks.push(chunk) + const body = chunks.length ? JSON.parse(Buffer.concat(chunks).toString("utf8")) : {} + const reply = (output: unknown) => { + res.setHeader("Content-Type", "application/json") + res.end(JSON.stringify(output)) + } + if (req.method === "PUT" && req.url === "/rest/file") { + writes++ + assert.equal(body.name, name) + assert.equal(body.contents, script + "\n") + stored = Buffer.from(mode === "wrong-bytes" ? body.contents.replace("valid", "other") : body.contents) + return reply({}) + } + if (req.method === "GET" && req.url === "/rest/file") { + const entry = { ".id": "*FILE", name: mode === "wrong-directory" ? "disk/restore.rsc" : name, size: String(stored.length) } + return reply(mode === "duplicate-name" ? [entry, { ...entry, ".id": "*OTHER" }] : [entry]) + } + if (req.method === "POST" && req.url === "/rest/file/read") { + reads++ + assert.deepEqual(body, { file: name, offset: "0", "chunk-size": String(stored.length) }) + return reply([{ data: stored.toString("utf8") }]) + } + if (req.method === "POST" && req.url === "/rest/import") { + imports++ + assert.deepEqual(body, { "file-name": name }) + return reply({}) + } + res.statusCode = 500 + return reply({ error: "unexpected request" }) + }) + await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve) }) + try { + const address = server.address() + assert(address && typeof address === "object") + const router = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "test", password: "test" }, routerId) + const restore = async () => { + const uploaded = await router.uploadConfigurationScript("restore.rsc", script) + await importBackupOnce(uploaded, file => router.importUploadedFile(file)) + } + if (mode === "verified") await restore() + else await assert.rejects(restore(), mode === "wrong-bytes" ? /отличается от резервной копии/ : /не подтвердил имя, ID или размер/) + assert.equal(writes, 1) + assert.equal(reads, mode === "verified" || mode === "wrong-bytes" ? 1 : 0) + assert.equal(imports, mode === "verified" ? 1 : 0) + } finally { + await new Promise(resolve => server.close(() => resolve())) + } + } +}) + test("legacy partial exports are rejected while native and limited scripts remain usable", () => { for (const prefix of ["", "\uFEFF\r\n "]) assert.throws(() => assertRestorableBackupScript(prefix + "# synthetic export generated by MikrotikManager\n/system identity\nset name=legacy"), /частичным экспортом/) for (const script of ["# oct/14/2026 by RouterOS 7.24.5\n/system identity\nset name=native", "/interface bridge\nadd name=owned-probe", "# harmless fixture\n"]) assert.doesNotThrow(() => assertRestorableBackupScript(script)) diff --git a/backend/src/services/backup-service.ts b/backend/src/services/backup-service.ts index 298b0aa3..7d641394 100644 --- a/backend/src/services/backup-service.ts +++ b/backend/src/services/backup-service.ts @@ -1,7 +1,13 @@ +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" import { randomUUID } from "node:crypto" +import { privateSecretFingerprint } from "../modules/mcp/security.js" +import { mcpDatabaseRequestSignal } from "../modules/mcp/database-request-lease.js" +import { withPartitionWrite } from "./partition-write-transaction.js" +import { createReadStream } from "node:fs" +import { BackupContentLimitError, BackupReadGuardError, checkBackupRead, collectBackupBytes, type BackupReadOptions } from "./backup-content-bounds.js" import { mkdir, rm, stat, writeFile, readFile } from "node:fs/promises" import path from "node:path" -import { and, desc, eq, isNotNull } from "drizzle-orm" +import { and, desc, eq, isNotNull, sql } from "drizzle-orm" import type { BackupScheduleSettingsDto, BackupStorageSettingsDto, @@ -11,10 +17,11 @@ import type { import { mapBackupEntry } from "./backup-dto.js" import { pruneBackupRecords } from "./backup-prune.js" import { assertRestorableBackupScript, importBackupOnce } from "./backup-restore.js" +import { withConfigMutationLock } from "./config-mutation-lock.js" import { backupDeletionPath, deleteStoredBackup } from "./backup-deletion.js" import { withBackupLocalFileStates } from "./backup-local-file-state.js" import { assertBackupStorageConnectionChange, planBackupStorageUpdate } from "./backup-storage-update.js" -import { BackupStorageConflictError, withBackupStorageOperation, withBackupStorageSettingsUpdate } from "./backup-storage-operation.js" +import { BackupStorageConflictError, withBackupStorageOperation, withBackupStorageSettingsUpdate, isBackupStorageBusy } from "./backup-storage-operation.js" import { db } from "../db/index.js" import { parseJsonArray } from "../db/json.js" import { backupEntries, backupScheduleSettings, backupStorageSettings } from "../db/schema.js" @@ -172,7 +179,8 @@ export async function getBackupScheduleSettings(): Promise[0]) { return executeGuardedApplicationWrite({ kind: "backup-schedule", id: "1" }, "all", () => updateBackupScheduleSettingsImpl(patch)) } +async function updateBackupScheduleSettingsImpl(patch: Partial<{ enabled: boolean frequency: "daily" | "weekly" | "monthly" hour: number @@ -263,7 +271,18 @@ export async function getBackupStorageSettings(): Promise 0, busy: isBackupStorageBusy() } +} + +export function updateBackupStorageSettings(...args: Parameters) { return executeGuardedApplicationWrite({ kind: "backup-storage", id: "1" }, "all", () => updateBackupStorageSettingsImpl(...args)) } +async function updateBackupStorageSettingsImpl( patch: PutBackupStorageSettings, ): Promise { return withBackupStorageSettingsUpdate(() => updateStorageSettings(patch)) @@ -457,7 +476,7 @@ async function createBackupForServer(id: string, kind: BackupMeta["kind"], notes s3Key, uploadError, } - await db.insert(backupEntries).values({ + await withPartitionWrite(async ({ db: writer }) => writer.insert(backupEntries).values({ id: meta.id, serverId: meta.serverId, serverName: meta.serverName, @@ -470,7 +489,7 @@ async function createBackupForServer(id: string, kind: BackupMeta["kind"], notes s3Etag, uploadError: meta.uploadError ?? null, createdAt: meta.createdAt, - }) + }), { signal: mcpDatabaseRequestSignal() }) return meta } @@ -483,32 +502,51 @@ export async function pruneBackupsForServer(serverId: string, keepCount: number) return pruneBackupRecords(toDelete.map(row => row.id), deleteBackupRecord) } -export async function readBackupContent(meta: BackupMeta): Promise { +export async function readBackupContent(meta: BackupMeta, options: BackupReadOptions = {}): Promise { return withBackupStorageOperation(async () => { // The route may have read metadata before a concurrent deletion changed the connection. - const current = await getBackupById(meta.id) + await checkBackupRead(options) + const boundedRow = options.maxBytes === undefined ? undefined : (await db.select().from(backupEntries).where(and(eq(backupEntries.id,meta.id),sql`octet_length(to_jsonb(${backupEntries})::text)<=32768`)).limit(1))[0] + const current = options.maxBytes === undefined ? await getBackupById(meta.id) : boundedRow ? rowToMeta(boundedRow) : null + await checkBackupRead(options) if (!current) throw new Error("Бэкап не найден") - return readStoredBackupContent(current) + return readStoredBackupContent(current, options) }) } -async function readStoredBackupContent(meta: BackupMeta): Promise { +async function readStoredBackupContent(meta: BackupMeta, options: BackupReadOptions = {}): Promise { + await checkBackupRead(options) let remoteUnavailable = false if ((meta.storage === "s3" || meta.storage === "both") && meta.s3Key) { try { const cfg = await getS3ConfigIfEnabled() if (cfg) { const client = createS3ClientFromConfig(cfg) - return await s3GetObject(client, cfg.bucket, meta.s3Key) + const content = await s3GetObject(client, cfg.bucket, meta.s3Key, options) + options.sourceRead?.("s3") + return content } remoteUnavailable = true - } catch { + } catch (error) { + await checkBackupRead(options) + if (error instanceof BackupContentLimitError || error instanceof BackupReadGuardError) throw error remoteUnavailable = true /* fallback: локальная копия, если есть */ } } try { - return await readFile(backupDeletionPath(BACKUPS_DIR, meta.filename)) + await checkBackupRead(options) + const file = backupDeletionPath(BACKUPS_DIR, meta.filename) + if (options.maxBytes !== undefined) { + const info = await stat(file) + await checkBackupRead(options, info.size) + const content = await collectBackupBytes(createReadStream(file, { signal: options.signal }), options) + options.sourceRead?.("local") + return content + } + const content = await readFile(file) + options.sourceRead?.("local") + return content } catch (error) { const code = (error as NodeJS.ErrnoException).code if (code === "ENOENT" || code === "ENOTDIR") throw new Error(remoteUnavailable @@ -525,10 +563,13 @@ export async function restoreBackupToDevice(id: string): Promise<{ backupId: str const row = await getServerRowById(meta.serverId) if (!row) throw new Error("Сервер не найден") const content = await readBackupContent(meta) - assertRestorableBackupScript(content.toString("utf8")) + const script = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(content) + assertRestorableBackupScript(script) const client = MikrotikClient.fromServer(row) - const uploaded = await client.uploadTextFile(meta.filename, content.toString("utf8"), 60_000) - await importBackupOnce(uploaded, file => client.importUploadedFile(file)) + await withConfigMutationLock([row.id], async () => { + const uploaded = await client.uploadConfigurationScript(meta.filename, script, 60_000) + await importBackupOnce(uploaded, file => client.importUploadedFile(file)) + }) return { backupId: meta.id, serverId: row.id, filename: meta.filename, serverName: row.name } } diff --git a/backend/src/services/backup-storage-operation.ts b/backend/src/services/backup-storage-operation.ts index 142e6b17..4fc3861a 100644 --- a/backend/src/services/backup-storage-operation.ts +++ b/backend/src/services/backup-storage-operation.ts @@ -1,6 +1,7 @@ /** Serializes storage changes with catalog-producing operations in one backend process. */ let tail: Promise = Promise.resolve() let pending = 0 +export function isBackupStorageBusy() { return pending > 0 } export function withBackupStorageOperation(operation: () => Promise): Promise { pending += 1 const result = tail.then(operation).finally(() => { pending -= 1 }) diff --git a/backend/src/services/bandwidth-admission-process.sql-helper.ts b/backend/src/services/bandwidth-admission-process.sql-helper.ts new file mode 100644 index 00000000..982136f7 --- /dev/null +++ b/backend/src/services/bandwidth-admission-process.sql-helper.ts @@ -0,0 +1,149 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { lstat, mkdir, realpath } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import type { Pool } from "pg" +import type { BandwidthBinding, BandwidthJournal, BandwidthScope, BandwidthTransition } from "./bandwidth-admission.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +/** Owned fixture only. All DB/private module imports happen after this guard. */ +export function assertOwnedBandwidthSql(): void { + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_ADMISSION, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert.equal(url.hostname, "127.0.0.1") + assert.equal(url.username, "mcp_test") + assert(url.password.length > 0) + assert(Number.isSafeInteger(Number(url.port)) && Number(url.port) > 0 && Number(url.port) <= 65535) + assert.equal(url.search, ""); assert.equal(url.hash, "") + assert.equal(url.pathname, "/postgres") + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) + assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +} +/** Create only the literal state child of the runner's existing private temp + * directory. Never recursively create a path supplied by environment data. */ +export async function prepareOwnedBandwidthState(): Promise { + assertOwnedBandwidthSql() + const state = resolve(process.env.MCP_STATE_DIR!), parent = dirname(state) + const canonicalTmp = await realpath(tmpdir()), parentStat = await lstat(parent) + assert(parentStat.isDirectory() && !parentStat.isSymbolicLink()) + const canonicalParent = await realpath(parent) + assert.equal(dirname(canonicalParent), canonicalTmp) + assert.match(basename(canonicalParent), /^mm-mcp-test-[A-Za-z0-9_-]+$/) + try { await mkdir(state, { mode: 0o700 }) } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error } + const stateStat = await lstat(state) + assert(stateStat.isDirectory() && !stateStat.isSymbolicLink()) + const canonicalState = await realpath(state) + assert.equal(basename(canonicalState), "state") + assert.equal(dirname(canonicalState), canonicalParent) +} +export function canonical(value: unknown): string { + return JSON.stringify(value, (_key, item: unknown) => item && typeof item === "object" && !Array.isArray(item) + ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item) +} +export const fixtureMac = (value: unknown): string => createHmac("sha256", "owned-bandwidth-synthetic-fixture-key-v1").update(canonical(value)).digest("hex") +export const scope = (milliseconds = 10000): BandwidthScope => ({ deadlineAt: Date.now() + milliseconds }) +export function fixtureBinding(source = "node-A", destination = "node-B", extra: Partial = {}): BandwidthBinding { + const core = { attemptId: randomUUID(), callerKind: "rest-speed" as const, operationId: randomUUID(), + source: { canonicalNodeId: source, endpointProof: fixtureMac(["endpoint", source]) }, + destination: { canonicalNodeId: destination, endpointProof: fixtureMac(["endpoint", destination]) }, + authorityProof: fixtureMac("authority"), authorityGeneration: "owned-authority-v1", controlGeneration: "owned-control-v1", + expiresAt: Date.now() + 120000, durationSec: 2147483647, requestProof: fixtureMac("request"), commandProof: fixtureMac("command"), ...extra } + return { ...core, bindingProof: fixtureMac(core) } +} +export const completionProof = (binding: BandwidthBinding): string => fixtureMac({ binding: binding.bindingProof, terminal: "done testing" }) +export interface Harness { + journal: BandwidthJournal + register(binding: BandwidthBinding): void + markNoSend(binding: BandwidthBinding): void + fault(kind: "lost-commit" | "rollback-before-commit"): void + lastPid(): number + committedLosses(): number +} +export async function createOwnedBandwidthHarness(ownedPool?: Pool): Promise { + assertOwnedBandwidthSql() + const { pool } = await import("../db/index.js") + const targetPool = ownedPool ?? pool + const { createProbeStorageConnectionConnector } = await import("./server-probe-storage-connection.js") + const { createBandwidthAdmissionSqlJournal } = await import("./bandwidth-admission-storage.js") + const allowed = new Map(), noSend = new Set() + let nextFault = "", pid = 0, losses = 0 + const connector = createProbeStorageConnectionConnector(async () => { + const raw = await targetPool.connect() + pid = Number((await raw.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + return raw + }) + const connect = async (window: BandwidthScope): Promise => { + const owned = await connector(window) + const fault = nextFault; nextFault = "" + return { + release: destroy => owned.release(destroy), + async query(sql, values, options) { + const commit = /^COMMIT\s*;?$/i.test(sql.trim()) + if (commit && fault === "rollback-before-commit") throw Error("Owned failure BEFORE actual COMMIT") + const result = await owned.query(sql, values, options) + if (commit && fault === "lost-commit") { losses++; throw Error("Owned lost ACK AFTER actual COMMIT") } + return result + }, + } + } + const dependencies = { connect, now: Date.now, + authorize: async (binding: BandwidthBinding, window: BandwidthScope) => { + assert(Date.now() < window.deadlineAt); window.signal?.throwIfAborted() + assert.equal(allowed.get(binding.attemptId), binding.bindingProof) + }, + verifyEvidence: async (transition: BandwidthTransition, binding: BandwidthBinding, evidence: unknown, window: BandwidthScope) => { + assert(Date.now() < window.deadlineAt); window.signal?.throwIfAborted() + assert.equal(allowed.get(binding.attemptId), binding.bindingProof) + if (transition === "complete") assert.deepEqual(evidence, { terminalProof: completionProof(binding) }) + if (transition === "release-before-dispatch") { + assert(noSend.has(binding.attemptId), "Only fixture-owned non-dispatched attempts may release") + assert.equal((evidence as { proof?: string }).proof, "private-no-send") + } + }, + eventProof: async (event: unknown) => fixtureMac(event), + } + // One physical SQL journal instance owns one local reserve ACK. The harness + // multiplexes independent attempts without teaching fresh instances adoption. + const journals = new Map() + const owned = (binding: BandwidthBinding) => { + let value = journals.get(binding.attemptId) + if (!value) { value = createBandwidthAdmissionSqlJournal(dependencies); journals.set(binding.attemptId, value) } + return value + } + const journal: BandwidthJournal = { + reserve: (binding, window) => owned(binding).reserve(binding, window), + arm: (binding, window) => owned(binding).arm(binding, window), + complete: (binding, evidence, window) => owned(binding).complete(binding, evidence, window), + releaseBeforeDispatch: (binding, evidence, window) => owned(binding).releaseBeforeDispatch(binding, evidence, window), + quarantine: (binding, window) => owned(binding).quarantine(binding, window), + } + return { journal, register: binding => { allowed.set(binding.attemptId, binding.bindingProof) }, + markNoSend: binding => { noSend.add(binding.attemptId) }, fault: kind => { nextFault = kind }, + lastPid: () => pid, committedLosses: () => losses } +} + +if (process.env.MMAPP_BANDWIDTH_CHILD === "true") { + assertOwnedBandwidthSql() + const harness = await createOwnedBandwidthHarness() + const { closePool, pool } = await import("../db/index.js") + pool.on("error", () => { /* Intentional owned pg_terminate_backend fixtures. */ }) + process.on("message", (message: { id: number; command: string; binding?: BandwidthBinding }) => { + void (async () => { + if (message.command === "close") { await closePool(); process.send?.({ id: message.id, ok: true }); process.disconnect(); return } + const binding = message.binding! + harness.register(binding) + try { + const result = message.command === "reserve" ? await harness.journal.reserve(binding, scope()) + : message.command === "arm" ? await harness.journal.arm(binding, scope()) + : message.command === "complete" ? await harness.journal.complete(binding, { terminalProof: completionProof(binding) }, scope()) + : await harness.journal.quarantine(binding, scope()) + process.send?.({ id: message.id, ok: true, result, pid: harness.lastPid(), processId: process.pid }) + } catch { process.send?.({ id: message.id, ok: false, processId: process.pid }) } + })().catch(() => { process.exitCode = 1; process.disconnect() }) + }) + process.send?.({ ready: true, processId: process.pid }) +} diff --git a/backend/src/services/bandwidth-admission-schema.ts b/backend/src/services/bandwidth-admission-schema.ts new file mode 100644 index 00000000..c1f0dfe4 --- /dev/null +++ b/backend/src/services/bandwidth-admission-schema.ts @@ -0,0 +1,208 @@ +/** Fixed externally provisioned namespace; never derived from caller input. */ +export const BANDWIDTH_ADMISSION_CONTROL_NAMESPACE = "mmapp_control" as const + +/** Fresh, opt-in control proposal only. Not a normal migration or readiness claim. + * The mmapp_control namespace MUST already be provisioned independently. This + * DDL does not create that namespace, migrate public tables or copy old data. + * SQL enforces structure, immutable ownership and transition chains. It cannot + * authenticate HMACs, native completion or private no-send ownership: the trusted + * adapter must independently verify those before SQL. GUCs grant no privilege. + * No SECURITY DEFINER, trigger-disable mechanism, TTL reclaim or cascade exists. */ +export const BANDWIDTH_ADMISSION_PROPOSAL_DDL = ` +CREATE FUNCTION mmapp_control.bandwidth_admission_binding_valid( + b jsonb, attempt uuid, caller text, operation text, source text, destination text, + proof text, generation text +) RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT SET search_path = pg_catalog, mmapp_control AS $body$ +DECLARE field text; node jsonb; +BEGIN + IF jsonb_typeof(b) IS DISTINCT FROM 'object' + OR NOT b ?& ARRAY['attemptId','callerKind','operationId','source','destination','authorityProof','authorityGeneration','controlGeneration','expiresAt','durationSec','requestProof','commandProof','bindingProof'] + OR b - ARRAY['attemptId','callerKind','operationId','source','destination','authorityProof','authorityGeneration','controlGeneration','expiresAt','durationSec','requestProof','commandProof','bindingProof'] <> '{}'::jsonb THEN RETURN false; END IF; + FOREACH field IN ARRAY ARRAY['attemptId','callerKind','operationId','authorityProof','authorityGeneration','controlGeneration','requestProof','commandProof','bindingProof'] LOOP + IF jsonb_typeof(b->field) IS DISTINCT FROM 'string' THEN RETURN false; END IF; + END LOOP; + FOREACH field IN ARRAY ARRAY['authorityProof','requestProof','commandProof','bindingProof'] LOOP + IF (b->>field) !~ '^[a-f0-9]{64}$' THEN RETURN false; END IF; + END LOOP; + FOREACH field IN ARRAY ARRAY['operationId','authorityGeneration','controlGeneration'] LOOP + IF (b->>field) !~ '^[a-zA-Z0-9_.:-]{1,200}$' THEN RETURN false; END IF; + END LOOP; + FOREACH field IN ARRAY ARRAY['source','destination'] LOOP + node := b->field; + IF jsonb_typeof(node) IS DISTINCT FROM 'object' + OR NOT node ?& ARRAY['canonicalNodeId','endpointProof'] + OR node - ARRAY['canonicalNodeId','endpointProof'] <> '{}'::jsonb + OR jsonb_typeof(node->'canonicalNodeId') IS DISTINCT FROM 'string' + OR jsonb_typeof(node->'endpointProof') IS DISTINCT FROM 'string' + OR (node->>'canonicalNodeId') !~ '^[a-zA-Z0-9_.:-]{1,200}$' + OR (node->>'endpointProof') !~ '^[a-f0-9]{64}$' THEN RETURN false; END IF; + END LOOP; + IF (b->>'attemptId') !~* '^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$' + OR jsonb_typeof(b->'expiresAt') IS DISTINCT FROM 'number' + OR jsonb_typeof(b->'durationSec') IS DISTINCT FROM 'number' THEN RETURN false; END IF; + RETURN COALESCE((b->>'attemptId')::uuid = attempt + AND (b->>'callerKind') COLLATE "C" = caller COLLATE "C" + AND (b->>'operationId') COLLATE "C" = operation COLLATE "C" + AND (b->'source'->>'canonicalNodeId') COLLATE "C" = source COLLATE "C" + AND (b->'destination'->>'canonicalNodeId') COLLATE "C" = destination COLLATE "C" + AND source COLLATE "C" <> destination COLLATE "C" + AND b->>'bindingProof' = proof AND (b->>'controlGeneration') COLLATE "C" = generation COLLATE "C" + AND (b->>'expiresAt')::numeric BETWEEN 1 AND 9007199254740991 + AND trunc((b->>'expiresAt')::numeric) = (b->>'expiresAt')::numeric + AND (b->>'durationSec')::numeric BETWEEN 3 AND 2147483647 + AND trunc((b->>'durationSec')::numeric) = (b->>'durationSec')::numeric, false); +END; +$body$; + +CREATE TABLE mmapp_control.bandwidth_attempts ( + attempt_id uuid PRIMARY KEY, + caller_kind text COLLATE "C" NOT NULL CHECK (caller_kind IN ('rest-speed','scheduled-speed','mcp-speed','routing-measurement','rest-diagnostic','mcp-diagnostic','legacy-diagnostic','speed-inner')), + operation_id text COLLATE "C" NOT NULL CHECK (operation_id ~ '^[a-zA-Z0-9_.:-]{1,200}$'), + binding jsonb NOT NULL CHECK (octet_length(binding::text) <= 16384), + source_node text COLLATE "C" NOT NULL, + destination_node text COLLATE "C" NOT NULL, + binding_proof text COLLATE "C" NOT NULL CHECK (binding_proof ~ '^[a-f0-9]{64}$'), + control_generation text COLLATE "C" NOT NULL CHECK (control_generation ~ '^[a-zA-Z0-9_.:-]{1,200}$'), + state text COLLATE "C" NOT NULL CHECK (state IN ('reserved','armed','unknown_busy','released')), + resolution text COLLATE "C" CHECK (resolution IN ('remote_completion','proven_not_dispatched')), + terminal_proof text COLLATE "C" CHECK (terminal_proof ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + UNIQUE (caller_kind, operation_id), + CHECK (mmapp_control.bandwidth_admission_binding_valid(binding,attempt_id,caller_kind,operation_id,source_node,destination_node,binding_proof,control_generation)), + CHECK (COALESCE((state <> 'released' AND resolution IS NULL AND terminal_proof IS NULL) + OR (state = 'released' AND resolution = 'proven_not_dispatched' AND terminal_proof IS NULL) + OR (state = 'released' AND resolution = 'remote_completion' AND terminal_proof IS NOT NULL), false)) +); +CREATE TABLE mmapp_control.bandwidth_node_occupancy ( + node_id text COLLATE "C" PRIMARY KEY, + attempt_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_attempts(attempt_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + binding_proof text COLLATE "C" NOT NULL CHECK (binding_proof ~ '^[a-f0-9]{64}$'), + control_generation text COLLATE "C" NOT NULL CHECK (control_generation ~ '^[a-zA-Z0-9_.:-]{1,200}$') +); +CREATE INDEX bandwidth_node_occupancy_attempt_idx ON mmapp_control.bandwidth_node_occupancy(attempt_id); +CREATE TABLE mmapp_control.bandwidth_attempt_events ( + ack_id uuid PRIMARY KEY, + attempt_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_attempts(attempt_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + transition text COLLATE "C" NOT NULL CHECK (transition IN ('reserve','arm','complete','release-before-dispatch','quarantine')), + old_state text COLLATE "C" CHECK (old_state IN ('reserved','armed')), + new_state text COLLATE "C" NOT NULL CHECK (new_state IN ('reserved','armed','unknown_busy','released')), + binding_proof text COLLATE "C" NOT NULL CHECK (binding_proof ~ '^[a-f0-9]{64}$'), + control_generation text COLLATE "C" NOT NULL CHECK (control_generation ~ '^[a-zA-Z0-9_.:-]{1,200}$'), + evidence_proof text COLLATE "C" NOT NULL CHECK (evidence_proof ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + UNIQUE (attempt_id, transition), + CHECK (COALESCE((transition = 'reserve' AND old_state IS NULL AND new_state = 'reserved') + OR (transition = 'arm' AND old_state = 'reserved' AND new_state = 'armed') + OR (transition = 'complete' AND old_state = 'armed' AND new_state = 'released') + OR (transition = 'release-before-dispatch' AND old_state IN ('reserved','armed') AND new_state = 'released') + OR (transition = 'quarantine' AND old_state IN ('reserved','armed') AND new_state = 'unknown_busy'), false)) +); + +CREATE FUNCTION mmapp_control.bandwidth_admission_attempt_guard() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, mmapp_control AS $body$ +BEGIN + IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'bandwidth attempts cannot be deleted' USING ERRCODE='23514'; END IF; + IF TG_OP = 'INSERT' THEN + IF NEW.state <> 'reserved' OR NEW.resolution IS NOT NULL OR NEW.terminal_proof IS NOT NULL THEN RAISE EXCEPTION 'bandwidth attempt must start reserved' USING ERRCODE='23514'; END IF; + RETURN NEW; + END IF; + IF ROW(NEW.attempt_id,NEW.caller_kind,NEW.operation_id,NEW.binding,NEW.source_node,NEW.destination_node,NEW.binding_proof,NEW.control_generation,NEW.created_at) + IS DISTINCT FROM ROW(OLD.attempt_id,OLD.caller_kind,OLD.operation_id,OLD.binding,OLD.source_node,OLD.destination_node,OLD.binding_proof,OLD.control_generation,OLD.created_at) THEN + RAISE EXCEPTION 'bandwidth binding is immutable' USING ERRCODE='23514'; + END IF; + IF NOT ((OLD.state = 'reserved' AND NEW.state IN ('armed','unknown_busy','released')) OR (OLD.state = 'armed' AND NEW.state IN ('unknown_busy','released'))) + OR (NEW.resolution = 'remote_completion' AND OLD.state <> 'armed') THEN + RAISE EXCEPTION 'invalid bandwidth attempt transition' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE TRIGGER bandwidth_attempt_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_attempts FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_admission_attempt_guard(); + +CREATE FUNCTION mmapp_control.bandwidth_admission_occupancy_guard() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, mmapp_control AS $body$ +DECLARE owner mmapp_control.bandwidth_attempts%ROWTYPE; target mmapp_control.bandwidth_node_occupancy%ROWTYPE; +BEGIN + IF TG_OP = 'UPDATE' THEN RAISE EXCEPTION 'bandwidth occupancy cannot be updated' USING ERRCODE='23514'; END IF; + IF TG_OP = 'DELETE' THEN target := OLD; ELSE target := NEW; END IF; + SELECT * INTO owner FROM mmapp_control.bandwidth_attempts WHERE attempt_id = target.attempt_id FOR UPDATE; + IF NOT FOUND OR owner.binding_proof <> target.binding_proof OR owner.control_generation <> target.control_generation + OR target.node_id NOT IN (owner.source_node,owner.destination_node) + OR (TG_OP = 'INSERT' AND owner.state <> 'reserved') OR (TG_OP = 'DELETE' AND owner.state <> 'released') THEN + RAISE EXCEPTION 'bandwidth occupancy owner or state mismatch' USING ERRCODE='23514'; + END IF; + IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; + RETURN NEW; +END; +$body$; +CREATE TRIGGER bandwidth_occupancy_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_occupancy FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_admission_occupancy_guard(); + +CREATE FUNCTION mmapp_control.bandwidth_admission_event_guard() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, mmapp_control AS $body$ +DECLARE owner mmapp_control.bandwidth_attempts%ROWTYPE; +BEGIN + IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'bandwidth events are append only' USING ERRCODE='23514'; END IF; + SELECT * INTO owner FROM mmapp_control.bandwidth_attempts WHERE attempt_id = NEW.attempt_id FOR UPDATE; + IF NOT FOUND OR owner.binding_proof <> NEW.binding_proof OR owner.control_generation <> NEW.control_generation OR owner.state <> NEW.new_state THEN + RAISE EXCEPTION 'bandwidth event owner or state mismatch' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE TRIGGER bandwidth_event_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_attempt_events FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_admission_event_guard(); + +CREATE FUNCTION mmapp_control.bandwidth_admission_reject_truncate() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, mmapp_control AS $body$ +BEGIN RAISE EXCEPTION 'bandwidth journal cannot be truncated' USING ERRCODE='23514'; END; +$body$; +CREATE TRIGGER bandwidth_attempts_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_attempts FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE TRIGGER bandwidth_occupancy_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_node_occupancy FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE TRIGGER bandwidth_events_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_attempt_events FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); + +CREATE FUNCTION mmapp_control.bandwidth_admission_consistency_guard() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, mmapp_control AS $body$ +DECLARE target_id uuid; owner mmapp_control.bandwidth_attempts%ROWTYPE; occupied bigint; reserves bigint; arms bigint; terminals bigint; total bigint; terminal mmapp_control.bandwidth_attempt_events%ROWTYPE; +BEGIN + IF TG_OP = 'DELETE' THEN target_id := OLD.attempt_id; ELSE target_id := NEW.attempt_id; END IF; + SELECT * INTO owner FROM mmapp_control.bandwidth_attempts WHERE attempt_id = target_id FOR UPDATE; + IF NOT FOUND THEN RAISE EXCEPTION 'bandwidth attempt missing' USING ERRCODE='23514'; END IF; + SELECT count(*) INTO occupied FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id = target_id; + IF (owner.state = 'released' AND occupied <> 0) OR (owner.state <> 'released' AND occupied <> 2) + OR EXISTS (SELECT 1 FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id = target_id + AND (binding_proof <> owner.binding_proof OR control_generation <> owner.control_generation OR node_id NOT IN (owner.source_node,owner.destination_node))) THEN + RAISE EXCEPTION 'bandwidth occupancy pair is incomplete' USING ERRCODE='23514'; + END IF; + SELECT count(*),count(*) FILTER (WHERE transition='reserve'),count(*) FILTER (WHERE transition='arm'),count(*) FILTER (WHERE transition IN ('complete','release-before-dispatch','quarantine')) + INTO total,reserves,arms,terminals FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=target_id; + IF reserves <> 1 OR arms > 1 OR terminals > 1 OR total <> reserves+arms+terminals + OR EXISTS (SELECT 1 FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=target_id AND (binding_proof <> owner.binding_proof OR control_generation <> owner.control_generation)) THEN + RAISE EXCEPTION 'bandwidth event chain is incomplete' USING ERRCODE='23514'; + END IF; + IF owner.state = 'reserved' THEN + IF total <> 1 THEN RAISE EXCEPTION 'reserved bandwidth event mismatch' USING ERRCODE='23514'; END IF; + ELSIF owner.state = 'armed' THEN + IF arms <> 1 OR terminals <> 0 OR total <> 2 THEN RAISE EXCEPTION 'armed bandwidth event mismatch' USING ERRCODE='23514'; END IF; + ELSE + SELECT * INTO terminal FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=target_id AND transition IN ('complete','release-before-dispatch','quarantine'); + IF terminals <> 1 OR terminal.old_state IS DISTINCT FROM (CASE WHEN arms=1 THEN 'armed' ELSE 'reserved' END) + OR terminal.new_state <> owner.state + OR (owner.state='unknown_busy' AND terminal.transition <> 'quarantine') + OR (owner.state='released' AND owner.resolution='remote_completion' AND (terminal.transition <> 'complete' OR arms <> 1)) + OR (owner.state='released' AND owner.resolution='proven_not_dispatched' AND terminal.transition <> 'release-before-dispatch') THEN + RAISE EXCEPTION 'terminal bandwidth event mismatch' USING ERRCODE='23514'; + END IF; + END IF; + RETURN NULL; +END; +$body$; +CREATE CONSTRAINT TRIGGER bandwidth_attempt_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_attempts DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_admission_consistency_guard(); +CREATE CONSTRAINT TRIGGER bandwidth_occupancy_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_occupancy DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_admission_consistency_guard(); +CREATE CONSTRAINT TRIGGER bandwidth_event_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_attempt_events DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_admission_consistency_guard(); + +-- Structural guards also run under replication apply. Table owners/superusers +-- still control DDL; this is not role isolation or HMAC verification. +ALTER TABLE mmapp_control.bandwidth_attempts ENABLE ALWAYS TRIGGER bandwidth_attempt_guard; +ALTER TABLE mmapp_control.bandwidth_node_occupancy ENABLE ALWAYS TRIGGER bandwidth_occupancy_guard; +ALTER TABLE mmapp_control.bandwidth_attempt_events ENABLE ALWAYS TRIGGER bandwidth_event_guard; +ALTER TABLE mmapp_control.bandwidth_attempts ENABLE ALWAYS TRIGGER bandwidth_attempts_no_truncate; +ALTER TABLE mmapp_control.bandwidth_node_occupancy ENABLE ALWAYS TRIGGER bandwidth_occupancy_no_truncate; +ALTER TABLE mmapp_control.bandwidth_attempt_events ENABLE ALWAYS TRIGGER bandwidth_events_no_truncate; +ALTER TABLE mmapp_control.bandwidth_attempts ENABLE ALWAYS TRIGGER bandwidth_attempt_consistency; +ALTER TABLE mmapp_control.bandwidth_node_occupancy ENABLE ALWAYS TRIGGER bandwidth_occupancy_consistency; +ALTER TABLE mmapp_control.bandwidth_attempt_events ENABLE ALWAYS TRIGGER bandwidth_event_consistency; +` diff --git a/backend/src/services/bandwidth-admission-storage.ts b/backend/src/services/bandwidth-admission-storage.ts new file mode 100644 index 00000000..7441e7f6 --- /dev/null +++ b/backend/src/services/bandwidth-admission-storage.ts @@ -0,0 +1,158 @@ +/** Fixed mmapp_control journal proposal, outside application backup/restore. + * All relations and transaction lookup paths are fixed; caller search_path or + * public shadow tables cannot substitute for externally provisioned control. + * No default pool, schema installation, native I/O + * or production registration. A fixed host must supply authenticated control + * generation and private evidence verification; SQL does not verify HMAC keys. */ +import { randomUUID } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { BANDWIDTH_CALLER_KINDS, type BandwidthBinding, type BandwidthJournal, type BandwidthJournalAck, type BandwidthScope, type BandwidthTransition } from "./bandwidth-admission.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +const label=z.string().regex(/^[a-zA-Z0-9_.:-]{1,200}$/),hash=z.string().regex(/^[a-f0-9]{64}$/) +const uuid=z.string().regex(/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i) +const participant=z.object({canonicalNodeId:label,endpointProof:hash}).strict() +const bindingSchema=z.object({attemptId:uuid,callerKind:z.enum(BANDWIDTH_CALLER_KINDS),operationId:label,source:participant,destination:participant, + authorityProof:hash,authorityGeneration:label,controlGeneration:label,expiresAt:z.number().int().positive().safe(),durationSec:z.number().int().min(3).max(2147483647),requestProof:hash,commandProof:hash,bindingProof:hash}).strict() +const completionSchema=z.object({terminalProof:hash}).strict() +const noSendSchema=z.object({from:z.enum(["reserved","armed"]),proof:z.literal("private-no-send")}).strict() +function evidenceSnapshot(raw:unknown,schema:z.ZodType):Readonly{ + if(!raw||typeof raw!=="object"||Object.getPrototypeOf(raw)!==Object.prototype)throw new BandwidthAdmissionStorageRefused() + const descriptors=Object.getOwnPropertyDescriptors(raw) + if(Reflect.ownKeys(raw).length!==Object.keys(descriptors).length||Object.values(descriptors).some(item=>!("value"in item)||!item.enumerable))refuse() + return Object.freeze(schema.parse(structuredClone(raw))) +} +type Row=Record +type Sql={query(text:string,values?:unknown[]):Promise<{rows:Row[]}>} +type State="reserved"|"armed"|"unknown_busy"|"released" +export interface BandwidthAdmissionStorageDependencies { + connect(scope:BandwidthScope):Promise + now():number + /** Fresh control readiness/generation and exact source ownership on every SQL + * boundary. Cleanup verifies original ownership, not renewed execute rights. */ + authorize(binding:Readonly,scope:BandwidthScope,transition:BandwidthTransition):Promise + /** Fixed private source only: no-send must prove no transport invocation; + * terminal evidence must authenticate this original native response/binding. */ + verifyEvidence(transition:BandwidthTransition,binding:Readonly,evidence:unknown,scope:BandwidthScope):Promise + eventProof(event:unknown):Promise +} +export class BandwidthAdmissionStorageRefused extends Error {readonly automaticReplay=false;constructor(){super("Bandwidth journal refused")}} +export class BandwidthAdmissionStorageUnknown extends Error {readonly automaticReplay=false;constructor(){super("Bandwidth journal COMMIT outcome unknown; replay forbidden")}} +const refuse=():never=>{throw new BandwidthAdmissionStorageRefused()} +const sameId=(a:unknown,b:string)=>typeof a==="string"&&a.toLowerCase()===b.toLowerCase() + +/** Every physical lease and its facade expire together. In-flight COMMIT is + * never retried/rolled back after uncertainty; a late callback cannot send SQL. */ +async function transaction(deps:BandwidthAdmissionStorageDependencies,binding:Readonly,transition:BandwidthTransition,rawScope:BandwidthScope,work:(sql:Sql,scope:BandwidthScope)=>Promise):Promise{ + if(!Number.isSafeInteger(rawScope.deadlineAt))refuse() + const scope=Object.freeze({signal:rawScope.signal,deadlineAt:Math.min(rawScope.deadlineAt,deps.now()+10000,...(["reserve","arm"].includes(transition)?[binding.expiresAt]:[]))}) + let active=true,busy=false,released=false,commitSent=false,committed=false,begun=false,physical:ProbeStorageConnection|undefined + const close=(unsafe:boolean)=>{if(physical&&!released){released=true;try{physical.release(unsafe)}catch{/* No driver details. */}}} + const live=()=>{if(!active||released||scope.signal?.aborted||deps.now()>=scope.deadlineAt)refuse()} + const fence=async()=>{live();await deps.authorize(binding,scope,transition);live()} + let stop!:()=>void + const stopped=new Promise((_resolve,reject)=>{stop=()=>{active=false;close(true);reject(commitSent?new BandwidthAdmissionStorageUnknown():new BandwidthAdmissionStorageRefused())}}) + const timer=setTimeout(stop,Math.max(1,scope.deadlineAt-deps.now()));scope.signal?.addEventListener("abort",stop,{once:true}) + const query=async(text:string,values:unknown[]=[])=>{ + await fence();const connection=physical;if(!connection||busy)throw new BandwidthAdmissionStorageRefused();busy=true + try{if(text==="COMMIT")commitSent=true;const result=await connection.query(text,values,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-deps.now()))});if(text==="COMMIT")committed=true;await fence();return result} + finally{busy=false} + } + const task=Promise.resolve().then(async()=>{ + await fence();const late=await deps.connect(scope);physical=late + if(!active||scope.signal?.aborted||deps.now()>=scope.deadlineAt){close(true);refuse()} + await query("BEGIN");begun=true + await query("SELECT pg_catalog.set_config('search_path','pg_catalog',true),pg_catalog.set_config('TimeZone','UTC',true),pg_catalog.set_config('statement_timeout','5000ms',true),pg_catalog.set_config('synchronous_commit','on',true)") + const nodes=[binding.source.canonicalNodeId,binding.destination.canonicalNodeId].sort() + for(const node of nodes){const lock=await query("SELECT pg_catalog.pg_try_advisory_xact_lock(pg_catalog.hashtextextended($1,764301)) AS locked",["bandwidth-node:"+node]);if(lock.rows.length!==1||lock.rows[0].locked!==true)refuse()} + const sql:Sql=Object.freeze({async query(text:string,values?:unknown[]){if(/^(?:BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|START|PREPARE|SET)\b/i.test(text.trim())||text.includes(";"))refuse();return query(text,values)}}) + const result=await work(sql,scope);await fence();await query("COMMIT");return result + }) + try{return await Promise.race([task,stopped])} + catch(error){ + if(commitSent)throw new BandwidthAdmissionStorageUnknown() + if(begun&&active&&!busy&&physical&&!released){try{await Promise.race([physical.query("ROLLBACK",[],{timeoutMs:5000}),stopped])}catch{close(true)}} + throw error instanceof BandwidthAdmissionStorageRefused?error:new BandwidthAdmissionStorageRefused() + }finally{active=false;clearTimeout(timer);scope.signal?.removeEventListener("abort",stop);close(!committed||busy)} +} + +export function createBandwidthAdmissionSqlJournal(rawDeps:BandwidthAdmissionStorageDependencies):BandwidthJournal { + const deps=Object.freeze({...rawDeps}) + let original:Readonly|undefined,phase:"new"|"reserving"|"reserved"|"arming"|"armed"|"closing"|"closed"|"uncertain"="new",quarantineAttempted=false + const capture=(raw:Readonly)=>{ + const value=bindingSchema.parse(structuredClone(raw));if(value.source.canonicalNodeId===value.destination.canonicalNodeId)refuse() + if(original&&!isDeepStrictEqual(value,original))refuse() + if(!original){Object.freeze(value.source);Object.freeze(value.destination);original=Object.freeze(value)} + return original + } + const material=(id:string,binding:Readonly,transition:BandwidthTransition,oldState:State|null,newState:State,terminalProof:string|null)=>({ackId:id,attemptId:binding.attemptId.toLowerCase(),transition,oldState,newState,bindingProof:binding.bindingProof,controlGeneration:binding.controlGeneration,terminalProof}) + const event=async(sql:Sql,binding:Readonly,transition:BandwidthTransition,oldState:State|null,newState:State,terminalProof:string|null):Promise=>{ + const id=randomUUID(),proof=await deps.eventProof(material(id,binding,transition,oldState,newState,terminalProof));hash.parse(proof) + const inserted=await sql.query("INSERT INTO mmapp_control.bandwidth_attempt_events(ack_id,attempt_id,transition,old_state,new_state,binding_proof,control_generation,evidence_proof) VALUES($1,$2,$3,$4,$5,$6,$7,$8) RETURNING ack_id::text",[id,binding.attemptId,transition,oldState,newState,binding.bindingProof,binding.controlGeneration,proof]) + if(inserted.rows.length!==1||inserted.rows[0].ack_id!==id)refuse() + return {outcome:"new",transition,ackId:id,attemptId:binding.attemptId,bindingProof:binding.bindingProof,controlGeneration:binding.controlGeneration} + } + const owned=async(sql:Sql,binding:Readonly,expected:readonly State[])=>{ + const rows=(await sql.query("SELECT * FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1 FOR UPDATE",[binding.attemptId])).rows + if(rows.length!==1)refuse();const row=rows[0] + if(!sameId(row.attempt_id,binding.attemptId)||!isDeepStrictEqual(row.binding,binding)||row.binding_proof!==binding.bindingProof||row.control_generation!==binding.controlGeneration||row.source_node!==binding.source.canonicalNodeId||row.destination_node!==binding.destination.canonicalNodeId||!expected.includes(row.state as State))refuse() + // The attempt row is locked above. Occupancy INSERT/DELETE guards acquire + // that same owner row, so they cannot race this snapshot. Avoid FOR UPDATE + // here: runtime deliberately has no occupancy UPDATE privilege. + const nodes=(await sql.query('SELECT node_id,attempt_id,binding_proof,control_generation FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1 ORDER BY node_id COLLATE "C"',[binding.attemptId])).rows + const expectedNodes=[binding.source.canonicalNodeId,binding.destination.canonicalNodeId].sort() + if(nodes.length!==2||nodes.some((node,index)=>node.node_id!==expectedNodes[index]||!sameId(node.attempt_id,binding.attemptId)||node.binding_proof!==binding.bindingProof||node.control_generation!==binding.controlGeneration))refuse() + const events=(await sql.query("SELECT * FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=$1 ORDER BY ack_id LIMIT 5",[binding.attemptId])).rows + if(!events.length||events.length>3||new Set(events.map(item=>item.transition)).size!==events.length)refuse() + const transitions=events.map(item=>item.transition).sort().join(",") + const valid=row.state==="reserved"?transitions==="reserve":row.state==="armed"?transitions==="arm,reserve":row.state==="unknown_busy"?["quarantine,reserve","arm,quarantine,reserve"].includes(transitions):false + if(!valid)refuse() + for(const item of events){ + if(!sameId(item.attempt_id,binding.attemptId)||item.binding_proof!==binding.bindingProof||item.control_generation!==binding.controlGeneration||!uuid.safeParse(item.ack_id).success)refuse() + const expectedProof=await deps.eventProof(material(String(item.ack_id),binding,item.transition as BandwidthTransition,item.old_state as State|null,item.new_state as State,null)) + if(!hash.safeParse(expectedProof).success||item.evidence_proof!==expectedProof)refuse() + } + return row + } + const change=async(sql:Sql,binding:Readonly,from:State,to:State,resolution:string|null,terminalProof:string|null)=>{ + const rows=(await sql.query("UPDATE mmapp_control.bandwidth_attempts SET state=$2,resolution=$3,terminal_proof=$4 WHERE attempt_id=$1 AND state=$5 AND binding_proof=$6 AND control_generation=$7 AND binding=$8::jsonb RETURNING attempt_id",[binding.attemptId,to,resolution,terminalProof,from,binding.bindingProof,binding.controlGeneration,JSON.stringify(binding)])).rows + if(rows.length!==1||!sameId(rows[0].attempt_id,binding.attemptId))refuse() + if(to==="released"){ + const deleted=(await sql.query("DELETE FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1 AND binding_proof=$2 AND control_generation=$3 AND node_id=ANY($4::text[]) RETURNING node_id",[binding.attemptId,binding.bindingProof,binding.controlGeneration,[binding.source.canonicalNodeId,binding.destination.canonicalNodeId]])).rows + if(deleted.length!==2||new Set(deleted.map(row=>row.node_id)).size!==2)refuse() + } + } + const transition=async(raw:Readonly,kind:BandwidthTransition,scope:BandwidthScope,evidence:unknown,from:State,to:State,resolution:string|null,terminalProof:string|null)=>{ + const binding=capture(raw) + return transaction(deps,binding,kind,scope,async(sql,current)=>{ + await owned(sql,binding,[from]);await deps.verifyEvidence(kind,binding,evidence,current) + await change(sql,binding,from,to,resolution,terminalProof) + return event(sql,binding,kind,from,to,terminalProof) + }) + } + const journal:BandwidthJournal={ + async reserve(raw,scope){ + if(phase!=="new")refuse();const binding=capture(raw);phase="reserving" + try{const ack=await transaction(deps,binding,"reserve",scope,async(sql,current)=>{ + await deps.verifyEvidence("reserve",binding,undefined,current) + const inserted=await sql.query("INSERT INTO mmapp_control.bandwidth_attempts(attempt_id,caller_kind,operation_id,binding,source_node,destination_node,binding_proof,control_generation,state) VALUES($1,$2,$3,$4::jsonb,$5,$6,$7,$8,'reserved') RETURNING attempt_id",[binding.attemptId,binding.callerKind,binding.operationId,JSON.stringify(binding),binding.source.canonicalNodeId,binding.destination.canonicalNodeId,binding.bindingProof,binding.controlGeneration]) + if(inserted.rows.length!==1||!sameId(inserted.rows[0].attempt_id,binding.attemptId))refuse() + const nodes=await sql.query("INSERT INTO mmapp_control.bandwidth_node_occupancy(node_id,attempt_id,binding_proof,control_generation) VALUES($1,$3,$4,$5),($2,$3,$4,$5) RETURNING node_id",[binding.source.canonicalNodeId,binding.destination.canonicalNodeId,binding.attemptId,binding.bindingProof,binding.controlGeneration]);if(nodes.rows.length!==2)refuse() + return event(sql,binding,"reserve",null,"reserved",null) + });phase="reserved";return ack}catch(error){phase="uncertain";throw error} + }, + async arm(raw,scope){if(phase!=="reserved")refuse();phase="arming";try{const ack=await transition(raw,"arm",scope,undefined,"reserved","armed",null,null);phase="armed";return ack}catch(error){phase="uncertain";throw error}}, + async complete(raw,evidence,scope){if(phase!=="armed")refuse();const captured=evidenceSnapshot(evidence,completionSchema);phase="closing";try{const ack=await transition(raw,"complete",scope,captured,"armed","released","remote_completion",captured.terminalProof);phase="closed";return ack}catch(error){phase="uncertain";throw error}}, + async releaseBeforeDispatch(raw,evidence,scope){if(!["reserved","armed"].includes(phase))refuse();const captured=evidenceSnapshot(evidence,noSendSchema);if(captured.from!==phase)refuse();const from=phase as "reserved"|"armed";phase="closing";try{const ack=await transition(raw,"release-before-dispatch",scope,captured,from,"released","proven_not_dispatched",null);phase="closed";return ack}catch(error){phase="uncertain";throw error}}, + async quarantine(raw,scope){ + if(!original||!["reserved","armed","uncertain"].includes(phase)||quarantineAttempted)refuse();quarantineAttempted=true;const binding=capture(raw);phase="uncertain" + return transaction(deps,binding,"quarantine",scope,async(sql,current)=>{ + const row=await owned(sql,binding,["reserved","armed"]);await deps.verifyEvidence("quarantine",binding,undefined,current) + await change(sql,binding,row.state as State,"unknown_busy",null,null) + return event(sql,binding,"quarantine",row.state as State,"unknown_busy",null) + }) + }, + } + return Object.freeze(journal) +} diff --git a/backend/src/services/bandwidth-admission.sql.test.ts b/backend/src/services/bandwidth-admission.sql.test.ts new file mode 100644 index 00000000..d867f531 --- /dev/null +++ b/backend/src/services/bandwidth-admission.sql.test.ts @@ -0,0 +1,462 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { fork, execFile } from "node:child_process" +import { once } from "node:events" +import { createHash, randomUUID } from "node:crypto" +import { createServer, request } from "node:http" +import { readFile, writeFile } from "node:fs/promises" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import { promisify } from "node:util" +import type { BandwidthBinding } from "./bandwidth-admission.js" +import { assertOwnedBandwidthSql, canonical, completionProof, createOwnedBandwidthHarness, fixtureBinding, fixtureMac, prepareOwnedBandwidthState, scope } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +await prepareOwnedBandwidthState() +const { pool, closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +const { BANDWIDTH_ADMISSION_PROPOSAL_DDL } = await import("./bandwidth-admission-schema.js") +pool.on("error", () => { /* An owned test deliberately terminates a released adapter backend. */ }) +const cases: string[] = [], limitations: string[] = [] +const children: ReturnType[] = [] +const tables = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events"] as const +const hash = (value: unknown) => createHash("sha256").update(canonical(value)).digest("hex") +async function snapshot(query = pool) { + const result: Record = {} + for (const name of tables) { + const values = (await query.query(`SELECT to_jsonb(t) AS row FROM mmapp_control.${name} t ORDER BY to_jsonb(t)::text`)).rows.map(row => row.row) + result[name] = { rows: values.length, sha256: hash(values) } + } + return result +} +const occupancy = async (binding: BandwidthBinding) => (await pool.query("SELECT node_id,attempt_id FROM mmapp_control.bandwidth_node_occupancy WHERE node_id=ANY($1::text[]) ORDER BY node_id", [[binding.source.canonicalNodeId, binding.destination.canonicalNodeId]])).rows +const fresh = (prefix: string) => fixtureBinding(prefix + ":A", prefix + ":B") +const barrier = () => { let release!: () => void; const reached = new Promise(resolve => { release = resolve }); return { reached, release } } +async function child() { + const processChild = fork(fileURLToPath(new URL("./bandwidth-admission-process.sql-helper.ts", import.meta.url)), [], { + execArgv: ["--import", "tsx"], stdio: ["ignore", "inherit", "inherit", "ipc"], env: { ...process.env, MMAPP_BANDWIDTH_CHILD: "true" }, + }) + children.push(processChild) + await Promise.race([once(processChild, "message"), once(processChild, "exit").then(() => { throw Error("Owned bandwidth child exited before ready") })]) + let id = 0 + return async (command: string, binding?: BandwidthBinding): Promise<{ ok: boolean; pid?: number; processId?: number }> => { + const expected = ++id + return new Promise((resolve, reject) => { + const timeout = setTimeout(() => { cleanup(); reject(Error("Owned IPC deadline")) }, 15000) + const cleanup = () => { clearTimeout(timeout); processChild.off("message", response) } + const response = (value: unknown) => { const result = value as { id: number; ok: boolean }; if (result.id === expected) { cleanup(); resolve(result) } } + processChild.on("message", response) + processChild.send({ id: expected, command, binding }) + }) + } +} +async function verifyEvents() { + const rows = (await pool.query(`SELECT e.*,a.terminal_proof FROM mmapp_control.bandwidth_attempt_events e JOIN mmapp_control.bandwidth_attempts a USING(attempt_id) ORDER BY e.ack_id`)).rows + for (const event of rows) assert.equal(event.evidence_proof, fixtureMac({ ackId: event.ack_id, attemptId: event.attempt_id, + transition: event.transition, oldState: event.old_state, newState: event.new_state, bindingProof: event.binding_proof, + controlGeneration: event.control_generation, terminalProof: event.transition === "complete" ? event.terminal_proof : null })) + return rows.length +} +async function verifyGuardMetadata(query = pool) { + const triggers = (await query.query(`SELECT t.tgname,t.tgenabled::text AS enabled FROM pg_catalog.pg_trigger t + JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + WHERE n.nspname='mmapp_control' AND c.relname=ANY($1::text[]) AND NOT t.tgisinternal ORDER BY t.tgname`, [[...tables]])).rows + assert.equal(triggers.length, 9); assert(triggers.every(row => row.enabled === "A")) + const functions = (await query.query(`SELECT p.proname,p.proconfig FROM pg_catalog.pg_proc p + JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace + WHERE n.nspname='mmapp_control' AND left(p.proname,length('bandwidth_admission_'))='bandwidth_admission_' ORDER BY p.proname`)).rows + assert.equal(functions.length, 6) + for (const fn of functions) assert((fn.proconfig as string[] | null)?.some(value => value.replaceAll(" ", "") === "search_path=pg_catalog,mmapp_control")) + return { triggers, functions } +} +try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + // Externally provisioned namespace in this disposable fixture only. Neither + // normal migrations nor the journal factory may create/enroll it. + await pool.query("CREATE SCHEMA mmapp_control") + await pool.query(BANDWIDTH_ADMISSION_PROPOSAL_DDL) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + const h = await createOwnedBandwidthHarness() + const reserve = async (binding: BandwidthBinding) => { h.register(binding); return h.journal.reserve(binding, scope()) } + const complete = async (binding: BandwidthBinding) => h.journal.complete(binding, { terminalProof: completionProof(binding) }, scope()) + { + const b = fresh("rollback") + h.register(b); h.fault("rollback-before-commit") + await assert.rejects(h.journal.reserve(b, scope())) + assert.equal((await occupancy(b)).length, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0].n, 0) + cases.push("reserve rollback before real COMMIT leaves neither half-pair nor attempt") + } + { + const one = await child(), two = await child(), b = fresh("process") + const first = await one("reserve", b); assert(first.ok) + const overlap = fixtureBinding(b.destination.canonicalNodeId, "process:C") + assert.equal((await two("reserve", overlap)).ok, false) + assert.equal((await two("reserve", fixtureBinding(b.destination.canonicalNodeId, b.source.canonicalNodeId))).ok, false) + const independent = fixtureBinding("process:C", "process:D"), parallel = await two("reserve", independent) + assert(parallel.ok); assert.notEqual(first.processId, parallel.processId); assert.notEqual(first.pid, parallel.pid) + assert.equal((await occupancy(b)).length, 2); assert.equal((await occupancy(independent)).length, 2) + assert.equal((await two("reserve", b)).ok, false) + assert((await one("arm", b)).ok); assert((await one("complete", b)).ok) + await one("close"); await two("close") + cases.push("two independent processes/backend PIDs: A-B blocks B-C/reversed B-A; disjoint C-D succeeds; repeat has no new ACK") + } + { + const a = await createOwnedBandwidthHarness(), b = await createOwnedBandwidthHarness() + const left = fresh("race"), right = fixtureBinding(left.destination.canonicalNodeId, "race:C") + a.register(left); b.register(right) + const results = await Promise.allSettled([a.journal.reserve(left, scope()), b.journal.reserve(right, scope())]) + assert.equal(results.filter(row => row.status === "fulfilled").length, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_node_occupancy WHERE node_id LIKE 'race:%'")).rows[0].n, 2) + cases.push("concurrent independent connections reserve intersecting pairs atomically: exactly one winner") + } + { + const b = fresh("reserve-ack") + h.register(b); h.fault("lost-commit") + await assert.rejects(h.journal.reserve(b, scope())) + assert.equal((await occupancy(b)).length, 2) + const observer = await child(); assert.equal((await observer("reserve", fixtureBinding(b.source.canonicalNodeId, "reserve-ack:C"))).ok, false); await observer("close") + cases.push("actual reserve COMMIT then lost ACK retains pair; independent process cannot reclaim") + } + { + const b = fresh("arm-ack"); await reserve(b); h.fault("lost-commit") + await assert.rejects(h.journal.arm(b, scope())) + assert.equal((await pool.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0].state, "armed") + assert.equal((await occupancy(b)).length, 2) + const restart = await createOwnedBandwidthHarness(); restart.register(b) + await assert.rejects(restart.journal.reserve(b, scope())) + cases.push("actual arm COMMIT then lost ACK: persisted armed pair survives fresh store and has no repeat reserve ACK") + } + { + const b = fresh("terminate"), received = barrier(), endRemote = barrier() + const server = createServer((_req, res) => { received.release(); void endRemote.reached.then(() => { if (!res.destroyed) res.end(JSON.stringify([{ status: "done testing" }])) }) }) + server.listen(0, "127.0.0.1"); server.unref(); await once(server, "listening") + const address = server.address(); assert(address && typeof address !== "string") + await reserve(b); await h.journal.arm(b, scope()) + const armPid = h.lastPid() + const req = request({ host: "127.0.0.1", port: address.port, path: "/synthetic-bandwidth", method: "POST" }) + req.on("error", () => {}); req.on("socket", socket => socket.unref()); req.end(); await received.reached + const { Client } = await import("pg") + const observer = new Client({ connectionString: process.env.DATABASE_URL }); await observer.connect() + try { + assert.notEqual(Number((await observer.query("SELECT pg_backend_pid() AS pid")).rows[0].pid), armPid) + assert.equal((await observer.query("SELECT pg_terminate_backend($1) AS killed", [armPid])).rows[0].killed, true) + } finally { await observer.end() } + const restart = await child(); assert.equal((await restart("reserve", fixtureBinding(b.destination.canonicalNodeId, "terminate:C"))).ok, false); await restart("close") + req.destroy(); endRemote.release(); await new Promise(resolve => server.close(() => resolve())) + await h.journal.quarantine(b, scope()); assert.equal((await occupancy(b)).length, 2) + cases.push("real pg_terminate_backend of committed arm backend AFTER loopback dispatch, while remote held running: new process remains blocked; socket close/quarantine retains pair") + } + for (const mode of ["lost-commit", "rollback-before-commit"] as const) { + const b = fresh("quarantine-" + mode); await reserve(b); await h.journal.arm(b, scope()); h.fault(mode) + await assert.rejects(h.journal.quarantine(b, scope())); assert.equal((await occupancy(b)).length, 2) + cases.push("quarantine " + mode + " never clears armed occupancy") + } + { + const b = fresh("complete-ack"); await reserve(b); await h.journal.arm(b, scope()); h.fault("lost-commit") + await assert.rejects(complete(b)); assert.equal((await occupancy(b)).length, 0) + assert.equal((await pool.query("SELECT resolution FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0].resolution, "remote_completion") + const next = fixtureBinding(b.source.canonicalNodeId, b.destination.canonicalNodeId); await reserve(next) + const before = await snapshot() + await assert.rejects(h.journal.quarantine(b, scope())); await assert.rejects(complete(b)) + h.markNoSend(b); await assert.rejects(h.journal.releaseBeforeDispatch(b, { from: "armed", proof: "private-no-send" }, scope())) + assert.deepEqual(await snapshot(), before) + cases.push("real completion COMMIT ACK loss releases both only with terminal proof; stale complete/quarantine/no-send never clears newer owner") + } + { + const b = fresh("proof"); await reserve(b); await h.journal.arm(b, scope()) + await assert.rejects(h.journal.complete(b, { terminalProof: "f".repeat(64) }, scope())) + await assert.rejects(h.journal.releaseBeforeDispatch(b, { from: "armed", proof: "private-no-send" }, scope())) + assert.equal((await occupancy(b)).length, 2) + const changed = fixtureBinding(b.source.canonicalNodeId, "proof:C", { source: { ...b.source, endpointProof: fixtureMac("changed epoch") } }) + h.register(changed); await assert.rejects(h.journal.reserve(changed, scope())) + cases.push("forged terminal/no-send evidence refused; changed endpoint epoch does not free canonical node") + } + { + const b = fresh("evidence-bad-first"); await reserve(b); await h.journal.arm(b, scope()) + const evidence = { terminalProof: "f".repeat(64) } + const pending = h.journal.complete(b, evidence, scope()) + evidence.terminalProof = completionProof(b) + await assert.rejects(pending) + assert.equal((await occupancy(b)).length, 2) + const current = (await pool.query("SELECT state,terminal_proof FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0] + assert.equal(current.state, "armed"); assert.equal(current.terminal_proof, null) + cases.push("actual SQL mutable evidence TOCTOU: initially invalid completion changed to valid after call still refuses, preserving both occupied nodes and NULL terminal proof") + } + { + const b = fresh("evidence-valid-first"); await reserve(b); await h.journal.arm(b, scope()) + const originalProof = completionProof(b), evidence = { terminalProof: originalProof } + const pending = h.journal.complete(b, evidence, scope()) + evidence.terminalProof = "f".repeat(64) + await pending + assert.equal((await occupancy(b)).length, 0) + const current = (await pool.query("SELECT state,terminal_proof FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0] + assert.equal(current.state, "released"); assert.equal(current.terminal_proof, originalProof) + cases.push("actual SQL mutable evidence TOCTOU: initially valid completion changed to invalid after call commits only captured valid proof and releases exact pair") + } + { + const b = fresh("evidence-no-send"); await reserve(b); h.markNoSend(b) + const evidence = { from: "reserved" as const, proof: "private-no-send" as const } + const pending = h.journal.releaseBeforeDispatch(b, evidence, scope()) + // Mutate caller memory after invocation without making the typed fixture + // provide a different contract at the call boundary. + Object.assign(evidence, { from: "armed", proof: "forged-after-call" }) + await pending + assert.equal((await occupancy(b)).length, 0) + const current = (await pool.query("SELECT state,resolution,terminal_proof FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0] + assert.equal(current.state, "released"); assert.equal(current.resolution, "proven_not_dispatched"); assert.equal(current.terminal_proof, null) + const event = (await pool.query("SELECT old_state FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=$1 AND transition='release-before-dispatch'", [b.attemptId])).rows[0] + assert.equal(event.old_state, "reserved") + cases.push("actual SQL mutable no-send evidence TOCTOU: caller mutation after call cannot change captured reserved release, resolution or event source state") + } + { + const b = fresh("delete"), id = Number((await pool.query("INSERT INTO servers(name,host,enabled) VALUES('owned-bw-delete','127.0.0.1',false) RETURNING id")).rows[0].id) + await reserve(b); await pool.query("DELETE FROM servers WHERE id=$1", [id]); assert.equal((await occupancy(b)).length, 2) + await assert.rejects(pool.query("DELETE FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])) + assert.equal((await occupancy(b)).length, 2) + cases.push("server DELETE does not cascade into canonical occupancy; active attempt DELETE rejected and pair preserved") + } + { + const b = fresh("expiry"); await reserve(b); await h.journal.arm(b, scope()); await h.journal.quarantine(b, scope()) + const newer = fixtureBinding(b.source.canonicalNodeId, "expiry:C", { controlGeneration: "new-owner-control", authorityGeneration: "new-credential" }) + h.register(newer); await assert.rejects(h.journal.reserve(newer, scope())); assert.equal((await occupancy(b)).length, 2) + const expired = { ...b, expiresAt: Date.now() - 1 }; h.register(expired) + await assert.rejects(h.journal.arm(expired, scope())) + cases.push("quarantined canonical nodes cannot be reclaimed by new control/credential generation; expired altered binding cannot rearm") + } + { + const b = fresh("cancel"); await reserve(b); h.markNoSend(b) + const outcomes = await Promise.allSettled([h.journal.arm(b, scope()), h.journal.releaseBeforeDispatch(b, { from: "reserved", proof: "private-no-send" }, scope())]) + assert.equal(outcomes.filter(value => value.status === "fulfilled").length, 1) + const row = (await pool.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0] + assert.equal((await occupancy(b)).length, row.state === "released" ? 0 : 2) + cases.push("same-instance local admission arm versus reserved cancel race: one local winner; not evidence of a database CAS race") + } + { + // Structural SQL fixture only: production evidence/private capability checks + // are not replaced by this direct UPDATE. Both branches supply valid event + // HMACs so the database transition/CAS/pair constraints are tested in full. + const b = fresh("physical-cas"); await reserve(b) + const { Client } = await import("pg") + const first = new Client({ connectionString: process.env.DATABASE_URL }), second = new Client({ connectionString: process.env.DATABASE_URL }) + await first.connect(); await second.connect() + const start = barrier(), pids: number[] = [] + let begun = 0 + const compete = async (client: typeof first, release: boolean): Promise => { + await client.query("BEGIN") + pids.push(Number((await client.query("SELECT pg_backend_pid() AS pid")).rows[0].pid)) + if (++begun === 2) start.release() + await start.reached + try { + const next = release ? "released" : "armed", transition = release ? "release-before-dispatch" : "arm" + const changed = await client.query("UPDATE mmapp_control.bandwidth_attempts SET state=$2,resolution=$3 WHERE attempt_id=$1 AND state='reserved' RETURNING attempt_id", [b.attemptId, next, release ? "proven_not_dispatched" : null]) + if (!changed.rows.length) { await client.query("ROLLBACK"); return false } + assert.equal(changed.rows.length, 1) + if (release) assert.equal((await client.query("DELETE FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1 RETURNING node_id", [b.attemptId])).rows.length, 2) + const ackId = randomUUID() + const evidence = fixtureMac({ ackId, attemptId: b.attemptId, transition, oldState: "reserved", newState: next, + bindingProof: b.bindingProof, controlGeneration: b.controlGeneration, terminalProof: null }) + await client.query("INSERT INTO mmapp_control.bandwidth_attempt_events(ack_id,attempt_id,transition,old_state,new_state,binding_proof,control_generation,evidence_proof) VALUES($1,$2,$3,'reserved',$4,$5,$6,$7)", [ackId, b.attemptId, transition, next, b.bindingProof, b.controlGeneration, evidence]) + await client.query("COMMIT") + return true + } catch (error) { await client.query("ROLLBACK").catch(() => {}); throw error } + } + try { + const winners = await Promise.all([compete(first, false), compete(second, true)]) + assert.equal(new Set(pids).size, 2) + assert.equal(winners.filter(Boolean).length, 1) + const result = (await pool.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0] + assert.equal((await occupancy(b)).length, result.state === "released" ? 0 : 2) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=$1", [b.attemptId])).rows[0].n, 2) + } finally { await first.end(); await second.end() } + cases.push("actual two-client distinct-backend reserved UPDATE CAS race after shared BEGIN barrier: arm versus release, one committed winner, loser rows=0/ROLLBACK, complete event HMAC and 0-or-2 occupancy") + } + { + const b = fresh("immutable-guards"); await reserve(b) + const before = await snapshot() + await assert.rejects(pool.query("UPDATE mmapp_control.bandwidth_attempts SET binding=jsonb_set(binding,'{durationSec}','3'::jsonb) WHERE attempt_id=$1", [b.attemptId])) + await assert.rejects(pool.query("UPDATE mmapp_control.bandwidth_node_occupancy SET node_id='foreign-node' WHERE attempt_id=$1", [b.attemptId])) + await assert.rejects(pool.query("UPDATE mmapp_control.bandwidth_node_occupancy SET attempt_id=$2 WHERE attempt_id=$1", [b.attemptId, randomUUID()])) + await assert.rejects(pool.query("DELETE FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1", [b.attemptId])) + await assert.rejects(pool.query("UPDATE mmapp_control.bandwidth_attempt_events SET evidence_proof=$2 WHERE attempt_id=$1", [b.attemptId, "f".repeat(64)])) + await assert.rejects(pool.query("DELETE FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=$1", [b.attemptId])) + await assert.rejects(pool.query("DELETE FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])) + await assert.rejects(pool.query("TRUNCATE mmapp_control.bandwidth_attempts,mmapp_control.bandwidth_node_occupancy,mmapp_control.bandwidth_attempt_events")) + assert.deepEqual(await snapshot(), before) + cases.push("actual DML guards refuse immutable binding, node identity/owner replacement, active occupancy DELETE, event UPDATE/DELETE, attempt DELETE and full journal TRUNCATE; exact rows unchanged") + } + { + const b = fresh("replica-guards"); await reserve(b) + const before = await snapshot() + const client = await pool.connect() + try { + for (const sql of ["DELETE FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", "DELETE FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1", "TRUNCATE mmapp_control.bandwidth_attempts,mmapp_control.bandwidth_node_occupancy,mmapp_control.bandwidth_attempt_events"]) { + await client.query("BEGIN") + try { + // Disposable superuser fixture only: prove ENABLE ALWAYS survives + // replica mode. This does not claim application role isolation. + await client.query("SET LOCAL session_replication_role=replica") + await assert.rejects(client.query(sql, sql.includes("$1") ? [b.attemptId] : [])) + } finally { await client.query("ROLLBACK") } + } + } finally { client.release() } + await verifyGuardMetadata() + assert.deepEqual(await snapshot(), before) + cases.push("owned replica mode in three separate rolled-back transactions cannot DELETE active attempt/occupancy or TRUNCATE journal; nine ALWAYS triggers and six fixed search_path functions verified in pg_catalog") + } + { + const b = fixtureBinding("real-expiry:A", "real-expiry:B", { expiresAt: Date.now() + 1000 }) + await reserve(b) + await new Promise(resolve => setTimeout(resolve, Math.max(1, b.expiresAt - Date.now() + 20))) + await assert.rejects(h.journal.arm(b, scope())) + await h.journal.quarantine(b, scope()) + assert.equal((await occupancy(b)).length, 2) + assert.equal((await pool.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0].state, "unknown_busy") + cases.push("real wall-clock original execute expiry refuses arm; bounded original cleanup can quarantine without freeing pair") + } + { + const b = fresh("blocked-deadline"), blocker = await pool.connect() + try { + await blocker.query("BEGIN"); await blocker.query("LOCK mmapp_control.bandwidth_node_occupancy IN ACCESS EXCLUSIVE MODE") + h.register(b) + await assert.rejects(h.journal.reserve(b, scope(200))) + const failedPid = h.lastPid() + await blocker.query("ROLLBACK") + assert.equal((await blocker.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0].n, 0) + const goneBy = Date.now() + 2000 + let remaining = 1 + while (remaining && Date.now() < goneBy) { + remaining = Number((await blocker.query("SELECT count(*)::int AS n FROM pg_stat_activity WHERE pid=$1", [failedPid])).rows[0].n) + if (remaining) await new Promise(resolve => setTimeout(resolve, 20)) + } + assert.equal(remaining, 0) + assert.equal((await occupancy(b)).length, 0) + } finally { await blocker.query("ROLLBACK").catch(() => {}); blocker.release() } + cases.push("real relation lock and 200ms owned deadline destroy blocked adapter backend; after unlock no late COMMIT/occupancy") + } + { + const b = fixtureBinding("uuid-case:A", "uuid-case:B", { attemptId: randomUUID().toUpperCase() }) + assert.equal((await reserve(b)).attemptId, b.attemptId) + assert.equal((await h.journal.arm(b, scope())).attemptId, b.attemptId) + assert.equal((await complete(b)).attemptId, b.attemptId) + assert.equal((await occupancy(b)).length, 0) + assert.equal((await pool.query("SELECT attempt_id::text FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [b.attemptId])).rows[0].attempt_id, b.attemptId.toLowerCase()) + cases.push("uppercase original UUID spelling is preserved in new ACKs while PostgreSQL identity and event HMACs use canonical UUID") + } + { + // Deliberately misleading PUBLIC objects belong only to this fixture. + // Real control SQL must be fixed-qualified, independently of search_path. + for (const name of tables) { + await pool.query(`CREATE TABLE public.${name}(sentinel text NOT NULL)`) + await pool.query(`INSERT INTO public.${name} VALUES('PUBLIC-SHADOW-MUST-STAY-UNCHANGED')`) + } + const { Pool } = await import("pg") + const shadowPool = new Pool({ connectionString: process.env.DATABASE_URL, options: "-c search_path=public,pg_catalog,mmapp_control", max: 2 }) + try { + const fixture = await createOwnedBandwidthHarness(shadowPool), b = fresh("search-path") + fixture.register(b) + await fixture.journal.reserve(b, scope()); await fixture.journal.arm(b, scope()) + assert.equal((await occupancy(b)).length, 2) + await fixture.journal.complete(b, { terminalProof: completionProof(b) }, scope()) + for (const name of tables) assert.deepEqual((await pool.query(`SELECT sentinel FROM public.${name}`)).rows, [{ sentinel: "PUBLIC-SHADOW-MUST-STAY-UNCHANGED" }]) + } finally { + await shadowPool.end() + for (const name of tables) await pool.query(`DROP TABLE public.${name}`) + } + cases.push("public shadow tables plus public-first search_path cannot stand in for fixed mmapp_control journal; reserve/arm/complete touch only control namespace") + } + { + const { Pool } = await import("pg"), url = new URL(process.env.DATABASE_URL!) + const dbName = "bandwidth_missing_" + randomUUID().replaceAll("-", "") + await pool.query(`CREATE DATABASE "${dbName}" TEMPLATE template0`) + url.pathname = "/" + dbName + const missing = new Pool({ connectionString: url.toString(), max: 2 }) + const namespaceInventory = async () => (await missing.query(`SELECT n.nspname,c.relname,c.relkind::text AS kind FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname IN ('public','mmapp_control') ORDER BY n.nspname,c.relname`)).rows + try { + const before = await namespaceInventory(), fixture = await createOwnedBandwidthHarness(missing), b = fresh("missing-control") + fixture.register(b); await assert.rejects(fixture.journal.reserve(b, scope())) + assert.deepEqual(await namespaceInventory(), before) + assert.equal((await missing.query("SELECT count(*)::int AS n FROM pg_catalog.pg_namespace WHERE nspname='mmapp_control'")).rows[0].n, 0) + // Existing public impostors still cannot make an erased/new database an + // enrolled source. No source-generation issuer is manufactured here. + for (const name of tables) await missing.query(`CREATE TABLE public.${name}(sentinel text)`) + const shadowed = await namespaceInventory(), another = await createOwnedBandwidthHarness(missing), next = fresh("missing-shadow") + another.register(next); await assert.rejects(another.journal.reserve(next, scope())) + assert.deepEqual(await namespaceInventory(), shadowed) + } finally { await missing.end(); await pool.query(`DROP DATABASE "${dbName}"`) } + cases.push("new/erased owned database lacking mmapp_control refuses journal reserve without ACK or object creation; PUBLIC impostors cannot auto-enroll it") + } + let eventCount = await verifyEvents(); assert(eventCount > 0); assert(h.committedLosses() >= 3) + cases.push("every persisted transition event verified with keyed fixture HMAC including terminal completion proof") + if (process.env.MMAPP_OWN_BANDWIDTH_RESTORE === "true") { + const execute = promisify(execFile), url = new URL(process.env.DATABASE_URL!) + const archive = resolve(process.env.MCP_STATE_DIR!, "bandwidth-proposal.dump"), dbName = "bandwidth_restore_" + randomUUID().replaceAll("-", "") + const env = { ...process.env, PGHOST: url.hostname, PGPORT: url.port, PGUSER: url.username, PGPASSWORD: decodeURIComponent(url.password) } + const { runPrivateDatabaseDump } = await import("./database-dump-connection.js") + const { restorePartitionedDatabase } = await import("./system-database-partition-restore.js") + const commandIo = async (program: string, args: string[]): Promise => { + try { return (await execute(program, args, { env, encoding: "buffer", timeout: 30000, maxBuffer: 32 * 1048576, windowsHide: true })).stdout } + catch { throw Error("Owned PostgreSQL archive command failed: " + program) } + } + const markerId = Number((await pool.query("INSERT INTO public.servers(name,host,enabled) VALUES('owned-bandwidth-public-before','127.0.0.1',false) RETURNING id")).rows[0].id) + const safeArchive = resolve(process.env.MCP_STATE_DIR!, "bandwidth-public-only.dump") + assert((await pool.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_attempts WHERE state='armed'")).rows[0].n > 0) + assert((await pool.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_attempts WHERE state='unknown_busy'")).rows[0].n > 0) + const appDump = await runPrivateDatabaseDump(url.toString(), { authorize: async () => {} }) + await writeFile(safeArchive, appDump, { flag: "wx", mode: 0o600 }) + const safeToc = await commandIo("pg_restore", ["--list", safeArchive]); assert(!safeToc.toString("utf8").includes("mmapp_control")) + // This busy pair did not exist when the application backup was created. + // A public rollback must preserve the NEWER control state as well. + const postBackup = fresh("after-backup") + await reserve(postBackup); await h.journal.arm(postBackup, scope()) + const preserved = await snapshot(), preservedGuards = await verifyGuardMetadata() + await pool.query("UPDATE public.servers SET name='owned-bandwidth-public-after' WHERE id=$1", [markerId]) + let safeReplacements = 0 + await restorePartitionedDatabase(safeArchive, url.toString(), { command: commandIo, + prepareSql: file => writeFile(file, "", { flag: "wx", mode: 0o600 }), beforeTargetReplacement: () => { safeReplacements++ } }) + assert.equal(safeReplacements, 1) + assert.equal((await pool.query("SELECT name FROM public.servers WHERE id=$1", [markerId])).rows[0].name, "owned-bandwidth-public-before") + assert.deepEqual(await snapshot(), preserved); assert.deepEqual(await verifyGuardMetadata(), preservedGuards) + const afterAppRestore = await createOwnedBandwidthHarness(), blocked = fixtureBinding(postBackup.source.canonicalNodeId, "after-app-restore:C") + afterAppRestore.register(blocked); await assert.rejects(afterAppRestore.journal.reserve(blocked, scope())) + assert.deepEqual(await snapshot(), preserved) + cases.push("actual application pg_dump excludes mmapp_control; actual public-only restore rolls public marker back while preserving NEWER post-backup armed pair and exact unknown/control hashes plus nine ALWAYS/six fixed-path guards; fresh overlapping reserve still refused") + const expected = await snapshot(), expectedGuards = await verifyGuardMetadata() + await execute("pg_dump", ["-Fc", "--no-owner", "-f", archive, "-d", "postgres"], { env }) + let unsafeCommands = 0, unsafePreparations = 0, unsafeReplacements = 0 + await assert.rejects(restorePartitionedDatabase(archive, url.toString(), { + command: async (program, args) => { unsafeCommands++; return commandIo(program, args) }, + prepareSql: async () => { unsafePreparations++ }, beforeTargetReplacement: () => { unsafeReplacements++ }, + })) + assert.equal(unsafeCommands, 1); assert.equal(unsafePreparations, 0); assert.equal(unsafeReplacements, 0) + assert.deepEqual(await snapshot(), expected) + assert.equal((await pool.query("SELECT name FROM public.servers WHERE id=$1", [markerId])).rows[0].name, "owned-bandwidth-public-before") + cases.push("actual full archive containing mmapp_control rejected by shared restore immediately after TOC, before prepare/decode/target replacement; control and public marker unchanged") + await pool.query(`CREATE DATABASE "${dbName}"`) + const { Pool } = await import("pg"), targetUrl = new URL(url); targetUrl.pathname = "/" + dbName + const restored = new Pool({ connectionString: targetUrl.toString() }) + try { + await execute("pg_restore", ["--no-owner", "--no-privileges", "--exit-on-error", "-d", dbName, archive], { env }) + assert.deepEqual(await snapshot(restored), expected) + assert.deepEqual(await verifyGuardMetadata(restored), expectedGuards) + assert.equal(Number((await restored.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + const content = await readFile(archive); content[0] ^= 0xff + const bad = resolve(process.env.MCP_STATE_DIR!, "bandwidth-proposal-tampered.dump"); await writeFile(bad, content) + await assert.rejects(execute("pg_restore", ["--list", bad], { env })) + cases.push("explicit full control-snapshot clone (NOT application restore) through real pg_dump/pg_restore into separate owned DB preserves exact proposal hashes/unknown occupancy, nine ALWAYS triggers, six fixed search_path functions and 39 markers; corrupted header rejected") + } finally { await restored.end(); await pool.query(`DROP DATABASE "${dbName}"`) } + } else limitations.push("Owned proposal dump/restore not requested") + eventCount = await verifyEvents() + limitations.push("No production native transport/broker integration or schema/control enrollment: SQL proposal only") + limitations.push("No real remote-completion/physical identity enrollment; terminal evidence is explicitly synthetic keyed fixture proof") + limitations.push("No aged retention/automatic GC integration; no automatic unknown release policy is supplied") + limitations.push("Deleting synthetic servers tests canonical-key independence; there is no implemented server-to-physical-node mapping") + limitations.push("Production source-generation issuer, authenticated external provisioning/READY and least-privilege connection/restore-role isolation are unproven; no automatic enrollment") + console.log("Owned bandwidth proposal SQL acceptance:", JSON.stringify({ proposalOnly: true, normalMigrationCount: MIGRATION_MANIFEST.length, normalEnrollmentChanged: false, + productionRestoreAccepted: false, runtimeDispatchIntegrated: false, nativeChrCalls: 0, actualCommitAckLosses: h.committedLosses(), eventCount, cases, limitations })) +} finally { + for (const processChild of children) if (processChild.connected) processChild.kill() + await closePool() +} diff --git a/backend/src/services/bandwidth-admission.test.ts b/backend/src/services/bandwidth-admission.test.ts new file mode 100644 index 00000000..8ccc5d01 --- /dev/null +++ b/backend/src/services/bandwidth-admission.test.ts @@ -0,0 +1,246 @@ +import assert from "node:assert/strict" +import { createHmac } from "node:crypto" +import { test } from "node:test" +import { setImmediate as turn, setTimeout as delay } from "node:timers/promises" +import { createBandwidthAdmission, BandwidthAdmissionError, type BandwidthAdmissionInput, type BandwidthAdmissionDependencies, type BandwidthBinding, type BandwidthDispatchCapability, type BandwidthJournalAck, type BandwidthTransition } from "./bandwidth-admission.js" + +const canonical = (value: unknown): string => Array.isArray(value) ? "[" + value.map(canonical).join(",") + "]" : value && typeof value === "object" ? "{" + Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value) +const proof = async (purpose: string, value: unknown) => createHmac("sha256", "test-key-not-a-human-credential").update(purpose + ":" + canonical(value)).digest("hex") +const deferred = () => { let resolve!: (value: T) => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const hasCode = (code: string) => (error: unknown) => error instanceof BandwidthAdmissionError && error.code === code +function fixture() { + const input: BandwidthAdmissionInput = { attemptId: "11111111-1111-4111-8111-111111111111", callerKind: "rest-speed", operationId: "run-1", request: { runId: "run-1", rate: 100 }, source: { canonicalNodeId: "physical:A", endpointProof: "a".repeat(64) }, destination: { canonicalNodeId: "physical:B", endpointProof: "b".repeat(64) }, authorityProof: "c".repeat(64), authorityGeneration: "actor-7", controlGeneration: "enrolled-2", expiresAt: Date.now() + 10000, durationSec: 10, nativeBody: { address: "192.0.2.2", user: "destination-user", password: "destination-secret", protocol: "tcp", direction: "both", duration: "10s", "local-tx-speed": "1000000" } } + const events: string[] = [], faults = new Map() + let occupancy = "free", sends = 0, authorized = true, endpointsCurrent = true, ackNumber = 0, lastBinding: Readonly | undefined + let sendBehavior = async (): Promise => [{ status: "done testing", "tx-total-average": "100" }] + let armDelay: Promise | undefined + let authorizeHook: (() => void) | undefined + let completeHook: (() => void) | undefined + let terminalHook: (() => void) | undefined + const ack = (transition: BandwidthTransition, binding: Readonly): BandwidthJournalAck => { + const kind = faults.get(transition) + if (kind === "throw") throw new Error("lost transaction ACK") + if (kind === "missing") return undefined as unknown as BandwidthJournalAck + return { outcome: kind === "existing" ? "existing" as "new" : "new", transition, ackId: kind === "duplicate" ? "ack-1" : `ack-${++ackNumber}`, attemptId: kind === "wrong" ? "foreign" : binding.attemptId, bindingProof: binding.bindingProof, controlGeneration: binding.controlGeneration } + } + const deps: BandwidthAdmissionDependencies = { + now: Date.now, cleanupBudgetMs: 100, + keyedProof: async (purpose, value) => { if (purpose === "bandwidth-terminal-v1") terminalHook?.(); return proof(purpose, value) }, + authorize: async binding => { events.push("authorize"); authorizeHook?.(); if (!authorized) throw new Error("revoked"); return { authorityProof: binding.authorityProof, authorityGeneration: binding.authorityGeneration, controlGeneration: binding.controlGeneration, expiresAt: binding.expiresAt } }, + endpoints: async binding => { events.push("endpoints"); return { source: { endpointProof: endpointsCurrent ? binding.source.endpointProof : "f".repeat(64), canonicalNodeId: binding.source.canonicalNodeId }, destination: { ...binding.destination } } }, + journal: { + reserve: async binding => { events.push("reserve"); lastBinding = binding; assert.equal(occupancy, "free", "a different attempt cannot reclaim busy nodes"); occupancy = "reserved"; return ack("reserve", binding) }, + arm: async binding => { events.push("arm"); assert.equal(occupancy, "reserved"); occupancy = "armed"; if (armDelay) await armDelay; return ack("arm", binding) }, + complete: async (binding, evidence, scope) => { events.push("complete"); completeHook?.(); assert.match(evidence.terminalProof, /^[a-f0-9]{64}$/); assert.equal(scope.signal, undefined, "cleanup does not inherit execute cancellation"); assert.equal(occupancy, "armed"); occupancy = "free"; return ack("complete", binding) }, + releaseBeforeDispatch: async (binding, evidence, scope) => { events.push("release-before-dispatch"); assert.equal(evidence.proof, "private-no-send"); assert.equal(sends, 0); assert.equal(scope.signal, undefined); assert.equal(occupancy, evidence.from); occupancy = "free"; return ack("release-before-dispatch", binding) }, + quarantine: async binding => { events.push("quarantine"); if (occupancy === "free") throw new Error("old attempt no longer owns occupancy"); occupancy = "unknown-busy"; return ack("quarantine", binding) }, + }, + send: async (_binding, body, scope) => { events.push("send"); sends++; assert.equal(occupancy, "armed"); assert.equal(body.password, input.nativeBody.password); assert.ok(scope.deadlineAt <= input.expiresAt); return sendBehavior() }, + } + return { input, deps, events, faults, gate: () => createBandwidthAdmission(input, deps), options: () => ({ deadlineAt: Date.now() + 5000 }), sends: () => sends, occupancy: () => occupancy, binding: () => lastBinding!, revoke: () => { authorized = false }, staleEndpoints: () => { endpointsCurrent = false }, send: (fn: typeof sendBehavior) => { sendBehavior = fn }, delayArm: (promise: Promise) => { armDelay = promise }, onAuthorize: (fn: () => void) => { authorizeHook = fn }, onComplete: (fn: () => void) => { completeHook = fn }, onTerminal: (fn: () => void) => { terminalHook = fn } } +} + +test("pure construction performs no I/O; new acknowledged arm permits exactly one fixed send", async () => { + const f = fixture(), gate = f.gate(); assert.equal(f.events.length, 0) + const cap = await gate.reserveAndArm(f.options()); assert.equal(f.sends(), 0); assert.equal(f.occupancy(), "armed") + const result = await gate.dispatch(cap) + assert.equal(result.remoteCompletionConfirmed, true); assert.equal(f.occupancy(), "free"); assert.equal(f.sends(), 1) + assert.deepEqual(gate.status(), { phase: "released", remoteCompletionConfirmed: true }) + await assert.rejects(gate.dispatch(cap), hasCode("REFUSED")); await assert.rejects(gate.reserveAndArm(f.options()), hasCode("REFUSED")) + assert.equal(f.events.filter(event => event === "arm").length, 1) + const index = f.events.indexOf("send"); assert.deepEqual(f.events.slice(index - 3, index + 1), ["authorize", "endpoints", "authorize", "send"]) +}) + +test("missing, existing, mismatched or duplicate ACK never mints/adopts a dispatch capability", async () => { + for (const transition of ["reserve", "arm"] as const) for (const fault of ["missing", "existing", "wrong", "throw", ...(transition === "arm" ? ["duplicate"] : [])]) { + const f = fixture(); f.faults.set(transition, fault); const gate = f.gate() + await assert.rejects(gate.reserveAndArm(f.options()), hasCode("PERSISTENCE_UNKNOWN")) + assert.equal(f.sends(), 0); assert.equal(gate.status().phase, "unknown-busy"); assert.notEqual(f.occupancy(), "free") + await assert.rejects(gate.reserveAndArm(f.options()), hasCode("REFUSED")) + assert.equal(f.events.filter(event => event === transition).length, 1, "no repeated CAS after lost ACK") + } +}) + +test("capability cannot be copied, serialized, forged, transferred to another gate or reused", async () => { + const f = fixture(), gate = f.gate(), cap = await gate.reserveAndArm(f.options()) + for (const fake of [{}, { ...cap }, JSON.parse(JSON.stringify(cap)), { attemptId: f.input.attemptId }]) await assert.rejects(gate.dispatch(fake as BandwidthDispatchCapability), hasCode("REFUSED")) + const foreign = fixture().gate(); await assert.rejects(foreign.dispatch(cap), hasCode("REFUSED")) + assert.equal(f.sends(), 0); await gate.cancel(cap) + await assert.rejects(gate.dispatch(cap), hasCode("REFUSED")); assert.equal(f.occupancy(), "free") +}) + +test("original body, destination credentials, request, identities and generation all enter keyed exact proofs", async () => { + const baseline = fixture(); await baseline.gate().reserveAndArm(baseline.options()) + const initial = baseline.binding() + for (const mutate of [(i: BandwidthAdmissionInput) => { i.nativeBody.password += "changed" }, (i: BandwidthAdmissionInput) => { i.nativeBody["remote-tx-speed"] = "99" }, (i: BandwidthAdmissionInput) => { i.request.rate = 101 }, (i: BandwidthAdmissionInput) => { i.destination.canonicalNodeId = "physical:C" }, (i: BandwidthAdmissionInput) => { i.authorityGeneration = "actor-8" }]) { + const f = fixture(); f.input.expiresAt = baseline.input.expiresAt; mutate(f.input); await f.gate().reserveAndArm(f.options()); assert.notEqual(f.binding().bindingProof, initial.bindingProof) + assert.ok(!JSON.stringify(f.binding()).includes("destination-secret")) + } +}) + +test("full duration 2147483647 is preserved and no shorter command duration is introduced", async () => { + const f = fixture(); f.input.durationSec = 2147483647; f.input.nativeBody.duration = "2147483647s" + f.deps.send = async (binding, body) => { assert.equal(binding.durationSec, 2147483647); assert.equal(body.duration, "2147483647s"); return [{ status: "done testing" }] } + const gate = f.gate(); await gate.dispatch(await gate.reserveAndArm(f.options())) +}) + +test("pre-dispatch revoke, stale endpoint and abort release only proven-not-sent ownership", async () => { + for (const fault of ["revoke", "endpoint", "abort"]) { + const f = fixture(), controller = new AbortController(), gate = f.gate(), cap = await gate.reserveAndArm({ ...f.options(), signal: controller.signal }) + if (fault === "revoke") f.revoke(); else if (fault === "endpoint") f.staleEndpoints(); else controller.abort() + await assert.rejects(gate.dispatch(cap)); assert.equal(f.sends(), 0); assert.equal(f.occupancy(), "free") + assert.equal(f.events.filter(event => event === "release-before-dispatch").length, 1) + await assert.rejects(gate.dispatch(cap), hasCode("REFUSED")) + } +}) + +test("reserved endpoint refusal releases without arm and failed no-send release never retries CAS", async () => { + const f = fixture(); f.staleEndpoints(); f.faults.set("release-before-dispatch", "throw") + const gate = f.gate(); await assert.rejects(gate.reserveAndArm(f.options()), hasCode("PERSISTENCE_UNKNOWN")) + assert.equal(f.events.includes("arm"), false); assert.equal(f.events.filter(event => event === "release-before-dispatch").length, 1); assert.equal(f.sends(), 0) +}) + +test("late arm ACK after cancellation cannot create capability or trigger release/send", async () => { + const f = fixture(), delayed = deferred(), controller = new AbortController(); f.delayArm(delayed.promise) + const gate = f.gate(), pending = gate.reserveAndArm({ ...f.options(), signal: controller.signal }) + await turn(); assert.equal(f.occupancy(), "armed"); const rejected = assert.rejects(pending); controller.abort(); await rejected + delayed.resolve(); await turn(); assert.equal(gate.status().phase, "unknown-busy"); assert.equal(f.sends(), 0); assert.equal(f.events.includes("release-before-dispatch"), false) +}) + +test("late endpoint continuation after proven no-dispatch release cannot send", async () => { + const f = fixture(), delayed = deferred<{ source: BandwidthAdmissionInput["source"]; destination: BandwidthAdmissionInput["destination"] }>(), controller = new AbortController() + const endpointReader = f.deps.endpoints + let reads = 0 + f.deps.endpoints = async (binding, scope) => ++reads === 1 ? endpointReader(binding, scope) : delayed.promise + const gate = f.gate(), cap = await gate.reserveAndArm({ ...f.options(), signal: controller.signal }), pending = gate.dispatch(cap) + await turn(); assert.equal(reads, 2) + const rejected = assert.rejects(pending, hasCode("ABORTED")); controller.abort(); await rejected + assert.equal(f.occupancy(), "free") + delayed.resolve({ source: f.input.source, destination: f.input.destination }); await turn() + assert.equal(f.sends(), 0); assert.equal(gate.status().phase, "released") +}) + +test("fresh generation or original expiry drift refuses dispatch before native", async () => { + for (const field of ["authorityGeneration", "controlGeneration", "expiresAt"] as const) { + const f = fixture(), original = f.deps.authorize + let drift = false + f.deps.authorize = async (binding, scope) => { const grant = await original(binding, scope); return drift ? { ...grant, [field]: field === "expiresAt" ? grant.expiresAt + 1 : "changed" } : grant } + const gate = f.gate(), cap = await gate.reserveAndArm(f.options()); drift = true + await assert.rejects(gate.dispatch(cap), hasCode("REFUSED")); assert.equal(f.sends(), 0); assert.equal(f.occupancy(), "free") + } +}) + +test("concurrent arm and concurrent dispatch cannot duplicate CAS or native", async () => { + const f = fixture(), gate = f.gate(), first = gate.reserveAndArm(f.options()) + await assert.rejects(gate.reserveAndArm(f.options()), hasCode("REFUSED")); const cap = await first + const dispatch = gate.dispatch(cap); await assert.rejects(gate.dispatch(cap), hasCode("REFUSED")); await dispatch + assert.equal(f.sends(), 1); assert.equal(f.events.filter(event => event === "arm").length, 1) +}) + +test("invoking transport makes throws, nonterminal, malformed and HTTP-like responses unknown-busy", async () => { + for (const raw of [[], [{ status: "testing" }], [{ status: "done testing" }, { status: "running" }], [{ status: "done testing", error: "bad" }], { status: 200 }, null]) { + const f = fixture(); f.send(async () => raw); const gate = f.gate(), cap = await gate.reserveAndArm(f.options()) + let error: unknown; try { await gate.dispatch(cap) } catch (caught) { error = caught } + assert.ok(hasCode("REMOTE_UNKNOWN")(error)); assert.equal(f.occupancy(), "unknown-busy"); assert.equal(f.events.includes("complete"), false); assert.equal(f.events.includes("release-before-dispatch"), false) + assert.equal(Object.hasOwn(error as object, "raw"), false); assert.equal(Object.hasOwn(error as object, "response"), false); assert.equal(String(error).includes("done testing"), false) + assert.deepEqual(gate.partialResponse(error), raw); assert.equal(gate.partialResponse({ ...(error as object) }), undefined) + await assert.rejects(gate.dispatch(cap), hasCode("REFUSED")); assert.equal(f.sends(), 1) + } + const f = fixture(); f.send(() => { throw new Error("transport threw before known physical send") }); const gate = f.gate() + await assert.rejects(gate.dispatch(await gate.reserveAndArm(f.options())), hasCode("REMOTE_UNKNOWN")); assert.equal(f.occupancy(), "unknown-busy") +}) + +test("native response preserves more than 2000 rows, 256 opaque keys and input-depth limits", async () => { + const f = fixture() + let nested: unknown = "deep value" + for (let index = 0; index < 40; index++) nested = { next: nested } + const opaque = Object.fromEntries(Array.from({ length: 300 }, (_, index) => [`opaque_${index}`, `value_${index}`])) + const raw = [...Array.from({ length: 2200 }, (_, index) => ({ status: "testing", sample: index })), { ...opaque, nested, status: "done testing" }] + f.send(async () => raw) + const gate = f.gate(), result = await gate.dispatch(await gate.reserveAndArm(f.options())) + assert.deepEqual(result.raw, raw); assert.equal(result.raw.length, 2201) + assert.equal((result.raw[2200] as Record).opaque_299, "value_299"); assert.equal(f.occupancy(), "free") + assert.notEqual(result.raw, raw); assert.ok(Object.isFrozen(result.raw[2200])) +}) + +test("native response accepts the exact existing 8 MiB JSON envelope and rejects excess without release", async () => { + const max = 8 * 1048576 + const empty = [{ status: "done testing", opaque: "" }] + const overhead = Buffer.byteLength(JSON.stringify(empty)) + for (const extra of [0, 1]) { + const f = fixture(), raw = [{ status: "done testing", opaque: "x".repeat(max - overhead + extra) }] + assert.equal(Buffer.byteLength(JSON.stringify(raw)), max + extra) + // A trusted proof provider may stream/canonicalize large evidence; this + // fixture does not require a second large recursive test-only serializer. + f.deps.keyedProof = async (purpose, value) => purpose === "bandwidth-terminal-v1" ? "e".repeat(64) : proof(purpose, value) + f.send(async () => raw) + const gate = f.gate(), cap = await gate.reserveAndArm(f.options()) + if (extra === 0) { const result = await gate.dispatch(cap); assert.deepEqual(result.raw, raw); assert.equal(f.occupancy(), "free") } + else { + await assert.rejects(gate.dispatch(cap), hasCode("REMOTE_UNKNOWN")); assert.equal(f.occupancy(), "unknown-busy") + assert.equal(f.events.includes("complete"), false); assert.equal(f.events.includes("release-before-dispatch"), false) + } + } +}) + +test("cleanup has a separate hard 10-second maximum without narrowing native duration", async () => { + for (const budget of [0, 10001, 2147483647]) { + const f = fixture(); f.deps.cleanupBudgetMs = budget + assert.throws(f.gate, hasCode("INVALID_INPUT")); assert.equal(f.events.length, 0) + } + const f = fixture(); f.deps.cleanupBudgetMs = 10000 + const original = f.deps.journal.complete + f.deps.journal.complete = async (binding, evidence, scope) => { const remaining = scope.deadlineAt - Date.now(); assert.ok(remaining > 0 && remaining <= 10000); return original(binding, evidence, scope) } + f.input.durationSec = 2147483647; f.input.nativeBody.duration = "2147483647s" + const gate = f.gate(); await gate.dispatch(await gate.reserveAndArm(f.options())) +}) + +test("abort and absolute timeout after send never claim remote stop or release on late done", async () => { + for (const fault of ["abort", "timeout"]) { + const f = fixture(), delayed = deferred(), controller = new AbortController(); f.send(() => delayed.promise) + const gate = f.gate(), cap = await gate.reserveAndArm({ signal: controller.signal, deadlineAt: Date.now() + (fault === "timeout" ? 40 : 5000) }) + const pending = gate.dispatch(cap), rejected = assert.rejects(pending, hasCode("REMOTE_UNKNOWN")) + await turn(); assert.equal(f.sends(), 1); if (fault === "abort") controller.abort(); await rejected + delayed.resolve([{ status: "done testing" }]); await turn() + assert.deepEqual(gate.status(), { phase: "unknown-busy", remoteCompletionConfirmed: false }); assert.equal(f.occupancy(), "unknown-busy"); assert.equal(f.events.includes("complete"), false); assert.equal(f.events.includes("release-before-dispatch"), false) + } +}) + +test("unknown persistence bookkeeping failure cannot free an armed attempt", async () => { + const f = fixture(); f.send(async () => [{ status: "testing" }]); f.faults.set("quarantine", "throw"); const gate = f.gate() + await assert.rejects(gate.dispatch(await gate.reserveAndArm(f.options())), hasCode("REMOTE_UNKNOWN")); assert.notEqual(f.occupancy(), "free") +}) + +test("strict terminal cleanup is independent of subsequent authority expiry/history, with no native replay after lost finalize ACK", async () => { + for (const lost of [false, true]) { + const f = fixture(), controller = new AbortController(); f.onTerminal(() => { controller.abort(); f.revoke() }); if (lost) f.faults.set("complete", "throw") + const gate = f.gate(), cap = await gate.reserveAndArm({ ...f.options(), signal: controller.signal }) + if (lost) await assert.rejects(gate.dispatch(cap), hasCode("COMPLETION_PERSISTENCE_UNKNOWN")) + else assert.equal((await gate.dispatch(cap)).remoteCompletionConfirmed, true) + assert.equal(gate.status().remoteCompletionConfirmed, true); assert.equal(f.occupancy(), "free") + assert.equal(f.events.filter(event => event === "complete").length, 1); await assert.rejects(gate.dispatch(cap), hasCode("REFUSED")); assert.equal(f.sends(), 1) + // No speed-history callback participates in occupancy release. + assert.equal(f.events.some(event => event.includes("history")), false) + } +}) + +test("terminal completion can commit after the original execute deadline using only bounded cleanup", async () => { + const f = fixture(), completed = f.deps.journal.complete + f.deps.journal.complete = async (binding, evidence, scope) => { await delay(50); return completed(binding, evidence, scope) } + const gate = f.gate(), cap = await gate.reserveAndArm({ deadlineAt: Date.now() + 40 }) + assert.equal((await gate.dispatch(cap)).remoteCompletionConfirmed, true) + assert.equal(f.occupancy(), "free") +}) + +test("captured generations/expiry and caller bindings cannot be changed through caller objects", async () => { + const f = fixture(), gate = f.gate(), options = f.options(), cap = await gate.reserveAndArm(options) + const oldExpiry = f.binding().expiresAt; options.deadlineAt += 999999; f.input.authorityGeneration = "new-actor"; f.input.expiresAt += 999999 + assert.equal(f.binding().expiresAt, oldExpiry); assert.equal(f.binding().authorityGeneration, "actor-7") + await gate.dispatch(cap) +}) + +test("untrusted kind, equal physical identities, malformed body and inconsistent duration refuse before I/O", () => { + for (const mutate of [(input: BandwidthAdmissionInput) => { input.callerKind = "arbitrary" as "rest-speed" }, (input: BandwidthAdmissionInput) => { input.destination = { ...input.source } }, (input: BandwidthAdmissionInput) => { input.durationSec = 60 }, (input: BandwidthAdmissionInput) => { input.nativeBody.callback = (() => {}) as unknown as string }, (input: BandwidthAdmissionInput) => { Object.assign(input, { unboundExtra: "reject" }) }]) { + const f = fixture(); mutate(f.input); assert.throws(f.gate, hasCode("INVALID_INPUT")); assert.deepEqual(f.events, []) + } +}) diff --git a/backend/src/services/bandwidth-admission.ts b/backend/src/services/bandwidth-admission.ts new file mode 100644 index 00000000..9f002eca --- /dev/null +++ b/backend/src/services/bandwidth-admission.ts @@ -0,0 +1,310 @@ +import { assertBandwidthTestCompleted } from "./bandwidth-test-status.js" +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { dispatchBandwidthNativeRequest, type BandwidthNativeDispatchGate, type BandwidthNativeRequestBinder } from "./bandwidth-native-dispatch.js" + +/** Pure foundation: all identities, control generations, proofs and durable I/O + * must come from trusted composition. This module enrolls no physical nodes and + * provides no default journal, readiness, native transport or recovery policy. */ +export const BANDWIDTH_CALLER_KINDS = ["rest-speed", "scheduled-speed", "mcp-speed", "routing-measurement", "rest-diagnostic", "mcp-diagnostic", "legacy-diagnostic", "speed-inner"] as const +export type BandwidthCallerKind = typeof BANDWIDTH_CALLER_KINDS[number] +type Json = null | boolean | number | string | Json[] | { [key: string]: Json } +export interface BandwidthParticipant { canonicalNodeId: string; endpointProof: string } +export interface BandwidthAdmissionInput { + attemptId: string + callerKind: BandwidthCallerKind + operationId: string + request: { [key: string]: Json } + source: BandwidthParticipant + destination: BandwidthParticipant + authorityProof: string + authorityGeneration: string + controlGeneration: string + expiresAt: number + durationSec: number + /** Exact native body, including destination credentials and every extra field. */ + nativeBody: Record +} +export interface BandwidthBinding { + attemptId: string; callerKind: BandwidthCallerKind; operationId: string + source: BandwidthParticipant; destination: BandwidthParticipant + authorityProof: string; authorityGeneration: string; controlGeneration: string + expiresAt: number; durationSec: number + requestProof: string; commandProof: string; bindingProof: string +} +export interface BandwidthScope { signal?: AbortSignal; deadlineAt: number } +export type BandwidthTransition = "reserve" | "arm" | "complete" | "release-before-dispatch" | "quarantine" +/** Only a NEW, transaction-committed transition may return this acknowledgement. + * A row read, existing/idempotent transition or uncertain COMMIT is not an ACK. */ +export interface BandwidthJournalAck { + outcome: "new"; transition: BandwidthTransition; ackId: string + attemptId: string; bindingProof: string; controlGeneration: string +} +export interface BandwidthJournal { + /** Every adapter must own an active I/O lifetime and recheck it, the original + * signal and absolute deadline at SQL dispatch and immediately before COMMIT. + * A model timeout retires its continuation, not a physical in-flight COMMIT. + * Late/uncertain writes stay fenced; they never justify retry or adoption. */ + reserve(binding: Readonly, scope: BandwidthScope): Promise + arm(binding: Readonly, scope: BandwidthScope): Promise + /** Atomically authenticate terminal evidence and release both exact owners. */ + complete(binding: Readonly, evidence: Readonly<{ terminalProof: string }>, scope: BandwidthScope): Promise + releaseBeforeDispatch(binding: Readonly, evidence: Readonly<{ from: "reserved" | "armed"; proof: "private-no-send" }>, scope: BandwidthScope): Promise + /** Failure of this bookkeeping must never clear reserved/armed occupancy. */ + quarantine(binding: Readonly, scope: BandwidthScope): Promise +} +export interface BandwidthAdmissionDependencies { + now(): number + cleanupBudgetMs: number + /** REQUIRED keyed, purpose-separated trusted proof; never plain SHA of secrets. */ + keyedProof(purpose: string, value: unknown): Promise + authorize(binding: Readonly, scope: BandwidthScope): Promise<{ authorityProof: string; authorityGeneration: string; controlGeneration: string; expiresAt: number }> + endpoints(binding: Readonly, scope: BandwidthScope): Promise<{ source: BandwidthParticipant; destination: BandwidthParticipant }> + journal: BandwidthJournal + /** Fixed trusted transport. Invocation means a physical send may have happened, + * even if it throws synchronously; no model-supplied execution callback exists. */ + send(binding: Readonly, nativeBody: Readonly>, scope: BandwidthScope, permit: BandwidthNativePermit): Promise + /** Fixed native client object captured by trusted broker composition. Without + * this target, a model-only admission cannot issue a usable transport permit. */ + nativeClient?: object + /** Fixed server-owned dispatch gate. No request supplies lease or authority callbacks. */ + nativeDispatchGate?: BandwidthNativeDispatchGate +} +export class BandwidthAdmissionError extends Error { + constructor(readonly code: "INVALID_INPUT" | "REFUSED" | "EXPIRED" | "ABORTED" | "PERSISTENCE_UNKNOWN" | "REMOTE_UNKNOWN" | "COMPLETION_PERSISTENCE_UNKNOWN") { super("Bandwidth admission: " + code) } +} +declare const capabilityBrand: unique symbol +export type BandwidthDispatchCapability = Readonly<{ [capabilityBrand]: true }> +declare const nativePermitBrand: unique symbol +export type BandwidthNativePermit = Readonly<{ [nativePermitBrand]: true }> +type NativePermitState={active:boolean;client:object|undefined;body:Readonly>;scope:Readonly;monotonicEnd:number;nativeDispatchGate?:Readonly} +const nativePermits=new WeakMap() +/** Verification only. Minting remains private to the committed admission + * dispatch below. A failed use consumes the permit before any comparison. */ +export function consumeBandwidthNativePermit(permit:unknown,client:object,path:string,body:unknown,signal?:AbortSignal):void{ + const state=permit&&typeof permit==="object"?nativePermits.get(permit):undefined + if(!state?.active)throw new BandwidthAdmissionError("REFUSED") + state.active=false + if(state.nativeDispatchGate||state.client!==client||path!=="/tool/bandwidth-test"||state.scope.signal!==signal + ||signal?.aborted||Date.now()>=state.scope.deadlineAt||performance.now()>=state.monotonicEnd + ||!isDeepStrictEqual(body,state.body))throw new BandwidthAdmissionError("REFUSED") +} +/** Private transport integration only. Retire before any await; the historical + * consume-only API cannot bypass a gate-bound permit. start is fixed rosPost, + * never a public command/body callback. Ungated v1 behavior remains unchanged. */ +export function dispatchBandwidthNativePermit(permit:unknown,client:object,path:string,body:unknown,signal:AbortSignal|undefined, + start:(bindRequest?:BandwidthNativeRequestBinder)=>Promise):Promise{ + const state=permit&&typeof permit==="object"?nativePermits.get(permit):undefined + if(!state?.active)throw new BandwidthAdmissionError("REFUSED") + state.active=false + const assertOriginal=()=>{ + if(state.client!==client||path!=="/tool/bandwidth-test"||state.scope.signal!==signal + ||signal?.aborted||Date.now()>=state.scope.deadlineAt||performance.now()>=state.monotonicEnd + ||!isDeepStrictEqual(body,state.body))throw new BandwidthAdmissionError("REFUSED") + } + assertOriginal() + return state.nativeDispatchGate + ?dispatchBandwidthNativeRequest(state.nativeDispatchGate,state.scope,state.monotonicEnd,assertOriginal,start) + :start() +} + +type Phase = "new" | "reserving" | "reserved" | "arming" | "armed" | "checking" | "sending" | "released" | "unknown-busy" +function fail(code: BandwidthAdmissionError["code"]): never { throw new BandwidthAdmissionError(code) } +const hash = (value: unknown): value is string => typeof value === "string" && /^[a-f0-9]{64}$/.test(value) +const label = (value: unknown): value is string => typeof value === "string" && /^[a-zA-Z0-9_.:-]{1,200}$/.test(value) +const plain = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +function json(value: unknown, depth = 0): boolean { + if (depth > 16) return false + if (value === null || typeof value === "string" || typeof value === "boolean") return true + if (typeof value === "number") return Number.isFinite(value) && (!Number.isInteger(value) || Number.isSafeInteger(value)) + if (Array.isArray(value)) return value.length <= 2000 && value.every(item => json(item, depth + 1)) + return plain(value) && Object.keys(value).length <= 256 && Object.values(value).every(item => json(item, depth + 1)) +} +function freeze(value: T): T { + const pending: unknown[] = [value] + while (pending.length) { + const item = pending.pop() + if (!item || typeof item !== "object" || Object.isFrozen(item)) continue + Object.freeze(item) + for (const child of Object.values(item)) if (child && typeof child === "object") pending.push(child) + } + return value +} +/** Native transport already supplies JSON. Preserve its complete existing 8 MiB + * envelope independently of the tighter request schema. JSON snapshot failures + * (cycles, unsupported values, serializer exhaustion) leave occupancy unknown; + * native rows, object keys and nesting receive no new business limits. */ +function nativeSnapshot(value: unknown): unknown { + try { + const text = JSON.stringify(value) + if (typeof text !== "string" || Buffer.byteLength(text) > 8 * 1048576) fail("REMOTE_UNKNOWN") + return freeze(JSON.parse(text) as unknown) + } catch { fail("REMOTE_UNKNOWN") } +} +function participant(value: unknown): value is BandwidthParticipant { return plain(value) && Object.keys(value).sort().join(",") === "canonicalNodeId,endpointProof" && label(value.canonicalNodeId) && hash(value.endpointProof) } +function sameEndpoints(value: { source: BandwidthParticipant; destination: BandwidthParticipant }, expected: BandwidthAdmissionInput) { + return participant(value.source) && participant(value.destination) && value.source.canonicalNodeId === expected.source.canonicalNodeId && value.source.endpointProof === expected.source.endpointProof && value.destination.canonicalNodeId === expected.destination.canonicalNodeId && value.destination.endpointProof === expected.destination.endpointProof +} + +export function createBandwidthAdmission(input: BandwidthAdmissionInput, deps: BandwidthAdmissionDependencies) { + if (!json(input) || Object.keys(input).sort().join(",") !== "attemptId,authorityGeneration,authorityProof,callerKind,controlGeneration,destination,durationSec,expiresAt,nativeBody,operationId,request,source" + || Buffer.byteLength(JSON.stringify(input)) > 1048576 || !plain(input.request) || !plain(input.nativeBody) + || Object.values(input.nativeBody).some(value => typeof value !== "string") + || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(input.attemptId) + || !BANDWIDTH_CALLER_KINDS.includes(input.callerKind) || !label(input.operationId) + || !participant(input.source) || !participant(input.destination) || input.source.canonicalNodeId === input.destination.canonicalNodeId + || !hash(input.authorityProof) || !label(input.authorityGeneration) || !label(input.controlGeneration) + || !Number.isSafeInteger(input.expiresAt) || input.expiresAt <= 0 + || !Number.isSafeInteger(input.durationSec) || input.durationSec < 3 || input.durationSec > 2147483647 + || input.nativeBody.duration !== `${input.durationSec}s` + || !Number.isSafeInteger(deps.cleanupBudgetMs) || deps.cleanupBudgetMs <= 0 || deps.cleanupBudgetMs > 10000) fail("INVALID_INPUT") + const captured = freeze(structuredClone(input)) + const now = deps.now.bind(deps), keyedProof = deps.keyedProof.bind(deps), authorize = deps.authorize.bind(deps), endpoints = deps.endpoints.bind(deps), send = deps.send.bind(deps) + const nativeClient=deps.nativeClient + const configuredDispatchGate=deps.nativeDispatchGate + const nativeDispatchGate=configuredDispatchGate?Object.freeze({acquire:configuredDispatchGate.acquire.bind(configuredDispatchGate)}):undefined + const journal = { reserve: deps.journal.reserve.bind(deps.journal), arm: deps.journal.arm.bind(deps.journal), complete: deps.journal.complete.bind(deps.journal), releaseBeforeDispatch: deps.journal.releaseBeforeDispatch.bind(deps.journal), quarantine: deps.journal.quarantine.bind(deps.journal) } + const cleanupBudgetMs = deps.cleanupBudgetMs + let phase: Phase = "new", binding: Readonly | undefined, originalScope: BandwidthScope | undefined, remoteCompletionConfirmed = false + let nativeMonotonicEnd=0 + const capabilities = new WeakMap(), acknowledgements = new Set(), partials = new WeakMap() + function check(scope: BandwidthScope) { + if (scope.signal?.aborted) fail("ABORTED") + if (now() >= scope.deadlineAt) fail("EXPIRED") + } + /** Race only closes our continuation. A late journal/transport result cannot + * mint a capability, release occupancy or resume dispatch in this scope. */ + function bounded(scope: BandwidthScope, work: () => Promise): Promise { + check(scope) + return new Promise((resolve, reject) => { + let settled = false, timer: ReturnType | undefined + const finish = (value: T | unknown, failed: boolean) => { + if (settled) return + settled = true; if (timer) clearTimeout(timer); scope.signal?.removeEventListener("abort", abort) + if (failed) reject(value); else resolve(value as T) + } + const abort = () => finish(new BandwidthAdmissionError("ABORTED"), true) + const tick = () => { if (settled) return; const remaining = scope.deadlineAt - now(); if (remaining <= 0) finish(new BandwidthAdmissionError("EXPIRED"), true); else timer = setTimeout(tick, Math.min(remaining, 2147483647)) } + scope.signal?.addEventListener("abort", abort, { once: true }); tick() + if (settled) return + if (scope.signal?.aborted) { abort(); return } + try { void work().then(value => finish(value, false), error => finish(error, true)) } catch (error) { finish(error, true) } + }) + } + function ack(value: BandwidthJournalAck, transition: BandwidthTransition) { + if (!value || value.outcome !== "new" || value.transition !== transition || !label(value.ackId) || acknowledgements.has(value.ackId) + || value.attemptId !== binding!.attemptId || value.bindingProof !== binding!.bindingProof || value.controlGeneration !== binding!.controlGeneration) fail("PERSISTENCE_UNKNOWN") + acknowledgements.add(value.ackId) + } + const cleanupScope = (): BandwidthScope => ({ deadlineAt: now() + cleanupBudgetMs }) + async function quarantine() { + phase = "unknown-busy" + if (!binding) return + const scope = cleanupScope() + try { ack(await bounded(scope, () => journal.quarantine(binding!, scope)), "quarantine") } catch { /* Existing occupancy remains authoritative even without this ACK. */ } + } + async function release(from: "reserved" | "armed") { + const scope = cleanupScope() + try { ack(await bounded(scope, () => journal.releaseBeforeDispatch(binding!, { from, proof: "private-no-send" }, scope)), "release-before-dispatch"); phase = "released" } + catch { await quarantine(); fail("PERSISTENCE_UNKNOWN") } + } + async function fresh(scope: BandwidthScope) { + const value = await bounded(scope, () => authorize(binding!, scope)); check(scope) + if (value.authorityProof !== captured.authorityProof || value.authorityGeneration !== captured.authorityGeneration || value.controlGeneration !== captured.controlGeneration || value.expiresAt !== captured.expiresAt) fail("REFUSED") + } + function take(capability: BandwidthDispatchCapability) { + const owned = capability && typeof capability === "object" ? capabilities.get(capability) : undefined + if (!owned?.active || phase !== "armed") fail("REFUSED") + owned.active = false // Retire before the first async boundary, including cancellation. + } + return Object.freeze({ + status: () => Object.freeze({ phase, remoteCompletionConfirmed }), + /** Private error-identity lookup for bounded diagnostic presentation. A local + * diagnostic may finish displaying partials while occupancy stays unknown. + * JSON copies of the error cannot retrieve/adopt these rows or retry native. */ + partialResponse: (error: unknown): unknown => error && typeof error === "object" && partials.has(error) ? nativeSnapshot(partials.get(error)) : undefined, + async reserveAndArm(options: BandwidthScope): Promise { + if (phase !== "new") fail("REFUSED") + if (!Number.isSafeInteger(options.deadlineAt) || options.deadlineAt <= 0) fail("INVALID_INPUT") + originalScope = Object.freeze({ signal: options.signal, deadlineAt: Math.min(options.deadlineAt, captured.expiresAt) }) + nativeMonotonicEnd=performance.now()+Math.max(0,originalScope.deadlineAt-Date.now()) + const scope = originalScope + // No operation may be resumed/retried through this admission instance. + phase = "checking" + try { + const requestProof = await bounded(scope, () => keyedProof("bandwidth-request-v1", captured.request)) + const commandProof = await bounded(scope, () => keyedProof("bandwidth-command-v1", captured.nativeBody)) + const core = { attemptId: captured.attemptId, callerKind: captured.callerKind, operationId: captured.operationId, source: captured.source, destination: captured.destination, authorityProof: captured.authorityProof, authorityGeneration: captured.authorityGeneration, controlGeneration: captured.controlGeneration, expiresAt: captured.expiresAt, durationSec: captured.durationSec, requestProof, commandProof } + const bindingProof = await bounded(scope, () => keyedProof("bandwidth-binding-v1", core)) + if (![requestProof, commandProof, bindingProof].every(hash)) fail("REFUSED") + binding = freeze({ ...core, bindingProof }) + await fresh(scope) + phase = "reserving"; ack(await bounded(scope, () => journal.reserve(binding!, scope)), "reserve"); phase = "reserved" + await fresh(scope) + const current = await bounded(scope, () => endpoints(binding!, scope)); check(scope) + if (!sameEndpoints(current, captured)) fail("REFUSED") + await fresh(scope) + phase = "arming"; ack(await bounded(scope, () => journal.arm(binding!, scope)), "arm"); phase = "armed" + check(scope) + const capability = Object.freeze(Object.create(null)) as BandwidthDispatchCapability + capabilities.set(capability, { active: true }); return capability + } catch (error) { + if (phase === "checking") { + phase = "released" // No reserve was attempted. + if (error instanceof BandwidthAdmissionError) throw error + fail("REFUSED") + } + if (phase === "reserved" || phase === "armed") await release(phase) + else if (phase === "reserving" || phase === "arming") await quarantine() + if (error instanceof BandwidthAdmissionError) throw error + fail("PERSISTENCE_UNKNOWN") + } + }, + async cancel(capability: BandwidthDispatchCapability): Promise { take(capability); phase = "checking"; await release("armed") }, + async dispatch(capability: BandwidthDispatchCapability): Promise<{ remoteCompletionConfirmed: true; raw: Array> }> { + take(capability); phase = "checking" + const scope = originalScope! + let invoked = false, partialRaw: unknown + try { + await fresh(scope) + const current = await bounded(scope, () => endpoints(binding!, scope)); check(scope) + if (!sameEndpoints(current, captured)) fail("REFUSED") + await fresh(scope); check(scope) + const received = await bounded(scope, async () => { + invoked = true; phase = "sending" + const permit=Object.freeze(Object.create(null)) as BandwidthNativePermit + const state:NativePermitState={active:true,client:nativeClient,body:captured.nativeBody,scope, + monotonicEnd:nativeMonotonicEnd,nativeDispatchGate} + nativePermits.set(permit,state) + try{return await send(binding!,captured.nativeBody,scope,permit)} + finally{state.active=false;nativePermits.delete(permit)} + }) + check(scope) + const raw = nativeSnapshot(received) + partialRaw = raw + try { assertBandwidthTestCompleted(raw) } catch { fail("REMOTE_UNKNOWN") } + remoteCompletionConfirmed = true + // Terminal bookkeeping is independent of expired/revoked execute authority. + const cleanup = cleanupScope() + try { + const terminalProof = await bounded(cleanup, () => keyedProof("bandwidth-terminal-v1", { bindingProof: binding!.bindingProof, raw })) + if (!hash(terminalProof)) fail("COMPLETION_PERSISTENCE_UNKNOWN") + ack(await bounded(cleanup, () => journal.complete(binding!, { terminalProof }, cleanup)), "complete") + phase = "released" + } catch { await quarantine(); fail("COMPLETION_PERSISTENCE_UNKNOWN") } + return { remoteCompletionConfirmed: true, raw: raw as Array> } + } catch (error) { + if (!invoked) { + await release("armed") + if (error instanceof BandwidthAdmissionError) throw error + fail("REFUSED") + } + else if ((phase as Phase) !== "released" && (phase as Phase) !== "unknown-busy") await quarantine() + if (error instanceof BandwidthAdmissionError && error.code === "COMPLETION_PERSISTENCE_UNKNOWN") throw error + const unknown = new BandwidthAdmissionError("REMOTE_UNKNOWN") + if (partialRaw !== undefined) partials.set(unknown, partialRaw) + throw unknown + } + }, + }) +} diff --git a/backend/src/services/bandwidth-control-catalog.test.ts b/backend/src/services/bandwidth-control-catalog.test.ts new file mode 100644 index 00000000..84d65a7a --- /dev/null +++ b/backend/src/services/bandwidth-control-catalog.test.ts @@ -0,0 +1,116 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_CONTROL_SOURCE_DDL } from "./bandwidth-control-source-schema.js" +import { observeBandwidthControlCatalog, BandwidthControlCatalogRefused } from "./bandwidth-control-catalog.js" + +type Row=Record +type Item={section:string;raw_json:string|null} +const refused=(error:unknown)=>error instanceof BandwidthControlCatalogRefused +/** Inert catalog fixture derived from the checked-in component, not an external + * supplied digest. Actual PostgreSQL deparser/types are covered separately. */ +function fixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_CONTROL_SOURCE_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_attempts:[["p",["attempt_id"]],["u",["caller_kind","operation_id"]]],bandwidth_node_occupancy:[["p",["node_id"]],["i",["attempt_id"]]],bandwidth_attempt_events:[["p",["ack_id"]],["u",["attempt_id","transition"]]],bandwidth_source_identity:[["p",["id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const table of["bandwidth_node_occupancy","bandwidth_attempt_events"]){ + const name=`${table}_attempt_id_fkey` + addConstraint(table,"f",["attempt_id"],name,{foreign_namespace:"mmapp_control",foreign_table:"bandwidth_attempts",foreign_keys:["attempt_id"],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],["bandwidth_attempts","RI_FKey_restrict_del",9],["bandwidth_attempts","RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +const run=(rows:Item[],live=()=>{})=>observeBandwidthControlCatalog({query:async()=>({rows})},live) +function mutate(rows:Item[],section:string,predicate:(row:Row)=>boolean,change:(row:Row)=>void){ + const item=rows.find(item=>item.section===section&&predicate(JSON.parse(item.raw_json!)as Row))!;assert.ok(item,section) + const row=JSON.parse(item.raw_json!)as Row;change(row);item.raw_json=JSON.stringify(row) +} + +test("fixed component validates and produces deterministic SHA independent of row/object order and CRLF",async()=>{ + const rows=fixture(),events:string[]=[] + const first=await observeBandwidthControlCatalog({query:async(text,values)=>{events.push("SQL");assert.deepEqual(values,[]);assert.ok(text.includes("pg_catalog.pg_proc"));assert.ok(text.includes("n.nspname='mmapp_control'"));assert.ok(text.includes("LIMIT 81"));return{rows}}},()=>events.push("live")) + assert.match(first,/^[a-f0-9]{64}$/);assert.deepEqual(events,["live","SQL","live"]) + const shuffled=rows.reverse().map(item=>{const row=JSON.parse(item.raw_json!);if(item.section==="functions")row.body=row.body.replace(/\n/g,"\r\n");return{...item,raw_json:JSON.stringify(Object.fromEntries(Object.entries(row).reverse()))}}) + assert.equal(await run(shuffled),first) +}) + +test("function replacement, SECURITY DEFINER, overload, changed arguments or search path cannot be blessed by a new digest",async()=>{ + for(const change of[(row:Row)=>{row.body="BEGIN RETURN NEW; END;"},(row:Row)=>{row.security_definer=true},(row:Row)=>{row.language="sql"},(row:Row)=>{row.config=["search_path=pg_catalog, public"]},(row:Row)=>{row.argument_types=["text"]},(row:Row)=>{row.return_type="text"},(row:Row)=>{row.owner="app_restorer"},(row:Row)=>{row.returns_set=true},(row:Row)=>{row.has_support=true}]){ + const rows=fixture();mutate(rows,"functions",row=>row.name==="bandwidth_source_identity_guard",change);await assert.rejects(run(rows),refused) + } + const overload=fixture();overload.push({...overload.find(item=>item.section==="functions")!});await assert.rejects(run(overload),refused) +}) + +test("all eleven guards must be ALWAYS and retain exact function, trigger type and deferred ownership",async()=>{ + for(const change of[(row:Row)=>{row.enabled="O"},(row:Row)=>{row.type=29},(row:Row)=>{row.function_name="bandwidth_admission_event_guard"},(row:Row)=>{row.deferrable=true},(row:Row)=>{row.condition="false"}]){ + const rows=fixture();mutate(rows,"triggers",row=>row.name==="bandwidth_source_identity_guard",change);await assert.rejects(run(rows),refused) + } + const rows=fixture();mutate(rows,"triggers",row=>row.name==="bandwidth_attempt_consistency",row=>{row.initially_deferred=false});await assert.rejects(run(rows),refused) + const internal=fixture();mutate(internal,"triggers",row=>row.internal===true,row=>{row.enabled="D"});await assert.rejects(run(internal),refused) +}) + +test("column type/nullability/default/collation and complete four-table footprint are fixed",async()=>{ + for(const change of[(row:Row)=>{row.type_name="int8"},(row:Row)=>{row.not_null=false},(row:Row)=>{row.generated="s"},(row:Row)=>{row.default_expr="1"},(row:Row)=>{row.collation_name="C"}]){ + const rows=fixture();mutate(rows,"columns",row=>row.table_name==="bandwidth_source_identity"&&row.name==="status",change);await assert.rejects(run(rows),refused) + } + for(const section of["namespace","tables","columns","functions","triggers"]){const rows=fixture(),index=rows.findIndex(item=>item.section===section);rows.splice(index,1);await assert.rejects(run(rows),refused)} + const rows=fixture();mutate(rows,"tables",row=>row.name==="bandwidth_attempts",row=>{row.row_security=true});await assert.rejects(run(rows),refused) +}) + +test("structural constraints, exact uniqueness/index shape and unrewritten tables are required",async()=>{ + for(const section of["constraints","indexes"]){const rows=fixture();rows.splice(rows.findIndex(item=>item.section===section),1);await assert.rejects(run(rows),refused)} + const fk=fixture();mutate(fk,"constraints",row=>row.type==="f",row=>{row.delete_action="c"});await assert.rejects(run(fk),refused) + const index=fixture();mutate(index,"indexes",row=>row.table_name==="bandwidth_node_occupancy"&&row.primary===true,row=>{row.predicate="false"});await assert.rejects(run(index),refused) + for(const section of["rules","policies"]){const rows=fixture();rows.push({section,raw_json:JSON.stringify({name:"bypass"})});await assert.rejects(run(rows),refused)} +}) + +test("observed constraint expressions are pinned by the digest without claiming arbitrary SQL equivalence",async()=>{ + const rows=fixture(),before=await run(rows) + mutate(rows,"constraints",row=>row.type==="c",row=>{row.definition="CHECK (changed_expression)"}) + assert.notEqual(await run(rows),before,"the fixed issuer must reject drift against its signed observation") +}) + +test("NULL budget sentinels, unknown tags, row/byte overflow and malformed JSON fail closed",async()=>{ + for(const extra of[{section:"functions",raw_json:null},{section:"foreign",raw_json:"{}"},{section:"functions",raw_json:"{"},{section:"functions",raw_json:JSON.stringify({body:"x".repeat(65536)})}])await assert.rejects(run([...fixture(),extra]),refused) + await assert.rejects(run(Array.from({length:231},()=>({section:"namespace",raw_json:"{}"}))),refused) +}) + +test("late catalog results and query failures honor the live boundary without exposing provider errors",async()=>{ + let active=true,resolve!:(value:{rows:Item[]})=>void + const pending=new Promise<{rows:Item[]}>(done=>{resolve=done}) + const result=observeBandwidthControlCatalog({query:async()=>pending},()=>{if(!active)throw new Error("closed owner")}) + active=false;const rejected=assert.rejects(result,/closed owner/);resolve({rows:fixture()});await rejected + let checks=0 + await assert.rejects(observeBandwidthControlCatalog({query:async()=>{throw new Error("SECRET")}},()=>{checks++}),error=>refused(error)&&!String(error).includes("SECRET"));assert.equal(checks,2) +}) diff --git a/backend/src/services/bandwidth-control-catalog.ts b/backend/src/services/bandwidth-control-catalog.ts new file mode 100644 index 00000000..6f43ac75 --- /dev/null +++ b/backend/src/services/bandwidth-control-catalog.ts @@ -0,0 +1,233 @@ +import { createHash } from "node:crypto" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_CONTROL_SOURCE_DDL } from "./bandwidth-control-source-schema.js" + +/** Fixed component verification plus a canonical observation digest. No default + * I/O, DDL, role assertion, readiness or generation issuance. Constraint/index + * definitions are captured for the fixed issuer's signed pin; their structural + * checks below are not a general proof of arbitrary SQL expression equivalence. */ +export interface BandwidthControlCatalogSql { query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> } +export class BandwidthControlCatalogRefused extends Error { + readonly code = "BANDWIDTH_CONTROL_CATALOG_REFUSED" + constructor() { super("Bandwidth control catalog refused") } +} +type Row = Record +const tableNames = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events", "bandwidth_source_identity"] as const +const functionNames = ["bandwidth_admission_binding_valid", "bandwidth_admission_attempt_guard", "bandwidth_admission_occupancy_guard", "bandwidth_admission_event_guard", "bandwidth_admission_reject_truncate", "bandwidth_admission_consistency_guard", "bandwidth_source_identity_guard"] as const +const normalizeBody = (value: string) => value.replace(/\r\n?/g, "\n").trim() +function expectedBodies(): Map { + const found = new Map() + for (const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL, BANDWIDTH_CONTROL_SOURCE_DDL]) { + for (const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)) { + if (found.has(match[1])) refuse() + found.set(match[1], normalizeBody(match[2])) + } + } + if (found.size !== functionNames.length || functionNames.some(name => !found.has(name))) refuse() + return found +} +const sections = { namespace: 1, tables: 4, columns: 32, functions: 7, triggers: 19, constraints: 80, indexes: 7, rules: 0, policies: 0 } as const +export const BANDWIDTH_CONTROL_CATALOG_SQL = `WITH component AS ( + SELECT c.* FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + WHERE n.nspname='mmapp_control' AND c.relname=ANY(ARRAY['bandwidth_attempts','bandwidth_node_occupancy','bandwidth_attempt_events','bandwidth_source_identity']) +), catalog AS MATERIALIZED ( + SELECT 'namespace'::text AS section,pg_catalog.to_jsonb(q)::text AS raw_json FROM ( + SELECT n.nspname::text AS name,o.rolname::text AS owner FROM pg_catalog.pg_namespace n + JOIN pg_catalog.pg_roles o ON o.oid=n.nspowner WHERE n.nspname='mmapp_control' LIMIT 2) q + UNION ALL SELECT 'tables',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS name,o.rolname::text AS owner,c.relkind::text AS kind,c.relpersistence::text AS persistence, + c.relrowsecurity AS row_security,c.relforcerowsecurity AS force_row_security,c.relispartition AS is_partition,c.relreplident::text AS replica_identity,c.reloptions AS options, + EXISTS(SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhrelid=c.oid) AS has_parent,EXISTS(SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhparent=c.oid) AS has_children, + c.reloftype<>0 AS typed_table,(SELECT am.amname::text FROM pg_catalog.pg_am am WHERE am.oid=c.relam) AS access_method + FROM component c JOIN pg_catalog.pg_roles o ON o.oid=c.relowner ORDER BY c.relname LIMIT 5) q + UNION ALL SELECT 'columns',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,a.attnum::integer AS position,a.attname::text AS name,tn.nspname::text AS type_namespace,t.typname::text AS type_name, + a.attnotnull AS not_null,a.attidentity::text AS identity,a.attgenerated::text AS generated, + cn.nspname::text AS collation_namespace,co.collname::text AS collation_name, + pg_catalog.pg_get_expr(d.adbin,d.adrelid,true) AS default_expr + FROM component c JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum>0 + JOIN pg_catalog.pg_type t ON t.oid=a.atttypid JOIN pg_catalog.pg_namespace tn ON tn.oid=t.typnamespace + LEFT JOIN pg_catalog.pg_collation co ON co.oid=a.attcollation LEFT JOIN pg_catalog.pg_namespace cn ON cn.oid=co.collnamespace + LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=c.oid AND d.adnum=a.attnum + WHERE NOT a.attisdropped ORDER BY c.relname,a.attnum LIMIT 33) q + UNION ALL SELECT 'functions',pg_catalog.to_jsonb(q)::text FROM ( + SELECT p.proname::text AS name,o.rolname::text AS owner,l.lanname::text AS language,p.prosrc AS body,p.prosecdef AS security_definer, + p.proisstrict AS strict,p.provolatile::text AS volatility,p.proleakproof AS leakproof,p.prokind::text AS kind, + p.proparallel::text AS parallel,p.pronargdefaults::integer AS argument_defaults,p.provariadic<>0 AS variadic,p.proconfig AS config, + p.proretset AS returns_set,p.prosupport<>0 AS has_support,p.proallargtypes IS NULL AND p.proargmodes IS NULL AS input_only,p.proargnames AS argument_names, + rt.typname::text AS return_type,rn.nspname::text AS return_namespace, + ARRAY(SELECT t.typname::text FROM pg_catalog.unnest(p.proargtypes::oid[]) WITH ORDINALITY a(type_oid,position) JOIN pg_catalog.pg_type t ON t.oid=a.type_oid ORDER BY a.position) AS argument_types, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(p.proargtypes::oid[]) WITH ORDINALITY a(type_oid,position) JOIN pg_catalog.pg_type t ON t.oid=a.type_oid JOIN pg_catalog.pg_namespace n ON n.oid=t.typnamespace ORDER BY a.position) AS argument_namespaces + FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace JOIN pg_catalog.pg_language l ON l.oid=p.prolang + JOIN pg_catalog.pg_roles o ON o.oid=p.proowner JOIN pg_catalog.pg_type rt ON rt.oid=p.prorettype JOIN pg_catalog.pg_namespace rn ON rn.oid=rt.typnamespace + WHERE n.nspname='mmapp_control' AND p.proname=ANY(ARRAY['bandwidth_admission_binding_valid','bandwidth_admission_attempt_guard','bandwidth_admission_occupancy_guard','bandwidth_admission_event_guard','bandwidth_admission_reject_truncate','bandwidth_admission_consistency_guard','bandwidth_source_identity_guard']) + ORDER BY p.proname,p.oid LIMIT 8) q + UNION ALL SELECT 'triggers',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,CASE WHEN t.tgisinternal THEN NULL ELSE t.tgname::text END AS name,t.tgisinternal AS internal, + t.tgtype::integer AS type,t.tgenabled::text AS enabled,t.tgdeferrable AS deferrable,t.tginitdeferred AS initially_deferred, + t.tgconstraint<>0 AS is_constraint,con.conname::text AS constraint_name, + pn.nspname::text AS function_namespace,p.proname::text AS function_name,t.tgnargs::integer AS argument_count, + pg_catalog.encode(t.tgargs,'hex') AS arguments,t.tgattr::text AS columns,pg_catalog.pg_get_expr(t.tgqual,t.tgrelid,true) AS condition, + t.tgoldtable::text AS old_table,t.tgnewtable::text AS new_table + FROM component c JOIN pg_catalog.pg_trigger t ON t.tgrelid=c.oid JOIN pg_catalog.pg_proc p ON p.oid=t.tgfoid + JOIN pg_catalog.pg_namespace pn ON pn.oid=p.pronamespace LEFT JOIN pg_catalog.pg_constraint con ON con.oid=t.tgconstraint + ORDER BY c.relname,t.tgname LIMIT 20) q + UNION ALL SELECT 'constraints',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,k.conname::text AS name,k.contype::text AS type,k.convalidated AS validated,k.condeferrable AS deferrable,k.condeferred AS initially_deferred, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(k.conkey) WITH ORDINALITY x(attnum,position) JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum=x.attnum ORDER BY x.position) AS keys, + fn.nspname::text AS foreign_namespace,fc.relname::text AS foreign_table, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(k.confkey) WITH ORDINALITY x(attnum,position) JOIN pg_catalog.pg_attribute a ON a.attrelid=k.confrelid AND a.attnum=x.attnum ORDER BY x.position) AS foreign_keys, + k.confupdtype::text AS update_action,k.confdeltype::text AS delete_action,k.confmatchtype::text AS match_type, + pg_catalog.pg_get_constraintdef(k.oid,true) AS definition + FROM component c JOIN pg_catalog.pg_constraint k ON k.conrelid=c.oid + LEFT JOIN pg_catalog.pg_class fc ON fc.oid=k.confrelid LEFT JOIN pg_catalog.pg_namespace fn ON fn.oid=fc.relnamespace + ORDER BY c.relname,k.conname LIMIT 81) q + UNION ALL SELECT 'indexes',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,ic.relname::text AS name,i.indisunique AS unique,i.indisprimary AS primary,i.indisvalid AS valid,i.indisready AS ready,i.indislive AS live, + i.indnkeyatts::integer AS key_count,i.indnatts::integer AS attribute_count,am.amname::text AS method, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(i.indkey::smallint[]) WITH ORDINALITY x(attnum,position) LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum=x.attnum ORDER BY x.position) AS keys, + ARRAY(SELECT co.collname::text FROM pg_catalog.unnest(i.indcollation::oid[]) WITH ORDINALITY x(collation_oid,position) LEFT JOIN pg_catalog.pg_collation co ON co.oid=x.collation_oid ORDER BY x.position) AS collations, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(i.indcollation::oid[]) WITH ORDINALITY x(collation_oid,position) LEFT JOIN pg_catalog.pg_collation co ON co.oid=x.collation_oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid=co.collnamespace ORDER BY x.position) AS collation_namespaces, + ARRAY(SELECT op.opcname::text FROM pg_catalog.unnest(i.indclass::oid[]) WITH ORDINALITY x(class_oid,position) JOIN pg_catalog.pg_opclass op ON op.oid=x.class_oid ORDER BY x.position) AS operator_classes, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(i.indclass::oid[]) WITH ORDINALITY x(class_oid,position) JOIN pg_catalog.pg_opclass op ON op.oid=x.class_oid JOIN pg_catalog.pg_namespace n ON n.oid=op.opcnamespace ORDER BY x.position) AS operator_class_namespaces, + i.indoption::text AS options,pg_catalog.pg_get_expr(i.indexprs,i.indrelid,true) AS expressions,pg_catalog.pg_get_expr(i.indpred,i.indrelid,true) AS predicate, + pg_catalog.pg_get_indexdef(i.indexrelid) AS definition + FROM component c JOIN pg_catalog.pg_index i ON i.indrelid=c.oid JOIN pg_catalog.pg_class ic ON ic.oid=i.indexrelid JOIN pg_catalog.pg_am am ON am.oid=ic.relam + ORDER BY c.relname,ic.relname LIMIT 8) q + UNION ALL SELECT 'rules',pg_catalog.to_jsonb(q)::text FROM (SELECT c.relname::text AS table_name,r.rulename::text AS name FROM component c JOIN pg_catalog.pg_rewrite r ON r.ev_class=c.oid LIMIT 1) q + UNION ALL SELECT 'policies',pg_catalog.to_jsonb(q)::text FROM (SELECT c.relname::text AS table_name,p.polname::text AS name FROM component c JOIN pg_catalog.pg_policy p ON p.polrelid=c.oid LIMIT 1) q +), metered AS ( + SELECT section,raw_json,pg_catalog.sum(pg_catalog.octet_length(raw_json)) OVER(ORDER BY section,raw_json) AS bytes FROM catalog +) SELECT section,CASE WHEN pg_catalog.octet_length(raw_json)<=65536 AND bytes<=1048576 THEN raw_json END AS raw_json FROM metered ORDER BY section,raw_json` + +function refuse(): never { throw new BandwidthControlCatalogRefused() } +const plain = (value: unknown): value is Row => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +const arrayEqual = (value: unknown, expected: unknown[]) => Array.isArray(value) && JSON.stringify(value) === JSON.stringify(expected) +function canonical(value: unknown): string { + if (Array.isArray(value)) return "[" + value.map(canonical).join(",") + "]" + if (plain(value)) return "{" + Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" + return JSON.stringify(value) +} +type Column = [name: string, type: string, notNull: boolean, defaultExpr?: string] +const text = (name: string, notNull = true): Column => [name, "text", notNull] +const columns: Record = { + bandwidth_attempts: [["attempt_id","uuid",true],text("caller_kind"),text("operation_id"),["binding","jsonb",true],text("source_node"),text("destination_node"),text("binding_proof"),text("control_generation"),text("state"),text("resolution",false),text("terminal_proof",false),["created_at","timestamptz",true,"clock_timestamp()"]], + bandwidth_node_occupancy: [text("node_id"),["attempt_id","uuid",true],text("binding_proof"),text("control_generation")], + bandwidth_attempt_events: [["ack_id","uuid",true],["attempt_id","uuid",true],text("transition"),text("old_state",false),text("new_state"),text("binding_proof"),text("control_generation"),text("evidence_proof"),["created_at","timestamptz",true,"clock_timestamp()"]], + bandwidth_source_identity: [["id","int4",true],["installation_id","uuid",true],["control_generation","uuid",true],text("attestation_sha256"),text("event_key_sha256"),["status","text",true,"'active'::text"],["created_at","timestamptz",true,"clock_timestamp()"]], +} +const triggers: Record = { + bandwidth_attempt_guard: ["bandwidth_attempts","bandwidth_admission_attempt_guard",31,false], + bandwidth_occupancy_guard: ["bandwidth_node_occupancy","bandwidth_admission_occupancy_guard",31,false], + bandwidth_event_guard: ["bandwidth_attempt_events","bandwidth_admission_event_guard",31,false], + bandwidth_attempts_no_truncate: ["bandwidth_attempts","bandwidth_admission_reject_truncate",34,false], + bandwidth_occupancy_no_truncate: ["bandwidth_node_occupancy","bandwidth_admission_reject_truncate",34,false], + bandwidth_events_no_truncate: ["bandwidth_attempt_events","bandwidth_admission_reject_truncate",34,false], + bandwidth_attempt_consistency: ["bandwidth_attempts","bandwidth_admission_consistency_guard",29,true], + bandwidth_occupancy_consistency: ["bandwidth_node_occupancy","bandwidth_admission_consistency_guard",29,true], + bandwidth_event_consistency: ["bandwidth_attempt_events","bandwidth_admission_consistency_guard",29,true], + bandwidth_source_identity_guard: ["bandwidth_source_identity","bandwidth_source_identity_guard",31,false], + bandwidth_source_identity_no_truncate: ["bandwidth_source_identity","bandwidth_source_identity_guard",34,false], +} + +export async function observeBandwidthControlCatalog(sql: BandwidthControlCatalogSql, live: () => void): Promise { + live() + let result: { rows: Row[] } + try { result = await sql.query(BANDWIDTH_CONTROL_CATALOG_SQL, []) } catch { live(); refuse() } + live() + return validateBandwidthControlCatalogRows(result.rows) +} + +/** Identical structural/function-body validation for either transport. */ +export function validateBandwidthControlCatalogRows(rows: unknown): string { + const bodies = expectedBodies() + if (!Array.isArray(rows) || rows.length > 230) refuse() + const found = Object.fromEntries(Object.keys(sections).map(key => [key, [] as Row[]])) as unknown as Record + let bytes = 0 + for (const item of rows) { + if (!plain(item) || Object.keys(item).sort().join(",") !== "raw_json,section" || typeof item.section !== "string" || !Object.hasOwn(sections,item.section) || typeof item.raw_json !== "string") refuse() + bytes += Buffer.byteLength(item.raw_json) + if (Buffer.byteLength(item.raw_json)>65536 || bytes>1048576) refuse() + let parsed: unknown + try { parsed = JSON.parse(item.raw_json) } catch { refuse() } + if (!plain(parsed)) refuse() + const section = item.section as keyof typeof sections + found[section].push(parsed) + if (found[section].length>sections[section]) refuse() + } + if (found.namespace.length!==1 || found.namespace[0].name!=="mmapp_control" || typeof found.namespace[0].owner!=="string") refuse() + if (found.tables.length!==4 || new Set(found.tables.map(row=>row.name)).size!==4) refuse() + for (const row of found.tables) if (!tableNames.includes(row.name as typeof tableNames[number]) || row.owner!==found.namespace[0].owner || row.kind!=="r" || row.persistence!=="p" || row.row_security!==false || row.force_row_security!==false || row.is_partition!==false || row.replica_identity!=="d" || row.options!==null || row.has_parent!==false || row.has_children!==false || row.typed_table!==false || row.access_method!=="heap") refuse() + if (found.columns.length!==32) refuse() + for (const table of tableNames) { + const actual = found.columns.filter(row=>row.table_name===table).sort((a,b)=>Number(a.position)-Number(b.position)) + if (actual.length!==columns[table].length) refuse() + for (const [index,expected] of columns[table].entries()) { + const row=actual[index],collation=expected[1]==="text"?(table==="bandwidth_source_identity"?"default":"C"):null + const defaultExpr=row.default_expr==="pg_catalog.clock_timestamp()"?"clock_timestamp()":row.default_expr==="'active'::pg_catalog.text"?"'active'::text":row.default_expr + if (row.position!==index+1 || row.name!==expected[0] || row.type_namespace!=="pg_catalog" || row.type_name!==expected[1] || row.not_null!==expected[2] || row.identity!=="" || row.generated!=="" || row.collation_name!==collation || row.collation_namespace!==(collation?"pg_catalog":null) || defaultExpr!==(expected[3]??null)) refuse() + row.default_expr=defaultExpr + } + } + if (found.functions.length!==7 || new Set(found.functions.map(row=>row.name)).size!==7) refuse() + for (const row of found.functions) { + const validator=row.name==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + if (typeof row.name!=="string" || !bodies.has(row.name) || typeof row.body!=="string" || normalizeBody(row.body)!==bodies.get(row.name) + || row.owner!==found.namespace[0].owner || row.language!=="plpgsql" || row.return_namespace!=="pg_catalog" || row.return_type!==(validator?"bool":"trigger") + || row.security_definer!==false || row.strict!==validator || row.volatility!==(validator?"i":"v") || row.leakproof!==false || row.kind!=="f" || row.parallel!=="u" || row.argument_defaults!==0 || row.variadic!==false + || row.returns_set!==false || row.has_support!==false || row.input_only!==true || (validator?!arrayEqual(row.argument_names,["b","attempt","caller","operation","source","destination","proof","generation"]):row.argument_names!==null) + || !arrayEqual(row.argument_types,args) || !arrayEqual(row.argument_namespaces,args.map(()=>"pg_catalog")) + || !Array.isArray(row.config) || row.config.length!==1 || typeof row.config[0]!=="string" || row.config[0].replace(/\s/g,"")!=="search_path=pg_catalog,mmapp_control") refuse() + row.body=normalizeBody(row.body) + row.config=["search_path=pg_catalog,mmapp_control"] + } + const userTriggers=found.triggers.filter(row=>row.internal===false),internal=found.triggers.filter(row=>row.internal===true) + if (userTriggers.length!==11 || internal.length!==8 || new Set(userTriggers.map(row=>row.name)).size!==11) refuse() + for (const row of found.triggers) { + if (row.argument_count!==0 || row.arguments!=="" || row.columns!=="" || row.condition!==null || row.old_table!==null || row.new_table!==null) refuse() + if (row.internal===false) { + const expected=typeof row.name==="string"?triggers[row.name]:undefined + if (!expected || row.table_name!==expected[0] || row.function_namespace!=="mmapp_control" || row.function_name!==expected[1] || row.type!==expected[2] || row.enabled!=="A" || row.deferrable!==expected[3] || row.initially_deferred!==expected[3] || row.is_constraint!==expected[3] || row.constraint_name!==(expected[3]?row.name:null)) refuse() + } else if (row.internal!==true || row.name!==null || row.function_namespace!=="pg_catalog" || !["RI_FKey_check_ins","RI_FKey_check_upd","RI_FKey_restrict_del","RI_FKey_restrict_upd"].includes(String(row.function_name)) || row.enabled!=="O" || row.is_constraint!==true || row.deferrable!==false || row.initially_deferred!==false) refuse() + } + const checkCounts: Record={bandwidth_attempts:10,bandwidth_node_occupancy:2,bandwidth_attempt_events:7,bandwidth_source_identity:4} + const keySpecs: Record={bandwidth_attempts:["p:attempt_id","u:caller_kind,operation_id"],bandwidth_node_occupancy:["p:node_id","f:attempt_id"],bandwidth_attempt_events:["p:ack_id","u:attempt_id,transition","f:attempt_id"],bandwidth_source_identity:["p:id"]} + for (const table of tableNames) { + const actual=found.constraints.filter(row=>row.table_name===table),keys:string[]=[] + if (actual.filter(row=>row.type==="c").length!==checkCounts[table]) refuse() + for (const row of actual) { + if (typeof row.name!=="string" || typeof row.definition!=="string" || !row.definition || row.validated!==true || !Array.isArray(row.keys) || !["p","u","f","c","n","t"].includes(String(row.type))) refuse() + if (row.type==="t") { if (row.deferrable!==true || row.initially_deferred!==true || !Object.hasOwn(triggers,row.name)) refuse() } + else if (row.deferrable!==false || row.initially_deferred!==false) refuse() + if (["p","u","f"].includes(String(row.type))) keys.push(row.type+":"+row.keys.join(",")) + if (row.type==="f" && (row.foreign_namespace!=="mmapp_control" || row.foreign_table!=="bandwidth_attempts" || !arrayEqual(row.foreign_keys,["attempt_id"]) || row.update_action!=="r" || row.delete_action!=="r" || row.match_type!=="s")) refuse() + const constraintKeys=row.keys + if (row.type==="n" && (constraintKeys.length!==1 || !columns[table].some(column=>column[0]===constraintKeys[0]&&column[2]))) refuse() + } + if (!arrayEqual(keys.sort(),keySpecs[table].sort())) refuse() + } + if (found.constraints.some(row=>!tableNames.includes(row.table_name as typeof tableNames[number]))) refuse() + const expectedInternal:string[]=[] + for (const fk of found.constraints.filter(row=>row.type==="f")) { + expectedInternal.push(`${fk.table_name}:${fk.name}:RI_FKey_check_ins:5`,`${fk.table_name}:${fk.name}:RI_FKey_check_upd:17`,`${fk.foreign_table}:${fk.name}:RI_FKey_restrict_del:9`,`${fk.foreign_table}:${fk.name}:RI_FKey_restrict_upd:17`) + } + if (!arrayEqual(internal.map(row=>`${row.table_name}:${row.constraint_name}:${row.function_name}:${row.type}`).sort(),expectedInternal.sort())) refuse() + if (!arrayEqual(found.constraints.filter(row=>row.type==="t").map(row=>row.table_name+":"+row.name).sort(),userTriggers.filter(row=>row.is_constraint===true).map(row=>row.table_name+":"+row.name).sort())) refuse() + const indexSpecs: Record={bandwidth_attempts:["p:attempt_id","u:caller_kind,operation_id"],bandwidth_node_occupancy:["p:node_id","i:attempt_id"],bandwidth_attempt_events:["p:ack_id","u:attempt_id,transition"],bandwidth_source_identity:["p:id"]} + if (found.indexes.length!==7) refuse() + for (const table of tableNames) { + const actual=found.indexes.filter(row=>row.table_name===table),keys:string[]=[] + for (const row of actual) { + if (!Array.isArray(row.keys) || !row.keys.length || typeof row.name!=="string" || typeof row.unique!=="boolean" || typeof row.primary!=="boolean" || row.primary&&!row.unique || row.key_count!==row.keys.length || row.attribute_count!==row.keys.length || row.valid!==true || row.ready!==true || row.live!==true || row.method!=="btree" || row.expressions!==null || row.predicate!==null || row.options!==row.keys.map(()=>"0").join(" ") || typeof row.definition!=="string") refuse() + const expectedColumns=row.keys.map(name=>columns[table].find(column=>column[0]===name)) + if (expectedColumns.some(column=>!column) || !arrayEqual(row.collations,expectedColumns.map(column=>column![1]==="text"?(table==="bandwidth_source_identity"?"default":"C"):null)) || !arrayEqual(row.collation_namespaces,expectedColumns.map(column=>column![1]==="text"?"pg_catalog":null)) || !arrayEqual(row.operator_classes,expectedColumns.map(column=>column![1]==="uuid"?"uuid_ops":column![1]==="int4"?"int4_ops":"text_ops")) || !arrayEqual(row.operator_class_namespaces,expectedColumns.map(()=>"pg_catalog"))) refuse() + keys.push((row.primary===true?"p":row.unique===true?"u":"i")+":"+row.keys.join(",")) + } + if (!arrayEqual(keys.sort(),indexSpecs[table].sort())) refuse() + } + // Sort semantic rows independently of SQL transport order. No physical OID or + // volatile statistics enter the digest; internal trigger names contain OIDs + // and are deliberately represented by their structural/function identity. + const snapshot=Object.fromEntries(Object.entries(found).map(([section,rows])=>[section,rows.map(canonical).sort()])) + return createHash("sha256").update(canonical(snapshot)).digest("hex") +} diff --git a/backend/src/services/bandwidth-control-file.test.ts b/backend/src/services/bandwidth-control-file.test.ts new file mode 100644 index 00000000..a9393ab3 --- /dev/null +++ b/backend/src/services/bandwidth-control-file.test.ts @@ -0,0 +1,57 @@ +import assert from "node:assert/strict" +import { test, mock } from "node:test" +import * as fs from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, dirname } from "node:path" + +test("bounded source files: exact reads, partial reads and concurrent inode/size changes", async t => { + const directory = await fs.mkdtemp(join(tmpdir(), "mm-bandwidth-file-")) + const path = join(directory, "issuer.key"), original = Buffer.alloc(32, 73) + let mode: "normal" | "partial" | "append" | "truncate" | "replace" | "cancel" = "normal", reads = 0 + let controller = new AbortController() + const moduleMock = mock.module("node:fs/promises", { namedExports: { ...fs, + open: async (...args: Parameters) => { + if (args[0] === path && mode === "replace") { + const replacement = join(directory, "replacement.key") + await fs.writeFile(replacement, original); await fs.rename(replacement, path) + } + const handle = await fs.open(...args) + if (args[0] !== path) return handle + return { stat: handle.stat.bind(handle), close: handle.close.bind(handle), + async read(buffer: Buffer, offset: number, length: number, position: number) { + reads++ + const result = await handle.read(buffer, offset, mode === "partial" ? Math.min(3, length) : length, position) + if (mode === "append") await fs.appendFile(path, Buffer.from([1])) + if (mode === "truncate") await fs.truncate(path, 4) + if (mode === "cancel") controller.abort() + return result + }, + } + }, + } }) + const { readBandwidthControlFile } = await import("./bandwidth-control-source.js") + const { closePool } = await import("../db/index.js") + const live = () => controller.signal.throwIfAborted() + const reset = async (next: typeof mode) => { mode = next; reads = 0; controller = new AbortController(); await fs.writeFile(path, original) } + try { + await t.test("exact 32-byte key needs one read and returns an independent buffer", async () => { + await reset("normal"); const result = await readBandwidthControlFile(path, 32, live) + assert.deepEqual(result, original); assert.equal(reads, 1) + result.fill(0); assert.deepEqual(await fs.readFile(path), original) + }) + await t.test("short reads still return every original byte", async () => { + await reset("partial"); assert.deepEqual(await readBandwidthControlFile(path, 32, live), original); assert.equal(reads, 11) + }) + for (const mutation of ["append", "truncate", "replace", "cancel"] as const) await t.test(`${mutation} during capture refuses`, async () => { + await reset(mutation); await assert.rejects(readBandwidthControlFile(path, 32, live)) + }) + await t.test("empty and oversized files refuse before any content read", async () => { + await reset("normal"); await fs.writeFile(path, Buffer.alloc(0)); await assert.rejects(readBandwidthControlFile(path, 32, live)); assert.equal(reads, 0) + await fs.writeFile(path, Buffer.alloc(33)); await assert.rejects(readBandwidthControlFile(path, 32, live)); assert.equal(reads, 0) + }) + } finally { + moduleMock.restore(); await closePool() + assert.equal(dirname(path), directory); assert(directory.startsWith(join(tmpdir(), "mm-bandwidth-file-"))) + await fs.rm(directory, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/bandwidth-control-identity.test.ts b/backend/src/services/bandwidth-control-identity.test.ts new file mode 100644 index 00000000..5fcf7986 --- /dev/null +++ b/backend/src/services/bandwidth-control-identity.test.ts @@ -0,0 +1,115 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { randomUUID } from "node:crypto" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_CONTROL_SOURCE_DDL } from "./bandwidth-control-source-schema.js" +import { BANDWIDTH_CONTROL_ROLE_SQL } from "./bandwidth-control-role.js" +import { BANDWIDTH_CONTROL_CATALOG_SQL, validateBandwidthControlCatalogRows } from "./bandwidth-control-catalog.js" +import { assertBandwidthControlIdentity, type BandwidthControlIdentityExpected } from "./bandwidth-control-identity.js" +type Row=Record +type Item={section:string;raw_json:string|null} +// Same complete inert catalog corpus as the standalone validator tests. +function fixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_CONTROL_SOURCE_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_attempts:[["p",["attempt_id"]],["u",["caller_kind","operation_id"]]],bandwidth_node_occupancy:[["p",["node_id"]],["i",["attempt_id"]]],bandwidth_attempt_events:[["p",["ack_id"]],["u",["attempt_id","transition"]]],bandwidth_source_identity:[["p",["id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const table of["bandwidth_node_occupancy","bandwidth_attempt_events"]){ + const name=`${table}_attempt_id_fkey` + addConstraint(table,"f",["attempt_id"],name,{foreign_namespace:"mmapp_control",foreign_table:"bandwidth_attempts",foreign_keys:["attempt_id"],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],["bandwidth_attempts","RI_FKey_restrict_del",9],["bandwidth_attempts","RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +const expected:BandwidthControlIdentityExpected={databaseName:"owned_database",runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",applicationRole:"restricted_application", + databaseOid:123,installationId:randomUUID(),controlGeneration:randomUUID(),sourceRecordSha256:"a".repeat(64),eventKeySha256:"b".repeat(64), + catalogSha256:validateBandwidthControlCatalogRows(fixture())} +function rows(){ + const observation={databaseName:expected.databaseName,sessionRole:expected.runtimeRole,currentRole:expected.runtimeRole,runtimeRoleExists:true,ownerRoleExists:true, + runtimeFlagsSafe:true,ownerMembership:false,dangerousMembership:false,schemaExists:true,schemaOwner:expected.ownerRole,schemaUsage:true,schemaCreate:false,publicSchemaUsage:false,publicSchemaCreate:false, + tables:["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events","bandwidth_source_identity"].map(name=>({name,exists:true,ownerRole:expected.ownerRole,ownedByRuntime:false,privilegesExact:true}))} + const entries:[string,unknown][]=[["role",{observation}],["database",{oid:expected.databaseOid,owner:expected.ownerRole}],["isolation",{unsafe:false}], + ["source",{installation_id:expected.installationId,control_generation:expected.controlGeneration,attestation_sha256:expected.sourceRecordSha256,event_key_sha256:expected.eventKeySha256,status:"active"}], + ...fixture().map(item=>["catalog",item] as [string,unknown])] + return entries.map(([kind,value],index)=>({kind,row_order:String(index+1),raw_json:JSON.stringify(value)})) +} +const run=(value:Row[],target=expected)=>assertBandwidthControlIdentity({query:async()=>({rows:value})},target,()=>{}) +const change=(value:ReturnType,kind:string,field:string,changed:unknown)=>{const row=value.find(row=>row.kind===kind)!;const parsed=JSON.parse(row.raw_json);parsed[field]=changed;row.raw_json=JSON.stringify(parsed)} +test("one fixed bounded SQL preserves all five observations and standalone catalog digest",async()=>{ + const events:string[]=[];let count=0 + await assertBandwidthControlIdentity({query:async(text,values)=>{ + count++;events.push("SQL");assert(text.includes(BANDWIDTH_CONTROL_ROLE_SQL));assert(text.includes(BANDWIDTH_CONTROL_CATALOG_SQL)) + assert(text.startsWith("/* bandwidth-control:identity */"));assert(text.includes("row_order::pg_catalog.text"));assert(text.includes("LIMIT 235")) + assert(!/\b(?:INSERT|UPDATE|DELETE|TRUNCATE|CALL)\s+mmapp_control\./i.test(text));assert.deepEqual(values,[expected.databaseName,expected.runtimeRole,expected.ownerRole,expected.applicationRole]) + return{rows:rows().reverse()} + }},expected,()=>events.push("live")) + assert.equal(count,1);assert.deepEqual(events,["live","SQL","live","live"]) +}) +test("database owner/OID, application isolation and exact original source fields all remain mandatory",async()=>{ + for(const[kind,field,value]of[["database","oid",124],["database","owner","other_owner"],["isolation","unsafe",true],["source","installation_id",randomUUID()], + ["source","control_generation",randomUUID()],["source","attestation_sha256","c".repeat(64)],["source","event_key_sha256","c".repeat(64)],["source","status","quarantined"]]as const){ + const data=rows();change(data,kind,field,value);await assert.rejects(run(data)) + } + for(const kind of ["role","database","isolation","source","catalog"])await assert.rejects(run(rows().filter(row=>row.kind!==kind).map((row,index)=>({...row,row_order:String(index+1)})))) + const role=rows();const parsed=JSON.parse(role[0].raw_json);parsed.observation.ownerMembership=true;role[0].raw_json=JSON.stringify(parsed);await assert.rejects(run(role)) +}) +test("modified function body cannot be blessed by changing only the expected catalog digest",async()=>{ + const data=rows(),row=data.find(row=>row.kind==="catalog"&&JSON.parse(row.raw_json).section==="functions")! + const outer=JSON.parse(row.raw_json),fn=JSON.parse(outer.raw_json);fn.body="BEGIN RETURN NEW; END;";outer.raw_json=JSON.stringify(fn);row.raw_json=JSON.stringify(outer) + await assert.rejects(run(data,{...expected,catalogSha256:"c".repeat(64)})) +}) +test("unknown tags, duplicate or invalid ordinal, missing payload and all row/byte sentinels fail closed",async()=>{ + for(const transform of [ + (value:Row[])=>{value[0].kind="unknown"},(value:Row[])=>{value[0].row_order="2"},(value:Row[])=>{value[0].row_order=1},(value:Row[])=>{value[0].row_order="0"}, + (value:Row[])=>{value[0].raw_json=null},(value:Row[])=>{value[0].raw_json={}},(value:Row[])=>{value[0].raw_json="{"},(value:Row[])=>{value[0].raw_json="a".repeat(131201)}, + (value:Row[])=>{value[0].extra=true},(value:Row[])=>{value.push({...value[0],row_order:String(value.length+1)})}, + ]){const data:Row[]=rows();transform(data);await assert.rejects(run(data))} + await assert.rejects(run(Array.from({length:235},(_,index)=>({kind:"catalog",row_order:String(index+1),raw_json:"{}"})))) + await assert.rejects(run(Array.from({length:33},(_,index)=>({kind:"catalog",row_order:String(index+1),raw_json:JSON.stringify({padding:"a".repeat(131000)})})))) + const sentinel=rows().find(row=>row.kind==="catalog")!;sentinel.raw_json=JSON.stringify({section:"functions",raw_json:null}) + const data=rows();data[data.findIndex(row=>row.kind==="catalog")]=sentinel;await assert.rejects(run(data)) +}) +test("immutable expectations and live boundaries prevent result adoption after drift or expiry",async()=>{ + const mutable={...expected};let checks=0 + await assertBandwidthControlIdentity({query:async()=>{mutable.sourceRecordSha256="c".repeat(64);mutable.ownerRole="changed_owner";return{rows:rows()}}},mutable,()=>{checks++}) + assert.equal(checks,3) + let calls=0 + await assert.rejects(assertBandwidthControlIdentity({query:async()=>{calls++;return{rows:rows()}}},expected,()=>{throw Error("expired")})) + assert.equal(calls,0) + let expired=false + await assert.rejects(assertBandwidthControlIdentity({query:async()=>{expired=true;return{rows:rows()}}},expected,()=>{if(expired)throw Error("expired")})) + let postFailure=0 + await assert.rejects(assertBandwidthControlIdentity({query:async()=>{throw Error("private driver details")}},expected,()=>{postFailure++}),error=>error instanceof Error&&!error.message.includes("private driver")) + assert.equal(postFailure,2) +}) + diff --git a/backend/src/services/bandwidth-control-identity.ts b/backend/src/services/bandwidth-control-identity.ts new file mode 100644 index 00000000..fdb67406 --- /dev/null +++ b/backend/src/services/bandwidth-control-identity.ts @@ -0,0 +1,100 @@ +/** One bounded, read-only MVCC snapshot of the five existing core identity + * observations. This does not cache authority or replace a dispatch fence. */ +import { z } from "zod" +import { BANDWIDTH_CONTROL_ROLE_SQL, captureBandwidthControlRoleExpected, validateBandwidthControlRuntimeRoleRows } from "./bandwidth-control-role.js" +import { BANDWIDTH_CONTROL_CATALOG_SQL, validateBandwidthControlCatalogRows } from "./bandwidth-control-catalog.js" + +export interface BandwidthControlIdentitySql { + query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> +} +const label = z.string().min(1).max(63).refine(value => value.trim() === value && Buffer.byteLength(value) <= 63 && !/[\x00-\x1f\x7f]/.test(value)) +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const expectedSchema = z.object({ databaseName: label, runtimeRole: label, ownerRole: label, applicationRole: label, + databaseOid: z.number().int().min(1).max(4294967295), installationId: z.string().uuid(), controlGeneration: z.string().uuid(), + sourceRecordSha256: hash, eventKeySha256: hash, catalogSha256: hash }).strict() +export type BandwidthControlIdentityExpected = z.infer +export class BandwidthControlIdentityRefused extends Error { + readonly code = "BANDWIDTH_CONTROL_IDENTITY_REFUSED" + constructor() { super("Bandwidth control identity refused") } +} +function refuse(): never { throw new BandwidthControlIdentityRefused() } +const plain = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +type Kind = "role" | "database" | "isolation" | "source" | "catalog" +// The embedded role/catalog statements are the SAME fixed statements used by +// their standalone observers. No provider/caller can supply a SQL fragment. +const OBSERVE = `/* bandwidth-control:identity */ WITH + core_role AS MATERIALIZED (${BANDWIDTH_CONTROL_ROLE_SQL}), + core_database AS MATERIALIZED ( + SELECT d.oid::pg_catalog.int8 AS oid,r.rolname AS owner + FROM pg_catalog.pg_database d JOIN pg_catalog.pg_roles r ON r.oid=d.datdba + WHERE d.datname=pg_catalog.current_database() LIMIT 2), + core_isolation AS MATERIALIZED ( + SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_roles r WHERE r.rolname=$4 AND + (r.rolsuper OR r.rolcreaterole OR r.rolreplication OR r.rolbypassrls + OR EXISTS(SELECT 1 FROM pg_catalog.pg_roles target WHERE target.rolname=ANY(ARRAY[$2,$3]::pg_catalog.text[]) AND pg_catalog.pg_has_role(r.oid,target.oid,'MEMBER')) + OR pg_catalog.has_schema_privilege(r.oid,'mmapp_control','USAGE') OR pg_catalog.has_schema_privilege(r.oid,'mmapp_control','CREATE'))) AS unsafe), + core_source AS MATERIALIZED ( + SELECT installation_id::pg_catalog.text,control_generation::pg_catalog.text,attestation_sha256,event_key_sha256,status + FROM mmapp_control.bandwidth_source_identity WHERE id=1 LIMIT 2), + core_catalog AS MATERIALIZED (${BANDWIDTH_CONTROL_CATALOG_SQL}), + bundled AS ( + SELECT 'role'::pg_catalog.text AS kind,pg_catalog.to_jsonb(r)::pg_catalog.text AS raw_json FROM core_role r + UNION ALL SELECT 'database',pg_catalog.to_jsonb(r)::pg_catalog.text FROM core_database r + UNION ALL SELECT 'isolation',pg_catalog.to_jsonb(r)::pg_catalog.text FROM core_isolation r + UNION ALL SELECT 'source',pg_catalog.to_jsonb(r)::pg_catalog.text FROM core_source r + UNION ALL SELECT 'catalog',pg_catalog.to_jsonb(r)::pg_catalog.text FROM core_catalog r), + metered AS ( + SELECT kind,raw_json,pg_catalog.row_number() OVER(ORDER BY kind COLLATE "C",raw_json COLLATE "C") AS row_order, + pg_catalog.count(*) OVER() AS row_count,pg_catalog.sum(pg_catalog.octet_length(raw_json)) OVER() AS total_bytes + FROM bundled) + SELECT kind,row_order::pg_catalog.text AS row_order, + CASE WHEN row_count<=234 AND total_bytes<=4194304 AND pg_catalog.octet_length(raw_json)<=131200 THEN raw_json END AS raw_json + FROM metered ORDER BY metered.row_order LIMIT 235` + +export const BANDWIDTH_CONTROL_IDENTITY_SQL = OBSERVE + +export async function assertBandwidthControlIdentity(sql: BandwidthControlIdentitySql, expected: BandwidthControlIdentityExpected, live: () => void): Promise { + // Capture immutable expectations before awaiting the driver. No new source + // generation, owner, source hash or catalog digest may be adopted from rows. + const captured = Object.freeze(expectedSchema.parse(expected)) + captureBandwidthControlRoleExpected({ databaseName: captured.databaseName, runtimeRole: captured.runtimeRole, ownerRole: captured.ownerRole }) + live() + let result: { rows: Record[] } + try { result = await sql.query(OBSERVE, [captured.databaseName, captured.runtimeRole, captured.ownerRole, captured.applicationRole]) } + catch { live(); refuse() } + live() + validateBandwidthControlIdentityRows(result?.rows,captured) + live() +} + +/** Pure exact counterpart of the existing five-observation validator. */ +export function validateBandwidthControlIdentityRows(rows:unknown,expected:BandwidthControlIdentityExpected):void { + const captured=Object.freeze(expectedSchema.parse(expected)) + const role=captureBandwidthControlRoleExpected({databaseName:captured.databaseName,runtimeRole:captured.runtimeRole,ownerRole:captured.ownerRole}) + if (!Array.isArray(rows) || rows.length > 234) refuse() + const found: Record[]> = { role: [], database: [], isolation: [], source: [], catalog: [] } + const ordinals = new Set() + let bytes = 0 + for (const row of rows) { + if (!plain(row) || Object.keys(row).sort().join(",") !== "kind,raw_json,row_order" || typeof row.kind !== "string" || !Object.hasOwn(found, row.kind) + || typeof row.row_order !== "string" || !/^[1-9][0-9]{0,2}$/.test(row.row_order) || typeof row.raw_json !== "string") refuse() + const ordinal = Number(row.row_order) + if (ordinal > rows.length || ordinals.has(ordinal)) refuse() + ordinals.add(ordinal) + const size = Buffer.byteLength(row.raw_json); bytes += size + if (size > 131200 || bytes > 4194304) refuse() + let value: unknown + try { value = JSON.parse(row.raw_json) } catch { refuse() } + if (!plain(value)) refuse() + const kind = row.kind as Kind + found[kind].push(value) + if (found[kind].length > (kind === "catalog" ? 230 : 1)) refuse() + } + validateBandwidthControlRuntimeRoleRows(found.role, role) + const facts = found.database, isolation = found.isolation, source = found.source + if (facts.length !== 1 || Number(facts[0].oid) !== captured.databaseOid || facts[0].owner !== captured.ownerRole) refuse() + if (isolation.length !== 1 || isolation[0].unsafe !== false) refuse() + if (source.length !== 1 || source[0].installation_id !== captured.installationId || source[0].control_generation !== captured.controlGeneration + || source[0].attestation_sha256 !== captured.sourceRecordSha256 || source[0].event_key_sha256 !== captured.eventKeySha256 || source[0].status !== "active") refuse() + if (validateBandwidthControlCatalogRows(found.catalog) !== captured.catalogSha256) refuse() +} diff --git a/backend/src/services/bandwidth-control-provision.ts b/backend/src/services/bandwidth-control-provision.ts new file mode 100644 index 00000000..6e769859 --- /dev/null +++ b/backend/src/services/bandwidth-control-provision.ts @@ -0,0 +1,119 @@ +/** Explicit offline owner operation. Never imported by startup, MCP or native + * transport. Seals an already installed EMPTY control store; does not enroll + * routers, create keys/roles/schema, or certify control-provider rollback. */ +import { createHash, createPrivateKey, createPublicKey, randomUUID, sign } from "node:crypto" +import { constants } from "node:fs" +import { link, lstat, open, realpath } from "node:fs/promises" +import { dirname, isAbsolute, resolve } from "node:path" +import { Pool, type PoolClient } from "pg" +import { z } from "zod" +import { assertBandwidthControlRuntimeRole } from "./bandwidth-control-role.js" +import { + bandwidthControlEndpointSha256, bandwidthControlSchemaSourceSha256, + canonicalBandwidthControlRecord, createConfiguredBandwidthControlSource, + observeBandwidthControlCatalog, readBandwidthControlFile, + type BandwidthControlRecord, +} from "./bandwidth-control-source.js" + +export const bandwidthControlProvisionConfigSchema=z.object({ + ownerDatabaseUrl:z.string().min(1),databaseUrl:z.string().min(1),applicationDatabaseUrl:z.string().min(1), + attestationFile:z.string().min(1),trustKeyFile:z.string().min(1),trustKeySha256:z.string().regex(/^[a-f0-9]{64}$/), + eventKeyFile:z.string().min(1),issuerPrivateKeyFile:z.string().min(1),attestationRef:z.string().trim().min(30).max(2000), +}).strict() +export class BandwidthControlProvisionError extends Error { + readonly code="BANDWIDTH_CONTROL_PROVISION_UNCONFIRMED" + constructor(){super("Выдача привязки control source не подтверждена; проверьте pending-файл и запись источника до повторной операции")} +} +const sha=(value:Buffer|string)=>createHash("sha256").update(value).digest("hex") +async function syncParent(path:string){ + const directory=dirname(path) + if(await realpath(directory)!==directory)throw new BandwidthControlProvisionError() + const handle=await open(directory,constants.O_RDONLY|(constants.O_DIRECTORY??0)|(constants.O_NOFOLLOW??0)) + try{if(!(await handle.stat()).isDirectory())throw new BandwidthControlProvisionError();await handle.sync()}finally{await handle.close()} +} + +/** Existing pending/final files are never overwritten or removed. If COMMIT or + * publication is uncertain, the operator must reconcile the original signed + * record with the immutable identity. This function never invents a recovery + * generation and never releases occupancy. */ +export async function sealEmptyBandwidthControlSource(input:unknown):Promise>{ + let owner:Pool|undefined,runtime:Pool|undefined,client:PoolClient|undefined + let eventKey:Buffer|undefined,privateBytes:Buffer|undefined + let transaction=false,commitStarted=false,ownerFailed=false + const deadline=Date.now()+15000 + const live=()=>{if(ownerFailed||Date.now()>=deadline)throw new BandwidthControlProvisionError()} + const timer=setTimeout(()=>{client?.release(true);client=undefined},15000) + try{ + const config=bandwidthControlProvisionConfigSchema.parse(input) + const paths=[config.attestationFile,config.trustKeyFile,config.eventKeyFile,config.issuerPrivateKeyFile] + if(paths.some(path=>!isAbsolute(path)||resolve(path)!==path)||new Set(paths).size!==paths.length)throw new Error() + // Constructor validates separate app/control DB and fixed pinned file config + // before owner I/O. It performs no connection or automatic enrollment. + const validation=createConfiguredBandwidthControlSource(config);await validation.close() + for(const path of [config.attestationFile,config.attestationFile+".pending"]){ + try{await lstat(path);throw new BandwidthControlProvisionError()} + catch(error){if((error as NodeJS.ErrnoException).code!=="ENOENT")throw error} + } + // A platform/filesystem without durable directory flush cannot issue this + // binding. In particular do not silently weaken this on Windows/EPERM. + await syncParent(config.attestationFile);live() + const target=new URL(config.databaseUrl),ownerUrl=new URL(config.ownerDatabaseUrl) + const runtimeRole=decodeURIComponent(target.username),ownerRole=decodeURIComponent(ownerUrl.username) + const databaseName=decodeURIComponent(target.pathname.slice(1)) + if(!/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(ownerRole)||ownerRole===runtimeRole)throw new Error() + // Password and owner username are the only allowed connection differences. + const ownerTarget=new URL(ownerUrl);ownerTarget.username=target.username;ownerTarget.password=target.password + if(bandwidthControlEndpointSha256(ownerTarget.toString())!==bandwidthControlEndpointSha256(target.toString()))throw new Error() + const publicBytes=await readBandwidthControlFile(config.trustKeyFile,16384,live) + const publicKey=createPublicKey(publicBytes) + if(publicKey.asymmetricKeyType!=="ed25519"||sha(publicKey.export({type:"spki",format:"der"}))!==config.trustKeySha256)throw new Error() + privateBytes=await readBandwidthControlFile(config.issuerPrivateKeyFile,16384,live) + const privateKey=createPrivateKey(privateBytes) + if(privateKey.asymmetricKeyType!=="ed25519"||!createPublicKey(privateKey).export({type:"spki",format:"der"}).equals(publicKey.export({type:"spki",format:"der"})))throw new Error() + eventKey=await readBandwidthControlFile(config.eventKeyFile,32,live);if(eventKey.length!==32)throw new Error() + owner=new Pool({connectionString:config.ownerDatabaseUrl,max:1,connectionTimeoutMillis:3000,query_timeout:5000,options:"-c search_path=pg_catalog"}) + runtime=new Pool({connectionString:config.databaseUrl,max:1,connectionTimeoutMillis:3000,query_timeout:5000,options:"-c search_path=pg_catalog"}) + owner.on("error",()=>{});runtime.on("error",()=>{}) + client=await owner.connect();client.on("error",()=>{ownerFailed=true});live() + await client.query("BEGIN");transaction=true + await client.query("SET LOCAL statement_timeout='5s'") + await client.query("SET LOCAL lock_timeout='2s'") + await client.query("SET LOCAL synchronous_commit=on") + await client.query("LOCK TABLE mmapp_control.bandwidth_attempts,mmapp_control.bandwidth_node_occupancy,mmapp_control.bandwidth_attempt_events,mmapp_control.bandwidth_source_identity IN SHARE ROW EXCLUSIVE MODE") + const facts=(await client.query(`SELECT d.oid::pg_catalog.int8 AS oid,d.datname,r.rolname AS owner, + SESSION_USER AS session_role,CURRENT_USER AS current_role, + r.rolsuper OR r.rolcreatedb OR r.rolcreaterole OR r.rolreplication OR r.rolbypassrls AS unsafe_owner FROM pg_catalog.pg_database d + JOIN pg_catalog.pg_roles r ON r.oid=d.datdba WHERE d.datname=pg_catalog.current_database()`)).rows + if(facts.length!==1||facts[0].datname!==databaseName||facts[0].owner!==ownerRole||facts[0].session_role!==ownerRole||facts[0].current_role!==ownerRole||facts[0].unsafe_owner!==false)throw new Error() + const state=(await client.query(`SELECT + EXISTS(SELECT 1 FROM mmapp_control.bandwidth_attempts) OR EXISTS(SELECT 1 FROM mmapp_control.bandwidth_node_occupancy) + OR EXISTS(SELECT 1 FROM mmapp_control.bandwidth_attempt_events) OR EXISTS(SELECT 1 FROM mmapp_control.bandwidth_source_identity) AS occupied`)).rows + if(state.length!==1||state[0].occupied!==false)throw new Error() + await assertBandwidthControlRuntimeRole(runtime,{databaseName,runtimeRole,ownerRole},live) + const record:BandwidthControlRecord={format:"mmapp-bandwidth-control-source-v1",installationId:randomUUID(),controlGeneration:randomUUID(), + databaseName,databaseOid:Number(facts[0].oid),runtimeRole,ownerRole,endpointSha256:bandwidthControlEndpointSha256(config.databaseUrl), + schemaSourceSha256:bandwidthControlSchemaSourceSha256(),catalogSha256:await observeBandwidthControlCatalog(runtime,live), + eventKeySha256:sha(eventKey),attestationRef:config.attestationRef} + const bytes=Buffer.from(canonicalBandwidthControlRecord(record)),pending=config.attestationFile+".pending" + const envelope=JSON.stringify({format:"mmapp-bandwidth-control-envelope-v1",payload:bytes.toString("base64url"),signature:sign(null,bytes,privateKey).toString("base64url")}) + // wx is the durable attempt marker. Never truncate it on rerun. Publishing + // also uses a no-replace hard link, so an existing attestation is untouched. + const file=await open(pending,"wx",0o600) + try{await file.writeFile(envelope);await file.sync()}finally{await file.close()} + await syncParent(pending) + live() + await client.query("INSERT INTO mmapp_control.bandwidth_source_identity(id,installation_id,control_generation,attestation_sha256,event_key_sha256) VALUES(1,$1,$2,$3,$4)",[record.installationId,record.controlGeneration,sha(bytes),record.eventKeySha256]) + live();commitStarted=true;await client.query("COMMIT");transaction=false;live() + client.release();client=undefined + const checked=createConfiguredBandwidthControlSource({...config,attestationFile:pending}) + try{await checked.identity({deadlineAt:Math.min(deadline,Date.now()+10000)})}finally{await checked.close()} + live();await link(pending,config.attestationFile);await syncParent(config.attestationFile);live() + return Object.freeze(record) + }catch{ + if(transaction&&!commitStarted&&client)try{await client.query("ROLLBACK")}catch{/* Destroy below; no retry. */} + throw new BandwidthControlProvisionError() + }finally{ + clearTimeout(timer);eventKey?.fill(0);privateBytes?.fill(0);client?.release(true) + await Promise.allSettled([owner?.end(),runtime?.end()]) + } +} diff --git a/backend/src/services/bandwidth-control-public-metadata.test.ts b/backend/src/services/bandwidth-control-public-metadata.test.ts new file mode 100644 index 00000000..2bf6389a --- /dev/null +++ b/backend/src/services/bandwidth-control-public-metadata.test.ts @@ -0,0 +1,184 @@ +/** Actual bounded files/Ed25519. Only IO scheduling and forbidden secret/PG + * traps are mocked; no authority, query result or signature is substituted. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { basename, dirname, join } from "node:path" +import { tmpdir } from "node:os" +import { mock, test } from "node:test" +import type { BandwidthPublicNodeBootstrapMetadata, BandwidthVerifiedNodeBootstrap } from "./bandwidth-control-source.js" + +const fs=await import("node:fs/promises"),pg=await import("pg") +const forbidden=new Set(),gates=new Map>(),reads:string[]=[] +let secretAttempts=0,poolAttempts=0 +const check=(path:unknown)=>{if(typeof path==="string"&&forbidden.has(path)){secretAttempts++;throw Error("secret IO forbidden")}} +mock.module("node:fs/promises",{namedExports:{...fs, + async lstat(...args:Parameters){check(args[0]);reads.push(String(args[0]));await gates.get(String(args[0]));return Reflect.apply(fs.lstat,fs,args)}, + async realpath(...args:Parameters){check(args[0]);return Reflect.apply(fs.realpath,fs,args)}, + async open(...args:Parameters){check(args[0]);return Reflect.apply(fs.open,fs,args)}, +}}) +const {default:pgDefault,...pgNamed}=pg +class ForbiddenPool {constructor(){poolAttempts++;throw Error("public metadata attempted PG")}} +mock.module("pg",{defaultExport:{...pgDefault,Pool:ForbiddenPool},namedExports:{...pgNamed,Pool:ForbiddenPool}}) +const core=await import("./bandwidth-control-source.js") +const node=await import("./bandwidth-node-bootstrap.js") +const sha=(value:string|Buffer)=>createHash("sha256").update(value).digest("hex") +const turn=()=>new Promise(done=>setImmediate(done)) +const scope=(signal?:AbortSignal)=>({deadlineAt:Date.now()+5000,signal}) + +function distinctPublicType(value:BandwidthPublicNodeBootstrapMetadata){ + // @ts-expect-error PUBLIC projection cannot satisfy strong database source. + const strong:BandwidthVerifiedNodeBootstrap=value + void strong + return value +} +async function fixture(){ + const directory=await fs.mkdtemp(join(tmpdir(),"mm-control-public-")) + const keys=generateKeyPairSync("ed25519"),eventKeyFile=join(directory,"event.secret"),trustKeyFile=join(directory,"trust.pem"),attestationFile=join(directory,"attestation.json") + const config={databaseUrl:"postgresql://control_runtime:secret@127.0.0.1:1/control",applicationDatabaseUrl:"postgresql://application:secret@127.0.0.1:1/application",eventKeyFile,trustKeyFile,attestationFile,trustKeySha256:sha(keys.publicKey.export({type:"spki",format:"der"}))} + let record={format:"mmapp-bandwidth-control-source-v1" as const,installationId:randomUUID(),controlGeneration:randomUUID(),databaseName:"control",databaseOid:123,runtimeRole:"control_runtime",ownerRole:"control_owner",endpointSha256:core.bandwidthControlEndpointSha256(config.databaseUrl),schemaSourceSha256:core.bandwidthControlSchemaSourceSha256(),catalogSha256:"a".repeat(64),eventKeySha256:"e".repeat(64),attestationRef:"explicit public-only control fixture attestation reference"} + const bootstrap={format:"mmapp-bandwidth-node-bootstrap-v1" as "mmapp-bandwidth-node-bootstrap-v1"|"mmapp-bandwidth-node-bootstrap-v2",installationId:record.installationId,issuerKeySha256:"b".repeat(64),quiescenceProof:"c".repeat(64),registrySchemaSourceSha256:node.bandwidthNodeRegistrySchemaSourceSha256(),registryCatalogSha256:"d".repeat(64),attestationRef:"explicit public-only node fixture attestation reference"} + const envelope=(text:string,format:string)=>{const payload=Buffer.from(text);return {format,payload:payload.toString("base64url"),signature:sign(null,payload,keys.privateKey).toString("base64url")}} + const signedNode=(text=node.canonicalBandwidthNodeBootstrap(bootstrap))=>envelope(text,"mmapp-bandwidth-node-bootstrap-envelope-v1") + const publish=async(text=core.canonicalBandwidthControlRecord(record))=>fs.writeFile(attestationFile,JSON.stringify(envelope(text,"mmapp-bandwidth-control-envelope-v1"))) + await fs.writeFile(trustKeyFile,keys.publicKey.export({type:"spki",format:"pem"}));await publish() + forbidden.add(eventKeyFile) + const reader=core.createConfiguredBandwidthControlSource(config) + return {reader,config,bootstrap,signedNode,publish,get record(){return record},set record(value){record=value},async close(){ + gates.delete(trustKeyFile);gates.delete(attestationFile);await reader.close();await turn();await turn() + forbidden.delete(eventKeyFile) + const actual=await fs.realpath(directory),parent=await fs.realpath(tmpdir()),stat=await fs.lstat(directory) + assert(stat.isDirectory()&&!stat.isSymbolicLink());assert.equal(dirname(actual),parent);assert.match(basename(actual),/^mm-control-public-[A-Za-z0-9]+$/) + await fs.rm(actual,{recursive:true,force:true}) + assert.equal(secretAttempts,0);assert.equal(poolAttempts,0) + }} +} + +test("inert construction; actual signed public v1/v2 with missing/permission-marked secret, secret read trap and zero PG",async()=>{ + const before=reads.length,f=await fixture() + try{ + assert.equal(reads.length,before) + for(const secret of ["missing","permission-denied"]){ + if(secret==="permission-denied"){await fs.writeFile(f.config.eventKeyFile,Buffer.alloc(32));await fs.chmod(f.config.eventKeyFile,0)} + for(const format of ["mmapp-bandwidth-node-bootstrap-v1","mmapp-bandwidth-node-bootstrap-v2"] as const){ + f.bootstrap.format=format;f.bootstrap.registrySchemaSourceSha256=format.endsWith("v2")?node.bandwidthPeerRegistrySchemaSourceSha256():node.bandwidthNodeRegistrySchemaSourceSha256() + const value=distinctPublicType(await f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope())) + assert.equal(value.format,"mmapp-bandwidth-public-node-metadata-v1");assert.deepEqual(value.bootstrap,f.bootstrap) + assert.equal(value.source.sourceRecordSha256,sha(core.canonicalBandwidthControlRecord(f.record))) + assert.deepEqual(value.publicDatabase,{databaseName:"control",runtimeRole:"control_runtime",ownerRole:"control_owner",databaseOid:123,catalogSha256:"a".repeat(64)}) + for(const part of [value,value.bootstrap,value.source,value.publicDatabase])assert(Object.isFrozen(part)) + assert.equal("runtimeRole"in value.source,false);assert.equal("keyCurrent"in value,false);assert.equal("credentialsVerified"in value,false) + } + } + }finally{await fs.chmod(f.config.eventKeyFile,0o600).catch(()=>{});await f.close()} +}) + +test("fresh control generation/hash and pinned public trust; no prior public result grants strong access",async()=>{ + const f=await fixture() + try{ + const first=await f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope()) + f.record={...f.record,controlGeneration:randomUUID()};await f.publish() + const current=await f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope());assert.notDeepEqual(current.source,first.source) + // Public/copied objects never create a genuine configured connection. + await assert.rejects(f.reader.verifyNodeBootstrap(f.signedNode(),first as never,scope())) + await assert.rejects(f.reader.verifyNodeBootstrap(f.signedNode(),structuredClone(current) as never,scope())) + await fs.writeFile(f.config.trustKeyFile,generateKeyPairSync("ed25519").publicKey.export({type:"spki",format:"pem"})) + await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope())) + }finally{await f.close()} +}) + +test("genuine configured CORE -> NODE public composition rereads signed files with no issuer/event key or SQL",async()=>{ + const {createConfiguredBandwidthNodeSource}=await import("./bandwidth-node-source.js") + const f=await fixture(),bootstrapFile=join(dirname(f.config.attestationFile),"node-bootstrap.json"),issuerKeyFile=join(dirname(f.config.attestationFile),"issuer.secret") + forbidden.add(issuerKeyFile) + const reader=createConfiguredBandwidthNodeSource(f.reader,{bootstrapFile,issuerKeyFile,issuerKeySha256:f.bootstrap.issuerKeySha256}) + try{ + for(const format of ["mmapp-bandwidth-node-bootstrap-v1","mmapp-bandwidth-node-bootstrap-v2"] as const){ + f.bootstrap.format=format;f.bootstrap.registrySchemaSourceSha256=format.endsWith("v2")?node.bandwidthPeerRegistrySchemaSourceSha256():node.bandwidthNodeRegistrySchemaSourceSha256() + await fs.writeFile(bootstrapFile,JSON.stringify(f.signedNode())) + const before=reads.length,first=await reader.inspectPublicBootstrap(scope()) + assert.equal(first.format,"mmapp-bandwidth-public-node-metadata-v1") + assert.equal(first.bootstrapSha256,sha(node.canonicalBandwidthNodeBootstrap(f.bootstrap))) + assert.equal(first.source.sourceRecordSha256,sha(core.canonicalBandwidthControlRecord(f.record))) + assert.equal(first.source.controlGeneration,f.record.controlGeneration) + assert.equal(first.publicDatabase.databaseOid,123) + assert.deepEqual(reads.slice(before).sort(),[bootstrapFile,bootstrapFile,f.config.trustKeyFile,f.config.trustKeyFile,f.config.attestationFile,f.config.attestationFile].sort()) + for(const part of [first,first.bootstrap,first.source,first.publicDatabase])assert(Object.isFrozen(part)) + f.record={...f.record,controlGeneration:randomUUID()};await f.publish() + const current=await reader.inspectPublicBootstrap(scope()) + assert.equal(current.source.controlGeneration,f.record.controlGeneration) + assert.notDeepEqual(current.source,first.source) + assert.equal(current.bootstrapSha256,first.bootstrapSha256) + assert.equal(secretAttempts,0);assert.equal(poolAttempts,0) + } + }finally{reader.close();forbidden.delete(issuerKeyFile);await f.close()} +}) + +test("control signature/canonical/schema/endpoint/database/roles and node signature/install/schema remain mandatory",async()=>{ + const f=await fixture(),original={...f.record},bootstrap={...f.bootstrap} + try{ + for(const change of [{databaseName:"elsewhere"},{runtimeRole:"other_runtime"},{ownerRole:"control_runtime"},{endpointSha256:"0".repeat(64)},{schemaSourceSha256:"0".repeat(64)}]){ + f.record={...original,...change};await f.publish();await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope())) + } + f.record=original;await f.publish(" "+core.canonicalBandwidthControlRecord(original));await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope())) + await f.publish();const corrupt=JSON.parse(await fs.readFile(f.config.attestationFile,"utf8"));corrupt.signature="A".repeat(86);await fs.writeFile(f.config.attestationFile,JSON.stringify(corrupt));await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope())) + await f.publish() + for(const change of [{installationId:randomUUID()},{registrySchemaSourceSha256:"0".repeat(64)},{issuerKeySha256:original.eventKeySha256}]){ + Object.assign(f.bootstrap,bootstrap,change);await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope())) + } + Object.assign(f.bootstrap,bootstrap) + await assert.rejects(f.reader.inspectPublicNodeBootstrap({...f.signedNode(),signature:"A".repeat(86)},scope())) + await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(" "+node.canonicalBandwidthNodeBootstrap(bootstrap)),scope())) + }finally{await f.close()} +}) + +test("accessors/proxies/toJSON are refused without executing caller code or public IO",async()=>{ + const f=await fixture();let called=0 + const trap=()=>{called++;throw Error("caller code")} + try{ + const before=reads.length + for(const envelope of [Object.defineProperty({...f.signedNode()},"payload",{get:trap}),{...f.signedNode(),toJSON:trap},new Proxy(f.signedNode(),{getPrototypeOf:trap})])await assert.rejects(f.reader.inspectPublicNodeBootstrap(envelope,scope())) + for(const input of [Object.defineProperty(scope(),"deadlineAt",{get:trap}),{...scope(),toJSON:trap},new Proxy(scope(),{ownKeys:trap})])await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),input)) + const aborted=new AbortController();aborted.abort() + for(const input of [{deadlineAt:Date.now()-1},scope(aborted.signal),{deadlineAt:NaN}])await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),input)) + assert.equal(called,0);assert.equal(reads.length,before) + const hostileSignal=new AbortController().signal + for(const property of ["aborted","addEventListener","removeEventListener"])Object.defineProperty(hostileSignal,property,{get:trap}) + assert.deepEqual((await f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope(hostileSignal))).bootstrap,f.bootstrap) + assert.equal(called,0) + }finally{await f.close()} +}) + +test("envelope and original scope captured before blocked IO; caller mutation cannot switch verified input",async()=>{ + const f=await fixture();let release!:()=>void + try{ + gates.set(f.config.trustKeyFile,new Promise(done=>{release=done})) + const signed=f.signedNode(),input=scope(),pending=f.reader.inspectPublicNodeBootstrap(signed,input) + await turn();signed.signature="A".repeat(86);input.deadlineAt=0;release() + assert.deepEqual((await pending).bootstrap,f.bootstrap) + }finally{release?.();await f.close()} +}) + +test("abort/deadline/close reject blocked reads before release and late IO cannot publish evidence",async()=>{ + for(const mode of ["abort","deadline","close"]){ + const f=await fixture();let release!:()=>void + try{ + gates.set(f.config.trustKeyFile,new Promise(done=>{release=done})) + const controller=new AbortController(),input=scope(controller.signal) + if(mode==="deadline")input.deadlineAt=Date.now()+30 + const pending=f.reader.inspectPublicNodeBootstrap(f.signedNode(),input);void pending.catch(()=>{}) + await turn();if(mode==="abort")controller.abort();if(mode==="close")await f.reader.close() + await assert.rejects(pending,{code:"BANDWIDTH_CONTROL_SOURCE_UNAVAILABLE"}) + release();await turn();await turn() + if(mode==="close")await assert.rejects(f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope())) + }finally{release?.();await f.close()} + } +}) + +test("wall rollback is refused after awaited IO, without renewing the original monotonic horizon",async()=>{ + const f=await fixture(),now=Date.now;let release!:()=>void + try{ + const initial=now();gates.set(f.config.trustKeyFile,new Promise(done=>{release=done})) + const pending=f.reader.inspectPublicNodeBootstrap(f.signedNode(),scope());void pending.catch(()=>{}) + await turn();Date.now=()=>initial-1000;release();await assert.rejects(pending) + }finally{Date.now=now;release?.();await f.close()} +}) diff --git a/backend/src/services/bandwidth-control-public-registry.test.ts b/backend/src/services/bandwidth-control-public-registry.test.ts new file mode 100644 index 00000000..0f7fd84e --- /dev/null +++ b/backend/src/services/bandwidth-control-public-registry.test.ts @@ -0,0 +1,246 @@ +/** SYNTHETIC raw PostgreSQL transport, genuine signed files and ORIGINAL pure + * role/catalog validators. No actual PG, Docker, network or secret key reads. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { EventEmitter } from "node:events" +import { basename, dirname, join } from "node:path" +import { tmpdir } from "node:os" +import { mock, test } from "node:test" +import { Pool } from "pg" +import { expected as coreExpected, rows as coreRows } from "../test/fixtures/bandwidth-control-identity.js" +import { fixture as managedFixture } from "../test/fixtures/bandwidth-node-identity.js" +import { fixture as peerFixture } from "../test/fixtures/bandwidth-peer-identity.js" + +const fs=await import("node:fs/promises"),secretFiles=new Set(),reads:string[]=[] +let secretAttempts=0 +mock.module("node:fs/promises",{namedExports:{...fs,async lstat(...args:Parameters){ + if(secretFiles.has(String(args[0]))){secretAttempts++;throw Error("forbidden secret IO")} + reads.push(String(args[0]));return Reflect.apply(fs.lstat,fs,args) +}}}) +const core=await import("./bandwidth-control-source.js"),node=await import("./bandwidth-node-bootstrap.js") +const sha=(value:string|Buffer)=>createHash("sha256").update(value).digest("hex") +const PID="SELECT pg_catalog.pg_backend_pid() AS backend_pid",LOCK="SELECT pg_catalog.pg_try_advisory_lock(8723116) AS acquired" +const turn=()=>new Promise(done=>setImmediate(done)) +const scope=(signal?:AbortSignal)=>({deadlineAt:Date.now()+5000,signal}) +const deferred=()=>{let resolve!:()=>void;const promise=new Promise(done=>{resolve=done});return {promise,resolve}} +type RawResult={command:string;rows:Record[]} +type Tag="PID"|"LOCK"|"HELD"|"JOINT" + +async function fixture(version=1){ + const directory=await fs.mkdtemp(join(await fs.realpath(tmpdir()),"mm-public-registry-")),keys=generateKeyPairSync("ed25519") + const registry=version===1?managedFixture():peerFixture(),config={databaseUrl:"postgresql://bandwidth_runtime:synthetic-password@127.0.0.1:1/owned_database",applicationDatabaseUrl:"postgresql://restricted_application:synthetic-password@127.0.0.1:1/application_database",eventKeyFile:join(directory,"event.secret"),trustKeyFile:join(directory,"trust.pem"),attestationFile:join(directory,"source.json"),trustKeySha256:sha(keys.publicKey.export({type:"spki",format:"der"}))} + let record={format:"mmapp-bandwidth-control-source-v1" as const,installationId:registry.expected.installationId,controlGeneration:randomUUID(),databaseName:"owned_database",databaseOid:123,runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",endpointSha256:core.bandwidthControlEndpointSha256(config.databaseUrl),schemaSourceSha256:core.bandwidthControlSchemaSourceSha256(),catalogSha256:coreExpected.catalogSha256,eventKeySha256:"e".repeat(64),attestationRef:"synthetic public registry control fixture, not operating acceptance"} + const bootstrap={format:version===1?"mmapp-bandwidth-node-bootstrap-v1" as const:"mmapp-bandwidth-node-bootstrap-v2" as const,installationId:record.installationId,issuerKeySha256:registry.expected.issuerKeySha256,quiescenceProof:registry.expected.quiescenceProof,registrySchemaSourceSha256:version===1?node.bandwidthNodeRegistrySchemaSourceSha256():node.bandwidthPeerRegistrySchemaSourceSha256(),registryCatalogSha256:registry.expected.registryCatalogSha256,attestationRef:"synthetic public registry bootstrap fixture, not operator authority"} + const signed=(format:string,text:string)=>{const payload=Buffer.from(text);return {format,payload:payload.toString("base64url"),signature:sign(null,payload,keys.privateKey).toString("base64url")}} + const envelope=()=>signed("mmapp-bandwidth-node-bootstrap-envelope-v1",node.canonicalBandwidthNodeBootstrap(bootstrap)) + const publish=()=>fs.writeFile(config.attestationFile,JSON.stringify(signed("mmapp-bandwidth-control-envelope-v1",core.canonicalBandwidthControlRecord(record)))) + await fs.writeFile(config.trustKeyFile,keys.publicKey.export({type:"spki",format:"pem"}));await publish();secretFiles.add(config.eventKeyFile) + Object.assign(coreExpected,{installationId:record.installationId,controlGeneration:record.controlGeneration,sourceRecordSha256:sha(core.canonicalBandwidthControlRecord(record)),eventKeySha256:record.eventKeySha256}) + const originalConnect=Pool.prototype.connect,ledger:Array<{tag:Tag;pid:number}>=[],destroyed:number[]=[] + const connectEntered=deferred() + let checkouts=0,nextPid=700,lockOwner:number|undefined + const controls:{query?:((tag:Tag,result:RawResult)=>Promise|RawResult);connectGate?:Promise}={} + Pool.prototype.connect=(async()=>{ + checkouts++;connectEntered.resolve();const pid=++nextPid + await controls.connectGate + return Object.assign(new EventEmitter(),{async query(input:{text:string;values?:unknown[]}){ + const text=input.text,tag:Tag=text===PID?"PID":text===LOCK?"LOCK":text.includes("pg_catalog.pg_locks")?"HELD":"JOINT" + ledger.push({tag,pid}) + let result:RawResult + if(tag==="PID")result={command:"SELECT",rows:[{backend_pid:pid}]} + else if(tag==="LOCK"){const acquired=lockOwner===undefined||lockOwner===pid;if(acquired)lockOwner=pid;result={command:"SELECT",rows:[{acquired}]}} + else if(tag==="HELD")result={command:"SELECT",rows:[{backend_pid:pid,held:lockOwner===pid}]} + else{ + assert(text.startsWith("/* bandwidth-control:registry-identity */"));assert.deepEqual(input.values,[record.databaseName,record.runtimeRole,record.ownerRole,"restricted_application"]) + result={command:"SELECT",rows:[...coreRows().map(row=>({family:"core",...row})),...registry.rows.map(row=>({family:"registry",...row}))]} + } + return controls.query?controls.query(tag,result):result + },release(unsafe:boolean){assert.equal(unsafe,true);assert(!destroyed.includes(pid));destroyed.push(pid);if(lockOwner===pid)lockOwner=undefined}}) + }) as never + const source=core.createConfiguredBandwidthControlSource(config) + return {source,config,bootstrap,envelope,publish,controls,ledger,destroyed,connectEntered,get record(){return record},set record(value){record=value},get checkouts(){return checkouts},get lockOwner(){return lockOwner},async close(){ + await source.close();await turn();await turn();Pool.prototype.connect=originalConnect;secretFiles.delete(config.eventKeyFile) + assert.equal(secretAttempts,0) + const actual=await fs.realpath(directory),parent=await fs.realpath(tmpdir()),stat=await fs.lstat(directory) + assert(stat.isDirectory()&&!stat.isSymbolicLink());assert.equal(dirname(actual),parent);assert.match(basename(actual),/^mm-public-registry-[A-Za-z0-9]+$/) + await fs.rm(actual,{recursive:true,force:true}) + }} +} + +test("public registry v1/v2 uses original validators, one physical raw lease and fixed SQL; no strong/query hooks",async()=>{ + for(const version of [1,2]){ + const before=reads.length,f=await fixture(version) + try{ + assert.equal(f.checkouts,0);assert.equal(reads.length,before) + const lease=await f.source.openPublicRegistryMetadata(f.envelope(),scope()) + assert(Object.isFrozen(lease));assert.deepEqual(Object.keys(lease).sort(),["close","inspect"]) + assert.deepEqual(f.ledger.map(row=>row.tag),["PID","JOINT","LOCK","HELD"]) + for(let index=0;index<2;index++){ + const result=await lease.inspect(f.envelope()) + assert.equal(result.format,"mmapp-bandwidth-public-registry-metadata-v1");assert.equal(result.backendPid,701) + assert.equal(result.metadata.source.sourceRecordSha256,sha(core.canonicalBandwidthControlRecord(f.record))) + for(const value of [result,result.metadata,result.metadata.bootstrap,result.metadata.source,result.metadata.publicDatabase])assert(Object.isFrozen(value)) + for(const key of ["query","verifyNodeBootstrap","keyCurrent","writerBarrier","quiescenceVerified"])assert.equal(key in result,false) + } + assert.deepEqual(f.ledger.map(row=>row.tag),["PID","JOINT","LOCK","HELD","HELD","JOINT","HELD","HELD","JOINT","HELD"]) + assert.equal(f.checkouts,1);assert.equal(new Set(f.ledger.map(row=>row.pid)).size,1) + lease.close();lease.close();assert.deepEqual(f.destroyed,[701]);await assert.rejects(lease.inspect(f.envelope())) + }finally{await f.close()} + } +}) + +test("bad PID/lock SELECT ACK, malformed row and lost lock ACK refuse with no replay or healthy reuse",async()=>{ + for(const scenario of ["pid-ack","pid-string","pid-extra","pid-empty","lock-ack","lock-false","lock-string","lock-extra","lock-lost"]){ + const f=await fixture() + try{ + f.controls.query=(tag,result)=>{ + if(tag==="PID"&&scenario==="pid-ack")return {...result,command:"UPDATE"} + if(tag==="PID"&&scenario==="pid-string")return {...result,rows:[{backend_pid:"701"}]} + if(tag==="PID"&&scenario==="pid-extra")return {...result,rows:[{backend_pid:701,extra:true}]} + if(tag==="PID"&&scenario==="pid-empty")return {...result,rows:[]} + if(tag==="LOCK"&&scenario==="lock-ack")return {...result,command:"UPDATE"} + if(tag==="LOCK"&&scenario==="lock-false")return {...result,rows:[{acquired:false}]} + if(tag==="LOCK"&&scenario==="lock-string")return {...result,rows:[{acquired:"true"}]} + if(tag==="LOCK"&&scenario==="lock-extra")return {...result,rows:[{acquired:true,extra:true}]} + if(tag==="LOCK"&&scenario==="lock-lost")throw Error("synthetic ACK lost AFTER acquisition") + return result + } + await assert.rejects(f.source.openPublicRegistryMetadata(f.envelope(),scope()),scenario) + assert.equal(f.ledger.filter(row=>row.tag==="LOCK").length,scenario.startsWith("pid")?0:1) + assert.deepEqual(f.destroyed,[701]);assert.equal(f.lockOwner,undefined) + }finally{await f.close()} + } +}) + +test("joint role/catalog/source validation precedes lock; malformed/revoked snapshots never publish",async()=>{ + for(const scenario of ["command","core-role","core-source","registry-authority","core-catalog","registry-catalog","missing-family"]){ + const f=await fixture(2) + try{ + f.controls.query=(tag,result)=>{ + if(tag!=="JOINT")return result + if(scenario==="command")return {...result,command:"UPDATE"} + if(scenario==="core-role"){ + const row=result.rows.find(row=>row.family==="core"&&row.kind==="role")!,value=JSON.parse(String(row.raw_json)) + value.observation.runtimeFlagsSafe=false;row.raw_json=JSON.stringify(value);return result + } + if(scenario==="missing-family")return {...result,rows:result.rows.filter(row=>row.family!=="core")} + if(scenario.endsWith("catalog")){ + const rows=result.rows.filter(row=>!(row.family===(scenario.startsWith("core")?"core":"registry")&&String(row.kind).endsWith("catalog"))) + assert(rows.lengthrow.family===(scenario==="core-source"?"core":"registry")&&row.kind===(scenario==="core-source"?"source":"authority"))! + row.raw_json=JSON.stringify({...JSON.parse(String(row.raw_json)),status:"quarantined"});return result + } + await assert.rejects(f.source.openPublicRegistryMetadata(f.envelope(),scope())) + assert.equal(f.ledger.filter(row=>row.tag==="LOCK").length,0);assert.deepEqual(f.destroyed,[701]) + }finally{await f.close()} + } +}) + +test("HELD exact PID/session ExclusiveLock is checked before AND after every identity query",async()=>{ + for(const scenario of ["before-false","before-pid","after-false","after-pid","after-ack"]){ + const f=await fixture() + try{ + const lease=await f.source.openPublicRegistryMetadata(f.envelope(),scope());let held=0,joint=0 + f.controls.query=(tag,result)=>{ + if(tag==="JOINT")joint++ + if(tag==="HELD"){ + held++ + if(held===(scenario.startsWith("before")?1:2)){ + if(scenario.endsWith("ack"))return {...result,command:"UPDATE"} + return {...result,rows:[{backend_pid:scenario.endsWith("pid")?702:701,held:!scenario.endsWith("false")}]} + } + } + return result + } + await assert.rejects(lease.inspect(f.envelope()));assert.equal(joint,scenario.startsWith("before")?0:1);assert.deepEqual(f.destroyed,[701]) + }finally{await f.close()} + } +}) + +test("fresh public source/bootstrap/trust drift pins original; mid-SQL generation change rejects AFTER SQL",async()=>{ + for(const scenario of ["source-before","source-during","bootstrap","trust"]){ + const f=await fixture() + try{ + const lease=await f.source.openPublicRegistryMetadata(f.envelope(),scope()),before=f.ledger.length + const drift=async()=>{f.record={...f.record,controlGeneration:randomUUID()};await f.publish()} + if(scenario==="source-before")await drift() + if(scenario==="source-during")f.controls.query=async(tag,result)=>{if(tag==="JOINT")await drift();return result} + if(scenario==="bootstrap")f.bootstrap.quiescenceProof="f".repeat(64) + if(scenario==="trust")await fs.writeFile(f.config.trustKeyFile,generateKeyPairSync("ed25519").publicKey.export({type:"spki",format:"pem"})) + await assert.rejects(lease.inspect(f.envelope())) + assert.equal(f.ledger.length-before,scenario==="source-during"?3:0);assert.deepEqual(f.destroyed,[701]) + }finally{await f.close()} + } +}) + +test("second open loses lock without unlocking winner; unknown acquisition is never retried",async()=>{ + const f=await fixture() + try{ + const winner=await f.source.openPublicRegistryMetadata(f.envelope(),scope()) + await assert.rejects(f.source.openPublicRegistryMetadata(f.envelope(),scope())) + assert.equal(f.checkouts,2);assert.equal(f.lockOwner,701);assert.deepEqual(f.destroyed,[702]);assert.equal(f.ledger.filter(row=>row.tag==="LOCK").length,2) + assert.equal((await winner.inspect(f.envelope())).backendPid,701);winner.close();assert.deepEqual(f.destroyed,[702,701]) + }finally{await f.close()} +}) + +test("blocked checkout abort/deadline/core close retires late physical lease; no SQL or leaked callback",async()=>{ + for(const scenario of ["abort","deadline","close"]){ + const f=await fixture(),gate=deferred(),controller=new AbortController();f.controls.connectGate=gate.promise + try{ + const input=scope(controller.signal);if(scenario==="deadline")input.deadlineAt=Date.now()+40 + const pending=f.source.openPublicRegistryMetadata(f.envelope(),input);void pending.catch(()=>{}) + await Promise.race([f.connectEntered.promise,pending]) + if(scenario==="abort")controller.abort();if(scenario==="close")await f.source.close() + await assert.rejects(pending);assert.equal(f.ledger.length,0);gate.resolve();await turn();await turn();assert.deepEqual(f.destroyed,[701]) + }finally{gate.resolve();await f.close()} + } +}) + +test("busy, close and abort during physical identity query reject immediately; late SQL cannot publish",async()=>{ + for(const scenario of ["busy","close","abort","deadline"]){ + const f=await fixture(),gate=deferred(),entered=deferred(),controller=new AbortController() + try{ + const input=scope(controller.signal);if(scenario==="deadline")input.deadlineAt=Date.now()+150 + const lease=await f.source.openPublicRegistryMetadata(f.envelope(),input) + f.controls.query=async(tag,result)=>{if(tag==="JOINT"){entered.resolve();await gate.promise}return result} + const pending=lease.inspect(f.envelope());void pending.catch(()=>{});await entered.promise + if(scenario==="busy")await assert.rejects(lease.inspect(f.envelope())) + if(scenario==="close")await f.source.close();if(scenario==="abort")controller.abort() + if(scenario==="deadline")input.deadlineAt=Date.now()+50000 + await assert.rejects(pending);assert.deepEqual(f.destroyed,[701]);const count=f.ledger.length + gate.resolve();await turn();await turn();assert.equal(f.ledger.length,count) + await assert.rejects(lease.inspect(f.envelope())) + }finally{gate.resolve();await f.close()} + } +}) + +test("wall rollback after real awaited SQL destroys the lease without issuing a late HELD query",async()=>{ + const f=await fixture(),gate=deferred(),entered=deferred(),now=Date.now + try{ + const wall=now(),lease=await f.source.openPublicRegistryMetadata(f.envelope(),scope()) + f.controls.query=async(tag,result)=>{if(tag==="JOINT"){entered.resolve();await gate.promise}return result} + const pending=lease.inspect(f.envelope());void pending.catch(()=>{});await entered.promise + const count=f.ledger.length;Date.now=()=>wall-1000;gate.resolve();await assert.rejects(pending) + assert.equal(f.ledger.length,count);assert.deepEqual(f.destroyed,[701]) + }finally{Date.now=now;gate.resolve();await f.close()} +}) + +test("open/inspect reject caller getters/proxies without execution; original envelope/scope captured",async()=>{ + const f=await fixture();let called=0 + const trap=()=>{called++;throw Error("caller getter")} + try{ + for(const envelope of [Object.defineProperty({...f.envelope()},"payload",{get:trap}),new Proxy(f.envelope(),{getPrototypeOf:trap}),{...f.envelope(),toJSON:trap}])await assert.rejects(f.source.openPublicRegistryMetadata(envelope,scope())) + await assert.rejects(f.source.openPublicRegistryMetadata(f.envelope(),Object.defineProperty(scope(),"deadlineAt",{get:trap}))) + assert.equal(called,0);assert.equal(f.checkouts,0) + const gate=deferred(),input=scope(),envelope=f.envelope();f.controls.connectGate=gate.promise + const pending=f.source.openPublicRegistryMetadata(envelope,input);await Promise.race([f.connectEntered.promise,pending]) + envelope.signature="A".repeat(86);input.deadlineAt=0;gate.resolve() + const lease=await pending,sqlGate=deferred(),entered=deferred(),inspectEnvelope=f.envelope() + f.controls.query=async(tag,result)=>{if(tag==="JOINT"){entered.resolve();await sqlGate.promise}return result} + const inspecting=lease.inspect(inspectEnvelope);await entered.promise;inspectEnvelope.signature="A".repeat(86);sqlGate.resolve() + assert.equal((await inspecting).backendPid,701) + await assert.rejects(lease.inspect(Object.defineProperty({...f.envelope()},"signature",{get:trap}))) + assert.equal(called,0);assert.deepEqual(f.destroyed,[701]) + }finally{await f.close()} +}) diff --git a/backend/src/services/bandwidth-control-registry-frame.test.ts b/backend/src/services/bandwidth-control-registry-frame.test.ts new file mode 100644 index 00000000..88089221 --- /dev/null +++ b/backend/src/services/bandwidth-control-registry-frame.test.ts @@ -0,0 +1,85 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createHash, generateKeyPairSync, randomBytes, randomUUID, sign } from "node:crypto" +import { mkdtemp, mkdir, writeFile, rm, realpath } from "node:fs/promises" +import { resolve, join, dirname, basename } from "node:path" +import { tmpdir } from "node:os" +import { Pool } from "pg" +import { createConfiguredBandwidthControlSource, canonicalBandwidthControlRecord, bandwidthControlEndpointSha256, bandwidthControlSchemaSourceSha256, BandwidthControlSourceError, type BandwidthControlRegistryDispatchFence } from "./bandwidth-control-source.js" +import { canonicalBandwidthNodeBootstrap, bandwidthNodeRegistrySchemaSourceSha256, bandwidthPeerRegistrySchemaSourceSha256 } from "./bandwidth-node-bootstrap.js" +import { expected as coreExpected, rows as coreRows } from "../test/fixtures/bandwidth-control-identity.js" +import { fixture as v1Fixture } from "../test/fixtures/bandwidth-node-identity.js" +import { fixture as v2Fixture } from "../test/fixtures/bandwidth-peer-identity.js" + +const sha=(value:Buffer|string)=>createHash("sha256").update(value).digest("hex") +test("configured core registry frames authenticate fresh files and complete metadata before/after driver SQL (fake PostgreSQL only)",async t=>{ + const temporaryParent=await realpath(tmpdir()),ownedRoot=await mkdtemp(join(temporaryParent,"mm-bandwidth-registry-frame-")) + t.after(async()=>{assert.equal(dirname(ownedRoot),temporaryParent);assert(basename(ownedRoot).startsWith("mm-bandwidth-registry-frame-"));await rm(ownedRoot,{recursive:true,force:true})}) + const oldConnect=Pool.prototype.connect + try{ + for(const version of[1,2])for(const scenario of["positive","empty","foreign","cross-frame","skip-after","key-rotation","bootstrap-rotation","revoked-core","revoked-registry","actor-revoked","abort","overlap","malformed","source-pin-rotation","sql-after-files"]){ + const directory=resolve(ownedRoot,randomUUID());await mkdir(directory,{recursive:true}) + const {publicKey,privateKey}=generateKeyPairSync("ed25519"),eventKey=randomBytes(32),registry=version===1?v1Fixture():v2Fixture() + const databaseUrl="postgresql://bandwidth_runtime:synthetic-password@127.0.0.1:65432/owned_database",applicationDatabaseUrl="postgresql://restricted_application:synthetic-password@127.0.0.1:65432/application_database" + const record={format:"mmapp-bandwidth-control-source-v1" as const,installationId:registry.expected.installationId,controlGeneration:randomUUID(),databaseName:"owned_database",databaseOid:123,runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",endpointSha256:bandwidthControlEndpointSha256(databaseUrl),schemaSourceSha256:bandwidthControlSchemaSourceSha256(),catalogSha256:coreExpected.catalogSha256,eventKeySha256:sha(eventKey),attestationRef:"Synthetic inert candidate control source only, not production authority"} + const bootstrap={format:version===1?"mmapp-bandwidth-node-bootstrap-v1" as const:"mmapp-bandwidth-node-bootstrap-v2" as const,installationId:registry.expected.installationId,issuerKeySha256:registry.expected.issuerKeySha256,quiescenceProof:registry.expected.quiescenceProof,registrySchemaSourceSha256:version===1?bandwidthNodeRegistrySchemaSourceSha256():bandwidthPeerRegistrySchemaSourceSha256(),registryCatalogSha256:registry.expected.registryCatalogSha256,attestationRef:"Synthetic inert candidate registry statement only, not production enrollment"} + const signed=(format:string,text:string)=>{const data=Buffer.from(text);return{format,payload:data.toString("base64url"),signature:sign(null,data,privateKey).toString("base64url")}} + const signedBootstrap=(value=bootstrap)=>signed("mmapp-bandwidth-node-bootstrap-envelope-v1",canonicalBandwidthNodeBootstrap(value)) + const trustKeyFile=resolve(directory,"trust.pem"),eventKeyFile=resolve(directory,"event.key"),attestationFile=resolve(directory,"source.json") + await Promise.all([writeFile(trustKeyFile,publicKey.export({type:"spki",format:"pem"})),writeFile(eventKeyFile,eventKey),writeFile(attestationFile,JSON.stringify(signed("mmapp-bandwidth-control-envelope-v1",canonicalBandwidthControlRecord(record))))]) + Object.assign(coreExpected,{installationId:record.installationId,controlGeneration:record.controlGeneration,sourceRecordSha256:sha(canonicalBandwidthControlRecord(record)),eventKeySha256:record.eventKeySha256}) + let destroyed=0,checkouts=0,jointCalls=0,targetCalls=0,phases=0,latestEnvelope=signedBootstrap(),previousVerified:unknown + const order:string[]=[],controller=new AbortController(),window={deadlineAt:Date.now()+10000,signal:controller.signal} + Pool.prototype.connect=(async()=>{ + checkouts++ + return {async query(config:{text:string;values?:unknown[]}){ + const text=config.text + if(text.startsWith("/* bandwidth-control:registry-identity */")){ + jointCalls++;order.push("joint") + assert.deepEqual(config.values,[record.databaseName,record.runtimeRole,record.ownerRole,"restricted_application"]) + assert(text.includes("core_identity AS MATERIALIZED"));assert(text.includes("registry_identity AS MATERIALIZED")) + const both=[...coreRows().map(row=>({family:"core",...row})),...registry.rows.map(row=>({family:"registry",...row}))] + if(scenario==="malformed")both[0]={...both[0],extra:"untrusted"} as never + if(scenario==="revoked-core"){const row=both.find(row=>row.family==="core"&&row.kind==="source")!;row.raw_json=JSON.stringify({...JSON.parse(row.raw_json!),status:"quarantined"})} + if(scenario==="revoked-registry"){const row=both.find(row=>row.family==="registry"&&row.kind==="authority")!;row.raw_json=JSON.stringify({...JSON.parse(row.raw_json!),status:"quarantined"})} + if(scenario==="abort")controller.abort() + return{rows:both,command:"SELECT"} + } + if(text.startsWith("/* bandwidth-control:identity */")){order.push("initial-core");return{rows:coreRows(),command:"SELECT"}} + if(text==="SELECT ownership_check"){order.push("ownership");return{rows:[],command:"SELECT"}} + assert.equal(text,"SELECT candidate_target");targetCalls++;order.push("target");return{rows:[{value:"original"}],command:"SELECT"} + },release(unsafe:boolean){assert.equal(unsafe,true);destroyed++}} + }) as never + const source=createConfiguredBandwidthControlSource({databaseUrl,applicationDatabaseUrl,trustKeyFile,eventKeyFile,attestationFile,trustKeySha256:sha(publicKey.export({type:"spki",format:"der"}))}) + let lease:Awaited>|undefined + const guard=async(fence:BandwidthControlRegistryDispatchFence)=>{ + phases++;order.push("files-before") + if(scenario==="empty")return + const verified=await fence.verifyNodeBootstrap(latestEnvelope) + if(scenario==="foreign"){await fence.assertRegistryIdentity(structuredClone(verified));return} + if(scenario==="cross-frame"&&phases===2){await fence.assertRegistryIdentity(previousVerified as never);return} + previousVerified=verified + if(scenario==="overlap"){const first=fence.assertRegistryIdentity(verified);await Promise.all([first,fence.assertRegistryIdentity(verified)]);return} + await fence.assertRegistryIdentity(verified) + if(scenario==="skip-after")return + await fence.query("SELECT ownership_check") + if(scenario==="key-rotation")await writeFile(eventKeyFile,randomBytes(32)) + if(scenario==="bootstrap-rotation")latestEnvelope=signedBootstrap({...bootstrap,quiescenceProof:"f".repeat(64)}) + if(scenario==="source-pin-rotation")await writeFile(attestationFile,JSON.stringify(signed("mmapp-bandwidth-control-envelope-v1",canonicalBandwidthControlRecord({...record,controlGeneration:randomUUID()})))) + await fence.verifyNodeBootstrap(latestEnvelope);order.push("files-after") + if(scenario==="sql-after-files")await fence.query("SELECT ownership_check") + if(scenario==="actor-revoked")throw Error("Original actor revoked") + order.push("actor-last") + } + try{ + lease=await source.connectRegistry(window,guard) + const work=lease.query("SELECT candidate_target") + if(scenario==="positive"){ + assert.deepEqual((await work).rows,[{value:"original"}]);assert.equal(checkouts,1);assert.equal(jointCalls,2);assert.equal(targetCalls,1) + assert.deepEqual(order,["initial-core","files-before","joint","ownership","files-after","actor-last","target","files-before","joint","ownership","files-after","actor-last"]) + }else await assert.rejects(work,(error:unknown)=>error instanceof BandwidthControlSourceError,`${version}:${scenario}`) + if(scenario!=="positive"&&scenario!=="cross-frame")assert.equal(targetCalls,0,`${version}:${scenario}`) + }finally{lease?.release(true);await source.close();assert.equal(destroyed,1);assert.equal(dirname(directory),ownedRoot);await rm(directory,{recursive:true,force:true})} + } + }finally{Pool.prototype.connect=oldConnect} +}) diff --git a/backend/src/services/bandwidth-control-registry-identity.test.ts b/backend/src/services/bandwidth-control-registry-identity.test.ts new file mode 100644 index 00000000..c67cca60 --- /dev/null +++ b/backend/src/services/bandwidth-control-registry-identity.test.ts @@ -0,0 +1,35 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { assertBandwidthControlRegistryIdentity } from "./bandwidth-control-registry-identity.js" +import { expected as coreExpected, rows as coreRows } from "../test/fixtures/bandwidth-control-identity.js" +import { fixture as v1Fixture } from "../test/fixtures/bandwidth-node-identity.js" +import { fixture as v2Fixture } from "../test/fixtures/bandwidth-peer-identity.js" + +test("joint snapshot preserves all old validators, exact family bounds and captured original expectations",async()=>{ + for(const version of[1,2]){ + const registry=version===1?v1Fixture():v2Fixture(),format=version===1?"mmapp-bandwidth-node-bootstrap-v1" as const:"mmapp-bandwidth-node-bootstrap-v2" as const + coreExpected.installationId=registry.expected.installationId + const core={...coreExpected},expected={...registry.expected} + const rows=()=>[...coreRows().map(row=>({family:"core",...row})),...registry.rows.map(row=>({family:"registry",...row}))] + const verify=(data:Record[])=>assertBandwidthControlRegistryIdentity({query:async()=>({rows:data})},core,expected,format,()=>{}) + await verify(rows().reverse()) + for(const mutate of[ + (data:Record[])=>{data[0].family="other"}, + (data:Record[])=>{delete data[0].family}, + (data:Record[])=>{data[0].raw_json=null}, + (data:Record[])=>{data[0].row_order="2"}, + (data:Record[])=>{data.push({...data[0]})}, + (data:Record[])=>{data[0].extra=true}, + (data:Record[])=>{Object.defineProperty(data[0],"family",{get(){throw Error("accessor invoked")},enumerable:true})}, + (data:Record[])=>{Object.defineProperty(data[0],Symbol("foreign"),{value:true})}, + (data:Record[])=>{data.splice(0,data.length,...data.filter(row=>row.family!=="registry"))}, + ]){const data:Record[]=rows();mutate(data);await assert.rejects(verify(data))} + const data=rows(),originalCore={...core},originalRegistry={...expected} + await assertBandwidthControlRegistryIdentity({query:async()=>{core.controlGeneration="changed";expected.issuerKeySha256="f".repeat(64);return{rows:data}}},core,expected,format,()=>{}) + Object.assign(core,originalCore);Object.assign(expected,originalRegistry) + let sqlCalls=0 + const hostile=Object.defineProperty({...core},"ownerRole",{get(){throw Error("getter invoked")},enumerable:true}) + await assert.rejects(assertBandwidthControlRegistryIdentity({query:async()=>{sqlCalls++;return{rows:data}}},hostile,expected,format,()=>{})) + assert.equal(sqlCalls,0) + } +}) diff --git a/backend/src/services/bandwidth-control-registry-identity.ts b/backend/src/services/bandwidth-control-registry-identity.ts new file mode 100644 index 00000000..2bd4421a --- /dev/null +++ b/backend/src/services/bandwidth-control-registry-identity.ts @@ -0,0 +1,47 @@ +/** One fixed MVCC snapshot, using both original component SQL/validators. + * No cache, supplied SQL, registration, authority or default source. */ +import { BANDWIDTH_CONTROL_IDENTITY_SQL, validateBandwidthControlIdentityRows, type BandwidthControlIdentityExpected } from "./bandwidth-control-identity.js" +import { BANDWIDTH_NODE_IDENTITY_SQL, validateBandwidthNodeIdentityRows, type BandwidthNodeIdentityExpected } from "./bandwidth-node-identity.js" +import { BANDWIDTH_PEER_IDENTITY_SQL, validateBandwidthPeerIdentityRows } from "./bandwidth-peer-identity.js" + +type Sql = { query(text:string,values?:unknown[]):Promise<{rows:Record[]}> } +type Format = "mmapp-bandwidth-node-bootstrap-v1"|"mmapp-bandwidth-node-bootstrap-v2" +const joint = (registry:string,limit:number) => `/* bandwidth-control:registry-identity */ WITH + core_identity AS MATERIALIZED (${BANDWIDTH_CONTROL_IDENTITY_SQL}), + registry_identity AS MATERIALIZED (${registry}) + SELECT 'core'::pg_catalog.text AS family,kind,row_order,raw_json FROM core_identity + UNION ALL SELECT 'registry'::pg_catalog.text AS family,kind,row_order,raw_json FROM registry_identity + LIMIT ${limit}` +export const BANDWIDTH_CONTROL_NODE_IDENTITY_SQL=joint(BANDWIDTH_NODE_IDENTITY_SQL,467) +export const BANDWIDTH_CONTROL_PEER_IDENTITY_SQL=joint(BANDWIDTH_PEER_IDENTITY_SQL,704) +const fail=():never=>{throw new Error("Bandwidth joint control/registry identity refused")} +const coreFields=["databaseName","runtimeRole","ownerRole","applicationRole","databaseOid","installationId","controlGeneration","sourceRecordSha256","eventKeySha256","catalogSha256"] as const +const registryFields=["databaseName","runtimeRole","ownerRole","installationId","issuerKeySha256","quiescenceProof","registryCatalogSha256"] as const +function capture(value:T,fields:readonly string[]):Readonly{ + if(!value||typeof value!=="object"||Object.getPrototypeOf(value)!==Object.prototype||Reflect.ownKeys(value).length!==fields.length)fail() + const descriptors=Object.getOwnPropertyDescriptors(value) + if(Object.keys(descriptors).sort().join(",")!==[...fields].sort().join(",")||Object.values(descriptors).some(field=>!("value"in field)||!field.enumerable||!["string","number"].includes(typeof field.value)))fail() + return Object.freeze(Object.fromEntries(fields.map(name=>[name,descriptors[name].value]))) as Readonly +} +export async function assertBandwidthControlRegistryIdentity(sql:Sql,core:BandwidthControlIdentityExpected,registry:BandwidthNodeIdentityExpected,format:Format,live:()=>void):Promise{ + const originalCore=capture(core,coreFields),originalRegistry=capture(registry,registryFields) + if(format!=="mmapp-bandwidth-node-bootstrap-v1"&&format!=="mmapp-bandwidth-node-bootstrap-v2")fail() + for(const field of["databaseName","runtimeRole","ownerRole","installationId"] as const)if(originalCore[field]!==originalRegistry[field])fail() + const v2=format==="mmapp-bandwidth-node-bootstrap-v2" + live() + let result:{rows:Record[]} + try{result=await sql.query(v2?BANDWIDTH_CONTROL_PEER_IDENTITY_SQL:BANDWIDTH_CONTROL_NODE_IDENTITY_SQL,[originalCore.databaseName,originalCore.runtimeRole,originalCore.ownerRole,originalCore.applicationRole])}catch{live();return fail()} + live() + if(!result||!Array.isArray(result.rows)||result.rows.length>(v2?703:466))fail() + const coreRows:Record[]=[],registryRows:Record[]=[] + for(const row of result.rows){ + if(!row||typeof row!=="object"||Object.getPrototypeOf(row)!==Object.prototype||Reflect.ownKeys(row).length!==4||Object.keys(row).sort().join(",")!=="family,kind,raw_json,row_order"||Object.values(Object.getOwnPropertyDescriptors(row)).some(field=>!("value"in field)||!field.enumerable))fail() + if(row.family!=="core"&&row.family!=="registry")fail() + const target=row.family==="core"?coreRows:registryRows + target.push({kind:row.kind,row_order:row.row_order,raw_json:row.raw_json}) + } + validateBandwidthControlIdentityRows(coreRows,originalCore) + if(v2)validateBandwidthPeerIdentityRows(registryRows,originalRegistry) + else validateBandwidthNodeIdentityRows(registryRows,originalRegistry) + live() +} diff --git a/backend/src/services/bandwidth-control-role.test.ts b/backend/src/services/bandwidth-control-role.test.ts new file mode 100644 index 00000000..225eac8f --- /dev/null +++ b/backend/src/services/bandwidth-control-role.test.ts @@ -0,0 +1,94 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { assertBandwidthControlRuntimeRole, BandwidthControlRuntimeRoleRefused, type BandwidthControlRoleExpected, type BandwidthControlRoleSql } from "./bandwidth-control-role.js" + +const expected: BandwidthControlRoleExpected = { databaseName: "owned_database", runtimeRole: "bandwidth_runtime", ownerRole: "control_owner" } +function observation() { + return { + databaseName: expected.databaseName, sessionRole: expected.runtimeRole, currentRole: expected.runtimeRole, + runtimeRoleExists: true, ownerRoleExists: true, runtimeFlagsSafe: true, + ownerMembership: false, dangerousMembership: false, + schemaExists: true, schemaOwner: expected.ownerRole, schemaUsage: true, schemaCreate: false, + publicSchemaUsage: false, publicSchemaCreate: false, + tables: ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events", "bandwidth_source_identity"].map(name => ({ name, exists: true, ownerRole: expected.ownerRole, ownedByRuntime: false, privilegesExact: true })), + } +} +const deferred = () => { let resolve!: (value: T) => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const refused = (error: unknown) => error instanceof BandwidthControlRuntimeRoleRefused && error.code === "BANDWIDTH_CONTROL_RUNTIME_ROLE_REFUSED" +async function check(value: unknown) { + return assertBandwidthControlRuntimeRole({ query: async () => ({ rows: [{ observation: value }] }) }, expected, () => {}) +} + +test("minimal separate runtime role succeeds with one catalog observation and live fences on both sides", async () => { + const events: string[] = [], calls: Array<{ text: string; values?: unknown[] }> = [] + const sql: BandwidthControlRoleSql = { query: async (text, values) => { events.push("SQL"); calls.push({ text, values }); return { rows: [{ observation: observation() }] } } } + assert.equal(await assertBandwidthControlRuntimeRole(sql, expected, () => { events.push("live") }), undefined) + assert.deepEqual(events, ["live", "SQL", "live"]); assert.equal(calls.length, 1) + assert.deepEqual(calls[0].values, [expected.databaseName, expected.runtimeRole, expected.ownerRole]) + const text = calls[0].text + for (const catalog of ["pg_roles", "pg_namespace", "pg_class", "has_schema_privilege", "has_table_privilege", "has_any_column_privilege", "aclexplode", "acldefault", "pg_has_role", "current_database"]) assert.ok(text.includes("pg_catalog." + catalog), catalog) + assert.match(text, /pg_has_role\(rt\.oid,r\.oid,'MEMBER'\)/) + for (const privilege of ["TRUNCATE", "REFERENCES", "TRIGGER", " WITH GRANT OPTION"]) assert.ok(text.includes(privilege)) + for (const role of ["pg_read_server_files", "pg_write_server_files", "pg_execute_server_program"]) assert.ok(text.includes(role)) + assert.ok(text.includes("n.nspname='mmapp_control'")); assert.ok(text.includes("'bandwidth_source_identity',ARRAY['SELECT']")) + assert.doesNotMatch(text, /\b(?:INSERT INTO|DELETE FROM|ALTER TABLE|CREATE SCHEMA|SET ROLE)\b/) + assert.ok(!text.includes(expected.runtimeRole)); assert.ok(!text.includes(expected.ownerRole)); assert.ok(!text.includes(expected.databaseName)) +}) + +test("session/current role, database, role flags and direct or transitive memberships must all match", async () => { + const failures: Record = { databaseName: "foreign_database", sessionRole: "app_restorer", currentRole: "control_owner", runtimeRoleExists: false, ownerRoleExists: false, runtimeFlagsSafe: false, ownerMembership: true, dangerousMembership: true } + for (const [field, value] of Object.entries(failures)) await assert.rejects(check({ ...observation(), [field]: value }), refused, field) +}) + +test("missing control schema, incorrect ownership and PUBLIC or runtime CREATE/USAGE escalation refuse", async () => { + const failures: Record = { schemaExists: false, schemaOwner: "app_restorer", schemaUsage: false, schemaCreate: true, publicSchemaUsage: true, publicSchemaCreate: true } + for (const [field, value] of Object.entries(failures)) await assert.rejects(check({ ...observation(), [field]: value }), refused, field) +}) + +test("all four relations require the fixed owner and exact least privileges, including read-only source identity", async () => { + for (let index = 0; index < 4; index++) { + for (const change of [{ exists: false }, { ownerRole: expected.runtimeRole }, { ownedByRuntime: true }, { privilegesExact: false }]) { + const value = observation(); Object.assign(value.tables[index], change) + await assert.rejects(check(value), refused) + } + } + const missing = observation(); missing.tables.pop(); await assert.rejects(check(missing), refused) + const duplicate = observation(); duplicate.tables[3] = { ...duplicate.tables[0] }; await assert.rejects(check(duplicate), refused) + const unknown = observation(); unknown.tables[3].name = "public_shadow"; await assert.rejects(check(unknown), refused) +}) + +test("malformed or truthy metadata never substitutes for exact boolean observations", async () => { + for (const value of [null, {}, { ...observation(), runtimeFlagsSafe: "true" }, { ...observation(), schemaCreate: null }, { ...observation(), extraReady: true }, { ...observation(), tables: null }]) await assert.rejects(check(value), refused) + const tableExtra = observation(); Object.assign(tableExtra.tables[0], { bypass: false }); await assert.rejects(check(tableExtra), refused) + for (const rows of [[], [{ observation: observation() }, { observation: observation() }], [{ observation: observation(), extra: true }]]) await assert.rejects(assertBandwidthControlRuntimeRole({ query: async () => ({ rows }) }, expected, () => {}), refused) +}) + +test("expected names stay bounded parameters and are captured before async SQL", async () => { + let calls = 0 + const invalid = ["", " ", "x".repeat(64), "x\u0000y", "x\ny", "я".repeat(32)] + for (const name of invalid) await assert.rejects(assertBandwidthControlRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, { ...expected, runtimeRole: name }, () => {}), refused) + await assert.rejects(assertBandwidthControlRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, { ...expected, ownerRole: expected.runtimeRole }, () => {}), refused) + assert.equal(calls, 0) + const pending = deferred<{ rows: Record[] }>(), mutable = { ...expected } + const result = assertBandwidthControlRuntimeRole({ query: async () => pending.promise }, mutable, () => {}) + mutable.ownerRole = "changed"; pending.resolve({ rows: [{ observation: observation() }] }); await result + const quoted = { databaseName: "db'quoted", runtimeRole: "runtime';select", ownerRole: 'owner"quoted' } + await assertBandwidthControlRuntimeRole({ query: async (text, values) => { assert.deepEqual(values, [quoted.databaseName, quoted.runtimeRole, quoted.ownerRole]); assert.ok(!text.includes(quoted.runtimeRole)); return { rows: [{ observation: { ...observation(), databaseName: quoted.databaseName, sessionRole: quoted.runtimeRole, currentRole: quoted.runtimeRole, schemaOwner: quoted.ownerRole, tables: observation().tables.map(row => ({ ...row, ownerRole: quoted.ownerRole })) } }] } } }, quoted, () => {}) +}) + +test("expired owners cannot start SQL or accept a late successful observation", async () => { + let calls = 0 + const expired = new Error("closed live owner") + await assert.rejects(assertBandwidthControlRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, expected, () => { throw expired }), error => error === expired) + assert.equal(calls, 0) + const pending = deferred<{ rows: Record[] }>() + let active = true + const result = assertBandwidthControlRuntimeRole({ query: async () => pending.promise }, expected, () => { if (!active) throw expired }) + active = false; const rejected = assert.rejects(result, error => error === expired); pending.resolve({ rows: [{ observation: observation() }] }); await rejected +}) + +test("SQL failure still performs the post-await live fence and returns no provider details", async () => { + let checks = 0 + await assert.rejects(assertBandwidthControlRuntimeRole({ query: async () => { throw new Error("SECRET-provider-details") } }, expected, () => { checks++ }), error => refused(error) && !String(error).includes("SECRET")) + assert.equal(checks, 2) +}) diff --git a/backend/src/services/bandwidth-control-role.ts b/backend/src/services/bandwidth-control-role.ts new file mode 100644 index 00000000..541c02e5 --- /dev/null +++ b/backend/src/services/bandwidth-control-role.ts @@ -0,0 +1,119 @@ +/** Catalog observation only. This is neither a control generation issuer nor a + * readiness/native capability. The caller owns its bounded SQL facade and must + * run this assertion at the relevant live authority boundary. No default I/O. */ +export interface BandwidthControlRoleSql { + query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> +} +export interface BandwidthControlRoleExpected { databaseName: string; runtimeRole: string; ownerRole: string } +export class BandwidthControlRuntimeRoleRefused extends Error { + readonly code = "BANDWIDTH_CONTROL_RUNTIME_ROLE_REFUSED" + constructor() { super("Bandwidth control runtime role refused") } +} +const TABLES = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events", "bandwidth_source_identity"] as const +/** Names are parameters; catalog paths, namespace, relations and privilege + * vocabulary are fixed. ACL-provided privilege names include newer PostgreSQL + * privileges without sending unsupported names to an older server. */ +export const BANDWIDTH_CONTROL_ROLE_SQL = `WITH + runtime AS (SELECT r.* FROM pg_catalog.pg_roles r WHERE r.rolname=$2 AND pg_catalog.current_database()::text=$1::text), + expected_owner AS (SELECT r.oid FROM pg_catalog.pg_roles r WHERE r.rolname=$3), + control_schema AS (SELECT n.* FROM pg_catalog.pg_namespace n WHERE n.nspname='mmapp_control'), + required(name,allowed) AS (VALUES + ('bandwidth_attempts',ARRAY['SELECT','INSERT','UPDATE']::text[]), + ('bandwidth_node_occupancy',ARRAY['SELECT','INSERT','DELETE']::text[]), + ('bandwidth_attempt_events',ARRAY['SELECT','INSERT']::text[]), + ('bandwidth_source_identity',ARRAY['SELECT']::text[])), + relations AS ( + SELECT e.name,e.allowed,c.oid,c.relowner,c.relacl,c.relkind + FROM required e LEFT JOIN control_schema n ON true + LEFT JOIN pg_catalog.pg_class c ON c.relnamespace=n.oid AND c.relname=e.name), + member_roles AS ( + SELECT r.* FROM pg_catalog.pg_roles r CROSS JOIN runtime rt + WHERE r.oid=rt.oid OR pg_catalog.pg_has_role(rt.oid,r.oid,'MEMBER')), + permissions AS ( + SELECT r.name,m.oid AS role_oid, + COALESCE(r.oid IS NOT NULL AND r.relkind IN ('r','p') AND NOT EXISTS ( + SELECT 1 FROM ( + SELECT pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','DELETE','TRUNCATE','REFERENCES','TRIGGER']::text[]) AS privilege + UNION SELECT a.privilege_type FROM pg_catalog.aclexplode(COALESCE(r.relacl,pg_catalog.acldefault('r',r.relowner))) a + ) p WHERE pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege) IS DISTINCT FROM (p.privilege=ANY(r.allowed)) + ),false) AS exact_table, + COALESCE(EXISTS ( + SELECT 1 FROM ( + SELECT pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','DELETE','TRUNCATE','REFERENCES','TRIGGER']::text[]) AS privilege + UNION SELECT a.privilege_type FROM pg_catalog.aclexplode(COALESCE(r.relacl,pg_catalog.acldefault('r',r.relowner))) a + ) p WHERE (pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege) AND NOT p.privilege=ANY(r.allowed)) + OR pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege || ' WITH GRANT OPTION') + ) OR EXISTS ( + SELECT 1 FROM pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','REFERENCES']::text[]) p(privilege) + WHERE (pg_catalog.has_any_column_privilege(m.oid,r.oid,p.privilege) AND NOT p.privilege=ANY(r.allowed)) + OR pg_catalog.has_any_column_privilege(m.oid,r.oid,p.privilege || ' WITH GRANT OPTION') + ),true) AS forbidden + FROM relations r CROSS JOIN member_roles m) + SELECT pg_catalog.jsonb_build_object( + 'databaseName',pg_catalog.current_database(), + 'sessionRole',SESSION_USER::text,'currentRole',CURRENT_USER::text, + 'runtimeRoleExists',EXISTS(SELECT 1 FROM runtime), + 'ownerRoleExists',EXISTS(SELECT 1 FROM expected_owner), + 'runtimeFlagsSafe',COALESCE((SELECT NOT (rolsuper OR rolcreaterole OR rolcreatedb OR rolreplication OR rolbypassrls) FROM runtime),false), + 'ownerMembership',COALESCE((SELECT pg_catalog.pg_has_role(rt.oid,o.oid,'MEMBER') FROM runtime rt CROSS JOIN expected_owner o),true), + 'dangerousMembership',EXISTS ( + SELECT 1 FROM member_roles m WHERE m.rolsuper OR m.rolcreaterole OR m.rolcreatedb OR m.rolreplication OR m.rolbypassrls + OR m.rolname IN ('pg_read_server_files','pg_write_server_files','pg_execute_server_program','pg_read_all_data','pg_write_all_data','pg_maintain','pg_signal_backend','pg_checkpoint','pg_create_subscription','pg_database_owner','pg_use_reserved_connections') + OR EXISTS(SELECT 1 FROM control_schema n WHERE n.nspowner=m.oid OR pg_catalog.has_schema_privilege(m.oid,n.oid,'CREATE')) + OR EXISTS(SELECT 1 FROM relations r WHERE r.relowner=m.oid) + OR EXISTS(SELECT 1 FROM permissions p WHERE p.role_oid=m.oid AND p.forbidden)), + 'schemaExists',EXISTS(SELECT 1 FROM control_schema), + 'schemaOwner',(SELECT o.rolname::text FROM control_schema n JOIN pg_catalog.pg_roles o ON o.oid=n.nspowner), + 'schemaUsage',COALESCE((SELECT pg_catalog.has_schema_privilege(rt.oid,n.oid,'USAGE') FROM runtime rt CROSS JOIN control_schema n),false), + 'schemaCreate',COALESCE((SELECT pg_catalog.has_schema_privilege(rt.oid,n.oid,'CREATE') FROM runtime rt CROSS JOIN control_schema n),true), + 'publicSchemaUsage',EXISTS(SELECT 1 FROM control_schema n CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(n.nspacl,pg_catalog.acldefault('n',n.nspowner))) a WHERE a.grantee=0 AND a.privilege_type='USAGE'), + 'publicSchemaCreate',EXISTS(SELECT 1 FROM control_schema n CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(n.nspacl,pg_catalog.acldefault('n',n.nspowner))) a WHERE a.grantee=0 AND a.privilege_type='CREATE'), + 'tables',(SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object( + 'name',r.name,'exists',r.oid IS NOT NULL AND r.relkind IN ('r','p'), + 'ownerRole',o.rolname::text,'ownedByRuntime',COALESCE(r.relowner=rt.oid,true), + 'privilegesExact',COALESCE(p.exact_table AND NOT p.forbidden,false)) ORDER BY r.name COLLATE "C") + FROM relations r CROSS JOIN runtime rt LEFT JOIN pg_catalog.pg_roles o ON o.oid=r.relowner + LEFT JOIN permissions p ON p.name=r.name AND p.role_oid=rt.oid) + ) AS observation` + +function refuse(): never { throw new BandwidthControlRuntimeRoleRefused() } +const plain = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +const label = (value: unknown): value is string => typeof value === "string" && value.trim() === value && value.length > 0 && Buffer.byteLength(value) <= 63 && !/[\x00-\x1f\x7f]/.test(value) +const exactKeys = (value: Record, fields: string[]) => Object.keys(value).sort().join(",") === [...fields].sort().join(",") + +export function captureBandwidthControlRoleExpected(expected: BandwidthControlRoleExpected): BandwidthControlRoleExpected { + if (!plain(expected) || !exactKeys(expected, ["databaseName", "runtimeRole", "ownerRole"])) refuse() + const captured = { databaseName: expected.databaseName, runtimeRole: expected.runtimeRole, ownerRole: expected.ownerRole } + if (!Object.values(captured).every(label) || captured.runtimeRole === captured.ownerRole) refuse() + return Object.freeze(captured) +} + +export async function assertBandwidthControlRuntimeRole(sql: BandwidthControlRoleSql, expected: BandwidthControlRoleExpected, live: () => void): Promise { + const captured = captureBandwidthControlRoleExpected(expected) + live() + let result: { rows: Record[] } + try { result = await sql.query(BANDWIDTH_CONTROL_ROLE_SQL, [captured.databaseName, captured.runtimeRole, captured.ownerRole]) } + catch { live(); refuse() } + live() + validateBandwidthControlRuntimeRoleRows(result?.rows, captured) +} + +/** Shared pure decoder for standalone and same-statement identity snapshots. */ +export function validateBandwidthControlRuntimeRoleRows(rows: unknown, expected: BandwidthControlRoleExpected): void { + const captured = captureBandwidthControlRoleExpected(expected) + if (!Array.isArray(rows) || rows.length !== 1 || !plain(rows[0]) || !exactKeys(rows[0], ["observation"])) refuse() + const value = rows[0].observation + if (!plain(value) || !exactKeys(value, ["databaseName","sessionRole","currentRole","runtimeRoleExists","ownerRoleExists","runtimeFlagsSafe","ownerMembership","dangerousMembership","schemaExists","schemaOwner","schemaUsage","schemaCreate","publicSchemaUsage","publicSchemaCreate","tables"])) refuse() + if (value.databaseName !== captured.databaseName || value.sessionRole !== captured.runtimeRole || value.currentRole !== captured.runtimeRole + || value.runtimeRoleExists !== true || value.ownerRoleExists !== true || value.runtimeFlagsSafe !== true + || value.ownerMembership !== false || value.dangerousMembership !== false || value.schemaExists !== true || value.schemaOwner !== captured.ownerRole + || value.schemaUsage !== true || value.schemaCreate !== false || value.publicSchemaUsage !== false || value.publicSchemaCreate !== false + || !Array.isArray(value.tables) || value.tables.length !== TABLES.length) refuse() + const seen = new Set() + for (const row of value.tables as unknown[]) { + if (!plain(row) || !exactKeys(row, ["name","exists","ownerRole","ownedByRuntime","privilegesExact"]) + || typeof row.name !== "string" || !TABLES.includes(row.name as typeof TABLES[number]) || seen.has(row.name) + || row.exists !== true || row.ownerRole !== captured.ownerRole || row.ownedByRuntime !== false || row.privilegesExact !== true) refuse() + seen.add(row.name as string) + } +} diff --git a/backend/src/services/bandwidth-control-source-schema.ts b/backend/src/services/bandwidth-control-source-schema.ts new file mode 100644 index 00000000..f60246f1 --- /dev/null +++ b/backend/src/services/bandwidth-control-source-schema.ts @@ -0,0 +1,37 @@ +/** External provisioner only. No namespace/key/generation is created on startup + * or native dispatch. No FK or type depends on application public objects. */ +export const BANDWIDTH_CONTROL_SOURCE_DDL = ` +CREATE TABLE mmapp_control.bandwidth_source_identity ( + id integer PRIMARY KEY CHECK(id=1), + installation_id uuid NOT NULL, + control_generation uuid NOT NULL, + attestation_sha256 text NOT NULL CHECK(attestation_sha256 ~ '^[a-f0-9]{64}$'), + event_key_sha256 text NOT NULL CHECK(event_key_sha256 ~ '^[a-f0-9]{64}$'), + status text NOT NULL DEFAULT 'active' CHECK(status IN ('active','quarantined')), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE FUNCTION mmapp_control.bandwidth_source_identity_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +BEGIN + IF TG_OP IN ('DELETE','TRUNCATE') THEN + RAISE EXCEPTION 'bandwidth source identity cannot be removed' USING ERRCODE='23514'; + END IF; + IF TG_OP='INSERT' THEN + IF NEW.status <> 'active' THEN RAISE EXCEPTION 'new source must be explicitly active' USING ERRCODE='23514'; END IF; + RETURN NEW; + END IF; + IF ROW(NEW.id,NEW.installation_id,NEW.control_generation,NEW.attestation_sha256,NEW.event_key_sha256,NEW.created_at) + IS DISTINCT FROM ROW(OLD.id,OLD.installation_id,OLD.control_generation,OLD.attestation_sha256,OLD.event_key_sha256,OLD.created_at) + OR OLD.status <> 'active' OR NEW.status <> 'quarantined' THEN + RAISE EXCEPTION 'source identity is immutable; only quarantine is allowed' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE TRIGGER bandwidth_source_identity_guard BEFORE INSERT OR UPDATE OR DELETE + ON mmapp_control.bandwidth_source_identity FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_source_identity_guard(); +CREATE TRIGGER bandwidth_source_identity_no_truncate BEFORE TRUNCATE + ON mmapp_control.bandwidth_source_identity FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_source_identity_guard(); +ALTER TABLE mmapp_control.bandwidth_source_identity ENABLE ALWAYS TRIGGER bandwidth_source_identity_guard; +ALTER TABLE mmapp_control.bandwidth_source_identity ENABLE ALWAYS TRIGGER bandwidth_source_identity_no_truncate; +` diff --git a/backend/src/services/bandwidth-control-source.sql.test.ts b/backend/src/services/bandwidth-control-source.sql.test.ts new file mode 100644 index 00000000..05aeeb8c --- /dev/null +++ b/backend/src/services/bandwidth-control-source.sql.test.ts @@ -0,0 +1,390 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomBytes, randomUUID, sign } from "node:crypto" +import { execFile } from "node:child_process" +import { createServer, type Socket } from "node:net" +import { open, readFile, stat, unlink, writeFile } from "node:fs/promises" +import { resolve } from "node:path" +import { promisify } from "node:util" +import type { Pool } from "pg" +import type { BandwidthBinding, BandwidthScope } from "./bandwidth-admission.js" +import type { BandwidthControlRecord } from "./bandwidth-control-source.js" +import { assertOwnedBandwidthSql, canonical, completionProof, fixtureBinding, fixtureMac, prepareOwnedBandwidthState, scope } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +await prepareOwnedBandwidthState() +const { Pool: PgPool } = await import("pg") +const { pool: admin, closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +const { BANDWIDTH_ADMISSION_PROPOSAL_DDL } = await import("./bandwidth-admission-schema.js") +const { BANDWIDTH_CONTROL_SOURCE_DDL, canonicalBandwidthControlRecord, bandwidthControlEndpointSha256, bandwidthControlSchemaSourceSha256, observeBandwidthControlCatalog, createConfiguredBandwidthControlSource } = await import("./bandwidth-control-source.js") +const { createBandwidthAdmissionSqlJournal } = await import("./bandwidth-admission-storage.js") +const { assertBandwidthControlRuntimeRole } = await import("./bandwidth-control-role.js") +const { sealEmptyBandwidthControlSource } = await import("./bandwidth-control-provision.js") +const cases: string[] = [] +const suffix = randomUUID().replaceAll("-", "") +const ownerRole = "bw_owner_" + suffix, runtimeRole = "bw_runtime_" + suffix, restorerRole = "bw_restore_" + suffix +const databaseName = "bw_control_source_" + suffix +assert(/^[a-z_]+[a-f0-9]{32}$/.test(databaseName)) +for (const name of [ownerRole, runtimeRole, restorerRole]) assert(/^bw_(?:owner|runtime|restore)_[a-f0-9]{32}$/.test(name)) +const quote = (name: string) => { assert(/^[a-z_]+[a-f0-9]{32}$/.test(name)); return '"' + name + '"' } +const passwords = new Map([ownerRole, runtimeRole, restorerRole].map(role => [role, randomBytes(32).toString("hex")])) +const base = new URL(process.env.DATABASE_URL!) +const roleUrl = (role: string): string => { + const value = new URL(base); value.pathname = "/" + databaseName; value.username = role; value.password = passwords.get(role)! + return value.toString() +} +const connections: Pool[] = [], createdRoles: string[] = [] +const sourceClosers: Array<() => Promise> = [] +let databaseCreated = false +let operatorIssuerAccepted=false +const connectRole = (role: string) => { const value = new PgPool({ connectionString: roleUrl(role), max: 4 }); connections.push(value); return value } +const expected = { databaseName, runtimeRole, ownerRole } +const live = () => { /* Only a fixed synchronous fixture lifetime fence; not a production issuer. */ } +async function metadata(connection: Pool) { + return (await connection.query(`SELECT t.tgname,t.tgenabled::text AS enabled FROM pg_catalog.pg_trigger t + JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + WHERE n.nspname='mmapp_control' AND c.relname IN ('bandwidth_attempts','bandwidth_node_occupancy','bandwidth_attempt_events','bandwidth_source_identity') AND NOT t.tgisinternal ORDER BY t.tgname`)).rows +} +async function rows(connection: Pool, includeSource = true) { + const result: Record = {} + for (const name of ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events", ...(includeSource ? ["bandwidth_source_identity"] : [])]) + result[name] = (await connection.query(`SELECT to_jsonb(t) AS row FROM mmapp_control.${name} t ORDER BY to_jsonb(t)::text`)).rows + return canonical(result) +} +try { + for (const role of [ownerRole, runtimeRole, restorerRole]) { + const password = passwords.get(role)!; assert(/^[a-f0-9]{64}$/.test(password)) + await admin.query(`CREATE ROLE ${quote(role)} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS NOINHERIT`) + createdRoles.push(role) + } + await admin.query(`CREATE DATABASE ${quote(databaseName)} OWNER ${quote(ownerRole)} TEMPLATE template0`); databaseCreated = true + const app = connectRole(restorerRole), owner = connectRole(ownerRole), runtime = connectRole(runtimeRole) + const adminUrl = new URL(base); adminUrl.pathname = "/" + databaseName + const adminTarget = new PgPool({ connectionString: adminUrl.toString() }); connections.push(adminTarget) + await owner.query(`GRANT USAGE,CREATE ON SCHEMA public TO ${quote(restorerRole)}`) + await adminTarget.query(`ALTER SCHEMA public OWNER TO ${quote(restorerRole)}`) + await applySqlMigrations(app) + assert.equal(Number((await app.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + await owner.query(`CREATE SCHEMA mmapp_control AUTHORIZATION ${quote(ownerRole)}`) + await owner.query(BANDWIDTH_ADMISSION_PROPOSAL_DDL) + await owner.query(BANDWIDTH_CONTROL_SOURCE_DDL) + await owner.query("REVOKE ALL ON SCHEMA mmapp_control FROM PUBLIC") + await owner.query("REVOKE ALL ON ALL TABLES IN SCHEMA mmapp_control FROM PUBLIC") + await owner.query("REVOKE ALL ON ALL FUNCTIONS IN SCHEMA mmapp_control FROM PUBLIC") + await owner.query(`GRANT USAGE ON SCHEMA mmapp_control TO ${quote(runtimeRole)}`) + await owner.query(`GRANT SELECT,INSERT,UPDATE ON mmapp_control.bandwidth_attempts TO ${quote(runtimeRole)}`) + await owner.query(`GRANT SELECT,INSERT,DELETE ON mmapp_control.bandwidth_node_occupancy TO ${quote(runtimeRole)}`) + await owner.query(`GRANT SELECT,INSERT ON mmapp_control.bandwidth_attempt_events TO ${quote(runtimeRole)}`) + await owner.query(`GRANT SELECT ON mmapp_control.bandwidth_source_identity TO ${quote(runtimeRole)}`) + await owner.query(`GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA mmapp_control TO ${quote(runtimeRole)}`) + const roles = (await admin.query(`SELECT rolname,rolsuper,rolcreatedb,rolcreaterole,rolreplication,rolbypassrls,rolinherit FROM pg_catalog.pg_roles WHERE rolname=ANY($1::text[]) ORDER BY rolname`, [[ownerRole, runtimeRole, restorerRole]])).rows + assert.equal(roles.length, 3) + for (const role of roles) for (const field of ["rolsuper", "rolcreatedb", "rolcreaterole", "rolreplication", "rolbypassrls", "rolinherit"]) assert.equal(role[field], false) + assert.equal((await app.query("SELECT pg_catalog.has_database_privilege(current_user,current_database(),'CREATE') AS allowed")).rows[0].allowed, false) + const outsideUrl = new URL(base); outsideUrl.username = restorerRole; outsideUrl.password = passwords.get(restorerRole)! + const outside = new PgPool({ connectionString: outsideUrl.toString() }); connections.push(outside) + await assert.rejects(outside.query(`DROP DATABASE ${quote(databaseName)}`), (error: unknown) => (error as { code?: string }).code === "42501") + cases.push("three actual restricted LOGIN roles; control owner owns target DATABASE and schema, app-restorer receives only public USAGE/CREATE for normal39 objects; restorer lacks database CREATE and actual outside-session DROP DATABASE is denied by permission") + + await assertBandwidthControlRuntimeRole(runtime, expected, live) + await assert.rejects(assertBandwidthControlRuntimeRole(owner, expected, live)) + await assert.rejects(assertBandwidthControlRuntimeRole(adminTarget, expected, live)) + await owner.query(`GRANT CREATE ON SCHEMA mmapp_control TO ${quote(runtimeRole)}`) + await assert.rejects(assertBandwidthControlRuntimeRole(runtime, expected, live)) + await owner.query(`REVOKE CREATE ON SCHEMA mmapp_control FROM ${quote(runtimeRole)}`) + await owner.query(`GRANT DELETE ON mmapp_control.bandwidth_attempts TO ${quote(runtimeRole)}`) + await assert.rejects(assertBandwidthControlRuntimeRole(runtime, expected, live)) + await owner.query(`REVOKE DELETE ON mmapp_control.bandwidth_attempts FROM ${quote(runtimeRole)}`) + await admin.query(`GRANT ${quote(ownerRole)} TO ${quote(runtimeRole)}`) + await assert.rejects(assertBandwidthControlRuntimeRole(runtime, expected, live)) + await admin.query(`REVOKE ${quote(ownerRole)} FROM ${quote(runtimeRole)}`) + await assertBandwidthControlRuntimeRole(runtime, expected, live) + cases.push("actual runtime role verifier accepts precise DML role and refuses owner/admin sessions, extra schema CREATE, extra attempt DELETE and dangerous owner membership; restored least privilege accepted") + + const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") + const { privateKey, publicKey } = generateKeyPairSync("ed25519"), eventKey = randomBytes(32) + const publicDer = publicKey.export({ type: "spki", format: "der" }), publicPem = publicKey.export({ type: "spki", format: "pem" }) + const installationId = randomUUID() + let controlGeneration:string = randomUUID() + const databaseOid = Number((await runtime.query("SELECT oid::int AS id FROM pg_catalog.pg_database WHERE datname=current_database()")).rows[0].id) + let record:BandwidthControlRecord = { format: "mmapp-bandwidth-control-source-v1" as const, installationId, controlGeneration, databaseName, runtimeRole, ownerRole, databaseOid, + endpointSha256: bandwidthControlEndpointSha256(roleUrl(runtimeRole)), schemaSourceSha256: bandwidthControlSchemaSourceSha256(), + catalogSha256: await observeBandwidthControlCatalog(runtime, live), eventKeySha256: sha(eventKey), + attestationRef: "Owned synthetic provisioner fixture; real Ed25519 signature; not production enrollment" } + const recordBytes = Buffer.from(canonicalBandwidthControlRecord(record), "utf8") + let envelope = { format: "mmapp-bandwidth-control-envelope-v1", payload: recordBytes.toString("base64url"), signature: sign(null, recordBytes, privateKey).toString("base64url") } + const attestationFile = resolve(process.env.MCP_STATE_DIR!, "bandwidth-role-attestation.json") + const trustKeyFile = resolve(process.env.MCP_STATE_DIR!, "bandwidth-role-trust.pem"), eventKeyFile = resolve(process.env.MCP_STATE_DIR!, "bandwidth-role-event.key") + await writeFile(attestationFile, JSON.stringify(envelope), { flag: "wx", mode: 0o600 }) + await writeFile(trustKeyFile, publicPem, { flag: "wx", mode: 0o600 }); await writeFile(eventKeyFile, eventKey, { flag: "wx", mode: 0o600 }) + // The configured application connection is the restricted app-restorer in + // the original application database, never the disposable admin account or + // the separately owned control database. + const applicationUrl = new URL(base) + applicationUrl.username = restorerRole; applicationUrl.password = passwords.get(restorerRole)! + const config = { databaseUrl: roleUrl(runtimeRole), applicationDatabaseUrl: applicationUrl.toString(), attestationFile, trustKeyFile, trustKeySha256: sha(publicDer), eventKeyFile } + assert.throws(() => createConfiguredBandwidthControlSource({ ...config, applicationDatabaseUrl: roleUrl(restorerRole) })) + for(const parameter of ["user="+ownerRole,"host=other-host","dbname=other-db","options=-crole%3D"+ownerRole]){ + assert.throws(()=>createConfiguredBandwidthControlSource({...config,applicationDatabaseUrl:config.applicationDatabaseUrl+"?"+parameter})) + assert.throws(()=>createConfiguredBandwidthControlSource({...config,databaseUrl:config.databaseUrl+"?"+parameter})) + } + const source = createConfiguredBandwidthControlSource(config); sourceClosers.push(async () => { await source.close() }) + const unprovisioned = await rows(owner) + await assert.rejects(source.connect(scope())) + assert.deepEqual(await rows(owner), unprovisioned) + assert.equal((await owner.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_source_identity")).rows[0].n, 0) + cases.push("configured control cannot reuse application database name; authentic signed files without externally installed source row refuse without automatic identity enrollment or ledger writes") + // Exercise the real explicit operator issuer, rather than inserting its row + // or signing its final record in the fixture. No startup enrollment is used. + await unlink(attestationFile) + const issuerPrivateKeyFile=resolve(process.env.MCP_STATE_DIR!,"bandwidth-role-issuer.pem") + await writeFile(issuerPrivateKeyFile,privateKey.export({type:"pkcs8",format:"pem"}),{flag:"wx",mode:0o600}) + const provisionConfig={...config,ownerDatabaseUrl:roleUrl(ownerRole),issuerPrivateKeyFile, + attestationRef:"Explicit owned operator issuer acceptance; empty preinstalled store; not physical node enrollment"} + await assert.rejects(sealEmptyBandwidthControlSource({...provisionConfig,trustKeySha256:"0".repeat(64)})) + assert.deepEqual(await rows(owner),unprovisioned) + let directorySync=false + try{const directory=await open(process.env.MCP_STATE_DIR!,"r");try{await directory.sync();directorySync=true}finally{await directory.close()}}catch{/* Actual filesystem capability, not a simulated durability acknowledgement. */} + if(directorySync){ + record={...await sealEmptyBandwidthControlSource(provisionConfig)} + controlGeneration=record.controlGeneration;operatorIssuerAccepted=true + envelope=JSON.parse(await readFile(attestationFile,"utf8")) + assert.equal(await readFile(attestationFile,"utf8"),await readFile(attestationFile+".pending","utf8")) + const sealed=await rows(owner) + await assert.rejects(sealEmptyBandwidthControlSource(provisionConfig)) + assert.deepEqual(await rows(owner),sealed) + cases.push("actual durable-filesystem operator issuer seals empty preinstalled store using existing keys, verifies committed runtime identity before no-replace publication, preserves pending evidence and refuses rerun") + }else{ + await assert.rejects(sealEmptyBandwidthControlSource(provisionConfig)) + assert.deepEqual(await rows(owner),unprovisioned) + for(const path of [attestationFile,attestationFile+".pending"])await assert.rejects(stat(path),(error:unknown)=>(error as NodeJS.ErrnoException).code==="ENOENT") + // This is explicitly the source-reader fixture, not issuer acceptance. + await writeFile(attestationFile,JSON.stringify(envelope),{flag:"wx",mode:0o600}) + await owner.query("INSERT INTO mmapp_control.bandwidth_source_identity(id,installation_id,control_generation,attestation_sha256,event_key_sha256) VALUES(1,$1,$2,$3,$4)",[installationId,controlGeneration,sha(recordBytes),sha(eventKey)]) + cases.push("actual filesystem cannot flush directories: explicit issuer refuses before SQL/pending publication with no ledger/file changes; reader tests continue with explicitly synthetic owner-installed identity, not an issuer durability claim") + } + const firstLease = await source.connect(scope()) + try { assert.equal((await firstLease.query("SELECT CURRENT_USER AS name")).rows[0].name, runtimeRole) } finally { firstLease.release() } + { + const before=await rows(owner),pin=sha(recordBytes),value={nested:{field:"original"}},snapshot=canonical(value) + const pending=source.admissionProof("bandwidth-request-v1",value,pin,scope()) + value.nested.field="mutated-after-call" + const proof=await pending + assert.equal(proof,createHmac("sha256",eventKey).update("mmapp-bandwidth-admission-v1:bandwidth-request-v1:"+snapshot).digest("hex")) + assert.notEqual(proof,await source.admissionProof("bandwidth-command-v1",JSON.parse(snapshot),pin,scope())) + await assert.rejects(source.admissionProof("arbitrary-unapproved-domain",{},pin,scope())) + await assert.rejects(source.admissionProof("bandwidth-request-v1",{},"a".repeat(64),scope())) + const large={raw:"x".repeat(8*1048576)} + assert.equal(await source.admissionProof("bandwidth-terminal-v1",large,pin,scope()),createHmac("sha256",eventKey).update("mmapp-bandwidth-admission-v1:bandwidth-terminal-v1:"+canonical(large)).digest("hex")) + await assert.rejects(source.admissionProof("bandwidth-terminal-v1","x".repeat(9*1048576),pin,scope())) + assert.deepEqual(await rows(owner),before) + cases.push("admission HMAC captures before await, separates fixed purpose domains, pins original signed source, accepts full eight-MiB native payload and refuses oversized/unknown-domain/replaced-source proofs without data changes") + } + { + const before=await rows(owner) + const foreignSource=createConfiguredBandwidthControlSource(config) + sourceClosers.push(async()=>{await foreignSource.close()}) + const windows=Array.from({length:4},()=>scope()) + const leases=await Promise.all(windows.map(window=>source.connect(window))) + try{ + const pids=await Promise.all(leases.map(async lease=>(await lease.query("SELECT pg_backend_pid() AS pid")).rows[0].pid)) + assert.equal(new Set(pids).size,4) + const events=leases.map((_lease,index)=>({fixture:"same-lease-event",index,nested:{value:"original"}})) + const snapshots=events.map(event=>canonical(event)) + const proofs=leases.map((lease,index)=>source.eventProofOnConnection(events[index],lease,windows[index])) + for(const event of events)event.nested.value="mutated-after-call" + assert.deepEqual(await Promise.all(proofs),snapshots.map(snapshot=>createHmac("sha256",eventKey).update("bandwidth-control-event-v1:"+snapshot).digest("hex"))) + assert.deepEqual(await Promise.all(leases.map(async lease=>(await lease.query("SELECT pg_backend_pid() AS pid")).rows[0].pid)),pids) + await assert.rejects(foreignSource.eventProofOnConnection({},leases[0],scope())) + await assert.rejects(source.eventProofOnConnection({},Object.freeze({...leases[0]}),scope())) + await assert.rejects(source.eventProofOnConnection({},firstLease,scope())) + await assert.rejects(source.eventProofOnConnection({payload:"x".repeat(65537)},leases[0],scope())) + const canceled=new AbortController();canceled.abort() + await assert.rejects(source.eventProofOnConnection({},leases[0],{...scope(),signal:canceled.signal})) + await assert.rejects(source.eventProofOnConnection({},leases[0],{deadlineAt:Date.now()-1})) + assert.deepEqual(await rows(owner),before) + }finally{for(const lease of leases)lease.release()} + await assert.rejects(source.eventProofOnConnection({},leases[0],scope())) + let allowed=true,guardCalls=0 + const guarded=await source.connect(scope(),async()=>{guardCalls++;assert(allowed)}) + allowed=false + try{await assert.rejects(source.eventProofOnConnection({},guarded,scope()));assert(guardCalls>0)}finally{guarded.release(true)} + assert.deepEqual(await rows(owner),before) + cases.push("four actual runtime leases saturate pool and sign captured events on the same four backend PIDs without fifth checkout; foreign/fake/released leases, oversized payload, abort, expiry and revoked original dispatch guard refuse with exact control data unchanged") + } + const makeJournal = (proofGate?:()=>Promise) => { + let activeScope: BandwidthScope | undefined + let activeConnection: Awaited> | undefined + const journal = createBandwidthAdmissionSqlJournal({ now: Date.now, connect: async window => { activeConnection=await source.connect(window); return activeConnection }, + authorize: async (binding, window) => { assert.equal(binding.controlGeneration, controlGeneration); assert.equal(binding.authorityProof, fixtureMac("authority")); assert(Date.now() < window.deadlineAt); window.signal?.throwIfAborted() }, + verifyEvidence: async (transition, binding, evidence) => { + if (transition === "complete") assert.deepEqual(evidence, { terminalProof: completionProof(binding) }) + if (transition === "release-before-dispatch") assert.deepEqual(evidence, {from:"reserved",proof:"private-no-send"}) + }, + eventProof: async event => { assert(activeScope); assert(activeConnection); if(proofGate)await proofGate(); return source.eventProofOnConnection(event, activeConnection, activeScope) }, + }) + const run = async (work: (window: BandwidthScope) => Promise): Promise => { activeScope = scope(); try { return await work(activeScope) } finally { activeScope = undefined; activeConnection=undefined } } + return { reserve: (binding: BandwidthBinding) => run(window => journal.reserve(binding, window)), arm: (binding: BandwidthBinding) => run(window => journal.arm(binding, window)), quarantine: (binding: BandwidthBinding) => run(window => journal.quarantine(binding, window)), + complete:(binding:BandwidthBinding)=>run(window=>journal.complete(binding,{terminalProof:completionProof(binding)},window)), + noSend:(binding:BandwidthBinding)=>run(window=>journal.releaseBeforeDispatch(binding,{from:"reserved",proof:"private-no-send"},window)) } + } + const fixture = makeJournal(), b = fixtureBinding("roles:A", "roles:B", { controlGeneration }) + await fixture.reserve(b); await fixture.arm(b); await fixture.quarantine(b) + cases.push("genuine owned Ed25519 signed record, pinned SPKI key, private raw event key and immutable source row verified by configured runtime-role source; journal uses that source for every connection and event MAC") + const {acceptBandwidthJournalDurability}=await import("./bandwidth-journal-durability.sql-helper.js") + const journalDurabilityAcceptance=await acceptBandwidthJournalDurability(source,owner,controlGeneration) + cases.push("actual runtime session synchronous_commit=off cannot weaken journal durability: all eight real COMMIT observe on, persisted MACs/terminal occupancy verified and LOCAL setting does not leak") + const {acceptBandwidthNativePermits}=await import("./bandwidth-native-permit.sql-helper.js") + const nativeTransportAcceptance=await acceptBandwidthNativePermits(source,owner,controlGeneration) + cases.push(...nativeTransportAcceptance.checks) + { + let arrived=0,release!:()=>void + const gate=new Promise(resolve=>{release=resolve}) + const proofGate=async()=>{if(++arrived===4)release();await gate} + const bindings=Array.from({length:4},(_unused,index)=>fixtureBinding("pool:A:"+index,"pool:B:"+index,{controlGeneration})) + const journals=bindings.map(()=>makeJournal(proofGate)) + await Promise.all(journals.map((journal,index)=>journal.reserve(bindings[index]))) + assert.equal(arrived,4) + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=ANY($1::uuid[])",[bindings.map(binding=>binding.attemptId)])).rows[0].n),8) + await Promise.all(journals.map((journal,index)=>journal.noSend(bindings[index]))) + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=ANY($1::uuid[])",[bindings.map(binding=>binding.attemptId)])).rows[0].n),0) + cases.push("four real journal transactions reach signing barrier together with all four pool leases held, commit reserves through same-lease MACs and release only proven-no-send occupancy without nested checkout") + } + { + const completed=fixtureBinding("roles:complete:A","roles:complete:B",{controlGeneration}),finished=makeJournal() + await finished.reserve(completed);await finished.arm(completed);await finished.complete(completed) + await assert.rejects(finished.complete(completed)) + const notSent=fixtureBinding("roles:no-send:A","roles:no-send:B",{controlGeneration}),cancelled=makeJournal() + await cancelled.reserve(notSent);await cancelled.noSend(notSent) + for(const attempt of [completed,notSent]){ + assert.equal((await owner.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1",[attempt.attemptId])).rows[0].n,0) + assert.equal((await owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1",[attempt.attemptId])).rows[0].state,"released") + } + assert.equal((await owner.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1",[b.attemptId])).rows[0].n,2) + cases.push("actual restricted runtime completes strict synthetic terminal attempt and releases reserved synthetic no-send attempt with exact DELETE rights; duplicate completion refused and unrelated quarantined pair stays occupied") + } + const original = await rows(owner), guards = await metadata(owner) + assert.equal(guards.length, 11); assert(guards.every(row => row.enabled === "A")) + cases.push("actual least-privilege runtime commits keyed reserve/arm/quarantine while preserving both unknown node owners") + + const denied = [ + "SELECT * FROM mmapp_control.bandwidth_attempts", + "SELECT * FROM mmapp_control.bandwidth_source_identity", + "UPDATE mmapp_control.bandwidth_attempts SET state='released'", + "DELETE FROM mmapp_control.bandwidth_node_occupancy", + "DROP TABLE mmapp_control.bandwidth_attempts CASCADE", + "ALTER TABLE mmapp_control.bandwidth_attempts ADD COLUMN hostile integer", + "DROP SCHEMA mmapp_control CASCADE", + ] + for (const sql of denied) await assert.rejects(app.query(sql)) + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_source_identity SET status='quarantined' WHERE id=1")) + await assert.rejects(runtime.query("DELETE FROM mmapp_control.bandwidth_source_identity")) + await assert.rejects(app.query("SET session_replication_role=replica")) + await assert.rejects(runtime.query("SET session_replication_role=replica")) + for (const role of [ownerRole, runtimeRole, base.username]) await assert.rejects(app.query(`SET ROLE "${role.replaceAll('"', '""')}"`)) + await app.query(`CREATE FUNCTION public.bw_owned_crafted(command text) RETURNS text LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog AS $body$ + BEGIN EXECUTE command; RETURN 'unexpectedly-allowed'; END $body$`) + for (const sql of denied) await assert.rejects(app.query("SELECT public.bw_owned_crafted($1)", [sql])) + assert.deepEqual(await rows(owner), original); assert.deepEqual(await metadata(owner), guards) + cases.push("actual app-restorer cannot read/write/drop/alter control, SET ROLE owner/runtime/admin, or use its own crafted PUBLIC SECURITY DEFINER function to reach control; rows and ALWAYS guards unchanged") + + if (process.env.MMAPP_OWN_BANDWIDTH_RESTORE === "true") { + const { runPrivateDatabaseDump } = await import("./database-dump-connection.js") + const { restorePartitionedDatabase } = await import("./system-database-partition-restore.js") + const execute = promisify(execFile), file = resolve(process.env.MCP_STATE_DIR!, "bandwidth-role-public.dump") + const appUrl = new URL(roleUrl(restorerRole)), env = { ...process.env, PGHOST: appUrl.hostname, PGPORT: appUrl.port, PGUSER: restorerRole, PGPASSWORD: passwords.get(restorerRole)! } + const command = async (program: string, args: string[]): Promise => { + try { return (await execute(program, args, { env, encoding: "buffer", timeout: 30000, maxBuffer: 32 * 1048576, windowsHide: true })).stdout } + catch { throw Error("Owned restricted-role archive command failed: " + program) } + } + const marker = Number((await app.query("INSERT INTO public.servers(name,host,enabled) VALUES('role-archive-before','127.0.0.1',false) RETURNING id")).rows[0].id) + await writeFile(file, await runPrivateDatabaseDump(appUrl.toString(), { authorize: async () => {} }), { flag: "wx", mode: 0o600 }) + const toc = (await command("pg_restore", ["--list", file])).toString("utf8"); assert(!toc.includes("mmapp_control")) + await app.query("UPDATE public.servers SET name='role-archive-after' WHERE id=$1", [marker]) + await restorePartitionedDatabase(file, appUrl.toString(), { command, prepareSql: path => writeFile(path, "", { flag: "wx", mode: 0o600 }) }) + assert.equal((await app.query("SELECT name FROM public.servers WHERE id=$1", [marker])).rows[0].name, "role-archive-before") + assert.deepEqual(await rows(owner), original); assert.deepEqual(await metadata(owner), guards) + await assertBandwidthControlRuntimeRole(runtime, expected, live) + const lease = await source.connect(scope()); lease.release() + const next = fixtureBinding("roles:B", "roles:C", { controlGeneration }), restarted = makeJournal() + await assert.rejects(restarted.reserve(next)) + assert.deepEqual(await rows(owner), original) + cases.push("actual public-only dump and transactional restore AUTHENTICATED AS non-superuser app-restorer restore public marker, preserve unknown control rows/ALWAYS metadata, and leave overlapping runtime reserve blocked") + } + { + const sockets=new Set(),blackhole=createServer(socket=>{sockets.add(socket);socket.on("error",()=>{});socket.on("close",()=>sockets.delete(socket))}) + await new Promise((done,reject)=>{blackhole.once("error",reject);blackhole.listen(0,"127.0.0.1",done)}) + const address=blackhole.address();assert(address&&typeof address!=="string") + const stalledUrl=new URL(config.databaseUrl);stalledUrl.port=String(address.port) + const signed=Buffer.from(canonicalBandwidthControlRecord({...record,endpointSha256:bandwidthControlEndpointSha256(stalledUrl.toString())})) + const stalledFile=resolve(process.env.MCP_STATE_DIR!,"bandwidth-physical-connect-timeout.json") + await writeFile(stalledFile,JSON.stringify({format:"mmapp-bandwidth-control-envelope-v1",payload:signed.toString("base64url"),signature:sign(null,signed,privateKey).toString("base64url")}),{flag:"wx",mode:0o600}) + const stalled=createConfiguredBandwidthControlSource({...config,databaseUrl:stalledUrl.toString(),attestationFile:stalledFile}) + const began=Date.now(),before=await rows(owner) + try{ + await assert.rejects(stalled.connect({deadlineAt:Date.now()+10000})) + await stalled.close() + assert(Date.now()-began<8000,"actual physical checkout and pool shutdown must finish before TCP/default timeout") + assert.deepEqual(await rows(owner),before) + }finally{ + await stalled.close();for(const socket of sockets)socket.destroy() + await new Promise((done,reject)=>blackhole.close(error=>error?reject(error):done())) + } + cases.push("actual loopback TCP peer accepts but never completes PostgreSQL handshake: physical checkout timeout and pool close finish within 8s, no SQL or ledger mutation and no abandoned healthy connection") + } + { + const before = await rows(owner), lease = await source.connect(scope()) + try { + await writeFile(attestationFile, JSON.stringify({ ...envelope, payload: Buffer.from(canonicalBandwidthControlRecord({ ...record, controlGeneration: randomUUID() })).toString("base64url") })) + await assert.rejects(lease.query("SELECT 1")); await assert.rejects(source.connect(scope())) + } finally { lease.release(true); await writeFile(attestationFile, JSON.stringify(envelope), { mode: 0o600 }) } + const signingLease=await source.connect(scope()) + await writeFile(eventKeyFile, randomBytes(32), { mode: 0o600 }) + try { + await assert.rejects(source.eventProof({ fixture: "event-key-drift" }, scope())) + await assert.rejects(source.eventProofOnConnection({fixture:"same-lease-key-drift"},signingLease,scope())) + } finally { signingLease.release(true);await writeFile(eventKeyFile, eventKey, { mode: 0o600 }) } + const healthy = await source.connect(scope()); healthy.release() + assert.deepEqual(await rows(owner), before) + cases.push("live configured lease and new checkout refuse unsigned changed generation; event MAC refuses private key drift; restoring exact trusted files creates no ledger writes") + } + { + const before=await rows(owner) + const altered=[ + {...envelope,signature:"A".repeat(86)}, + {...envelope,payload:Buffer.from(JSON.stringify(record,null,2)).toString("base64url")}, + {...envelope,unknownField:true}, + ] + for(const value of altered){ + await writeFile(attestationFile,JSON.stringify(value)) + try{await assert.rejects(source.connect(scope()))}finally{await writeFile(attestationFile,JSON.stringify(envelope))} + } + const wrongIssuer=generateKeyPairSync("ed25519").publicKey.export({type:"spki",format:"pem"}) + await writeFile(trustKeyFile,wrongIssuer) + try{await assert.rejects(source.connect(scope()))}finally{await writeFile(trustKeyFile,publicPem)} + const lease=await source.connect(scope()) + await owner.query("ALTER TABLE mmapp_control.bandwidth_source_identity DISABLE TRIGGER bandwidth_source_identity_guard") + try{await assert.rejects(lease.query("SELECT 1"));await assert.rejects(source.connect(scope()))} + finally{lease.release(true);await owner.query("ALTER TABLE mmapp_control.bandwidth_source_identity ENABLE ALWAYS TRIGGER bandwidth_source_identity_guard")} + const healthy=await source.connect(scope());healthy.release() + assert.deepEqual(await rows(owner),before) + cases.push("actual malformed signature, noncanonical payload, unknown envelope field, substituted issuer and disabled ALWAYS source guard refuse live/new source leases without changing ledger; restoring exact files/catalog recovers only the same identity") + } + { + const before = await rows(owner, false) + await owner.query("UPDATE mmapp_control.bandwidth_source_identity SET status='quarantined' WHERE id=1") + await assert.rejects(source.connect(scope())); await assert.rejects(source.eventProof({ fixture: "quarantined" }, scope())) + assert.deepEqual(await rows(owner, false), before) + cases.push("actual external owner source quarantine blocks new leases/event signing and never clears existing unknown occupancy") + } + console.log("Owned bandwidth control source/roles:", JSON.stringify({ cases, nativeTransportAcceptance, journalDurabilityAcceptance, normalMigrationCount: MIGRATION_MANIFEST.length, productionReady: false, productionIssuerAccepted: false, operatorIssuerAccepted, + nativeChrCalls: 0, actualRestrictedRoles: 3, sourceGenerationClaimed: false, runtimeDispatchIntegrated: false, + limits: ["Role isolation is proven only for the explicit exercised SQL/function/restore attempts", "Control-provider rollback/clone fencing, physical router identity and mandatory native dispatch integration remain unproven; explicit issuer does not supply native readiness"] })) +} finally { + for (const close of sourceClosers) await close() + for (const connection of connections) await connection.end() + if (databaseCreated) await admin.query(`DROP DATABASE ${quote(databaseName)}`) + for (const role of createdRoles.reverse()) await admin.query(`DROP ROLE ${quote(role)}`) + await closePool() +} diff --git a/backend/src/services/bandwidth-control-source.ts b/backend/src/services/bandwidth-control-source.ts new file mode 100644 index 00000000..43d6cd7e --- /dev/null +++ b/backend/src/services/bandwidth-control-source.ts @@ -0,0 +1,516 @@ +/** Operational reader for an externally signed, explicitly provisioned control + * source. Constructor/import performs no I/O or enrollment. This authenticates a + * configured component source, not general application-schema READY or physical + * node identity, and cannot certify rollback of the control provider itself. */ +import { constants, type Stats } from "node:fs" +import { lstat, open, realpath } from "node:fs/promises" +import { isAbsolute, resolve } from "node:path" +import { createHash, createHmac, createPublicKey, verify, type KeyObject } from "node:crypto" +import { performance } from "node:perf_hooks" +import { types } from "node:util" +import { Pool } from "pg" +import { z } from "zod" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_CONTROL_SOURCE_DDL } from "./bandwidth-control-source-schema.js" +import { observeBandwidthControlCatalog } from "./bandwidth-control-catalog.js" +import { assertBandwidthControlRegistryIdentity } from "./bandwidth-control-registry-identity.js" +import { assertBandwidthControlIdentity, type BandwidthControlIdentityExpected } from "./bandwidth-control-identity.js" +import { createProbeStorageConnectionConnector } from "./server-probe-storage-connection.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { bandwidthNodeBootstrapSchema,bandwidthNodeBootstrapEnvelopeSchema,canonicalBandwidthNodeBootstrap,bandwidthNodeRegistrySchemaSourceSha256,bandwidthPeerRegistrySchemaSourceSha256,type BandwidthNodeBootstrap } from "./bandwidth-node-bootstrap.js" + +export { BANDWIDTH_CONTROL_SOURCE_DDL, observeBandwidthControlCatalog } +const hash=z.string().regex(/^[a-f0-9]{64}$/),role=z.string().regex(/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/) +export const bandwidthControlRecordSchema=z.object({ + format:z.literal("mmapp-bandwidth-control-source-v1"),installationId:z.string().uuid(),controlGeneration:z.string().uuid(), + databaseName:z.string().min(1).max(63),databaseOid:z.number().int().positive().max(4294967295),runtimeRole:role,ownerRole:role, + endpointSha256:hash,schemaSourceSha256:hash,catalogSha256:hash,eventKeySha256:hash,attestationRef:z.string().trim().min(30).max(2000), +}).strict() +export type BandwidthControlRecord=z.infer +export interface BandwidthControlSourceConfig { + databaseUrl:string;applicationDatabaseUrl:string;attestationFile:string;trustKeyFile:string;trustKeySha256:string;eventKeyFile:string +} +export class BandwidthControlSourceError extends Error {readonly code="BANDWIDTH_CONTROL_SOURCE_UNAVAILABLE";constructor(){super("Подписанный источник bandwidth control не подтверждён")}} +const refused=():never=>{throw new BandwidthControlSourceError()} +const digest=(value:string|Buffer)=>createHash("sha256").update(value).digest("hex") +function canonical(value:unknown):string { + if(Array.isArray(value))return "["+value.map(canonical).join(",")+"]" + if(value&&typeof value==="object")return "{"+Object.entries(value).sort(([a],[b])=>a.localeCompare(b)).map(([key,item])=>JSON.stringify(key)+":"+canonical(item)).join(",")+"}" + return JSON.stringify(value) +} +export function canonicalBandwidthControlRecord(value:unknown):string{return canonical(bandwidthControlRecordSchema.parse(value))} +export function bandwidthControlSchemaSourceSha256():string{return digest(BANDWIDTH_ADMISSION_PROPOSAL_DDL+"\n"+BANDWIDTH_CONTROL_SOURCE_DDL)} +function databaseUrl(raw:string):URL { + let value:URL;try{value=new URL(raw)}catch{throw new BandwidthControlSourceError()} + if(!["postgres:","postgresql:"].includes(value.protocol)||!value.hostname||!value.username||!value.password||value.hash||!/^\/[^/]+$/.test(value.pathname))refused() + // pg-connection-string lets query parameters override authority/path fields. + // In particular ?user=owner must never bypass the application-role fence. + const allowed=new Set(["sslmode","sslrootcert","sslcert","sslkey","sslpassword"]),seen=new Set() + for(const key of value.searchParams.keys()){if(!allowed.has(key)||seen.has(key))refused();seen.add(key)} + return value +} +export function bandwidthControlEndpointSha256(raw:string):string { + const value=databaseUrl(raw);value.password="";value.protocol="postgresql:";value.hostname=value.hostname.toLowerCase();value.port=value.port||"5432";value.searchParams.sort() + return digest(value.toString()) +} +const envelopeSchema=z.object({format:z.literal("mmapp-bandwidth-control-envelope-v1"),payload:z.string().min(1).max(24000).regex(/^[A-Za-z0-9_-]+$/),signature:z.string().length(86).regex(/^[A-Za-z0-9_-]+$/)}).strict() +type Material={record:BandwidthControlRecord;recordHash:string;eventKey:Buffer;trustKey:KeyObject} +type PublicMaterial=Pick +export interface BandwidthVerifiedNodeBootstrap { + bootstrap:Readonly + source:Readonly<{installationId:string;controlGeneration:string;sourceRecordSha256:string;databaseName:string;runtimeRole:string;ownerRole:string}> +} +/** Public signed metadata only. No current key, credential, SQL/source-role, + * quiescence or dispatch authority. Deliberately not the strong verified type. */ +export interface BandwidthPublicNodeBootstrapMetadata { + format:"mmapp-bandwidth-public-node-metadata-v1" + bootstrap:Readonly + source:Readonly<{installationId:string;controlGeneration:string;sourceRecordSha256:string}> + publicDatabase:Readonly<{databaseName:string;runtimeRole:string;ownerRole:string;databaseOid:number;catalogSha256:string}> +} +/** Current public SQL metadata only, never a writer/Q/strong-source lease. */ +export interface BandwidthPublicRegistryMetadataLease { + inspect(envelope:unknown):Promise> + close():void +} +const PUBLIC_PID_SQL="SELECT pg_catalog.pg_backend_pid() AS backend_pid" +const PUBLIC_LOCK_SQL="SELECT pg_catalog.pg_try_advisory_lock(8723116) AS acquired" +const PUBLIC_HELD_SQL="SELECT pg_catalog.pg_backend_pid() AS backend_pid, EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND pid=pg_catalog.pg_backend_pid() AND classid=0::pg_catalog.oid AND objid=8723116::pg_catalog.oid AND objsubid=1 AND mode='ExclusiveLock' AND granted) AS held" +function publicDataFields(value:unknown,keys:readonly string[]){ + if(!value||typeof value!=="object"||types.isProxy(value)||Object.getPrototypeOf(value)!==Object.prototype)refused() + const descriptors=Object.getOwnPropertyDescriptors(value) + if(Reflect.ownKeys(descriptors).some(key=>typeof key!=="string"||!keys.includes(key)) + ||Object.values(descriptors).some(item=>!("value"in item)||!item.enumerable))refused() + return descriptors +} +function capturePublicEnvelope(envelope:unknown){ + const fields=publicDataFields(envelope,["format","payload","signature"]) + return bandwidthNodeBootstrapEnvelopeSchema.parse({format:fields.format?.value,payload:fields.payload?.value,signature:fields.signature?.value}) +} +function publicNodeMetadata(signed:ReturnType,value:PublicMaterial):BandwidthPublicNodeBootstrapMetadata{ + const nodeBytes=Buffer.from(signed.payload,"base64url"),nodeSignature=Buffer.from(signed.signature,"base64url") + if(nodeBytes.toString("base64url")!==signed.payload||nodeSignature.toString("base64url")!==signed.signature||nodeSignature.length!==64||!verify(null,nodeBytes,value.trustKey,nodeSignature))refused() + const nodeText=new TextDecoder("utf-8",{fatal:true}).decode(nodeBytes),bootstrap=bandwidthNodeBootstrapSchema.parse(JSON.parse(nodeText)),record=value.record + if(canonicalBandwidthNodeBootstrap(bootstrap)!==nodeText||bootstrap.installationId!==record.installationId + ||bootstrap.issuerKeySha256===record.eventKeySha256||bootstrap.registrySchemaSourceSha256!==(bootstrap.format==="mmapp-bandwidth-node-bootstrap-v2"?bandwidthPeerRegistrySchemaSourceSha256():bandwidthNodeRegistrySchemaSourceSha256()))refused() + return Object.freeze({format:"mmapp-bandwidth-public-node-metadata-v1" as const,bootstrap:Object.freeze(bootstrap),source:Object.freeze({installationId:record.installationId,controlGeneration:record.controlGeneration,sourceRecordSha256:value.recordHash}),publicDatabase:Object.freeze({databaseName:record.databaseName,runtimeRole:record.runtimeRole,ownerRole:record.ownerRole,databaseOid:record.databaseOid,catalogSha256:record.catalogSha256})}) +} +/** Server-only, single-dispatch scope. Retaining it past the fixed guard cannot + * bypass source checks or perform late SQL; it never owns another pool lease. */ +export interface BandwidthControlDispatchFence { + query(text:string,values?:unknown[]):Promise<{rows:Record[]}> + verifyNodeBootstrap(envelope:unknown):Promise + sourceIdentity():Promise> +} +export interface BandwidthControlRegistryDispatchFence extends BandwidthControlDispatchFence { + /** Only a genuine verified object from THIS phase; executes fixed joint SQL. */ + assertRegistryIdentity(verified:BandwidthVerifiedNodeBootstrap):Promise +} +function verifyNodeRecord(signed:ReturnType,value:Material):BandwidthVerifiedNodeBootstrap{ + const bytes=Buffer.from(signed.payload,"base64url"),signature=Buffer.from(signed.signature,"base64url") + if(bytes.toString("base64url")!==signed.payload||signature.toString("base64url")!==signed.signature||signature.length!==64||!verify(null,bytes,value.trustKey,signature))refused() + const text=new TextDecoder("utf-8",{fatal:true}).decode(bytes),record=bandwidthNodeBootstrapSchema.parse(JSON.parse(text)) + if(canonicalBandwidthNodeBootstrap(record)!==text||record.installationId!==value.record.installationId + ||record.issuerKeySha256===value.record.eventKeySha256||record.registrySchemaSourceSha256!==(record.format==="mmapp-bandwidth-node-bootstrap-v2"?bandwidthPeerRegistrySchemaSourceSha256():bandwidthNodeRegistrySchemaSourceSha256()))refused() + return Object.freeze({bootstrap:Object.freeze(record),source:Object.freeze({installationId:value.record.installationId, + controlGeneration:value.record.controlGeneration,sourceRecordSha256:value.recordHash,databaseName:value.record.databaseName, + runtimeRole:value.record.runtimeRole,ownerRole:value.record.ownerRole})}) +} +function sameFile(a:Stats,b:Stats){return a.dev===b.dev&&a.ino===b.ino&&a.size===b.size&&a.mtimeMs===b.mtimeMs&&a.ctimeMs===b.ctimeMs} +export async function readBandwidthControlFile(path:string,max:number,live:()=>void):Promise{ + live();const before=await lstat(path);live() + if(!before.isFile()||before.isSymbolicLink()||before.size<1||before.size>max)refused() + if(await realpath(path)!==path)refused();live() + const handle=await open(path,constants.O_RDONLY|(constants.O_NOFOLLOW??0));let scratch:Buffer|undefined + try{ + live() + const opened=await handle.stat();live();if(!opened.isFile()||!sameFile(before,opened))refused() + // The opened inode's exact size was bounded and matched against lstat. + // Read that size (allowing short reads), then compare fstat again. An extra + // EOF read adds no evidence: truncation, append or replacement must already + // fail the unchanged inode/size/time checks below. Never cache file bytes. + const data=Buffer.alloc(opened.size);scratch=data;let length=0 + while(lengthmax||length!==opened.size||!sameFile(opened,after))refused() + return Buffer.from(data.subarray(0,length)) + }finally{scratch?.fill(0);await handle.close()} +} +const readBounded=readBandwidthControlFile + +/** All arguments are server/operator configuration, never model JSON. Missing + * files, namespace or signed identity refuse; no key, marker or pool fallback. */ +export function createConfiguredBandwidthControlSource(raw:BandwidthControlSourceConfig){ + const config=Object.freeze({...raw}) + const target=databaseUrl(config.databaseUrl),application=databaseUrl(config.applicationDatabaseUrl) + const name=decodeURIComponent(target.pathname.slice(1)),runtime=decodeURIComponent(target.username),appRole=decodeURIComponent(application.username) + if(name===decodeURIComponent(application.pathname.slice(1))||runtime===appRole||!hash.safeParse(config.trustKeySha256).success||!role.safeParse(runtime).success||!role.safeParse(appRole).success)refused() + const paths=[config.attestationFile,config.trustKeyFile,config.eventKeyFile] + if(paths.some(path=>typeof path!=="string"||!isAbsolute(path)||resolve(path)!==path)||new Set(paths).size!==3)refused() + let pool:Pool|undefined,closed=false,closing:Promise|undefined + const verifiedHashes=new WeakMap() + const publicStops=new Set<()=>void>() + async function publicMaterial(live:()=>void):Promise{ + const [bytes,publicBytes]=await Promise.all([readBounded(config.attestationFile,32768,live),readBounded(config.trustKeyFile,16384,live)]) + live() + const publicKey=createPublicKey(publicBytes) + if(publicKey.asymmetricKeyType!=="ed25519"||digest(publicKey.export({type:"spki",format:"der"}))!==config.trustKeySha256)refused() + const control=envelopeSchema.parse(JSON.parse(new TextDecoder("utf-8",{fatal:true}).decode(bytes))) + const payload=Buffer.from(control.payload,"base64url"),signature=Buffer.from(control.signature,"base64url") + if(payload.toString("base64url")!==control.payload||signature.toString("base64url")!==control.signature||signature.length!==64||!verify(null,payload,publicKey,signature))refused() + const text=new TextDecoder("utf-8",{fatal:true}).decode(payload),record=bandwidthControlRecordSchema.parse(JSON.parse(text)) + if(canonicalBandwidthControlRecord(record)!==text||record.databaseName!==name||record.runtimeRole!==runtime||record.ownerRole===runtime + ||record.endpointSha256!==bandwidthControlEndpointSha256(config.databaseUrl)||record.schemaSourceSha256!==bandwidthControlSchemaSourceSha256())refused() + live();return {record,recordHash:digest(payload),trustKey:publicKey} + } + async function inspectPublicNodeBootstrap(envelope:unknown,input:BandwidthScope):Promise{ + // Capture only inert own data properties before any await. In particular, + // zod/JSON must never invoke a caller getter, toJSON or proxy trap. + const startedAt=Date.now(),startedMono=performance.now() + let active=true,timer:ReturnType|undefined,signal:AbortSignal|undefined,stop:(()=>void)|undefined + try{ + const signed=capturePublicEnvelope(envelope) + const lifetime=publicDataFields(input,["deadlineAt","signal"]) + const deadlineAt=lifetime.deadlineAt?.value,capturedSignal=lifetime.signal?.value + if(!Number.isSafeInteger(deadlineAt)||types.isProxy(capturedSignal)||capturedSignal!==undefined&&!(capturedSignal instanceof AbortSignal))refused() + signal=capturedSignal + const end=startedMono+Math.max(0,Math.min(deadlineAt-startedAt,10000)) + const live=()=>{const now=Date.now();if(!active||closed||signal&&Object.getOwnPropertyDescriptor(AbortSignal.prototype,"aborted")!.get!.call(signal)||now=deadlineAt||performance.now()>=end)refused()} + live() + const stopped=new Promise((_resolve,reject)=>{stop=()=>{active=false;reject(new BandwidthControlSourceError())}}) + publicStops.add(stop!) + timer=setTimeout(stop!,Math.max(1,Math.ceil(end-performance.now()))) + if(signal)EventTarget.prototype.addEventListener.call(signal,"abort",stop!,{once:true}) + return await Promise.race([stopped,(async()=>{ + live() + const result=publicNodeMetadata(signed,await publicMaterial(live));live();return result + })()]) + }catch{throw new BandwidthControlSourceError()} + finally{active=false;clearTimeout(timer);if(stop){publicStops.delete(stop);if(signal)EventTarget.prototype.removeEventListener.call(signal,"abort",stop)}} + } + async function material(live:()=>void):Promise{ + // Public files are independent. Complete both reads before acquiring the + // secret key, so a stalled public read cannot retain a key buffer. + const [bytes,publicBytes]=await Promise.all([readBounded(config.attestationFile,32768,live),readBounded(config.trustKeyFile,16384,live)]) + const eventKey=await readBounded(config.eventKeyFile,32,live) + try{ + if(eventKey.length!==32)refused() + const publicKey=createPublicKey(publicBytes) + if(publicKey.asymmetricKeyType!=="ed25519"||digest(publicKey.export({type:"spki",format:"der"}))!==config.trustKeySha256)refused() + const envelope=envelopeSchema.parse(JSON.parse(new TextDecoder("utf-8",{fatal:true}).decode(bytes))) + const payload=Buffer.from(envelope.payload,"base64url"),signature=Buffer.from(envelope.signature,"base64url") + if(payload.toString("base64url")!==envelope.payload||signature.toString("base64url")!==envelope.signature||signature.length!==64||!verify(null,payload,publicKey,signature))refused() + const text=new TextDecoder("utf-8",{fatal:true}).decode(payload),record=bandwidthControlRecordSchema.parse(JSON.parse(text)) + if(canonicalBandwidthControlRecord(record)!==text||record.databaseName!==name||record.runtimeRole!==runtime||record.ownerRole===runtime + ||record.endpointSha256!==bandwidthControlEndpointSha256(config.databaseUrl)||record.schemaSourceSha256!==bandwidthControlSchemaSourceSha256()||record.eventKeySha256!==digest(eventKey))refused() + live();return {record,recordHash:digest(payload),eventKey,trustKey:publicKey} + }catch{eventKey.fill(0);throw new BandwidthControlSourceError()} + } + const connector=createProbeStorageConnectionConnector(async()=>{ + if(closed)refused() + if(!pool){pool=new Pool({connectionString:config.databaseUrl,max:4,connectionTimeoutMillis:3000,query_timeout:5000,options:"-c search_path=pg_catalog",application_name:"mmapp-bandwidth-control"});pool.on("error",()=>{/* Idle backend loss: every later borrow re-verifies its source. */})} + return pool.connect() + }) + /** Fixed public metadata inspection on one physical lease. The session lock + * is observed here, not a quiescence proof or permission to execute writes. */ + async function openPublicRegistryMetadata(envelope:unknown,input:BandwidthScope):Promise{ + const wall=Date.now(),mono=performance.now() + let retire=()=>{} + try{ + const originalSigned=capturePublicEnvelope(envelope),fields=publicDataFields(input,["deadlineAt","signal"]) + const requested:unknown=fields.deadlineAt?.value,signal:unknown=fields.signal?.value + if(typeof requested!=="number"||!Number.isSafeInteger(requested)||signal!==undefined&&(types.isProxy(signal)||!(signal instanceof AbortSignal)))refused() + const deadlineAt=Math.min(requested as number,wall+10000),end=mono+Math.max(0,deadlineAt-wall),originalSignal=signal as AbortSignal|undefined + const aborted=Object.getOwnPropertyDescriptor(AbortSignal.prototype,"aborted")!.get! + const isAborted=()=>originalSignal?Reflect.apply(aborted,originalSignal,[]):false + isAborted() // Reject forged native prototypes before installing listeners. + const controller=new AbortController(),scope=Object.freeze({deadlineAt,signal:controller.signal}) + let active=true,busy=false,released=false,connection:ProbeStorageConnection|undefined,pid:number|undefined,pin:string|undefined + let rejectStopped!:(error:BandwidthControlSourceError)=>void + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject}) + void stopped.catch(()=>{}) + const live=()=>{const now=Date.now();if(!active||closed||connection?.lifetimeSignal?.aborted||isAborted()||now=deadlineAt||performance.now()>=end)refused()} + const release=()=>{if(connection&&!released){released=true;try{connection.release(true)}catch{/* Destroy attempt only; never healthy pool reuse. */}}} + retire=()=>{if(!active){release();return}active=false;clearTimeout(timer);publicStops.delete(retire);if(originalSignal)Reflect.apply(EventTarget.prototype.removeEventListener,originalSignal,["abort",retire]);controller.abort();release();rejectStopped(new BandwidthControlSourceError())} + publicStops.add(retire) + const timer=setTimeout(retire,Math.max(1,Math.ceil(end-performance.now()))) + if(originalSignal)Reflect.apply(EventTarget.prototype.addEventListener,originalSignal,["abort",retire,{once:true}]) + let fixedRegistrySql:readonly string[]=[] + const query=async(text:string,values?:unknown[])=>{ + live() + if(!connection||![PUBLIC_PID_SQL,PUBLIC_LOCK_SQL,PUBLIC_HELD_SQL,...fixedRegistrySql].includes(text))refused() + const result=await connection!.query(text,values,{signal:controller.signal,timeoutMs:Math.max(1,Math.ceil(Math.min(5000,end-performance.now(),deadlineAt-Date.now())))}) + live();if(!result||!("command"in result)||result.command!=="SELECT"||!Array.isArray(result.rows))refused() + return result + } + const singleton=(rows:Record[],names:readonly string[])=>{if(rows.length!==1)refused();const fields=publicDataFields(rows[0],names);if(Object.keys(fields).length!==names.length)refused();return fields} + const held=async()=>{ + const fields=singleton((await query(PUBLIC_HELD_SQL)).rows,["backend_pid","held"]) + if(fields.backend_pid.value!==pid||fields.held.value!==true)refused() + } + const current=async(signed:ReturnType)=>{ + live();const value=await publicMaterial(live),metadata=publicNodeMetadata(signed,value),fingerprint=canonical(metadata) + live();if(pin!==undefined&&pin!==fingerprint)refused();pin??=fingerprint + return {value,metadata} + } + const identity=async(value:PublicMaterial,metadata:BandwidthPublicNodeBootstrapMetadata)=>{ + const record=value.record,bootstrap=metadata.bootstrap + await assertBandwidthControlRegistryIdentity({query},{databaseName:name,runtimeRole:runtime,ownerRole:record.ownerRole,applicationRole:appRole,databaseOid:record.databaseOid,installationId:record.installationId,controlGeneration:record.controlGeneration,sourceRecordSha256:value.recordHash,eventKeySha256:record.eventKeySha256,catalogSha256:record.catalogSha256}, + {databaseName:name,runtimeRole:runtime,ownerRole:record.ownerRole,installationId:bootstrap.installationId,issuerKeySha256:bootstrap.issuerKeySha256,quiescenceProof:bootstrap.quiescenceProof,registryCatalogSha256:bootstrap.registryCatalogSha256},bootstrap.format,live) + } + const inspect=async(rawEnvelope:unknown)=>{ + try{ + live();if(busy)refused();busy=true + const signed=capturePublicEnvelope(rawEnvelope) + return await Promise.race([stopped,(async()=>{ + const before=await current(signed) + await held() + await identity(before.value,before.metadata) + await held();const after=await current(signed);live() + return Object.freeze({format:"mmapp-bandwidth-public-registry-metadata-v1" as const,metadata:after.metadata,backendPid:pid!}) + })()]) + }catch{retire();throw new BandwidthControlSourceError()} + finally{busy=false} + } + return await Promise.race([stopped,(async()=>{ + live() + // Only this new public lease needs the fixed SQL constants. Existing + // strong APIs retain their original static module dependency surface. + const sql=await import("./bandwidth-control-registry-identity.js");live() + fixedRegistrySql=Object.freeze([sql.BANDWIDTH_CONTROL_NODE_IDENTITY_SQL,sql.BANDWIDTH_CONTROL_PEER_IDENTITY_SQL]) + const first=await current(originalSigned) + connection=await connector(scope) + if(!active){release();refused()} + live() + const backend=singleton((await query(PUBLIC_PID_SQL)).rows,["backend_pid"]) + if(typeof backend.backend_pid.value!=="number"||!Number.isSafeInteger(backend.backend_pid.value)||backend.backend_pid.value<1||backend.backend_pid.value>2147483647)refused() + pid=backend.backend_pid.value + // Authenticate current role/source/catalog before acquiring a session + // lock; signed public expectations alone never admit a raw DB role. + await identity(first.value,first.metadata) + await current(originalSigned) + const lock=singleton((await query(PUBLIC_LOCK_SQL)).rows,["acquired"]) + if(lock.acquired.value!==true)refused() + await held();await current(originalSigned);live() + return Object.freeze({inspect,close:retire}) + })()]) + }catch{retire();throw new BandwidthControlSourceError()} + } + async function identity(connection:ProbeStorageConnection,value:Material,scope:BandwidthScope,live:()=>void){ + const sql={query:async(text:string,values?:unknown[])=>{live();const result=await connection.query(text,values,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))});live();return result}} + const record=value.record + await assertBandwidthControlIdentity(sql,{databaseName:name,runtimeRole:runtime,ownerRole:record.ownerRole,applicationRole:appRole, + databaseOid:record.databaseOid,installationId:record.installationId,controlGeneration:record.controlGeneration, + sourceRecordSha256:value.recordHash,eventKeySha256:record.eventKeySha256,catalogSha256:record.catalogSha256},live) + live() + } + async function connect(input:BandwidthScope,dispatchGuard?:(fence:BandwidthControlDispatchFence)=>Promise):Promise{ + if(dispatchGuard!==undefined&&typeof dispatchGuard!=="function")refused() + if(!input||!Number.isSafeInteger(input.deadlineAt)||input.signal!==undefined&&!(input.signal instanceof AbortSignal))refused() + const scope=Object.freeze({signal:input.signal,deadlineAt:Math.min(input.deadlineAt,Date.now()+10000)}) + let active=true,released=false,busy=false,connection:ProbeStorageConnection|undefined,owned:ProbeStorageConnection|undefined,pinnedRecordHash:string|undefined,stop!:()=>void + const close=(unsafe:boolean)=>{active=false;if(owned)verifiedHashes.delete(owned);if(connection&&!released){released=true;try{connection.release(unsafe)}catch{/* No unsafe pool reuse. */}}} + const live=()=>{if(closed||!active||connection?.lifetimeSignal?.aborted||scope.signal?.aborted||Date.now()>=scope.deadlineAt)refused()} + let rejectStopped!:(error:BandwidthControlSourceError)=>void + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject;stop=()=>retire(true)}) + const timer=setTimeout(stop,Math.max(1,scope.deadlineAt-Date.now()));scope.signal?.addEventListener("abort",stop,{once:true}) + function retire(unsafe:boolean){clearTimeout(timer);scope.signal?.removeEventListener("abort",stop);close(unsafe);rejectStopped(new BandwidthControlSourceError())} + const checked=async()=>{ + const value=await material(live) + try{if(!connection||pinnedRecordHash!==undefined&&value.recordHash!==pinnedRecordHash)refused();await identity(connection!,value,scope,live);pinnedRecordHash??=value.recordHash;return {recordHash:value.recordHash}} + finally{value.eventKey.fill(0)} + } + const fence=async()=>{ + if(!dispatchGuard)return + let running=true,using=false + const valid=()=>{live();if(!running)refused()} + const exclusive=async(work:()=>Promise)=>{valid();if(using){retire(true);refused()};using=true;try{const result=await work();valid();return result}finally{using=false}} + const context:BandwidthControlDispatchFence=Object.freeze({ + query:(text:string,values?:unknown[])=>exclusive(async()=>{const result=await connection!.query(text,values,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))});valid();return result}), + verifyNodeBootstrap:(envelope:unknown)=>{ + const signed=bandwidthNodeBootstrapEnvelopeSchema.parse(envelope) + return exclusive(async()=>{const value=await material(valid);try{if(value.recordHash!==pinnedRecordHash)refused();return verifyNodeRecord(signed,value)}finally{value.eventKey.fill(0)}}) + }, + sourceIdentity:()=>exclusive(async()=>{ + const value=await material(valid) + try{ + if(value.recordHash!==pinnedRecordHash)refused() + return Object.freeze({installationId:value.record.installationId,controlGeneration:value.record.controlGeneration,sourceRecordSha256:value.recordHash}) + }finally{value.eventKey.fill(0)} + }), + }) + try{await dispatchGuard(context);valid();if(using)refused()} + finally{running=false;if(using)retire(true)} + } + try{return await Promise.race([Promise.resolve().then(async()=>{ + live();const first=await material(live);pinnedRecordHash=first.recordHash;first.eventKey.fill(0) + connection=await connector(scope);if(!active){close(true);refused()} + const initial=await checked() + const lifetimeSignal=connection.lifetimeSignal + owned=Object.freeze({ + ...(lifetimeSignal?{lifetimeSignal}:{}), + async query(text:string,values?:unknown[],options?:{signal?:AbortSignal;timeoutMs:number}){ + if(busy) {retire(true);refused()};busy=true + const work=async()=>{ + try{live();await checked();await fence();live();const result=await connection!.query(text,values,options);live();const current=await checked();await fence();live();verifiedHashes.set(owned!,current.recordHash);return result} + catch{retire(true);throw new BandwidthControlSourceError()} + finally{busy=false} + } + return Promise.race([work(),stopped]) + }, + release(unsafe=false){retire(unsafe||busy)}, + }) + verifiedHashes.set(owned!,initial.recordHash);return owned! + }),stopped])}catch{clearTimeout(timer);scope.signal?.removeEventListener("abort",stop);close(true);throw new BandwidthControlSourceError()} + } + async function connectRegistry(input:BandwidthScope,dispatchGuard:(fence:BandwidthControlRegistryDispatchFence)=>Promise):Promise{ + if(typeof dispatchGuard!=="function")refused() + if(!input||!Number.isSafeInteger(input.deadlineAt)||input.signal!==undefined&&!(input.signal instanceof AbortSignal))refused() + const scope=Object.freeze({signal:input.signal,deadlineAt:Math.min(input.deadlineAt,Date.now()+10000)}) + let active=true,released=false,busy=false,connection:ProbeStorageConnection|undefined,owned:ProbeStorageConnection|undefined,pinnedRecordHash:string|undefined,stop!:()=>void + const close=(unsafe:boolean)=>{active=false;if(owned)verifiedHashes.delete(owned);if(connection&&!released){released=true;try{connection.release(unsafe)}catch{/* No unsafe pool reuse. */}}} + const live=()=>{if(closed||!active||connection?.lifetimeSignal?.aborted||scope.signal?.aborted||Date.now()>=scope.deadlineAt)refused()} + let rejectStopped!:(error:BandwidthControlSourceError)=>void + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject;stop=()=>retire(true)}) + const timer=setTimeout(stop,Math.max(1,scope.deadlineAt-Date.now()));scope.signal?.addEventListener("abort",stop,{once:true}) + function retire(unsafe:boolean){clearTimeout(timer);scope.signal?.removeEventListener("abort",stop);close(unsafe);rejectStopped(new BandwidthControlSourceError())} + const checked=async()=>{ + const value=await material(live) + try{if(!connection||pinnedRecordHash!==undefined&&value.recordHash!==pinnedRecordHash)refused();await identity(connection!,value,scope,live);pinnedRecordHash??=value.recordHash;return {recordHash:value.recordHash}} + finally{value.eventKey.fill(0)} + } + const fence=async()=>{ + let running=true,using=false,verification=0,checkedVerification=0,sqlSequence=0 + let latest: {verified:BandwidthVerifiedNodeBootstrap;hash:string;recordHash:string;sqlSequence:number}|undefined,checked:{hash:string;recordHash:string}|undefined + const genuine=new WeakMap() + const valid=()=>{live();if(!running)refused()} + const exclusive=async(work:()=>Promise)=>{valid();if(using){retire(true);refused()};using=true;try{const result=await work();valid();return result}finally{using=false}} + const context:BandwidthControlRegistryDispatchFence=Object.freeze({ + query:(text:string,values?:unknown[])=>exclusive(async()=>{sqlSequence++;const result=await connection!.query(text,values,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))});valid();return result}), + verifyNodeBootstrap:(envelope:unknown)=>{ + const signed=bandwidthNodeBootstrapEnvelopeSchema.parse(envelope) + return exclusive(async()=>{const value=await material(valid);try{if(value.recordHash!==pinnedRecordHash)refused() + const verified=verifyNodeRecord(signed,value),record=value.record,bootstrapHash=digest(canonicalBandwidthNodeBootstrap(verified.bootstrap)) + const captured={core:{databaseName:name,runtimeRole:runtime,ownerRole:record.ownerRole,applicationRole:appRole,databaseOid:record.databaseOid,installationId:record.installationId,controlGeneration:record.controlGeneration,sourceRecordSha256:value.recordHash,eventKeySha256:record.eventKeySha256,catalogSha256:record.catalogSha256},hash:bootstrapHash,recordHash:value.recordHash,verification:++verification} + genuine.set(verified,captured);latest={verified,hash:bootstrapHash,recordHash:value.recordHash,sqlSequence};return verified}finally{value.eventKey.fill(0)}}) + }, + assertRegistryIdentity:(verified:BandwidthVerifiedNodeBootstrap)=>exclusive(async()=>{ + const captured=verified&&typeof verified==="object"?genuine.get(verified):undefined + if(!captured||latest?.verified!==verified||captured.verification!==verification||checked)refused() + const bootstrap=verified.bootstrap + const expected={databaseName:captured!.core.databaseName,runtimeRole:captured!.core.runtimeRole,ownerRole:captured!.core.ownerRole,installationId:bootstrap.installationId,issuerKeySha256:bootstrap.issuerKeySha256,quiescenceProof:bootstrap.quiescenceProof,registryCatalogSha256:bootstrap.registryCatalogSha256} + const sql={query:async(text:string,values?:unknown[])=>{valid();const result=await connection!.query(text,values,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))});valid();return result}} + sqlSequence++;await assertBandwidthControlRegistryIdentity(sql,captured!.core,expected,bootstrap.format,valid) + valid();checked={hash:captured!.hash,recordHash:captured!.recordHash};checkedVerification=captured!.verification + }), + sourceIdentity:()=>exclusive(async()=>{ + const value=await material(valid) + try{ + if(value.recordHash!==pinnedRecordHash)refused() + return Object.freeze({installationId:value.record.installationId,controlGeneration:value.record.controlGeneration,sourceRecordSha256:value.recordHash}) + }finally{value.eventKey.fill(0)} + }), + }) + try{await dispatchGuard(context);valid() + if(using||!checked||!latest||verification<=checkedVerification||latest.sqlSequence!==sqlSequence||latest.hash!==checked.hash||latest.recordHash!==checked.recordHash||latest.recordHash!==pinnedRecordHash)refused() + return {recordHash:latest!.recordHash} + } + finally{running=false;if(using)retire(true)} + } + try{return await Promise.race([Promise.resolve().then(async()=>{ + live();const first=await material(live);pinnedRecordHash=first.recordHash;first.eventKey.fill(0) + connection=await connector(scope);if(!active){close(true);refused()} + const initial=await checked() + const lifetimeSignal=connection.lifetimeSignal + owned=Object.freeze({ + ...(lifetimeSignal?{lifetimeSignal}:{}), + async query(text:string,values?:unknown[],options?:{signal?:AbortSignal;timeoutMs:number}){ + if(busy) {retire(true);refused()};busy=true + const work=async()=>{ + try{live();await fence();live();const result=await connection!.query(text,values,options);live();const current=await fence();live();verifiedHashes.set(owned!,current.recordHash);return result} + catch{retire(true);throw new BandwidthControlSourceError()} + finally{busy=false} + } + return Promise.race([work(),stopped]) + }, + release(unsafe=false){retire(unsafe||busy)}, + }) + verifiedHashes.set(owned!,initial.recordHash);return owned! + }),stopped])}catch{clearTimeout(timer);scope.signal?.removeEventListener("abort",stop);close(true);throw new BandwidthControlSourceError()} + } + async function withSource(scope:BandwidthScope,work:(value:Material)=>T):Promise{ + const connection=await connect(scope) + try{const live=()=>{if(closed||connection.lifetimeSignal?.aborted||scope.signal?.aborted||Date.now()>=scope.deadlineAt)refused()};const value=await material(live);try{live();if(verifiedHashes.get(connection)!==value.recordHash)refused();const result=work(value);live();return result}finally{value.eventKey.fill(0)}}finally{connection.release()} + } + async function eventProofOnConnection(event:unknown,connection:ProbeStorageConnection,input:BandwidthScope){ + try{ + // Capture the event before any await: caller mutation must not change the + // authenticated event after the operation has begun. + const text=JSON.stringify(event) + if(typeof text!=="string"||Buffer.byteLength(text)>65536)refused() + const snapshot=canonical(JSON.parse(text)) + if(!input||!Number.isSafeInteger(input.deadlineAt)||input.signal!==undefined&&!(input.signal instanceof AbortSignal))refused() + const scope=Object.freeze({deadlineAt:input.deadlineAt,signal:input.signal}) + const live=()=>{if(closed||!verifiedHashes.has(connection)||connection.lifetimeSignal?.aborted||scope.signal?.aborted||Date.now()>=scope.deadlineAt)refused()} + live() + await connection.query("SELECT 1",undefined,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))}) + live() + const value=await material(live) + try{ + if(verifiedHashes.get(connection)!==value.recordHash)refused() + live() + return createHmac("sha256",value.eventKey).update("bandwidth-control-event-v1:"+snapshot).digest("hex") + }finally{value.eventKey.fill(0)} + }catch{throw new BandwidthControlSourceError()} + } + return Object.freeze({connect,connectRegistry,eventProofOnConnection,inspectPublicNodeBootstrap,openPublicRegistryMetadata, + /** Domain-separated admission proofs, including the existing 8 MiB native + * terminal envelope. The expected original source record is never adopted + * from a later valid generation. This is server-owned broker input only. */ + async admissionProof(purpose:string,value:unknown,expectedSourceRecordSha256:string,scope:BandwidthScope){ + try{ + if(!["bandwidth-request-v1","bandwidth-command-v1","bandwidth-binding-v1","bandwidth-terminal-v1","bandwidth-endpoint-v1"].includes(purpose)||!hash.safeParse(expectedSourceRecordSha256).success)refused() + const text=JSON.stringify(value) + if(typeof text!=="string"||Buffer.byteLength(text)>9*1048576)refused() + const snapshot=canonical(JSON.parse(text)) + return await withSource(scope,material=>{ + if(material.recordHash!==expectedSourceRecordSha256)refused() + return createHmac("sha256",material.eventKey).update("mmapp-bandwidth-admission-v1:"+purpose+":"+snapshot).digest("hex") + }) + }catch{throw new BandwidthControlSourceError()} + }, + /** Verify the fixed node bootstrap on THIS active physical source lease. + * No nested checkout: a registry transaction and its key/authority guards + * must not exhaust the pool by recursively borrowing additional clients. + * Neither a JSON lease nor a released/foreign source connection is valid. */ + async verifyNodeBootstrap(envelope:unknown,connection:ProbeStorageConnection,input:BandwidthScope){ + const scope=Object.freeze({deadlineAt:input.deadlineAt,signal:input.signal}) + const live=()=>{if(closed||!verifiedHashes.has(connection)||connection.lifetimeSignal?.aborted||!Number.isSafeInteger(scope.deadlineAt)||scope.signal?.aborted||Date.now()>=scope.deadlineAt)refused()} + try{ + const signed=bandwidthNodeBootstrapEnvelopeSchema.parse(envelope) + live();await connection.query("SELECT 1",undefined,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))});live() + const value=await material(live) + try{ + if(verifiedHashes.get(connection)!==value.recordHash)refused() + live();return verifyNodeRecord(signed,value) + }finally{value.eventKey.fill(0)} + }catch{throw new BandwidthControlSourceError()} + }, + async eventProof(event:unknown,scope:BandwidthScope){try{const text=JSON.stringify(event);if(typeof text!=="string"||Buffer.byteLength(text)>65536)refused();const snapshot=canonical(JSON.parse(text));return await withSource(scope,value=>createHmac("sha256",value.eventKey).update("bandwidth-control-event-v1:"+snapshot).digest("hex"))}catch{throw new BandwidthControlSourceError()}}, + identity:(scope:BandwidthScope)=>withSource(scope,value=>Object.freeze({installationId:value.record.installationId,controlGeneration:value.record.controlGeneration,sourceAuthenticated:true as const,applicationSchemaReady:false as const})), + close(){closed=true;for(const stop of publicStops)stop();closing??=pool?pool.end():Promise.resolve();return closing}, + }) +} + +let productionSource:ReturnType|undefined +/** Fixed lazy server assembly. No caller configuration or automatic bootstrap; + * absence affects the caller requesting this source, not registry imports. */ +export function getProductionBandwidthControlSource(){ + if(productionSource)return productionSource + const required=(name:string)=>{const value=process.env[name];if(!value?.trim())throw new BandwidthControlSourceError();return value} + productionSource=createConfiguredBandwidthControlSource({databaseUrl:required("MMAPP_BANDWIDTH_CONTROL_URL"),applicationDatabaseUrl:required("DATABASE_URL"), + attestationFile:required("MMAPP_BANDWIDTH_CONTROL_ATTESTATION_FILE"),trustKeyFile:required("MMAPP_BANDWIDTH_CONTROL_TRUST_KEY_FILE"), + trustKeySha256:required("MMAPP_BANDWIDTH_CONTROL_TRUST_KEY_SHA256"),eventKeyFile:required("MMAPP_BANDWIDTH_CONTROL_EVENT_KEY_FILE")}) + return productionSource +} +export async function closeProductionBandwidthControlSource(){await productionSource?.close()} diff --git a/backend/src/services/bandwidth-execution-host.sql-helper.ts b/backend/src/services/bandwidth-execution-host.sql-helper.ts new file mode 100644 index 00000000..363fd4ef --- /dev/null +++ b/backend/src/services/bandwidth-execution-host.sql-helper.ts @@ -0,0 +1,238 @@ +/** Real owned PostgreSQL -> configured sources -> registry -> broker -> HTTP. + * Operator grants, license claims and remote-address approval are explicitly + * synthetic fixture authority. This is not production enrollment/readiness. */ +import assert from "node:assert/strict" +import { createHmac, randomBytes, randomUUID, createHash } from "node:crypto" +import { createServer, type ServerResponse } from "node:http" +import { once } from "node:events" +import { setTimeout as delay } from "node:timers/promises" +import type { Pool } from "pg" +import type { createConfiguredBandwidthControlSource } from "./bandwidth-control-source.js" +import type { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" +import type { BandwidthNodeSourceSnapshot } from "./bandwidth-node-observation.js" +import type { BandwidthExecutionIntent } from "./bandwidth-execution-host.js" +import { assertOwnedBandwidthSql, scope } from "./bandwidth-admission-process.sql-helper.js" + +type Core = ReturnType +type Nodes = ReturnType +type Row = { id: number; host: string; port: number; use_ssl: boolean; verify_ssl: boolean; username: string; password: string; routing_endpoint_epoch: string; enabled: boolean } +type Grant = { intent: BandwidthExecutionIntent; rows: readonly Row[]; allowed: Set; remoteAllowed: boolean; retireRemote: boolean } +const tableNames = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events", "bandwidth_source_identity", "bandwidth_node_authority", "bandwidth_nodes", "bandwidth_node_aliases", "bandwidth_node_events"] as const + +export async function acceptBandwidthExecutionHost(core: Core, nodeSource: Nodes, owner: Pool) { + assertOwnedBandwidthSql() + // Imports that allocate/use the default application pool follow the guard. + const { pool: admin } = await import("../db/index.js") + const { applySqlMigrations } = await import("../db/migrate.js") + const { createBandwidthNodeObservationReader } = await import("./bandwidth-node-observation.js") + const { createBandwidthNodeRegistry } = await import("./bandwidth-node-registry.js") + const { createBandwidthExecutionHost, BandwidthExecutionHostError } = await import("./bandwidth-execution-host.js") + await applySqlMigrations(admin) + const key = randomBytes(32), ownIds = new Set(), claims = new Map(), grants = new WeakMap() + const mac = (purpose: string, value: unknown) => createHmac("sha256", key).update("owned-broker-" + purpose + ":" + JSON.stringify(value)).digest("hex") + const checks: string[] = [] + let posts = 0, gets = 0, credentialReads = 0, credentialId = -1 + let mode: "terminal" | "drop" | "nonterminal" = "terminal", pendingResponse: ServerResponse | undefined + let notifyPost: (() => void) | undefined, lastBody: unknown, postAt = 0 + const licenseReads=new Map() + let changedLicense: {id:number;at:number}|undefined + const server = createServer(async (req, res) => { + try { + const auth = Buffer.from((req.headers.authorization ?? "").slice(6), "base64").toString() + const match = /^broker-(\d+):/.exec(auth), id = Number(match?.[1]) + assert(ownIds.has(id)); assert.equal(auth, `broker-${id}:owned-localhost-secret`) + res.setHeader("content-type", "application/json") + if (req.method === "GET") { + assert.equal(req.url, "/rest/system/license"); assert(claims.has(id)); assert.notEqual(id, credentialId) + gets++;const count=(licenseReads.get(id)??0)+1;licenseReads.set(id,count) + const claim=claims.get(id)+(changedLicense?.id===id&&count>=changedLicense.at?"-CHANGED-AFTER-ORIGINAL-PHASE":"") + res.end(JSON.stringify({ "system-id": claim })); return + } + assert.equal(req.method, "POST"); assert.equal(req.url, "/rest/tool/bandwidth-test") + let text = ""; for await (const chunk of req) { text += String(chunk); assert(Buffer.byteLength(text) <= 65536) } + lastBody = JSON.parse(text); posts++; postAt = Date.now() + if (mode === "drop") { res.destroy(); notifyPost?.(); return } + if (mode === "nonterminal") { res.end(JSON.stringify([{ status: "running" }])); notifyPost?.(); return } + pendingResponse = res; notifyPost?.() + } catch { res.destroy() } + }) + server.listen(0, "127.0.0.1"); await once(server, "listening") + const address = server.address(); assert(address && typeof address !== "string"); const port = address.port + const live = (current: { deadlineAt: number; signal?: AbortSignal }) => { current.signal?.throwIfAborted(); assert(Date.now() < current.deadlineAt) } + const readRow = async (id: number): Promise => { + assert(ownIds.has(id)) + if (id === credentialId) credentialReads++ + const result = await admin.query("SELECT id,host,port,use_ssl,verify_ssl,username,password,routing_endpoint_epoch::text,enabled FROM public.servers WHERE id=$1", [id]) + assert.equal(result.rows.length, 1) + return result.rows[0] as Row + } + const reader = createBandwidthNodeObservationReader({ async read(id, current) { + live(current); assert(ownIds.has(id)); assert.notEqual(id, credentialId) + const row = await readRow(id); live(current) + const value: BandwidthNodeSourceSnapshot = { serverId: id, enabled: row.enabled, sourceProof: mac("source", row), authorityGeneration: "owned-broker-actor-v1", + endpoint: { host: row.host, port: row.port, useSsl: row.use_ssl, verifySsl: row.verify_ssl, username: row.username, password: row.password, routingEndpointEpoch: row.routing_endpoint_epoch } } + return value + } }) + const snapshot = async () => { + const result = [] + for (const table of tableNames) result.push((await owner.query(`SELECT to_jsonb(t) AS row FROM mmapp_control.${table} t ORDER BY to_jsonb(t)::text`)).rows) + return createHash("sha256").update(JSON.stringify(result)).digest("hex") + } + const allocate = async (claim?: string) => { + // Two owned application fixture databases have independent serial + // sequences but share this canonical registry. Keep their test aliases + // disjoint; changing a claim on an existing alias must still be refused. + const externalMax=Number((await owner.query("SELECT coalesce(max(server_id),0)::pg_catalog.text AS last FROM mmapp_control.bandwidth_node_aliases")).rows[0].last) + const applicationMax=Number((await admin.query("SELECT coalesce(max(id),0)::pg_catalog.text AS last FROM public.servers")).rows[0].last) + const nextId=Math.max(externalMax,applicationMax)+100000 + assert(Number.isSafeInteger(nextId)&&nextId>0) + const row = (await admin.query("INSERT INTO public.servers(id,name,host,port,use_ssl,verify_ssl,password,enabled) VALUES($1,'owned-bandwidth-broker','127.0.0.1',$2,false,true,'owned-localhost-secret',true) RETURNING id", [nextId,port])).rows[0] + const id = Number(row.id); ownIds.add(id) + await admin.query("UPDATE public.servers SET username=$2 WHERE id=$1", [id, "broker-" + id]) + if (claim) claims.set(id, claim) + return id + } + const enroll = async (id: number) => { + const current = scope(10000), token = await reader.observe(id, current), observed = await reader.inspect(token, current) + const guard = async () => { assert.deepEqual(await reader.inspect(token, current), observed); live(current) } + const session = await nodeSource.open(current, guard) + try { + const binding = await session.binding(), expiry = Date.now() + 30000 + const registry = createBandwidthNodeRegistry({ now: Date.now, connect: async () => session.connection, + inspect: (value, window) => reader.inspect(value, window), keyedProof: (purpose, value) => session.keyedProof(purpose, value), keyedProofs: inputs => session.keyedProofs(inputs), + authorize: async (action, serverId, window, evidence) => { + assert.equal(action, "register"); assert.equal(serverId, id); assert.deepEqual(evidence, observed) + await guard(); live(window); assert(Date.now() < expiry) + return { installationId: binding.installationId, issuerKeySha256: binding.issuerKeySha256, quiescenceProof: binding.quiescenceProof, + authorityProof: mac("SYNTHETIC-operator-enrollment", { id, claims: observed.claims }), authorityGeneration: "synthetic-owned-operator-v1", expiresAt: expiry } + } }) + assert.equal((await registry.register(token, current)).status, "active") + } finally { session.close() } + } + const retire = async (id: number) => { + const current = scope(10000), session = await nodeSource.open(current) + try { + const binding = await session.binding(), expiry = Date.now() + 10000 + const registry = createBandwidthNodeRegistry({ now: Date.now, connect: async () => session.connection, + inspect: (value, window) => reader.inspect(value, window), keyedProof: (purpose, value) => session.keyedProof(purpose, value), keyedProofs: inputs => session.keyedProofs(inputs), + authorize: async (action, serverId, window, evidence) => { + assert.equal(action, "tombstone"); assert.equal(serverId, id); assert.equal(evidence, undefined); live(window) + return { installationId: binding.installationId, issuerKeySha256: binding.issuerKeySha256, quiescenceProof: binding.quiescenceProof, + authorityProof: mac("SYNTHETIC-operator-retirement", { id }), authorityGeneration: "synthetic-owned-operator-v1", expiresAt: expiry } + } }) + assert.equal((await registry.retire(id, "tombstone", current)).status, "tombstoned") + } finally { session.close() } + } + const context = async (sourceServerId: number, actualRemoteServerId: number) => { + const rows = await Promise.all([sourceServerId, actualRemoteServerId, credentialId].map(readRow)), expiresAt = Date.now() + 60000 + const intent: BandwidthExecutionIntent = { attemptId: randomUUID(), operationId: randomUUID(), callerKind: "rest-diagnostic", sourceServerId, actualRemoteServerId, credentialServerId: credentialId, + request: { fixture: "synthetic-owned-broker", address: "198.51.100.42@fixture-vrf" }, durationSec: 2147483647, socketTimeoutMs: 30000, + nativeBody: { address: "198.51.100.42@fixture-vrf", user: rows[2].username, password: rows[2].password, protocol: "tcp", direction: "both", duration: "2147483647s" }, + authorityProof: mac("sealed-original", { sourceServerId, actualRemoteServerId, credentialId, rows, expiresAt }), authorityGeneration: "owned-broker-actor-v1", expiresAt } + const opaque = Object.freeze(Object.create(null)) as object + const grant: Grant = { intent, rows, allowed: new Set([sourceServerId, actualRemoteServerId, credentialId]), remoteAllowed: true, retireRemote: false } + grants.set(opaque, grant); return { opaque, grant } + } + const host = createBandwidthExecutionHost({ coreSource: core, nodeSource, observer: reader, + readOriginal: async (opaque, current) => { + const grant = opaque && typeof opaque === "object" ? grants.get(opaque) : undefined + assert(grant); live(current); assert(Date.now() < grant.intent.expiresAt) + // Authorize every ID before reading ANY credentials, especially the third. + for (const id of [grant.intent.sourceServerId, grant.intent.actualRemoteServerId, grant.intent.credentialServerId]) assert(grant.allowed.has(id)) + // One bounded, scoped snapshot for all three already-authorized IDs. + // It reads the same original fields without three separate pool checkouts. + const ids=grant.rows.map(row=>row.id) + credentialReads++ + const selected=(await admin.query("SELECT id,host,port,use_ssl,verify_ssl,username,password,routing_endpoint_epoch::text,enabled FROM public.servers WHERE id=ANY($1::bigint[])",[ids])).rows as Row[] + assert.equal(selected.length,ids.length) + const byId=new Map(selected.map(row=>[Number(row.id),row])) + const fresh=ids.map(id=>byId.get(id));live(current) + assert.deepEqual(fresh, grant.rows) + return structuredClone(grant.intent) + }, + verifyRemote: async (opaque, intent, evidence, current) => { + const grant = opaque && typeof opaque === "object" ? grants.get(opaque) : undefined + assert(grant); live(current); assert(grant.remoteAllowed); assert.deepEqual(intent, grant.intent) + assert.equal(intent.nativeBody.address, "198.51.100.42@fixture-vrf") + assert.equal(evidence.remoteReceipt.serverId, intent.actualRemoteServerId); assert.notEqual(intent.actualRemoteServerId, intent.credentialServerId) + assert.equal(evidence.remoteEvidence.serverId, intent.actualRemoteServerId) + if (grant.retireRemote) { grant.retireRemote = false; await retire(intent.actualRemoteServerId) } + live(current) + return { proof: mac("SYNTHETIC-exact-address-VRF-policy", { address: intent.nativeBody.address, actualRemote: evidence.remoteReceipt, credentialId: intent.credentialServerId }), expiresAt: intent.expiresAt } + } }) + const execute = async(value: { opaque: object }) => { + const began=Date.now() + try{return await host.execute(value.opaque,scope(60000))} + catch(error){console.error("Owned broker refusal timing:",JSON.stringify({elapsedMs:Date.now()-began,phase:error instanceof BandwidthExecutionHostError?error.phase:"dependency",posts}));throw error} + } + const unknown = async (attemptId: string) => { + assert.equal((await owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [attemptId])).rows[0]?.state, "unknown_busy") + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1", [attemptId])).rows[0].n), 2) + } + try { + const sourceId = await allocate("BROKER-SOURCE-" + randomUUID()), remoteId = await allocate("BROKER-REMOTE-" + randomUUID()) + credentialId = await allocate() + const {acceptBandwidthServiceContextSql}=await import("../modules/mcp/bandwidth-service-context.sql-helper.js") + const serviceContextAcceptance=await acceptBandwidthServiceContextSql([sourceId,remoteId,credentialId]) + await enroll(sourceId); await enroll(remoteId) + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_aliases WHERE server_id=$1", [credentialId])).rows[0].n), 0) + const denied = await context(sourceId, remoteId), baseline = await snapshot(), beforeReads = credentialReads + denied.grant.allowed.delete(credentialId) + await assert.rejects(execute(denied), error => error instanceof Error && "code" in error && error.code === "REFUSED") + assert.equal(credentialReads, beforeReads); assert.equal(posts, 0); assert.equal(await snapshot(), baseline) + checks.push("third credential-provider ID denied before any credential fetch, native POST or control mutation") + const policyDenied = await context(sourceId, remoteId); policyDenied.grant.remoteAllowed = false + await assert.rejects(execute(policyDenied), error => error instanceof Error && "code" in error && error.code === "REFUSED") + assert.equal(posts, 0); assert.equal(await snapshot(), baseline) + checks.push("exact address/VRF remote policy denial preserves control ledger and sends zero POST") + const changedSubject=await context(sourceId,remoteId) + changedLicense={id:sourceId,at:(licenseReads.get(sourceId)??0)+2} + try{ + await assert.rejects(execute(changedSubject),error=>error instanceof BandwidthExecutionHostError&&error.code==="REFUSED"&&error.phase==="source-resolution") + assert.equal(posts,0);assert.equal(await snapshot(),baseline) + }finally{changedLicense=undefined} + checks.push("a NEW genuine license observation with changed original claims is refused between phases before reserve/native; no token renewal, alias adoption or control mutation") + + const success = await context(sourceId, remoteId) + const entered = new Promise(resolve => { notifyPost = resolve }) + const running = execute(success); void running.catch(() => {}) + await Promise.race([entered, running.then(() => assert.fail("Completed before native response"))]) + // Every admission lease must be returned before awaiting a long native call. + const borrowed = await Promise.allSettled(Array.from({ length: 4 }, () => core.connect(scope(5000)))) + try { + assert(borrowed.every(result => result.status === "fulfilled")) + await Promise.all(borrowed.map(result => result.status === "fulfilled" ? result.value.query("SELECT 1") : Promise.reject(Error("Lease unavailable")))) + } finally { for (const result of borrowed) if (result.status === "fulfilled") result.value.release(true) } + await delay(Math.max(0, 5400 - (Date.now() - postAt))) + assert(pendingResponse); pendingResponse.end(JSON.stringify([{ status: "done testing", "tx-total-average": "1000" }])) + const completed = await running + assert.equal(completed.remoteCompletionConfirmed, true); assert(Date.now() - postAt >= 5300) + assert.deepEqual(lastBody, success.grant.intent.nativeBody); assert.equal(posts, 1) + assert.equal((await owner.query("SELECT state,resolution FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [success.grant.intent.attemptId])).rows[0].state, "released") + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1", [success.grant.intent.attemptId])).rows[0].n), 0) + const audit = (await admin.query("SELECT action FROM public.mcp_audit WHERE payload->>'serverId'=$1", [String(sourceId)])).rows.map(row => row.action) + assert(audit.includes("configuration.intent")); assert(audit.includes("configuration.acknowledged")) + checks.push("real configured broker terminal response beyond 5.3s observation TTL releases both owners without grant renewal; exact max duration/body and actual configuration intent/acknowledged audit; four core leases available during native wait") + + mode = "drop"; const dropped = await context(sourceId, remoteId) + await assert.rejects(execute(dropped)); assert.equal(posts, 2); await unknown(dropped.grant.intent.attemptId) + const again = await context(sourceId, remoteId) + await assert.rejects(execute(again)); assert.equal(posts, 2); await unknown(dropped.grant.intent.attemptId) + checks.push("one dropped physical POST leaves both exact owners unknown-busy; fresh attempt on same pair sends zero additional POST and never retries") + + const otherSource = await allocate("BROKER-OTHER-SOURCE-" + randomUUID()), otherRemote = await allocate("BROKER-OTHER-REMOTE-" + randomUUID()) + await enroll(otherSource); await enroll(otherRemote) + const retired = await context(otherSource, otherRemote); retired.grant.retireRemote = true + await assert.rejects(execute(retired)); assert.equal(posts, 2) + assert.equal((await owner.query("SELECT status FROM mmapp_control.bandwidth_node_aliases WHERE server_id=$1", [otherRemote])).rows[0].status, "tombstoned") + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [retired.grant.intent.attemptId])).rows[0].n), 0) + await unknown(dropped.grant.intent.attemptId) + checks.push("genuine operator tombstone after resolve is caught by same-physical alias fence before reservation/native dispatch; unrelated unknown owners preserved") + return { checks, scenarios: 6, serviceContextAcceptance, actualLocalhostBandwidthPosts: posts, actualLocalhostLicenseGets: gets, productionActivation: false, operatorAuthoritySynthetic: true, remotePolicySynthetic: true, realChrCalls: 0 } + } finally { + reader.close(); server.closeAllConnections() + await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())) + key.fill(0) + if (ownIds.size) await admin.query("DELETE FROM public.servers WHERE id=ANY($1::bigint[])", [[...ownIds]]) + } +} diff --git a/backend/src/services/bandwidth-execution-host.ts b/backend/src/services/bandwidth-execution-host.ts new file mode 100644 index 00000000..30eea79e --- /dev/null +++ b/backend/src/services/bandwidth-execution-host.ts @@ -0,0 +1,269 @@ +/** Trusted internal execution composition only. No enrollment, default source, + * production activation or model-supplied transport/authority callbacks. Raw + * results are for the complete internal workflow, never a public readiness API. */ +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { BANDWIDTH_CALLER_KINDS, createBandwidthAdmission, type BandwidthAdmissionInput, type BandwidthBinding, type BandwidthJournal, type BandwidthScope, type BandwidthTransition } from "./bandwidth-admission.js" +import { createBandwidthAdmissionSqlJournal } from "./bandwidth-admission-storage.js" +import { createBandwidthNodeRegistry, type BandwidthNodeRegistryReceipt } from "./bandwidth-node-registry.js" +import type { createConfiguredBandwidthControlSource, BandwidthControlDispatchFence } from "./bandwidth-control-source.js" +import type { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" +import type { createBandwidthNodeObservationReader, BandwidthNodeObservationEvidence } from "./bandwidth-node-observation.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +type Core = ReturnType +type Nodes = ReturnType +type Observer = ReturnType +type NodeSession = Awaited> +type NodeBinding = Awaited> +const hash = z.string().regex(/^[a-f0-9]{64}$/), label = z.string().regex(/^[a-zA-Z0-9_.:-]{1,200}$/) +const positive = z.number().int().positive().safe() +const intentSchema = z.object({ attemptId: z.string().uuid(), operationId: label, callerKind: z.enum(BANDWIDTH_CALLER_KINDS), + sourceServerId: positive, actualRemoteServerId: positive, credentialServerId: positive, + request: z.record(z.string(), z.unknown()).transform(value => value as BandwidthAdmissionInput["request"]), nativeBody: z.record(z.string(), z.string()), + durationSec: z.number().int().min(3).max(2147483647), socketTimeoutMs: z.number().int().min(1).max(2147483647), + authorityProof: hash, authorityGeneration: label, expiresAt: positive }).strict() +export type BandwidthExecutionIntent = Readonly> +export interface BandwidthExecutionRemoteEvidence { + readonly sourceEvidence: Readonly + readonly remoteEvidence: Readonly + readonly sourceReceipt: Readonly + readonly remoteReceipt: Readonly +} +export interface BandwidthExecutionHostDependencies { + coreSource: Core + nodeSource: Nodes + observer: Observer + /** Fixed server-owned original human/scheduler/MCP context verifier. Every + * read authenticates exact sealed intent AND current rights to ALL THREE IDs, + * current source/destination credentials and original nonrenewed expiry. */ + readOriginal(originalContext: unknown, scope: Readonly): Promise + /** Independently authenticate wire address/VRF/zone against the ACTUAL remote + * canonical receipt. Credential provider identity alone is insufficient. */ + verifyRemote(originalContext: unknown, intent: BandwidthExecutionIntent, evidence: Readonly, scope: Readonly): Promise<{ proof: string; expiresAt: number }> +} +export class BandwidthExecutionHostError extends Error { + readonly automaticReplay = false + constructor(readonly code: "REFUSED" | "OUTCOME_UNKNOWN",readonly phase?:BandwidthExecutionPhase) { super("Bandwidth execution " + code) } +} +type BandwidthExecutionPhase="original-context"|"source-observation"|"remote-observation"|"source-resolution"|"remote-resolution"|"remote-policy"|"participant-proof"|"strict-client"|"reserve-arm"|"native-send"|"terminal-proof"|BandwidthTransition +function refuse(): never { throw new BandwidthExecutionHostError("REFUSED") } +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} +/** Reject getters, serializers, symbols and non-JSON values before any await. */ +function capture(value: unknown, limit = 1048576): unknown { + const seen = new Set() + const walk = (item: unknown, depth: number): unknown => { + if (depth > 32) refuse() + if (item === null || typeof item === "string" || typeof item === "boolean") return item + if (typeof item === "number") { if (!Number.isFinite(item) || Number.isInteger(item) && !Number.isSafeInteger(item)) refuse(); return item } + if (!item || typeof item !== "object" || seen.has(item)) refuse() + seen.add(item) + try { + if (Array.isArray(item)) { + const descriptors = Object.getOwnPropertyDescriptors(item) + if (Reflect.ownKeys(item).length !== item.length + 1 || Object.keys(descriptors).some(key => key !== "length" && (!/^(0|[1-9][0-9]*)$/.test(key) || !("value" in descriptors[key])))) refuse() + return Array.from({ length: item.length }, (_, index) => walk(descriptors[String(index)].value, depth + 1)) + } + if (Object.getPrototypeOf(item) !== Object.prototype) refuse() + const descriptors = Object.getOwnPropertyDescriptors(item) + if (Reflect.ownKeys(item).length !== Object.keys(descriptors).length || Object.values(descriptors).some(entry => !("value" in entry) || !entry.enumerable)) refuse() + return Object.fromEntries(Object.entries(descriptors).map(([key, entry]) => [key, walk(entry.value, depth + 1)])) + } finally { seen.delete(item) } + } + const result = walk(value, 0) + if (Buffer.byteLength(JSON.stringify(result)) > limit) refuse() + return freeze(result) +} +const readIntent = (value: unknown): BandwidthExecutionIntent => { + const parsed = intentSchema.parse(capture(value)) + if (parsed.nativeBody.duration !== `${parsed.durationSec}s` || !parsed.nativeBody.address?.trim()) refuse() + return freeze(parsed) +} +const nodeSubject=(value:Readonly)=>({version:value.version,serverId:value.serverId,sourceProof:value.sourceProof, + authorityGeneration:value.authorityGeneration,routingEndpointEpoch:value.routingEndpointEpoch,claims:value.claims, + physicalUniquenessVerified:value.physicalUniquenessVerified,cloneResistanceVerified:value.cloneResistanceVerified}) + +export function createBandwidthExecutionHost(dependencies: BandwidthExecutionHostDependencies) { + // Capture the trusted assembly once; request values never choose providers. + const readOriginal = dependencies.readOriginal.bind(dependencies), verifyRemote = dependencies.verifyRemote.bind(dependencies) + const core = Object.freeze({ connect: dependencies.coreSource.connect.bind(dependencies.coreSource), + admissionProof: dependencies.coreSource.admissionProof.bind(dependencies.coreSource), eventProof: dependencies.coreSource.eventProofOnConnection.bind(dependencies.coreSource) }) + const nodes = Object.freeze({ open: dependencies.nodeSource.open.bind(dependencies.nodeSource), assertResolvedOnFence: dependencies.nodeSource.assertResolvedOnFence.bind(dependencies.nodeSource) }) + const observer = Object.freeze({ observe: dependencies.observer.observe.bind(dependencies.observer), inspect: dependencies.observer.inspect.bind(dependencies.observer), strictClient: dependencies.observer.strictClient.bind(dependencies.observer) }) + return Object.freeze({ async execute(originalContext: unknown, inputScope: BandwidthScope) { + if (!inputScope || !Number.isSafeInteger(inputScope.deadlineAt) || inputScope.signal !== undefined && !(inputScope.signal instanceof AbortSignal)) refuse() + const entered = Date.now(), enteredMonotonic=performance.now(), controller = new AbortController() + let monotonicEnd=enteredMonotonic+Math.max(0,inputScope.deadlineAt-entered),mutationPossible=false + let phase:BandwidthExecutionPhase="original-context" + const parentDeadline = inputScope.deadlineAt, parentSignal = inputScope.signal + let running = true, intent: BandwidthExecutionIntent | undefined, timer: ReturnType | undefined + const abort = () => controller.abort() + parentSignal?.addEventListener("abort", abort, { once: true }); if (parentSignal?.aborted) abort() + const scope: BandwidthScope = Object.freeze({ deadlineAt: parentDeadline, signal: controller.signal }) + let rejectStopped!: (error: BandwidthExecutionHostError) => void + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + const stop = () => { if (!running) return; controller.abort(); rejectStopped(new BandwidthExecutionHostError(mutationPossible?"OUTCOME_UNKNOWN":"REFUSED")) } + controller.signal.addEventListener("abort", stop, { once: true }) + const live = (current: BandwidthScope = scope) => { + if (!running || current.signal?.aborted || controller.signal.aborted || Date.now() >= Math.min(current.deadlineAt, intent?.expiresAt ?? Infinity) || performance.now() >= monotonicEnd) refuse() + } + const schedule = () => { + clearTimeout(timer) + const remaining = Math.min(Math.min(parentDeadline, intent?.expiresAt ?? Infinity) - Date.now(), monotonicEnd - performance.now()) + timer = setTimeout(() => { if (remaining > 2147483647) schedule(); else stop() }, Math.max(1, Math.min(2147483647, remaining))) + } + schedule() + const work = async () => { + live(); intent = readIntent(await readOriginal(originalContext, scope)) + monotonicEnd=Math.min(monotonicEnd,enteredMonotonic+Math.max(0,intent.expiresAt-entered)) + live(); schedule() + const original = intent, executeScope = Object.freeze({ deadlineAt: Math.min(parentDeadline, original.expiresAt), signal: controller.signal }) + const actor = async (current: BandwidthScope) => { live(current); const fresh = readIntent(await readOriginal(originalContext, current)); live(current); if (!isDeepStrictEqual(fresh, original)) refuse() } + await actor(executeScope) + phase="source-observation";let sourceToken = await observer.observe(original.sourceServerId, executeScope); live() + phase="remote-observation";let remoteToken = await observer.observe(original.actualRemoteServerId, executeScope); live() + let sourceEvidence = freeze(await observer.inspect(sourceToken, executeScope)), remoteEvidence = freeze(await observer.inspect(remoteToken, executeScope)) + const originalSourceSubject=freeze(structuredClone(nodeSubject(sourceEvidence))),originalRemoteSubject=freeze(structuredClone(nodeSubject(remoteEvidence))) + let preScope = Object.freeze({ deadlineAt: Math.min(executeScope.deadlineAt, sourceEvidence.expiresAt, remoteEvidence.expiresAt), signal: controller.signal }) + // Each short phase obtains NEW real observations of the SAME original + // approved subjects. Expired capabilities are never inspected/adopted; + // actor/peer expiry, credentials, epochs and claims cannot be renewed. + // Call only between transitions, with no journal/registry lease held. + const observePhase=async()=>{ + await actor(executeScope) + const tokens=await Promise.all([observer.observe(original.sourceServerId,executeScope),observer.observe(original.actualRemoteServerId,executeScope)]) + const evidence=await Promise.all(tokens.map(token=>observer.inspect(token,executeScope))) + if(!isDeepStrictEqual(nodeSubject(evidence[0]),originalSourceSubject)||!isDeepStrictEqual(nodeSubject(evidence[1]),originalRemoteSubject))refuse() + live() + sourceToken=tokens[0];remoteToken=tokens[1];sourceEvidence=freeze(evidence[0]);remoteEvidence=freeze(evidence[1]) + preScope=Object.freeze({deadlineAt:Math.min(executeScope.deadlineAt,sourceEvidence.expiresAt,remoteEvidence.expiresAt),signal:controller.signal}) + await actor(preScope) + } + const nativeFresh = async (current: BandwidthScope) => { + live(current) + if (!isDeepStrictEqual(await observer.inspect(sourceToken, current), sourceEvidence) || !isDeepStrictEqual(await observer.inspect(remoteToken, current), remoteEvidence)) refuse() + live(current) + } + const originalGuard = async () => { await nativeFresh(preScope); await actor(preScope) } + let pin: NodeBinding | undefined + const resolve = async (token: unknown, evidence: Readonly) => { + await originalGuard(); const session = await nodes.open(preScope, originalGuard) + try { + const binding = freeze(await session.binding()); if (pin && !isDeepStrictEqual(pin, binding)) refuse(); pin ??= binding + const registry = createBandwidthNodeRegistry({ now: Date.now, connect: async () => session.connection, + inspect: (value, current) => observer.inspect(value, current), keyedProof: (purpose, value) => session.keyedProof(purpose, value), keyedProofs: inputs => session.keyedProofs(inputs), + authorize: async (action, id, current, genuine) => { + if (action !== "resolve" || id !== evidence.serverId || !isDeepStrictEqual(genuine, evidence)) refuse() + if (!isDeepStrictEqual(await session.binding(), pin)) refuse() + await nativeFresh(current); await actor(current) + return { installationId: binding.installationId, issuerKeySha256: binding.issuerKeySha256, quiescenceProof: binding.quiescenceProof, + authorityProof: original.authorityProof, authorityGeneration: original.authorityGeneration, expiresAt: original.expiresAt } + } }) + const receipt = freeze(await registry.resolve(token, preScope)); if (receipt.outcome !== "existing" || receipt.status !== "active") refuse() + return receipt + } finally { session.close() } + } + phase="source-resolution";const sourceReceipt = await resolve(sourceToken, sourceEvidence) + await observePhase() + phase="remote-resolution";const remoteReceipt = await resolve(remoteToken, remoteEvidence) + if (!pin || sourceReceipt.canonicalNodeId === remoteReceipt.canonicalNodeId) refuse() + const originalPin = pin + const participants = ()=>freeze({ sourceEvidence, remoteEvidence, sourceReceipt, remoteReceipt }) + const remoteSchema = z.object({ proof: hash, expiresAt: positive }).strict() + await observePhase() + phase="remote-policy";const peer = freeze(remoteSchema.parse(capture(await verifyRemote(originalContext, original, participants(), preScope)))) + if (peer.expiresAt <= Date.now()) refuse() + const peerMonotonicEnd=enteredMonotonic+Math.max(0,peer.expiresAt-entered) + const beforeDispatch = async () => { + await nativeFresh(preScope) + const currentPeer = remoteSchema.parse(capture(await verifyRemote(originalContext, original, participants(), preScope))) + if (!isDeepStrictEqual(currentPeer, peer) || Date.now() >= peer.expiresAt || performance.now()>=peerMonotonicEnd) refuse() + await actor(preScope) + } + const identityMatches = (value: { installationId: string; controlGeneration: string; sourceRecordSha256: string }) => { + if (value.installationId !== originalPin.installationId || value.controlGeneration !== originalPin.controlGeneration || value.sourceRecordSha256 !== originalPin.sourceRecordSha256) refuse() + } + const aliasFence = (fence: BandwidthControlDispatchFence, current: BandwidthScope) => nodes.assertResolvedOnFence(fence, { + binding: originalPin, participants: [{ evidence: sourceEvidence, receipt: sourceReceipt }, { evidence: remoteEvidence, receipt: remoteReceipt }], + }, current) + const dispatchGuard = (current: BandwidthScope, execute: boolean) => async (fence: BandwidthControlDispatchFence) => { + identityMatches(await fence.sourceIdentity()) + if (execute) { await aliasFence(fence, current); await beforeDispatch() } + } + const endpointProof = async (receipt: BandwidthNodeRegistryReceipt, evidence: BandwidthNodeObservationEvidence) => core.admissionProof("bandwidth-endpoint-v1", { receipt, subject:nodeSubject(evidence), peer, sourceRecordSha256: originalPin.sourceRecordSha256 }, originalPin.sourceRecordSha256, preScope) + phase="participant-proof";const source = { canonicalNodeId: sourceReceipt.canonicalNodeId, endpointProof: await endpointProof(sourceReceipt, sourceEvidence) } + const destination = { canonicalNodeId: remoteReceipt.canonicalNodeId, endpointProof: await endpointProof(remoteReceipt, remoteEvidence) } + phase="strict-client";const client = await observer.strictClient(sourceToken, preScope); await beforeDispatch() + let activeJournal: { connection: ProbeStorageConnection; scope: BandwidthScope } | undefined + let originalBinding: Readonly | undefined, sendInvocations = 0, terminal: { bindingProof: string; proof: string } | undefined + let transition: BandwidthTransition | undefined + const own = (binding: Readonly) => { + if (originalBinding && !isDeepStrictEqual(originalBinding, binding)) refuse() + originalBinding ??= freeze(structuredClone(binding)) + } + const baseJournal = createBandwidthAdmissionSqlJournal({ now: Date.now, + connect: async current => { + if (activeJournal) refuse() + const connection = await core.connect(current, dispatchGuard(current, transition === "reserve" || transition === "arm")) + activeJournal = { connection, scope: current } + return { query: (text, values, options) => connection.query(text, values, options), release(unsafe) { activeJournal = undefined; connection.release(unsafe) } } + }, + authorize: async (binding, current, transition) => { own(binding); if (transition === "reserve" || transition === "arm") await beforeDispatch(); else if (current.signal?.aborted || Date.now() >= current.deadlineAt) refuse() }, + verifyEvidence: async (transition, binding, evidence) => { + own(binding) + if (transition === "complete") { if (!terminal || terminal.bindingProof !== binding.bindingProof || !isDeepStrictEqual(evidence, { terminalProof: terminal.proof })) refuse() } + else if (transition === "release-before-dispatch") { if (sendInvocations !== 0 || !evidence || !["reserved", "armed"].includes((evidence as { from: string }).from) || (evidence as { proof: string }).proof !== "private-no-send") refuse() } + else if (evidence !== undefined) refuse() + }, + eventProof: async event => { const current = activeJournal; if (!current) refuse(); return core.eventProof(event, current.connection, current.scope) }, + }) + // Track the transition explicitly: cleanup scopes intentionally do NOT + // inherit expired execute/native-observation authority or cancellation. + const inTransition = async (kind: BandwidthTransition, work: () => Promise) => { + if (transition) refuse(); transition = kind;phase=kind + try { return await work() } finally { transition = undefined } + } + const journal: BandwidthJournal = Object.freeze({ + reserve: async(binding, current) => {await observePhase();mutationPossible=true;return inTransition("reserve", () => baseJournal.reserve(binding, current))}, + arm: async(binding, current) => {await observePhase();return inTransition("arm", () => baseJournal.arm(binding, current))}, + complete: (binding, evidence, current) => inTransition("complete", () => baseJournal.complete(binding, evidence, current)), + releaseBeforeDispatch: (binding, evidence, current) => inTransition("release-before-dispatch", () => baseJournal.releaseBeforeDispatch(binding, evidence, current)), + quarantine: (binding, current) => inTransition("quarantine", () => baseJournal.quarantine(binding, current)), + }) + const admissionInput: BandwidthAdmissionInput = { attemptId: original.attemptId, callerKind: original.callerKind, operationId: original.operationId, + request: { originalRequest: original.request, context: { sourceServerId: original.sourceServerId, actualRemoteServerId: original.actualRemoteServerId, credentialServerId: original.credentialServerId, + socketTimeoutMs: original.socketTimeoutMs, peerProof: peer.proof } }, source, destination, authorityProof: original.authorityProof, + authorityGeneration: original.authorityGeneration, controlGeneration: originalPin.controlGeneration, expiresAt: original.expiresAt, durationSec: original.durationSec, nativeBody: original.nativeBody } + const admission = createBandwidthAdmission(admissionInput, { now: Date.now, cleanupBudgetMs: 10000, nativeClient: client, journal, + authorize: async (_binding, current) => { await beforeDispatch(); live(current); return { authorityProof: original.authorityProof, authorityGeneration: original.authorityGeneration, controlGeneration: originalPin.controlGeneration, expiresAt: original.expiresAt } }, + endpoints: async () => { await beforeDispatch(); return { source, destination } }, + keyedProof: async (purpose, value) => { + const current = purpose === "bandwidth-terminal-v1" ? { deadlineAt: Date.now() + 10000 } : preScope + const proof = await core.admissionProof(purpose, value, originalPin.sourceRecordSha256, current) + if (purpose === "bandwidth-terminal-v1") { phase="terminal-proof";if (!originalBinding) refuse(); terminal = { bindingProof: originalBinding.bindingProof, proof } } + return proof + }, + send: async (_binding, body, current, permit) => { + // A send invocation is already potentially ambiguous. No later + // transport exception can fabricate private no-send evidence. + sendInvocations++ + phase="native-send" + return client.post("/tool/bandwidth-test", { ...body }, original.socketTimeoutMs, current.signal, async () => { + await observePhase() + const lease = await core.connect(preScope, dispatchGuard(preScope, true)) + try { await lease.query("SELECT 1"); await beforeDispatch() } finally { lease.release(true) } + }, permit) + } }) + phase="reserve-arm";const capability=await admission.reserveAndArm(executeScope) + try{await observePhase()}catch(error){await admission.cancel(capability);throw error} + return admission.dispatch(capability) + } + try { return await Promise.race([work(), stopped]) } + catch { throw new BandwidthExecutionHostError(mutationPossible?"OUTCOME_UNKNOWN":"REFUSED",phase) } + finally { running = false; clearTimeout(timer); parentSignal?.removeEventListener("abort", abort); controller.signal.removeEventListener("abort", stop); controller.abort() } + } }) +} diff --git a/backend/src/services/bandwidth-external-node-registry-catalog.test.ts b/backend/src/services/bandwidth-external-node-registry-catalog.test.ts new file mode 100644 index 00000000..2a403803 --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry-catalog.test.ts @@ -0,0 +1,81 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL } from "./bandwidth-external-node-registry-schema.js" +import { observeBandwidthExternalNodeRegistryCatalog, BandwidthExternalNodeRegistryCatalogRefused } from "./bandwidth-external-node-registry-catalog.js" + +type Row=Record +type Item={section:string;raw_json:string|null} +const refused=(error:unknown)=>error instanceof BandwidthExternalNodeRegistryCatalogRefused +/** Inert catalog fixture derived from the checked-in component, not an external + * supplied digest. Actual PostgreSQL deparser/types are covered separately. */ +function fixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_external_node_aliases","bandwidth_external_node_events"] + const functions=["bandwidth_external_node_alias_guard","bandwidth_external_node_event_guard","bandwidth_external_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_external_node_aliases:[["p",["enrollment_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_external_node_events:[["p",["event_id"]],["u",["enrollment_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_external_node_aliases","node_id","bandwidth_nodes"],["bandwidth_external_node_events","enrollment_id","bandwidth_external_node_aliases"],["bandwidth_external_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +const run=(rows:Item[],live=()=>{})=>observeBandwidthExternalNodeRegistryCatalog({query:async()=>({rows})},live) +function mutate(rows:Item[],section:string,predicate:(row:Row)=>boolean,change:(row:Row)=>void){ + const item=rows.find(item=>item.section===section&&predicate(JSON.parse(item.raw_json!)as Row))!;assert.ok(item,section) + const row=JSON.parse(item.raw_json!)as Row;change(row);item.raw_json=JSON.stringify(row) +} + +test("external fixed catalog includes two tables, original-node incoming FK guards and deterministic pin",async()=>{ + const rows=fixture(),first=await run(rows);assert.match(first,/^[a-f0-9]{64}$/);assert.equal(await run(rows.reverse()),first) + assert.equal(rows.filter(item=>item.section==="tables").length,2) + assert.equal(rows.filter(item=>item.section==="triggers"&&JSON.parse(item.raw_json!).table_name==="bandwidth_nodes").length,4) +}) +test("external catalog refuses altered UUID/reference, guard body, ALWAYS trigger and occupancy tables",async()=>{ + for(const [section,predicate,change]of [ + ["columns",(r:Row)=>r.name==="enrollment_id",(r:Row)=>{r.type_name="int8"}], + ["functions",(r:Row)=>r.name==="bandwidth_external_node_alias_guard",(r:Row)=>{r.body="BEGIN RETURN NEW; END;"}], + ["triggers",(r:Row)=>r.name==="bandwidth_external_node_alias_guard",(r:Row)=>{r.enabled="O"}], + ["constraints",(r:Row)=>r.type==="f",(r:Row)=>{r.delete_action="c"}], + ["tables",(_r:Row)=>true,(r:Row)=>{r.name="bandwidth_node_occupancy"}], + ] as Array<[string,(r:Row)=>boolean,(r:Row)=>void]>){const rows=fixture();mutate(rows,section,predicate,change);await assert.rejects(run(rows),refused)} +}) +test("external catalog requires all inherited RI trigger identities and no extra policies",async()=>{ + const rows=fixture();rows.splice(rows.findIndex(item=>item.section==="triggers"&&JSON.parse(item.raw_json!).table_name==="bandwidth_nodes"),1);await assert.rejects(run(rows),refused) + const policy=fixture();policy.push({section:"policies",raw_json:JSON.stringify({name:"unsafe"})});await assert.rejects(run(policy),refused) +}) diff --git a/backend/src/services/bandwidth-external-node-registry-catalog.ts b/backend/src/services/bandwidth-external-node-registry-catalog.ts new file mode 100644 index 00000000..ce91d5d2 --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry-catalog.ts @@ -0,0 +1,237 @@ +import { createHash } from "node:crypto" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL } from "./bandwidth-external-node-registry-schema.js" + +/** Fixed external extension verification plus a canonical observation digest. + * A new combined signed installation pin is mandatory: incoming FK triggers + * extend the original node catalog footprint, so a v1 catalog pin cannot attest it. + * No default + * I/O, DDL, role assertion, readiness or generation issuance. Constraint/index + * definitions are captured for the fixed issuer's signed pin; their structural + * checks below are not a general proof of arbitrary SQL expression equivalence. + * The core truncate function referenced by registry guards is independently + * checked against the core literal here, including its owner/path/signature. */ +export interface BandwidthExternalNodeRegistryCatalogSql { query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> } +export class BandwidthExternalNodeRegistryCatalogRefused extends Error { + readonly code = "BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_REFUSED" + constructor() { super("Bandwidth node registry catalog refused") } +} +type Row = Record +const tableNames = ["bandwidth_external_node_aliases", "bandwidth_external_node_events"] as const +const functionNames = ["bandwidth_external_node_alias_guard", "bandwidth_external_node_event_guard", "bandwidth_external_node_alias_consistency_guard", "bandwidth_admission_reject_truncate"] as const +const normalizeBody = (value: string) => value.replace(/\r\n?/g, "\n").trim() +function expectedBodies(): Map { + const found = new Map() + for (const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL, BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL]) { + for (const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)) { + if (!functionNames.includes(match[1] as typeof functionNames[number])) continue + if (found.has(match[1])) refuse() + found.set(match[1], normalizeBody(match[2])) + } + } + if (found.size !== functionNames.length || functionNames.some(name => !found.has(name))) refuse() + return found +} +const sections = { namespace: 1, tables: 2, columns: 16, functions: 4, triggers: 18, constraints: 80, indexes: 6, rules: 0, policies: 0 } as const +export const BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL = `WITH component AS ( + SELECT c.* FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + WHERE n.nspname='mmapp_control' AND c.relname=ANY(ARRAY['bandwidth_external_node_aliases','bandwidth_external_node_events']) +), catalog AS MATERIALIZED ( + SELECT 'namespace'::text AS section,pg_catalog.to_jsonb(q)::text AS raw_json FROM ( + SELECT n.nspname::text AS name,o.rolname::text AS owner FROM pg_catalog.pg_namespace n + JOIN pg_catalog.pg_roles o ON o.oid=n.nspowner WHERE n.nspname='mmapp_control' LIMIT 2) q + UNION ALL SELECT 'tables',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS name,o.rolname::text AS owner,c.relkind::text AS kind,c.relpersistence::text AS persistence, + c.relrowsecurity AS row_security,c.relforcerowsecurity AS force_row_security,c.relispartition AS is_partition,c.relreplident::text AS replica_identity,c.reloptions AS options, + EXISTS(SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhrelid=c.oid) AS has_parent,EXISTS(SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhparent=c.oid) AS has_children, + c.reloftype<>0 AS typed_table,(SELECT am.amname::text FROM pg_catalog.pg_am am WHERE am.oid=c.relam) AS access_method + FROM component c JOIN pg_catalog.pg_roles o ON o.oid=c.relowner ORDER BY c.relname LIMIT 3) q + UNION ALL SELECT 'columns',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,a.attnum::integer AS position,a.attname::text AS name,tn.nspname::text AS type_namespace,t.typname::text AS type_name, + a.attnotnull AS not_null,a.attidentity::text AS identity,a.attgenerated::text AS generated, + cn.nspname::text AS collation_namespace,co.collname::text AS collation_name, + pg_catalog.pg_get_expr(d.adbin,d.adrelid,true) AS default_expr + FROM component c JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum>0 + JOIN pg_catalog.pg_type t ON t.oid=a.atttypid JOIN pg_catalog.pg_namespace tn ON tn.oid=t.typnamespace + LEFT JOIN pg_catalog.pg_collation co ON co.oid=a.attcollation LEFT JOIN pg_catalog.pg_namespace cn ON cn.oid=co.collnamespace + LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=c.oid AND d.adnum=a.attnum + WHERE NOT a.attisdropped ORDER BY c.relname,a.attnum LIMIT 17) q + UNION ALL SELECT 'functions',pg_catalog.to_jsonb(q)::text FROM ( + SELECT p.proname::text AS name,o.rolname::text AS owner,l.lanname::text AS language,p.prosrc AS body,p.prosecdef AS security_definer, + p.proisstrict AS strict,p.provolatile::text AS volatility,p.proleakproof AS leakproof,p.prokind::text AS kind, + p.proparallel::text AS parallel,p.pronargdefaults::integer AS argument_defaults,p.provariadic<>0 AS variadic,p.proconfig AS config, + p.proretset AS returns_set,p.prosupport<>0 AS has_support,p.proallargtypes IS NULL AND p.proargmodes IS NULL AS input_only,p.proargnames AS argument_names, + rt.typname::text AS return_type,rn.nspname::text AS return_namespace, + ARRAY(SELECT t.typname::text FROM pg_catalog.unnest(p.proargtypes::oid[]) WITH ORDINALITY a(type_oid,position) JOIN pg_catalog.pg_type t ON t.oid=a.type_oid ORDER BY a.position) AS argument_types, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(p.proargtypes::oid[]) WITH ORDINALITY a(type_oid,position) JOIN pg_catalog.pg_type t ON t.oid=a.type_oid JOIN pg_catalog.pg_namespace n ON n.oid=t.typnamespace ORDER BY a.position) AS argument_namespaces + FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace JOIN pg_catalog.pg_language l ON l.oid=p.prolang + JOIN pg_catalog.pg_roles o ON o.oid=p.proowner JOIN pg_catalog.pg_type rt ON rt.oid=p.prorettype JOIN pg_catalog.pg_namespace rn ON rn.oid=rt.typnamespace + WHERE n.nspname='mmapp_control' AND p.proname=ANY(ARRAY['bandwidth_external_node_alias_guard','bandwidth_external_node_event_guard','bandwidth_external_node_alias_consistency_guard','bandwidth_admission_reject_truncate']) + ORDER BY p.proname,p.oid LIMIT 5) q + UNION ALL SELECT 'triggers',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,CASE WHEN t.tgisinternal THEN NULL ELSE t.tgname::text END AS name,t.tgisinternal AS internal, + t.tgtype::integer AS type,t.tgenabled::text AS enabled,t.tgdeferrable AS deferrable,t.tginitdeferred AS initially_deferred, + t.tgconstraint<>0 AS is_constraint,con.conname::text AS constraint_name, + pn.nspname::text AS function_namespace,p.proname::text AS function_name,t.tgnargs::integer AS argument_count, + pg_catalog.encode(t.tgargs,'hex') AS arguments,t.tgattr::text AS columns,pg_catalog.pg_get_expr(t.tgqual,t.tgrelid,true) AS condition, + t.tgoldtable::text AS old_table,t.tgnewtable::text AS new_table + FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace component_ns ON component_ns.oid=c.relnamespace + JOIN pg_catalog.pg_trigger t ON t.tgrelid=c.oid JOIN pg_catalog.pg_proc p ON p.oid=t.tgfoid + JOIN pg_catalog.pg_namespace pn ON pn.oid=p.pronamespace LEFT JOIN pg_catalog.pg_constraint con ON con.oid=t.tgconstraint + WHERE component_ns.nspname='mmapp_control' AND (c.oid IN (SELECT oid FROM component) + OR (c.relname='bandwidth_nodes' AND t.tgisinternal AND con.conrelid IN (SELECT oid FROM component))) + ORDER BY c.relname,t.tgname LIMIT 19) q + UNION ALL SELECT 'constraints',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,k.conname::text AS name,k.contype::text AS type,k.convalidated AS validated,k.condeferrable AS deferrable,k.condeferred AS initially_deferred, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(k.conkey) WITH ORDINALITY x(attnum,position) JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum=x.attnum ORDER BY x.position) AS keys, + fn.nspname::text AS foreign_namespace,fc.relname::text AS foreign_table, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(k.confkey) WITH ORDINALITY x(attnum,position) JOIN pg_catalog.pg_attribute a ON a.attrelid=k.confrelid AND a.attnum=x.attnum ORDER BY x.position) AS foreign_keys, + k.confupdtype::text AS update_action,k.confdeltype::text AS delete_action,k.confmatchtype::text AS match_type, + pg_catalog.pg_get_constraintdef(k.oid,true) AS definition + FROM component c JOIN pg_catalog.pg_constraint k ON k.conrelid=c.oid + LEFT JOIN pg_catalog.pg_class fc ON fc.oid=k.confrelid LEFT JOIN pg_catalog.pg_namespace fn ON fn.oid=fc.relnamespace + ORDER BY c.relname,k.conname LIMIT 81) q + UNION ALL SELECT 'indexes',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,ic.relname::text AS name,i.indisunique AS unique,i.indisprimary AS primary,i.indisvalid AS valid,i.indisready AS ready,i.indislive AS live, + i.indnkeyatts::integer AS key_count,i.indnatts::integer AS attribute_count,am.amname::text AS method, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(i.indkey::smallint[]) WITH ORDINALITY x(attnum,position) LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum=x.attnum ORDER BY x.position) AS keys, + ARRAY(SELECT co.collname::text FROM pg_catalog.unnest(i.indcollation::oid[]) WITH ORDINALITY x(collation_oid,position) LEFT JOIN pg_catalog.pg_collation co ON co.oid=x.collation_oid ORDER BY x.position) AS collations, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(i.indcollation::oid[]) WITH ORDINALITY x(collation_oid,position) LEFT JOIN pg_catalog.pg_collation co ON co.oid=x.collation_oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid=co.collnamespace ORDER BY x.position) AS collation_namespaces, + ARRAY(SELECT op.opcname::text FROM pg_catalog.unnest(i.indclass::oid[]) WITH ORDINALITY x(class_oid,position) JOIN pg_catalog.pg_opclass op ON op.oid=x.class_oid ORDER BY x.position) AS operator_classes, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(i.indclass::oid[]) WITH ORDINALITY x(class_oid,position) JOIN pg_catalog.pg_opclass op ON op.oid=x.class_oid JOIN pg_catalog.pg_namespace n ON n.oid=op.opcnamespace ORDER BY x.position) AS operator_class_namespaces, + i.indoption::text AS options,pg_catalog.pg_get_expr(i.indexprs,i.indrelid,true) AS expressions,pg_catalog.pg_get_expr(i.indpred,i.indrelid,true) AS predicate, + pg_catalog.pg_get_indexdef(i.indexrelid) AS definition + FROM component c JOIN pg_catalog.pg_index i ON i.indrelid=c.oid JOIN pg_catalog.pg_class ic ON ic.oid=i.indexrelid JOIN pg_catalog.pg_am am ON am.oid=ic.relam + ORDER BY c.relname,ic.relname LIMIT 7) q + UNION ALL SELECT 'rules',pg_catalog.to_jsonb(q)::text FROM (SELECT c.relname::text AS table_name,r.rulename::text AS name FROM component c JOIN pg_catalog.pg_rewrite r ON r.ev_class=c.oid LIMIT 1) q + UNION ALL SELECT 'policies',pg_catalog.to_jsonb(q)::text FROM (SELECT c.relname::text AS table_name,p.polname::text AS name FROM component c JOIN pg_catalog.pg_policy p ON p.polrelid=c.oid LIMIT 1) q +), metered AS ( + SELECT section,raw_json,pg_catalog.sum(pg_catalog.octet_length(raw_json)) OVER(ORDER BY section,raw_json) AS bytes FROM catalog +) SELECT section,CASE WHEN pg_catalog.octet_length(raw_json)<=65536 AND bytes<=1048576 THEN raw_json END AS raw_json FROM metered ORDER BY section,raw_json` + +function refuse(): never { throw new BandwidthExternalNodeRegistryCatalogRefused() } +const plain = (value: unknown): value is Row => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +const arrayEqual = (value: unknown, expected: unknown[]) => Array.isArray(value) && JSON.stringify(value) === JSON.stringify(expected) +function canonical(value: unknown): string { + if (Array.isArray(value)) return "[" + value.map(canonical).join(",") + "]" + if (plain(value)) return "{" + Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" + return JSON.stringify(value) +} +type Column = [name: string, type: string, notNull: boolean, defaultExpr?: string] +const text = (name: string, notNull = true): Column => [name, "text", notNull] +const columns: Record = { + bandwidth_external_node_aliases: [["enrollment_id","uuid",true],["incarnation","uuid",true],["node_id","uuid",true],text("status"),text("record_mac"),["created_at","timestamptz",true,"clock_timestamp()"]], + bandwidth_external_node_events: [["event_id","uuid",true],["enrollment_id","uuid",true],["incarnation","uuid",true],["node_id","uuid",true],text("kind"),text("old_status",false),text("new_status"),text("authority_proof"),text("event_mac"),["created_at","timestamptz",true,"clock_timestamp()"]], +} +const triggers: Record = { + bandwidth_external_node_alias_guard: ["bandwidth_external_node_aliases","bandwidth_external_node_alias_guard",31,false], + bandwidth_external_node_event_guard: ["bandwidth_external_node_events","bandwidth_external_node_event_guard",31,false], + bandwidth_external_node_aliases_no_truncate: ["bandwidth_external_node_aliases","bandwidth_admission_reject_truncate",34,false], + bandwidth_external_node_events_no_truncate: ["bandwidth_external_node_events","bandwidth_admission_reject_truncate",34,false], + bandwidth_external_node_alias_consistency: ["bandwidth_external_node_aliases","bandwidth_external_node_alias_consistency_guard",29,true], + bandwidth_external_node_event_consistency: ["bandwidth_external_node_events","bandwidth_external_node_alias_consistency_guard",29,true], +} + +export async function observeBandwidthExternalNodeRegistryCatalog(sql: BandwidthExternalNodeRegistryCatalogSql, live: () => void): Promise { + live() + let result: { rows: Row[] } + try { result = await sql.query(BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL, []) } catch { live(); refuse() } + live() + return validateBandwidthExternalNodeRegistryCatalogRows(result.rows) +} + +export function validateBandwidthExternalNodeRegistryCatalogRows(rows: unknown): string { + const bodies = expectedBodies() + if (!Array.isArray(rows) || rows.length > 230) refuse() + const found = Object.fromEntries(Object.keys(sections).map(key => [key, [] as Row[]])) as unknown as Record + let bytes = 0 + for (const item of rows) { + if (!plain(item) || Object.keys(item).sort().join(",") !== "raw_json,section" || typeof item.section !== "string" || !Object.hasOwn(sections,item.section) || typeof item.raw_json !== "string") refuse() + bytes += Buffer.byteLength(item.raw_json) + if (Buffer.byteLength(item.raw_json)>65536 || bytes>1048576) refuse() + let parsed: unknown + try { parsed = JSON.parse(item.raw_json) } catch { refuse() } + if (!plain(parsed)) refuse() + const section = item.section as keyof typeof sections + found[section].push(parsed) + if (found[section].length>sections[section]) refuse() + } + if (found.namespace.length!==1 || found.namespace[0].name!=="mmapp_control" || typeof found.namespace[0].owner!=="string") refuse() + if (found.tables.length!==2 || new Set(found.tables.map(row=>row.name)).size!==2) refuse() + for (const row of found.tables) if (!tableNames.includes(row.name as typeof tableNames[number]) || row.owner!==found.namespace[0].owner || row.kind!=="r" || row.persistence!=="p" || row.row_security!==false || row.force_row_security!==false || row.is_partition!==false || row.replica_identity!=="d" || row.options!==null || row.has_parent!==false || row.has_children!==false || row.typed_table!==false || row.access_method!=="heap") refuse() + if (found.columns.length!==16) refuse() + for (const table of tableNames) { + const actual = found.columns.filter(row=>row.table_name===table).sort((a,b)=>Number(a.position)-Number(b.position)) + if (actual.length!==columns[table].length) refuse() + for (const [index,expected] of columns[table].entries()) { + const row=actual[index],collation=expected[1]==="text"?"C":null + const defaultExpr=row.default_expr==="pg_catalog.clock_timestamp()"?"clock_timestamp()":row.default_expr + if (row.position!==index+1 || row.name!==expected[0] || row.type_namespace!=="pg_catalog" || row.type_name!==expected[1] || row.not_null!==expected[2] || row.identity!=="" || row.generated!=="" || row.collation_name!==collation || row.collation_namespace!==(collation?"pg_catalog":null) || defaultExpr!==(expected[3]??null)) refuse() + row.default_expr=defaultExpr + } + } + if (found.functions.length!==4 || new Set(found.functions.map(row=>row.name)).size!==4) refuse() + for (const row of found.functions) { + const args: string[]=[] + if (typeof row.name!=="string" || !bodies.has(row.name) || typeof row.body!=="string" || normalizeBody(row.body)!==bodies.get(row.name) + || row.owner!==found.namespace[0].owner || row.language!=="plpgsql" || row.return_namespace!=="pg_catalog" || row.return_type!=="trigger" + || row.security_definer!==false || row.strict!==false || row.volatility!=="v" || row.leakproof!==false || row.kind!=="f" || row.parallel!=="u" || row.argument_defaults!==0 || row.variadic!==false + || row.returns_set!==false || row.has_support!==false || row.input_only!==true || row.argument_names!==null + || !arrayEqual(row.argument_types,args) || !arrayEqual(row.argument_namespaces,args.map(()=>"pg_catalog")) + || !Array.isArray(row.config) || row.config.length!==1 || typeof row.config[0]!=="string" || row.config[0].replace(/\s/g,"")!=="search_path=pg_catalog,mmapp_control") refuse() + row.body=normalizeBody(row.body) + row.config=["search_path=pg_catalog,mmapp_control"] + } + const userTriggers=found.triggers.filter(row=>row.internal===false),internal=found.triggers.filter(row=>row.internal===true) + if (userTriggers.length!==6 || internal.length!==12 || new Set(userTriggers.map(row=>row.name)).size!==6) refuse() + for (const row of found.triggers) { + if (row.argument_count!==0 || row.arguments!=="" || row.columns!=="" || row.condition!==null || row.old_table!==null || row.new_table!==null) refuse() + if (row.internal===false) { + const expected=typeof row.name==="string"?triggers[row.name]:undefined + if (!expected || row.table_name!==expected[0] || row.function_namespace!=="mmapp_control" || row.function_name!==expected[1] || row.type!==expected[2] || row.enabled!=="A" || row.deferrable!==expected[3] || row.initially_deferred!==expected[3] || row.is_constraint!==expected[3] || row.constraint_name!==(expected[3]?row.name:null)) refuse() + } else if (row.internal!==true || row.name!==null || row.function_namespace!=="pg_catalog" || !["RI_FKey_check_ins","RI_FKey_check_upd","RI_FKey_restrict_del","RI_FKey_restrict_upd"].includes(String(row.function_name)) || row.enabled!=="O" || row.is_constraint!==true || row.deferrable!==false || row.initially_deferred!==false) refuse() + } + const checkCounts: Record={bandwidth_external_node_aliases:2,bandwidth_external_node_events:6} + const keySpecs: Record={bandwidth_external_node_aliases:["p:enrollment_id","u:incarnation","f:node_id"],bandwidth_external_node_events:["p:event_id","u:enrollment_id,kind","f:enrollment_id","f:node_id"]} + for (const table of tableNames) { + const actual=found.constraints.filter(row=>row.table_name===table),keys:string[]=[] + if (actual.filter(row=>row.type==="c").length!==checkCounts[table]) refuse() + for (const row of actual) { + if (typeof row.name!=="string" || typeof row.definition!=="string" || !row.definition || row.validated!==true || !Array.isArray(row.keys) || !["p","u","f","c","n","t"].includes(String(row.type))) refuse() + if (row.type==="t") { if (row.deferrable!==true || row.initially_deferred!==true || !Object.hasOwn(triggers,row.name)) refuse() } + else if (row.deferrable!==false || row.initially_deferred!==false) refuse() + if (["p","u","f"].includes(String(row.type))) keys.push(row.type+":"+row.keys.join(",")) + if (row.type==="f") { + const target=arrayEqual(row.keys,["node_id"])?"bandwidth_nodes":table==="bandwidth_external_node_events"&&arrayEqual(row.keys,["enrollment_id"])?"bandwidth_external_node_aliases":undefined + if (!target || row.foreign_namespace!=="mmapp_control" || row.foreign_table!==target || !arrayEqual(row.foreign_keys,row.keys) || row.update_action!=="r" || row.delete_action!=="r" || row.match_type!=="s") refuse() + } + const constraintKeys=row.keys + if (row.type==="n" && (constraintKeys.length!==1 || !columns[table].some(column=>column[0]===constraintKeys[0]&&column[2]))) refuse() + } + if (!arrayEqual(keys.sort(),keySpecs[table].sort())) refuse() + } + if (found.constraints.some(row=>!tableNames.includes(row.table_name as typeof tableNames[number]))) refuse() + const expectedInternal:string[]=[] + for (const fk of found.constraints.filter(row=>row.type==="f")) { + expectedInternal.push(`${fk.table_name}:${fk.name}:RI_FKey_check_ins:5`,`${fk.table_name}:${fk.name}:RI_FKey_check_upd:17`,`${fk.foreign_table}:${fk.name}:RI_FKey_restrict_del:9`,`${fk.foreign_table}:${fk.name}:RI_FKey_restrict_upd:17`) + } + if (!arrayEqual(internal.map(row=>`${row.table_name}:${row.constraint_name}:${row.function_name}:${row.type}`).sort(),expectedInternal.sort())) refuse() + if (!arrayEqual(found.constraints.filter(row=>row.type==="t").map(row=>row.table_name+":"+row.name).sort(),userTriggers.filter(row=>row.is_constraint===true).map(row=>row.table_name+":"+row.name).sort())) refuse() + const indexSpecs: Record={bandwidth_external_node_aliases:["p:enrollment_id","u:incarnation","i:node_id"],bandwidth_external_node_events:["p:event_id","u:enrollment_id,kind","i:node_id"]} + if (found.indexes.length!==6) refuse() + for (const table of tableNames) { + const actual=found.indexes.filter(row=>row.table_name===table),keys:string[]=[] + for (const row of actual) { + if (!Array.isArray(row.keys) || !row.keys.length || typeof row.name!=="string" || typeof row.unique!=="boolean" || typeof row.primary!=="boolean" || row.primary&&!row.unique || row.key_count!==row.keys.length || row.attribute_count!==row.keys.length || row.valid!==true || row.ready!==true || row.live!==true || row.method!=="btree" || row.expressions!==null || row.predicate!==null || row.options!==row.keys.map(()=>"0").join(" ") || typeof row.definition!=="string") refuse() + const expectedColumns=row.keys.map(name=>columns[table].find(column=>column[0]===name)) + if (expectedColumns.some(column=>!column) || !arrayEqual(row.collations,expectedColumns.map(column=>column![1]==="text"?"C":null)) || !arrayEqual(row.collation_namespaces,expectedColumns.map(column=>column![1]==="text"?"pg_catalog":null)) || !arrayEqual(row.operator_classes,expectedColumns.map(column=>column![1]==="uuid"?"uuid_ops":column![1]==="int4"?"int4_ops":column![1]==="int8"?"int8_ops":"text_ops")) || !arrayEqual(row.operator_class_namespaces,expectedColumns.map(()=>"pg_catalog"))) refuse() + keys.push((row.primary===true?"p":row.unique===true?"u":"i")+":"+row.keys.join(",")) + } + if (!arrayEqual(keys.sort(),indexSpecs[table].sort())) refuse() + } + // Sort semantic rows independently of SQL transport order. No physical OID or + // volatile statistics enter the digest; internal trigger names contain OIDs + // and are deliberately represented by their structural/function identity. + const snapshot=Object.fromEntries(Object.entries(found).map(([section,rows])=>[section,rows.map(canonical).sort()])) + return createHash("sha256").update(canonical(snapshot)).digest("hex") +} diff --git a/backend/src/services/bandwidth-external-node-registry-role.test.ts b/backend/src/services/bandwidth-external-node-registry-role.test.ts new file mode 100644 index 00000000..05a96fa1 --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry-role.test.ts @@ -0,0 +1,97 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { assertBandwidthExternalNodeRegistryRuntimeRole, BandwidthExternalNodeRegistryRuntimeRoleRefused, type BandwidthExternalNodeRegistryRoleExpected, type BandwidthExternalNodeRegistryRoleSql } from "./bandwidth-external-node-registry-role.js" + +const expected: BandwidthExternalNodeRegistryRoleExpected = { databaseName: "owned_database", runtimeRole: "bandwidth_runtime", ownerRole: "control_owner" } +function observation() { + return { + databaseName: expected.databaseName, sessionRole: expected.runtimeRole, currentRole: expected.runtimeRole, + runtimeRoleExists: true, ownerRoleExists: true, runtimeFlagsSafe: true, + ownerMembership: false, dangerousMembership: false, + schemaExists: true, schemaOwner: expected.ownerRole, schemaUsage: true, schemaCreate: false, + publicSchemaUsage: false, publicSchemaCreate: false, + tables: ["bandwidth_external_node_aliases", "bandwidth_nodes", "bandwidth_external_node_events", "bandwidth_node_authority"].map(name => ({ name, exists: true, ownerRole: expected.ownerRole, ownedByRuntime: false, privilegesExact: true })), + } +} +const deferred = () => { let resolve!: (value: T) => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const refused = (error: unknown) => error instanceof BandwidthExternalNodeRegistryRuntimeRoleRefused && error.code === "BANDWIDTH_EXTERNAL_NODE_REGISTRY_RUNTIME_ROLE_REFUSED" +async function check(value: unknown) { + return assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => ({ rows: [{ observation: value }] }) }, expected, () => {}) +} + +test("minimal separate runtime role succeeds with one catalog observation and live fences on both sides", async () => { + const events: string[] = [], calls: Array<{ text: string; values?: unknown[] }> = [] + const sql: BandwidthExternalNodeRegistryRoleSql = { query: async (text, values) => { events.push("SQL"); calls.push({ text, values }); return { rows: [{ observation: observation() }] } } } + assert.equal(await assertBandwidthExternalNodeRegistryRuntimeRole(sql, expected, () => { events.push("live") }), undefined) + assert.deepEqual(events, ["live", "SQL", "live"]); assert.equal(calls.length, 1) + assert.deepEqual(calls[0].values, [expected.databaseName, expected.runtimeRole, expected.ownerRole]) + const text = calls[0].text + for (const catalog of ["pg_roles", "pg_namespace", "pg_class", "has_schema_privilege", "has_table_privilege", "has_any_column_privilege", "aclexplode", "acldefault", "pg_has_role", "current_database"]) assert.ok(text.includes("pg_catalog." + catalog), catalog) + assert.match(text, /pg_has_role\(rt\.oid,r\.oid,'MEMBER'\)/) + for (const privilege of ["TRUNCATE", "REFERENCES", "TRIGGER", " WITH GRANT OPTION"]) assert.ok(text.includes(privilege)) + for (const role of ["pg_read_server_files", "pg_write_server_files", "pg_execute_server_program"]) assert.ok(text.includes(role)) + assert.ok(text.includes("n.nspname='mmapp_control'")); assert.ok(text.includes("'bandwidth_node_authority',ARRAY['SELECT']")) + assert.ok(text.includes("'bandwidth_nodes',ARRAY['SELECT','INSERT']")); assert.ok(text.includes("'bandwidth_external_node_aliases',ARRAY['SELECT','INSERT','UPDATE']")) + for (const coreTable of ["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events","bandwidth_source_identity"]) assert.ok(!text.includes("'"+coreTable+"'"), "core privileges remain a separate source assertion") + assert.doesNotMatch(text, /\b(?:INSERT INTO|DELETE FROM|ALTER TABLE|CREATE SCHEMA|SET ROLE)\b/) + assert.ok(!text.includes(expected.runtimeRole)); assert.ok(!text.includes(expected.ownerRole)); assert.ok(!text.includes(expected.databaseName)) +}) + +test("session/current role, database, role flags and direct or transitive memberships must all match", async () => { + const failures: Record = { databaseName: "foreign_database", sessionRole: "app_restorer", currentRole: "control_owner", runtimeRoleExists: false, ownerRoleExists: false, runtimeFlagsSafe: false, ownerMembership: true, dangerousMembership: true } + for (const [field, value] of Object.entries(failures)) await assert.rejects(check({ ...observation(), [field]: value }), refused, field) +}) + +test("missing control schema, incorrect ownership and PUBLIC or runtime CREATE/USAGE escalation refuse", async () => { + const failures: Record = { schemaExists: false, schemaOwner: "app_restorer", schemaUsage: false, schemaCreate: true, publicSchemaUsage: true, publicSchemaCreate: true } + for (const [field, value] of Object.entries(failures)) await assert.rejects(check({ ...observation(), [field]: value }), refused, field) +}) + +test("all four registry relations require fixed ownership and exact privileges, including SELECT-only authority", async () => { + for (let index = 0; index < 4; index++) { + for (const change of [{ exists: false }, { ownerRole: expected.runtimeRole }, { ownedByRuntime: true }, { privilegesExact: false }]) { + const value = observation(); Object.assign(value.tables[index], change) + await assert.rejects(check(value), refused) + } + } + const missing = observation(); missing.tables.pop(); await assert.rejects(check(missing), refused) + const duplicate = observation(); duplicate.tables[3] = { ...duplicate.tables[0] }; await assert.rejects(check(duplicate), refused) + const unknown = observation(); unknown.tables[3].name = "public_shadow"; await assert.rejects(check(unknown), refused) +}) + +test("malformed or truthy metadata never substitutes for exact boolean observations", async () => { + for (const value of [null, {}, { ...observation(), runtimeFlagsSafe: "true" }, { ...observation(), schemaCreate: null }, { ...observation(), extraReady: true }, { ...observation(), tables: null }]) await assert.rejects(check(value), refused) + const tableExtra = observation(); Object.assign(tableExtra.tables[0], { bypass: false }); await assert.rejects(check(tableExtra), refused) + for (const rows of [[], [{ observation: observation() }, { observation: observation() }], [{ observation: observation(), extra: true }]]) await assert.rejects(assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => ({ rows }) }, expected, () => {}), refused) +}) + +test("expected names stay bounded parameters and are captured before async SQL", async () => { + let calls = 0 + const invalid = ["", " ", "x".repeat(64), "x\u0000y", "x\ny", "я".repeat(32)] + for (const name of invalid) await assert.rejects(assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, { ...expected, runtimeRole: name }, () => {}), refused) + await assert.rejects(assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, { ...expected, ownerRole: expected.runtimeRole }, () => {}), refused) + assert.equal(calls, 0) + const pending = deferred<{ rows: Record[] }>(), mutable = { ...expected } + const result = assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => pending.promise }, mutable, () => {}) + mutable.ownerRole = "changed"; pending.resolve({ rows: [{ observation: observation() }] }); await result + const quoted = { databaseName: "db'quoted", runtimeRole: "runtime';select", ownerRole: 'owner"quoted' } + await assertBandwidthExternalNodeRegistryRuntimeRole({ query: async (text, values) => { assert.deepEqual(values, [quoted.databaseName, quoted.runtimeRole, quoted.ownerRole]); assert.ok(!text.includes(quoted.runtimeRole)); return { rows: [{ observation: { ...observation(), databaseName: quoted.databaseName, sessionRole: quoted.runtimeRole, currentRole: quoted.runtimeRole, schemaOwner: quoted.ownerRole, tables: observation().tables.map(row => ({ ...row, ownerRole: quoted.ownerRole })) } }] } } }, quoted, () => {}) +}) + +test("expired owners cannot start SQL or accept a late successful observation", async () => { + let calls = 0 + const expired = new Error("closed live owner") + await assert.rejects(assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, expected, () => { throw expired }), error => error === expired) + assert.equal(calls, 0) + const pending = deferred<{ rows: Record[] }>() + let active = true + const result = assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => pending.promise }, expected, () => { if (!active) throw expired }) + active = false; const rejected = assert.rejects(result, error => error === expired); pending.resolve({ rows: [{ observation: observation() }] }); await rejected +}) + +test("SQL failure still performs the post-await live fence and returns no provider details", async () => { + let checks = 0 + await assert.rejects(assertBandwidthExternalNodeRegistryRuntimeRole({ query: async () => { throw new Error("SECRET-provider-details") } }, expected, () => { checks++ }), error => refused(error) && !String(error).includes("SECRET")) + assert.equal(checks, 2) +}) + diff --git a/backend/src/services/bandwidth-external-node-registry-role.ts b/backend/src/services/bandwidth-external-node-registry-role.ts new file mode 100644 index 00000000..ea5e642a --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry-role.ts @@ -0,0 +1,119 @@ +/** Catalog observation only. This is neither a control generation issuer nor a + * readiness/native capability. The caller owns its bounded SQL facade and must + * run this assertion at the relevant live authority boundary. No default I/O. + * Only registry relations participate in its least-privilege matrix: legitimate + * core bandwidth DML is independently checked by the existing core source. */ +export interface BandwidthExternalNodeRegistryRoleSql { + query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> +} +export interface BandwidthExternalNodeRegistryRoleExpected { databaseName: string; runtimeRole: string; ownerRole: string } +export class BandwidthExternalNodeRegistryRuntimeRoleRefused extends Error { + readonly code = "BANDWIDTH_EXTERNAL_NODE_REGISTRY_RUNTIME_ROLE_REFUSED" + constructor() { super("Bandwidth node registry runtime role refused") } +} +const TABLES = ["bandwidth_external_node_aliases", "bandwidth_nodes", "bandwidth_external_node_events", "bandwidth_node_authority"] as const +/** Names are parameters; catalog paths, namespace, relations and privilege + * vocabulary are fixed. ACL-provided privilege names include newer PostgreSQL + * privileges without sending unsupported names to an older server. */ +export const BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL = `WITH + runtime AS (SELECT r.* FROM pg_catalog.pg_roles r WHERE r.rolname=$2 AND pg_catalog.current_database()::text=$1::text), + expected_owner AS (SELECT r.oid FROM pg_catalog.pg_roles r WHERE r.rolname=$3), + control_schema AS (SELECT n.* FROM pg_catalog.pg_namespace n WHERE n.nspname='mmapp_control'), + required(name,allowed) AS (VALUES + ('bandwidth_external_node_aliases',ARRAY['SELECT','INSERT','UPDATE']::text[]), + ('bandwidth_nodes',ARRAY['SELECT','INSERT']::text[]), + ('bandwidth_external_node_events',ARRAY['SELECT','INSERT']::text[]), + ('bandwidth_node_authority',ARRAY['SELECT']::text[])), + relations AS ( + SELECT e.name,e.allowed,c.oid,c.relowner,c.relacl,c.relkind + FROM required e LEFT JOIN control_schema n ON true + LEFT JOIN pg_catalog.pg_class c ON c.relnamespace=n.oid AND c.relname=e.name), + member_roles AS ( + SELECT r.* FROM pg_catalog.pg_roles r CROSS JOIN runtime rt + WHERE r.oid=rt.oid OR pg_catalog.pg_has_role(rt.oid,r.oid,'MEMBER')), + permissions AS ( + SELECT r.name,m.oid AS role_oid, + COALESCE(r.oid IS NOT NULL AND r.relkind IN ('r','p') AND NOT EXISTS ( + SELECT 1 FROM ( + SELECT pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','DELETE','TRUNCATE','REFERENCES','TRIGGER']::text[]) AS privilege + UNION SELECT a.privilege_type FROM pg_catalog.aclexplode(COALESCE(r.relacl,pg_catalog.acldefault('r',r.relowner))) a + ) p WHERE pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege) IS DISTINCT FROM (p.privilege=ANY(r.allowed)) + ),false) AS exact_table, + COALESCE(EXISTS ( + SELECT 1 FROM ( + SELECT pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','DELETE','TRUNCATE','REFERENCES','TRIGGER']::text[]) AS privilege + UNION SELECT a.privilege_type FROM pg_catalog.aclexplode(COALESCE(r.relacl,pg_catalog.acldefault('r',r.relowner))) a + ) p WHERE (pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege) AND NOT p.privilege=ANY(r.allowed)) + OR pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege || ' WITH GRANT OPTION') + ) OR EXISTS ( + SELECT 1 FROM pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','REFERENCES']::text[]) p(privilege) + WHERE (pg_catalog.has_any_column_privilege(m.oid,r.oid,p.privilege) AND NOT p.privilege=ANY(r.allowed)) + OR pg_catalog.has_any_column_privilege(m.oid,r.oid,p.privilege || ' WITH GRANT OPTION') + ),true) AS forbidden + FROM relations r CROSS JOIN member_roles m) + SELECT pg_catalog.jsonb_build_object( + 'databaseName',pg_catalog.current_database(), + 'sessionRole',SESSION_USER::text,'currentRole',CURRENT_USER::text, + 'runtimeRoleExists',EXISTS(SELECT 1 FROM runtime), + 'ownerRoleExists',EXISTS(SELECT 1 FROM expected_owner), + 'runtimeFlagsSafe',COALESCE((SELECT NOT (rolsuper OR rolcreaterole OR rolcreatedb OR rolreplication OR rolbypassrls) FROM runtime),false), + 'ownerMembership',COALESCE((SELECT pg_catalog.pg_has_role(rt.oid,o.oid,'MEMBER') FROM runtime rt CROSS JOIN expected_owner o),true), + 'dangerousMembership',EXISTS ( + SELECT 1 FROM member_roles m WHERE m.rolsuper OR m.rolcreaterole OR m.rolcreatedb OR m.rolreplication OR m.rolbypassrls + OR m.rolname IN ('pg_read_server_files','pg_write_server_files','pg_execute_server_program','pg_read_all_data','pg_write_all_data','pg_maintain','pg_signal_backend','pg_checkpoint','pg_create_subscription','pg_database_owner','pg_use_reserved_connections') + OR EXISTS(SELECT 1 FROM control_schema n WHERE n.nspowner=m.oid OR pg_catalog.has_schema_privilege(m.oid,n.oid,'CREATE')) + OR EXISTS(SELECT 1 FROM relations r WHERE r.relowner=m.oid) + OR EXISTS(SELECT 1 FROM permissions p WHERE p.role_oid=m.oid AND p.forbidden)), + 'schemaExists',EXISTS(SELECT 1 FROM control_schema), + 'schemaOwner',(SELECT o.rolname::text FROM control_schema n JOIN pg_catalog.pg_roles o ON o.oid=n.nspowner), + 'schemaUsage',COALESCE((SELECT pg_catalog.has_schema_privilege(rt.oid,n.oid,'USAGE') FROM runtime rt CROSS JOIN control_schema n),false), + 'schemaCreate',COALESCE((SELECT pg_catalog.has_schema_privilege(rt.oid,n.oid,'CREATE') FROM runtime rt CROSS JOIN control_schema n),true), + 'publicSchemaUsage',EXISTS(SELECT 1 FROM control_schema n CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(n.nspacl,pg_catalog.acldefault('n',n.nspowner))) a WHERE a.grantee=0 AND a.privilege_type='USAGE'), + 'publicSchemaCreate',EXISTS(SELECT 1 FROM control_schema n CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(n.nspacl,pg_catalog.acldefault('n',n.nspowner))) a WHERE a.grantee=0 AND a.privilege_type='CREATE'), + 'tables',(SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object( + 'name',r.name,'exists',r.oid IS NOT NULL AND r.relkind IN ('r','p'), + 'ownerRole',o.rolname::text,'ownedByRuntime',COALESCE(r.relowner=rt.oid,true), + 'privilegesExact',COALESCE(p.exact_table AND NOT p.forbidden,false)) ORDER BY r.name COLLATE "C") + FROM relations r CROSS JOIN runtime rt LEFT JOIN pg_catalog.pg_roles o ON o.oid=r.relowner + LEFT JOIN permissions p ON p.name=r.name AND p.role_oid=rt.oid) + ) AS observation` + +function refuse(): never { throw new BandwidthExternalNodeRegistryRuntimeRoleRefused() } +const plain = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +const label = (value: unknown): value is string => typeof value === "string" && value.trim() === value && value.length > 0 && Buffer.byteLength(value) <= 63 && !/[\x00-\x1f\x7f]/.test(value) +const exactKeys = (value: Record, fields: string[]) => Object.keys(value).sort().join(",") === [...fields].sort().join(",") + +export function captureBandwidthExternalNodeRegistryRoleExpected(expected: BandwidthExternalNodeRegistryRoleExpected): BandwidthExternalNodeRegistryRoleExpected { + if (!plain(expected) || !exactKeys(expected, ["databaseName", "runtimeRole", "ownerRole"])) refuse() + const captured = { databaseName: expected.databaseName, runtimeRole: expected.runtimeRole, ownerRole: expected.ownerRole } + if (!Object.values(captured).every(label) || captured.runtimeRole === captured.ownerRole) refuse() + return captured +} + +export async function assertBandwidthExternalNodeRegistryRuntimeRole(sql: BandwidthExternalNodeRegistryRoleSql, expected: BandwidthExternalNodeRegistryRoleExpected, live: () => void): Promise { + const captured = captureBandwidthExternalNodeRegistryRoleExpected(expected) + live() + let result: { rows: Record[] } + try { result = await sql.query(BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL, [captured.databaseName, captured.runtimeRole, captured.ownerRole]) } + catch { live(); refuse() } + live() + validateBandwidthExternalNodeRegistryRuntimeRoleRows(result?.rows, captured) +} + +export function validateBandwidthExternalNodeRegistryRuntimeRoleRows(rows: unknown, captured: Readonly): void { + if (!Array.isArray(rows) || rows.length !== 1 || !plain(rows[0]) || !exactKeys(rows[0], ["observation"])) refuse() + const value = rows[0].observation + if (!plain(value) || !exactKeys(value, ["databaseName","sessionRole","currentRole","runtimeRoleExists","ownerRoleExists","runtimeFlagsSafe","ownerMembership","dangerousMembership","schemaExists","schemaOwner","schemaUsage","schemaCreate","publicSchemaUsage","publicSchemaCreate","tables"])) refuse() + if (value.databaseName !== captured.databaseName || value.sessionRole !== captured.runtimeRole || value.currentRole !== captured.runtimeRole + || value.runtimeRoleExists !== true || value.ownerRoleExists !== true || value.runtimeFlagsSafe !== true + || value.ownerMembership !== false || value.dangerousMembership !== false || value.schemaExists !== true || value.schemaOwner !== captured.ownerRole + || value.schemaUsage !== true || value.schemaCreate !== false || value.publicSchemaUsage !== false || value.publicSchemaCreate !== false + || !Array.isArray(value.tables) || value.tables.length !== TABLES.length) refuse() + const seen = new Set() + for (const row of value.tables as unknown[]) { + if (!plain(row) || !exactKeys(row, ["name","exists","ownerRole","ownedByRuntime","privilegesExact"]) + || typeof row.name !== "string" || !TABLES.includes(row.name as typeof TABLES[number]) || seen.has(row.name) + || row.exists !== true || row.ownerRole !== captured.ownerRole || row.ownedByRuntime !== false || row.privilegesExact !== true) refuse() + seen.add(row.name as string) + } +} diff --git a/backend/src/services/bandwidth-external-node-registry-schema.ts b/backend/src/services/bandwidth-external-node-registry-schema.ts new file mode 100644 index 00000000..686d45cc --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry-schema.ts @@ -0,0 +1,110 @@ +/** Fresh-only, explicit owner installation. Requires the original registry and core guards. + * No seed, migration registration, occupancy mutation or automatic execution. */ +export const BANDWIDTH_EXTERNAL_NODE_REGISTRY_TABLES = Object.freeze(["bandwidth_external_node_aliases", "bandwidth_external_node_events"] as const) +export const BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL = ` +CREATE TABLE mmapp_control.bandwidth_external_node_aliases ( + enrollment_id uuid PRIMARY KEY, + incarnation uuid NOT NULL UNIQUE, + node_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_nodes(node_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + status text COLLATE "C" NOT NULL CHECK (status IN ('active','tombstoned','quarantined')), + record_mac text COLLATE "C" NOT NULL CHECK (record_mac ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE INDEX bandwidth_external_node_aliases_node_idx ON mmapp_control.bandwidth_external_node_aliases(node_id); +CREATE TABLE mmapp_control.bandwidth_external_node_events ( + event_id uuid PRIMARY KEY, + enrollment_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_external_node_aliases(enrollment_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + incarnation uuid NOT NULL, + node_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_nodes(node_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + kind text COLLATE "C" NOT NULL CHECK (kind IN ('enroll','tombstone','quarantine')), + old_status text COLLATE "C" CHECK (old_status='active'), + new_status text COLLATE "C" NOT NULL CHECK (new_status IN ('active','tombstoned','quarantined')), + authority_proof text COLLATE "C" NOT NULL CHECK (authority_proof ~ '^[a-f0-9]{64}$'), + event_mac text COLLATE "C" NOT NULL CHECK (event_mac ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + UNIQUE (enrollment_id,kind), + CHECK (COALESCE( + (kind='enroll' AND old_status IS NULL AND new_status='active') + OR (kind='tombstone' AND old_status='active' AND new_status='tombstoned') + OR (kind='quarantine' AND old_status='active' AND new_status='quarantined'), false)) +); +CREATE INDEX bandwidth_external_node_events_node_idx ON mmapp_control.bandwidth_external_node_events(node_id); + +CREATE FUNCTION mmapp_control.bandwidth_external_node_alias_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +BEGIN + IF TG_OP='DELETE' THEN RAISE EXCEPTION 'bandwidth node aliases cannot be deleted' USING ERRCODE='23514'; END IF; + IF TG_OP='INSERT' THEN + IF NEW.status <> 'active' THEN RAISE EXCEPTION 'bandwidth node alias must start active' USING ERRCODE='23514'; END IF; + -- Keep the reference invariant under replica mode as well: ordinary FK + -- implementation triggers are not ALWAYS, while this row guard is. + -- Nodes cannot be updated or deleted, so this existence check needs no row + -- lock (and must not require UPDATE rights for the registry runtime). + PERFORM 1 FROM mmapp_control.bandwidth_nodes WHERE node_id=NEW.node_id; + IF NOT FOUND THEN RAISE EXCEPTION 'bandwidth node alias requires its original node' USING ERRCODE='23514'; END IF; + RETURN NEW; + END IF; + IF ROW(NEW.enrollment_id,NEW.incarnation,NEW.node_id,NEW.created_at) + IS DISTINCT FROM ROW(OLD.enrollment_id,OLD.incarnation,OLD.node_id,OLD.created_at) + OR OLD.status <> 'active' OR NEW.status NOT IN ('tombstoned','quarantined') + OR NEW.record_mac IS NOT DISTINCT FROM OLD.record_mac THEN + RAISE EXCEPTION 'bandwidth node alias cannot be rebound or reactivated' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE FUNCTION mmapp_control.bandwidth_external_node_event_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +DECLARE alias_row mmapp_control.bandwidth_external_node_aliases%ROWTYPE; +BEGIN + IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'bandwidth node events are append only' USING ERRCODE='23514'; END IF; + SELECT * INTO alias_row FROM mmapp_control.bandwidth_external_node_aliases WHERE enrollment_id=NEW.enrollment_id FOR UPDATE; + IF NOT FOUND OR alias_row.incarnation <> NEW.incarnation OR alias_row.node_id <> NEW.node_id OR alias_row.status <> NEW.new_status THEN + RAISE EXCEPTION 'bandwidth node event does not match its original alias' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE FUNCTION mmapp_control.bandwidth_external_node_alias_consistency_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +DECLARE target_id uuid; alias_row mmapp_control.bandwidth_external_node_aliases%ROWTYPE; + total bigint; enrollments bigint; terminals bigint; terminal mmapp_control.bandwidth_external_node_events%ROWTYPE; +BEGIN + IF TG_OP='DELETE' THEN target_id:=OLD.enrollment_id; ELSE target_id:=NEW.enrollment_id; END IF; + SELECT * INTO alias_row FROM mmapp_control.bandwidth_external_node_aliases WHERE enrollment_id=target_id FOR UPDATE; + IF NOT FOUND THEN RAISE EXCEPTION 'bandwidth node alias missing' USING ERRCODE='23514'; END IF; + SELECT count(*),count(*) FILTER (WHERE kind='enroll'),count(*) FILTER (WHERE kind IN ('tombstone','quarantine')) + INTO total,enrollments,terminals FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=target_id; + IF enrollments <> 1 OR terminals > 1 OR total <> enrollments+terminals OR total > 2 + OR EXISTS(SELECT 1 FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=target_id + AND (incarnation <> alias_row.incarnation OR node_id <> alias_row.node_id)) THEN + RAISE EXCEPTION 'bandwidth node alias event chain is incomplete' USING ERRCODE='23514'; + END IF; + IF alias_row.status='active' THEN + IF total <> 1 OR terminals <> 0 THEN RAISE EXCEPTION 'active bandwidth alias has a terminal event' USING ERRCODE='23514'; END IF; + ELSE + SELECT * INTO terminal FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=target_id AND kind IN ('tombstone','quarantine'); + IF terminals <> 1 OR terminal.old_status IS DISTINCT FROM 'active' OR terminal.new_status IS DISTINCT FROM alias_row.status + OR (alias_row.status='tombstoned' AND terminal.kind IS DISTINCT FROM 'tombstone') + OR (alias_row.status='quarantined' AND terminal.kind IS DISTINCT FROM 'quarantine') THEN + RAISE EXCEPTION 'terminal bandwidth node alias event mismatch' USING ERRCODE='23514'; + END IF; + END IF; + RETURN NULL; +END; +$body$; + +CREATE TRIGGER bandwidth_external_node_alias_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_external_node_aliases FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_external_node_alias_guard(); +CREATE TRIGGER bandwidth_external_node_event_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_external_node_events FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_external_node_event_guard(); +CREATE TRIGGER bandwidth_external_node_aliases_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_external_node_aliases FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE TRIGGER bandwidth_external_node_events_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_external_node_events FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE CONSTRAINT TRIGGER bandwidth_external_node_alias_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_external_node_aliases DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_external_node_alias_consistency_guard(); +CREATE CONSTRAINT TRIGGER bandwidth_external_node_event_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_external_node_events DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_external_node_alias_consistency_guard(); + +ALTER TABLE mmapp_control.bandwidth_external_node_aliases ENABLE ALWAYS TRIGGER bandwidth_external_node_alias_guard; +ALTER TABLE mmapp_control.bandwidth_external_node_events ENABLE ALWAYS TRIGGER bandwidth_external_node_event_guard; +ALTER TABLE mmapp_control.bandwidth_external_node_aliases ENABLE ALWAYS TRIGGER bandwidth_external_node_aliases_no_truncate; +ALTER TABLE mmapp_control.bandwidth_external_node_events ENABLE ALWAYS TRIGGER bandwidth_external_node_events_no_truncate; +ALTER TABLE mmapp_control.bandwidth_external_node_aliases ENABLE ALWAYS TRIGGER bandwidth_external_node_alias_consistency; +ALTER TABLE mmapp_control.bandwidth_external_node_events ENABLE ALWAYS TRIGGER bandwidth_external_node_event_consistency; +` diff --git a/backend/src/services/bandwidth-external-node-registry.sql-helper.ts b/backend/src/services/bandwidth-external-node-registry.sql-helper.ts new file mode 100644 index 00000000..e2983e92 --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry.sql-helper.ts @@ -0,0 +1,211 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +/** Disposable localhost acceptance only. No database module is loaded until + * the runner's owned DB/state guard passes. Never used by production hosts. */ +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomBytes, randomUUID, sign } from "node:crypto" +import { createServer } from "node:http" +import { writeFile } from "node:fs/promises" +import { resolve } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { performance } from "node:perf_hooks" +import { once } from "node:events" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import type { BandwidthNodeSourceSnapshot } from "./bandwidth-node-observation.js" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState, canonical, scope } from "./bandwidth-admission-process.sql-helper.js" + +export async function createOwnedExternalRegistryHarness(){ + assertOwnedBandwidthSql();assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY,"true");await prepareOwnedBandwidthState() + const {pool:bootstrapAdmin,closePool}=await import("../db/index.js"),{Pool}=await import("pg") + const {applySqlMigrations}=await import("../db/migrate.js") + const {BANDWIDTH_CONTROL_SOURCE_DDL,createConfiguredBandwidthControlSource,canonicalBandwidthControlRecord,bandwidthControlEndpointSha256,bandwidthControlSchemaSourceSha256,observeBandwidthControlCatalog}=await import("./bandwidth-control-source.js") + const {createConfiguredBandwidthNodeSource}=await import("./bandwidth-node-source.js") + const {canonicalBandwidthNodeValue,canonicalBandwidthNodeBootstrap,bandwidthPeerRegistrySchemaSourceSha256,bandwidthNodeRegistrySchemaSourceSha256}=await import("./bandwidth-node-bootstrap.js") + const {observeBandwidthPeerRegistryCatalog}=await import("./bandwidth-node-registry-catalog.js") + const {BANDWIDTH_ADMISSION_PROPOSAL_DDL}=await import("./bandwidth-admission-schema.js") + const {BANDWIDTH_NODE_REGISTRY_DDL}=await import("./bandwidth-node-registry-schema.js") + const {BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL}=await import("./bandwidth-external-node-registry-schema.js") + const {createBandwidthNodeRegistry}=await import("./bandwidth-node-registry.js") + const {createBandwidthExternalNodeRegistry}=await import("./bandwidth-external-node-registry.js") + const {createBandwidthNodeObservationReader}=await import("./bandwidth-node-observation.js") + const {parseBandwidthPeerRef}=await import("./bandwidth-peer-reference.js") + const {createBandwidthPeerObservationReader}=await import("./bandwidth-peer-observation.js") + const {createProbeStorageConnectionConnector}=await import("./server-probe-storage-connection.js") + await import("./mikrotik.js") + const suffix=randomUUID().replaceAll("-",""),ownerRole="bex_owner_"+suffix,managedRole="bex_managed_"+suffix,externalRole="bex_external_"+suffix,sourceRole="bex_source_"+suffix,appRole="bex_app_"+suffix,databaseName="bex_control_"+suffix + const ident=(v:string)=>{assert(/^bex_(?:owner|managed|external|source|app|control)_[a-f0-9]{32}$/.test(v));return '"'+v+'"'} + const roleUrls=new Map() + const roles:string[]=[],pools:InstanceType[]=[],key=randomBytes(32),installationId=randomUUID(),issuerKeySha256=createHash("sha256").update(key).digest("hex"),quiescenceProof="b".repeat(64) + const root={installationId,issuerKeySha256,quiescenceProof},expiresAt=Date.now()+300000 + const mac=(purpose:string,value:unknown)=>createHmac("sha256",key).update("mmapp-bandwidth-node-"+purpose+"-v1:"+canonicalBandwidthNodeValue(value)).digest("hex") + const managedSources=new Map(),externalSources=new Map(),licenses=new Map>() + let nativeGets=0,nativePosts=0,btestMode:"terminal"|"drop"="terminal",lastNativeBody:unknown,serverStarted=false,databaseCreated=false,admin:InstanceType|undefined,owner:InstanceType|undefined + let beforeBtestResponse:(()=>Promise)|undefined,responseDelayMs=3100 + const configuredNodes:Array>=[],configuredCores:Array>=[] + const routerGuardCompatibilityCleanups:Array<()=>Promise>=[] + const server=createServer(async(req,res)=>{ + try{ + const auth=Buffer.from((req.headers.authorization??"").slice(6),"base64").toString(),user=auth.split(":")[0] + assert(licenses.has(user));assert.equal(auth,user+":owned-localhost-only") + res.setHeader("content-type","application/json") + if(req.method==="GET"){assert.equal(req.url,"/rest/system/license");nativeGets++;res.end(JSON.stringify(licenses.get(user)));return} + assert.equal(req.method,"POST");assert.equal(req.url,"/rest/tool/bandwidth-test");assert(user.startsWith("managed-")) + let raw="";for await(const chunk of req){raw+=String(chunk);assert(Buffer.byteLength(raw)<=65536)} + lastNativeBody=JSON.parse(raw);nativePosts++ + if(btestMode==="drop"){res.destroy();return} + await beforeBtestResponse?.() + await delay(responseDelayMs);res.end(JSON.stringify([{status:"done testing","tx-total-average":"1000","rx-total-average":"2000"}])) + }catch{res.destroy()} + }) + const managedReader=createBandwidthNodeObservationReader({async read(id){return managedSources.get(id)}}) + /** Fixed owned management source only. Operator authority must be verified + * before invoking this fixture accessor; it never grants that authority. */ + const readPeerSnapshot=async(rawPeer:import("./bandwidth-peer-reference.js").BandwidthPeerRef,current:import("./bandwidth-admission.js").BandwidthScope):Promise=>{ + assert(current&&Number.isSafeInteger(current.deadlineAt));current.signal?.throwIfAborted();assert(Date.now()(done=>server.close(()=>done()))} + for(const source of configuredNodes)source.close();for(const core of configuredCores)await core.close() + for(const cleanup of routerGuardCompatibilityCleanups.reverse())await cleanup() + for(const p of pools)await p.end() + if(databaseCreated)await bootstrapAdmin.query(`DROP DATABASE ${ident(databaseName)}`) + for(const role of [...roles].reverse()){await bootstrapAdmin.query(`DROP OWNED BY ${ident(role)}`);await bootstrapAdmin.query(`DROP ROLE ${ident(role)}`)} + key.fill(0);await closePool() + } + try{ + await applySqlMigrations(bootstrapAdmin);assert.equal(Number((await bootstrapAdmin.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n),MIGRATION_MANIFEST.length) + const created=new Map>() + for(const role of [ownerRole,managedRole,externalRole,sourceRole,appRole]){ + const password=randomBytes(32).toString("hex");await bootstrapAdmin.query(`CREATE ROLE ${ident(role)} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS NOINHERIT`);roles.push(role) + const url=new URL(process.env.DATABASE_URL!);url.username=role;url.password=password;if(role!==appRole)url.pathname="/"+databaseName;roleUrls.set(role,url.toString()) + } + await bootstrapAdmin.query(`CREATE DATABASE ${ident(databaseName)} OWNER ${ident(ownerRole)} TEMPLATE template0`);databaseCreated=true + for(const role of [ownerRole,managedRole,externalRole,sourceRole]){const pool=new Pool({connectionString:roleUrls.get(role),max:4});pools.push(pool);created.set(role,pool)} + const adminUrl=new URL(process.env.DATABASE_URL!);adminUrl.pathname="/"+databaseName;admin=new Pool({connectionString:adminUrl.toString(),max:4});pools.push(admin) + owner=created.get(ownerRole)!;const managed=created.get(managedRole)!,external=created.get(externalRole)! + await owner.query("CREATE SCHEMA mmapp_control");await owner.query(BANDWIDTH_ADMISSION_PROPOSAL_DDL);await owner.query(BANDWIDTH_CONTROL_SOURCE_DDL);await owner.query(BANDWIDTH_NODE_REGISTRY_DDL);await owner.query(BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL) + await owner.query("REVOKE ALL ON SCHEMA mmapp_control FROM PUBLIC; REVOKE ALL ON ALL TABLES IN SCHEMA mmapp_control FROM PUBLIC; REVOKE ALL ON ALL FUNCTIONS IN SCHEMA mmapp_control FROM PUBLIC") + for(const [role,prefix]of [[managedRole,"bandwidth_node"],[externalRole,"bandwidth_external_node"],[sourceRole,"bandwidth_node"],[sourceRole,"bandwidth_external_node"]]){ + await owner.query(`GRANT USAGE ON SCHEMA mmapp_control TO ${ident(role)}`) + await owner.query(`GRANT SELECT ON mmapp_control.bandwidth_node_authority TO ${ident(role)}`) + await owner.query(`GRANT SELECT,INSERT ON mmapp_control.bandwidth_nodes,mmapp_control.${prefix}_events TO ${ident(role)}`) + await owner.query(`GRANT SELECT,INSERT,UPDATE ON mmapp_control.${prefix}_aliases TO ${ident(role)}`) + for(const fn of [prefix+"_alias_guard",prefix+"_event_guard",prefix+"_alias_consistency_guard","bandwidth_admission_reject_truncate"]) + await owner.query(`GRANT EXECUTE ON FUNCTION mmapp_control.${fn}() TO ${ident(role)}`) + } + await owner.query(`GRANT SELECT,INSERT,UPDATE ON mmapp_control.bandwidth_attempts TO ${ident(sourceRole)}`) + await owner.query(`GRANT SELECT,INSERT,DELETE ON mmapp_control.bandwidth_node_occupancy TO ${ident(sourceRole)}`) + await owner.query(`GRANT SELECT,INSERT ON mmapp_control.bandwidth_attempt_events TO ${ident(sourceRole)}`) + await owner.query(`GRANT SELECT ON mmapp_control.bandwidth_source_identity TO ${ident(sourceRole)}`) + await owner.query(`GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA mmapp_control TO ${ident(sourceRole)}`) + await owner.query("INSERT INTO mmapp_control.bandwidth_node_authority(id,installation_id,issuer_key_sha256,quiescence_proof,status) VALUES(1,$1,$2,$3,'active')",[installationId,issuerKeySha256,quiescenceProof]) + server.listen(0,"127.0.0.1");await once(server,"listening");serverStarted=true + const address=server.address();assert(address&&typeof address!=="string");const port=address.port + const endpoint=(user:string)=>({host:"127.0.0.1",port,useSsl:false,verifySsl:true,username:user,password:"owned-localhost-only",routingEndpointEpoch:randomUUID()}) + const managedEndpoint=async(claims:Record)=>{const id=Number((await bootstrapAdmin.query("INSERT INTO public.servers(name,host,enabled) VALUES('owned-external-registry-managed','127.0.0.1',false) RETURNING id")).rows[0].id),user="managed-"+id;await bootstrapAdmin.query("UPDATE public.servers SET port=$2,use_ssl=false,verify_ssl=true,username=$3,password=$4,enabled=true WHERE id=$1",[id,port,user,"owned-localhost-only"]);licenses.set(user,claims);managedSources.set(id,{serverId:id,enabled:true,sourceProof:mac("source",id),authorityGeneration:"owned-managed",endpoint:endpoint(user)});return id} + const credentialEndpoint=async()=>Number((await bootstrapAdmin.query("INSERT INTO public.servers(name,host,enabled,username,password) VALUES('owned-distinct-bandwidth-credentials','127.0.0.1',true,'owned-remote-user','owned-remote-credential') RETURNING id")).rows[0].id) + const applicationRows=async(ids:readonly number[])=>{assert(ids.length>0&&ids.length<=3&&ids.every(id=>Number.isSafeInteger(id)&&id>0));return(await bootstrapAdmin.query("SELECT id,host,port,use_ssl,verify_ssl,username,password,routing_endpoint_epoch::text,enabled FROM public.servers WHERE id=ANY($1::bigint[]) ORDER BY id",[ids])).rows} + const externalEndpoint=(claims:Record)=>{const enrollmentId=randomUUID(),user="external-"+enrollmentId;licenses.set(user,claims);externalSources.set(enrollmentId,{peerRef:{kind:"external",enrollmentId},enabled:true,sourceProof:mac("source",enrollmentId),authorityGeneration:"owned-external",endpoint:endpoint(user)});return enrollmentId} + let revoked=false + const authorize=async()=>{assert(!revoked,"original operator grant revoked");return{...root,authorityProof:mac("operator","fixture-only-original-grant"),authorityGeneration:"owned-operator-grant",expiresAt}} + const connector=(runtime:InstanceType)=>createProbeStorageConnectionConnector(async()=>runtime.connect()) + const managedConnect=connector(managed),externalConnect=connector(external) + const managedHost=createBandwidthNodeRegistry({now:Date.now,connect:managedConnect,inspect:managedReader.inspect,authorize,keyedProof:async(p,v)=>mac(p,v)}) + function externalHost(fault?:"lost"|"rows",hook?:(sql:string)=>Promise){ + let commits=0,rollbacks=0 + const host=createBandwidthExternalNodeRegistry({now:Date.now,inspect:externalReader.inspect,authorize,keyedProof:async(p,v)=>mac(p,v),connect:async window=>{ + const connection=await externalConnect(window) + return{release:(destroy?:boolean)=>connection.release(destroy),async query(sql:string,values?:unknown[],options?:{signal?:AbortSignal;timeoutMs:number}){ + await hook?.(sql);const result=await connection.query(sql,values,options) + if(sql==="ROLLBACK")rollbacks++ + if(sql==="COMMIT"){commits++;if(fault==="lost")throw Error("Owned ACK loss AFTER actual SQL COMMIT");if(fault==="rows")return{rows:result.rows}} + return result + }} + }}) + return{host,commits:()=>commits,rollbacks:()=>rollbacks} + } + async function digest(tables:readonly string[]){const snapshot:Record={};for(const table of tables){assert(/^bandwidth_[a-z_]+$/.test(table));snapshot[table]=(await owner!.query(`SELECT pg_catalog.to_jsonb(t) AS row FROM mmapp_control.${table} t ORDER BY pg_catalog.to_jsonb(t)::text`)).rows}return createHash("sha256").update(canonical(snapshot)).digest("hex")} + /** Planned local diagnostic sampling only: one warmup plus three reads of + * each fixed metadata statement, on the actual combined source role. No + * supplied SQL, retries, authority issuance or host budget modifications. */ + async function profileMetadata(){ + assertOwnedBandwidthSql();assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY,"true") + const {BANDWIDTH_NODE_REGISTRY_ROLE_SQL}=await import("./bandwidth-node-registry-role.js") + const {BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL}=await import("./bandwidth-external-node-registry-role.js") + const {BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL,BANDWIDTH_PEER_REGISTRY_INCOMING_SQL}=await import("./bandwidth-node-registry-catalog.js") + const {BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL}=await import("./bandwidth-external-node-registry-catalog.js") + const {BANDWIDTH_PEER_IDENTITY_SQL}=await import("./bandwidth-peer-identity.js") + const {BANDWIDTH_CONTROL_ROLE_SQL}=await import("./bandwidth-control-role.js") + const {BANDWIDTH_CONTROL_CATALOG_SQL}=await import("./bandwidth-control-catalog.js") + const roleValues=[databaseName,sourceRole,ownerRole] + const components=[ + {name:"managed.role",text:BANDWIDTH_NODE_REGISTRY_ROLE_SQL,values:roleValues,maxRows:1}, + {name:"external.role",text:BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL,values:roleValues,maxRows:1}, + {name:"managed.catalog.v2",text:BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL,values:[],maxRows:234}, + {name:"external.catalog",text:BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL,values:[],maxRows:230}, + {name:"incoming.foreignkeys",text:BANDWIDTH_PEER_REGISTRY_INCOMING_SQL,values:[],maxRows:2}, + {name:"registry.authority",text:"SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status FROM mmapp_control.bandwidth_node_authority WHERE id=1 LIMIT 2",values:[],maxRows:1}, + {name:"peer.identity.combined",text:BANDWIDTH_PEER_IDENTITY_SQL,values:roleValues,maxRows:469}, + {name:"core.role",text:BANDWIDTH_CONTROL_ROLE_SQL,values:roleValues,maxRows:1}, + {name:"core.catalog",text:BANDWIDTH_CONTROL_CATALOG_SQL,values:[],maxRows:230}, + ] + const runtime=created.get(sourceRole)!,window=scope(10000),connection=await connector(runtime)(window),report:Array<{name:string;count:number;warmup:{durationMs:number;rows:number};samples:Array<{durationMs:number;rows:number}>}>=[] + const rounded=(value:number)=>Math.round(value*10)/10,began=performance.now() + try{ + await connection.query("BEGIN READ ONLY",undefined,{timeoutMs:1000}) + assert.equal((await connection.query("SHOW transaction_read_only",undefined,{timeoutMs:1000})).rows[0].transaction_read_only,"on") + for(const component of components){ + const readings:Array<{durationMs:number;rows:number}>=[] + for(let sample=0;sample<4;sample++){ + assert(Date.now()0&&result.rows.length<=component.maxRows) + readings.push({durationMs,rows:result.rows.length}) + } + report.push({name:component.name,count:4,warmup:readings[0],samples:readings.slice(1)}) + } + await connection.query("COMMIT",undefined,{timeoutMs:1000}) + const summary={readOnly:true,fixtureOnly:true,warmups:1,plannedSamples:3,retry:false,statementCount:36,elapsedMs:rounded(performance.now()-began),components:report} + const text=JSON.stringify(summary);assert(Buffer.byteLength(text)<16384) + console.log("Owned v2 peer metadata local microprofile:",text) + }finally{connection.release(true)} + } + async function createSignedSource(){ + const runtime=created.get(sourceRole)!,controlGeneration=randomUUID(),eventKey=randomBytes(32),{publicKey,privateKey}=generateKeyPairSync("ed25519") + const sha=(v:Buffer|string)=>createHash("sha256").update(v).digest("hex"),live=()=>{} + const record={format:"mmapp-bandwidth-control-source-v1",installationId,controlGeneration,databaseName,runtimeRole:sourceRole,ownerRole, + databaseOid:Number((await runtime.query("SELECT oid::text AS id FROM pg_catalog.pg_database WHERE datname=current_database()")).rows[0].id), + endpointSha256:bandwidthControlEndpointSha256(roleUrls.get(sourceRole)!),schemaSourceSha256:bandwidthControlSchemaSourceSha256(),catalogSha256:await observeBandwidthControlCatalog(runtime,live),eventKeySha256:sha(eventKey),attestationRef:"Owned synthetic operator control statement signed Ed25519; no production authority"} + const bootstrap={format:"mmapp-bandwidth-node-bootstrap-v2",...root,registrySchemaSourceSha256:bandwidthPeerRegistrySchemaSourceSha256(),registryCatalogSha256:await observeBandwidthPeerRegistryCatalog(runtime,live),attestationRef:"Owned synthetic operator v2 combined registry statement genuinely signed; not production enrollment"} + const signed=(format:string,bytes:Buffer)=>({format,payload:bytes.toString("base64url"),signature:sign(null,bytes,privateKey).toString("base64url")}) + const signedBootstrap=(value:unknown)=>signed("mmapp-bandwidth-node-bootstrap-envelope-v1",Buffer.from(canonicalBandwidthNodeBootstrap(value))) + const path=(name:string)=>resolve(process.env.MCP_STATE_DIR!,"external-source-"+name),attestationFile=path("core.json"),trustKeyFile=path("trust.pem"),eventKeyFile=path("event.key"),issuerKeyFile=path("issuer.key"),bootstrapFile=path("bootstrap.json"),v1File=path("v1-bootstrap.json"),badPinFile=path("bad-pin-bootstrap.json") + for(const [file,bytes]of [[attestationFile,JSON.stringify(signed("mmapp-bandwidth-control-envelope-v1",Buffer.from(canonicalBandwidthControlRecord(record))))],[trustKeyFile,publicKey.export({type:"spki",format:"pem"})],[eventKeyFile,eventKey],[issuerKeyFile,key],[bootstrapFile,JSON.stringify(signedBootstrap(bootstrap))],[v1File,JSON.stringify(signedBootstrap({...bootstrap,format:"mmapp-bandwidth-node-bootstrap-v1",registrySchemaSourceSha256:bandwidthNodeRegistrySchemaSourceSha256()}))],[badPinFile,JSON.stringify(signedBootstrap({...bootstrap,registryCatalogSha256:"f".repeat(64)}))]] as const)await writeFile(file,bytes,{flag:"wx",mode:0o600}) + await owner!.query("INSERT INTO mmapp_control.bandwidth_source_identity(id,installation_id,control_generation,attestation_sha256,event_key_sha256) VALUES(1,$1,$2,$3,$4)",[installationId,controlGeneration,sha(canonicalBandwidthControlRecord(record)),sha(eventKey)]) + eventKey.fill(0) + const core=createConfiguredBandwidthControlSource({databaseUrl:roleUrls.get(sourceRole)!,applicationDatabaseUrl:roleUrls.get(appRole)!,attestationFile,trustKeyFile,trustKeySha256:sha(publicKey.export({type:"spki",format:"der"})),eventKeyFile});configuredCores.push(core) + const make=(file:string)=>{const node=createConfiguredBandwidthNodeSource(core,{issuerKeyFile,issuerKeySha256,bootstrapFile:file});configuredNodes.push(node);return node} + const node=make(bootstrapFile) + if(process.env.MMAPP_TEST_ROUTER_GUARD_COMPATIBILITY==="true"){ + const {acceptOwnedRouterGuardControlCompatibility}=await import("./router-configuration-guard-compatibility.sql-helper.js") + const accepted=await acceptOwnedRouterGuardControlCompatibility({admin:admin!,databaseName,runtimeRole:sourceRole,ownerRole,v2:true,core,node}) + routerGuardCompatibilityCleanups.push(accepted.cleanup) + console.log("router guard same-control compatibility:",JSON.stringify(accepted.report)) + } + return{core,runtimeRole:sourceRole,node,v1:make(v1File),badPin:make(badPinFile),controlGeneration, + managedInspect:managedReader.inspect,externalInspect:externalReader.inspect,peerInspect:externalReader.inspect, + observer:externalReader,peerObservation:(peer:import("./bandwidth-peer-reference.js").BandwidthPeerRef)=>externalReader.observe(peer,scope())} + } + return{admin,owner,managed,external,databaseName,ownerRole,managedRole,externalRole,root,mac,scope,createSignedSource,profileMetadata,readPeerSnapshot,managedEndpoint,externalEndpoint,credentialEndpoint,applicationRows,managedHost,externalHost, + nativePosts:()=>nativePosts,nativeBodyDigest:()=>createHash("sha256").update(canonicalBandwidthNodeValue(lastNativeBody)).digest("hex"),setBtestMode:(mode:"terminal"|"drop")=>{btestMode=mode}, + setBeforeBtestResponse:(hook:(()=>Promise)|undefined,delayMs=3100)=>{assert(Number.isSafeInteger(delayMs)&&delayMs>=0&&delayMs<=6000);beforeBtestResponse=hook;responseDelayMs=delayMs}, + managedObservation:(id:number)=>managedReader.observe(id,scope()),externalObservation:(id:string)=>externalReader.observe({kind:"external",enrollmentId:id},scope()), + digest,nativeGets:()=>nativeGets,revoke:()=>{revoked=true},close} + }catch(error){await close();throw error} +} diff --git a/backend/src/services/bandwidth-external-node-registry.sql.test.ts b/backend/src/services/bandwidth-external-node-registry.sql.test.ts new file mode 100644 index 00000000..f3e7b64f --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry.sql.test.ts @@ -0,0 +1,281 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import type { BandwidthExternalNodeRegistryReceipt } from "./bandwidth-external-node-registry.js" +import type { BandwidthResolvedPeerFence } from "./bandwidth-peer-fence.js" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createOwnedExternalRegistryHarness } from "./bandwidth-external-node-registry.sql-helper.js" +import { assertOwnedBandwidthSql, createOwnedBandwidthHarness, fixtureBinding } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql();assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY,"true") +const f=await createOwnedExternalRegistryHarness(),cases:string[]=[] +const {BandwidthExternalNodeRegistryUnknown,createBandwidthExternalNodeRegistry}=await import("./bandwidth-external-node-registry.js") +const {createBandwidthNodeRegistry}=await import("./bandwidth-node-registry.js") +const {observeBandwidthExternalNodeRegistryCatalog}=await import("./bandwidth-external-node-registry-catalog.js") +const {observeBandwidthNodeRegistryCatalog,observeBandwidthPeerRegistryCatalog}=await import("./bandwidth-node-registry-catalog.js") +const {assertBandwidthExternalNodeRegistryRuntimeRole}=await import("./bandwidth-external-node-registry-role.js") +const {assertBandwidthNodeRegistryRuntimeRole}=await import("./bandwidth-node-registry-role.js") +const coreTables=["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events"] +const extensionTables=["bandwidth_external_node_aliases","bandwidth_external_node_events"] +const allTables=[...coreTables,"bandwidth_source_identity","bandwidth_node_authority","bandwidth_nodes","bandwidth_node_aliases","bandwidth_node_events",...extensionTables] +const roleExpected={databaseName:f.databaseName,runtimeRole:f.externalRole,ownerRole:f.ownerRole} +const live=()=>{} +const host=f.externalHost().host +let acceptExecution!:()=>Promise<{checks:string[];posts:number;productionContextProvider:boolean;productionMappingAttestor:boolean}> +let acceptOperator!:()=>Promise +let acceptWriter!:()=>Promise +let acceptPeers!:(id:string,receipt:BandwidthExternalNodeRegistryReceipt,reject:boolean,changePin?:boolean)=>Promise +try{ + await assertBandwidthExternalNodeRegistryRuntimeRole(f.external,roleExpected,live) + await assertBandwidthNodeRegistryRuntimeRole(f.managed,{...roleExpected,runtimeRole:f.managedRole},live) + for(const runtime of [f.managed,f.external]){ + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_nodes SET claim_proof=claim_proof")) + await assert.rejects(runtime.query("SELECT * FROM mmapp_control.bandwidth_node_occupancy")) + await assert.rejects(runtime.query("DELETE FROM mmapp_control.bandwidth_nodes")) + } + await assert.rejects(f.external.query("SELECT * FROM mmapp_control.bandwidth_node_aliases")) + await assert.rejects(f.managed.query("SELECT * FROM mmapp_control.bandwidth_external_node_aliases")) + cases.push("explicit non-superuser owner plus separate managed/external LOGIN roles: exact table ACLs, no core occupancy or foreign alias access") + const claims={"system-id":"OWNED-SHARED-SYSTEM","software-id":"OWNED-SHARED-SOFTWARE"} + const managedId=await f.managedEndpoint(claims),externalId=f.externalEndpoint(claims) + const managedObservation=await f.managedObservation(managedId),externalObservation=await f.externalObservation(externalId) + const [managed,external]=await Promise.all([f.managedHost.register(managedObservation,f.scope()),host.register(externalObservation,f.scope())]) + assert.equal(managed.canonicalNodeId,external.canonicalNodeId);assert.notEqual(managed.incarnation,external.incarnation) + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_nodes")).rows[0].n),1) + assert.equal((await host.resolve(await f.externalObservation(externalId),f.scope())).canonicalNodeId,managed.canonicalNodeId) + assert.equal((await host.register(await f.externalObservation(externalId),f.scope())).outcome,"existing") + cases.push("genuine localhost v1/v2 license GETs; independent PG sessions concurrently share original typed claim/node MAC issuer and advisory lock, yielding exactly one canonical node") + { + const missing=f.externalEndpoint({"system-id":"OWNED-MISSING"}),before=await f.digest(allTables) + await assert.rejects(host.resolve(await f.externalObservation(missing),f.scope())) + await assert.rejects(host.register(JSON.parse(JSON.stringify(await f.externalObservation(missing))),f.scope())) + assert.equal(await f.digest(allTables),before) + const distinct=f.externalEndpoint({"software-id":"OWNED-SHARED-SYSTEM"}) + assert.notEqual((await host.register(await f.externalObservation(distinct),f.scope())).canonicalNodeId,managed.canonicalNodeId) + cases.push("resolve absent and serialized private observation never auto-enroll; typed software-only identity does not collapse with system/full vector") + } + { + const signed=await f.createSignedSource() + acceptExecution=async()=>{const {acceptBandwidthPeerExecutionHost}=await import("./bandwidth-peer-execution-host.sql-helper.js");return acceptBandwidthPeerExecutionHost(f,signed)} + acceptOperator=async()=>{const {acceptOwnedOperatorRegistry}=await import("./bandwidth-operator-registry.sql-helper.js");return acceptOwnedOperatorRegistry(f,signed)} + acceptWriter=async()=>{const {acceptOwnedOperatorWriterComposition}=await import("./bandwidth-operator-writer-composition.sql-helper.js");return acceptOwnedOperatorWriterComposition(f,signed)} + await assert.rejects(signed.v1.open(f.scope())) + await assert.rejects(signed.badPin.open(f.scope())) + const originalExpiresAt=Date.now()+60000 + const signedAuthority=async()=>({...f.root,authorityProof:f.mac("signed-original-operator",claims),authorityGeneration:"owned-original-signed-operation",expiresAt:originalExpiresAt}) + const signedExternalId=f.externalEndpoint(claims),externalToken=await f.externalObservation(signedExternalId) + const beforeExternal=await signed.externalInspect(externalToken,f.scope()) + const externalWindow={deadlineAt:Math.min(Date.now()+10000,beforeExternal.expiresAt,originalExpiresAt)} + const externalGuard=async()=>{const current=await signed.externalInspect(externalToken,externalWindow);assert.deepEqual(current,beforeExternal);assert(Date.now()extSession.connection,inspect:signed.externalInspect,authorize:signedAuthority,keyedProof:extSession.keyedProof,keyedProofs:extSession.keyedProofs}) + signedExternal=await signedHost.register(externalToken,externalWindow) + }finally{extSession.close()} + assert.equal(signedExternal.canonicalNodeId,managed.canonicalNodeId) + const secondManagedId=await f.managedEndpoint(claims),managedToken=await f.managedObservation(secondManagedId) + const beforeManaged=await signed.managedInspect(managedToken,f.scope()),managedWindow={deadlineAt:Math.min(Date.now()+10000,beforeManaged.expiresAt,originalExpiresAt)} + const managedGuard=async()=>{assert.deepEqual(await signed.managedInspect(managedToken,managedWindow),beforeManaged);assert(Date.now()managedSession.connection,inspect:signed.managedInspect,authorize:signedAuthority,keyedProof:managedSession.keyedProof,keyedProofs:managedSession.keyedProofs}) + signedManaged=await signedHost.register(managedToken,managedWindow) + }finally{managedSession.close()} + assert.equal(signedManaged.canonicalNodeId,managed.canonicalNodeId) + const window=f.scope(4500),session=await signed.node.open(window) + let binding + try{ + binding=await session.binding();assert.equal(binding.controlGeneration,signed.controlGeneration) + const aliasMaterial={...f.root,enrollmentId:signedExternal.enrollmentId,incarnation:signedExternal.incarnation,nodeId:signedExternal.canonicalNodeId,status:"active"} + assert.equal(await session.keyedProof("external-alias",aliasMaterial),f.mac("external-alias",aliasMaterial)) + await assert.rejects(session.keyedProof("external-alias",{...f.root,serverId:managedId,incarnation:managed.incarnation,nodeId:managed.canonicalNodeId,status:"active"})) + }finally{session.close()} + const firstEvidence=await signed.managedInspect(await f.managedObservation(managedId),f.scope()) + const secondEvidence=await signed.managedInspect(await f.managedObservation(secondManagedId),f.scope()) + const fenceWindow=f.scope(4000),expected={binding,participants:[{evidence:firstEvidence,receipt:managed},{evidence:secondEvidence,receipt:signedManaged}]} + const before=await f.digest(allTables) + const lease=await signed.core.connect(fenceWindow,async fence=>signed.node.assertResolvedOnFence(fence,expected,fenceWindow)) + try{await lease.query("BEGIN");await lease.query("SELECT 1 AS owned_signed_v2_fence_marker");await lease.query("COMMIT")}finally{lease.release(true)} + assert.equal(await f.digest(allTables),before) + const badWindow=f.scope(4000),altered={...expected,binding:{...binding,sourceRecordSha256:"a".repeat(64)}} + const rejected=await signed.core.connect(badWindow,async fence=>signed.node.assertResolvedOnFence(fence,altered,badWindow)) + try{await assert.rejects(rejected.query("SELECT 1 AS forbidden_signed_v2_pin_marker"))}finally{rejected.release(true)} + assert.equal(await f.digest(allTables),before) + acceptPeers=async(destinationId,destinationReceipt,reject,changePin=false)=>{ + const currentSource=await signed.peerInspect(await signed.peerObservation({kind:"server",serverId:secondManagedId}),f.scope()) + const currentDestination=await signed.peerInspect(await signed.peerObservation({kind:"external",enrollmentId:destinationId}),f.scope()) + const {serverId:sourceId,...sourceReceipt}=signedManaged + const {enrollmentId,...externalReceipt}=destinationReceipt + const peerExpected:BandwidthResolvedPeerFence={binding:changePin?{...binding,sourceRecordSha256:"f".repeat(64)}:binding,participants:[ + {evidence:currentSource,receipt:{...sourceReceipt,peerRef:{kind:"server",serverId:sourceId}}}, + {evidence:currentDestination,receipt:{...externalReceipt,peerRef:{kind:"external",enrollmentId}}}, + ]} + const {Client}=await import("pg"),originalQuery=Client.prototype.query,pids=new Set() + let actualMarkers=0,actualWrites=0 + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + const connection=this as unknown as {connectionParameters:{user:string;database:string};processID:number} + if(connection.connectionParameters.user===signed.runtimeRole&&connection.connectionParameters.database===f.databaseName){ + const sql=typeof args[0]==="string"?args[0]:(args[0] as {text?:string})?.text??"" + if(sql.includes("owned_external_peer_final_marker"))actualMarkers++ + if(/^(?:DO|INSERT|UPDATE|DELETE|TRUNCATE|ALTER|CREATE|DROP)\b/i.test(sql.trim()))actualWrites++ + if(connection.processID)pids.add(connection.processID) + } + return Reflect.apply(originalQuery,this,args) + } as typeof Client.prototype.query + const snapshot=await f.digest(allTables),peerWindow=f.scope(4000) + try{ + const peerLease=await signed.core.connect(peerWindow,async fence=>signed.node.assertResolvedPeersOnFence(fence,peerExpected,peerWindow)) + try{ + if(reject)await assert.rejects(peerLease.query("SELECT pg_catalog.pg_backend_pid() AS pid /* owned_external_peer_final_marker */")) + else{await peerLease.query("BEGIN");await peerLease.query("SELECT pg_catalog.pg_backend_pid() AS pid /* owned_external_peer_final_marker */");await peerLease.query("COMMIT")} + }finally{peerLease.release(true)} + }finally{Client.prototype.query=originalQuery} + assert.equal(actualMarkers,reject?0:1);assert.equal(actualWrites,0);assert.equal(pids.size,1) + assert.equal(await f.digest(allTables),snapshot) + } + cases.push("genuine Ed25519 signed core record and bootstrap v2 + owner-only key files authenticate configured source.open/keyedProof; actual managed/external registry transactions reuse existing canonical node with operational HMAC domains; signed v1 footprint and forged v2 pin refuse") + cases.push("actual configured v2 same-lease alias fence verifies two managed participants through BEGIN/SELECT/COMMIT, rejects changed original source pin without data mutation") + } + const otherId=f.externalEndpoint({"system-id":"OWNED-OTHER"}),other=await host.register(await f.externalObservation(otherId),f.scope()) + const core=await createOwnedBandwidthHarness(f.admin),binding=fixtureBinding(managed.canonicalNodeId,other.canonicalNodeId) + core.register(binding);await core.journal.reserve(binding,f.scope());await core.journal.arm(binding,f.scope());await core.journal.quarantine(binding,f.scope()) + const coreBefore=await f.digest(coreTables) + assert.equal((await host.retire(externalId,"tombstone",f.scope())).status,"tombstoned") + assert.equal((await host.retire(externalId,"tombstone",f.scope())).outcome,"existing") + await assert.rejects(host.register(await f.externalObservation(externalId),f.scope())) + await assert.rejects(host.resolve(await f.externalObservation(externalId),f.scope())) + await assert.rejects(host.retire(externalId,"quarantine",f.scope())) + const replacementId=f.externalEndpoint(claims),replacement=await host.register(await f.externalObservation(replacementId),f.scope()) + assert.equal(replacement.canonicalNodeId,managed.canonicalNodeId);assert.notEqual(replacement.incarnation,external.incarnation) + await f.managedHost.retire(managedId,"quarantine",f.scope()) + assert.equal(await f.digest(coreTables),coreBefore) + const overlap=fixtureBinding(replacement.canonicalNodeId,randomUUID());core.register(overlap);await assert.rejects(core.journal.reserve(overlap,f.scope())) + assert.equal(await f.digest(coreTables),coreBefore) + cases.push("actual reserve/arm/unknown core occupancy survives managed quarantine, external terminal retirement/replacement alias byte-for-byte; overlapping attempt remains refused") + { + // The source is the active signed managed alias; the external destination has + // a distinct canonical node. Identity checks alone do not clear occupancy. + await acceptPeers(otherId,other,false) + await acceptPeers(otherId,other,true,true) + // The mixed wire tuple cannot use aliases of the same canonical node. + await acceptPeers(replacementId,replacement,true) + const otherAliasId=f.externalEndpoint({"system-id":"OWNED-OTHER"}),otherAlias=await host.register(await f.externalObservation(otherAliasId),f.scope()) + assert.equal(otherAlias.canonicalNodeId,other.canonicalNodeId) + await acceptPeers(otherAliasId,otherAlias,false) + const aliasOverlap=fixtureBinding(managed.canonicalNodeId,otherAlias.canonicalNodeId);core.register(aliasOverlap) + await assert.rejects(core.journal.reserve(aliasOverlap,f.scope()));assert.equal(await f.digest(coreTables),coreBefore) + await host.retire(otherAliasId,"tombstone",f.scope());await acceptPeers(otherAliasId,otherAlias,true) + for(const kind of ["external-alias","external-event"] as const){ + const id=f.externalEndpoint({"system-id":"OWNED-OTHER"}),incarnation=randomUUID(),eventId=randomUUID(),authorityProof=f.mac("tampered-fixture-original",id) + const aliasMaterial={...f.root,enrollmentId:id,incarnation,nodeId:other.canonicalNodeId,status:"active"} + const eventMaterial={...f.root,eventId,enrollmentId:id,incarnation,nodeId:other.canonicalNodeId,kind:"enroll",oldStatus:null,newStatus:"active",authorityProof} + const connection=await f.owner.connect() + try{ + await connection.query("BEGIN") + await connection.query("INSERT INTO mmapp_control.bandwidth_external_node_aliases(enrollment_id,incarnation,node_id,status,record_mac) VALUES($1,$2,$3,'active',$4)",[id,incarnation,other.canonicalNodeId,kind==="external-alias"?"f".repeat(64):f.mac("external-alias",aliasMaterial)]) + await connection.query("INSERT INTO mmapp_control.bandwidth_external_node_events(event_id,enrollment_id,incarnation,node_id,kind,old_status,new_status,authority_proof,event_mac) VALUES($1,$2,$3,$4,'enroll',NULL,'active',$5,$6)",[eventId,id,incarnation,other.canonicalNodeId,authorityProof,kind==="external-event"?"f".repeat(64):f.mac("external-event",eventMaterial)]) + await connection.query("COMMIT") + }finally{await connection.query("ROLLBACK").catch(()=>{});connection.release()} + const forged:BandwidthExternalNodeRegistryReceipt={enrollmentId:id,canonicalNodeId:other.canonicalNodeId,incarnation,status:"active",outcome:"new"} + await acceptPeers(id,forged,true) + } + assert.equal(await f.digest(coreTables),coreBefore) + cases.push("actual signed v2 mixed managed-source/external-destination fence uses genuine v2 observations on one physical PG lease: final marker only for valid distinct canonical participants, zero native DML") + cases.push("before real final peer marker: changed signed source pin, same-canonical aliases, terminal external alias and separately forged alias/event MACs refuse with zero marker sends and unchanged rows; valid replacement external alias shares occupied canonical node and cannot bypass actual unknown journal occupancy") + } + + { + const before=await f.digest(allTables),connection=await f.admin.connect() + try{ + await connection.query("BEGIN");await connection.query("SET LOCAL session_replication_role=replica") + await assert.rejects(connection.query("INSERT INTO mmapp_control.bandwidth_external_node_aliases(enrollment_id,incarnation,node_id,status,record_mac) VALUES($1,$2,$3,'active',$4)",[randomUUID(),randomUUID(),randomUUID(),"a".repeat(64)])) + await connection.query("ROLLBACK") + await connection.query("BEGIN");await connection.query("SET LOCAL session_replication_role=replica") + await assert.rejects(connection.query("INSERT INTO mmapp_control.bandwidth_external_node_events(event_id,enrollment_id,incarnation,node_id,kind,old_status,new_status,authority_proof,event_mac) VALUES($1,$2,$3,$4,'enroll',NULL,'active',$5,$6)",[randomUUID(),randomUUID(),randomUUID(),replacement.canonicalNodeId,"a".repeat(64),"b".repeat(64)])) + await connection.query("ROLLBACK") + }finally{await connection.query("ROLLBACK").catch(()=>{});connection.release()} + for(const table of extensionTables)await assert.rejects(f.owner.query(`TRUNCATE mmapp_control.${table} CASCADE`),error=>(error as {code?:string}).code==="23514") + await assert.rejects(f.owner.query("UPDATE mmapp_control.bandwidth_external_node_aliases SET node_id=$1 WHERE enrollment_id=$2",[other.canonicalNodeId,replacementId])) + await assert.rejects(f.owner.query("UPDATE mmapp_control.bandwidth_external_node_aliases SET status='active' WHERE enrollment_id=$1",[externalId])) + await assert.rejects(f.owner.query("DELETE FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=$1",[replacementId])) + const missingEvent=await f.owner.connect() + try{ + await missingEvent.query("BEGIN") + await missingEvent.query("INSERT INTO mmapp_control.bandwidth_external_node_aliases(enrollment_id,incarnation,node_id,status,record_mac) VALUES($1,$2,$3,'active',$4)",[randomUUID(),randomUUID(),replacement.canonicalNodeId,"a".repeat(64)]) + await assert.rejects(missingEvent.query("COMMIT"),error=>(error as {code?:string}).code==="23514") + }finally{await missingEvent.query("ROLLBACK").catch(()=>{});missingEvent.release()} + assert.equal(await f.digest(allTables),before) + cases.push("ALWAYS reference guards reject absent node/alias under real replica mode; owner TRUNCATE, alias rebinding/reactivation and event deletion refuse without durable mutation") + } + { + const digest=await observeBandwidthExternalNodeRegistryCatalog(f.external,live);assert.match(digest,/^[a-f0-9]{64}$/) + const combined=await observeBandwidthPeerRegistryCatalog(f.external,live);assert.match(combined,/^[a-f0-9]{64}$/) + assert.equal(await observeBandwidthPeerRegistryCatalog(f.external,live),combined) + const incoming=(await f.admin.query(`SELECT t.tgname::text AS name FROM pg_catalog.pg_trigger t + JOIN pg_catalog.pg_constraint k ON k.oid=t.tgconstraint + JOIN pg_catalog.pg_class c ON c.oid=k.conrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + JOIN pg_catalog.pg_proc p ON p.oid=t.tgfoid + WHERE t.tgrelid='mmapp_control.bandwidth_nodes'::regclass AND n.nspname='mmapp_control' + AND c.relname='bandwidth_external_node_aliases' AND p.proname='RI_FKey_restrict_del'`)).rows + assert.equal(incoming.length,1);assert.match(incoming[0].name,/^[A-Za-z0-9_]+$/) + const tamper=await f.admin.connect() + try{ + await tamper.query("BEGIN") + await tamper.query(`ALTER TABLE mmapp_control.bandwidth_nodes DISABLE TRIGGER "${incoming[0].name}"`) + await assert.rejects(observeBandwidthPeerRegistryCatalog(tamper,live));await tamper.query("ROLLBACK") + await tamper.query("BEGIN") + await tamper.query("CREATE OR REPLACE FUNCTION mmapp_control.bandwidth_node_immutable_guard() RETURNS trigger LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ BEGIN RETURN NEW; END; $body$;") + await assert.rejects(observeBandwidthPeerRegistryCatalog(tamper,live));await tamper.query("ROLLBACK") + await tamper.query("BEGIN") + await tamper.query("ALTER TABLE mmapp_control.bandwidth_external_node_aliases ALTER COLUMN status SET DEFAULT 'active'") + await assert.rejects(observeBandwidthPeerRegistryCatalog(tamper,live));await tamper.query("ROLLBACK") + }finally{await tamper.query("ROLLBACK").catch(()=>{});tamper.release()} + assert.equal(await observeBandwidthPeerRegistryCatalog(f.external,live),combined) + // The old footprint cannot bless incoming extension FK triggers. No v1 source + // or pin is weakened merely to make the extension appear production-ready. + await assert.rejects(observeBandwidthNodeRegistryCatalog(f.managed,live)) + await f.owner.query("ALTER TABLE mmapp_control.bandwidth_external_node_aliases DISABLE TRIGGER bandwidth_external_node_alias_guard") + try{await assert.rejects(observeBandwidthExternalNodeRegistryCatalog(f.external,live))}finally{await f.owner.query("ALTER TABLE mmapp_control.bandwidth_external_node_aliases ENABLE ALWAYS TRIGGER bandwidth_external_node_alias_guard")} + assert.equal(await observeBandwidthExternalNodeRegistryCatalog(f.external,live),digest) + await f.owner.query(`GRANT DELETE ON mmapp_control.bandwidth_external_node_aliases TO "${f.externalRole}"`) + try{await assert.rejects(assertBandwidthExternalNodeRegistryRuntimeRole(f.external,roleExpected,live))}finally{await f.owner.query(`REVOKE DELETE ON mmapp_control.bandwidth_external_node_aliases FROM "${f.externalRole}"`)} + await assertBandwidthExternalNodeRegistryRuntimeRole(f.external,roleExpected,live) + cases.push("real PG extension and combined catalogs pass stable full digests; incoming FK disable, managed immutable-body replacement, extension column default, external guard disable and overprivileged role fail and restore exact pins; v1 catalog intentionally refuses extension; signed production pin remains open") + } + let actualCommitLosses=0,rowsOnlyCommitAcks=0 + for(const mode of ["lost","rows"] as const){ + const id=f.externalEndpoint({"system-id":"OWNED-COMMIT-"+mode}),fault=f.externalHost(mode),token=await f.externalObservation(id) + await assert.rejects(fault.host.register(token,f.scope()),error=>error instanceof BandwidthExternalNodeRegistryUnknown&&error.automaticReplay===false) + assert.equal(fault.commits(),1);assert.equal(fault.rollbacks(),0) + const row=(await f.owner.query("SELECT node_id::text FROM mmapp_control.bandwidth_external_node_aliases WHERE enrollment_id=$1",[id])).rows[0];assert(row) + assert.equal((await host.resolve(await f.externalObservation(id),f.scope())).canonicalNodeId,row.node_id) + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=$1",[id])).rows[0].n),1) + const retire=f.externalHost(mode) + await assert.rejects(retire.host.retire(id,"quarantine",f.scope()),error=>error instanceof BandwidthExternalNodeRegistryUnknown&&error.automaticReplay===false) + assert.equal(retire.commits(),1);assert.equal(retire.rollbacks(),0) + assert.equal((await host.retire(id,"quarantine",f.scope())).outcome,"existing") + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=$1",[id])).rows[0].n),2) + if(mode==="lost")actualCommitLosses+=2;else rowsOnlyCommitAcks+=2 + } + cases.push("real COMMIT persists enrollment and retirement before lost ACK/rows-only wrappers: Unknown/no automatic replay, one physical COMMIT, no rollback, durable event counts remain one/two during fresh reconciliation") + { + const id=f.externalEndpoint({"system-id":"OWNED-REVOKED"}),token=await f.externalObservation(id),before=await f.digest(allTables) + let eventInserted=false + const revoked=f.externalHost(undefined,async sql=>{ + if(sql.startsWith("DO $mmapp_bandwidth_register$")||sql.startsWith("INSERT INTO mmapp_control.bandwidth_external_node_events"))eventInserted=true + else if(eventInserted&&sql.includes("SELECT installation_id::pg_catalog.text"))f.revoke() + }) + await assert.rejects(revoked.host.register(token,f.scope()));assert(eventInserted);assert.equal(revoked.commits(),0);assert.equal(revoked.rollbacks(),1) + await assert.rejects(host.register(await f.externalObservation(id),f.scope()));assert.equal(await f.digest(allTables),before) + cases.push("original operator grant revoked after actual event INSERT and before final authority SELECT: real transaction rolls back, COMMIT is not sent; fresh genuine observation remains denied") + } + assert.equal(await f.digest(coreTables),coreBefore) + await f.profileMetadata() + const execution=await acceptExecution();cases.push(...execution.checks) + cases.push(...await acceptOperator()) + cases.push(...await acceptWriter()) + assert.equal((await f.owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1",[binding.attemptId])).rows[0]?.state,"unknown_busy") + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1",[binding.attemptId])).rows[0].n),2) + console.log("Owned external bandwidth registry:",JSON.stringify({cases:cases.length,checks:cases,actualLocalhostLicenseGets:f.nativeGets(),actualLocalhostBtestPosts:f.nativePosts(),actualCommitLosses,rowsOnlyCommitAcks,normalMigrationCount:MIGRATION_MANIFEST.length,coreUnknownPairPreserved:true,realChrCalls:0,productionWired:false,combinedSignedProductionPin:false,limitations:["Synthetic explicit operator/quiescence and issuer fixture only; no production enrollment, clone resistance, authenticated bandwidth endpoint mapping or caller activation claim","One runner-owned ephemeral localhost cluster; no remote DB/CHR; native evidence uses localhost license responder"]})) +}finally{await f.close()} diff --git a/backend/src/services/bandwidth-external-node-registry.test.ts b/backend/src/services/bandwidth-external-node-registry.test.ts new file mode 100644 index 00000000..347e669d --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry.test.ts @@ -0,0 +1,113 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createHmac, randomUUID } from "node:crypto" +import { createBandwidthExternalNodeRegistry, BandwidthExternalNodeRegistryError, BandwidthExternalNodeRegistryUnknown } from "./bandwidth-external-node-registry.js" +import { createBandwidthNodeRegistry } from "./bandwidth-node-registry.js" +import type { BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +type Row=Record +const canonical=(v:unknown):string=>Array.isArray(v)?"["+v.map(canonical).join(",")+"]":v&&typeof v==="object"?"{"+Object.entries(v).sort(([a],[b])=>a.localeCompare(b)).map(([k,v])=>JSON.stringify(k)+":"+canonical(v)).join(",")+"}":JSON.stringify(v) +const mac=(purpose:string,value:unknown)=>createHmac("sha256","synthetic-fixed-issuer-only").update(purpose+":"+canonical(value)).digest("hex") +const installationId=randomUUID(),issuerKeySha256="a".repeat(64),quiescenceProof="b".repeat(64) +const root={installationId,issuerKeySha256,quiescenceProof} +const scope=()=>({deadlineAt:Date.now()+5000}) +/** Inert SQL state machine, no pool/PG/remote imports. The same node table and + * MAC issuer are shared by the real managed and external registry functions. */ +function fixture(){ + const nodes:Row[]=[],aliases:Row[]=[],events:Row[]=[],managedAliases:Row[]=[],managedEvents:Row[]=[],queries:string[]=[],purposes:string[]=[] + const occupancy={status:"unknown",node_id:"never-changed"},observations=new WeakMap() + let revoked=false,keyChanged=false,commitMode:"ok"|"lost"|"rows"="ok",afterEvent=false,finalSelect=false,inspectionsAfterSelect=0, revokeAtCommit=false,leaseCount=0 + let onProof:((purpose:string)=>void)|undefined + const authority=()=>{if(revoked)throw Error("original grant revoked");return{...root,issuerKeySha256:keyChanged?"f".repeat(64):issuerKeySha256,authorityProof:"c".repeat(64),authorityGeneration:"original-grant",expiresAt:Date.now()+60000}} + // Stable original expiry: extending it at every check must be refused. + const expiresAt=Date.now()+60000 + const authorize=async()=>({...authority(),expiresAt}) + const inspect=async(token:unknown)=>{ + const e=token&&typeof token==="object"?observations.get(token):undefined;if(!e)throw Error("not genuine") + if(revokeAtCommit&&finalSelect&&++inspectionsAfterSelect===3)revoked=true + return e + } + const connect=async():Promise=>{ + leaseCount++;const before=structuredClone({nodes,aliases,events,managedAliases,managedEvents});let committed=false + const restore=()=>{for(const [target,old]of[[nodes,before.nodes],[aliases,before.aliases],[events,before.events],[managedAliases,before.managedAliases],[managedEvents,before.managedEvents]])target.splice(0,target.length,...old)} + return{release(){if(!committed)restore()},async query(sql,values=[]){ + queries.push(sql);const rows=(r:Row[]=[],command?:string)=>({rows:structuredClone(r),command}) + if(sql==="BEGIN")return rows([],"BEGIN") + if(sql==="ROLLBACK"){restore();return rows([],"ROLLBACK")} + if(sql==="COMMIT"){committed=true;if(commitMode==="lost")throw Error("lost physical ACK");return rows([],commitMode==="rows"?undefined:"COMMIT")} + if(sql.includes("set_config")||sql.includes("pg_advisory_xact_lock"))return rows() + if(sql.includes("FROM mmapp_control.bandwidth_node_authority")){if(afterEvent)finalSelect=true;return rows([{installation_id:installationId,issuer_key_sha256:issuerKeySha256,quiescence_proof:quiescenceProof,status:"active"}])} + const isExternal=sql.includes("bandwidth_external_node_"),a=isExternal?aliases:managedAliases,e=isExternal?events:managedEvents,id=isExternal?"enrollment_id":"server_id" + if(sql.startsWith("WITH aliases AS MATERIALIZED")){ + const selected=a.filter(row=>row[id]===values[0]) + return rows([{aliases:selected.map(row=>({subject:String(row[id]),incarnation:row.incarnation,node_id:row.node_id,status:row.status,record_mac:row.record_mac})),nodes:nodes.filter(row=>selected.some(alias=>alias.node_id===row.node_id)),events:e.filter(row=>row[id]===values[0]).map(row=>({event_id:row.event_id,incarnation:row.incarnation,node_id:row.node_id,kind:row.kind,old_status:row.old_status,new_status:row.new_status,authority_proof:row.authority_proof,event_mac:row.event_mac})),claim_nodes:values[1]===null?[]:nodes.filter(row=>row.claim_proof===values[1])}],"SELECT") + } + if(sql.startsWith("DO $mmapp_bandwidth_register$")){ + // Inert adapter parses only the candidate's validated literal VALUES; + // this model does not certify PostgreSQL sequential SPI semantics. + const valuesOf=(table:string)=>{const line=sql.split("\n").find(line=>line.trimStart().startsWith("INSERT INTO mmapp_control."+table+"("));assert(line);const match=/ VALUES\((.*)\)$/.exec(line!);assert(match);return match![1].split(",").map(value=>value.replace(/::pg_catalog\.(?:uuid|text|int8)$/,"" ).replace(/^'|'$/g,""))} + const node=valuesOf("bandwidth_nodes") + if(sql.includes("IF true THEN")){assert(!nodes.some(row=>row.claim_proof===node[1]));nodes.push({node_id:node[0],claim_proof:node[1],enrollment_mac:node[2]})} + else assert(nodes.some(row=>row.node_id===node[0]&&row.claim_proof===node[1]&&row.enrollment_mac===node[2])) + const alias=valuesOf(isExternal?"bandwidth_external_node_aliases":"bandwidth_node_aliases"),event=valuesOf(isExternal?"bandwidth_external_node_events":"bandwidth_node_events"),subject=isExternal?alias[0]:Number(alias[0]) + assert(!a.some(row=>row[id]===subject));a.push({[id]:subject,incarnation:alias[1],node_id:alias[2],status:alias[3],record_mac:alias[4]}) + e.push({event_id:event[0],[id]:isExternal?event[1]:Number(event[1]),incarnation:event[2],node_id:event[3],kind:event[4],old_status:event[5]==="NULL"?null:event[5],new_status:event[6],authority_proof:event[7],event_mac:event[8]});afterEvent=true + return rows([],"DO") + } + if(sql.startsWith("SELECT")&&sql.includes("FROM mmapp_control.bandwidth_nodes"))return rows(nodes.filter(row=>row[sql.includes("WHERE claim_proof")?"claim_proof":"node_id"]===values[0])) + if(sql.startsWith("INSERT INTO mmapp_control.bandwidth_nodes")){const row={node_id:values[0],claim_proof:values[1],enrollment_mac:values[2]};assert(!nodes.some(n=>n.claim_proof===row.claim_proof));nodes.push(row);return rows([{node_id:row.node_id}])} + if(sql.startsWith("SELECT")&&sql.includes("_aliases WHERE"))return rows(a.filter(row=>row[id]===values[0])) + if(sql.startsWith("SELECT")&&sql.includes("_events WHERE"))return rows(e.filter(row=>row[id]===values[0]).sort((a,b)=>String(a.kind).localeCompare(String(b.kind)))) + if(sql.startsWith("INSERT")&&sql.includes("_aliases(")){const row={[id]:values[0],incarnation:values[1],node_id:values[2],status:"active",record_mac:values[3]};assert(!a.some(r=>r[id]===row[id]));a.push(row);return rows([{[id]:row[id]}])} + if(sql.startsWith("INSERT")&&sql.includes("_events(")){e.push({event_id:values[0],[id]:values[1],incarnation:values[2],node_id:values[3],kind:values[4],old_status:values[5],new_status:values[6],authority_proof:values[7],event_mac:values[8]});afterEvent=true;return rows([{event_id:values[0]}])} + if(sql.startsWith("UPDATE")&&sql.includes("_aliases SET")){const row=a.find(r=>r[id]===values[0]&&r.status==="active"&&r.record_mac===values[3]);if(!row)return rows();row.status=values[1];row.record_mac=values[2];return rows([{[id]:row[id]}])} + throw Error("Unexpected SQL: "+sql) + }} + } + const keyedProof=async(purpose:string,value:unknown)=>{purposes.push(purpose);onProof?.(purpose);return mac(purpose,value)} + const host=createBandwidthExternalNodeRegistry({now:Date.now,connect,inspect,authorize,keyedProof}) + function observation(enrollmentId:string=randomUUID(),claims:BandwidthPeerObservationEvidence["claims"]=[{kind:"system-id",value:"shared-identity"}]){ + const token={};observations.set(token,{version:2,peerRef:{kind:"external",enrollmentId},claims,sourceProof:"d".repeat(64),authorityGeneration:"source-v2",routingEndpointEpoch:randomUUID(),observedAt:Date.now(),expiresAt:Date.now()+5000,physicalUniquenessVerified:false,cloneResistanceVerified:false});return token + } + const managed=createBandwidthNodeRegistry({now:Date.now,connect,authorize,keyedProof,inspect:async()=>({version:1,serverId:42,claims:[{kind:"system-id",value:"shared-identity"}],sourceProof:"d".repeat(64),authorityGeneration:"source-v1",routingEndpointEpoch:installationId,observedAt:1,expiresAt,physicalUniquenessVerified:false,cloneResistanceVerified:false})}) + return{host,managed,observation,nodes,aliases,events,queries,purposes,occupancy,setCommit:(mode:typeof commitMode)=>{commitMode=mode},revoke:()=>{revoked=true},changeKey:()=>{keyChanged=true},revokeAtCommit:()=>{revokeAtCommit=true;afterEvent=false;finalSelect=false;inspectionsAfterSelect=0},proofHook:(hook:typeof onProof)=>{onProof=hook},leases:()=>leaseCount} +} + +test("managed and external aliases use the identical issuer/typed claim domain and canonical node; unknown occupancy untouched",async()=>{ + const f=fixture(),before=structuredClone(f.occupancy),managed=await f.managed.register({},scope()),token=f.observation(),external=await f.host.register(token,scope()) + assert.equal(external.canonicalNodeId,managed.canonicalNodeId);assert.notEqual(external.incarnation,managed.incarnation);assert.equal(f.nodes.length,1) + assert.equal((await f.host.resolve(token,scope())).canonicalNodeId,managed.canonicalNodeId) + assert.equal((await f.host.register(token,scope())).outcome,"existing");assert.equal(f.events.length,1) + assert(f.purposes.includes("external-alias"));assert(f.purposes.includes("external-event"));assert.deepEqual(f.occupancy,before) + assert(f.queries.includes("SELECT pg_catalog.pg_advisory_xact_lock(8723116)"));assert(!f.queries.some(q=>/occupancy|bandwidth_attempt/.test(q))) +}) +test("external terminal alias cannot reactivate/rebind; fresh alias preserves node and unknown",async()=>{ + const f=fixture(),token=f.observation(),first=await f.host.register(token,scope()) + assert.equal((await f.host.retire(first.enrollmentId,"tombstone",scope())).status,"tombstoned") + assert.equal((await f.host.retire(first.enrollmentId,"tombstone",scope())).outcome,"existing") + await assert.rejects(f.host.register(token,scope()));await assert.rejects(f.host.resolve(token,scope()));await assert.rejects(f.host.retire(first.enrollmentId,"quarantine",scope())) + const replacement=await f.host.register(f.observation(),scope());assert.equal(replacement.canonicalNodeId,first.canonicalNodeId);assert.notEqual(replacement.incarnation,first.incarnation);assert.equal(f.events.length,3) +}) +test("resolve absent, cloned observation, invalid enrollment and mismatched license do not auto-enroll",async()=>{ + const f=fixture(),token=f.observation();await assert.rejects(f.host.resolve(token,scope()));assert.equal(f.nodes.length,0) + await assert.rejects(f.host.register({...token},scope()));await assert.rejects(f.host.retire("42","tombstone",scope()));assert.equal(f.nodes.length,0) + const first=await f.host.register(token,scope());await assert.rejects(f.host.register(f.observation(first.enrollmentId,[{kind:"software-id",value:"shared-identity"}]),scope()));assert.equal(f.nodes.length,1) +}) +test("full typed license vector and claim kind remain distinct",async()=>{ + const f=fixture(),ids=[] + for(const claims of [[{kind:"system-id",value:"ONE"}],[{kind:"software-id",value:"ONE"}],[{kind:"system-id",value:"ONE"},{kind:"software-id",value:"TWO"}]] as BandwidthPeerObservationEvidence["claims"][])ids.push((await f.host.register(f.observation(undefined,claims),scope())).canonicalNodeId) + assert.equal(new Set(ids).size,3) +}) +test("revocation or issuer drift during proof prevents writes, destroys lease and rolls back",async()=>{ + for(const change of ["revoke","changeKey"] as const){const f=fixture();f.proofHook(purpose=>{if(purpose==="external-event")f[change]()});await assert.rejects(f.host.register(f.observation(),scope()),BandwidthExternalNodeRegistryError);assert.equal(f.nodes.length,0);assert.equal(f.aliases.length,0);assert(!f.queries.includes("COMMIT"));assert(f.queries.includes("ROLLBACK"))} +}) +test("original grant is checked after final asynchronous inspection before COMMIT",async()=>{ + const f=fixture();f.revokeAtCommit();await assert.rejects(f.host.register(f.observation(),scope()),BandwidthExternalNodeRegistryError);assert(!f.queries.includes("COMMIT"));assert.equal(f.aliases.length,0) +}) +test("lost or rows-only physical COMMIT ACK is unknown, no replay or post-COMMIT rollback; fresh resolve reconciles",async()=>{ + for(const mode of ["lost","rows"] as const){const f=fixture(),token=f.observation();f.setCommit(mode);await assert.rejects(f.host.register(token,scope()),error=>error instanceof BandwidthExternalNodeRegistryUnknown&&error.automaticReplay===false);assert.equal(f.aliases.length,1);assert.equal(f.events.length,1);assert.equal(f.queries.filter(q=>q==="COMMIT").length,1);assert(!f.queries.includes("ROLLBACK"));f.setCommit("ok");assert.equal((await f.host.resolve(token,scope())).canonicalNodeId,f.nodes[0].node_id);assert.equal(f.events.length,1)} +}) +test("prior alias/node/event MAC tampering refuses without replacement identity",async()=>{ + for(const target of ["aliases","nodes","events"] as const){const f=fixture(),token=f.observation();await f.host.register(token,scope());const rows=f[target];rows[0][target==="aliases"?"record_mac":target==="nodes"?"enrollment_mac":"event_mac"]="f".repeat(64);await assert.rejects(f.host.resolve(token,scope()));assert.equal(f.nodes.length,1);assert.equal(f.events.length,1)} +}) diff --git a/backend/src/services/bandwidth-external-node-registry.ts b/backend/src/services/bandwidth-external-node-registry.ts new file mode 100644 index 00000000..fedc343f --- /dev/null +++ b/backend/src/services/bandwidth-external-node-registry.ts @@ -0,0 +1,254 @@ +/** External enrollment extension sharing the original canonical claim/node MAC domains. + * No production host wiring or signed extension catalog pin is provided. No + * default pool, source, signing key, enrollment or native dispatch is supplied. + * Genuine observations are inspected by the fixed same-reader composition. + * The original node key survives aliases, credential epochs and tombstones. */ +import { randomUUID } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { bandwidthRegistrySnapshotSql, parseBandwidthRegistrySnapshot, bandwidthRegistrySnapshotProofs, bandwidthRegistryEnrollmentSql, type BandwidthRegistrySnapshot } from "./bandwidth-node-registry-batch.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { BandwidthNodeProofInput } from "./bandwidth-node-source.js" +import type { BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" + +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid() +const authoritySchema=z.object({installationId:uuid,issuerKeySha256:hash,quiescenceProof:hash, + authorityProof:hash,authorityGeneration:z.string().min(1).max(200),expiresAt:z.number().int().positive()}).strict() +type Authority=z.infer +export type BandwidthExternalNodeRegistryAction="register"|"resolve"|"tombstone"|"quarantine" +export interface BandwidthExternalNodeRegistryDependencies { + now():number + /** Bounded owned lease; query results must preserve the actual PG command. + * Rows alone never establish a physical BEGIN/COMMIT acknowledgement. */ + connect(scope:BandwidthScope):Promise + /** Fixed private provenance verifier, not a JSON/native-response parser. */ + inspect(observation:unknown,scope:BandwidthScope):Promise + /** Must independently authenticate original human enrollment/quiescence or + * current execution/retirement authority, issuer key and installation. A SQL + * row, model flag or generic read permission cannot issue this authority. */ + authorize(action:BandwidthExternalNodeRegistryAction,enrollmentId:string,scope:BandwidthScope,evidence:Readonly|undefined):Promise + /** Trusted constructor-only fixed path. ONE fresh provenance/authority read; + * both projections must be synchronous after that read. Never request JSON. */ + inspectAndAuthorize?(action:BandwidthExternalNodeRegistryAction,observation:unknown,requestedId:string|undefined,scope:BandwidthScope):Promise> + /** Stable separate externally provisioned issuer key; never endpoint/epoch, + * credentials, per-operation generation or an automatically generated key. */ + keyedProof(purpose:string,value:unknown,scope:BandwidthScope):Promise + /** Fixed optional source capability: local MACs share one freshly checked key. + * No caller supplies a callback; scalar fallback retains all original guards. */ + keyedProofs?(inputs:readonly BandwidthNodeProofInput[],scope:BandwidthScope):Promise +} +export interface BandwidthExternalNodeRegistryReceipt { + readonly enrollmentId:string;readonly canonicalNodeId:string;readonly incarnation:string + readonly status:"active"|"tombstoned"|"quarantined";readonly outcome:"new"|"existing" +} +export class BandwidthExternalNodeRegistryError extends Error { + readonly code:string="BANDWIDTH_EXTERNAL_NODE_REGISTRY_REFUSED" + readonly automaticReplay=false + constructor(){super("Постоянная привязка bandwidth-узла не подтверждена")} +} +/** COMMIT may have persisted. Reconcile by fresh read under original authority; + * never replay the enrollment/retirement automatically. */ +export class BandwidthExternalNodeRegistryUnknown extends BandwidthExternalNodeRegistryError { + override readonly code="BANDWIDTH_EXTERNAL_NODE_REGISTRY_UNKNOWN" +} +const refuse=():never=>{throw new BandwidthExternalNodeRegistryError()} +const evidenceSchema=z.object({version:z.literal(2),peerRef:z.object({kind:z.literal("external"),enrollmentId:uuid}).strict(), + sourceProof:hash,authorityGeneration:z.string().min(1).max(200),routingEndpointEpoch:uuid, + observedAt:z.number().int().nonnegative(),expiresAt:z.number().int().positive(), + claims:z.array(z.object({kind:z.enum(["system-id","software-id"]),value:z.string().min(1).max(256)}).strict()).min(1).max(2), + physicalUniquenessVerified:z.literal(false),cloneResistanceVerified:z.literal(false)}).strict() +type Evidence=z.infer +type Sql={query(text:string,values?:unknown[]):Promise<{rows:Record[];command?:unknown}>} +type Alias={enrollment_id:unknown;incarnation:unknown;node_id:unknown;status:unknown;record_mac:unknown} + +export function createBandwidthExternalNodeRegistry(dependencies:BandwidthExternalNodeRegistryDependencies){ + // Capture server-owned methods once. No request accepts a source, SQL facade, + // signing callback, chosen UUID or caller-asserted license evidence. + const deps=Object.freeze({now:dependencies.now.bind(dependencies),connect:dependencies.connect.bind(dependencies), + inspect:dependencies.inspect.bind(dependencies),authorize:dependencies.authorize.bind(dependencies),keyedProof:dependencies.keyedProof.bind(dependencies), + keyedProofs:dependencies.keyedProofs?.bind(dependencies),inspectAndAuthorize:dependencies.inspectAndAuthorize?.bind(dependencies)}) + const captureEvidence=(raw:unknown)=>{ + const value=evidenceSchema.parse(raw) + if(value.claims.some((claim,index)=>index>0&&value.claims[index-1].kind!=="system-id"||index===1&&claim.kind!=="software-id") + ||value.claims.length===2&&value.claims[0].kind===value.claims[1].kind)refuse() + Object.freeze(value.peerRef);value.claims.forEach(Object.freeze);Object.freeze(value.claims) + return Object.freeze(value) + } + const inspect=async(observation:unknown,scope:BandwidthScope)=>captureEvidence(await deps.inspect(observation,scope)) + const frames=new WeakSet() + const combined=async(action:BandwidthExternalNodeRegistryAction,observation:unknown,requestedId:string|undefined,scope:BandwidthScope)=>{ + const raw=await deps.inspectAndAuthorize!(action,observation,requestedId,scope) + if(!raw||Object.getPrototypeOf(raw)!==Object.prototype||!Object.isFrozen(raw)||frames.has(raw))refuse() + const fields=Object.getOwnPropertyDescriptors(raw),keys=["action","observation","scope","enrollmentId","evidence","authority"] + if(Reflect.ownKeys(raw).length!==keys.length||keys.some(key=>!fields[key]||!("value"in fields[key])||!fields[key].enumerable) + ||fields.action.value!==action||fields.observation.value!==observation||fields.scope.value!==scope)refuse() + frames.add(raw) + let budget=1000 + const data=(value:unknown,depth=0):void=>{ + if(--budget<0||depth>8)refuse() + if(!value||typeof value!=="object")return + const array=Array.isArray(value),descriptors=Object.getOwnPropertyDescriptors(value) + if(Object.getPrototypeOf(value)!==(array?Array.prototype:Object.prototype))refuse() + for(const key of Reflect.ownKeys(value)){ + if(array&&key==="length")continue + if(typeof key!=="string")return refuse() + const field=descriptors[key] + if(!field||!("value"in field)||!field.enumerable)return refuse() + data(field.value,depth+1) + } + } + data(fields.evidence.value);data(fields.authority.value) + const evidence=fields.evidence.value===undefined?undefined:captureEvidence(fields.evidence.value) + const enrollmentId=fields.enrollmentId.value + if(!uuid.safeParse(enrollmentId).success||requestedId!==undefined&&enrollmentId!==requestedId + ||requestedId===undefined&&!evidence||evidence&&evidence.peerRef.enrollmentId!==enrollmentId)refuse() + return Object.freeze({enrollmentId:enrollmentId as string,evidence,authority:Object.freeze(authoritySchema.parse(fields.authority.value))}) + } + async function transaction(action:BandwidthExternalNodeRegistryAction,observation:unknown,requestedId:string|undefined,input:BandwidthScope, + work:(sql:Sql,authority:Authority,evidence:Evidence|undefined,proof:(purpose:string,value:unknown)=>Promise,proofs:(inputs:readonly BandwidthNodeProofInput[])=>Promise)=>Promise):Promise{ + if(!input||!Number.isSafeInteger(input.deadlineAt)||input.signal!==undefined&&!(input.signal instanceof AbortSignal))throw new BandwidthExternalNodeRegistryError() + let connection:ProbeStorageConnection|undefined,commitStarted=false,transactionStarted=false,active=true,released=false,originalExpiry=Infinity + const startedAt=deps.now(),monotonicStart=performance.now() + const scope=Object.freeze({deadlineAt:Math.min(input.deadlineAt,startedAt+10000),signal:input.signal}) + const remaining=()=>Math.min(scope.deadlineAt,originalExpiry)-startedAt-(performance.now()-monotonicStart) + const release=()=>{if(connection&&!released){released=true;try{connection.release(true)}catch{/* No unsafe pool reuse. */}}} + const live=()=>{if(!active||scope.signal?.aborted||remaining()<=0||deps.now()>=Math.min(scope.deadlineAt,originalExpiry))refuse()} + let rejectStopped!:(error:BandwidthExternalNodeRegistryError)=>void,timer:ReturnType + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject}) + const stop=()=>{active=false;release();rejectStopped(commitStarted?new BandwidthExternalNodeRegistryUnknown():new BandwidthExternalNodeRegistryError())} + const schedule=()=>{clearTimeout(timer);timer=setTimeout(stop,Math.max(1,Math.min(10000,remaining(),scope.deadlineAt-deps.now(),originalExpiry-deps.now())))} + schedule();scope.signal?.addEventListener("abort",stop,{once:true}) + const run=async()=>{ + try{ + live();const first=deps.inspectAndAuthorize?await combined(action,observation,requestedId,scope):undefined;live() + const evidence=first?first.evidence:requestedId===undefined?await inspect(observation,scope):undefined;live() + if(evidence){originalExpiry=evidence.expiresAt;schedule();live()} + const enrollmentId=evidence?.peerRef.enrollmentId??requestedId! + if(!uuid.safeParse(enrollmentId).success)refuse() + const authority=first?first.authority:authoritySchema.parse(await deps.authorize(action,enrollmentId,scope,evidence));live() + originalExpiry=Math.min(originalExpiry,authority.expiresAt);schedule();live() + if(authority.expiresAt<=deps.now()||evidence&&evidence.expiresAt<=deps.now())refuse() + const guard=async()=>{ + live();if(deps.now()>=authority.expiresAt||evidence&&deps.now()>=evidence.expiresAt)refuse() + if(deps.inspectAndAuthorize){ + const current=await combined(action,observation,enrollmentId,scope);live() + if(!isDeepStrictEqual(current.evidence,evidence)||!isDeepStrictEqual(current.authority,authority))refuse() + return + } + if(evidence&&!isDeepStrictEqual(await inspect(observation,scope),evidence))refuse() + // The original enrollment/execution grant is the LAST asynchronous + // check before SQL/COMMIT. Native provenance may itself await source IO; + // a revocation during that IO must not authorize a subsequent write. + const current=authoritySchema.parse(await deps.authorize(action,enrollmentId,scope,evidence));live() + if(!isDeepStrictEqual(current,authority))refuse() + live() + } + const freeze=(value:unknown):unknown=>{if(value&&typeof value==="object"){Object.values(value).forEach(freeze);Object.freeze(value)}return value} + const proof=async(purpose:string,value:unknown)=>{const snapshot=freeze(JSON.parse(JSON.stringify(value)));await guard();const result=await deps.keyedProof(purpose,snapshot,scope);await guard();if(!hash.safeParse(result).success)refuse();return result} + const proofs=async(inputs:readonly BandwidthNodeProofInput[])=>{ + if(!deps.keyedProofs){const values:string[]=[];for(const entry of inputs)values.push(await proof(entry.purpose,entry.value));return Object.freeze(values)} + const snapshot=freeze(JSON.parse(JSON.stringify(inputs))) as readonly BandwidthNodeProofInput[] + await guard() + const values=await deps.keyedProofs(snapshot,scope) + if(!Array.isArray(values)||Object.getPrototypeOf(values)!==Array.prototype||values.length!==snapshot.length||Reflect.ownKeys(values).length!==values.length+1)refuse() + const descriptors=Object.getOwnPropertyDescriptors(values) + const captured=Object.freeze(Array.from({length:values.length},(_,index)=>{ + const field=descriptors[String(index)] + if(!field||!("value"in field)||!field.enumerable||!hash.safeParse(field.value).success)refuse() + return field.value as string + })) + await guard() + return captured + } + connection=await deps.connect(scope);live() + const sql:Sql={query:async(text,values)=>{await guard();const result=await connection!.query(text,values,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-deps.now()))});await guard();return result}} + const started=await sql.query("BEGIN");transactionStarted=true;if(started.command!=="BEGIN")refuse() + await sql.query("SELECT pg_catalog.set_config('search_path','pg_catalog',true),pg_catalog.set_config('statement_timeout','5000',true),pg_catalog.set_config('lock_timeout','2000',true),pg_catalog.set_config('synchronous_commit','on',true)") + const checkAuthority=async()=>{ + const rows=(await sql.query("SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status FROM mmapp_control.bandwidth_node_authority WHERE id=1")).rows + if(rows.length!==1||rows[0].installation_id!==authority.installationId||rows[0].issuer_key_sha256!==authority.issuerKeySha256||rows[0].quiescence_proof!==authority.quiescenceProof||rows[0].status!=="active")refuse() + } + await checkAuthority() + // All state changes, including unrelated alias enrollment, serialize on + // this registry-only lock. It never releases or alters bandwidth owners. + if(action!=="resolve")await sql.query("SELECT pg_catalog.pg_advisory_xact_lock(8723116)") + const result=await work(sql,authority,evidence,proof,proofs) + await checkAuthority();await guard();commitStarted=true;const committed=await sql.query("COMMIT");if(committed.command!=="COMMIT")refuse();transactionStarted=false;await checkAuthority();await guard() + return result + }catch{ + if(active&&connection&&!released&&transactionStarted&&!commitStarted)try{await connection.query("ROLLBACK",undefined,{signal:scope.signal,timeoutMs:1000})}catch{/* Destroy the owned lease below. */} + throw commitStarted?new BandwidthExternalNodeRegistryUnknown():new BandwidthExternalNodeRegistryError() + }finally{release()} + } + try{return await Promise.race([run(),stopped])} + finally{active=false;clearTimeout(timer!);scope.signal?.removeEventListener("abort",stop);release()} + } + const root=(a:Authority)=>({installationId:a.installationId,issuerKeySha256:a.issuerKeySha256,quiescenceProof:a.quiescenceProof}) + const aliasMaterial=(a:Authority,row:Alias)=>({...root(a),enrollmentId:String(row.enrollment_id),incarnation:row.incarnation,nodeId:row.node_id,status:row.status}) + const receipt=(row:Alias,outcome:"new"|"existing"):Readonly=>{ + if(!uuid.safeParse(row.enrollment_id).success||!uuid.safeParse(row.incarnation).success||!uuid.safeParse(row.node_id).success||!["active","tombstoned","quarantined"].includes(String(row.status)))refuse() + return Object.freeze({enrollmentId:String(row.enrollment_id),canonicalNodeId:String(row.node_id),incarnation:String(row.incarnation),status:row.status as BandwidthExternalNodeRegistryReceipt["status"],outcome}) + } + const snapshot=async(sql:Sql,enrollmentId:string,claimProof:string|null,lock:boolean)=>{ + const result=await sql.query(bandwidthRegistrySnapshotSql("external",lock),[enrollmentId,claimProof]) + if(result.command!=="SELECT")refuse() + return parseBandwidthRegistrySnapshot(result.rows,"external",enrollmentId,claimProof) + } + const snapshotAlias=(value:BandwidthRegistrySnapshot):Alias=>{ + const row=value.aliases[0];if(!row)refuse() + return {enrollment_id:String(row.subject),incarnation:row.incarnation,node_id:row.node_id,status:row.status,record_mac:row.record_mac} + } + const checkedAlias=async(a:Authority,value:BandwidthRegistrySnapshot,proofs:(inputs:readonly BandwidthNodeProofInput[])=>Promise)=>{ + const row=snapshotAlias(value);receipt(row,"existing") + const items=bandwidthRegistrySnapshotProofs("external",value,root(a)) + const signed=await proofs(items.map(({purpose,value})=>({purpose,value}))) + if(items.some((item,index)=>item.expected!==signed[index]))refuse() + return value.nodes[0].claim_proof + } + const event=async(sql:Sql,a:Authority,row:Alias,kind:"enroll"|"tombstone"|"quarantine",proof:(purpose:string,value:unknown)=>Promise)=>{ + const id=randomUUID(),old=kind==="enroll"?null:"active" + const mac=await proof("external-event",{...root(a),eventId:id,enrollmentId:String(row.enrollment_id),incarnation:row.incarnation,nodeId:row.node_id,kind,oldStatus:old,newStatus:row.status,authorityProof:a.authorityProof}) + const result=await sql.query("INSERT INTO mmapp_control.bandwidth_external_node_events(event_id,enrollment_id,incarnation,node_id,kind,old_status,new_status,authority_proof,event_mac) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING event_id",[id,String(row.enrollment_id),row.incarnation,row.node_id,kind,old,row.status,a.authorityProof,mac]) + if(result.rows.length!==1||result.rows[0].event_id!==id)refuse() + } + return Object.freeze({ + register:(observation:unknown,scope:BandwidthScope)=>transaction("register",observation,undefined,scope,async(sql,a,e,proof,proofs)=>{ + const claim=await proof("claim",e!.claims),value=await snapshot(sql,e!.peerRef.enrollmentId,claim,true) + if(value.aliases.length){const row=snapshotAlias(value);if(row.status!=="active"||await checkedAlias(a,value,proofs)!==claim)refuse();return receipt(row,"existing")} + const candidate=value.claim_nodes[0],nodeId=candidate?.node_id??randomUUID() + const row:Alias={enrollment_id:e!.peerRef.enrollmentId,incarnation:randomUUID(),node_id:nodeId,status:"active",record_mac:""} + const eventId=randomUUID() + const [nodeMac,aliasMac,eventMac]=await proofs([ + {purpose:"node",value:{...root(a),nodeId,claimProof:claim}}, + {purpose:"external-alias",value:aliasMaterial(a,row)}, + {purpose:"external-event",value:{...root(a),eventId,enrollmentId:e!.peerRef.enrollmentId,incarnation:row.incarnation,nodeId,kind:"enroll",oldStatus:null,newStatus:"active",authorityProof:a.authorityProof}}, + ]) + if(candidate&&candidate.enrollment_mac!==nodeMac)refuse() + row.record_mac=aliasMac + const written=await sql.query(bandwidthRegistryEnrollmentSql({kind:"external",subject:e!.peerRef.enrollmentId,insertNode:!candidate,nodeId,claimProof:claim,nodeMac,incarnation:String(row.incarnation),aliasMac:String(row.record_mac),eventId,authorityProof:a.authorityProof,eventMac})) + if(written.command!=="DO")refuse() + return receipt(row,"new") + }), + resolve:(observation:unknown,scope:BandwidthScope)=>transaction("resolve",observation,undefined,scope,async(sql,a,e,proof,proofs)=>{ + const value=await snapshot(sql,e!.peerRef.enrollmentId,null,false),row=snapshotAlias(value) + if(row.status!=="active"||await checkedAlias(a,value,proofs)!==await proof("claim",e!.claims))refuse() + return receipt(row,"existing") + }), + retire:(enrollmentId:string,kind:"tombstone"|"quarantine",scope:BandwidthScope)=>{ + if(kind!=="tombstone"&&kind!=="quarantine")return Promise.reject(new BandwidthExternalNodeRegistryError()) + return transaction(kind,undefined,enrollmentId,scope,async(sql,a,_e,proof,proofs)=>{ + const value=await snapshot(sql,enrollmentId,null,true),row=snapshotAlias(value);await checkedAlias(a,value,proofs) + const next=kind==="tombstone"?"tombstoned":"quarantined" + if(row.status===next)return receipt(row,"existing") + if(row.status!=="active")refuse() + const updated:Alias={...row,status:next};updated.record_mac=await proof("external-alias",aliasMaterial(a,updated)) + const changed=await sql.query("UPDATE mmapp_control.bandwidth_external_node_aliases SET status=$2,record_mac=$3 WHERE enrollment_id=$1 AND status='active' AND record_mac=$4 RETURNING enrollment_id",[enrollmentId,next,updated.record_mac,row.record_mac]);if(changed.rows.length!==1||String(changed.rows[0].enrollment_id)!==enrollmentId)refuse() + await event(sql,a,updated,kind,proof);return receipt(updated,"new") + }) + }, + }) +} diff --git a/backend/src/services/bandwidth-external-peer-metadata.sql-helper.ts b/backend/src/services/bandwidth-external-peer-metadata.sql-helper.ts new file mode 100644 index 00000000..8471af1e --- /dev/null +++ b/backend/src/services/bandwidth-external-peer-metadata.sql-helper.ts @@ -0,0 +1,225 @@ +/** Owned integration: genuine operator/window/source guards, real SQL owner and + * localhost license HTTP. Docker Q and configured catalog attestation are explicit + * synthetic fixture boundaries; this does not accept production quiescence. */ +import assert from "node:assert/strict" +import { createCipheriv, createHash, generateKeyPairSync, randomBytes, randomUUID, sign } from "node:crypto" +import { copyFile, mkdir, mkdtemp, rename, rm, writeFile } from "node:fs/promises" +import { dirname, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setImmediate as immediate } from "node:timers/promises" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import { bandwidthOperatorEnrollmentGrantSchema, createBandwidthOperatorEnrollmentPrerequisite } from "./bandwidth-operator-enrollment.js" +import { createBandwidthOperatorOriginalAuthorityReader, createBandwidthOperatorExternalFencer, createBandwidthOperatorWriterWindows, type BandwidthOperatorWriterWindow } from "./bandwidth-operator-writer-window.js" +import { createProbeStorageConnectionConnector } from "./server-probe-storage-connection.js" +import { createConfiguredExternalBandwidthPeerSource } from "./bandwidth-external-peer-source.js" +import { createConfiguredManagedBandwidthPeerSource } from "./bandwidth-managed-peer-source.js" +import type { createOwnedExternalRegistryHarness } from "./bandwidth-external-node-registry.sql-helper.js" + +type Harness = Awaited> +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +export async function acceptOwnedExternalPeerMetadata(f: Harness): Promise { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + assert.equal(process.env.MMAPP_OWN_EXTERNAL_PEER_METADATA, "true") + const parent = resolve(process.env.MCP_STATE_DIR!), folder = await mkdtemp(resolve(parent, "external-peer-metadata-")) + assert.equal(dirname(folder), parent) + const authorityFolder = resolve(folder, "authority"), sourceFolder = resolve(folder, "source") + await mkdir(authorityFolder); await mkdir(sourceFolder) + const trustKeyFile = resolve(authorityFolder, "trust.pem"), grantFile = resolve(authorityFolder, "grant.json") + const profileFile = resolve(sourceFolder, "profile.json"), keyFile = resolve(sourceFolder, "management.key") + const keys = generateKeyPairSync("ed25519"), key = randomBytes(32), keySha256 = sha(key) + const publicBytes = keys.publicKey.export({ type: "spki", format: "pem" }) + await writeFile(trustKeyFile, publicBytes, { flag: "wx", mode: 0o600 }) + await writeFile(keyFile, key, { flag: "wx", mode: 0o600 }) + const vector = { "system-id": "OWNED-EXTERNAL-PUBLIC-METADATA", "software-id": "OWNED-EXTERNAL-METADATA-SW" } + const enrollmentId = f.externalEndpoint(vector), peerRef = Object.freeze({ kind: "external" as const, enrollmentId }) + const setup = await f.readPeerSnapshot(peerRef, f.scope()); assert(setup) + const endpoint = setup.endpoint + const header = { format: "mmapp-bandwidth-external-management-v1", installationId: f.root.installationId, enrollmentId, + generation: randomUUID(), enabled: true, routingEndpointEpoch: endpoint.routingEndpointEpoch, keySha256 } + const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv) + cipher.setAAD(Buffer.from(canonical(header))) + const encrypted = Buffer.concat([cipher.update(Buffer.from(canonical({ host: endpoint.host, port: endpoint.port, useSsl: endpoint.useSsl, + verifySsl: endpoint.verifySsl, username: endpoint.username, password: endpoint.password }))), cipher.final()]) + const profile = { ...header, ciphertext: Buffer.concat([iv, cipher.getAuthTag(), encrypted]).toString("base64url") } + const profileBytes = Buffer.from(canonical(profile)), sourceProof = sha(profileBytes) + await writeFile(profileFile, profileBytes, { flag: "wx", mode: 0o600 }); key.fill(0) + const managedId = await f.managedEndpoint(vector), [managedRow] = await f.applicationRows([managedId]) + assert(managedRow) + const managedConfig = { serverId: managedId, sourceProof: sha(randomBytes(32)), authorityGeneration: randomUUID(), routingEndpointEpoch: String(managedRow.routing_endpoint_epoch) } + const checks: string[] = [], coreTables = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events", "bandwidth_nodes", "bandwidth_node_aliases", "bandwidth_external_node_aliases"] + const baseline = await f.digest(coreTables), posts = f.nativePosts() + const connector = createProbeStorageConnectionConnector(async () => f.owner!.connect()) + const gone = async (pid: number) => { + const end = performance.now() + 2000 + for (;;) { + await f.owner!.query("SELECT pg_catalog.pg_stat_clear_snapshot()") + if ((await f.owner!.query("SELECT count(*)::int AS n FROM pg_catalog.pg_stat_activity WHERE pid=$1", [pid])).rows[0].n === 0) return + assert(performance.now() < end); await immediate() + } + } + try { + for (const mode of ["positive", "generation", "ciphertext", "directory-clone", "grant", "copies", "managed-positive", "physical-owner-loss", "physical-owner-during-Q"] as const) { + console.log("Owned external peer metadata stage:", mode) + await writeFile(profileFile, profileBytes) + const currentPeer = mode === "managed-positive" ? Object.freeze({ kind: "server" as const, serverId: managedId }) : peerRef + const binding = mode === "managed-positive" ? managedConfig : { sourceProof, authorityGeneration: header.generation, routingEndpointEpoch: header.routingEndpointEpoch } + const now = Date.now(), grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", grantId: randomUUID(), + operatorId: "owned-external-metadata-operator", ...f.root, executorInventorySha256: sha("EXPLICIT OWNED SYNTHETIC DOCKER Q"), executorsStoppedAt: now - 1000, + issuedAt: now - 1000, expiresAt: now + 120000, peerRef: currentPeer, sourceProof: binding.sourceProof, authorityGeneration: binding.authorityGeneration, + routingEndpointEpoch: binding.routingEndpointEpoch, claims: Object.entries(vector).map(([kind, value]) => ({ kind, value })) }) + const signed = Buffer.from(canonical(grant)), grantBytes = Buffer.from(JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", + payload: signed.toString("base64url"), signature: sign(null, signed, keys.privateKey).toString("base64url") })) + await writeFile(grantFile, grantBytes, { mode: 0o600 }) + const config = { trustKeyFile, grantFile, trustKeySha256: sha(keys.publicKey.export({ type: "spki", format: "der" })), ...f.root } + const originals = createBandwidthOperatorOriginalAuthorityReader(config) + const scope = Object.freeze({ deadlineAt: Date.now() + 5000 }) + const original = await originals.acquire(scope) + const caseGets = f.nativeGets() + let q = 0, sql = 0, commits = 0, pid = 0, metadataPassed = false, negativeObserved = false + let lifetimeSignal: AbortSignal | undefined, armPostHeld = false, heldAcknowledged = false, injected = false + let injection: Promise | undefined + let callbackStarted = false, completeCallback!: () => void + const callbackDone = new Promise(resolve => { completeCallback = resolve }) + const joinCallback = async () => { + if (!callbackStarted) return + let timer: ReturnType | undefined + try { await Promise.race([callbackDone, new Promise((_resolve, reject) => { timer = setTimeout(() => reject(Error("Owned callback cleanup did not finish")), 3500) })]) } + finally { clearTimeout(timer) } + } + const terminateOwner = async () => { + assert(Number.isSafeInteger(pid) && pid > 0) + const terminated = await f.admin!.query("SELECT pg_catalog.pg_terminate_backend(pid) AS terminated FROM pg_catalog.pg_stat_activity WHERE pid=$1 AND datname=$2 AND usename=$3", [pid, f.databaseName, f.ownerRole]) + assert.equal(terminated.command, "SELECT"); assert.equal(terminated.rowCount, 1) + assert.equal(terminated.rows.length, 1); assert.equal(terminated.rows[0].terminated, true) + } + const external = createBandwidthOperatorExternalFencer({ async acquire() { return { async assertCurrent() { + q++ + if (mode === "physical-owner-during-Q" && armPostHeld && heldAcknowledged && !injection) { + injection = (async () => { + await terminateOwner(); await gone(pid) + assert(lifetimeSignal) + const signalEnd = Math.min(performance.now() + 1000, performance.now() + Math.max(0, scope.deadlineAt - Date.now())) + while (!lifetimeSignal.aborted) { assert(performance.now() < signalEnd); await immediate() } + assert(Date.now() < scope.deadlineAt, "transport must revoke before original deadline, not because it expired") + assert.equal(lifetimeSignal.aborted, true) + injected = true + })() + // Return this Q normally only after the actual transport lifetime is + // revoked. A preceding successful HELD row must no longer authorize. + await injection + } + }, close() {} } } }) + let window: BandwidthOperatorWriterWindow | undefined + const windows = createBandwidthOperatorWriterWindows({ originals, external, source: { + async open(current, guard) { + await guard(); const raw = await connector(current) + lifetimeSignal = raw.lifetimeSignal + let closed = false + const close = () => { if (!closed) { closed = true; raw.release(true) } } + return Object.freeze({ + connection: Object.freeze({ release: close, lifetimeSignal: raw.lifetimeSignal, async query(text: string, values?: unknown[], options?: { signal?: AbortSignal; timeoutMs: number }) { + await guard(); sql++; if (text === "COMMIT") commits++ + const result = await raw.query(text, values, options) + if (armPostHeld && text === "SELECT pg_catalog.pg_backend_pid() AS backend_pid, EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND pid=pg_catalog.pg_backend_pid() AND mode='ExclusiveLock' AND classid=0::pg_catalog.oid AND objid=8723116::pg_catalog.oid AND objsubid=1 AND granted) AS held") { + assert("command" in result); assert.equal(result.command, "SELECT") + assert.equal(result.rows.length, 1); assert.equal(result.rows[0].backend_pid, pid); assert.equal(result.rows[0].held, true) + heldAcknowledged = true + } + await guard() + if (text === "SELECT pg_catalog.pg_backend_pid() AS backend_pid") pid = Number(result.rows[0].backend_pid) + return result + } }), binding: async () => { await guard(); return Object.freeze({ ...f.root }) }, + keyedProof: async () => { throw Error("Owned metadata fixture does not provide HMAC authority") }, close, + }) + }, + }, registry: { async register(_original, borrowed, current) { + callbackStarted = true + assert(window) + const guard = windows.createPeerSourceGuard(window, original) + const source = mode === "managed-positive" ? createConfiguredManagedBandwidthPeerSource(managedConfig, guard) + : createConfiguredExternalBandwidthPeerSource({ profileFile, keyFile, installationId: f.root.installationId, enrollmentId, keySha256 }, guard) + const coordinator = createBandwidthOperatorEnrollmentPrerequisite(config, source, { async verify(_grant, requested) { await windows.verify(window!, original, requested) } }) + try { + if (mode === "physical-owner-loss") { + await terminateOwner() + const postCount = f.nativePosts() + await assert.rejects(coordinator.acquire(currentPeer, current)) + // verifyRead awaits the genuine held-owner check before key read / + // decrypt. A socket error may retire the window even earlier. + assert.equal(commits, 0); assert.equal(f.nativeGets(), caseGets); assert.equal(f.nativePosts(), postCount) + negativeObserved = true + throw Error("Owned expected physical owner loss: no secret/native effect or COMMIT") + } + if (mode === "physical-owner-during-Q") { + armPostHeld = true + await assert.rejects(coordinator.acquire(currentPeer, current)) + await injection; assert(heldAcknowledged && injected) + assert.equal(commits, 0); assert.equal(f.nativeGets(), caseGets); assert.equal(f.nativePosts(), posts) + negativeObserved = true + throw Error("Owned expected post-HELD owner loss: Q returns normally after transport revocation") + } + const prerequisite = await coordinator.acquire(currentPeer, current) + assert.equal(f.nativeGets(), caseGets + 1) + const ownership = await f.owner!.query("SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0::pg_catalog.oid AND objid=8723116::pg_catalog.oid AND objsubid=1 AND mode='ExclusiveLock' AND granted) AS held", [pid]) + assert.equal(ownership.rows[0].held, true) + const qBefore = q, sqlBefore = sql, getsBefore = f.nativeGets() + if (mode === "positive" || mode === "managed-positive") { + if (mode === "positive") await rename(keyFile, keyFile + ".held") + try { + const metadata = await coordinator.inspectMetadata(prerequisite, current) + assert.equal(metadata.format, "mmapp-bandwidth-operator-enrollment-metadata-v1") + assert.equal(metadata.metadata.evidence.peerRef.kind, currentPeer.kind) + assert.equal(JSON.stringify(metadata).includes('"endpoint"'), false) + assert.equal(JSON.stringify(metadata).includes(endpoint.password), false) + assert.equal(JSON.stringify(metadata).includes(endpoint.username), false) + assert.equal(q, qBefore); assert.equal(sql, sqlBefore); assert.equal(f.nativeGets(), getsBefore) + metadataPassed = true + } finally { if (mode === "positive") await rename(keyFile + ".held", keyFile) } + const began = await borrowed.connection.query("BEGIN", undefined, { timeoutMs: 1000 }) + assert("command" in began); assert.equal(began.command, "BEGIN") + const committed = await borrowed.connection.query("COMMIT", undefined, { timeoutMs: 1000 }) + assert("command" in committed); assert.equal(committed.command, "COMMIT") + borrowed.connection.release(true) + return "known-commit" + } + const inspect = () => coordinator.inspectMetadata(prerequisite, current) + if (mode === "generation") await writeFile(profileFile, canonical({ ...profile, generation: randomUUID() })) + if (mode === "ciphertext") await writeFile(profileFile, canonical({ ...profile, ciphertext: Buffer.concat([iv, randomBytes(16), encrypted]).toString("base64url") })) + if (mode === "grant") await writeFile(grantFile, "{}") + if (mode === "directory-clone") { + await rename(sourceFolder, sourceFolder + ".held"); await mkdir(sourceFolder) + try { + await copyFile(resolve(sourceFolder + ".held", "profile.json"), profileFile) + await copyFile(resolve(sourceFolder + ".held", "management.key"), keyFile) + await assert.rejects(inspect) + } finally { assert.equal(dirname(sourceFolder), folder); await rm(sourceFolder, { recursive: true }); await rename(sourceFolder + ".held", sourceFolder) } + } else if (mode === "copies") { + await assert.rejects(coordinator.inspectMetadata({ ...prerequisite }, current)) + await assert.rejects(coordinator.inspectMetadata(JSON.parse(JSON.stringify(prerequisite)), current)) + assert.throws(() => createConfiguredExternalBandwidthPeerSource({ profileFile, keyFile, installationId: f.root.installationId, enrollmentId, keySha256 }, { ...guard })) + } else await assert.rejects(inspect) + negativeObserved = true + assert.equal(sql, sqlBefore); assert.equal(q, qBefore); assert.equal(f.nativeGets(), getsBefore) + throw Error("Owned expected negative: no COMMIT or automatic replay") + } finally { try { coordinator.close(); source.close() } finally { completeCallback() } } + } } }) + try { + window = await windows.open(original, scope) + if (mode === "positive" || mode === "managed-positive") { assert.equal(await windows.register(window), "known-commit"); assert(metadataPassed) } + else { await assert.rejects(windows.register(window)); await joinCallback(); assert(negativeObserved) } + } finally { windows.close(); originals.close(); await writeFile(grantFile, grantBytes); await writeFile(profileFile, profileBytes) } + assert(pid > 0); await gone(pid) + if (mode === "physical-owner-loss" || mode === "physical-owner-during-Q") assert.equal(commits, 0) + checks.push(mode === "positive" ? "genuine signed original/window guard, actual held PostgreSQL PID/session lock and localhost license GET; encrypted external metadata succeeds with key absent and zero Q/control-SQL/native/projection" + : mode === "managed-positive" ? "genuine managed guarded credentials use actual application row epoch; actual localhost GET and same physical owner; metadata fixed application boolean comparison adds no Q/control-SQL/native or credential projection" + : mode === "physical-owner-loss" ? "only exact owned database/owner/PID terminated; genuine window/source guard refuses before initial native GET and COMMIT; ordered read guard precedes key/decrypt; physical PID disappears" + : mode === "physical-owner-during-Q" ? "actual HELD ACK followed by pinned-owner termination during awaited Q; raw lifetime signal revoked before normal Q return; no key/decrypt admission, native GET or COMMIT" + : "owned external metadata refuses " + mode + " without new Q/SQL/native calls or automatic replay; physical owner retires") + } + assert.equal(await f.digest(coreTables), baseline); assert.equal(f.nativePosts(), posts) + return Object.freeze(checks) + } finally { + assert.equal(dirname(folder), parent); await rm(folder, { recursive: true }) + } +} diff --git a/backend/src/services/bandwidth-external-peer-metadata.sql.test.ts b/backend/src/services/bandwidth-external-peer-metadata.sql.test.ts new file mode 100644 index 00000000..e030ab04 --- /dev/null +++ b/backend/src/services/bandwidth-external-peer-metadata.sql.test.ts @@ -0,0 +1,23 @@ +/** Exclusive disposable SQL/localhost profile, not a real Docker Q test. */ +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +async function main() { + assertOwnedBandwidthSql() + assert.equal(process.env.MMAPP_OWN_EXTERNAL_PEER_METADATA, "true") + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + const { createOwnedExternalRegistryHarness } = await import("./bandwidth-external-node-registry.sql-helper.js") + const { acceptOwnedExternalPeerMetadata } = await import("./bandwidth-external-peer-metadata.sql-helper.js") + const fixture = await createOwnedExternalRegistryHarness() + try { + const checks = await acceptOwnedExternalPeerMetadata(fixture) + console.log("Owned guarded peer sources accepted:", JSON.stringify({ + groups: checks.length, checks, actualSql: true, genuineLocalhostLicense: true, + guardedManagedAndExternalSources: true, originalWindowMs: 5000, + quiescenceVerifier: "synthetic-fixture", catalogSource: "synthetic-fixture", + productionOperatorWindowAccepted: false, productionWired: false, + })) + } finally { await fixture.close() } +} + +void main().catch(() => { console.error("Owned guarded peer-source acceptance failed; no real CHR/Docker actions"); process.exitCode = 1 }) diff --git a/backend/src/services/bandwidth-external-peer-source.test.ts b/backend/src/services/bandwidth-external-peer-source.test.ts new file mode 100644 index 00000000..f7e481b4 --- /dev/null +++ b/backend/src/services/bandwidth-external-peer-source.test.ts @@ -0,0 +1,215 @@ +/** Real AES-GCM/Ed25519/files and genuine PRIVATE branded operator-window + * guard. Only external Q + physical SQL are a labelled synthetic fixture. + * No PostgreSQL, RouterOS, daemon or production credentials. */ +import assert from "node:assert/strict" +import { createCipheriv, createHash, generateKeyPairSync, randomBytes, randomUUID, sign } from "node:crypto" +import { basename, dirname, join } from "node:path" +import { tmpdir } from "node:os" +import { mock, test } from "node:test" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" + +const fs=await import("node:fs/promises"),actual=await import("./bandwidth-control-source.js") +const reads:string[]=[],gates=new Map>(),keyPaths=new Set(),keyBuffers:Buffer[]=[] +mock.module("./bandwidth-control-source.js",{namedExports:{...actual,async readBandwidthControlFile(path:string,max:number,live:()=>void){ + reads.push(path);const bytes=await actual.readBandwidthControlFile(path,max,live) + if(keyPaths.has(path))keyBuffers.push(bytes) + await gates.get(path);return bytes +}}}) +const {createConfiguredExternalBandwidthPeerSource,BandwidthExternalPeerSourceError}=await import("./bandwidth-external-peer-source.js") +const {createBandwidthOperatorOriginalAuthorityReader,createBandwidthOperatorExternalFencer,createBandwidthOperatorWriterWindows}=await import("./bandwidth-operator-writer-window.js") +const {bandwidthOperatorEnrollmentGrantSchema}=await import("./bandwidth-operator-enrollment.js") +const sha=(value:string|Buffer)=>createHash("sha256").update(value).digest("hex") +const scope=(ms=5000,signal?:AbortSignal)=>({deadlineAt:Date.now()+ms,signal}) +const turn=()=>new Promise(done=>setImmediate(done)) +const deferred=()=>{let resolve!:()=>void;const promise=new Promise(done=>{resolve=done});return {promise,resolve}} +const denied=(error:unknown)=>error instanceof BandwidthExternalPeerSourceError + +async function fixture(options:{badAad?:boolean;badTag?:boolean;noncanonicalPlain?:boolean;profileInstallationId?:string}={}){ + const directory=await fs.mkdtemp(join(await fs.realpath(tmpdir()),"mm-external-peer-")),profileDirectory=join(directory,"profile"),keyDirectory=join(directory,"keys") + await fs.mkdir(profileDirectory);await fs.mkdir(keyDirectory) + const key=randomBytes(32),operator=generateKeyPairSync("ed25519"),root={installationId:randomUUID(),issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64)} + const config={profileFile:join(profileDirectory,"management.json"),keyFile:join(keyDirectory,"management.key"),installationId:options.profileInstallationId??root.installationId,enrollmentId:randomUUID(),keySha256:sha(key)} + const endpoint={host:"127.0.0.1",port:80,useSsl:false,verifySsl:true,username:"OWNED_EXTERNAL_USER",password:"OWNED_EXTERNAL_PRIVATE_PASSWORD"} + const header={format:"mmapp-bandwidth-external-management-v1" as const,installationId:config.installationId,enrollmentId:config.enrollmentId,generation:randomUUID(),enabled:true,routingEndpointEpoch:randomUUID(),keySha256:config.keySha256} + function encrypt(value=endpoint,aad:unknown=header){ + const iv=randomBytes(12),cipher=createCipheriv("aes-256-gcm",key,iv);cipher.setAAD(Buffer.from(canonical(aad))) + const bytes=Buffer.concat([cipher.update((options.noncanonicalPlain?" ":"")+canonical(value)),cipher.final()]) + return Buffer.concat([iv,cipher.getAuthTag(),bytes]).toString("base64url") + } + let profile={...header,ciphertext:encrypt(endpoint,options.badAad?{...header,generation:randomUUID()}:header)} + if(options.badTag){const bytes=Buffer.from(profile.ciphertext,"base64url");bytes[12]^=1;profile={...profile,ciphertext:bytes.toString("base64url")}} + const publish=()=>fs.writeFile(config.profileFile,canonical(profile)) + await publish();await fs.writeFile(config.keyFile,key);keyPaths.add(config.keyFile) + const trustKeyFile=join(directory,"operator.pem"),grantFile=join(directory,"operator.json") + await fs.writeFile(trustKeyFile,operator.publicKey.export({type:"spki",format:"pem"})) + let grant=bandwidthOperatorEnrollmentGrantSchema.parse({format:"mmapp-bandwidth-operator-enrollment-v1",...root,grantId:randomUUID(),operatorId:"SYNTHETIC-EXTERNAL-STORAGE-OPERATOR",executorInventorySha256:"c".repeat(64),executorsStoppedAt:Date.now()-2000,issuedAt:Date.now()-1000,expiresAt:Date.now()+30000, + peerRef:{kind:"external",enrollmentId:config.enrollmentId},sourceProof:sha(canonical(profile)),authorityGeneration:profile.generation,routingEndpointEpoch:profile.routingEndpointEpoch,claims:[{kind:"system-id",value:"SYNTHETIC-EXTERNAL-IDENTITY"}]}) + async function publishGrant(){const bytes=Buffer.from(canonical(grant));await fs.writeFile(grantFile,JSON.stringify({format:"mmapp-bandwidth-operator-enrollment-envelope-v1",payload:bytes.toString("base64url"),signature:sign(null,bytes,operator.privateKey).toString("base64url")}))} + await publishGrant() + const originals=createBandwidthOperatorOriginalAuthorityReader({...root,trustKeyFile,grantFile,trustKeySha256:sha(operator.publicKey.export({type:"spki",format:"der"}))}) + let q=0,sql=0,held=false,destroyed=0,allowed=true + const controls:{onQ?:()=>Promise}={} + const external=createBandwidthOperatorExternalFencer({async acquire(){return {async assertCurrent(){q++;await controls.onQ?.();if(!allowed)throw Error("synthetic Q revoked")},close(){}}}}) + const windows=createBandwidthOperatorWriterWindows({originals,external,source:{async open(_scope,guard){await guard();const lifetime=new AbortController();let closed=false;return { + async binding(){await guard();return root},async keyedProof(){assert.fail("external storage cannot request MACs")},close(){if(closed)return;closed=true;lifetime.abort();destroyed++;held=false},connection:Object.freeze({lifetimeSignal:lifetime.signal,release(){assert.fail("physical release is window owner only")},async query(text:string){sql++;await guard();if(text.includes("pg_try_advisory_lock(")){held=true;return {rows:[{acquired:true}]}}if(text.includes("pg_locks"))return {rows:[{backend_pid:71,held}]};if(text.includes("pg_backend_pid()"))return {rows:[{backend_pid:71}]};throw Error("unexpected synthetic SQL")}}), + }}},registry:{async register(){assert.fail("no registration in source fixture")}}}) + const lifetime=scope(),original=await originals.acquire(lifetime),window=await windows.open(original,lifetime),guard=windows.createPeerSourceGuard(window,original) + const before=reads.length,source=createConfiguredExternalBandwidthPeerSource({...config,installationId:root.installationId},guard) + assert.equal(reads.length,before,"source constructor must be inert") + return {source,config,endpoint,header,guard,windows,originals,grantFile,profileDirectory,controls,get profile(){return profile},set profile(value){profile=value},get grant(){return grant},set grant(value){grant=value},publish,publishGrant,encrypt,get q(){return q},get sql(){return sql},get destroyed(){return destroyed},revoke(){allowed=false},lose(){held=false},get peer(){return {kind:"external" as const,enrollmentId:config.enrollmentId}},async close(){ + source.close();windows.close();originals.close();gates.delete(config.keyFile);keyPaths.delete(config.keyFile);key.fill(0) + await turn();await turn() + const actual=await fs.realpath(directory),parent=await fs.realpath(tmpdir()),stat=await fs.lstat(directory) + assert(stat.isDirectory()&&!stat.isSymbolicLink());assert.equal(dirname(actual),parent);assert.match(basename(actual),/^mm-external-peer-[A-Za-z0-9]+$/) + await fs.rm(actual,{recursive:true,force:true}) + }} +} + +test("real AES-GCM private read under genuine guard; metadata fresh file checks with key missing perform zero Q/SQL/key reads",async()=>{ + const f=await fixture(),firstKey=keyBuffers.length + try{ + const perCall=new AbortController(),expected=await f.source.read(f.peer,scope(5000,perCall.signal));assert(expected) + assert.deepEqual(expected.endpoint,{...f.endpoint,routingEndpointEpoch:f.header.routingEndpointEpoch}) + assert.equal(expected.sourceProof,sha(canonical(f.profile)));assert.equal(expected.authorityGeneration,f.profile.generation) + assert(Object.isFrozen(expected));assert(Object.isFrozen(expected.endpoint));assert.equal(keyBuffers.length-firstKey,1);assert(keyBuffers.at(-1)!.every(byte=>byte===0)) + await fs.rename(f.config.keyFile,f.config.keyFile+".absent") + perCall.abort() // A reader disposes its temporary IO signal on success. + const q=f.q,sql=f.sql,keyReads=reads.filter(path=>path===f.config.keyFile).length + await f.source.assertMetadataCurrent!(structuredClone(expected),scope()) + assert.equal(f.q,q);assert.equal(f.sql,sql);assert.equal(reads.filter(path=>path===f.config.keyFile).length,keyReads) + assert.equal(f.destroyed,0) + }finally{await f.close()} +}) + +test("anonymous/copied/structural guard and proxy/accessor configuration cannot construct a credential source",async()=>{ + const f=await fixture();let called=0;const trap=()=>{called++;throw Error("caller getter")} + try{ + for(const guard of [{verifyRead:async()=>{},verifyMetadata:async()=>{}},{...f.guard},new Proxy(f.guard,{get:trap}),{}])assert.throws(()=>createConfiguredExternalBandwidthPeerSource(f.config,guard as never),denied) + for(const config of [Object.defineProperty({...f.config},"profileFile",{get:trap}),new Proxy(f.config,{getPrototypeOf:trap}),{...f.config,toJSON:trap}])assert.throws(()=>createConfiguredExternalBandwidthPeerSource(config,f.guard),denied) + assert.equal(called,0) + }finally{await f.close()} +}) + +test("encrypted profile/config installation cannot cross genuine original guard installation, even with signed exact sourceProof",async()=>{ + const f=await fixture({profileInstallationId:randomUUID()}) + try{ + assert.notEqual(f.config.installationId,f.guard.installationId) + assert.equal(f.profile.installationId,f.config.installationId) + assert.equal(f.grant.sourceProof,sha(canonical(f.profile))) + const before=reads.length,q=f.q,sql=f.sql + assert.throws(()=>createConfiguredExternalBandwidthPeerSource(f.config,f.guard),denied) + assert.equal(reads.length,before);assert.equal(f.q,q);assert.equal(f.sql,sql) + }finally{await f.close()} +}) + +test("Q/session/original revocation happens BEFORE key read; no failed read publishes a metadata binding",async()=>{ + for(const mode of ["Q","session","grant"]){ + const f=await fixture() + try{ + const keyReads=reads.filter(path=>path===f.config.keyFile).length + if(mode==="Q")f.revoke();if(mode==="session")f.lose();if(mode==="grant")await fs.unlink(f.grantFile) + await assert.rejects(f.source.read(f.peer,scope()),denied) + assert.equal(reads.filter(path=>path===f.config.keyFile).length,keyReads) + await assert.rejects(f.source.assertMetadataCurrent!({} as never,scope()),denied) + assert.equal(f.destroyed,1) + }finally{await f.close()} + } +}) + +test("AAD/auth-tag/noncanonical plaintext and changed key pin reject; no key remains retained after failure",async()=>{ + for(const mode of ["aad","tag","canonical","key"]){ + const f=await fixture({badAad:mode==="aad",badTag:mode==="tag",noncanonicalPlain:mode==="canonical"}),before=keyBuffers.length + try{ + if(mode==="key")await fs.writeFile(f.config.keyFile,randomBytes(32)) + await assert.rejects(f.source.read(f.peer,scope()),denied) + for(const key of keyBuffers.slice(before))assert(key.every(byte=>byte===0)) + }finally{await f.close()} + } +}) + +test("fresh encrypted profile/gen/ciphertext/enabled changes and same-byte directory clone permanently refuse original metadata",async()=>{ + for(const mode of ["generation","ciphertext","disabled","directory"]){ + const f=await fixture() + try{ + const expected=await f.source.read(f.peer,scope());assert(expected) + const q=f.q,sql=f.sql,keys=reads.filter(path=>path===f.config.keyFile).length + if(mode==="directory"){ + const bytes=await fs.readFile(f.config.profileFile),target=f.profileDirectory+"-old",stat=await fs.lstat(f.profileDirectory) + assert(stat.isDirectory()&&!stat.isSymbolicLink());assert.equal(await fs.realpath(f.profileDirectory),f.profileDirectory) + assert.equal(dirname(target),dirname(f.profileDirectory));assert.match(basename(dirname(target)),/^mm-external-peer-[A-Za-z0-9]+$/) + await fs.rename(f.profileDirectory,target);await fs.mkdir(f.profileDirectory);await fs.writeFile(f.config.profileFile,bytes) + }else{f.profile={...f.profile,...(mode==="generation"?{generation:randomUUID()}:mode==="disabled"?{enabled:false}:{ciphertext:f.encrypt()})};await f.publish()} + await assert.rejects(f.source.assertMetadataCurrent!(expected,scope()),denied) + assert.equal(f.q,q);assert.equal(f.sql,sql);assert.equal(reads.filter(path=>path===f.config.keyFile).length,keys) + await assert.rejects(f.source.read(f.peer,scope()),denied) + }finally{await f.close()} + } +}) + +test("metadata requires this instance's successful original private binding; mismatched credentials never read key",async()=>{ + const f=await fixture() + try{ + const noRead=createConfiguredExternalBandwidthPeerSource(f.config,f.guard),before=reads.length + await assert.rejects(noRead.assertMetadataCurrent!({} as never,scope()),denied);assert.equal(reads.length,before) + const expected=await f.source.read(f.peer,scope());assert(expected) + const changed={...expected,endpoint:{...expected.endpoint,password:"wrong"}},keys=reads.filter(path=>path===f.config.keyFile).length + await assert.rejects(f.source.assertMetadataCurrent!(changed,scope()),denied);assert.equal(reads.filter(path=>path===f.config.keyFile).length,keys) + }finally{await f.close()} +}) + +test("abort/close during delayed key result refuses promptly and late key buffer is wiped; lifetime cannot resume",async()=>{ + for(const mode of ["abort","close","deadline"]){ + const f=await fixture(),gate=deferred(),controller=new AbortController(),before=keyBuffers.length + try{ + gates.set(f.config.keyFile,gate.promise) + const input=scope(mode==="deadline"?200:5000,controller.signal),pending=f.source.read(f.peer,input);void pending.catch(()=>{}) + const end=Date.now()+2000;while(keyBuffers.length===before&&Date.now()byte===0)) + await assert.rejects(f.source.read(f.peer,scope()),denied) + }finally{gate.resolve();await f.close()} + } +}) + +test("missing/malformed/disabled/foreign public profile and missing key refuse without bootstrap or fallback",async()=>{ + for(const mode of ["missing-profile","json","disabled","foreign","missing-key"]){ + const f=await fixture() + try{ + if(mode==="missing-profile")await fs.unlink(f.config.profileFile) + if(mode==="json")await fs.writeFile(f.config.profileFile,"{") + if(mode==="disabled"){f.profile={...f.profile,enabled:false};await f.publish()} + if(mode==="foreign"){f.profile={...f.profile,installationId:randomUUID()};await f.publish()} + if(mode==="missing-key")await fs.unlink(f.config.keyFile) + const before=reads.filter(path=>path===f.config.keyFile).length + await assert.rejects(f.source.read(f.peer,scope()),denied) + assert.equal(reads.filter(path=>path===f.config.keyFile).length-before,mode==="missing-key"?1:0) + await assert.rejects(f.source.read(f.peer,scope()),denied) + }finally{await f.close()} + } +}) + +test("source first wall/monotonic horizon cannot be renewed or rolled back by later metadata scopes",async()=>{ + for(const mode of ["expired","rollback"]){ + const f=await fixture(),now=Date.now + try{ + const wall=now(),expected=await f.source.read(f.peer,scope());assert(expected) + const q=f.q,sql=f.sql,keys=reads.filter(path=>path===f.config.keyFile).length + Date.now=()=>mode==="expired"?wall+6000:wall-1000 + await assert.rejects(f.source.assertMetadataCurrent!(expected,scope()),denied) + assert.equal(f.q,q);assert.equal(f.sql,sql);assert.equal(reads.filter(path=>path===f.config.keyFile).length,keys) + }finally{Date.now=now;await f.close()} + } +}) + +test("metadata grant withdrawal rejects without Q/SQL/key; copies/getters cannot change private expected snapshot",async()=>{ + const f=await fixture();let called=0 + try{ + const expected=await f.source.read(f.peer,scope());assert(expected) + const q=f.q,sql=f.sql,keys=reads.filter(path=>path===f.config.keyFile).length + await fs.unlink(f.grantFile) + await assert.rejects(f.source.assertMetadataCurrent!(structuredClone(expected),scope()),denied) + assert.equal(f.q,q);assert.equal(f.sql,sql);assert.equal(reads.filter(path=>path===f.config.keyFile).length,keys) + const hostile=Object.defineProperty({...expected},"endpoint",{get(){called++;return expected.endpoint}}) + await assert.rejects(f.source.assertMetadataCurrent!(hostile,scope()),denied);assert.equal(called,0) + }finally{await f.close()} +}) diff --git a/backend/src/services/bandwidth-external-peer-source.ts b/backend/src/services/bandwidth-external-peer-source.ts new file mode 100644 index 00000000..a29eea20 --- /dev/null +++ b/backend/src/services/bandwidth-external-peer-source.ts @@ -0,0 +1,150 @@ +/** Explicitly configured encrypted external management endpoint, one peer and + * one original operator-window lifetime. No provisioning/default source or + * wire mapping. Public metadata never certifies keys, Q or current license. */ +import { createDecipheriv, createHash } from "node:crypto" +import { lstat, realpath } from "node:fs/promises" +import { dirname, isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual, types } from "node:util" +import { z } from "zod" +import { readBandwidthControlFile } from "./bandwidth-control-source.js" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import { parseBandwidthPeerRef, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthNodeObservationScope } from "./bandwidth-node-observation.js" +import type { BandwidthPeerSourceSnapshot, BandwidthPeerTrustedSource } from "./bandwidth-peer-observation.js" +import { assertBandwidthOperatorPeerSourceGuard, type BandwidthOperatorPeerSourceGuard, type BandwidthOperatorPeerSourceBinding } from "./bandwidth-operator-writer-window.js" + +export interface ExternalBandwidthPeerSourceConfig { + profileFile:string;keyFile:string;installationId:string;enrollmentId:string;keySha256:string +} +export class BandwidthExternalPeerSourceError extends Error { + readonly code="BANDWIDTH_EXTERNAL_PEER_SOURCE_UNAVAILABLE" + constructor(){super("Configured external peer source unavailable")} +} +const fail=():never=>{throw new BandwidthExternalPeerSourceError()} +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid().refine(value=>value===value.toLowerCase()) +const profileSchema=z.object({format:z.literal("mmapp-bandwidth-external-management-v1"),installationId:uuid,enrollmentId:uuid,generation:uuid,enabled:z.boolean(),routingEndpointEpoch:uuid,keySha256:hash,ciphertext:z.string().min(39).max(131072).regex(/^[A-Za-z0-9_-]+$/)}).strict() +const endpointSchema=z.object({host:z.string().min(1).max(1000).refine(value=>!/[\s/\\\x00-\x1f\x7f]/.test(value)),port:z.number().int().min(1).max(65535),useSsl:z.boolean(),verifySsl:z.boolean(),username:z.string().refine(value=>Buffer.byteLength(value)<=1000),password:z.string().refine(value=>Buffer.byteLength(value)<=65536)}).strict() +const sha=(value:string|Buffer)=>createHash("sha256").update(value).digest("hex") +function fields(value:unknown,names:readonly string[],optional=false){ + if(!value||typeof value!=="object"||types.isProxy(value)||Object.getPrototypeOf(value)!==Object.prototype)fail() + const descriptors=Object.getOwnPropertyDescriptors(value) + if(Reflect.ownKeys(descriptors).some(key=>typeof key!=="string"||!names.includes(key))||!optional&&Object.keys(descriptors).length!==names.length||Object.values(descriptors).some(field=>!("value"in field)||!field.enumerable))fail() + return descriptors +} +function peer(value:unknown,enrollmentId:string){ + fields(value,["kind","enrollmentId"]) + const parsed=parseBandwidthPeerRef(value) + if(parsed.kind!=="external"||parsed.enrollmentId!==enrollmentId)fail() + return parsed +} +type Directory={dev:number;ino:number} +/** Trusted server construction only. Profile/AAD is canonical UTF-8 JSON. + * ciphertext is base64url(iv12 || GCM-tag16 || ciphertext), following the + * existing seal framing with NEW AAD binding ALL public header fields. */ +export function createConfiguredExternalBandwidthPeerSource(raw:ExternalBandwidthPeerSourceConfig,guard:BandwidthOperatorPeerSourceGuard):BandwidthPeerTrustedSource&{close():void}{ + let config:Readonly + try{ + const data=fields(raw,["profileFile","keyFile","installationId","enrollmentId","keySha256"]) + config=Object.freeze({profileFile:data.profileFile.value,keyFile:data.keyFile.value,installationId:uuid.parse(data.installationId.value),enrollmentId:uuid.parse(data.enrollmentId.value),keySha256:hash.parse(data.keySha256.value)}) + if([config.profileFile,config.keyFile].some(path=>typeof path!=="string"||!isAbsolute(path)||resolve(path)!==path)||config.profileFile===config.keyFile)fail() + assertBandwidthOperatorPeerSourceGuard(guard) + if(config.installationId!==guard.installationId)fail() + }catch{return fail()} + const verifyRead=guard.verifyRead.bind(guard),verifyMetadata=guard.verifyMetadata.bind(guard),directory=dirname(config.profileFile) + let closed=false,busy=false,directoryPin:Directory|undefined,horizon:{wall:number;end:number;deadlineAt:number}|undefined + let retained:Readonly|undefined + const operations=new Set() + const close=()=>{closed=true;retained=undefined;for(const operation of operations)operation.abort()} + function operation(input:BandwidthNodeObservationScope){ + const wall=Date.now(),mono=performance.now(),data=fields(input,["deadlineAt","signal"],true),requested:unknown=data.deadlineAt?.value,signal:unknown=data.signal?.value + if(closed||busy||typeof requested!=="number"||!Number.isSafeInteger(requested)||signal!==undefined&&(types.isProxy(signal)||!(signal instanceof AbortSignal)))fail() + const external=signal as AbortSignal|undefined,aborted=Object.getOwnPropertyDescriptor(AbortSignal.prototype,"aborted")!.get! + const isAborted=()=>external?Reflect.apply(aborted,external,[]):false + isAborted() + const deadlineAt=Math.min(requested as number,wall+5000,horizon?.deadlineAt??Infinity),end=Math.min(mono+Math.max(0,deadlineAt-wall),horizon?.end??Infinity) + if(wall<(horizon?.wall??wall)||wall>=deadlineAt||mono>=end||isAborted())fail() + horizon??={wall,end,deadlineAt};busy=true + const controller=new AbortController(),scope=Object.freeze({deadlineAt,signal:controller.signal}),abort=()=>controller.abort() + let rejectStopped!:(error:BandwidthExternalPeerSourceError)=>void + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject});void stopped.catch(()=>{}) + const stop=()=>{close();rejectStopped(new BandwidthExternalPeerSourceError())} + controller.signal.addEventListener("abort",stop,{once:true});operations.add(controller) + const timer=setTimeout(abort,Math.max(1,Math.ceil(end-performance.now()))) + if(external)Reflect.apply(EventTarget.prototype.addEventListener,external,["abort",abort,{once:true}]) + const live=()=>{const now=Date.now();if(closed||controller.signal.aborted||isAborted()||now=deadlineAt||performance.now()>=end)fail()} + return {scope,live,stopped,dispose(){clearTimeout(timer);if(external)Reflect.apply(EventTarget.prototype.removeEventListener,external,["abort",abort]);controller.signal.removeEventListener("abort",stop);operations.delete(controller);controller.abort();busy=false}} + } + async function dir(live:()=>void){ + live();if(await realpath(directory)!==directory)fail();live() + const stat=await lstat(directory);live();if(!stat.isDirectory()||stat.isSymbolicLink())fail() + const value={dev:stat.dev,ino:stat.ino} + if(directoryPin&&!isDeepStrictEqual(value,directoryPin))fail() + return value + } + async function publicProfile(live:()=>void){ + const before=await dir(live),bytes=await readBandwidthControlFile(config.profileFile,131072,live),after=await dir(live) + if(!isDeepStrictEqual(before,after))fail();directoryPin??=Object.freeze(before) + const text=new TextDecoder("utf-8",{fatal:true}).decode(bytes),profile=profileSchema.parse(JSON.parse(text)) + if(canonical(profile)!==text||profile.installationId!==config.installationId||profile.enrollmentId!==config.enrollmentId||profile.keySha256!==config.keySha256||!profile.enabled)fail() + const binding=Object.freeze({peerRef:Object.freeze({kind:"external" as const,enrollmentId:config.enrollmentId}),sourceProof:sha(text),authorityGeneration:profile.generation,routingEndpointEpoch:profile.routingEndpointEpoch}) + if(retained&&(retained.sourceProof!==binding.sourceProof||retained.authorityGeneration!==binding.authorityGeneration||retained.endpoint.routingEndpointEpoch!==binding.routingEndpointEpoch))fail() + live();return {profile,binding} + } + function expectedSnapshot(raw:unknown){ + const data=fields(raw,["peerRef","enabled","sourceProof","authorityGeneration","endpoint"]),endpoint=fields(data.endpoint.value,["host","port","useSsl","verifySsl","username","password","routingEndpointEpoch"]) + const values=Object.fromEntries(Object.entries(endpoint).filter(([name])=>name!=="routingEndpointEpoch").map(([name,field])=>[name,field.value])),captured=endpointSchema.parse(values) + if(data.enabled.value!==true)fail() + return Object.freeze({peerRef:peer(data.peerRef.value,config.enrollmentId),enabled:true,sourceProof:hash.parse(data.sourceProof.value),authorityGeneration:uuid.parse(data.authorityGeneration.value),endpoint:Object.freeze({...captured,routingEndpointEpoch:uuid.parse(endpoint.routingEndpointEpoch.value)})}) + } + return Object.freeze({ + async read(rawPeer:BandwidthPeerRef,input:Readonly){ + let owned:ReturnType|undefined + try{ + peer(rawPeer,config.enrollmentId);owned=operation(input) + return await Promise.race([owned.stopped,(async()=>{ + let key:Buffer|undefined,plain:Buffer|undefined + const pieces:Buffer[]=[] + try{ + const first=await publicProfile(owned!.live) + await verifyRead(first.binding,owned!.scope);owned!.live() + key=await readBandwidthControlFile(config.keyFile,32,owned!.live);owned!.live() + if(key.length!==32||sha(key)!==config.keySha256)fail() + await verifyRead(first.binding,owned!.scope);owned!.live() + const data=Buffer.from(first.profile.ciphertext,"base64url") + if(data.length<29||data.toString("base64url")!==first.profile.ciphertext)fail() + const {ciphertext:_ciphertext,...header}=first.profile;void _ciphertext + const decipher=createDecipheriv("aes-256-gcm",key,data.subarray(0,12));decipher.setAAD(Buffer.from(canonical(header)));decipher.setAuthTag(data.subarray(12,28)) + pieces.push(decipher.update(data.subarray(28)));pieces.push(decipher.final());plain=Buffer.concat(pieces) + const text=new TextDecoder("utf-8",{fatal:true}).decode(plain),endpoint=endpointSchema.parse(JSON.parse(text)) + if(canonical(endpoint)!==text)fail() + const value=Object.freeze({peerRef:first.binding.peerRef,enabled:true,sourceProof:first.binding.sourceProof,authorityGeneration:first.binding.authorityGeneration,endpoint:Object.freeze({...endpoint,routingEndpointEpoch:first.binding.routingEndpointEpoch})}) + const final=await publicProfile(owned!.live) + if(!isDeepStrictEqual(final.binding,first.binding))fail() + await verifyRead(first.binding,owned!.scope);owned!.live() + if(retained&&!isDeepStrictEqual(retained,value))fail();retained??=value + return retained + }finally{key?.fill(0);plain?.fill(0);for(const piece of pieces)piece.fill(0)} + })()]) + }catch{close();throw new BandwidthExternalPeerSourceError()} + finally{owned?.dispose()} + }, + async assertMetadataCurrent(expected:Readonly,input:Readonly){ + let owned:ReturnType|undefined + try{ + const captured=expectedSnapshot(expected) + if(!retained||!isDeepStrictEqual(captured,retained))fail() + owned=operation(input) + await Promise.race([owned.stopped,(async()=>{ + const binding:Readonly=Object.freeze({peerRef:retained!.peerRef,sourceProof:retained!.sourceProof,authorityGeneration:retained!.authorityGeneration,routingEndpointEpoch:retained!.endpoint.routingEndpointEpoch}) + await verifyMetadata(binding,owned!.scope);owned!.live() + const current=await publicProfile(owned!.live) + if(!isDeepStrictEqual(current.binding,binding))fail() + await verifyMetadata(binding,owned!.scope);owned!.live() + })()]) + }catch{close();throw new BandwidthExternalPeerSourceError()} + finally{owned?.dispose()} + }, + close, + }) +} diff --git a/backend/src/services/bandwidth-journal-durability.sql-helper.ts b/backend/src/services/bandwidth-journal-durability.sql-helper.ts new file mode 100644 index 00000000..1d8bfa52 --- /dev/null +++ b/backend/src/services/bandwidth-journal-durability.sql-helper.ts @@ -0,0 +1,122 @@ +/** Owned-local PostgreSQL regression only. It deliberately changes disposable + * runtime SESSION defaults and observes real driver COMMITs; no native traffic. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { Pool } from "pg" +import type { createConfiguredBandwidthControlSource } from "./bandwidth-control-source.js" +import type { BandwidthBinding, BandwidthScope } from "./bandwidth-admission.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import { assertOwnedBandwidthSql, fixtureBinding, completionProof, scope } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() + +export async function acceptBandwidthJournalDurability(source: ReturnType, owner: Pool, controlGeneration: string) { + assertOwnedBandwidthSql() + const { Client } = await import("pg") + const { createBandwidthAdmissionSqlJournal } = await import("./bandwidth-admission-storage.js") + const connect = source.connect.bind(source), eventProof = source.eventProofOnConnection.bind(source) + const originalQuery = Client.prototype.query, pids = new Set(), commitSettings: string[] = [] + const held = new Set() + let patched = false, allDefaultsRestored = false + const acquireFour = async () => { + // Retain the completed siblings too if one checkout fails: finally must + // never leave a session owned by this helper checked out. + const results = await Promise.allSettled(Array.from({ length: 4 }, async () => { const lease = await connect(scope()); held.add(lease); return lease })) + for (const result of results) if (result.status === "rejected") throw result.reason + return results.map(result => (result as PromiseFulfilledResult).value) + } + const release = (lease: ProbeStorageConnection) => { held.delete(lease); lease.release() } + try { + const initial = await acquireFour() + for (const lease of initial) { + await lease.query("SET SESSION synchronous_commit=off") + const row = (await lease.query("SELECT pg_catalog.pg_backend_pid() AS pid,pg_catalog.current_setting('synchronous_commit') AS setting")).rows[0] + assert.equal(row.setting, "off"); pids.add(Number(row.pid)) + } + assert.equal(pids.size, 4) + initial.forEach(release) + Client.prototype.query = function (this: InstanceType, ...args: unknown[]) { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "" + const pid = (this as unknown as { processID: number }).processID + if (!pids.has(pid) || !/^COMMIT\s*;?$/i.test(text.trim())) return Reflect.apply(originalQuery, this, args) + // The real same-driver observation runs inside this transaction and + // precedes its genuine COMMIT. Neither result is fabricated or replaced. + return (async () => { + const observation = await Reflect.apply(originalQuery, this, ["SELECT pg_catalog.current_setting('synchronous_commit') AS setting,pg_catalog.pg_backend_pid() AS pid"]) + assert.equal(Number(observation.rows[0].pid), pid) + assert.equal(observation.rows[0].setting, "on", "journal COMMIT must override the inherited off SESSION default") + commitSettings.push(observation.rows[0].setting) + return Reflect.apply(originalQuery, this, args) + })() + } as typeof Client.prototype.query + patched = true + const prefix = "durability-" + randomUUID(), bindings = [ + fixtureBinding(prefix + ":A", prefix + ":B", { controlGeneration }), + fixtureBinding(prefix + ":C", prefix + ":D", { controlGeneration }), + fixtureBinding(prefix + ":E", prefix + ":F", { controlGeneration }), + ] + const noSend = new Set() + const journalFor = (expected: BandwidthBinding) => { + let active: { connection: ProbeStorageConnection; scope: BandwidthScope } | undefined + return createBandwidthAdmissionSqlJournal({ now: Date.now, + connect: async current => { + assert(!active); const connection = await connect(current); held.add(connection); active = { connection, scope: current } + return { query: (text, values, options) => connection.query(text, values, options), release(unsafe) { active = undefined; held.delete(connection); connection.release(unsafe) } } + }, + authorize: async (binding, current) => { assert(Date.now() < current.deadlineAt); current.signal?.throwIfAborted(); assert.deepEqual(binding, expected); assert.equal(binding.controlGeneration, controlGeneration) }, + verifyEvidence: async (transition, binding, evidence) => { + assert.deepEqual(binding, expected) + if (transition === "complete") assert.deepEqual(evidence, { terminalProof: completionProof(binding) }) + else if (transition === "release-before-dispatch") { assert(noSend.has(binding.attemptId)); assert.equal((evidence as { proof?: string }).proof, "private-no-send") } + else assert.equal(evidence, undefined) + }, + eventProof: async event => { assert(active); return eventProof(event, active.connection, active.scope) }, + }) + } + const completed = journalFor(bindings[0]) + await completed.reserve(bindings[0], scope()); await completed.arm(bindings[0], scope()) + await completed.complete(bindings[0], { terminalProof: completionProof(bindings[0]) }, scope()) + for (const [index, from] of [[1, "reserved"], [2, "armed"]] as const) { + const binding = bindings[index], journal = journalFor(binding); noSend.add(binding.attemptId) + await journal.reserve(binding, scope()); if (from === "armed") await journal.arm(binding, scope()) + await journal.releaseBeforeDispatch(binding, { from, proof: "private-no-send" }, scope()) + } + assert.equal(commitSettings.length, 8) + const ids = bindings.map(binding => binding.attemptId) + const attempts = (await owner.query("SELECT attempt_id::text,state,resolution,terminal_proof FROM mmapp_control.bandwidth_attempts WHERE attempt_id=ANY($1::uuid[])", [ids])).rows + assert.equal(attempts.length, 3) + for (const binding of bindings) { + const row = attempts.find(attempt => attempt.attempt_id === binding.attemptId); assert(row); assert.equal(row.state, "released") + assert.equal(row.resolution, binding === bindings[0] ? "remote_completion" : "proven_not_dispatched") + assert.equal(row.terminal_proof, binding === bindings[0] ? completionProof(binding) : null) + } + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=ANY($1::uuid[])", [ids])).rows[0].n), 0) + const events = (await owner.query("SELECT ack_id::text,attempt_id::text,transition,old_state,new_state,binding_proof,control_generation,evidence_proof FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=ANY($1::uuid[])", [ids])).rows + assert.equal(events.length, 8) + const after = await acquireFour(), afterPids = new Set() + for (const lease of after) { + const row = (await lease.query("SELECT pg_catalog.pg_backend_pid() AS pid,pg_catalog.current_setting('synchronous_commit') AS setting")).rows[0] + afterPids.add(Number(row.pid)); assert.equal(row.setting, "off", "SET LOCAL must not change the reusable SESSION default") + } + assert.deepEqual(afterPids, pids) + for (const event of events) { + const material = { ackId: event.ack_id, attemptId: event.attempt_id, transition: event.transition, oldState: event.old_state, newState: event.new_state, + bindingProof: event.binding_proof, controlGeneration: event.control_generation, terminalProof: event.transition === "complete" ? completionProof(bindings[0]) : null } + assert.equal(event.evidence_proof, await eventProof(material, after[0], scope())) + } + for (const lease of after) { await lease.query("SET SESSION synchronous_commit=on"); release(lease) } + allDefaultsRestored = true + return { checks: 5, physicalRuntimeSessions: pids.size, actualCommitObservations: commitSettings.length, allCommitSettings: "on", subsequentSessionDefaults: "off", persistedAttempts: attempts.length, persistedEvents: events.length, remainingOccupancy: 0, syntheticPrivateEvidence: true, realNativeCalls: 0 } + } finally { + if (patched) Client.prototype.query = originalQuery + // Also repair the test's SESSION defaults when an assertion fails. Acquire + // the entire pool, not four sequential checkouts of the same backend. + if (!allDefaultsRestored) { + for (const lease of [...held]) { try { await lease.query("SET SESSION synchronous_commit=on") } catch { /* Retired leases are destroyed by their source. */ } finally { held.delete(lease); lease.release(true) } } + try { + const leases = await acquireFour() + for (const lease of leases) { try { await lease.query("SET SESSION synchronous_commit=on") } finally { release(lease) } } + } finally { for (const lease of [...held]) { held.delete(lease); lease.release(true) } } + } + } +} diff --git a/backend/src/services/bandwidth-managed-peer-source.test.ts b/backend/src/services/bandwidth-managed-peer-source.test.ts new file mode 100644 index 00000000..6728d679 --- /dev/null +++ b/backend/src/services/bandwidth-managed-peer-source.test.ts @@ -0,0 +1,134 @@ +/** Guard and application transport controlled; no PG/Q authority claim here. + * Genuine guard + real SQL compositions are covered by the owned PG profile. */ +import assert from "node:assert/strict" +import { EventEmitter } from "node:events" +import { randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import { mock, test } from "node:test" + +let reads = 0, metas = 0, checkouts = 0, projections = 0, predicates = 0, revoked = false +let beforeRead: (() => void) | undefined, queryGate: Promise | undefined +let current = { host: "127.0.0.1", port: 8443, useSsl: true, verifySsl: true, username: "private-user", password: "private-original-password", routingEndpointEpoch: randomUUID() } +let enabled = true +const clients: Array = [] +const guard = Object.freeze({ async verifyRead() { reads++; beforeRead?.(); if (revoked) throw Error("revoked") }, + async verifyMetadata() { metas++; if (revoked) throw Error("revoked") } }) +const owner = guard +mock.module("./bandwidth-operator-writer-window.js", { namedExports: { + assertBandwidthOperatorPeerSourceGuard(value: unknown) { assert.equal(value, owner) }, +} }) +mock.module("../db/index.js", { namedExports: { pool: { async connect() { + checkouts++ + const client = Object.assign(new EventEmitter(), { destroyed: false, releases: 0, + async query(input: { text: string; values: unknown[] }) { + await queryGate + const { text, values } = input + if (text.startsWith("/* bandwidth-managed:guarded-credentials */")) { + projections++; assert(text.includes("enabled IS TRUE AND routing_endpoint_epoch=$2::uuid")) + assert.deepEqual(values, [17, initialEpoch]) + return { command: "SELECT", rowCount: enabled && current.routingEndpointEpoch === values[1] ? 1 : 0, + rows: enabled && current.routingEndpointEpoch === values[1] ? [{ endpoint: { ...current } }] : [] } + } + assert(text.startsWith("/* bandwidth-managed:metadata-current */")); predicates++ + const match = enabled && values[0] === 17 && [current.host, current.port, current.useSsl, current.verifySsl, + current.username, current.password, current.routingEndpointEpoch].every((value, i) => value === values[i + 1]) + return { command: "SELECT", rowCount: 1, rows: [{ metadata_current: match }] } + }, + release(destroy: boolean) { this.releases++; assert.equal(this.releases, 1); this.destroyed = destroy }, + }) + clients.push(client); return client +} } } }) +const { createConfiguredManagedBandwidthPeerSource } = await import("./bandwidth-managed-peer-source.js") +const initialEpoch = current.routingEndpointEpoch +const config = { serverId: 17, sourceProof: "c".repeat(64), authorityGeneration: "controlled-unit-generation", routingEndpointEpoch: initialEpoch } +const peer = { kind: "server" as const, serverId: 17 } +const scope = (signal?: AbortSignal, duration = 1000) => ({ deadlineAt: Date.now() + duration, signal }) +const make = () => createConfiguredManagedBandwidthPeerSource(config, guard as never) +const reset = () => { reads = metas = checkouts = projections = predicates = 0; revoked = false; beforeRead = undefined; queryGate = undefined; + enabled = true; current = { host: "127.0.0.1", port: 8443, useSsl: true, verifySsl: true, username: "private-user", password: "private-original-password", routingEndpointEpoch: initialEpoch }; clients.length = 0 } +const turn = () => new Promise(done => setImmediate(done)) + +test("managed operator source fixed transport and private binding", async t => { + await t.test("inert constructor, guard before projection, metadata uses no read guard or credential result", async () => { + reset(); const source = make(); assert.equal(checkouts + reads + metas, 0) + try { + const snapshot = await source.read(peer, scope()) + assert(Object.isFrozen(snapshot) && Object.isFrozen(snapshot.endpoint)); assert.equal(snapshot.endpoint.password, current.password) + assert.equal(reads, 2); assert.equal(projections, 1); assert.equal(predicates, 1) + const baseline = { reads, projections } + await source.assertMetadataCurrent(snapshot, scope()); await source.assertMetadataCurrent(structuredClone(snapshot), scope()) + assert.equal(reads, baseline.reads); assert.equal(projections, baseline.projections); assert.equal(predicates, 3) + assert.equal(clients.filter(client => client.destroyed).length, 1) + } finally { source.close() } + }) + await t.test("metadata requires previous read; no structural guard or foreign peer fallback", async () => { + reset(); assert.throws(() => createConfiguredManagedBandwidthPeerSource(config, { ...guard } as never)) + const source = make() + await assert.rejects(source.assertMetadataCurrent({} as never, scope())); assert.equal(checkouts, 0) + await assert.rejects(source.read({ kind: "external", enrollmentId: randomUUID() } as never, scope())); assert.equal(checkouts, 0) + source.close() + }) + await t.test("current grant failure precedes pool/secret dispatch; changed epoch gates SQL payload", async () => { + for (const fault of ["original", "epoch", "disabled"] as const) { + reset(); const source = make() + if (fault === "original") revoked = true + else beforeRead = () => { if (fault === "epoch") current.routingEndpointEpoch = randomUUID(); else enabled = false } + await assert.rejects(source.read(peer, scope())) + if (fault === "original") assert.equal(checkouts, 0) + assert.equal(predicates, 0); await assert.rejects(source.read(peer, scope())); source.close() + } + }) + await t.test("all private snapshot fields/current endpoint changes refuse metadata without another projection", async () => { + for (const fault of ["password", "epoch", "enabled", "expected"] as const) { + reset(); const source = make(), original = await source.read(peer, scope()), before = { reads, projections } + if (fault === "password") current.password = "changed" + if (fault === "epoch") current.routingEndpointEpoch = randomUUID() + if (fault === "enabled") enabled = false + const expected = fault === "expected" ? { ...original, endpoint: { ...original.endpoint, username: "forged" } } : original + await assert.rejects(source.assertMetadataCurrent(expected, scope())); assert.equal(reads, before.reads); assert.equal(projections, before.projections) + await assert.rejects(source.read(peer, scope())); source.close() + } + }) + await t.test("scope/expected getters and proxies have zero callbacks", async () => { + reset(); let touched = 0 + const source = make(), original = await source.read(peer, scope()) + const hostile = { ...original, get endpoint() { touched++; return original.endpoint } } + await assert.rejects(source.assertMetadataCurrent(hostile, scope())); assert.equal(touched, 0); source.close() + const second = make() + await assert.rejects(second.read(peer, new Proxy(scope(), { getPrototypeOf() { touched++; return Object.prototype } }))) + assert.equal(touched, 0); second.close() + }) + await t.test("abort/close/busy pending driver retire source and suppress late results", async () => { + for (const fault of ["abort", "close", "busy"] as const) { + reset(); const source = make(), controller = new AbortController() + let finish!: () => void; queryGate = new Promise(done => { finish = done }) + const pending = source.read(peer, scope(controller.signal)); void pending.catch(() => {}) + while (!clients.length) await turn() + if (fault === "abort") controller.abort() + if (fault === "close") source.close() + if (fault === "busy") await assert.rejects(source.read(peer, scope())) + await assert.rejects(pending); assert(clients[0].destroyed) + finish(); await turn(); await turn() + assert.equal(predicates, 0); await assert.rejects(source.read(peer, scope())); source.close() + } + }) + await t.test("source close revokes retained metadata and pre-abort cannot borrow", async () => { + reset(); const source = make(), original = await source.read(peer, scope()) + source.close(); await assert.rejects(source.assertMetadataCurrent(original, scope())); await assert.rejects(source.read(peer, scope())) + const second = make(), expired = new AbortController(); expired.abort() + await assert.rejects(second.read(peer, scope(expired.signal))); assert.equal(reads, 2); second.close() + }) + await t.test("later scope cannot renew the first wall or monotonic horizon", async () => { + for (const clock of ["wall", "mono"] as const) { + reset(); const source = make(), original = await source.read(peer, scope(undefined, 1000)) + const before = { reads, projections, predicates } + const realWall = Date.now.bind(Date), realMono = performance.now.bind(performance) + const spy = clock === "wall" ? mock.method(Date, "now", () => realWall() + 2000) + : mock.method(performance, "now", () => realMono() + 2000) + try { + await assert.rejects(source.assertMetadataCurrent(original, scope(undefined, 5000))) + assert.deepEqual({ reads, projections, predicates }, before) + } finally { spy.mock.restore(); source.close() } + } + }) +}) diff --git a/backend/src/services/bandwidth-managed-peer-source.ts b/backend/src/services/bandwidth-managed-peer-source.ts new file mode 100644 index 00000000..9911eb3f --- /dev/null +++ b/backend/src/services/bandwidth-managed-peer-source.ts @@ -0,0 +1,137 @@ +/** Fixed operator-owned managed source. Not a runtime default or model API. + * The genuine held-window guard controls every management secret projection. */ +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual, types } from "node:util" +import { z } from "zod" +import { + assertBandwidthOperatorPeerSourceGuard, type BandwidthOperatorPeerSourceGuard, + type BandwidthOperatorPeerSourceBinding, +} from "./bandwidth-operator-writer-window.js" +import type { BandwidthPeerSourceSnapshot, BandwidthPeerTrustedSource } from "./bandwidth-peer-observation.js" +import type { BandwidthNodeObservationScope } from "./bandwidth-node-observation.js" +import { createManagedBandwidthPeerMetadataChecker } from "./bandwidth-peer-metadata-source.js" +import { createProbeStorageConnectionConnector } from "./server-probe-storage-connection.js" + +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const configSchema = z.object({ serverId: z.number().int().positive().max(Number.MAX_SAFE_INTEGER), + sourceProof: hash, authorityGeneration: z.string().min(1).max(200).refine(value => Buffer.byteLength(value) <= 200 && value.trim() === value && !/[\u0000-\u001f\u007f]/.test(value)), routingEndpointEpoch: z.string().uuid() }).strict() +const endpointSchema = z.object({ host: z.string().min(1).max(1000).refine(value => Buffer.byteLength(value) <= 1000 && !/[\s/\\\u0000-\u001f\u007f]/.test(value)), + port: z.number().int().min(1).max(65535), useSsl: z.boolean(), verifySsl: z.boolean(), + username: z.string().refine(value => Buffer.byteLength(value) <= 1000), + password: z.string().refine(value => Buffer.byteLength(value) <= 65536), routingEndpointEpoch: z.string().uuid() }).strict() +const SQL = `/* bandwidth-managed:guarded-credentials */ WITH chosen AS MATERIALIZED ( + SELECT pg_catalog.jsonb_build_object('host',host,'port',port,'useSsl',use_ssl,'verifySsl',verify_ssl, + 'username',username,'password',password,'routingEndpointEpoch',routing_endpoint_epoch) AS endpoint + FROM public.servers WHERE id=$1::bigint AND enabled IS TRUE AND routing_endpoint_epoch=$2::uuid LIMIT 2) + SELECT CASE WHEN pg_catalog.octet_length(endpoint::text)<=70000 THEN endpoint END AS endpoint FROM chosen` + +export class BandwidthManagedPeerSourceRefused extends Error { + readonly code = "BANDWIDTH_MANAGED_PEER_SOURCE_REFUSED" + constructor() { super("Managed operator peer source unavailable") } +} +const refuse = (): never => { throw new BandwidthManagedPeerSourceRefused() } +function data(raw: unknown, names: readonly string[]): Record { + if (!raw || typeof raw !== "object" || types.isProxy(raw) || Object.getPrototypeOf(raw) !== Object.prototype) return refuse() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== names.length || names.some(name => !fields[name]) + || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) return refuse() + return Object.fromEntries(names.map(name => [name, fields[name].value])) +} +function captureSnapshot(raw: unknown): Readonly { + const value = data(raw, ["peerRef", "enabled", "sourceProof", "authorityGeneration", "endpoint"]) + const peer = data(value.peerRef, ["kind", "serverId"]) + if (peer.kind !== "server" || !Number.isSafeInteger(peer.serverId) || Number(peer.serverId) < 1 || value.enabled !== true + || !hash.safeParse(value.sourceProof).success || typeof value.authorityGeneration !== "string") refuse() + const endpoint = endpointSchema.parse(data(value.endpoint, ["host", "port", "useSsl", "verifySsl", "username", "password", "routingEndpointEpoch"])) + return Object.freeze({ peerRef: Object.freeze({ kind: "server" as const, serverId: Number(peer.serverId) }), enabled: true, + sourceProof: String(value.sourceProof), authorityGeneration: value.authorityGeneration as string, endpoint: Object.freeze(endpoint) }) +} + +export function createConfiguredManagedBandwidthPeerSource(raw: z.infer, guard: BandwidthOperatorPeerSourceGuard) { + assertBandwidthOperatorPeerSourceGuard(guard) + const config = Object.freeze(configSchema.parse(data(raw, ["serverId", "sourceProof", "authorityGeneration", "routingEndpointEpoch"]))) + const verifyRead = guard.verifyRead.bind(guard), verifyMetadata = guard.verifyMetadata.bind(guard) + const peerRef = Object.freeze({ kind: "server" as const, serverId: config.serverId }) + const binding: Readonly = Object.freeze({ peerRef, + sourceProof: config.sourceProof, authorityGeneration: config.authorityGeneration, routingEndpointEpoch: config.routingEndpointEpoch }) + const checker = createManagedBandwidthPeerMetadataChecker() + const connector = createProbeStorageConnectionConnector(async () => (await import("../db/index.js")).pool.connect()) + let closed = false, busy = false, end: number | undefined, wallStart: number | undefined, firstDeadline: number | undefined + let original: Readonly | undefined + const pending = new Set<() => void>() + const close = () => { if (closed) return; closed = true; original = undefined; for (const stop of [...pending]) stop() } + async function operation(input: BandwidthNodeObservationScope, work: (scope: Readonly, live: () => void) => Promise): Promise { + const wall = Date.now(), mono = performance.now() + let stop: (() => void) | undefined, timer: ReturnType | undefined, signal: AbortSignal | undefined + try { + if (closed || busy) { close(); return refuse() } + busy = true + if (!input || typeof input !== "object" || types.isProxy(input) || Object.getPrototypeOf(input) !== Object.prototype) refuse() + const fields = data(input, Object.hasOwn(input, "signal") ? ["deadlineAt", "signal"] : ["deadlineAt"]) + if (!Number.isSafeInteger(fields.deadlineAt) || fields.signal !== undefined && (types.isProxy(fields.signal) || !(fields.signal instanceof AbortSignal))) refuse() + signal = fields.signal as AbortSignal | undefined + const deadlineAt = Math.min(Number(fields.deadlineAt), wall + 5000, firstDeadline ?? Infinity) + firstDeadline ??= deadlineAt + end ??= mono + Math.max(0, deadlineAt - wall); wallStart ??= wall + const boundedEnd = Math.min(end, mono + Math.max(0, deadlineAt - wall)) + const controller = new AbortController(), aborted = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "aborted")!.get! + let rejectStopped!: (error: BandwidthManagedPeerSourceRefused) => void, active = true + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }); void stopped.catch(() => {}) + stop = () => { if (!active) return; active = false; controller.abort(); rejectStopped(new BandwidthManagedPeerSourceRefused()) } + const abort = stop + const live = () => { const now = Date.now(); if (closed || !active || now < wall || now < wallStart! || now >= deadlineAt || performance.now() >= boundedEnd + || signal && Reflect.apply(aborted, signal, [])) refuse() } + live(); pending.add(stop) + timer = setTimeout(() => { close(); abort() }, Math.max(1, Math.ceil(boundedEnd - performance.now()))) + if (signal) Reflect.apply(EventTarget.prototype.addEventListener, signal, ["abort", stop, { once: true }]) + const scope = Object.freeze({ deadlineAt, signal: controller.signal }) + try { const result = await Promise.race([stopped, Promise.resolve().then(() => { live(); return work(scope, live) })]); live(); return result } + finally { active = false; controller.abort() } + } catch { close(); throw new BandwidthManagedPeerSourceRefused() } + finally { + clearTimeout(timer) + if (stop) { pending.delete(stop); if (signal) Reflect.apply(EventTarget.prototype.removeEventListener, signal, ["abort", stop]) } + busy = false + } + } + const source = Object.freeze({ + async read(rawPeer: Parameters[0], scope: BandwidthNodeObservationScope): Promise> { + return operation(scope, async (current, live) => { + const peer = data(rawPeer, ["kind", "serverId"]) + if (peer.kind !== "server" || peer.serverId !== config.serverId) refuse() + // Authenticate metadata before even borrowing the application lease. + await verifyMetadata(binding, current); live() + const connection = await connector(current) + let snapshot: Readonly + try { + live() + await verifyRead(binding, current); live() + const result = await connection.query(SQL, [config.serverId, config.routingEndpointEpoch], { signal: current.signal, timeoutMs: Math.max(1, Math.ceil(Math.min(5000, current.deadlineAt - Date.now()))) }) + live() + if (!("command" in result) || result.command !== "SELECT" || result.rows.length !== 1) refuse() + const row = data(result.rows[0], ["endpoint"]) + snapshot = captureSnapshot({ peerRef, enabled: true, sourceProof: config.sourceProof, authorityGeneration: config.authorityGeneration, + endpoint: data(row.endpoint, ["host", "port", "useSsl", "verifySsl", "username", "password", "routingEndpointEpoch"]) }) + if (snapshot.endpoint.routingEndpointEpoch !== config.routingEndpointEpoch || original && !isDeepStrictEqual(original, snapshot)) refuse() + } finally { connection.release(true) } + live() + await checker.assertMetadataCurrent(snapshot!, current); live() + await verifyRead(binding, current); live() + original ??= snapshot! + return original + }) + }, + async assertMetadataCurrent(expected: Readonly, scope: BandwidthNodeObservationScope): Promise { + if (!original) return refuse() + return operation(scope, async (current, live) => { + const captured = captureSnapshot(expected) + if (!original || !isDeepStrictEqual(captured, original)) refuse() + await verifyMetadata(binding, current); live() + await checker.assertMetadataCurrent(captured, current); live() + await verifyMetadata(binding, current); live() + }) + }, + close, + } satisfies BandwidthPeerTrustedSource & { close(): void }) + return source +} diff --git a/backend/src/services/bandwidth-native-dispatch-composite.test.ts b/backend/src/services/bandwidth-native-dispatch-composite.test.ts new file mode 100644 index 00000000..8b01f1e7 --- /dev/null +++ b/backend/src/services/bandwidth-native-dispatch-composite.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import { test } from "node:test" +import { createBandwidthNativeDispatchCompositeGate } from "./bandwidth-native-dispatch-composite.js" +import type { BandwidthNativeDispatchGate } from "./bandwidth-native-dispatch.js" + +const scope = (signal = new AbortController().signal, ms = 2000) => ({ deadlineAt: Date.now() + ms, signal }) +function fixture() { + const events: string[] = [] + const held = new Set() + const gate = (name: string): BandwidthNativeDispatchGate => ({ async acquire() { + events.push("open:" + name); held.add(name) + return { async verify() { assert(held.has(name)); events.push("verify:" + name) }, close() { events.push("close:" + name); held.delete(name) } } + } }) + return { events, held, gate } +} +test("fixed mapping-first order, captured gates, repeated same-scope checks and reverse physical close", async () => { + const f = fixture(), mapping = f.gate("mapping"), registry = f.gate("registry") + const composite = createBandwidthNativeDispatchCompositeGate([mapping, registry]) + mapping.acquire = async () => { throw Error("mutable callback replacement") } + const current = scope(), lease = await composite.acquire(current) + assert.deepEqual([...f.held], ["mapping", "registry"]) + await lease.verify(current); await lease.verify({ ...current }) + lease.close(); lease.close() + assert.deepEqual(f.events, ["open:mapping", "open:registry", "verify:mapping", "verify:registry", "verify:mapping", "verify:registry", "close:registry", "close:mapping"]) + await assert.rejects(lease.verify(current)); await assert.rejects(composite.acquire(current)); assert.equal(f.held.size, 0) +}) +test("second acquisition failure releases the already held global barrier", async () => { + const f = fixture() + const composite = createBandwidthNativeDispatchCompositeGate([f.gate("mapping"), { async acquire() { throw Error("registry unavailable") } }]) + await assert.rejects(composite.acquire(scope()), /registry unavailable/) + assert.deepEqual(f.events, ["open:mapping", "close:mapping"]); assert.equal(f.held.size, 0) +}) +test("abort while acquiring registry immediately releases mapping and destroys a later returned lease", async () => { + const f = fixture(), controller = new AbortController() + let resume!: () => void, entered!: () => void + const waiting = new Promise(resolve => { entered = resolve }) + const second: BandwidthNativeDispatchGate = { async acquire(current) { entered(); await new Promise(resolve => { resume = resolve }); return f.gate("registry").acquire(current) } } + const composite = createBandwidthNativeDispatchCompositeGate([f.gate("mapping"), second]) + const pending = composite.acquire(scope(controller.signal)); await waiting; controller.abort() + await assert.rejects(pending); assert.equal(f.held.size, 0) + resume(); await delay(10) + assert.deepEqual(f.events, ["open:mapping", "close:mapping", "open:registry", "close:registry"]); assert.equal(f.held.size, 0) +}) +test("revoked mapping prevents the next registry query and retires both physical leases", async () => { + const f = fixture(), mapping: BandwidthNativeDispatchGate = { async acquire() { + f.held.add("mapping"); return { async verify() { throw Error("mapping revoked") }, close() { f.events.push("close:mapping"); f.held.delete("mapping") } } + } } + const current = scope(), lease = await createBandwidthNativeDispatchCompositeGate([mapping, f.gate("registry")]).acquire(current) + await assert.rejects(lease.verify(current), /mapping revoked/) + assert(!f.events.includes("verify:registry")); assert.equal(f.held.size, 0) +}) +test("a stalled verification aborts promptly and its late continuation cannot check the next gate", async () => { + const f = fixture(), controller = new AbortController(); let resume!: () => void, entered!: () => void + const waiting = new Promise(resolve => { entered = resolve }) + const mapping: BandwidthNativeDispatchGate = { async acquire() { return { + async verify() { entered(); await new Promise(resolve => { resume = resolve }) }, close() { f.events.push("close:mapping") }, + } } } + const current = scope(controller.signal), lease = await createBandwidthNativeDispatchCompositeGate([mapping, f.gate("registry")]).acquire(current) + const pending = lease.verify(current); await waiting; controller.abort(); await assert.rejects(pending) + resume(); await delay(10); assert(!f.events.includes("verify:registry")); assert.equal(f.held.size, 0) +}) +test("one failing close does not skip the other barrier and can be retried", async () => { + const f = fixture(); let closes = 0 + const registry: BandwidthNativeDispatchGate = { async acquire() { return { async verify() {}, close() { closes++; if (closes === 1) throw Error("physical close failed") } } } } + const lease = await createBandwidthNativeDispatchCompositeGate([f.gate("mapping"), registry]).acquire(scope()) + assert.throws(() => lease.close(), /physical close failed/); assert.equal(f.held.size, 0) + lease.close(); assert.equal(closes, 2) +}) +test("pre-abort and structurally changed scopes refuse without another gate query", async () => { + const f = fixture(), mapping = f.gate("mapping"), registry = f.gate("registry"), controller = new AbortController(); controller.abort() + await assert.rejects(createBandwidthNativeDispatchCompositeGate([mapping, registry]).acquire(scope(controller.signal))); assert.equal(f.events.length, 0) + assert.throws(() => createBandwidthNativeDispatchCompositeGate([mapping, mapping])) + const current = scope(), lease = await createBandwidthNativeDispatchCompositeGate([mapping, registry]).acquire(current) + await assert.rejects(lease.verify({ ...current, deadlineAt: current.deadlineAt + 1 })) + assert.equal(f.events.filter(value => value.startsWith("verify:")).length, 0); assert.equal(f.held.size, 0) +}) diff --git a/backend/src/services/bandwidth-native-dispatch-composite.ts b/backend/src/services/bandwidth-native-dispatch-composite.ts new file mode 100644 index 00000000..3ac52ddc --- /dev/null +++ b/backend/src/services/bandwidth-native-dispatch-composite.ts @@ -0,0 +1,85 @@ +/** Trusted assembly only. Acquire the global mapping barrier BEFORE the + * registry/owner barrier; retain both through the existing private native + * ClientRequest.finish handoff. This is transport composition, not a mapping + * issuer, RouterOS route proof, or all-writer activation. */ +import { performance } from "node:perf_hooks" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { BandwidthNativeDispatchGate, BandwidthNativeDispatchLease } from "./bandwidth-native-dispatch.js" + +const refused = () => new Error("Bandwidth composite native fence refused") +function captureScope(raw: Readonly): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype) throw refused() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== Object.keys(fields).length || Object.keys(fields).some(key => !["deadlineAt", "signal"].includes(key)) + || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) throw refused() + const deadlineAt = fields.deadlineAt?.value as number, signal = fields.signal?.value as AbortSignal | undefined + if (!Number.isSafeInteger(deadlineAt) || signal !== undefined && !(signal instanceof AbortSignal)) throw refused() + return Object.freeze({ deadlineAt, signal }) +} + +export function createBandwidthNativeDispatchCompositeGate(gates: readonly Readonly[]): Readonly { + if (!Array.isArray(gates) || gates.length < 2 || gates.length > 4 || new Set(gates).size !== gates.length) throw refused() + const acquire = Object.freeze(gates.map(gate => { + if (!gate || typeof gate.acquire !== "function") throw refused() + return gate.acquire.bind(gate) + })) + let used = false + return Object.freeze({ async acquire(input: Readonly): Promise { + if (used) throw refused(); used = true + const scope = captureScope(input), mono = performance.now(), wall = Date.now() + const end = mono + Math.max(0, Math.min(5000, scope.deadlineAt - wall)) + const leases: Array> = [] + let active = true, verifying = false + let rejectStopped!: (error: Error) => void + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + void stopped.catch(() => {}) + const live = () => { if (!active || scope.signal?.aborted || Date.now() >= scope.deadlineAt || performance.now() >= end) throw refused() } + // Retry a failed child's physical close when the private kernel retries + // close(); successfully retired children are never returned to healthy use. + const close = () => { + active = false; clearTimeout(timer); scope.signal?.removeEventListener("abort", stop) + let failure: unknown + for (let index = leases.length - 1; index >= 0; index--) { + try { leases[index].close(); leases.splice(index, 1) } catch (error) { failure ??= error } + } + if (failure) throw failure + } + const stop = () => { try { close() } catch { /* Kernel close retries failed physical destruction. */ } rejectStopped(refused()) } + const captureLease = (value: Readonly) => { + if (!value || typeof value.verify !== "function" || typeof value.close !== "function") { + try { value?.close?.() } catch { /* Invalid late lease cannot become authority. */ } + throw refused() + } + return Object.freeze({ verify: value.verify.bind(value), close: value.close.bind(value) }) + } + const timer = setTimeout(stop, Math.max(1, end - performance.now())) + scope.signal?.addEventListener("abort", stop, { once: true }) + try { + live() + for (const open of acquire) { + live() + const pending = Promise.resolve().then(() => { live(); return open(scope) }).then(value => { + // Track before checking time: even a boundary expiry must retire the + // just-returned physical lease. A stalled acquire can resolve late. + const child = captureLease(value); leases.push(child) + if (!active) { try { close() } catch { try { close() } catch { /* Source must also bound its own physical lease. */ } } } + return child + }) + await Promise.race([pending, stopped]); live() + } + live() + return Object.freeze({ + async verify(current: Readonly) { + try { + const next = captureScope(current) + if (verifying || next.deadlineAt !== scope.deadlineAt || next.signal !== scope.signal) throw refused() + live(); verifying = true + for (const lease of leases) { live(); await Promise.race([lease.verify(scope), stopped]); live() } + } catch (error) { stop(); throw error } + finally { verifying = false } + }, + close, + }) + } catch (error) { try { close() } catch { /* Every owned child was attempted. */ } throw error } + } }) +} diff --git a/backend/src/services/bandwidth-native-dispatch-source.sql-helper.ts b/backend/src/services/bandwidth-native-dispatch-source.sql-helper.ts new file mode 100644 index 00000000..d59f2320 --- /dev/null +++ b/backend/src/services/bandwidth-native-dispatch-source.sql-helper.ts @@ -0,0 +1,59 @@ +/** Owned PostgreSQL acceptance for the production-source producer, separate + * from transport fixtures. Only the outer runner provisions a disposable DB. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { Pool } from "pg" +import type { createConfiguredBandwidthControlSource } from "./bandwidth-control-source.js" +import { createBandwidthAdmissionSqlJournal } from "./bandwidth-admission-storage.js" +import { createBandwidthNativeDispatchSqlGate } from "./bandwidth-native-dispatch-source.js" +import { assertOwnedBandwidthSql, fixtureBinding } from "./bandwidth-admission-process.sql-helper.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +export async function acceptOwnedBandwidthNativeDispatchSqlSource(core: ReturnType, controlGeneration: string, owner: Pool) { + assertOwnedBandwidthSql() + const scope = (): BandwidthScope => ({ deadlineAt: Date.now() + 10000 }) + const binding = fixtureBinding("native-sql-" + randomUUID(), "native-sql-" + randomUUID(), { controlGeneration }) + let journalConnection: ProbeStorageConnection | undefined, journalScope: BandwidthScope | undefined + const journal = createBandwidthAdmissionSqlJournal({ now: Date.now, + connect: async current => { journalScope = current; journalConnection = await core.connect(current); return journalConnection }, + authorize: async expected => { assert.deepEqual(expected, binding) }, verifyEvidence: async () => {}, + eventProof: async event => { assert(journalConnection && journalScope); return core.eventProofOnConnection(event, journalConnection, journalScope) }, + }) + await journal.reserve(binding, scope()); await journal.arm(binding, scope()) + const before = await owner.query("SELECT binding,state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [binding.attemptId]) + const make = () => createBandwidthNativeDispatchSqlGate(core, binding, async fence => { + assert.equal((await fence.sourceIdentity()).controlGeneration, controlGeneration) + }) + const ownedScope = { deadlineAt: Date.now() + 5000 } + const gate = make(), lease = await gate.acquire(ownedScope) + try { + await lease.verify(ownedScope) + assert.equal((await owner.query("SELECT pg_catalog.pg_try_advisory_xact_lock(8723116) AS locked")).rows[0].locked, false) + for (const node of [binding.source.canonicalNodeId, binding.destination.canonicalNodeId]) { + assert.equal((await owner.query("SELECT pg_catalog.pg_try_advisory_xact_lock(pg_catalog.hashtextextended($1,764301)) AS locked", ["bandwidth-node:" + node])).rows[0].locked, false) + } + await assert.rejects(owner.query("SELECT attempt_id FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1 FOR UPDATE NOWAIT", [binding.attemptId]), error => (error as { code?: string }).code === "55P03") + const second = make() + await assert.rejects(second.acquire(scope())) + await lease.verify(ownedScope) + assert.deepEqual((await owner.query("SELECT binding,state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [binding.attemptId])).rows, before.rows) + } finally { lease.close() } + await assert.rejects(lease.verify(scope())); await assert.rejects(gate.acquire(scope())) + // Destruction retires all locks physically without COMMIT or ledger writes. + const rival=await owner.connect() + try{ + await rival.query("BEGIN") + await rival.query("SET LOCAL lock_timeout='2000ms'") + // Destroying the client closes the socket; PostgreSQL observes that close + // asynchronously. Wait on the real lock, rather than assume immediate EOF. + await rival.query("SELECT pg_catalog.pg_advisory_xact_lock(8723116)") + await rival.query("SELECT attempt_id FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1 FOR UPDATE NOWAIT", [binding.attemptId]) + }finally{try{await rival.query("ROLLBACK")}finally{rival.release(true)}} + await journal.quarantine(binding, scope()) + const deniedScope = { deadlineAt: Date.now() + 5000 }, quarantined = make(), denied = await quarantined.acquire(deniedScope) + try { await assert.rejects(denied.verify(deniedScope)) } finally { denied.close() } + assert.equal((await owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [binding.attemptId])).rows[0].state, "unknown_busy") + assert.equal(Number((await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1", [binding.attemptId])).rows[0].n), 2) + return { checks: ["actual authenticated same-lease armed row, two owners and reserve/arm event MACs; registry/canonical/owner locks exclude a rival, physical close releases locks without ledger mutation; unknown occupancy cannot send or clear"], actualNativePosts: 0, peerGuardSynthetic: true } +} diff --git a/backend/src/services/bandwidth-native-dispatch-source.test.ts b/backend/src/services/bandwidth-native-dispatch-source.test.ts new file mode 100644 index 00000000..774332fc --- /dev/null +++ b/backend/src/services/bandwidth-native-dispatch-source.test.ts @@ -0,0 +1,70 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { test } from "node:test" +import { createBandwidthNativeDispatchSqlGate } from "./bandwidth-native-dispatch-source.js" +import type { BandwidthBinding } from "./bandwidth-admission.js" +import type { BandwidthControlDispatchFence } from "./bandwidth-control-source.js" + +test("fixed native SQL producer uses one authenticated lease; no DML, replay or released continuation (fake SQL only)", async () => { + const binding: BandwidthBinding = { attemptId: randomUUID(), callerKind: "mcp-speed", operationId: "original", source: { canonicalNodeId: randomUUID(), endpointProof: "a".repeat(64) }, destination: { canonicalNodeId: randomUUID(), endpointProof: "b".repeat(64) }, + authorityProof: "c".repeat(64), authorityGeneration: "actor", controlGeneration: "source", expiresAt: Date.now() + 60000, durationSec: 3, requestProof: "d".repeat(64), commandProof: "e".repeat(64), bindingProof: "f".repeat(64) } + const mac = (value: unknown) => createHmac("sha256", "synthetic-unit-only").update(JSON.stringify(value)).digest("hex") + for (const corruption of ["none", "longer-original", "row", "owner", "extra-owner", "missing-event", "event-mac", "event-shape", "registry-lock", "owner-lock", "authority", "abort", "begin-rows-only", "smaller-scope", "concurrent-verify"]) { + let checkouts = 0, destroys = 0, checks = 0, macReads = 0, transaction = false, revoked = false, released = false + const sql: string[] = [], controller = new AbortController(), scope = { deadlineAt: Date.now() + (corruption === "longer-original" ? 10000 : 3000), signal: controller.signal } + const row = { attempt_id: binding.attemptId, binding, caller_kind: binding.callerKind, operation_id: binding.operationId, state: "armed", source_node: binding.source.canonicalNodeId, destination_node: binding.destination.canonicalNodeId, + binding_proof: binding.bindingProof, control_generation: binding.controlGeneration, resolution: null, terminal_proof: null } + const owners = [binding.source.canonicalNodeId, binding.destination.canonicalNodeId].sort().map(node_id => ({ node_id, attempt_id: binding.attemptId, binding_proof: binding.bindingProof, control_generation: binding.controlGeneration })) + const events = ["reserve", "arm"].map(transition => { + const ack_id = randomUUID(), old_state = transition === "reserve" ? null : "reserved", new_state = transition === "reserve" ? "reserved" : "armed" + return { ack_id, attempt_id: binding.attemptId, binding_proof: binding.bindingProof, control_generation: binding.controlGeneration, transition, old_state, new_state, + evidence_proof: mac({ ackId: ack_id, attemptId: binding.attemptId.toLowerCase(), transition, oldState: old_state, newState: new_state, bindingProof: binding.bindingProof, controlGeneration: binding.controlGeneration, terminalProof: null }) } + }) + let guard: ((fence: BandwidthControlDispatchFence) => Promise) | undefined + const fence = {} as BandwidthControlDispatchFence + const physical = { async query(text: string) { + assert(!released); sql.push(text) + assert(!/\b(?:INSERT|UPDATE|DELETE|COMMIT|ROLLBACK)\b/.test(text), "fixed producer never writes or commits") + await guard?.(fence) + let rows: Record[] = [] + if (text === "BEGIN") transaction = true + else if (text.includes("AS locked")) { assert(transaction); rows = [{ locked: !(text.includes("8723116") && corruption === "registry-lock" || text.includes("764301") && corruption === "owner-lock") }] } + else if(text.includes("/* bandwidth-native:armed-snapshot */"))rows=[{ + attempt:{...row,...(corruption==="row"?{state:"unknown_busy"}:{})}, + owners:corruption==="extra-owner"?[...owners,owners[0]]:corruption==="owner"?[{...owners[0],binding_proof:"0".repeat(64)},owners[1]]:owners, + events:corruption==="missing-event"?events.slice(0,1):events.map((event,index)=>({...event,...(index===0&&corruption==="event-mac"?{evidence_proof:"0".repeat(64)}:{}),...(index===0&&corruption==="event-shape"?{old_state:"armed"}:{})})), + }] + else if (text.includes("FROM mmapp_control.bandwidth_attempts")) rows = [{ ...row, ...(corruption === "row" ? { state: "unknown_busy" } : {}) }] + else if (text.includes("FROM mmapp_control.bandwidth_node_occupancy")) rows = corruption === "extra-owner" ? [...owners, owners[0]] : corruption === "owner" ? [{ ...owners[0], binding_proof: "0".repeat(64) }, owners[1]] : owners + else if (text.includes("FROM mmapp_control.bandwidth_attempt_events")) rows = corruption === "missing-event" ? events.slice(0, 1) : events.map((event, index) => ({ ...event, ...(index === 0 && corruption === "event-mac" ? { evidence_proof: "0".repeat(64) } : {}), ...(index === 0 && corruption === "event-shape" ? { old_state: "armed" } : {}) })) + await guard?.(fence) + return { rows, ...(corruption === "begin-rows-only" ? {} : { command: text === "BEGIN" ? "BEGIN" : "SELECT" }) } + }, release(unsafe?: boolean) { assert.equal(unsafe, true); assert(!released); released = true; destroys++ } } + const core = { + async connect(_scope: unknown, supplied?: (fence: BandwidthControlDispatchFence) => Promise) { checkouts++; assert.equal(checkouts, 1); guard = supplied; return physical }, + async eventProofOnConnection(value: unknown, connection: unknown) { assert.equal(connection, physical); macReads++; await physical.query("SELECT 1"); return mac(value) }, + } + const gate = createBandwidthNativeDispatchSqlGate(core as never, binding, async () => { assert(transaction); checks++; if (revoked) throw Error("revoked original authority") }) + let lease: Awaited> | undefined + try { + if (["registry-lock", "owner-lock", "begin-rows-only"].includes(corruption)) { await assert.rejects(gate.acquire(scope)); continue } + lease = await gate.acquire(scope) + if (corruption === "authority") revoked = true + if (corruption === "abort") controller.abort() + if (corruption === "none" || corruption === "longer-original") { + await lease.verify(scope); await lease.verify(scope) + assert.equal(macReads, 4); assert(checks > 0); assert(!released) + assert(sql.some(text => text.includes("/* bandwidth-native:armed-snapshot */")&&text.includes("FOR SHARE NOWAIT"))) + const locks = sql.filter(text => text.includes("764301")); assert.equal(locks.length, 2) + } else if (corruption === "smaller-scope") await assert.rejects(lease.verify({ ...scope, deadlineAt: scope.deadlineAt - 1 })) + else if (corruption === "concurrent-verify") { + const first = lease.verify(scope) + await Promise.all([assert.rejects(first), assert.rejects(lease.verify(scope))]) + } else await assert.rejects(lease.verify(scope)) + } finally { + lease?.close(); assert.equal(destroys, 1); assert.equal(checkouts, 1) + if (lease) await assert.rejects(lease.verify(scope)) + await assert.rejects(gate.acquire(scope)) + } + } +}) diff --git a/backend/src/services/bandwidth-native-dispatch-source.ts b/backend/src/services/bandwidth-native-dispatch-source.ts new file mode 100644 index 00000000..41cbdbed --- /dev/null +++ b/backend/src/services/bandwidth-native-dispatch-source.ts @@ -0,0 +1,115 @@ +/** Staged, server-owned SQL producer for the native handoff kernel. One + * authenticated physical control lease retains registry and exact owner locks + * until ClientRequest.finish. No DML, COMMIT, nested control checkout or repair. */ +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import type { BandwidthBinding, BandwidthScope } from "./bandwidth-admission.js" +import type { createConfiguredBandwidthControlSource, BandwidthControlDispatchFence } from "./bandwidth-control-source.js" +import type { BandwidthNativeDispatchGate } from "./bandwidth-native-dispatch.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +type Core = Pick, "connect" | "eventProofOnConnection"> +const refused = (): never => { throw new Error("Bandwidth native SQL fence refused") } +const sameId = (value: unknown, expected: string) => typeof value === "string" && value.toLowerCase() === expected.toLowerCase() +const hash = (value: unknown) => typeof value === "string" && /^[a-f0-9]{64}$/.test(value) +/** Lock the exact owner BEFORE observing dependent rows. All three facts share + * one command snapshot; LIMIT 3 preserves overflow rejection for the two sets. */ +export const BANDWIDTH_NATIVE_ARMED_SNAPSHOT_SQL=`/* bandwidth-native:armed-snapshot */ WITH + owned AS MATERIALIZED ( + SELECT * FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1 FOR SHARE NOWAIT + ), owners AS MATERIALIZED ( + SELECT node_id,attempt_id,binding_proof,control_generation FROM mmapp_control.bandwidth_node_occupancy + WHERE attempt_id=$1 AND EXISTS(SELECT 1 FROM owned) ORDER BY node_id COLLATE "C" LIMIT 3 + ), events AS MATERIALIZED ( + SELECT * FROM mmapp_control.bandwidth_attempt_events + WHERE attempt_id=$1 AND EXISTS(SELECT 1 FROM owned) ORDER BY ack_id LIMIT 3 + ) + SELECT (SELECT pg_catalog.to_jsonb(a) FROM owned a) AS attempt, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(o) ORDER BY o.node_id COLLATE "C") FROM owners o),'[]'::pg_catalog.jsonb) AS owners, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(e) ORDER BY e.ack_id) FROM events e),'[]'::pg_catalog.jsonb) AS events` + +/** Dependencies are trusted assembly, never request-supplied capabilities. + * check authenticates the ORIGINAL source pin, peers and current original actor + * on the supplied core fence. It must not acquire another control connection. */ +export function createBandwidthNativeDispatchSqlGate(coreSource: Core, input: Readonly, + check: (fence: BandwidthControlDispatchFence, scope: Readonly) => Promise): Readonly { + const binding = structuredClone(input) + Object.freeze(binding.source); Object.freeze(binding.destination); Object.freeze(binding) + if (!hash(binding.bindingProof) || binding.source.canonicalNodeId === binding.destination.canonicalNodeId) refused() + const connect = coreSource.connect.bind(coreSource), eventProof = coreSource.eventProofOnConnection.bind(coreSource) + const authenticate = check + let acquired = false + return Object.freeze({ async acquire(original: Readonly) { + if (acquired) refused(); acquired = true + const entered = Date.now(), mono = performance.now() + const inputDeadline=original.deadlineAt,inputSignal=original.signal + const scope = Object.freeze({ deadlineAt: Math.min(inputDeadline, binding.expiresAt, entered + 5000), signal: inputSignal }) + const end = mono + Math.max(0, scope.deadlineAt - entered) + let connection: ProbeStorageConnection | undefined, active = true, begun = false, busy = false + const live = () => { if (!active || scope.signal?.aborted || Date.now() >= scope.deadlineAt || performance.now() >= end) refused() } + const close = () => { active = false; clearTimeout(timer); scope.signal?.removeEventListener("abort", close); const owned = connection; connection = undefined; owned?.release(true) } + const timer = setTimeout(close, Math.max(1, end - performance.now())) + scope.signal?.addEventListener("abort", close, { once: true }) + const query = async (text: string, values?: unknown[]) => { + live(); const owned = connection ?? refused() + const result = await owned.query(text, values, { signal: scope.signal, timeoutMs: Math.max(1, Math.min(5000, scope.deadlineAt - Date.now())) }) + live(); return result + } + const rows = async (text: string, values?: unknown[]) => (await query(text, values)).rows + const verify = async (current: Readonly) => { + try { + live() + // Only this lease's captured scope is usable. A replacement signal or + // changed deadline cannot leave already scheduled I/O alive afterward. + if (busy || current.signal !== inputSignal || current.deadlineAt !== inputDeadline) refused() + busy = true + // NOWAIT bounds direct runtime writes as well as the cooperating + // advisory protocol. Occupancy/event triggers lock this same row. + const result=await query(BANDWIDTH_NATIVE_ARMED_SNAPSHOT_SQL,[binding.attemptId]) + if(!("command"in result)||result.command!=="SELECT"||result.rows.length!==1)refused() + const snapshot=result.rows[0],row=snapshot.attempt + if(!row||typeof row!=="object"||Array.isArray(row)||Object.keys(snapshot).sort().join(",")!=="attempt,events,owners" + ||!Array.isArray(snapshot.owners)||!Array.isArray(snapshot.events))refused() + const attempt=row as Record,owners=snapshot.owners as Record[],events=snapshot.events as Record[] + if (!sameId(attempt.attempt_id, binding.attemptId) || !isDeepStrictEqual(attempt.binding, binding) + || attempt.caller_kind !== binding.callerKind || attempt.operation_id !== binding.operationId || attempt.state !== "armed" + || attempt.source_node !== binding.source.canonicalNodeId || attempt.destination_node !== binding.destination.canonicalNodeId + || attempt.binding_proof !== binding.bindingProof || attempt.control_generation !== binding.controlGeneration || attempt.resolution !== null || attempt.terminal_proof !== null) refused() + const nodes = [binding.source.canonicalNodeId, binding.destination.canonicalNodeId].sort() + if (owners.length !== 2 || owners.some((owner, index) => owner.node_id !== nodes[index] || !sameId(owner.attempt_id, binding.attemptId) || owner.binding_proof !== binding.bindingProof || owner.control_generation !== binding.controlGeneration)) refused() + if (events.length !== 2 || events.map(event => event.transition).sort().join(",") !== "arm,reserve") refused() + for (const event of events) { + const reserve = event.transition === "reserve" + if (!sameId(event.attempt_id, binding.attemptId) || typeof event.ack_id !== "string" || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(event.ack_id) + || event.binding_proof !== binding.bindingProof || event.control_generation !== binding.controlGeneration + || event.old_state !== (reserve ? null : "reserved") || event.new_state !== (reserve ? "reserved" : "armed")) refused() + const proof = await eventProof({ ackId: event.ack_id, attemptId: binding.attemptId.toLowerCase(), transition: event.transition, + oldState: event.old_state, newState: event.new_state, bindingProof: binding.bindingProof, controlGeneration: binding.controlGeneration, terminalProof: null }, connection!, scope) + live(); if (!hash(proof) || event.evidence_proof !== proof) refused() + } + // The source checks its fixed guard before AND after this query. No + // fresh permit or updated authority is adopted after the final await. + await query("SELECT 1") + live() + } catch (error) { close(); throw error } finally { busy = false } + } + try { + live() + const late = await connect(scope, async fence => { live(); if (begun) { await authenticate(fence, scope); live() } }) + if (!active) { late.release(true); refused() }; connection = late + // READ ONLY forbids row locks in PostgreSQL. The fixed SQL below has no + // writes; this ordinary transaction exists solely to retain locks. + const begin = await query("BEGIN") as { rows: Record[]; command?: unknown } + if (begin.command !== "BEGIN") refused() + begun = true + await query("SELECT pg_catalog.set_config('search_path','pg_catalog',true),pg_catalog.set_config('statement_timeout','5000ms',true),pg_catalog.set_config('lock_timeout','1ms',true)") + const locked = await rows("SELECT pg_catalog.pg_try_advisory_xact_lock_shared(8723116) AS locked") + if (locked.length !== 1 || locked[0].locked !== true) refused() + for (const node of [binding.source.canonicalNodeId, binding.destination.canonicalNodeId].sort()) { + const owner = await rows("SELECT pg_catalog.pg_try_advisory_xact_lock(pg_catalog.hashtextextended($1,764301)) AS locked", ["bandwidth-node:" + node]) + if (owner.length !== 1 || owner[0].locked !== true) refused() + } + live(); return Object.freeze({ verify, close }) + } catch (error) { close(); throw error } + } }) +} diff --git a/backend/src/services/bandwidth-native-dispatch.test.ts b/backend/src/services/bandwidth-native-dispatch.test.ts new file mode 100644 index 00000000..803b3548 --- /dev/null +++ b/backend/src/services/bandwidth-native-dispatch.test.ts @@ -0,0 +1,249 @@ +import assert from "node:assert/strict" +import http from "node:http" +import https from "node:https" +import type { TLSSocket } from "node:tls" +import { execFileSync } from "node:child_process" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, resolve, relative, isAbsolute, basename } from "node:path" +import { once } from "node:events" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { MikrotikClient } from "./mikrotik.js" +import { createBandwidthAdmission, consumeBandwidthNativePermit, type BandwidthBinding, type BandwidthTransition, type BandwidthNativePermit } from "./bandwidth-admission.js" +import type { BandwidthNativeDispatchGate } from "./bandwidth-native-dispatch.js" +import { createBandwidthNativeDispatchCompositeGate } from "./bandwidth-native-dispatch-composite.js" +import { pool, closePool } from "../db/index.js" +import { installManagedMutationSqlFixture } from "../test/fixtures/managed-mutation-sql.js" + +test("real private permit-bound HTTP handoff; fake SQL gate only, no database", async t => { + let held = false, acquired = 0, closed = 0, failVerify = false, failClose = false, delayAcquire: Promise | undefined + let slow = false, posts = 0, created = 0, finishes = 0, beforeCalls = 0, abortOnConstruction: (() => void) | undefined + let receive: (() => void) | undefined, releaseOccupiers: (() => void)[] = [] + let pendingTransport: Promise | undefined + const bodies: unknown[] = [], order: string[] = [], done = [{ status: "done testing", "rx-total-average": "1000" }] + const server = http.createServer((req, res) => { + assert.equal(req.headers.authorization, "Basic " + Buffer.from("fixture:fixture").toString("base64")) + if (req.url === "/rest/occupy/get") { releaseOccupiers.push(() => res.end("{}")); receive?.(); return } + assert.equal(req.url, "/rest/tool/bandwidth-test"); posts++ + let body = ""; req.on("data", chunk => { body += chunk }); req.on("end", () => { + bodies.push(JSON.parse(body)); receive?.() + const timer = setTimeout(() => { res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify(done)) }, slow ? 5200 : 0) + res.once("close", () => clearTimeout(timer)) + }) + }) + server.listen(0, "127.0.0.1"); await once(server, "listening"); const address = server.address(); assert(address && typeof address !== "string") + const params = { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture" } + const client = new MikrotikClient(params, undefined, { requireBandwidthPermit: true }), legacy = new MikrotikClient(params) + const actualRequest = http.request + http.request = ((...args: Parameters) => { + const request = Reflect.apply(actualRequest, http, args) as http.ClientRequest + const options = args[0] as http.RequestOptions + if (options.path === "/rest/tool/bandwidth-test") { + created++; order.push("real-request-construction") + request.once("finish", () => { finishes++; order.push("actual-finish"); if (expectGate) assert(held, "gate must remain held through actual socket handoff") }) + abortOnConstruction?.() + } + return request + }) as typeof http.request + let expectGate = true + const oldQuery = pool.query, oldConnect = pool.connect + pool.query = (() => { throw Error("No fixture DB query") }) as never; pool.connect = (() => { throw Error("No fixture DB checkout") }) as never + const gate: BandwidthNativeDispatchGate = { async acquire() { + acquired++; await delayAcquire; held = true; order.push("lock-acquired") + return { async verify() { assert(held); order.push("last-authority-check"); if (failVerify) throw Error("revoked") }, close() { closed++; held = false; order.push("lock-close"); if (failClose) throw Error("destroy failed") } } + } } + const scope = (ms = 8000, signal = new AbortController().signal) => ({ deadlineAt: Date.now() + ms, signal }) + const reset = () => { assert(!held); pendingTransport = undefined; acquired = 0; closed = 0; failVerify = false; failClose = false; delayAcquire = undefined; slow = false; beforeCalls = 0; abortOnConstruction = undefined; receive = undefined; order.length = 0; expectGate = true } + const ack = (binding: Readonly, transition: BandwidthTransition) => ({ outcome: "new" as const, transition, ackId: randomUUID(), attemptId: binding.attemptId, bindingProof: binding.bindingProof, controlGeneration: binding.controlGeneration }) + const body = { address: "192.0.2.9%gre1@blue", user: "fake-remote", password: "fake-password", duration: "3s", protocol: "udp", direction: "receive", extra: "preserved" } + function broker(options: { gated?: boolean; dispatchGate?: BandwidthNativeDispatchGate; abuseConsume?: boolean; copyReplay?: boolean; durationSec?: number; target?: MikrotikClient } = {}) { + const target = options.target ?? client + let quarantines = 0, completed = 0, savedPermit: BandwidthNativePermit | undefined + const durationSec = options.durationSec ?? 3, nativeBody = { ...body, duration: durationSec + "s" }, expiresAt = Date.now() + 8000 + const authority = { authorityProof: "a".repeat(64), authorityGeneration: "unit-original", controlGeneration: "unit-control", expiresAt } + const endpoints = { source: { canonicalNodeId: "unit-source", endpointProof: "b".repeat(64) }, destination: { canonicalNodeId: "unit-remote", endpointProof: "c".repeat(64) } } + const admission = createBandwidthAdmission({ attemptId: randomUUID(), operationId: "unit-operation", callerKind: "routing-measurement", durationSec, nativeBody, request: { sealed: "exact" }, ...authority, ...endpoints }, { + now: Date.now, cleanupBudgetMs: 1000, nativeClient: target, nativeDispatchGate: options.gated === false ? undefined : options.dispatchGate ?? gate, + authorize: async () => authority, endpoints: async () => endpoints, keyedProof: async () => "d".repeat(64), + journal: { reserve: async binding => ack(binding, "reserve"), arm: async binding => ack(binding, "arm"), complete: async binding => { completed++; return ack(binding, "complete") }, releaseBeforeDispatch: async binding => ack(binding, "release-before-dispatch"), quarantine: async binding => { quarantines++; return ack(binding, "quarantine") } }, + send: async (_binding, command, current, permit) => { + savedPermit = permit + const call = (token: unknown = permit) => target.post("/tool/bandwidth-test", { ...command }, 10000, current.signal, async () => { beforeCalls++; order.push("existing-beforeDispatch") }, token as BandwidthNativePermit) + if (options.abuseConsume) assert.throws(() => consumeBandwidthNativePermit(permit, target, "/tool/bandwidth-test", command, current.signal)) + if (options.copyReplay) await assert.rejects(call({ ...permit })) + pendingTransport = call() + const response = await pendingTransport + if (options.copyReplay) await assert.rejects(call()) + return response + }, + }) + return { admission, counts: () => ({ quarantines, completed }), saved: () => savedPermit } + } + try { + await t.test("real finish releases gate before a 5.2s response; existing hook, exact body/auth and one-shot permit preserved", async () => { + reset(); slow = true; const startPosts = posts, { admission, counts } = broker({ copyReplay: true }), capability = await admission.reserveAndArm(scope()) + const result = await admission.dispatch(capability) + assert.deepEqual(result.raw, done); assert.equal(posts, startPosts + 1); assert.deepEqual(bodies.at(-1), body) + assert.equal(acquired, 1); assert.equal(closed, 1); assert.equal(beforeCalls, 3); assert(!held); assert.deepEqual(counts(), { quarantines: 0, completed: 1 }) + assert(order.indexOf("existing-beforeDispatch") < order.indexOf("lock-acquired")); assert(order.indexOf("last-authority-check") < order.indexOf("real-request-construction")); assert(order.indexOf("actual-finish") < order.indexOf("lock-close")) + assert(order.indexOf("real-request-construction") < order.lastIndexOf("last-authority-check"), "socket-ready authority is checked again after request construction") + }) + await t.test("mapping and SQL barriers both survive actual private-permit finish and retire before the long response", async () => { + reset(); slow = true + let mappingHeld = false, finishedWhileHeld = false + let acknowledgeFinish!: () => void + const finished = new Promise(resolve => { acknowledgeFinish = resolve }) + const mapping: BandwidthNativeDispatchGate = { async acquire() { + mappingHeld = true; order.push("mapping-open") + return { async verify() { assert(mappingHeld); order.push("mapping-verify") }, close() { mappingHeld = false; order.push("mapping-close") } } + } } + const snapshotRequest = http.request + http.request = ((...args: Parameters) => { + const request = Reflect.apply(snapshotRequest, http, args) as http.ClientRequest + if ((args[0] as http.RequestOptions).path === "/rest/tool/bandwidth-test") request.once("finish", () => { assert(mappingHeld); assert(held); finishedWhileHeld = true; acknowledgeFinish() }) + return request + }) as typeof http.request + try { + const before = posts, { admission } = broker({ dispatchGate: createBandwidthNativeDispatchCompositeGate([mapping, gate]) }) + const capability = await admission.reserveAndArm(scope()), pending = admission.dispatch(capability) + await Promise.race([finished, pending.then(() => { throw Error("response arrived without held native finish") })]) + await new Promise(resolve => setTimeout(resolve, 5)) + assert(finishedWhileHeld) + assert(!mappingHeld); assert(!held); assert.equal(posts, before + 1) + assert(order.indexOf("mapping-open") < order.indexOf("lock-acquired")) + assert(order.indexOf("actual-finish") < order.indexOf("mapping-close")) + const result = await pending; assert.deepEqual(result.raw, done) + } finally { http.request = snapshotRequest } + }) + await t.test("historical consume refuses gate-bound permits and cannot bypass the gate", async () => { + reset(); const before = posts, { admission, counts } = broker({ abuseConsume: true }), capability = await admission.reserveAndArm(scope()) + await assert.rejects(admission.dispatch(capability)); assert.equal(posts, before); assert.equal(acquired, 0); assert.equal(counts().quarantines, 1) + }) + await t.test("v1 permit and managed legacy post preserve body/duration; unscoped legacy refuses", async () => { + reset(); expectGate = false; const { admission } = broker({ gated: false, durationSec: 2147483647 }), capability = await admission.reserveAndArm(scope()) + await admission.dispatch(capability); assert.equal((bodies.at(-1) as Record).duration, "2147483647s"); assert.equal(acquired, 0) + const beforeLegacy = posts + await assert.rejects(legacy.post("/tool/bandwidth-test", { ...body }), { code: "MIKROTIK_MANAGED_SOURCE_REQUIRED" }) + assert.equal(posts, beforeLegacy) + const managed = installManagedMutationSqlFixture(71) + try { await new MikrotikClient(params, 71).post("/tool/bandwidth-test", { ...body }); assert.deepEqual(bodies.at(-1), body) } + finally { managed.restore() } + }) + await t.test("revoked final authority and timeout/cancelled late acquire create no request", async () => { + reset(); failVerify = true; const beforeCreated = created, denied = broker(), capability = await denied.admission.reserveAndArm(scope()) + await assert.rejects(denied.admission.dispatch(capability)); assert.equal(created, beforeCreated); assert.equal(closed, 1) + reset(); let resume!: () => void; delayAcquire = new Promise(resolve => { resume = resolve }); const late = broker(), lateCapability = await late.admission.reserveAndArm(scope(35)) + await assert.rejects(late.admission.dispatch(lateCapability)); resume(); await new Promise(resolve => setTimeout(resolve, 20)); assert.equal(created, beforeCreated); assert.equal(closed, 1); assert(!held) + }) + await t.test("queued shared Agent sockets cannot emit a late POST after handoff expiry", async () => { + reset(); expectGate = false; releaseOccupiers = [] + let occupying!: () => void; const occupied = new Promise(resolve => { occupying = resolve }); receive = () => { if (releaseOccupiers.length === 2) occupying() } + const occupiers = [legacy.post("/occupy/get", {}), legacy.post("/occupy/get", {})]; await occupied; expectGate = true; receive = undefined + const before = posts, beforeFinishes = finishes, { admission, counts } = broker(), capability = await admission.reserveAndArm(scope(45)) + await assert.rejects(admission.dispatch(capability)); assert.equal(posts, before) + // Admission's deadline race can settle before the private transport's + // own finalizer. Observe that real promise, not an arbitrary delay or an + // assumption about ordering between two timers with the same deadline. + assert(pendingTransport); await assert.rejects(pendingTransport) + assert.equal(held, false); releaseOccupiers.forEach(release => release()); await Promise.all(occupiers); await new Promise(resolve => setTimeout(resolve, 30)) + assert.equal(posts, before); assert.equal(finishes, beforeFinishes); assert.equal(closed, 1); assert.equal(counts().quarantines, 1) + }) + await t.test("authority revoked while queued is rechecked after socket handoff and emits zero POST", async () => { + reset(); expectGate = false; releaseOccupiers = [] + let occupied!: () => void; const occupying = new Promise(resolve => { occupied = resolve }); receive = () => { if (releaseOccupiers.length === 2) occupied() } + const occupiers = [legacy.post("/occupy/get", {}), legacy.post("/occupy/get", {})]; await occupying; expectGate = true; receive = undefined + const before = posts, beforeCreated = created, beforeFinishes = finishes, { admission, counts } = broker(), capability = await admission.reserveAndArm(scope()) + const pending = admission.dispatch(capability), refused = assert.rejects(pending) + while (created === beforeCreated) await new Promise(resolve => setTimeout(resolve, 1)) + assert(held); failVerify = true; releaseOccupiers.splice(0).forEach(release => release()); await Promise.all(occupiers) + await refused; await new Promise(resolve => setTimeout(resolve, 25)) + assert.equal(posts, before); assert.equal(finishes, beforeFinishes); assert.equal(closed, 1); assert.equal(counts().quarantines, 1); assert(!held) + assert.equal(order.filter(event => event === "last-authority-check").length, 2) + }) + await t.test("wall rollback cannot renew gated acquisition or dispatch a late original permit", async () => { + reset(); let resume!: () => void; delayAcquire = new Promise(resolve => { resume = resolve }) + const originalNow = Date.now, before = created, { admission, counts } = broker(), capability = await admission.reserveAndArm(scope(40)) + const pending = admission.dispatch(capability) + Date.now = () => originalNow() - 10000 + try { await assert.rejects(pending) } finally { Date.now = originalNow } + resume(); await new Promise(resolve => setTimeout(resolve, 20)); assert.equal(created, before); assert.equal(closed, 1); assert.equal(counts().quarantines, 1); assert(!held) + }) + await t.test("abort during request binding destroys before flush, settles once and never posts", async () => { + reset(); const controller = new AbortController(), before = posts, beforeFinishes = finishes, { admission } = broker(), capability = await admission.reserveAndArm(scope(8000, controller.signal)) + abortOnConstruction = () => controller.abort(); await assert.rejects(admission.dispatch(capability)); await new Promise(resolve => setTimeout(resolve, 25)) + assert.equal(posts, before); assert.equal(finishes, beforeFinishes); assert(!held); assert.equal(closed, 1) + }) + await t.test("gate close failure remains unknown and retries physical destruction", async () => { + reset(); failClose = true; const { admission, counts } = broker(), capability = await admission.reserveAndArm(scope()) + await assert.rejects(admission.dispatch(capability)); assert.equal(closed, 2); assert.equal(counts().quarantines, 1); assert.equal(counts().completed, 0); assert(!held) + }) + await t.test("abort after handoff leaves unknown owners while SQL lease remains closed", async () => { + reset(); slow = true; let sent!: () => void; const received = new Promise(resolve => { sent = resolve }); receive = sent + const controller = new AbortController(), { admission, counts } = broker(), capability = await admission.reserveAndArm(scope(8000, controller.signal)) + const pending = admission.dispatch(capability); await received; controller.abort(); await assert.rejects(pending); assert.equal(closed, 1); assert.equal(counts().quarantines, 1); assert(!held) + }) + await t.test("native TLS fresh and established queued sockets reauthorize before flushing", async tls => { + const openssl = process.env.MMAPP_TEST_OPENSSL; assert(openssl, "Set MMAPP_TEST_OPENSSL for the owned TLS fixture") + const directory = await mkdtemp(join(tmpdir(), "mm-native-handoff-tls-")), keyFile = join(directory, "fixture.key"), certFile = join(directory, "fixture.crt") + let tlsServer: https.Server | undefined, agent: https.Agent | undefined + const actualHttpsRequest = https.request + let tlsPosts = 0, tlsCreated = 0, secureReady = false, occupying: (() => void) | undefined + let release: (() => void)[] = [] + const assignedEstablished: boolean[] = [] + try { + execFileSync(openssl, ["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-subj", "/CN=localhost", "-addext", "subjectAltName=IP:127.0.0.1,DNS:localhost", "-days", "2", "-keyout", keyFile, "-out", certFile], { stdio: "ignore", timeout: 20000 }) + const cert = await readFile(certFile), key = await readFile(keyFile) + tlsServer = https.createServer({ key, cert }, (request, response) => { + assert.equal(request.headers.authorization, "Basic " + Buffer.from("fixture:fixture").toString("base64")) + if (request.url === "/rest/occupy/get") { release.push(() => response.end("{}")); occupying?.(); return } + assert.equal(request.url, "/rest/tool/bandwidth-test"); tlsPosts++ + let input = ""; request.on("data", chunk => { input += chunk }); request.on("end", () => { assert.deepEqual(JSON.parse(input), body); response.end(JSON.stringify(done)) }) + }) + tlsServer.on("tlsClientError", () => {}) + tlsServer.listen(0, "127.0.0.1"); await once(tlsServer, "listening"); const bound = tlsServer.address(); assert(bound && typeof bound !== "string") + agent = new https.Agent({ keepAlive: false, maxSockets: 2, ca: cert }) + https.request = ((...args: Parameters) => { + const options = args[0] as https.RequestOptions + assert.equal(options.hostname, "127.0.0.1"); assert.equal(Number(options.port), bound.port) + args[0] = { ...options, agent } as never + const request = Reflect.apply(actualHttpsRequest, https, args) as http.ClientRequest + if (options.path === "/rest/tool/bandwidth-test") { + tlsCreated++ + request.once("socket", value => { secureReady = !!(value as TLSSocket).getPeerFinished(); assignedEstablished.push(secureReady); value.once("secureConnect", () => { secureReady = true }) }) + request.once("finish", () => { assert(held); assert(secureReady, "TLS bytes must wait for the authenticated socket handshake") }) + } + return request + }) as typeof https.request + const endpoint = { ...params, port: bound.port, useSsl: true, verifySsl: true }, target = new MikrotikClient(endpoint, undefined, { requireBandwidthPermit: true }), plain = new MikrotikClient(endpoint) + await tls.test("fresh TLS socket waits secureConnect and genuine permit succeeds", async () => { + reset(); const { admission } = broker({ target }), capability = await admission.reserveAndArm(scope()) + await admission.dispatch(capability); assert.equal(tlsPosts, 1); assert.equal(assignedEstablished.at(-1), false); assert.equal(closed, 1) + }) + const occupy = async () => { + release = []; let both!: () => void; const arrived = new Promise(resolve => { both = resolve }); occupying = () => { if (release.length === 2) both() } + const pending = [plain.post("/occupy/get", {}), plain.post("/occupy/get", {})]; await arrived; occupying = undefined; return pending + } + await tls.test("queued established TLS socket succeeds without waiting for a second secureConnect event", async () => { + reset(); const blockers = await occupy(), beforeCreated = tlsCreated, beforePosts = tlsPosts, { admission } = broker({ target }), capability = await admission.reserveAndArm(scope()) + const pending = admission.dispatch(capability) + while (tlsCreated === beforeCreated) await new Promise(resolve => setTimeout(resolve, 1)) + assert(held); release.splice(0).forEach(done => done()); await Promise.all(blockers); await pending + assert.equal(tlsPosts, beforePosts + 1); assert.equal(assignedEstablished.at(-1), true); assert.equal(closed, 1) + }) + await tls.test("queued established TLS socket revoked before assignment emits zero POST", async () => { + reset(); const blockers = await occupy(), beforeCreated = tlsCreated, beforePosts = tlsPosts, { admission, counts } = broker({ target }), capability = await admission.reserveAndArm(scope()) + const pending = admission.dispatch(capability), refused = assert.rejects(pending) + while (tlsCreated === beforeCreated) await new Promise(resolve => setTimeout(resolve, 1)) + failVerify = true; release.splice(0).forEach(done => done()); await Promise.all(blockers); await refused + assert.equal(tlsPosts, beforePosts); assert.equal(assignedEstablished.at(-1), true); assert.equal(closed, 1); assert.equal(counts().quarantines, 1) + }) + } finally { + https.request = actualHttpsRequest; release.forEach(done => done()); agent?.destroy() + if (tlsServer) { tlsServer.closeAllConnections(); await new Promise(resolve => tlsServer!.close(() => resolve())) } + const within = relative(resolve(tmpdir()), resolve(directory)); assert(within && !within.startsWith("..") && !isAbsolute(within) && basename(directory).startsWith("mm-native-handoff-tls-")) + await rm(directory, { recursive: true, force: true }) + } + }) + } finally { http.request = actualRequest; pool.query = oldQuery; pool.connect = oldConnect; releaseOccupiers.forEach(release => release()); server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())); await closePool() } +}) diff --git a/backend/src/services/bandwidth-native-dispatch.ts b/backend/src/services/bandwidth-native-dispatch.ts new file mode 100644 index 00000000..ee41ae61 --- /dev/null +++ b/backend/src/services/bandwidth-native-dispatch.ts @@ -0,0 +1,107 @@ +/** Internal committed-permit scheduling only. A fixed server-owned gate supplies + * actual registry serialization and original-authority checks on ONE lease. + * This module does not mint permits, register nodes or configure a default gate. */ +import { ClientRequest } from "node:http" +import type { Socket } from "node:net" +import { TLSSocket } from "node:tls" +import { performance } from "node:perf_hooks" +import type { BandwidthScope } from "./bandwidth-admission.js" + +export interface BandwidthNativeDispatchLease { + /** Same-lease shared registry lock and final original/source/peer/owner checks. */ + verify(scope: Readonly): Promise + /** Physically destroy the owned read-only lease; never return it healthy. */ + close(): void +} +export interface BandwidthNativeDispatchGate { + acquire(scope: Readonly): Promise +} +export type BandwidthNativeRequestBinder = (request: ClientRequest, sendBytesOnce: () => void) => void +export class BandwidthNativeDispatchError extends Error { + readonly automaticReplay = false + constructor(readonly code: "REFUSED" | "OUTCOME_UNKNOWN") { super("Bandwidth native dispatch " + code) } +} +/** Called only by the private committed-permit dispatcher. start synchronously + * constructs rosPost and binds its exact request BEFORE write/end. A caller + * callback, evidence JSON or a queued Agent request cannot acknowledge handoff. */ +export async function dispatchBandwidthNativeRequest(gate: Readonly, original: Readonly, originalMonotonicEnd: number, + assertOriginalPermit: () => void, start: (bindRequest: BandwidthNativeRequestBinder) => Promise): Promise { + const entered = Date.now(), enteredMono = performance.now(), signal = original.signal + const deadlineAt = Math.min(original.deadlineAt, entered + 5000) + const monotonicEnd = Math.min(originalMonotonicEnd, enteredMono + Math.max(0, deadlineAt - entered)) + const controller = new AbortController(), scope = Object.freeze({ deadlineAt, signal: controller.signal }) + let active = true, invoked = false, lease: Readonly | undefined, request: ClientRequest | undefined + let rejectStopped!: (error: BandwidthNativeDispatchError) => void, rejectHandoff: ((error: BandwidthNativeDispatchError) => void) | undefined + const error = () => new BandwidthNativeDispatchError(invoked ? "OUTCOME_UNKNOWN" : "REFUSED") + const closeLease = (strict = false) => { + const current = lease; lease = undefined + if (!current) return + try { current.close() } + catch { try { current.close() } catch { /* Retry physical destruction, never healthy pool return. */ } if (strict) throw error() } + } + const stop = () => { active = false; controller.abort(); request?.destroy(error()); closeLease(); const failure = error(); rejectHandoff?.(failure); rejectStopped(failure) } + const live = () => { if (!active || signal?.aborted || Date.now() >= deadlineAt || performance.now() >= monotonicEnd) throw error() } + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + const timer = setTimeout(stop, Math.max(1, monotonicEnd - performance.now())) + signal?.addEventListener("abort", stop, { once: true }) + let cleanupRequest = () => {} + const run = async () => { + live(); assertOriginalPermit() + const acquired = await gate.acquire(scope) + if (!active) { try { acquired.close() } catch { try { acquired.close() } catch { /* Late lease must be destroyed. */ } } throw error() } + const ownedLease = Object.freeze({ verify: acquired.verify.bind(acquired), close: acquired.close.bind(acquired) }) + lease = ownedLease; live(); assertOriginalPermit() + await ownedLease.verify(scope); live(); assertOriginalPermit() + let acknowledge!: () => void + const handoff = new Promise((resolve, reject) => { acknowledge = resolve; rejectHandoff = reject }) + void handoff.catch(() => {}) + const bindRequest: BandwidthNativeRequestBinder = (created, sendBytesOnce) => { + if (!(created instanceof ClientRequest)) throw error() + if (!active || request) { created.destroy(error()); throw error() } + request = created + let socketHandled = false, sendStarted = false, cleanupReady = () => {} + const finish = () => { try { live(); if (!sendStarted || !created.socket || created.destroyed) throw error(); acknowledge() } catch { rejectHandoff?.(error()) } } + const failed = () => rejectHandoff?.(error()) + const ready = () => { + cleanupReady() + void (async () => { + live(); assertOriginalPermit() + // A queued request may have waited while the original actor, source + // or mapping was revoked. Recheck on THIS lease after socket readiness. + await ownedLease.verify(scope); live(); assertOriginalPermit() + sendStarted = true + sendBytesOnce() // No await between final authority and header/body flush. + })().catch(() => { rejectHandoff?.(error()); created.destroy(error()) }) + } + const socket = (current: Socket) => { + if (socketHandled) return + socketHandled = true + if (current instanceof TLSSocket && !current.getPeerFinished()) { + current.once("secureConnect", ready); cleanupReady = () => current.removeListener("secureConnect", ready) + } else if (current.connecting) { + current.once("connect", ready); cleanupReady = () => current.removeListener("connect", ready) + } else ready() + } + created.once("finish", finish); created.once("error", failed); created.once("close", failed) + created.once("socket", socket) + cleanupRequest = () => { cleanupReady(); created.removeListener("socket", socket); created.removeListener("finish", finish); created.removeListener("error", failed); created.removeListener("close", failed) } + if (created.socket) socket(created.socket) + } + // Request construction does not send bytes. The fixed private binder waits + // through Agent queue/DNS/TLS, revalidates, then flushes under the same gate. + invoked = true + const response = start(bindRequest) + void response.catch(() => {}) + if (!request) throw error() + await handoff; live() + closeLease(true) + return { response } + } + try { + const completed = await Promise.race([run(), stopped]) + // Do not await response here: retire the SHORT SQL lifetime immediately. + // rosPost/admission retain the original response timeout, abort and parser. + return completed.response + } catch { request?.destroy(error()); throw error() } + finally { active = false; clearTimeout(timer); signal?.removeEventListener("abort", stop); controller.abort(); cleanupRequest(); closeLease() } +} diff --git a/backend/src/services/bandwidth-native-permit.sql-helper.ts b/backend/src/services/bandwidth-native-permit.sql-helper.ts new file mode 100644 index 00000000..1ffefaf1 --- /dev/null +++ b/backend/src/services/bandwidth-native-permit.sql-helper.ts @@ -0,0 +1,91 @@ +/** Actual PostgreSQL -> admission -> strict RouterOS HTTP acceptance. Only an + * explicitly owned disposable fixture may invoke this helper. */ +import assert from "node:assert/strict" +import {createServer} from "node:http" +import {once} from "node:events" +import {randomUUID} from "node:crypto" +import type {Pool} from "pg" +import type {createConfiguredBandwidthControlSource} from "./bandwidth-control-source.js" +import {createBandwidthAdmission,type BandwidthBinding,type BandwidthJournal,type BandwidthNativePermit,type BandwidthScope} from "./bandwidth-admission.js" +import {assertOwnedBandwidthSql,fixtureMac,scope} from "./bandwidth-admission-process.sql-helper.js" + +export async function acceptBandwidthNativePermits(source:ReturnType,owner:Pool,controlGeneration:string){ + assertOwnedBandwidthSql() + const {MikrotikClient}=await import("./mikrotik.js") + const {createBandwidthAdmissionSqlJournal}=await import("./bandwidth-admission-storage.js") + const checks:string[]=[] + let posts=0 + const scenarios=["direct","helper","arm-ack-loss","wrong-client","copied-permit","changed-body","nonterminal"] as const + for(const scenario of scenarios){ + let scenarioPosts=0,received:unknown + const server=createServer(async(req,res)=>{ + let text="";for await(const chunk of req)text+=String(chunk) + assert.equal(req.url,"/rest/tool/bandwidth-test");assert.equal(req.method,"POST") + received=JSON.parse(text);scenarioPosts++;posts++ + res.setHeader("content-type","application/json") + res.end(JSON.stringify([{status:scenario==="nonterminal"?"running":"done testing","tx-total-average":"1000","rx-total-average":"2000"}])) + }) + server.listen(0,"127.0.0.1");await once(server,"listening") + const address=server.address();assert(address&&typeof address==="object") + const params={host:"127.0.0.1",port:address.port,useSsl:false,verifySsl:false,username:"synthetic-native-user",password:"synthetic-native-password"} + const client=new MikrotikClient(params,undefined,{requireBandwidthPermit:true}) + const other=new MikrotikClient(params,undefined,{requireBandwidthPermit:true}) + const nativeBody={address:"198.51.100.42@isolated-test",user:"synthetic-peer",password:"synthetic-peer-password",protocol:"tcp",direction:"both",duration:"3s"} + let activeConnection:Awaited>|undefined,activeScope:BandwidthScope|undefined,terminalProof:string|undefined,lastRaw:unknown + const rawJournal=createBandwidthAdmissionSqlJournal({now:Date.now, + connect:async window=>{activeScope=window;activeConnection=await source.connect(window);return activeConnection}, + authorize:async(binding,window)=>{assert.equal(binding.controlGeneration,controlGeneration);assert.equal(binding.authorityProof,fixtureMac("authority"));assert(Date.now(){ + if(transition==="complete"){assert(terminalProof);assert.deepEqual(evidence,{terminalProof})} + if(transition==="release-before-dispatch")assert.deepEqual(evidence,{from:"armed",proof:"private-no-send"}) + }, + eventProof:async event=>{assert(activeConnection&&activeScope);return source.eventProofOnConnection(event,activeConnection,activeScope)}, + }) + const journal:BandwidthJournal={...rawJournal,arm:async(binding,window)=>{const ack=await rawJournal.arm(binding,window);if(scenario==="arm-ack-loss")throw Error("Owned actual arm COMMIT ACK deliberately lost");return ack}} + const id=randomUUID(),input={attemptId:id,callerKind:"mcp-speed" as const,operationId:randomUUID(),request:{scenario}, + source:{canonicalNodeId:"strict:"+id+":A",endpointProof:fixtureMac([id,"source"])}, + destination:{canonicalNodeId:"strict:"+id+":B",endpointProof:fixtureMac([id,"remote"])}, + authorityProof:fixtureMac("authority"),authorityGeneration:"synthetic-original-grant",controlGeneration,expiresAt:Date.now()+60000,durationSec:3,nativeBody} + const admission=createBandwidthAdmission(input,{now:Date.now,cleanupBudgetMs:10000,nativeClient:client,journal, + keyedProof:async(purpose,value)=>{ + if(purpose==="bandwidth-terminal-v1")assert.deepEqual((value as {raw:unknown}).raw,lastRaw) + const proof=await source.eventProof({purpose,value},scope()) + if(purpose==="bandwidth-terminal-v1")terminalProof=proof + return proof + }, + authorize:async()=>({authorityProof:input.authorityProof,authorityGeneration:input.authorityGeneration,controlGeneration,expiresAt:input.expiresAt}), + endpoints:async()=>({source:input.source,destination:input.destination}), + send:async(_binding:Readonly,body,window,permit)=>{ + const selected=scenario==="wrong-client"?other:client + const usedPermit=scenario==="copied-permit"?JSON.parse(JSON.stringify(permit)) as BandwidthNativePermit:permit + const usedBody=scenario==="changed-body"?{...body,address:"203.0.113.99"}:body + const raw=scenario==="helper"?await selected.bandwidthTest({address:body.address,user:body.user,password:body.password,protocol:"tcp",direction:"both",durationSec:3},window.signal,undefined,usedPermit) + :await selected.post("/tool/bandwidth-test",usedBody,30000,window.signal,undefined,usedPermit) + lastRaw=raw + await assert.rejects(selected.post("/tool/bandwidth-test",body,30000,window.signal,undefined,permit)) + return raw + }, + }) + try{ + await assert.rejects(client.post("/tool/bandwidth-test",nativeBody)) + await assert.rejects(client.bandwidthTest({address:nativeBody.address,user:nativeBody.user,password:nativeBody.password,durationSec:3})) + assert.equal(scenarioPosts,0) + if(scenario==="arm-ack-loss")await assert.rejects(admission.reserveAndArm(scope()),error=>error instanceof Error&&"code" in error&&error.code==="PERSISTENCE_UNKNOWN") + else{ + const capability=await admission.reserveAndArm(scope()) + if(scenario==="direct"||scenario==="helper")assert.equal((await admission.dispatch(capability)).remoteCompletionConfirmed,true) + else await assert.rejects(admission.dispatch(capability),error=>error instanceof Error&&"code" in error&&error.code==="REMOTE_UNKNOWN") + await assert.rejects(admission.dispatch(capability)) + } + const completed=scenario==="direct"||scenario==="helper" + const occupancy=await owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1",[id]) + assert.equal(Number(occupancy.rows[0].n),completed?0:2) + assert.equal(scenarioPosts,completed||scenario==="nonterminal"?1:0) + if(scenarioPosts)assert.deepEqual(received,nativeBody) + const persisted=await owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1",[id]) + assert.equal(persisted.rows[0].state,completed?"released":"unknown_busy") + checks.push("actual signed-source/runtime-role PG journal -> strict native "+scenario+": "+scenarioPosts+" HTTP POST; exact body, no permit replay; "+(completed?"terminal completion releases both owners":"both owners stay unknown-busy")) + }finally{server.closeAllConnections();await new Promise((resolve,reject)=>server.close(error=>error?reject(error):resolve()))} + } + return {checks,scenarios:scenarios.length,actualLocalhostBandwidthPosts:posts,realChrCalls:0,globalCallerActivation:false,operatorAuthoritySynthetic:true} +} diff --git a/backend/src/services/bandwidth-native-permit.test.ts b/backend/src/services/bandwidth-native-permit.test.ts new file mode 100644 index 00000000..9e247f13 --- /dev/null +++ b/backend/src/services/bandwidth-native-permit.test.ts @@ -0,0 +1,175 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { MikrotikClient } from "./mikrotik.js" +import { closePool, pool } from "../db/index.js" +import { createBandwidthAdmission, consumeBandwidthNativePermit, type BandwidthAdmissionDependencies, type BandwidthAdmissionInput, type BandwidthJournalAck, type BandwidthNativePermit } from "./bandwidth-admission.js" + +const canonical = (value: unknown): string => Array.isArray(value) ? "[" + value.map(canonical).join(",") + "]" : value && typeof value === "object" ? "{" + Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value) +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const body = () => ({ address: "192.0.2.2", user: "synthetic-destination", password: "synthetic-secret", protocol: "tcp", direction: "both", duration: "2147483647s" }) + +test("staged strict native permit: private admission and actual localhost HTTP", async t => { + const received: Array<{ path: string; body: unknown; auth: string }> = [] + const server = createServer(async (request, response) => { + let text = ""; for await (const chunk of request) text += String(chunk) + received.push({ path: request.url!, body: JSON.parse(text), auth: request.headers.authorization! }) + response.writeHead(200, { "content-type": "application/json" }); response.end(JSON.stringify([{ status: "done testing" }])) + }) + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const address = server.address(); assert(address && typeof address !== "string") + const params = () => ({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "synthetic-source", password: "source-secret" }) + const strict = () => new MikrotikClient(params(), undefined, { requireBandwidthPermit: true }) + const originalQuery = pool.query, originalConnect = pool.connect + let sqlCalls = 0, groups = 0 + pool.query = (() => { sqlCalls++; throw Error("Native permit fixture must perform no SQL") }) as never + pool.connect = (() => { sqlCalls++; throw Error("Native permit fixture must acquire no backend") }) as never + function fixture(client: object | undefined, send: BandwidthAdmissionDependencies["send"], durationMs = 2000) { + const input: BandwidthAdmissionInput = { attemptId: randomUUID(), callerKind: "rest-speed", operationId: "owned-localhost-fixture", request: { duration: 2147483647 }, + source: { canonicalNodeId: "synthetic-A", endpointProof: "a".repeat(64) }, destination: { canonicalNodeId: "synthetic-B", endpointProof: "b".repeat(64) }, + authorityProof: "c".repeat(64), authorityGeneration: "synthetic-actor-v1", controlGeneration: "synthetic-control-v1", expiresAt: Date.now() + durationMs, durationSec: 2147483647, nativeBody: body() } + const transitions: string[] = [] + let occupancy = "free" + const ack = (transition: BandwidthJournalAck["transition"], binding: Parameters[0]): BandwidthJournalAck => { + transitions.push(transition); return { outcome: "new", transition, ackId: randomUUID(), attemptId: binding.attemptId, bindingProof: binding.bindingProof, controlGeneration: binding.controlGeneration } + } + const deps: BandwidthAdmissionDependencies = { nativeClient: client, now: Date.now, cleanupBudgetMs: 100, + keyedProof: async (purpose, value) => createHmac("sha256", "synthetic-unit-journal-key").update(purpose + ":" + canonical(value)).digest("hex"), + authorize: async binding => ({ authorityProof: binding.authorityProof, authorityGeneration: binding.authorityGeneration, controlGeneration: binding.controlGeneration, expiresAt: binding.expiresAt }), + endpoints: async binding => ({ source: binding.source, destination: binding.destination }), send, + journal: { + reserve: async binding => { assert.equal(occupancy, "free"); occupancy = "reserved"; return ack("reserve", binding) }, + arm: async binding => { assert.equal(occupancy, "reserved"); occupancy = "armed"; return ack("arm", binding) }, + complete: async binding => { assert.equal(occupancy, "armed"); occupancy = "free"; return ack("complete", binding) }, + releaseBeforeDispatch: async binding => { occupancy = "free"; return ack("release-before-dispatch", binding) }, + quarantine: async binding => { occupancy = "unknown-busy"; return ack("quarantine", binding) }, + } } + return { deps, input, transitions, occupancy: () => occupancy, gate: () => createBandwidthAdmission(input, deps) } + } + const run = async (f: ReturnType, signal?: AbortSignal) => { const gate = f.gate(); return gate.dispatch(await gate.reserveAndArm({ deadlineAt: f.input.expiresAt, signal })) } + const check = async (name: string, work: () => Promise) => { await t.test(name, work); groups++ } + try { + await check("unadmitted helper/direct, forged and serialized permits produce zero HTTP", async () => { + const client = strict(), start = received.length + await assert.rejects(client.bandwidthTest({ ...body(), durationSec: 2147483647, protocol: "tcp", direction: "both" })) + for (const permit of [undefined, {}, Object.freeze(Object.create(null)), JSON.parse("{}")]) await assert.rejects(client.post("/tool/bandwidth-test", body(), 500, undefined, undefined, permit as BandwidthNativePermit)) + assert.equal(received.length, start) + }) + for (const mode of ["helper", "direct"] as const) await check("actual admitted " + mode + " preserves full duration and consumes one permit", async () => { + const client = strict(); let saved: BandwidthNativePermit | undefined + const f = fixture(client, async (_binding, native, scope, permit) => { + assert.equal(f.occupancy(), "armed"); assert.deepEqual(f.transitions, ["reserve", "arm"]); saved = permit + return mode === "direct" ? client.post("/tool/bandwidth-test", { ...native }, 500, scope.signal, undefined, permit) + : client.bandwidthTest({ address: native.address, user: native.user, password: native.password, protocol: "tcp", direction: "both", durationSec: 2147483647 }, scope.signal, undefined, permit) + }) + const start = received.length; assert.equal((await run(f)).remoteCompletionConfirmed, true) + assert.equal(received.length, start + 1); assert.deepEqual(received.at(-1)!.body, body()); assert.equal(received.at(-1)!.path, "/rest/tool/bandwidth-test") + assert.equal(f.occupancy(), "free"); await assert.rejects(client.post("/tool/bandwidth-test", body(), 500, undefined, undefined, saved)); assert.equal(received.length, start + 1) + }) + for (const attack of ["copy", "serialize", "client", "body", "signal", "missing-client"] as const) await check("admitted permit cannot transfer via " + attack, async () => { + const client = strict(), other = strict(), controller = new AbortController(), start = received.length + const f = fixture(attack === "missing-client" ? undefined : client, async (_binding, native, scope, permit) => { + const candidate = attack === "copy" ? { ...permit } : attack === "serialize" ? JSON.parse(JSON.stringify(permit)) : permit + await assert.rejects((attack === "client" ? other : client).post("/tool/bandwidth-test", attack === "body" ? { ...native, address: "192.0.2.99" } : { ...native }, 500, attack === "signal" ? controller.signal : scope.signal, undefined, candidate)) + if (!["copy", "serialize"].includes(attack)) await assert.rejects(client.post("/tool/bandwidth-test", { ...native }, 500, scope.signal, undefined, permit), "failed genuine use consumes permit") + throw Error("private send refused") + }) + await assert.rejects(run(f)); assert.equal(received.length, start); assert.equal(f.occupancy(), "unknown-busy") + }) + await check("native target captured exactly once despite later dependencies mutation", async () => { + const client = strict(), other = strict(), start = received.length + const f = fixture(client, (_binding, native, scope, permit) => client.post("/tool/bandwidth-test", { ...native }, 500, scope.signal, undefined, permit)) + let reads = 0; Object.defineProperty(f.deps, "nativeClient", { configurable: true, get() { reads++; return client } }) + const gate = f.gate(); assert.equal(reads, 1); Object.defineProperty(f.deps, "nativeClient", { value: other }) + await gate.dispatch(await gate.reserveAndArm({ deadlineAt: f.input.expiresAt })); assert.equal(reads, 1); assert.equal(received.length, start + 1) + }) + await check("strict constructor snapshots mutable endpoint and credentials", async () => { + const mutable = params(), client = new MikrotikClient(mutable, undefined, { requireBandwidthPermit: true }), start = received.length + mutable.host = "invalid.invalid"; mutable.port = 1; mutable.username = "MUTATED"; mutable.password = "MUTATED"; mutable.useSsl = true + const f = fixture(client, (_binding, native, scope, permit) => client.post("/tool/bandwidth-test", { ...native }, 500, scope.signal, undefined, permit)) + await run(f); assert.equal(received.length, start + 1); assert.equal(received.at(-1)!.auth, "Basic " + Buffer.from("synthetic-source:source-secret").toString("base64")) + }) + await check("strict bodies refuse accessors without evaluation; synchronous body snapshot survives callback mutation", async () => { + const client = strict(), start = received.length; let reads = 0 + const denied = fixture(client, async (_binding, native, scope, permit) => { + const accessor = { ...native }; Object.defineProperty(accessor, "address", { enumerable: true, get() { reads++; return native.address } }) + return client.post("/tool/bandwidth-test", accessor, 500, scope.signal, undefined, permit) + }) + await assert.rejects(run(denied)); assert.equal(reads, 0); assert.equal(received.length, start) + const accepted = fixture(client, (_binding, native, scope, permit) => { + const mutable = { ...native }; return client.post("/tool/bandwidth-test", mutable, 500, scope.signal, async () => { mutable.address = "192.0.2.99"; mutable.password = "CHANGED" }, permit) + }) + await run(accepted); assert.equal(received.length, start + 1); assert.deepEqual(received.at(-1)!.body, body()) + }) + await check("encoded/slashed/case/dot aliases never bypass strict canonical path", async () => { + const client = strict(), start = received.length + for (const path of ["/tool/%62andwidth-test", "/tool/%2562andwidth-test", "/%74ool/bandwidth-test", "/tool//bandwidth-test", "/TOOL/BANDWIDTH-TEST", "/x/../tool/bandwidth-test", "/tool/bandwidth-test/", "/tool/bandwidth-test?x=1", "/tool/bandwidth-test?bad=%", "/%74ool/bandwidth-test?bad=%", "/tool/bandwidth-test/%"]) { + await assert.rejects(client.post(path, body(), 500)) + const f = fixture(client, (_binding, native, scope, permit) => client.post(path, { ...native }, 500, scope.signal, undefined, permit)) + await assert.rejects(run(f)) + } + assert.equal(received.length, start) + }) + await check("custom apiPath cannot hide a bandwidth command in the final request target",async()=>{ + const start=received.length + for(const [apiPath,path] of [["/rest/tool","/bandwidth-test"],["/rest/tool/bandwidth-test",""],["/rest/%74ool","/%62andwidth-test"],["/proxy/rest/tool","/bandwidth-test?bad=%"],["/proxy%25/rest/%74ool","/bandwidth-test"],["/proxy%/rest/%74ool","/bandwidth-test"]]){ + const client=new MikrotikClient({...params(),apiPath},undefined,{requireBandwidthPermit:true}) + await assert.rejects(client.post(path,body(),500)) + const f=fixture(client,(_binding,native,scope,permit)=>client.post(path,{...native},500,scope.signal,undefined,permit)) + await assert.rejects(run(f)) + } + assert.equal(received.length,start) + const configured={...params(),apiPath:"/private-proxy/rest"} + const client=new MikrotikClient(configured,undefined,{requireBandwidthPermit:true}) + configured.apiPath="/rest/tool" + const valid=fixture(client,(_binding,native,scope,permit)=>client.post("/tool/bandwidth-test",{...native},500,scope.signal,undefined,permit)) + await run(valid) + assert.equal(received.length,start+1) + assert.equal(received.at(-1)!.path,"/private-proxy/rest/tool/bandwidth-test") + }) + await check("clock rollback before mint cannot renew the original native monotonic budget",async()=>{ + const client=strict(),start=received.length,realNow=Date.now + const f=fixture(client,(_binding,native,scope,permit)=>client.post("/tool/bandwidth-test",{...native},500,scope.signal,undefined,permit),80) + f.deps.now=()=>Date.now() + const arm=f.deps.journal.arm.bind(f.deps.journal) + f.deps.journal.arm=async(binding,scope)=>{ + const ack=await arm(binding,scope) + Date.now=()=>realNow()-60000 + await delay(120) + return ack + } + try{await assert.rejects(run(f));assert.equal(received.length,start);assert.equal(f.occupancy(),"unknown-busy")} + finally{Date.now=realNow} + }) + for (const reason of ["expiry", "abort"] as const) await check("late callback after " + reason + " cannot dispatch saved permit", async () => { + const client = strict(), controller = new AbortController(), entered = deferred(), release = deferred(), start = received.length + let late: Promise | undefined + const f = fixture(client, (_binding, native, scope, permit) => { + late = client.post("/tool/bandwidth-test", { ...native }, 500, scope.signal, async () => { entered.resolve(); await release.promise }, permit) + return late + }, reason === "expiry" ? 80 : 2000) + const pending = run(f, controller.signal), rejected = assert.rejects(pending) + await entered.promise; if (reason === "abort") controller.abort() + await rejected; assert.equal(received.length, start); assert.equal(f.occupancy(), "unknown-busy") + release.resolve(); await assert.rejects(late!); await delay(10); assert.equal(received.length, start) + }) + await check("verification export cannot mint; fromServer opts into strict mode", async () => { + assert.throws(() => consumeBandwidthNativePermit({}, strict(), "/tool/bandwidth-test", body())) + const from = MikrotikClient.fromServer({ ...params(), id: 123 } as Parameters[0], { requireBandwidthPermit: true }) + const start = received.length, accessor = body(); let reads = 0 + Object.defineProperty(accessor, "address", { enumerable: true, get() { reads++; return "192.0.2.2" } }) + // Strict-body validation occurs before the server-scoped configuration + // path. A legacy client would reach the forbidden SQL hook instead. + await assert.rejects(from.post("/tool/bandwidth-test", accessor, 500)); assert.equal(reads, 0); assert.equal(sqlCalls, 0) + const aborted = new AbortController(); aborted.abort() + await assert.rejects(from.bandwidthTest({ ...body(), protocol: "tcp", direction: "both" }, aborted.signal)); assert.equal(received.length, start) + assert.equal(sqlCalls, 0) + }) + console.log("Bandwidth native permit:", JSON.stringify({ groups, actualLocalhostHttp: received.length, sqlCalls, realChrCalls: 0, fakeJournalOnly: true, mandatoryAllCallerGate: false })) + } finally { + pool.query = originalQuery; pool.connect = originalConnect + server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())); await closePool() + } +}) diff --git a/backend/src/services/bandwidth-node-bootstrap-v2.test.ts b/backend/src/services/bandwidth-node-bootstrap-v2.test.ts new file mode 100644 index 00000000..3d193ce1 --- /dev/null +++ b/backend/src/services/bandwidth-node-bootstrap-v2.test.ts @@ -0,0 +1,126 @@ +/** Unit-only IO: signed payload serialization and source/fence version routing. + * Catalog/role internals have separate actual PostgreSQL acceptance. */ +import assert from "node:assert/strict" +import { createHash, createHmac, randomUUID } from "node:crypto" +import { mkdtemp, writeFile, rm } from "node:fs/promises" +import os from "node:os" +import path from "node:path" +import { test, mock } from "node:test" +import { BANDWIDTH_NODE_REGISTRY_DDL } from "./bandwidth-node-registry-schema.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL } from "./bandwidth-external-node-registry-schema.js" +import { bandwidthNodeBootstrapSchema, canonicalBandwidthNodeBootstrap, canonicalBandwidthNodeValue, bandwidthNodeRegistrySchemaSourceSha256, bandwidthPeerRegistrySchemaSourceSha256 } from "./bandwidth-node-bootstrap.js" + +const sha = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +test("v1 canonical bytes/hash stay fixed while v2 explicitly binds the combined DDL", () => { + const root = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64), registryCatalogSha256: "c".repeat(64), attestationRef: "unit-only explicit operator attestation reference" } + const v1 = { format: "mmapp-bandwidth-node-bootstrap-v1", ...root, registrySchemaSourceSha256: bandwidthNodeRegistrySchemaSourceSha256() } + assert.equal(bandwidthNodeRegistrySchemaSourceSha256(), sha(BANDWIDTH_NODE_REGISTRY_DDL)) + assert.equal(canonicalBandwidthNodeBootstrap(v1), canonicalBandwidthNodeValue(v1)) + const v2 = { ...v1, format: "mmapp-bandwidth-node-bootstrap-v2", registrySchemaSourceSha256: bandwidthPeerRegistrySchemaSourceSha256() } + assert.equal(bandwidthPeerRegistrySchemaSourceSha256(), sha(BANDWIDTH_NODE_REGISTRY_DDL + "\0" + BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL)) + assert.notEqual(v1.registrySchemaSourceSha256, v2.registrySchemaSourceSha256) + assert.equal(canonicalBandwidthNodeBootstrap(v2), canonicalBandwidthNodeValue(v2)) + assert.throws(() => bandwidthNodeBootstrapSchema.parse({ ...v2, format: "mmapp-bandwidth-node-bootstrap-v3" })) + assert.throws(() => bandwidthNodeBootstrapSchema.parse({ ...v2, upgrade: true })) +}) + +test("source dual-version catalog/role selection, external shapes and unchanged canonical MAC domains", async () => { + // This fixture supplies a scalar fake core. Fused core/registry SQL has its + // own genuine signed-file frame and owned PostgreSQL acceptance suites. + mock.module("./bandwidth-control-registry-identity.js",{namedExports:{assertBandwidthControlRegistryIdentity:async()=>{throw Error("scalar core fixture must not invoke joint SQL")}}}) + const counters = { managed: 0, combined: 0, role: 0, externalRole: 0, connections: 0, releases: 0 } + let extension = false, externalRoleAllowed = true, drift: (() => void) | undefined + const catalog = "c".repeat(64) + mock.module("./bandwidth-node-registry-catalog.js", { namedExports: { + observeBandwidthNodeRegistryCatalog: async (_sql: unknown, live: () => void) => { counters.managed++; live(); if (extension) throw Error("v1 rejects extension"); return catalog }, + observeBandwidthPeerRegistryCatalog: async (_sql: unknown, live: () => void) => { counters.combined++; live(); if (!extension) throw Error("missing external component"); return catalog }, + } }) + mock.module("./bandwidth-node-registry-role.js", { namedExports: { assertBandwidthNodeRegistryRuntimeRole: async (_sql: unknown, _expected: unknown, live: () => void) => { counters.role++; live() } } }) + mock.module("./bandwidth-external-node-registry-role.js", { namedExports: { assertBandwidthExternalNodeRegistryRuntimeRole: async (_sql: unknown, _expected: unknown, live: () => void) => { counters.externalRole++; live(); if (!externalRoleAllowed) throw Error("external role denied") } } }) + // Preserve the original fake metadata boundary while production obtains its + // same strict component observations in one SQL snapshot. + mock.module("./bandwidth-peer-identity.js", { namedExports: { assertBandwidthPeerIdentity: async (sql: { query(text: string): Promise<{ rows: Record[] }> }, expected: Record, live: () => void) => { + counters.role++; counters.externalRole++; counters.combined++; live() + if (!extension || !externalRoleAllowed || expected.registryCatalogSha256 !== catalog) throw Error("identity refused") + const rows = (await sql.query("SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status FROM mmapp_control.bandwidth_node_authority WHERE id=1")).rows + live() + if (rows.length !== 1 || rows[0].installation_id !== expected.installationId || rows[0].issuer_key_sha256 !== expected.issuerKeySha256 || rows[0].quiescence_proof !== expected.quiescenceProof || rows[0].status !== "active") throw Error("authority refused") + } } }) + mock.module("./bandwidth-node-identity.js", { namedExports: { assertBandwidthNodeIdentity: async (sql: {query(text:string):Promise<{rows:Record[]}>}, expected:Record, live:()=>void) => { + counters.role++; counters.managed++; live() + if(extension || expected.registryCatalogSha256!==catalog)throw Error("v1 identity refused") + const rows=(await sql.query("SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status FROM mmapp_control.bandwidth_node_authority WHERE id=1")).rows + live() + if(rows.length!==1||rows[0].installation_id!==expected.installationId||rows[0].issuer_key_sha256!==expected.issuerKeySha256||rows[0].quiescence_proof!==expected.quiescenceProof||rows[0].status!=="active")throw Error("v1 authority refused") + } } }) + const { createConfiguredBandwidthNodeSource } = await import("./bandwidth-node-source.js") + const directory = await mkdtemp(path.join(os.tmpdir(), "mm-bootstrap-v2-")), issuerKeyFile = path.join(directory, "issuer"), bootstrapFile = path.join(directory, "bootstrap") + const key = Buffer.alloc(32, 71), issuerKeySha256 = sha(key) + await writeFile(issuerKeyFile, key); await writeFile(bootstrapFile, JSON.stringify({ unitOnly: true })) + const root = { installationId: randomUUID(), issuerKeySha256, quiescenceProof: "b".repeat(64) } + let bootstrap = { ...root, format: "mmapp-bandwidth-node-bootstrap-v1" as "mmapp-bandwidth-node-bootstrap-v1" | "mmapp-bandwidth-node-bootstrap-v2", + registrySchemaSourceSha256: bandwidthNodeRegistrySchemaSourceSha256(), registryCatalogSha256: catalog, attestationRef: "unit-only explicit bootstrap operator reference" } + const sourceRecord = { installationId: root.installationId, controlGeneration: randomUUID(), sourceRecordSha256: "d".repeat(64), databaseName: "unit-control", runtimeRole: "unit-runtime", ownerRole: "unit-owner" } + const fence = { verifyNodeBootstrap: async () => ({ bootstrap: structuredClone(bootstrap), source: sourceRecord }), + query: async () => { drift?.(); return { rows: [{ installation_id: root.installationId, issuer_key_sha256: issuerKeySha256, quiescence_proof: root.quiescenceProof, status: "active" }] } } } + const core = { async connect(_scope: unknown, dispatch: (value: unknown) => Promise) { + counters.connections++ + return { query: async () => { await dispatch(fence); return { rows: [] } }, release: () => { counters.releases++ } } + } } + const source = createConfiguredBandwidthNodeSource(core as never, { issuerKeyFile, issuerKeySha256, bootstrapFile }) + const scope = () => ({ deadlineAt: Date.now() + 5000 }) + const external = { ...root, enrollmentId: randomUUID(), incarnation: randomUUID(), nodeId: randomUUID(), status: "active" } + const claim = [{ kind: "software-id", value: "UNIT-SOFTWARE-ID" }] + const expected = (purpose: string, value: unknown) => createHmac("sha256", key).update("mmapp-bandwidth-node-" + purpose + "-v1:" + canonicalBandwidthNodeValue(value)).digest("hex") + try { + let session = await source.open(scope()) + assert(counters.managed > 0); assert.equal(counters.combined, 0); assert.equal(counters.externalRole, 0) + assert.equal(await session.keyedProof("claim", claim), expected("claim", claim)); session.close() + session = await source.open(scope()); await assert.rejects(session.keyedProof("external-alias", external)); session.close() + extension = true; await assert.rejects(source.open(scope()), "v1 pin must refuse after extension") + bootstrap = { ...bootstrap, format: "mmapp-bandwidth-node-bootstrap-v2", registrySchemaSourceSha256: bandwidthPeerRegistrySchemaSourceSha256() } + session = await source.open(scope()); assert(counters.combined > 0); assert(counters.externalRole > 0) + const leaseCount = counters.connections + assert.equal(await session.keyedProof("external-alias", external), expected("external-alias", external)) + assert.equal(await session.keyedProof("claim", claim), expected("claim", claim)) + const node = { ...root, nodeId: external.nodeId, claimProof: sha("unit claim") } + assert.equal(await session.keyedProof("node", node), expected("node", node)) + const event = { ...root, eventId: randomUUID(), enrollmentId: external.enrollmentId, incarnation: external.incarnation, nodeId: external.nodeId, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: "f".repeat(64) } + assert.equal(await session.keyedProof("external-event", event), expected("external-event", event)) + for (const invalid of [{ ...external, serverId: 12 }, { ...external, enrollmentId: 12 }]) await assert.rejects(session.keyedProof("external-alias", invalid)) + await assert.rejects(session.keyedProof("external-event", { ...event, oldStatus: "active" })) + assert.equal(counters.connections, leaseCount, "proof guards use the same lease") + session.close() + externalRoleAllowed = false; await assert.rejects(source.open(scope())); externalRoleAllowed = true + session = await source.open(scope()); drift = () => { bootstrap.quiescenceProof = "e".repeat(64) } + await assert.rejects(session.keyedProof("claim", claim), "signed bootstrap cannot change after pin"); drift = undefined; session.close() + bootstrap.quiescenceProof = root.quiescenceProof + session = await source.open(scope()); const binding = await session.binding(); session.close() + const participants = [101, 102].map(serverId => ({ + evidence: { version: 1 as const, serverId, sourceProof: "a".repeat(64), authorityGeneration: "unit-original", routingEndpointEpoch: randomUUID(), + observedAt: Date.now(), expiresAt: Date.now() + 5000, claims: claim as { kind: "software-id"; value: string }[], physicalUniquenessVerified: false as const, cloneResistanceVerified: false as const }, + receipt: { serverId, canonicalNodeId: randomUUID(), incarnation: randomUUID(), status: "active" as const, outcome: "existing" as const }, + eventId: randomUUID(), + })) + const dispatchFence = { ...fence, async query(text: string, values?: unknown[]) { + if (text.includes("pg_try_advisory")) return { rows: [{ locked: true }] } + if (text.includes("FROM mmapp_control.bandwidth_node_authority")) return fence.query() + const p = participants.find(p => p.receipt.serverId === values?.[0]); assert(p) + const r = p.receipt, claimProof = expected("claim", p.evidence.claims) + if (text.includes("FROM mmapp_control.bandwidth_node_aliases")) return { rows: [{ server_id: r.serverId, incarnation: r.incarnation, node_id: r.canonicalNodeId, status: "active", claim_proof: claimProof, + record_mac: expected("alias", { ...root, serverId: r.serverId, incarnation: r.incarnation, nodeId: r.canonicalNodeId, status: "active" }), + enrollment_mac: expected("node", { ...root, nodeId: r.canonicalNodeId, claimProof }) }] } + assert(text.includes("FROM mmapp_control.bandwidth_node_events")) + const event = { ...root, eventId: p.eventId, serverId: r.serverId, incarnation: r.incarnation, nodeId: r.canonicalNodeId, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: "f".repeat(64) } + return { rows: [{ event_id: p.eventId, incarnation: r.incarnation, node_id: r.canonicalNodeId, kind: "enroll", old_status: null, new_status: "active", authority_proof: event.authorityProof, event_mac: expected("event", event) }] } + } } + const beforeFenceConnections = counters.connections, beforeFenceExternal = counters.externalRole + const resolved = participants.map(({ evidence, receipt }) => ({ evidence, receipt })) + await source.assertResolvedOnFence(dispatchFence as never, { binding, participants: resolved }, scope()) + assert.equal(counters.connections, beforeFenceConnections, "dispatch fence never borrows another lease") + assert(counters.externalRole > beforeFenceExternal, "v2 dispatch fence authenticates external runtime role") + externalRoleAllowed = false + await assert.rejects(source.assertResolvedOnFence(dispatchFence as never, { binding, participants: resolved }, scope())); externalRoleAllowed = true + assert(counters.releases > 0) + } finally { source.close(); mock.restoreAll(); await rm(directory, { recursive: true, force: true }) } +}) diff --git a/backend/src/services/bandwidth-node-bootstrap.ts b/backend/src/services/bandwidth-node-bootstrap.ts new file mode 100644 index 00000000..2b1a35f1 --- /dev/null +++ b/backend/src/services/bandwidth-node-bootstrap.ts @@ -0,0 +1,26 @@ +/** Fixed signed operator statement, never a model readiness flag. The separate + * current actor/enrollment grant and global native-writer barrier remain + * mandatory; this statement does not grant network write authority. */ +import {createHash} from "node:crypto" +import {z} from "zod" +import {BANDWIDTH_NODE_REGISTRY_DDL} from "./bandwidth-node-registry-schema.js" +import {BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL} from "./bandwidth-external-node-registry-schema.js" +const hash=z.string().regex(/^[a-f0-9]{64}$/) +export const bandwidthNodeBootstrapSchema=z.object({ + format:z.enum(["mmapp-bandwidth-node-bootstrap-v1","mmapp-bandwidth-node-bootstrap-v2"]),installationId:z.string().uuid(), + issuerKeySha256:hash,quiescenceProof:hash,registrySchemaSourceSha256:hash,registryCatalogSha256:hash, + attestationRef:z.string().trim().min(30).max(2000), +}).strict() +export type BandwidthNodeBootstrap=z.infer +export const bandwidthNodeBootstrapEnvelopeSchema=z.object({ + format:z.literal("mmapp-bandwidth-node-bootstrap-envelope-v1"), + payload:z.string().min(1).max(24000).regex(/^[A-Za-z0-9_-]+$/),signature:z.string().length(86).regex(/^[A-Za-z0-9_-]+$/), +}).strict() +export function canonicalBandwidthNodeValue(value:unknown):string{ + if(Array.isArray(value))return "["+value.map(canonicalBandwidthNodeValue).join(",")+"]" + if(value&&typeof value==="object")return "{"+Object.entries(value).sort(([a],[b])=>ab?1:0).map(([key,item])=>JSON.stringify(key)+":"+canonicalBandwidthNodeValue(item)).join(",")+"}" + return JSON.stringify(value) +} +export function canonicalBandwidthNodeBootstrap(value:unknown):string{return canonicalBandwidthNodeValue(bandwidthNodeBootstrapSchema.parse(value))} +export function bandwidthNodeRegistrySchemaSourceSha256():string{return createHash("sha256").update(BANDWIDTH_NODE_REGISTRY_DDL).digest("hex")} +export function bandwidthPeerRegistrySchemaSourceSha256():string{return createHash("sha256").update(BANDWIDTH_NODE_REGISTRY_DDL+"\0"+BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL).digest("hex")} diff --git a/backend/src/services/bandwidth-node-fence.ts b/backend/src/services/bandwidth-node-fence.ts new file mode 100644 index 00000000..f1c5cb2d --- /dev/null +++ b/backend/src/services/bandwidth-node-fence.ts @@ -0,0 +1,110 @@ +/** Read-only canonical node verification on an EXISTING ephemeral core fence. + * Never checks out another client or enrolls a node. Server composition only. */ +import {createHash,createHmac} from "node:crypto" +import {performance} from "node:perf_hooks" +import {z} from "zod" +import {readBandwidthControlFile,type BandwidthControlDispatchFence} from "./bandwidth-control-source.js" +import {canonicalBandwidthNodeValue} from "./bandwidth-node-bootstrap.js" +import type {BandwidthNodeSourceConfig} from "./bandwidth-node-source.js" +import type {BandwidthNodeObservationEvidence} from "./bandwidth-node-observation.js" +import type {BandwidthNodeRegistryReceipt} from "./bandwidth-node-registry.js" +import type {BandwidthScope} from "./bandwidth-admission.js" + +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid(),id=z.number().int().positive().safe() +export const bandwidthNodeBindingSchema=z.object({installationId:uuid,issuerKeySha256:hash,quiescenceProof:hash, + bootstrapSha256:hash,controlGeneration:uuid,sourceRecordSha256:hash}).strict() +export type BandwidthNodeBinding=z.infer +export interface BandwidthResolvedNodeFence { + binding:Readonly + participants:readonly {evidence:Readonly;receipt:Readonly}[] +} +const claim=z.object({kind:z.enum(["system-id","software-id"]),value:z.string().min(1).max(256)}).strict() +const evidence=z.object({version:z.literal(1),serverId:id,sourceProof:hash,authorityGeneration:z.string().min(1).max(200), + routingEndpointEpoch:uuid,observedAt:z.number().int().nonnegative(),expiresAt:z.number().int().positive(), + claims:z.array(claim).min(1).max(2).refine(v=>v.length===1||v[0].kind==="system-id"&&v[1].kind==="software-id"), + physicalUniquenessVerified:z.literal(false),cloneResistanceVerified:z.literal(false)}).strict() +const receipt=z.object({serverId:id,canonicalNodeId:uuid,incarnation:uuid,status:z.literal("active"),outcome:z.enum(["new","existing"])}).strict() +const expectedSchema=z.object({binding:bandwidthNodeBindingSchema,participants:z.array(z.object({evidence,receipt}).strict()).length(2)}).strict() +const digest=(value:Buffer|string)=>createHash("sha256").update(value).digest("hex") +export class BandwidthNodeFenceError extends Error{readonly code="BANDWIDTH_NODE_FENCE_REFUSED";constructor(){super("Актуальная постоянная привязка bandwidth-узлов не подтверждена")}} +const fail=():never=>{throw new BandwidthNodeFenceError()} + +export async function assertResolvedBandwidthNodesOnFence(config:Readonly,sessions:Set<()=>void>,closed:()=>boolean, + fence:BandwidthControlDispatchFence,input:BandwidthResolvedNodeFence,original:BandwidthScope):Promise{ + const expected=expectedSchema.parse(input) + if(!original||!Number.isSafeInteger(original.deadlineAt)||original.signal!==undefined&&!(original.signal instanceof AbortSignal))fail() + const scope=Object.freeze({deadlineAt:Math.min(original.deadlineAt,...expected.participants.map(p=>p.evidence.expiresAt)),signal:original.signal}) + const end=performance.now()+Math.max(0,Math.min(10000,scope.deadlineAt-Date.now())) + let active=true,rejectStopped!:(error:Error)=>void + const keys=new Set() + const wipe=(key:Buffer)=>{key.fill(0);keys.delete(key)} + const live=()=>{if(!active||closed()||scope.signal?.aborted||Date.now()>=scope.deadlineAt||performance.now()>=end)fail()} + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject}) + const stop=()=>{active=false;for(const key of keys)key.fill(0);keys.clear();rejectStopped(new BandwidthNodeFenceError())} + const timer=setTimeout(stop,Math.max(1,Math.min(scope.deadlineAt-Date.now(),end-performance.now()))) + sessions.add(stop);scope.signal?.addEventListener("abort",stop,{once:true}) + const root={installationId:expected.binding.installationId,issuerKeySha256:expected.binding.issuerKeySha256,quiescenceProof:expected.binding.quiescenceProof} + async function material(){ + live() + const bytes=await readBandwidthControlFile(config.bootstrapFile,32768,live) + const verified=await fence.verifyNodeBootstrap(JSON.parse(new TextDecoder("utf-8",{fatal:true}).decode(bytes)));live() + const actual={installationId:verified.bootstrap.installationId,issuerKeySha256:verified.bootstrap.issuerKeySha256,quiescenceProof:verified.bootstrap.quiescenceProof, + bootstrapSha256:digest(canonicalBandwidthNodeValue(verified.bootstrap)),controlGeneration:verified.source.controlGeneration,sourceRecordSha256:verified.source.sourceRecordSha256} + if(canonicalBandwidthNodeValue(actual)!==canonicalBandwidthNodeValue(expected.binding)||config.issuerKeySha256!==actual.issuerKeySha256)fail() + let key:Buffer|undefined + try{ + key=await readBandwidthControlFile(config.issuerKeyFile,32,live);keys.add(key);live() + if(key.length!==32||digest(key)!==config.issuerKeySha256)fail() + return {verified,key} + }catch(error){if(key)wipe(key);throw error} + } + const sql={query:async(text:string,values?:unknown[])=>{live();const result=await fence.query(text,values);live();return result}} + try{await Promise.race([Promise.resolve().then(async()=>{ + // The registry's fixed mutators take the exclusive form of this key. + // Inside a journal transaction this shared lock lasts through COMMIT; + // an implicit read-only statement alone is not a native-send barrier. + const lock=(await sql.query("SELECT pg_catalog.pg_try_advisory_xact_lock_shared(8723116) AS locked")).rows + if(lock.length!==1||lock[0].locked!==true)fail() + const before=await material() + try{ + const v=before.verified + const v2=v.bootstrap.format==="mmapp-bandwidth-node-bootstrap-v2" + const role={databaseName:v.source.databaseName,runtimeRole:v.source.runtimeRole,ownerRole:v.source.ownerRole} + if(v2){ + const {assertBandwidthPeerIdentity}=await import("./bandwidth-peer-identity.js") + live();await assertBandwidthPeerIdentity(sql,{...role,...root,registryCatalogSha256:v.bootstrap.registryCatalogSha256},live) + }else{ + const {assertBandwidthNodeIdentity}=await import("./bandwidth-node-identity.js") + live();await assertBandwidthNodeIdentity(sql,{...role,...root,registryCatalogSha256:v.bootstrap.registryCatalogSha256},live) + } + const pending=[] + for(const participant of expected.participants){ + if(participant.evidence.serverId!==participant.receipt.serverId)fail() + const rows=(await sql.query("SELECT a.server_id,a.incarnation::pg_catalog.text,a.node_id::pg_catalog.text,a.status,a.record_mac,n.claim_proof,n.enrollment_mac FROM mmapp_control.bandwidth_node_aliases a JOIN mmapp_control.bandwidth_nodes n ON n.node_id=a.node_id WHERE a.server_id=$1",[participant.receipt.serverId])).rows + if(rows.length!==1)fail() + const row=rows[0],r=participant.receipt + if(Number(row.server_id)!==r.serverId||row.incarnation!==r.incarnation||row.node_id!==r.canonicalNodeId||row.status!=="active"||!hash.safeParse(row.claim_proof).success)fail() + const events=(await sql.query("SELECT event_id::pg_catalog.text,incarnation::pg_catalog.text,node_id::pg_catalog.text,kind,old_status,new_status,authority_proof,event_mac FROM mmapp_control.bandwidth_node_events WHERE server_id=$1 ORDER BY kind LIMIT 3",[r.serverId])).rows + if(events.length!==1)fail() + const event=events[0] + if(!uuid.safeParse(event.event_id).success||event.incarnation!==r.incarnation||event.node_id!==r.canonicalNodeId||event.kind!=="enroll"||event.old_status!==null||event.new_status!=="active"||!hash.safeParse(event.authority_proof).success)fail() + pending.push({participant,row,event}) + } + // Re-read the signed source/key after ALL awaited catalog/row observations. + const after=await material() + try{ + const proof=(purpose:string,value:unknown)=>createHmac("sha256",after.key).update("mmapp-bandwidth-node-"+purpose+"-v1:"+canonicalBandwidthNodeValue(value)).digest("hex") + for(const {participant,row,event} of pending){ + const r=participant.receipt + if(row.claim_proof!==proof("claim",participant.evidence.claims) + ||row.enrollment_mac!==proof("node",{...root,nodeId:r.canonicalNodeId,claimProof:row.claim_proof}) + ||row.record_mac!==proof("alias",{...root,serverId:r.serverId,incarnation:r.incarnation,nodeId:r.canonicalNodeId,status:"active"}) + ||event.event_mac!==proof("event",{...root,eventId:event.event_id,serverId:r.serverId,incarnation:r.incarnation,nodeId:r.canonicalNodeId,kind:"enroll",oldStatus:null,newStatus:"active",authorityProof:event.authority_proof}))fail() + } + live() + }finally{wipe(after.key)} + }finally{wipe(before.key)} + }),stopped])} + catch{throw new BandwidthNodeFenceError()} + finally{active=false;clearTimeout(timer);scope.signal?.removeEventListener("abort",stop);sessions.delete(stop);for(const key of keys)key.fill(0);keys.clear()} +} diff --git a/backend/src/services/bandwidth-node-identity.test.ts b/backend/src/services/bandwidth-node-identity.test.ts new file mode 100644 index 00000000..47176b52 --- /dev/null +++ b/backend/src/services/bandwidth-node-identity.test.ts @@ -0,0 +1,116 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_NODE_REGISTRY_DDL } from "./bandwidth-node-registry-schema.js" +import { validateBandwidthNodeRegistryCatalogRows } from "./bandwidth-node-registry-catalog.js" +import { assertBandwidthNodeIdentity, validateBandwidthNodeIdentityRows, BANDWIDTH_NODE_IDENTITY_SQL, BandwidthNodeIdentityRefused, type BandwidthNodeIdentityExpected } from "./bandwidth-node-identity.js" +type Row=Record +type Item={section:string;raw_json:string|null} +type Batch={kind:string;row_order:string;raw_json:string|null} +function catalogFixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_node_authority","bandwidth_nodes","bandwidth_node_aliases","bandwidth_node_events"] + const functions=["bandwidth_node_authority_guard","bandwidth_node_immutable_guard","bandwidth_node_alias_guard","bandwidth_node_event_guard","bandwidth_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_node_authority:[["p",["id"]]],bandwidth_nodes:[["p",["node_id"]],["u",["claim_proof"]]],bandwidth_node_aliases:[["p",["server_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_node_events:[["p",["event_id"]],["u",["server_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_node_aliases","node_id","bandwidth_nodes"],["bandwidth_node_events","server_id","bandwidth_node_aliases"],["bandwidth_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +function fixture(){ + const catalog=catalogFixture(),expected:BandwidthNodeIdentityExpected={databaseName:"v1_control",runtimeRole:"v1_runtime",ownerRole:"fixed_owner",installationId:"11111111-1111-4111-8111-111111111111",issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64),registryCatalogSha256:validateBandwidthNodeRegistryCatalogRows(catalog)} + const role={observation:{databaseName:expected.databaseName,sessionRole:expected.runtimeRole,currentRole:expected.runtimeRole,runtimeRoleExists:true,ownerRoleExists:true,runtimeFlagsSafe:true,ownerMembership:false,dangerousMembership:false,schemaExists:true,schemaOwner:expected.ownerRole,schemaUsage:true,schemaCreate:false,publicSchemaUsage:false,publicSchemaCreate:false,tables:["bandwidth_node_aliases","bandwidth_nodes","bandwidth_node_events","bandwidth_node_authority"].map(name=>({name,exists:true,ownerRole:expected.ownerRole,ownedByRuntime:false,privilegesExact:true}))}} + const authority={installation_id:expected.installationId,issuer_key_sha256:expected.issuerKeySha256,quiescence_proof:expected.quiescenceProof,status:"active"} + const groups:Record={role:[role],catalog,authority:[authority]},rows:Batch[]=Object.entries(groups).flatMap(([kind,values])=>values.map(value=>({kind,row_order:"",raw_json:JSON.stringify(value)}))) + ordinal(rows);return{rows,expected,catalog} +} +function ordinal(rows:Batch[]){rows.forEach((row,index)=>{row.row_order=String(index+1)});return rows} +const refused=(error:unknown)=>error instanceof BandwidthNodeIdentityRefused +const check=(f:ReturnType)=>validateBandwidthNodeIdentityRows(f.rows,f.expected) +function change(f:ReturnType,kind:string,mutate:(row:Row)=>void){const item=f.rows.find(row=>row.kind===kind)!;const value=JSON.parse(item.raw_json!) as Row;mutate(value);item.raw_json=JSON.stringify(value)} +function catalogChange(f:ReturnType,section:string,mutate:(row:Row)=>void){const item=f.rows.find(row=>row.kind==="catalog"&&(JSON.parse(row.raw_json!) as Item).section===section)!;const value=JSON.parse(item.raw_json!) as Item,row=JSON.parse(value.raw_json!) as Row;mutate(row);value.raw_json=JSON.stringify(row);item.raw_json=JSON.stringify(value)} + +test("exact original role/catalog/authority share ONE fixed materialized SELECT and retain before/after/final live checks",async()=>{ + const f=fixture(),events:string[]=[];let calls=0 + await assertBandwidthNodeIdentity({async query(text,values){calls++;events.push("SQL");assert.equal(text,BANDWIDTH_NODE_IDENTITY_SQL);assert.deepEqual(values,[f.expected.databaseName,f.expected.runtimeRole,f.expected.ownerRole]);return{rows:f.rows}}},f.expected,()=>events.push("live")) + assert.equal(calls,1);assert.deepEqual(events,["live","SQL","live","live"]) + for(const name of ["node_role","node_catalog","authority"])assert(BANDWIDTH_NODE_IDENTITY_SQL.includes(name+" AS MATERIALIZED")) + assert(BANDWIDTH_NODE_IDENTITY_SQL.includes("ORDER BY c.relname,t.tgname LIMIT 23"));assert(!BANDWIDTH_NODE_IDENTITY_SQL.includes("ORDER BY c.relname,t.tgname LIMIT 27"));assert(!BANDWIDTH_NODE_IDENTITY_SQL.includes("bandwidth_external_node"));assert(!/INSERT INTO|CREATE TABLE|CREATE FUNCTION/.test(BANDWIDTH_NODE_IDENTITY_SQL)) +}) +test("legacy signed digest is exactly unchanged; transport order/key order do not alter meaning",()=>{ + const f=fixture();assert.equal(validateBandwidthNodeRegistryCatalogRows(f.catalog),f.expected.registryCatalogSha256);f.rows.reverse();for(const item of f.rows){const row=JSON.parse(item.raw_json!) as Row;item.raw_json=JSON.stringify(Object.fromEntries(Object.entries(row).reverse()))}check(f) +}) +test("missing/duplicate role, catalog or authority cannot substitute a partial snapshot",()=>{ + for(const kind of ["role","catalog","authority"]){const f=fixture();f.rows=ordinal(f.rows.filter(row=>row.kind!==kind));assert.throws(()=>check(f),refused);const duplicate=fixture();duplicate.rows.push({...duplicate.rows.find(row=>row.kind===kind)!});ordinal(duplicate.rows);assert.throws(()=>check(duplicate),refused)} +}) +test("outer tags/shape/ordinals, NULL overflow and malformed nested JSON fail closed",()=>{ + for(const mutate of [(rows:Batch[])=>{rows[0].kind="external-role"},(rows:Batch[])=>{rows[0].raw_json=null},(rows:Batch[])=>{rows[0].raw_json="{"},(rows:Batch[])=>{rows[0].raw_json="[]"},(rows:Batch[])=>{rows[0].row_order=rows[1].row_order},(rows:Batch[])=>{rows[0].row_order="0"},(rows:Batch[])=>{rows[0].row_order="01"},(rows:Batch[])=>{rows[0].row_order="999"}]){const f=fixture();mutate(f.rows);assert.throws(()=>check(f),refused)} + const f=fixture();catalogChange(f,"functions",row=>{delete row.body});assert.throws(()=>check(f),refused) +}) +test("strict row/per-row/total/component byte bounds preserve the old tighter catalog budget",()=>{ + const f=fixture(),row={kind:"catalog",row_order:"1",raw_json:JSON.stringify({padding:"x".repeat(131180)})} + assert.throws(()=>validateBandwidthNodeIdentityRows(ordinal(Array.from({length:233},()=>({...row}))),f.expected),refused) + assert.throws(()=>validateBandwidthNodeIdentityRows([{...row,raw_json:JSON.stringify({padding:"x".repeat(131201)})}],f.expected),refused) + assert.throws(()=>validateBandwidthNodeIdentityRows(ordinal(Array.from({length:33},()=>({...row}))),f.expected),refused) + assert.throws(()=>validateBandwidthNodeIdentityRows(ordinal(Array.from({length:231},()=>({...row,raw_json:"{}"}))),f.expected),refused) +}) +test("v1 still requires EXACT22 triggers; an external incoming FK cannot be blessed by the new observer",()=>{ + const f=fixture(),existing=f.rows.find(row=>row.kind==="catalog"&&(JSON.parse(row.raw_json!) as Item).section==="triggers")!,extra=structuredClone(existing),item=JSON.parse(extra.raw_json!) as Item,trigger=JSON.parse(item.raw_json!) as Row + trigger.table_name="bandwidth_nodes";trigger.constraint_name="bandwidth_external_node_aliases_node_id_fkey";trigger.name=null;trigger.internal=true;item.raw_json=JSON.stringify(trigger);extra.raw_json=JSON.stringify(item);f.rows.push(extra);ordinal(f.rows);assert.throws(()=>check(f),refused) +}) +test("old role privileges/membership/schema ownership, literal guard and signed expression pin checks remain exact",()=>{ + for(const mutate of [(row:Row)=>{(row.observation as Row).dangerousMembership=true},(row:Row)=>{(row.observation as Row).runtimeFlagsSafe=false},(row:Row)=>{(row.observation as Row).publicSchemaCreate=true},(row:Row)=>{((row.observation as Row).tables as Row[])[0].privilegesExact=false}]){const f=fixture();change(f,"role",mutate);assert.throws(()=>check(f),refused)} + for(const [section,mutate]of [["functions",(row:Row)=>{row.body="BEGIN RETURN NEW; END;"}],["triggers",(row:Row)=>{row.enabled="O"}],["constraints",(row:Row)=>{row.definition="CHECK(true)"}],["indexes",(row:Row)=>{row.predicate="false"}]] as const){const f=fixture();catalogChange(f,section,mutate);assert.throws(()=>check(f),refused)} +}) +test("active authority exact installation/issuer/quiescence is required alongside matching legacy catalog",()=>{ + for(const mutate of [(row:Row)=>{row.installation_id="22222222-2222-4222-8222-222222222222"},(row:Row)=>{row.issuer_key_sha256="f".repeat(64)},(row:Row)=>{row.quiescence_proof="f".repeat(64)},(row:Row)=>{row.status="quarantined"},(row:Row)=>{row.ready=true}]){const f=fixture();change(f,"authority",mutate);assert.throws(()=>check(f),refused)} +}) +test("original expected scalar pins are copied beforeawait; input getters/symbols cannot provide a later authority",async()=>{ + const f=fixture(),original={...f.expected} + await assertBandwidthNodeIdentity({async query(_text,values){assert.deepEqual(values,[original.databaseName,original.runtimeRole,original.ownerRole]);f.expected.registryCatalogSha256="f".repeat(64);f.expected.issuerKeySha256="f".repeat(64);return{rows:f.rows}}},f.expected,()=>{}) + const g=fixture();let getters=0,calls=0;Object.defineProperty(g.expected,"issuerKeySha256",{enumerable:true,get(){getters++;return"a".repeat(64)}}) + await assert.rejects(assertBandwidthNodeIdentity({async query(){calls++;return{rows:g.rows}}},g.expected,()=>{}),refused);assert.equal(getters,0);assert.equal(calls,0) + const h=fixture();Object.defineProperty(h.expected,Symbol("proof"),{value:true});assert.throws(()=>check(h),refused) +}) +test("late scope/original grant revocation and SQL failure preserve final live boundary without endpoint/key disclosure",async()=>{ + const f=fixture(),expired=Error("original grant stopped");let live=0 + await assert.rejects(assertBandwidthNodeIdentity({async query(){return{rows:f.rows}}},f.expected,()=>{if(++live===2)throw expired}),error=>error===expired);assert.equal(live,2) + const events:string[]=[];await assert.rejects(assertBandwidthNodeIdentity({async query(){events.push("SQL");throw Error("private endpoint/password/key")}},f.expected,()=>events.push("live")),error=>refused(error)&&!String(error).includes("password"));assert.deepEqual(events,["live","SQL","live"]) + let calls=0;await assert.rejects(assertBandwidthNodeIdentity({async query(){calls++;return{rows:f.rows}}},f.expected,()=>{throw expired}),error=>error===expired);assert.equal(calls,0) +}) diff --git a/backend/src/services/bandwidth-node-identity.ts b/backend/src/services/bandwidth-node-identity.ts new file mode 100644 index 00000000..33ae9a11 --- /dev/null +++ b/backend/src/services/bandwidth-node-identity.ts @@ -0,0 +1,81 @@ +/** One bounded MVCC snapshot of the ORIGINAL v1 registry identity. No new lease, + * supplied SQL, readiness assertion, cache, retry or native authority. The same + * pure original role/catalog validators and unchanged signed catalog digest. */ +import { z } from "zod" +import { BANDWIDTH_NODE_REGISTRY_ROLE_SQL, captureBandwidthNodeRegistryRoleExpected, validateBandwidthNodeRegistryRuntimeRoleRows } from "./bandwidth-node-registry-role.js" +import { BANDWIDTH_NODE_REGISTRY_CATALOG_SQL, validateBandwidthNodeRegistryCatalogRows } from "./bandwidth-node-registry-catalog.js" + +export interface BandwidthNodeIdentitySql { query(text:string,values?:unknown[]):Promise<{rows:Record[]}> } +const hash=z.string().regex(/^[a-f0-9]{64}$/) +const expectedSchema=z.object({databaseName:z.string(),runtimeRole:z.string(),ownerRole:z.string(),installationId:z.string().uuid(),issuerKeySha256:hash,quiescenceProof:hash,registryCatalogSha256:hash}).strict() +export type BandwidthNodeIdentityExpected=z.infer +export class BandwidthNodeIdentityRefused extends Error { + readonly code="BANDWIDTH_NODE_IDENTITY_REFUSED" + constructor(){super("Bandwidth v1 node registry identity refused")} +} +function fail():never{throw new BandwidthNodeIdentityRefused()} +const plain=(v:unknown):v is Record=>!!v&&typeof v==="object"&&Object.getPrototypeOf(v)===Object.prototype +const fields=["databaseName","runtimeRole","ownerRole","installationId","issuerKeySha256","quiescenceProof","registryCatalogSha256"] as const +/** Original scalar expectations are copied before the first asynchronous SQL; + * caller getters, serializers, symbols and later mutations cannot select pins. */ +function capture(expected:BandwidthNodeIdentityExpected):Readonly{ + if(!plain(expected)||Reflect.ownKeys(expected).length!==fields.length)fail() + const descriptors=Object.getOwnPropertyDescriptors(expected) + if(Object.keys(descriptors).sort().join(",")!==[...fields].sort().join(",")||Object.values(descriptors).some(field=>!("value"in field)||!field.enumerable))fail() + let parsed:BandwidthNodeIdentityExpected + try{parsed=expectedSchema.parse(Object.fromEntries(fields.map(name=>[name,descriptors[name].value])));captureBandwidthNodeRegistryRoleExpected({databaseName:parsed.databaseName,runtimeRole:parsed.runtimeRole,ownerRole:parsed.ownerRole})}catch{return fail()} + return Object.freeze(parsed) +} +type Kind="role"|"catalog"|"authority" +const limits:Record={role:1,catalog:230,authority:1} +/** Exact old role SQL, strict 22-trigger v1 catalog SQL and authority share one + * command snapshot. No new catalog, pin format, DDL, cache or source identity. */ +export const BANDWIDTH_NODE_IDENTITY_SQL=`/* bandwidth-node:identity */ WITH + node_role AS MATERIALIZED (${BANDWIDTH_NODE_REGISTRY_ROLE_SQL}), + node_catalog AS MATERIALIZED (${BANDWIDTH_NODE_REGISTRY_CATALOG_SQL}), + authority AS MATERIALIZED ( + SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status + FROM mmapp_control.bandwidth_node_authority WHERE id=1 LIMIT 2), + bundled AS ( + SELECT 'role'::pg_catalog.text AS kind,pg_catalog.to_jsonb(r)::pg_catalog.text AS raw_json FROM node_role r + UNION ALL SELECT 'catalog',pg_catalog.to_jsonb(r)::pg_catalog.text FROM node_catalog r + UNION ALL SELECT 'authority',pg_catalog.to_jsonb(r)::pg_catalog.text FROM authority r), + metered AS ( + SELECT kind,raw_json,pg_catalog.row_number() OVER(ORDER BY kind COLLATE "C",raw_json COLLATE "C") AS row_order, + pg_catalog.count(*) OVER() AS row_count,pg_catalog.sum(pg_catalog.octet_length(raw_json)) OVER() AS total_bytes + FROM bundled) + SELECT kind,row_order::pg_catalog.text AS row_order, + CASE WHEN row_count<=232 AND total_bytes<=4194304 AND pg_catalog.octet_length(raw_json)<=131200 THEN raw_json END AS raw_json + FROM metered ORDER BY metered.row_order LIMIT 233` + +/** Pure counterpart for captured rows. Component validators retain their own + * tighter structural/byte bounds and the original exact signed digest. */ +export function validateBandwidthNodeIdentityRows(rows:unknown,expected:BandwidthNodeIdentityExpected):void{ + const captured=capture(expected) + if(!Array.isArray(rows)||rows.length>232)fail() + const found:Record[]>={role:[],catalog:[],authority:[]} + const ordinals=new Set();let bytes=0 + for(const row of rows){ + if(!plain(row)||Object.keys(row).sort().join(",")!=="kind,raw_json,row_order"||typeof row.kind!=="string"||!Object.hasOwn(found,row.kind)||typeof row.raw_json!=="string"||typeof row.row_order!=="string"||!/^[1-9][0-9]{0,2}$/.test(row.row_order))fail() + const ordinal=Number(row.row_order);if(ordinal>rows.length||ordinals.has(ordinal))fail();ordinals.add(ordinal) + const size=Buffer.byteLength(row.raw_json);bytes+=size;if(size>131200||bytes>4194304)fail() + let value:unknown;try{value=JSON.parse(row.raw_json)}catch{return fail()};if(!plain(value))fail() + const kind=row.kind as Kind;found[kind].push(value);if(found[kind].length>limits[kind])fail() + } + try{ + const role={databaseName:captured.databaseName,runtimeRole:captured.runtimeRole,ownerRole:captured.ownerRole} + validateBandwidthNodeRegistryRuntimeRoleRows(found.role,role) + if(validateBandwidthNodeRegistryCatalogRows(found.catalog)!==captured.registryCatalogSha256)fail() + if(found.authority.length!==1)fail() + const authority=found.authority[0] + if(Object.keys(authority).sort().join(",")!=="installation_id,issuer_key_sha256,quiescence_proof,status"||authority.installation_id!==captured.installationId||authority.issuer_key_sha256!==captured.issuerKeySha256||authority.quiescence_proof!==captured.quiescenceProof||authority.status!=="active")fail() + }catch{return fail()} +} + +export async function assertBandwidthNodeIdentity(sql:BandwidthNodeIdentitySql,expected:BandwidthNodeIdentityExpected,live:()=>void):Promise{ + const captured=capture(expected) + live();let result:{rows:Record[]} + try{result=await sql.query(BANDWIDTH_NODE_IDENTITY_SQL,[captured.databaseName,captured.runtimeRole,captured.ownerRole])}catch{live();return fail()} + live();validateBandwidthNodeIdentityRows(result?.rows,captured);live() +} + diff --git a/backend/src/services/bandwidth-node-key-effect.test.ts b/backend/src/services/bandwidth-node-key-effect.test.ts new file mode 100644 index 00000000..6f1b26dc --- /dev/null +++ b/backend/src/services/bandwidth-node-key-effect.test.ts @@ -0,0 +1,146 @@ +/** Real local file and crypto effects. Wrapped fs observers inject races; + * neither Q/PG nor a full operating window is claimed by these unit tests. */ +import assert from "node:assert/strict" +import * as fs from "node:fs" +import {mkdtemp,writeFile,rm,rename,mkdir,symlink} from "node:fs/promises" +import {createHash,createHmac,randomUUID} from "node:crypto" +import {tmpdir} from "node:os" +import path from "node:path" +import {mock,test} from "node:test" +import {performance} from "node:perf_hooks" +import {canonicalBandwidthNodeValue,type BandwidthNodeBootstrap} from "./bandwidth-node-bootstrap.js" +import type {BandwidthNodeProofInput} from "./bandwidth-node-proof-input.js" + +test("fixed synchronous local key effect",async t=>{ + let hook:((phase:string)=>void)|undefined,reads=0,opens=0,closes=0,syntheticLink=false,shortRead=false,closeFault=false + const buffers:Buffer[]=[] + mock.module("node:fs",{namedExports:{...fs, + lstatSync:(...args:Parameters)=>{const value=fs.lstatSync(...args);return value&&syntheticLink&&String(args[0]).endsWith("issuer")?Object.assign(Object.create(value),{isSymbolicLink:()=>true}):value}, + openSync:(...args:Parameters)=>{opens++;const result=fs.openSync(...args);hook?.("open");return result}, + readSync:(fd:number,buffer:Buffer,offset:number,length:number,position:number)=>{ + reads++;buffers.push(buffer);hook?.("before-read") + const result=fs.readSync(fd,buffer,offset,shortRead?Math.min(7,length):length,position);hook?.("after-read");return result + }, + closeSync:(fd:number)=>{closes++;fs.closeSync(fd);hook?.("close");if(closeFault)throw Error("synthetic close failure")}, + }}) + const {createBandwidthNodeKeyEffect,BandwidthNodeKeyEffectRefused}=await import("./bandwidth-node-key-effect.js") + async function fixture(v2=true){ + const base=await mkdtemp(path.join(tmpdir(),"mm-fixed-key-")),directory=path.join(base,"keys") + await mkdir(directory);const keyFile=path.join(directory,"issuer"),key=Buffer.alloc(32,81) + await writeFile(keyFile,key) + const bootstrap:BandwidthNodeBootstrap={format:v2?"mmapp-bandwidth-node-bootstrap-v2":"mmapp-bandwidth-node-bootstrap-v1",installationId:randomUUID(),issuerKeySha256:createHash("sha256").update(key).digest("hex"),quiescenceProof:"b".repeat(64),registrySchemaSourceSha256:"c".repeat(64),registryCatalogSha256:"d".repeat(64),attestationRef:"Explicit synthetic bootstrap for local key unit acceptance"} + const actor=new AbortController(),owner=new AbortController(),deadlineAt=Date.now()+5000 + const config={keyFile,bootstrap,deadlineAt,signal:actor.signal,ownerSignal:owner.signal} + const inputs:BandwidthNodeProofInput[]=[{purpose:"claim",value:[{kind:"system-id",value:"synthetic-router"}]}] + const expected=(items:readonly BandwidthNodeProofInput[])=>items.map(({purpose,value})=>createHmac("sha256",key).update("mmapp-bandwidth-node-"+purpose+"-v1:"+canonicalBandwidthNodeValue(value)).digest("hex")) + const start=buffers.length + return {base,directory,keyFile,key,bootstrap,actor,owner,config,inputs,expected, + create:()=>createBandwidthNodeKeyEffect(config), + async dispose(){hook=undefined;syntheticLink=false;shortRead=false;closeFault=false;for(const buffer of buffers.slice(start))assert(buffer.every(byte=>byte===0),"all actual scratch keys wiped");await rm(base,{recursive:true,force:true})}, + } + } + await t.test("construction is inert; claim and both node versions preserve domains",async()=>{ + for(const v2 of [false,true]){const f=await fixture(v2);try{ + const before={reads,opens,closes},effect=f.create();assert.deepEqual({reads,opens,closes},before) + const root={installationId:f.bootstrap.installationId,issuerKeySha256:f.bootstrap.issuerKeySha256,quiescenceProof:f.bootstrap.quiescenceProof},nodeId=randomUUID(),incarnation=randomUUID() + const subject=v2?{enrollmentId:randomUUID()}:{serverId:71} + const items:BandwidthNodeProofInput[]=[{purpose:"node",value:{...root,nodeId,claimProof:"e".repeat(64)}},{purpose:v2?"external-alias":"alias",value:{...root,...subject,nodeId,incarnation,status:"active"}},{purpose:v2?"external-event":"event",value:{...root,...subject,nodeId,incarnation,eventId:randomUUID(),kind:"enroll",oldStatus:null,newStatus:"active",authorityProof:"f".repeat(64)}}] + assert.deepEqual(effect.calculate(f.inputs),f.expected(f.inputs));const result=effect.calculate(items) + assert.deepEqual(result,f.expected(items));assert(Object.isFrozen(result));assert.equal(reads-before.reads,2);assert.equal(opens-before.opens,2);assert.equal(closes-before.closes,2) + assert.equal(typeof (result as unknown as {then?:unknown}).then,"undefined","no asynchronous result or key handle") + }finally{await f.dispose()}} + }) + for(const fault of ["wrong-hash","short","long","missing","directory"]){await t.test(`invalid key ${fault} refuses and poisons effect`,async()=>{ + const f=await fixture();try{const effect=f.create() + if(fault==="missing")await rm(f.keyFile) + else if(fault==="directory"){await rm(f.keyFile);await mkdir(f.keyFile)} + else await writeFile(f.keyFile,Buffer.alloc(fault==="short"?31:fault==="long"?33:32,82)) + assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused) + if(fault==="directory")await rm(f.keyFile,{recursive:true}) + await writeFile(f.keyFile,f.key);const n=opens;assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused);assert.equal(opens,n) + }finally{await f.dispose()} + })} + for(const phase of ["open","before-read","after-read","close"]){for(const who of ["actor","owner"] as const){await t.test(`${who} revocation during ${phase} suppresses MAC`,async()=>{ + const f=await fixture();try{const effect=f.create();hook=p=>{if(p===phase)f[who].abort()};assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused) + }finally{await f.dispose()} + })}} + for(const phase of ["before-read","after-read","close"]){await t.test(`late synchronous ${phase} return cannot publish MAC`,async()=>{ + const f=await fixture(),now=Date.now;try{const effect=f.create();hook=p=>{if(p===phase)Date.now=()=>f.config.deadlineAt+1};assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused) + }finally{Date.now=now;await f.dispose()} + })} + await t.test("key replacement after read refuses before publication",async()=>{ + const f=await fixture();try{const effect=f.create();hook=p=>{if(p==="after-read"){fs.renameSync(f.keyFile,f.keyFile+".old");fs.writeFileSync(f.keyFile,f.key)}};assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused) + }finally{await f.dispose()} + }) + await t.test("same-byte directory clone between phases refuses",async()=>{ + const f=await fixture();try{const effect=f.create();effect.calculate(f.inputs);await rename(f.directory,f.directory+"-old");await mkdir(f.directory);await writeFile(f.keyFile,f.key);assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused) + }finally{await f.dispose()} + }) + await t.test("key symlink refuses before opening target (synthetic stat fallback on Windows)",async()=>{ + const f=await fixture();try{const target=path.join(f.base,"target");await writeFile(target,f.key);await rm(f.keyFile) + try{await symlink(target,f.keyFile)}catch(e){if((e as NodeJS.ErrnoException).code!=="EPERM")throw e;await writeFile(f.keyFile,f.key);syntheticLink=true} + const effect=f.create(),n=opens;assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused);assert.equal(opens,n) + }finally{await f.dispose()} + }) + await t.test("invalid/callback/proxy input never opens key or invokes caller",async()=>{ + const f=await fixture();try{let calls=0 + const bad=[[],Array(1),[{purpose:"claim",get value(){calls++;return []}}],new Proxy([],{ownKeys(){calls++;return []}}),[{purpose:"unknown",value:[]}],Array(5).fill(f.inputs[0])] + for(const item of bad){const effect=f.create(),n=opens;assert.throws(()=>effect.calculate(item as BandwidthNodeProofInput[]),BandwidthNodeKeyEffectRefused);assert.equal(opens,n)}assert.equal(calls,0) + }finally{await f.dispose()} + }) + await t.test("foreign root and v1 external proofs refuse before key IO",async()=>{ + const f=await fixture(false);try{ + const root={installationId:f.bootstrap.installationId,issuerKeySha256:f.bootstrap.issuerKeySha256,quiescenceProof:f.bootstrap.quiescenceProof} + const cases=[{purpose:"node",value:{...root,installationId:randomUUID(),nodeId:randomUUID(),claimProof:"e".repeat(64)}},{purpose:"external-alias",value:{...root,enrollmentId:randomUUID(),incarnation:randomUUID(),nodeId:randomUUID(),status:"active"}}] + for(const item of cases){const effect=f.create(),n=opens;assert.throws(()=>effect.calculate([item]),BandwidthNodeKeyEffectRefused);assert.equal(opens,n)} + }finally{await f.dispose()} + }) + await t.test("closed or already expired original scope refuses before key IO",async()=>{ + const f=await fixture();try{const n=opens,effect=f.create();effect.close();assert.throws(()=>effect.calculate(f.inputs));const expired=createBandwidthNodeKeyEffect({...f.config,deadlineAt:Date.now()-1});assert.throws(()=>expired.calculate(f.inputs));assert.equal(opens,n) + }finally{await f.dispose()} + }) + await t.test("short reads complete exactly 32 bytes under the same checks",async()=>{ + const f=await fixture();try{shortRead=true;const n=reads;assert.deepEqual(f.create().calculate(f.inputs),f.expected(f.inputs));assert.equal(reads-n,5)}finally{await f.dispose()} + }) + await t.test("close failure suppresses output, wipes key and prohibits replay",async()=>{ + const f=await fixture();try{const effect=f.create(),n=closes;closeFault=true;assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused);assert.equal(closes-n,1);closeFault=false;const before=opens;assert.throws(()=>effect.calculate(f.inputs));assert.equal(opens,before)}finally{await f.dispose()} + }) + await t.test("scratch key is wiped before descriptor closure",async()=>{ + const f=await fixture();try{const first=buffers.length;let checked=false;hook=p=>{if(p==="close"){checked=true;assert(buffers.slice(first).length>0);for(const b of buffers.slice(first))assert(b.every(byte=>byte===0))}};assert.deepEqual(f.create().calculate(f.inputs),f.expected(f.inputs));assert(checked)}finally{await f.dispose()} + }) + await t.test("monotonic horizon still expires after wall clock rolls backward",async()=>{ + const f=await fixture(),wall=Date.now,mono=performance.now.bind(performance);let elapsed=0 + const clock=mock.method(performance,"now",()=>mono()+elapsed) + try{const effect=f.create();Date.now=()=>wall()-60000;elapsed=5001;const n=opens;assert.throws(()=>effect.calculate(f.inputs),BandwidthNodeKeyEffectRefused);assert.equal(opens,n)}finally{Date.now=wall;clock.mock.restore();await f.dispose()} + }) + await t.test("longer requested deadline cannot extend five-second local horizon",async()=>{ + const f=await fixture(),mono=performance.now.bind(performance);let elapsed=0 + const clock=mock.method(performance,"now",()=>mono()+elapsed) + try{const effect=createBandwidthNodeKeyEffect({...f.config,deadlineAt:Date.now()+60000});elapsed=5001;const n=opens;assert.throws(()=>effect.calculate(f.inputs));assert.equal(opens,n)}finally{clock.mock.restore();await f.dispose()} + }) + await t.test("constructor rejects getter configuration and fake owner signal without IO",async()=>{ + const f=await fixture();try{let calls=0;const n=opens + assert.throws(()=>createBandwidthNodeKeyEffect({...f.config,get keyFile(){calls++;return f.keyFile}}));assert.throws(()=>createBandwidthNodeKeyEffect({...f.config,ownerSignal:{} as AbortSignal}));assert.equal(calls,0);assert.equal(opens,n) + }finally{await f.dispose()} + }) + await t.test("proxy actor/owner signals are rejected without any traps",async()=>{ + const f=await fixture();try{let calls=0;const n=opens + for(const name of ["signal","ownerSignal"] as const){const signal=new Proxy(f.config[name],{get(){calls++;return false},has(){calls++;return false},getPrototypeOf(){calls++;return AbortSignal.prototype}});assert.throws(()=>createBandwidthNodeKeyEffect({...f.config,[name]:signal}),BandwidthNodeKeyEffectRefused)} + assert.equal(calls,0);assert.equal(opens,n) + }finally{await f.dispose()} + }) + await t.test("real signal own-symbol accessors refuse before the initial builtin aborted getter",async()=>{ + for(const name of ["signal","ownerSignal"] as const){const f=await fixture();try{let calls=0;const n=opens,symbol=Object.getOwnPropertySymbols(f.config[name]).find(key=>String(key)==="Symbol(kAborted)");assert(symbol) + const original=Object.getOwnPropertyDescriptor(f.config[name],symbol)! + Object.defineProperty(f.config[name],symbol,{configurable:true,get(){calls++;return original.value}}) + try{assert.throws(()=>f.create(),BandwidthNodeKeyEffectRefused);assert.equal(calls,0);assert.equal(opens,n)}finally{Object.defineProperty(f.config[name],symbol,original)} + }finally{await f.dispose()}} + }) + await t.test("later own-symbol getter mutation cannot execute during key read/HMAC/close",async()=>{ + for(const name of ["signal","ownerSignal"] as const){const f=await fixture();try{let calls=0;const effect=f.create(),symbol=Object.getOwnPropertySymbols(f.config[name]).find(key=>String(key)==="Symbol(kAborted)");assert(symbol) + const original=Object.getOwnPropertyDescriptor(f.config[name],symbol)! + Object.defineProperty(f.config[name],symbol,{configurable:true,get(){calls++;return original.value}}) + try{assert.deepEqual(effect.calculate(f.inputs),f.expected(f.inputs));effect.close();assert.equal(calls,0)}finally{Object.defineProperty(f.config[name],symbol,original)} + }finally{await f.dispose()}} + }) +}) diff --git a/backend/src/services/bandwidth-node-key-effect.ts b/backend/src/services/bandwidth-node-key-effect.ts new file mode 100644 index 00000000..e8773070 --- /dev/null +++ b/backend/src/services/bandwidth-node-key-effect.ts @@ -0,0 +1,98 @@ +/** Fixed local key/MAC primitive for the prospective closed operator pipeline. + * This is NOT an authority/Q capability. No runtime caller is wired to it. + * The dispatcher must verify current public source, actor, Q and same-owner + * metadata immediately before calling, then post-check before using results. + * Only a local regular 32-byte key is supported. Synchronous OS I/O cannot be + * preempted; late returns are refused, not presented as bounded OS latency. */ +import {constants,lstatSync,realpathSync,openSync,fstatSync,readSync,closeSync,type Stats} from "node:fs" +import {createHash,createHmac} from "node:crypto" +import {dirname,isAbsolute,resolve} from "node:path" +import {performance} from "node:perf_hooks" +import {types} from "node:util" +import {bandwidthNodeBootstrapSchema,type BandwidthNodeBootstrap} from "./bandwidth-node-bootstrap.js" +import {captureBandwidthNodeProofBatch,type BandwidthNodeProofInput} from "./bandwidth-node-proof-input.js" + +export class BandwidthNodeKeyEffectRefused extends Error { + constructor(){super("Bandwidth local key effect refused")} +} +const fail=():never=>{throw new BandwidthNodeKeyEffectRefused()} +const same=(a:Stats,b:Stats)=>a.dev===b.dev&&a.ino===b.ino&&a.size===b.size&&a.mtimeMs===b.mtimeMs&&a.ctimeMs===b.ctimeMs +function fields(value:unknown,names:readonly string[]):Record{ + if(!value||typeof value!=="object"||types.isProxy(value)||Object.getPrototypeOf(value)!==Object.prototype)fail() + const ds=Object.getOwnPropertyDescriptors(value),keys=Reflect.ownKeys(ds) + if(keys.length!==names.length||keys.some(key=>typeof key!=="string"||!names.includes(key)))fail() + const result:Record={} + for(const name of names){const d=ds[name];if(!d||!("value"in d)||!d.enumerable)fail();result[name]=d.value} + return result +} +const aborted=Object.getOwnPropertyDescriptor(AbortSignal.prototype,"aborted")!.get! +function isAborted(value:AbortSignal):boolean{return Reflect.apply(aborted,value,[]) as boolean} +const addAbort=EventTarget.prototype.addEventListener,removeAbort=EventTarget.prototype.removeEventListener + +/** Inert construction. Both signals and the original five-second horizon are + * pinned before any filesystem access; later calls cannot renew this object. */ +export function createBandwidthNodeKeyEffect(raw:{keyFile:string;bootstrap:BandwidthNodeBootstrap;deadlineAt:number;signal:AbortSignal;ownerSignal:AbortSignal}){ + const beganWall=Date.now(),beganMono=performance.now() + let keyFile:string,bootstrap:BandwidthNodeBootstrap,deadlineAt:number,signal:AbortSignal,ownerSignal:AbortSignal + let actorAborted=false,ownerAborted=false,actorListening=false,ownerListening=false,calculating=false + const actorStop=()=>{actorAborted=true},ownerStop=()=>{ownerAborted=true} + const detach=()=>{ + if(actorListening){actorListening=false;Reflect.apply(removeAbort,signal,["abort",actorStop])} + if(ownerListening){ownerListening=false;Reflect.apply(removeAbort,ownerSignal,["abort",ownerStop])} + } + try{ + const c=fields(raw,["keyFile","bootstrap","deadlineAt","signal","ownerSignal"]) + if(typeof c.keyFile!=="string"||!isAbsolute(c.keyFile)||resolve(c.keyFile)!==c.keyFile||!Number.isFinite(c.deadlineAt)||types.isProxy(c.signal)||types.isProxy(c.ownerSignal))fail() + keyFile=c.keyFile as string;deadlineAt=c.deadlineAt as number;signal=c.signal as AbortSignal;ownerSignal=c.ownerSignal as AbortSignal + // Node implements even its builtin aborted getter in JS against an own + // configurable symbol. Reject accessors BEFORE invoking that getter, and + // never inspect any signal property while the scratch key is live. + for(const value of [signal,ownerSignal])if(!value||typeof value!=="object"||Reflect.ownKeys(value).some(name=>!("value"in Object.getOwnPropertyDescriptor(value,name)!)))fail() + actorAborted=isAborted(signal);ownerAborted=isAborted(ownerSignal) + bootstrap=bandwidthNodeBootstrapSchema.parse(fields(c.bootstrap,Object.keys(bandwidthNodeBootstrapSchema.shape))) + Reflect.apply(addAbort,signal,["abort",actorStop,{once:true}]);actorListening=true + Reflect.apply(addAbort,ownerSignal,["abort",ownerStop,{once:true}]);ownerListening=true + }catch{try{detach()}catch{/* No key/file exists during constructor cleanup. */}return fail()} + const wallEnd=Math.min(deadlineAt,beganWall+5000),monoEnd=beganMono+Math.max(0,wallEnd-beganWall) + let closed=false,directory:Stats|undefined + const live=()=>{if(closed||actorAborted||ownerAborted||Date.now()>=wallEnd||performance.now()>=monoEnd)fail()} + const directoryCurrent=()=>{ + live();const parent=dirname(keyFile),now=lstatSync(parent);live() + if(!now.isDirectory()||now.isSymbolicLink()||realpathSync(parent)!==parent)fail();live() + if(directory&&(directory.dev!==now.dev||directory.ino!==now.ino))fail() + directory??=now + } + return Object.freeze({ + calculate(inputs:readonly BandwidthNodeProofInput[]):readonly string[]{ + let key:Buffer|undefined,fd:number|undefined + try{ + calculating=true + live();const captured=captureBandwidthNodeProofBatch(inputs) + for(const {purpose,text}of captured){ + if(purpose.startsWith("external-")&&bootstrap.format!=="mmapp-bandwidth-node-bootstrap-v2")fail() + if(purpose!=="claim"){ + const value=JSON.parse(text) + if(value.installationId!==bootstrap.installationId||value.issuerKeySha256!==bootstrap.issuerKeySha256||value.quiescenceProof!==bootstrap.quiescenceProof)fail() + } + } + directoryCurrent();const before=lstatSync(keyFile);live() + if(!before.isFile()||before.isSymbolicLink()||before.size!==32||realpathSync(keyFile)!==keyFile)fail();live() + fd=openSync(keyFile,constants.O_RDONLY|(constants.O_NOFOLLOW??0)|(constants.O_NONBLOCK??0));live() + const opened=fstatSync(fd);live();if(!opened.isFile()||!same(before,opened))fail() + key=Buffer.alloc(32);let length=0 + while(length<32){live();const n=readSync(fd,key,length,32-length,length);live();if(!n)fail();length+=n} + const after=fstatSync(fd);live();if(!same(opened,after)||!same(after,lstatSync(keyFile)))fail() + directoryCurrent();live() + if(createHash("sha256").update(key).digest("hex")!==bootstrap.issuerKeySha256)fail() + const result=captured.map(({purpose,text})=>{live();return createHmac("sha256",key!).update("mmapp-bandwidth-node-"+purpose+"-v1:"+text).digest("hex")}) + live();return Object.freeze(result) + }catch{closed=true;return fail()} + finally{ + key?.fill(0) + try{if(fd!==undefined){try{closeSync(fd);live()}catch{closed=true;fail()}}} + finally{calculating=false;if(closed){try{detach()}catch{closed=true;fail()}}} + } + }, + close(){closed=true;if(!calculating){try{detach()}catch{return fail()}}}, + }) +} diff --git a/backend/src/services/bandwidth-node-observation.test.ts b/backend/src/services/bandwidth-node-observation.test.ts new file mode 100644 index 00000000..18354207 --- /dev/null +++ b/backend/src/services/bandwidth-node-observation.test.ts @@ -0,0 +1,181 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { once } from "node:events" +import { createBandwidthNodeObservationReader, BandwidthNodeObservationError, type BandwidthNodeSourceSnapshot, type BandwidthNodeObservationScope } from "./bandwidth-node-observation.js" + +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const bounded = (work: Promise) => Promise.race([work, new Promise((_resolve, reject) => { const timer = setTimeout(() => reject(Error("Owned localhost fixture timed out")), 3000); timer.unref(); void work.finally(() => clearTimeout(timer)).catch(() => {}) })]) +const scope = (ms = 4000): BandwidthNodeObservationScope => ({ deadlineAt: Date.now() + ms }) +const denied = (code?: BandwidthNodeObservationError["code"]) => (error: unknown) => error instanceof BandwidthNodeObservationError && (!code || error.code === code) +const cases: string[] = [], requests: Array<{ method: string | undefined; path: string | undefined; authenticated: boolean }> = [] +let response: unknown = { "system-id": "SYS-001", "software-id": "SOFT-002" }, status = 200, hold = false +let onRequest: (() => void) | undefined, onClosed: (() => void) | undefined +const username = "NODE_OBSERVATION_PRIVATE_USER", password = "NODE_OBSERVATION_PRIVATE_PASSWORD" +const authorization = "Basic " + Buffer.from(username + ":" + password).toString("base64") +const server = createServer((req, res) => { + requests.push({ method: req.method, path: req.url, authenticated: req.headers.authorization === authorization }) + const closed = onClosed; res.once("close", () => { if (!res.writableEnded) closed?.() }) + onRequest?.() + if (hold) { res.writeHead(200); res.flushHeaders(); return } + res.writeHead(status, { "content-type": "application/json", ...(status === 302 ? { location: "/must-not-follow" } : {}) }) + res.end(JSON.stringify(response)) +}) +server.listen(0, "127.0.0.1"); await once(server, "listening") +const address = server.address(); assert(address && typeof address !== "string") +const initial: BandwidthNodeSourceSnapshot = { serverId: 17, enabled: true, sourceProof: "a".repeat(64), authorityGeneration: "trusted-source-generation", + endpoint: { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username, password, routingEndpointEpoch: randomUUID() } } +let current = structuredClone(initial), reads = 0, readHook: (() => void) | undefined +const readers: ReturnType[] = [] +const make = () => { + const source = { async read() { reads++; readHook?.(); return current } } + const reader = createBandwidthNodeObservationReader(source); readers.push(reader); return { reader, source } +} +const reset = () => { current = structuredClone(initial); response = { "system-id": "SYS-001", "software-id": "SOFT-002" }; status = 200; hold = false; onRequest = undefined; onClosed = undefined; readHook = undefined } +const inert = make(); assert.equal(reads, 0); assert.equal(requests.length, 0) +// The client transitively allocates its existing default pool on import. Prove +// actual GET observation never borrows it; no test database is configured/read. +const { pool, closePool } = await import("../db/index.js") +const originalQuery = pool.query, originalConnect = pool.connect +let databaseCalls = 0 +pool.query = (() => { databaseCalls++; throw Error("Default database access forbidden in native observation fixture") }) as typeof pool.query +pool.connect = (() => { databaseCalls++; throw Error("Default database checkout forbidden in native observation fixture") }) as typeof pool.connect +try { + const observation = await inert.reader.observe(17, scope()).catch(error => { + console.error("Owned initial native observation diagnostic:", JSON.stringify({ databaseCalls, requests, reads })) + throw error + }), evidence = await inert.reader.inspect(observation, scope()) + assert.deepEqual(evidence.claims, [{ kind: "system-id", value: "SYS-001" }, { kind: "software-id", value: "SOFT-002" }]) + assert.equal(evidence.sourceProof, initial.sourceProof); assert.equal(evidence.authorityGeneration, initial.authorityGeneration) + assert.equal(evidence.physicalUniquenessVerified, false); assert.equal(evidence.cloneResistanceVerified, false) + assert(!JSON.stringify(evidence).includes(username)); assert(!JSON.stringify(evidence).includes(password)) + assert.equal(requests.length, 1); assert.deepEqual(requests[0], { method: "GET", path: "/rest/system/license", authenticated: true }) + cases.push("inert reader construction then actual authenticated MikrotikClient GET, fixed path and ordered typed two-kind claims, no physical uniqueness/clone-resistance assertion or exposed credentials") + + for (const native of [{ "system-id": "SAME" }, [{ "software-id": "SAME" }], [{ "software-id": "SAME", "system-id": "SAME" }]]) { + reset(); response = native + const reader = make().reader, token = await reader.observe(17, scope()), projected = await reader.inspect(token, scope()) + const row = Array.isArray(native) ? native[0] : native + assert.deepEqual(projected.claims.map(item => item.kind), ["system-id", "software-id"].filter(kind => Object.hasOwn(row, kind))) + assert(projected.claims.every(item => item.value === "SAME")) + } + cases.push("strict object or array-one responses preserve system-id/software-id kind, including equal values and fixed vector order") + + const beforeFake = requests.length, beforeFakeReads = reads + for (const fake of [JSON.parse(JSON.stringify(observation)), { ...observation }, evidence, { kind: "routeros-license-observation", claims: [{ kind: "system-id", value: "FORGED" }] }]) + await assert.rejects(inert.reader.inspect(fake, scope()), denied("NOT_GENUINE")) + const other = make().reader + await assert.rejects(other.inspect(observation, scope()), denied("NOT_GENUINE")) + assert.equal(reads, beforeFakeReads); assert.equal(requests.length, beforeFake) + assert.equal(Reflect.set(observation, "kind", "forged"), false) + assert.equal(Reflect.set(evidence.claims[0], "value", "forged"), false) + cases.push("serialized, copied, mutated and another-reader observations cannot manufacture private provenance; frozen claim metadata cannot mutate stored evidence") + + for (const malformed of [[], [{ "system-id": "A" }, { "system-id": "B" }], null, "A", {}, { "system-id": 1 }, { "system-id": " " }, { "system-id": " A" }, { "system-id": "A\u0000B" }, { "system-id": "x".repeat(257) }, { "system-id": null, "software-id": "VALID" }]) { + reset(); response = malformed + await assert.rejects(make().reader.observe(17, scope()), denied("INVALID_LICENSE_RESPONSE")) + } + reset(); response = Object.fromEntries([["system-id", "VALID"], ...Array.from({ length: 64 }, (_, n) => ["field" + n, "x"])]) + await assert.rejects(make().reader.observe(17, scope()), denied("INVALID_LICENSE_RESPONSE")) + cases.push("ambiguous/missing/non-string/empty/control/oversize claims and too many native fields refuse; invalid present claim never falls back to another kind") + + reset(); response = { "system-id": "x".repeat(20000) } + await assert.rejects(make().reader.observe(17, scope()), denied()) + reset(); status = 500; response = { error: password } + const nativeError = await make().reader.observe(17, scope()).then(() => assert.fail("Expected HTTP error"), error => error as Error) + assert(!nativeError.message.includes(password)); assert(!nativeError.message.includes(username)) + reset(); status = 302; const beforeRedirect = requests.length + await assert.rejects(make().reader.observe(17, scope()), denied()); assert.equal(requests.length, beforeRedirect + 1) + cases.push("wire-byte cap, HTTP rejection and redirect refuse without reflecting provider secrets or following another URL") + + reset(); const preSend = requests.length, at = reads + readHook = () => { if (reads === at + 2) current.endpoint.password = "CHANGED_BEFORE_DISPATCH" } + await assert.rejects(make().reader.observe(17, scope()), denied("SOURCE_CHANGED")); assert.equal(requests.length, preSend) + reset(); onRequest = () => { current.endpoint.password = "CHANGED_AFTER_SEND" } + await assert.rejects(make().reader.observe(17, scope()), denied("SOURCE_CHANGED")) + reset(); onRequest = () => { current.sourceProof = "b".repeat(64) } + await assert.rejects(make().reader.observe(17, scope()), denied("SOURCE_CHANGED")) + cases.push("trusted endpoint credentials drift before dispatch gives zero GET; credential/source proof drift during real GET prevents publishing an observation") + + reset(); const stableReader = make().reader, stable = await stableReader.observe(17, scope()), countBeforeInspect = requests.length + current.authorityGeneration = "NEW_GENERATION" + await assert.rejects(stableReader.inspect(stable, scope()), denied("SOURCE_CHANGED")); assert.equal(requests.length, countBeforeInspect) + reset(); const revoked = make().reader, revokedToken = await revoked.observe(17, scope()) + current.enabled = false + await assert.rejects(revoked.inspect(revokedToken, scope()), denied()) + cases.push("issuer inspection re-reads fixed trusted source; changed authority or disabled source refuses without another native GET") + + reset(); const composed = make() + composed.source.read = async () => { throw Error("Replacement must not redirect the captured trusted reader") } + const composedToken = await composed.reader.observe(17, scope()); await composed.reader.inspect(composedToken, scope()) + const beforeRequestCallback = requests.length, beforeCallbackReads = reads + await assert.rejects(composed.reader.observe(17, Object.assign(scope(), { read: async () => initial })), denied("INVALID_INPUT")) + assert.equal(requests.length, beforeRequestCallback); assert.equal(reads, beforeCallbackReads) + cases.push("constructor captures its fixed trusted source method; request scope cannot inject a source or transport callback") + + reset(); const alreadyCancelled = new AbortController(); alreadyCancelled.abort() + const cancelledRequests = requests.length, cancelledReads = reads + await assert.rejects(make().reader.observe(17, { ...scope(), signal: alreadyCancelled.signal }), denied("CANCELLED")) + assert.equal(reads, cancelledReads); assert.equal(requests.length, cancelledRequests) + reset(); hold = true; const arrived = deferred(), socketClosed = deferred(), controller = new AbortController() + onRequest = arrived.resolve; onClosed = socketClosed.resolve + const cancelled = make().reader.observe(17, { ...scope(), signal: controller.signal }) + await bounded(arrived.promise); controller.abort() + await assert.rejects(cancelled, denied()); await bounded(socketClosed.promise) + cases.push("pre-abort performs no source/native work; cancellation during actual localhost GET closes owned response socket and publishes no observation") + + reset(); hold = true; const timedOutClosed = deferred(), timeoutArrived = deferred() + onClosed = timedOutClosed.resolve; onRequest = timeoutArrived.resolve + const timedOut = make().reader.observe(17, scope(300)) + await bounded(timeoutArrived.promise); await assert.rejects(timedOut, denied()); await bounded(timedOutClosed.promise) + cases.push("absolute caller deadline bounds a real stalled HTTP response and closes its owned socket") + + reset(); const shortReader = make().reader, expires = Date.now() + 250 + const short = await shortReader.observe(17, { deadlineAt: expires }) + await new Promise(resolve => setTimeout(resolve, Math.max(1, expires - Date.now() + 10))) + const beforeExpired = reads + await assert.rejects(shortReader.inspect(short, scope()), denied("CANCELLED")); assert.equal(reads, beforeExpired) + reset(); const originalAbort = new AbortController(), originalReader = make().reader + const originalToken = await originalReader.observe(17, { ...scope(), signal: originalAbort.signal }); originalAbort.abort() + await assert.rejects(originalReader.inspect(originalToken, scope()), denied("CANCELLED")) + cases.push("new inspection scope cannot extend original observation expiry or resurrect its cancelled original signal") + + reset() + const clientReader=make().reader,clientToken=await clientReader.observe(17,scope()) + const strictClient=await clientReader.strictClient(clientToken,scope()),clientRequests=requests.length,clientReads=reads + await assert.rejects(strictClient.post("/tool/bandwidth-test",{address:"192.0.2.1",user:"synthetic",password:"synthetic",duration:"3s"})) + assert.equal(requests.length,clientRequests);assert.equal(databaseCalls,0) + await assert.rejects(make().reader.strictClient(clientToken,scope()),denied("NOT_GENUINE")) + await assert.rejects(clientReader.strictClient(JSON.parse(JSON.stringify(clientToken)),scope()),denied("NOT_GENUINE")) + assert.equal(reads,clientReads) + current.endpoint.password+="-drift" + await assert.rejects(clientReader.strictClient(clientToken,scope()),denied("SOURCE_CHANGED")) + await assert.rejects(shortReader.strictClient(short,scope()),denied("CANCELLED")) + assert.equal(requests.length,clientRequests);assert.equal(databaseCalls,0) + cases.push("same-reader genuine observation creates only strict frozen native client; unadmitted bandwidth refuses before SQL/HTTP, foreign/copy/expired/drifted factory inputs refuse without endpoint injection") + + reset(); const beforeStall = requests.length + let resolveLate!: (snapshot: BandwidthNodeSourceSnapshot) => void + const late = new Promise(resolve => { resolveLate = resolve }) + const stalled = createBandwidthNodeObservationReader({ read: async () => late }); readers.push(stalled) + await assert.rejects(stalled.observe(17, scope(50)), denied("CANCELLED")); assert.equal(requests.length, beforeStall) + resolveLate(structuredClone(initial)); await new Promise(resolve => setImmediate(resolve)) + assert.equal(requests.length, beforeStall) + reset(); hold = true; const closeArrived = deferred(), closeSocket = deferred(), closing = make().reader + onRequest = closeArrived.resolve; onClosed = closeSocket.resolve + const closePending = closing.observe(17, scope()); await bounded(closeArrived.promise); closing.close() + await assert.rejects(closePending, denied()); await bounded(closeSocket.promise) + await assert.rejects(closing.inspect(observation, scope()), denied("CLOSED")) + await assert.rejects(closing.observe(17, scope()), denied("CLOSED")) + cases.push("stalled trusted source cannot trigger late native IO after timeout; close cancels active HTTP and invalidates observation/reader use") + + assert.equal(databaseCalls, 0) + assert(requests.every(item => item.method === "GET" && item.path === "/rest/system/license" && item.authenticated)) + console.log("Bandwidth node native observation:", JSON.stringify({ cases: cases.length, checks: cases, actualLocalhostGets: requests.length, + defaultDatabaseCalls: databaseCalls, productionWired: false, canonicalNodeIdsCreated: 0, physicalUniquenessVerified: false, cloneResistanceVerified: false, realChrCalls: 0 })) +} finally { + for (const reader of readers) reader.close() + pool.query = originalQuery; pool.connect = originalConnect + server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) + await closePool() +} diff --git a/backend/src/services/bandwidth-node-observation.ts b/backend/src/services/bandwidth-node-observation.ts new file mode 100644 index 00000000..be4052b3 --- /dev/null +++ b/backend/src/services/bandwidth-node-observation.ts @@ -0,0 +1,174 @@ +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" + +export interface BandwidthNodeObservationScope { deadlineAt: number; signal?: AbortSignal } +export interface BandwidthNodeEndpoint { + host: string; port: number; useSsl: boolean; verifySsl: boolean; username: string; password: string; routingEndpointEpoch: string +} +export interface BandwidthNodeSourceSnapshot { + serverId: number; enabled: boolean; sourceProof: string; authorityGeneration: string; endpoint: BandwidthNodeEndpoint +} +/** Server-owned composition only. This fixed reader must itself check current + * authority and honor its owned deadline/signal. No callback or endpoint is + * accepted by observe()/inspect(), and native transport is never injectable. */ +export interface BandwidthNodeTrustedSource { + read(serverId: number, scope: Readonly): Promise +} +export type BandwidthNodeLicenseKind = "system-id" | "software-id" +export interface BandwidthNodeLicenseClaim { readonly kind: BandwidthNodeLicenseKind; readonly value: string } +declare const observationBrand: unique symbol +export interface BandwidthNodeObservation { + readonly kind: "routeros-license-observation" + readonly [observationBrand]: true +} +export interface BandwidthNodeObservationEvidence { + readonly version: 1; readonly serverId: number; readonly sourceProof: string; readonly authorityGeneration: string + readonly routingEndpointEpoch: string; readonly observedAt: number; readonly expiresAt: number + readonly claims: readonly BandwidthNodeLicenseClaim[] + readonly physicalUniquenessVerified: false; readonly cloneResistanceVerified: false +} +export class BandwidthNodeObservationError extends Error { + constructor(readonly code: "INVALID_INPUT" | "CANCELLED" | "SOURCE_CHANGED" | "SOURCE_UNAVAILABLE" | "INVALID_LICENSE_RESPONSE" | "NOT_GENUINE" | "CLOSED") { + super("Bandwidth node observation: " + code) + } +} +const MAX_OBSERVATION_MS = 5000, MAX_LICENSE_BYTES = 16384 +function fail(code: BandwidthNodeObservationError["code"]): never { throw new BandwidthNodeObservationError(code) } +const plain = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +function fields(value: unknown, names: string[]): asserts value is Record { + if (!plain(value)) fail("INVALID_INPUT") + const descriptors = Object.getOwnPropertyDescriptors(value) + if (Reflect.ownKeys(value).length !== names.length || Object.keys(descriptors).sort().join(",") !== names.sort().join(",") + || Object.values(descriptors).some(item => !("value" in item) || !item.enumerable)) fail("INVALID_INPUT") +} +const text = (value: unknown, max: number): value is string => typeof value === "string" && value.length > 0 && Buffer.byteLength(value) <= max && value.trim() === value && !/[\u0000-\u001f\u007f]/.test(value) +function capture(raw: unknown, serverId: number): Readonly { + fields(raw, ["serverId", "enabled", "sourceProof", "authorityGeneration", "endpoint"]) + fields(raw.endpoint, ["host", "port", "useSsl", "verifySsl", "username", "password", "routingEndpointEpoch"]) + const endpoint = raw.endpoint + if (raw.serverId !== serverId || raw.enabled !== true || typeof raw.sourceProof !== "string" || !/^[a-f0-9]{64}$/.test(raw.sourceProof) + || !text(raw.authorityGeneration, 200) || !text(endpoint.host, 1000) || /[\s/\\]/.test(endpoint.host) + || !Number.isSafeInteger(endpoint.port) || Number(endpoint.port) < 1 || Number(endpoint.port) > 65535 + || typeof endpoint.useSsl !== "boolean" || typeof endpoint.verifySsl !== "boolean" + || typeof endpoint.username !== "string" || Buffer.byteLength(endpoint.username) > 1000 + || typeof endpoint.password !== "string" || Buffer.byteLength(endpoint.password) > 65536 + || typeof endpoint.routingEndpointEpoch !== "string" || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(endpoint.routingEndpointEpoch)) fail("SOURCE_UNAVAILABLE") + return Object.freeze({ ...raw, endpoint: Object.freeze({ ...endpoint }) }) as unknown as Readonly +} +function claims(raw: unknown): readonly BandwidthNodeLicenseClaim[] { + const row: unknown = Array.isArray(raw) ? raw.length === 1 ? raw[0] : undefined : raw + if (!plain(row) || Object.keys(row).length > 64) fail("INVALID_LICENSE_RESPONSE") + const result: BandwidthNodeLicenseClaim[] = [] + // Kind and fixed vector order are significant, including when both values + // happen to be equal. No preferred-ID fallback or identifier normalization. + for (const kind of ["system-id", "software-id"] as const) { + if (!Object.hasOwn(row, kind)) continue + if (!text(row[kind], 256)) fail("INVALID_LICENSE_RESPONSE") + result.push(Object.freeze({ kind, value: row[kind] })) + } + if (!result.length) fail("INVALID_LICENSE_RESPONSE") + return Object.freeze(result) +} +type Origin = { source: Readonly; claims: readonly BandwidthNodeLicenseClaim[]; observedAt: number; expiresAt: number; monotonicEnd: number; signal?: AbortSignal } + +/** Inert construction/import: no default pool, authority lookup, key generation + * or native request. Actual observation lazily imports the existing client; + * its transitive imports may allocate their existing pool, but no DB query is + * used here. A future issuer must pin THIS reader, not trust any JSON evidence. */ +export function createBandwidthNodeObservationReader(trustedSource: BandwidthNodeTrustedSource) { + if (!trustedSource || typeof trustedSource.read !== "function") fail("INVALID_INPUT") + const readSource = trustedSource.read.bind(trustedSource), observations = new WeakMap(), windows = new Set() + let closed = false + const active = () => { if (closed) fail("CLOSED") } + function window(raw: BandwidthNodeObservationScope, origin?: Origin) { + active() + if (!plain(raw) || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(item => !("value" in item) || !item.enumerable) + || Object.keys(raw).some(key => !["deadlineAt", "signal"].includes(key)) + || !Number.isSafeInteger(raw.deadlineAt) || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) fail("INVALID_INPUT") + const external = raw.signal + const duration = Math.min(MAX_OBSERVATION_MS, raw.deadlineAt - Date.now(), origin ? origin.expiresAt - Date.now() : Infinity, + origin ? origin.monotonicEnd - performance.now() : Infinity) + if (duration <= 0 || external?.aborted || origin?.signal?.aborted) fail("CANCELLED") + const deadlineAt = Math.min(raw.deadlineAt, Date.now() + Math.floor(duration), origin?.expiresAt ?? Infinity) + const monotonicEnd = performance.now() + duration, controller = new AbortController() + windows.add(controller) + const abort = () => controller.abort(), timer = setTimeout(abort, Math.max(1, Math.ceil(duration))) + external?.addEventListener("abort", abort, { once: true }); origin?.signal?.addEventListener("abort", abort, { once: true }) + const live = () => { active(); if (controller.signal.aborted || external?.aborted || origin?.signal?.aborted || Date.now() >= deadlineAt || performance.now() >= monotonicEnd) fail("CANCELLED") } + const ownedScope = Object.freeze({ deadlineAt, signal: controller.signal }) + const wait = async (work: () => Promise): Promise => { + live() + let stop!: () => void + const cancelled = new Promise((_resolve, reject) => { stop = () => reject(new BandwidthNodeObservationError("CANCELLED")); controller.signal.addEventListener("abort", stop, { once: true }) }) + try { const result = await Promise.race([Promise.resolve().then(() => { live(); return work() }), cancelled]); live(); return result } + finally { controller.signal.removeEventListener("abort", stop) } + } + return { live, wait, scope: ownedScope, external, monotonicEnd, + dispose() { clearTimeout(timer); external?.removeEventListener("abort", abort); origin?.signal?.removeEventListener("abort", abort); windows.delete(controller); controller.abort() } } + } + return Object.freeze({ + async observe(serverId: number, scope: BandwidthNodeObservationScope): Promise { + if (!Number.isSafeInteger(serverId) || serverId < 1) fail("INVALID_INPUT") + const owned = window(scope) + try { + const before = capture(await owned.wait(() => readSource(serverId, owned.scope)), serverId) + const fence = async () => { + const current = capture(await owned.wait(() => readSource(serverId, owned.scope)), serverId) + if (!isDeepStrictEqual(current, before)) fail("SOURCE_CHANGED") + } + const { MikrotikClient } = await owned.wait(() => import("./mikrotik.js")) + const client = new MikrotikClient(before.endpoint) + const raw = await owned.wait(() => client.get("/system/license", Math.max(1, owned.scope.deadlineAt - Date.now()), owned.scope.signal, MAX_LICENSE_BYTES, fence)) + const observedAt = Date.now(), vector = claims(raw) + await fence(); owned.live() + const observation = Object.freeze({ kind: "routeros-license-observation" as const }) as BandwidthNodeObservation + observations.set(observation, { source: before, claims: vector, observedAt, expiresAt: owned.scope.deadlineAt, monotonicEnd: owned.monotonicEnd, signal: owned.external }) + return observation + } catch (error) { + if (error instanceof BandwidthNodeObservationError) throw error + throw new BandwidthNodeObservationError("SOURCE_UNAVAILABLE") + } finally { owned.dispose() } + }, + async inspect(observation: unknown, scope: BandwidthNodeObservationScope): Promise { + active() + const origin = observation && typeof observation === "object" ? observations.get(observation) : undefined + if (!origin) fail("NOT_GENUINE") + const owned = window(scope, origin) + try { + const current = capture(await owned.wait(() => readSource(origin.source.serverId, owned.scope)), origin.source.serverId) + if (!isDeepStrictEqual(current, origin.source)) fail("SOURCE_CHANGED") + owned.live() + return Object.freeze({ version: 1 as const, serverId: origin.source.serverId, sourceProof: origin.source.sourceProof, + authorityGeneration: origin.source.authorityGeneration, routingEndpointEpoch: origin.source.endpoint.routingEndpointEpoch, + observedAt: origin.observedAt, expiresAt: origin.expiresAt, claims: origin.claims, physicalUniquenessVerified: false as const, cloneResistanceVerified: false as const }) + } catch (error) { + if (error instanceof BandwidthNodeObservationError) throw error + throw new BandwidthNodeObservationError("SOURCE_UNAVAILABLE") + } finally { owned.dispose() } + }, + /** Backend-only strict client from this reader's genuine origin. Neither + * request JSON nor a copied observation can choose its endpoint/credentials. + * The server ID retains the existing configuration policy/audit boundary. */ + async strictClient(observation:unknown,scope:BandwidthNodeObservationScope){ + active() + const origin=observation&&typeof observation==="object"?observations.get(observation):undefined + if(!origin)fail("NOT_GENUINE") + const owned=window(scope,origin) + const fresh=async()=>{ + const current=capture(await owned.wait(()=>readSource(origin.source.serverId,owned.scope)),origin.source.serverId) + if(!isDeepStrictEqual(current,origin.source))fail("SOURCE_CHANGED") + } + try{ + await fresh() + const {MikrotikClient}=await owned.wait(()=>import("./mikrotik.js")) + await fresh();owned.live() + return new MikrotikClient(origin.source.endpoint,origin.source.serverId,{requireBandwidthPermit:true}) + }catch(error){ + if(error instanceof BandwidthNodeObservationError)throw error + throw new BandwidthNodeObservationError("SOURCE_UNAVAILABLE") + }finally{owned.dispose()} + }, + close() { closed = true; for (const controller of windows) controller.abort() }, + }) +} diff --git a/backend/src/services/bandwidth-node-proof-batch.test.ts b/backend/src/services/bandwidth-node-proof-batch.test.ts new file mode 100644 index 00000000..5edbc1d4 --- /dev/null +++ b/backend/src/services/bandwidth-node-proof-batch.test.ts @@ -0,0 +1,147 @@ +/** Actual signed files and configured source; only catalog SQL and the core + * transport are synthetic. This does not establish PostgreSQL acceptance. */ +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomUUID, sign, verify } from "node:crypto" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import os from "node:os" +import path from "node:path" +import { mock, test } from "node:test" +import { bandwidthNodeBootstrapSchema, canonicalBandwidthNodeBootstrap, canonicalBandwidthNodeValue, bandwidthNodeRegistrySchemaSourceSha256, bandwidthPeerRegistrySchemaSourceSha256 } from "./bandwidth-node-bootstrap.js" +import type { BandwidthNodeProofInput } from "./bandwidth-node-source.js" + +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } + +test("configured source atomic MAC batch boundaries", async t => { + mock.module("./bandwidth-control-registry-identity.js",{namedExports:{assertBandwidthControlRegistryIdentity:async()=>{throw Error("scalar core fixture must not invoke joint SQL")}}}) + const identity = async (sql: { query(text: string): Promise }, _expected: unknown, live: () => void) => { live(); await sql.query("synthetic catalog boundary"); live() } + mock.module("./bandwidth-node-identity.js", { namedExports: { assertBandwidthNodeIdentity: identity } }) + mock.module("./bandwidth-peer-identity.js", { namedExports: { assertBandwidthPeerIdentity: identity } }) + const { createConfiguredBandwidthNodeSource } = await import("./bandwidth-node-source.js") + async function fixture(v2 = true) { + const directory = await mkdtemp(path.join(os.tmpdir(), "mm-node-proof-batch-")) + const issuerKeyFile = path.join(directory, "issuer"), bootstrapFile = path.join(directory, "bootstrap") + const key = Buffer.alloc(32, 81), keys = generateKeyPairSync("ed25519") + const root = { installationId: randomUUID(), issuerKeySha256: sha(key), quiescenceProof: "b".repeat(64) } + const bootstrap = { ...root, format: v2 ? "mmapp-bandwidth-node-bootstrap-v2" : "mmapp-bandwidth-node-bootstrap-v1", + registrySchemaSourceSha256: v2 ? bandwidthPeerRegistrySchemaSourceSha256() : bandwidthNodeRegistrySchemaSourceSha256(), registryCatalogSha256: "c".repeat(64), attestationRef: "explicit unit operator bootstrap attestation" } + const publish = async () => { const payload = Buffer.from(canonicalBandwidthNodeBootstrap(bootstrap)); await writeFile(bootstrapFile, JSON.stringify({ payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) } + await writeFile(issuerKeyFile, key); await publish() + let queries = 0, connections = 0, releases = 0, guards = 0, revoked = false + let metadataHook: (() => Promise) | undefined + const sourceRecord = { installationId: root.installationId, controlGeneration: randomUUID(), sourceRecordSha256: "d".repeat(64), databaseName: "mock-control", runtimeRole: "mock-runtime", ownerRole: "mock-owner" } + const fence = { + async verifyNodeBootstrap(raw: unknown) { + const signed = raw as { payload: string; signature: string }, payload = Buffer.from(signed.payload, "base64url") + assert(verify(null, payload, keys.publicKey, Buffer.from(signed.signature, "base64url"))) + return { bootstrap: bandwidthNodeBootstrapSchema.parse(JSON.parse(payload.toString())), source: { ...sourceRecord } } + }, + async query() { await metadataHook?.(); return { rows: [] } }, + } + const core = { async connect(_scope: unknown, dispatch: (value: unknown) => Promise) { + connections++ + return { async query() { queries++; await dispatch(fence); await dispatch(fence); return { rows: [] } }, release() { releases++ } } + } } + const source = createConfiguredBandwidthNodeSource(core as never, { issuerKeyFile, bootstrapFile, issuerKeySha256: root.issuerKeySha256 }) + const originalGuard=async()=>{guards++;if(revoked)throw Error("original authority revoked")} + const open = (signal?: AbortSignal, lifetime = 5000) => source.open({ deadlineAt: Date.now() + lifetime, signal }, originalGuard) + const openOwned=(ownership:Parameters[2])=>source.openWithOwnershipFence({deadlineAt:Date.now()+5000},originalGuard,ownership) + const nodeId = randomUUID(), incarnation = randomUUID(), eventId = randomUUID() + const subject = v2 ? { enrollmentId: randomUUID() } : { serverId: 71 } + const batch = (): BandwidthNodeProofInput[] => [ + { purpose: "node", value: { ...root, nodeId, claimProof: "e".repeat(64) } }, + { purpose: v2 ? "external-alias" : "alias", value: { ...root, ...subject, nodeId, incarnation, status: "active" } }, + { purpose: v2 ? "external-event" : "event", value: { ...root, ...subject, nodeId, incarnation, eventId, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: "f".repeat(64) } }, + ] + const expected = (items: readonly BandwidthNodeProofInput[]) => items.map(({ purpose, value }) => createHmac("sha256", key).update("mmapp-bandwidth-node-" + purpose + "-v1:" + canonicalBandwidthNodeValue(value)).digest("hex")) + return { source, open, openOwned, batch, expected, root, bootstrap, sourceRecord, publish, issuerKeyFile, + counts: () => ({ queries, connections, releases, guards }), hook: (value?: () => Promise) => { metadataHook = value }, revoke: () => { revoked = true }, + async dispose() { source.close(); await rm(directory, { recursive: true, force: true }) } } + } + try { + await t.test("raw ownership probes share the lease and files plus original authority remain checked afterward",async()=>{ + for(const fault of ["none","key","grant"] as const){const f=await fixture();try{ + let probes=0,armed=false + const session=await f.openOwned(async fence=>{ + probes++;await fence.query("fixed synthetic ownership probe") + if(armed&&fault==="key")await writeFile(f.issuerKeyFile,Buffer.alloc(32,82)) + if(armed&&fault==="grant")f.revoke() + }) + const before=f.counts().queries;armed=true + if(fault==="none"){assert.deepEqual(await session.keyedProofs(f.batch()),f.expected(f.batch()));assert.equal(f.counts().queries-before,1);assert.equal(f.counts().connections,1);assert(probes>=4);session.close()} + else{await assert.rejects(session.keyedProofs(f.batch()));assert.equal(f.counts().releases,1)} + }finally{await f.dispose()}} + }) + await t.test("both versions preserve scalar domains, one fresh cycle and frozen output", async () => { + for (const v2 of [false, true]) { const f = await fixture(v2); try { + const session = await f.open(), before = f.counts(), items = f.batch() + const result = await session.keyedProofs(items) + assert.deepEqual(result, f.expected(items)); assert(Object.isFrozen(result)) + assert.equal(f.counts().queries - before.queries, 1); assert.equal(f.counts().connections, 1) + assert.equal(f.counts().guards - before.guards, 2, "original guard surrounds physical query") + for (let index = 0; index < items.length; index++) assert.equal(await session.keyedProof(items[index].purpose, items[index].value), result[index]) + session.close() + } finally { await f.dispose() } } + }) + await t.test("capture precedes blocked metadata and caller mutation cannot change any MAC", async () => { + const f = await fixture(); try { + const session = await f.open(), entered = deferred(), unblock = deferred(), items = f.batch(), expected = f.expected(items) + f.hook(async () => { entered.resolve(); await unblock.promise }) + const pending = session.keyedProofs(items); await entered.promise + ;(items[0].value as Record).claimProof = "a".repeat(64) + items[1] = { purpose: "claim", value: [{ kind: "system-id", value: "replacement" }] }; items.length = 1 + unblock.resolve(); assert.deepEqual(await pending, expected); session.close() + } finally { await f.dispose() } + }) + await t.test("accessors, sparse/symbol/serializer inputs and bounds reject before IO without executing caller code", async () => { + const f = await fixture(); try { + const session = await f.open(), before = f.counts().queries; let calls = 0 + const getter = { purpose: "node", get value() { calls++; return f.batch()[0].value } } + const nested = { ...f.batch()[0], value: { get installationId() { calls++; return f.root.installationId } } } + const sparse = new Array(3); sparse[0] = f.batch()[0] + const symbol = Object.assign(f.batch()[0], { [Symbol("hidden")]: "hidden" }) + const serializer = { purpose: "node", value: { toJSON() { calls++; return f.batch()[0].value } } } + for (const invalid of [[], sparse, [getter], [nested], [symbol], [serializer], [...f.batch(), ...f.batch()], [{ purpose: "claim", value: [{ kind: "system-id", value: "x".repeat(65537) }] }]]) await assert.rejects(session.keyedProofs(invalid)) + assert.equal(calls, 0); assert.equal(f.counts().queries, before); session.close() + } finally { await f.dispose() } + }) + await t.test("one wrong root or external domain under v1 rejects the entire batch", async () => { + for (const external of [false, true]) { const f = await fixture(false); try { + const session = await f.open(), items = f.batch() + items[2] = external ? { purpose: "external-alias", value: { ...f.root, enrollmentId: randomUUID(), nodeId: randomUUID(), incarnation: randomUUID(), status: "active" } } : { ...items[2], value: { ...(items[2].value as object), installationId: randomUUID() } } + await assert.rejects(session.keyedProofs(items)); assert.equal(f.counts().releases, 1) + } finally { await f.dispose() } } + }) + await t.test("key/bootstrap/source drift and last authority revocation after metadata refuse all MACs", async () => { + for (const fault of ["key", "bootstrap", "source", "grant"] as const) { const f = await fixture(); try { + const session = await f.open(); let applied = false + f.hook(async () => { if (applied) return; applied = true + if (fault === "key") await writeFile(f.issuerKeyFile, Buffer.alloc(32, 82)) + if (fault === "bootstrap") { f.bootstrap.quiescenceProof = "a".repeat(64); await f.publish() } + if (fault === "source") f.sourceRecord.sourceRecordSha256 = "a".repeat(64) + if (fault === "grant") f.revoke() + }) + await assert.rejects(session.keyedProofs(f.batch())); assert.equal(f.counts().releases, 1) + } finally { await f.dispose() } } + }) + await t.test("abort/expiry during blocked IO retires once and a late result cannot resume", async () => { + for (const expire of [false, true]) { const f = await fixture(); const unblock = deferred(); try { + const abort = new AbortController(), session = await f.open(abort.signal, expire ? 200 : 5000), entered = deferred() + f.hook(async () => { entered.resolve(); await unblock.promise }) + const pending = session.keyedProofs(f.batch()), rejected = assert.rejects(pending) + await entered.promise; if (!expire) abort.abort(); await rejected + assert.equal(f.counts().releases, 1); unblock.resolve() + await assert.rejects(session.binding()); assert.equal(f.counts().releases, 1) + } finally { unblock.resolve(); await f.dispose() } } + }) + await t.test("concurrent batch/scalar or binding destroys the lease instead of overlapping", async () => { + for (const scalar of [false, true]) { const f = await fixture(); const unblock = deferred(); try { + const session = await f.open(), entered = deferred() + f.hook(async () => { entered.resolve(); await unblock.promise }) + const first = assert.rejects(session.keyedProofs(f.batch())); await entered.promise + await assert.rejects(scalar ? session.keyedProof("claim", [{ kind: "system-id", value: "second" }]) : session.binding()) + await first; unblock.resolve(); assert.equal(f.counts().releases, 1) + } finally { unblock.resolve(); await f.dispose() } } + }) + } finally { mock.restoreAll() } +}) diff --git a/backend/src/services/bandwidth-node-proof-input.ts b/backend/src/services/bandwidth-node-proof-input.ts new file mode 100644 index 00000000..6d76a22f --- /dev/null +++ b/backend/src/services/bandwidth-node-proof-input.ts @@ -0,0 +1,57 @@ +/** Pure, bounded capture shared by the existing strong source and the fixed + * key effect. No files, SQL, keys, authority or caller callbacks. */ +import {types} from "node:util" +import {z} from "zod" +import {canonicalBandwidthNodeValue} from "./bandwidth-node-bootstrap.js" +export interface BandwidthNodeProofInput {readonly purpose:string;readonly value:unknown} +const fail=():never=>{throw new Error("Bandwidth proof input refused")} +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid(),sid=z.number().int().positive().max(Number.MAX_SAFE_INTEGER) +const rootFields={installationId:uuid,issuerKeySha256:hash,quiescenceProof:hash} +const claim=z.object({kind:z.enum(["system-id","software-id"]),value:z.string().min(1).refine(value=>Buffer.byteLength(value)<=256&&value.trim()===value&&!/[\u0000-\u001f\u007f]/.test(value))}).strict() +export const bandwidthNodeProofShapes={ + claim:z.array(claim).min(1).max(2).refine(value=>value.length===1||value[0].kind==="system-id"&&value[1].kind==="software-id"), + node:z.object({...rootFields,nodeId:uuid,claimProof:hash}).strict(), + alias:z.object({...rootFields,serverId:sid,incarnation:uuid,nodeId:uuid,status:z.enum(["active","tombstoned","quarantined"])}).strict(), + event:z.object({...rootFields,eventId:uuid,serverId:sid,incarnation:uuid,nodeId:uuid,kind:z.enum(["enroll","tombstone","quarantine"]), + oldStatus:z.literal("active").nullable(),newStatus:z.enum(["active","tombstoned","quarantined"]),authorityProof:hash}).strict() + .refine(value=>value.kind==="enroll"?value.oldStatus===null&&value.newStatus==="active":value.oldStatus==="active"&&value.newStatus===(value.kind==="tombstone"?"tombstoned":"quarantined")), + "external-alias":z.object({...rootFields,enrollmentId:uuid,incarnation:uuid,nodeId:uuid,status:z.enum(["active","tombstoned","quarantined"])}).strict(), + "external-event":z.object({...rootFields,eventId:uuid,enrollmentId:uuid,incarnation:uuid,nodeId:uuid,kind:z.enum(["enroll","tombstone","quarantine"]), + oldStatus:z.literal("active").nullable(),newStatus:z.enum(["active","tombstoned","quarantined"]),authorityProof:hash}).strict() + .refine(value=>value.kind==="enroll"?value.oldStatus===null&&value.newStatus==="active":value.oldStatus==="active"&&value.newStatus===(value.kind==="tombstone"?"tombstoned":"quarantined")), +} as const +/** Capture a small plain batch before any IO. Never evaluate accessors or + * serializers, retain caller objects, or let request code run under the key. */ +export function captureBandwidthNodeProofBatch(input:readonly BandwidthNodeProofInput[]){ + let bytes=0,nodes=0 + const copy=(value:unknown,depth=0):unknown=>{ + if(++nodes>512||depth>8)fail() + if(value===null||typeof value==="boolean"||typeof value==="number"&&Number.isFinite(value))return value + if(typeof value==="string"){bytes+=Buffer.byteLength(value);if(bytes>65536)fail();return value} + if(!value||typeof value!=="object"||types.isProxy(value))return fail() + const array=Array.isArray(value),descriptors=Object.getOwnPropertyDescriptors(value) + if(Object.getPrototypeOf(value)!==(array?Array.prototype:Object.prototype)||Reflect.ownKeys(value).some(key=>typeof key!=="string"))fail() + if(array){ + const length=descriptors.length?.value + if(!Number.isSafeInteger(length)||length<0||length>64||Object.keys(descriptors).length!==length+1)fail() + return Object.freeze(Array.from({length},(_,index)=>{const field=descriptors[String(index)];if(!field||!("value"in field)||!field.enumerable)fail();return copy(field.value,depth+1)})) + } + const result:Record={} + for(const [name,field]of Object.entries(descriptors)){ + if(!("value"in field)||!field.enumerable||name==="__proto__")fail() + bytes+=Buffer.byteLength(name);if(bytes>65536)fail() + Object.defineProperty(result,name,{value:copy(field.value,depth+1),enumerable:true}) + } + return Object.freeze(result) + } + const entries=copy(input) as BandwidthNodeProofInput[] + if(!Array.isArray(entries)||entries.length<1||entries.length>4)fail() + let total=0 + return Object.freeze(entries.map(entry=>{ + if(!entry||typeof entry!=="object"||Object.keys(entry).sort().join(",")!=="purpose,value"||typeof entry.purpose!=="string"||!Object.hasOwn(bandwidthNodeProofShapes,entry.purpose))fail() + const text=canonicalBandwidthNodeValue(bandwidthNodeProofShapes[entry.purpose as keyof typeof bandwidthNodeProofShapes].parse(entry.value));total+=Buffer.byteLength(text) + if(total>65536)fail() + return Object.freeze({purpose:entry.purpose,text}) + })) +} + diff --git a/backend/src/services/bandwidth-node-public-metadata.test.ts b/backend/src/services/bandwidth-node-public-metadata.test.ts new file mode 100644 index 00000000..c24dce77 --- /dev/null +++ b/backend/src/services/bandwidth-node-public-metadata.test.ts @@ -0,0 +1,150 @@ +/** Real signed bootstrap files + explicitly controlled public core verifier. + * No pool/SQL/issuer-secret/event-key/Q/native or strong-source acceptance. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign, verify } from "node:crypto" +import { lstat, mkdtemp, readFile, realpath, rm, unlink, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, join } from "node:path" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" +import type { createConfiguredBandwidthControlSource, BandwidthPublicNodeBootstrapMetadata } from "./bandwidth-control-source.js" +import { bandwidthNodeBootstrapEnvelopeSchema, bandwidthNodeBootstrapSchema, canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import type { BandwidthScope } from "./bandwidth-admission.js" + +test("node public bootstrap is secret-free fresh metadata, never strong authority", async t => { + const root = await realpath(tmpdir()), folder = await mkdtemp(join(root, "bw-node-public-")), keys = generateKeyPairSync("ed25519") + const bootstrapFile = join(folder, "bootstrap.json"), issuerKeyFile = join(folder, "missing-issuer-secret"), eventKeyFile = join(folder, "missing-event-secret") + const issuerKeySha256 = "b".repeat(64), installationId = randomUUID() + const initialBootstrap = bandwidthNodeBootstrapSchema.parse({ format: "mmapp-bandwidth-node-bootstrap-v2", installationId, issuerKeySha256, + quiescenceProof: "c".repeat(64), registrySchemaSourceSha256: "d".repeat(64), registryCatalogSha256: "e".repeat(64), attestationRef: "controlled public fixture; no current Q or SQL proof" }) + const initialSource = { installationId, controlGeneration: randomUUID(), sourceRecordSha256: "f".repeat(64) } + let source = { ...initialSource }, calls = 0, connects = 0, strongVerifications = 0 + let hook: ((call: number, scope: BandwidthScope) => Promise) | undefined + let after: ((call: number) => Promise) | undefined + const all: Array> = [] + const scope = (ms = 2000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + ms, ...(signal ? { signal } : {}) }) + const core = { + async connect() { connects++; throw Error("No pool borrow permitted") }, + async verifyNodeBootstrap() { strongVerifications++; throw Error("No strong verification permitted") }, + async inspectPublicNodeBootstrap(raw: unknown, requested: BandwidthScope): Promise { + const call = ++calls; await hook?.(call, requested) + const envelope = bandwidthNodeBootstrapEnvelopeSchema.parse(raw), bytes = Buffer.from(envelope.payload, "base64url") + assert(verify(null, bytes, keys.publicKey, Buffer.from(envelope.signature, "base64url"))) + const bootstrap = bandwidthNodeBootstrapSchema.parse(JSON.parse(bytes.toString("utf8"))) + assert.equal(canonicalBandwidthNodeValue(bootstrap), bytes.toString("utf8")) + const result = Object.freeze({ format: "mmapp-bandwidth-public-node-metadata-v1" as const, bootstrap: Object.freeze(bootstrap), source: Object.freeze({ ...source }), + publicDatabase: Object.freeze({ databaseName: "public_fixture", runtimeRole: "public_reader", ownerRole: "public_owner", databaseOid: 1234, catalogSha256: "a".repeat(64) }) }) + await after?.(call); return result + }, + } + const make = (expected = issuerKeySha256) => { + const value = createConfiguredBandwidthNodeSource(core as unknown as ReturnType, { bootstrapFile, issuerKeyFile, issuerKeySha256: expected }) + all.push(value); return value + } + const publish = async (value = initialBootstrap) => { + const payload = Buffer.from(canonicalBandwidthNodeValue(value)) + await writeFile(bootstrapFile, JSON.stringify({ format: "mmapp-bandwidth-node-bootstrap-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) + } + const reset = async () => { calls = 0; hook = undefined; after = undefined; source = { ...initialSource }; await publish() } + try { + await t.test("constructor performs no IO; missing secrets still allow immutable public projection without lease", async () => { + const node = make(); assert.equal(calls + connects + strongVerifications, 0) + await assert.rejects(readFile(bootstrapFile), { code: "ENOENT" }); await reset() + const result = await node.inspectPublicBootstrap(scope()) + assert.equal(calls, 2); assert.equal(connects + strongVerifications, 0) + assert.equal(result.bootstrapSha256, createHash("sha256").update(canonicalBandwidthNodeValue(initialBootstrap)).digest("hex")) + assert(Object.isFrozen(result) && Object.isFrozen(result.bootstrap) && Object.isFrozen(result.source) && Object.isFrozen(result.publicDatabase)) + assert.deepEqual(Object.keys(result.source).sort(), ["controlGeneration", "installationId", "sourceRecordSha256"]) + assert(!("keyCurrent" in result) && !("quiescenceVerified" in result) && !("catalogVerified" in result)) + await assert.rejects(readFile(issuerKeyFile), { code: "ENOENT" }); await assert.rejects(readFile(eventKeyFile), { code: "ENOENT" }) + }) + await t.test("every call rereads public bootstrap/source and captures its fixed verifier", async () => { + await reset(); const node = make(), first = await node.inspectPublicBootstrap(scope()) + source.controlGeneration = randomUUID(); source.sourceRecordSha256 = "1".repeat(64) + await publish({ ...initialBootstrap, attestationRef: "new signed PUBLIC statement; no execution grant" }) + const original = core.inspectPublicNodeBootstrap + try { + core.inspectPublicNodeBootstrap = async () => { throw Error("Replacement core method") } + const second = await node.inspectPublicBootstrap(scope()) + assert.notEqual(second.bootstrapSha256, first.bootstrapSha256); assert.notEqual(second.source.controlGeneration, first.source.controlGeneration); assert.equal(calls, 4) + } finally { core.inspectPublicNodeBootstrap = original } + }) + await t.test("bootstrap/source drift during public span and issuer mismatch cannot publish", async () => { + for (const fault of ["bootstrap", "source", "issuer"] as const) { + await reset(); const node = make(fault === "issuer" ? "9".repeat(64) : issuerKeySha256) + after = async call => { if (call === 1) { if (fault === "bootstrap") await publish({ ...initialBootstrap, quiescenceProof: "2".repeat(64) }); if (fault === "source") source.sourceRecordSha256 = "3".repeat(64) } } + await assert.rejects(node.inspectPublicBootstrap(scope())); assert.equal(connects + strongVerifications, 0) + } + }) + await t.test("missing/tampered bootstrap and accessor/foreign/expired scopes fail before trusted core", async () => { + await reset(); const node = make(); await unlink(bootstrapFile) + await assert.rejects(node.inspectPublicBootstrap(scope())); assert.equal(calls, 0) + await writeFile(bootstrapFile, "{"); await assert.rejects(node.inspectPublicBootstrap(scope())); assert.equal(calls, 0) + await publish(); const raw = JSON.parse(await readFile(bootstrapFile, "utf8")); raw.signature = "a".repeat(86); await writeFile(bootstrapFile, JSON.stringify(raw)) + await assert.rejects(node.inspectPublicBootstrap(scope())); assert.equal(calls, 1) + await reset(); let getters = 0 + const getter = { get deadlineAt() { getters++; return Date.now() + 2000 } } + for (const input of [getter, { ...scope(), extra: true }, Object.assign(Object.create(null), scope()), { deadlineAt: Date.now() - 1 }]) await assert.rejects(node.inspectPublicBootstrap(input)) + assert.equal(getters, 0); assert.equal(calls, 0) + const caller = new AbortController(); caller.abort(); await assert.rejects(node.inspectPublicBootstrap(scope(2000, caller.signal))); assert.equal(calls, 0) + }) + await t.test("original cancellation/close rejects pending core and consumes late results", async () => { + for (const cancel of ["abort", "close"] as const) { + await reset(); const node = make(), caller = new AbortController() + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + let observed: AbortSignal | undefined + hook = async (_call, requested) => { observed = requested.signal; entered(); await held } + const input = scope(2000, caller.signal), pending = node.inspectPublicBootstrap(input); void pending.catch(() => {}) + await started; input.deadlineAt += 60000; input.signal = new AbortController().signal + if (cancel === "abort") caller.abort(); else node.close() + await assert.rejects(pending); assert(observed?.aborted); release(); await delay(0) + assert.equal(calls, 1) + if (cancel === "close") await assert.rejects(node.inspectPublicBootstrap(scope())) + } + }) + await t.test("scope/signal proxies run no traps; native signal operations ignore own hostile overrides", async () => { + await reset(); const node = make(); let traps = 0, overrides = 0 + const trap = () => { traps++; throw Error("No proxy trap permitted") } + const proxy = new Proxy(scope(), { getPrototypeOf: trap, ownKeys: trap, getOwnPropertyDescriptor: trap, get: trap }) + await assert.rejects(node.inspectPublicBootstrap(proxy)); assert.equal(traps, 0); assert.equal(calls, 0) + const proxySignal = new Proxy(new AbortController().signal, { get: trap, getPrototypeOf: trap }) + await assert.rejects(node.inspectPublicBootstrap(scope(2000, proxySignal))); assert.equal(traps, 0); assert.equal(calls, 0) + const caller = new AbortController() + Object.defineProperties(caller.signal, { + aborted: { get() { overrides++; throw Error("No caller getter") } }, + addEventListener: { value() { overrides++; throw Error("No caller add") } }, + removeEventListener: { value() { overrides++; throw Error("No caller remove") } }, + }) + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + hook = async () => { entered(); await held } + const pending = node.inspectPublicBootstrap(scope(2000, caller.signal)); void pending.catch(() => {}) + await started; caller.abort(); await assert.rejects(pending); release(); await delay(0) + assert.equal(overrides, 0); assert.equal(calls, 1) + }) + await t.test("two rounds share the first source cap of at most10000ms", async () => { + await reset(); const node = make(), input = scope(60000), began = Date.now(), seen: BandwidthScope[] = [] + hook = async (_call, requested) => { seen.push(requested) } + await node.inspectPublicBootstrap(input) + assert.equal(seen.length, 2); assert.equal(seen[0], seen[1]); assert(seen[0].deadlineAt <= began + 10000); assert(seen[0].deadlineAt < input.deadlineAt) + }) + await t.test("original monotonic deadline cannot renew through mutation or backward wall jump", async () => { + await reset(); const node = make(), originalNow = Date.now + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + hook = async () => { entered(); await held } + const input = scope(30), pending = node.inspectPublicBootstrap(input); void pending.catch(() => {}) + try { + await started; input.deadlineAt = originalNow() + 60000; Date.now = () => originalNow() - 60000 + await assert.rejects(pending); assert.equal(calls, 1) + } finally { Date.now = originalNow; release(); await delay(0) } + }) + assert.equal(connects + strongVerifications, 0) + } finally { + for (const node of all) node.close() + const actual = await realpath(folder); assert.equal(dirname(actual), root); assert.match(basename(actual), /^bw-node-public-/); assert((await lstat(folder)).isDirectory()) + await rm(actual, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/bandwidth-node-public-registry.test.ts b/backend/src/services/bandwidth-node-public-registry.test.ts new file mode 100644 index 00000000..2bd2b830 --- /dev/null +++ b/backend/src/services/bandwidth-node-public-registry.test.ts @@ -0,0 +1,166 @@ +/** Actual node wrapper, real Ed25519 bootstrap files, controlled public core + * lease. No actual PG/SQL/catalog/ownership/Q/native acceptance claim. */ +import assert from "node:assert/strict" +import { generateKeyPairSync, randomUUID, sign, verify } from "node:crypto" +import { lstat, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, join } from "node:path" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" +import type { createConfiguredBandwidthControlSource, BandwidthPublicNodeBootstrapMetadata } from "./bandwidth-control-source.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { bandwidthNodeBootstrapEnvelopeSchema, bandwidthNodeBootstrapSchema, canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" + +test("node public registry metadata owns exactly one controlled public lease", async t => { + const root = await realpath(tmpdir()), folder = await mkdtemp(join(root, "bw-node-public-registry-")), keys = generateKeyPairSync("ed25519") + const bootstrapFile = join(folder, "bootstrap.json"), issuerKeyFile = join(folder, "missing-issuer-secret"), eventKeyFile = join(folder, "missing-event-secret") + const issuerKeySha256 = "b".repeat(64), installationId = randomUUID() + const bootstrap = bandwidthNodeBootstrapSchema.parse({ format: "mmapp-bandwidth-node-bootstrap-v2", installationId, issuerKeySha256, + quiescenceProof: "c".repeat(64), registrySchemaSourceSha256: "d".repeat(64), registryCatalogSha256: "e".repeat(64), attestationRef: "controlled public registry fixture only, not Q or native proof" }) + const initialSource = { installationId, controlGeneration: randomUUID(), sourceRecordSha256: "f".repeat(64) } + let source = { ...initialSource }, backendPid = 777, catalogSha256 = "a".repeat(64), opens = 0, inspections = 0, closes = 0, forbidden = 0, getters = 0 + let onOpen: ((scope: BandwidthScope) => Promise) | undefined, onInspect: ((call: number) => Promise) | undefined, afterInspect: ((call: number) => Promise) | undefined + let resultFault: "accessor" | "nested-accessor" | "proxy" | "extra" | "wrong-bootstrap" | undefined + let leaseFault = false + const scopes: BandwidthScope[] = [], all: Array> = [] + const scope = (ms = 2000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + ms, ...(signal ? { signal } : {}) }) + const decode = (input: unknown) => { + const envelope = bandwidthNodeBootstrapEnvelopeSchema.parse(input), bytes = Buffer.from(envelope.payload, "base64url") + assert(verify(null, bytes, keys.publicKey, Buffer.from(envelope.signature, "base64url"))) + const value = bandwidthNodeBootstrapSchema.parse(JSON.parse(bytes.toString("utf8"))); assert.equal(canonicalBandwidthNodeValue(value), bytes.toString("utf8")); return value + } + const core = { + async connect() { forbidden++; throw Error("No generic/strong source borrow") }, + async verifyNodeBootstrap() { forbidden++; throw Error("No strong verifier") }, + async inspectPublicNodeBootstrap() { forbidden++; throw Error("No second standalone public verifier/borrow") }, + async openPublicRegistryMetadata(envelope: unknown, originalScope: BandwidthScope) { + opens++; scopes.push(originalScope); decode(envelope) + let closed = false + const close = () => { if (!closed) { closed = true; closes++ } } + const lease = { + async inspect(current: unknown) { + assert(!closed); const call = ++inspections; await onInspect?.(call) + const value = decode(current) + const metadata: BandwidthPublicNodeBootstrapMetadata = { format: "mmapp-bandwidth-public-node-metadata-v1", bootstrap: value, source: { ...source }, + publicDatabase: { databaseName: "public_fixture", runtimeRole: "public_reader", ownerRole: "public_owner", databaseOid: 1234, catalogSha256 } } + const result: Record = { format: "mmapp-bandwidth-public-registry-metadata-v1", metadata, backendPid } + if (resultFault === "accessor") Object.defineProperty(result, "metadata", { enumerable: true, get() { getters++; return metadata } }) + if (resultFault === "nested-accessor") Object.defineProperty(metadata.source, "sourceRecordSha256", { enumerable: true, get() { getters++; return source.sourceRecordSha256 } }) + if (resultFault === "extra") result.query = () => { forbidden++; throw Error("No query leaked") } + if (resultFault === "wrong-bootstrap") metadata.bootstrap = { ...value, quiescenceProof: "9".repeat(64) } + await afterInspect?.(call) + if (resultFault === "proxy") return new Proxy(Object.freeze(result), { getPrototypeOf() { getters++; throw Error("No proxy traps") } }) as never + return Object.freeze(result) as never + }, close, + } + await onOpen?.(originalScope) + if (leaseFault) return Object.freeze({ get inspect() { getters++; return lease.inspect }, close }) as never + return Object.freeze(lease) + }, + } + const make = (expected = issuerKeySha256) => { + const value = createConfiguredBandwidthNodeSource(core as unknown as ReturnType, { bootstrapFile, issuerKeyFile, issuerKeySha256: expected }) + all.push(value); return value + } + const publish = async (value = bootstrap) => { + const payload = Buffer.from(canonicalBandwidthNodeValue(value)) + await writeFile(bootstrapFile, JSON.stringify({ format: "mmapp-bandwidth-node-bootstrap-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) + } + const reset = async () => { source = { ...initialSource }; backendPid = 777; catalogSha256 = "a".repeat(64); opens = inspections = closes = getters = 0; + onOpen = onInspect = afterInspect = undefined; resultFault = undefined; leaseFault = false; scopes.length = 0; await publish() } + try { + await t.test("constructor inert, missing secrets succeed, repeated inspect keeps one core lease/PID and immutable public output", async () => { + const node = make(); assert.equal(opens + inspections + forbidden, 0) + await assert.rejects(readFile(bootstrapFile), { code: "ENOENT" }); await reset() + const session = await node.openPublicRegistryMetadata(scope(60000)) + assert.deepEqual(Object.keys(session).sort(), ["close", "inspect"]); assert.equal(opens, 1); assert.equal(inspections, 2) + const first = await session.inspect(), second = await session.inspect() + assert.deepEqual(first, second); assert.equal(first.backendPid, 777); assert.equal(inspections, 6); assert.equal(opens, 1) + assert(Object.isFrozen(session) && Object.isFrozen(first) && Object.isFrozen(first.metadata) && Object.isFrozen(first.metadata.source)) + assert(!("query" in session) && !("keyCurrent" in first) && !("quiescenceVerified" in first)); assert.equal(forbidden, 0) + session.close(); session.close(); assert.equal(closes, 1); await assert.rejects(session.inspect()) + await assert.rejects(readFile(issuerKeyFile), { code: "ENOENT" }); await assert.rejects(readFile(eventKeyFile), { code: "ENOENT" }) + }) + await t.test("current bootstrap/source/PID/public catalog drift retires original session, no second borrow", async () => { + for (const fault of ["bootstrap", "source", "pid", "catalog"] as const) { + await reset(); const node = make(), session = await node.openPublicRegistryMetadata(scope()) + if (fault === "bootstrap") await publish({ ...bootstrap, quiescenceProof: "8".repeat(64) }) + if (fault === "source") source.sourceRecordSha256 = "7".repeat(64) + if (fault === "pid") backendPid++ + if (fault === "catalog") catalogSha256 = "6".repeat(64) + await assert.rejects(session.inspect()); assert.equal(closes, 1); assert.equal(opens, 1); await assert.rejects(session.inspect()) + } + }) + await t.test("changes during either same-lease round/final file read cannot publish", async () => { + for (const fault of ["source-between", "bootstrap-last", "pid-between"] as const) { + await reset(); const node = make() + afterInspect = async call => { + if (fault === "source-between" && call === 1) source.sourceRecordSha256 = "5".repeat(64) + if (fault === "pid-between" && call === 1) backendPid++ + if (fault === "bootstrap-last" && call === 2) await publish({ ...bootstrap, attestationRef: "new signed file appeared during final core SQL round" }) + } + await assert.rejects(node.openPublicRegistryMetadata(scope())); assert.equal(opens, 1); assert.equal(closes, 1) + } + }) + await t.test("catalog error, malformed/proxy/accessor data, wrong bootstrap/issuer close without evaluating getters", async () => { + for (const fault of ["catalog-error", "accessor", "nested-accessor", "proxy", "extra", "wrong-bootstrap", "issuer", "lease-accessor"] as const) { + await reset(); const node = make(fault === "issuer" ? "9".repeat(64) : issuerKeySha256) + if (fault === "catalog-error") onInspect = async () => { throw Error("actual controlled core catalog refusal") } + else if (fault === "lease-accessor") leaseFault = true + else if (fault !== "issuer") resultFault = fault + await assert.rejects(node.openPublicRegistryMetadata(scope())); assert.equal(opens, fault === "issuer" ? 0 : 1); assert.equal(closes, fault === "issuer" ? 0 : 1); assert.equal(getters, 0) + } + }) + await t.test("abort or source.close during core open destroys a late lease before inspect/publication", async () => { + for (const cancel of ["abort", "close"] as const) { + await reset(); const node = make(), caller = new AbortController() + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + onOpen = async () => { entered(); await held } + const pending = node.openPublicRegistryMetadata(scope(2000, caller.signal)); void pending.catch(() => {}) + await started; if (cancel === "abort") caller.abort(); else node.close() + await assert.rejects(pending); release(); await delay(0); assert.equal(opens, 1); assert.equal(closes, 1); assert.equal(inspections, 0) + } + }) + await t.test("cancel/close/busy pending inspect retires once; late continuation performs no more core IO", async () => { + for (const cancel of ["abort", "close", "busy", "source-close"] as const) { + await reset(); const node = make(), caller = new AbortController(), session = await node.openPublicRegistryMetadata(scope(2000, caller.signal)) + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + onInspect = async () => { entered(); await held } + const pending = session.inspect(); void pending.catch(() => {}); await started + if (cancel === "abort") caller.abort(); else if (cancel === "close") session.close(); else if (cancel === "source-close") node.close(); else await assert.rejects(session.inspect()) + await assert.rejects(pending); release(); await delay(0); assert.equal(closes, 1); assert.equal(opens, 1); assert.equal(inspections, 3) + await assert.rejects(session.inspect()) + } + }) + await t.test("hostile scope/signal inputs run no traps; original signal/deadline remain pinned", async () => { + await reset(); const node = make(); let traps = 0 + const trap = () => { traps++; throw Error("No caller trap") } + await assert.rejects(node.openPublicRegistryMetadata(new Proxy(scope(), { getPrototypeOf: trap, ownKeys: trap }))) + await assert.rejects(node.openPublicRegistryMetadata({ get deadlineAt() { traps++; return Date.now() + 2000 } })) + assert.equal(traps, 0); assert.equal(opens, 0) + const caller = new AbortController() + Object.defineProperties(caller.signal, { aborted: { get: trap }, addEventListener: { value: trap }, removeEventListener: { value: trap } }) + const input = scope(60000, caller.signal), began = Date.now(), session = await node.openPublicRegistryMetadata(input) + assert(scopes[0].deadlineAt <= began + 10000); input.deadlineAt += 60000; input.signal = new AbortController().signal + caller.abort(); await assert.rejects(session.inspect()); assert.equal(closes, 1); assert.equal(traps, 0) + }) + await t.test("original monotonic expiry and backward wall do not revive a blocked open", async () => { + await reset(); const node = make(), originalNow = Date.now + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + onOpen = async () => { entered(); await held } + const input = scope(30), pending = node.openPublicRegistryMetadata(input); void pending.catch(() => {}) + try { await started; input.deadlineAt = originalNow() + 60000; Date.now = () => originalNow() - 60000; await assert.rejects(pending) } + finally { Date.now = originalNow; release(); await delay(0) } + assert.equal(closes, 1); assert.equal(inspections, 0) + }) + assert.equal(forbidden, 0) + } finally { + for (const node of all) node.close() + const actual = await realpath(folder); assert.equal(dirname(actual), root); assert.match(basename(actual), /^bw-node-public-registry-/); assert((await lstat(folder)).isDirectory()) + await rm(actual, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/bandwidth-node-registry-batch.test.ts b/backend/src/services/bandwidth-node-registry-batch.test.ts new file mode 100644 index 00000000..4ffb0ead --- /dev/null +++ b/backend/src/services/bandwidth-node-registry-batch.test.ts @@ -0,0 +1,157 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createHmac, randomUUID } from "node:crypto" +import { bandwidthRegistryEnrollmentSql, bandwidthRegistrySnapshotSql, parseBandwidthRegistrySnapshot, bandwidthRegistrySnapshotProofs, type BandwidthRegistryEnrollmentBatch, type BandwidthRegistryKind } from "./bandwidth-node-registry-batch.js" +import { createBandwidthNodeRegistry } from "./bandwidth-node-registry.js" +import { createBandwidthExternalNodeRegistry, BandwidthExternalNodeRegistryUnknown } from "./bandwidth-external-node-registry.js" +import type { BandwidthNodeProofInput } from "./bandwidth-node-source.js" + +type Row=Record +const canonical=(v:unknown):string=>Array.isArray(v)?"["+v.map(canonical).join(",")+"]":v&&typeof v==="object"?"{"+Object.entries(v).sort(([a],[b])=>ab?1:0).map(([k,v])=>JSON.stringify(k)+":"+canonical(v)).join(",")+"}":JSON.stringify(v) +const mac=(purpose:string,value:unknown)=>createHmac("sha256","inert-batch-candidate-fixture-key").update("mmapp-bandwidth-node-"+purpose+"-v1:"+canonical(value)).digest("hex") +const root={installationId:randomUUID(),issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64)} +const scope=()=>({deadlineAt:Date.now()+2000}) +const material=(kind:BandwidthRegistryKind="managed"):BandwidthRegistryEnrollmentBatch=>({kind,subject:kind==="managed"?71:randomUUID(),insertNode:true,nodeId:randomUUID(),claimProof:"a".repeat(64),nodeMac:"b".repeat(64),incarnation:randomUUID(),aliasMac:"c".repeat(64),eventId:randomUUID(),authorityProof:"d".repeat(64),eventMac:"e".repeat(64)}) +function fixture(atomic=false){ + const nodes:Row[]=[],aliases:Row[]=[],events:Row[]=[],queries:string[]=[],releases:boolean[]=[],proofs:Array<{purpose:string;value:Row|unknown[];mac:string}>=[],occupancy={status:"unknown",owner:"original-attempt"},tokens=new WeakMap;expiresAt:number}>() + let revoked=false,dropDoCommand=false,loseCommit=false,afterBatch=false,finalAuthority=false,finalInspections=0,revokeBeforeCommit=false + let proofHook:((purpose:string)=>void)|undefined,queryHook:((sql:string)=>void)|undefined + let batchCalls=0,batchHook:(()=>Promise)|undefined,batchFault:"short"|"long"|"malformed"|undefined + const expiry=Date.now()+60000 + const authorize=async()=>{if(revoked)throw Error("original grant revoked");return{...root,authorityProof:"d".repeat(64),authorityGeneration:"fixture-original-grant",expiresAt:expiry}} + const inspect=(kind:BandwidthRegistryKind)=>async(token:unknown)=>{const source=token&&typeof token==="object"?tokens.get(token):undefined;assert(source&&source.kind===kind) + if(revokeBeforeCommit&&finalAuthority&&++finalInspections===3)revoked=true + return{version:kind==="managed"?1:2,...(kind==="managed"?{serverId:Number(source.id)}:{peerRef:{kind:"external",enrollmentId:String(source.id)}}),sourceProof:"e".repeat(64),authorityGeneration:"fixture-source",routingEndpointEpoch:root.installationId,observedAt:1,expiresAt:source.expiresAt,claims:source.claims,physicalUniquenessVerified:false,cloneResistanceVerified:false} as never + } + const connect=async()=>{const saved=structuredClone({nodes,aliases,events});let committed=false + const restore=()=>{for(const [target,before]of[[nodes,saved.nodes],[aliases,saved.aliases],[events,saved.events]])target.splice(0,target.length,...before)} + return{release(unsafe?:boolean){releases.push(unsafe===true);if(!committed)restore()},async query(sql:string,values:unknown[]=[]){ + queries.push(sql);const result=(rows:Row[]=[],command?:string)=>({rows:structuredClone(rows),command}) + if(sql==="BEGIN")return result([],"BEGIN") + if(sql==="ROLLBACK"){restore();return result([],"ROLLBACK")} + if(sql==="COMMIT"){committed=true;if(loseCommit)throw Error("fixture loss AFTER actual simulated commit");return result([],"COMMIT")} + if(sql.includes("set_config")||sql.includes("pg_advisory_xact_lock"))return result([],"SELECT") + if(sql.startsWith("SELECT installation_id")){if(afterBatch)finalAuthority=true;queryHook?.(sql);return result([{installation_id:root.installationId,issuer_key_sha256:root.issuerKeySha256,quiescence_proof:root.quiescenceProof,status:"active"}],"SELECT")} + const external=sql.includes("bandwidth_external_node_aliases"),kind=external?"external":"managed",id=String(values[0]) + if(sql.startsWith("WITH aliases AS MATERIALIZED")){ + const found=aliases.filter(a=>a.kind===kind&&a.subject===id),current=nodes.filter(n=>found.some(a=>a.node_id===n.node_id)),chain=events.filter(e=>e.alias_kind===kind&&e.subject===id).map(({alias_kind:_k,subject:_id,...event})=>event) + return result([{aliases:found.map(({kind:_k,...a})=>a),nodes:current,events:chain,claim_nodes:values[1]===null?[]:nodes.filter(n=>n.claim_proof===values[1])}],"SELECT") + } + if(sql.startsWith("DO $mmapp_bandwidth_register$")){ + const latest=(purpose:string)=>{const proof=[...proofs].reverse().find(p=>p.purpose===purpose);assert(proof);return proof} + const n=latest("node"),a=latest(external?"external-alias":"alias"),e=latest(external?"external-event":"event"),nm=n.value as Row,am=a.value as Row,em=e.value as Row,subject=String(external?am.enrollmentId:am.serverId) + // Inert model only. It checks that the exact signed values are present in + // the candidate's fixed DO text; actual PL/pgSQL visibility needs owned PG. + for(const value of [nm.nodeId,nm.claimProof,n.mac,am.incarnation,a.mac,em.eventId,e.mac])assert(sql.includes(String(value))) + if(sql.includes("IF true THEN")){assert(!nodes.some(node=>node.claim_proof===nm.claimProof));nodes.push({node_id:nm.nodeId,claim_proof:nm.claimProof,enrollment_mac:n.mac})} + else assert(nodes.some(node=>node.node_id===nm.nodeId&&node.claim_proof===nm.claimProof&&node.enrollment_mac===n.mac)) + assert(!aliases.some(alias=>alias.kind===kind&&alias.subject===subject));aliases.push({kind,subject,incarnation:am.incarnation,node_id:am.nodeId,status:"active",record_mac:a.mac}) + events.push({alias_kind:kind,subject,event_id:em.eventId,incarnation:em.incarnation,node_id:em.nodeId,kind:"enroll",old_status:null,new_status:"active",authority_proof:em.authorityProof,event_mac:e.mac});afterBatch=true;queryHook?.(sql);return result([],dropDoCommand?undefined:"DO") + } + if(sql.startsWith("UPDATE")&&sql.includes("_aliases SET")){const row=aliases.find(a=>a.kind===kind&&a.subject===id&&a.status==="active"&&a.record_mac===values[3]);assert(row);row.status=values[1];row.record_mac=values[2];return result([{[external?"enrollment_id":"server_id"]:external?id:Number(id)}],"UPDATE")} + if(sql.startsWith("INSERT")&&sql.includes("_events(")){const external=sql.includes("bandwidth_external_node_events"),kind=external?"external":"managed";events.push({alias_kind:kind,subject:String(values[1]),event_id:values[0],incarnation:values[2],node_id:values[3],kind:values[4],old_status:values[5],new_status:values[6],authority_proof:values[7],event_mac:values[8]});return result([{event_id:values[0]}],"INSERT")} + throw Error("unexpected inert candidate SQL") + }} + } + const keyedProof=async(purpose:string,value:unknown)=>{const digest=mac(purpose,value);proofs.push({purpose,value:structuredClone(value) as Row,mac:digest});proofHook?.(purpose);return digest} + const batch=atomic?{keyedProofs:async(inputs:readonly BandwidthNodeProofInput[])=>{ + batchCalls++;assert(Object.isFrozen(inputs));assert(inputs.every(Object.isFrozen)) + await batchHook?.() + const values=inputs.map(({purpose,value})=>{const digest=mac(purpose,value);proofs.push({purpose,value:structuredClone(value) as Row,mac:digest});return digest}) + // No await between pure MACs; the hook models source revocation before return. + inputs.forEach(({purpose})=>proofHook?.(purpose)) + if(batchFault==="short")return values.slice(1) + if(batchFault==="long")return [...values,"a".repeat(64)] + if(batchFault==="malformed")return values.map((value,index)=>index===1?"bad":value) + return Object.freeze(values) + }}:{} + const managed=createBandwidthNodeRegistry({now:Date.now,connect,authorize,inspect:inspect("managed"),keyedProof,...batch}),external=createBandwidthExternalNodeRegistry({now:Date.now,connect,authorize,inspect:inspect("external"),keyedProof,...batch}) + const observe=(kind:BandwidthRegistryKind,id:number|string,claims:Array<{kind:"system-id"|"software-id";value:string}>=[{kind:"system-id",value:"SAME-NODE"}])=>{const token={};tokens.set(token,{kind,id,claims,expiresAt:expiry});return token} + return{managed,external,observe,nodes,aliases,events,queries,releases,proofs,occupancy,dropDo:()=>{dropDoCommand=true},loseCommit:()=>{loseCommit=true},revoke:()=>{revoked=true},proofHook:(hook:typeof proofHook)=>{proofHook=hook},queryHook:(hook:typeof queryHook)=>{queryHook=hook},revokeBeforeCommit:()=>{revokeBeforeCommit=true},batchCalls:()=>batchCalls,batchHook:(hook:typeof batchHook)=>{batchHook=hook},batchFault:(fault:typeof batchFault)=>{batchFault=fault}} +} + +test("fixed managed/external DO is invoker-only sequential SPI with STRICT returning and exact expected IDs",()=>{ + for(const kind of ["managed","external"] as const)for(const insertNode of [true,false]){const p={...material(kind),insertNode},sql=bandwidthRegistryEnrollmentSql(p) + assert(sql.startsWith("DO $mmapp_bandwidth_register$"));assert(sql.includes(`IF ${insertNode} THEN`));assert.equal((sql.match(/INSERT INTO/g)||[]).length,3);assert.equal((sql.match(/RETURNING .*INTO STRICT/g)||[]).length,3);assert(sql.includes("SELECT node_id,claim_proof,enrollment_mac INTO STRICT"));assert(sql.indexOf("INSERT INTO mmapp_control.bandwidth_nodes"){ + for(const field of ["nodeId","incarnation","eventId","claimProof","nodeMac","aliasMac","authorityProof","eventMac"]){const p=material();(p as unknown as Row)[field]="'; DROP TABLE x; --";assert.throws(()=>bandwidthRegistryEnrollmentSql(p))} + for(const value of [0,-1,1.5,Number.MAX_SAFE_INTEGER+1,NaN,Infinity,"71",randomUUID()])assert.throws(()=>bandwidthRegistryEnrollmentSql({...material(),subject:value as number})) + assert.throws(()=>bandwidthRegistryEnrollmentSql({...material("external"),subject:71}));assert.throws(()=>bandwidthRegistryEnrollmentSql({...material(),insertNode:"true" as unknown as boolean}));assert.throws(()=>bandwidthRegistryEnrollmentSql({...material(),extra:true} as BandwidthRegistryEnrollmentBatch)) + let reads=0;const p=material();Object.defineProperty(p,"nodeMac",{enumerable:true,get(){reads++;return"a".repeat(64)}});assert.throws(()=>bandwidthRegistryEnrollmentSql(p));assert.equal(reads,0) +}) +test("snapshot SQL variants remain fixed and bind IDs/hash; pure parser refuses missing/duplicate/current-node/event inconsistencies",()=>{ + for(const kind of ["managed","external"] as const)for(const lock of [true,false]){const sql=bandwidthRegistrySnapshotSql(kind,lock);assert(sql.includes("WITH aliases AS MATERIALIZED"));assert(sql.includes("claim_proof=$2::pg_catalog.text"));assert.equal(sql.includes("FOR UPDATE"),lock);assert(!/\b(?:INSERT|DELETE|CREATE|ALTER|TRUNCATE)\b/.test(sql));assert(!sql.includes("bandwidth_node_occupancy"))} + assert.throws(()=>bandwidthRegistrySnapshotSql("injected" as BandwidthRegistryKind,true));assert.throws(()=>bandwidthRegistrySnapshotSql("managed",1 as unknown as boolean)) + const p=material(),empty={aliases:[],nodes:[],events:[],claim_nodes:[]};assert(Object.isFrozen(parseBandwidthRegistrySnapshot([empty],"managed",71,p.claimProof))) + const node={node_id:p.nodeId,claim_proof:p.claimProof,enrollment_mac:p.nodeMac},snapshot=parseBandwidthRegistrySnapshot([{aliases:[{subject:"71",incarnation:p.incarnation,node_id:p.nodeId,status:"active",record_mac:p.aliasMac}],nodes:[node],events:[{event_id:p.eventId,incarnation:p.incarnation,node_id:p.nodeId,kind:"enroll",old_status:null,new_status:"active",authority_proof:p.authorityProof,event_mac:p.eventMac}],claim_nodes:[node]}],"managed",71,p.claimProof) + const descriptors=bandwidthRegistrySnapshotProofs("managed",snapshot,root);assert.deepEqual(descriptors.map(value=>value.purpose),["alias","node","event"]);assert(Object.isFrozen(descriptors));assert(descriptors.every(value=>Object.isFrozen(value)&&Object.isFrozen(value.value))) + for(const data of [[],[empty,empty],[{...empty,nodes:[{node_id:p.nodeId,claim_proof:p.claimProof,enrollment_mac:p.nodeMac}]}],[{...empty,events:[{}]}],[{...empty,claim_nodes:[{node_id:p.nodeId,claim_proof:"f".repeat(64),enrollment_mac:p.nodeMac}]}]])assert.throws(()=>parseBandwidthRegistrySnapshot(data,"managed",71,p.claimProof)) +}) +test("candidate managed/external register uses one snapshot SELECT and one DO; same typed claim issuer preserves node and unknown",async()=>{ + const f=fixture(),managed=await f.managed.register(f.observe("managed",71),scope()),externalId=randomUUID(),external=await f.external.register(f.observe("external",externalId),scope()) + assert.equal(managed.canonicalNodeId,external.canonicalNodeId);assert.notEqual(managed.incarnation,external.incarnation);assert.equal(f.nodes.length,1);assert.equal(f.aliases.length,2);assert.equal(f.events.length,2) + assert.equal(f.queries.filter(q=>q.startsWith("WITH aliases AS MATERIALIZED")).length,2);assert.equal(f.queries.filter(q=>q.startsWith("DO $mmapp_bandwidth_register$")).length,2);assert.equal(f.queries.length,18);assert.deepEqual(f.occupancy,{status:"unknown",owner:"original-attempt"});assert(!f.queries.some(q=>q.includes("bandwidth_node_occupancy"))) +}) +test("existing alias, resolve and retirement reuse one pure snapshot proof path; terminal alias never re-enrolls or changes canonical identity",async()=>{ + const f=fixture(),id=randomUUID(),token=f.observe("external",id),first=await f.external.register(token,scope()) + assert.equal((await f.external.register(token,scope())).outcome,"existing");assert.equal((await f.external.resolve(token,scope())).canonicalNodeId,first.canonicalNodeId) + assert.equal((await f.external.retire(id,"tombstone",scope())).status,"tombstoned");assert.equal((await f.external.retire(id,"tombstone",scope())).outcome,"existing") + await assert.rejects(f.external.register(token,scope()));await assert.rejects(f.external.resolve(token,scope()));await assert.rejects(f.external.retire(id,"quarantine",scope())) + const replacement=await f.external.register(f.observe("external",randomUUID()),scope());assert.equal(replacement.canonicalNodeId,first.canonicalNodeId);assert.notEqual(replacement.incarnation,first.incarnation);assert.equal(f.nodes.length,1) +}) +test("snapshot node/alias/event MAC or full typed claim drift refuses without replacement node/event",async()=>{ + for(const fault of ["node","alias","event","typed-claim"]){const f=fixture(),token=f.observe("managed",71);await f.managed.register(token,scope());if(fault==="node")f.nodes[0].enrollment_mac="f".repeat(64);if(fault==="alias")f.aliases[0].record_mac="f".repeat(64);if(fault==="event")f.events[0].event_mac="f".repeat(64) + await assert.rejects(f.managed.register(fault==="typed-claim"?f.observe("managed",71,[{kind:"software-id",value:"SAME-NODE"}]):token,scope()));assert.equal(f.nodes.length,1);assert.equal(f.events.length,1) + } +}) +test("rows-only DO completion cannot acknowledge the batch; outer transaction rolls back and does not COMMIT",async()=>{ + const f=fixture();f.dropDo();await assert.rejects(f.external.register(f.observe("external",randomUUID()),scope()));assert.equal(f.nodes.length,0);assert.equal(f.aliases.length,0);assert.equal(f.events.length,0);assert(!f.queries.includes("COMMIT"));assert(f.queries.includes("ROLLBACK")) +}) +test("original grant revocation during event signing or after actual batch dispatch prevents COMMIT and preserves preexisting unknown",async()=>{ + for(const boundary of ["event-proof","after-do"]){const f=fixture();if(boundary==="event-proof")f.proofHook(purpose=>{if(purpose==="external-event")f.revoke()});else f.queryHook(sql=>{if(sql.startsWith("DO $mmapp_bandwidth_register$"))f.revoke()}) + await assert.rejects(f.external.register(f.observe("external",randomUUID()),scope()));assert(!f.queries.includes("COMMIT"));assert.equal(f.nodes.length,0);assert.equal(f.events.length,0);assert.equal(f.queries.filter(q=>q.startsWith("DO $mmapp_bandwidth_register$")).length,boundary==="after-do"?1:0);assert.deepEqual(f.occupancy,{status:"unknown",owner:"original-attempt"}) + } +}) +test("the original final pre-COMMIT inspection/authority boundary remains effective after DO, with no weakened late-grant fault",async()=>{ + const f=fixture();f.revokeBeforeCommit();await assert.rejects(f.managed.register(f.observe("managed",71),scope()));assert.equal(f.queries.filter(q=>q.startsWith("DO $mmapp_bandwidth_register$")).length,1);assert(!f.queries.includes("COMMIT"));assert.deepEqual(f.releases,[true]);assert.equal(f.nodes.length,0);assert.equal(f.aliases.length,0) +}) +test("existing external lost COMMIT ACK remains unknown/no replay even though node+alias+event writes are batched",async()=>{ + const f=fixture();f.loseCommit();await assert.rejects(f.external.register(f.observe("external",randomUUID()),scope()),error=>error instanceof BandwidthExternalNodeRegistryUnknown&&error.automaticReplay===false);assert.equal(f.nodes.length,1);assert.equal(f.aliases.length,1);assert.equal(f.events.length,1);assert.equal(f.queries.filter(q=>q==="COMMIT").length,1);assert(!f.queries.includes("ROLLBACK")) +}) + +test("trusted atomic signer preserves scalar domains and exact enrollment data for both participant kinds",async()=>{ + for(const kind of ["managed","external"] as const){ + const f=fixture(true),id=kind==="managed"?71:randomUUID(),token=f.observe(kind,id) + const receipt=await (kind==="managed"?f.managed:f.external).register(token,scope()) + assert.equal(receipt.status,"active");assert.equal(f.batchCalls(),1);assert.equal(f.proofs.length,4) + assert.equal(f.proofs[0].purpose,"claim") + assert.equal(f.queries.filter(q=>q.startsWith("DO $mmapp_bandwidth_register$")).length,1) + assert.equal(f.nodes.length,1);assert.equal(f.events.length,1) + assert.deepEqual(f.occupancy,{status:"unknown",owner:"original-attempt"}) + } +}) + +test("atomic signer result corruption and original authority loss refuse before enrollment DO",async()=>{ + for(const kind of ["managed","external"] as const)for(const fault of ["short","long","malformed","revoke"] as const){ + const f=fixture(true),id=kind==="managed"?71:randomUUID() + if(fault==="revoke")f.proofHook(purpose=>{if(purpose.endsWith("event"))f.revoke()});else f.batchFault(fault) + await assert.rejects((kind==="managed"?f.managed:f.external).register(f.observe(kind,id),scope())) + assert.equal(f.batchCalls(),1);assert.equal(f.nodes.length,0);assert.equal(f.events.length,0) + assert(!f.queries.some(q=>q.startsWith("DO $mmapp_bandwidth_register$")||q==="COMMIT")) + } +}) + +test("aborted pending atomic signing destroys its lease; late MACs cannot resume a write",async()=>{ + const f=fixture(true),controller=new AbortController() + let release!:()=>void,entered!:()=>void + const waiting=new Promise(done=>{entered=done}),held=new Promise(done=>{release=done}) + f.batchHook(async()=>{entered();await held}) + const pending=f.external.register(f.observe("external",randomUUID()),{deadlineAt:Date.now()+2000,signal:controller.signal}) + await waiting;controller.abort();await assert.rejects(pending) + release();await new Promise(done=>setTimeout(done,10)) + assert.deepEqual(f.releases,[true]);assert.equal(f.nodes.length,0) + assert(!f.queries.some(q=>q.startsWith("DO $mmapp_bandwidth_register$")||q==="COMMIT")) +}) diff --git a/backend/src/services/bandwidth-node-registry-batch.ts b/backend/src/services/bandwidth-node-registry-batch.ts new file mode 100644 index 00000000..40084133 --- /dev/null +++ b/backend/src/services/bandwidth-node-registry-batch.ts @@ -0,0 +1,102 @@ +/** Fixed invoker-only SQL shapes for the existing registry transaction. No + * DDL/key/bootstrap changes, dynamic SQL or occupancy access. */ +import { z } from "zod" + +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid().regex(/^[a-f0-9-]+$/) +const status=z.enum(["active","tombstoned","quarantined"]) +const aliasSchema=z.object({subject:z.string().min(1).max(64),incarnation:uuid,node_id:uuid,status,record_mac:hash}).strict() +const nodeSchema=z.object({node_id:uuid,claim_proof:hash,enrollment_mac:hash}).strict() +const eventSchema=z.object({event_id:uuid,incarnation:uuid,node_id:uuid,kind:z.enum(["enroll","tombstone","quarantine"]),old_status:z.literal("active").nullable(),new_status:status,authority_proof:hash,event_mac:hash}).strict() +const snapshotSchema=z.object({aliases:z.array(aliasSchema).max(1),nodes:z.array(nodeSchema).max(1),events:z.array(eventSchema).max(2),claim_nodes:z.array(nodeSchema).max(1)}).strict() +export type BandwidthRegistryKind="managed"|"external" +export type BandwidthRegistrySnapshot=Readonly> +export class BandwidthRegistryBatchRefused extends Error{constructor(){super("Bandwidth registry batch refused")}} +function fail():never{throw new BandwidthRegistryBatchRefused()} +function freeze(value:T):T{if(value&&typeof value==="object"){Object.values(value).forEach(freeze);Object.freeze(value)}return value} +function subject(kind:BandwidthRegistryKind,value:unknown):string{ + if(kind==="managed"){if(!Number.isSafeInteger(value)||Number(value)<1)fail();return String(value)} + if(kind!=="external"||!uuid.safeParse(value).success)fail();return value as string +} +const query=(kind:BandwidthRegistryKind,lock:boolean)=>{ + const external=kind==="external",id=external?"enrollment_id":"server_id",aliases=external?"bandwidth_external_node_aliases":"bandwidth_node_aliases",events=external?"bandwidth_external_node_events":"bandwidth_node_events",cast=external?"uuid":"int8" + return `WITH aliases AS MATERIALIZED ( + SELECT ${id}::pg_catalog.text AS subject,incarnation::pg_catalog.text,node_id::pg_catalog.text,status,record_mac + FROM mmapp_control.${aliases} WHERE ${id}=$1::pg_catalog.${cast} LIMIT 2${lock?" FOR UPDATE":""} +), nodes AS MATERIALIZED ( + SELECT n.node_id::pg_catalog.text,n.claim_proof,n.enrollment_mac + FROM mmapp_control.bandwidth_nodes n JOIN aliases a ON n.node_id=a.node_id::pg_catalog.uuid +), events AS MATERIALIZED ( + SELECT event_id::pg_catalog.text,incarnation::pg_catalog.text,node_id::pg_catalog.text,kind,old_status,new_status,authority_proof,event_mac + FROM mmapp_control.${events} WHERE ${id}=$1::pg_catalog.${cast} ORDER BY kind LIMIT 3 +), claim_nodes AS MATERIALIZED ( + SELECT node_id::pg_catalog.text,claim_proof,enrollment_mac FROM mmapp_control.bandwidth_nodes + WHERE $2::pg_catalog.text IS NOT NULL AND claim_proof=$2::pg_catalog.text LIMIT 2 +) +SELECT COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(a)) FROM aliases a),'[]'::pg_catalog.jsonb) AS aliases, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(n)) FROM nodes n),'[]'::pg_catalog.jsonb) AS nodes, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(e)) FROM events e),'[]'::pg_catalog.jsonb) AS events, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(c)) FROM claim_nodes c),'[]'::pg_catalog.jsonb) AS claim_nodes` +} +const managed=query("managed",false),managedLock=query("managed",true),external=query("external",false),externalLock=query("external",true) +export function bandwidthRegistrySnapshotSql(kind:BandwidthRegistryKind,lock:boolean):string{ + if(typeof lock!=="boolean"||kind!=="managed"&&kind!=="external")fail() + return kind==="managed"?lock?managedLock:managed:lock?externalLock:external +} +/** Pure snapshot shape/link validation. Cryptographic comparisons below use + * the same original claim/node/alias/event MAC domains as v1. */ +export function parseBandwidthRegistrySnapshot(rows:unknown,kind:BandwidthRegistryKind,id:number|string,claimProof:string|null):BandwidthRegistrySnapshot{ + const wanted=subject(kind,id);if(claimProof!==null&&!hash.safeParse(claimProof).success)fail() + if(!Array.isArray(rows)||rows.length!==1)fail() + let value:z.infer;try{value=snapshotSchema.parse(rows[0])}catch{return fail()} + const alias=value.aliases[0],node=value.nodes[0],candidate=value.claim_nodes[0] + if(!alias){if(node||value.events.length)fail()} + else{ + if(alias.subject!==wanted||!node||node.node_id!==alias.node_id)fail() + const terminal=alias.status!=="active",kinds=value.events.map(event=>event.kind).sort().join(",") + if(value.events.length!==(terminal?2:1)||kinds!==(terminal?alias.status==="tombstoned"?"enroll,tombstone":"enroll,quarantine":"enroll"))fail() + for(const event of value.events)if(event.incarnation!==alias.incarnation||event.node_id!==alias.node_id||event.old_status!==(event.kind==="enroll"?null:"active")||event.new_status!==(event.kind==="enroll"?"active":alias.status))fail() + } + if(candidate&&(claimProof===null||candidate.claim_proof!==claimProof))fail() + if(claimProof!==null&&node?.claim_proof===claimProof&&(!candidate||candidate.node_id!==node.node_id||candidate.enrollment_mac!==node.enrollment_mac))fail() + return freeze(value) +} +const rootSchema=z.object({installationId:uuid,issuerKeySha256:hash,quiescenceProof:hash}).strict() +export function bandwidthRegistrySnapshotProofs(kind:BandwidthRegistryKind,snapshot:BandwidthRegistrySnapshot,authority:z.infer){ + const root=rootSchema.parse(authority),alias=snapshot.aliases[0],node=snapshot.nodes[0];if(!alias||!node)fail() + const id=kind==="managed"?{serverId:Number(alias.subject)}:{enrollmentId:alias.subject} + subject(kind,kind==="managed"?Number(alias.subject):alias.subject) + return freeze([{purpose:kind==="managed"?"alias":"external-alias",value:{...root,...id,incarnation:alias.incarnation,nodeId:alias.node_id,status:alias.status},expected:alias.record_mac}, + {purpose:"node",value:{...root,nodeId:node.node_id,claimProof:node.claim_proof},expected:node.enrollment_mac}, + ...snapshot.events.map(event=>({purpose:kind==="managed"?"event":"external-event",value:{...root,eventId:event.event_id,...id,incarnation:event.incarnation,nodeId:event.node_id,kind:event.kind,oldStatus:event.old_status,newStatus:event.new_status,authorityProof:event.authority_proof},expected:event.event_mac}))]) +} +const batchSchema=z.object({kind:z.enum(["managed","external"]),subject:z.union([z.number().int().positive().safe(),uuid]),insertNode:z.boolean(),nodeId:uuid,claimProof:hash,nodeMac:hash,incarnation:uuid,aliasMac:hash,eventId:uuid,authorityProof:hash,eventMac:hash}).strict() +export type BandwidthRegistryEnrollmentBatch=z.infer +/** Only safeinteger, UUID, hash64 and boolean values enter fixed SQL literals. + * No native claims, names, endpoints, SQL callbacks, identifiers or model text. */ +export function bandwidthRegistryEnrollmentSql(raw:BandwidthRegistryEnrollmentBatch):string{ + if(!raw||typeof raw!=="object"||Object.getPrototypeOf(raw)!==Object.prototype||Reflect.ownKeys(raw).length!==Object.keys(raw).length||Object.values(Object.getOwnPropertyDescriptors(raw)).some(field=>!("value"in field)||!field.enumerable))fail() + let p:BandwidthRegistryEnrollmentBatch;try{p=batchSchema.parse(raw)}catch{return fail()} + const id=subject(p.kind,p.subject),external=p.kind==="external",idColumn=external?"enrollment_id":"server_id",idType=external?"uuid":"int8",aliases=external?"bandwidth_external_node_aliases":"bandwidth_node_aliases",events=external?"bandwidth_external_node_events":"bandwidth_node_events" + const literal=(v:string,t:"uuid"|"text")=>`'${v}'::pg_catalog.${t}`,node=literal(p.nodeId,"uuid"),incarnation=literal(p.incarnation,"uuid"),event=literal(p.eventId,"uuid"),claim=literal(p.claimProof,"text"),nodeMac=literal(p.nodeMac,"text"),aliasMac=literal(p.aliasMac,"text"),authority=literal(p.authorityProof,"text"),eventMac=literal(p.eventMac,"text"),subjectLiteral=external?literal(id,"uuid"):`${id}::pg_catalog.int8` + return `DO $mmapp_bandwidth_register$ +DECLARE got_node pg_catalog.uuid; got_claim pg_catalog.text; got_node_mac pg_catalog.text; + got_subject pg_catalog.${idType}; got_incarnation pg_catalog.uuid; got_alias_node pg_catalog.uuid; got_alias_mac pg_catalog.text; + got_event pg_catalog.uuid; got_event_subject pg_catalog.${idType}; got_event_incarnation pg_catalog.uuid; got_event_node pg_catalog.uuid; got_authority pg_catalog.text; got_event_mac pg_catalog.text; +BEGIN + IF ${p.insertNode?"true":"false"} THEN + INSERT INTO mmapp_control.bandwidth_nodes(node_id,claim_proof,enrollment_mac) VALUES(${node},${claim},${nodeMac}) + RETURNING node_id,claim_proof,enrollment_mac INTO STRICT got_node,got_claim,got_node_mac; + IF got_node IS DISTINCT FROM ${node} OR got_claim IS DISTINCT FROM ${claim} OR got_node_mac IS DISTINCT FROM ${nodeMac} THEN RAISE EXCEPTION 'bandwidth registry node mismatch' USING ERRCODE='23514'; END IF; + ELSE + SELECT node_id,claim_proof,enrollment_mac INTO STRICT got_node,got_claim,got_node_mac FROM mmapp_control.bandwidth_nodes WHERE claim_proof=${claim}; + IF got_node IS DISTINCT FROM ${node} OR got_claim IS DISTINCT FROM ${claim} OR got_node_mac IS DISTINCT FROM ${nodeMac} THEN RAISE EXCEPTION 'bandwidth registry existing node mismatch' USING ERRCODE='23514'; END IF; + END IF; + INSERT INTO mmapp_control.${aliases}(${idColumn},incarnation,node_id,status,record_mac) VALUES(${subjectLiteral},${incarnation},${node},'active',${aliasMac}) + RETURNING ${idColumn},incarnation,node_id,record_mac INTO STRICT got_subject,got_incarnation,got_alias_node,got_alias_mac; + IF got_subject IS DISTINCT FROM ${subjectLiteral} OR got_incarnation IS DISTINCT FROM ${incarnation} OR got_alias_node IS DISTINCT FROM ${node} OR got_alias_mac IS DISTINCT FROM ${aliasMac} THEN RAISE EXCEPTION 'bandwidth registry alias mismatch' USING ERRCODE='23514'; END IF; + INSERT INTO mmapp_control.${events}(event_id,${idColumn},incarnation,node_id,kind,old_status,new_status,authority_proof,event_mac) VALUES(${event},${subjectLiteral},${incarnation},${node},'enroll',NULL,'active',${authority},${eventMac}) + RETURNING event_id,${idColumn},incarnation,node_id,authority_proof,event_mac INTO STRICT got_event,got_event_subject,got_event_incarnation,got_event_node,got_authority,got_event_mac; + IF got_event IS DISTINCT FROM ${event} OR got_event_subject IS DISTINCT FROM ${subjectLiteral} OR got_event_incarnation IS DISTINCT FROM ${incarnation} OR got_event_node IS DISTINCT FROM ${node} OR got_authority IS DISTINCT FROM ${authority} OR got_event_mac IS DISTINCT FROM ${eventMac} THEN RAISE EXCEPTION 'bandwidth registry event mismatch' USING ERRCODE='23514'; END IF; +END +$mmapp_bandwidth_register$;` +} diff --git a/backend/src/services/bandwidth-node-registry-catalog.test.ts b/backend/src/services/bandwidth-node-registry-catalog.test.ts new file mode 100644 index 00000000..a293d37f --- /dev/null +++ b/backend/src/services/bandwidth-node-registry-catalog.test.ts @@ -0,0 +1,120 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_NODE_REGISTRY_DDL } from "./bandwidth-node-registry-schema.js" +import { observeBandwidthNodeRegistryCatalog, BandwidthNodeRegistryCatalogRefused } from "./bandwidth-node-registry-catalog.js" + +type Row=Record +type Item={section:string;raw_json:string|null} +const refused=(error:unknown)=>error instanceof BandwidthNodeRegistryCatalogRefused +/** Inert catalog fixture derived from the checked-in component, not an external + * supplied digest. Actual PostgreSQL deparser/types are covered separately. */ +function fixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_node_authority","bandwidth_nodes","bandwidth_node_aliases","bandwidth_node_events"] + const functions=["bandwidth_node_authority_guard","bandwidth_node_immutable_guard","bandwidth_node_alias_guard","bandwidth_node_event_guard","bandwidth_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_node_authority:[["p",["id"]]],bandwidth_nodes:[["p",["node_id"]],["u",["claim_proof"]]],bandwidth_node_aliases:[["p",["server_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_node_events:[["p",["event_id"]],["u",["server_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_node_aliases","node_id","bandwidth_nodes"],["bandwidth_node_events","server_id","bandwidth_node_aliases"],["bandwidth_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +const run=(rows:Item[],live=()=>{})=>observeBandwidthNodeRegistryCatalog({query:async()=>({rows})},live) +function mutate(rows:Item[],section:string,predicate:(row:Row)=>boolean,change:(row:Row)=>void){ + const item=rows.find(item=>item.section===section&&predicate(JSON.parse(item.raw_json!)as Row))!;assert.ok(item,section) + const row=JSON.parse(item.raw_json!)as Row;change(row);item.raw_json=JSON.stringify(row) +} + +test("fixed component validates and produces deterministic SHA independent of row/object order and CRLF",async()=>{ + const rows=fixture(),events:string[]=[] + const first=await observeBandwidthNodeRegistryCatalog({query:async(text,values)=>{events.push("SQL");assert.deepEqual(values,[]);assert.ok(text.includes("pg_catalog.pg_proc"));assert.ok(text.includes("n.nspname='mmapp_control'"));assert.ok(text.includes("LIMIT 81"));return{rows}}},()=>events.push("live")) + assert.match(first,/^[a-f0-9]{64}$/);assert.deepEqual(events,["live","SQL","live"]) + const shuffled=rows.reverse().map(item=>{const row=JSON.parse(item.raw_json!);if(item.section==="functions")row.body=row.body.replace(/\n/g,"\r\n");return{...item,raw_json:JSON.stringify(Object.fromEntries(Object.entries(row).reverse()))}}) + assert.equal(await run(shuffled),first) +}) + +test("function replacement, SECURITY DEFINER, overload, changed arguments or search path cannot be blessed by a new digest",async()=>{ + for(const name of["bandwidth_node_alias_guard","bandwidth_admission_reject_truncate"])for(const change of[(row:Row)=>{row.body="BEGIN RETURN NEW; END;"},(row:Row)=>{row.security_definer=true},(row:Row)=>{row.language="sql"},(row:Row)=>{row.config=["search_path=pg_catalog, public"]},(row:Row)=>{row.argument_types=["text"]},(row:Row)=>{row.return_type="text"},(row:Row)=>{row.owner="app_restorer"},(row:Row)=>{row.returns_set=true},(row:Row)=>{row.has_support=true}]){ + const rows=fixture();mutate(rows,"functions",row=>row.name===name,change);await assert.rejects(run(rows),refused) + } + const overload=fixture();overload.push({...overload.find(item=>item.section==="functions")!});await assert.rejects(run(overload),refused) +}) + +test("all ten guards must be ALWAYS and retain exact function, trigger type and deferred ownership",async()=>{ + for(const change of[(row:Row)=>{row.enabled="O"},(row:Row)=>{row.type=29},(row:Row)=>{row.function_name="bandwidth_admission_event_guard"},(row:Row)=>{row.deferrable=true},(row:Row)=>{row.condition="false"}]){ + const rows=fixture();mutate(rows,"triggers",row=>row.name==="bandwidth_node_alias_guard",change);await assert.rejects(run(rows),refused) + } + const rows=fixture();mutate(rows,"triggers",row=>row.name==="bandwidth_node_alias_consistency",row=>{row.initially_deferred=false});await assert.rejects(run(rows),refused) + const internal=fixture();mutate(internal,"triggers",row=>row.internal===true,row=>{row.enabled="D"});await assert.rejects(run(internal),refused) +}) + +test("column type/nullability/default/collation and complete four-table footprint are fixed",async()=>{ + for(const change of[(row:Row)=>{row.type_name="int8"},(row:Row)=>{row.not_null=false},(row:Row)=>{row.generated="s"},(row:Row)=>{row.default_expr="1"},(row:Row)=>{row.collation_name="default"}]){ + const rows=fixture();mutate(rows,"columns",row=>row.table_name==="bandwidth_node_authority"&&row.name==="status",change);await assert.rejects(run(rows),refused) + } + for(const section of["namespace","tables","columns","functions","triggers"]){const rows=fixture(),index=rows.findIndex(item=>item.section===section);rows.splice(index,1);await assert.rejects(run(rows),refused)} + const rows=fixture();mutate(rows,"tables",row=>row.name==="bandwidth_nodes",row=>{row.row_security=true});await assert.rejects(run(rows),refused) +}) + +test("structural constraints, exact uniqueness/index shape and unrewritten tables are required",async()=>{ + for(const section of["constraints","indexes"]){const rows=fixture();rows.splice(rows.findIndex(item=>item.section===section),1);await assert.rejects(run(rows),refused)} + const fk=fixture();mutate(fk,"constraints",row=>row.type==="f",row=>{row.delete_action="c"});await assert.rejects(run(fk),refused) + const index=fixture();mutate(index,"indexes",row=>row.table_name==="bandwidth_node_aliases"&&row.primary===true,row=>{row.predicate="false"});await assert.rejects(run(index),refused) + for(const section of["rules","policies"]){const rows=fixture();rows.push({section,raw_json:JSON.stringify({name:"bypass"})});await assert.rejects(run(rows),refused)} +}) + +test("observed constraint expressions are pinned by the digest without claiming arbitrary SQL equivalence",async()=>{ + const rows=fixture(),before=await run(rows) + mutate(rows,"constraints",row=>row.type==="c",row=>{row.definition="CHECK (changed_expression)"}) + assert.notEqual(await run(rows),before,"the fixed issuer must reject drift against its signed observation") +}) + +test("NULL budget sentinels, unknown tags, row/byte overflow and malformed JSON fail closed",async()=>{ + for(const extra of[{section:"functions",raw_json:null},{section:"foreign",raw_json:"{}"},{section:"functions",raw_json:"{"},{section:"functions",raw_json:JSON.stringify({body:"x".repeat(65536)})}])await assert.rejects(run([...fixture(),extra]),refused) + await assert.rejects(run(Array.from({length:231},()=>({section:"namespace",raw_json:"{}"}))),refused) +}) + +test("late catalog results and query failures honor the live boundary without exposing provider errors",async()=>{ + let active=true,resolve!:(value:{rows:Item[]})=>void + const pending=new Promise<{rows:Item[]}>(done=>{resolve=done}) + const result=observeBandwidthNodeRegistryCatalog({query:async()=>pending},()=>{if(!active)throw new Error("closed owner")}) + active=false;const rejected=assert.rejects(result,/closed owner/);resolve({rows:fixture()});await rejected + let checks=0 + await assert.rejects(observeBandwidthNodeRegistryCatalog({query:async()=>{throw new Error("SECRET")}},()=>{checks++}),error=>refused(error)&&!String(error).includes("SECRET"));assert.equal(checks,2) +}) diff --git a/backend/src/services/bandwidth-node-registry-catalog.ts b/backend/src/services/bandwidth-node-registry-catalog.ts new file mode 100644 index 00000000..bd5b8fda --- /dev/null +++ b/backend/src/services/bandwidth-node-registry-catalog.ts @@ -0,0 +1,292 @@ +import { createHash } from "node:crypto" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_NODE_REGISTRY_DDL } from "./bandwidth-node-registry-schema.js" +import { observeBandwidthExternalNodeRegistryCatalog,validateBandwidthExternalNodeRegistryCatalogRows } from "./bandwidth-external-node-registry-catalog.js" + +/** Fixed component verification plus a canonical observation digest. No default + * I/O, DDL, role assertion, readiness or generation issuance. Constraint/index + * definitions are captured for the fixed issuer's signed pin; their structural + * checks below are not a general proof of arbitrary SQL expression equivalence. + * The core truncate function referenced by registry guards is independently + * checked against the core literal here, including its owner/path/signature. */ +export interface BandwidthNodeRegistryCatalogSql { query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> } +export class BandwidthNodeRegistryCatalogRefused extends Error { + readonly code = "BANDWIDTH_NODE_REGISTRY_CATALOG_REFUSED" + constructor() { super("Bandwidth node registry catalog refused") } +} +type Row = Record +const tableNames = ["bandwidth_node_authority", "bandwidth_nodes", "bandwidth_node_aliases", "bandwidth_node_events"] as const +const functionNames = ["bandwidth_node_authority_guard", "bandwidth_node_immutable_guard", "bandwidth_node_alias_guard", "bandwidth_node_event_guard", "bandwidth_node_alias_consistency_guard", "bandwidth_admission_reject_truncate"] as const +const normalizeBody = (value: string) => value.replace(/\r\n?/g, "\n").trim() +function expectedBodies(): Map { + const found = new Map() + for (const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL, BANDWIDTH_NODE_REGISTRY_DDL]) { + for (const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)) { + if (!functionNames.includes(match[1] as typeof functionNames[number])) continue + if (found.has(match[1])) refuse() + found.set(match[1], normalizeBody(match[2])) + } + } + if (found.size !== functionNames.length || functionNames.some(name => !found.has(name))) refuse() + return found +} +const sections = { namespace: 1, tables: 4, columns: 26, functions: 6, triggers: 22, constraints: 80, indexes: 9, rules: 0, policies: 0 } as const +export const BANDWIDTH_NODE_REGISTRY_CATALOG_SQL = `WITH component AS ( + SELECT c.* FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + WHERE n.nspname='mmapp_control' AND c.relname=ANY(ARRAY['bandwidth_node_authority','bandwidth_nodes','bandwidth_node_aliases','bandwidth_node_events']) +), catalog AS MATERIALIZED ( + SELECT 'namespace'::text AS section,pg_catalog.to_jsonb(q)::text AS raw_json FROM ( + SELECT n.nspname::text AS name,o.rolname::text AS owner FROM pg_catalog.pg_namespace n + JOIN pg_catalog.pg_roles o ON o.oid=n.nspowner WHERE n.nspname='mmapp_control' LIMIT 2) q + UNION ALL SELECT 'tables',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS name,o.rolname::text AS owner,c.relkind::text AS kind,c.relpersistence::text AS persistence, + c.relrowsecurity AS row_security,c.relforcerowsecurity AS force_row_security,c.relispartition AS is_partition,c.relreplident::text AS replica_identity,c.reloptions AS options, + EXISTS(SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhrelid=c.oid) AS has_parent,EXISTS(SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhparent=c.oid) AS has_children, + c.reloftype<>0 AS typed_table,(SELECT am.amname::text FROM pg_catalog.pg_am am WHERE am.oid=c.relam) AS access_method + FROM component c JOIN pg_catalog.pg_roles o ON o.oid=c.relowner ORDER BY c.relname LIMIT 5) q + UNION ALL SELECT 'columns',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,a.attnum::integer AS position,a.attname::text AS name,tn.nspname::text AS type_namespace,t.typname::text AS type_name, + a.attnotnull AS not_null,a.attidentity::text AS identity,a.attgenerated::text AS generated, + cn.nspname::text AS collation_namespace,co.collname::text AS collation_name, + pg_catalog.pg_get_expr(d.adbin,d.adrelid,true) AS default_expr + FROM component c JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum>0 + JOIN pg_catalog.pg_type t ON t.oid=a.atttypid JOIN pg_catalog.pg_namespace tn ON tn.oid=t.typnamespace + LEFT JOIN pg_catalog.pg_collation co ON co.oid=a.attcollation LEFT JOIN pg_catalog.pg_namespace cn ON cn.oid=co.collnamespace + LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=c.oid AND d.adnum=a.attnum + WHERE NOT a.attisdropped ORDER BY c.relname,a.attnum LIMIT 27) q + UNION ALL SELECT 'functions',pg_catalog.to_jsonb(q)::text FROM ( + SELECT p.proname::text AS name,o.rolname::text AS owner,l.lanname::text AS language,p.prosrc AS body,p.prosecdef AS security_definer, + p.proisstrict AS strict,p.provolatile::text AS volatility,p.proleakproof AS leakproof,p.prokind::text AS kind, + p.proparallel::text AS parallel,p.pronargdefaults::integer AS argument_defaults,p.provariadic<>0 AS variadic,p.proconfig AS config, + p.proretset AS returns_set,p.prosupport<>0 AS has_support,p.proallargtypes IS NULL AND p.proargmodes IS NULL AS input_only,p.proargnames AS argument_names, + rt.typname::text AS return_type,rn.nspname::text AS return_namespace, + ARRAY(SELECT t.typname::text FROM pg_catalog.unnest(p.proargtypes::oid[]) WITH ORDINALITY a(type_oid,position) JOIN pg_catalog.pg_type t ON t.oid=a.type_oid ORDER BY a.position) AS argument_types, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(p.proargtypes::oid[]) WITH ORDINALITY a(type_oid,position) JOIN pg_catalog.pg_type t ON t.oid=a.type_oid JOIN pg_catalog.pg_namespace n ON n.oid=t.typnamespace ORDER BY a.position) AS argument_namespaces + FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace JOIN pg_catalog.pg_language l ON l.oid=p.prolang + JOIN pg_catalog.pg_roles o ON o.oid=p.proowner JOIN pg_catalog.pg_type rt ON rt.oid=p.prorettype JOIN pg_catalog.pg_namespace rn ON rn.oid=rt.typnamespace + WHERE n.nspname='mmapp_control' AND p.proname=ANY(ARRAY['bandwidth_node_authority_guard','bandwidth_node_immutable_guard','bandwidth_node_alias_guard','bandwidth_node_event_guard','bandwidth_node_alias_consistency_guard','bandwidth_admission_reject_truncate']) + ORDER BY p.proname,p.oid LIMIT 7) q + UNION ALL SELECT 'triggers',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,CASE WHEN t.tgisinternal THEN NULL ELSE t.tgname::text END AS name,t.tgisinternal AS internal, + t.tgtype::integer AS type,t.tgenabled::text AS enabled,t.tgdeferrable AS deferrable,t.tginitdeferred AS initially_deferred, + t.tgconstraint<>0 AS is_constraint,con.conname::text AS constraint_name, + pn.nspname::text AS function_namespace,p.proname::text AS function_name,t.tgnargs::integer AS argument_count, + pg_catalog.encode(t.tgargs,'hex') AS arguments,t.tgattr::text AS columns,pg_catalog.pg_get_expr(t.tgqual,t.tgrelid,true) AS condition, + t.tgoldtable::text AS old_table,t.tgnewtable::text AS new_table + FROM component c JOIN pg_catalog.pg_trigger t ON t.tgrelid=c.oid JOIN pg_catalog.pg_proc p ON p.oid=t.tgfoid + JOIN pg_catalog.pg_namespace pn ON pn.oid=p.pronamespace LEFT JOIN pg_catalog.pg_constraint con ON con.oid=t.tgconstraint + ORDER BY c.relname,t.tgname LIMIT 23) q + UNION ALL SELECT 'constraints',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,k.conname::text AS name,k.contype::text AS type,k.convalidated AS validated,k.condeferrable AS deferrable,k.condeferred AS initially_deferred, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(k.conkey) WITH ORDINALITY x(attnum,position) JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum=x.attnum ORDER BY x.position) AS keys, + fn.nspname::text AS foreign_namespace,fc.relname::text AS foreign_table, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(k.confkey) WITH ORDINALITY x(attnum,position) JOIN pg_catalog.pg_attribute a ON a.attrelid=k.confrelid AND a.attnum=x.attnum ORDER BY x.position) AS foreign_keys, + k.confupdtype::text AS update_action,k.confdeltype::text AS delete_action,k.confmatchtype::text AS match_type, + pg_catalog.pg_get_constraintdef(k.oid,true) AS definition + FROM component c JOIN pg_catalog.pg_constraint k ON k.conrelid=c.oid + LEFT JOIN pg_catalog.pg_class fc ON fc.oid=k.confrelid LEFT JOIN pg_catalog.pg_namespace fn ON fn.oid=fc.relnamespace + ORDER BY c.relname,k.conname LIMIT 81) q + UNION ALL SELECT 'indexes',pg_catalog.to_jsonb(q)::text FROM ( + SELECT c.relname::text AS table_name,ic.relname::text AS name,i.indisunique AS unique,i.indisprimary AS primary,i.indisvalid AS valid,i.indisready AS ready,i.indislive AS live, + i.indnkeyatts::integer AS key_count,i.indnatts::integer AS attribute_count,am.amname::text AS method, + ARRAY(SELECT a.attname::text FROM pg_catalog.unnest(i.indkey::smallint[]) WITH ORDINALITY x(attnum,position) LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attnum=x.attnum ORDER BY x.position) AS keys, + ARRAY(SELECT co.collname::text FROM pg_catalog.unnest(i.indcollation::oid[]) WITH ORDINALITY x(collation_oid,position) LEFT JOIN pg_catalog.pg_collation co ON co.oid=x.collation_oid ORDER BY x.position) AS collations, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(i.indcollation::oid[]) WITH ORDINALITY x(collation_oid,position) LEFT JOIN pg_catalog.pg_collation co ON co.oid=x.collation_oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid=co.collnamespace ORDER BY x.position) AS collation_namespaces, + ARRAY(SELECT op.opcname::text FROM pg_catalog.unnest(i.indclass::oid[]) WITH ORDINALITY x(class_oid,position) JOIN pg_catalog.pg_opclass op ON op.oid=x.class_oid ORDER BY x.position) AS operator_classes, + ARRAY(SELECT n.nspname::text FROM pg_catalog.unnest(i.indclass::oid[]) WITH ORDINALITY x(class_oid,position) JOIN pg_catalog.pg_opclass op ON op.oid=x.class_oid JOIN pg_catalog.pg_namespace n ON n.oid=op.opcnamespace ORDER BY x.position) AS operator_class_namespaces, + i.indoption::text AS options,pg_catalog.pg_get_expr(i.indexprs,i.indrelid,true) AS expressions,pg_catalog.pg_get_expr(i.indpred,i.indrelid,true) AS predicate, + pg_catalog.pg_get_indexdef(i.indexrelid) AS definition + FROM component c JOIN pg_catalog.pg_index i ON i.indrelid=c.oid JOIN pg_catalog.pg_class ic ON ic.oid=i.indexrelid JOIN pg_catalog.pg_am am ON am.oid=ic.relam + ORDER BY c.relname,ic.relname LIMIT 10) q + UNION ALL SELECT 'rules',pg_catalog.to_jsonb(q)::text FROM (SELECT c.relname::text AS table_name,r.rulename::text AS name FROM component c JOIN pg_catalog.pg_rewrite r ON r.ev_class=c.oid LIMIT 1) q + UNION ALL SELECT 'policies',pg_catalog.to_jsonb(q)::text FROM (SELECT c.relname::text AS table_name,p.polname::text AS name FROM component c JOIN pg_catalog.pg_policy p ON p.polrelid=c.oid LIMIT 1) q +), metered AS ( + SELECT section,raw_json,pg_catalog.sum(pg_catalog.octet_length(raw_json)) OVER(ORDER BY section,raw_json) AS bytes FROM catalog +) SELECT section,CASE WHEN pg_catalog.octet_length(raw_json)<=65536 AND bytes<=1048576 THEN raw_json END AS raw_json FROM metered ORDER BY section,raw_json` + +function refuse(): never { throw new BandwidthNodeRegistryCatalogRefused() } +const plain = (value: unknown): value is Row => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +const arrayEqual = (value: unknown, expected: unknown[]) => Array.isArray(value) && JSON.stringify(value) === JSON.stringify(expected) +function canonical(value: unknown): string { + if (Array.isArray(value)) return "[" + value.map(canonical).join(",") + "]" + if (plain(value)) return "{" + Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" + return JSON.stringify(value) +} +type Column = [name: string, type: string, notNull: boolean, defaultExpr?: string] +const text = (name: string, notNull = true): Column => [name, "text", notNull] +const columns: Record = { + bandwidth_node_authority: [["id","int4",true],["installation_id","uuid",true],text("issuer_key_sha256"),text("quiescence_proof"),text("status"),["created_at","timestamptz",true,"clock_timestamp()"]], + bandwidth_nodes: [["node_id","uuid",true],text("claim_proof"),text("enrollment_mac"),["created_at","timestamptz",true,"clock_timestamp()"]], + bandwidth_node_aliases: [["server_id","int8",true],["incarnation","uuid",true],["node_id","uuid",true],text("status"),text("record_mac"),["created_at","timestamptz",true,"clock_timestamp()"]], + bandwidth_node_events: [["event_id","uuid",true],["server_id","int8",true],["incarnation","uuid",true],["node_id","uuid",true],text("kind"),text("old_status",false),text("new_status"),text("authority_proof"),text("event_mac"),["created_at","timestamptz",true,"clock_timestamp()"]], +} +const triggers: Record = { + bandwidth_node_authority_guard: ["bandwidth_node_authority","bandwidth_node_authority_guard",31,false], + bandwidth_node_immutable_guard: ["bandwidth_nodes","bandwidth_node_immutable_guard",27,false], + bandwidth_node_alias_guard: ["bandwidth_node_aliases","bandwidth_node_alias_guard",31,false], + bandwidth_node_event_guard: ["bandwidth_node_events","bandwidth_node_event_guard",31,false], + bandwidth_node_authority_no_truncate: ["bandwidth_node_authority","bandwidth_admission_reject_truncate",34,false], + bandwidth_nodes_no_truncate: ["bandwidth_nodes","bandwidth_admission_reject_truncate",34,false], + bandwidth_node_aliases_no_truncate: ["bandwidth_node_aliases","bandwidth_admission_reject_truncate",34,false], + bandwidth_node_events_no_truncate: ["bandwidth_node_events","bandwidth_admission_reject_truncate",34,false], + bandwidth_node_alias_consistency: ["bandwidth_node_aliases","bandwidth_node_alias_consistency_guard",29,true], + bandwidth_node_event_consistency: ["bandwidth_node_events","bandwidth_node_alias_consistency_guard",29,true], +} + +export const BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL = BANDWIDTH_NODE_REGISTRY_CATALOG_SQL.replace("ORDER BY c.relname,t.tgname LIMIT 23", "ORDER BY c.relname,t.tgname LIMIT 27") +export const BANDWIDTH_PEER_REGISTRY_INCOMING_SQL = `SELECT c.relname::text AS table_name,k.conname::text AS constraint_name + FROM pg_catalog.pg_constraint k + JOIN pg_catalog.pg_class c ON c.oid=k.conrelid + JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + JOIN pg_catalog.pg_class target ON target.oid=k.confrelid + JOIN pg_catalog.pg_namespace tn ON tn.oid=target.relnamespace + WHERE n.nspname='mmapp_control' AND c.relname=ANY(ARRAY['bandwidth_external_node_aliases','bandwidth_external_node_events']) + AND k.contype='f' AND tn.nspname='mmapp_control' AND target.relname='bandwidth_nodes' + ORDER BY c.relname,k.conname LIMIT 3` + +export async function observeBandwidthNodeRegistryCatalog(sql: BandwidthNodeRegistryCatalogSql, live: () => void): Promise { + return observeManagedCatalog(sql, live) +} + +/** Explicit v2 component: verify the complete external footprint, including its + * four inbound node FK triggers, then retain those exact triggers in the + * managed catalog proof as well. A v1 observer/pin is deliberately unchanged. */ +export async function observeBandwidthPeerRegistryCatalog(sql: BandwidthNodeRegistryCatalogSql, live: () => void): Promise { + live() + const external = await observeBandwidthExternalNodeRegistryCatalog(sql, live) + live() + const result = await sql.query(BANDWIDTH_PEER_REGISTRY_INCOMING_SQL, []) + live() + const incoming = incomingNodeTriggers(result.rows) + const managed = await observeManagedCatalog(sql, live, incoming) + live() + return createHash("sha256").update(canonical({ format: "mmapp-bandwidth-peer-registry-catalog-v2", managed, external })).digest("hex") +} + +function incomingNodeTriggers(rows: unknown): string[] { + if (!Array.isArray(rows) || rows.length !== 2) refuse() + const names = rows.map(row => { + if (!plain(row) || Object.keys(row).sort().join(",") !== "constraint_name,table_name" + || typeof row.constraint_name !== "string" || !row.constraint_name || row.constraint_name.length > 63 + || !["bandwidth_external_node_aliases", "bandwidth_external_node_events"].includes(String(row.table_name))) refuse() + return { table: row.table_name as string, name: row.constraint_name } + }) + if (new Set(names.map(row => row.table)).size !== 2) refuse() + const incoming = names.flatMap(row => [`bandwidth_nodes:${row.name}:RI_FKey_restrict_del:9`, `bandwidth_nodes:${row.name}:RI_FKey_restrict_upd:17`]) + return incoming +} + +export function validateBandwidthPeerRegistryCatalogRows(managedRows: unknown, externalRows: unknown, incomingRows: unknown): string { + const external = validateBandwidthExternalNodeRegistryCatalogRows(externalRows) + const incoming = incomingNodeTriggers(incomingRows) + const managed = validateBandwidthNodeRegistryCatalogRows(managedRows, incoming) + return createHash("sha256").update(canonical({ format: "mmapp-bandwidth-peer-registry-catalog-v2", managed, external })).digest("hex") +} + +async function observeManagedCatalog(sql: BandwidthNodeRegistryCatalogSql, live: () => void, incoming: readonly string[] = []): Promise { + live() + let result: { rows: Row[] } + const statement = incoming.length ? BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL : BANDWIDTH_NODE_REGISTRY_CATALOG_SQL + try { result = await sql.query(statement, []) } catch { live(); refuse() } + live() + return validateBandwidthNodeRegistryCatalogRows(result.rows, incoming) +} + +/** Default remains the strict v1 footprint. V2 incoming identities must be + * derived from the same captured external FK rows by the combined validator. */ +export function validateBandwidthNodeRegistryCatalogRows(rows: unknown, incoming: readonly string[] = []): string { + const limits = { ...sections, triggers: sections.triggers + incoming.length } + const bodies = expectedBodies() + if (!Array.isArray(rows) || rows.length > 230 + incoming.length) refuse() + const found = Object.fromEntries(Object.keys(sections).map(key => [key, [] as Row[]])) as unknown as Record + let bytes = 0 + for (const item of rows) { + if (!plain(item) || Object.keys(item).sort().join(",") !== "raw_json,section" || typeof item.section !== "string" || !Object.hasOwn(sections,item.section) || typeof item.raw_json !== "string") refuse() + bytes += Buffer.byteLength(item.raw_json) + if (Buffer.byteLength(item.raw_json)>65536 || bytes>1048576) refuse() + let parsed: unknown + try { parsed = JSON.parse(item.raw_json) } catch { refuse() } + if (!plain(parsed)) refuse() + const section = item.section as keyof typeof sections + found[section].push(parsed) + if (found[section].length>limits[section]) refuse() + } + if (found.namespace.length!==1 || found.namespace[0].name!=="mmapp_control" || typeof found.namespace[0].owner!=="string") refuse() + if (found.tables.length!==4 || new Set(found.tables.map(row=>row.name)).size!==4) refuse() + for (const row of found.tables) if (!tableNames.includes(row.name as typeof tableNames[number]) || row.owner!==found.namespace[0].owner || row.kind!=="r" || row.persistence!=="p" || row.row_security!==false || row.force_row_security!==false || row.is_partition!==false || row.replica_identity!=="d" || row.options!==null || row.has_parent!==false || row.has_children!==false || row.typed_table!==false || row.access_method!=="heap") refuse() + if (found.columns.length!==26) refuse() + for (const table of tableNames) { + const actual = found.columns.filter(row=>row.table_name===table).sort((a,b)=>Number(a.position)-Number(b.position)) + if (actual.length!==columns[table].length) refuse() + for (const [index,expected] of columns[table].entries()) { + const row=actual[index],collation=expected[1]==="text"?"C":null + const defaultExpr=row.default_expr==="pg_catalog.clock_timestamp()"?"clock_timestamp()":row.default_expr + if (row.position!==index+1 || row.name!==expected[0] || row.type_namespace!=="pg_catalog" || row.type_name!==expected[1] || row.not_null!==expected[2] || row.identity!=="" || row.generated!=="" || row.collation_name!==collation || row.collation_namespace!==(collation?"pg_catalog":null) || defaultExpr!==(expected[3]??null)) refuse() + row.default_expr=defaultExpr + } + } + if (found.functions.length!==6 || new Set(found.functions.map(row=>row.name)).size!==6) refuse() + for (const row of found.functions) { + const args: string[]=[] + if (typeof row.name!=="string" || !bodies.has(row.name) || typeof row.body!=="string" || normalizeBody(row.body)!==bodies.get(row.name) + || row.owner!==found.namespace[0].owner || row.language!=="plpgsql" || row.return_namespace!=="pg_catalog" || row.return_type!=="trigger" + || row.security_definer!==false || row.strict!==false || row.volatility!=="v" || row.leakproof!==false || row.kind!=="f" || row.parallel!=="u" || row.argument_defaults!==0 || row.variadic!==false + || row.returns_set!==false || row.has_support!==false || row.input_only!==true || row.argument_names!==null + || !arrayEqual(row.argument_types,args) || !arrayEqual(row.argument_namespaces,args.map(()=>"pg_catalog")) + || !Array.isArray(row.config) || row.config.length!==1 || typeof row.config[0]!=="string" || row.config[0].replace(/\s/g,"")!=="search_path=pg_catalog,mmapp_control") refuse() + row.body=normalizeBody(row.body) + row.config=["search_path=pg_catalog,mmapp_control"] + } + const userTriggers=found.triggers.filter(row=>row.internal===false),internal=found.triggers.filter(row=>row.internal===true) + if (userTriggers.length!==10 || internal.length!==12+incoming.length || new Set(userTriggers.map(row=>row.name)).size!==10) refuse() + for (const row of found.triggers) { + if (row.argument_count!==0 || row.arguments!=="" || row.columns!=="" || row.condition!==null || row.old_table!==null || row.new_table!==null) refuse() + if (row.internal===false) { + const expected=typeof row.name==="string"?triggers[row.name]:undefined + if (!expected || row.table_name!==expected[0] || row.function_namespace!=="mmapp_control" || row.function_name!==expected[1] || row.type!==expected[2] || row.enabled!=="A" || row.deferrable!==expected[3] || row.initially_deferred!==expected[3] || row.is_constraint!==expected[3] || row.constraint_name!==(expected[3]?row.name:null)) refuse() + } else if (row.internal!==true || row.name!==null || row.function_namespace!=="pg_catalog" || !["RI_FKey_check_ins","RI_FKey_check_upd","RI_FKey_restrict_del","RI_FKey_restrict_upd"].includes(String(row.function_name)) || row.enabled!=="O" || row.is_constraint!==true || row.deferrable!==false || row.initially_deferred!==false) refuse() + } + const checkCounts: Record={bandwidth_node_authority:4,bandwidth_nodes:2,bandwidth_node_aliases:3,bandwidth_node_events:6} + const keySpecs: Record={bandwidth_node_authority:["p:id"],bandwidth_nodes:["p:node_id","u:claim_proof"],bandwidth_node_aliases:["p:server_id","u:incarnation","f:node_id"],bandwidth_node_events:["p:event_id","u:server_id,kind","f:server_id","f:node_id"]} + for (const table of tableNames) { + const actual=found.constraints.filter(row=>row.table_name===table),keys:string[]=[] + if (actual.filter(row=>row.type==="c").length!==checkCounts[table]) refuse() + for (const row of actual) { + if (typeof row.name!=="string" || typeof row.definition!=="string" || !row.definition || row.validated!==true || !Array.isArray(row.keys) || !["p","u","f","c","n","t"].includes(String(row.type))) refuse() + if (row.type==="t") { if (row.deferrable!==true || row.initially_deferred!==true || !Object.hasOwn(triggers,row.name)) refuse() } + else if (row.deferrable!==false || row.initially_deferred!==false) refuse() + if (["p","u","f"].includes(String(row.type))) keys.push(row.type+":"+row.keys.join(",")) + if (row.type==="f") { + const target=arrayEqual(row.keys,["node_id"])?"bandwidth_nodes":table==="bandwidth_node_events"&&arrayEqual(row.keys,["server_id"])?"bandwidth_node_aliases":undefined + if (!target || row.foreign_namespace!=="mmapp_control" || row.foreign_table!==target || !arrayEqual(row.foreign_keys,row.keys) || row.update_action!=="r" || row.delete_action!=="r" || row.match_type!=="s") refuse() + } + const constraintKeys=row.keys + if (row.type==="n" && (constraintKeys.length!==1 || !columns[table].some(column=>column[0]===constraintKeys[0]&&column[2]))) refuse() + } + if (!arrayEqual(keys.sort(),keySpecs[table].sort())) refuse() + } + if (found.constraints.some(row=>!tableNames.includes(row.table_name as typeof tableNames[number]))) refuse() + const expectedInternal:string[]=[...incoming] + for (const fk of found.constraints.filter(row=>row.type==="f")) { + expectedInternal.push(`${fk.table_name}:${fk.name}:RI_FKey_check_ins:5`,`${fk.table_name}:${fk.name}:RI_FKey_check_upd:17`,`${fk.foreign_table}:${fk.name}:RI_FKey_restrict_del:9`,`${fk.foreign_table}:${fk.name}:RI_FKey_restrict_upd:17`) + } + if (!arrayEqual(internal.map(row=>`${row.table_name}:${row.constraint_name}:${row.function_name}:${row.type}`).sort(),expectedInternal.sort())) refuse() + if (!arrayEqual(found.constraints.filter(row=>row.type==="t").map(row=>row.table_name+":"+row.name).sort(),userTriggers.filter(row=>row.is_constraint===true).map(row=>row.table_name+":"+row.name).sort())) refuse() + const indexSpecs: Record={bandwidth_node_authority:["p:id"],bandwidth_nodes:["p:node_id","u:claim_proof"],bandwidth_node_aliases:["p:server_id","u:incarnation","i:node_id"],bandwidth_node_events:["p:event_id","u:server_id,kind","i:node_id"]} + if (found.indexes.length!==9) refuse() + for (const table of tableNames) { + const actual=found.indexes.filter(row=>row.table_name===table),keys:string[]=[] + for (const row of actual) { + if (!Array.isArray(row.keys) || !row.keys.length || typeof row.name!=="string" || typeof row.unique!=="boolean" || typeof row.primary!=="boolean" || row.primary&&!row.unique || row.key_count!==row.keys.length || row.attribute_count!==row.keys.length || row.valid!==true || row.ready!==true || row.live!==true || row.method!=="btree" || row.expressions!==null || row.predicate!==null || row.options!==row.keys.map(()=>"0").join(" ") || typeof row.definition!=="string") refuse() + const expectedColumns=row.keys.map(name=>columns[table].find(column=>column[0]===name)) + if (expectedColumns.some(column=>!column) || !arrayEqual(row.collations,expectedColumns.map(column=>column![1]==="text"?"C":null)) || !arrayEqual(row.collation_namespaces,expectedColumns.map(column=>column![1]==="text"?"pg_catalog":null)) || !arrayEqual(row.operator_classes,expectedColumns.map(column=>column![1]==="uuid"?"uuid_ops":column![1]==="int4"?"int4_ops":column![1]==="int8"?"int8_ops":"text_ops")) || !arrayEqual(row.operator_class_namespaces,expectedColumns.map(()=>"pg_catalog"))) refuse() + keys.push((row.primary===true?"p":row.unique===true?"u":"i")+":"+row.keys.join(",")) + } + if (!arrayEqual(keys.sort(),indexSpecs[table].sort())) refuse() + } + // Sort semantic rows independently of SQL transport order. No physical OID or + // volatile statistics enter the digest; internal trigger names contain OIDs + // and are deliberately represented by their structural/function identity. + const snapshot=Object.fromEntries(Object.entries(found).map(([section,rows])=>[section,rows.map(canonical).sort()])) + return createHash("sha256").update(canonical(snapshot)).digest("hex") +} diff --git a/backend/src/services/bandwidth-node-registry-role.test.ts b/backend/src/services/bandwidth-node-registry-role.test.ts new file mode 100644 index 00000000..c040acdd --- /dev/null +++ b/backend/src/services/bandwidth-node-registry-role.test.ts @@ -0,0 +1,96 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { assertBandwidthNodeRegistryRuntimeRole, BandwidthNodeRegistryRuntimeRoleRefused, type BandwidthNodeRegistryRoleExpected, type BandwidthNodeRegistryRoleSql } from "./bandwidth-node-registry-role.js" + +const expected: BandwidthNodeRegistryRoleExpected = { databaseName: "owned_database", runtimeRole: "bandwidth_runtime", ownerRole: "control_owner" } +function observation() { + return { + databaseName: expected.databaseName, sessionRole: expected.runtimeRole, currentRole: expected.runtimeRole, + runtimeRoleExists: true, ownerRoleExists: true, runtimeFlagsSafe: true, + ownerMembership: false, dangerousMembership: false, + schemaExists: true, schemaOwner: expected.ownerRole, schemaUsage: true, schemaCreate: false, + publicSchemaUsage: false, publicSchemaCreate: false, + tables: ["bandwidth_node_aliases", "bandwidth_nodes", "bandwidth_node_events", "bandwidth_node_authority"].map(name => ({ name, exists: true, ownerRole: expected.ownerRole, ownedByRuntime: false, privilegesExact: true })), + } +} +const deferred = () => { let resolve!: (value: T) => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const refused = (error: unknown) => error instanceof BandwidthNodeRegistryRuntimeRoleRefused && error.code === "BANDWIDTH_NODE_REGISTRY_RUNTIME_ROLE_REFUSED" +async function check(value: unknown) { + return assertBandwidthNodeRegistryRuntimeRole({ query: async () => ({ rows: [{ observation: value }] }) }, expected, () => {}) +} + +test("minimal separate runtime role succeeds with one catalog observation and live fences on both sides", async () => { + const events: string[] = [], calls: Array<{ text: string; values?: unknown[] }> = [] + const sql: BandwidthNodeRegistryRoleSql = { query: async (text, values) => { events.push("SQL"); calls.push({ text, values }); return { rows: [{ observation: observation() }] } } } + assert.equal(await assertBandwidthNodeRegistryRuntimeRole(sql, expected, () => { events.push("live") }), undefined) + assert.deepEqual(events, ["live", "SQL", "live"]); assert.equal(calls.length, 1) + assert.deepEqual(calls[0].values, [expected.databaseName, expected.runtimeRole, expected.ownerRole]) + const text = calls[0].text + for (const catalog of ["pg_roles", "pg_namespace", "pg_class", "has_schema_privilege", "has_table_privilege", "has_any_column_privilege", "aclexplode", "acldefault", "pg_has_role", "current_database"]) assert.ok(text.includes("pg_catalog." + catalog), catalog) + assert.match(text, /pg_has_role\(rt\.oid,r\.oid,'MEMBER'\)/) + for (const privilege of ["TRUNCATE", "REFERENCES", "TRIGGER", " WITH GRANT OPTION"]) assert.ok(text.includes(privilege)) + for (const role of ["pg_read_server_files", "pg_write_server_files", "pg_execute_server_program"]) assert.ok(text.includes(role)) + assert.ok(text.includes("n.nspname='mmapp_control'")); assert.ok(text.includes("'bandwidth_node_authority',ARRAY['SELECT']")) + assert.ok(text.includes("'bandwidth_nodes',ARRAY['SELECT','INSERT']")); assert.ok(text.includes("'bandwidth_node_aliases',ARRAY['SELECT','INSERT','UPDATE']")) + for (const coreTable of ["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events","bandwidth_source_identity"]) assert.ok(!text.includes("'"+coreTable+"'"), "core privileges remain a separate source assertion") + assert.doesNotMatch(text, /\b(?:INSERT INTO|DELETE FROM|ALTER TABLE|CREATE SCHEMA|SET ROLE)\b/) + assert.ok(!text.includes(expected.runtimeRole)); assert.ok(!text.includes(expected.ownerRole)); assert.ok(!text.includes(expected.databaseName)) +}) + +test("session/current role, database, role flags and direct or transitive memberships must all match", async () => { + const failures: Record = { databaseName: "foreign_database", sessionRole: "app_restorer", currentRole: "control_owner", runtimeRoleExists: false, ownerRoleExists: false, runtimeFlagsSafe: false, ownerMembership: true, dangerousMembership: true } + for (const [field, value] of Object.entries(failures)) await assert.rejects(check({ ...observation(), [field]: value }), refused, field) +}) + +test("missing control schema, incorrect ownership and PUBLIC or runtime CREATE/USAGE escalation refuse", async () => { + const failures: Record = { schemaExists: false, schemaOwner: "app_restorer", schemaUsage: false, schemaCreate: true, publicSchemaUsage: true, publicSchemaCreate: true } + for (const [field, value] of Object.entries(failures)) await assert.rejects(check({ ...observation(), [field]: value }), refused, field) +}) + +test("all four registry relations require fixed ownership and exact privileges, including SELECT-only authority", async () => { + for (let index = 0; index < 4; index++) { + for (const change of [{ exists: false }, { ownerRole: expected.runtimeRole }, { ownedByRuntime: true }, { privilegesExact: false }]) { + const value = observation(); Object.assign(value.tables[index], change) + await assert.rejects(check(value), refused) + } + } + const missing = observation(); missing.tables.pop(); await assert.rejects(check(missing), refused) + const duplicate = observation(); duplicate.tables[3] = { ...duplicate.tables[0] }; await assert.rejects(check(duplicate), refused) + const unknown = observation(); unknown.tables[3].name = "public_shadow"; await assert.rejects(check(unknown), refused) +}) + +test("malformed or truthy metadata never substitutes for exact boolean observations", async () => { + for (const value of [null, {}, { ...observation(), runtimeFlagsSafe: "true" }, { ...observation(), schemaCreate: null }, { ...observation(), extraReady: true }, { ...observation(), tables: null }]) await assert.rejects(check(value), refused) + const tableExtra = observation(); Object.assign(tableExtra.tables[0], { bypass: false }); await assert.rejects(check(tableExtra), refused) + for (const rows of [[], [{ observation: observation() }, { observation: observation() }], [{ observation: observation(), extra: true }]]) await assert.rejects(assertBandwidthNodeRegistryRuntimeRole({ query: async () => ({ rows }) }, expected, () => {}), refused) +}) + +test("expected names stay bounded parameters and are captured before async SQL", async () => { + let calls = 0 + const invalid = ["", " ", "x".repeat(64), "x\u0000y", "x\ny", "я".repeat(32)] + for (const name of invalid) await assert.rejects(assertBandwidthNodeRegistryRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, { ...expected, runtimeRole: name }, () => {}), refused) + await assert.rejects(assertBandwidthNodeRegistryRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, { ...expected, ownerRole: expected.runtimeRole }, () => {}), refused) + assert.equal(calls, 0) + const pending = deferred<{ rows: Record[] }>(), mutable = { ...expected } + const result = assertBandwidthNodeRegistryRuntimeRole({ query: async () => pending.promise }, mutable, () => {}) + mutable.ownerRole = "changed"; pending.resolve({ rows: [{ observation: observation() }] }); await result + const quoted = { databaseName: "db'quoted", runtimeRole: "runtime';select", ownerRole: 'owner"quoted' } + await assertBandwidthNodeRegistryRuntimeRole({ query: async (text, values) => { assert.deepEqual(values, [quoted.databaseName, quoted.runtimeRole, quoted.ownerRole]); assert.ok(!text.includes(quoted.runtimeRole)); return { rows: [{ observation: { ...observation(), databaseName: quoted.databaseName, sessionRole: quoted.runtimeRole, currentRole: quoted.runtimeRole, schemaOwner: quoted.ownerRole, tables: observation().tables.map(row => ({ ...row, ownerRole: quoted.ownerRole })) } }] } } }, quoted, () => {}) +}) + +test("expired owners cannot start SQL or accept a late successful observation", async () => { + let calls = 0 + const expired = new Error("closed live owner") + await assert.rejects(assertBandwidthNodeRegistryRuntimeRole({ query: async () => { calls++; return { rows: [] } } }, expected, () => { throw expired }), error => error === expired) + assert.equal(calls, 0) + const pending = deferred<{ rows: Record[] }>() + let active = true + const result = assertBandwidthNodeRegistryRuntimeRole({ query: async () => pending.promise }, expected, () => { if (!active) throw expired }) + active = false; const rejected = assert.rejects(result, error => error === expired); pending.resolve({ rows: [{ observation: observation() }] }); await rejected +}) + +test("SQL failure still performs the post-await live fence and returns no provider details", async () => { + let checks = 0 + await assert.rejects(assertBandwidthNodeRegistryRuntimeRole({ query: async () => { throw new Error("SECRET-provider-details") } }, expected, () => { checks++ }), error => refused(error) && !String(error).includes("SECRET")) + assert.equal(checks, 2) +}) diff --git a/backend/src/services/bandwidth-node-registry-role.ts b/backend/src/services/bandwidth-node-registry-role.ts new file mode 100644 index 00000000..af0b9a9f --- /dev/null +++ b/backend/src/services/bandwidth-node-registry-role.ts @@ -0,0 +1,119 @@ +/** Catalog observation only. This is neither a control generation issuer nor a + * readiness/native capability. The caller owns its bounded SQL facade and must + * run this assertion at the relevant live authority boundary. No default I/O. + * Only registry relations participate in its least-privilege matrix: legitimate + * core bandwidth DML is independently checked by the existing core source. */ +export interface BandwidthNodeRegistryRoleSql { + query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> +} +export interface BandwidthNodeRegistryRoleExpected { databaseName: string; runtimeRole: string; ownerRole: string } +export class BandwidthNodeRegistryRuntimeRoleRefused extends Error { + readonly code = "BANDWIDTH_NODE_REGISTRY_RUNTIME_ROLE_REFUSED" + constructor() { super("Bandwidth node registry runtime role refused") } +} +const TABLES = ["bandwidth_node_aliases", "bandwidth_nodes", "bandwidth_node_events", "bandwidth_node_authority"] as const +/** Names are parameters; catalog paths, namespace, relations and privilege + * vocabulary are fixed. ACL-provided privilege names include newer PostgreSQL + * privileges without sending unsupported names to an older server. */ +export const BANDWIDTH_NODE_REGISTRY_ROLE_SQL = `WITH + runtime AS (SELECT r.* FROM pg_catalog.pg_roles r WHERE r.rolname=$2 AND pg_catalog.current_database()::text=$1::text), + expected_owner AS (SELECT r.oid FROM pg_catalog.pg_roles r WHERE r.rolname=$3), + control_schema AS (SELECT n.* FROM pg_catalog.pg_namespace n WHERE n.nspname='mmapp_control'), + required(name,allowed) AS (VALUES + ('bandwidth_node_aliases',ARRAY['SELECT','INSERT','UPDATE']::text[]), + ('bandwidth_nodes',ARRAY['SELECT','INSERT']::text[]), + ('bandwidth_node_events',ARRAY['SELECT','INSERT']::text[]), + ('bandwidth_node_authority',ARRAY['SELECT']::text[])), + relations AS ( + SELECT e.name,e.allowed,c.oid,c.relowner,c.relacl,c.relkind + FROM required e LEFT JOIN control_schema n ON true + LEFT JOIN pg_catalog.pg_class c ON c.relnamespace=n.oid AND c.relname=e.name), + member_roles AS ( + SELECT r.* FROM pg_catalog.pg_roles r CROSS JOIN runtime rt + WHERE r.oid=rt.oid OR pg_catalog.pg_has_role(rt.oid,r.oid,'MEMBER')), + permissions AS ( + SELECT r.name,m.oid AS role_oid, + COALESCE(r.oid IS NOT NULL AND r.relkind IN ('r','p') AND NOT EXISTS ( + SELECT 1 FROM ( + SELECT pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','DELETE','TRUNCATE','REFERENCES','TRIGGER']::text[]) AS privilege + UNION SELECT a.privilege_type FROM pg_catalog.aclexplode(COALESCE(r.relacl,pg_catalog.acldefault('r',r.relowner))) a + ) p WHERE pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege) IS DISTINCT FROM (p.privilege=ANY(r.allowed)) + ),false) AS exact_table, + COALESCE(EXISTS ( + SELECT 1 FROM ( + SELECT pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','DELETE','TRUNCATE','REFERENCES','TRIGGER']::text[]) AS privilege + UNION SELECT a.privilege_type FROM pg_catalog.aclexplode(COALESCE(r.relacl,pg_catalog.acldefault('r',r.relowner))) a + ) p WHERE (pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege) AND NOT p.privilege=ANY(r.allowed)) + OR pg_catalog.has_table_privilege(m.oid,r.oid,p.privilege || ' WITH GRANT OPTION') + ) OR EXISTS ( + SELECT 1 FROM pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','REFERENCES']::text[]) p(privilege) + WHERE (pg_catalog.has_any_column_privilege(m.oid,r.oid,p.privilege) AND NOT p.privilege=ANY(r.allowed)) + OR pg_catalog.has_any_column_privilege(m.oid,r.oid,p.privilege || ' WITH GRANT OPTION') + ),true) AS forbidden + FROM relations r CROSS JOIN member_roles m) + SELECT pg_catalog.jsonb_build_object( + 'databaseName',pg_catalog.current_database(), + 'sessionRole',SESSION_USER::text,'currentRole',CURRENT_USER::text, + 'runtimeRoleExists',EXISTS(SELECT 1 FROM runtime), + 'ownerRoleExists',EXISTS(SELECT 1 FROM expected_owner), + 'runtimeFlagsSafe',COALESCE((SELECT NOT (rolsuper OR rolcreaterole OR rolcreatedb OR rolreplication OR rolbypassrls) FROM runtime),false), + 'ownerMembership',COALESCE((SELECT pg_catalog.pg_has_role(rt.oid,o.oid,'MEMBER') FROM runtime rt CROSS JOIN expected_owner o),true), + 'dangerousMembership',EXISTS ( + SELECT 1 FROM member_roles m WHERE m.rolsuper OR m.rolcreaterole OR m.rolcreatedb OR m.rolreplication OR m.rolbypassrls + OR m.rolname IN ('pg_read_server_files','pg_write_server_files','pg_execute_server_program','pg_read_all_data','pg_write_all_data','pg_maintain','pg_signal_backend','pg_checkpoint','pg_create_subscription','pg_database_owner','pg_use_reserved_connections') + OR EXISTS(SELECT 1 FROM control_schema n WHERE n.nspowner=m.oid OR pg_catalog.has_schema_privilege(m.oid,n.oid,'CREATE')) + OR EXISTS(SELECT 1 FROM relations r WHERE r.relowner=m.oid) + OR EXISTS(SELECT 1 FROM permissions p WHERE p.role_oid=m.oid AND p.forbidden)), + 'schemaExists',EXISTS(SELECT 1 FROM control_schema), + 'schemaOwner',(SELECT o.rolname::text FROM control_schema n JOIN pg_catalog.pg_roles o ON o.oid=n.nspowner), + 'schemaUsage',COALESCE((SELECT pg_catalog.has_schema_privilege(rt.oid,n.oid,'USAGE') FROM runtime rt CROSS JOIN control_schema n),false), + 'schemaCreate',COALESCE((SELECT pg_catalog.has_schema_privilege(rt.oid,n.oid,'CREATE') FROM runtime rt CROSS JOIN control_schema n),true), + 'publicSchemaUsage',EXISTS(SELECT 1 FROM control_schema n CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(n.nspacl,pg_catalog.acldefault('n',n.nspowner))) a WHERE a.grantee=0 AND a.privilege_type='USAGE'), + 'publicSchemaCreate',EXISTS(SELECT 1 FROM control_schema n CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(n.nspacl,pg_catalog.acldefault('n',n.nspowner))) a WHERE a.grantee=0 AND a.privilege_type='CREATE'), + 'tables',(SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object( + 'name',r.name,'exists',r.oid IS NOT NULL AND r.relkind IN ('r','p'), + 'ownerRole',o.rolname::text,'ownedByRuntime',COALESCE(r.relowner=rt.oid,true), + 'privilegesExact',COALESCE(p.exact_table AND NOT p.forbidden,false)) ORDER BY r.name COLLATE "C") + FROM relations r CROSS JOIN runtime rt LEFT JOIN pg_catalog.pg_roles o ON o.oid=r.relowner + LEFT JOIN permissions p ON p.name=r.name AND p.role_oid=rt.oid) + ) AS observation` + +function refuse(): never { throw new BandwidthNodeRegistryRuntimeRoleRefused() } +const plain = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +const label = (value: unknown): value is string => typeof value === "string" && value.trim() === value && value.length > 0 && Buffer.byteLength(value) <= 63 && !/[\x00-\x1f\x7f]/.test(value) +const exactKeys = (value: Record, fields: string[]) => Object.keys(value).sort().join(",") === [...fields].sort().join(",") + +export function captureBandwidthNodeRegistryRoleExpected(expected: BandwidthNodeRegistryRoleExpected): BandwidthNodeRegistryRoleExpected { + if (!plain(expected) || !exactKeys(expected, ["databaseName", "runtimeRole", "ownerRole"])) refuse() + const captured = { databaseName: expected.databaseName, runtimeRole: expected.runtimeRole, ownerRole: expected.ownerRole } + if (!Object.values(captured).every(label) || captured.runtimeRole === captured.ownerRole) refuse() + return captured +} + +export async function assertBandwidthNodeRegistryRuntimeRole(sql: BandwidthNodeRegistryRoleSql, expected: BandwidthNodeRegistryRoleExpected, live: () => void): Promise { + const captured = captureBandwidthNodeRegistryRoleExpected(expected) + live() + let result: { rows: Record[] } + try { result = await sql.query(BANDWIDTH_NODE_REGISTRY_ROLE_SQL, [captured.databaseName, captured.runtimeRole, captured.ownerRole]) } + catch { live(); refuse() } + live() + validateBandwidthNodeRegistryRuntimeRoleRows(result?.rows, captured) +} + +export function validateBandwidthNodeRegistryRuntimeRoleRows(rows: unknown, captured: Readonly): void { + if (!Array.isArray(rows) || rows.length !== 1 || !plain(rows[0]) || !exactKeys(rows[0], ["observation"])) refuse() + const value = rows[0].observation + if (!plain(value) || !exactKeys(value, ["databaseName","sessionRole","currentRole","runtimeRoleExists","ownerRoleExists","runtimeFlagsSafe","ownerMembership","dangerousMembership","schemaExists","schemaOwner","schemaUsage","schemaCreate","publicSchemaUsage","publicSchemaCreate","tables"])) refuse() + if (value.databaseName !== captured.databaseName || value.sessionRole !== captured.runtimeRole || value.currentRole !== captured.runtimeRole + || value.runtimeRoleExists !== true || value.ownerRoleExists !== true || value.runtimeFlagsSafe !== true + || value.ownerMembership !== false || value.dangerousMembership !== false || value.schemaExists !== true || value.schemaOwner !== captured.ownerRole + || value.schemaUsage !== true || value.schemaCreate !== false || value.publicSchemaUsage !== false || value.publicSchemaCreate !== false + || !Array.isArray(value.tables) || value.tables.length !== TABLES.length) refuse() + const seen = new Set() + for (const row of value.tables as unknown[]) { + if (!plain(row) || !exactKeys(row, ["name","exists","ownerRole","ownedByRuntime","privilegesExact"]) + || typeof row.name !== "string" || !TABLES.includes(row.name as typeof TABLES[number]) || seen.has(row.name) + || row.exists !== true || row.ownerRole !== captured.ownerRole || row.ownedByRuntime !== false || row.privilegesExact !== true) refuse() + seen.add(row.name as string) + } +} diff --git a/backend/src/services/bandwidth-node-registry-schema.ts b/backend/src/services/bandwidth-node-registry-schema.ts new file mode 100644 index 00000000..b3a4ea99 --- /dev/null +++ b/backend/src/services/bandwidth-node-registry-schema.ts @@ -0,0 +1,163 @@ +/** External owner installs this component only after mmapp_control and the core + * bandwidth DDL exist. No namespace, authority, node or alias is auto-enrolled. + * Structural guards do not authenticate HMACs, quiescence or physical identity. + * The trusted issuer must verify those independently. No application-server FK, + * endpoint epoch or changing access generation defines a persistent node key. + * Existing unknown occupancy is never read, cleared or rebound by this DDL. */ +export const BANDWIDTH_NODE_REGISTRY_TABLES = Object.freeze([ + "bandwidth_node_authority", "bandwidth_nodes", "bandwidth_node_aliases", "bandwidth_node_events", +] as const) + +/** Fresh-only literal SQL. No IF NOT EXISTS, seed, GUC privilege bypass, + * SECURITY DEFINER, cascade, reconciliation or time-based reclamation. */ +export const BANDWIDTH_NODE_REGISTRY_DDL = ` +CREATE TABLE mmapp_control.bandwidth_node_authority ( + id integer PRIMARY KEY CHECK (id=1), + installation_id uuid NOT NULL, + issuer_key_sha256 text COLLATE "C" NOT NULL CHECK (issuer_key_sha256 ~ '^[a-f0-9]{64}$'), + quiescence_proof text COLLATE "C" NOT NULL CHECK (quiescence_proof ~ '^[a-f0-9]{64}$'), + status text COLLATE "C" NOT NULL CHECK (status IN ('active','quarantined')), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE TABLE mmapp_control.bandwidth_nodes ( + node_id uuid PRIMARY KEY, + claim_proof text COLLATE "C" NOT NULL UNIQUE CHECK (claim_proof ~ '^[a-f0-9]{64}$'), + enrollment_mac text COLLATE "C" NOT NULL CHECK (enrollment_mac ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE TABLE mmapp_control.bandwidth_node_aliases ( + server_id bigint PRIMARY KEY CHECK (server_id BETWEEN 1 AND 9007199254740991), + incarnation uuid NOT NULL UNIQUE, + node_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_nodes(node_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + status text COLLATE "C" NOT NULL CHECK (status IN ('active','tombstoned','quarantined')), + record_mac text COLLATE "C" NOT NULL CHECK (record_mac ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE INDEX bandwidth_node_aliases_node_idx ON mmapp_control.bandwidth_node_aliases(node_id); +CREATE TABLE mmapp_control.bandwidth_node_events ( + event_id uuid PRIMARY KEY, + server_id bigint NOT NULL REFERENCES mmapp_control.bandwidth_node_aliases(server_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + incarnation uuid NOT NULL, + node_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_nodes(node_id) ON DELETE RESTRICT ON UPDATE RESTRICT, + kind text COLLATE "C" NOT NULL CHECK (kind IN ('enroll','tombstone','quarantine')), + old_status text COLLATE "C" CHECK (old_status='active'), + new_status text COLLATE "C" NOT NULL CHECK (new_status IN ('active','tombstoned','quarantined')), + authority_proof text COLLATE "C" NOT NULL CHECK (authority_proof ~ '^[a-f0-9]{64}$'), + event_mac text COLLATE "C" NOT NULL CHECK (event_mac ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + UNIQUE (server_id,kind), + CHECK (COALESCE( + (kind='enroll' AND old_status IS NULL AND new_status='active') + OR (kind='tombstone' AND old_status='active' AND new_status='tombstoned') + OR (kind='quarantine' AND old_status='active' AND new_status='quarantined'), false)) +); +CREATE INDEX bandwidth_node_events_node_idx ON mmapp_control.bandwidth_node_events(node_id); + +CREATE FUNCTION mmapp_control.bandwidth_node_authority_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +BEGIN + IF TG_OP='DELETE' THEN RAISE EXCEPTION 'bandwidth node authority cannot be deleted' USING ERRCODE='23514'; END IF; + IF TG_OP='INSERT' THEN + IF NEW.status <> 'active' THEN RAISE EXCEPTION 'bandwidth node authority must start active' USING ERRCODE='23514'; END IF; + RETURN NEW; + END IF; + IF ROW(NEW.id,NEW.installation_id,NEW.issuer_key_sha256,NEW.quiescence_proof,NEW.created_at) + IS DISTINCT FROM ROW(OLD.id,OLD.installation_id,OLD.issuer_key_sha256,OLD.quiescence_proof,OLD.created_at) + OR OLD.status <> 'active' OR NEW.status <> 'quarantined' THEN + RAISE EXCEPTION 'bandwidth node authority is immutable except quarantine' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE FUNCTION mmapp_control.bandwidth_node_immutable_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +BEGIN + RAISE EXCEPTION 'bandwidth nodes are immutable' USING ERRCODE='23514'; +END; +$body$; +CREATE FUNCTION mmapp_control.bandwidth_node_alias_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +BEGIN + IF TG_OP='DELETE' THEN RAISE EXCEPTION 'bandwidth node aliases cannot be deleted' USING ERRCODE='23514'; END IF; + IF TG_OP='INSERT' THEN + IF NEW.status <> 'active' THEN RAISE EXCEPTION 'bandwidth node alias must start active' USING ERRCODE='23514'; END IF; + -- Keep the reference invariant under replica mode as well: ordinary FK + -- implementation triggers are not ALWAYS, while this row guard is. + -- Nodes cannot be updated or deleted, so this existence check needs no row + -- lock (and must not require UPDATE rights for the registry runtime). + PERFORM 1 FROM mmapp_control.bandwidth_nodes WHERE node_id=NEW.node_id; + IF NOT FOUND THEN RAISE EXCEPTION 'bandwidth node alias requires its original node' USING ERRCODE='23514'; END IF; + RETURN NEW; + END IF; + IF ROW(NEW.server_id,NEW.incarnation,NEW.node_id,NEW.created_at) + IS DISTINCT FROM ROW(OLD.server_id,OLD.incarnation,OLD.node_id,OLD.created_at) + OR OLD.status <> 'active' OR NEW.status NOT IN ('tombstoned','quarantined') + OR NEW.record_mac IS NOT DISTINCT FROM OLD.record_mac THEN + RAISE EXCEPTION 'bandwidth node alias cannot be rebound or reactivated' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE FUNCTION mmapp_control.bandwidth_node_event_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +DECLARE alias_row mmapp_control.bandwidth_node_aliases%ROWTYPE; +BEGIN + IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'bandwidth node events are append only' USING ERRCODE='23514'; END IF; + SELECT * INTO alias_row FROM mmapp_control.bandwidth_node_aliases WHERE server_id=NEW.server_id FOR UPDATE; + IF NOT FOUND OR alias_row.incarnation <> NEW.incarnation OR alias_row.node_id <> NEW.node_id OR alias_row.status <> NEW.new_status THEN + RAISE EXCEPTION 'bandwidth node event does not match its original alias' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END; +$body$; +CREATE FUNCTION mmapp_control.bandwidth_node_alias_consistency_guard() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$ +DECLARE target_id bigint; alias_row mmapp_control.bandwidth_node_aliases%ROWTYPE; + total bigint; enrollments bigint; terminals bigint; terminal mmapp_control.bandwidth_node_events%ROWTYPE; +BEGIN + IF TG_OP='DELETE' THEN target_id:=OLD.server_id; ELSE target_id:=NEW.server_id; END IF; + SELECT * INTO alias_row FROM mmapp_control.bandwidth_node_aliases WHERE server_id=target_id FOR UPDATE; + IF NOT FOUND THEN RAISE EXCEPTION 'bandwidth node alias missing' USING ERRCODE='23514'; END IF; + SELECT count(*),count(*) FILTER (WHERE kind='enroll'),count(*) FILTER (WHERE kind IN ('tombstone','quarantine')) + INTO total,enrollments,terminals FROM mmapp_control.bandwidth_node_events WHERE server_id=target_id; + IF enrollments <> 1 OR terminals > 1 OR total <> enrollments+terminals OR total > 2 + OR EXISTS(SELECT 1 FROM mmapp_control.bandwidth_node_events WHERE server_id=target_id + AND (incarnation <> alias_row.incarnation OR node_id <> alias_row.node_id)) THEN + RAISE EXCEPTION 'bandwidth node alias event chain is incomplete' USING ERRCODE='23514'; + END IF; + IF alias_row.status='active' THEN + IF total <> 1 OR terminals <> 0 THEN RAISE EXCEPTION 'active bandwidth alias has a terminal event' USING ERRCODE='23514'; END IF; + ELSE + SELECT * INTO terminal FROM mmapp_control.bandwidth_node_events WHERE server_id=target_id AND kind IN ('tombstone','quarantine'); + IF terminals <> 1 OR terminal.old_status IS DISTINCT FROM 'active' OR terminal.new_status IS DISTINCT FROM alias_row.status + OR (alias_row.status='tombstoned' AND terminal.kind IS DISTINCT FROM 'tombstone') + OR (alias_row.status='quarantined' AND terminal.kind IS DISTINCT FROM 'quarantine') THEN + RAISE EXCEPTION 'terminal bandwidth node alias event mismatch' USING ERRCODE='23514'; + END IF; + END IF; + RETURN NULL; +END; +$body$; + +CREATE TRIGGER bandwidth_node_authority_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_authority FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_authority_guard(); +CREATE TRIGGER bandwidth_node_immutable_guard BEFORE UPDATE OR DELETE ON mmapp_control.bandwidth_nodes FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_immutable_guard(); +CREATE TRIGGER bandwidth_node_alias_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_aliases FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_alias_guard(); +CREATE TRIGGER bandwidth_node_event_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_events FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_event_guard(); +CREATE TRIGGER bandwidth_node_authority_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_node_authority FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE TRIGGER bandwidth_nodes_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_nodes FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE TRIGGER bandwidth_node_aliases_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_node_aliases FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE TRIGGER bandwidth_node_events_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_node_events FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate(); +CREATE CONSTRAINT TRIGGER bandwidth_node_alias_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_aliases DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_alias_consistency_guard(); +CREATE CONSTRAINT TRIGGER bandwidth_node_event_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_events DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_alias_consistency_guard(); + +ALTER TABLE mmapp_control.bandwidth_node_authority ENABLE ALWAYS TRIGGER bandwidth_node_authority_guard; +ALTER TABLE mmapp_control.bandwidth_nodes ENABLE ALWAYS TRIGGER bandwidth_node_immutable_guard; +ALTER TABLE mmapp_control.bandwidth_node_aliases ENABLE ALWAYS TRIGGER bandwidth_node_alias_guard; +ALTER TABLE mmapp_control.bandwidth_node_events ENABLE ALWAYS TRIGGER bandwidth_node_event_guard; +ALTER TABLE mmapp_control.bandwidth_node_authority ENABLE ALWAYS TRIGGER bandwidth_node_authority_no_truncate; +ALTER TABLE mmapp_control.bandwidth_nodes ENABLE ALWAYS TRIGGER bandwidth_nodes_no_truncate; +ALTER TABLE mmapp_control.bandwidth_node_aliases ENABLE ALWAYS TRIGGER bandwidth_node_aliases_no_truncate; +ALTER TABLE mmapp_control.bandwidth_node_events ENABLE ALWAYS TRIGGER bandwidth_node_events_no_truncate; +ALTER TABLE mmapp_control.bandwidth_node_aliases ENABLE ALWAYS TRIGGER bandwidth_node_alias_consistency; +ALTER TABLE mmapp_control.bandwidth_node_events ENABLE ALWAYS TRIGGER bandwidth_node_event_consistency; +` diff --git a/backend/src/services/bandwidth-node-registry.sql.test.ts b/backend/src/services/bandwidth-node-registry.sql.test.ts new file mode 100644 index 00000000..c76e4eb4 --- /dev/null +++ b/backend/src/services/bandwidth-node-registry.sql.test.ts @@ -0,0 +1,372 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { createHash, createHmac, randomBytes, randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { once } from "node:events" +import { execFile } from "node:child_process" +import { writeFile } from "node:fs/promises" +import { resolve } from "node:path" +import { promisify } from "node:util" +import type { BandwidthNodeRegistryAction } from "./bandwidth-node-registry.js" +import type { BandwidthNodeObservationEvidence, BandwidthNodeSourceSnapshot } from "./bandwidth-node-observation.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { assertOwnedBandwidthSql, canonical, createOwnedBandwidthHarness, fixtureBinding, prepareOwnedBandwidthState, scope } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +await prepareOwnedBandwidthState() +const { pool, closePool } = await import("../db/index.js") +const { Pool } = await import("pg") +const { applySqlMigrations } = await import("../db/migrate.js") +const { BANDWIDTH_ADMISSION_PROPOSAL_DDL } = await import("./bandwidth-admission-schema.js") +const { BANDWIDTH_NODE_REGISTRY_DDL, BANDWIDTH_NODE_REGISTRY_TABLES } = await import("./bandwidth-node-registry-schema.js") +const { createBandwidthNodeRegistry } = await import("./bandwidth-node-registry.js") +const { createBandwidthNodeObservationReader } = await import("./bandwidth-node-observation.js") +const { createProbeStorageConnectionConnector } = await import("./server-probe-storage-connection.js") +const cases: string[] = [], limitations: string[] = [] +const key = randomBytes(32), installationId = randomUUID(), issuerKeySha256 = createHash("sha256").update(key).digest("hex") +const mac = (purpose: string, value: unknown) => createHmac("sha256", key).update("owned-bandwidth-node-registry:" + purpose + ":").update(canonical(value)).digest("hex") +const quiescenceProof = mac("synthetic-operator-quiescence", "Owned local fixture only; not actual production enrollment readiness") +const root = { installationId, issuerKeySha256, quiescenceProof } +const role = "bw_registry_" + randomUUID().replaceAll("-", ""), password = randomBytes(32).toString("hex") +assert(/^bw_registry_[a-f0-9]{32}$/.test(role)); assert(/^[a-f0-9]{64}$/.test(password)) +const roleUrl = new URL(process.env.DATABASE_URL!); roleUrl.username = role; roleUrl.password = password +let runtime: InstanceType | undefined, roleCreated = false, nativeGets = 0, realCommitLosses = 0, droppedCommitCommands = 0 +const sources = new Map(), license = new Map>() +const server = createServer((req, res) => { + const auth = Buffer.from((req.headers.authorization ?? "").replace(/^Basic /, ""), "base64").toString("utf8") + const match = /^reg-(\d+)(?:-v2)?:/.exec(auth), id = match ? Number(match[1]) : -1 + assert.equal(req.method, "GET"); assert.equal(req.url, "/rest/system/license"); nativeGets++ + if (!license.has(id)) { res.writeHead(404); res.end("{}"); return } + res.setHeader("content-type", "application/json"); res.end(JSON.stringify(license.get(id))) +}) +server.listen(0, "127.0.0.1"); await once(server, "listening") +const address = server.address(); assert(address && typeof address !== "string") +const nativePort = address.port +const reader = createBandwidthNodeObservationReader({ async read(id) { return sources.get(id) } }) +const observation = (id: number) => reader.observe(id, scope()) +const coreTables = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events"] +async function digestTables(tables: readonly string[]) { + const result: Record = {} + for (const table of tables) { + assert(/^bandwidth_[a-z_]+$/.test(table)) + const rows = (await pool.query(`SELECT to_jsonb(t) AS row FROM mmapp_control.${table} t ORDER BY to_jsonb(t)::text`)).rows + result[table] = { rows: rows.length, sha256: createHash("sha256").update(canonical(rows)).digest("hex") } + } + return result +} +async function endpoint(claims: Record) { + const id = Number((await pool.query("INSERT INTO public.servers(name,host,enabled) VALUES('owned-registry','127.0.0.1',false) RETURNING id")).rows[0].id) + sources.set(id, { serverId: id, enabled: true, sourceProof: mac("source", id), authorityGeneration: "owned-source-v1", + endpoint: { host: "127.0.0.1", port: nativePort, useSsl: false, verifySsl: true, username: "reg-" + id, password: "owned-native-private", routingEndpointEpoch: randomUUID() } }) + license.set(id, claims); return id +} +const expiry = Date.now() + 300000, deniedWrites = new Set(), expired = new Map(), epochs = new Map() +let authorizeHook: ((action: BandwidthNodeRegistryAction, id: number, evidence: Readonly | undefined) => Promise) | undefined +let proofHook: ((purpose: string) => Promise) | undefined +const authorize = async (action: BandwidthNodeRegistryAction, id: number, _window: BandwidthScope, evidence: Readonly | undefined) => { + await authorizeHook?.(action, id, evidence) + assert(sources.has(id)) + if (action !== "resolve" && deniedWrites.has(id)) throw Error("Owned original grant does not authorize registry write") + return { ...root, authorityProof: mac("original-grant", { action, id, epoch: epochs.get(id) ?? 1 }), authorityGeneration: "owned-grant-" + (epochs.get(id) ?? 1), expiresAt: expired.get(id) ?? expiry } +} +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function makeHost(options: { loseCommit?: boolean; dropCommitCommand?: boolean; beginBarrier?: { enter(): Promise }; revokeBeforeCommit?: number; now?: () => number } = {}) { + let lose = options.loseCommit ?? false, lastPid = 0, checkouts = 0, commits = 0, queriesAfterRevocation = 0, physicalQueries = 0 + let dropCommand = options.dropCommitCommand ?? false + let enrolled = false, finalAuthorityReturned = false, inspections = 0, revokedInsideInspect = false + const connector = createProbeStorageConnectionConnector(async () => { + assert(runtime); const raw = await runtime.connect(); checkouts++ + lastPid = Number((await raw.query("SELECT pg_backend_pid() AS pid")).rows[0].pid); return raw + }) + const host = createBandwidthNodeRegistry({ now: options.now ?? Date.now, authorize, + inspect: async (token, window) => { + const value = await reader.inspect(token, window) + // The first two inspections follow the final authority SELECT and the + // explicit final guard. The third is the real COMMIT dispatch guard. + if (value.serverId === options.revokeBeforeCommit && finalAuthorityReturned && ++inspections === 3) { + deniedWrites.add(value.serverId); revokedInsideInspect = true + } + return value + }, + keyedProof: async (purpose, value) => { await proofHook?.(purpose); return mac(purpose, value) }, + connect: async window => { + const client = await connector(window) + return { release: destroy => client.release(destroy), async query(sql, values, queryOptions) { + physicalQueries++ + if (revokedInsideInspect) queriesAfterRevocation++ + const result = await client.query(sql, values, queryOptions) + if (sql.startsWith("DO $mmapp_bandwidth_register$") || sql.startsWith("INSERT INTO mmapp_control.bandwidth_node_events")) enrolled = true + if (enrolled && sql.includes("SELECT installation_id::pg_catalog.text")) finalAuthorityReturned = true + if (sql === "BEGIN") await options.beginBarrier?.enter() + if (sql === "COMMIT") { + commits++ + if (lose) { lose = false; realCommitLosses++; throw Error("Owned lost ACK AFTER actual registry COMMIT") } + if (dropCommand) { dropCommand = false; droppedCommitCommands++; return { rows: result.rows } } + } + return result + } } + }, + }) + return { host, pid: () => lastPid, checkouts: () => checkouts, commits: () => commits, physicalQueries: () => physicalQueries, queriesAfterRevocation: () => queriesAfterRevocation, revokedInsideInspect: () => revokedInsideInspect } +} +try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + await pool.query("CREATE SCHEMA mmapp_control"); await pool.query(BANDWIDTH_ADMISSION_PROPOSAL_DDL); await pool.query(BANDWIDTH_NODE_REGISTRY_DDL) + await pool.query(`CREATE ROLE "${role}" LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS NOINHERIT`); roleCreated = true + await pool.query("REVOKE ALL ON SCHEMA mmapp_control FROM PUBLIC"); await pool.query(`GRANT USAGE ON SCHEMA mmapp_control TO "${role}"`) + await pool.query("REVOKE ALL ON ALL FUNCTIONS IN SCHEMA mmapp_control FROM PUBLIC") + await pool.query(`GRANT SELECT ON mmapp_control.bandwidth_node_authority TO "${role}"`) + await pool.query(`GRANT SELECT,INSERT ON mmapp_control.bandwidth_nodes,mmapp_control.bandwidth_node_events TO "${role}"`) + await pool.query(`GRANT SELECT,INSERT,UPDATE ON mmapp_control.bandwidth_node_aliases TO "${role}"`) + for (const fn of ["bandwidth_node_authority_guard", "bandwidth_node_immutable_guard", "bandwidth_node_alias_guard", "bandwidth_node_event_guard", "bandwidth_node_alias_consistency_guard", "bandwidth_admission_reject_truncate"]) + await pool.query(`GRANT EXECUTE ON FUNCTION mmapp_control.${fn}() TO "${role}"`) + runtime = new Pool({ connectionString: roleUrl.toString(), max: 6 }) + const flags = (await runtime.query("SELECT rolname,rolsuper,rolcreatedb,rolcreaterole,rolreplication,rolbypassrls,rolinherit FROM pg_catalog.pg_roles WHERE rolname=current_user")).rows[0] + assert.equal(flags.rolname, role) + for (const field of ["rolsuper", "rolcreatedb", "rolcreaterole", "rolreplication", "rolbypassrls", "rolinherit"]) assert.equal(flags[field], false) + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_nodes SET claim_proof=claim_proof")) + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_node_authority SET status='quarantined'")) + await assert.rejects(runtime.query("DELETE FROM mmapp_control.bandwidth_node_aliases")) + await assert.rejects(runtime.query("SELECT * FROM mmapp_control.bandwidth_attempts")) + cases.push("actual restricted LOGIN registry role: precise registry DML, no node/authority UPDATE, no DELETE, no core access or elevated flags") + const primary = makeHost(), sameClaims = { "system-id": "SYS-SHARED", "software-id": "SOFT-SHARED" } + const a = await endpoint(sameClaims), b = await endpoint(sameClaims) + const unseeded = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + await assert.rejects(primary.host.register(await observation(a), scope())) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), unseeded) + await pool.query("INSERT INTO mmapp_control.bandwidth_node_authority(id,installation_id,issuer_key_sha256,quiescence_proof,status) VALUES(1,$1,$2,$3,'active')", [installationId, issuerKeySha256, quiescenceProof]) + cases.push("missing external authority refuses without bootstrap/seed; owner explicitly installs synthetic quiescence/installation/separate issuer key record") + + const first = await primary.host.register(await observation(a), scope()), second = await primary.host.register(await observation(b), scope()) + assert.match(first.canonicalNodeId, /^[a-f0-9-]{36}$/); assert.equal(first.canonicalNodeId, second.canonicalNodeId); assert.notEqual(first.incarnation, second.incarnation) + assert.equal((await primary.host.register(await observation(a), scope())).outcome, "existing") + const missing = await endpoint({ "system-id": "MISSING" }), beforeResolve = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + await assert.rejects(primary.host.resolve(await observation(missing), scope())); assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), beforeResolve) + await assert.rejects(primary.host.register(JSON.parse(JSON.stringify(await observation(missing))), scope())) + cases.push("two genuine native aliases share one random canonical UUID with distinct incarnations; existing registration is a receipt; resolve/serialized observation never auto-enroll") + { + const id = await endpoint({ "system-id": "ACTUAL-OBSERVED-UNAPPROVED-CLAIM" }), token = await observation(id), fixture = makeHost() + const before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + const approvedClaims = [{ kind: "system-id", value: "DIFFERENT-ORIGINAL-APPROVED-CLAIM" }] + let received: Readonly | undefined + authorizeHook = async (action, serverId, evidence) => { + if (action === "register" && serverId === id) { + received = evidence + assert(evidence); assert.equal(evidence.sourceProof, sources.get(id)!.sourceProof) + // The fixed original-grant fixture approves another claim. Genuine + // network evidence is necessary but cannot substitute for that intent. + assert.deepEqual(evidence.claims, approvedClaims) + } + } + try { + await assert.rejects(fixture.host.register(token, scope())) + assert(received); assert.equal(received.serverId, id); assert.equal(received.sourceProof, sources.get(id)!.sourceProof) + assert.deepEqual(received.claims, [{ kind: "system-id", value: "ACTUAL-OBSERVED-UNAPPROVED-CLAIM" }]) + assert(Object.isFrozen(received)); assert(Object.isFrozen(received.claims)); assert(received.claims.every(Object.isFrozen)) + assert(!Object.hasOwn(received, "endpoint")); assert(!JSON.stringify(received).includes("owned-native-private")); assert(!JSON.stringify(received).includes("reg-" + id)) + assert.equal(fixture.checkouts(), 0); assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + } finally { authorizeHook = undefined } + cases.push("fixed original grant receives deeply frozen genuine inspected claims/source as fourth argument and denies different approved intent before any PG checkout; credentials/endpoint remain private") + } + const typedIds: string[] = [] + for (const claims of [{ "system-id": "LEFT", "software-id": "RIGHT" }, { "system-id": "RIGHT", "software-id": "LEFT" }, { "system-id": "ONE" }, { "software-id": "ONE" }] as Array>) { + const id = await endpoint(claims); typedIds.push((await primary.host.register(await observation(id), scope())).canonicalNodeId) + } + assert.equal(new Set(typedIds).size, 4) + license.set(a, { "system-id": "FOREIGN", "software-id": "SOFT-SHARED" }) + const driftBefore = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + await assert.rejects(primary.host.resolve(await observation(a), scope())); await assert.rejects(primary.host.register(await observation(a), scope())) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), driftBefore); license.set(a, sameClaims) + const source = sources.get(a)!; source.endpoint.username = "reg-" + a + "-v2"; source.endpoint.password = "changed-private"; source.endpoint.routingEndpointEpoch = randomUUID(); source.sourceProof = mac("new-source-epoch", a) + assert.equal((await primary.host.resolve(await observation(a), scope())).canonicalNodeId, first.canonicalNodeId) + cases.push("typed claim kinds/full vectors never collapse; native claim drift cannot mint a replacement key; credentials/endpoint epoch drift with same observed claims preserves the canonical UUID") + + const otherId = await endpoint({ "system-id": "OTHER-NODE" }), other = await primary.host.register(await observation(otherId), scope()) + const core = await createOwnedBandwidthHarness(), coreBinding = fixtureBinding(first.canonicalNodeId, other.canonicalNodeId) + core.register(coreBinding); await core.journal.reserve(coreBinding, scope()); await core.journal.arm(coreBinding, scope()); await core.journal.quarantine(coreBinding, scope()) + const coreBefore = await digestTables(coreTables) + assert.equal((await primary.host.retire(a, "tombstone", scope())).status, "tombstoned") + await assert.rejects(primary.host.register(await observation(a), scope())); await assert.rejects(primary.host.resolve(await observation(a), scope())) + await assert.rejects(primary.host.retire(a, "quarantine", scope())) + assert.equal((await primary.host.retire(a, "tombstone", scope())).outcome, "existing") + const replacementId = await endpoint(sameClaims), replacement = await primary.host.register(await observation(replacementId), scope()) + assert.equal(replacement.canonicalNodeId, first.canonicalNodeId); assert.notEqual(replacement.incarnation, first.incarnation) + await primary.host.retire(b, "quarantine", scope()); await assert.rejects(primary.host.register(await observation(b), scope())) + await pool.query("DELETE FROM public.servers WHERE id=ANY($1::int[])", [[a, b]]) + assert.deepEqual(await digestTables(coreTables), coreBefore) + const overlapping = fixtureBinding(replacement.canonicalNodeId, randomUUID()); core.register(overlapping) + await assert.rejects(core.journal.reserve(overlapping, scope())); assert.deepEqual(await digestTables(coreTables), coreBefore) + cases.push("actual core reserve/arm/unknown quarantine survives alias tombstone/quarantine/new alias and SQL app-server DELETE byte-for-byte; replacement alias cannot evade unknown occupancy") + + { + const c1 = await endpoint({ "system-id": "CONCURRENT" }), c2 = await endpoint({ "system-id": "CONCURRENT" }), gate = deferred(); let joined = 0 + const beginBarrier = { async enter() { if (++joined === 2) gate.resolve(); await gate.promise } } + const left = makeHost({ beginBarrier }), right = makeHost({ beginBarrier }), o1 = await observation(c1), o2 = await observation(c2) + const [r1, r2] = await Promise.all([left.host.register(o1, scope()), right.host.register(o2, scope())]) + assert.notEqual(left.pid(), right.pid()); assert.equal(r1.canonicalNodeId, r2.canonicalNodeId); assert.notEqual(r1.incarnation, r2.incarnation) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mmapp_control.bandwidth_nodes WHERE node_id=$1", [r1.canonicalNodeId])).rows[0].n, 1) + cases.push("two independent bounded PG clients/host instances start behind a real BEGIN barrier and concurrently enroll same native claim as exactly one node/two incarnations") + } + { + const id = await endpoint({ "system-id": "LOST-COMMIT" }), lost = makeHost({ loseCommit: true }) + await assert.rejects(lost.host.register(await observation(id), scope())) + const persisted = (await pool.query("SELECT node_id::text FROM mmapp_control.bandwidth_node_aliases WHERE server_id=$1", [id])).rows[0]; assert(persisted) + const receipt = await makeHost().host.register(await observation(id), scope()) + assert.equal(receipt.outcome, "existing"); assert.equal(receipt.canonicalNodeId, persisted.node_id) + const lostRetire = makeHost({ loseCommit: true }); await assert.rejects(lostRetire.host.retire(id, "tombstone", scope())) + assert.equal((await makeHost().host.retire(id, "tombstone", scope())).outcome, "existing") + await assert.rejects(primary.host.register(await observation(id), scope())) + cases.push("real register and retirement COMMITs lose ACK only AFTER server completion: durable rows persist, calls refuse, fresh host returns existing receipt without reactivation/native capability") + } + { + const id = await endpoint({ "system-id": "MISSING-COMMIT-COMMAND" }), badAck = makeHost({ dropCommitCommand: true }) + await assert.rejects(badAck.host.register(await observation(id), scope())) + const stored = (await pool.query("SELECT node_id::text FROM mmapp_control.bandwidth_node_aliases WHERE server_id=$1", [id])).rows[0]; assert(stored) + const before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + const existing = await makeHost().host.register(await observation(id), scope()) + assert.equal(existing.outcome, "existing"); assert.equal(existing.canonicalNodeId, stored.node_id) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + cases.push("actual COMMIT succeeds but wrapper drops command and returns rows-only: host refuses the ACK; durable enrollment survives and fresh registration only reads existing receipt with no second node/event") + } + for (const bad of ["node", "alias", "event"] as const) { + const id = await endpoint({ "system-id": "MAC-TAMPER-" + bad }), token = await observation(id), evidence = await reader.inspect(token, scope()) + const claimProof = mac("claim", evidence.claims), nodeId = randomUUID(), incarnation = randomUUID(), eventId = randomUUID(), authority = await authorize("register", id, scope(), evidence) + const nodeMac = mac("node", { ...root, nodeId, claimProof }), aliasMac = mac("alias", { ...root, serverId: id, incarnation, nodeId, status: "active" }) + const eventMac = mac("event", { ...root, eventId, serverId: id, incarnation, nodeId, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: authority.authorityProof }) + const owner = await pool.connect() + try { + await owner.query("BEGIN") + await owner.query("INSERT INTO mmapp_control.bandwidth_nodes(node_id,claim_proof,enrollment_mac) VALUES($1,$2,$3)", [nodeId, claimProof, bad === "node" ? "f".repeat(64) : nodeMac]) + await owner.query("INSERT INTO mmapp_control.bandwidth_node_aliases(server_id,incarnation,node_id,status,record_mac) VALUES($1,$2,$3,'active',$4)", [id, incarnation, nodeId, bad === "alias" ? "f".repeat(64) : aliasMac]) + await owner.query("INSERT INTO mmapp_control.bandwidth_node_events(event_id,server_id,incarnation,node_id,kind,old_status,new_status,authority_proof,event_mac) VALUES($1,$2,$3,$4,'enroll',NULL,'active',$5,$6)", [eventId, id, incarnation, nodeId, authority.authorityProof, bad === "event" ? "f".repeat(64) : eventMac]) + await owner.query("COMMIT") + } finally { owner.release() } + const before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + await assert.rejects(primary.host.resolve(token, scope())); await assert.rejects(primary.host.register(token, scope())) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + } + cases.push("owned structurally valid INSERT fixtures with individually forged prior node/alias/event MACs refuse genuine-observation resolve/register with exact data unchanged; no trigger disabling or immutable UPDATE") + + { + const id = await endpoint({ "system-id": "REVOKED-IN-COMMIT-INSPECT" }), token = await observation(id) + const before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), fixture = makeHost({ revokeBeforeCommit: id }) + try { + await assert.rejects(fixture.host.register(token, scope())) + assert.equal(fixture.revokedInsideInspect(), true); assert.equal(fixture.commits(), 0); assert.equal(fixture.queriesAfterRevocation(), 0) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + } finally { deniedWrites.delete(id) } + cases.push("real pre-COMMIT genuine-inspect wrapper revokes original write grant: final authorize runs after inspection, no subsequent physical SQL/COMMIT and no committed node/alias/event") + } + + { + const before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), owner = await pool.connect() + try { + for (const sql of ["UPDATE mmapp_control.bandwidth_nodes SET claim_proof='" + "f".repeat(64) + "'", "DELETE FROM mmapp_control.bandwidth_node_aliases", "UPDATE mmapp_control.bandwidth_node_events SET event_mac='" + "f".repeat(64) + "'", "TRUNCATE mmapp_control.bandwidth_nodes,mmapp_control.bandwidth_node_aliases,mmapp_control.bandwidth_node_events"]) { + await owner.query("BEGIN"); try { await owner.query("SET LOCAL session_replication_role=replica"); await assert.rejects(owner.query(sql)) } finally { await owner.query("ROLLBACK") } + } + } finally { owner.release() } + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_node_aliases SET status='active',record_mac=$2 WHERE server_id=$1", [a, "f".repeat(64)])) + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_node_aliases SET status='active',record_mac=$2 WHERE server_id=$1", [b, "f".repeat(64)])) + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_node_aliases SET node_id=$2,record_mac=$3 WHERE server_id=$1", [replacementId, other.canonicalNodeId, "f".repeat(64)])) + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_node_aliases SET status='tombstoned',record_mac=$2 WHERE server_id=$1", [replacementId, "f".repeat(64)])) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + const triggers = (await pool.query("SELECT t.tgenabled::text AS enabled FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='mmapp_control' AND c.relname=ANY($1::text[]) AND NOT t.tgisinternal", [[...BANDWIDTH_NODE_REGISTRY_TABLES]])).rows + assert.equal(triggers.length, 10); assert(triggers.every(row => row.enabled === "A")) + cases.push("normal-role terminal reactivation and missing terminal event fail; actual owner replica-mode node/alias/event/TRUNCATE mutations refuse, ten ALWAYS guards and exact data preserved") + } + { + const id = await endpoint({ "system-id": "WRITES-DENIED" }), token = await observation(id), before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + deniedWrites.add(id); await assert.rejects(primary.host.register(token, scope())); deniedWrites.delete(id) + let calls = 0 + authorizeHook = async (_action, serverId) => { if (serverId === id && ++calls === 2) epochs.set(id, 2) } + await assert.rejects(primary.host.register(token, scope())); authorizeHook = undefined + sources.get(id)!.endpoint.routingEndpointEpoch = randomUUID() + await assert.rejects(primary.host.register(token, scope())); assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + cases.push("fixed original grant write denial, authority epoch drift between guards and stale genuine source endpoint epoch all refuse without registry mutation") + } + { + const id = await endpoint({ "system-id": "LATE-AUTH" }), token = await observation(id), gate = deferred(), began = deferred(), before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), late = makeHost() + authorizeHook = async (_action, serverId) => { if (serverId === id) { began.resolve(); await gate.promise } } + const pending = late.host.register(token, scope(120)); await began.promise; await assert.rejects(pending) + authorizeHook = undefined; gate.resolve(); await new Promise(resolve => setImmediate(resolve)) + assert.equal(late.checkouts(), 0); assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + cases.push("stalled original authorizer is bounded; late success after deadline never checks out PG or commits a registry row") + } + { + const id = await endpoint({ "system-id": "EXPIRED-ORIGINAL" }), token = await observation(id), gate = deferred(), began = deferred(), before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + expired.set(id, Date.now() + 150); proofHook = async purpose => { if (purpose === "claim") { began.resolve(); await gate.promise } } + const pending = makeHost().host.register(token, scope()); await began.promise; await assert.rejects(pending) + expired.set(id, Date.now() + 30000); proofHook = undefined; gate.resolve(); await new Promise(resolve => setImmediate(resolve)) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + cases.push("original authority expires during private claim proof; replacing grant expiry and resolving late proof cannot resume old SQL/COMMIT") + } + { + const id = await endpoint({ "system-id": "RELATION-LOCK" }), token = await observation(id), blocker = await pool.connect(), fixture = makeHost() + const before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + try { + await blocker.query("BEGIN"); await blocker.query("LOCK mmapp_control.bandwidth_nodes IN ACCESS EXCLUSIVE MODE") + await assert.rejects(fixture.host.register(token, scope(200))); const pid = fixture.pid() + await blocker.query("ROLLBACK") + let present = 1; const until = Date.now() + 2000 + while (present && Date.now() < until) { present = Number((await blocker.query("SELECT count(*) AS n FROM pg_catalog.pg_stat_activity WHERE pid=$1", [pid])).rows[0].n); if (present) await new Promise(resolve => setTimeout(resolve, 20)) } + assert.equal(present, 0); assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + } finally { await blocker.query("ROLLBACK").catch(() => {}); blocker.release() } + cases.push("actual relation-lock/deadline destroys bounded runtime backend; unlock cannot produce late node/alias/event COMMIT") + } + { + const id = await endpoint({ "system-id": "MONOTONIC-ROLLBACK" }), token = await observation(id), began = deferred(), gate = deferred() + const before = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + let offset = 0 + const fixture = makeHost({ now: () => Date.now() + offset }) + expired.set(id, Date.now() + 150) + proofHook = async purpose => { if (purpose === "claim") { began.resolve(); await gate.promise } } + const pending = fixture.host.register(token, scope(500)) + let safetyTimer: ReturnType | undefined + try { + await began.promise; offset = -60000 + const safety = new Promise((_resolve, reject) => { safetyTimer = setTimeout(() => reject(Error("Monotonic deadline did not stop the original registry operation")), 1500) }) + await assert.rejects(Promise.race([pending, safety]), (error: unknown) => (error as { code?: string }).code === "BANDWIDTH_NODE_REGISTRY_REFUSED") + const queriesAtReject = fixture.physicalQueries(), pid = fixture.pid(); assert(pid > 0) + proofHook = undefined; gate.resolve(); await new Promise(resolve => setImmediate(resolve)) + assert.equal(fixture.physicalQueries(), queriesAtReject); assert.equal(fixture.commits(), 0) + let present = 1; const goneBy = Date.now() + 2000 + while (present && Date.now() < goneBy) { present = Number((await pool.query("SELECT count(*) AS n FROM pg_catalog.pg_stat_activity WHERE pid=$1", [pid])).rows[0].n); if (present) await new Promise(resolve => setTimeout(resolve, 20)) } + assert.equal(present, 0); assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), before) + } finally { if (safetyTimer) clearTimeout(safetyTimer); proofHook = undefined; gate.resolve(); expired.delete(id) } + cases.push("actual original grant expires while host-only wall clock rewinds 60 seconds during deferred private proof: real monotonic timer rejects, backend disappears, late proof sends no SQL/COMMIT and exact registry data stays unchanged") + } + + if (process.env.MMAPP_OWN_BANDWIDTH_RESTORE === "true") { + const { runPrivateDatabaseDump } = await import("./database-dump-connection.js"), { restorePartitionedDatabase } = await import("./system-database-partition-restore.js") + const execute = promisify(execFile), file = resolve(process.env.MCP_STATE_DIR!, "node-registry-public.dump"), url = new URL(process.env.DATABASE_URL!) + const env = { ...process.env, PGHOST: url.hostname, PGPORT: url.port, PGUSER: url.username, PGPASSWORD: decodeURIComponent(url.password) } + const command = async (program: string, args: string[]): Promise => { try { return (await execute(program, args, { env, encoding: "buffer", timeout: 30000, maxBuffer: 32 * 1048576, windowsHide: true })).stdout } catch { throw Error("Owned registry archive command failed: " + program) } } + const marker = Number((await pool.query("INSERT INTO public.servers(name,host,enabled) VALUES('registry-before-restore','127.0.0.1',false) RETURNING id")).rows[0].id) + const before = await digestTables([...coreTables, ...BANDWIDTH_NODE_REGISTRY_TABLES]) + await writeFile(file, await runPrivateDatabaseDump(url.toString(), { authorize: async () => {} }), { flag: "wx", mode: 0o600 }) + assert(!(await command("pg_restore", ["--list", file])).toString("utf8").includes("mmapp_control")) + await pool.query("UPDATE public.servers SET name='registry-after-restore' WHERE id=$1", [marker]) + await restorePartitionedDatabase(file, url.toString(), { command, prepareSql: path => writeFile(path, "", { flag: "wx", mode: 0o600 }) }) + assert.equal((await pool.query("SELECT name FROM public.servers WHERE id=$1", [marker])).rows[0].name, "registry-before-restore") + assert.deepEqual(await digestTables([...coreTables, ...BANDWIDTH_NODE_REGISTRY_TABLES]), before) + assert.equal((await primary.host.resolve(await observation(replacementId), scope())).canonicalNodeId, first.canonicalNodeId) + cases.push("actual public-only dump/shared transactional restore replaces observable application row while exact seven control tables, canonical aliases and unknown core occupancy survive") + } else limitations.push("Public-only restore not requested") + const authorityBefore = await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES) + await assert.rejects(pool.query("UPDATE mmapp_control.bandwidth_node_authority SET issuer_key_sha256=$1 WHERE id=1", ["f".repeat(64)])) + assert.deepEqual(await digestTables(BANDWIDTH_NODE_REGISTRY_TABLES), authorityBefore) + await pool.query("UPDATE mmapp_control.bandwidth_node_authority SET status='quarantined' WHERE id=1") + await assert.rejects(primary.host.resolve(await observation(replacementId), scope())) + assert.deepEqual(await digestTables(coreTables), coreBefore) + limitations.push("Synthetic explicit operator/quiescence authority and localhost license identities only; no genuine production enrollment readiness, physical uniqueness, clone resistance or dispatch wiring") + limitations.push("No cross-process registry test; concurrency uses two real backend sessions/host instances") + console.log("Owned canonical bandwidth registry:", JSON.stringify({ cases: cases.length, checks: cases, actualLocalhostLicenseGets: nativeGets, realCommitAckLosses: realCommitLosses, droppedCommitCommands, + normalMigrationCount: MIGRATION_MANIFEST.length, externalIssuerFixtureOnly: true, restrictedRegistryRole: true, coreUnknownPairPreserved: true, realChrCalls: 0, productionWired: false, limitations })) +} finally { + authorizeHook = undefined; proofHook = undefined; reader.close() + server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) + await runtime?.end() + if (roleCreated) { await pool.query(`DROP OWNED BY "${role}"`); await pool.query(`DROP ROLE "${role}"`) } + key.fill(0); await closePool() +} diff --git a/backend/src/services/bandwidth-node-registry.ts b/backend/src/services/bandwidth-node-registry.ts new file mode 100644 index 00000000..948cfbcb --- /dev/null +++ b/backend/src/services/bandwidth-node-registry.ts @@ -0,0 +1,247 @@ +/** Explicit trusted host for the externally installed canonical registry. No + * default pool, source, signing key, enrollment or native dispatch is supplied. + * Genuine observations are inspected by the fixed same-reader composition. + * The original node key survives aliases, credential epochs and tombstones. */ +import { randomUUID } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { bandwidthRegistrySnapshotSql, parseBandwidthRegistrySnapshot, bandwidthRegistrySnapshotProofs, bandwidthRegistryEnrollmentSql, type BandwidthRegistrySnapshot } from "./bandwidth-node-registry-batch.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { BandwidthNodeProofInput } from "./bandwidth-node-source.js" +import type { BandwidthNodeObservationEvidence } from "./bandwidth-node-observation.js" + +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid() +const authoritySchema=z.object({installationId:uuid,issuerKeySha256:hash,quiescenceProof:hash, + authorityProof:hash,authorityGeneration:z.string().min(1).max(200),expiresAt:z.number().int().positive()}).strict() +type Authority=z.infer +export type BandwidthNodeRegistryAction="register"|"resolve"|"tombstone"|"quarantine" +export interface BandwidthNodeRegistryDependencies { + now():number + /** Bounded owned lease; query results must preserve the actual PG command. + * Rows alone never establish a physical BEGIN/COMMIT acknowledgement. */ + connect(scope:BandwidthScope):Promise + /** Fixed private provenance verifier, not a JSON/native-response parser. */ + inspect(observation:unknown,scope:BandwidthScope):Promise + /** Must independently authenticate original human enrollment/quiescence or + * current execution/retirement authority, issuer key and installation. A SQL + * row, model flag or generic read permission cannot issue this authority. */ + authorize(action:BandwidthNodeRegistryAction,serverId:number,scope:BandwidthScope,evidence:Readonly|undefined):Promise + /** Trusted constructor-only fixed path. ONE fresh provenance/authority read; + * both projections must be synchronous after that read. Never request JSON. */ + inspectAndAuthorize?(action:BandwidthNodeRegistryAction,observation:unknown,requestedId:number|undefined,scope:BandwidthScope):Promise> + /** Stable separate externally provisioned issuer key; never endpoint/epoch, + * credentials, per-operation generation or an automatically generated key. */ + keyedProof(purpose:string,value:unknown,scope:BandwidthScope):Promise + /** Fixed optional source capability: local MACs share one freshly checked key. + * No caller supplies a callback; scalar fallback retains all original guards. */ + keyedProofs?(inputs:readonly BandwidthNodeProofInput[],scope:BandwidthScope):Promise +} +export interface BandwidthNodeRegistryReceipt { + readonly serverId:number;readonly canonicalNodeId:string;readonly incarnation:string + readonly status:"active"|"tombstoned"|"quarantined";readonly outcome:"new"|"existing" +} +export class BandwidthNodeRegistryError extends Error { + readonly code="BANDWIDTH_NODE_REGISTRY_REFUSED" + constructor(){super("Постоянная привязка bandwidth-узла не подтверждена")} +} +const refuse=():never=>{throw new BandwidthNodeRegistryError()} +const evidenceSchema=z.object({version:z.literal(1),serverId:z.number().int().positive().max(Number.MAX_SAFE_INTEGER), + sourceProof:hash,authorityGeneration:z.string().min(1).max(200),routingEndpointEpoch:uuid, + observedAt:z.number().int().nonnegative(),expiresAt:z.number().int().positive(), + claims:z.array(z.object({kind:z.enum(["system-id","software-id"]),value:z.string().min(1).max(256)}).strict()).min(1).max(2), + physicalUniquenessVerified:z.literal(false),cloneResistanceVerified:z.literal(false)}).strict() +type Evidence=z.infer +type Sql={query(text:string,values?:unknown[]):Promise<{rows:Record[];command?:unknown}>} +type Alias={server_id:unknown;incarnation:unknown;node_id:unknown;status:unknown;record_mac:unknown} + +export function createBandwidthNodeRegistry(dependencies:BandwidthNodeRegistryDependencies){ + // Capture server-owned methods once. No request accepts a source, SQL facade, + // signing callback, chosen UUID or caller-asserted license evidence. + const deps=Object.freeze({now:dependencies.now.bind(dependencies),connect:dependencies.connect.bind(dependencies), + inspect:dependencies.inspect.bind(dependencies),authorize:dependencies.authorize.bind(dependencies),keyedProof:dependencies.keyedProof.bind(dependencies), + keyedProofs:dependencies.keyedProofs?.bind(dependencies),inspectAndAuthorize:dependencies.inspectAndAuthorize?.bind(dependencies)}) + const captureEvidence=(raw:unknown)=>{ + const value=evidenceSchema.parse(raw) + if(value.claims.some((claim,index)=>index>0&&value.claims[index-1].kind!=="system-id"||index===1&&claim.kind!=="software-id") + ||value.claims.length===2&&value.claims[0].kind===value.claims[1].kind)refuse() + value.claims.forEach(Object.freeze);Object.freeze(value.claims) + return Object.freeze(value) + } + const inspect=async(observation:unknown,scope:BandwidthScope)=>captureEvidence(await deps.inspect(observation,scope)) + const frames=new WeakSet() + const combined=async(action:BandwidthNodeRegistryAction,observation:unknown,requestedId:number|undefined,scope:BandwidthScope)=>{ + const raw=await deps.inspectAndAuthorize!(action,observation,requestedId,scope) + if(!raw||Object.getPrototypeOf(raw)!==Object.prototype||!Object.isFrozen(raw)||frames.has(raw))refuse() + const fields=Object.getOwnPropertyDescriptors(raw),keys=["action","observation","scope","serverId","evidence","authority"] + if(Reflect.ownKeys(raw).length!==keys.length||keys.some(key=>!fields[key]||!("value"in fields[key])||!fields[key].enumerable) + ||fields.action.value!==action||fields.observation.value!==observation||fields.scope.value!==scope)refuse() + frames.add(raw) + let budget=1000 + const data=(value:unknown,depth=0):void=>{ + if(--budget<0||depth>8)refuse() + if(!value||typeof value!=="object")return + const array=Array.isArray(value),descriptors=Object.getOwnPropertyDescriptors(value) + if(Object.getPrototypeOf(value)!==(array?Array.prototype:Object.prototype))refuse() + for(const key of Reflect.ownKeys(value)){ + if(array&&key==="length")continue + if(typeof key!=="string")return refuse() + const field=descriptors[key] + if(!field||!("value"in field)||!field.enumerable)return refuse() + data(field.value,depth+1) + } + } + data(fields.evidence.value);data(fields.authority.value) + const evidence=fields.evidence.value===undefined?undefined:captureEvidence(fields.evidence.value) + const serverId=fields.serverId.value + if(typeof serverId!=="number"||!Number.isSafeInteger(serverId)||serverId<1||requestedId!==undefined&&serverId!==requestedId + ||requestedId===undefined&&!evidence||evidence&&evidence.serverId!==serverId)refuse() + return Object.freeze({serverId:serverId as number,evidence,authority:Object.freeze(authoritySchema.parse(fields.authority.value))}) + } + async function transaction(action:BandwidthNodeRegistryAction,observation:unknown,requestedId:number|undefined,input:BandwidthScope, + work:(sql:Sql,authority:Authority,evidence:Evidence|undefined,proof:(purpose:string,value:unknown)=>Promise,proofs:(inputs:readonly BandwidthNodeProofInput[])=>Promise)=>Promise):Promise{ + if(!input||!Number.isSafeInteger(input.deadlineAt)||input.signal!==undefined&&!(input.signal instanceof AbortSignal))throw new BandwidthNodeRegistryError() + let connection:ProbeStorageConnection|undefined,commitStarted=false,transactionStarted=false,active=true,released=false,originalExpiry=Infinity + const startedAt=deps.now(),monotonicStart=performance.now() + const scope=Object.freeze({deadlineAt:Math.min(input.deadlineAt,startedAt+10000),signal:input.signal}) + const remaining=()=>Math.min(scope.deadlineAt,originalExpiry)-startedAt-(performance.now()-monotonicStart) + const release=()=>{if(connection&&!released){released=true;try{connection.release(true)}catch{/* No unsafe pool reuse. */}}} + const live=()=>{if(!active||scope.signal?.aborted||remaining()<=0||deps.now()>=Math.min(scope.deadlineAt,originalExpiry))refuse()} + let rejectStopped!:(error:BandwidthNodeRegistryError)=>void,timer:ReturnType + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject}) + const stop=()=>{active=false;release();rejectStopped(new BandwidthNodeRegistryError())} + const schedule=()=>{clearTimeout(timer);timer=setTimeout(stop,Math.max(1,Math.min(10000,remaining(),scope.deadlineAt-deps.now(),originalExpiry-deps.now())))} + schedule();scope.signal?.addEventListener("abort",stop,{once:true}) + const run=async()=>{ + try{ + live();const first=deps.inspectAndAuthorize?await combined(action,observation,requestedId,scope):undefined;live() + const evidence=first?first.evidence:requestedId===undefined?await inspect(observation,scope):undefined;live() + if(evidence){originalExpiry=evidence.expiresAt;schedule();live()} + const serverId=evidence?.serverId??requestedId! + if(!Number.isSafeInteger(serverId)||serverId<1)refuse() + const authority=first?first.authority:authoritySchema.parse(await deps.authorize(action,serverId,scope,evidence));live() + originalExpiry=Math.min(originalExpiry,authority.expiresAt);schedule();live() + if(authority.expiresAt<=deps.now()||evidence&&evidence.expiresAt<=deps.now())refuse() + const guard=async()=>{ + live();if(deps.now()>=authority.expiresAt||evidence&&deps.now()>=evidence.expiresAt)refuse() + if(deps.inspectAndAuthorize){ + const current=await combined(action,observation,serverId,scope);live() + if(!isDeepStrictEqual(current.evidence,evidence)||!isDeepStrictEqual(current.authority,authority))refuse() + return + } + if(evidence&&!isDeepStrictEqual(await inspect(observation,scope),evidence))refuse() + // The original enrollment/execution grant is the LAST asynchronous + // check before SQL/COMMIT. Native provenance may itself await source IO; + // a revocation during that IO must not authorize a subsequent write. + const current=authoritySchema.parse(await deps.authorize(action,serverId,scope,evidence));live() + if(!isDeepStrictEqual(current,authority))refuse() + live() + } + const freeze=(value:unknown):unknown=>{if(value&&typeof value==="object"){Object.values(value).forEach(freeze);Object.freeze(value)}return value} + const proof=async(purpose:string,value:unknown)=>{const snapshot=freeze(JSON.parse(JSON.stringify(value)));await guard();const result=await deps.keyedProof(purpose,snapshot,scope);await guard();if(!hash.safeParse(result).success)refuse();return result} + const proofs=async(inputs:readonly BandwidthNodeProofInput[])=>{ + if(!deps.keyedProofs){const values:string[]=[];for(const entry of inputs)values.push(await proof(entry.purpose,entry.value));return Object.freeze(values)} + const snapshot=freeze(JSON.parse(JSON.stringify(inputs))) as readonly BandwidthNodeProofInput[] + await guard() + const values=await deps.keyedProofs(snapshot,scope) + if(!Array.isArray(values)||Object.getPrototypeOf(values)!==Array.prototype||values.length!==snapshot.length||Reflect.ownKeys(values).length!==values.length+1)refuse() + const descriptors=Object.getOwnPropertyDescriptors(values) + const captured=Object.freeze(Array.from({length:values.length},(_,index)=>{ + const field=descriptors[String(index)] + if(!field||!("value"in field)||!field.enumerable||!hash.safeParse(field.value).success)refuse() + return field.value as string + })) + await guard() + return captured + } + connection=await deps.connect(scope);live() + const sql:Sql={query:async(text,values)=>{await guard();const result=await connection!.query(text,values,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-deps.now()))});await guard();return result}} + const started=await sql.query("BEGIN");transactionStarted=true;if(started.command!=="BEGIN")refuse() + await sql.query("SELECT pg_catalog.set_config('search_path','pg_catalog',true),pg_catalog.set_config('statement_timeout','5000',true),pg_catalog.set_config('lock_timeout','2000',true),pg_catalog.set_config('synchronous_commit','on',true)") + const checkAuthority=async()=>{ + const rows=(await sql.query("SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status FROM mmapp_control.bandwidth_node_authority WHERE id=1")).rows + if(rows.length!==1||rows[0].installation_id!==authority.installationId||rows[0].issuer_key_sha256!==authority.issuerKeySha256||rows[0].quiescence_proof!==authority.quiescenceProof||rows[0].status!=="active")refuse() + } + await checkAuthority() + // All state changes, including unrelated alias enrollment, serialize on + // this registry-only lock. It never releases or alters bandwidth owners. + if(action!=="resolve")await sql.query("SELECT pg_catalog.pg_advisory_xact_lock(8723116)") + const result=await work(sql,authority,evidence,proof,proofs) + await checkAuthority();await guard();commitStarted=true;const committed=await sql.query("COMMIT");if(committed.command!=="COMMIT")refuse();transactionStarted=false;await checkAuthority();await guard() + return result + }catch{ + if(active&&connection&&!released&&transactionStarted&&!commitStarted)try{await connection.query("ROLLBACK",undefined,{signal:scope.signal,timeoutMs:1000})}catch{/* Destroy the owned lease below. */} + throw new BandwidthNodeRegistryError() + }finally{release()} + } + try{return await Promise.race([run(),stopped])} + finally{active=false;clearTimeout(timer!);scope.signal?.removeEventListener("abort",stop);release()} + } + const root=(a:Authority)=>({installationId:a.installationId,issuerKeySha256:a.issuerKeySha256,quiescenceProof:a.quiescenceProof}) + const aliasMaterial=(a:Authority,row:Alias)=>({...root(a),serverId:Number(row.server_id),incarnation:row.incarnation,nodeId:row.node_id,status:row.status}) + const receipt=(row:Alias,outcome:"new"|"existing"):Readonly=>{ + if(!Number.isSafeInteger(Number(row.server_id))||Number(row.server_id)<1||!uuid.safeParse(row.incarnation).success||!uuid.safeParse(row.node_id).success||!["active","tombstoned","quarantined"].includes(String(row.status)))refuse() + return Object.freeze({serverId:Number(row.server_id),canonicalNodeId:String(row.node_id),incarnation:String(row.incarnation),status:row.status as BandwidthNodeRegistryReceipt["status"],outcome}) + } + const snapshot=async(sql:Sql,serverId:number,claimProof:string|null,lock:boolean)=>{ + const result=await sql.query(bandwidthRegistrySnapshotSql("managed",lock),[serverId,claimProof]) + if(result.command!=="SELECT")refuse() + return parseBandwidthRegistrySnapshot(result.rows,"managed",serverId,claimProof) + } + const snapshotAlias=(value:BandwidthRegistrySnapshot):Alias=>{ + const row=value.aliases[0];if(!row)refuse() + return {server_id:Number(row.subject),incarnation:row.incarnation,node_id:row.node_id,status:row.status,record_mac:row.record_mac} + } + const checkedAlias=async(a:Authority,value:BandwidthRegistrySnapshot,proofs:(inputs:readonly BandwidthNodeProofInput[])=>Promise)=>{ + const row=snapshotAlias(value);receipt(row,"existing") + const items=bandwidthRegistrySnapshotProofs("managed",value,root(a)) + const signed=await proofs(items.map(({purpose,value})=>({purpose,value}))) + if(items.some((item,index)=>item.expected!==signed[index]))refuse() + return value.nodes[0].claim_proof + } + const event=async(sql:Sql,a:Authority,row:Alias,kind:"enroll"|"tombstone"|"quarantine",proof:(purpose:string,value:unknown)=>Promise)=>{ + const id=randomUUID(),old=kind==="enroll"?null:"active" + const mac=await proof("event",{...root(a),eventId:id,serverId:Number(row.server_id),incarnation:row.incarnation,nodeId:row.node_id,kind,oldStatus:old,newStatus:row.status,authorityProof:a.authorityProof}) + const result=await sql.query("INSERT INTO mmapp_control.bandwidth_node_events(event_id,server_id,incarnation,node_id,kind,old_status,new_status,authority_proof,event_mac) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING event_id",[id,Number(row.server_id),row.incarnation,row.node_id,kind,old,row.status,a.authorityProof,mac]) + if(result.rows.length!==1||result.rows[0].event_id!==id)refuse() + } + return Object.freeze({ + register:(observation:unknown,scope:BandwidthScope)=>transaction("register",observation,undefined,scope,async(sql,a,e,proof,proofs)=>{ + const claim=await proof("claim",e!.claims),value=await snapshot(sql,e!.serverId,claim,true) + if(value.aliases.length){const row=snapshotAlias(value);if(row.status!=="active"||await checkedAlias(a,value,proofs)!==claim)refuse();return receipt(row,"existing")} + const candidate=value.claim_nodes[0],nodeId=candidate?.node_id??randomUUID() + const row:Alias={server_id:e!.serverId,incarnation:randomUUID(),node_id:nodeId,status:"active",record_mac:""} + const eventId=randomUUID() + const [nodeMac,aliasMac,eventMac]=await proofs([ + {purpose:"node",value:{...root(a),nodeId,claimProof:claim}}, + {purpose:"alias",value:aliasMaterial(a,row)}, + {purpose:"event",value:{...root(a),eventId,serverId:e!.serverId,incarnation:row.incarnation,nodeId,kind:"enroll",oldStatus:null,newStatus:"active",authorityProof:a.authorityProof}}, + ]) + if(candidate&&candidate.enrollment_mac!==nodeMac)refuse() + row.record_mac=aliasMac + const written=await sql.query(bandwidthRegistryEnrollmentSql({kind:"managed",subject:e!.serverId,insertNode:!candidate,nodeId,claimProof:claim,nodeMac,incarnation:String(row.incarnation),aliasMac:String(row.record_mac),eventId,authorityProof:a.authorityProof,eventMac})) + if(written.command!=="DO")refuse() + return receipt(row,"new") + }), + resolve:(observation:unknown,scope:BandwidthScope)=>transaction("resolve",observation,undefined,scope,async(sql,a,e,proof,proofs)=>{ + const value=await snapshot(sql,e!.serverId,null,false),row=snapshotAlias(value) + if(row.status!=="active"||await checkedAlias(a,value,proofs)!==await proof("claim",e!.claims))refuse() + return receipt(row,"existing") + }), + retire:(serverId:number,kind:"tombstone"|"quarantine",scope:BandwidthScope)=>{ + if(kind!=="tombstone"&&kind!=="quarantine")return Promise.reject(new BandwidthNodeRegistryError()) + return transaction(kind,undefined,serverId,scope,async(sql,a,_e,proof,proofs)=>{ + const value=await snapshot(sql,serverId,null,true),row=snapshotAlias(value);await checkedAlias(a,value,proofs) + const next=kind==="tombstone"?"tombstoned":"quarantined" + if(row.status===next)return receipt(row,"existing") + if(row.status!=="active")refuse() + const updated:Alias={...row,status:next};updated.record_mac=await proof("alias",aliasMaterial(a,updated)) + const changed=await sql.query("UPDATE mmapp_control.bandwidth_node_aliases SET status=$2,record_mac=$3 WHERE server_id=$1 AND status='active' AND record_mac=$4 RETURNING server_id",[serverId,next,updated.record_mac,row.record_mac]);if(changed.rows.length!==1||Number(changed.rows[0].server_id)!==serverId)refuse() + await event(sql,a,updated,kind,proof);return receipt(updated,"new") + }) + }, + }) +} diff --git a/backend/src/services/bandwidth-node-source.sql.test.ts b/backend/src/services/bandwidth-node-source.sql.test.ts new file mode 100644 index 00000000..b6134c08 --- /dev/null +++ b/backend/src/services/bandwidth-node-source.sql.test.ts @@ -0,0 +1,433 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomBytes, randomUUID, sign } from "node:crypto" +import { createServer } from "node:http" +import { once } from "node:events" +import { writeFile } from "node:fs/promises" +import { execFile } from "node:child_process" +import { promisify } from "node:util" +import { resolve } from "node:path" +import type { BandwidthNodeObservationEvidence, BandwidthNodeSourceSnapshot } from "./bandwidth-node-observation.js" +import type { BandwidthNodeRegistryAction } from "./bandwidth-node-registry.js" +import type { BandwidthControlDispatchFence } from "./bandwidth-control-source.js" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState, scope } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql(); await prepareOwnedBandwidthState() +const { pool: admin, closePool } = await import("../db/index.js"), { Pool, Client } = await import("pg") +const { applySqlMigrations } = await import("../db/migrate.js") +const { BANDWIDTH_ADMISSION_PROPOSAL_DDL } = await import("./bandwidth-admission-schema.js") +const { BANDWIDTH_NODE_REGISTRY_DDL } = await import("./bandwidth-node-registry-schema.js") +const { BANDWIDTH_CONTROL_SOURCE_DDL, canonicalBandwidthControlRecord, bandwidthControlEndpointSha256, bandwidthControlSchemaSourceSha256, observeBandwidthControlCatalog, createConfiguredBandwidthControlSource } = await import("./bandwidth-control-source.js") +const { canonicalBandwidthNodeValue, canonicalBandwidthNodeBootstrap, bandwidthNodeRegistrySchemaSourceSha256 } = await import("./bandwidth-node-bootstrap.js") +const { observeBandwidthNodeRegistryCatalog } = await import("./bandwidth-node-registry-catalog.js") +const { createConfiguredBandwidthNodeSource } = await import("./bandwidth-node-source.js") +const { createBandwidthNodeObservationReader } = await import("./bandwidth-node-observation.js") +const { createBandwidthNodeRegistry } = await import("./bandwidth-node-registry.js") +// Real application startup loads the native client before these short phases. +// Do the same in this owned TS fixture; never renew an observed token's TTL. +await import("./mikrotik.js") +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +const suffix = randomUUID().replaceAll("-", ""), ownerRole = "bns_owner_" + suffix, runtimeRole = "bns_runtime_" + suffix, appRole = "bns_app_" + suffix, databaseName = "bns_control_" + suffix +const ident = (value: string) => { assert(/^[a-z_]+[a-f0-9]{32}$/.test(value)); return '"' + value + '"' } +const passwords = new Map([ownerRole, runtimeRole, appRole].map(role => [role, randomBytes(32).toString("hex")])) +const base = new URL(process.env.DATABASE_URL!), pools: InstanceType[] = [], roles: string[] = [] +const connectionUrl = (role: string, name = databaseName) => { const value = new URL(base); value.pathname = "/" + name; value.username = role; value.password = passwords.get(role)!; return value.toString() } +const rolePool = (role: string) => { const pool = new Pool({ connectionString: connectionUrl(role), max: 4 }); pools.push(pool); return pool } +const cases: string[] = [], limitations: string[] = [] +const allTables = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events", "bandwidth_source_identity", "bandwidth_node_authority", "bandwidth_nodes", "bandwidth_node_aliases", "bandwidth_node_events"] +let databaseCreated = false, nativeGets = 0 +const endpoints = new Map(), licenses = new Map>() +const server = createServer((req, res) => { + assert.equal(req.method, "GET"); assert.equal(req.url, "/rest/system/license"); nativeGets++ + const credentials = Buffer.from((req.headers.authorization ?? "").slice(6), "base64").toString(), id = Number(/^node-(\d+):/.exec(credentials)?.[1]) + res.setHeader("content-type", "application/json"); res.end(JSON.stringify(licenses.get(id) ?? {})) +}) +server.listen(0, "127.0.0.1"); await once(server, "listening") +const address = server.address(); assert(address && typeof address !== "string"); const port = address.port +const reader = createBandwidthNodeObservationReader({ async read(id) { return endpoints.get(id) } }) +let core: ReturnType | undefined, nodeSource: ReturnType | undefined +const extraSources: Array> = [] +let routerGuardCompatibilityCleanup: (() => Promise) | undefined +const eventKey = randomBytes(32), issuerKey = randomBytes(32), actorKey = randomBytes(32) +const originalDriverQuery = Client.prototype.query +type Actor = { id: number; action: BandwidthNodeRegistryAction; allowed: boolean; expiresAt: number; approvedClaims: unknown; sourceProof: string } +let intercept: { actor: Actor; armed: boolean; fired: boolean; writesAfter: number; commits: number } | undefined +let catalogMutation: { fired: boolean; run: () => Promise } | undefined +let physicalRuntimeQueries = 0 +let closedCoreMainDispatches = 0 +// Instrument the real driver only in this disposable test. No response/result +// is synthesized and no production source callback is replaced. +Client.prototype.query = function (this: InstanceType, ...args: unknown[]) { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "" + const target = this as unknown as { connectionParameters: { user: string; database: string } } + const isRuntime = target.connectionParameters.user === runtimeRole && target.connectionParameters.database === databaseName + if (isRuntime) physicalRuntimeQueries++ + if (isRuntime && text.includes("CORE-CLOSED-GUARD-NO-DISPATCH")) closedCoreMainDispatches++ + const watch = isRuntime ? intercept : undefined, mutation = isRuntime ? catalogMutation : undefined + if (watch?.fired && /^(?:DO|INSERT|UPDATE|DELETE|COMMIT)\b/i.test(text.trim())) watch.writesAfter++ + if (watch && /^COMMIT\b/i.test(text.trim())) watch.commits++ + const result = Reflect.apply(originalDriverQuery, this, args) as unknown + if (!result || typeof (result as Promise).then !== "function") return result + return (result as Promise).then(async value => { + const nodeCatalog = text.includes("'bandwidth_node_authority','bandwidth_nodes','bandwidth_node_aliases','bandwidth_node_events'") && text.includes("p.prosrc AS body") + if (watch?.armed && !watch.fired && nodeCatalog) { watch.actor.allowed = false; watch.fired = true } + if (mutation && !mutation.fired && nodeCatalog) { mutation.fired = true; await mutation.run() } + return value + }) +} as typeof Client.prototype.query +try { + for (const role of [ownerRole, runtimeRole, appRole]) { + const password = passwords.get(role)!; assert(/^[a-f0-9]{64}$/.test(password)) + await admin.query(`CREATE ROLE ${ident(role)} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS NOINHERIT`); roles.push(role) + } + await admin.query(`CREATE DATABASE ${ident(databaseName)} OWNER ${ident(ownerRole)} TEMPLATE template0`); databaseCreated = true + const owner = rolePool(ownerRole), runtime = rolePool(runtimeRole), app = rolePool(appRole) + const adminUrl = new URL(base); adminUrl.pathname = "/" + databaseName + const targetAdmin = new Pool({ connectionString: adminUrl.toString() }); pools.push(targetAdmin) + await targetAdmin.query(`ALTER SCHEMA public OWNER TO ${ident(appRole)}`) + await applySqlMigrations(app) + await owner.query("CREATE SCHEMA mmapp_control"); await owner.query(BANDWIDTH_ADMISSION_PROPOSAL_DDL); await owner.query(BANDWIDTH_CONTROL_SOURCE_DDL); await owner.query(BANDWIDTH_NODE_REGISTRY_DDL) + await owner.query("REVOKE ALL ON SCHEMA mmapp_control FROM PUBLIC; REVOKE ALL ON ALL TABLES IN SCHEMA mmapp_control FROM PUBLIC; REVOKE ALL ON ALL FUNCTIONS IN SCHEMA mmapp_control FROM PUBLIC") + await owner.query(`GRANT USAGE ON SCHEMA mmapp_control TO ${ident(runtimeRole)}`) + await owner.query(`GRANT SELECT,INSERT,UPDATE ON mmapp_control.bandwidth_attempts,mmapp_control.bandwidth_node_aliases TO ${ident(runtimeRole)}`) + await owner.query(`GRANT SELECT,INSERT,DELETE ON mmapp_control.bandwidth_node_occupancy TO ${ident(runtimeRole)}`) + await owner.query(`GRANT SELECT,INSERT ON mmapp_control.bandwidth_attempt_events,mmapp_control.bandwidth_nodes,mmapp_control.bandwidth_node_events TO ${ident(runtimeRole)}`) + await owner.query(`GRANT SELECT ON mmapp_control.bandwidth_source_identity,mmapp_control.bandwidth_node_authority TO ${ident(runtimeRole)}`) + await owner.query(`GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA mmapp_control TO ${ident(runtimeRole)}`) + const functions = (await owner.query("SELECT count(*)::int AS n FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace WHERE n.nspname='mmapp_control'")).rows[0].n + assert.equal(functions, 12) + assert.equal(Number((await app.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + await assert.rejects(app.query("SELECT * FROM mmapp_control.bandwidth_node_authority")) + await assert.rejects(runtime.query("UPDATE mmapp_control.bandwidth_node_authority SET status='quarantined'")) + cases.push("actual separate owner/combined least-privilege runtime/restricted application roles, explicit external core+registry DDL with 12 functions, normal39 unchanged, no bootstrap rows auto-created") + + const live = () => {}, { publicKey, privateKey } = generateKeyPairSync("ed25519") + const installationId = randomUUID(), controlGeneration = randomUUID(), issuerKeySha256 = sha(issuerKey) + const quiescenceProof = sha("SYNTHETIC owned operator/quiescence statement; not legitimate production enrollment") + const coreRecord = { format: "mmapp-bandwidth-control-source-v1" as const, installationId, controlGeneration, databaseName, runtimeRole, ownerRole, + databaseOid: Number((await runtime.query("SELECT oid::text AS id FROM pg_catalog.pg_database WHERE datname=current_database()")).rows[0].id), + endpointSha256: bandwidthControlEndpointSha256(connectionUrl(runtimeRole)), schemaSourceSha256: bandwidthControlSchemaSourceSha256(), catalogSha256: await observeBandwidthControlCatalog(runtime, live), + eventKeySha256: sha(eventKey), attestationRef: "Owned synthetic external control record with genuine Ed25519 signature; not native readiness" } + const bootstrap = { format: "mmapp-bandwidth-node-bootstrap-v1" as const, installationId, issuerKeySha256, quiescenceProof, + registrySchemaSourceSha256: bandwidthNodeRegistrySchemaSourceSha256(), registryCatalogSha256: await observeBandwidthNodeRegistryCatalog(runtime, live), + attestationRef: "Owned synthetic operator registry bootstrap with genuine signature; no production enrollment" } + const signedCore = (value: unknown) => { const bytes = Buffer.from(canonicalBandwidthControlRecord(value)); return { format: "mmapp-bandwidth-control-envelope-v1", payload: bytes.toString("base64url"), signature: sign(null, bytes, privateKey).toString("base64url") } } + const signedBootstrap = (value: unknown) => { const bytes = Buffer.from(canonicalBandwidthNodeBootstrap(value)); return { format: "mmapp-bandwidth-node-bootstrap-envelope-v1", payload: bytes.toString("base64url"), signature: sign(null, bytes, privateKey).toString("base64url") } } + const attestationFile = resolve(process.env.MCP_STATE_DIR!, "node-source-core.json"), trustKeyFile = resolve(process.env.MCP_STATE_DIR!, "node-source-trust.pem"), eventKeyFile = resolve(process.env.MCP_STATE_DIR!, "node-source-core.key") + const bootstrapFile = resolve(process.env.MCP_STATE_DIR!, "node-source-bootstrap.json"), issuerKeyFile = resolve(process.env.MCP_STATE_DIR!, "node-source-issuer.key") + const originalBootstrap = signedBootstrap(bootstrap) + for (const [file, bytes] of [[attestationFile, JSON.stringify(signedCore(coreRecord))], [trustKeyFile, publicKey.export({ type: "spki", format: "pem" })], [eventKeyFile, eventKey], [bootstrapFile, JSON.stringify(originalBootstrap)], [issuerKeyFile, issuerKey]] as const) + await writeFile(file, bytes, { flag: "wx", mode: 0o600 }) + await owner.query("INSERT INTO mmapp_control.bandwidth_source_identity(id,installation_id,control_generation,attestation_sha256,event_key_sha256) VALUES(1,$1,$2,$3,$4)", [installationId, controlGeneration, sha(canonicalBandwidthControlRecord(coreRecord)), sha(eventKey)]) + await owner.query("INSERT INTO mmapp_control.bandwidth_node_authority(id,installation_id,issuer_key_sha256,quiescence_proof,status) VALUES(1,$1,$2,$3,'active')", [installationId, issuerKeySha256, quiescenceProof]) + const config = { databaseUrl: connectionUrl(runtimeRole), applicationDatabaseUrl: connectionUrl(appRole, "postgres"), attestationFile, trustKeyFile, + trustKeySha256: sha(publicKey.export({ type: "spki", format: "der" })), eventKeyFile } + core = createConfiguredBandwidthControlSource(config) + const nodeConfig = { issuerKeyFile, issuerKeySha256, bootstrapFile } + nodeSource = createConfiguredBandwidthNodeSource(core, nodeConfig) + const source = nodeSource, coreSource = core + if (process.env.MMAPP_TEST_ROUTER_GUARD_COMPATIBILITY === "true") { + const { acceptOwnedRouterGuardControlCompatibility } = await import("./router-configuration-guard-compatibility.sql-helper.js") + const accepted = await acceptOwnedRouterGuardControlCompatibility({ admin: targetAdmin, databaseName, runtimeRole, ownerRole, v2: false, core: coreSource, node: source }) + routerGuardCompatibilityCleanup = accepted.cleanup + cases.push("router guard same-control compatibility: " + JSON.stringify(accepted.report)) + } + async function snapshot(tables = allTables) { + const result: Record = {} + for (const table of tables) { assert(/^bandwidth_[a-z_]+$/.test(table)); result[table] = sha(canonicalBandwidthNodeValue((await owner.query(`SELECT to_jsonb(t) AS row FROM mmapp_control.${table} t ORDER BY to_jsonb(t)::text`)).rows)) } + return result + } + async function endpoint(claims: Record) { + const id = Number((await app.query("INSERT INTO public.servers(name,host,enabled) VALUES('node-source-fixture','127.0.0.1',false) RETURNING id")).rows[0].id) + endpoints.set(id, { serverId: id, enabled: true, sourceProof: sha("owned-source:" + id), authorityGeneration: "owned-source-v1", + endpoint: { host: "127.0.0.1", port, useSsl: false, verifySsl: true, username: "node-" + id, password: "LOCALHOST-NATIVE-PRIVATE", routingEndpointEpoch: randomUUID() } }) + licenses.set(id, claims); return id + } + const actorFor = (id: number, action: BandwidthNodeRegistryAction): Actor => ({ id, action, allowed: true, expiresAt: Date.now() + 120000, + approvedClaims: ["system-id", "software-id"].filter(kind => Object.hasOwn(licenses.get(id)!, kind)).map(kind => ({ kind, value: licenses.get(id)![kind] })), sourceProof: endpoints.get(id)!.sourceProof }) + const actorGuard = (actor: Actor) => { if (!actor.allowed || Date.now() >= actor.expiresAt) throw Error("Original actor grant revoked/expired") } + async function operation(id: number, action: "register" | "resolve", actor = actorFor(id, action), armPhase?: "INSERT" | "COMMIT") { + const diagnosticBegan=Date.now() + const diagnosticQueries=physicalRuntimeQueries + let diagnosticBindings=0,diagnosticProofs=0,diagnosticCommands=0,diagnosticLast="open" + const parentScope = scope(10000), token = await reader.observe(id, parentScope), observed = await reader.inspect(token, parentScope) + const diagnosticObserved=Date.now() + // This fixed composition supplies original native/actor lifetimes. NodeSource + // cannot infer authority or observation TTL from an opaque connection scope. + const phaseScope = Object.freeze({ deadlineAt: Math.min(parentScope.deadlineAt, observed.expiresAt, actor.expiresAt), signal: parentScope.signal }) + const originalIntentGuard = async (evidence?: Readonly) => { + // Reader freshness uses the trusted application provider, never this core + // lease. Actor revocation is checked LAST after asynchronous observation. + const genuine = await reader.inspect(token, phaseScope) + assert.equal(genuine.sourceProof, actor.sourceProof); assert.deepEqual(genuine.claims, actor.approvedClaims) + if (evidence) { assert.equal(evidence.sourceProof, genuine.sourceProof); assert.deepEqual(evidence.claims, genuine.claims) } + actorGuard(actor) + } + const session = await source.open(phaseScope, () => originalIntentGuard()) + const diagnosticOpened=Date.now() + try { + const host = createBandwidthNodeRegistry({ now: Date.now, connect: async () => ({ release: () => session.close(), async query(text, values, options) { + diagnosticCommands++;diagnosticLast=text.startsWith("DO ")?"register-batch":text.startsWith("WITH aliases")?"snapshot":text.startsWith("SELECT installation_id")?"authority":text.startsWith("SELECT pg_catalog.set_config")?"config":text.includes("pg_advisory_xact_lock")?"registry-lock":["BEGIN","COMMIT","ROLLBACK"].includes(text)?text:"other" + if (intercept && (armPhase === "INSERT" && (text.startsWith("DO $mmapp_bandwidth_register$") || text.startsWith("INSERT INTO mmapp_control.bandwidth_nodes")) || armPhase === "COMMIT" && text === "COMMIT")) intercept.armed = true + return session.connection.query(text, values, options) + } }), inspect: (value, current) => reader.inspect(value, current), + keyedProof: (purpose, value) => {diagnosticProofs++;return session.keyedProof(purpose, value)}, + keyedProofs: inputs => {diagnosticProofs++;return session.keyedProofs(inputs)}, + authorize: async (wanted, serverId, _current, evidence) => { + assert.equal(wanted, actor.action); assert.equal(serverId, actor.id) + const genuine = evidence as Readonly | undefined + assert(genuine) + diagnosticBindings++;const binding = await session.binding(); await originalIntentGuard(genuine) + return { installationId: binding.installationId, issuerKeySha256: binding.issuerKeySha256, quiescenceProof: binding.quiescenceProof, + authorityProof: createHmac("sha256", actorKey).update(canonicalBandwidthNodeValue({ wanted, serverId, intent: actor.approvedClaims })).digest("hex"), authorityGeneration: "owned-original-actor-v1", expiresAt: actor.expiresAt } + } }) + return action === "register" ? await host.register(token, phaseScope) : await host.resolve(token, phaseScope) + } catch(error){ + console.error("Owned node registry phase timing:",JSON.stringify({action,observeMs:diagnosticObserved-diagnosticBegan,openMs:diagnosticOpened-diagnosticObserved,registryMs:Date.now()-diagnosticOpened,observationRemainingMs:observed.expiresAt-Date.now(),physicalSql:physicalRuntimeQueries-diagnosticQueries,bindings:diagnosticBindings,proofs:diagnosticProofs,commands:diagnosticCommands,last:diagnosticLast})) + throw error + } finally { session.close() } + } + const a = await endpoint({ "system-id": "OP-SYS", "software-id": "OP-SOFT" }), b = await endpoint({ "system-id": "OP-SYS", "software-id": "OP-SOFT" }) + const first = await operation(a, "register"), second = await operation(b, "register") + assert.equal(first.canonicalNodeId, second.canonicalNodeId); assert.notEqual(first.incarnation, second.incarnation) + assert.equal((await operation(a, "resolve")).canonicalNodeId, first.canonicalNodeId) + cases.push("actual configured core+node source, genuine native reader and registry host compose register/resolve through one session with actor sealed intent; same claims preserve canonical UUID across aliases") + const {acceptBandwidthExecutionHost}=await import("./bandwidth-execution-host.sql-helper.js") + const executionHostAcceptance=await acceptBandwidthExecutionHost(coreSource,source,owner) + cases.push(...executionHostAcceptance.checks) + { + const tokens=await Promise.all([reader.observe(a,scope()),reader.observe(b,scope())]) + const evidences=await Promise.all(tokens.map(token=>reader.inspect(token,scope()))) + const window=scope(4500),session=await source.open(window) + const binding=await session.binding();session.close() + const expected={binding,participants:[{evidence:evidences[0],receipt:first},{evidence:evidences[1],receipt:second}]} + const before=await snapshot() + const leases=await Promise.all(Array.from({length:4},()=>coreSource.connect(window,async fence=>source.assertResolvedOnFence(fence,expected,window)))) + try{ + await Promise.all(leases.map(lease=>lease.query("BEGIN"))) + const pids=await Promise.all(leases.map(async lease=>(await lease.query("SELECT pg_catalog.pg_backend_pid() AS pid")).rows[0].pid)) + assert.equal(new Set(pids).size,4) + assert.equal((await owner.query("SELECT pg_catalog.pg_try_advisory_xact_lock(8723116) AS locked")).rows[0].locked,false) + await Promise.all(leases.map(lease=>lease.query("COMMIT"))) + }finally{leases.forEach(lease=>lease.release(true))} + assert.equal((await owner.query("SELECT pg_catalog.pg_try_advisory_xact_lock(8723116) AS locked")).rows[0].locked,true) + assert.deepEqual(await snapshot(),before) + cases.push("four actual occupied core leases authenticate signed node role/catalog/claim/node/alias/event MACs through the SAME ephemeral fence with no fifth checkout; shared registry writer lock persists through real BEGIN/COMMIT and data remains exact") + for(const change of ["claim","incarnation","source-record"] as const){ + const freshTokens=await Promise.all([reader.observe(a,scope()),reader.observe(b,scope())]) + const freshEvidence=await Promise.all(freshTokens.map(token=>reader.inspect(token,scope()))) + const altered={binding:{...binding},participants:[{evidence:freshEvidence[0],receipt:{...first}},{evidence:freshEvidence[1],receipt:{...second}}]} + if(change==="claim")altered.participants[0].evidence={...altered.participants[0].evidence,claims:[{kind:"system-id",value:"FORGED-LICENSE"}]} + if(change==="incarnation")altered.participants[0].receipt={...altered.participants[0].receipt,incarnation:randomUUID()} + if(change==="source-record")altered.binding.sourceRecordSha256="a".repeat(64) + const freshWindow=scope(4000),lease=await coreSource.connect(freshWindow,async fence=>source.assertResolvedOnFence(fence,altered,freshWindow)) + try{await assert.rejects(lease.query("SELECT 1 AS forbidden_fence_marker"))}finally{lease.release(true)} + assert.deepEqual(await snapshot(),before) + } + cases.push("same-physical node fence refuses forged claims, replaced alias incarnation and original signed source hash without changing any of eight control tables") + } + { + const window = scope(10000), session = await source.open(window) + try { + const binding = await session.binding(); assert.equal(binding.installationId, installationId); assert.equal(binding.controlGeneration, controlGeneration) + assert.equal(Object.hasOwn(binding, "approved"), false); assert.equal(Object.hasOwn(binding, "ready"), false) + await assert.rejects(session.keyedProof("unknown", {})); await assert.rejects(session.keyedProof("node", { ...binding, nodeId: randomUUID(), claimProof: "a".repeat(64) })) + const value = { installationId, issuerKeySha256, quiescenceProof, nodeId: randomUUID(), claimProof: "a".repeat(64) }, original = canonicalBandwidthNodeValue(value) + const pending = session.keyedProof("node", value); value.claimProof = "b".repeat(64) + assert.equal(await pending, createHmac("sha256", issuerKey).update("mmapp-bandwidth-node-node-v1:" + original).digest("hex")) + await assert.rejects(session.keyedProof("node", { installationId: randomUUID(), issuerKeySha256, quiescenceProof, nodeId: randomUUID(), claimProof: "a".repeat(64) })) + } finally { session.close(); session.close() } + await assert.rejects(session.binding()) + const actor = actorFor(a, "register"); actor.allowed = false + const before = await snapshot(); await assert.rejects(operation(a, "register", actor)); assert.deepEqual(await snapshot(), before) + cases.push("binding/HMAC never grant actor permission; unknown/malformed/root-mismatched signing inputs refuse, proof input captured before await, closed session unusable and actor denial changes no row") + } + { + const coreLease = await coreSource.connect(scope(10000)), foreign = await coreSource.connect(scope(10000)) + const other = createConfiguredBandwidthControlSource(config) + try { + await assert.rejects(other.verifyNodeBootstrap(originalBootstrap, coreLease, scope())) + await assert.rejects(coreSource.verifyNodeBootstrap(originalBootstrap, { query: async () => ({ rows: [] }), release() {} }, scope())) + foreign.release(); await assert.rejects(coreSource.verifyNodeBootstrap(originalBootstrap, foreign, scope())) + assert.equal((await coreSource.verifyNodeBootstrap(originalBootstrap, coreLease, scope())).bootstrap.installationId, installationId) + } finally { coreLease.release(); foreign.release(); await other.close() } + cases.push("bootstrap verifier requires its own active genuine core lease: different factory, fake facade and released lease cannot establish provenance") + } + { + // Four sessions deliberately saturate the fixed core pool. Every binding, + // HMAC and query must stay on its already-owned connection, not borrow #5. + const sessions = await Promise.all(Array.from({ length: 4 }, () => source.open(scope(10000)))) + try { + const result = await Promise.all(sessions.map(async session => { + const firstPid = Number((await session.connection.query("SELECT pg_catalog.pg_backend_pid() AS pid")).rows[0].pid) + await session.binding(); await session.keyedProof("claim", [{ kind: "system-id", value: "FOUR-LEASES" }]) + const after = Number((await session.connection.query("SELECT pg_catalog.pg_backend_pid() AS pid")).rows[0].pid) + assert.equal(firstPid, after); return firstPid + })) + assert.equal(new Set(result).size, 4) + } finally { sessions.forEach(session => session.close()) } + cases.push("four concurrent sessions saturate actual core pool: binding/proof/query retain four distinct original backend PIDs with no nested fifth checkout/deadlock") + } + { + const before = await snapshot(), session = await source.open(scope(10000)) + try { + const changed = signedBootstrap({ ...bootstrap, attestationRef: "A DIFFERENT genuinely signed operator statement must not replace a leased original binding" }) + await writeFile(bootstrapFile, JSON.stringify(changed)); await assert.rejects(session.binding()) + } finally { session.close(); await writeFile(bootstrapFile, JSON.stringify(originalBootstrap)) } + const malformed = [ + { ...originalBootstrap, signature: "A".repeat(86) }, + signedBootstrap({ ...bootstrap, installationId: randomUUID() }), + signedBootstrap({ ...bootstrap, registrySchemaSourceSha256: "f".repeat(64) }), + signedBootstrap({ ...bootstrap, issuerKeySha256: sha(eventKey) }), + { ...originalBootstrap, unexpected: true }, + ] + for (const envelope of malformed) { + await writeFile(bootstrapFile, JSON.stringify(envelope)); await assert.rejects(source.open(scope(10000))) + } + await writeFile(bootstrapFile, JSON.stringify(originalBootstrap)) + await writeFile(issuerKeyFile, randomBytes(32)); await assert.rejects(source.open(scope(10000))); await writeFile(issuerKeyFile, issuerKey) + const wrongPin = createConfiguredBandwidthNodeSource(coreSource, { ...nodeConfig, issuerKeySha256: "f".repeat(64) }); extraSources.push(wrongPin) + await assert.rejects(wrongPin.open(scope(10000))) + const missing = createConfiguredBandwidthNodeSource(coreSource, { ...nodeConfig, bootstrapFile: resolve(process.env.MCP_STATE_DIR!, "MISSING-OPERATOR-BOOTSTRAP.json") }); extraSources.push(missing) + await assert.rejects(missing.open(scope(10000))) + assert.deepEqual(await snapshot(), before) + cases.push("wrong issuer key/pin, same-key-as-core bootstrap, cross-install/schema/signature/unknown-field records and missing bootstrap refuse; valid different signed bootstrap cannot replace original live session; no auto rows or data mutation") + } + { + const before = await snapshot() + await owner.query(`GRANT CREATE ON SCHEMA mmapp_control TO ${ident(runtimeRole)}`) + await assert.rejects(source.open(scope(10000))); await owner.query(`REVOKE CREATE ON SCHEMA mmapp_control FROM ${ident(runtimeRole)}`) + await owner.query("ALTER TABLE mmapp_control.bandwidth_nodes DISABLE TRIGGER bandwidth_node_immutable_guard") + try { await assert.rejects(source.open(scope(10000))) } + finally { await owner.query("ALTER TABLE mmapp_control.bandwidth_nodes ENABLE ALWAYS TRIGGER bandwidth_node_immutable_guard") } + assert.deepEqual(await snapshot(), before) + cases.push("actual excess runtime ACL and disabled registry guard refuse via fixed source/role/catalog verification; guard disabling is metadata-only negative fixture with zero data writes") + } + for (const phase of ["INSERT", "COMMIT"] as const) { + const id = await endpoint({ "system-id": "REVOKED-IN-METADATA-" + phase }), actor = actorFor(id, "register"), before = await snapshot() + intercept = { actor, armed: false, fired: false, writesAfter: 0, commits: 0 } + try { + await assert.rejects(operation(id, "register", actor, phase)) + assert.equal(intercept.fired, true); assert.equal(intercept.writesAfter, 0); assert.equal(intercept.commits, 0) + assert.deepEqual(await snapshot(), before) + } finally { intercept = undefined } + cases.push("real original actor revoke DURING global catalog observation before " + phase + ": final core dispatch guard forbids physical write/COMMIT and preserves exact data") + } + for (const kind of ["issuer-key", "bootstrap"] as const) { + const before = await snapshot(), session = await source.open(scope(10000)) + const replacement = signedBootstrap({ ...bootstrap, attestationRef: "Different genuinely signed operator bootstrap changed DURING actual PostgreSQL catalog observation" }) + catalogMutation = { fired: false, run: () => kind === "issuer-key" ? writeFile(issuerKeyFile, randomBytes(32)) : writeFile(bootstrapFile, JSON.stringify(replacement)) } + try { + await assert.rejects(session.keyedProof("claim", [{ kind: "system-id", value: "NO-STALE-HMAC" }])) + assert.equal(catalogMutation.fired, true) + assert.deepEqual(await snapshot(), before) + } finally { + catalogMutation = undefined; session.close() + await writeFile(issuerKeyFile, issuerKey); await writeFile(bootstrapFile, JSON.stringify(originalBootstrap)) + } + cases.push("actual " + kind + " file changes AFTER PostgreSQL node-catalog result and before HMAC: operation refuses without returning stale proof or changing any control row") + } + { + let stalled = false, entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), gate = new Promise(resolve => { release = resolve }) + const before = await snapshot(), session = await source.open(scope(10000), async () => { if (stalled) { entered(); await gate } }) + stalled = true + const pending = session.connection.query("SELECT 'CLOSED-SESSION-MUST-NOT-DISPATCH'::text") + const rejected = assert.rejects(pending) + let timer: ReturnType | undefined, startTimer: ReturnType | undefined + try { + await Promise.race([started, new Promise((_resolve, reject) => { startTimer = setTimeout(() => reject(Error("Actual query did not reach original actor guard")), 3000) })]) + clearTimeout(startTimer); session.close() + await Promise.race([rejected, new Promise((_resolve, reject) => { timer = setTimeout(() => reject(Error("Closed session did not promptly reject stalled actor guard")), 750) })]) + const countAfterClose = physicalRuntimeQueries + release(); await new Promise(resolve => setTimeout(resolve, 75)) + assert.equal(physicalRuntimeQueries, countAfterClose) + assert.deepEqual(await snapshot(), before) + } finally { clearTimeout(timer); clearTimeout(startTimer); release(); session.close() } + cases.push("closing an actual session while original actor guard is stalled rejects before gate/deadline; releasing late guard dispatches no further physical SQL and changes no row") + } + { + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), gate = new Promise(resolve => { release = resolve }) + const before = await snapshot(), lease = await coreSource.connect(scope(10000), async () => { entered(); await gate }) + const dispatchesBefore = closedCoreMainDispatches + const pending = lease.query("SELECT 'CORE-CLOSED-GUARD-NO-DISPATCH'::text"), rejected = assert.rejects(pending) + let timer: ReturnType | undefined, startTimer: ReturnType | undefined + try { + await Promise.race([started, new Promise((_resolve, reject) => { startTimer = setTimeout(() => reject(Error("Core query did not reach actual guard")), 3000) })]) + clearTimeout(startTimer); lease.release(true) + await Promise.race([rejected, new Promise((_resolve, reject) => { timer = setTimeout(() => reject(Error("Released core lease did not reject stalled guard")), 750) })]) + const queriesAfterRelease = physicalRuntimeQueries + release(); await new Promise(resolve => setTimeout(resolve, 75)) + assert.equal(physicalRuntimeQueries, queriesAfterRelease); assert.equal(closedCoreMainDispatches, dispatchesBefore) + assert.deepEqual(await snapshot(), before) + } finally { clearTimeout(timer); clearTimeout(startTimer); release(); lease.release(true) } + cases.push("direct actual core lease release rejects query stalled AFTER checkout within750ms while guard gate is closed; late guard continuation sends zero main/late SQL and preserves all eight tables") + } + { + let saved: BandwidthControlDispatchFence | undefined + const before = await snapshot(), lease = await coreSource.connect(scope(10000), async fence => { saved = fence }) + try { + assert.equal((await lease.query("SELECT 1::integer AS marker")).rows[0].marker, 1) + assert(saved) + const count = physicalRuntimeQueries + await assert.rejects(saved.query("SELECT 1")); await assert.rejects(saved.verifyNodeBootstrap(originalBootstrap));await assert.rejects(saved.sourceIdentity()) + assert.equal(physicalRuntimeQueries, count); assert.deepEqual(await snapshot(), before) + } finally { lease.release(true) } + cases.push("captured genuine raw dispatch fence expires when actual query finishes: retained query and valid signed bootstrap verification reject without any new physical driver query") + } + if (process.env.MMAPP_OWN_BANDWIDTH_RESTORE === "true") { + const { runPrivateDatabaseDump } = await import("./database-dump-connection.js"), { restorePartitionedDatabase } = await import("./system-database-partition-restore.js") + const execute = promisify(execFile), url = new URL(connectionUrl(appRole)), file = resolve(process.env.MCP_STATE_DIR!, "operational-node-source-public.dump") + const env = { ...process.env, PGHOST: url.hostname, PGPORT: url.port, PGUSER: appRole, PGPASSWORD: passwords.get(appRole)! } + const command = async (program: string, args: string[]): Promise => { try { return (await execute(program, args, { env, encoding: "buffer", timeout: 30000, maxBuffer: 32 * 1048576, windowsHide: true })).stdout } catch { throw Error("Owned operational source archive command failed: " + program) } } + const before = await snapshot(), marker = Number((await app.query("INSERT INTO public.servers(name,host,enabled) VALUES('operational-before','127.0.0.1',false) RETURNING id")).rows[0].id) + await writeFile(file, await runPrivateDatabaseDump(url.toString(), { authorize: async () => {} }), { flag: "wx", mode: 0o600 }) + await app.query("UPDATE public.servers SET name='operational-after' WHERE id=$1", [marker]) + await restorePartitionedDatabase(file, url.toString(), { command, prepareSql: path => writeFile(path, "", { flag: "wx", mode: 0o600 }) }) + assert.equal((await app.query("SELECT name FROM public.servers WHERE id=$1", [marker])).rows[0].name, "operational-before") + assert.deepEqual(await snapshot(), before); assert.equal((await operation(a, "resolve")).canonicalNodeId, first.canonicalNodeId) + cases.push("real restricted-application-role public-only dump/shared restore changes public marker while all eight external control tables and canonical UUID survive; fresh operational resolve succeeds") + } else limitations.push("Public-only restore not requested") + { + const coreLease = await coreSource.connect(scope(10000)), nodeLease = await source.open(scope(10000)) + const before = await snapshot(allTables.filter(name => name !== "bandwidth_source_identity")) + try { + // Explicit external-owner replacement fixture, not a runtime migration or + // automatic enrollment. No guard is disabled to alter ledger data. + await owner.query("DROP TABLE mmapp_control.bandwidth_source_identity") + await owner.query("DROP FUNCTION mmapp_control.bandwidth_source_identity_guard()") + await owner.query(BANDWIDTH_CONTROL_SOURCE_DDL) + await owner.query(`REVOKE ALL ON mmapp_control.bandwidth_source_identity FROM PUBLIC; GRANT SELECT ON mmapp_control.bandwidth_source_identity TO ${ident(runtimeRole)}; REVOKE ALL ON FUNCTION mmapp_control.bandwidth_source_identity_guard() FROM PUBLIC; GRANT EXECUTE ON FUNCTION mmapp_control.bandwidth_source_identity_guard() TO ${ident(runtimeRole)}`) + const renewed = { ...coreRecord, controlGeneration: randomUUID(), catalogSha256: await observeBandwidthControlCatalog(runtime, live), attestationRef: "Owned external valid source renewal fixture; persistent registry bootstrap identity deliberately unchanged" } + await owner.query("INSERT INTO mmapp_control.bandwidth_source_identity(id,installation_id,control_generation,attestation_sha256,event_key_sha256) VALUES(1,$1,$2,$3,$4)", [installationId, renewed.controlGeneration, sha(canonicalBandwidthControlRecord(renewed)), sha(eventKey)]) + await writeFile(attestationFile, JSON.stringify(signedCore(renewed))) + await assert.rejects(coreLease.query("SELECT 1")); await assert.rejects(nodeLease.binding()) + assert.equal((await operation(a, "resolve")).canonicalNodeId, first.canonicalNodeId) + assert.deepEqual(await snapshot(allTables.filter(name => name !== "bandwidth_source_identity")), before) + } finally { coreLease.release(true); nodeLease.close() } + cases.push("valid externally signed core-generation renewal rejects original leased core/node sessions; fresh session resolves SAME persistent canonical UUID with unchanged seven non-identity tables and unchanged issuer/bootstrap") + } + { + const before = await snapshot(allTables.filter(name => !["bandwidth_source_identity", "bandwidth_node_authority"].includes(name))) + await owner.query("UPDATE mmapp_control.bandwidth_node_authority SET status='quarantined' WHERE id=1") + await assert.rejects(source.open(scope(10000))) + await owner.query("UPDATE mmapp_control.bandwidth_source_identity SET status='quarantined' WHERE id=1") + await assert.rejects(coreSource.connect(scope(10000))); await assert.rejects(source.open(scope(10000))) + assert.deepEqual(await snapshot(allTables.filter(name => !["bandwidth_source_identity", "bandwidth_node_authority"].includes(name))), before) + cases.push("terminal registry authority and core source quarantine reject new operational sessions while every node/alias/event/core-occupancy row stays unchanged") + } + limitations.push("Genuine signatures/native localhost observations but synthetic operator/quiescence and actor grants; no production enrollment/physical uniqueness/clone fencing/native dispatch readiness") + limitations.push("No automatic schema/bootstrap/key creation, external-provider rollback guarantee or production source catalog promotion is inferred from this fixture") + console.log("Owned operational bandwidth node source:", JSON.stringify({ cases: cases.length, checks: cases, executionHostAcceptance, nativeLocalhostGets: nativeGets, realChrCalls: 0, normalMigrationCount: MIGRATION_MANIFEST.length, + explicitOwnedExternalInstallation: true, productionReady: false, runtimeDispatchIntegrated: false, limitations })) +} finally { + intercept = undefined; catalogMutation = undefined; Client.prototype.query = originalDriverQuery + extraSources.forEach(source => source.close()); nodeSource?.close(); await core?.close() + reader.close(); server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) + await routerGuardCompatibilityCleanup?.() + for (const pool of pools) await pool.end() + if (databaseCreated) await admin.query(`DROP DATABASE ${ident(databaseName)}`) + for (const role of roles.reverse()) await admin.query(`DROP ROLE ${ident(role)}`) + eventKey.fill(0); issuerKey.fill(0); actorKey.fill(0); await closePool() +} diff --git a/backend/src/services/bandwidth-node-source.ts b/backend/src/services/bandwidth-node-source.ts new file mode 100644 index 00000000..7d38ba7e --- /dev/null +++ b/backend/src/services/bandwidth-node-source.ts @@ -0,0 +1,367 @@ +/** Fixed operational registry source/key assembly. It does not issue actor, + * enrollment or native execution authority, create keys/rows/schema, or certify + * rollback/clone fencing of the provider. Each session uses ONE core lease; + * key/catalog guards never recursively borrow another control connection. */ +import {createHash,createHmac} from "node:crypto" +import {isAbsolute,resolve} from "node:path" +import {performance} from "node:perf_hooks" +import {types} from "node:util" +import {z} from "zod" +import {bandwidthNodeProofShapes as shapes,captureBandwidthNodeProofBatch as captureProofBatch,type BandwidthNodeProofInput} from "./bandwidth-node-proof-input.js" +import {getProductionBandwidthControlSource,readBandwidthControlFile,type createConfiguredBandwidthControlSource,type BandwidthControlDispatchFence,type BandwidthControlRegistryDispatchFence,type BandwidthPublicNodeBootstrapMetadata} from "./bandwidth-control-source.js" +import {canonicalBandwidthNodeValue,bandwidthNodeBootstrapEnvelopeSchema,bandwidthNodeBootstrapSchema,type BandwidthNodeBootstrap} from "./bandwidth-node-bootstrap.js" +import type {ProbeStorageConnection} from "./server-probe-journal-maintenance.js" +import type {BandwidthScope} from "./bandwidth-admission.js" +import {assertResolvedBandwidthNodesOnFence,type BandwidthResolvedNodeFence} from "./bandwidth-node-fence.js" +import {assertResolvedBandwidthPeersOnFence,type BandwidthResolvedPeerFence} from "./bandwidth-peer-fence.js" + +type Core=ReturnType +type Verified=Awaited> +export interface BandwidthNodeSourceConfig {issuerKeyFile:string;issuerKeySha256:string;bootstrapFile:string} +export type {BandwidthNodeProofInput} from "./bandwidth-node-proof-input.js" +/** Public signed metadata ONLY. Does not prove a current issuer/event key, + * installed catalog/ownership, held Q, SQL lease or native execution authority. */ +export interface BandwidthNodePublicBootstrapMetadata extends BandwidthPublicNodeBootstrapMetadata {readonly bootstrapSha256:string} +export interface BandwidthNodePublicRegistryMetadataSnapshot { + readonly format:"mmapp-bandwidth-public-registry-metadata-v1" + readonly metadata:Readonly + readonly backendPid:number +} +/** Public SQL metadata only; exposes neither a query handle nor Q/write proof. */ +export interface BandwidthNodePublicRegistryMetadataLease { + inspect():Promise> + close():void +} +export class BandwidthNodeSourceError extends Error { + readonly code="BANDWIDTH_NODE_SOURCE_UNAVAILABLE" + constructor(){super("Операторская привязка и ключ реестра bandwidth не подтверждены")} +} +const fail=():never=>{throw new BandwidthNodeSourceError()} +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid() +const digest=(value:Buffer|string)=>createHash("sha256").update(value).digest("hex") +const root=(value:BandwidthNodeBootstrap)=>({installationId:value.installationId,issuerKeySha256:value.issuerKeySha256,quiescenceProof:value.quiescenceProof}) +const publicMetadataSchema=z.object({format:z.literal("mmapp-bandwidth-public-node-metadata-v1"),bootstrap:bandwidthNodeBootstrapSchema, + source:z.object({installationId:uuid,controlGeneration:uuid,sourceRecordSha256:hash}).strict(), + publicDatabase:z.object({databaseName:z.string().min(1).max(63),runtimeRole:z.string().min(1).max(63),ownerRole:z.string().min(1).max(63), + databaseOid:z.number().int().min(1).max(4294967295),catalogSha256:hash}).strict()}).strict() +function capturePublicData(input:unknown):unknown{ + let nodes=0,bytes=0 + const copy=(value:unknown,depth=0):unknown=>{ + if(++nodes>256||depth>8)fail() + if(value===null||typeof value==="boolean"||typeof value==="number"&&Number.isFinite(value))return value + if(typeof value==="string"){bytes+=Buffer.byteLength(value);if(bytes>65536)fail();return value} + if(!value||typeof value!=="object"||types.isProxy(value)||Object.getPrototypeOf(value)!==Object.prototype)return fail() + const result:Record={} + for(const key of Reflect.ownKeys(value)){ + if(typeof key!=="string"||key==="__proto__")return fail() + const field=Object.getOwnPropertyDescriptor(value,key) + if(!field||!("value"in field)||!field.enumerable)return fail() + bytes+=Buffer.byteLength(key);if(bytes>65536)fail() + Object.defineProperty(result,key,{value:copy(field.value,depth+1),enumerable:true}) + } + return Object.freeze(result) + } + return copy(input) +} + +/** Server-owned construction only, never request JSON. The production getter + * below fixes the genuine core source and operator paths. Construction is inert. + * dispatchGuard must check the original actor/source grant without re-entering + * this control lease; the core invokes it AFTER its own async metadata guards, + * immediately before actual driver SQL and before acknowledging the result. */ +export function createConfiguredBandwidthNodeSource(coreSource:Core,raw:BandwidthNodeSourceConfig){ + const config=Object.freeze({...raw}) + if(!hash.safeParse(config.issuerKeySha256).success||[config.issuerKeyFile,config.bootstrapFile].some(path=>typeof path!=="string"||!isAbsolute(path)||resolve(path)!==path) + ||config.issuerKeyFile===config.bootstrapFile)fail() + const baseConnect=coreSource.connect.bind(coreSource) + const registryConnect=(coreSource as Partial>).connectRegistry?.bind(coreSource) + const inspectPublic=(coreSource as Partial>).inspectPublicNodeBootstrap?.bind(coreSource) + const openPublicRegistry=(coreSource as Partial>).openPublicRegistryMetadata?.bind(coreSource) + const core=Object.freeze({connect:(scope:BandwidthScope,guard:(fence:BandwidthControlDispatchFence)=>Promise)=> + registryConnect?registryConnect(scope,guard):baseConnect(scope,guard)}) + let closed=false + const sessions=new Set<()=>void>() + const publicOperations=new Set<()=>void>() + async function inspectPublicBootstrap(input:BandwidthScope):Promise>{ + // Capture the caller's FIRST immutable horizon before any file/core await. + const wall=Date.now(),mono=performance.now() + if(closed||!input||types.isProxy(input)||Object.getPrototypeOf(input)!==Object.prototype)fail() + const descriptors=Object.getOwnPropertyDescriptors(input) + if(Reflect.ownKeys(input).some(key=>typeof key!=="string"||!["deadlineAt","signal"].includes(key)) + ||Object.values(descriptors).some(field=>!("value"in field)||!field.enumerable))fail() + const requestedDeadline:unknown=descriptors.deadlineAt?.value,signal:unknown=descriptors.signal?.value + if(typeof requestedDeadline!=="number"||!Number.isSafeInteger(requestedDeadline)||signal!==undefined&&(types.isProxy(signal)||!(signal instanceof AbortSignal)))return fail() + const deadlineAt=Math.min(requestedDeadline,wall+10000),end=mono+Math.max(0,deadlineAt-wall),originalSignal=signal as AbortSignal|undefined + // Native operations ignore hostile AbortSignal subclass accessors/methods. + const aborted=Object.getOwnPropertyDescriptor(AbortSignal.prototype,"aborted")!.get! + const isAborted=()=>originalSignal?Reflect.apply(aborted,originalSignal,[]):false + try{isAborted()}catch{return fail()} + const controller=new AbortController(),scope=Object.freeze({deadlineAt,signal:controller.signal}) + let active=true,rejectStopped!:(error:BandwidthNodeSourceError)=>void + const live=()=>{const now=Date.now();if(closed||!active||isAborted()||controller.signal.aborted||now=deadlineAt||performance.now()>=end)fail()} + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject}) + const stop=()=>{if(!active)return;active=false;controller.abort();rejectStopped(new BandwidthNodeSourceError())} + const timer=setTimeout(stop,Math.max(1,Math.min(2147483647,end-performance.now()))) + publicOperations.add(stop);if(originalSignal)Reflect.apply(EventTarget.prototype.addEventListener,originalSignal,["abort",stop,{once:true}]) + const readPublic=async()=>{ + live();const bytes=await readBandwidthControlFile(config.bootstrapFile,32768,live);live() + const envelope=bandwidthNodeBootstrapEnvelopeSchema.parse(JSON.parse(new TextDecoder("utf-8",{fatal:true}).decode(bytes))) + if(!inspectPublic)return fail() + const value=publicMetadataSchema.parse(await inspectPublic(envelope,scope));live() + if(value.bootstrap.issuerKeySha256!==config.issuerKeySha256||value.source.installationId!==value.bootstrap.installationId)fail() + Object.freeze(value.bootstrap);Object.freeze(value.source);Object.freeze(value.publicDatabase) + return Object.freeze({...value,bootstrapSha256:digest(canonicalBandwidthNodeValue(value.bootstrap))}) + } + try{ + live() + return await Promise.race([Promise.resolve().then(async()=>{ + const before=await readPublic(),after=await readPublic();live() + if(canonicalBandwidthNodeValue(before)!==canonicalBandwidthNodeValue(after))fail() + return after + }),stopped]) + }catch{throw new BandwidthNodeSourceError()} + finally{active=false;clearTimeout(timer);if(originalSignal)Reflect.apply(EventTarget.prototype.removeEventListener,originalSignal,["abort",stop]);publicOperations.delete(stop);controller.abort()} + } + async function openPublicRegistryMetadata(input:BandwidthScope):Promise>{ + const wall=Date.now(),mono=performance.now() + if(closed||!input||types.isProxy(input)||Object.getPrototypeOf(input)!==Object.prototype)return fail() + const fields=Object.getOwnPropertyDescriptors(input) + if(Reflect.ownKeys(input).some(key=>typeof key!=="string"||!["deadlineAt","signal"].includes(key)) + ||Object.values(fields).some(field=>!("value"in field)||!field.enumerable))return fail() + const requested:unknown=fields.deadlineAt?.value,signal:unknown=fields.signal?.value + if(typeof requested!=="number"||!Number.isSafeInteger(requested)||signal!==undefined&&(types.isProxy(signal)||!(signal instanceof AbortSignal)))return fail() + const deadlineAt=Math.min(requested,wall+10000),end=mono+Math.max(0,deadlineAt-wall),originalSignal=signal as AbortSignal|undefined + const aborted=Object.getOwnPropertyDescriptor(AbortSignal.prototype,"aborted")!.get! + const isAborted=()=>originalSignal?Reflect.apply(aborted,originalSignal,[]):false + try{isAborted()}catch{return fail()} + const controller=new AbortController(),scope=Object.freeze({deadlineAt,signal:controller.signal}) + type PublicCoreLease={inspect(envelope:unknown):Promise;close():void} + let active=true,busy=true,owned:PublicCoreLease|undefined,physicallyClosed=false,pinned:Readonly|undefined + let originalFile:string|undefined,rejectStopped!:(error:BandwidthNodeSourceError)=>void + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject});void stopped.catch(()=>{}) + const closeOwned=()=>{ + if(!owned||physicallyClosed)return + for(let attempt=0;attempt<2;attempt++)try{owned.close();physicallyClosed=true;return}catch{/* Never return this lease healthy. */} + } + const live=()=>{const now=Date.now();if(closed||!active||isAborted()||controller.signal.aborted||now=deadlineAt||performance.now()>=end)fail()} + const stop=()=>{ + if(!active)return + active=false;clearTimeout(timer);controller.abort();closeOwned() + if(originalSignal)Reflect.apply(EventTarget.prototype.removeEventListener,originalSignal,["abort",stop]) + publicOperations.delete(stop);rejectStopped(new BandwidthNodeSourceError()) + } + const timer=setTimeout(stop,Math.max(1,Math.min(2147483647,end-performance.now()))) + publicOperations.add(stop);if(originalSignal)Reflect.apply(EventTarget.prototype.addEventListener,originalSignal,["abort",stop,{once:true}]) + const readEnvelope=async()=>{ + live();const bytes=await readBandwidthControlFile(config.bootstrapFile,32768,live);live() + const file=digest(bytes) + if(originalFile!==undefined&&file!==originalFile)fail() + const envelope=bandwidthNodeBootstrapEnvelopeSchema.parse(capturePublicData(JSON.parse(new TextDecoder("utf-8",{fatal:true}).decode(bytes)))) + const payload=Buffer.from(envelope.payload,"base64url"),text=new TextDecoder("utf-8",{fatal:true}).decode(payload) + const bootstrap=bandwidthNodeBootstrapSchema.parse(capturePublicData(JSON.parse(text))) + if(payload.toString("base64url")!==envelope.payload||canonicalBandwidthNodeValue(bootstrap)!==text||bootstrap.issuerKeySha256!==config.issuerKeySha256)fail() + return {file,envelope} + } + const captureLease=(raw:unknown):PublicCoreLease=>{ + if(!raw||typeof raw!=="object"||types.isProxy(raw)||Object.getPrototypeOf(raw)!==Object.prototype)return fail() + const descriptors=Object.getOwnPropertyDescriptors(raw),inspect=descriptors.inspect,close=descriptors.close + const cleanup=()=>{if(close&&"value"in close&&typeof close.value==="function")try{Reflect.apply(close.value,raw,[])}catch{/* Malformed producer lease is refused. */}} + if(!Object.isFrozen(raw)||Reflect.ownKeys(raw).length!==2||!inspect||!("value"in inspect)||typeof inspect.value!=="function"||!inspect.enumerable + ||!close||!("value"in close)||typeof close.value!=="function"||!close.enumerable){cleanup();return fail()} + return Object.freeze({inspect:(envelope:unknown)=>Reflect.apply(inspect.value,raw,[envelope]) as Promise,close:()=>{Reflect.apply(close.value,raw,[])}}) + } + const captureResult=(raw:unknown,envelope:ReturnType):Readonly=>{ + if(!raw||typeof raw!=="object"||types.isProxy(raw)||!Object.isFrozen(raw))return fail() + const value=z.object({format:z.literal("mmapp-bandwidth-public-registry-metadata-v1"),metadata:publicMetadataSchema,backendPid:z.number().int().positive().max(2147483647)}).strict().parse(capturePublicData(raw)) + if(value.metadata.bootstrap.issuerKeySha256!==config.issuerKeySha256||value.metadata.source.installationId!==value.metadata.bootstrap.installationId)fail() + const payload=Buffer.from(envelope.payload,"base64url") + if(payload.toString("base64url")!==envelope.payload||new TextDecoder("utf-8",{fatal:true}).decode(payload)!==canonicalBandwidthNodeValue(value.metadata.bootstrap))fail() + Object.freeze(value.metadata.bootstrap);Object.freeze(value.metadata.source);Object.freeze(value.metadata.publicDatabase) + const metadata=Object.freeze({...value.metadata,bootstrapSha256:digest(canonicalBandwidthNodeValue(value.metadata.bootstrap))}) + return Object.freeze({format:value.format,metadata,backendPid:value.backendPid}) + } + const inspectSpan=async()=>{ + const before=await readEnvelope() + const first=await owned!.inspect(before.envelope);live() + const captured=captureResult(first,before.envelope),after=await readEnvelope() + if(canonicalBandwidthNodeValue(before.envelope)!==canonicalBandwidthNodeValue(after.envelope))fail() + const second=await owned!.inspect(after.envelope);live() + const result=captureResult(second,after.envelope);await readEnvelope();live() + if(canonicalBandwidthNodeValue(captured)!==canonicalBandwidthNodeValue(result) + ||pinned&&canonicalBandwidthNodeValue(pinned)!==canonicalBandwidthNodeValue(result))fail() + pinned??=result;return result + } + try{ + live() + return await Promise.race([Promise.resolve().then(async()=>{ + const initial=await readEnvelope();originalFile=initial.file + if(!openPublicRegistry)return fail() + // Capture/close even a producer that ignores abort and returns late. + owned=captureLease(await openPublicRegistry(initial.envelope,scope)) + if(!active){closeOwned();return fail()} + live();await inspectSpan();live();busy=false + return Object.freeze({ + async inspect(){ + try{ + live();if(busy){stop();return fail()}busy=true + return await Promise.race([Promise.resolve().then(inspectSpan),stopped]) + }catch{stop();throw new BandwidthNodeSourceError()} + finally{busy=false} + },close:stop, + }) + }),stopped]) + }catch{stop();throw new BandwidthNodeSourceError()} + } + async function open(input:BandwidthScope,dispatchGuard?:()=>Promise,ownershipGuard?:(fence:BandwidthControlDispatchFence)=>Promise){ + if(!input||!Number.isSafeInteger(input.deadlineAt)||input.signal!==undefined&&!(input.signal instanceof AbortSignal) + ||dispatchGuard!==undefined&&typeof dispatchGuard!=="function"||ownershipGuard!==undefined&&typeof ownershipGuard!=="function")fail() + const began=Date.now(),end=performance.now()+Math.max(0,Math.min(10000,input.deadlineAt-began)) + const scope=Object.freeze({deadlineAt:Math.min(input.deadlineAt,began+10000),signal:input.signal}) + type Captured={verified:Verified;key:Buffer;bootstrapHash:string} + let connection:ProbeStorageConnection|undefined,active=true,released=false,busy=false,pinned:Verified|undefined,pinnedBootstrap:string|undefined,captureRequested=false,captured:Captured|undefined + const keys=new Set() + const wipe=(key:Buffer)=>{key.fill(0);keys.delete(key)} + const live=()=>{if(closed||!active||connection?.lifetimeSignal?.aborted||scope.signal?.aborted||Date.now()>=scope.deadlineAt||performance.now()>=end)fail()} + const close=()=>{active=false;for(const key of keys)key.fill(0);keys.clear();captured=undefined;if(connection&&!released){released=true;try{connection.release(true)}catch{/* The lease is never reused healthy. */}}} + const clearCapture=()=>{if(captured)wipe(captured.key);captured=undefined} + let stop!:()=>void + const stopped=new Promise((_resolve,reject)=>{stop=()=>{dispose();reject(new BandwidthNodeSourceError())}}) + const timer=setTimeout(stop,Math.max(1,Math.min(scope.deadlineAt-Date.now(),end-performance.now()))) + const dispose=()=>{clearTimeout(timer);scope.signal?.removeEventListener("abort",stop);sessions.delete(stop);close()} + sessions.add(stop);scope.signal?.addEventListener("abort",stop,{once:true}) + async function files(fence:BandwidthControlDispatchFence):Promise{ + live() + const bytes=await readBandwidthControlFile(config.bootstrapFile,32768,live) + const verified=await fence.verifyNodeBootstrap(JSON.parse(new TextDecoder("utf-8",{fatal:true}).decode(bytes)));live() + const bootstrapHash=digest(canonicalBandwidthNodeValue(verified.bootstrap)) + if(pinnedBootstrap!==undefined&&(bootstrapHash!==pinnedBootstrap||verified.source.sourceRecordSha256!==pinned!.source.sourceRecordSha256))fail() + if(verified.bootstrap.issuerKeySha256!==config.issuerKeySha256)fail() + let key:Buffer|undefined + try{ + key=await readBandwidthControlFile(config.issuerKeyFile,32,live) + keys.add(key);live() + if(key.length!==32||digest(key)!==config.issuerKeySha256)fail() + live() + return {verified,key,bootstrapHash} + }catch(error){if(key)wipe(key);throw error} + } + const dispatch=async(fence:BandwidthControlDispatchFence)=>{ + let before:Captured|undefined,after:Captured|undefined + try{ + before=await files(fence) + const sql={query:async(text:string,values?:unknown[])=>{live();const result=await fence.query(text,values);live();return result}} + const verified=before.verified + const v2=verified.bootstrap.format==="mmapp-bandwidth-node-bootstrap-v2" + const expected=root(verified.bootstrap) + const role={databaseName:verified.source.databaseName,runtimeRole:verified.source.runtimeRole,ownerRole:verified.source.ownerRole} + const joint=fence as BandwidthControlDispatchFence & Partial + if(typeof joint.assertRegistryIdentity==="function"){ + await joint.assertRegistryIdentity(verified);live() + }else if(v2){ + const {assertBandwidthPeerIdentity}=await import("./bandwidth-peer-identity.js") + live();await assertBandwidthPeerIdentity(sql,{...role,...expected,registryCatalogSha256:verified.bootstrap.registryCatalogSha256},live) + }else{ + const {assertBandwidthNodeIdentity}=await import("./bandwidth-node-identity.js") + live();await assertBandwidthNodeIdentity(sql,{...role,...expected,registryCatalogSha256:verified.bootstrap.registryCatalogSha256},live) + } + // Fixed ownership checks use this SAME raw driver fence, never a + // recursive source query or another lease. Files are re-read AFTER it. + await ownershipGuard?.(fence);live() + // Re-read after ALL asynchronous registry metadata. A key/bootstrap + // changed during those queries cannot produce one last stale HMAC. + after=await files(fence) + if(after.bootstrapHash!==before.bootstrapHash||after.verified.source.sourceRecordSha256!==before.verified.source.sourceRecordSha256)fail() + await dispatchGuard?.();live() + pinned??=after.verified;pinnedBootstrap??=after.bootstrapHash + if(captureRequested){clearCapture();captured=after;after=undefined} + }finally{if(before)wipe(before.key);if(after)wipe(after.key)} + } + const fresh=async(work:(value:Captured)=>T)=>{ + captureRequested=true;clearCapture() + try{ + live();await connection!.query("SELECT 1",undefined,{signal:scope.signal,timeoutMs:Math.max(1,Math.min(5000,scope.deadlineAt-Date.now()))});live() + const value=captured as Captured|undefined;if(!value)fail() + captured=undefined + try{const result=work(value!);live();return result}finally{wipe(value!.key)} + }finally{captureRequested=false;clearCapture()} + } + const owned=async(work:()=>Promise):Promise=>{ + live();if(busy){stop();fail()};busy=true + try{return await Promise.race([Promise.resolve().then(async()=>{live();return work()}),stopped])} + catch{dispose();throw new BandwidthNodeSourceError()} + finally{busy=false} + } + try{ + return await Promise.race([Promise.resolve().then(async()=>{ + live();await dispatchGuard?.();live();connection=await core.connect(scope,dispatch) + if(!active||closed){close();fail()};live();await fresh(()=>undefined) + const lifetimeSignal=connection.lifetimeSignal + const facade:ProbeStorageConnection=Object.freeze({ + ...(lifetimeSignal?{lifetimeSignal}:{}), + query:(text:string,values?:unknown[],options?:{signal?:AbortSignal;timeoutMs:number})=>owned(async()=>{ + live();const result=await connection!.query(text,values,options);live();return result + }), + release:()=>stop(), + }) + return Object.freeze({connection:facade, + binding:()=>owned(()=>fresh(value=>Object.freeze({...root(value.verified.bootstrap),bootstrapSha256:value.bootstrapHash, + controlGeneration:value.verified.source.controlGeneration,sourceRecordSha256:value.verified.source.sourceRecordSha256}))), + keyedProof:(purpose:string,value:unknown)=>{ + if(!Object.hasOwn(shapes,purpose))return Promise.reject(new BandwidthNodeSourceError()) + let text:string + try{text=canonicalBandwidthNodeValue(shapes[purpose as keyof typeof shapes].parse(value));if(Buffer.byteLength(text)>65536)fail()}catch{return Promise.reject(new BandwidthNodeSourceError())} + return owned(()=>fresh(material=>{ + if(purpose.startsWith("external-")&&material.verified.bootstrap.format!=="mmapp-bandwidth-node-bootstrap-v2")fail() + if(purpose!=="claim"){ + const record=JSON.parse(text),expected=root(material.verified.bootstrap) + if(record.installationId!==expected.installationId||record.issuerKeySha256!==expected.issuerKeySha256||record.quiescenceProof!==expected.quiescenceProof)fail() + } + return createHmac("sha256",material.key).update("mmapp-bandwidth-node-"+purpose+"-v1:"+text).digest("hex") + })) + }, + keyedProofs:(inputs:readonly BandwidthNodeProofInput[])=>{ + let captured:ReturnType + try{captured=captureProofBatch(inputs)}catch{return Promise.reject(new BandwidthNodeSourceError())} + return owned(()=>fresh(material=>{ + const expected=root(material.verified.bootstrap) + // All MACs are pure synchronous operations on ONE freshly checked + // key. No await, user callback or authority cache between them. + const values=captured.map(({purpose,text})=>{ + if(purpose.startsWith("external-")&&material.verified.bootstrap.format!=="mmapp-bandwidth-node-bootstrap-v2")fail() + if(purpose!=="claim"){ + const record=JSON.parse(text) + if(record.installationId!==expected.installationId||record.issuerKeySha256!==expected.issuerKeySha256||record.quiescenceProof!==expected.quiescenceProof)fail() + } + return createHmac("sha256",material.key).update("mmapp-bandwidth-node-"+purpose+"-v1:"+text).digest("hex") + }) + return Object.freeze(values) + })) + }, + close:()=>stop(), + }) + }),stopped]) + }catch{dispose();throw new BandwidthNodeSourceError()} + } + return Object.freeze({open,inspectPublicBootstrap,openPublicRegistryMetadata, + /** Operator-window composition only: capture an additional fixed raw + * ownership observer inside every existing before/after source fence. */ + openWithOwnershipFence:(input:BandwidthScope,dispatchGuard:()=>Promise,ownershipGuard:(fence:BandwidthControlDispatchFence)=>Promise)=>{ + if(typeof ownershipGuard!=="function")return Promise.reject(new BandwidthNodeSourceError()) + return open(input,dispatchGuard,ownershipGuard) + }, + assertResolvedOnFence:(fence:BandwidthControlDispatchFence,expected:BandwidthResolvedNodeFence,scope:BandwidthScope)=>assertResolvedBandwidthNodesOnFence(config,sessions,()=>closed,fence,expected,scope), + assertResolvedPeersOnFence:(fence:BandwidthControlDispatchFence,expected:BandwidthResolvedPeerFence,scope:BandwidthScope)=>assertResolvedBandwidthPeersOnFence(config,sessions,()=>closed,fence,expected,scope), + close(){closed=true;for(const stop of [...publicOperations])stop();for(const stop of [...sessions])stop()}}) +} + +let production:ReturnType|undefined +/** No fallback key/path, no startup I/O and no automatic marker. Missing + * operator configuration affects the requesting operation, not tool discovery. */ +export function getProductionBandwidthNodeSource(){ + if(production)return production + const required=(name:string)=>{const value=process.env[name];if(!value?.trim())throw new BandwidthNodeSourceError();return value} + const config={issuerKeyFile:required("MMAPP_BANDWIDTH_NODE_ISSUER_KEY_FILE"),issuerKeySha256:required("MMAPP_BANDWIDTH_NODE_ISSUER_KEY_SHA256"),bootstrapFile:required("MMAPP_BANDWIDTH_NODE_BOOTSTRAP_FILE")} + production=createConfiguredBandwidthNodeSource(getProductionBandwidthControlSource(),config) + return production +} +export function closeProductionBandwidthNodeSource(){production?.close()} diff --git a/backend/src/services/bandwidth-operator-docker-stop.sql-helper.ts b/backend/src/services/bandwidth-operator-docker-stop.sql-helper.ts new file mode 100644 index 00000000..65e362bf --- /dev/null +++ b/backend/src/services/bandwidth-operator-docker-stop.sql-helper.ts @@ -0,0 +1,184 @@ +/** Owned local PG acceptance: actual configured composition/source/registry, + * signed original grant, genuine localhost license HTTP; Docker HTTP is MOCKED. + * No daemon/backend actions, no result/ACK replacement, original5s horizon. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { EventEmitter } from "node:events" +import { lstat, mkdtemp, realpath, rm, writeFile } from "node:fs/promises" +import http from "node:http" +import { basename, dirname, resolve } from "node:path" +import { PassThrough } from "node:stream" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import { bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { createDockerBandwidthOperatorStopAdapter } from "./bandwidth-operator-docker-stop.js" +import { createBandwidthOperatorWriterComposition } from "./bandwidth-operator-writer-composition.js" +import type { createOwnedExternalRegistryHarness } from "./bandwidth-external-node-registry.sql-helper.js" + +const LOCK_SQL="SELECT pg_catalog.pg_try_advisory_lock(8723116) AS acquired" +const PID_SQL="SELECT pg_catalog.pg_backend_pid() AS backend_pid" +const HELD_SQL="SELECT pg_catalog.pg_backend_pid() AS backend_pid, EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND pid=pg_catalog.pg_backend_pid() AND classid=0::pg_catalog.oid AND objid=8723116::pg_catalog.oid AND objsubid=1 AND mode='ExclusiveLock' AND granted) AS held" +const STAND_LATENCY=Object.freeze({info:25.323,inventory:197.916,inspect:1.827}) +/** Fixed measured stand profile, owned fixture only; never request/model data. */ +export function operatorDockerLatencyMode(){ + const mode=process.env.MMAPP_TEST_OPERATOR_DOCKER_LATENCY + assert(mode===undefined||mode==="stand-20261028") + if(mode!==undefined){assertOwnedBandwidthSql();assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY,"true");assert.equal(process.env.MMAPP_OWN_OPERATOR_DOCKER_SQL,"true")} + return mode +} +function mockDockerDelivery(){ + const pending=new Set>() + let closed=false,canceled=0,delivered=0 + return { + schedule(delayMs:number,work:()=>void){ + if(closed)return ()=>{} + let stopped=false,timer:ReturnType|undefined + const deliver=()=>{if(timer)pending.delete(timer);if(!closed&&!stopped){delivered++;work()}} + // Node truncates fractional timer delays; round UP to avoid modeling + // measured 1.827ms inspect calls as only 1ms. + if(delayMs>0){timer=setTimeout(deliver,Math.ceil(delayMs));pending.add(timer)}else queueMicrotask(deliver) + return ()=>{if(stopped)return;stopped=true;if(timer&&pending.delete(timer)){clearTimeout(timer);canceled++}} + }, + close(){closed=true;for(const timer of pending){clearTimeout(timer);canceled++}pending.clear()}, + metrics(){return {pendingTimers:pending.size,canceledTimers:canceled,deliveredCallbacks:delivered}}, + } +} +export async function assertOperatorDockerLatencyDiagnostics(){ + const delivery=mockDockerDelivery();let callbacks=0 + delivery.schedule(8,()=>{callbacks++})() + delivery.schedule(8,()=>{callbacks++});delivery.close() + delivery.schedule(0,()=>{callbacks++}) + delivery.schedule(10000,()=>{callbacks++}) + await new Promise(resolve=>setTimeout(resolve,16)) + assert.equal(callbacks,0);assert.deepEqual(delivery.metrics(),{pendingTimers:0,canceledTimers:2,deliveredCallbacks:0}) + const scanMs=2*STAND_LATENCY.info+2*STAND_LATENCY.inventory+10*STAND_LATENCY.inspect + assert.equal(scanMs,464.748) + return {fixedProfile:"stand-20261028",scanGets:14,scanMs,referenceGets:4312,referenceScans:4312/14,referenceSequentialLatencyMs:scanMs*(4312/14),canceledLateCallbacks:2} +} +/** Diagnostic labels ONLY; never SQL admission, completeness or ACK evidence. */ +export function operatorDockerSqlTag(text:string) { + if(text===LOCK_SQL)return "WINDOW_LOCK" + if(text===PID_SQL)return "WINDOW_PID" + if(text===HELD_SQL)return "WINDOW_HELD" + if(text==="BEGIN")return "BEGIN" + if(text==="COMMIT")return "COMMIT" + if(text==="ROLLBACK")return "ROLLBACK" + if(text==="SELECT 1")return "SOURCE_FRESH" + return "OTHER" +} +export function operatorDockerSafeCode(error:unknown) { + const descriptor=error&&typeof error==="object"?Object.getOwnPropertyDescriptor(error,"code"):undefined + const code=descriptor&&"value"in descriptor?descriptor.value:undefined + if(code==="BANDWIDTH_CONTROL_SOURCE_UNAVAILABLE")return "CONTROL_SOURCE_UNAVAILABLE" + if(code==="BANDWIDTH_NODE_SOURCE_UNAVAILABLE")return "NODE_SOURCE_UNAVAILABLE" + if(code==="BANDWIDTH_OPERATOR_WRITER_FENCE_REFUSED")return "WRITER_FENCE_REFUSED" + if(code==="BANDWIDTH_OPERATOR_WRITER_FENCE_UNKNOWN")return "WRITER_FENCE_UNKNOWN" + if(code==="BANDWIDTH_OPERATOR_ENROLLMENT_REFUSED")return "ORIGINAL_OR_OBSERVATION_REFUSED" + if(code==="ERR_ASSERTION")return "ASSERTION" + if(["42501","42P01","42703","23514","23505","23503","40001","40P01","55P03","25P02","57014","57P01","08006","53200","53300","XX000"].includes(code))return "PG_"+code + return "UNKNOWN" +} +export function assertOperatorDockerSqlDiagnostics(){ + for(const [sql,tag]of [[LOCK_SQL,"WINDOW_LOCK"],[PID_SQL,"WINDOW_PID"],[HELD_SQL,"WINDOW_HELD"],["BEGIN","BEGIN"],["COMMIT","COMMIT"],["ROLLBACK","ROLLBACK"],["SELECT 1","SOURCE_FRESH"],["SELECT 'SECRET'","OTHER"]])assert.equal(operatorDockerSqlTag(sql),tag) + assert.equal(operatorDockerSafeCode({code:"BANDWIDTH_CONTROL_SOURCE_UNAVAILABLE",message:"SECRET"}),"CONTROL_SOURCE_UNAVAILABLE") + assert.equal(operatorDockerSafeCode({code:"BANDWIDTH_NODE_SOURCE_UNAVAILABLE",stack:"SECRET"}),"NODE_SOURCE_UNAVAILABLE") + let called=0;const error=Object.defineProperty({},"code",{get(){called++;return"SECRET"}}) + assert.equal(operatorDockerSafeCode(error),"UNKNOWN");assert.equal(called,0) + assert.equal(operatorDockerSafeCode({code:"SECRET"}),"UNKNOWN") + return {sqlTags:8,knownUnavailableCodes:2,getterEvaluations:called,realDatabaseCalls:0} +} +type Harness=Awaited> +type Signed=Awaited> +const sha=(data:string|Buffer)=>createHash("sha256").update(data).digest("hex") + +export async function acceptOwnedDockerOperatorSql(f:Harness,signed:Signed) { + assertOwnedBandwidthSql();assert.equal(process.env.MMAPP_OWN_OPERATOR_DOCKER_SQL,"true") + const latencyMode=operatorDockerLatencyMode(),delivery=mockDockerDelivery(),responses=new Set() + const dockerKinds={info:0,inventory:0,inspect:0} + const state=resolve(process.env.MCP_STATE_DIR!),folder=await mkdtemp(resolve(state,"operator-docker-sql-")) + assert.equal(dirname(folder),state) + const keys=generateKeyPairSync("ed25519"),trustKeyFile=resolve(folder,"operator.pub"),grantFile=resolve(folder,"operator.grant.json") + const peer={kind:"external" as const,enrollmentId:f.externalEndpoint({"system-id":"OWNED-DOCKER-SQL-SYSTEM","software-id":"OWNED-DOCKER-SQL-SOFTWARE"})} + const endpoint=await f.readPeerSnapshot(peer,f.scope());assert(endpoint) + const ids=["1".repeat(64),"2".repeat(64),"3".repeat(64),"4".repeat(64),"5".repeat(64)] + const policy={format:"mmapp-bandwidth-docker-only-quiescence-v1",installationId:f.root.installationId,daemonId:"owned-docker-sql-mock",socketPath:"/var/run/docker.sock", + automaticExecutorScope:"complete-docker-only-inventory",outsideAutomaticControllers:"none-operator-asserted",administratorBoundary:"trusted-no-manual-start-create-or-policy-change-during-window", + socketAliases:["/var/run/docker.sock","/run/docker.sock"],containers:ids.map((containerId,index)=>({containerId,imageId:"sha256:"+String(index+1).repeat(64),project:index===4?"owned-sql-pg":"owned-app",service:index===0?"backend":"unrelated-"+index,role:index===0?"backend":"unrelated"})),sourceArtifacts:[{name:"owned-local-sql-fixture",sha256:sha("synthetic Docker HTTP only; not actual backend acceptance")}]} + const now=Date.now(),grant=bandwidthOperatorEnrollmentGrantSchema.parse({format:"mmapp-bandwidth-operator-enrollment-v1",...f.root,grantId:randomUUID(),operatorId:"owned-docker-sql-original", + executorInventorySha256:sha(canonical(policy)),executorsStoppedAt:now-1000,issuedAt:now,expiresAt:now+120000,peerRef:peer,sourceProof:endpoint.sourceProof,authorityGeneration:endpoint.authorityGeneration,routingEndpointEpoch:endpoint.endpoint.routingEndpointEpoch, + claims:[{kind:"system-id",value:"OWNED-DOCKER-SQL-SYSTEM"},{kind:"software-id",value:"OWNED-DOCKER-SQL-SOFTWARE"}]}) + const payload=Buffer.from(canonical(grant)),publicSha=sha(keys.publicKey.export({type:"spki",format:"der"})),inventory=canonical(policy) + await writeFile(trustKeyFile,keys.publicKey.export({type:"spki",format:"pem"}),{flag:"wx",mode:0o600}) + await writeFile(grantFile,JSON.stringify({format:"mmapp-bandwidth-operator-enrollment-envelope-v1",payload:payload.toString("base64url"),signature:sign(null,payload,keys.privateKey).toString("base64url")}),{flag:"wx",mode:0o600}) + const {Client}=await import("pg"),driverQuery=Client.prototype.query,httpRequest=http.request + const pids=new Set(),commands:string[]=[],physicalTags:Record={} + let firstFailure:{command:string;code:string;elapsedMs:number;remainingMs:number}|undefined,firstPhysicalFailure:{command:string;code:string}|undefined + let began=0,deadline=0,dockerGets=0,sourceOpens=0,physicalDestroys=0,credentials=0,commits=0,heldChecks=0,physicalPid:number|undefined + const inspections=new Map(ids.map((Id,index)=>[Id,{Id,Image:policy.containers[index].imageId,Config:{Labels:{"com.docker.compose.project":policy.containers[index].project,"com.docker.compose.service":policy.containers[index].service},Env:[]},HostConfig:{Privileged:false,PidMode:"",RestartPolicy:{Name:index===0?"no":"unless-stopped",MaximumRetryCount:0}},State:{Running:index!==0,Restarting:false,Paused:false,Dead:false,Status:index===0?"exited":"running",Pid:index===0?0:123,FinishedAt:new Date(now-2000).toISOString()},Mounts:index===4?[{Type:"tmpfs",Source:"",Destination:"/var/lib/postgresql/data"}]:[]}])) + http.request=function(...args:unknown[]){ + const options=args[0] as http.RequestOptions + if(!options||typeof options!=="object"||options.socketPath!=="/var/run/docker.sock")return Reflect.apply(httpRequest,http,args) + assert.equal(options.method,"GET");const path=String(options.path);assert(/^\/v1\.45\/(info|containers\/json\?all=1&size=0|containers\/[a-f0-9]{64}\/json)$/.test(path));dockerGets++ + const callback=args[1] as (response:unknown)=>void + const kind=path==="/v1.45/info"?"info":path.includes("?all=")?"inventory":"inspect";dockerKinds[kind]++ + let cancel=()=>{},response:PassThrough|undefined,destroyed=false + return Object.assign(new EventEmitter(),{destroy(){destroyed=true;cancel();if(response){responses.delete(response);response.destroy()}},end(){cancel=delivery.schedule(latencyMode?STAND_LATENCY[kind]:0,()=>{ + if(destroyed)return + response=Object.assign(new PassThrough(),{statusCode:200,headers:{"content-type":"application/json"},complete:true});responses.add(response);response.once("close",()=>{if(response)responses.delete(response)});callback(response) + response.end(JSON.stringify(path==="/v1.45/info"?{ID:policy.daemonId,Swarm:{LocalNodeState:"inactive"}}:path.includes("?all=")?ids.map(Id=>({Id})):inspections.get(path.split("/")[3]))) + })}}) + } as typeof http.request + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + const physical=this as unknown as {connectionParameters:{user:string;database:string};processID:number} + const text=typeof args[0]==="string"?args[0]:(args[0] as {text?:string})?.text??"" + const own=physical.connectionParameters.user===signed.runtimeRole&&physical.connectionParameters.database===f.databaseName + if(own){pids.add(physical.processID);const tag=operatorDockerSqlTag(text);physicalTags[tag]=(physicalTags[tag]??0)+1} + const result=Reflect.apply(driverQuery,this,args) as unknown + if(own&&result&&typeof(result as Promise).then==="function")void(result as Promise).then(undefined,error=>{firstPhysicalFailure??={command:operatorDockerSqlTag(text),code:operatorDockerSafeCode(error)}}) + return result // EXACT actual driver value/promise/ACK, no replacement + } as typeof Client.prototype.query + const rival=await f.owner.connect(),before=await f.digest(["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events"]) + async function held(){ + const rows=(await rival.query("SELECT pid FROM pg_catalog.pg_locks WHERE locktype='advisory' AND classid=0::oid AND objid=8723116::oid AND objsubid=1 AND mode='ExclusiveLock' AND granted")).rows + assert.equal(rows.length,1);assert.equal(rows[0].pid,physicalPid) + const shared=await rival.query("SELECT pg_catalog.pg_try_advisory_lock_shared(8723116) AS acquired") + if(shared.rows[0].acquired){await rival.query("SELECT pg_catalog.pg_advisory_unlock_shared(8723116)");assert.fail("exclusive owner lost")} + heldChecks++ + } + const fixedDocker=createDockerBandwidthOperatorStopAdapter({socketPath:policy.socketPath,daemonId:policy.daemonId,inventoryCanonicalJson:inventory,inventorySignatureBase64url:sign(null,Buffer.from(inventory),keys.privateKey).toString("base64url"),inventoryPublicKeyPem:keys.publicKey.export({type:"spki",format:"pem"}).toString(),inventoryPublicKeySha256:publicSha}) + const composition=createBandwidthOperatorWriterComposition({config:{...f.root,trustKeyFile,grantFile,trustKeySha256:publicSha},externalStop:fixedDocker, + nodeSource:{...signed.node,async openWithOwnershipFence(scope,guard,ownership){ + sourceOpens++;const session=await signed.node.openWithOwnershipFence(scope,guard,ownership);let closed=false + return {...session,close(){if(!closed){closed=true;physicalDestroys++}session.close()},connection:{release:session.connection.release.bind(session.connection),async query(text,values,options){ + const command=operatorDockerSqlTag(text);commands.push(command) + try { + const result=await session.connection.query(text,values,options) + const pid=result.rows[0]?.backend_pid;if(typeof pid==="number"){if(physicalPid===undefined)physicalPid=pid;else assert.equal(pid,physicalPid)} + // EXACT packetV7 opening exclusions, then same rival ownership proof. + if(!text.includes("pg_try_advisory_lock(")&&!text.includes("pg_backend_pid()"))await held() + if(text==="COMMIT"){assert("command"in result);assert.equal(result.command,"COMMIT");commits++} + return result + }catch(error){firstFailure??={command,code:operatorDockerSafeCode(error),elapsedMs:Date.now()-began,remainingMs:Math.max(0,deadline-Date.now())};throw error} + }}} + }},observationSource:{async read(requested,scope){assert.deepEqual(requested,peer);await held();credentials++;return f.readPeerSnapshot(requested,scope)}}}) + try { + began=Date.now();deadline=began+5000 + await composition.register({deadlineAt:deadline,signal:new AbortController().signal}) + assert.equal(commands[0],"WINDOW_LOCK");assert.equal(commands[1],"WINDOW_PID");assert.equal(pids.size,1) + assert.equal(commits,1);assert.equal(sourceOpens,1);assert.equal(physicalDestroys,1);assert(credentials>0&&heldChecks>0&&f.nativeGets()>0);assert.equal(f.nativePosts(),0) + assert.equal(await f.digest(["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events"]),before) + assert.equal((await f.owner.query("SELECT count(*)::integer AS count FROM mmapp_control.bandwidth_external_node_aliases WHERE enrollment_id=$1",[peer.enrollmentId])).rows[0].count,1) + return {passed:true,accepted:true,latencyMode:latencyMode??"none",actualSql:true,genuineLocalhostLicenseHttp:true,mockedDockerHttp:true,actualDockerStop:false,originalWindowMs:5000,elapsedMs:Date.now()-began,commands,physicalTags,physicalPids:pids.size,sourceOpens,physicalDestroys,commits,credentials,heldChecks,dockerGets,dockerKinds} + }catch(error){ + console.log("Owned Docker SQL safe failure:",JSON.stringify({accepted:false,latencyMode:latencyMode??"none",firstFailure,firstPhysicalFailure,elapsedMs:Date.now()-began,commands,physicalTags,physicalPids:pids.size,sourceOpens,physicalDestroys,commits,credentials,heldChecks,dockerGets,dockerKinds,modeledSequentialLatencyMs:latencyMode?dockerKinds.info*STAND_LATENCY.info+dockerKinds.inventory*STAND_LATENCY.inventory+dockerKinds.inspect*STAND_LATENCY.inspect:0,code:operatorDockerSafeCode(error)})) + throw error + }finally{ + composition.close();delivery.close();for(const response of responses)response.destroy();responses.clear() + await new Promise(resolve=>setImmediate(resolve));assert.equal(delivery.metrics().pendingTimers,0) + console.log("Owned Docker SQL mock drained:",JSON.stringify({...delivery.metrics(),activeResponses:responses.size})) + rival.release(true);Client.prototype.query=driverQuery;http.request=httpRequest + const actual=await realpath(folder),parent=await realpath(state),info=await lstat(folder) + assert(info.isDirectory()&&!info.isSymbolicLink());assert.equal(dirname(actual),parent);assert.match(basename(actual),/^operator-docker-sql-[A-Za-z0-9]+$/) + await rm(actual,{recursive:true,force:true}) + } +} diff --git a/backend/src/services/bandwidth-operator-docker-stop.sql.test.ts b/backend/src/services/bandwidth-operator-docker-stop.sql.test.ts new file mode 100644 index 00000000..34294147 --- /dev/null +++ b/backend/src/services/bandwidth-operator-docker-stop.sql.test.ts @@ -0,0 +1,16 @@ +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import { assertOperatorDockerSqlDiagnostics, assertOperatorDockerLatencyDiagnostics, operatorDockerLatencyMode, operatorDockerSafeCode } from "./bandwidth-operator-docker-stop.sql-helper.js" + +async function main(){ + assertOwnedBandwidthSql();assert.equal(process.env.MMAPP_OWN_OPERATOR_DOCKER_SQL,"true") + operatorDockerLatencyMode() + console.log("Owned Docker SQL diagnostic classification:",JSON.stringify(assertOperatorDockerSqlDiagnostics())) + console.log("Owned Docker SQL fixed latency budget:",JSON.stringify(await assertOperatorDockerLatencyDiagnostics())) + const {createOwnedExternalRegistryHarness}=await import("./bandwidth-external-node-registry.sql-helper.js") + const {acceptOwnedDockerOperatorSql}=await import("./bandwidth-operator-docker-stop.sql-helper.js") + const fixture=await createOwnedExternalRegistryHarness() + try {const signed=await fixture.createSignedSource();console.log("Owned Docker operator SQL:",JSON.stringify(await acceptOwnedDockerOperatorSql(fixture,signed)))} + finally{await fixture.close()} +} +void main().catch(error=>{console.log("Owned Docker operator SQL refused:",JSON.stringify({code:operatorDockerSafeCode(error),actualBackendActions:0}));process.exitCode=1}) diff --git a/backend/src/services/bandwidth-operator-docker-stop.test.ts b/backend/src/services/bandwidth-operator-docker-stop.test.ts new file mode 100644 index 00000000..4d8416e7 --- /dev/null +++ b/backend/src/services/bandwidth-operator-docker-stop.test.ts @@ -0,0 +1,313 @@ +/** Synthetic Docker HTTP only. No daemon, socket, network or container actions. */ +import { test, mock } from "node:test" +import assert from "node:assert/strict" +import { EventEmitter } from "node:events" +import { PassThrough } from "node:stream" +import http from "node:http" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { performance } from "node:perf_hooks" +import { lstat, mkdtemp, realpath, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, join } from "node:path" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import type { BandwidthOperatorOriginalAuthoritySnapshot } from "./bandwidth-operator-writer-window.js" +import { createBandwidthOperatorOriginalAuthorityReader } from "./bandwidth-operator-writer-window.js" +import { bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { createDockerBandwidthOperatorStopAdapter, createDockerBandwidthOperatorWriterComposition, + type DockerBandwidthOperatorWriterCompositionDependencies } from "./bandwidth-operator-docker-stop.js" + +const sha = (data: string | Buffer) => createHash("sha256").update(data).digest("hex") +const installationId = "00000000-0000-4000-8000-000000000001" +const { publicKey, privateKey } = generateKeyPairSync("ed25519") +const ids = ["a".repeat(64), "b".repeat(64), "c".repeat(64)] +function fixture(members = [ + { service: "backend", role: "backend" }, { service: "updater", role: "updater" }, { service: "proxy", role: "unrelated" }, +]) { + const ids = members.map((_member, index) => String.fromCharCode(97 + index).repeat(64)) + const now = Date.now() + const policy = { + format: "mmapp-bandwidth-docker-only-quiescence-v1", installationId, daemonId: "owned-daemon", socketPath: "/var/run/docker.sock", + automaticExecutorScope: "complete-docker-only-inventory", outsideAutomaticControllers: "none-operator-asserted", + administratorBoundary: "trusted-no-manual-start-create-or-policy-change-during-window", + socketAliases: ["/var/run/docker.sock", "/run/docker.sock"], + containers: ids.map((containerId, index) => ({ containerId, imageId: "sha256:" + String(index + 1).repeat(64), + project: "owned-project", service: members[index].service, role: members[index].role })), + sourceArtifacts: [{ name: "owned-compose-fixture", sha256: sha("fixture compose source") }], + } + function signedConfig() { + const bytes = canonical(policy) + return { socketPath: policy.socketPath, daemonId: policy.daemonId, inventoryCanonicalJson: bytes, + inventorySignatureBase64url: sign(null, Buffer.from(bytes), privateKey).toString("base64url"), + inventoryPublicKeyPem: publicKey.export({ format: "pem", type: "spki" }).toString(), + inventoryPublicKeySha256: sha(publicKey.export({ format: "der", type: "spki" })) } + } + const original = Object.freeze({ grant: Object.freeze({ executorInventorySha256: sha(canonical(policy)), installationId, + executorsStoppedAt: now - 1000, issuedAt: now - 500, expiresAt: now + 4500 }), + proof: "f".repeat(64), end: performance.now() + 4500 }) as unknown as BandwidthOperatorOriginalAuthoritySnapshot + const inspect = new Map(ids.map((Id, index) => [Id, { + Id, Image: policy.containers[index].imageId, + Config: { Labels: { "com.docker.compose.project": "owned-project", "com.docker.compose.service": policy.containers[index].service }, Env: ["SECRET=never print this"] }, + HostConfig: { RestartPolicy: { Name: "no", MaximumRetryCount: 0 }, Privileged: false, PidMode: "" }, + State: { Running: false, Restarting: false, Paused: false, Dead: false, Pid: 0, Status: "exited", FinishedAt: new Date(now - 2000).toISOString() }, + Mounts: members[index].role === "updater" ? [{ Type: "bind", Source: "/var/run/docker.sock", Destination: "/var/run/docker.sock", RW: true }] : [], + }])) + let calls = 0, destroyed = 0, swarm = "inactive", daemon = "owned-daemon" + let listed = [...ids], before: ((path: string, count: number) => void) | undefined + let payload: string | undefined, hang = false + const requests: http.RequestOptions[] = [] + const transport = mock.method(http, "request", ((options: http.RequestOptions, callback: (value: unknown) => void) => { + calls++; const path = String(options.path); requests.push(options); before?.(path, calls) + assert.equal(options.method, "GET"); assert.equal(options.socketPath, "/var/run/docker.sock"); assert.equal(options.agent, false) + assert.equal(options.hostname, undefined); assert.equal(options.port, undefined) + assert(/^\/v1\.45\/(info|containers\/json\?all=1&size=0|containers\/[a-f0-9]{64}\/json)$/.test(path)) + const request = Object.assign(new EventEmitter(), { destroy() { destroyed++ }, end() { + if (hang) return + queueMicrotask(() => { + const response = Object.assign(new PassThrough(), { statusCode: 200, headers: { "content-type": "application/json" }, complete: true }) + callback(response) + const value = path === "/v1.45/info" ? { ID: daemon, Swarm: { LocalNodeState: swarm } } + : path.includes("?all=") ? listed.map(Id => ({ Id })) : inspect.get(path.split("/")[3]) + response.end(payload ?? JSON.stringify(value)) + }) + } }) + return request + }) as typeof http.request) + return { policy, original, inspect, signedConfig, requests, + adapter: () => createDockerBandwidthOperatorStopAdapter(signedConfig()), scope: () => ({ deadlineAt: Date.now() + 4000 }), + get calls() { return calls }, get destroyed() { return destroyed }, + set before(value: typeof before) { before = value }, set swarm(value: string) { swarm = value }, set daemon(value: string) { daemon = value }, + set listed(value: string[]) { listed = value }, set payload(value: string) { payload = value }, set hang(value: boolean) { hang = value }, + close() { transport.mock.restore() }, + } +} +async function refused(work: () => unknown) { + await assert.rejects(async () => work(), (error: unknown) => { + assert.equal((error as { code: string }).code, "BANDWIDTH_OPERATOR_WRITER_FENCE_REFUSED") + assert(!String(error).includes("SECRET")); return true + }) +} +test("inert constructor; immutable authenticated policy; GET-only opaque held reinspection", async () => { + const f = fixture() + try { + const config = f.signedConfig(), adapter = createDockerBandwidthOperatorStopAdapter(config) + assert.equal(f.calls, 0) + config.socketPath = "/other.sock"; config.daemonId = "evil"; config.inventoryCanonicalJson = "{}" + const held = await adapter.acquire(f.original, f.scope()) + assert.equal(f.calls, 10); assert.deepEqual(Object.keys(held).sort(), ["assertCurrent", "close"]) + await held.assertCurrent(f.original, f.scope()); assert.equal(f.calls, 20) + held.close(); await refused(() => held.assertCurrent(f.original, f.scope())); assert.equal(f.calls, 20) + } finally { f.close() } +}) +test("actual stand shape: frontend/backend/portal/postgres, no updater; unrelated services remain running", async () => { + const f = fixture([{ service: "frontend", role: "unrelated" }, { service: "backend", role: "backend" }, + { service: "portal", role: "unrelated" }, { service: "postgres", role: "unrelated" }]) + try { + for (const item of f.policy.containers) { + const live = f.inspect.get(item.containerId)! + if (item.role === "unrelated") { live.State.Running = true; live.State.Status = "running"; live.State.Pid = 123; live.HostConfig.RestartPolicy.Name = "unless-stopped" } + } + const held = await f.adapter().acquire(f.original, f.scope()) + assert.equal(f.calls, 12); await held.assertCurrent(f.original, f.scope()); assert.equal(f.calls, 24); held.close() + } finally { f.close() } +}) + +test("genuine signed original grant distinguishes tmpfs from host binds and malformed mounts", async t => { + const f = fixture(), directory = await mkdtemp(join(tmpdir(), "bw-tmpfs-grant-")) + const trustKeyFile = join(directory,"operator.pub"), grantFile = join(directory,"operator.json"), now = Date.now() + const root = { installationId, issuerKeySha256:"7".repeat(64), quiescenceProof:"8".repeat(64) } + const grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format:"mmapp-bandwidth-operator-enrollment-v1", ...root, + grantId:randomUUID(), operatorId:"owned-mount-schema-unit", executorInventorySha256:sha(canonical(f.policy)), executorsStoppedAt:f.original.grant.executorsStoppedAt, + issuedAt:now, expiresAt:now+120000, peerRef:{kind:"external",enrollmentId:randomUUID()}, sourceProof:"9".repeat(64), authorityGeneration:"owned-local", + routingEndpointEpoch:randomUUID(), claims:[{kind:"system-id",value:"OWNED-TMPFS-SCHEMA"}] }) + const payload = Buffer.from(canonical(grant)) + await writeFile(trustKeyFile,publicKey.export({type:"spki",format:"pem"})) + await writeFile(grantFile,JSON.stringify({format:"mmapp-bandwidth-operator-enrollment-envelope-v1",payload:payload.toString("base64url"),signature:sign(null,payload,privateKey).toString("base64url")})) + const originals = createBandwidthOperatorOriginalAuthorityReader({...root,trustKeyFile,grantFile,trustKeySha256:sha(publicKey.export({type:"spki",format:"der"}))}) + const unrelated = f.inspect.get(ids[2])! + unrelated.State.Running=true;unrelated.State.Status="running";unrelated.State.Pid=321;unrelated.HostConfig.RestartPolicy.Name="unless-stopped" + const setMounts = (mounts:unknown[]) => { (unrelated as {Mounts:unknown[]}).Mounts=mounts } + const original = async () => originals.inspect(await originals.acquire(f.scope()),f.scope()) + try { + for (const [name,mount] of [ + ["empty-source",{Type:"tmpfs",Source:"",Destination:"/var/lib/postgresql/data"}], + ["omitted-source",{Type:"tmpfs",Destination:"/var/lib/postgresql/data"}], + ["virtual-socket-name",{Type:"tmpfs",Source:"",Destination:"/var/run/docker.sock"}], + ] as const) await t.test("recognized tmpfs "+name,async()=>{ + setMounts([mount]);const held=await f.adapter().acquire(await original(),f.scope());held.close() + }) + for (const [name,mount] of [ + ["empty-bind",{Type:"bind",Source:"",Destination:"/safe"}], + ["empty-volume",{Type:"volume",Source:"",Destination:"/safe"}], + ["host-root-bind",{Type:"bind",Source:"/",Destination:"/root"}], + ["host-parent-bind",{Type:"bind",Source:"/run",Destination:"/host-run"}], + ["unknown-type",{Type:"unknown",Source:"/safe",Destination:"/safe"}], + ["missing-type",{Source:"/safe",Destination:"/safe"}], + ["null-tmpfs-source",{Type:"tmpfs",Source:null,Destination:"/safe"}], + ["nonempty-tmpfs-source",{Type:"tmpfs",Source:"/host",Destination:"/safe"}], + ["relative-tmpfs-destination",{Type:"tmpfs",Destination:"relative"}], + ["noncanonical-tmpfs-destination",{Type:"tmpfs",Destination:"/a/../safe"}], + ["relative-bind-source",{Type:"bind",Source:"relative",Destination:"/safe"}], + ] as const) await t.test("refuses "+name,async()=>{ + setMounts([mount]);await refused(async()=>f.adapter().acquire(await original(),f.scope())) + }) + await t.test("all mounts validated after a dangerous bind on stopped protected executor",async()=>{ + setMounts([]) + ;(f.inspect.get(ids[0])! as {Mounts:unknown[]}).Mounts=[{Type:"bind",Source:"/run/docker.sock",Destination:"/socket"},{Type:"unknown",Source:"/safe",Destination:"/safe"}] + await refused(async()=>f.adapter().acquire(await original(),f.scope())) + }) + } finally { + originals.close();f.close() + const actual=await realpath(directory), parent=await realpath(tmpdir()), info=await lstat(directory) + assert(info.isDirectory()&&!info.isSymbolicLink());assert.equal(dirname(actual),parent);assert.match(basename(actual),/^bw-tmpfs-grant-[A-Za-z0-9]+$/) + await rm(actual,{recursive:true,force:true}) + } +}) + +test("multiple backend replicas all require stopped state and disabled restart, with optional updater", async () => { + for (const updater of [false, true]) { + const members = [{ service: "backend", role: "backend" }, { service: "backend", role: "backend" }, ...(updater ? [{ service: "updater", role: "updater" }] : [])] + const f = fixture(members) + try { + const held = await f.adapter().acquire(f.original, f.scope()); held.close() + f.inspect.get(ids[1])!.State.Running = true + await refused(() => f.adapter().acquire(f.original, f.scope())) + f.inspect.get(ids[1])!.State.Running = false; f.inspect.get(ids[1])!.HostConfig.RestartPolicy.Name = "always" + await refused(() => f.adapter().acquire(f.original, f.scope())) + } finally { f.close() } + } +}) + +test("backend-only complete inventory is valid; no backend and empty inventory refuse before I/O", async () => { + const f = fixture([{ service: "backend", role: "backend" }]) + try { const held = await f.adapter().acquire(f.original, f.scope()); assert.equal(f.calls, 6); held.close() } finally { f.close() } + for (const members of [[], [{ service: "frontend", role: "unrelated" }]]) { + const g = fixture(members) + try { assert.throws(() => g.adapter()); assert.equal(g.calls, 0) } finally { g.close() } + } +}) + +test("signature/policy/inventory binding refuses before any Docker request", async () => { + const f = fixture() + try { + const config = f.signedConfig(); config.inventorySignatureBase64url = "a".repeat(86) + assert.throws(() => createDockerBandwidthOperatorStopAdapter(config)) + ;(f.policy as Record).outsideAutomaticControllers = "host-supervisor" + assert.throws(() => f.adapter()); assert.equal(f.calls, 0) + } finally { f.close() } + const g = fixture() + try { await refused(() => g.adapter().acquire({ ...g.original, grant: { ...g.original.grant, executorInventorySha256: "0".repeat(64) } }, g.scope())); assert.equal(g.calls, 0) } + finally { g.close() } +}) +for (const change of ["running", "restart", "image", "labels", "late-exit", "socket-ro", "socket-parent", "privileged", "remote-docker", "unknown", "missing", "daemon", "swarm"] as const) { + test(`live API refuses ${change}`, async () => { + const f = fixture() + try { + const backend = f.inspect.get(ids[0])!, unrelated = f.inspect.get(ids[2])! + if (change === "running") backend.State.Running = true + if (change === "restart") f.inspect.get(ids[1])!.HostConfig.RestartPolicy.Name = "unless-stopped" + if (change === "image") backend.Image = "sha256:" + "f".repeat(64) + if (change === "labels") backend.Config.Labels["com.docker.compose.project"] = "other-project" + if (change === "late-exit") backend.State.FinishedAt = new Date().toISOString() + if (change === "socket-ro") unrelated.Mounts = [{ Type: "bind", Source: "/var/run/docker.sock", Destination: "/socket", RW: false }] + if (change === "socket-parent") unrelated.Mounts = [{ Type: "bind", Source: "/run", Destination: "/host-run", RW: false }] + if (change === "privileged") unrelated.HostConfig.Privileged = true + if (change === "remote-docker") unrelated.Config.Env.push("DOCKER_HOST=tcp://controller:2375") + if (change === "unknown") f.listed = [...ids, "d".repeat(64)] + if (change === "missing") f.listed = ids.slice(0, 2) + if (change === "daemon") f.daemon = "other-daemon" + if (change === "swarm") f.swarm = "active" + await refused(() => f.adapter().acquire(f.original, f.scope())) + } finally { f.close() } + }) +} +test("second inspect catches reactivation during scan; held failure terminal", async () => { + const f = fixture() + try { + const held = await f.adapter().acquire(f.original, f.scope()) + f.before = (_path, count) => { if (count === 16) f.inspect.get(ids[0])!.State.Restarting = true } + await refused(() => held.assertCurrent(f.original, f.scope())) + const count = f.calls; f.inspect.get(ids[0])!.State.Restarting = false + await refused(() => held.assertCurrent(f.original, f.scope())); assert.equal(f.calls, count) + } finally { f.close() } +}) +test("original cancellation and monotonic horizon cannot be replaced", async () => { + const f = fixture() + try { + const controller = new AbortController(), original = Object.freeze({ ...f.original, signal: controller.signal }) + const held = await f.adapter().acquire(original, f.scope()); controller.abort() + await refused(() => held.assertCurrent(original, f.scope())); assert.equal(f.calls, 10) + await refused(() => f.adapter().acquire({ ...f.original, end: performance.now() - 1 }, f.scope())); assert.equal(f.calls, 10) + } finally { f.close() } +}) +test("trailing daemon/list checks refuse state replacement after inspections", async () => { + for (const change of ["daemon", "inventory"] as const) { + const f = fixture() + try { + f.before = (path, count) => { + if (change === "daemon" && path === "/v1.45/info" && count === 10) f.daemon = "replacement" + if (change === "inventory" && count === 9) f.listed = [...ids, "d".repeat(64)] + } + await refused(() => f.adapter().acquire(f.original, f.scope())) + } finally { f.close() } + } +}) +test("original replacement refuses and closing aborts an in-flight assert", async () => { + const f = fixture() + try { + const held = await f.adapter().acquire(f.original, f.scope()) + await refused(() => held.assertCurrent({ ...f.original }, f.scope())); assert.equal(f.calls, 10) + const another = await f.adapter().acquire(f.original, f.scope()) + f.hang = true + const pending = another.assertCurrent(f.original, f.scope()) + another.close(); await refused(() => pending); assert(f.destroyed > 0) + } finally { f.close() } +}) +test("bounded malformed/oversize JSON and in-flight timeout/cancellation refuse", async () => { + for (const payload of ["{", "x".repeat(2 * 1024 * 1024 + 1)]) { + const f = fixture() + try { f.payload = payload; await refused(() => f.adapter().acquire(f.original, f.scope())) } finally { f.close() } + } + const g = fixture() + try { + g.hang = true; await refused(() => g.adapter().acquire(g.original, { deadlineAt: Date.now() + 30 })); assert(g.destroyed > 0) + const controller = new AbortController() + const pending = g.adapter().acquire(g.original, { ...g.scope(), signal: controller.signal }); controller.abort() + await refused(() => pending); assert(g.destroyed > 1) + } finally { g.close() } +}) + +for (const prepared of [false, true]) { + test(`concrete Docker composition ${prepared ? "verifies live stop before source lease" : "refuses live stop failure before source lease"}`, async () => { + const f = fixture(), directory = await mkdtemp(join(tmpdir(), "docker-operator-composition-")) + let leases = 0, credentials = 0 + let composition: ReturnType | undefined + try { + const trustKeyFile = join(directory, "operator.pub"), grantFile = join(directory, "grant.json") + const config = { trustKeyFile, grantFile, trustKeySha256: sha(publicKey.export({ format: "der", type: "spki" })), + installationId, issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } + const grant = { ...f.original.grant, format: "mmapp-bandwidth-operator-enrollment-v1", + grantId: "00000000-0000-4000-8000-000000000002", operatorId: "owned-docker-composition-fixture", + issuerKeySha256: config.issuerKeySha256, quiescenceProof: config.quiescenceProof, + peerRef: { kind: "external", enrollmentId: "00000000-0000-4000-8000-000000000003" }, + sourceProof: "d".repeat(64), authorityGeneration: "owned-fixture", routingEndpointEpoch: "00000000-0000-4000-8000-000000000004", + claims: [{ kind: "system-id", value: "owned-synthetic-system" }] } + const payload = Buffer.from(canonical(grant)) + await writeFile(trustKeyFile, publicKey.export({ format: "pem", type: "spki" })) + await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", + payload: payload.toString("base64url"), signature: sign(null, payload, privateKey).toString("base64url") })) + const refuseLease = async () => { leases++; assert(f.calls >= 20); throw Error("synthetic source boundary reached; no DB") } + const nodeSource = { open: refuseLease, openWithOwnershipFence: refuseLease } as unknown as DockerBandwidthOperatorWriterCompositionDependencies["nodeSource"] + if (!prepared) f.daemon = "wrong-live-daemon" + composition = createDockerBandwidthOperatorWriterComposition({ config, nodeSource, + observationSource: { async read() { credentials++; throw Error("must not resolve credentials") } }, docker: f.signedConfig() }) + assert.equal(f.calls, 0); assert.equal(leases, 0) + await refused(() => composition!.register(f.scope())) + assert.equal(leases, prepared ? 1 : 0); assert.equal(credentials, 0) + assert.equal(f.calls, prepared ? 20 : 1) + composition.close() + await refused(() => composition!.register(f.scope())) + assert.equal(f.calls, prepared ? 20 : 1) + } finally { composition?.close(); f.close(); await rm(directory, { recursive: true, force: true }) } + }) +} + diff --git a/backend/src/services/bandwidth-operator-docker-stop.ts b/backend/src/services/bandwidth-operator-docker-stop.ts new file mode 100644 index 00000000..0a7f8379 --- /dev/null +++ b/backend/src/services/bandwidth-operator-docker-stop.ts @@ -0,0 +1,250 @@ +/** Optional trusted-construction Docker-only automatic-executor stop adapter. + * Engine RestartPolicy=no and already stopped inventoried recreation controllers + * enforce automatic restart/recreation prohibition in this supported boundary. + * Complete Docker-only inventory is a trusted authenticated operator assertion; + * host/remote supervisors must be absent, manual/root administrators trusted. + * Live GETs verify stopped state/policy repeatedly; signatures, SQL locks and + * sourceArtifacts provenance assertions alone never prove stop or code hashes. + * No stop/start/configuration operations, startup activation or default wiring. + * Operating acceptance remains required before this can satisfy S3-06. + */ +import http from "node:http" +import { createHash, createPublicKey, verify } from "node:crypto" +import { posix } from "node:path" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { BandwidthOperatorWriterFenceError, type BandwidthOperatorExternalStopAdapter, + type BandwidthOperatorOriginalAuthoritySnapshot, type BandwidthOperatorScope } from "./bandwidth-operator-writer-window.js" + +import { createBandwidthOperatorWriterComposition, + type BandwidthOperatorWriterCompositionDependencies } from "./bandwidth-operator-writer-composition.js" +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const id = hash, label = z.string().min(1).max(200) +const socket = z.string().max(256).refine(value => value.startsWith("/") && posix.normalize(value) === value && !/[\x00-\x20\x7f]/.test(value)) +const target = z.object({ containerId: id, imageId: z.string().regex(/^sha256:[a-f0-9]{64}$/), + project: label, service: label, role: z.enum(["backend", "updater", "recreation-controller", "unrelated"]), +}).strict() +const policySchema = z.object({ + format: z.literal("mmapp-bandwidth-docker-only-quiescence-v1"), installationId: z.string().uuid(), + daemonId: z.string().min(1).max(200), socketPath: socket, + // These are explicit signed scope restrictions, not evidence of live state. + automaticExecutorScope: z.literal("complete-docker-only-inventory"), + outsideAutomaticControllers: z.literal("none-operator-asserted"), + administratorBoundary: z.literal("trusted-no-manual-start-create-or-policy-change-during-window"), + socketAliases: z.array(socket).min(1).max(16), containers: z.array(target).min(1).max(256), + sourceArtifacts: z.array(z.object({ name: label, sha256: hash }).strict()).min(1).max(32), +}).strict() +export interface DockerBandwidthOperatorStopConfig { + socketPath: string + daemonId: string + /** Exact canonical UTF-8 signed policy, never a path/URL or mutable callback. */ + inventoryCanonicalJson: string + inventorySignatureBase64url: string + inventoryPublicKeyPem: string + inventoryPublicKeySha256: string +} +const sha = (data: string | Buffer) => createHash("sha256").update(data).digest("hex") +const refuse = (): never => { throw new BandwidthOperatorWriterFenceError() } +const object = (value: unknown): Record => { + if (!value || typeof value !== "object" || Array.isArray(value)) return refuse() + return value as Record +} +const array = (value: unknown, max = 256): unknown[] => { + if (!Array.isArray(value) || value.length > max) return refuse() + return value +} +function plainScope(raw: Readonly): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.keys(raw).some(key => key !== "deadlineAt" && key !== "signal") + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(value => !("value" in value) || !value.enumerable) + || !Number.isSafeInteger(raw.deadlineAt) || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) return refuse() + return Object.freeze({ deadlineAt: raw.deadlineAt, signal: raw.signal }) +} + +/** Trusted construction only; no transport/client escapes this closure. Only + * fixed Docker Engine v1.45 GET paths over the pinned Unix socket are issued. + * Import and construction perform no Docker I/O. Test via synthetic HTTP mock. + */ +export function createDockerBandwidthOperatorStopAdapter(raw: DockerBandwidthOperatorStopConfig): BandwidthOperatorExternalStopAdapter { + let policy: z.infer, inventoryHash: string + try { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype || Reflect.ownKeys(raw).length !== 6 + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(value => !("value" in value) || !value.enumerable) + || typeof raw.inventoryCanonicalJson !== "string" || Buffer.byteLength(raw.inventoryCanonicalJson) > 131072 + || typeof raw.inventoryPublicKeyPem !== "string" || Buffer.byteLength(raw.inventoryPublicKeyPem) > 16384) refuse() + const socketPath = socket.parse(raw.socketPath), daemonId = label.parse(raw.daemonId) + const bytes = Buffer.from(raw.inventoryCanonicalJson, "utf8") + const key = createPublicKey(raw.inventoryPublicKeyPem) + if (key.asymmetricKeyType !== "ed25519" || sha(key.export({ type: "spki", format: "der" })) !== hash.parse(raw.inventoryPublicKeySha256) + || !/^[A-Za-z0-9_-]{86}$/.test(raw.inventorySignatureBase64url)) refuse() + const signature = Buffer.from(raw.inventorySignatureBase64url, "base64url") + if (signature.toString("base64url") !== raw.inventorySignatureBase64url || !verify(null, bytes, key, signature)) refuse() + policy = policySchema.parse(JSON.parse(raw.inventoryCanonicalJson)) + if (canonicalBandwidthNodeValue(policy) !== raw.inventoryCanonicalJson || policy.socketPath !== socketPath || policy.daemonId !== daemonId + || !policy.socketAliases.includes(socketPath) || !policy.socketAliases.includes("/var/run/docker.sock") + || new Set(policy.socketAliases).size !== policy.socketAliases.length + || policy.containers.filter(item => item.role === "backend").length < 1 + || new Set(policy.containers.map(item => item.containerId)).size !== policy.containers.length + || policy.containers.some((item, index) => index > 0 && item.containerId <= policy.containers[index - 1].containerId) + || new Set(policy.sourceArtifacts.map(item => item.name)).size !== policy.sourceArtifacts.length) refuse() + inventoryHash = sha(bytes) + } catch { return refuse() } + // zod parsed a detached copy. Nothing later reads the caller's config/policy. + const pinnedSocket = policy.socketPath, pinnedDaemon = policy.daemonId + const targets = new Map(policy.containers.map(item => [item.containerId, Object.freeze(item)])) + const aliases = Object.freeze([...policy.socketAliases]) + const expectedIds = [...targets.keys()] + const MAX_BODY = 2 * 1024 * 1024 + type Lifetime = { original: BandwidthOperatorOriginalAuthoritySnapshot; proof: string; end: number; deadlineAt: number; + originalSignal?: AbortSignal; inputSignal?: AbortSignal; controller: AbortController; stopped: boolean; busy: boolean } + + function live(state: Lifetime, scope: Readonly) { + if (state.stopped || state.originalSignal?.aborted || state.inputSignal?.aborted || scope.signal?.aborted + || Date.now() >= state.deadlineAt || Date.now() >= scope.deadlineAt || Date.now() >= state.original.grant.expiresAt + || performance.now() >= state.end) refuse() + } + async function get(path: string, state: Lifetime, scope: Readonly): Promise { + live(state, scope) + const remaining = Math.min(state.end - performance.now(), state.deadlineAt - Date.now(), scope.deadlineAt - Date.now(), state.original.grant.expiresAt - Date.now()) + const result = await new Promise((resolve, reject) => { + let done = false, bytes = 0, request: http.ClientRequest | undefined + const signals = [...new Set([state.originalSignal, state.inputSignal, scope.signal, state.controller.signal].filter((value): value is AbortSignal => !!value))] + const finish = (failure?: unknown, value?: unknown) => { + if (done) return + done = true; clearTimeout(timer); signals.forEach(signal => signal.removeEventListener("abort", abort)) + if (failure) { request?.destroy(); reject(new BandwidthOperatorWriterFenceError()) } else resolve(value) + } + const abort = () => finish(true) + const timer = setTimeout(abort, Math.max(1, Math.ceil(remaining))) + signals.forEach(signal => signal.addEventListener("abort", abort, { once: true })) + try { + live(state, scope) + request = http.request({ socketPath: pinnedSocket, path, method: "GET", agent: false, + headers: { Accept: "application/json", Connection: "close" }, maxHeaderSize: 16384 }, response => { + const chunks: Buffer[] = [] + if (response.statusCode !== 200 || response.headers["content-encoding"] && response.headers["content-encoding"] !== "identity" + || !/^application\/json(?:\s*;|$)/i.test(String(response.headers["content-type"])) + || response.headers["content-length"] !== undefined && (!/^\d+$/.test(String(response.headers["content-length"])) || Number(response.headers["content-length"]) > MAX_BODY)) { + response.resume(); finish(true); return + } + response.on("data", (chunk: Buffer) => { + try { live(state, scope); bytes += chunk.length; if (bytes > MAX_BODY) { response.destroy(); finish(true) } else chunks.push(Buffer.from(chunk)) } + catch { response.destroy(); finish(true) } + }) + response.on("aborted", abort); response.on("error", abort) + response.on("end", () => { + try { + live(state, scope) + if (!response.complete) return finish(true) + finish(undefined, JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks)))) + } catch { finish(true) } + }) + }) + request.on("error", abort); request.end() + } catch { finish(true) } + }) + live(state, scope); return result + } + async function daemon(state: Lifetime, scope: Readonly) { + const info = object(await get("/v1.45/info", state, scope)) + if (info.ID !== pinnedDaemon || object(info.Swarm).LocalNodeState !== "inactive") refuse() + } + async function inventory(state: Lifetime, scope: Readonly) { + const listed = array(await get("/v1.45/containers/json?all=1&size=0", state, scope)).map(value => id.parse(object(value).Id)).sort() + if (listed.length !== expectedIds.length || listed.some((value, index) => value !== expectedIds[index])) refuse() + } + async function inspect(containerId: string, state: Lifetime, scope: Readonly) { + const expected = targets.get(containerId)! + const data = object(await get(`/v1.45/containers/${containerId}/json`, state, scope)) + const config = object(data.Config), labels = object(config.Labels), host = object(data.HostConfig), status = object(data.State) + if (data.Id !== containerId || data.Image !== expected.imageId || labels["com.docker.compose.project"] !== expected.project + || labels["com.docker.compose.service"] !== expected.service) refuse() + // Inspect ALL containers, including unrelated ones. Read-only bind mounts + // still grant Docker HTTP writes; parent-directory binds can expose sockets. + const mountAccess = array(data.Mounts).map(value => { + const mount = object(value) + const destination = mount.Destination + const canonicalPath = (path: unknown): path is string => typeof path === "string" && path.startsWith("/") && posix.normalize(path) === path && !/[\x00-\x1f\x7f]/.test(path) + if (!canonicalPath(destination)) return refuse() + // Genuine tmpfs has no host source. Its destination, even docker.sock, + // does not create a host Docker socket bind. Unknown/nonempty sources + // are not reclassified as tmpfs merely because they look harmless. + if (mount.Type === "tmpfs") { + if (mount.Source !== undefined && mount.Source !== "") return refuse() + return false + } + if (mount.Type !== "bind" && mount.Type !== "volume" || !canonicalPath(mount.Source)) return refuse() + const source = mount.Source + return aliases.some(alias => source === alias || alias.startsWith(source.replace(/\/$/, "") + "/")) + || /(^|\/)docker\.sock$/.test(source) || /(^|\/)docker\.sock$/.test(destination) + }).some(Boolean) + const remoteAccess = array(config.Env, 4096).some(value => { + if (typeof value !== "string") return refuse() + return /^(DOCKER_HOST|DOCKER_CONTEXT|CONTAINER_HOST)=/.test(value) + }) + if (typeof host.Privileged !== "boolean" || typeof host.PidMode !== "string") refuse() + const dangerous = mountAccess || remoteAccess || host.Privileged || host.PidMode === "host" + if (expected.role === "unrelated") { if (dangerous) refuse(); return } + if (status.Running !== false || status.Restarting !== false || status.Paused !== false || status.Dead !== false + || status.Status !== "exited" || status.Pid !== 0 || object(host.RestartPolicy).Name !== "no" + || object(host.RestartPolicy).MaximumRetryCount !== 0) refuse() + // Existing executor process must have exited no later than the signed + // stopped-at assertion. Created containers are deliberately unsupported. + const finished = typeof status.FinishedAt === "string" ? Date.parse(status.FinishedAt) : NaN + if (!Number.isFinite(finished) || finished <= 0 || finished > state.original.grant.executorsStoppedAt) refuse() + } + async function scan(state: Lifetime, scope: Readonly) { + live(state, scope); await daemon(state, scope); await inventory(state, scope) + for (const containerId of expectedIds) await inspect(containerId, state, scope) + // Recheck all facts across I/O. Still a sampled observation, never an + // atomic prohibition against trusted administrators changing the daemon. + for (const containerId of expectedIds) await inspect(containerId, state, scope) + await inventory(state, scope); await daemon(state, scope); live(state, scope) + } + return Object.freeze({ + async acquire(original: BandwidthOperatorOriginalAuthoritySnapshot, rawScope: Readonly) { + const scope = plainScope(rawScope), wall = Date.now(), mono = performance.now() + if (!original || original.grant.executorInventorySha256 !== inventoryHash || original.grant.installationId !== policy.installationId + || !Number.isFinite(original.end) || !/^[a-f0-9]{64}$/.test(original.proof)) refuse() + // Preserve the SAME original monotonic horizon, cancellation and grant. + const state: Lifetime = { original, proof: sha(canonicalBandwidthNodeValue(original.grant)), + end: Math.min(original.end, mono + Math.max(0, Math.min(5000, scope.deadlineAt - wall, original.grant.expiresAt - wall))), + deadlineAt: scope.deadlineAt, originalSignal: original.signal, inputSignal: scope.signal, controller: new AbortController(), stopped: false, busy: false } + try { await scan(state, scope) } catch { state.stopped = true; state.controller.abort(); return refuse() } + // Opaque closure-held capability: no evidence booleans/client/HTTP leaks. + return Object.freeze({ + async assertCurrent(current: BandwidthOperatorOriginalAuthoritySnapshot, requested: Readonly) { + try { + const checkScope = plainScope(requested) + if (state.busy || current !== original || current.proof !== original.proof || sha(canonicalBandwidthNodeValue(current.grant)) !== state.proof) refuse() + state.busy = true; await scan(state, checkScope) + } catch { state.stopped = true; state.controller.abort(); return refuse() } + finally { state.busy = false } + }, + close() { state.stopped = true; state.controller.abort() }, + }) + }, + }) +} + +export type DockerBandwidthOperatorWriterCompositionDependencies = + Omit & { + docker: DockerBandwidthOperatorStopConfig + } + +/** Explicit Linux-host operator composition; backend target remains stopped. + * Same existing original authority -> external Q -> node source -> registry + * path, one held SESSION lease. Construction performs no I/O and installs no + * runtime default. register requires separate operational preparation/acceptance. + * Trusted dependencies only, never an HTTP/MCP/model-provided config surface. + */ +export function createDockerBandwidthOperatorWriterComposition(dependencies: DockerBandwidthOperatorWriterCompositionDependencies) { + return createBandwidthOperatorWriterComposition({ + config: dependencies.config, + nodeSource: dependencies.nodeSource, + observationSource: dependencies.observationSource, + createObservationSource: dependencies.createObservationSource, + externalStop: createDockerBandwidthOperatorStopAdapter(dependencies.docker), + }) +} diff --git a/backend/src/services/bandwidth-operator-enrollment.test.ts b/backend/src/services/bandwidth-operator-enrollment.test.ts new file mode 100644 index 00000000..00b49271 --- /dev/null +++ b/backend/src/services/bandwidth-operator-enrollment.test.ts @@ -0,0 +1,226 @@ +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { once } from "node:events" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { createServer } from "node:http" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { createBandwidthOperatorEnrollmentPrerequisite, BandwidthOperatorEnrollmentError, + bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { bandwidthPeerRefKey, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import { createBandwidthOperatorRegistry } from "./bandwidth-operator-registry.js" + +const folder = await mkdtemp(join(tmpdir(), "bandwidth-operator-enrollment-")) +const keys = generateKeyPairSync("ed25519"), stranger = generateKeyPairSync("ed25519") +const digest = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex") +const keyFile = join(folder, "operator.pub"), grantFile = join(folder, "grant.json") +await writeFile(keyFile, keys.publicKey.export({ type: "spki", format: "pem" })) +let requests = 0, reads = 0, verifies = 0, databaseCalls = 0, fenced = true +let onRequest: (() => Promise) | undefined, onVerify: (() => Promise) | undefined +const server = createServer(async (req, res) => { + requests++ + assert.equal(req.method, "GET"); assert.equal(req.url, "/rest/system/license") + assert.equal(req.headers.authorization, "Basic " + Buffer.from("synthetic-operator:synthetic-password").toString("base64")) + await onRequest?.() + res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ "system-id": "SYNTHETIC-SYSTEM", "software-id": "SYNTHETIC-SOFTWARE" })) +}) +server.listen(0, "127.0.0.1"); await once(server, "listening") +const address = server.address(); assert(address && typeof address !== "string") +const managed: BandwidthPeerRef = { kind: "server", serverId: 73 }, external: BandwidthPeerRef = { kind: "external", enrollmentId: randomUUID() } +const config = { trustKeyFile: keyFile, trustKeySha256: digest(keys.publicKey.export({ type: "spki", format: "der" })), grantFile, + installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } +const endpoint = { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "synthetic-operator", password: "synthetic-password", routingEndpointEpoch: randomUUID() } +let source: BandwidthPeerSourceSnapshot = { peerRef: managed, enabled: true, sourceProof: "c".repeat(64), authorityGeneration: "original-operator-source", endpoint } +const trusted = { async read(peer: BandwidthPeerRef) { reads++; return bandwidthPeerRefKey(peer) === bandwidthPeerRefKey(source.peerRef) ? source : undefined } } +const quiescence = { async verify(grant: Readonly<{ quiescenceProof: string; executorInventorySha256: string }>) { + verifies++; assert.equal(grant.quiescenceProof, config.quiescenceProof); assert.equal(grant.executorInventorySha256, "d".repeat(64)); await onVerify?.() + if (!fenced) throw Error("Original executor fence unavailable") +} } +const make = () => createBandwidthOperatorEnrollmentPrerequisite(config, trusted, quiescence) +const coordinators: ReturnType[] = [] +const coordinator = make(); coordinators.push(coordinator) +const scope = (ms = 4000) => ({ deadlineAt: Date.now() + ms }) +const refused = (error: unknown) => error instanceof BandwidthOperatorEnrollmentError +function grant(peerRef: BandwidthPeerRef = source.peerRef) { + return bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", grantId: randomUUID(), operatorId: "synthetic-original-operator", + installationId: config.installationId, issuerKeySha256: config.issuerKeySha256, quiescenceProof: config.quiescenceProof, executorInventorySha256: "d".repeat(64), + executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000, peerRef, sourceProof: source.sourceProof, + authorityGeneration: source.authorityGeneration, routingEndpointEpoch: source.endpoint.routingEndpointEpoch, + claims: [{ kind: "system-id", value: "SYNTHETIC-SYSTEM" }, { kind: "software-id", value: "SYNTHETIC-SOFTWARE" }] }) +} +async function publish(value = grant(), privateKey = keys.privateKey) { + const payload = Buffer.from(canonicalBandwidthNodeValue(value)) + await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, privateKey).toString("base64url") })) +} +// No import of this component may borrow the application/default DB pool. +const { pool, closePool } = await import("../db/index.js") +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { databaseCalls++; throw Error("Forbidden database call") }) as typeof pool.query +pool.connect = (() => { databaseCalls++; throw Error("Forbidden database checkout") }) as typeof pool.connect +const cases: string[] = [] +try { + assert.equal(requests, 0); assert.equal(reads, 0); assert.equal(verifies, 0) + await assert.rejects(coordinator.acquire(managed, scope()), refused) + assert.equal(requests, 0) + for (const value of [grant(external), { ...grant(), installationId: randomUUID() }, { ...grant(), issuerKeySha256: "e".repeat(64) }, + { ...grant(), quiescenceProof: "e".repeat(64) }, { ...grant(), expiresAt: Date.now() - 1 }, { ...grant(), issuedAt: Date.now() + 1000 }]) { + await publish(value); await assert.rejects(coordinator.acquire(managed, scope()), refused) + } + await publish(grant(), stranger.privateKey); await assert.rejects(coordinator.acquire(managed, scope()), refused) + assert.equal(requests, 0) + cases.push("inert constructor; missing/foreign/expired/future/wrong-peer/operator-key grants refuse before native GET") + + await publish(); fenced = false + await assert.rejects(coordinator.acquire(managed, scope()), refused); assert.equal(requests, 0) + fenced = true + const originalVerify = quiescence.verify, originalRead = trusted.read + quiescence.verify = async () => { throw Error("Replaced verifier must not be used") } + trusted.read = async () => { throw Error("Replaced source must not be used") } + const token = await coordinator.acquire(managed, scope()), inspected = await coordinator.inspect(token, scope()) + assert.equal(requests, 1); assert.equal(inspected.evidence.version, 2); assert.deepEqual(inspected.evidence.peerRef, managed) + assert.equal(inspected.writerBarrierVerified, false); assert.equal(inspected.endpointMappingVerified, false) + assert(!JSON.stringify(inspected).includes(endpoint.password)); assert(!JSON.stringify(inspected).includes(endpoint.username)) + quiescence.verify = originalVerify; trusted.read = originalRead + for (const fake of [{ ...token }, JSON.parse(JSON.stringify(token)), inspected, inspected.observation]) await assert.rejects(coordinator.inspect(fake, scope()), refused) + const second = make(); coordinators.push(second); await assert.rejects(second.inspect(token, scope()), refused) + cases.push("real authenticated license GET; captured fixed dependencies; same-instance opaque prerequisite, no credentials, no DB or dispatch") + + const beforeRevokedCredentialReads = reads + fenced = false; await assert.rejects(coordinator.inspect(token, scope()), refused); fenced = true + assert.equal(reads, beforeRevokedCredentialReads) + await rm(grantFile); await assert.rejects(coordinator.inspect(token, scope()), refused) + assert.equal(reads, beforeRevokedCredentialReads) + await publish(); await assert.rejects(coordinator.inspect(token, scope()), refused) + source = { ...source, sourceProof: "e".repeat(64) }; await assert.rejects(coordinator.inspect(token, scope()), refused) + source = { ...source, sourceProof: "c".repeat(64) } + cases.push("fence loss, deletion/replacement revocation and current-source drift invalidate existing prerequisite") + + await publish({ ...grant(), claims: [{ kind: "software-id", value: "SYNTHETIC-SYSTEM" }] }) + await assert.rejects(coordinator.acquire(managed, scope()), refused) + await publish() + onRequest = async () => { await publish() } + await assert.rejects(coordinator.acquire(managed, scope()), refused); onRequest = undefined + await publish(); onVerify = async () => { fenced = false } + await assert.rejects(coordinator.acquire(managed, scope()), refused); onVerify = undefined; fenced = true + cases.push("typed claims mismatch, mid-GET grant replacement and asynchronous quiescence loss refuse publication") + + await publish(); onRequest = async () => { source = { ...source, authorityGeneration: "changed-during-native-get" } } + await assert.rejects(coordinator.acquire(managed, scope()), refused); onRequest = undefined + source = { ...source, authorityGeneration: "original-operator-source" } + await publish(); await writeFile(keyFile, stranger.publicKey.export({ type: "spki", format: "pem" })) + const beforeKeyFailure = requests + await assert.rejects(coordinator.acquire(managed, scope()), refused); assert.equal(requests, beforeKeyFailure) + await writeFile(keyFile, keys.publicKey.export({ type: "spki", format: "pem" })) + cases.push("source authority change during native GET and replaced pinned operator key fail closed") + + // An uncooperative external fence verifier must not extend the owned bound, + // and its eventual answer cannot publish a late capability. + let release!: () => void + const held = new Promise(done => { release = done }) + onVerify = () => held; await publish() + const beforeHeld = requests + await assert.rejects(coordinator.acquire(managed, scope(80)), refused) + release(); onVerify = undefined + await new Promise(done => setTimeout(done, 10)); assert.equal(requests, beforeHeld) + cases.push("held independent verifier is bounded, and late success performs no native GET or capability publication") + + // Original signed grant expiry is anchored BEFORE file IO. Moving the wall + // backwards while a verifier is stalled must not renew that lifetime. + const realNow = Date.now, shortGrant = { ...grant(), expiresAt: realNow() + 200 } + await publish(shortGrant) + let resumeFence!: () => void + const stalledFence = new Promise(done => { resumeFence = done }) + onVerify = async () => { Date.now = () => realNow() - 500; await stalledFence } + const beforeWallRollback = requests + let watchdog: ReturnType | undefined + try { + const timeout = new Promise((_resolve, reject) => { watchdog = setTimeout(() => reject(Error("Original grant monotonic bound was renewed")), 750) }) + await assert.rejects(Promise.race([coordinator.acquire(managed, scope()), timeout]), refused) + assert.equal(requests, beforeWallRollback) + } finally { if (watchdog) clearTimeout(watchdog); Date.now = realNow; resumeFence(); onVerify = undefined } + await new Promise(done => setTimeout(done, 10)); assert.equal(requests, beforeWallRollback) + cases.push("backward wall-clock jump during stalled quiescence cannot renew the original signed grant; expiry refuses before credential lookup/native GET") + + const originalAbort = new AbortController(), swappedScope = { ...scope(), signal: originalAbort.signal } + onVerify = async () => { originalAbort.abort(); swappedScope.signal = new AbortController().signal; swappedScope.deadlineAt = Date.now() + 60000 } + await publish(); const beforeScopeSwap = requests + await assert.rejects(coordinator.acquire(managed, swappedScope), refused); assert.equal(requests, beforeScopeSwap); onVerify = undefined + const getterScope = Object.defineProperty({}, "deadlineAt", { get() { throw Error("Accessor must never run") }, enumerable: true }) + await assert.rejects(coordinator.acquire(managed, getterScope as { deadlineAt: number }), refused) + cases.push("original scope is captured: signal/deadline replacement cannot revoke cancellation, and input accessors refuse without evaluation") + + source = { ...source, peerRef: external }; await publish() + const externalToken = await coordinator.acquire(external, scope()), externalProof = await coordinator.inspect(externalToken, scope()) + assert.deepEqual(externalProof.evidence.peerRef, external); assert.equal("serverId" in externalProof.evidence, false) + assert.deepEqual(externalProof.evidence.claims, inspected.evidence.claims) + await assert.rejects(coordinator.acquire(managed, scope()), refused) + cases.push("external enrollment uses independent management channel and v2 peer reference, same typed vector, no invented application server ID") + + // Full component composition: genuine signed grant + actual license GET + + // actual registry factories. Only the operator SQL/key-source is synthetic. + type Row = Record + const nodes: Row[] = [], aliases: Row[] = [], events: Row[] = [], registryQueries: string[] = [], signedMaterials: Array<{purpose:string;value:Row;mac:string}> = [] + const operatorRegistry = createBandwidthOperatorRegistry({ coordinator, source: { async open() { + return { close() {}, async binding() { return { installationId: config.installationId, issuerKeySha256: config.issuerKeySha256, quiescenceProof: config.quiescenceProof } }, + async keyedProof(purpose, value) { const digest=createHmac("sha256", "SYNTHETIC-PIPELINE-ISSUER").update(purpose + ":" + canonicalBandwidthNodeValue(value)).digest("hex");signedMaterials.push({purpose,value:structuredClone(value) as Row,mac:digest});return digest }, + connection: { release() {}, async query(sql, values = []) { + registryQueries.push(sql); const rows = (value: Row[] = [], command?: string) => ({ rows: structuredClone(value), command }) + if (sql === "BEGIN" || sql === "COMMIT" || sql === "ROLLBACK") return rows([], sql) + if (sql.includes("set_config") || sql.includes("pg_advisory_xact_lock")) return rows() + if (sql.includes("FROM mmapp_control.bandwidth_node_authority")) return rows([{ installation_id: config.installationId, issuer_key_sha256: config.issuerKeySha256, quiescence_proof: config.quiescenceProof, status: "active" }]) + const isExternal=sql.includes("bandwidth_external_node_"),subjectColumn=isExternal?"enrollment_id":"server_id" + if(sql.startsWith("WITH aliases AS MATERIALIZED")){ + const selected=aliases.filter(row=>row[subjectColumn]===values[0]) + return rows([{aliases:selected.map(row=>({subject:String(row[subjectColumn]),incarnation:row.incarnation,node_id:row.node_id,status:row.status,record_mac:row.record_mac})),nodes:nodes.filter(row=>selected.some(alias=>alias.node_id===row.node_id)),events:events.filter(row=>row[subjectColumn]===values[0]).map(row=>({event_id:row.event_id,incarnation:row.incarnation,node_id:row.node_id,kind:row.kind,old_status:row.old_status,new_status:row.new_status,authority_proof:row.authority_proof,event_mac:row.event_mac})),claim_nodes:values[1]===null?[]:nodes.filter(row=>row.claim_proof===values[1])}],"SELECT") + } + if(sql.startsWith("DO $mmapp_bandwidth_register$")){ + const last=(purpose:string)=>{const value=[...signedMaterials].reverse().find(value=>value.purpose===purpose);assert(value);return value!} + const node=last("node"),alias=last(isExternal?"external-alias":"alias"),event=last(isExternal?"external-event":"event"),subject=isExternal?alias.value.enrollmentId:alias.value.serverId + for(const value of [node.value.nodeId,node.value.claimProof,node.mac,alias.value.incarnation,alias.mac,event.value.eventId,event.mac])assert(sql.includes(String(value))) + if(sql.includes("IF true THEN")){assert(!nodes.some(row=>row.claim_proof===node.value.claimProof));nodes.push({node_id:node.value.nodeId,claim_proof:node.value.claimProof,enrollment_mac:node.mac})} + else assert(nodes.some(row=>row.node_id===node.value.nodeId&&row.claim_proof===node.value.claimProof&&row.enrollment_mac===node.mac)) + aliases.push({[subjectColumn]:subject,incarnation:alias.value.incarnation,node_id:alias.value.nodeId,status:"active",record_mac:alias.mac}) + events.push({event_id:event.value.eventId,[subjectColumn]:subject,incarnation:event.value.incarnation,node_id:event.value.nodeId,kind:"enroll",old_status:null,new_status:"active",authority_proof:event.value.authorityProof,event_mac:event.mac}) + return rows([],"DO") + } + if (sql.startsWith("SELECT") && sql.includes("FROM mmapp_control.bandwidth_nodes")) return rows(nodes.filter(row => row.claim_proof === values[0])) + if (sql.startsWith("SELECT") && sql.includes("_aliases WHERE")) return rows() + if (sql.startsWith("INSERT INTO mmapp_control.bandwidth_nodes")) { nodes.push({ node_id: values[0], claim_proof: values[1], enrollment_mac: values[2] }); return rows([{ node_id: values[0] }]) } + const id = sql.includes("bandwidth_external_node_") ? "enrollment_id" : "server_id" + if (sql.startsWith("INSERT") && sql.includes("_aliases(")) { aliases.push({ [id]: values[0], node_id: values[2] }); return rows([{ [id]: values[0] }]) } + if (sql.startsWith("INSERT") && sql.includes("_events(")) return rows([{ event_id: values[0] }]) + throw Error("Unexpected composed registry query: " + sql) + } }, + } + } } }) + source = { ...source, peerRef: managed }; await publish() + const composedManaged = await operatorRegistry.register(await coordinator.acquire(managed, scope()), scope()) + source = { ...source, peerRef: external }; await publish() + const composedExternal = await operatorRegistry.register(await coordinator.acquire(external, scope()), scope()) + assert.equal(composedManaged.canonicalNodeId, composedExternal.canonicalNodeId) + assert.equal(nodes.length, 1); assert.equal(aliases.length, 2); assert.equal(registryQueries.filter(sql => sql === "COMMIT").length, 2) + assert(!registryQueries.some(sql => /occupancy|bandwidth_attempt/.test(sql))) + cases.push("genuine signed operator/quiescence prerequisite and actual managed/external registry factories compose to one canonical node using inert SQL source") + + const abort = new AbortController(); abort.abort() + const before = requests + await assert.rejects(coordinator.acquire(external, { ...scope(), signal: abort.signal }), refused) + await assert.rejects(coordinator.acquire(external, { deadlineAt: Date.now() - 1 }), refused); assert.equal(requests, before) + const liveAbort = new AbortController(); await publish() + const revoked = await coordinator.acquire(external, { ...scope(), signal: liveAbort.signal }); liveAbort.abort() + await assert.rejects(coordinator.inspect(revoked, scope()), refused) + await publish(); const short = await coordinator.acquire(external, scope(250)) + await new Promise(done => setTimeout(done, 270)); await assert.rejects(coordinator.inspect(short, scope()), refused) + await publish(); const closed = await coordinator.acquire(external, scope()); coordinator.close() + await assert.rejects(coordinator.inspect(closed, scope()), refused); await assert.rejects(coordinator.acquire(external, scope()), refused) + cases.push("pre-cancellation/deadline, original signal cancellation, observation expiry and close refuse capability use") + assert.equal(databaseCalls, 0) + console.log(JSON.stringify({ ok: true, syntheticOnly: true, nativeLicenseGets: requests, groups: cases.length, cases, productionEnrollmentComplete: false, productionRegistryWrites: 0, syntheticRegistryRegistrations: 2, databaseCalls })) +} finally { + for (const item of coordinators) item.close() + pool.query = oldQuery; pool.connect = oldConnect; await closePool() + server.closeAllConnections(); server.close(); await once(server, "close") + await rm(folder, { recursive: true, force: true }) +} diff --git a/backend/src/services/bandwidth-operator-enrollment.ts b/backend/src/services/bandwidth-operator-enrollment.ts new file mode 100644 index 00000000..d7023431 --- /dev/null +++ b/backend/src/services/bandwidth-operator-enrollment.ts @@ -0,0 +1,222 @@ +/** Read-only operator enrollment prerequisite. No runtime default, SQL, issuer + * private key, native execution, alias registration or occupancy reset. The + * signed quiescence statement authenticates its operator, not a writer barrier. */ +import { createHash, createPublicKey, verify } from "node:crypto" +import { isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual, types } from "node:util" +import { z } from "zod" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { readBandwidthControlFile } from "./bandwidth-control-source.js" +import type { BandwidthNodeObservationScope } from "./bandwidth-node-observation.js" +import { createBandwidthPeerObservationReader, type BandwidthPeerObservation, type BandwidthPeerObservationEvidence, + type BandwidthPeerTrustedSource } from "./bandwidth-peer-observation.js" +import { parseBandwidthPeerRef, bandwidthPeerRefKey, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" + +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +const claim = z.object({ kind: z.enum(["system-id", "software-id"]), value: z.string().min(1).max(256) + .refine(value => Buffer.byteLength(value) <= 256 && value.trim() === value && !/[\u0000-\u001f\u007f]/.test(value)) }).strict() +/** An operator signs canonical UTF-8 JSON, never model-generated readiness. + * Replacing/removing the one current grant file revokes existing capabilities. */ +export const bandwidthOperatorEnrollmentGrantSchema = z.object({ + format: z.literal("mmapp-bandwidth-operator-enrollment-v1"), grantId: uuid, + operatorId: z.string().trim().min(1).max(200), installationId: uuid, issuerKeySha256: hash, + quiescenceProof: hash, executorInventorySha256: hash, executorsStoppedAt: z.number().int().nonnegative(), + issuedAt: z.number().int().nonnegative(), expiresAt: z.number().int().positive(), + peerRef: z.union([z.object({kind:z.literal("server"),serverId:z.number().int().positive().max(Number.MAX_SAFE_INTEGER)}).strict(), + z.object({kind:z.literal("external"),enrollmentId:uuid.regex(/^[a-f0-9-]+$/)}).strict()]), sourceProof: hash, + authorityGeneration: z.string().min(1).max(200), routingEndpointEpoch: uuid, + claims: z.array(claim).min(1).max(2).refine(value => value.length === 1 || value[0].kind === "system-id" && value[1].kind === "software-id"), +}).strict().refine(value => value.executorsStoppedAt <= value.issuedAt && value.issuedAt < value.expiresAt && value.expiresAt - value.issuedAt <= 600000) +type Grant = z.infer +/** Independent executor-fence authority, installed only by trusted operator + * composition. Must authenticate the live original fence and inventory on + * EVERY call. Signed grant fields, DB alias existence or network-write scope + * alone must never satisfy this interface. Absence/errors refuse admission. */ +export interface BandwidthOperatorQuiescenceVerifier { + verify(grant: Readonly, scope: Readonly): Promise +} +const envelope = z.object({ format: z.literal("mmapp-bandwidth-operator-enrollment-envelope-v1"), + payload: z.string().min(1).max(16000).regex(/^[A-Za-z0-9_-]+$/), signature: z.string().length(86).regex(/^[A-Za-z0-9_-]+$/) }).strict() +const configuration = z.object({ trustKeyFile: z.string(), trustKeySha256: hash, grantFile: z.string(), + installationId: uuid, issuerKeySha256: hash, quiescenceProof: hash }).strict() +export type BandwidthOperatorEnrollmentConfig = z.infer +declare const prerequisiteBrand: unique symbol +export interface BandwidthOperatorEnrollmentPrerequisite { + readonly kind: "operator-bandwidth-enrollment-prerequisite"; readonly [prerequisiteBrand]: true +} +/** PUBLIC metadata snapshot only. Its nested projection cannot substitute for + * the strong coordinator inspection, current Q, writer barrier or dispatch. */ +export interface BandwidthOperatorEnrollmentPublicMetadata { + readonly format: "mmapp-bandwidth-operator-enrollment-metadata-v1" + readonly metadata: Readonly<{ + observation: BandwidthPeerObservation; evidence: Readonly; operatorId: string; grantId: string; executorInventorySha256: string + authority: Readonly<{ installationId: string; issuerKeySha256: string; quiescenceProof: string; authorityProof: string; authorityGeneration: string; expiresAt: number }> + }> +} +export class BandwidthOperatorEnrollmentError extends Error { + readonly code = "BANDWIDTH_OPERATOR_ENROLLMENT_REFUSED" + constructor() { super("Исходный операторский enrollment и наблюдение лицензии не подтверждены") } +} +const refuse = (): never => { throw new BandwidthOperatorEnrollmentError() } +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +const coordinators = new WeakSet() +/** Server composition must receive this module's actual fixed reader, never a + * structural JSON/facade that can manufacture original operator authority. */ +export function assertBandwidthOperatorEnrollmentCoordinator(value: unknown): asserts value is ReturnType { + if (!value || typeof value !== "object" || !coordinators.has(value)) return refuse() +} +function captureScope(raw: BandwidthNodeObservationScope): Readonly { + if (!raw || typeof raw !== "object" || Object.getPrototypeOf(raw) !== Object.prototype + || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.keys(raw).some(key => !["deadlineAt", "signal"].includes(key)) + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(field => !("value" in field) || !field.enumerable) + || !Number.isSafeInteger(raw.deadlineAt) || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) return refuse() + return Object.freeze({ deadlineAt: raw.deadlineAt, signal: raw.signal }) +} + +/** Construction captures trusted source methods and immutable operator paths. + * No request can choose an endpoint, parser, signing key, SQL or transport. */ +export function createBandwidthOperatorEnrollmentPrerequisite(raw: BandwidthOperatorEnrollmentConfig, source: BandwidthPeerTrustedSource, + quiescence: BandwidthOperatorQuiescenceVerifier) { + const config = Object.freeze(configuration.parse(raw)) + if ([config.trustKeyFile, config.grantFile].some(path => !isAbsolute(path) || resolve(path) !== path) || config.trustKeyFile === config.grantFile) refuse() + if (!quiescence || typeof quiescence.verify !== "function") refuse() + const verifyQuiescence = quiescence.verify.bind(quiescence), reader = createBandwidthPeerObservationReader(source) + type Origin = { grant: Grant; observation: BandwidthPeerObservation; evidence: BandwidthPeerObservationEvidence; signal?: AbortSignal; end: number } + const origins = new WeakMap(), windows = new Set(), budgets = new Set<() => void>() + let closed = false + const operation = async (rawScope: BandwidthNodeObservationScope, origin: Origin | undefined, + run: (scope: Readonly, live: () => void) => Promise): Promise => { + const input = captureScope(rawScope) + const deadlineAt = Math.min(input.deadlineAt, Date.now() + 5000, origin?.grant.expiresAt ?? Infinity, origin?.evidence.expiresAt ?? Infinity) + const end = Math.min(performance.now() + Math.max(0, deadlineAt - Date.now()), origin?.end ?? Infinity) + const controller = new AbortController(), abort = () => controller.abort() + const live = () => { if (closed || controller.signal.aborted || input.signal?.aborted || origin?.signal?.aborted || Date.now() >= deadlineAt || performance.now() >= end) refuse() } + live(); windows.add(controller) + input.signal?.addEventListener("abort", abort, { once: true }); origin?.signal?.addEventListener("abort", abort, { once: true }) + const timer = setTimeout(abort, Math.max(1, end - performance.now())) + let stop!: () => void + const stopped = new Promise((_resolve, reject) => { stop = () => reject(new BandwidthOperatorEnrollmentError()); controller.signal.addEventListener("abort", stop, { once: true }) }) + try { const result = await Promise.race([Promise.resolve().then(() => { live(); return run(Object.freeze({ deadlineAt, signal: controller.signal }), live) }), stopped]); live(); return result } + catch { throw new BandwidthOperatorEnrollmentError() } + finally { clearTimeout(timer); input.signal?.removeEventListener("abort", abort); origin?.signal?.removeEventListener("abort", abort); controller.signal.removeEventListener("abort", stop); windows.delete(controller); controller.abort() } + } + async function current(peerRef: BandwidthPeerRef, live: () => void): Promise { + const [publicBytes, grantBytes] = await Promise.all([ + readBandwidthControlFile(config.trustKeyFile, 16384, live), + readBandwidthControlFile(config.grantFile, 32768, live), + ]); live() + const key = createPublicKey(publicBytes) + if (key.asymmetricKeyType !== "ed25519" || sha(key.export({ type: "spki", format: "der" })) !== config.trustKeySha256) refuse() + const signed = envelope.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(grantBytes))) + const payload = Buffer.from(signed.payload, "base64url"), signature = Buffer.from(signed.signature, "base64url") + if (payload.toString("base64url") !== signed.payload || signature.toString("base64url") !== signed.signature || !verify(null, payload, key, signature)) refuse() + const value = bandwidthOperatorEnrollmentGrantSchema.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(payload))) + if (canonicalBandwidthNodeValue(value) !== payload.toString("utf8") || bandwidthPeerRefKey(value.peerRef) !== bandwidthPeerRefKey(peerRef) || value.installationId !== config.installationId + || value.issuerKeySha256 !== config.issuerKeySha256 || value.quiescenceProof !== config.quiescenceProof || value.issuedAt > Date.now() || value.expiresAt <= Date.now()) refuse() + Object.freeze(value.peerRef); value.claims.forEach(Object.freeze); Object.freeze(value.claims) + live(); return Object.freeze(value) + } + const matches = (grant: Grant, evidence: BandwidthPeerObservationEvidence) => { + if (bandwidthPeerRefKey(grant.peerRef) !== bandwidthPeerRefKey(evidence.peerRef) || grant.sourceProof !== evidence.sourceProof || grant.authorityGeneration !== evidence.authorityGeneration + || grant.routingEndpointEpoch !== evidence.routingEndpointEpoch || !isDeepStrictEqual(grant.claims, evidence.claims)) refuse() + } + const coordinator = Object.freeze({ + async acquire(rawPeer: BandwidthPeerRef, rawScope: BandwidthNodeObservationScope): Promise { + const peerRef = parseBandwidthPeerRef(rawPeer), input = captureScope(rawScope) + // Anchor BOTH the scope and the not-yet-read signed grant to entry time, + // before any file IO. A backward wall jump must not renew either bound. + const enteredWall = Date.now(), enteredMono = performance.now() + let budgetEnd = enteredMono + Math.max(0, Math.min(5000, input.deadlineAt - enteredWall)), published = false + const budget = new AbortController() + let timer: ReturnType + const cancelBudget = () => { clearTimeout(timer); budget.abort(); budgets.delete(cancelBudget) } + const scheduleBudget = () => { clearTimeout(timer); timer = setTimeout(cancelBudget, Math.max(1, budgetEnd - performance.now())); timer.unref() } + const signal = input.signal ? AbortSignal.any([input.signal, budget.signal]) : budget.signal + budgets.add(cancelBudget); scheduleBudget() + try { return await operation({ ...input, signal }, undefined, async (scope, live) => { + const grant = await current(peerRef, live) + budgetEnd = Math.min(budgetEnd, enteredMono + Math.max(0, grant.expiresAt - enteredWall)); scheduleBudget() + const grantLive = () => { live(); if (signal.aborted || performance.now() >= budgetEnd || Date.now() >= grant.expiresAt) refuse() } + grantLive() + await verifyQuiescence(grant, scope); grantLive() + // Genuine native GET uses the fixed source, never signed response JSON. + // Preserve the ORIGINAL cancellation lifetime. The per-operation + // controller is disposed after acquire and must not revoke its origin. + const observation = await reader.observe(peerRef, { signal, deadlineAt: Math.min(scope.deadlineAt, grant.expiresAt) }); grantLive() + const evidence = await reader.inspect(observation, scope); grantLive(); matches(grant, evidence) + await verifyQuiescence(grant, scope); grantLive() + if (!isDeepStrictEqual(grant, await current(peerRef, grantLive))) refuse() + grantLive() + const capability = Object.freeze({ kind: "operator-bandwidth-enrollment-prerequisite" as const }) as BandwidthOperatorEnrollmentPrerequisite + origins.set(capability, { grant, observation, evidence, signal, end: budgetEnd }) + published = true + return capability + }) } finally { if (!published) cancelBudget() } + }, + async inspect(capability: unknown, input: BandwidthNodeObservationScope) { + const origin = capability && typeof capability === "object" ? origins.get(capability) : undefined + if (!origin) return refuse() + return operation(input, origin, async (scope, live) => { + // Authenticate the CURRENT original operator/fence before resolving + // management secrets, including inspection of an existing observation. + const before = await current(origin.grant.peerRef, live) + if (!isDeepStrictEqual(before, origin.grant)) refuse() + await verifyQuiescence(before, scope); live() + const evidence = await reader.inspect(origin.observation, scope); live() + if (!isDeepStrictEqual(evidence, origin.evidence)) refuse() + const grant = await current(evidence.peerRef, live) + if (!isDeepStrictEqual(grant, origin.grant)) refuse(); matches(grant, evidence) + await verifyQuiescence(grant, scope); live() + if (!isDeepStrictEqual(grant, await current(evidence.peerRef, live))) refuse() + return Object.freeze({ observation: origin.observation, evidence, operatorId: grant.operatorId, grantId: grant.grantId, + executorInventorySha256: grant.executorInventorySha256, + authority: Object.freeze({ installationId: grant.installationId, issuerKeySha256: grant.issuerKeySha256, quiescenceProof: grant.quiescenceProof, + authorityProof: sha(canonicalBandwidthNodeValue(grant)), authorityGeneration: grant.grantId, expiresAt: Math.min(grant.expiresAt, evidence.expiresAt) }), + writerBarrierVerified: false as const, endpointMappingVerified: false as const }) + }) + }, + async inspectMetadata(capability: unknown, rawScope: BandwidthNodeObservationScope): Promise> { + const enteredWall = Date.now(), enteredMono = performance.now() + const origin = capability && typeof capability === "object" ? origins.get(capability) : undefined + if (!origin || closed || !rawScope || types.isProxy(rawScope) || Object.getPrototypeOf(rawScope) !== Object.prototype) return refuse() + const fields = Object.getOwnPropertyDescriptors(rawScope) + if (Reflect.ownKeys(rawScope).some(key => typeof key !== "string" || !["deadlineAt", "signal"].includes(key)) + || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) return refuse() + const deadlineAt: unknown = fields.deadlineAt?.value, signal: unknown = fields.signal?.value + if (typeof deadlineAt !== "number" || !Number.isSafeInteger(deadlineAt) || signal !== undefined && (types.isProxy(signal) || !(signal instanceof AbortSignal))) return refuse() + const originalSignal = signal as AbortSignal | undefined, aborted = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "aborted")!.get! + const isAborted = () => originalSignal ? Reflect.apply(aborted, originalSignal, []) : false + try { if (isAborted()) return refuse() } catch { return refuse() } + const controller = new AbortController(), abort = () => controller.abort() + if (originalSignal) Reflect.apply(EventTarget.prototype.addEventListener, originalSignal, ["abort", abort, { once: true }]) + const end = Math.min(origin.end, enteredMono + Math.max(0, Math.min(5000, deadlineAt - enteredWall))) + try { + return await operation({ deadlineAt, signal: controller.signal }, origin, async (scope, live) => { + const currentLive = () => { live(); const now = Date.now(); if (isAborted() || now < enteredWall || performance.now() >= end) refuse() } + currentLive() + const before = await current(origin.grant.peerRef, currentLive) + if (!isDeepStrictEqual(before, origin.grant)) refuse() + // This new path never invokes Q, reads credentials, or repeats native + // observation. Missing fixed metadata checker MUST refuse in reader. + const inspected = await reader.inspectMetadata(origin.observation, scope); currentLive() + if (inspected.format !== "mmapp-bandwidth-peer-public-metadata-v1" || !isDeepStrictEqual(inspected.evidence, origin.evidence)) refuse() + const grant = await current(origin.grant.peerRef, currentLive) + if (!isDeepStrictEqual(grant, origin.grant)) refuse(); matches(grant, inspected.evidence); currentLive() + const authority = Object.freeze({ installationId: grant.installationId, issuerKeySha256: grant.issuerKeySha256, quiescenceProof: grant.quiescenceProof, + authorityProof: sha(canonicalBandwidthNodeValue(grant)), authorityGeneration: grant.grantId, expiresAt: Math.min(grant.expiresAt, inspected.evidence.expiresAt) }) + return Object.freeze({ format: "mmapp-bandwidth-operator-enrollment-metadata-v1" as const, + metadata: Object.freeze({ observation: origin.observation, evidence: inspected.evidence, operatorId: grant.operatorId, grantId: grant.grantId, + executorInventorySha256: grant.executorInventorySha256, authority }) }) + }) + } finally { + if (originalSignal) Reflect.apply(EventTarget.prototype.removeEventListener, originalSignal, ["abort", abort]) + controller.abort() + } + }, + close() { closed = true; reader.close(); for (const controller of windows) controller.abort(); for (const cancel of budgets) cancel() }, + }) + coordinators.add(coordinator) + return coordinator +} diff --git a/backend/src/services/bandwidth-operator-metadata.sql.test.ts b/backend/src/services/bandwidth-operator-metadata.sql.test.ts new file mode 100644 index 00000000..1ff67f48 --- /dev/null +++ b/backend/src/services/bandwidth-operator-metadata.sql.test.ts @@ -0,0 +1,164 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +/** Exclusive disposable PG + localhost license. Q is a synthetic fixture. */ +import assert from "node:assert/strict" +import { generateKeyPairSync, createHash, randomUUID, sign } from "node:crypto" +import { readFile, unlink, writeFile } from "node:fs/promises" +import { resolve } from "node:path" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" + +async function main() { + assertOwnedBandwidthSql() + assert.equal(process.env.MMAPP_OWN_OPERATOR_PEER_METADATA, "true") + const { createOwnedExternalRegistryHarness } = await import("./bandwidth-external-node-registry.sql-helper.js") + const { createBandwidthOperatorEnrollmentPrerequisite, bandwidthOperatorEnrollmentGrantSchema } = await import("./bandwidth-operator-enrollment.js") + const { createManagedBandwidthPeerMetadataChecker } = await import("./bandwidth-peer-metadata-source.js") + const { canonicalBandwidthNodeValue: canonical } = await import("./bandwidth-node-bootstrap.js") + const { pool } = await import("../db/index.js") + const fixture = await createOwnedExternalRegistryHarness() + let coordinator: ReturnType | undefined + let serverId: number | undefined + const checks: string[] = [] + try { + const claims = { "system-id": "OWNED-OPERATOR-METADATA", "software-id": "OWNED-PUBLIC-METADATA" } + serverId = await fixture.managedEndpoint(claims) + const peer = Object.freeze({ kind: "server" as const, serverId }) + const originalRow = (await fixture.applicationRows([serverId]))[0] + assert(originalRow) + const endpoint = { + host: String(originalRow.host), port: Number(originalRow.port), useSsl: originalRow.use_ssl as boolean, + verifySsl: originalRow.verify_ssl as boolean, username: String(originalRow.username), password: String(originalRow.password), + routingEndpointEpoch: String(originalRow.routing_endpoint_epoch), + } + const sourceProof = "d".repeat(64), authorityGeneration = "owned-operator-metadata-fixture" + const keys = generateKeyPairSync("ed25519"), now = Date.now() + let grant = bandwidthOperatorEnrollmentGrantSchema.parse({ + format: "mmapp-bandwidth-operator-enrollment-v1", ...fixture.root, grantId: randomUUID(), operatorId: "owned-metadata-operator", + executorInventorySha256: "c".repeat(64), executorsStoppedAt: now - 1000, issuedAt: now, expiresAt: now + 120000, + peerRef: peer, sourceProof, authorityGeneration, routingEndpointEpoch: endpoint.routingEndpointEpoch, + claims: Object.entries(claims).map(([kind, value]) => ({ kind, value })), + }) + const state = resolve(process.env.MCP_STATE_DIR!), trustKeyFile = resolve(state, "operator-metadata-trust.pem"), grantFile = resolve(state, "operator-metadata-grant.json") + const grantBytes = () => { + const bytes = Buffer.from(canonical(grant)) + return JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: bytes.toString("base64url"), signature: sign(null, bytes, keys.privateKey).toString("base64url") }) + } + await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" }), { flag: "wx", mode: 0o600 }) + await writeFile(grantFile, grantBytes(), { flag: "wx", mode: 0o600 }) + const checker = createManagedBandwidthPeerMetadataChecker() + let projections = 0, metadataChecks = 0, qChecks = 0, metadataPhase = false + const quiescence = { async verify(current: unknown) { qChecks++; assert(!metadataPhase, "metadata must not assert Q"); assert.deepEqual(current, grant) } } + coordinator = createBandwidthOperatorEnrollmentPrerequisite({ + trustKeyFile, grantFile, trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex"), ...fixture.root, + }, { + async read(requested, scope): Promise { + projections++; assert(!metadataPhase, "metadata must not reproject credentials"); assert.deepEqual(requested, peer) + await quiescence.verify(grant); scope.signal?.throwIfAborted() + const row = (await fixture.applicationRows([serverId!]))[0]; assert(row) + return { peerRef: peer, enabled: row.enabled as boolean, sourceProof, authorityGeneration, + endpoint: { host: String(row.host), port: Number(row.port), useSsl: row.use_ssl as boolean, verifySsl: row.verify_ssl as boolean, + username: String(row.username), password: String(row.password), routingEndpointEpoch: String(row.routing_endpoint_epoch) } } + }, + async assertMetadataCurrent(expected, scope) { metadataChecks++; await checker.assertMetadataCurrent(expected, scope) }, + }, quiescence) + console.log("Owned operator metadata stage:", "genuine-license-acquire") + const scope = () => ({ deadlineAt: Date.now() + 4500 }) + let prerequisite = await coordinator.acquire(peer, scope()) + assert(fixture.nativeGets() > 0); assert(projections > 0); assert(qChecks > 0) + metadataPhase = true + const inspect = async (capability: unknown = prerequisite) => { + const before = { projections, qChecks, gets: fixture.nativeGets(), posts: fixture.nativePosts() } + try { return await coordinator!.inspectMetadata(capability, scope()) } + finally { + assert.equal(projections, before.projections); assert.equal(qChecks, before.qChecks) + assert.equal(fixture.nativeGets(), before.gets); assert.equal(fixture.nativePosts(), before.posts) + } + } + const freshOrigin = async () => { + // The real endpoint trigger advances epoch on every edit, including an + // edit restoring old values. A fresh grant is required; never bypass it. + metadataPhase = false + const row = (await fixture.applicationRows([serverId!]))[0] + grant = bandwidthOperatorEnrollmentGrantSchema.parse({ ...grant, grantId: randomUUID(), routingEndpointEpoch: String(row.routing_endpoint_epoch) }) + await writeFile(grantFile, grantBytes()) + prerequisite = await coordinator!.acquire(peer, scope()) + metadataPhase = true + } + console.log("Owned operator metadata stage:", "current-managed-metadata") + const first = await inspect() + assert.equal(first.format, "mmapp-bandwidth-operator-enrollment-metadata-v1") + assert.deepEqual(first.metadata.evidence.peerRef, peer) + assert.deepEqual(first.metadata.evidence.claims, grant.claims) + assert(Object.isFrozen(first) && Object.isFrozen(first.metadata)) + const encoded = JSON.stringify(first) + assert(!encoded.includes(endpoint.password) && !encoded.includes(endpoint.username)) + assert(!("writerBarrierVerified" in first) && !("quiescenceVerified" in first)) + assert.deepEqual(await inspect(), first) + assert.equal(metadataChecks, 2) + checks.push("genuine signed operator/license origin plus actual fixed SQL current endpoint predicate, public metadata only; no Q/native/credential re-projection") + + console.log("Owned operator metadata stage:", "private-predicate-with-unchanged-epoch") + const wrongEndpoints = [ + { ...endpoint, password: "owned-wrong-private-predicate" }, + { ...endpoint, username: "owned-wrong-user-predicate" }, + { ...endpoint, host: "127.0.0.2" }, + { ...endpoint, port: endpoint.port === 1 ? 2 : 1 }, + { ...endpoint, useSsl: !endpoint.useSsl }, + { ...endpoint, verifySsl: !endpoint.verifySsl }, + ] + for (const wrong of wrongEndpoints) { + assert.equal(wrong.routingEndpointEpoch, endpoint.routingEndpointEpoch) + await assert.rejects(checker.assertMetadataCurrent({ peerRef: peer, enabled: true, sourceProof, authorityGeneration, endpoint: wrong }, scope())) + } + assert.equal(String((await fixture.applicationRows([serverId]))[0].routing_endpoint_epoch), endpoint.routingEndpointEpoch) + checks.push("actual SQL rejects wrong private password/username/host/port/TLS predicates while the real row and endpoint epoch remain unchanged; epoch alone is insufficient") + + console.log("Owned operator metadata stage:", "endpoint-field-drift") + const faults = [ + { sql: "UPDATE public.servers SET password=$2 WHERE id=$1", next: "owned-changed-password-only", previous: endpoint.password }, + { sql: "UPDATE public.servers SET username=$2 WHERE id=$1", next: "owned-changed-username", previous: endpoint.username }, + { sql: "UPDATE public.servers SET host=$2 WHERE id=$1", next: "127.0.0.2", previous: endpoint.host }, + { sql: "UPDATE public.servers SET port=$2 WHERE id=$1", next: endpoint.port === 1 ? 2 : 1, previous: endpoint.port }, + { sql: "UPDATE public.servers SET use_ssl=$2 WHERE id=$1", next: !endpoint.useSsl, previous: endpoint.useSsl }, + { sql: "UPDATE public.servers SET verify_ssl=$2 WHERE id=$1", next: !endpoint.verifySsl, previous: endpoint.verifySsl }, + { sql: "UPDATE public.servers SET enabled=$2 WHERE id=$1", next: false, previous: true }, + ] + for (const fault of faults) { + await freshOrigin() + try { + assert.equal((await pool.query(fault.sql, [serverId, fault.next])).rowCount, 1) + assert.notEqual(String((await fixture.applicationRows([serverId]))[0].routing_endpoint_epoch), grant.routingEndpointEpoch) + await assert.rejects(inspect()) + } + finally { await pool.query(fault.sql, [serverId, fault.previous]) } + await assert.rejects(inspect()) + } + await freshOrigin(); await inspect() + checks.push("actual password, username, host, port, TLS, verification and enabled drift refuse; real trigger advances endpoint epoch and restored values never revive the old origin; no trigger bypass") + + console.log("Owned operator metadata stage:", "grant-and-private-origin") + const beforeInvalid = metadataChecks, savedGrant = await readFile(grantFile) + try { await unlink(grantFile); await assert.rejects(inspect()) } + finally { await writeFile(grantFile, savedGrant) } + await assert.rejects(inspect(JSON.parse(JSON.stringify(prerequisite)))) + assert.equal(metadataChecks, beforeInvalid) + checks.push("deleted current signed grant and serialized prerequisite refuse before metadata SQL; exact grant bytes restored") + const restored = (await fixture.applicationRows([serverId]))[0] + const { routing_endpoint_epoch: oldEpoch, ...originalFields } = originalRow + const { routing_endpoint_epoch: newEpoch, ...restoredFields } = restored + assert.deepEqual(restoredFields, originalFields); assert.notEqual(newEpoch, oldEpoch) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + console.log("Owned operator peer metadata accepted:", JSON.stringify({ + groups: checks.length, checks, actualSql: true, genuineLocalhostLicenseAcquire: true, + coordinatorMetadataChecks: metadataChecks, unchangedEpochPredicateRejections: wrongEndpoints.length, + metadataCredentialProjections: 0, metadataQChecks: 0, metadataNativeCalls: 0, + externalMetadataAccepted: false, operatorWindowAccepted: false, productionWired: false, + })) + } finally { + coordinator?.close() + if (serverId !== undefined) await pool.query("DELETE FROM public.servers WHERE id=$1", [serverId]) + await fixture.close() + } +} + +void main().catch(() => { console.error("Owned operator peer metadata acceptance failed; no real CHR/Docker actions"); process.exitCode = 1 }) diff --git a/backend/src/services/bandwidth-operator-metadata.test.ts b/backend/src/services/bandwidth-operator-metadata.test.ts new file mode 100644 index 00000000..633bfbae --- /dev/null +++ b/backend/src/services/bandwidth-operator-metadata.test.ts @@ -0,0 +1,152 @@ +/** Genuine coordinator prerequisite, real Ed25519 grant and authenticated + * localhost license GET. Metadata checker is explicit controlled source state, + * not production SQL/Q or public snapshot admission authority. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { once } from "node:events" +import { createServer } from "node:http" +import { lstat, mkdir, mkdtemp, realpath, rm, unlink, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, join } from "node:path" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { createBandwidthOperatorEnrollmentPrerequisite, bandwidthOperatorEnrollmentGrantSchema, + type BandwidthOperatorEnrollmentPublicMetadata } from "./bandwidth-operator-enrollment.js" +import type { BandwidthPeerSourceSnapshot, BandwidthPeerTrustedSource } from "./bandwidth-peer-observation.js" +import type { BandwidthNodeObservationScope } from "./bandwidth-node-observation.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" + +type Coordinator = ReturnType +function typeSeparation(value: BandwidthOperatorEnrollmentPublicMetadata) { + // @ts-expect-error PUBLIC outer wrapper is not a strong current-Q inspection. + const strong: Awaited> = value + return strong +} +void typeSeparation + +test("operator PUBLIC metadata authenticates current original grant without Q/credentials/native", async t => { + let nativeGets = 0 + const server = createServer((request, response) => { + nativeGets++; assert.equal(request.method, "GET"); assert.equal(request.url, "/rest/system/license") + assert.equal(request.headers.authorization, "Basic " + Buffer.from("metadata-user:metadata-private-password").toString("base64")) + response.writeHead(200, { "content-type": "application/json" }); response.end(JSON.stringify({ "system-id": "SYNTHETIC-OPERATOR-METADATA" })) + }) + server.listen(0, "127.0.0.1"); await once(server, "listening") + const address = server.address(); assert(address && typeof address !== "string") + const port = address.port + const root = await realpath(tmpdir()), folder = await mkdtemp(join(root, "bw-operator-metadata-")) + const all: Coordinator[] = [] + let ordinal = 0 + const scope = (ms = 4000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + ms, ...(signal ? { signal } : {}) }) + async function fixture(checker = true) { + const directory = join(folder, String(++ordinal)), keys = generateKeyPairSync("ed25519"), peerRef = { kind: "server" as const, serverId: 71 } + await mkdir(directory) + const trustKeyFile = join(directory, "operator.pub"), grantFile = join(directory, "grant.json") + await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" })) + const config = { trustKeyFile, grantFile, trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex"), + installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } + let currentSource: BandwidthPeerSourceSnapshot = { peerRef, enabled: true, sourceProof: "c".repeat(64), authorityGeneration: "original-source", + endpoint: { host: "127.0.0.1", port, username: "metadata-user", password: "metadata-private-password", useSsl: false, verifySsl: true, routingEndpointEpoch: randomUUID() } } + const expectedSource = structuredClone(currentSource) + let reads = 0, q = 0, checks = 0, hook: (() => Promise) | undefined + const trusted: BandwidthPeerTrustedSource = { async read() { reads++; return currentSource }, + ...(checker ? { async assertMetadataCurrent(expected: Readonly, requested: Readonly) { + checks++; assert.deepEqual(expected, expectedSource); assert(Object.isFrozen(expected) && Object.isFrozen(expected.endpoint)) + await hook?.(); requested.signal?.throwIfAborted(); assert.deepEqual(currentSource, expected) + } } : {}) } + const coordinator = createBandwidthOperatorEnrollmentPrerequisite(config, trusted, { async verify() { q++ } }); all.push(coordinator) + // Build only the exact signed descriptor (configuration paths are private). + const grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", grantId: randomUUID(), operatorId: "original-metadata-operator", + installationId: config.installationId, issuerKeySha256: config.issuerKeySha256, quiescenceProof: config.quiescenceProof, + executorInventorySha256: "d".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000, + peerRef, sourceProof: expectedSource.sourceProof, authorityGeneration: expectedSource.authorityGeneration, routingEndpointEpoch: expectedSource.endpoint.routingEndpointEpoch, + claims: [{ kind: "system-id", value: "SYNTHETIC-OPERATOR-METADATA" }] }) + const publish = async (value = grant) => { const payload = Buffer.from(canonicalBandwidthNodeValue(value)); await writeFile(grantFile, JSON.stringify({ + format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) } + await publish() + return { coordinator, trusted, grantFile, trustKeyFile, peerRef, grant, publish, + reset() { reads = q = checks = nativeGets = 0 }, counts: () => ({ reads, q, checks, nativeGets }), + hook(value: typeof hook) { hook = value }, drift() { currentSource = { ...currentSource, endpoint: { ...currentSource.endpoint, password: "changed" } } } } + } + try { + await t.test("genuine acquire needs Q/license; PUBLIC metadata then uses only private checker and signed files", async () => { + const f = await fixture(), token = await f.coordinator.acquire(f.peerRef, scope()) + assert(f.counts().q > 0 && f.counts().reads > 0 && f.counts().nativeGets > 0); f.reset() + const value = await f.coordinator.inspectMetadata(token, scope()) + assert.equal(value.format, "mmapp-bandwidth-operator-enrollment-metadata-v1"); assert.equal(value.metadata.grantId, f.grant.grantId) + assert(Object.isFrozen(value) && Object.isFrozen(value.metadata) && Object.isFrozen(value.metadata.authority)) + assert(!("writerBarrierVerified" in value) && !("writerBarrierVerified" in value.metadata) && !("endpointMappingVerified" in value.metadata)) + assert(!JSON.stringify(value).includes("metadata-private-password")); assert(!JSON.stringify(value).includes("metadata-user")) + assert.deepEqual(f.counts(), { reads: 0, q: 0, checks: 1, nativeGets: 0 }) + }) + await t.test("constructor captures checker; absent checker refuses with no credential/native/Q fallback", async () => { + const f = await fixture(), token = await f.coordinator.acquire(f.peerRef, scope()); f.reset() + f.trusted.assertMetadataCurrent = async () => { throw Error("replacement") } + await f.coordinator.inspectMetadata(token, scope()); assert.deepEqual(f.counts(), { reads: 0, q: 0, checks: 1, nativeGets: 0 }) + const missing = await fixture(false), absentToken = await missing.coordinator.acquire(missing.peerRef, scope()); missing.reset() + await assert.rejects(missing.coordinator.inspectMetadata(absentToken, scope())); assert.deepEqual(missing.counts(), { reads: 0, q: 0, checks: 0, nativeGets: 0 }) + }) + await t.test("foreign/copied prerequisites and revoked original grant refuse before checker", async () => { + const f = await fixture(), token = await f.coordinator.acquire(f.peerRef, scope()), foreign = await fixture(); f.reset(); foreign.reset() + for (const copied of [{ ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(f.coordinator.inspectMetadata(copied, scope())) + await assert.rejects(foreign.coordinator.inspectMetadata(token, scope())) + await f.publish({ ...f.grant, grantId: randomUUID() }); await assert.rejects(f.coordinator.inspectMetadata(token, scope())) + assert.deepEqual(f.counts(), { reads: 0, q: 0, checks: 0, nativeGets: 0 }) + }) + await t.test("grant rotation/deletion/key change during checker and endpoint drift reject without fallback", async () => { + for (const fault of ["rotate", "delete", "key", "endpoint"] as const) { + const f = await fixture(), token = await f.coordinator.acquire(f.peerRef, scope()); f.reset() + f.hook(async () => { if (fault === "rotate") await f.publish({ ...f.grant, grantId: randomUUID() }); if (fault === "delete") await unlink(f.grantFile) + if (fault === "key") await writeFile(f.trustKeyFile, generateKeyPairSync("ed25519").publicKey.export({ type: "spki", format: "pem" })); if (fault === "endpoint") f.drift() }) + await assert.rejects(f.coordinator.inspectMetadata(token, scope())); assert.deepEqual(f.counts(), { reads: 0, q: 0, checks: 1, nativeGets: 0 }) + } + }) + await t.test("original abort/expiry cannot renew with larger deadline/new signal; new shorter call remains supported", async () => { + const f = await fixture(), caller = new AbortController(), token = await f.coordinator.acquire(f.peerRef, scope(4000, caller.signal)); f.reset() + await f.coordinator.inspectMetadata(token, scope(1000)); caller.abort() + await assert.rejects(f.coordinator.inspectMetadata(token, scope(60000, new AbortController().signal))); assert.equal(f.counts().checks, 1) + const g = await fixture(), short = await g.coordinator.acquire(g.peerRef, scope(700)); g.reset(); await delay(750) + await assert.rejects(g.coordinator.inspectMetadata(short, scope(60000))); assert.deepEqual(g.counts(), { reads: 0, q: 0, checks: 0, nativeGets: 0 }) + }) + await t.test("caller mutation/abort or coordinator.close during pending checker prevents late publication", async () => { + for (const cancel of ["abort", "close"] as const) { + const f = await fixture(), token = await f.coordinator.acquire(f.peerRef, scope()); f.reset() + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + f.hook(async () => { entered(); await held }) + const caller = new AbortController(), input = scope(3000, caller.signal), pending = f.coordinator.inspectMetadata(token, input); void pending.catch(() => {}) + await started; input.deadlineAt += 60000; input.signal = new AbortController().signal + if (cancel === "abort") caller.abort(); else f.coordinator.close() + await assert.rejects(pending); release(); await delay(0); assert.deepEqual(f.counts(), { reads: 0, q: 0, checks: 1, nativeGets: 0 }) + } + }) + await t.test("scope proxy/getter rejected without user code; native signal overrides ignored", async () => { + const f = await fixture(), token = await f.coordinator.acquire(f.peerRef, scope()); f.reset(); let traps = 0 + const trap = () => { traps++; throw Error("No caller trap") } + await assert.rejects(f.coordinator.inspectMetadata(token, new Proxy(scope(), { getPrototypeOf: trap, ownKeys: trap }))) + await assert.rejects(f.coordinator.inspectMetadata(token, { get deadlineAt() { return trap() } })); assert.equal(traps, 0) + const caller = new AbortController(); Object.defineProperties(caller.signal, { aborted: { get: trap }, addEventListener: { value: trap }, removeEventListener: { value: trap } }) + await f.coordinator.inspectMetadata(token, scope(1000, caller.signal)); assert.equal(traps, 0) + assert.deepEqual(f.counts(), { reads: 0, q: 0, checks: 1, nativeGets: 0 }) + }) + await t.test("short call's monotonic horizon survives caller mutation and wall rollback during checker", async () => { + const f = await fixture(), token = await f.coordinator.acquire(f.peerRef, scope()); f.reset() + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + f.hook(async () => { entered(); await held }) + const originalNow = Date.now, input = scope(200), pending = f.coordinator.inspectMetadata(token, input); void pending.catch(() => {}) + try { + await Promise.race([started, pending.then(() => assert.fail("checker was not reached"))]) + input.deadlineAt = originalNow() + 60000; Date.now = () => originalNow() - 60000 + await assert.rejects(pending) + } finally { Date.now = originalNow; release(); await delay(0) } + assert.deepEqual(f.counts(), { reads: 0, q: 0, checks: 1, nativeGets: 0 }) + }) + } finally { + for (const coordinator of all) coordinator.close() + server.closeAllConnections(); server.close(); await once(server, "close") + const { closePool } = await import("../db/index.js"); await closePool() + const actual = await realpath(folder); assert.equal(dirname(actual), root); assert.match(basename(actual), /^bw-operator-metadata-/); assert((await lstat(folder)).isDirectory()) + await rm(actual, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/bandwidth-operator-peer-source-composition.test.ts b/backend/src/services/bandwidth-operator-peer-source-composition.test.ts new file mode 100644 index 00000000..6c957168 --- /dev/null +++ b/backend/src/services/bandwidth-operator-peer-source-composition.test.ts @@ -0,0 +1,99 @@ +/** Real signed original/window/guard/coordinator + localhost license GET. + * Registry and SQL session are explicit inert models; no PG/Docker/Q proof. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { once } from "node:events" +import { createServer } from "node:http" +import { mkdtemp, realpath, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { dirname, join } from "node:path" +import { mock, test } from "node:test" +import type { createBandwidthOperatorRegistry } from "./bandwidth-operator-registry.js" +import { assertBandwidthOperatorPeerSourceGuard, type BandwidthOperatorPeerSourceGuard } from "./bandwidth-operator-writer-window.js" +import { bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import type { BandwidthOperatorWriterCompositionDependencies } from "./bandwidth-operator-writer-composition.js" + +type RegistryDependencies = Parameters[0] +let counters = () => ({ q: 0, sql: 0 }) +mock.module("./bandwidth-operator-registry.js", { namedExports: { + createBandwidthOperatorRegistry(deps: RegistryDependencies) { + return { async register(token: unknown, scope: { deadlineAt: number }) { + const check = async () => { await deps.coordinator.inspect(token, scope) } + const borrowed = await deps.source.open(scope, check) + const before = counters(), metadata = await deps.coordinator.inspectMetadata(token, scope) + assert.equal(metadata.format, "mmapp-bandwidth-operator-enrollment-metadata-v1"); assert.deepEqual(counters(), before) + await borrowed.connection.query("BEGIN"); await borrowed.connection.query("COMMIT"); borrowed.connection.release(true) + return { enrollmentId: "synthetic", canonicalNodeId: randomUUID(), incarnation: randomUUID(), status: "active", outcome: "new" } + } } + }, +} }) + +test("constructor factory gets genuine guard only inside held registry window", async () => { + const root = await realpath(tmpdir()), folder = await mkdtemp(join(root, "bw-peer-source-compose-")), keys = generateKeyPairSync("ed25519") + let nativeGets = 0, factoryCalls = 0, sourceCloses = 0, q = 0, sql = 0, held = false, physicalCloses = 0 + const events: string[] = [] + const server = createServer((request, response) => { + nativeGets++; assert.equal(request.url, "/rest/system/license"); assert.equal(request.method, "GET") + assert.equal(request.headers.authorization, "Basic " + Buffer.from("synthetic-compose:synthetic-private").toString("base64")) + response.writeHead(200, { "content-type": "application/json" }); response.end('{"system-id":"SYNTHETIC-COMPOSE"}') + }) + server.listen(0, "127.0.0.1"); await once(server, "listening") + const address = server.address(); assert(address && typeof address !== "string") + const trustKeyFile = join(folder, "operator.pub"), grantFile = join(folder, "grant.json"), peerRef = { kind: "external" as const, enrollmentId: randomUUID() } + const rootFields = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } + const expected: BandwidthPeerSourceSnapshot = { peerRef, enabled: true, sourceProof: "c".repeat(64), authorityGeneration: "original-compose-source", endpoint: { + host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "synthetic-compose", password: "synthetic-private", routingEndpointEpoch: randomUUID() } } + const grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", ...rootFields, grantId: randomUUID(), operatorId: "synthetic-compose", + executorInventorySha256: "d".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000, + peerRef, sourceProof: expected.sourceProof, authorityGeneration: expected.authorityGeneration, routingEndpointEpoch: expected.endpoint.routingEndpointEpoch, + claims: [{ kind: "system-id", value: "SYNTHETIC-COMPOSE" }] }) + const binding = { peerRef, sourceProof: expected.sourceProof, authorityGeneration: expected.authorityGeneration, routingEndpointEpoch: expected.endpoint.routingEndpointEpoch } + const payload = Buffer.from(canonicalBandwidthNodeValue(grant)); await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" })) + await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) + const config = { ...rootFields, trustKeyFile, grantFile, trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex") } + let retained: BandwidthOperatorPeerSourceGuard | undefined + const sourceOpen = async (_scope: unknown, guard: () => Promise) => { + events.push("open"); await guard() + let closed = false + const lifetime = new AbortController() + return { async binding() { await guard(); return rootFields }, async keyedProof() { throw Error("No key in composition fixture") }, + close() { if (!closed) { closed = true; lifetime.abort(); held = false; physicalCloses++ } }, connection: { + lifetimeSignal: lifetime.signal, + release() { throw Error("Owner closes physical session") }, async query(text: string) { + await guard(); sql++; events.push(text) + if (text.includes("pg_try_advisory_lock")) { held = true; return { rows: [{ acquired: true }] } } + if (text.includes("pg_locks")) return { rows: [{ held, backend_pid: 777 }] } + if (text.includes("pg_backend_pid()")) return { rows: [{ backend_pid: 777 }] } + return { rows: [], command: text } + }, + } } + } + const dependencies: BandwidthOperatorWriterCompositionDependencies = { config, nodeSource: { open: sourceOpen, openWithOwnershipFence: sourceOpen } as never, + externalStop: { async acquire() { return { async assertCurrent() { q++ }, close() {} } } }, + createObservationSource(guard) { + factoryCalls++; events.push("factory"); assert(held); assertBandwidthOperatorPeerSourceGuard(guard); retained = guard + assert(events.some(event => event.includes("pg_backend_pid()"))) + return { async read(_peer, requested) { await guard.verifyRead(binding, requested); return expected }, + async assertMetadataCurrent(privateExpected, scope) { await guard.verifyMetadata(binding, scope); assert.deepEqual(privateExpected, expected) }, close() { sourceCloses++ } } + } } + const { createBandwidthOperatorWriterComposition } = await import("./bandwidth-operator-writer-composition.js") + const composition = createBandwidthOperatorWriterComposition(dependencies) + counters = () => ({ q, sql }) + try { + assert.equal(factoryCalls + nativeGets + sql + q, 0) + assert.throws(() => createBandwidthOperatorWriterComposition({ ...dependencies, observationSource: { async read() { return expected } } })) + assert.throws(() => createBandwidthOperatorWriterComposition({ config, nodeSource: dependencies.nodeSource, externalStop: dependencies.externalStop })) + dependencies.createObservationSource = () => { throw Error("Replacement callback must not run") } + await assert.rejects(composition.register({ deadlineAt: Date.now() + 4000, createObservationSource: () => expected } as never)); assert.equal(factoryCalls, 0) + await composition.register({ deadlineAt: Date.now() + 4000 }) + assert.equal(factoryCalls, 1); assert.equal(sourceCloses, 1); assert.equal(nativeGets, 1); assert.equal(physicalCloses, 1) + assert(events.indexOf("factory") > events.findIndex(event => event.includes("pg_try_advisory_lock"))) + assert(retained); assert.throws(() => assertBandwidthOperatorPeerSourceGuard(retained)) + } finally { + composition.close(); server.closeAllConnections(); server.close(); await once(server, "close") + const { closePool } = await import("../db/index.js"); await closePool() + assert.equal(dirname(await realpath(folder)), root); await rm(folder, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/bandwidth-operator-peer-source-guard.test.ts b/backend/src/services/bandwidth-operator-peer-source-guard.test.ts new file mode 100644 index 00000000..e4466287 --- /dev/null +++ b/backend/src/services/bandwidth-operator-peer-source-guard.test.ts @@ -0,0 +1,184 @@ +/** Genuine signed original reader/window/guard. SQL session ownership and Q + * are explicit inert fixture models, NOT actual PostgreSQL/Docker enforcement. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { lstat, mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, join } from "node:path" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { assertBandwidthOperatorPeerSourceGuard, createBandwidthOperatorExternalFencer, createBandwidthOperatorOriginalAuthorityReader, + createBandwidthOperatorWriterWindows, type BandwidthOperatorPeerSourceBinding, type BandwidthOperatorPeerSourceGuard, + type BandwidthOperatorRegistryBorrow, type BandwidthOperatorScope } from "./bandwidth-operator-writer-window.js" + +const scope = (ms = 4000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + ms, ...(signal ? { signal } : {}) }) +test("genuine operator peer source guard binds original held window", async t => { + const root = await realpath(tmpdir()), folder = await mkdtemp(join(root, "bw-peer-source-guard-")), cleanup: Array<() => void> = [] + let ordinal = 0, lifetimeGetters = 0 + async function fixture(ttl = 4000, lifetimeMode: "normal" | "missing" | "hostile" | "accessor" | "proxy" | "aborted" = "normal") { + const directory = join(folder, String(++ordinal)); await mkdir(directory) + const keys = generateKeyPairSync("ed25519"), trustKeyFile = join(directory, "operator.pub"), grantFile = join(directory, "grant.json") + await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" })) + const rootFields = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } + const grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", ...rootFields, grantId: randomUUID(), operatorId: "synthetic-peer-guard", + executorInventorySha256: "c".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000, + peerRef: { kind: "external", enrollmentId: randomUUID() }, sourceProof: "d".repeat(64), authorityGeneration: "synthetic-original-source", routingEndpointEpoch: randomUUID(), + claims: [{ kind: "system-id", value: "SYNTHETIC-PEER-GUARD" }] }) + const publish = async (value = grant) => { const payload = Buffer.from(canonicalBandwidthNodeValue(value)); await writeFile(grantFile, JSON.stringify({ + format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) } + await publish() + const originals = createBandwidthOperatorOriginalAuthorityReader({ ...rootFields, trustKeyFile, grantFile, + trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex") }) + let q = 0, queries = 0, heldQueries = 0, opens = 0, destroys = 0, externalCloses = 0, secrets = 0, held = false, pid = 777, qAllowed = true, physicalClosed = false, closing = false + const leaseLifetime = new AbortController() + if (lifetimeMode === "hostile") Object.defineProperties(leaseLifetime.signal, { + aborted: { get() { lifetimeGetters++; throw Error("No own getter") } }, + addEventListener: { value() { lifetimeGetters++; throw Error("No own method") } }, + removeEventListener: { value() { lifetimeGetters++; throw Error("No own method") } }, + }) + if (lifetimeMode === "aborted") leaseLifetime.abort() + let heldHook: (() => Promise) | undefined, qHook: (() => Promise) | undefined + let registerHook: ((borrowed: BandwidthOperatorRegistryBorrow, requested: BandwidthOperatorScope) => Promise) | undefined + const caller = new AbortController(), originalScope = scope(ttl, caller.signal), authority = await originals.acquire(originalScope) + const external = createBandwidthOperatorExternalFencer({ async acquire() { return { + async assertCurrent() { q++; await qHook?.(); if (!qAllowed) throw Error("synthetic Q revoked") }, close() { externalCloses++ }, + } } }) + const windows = createBandwidthOperatorWriterWindows({ originals, external, source: { + async open(_requested, guard) { + opens++; await guard() + const connection = { + release() { throw Error("Owner-only physical close") }, async query(text: string) { + assert(!physicalClosed); await guard(); queries++ + if (text.includes("pg_try_advisory_lock")) { held = true; return { rows: [{ acquired: true }] } } + if (text.includes("pg_locks")) { heldQueries++; await heldHook?.(); return { rows: [{ held, backend_pid: pid }] } } + if (text.includes("pg_backend_pid()")) return { rows: [{ backend_pid: pid }] } + if (["BEGIN", "COMMIT"].includes(text)) return { rows: [], command: text } + throw Error("Unexpected fixed fixture SQL") + }, + } + if (lifetimeMode !== "missing") Object.defineProperty(connection, "lifetimeSignal", lifetimeMode === "accessor" + ? { enumerable: true, get() { lifetimeGetters++; return leaseLifetime.signal } } + : { enumerable: true, value: lifetimeMode === "proxy" ? new Proxy(leaseLifetime.signal, { getPrototypeOf() { lifetimeGetters++; throw Error("No proxy trap") } }) : leaseLifetime.signal }) + return { async binding() { await guard(); return rootFields }, async keyedProof() { secrets++; throw Error("No secret MAC permitted") }, + close() { if (!closing) { closing = true; leaseLifetime.abort(); physicalClosed = true; held = false; destroys++ } }, connection } + }, + }, registry: { async register(_original, borrowed, requested) { await registerHook?.(borrowed, requested); await borrowed.connection.query("BEGIN"); + await borrowed.connection.query("COMMIT"); borrowed.connection.release(true); return "synthetic-registry-complete" } } }) + cleanup.push(() => { windows.close(); originals.close() }) + const window = await windows.open(authority, originalScope) + const binding: BandwidthOperatorPeerSourceBinding = { peerRef: grant.peerRef, sourceProof: grant.sourceProof, authorityGeneration: grant.authorityGeneration, routingEndpointEpoch: grant.routingEndpointEpoch } + return { windows, originals, authority, window, binding, grant, publish, caller, + guard: () => windows.createPeerSourceGuard(window, authority), reset() { q = queries = heldQueries = 0 }, + counts: () => ({ q, queries, heldQueries, opens, destroys, externalCloses, secrets }), + loseLock() { held = false }, loseOwner() { leaseLifetime.abort() }, changePid() { pid++ }, revokeQ() { qAllowed = false }, heldHook(value: typeof heldHook) { heldHook = value }, + qHook(value: typeof qHook) { qHook = value }, registerHook(value: typeof registerHook) { registerHook = value } } + } + try { + await t.test("brand rejects flags/JSON/copies; genuine read checks actual same fixture lease/PID with Q", async () => { + const f = await fixture(), guard = f.guard(); f.reset() + assertBandwidthOperatorPeerSourceGuard(guard) + assert.equal(guard.installationId, f.grant.installationId) + assert.deepEqual(Object.keys(guard).sort(), ["installationId", "verifyMetadata", "verifyRead"]) + const installation = Object.getOwnPropertyDescriptor(guard, "installationId")! + assert.equal(installation.value, f.grant.installationId); assert.equal(installation.writable, false); assert.equal(installation.configurable, false); assert(!installation.get) + for (const fake of [{ verifyRead: async () => {}, verifyMetadata: async () => {} }, { ...guard }, JSON.parse(JSON.stringify(guard)), { ready: true }]) assert.throws(() => assertBandwidthOperatorPeerSourceGuard(fake)) + await guard.verifyRead(f.binding, scope()); assert.equal(f.counts().heldQueries, 1); assert(f.counts().q > 0) + assert.equal(f.counts().opens, 1); assert.equal(f.counts().secrets, 0) + }) + await t.test("metadata checks signed original only: zero Q/SQL/secret, never a held/Q proof", async () => { + const f = await fixture(), guard = f.guard(); f.reset(); f.revokeQ(); f.loseLock() + await guard.verifyMetadata(f.binding, scope()); assert.equal(f.counts().q + f.counts().queries + f.counts().secrets, 0) + await assert.rejects(guard.verifyRead(f.binding, scope())); assert.equal(f.counts().destroys, 1) + }) + await t.test("missing/copied window or foreign original cannot mint a genuine guard", async () => { + const f = await fixture(); f.reset() + assert.throws(() => f.windows.createPeerSourceGuard({}, f.authority)); assert.throws(() => f.windows.createPeerSourceGuard({ ...f.window }, f.authority)) + const foreign = await f.originals.acquire(scope()) + assert.throws(() => f.windows.createPeerSourceGuard(f.window, foreign)); assert.equal(f.counts().destroys, 1); assert.equal(f.counts().queries, 0) + }) + await t.test("tuple drift/proxies/accessors fail before any secret or held SQL and revoke all window guards", async () => { + for (const fault of ["peer", "proof", "generation", "epoch", "accessor", "proxy"] as const) { + const f = await fixture(), guard = f.guard(), other = f.guard(); f.reset(); let getters = 0 + let binding = structuredClone(f.binding) + if (fault === "peer") binding = { ...binding, peerRef: { kind: "external", enrollmentId: randomUUID() } } + if (fault === "proof") binding = { ...binding, sourceProof: "9".repeat(64) } + if (fault === "generation") binding = { ...binding, authorityGeneration: "different" } + if (fault === "epoch") binding = { ...binding, routingEndpointEpoch: randomUUID() } + if (fault === "accessor") Object.defineProperty(binding, "sourceProof", { enumerable: true, get() { getters++; return f.binding.sourceProof } }) + if (fault === "proxy") binding = new Proxy(binding, { getPrototypeOf() { getters++; throw Error("No proxy trap") } }) + await assert.rejects(guard.verifyRead(binding, scope())); assert.equal(getters, 0) + assert.equal(f.counts().q + f.counts().queries + f.counts().secrets, 0); assert.equal(f.counts().destroys, 1) + assert.throws(() => assertBandwidthOperatorPeerSourceGuard(other)) + } + }) + await t.test("held lock/PID drift and original grant rotation during Q deny the read", async () => { + for (const fault of ["lock", "pid", "grant"] as const) { + const f = await fixture(), guard = f.guard(); f.reset() + if (fault === "lock") f.loseLock() + if (fault === "pid") f.changePid() + if (fault === "grant") { let done = false; f.qHook(async () => { if (!done) { done = true; await f.publish({ ...f.grant, grantId: randomUUID() }) } }) } + await assert.rejects(guard.verifyRead(f.binding, scope())); assert.equal(f.counts().destroys, 1); assert.equal(f.counts().secrets, 0) + if (fault !== "grant") assert.equal(f.counts().heldQueries, 1) + } + }) + await t.test("physical owner abort DURING post-HELD Q revokes guard before consumer key callback", async () => { + const f = await fixture(), guard = f.guard(); f.reset(); let keyCallbacks = 0, killed = false + f.qHook(async () => { if (!killed && f.counts().heldQueries > 0) { killed = true; f.loseOwner() } }) + const consumer = async () => { await guard.verifyRead(f.binding, scope()); keyCallbacks++ } + await assert.rejects(consumer()); assert(killed); assert.equal(keyCallbacks, 0); assert.equal(f.counts().heldQueries, 1) + assert.throws(() => assertBandwidthOperatorPeerSourceGuard(guard)); assert.equal(f.counts().destroys, 1) + await assert.rejects(guard.verifyMetadata(f.binding, scope())) + }) + await t.test("guard requires native lifetime; legacy window without signal still registers; hostile getters never execute", async () => { + const missing = await fixture(4000, "missing"); missing.reset() + assert.throws(() => missing.guard()); assert.equal(missing.counts().destroys, 1); assert.equal(missing.counts().queries, 0) + const legacy = await fixture(4000, "missing") + assert.equal(await legacy.windows.register(legacy.window), "synthetic-registry-complete"); assert.equal(legacy.counts().destroys, 1) + for (const mode of ["accessor", "proxy", "aborted"] as const) await assert.rejects(fixture(4000, mode)) + const hostile = await fixture(4000, "hostile"), guard = hostile.guard(); hostile.reset() + await guard.verifyRead(hostile.binding, scope()); hostile.loseOwner() + assert.equal(hostile.counts().destroys, 1); assert.throws(() => assertBandwidthOperatorPeerSourceGuard(guard)); assert.equal(lifetimeGetters, 0) + }) + await t.test("idle physical lifetime loss revokes metadata guard without a new SQL/Q check", async () => { + const f = await fixture(), guard = f.guard(); f.reset(); f.loseOwner() + assert.equal(f.counts().destroys, 1); assert.throws(() => assertBandwidthOperatorPeerSourceGuard(guard)) + await assert.rejects(guard.verifyMetadata(f.binding, scope())); assert.equal(f.counts().q + f.counts().queries, 0) + }) + await t.test("metadata original revocation/close/abort/expiry cannot revive under larger fresh scope", async () => { + for (const fault of ["grant", "close", "abort", "expiry"] as const) { + const f = await fixture(fault === "expiry" ? 700 : 4000), guard = f.guard(); f.reset() + if (fault === "grant") await f.publish({ ...f.grant, grantId: randomUUID() }) + if (fault === "close") f.windows.close() + if (fault === "abort") f.caller.abort() + if (fault === "expiry") await delay(750) + await assert.rejects(guard.verifyMetadata(f.binding, scope(60000, new AbortController().signal))) + assert.equal(f.counts().q + f.counts().queries, 0); assert.equal(f.counts().destroys, 1) + } + }) + await t.test("guard is usable during registry phase and revoked after logical/final release", async () => { + const f = await fixture(); let retained!: BandwidthOperatorPeerSourceGuard + f.registerHook(async (_borrowed, requested) => { retained = f.guard(); await retained.verifyMetadata(f.binding, requested); await retained.verifyRead(f.binding, requested) }) + await f.windows.register(f.window); assert.equal(f.counts().destroys, 1) + assert.throws(() => assertBandwidthOperatorPeerSourceGuard(retained)); await assert.rejects(retained.verifyMetadata(f.binding, scope())) + }) + await t.test("requested signal mutation/cancel/short expiry or concurrent guard use destroys blocked lease; late held result cannot publish", async () => { + for (const fault of ["abort", "expiry", "busy"] as const) { + const f = await fixture(), guard = f.guard(); f.reset() + let entered!: () => void, release!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { release = resolve }) + f.heldHook(async () => { entered(); await held }) + const caller = new AbortController(), input = scope(fault === "expiry" ? 200 : 3000, caller.signal), pending = guard.verifyRead(f.binding, input); void pending.catch(() => {}) + await Promise.race([started, pending.then(() => assert.fail("held probe was not reached"))]); input.deadlineAt += 60000; input.signal = new AbortController().signal + if (fault === "abort") caller.abort() + if (fault === "busy") await assert.rejects(guard.verifyMetadata(f.binding, scope())) + await assert.rejects(pending); release(); await delay(0); assert.equal(f.counts().destroys, 1); assert.equal(f.counts().heldQueries, 1); assert.equal(f.counts().secrets, 0) + } + }) + } finally { + for (const close of cleanup) close() + const actual = await realpath(folder); assert.equal(dirname(actual), root); assert.match(basename(actual), /^bw-peer-source-guard-/); assert((await lstat(folder)).isDirectory()) + await rm(actual, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/bandwidth-operator-public-files.test.ts b/backend/src/services/bandwidth-operator-public-files.test.ts new file mode 100644 index 00000000..f321beb8 --- /dev/null +++ b/backend/src/services/bandwidth-operator-public-files.test.ts @@ -0,0 +1,65 @@ +/** Actual Ed25519/schema/private original capabilities, mocked bounded PUBLIC + * file IO scheduling only. No secret key files, SQL, native HTTP or Docker. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { resolve } from "node:path" +import { mock, test } from "node:test" + +const actual = await import("./bandwidth-control-source.js") +const files = new Map(), gates = new Map>(), started:string[]=[] +mock.module("./bandwidth-control-source.js",{namedExports:{...actual,async readBandwidthControlFile(path:string,max:number,live:()=>void){ + live();started.push(path);const bytes=files.get(path);if(!bytes||bytes.length>max)throw Error("bounded public file unavailable") + await gates.get(path);live();return Buffer.from(bytes) +}}}) +const {createBandwidthOperatorOriginalAuthorityReader}=await import("./bandwidth-operator-writer-window.js") +const {createBandwidthOperatorEnrollmentPrerequisite}=await import("./bandwidth-operator-enrollment.js") +const {canonicalBandwidthNodeValue:canonical}=await import("./bandwidth-node-bootstrap.js") +const {publicKey,privateKey}=generateKeyPairSync("ed25519"),sha=(value:Buffer|string)=>createHash("sha256").update(value).digest("hex") +const trustKeyFile=resolve("tmp/mock-operator-public-trust.pem"),grantFile=resolve("tmp/mock-operator-public-grant.json") +const root={installationId:randomUUID(),issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64)} +const peer={kind:"external" as const,enrollmentId:randomUUID()} +const config={...root,trustKeyFile,grantFile,trustKeySha256:sha(publicKey.export({type:"spki",format:"der"}))} +const scope=(signal?:AbortSignal)=>({deadlineAt:Date.now()+5000,signal}) +function publish(){ + const now=Date.now(),grant={format:"mmapp-bandwidth-operator-enrollment-v1",...root,grantId:randomUUID(),operatorId:"owned-public-read-unit",executorInventorySha256:"c".repeat(64),executorsStoppedAt:now-1000,issuedAt:now,expiresAt:now+120000, + peerRef:peer,sourceProof:"d".repeat(64),authorityGeneration:"owned-current",routingEndpointEpoch:randomUUID(),claims:[{kind:"system-id",value:"OWNED-PUBLIC-READ"}]} + const payload=Buffer.from(canonical(grant));files.set(trustKeyFile,Buffer.from(publicKey.export({type:"spki",format:"pem"})));files.set(grantFile,Buffer.from(JSON.stringify({format:"mmapp-bandwidth-operator-enrollment-envelope-v1",payload:payload.toString("base64url"),signature:sign(null,payload,privateKey).toString("base64url")}))) +} +function setup(){files.clear();gates.clear();started.length=0;publish()} +const turn=()=>new Promise(done=>setImmediate(done)) + +test("original public pair overlaps; genuine next inspect rejects replaced grant, no cached authority",async()=>{ + setup();let release!:()=>void;const gate=new Promise(done=>{release=done});gates.set(trustKeyFile,gate);gates.set(grantFile,gate) + const reader=createBandwidthOperatorOriginalAuthorityReader(config),pending=reader.acquire(scope());void pending.catch(()=>{}) + try{ + await turn();assert.deepEqual([...started].sort(),[trustKeyFile,grantFile].sort());release();const token=await pending + gates.clear();publish();await assert.rejects(reader.inspect(token,scope())) + assert.equal(started.filter(path=>path===grantFile).length,2);assert.equal(started.filter(path=>path===trustKeyFile).length,2) + }finally{release();reader.close()} +}) +test("enrollment public pair overlaps before independent Q and credential source",async()=>{ + setup();let release!:()=>void;const gate=new Promise(done=>{release=done});gates.set(trustKeyFile,gate);gates.set(grantFile,gate) + let q=0,credentials=0 + const coordinator=createBandwidthOperatorEnrollmentPrerequisite(config,{async read(){credentials++;return undefined}},{async verify(){q++;throw Error("stop before credentials")}}) + const pending=coordinator.acquire(peer,scope());void pending.catch(()=>{}) + try{ + await turn();assert.deepEqual([...started].sort(),[trustKeyFile,grantFile].sort());assert.equal(q,0);release();await assert.rejects(pending) + assert.equal(q,1);assert.equal(credentials,0) + }finally{release();coordinator.close()} +}) +test("cancelled blocked public reads never issue original capability or enter Q after late completion",async()=>{ + for(const mode of ["original","coordinator"]){ + setup();let release!:()=>void;const gate=new Promise(done=>{release=done});gates.set(trustKeyFile,gate);gates.set(grantFile,gate) + const controller=new AbortController();let q=0,credentials=0 + const reader=createBandwidthOperatorOriginalAuthorityReader(config),coordinator=createBandwidthOperatorEnrollmentPrerequisite(config,{async read(){credentials++;return undefined}},{async verify(){q++}}) + const pending=mode==="original"?reader.acquire(scope(controller.signal)):coordinator.acquire(peer,scope(controller.signal));void pending.catch(()=>{}) + try{await turn();assert.equal(started.length,2);controller.abort();release();await assert.rejects(pending);await turn();assert.equal(q,0);assert.equal(credentials,0)} + finally{release();reader.close();coordinator.close()} + } +}) +test("one failed public read cannot publish authority while its sibling completes late",async()=>{ + setup();files.delete(trustKeyFile);let release!:()=>void;gates.set(grantFile,new Promise(done=>{release=done})) + const reader=createBandwidthOperatorOriginalAuthorityReader(config) + try{await assert.rejects(reader.acquire(scope()));assert.equal(started.length,2);reader.close();release();await turn()} + finally{release();reader.close()} +}) diff --git a/backend/src/services/bandwidth-operator-registry.sql-helper.ts b/backend/src/services/bandwidth-operator-registry.sql-helper.ts new file mode 100644 index 00000000..b15bf9a5 --- /dev/null +++ b/backend/src/services/bandwidth-operator-registry.sql-helper.ts @@ -0,0 +1,97 @@ +/** Genuine coordinator -> configured signed source -> actual registry SQL. + * Disposable runner only. Live executor fencing remains an explicit synthetic + * fixture; this is not proof of production quiescence or writer enrollment. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { mkdtemp, rm, stat } from "node:fs/promises" +import { resolve, dirname, sep } from "node:path" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { createBandwidthOperatorEnrollmentPrerequisite, bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { createBandwidthOperatorRegistry, BandwidthOperatorRegistryUnknown } from "./bandwidth-operator-registry.js" +import { BandwidthExternalNodeRegistryUnknown } from "./bandwidth-external-node-registry.js" +import { bandwidthPeerRefKey, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { createOwnedExternalRegistryHarness } from "./bandwidth-external-node-registry.sql-helper.js" + +type Harness = Awaited> +type Signed = Awaited> +export async function acceptOwnedOperatorRegistry(f: Harness, signed: Signed): Promise { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + const state = resolve(process.env.MCP_STATE_DIR!), folder = await mkdtemp(resolve(state, "operator-registry-")) + assert.equal(dirname(folder), state) + const trustKeyFile = resolve(folder, "trust.pem"), grantFile = resolve(folder, "grant.json") + const { publicKey, privateKey } = generateKeyPairSync("ed25519"), sha = (bytes: Buffer | string) => createHash("sha256").update(bytes).digest("hex") + await import("node:fs/promises").then(({ writeFile }) => writeFile(trustKeyFile, publicKey.export({ type: "spki", format: "pem" }), { flag: "wx", mode: 0o600 })) + const checks: string[] = [], claims = { "system-id": "OWNED-COORDINATOR-SHARED", "software-id": "OWNED-COORDINATOR-SOFTWARE" } + let allowedPeer: BandwidthPeerRef | undefined, credentialReads = 0, quiescent = true, opens = 0, removeAfterCommit = false + let diagnostic: { began: number; queryCount: number; commitAttempted: boolean; commitAcknowledged: boolean } | undefined + const coordinator = createBandwidthOperatorEnrollmentPrerequisite({ trustKeyFile, trustKeySha256: sha(publicKey.export({ type: "spki", format: "der" })), grantFile, ...f.root }, { + async read(peer, scope) { + assert(allowedPeer); assert.equal(bandwidthPeerRefKey(peer), bandwidthPeerRefKey(allowedPeer)); assert(quiescent) + credentialReads++; return f.readPeerSnapshot(peer, scope) + }, + }, { async verify(grant, scope) { + assert(quiescent); scope.signal?.throwIfAborted(); assert(Date.now() < scope.deadlineAt) + assert.equal(grant.executorInventorySha256, sha("OWNED SYNTHETIC QUIESCENCE FIXTURE")); assert.equal(grant.operatorId, "owned-coordinator-operator") + } }) + const registry = createBandwidthOperatorRegistry({ coordinator, source: { async open(scope, guard) { + opens++; const session = await signed.node.open(scope, guard) + return { ...session, connection: { release: session.connection.release.bind(session.connection), async query(text, values, options) { + if (diagnostic) { diagnostic.queryCount++; if (text === "COMMIT") diagnostic.commitAttempted = true } + const result = await session.connection.query(text, values, options) + if (diagnostic && text === "COMMIT" && "command" in result && result.command === "COMMIT") diagnostic.commitAcknowledged = true + if (text === "COMMIT" && removeAfterCommit) { removeAfterCommit = false; await rm(grantFile) } + return result + } } } + } } }) + async function publish(peer: BandwidthPeerRef, vector = claims) { + const source = await f.readPeerSnapshot(peer, f.scope()); assert(source) + const now = Date.now(), grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", grantId: randomUUID(), + operatorId: "owned-coordinator-operator", ...f.root, executorInventorySha256: sha("OWNED SYNTHETIC QUIESCENCE FIXTURE"), executorsStoppedAt: now - 1000, + issuedAt: now - 1000, expiresAt: now + 120000, peerRef: peer, sourceProof: source.sourceProof, authorityGeneration: source.authorityGeneration, + routingEndpointEpoch: source.endpoint.routingEndpointEpoch, claims: Object.entries(vector).map(([kind, value]) => ({ kind, value })) }) + const bytes = Buffer.from(canonicalBandwidthNodeValue(grant)) + const { writeFile } = await import("node:fs/promises") + await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: bytes.toString("base64url"), signature: sign(null, bytes, privateKey).toString("base64url") }), { mode: 0o600 }) + allowedPeer = peer + } + const coreTables = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events"], beforeCore = await f.digest(coreTables) + try { + const sourceId = await f.managedEndpoint(claims), peer: BandwidthPeerRef = { kind: "server", serverId: sourceId } + await publish(peer); const managed = await registry.register(await coordinator.acquire(peer, f.scope()), f.scope()) + const enrollmentId = f.externalEndpoint(claims), external: BandwidthPeerRef = { kind: "external", enrollmentId } + await publish(external); const destination = await registry.register(await coordinator.acquire(external, f.scope()), f.scope()) + assert.equal(managed.canonicalNodeId, destination.canonicalNodeId) + assert.equal((await f.owner.query("SELECT node_id::text FROM mmapp_control.bandwidth_node_aliases WHERE server_id=$1", [sourceId])).rows[0].node_id, managed.canonicalNodeId) + assert.equal((await f.owner.query("SELECT node_id::text FROM mmapp_control.bandwidth_external_node_aliases WHERE enrollment_id=$1", [enrollmentId])).rows[0].node_id, managed.canonicalNodeId) + checks.push("genuine Ed25519 operator grant, native managed/external license GET and branded coordinator compose with configured signed-v2 node source and actual PostgreSQL registry factories to reuse one canonical node") + + await publish(external); const revoked = await coordinator.acquire(external, f.scope()), beforeReads = credentialReads, beforeOpens = opens + await rm(grantFile); await assert.rejects(registry.register(revoked, f.scope())); assert.equal(credentialReads, beforeReads); assert.equal(opens, beforeOpens) + await publish(external); const fenced = await coordinator.acquire(external, f.scope()), fenceReads = credentialReads + quiescent = false; await assert.rejects(registry.register(fenced, f.scope())); assert.equal(credentialReads, fenceReads); assert.equal(opens, beforeOpens); quiescent = true + checks.push("deleted signed grant and revoked independent quiescence fixture reject genuine registration before credentials, source lease or registry SQL") + + const lostClaims = { "system-id": "OWNED-COORDINATOR-COMMIT", "software-id": "OWNED-COORDINATOR-COMMIT-SOFTWARE" }, lostId = f.externalEndpoint(lostClaims), lostPeer: BandwidthPeerRef = { kind: "external", enrollmentId: lostId } + await publish(lostPeer, lostClaims); const lost = await coordinator.acquire(lostPeer, f.scope()); removeAfterCommit = true + diagnostic = { began: Date.now(), queryCount: 0, commitAttempted: false, commitAcknowledged: false } + await assert.rejects(registry.register(lost, f.scope()), error => { + const accepted = (error instanceof BandwidthOperatorRegistryUnknown || error instanceof BandwidthExternalNodeRegistryUnknown) && error.automaticReplay === false + if (removeAfterCommit || !accepted) console.error("Owned operator registry local diagnostic:", JSON.stringify({ elapsedMs: Date.now() - diagnostic!.began, + queryCount: diagnostic!.queryCount, commitAttempted: diagnostic!.commitAttempted, commitAcknowledged: diagnostic!.commitAcknowledged, + code: error && typeof error === "object" && "code" in error ? String(error.code).slice(0, 100) : "unclassified" })) + return accepted + }) + assert.equal(removeAfterCommit, false, "The genuine source acknowledged COMMIT before the fixture revoked the grant") + await assert.rejects(stat(grantFile), { code: "ENOENT" }) + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=$1", [lostId])).rows[0].n), 1) + const afterCommitReads = credentialReads; await assert.rejects(registry.register(lost, f.scope())); assert.equal(credentialReads, afterCommitReads) + checks.push("real coordinator registration COMMIT persists exactly one enrollment before grant-file revocation: bridge returns Unknown with no automatic replay, and original capability cannot reopen credentials") + assert.equal(await f.digest(coreTables), beforeCore) + return checks + } finally { + coordinator.close() + assert.equal(dirname(folder), state); assert(folder.startsWith(state + sep)) + await rm(folder, { recursive: true, force: true }) + } +} diff --git a/backend/src/services/bandwidth-operator-registry.test.ts b/backend/src/services/bandwidth-operator-registry.test.ts new file mode 100644 index 00000000..855251ee --- /dev/null +++ b/backend/src/services/bandwidth-operator-registry.test.ts @@ -0,0 +1,193 @@ +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { after, test } from "node:test" +import { once } from "node:events" +import { createServer } from "node:http" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { createBandwidthOperatorRegistry, type BandwidthOperatorRegistryDependencies } from "./bandwidth-operator-registry.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import { createBandwidthOperatorEnrollmentPrerequisite, bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" + +type Row = Record +const scope = (ms = 4000) => ({ deadlineAt: Date.now() + ms }) +const root = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } +const mac = (purpose: string, value: unknown) => createHmac("sha256", "SYNTHETIC_OPERATOR_REGISTRY_ISSUER") + .update(purpose + ":" + canonicalBandwidthNodeValue(value)).digest("hex") + +const server = createServer((req, res) => { + assert.equal(req.method, "GET"); assert.equal(req.url, "/rest/system/license") + assert.equal(req.headers.authorization, "Basic " + Buffer.from("SYNTHETIC-OPERATOR:SYNTHETIC-PASSWORD").toString("base64")) + res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ "system-id": "SYNTHETIC-SHARED-CLAIM" })) +}) +server.listen(0, "127.0.0.1"); await once(server, "listening") +const address = server.address(); assert(address && typeof address !== "string") +const port = address.port, cleanup: Array<() => Promise> = [] +after(async () => { for (const close of cleanup) await close(); server.closeAllConnections(); server.close(); await once(server, "close"); const { closePool } = await import("../db/index.js"); await closePool() }) + +/** Genuine module-branded coordinator, signed synthetic operator files and + * authenticated native localhost GET; only SQL and executor fence are inert. */ +async function fixture(atomic=true) { + const nodes: Row[] = [], managed: Row[] = [], external: Row[] = [], managedEvents: Row[] = [], externalEvents: Row[] = [] + const queries: string[] = [], purposes: string[] = [], signedMaterials: Array<{purpose:string;value:Row;mac:string}> = [] + const occupancy = Object.freeze({ state: "unknown", canonicalNode: "existing-occupied-node" }) + let opens = 0, closes = 0, inspections = 0, batches = 0, revoked = false, badBinding = false, commitMode: "ok" | "lost" | "rows" | "hold" = "ok" + let hook: ((stage: string) => void | Promise) | undefined, openHook: (() => Promise) | undefined + const folder = await mkdtemp(join(tmpdir(), "operator-registry-")), keys = generateKeyPairSync("ed25519") + const trustKeyFile = join(folder, "operator.pub"), grantFile = join(folder, "grant.json") + await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" })) + let currentSource: BandwidthPeerSourceSnapshot | undefined, lastGrant: ReturnType | undefined + const publish = async (value: NonNullable) => { + lastGrant = value; const payload = Buffer.from(canonicalBandwidthNodeValue(value)) + await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) + } + const coordinator = createBandwidthOperatorEnrollmentPrerequisite({ ...root, trustKeyFile, grantFile, + trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex") }, + { async read() { return currentSource } }, { async verify() { inspections++; await hook?.("inspect"); if (revoked) throw Error("Original synthetic executor fence revoked") } }) + cleanup.push(async () => { coordinator.close(); await rm(folder, { recursive: true, force: true }) }) + const source: BandwidthOperatorRegistryDependencies["source"] = { async open(_scope, dispatchGuard) { + opens++; await dispatchGuard(); await openHook?.() + const before = structuredClone({ nodes, managed, external, managedEvents, externalEvents }); let committed = false, released = false + const restore = () => { for (const [target, original] of [[nodes, before.nodes], [managed, before.managed], [external, before.external], [managedEvents, before.managedEvents], [externalEvents, before.externalEvents]]) target.splice(0, target.length, ...original) } + const close = () => { if (released) return; released = true; closes++; if (!committed) restore() } + return { close, async binding() { await hook?.("binding"); return { ...root, issuerKeySha256: badBinding ? "e".repeat(64) : root.issuerKeySha256 } }, + async keyedProof(purpose, value) { purposes.push(purpose); await hook?.("proof:" + purpose); const digest=mac(purpose,value);signedMaterials.push({purpose,value:structuredClone(value) as Row,mac:digest});return digest }, + ...(atomic?{async keyedProofs(inputs:readonly {purpose:string;value:unknown}[]){ + batches++;const values=inputs.map(({purpose,value})=>{purposes.push(purpose);const digest=mac(purpose,value);signedMaterials.push({purpose,value:structuredClone(value) as Row,mac:digest});return digest}) + // Issuer is modeled here; genuine atomic source is tested separately. + // Preserve revocation injection before returning the complete result. + for(const {purpose}of inputs)await hook?.("proof:"+purpose) + return Object.freeze(values) + }}:{}), + connection: { release: close, async query(sql, values = []) { + await dispatchGuard(); queries.push(sql); await hook?.("sql:" + sql) + const rows = (value: Row[] = [], command?: string) => ({ rows: structuredClone(value), command }) + if (sql === "BEGIN") return rows([], "BEGIN") + if (sql === "ROLLBACK") { restore(); return rows([], "ROLLBACK") } + if (sql === "COMMIT") { committed = true; if (commitMode === "lost") throw Error("Synthetic lost COMMIT ACK"); if (commitMode === "hold") await new Promise(() => {}); return rows([], commitMode === "rows" ? undefined : "COMMIT") } + if (sql.includes("set_config") || sql.includes("pg_advisory_xact_lock")) return rows() + if (sql.includes("FROM mmapp_control.bandwidth_node_authority")) return rows([{ installation_id: root.installationId, issuer_key_sha256: root.issuerKeySha256, quiescence_proof: root.quiescenceProof, status: "active" }]) + const isExternal = sql.includes("bandwidth_external_node_"), aliases = isExternal ? external : managed, events = isExternal ? externalEvents : managedEvents, id = isExternal ? "enrollment_id" : "server_id" + if(sql.startsWith("WITH aliases AS MATERIALIZED")){ + const selected=aliases.filter(row=>row[id]===values[0]) + return rows([{aliases:selected.map(row=>({subject:String(row[id]),incarnation:row.incarnation,node_id:row.node_id,status:row.status,record_mac:row.record_mac})),nodes:nodes.filter(row=>selected.some(alias=>alias.node_id===row.node_id)),events:events.filter(row=>row[id]===values[0]).map(row=>({event_id:row.event_id,incarnation:row.incarnation,node_id:row.node_id,kind:row.kind,old_status:row.old_status,new_status:row.new_status,authority_proof:row.authority_proof,event_mac:row.event_mac})),claim_nodes:values[1]===null?[]:nodes.filter(row=>row.claim_proof===values[1])}],"SELECT") + } + if(sql.startsWith("DO $mmapp_bandwidth_register$")){ + // Model only the candidate's actual atomic DO boundary from captured + // signed material. No SQL parser or synthetic metadata guard replay. + const last=(purpose:string)=>{const value=[...signedMaterials].reverse().find(value=>value.purpose===purpose);assert(value);return value!} + const node=last("node"),alias=last(isExternal?"external-alias":"alias"),event=last(isExternal?"external-event":"event"),subject=isExternal?alias.value.enrollmentId:alias.value.serverId + for(const value of [node.value.nodeId,node.value.claimProof,node.mac,alias.value.incarnation,alias.mac,event.value.eventId,event.mac])assert(sql.includes(String(value))) + if(sql.includes("IF true THEN")){assert(!nodes.some(row=>row.claim_proof===node.value.claimProof));nodes.push({node_id:node.value.nodeId,claim_proof:node.value.claimProof,enrollment_mac:node.mac})} + else assert(nodes.some(row=>row.node_id===node.value.nodeId&&row.claim_proof===node.value.claimProof&&row.enrollment_mac===node.mac)) + assert(!aliases.some(row=>row[id]===subject));aliases.push({[id]:subject,incarnation:alias.value.incarnation,node_id:alias.value.nodeId,status:"active",record_mac:alias.mac}) + events.push({event_id:event.value.eventId,[id]:subject,incarnation:event.value.incarnation,node_id:event.value.nodeId,kind:"enroll",old_status:null,new_status:"active",authority_proof:event.value.authorityProof,event_mac:event.mac}) + await hook?.("batch-applied") + return rows([],"DO") + } + if (sql.startsWith("SELECT") && sql.includes("FROM mmapp_control.bandwidth_nodes")) return rows(nodes.filter(row => row[sql.includes("WHERE claim_proof") ? "claim_proof" : "node_id"] === values[0])) + if (sql.startsWith("INSERT INTO mmapp_control.bandwidth_nodes")) { const row = { node_id: values[0], claim_proof: values[1], enrollment_mac: values[2] }; nodes.push(row); return rows([{ node_id: row.node_id }]) } + if (sql.startsWith("SELECT") && sql.includes("_aliases WHERE")) return rows(aliases.filter(row => row[id] === values[0])) + if (sql.startsWith("SELECT") && sql.includes("_events WHERE")) return rows(events.filter(row => row[id] === values[0]).sort((a, b) => String(a.kind).localeCompare(String(b.kind)))) + if (sql.startsWith("INSERT") && sql.includes("_aliases(")) { const row = { [id]: values[0], incarnation: values[1], node_id: values[2], status: "active", record_mac: values[3] }; aliases.push(row); return rows([{ [id]: row[id] }]) } + if (sql.startsWith("INSERT") && sql.includes("_events(")) { events.push({ event_id: values[0], [id]: values[1], incarnation: values[2], node_id: values[3], kind: values[4], old_status: values[5], new_status: values[6], authority_proof: values[7], event_mac: values[8] }); return rows([{ event_id: values[0] }]) } + throw Error("Unexpected actual registry SQL: " + sql) + } }, + } + } } + const dependencies = { coordinator, source }, host = createBandwidthOperatorRegistry(dependencies) + async function prerequisite(peerRef: BandwidthPeerRef, ms = 4000) { + const expiresAt = Date.now() + ms, routingEndpointEpoch = randomUUID() + currentSource = { peerRef, enabled: true, sourceProof: "c".repeat(64), authorityGeneration: "original-source", endpoint: { + host: "127.0.0.1", port, useSsl: false, verifySsl: true, username: "SYNTHETIC-OPERATOR", password: "SYNTHETIC-PASSWORD", routingEndpointEpoch } } + await publish(bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", ...root, grantId: randomUUID(), operatorId: "synthetic-original-operator", + executorInventorySha256: "e".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt, + peerRef, sourceProof: currentSource.sourceProof, authorityGeneration: currentSource.authorityGeneration, routingEndpointEpoch, claims: [{ kind: "system-id", value: "SYNTHETIC-SHARED-CLAIM" }] })) + return coordinator.acquire(peerRef, scope(ms)) + } + return { host, coordinator, dependencies, source, prerequisite, nodes, managed, external, managedEvents, externalEvents, queries, purposes, occupancy, + stats: () => ({ opens, closes, inspections, batches }), revoke: () => { revoked = true }, renew: async () => { assert(lastGrant); await publish({ ...lastGrant, expiresAt: lastGrant.expiresAt + 1000 }) }, badBinding: () => { badBinding = true }, + hook: (value: typeof hook) => { hook = value }, openHook: (value: typeof openHook) => { openHook = value }, commit: (value: typeof commitMode) => { commitMode = value } } +} + +test("explicit managed/external registration runs actual factories, shares canonical node, preserves unknown", async () => { + const f = await fixture(), initial = f.stats(); assert.equal(initial.opens, 0); assert.equal(initial.inspections, 0) + assert.deepEqual(Object.keys(f.host), ["register"]) + const managedToken = await f.prerequisite({ kind: "server", serverId: 37 }) + const first = await f.host.register(managedToken, scope()), externalToken = await f.prerequisite({ kind: "external", enrollmentId: randomUUID() }) + const second = await f.host.register(externalToken, scope()) + assert.equal(first.canonicalNodeId, second.canonicalNodeId); assert.notEqual(first.incarnation, second.incarnation) + assert.equal(f.nodes.length, 1); assert.equal(f.managed.length, 1); assert.equal(f.external.length, 1) + assert.equal((await f.host.register(externalToken, scope())).outcome, "existing"); assert.equal(f.externalEvents.length, 1) + assert(f.purposes.includes("alias")); assert(f.purposes.includes("external-alias")); assert(f.purposes.includes("external-event")) + assert(!f.queries.some(sql => /occupancy|bandwidth_attempt/.test(sql))); assert.equal(f.occupancy.state, "unknown") + assert.equal(f.queries.filter(sql => sql === "COMMIT").length, 3); assert.deepEqual(f.stats().opens, f.stats().closes) +}) + +test("captured fixed dependencies cannot be replaced; fake prerequisite fails before lease", async () => { + const f = await fixture(), token = await f.prerequisite({ kind: "server", serverId: 12 }) + f.dependencies.coordinator = { ...f.coordinator } + assert.throws(() => createBandwidthOperatorRegistry(f.dependencies)) + f.source.open = async () => { throw Error("Replacement source") } + await assert.rejects(f.host.register({ ...token }, scope())); assert.equal(f.stats().opens, 0) + assert.equal((await f.host.register(token, scope())).status, "active") +}) + +test("scalar-only trusted source remains compatible with the original guard path", async () => { + const f=await fixture(false),token=await f.prerequisite({kind:"external",enrollmentId:randomUUID()}) + assert.equal((await f.host.register(token,scope())).status,"active") + assert.equal(f.stats().batches,0);assert.equal(f.externalEvents.length,1) + assert.deepEqual(f.purposes,["claim","node","external-alias","external-event"]) +}) + +test("source binding drift and original grant renewal/revocation refuse without commit", async () => { + for (const change of ["badBinding", "renew", "revoke"] as const) { + const f = await fixture(), token = await f.prerequisite({ kind: "external", enrollmentId: randomUUID() }) + if (change === "badBinding") f.badBinding() + else f.hook(async stage => { if (stage === "binding") await f[change]() }) + await assert.rejects(f.host.register(token, scope())); assert.equal(f.queries.includes("COMMIT"), false); assert.equal(f.nodes.length, 0) + } +}) + +test("original quiescence/authority loss during issuer MAC rolls back actual factory writes", async () => { + for (const peerRef of [{ kind: "server", serverId: 21 }, { kind: "external", enrollmentId: randomUUID() }] as BandwidthPeerRef[]) + for (const boundary of ["event-proof", "after-atomic-batch"] as const) { + const f = await fixture(), token = await f.prerequisite(peerRef) + f.hook(stage => { if (boundary === "event-proof" ? stage === "proof:event" || stage === "proof:external-event" : stage === "batch-applied") f.revoke() }) + await assert.rejects(f.host.register(token, scope())); assert.equal(f.queries.includes("COMMIT"), false) + assert.equal(f.queries.filter(sql=>sql.startsWith("DO $mmapp_bandwidth_register$")).length,boundary==="after-atomic-batch"?1:0) + assert.equal(f.nodes.length, 0); assert.equal(f.managed.length, 0); assert.equal(f.external.length, 0) + } +}) + +test("no original-expiry renewal, cancelled setup destroys late lease, and no SQL follows", async () => { + const f = await fixture(), token = await f.prerequisite({ kind: "server", serverId: 42 }, 350) + f.openHook(() => new Promise(done => setTimeout(done, 500))) + await assert.rejects(f.host.register(token, scope(1000))) + await new Promise(done => setTimeout(done, 550)); assert.equal(f.stats().closes, 1); assert.equal(f.queries.length, 0) +}) + +test("lost/rows-only COMMIT ACK and held COMMIT remain unknown with no replay", async () => { + for (const peerRef of [{ kind: "server", serverId: 48 }, { kind: "external", enrollmentId: randomUUID() }] as BandwidthPeerRef[]) + for (const mode of ["lost", "rows", "hold"] as const) { + // Actual signed-file guards take roughly a second on this filesystem. Use + // the existing four-second fixture budget to reach COMMIT; never renew it. + const f = await fixture(), token = await f.prerequisite(peerRef, 4000) + f.commit(mode) + await assert.rejects(f.host.register(token, scope()), error => !!error && typeof error === "object" && "code" in error && String(error.code).endsWith("UNKNOWN") && "automaticReplay" in error && error.automaticReplay === false) + assert.equal(f.external.length + f.managed.length, 1); assert.equal(f.externalEvents.length + f.managedEvents.length, 1); assert.equal(f.queries.filter(sql => sql === "COMMIT").length, 1) + assert(!f.queries.includes("ROLLBACK")); assert.equal(f.occupancy.state, "unknown") + } +}) + +test("original authority loss after COMMIT ACK is UNKNOWN for both real registry factories", async () => { + for (const peerRef of [{ kind: "server", serverId: 49 }, { kind: "external", enrollmentId: randomUUID() }] as BandwidthPeerRef[]) { + const f = await fixture(), token = await f.prerequisite(peerRef) + f.hook(stage => { if (stage === "sql:COMMIT") f.revoke() }) + await assert.rejects(f.host.register(token, scope()), error => !!error && typeof error === "object" && "code" in error && String(error.code).endsWith("UNKNOWN")) + assert.equal(f.nodes.length, 1); assert.equal(f.managed.length + f.external.length, 1); assert(!f.queries.includes("ROLLBACK")) + } +}) diff --git a/backend/src/services/bandwidth-operator-registry.ts b/backend/src/services/bandwidth-operator-registry.ts new file mode 100644 index 00000000..80528447 --- /dev/null +++ b/backend/src/services/bandwidth-operator-registry.ts @@ -0,0 +1,162 @@ +/** Explicit operator-only bridge. No startup/runtime registration, default + * source, keys or SQL. Source must pin the FULL installed registry catalog; + * the old managed-only source cannot admit external MAC domains by fallback. */ +import { isDeepStrictEqual } from "node:util" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import type { BandwidthNodeProofInput } from "./bandwidth-node-source.js" +import { createBandwidthNodeRegistry, type BandwidthNodeRegistryReceipt } from "./bandwidth-node-registry.js" +import { createBandwidthExternalNodeRegistry, type BandwidthExternalNodeRegistryReceipt } from "./bandwidth-external-node-registry.js" +import type { BandwidthNodeObservationEvidence } from "./bandwidth-node-observation.js" +import { assertBandwidthOperatorEnrollmentCoordinator, type createBandwidthOperatorEnrollmentPrerequisite } from "./bandwidth-operator-enrollment.js" + +type Coordinator = ReturnType +type Inspection = Awaited> +export interface BandwidthOperatorRegistrySession { + readonly connection: ProbeStorageConnection + binding(): Promise<{ installationId: string; issuerKeySha256: string; quiescenceProof: string }> + keyedProof(purpose: string, value: unknown): Promise + keyedProofs?(inputs: readonly BandwidthNodeProofInput[]): Promise + close(): void +} +export interface BandwidthOperatorRegistryDependencies { + coordinator: Coordinator + source: { open(scope: BandwidthScope, dispatchGuard: () => Promise): Promise } +} +export class BandwidthOperatorRegistryError extends Error { + readonly code: string = "BANDWIDTH_OPERATOR_REGISTRY_REFUSED" + readonly automaticReplay = false + constructor() { super("Операторская регистрация bandwidth не подтверждена") } +} +/** Successful COMMIT followed by failed original-authority confirmation must + * be reconciled, never automatically replayed with a new alias/node. */ +export class BandwidthOperatorRegistryUnknown extends BandwidthOperatorRegistryError { + override readonly code = "BANDWIDTH_OPERATOR_REGISTRY_UNKNOWN" +} +const refuse = (): never => { throw new BandwidthOperatorRegistryError() } +function captureScope(raw: BandwidthScope): Readonly { + if (!raw || typeof raw !== "object" || Object.getPrototypeOf(raw) !== Object.prototype + || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.keys(raw).some(key => !["deadlineAt", "signal"].includes(key)) + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(field => !("value" in field) || !field.enumerable) + || !Number.isSafeInteger(raw.deadlineAt) || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) return refuse() + return Object.freeze({ deadlineAt: raw.deadlineAt, signal: raw.signal }) +} +function managedEvidence(value: Inspection): BandwidthNodeObservationEvidence { + const { version: _version, peerRef, ...evidence } = value.evidence + if (peerRef.kind !== "server") return refuse() + return Object.freeze({ ...evidence, version: 1, serverId: peerRef.serverId }) +} + +/** Trusted server composition only. register accepts an existing opaque + * prerequisite and scope; callers cannot pass factories, SQL, guards or keys. */ +export function createBandwidthOperatorRegistry(dependencies: BandwidthOperatorRegistryDependencies) { + assertBandwidthOperatorEnrollmentCoordinator(dependencies.coordinator) + const inspect = dependencies.coordinator.inspect.bind(dependencies.coordinator) + const open = dependencies.source.open.bind(dependencies.source) + return Object.freeze({ + async register(prerequisite: unknown, rawScope: BandwidthScope): Promise> { + const input = captureScope(rawScope), original = await inspect(prerequisite, input) + // Never renew the observation/operator grant when opening another lease. + const scope = Object.freeze({ deadlineAt: Math.min(input.deadlineAt, original.authority.expiresAt, original.evidence.expiresAt), signal: input.signal }) + const originalPeer = original.evidence.peerRef + let session: BandwidthOperatorRegistrySession | undefined, closed = false, commitStarted = false + const live = () => { if (closed || scope.signal?.aborted || Date.now() >= scope.deadlineAt) refuse() } + const check = async () => { + live(); const current = await inspect(prerequisite, scope); live() + if (current.observation !== original.observation || !isDeepStrictEqual(current, original)) refuse() + return current + } + const frameScope = (requested: BandwidthScope) => { + live() + if (!Number.isSafeInteger(requested.deadlineAt) || requested.deadlineAt > scope.deadlineAt || requested.deadlineAt <= Date.now() + || requested.signal !== scope.signal || requested.signal?.aborted) refuse() + } + let release!: () => void + const stopped = new Promise((_resolve, reject) => { release = () => { closed = true; session?.close(); reject(commitStarted ? new BandwidthOperatorRegistryUnknown() : new BandwidthOperatorRegistryError()) } }) + const timer = setTimeout(release, Math.max(1, scope.deadlineAt - Date.now())) + scope.signal?.addEventListener("abort", release, { once: true }) + const run = async () => { + try { + await check() + const opened = await open(scope, async () => { await check() }) + // A late open may have acquired a lease after cancellation; destroy it. + if (closed) { opened.close(); return refuse() } + session = opened + const binding = opened.binding.bind(opened), keyedProof = opened.keyedProof.bind(opened), close = opened.close.bind(opened) + const keyedProofs = opened.keyedProofs?.bind(opened) + const query = opened.connection.query.bind(opened.connection), free = opened.connection.release.bind(opened.connection) + const expectedRoot = { installationId: original.authority.installationId, issuerKeySha256: original.authority.issuerKeySha256, quiescenceProof: original.authority.quiescenceProof } + await check(); const actualRoot = await binding(); await check() + if (Object.entries(expectedRoot).some(([key, value]) => actualRoot[key as keyof typeof expectedRoot] !== value)) refuse() + let borrowed = false + const connection: ProbeStorageConnection = Object.freeze({ + query: async (text: string, values?: unknown[], options?: { signal?: AbortSignal; timeoutMs: number }) => { + await check(); if (text === "COMMIT") commitStarted = true + const result = await query(text, values, options); await check(); return result + }, release: (destroy?: boolean) => { free(destroy) }, + }) + const common = { + now: Date.now, + connect: async () => { await check(); if (borrowed) refuse(); borrowed = true; return connection }, + keyedProof: async (purpose: string, value: unknown) => { await check(); const result = await keyedProof(purpose, value); await check(); return result }, + ...(keyedProofs ? { keyedProofs: async (inputs: readonly BandwidthNodeProofInput[]) => { await check(); const result = await keyedProofs(inputs); await check(); return result } } : {}), + } + let receipt: Readonly + if (originalPeer.kind === "server") { + const expected = managedEvidence(original) + const registry = createBandwidthNodeRegistry({ ...common, + async inspectAndAuthorize(action, observation, requestedId, requested) { + frameScope(requested) + const current = await check(); frameScope(requested) + const evidence = managedEvidence(current) + if (action !== "register" || observation !== original.observation || requestedId !== undefined && requestedId !== originalPeer.serverId + || !isDeepStrictEqual(evidence, expected)) refuse() + // Both fields derive synchronously from this ONE genuine fresh + // inspection; this frame is never retained for another guard. + return Object.freeze({ action, observation, scope: requested, serverId: originalPeer.serverId, evidence, authority: current.authority }) + }, + // Projection/equality is synchronous: one fresh inspect is both + // its before/after boundary. Do not repeat identical file IO. + inspect: async observation => { await check(); if (observation !== original.observation) refuse(); return managedEvidence(original) }, + authorize: async (action, serverId, _scope, evidence) => { + await check(); if (action !== "register" || serverId !== originalPeer.serverId || !isDeepStrictEqual(evidence, expected)) refuse() + return original.authority + }, + }) + receipt = await registry.register(original.observation, scope) + } else { + const registry = createBandwidthExternalNodeRegistry({ ...common, + async inspectAndAuthorize(action, observation, requestedId, requested) { + frameScope(requested) + const current = await check(); frameScope(requested) + if (action !== "register" || observation !== original.observation || requestedId !== undefined && requestedId !== originalPeer.enrollmentId + || !isDeepStrictEqual(current.evidence, original.evidence)) refuse() + return Object.freeze({ action, observation, scope: requested, enrollmentId: originalPeer.enrollmentId, evidence: current.evidence, authority: current.authority }) + }, + inspect: async observation => { await check(); if (observation !== original.observation) refuse(); return original.evidence }, + authorize: async (action, enrollmentId, _scope, evidence) => { + await check(); if (action !== "register" || enrollmentId !== originalPeer.enrollmentId || !isDeepStrictEqual(evidence, original.evidence)) refuse() + return original.authority + }, + }) + receipt = await registry.register(original.observation, scope) + } + try { await check() } catch { throw new BandwidthOperatorRegistryUnknown() } + close(); return receipt + } catch (error) { + // The older managed factory does not distinguish post-COMMIT + // refusal. The bridge must retain uncertainty for BOTH peer kinds. + if (commitStarted) { + if (error && typeof error === "object" && "code" in error && String(error.code).endsWith("UNKNOWN") + && "automaticReplay" in error && error.automaticReplay === false) throw error + throw new BandwidthOperatorRegistryUnknown() + } + throw error + } finally { session?.close() } + } + try { return await Promise.race([run(), stopped]) } + finally { clearTimeout(timer); scope.signal?.removeEventListener("abort", release); closed = true; session?.close() } + }, + }) +} diff --git a/backend/src/services/bandwidth-operator-writer-composition.sql-helper.ts b/backend/src/services/bandwidth-operator-writer-composition.sql-helper.ts new file mode 100644 index 00000000..a6bf2623 --- /dev/null +++ b/backend/src/services/bandwidth-operator-writer-composition.sql-helper.ts @@ -0,0 +1,132 @@ +/** Owned disposable PostgreSQL acceptance only. Native license GET is genuine + * localhost HTTP; external stop/restart enforcement remains SYNTHETIC. */ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { dirname, resolve, sep } from "node:path" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { createBandwidthOperatorWriterComposition } from "./bandwidth-operator-writer-composition.js" +import { BandwidthOperatorWriterFenceUnknown } from "./bandwidth-operator-writer-window.js" +import { bandwidthPeerRefKey, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { createOwnedExternalRegistryHarness } from "./bandwidth-external-node-registry.sql-helper.js" + +type Harness = Awaited> +type Signed = Awaited> +export async function acceptOwnedOperatorWriterComposition(f: Harness, signed: Signed): Promise { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + assert.equal(typeof signed.node.openWithOwnershipFence, "function") + const state = resolve(process.env.MCP_STATE_DIR!), folder = await mkdtemp(resolve(state, "operator-writer-")) + assert.equal(dirname(folder), state) + const trustKeyFile = resolve(folder, "trust.pem"), grantFile = resolve(folder, "grant.json") + const keys = generateKeyPairSync("ed25519"), sha = (bytes: Buffer | string) => createHash("sha256").update(bytes).digest("hex") + await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" }), { flag: "wx", mode: 0o600 }) + const { Client } = await import("pg"), originalQuery = Client.prototype.query + const rival = await f.owner.connect(), pids = new Set(), checks: string[] = [] + let allowedPeer: BandwidthPeerRef | undefined, syntheticStopped = true, externalHeld = false, removeAfterCommit = false + let leaseOpens = 0, physicalDestroys = 0, credentialReads = 0, rivalChecks = 0 + let diagnostic = { began: Date.now(), queries: 0, rawOwnership: 0, commitAttempted: false, commitAcknowledged: false, boundary: "construction" } + async function assertHeld() { + assert(externalHeld, "synthetic external fence must stay held") + assert.equal(pids.size, 1, "one physical configured source PID; no nested control checkout") + const pid = [...pids][0] + const held = await rival.query("SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND pid=$1 AND classid=0::pg_catalog.oid AND objid=8723116::pg_catalog.oid AND objsubid=1 AND mode='ExclusiveLock' AND granted) AS held", [pid]) + assert.equal(held.rows[0].held, true) + const shared = await rival.query("SELECT pg_catalog.pg_try_advisory_lock_shared(8723116) AS acquired") + if (shared.rows[0].acquired === true) { await rival.query("SELECT pg_catalog.pg_advisory_unlock_shared(8723116)"); assert.fail("rival native shared handoff entered exclusive operator window") } + rivalChecks++ + } + Client.prototype.query = function(this: InstanceType, ...args: unknown[]) { + const physical = this as unknown as { connectionParameters: { user: string; database: string }; processID: number } + if (physical.connectionParameters.user === signed.runtimeRole && physical.connectionParameters.database === f.databaseName) pids.add(physical.processID) + return Reflect.apply(originalQuery, this, args) + } as typeof Client.prototype.query + const composition = createBandwidthOperatorWriterComposition({ + config: { ...f.root, trustKeyFile, grantFile, trustKeySha256: sha(keys.publicKey.export({ type: "spki", format: "der" })) }, + nodeSource: { ...signed.node, async openWithOwnershipFence(scope, guard, ownership) { + leaseOpens++; diagnostic.boundary = "source-open" + const session = await signed.node.openWithOwnershipFence(scope, guard, async fence => { + diagnostic.rawOwnership++; diagnostic.boundary = "raw-ownership" + await ownership(fence) + }) + let closed = false + return { ...session, close() { if (!closed) { closed = true; physicalDestroys++ }; session.close() }, connection: { + release: session.connection.release.bind(session.connection), + async query(text, values, options) { + diagnostic.queries++; diagnostic.boundary = text === "COMMIT" ? "commit" : "driver-query" + if (text === "COMMIT") diagnostic.commitAttempted = true + const result = await session.connection.query(text, values, options) + // Opening lock and PID queries precede the held phase; all registry + // queries thereafter must preserve the same session exclusion. + if (!text.includes("pg_try_advisory_lock(") && !text.includes("pg_backend_pid()")) await assertHeld() + if (text === "COMMIT" && "command" in result && result.command === "COMMIT") diagnostic.commitAcknowledged = true + if (text === "COMMIT" && removeAfterCommit) { removeAfterCommit = false; await rm(grantFile) } + return result + }, + } } + } }, + observationSource: { async read(peer, scope) { + diagnostic.boundary = "before-credentials"; assert(allowedPeer) + assert.equal(bandwidthPeerRefKey(peer), bandwidthPeerRefKey(allowedPeer)); await assertHeld() + credentialReads++; return f.readPeerSnapshot(peer, scope) + } }, + externalStop: { async acquire(original, scope) { + assert(syntheticStopped); assert.equal(original.grant.executorInventorySha256, sha("OWNED SYNTHETIC STOP/RESTART FIXTURE")) + scope.signal?.throwIfAborted(); externalHeld = true + return { async assertCurrent(current, requested) { + assert(syntheticStopped && externalHeld); assert.equal(current.grant.grantId, original.grant.grantId) + requested.signal?.throwIfAborted(); assert(Date.now() < requested.deadlineAt) + }, close() { externalHeld = false } } + } }, + }) + async function publish(peer: BandwidthPeerRef) { + const snapshot = await f.readPeerSnapshot(peer, f.scope()); assert(snapshot) + const now = Date.now(), grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", ...f.root, + grantId: randomUUID(), operatorId: "owned-writer-window", executorInventorySha256: sha("OWNED SYNTHETIC STOP/RESTART FIXTURE"), + executorsStoppedAt: now - 1000, issuedAt: now - 1000, expiresAt: now + 120000, peerRef: peer, sourceProof: snapshot.sourceProof, + authorityGeneration: snapshot.authorityGeneration, routingEndpointEpoch: snapshot.endpoint.routingEndpointEpoch, + claims: [{ kind: "system-id", value: "OWNED-SHARED-SYSTEM" }, { kind: "software-id", value: "OWNED-SHARED-SOFTWARE" }] }) + const bytes = Buffer.from(canonicalBandwidthNodeValue(grant)) + await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: bytes.toString("base64url"), signature: sign(null, bytes, keys.privateKey).toString("base64url") }), { mode: 0o600 }) + allowedPeer = peer + } + async function register() { + pids.clear(); diagnostic = { began: Date.now(), queries: 0, rawOwnership: 0, commitAttempted: false, commitAcknowledged: false, boundary: "original-authority" } + try { return await composition.register({ deadlineAt: Date.now() + 5000 }) } + catch (error) { + console.error("Owned writer window diagnostic:", JSON.stringify({ elapsedMs: Date.now() - diagnostic.began, queries: diagnostic.queries, + rawOwnership: diagnostic.rawOwnership, commitAttempted: diagnostic.commitAttempted, commitAcknowledged: diagnostic.commitAcknowledged, + boundary: diagnostic.boundary, physicalPids: pids.size, code: error && typeof error === "object" && "code" in error ? String(error.code).slice(0, 100) : "unclassified" })) + throw error + } + } + const coreTables = ["bandwidth_attempts", "bandwidth_node_occupancy", "bandwidth_attempt_events"] + try { + const occupied = await f.owner.query("SELECT o.node_id FROM mmapp_control.bandwidth_node_occupancy o JOIN mmapp_control.bandwidth_attempts a ON a.attempt_id=o.attempt_id WHERE a.state='unknown_busy'") + assert(occupied.rows.length > 0, "the caller must establish genuine journal UNKNOWN occupancy first") + const before = await f.digest(coreTables), claims = { "system-id": "OWNED-SHARED-SYSTEM", "software-id": "OWNED-SHARED-SOFTWARE" } + const managedPeer: BandwidthPeerRef = { kind: "server", serverId: await f.managedEndpoint(claims) } + await publish(managedPeer); const managed = await register() + const externalPeer: BandwidthPeerRef = { kind: "external", enrollmentId: f.externalEndpoint(claims) } + await publish(externalPeer); const external = await register() + assert.equal(managed.canonicalNodeId, external.canonicalNodeId) + assert(occupied.rows.some(row => row.node_id === managed.canonicalNodeId), "safe alias reuses the occupied canonical") + assert.equal(await f.digest(coreTables), before) + assert.equal(leaseOpens, 2); assert.equal(physicalDestroys, 2); assert(credentialReads > 0 && rivalChecks > 0) + checks.push("actual writer composition: original Ed25519 grant before synthetic external Q; genuine localhost managed/external license GET and actual registries reuse the same UNKNOWN-occupied canonical with unchanged journal/owners") + checks.push("each operation uses one configured physical PID and session-exclusive 8723116 through credentials and registry COMMIT; rival native shared acquisition refuses, raw ownership checks avoid nested source checkout, final owner destroys") + await publish(externalPeer); removeAfterCommit = true + await assert.rejects(register(), error => error instanceof BandwidthOperatorWriterFenceUnknown && error.automaticReplay === false) + assert.equal(removeAfterCommit, false); assert(diagnostic.commitAcknowledged) + assert.equal(physicalDestroys, 3); assert.equal(await f.digest(coreTables), before) + const reads = credentialReads, opens = leaseOpens + syntheticStopped = false; await publish(externalPeer); await assert.rejects(register()) + assert.equal(credentialReads, reads); assert.equal(leaseOpens, opens) + checks.push("actual acknowledged COMMIT followed by grant withdrawal returns owner UNKNOWN without replay; revoked synthetic external stop rejects before configured lease and credentials; production stop issuer remains absent") + return checks + } finally { + composition.close(); Client.prototype.query = originalQuery; rival.release(true) + assert.equal(dirname(folder), state); assert(folder.startsWith(state + sep)); await rm(folder, { recursive: true, force: true }) + } +} diff --git a/backend/src/services/bandwidth-operator-writer-composition.test.ts b/backend/src/services/bandwidth-operator-writer-composition.test.ts new file mode 100644 index 00000000..e3a64aa1 --- /dev/null +++ b/backend/src/services/bandwidth-operator-writer-composition.test.ts @@ -0,0 +1,39 @@ +import assert from "node:assert/strict" +import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { test } from "node:test" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { createBandwidthOperatorWriterComposition, type BandwidthOperatorWriterCompositionDependencies } from "./bandwidth-operator-writer-composition.js" + +test("actual composition refuses missing external enforcement before source lease or credentials", async () => { + const directory = await mkdtemp(join(tmpdir(), "operator-composition-")) + const keys = generateKeyPairSync("ed25519") + const trustKeyFile = join(directory, "operator.pub"), grantFile = join(directory, "grant.json") + const root = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } + const grant = { format: "mmapp-bandwidth-operator-enrollment-v1", ...root, grantId: randomUUID(), operatorId: "synthetic-test", + executorInventorySha256: "c".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 4000, + peerRef: { kind: "external", enrollmentId: randomUUID() }, sourceProof: "d".repeat(64), authorityGeneration: "original", + routingEndpointEpoch: randomUUID(), claims: [{ kind: "system-id", value: "synthetic-system" }] } + const payload = Buffer.from(canonicalBandwidthNodeValue(grant)) + await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" })) + await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", + payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) + let leases = 0, credentials = 0, external = 0 + // This deliberate synthetic source cannot open: the test proves ordering and + // refusal, never a genuine service stop, production source or native success. + const nodeSource = { async open() { leases++; throw Error("must not open") }, async openWithOwnershipFence() { leases++; throw Error("must not open") } } as unknown as BandwidthOperatorWriterCompositionDependencies["nodeSource"] + const composition = createBandwidthOperatorWriterComposition({ + config: { ...root, trustKeyFile, grantFile, trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex") }, + nodeSource, observationSource: { async read() { credentials++; throw Error("must not resolve") } }, + externalStop: { async acquire(original) { external++; assert.deepEqual(original.grant, grant); throw Error("No enforced external stop/restart prohibition") } }, + }) + try { + await assert.rejects(composition.register({ deadlineAt: Date.now() + 3000 }), { code: "BANDWIDTH_OPERATOR_WRITER_FENCE_REFUSED" }) + assert.equal(external, 1); assert.equal(leases, 0); assert.equal(credentials, 0) + composition.close() + await assert.rejects(composition.register({ deadlineAt: Date.now() + 3000 })) + assert.equal(external, 1) + } finally { composition.close(); await rm(directory, { recursive: true, force: true }) } +}) diff --git a/backend/src/services/bandwidth-operator-writer-composition.ts b/backend/src/services/bandwidth-operator-writer-composition.ts new file mode 100644 index 00000000..a359896e --- /dev/null +++ b/backend/src/services/bandwidth-operator-writer-composition.ts @@ -0,0 +1,92 @@ +/** Inert construction of the actual observation -> registry path on one held + * configured node lease. Nothing here installs a production external enforcer, + * admits scheduler/listener work or certifies a deployment inventory. */ +import { isDeepStrictEqual } from "node:util" +import type { BandwidthOperatorEnrollmentConfig } from "./bandwidth-operator-enrollment.js" +import { createBandwidthOperatorEnrollmentPrerequisite } from "./bandwidth-operator-enrollment.js" +import { createBandwidthOperatorRegistry } from "./bandwidth-operator-registry.js" +import type { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" +import type { BandwidthPeerTrustedSource } from "./bandwidth-peer-observation.js" +import { + createBandwidthOperatorOriginalAuthorityReader, createBandwidthOperatorExternalFencer, + createBandwidthOperatorWriterWindows, BandwidthOperatorWriterFenceError, + type BandwidthOperatorExternalStopAdapter, type BandwidthOperatorScope, + type BandwidthOperatorWriterWindow, type BandwidthOperatorPeerSourceGuard, +} from "./bandwidth-operator-writer-window.js" + +export interface BandwidthOperatorWriterCompositionDependencies { + config: BandwidthOperatorEnrollmentConfig + nodeSource: ReturnType + observationSource?: BandwidthPeerTrustedSource + /** Optional fixed server-constructor assembly, invoked ONLY after the genuine + * window is held. Mutually exclusive with legacy observationSource. */ + createObservationSource?(guard: BandwidthOperatorPeerSourceGuard): BandwidthPeerTrustedSource & { close?(): void } + /** Required trusted service-manager/credential enforcer. There is deliberately + * no default, JSON attestor, pg_locks fallback or empty-database shortcut. */ + externalStop: BandwidthOperatorExternalStopAdapter +} + +export function createBandwidthOperatorWriterComposition(dependencies: BandwidthOperatorWriterCompositionDependencies) { + const config = Object.freeze({ ...dependencies.config }) + const originals = createBandwidthOperatorOriginalAuthorityReader(config) + const external = createBandwidthOperatorExternalFencer(dependencies.externalStop) + const open = dependencies.nodeSource.open.bind(dependencies.nodeSource) + const openWithOwnershipFence = dependencies.nodeSource.openWithOwnershipFence.bind(dependencies.nodeSource) + const createObservationSource = dependencies.createObservationSource?.bind(dependencies) + const read = dependencies.observationSource?.read.bind(dependencies.observationSource) + if (!!createObservationSource === !!read) throw new BandwidthOperatorWriterFenceError() + const active = new Set<() => void>() + let closed = false + return Object.freeze({ + async register(scope: BandwidthOperatorScope) { + if (closed) throw new BandwidthOperatorWriterFenceError() + // Original authority exists BEFORE external Q and BEFORE secrets/native + // observation. The signed peer is fixed; no caller supplies a new peer. + const originalCap = await originals.acquire(scope) + if (closed) throw new BandwidthOperatorWriterFenceError() + let window: BandwidthOperatorWriterWindow | undefined + const windows = createBandwidthOperatorWriterWindows({ originals, external, source: { open, openWithOwnershipFence }, registry: { + async register(original, borrowed, heldScope) { + if (!window) throw new BandwidthOperatorWriterFenceError() + const observationSource = createObservationSource?.(windows.createPeerSourceGuard(window, originalCap)) + const readObservation = observationSource ? observationSource.read.bind(observationSource) : read! + const checkMetadata = observationSource?.assertMetadataCurrent?.bind(observationSource) + const closeObservation = observationSource?.close?.bind(observationSource) + const verify = async (grant: unknown, requested: BandwidthOperatorScope) => { + if (!window || !isDeepStrictEqual(grant, original.grant)) throw new BandwidthOperatorWriterFenceError() + await windows.verify(window, originalCap, requested) + } + const coordinator = createBandwidthOperatorEnrollmentPrerequisite(config, { + async read(peerRef, requested) { + // The coordinator invokes Q before reaching this credential source; + // keep an additional original/held-Q check at that exact boundary. + await verify(original.grant, requested) + return readObservation(peerRef, requested) + }, + ...(checkMetadata ? { assertMetadataCurrent: checkMetadata } : {}), + }, { verify }) + try { + const prerequisite = await coordinator.acquire(original.grant.peerRef, heldScope) + const registry = createBandwidthOperatorRegistry({ coordinator, source: { + async open(requested, guard) { + await verify(original.grant, requested); await guard() + // Registry owns BEGIN/COMMIT but borrows the owner's SAME lease. + // release(true) is logical; close is completed by the window + // after its final original-authority/Q check, even on failure. + return Object.freeze({ ...borrowed, close() {} }) + }, + } }) + return await registry.register(prerequisite, heldScope) + } finally { coordinator.close(); closeObservation?.() } + }, + } }) + const close = () => windows.close() + active.add(close) + try { + window = await windows.open(originalCap, scope) + return await windows.register(window) + } finally { close(); active.delete(close) } + }, + close() { closed = true; for (const stop of active) stop(); originals.close() }, + }) +} diff --git a/backend/src/services/bandwidth-operator-writer-window.test.ts b/backend/src/services/bandwidth-operator-writer-window.test.ts new file mode 100644 index 00000000..0c455516 --- /dev/null +++ b/backend/src/services/bandwidth-operator-writer-window.test.ts @@ -0,0 +1,279 @@ +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { mkdtemp, rm, writeFile, unlink } from "node:fs/promises" +import { join } from "node:path" +import { tmpdir } from "node:os" +import { performance } from "node:perf_hooks" +import { after, test } from "node:test" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { bandwidthOperatorEnrollmentGrantSchema } from "./bandwidth-operator-enrollment.js" +import { createBandwidthOperatorOriginalAuthorityReader, createBandwidthOperatorExternalFencer, createBandwidthOperatorWriterWindows, + BandwidthOperatorWriterFenceUnknown, type BandwidthOperatorRegistryBorrow, type BandwidthOperatorWriterWindowDependencies } from "./bandwidth-operator-writer-window.js" + +const later = (ms: number) => new Promise(done => setTimeout(done, ms)) +const scope = (ms = 4000) => ({ deadlineAt: Date.now() + ms }) +const cleanup: Array<() => Promise> = [] +after(async () => { for (const close of cleanup) await close() }) +type Result = { canonicalNodeId: string; incarnation: string } +type Dependencies = BandwidthOperatorWriterWindowDependencies +type Mode = "ok" | "query-error" | "commit-lost" | "commit-rows" | "commit-held" | "withdraw-after-commit" | "missing-release" | "extra-after-release" + +/** Local synthetic process/driver/external-enforcer model. NO real PostgreSQL, + * network, external service manager or production stop-proof is used. */ +async function fixture(options: { mode?: Mode; ttl?: number; lifecycle?: "register" | "quarantine"; rawFence?: boolean } = {}) { + const folder = await mkdtemp(join(tmpdir(), "writer-window-candidate-")), keys = generateKeyPairSync("ed25519") + const trustKeyFile = join(folder, "operator.pub"), grantFile = join(folder, "grant.json") + const root = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } + await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" })) + let grant = bandwidthOperatorEnrollmentGrantSchema.parse({ format: "mmapp-bandwidth-operator-enrollment-v1", ...root, grantId: randomUUID(), operatorId: "SYNTHETIC-OPERATOR", + executorInventorySha256: "c".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt: Date.now() + (options.ttl ?? 4000), + peerRef: { kind: "external", enrollmentId: randomUUID() }, sourceProof: "d".repeat(64), authorityGeneration: "SYNTHETIC-ORIGINAL", routingEndpointEpoch: randomUUID(), + claims: [{ kind: "system-id", value: "SYNTHETIC-CANONICAL-CLAIM" }] }) + async function publish(value = grant) { grant = value; const payload = Buffer.from(canonicalBandwidthNodeValue(value)); await writeFile(grantFile, + JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") })) } + await publish() + const originals = createBandwidthOperatorOriginalAuthorityReader({ ...root, trustKeyFile, grantFile, + trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex") }) + const events: string[] = [], queries: string[] = [] + const occupancy = Object.freeze({ canonicalNodeId: randomUUID(), state: "unknown", attempt: randomUUID() }) + const aliases: Array<{ id: string; canonicalNodeId: string; status: "active" | "quarantined" }> = [] + let mode: Mode = options.mode ?? "ok", sharedWriters = 0, held = false, physicalClosed = false, commits = 0, opens = 0, destroys = 0, + externalAcquires = 0, externalCloses = 0, externalAssertions = 0, finalReads = 0, backend = 73, externalAllowed = true, + borrowedReleased = false, dml = 0 + let externalDelay: (() => Promise) | undefined, sourceDelay: (() => Promise) | undefined, + hook: ((event: string) => void | Promise) | undefined + const external = createBandwidthOperatorExternalFencer({ async acquire(original) { + externalAcquires++; events.push("external.acquire"); assert.equal(original.grant.executorInventorySha256, grant.executorInventorySha256) + await externalDelay?.(); if (!externalAllowed) throw Error("Synthetic old executor stop/restart prohibition is NOT held") + let stopped = false + return { async assertCurrent(current) { + externalAssertions++; events.push("external.assert"); assert(!stopped); assert.equal(current.grant.grantId, original.grant.grantId) + if (borrowedReleased) { finalReads++; assert.equal(physicalClosed, false, "final authority check must precede physical destroy") } + await hook?.("external.assert"); if (!externalAllowed) throw Error("External restart prohibition revoked") + }, close() { if (!stopped) { stopped = true; externalCloses++; events.push("external.close") } } } + } }) + const source: Dependencies["source"] = { async open(_scope, guard) { + opens++; events.push("source.open"); await guard(); await sourceDelay?.() + physicalClosed = false + const close = () => { if (physicalClosed) return; physicalClosed = true; held = false; destroys++; events.push("source.destroy") } + return { close, async binding() { await guard(); return root }, async keyedProof(purpose, value) { + await guard(); await hook?.("keyedProof"); return createHmac("sha256", "SYNTHETIC-CANDIDATE-ISSUER").update(purpose + ":" + canonicalBandwidthNodeValue(value)).digest("hex") + }, connection: { release() { throw Error("Physical connection.release is owner-only; must destroy") }, async query(text) { + assert(!physicalClosed); await guard(); queries.push(text); events.push("query:" + text); await hook?.(text) + if (text.includes("pg_try_advisory_lock(8723116)")) { const acquired = sharedWriters === 0 && !held; if (acquired) held = true; return { rows: [{ acquired }] } } + if (text.includes("pg_locks")) return { rows: [{ backend_pid: backend, held }] } + if (text.includes("pg_backend_pid()")) return { rows: [{ backend_pid: backend }] } + if (text === "SELECT pg_catalog.pg_advisory_xact_lock(8723116)") { assert(held, "same backend reenters the SAME session key without deadlocking"); return { rows: [] } } + if (text === "BEGIN") return { rows: [], command: "BEGIN" } + if (text === "COMMIT") { commits++; if (mode === "commit-held") await new Promise(() => {}); if (mode === "commit-lost") throw Error("Synthetic lost actual ACK"); return { rows: [], command: mode === "commit-rows" ? undefined : "COMMIT" } } + if (text === "INSERT SYNTHETIC ALIAS") { + if (mode === "query-error") throw Error("Synthetic physical driver failed") + dml++; aliases.push({ id: randomUUID(), canonicalNodeId: occupancy.canonicalNodeId, status: options.lifecycle === "quarantine" ? "quarantined" : "active" }); return { rows: [] } + } + throw Error("Unexpected inert SQL: " + text) + } } } + } } + if (options.rawFence) source.openWithOwnershipFence = async (requested, guard, verifyOwnership) => { + const session = await source.open(requested, guard) + const dispatch = async (work: () => Promise) => { + const boundary = async () => { + events.push("raw.metadata") + await verifyOwnership({ + async query(text) { + assert(text.includes("pg_locks")); events.push("raw.ownership") + return { rows: [{ backend_pid: backend, held }] } + }, + async verifyNodeBootstrap() { throw Error("unused") }, + async sourceIdentity() { throw Error("unused") }, + }) + events.push("raw.afterfiles"); await guard(); events.push("raw.original-last") + } + await boundary(); const result = await work(); await boundary(); return result + } + return { ...session, + binding: () => dispatch(() => session.binding()), + keyedProof: (purpose, value) => dispatch(() => session.keyedProof(purpose, value)), + keyedProofs: inputs => dispatch(async () => { + events.push("raw.batch-select1") + return inputs.map(input => createHmac("sha256", "SYNTHETIC-CANDIDATE-ISSUER").update(input.purpose + ":" + canonicalBandwidthNodeValue(input.value)).digest("hex")) + }), + connection: { release: session.connection.release, + query: (text, values, queryOptions) => dispatch(() => session.connection.query(text, values, queryOptions)), + }, + } + } + const registry: Dependencies["registry"] = { async register(_original, borrowed) { + let result: Result | undefined + try { + await borrowed.connection.query("BEGIN") + await borrowed.connection.query("SELECT pg_catalog.pg_advisory_xact_lock(8723116)") + if (options.rawFence) { + assert(borrowed.keyedProofs) + const proofs = await borrowed.keyedProofs([{ purpose: "external-alias", value: { ...root, canonicalNode: occupancy.canonicalNodeId } }]) + assert.equal(proofs.length, 1) + } else await borrowed.keyedProof("external-alias", { ...root, canonicalNode: occupancy.canonicalNodeId }) + await borrowed.connection.query("INSERT SYNTHETIC ALIAS") + await borrowed.connection.query("COMMIT") + if (mode === "withdraw-after-commit") await unlink(grantFile) + result = { canonicalNodeId: occupancy.canonicalNodeId, incarnation: randomUUID() } + } finally { + if (mode !== "missing-release") { borrowed.connection.release(true); borrowedReleased = true; events.push("borrow.logical-release") } + } + if (mode === "extra-after-release") await borrowed.connection.query("INSERT SYNTHETIC ALIAS") + assert(result); return result + } } + const dependencies = { originals, external, source, registry }, windows = createBandwidthOperatorWriterWindows(dependencies) + cleanup.push(async () => { windows.close(); originals.close(); await rm(folder, { recursive: true, force: true }) }) + const authority = await originals.acquire(scope(options.ttl ?? 4000)) + return { windows, originals, dependencies, authority, events, queries, occupancy, aliases, publish, + grant: () => grant, snapshot: () => ({ opens, destroys, commits, held, physicalClosed, dml, externalAcquires, externalCloses, externalAssertions, finalReads }), + setShared: (value: number) => { sharedWriters = value }, revokeExternal: () => { externalAllowed = false }, loseSession: () => { held = false }, changeBackend: () => { backend++ }, + sourceDelay: (value: typeof sourceDelay) => { sourceDelay = value }, externalDelay: (value: typeof externalDelay) => { externalDelay = value }, hook: (value: typeof hook) => { hook = value }, setMode: (value: Mode) => { mode = value } } +} + +test("signed original BEFORE external fence; one physical source/session lock; logical release preserves final read", async () => { + const f = await fixture(), before = f.occupancy, window = await f.windows.open(f.authority, scope()) + assert.equal(f.snapshot().externalAcquires, 1); assert.equal(f.snapshot().opens, 1); assert(f.snapshot().held) + await f.windows.verify(window, f.authority, scope(3000)) + const receipt = await f.windows.register(window) + assert.equal(receipt.canonicalNodeId, before.canonicalNodeId); assert.deepEqual(f.occupancy, before); assert.equal(f.snapshot().finalReads, 1) + assert.equal(f.snapshot().destroys, 1); assert.equal(f.snapshot().externalCloses, 1); assert.equal(f.snapshot().held, false) + assert(f.events.indexOf("borrow.logical-release") < f.events.indexOf("source.destroy")) + assert.equal(f.queries.filter(sql => sql.includes("pg_try_advisory_lock")).length, 1) + assert(f.queries.indexOf("BEGIN") > f.queries.findIndex(sql => sql.includes("pg_try_advisory_lock")), "no outer BEGIN competes with factory BEGIN") + assert(!f.queries.some(sql => /occupancy|UNLOCK|DELETE|TRUNCATE/.test(sql))) + await assert.rejects(f.windows.register(window)); await assert.rejects(f.windows.verify(window, f.authority, scope(100))) +}) + +test("raw source ownership fence checks exact held backend before final files without outer metadata SQL", async () => { + const healthy = await fixture({ rawFence: true }) + const window = await healthy.windows.open(healthy.authority, scope()) + assert(!healthy.events.includes("raw.ownership"), "opening cannot require the not-yet-acquired lock") + await healthy.windows.register(window) + assert(healthy.events.includes("raw.batch-select1"), "batch MAC path must use its fresh raw-fenced dispatch") + assert(!healthy.queries.some(query => query.includes("pg_locks")), "ownership checks must bypass recursive source metadata") + const checks = healthy.events.flatMap((event, index) => event === "raw.ownership" ? [index] : []) + assert(checks.length >= 8) + for (const index of checks) { + assert.equal(healthy.events[index - 1], "raw.metadata") + assert.equal(healthy.events[index + 1], "raw.afterfiles") + // The original guard runs after the final source file checks. External Q + // appends its own events before the guard acknowledges this boundary. + assert(healthy.events.slice(index + 2).includes("raw.original-last")) + } + assert.equal(healthy.snapshot().destroys, 1) + for (const failure of ["held", "pid"] as const) { + const fixtureValue = await fixture({ rawFence: true }) + const heldWindow = await fixtureValue.windows.open(fixtureValue.authority, scope()) + fixtureValue.hook(event => { + if (event === "INSERT SYNTHETIC ALIAS") { + if (failure === "held") fixtureValue.loseSession(); else fixtureValue.changeBackend() + } + }) + await assert.rejects(fixtureValue.windows.register(heldWindow)) + assert.equal(fixtureValue.snapshot().destroys, 1) + assert.equal(fixtureValue.snapshot().commits, 0, "post-dispatch ownership loss refuses before COMMIT") + } +}) + +test("constructor and private capability copies refuse before source or external stop acquisition", async () => { + const f = await fixture() + assert.throws(() => createBandwidthOperatorWriterWindows({ ...f.dependencies, originals: { ...f.originals } })) + assert.throws(() => createBandwidthOperatorWriterWindows({ ...f.dependencies, external: { ...f.dependencies.external } })) + await assert.rejects(f.windows.open({ ...f.authority }, scope())); assert.equal(f.snapshot().externalAcquires, 0); assert.equal(f.snapshot().opens, 0) + const window = await f.windows.open(f.authority, scope()); await assert.rejects(f.windows.register({ ...window })); f.windows.close() +}) + +test("shared native handoff holder blocks exclusive window; SQL absence alone does not issue external stop proof", async () => { + const f = await fixture(); f.setShared(1) + await assert.rejects(f.windows.open(f.authority, scope())); assert.equal(f.snapshot().held, false); assert.equal(f.snapshot().destroys, 1) + const missing = await fixture(); missing.revokeExternal() + await assert.rejects(missing.windows.open(missing.authority, scope())); assert.equal(missing.snapshot().opens, 0); assert.equal(missing.queries.length, 0) +}) + +test("safe alias register and quarantine preserve an existing canonical UNKNOWN; no blanket occupancy prohibition", async () => { + for (const lifecycle of ["register", "quarantine"] as const) { + const f = await fixture({ lifecycle }), before = structuredClone(f.occupancy) + const receipt = await f.windows.register(await f.windows.open(f.authority, scope())) + assert.equal(receipt.canonicalNodeId, before.canonicalNodeId); assert.deepEqual(f.occupancy, before) + assert.equal(f.aliases[0].status, lifecycle === "quarantine" ? "quarantined" : "active") + } +}) + +test("physical driver failure, held-lock loss, backend drift and external revoke destroy immediately", async () => { + for (const fault of ["query-error", "lock", "backend", "external"] as const) { + const f = await fixture({ mode: fault === "query-error" ? "query-error" : "ok" }), window = await f.windows.open(f.authority, scope()) + if (fault === "lock") f.hook(stage => { if (stage === "BEGIN") f.loseSession() }) + if (fault === "backend") f.hook(stage => { if (stage === "keyedProof") f.changeBackend() }) + if (fault === "external") f.hook(stage => { if (stage === "keyedProof") f.revokeExternal() }) + await assert.rejects(f.windows.register(window)); assert.equal(f.snapshot().destroys, 1); assert.equal(f.snapshot().externalCloses, 1); assert.equal(f.snapshot().commits, 0) + } +}) + +test("lost/rows-only COMMIT ACK, final grant withdrawal, missing logical release and illegal post-release SQL are UNKNOWN", async () => { + for (const mode of ["commit-lost", "commit-rows", "withdraw-after-commit", "missing-release", "extra-after-release"] as const) { + const f = await fixture({ mode }), window = await f.windows.open(f.authority, scope()), before = f.occupancy + await assert.rejects(f.windows.register(window), error => error instanceof BandwidthOperatorWriterFenceUnknown && error.automaticReplay === false) + assert.equal(f.snapshot().commits, 1); assert.equal(f.snapshot().destroys, 1); assert.equal(f.snapshot().externalCloses, 1) + assert.deepEqual(f.occupancy, before); assert.equal(f.snapshot().dml, 1); await assert.rejects(f.windows.register(window)) + } +}) + +test("cancel during held COMMIT is UNKNOWN and physically destroys before the late driver completes", async () => { + const f = await fixture({ mode: "commit-held" }), abort = new AbortController() + const window = await f.windows.open(f.authority, { ...scope(), signal: abort.signal }) + f.hook(stage => { if (stage === "COMMIT") abort.abort() }) + await assert.rejects(f.windows.register(window), BandwidthOperatorWriterFenceUnknown) + assert.equal(f.snapshot().destroys, 1); assert.equal(f.snapshot().externalCloses, 1); assert.equal(f.snapshot().held, false) +}) + +test("cancelled source/external acquisitions destroy late resources and cannot publish a held window", async () => { + for (const stage of ["source", "external"] as const) { + const f = await fixture(), abort = new AbortController(); let resume!: () => void + const held = new Promise(done => { resume = done }) + if (stage === "source") f.sourceDelay(() => { abort.abort(); return held }) + else f.externalDelay(() => { abort.abort(); return held }) + await assert.rejects(f.windows.open(f.authority, { ...scope(), signal: abort.signal })) + resume(); await later(20) + assert.equal(f.snapshot().externalCloses, 1) + assert.equal(f.snapshot().destroys, stage === "source" ? 1 : 0) + assert(!f.queries.includes("BEGIN")) + } +}) + +test("original signed grant/inventory cannot be renewed or replaced between authority and quiescence", async () => { + const f = await fixture(), before = f.snapshot() + await f.publish({ ...f.grant(), executorInventorySha256: "e".repeat(64), expiresAt: f.grant().expiresAt + 1000 }) + await assert.rejects(f.windows.open(f.authority, scope())) + assert.equal(f.snapshot().externalAcquires, before.externalAcquires); assert.equal(f.snapshot().opens, before.opens) +}) + +test("the original signed-authority cancellation survives later scopes and retires an already held owner", async () => { + const f = await fixture(), abort = new AbortController() + const authority = await f.originals.acquire({ ...scope(), signal: abort.signal }) + const window = await f.windows.open(authority, scope()) + abort.abort() + assert.equal(f.snapshot().destroys, 1); assert.equal(f.snapshot().externalCloses, 1) + await assert.rejects(f.windows.register(window)); await assert.rejects(f.originals.inspect(authority, scope())) +}) + +test("a shorter verifier scope and its cancellation bound a stalled fence without renewing the held window", async () => { + for (const kind of ["deadline", "abort"] as const) { + const f = await fixture(), window = await f.windows.open(f.authority, scope()), abort = new AbortController() + let resume!: () => void; const stalled = new Promise(done => { resume = done }) + f.hook(stage => { if (stage === "external.assert") { if (kind === "abort") abort.abort(); return stalled } }) + try { await assert.rejects(f.windows.verify(window, f.authority, { ...scope(kind === "deadline" ? 50 : 3000), signal: abort.signal })) } + finally { resume() } + assert.equal(f.snapshot().destroys, 1); assert.equal(f.snapshot().externalCloses, 1); assert(!f.queries.includes("BEGIN")) + } +}) + +test("backward wall jump while external fence is stalled preserves original monotonic grant expiry and no source checkout", async () => { + const f = await fixture({ ttl: 250 }), realNow = Date.now + let resume!: () => void; const held = new Promise(done => { resume = done }) + f.externalDelay(() => { Date.now = () => realNow() - 500; return held }) + const began = performance.now() + try { await assert.rejects(f.windows.open(f.authority, scope())); assert(performance.now() - began < 750); assert.equal(f.snapshot().opens, 0) } + finally { Date.now = realNow; resume() } + await later(20); assert.equal(f.snapshot().externalCloses, 1) +}) diff --git a/backend/src/services/bandwidth-operator-writer-window.ts b/backend/src/services/bandwidth-operator-writer-window.ts new file mode 100644 index 00000000..1d3c018d --- /dev/null +++ b/backend/src/services/bandwidth-operator-writer-window.ts @@ -0,0 +1,414 @@ +/** Explicit operator-only held writer window. No startup activation or external + * stop issuer. Trusted construction must enforce stop/restart prohibition for + * the original inventory; session exclusion alone proves no old executor. */ +import { createHash, createPublicKey, verify } from "node:crypto" +import { isDeepStrictEqual, types } from "node:util" +import { isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { bandwidthOperatorEnrollmentGrantSchema, type BandwidthOperatorEnrollmentConfig } from "./bandwidth-operator-enrollment.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { readBandwidthControlFile, type BandwidthControlDispatchFence } from "./bandwidth-control-source.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js" + +export interface BandwidthOperatorScope { deadlineAt: number; signal?: AbortSignal } +type Grant = z.infer +export type BandwidthOperatorOriginalAuthoritySnapshot = Readonly<{ grant: Readonly; proof: string; end: number; signal?: AbortSignal }> +const originalsBrand = new WeakSet(), fencersBrand = new WeakSet(), peerGuardsBrand = new WeakSet() +const nativeSignalAborted = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "aborted")!.get! +const LOCK_SQL = "SELECT pg_catalog.pg_try_advisory_lock(8723116) AS acquired" +const BACKEND_SQL = "SELECT pg_catalog.pg_backend_pid() AS backend_pid" +const HELD_SQL = "SELECT pg_catalog.pg_backend_pid() AS backend_pid, EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND pid=pg_catalog.pg_backend_pid() AND mode='ExclusiveLock' AND classid=0::pg_catalog.oid AND objid=8723116::pg_catalog.oid AND objsubid=1 AND granted) AS held" +const hash = z.string().regex(/^[a-f0-9]{64}$/) +const configSchema = z.object({ trustKeyFile: z.string(), trustKeySha256: hash, grantFile: z.string(), installationId: z.string().uuid(), issuerKeySha256: hash, quiescenceProof: hash }).strict() +const envelopeSchema = z.object({ format: z.literal("mmapp-bandwidth-operator-enrollment-envelope-v1"), payload: z.string().min(1).max(16000).regex(/^[A-Za-z0-9_-]+$/), signature: z.string().length(86).regex(/^[A-Za-z0-9_-]+$/) }).strict() +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +export class BandwidthOperatorWriterFenceError extends Error { + readonly code: string = "BANDWIDTH_OPERATOR_WRITER_FENCE_REFUSED" + readonly automaticReplay = false + constructor() { super("BandwidthOperator held writer fence unavailable") } +} +export class BandwidthOperatorWriterFenceUnknown extends BandwidthOperatorWriterFenceError { + override readonly code = "BANDWIDTH_OPERATOR_WRITER_FENCE_UNKNOWN" +} +const refuse = (): never => { throw new BandwidthOperatorWriterFenceError() } +export interface BandwidthOperatorPeerSourceBinding { + readonly peerRef: BandwidthPeerRef; readonly sourceProof: string; readonly authorityGeneration: string; readonly routingEndpointEpoch: string +} +declare const peerSourceGuardBrand: unique symbol +export interface BandwidthOperatorPeerSourceGuard { + readonly [peerSourceGuardBrand]: true + readonly installationId: string + verifyRead(binding: BandwidthOperatorPeerSourceBinding, scope: Readonly): Promise + verifyMetadata(binding: BandwidthOperatorPeerSourceBinding, scope: Readonly): Promise +} +/** Provenance only; every method independently checks the original live window. */ +export function assertBandwidthOperatorPeerSourceGuard(value: unknown): asserts value is BandwidthOperatorPeerSourceGuard { + if (!value || typeof value !== "object" || !peerGuardsBrand.has(value)) refuse() +} +function peerBindingSnapshot(raw: BandwidthOperatorPeerSourceBinding): Readonly { + if (!raw || types.isProxy(raw) || Object.getPrototypeOf(raw) !== Object.prototype) return refuse() + const fields = Object.getOwnPropertyDescriptors(raw), names = ["peerRef", "sourceProof", "authorityGeneration", "routingEndpointEpoch"] + if (Reflect.ownKeys(raw).length !== names.length || names.some(name => !fields[name] || !("value" in fields[name]) || !fields[name].enumerable)) return refuse() + const peer: unknown = fields.peerRef.value + if (!peer || typeof peer !== "object" || types.isProxy(peer) || Object.getPrototypeOf(peer) !== Object.prototype) return refuse() + const peerFields = Object.getOwnPropertyDescriptors(peer), kind = peerFields.kind + if (!kind || !("value" in kind) || !kind.enumerable || !["server", "external"].includes(kind.value)) return refuse() + const id = kind.value === "server" ? "serverId" : "enrollmentId" + if (Reflect.ownKeys(peer).length !== 2 || !peerFields[id] || !("value" in peerFields[id]) || !peerFields[id].enumerable) return refuse() + const value = z.object({ peerRef: z.union([z.object({ kind: z.literal("server"), serverId: z.number().int().positive().safe() }).strict(), + z.object({ kind: z.literal("external"), enrollmentId: z.string().uuid().regex(/^[a-f0-9-]+$/) }).strict()]), sourceProof: hash, + authorityGeneration: z.string().min(1).max(200), routingEndpointEpoch: z.string().uuid() }).strict().parse({ peerRef: { kind: kind.value, [id]: peerFields[id].value }, + sourceProof: fields.sourceProof.value, authorityGeneration: fields.authorityGeneration.value, routingEndpointEpoch: fields.routingEndpointEpoch.value }) + Object.freeze(value.peerRef); return Object.freeze(value) +} +function scopeSnapshot(raw: BandwidthOperatorScope): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.keys(raw).some(key => !["deadlineAt", "signal"].includes(key)) + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(value => !("value" in value) || !value.enumerable) + || !Number.isSafeInteger(raw.deadlineAt) || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) return refuse() + return Object.freeze({ deadlineAt: raw.deadlineAt, signal: raw.signal }) +} +function freeze(value: T): T { if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } return value } +declare const originalBrand: unique symbol +export interface BandwidthOperatorOriginalAuthority { readonly [originalBrand]: true } + +/** Separate original signed authority BEFORE external quiescence. It supplies + * permission to attempt enrollment, never a claim that any executor stopped. */ +export function createBandwidthOperatorOriginalAuthorityReader(raw: BandwidthOperatorEnrollmentConfig) { + const config = Object.freeze(configSchema.parse(raw)), issued = new WeakMap() + if ([config.trustKeyFile, config.grantFile].some(path => !isAbsolute(path) || resolve(path) !== path) || config.trustKeyFile === config.grantFile) refuse() + let closed = false + async function read(live: () => void) { + // Independent bounded PUBLIC files. Retain every per-file inode/path/time + // fence and every current read; only overlap their I/O, never cache grant + // authority or move a secret-key read across an async verifier boundary. + live(); const [publicBytes, grantBytes] = await Promise.all([ + readBandwidthControlFile(config.trustKeyFile, 16384, live), + readBandwidthControlFile(config.grantFile, 32768, live), + ]); live() + const key = createPublicKey(publicBytes) + if (key.asymmetricKeyType !== "ed25519" || sha(key.export({ type: "spki", format: "der" })) !== config.trustKeySha256) refuse() + const signed = envelopeSchema.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(grantBytes))) + const bytes = Buffer.from(signed.payload, "base64url"), signature = Buffer.from(signed.signature, "base64url") + if (bytes.toString("base64url") !== signed.payload || signature.toString("base64url") !== signed.signature || !verify(null, bytes, key, signature)) refuse() + const grant = bandwidthOperatorEnrollmentGrantSchema.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes))) + if (canonicalBandwidthNodeValue(grant) !== bytes.toString("utf8") || grant.installationId !== config.installationId || grant.issuerKeySha256 !== config.issuerKeySha256 + || grant.quiescenceProof !== config.quiescenceProof || grant.issuedAt > Date.now() || grant.expiresAt <= Date.now()) refuse() + live(); return freeze({ grant, proof: sha(bytes) }) + } + const reader = Object.freeze({ + async acquire(rawScope: BandwidthOperatorScope): Promise { + const scope = scopeSnapshot(rawScope), wall = Date.now(), mono = performance.now(), end = mono + Math.max(0, Math.min(5000, scope.deadlineAt - wall)) + const live = () => { if (closed || scope.signal?.aborted || Date.now() >= scope.deadlineAt || performance.now() >= end) refuse() } + const original = await read(live) + const originalEnd = Math.min(end, mono + Math.max(0, original.grant.expiresAt - wall)) + live(); if (performance.now() >= originalEnd) refuse() + const capability = Object.freeze(Object.create(null)) as BandwidthOperatorOriginalAuthority + issued.set(capability, Object.freeze({ ...original, end: originalEnd, signal: scope.signal })); return capability + }, + async inspect(capability: unknown, rawScope: BandwidthOperatorScope): Promise { + const origin = capability && typeof capability === "object" ? issued.get(capability) : undefined + if (!origin) return refuse() + const scope = scopeSnapshot(rawScope), wall = Date.now(), mono = performance.now(), end = Math.min(origin.end, mono + Math.max(0, scope.deadlineAt - wall)) + const live = () => { if (closed || scope.signal?.aborted || origin.signal?.aborted || Date.now() >= scope.deadlineAt || Date.now() >= origin.grant.expiresAt || performance.now() >= end) refuse() } + const current = await read(live) + if (!isDeepStrictEqual(current.grant, origin.grant) || current.proof !== origin.proof) refuse() + live(); return origin + }, + close() { closed = true }, + }) + originalsBrand.add(reader); return reader +} +type Reader = ReturnType + +/** A production adapter must hold an ENFORCED external stop+restart prohibition + * across the complete original inventory. A DB lock/empty SQL result, an + * operator signature or network-write permission MUST NOT satisfy this ABI. */ +export interface BandwidthOperatorExternalStopAdapter { + acquire(original: BandwidthOperatorOriginalAuthoritySnapshot, scope: Readonly): Promise<{ + assertCurrent(original: BandwidthOperatorOriginalAuthoritySnapshot, scope: Readonly): Promise + close(): void + }> +} +export function createBandwidthOperatorExternalFencer(adapter: BandwidthOperatorExternalStopAdapter) { + const acquire = adapter.acquire.bind(adapter) + const fencer = Object.freeze({ acquire }) + fencersBrand.add(fencer); return fencer +} +type Fencer = ReturnType +export interface BandwidthOperatorNodeSession { + readonly connection: ProbeStorageConnection + binding(): Promise<{ installationId: string; issuerKeySha256: string; quiescenceProof: string }> + keyedProof(purpose: string, value: unknown): Promise + keyedProofs?(inputs: readonly { purpose: string; value: unknown }[]): Promise + close(): void +} +export interface BandwidthOperatorRegistryBorrow { + readonly connection: ProbeStorageConnection + binding(): Promise<{ installationId: string; issuerKeySha256: string; quiescenceProof: string }> + keyedProof(purpose: string, value: unknown): Promise + keyedProofs?(inputs: readonly { purpose: string; value: unknown }[]): Promise +} +export interface BandwidthOperatorWriterWindowDependencies { + originals: Reader + external: Fencer + /** Actual combined signed source in a future reviewed composition. open MUST + * NOT acquire another lock/lease in its dispatchGuard or wait for this owner. */ + source: { + open(scope: BandwidthOperatorScope, dispatchGuard: () => Promise): Promise + /** Trusted configured-source path: ownership SQL uses its existing raw core + * fence, before the source's final file checks and original authority guard. */ + openWithOwnershipFence?(scope: BandwidthOperatorScope, dispatchGuard: () => Promise, + verifyOwnership: (fence: BandwidthControlDispatchFence) => Promise): Promise + } + /** Captured operator registry factory adapter, never a per-request callback. + * It keeps its own BEGIN/COMMIT. Its finally release(true) is LOGICAL only + * after an exact COMMIT ACK, pending the owner's final read-only verification. */ + registry: { register(original: BandwidthOperatorOriginalAuthoritySnapshot, borrowed: BandwidthOperatorRegistryBorrow, scope: BandwidthOperatorScope): Promise } +} +declare const windowBrand: unique symbol +export interface BandwidthOperatorWriterWindow { readonly [windowBrand]: true } + +/** BandwidthOperator only. One physical source lease, fixed SESSION exclusive lock, + * private lifetime and two-phase owner/borrow completion. No occupancy API. */ +export function createBandwidthOperatorWriterWindows(dependencies: BandwidthOperatorWriterWindowDependencies) { + if (!originalsBrand.has(dependencies.originals) || !fencersBrand.has(dependencies.external)) refuse() + const inspect = dependencies.originals.inspect.bind(dependencies.originals), acquireExternal = dependencies.external.acquire.bind(dependencies.external) + const openSource = dependencies.source.open.bind(dependencies.source), register = dependencies.registry.register.bind(dependencies.registry) + const openWithOwnershipFence = dependencies.source.openWithOwnershipFence?.bind(dependencies.source) + type State = { + original: BandwidthOperatorOriginalAuthoritySnapshot; originalCap: unknown; scope: Readonly; end: number; controller: AbortController; timer: ReturnType + phase: "opening" | "held" | "registry" | "logical-release" | "final-read" | "terminal"; commitStarted: boolean; commitAck: boolean + session?: BandwidthOperatorNodeSession; external?: Awaited>; query?: ProbeStorageConnection["query"] + binding?: BandwidthOperatorNodeSession["binding"]; keyedProof?: BandwidthOperatorNodeSession["keyedProof"]; keyedProofs?: BandwidthOperatorNodeSession["keyedProofs"]; backend?: number; busy: boolean + externalClose?: () => void; sessionClose?: () => void; inputAbort: () => void; originalSignal?: AbortSignal; stopReject: (error: Error) => void; stopped: Promise + peerGuards?: Set + lifetimeSignal?: AbortSignal; lifetimeAbort?: () => void + } + const states = new WeakMap(), active = new Set() + let closed = false + function retire(state: State) { + if (state.phase === "terminal") return + state.phase = "terminal"; clearTimeout(state.timer); state.originalSignal?.removeEventListener("abort", state.inputAbort) + // Intentional retirement is already terminal before physical destruction + // aborts its lease signal. It must not turn successful COMMIT into UNKNOWN. + if (state.lifetimeSignal && state.lifetimeAbort) Reflect.apply(EventTarget.prototype.removeEventListener, state.lifetimeSignal, ["abort", state.lifetimeAbort]) + state.lifetimeAbort = undefined + for (const guard of state.peerGuards ?? []) peerGuardsBrand.delete(guard) + state.peerGuards?.clear() + state.controller.abort() + try { state.sessionClose?.() } catch { /* Terminal, never healthy reuse. */ } + try { state.externalClose?.() } catch { /* Failed external teardown cannot issue readiness. */ } + active.delete(state) + } + const error = (state: State) => state.commitStarted ? new BandwidthOperatorWriterFenceUnknown() : new BandwidthOperatorWriterFenceError() + function stop(state: State) { const failure = error(state); retire(state); state.stopReject(failure) } + function live(state: State) { + if (closed || state.phase === "terminal" || state.controller.signal.aborted || state.originalSignal?.aborted + || state.lifetimeSignal && Reflect.apply(nativeSignalAborted, state.lifetimeSignal, []) || Date.now() >= state.scope.deadlineAt + || Date.now() >= state.original.grant.expiresAt || performance.now() >= state.end) throw error(state) + } + async function check(state: State) { + live(state); const current = await inspect(state.originalCap, state.scope); live(state) + if (!isDeepStrictEqual(current, state.original)) throw error(state) + if (state.external) { await state.external.assertCurrent(state.original, state.scope); live(state) } + } + async function checkSessionLock(state: State) { + await check(state) + const held = await state.query!(HELD_SQL, undefined, { signal: state.scope.signal, timeoutMs: Math.max(1, state.scope.deadlineAt - Date.now()) }) + await check(state) + if (held.rows.length !== 1 || held.rows[0].held !== true || held.rows[0].backend_pid !== state.backend) throw error(state) + } + async function verifyOwnership(state: State, fence: BandwidthControlDispatchFence) { + live(state) + // Opening must acquire the lock and physical PID before checking ownership. + // Every subsequent configured-source driver dispatch checks BOTH facts on + // the already-held raw lease; never recursively call the source connection. + if (state.phase === "opening") return + if (state.backend === undefined) throw error(state) + const held = await fence.query(HELD_SQL) + live(state) + if (held.rows.length !== 1 || held.rows[0].held !== true || held.rows[0].backend_pid !== state.backend) throw error(state) + } + async function owned(state: State, work: () => Promise): Promise { + live(state) + try { return await Promise.race([Promise.resolve().then(async () => { live(state); return work() }), state.stopped]) } + catch { const failure = error(state); retire(state); throw failure } + } + const windowState = (capability: unknown) => { + const state = capability && typeof capability === "object" ? states.get(capability) : undefined + if (!state) return refuse() + live(state); return state + } + return Object.freeze({ + async open(originalCap: BandwidthOperatorOriginalAuthority, rawScope: BandwidthOperatorScope): Promise { + if (closed) return refuse() + const input = scopeSnapshot(rawScope), wall = Date.now(), mono = performance.now() + const original = await inspect(originalCap, input) + const originalSignal = input.signal && original.signal ? AbortSignal.any([input.signal, original.signal]) : input.signal ?? original.signal + const controller = new AbortController(), scope = Object.freeze({ deadlineAt: Math.min(input.deadlineAt, original.grant.expiresAt, wall + 5000), signal: controller.signal }) + const end = Math.min(original.end, mono + Math.max(0, scope.deadlineAt - wall)) + let reject!: (error: Error) => void + const stopped = new Promise((_resolve, failure) => { reject = failure }); void stopped.catch(() => {}) + const state: State = { original, originalCap, scope, end, controller, phase: "opening", commitStarted: false, commitAck: false, busy: false, + timer: undefined as unknown as ReturnType, inputAbort: () => {}, originalSignal, stopReject: reject, stopped } + state.inputAbort = () => stop(state); originalSignal?.addEventListener("abort", state.inputAbort, { once: true }) + state.timer = setTimeout(() => stop(state), Math.max(1, end - performance.now())); active.add(state) + return owned(state, async () => { + await check(state) + const external = await acquireExternal(original, scope) + if (state.controller.signal.aborted) { external.close(); throw error(state) } + state.external = { assertCurrent: external.assertCurrent.bind(external), close: external.close.bind(external) }; state.externalClose = state.external.close + await check(state) + const session = openWithOwnershipFence + ? await openWithOwnershipFence(scope, () => check(state), fence => verifyOwnership(state, fence)) + : await openSource(scope, () => check(state)) + if (state.controller.signal.aborted) { session.close(); throw error(state) } + state.session = session; state.sessionClose = session.close.bind(session) + const connection = session.connection + if (types.isProxy(connection)) throw error(state) + const lifetime = Object.getOwnPropertyDescriptor(connection, "lifetimeSignal") + if (lifetime) { + if (!("value" in lifetime)) throw error(state) + if (lifetime.value !== undefined) { + const signal: unknown = lifetime.value + if (!signal || types.isProxy(signal) || !(signal instanceof AbortSignal)) throw error(state) + // Native brand check/read; do not evaluate a subclass/own getter. + Reflect.apply(nativeSignalAborted, signal, []) + state.lifetimeSignal = signal + state.lifetimeAbort = () => { if (state.phase !== "terminal") stop(state) } + Reflect.apply(EventTarget.prototype.addEventListener, signal, ["abort", state.lifetimeAbort, { once: true }]) + if (Reflect.apply(nativeSignalAborted, signal, [])) { stop(state); throw error(state) } + } + } + live(state); state.query = connection.query.bind(connection) + state.binding = session.binding.bind(session); state.keyedProof = session.keyedProof.bind(session) + state.keyedProofs = session.keyedProofs?.bind(session) + await check(state); const root = await state.binding(); await check(state) + if (root.installationId !== original.grant.installationId || root.issuerKeySha256 !== original.grant.issuerKeySha256 || root.quiescenceProof !== original.grant.quiescenceProof) throw error(state) + const locked = await state.query(LOCK_SQL, undefined, { signal: scope.signal, timeoutMs: Math.max(1, scope.deadlineAt - Date.now()) }); await check(state) + if (locked.rows.length !== 1 || locked.rows[0].acquired !== true) throw error(state) + const backend = await state.query(BACKEND_SQL, undefined, { signal: scope.signal, timeoutMs: Math.max(1, scope.deadlineAt - Date.now()) }); await check(state) + if (backend.rows.length !== 1 || !Number.isSafeInteger(backend.rows[0].backend_pid) || Number(backend.rows[0].backend_pid) <= 0) throw error(state) + state.backend = Number(backend.rows[0].backend_pid); state.phase = "held" + const capability = Object.freeze(Object.create(null)) as BandwidthOperatorWriterWindow; states.set(capability, state); return capability + }) + }, + /** Read-only quiescence check suitable for a fixed coordinator adapter. + * It NEVER performs nested session SQL inside source dispatch guards. */ + async verify(capability: unknown, originalCap: BandwidthOperatorOriginalAuthority, rawScope: BandwidthOperatorScope) { + const state = windowState(capability), requested = scopeSnapshot(rawScope) + if (state.originalCap !== originalCap || requested.signal?.aborted || Date.now() >= requested.deadlineAt || requested.deadlineAt > state.scope.deadlineAt) throw error(state) + // A shorter verifier call also owns a real bounded lifetime; its abort + // cannot disappear just because the enclosing window has time remaining. + const end = Math.min(state.end, performance.now() + Math.max(0, requested.deadlineAt - Date.now())) + const stopRequested = () => stop(state), timer = setTimeout(stopRequested, Math.max(1, end - performance.now())) + requested.signal?.addEventListener("abort", stopRequested, { once: true }) + try { return await owned(state, () => check(state)) } + finally { clearTimeout(timer); requested.signal?.removeEventListener("abort", stopRequested) } + }, + /** Fixed server-owned credential source guard. The metadata method is NOT + * held-owner/Q proof; a separate public SQL metadata lease supplies that. */ + createPeerSourceGuard(capability: unknown, originalCap: BandwidthOperatorOriginalAuthority): BandwidthOperatorPeerSourceGuard { + const state = windowState(capability) + try { if (state.originalCap !== originalCap || !["held", "registry"].includes(state.phase) + || !state.lifetimeSignal || Reflect.apply(nativeSignalAborted, state.lifetimeSignal, [])) throw error(state) } + catch { stop(state); throw error(state) } + const original = state.original, originalScope = state.scope, originalEnd = state.end + const expected = peerBindingSnapshot({ peerRef: original.grant.peerRef, sourceProof: original.grant.sourceProof, + authorityGeneration: original.grant.authorityGeneration, routingEndpointEpoch: original.grant.routingEndpointEpoch }) + const inspectOriginal = inspect, assertHeld = () => checkSessionLock(state) + let busy = false + const verifySource = async (read: boolean, rawBinding: BandwidthOperatorPeerSourceBinding, rawScope: Readonly) => { + const enteredWall = Date.now(), enteredMono = performance.now() + let timer: ReturnType | undefined, requestedSignal: AbortSignal | undefined, aborted: (() => boolean) | undefined + const stopRequested = () => stop(state) + try { + live(state); if (busy || !["held", "registry"].includes(state.phase)) throw error(state); busy = true + const binding = peerBindingSnapshot(rawBinding) + if (!isDeepStrictEqual(binding, expected) || !rawScope || types.isProxy(rawScope) || Object.getPrototypeOf(rawScope) !== Object.prototype) throw error(state) + const fields = Object.getOwnPropertyDescriptors(rawScope) + if (Reflect.ownKeys(rawScope).some(key => typeof key !== "string" || !["deadlineAt", "signal"].includes(key)) + || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) throw error(state) + const deadline: unknown = fields.deadlineAt?.value, signal: unknown = fields.signal?.value + if (typeof deadline !== "number" || !Number.isSafeInteger(deadline) || signal !== undefined && (types.isProxy(signal) || !(signal instanceof AbortSignal))) throw error(state) + requestedSignal = signal as AbortSignal | undefined + const nativeAborted = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "aborted")!.get! + aborted = () => requestedSignal ? Reflect.apply(nativeAborted, requestedSignal, []) : false + if (aborted()) throw error(state) + const deadlineAt = Math.min(deadline, originalScope.deadlineAt, original.grant.expiresAt) + const end = Math.min(originalEnd, enteredMono + Math.max(0, deadlineAt - enteredWall)) + const requested = Object.freeze({ deadlineAt, signal: originalScope.signal }) + const current = () => { live(state); if (!peerGuardsBrand.has(guard) || aborted!() || Date.now() < enteredWall || Date.now() >= deadlineAt + || performance.now() >= end || !["held", "registry"].includes(state.phase)) throw error(state) } + current(); timer = setTimeout(stopRequested, Math.max(1, end - performance.now())) + if (requestedSignal) Reflect.apply(EventTarget.prototype.addEventListener, requestedSignal, ["abort", stopRequested, { once: true }]) + const originalCurrent = async () => { + current(); const value = await inspectOriginal(originalCap, requested); current() + if (!isDeepStrictEqual(value, original)) throw error(state) + } + await owned(state, async () => { + await originalCurrent() + if (read) { if (state.busy) throw error(state); await assertHeld(); current() } + await originalCurrent(); current() + }) + } catch { stop(state); throw error(state) } + finally { + busy = false; if (timer) clearTimeout(timer) + if (requestedSignal) Reflect.apply(EventTarget.prototype.removeEventListener, requestedSignal, ["abort", stopRequested]) + } + } + const guard = Object.freeze({ installationId: original.grant.installationId, + verifyRead: (binding: BandwidthOperatorPeerSourceBinding, scope: Readonly) => verifySource(true, binding, scope), + verifyMetadata: (binding: BandwidthOperatorPeerSourceBinding, scope: Readonly) => verifySource(false, binding, scope) }) as BandwidthOperatorPeerSourceGuard + peerGuardsBrand.add(guard); (state.peerGuards ??= new Set()).add(guard); return guard + }, + async register(capability: unknown): Promise { + const state = windowState(capability) + if (state.phase !== "held") throw error(state) + state.phase = "registry" + return owned(state, async () => { + const io = async (work: () => Promise) => { + live(state); if (state.busy || state.phase !== "registry") throw error(state) + state.busy = true + try { + // Configured sources enforce lock/backend ownership within their + // existing raw before/after driver fences. Legacy structural sources + // retain the original outer SQL checks, with no weaker fallback. + if (openWithOwnershipFence) await check(state); else await checkSessionLock(state) + const result = await work() + if (openWithOwnershipFence) await check(state); else await checkSessionLock(state) + return result + } finally { state.busy = false } + } + const borrowed: BandwidthOperatorRegistryBorrow = Object.freeze({ + binding: () => io(() => state.binding!()), keyedProof: (purpose: string, value: unknown) => io(() => state.keyedProof!(purpose, value)), + ...(state.keyedProofs ? { keyedProofs: (inputs: readonly { purpose: string; value: unknown }[]) => io(() => state.keyedProofs!(inputs)) } : {}), + connection: Object.freeze({ + query: (text: string, values?: unknown[], options?: { signal?: AbortSignal; timeoutMs: number }) => io(async () => { + if (text.trim() === "COMMIT") state.commitStarted = true + const result = await state.query!(text, values, options) + if (text.trim() === "COMMIT" && "command" in result && result.command === "COMMIT") state.commitAck = true + return result + }), + release: () => { + if (state.phase === "terminal") return + if (state.busy || !state.commitAck || state.phase !== "registry") { stop(state); return } + // This is the existing registry finally release(true). The OWNER + // keeps the physical backend alive for one final read-only check. + state.phase = "logical-release" + }, + }), + }) + const result = await register(state.original, borrowed, state.scope) + live(state); if (!state.commitAck || state.phase !== "logical-release") throw error(state) + state.phase = "final-read" + await check(state) // ONLY original signed authority + held external fence; no driver SQL. + live(state); retire(state); return result + }) + }, + close() { closed = true; for (const state of [...active]) stop(state) }, + }) +} diff --git a/backend/src/services/bandwidth-peer-direct-observation.test.ts b/backend/src/services/bandwidth-peer-direct-observation.test.ts new file mode 100644 index 00000000..1574543b --- /dev/null +++ b/backend/src/services/bandwidth-peer-direct-observation.test.ts @@ -0,0 +1,150 @@ +/** Actual localhost authenticated GET and fixed read-only /get; no DB/CHR. */ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { once } from "node:events" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { test } from "node:test" +import { createBandwidthPeerDirectObservationReader, BandwidthPeerDirectObservationError, type BandwidthPeerDirectObservationPlan } from "./bandwidth-peer-direct-observation.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" + +const scope = (duration = 4000) => ({ deadlineAt: Date.now() + duration }) +test("bounded direct native profile purpose and effective defaults", async t => { + const { pool, closePool } = await import("../db/index.js") + const query = pool.query, connect = pool.connect; let databaseCalls = 0 + pool.query = (() => { databaseCalls++; throw Error("No DB") }) as typeof pool.query + pool.connect = (() => { databaseCalls++; throw Error("No DB") }) as typeof pool.connect + async function fixture(ipv6 = false) { + const sourcePeer = { kind: "server" as const, serverId: 71 }, remotePeer = { kind: "external" as const, enrollmentId: randomUUID() } + const requests: { path: string; method: string; body?: Record; authenticated: boolean }[] = [] + const payload: Record = { + "/interface": [{ ".id": "*i", name: "ether1", type: "ether", "rx-byte": "1" }], + "/routing/table": [{ ".id": "*t", name: "blue" }], + "/ip/vrf": [{ ".id": "*v", name: "blue", interfaces: "ether1" }], + "/routing/rule": [], "/ip/firewall/nat": [], "/ipv6/firewall/nat": [], "/ip/firewall/mangle": [], "/ipv6/firewall/mangle": [], + "/ip/address": [{ ".id": "*a", address: "192.0.2.2/24", interface: "ether1" }], + "/ipv6/address": [{ ".id": "*a", address: "fe80::2/64", interface: "ether1" }], + } + let scalarHook: ((resource: string, body: Record) => unknown) | undefined, requestHook: ((resource: string) => void) | undefined, licenseChanged = false, revoked = false + const router = createServer((req, res) => { + const resource = req.url!.slice(5), authenticated = req.headers.authorization === "Basic " + Buffer.from("profile-user:profile-private-password").toString("base64") + if (req.method === "POST") { + let bytes = ""; req.on("data", chunk => { bytes += chunk }); req.on("end", () => { + const body = JSON.parse(bytes) as Record + requests.push({ path: req.url!, method: "POST", body, authenticated }); requestHook?.(resource) + res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify(scalarHook ? scalarHook(resource, body) : { ret: body["value-name"] === "fib" ? "true" : "false" })) + }); return + } + requests.push({ path: req.url!, method: req.method!, authenticated }); requestHook?.(resource) + res.writeHead(200, { "content-type": "application/json" }) + if (resource === "/system/license") { res.end(JSON.stringify({ "system-id": licenseChanged ? "NEW-LICENSE" : "GENUINE-PROFILE-LICENSE" })); return } + if (resource === "/routing/route") { res.end("[]"); return } + res.end(JSON.stringify(payload[resource] ?? [])) + }) + router.listen(0, "127.0.0.1"); await once(router, "listening"); const address = router.address(); assert(address && typeof address !== "string") + const endpoint = { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "profile-user", password: "profile-private-password", routingEndpointEpoch: randomUUID() } + const snapshots: BandwidthPeerSourceSnapshot[] = [sourcePeer, remotePeer].map(peerRef => ({ peerRef, enabled: true, sourceProof: "a".repeat(64), authorityGeneration: "original-source-grant", endpoint: { ...endpoint } })) + const trusted = { async read(peerRef: typeof sourcePeer | typeof remotePeer) { if (revoked) throw Error(endpoint.password); return snapshots.find(value => value.peerRef.kind === peerRef.kind) } } + const reader = createBandwidthPeerDirectObservationReader(trusted), wireAddress = ipv6 ? "fe80::2%ether1@blue" : "192.0.2.2@blue" + const source: BandwidthPeerDirectObservationPlan = { role: "source", peerRef: sourcePeer, wireAddress, sourceInterface: "ether1" }, remote: BandwidthPeerDirectObservationPlan = { role: "remote", peerRef: remotePeer, wireAddress, remoteInterface: "ether1" } + return { reader, trusted, source, remote, payload, requests, snapshots, scalarHook: (value: typeof scalarHook) => { scalarHook = value }, requestHook: (value: typeof requestHook) => { requestHook = value }, changeLicense: () => { licenseChanged = true }, revoke: () => { revoked = true }, + async dispose() { reader.close(); router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) } } + } + try { + await t.test("12 pair GETs avoid the RIB and other family; omitted effective flags use only bounded real /get", async () => { + for (const ipv6 of [false, true]) { const f = await fixture(ipv6); try { + const sourceToken = await f.reader.observe(f.source, scope()), source = await f.reader.inspect(sourceToken, scope()), remote = await f.reader.inspect(await f.reader.observe(f.remote, scope()), scope()) + assert.equal(source.acquisition.catalogueGets, 6); assert.equal(remote.acquisition.catalogueGets, 2) + assert.equal(source.acquisition.effectiveDefaultGets, 3); assert.equal(remote.acquisition.effectiveDefaultGets, 3) + assert.equal(f.requests.filter(request => request.method === "GET").length, 12) + assert.equal(source.facts.orderedVrfOwner, "blue"); assert.equal(source.facts.currentPolicyAllDisabled, true); assert.equal(remote.facts.address, ipv6 ? "fe80::2" : "192.0.2.2") + assert(f.requests.every(request => request.authenticated)); assert(!f.requests.some(request => request.path.includes("/routing/route"))) + const posts = f.requests.filter(request => request.method === "POST"); assert(posts.every(request => request.path.endsWith("/get") && /^\*[a-z0-9]+$/i.test(request.body![".id"]) && ["disabled", "invalid", "fib"].includes(request.body!["value-name"]))) + assert.equal(source.nativeDispatchAdmissible, false); assert.equal(source.effectiveRoutingVerified, false); assert.equal(source.effectiveNatVerified, false); assert.equal(source.connectionTrackingVerified, false) + assert.equal(remote.configurationFenceHeld, false); assert(Object.isFrozen(source.facts)); assert(!JSON.stringify(source).includes("private-password")) + await assert.rejects(f.reader.inspect({ ...sourceToken }, scope()), BandwidthPeerDirectObservationError) + const foreign = createBandwidthPeerDirectObservationReader(f.trusted) + try { await assert.rejects(foreign.inspect(sourceToken, scope()), BandwidthPeerDirectObservationError) } finally { foreign.close() } + } finally { await f.dispose() } } + }) + await t.test("every active or unknown routing/NAT/mangle rule refuses; measured disabled rules retain complete configuration identity", async () => { + for (const resource of ["/routing/rule", "/ip/firewall/nat", "/ip/firewall/mangle"]) for (const disabled of ["false", "unknown", undefined]) { const f = await fixture(); try { + f.payload[resource] = [{ ".id": "*p", chain: "apparently-irrelevant", disabled, "unknown-matcher": "cannot-be-ignored" }] + if (disabled === undefined) { delete (f.payload[resource] as Record[])[0].disabled; f.scalarHook((_resource, body) => ({ ret: body["value-name"] === "fib" ? "true" : "false" })) } + await assert.rejects(f.reader.observe(f.source, scope()), BandwidthPeerDirectObservationError) + } finally { await f.dispose() } } + const f = await fixture(); try { + f.payload["/ip/firewall/nat"] = [{ ".id": "*p", disabled: "true", "unknown-matcher": "first" }] + const first = await f.reader.inspect(await f.reader.observe(f.source, scope()), scope()) + ;(f.payload["/ip/firewall/nat"] as Record[])[0]["unknown-matcher"] = "second" + const second = await f.reader.inspect(await f.reader.observe(f.source, scope()), scope()); assert.notEqual(first.snapshotSha256, second.snapshotSha256) + } finally { await f.dispose() } + }) + await t.test("empty printed table fib is measured through fixed /get; false or malformed effective values refuse", async () => { + const f = await fixture(); try { + f.payload["/routing/table"] = [{ ".id": "*t", name: "blue", fib: "", invalid: "false", dynamic: "true" }] + const source = await f.reader.inspect(await f.reader.observe(f.source, scope()), scope()) + const remote = await f.reader.inspect(await f.reader.observe(f.remote, scope()), scope()) + assert.equal(source.acquisition.catalogueGets + remote.acquisition.catalogueGets + source.acquisition.licenseGets + remote.acquisition.licenseGets, 12) + assert.equal(source.acquisition.effectiveDefaultGets, 3) + const fibReads = f.requests.filter(request => request.path === "/rest/routing/table/get") + assert.equal(fibReads.length, 1); assert.deepEqual(fibReads[0].body, { ".id": "*t", "value-name": "fib" }) + ;(f.payload["/routing/table"] as Record[])[0].fib = "true" + const explicit = await f.reader.inspect(await f.reader.observe(f.source, scope()), scope()) + assert.equal(explicit.acquisition.effectiveDefaultGets, 2, "recognized printed fib avoids another getter") + assert.notEqual(source.snapshotSha256, explicit.snapshotSha256, "raw empty print plus its measured getter must remain bound") + } finally { await f.dispose() } + for (const ret of ["false", "", null, "unknown"]) { const denied = await fixture(); try { + denied.payload["/routing/table"] = [{ ".id": "*t", name: "blue", fib: "" }] + denied.scalarHook((_resource, body) => ({ ret: body["value-name"] === "fib" ? ret : "false" })) + await assert.rejects(denied.reader.observe(denied.source, scope()), BandwidthPeerDirectObservationError) + assert.equal(denied.requests.filter(request => request.path === "/rest/routing/table/get").length, 1) + } finally { await denied.dispose() } } + }) + await t.test("unknown/malformed/oversized effective getter and bounded hydration exhaustion never synthesize defaults", async () => { + for (const ret of ["", "unknown", null, "x".repeat(5000)]) { const f = await fixture(); try { + f.scalarHook(() => ({ ret })); await assert.rejects(f.reader.observe(f.remote, scope()), BandwidthPeerDirectObservationError) + } finally { await f.dispose() } } + const f = await fixture(); try { + f.payload["/routing/rule"] = Array.from({ length: 40 }, (_, index) => ({ ".id": "*" + index.toString(16) })) + f.scalarHook((_resource, body) => ({ ret: body["value-name"] === "fib" || _resource === "/routing/rule/get" ? "true" : "false" })) + await assert.rejects(f.reader.observe(f.source, scope()), BandwidthPeerDirectObservationError) + assert.equal(f.requests.filter(request => request.method === "POST").length, 32) + } finally { await f.dispose() } + }) + await t.test("VRF interface-list/ambiguous ownership, invalid address and external source execution refuse", async () => { + const f = await fixture(true); try { + const before = f.requests.length + for (const wireAddress of ["peer.example@blue", "fe80::2", "192.0.2.2%ether1", "192.0.2.2@@blue", "http://192.0.2.2/"]) await assert.rejects(f.reader.observe({ ...f.source, wireAddress }, scope()), BandwidthPeerDirectObservationError) + assert.equal(f.requests.length, before) + f.payload["/ip/vrf"] = [{ ".id": "*v", name: "blue", interfaces: "unresolved-list", disabled: "false" }] + await assert.rejects(f.reader.observe(f.source, scope()), BandwidthPeerDirectObservationError) + f.scalarHook((_resource, body) => ({ ret: body["value-name"] === "invalid" ? "true" : "false" })); await assert.rejects(f.reader.observe(f.remote, scope()), BandwidthPeerDirectObservationError) + await assert.rejects(f.reader.observe({ ...f.source, peerRef: f.remote.peerRef }, scope())) + } finally { await f.dispose() } + }) + await t.test("same endpoint license replacement, original source drift during defaults and pre-credential revoke refuse", async () => { + const changed = await fixture(); try { + changed.requestHook(resource => { if (resource === "/ip/address") changed.changeLicense() }) + await assert.rejects(changed.reader.observe(changed.remote, scope()), BandwidthPeerDirectObservationError) + } finally { await changed.dispose() } + const drift = await fixture(); try { + drift.requestHook(resource => { if (resource.endsWith("/get")) drift.snapshots[1].endpoint.routingEndpointEpoch = randomUUID() }) + await assert.rejects(drift.reader.observe(drift.remote, scope()), BandwidthPeerDirectObservationError) + } finally { await drift.dispose() } + const revoked = await fixture(); try { + revoked.revoke(); await assert.rejects(revoked.reader.observe(revoked.source, scope()), BandwidthPeerDirectObservationError); assert.equal(revoked.requests.length, 0) + } finally { await revoked.dispose() } + }) + await t.test("source lifetime cannot be renewed; telemetry cannot change configuration fingerprint", async () => { + const f = await fixture(); try { + const controller = new AbortController(), first = await f.reader.inspect(await f.reader.observe(f.remote, { ...scope(), signal: controller.signal }), scope()) + ;(f.payload["/interface"] as Record[])[0]["rx-byte"] = "999999999" + const second = await f.reader.inspect(await f.reader.observe(f.remote, scope()), scope()); assert.equal(first.snapshotSha256, second.snapshotSha256) + const token = await f.reader.observe(f.remote, scope(200)); await delay(210); await assert.rejects(f.reader.inspect(token, scope()), BandwidthPeerDirectObservationError) + controller.abort(); assert.equal(first.nativeDispatchAdmissible, false) + } finally { await f.dispose() } + }) + assert.equal(databaseCalls, 0) + } finally { pool.query = query; pool.connect = connect; await closePool() } +}) diff --git a/backend/src/services/bandwidth-peer-direct-observation.ts b/backend/src/services/bandwidth-peer-direct-observation.ts new file mode 100644 index 00000000..f0604ca4 --- /dev/null +++ b/backend/src/services/bandwidth-peer-direct-observation.ts @@ -0,0 +1,214 @@ +/** Optional fixed direct-peer observation component. No RIB/BGP dump, route evaluator, + * mutable native command, DNS/NAT certification or dispatch authority. */ +import http from "node:http" +import https from "node:https" +import { createHash } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import { createBandwidthPeerObservationReader, type BandwidthPeerTrustedSource, type BandwidthPeerSourceSnapshot, type BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import { parseBandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { isIP } from "node:net" + +const MAX_GETTERS = 32, MAX_ROWS = 2048 +type Row = Readonly> +const label = z.string().min(1).max(128).regex(/^[^\u0000-\u001f\u007f%@]+$/) +const ref = z.unknown().transform(parseBandwidthPeerRef) +const planSchema = z.discriminatedUnion("role", [ + z.object({ role: z.literal("source"), peerRef: ref, wireAddress: z.string().min(1).max(1024), sourceInterface: label }).strict(), + z.object({ role: z.literal("remote"), peerRef: ref, wireAddress: z.string().min(1).max(1024), remoteInterface: label }).strict(), +]) +export type BandwidthPeerDirectObservationPlan = z.infer +const endpointSchema = z.object({ host: z.string().min(1).max(1000).regex(/^[^\s/\\\u0000-\u001f\u007f]+$/), port: z.number().int().min(1).max(65535), useSsl: z.boolean(), verifySsl: z.boolean(), username: z.string().max(1000), password: z.string().max(65536), routingEndpointEpoch: z.string().uuid() }).strict() +const sourceSchema = z.object({ peerRef: ref, enabled: z.literal(true), sourceProof: z.string().regex(/^[a-f0-9]{64}$/), authorityGeneration: z.string().min(1).max(200), endpoint: endpointSchema }).strict() +export class BandwidthPeerDirectObservationError extends Error { + readonly code = "BANDWIDTH_PEER_DIRECT_OBSERVATION_REFUSED" + constructor() { super("Direct peer observation refused") } +} +const fail = (): never => { throw new BandwidthPeerDirectObservationError() } +/** Fixed literal grammar, separate from a mapping grant or command builder. + * Exact input is retained; this parser never substitutes a management host. */ +export function parseBandwidthPeerDirectWireAddress(address: string) { + if (typeof address !== "string" || !address.length || address.length > 1024 || /[\u0000-\u001f\u007f]/.test(address)) return fail() + const tableParts = address.split("@"); if (tableParts.length > 2 || tableParts.some(part => !part.length)) fail() + const zoneParts = tableParts[0].split("%"); if (zoneParts.length > 2 || zoneParts.some(part => !part.length)) fail() + const host = zoneParts[0], family = isIP(host), zone = zoneParts[1] ?? null, routingTable = tableParts[1] ?? "main" + if (!family || zone && family !== 6 || Buffer.byteLength(routingTable) > 128 || zone && Buffer.byteLength(zone) > 128 || /[%@]/.test(routingTable)) fail() + if (family === 6 && /^fe[89ab]/i.test(host) && !zone) fail() + return Object.freeze({ address, host, family: family === 4 ? "ipv4" as const : "ipv6" as const, routingTable, zone }) +} +function freeze(value: T): T { if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } return value } +function copy(raw: unknown, maxBytes = 1048576): unknown { + let nodes = 0 + const walk = (value: unknown, depth = 0): unknown => { + if (++nodes > 20000 || depth > 8) return fail() + if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return value + if (!value || typeof value !== "object") return fail() + const array = Array.isArray(value), fields = Object.getOwnPropertyDescriptors(value) + if (Object.getPrototypeOf(value) !== (array ? Array.prototype : Object.prototype) || Reflect.ownKeys(value).some(key => typeof key !== "string")) fail() + if (array) { + const length = fields.length.value as number + if (length > MAX_ROWS || Object.keys(fields).length !== length + 1) fail() + return Object.freeze(Array.from({ length }, (_, index) => { const item = fields[String(index)]; if (!item || !("value" in item) || !item.enumerable) return fail(); return walk(item.value, depth + 1) })) + } + const result: Record = {} + for (const [key, item] of Object.entries(fields)) { if (!("value" in item) || !item.enumerable || key === "__proto__" || Buffer.byteLength(key) > 128) fail(); Object.defineProperty(result, key, { value: walk(item.value, depth + 1), enumerable: true }) } + return Object.freeze(result) + } + const value = walk(raw); if (Buffer.byteLength(JSON.stringify(value)) > maxBytes) fail(); return value +} +const flag = (value: unknown) => value === true || value === "true" || value === "yes" ? true : value === false || value === "false" || value === "no" ? false : undefined +function rows(raw: unknown, maximum: number): readonly Row[] { + const value = copy(raw) + if (!Array.isArray(value) || value.length > maximum || value.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"])) || new Set(value.map(row => row[".id"])).size !== value.length) return fail() + return value as readonly Row[] +} +function unique(values: readonly Row[], field: string, wanted: string): Row { const found = values.filter(row => row[field] === wanted); if (found.length !== 1) return fail(); return found[0] } +function sameAddress(left: string, right: string) { try { return new URL("http://[" + left + "]").hostname === new URL("http://[" + right + "]").hostname } catch { return left === right } } +declare const profileBrand: unique symbol +export type BandwidthPeerDirectObservation = Readonly<{ kind: "routeros-peer-direct-observation"; [profileBrand]: true }> +export interface BandwidthPeerDirectObservationEvidence { + readonly profile: "bandwidth-peer-direct-observation-v1" + readonly plan: Readonly + readonly identity: Readonly + readonly observedAt: number; readonly expiresAt: number + readonly snapshotSha256: string + readonly facts: Readonly<{ address?: string; interface: string; routingTable?: string; orderedVrfOwner?: string; currentPolicyAllDisabled?: true }> + readonly acquisition: Readonly<{ catalogueGets: number; effectiveDefaultGets: number; licenseGets: 2; fullRibRead: false }> + readonly effectiveRoutingVerified: false; readonly effectiveNatVerified: false + readonly connectionTrackingVerified: false; readonly configurationFenceHeld: false; readonly nativeDispatchAdmissible: false +} +type Origin = { source: Readonly; evidence: Readonly; end: number; signal?: AbortSignal } +export function createBandwidthPeerDirectObservationReader(trustedSource: BandwidthPeerTrustedSource) { + if (!trustedSource || typeof trustedSource.read !== "function") fail() + const read = trustedSource.read.bind(trustedSource), licenses = createBandwidthPeerObservationReader({ read }), origins = new WeakMap(), windows = new Set() + let closed = false + async function operation(raw: BandwidthScope, origin: Origin | undefined, work: (owned: { scope: Readonly; live(): void; wait(work: () => Promise): Promise; end: number; external?: AbortSignal }) => Promise): Promise { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype) return fail() + const descriptors = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== Object.keys(descriptors).length || Object.values(descriptors).some(item => !("value" in item) || !item.enumerable) || Object.keys(descriptors).some(key => !["deadlineAt", "signal"].includes(key))) fail() + const deadline = descriptors.deadlineAt?.value as number, external = descriptors.signal?.value as AbortSignal | undefined + if (!Number.isSafeInteger(deadline) || external !== undefined && !(external instanceof AbortSignal)) fail() + const wall = Date.now(), mono = performance.now(), deadlineAt = Math.min(deadline, wall + 5000, origin?.evidence.expiresAt ?? Infinity), end = Math.min(mono + Math.max(0, deadlineAt - wall), origin?.end ?? Infinity) + const controller = new AbortController(), abort = () => controller.abort(), scope = Object.freeze({ deadlineAt, signal: controller.signal }) + const live = () => { if (closed || controller.signal.aborted || external?.aborted || origin?.signal?.aborted || Date.now() >= deadlineAt || performance.now() >= end) fail() } + live(); windows.add(controller); external?.addEventListener("abort", abort, { once: true }); origin?.signal?.addEventListener("abort", abort, { once: true }); const timer = setTimeout(abort, Math.max(1, end - mono)) + const wait = async (fn: () => Promise): Promise => { + live(); let cancel!: () => void + const stopped = new Promise((_resolve, reject) => { cancel = () => reject(new BandwidthPeerDirectObservationError()); controller.signal.addEventListener("abort", cancel, { once: true }) }) + try { const value = await Promise.race([Promise.resolve().then(() => { live(); return fn() }), stopped]); live(); return value } finally { controller.signal.removeEventListener("abort", cancel) } + } + try { return await work({ scope, live, wait, end, external }) } catch { throw new BandwidthPeerDirectObservationError() } + finally { clearTimeout(timer); windows.delete(controller); external?.removeEventListener("abort", abort); origin?.signal?.removeEventListener("abort", abort); controller.abort() } + } + return Object.freeze({ + async observe(rawPlan: BandwidthPeerDirectObservationPlan, rawScope: BandwidthScope): Promise { + const plan = freeze(planSchema.parse(copy(rawPlan))), wire = parseBandwidthPeerDirectWireAddress(plan.wireAddress) + if (plan.role === "source" && plan.peerRef.kind !== "server" || plan.role === "source" && wire.zone && wire.zone !== plan.sourceInterface) fail() + return operation(rawScope, undefined, async owned => { + const before = freeze(sourceSchema.parse(copy(await owned.wait(() => read(plan.peerRef, owned.scope))))) + if (!isDeepStrictEqual(before.peerRef, plan.peerRef)) fail() + const fresh = async () => { if (!isDeepStrictEqual(sourceSchema.parse(copy(await owned.wait(() => read(plan.peerRef, owned.scope)))), before)) fail() } + const identityToken = await owned.wait(() => licenses.observe(plan.peerRef, owned.scope)); await fresh() + const identity = await owned.wait(() => licenses.inspect(identityToken, owned.scope)); await fresh() + const { MikrotikClient } = await owned.wait(() => import("./mikrotik.js")); await fresh() + const client = new MikrotikClient(before.endpoint), snapshots: unknown[] = [], defaults: unknown[] = [] + let calls = 0, getters = 0, totalRows = 0, totalBytes = 0 + const get = async (resource: string, maximum: number) => { + await fresh(); calls++ + const value = rows(await owned.wait(() => client.get(resource, Math.max(1, owned.scope.deadlineAt - Date.now()), owned.scope.signal, resource === "/interface" ? 262144 : 131072, fresh)), maximum) + await fresh(); totalRows += value.length; totalBytes += Buffer.byteLength(JSON.stringify(value)) + if (totalRows > MAX_ROWS || totalBytes > 1048576) fail() + // Interface traffic counters do not describe the mapping. Preserve + // every policy property; only known interface telemetry is excluded. + const stable = resource === "/interface" ? value.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !/^(?:rx-|tx-|fp-|last-link-|link-downs$|driver-)/.test(key)))) : value + snapshots.push({ resource, rows: stable }); return value + } + /** Read-only POST /get, with fixed resource/id/property vocabulary and a + * 4KiB streaming cap. MikrotikClient.post's generic 8MiB response bound + * is deliberately not used for these scalar defaults. */ + const scalar = async (resource: string, id: unknown, property: "disabled" | "invalid" | "fib") => { + const allowed = new Set(["/interface", "/ip/address", "/ipv6/address", "/ip/vrf", "/routing/table", "/routing/rule", "/ip/firewall/nat", "/ipv6/firewall/nat", "/ip/firewall/mangle", "/ipv6/firewall/mangle"]) + if (!allowed.has(resource) || typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id) || ++getters > MAX_GETTERS) fail() + await fresh() + const value = await owned.wait(() => new Promise((resolveValue, reject) => { + const endpoint = before.endpoint, transport = endpoint.useSsl ? https : http, body = JSON.stringify({ ".id": id, "value-name": property }) + const request = transport.request({ hostname: endpoint.host, port: endpoint.port, path: "/rest" + resource + "/get", method: "POST", signal: owned.scope.signal, rejectUnauthorized: endpoint.useSsl ? endpoint.verifySsl : undefined, + headers: { authorization: "Basic " + Buffer.from(endpoint.username + ":" + endpoint.password).toString("base64"), "content-type": "application/json", "content-length": Buffer.byteLength(body) } }, response => { + const chunks: Buffer[] = []; let bytes = 0 + response.on("error", reject); response.once("aborted", () => reject(new BandwidthPeerDirectObservationError())) + if (response.statusCode !== 200 || Number(response.headers["content-length"] ?? 0) > 4096) { response.destroy(); request.destroy(new BandwidthPeerDirectObservationError()); return } + response.on("data", (chunk: Buffer) => { bytes += chunk.length; if (bytes > 4096) { response.destroy(); request.destroy(new BandwidthPeerDirectObservationError()) } else chunks.push(chunk) }) + response.on("end", () => { try { resolveValue(JSON.parse(Buffer.concat(chunks).toString("utf8"))) } catch { reject(new BandwidthPeerDirectObservationError()) } }) + }) + const timer = setTimeout(() => request.destroy(new BandwidthPeerDirectObservationError()), Math.max(1, owned.scope.deadlineAt - Date.now())) + request.once("close", () => clearTimeout(timer)); request.once("error", reject); request.end(body) + })) + await fresh(); const parsed = copy(value, 4096) + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed) || Object.keys(parsed).join() !== "ret") fail() + const ret = flag((parsed as Record).ret); if (ret === undefined) fail() + defaults.push({ resource, id, property, value: ret }); return ret + } + const boolean = async (resource: string, row: Row, property: "disabled" | "invalid" | "fib") => { + // Actual RouterOS main tables may print fib:"" while /get fib + // returns the effective boolean. Keep the raw empty value in the + // catalogue snapshot and separately record the measured getter. + if (resource === "/routing/table" && property === "fib" && row.fib === "") return scalar(resource, row[".id"], property) + if (Object.hasOwn(row, property)) { const value = flag(row[property]); if (value === undefined) return fail(); return value } + return scalar(resource, row[".id"], property) + } + const enabled = async (resource: string, row: Row) => { if (await boolean(resource, row, "disabled")) fail() } + const interfaces = await get("/interface", 1024), iface = plan.role === "source" ? plan.sourceInterface : plan.remoteInterface + await enabled("/interface", unique(interfaces, "name", iface)) + let facts: BandwidthPeerDirectObservationEvidence["facts"] + if (plan.role === "remote") { + const resource = wire.family === "ipv4" ? "/ip/address" : "/ipv6/address", addresses = await get(resource, 1024) + const selected = addresses.filter(row => row.interface === iface && typeof row.address === "string" && sameAddress(row.address.split("/")[0], wire.host)) + if (selected.length !== 1) fail() + await enabled(resource, selected[0]); if (await boolean(resource, selected[0], "invalid")) fail() + facts = Object.freeze({ address: wire.host, interface: iface }) + } else { + const tables = await get("/routing/table", 64), table = unique(tables, "name", wire.routingTable) + if (!await boolean("/routing/table", table, "fib")) fail() + for (const property of ["disabled", "invalid"] as const) if (Object.hasOwn(table, property) && flag(table[property]) !== false) fail() + const vrfs = await get("/ip/vrf", 64); let owner: string | undefined + const names = new Set(interfaces.map(row => row.name)) + for (const row of vrfs) { + if (await boolean("/ip/vrf", row, "disabled")) continue + if (Object.hasOwn(row, "invalid") && flag(row.invalid) !== false) fail() + if (typeof row.interfaces !== "string" || typeof row.name !== "string") return fail() + const members = row.interfaces.split(",").map(item => item.trim()) + if (members.some(item => !["all", "none"].includes(item) && !names.has(item))) fail() + if (members.includes("all") || members.includes(iface)) { owner = row.name; break } + } + if (owner !== wire.routingTable) fail() + const base = wire.family === "ipv4" ? "/ip" : "/ipv6" + for (const resource of ["/routing/rule", base + "/firewall/nat", base + "/firewall/mangle"]) { + const policy = await get(resource, 256) + // No matcher interpretation or irrelevant-chain shortcut. Disabled + // proves this CURRENT rule cannot apply; unknown/enabled refuses. + for (const row of policy) if (!await boolean(resource, row, "disabled")) fail() + } + facts = Object.freeze({ interface: iface, routingTable: wire.routingTable, orderedVrfOwner: owner, currentPolicyAllDisabled: true }) + } + const finalToken = await owned.wait(() => licenses.observe(plan.peerRef, owned.scope)); await fresh() + const finalIdentity = await owned.wait(() => licenses.inspect(finalToken, owned.scope)); await fresh() + const { observedAt: _a, expiresAt: _b, ...firstSubject } = identity, { observedAt: _c, expiresAt: _d, ...lastSubject } = finalIdentity + if (!isDeepStrictEqual(firstSubject, lastSubject)) fail() + const snapshotSha256 = createHash("sha256").update("mmapp-bandwidth-peer-direct-observation-v1:" + canonical({ plan, snapshots, defaults, facts })).digest("hex") + const evidence = freeze({ profile: "bandwidth-peer-direct-observation-v1" as const, plan, identity, observedAt: Date.now(), expiresAt: Math.min(owned.scope.deadlineAt, identity.expiresAt), snapshotSha256, facts, + acquisition: { catalogueGets: calls, effectiveDefaultGets: getters, licenseGets: 2 as const, fullRibRead: false as const }, effectiveRoutingVerified: false as const, effectiveNatVerified: false as const, + connectionTrackingVerified: false as const, configurationFenceHeld: false as const, nativeDispatchAdmissible: false as const }) + const token = Object.freeze({ kind: "routeros-peer-direct-observation" as const }) as BandwidthPeerDirectObservation + origins.set(token, { source: before, evidence, end: owned.end, signal: owned.external }); return token + }) + }, + async inspect(token: unknown, rawScope: BandwidthScope): Promise> { + const origin = token && typeof token === "object" ? origins.get(token) : undefined; if (!origin) return fail() + return operation(rawScope, origin, async owned => { if (!isDeepStrictEqual(sourceSchema.parse(copy(await owned.wait(() => read(origin.source.peerRef, owned.scope)))), origin.source)) fail(); owned.live(); return origin.evidence }) + }, + close() { closed = true; licenses.close(); for (const controller of windows) controller.abort() }, + }) +} diff --git a/backend/src/services/bandwidth-peer-execution-host-fence.test.ts b/backend/src/services/bandwidth-peer-execution-host-fence.test.ts new file mode 100644 index 00000000..e46a2524 --- /dev/null +++ b/backend/src/services/bandwidth-peer-execution-host-fence.test.ts @@ -0,0 +1,201 @@ +/** Actual host/admission/private permit/kernel/managed lock+audit and localhost + * HTTP. Original/registry/journal/SQL rows/mapping authority are synthetic HERE; + * no PostgreSQL or production mapping/ambient MCP app-lock claim is made. */ +import assert from "node:assert/strict" +import http from "node:http" +import { once } from "node:events" +import { createHash, randomUUID } from "node:crypto" +import { mock, test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import type { BandwidthBinding, BandwidthJournal, BandwidthScope, BandwidthTransition } from "./bandwidth-admission.js" +import type { BandwidthPeerExecutionHostDependencies, BandwidthPeerExecutionIntent } from "./bandwidth-peer-execution-host.js" +import type { BandwidthNativeDispatchLease } from "./bandwidth-native-dispatch.js" +const digest = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex") +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +let journal: BandwidthJournal +mock.module("./bandwidth-admission-storage.js", { namedExports: { createBandwidthAdmissionSqlJournal: (dependencies: { + authorize(binding: Readonly, scope: BandwidthScope, transition: BandwidthTransition): Promise +}): BandwidthJournal => ({ + async reserve(binding, scope) { await dependencies.authorize(binding, scope, "reserve"); return journal.reserve(binding, scope) }, + async arm(binding, scope) { await dependencies.authorize(binding, scope, "arm"); return journal.arm(binding, scope) }, + async complete(binding, evidence, scope) { await dependencies.authorize(binding, scope, "complete"); return journal.complete(binding, evidence, scope) }, + async releaseBeforeDispatch(binding, evidence, scope) { await dependencies.authorize(binding, scope, "release-before-dispatch"); return journal.releaseBeforeDispatch(binding, evidence, scope) }, + async quarantine(binding, scope) { await dependencies.authorize(binding, scope, "quarantine"); return journal.quarantine(binding, scope) }, +}) } }) +mock.module("./bandwidth-peer-resolver.js", { namedExports: { createBandwidthPeerResolver: (dependencies: { inspect(value: unknown, scope: BandwidthScope): Promise<{ peerRef: { serverId: number } }> }) => ({ + async resolve(token: unknown, scope: BandwidthScope) { const evidence = await dependencies.inspect(token, scope); return Object.freeze({ peerRef: evidence.peerRef, + canonicalNodeId: evidence.peerRef.serverId === 71 ? "11111111-1111-4111-8111-111111111111" : "22222222-2222-4222-8222-222222222222", incarnation: "33333333-3333-4333-8333-333333333333", status: "active", outcome: "existing" }) }, +}) } }) +const { createBandwidthPeerExecutionHost } = await import("./bandwidth-peer-execution-host.js") +const { MikrotikClient } = await import("./mikrotik.js") +const { pool, closePool } = await import("../db/index.js") +const { installManagedMutationSqlFixture } = await import("../test/fixtures/managed-mutation-sql.js") + +test("prepared mapping precedes original managed locks; actual private HTTP finish owns both fences", { timeout: 20000 }, async t => { + let posts = 0, finishes = 0, created = 0, mappingHeld = false, sqlHeld = false, managedHeld = false + let mappingAcquires = 0, mappingCloses = 0, sqlCloses = 0, mappingChecks = 0, managedCheckouts = 0, quarantines = 0 + let failVerify = false, failCloseOnce = false, failAudit = false, synchronousPostRefusal = false + let acquireWait: Promise | undefined, acquireEntered = deferred(), requestCreated = deferred(), received = deferred(), responseGate = deferred() + let holdResponse = false, occupiers: http.ServerResponse[] = [], occupied = deferred() + const events: string[] = [], sql: string[] = [] + let binding: Readonly | undefined, recordEvents: Record[] = [] + let preparedScope: Readonly | undefined + const done = [{ status: "done testing", "tx-total-average": "1000000" }] + const server = http.createServer((request, response) => { + assert.equal(request.headers.authorization, "Basic " + Buffer.from("fixture:fixture").toString("base64")) + if (request.url === "/rest/occupy/get") { occupiers.push(response); if (occupiers.length === 2) occupied.resolve(); return } + assert.equal(request.url, "/rest/tool/bandwidth-test"); posts++ + void (async () => { + let text = ""; for await (const chunk of request) text += String(chunk) + assert.deepEqual(JSON.parse(text), { address: "198.51.100.2", user: "peer", password: "private-peer", protocol: "tcp", direction: "both", duration: "3s" }) + received.resolve(); if (holdResponse) await responseGate.promise + response.setHeader("content-type", "application/json"); response.end(JSON.stringify(done)) + })().catch(() => response.destroy()) + }) + server.listen(0, "127.0.0.1"); await once(server, "listening") + const address = server.address(); assert(address && typeof address !== "string") + const endpoint = { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture" } + const native = new MikrotikClient(endpoint, 71, { requireBandwidthPermit: true }), readClient = new MikrotikClient(endpoint) + const managed = installManagedMutationSqlFixture(71), fixtureConnect = pool.connect, fixtureQuery = pool.query + pool.connect = (async (...args: unknown[]) => { + const heldAtCheckout = mappingHeld + const client = await Reflect.apply(fixtureConnect, pool, args) + return { async query(text: string, values?: unknown[]) { + const acquire = text.startsWith("SELECT pg_try_advisory_lock(") + if (acquire) assert(heldAtCheckout, "mapping S must precede managed config lock checkout") + const result = await client.query(text, values) + if (acquire) { managedCheckouts++; events.push("managed-checkout"); managedHeld = true; events.push("managed-lock") } + else if (text.includes("pg_advisory_unlock")) managedHeld = false + return result + }, release() { client.release() } } + }) as typeof pool.connect + pool.query = (async (...args: unknown[]) => { + if (failAudit && String(args[0]).startsWith("INSERT INTO mcp_audit") && args[1] && (args[1] as unknown[])[1] === "configuration.intent") throw Error("Synthetic pre-transport audit refusal") + return Reflect.apply(fixtureQuery, pool, args) + }) as typeof pool.query + const realRequest = http.request + http.request = ((...args: Parameters) => { + const request = Reflect.apply(realRequest, http, args) as http.ClientRequest + const options = args[0] as http.RequestOptions + if (options.path === "/rest/tool/bandwidth-test") { + created++; requestCreated.resolve() + request.once("finish", () => { finishes++; events.push("finish"); assert(mappingHeld); assert(sqlHeld) }) + } + return request + }) as typeof http.request + const sourcePin = { installationId: "44444444-4444-4444-8444-444444444444", controlGeneration: "owned-control", sourceRecordSha256: "a".repeat(64), issuerKeySha256: "b".repeat(64), quiescenceProof: "c".repeat(64) } + const evidence = (serverId: number) => Object.freeze({ version: 2 as const, peerRef: Object.freeze({ kind: "server" as const, serverId }), + sourceProof: "d".repeat(64), authorityGeneration: "owned-source", routingEndpointEpoch: "55555555-5555-4555-8555-555555555555", + observedAt: Date.now(), expiresAt: Date.now() + 5000, claims: [{ kind: "system-id" as const, value: "owned-" + serverId }], physicalUniquenessVerified: false as const, cloneResistanceVerified: false as const }) + const context = Object.freeze(Object.create(null)) + let intent: BandwidthPeerExecutionIntent + const ack = (kind: BandwidthTransition, value: Readonly) => ({ outcome: "new" as const, transition: kind, ackId: randomUUID(), attemptId: value.attemptId, bindingProof: value.bindingProof, controlGeneration: value.controlGeneration }) + const append = (transition: "reserve" | "arm", value: Readonly) => { + const ack_id = randomUUID(), old_state = transition === "reserve" ? null : "reserved", new_state = transition === "reserve" ? "reserved" : "armed" + recordEvents.push({ ack_id, attempt_id: value.attemptId, transition, old_state, new_state, binding_proof: value.bindingProof, control_generation: value.controlGeneration, + evidence_proof: digest({ ackId: ack_id, attemptId: value.attemptId.toLowerCase(), transition, oldState: old_state, newState: new_state, + bindingProof: value.bindingProof, controlGeneration: value.controlGeneration, terminalProof: null }) }) + } + journal = { async reserve(value) { binding = value; append("reserve", value); return ack("reserve", value) }, async arm(value) { append("arm", value); return ack("arm", value) }, + async complete(value) { return ack("complete", value) }, async releaseBeforeDispatch(value) { return ack("release-before-dispatch", value) }, async quarantine(value) { quarantines++; return ack("quarantine", value) } } + const dependencies = { + coreSource: { async admissionProof(_purpose: string, value: unknown) { return digest(value) }, async eventProofOnConnection(value: unknown) { return digest(value) }, + async connect(_scope: unknown, guard: (fence: unknown) => Promise) { + assert(mappingHeld); assert(managedHeld); sqlHeld = true; events.push("sql-acquire") + const fence = { async sourceIdentity() { return sourcePin } } + return { async query(text: string) { + sql.push(text); assert(sqlHeld); await guard(fence) + let rows: Record[] = [] + if (text.includes("AS locked")) rows = [{ locked: true }] + if (text.includes("/* bandwidth-native:armed-snapshot */")) { + assert(binding) + rows = [{ attempt: { attempt_id: binding.attemptId, binding, caller_kind: binding.callerKind, operation_id: binding.operationId, state: "armed", + source_node: binding.source.canonicalNodeId, destination_node: binding.destination.canonicalNodeId, binding_proof: binding.bindingProof, + control_generation: binding.controlGeneration, resolution: null, terminal_proof: null }, + owners: [binding.source.canonicalNodeId, binding.destination.canonicalNodeId].sort().map(node_id => ({ node_id, attempt_id: binding!.attemptId, binding_proof: binding!.bindingProof, control_generation: binding!.controlGeneration })), events: recordEvents }] + } + await guard(fence); return { command: text === "BEGIN" ? "BEGIN" : "SELECT", rows } + }, release(unsafe: boolean) { assert.equal(unsafe, true); assert(sqlHeld); sqlHeld = false; sqlCloses++; events.push("sql-close") } } + } }, + nodeSource: { async open() { return { connection: {}, binding: async () => sourcePin, keyedProof: async () => "f".repeat(64), close() {} } }, async assertResolvedPeersOnFence() { assert(mappingHeld) } }, + observer: { async observe(peer: { serverId: number }) { return Object.freeze({ value: evidence(peer.serverId) }) }, async inspect(token: { value: ReturnType }) { return token.value }, + async strictClient() { if (synchronousPostRefusal) return { post() { throw Error("Synthetic synchronous facade refusal") } }; return native } }, + async readOriginal(original: unknown) { assert.equal(original, context); return intent }, + async verifyRemote() { return { proof: "e".repeat(64), expiresAt: intent.expiresAt } }, + async acquireRemoteFence(_original: unknown, _intent: unknown, _evidence: unknown, _expected: unknown, scope: Readonly): Promise> { + mappingAcquires++; assert(!managedHeld); assert(!sqlHeld); preparedScope = scope; acquireEntered.resolve(); await acquireWait + mappingHeld = true; events.push("mapping-acquire") + return Object.freeze({ async verify(current: Readonly) { assert.equal(current, preparedScope); assert(mappingHeld); current.signal?.throwIfAborted(); assert(Date.now() < current.deadlineAt) + mappingChecks++; events.push("mapping-verify"); if (failVerify) throw Error("Synthetic mapping revoked") }, + close() { if (failCloseOnce) { failCloseOnce = false; throw Error("Synthetic physical destruction failure") } + if (mappingHeld) { mappingHeld = false; mappingCloses++; events.push("mapping-close") } } }) + }, + } as unknown as BandwidthPeerExecutionHostDependencies + const host = createBandwidthPeerExecutionHost(dependencies) + const reset = () => { + assert(!mappingHeld); assert(!sqlHeld); assert(!managedHeld) + failVerify = false; failCloseOnce = false; failAudit = false; synchronousPostRefusal = false; acquireWait = undefined; holdResponse = false + acquireEntered = deferred(); requestCreated = deferred(); received = deferred(); responseGate = deferred(); occupied = deferred(); occupiers = [] + mappingAcquires = 0; mappingCloses = 0; sqlCloses = 0; mappingChecks = 0; managedCheckouts = 0; quarantines = 0; recordEvents = []; events.length = 0; sql.length = 0 + intent = { attemptId: randomUUID(), operationId: randomUUID(), callerKind: "rest-speed", sourceServerId: 71, credentialServerId: 72, + actualRemotePeer: { kind: "server", serverId: 72 }, request: { owned: "synthetic-original-context" }, nativeBody: { address: "198.51.100.2", user: "peer", password: "private-peer", protocol: "tcp", direction: "both", duration: "3s" }, + durationSec: 3, socketTimeoutMs: 30000, authorityProof: "f".repeat(64), authorityGeneration: "owned-actor", expiresAt: Date.now() + 60000 } + } + try { + await t.test("mapping S exists before managed lock/audit; original scope is borrowed once; both close at actual finish before long response", async () => { + reset(); holdResponse = true; const before = posts, beforeFinish = finishes + let settled = false + const pending = host.execute(context, { deadlineAt: Date.now() + 20000 }).finally(() => { settled = true }) + try { await Promise.race([received.promise, pending.then(() => { assert.fail("Native response unexpectedly finished before held fixture") })]) } + catch (error) { console.error("Synthetic host fence trace", { events, sql, mappingChecks, actions: managed.actions }); throw error } + await delay(15) + assert.equal(mappingAcquires, 1); assert.equal(managedCheckouts, 1); assert.equal(finishes, beforeFinish + 1) + assert.equal(mappingHeld, false); assert.equal(sqlHeld, false); assert.equal(mappingCloses, 1); assert.equal(sqlCloses, 1); assert(mappingChecks >= 3) + assert(events.indexOf("mapping-acquire") < events.indexOf("managed-lock")); assert(events.indexOf("managed-lock") < events.indexOf("sql-acquire")) + assert(events.indexOf("finish") < events.indexOf("mapping-close")); assert(events.indexOf("finish") < events.indexOf("sql-close")) + await delay(5100); assert.equal(settled, false, "short preparation timer must retire at finish, not abort the existing long response") + responseGate.resolve(); assert.deepEqual((await pending).raw, done); assert.equal(posts, before + 1) + assert.deepEqual(managed.actions.slice(-2), ["configuration.intent", "configuration.acknowledged"]) + }) + await t.test("audit/synchronous pre-post refusal retires prepared lease without SQL gate/request", async () => { + for (const mode of ["audit", "sync"] as const) { + reset(); if (mode === "audit") failAudit = true; else synchronousPostRefusal = true + const before = posts, beforeCreated = created + await assert.rejects(host.execute(context, { deadlineAt: Date.now() + 8000 })) + assert.equal(posts, before); assert.equal(created, beforeCreated); assert.equal(mappingAcquires, 1); assert.equal(mappingCloses, 1); assert.equal(sqlCloses, 0); assert.equal(mappingHeld, false) + } + }) + await t.test("cancelled late mapping acquire is destroyed, never acquires managed lock or emits HTTP", async () => { + reset(); const gate = deferred(); acquireWait = gate.promise; const parent = new AbortController(), before = posts + const pending = host.execute(context, { deadlineAt: Date.now() + 8000, signal: parent.signal }) + const refused = assert.rejects(pending); await acquireEntered.promise; parent.abort(); await refused + gate.resolve(); await delay(20) + assert.equal(posts, before); assert.equal(mappingCloses, 1); assert.equal(managedCheckouts, 0); assert.equal(mappingHeld, false) + }) + await t.test("queued private HTTP rechecks the same mapping after socket-ready; revocation sends no POST", async () => { + reset(); const occupying = [readClient.post("/occupy/get", {}), readClient.post("/occupy/get", {})]; await occupied.promise + const before = posts, pending = host.execute(context, { deadlineAt: Date.now() + 8000 }), refused = assert.rejects(pending) + await requestCreated.promise; assert(mappingHeld); assert(sqlHeld); failVerify = true + occupiers.splice(0).forEach(response => response.end("{}")); await Promise.all(occupying); await refused; await delay(20) + assert.equal(posts, before); assert.equal(mappingAcquires, 1); assert.equal(mappingCloses, 1); assert.equal(sqlCloses, 1); assert.equal(mappingHeld, false); assert.equal(sqlHeld, false) + }) + await t.test("queued deadline retires prepared mapping and SQL; later Agent capacity emits no POST", async () => { + reset(); const occupying = [readClient.post("/occupy/get", {}), readClient.post("/occupy/get", {})]; await occupied.promise + const before = posts + await assert.rejects(host.execute(context, { deadlineAt: Date.now() + 150 })) + assert.equal(mappingHeld, false); assert.equal(sqlHeld, false) + occupiers.splice(0).forEach(response => response.end("{}")); await Promise.all(occupying); await delay(25) + assert.equal(posts, before); assert.equal(mappingAcquires, 1); assert.equal(mappingCloses, 1); assert.equal(sqlCloses, 1) + }) + await t.test("failed physical close is retried and remains unknown rather than healthy completion", async () => { + reset(); failCloseOnce = true + await assert.rejects(host.execute(context, { deadlineAt: Date.now() + 8000 })) + assert.equal(mappingCloses, 1); assert.equal(mappingHeld, false); assert.equal(sqlHeld, false); assert.equal(quarantines, 1) + }) + } finally { + responseGate.resolve(); occupiers.forEach(response => response.end("{}")) + http.request = realRequest; pool.connect = fixtureConnect; pool.query = fixtureQuery; managed.restore() + server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) + mock.restoreAll(); await closePool() + } +}) diff --git a/backend/src/services/bandwidth-peer-execution-host.sql-helper.ts b/backend/src/services/bandwidth-peer-execution-host.sql-helper.ts new file mode 100644 index 00000000..51446035 --- /dev/null +++ b/backend/src/services/bandwidth-peer-execution-host.sql-helper.ts @@ -0,0 +1,229 @@ +/** Actual owned PG -> signed configured v2 source -> mixed peer broker -> HTTP. + * Original grants and wire mapping are fixed synthetic operator fixtures. This + * does not supply a production context provider, mapping attestor or activation. */ +import assert from "node:assert/strict" +import { performance } from "node:perf_hooks" +import { createHash, randomUUID } from "node:crypto" +import type { createOwnedExternalRegistryHarness } from "./bandwidth-external-node-registry.sql-helper.js" +import type { BandwidthPeerExecutionIntent } from "./bandwidth-peer-execution-host.js" +import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +type Fixture=Awaited> +type Signed=Awaited> +type Grant={intent:BandwidthPeerExecutionIntent;rowsDigest:string;allowed:boolean;driftMapping:boolean;mappingReads:number;mappingGeneration:number} +export async function acceptBandwidthPeerExecutionHost(f:Fixture,signed:Signed){ + assertOwnedBandwidthSql();assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY,"true") + const {Client}=await import("pg") + const {createBandwidthPeerExecutionHost,BandwidthPeerExecutionHostError,readBandwidthPeerCompletion}=await import("./bandwidth-peer-execution-host.js") + const {createBandwidthNodeRegistry}=await import("./bandwidth-node-registry.js") + const {createBandwidthExternalNodeRegistry}=await import("./bandwidth-external-node-registry.js") + const {canonicalBandwidthNodeValue}=await import("./bandwidth-node-bootstrap.js") + const digest=(v:unknown)=>createHash("sha256").update(canonicalBandwidthNodeValue(v)).digest("hex") + const rows=new WeakMap(),checks:string[]=[],initialPosts=f.nativePosts() + const tables=["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events","bandwidth_source_identity","bandwidth_node_authority","bandwidth_nodes","bandwidth_node_aliases","bandwidth_node_events","bandwidth_external_node_aliases","bandwidth_external_node_events"] + const live=(scope:{deadlineAt:number;signal?:AbortSignal})=>{scope.signal?.throwIfAborted();assert(Date.now())=>{assert(e.peerRef.kind==="server");const {version:_version,peerRef,...rest}=e;return{...rest,version:1 as const,serverId:peerRef.serverId}} + const enroll=async(peerRef:BandwidthPeerRef)=>{ + const began=Date.now() + const token=await signed.observer.observe(peerRef,f.scope()),evidence=await signed.observer.inspect(token,f.scope()) + const scope={deadlineAt:evidence.expiresAt},expiresAt=Date.now()+60000 + const guard=async()=>{assert.deepEqual(await signed.observer.inspect(token,scope),evidence);live(scope)} + const session=await signed.node.open(scope,guard) + try{ + const authorize=async()=>{await guard();return{...f.root,authorityProof:f.mac("SYNTHETIC-original-enrollment",{peerRef,claims:evidence.claims}),authorityGeneration:"owned-peer-execution-enrollment",expiresAt}} + const common={now:Date.now,connect:async()=>session.connection,keyedProof:session.keyedProof,keyedProofs:session.keyedProofs,authorize} + if(peerRef.kind==="server")return await createBandwidthNodeRegistry({...common,inspect:async(value,current)=>toManaged(await signed.observer.inspect(value,current))}).register(token,scope) + return await createBandwidthExternalNodeRegistry({...common,inspect:signed.observer.inspect}).register(token,scope) + }catch(error){ + console.error("Owned v2 peer enrollment timing:",JSON.stringify({peerKind:peerRef.kind,elapsedMs:Date.now()-began,observationRemainingMs:evidence.expiresAt-Date.now()})) + throw error + }finally{session.close()} + } + const sourceReceipt=await enroll({kind:"server",serverId:sourceId}),remoteReceipt=await enroll({kind:"external",enrollmentId:remoteId}) + assert.notEqual(sourceReceipt.canonicalNodeId,remoteReceipt.canonicalNodeId) + const {acceptOwnedBandwidthNativeDispatchSqlSource}=await import("./bandwidth-native-dispatch-source.sql-helper.js") + const nativeSourceAcceptance=await acceptOwnedBandwidthNativeDispatchSqlSource(signed.core,signed.controlGeneration,f.owner) + checks.push(...nativeSourceAcceptance.checks) + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_aliases WHERE server_id=$1",[credentialId])).rows[0].n),0) + const context=async(enrollmentId=remoteId)=>{ + const currentRows=await f.applicationRows([sourceId,credentialId]),credential=currentRows.find(row=>Number(row.id)===credentialId) + assert.equal(currentRows.length,2);assert(credential);assert.equal(typeof credential.username,"string");assert.equal(typeof credential.password,"string") + const expiresAt=Date.now()+60000 + const intent:BandwidthPeerExecutionIntent={attemptId:randomUUID(),operationId:randomUUID(),callerKind:"rest-diagnostic",sourceServerId:sourceId,actualRemotePeer:{kind:"external",enrollmentId},credentialServerId:credentialId, + request:{fixture:"SYNTHETIC-OWNED-original-context",bwRemoteAddress:"203.0.113.57@owned-external-vrf",dstServerId:credentialId}, + nativeBody:{address:"203.0.113.57@owned-external-vrf",user:String(credential.username),password:String(credential.password),protocol:"tcp",direction:"both",duration:"3s"},durationSec:3,socketTimeoutMs:15000, + authorityProof:f.mac("SYNTHETIC-sealed-original",{sourceId,enrollmentId,credentialId,rowsDigest:digest(currentRows),expiresAt}),authorityGeneration:"owned-independent-original-context-v2",expiresAt} + const opaque=Object.freeze(Object.create(null)) as object,grant:Grant={intent,rowsDigest:digest(currentRows),allowed:true,driftMapping:false,mappingReads:0,mappingGeneration:1} + rows.set(opaque,grant);return{opaque,grant} + } + let credentialReads=0 + type Stat={calls:number;completed:number;failed:number;totalMs:number;maxMs:number} + type Run={began:number;io:Record;sql:Record;pids:Set;physicalSql:number;nativePid?:number;nativeLocksProved?:boolean} + let activeRun:Run|undefined,lastRun:Record|undefined + const add=(group:Record,name:string,elapsed:number,failed:boolean)=>{const stat=group[name]??={calls:0,completed:0,failed:0,totalMs:0,maxMs:0};stat.completed++;if(failed)stat.failed++;stat.totalMs+=elapsed;stat.maxMs=Math.max(stat.maxMs,elapsed)} + const timed=async(name:string,work:()=>Promise):Promise=>{ + const run=activeRun;if(!run)return work() + const stat=run.io[name]??={calls:0,completed:0,failed:0,totalMs:0,maxMs:0};stat.calls++;const began=performance.now() + try{const value=await work();add(run.io,name,performance.now()-began,false);return value}catch(error){add(run.io,name,performance.now()-began,true);throw error} + } + const measuredCore:typeof signed.core={...signed.core, + connect:(...args)=>timed("core.connect",()=>signed.core.connect(...args)), + admissionProof:(...args)=>timed("core.admissionProof",()=>signed.core.admissionProof(...args)), + eventProofOnConnection:(...args)=>timed("core.eventProof",()=>signed.core.eventProofOnConnection(...args)), + } + const measuredNodes:typeof signed.node={...signed.node, + open:(...args)=>timed("node.open",async()=>{const session=await signed.node.open(...args);return{...session, + binding:()=>timed("node.binding",()=>session.binding()),keyedProof:(...proofArgs)=>timed("node.keyedProof",()=>session.keyedProof(...proofArgs)), + }}), + assertResolvedPeersOnFence:(...args)=>timed("node.peerFence",()=>signed.node.assertResolvedPeersOnFence(...args)), + } + const measuredObserver:typeof signed.observer={...signed.observer, + observe:(...args)=>timed("observer.observe",()=>signed.observer.observe(...args)), + inspect:(...args)=>timed("observer.inspect",()=>signed.observer.inspect(...args)), + strictClient:(...args)=>timed("observer.strictClient",()=>signed.observer.strictClient(...args)), + } + const category=(sql:string)=>{ + if(sql.includes("/* bandwidth-control:identity */"))return"core.identity" + if(sql.includes("/* bandwidth-peer:identity */"))return"peer.identity" + if(sql.includes("FROM public.servers"))return"application.servers" + if(/^(?:BEGIN|COMMIT|ROLLBACK)\b/i.test(sql.trim()))return"transaction" + if(/^(?:INSERT|UPDATE|DELETE)\b/i.test(sql.trim()))return"control.dml" + if(sql.includes("bandwidth_node_authority"))return"registry.authority" + if(sql.includes("pg_catalog.pg_class")||sql.includes("pg_catalog.pg_roles"))return"catalog.other" + if(sql.includes("bandwidth_external_node_aliases")||sql.includes("bandwidth_node_aliases"))return"registry.alias" + if(sql.includes("bandwidth_external_node_events")||sql.includes("bandwidth_node_events"))return"registry.event" + if(sql.includes("bandwidth_nodes"))return"registry.node" + if(sql.includes("pg_advisory"))return"registry.lock" + if(sql.includes("set_config"))return"config" + return"other" + } + const summarize=(group:Record)=>Object.fromEntries(Object.entries(group).map(([name,value])=>[name,{...value,totalMs:Math.round(value.totalMs*10)/10,maxMs:Math.round(value.maxMs*10)/10}])) + const diagnostic=(reason:string)=>{const text=JSON.stringify({reason,...lastRun});assert(Buffer.byteLength(text)<16384);console.error("Owned v2 peer host local diagnostic:",text)} + const host=createBandwidthPeerExecutionHost({coreSource:measuredCore,nodeSource:measuredNodes,observer:measuredObserver, + readOriginal:(opaque,scope)=>timed("original-context",async()=>{ + const grant=opaque&&typeof opaque==="object"?rows.get(opaque):undefined;assert(grant);live(scope) + assert(grant.allowed);assert(Date.now()timed("remote-policy",async()=>{ + const grant=opaque&&typeof opaque==="object"?rows.get(opaque):undefined;assert(grant);live(scope);assert(grant.allowed);assert.equal(digest(intent),digest(grant.intent)) + assert.equal(intent.actualRemotePeer.kind,"external");assert.equal(evidence.remoteEvidence.peerRef.kind,"external");assert.equal(evidence.remoteReceipt.peerRef.kind,"external") + assert.equal(intent.nativeBody.address,"203.0.113.57@owned-external-vrf") + assert.equal(digest(evidence.remoteEvidence.peerRef),digest(intent.actualRemotePeer));assert.equal(digest(evidence.remoteReceipt.peerRef),digest(intent.actualRemotePeer)) + assert.equal(evidence.remoteReceipt.canonicalNodeId,remoteReceipt.canonicalNodeId);assert.notEqual(intent.sourceServerId,intent.credentialServerId) + const proof=f.mac("SYNTHETIC-independent-wire-address-mapping",{address:intent.nativeBody.address,sourceRealm:"owned-source-realm",vrf:"owned-external-vrf",zone:"owned-lab-zone",canonicalNodeId:evidence.remoteReceipt.canonicalNodeId,enrollment:intent.actualRemotePeer,credentialId,mappingGeneration:grant.mappingGeneration}) + grant.mappingReads++;if(grant.driftMapping&&grant.mappingReads===1)grant.mappingGeneration++ + return{proof,expiresAt:intent.expiresAt} + })}) + const execute=async(value:{opaque:object})=>{ + assert(!activeRun);const run:Run={began:performance.now(),io:{},sql:{},pids:new Set(),physicalSql:0};activeRun=run + const originalQuery=Client.prototype.query + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + const connection=this as unknown as {connectionParameters:{user:string;database:string};processID:number} + const target=connection.connectionParameters + if(!(target.database===f.databaseName&&target.user===signed.runtimeRole||target.database==="postgres"&&target.user==="mcp_test"))return Reflect.apply(originalQuery,this,args) + const sql=typeof args[0]==="string"?args[0]:(args[0] as {text?:string})?.text??"",name=category(sql),began=performance.now() + const stat=run.sql[name]??={calls:0,completed:0,failed:0,totalMs:0,maxMs:0};stat.calls++;run.physicalSql++ + if(target.database===f.databaseName&&connection.processID)run.pids.add(connection.processID) + let result:unknown + try{result=Reflect.apply(originalQuery,this,args)}catch(error){add(run.sql,name,performance.now()-began,true);throw error} + if(!result||typeof(result as Promise).then!=="function"){add(run.sql,name,performance.now()-began,false);return result} + return(result as Promise).then(async value=>{ + add(run.sql,name,performance.now()-began,false) + if(target.database===f.databaseName&&target.user===signed.runtimeRole&&(sql.includes("/* bandwidth-native:armed-snapshot */")||sql.endsWith("FOR SHARE NOWAIT"))&&!run.nativeLocksProved){ + assert(connection.processID);run.nativePid=connection.processID + assert.equal((await f.owner.query("SELECT pg_catalog.pg_try_advisory_xact_lock(8723116) AS locked")).rows[0].locked,false) + for(const node of[sourceReceipt.canonicalNodeId,remoteReceipt.canonicalNodeId])assert.equal((await f.owner.query("SELECT pg_catalog.pg_try_advisory_xact_lock(pg_catalog.hashtextextended($1,764301)) AS locked",["bandwidth-node:"+node])).rows[0].locked,false) + const grant=rows.get(valueContext.opaque);assert(grant) + await assert.rejects(f.owner.query("SELECT attempt_id FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1 FOR UPDATE NOWAIT",[grant.intent.attemptId]),error=>(error as{code?:string}).code==="55P03") + run.nativeLocksProved=true + } + return value + },error=>{add(run.sql,name,performance.now()-began,true);throw error}) + } as typeof Client.prototype.query + const valueContext=value + let failure:unknown + try{return await host.execute(value.opaque,f.scope(60000))}catch(error){failure=error;throw error} + finally{ + Client.prototype.query=originalQuery;activeRun=undefined + const grant=rows.get(value.opaque) + lastRun={inclusiveTimings:true,elapsedMs:Math.round((performance.now()-run.began)*10)/10,error:failure instanceof BandwidthPeerExecutionHostError?{code:failure.code,phase:failure.phase??"none"}:failure?{code:"dependency",phase:"unknown"}:undefined,mappingReads:grant?.mappingReads??0,nativePosts:f.nativePosts()-initialPosts,physicalSql:run.physicalSql,sourcePhysicalPids:run.pids.size,nativeLocksProved:run.nativeLocksProved===true,io:summarize(run.io),statements:summarize(run.sql)} + } + } + const baseline=await f.digest(tables),denied=await context(),reads=credentialReads + denied.grant.allowed=false + await assert.rejects(execute(denied),e=>e instanceof BandwidthPeerExecutionHostError&&e.code==="REFUSED") + assert.equal(credentialReads,reads);assert.equal(f.nativePosts(),initialPosts);assert.equal(await f.digest(tables),baseline) + const drift=await context();drift.grant.driftMapping=true + await assert.rejects(execute(drift),e=>{const valid=e instanceof BandwidthPeerExecutionHostError&&e.code==="REFUSED";if(!valid)diagnostic("mapping-refusal-mismatch");return valid}) + if(drift.grant.mappingReads<2)diagnostic("mapping-denial-precondition-not-reached") + assert(drift.grant.mappingReads>=2);assert.equal(f.nativePosts(),initialPosts);assert.equal(await f.digest(tables),baseline) + checks.push("actual v2 peer host: revoked original fixture grant denies before credential fetch; independently changed exact address/VRF/zone mapping proof denies before any POST or ledger mutation") + const success=await context() + let retiredBeforeResponse=false + f.setBeforeBtestResponse(async()=>{ + const pid=activeRun?.nativePid;assert(pid);assert.equal(activeRun?.nativeLocksProved,true) + const rival=await f.owner.connect() + try{ + await rival.query("BEGIN") + await rival.query("SET LOCAL lock_timeout='2000ms'") + // This real lock acquisition waits for physical retirement, without a + // sleep or fabricated finish ACK. The terminal response is still held. + await rival.query("SELECT pg_catalog.pg_advisory_xact_lock(8723116)") + const retiredDeadline=performance.now()+2000 + let present=true + while(present&&performance.now()> + try{result=await execute(success).catch(error=>{diagnostic("positive-execution-refused");throw error})} + finally{f.setBeforeBtestResponse(undefined)} + assert(retiredBeforeResponse) + assert.equal(lastRun?.nativeLocksProved,true) + assert.equal(result.remoteCompletionConfirmed,true);assert.equal(f.nativePosts(),initialPosts+1) + assert(Object.isFrozen(result));assert(Object.isFrozen(result.raw)) + assert.deepEqual(readBandwidthPeerCompletion(result,success.opaque),success.grant.intent) + for(const copied of[{...result},JSON.parse(JSON.stringify(result)),{remoteCompletionConfirmed:true,raw:result.raw}])assert.throws(()=>readBandwidthPeerCompletion(copied,success.opaque),BandwidthPeerExecutionHostError) + assert.throws(()=>readBandwidthPeerCompletion(result,{}),BandwidthPeerExecutionHostError) + assert.equal(f.nativeBodyDigest(),digest(success.grant.intent.nativeBody));assert.equal(success.grant.intent.nativeBody.duration,"3s");assert.equal(success.grant.intent.nativeBody.address,"203.0.113.57@owned-external-vrf") + const state=(await f.owner.query("SELECT state,resolution FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1",[success.grant.intent.attemptId])).rows[0] + assert.equal(state.state,"released") + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1",[success.grant.intent.attemptId])).rows[0].n),0) + const eventKinds=(await f.owner.query("SELECT transition FROM mmapp_control.bandwidth_attempt_events WHERE attempt_id=$1 ORDER BY created_at",[success.grant.intent.attemptId])).rows.map(row=>row.transition) + assert(eventKinds.includes("reserve"));assert(eventKinds.includes("arm"));assert(eventKinds.includes("complete")) + checks.push("actual signed core/node v2 host resolves genuine managed source and external destination with distinct real managed credential provider, alias fence and reserve/arm/complete; exactly one localhost btest POST preserves literal body/address/duration3s, terminal releases both owners") + checks.push("actual native SQL producer excludes registry/canonical/owner-row rivals on one lease; physical backend and locks retire while response is held and both owners remain armed; 5.2-second response retains original native lifetime") + checks.push("private completion receipt is minted only after actual native terminal plus durable ACK, frozen and bound to the exact original; copied/JSON/forged/wrong-original results cannot authorize post-run work") + f.setBtestMode("drop") + try{ + const dropped=await context();await assert.rejects(execute(dropped),e=>{const valid=e instanceof BandwidthPeerExecutionHostError&&e.code==="OUTCOME_UNKNOWN"&&e.automaticReplay===false;if(!valid)diagnostic("dropped-response-outcome-mismatch");return valid}) + assert.equal(f.nativePosts(),initialPosts+2) + assert.equal((await f.owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1",[dropped.grant.intent.attemptId])).rows[0]?.state,"unknown_busy") + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy WHERE attempt_id=$1",[dropped.grant.intent.attemptId])).rows[0].n),2) + const extraId=f.externalEndpoint(remoteClaims),extra=await enroll({kind:"external",enrollmentId:extraId});assert.equal(extra.canonicalNodeId,remoteReceipt.canonicalNodeId) + const unknownBefore=await f.digest(["bandwidth_node_occupancy"]),overlap=await context(extraId) + await assert.rejects(execute(overlap));assert.equal(f.nativePosts(),initialPosts+2);assert.equal(await f.digest(["bandwidth_node_occupancy"]),unknownBefore) + assert.equal((await f.owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1",[dropped.grant.intent.attemptId])).rows[0]?.state,"unknown_busy") + checks.push("actual dropped native response leaves unknown_busy with two owners and no replay; a genuinely enrolled different external alias of the same node cannot send another POST or alter existing unknown occupancy") + }finally{f.setBtestMode("terminal")} + return{checks,posts:f.nativePosts()-initialPosts,productionContextProvider:false,productionMappingAttestor:false} +} diff --git a/backend/src/services/bandwidth-peer-execution-host.ts b/backend/src/services/bandwidth-peer-execution-host.ts new file mode 100644 index 00000000..6fcaa1c9 --- /dev/null +++ b/backend/src/services/bandwidth-peer-execution-host.ts @@ -0,0 +1,391 @@ +/** Trusted internal execution composition only. No enrollment, default source, + * production activation or model-supplied transport/authority callbacks. Raw + * results are for the complete internal workflow, never a public readiness API. + * The staged native SQL producer retains registry and exact owner locks through + * the private ClientRequest.finish barrier. Runtime callers remain disabled. */ +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { BANDWIDTH_CALLER_KINDS, createBandwidthAdmission, type BandwidthAdmissionInput, type BandwidthBinding, type BandwidthJournal, type BandwidthScope, type BandwidthTransition } from "./bandwidth-admission.js" +import { createBandwidthAdmissionSqlJournal } from "./bandwidth-admission-storage.js" +import type { createConfiguredBandwidthControlSource, BandwidthControlDispatchFence } from "./bandwidth-control-source.js" +import type { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" +import type { createBandwidthPeerObservationReader, BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import { parseBandwidthPeerRef } from "./bandwidth-peer-reference.js" +import { createBandwidthPeerResolver } from "./bandwidth-peer-resolver.js" +import { bandwidthPeerRefKey } from "./bandwidth-peer-reference.js" +import type { BandwidthPeerRegistryReceipt } from "./bandwidth-peer-fence.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import { createBandwidthNativeDispatchSqlGate } from "./bandwidth-native-dispatch-source.js" +import { createBandwidthNativeDispatchCompositeGate } from "./bandwidth-native-dispatch-composite.js" +import type { BandwidthNativeDispatchLease } from "./bandwidth-native-dispatch.js" + +type Core = ReturnType +type Nodes = ReturnType +type Observer = ReturnType +type NodeSession = Awaited> +type NodeBinding = Awaited> +const hash = z.string().regex(/^[a-f0-9]{64}$/), label = z.string().regex(/^[a-zA-Z0-9_.:-]{1,200}$/) +const positive = z.number().int().positive().safe() +const intentSchema = z.object({ attemptId: z.string().uuid(), operationId: label, callerKind: z.enum(BANDWIDTH_CALLER_KINDS), + sourceServerId: positive, actualRemotePeer: z.union([z.object({kind:z.literal("server"),serverId:positive}).strict(),z.object({kind:z.literal("external"),enrollmentId:z.string().uuid()}).strict()]).transform(parseBandwidthPeerRef), credentialServerId: positive, + request: z.record(z.string(), z.unknown()).transform(value => value as BandwidthAdmissionInput["request"]), nativeBody: z.record(z.string(), z.string()), + durationSec: z.number().int().min(3).max(2147483647), socketTimeoutMs: z.number().int().min(1).max(2147483647), + authorityProof: hash, authorityGeneration: label, expiresAt: positive }).strict() +export type BandwidthPeerExecutionIntent = Readonly> +export interface BandwidthPeerExecutionRemoteEvidence { + readonly sourceEvidence: Readonly + readonly remoteEvidence: Readonly + readonly sourceReceipt: Readonly + readonly remoteReceipt: Readonly +} +export interface BandwidthPeerExecutionHostDependencies { + coreSource: Core + nodeSource: Nodes + observer: Observer + /** Fixed server-owned original human/scheduler/MCP context verifier. Every + * read authenticates exact sealed intent AND current rights to ALL THREE IDs, + * current source/destination credentials and original nonrenewed expiry. */ + readOriginal(originalContext: unknown, scope: Readonly): Promise + /** Independently authenticate wire address/VRF/zone against the ACTUAL remote + * canonical receipt. Credential provider identity alone is insufficient. */ + verifyRemote(originalContext: unknown, intent: BandwidthPeerExecutionIntent, evidence: Readonly, scope: Readonly): Promise<{ proof: string; expiresAt: number }> + /** Trusted production mapping assembly must hold its actual configuration/ + * path fence, bound to this exact previously authenticated proof, until the + * private native finish barrier closes it. The optional legacy seam remains + * for scoped synthetic acceptance; its absence is not production readiness. */ + acquireRemoteFence?(originalContext: unknown, intent: BandwidthPeerExecutionIntent, evidence: Readonly, + expected: Readonly<{ proof: string; expiresAt: number }>, scope: Readonly): Promise> +} +export class BandwidthPeerExecutionHostError extends Error { + readonly automaticReplay = false + constructor(readonly code: "REFUSED" | "OUTCOME_UNKNOWN",readonly phase?:BandwidthPeerExecutionPhase) { super("Bandwidth execution " + code) } +} +type BandwidthPeerExecutionPhase="original-context"|"source-observation"|"remote-observation"|"source-resolution"|"remote-resolution"|"remote-policy"|"participant-proof"|"strict-client"|"reserve-arm"|"native-send"|"terminal-proof"|BandwidthTransition +function refuse(): never { throw new BandwidthPeerExecutionHostError("REFUSED") } +const executionHosts = new WeakSet() +const completions = new WeakMap>() +/** Internal proof of this exact native completion AND durable completion ACK. + * JSON, copied results and another original cannot authorize post-run work. */ +export function readBandwidthPeerCompletion(value: unknown, originalContext: unknown): Readonly { + const completion = value && typeof value === "object" ? completions.get(value) : undefined + if (!completion || completion.originalContext !== originalContext) return refuse() + return completion.intent +} +export function assertBandwidthPeerExecutionHost(value: unknown): asserts value is ReturnType { + if (!value || typeof value !== "object" || !executionHosts.has(value)) refuse() +} +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} +/** Reject getters, serializers, symbols and non-JSON values before any await. */ +function capture(value: unknown, limit = 1048576): unknown { + const seen = new Set() + const walk = (item: unknown, depth: number): unknown => { + if (depth > 32) refuse() + if (item === null || typeof item === "string" || typeof item === "boolean") return item + if (typeof item === "number") { if (!Number.isFinite(item) || Number.isInteger(item) && !Number.isSafeInteger(item)) refuse(); return item } + if (!item || typeof item !== "object" || seen.has(item)) refuse() + seen.add(item) + try { + if (Array.isArray(item)) { + const descriptors = Object.getOwnPropertyDescriptors(item) + if (Reflect.ownKeys(item).length !== item.length + 1 || Object.keys(descriptors).some(key => key !== "length" && (!/^(0|[1-9][0-9]*)$/.test(key) || !("value" in descriptors[key])))) refuse() + return Array.from({ length: item.length }, (_, index) => walk(descriptors[String(index)].value, depth + 1)) + } + if (Object.getPrototypeOf(item) !== Object.prototype) refuse() + const descriptors = Object.getOwnPropertyDescriptors(item) + if (Reflect.ownKeys(item).length !== Object.keys(descriptors).length || Object.values(descriptors).some(entry => !("value" in entry) || !entry.enumerable)) refuse() + return Object.fromEntries(Object.entries(descriptors).map(([key, entry]) => [key, walk(entry.value, depth + 1)])) + } finally { seen.delete(item) } + } + const result = walk(value, 0) + if (Buffer.byteLength(JSON.stringify(result)) > limit) refuse() + return freeze(result) +} +const readIntent = (value: unknown): BandwidthPeerExecutionIntent => { + const parsed = intentSchema.parse(capture(value)) + if (parsed.nativeBody.duration !== `${parsed.durationSec}s` || !parsed.nativeBody.address?.trim()) refuse() + return freeze(parsed) +} +const nodeSubject=(value:Readonly)=>({version:value.version,peerRef:value.peerRef,sourceProof:value.sourceProof, + authorityGeneration:value.authorityGeneration,routingEndpointEpoch:value.routingEndpointEpoch,claims:value.claims, + physicalUniquenessVerified:value.physicalUniquenessVerified,cloneResistanceVerified:value.cloneResistanceVerified}) + +export function createBandwidthPeerExecutionHost(dependencies: BandwidthPeerExecutionHostDependencies) { + // Capture the trusted assembly once; request values never choose providers. + const readOriginal = dependencies.readOriginal.bind(dependencies), verifyRemote = dependencies.verifyRemote.bind(dependencies) + const acquireRemoteFence = dependencies.acquireRemoteFence?.bind(dependencies) + const core = Object.freeze({ connect: dependencies.coreSource.connect.bind(dependencies.coreSource), + admissionProof: dependencies.coreSource.admissionProof.bind(dependencies.coreSource), eventProof: dependencies.coreSource.eventProofOnConnection.bind(dependencies.coreSource) }) + const nodes = Object.freeze({ open: dependencies.nodeSource.open.bind(dependencies.nodeSource), assertResolvedPeersOnFence: dependencies.nodeSource.assertResolvedPeersOnFence.bind(dependencies.nodeSource) }) + const observer = Object.freeze({ observe: dependencies.observer.observe.bind(dependencies.observer), inspect: dependencies.observer.inspect.bind(dependencies.observer), strictClient: dependencies.observer.strictClient.bind(dependencies.observer) }) + const host = Object.freeze({ async execute(originalContext: unknown, inputScope: BandwidthScope) { + if (!inputScope || !Number.isSafeInteger(inputScope.deadlineAt) || inputScope.signal !== undefined && !(inputScope.signal instanceof AbortSignal)) refuse() + const entered = Date.now(), enteredMonotonic=performance.now(), controller = new AbortController() + let monotonicEnd=enteredMonotonic+Math.max(0,inputScope.deadlineAt-entered),mutationPossible=false + let phase:BandwidthPeerExecutionPhase="original-context" + const parentDeadline = inputScope.deadlineAt, parentSignal = inputScope.signal + let running = true, intent: BandwidthPeerExecutionIntent | undefined, timer: ReturnType | undefined + // Separate short preparation cap; clearing it never changes/renews the + // original monotonic horizon above. Reuse the existing owner timer. + let remotePhase: Readonly<{ deadlineAt: number; end: number }> | undefined, retireRemote: (() => void) | undefined, stopping = false + const abort = () => controller.abort() + parentSignal?.addEventListener("abort", abort, { once: true }); if (parentSignal?.aborted) abort() + const scope: BandwidthScope = Object.freeze({ deadlineAt: parentDeadline, signal: controller.signal }) + let rejectStopped!: (error: BandwidthPeerExecutionHostError) => void + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + const stop = () => { + if (!running || stopping) return; stopping = true + try { retireRemote?.() } catch { /* Conservative unknown; physical destruction was retried. */ } + controller.abort(); rejectStopped(new BandwidthPeerExecutionHostError(mutationPossible?"OUTCOME_UNKNOWN":"REFUSED")) + } + controller.signal.addEventListener("abort", stop, { once: true }) + const live = (current: BandwidthScope = scope) => { + if (!running || current.signal?.aborted || controller.signal.aborted || Date.now() >= Math.min(current.deadlineAt, intent?.expiresAt ?? Infinity, remotePhase?.deadlineAt ?? Infinity) + || performance.now() >= Math.min(monotonicEnd, remotePhase?.end ?? Infinity)) refuse() + } + const schedule = () => { + clearTimeout(timer) + const remaining = Math.min(Math.min(parentDeadline, intent?.expiresAt ?? Infinity, remotePhase?.deadlineAt ?? Infinity) - Date.now(), + Math.min(monotonicEnd, remotePhase?.end ?? Infinity) - performance.now()) + timer = setTimeout(() => { if (remaining > 2147483647) schedule(); else stop() }, Math.max(1, Math.min(2147483647, remaining))) + } + schedule() + const work = async () => { + live(); intent = readIntent(await readOriginal(originalContext, scope)) + monotonicEnd=Math.min(monotonicEnd,enteredMonotonic+Math.max(0,intent.expiresAt-entered)) + live(); schedule() + const original = intent, executeScope = Object.freeze({ deadlineAt: Math.min(parentDeadline, original.expiresAt), signal: controller.signal }) + const actor = async (current: BandwidthScope) => { live(current); const fresh = readIntent(await readOriginal(originalContext, current)); live(current); if (!isDeepStrictEqual(fresh, original)) refuse() } + await actor(executeScope) + phase="source-observation";let sourceToken = await observer.observe({kind:"server",serverId:original.sourceServerId}, executeScope); live() + phase="remote-observation";let remoteToken = await observer.observe(original.actualRemotePeer, executeScope); live() + let sourceEvidence = freeze(await observer.inspect(sourceToken, executeScope)), remoteEvidence = freeze(await observer.inspect(remoteToken, executeScope)) + const originalSourceSubject=freeze(structuredClone(nodeSubject(sourceEvidence))),originalRemoteSubject=freeze(structuredClone(nodeSubject(remoteEvidence))) + let preScope = Object.freeze({ deadlineAt: Math.min(executeScope.deadlineAt, sourceEvidence.expiresAt, remoteEvidence.expiresAt), signal: controller.signal }) + // Each short phase obtains NEW real observations of the SAME original + // approved subjects. Expired capabilities are never inspected/adopted; + // actor/peer expiry, credentials, epochs and claims cannot be renewed. + // Call only between transitions, with no journal/registry lease held. + const observePhase=async()=>{ + await actor(executeScope) + const tokens=await Promise.all([observer.observe({kind:"server",serverId:original.sourceServerId},executeScope),observer.observe(original.actualRemotePeer,executeScope)]) + const evidence=await Promise.all(tokens.map(token=>observer.inspect(token,executeScope))) + if(!isDeepStrictEqual(nodeSubject(evidence[0]),originalSourceSubject)||!isDeepStrictEqual(nodeSubject(evidence[1]),originalRemoteSubject))refuse() + live() + sourceToken=tokens[0];remoteToken=tokens[1];sourceEvidence=freeze(evidence[0]);remoteEvidence=freeze(evidence[1]) + preScope=Object.freeze({deadlineAt:Math.min(executeScope.deadlineAt,sourceEvidence.expiresAt,remoteEvidence.expiresAt),signal:controller.signal}) + await actor(preScope) + } + const nativeFresh = async (current: BandwidthScope) => { + live(current) + if (!isDeepStrictEqual(await observer.inspect(sourceToken, current), sourceEvidence) || !isDeepStrictEqual(await observer.inspect(remoteToken, current), remoteEvidence)) refuse() + live(current) + } + const originalGuard = async () => { await nativeFresh(preScope); await actor(preScope) } + let pin: NodeBinding | undefined + const resolve = async (token: unknown, evidence: Readonly) => { + await originalGuard(); const session = await nodes.open(preScope, originalGuard) + try { + const binding = freeze(await session.binding()); if (pin && !isDeepStrictEqual(pin, binding)) refuse(); pin ??= binding + const resolver = createBandwidthPeerResolver({connect:async()=>session.connection, + inspect:(value,current)=>observer.inspect(value,current),keyedProof:(purpose,value)=>session.keyedProof(purpose,value), + keyedProofs:session.keyedProofs?.bind(session), + authorize:async(peer,current,genuine)=>{ + if(bandwidthPeerRefKey(peer)!==bandwidthPeerRefKey(evidence.peerRef)||!isDeepStrictEqual(genuine,evidence))refuse() + if(!isDeepStrictEqual(await session.binding(),pin))refuse() + await nativeFresh(current);await actor(current) + return {installationId:binding.installationId,issuerKeySha256:binding.issuerKeySha256,quiescenceProof:binding.quiescenceProof, + authorityProof:original.authorityProof,authorityGeneration:original.authorityGeneration,expiresAt:original.expiresAt} + }}) + const receipt = freeze(await resolver.resolve(token,preScope)) + return receipt + } finally { session.close() } + } + phase="source-resolution";const sourceReceipt = await resolve(sourceToken, sourceEvidence) + await observePhase() + phase="remote-resolution";const remoteReceipt = await resolve(remoteToken, remoteEvidence) + if (!pin || sourceReceipt.canonicalNodeId === remoteReceipt.canonicalNodeId) refuse() + const originalPin = pin + const participants = ()=>freeze({ sourceEvidence, remoteEvidence, sourceReceipt, remoteReceipt }) + const remoteSchema = z.object({ proof: hash, expiresAt: positive }).strict() + await observePhase() + phase="remote-policy";const peer = freeze(remoteSchema.parse(capture(await verifyRemote(originalContext, original, participants(), preScope)))) + if (peer.expiresAt <= Date.now()) refuse() + const peerMonotonicEnd=enteredMonotonic+Math.max(0,peer.expiresAt-entered) + const beforeDispatch = async () => { + await nativeFresh(preScope) + const currentPeer = remoteSchema.parse(capture(await verifyRemote(originalContext, original, participants(), preScope))) + if (!isDeepStrictEqual(currentPeer, peer) || Date.now() >= peer.expiresAt || performance.now()>=peerMonotonicEnd) refuse() + await actor(preScope) + } + type PreparedRemote = Readonly<{ verify(): Promise; close(): void }> + let preparedRemote: PreparedRemote | undefined + const prepareRemote = async (current: Readonly): Promise => { + if (!acquireRemoteFence) return undefined + if (preparedRemote || remotePhase || retireRemote) refuse() + live(current) + const wall = Date.now(), mono = performance.now() + const originalScope = Object.freeze({ deadlineAt: Math.min(current.deadlineAt, peer.expiresAt, wall + 5000), signal: current.signal }) + const cap = Object.freeze({ deadlineAt: originalScope.deadlineAt, end: Math.min(monotonicEnd, peerMonotonicEnd, mono + Math.max(0, originalScope.deadlineAt - wall)) }) + remotePhase = cap; schedule() + let active = true, pending = true, physicallyClosed = false, closeFailed = false + let owned: Readonly | undefined + const close = () => { + active = false + if (physicallyClosed) { if (closeFailed) refuse(); return } + if (!owned && pending) return // Late acquire must still be captured/destroyed. + let failure = false + if (owned) { + for (let attempt = 0; attempt < 2; attempt++) { + try { owned.close(); physicallyClosed = true; break } catch { failure = true } + } + } else physicallyClosed = true + if (failure || !physicallyClosed) { closeFailed = true; refuse() } + if (remotePhase === cap) remotePhase = undefined + if (retireRemote === close) retireRemote = undefined + if (running && !stopping && !controller.signal.aborted) schedule() + } + retireRemote = close + const verify = async () => { + if (!active || !owned || physicallyClosed || remotePhase !== cap) refuse() + live(originalScope) + // Actual producer receives the EXACT preparation scope it acquired; + // the private kernel/composite's later scope cannot renew this lease. + await owned.verify(originalScope); live(originalScope) + await actor(originalScope); live(originalScope) + } + try { + const acquired = await acquireRemoteFence(originalContext, original, participants(), peer, originalScope) + pending = false + if (!acquired || typeof acquired.verify !== "function" || typeof acquired.close !== "function") { + try { acquired?.close?.() } catch { try { acquired?.close?.() } catch { /* Never return a malformed/late lease healthy. */ } } + refuse() + } + owned = Object.freeze({ verify: acquired.verify.bind(acquired), close: acquired.close.bind(acquired) }) + if (!active) { close(); refuse() } + await verify(); await beforeDispatch(); live(originalScope) + return Object.freeze({ verify, close }) + } catch (error) { pending = false; try { close() } catch { /* Conservative refusal/unknown; no post. */ } throw error } + } + const identityMatches = (value: { installationId: string; controlGeneration: string; sourceRecordSha256: string }) => { + if (value.installationId !== originalPin.installationId || value.controlGeneration !== originalPin.controlGeneration || value.sourceRecordSha256 !== originalPin.sourceRecordSha256) refuse() + } + const aliasFence = (fence: BandwidthControlDispatchFence, current: BandwidthScope) => nodes.assertResolvedPeersOnFence(fence, { + binding: originalPin, participants: [{ evidence: sourceEvidence, receipt: sourceReceipt }, { evidence: remoteEvidence, receipt: remoteReceipt }], + }, current) + const dispatchGuard = (current: BandwidthScope, execute: boolean) => async (fence: BandwidthControlDispatchFence) => { + identityMatches(await fence.sourceIdentity()) + if (execute) { await aliasFence(fence, current); await beforeDispatch() } + } + const endpointProof = async (receipt: BandwidthPeerRegistryReceipt, evidence: BandwidthPeerObservationEvidence) => core.admissionProof("bandwidth-endpoint-v1", { receipt, subject:nodeSubject(evidence), peer, sourceRecordSha256: originalPin.sourceRecordSha256 }, originalPin.sourceRecordSha256, preScope) + phase="participant-proof";const source = { canonicalNodeId: sourceReceipt.canonicalNodeId, endpointProof: await endpointProof(sourceReceipt, sourceEvidence) } + const destination = { canonicalNodeId: remoteReceipt.canonicalNodeId, endpointProof: await endpointProof(remoteReceipt, remoteEvidence) } + phase="strict-client";const client = await observer.strictClient(sourceToken, preScope); await beforeDispatch() + let activeJournal: { connection: ProbeStorageConnection; scope: BandwidthScope } | undefined + let originalBinding: Readonly | undefined, sendInvocations = 0, terminal: { bindingProof: string; proof: string } | undefined + let transition: BandwidthTransition | undefined + const own = (binding: Readonly) => { + if (originalBinding && !isDeepStrictEqual(originalBinding, binding)) refuse() + originalBinding ??= freeze(structuredClone(binding)) + } + const baseJournal = createBandwidthAdmissionSqlJournal({ now: Date.now, + connect: async current => { + if (activeJournal) refuse() + const connection = await core.connect(current, dispatchGuard(current, transition === "reserve" || transition === "arm")) + activeJournal = { connection, scope: current } + return { query: (text, values, options) => connection.query(text, values, options), release(unsafe) { activeJournal = undefined; connection.release(unsafe) } } + }, + authorize: async (binding, current, transition) => { own(binding); if (transition === "reserve" || transition === "arm") await beforeDispatch(); else if (current.signal?.aborted || Date.now() >= current.deadlineAt) refuse() }, + verifyEvidence: async (transition, binding, evidence) => { + own(binding) + if (transition === "complete") { if (!terminal || terminal.bindingProof !== binding.bindingProof || !isDeepStrictEqual(evidence, { terminalProof: terminal.proof })) refuse() } + else if (transition === "release-before-dispatch") { if (sendInvocations !== 0 || !evidence || !["reserved", "armed"].includes((evidence as { from: string }).from) || (evidence as { proof: string }).proof !== "private-no-send") refuse() } + else if (evidence !== undefined) refuse() + }, + eventProof: async event => { const current = activeJournal; if (!current) refuse(); return core.eventProof(event, current.connection, current.scope) }, + }) + // Track the transition explicitly: cleanup scopes intentionally do NOT + // inherit expired execute/native-observation authority or cancellation. + const inTransition = async (kind: BandwidthTransition, work: () => Promise) => { + if (transition) refuse(); transition = kind;phase=kind + try { return await work() } finally { transition = undefined } + } + const journal: BandwidthJournal = Object.freeze({ + reserve: async(binding, current) => {await observePhase();mutationPossible=true;return inTransition("reserve", () => baseJournal.reserve(binding, current))}, + arm: async(binding, current) => {await observePhase();return inTransition("arm", () => baseJournal.arm(binding, current))}, + complete: (binding, evidence, current) => inTransition("complete", () => baseJournal.complete(binding, evidence, current)), + releaseBeforeDispatch: (binding, evidence, current) => inTransition("release-before-dispatch", () => baseJournal.releaseBeforeDispatch(binding, evidence, current)), + quarantine: (binding, current) => inTransition("quarantine", () => baseJournal.quarantine(binding, current)), + }) + const admissionInput: BandwidthAdmissionInput = { attemptId: original.attemptId, callerKind: original.callerKind, operationId: original.operationId, + request: { originalRequest: original.request, context: { sourceServerId: original.sourceServerId, actualRemotePeer: original.actualRemotePeer, credentialServerId: original.credentialServerId, + socketTimeoutMs: original.socketTimeoutMs, peerProof: peer.proof } }, source, destination, authorityProof: original.authorityProof, + authorityGeneration: original.authorityGeneration, controlGeneration: originalPin.controlGeneration, expiresAt: original.expiresAt, durationSec: original.durationSec, nativeBody: original.nativeBody } + const admission = createBandwidthAdmission(admissionInput, { now: Date.now, cleanupBudgetMs: 10000, nativeClient: client, journal, + nativeDispatchGate: Object.freeze({ acquire: async (current: Readonly) => { + // Refresh observations before taking the short SQL lease. The gate + // cannot renew the original actor/peer proof, binding or source pin. + await observePhase() + if (!originalBinding) refuse() + const gate = createBandwidthNativeDispatchSqlGate({ connect: core.connect, eventProofOnConnection: core.eventProof }, originalBinding, + async (fence, ownedScope) => { identityMatches(await fence.sourceIdentity()); await aliasFence(fence, ownedScope); await beforeDispatch(); live(ownedScope) }) + if (!acquireRemoteFence) return gate.acquire(current) + const prepared = preparedRemote + if (!prepared) refuse() + // Mapping S was prepared BEFORE client.post acquired managed-server + // locks. This gate BORROWS it; it must never acquire another mapping + // lease under those locks. The kernel still owns its own exact scope. + return createBandwidthNativeDispatchCompositeGate([ + { acquire: async borrowedScope => { + live(borrowedScope); await prepared.verify(); live(borrowedScope) + return Object.freeze({ + async verify(next: Readonly) { + if (next.deadlineAt !== borrowedScope.deadlineAt || next.signal !== borrowedScope.signal) refuse() + live(next); await prepared.verify(); live(next) + }, + close: prepared.close, + }) + } }, gate, + ]).acquire(current) + } }), + authorize: async (_binding, current) => { await beforeDispatch(); live(current); return { authorityProof: original.authorityProof, authorityGeneration: original.authorityGeneration, controlGeneration: originalPin.controlGeneration, expiresAt: original.expiresAt } }, + endpoints: async () => { await beforeDispatch(); return { source, destination } }, + keyedProof: async (purpose, value) => { + const current = purpose === "bandwidth-terminal-v1" ? { deadlineAt: Date.now() + 10000 } : preScope + const proof = await core.admissionProof(purpose, value, originalPin.sourceRecordSha256, current) + if (purpose === "bandwidth-terminal-v1") { phase="terminal-proof";if (!originalBinding) refuse(); terminal = { bindingProof: originalBinding.bindingProof, proof } } + return proof + }, + send: async (_binding, body, current, permit) => { + // A send invocation is already potentially ambiguous. No later + // transport exception can fabricate private no-send evidence. + sendInvocations++ + phase="native-send" + try { + preparedRemote = await prepareRemote(current) + live(current) + return await client.post("/tool/bandwidth-test", { ...body }, original.socketTimeoutMs, current.signal, undefined, permit) + } finally { + try { preparedRemote?.close() } finally { preparedRemote = undefined } + } + } }) + phase="reserve-arm";const capability=await admission.reserveAndArm(executeScope) + try{await observePhase()}catch(error){await admission.cancel(capability);throw error} + const result = freeze(await admission.dispatch(capability)) + completions.set(result, Object.freeze({ originalContext, intent: original })) + return result + } + try { return await Promise.race([work(), stopped]) } + catch { throw new BandwidthPeerExecutionHostError(mutationPossible?"OUTCOME_UNKNOWN":"REFUSED",phase) } + finally { + running = false; clearTimeout(timer) + try { retireRemote?.() } catch { /* Already unknown; never return a failed physical close healthy. */ } + parentSignal?.removeEventListener("abort", abort); controller.signal.removeEventListener("abort", stop); controller.abort() + } + } }) + executionHosts.add(host); return host +} + diff --git a/backend/src/services/bandwidth-peer-fence.test.ts b/backend/src/services/bandwidth-peer-fence.test.ts new file mode 100644 index 00000000..7542bf61 --- /dev/null +++ b/backend/src/services/bandwidth-peer-fence.test.ts @@ -0,0 +1,121 @@ +import assert from "node:assert/strict" +import { createHash, createHmac, randomUUID } from "node:crypto" +import { mkdtemp, writeFile, rm } from "node:fs/promises" +import path from "node:path" +import os from "node:os" +import { mock, test } from "node:test" +import { canonicalBandwidthNodeValue, bandwidthPeerRegistrySchemaSourceSha256 } from "./bandwidth-node-bootstrap.js" +import type { BandwidthResolvedPeerFence } from "./bandwidth-peer-fence.js" +import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js" + +test("v2 peer participant fence: exact native claims/alias history on one existing lease", async t => { + mock.module("./bandwidth-control-registry-identity.js",{namedExports:{assertBandwidthControlRegistryIdentity:async()=>{throw Error("isolated peer fence must not invoke joint SQL")}}}) + let managedRoles = 0, externalRoles = 0, catalogs = 0, catalogAllowed = true, roleAllowed = true + mock.module("./bandwidth-node-registry-catalog.js", { namedExports: { + observeBandwidthNodeRegistryCatalog: async () => { throw Error("v2 must never fall back") }, + observeBandwidthPeerRegistryCatalog: async (_sql: unknown, live: () => void) => { catalogs++; live(); if (!catalogAllowed) throw Error("catalog refused"); return "c".repeat(64) }, + } }) + mock.module("./bandwidth-node-registry-role.js", { namedExports: { assertBandwidthNodeRegistryRuntimeRole: async (_sql: unknown, _role: unknown, live: () => void) => { managedRoles++; live() } } }) + mock.module("./bandwidth-external-node-registry-role.js", { namedExports: { assertBandwidthExternalNodeRegistryRuntimeRole: async (_sql: unknown, _role: unknown, live: () => void) => { externalRoles++; live(); if (!roleAllowed) throw Error("role refused") } } }) + // This suite fakes metadata IO only; the batch's exact SQL/validators have + // their own unit and real PostgreSQL profiles. + mock.module("./bandwidth-peer-identity.js", { namedExports: { assertBandwidthPeerIdentity: async (sql: { query(text: string): Promise<{ rows: Record[] }> }, expected: Record, live: () => void) => { + managedRoles++; externalRoles++; catalogs++; live() + if (!catalogAllowed || !roleAllowed || expected.registryCatalogSha256 !== "c".repeat(64)) throw Error("identity refused") + const rows = (await sql.query("SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status FROM mmapp_control.bandwidth_node_authority WHERE id=1")).rows + live() + if (rows.length !== 1 || rows[0].installation_id !== expected.installationId || rows[0].issuer_key_sha256 !== expected.issuerKeySha256 || rows[0].quiescence_proof !== expected.quiescenceProof || rows[0].status !== "active") throw Error("authority refused") + } } }) + const { assertResolvedBandwidthPeersOnFence, BandwidthPeerFenceError } = await import("./bandwidth-peer-fence.js") + const directory = await mkdtemp(path.join(os.tmpdir(), "mm-peer-fence-")), issuerKeyFile = path.join(directory, "issuer"), bootstrapFile = path.join(directory, "bootstrap") + const key = Buffer.alloc(32, 83), sha = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") + await writeFile(issuerKeyFile, key); await writeFile(bootstrapFile, JSON.stringify({ unitOnly: true })) + const root = { installationId: randomUUID(), issuerKeySha256: sha(key), quiescenceProof: "b".repeat(64) }, source = { controlGeneration: randomUUID(), sourceRecordSha256: "d".repeat(64), databaseName: "unit-db", ownerRole: "unit-owner", runtimeRole: "unit-runtime" } + const config = { issuerKeyFile, issuerKeySha256: root.issuerKeySha256, bootstrapFile }, sessions = new Set<() => void>() + let bootstrap = { ...root, format: "mmapp-bandwidth-node-bootstrap-v2", registrySchemaSourceSha256: bandwidthPeerRegistrySchemaSourceSha256(), registryCatalogSha256: "c".repeat(64), attestationRef: "unit-only explicit operator attestation" } + const proof = (purpose: string, value: unknown) => createHmac("sha256", key).update("mmapp-bandwidth-node-" + purpose + "-v1:" + canonicalBandwidthNodeValue(value)).digest("hex") + let input: BandwidthResolvedPeerFence, calls: { text: string; values?: unknown[] }[] = [], aliasFault: ((row: Record) => void) | undefined, + eventFault: ((row: Record) => void) | undefined, sqlHook: (() => Promise | void) | undefined, locked = true + const eventIds = new Map() + function reset(managedRemote = false) { + bootstrap = { ...bootstrap, ...root, format: "mmapp-bandwidth-node-bootstrap-v2" }; catalogAllowed = true; roleAllowed = true; locked = true + calls = []; aliasFault = undefined; eventFault = undefined; sqlHook = undefined + const refs: BandwidthPeerRef[] = [{ kind: "server", serverId: 101 }, managedRemote ? { kind: "server", serverId: 102 } : { kind: "external", enrollmentId: randomUUID() }] + input = { binding: { ...root, controlGeneration: source.controlGeneration, sourceRecordSha256: source.sourceRecordSha256, bootstrapSha256: sha(canonicalBandwidthNodeValue(bootstrap)) }, + participants: refs.map((peerRef, index) => ({ evidence: { version: 2, peerRef, sourceProof: "a".repeat(64), authorityGeneration: "unit-grant", routingEndpointEpoch: randomUUID(), observedAt: Date.now(), expiresAt: Date.now() + 5000, + claims: [{ kind: "software-id", value: "UNIT-LICENSE-" + index }], physicalUniquenessVerified: false, cloneResistanceVerified: false }, + receipt: { peerRef, canonicalNodeId: randomUUID(), incarnation: randomUUID(), status: "active", outcome: "existing" } })) } + } + const fence = { verifyNodeBootstrap: async () => ({ bootstrap: structuredClone(bootstrap), source: { ...root, ...source } }), async query(text: string, values?: unknown[]) { + calls.push({ text, values }); await sqlHook?.() + if (text.includes("pg_try_advisory")) return { rows: [{ locked }] } + if (text.includes("FROM mmapp_control.bandwidth_node_authority")) return { rows: [{ installation_id: root.installationId, issuer_key_sha256: root.issuerKeySha256, quiescence_proof: root.quiescenceProof, status: "active" }] } + const external = text.includes("bandwidth_external_node_"), participant = input.participants.find(item => item.receipt.peerRef.kind === (external ? "external" : "server") && (item.receipt.peerRef.kind === "server" ? item.receipt.peerRef.serverId : item.receipt.peerRef.enrollmentId) === values?.[0]); assert(participant) + const r = participant.receipt, ref = r.peerRef, subject = ref.kind === "server" ? { serverId: ref.serverId } : { enrollmentId: ref.enrollmentId }, claimProof = proof("claim", participant.evidence.claims) + if (text.includes("aliases a JOIN")) { + const row: Record = { ...(ref.kind === "server" ? { server_id: ref.serverId } : { enrollment_id: ref.enrollmentId }), incarnation: r.incarnation, node_id: r.canonicalNodeId, status: "active", claim_proof: claimProof, + enrollment_mac: proof("node", { ...root, nodeId: r.canonicalNodeId, claimProof }), record_mac: proof(external ? "external-alias" : "alias", { ...root, ...subject, incarnation: r.incarnation, nodeId: r.canonicalNodeId, status: "active" }) } + aliasFault?.(row); return { rows: [row] } + } + assert(text.includes("events WHERE")); const id = String(values?.[0]); if (!eventIds.has(id)) eventIds.set(id, randomUUID()); const eventId = eventIds.get(id)! + const row: Record = { event_id: eventId, incarnation: r.incarnation, node_id: r.canonicalNodeId, kind: "enroll", old_status: null, new_status: "active", authority_proof: "f".repeat(64), + event_mac: proof(external ? "external-event" : "event", { ...root, ...subject, eventId, incarnation: r.incarnation, nodeId: r.canonicalNodeId, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: "f".repeat(64) }) } + eventFault?.(row); return { rows: [row] } + } } + const scope = () => ({ deadlineAt: Date.now() + 5000 }) + const check = (current = scope()) => assertResolvedBandwidthPeersOnFence(config, sessions, () => false, fence as never, input, current) + try { + await t.test("mixed managed/external and managed/managed verify all MAC domains with static parameterized SQL", async () => { + for (const managed of [false, true]) { + reset(managed); await check(); assert.equal(sessions.size, 0) + assert.equal(calls.length, 6); assert(calls.every(call => !/\b(?:INSERT|UPDATE|DELETE|BEGIN|COMMIT|ROLLBACK)\b/.test(call.text))) + assert(calls[0].text.includes("pg_try_advisory_xact_lock_shared(8723116)")) + assert.equal(calls.filter(call => call.text.includes("bandwidth_external_node_")).length, managed ? 0 : 2) + } + assert.equal(managedRoles, 2); assert.equal(externalRoles, 2); assert.equal(catalogs, 2) + }) + await t.test("unknown/changed participant references, duplicate canonical identity and hostile descriptors fail before SQL", async () => { + for (const fault of ["ref", "canonical", "source", "terminal", "getter"] as const) { + reset(); const mutable = input as unknown as { participants: { receipt: { peerRef: BandwidthPeerRef; canonicalNodeId: string; status: string }; evidence: object }[] } + if (fault === "ref") mutable.participants[1].receipt.peerRef = { kind: "external", enrollmentId: randomUUID() } + if (fault === "canonical") mutable.participants[1].receipt.canonicalNodeId = mutable.participants[0].receipt.canonicalNodeId + if (fault === "source") mutable.participants.reverse() + if (fault === "terminal") mutable.participants[1].receipt.status = "tombstoned" + let reads = 0; if (fault === "getter") Object.defineProperty(mutable.participants[1].evidence, "claims", { enumerable: true, get() { reads++; return [] } }) + await assert.rejects(check(), BandwidthPeerFenceError); assert.equal(calls.length, 0); assert.equal(reads, 0) + } + }) + await t.test("alias/node/claim/event corruption and terminal alias status refuse", async () => { + for (const field of ["record_mac", "enrollment_mac", "claim_proof", "status", "incarnation", "node_id"]) { + reset(); aliasFault = row => { row[field] = field === "status" ? "tombstoned" : "0".repeat(64) }; await assert.rejects(check(), BandwidthPeerFenceError) + } + for (const field of ["event_mac", "authority_proof", "old_status", "new_status", "kind", "incarnation"]) { + reset(); eventFault = row => { row[field] = "changed" }; await assert.rejects(check(), BandwidthPeerFenceError) + } + }) + await t.test("signed v1/root drift/catalog/role rejection and lost shared lock never fall back", async () => { + for (const fault of ["v1", "root", "catalog", "role", "lock"] as const) { + reset(); if (fault === "v1") bootstrap.format = "mmapp-bandwidth-node-bootstrap-v1" + if (fault === "root") bootstrap.quiescenceProof = "e".repeat(64) + if (fault === "catalog") catalogAllowed = false + if (fault === "role") roleAllowed = false + if (fault === "lock") locked = false + await assert.rejects(check(), BandwidthPeerFenceError) + } + }) + await t.test("key/source pin changes during awaited rows cannot yield a stale last verification", async () => { + reset(); sqlHook = async () => { if (calls.length === 6) await writeFile(issuerKeyFile, Buffer.alloc(32, 99)) } + await assert.rejects(check(), BandwidthPeerFenceError); await writeFile(issuerKeyFile, key) + reset(); sqlHook = () => { if (calls.length === 6) bootstrap.quiescenceProof = "e".repeat(64) }; await assert.rejects(check(), BandwidthPeerFenceError) + }) + await t.test("abort/deadline stop late SQL continuation and clear tracked windows", async () => { + reset(); let resume!: () => void, started!: () => void; const pending = new Promise(resolve => { resume = resolve }), dispatched = new Promise(resolve => { started = resolve }) + sqlHook = () => { started(); return pending } + const controller = new AbortController(), work = check({ ...scope(), signal: controller.signal } as never); await dispatched; controller.abort() + await assert.rejects(work, BandwidthPeerFenceError); resume(); await new Promise(resolve => setTimeout(resolve, 10)); assert.equal(calls.length, 1); assert.equal(sessions.size, 0) + reset(); let resumeDeadline!: () => void; const deadlinePending = new Promise(resolve => { resumeDeadline = resolve }); sqlHook = () => deadlinePending; const now = Date.now + try { const work = check({ deadlineAt: now() + 30 }); Date.now = () => now() - 10000; await assert.rejects(work, BandwidthPeerFenceError) } finally { Date.now = now } + resumeDeadline(); await new Promise(resolve => setTimeout(resolve, 10)); assert.equal(sessions.size, 0); assert.equal(calls.length, 1) + }) + } finally { mock.restoreAll(); await rm(directory, { recursive: true, force: true }) } +}) diff --git a/backend/src/services/bandwidth-peer-fence.ts b/backend/src/services/bandwidth-peer-fence.ts new file mode 100644 index 00000000..fa427a44 --- /dev/null +++ b/backend/src/services/bandwidth-peer-fence.ts @@ -0,0 +1,149 @@ +/** Read-only v2 participant verification on the caller's EXISTING core fence. + * No lease acquisition, registration, native transport or runtime activation. */ +import { createHash, createHmac } from "node:crypto" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { readBandwidthControlFile, type BandwidthControlDispatchFence } from "./bandwidth-control-source.js" +import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js" +import { bandwidthNodeBindingSchema, type BandwidthNodeBinding } from "./bandwidth-node-fence.js" +import { parseBandwidthPeerRef, bandwidthPeerRefKey, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthNodeSourceConfig } from "./bandwidth-node-source.js" +import type { BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import type { BandwidthScope } from "./bandwidth-admission.js" + +export interface BandwidthPeerRegistryReceipt { + readonly peerRef: BandwidthPeerRef; readonly canonicalNodeId: string; readonly incarnation: string + readonly status: "active" | "tombstoned" | "quarantined"; readonly outcome: "new" | "existing" +} +export interface BandwidthResolvedPeerFence { + readonly binding: Readonly + readonly participants: readonly { evidence: Readonly; receipt: Readonly }[] +} +export class BandwidthPeerFenceError extends Error { + readonly code = "BANDWIDTH_PEER_FENCE_REFUSED" + constructor() { super("Актуальная постоянная привязка bandwidth peers не подтверждена") } +} +function fail(): never { throw new BandwidthPeerFenceError() } +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +const peer = z.unknown().transform(value => parseBandwidthPeerRef(value)) +const claim = z.object({ kind: z.enum(["system-id", "software-id"]), value: z.string().min(1).refine(value => Buffer.byteLength(value) <= 256 && value.trim() === value && !/[\u0000-\u001f\u007f]/.test(value)) }).strict() +const evidenceSchema = z.object({ version: z.literal(2), peerRef: peer, sourceProof: hash, authorityGeneration: z.string().min(1).max(200), + routingEndpointEpoch: uuid, observedAt: z.number().int().nonnegative().safe(), expiresAt: z.number().int().positive().safe(), + claims: z.array(claim).min(1).max(2).refine(value => value.length === 1 || value[0].kind === "system-id" && value[1].kind === "software-id"), + physicalUniquenessVerified: z.literal(false), cloneResistanceVerified: z.literal(false) }).strict() +const receiptSchema = z.object({ peerRef: peer, canonicalNodeId: uuid, incarnation: uuid, status: z.literal("active"), outcome: z.enum(["new", "existing"]) }).strict() +const expectedSchema = z.object({ binding: bandwidthNodeBindingSchema, participants: z.array(z.object({ evidence: evidenceSchema, receipt: receiptSchema }).strict()).length(2) }).strict() +const digest = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +/** Never run caller accessors/serializers while capturing a fence expectation. */ +function capture(value: unknown): unknown { + const seen = new Set() + const walk = (item: unknown, depth: number): unknown => { + if (depth > 32) fail() + if (item === null || typeof item === "string" || typeof item === "boolean" || typeof item === "number" && Number.isSafeInteger(item)) return item + if (!item || typeof item !== "object" || seen.has(item)) fail() + seen.add(item) + try { + const descriptors = Object.getOwnPropertyDescriptors(item) + if (Object.values(descriptors).some(field => !("value" in field))) fail() + if (Array.isArray(item)) { + if (Reflect.ownKeys(item).length !== item.length + 1) fail() + return Array.from({ length: item.length }, (_, index) => { const entry = descriptors[String(index)]; if (!entry?.enumerable) fail(); return walk(entry.value, depth + 1) }) + } + if (Object.getPrototypeOf(item) !== Object.prototype || Reflect.ownKeys(item).length !== Object.keys(descriptors).length || Object.values(descriptors).some(field => !field.enumerable)) fail() + return Object.fromEntries(Object.entries(descriptors).map(([key, entry]) => [key, walk(entry.value, depth + 1)])) + } finally { seen.delete(item) } + } + const result = walk(value, 0) + if (Buffer.byteLength(JSON.stringify(result)) > 1048576) fail() + return result +} + +export async function assertResolvedBandwidthPeersOnFence(config: Readonly, sessions: Set<() => void>, closed: () => boolean, + fence: BandwidthControlDispatchFence, input: BandwidthResolvedPeerFence, original: BandwidthScope): Promise { + let expected: z.infer + try { expected = expectedSchema.parse(capture(input)) } catch { return fail() } + if (!original || Object.getPrototypeOf(original) !== Object.prototype || Reflect.ownKeys(original).length !== Object.keys(original).length + || Object.keys(original).some(key => !["deadlineAt", "signal"].includes(key)) || Object.values(Object.getOwnPropertyDescriptors(original)).some(field => !("value" in field) || !field.enumerable) + || !Number.isSafeInteger(original.deadlineAt) || original.signal !== undefined && !(original.signal instanceof AbortSignal)) fail() + const source = expected.participants[0], remote = expected.participants[1] + if (source.evidence.peerRef.kind !== "server" || source.receipt.canonicalNodeId === remote.receipt.canonicalNodeId + || bandwidthPeerRefKey(source.evidence.peerRef) === bandwidthPeerRefKey(remote.evidence.peerRef)) fail() + for (const participant of expected.participants) { + if (bandwidthPeerRefKey(participant.evidence.peerRef) !== bandwidthPeerRefKey(participant.receipt.peerRef) + || participant.evidence.observedAt > Date.now() || participant.evidence.observedAt >= participant.evidence.expiresAt) fail() + } + const entered = Date.now(), enteredMono = performance.now() + const scope = Object.freeze({ deadlineAt: Math.min(original.deadlineAt, ...expected.participants.map(participant => participant.evidence.expiresAt)), signal: original.signal }) + const end = enteredMono + Math.max(0, Math.min(10000, scope.deadlineAt - entered)) + let active = true, rejectStopped!: (error: Error) => void + const keys = new Set(), wipe = (key: Buffer) => { key.fill(0); keys.delete(key) } + const live = () => { if (!active || closed() || scope.signal?.aborted || Date.now() >= scope.deadlineAt || performance.now() >= end) fail() } + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + const stop = () => { active = false; for (const key of keys) key.fill(0); keys.clear(); rejectStopped(new BandwidthPeerFenceError()) } + const timer = setTimeout(stop, Math.max(1, Math.min(scope.deadlineAt - Date.now(), end - performance.now()))) + sessions.add(stop); scope.signal?.addEventListener("abort", stop, { once: true }) + const root = { installationId: expected.binding.installationId, issuerKeySha256: expected.binding.issuerKeySha256, quiescenceProof: expected.binding.quiescenceProof } + async function material() { + live() + const bytes = await readBandwidthControlFile(config.bootstrapFile, 32768, live) + const verified = await fence.verifyNodeBootstrap(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes))); live() + if (verified.bootstrap.format !== "mmapp-bandwidth-node-bootstrap-v2") fail() + const actual = { installationId: verified.bootstrap.installationId, issuerKeySha256: verified.bootstrap.issuerKeySha256, quiescenceProof: verified.bootstrap.quiescenceProof, + bootstrapSha256: digest(canonicalBandwidthNodeValue(verified.bootstrap)), controlGeneration: verified.source.controlGeneration, sourceRecordSha256: verified.source.sourceRecordSha256 } + if (canonicalBandwidthNodeValue(actual) !== canonicalBandwidthNodeValue(expected.binding) || config.issuerKeySha256 !== actual.issuerKeySha256) fail() + let key: Buffer | undefined + try { + key = await readBandwidthControlFile(config.issuerKeyFile, 32, live); keys.add(key); live() + if (key.length !== 32 || digest(key) !== config.issuerKeySha256) fail() + return { verified, key } + } catch (error) { if (key) wipe(key); throw error } + } + const sql = { query: async (text: string, values?: unknown[]) => { live(); const result = await fence.query(text, values); live(); return result } } + try { + await Promise.race([Promise.resolve().then(async () => { + // Fixed registry writers take the exclusive key. The caller's existing + // journal transaction must retain this shared lock through dispatch; + // an isolated implicit SELECT does not establish a native-send barrier. + const lock = (await sql.query("SELECT pg_catalog.pg_try_advisory_xact_lock_shared(8723116) AS locked")).rows + if (lock.length !== 1 || lock[0].locked !== true) fail() + const before = await material() + try { + const value = before.verified, role = { databaseName: value.source.databaseName, runtimeRole: value.source.runtimeRole, ownerRole: value.source.ownerRole } + const { assertBandwidthPeerIdentity } = await import("./bandwidth-peer-identity.js") + live(); await assertBandwidthPeerIdentity(sql, { ...role, ...root, registryCatalogSha256: value.bootstrap.registryCatalogSha256 }, live) + const pending = [] + for (const participant of expected.participants) { + const ref = participant.receipt.peerRef, managed = ref.kind === "server", id = managed ? ref.serverId : ref.enrollmentId + // Both SQL branches are fixed. Identifiers never come from the peer. + const rows = (await sql.query(managed + ? "SELECT a.server_id,a.incarnation::pg_catalog.text,a.node_id::pg_catalog.text,a.status,a.record_mac,n.claim_proof,n.enrollment_mac FROM mmapp_control.bandwidth_node_aliases a JOIN mmapp_control.bandwidth_nodes n ON n.node_id=a.node_id WHERE a.server_id=$1" + : "SELECT a.enrollment_id::pg_catalog.text,a.incarnation::pg_catalog.text,a.node_id::pg_catalog.text,a.status,a.record_mac,n.claim_proof,n.enrollment_mac FROM mmapp_control.bandwidth_external_node_aliases a JOIN mmapp_control.bandwidth_nodes n ON n.node_id=a.node_id WHERE a.enrollment_id=$1", [id])).rows + if (rows.length !== 1) fail() + const row = rows[0], receipt = participant.receipt + if ((managed ? Number(row.server_id) !== id : row.enrollment_id !== id) || row.incarnation !== receipt.incarnation || row.node_id !== receipt.canonicalNodeId || row.status !== "active" || !hash.safeParse(row.claim_proof).success) fail() + const events = (await sql.query(managed + ? "SELECT event_id::pg_catalog.text,incarnation::pg_catalog.text,node_id::pg_catalog.text,kind,old_status,new_status,authority_proof,event_mac FROM mmapp_control.bandwidth_node_events WHERE server_id=$1 ORDER BY kind LIMIT 3" + : "SELECT event_id::pg_catalog.text,incarnation::pg_catalog.text,node_id::pg_catalog.text,kind,old_status,new_status,authority_proof,event_mac FROM mmapp_control.bandwidth_external_node_events WHERE enrollment_id=$1 ORDER BY kind LIMIT 3", [id])).rows + if (events.length !== 1) fail() + const event = events[0] + if (!uuid.safeParse(event.event_id).success || event.incarnation !== receipt.incarnation || event.node_id !== receipt.canonicalNodeId || event.kind !== "enroll" || event.old_status !== null || event.new_status !== "active" || !hash.safeParse(event.authority_proof).success) fail() + pending.push({ participant, row, event }) + } + const after = await material() + try { + const proof = (purpose: string, value: unknown) => createHmac("sha256", after.key).update("mmapp-bandwidth-node-" + purpose + "-v1:" + canonicalBandwidthNodeValue(value)).digest("hex") + for (const { participant, row, event } of pending) { + const receipt = participant.receipt, ref = receipt.peerRef + const subject = ref.kind === "server" ? { serverId: ref.serverId } : { enrollmentId: ref.enrollmentId } + if (row.claim_proof !== proof("claim", participant.evidence.claims) + || row.enrollment_mac !== proof("node", { ...root, nodeId: receipt.canonicalNodeId, claimProof: row.claim_proof }) + || row.record_mac !== proof(ref.kind === "server" ? "alias" : "external-alias", { ...root, ...subject, incarnation: receipt.incarnation, nodeId: receipt.canonicalNodeId, status: "active" }) + || event.event_mac !== proof(ref.kind === "server" ? "event" : "external-event", { ...root, ...subject, eventId: event.event_id, incarnation: receipt.incarnation, nodeId: receipt.canonicalNodeId, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: event.authority_proof })) fail() + } + live() + } finally { wipe(after.key) } + } finally { wipe(before.key) } + }), stopped]) + } catch { throw new BandwidthPeerFenceError() } + finally { active = false; clearTimeout(timer); scope.signal?.removeEventListener("abort", stop); sessions.delete(stop); for (const key of keys) key.fill(0); keys.clear() } +} diff --git a/backend/src/services/bandwidth-peer-identity.test.ts b/backend/src/services/bandwidth-peer-identity.test.ts new file mode 100644 index 00000000..24f0e352 --- /dev/null +++ b/backend/src/services/bandwidth-peer-identity.test.ts @@ -0,0 +1,195 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "./bandwidth-admission-schema.js" +import { BANDWIDTH_NODE_REGISTRY_DDL } from "./bandwidth-node-registry-schema.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL } from "./bandwidth-external-node-registry-schema.js" +import { BANDWIDTH_PEER_IDENTITY_SQL, assertBandwidthPeerIdentity, validateBandwidthPeerIdentityRows, BandwidthPeerIdentityRefused, type BandwidthPeerIdentityExpected } from "./bandwidth-peer-identity.js" +import { validateBandwidthPeerRegistryCatalogRows, validateBandwidthNodeRegistryCatalogRows } from "./bandwidth-node-registry-catalog.js" +type Row=Record +type Item={section:string;raw_json:string|null} +type Batch={kind:string;row_order:string;raw_json:string|null} +// Inert DDL-derived catalog facts, matching the independent component tests. +// No database callbacks or observer replays implement the batch validator. +function managedFixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_node_authority","bandwidth_nodes","bandwidth_node_aliases","bandwidth_node_events"] + const functions=["bandwidth_node_authority_guard","bandwidth_node_immutable_guard","bandwidth_node_alias_guard","bandwidth_node_event_guard","bandwidth_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_node_authority:[["p",["id"]]],bandwidth_nodes:[["p",["node_id"]],["u",["claim_proof"]]],bandwidth_node_aliases:[["p",["server_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_node_events:[["p",["event_id"]],["u",["server_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_node_aliases","node_id","bandwidth_nodes"],["bandwidth_node_events","server_id","bandwidth_node_aliases"],["bandwidth_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +function externalFixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_external_node_aliases","bandwidth_external_node_events"] + const functions=["bandwidth_external_node_alias_guard","bandwidth_external_node_event_guard","bandwidth_external_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_external_node_aliases:[["p",["enrollment_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_external_node_events:[["p",["event_id"]],["u",["enrollment_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_external_node_aliases","node_id","bandwidth_nodes"],["bandwidth_external_node_events","enrollment_id","bandwidth_external_node_aliases"],["bandwidth_external_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +const role=(external:boolean)=>({observation:{databaseName:"owned_database",sessionRole:"bandwidth_runtime",currentRole:"bandwidth_runtime",runtimeRoleExists:true,ownerRoleExists:true,runtimeFlagsSafe:true,ownerMembership:false,dangerousMembership:false,schemaExists:true,schemaOwner:"fixed_owner",schemaUsage:true,schemaCreate:false,publicSchemaUsage:false,publicSchemaCreate:false,tables:[external?"bandwidth_external_node_aliases":"bandwidth_node_aliases","bandwidth_nodes",external?"bandwidth_external_node_events":"bandwidth_node_events","bandwidth_node_authority"].map(name=>({name,exists:true,ownerRole:"fixed_owner",ownedByRuntime:false,privilegesExact:true}))}}) +function fixture(){ + const external=externalFixture(),managed=managedFixture() + managed.push(...external.filter(item=>item.section==="triggers"&&JSON.parse(item.raw_json!).table_name==="bandwidth_nodes")) + const incoming=external.filter(item=>item.section==="constraints").map(item=>JSON.parse(item.raw_json!) as Row).filter(row=>row.type==="f"&&row.foreign_table==="bandwidth_nodes").map(row=>({table_name:row.table_name,constraint_name:row.name})) + const expected:BandwidthPeerIdentityExpected={databaseName:"owned_database",runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",installationId:"11111111-1111-4111-8111-111111111111",issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64),registryCatalogSha256:validateBandwidthPeerRegistryCatalogRows(managed,external,incoming)} + const authority={installation_id:expected.installationId,issuer_key_sha256:expected.issuerKeySha256,quiescence_proof:expected.quiescenceProof,status:"active"} + const groups:Record={"managed-role":[role(false)],"external-role":[role(true)],"managed-catalog":managed,"external-catalog":external,incoming,authority:[authority]} + const rows:Batch[]=Object.entries(groups).flatMap(([kind,items])=>items.map(value=>({kind,row_order:"",raw_json:JSON.stringify(value)}))) + ordinal(rows);return{expected,rows,managed,external,incoming} +} +function ordinal(rows:Batch[]){rows.forEach((row,index)=>{row.row_order=String(index+1)});return rows} +const refused=(e:unknown)=>e instanceof BandwidthPeerIdentityRefused +const check=(f:ReturnType)=>validateBandwidthPeerIdentityRows(f.rows,f.expected) +function change(f:ReturnType,kind:string,mutate:(row:Row)=>void){const item=f.rows.find(row=>row.kind===kind)!;assert(item);const value=JSON.parse(item.raw_json!) as Row;mutate(value);item.raw_json=JSON.stringify(value)} +function catalogChange(f:ReturnType,kind:string,section:string,predicate:(row:Row)=>boolean,mutate:(row:Row)=>void){ + const item=f.rows.find(row=>{if(row.kind!==kind)return false;const value=JSON.parse(row.raw_json!) as Item;return value.section===section&&predicate(JSON.parse(value.raw_json!) as Row)})!;assert(item) + const value=JSON.parse(item.raw_json!) as Item,row=JSON.parse(value.raw_json!) as Row;mutate(row);value.raw_json=JSON.stringify(row);item.raw_json=JSON.stringify(value) +} + +test("one materialized SQL snapshot validates both exact roles, complete catalogs/FKs and authority, with three live fences",async()=>{ + const f=fixture(),events:string[]=[],calls:string[]=[] + await assertBandwidthPeerIdentity({async query(text,values){events.push("SQL");calls.push(text);assert.deepEqual(values,[f.expected.databaseName,f.expected.runtimeRole,f.expected.ownerRole]);return{rows:f.rows}}},f.expected,()=>events.push("live")) + assert.deepEqual(events,["live","SQL","live","live"]);assert.equal(calls.length,1);assert.equal(calls[0],BANDWIDTH_PEER_IDENTITY_SQL) + for(const cte of ["managed_role","external_role","managed_catalog","external_catalog","incoming","authority"])assert(calls[0].includes(cte+" AS MATERIALIZED")) + assert(calls[0].includes("ORDER BY c.relname,t.tgname LIMIT 27"));assert(calls[0].includes("ORDER BY c.relname,t.tgname LIMIT 19"));assert(calls[0].includes("row_count<=469"));assert(calls[0].includes("LIMIT 470")) + assert(!/INSERT INTO|CREATE TABLE|CREATE FUNCTION/.test(calls[0]));assert(!calls[0].includes("owned_database")) +}) +test("pure combined digest and batch preserve strict v1 footprint and semantic transport-order independence",()=>{ + const f=fixture();assert.throws(()=>validateBandwidthNodeRegistryCatalogRows(f.managed));assert.equal(validateBandwidthPeerRegistryCatalogRows(f.managed,f.external,f.incoming),f.expected.registryCatalogSha256) + f.rows.reverse();for(const row of f.rows){const value=JSON.parse(row.raw_json!) as Row;row.raw_json=JSON.stringify(Object.fromEntries(Object.entries(value).reverse()))}check(f) +}) +test("missing or duplicated every component refuses without accepting another section as authority",()=>{ + for(const kind of ["managed-role","external-role","managed-catalog","external-catalog","incoming","authority"]){ + const f=fixture();f.rows=ordinal(f.rows.filter(row=>row.kind!==kind));assert.throws(()=>check(f),refused) + const duplicate=fixture(),row=duplicate.rows.find(row=>row.kind===kind)!;duplicate.rows.push({...row});ordinal(duplicate.rows);assert.throws(()=>check(duplicate),refused) + } +}) +test("outer result shape, unknown tags, NULL overflow, malformed JSON and duplicate/missing ordinals fail closed",()=>{ + const changes=[(rows:Batch[])=>{rows[0].kind="untrusted"},(rows:Batch[])=>{rows[0].raw_json=null},(rows:Batch[])=>{rows[0].raw_json="{"},(rows:Batch[])=>{rows[0].raw_json="[]"},(rows:Batch[])=>{rows[0].row_order=rows[1].row_order},(rows:Batch[])=>{rows[0].row_order="0"},(rows:Batch[])=>{rows[0].row_order="01"},(rows:Batch[])=>{rows[0].row_order="999"}] + for(const mutate of changes){const f=fixture();mutate(f.rows);assert.throws(()=>check(f),refused)} + const extra=fixture();(extra.rows[0] as unknown as Row).trusted=true;assert.throws(()=>check(extra),refused) + for(const invalid of [undefined,null,{},"rows",[null]])assert.throws(()=>validateBandwidthPeerIdentityRows(invalid,fixture().expected),refused) +}) +test("row/per-row/total-byte/component limits reject sentinels and oversized observations before semantic trust",()=>{ + const f=fixture(),valid={kind:"managed-catalog",row_order:"1",raw_json:JSON.stringify({padding:"x".repeat(131180)})} + assert.throws(()=>validateBandwidthPeerIdentityRows(ordinal(Array.from({length:470},()=>({...valid}))),f.expected),refused) + assert.throws(()=>validateBandwidthPeerIdentityRows([{...valid,raw_json:JSON.stringify({padding:"x".repeat(131201)})}],f.expected),refused) + assert.throws(()=>validateBandwidthPeerIdentityRows(ordinal(Array.from({length:49},()=>({...valid}))),f.expected),refused) + for(const [kind,count]of [["managed-catalog",235],["external-catalog",231]] as const)assert.throws(()=>validateBandwidthPeerIdentityRows(ordinal(Array.from({length:count},()=>({kind,row_order:"",raw_json:"{}"}))),f.expected),refused) +}) +test("both least-privilege role observations retain exact membership/ACL/owner/flag checks",()=>{ + for(const kind of ["managed-role","external-role"])for(const mutate of [ + (row:Row)=>{(row.observation as Row).runtimeFlagsSafe=false},(row:Row)=>{(row.observation as Row).dangerousMembership=true},(row:Row)=>{(row.observation as Row).ownerMembership=true},(row:Row)=>{(row.observation as Row).publicSchemaCreate=true},(row:Row)=>{(row.observation as Row).schemaOwner="application_owner"}, + (row:Row)=>{((row.observation as Row).tables as Row[])[0].privilegesExact=false},(row:Row)=>{((row.observation as Row).tables as Row[])[0].ownedByRuntime=true},(row:Row)=>{const tables=(row.observation as Row).tables as Row[];tables[1]={...tables[0]}}, + ]){const f=fixture();change(f,kind,mutate);assert.throws(()=>check(f),refused)} +}) +test("managed and external catalogs retain literal guards, ALWAYS triggers, UUID columns, indexes and signed expression pin",()=>{ + for(const kind of ["managed-catalog","external-catalog"])for(const [section,predicate,mutate]of [ + ["functions",()=>true,(row:Row)=>{row.body="BEGIN RETURN NEW; END;"}], + ["triggers",(row:Row)=>row.internal===false,(row:Row)=>{row.enabled="O"}], + ["columns",(row:Row)=>row.type_name==="uuid",(row:Row)=>{row.type_name="int8"}], + ["indexes",()=>true,(row:Row)=>{row.predicate="false"}], + ["constraints",(row:Row)=>row.type==="f",(row:Row)=>{row.delete_action="c"}], + ["constraints",(row:Row)=>row.type==="c",(row:Row)=>{row.definition="CHECK(true)"}], + ] as Array<[string,(row:Row)=>boolean,(row:Row)=>void]>){const f=fixture();catalogChange(f,kind,section,predicate,mutate);assert.throws(()=>check(f),refused)} +}) +test("two incoming FK identities must match both complete trigger footprints and signed original pin",()=>{ + for(const mutate of [(row:Row)=>{row.constraint_name="different_fk"},(row:Row)=>{row.table_name="bandwidth_node_occupancy"},(row:Row)=>{row.constraint_name="x".repeat(64)},(row:Row)=>{row.extra=true}]){const f=fixture();change(f,"incoming",mutate);assert.throws(()=>check(f),refused)} + const f=fixture();catalogChange(f,"managed-catalog","triggers",row=>String(row.constraint_name).startsWith("bandwidth_external"),row=>{row.constraint_name="wrong_original_foreign_key"});assert.throws(()=>check(f),refused) +}) +test("immutable authority exact installation/key/quiescence/status row is required, independently of valid catalog digests",()=>{ + for(const mutate of [(row:Row)=>{row.installation_id="22222222-2222-4222-8222-222222222222"},(row:Row)=>{row.issuer_key_sha256="f".repeat(64)},(row:Row)=>{row.quiescence_proof="f".repeat(64)},(row:Row)=>{row.status="quarantined"},(row:Row)=>{row.ready=true},(row:Row)=>{delete row.status}]){const f=fixture();change(f,"authority",mutate);assert.throws(()=>check(f),refused)} +}) +test("caller mutation during SQL cannot replace original expected role/root/catalog pin",async()=>{ + const f=fixture(),original={...f.expected};let calls=0 + await assertBandwidthPeerIdentity({async query(_text,values){calls++;assert.deepEqual(values,[original.databaseName,original.runtimeRole,original.ownerRole]);f.expected.databaseName="changed_database";f.expected.runtimeRole="changed_role";f.expected.registryCatalogSha256="f".repeat(64);f.expected.installationId="22222222-2222-4222-8222-222222222222";return{rows:f.rows}}},f.expected,()=>{}) + assert.equal(calls,1) +}) +test("expected getters/symbols/additional fields/malformed root are refused before SQL without invoking accessors",async()=>{ + for(const field of ["installationId","issuerKeySha256","registryCatalogSha256"]){const f=fixture();(f.expected as Row)[field]="invalid";let calls=0;await assert.rejects(assertBandwidthPeerIdentity({async query(){calls++;return{rows:f.rows}}},f.expected,()=>{}),refused);assert.equal(calls,0)} + const f=fixture();let getters=0,calls=0;Object.defineProperty(f.expected,"issuerKeySha256",{enumerable:true,get(){getters++;return"a".repeat(64)}}) + await assert.rejects(assertBandwidthPeerIdentity({async query(){calls++;return{rows:f.rows}}},f.expected,()=>{}),refused);assert.equal(getters,0);assert.equal(calls,0) + const symbol=fixture();Object.defineProperty(symbol.expected,Symbol("proof"),{value:true});assert.throws(()=>check(symbol),refused) +}) +test("expiry/cancellation live boundary rejects late valid snapshots and SQL failure without exposing provider detail",async()=>{ + const f=fixture(),closed=Error("original authority closed");let calls=0,checks=0 + await assert.rejects(assertBandwidthPeerIdentity({async query(){calls++;return{rows:f.rows}}},f.expected,()=>{if(++checks===2)throw closed}),error=>error===closed);assert.equal(calls,1);assert.equal(checks,2) + const events:string[]=[];await assert.rejects(assertBandwidthPeerIdentity({async query(){events.push("SQL");throw Error("private provider endpoint/password")}},f.expected,()=>events.push("live")),e=>refused(e)&&!String(e).includes("password"));assert.deepEqual(events,["live","SQL","live"]) + let before=0;await assert.rejects(assertBandwidthPeerIdentity({async query(){before++;return{rows:f.rows}}},f.expected,()=>{throw closed}),error=>error===closed);assert.equal(before,0) +}) diff --git a/backend/src/services/bandwidth-peer-identity.ts b/backend/src/services/bandwidth-peer-identity.ts new file mode 100644 index 00000000..18acb989 --- /dev/null +++ b/backend/src/services/bandwidth-peer-identity.ts @@ -0,0 +1,91 @@ +/** One bounded MVCC snapshot of the full v2 registry identity. No new lease, + * supplied SQL, readiness assertion, cache, retry or native authority. The same + * pure role/catalog validators and signed catalog digest serve all observers. */ +import { z } from "zod" +import { BANDWIDTH_NODE_REGISTRY_ROLE_SQL, captureBandwidthNodeRegistryRoleExpected, validateBandwidthNodeRegistryRuntimeRoleRows } from "./bandwidth-node-registry-role.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL, validateBandwidthExternalNodeRegistryRuntimeRoleRows } from "./bandwidth-external-node-registry-role.js" +import { BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL, BANDWIDTH_PEER_REGISTRY_INCOMING_SQL, validateBandwidthPeerRegistryCatalogRows } from "./bandwidth-node-registry-catalog.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL } from "./bandwidth-external-node-registry-catalog.js" + +export interface BandwidthPeerIdentitySql { query(text:string,values?:unknown[]):Promise<{rows:Record[]}> } +const hash=z.string().regex(/^[a-f0-9]{64}$/) +const expectedSchema=z.object({databaseName:z.string(),runtimeRole:z.string(),ownerRole:z.string(),installationId:z.string().uuid(),issuerKeySha256:hash,quiescenceProof:hash,registryCatalogSha256:hash}).strict() +export type BandwidthPeerIdentityExpected=z.infer +export class BandwidthPeerIdentityRefused extends Error { + readonly code="BANDWIDTH_PEER_IDENTITY_REFUSED" + constructor(){super("Bandwidth peer registry identity refused")} +} +function fail():never{throw new BandwidthPeerIdentityRefused()} +const plain=(v:unknown):v is Record=>!!v&&typeof v==="object"&&Object.getPrototypeOf(v)===Object.prototype +const fields=["databaseName","runtimeRole","ownerRole","installationId","issuerKeySha256","quiescenceProof","registryCatalogSha256"] as const +/** Original scalar expectations are copied before the first asynchronous SQL; + * caller getters, serializers, symbols and later mutations cannot select pins. */ +function capture(expected:BandwidthPeerIdentityExpected):Readonly{ + if(!plain(expected)||Reflect.ownKeys(expected).length!==fields.length)fail() + const descriptors=Object.getOwnPropertyDescriptors(expected) + if(Object.keys(descriptors).sort().join(",")!==[...fields].sort().join(",")||Object.values(descriptors).some(field=>!("value"in field)||!field.enumerable))fail() + let parsed:BandwidthPeerIdentityExpected + try{parsed=expectedSchema.parse(Object.fromEntries(fields.map(name=>[name,descriptors[name].value])));captureBandwidthNodeRegistryRoleExpected({databaseName:parsed.databaseName,runtimeRole:parsed.runtimeRole,ownerRole:parsed.ownerRole})}catch{return fail()} + return Object.freeze(parsed) +} +type Kind="managed-role"|"external-role"|"managed-catalog"|"external-catalog"|"incoming"|"authority" +const limits:Record={"managed-role":1,"external-role":1,"managed-catalog":234,"external-catalog":230,incoming:2,authority:1} +/** Every nested query is a fixed existing observer statement; CTE evaluation + * shares ONE PostgreSQL command snapshot, including inbound FK identities and + * the immutable authority. Null overflow rows deliberately fail validation. */ +export const BANDWIDTH_PEER_IDENTITY_SQL=`/* bandwidth-peer:identity */ WITH + managed_role AS MATERIALIZED (${BANDWIDTH_NODE_REGISTRY_ROLE_SQL}), + external_role AS MATERIALIZED (${BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL}), + managed_catalog AS MATERIALIZED (${BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL}), + external_catalog AS MATERIALIZED (${BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL}), + incoming AS MATERIALIZED (${BANDWIDTH_PEER_REGISTRY_INCOMING_SQL}), + authority AS MATERIALIZED ( + SELECT installation_id::pg_catalog.text,issuer_key_sha256,quiescence_proof,status + FROM mmapp_control.bandwidth_node_authority WHERE id=1 LIMIT 2), + bundled AS ( + SELECT 'managed-role'::pg_catalog.text AS kind,pg_catalog.to_jsonb(r)::pg_catalog.text AS raw_json FROM managed_role r + UNION ALL SELECT 'external-role',pg_catalog.to_jsonb(r)::pg_catalog.text FROM external_role r + UNION ALL SELECT 'managed-catalog',pg_catalog.to_jsonb(r)::pg_catalog.text FROM managed_catalog r + UNION ALL SELECT 'external-catalog',pg_catalog.to_jsonb(r)::pg_catalog.text FROM external_catalog r + UNION ALL SELECT 'incoming',pg_catalog.to_jsonb(r)::pg_catalog.text FROM incoming r + UNION ALL SELECT 'authority',pg_catalog.to_jsonb(r)::pg_catalog.text FROM authority r), + metered AS ( + SELECT kind,raw_json,pg_catalog.row_number() OVER(ORDER BY kind COLLATE "C",raw_json COLLATE "C") AS row_order, + pg_catalog.count(*) OVER() AS row_count,pg_catalog.sum(pg_catalog.octet_length(raw_json)) OVER() AS total_bytes + FROM bundled) + SELECT kind,row_order::pg_catalog.text AS row_order, + CASE WHEN row_count<=469 AND total_bytes<=6291456 AND pg_catalog.octet_length(raw_json)<=131200 THEN raw_json END AS raw_json + FROM metered ORDER BY metered.row_order LIMIT 470` + +/** Pure counterpart for captured rows. Component validators retain their own + * tighter structural/byte bounds and the original exact signed digest. */ +export function validateBandwidthPeerIdentityRows(rows:unknown,expected:BandwidthPeerIdentityExpected):void{ + const captured=capture(expected) + if(!Array.isArray(rows)||rows.length>469)fail() + const found:Record[]>={"managed-role":[],"external-role":[],"managed-catalog":[],"external-catalog":[],incoming:[],authority:[]} + const ordinals=new Set();let bytes=0 + for(const row of rows){ + if(!plain(row)||Object.keys(row).sort().join(",")!=="kind,raw_json,row_order"||typeof row.kind!=="string"||!Object.hasOwn(found,row.kind)||typeof row.raw_json!=="string"||typeof row.row_order!=="string"||!/^[1-9][0-9]{0,2}$/.test(row.row_order))fail() + const ordinal=Number(row.row_order);if(ordinal>rows.length||ordinals.has(ordinal))fail();ordinals.add(ordinal) + const size=Buffer.byteLength(row.raw_json);bytes+=size;if(size>131200||bytes>6291456)fail() + let value:unknown;try{value=JSON.parse(row.raw_json)}catch{return fail()};if(!plain(value))fail() + const kind=row.kind as Kind;found[kind].push(value);if(found[kind].length>limits[kind])fail() + } + try{ + const role={databaseName:captured.databaseName,runtimeRole:captured.runtimeRole,ownerRole:captured.ownerRole} + validateBandwidthNodeRegistryRuntimeRoleRows(found["managed-role"],role) + validateBandwidthExternalNodeRegistryRuntimeRoleRows(found["external-role"],role) + if(validateBandwidthPeerRegistryCatalogRows(found["managed-catalog"],found["external-catalog"],found.incoming)!==captured.registryCatalogSha256)fail() + if(found.authority.length!==1)fail() + const authority=found.authority[0] + if(Object.keys(authority).sort().join(",")!=="installation_id,issuer_key_sha256,quiescence_proof,status"||authority.installation_id!==captured.installationId||authority.issuer_key_sha256!==captured.issuerKeySha256||authority.quiescence_proof!==captured.quiescenceProof||authority.status!=="active")fail() + }catch{return fail()} +} + +export async function assertBandwidthPeerIdentity(sql:BandwidthPeerIdentitySql,expected:BandwidthPeerIdentityExpected,live:()=>void):Promise{ + const captured=capture(expected) + live();let result:{rows:Record[]} + try{result=await sql.query(BANDWIDTH_PEER_IDENTITY_SQL,[captured.databaseName,captured.runtimeRole,captured.ownerRole])}catch{live();return fail()} + live();validateBandwidthPeerIdentityRows(result?.rows,captured);live() +} + diff --git a/backend/src/services/bandwidth-peer-metadata-source.test.ts b/backend/src/services/bandwidth-peer-metadata-source.test.ts new file mode 100644 index 00000000..0920b592 --- /dev/null +++ b/backend/src/services/bandwidth-peer-metadata-source.test.ts @@ -0,0 +1,123 @@ +/** Actual checker; synthetic application driver boundary only. No DB/native/Q. */ +import assert from "node:assert/strict" +import { test, mock } from "node:test" +import { setImmediate as immediate } from "node:timers/promises" +import { EventEmitter } from "node:events" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import type { assertManagedBandwidthPeerMetadataChecker as AssertChecker } from "./bandwidth-peer-metadata-source.js" + +type Query = { text: string; values: unknown[]; query_timeout: number } +const fresh = (): BandwidthPeerSourceSnapshot => ({ peerRef: { kind: "server", serverId: 23 }, enabled: true, + sourceProof: "a".repeat(64), authorityGeneration: "original-server-owned-generation", + endpoint: { host: "127.0.0.1", port: 8729, useSsl: true, verifySsl: false, username: "private-user", password: "private-password", routingEndpointEpoch: "163cd719-185a-489b-a985-cdc9630c3d0f" } }) +let connects = 0, queries: Query[] = [], releases: boolean[] = [] +let acquire: (() => Promise) | undefined, queryWait: (() => Promise) | undefined +let current = fresh(), override: unknown, releaseThrows = false +const client = Object.assign(new EventEmitter(), { release(destroy = false) { releases.push(destroy); if (releaseThrows) throw Error("raw release error") }, async query(config: Query) { + queries.push(config); await queryWait?.() + if (override !== undefined) return override + const e = current.endpoint, expected = [current.peerRef.kind === "server" ? current.peerRef.serverId : -1, e.host, e.port, e.useSsl, e.verifySsl, e.username, e.password, e.routingEndpointEpoch] + return { command: "SELECT", rowCount: 1, rows: [{ metadata_current: current.enabled && expected.every((value, index) => value === config.values[index]) }] } +} }) +mock.module("../db/index.js", { namedExports: { pool: { async connect() { connects++; await acquire?.(); return client } } } }) +const { createManagedBandwidthPeerMetadataChecker, assertManagedBandwidthPeerMetadataChecker: assertChecker } = await import("./bandwidth-peer-metadata-source.js") +const assertManagedBandwidthPeerMetadataChecker: typeof AssertChecker = assertChecker +const scope = (signal?: AbortSignal) => ({ deadlineAt: Date.now() + 5000, ...(signal ? { signal } : {}) }) +const reset = () => { connects = 0; queries = []; releases = []; current = fresh(); override = undefined; acquire = undefined; queryWait = undefined; releaseThrows = false; client.removeAllListeners() } +const entered = async () => { for (let count = 0; !connects && count < 100; count++) await immediate(); assert.equal(connects, 1) } + +test("managed metadata checker: actual fixed source, synthetic app SQL only", async t => { + await t.test("construction/brand do no I/O; forged wrappers and external input refuse before checkout", async () => { + reset(); const checker = createManagedBandwidthPeerMetadataChecker() + assertManagedBandwidthPeerMetadataChecker(checker); assert.throws(() => assertManagedBandwidthPeerMetadataChecker({ ...checker })) + assert.equal(connects, 0) + await assert.rejects(checker.assertMetadataCurrent({ ...fresh(), peerRef: { kind: "external", enrollmentId: "163cd719-185a-489b-a985-cdc9630c3d0f" } }, scope())) + assert.equal(connects, 0); assert.equal(queries.length, 0) + }) + await t.test("descriptor/proxy malformed originals/scopes cannot execute getters or driver", async () => { + reset(); const checker = createManagedBandwidthPeerMetadataChecker(); let traps = 0 + const getter = Object.defineProperty(fresh(), "endpoint", { enumerable: true, get() { traps++; throw Error("getter") } }) + const proxy = new Proxy(fresh(), { getPrototypeOf() { traps++; throw Error("proxy") } }) + const scopeProxy = new Proxy(scope(), { getOwnPropertyDescriptor() { traps++; throw Error("scope proxy") } }) + await assert.rejects(checker.assertMetadataCurrent(getter, scope())) + await assert.rejects(checker.assertMetadataCurrent(proxy, scope())) + await assert.rejects(checker.assertMetadataCurrent(fresh(), scopeProxy)) + for (const value of [NaN, Infinity, -1]) await assert.rejects(checker.assertMetadataCurrent(fresh(), { deadlineAt: value })) + assert.equal(traps, 0); assert.equal(connects, 0) + }) + await t.test("fixed SQL returns void; private primitives captured before first await, no credential projection", async () => { + reset(); const checker = createManagedBandwidthPeerMetadataChecker(), expected = fresh() + const pending = checker.assertMetadataCurrent(expected, scope()) + expected.endpoint.password = "later-caller-mutation"; expected.endpoint.host = "changed.invalid" + assert.equal(await pending, undefined) + assert.deepEqual(releases, [false]); assert.equal(queries.length, 1) + const query = queries[0] + assert.match(query.text, /^\/\* bandwidth-managed:metadata-current \*\/ SELECT EXISTS/) + assert.match(query.text, /username=\$6::text AND password=\$7::text/) + assert.match(query.text, /\) AS metadata_current$/) + assert(!/RETURNING|md5|digest|INSERT|UPDATE|DELETE|COMMIT|pg_advisory/i.test(query.text)) + assert.equal(query.values[6], "private-password"); assert.equal(query.values[1], "127.0.0.1") + assert(query.query_timeout > 0 && query.query_timeout <= 5000) + }) + await t.test("all current endpoint/credential/epoch/id/enabled drifts refuse, one fixed boolean statement", async () => { + const mutations: Array<(value: BandwidthPeerSourceSnapshot) => void> = [ + value => { value.peerRef = { kind: "server", serverId: 24 } }, value => { value.enabled = false }, + value => { value.endpoint.host = "other.invalid" }, value => { value.endpoint.port++ }, + value => { value.endpoint.useSsl = false }, value => { value.endpoint.verifySsl = true }, + value => { value.endpoint.username += "x" }, value => { value.endpoint.password += "x" }, + value => { value.endpoint.routingEndpointEpoch = "263cd719-185a-489b-a985-cdc9630c3d0f" }, + ] + for (const mutate of mutations) { + reset(); mutate(current) + await assert.rejects(createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope())) + assert.equal(queries.length, 1); assert.deepEqual(releases, [true]) + } + }) + await t.test("rows-only, extra credential fields, truthy string and malformed driver ACK fail closed", async () => { + for (const result of [ { rows: [{ metadata_current: true }] }, { command: "SELECT", rowCount: 1, rows: [{ metadata_current: true, password: "never-adopt" }] }, + { command: "SELECT", rowCount: 1, rows: [{ metadata_current: "true" }] }, { command: "UPDATE", rowCount: 1, rows: [{ metadata_current: true }] }, + { command: "SELECT", rowCount: 0, rows: [{ metadata_current: true }] } ]) { + reset(); override = result + await assert.rejects(createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope())) + assert.deepEqual(releases, [true]) + } + }) + await t.test("original abort destroys pending query; late true result cannot publish", async () => { + reset(); let finish!: () => void; queryWait = () => new Promise(resolve => { finish = resolve }) + const controller = new AbortController(), pending = createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope(controller.signal)) + await entered(); while (!queries.length) await immediate() + controller.abort(); await assert.rejects(pending); assert.deepEqual(releases, [true]) + finish(); await immediate(); assert.deepEqual(releases, [true]) + }) + await t.test("canceled queued checkout destroys its late lease and does not dispatch SQL", async () => { + reset(); let finish!: () => void; acquire = () => new Promise(resolve => { finish = resolve }) + const controller = new AbortController(), pending = createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope(controller.signal)) + await entered(); controller.abort(); await assert.rejects(pending) + finish(); await immediate(); assert.deepEqual(releases, [true]); assert.equal(queries.length, 0) + }) + await t.test("original deadline expires pending SQL without renewal", async () => { + reset(); let finish!: () => void; queryWait = () => new Promise(resolve => { finish = resolve }) + const pending = createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), { deadlineAt: Date.now() + 30 }) + await assert.rejects(pending); assert.deepEqual(releases, [true]); finish(); await immediate() + }) + await t.test("socket error aborts pending SQL and late errors after destruction remain absorbed", async () => { + reset(); let finish!: () => void; queryWait = () => new Promise(resolve => { finish = resolve }) + const pending = createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope()) + await entered(); while (!queries.length) await immediate() + client.emit("error", Error("owned socket failure")); await assert.rejects(pending) + assert.deepEqual(releases, [true]); assert.doesNotThrow(() => client.emit("error", Error("late socket failure"))) + finish(); await immediate() + }) + await t.test("healthy/destroyed/late-checkout release exceptions are sanitized without a retry", async () => { + reset(); releaseThrows = true + await assert.rejects(createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope()), { code: "BANDWIDTH_PEER_METADATA_SOURCE_REFUSED" }) + assert.deepEqual(releases, [false]); assert.doesNotThrow(() => client.emit("error", Error("late uncertain release"))) + reset(); releaseThrows = true; override = { command: "SELECT", rowCount: 1, rows: [{ metadata_current: false }] } + await assert.rejects(createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope()), { code: "BANDWIDTH_PEER_METADATA_SOURCE_REFUSED" }) + assert.deepEqual(releases, [true]); assert.doesNotThrow(() => client.emit("error", Error("late destroy error"))) + reset(); releaseThrows = true; let finish!: () => void; acquire = () => new Promise(resolve => { finish = resolve }) + const controller = new AbortController(), pending = createManagedBandwidthPeerMetadataChecker().assertMetadataCurrent(fresh(), scope(controller.signal)) + await entered(); controller.abort(); await assert.rejects(pending); finish(); await immediate() + assert.deepEqual(releases, [true]); assert.doesNotThrow(() => client.emit("error", Error("late checkout error"))) + }) +}) diff --git a/backend/src/services/bandwidth-peer-metadata-source.ts b/backend/src/services/bandwidth-peer-metadata-source.ts new file mode 100644 index 00000000..094101f4 --- /dev/null +++ b/backend/src/services/bandwidth-peer-metadata-source.ts @@ -0,0 +1,107 @@ +/** Fixed managed metadata comparison. This is NOT actor, Q or native authority. + * The trusted caller must check its genuine current original grant first. + * Passwords are private bound predicates, never result projections or hashes. */ +import { types } from "node:util" +import { performance } from "node:perf_hooks" +import type { PoolClient, QueryConfig } from "pg" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import type { BandwidthNodeObservationScope } from "./bandwidth-node-observation.js" + +export class BandwidthPeerMetadataSourceRefused extends Error { + readonly code = "BANDWIDTH_PEER_METADATA_SOURCE_REFUSED" + constructor() { super("Bandwidth peer metadata refused") } +} +const refuse = (): never => { throw new BandwidthPeerMetadataSourceRefused() } +const checkers = new WeakSet() +function data(raw: unknown, names: readonly string[]): Record { + if (!raw || typeof raw !== "object" || types.isProxy(raw) || Object.getPrototypeOf(raw) !== Object.prototype) return refuse() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== names.length || names.some(name => !fields[name]) + || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) return refuse() + return Object.fromEntries(names.map(name => [name, fields[name].value])) +} +const bounded = (raw: unknown, max: number): raw is string => typeof raw === "string" && Buffer.byteLength(raw) <= max +const SQL = `/* bandwidth-managed:metadata-current */ SELECT EXISTS ( + SELECT 1 FROM public.servers + WHERE id=$1::bigint AND enabled IS TRUE AND host=$2::text AND port=$3::integer + AND use_ssl=$4::boolean AND verify_ssl=$5::boolean + AND username=$6::text AND password=$7::text + AND routing_endpoint_epoch=$8::uuid +) AS metadata_current` + +export function assertManagedBandwidthPeerMetadataChecker(value: unknown): asserts value is ReturnType { + if (!value || typeof value !== "object" || !checkers.has(value)) refuse() +} +export function createManagedBandwidthPeerMetadataChecker() { + const checker = Object.freeze({ + async assertMetadataCurrent(expected: Readonly, rawScope: BandwidthNodeObservationScope): Promise { + const wall = Date.now(), mono = performance.now() + let client: PoolClient | undefined, active = true, released = false, success = false + let releaseFailed = false, socketFailed = false + let timer: ReturnType | undefined, signal: AbortSignal | undefined, cancel: (() => void) | undefined + const onError = () => { socketFailed = true; if (active) cancel?.() } + const release = (destroy: boolean) => { + if (!client || released) return + released = true + try { + client.release(destroy) + if (!destroy) client.removeListener("error", onError) + } catch { releaseFailed = true } + // A destroyed/uncertain client retains its listener as a late-error + // absorber. Never retry an uncertain release or expose its raw error. + } + try { + const original = data(expected, ["peerRef", "enabled", "sourceProof", "authorityGeneration", "endpoint"]) + // External source has no persistent current metadata provider. Never + // invent a server alias, consult a supplied snapshot again or fall back. + const peer = data(original.peerRef, ["kind", "serverId"]) + const endpoint = data(original.endpoint, ["host", "port", "useSsl", "verifySsl", "username", "password", "routingEndpointEpoch"]) + if (peer.kind !== "server" || !Number.isSafeInteger(peer.serverId) || Number(peer.serverId) < 1 || original.enabled !== true + || !bounded(original.sourceProof, 64) || !/^[a-f0-9]{64}$/.test(original.sourceProof) + || !bounded(original.authorityGeneration, 200) || !original.authorityGeneration + || !bounded(endpoint.host, 1000) || !endpoint.host || /[\s/\\\u0000-\u001f\u007f]/.test(endpoint.host) + || !Number.isSafeInteger(endpoint.port) || Number(endpoint.port) < 1 || Number(endpoint.port) > 65535 + || typeof endpoint.useSsl !== "boolean" || typeof endpoint.verifySsl !== "boolean" + || !bounded(endpoint.username, 1000) || !bounded(endpoint.password, 65536) + || !bounded(endpoint.routingEndpointEpoch, 36) || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(endpoint.routingEndpointEpoch)) refuse() + if (!rawScope || typeof rawScope !== "object" || types.isProxy(rawScope) || Object.getPrototypeOf(rawScope) !== Object.prototype) refuse() + const fields = data(rawScope, Object.hasOwn(rawScope, "signal") ? ["deadlineAt", "signal"] : ["deadlineAt"]) + if (!Number.isSafeInteger(fields.deadlineAt) || fields.signal !== undefined && (types.isProxy(fields.signal) || !(fields.signal instanceof AbortSignal))) refuse() + signal = fields.signal as AbortSignal | undefined + const deadline = Math.min(Number(fields.deadlineAt), wall + 5000), end = mono + Math.max(0, deadline - wall) + const aborted = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "aborted")!.get! + const live = () => { const now = Date.now(); if (!active || now < wall || now >= deadline || performance.now() >= end || signal && Reflect.apply(aborted, signal, [])) refuse() } + const values = [peer.serverId, endpoint.host, endpoint.port, endpoint.useSsl, endpoint.verifySsl, endpoint.username, endpoint.password, endpoint.routingEndpointEpoch] + let rejectStopped!: (error: BandwidthPeerMetadataSourceRefused) => void + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }); void stopped.catch(() => {}) + cancel = () => { active = false; release(true); rejectStopped(new BandwidthPeerMetadataSourceRefused()) } + live(); timer = setTimeout(cancel, Math.max(1, Math.ceil(end - performance.now()))) + if (signal) Reflect.apply(EventTarget.prototype.addEventListener, signal, ["abort", cancel, { once: true }]) + await Promise.race([stopped, (async () => { + live(); const { pool } = await import("../db/index.js"); live() + const acquired = await pool.connect() + if (!active) { + acquired.on("error", () => {}) + try { acquired.release(true) } catch { /* Original call already refused. */ } + return refuse() + } + client = acquired; client.on("error", onError); live() + const config: QueryConfig & { query_timeout: number } = { text: SQL, values, query_timeout: Math.max(1, Math.ceil(Math.min(deadline - Date.now(), end - performance.now()))) } + const result = await client.query(config) + live() + if (result.command !== "SELECT" || result.rowCount !== 1 || result.rows.length !== 1) refuse() + const row = data(result.rows[0], ["metadata_current"]) + if (row.metadata_current !== true) refuse() + live(); success = true + })()]); live() + } catch { throw new BandwidthPeerMetadataSourceRefused() } + finally { + active = false; clearTimeout(timer) + if (signal && cancel) Reflect.apply(EventTarget.prototype.removeEventListener, signal, ["abort", cancel]) + release(!success) + if (releaseFailed || socketFailed) throw new BandwidthPeerMetadataSourceRefused() + } + }, + }) + checkers.add(checker); return checker +} diff --git a/backend/src/services/bandwidth-peer-metadata.test.ts b/backend/src/services/bandwidth-peer-metadata.test.ts new file mode 100644 index 00000000..b6ff23a3 --- /dev/null +++ b/backend/src/services/bandwidth-peer-metadata.test.ts @@ -0,0 +1,152 @@ +/** Genuine localhost authenticated license observation + private reader token. + * Metadata checker is an explicit trusted fixture service, not request JSON. + * No remote RouterOS, key files, PostgreSQL or writer/Q authority. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { once } from "node:events" +import { isDeepStrictEqual } from "node:util" +import { after, test } from "node:test" +import { createBandwidthPeerObservationReader, BandwidthPeerObservationError, type BandwidthPeerSourceSnapshot, type BandwidthPeerTrustedSource, type BandwidthPeerPublicMetadata, type BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" + +const db=await import("../db/index.js"),originalQuery=db.pool.query,originalConnect=db.pool.connect +let databaseCalls=0 +db.pool.query=(()=>{databaseCalls++;throw Error("metadata unit must not query DB")}) as typeof db.pool.query +db.pool.connect=(()=>{databaseCalls++;throw Error("metadata unit must not borrow DB")}) as typeof db.pool.connect +after(async()=>{db.pool.query=originalQuery;db.pool.connect=originalConnect;assert.equal(databaseCalls,0);await db.closePool()}) +const scope=(ms=5000,signal?:AbortSignal)=>({deadlineAt:Date.now()+ms,signal}) +const deferred=()=>{let resolve!:()=>void;const promise=new Promise(done=>{resolve=done});return {promise,resolve}} +const turn=()=>new Promise(done=>setImmediate(done)) +const refused=(code?:BandwidthPeerObservationError["code"])=>(error:unknown)=>error instanceof BandwidthPeerObservationError&&(!code||error.code===code) + +function publicNotOrdinary(value:BandwidthPeerPublicMetadata){ + // @ts-expect-error PUBLIC wrapper cannot satisfy an ordinary evidence input. + const ordinary:BandwidthPeerObservationEvidence=value + void ordinary + return value +} +async function fixture(checker=true,managed=false,originalScope=scope()){ + const username="OWNED_METADATA_USER",password="OWNED_METADATA_PRIVATE_PASSWORD",auth="Basic "+Buffer.from(username+":"+password).toString("base64") + let gets=0,reads=0,checks=0,forbidReads=false + const server=createServer((request,response)=>{gets++;assert.equal(request.url,"/rest/system/license");assert.equal(request.method,"GET");assert.equal(request.headers.authorization,auth);response.writeHead(200,{"content-type":"application/json"});response.end(JSON.stringify({"system-id":"OWNED-METADATA-SYSTEM","software-id":"OWNED-METADATA-SOFTWARE"}))}) + server.listen(0,"127.0.0.1");await once(server,"listening");const address=server.address();assert(address&&typeof address!=="string") + let current:BandwidthPeerSourceSnapshot={peerRef:managed?{kind:"server",serverId:83}:{kind:"external",enrollmentId:randomUUID()},enabled:true,sourceProof:"a".repeat(64),authorityGeneration:"owned-metadata-original",endpoint:{host:"127.0.0.1",port:address.port,useSsl:false,verifySsl:true,username,password,routingEndpointEpoch:randomUUID()}} + const original=structuredClone(current),received:Readonly[]=[],controls:{check?:()=>Promise}={} + const source:BandwidthPeerTrustedSource={async read(){reads++;if(forbidReads)throw Error("metadata credential fallback attempted");return current}} + if(checker)source.assertMetadataCurrent=async(expected,input)=>{ + checks++;received.push(expected);assert(Object.isFrozen(expected));assert(Object.isFrozen(expected.endpoint));assert(Object.isFrozen(expected.peerRef));assert.equal(expected.endpoint.password,password) + assert(input.deadlineAt<=originalScope.deadlineAt);assert(input.signal instanceof AbortSignal) + await controls.check?.() + if(!isDeepStrictEqual(current,expected))throw new BandwidthPeerObservationError("SOURCE_CHANGED") + } + const reader=createBandwidthPeerObservationReader(source) + let token:Awaited>,ordinary:BandwidthPeerObservationEvidence + try{token=await reader.observe(current.peerRef,originalScope);ordinary=await reader.inspect(token,scope())} + catch(error){reader.close();server.closeAllConnections();await new Promise(done=>server.close(()=>done()));throw error} + const originalReads=reads,originalGets=gets;forbidReads=true + return {reader,source,token,ordinary,original,received,controls,get current(){return current},set current(value){current=value},get checks(){return checks},get reads(){return reads},get gets(){return gets},originalReads,originalGets,password,async close(){reader.close();server.closeAllConnections();await new Promise(done=>server.close(()=>done()));assert.equal(reads,originalReads);assert.equal(gets,originalGets)}} +} + +test("genuine managed/external metadata preserves original evidence, shares private frozen snapshot and never projects credentials",async()=>{ + for(const managed of [false,true]){ + const f=await fixture(true,managed) + try{ + const first=publicNotOrdinary(await f.reader.inspectMetadata(f.token,scope())),second=await f.reader.inspectMetadata(f.token,scope()) + assert.equal(first.format,"mmapp-bandwidth-peer-public-metadata-v1");assert.deepEqual(first.evidence,f.ordinary);assert.deepEqual(second.evidence,f.ordinary) + assert.equal(f.checks,2);assert.equal(f.received[0],f.received[1]);assert.equal(first.evidence.claims,second.evidence.claims) + for(const part of [first,first.evidence,first.evidence.peerRef,first.evidence.claims,first.evidence.claims[0]])assert(Object.isFrozen(part)) + const output=JSON.stringify(first);for(const forbidden of [f.password,f.original.endpoint.username,f.original.endpoint.host,"password","endpoint","keyCurrent","credentialsVerified","currentLicense","writerBarrier"])assert.equal(output.includes(forbidden),false) + assert.equal(first.evidence.physicalUniquenessVerified,false);assert.equal(first.evidence.cloneResistanceVerified,false) + }finally{await f.close()} + } +}) + +test("absent checker refuses without credential fallback; method is captured once and later replacement cannot grant or revoke",async()=>{ + const missing=await fixture(false) + try{ + missing.source.assertMetadataCurrent=async()=>{assert.fail("late checker must not be adopted")} + await assert.rejects(missing.reader.inspectMetadata(missing.token,scope()),refused("SOURCE_UNAVAILABLE"));assert.equal(missing.checks,0) + }finally{await missing.close()} + const f=await fixture() + try{ + f.source.assertMetadataCurrent=async()=>{assert.fail("replacement checker must not execute")} + assert.deepEqual((await f.reader.inspectMetadata(f.token,scope())).evidence,f.ordinary);assert.equal(f.checks,1) + f.current={...f.current,enabled:false};await assert.rejects(f.reader.inspectMetadata(f.token,scope()),refused("SOURCE_CHANGED"));assert.equal(f.checks,2) + }finally{await f.close()} +}) + +test("copies, ordinary/public evidence, proxies and foreign reader tokens never reach metadata checker",async()=>{ + const f=await fixture(),foreign=await fixture() + try{ + const wrapper=await f.reader.inspectMetadata(f.token,scope()),before=f.checks + for(const token of [{...f.token},structuredClone(f.token),f.ordinary,wrapper,new Proxy(f.token,{get(){assert.fail("token trap")}}),null])await assert.rejects(f.reader.inspectMetadata(token,scope()),refused("NOT_GENUINE")) + await assert.rejects(foreign.reader.inspectMetadata(f.token,scope()),refused("NOT_GENUINE"));assert.equal(f.checks,before);assert.equal(foreign.checks,0) + }finally{await foreign.close();await f.close()} +}) + +test("all current endpoint/credential/epoch/enabled/source drift must be rejected by checker, no refreshed secret snapshot",async()=>{ + const mutations:Array<(value:BandwidthPeerSourceSnapshot)=>void>=[value=>{value.enabled=false},value=>{value.sourceProof="b".repeat(64)},value=>{value.authorityGeneration="new"},value=>{value.endpoint.host="192.0.2.2"},value=>{value.endpoint.port++},value=>{value.endpoint.useSsl=true},value=>{value.endpoint.verifySsl=false},value=>{value.endpoint.username="new"},value=>{value.endpoint.password="new"},value=>{value.endpoint.routingEndpointEpoch=randomUUID()},value=>{value.peerRef={kind:"server",serverId:84}}] + const f=await fixture() + try{for(const mutate of mutations){const current=structuredClone(f.original);mutate(current);f.current=current;await assert.rejects(f.reader.inspectMetadata(f.token,scope()),refused("SOURCE_CHANGED"))}assert.equal(f.checks,mutations.length)} + finally{await f.close()} +}) + +test("malformed checker/scope descriptors and proxies never execute caller getters; hostile native signal methods ignored",async()=>{ + let called=0;const trap=()=>{called++;throw Error("caller code")} + const source={async read(){assert.fail("constructor IO")}} + assert.throws(()=>createBandwidthPeerObservationReader(Object.defineProperty(source,"assertMetadataCurrent",{get:trap})),refused("INVALID_INPUT")) + assert.throws(()=>createBandwidthPeerObservationReader(new Proxy(source,{get:trap})),refused("INVALID_INPUT")) + const f=await fixture() + try{ + for(const input of [Object.defineProperty(scope(),"deadlineAt",{get:trap}),{...scope(),checker:trap},{...scope(),toJSON:trap},new Proxy(scope(),{getPrototypeOf:trap})])await assert.rejects(f.reader.inspectMetadata(f.token,input),refused("INVALID_INPUT")) + assert.equal(f.checks,0);assert.equal(called,0) + const signal=new AbortController().signal + for(const field of ["aborted","addEventListener","removeEventListener"])Object.defineProperty(signal,field,{get:trap}) + await f.reader.inspectMetadata(f.token,scope(5000,signal));assert.equal(called,0);assert.equal(f.checks,1) + }finally{await f.close()} +}) + +test("checker failure reveals no private message and cannot fall back to source.read/native",async()=>{ + const f=await fixture() + try{ + f.controls.check=async()=>{throw Error(f.password)} + const error=await f.reader.inspectMetadata(f.token,scope()).then(()=>assert.fail("expected refusal"),error=>error as Error) + assert(error instanceof BandwidthPeerObservationError);assert.equal(error.code,"SOURCE_UNAVAILABLE");assert.equal(error.message.includes(f.password),false);assert.equal(f.checks,1) + }finally{await f.close()} +}) + +test("blocked checker abort/close/deadline/original signal cancellation refuse promptly; late success cannot publish",async()=>{ + for(const mode of ["abort","close","deadline","original-abort"]){ + const originalController=new AbortController(),f=await fixture(true,false,scope(5000,originalController.signal)),gate=deferred(),entered=deferred(),controller=new AbortController() + try{ + f.controls.check=async()=>{entered.resolve();await gate.promise} + const input=scope(mode==="deadline"?40:5000,controller.signal),pending=f.reader.inspectMetadata(f.token,input);void pending.catch(()=>{}) + await entered.promise + if(mode==="abort")controller.abort();if(mode==="original-abort")originalController.abort();if(mode==="close")f.reader.close() + input.deadlineAt=Date.now()+50000 + await assert.rejects(pending,refused());gate.resolve();await turn();await turn();assert.equal(f.checks,1) + }finally{gate.resolve();await f.close()} + } +}) + +test("expired original observation or original aborted signal cannot be renewed by a fresh metadata scope",async()=>{ + const f=await fixture(),now=Date.now + try{ + Date.now=()=>f.ordinary.expiresAt+1 + await assert.rejects(f.reader.inspectMetadata(f.token,{deadlineAt:f.ordinary.expiresAt+50000}),refused("CANCELLED"));assert.equal(f.checks,0) + }finally{Date.now=now;await f.close()} +}) + +test("wall rollback before and during checker is refused; original evidence timestamps never adopted from later scope",async()=>{ + for(const mode of ["before","during"]){ + const f=await fixture(),now=Date.now,gate=deferred(),entered=deferred() + try{ + if(mode==="before"){Date.now=()=>f.ordinary.observedAt-1000;await assert.rejects(f.reader.inspectMetadata(f.token,scope()),refused("CANCELLED"));assert.equal(f.checks,0)} + else{ + f.controls.check=async()=>{entered.resolve();await gate.promise} + const pending=f.reader.inspectMetadata(f.token,scope());void pending.catch(()=>{});await entered.promise + Date.now=()=>f.ordinary.observedAt-1000;gate.resolve();await assert.rejects(pending,refused("CANCELLED")) + } + }finally{Date.now=now;gate.resolve();await f.close()} + } +}) diff --git a/backend/src/services/bandwidth-peer-network-observation.test.ts b/backend/src/services/bandwidth-peer-network-observation.test.ts new file mode 100644 index 00000000..ba33d465 --- /dev/null +++ b/backend/src/services/bandwidth-peer-network-observation.test.ts @@ -0,0 +1,140 @@ +/** Localhost HTTP acceptance for the fixed GET reader. No RouterOS, mapping, + * PostgreSQL, DNS/NAT authority or production activation is asserted. */ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { once } from "node:events" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mock, test } from "node:test" +import { createBandwidthPeerNetworkObservationReader, BandwidthPeerNetworkObservationError } from "./bandwidth-peer-network-observation.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js" + +const scope = (duration = 4000) => ({ deadlineAt: Date.now() + duration }) +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const refused = (code?: BandwidthPeerNetworkObservationError["code"]) => (error: unknown) => error instanceof BandwidthPeerNetworkObservationError && (!code || error.code === code) +const managed = { kind: "server" as const, serverId: 71 }, external = { kind: "external" as const, enrollmentId: randomUUID() } + +test("genuine peer network observation fixed transport and provenance", async t => { + const { pool, closePool } = await import("../db/index.js") + const query = pool.query, connect = pool.connect; let databaseCalls = 0 + pool.query = (() => { databaseCalls++; throw Error("No application DB allowed") }) as typeof pool.query + pool.connect = (() => { databaseCalls++; throw Error("No application DB allowed") }) as typeof pool.connect + async function fixture(peer: BandwidthPeerRef = external) { + const requests: { method?: string; path?: string; authenticated: boolean }[] = [] + let responseHook: ((resource: string) => unknown) | undefined, onRequest: ((resource: string) => void) | undefined, hold = "", denied = false, sourceHook: (() => Promise | void) | undefined, licenseSystem = "SYS-NET" + const user = "NETWORK_TEST_USER", password = "NETWORK_TEST_PASSWORD", auth = "Basic " + Buffer.from(user + ":" + password).toString("base64") + const router = createServer((req, res) => { + requests.push({ method: req.method, path: req.url, authenticated: req.headers.authorization === auth }) + const resource = req.url!.slice("/rest".length); onRequest?.(resource) + res.writeHead(200, { "content-type": "application/json" }) + if (hold === resource) { res.flushHeaders(); return } + const defaults: Record = { + "/system/license": { "system-id": licenseSystem, "software-id": "SOFT-NET" }, + "/ip/address": [{ ".id": "*a", address: "192.0.2.9/24", interface: "ether1", "unknown-sensitive-value": password }], + "/ipv6/address": [{ ".id": "*b", address: "fe80::1/64", interface: "ether2", disabled: "false" }], + "/interface": [{ ".id": "*1", name: "ether1", running: "true" }, { ".id": "*2", name: "ether2", disabled: "false" }], + "/ip/vrf": [{ name: "first", interfaces: "ether1" }, { name: "main", interfaces: "all" }], + "/routing/table": [{ name: "main", fib: "true" }], + "/ip/firewall/nat": [{ ".id": "*n", chain: "dstnat", action: "dst-nat", "dst-address": "198.51.100.1", "to-addresses": "192.0.2.9", "new-unrecognized-matcher": "unknown" }], + } + res.end(JSON.stringify(responseHook ? responseHook(resource) : defaults[resource] ?? [])) + }) + router.listen(0, "127.0.0.1"); await once(router, "listening") + const address = router.address(); assert(address && typeof address !== "string") + let current: BandwidthPeerSourceSnapshot = { peerRef: peer, enabled: true, sourceProof: "a".repeat(64), authorityGeneration: "original-network-actor", + endpoint: { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: user, password, routingEndpointEpoch: randomUUID() } } + const source = { async read() { await sourceHook?.(); if (denied) throw Error(password); return current } } + const reader = createBandwidthPeerNetworkObservationReader(source) + return { reader, source, requests, password, current: () => current, replace: (next: BandwidthPeerSourceSnapshot) => { current = next }, + hook: (value?: (resource: string) => unknown) => { responseHook = value }, onRequest: (value: (resource: string) => void) => { onRequest = value }, + hold: (resource: string) => { hold = resource }, revoke: () => { denied = true }, sourceHook: (value: () => Promise | void) => { sourceHook = value }, licenseSystem: (value: string) => { licenseSystem = value }, + async dispose() { reader.close(); router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) } } + } + try { + await t.test("managed/external actual authenticated license and fixed complete GET catalogues; no mapping or effective semantics assertion", async () => { + for (const peer of [managed, external]) { const f = await fixture(peer); try { + const token = await f.reader.observe(peer, scope()), evidence = await f.reader.inspect(token, scope()) + assert.deepEqual(evidence.identity.peerRef, peer); assert.equal(evidence.identity.claims[0].value, "SYS-NET") + assert.equal(evidence.catalogues.length, 12); assert.equal(f.requests.length, 14) + assert(f.requests.every(req => req.method === "GET" && req.authenticated && req.path?.startsWith("/rest/") && !req.path.includes("?"))) + const addresses = evidence.catalogues.find(item => item.resource === "/ip/address")!, nat = evidence.catalogues.find(item => item.resource === "/ip/firewall/nat")! + assert.equal(Object.hasOwn(addresses.rows[0], "disabled"), false, "omitted default remains absent") + assert.deepEqual(addresses.omittedFields[0], ["unknown-sensitive-value"]) + assert.equal(nat.rows[0]["to-addresses"], "192.0.2.9"); assert.deepEqual(nat.omittedFields[0], ["new-unrecognized-matcher"]) + assert.deepEqual(evidence.catalogues.find(item => item.resource === "/ip/vrf")!.rows.map(row => row.name), ["first", "main"]) + assert(evidence.catalogues.every(item => item.allRowsCaptured && item.semanticsVerified === false)) + assert.equal(evidence.mappingVerified, false); assert.equal(evidence.dnsResolverFenceVerified, false); assert.equal(evidence.natPathCoverageVerified, false) + assert(!JSON.stringify(evidence).includes(f.password)); assert(!JSON.stringify(token).includes("192.0.2.9")); assert(Object.isFrozen(evidence.catalogues[0].rows[0])) + assert.deepEqual(Object.keys(f.reader).sort(), ["close", "inspect", "observe"]) + } finally { await f.dispose() } } + }) + await t.test("actual license changes behind the same endpoint during catalogues reject the entire observation", async () => { + const f = await fixture(); try { + f.onRequest(resource => { if (resource === "/interface/list/member") f.licenseSystem("OTHER-PHYSICAL-IDENTITY") }) + await assert.rejects(f.reader.observe(external, scope()), refused("SOURCE_CHANGED")) + assert.equal(f.requests.length, 14); assert.equal(f.requests.filter(req => req.path === "/rest/system/license").length, 2) + } finally { await f.dispose() } + }) + await t.test("copies, evidence, foreign token, bad scopes and hostile peer input never gain provenance", async () => { + const f = await fixture(), other = createBandwidthPeerNetworkObservationReader(f.source); try { + const token = await f.reader.observe(external, scope()), evidence = await f.reader.inspect(token, scope()), before = f.requests.length + for (const fake of [{ ...token }, JSON.parse(JSON.stringify(token)), evidence, null]) await assert.rejects(f.reader.inspect(fake, scope()), refused("NOT_GENUINE")) + await assert.rejects(other.inspect(token, scope()), refused("NOT_GENUINE")) + let getters = 0 + await assert.rejects(f.reader.observe({ kind: "external", get enrollmentId() { getters++; return external.enrollmentId } }, scope())) + await assert.rejects(f.reader.observe(external, { get deadlineAt() { getters++; return Date.now() + 1000 } })) + assert.equal(getters, 0); assert.equal(f.requests.length, before) + } finally { other.close(); await f.dispose() } + }) + await t.test("source/epoch/credentials/enable drift during a GET prevents token publication and further resources", async () => { + for (const fault of ["epoch", "credential", "enabled", "proof"] as const) { const f = await fixture(); try { + f.onRequest(resource => { if (resource !== "/ip/address") return + if (fault === "epoch") f.current().endpoint.routingEndpointEpoch = randomUUID() + if (fault === "credential") f.current().endpoint.password = "changed" + if (fault === "enabled") f.current().enabled = false + if (fault === "proof") f.current().sourceProof = "b".repeat(64) + }) + await assert.rejects(f.reader.observe(external, scope()), refused()); assert.equal(f.requests.length, 2) + } finally { await f.dispose() } } + }) + await t.test("original revoke before native credentials causes no request and private errors remain redacted", async () => { + const f = await fixture(); try { + f.revoke(); const error = await f.reader.observe(external, scope()).catch(error => error as Error) + assert(error instanceof BandwidthPeerNetworkObservationError); assert(!error.message.includes(f.password)); assert.equal(f.requests.length, 0) + } finally { await f.dispose() } + }) + await t.test("malformed, oversized or excessive catalogue responses cannot produce truncated success", async () => { + for (const response of [{ rows: [] }, [{ address: null, nested: { no: "unbounded recursive metadata" } }], Array.from({ length: 2049 }, () => ({})), [{ address: "x".repeat(150000) }]]) { + const f = await fixture(); try { + f.hook(resource => resource === "/system/license" ? { "system-id": "SYS-NET" } : response) + await assert.rejects(f.reader.observe(external, scope()), refused()); assert.equal(f.requests.length, 2) + } finally { await f.dispose() } + } + }) + await t.test("abort, stalled source and wall rollback keep original bounded lifetime with no late GET", async () => { + const f = await fixture(); try { + const entered = deferred(); f.hold("/ip/address"); f.onRequest(resource => { if (resource === "/ip/address") entered.resolve() }) + const controller = new AbortController(), result = assert.rejects(f.reader.observe(external, { ...scope(), signal: controller.signal }), refused("CANCELLED")) + await entered.promise; controller.abort(); await result; assert.equal(f.requests.length, 2) + } finally { await f.dispose() } + const stalled = await fixture(), entered = deferred(), unblock = deferred(); try { + stalled.sourceHook(async () => { entered.resolve(); await unblock.promise }) + const pending = assert.rejects(stalled.reader.observe(external, scope(120)), refused("CANCELLED")); await entered.promise + const wall = Date.now(); mock.method(Date, "now", () => wall - 60000) + try { await pending } finally { mock.restoreAll() } + unblock.resolve(); await delay(10); assert.equal(stalled.requests.length, 0) + } finally { unblock.resolve(); await stalled.dispose() } + }) + await t.test("original expiry/cancellation and closed reader cannot be renewed by inspection", async () => { + const f = await fixture(); try { + const controller = new AbortController(), token = await f.reader.observe(external, { ...scope(), signal: controller.signal }) + controller.abort(); await assert.rejects(f.reader.inspect(token, scope()), refused("CANCELLED")) + const second = await f.reader.observe(external, scope(500)); await delay(510) + await assert.rejects(f.reader.inspect(second, scope()), refused("CANCELLED")); f.reader.close() + await assert.rejects(f.reader.inspect(second, scope()), refused("CLOSED")); await assert.rejects(f.reader.observe(external, scope()), refused("CLOSED")) + } finally { await f.dispose() } + }) + assert.equal(databaseCalls, 0) + } finally { pool.query = query; pool.connect = connect; await closePool() } +}) diff --git a/backend/src/services/bandwidth-peer-network-observation.ts b/backend/src/services/bandwidth-peer-network-observation.ts new file mode 100644 index 00000000..827ac7d7 --- /dev/null +++ b/backend/src/services/bandwidth-peer-network-observation.ts @@ -0,0 +1,174 @@ +/** Genuine read-only management observations, never proof of the actual wire + * destination, effective NAT path, DNS resolution or clone resistance. Native + * catalogues are untrusted data; only this reader's opaque token has provenance. */ +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { createBandwidthPeerObservationReader, type BandwidthPeerTrustedSource, type BandwidthPeerSourceSnapshot, type BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import { parseBandwidthPeerRef, bandwidthPeerRefKey, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthNodeObservationScope } from "./bandwidth-node-observation.js" + +const paths = ["/ip/address", "/ipv6/address", "/interface", "/ip/vrf", "/routing/table", "/routing/route", "/routing/rule", "/ip/firewall/nat", "/ip/firewall/mangle", "/ipv6/firewall/mangle", "/interface/list", "/interface/list/member"] as const +type Resource = typeof paths[number] +type Fact = string | number | boolean | null | readonly Fact[] +type Row = Readonly> +const common = [".id", "disabled", "invalid", "dynamic"] +const policy = ["chain", "action", "src-address", "dst-address", "src-address-list", "dst-address-list", "src-port", "dst-port", "protocol", "in-interface", "out-interface", "in-interface-list", "out-interface-list", "routing-mark", "connection-mark", "connection-state", "connection-nat-state", "connection-type", "connection-bytes", "connection-rate", "connection-limit", "connection-pcc", "per-connection-classifier", "src-address-type", "dst-address-type", "src-mac-address", "to-addresses", "to-ports", "jump-target", "new-routing-mark", "new-connection-mark", "new-packet-mark", "passthrough", "hotspot", "ipsec-policy", "packet-mark", "packet-size", "fragment", "tcp-flags", "time", "random", "nth", "psd", "limit", "dst-limit", "layer7-protocol", "content", "tls-host", "log", "log-prefix"] +const fields: Record> = { + "/ip/address": new Set([...common, "address", "network", "interface", "actual-interface"]), + "/ipv6/address": new Set([...common, "address", "interface", "actual-interface", "advertise", "from-pool", "no-dad"]), + "/interface": new Set([...common, "name", "type", "running", "slave", "mtu", "actual-mtu", "l2mtu", "mac-address", "default-name"]), + "/ip/vrf": new Set([...common, "name", "interfaces", "routing-table"]), + "/routing/table": new Set([...common, "name", "fib"]), + "/routing/route": new Set([...common, "dst-address", "gateway", "immediate-gw", "routing-table", "vrf-interface", "active", "unreachable", "blackhole", "distance", "scope", "target-scope", "local-address", "pref-src", "belongs-to", "routing-mark"]), + "/routing/rule": new Set([...common, "src-address", "dst-address", "interface", "routing-mark", "action", "table", "min-prefix", "suppress-prefixlength"]), + "/ip/firewall/nat": new Set([...common, ...policy]), + "/ip/firewall/mangle": new Set([...common, ...policy]), + "/ipv6/firewall/mangle": new Set([...common, ...policy]), + "/interface/list": new Set([...common, "name", "include", "exclude"]), + "/interface/list/member": new Set([...common, "list", "interface"]), +} +declare const networkBrand: unique symbol +export type BandwidthPeerNetworkObservation = Readonly<{ kind: "routeros-peer-network-observation"; [networkBrand]: true }> +export interface BandwidthPeerNetworkCatalogue { + readonly resource: Resource + readonly observedAt: number + readonly rows: readonly Row[] + /** Original response order is retained, including ordered VRF/NAT rules. */ + readonly omittedFields: readonly (readonly string[])[] + readonly allRowsCaptured: true + readonly semanticsVerified: false +} +export interface BandwidthPeerNetworkEvidence { + readonly version: 1 + readonly identity: Readonly + readonly observedAt: number + readonly expiresAt: number + readonly catalogues: readonly BandwidthPeerNetworkCatalogue[] + readonly mappingVerified: false + readonly dnsResolverFenceVerified: false + readonly natPathCoverageVerified: false +} +export class BandwidthPeerNetworkObservationError extends Error { + constructor(readonly code: "INVALID_INPUT" | "CANCELLED" | "CLOSED" | "SOURCE_CHANGED" | "SOURCE_UNAVAILABLE" | "INVALID_NATIVE_RESPONSE" | "NOT_GENUINE") { super("Bandwidth peer network observation: " + code) } +} +const fail = (code: BandwidthPeerNetworkObservationError["code"]): never => { throw new BandwidthPeerNetworkObservationError(code) } +const plain = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +function own(value: unknown): Record { + if (!plain(value)) return fail("INVALID_INPUT") + const descriptors = Object.getOwnPropertyDescriptors(value) + if (Reflect.ownKeys(value).length !== Object.keys(descriptors).length || Object.values(descriptors).some(item => !("value" in item) || !item.enumerable)) fail("INVALID_INPUT") + return descriptors +} +function sourceSnapshot(raw: unknown, peer: BandwidthPeerRef): Readonly { + const source = own(raw), endpoint = own(source.endpoint?.value) + if (Object.keys(source).sort().join() !== "authorityGeneration,enabled,endpoint,peerRef,sourceProof" || Object.keys(endpoint).sort().join() !== "host,password,port,routingEndpointEpoch,useSsl,username,verifySsl") fail("SOURCE_UNAVAILABLE") + const value = Object.fromEntries(Object.entries(source).map(([key, item]) => [key, item.value])) as unknown as BandwidthPeerSourceSnapshot + const copied = Object.fromEntries(Object.entries(endpoint).map(([key, item]) => [key, item.value])) as unknown as BandwidthPeerSourceSnapshot["endpoint"] + const ref = parseBandwidthPeerRef(value.peerRef) + if (bandwidthPeerRefKey(ref) !== bandwidthPeerRefKey(peer) || value.enabled !== true || typeof value.sourceProof !== "string" || !/^[a-f0-9]{64}$/.test(value.sourceProof) + || typeof value.authorityGeneration !== "string" || !value.authorityGeneration.length || Buffer.byteLength(value.authorityGeneration) > 200 + || typeof copied.host !== "string" || !copied.host.length || Buffer.byteLength(copied.host) > 1000 || /[\s/\\\u0000-\u001f\u007f]/.test(copied.host) + || !Number.isSafeInteger(copied.port) || copied.port < 1 || copied.port > 65535 || typeof copied.useSsl !== "boolean" || typeof copied.verifySsl !== "boolean" + || typeof copied.username !== "string" || Buffer.byteLength(copied.username) > 1000 || typeof copied.password !== "string" || Buffer.byteLength(copied.password) > 65536 + || typeof copied.routingEndpointEpoch !== "string" || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(copied.routingEndpointEpoch)) fail("SOURCE_UNAVAILABLE") + return Object.freeze({ peerRef: ref, enabled: true, sourceProof: value.sourceProof, authorityGeneration: value.authorityGeneration, endpoint: Object.freeze(copied) }) +} +function catalogue(resource: Resource, raw: unknown, observedAt: number, budget: { rows: number; bytes: number }): BandwidthPeerNetworkCatalogue { + if (!Array.isArray(raw) || raw.length > 2048 || Reflect.ownKeys(raw).length !== raw.length + 1) return fail("INVALID_NATIVE_RESPONSE") + const result: Row[] = [], omitted: string[][] = [] + const fact = (value: unknown, depth = 0): Fact => { + if (depth > 2) return fail("INVALID_NATIVE_RESPONSE") + if (value === null || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return value + if (typeof value === "string" && Buffer.byteLength(value) <= 8192) return value + if (Array.isArray(value) && value.length <= 64 && Reflect.ownKeys(value).length === value.length + 1) return Object.freeze(value.map(item => fact(item, depth + 1))) + return fail("INVALID_NATIVE_RESPONSE") + } + for (const row of raw) { + if (!plain(row) || Reflect.ownKeys(row).length > 128) fail("INVALID_NATIVE_RESPONSE") + const descriptors = own(row), projected: Record = {}, missing: string[] = [] + if (++budget.rows > 4096) fail("INVALID_NATIVE_RESPONSE") + for (const [key, item] of Object.entries(descriptors)) { + if (Buffer.byteLength(key) > 128 || key === "__proto__") fail("INVALID_NATIVE_RESPONSE") + // Validate all values before projecting; malformed unknown properties do + // not become a trustworthy complete catalogue by being discarded. + const value = fact(item.value) + budget.bytes += Buffer.byteLength(key) + Buffer.byteLength(JSON.stringify(value)) + if (budget.bytes > 1048576) fail("INVALID_NATIVE_RESPONSE") + if (fields[resource].has(key)) Object.defineProperty(projected, key, { value, enumerable: true }) + else missing.push(key) + } + result.push(Object.freeze(projected)); omitted.push(Object.freeze(missing.sort()) as string[]) + } + return Object.freeze({ resource, observedAt, rows: Object.freeze(result), omittedFields: Object.freeze(omitted), allRowsCaptured: true, semanticsVerified: false }) +} +type Origin = { source: Readonly; evidence: Readonly; monotonicEnd: number; signal?: AbortSignal } +export function createBandwidthPeerNetworkObservationReader(trustedSource: BandwidthPeerTrustedSource) { + if (!trustedSource || typeof trustedSource.read !== "function") fail("INVALID_INPUT") + const read = trustedSource.read.bind(trustedSource), licenses = createBandwidthPeerObservationReader(Object.freeze({ read })), observations = new WeakMap(), windows = new Set() + let closed = false + const active = () => { if (closed) fail("CLOSED") } + function window(raw: BandwidthNodeObservationScope, origin?: Origin) { + active(); const descriptors = own(raw) + if (Object.keys(descriptors).some(key => !["deadlineAt", "signal"].includes(key))) fail("INVALID_INPUT") + const deadline = descriptors.deadlineAt?.value as number, external = descriptors.signal?.value as AbortSignal | undefined + if (!Number.isSafeInteger(deadline) || external !== undefined && !(external instanceof AbortSignal)) fail("INVALID_INPUT") + const wall = Date.now(), mono = performance.now(), duration = Math.min(5000, deadline - wall, origin ? origin.evidence.expiresAt - wall : Infinity, origin ? origin.monotonicEnd - mono : Infinity) + if (duration <= 0 || external?.aborted || origin?.signal?.aborted) fail("CANCELLED") + const deadlineAt = Math.min(deadline, wall + Math.floor(duration), origin?.evidence.expiresAt ?? Infinity), monotonicEnd = mono + duration, controller = new AbortController() + const abort = () => controller.abort(), timer = setTimeout(abort, Math.max(1, Math.ceil(duration))) + windows.add(controller); external?.addEventListener("abort", abort, { once: true }); origin?.signal?.addEventListener("abort", abort, { once: true }) + const live = () => { active(); if (controller.signal.aborted || external?.aborted || origin?.signal?.aborted || Date.now() >= deadlineAt || performance.now() >= monotonicEnd) fail("CANCELLED") } + const scope = Object.freeze({ deadlineAt, signal: controller.signal }) + const wait = async (work: () => Promise): Promise => { + live(); let reject!: () => void + const cancelled = new Promise((_resolve, denied) => { reject = () => denied(new BandwidthPeerNetworkObservationError("CANCELLED")); controller.signal.addEventListener("abort", reject, { once: true }) }) + try { const value = await Promise.race([Promise.resolve().then(() => { live(); return work() }), cancelled]); live(); return value } + finally { controller.signal.removeEventListener("abort", reject) } + } + return { scope, live, wait, monotonicEnd, external, dispose() { clearTimeout(timer); external?.removeEventListener("abort", abort); origin?.signal?.removeEventListener("abort", abort); windows.delete(controller); controller.abort() } } + } + return Object.freeze({ + async observe(rawPeer: BandwidthPeerRef, scope: BandwidthNodeObservationScope): Promise { + const peer = parseBandwidthPeerRef(rawPeer), owned = window(scope) + try { + const before = sourceSnapshot(await owned.wait(() => read(peer, owned.scope)), peer) + const fresh = async () => { if (!isDeepStrictEqual(sourceSnapshot(await owned.wait(() => read(peer, owned.scope)), peer), before)) fail("SOURCE_CHANGED") } + const identityToken = await owned.wait(() => licenses.observe(peer, owned.scope)); await fresh() + const identity = await owned.wait(() => licenses.inspect(identityToken, owned.scope)); await fresh() + const { MikrotikClient } = await owned.wait(() => import("./mikrotik.js")); await fresh() + const client = new MikrotikClient(before.endpoint), catalogues: BandwidthPeerNetworkCatalogue[] = [], budget = { rows: 0, bytes: 0 } + // Fixed full responses, no .proplist/filter/limit or pagination that + // silently turns omitted rules into absence. Catalogue wire bound <=1.5MiB. + for (const resource of paths) { + await fresh() + const rows = await owned.wait(() => client.get(resource, Math.max(1, owned.scope.deadlineAt - Date.now()), owned.scope.signal, 131072, fresh)) + const observedAt = Date.now() + await fresh(); catalogues.push(catalogue(resource, rows, observedAt, budget)); owned.live() + } + // A fixed management endpoint can begin serving another router without + // its stored credentials/epoch changing. Bracket catalogues with a + // second genuine license GET, under the ORIGINAL operation budget. + const afterToken = await owned.wait(() => licenses.observe(peer, owned.scope)); await fresh() + const afterIdentity = await owned.wait(() => licenses.inspect(afterToken, owned.scope)); await fresh() + const { observedAt: _beforeAt, expiresAt: _beforeExpiry, ...beforeSubject } = identity + const { observedAt: _afterAt, expiresAt: _afterExpiry, ...afterSubject } = afterIdentity + if (!isDeepStrictEqual(beforeSubject, afterSubject)) fail("SOURCE_CHANGED") + const evidence = Object.freeze({ version: 1 as const, identity, observedAt: Date.now(), expiresAt: Math.min(owned.scope.deadlineAt, identity.expiresAt), catalogues: Object.freeze(catalogues), mappingVerified: false as const, dnsResolverFenceVerified: false as const, natPathCoverageVerified: false as const }) + const token = Object.freeze({ kind: "routeros-peer-network-observation" as const }) as BandwidthPeerNetworkObservation + observations.set(token, { source: before, evidence, monotonicEnd: owned.monotonicEnd, signal: owned.external }); return token + } catch (error) { if (error instanceof BandwidthPeerNetworkObservationError) throw error; throw new BandwidthPeerNetworkObservationError("SOURCE_UNAVAILABLE") } + finally { owned.dispose() } + }, + async inspect(token: unknown, scope: BandwidthNodeObservationScope): Promise> { + active(); const origin = token && typeof token === "object" ? observations.get(token) : undefined + if (!origin) return fail("NOT_GENUINE") + const owned = window(scope, origin) + try { + if (!isDeepStrictEqual(sourceSnapshot(await owned.wait(() => read(origin.source.peerRef, owned.scope)), origin.source.peerRef), origin.source)) fail("SOURCE_CHANGED") + owned.live(); return origin.evidence + } catch (error) { if (error instanceof BandwidthPeerNetworkObservationError) throw error; throw new BandwidthPeerNetworkObservationError("SOURCE_UNAVAILABLE") } + finally { owned.dispose() } + }, + close() { closed = true; licenses.close(); for (const controller of windows) controller.abort() }, + }) +} diff --git a/backend/src/services/bandwidth-peer-observation.test.ts b/backend/src/services/bandwidth-peer-observation.test.ts new file mode 100644 index 00000000..143148fd --- /dev/null +++ b/backend/src/services/bandwidth-peer-observation.test.ts @@ -0,0 +1,113 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { once } from "node:events" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { createBandwidthPeerObservationReader, BandwidthPeerObservationError, type BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import { parseBandwidthPeerRef, bandwidthPeerRefKey, BandwidthPeerReferenceError, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" + +const scope = (ms = 4000) => ({ deadlineAt: Date.now() + ms }) +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const denied = (code?: BandwidthPeerObservationError["code"]) => (error: unknown) => error instanceof BandwidthPeerObservationError && (!code || error.code === code) +const cases: string[] = [], requests: { method?: string; path?: string; authenticated: boolean }[] = [] +const external = Object.freeze({ kind: "external" as const, enrollmentId: randomUUID() }), managed = Object.freeze({ kind: "server" as const, serverId: 71 }) +const username = "PEER_TEST_ONLY_USER", password = "PEER_TEST_ONLY_PASSWORD", auth = "Basic " + Buffer.from(username + ":" + password).toString("base64") +let payload: unknown = { "system-id": "SYS-PEER", "software-id": "SOFT-PEER" }, status = 200, hold = false +let onRequest: (() => void) | undefined, onClosed: (() => void) | undefined +const router = createServer((req, res) => { + requests.push({ method: req.method, path: req.url, authenticated: req.headers.authorization === auth }) + res.once("close", () => { if (!res.writableEnded) onClosed?.() }); onRequest?.() + res.writeHead(status, { "content-type": "application/json", ...(status === 302 ? { location: "/never-follow" } : {}) }) + if (hold) res.flushHeaders(); else res.end(JSON.stringify(payload)) +}) +router.listen(0, "127.0.0.1"); await once(router, "listening") +const address = router.address(); assert(address && typeof address !== "string") +const initial: BandwidthPeerSourceSnapshot = { peerRef: external, enabled: true, sourceProof: "a".repeat(64), authorityGeneration: "owned-peer-grant", + endpoint: { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username, password, routingEndpointEpoch: randomUUID() } } +let current = structuredClone(initial), reads = 0, readHook: (() => Promise | void) | undefined +const readers: ReturnType[] = [] +const make = () => { + const source = { async read() { reads++; await readHook?.(); return current } } + const reader = createBandwidthPeerObservationReader(source); readers.push(reader); return { reader, source } +} +const reset = (peerRef: BandwidthPeerRef = external) => { current = { ...structuredClone(initial), peerRef }; payload = { "system-id": "SYS-PEER", "software-id": "SOFT-PEER" }; status = 200; hold = false; readHook = undefined; onRequest = undefined; onClosed = undefined } +const { pool, closePool } = await import("../db/index.js") +const originalQuery = pool.query, originalConnect = pool.connect +let databaseCalls = 0 +pool.query = (() => { databaseCalls++; throw Error("Peer fixture cannot query application DB") }) as typeof pool.query +pool.connect = (() => { databaseCalls++; throw Error("Peer fixture cannot borrow application pool") }) as typeof pool.connect +try { + const { reader } = make(); assert.equal(reads, 0); assert.equal(requests.length, 0) + let evaluated = 0 + const hostile = { kind: "external", get enrollmentId() { evaluated++; return external.enrollmentId } } + for (const invalid of [hostile, null, [], { ...external, serverId: 1 }, { kind: "server", serverId: 0 }, { kind: "server", serverId: Number.MAX_SAFE_INTEGER + 1 }, { kind: "external", enrollmentId: external.enrollmentId.toUpperCase() }, Object.create(external)]) { + assert.throws(() => parseBandwidthPeerRef(invalid), BandwidthPeerReferenceError) + await assert.rejects(reader.observe(invalid as BandwidthPeerRef, scope())) + } + assert.equal(evaluated, 0); assert.equal(reads, 0); assert.equal(requests.length, 0) + assert.notEqual(bandwidthPeerRefKey(managed), bandwidthPeerRefKey(external)) + cases.push("tagged managed/external references are distinct; hostile getter, inherited, mixed, unsafe and noncanonical refs refuse before source or transport") + + const token = await reader.observe(external, scope()), evidence = await reader.inspect(token, scope()) + assert.equal(evidence.version, 2); assert.deepEqual(evidence.peerRef, external); assert.equal(Object.hasOwn(evidence, "serverId"), false) + assert.deepEqual(evidence.claims, [{ kind: "system-id", value: "SYS-PEER" }, { kind: "software-id", value: "SOFT-PEER" }]) + assert.equal(evidence.physicalUniquenessVerified, false); assert.equal(evidence.cloneResistanceVerified, false) + assert.equal(requests.length, 1); assert(!JSON.stringify(evidence).includes(password)); assert(!JSON.stringify(evidence).includes(username)) + reset(managed); const managedToken = await reader.observe(managed, scope()), managedEvidence = await reader.inspect(managedToken, scope()) + assert.deepEqual(managedEvidence.claims, evidence.claims); assert.deepEqual(managedEvidence.peerRef, managed) + cases.push("real authenticated fixed license GET for external and managed refs preserves equal typed claims without inventing an application ID, canonical node, wire proof or physical uniqueness") + + const beforeFake = reads + for (const fake of [{ ...token }, JSON.parse(JSON.stringify(token)), evidence, { kind: "routeros-license-observation" }]) await assert.rejects(reader.inspect(fake, scope()), denied("NOT_GENUINE")) + await assert.rejects(make().reader.inspect(token, scope()), denied("NOT_GENUINE")) + assert.equal(reads, beforeFake); assert(Object.isFrozen(evidence.peerRef)); assert(Object.isFrozen(evidence.claims[0])) + cases.push("copied/evidence/v1-looking and foreign-reader tokens never acquire provenance or perform source reads; exported metadata frozen and credentials absent") + + reset(); const externalToken = await reader.observe(external, scope()), countBeforeClient = requests.length + await assert.rejects(reader.strictClient(externalToken, scope()), denied("INVALID_INPUT")); assert.equal(requests.length, countBeforeClient) + reset(managed); const sourceToken = await reader.observe(managed, scope()), client = await reader.strictClient(sourceToken, scope()) + await assert.rejects(client.post("/tool/bandwidth-test", { address: "192.0.2.9", duration: "3s" })); assert.equal(requests.length, countBeforeClient + 1) + cases.push("external destination identity does not issue source execution authority; genuine managed source client requires strict native permit before any POST") + + for (const change of [() => { current.endpoint.password = "CHANGED" }, () => { current.endpoint.routingEndpointEpoch = randomUUID() }, () => { current.peerRef = managed }, () => { current.sourceProof = "b".repeat(64) }, () => { current.enabled = false }]) { + reset(); onRequest = change; await assert.rejects(reader.observe(external, scope()), denied()) + } + reset(); const prior = requests.length, baseReads = reads + readHook = () => { if (reads === baseReads + 2) current.endpoint.password = "CHANGED_BEFORE_SEND" } + await assert.rejects(reader.observe(external, scope()), denied("SOURCE_CHANGED")); assert.equal(requests.length, prior) + reset(); readHook = () => { throw Error("PRIVATE_SOURCE_DENIED") } + const sourceError = await reader.observe(external, scope()).then(() => assert.fail("Expected denied source"), error => error as Error) + assert(!sourceError.message.includes("PRIVATE_SOURCE_DENIED")); assert.equal(requests.length, prior) + cases.push("credential/epoch/ref/proof/enabled drift during HTTP prevents publication; pre-send drift and revoked source make zero requests and never reflect private errors") + + for (const invalid of [[], [{ "system-id": "A" }, { "system-id": "B" }], {}, { "system-id": null, "software-id": "VALID" }, { "software-id": " A" }, { "system-id": "x".repeat(20000) }]) { + reset(); payload = invalid; await assert.rejects(reader.observe(external, scope()), denied()) + } + for (const badStatus of [302, 401, 500]) { reset(); status = badStatus; payload = { message: password }; const before: number = requests.length; const error = await reader.observe(external, scope()).then(() => assert.fail(), error => error as Error); assert(!error.message.includes(password)); assert.equal(requests.length, before + 1) } + reset(); payload = [{ "system-id": "SAME", "software-id": "SAME" }] + const equal = await reader.inspect(await reader.observe(external, scope()), scope()); assert.deepEqual(equal.claims.map(claim => claim.kind), ["system-id", "software-id"]) + cases.push("typed claim kinds survive equal values; invalid/ambiguous/oversized responses and auth/errors/redirects refuse without a fallback identity, reflected credentials or redirect follow") + + reset(); const pending = deferred(), started = deferred(), closed = deferred(); hold = true + onRequest = started.resolve; onClosed = closed.resolve + const controller = new AbortController(), cancelled = assert.rejects(reader.observe(external, { deadlineAt: Date.now() + 1000, signal: controller.signal }), denied("CANCELLED")) + await Promise.race([started.promise, delay(2000).then(() => assert.fail("Native request did not start"))]); controller.abort(); await cancelled + await Promise.race([closed.promise, delay(2000).then(() => assert.fail("Cancelled socket remained open"))]) + reset(); const preStall = requests.length; readHook = () => pending.promise + await assert.rejects(reader.observe(external, scope(30)), denied("CANCELLED")); pending.resolve(); await delay(30); assert.equal(requests.length, preStall) + cases.push("real HTTP cancellation closes owned socket; stalled source expiry cannot dispatch a late GET") + + reset(); const expiry = await reader.observe(external, scope(80)), expiryReads = reads + await delay(100); await assert.rejects(reader.inspect(expiry, scope()), denied("CANCELLED")); assert.equal(reads, expiryReads) + const abort = new AbortController(), inherited = await reader.observe(external, { deadlineAt: Date.now() + 4000, signal: abort.signal }); abort.abort() + await assert.rejects(reader.inspect(inherited, scope()), denied("CANCELLED")) + reset(); const closing = make().reader; closing.close(); await assert.rejects(closing.observe(external, scope()), denied("CLOSED")); await assert.rejects(closing.inspect(token, scope()), denied("CLOSED")) + cases.push("original observation expiry/abort cannot be renewed by a fresh scope; closed readers never adopt old tokens") + + assert.equal(databaseCalls, 0); assert(requests.every(req => req.method === "GET" && req.path === "/rest/system/license" && req.authenticated)) + console.log("Bandwidth peer native observation:", JSON.stringify({ cases: cases.length, checks: cases, actualLocalhostGets: requests.length, defaultDatabaseCalls: databaseCalls, actualPeerKinds: ["server", "external"], canonicalNodesCreated: 0, wirePeerMappingVerified: false, productionWired: false, realChrCalls: 0 })) +} finally { + for (const reader of readers) reader.close() + pool.query = originalQuery; pool.connect = originalConnect + router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())); await closePool() +} diff --git a/backend/src/services/bandwidth-peer-observation.ts b/backend/src/services/bandwidth-peer-observation.ts new file mode 100644 index 00000000..bfebfc2b --- /dev/null +++ b/backend/src/services/bandwidth-peer-observation.ts @@ -0,0 +1,207 @@ +/** Genuine RouterOS license observation for a managed OR external enrollment. + * A fixed server-owned source supplies the management endpoint. The bandwidth + * wire target and its mapping to this identity require a separate peer proof. */ +import { isDeepStrictEqual, types } from "node:util" +import { performance } from "node:perf_hooks" +import { parseBandwidthPeerRef, bandwidthPeerRefKey, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthNodeEndpoint, BandwidthNodeObservationScope, BandwidthNodeLicenseClaim } from "./bandwidth-node-observation.js" + +export interface BandwidthPeerSourceSnapshot { + peerRef: BandwidthPeerRef; enabled: boolean; sourceProof: string; authorityGeneration: string; endpoint: BandwidthNodeEndpoint +} +export interface BandwidthPeerTrustedSource { + /** Must verify the original actor/operator grant BEFORE resolving secrets. */ + read(peerRef: BandwidthPeerRef, scope: Readonly): Promise + /** Optional trusted constructor dependency, captured once as an own method. + * Compare the PRIVATE original snapshot to all current endpoint, enabled, + * epoch and credential fields inside the service/SQL; return no projection. + * This is not a current RouterOS license, Q, key or write-authority check. */ + assertMetadataCurrent?(expected:Readonly,scope:Readonly):Promise +} +declare const peerObservationBrand: unique symbol +export interface BandwidthPeerObservation { readonly kind: "routeros-peer-license-observation"; readonly [peerObservationBrand]: true } +export interface BandwidthPeerObservationEvidence { + readonly version: 2; readonly peerRef: BandwidthPeerRef; readonly sourceProof: string; readonly authorityGeneration: string + readonly routingEndpointEpoch: string; readonly observedAt: number; readonly expiresAt: number + readonly claims: readonly BandwidthNodeLicenseClaim[] + readonly physicalUniquenessVerified: false; readonly cloneResistanceVerified: false +} +/** Public metadata prerequisite, deliberately not ordinary observation evidence. */ +export interface BandwidthPeerPublicMetadata { + readonly format:"mmapp-bandwidth-peer-public-metadata-v1" + readonly evidence:Readonly +} +export class BandwidthPeerObservationError extends Error { + constructor(readonly code: "INVALID_INPUT" | "CANCELLED" | "SOURCE_CHANGED" | "SOURCE_UNAVAILABLE" | "INVALID_LICENSE_RESPONSE" | "NOT_GENUINE" | "CLOSED") { super("Bandwidth peer observation: " + code) } +} +function fail(code: BandwidthPeerObservationError["code"]): never { throw new BandwidthPeerObservationError(code) } +const plain = (raw: unknown): raw is Record => !!raw && typeof raw === "object" && Object.getPrototypeOf(raw) === Object.prototype +function fields(raw: unknown, names: readonly string[]): asserts raw is Record { + if (!plain(raw)) fail("INVALID_INPUT") + const descriptors = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== names.length || Object.keys(descriptors).sort().join(",") !== [...names].sort().join(",") + || Object.values(descriptors).some(field => !("value" in field) || !field.enumerable)) fail("INVALID_INPUT") +} +const text = (raw: unknown, max: number): raw is string => typeof raw === "string" && raw.length > 0 && Buffer.byteLength(raw) <= max && raw.trim() === raw && !/[\u0000-\u001f\u007f]/.test(raw) +function snapshot(raw: unknown, peer: BandwidthPeerRef): Readonly { + fields(raw, ["peerRef", "enabled", "sourceProof", "authorityGeneration", "endpoint"]) + fields(raw.endpoint, ["host", "port", "useSsl", "verifySsl", "username", "password", "routingEndpointEpoch"]) + const endpoint = raw.endpoint, peerRef = parseBandwidthPeerRef(raw.peerRef) + if (bandwidthPeerRefKey(peerRef) !== bandwidthPeerRefKey(peer) || raw.enabled !== true + || typeof raw.sourceProof !== "string" || !/^[a-f0-9]{64}$/.test(raw.sourceProof) || !text(raw.authorityGeneration, 200) + || !text(endpoint.host, 1000) || /[\s/\\]/.test(endpoint.host) || !Number.isSafeInteger(endpoint.port) || Number(endpoint.port) < 1 || Number(endpoint.port) > 65535 + || typeof endpoint.useSsl !== "boolean" || typeof endpoint.verifySsl !== "boolean" + || typeof endpoint.username !== "string" || Buffer.byteLength(endpoint.username) > 1000 + || typeof endpoint.password !== "string" || Buffer.byteLength(endpoint.password) > 65536 + || typeof endpoint.routingEndpointEpoch !== "string" || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(endpoint.routingEndpointEpoch)) fail("SOURCE_UNAVAILABLE") + return Object.freeze({ peerRef, enabled: true, sourceProof: raw.sourceProof, authorityGeneration: raw.authorityGeneration, + endpoint: Object.freeze({ host: endpoint.host, port: Number(endpoint.port), useSsl: endpoint.useSsl, verifySsl: endpoint.verifySsl, + username: endpoint.username, password: endpoint.password, routingEndpointEpoch: endpoint.routingEndpointEpoch }) }) +} +function license(raw: unknown): readonly BandwidthNodeLicenseClaim[] { + if (Array.isArray(raw)) { + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== 2 || raw.length !== 1 || !fields["0"] || !("value" in fields["0"]) || !fields["0"].enumerable) fail("INVALID_LICENSE_RESPONSE") + } + const row = Array.isArray(raw) ? raw.length === 1 ? raw[0] : undefined : raw + if (!plain(row) || Reflect.ownKeys(row).length > 64) fail("INVALID_LICENSE_RESPONSE") + const descriptors = Object.getOwnPropertyDescriptors(row) + if (Reflect.ownKeys(row).length !== Object.keys(descriptors).length || Object.values(descriptors).some(field => !("value" in field) || !field.enumerable)) fail("INVALID_LICENSE_RESPONSE") + const claims: BandwidthNodeLicenseClaim[] = [] + for (const kind of ["system-id", "software-id"] as const) { + if (!Object.hasOwn(row, kind)) continue + const value = descriptors[kind].value + if (!text(value, 256)) fail("INVALID_LICENSE_RESPONSE") + claims.push(Object.freeze({ kind, value })) + } + if (!claims.length) fail("INVALID_LICENSE_RESPONSE") + return Object.freeze(claims) +} +type Origin = { source: Readonly; claims: readonly BandwidthNodeLicenseClaim[]; observedAt: number; expiresAt: number; monotonicEnd: number; signal?: AbortSignal } +export function createBandwidthPeerObservationReader(trustedSource: BandwidthPeerTrustedSource) { + if (!trustedSource || types.isProxy(trustedSource) || typeof trustedSource.read !== "function") fail("INVALID_INPUT") + const metadataDescriptor=Object.getOwnPropertyDescriptor(trustedSource,"assertMetadataCurrent") + if(metadataDescriptor&&(!("value"in metadataDescriptor)||metadataDescriptor.value!==undefined&&typeof metadataDescriptor.value!=="function"))fail("INVALID_INPUT") + const assertMetadataCurrent=metadataDescriptor?.value?.bind(trustedSource) as BandwidthPeerTrustedSource["assertMetadataCurrent"] + const read = trustedSource.read.bind(trustedSource), observations = new WeakMap(), windows = new Set() + let closed = false + const active = () => { if (closed) fail("CLOSED") } + function window(raw: BandwidthNodeObservationScope, origin?: Origin) { + active() + if (!plain(raw) || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(field => !("value" in field) || !field.enumerable) + || Object.keys(raw).some(key => !["deadlineAt", "signal"].includes(key)) || !Number.isSafeInteger(raw.deadlineAt) + || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) fail("INVALID_INPUT") + const external = raw.signal + const duration = Math.min(5000, raw.deadlineAt - Date.now(), origin ? origin.expiresAt - Date.now() : Infinity, origin ? origin.monotonicEnd - performance.now() : Infinity) + if (duration <= 0 || external?.aborted || origin?.signal?.aborted) fail("CANCELLED") + const deadlineAt = Math.min(raw.deadlineAt, Date.now() + Math.floor(duration), origin?.expiresAt ?? Infinity) + const monotonicEnd = performance.now() + duration, controller = new AbortController() + windows.add(controller) + const abort = () => controller.abort(), timer = setTimeout(abort, Math.max(1, Math.ceil(duration))) + external?.addEventListener("abort", abort, { once: true }); origin?.signal?.addEventListener("abort", abort, { once: true }) + const live = () => { active(); if (controller.signal.aborted || external?.aborted || origin?.signal?.aborted || Date.now() >= deadlineAt || performance.now() >= monotonicEnd) fail("CANCELLED") } + const scope = Object.freeze({ deadlineAt, signal: controller.signal }) + const wait = async (work: () => Promise): Promise => { + live() + let stop!: () => void + const cancelled = new Promise((_resolve, reject) => { stop = () => reject(new BandwidthPeerObservationError("CANCELLED")); controller.signal.addEventListener("abort", stop, { once: true }) }) + try { const result = await Promise.race([Promise.resolve().then(() => { live(); return work() }), cancelled]); live(); return result } + finally { controller.signal.removeEventListener("abort", stop) } + } + return { live, wait, scope, external, monotonicEnd, dispose() { clearTimeout(timer); external?.removeEventListener("abort", abort); origin?.signal?.removeEventListener("abort", abort); windows.delete(controller); controller.abort() } } + } + return Object.freeze({ + async observe(rawPeer: BandwidthPeerRef, scope: BandwidthNodeObservationScope): Promise { + const peer = parseBandwidthPeerRef(rawPeer), owned = window(scope) + try { + const before = snapshot(await owned.wait(() => read(peer, owned.scope)), peer) + const fence = async () => { if (!isDeepStrictEqual(snapshot(await owned.wait(() => read(peer, owned.scope)), peer), before)) fail("SOURCE_CHANGED") } + const { MikrotikClient } = await owned.wait(() => import("./mikrotik.js")) + const client = new MikrotikClient(before.endpoint) + const raw = await owned.wait(() => client.get("/system/license", Math.max(1, owned.scope.deadlineAt - Date.now()), owned.scope.signal, 16384, fence)) + const observedAt = Date.now(), claims = license(raw) + await fence(); owned.live() + const token = Object.freeze({ kind: "routeros-peer-license-observation" as const }) as BandwidthPeerObservation + observations.set(token, { source: before, claims, observedAt, expiresAt: owned.scope.deadlineAt, monotonicEnd: owned.monotonicEnd, signal: owned.external }) + return token + } catch (error) { if (error instanceof BandwidthPeerObservationError) throw error; throw new BandwidthPeerObservationError("SOURCE_UNAVAILABLE") } + finally { owned.dispose() } + }, + async inspect(token: unknown, scope: BandwidthNodeObservationScope): Promise { + active() + const origin = token && typeof token === "object" ? observations.get(token) : undefined + if (!origin) fail("NOT_GENUINE") + const owned = window(scope, origin) + try { + if (!isDeepStrictEqual(snapshot(await owned.wait(() => read(origin.source.peerRef, owned.scope)), origin.source.peerRef), origin.source)) fail("SOURCE_CHANGED") + owned.live() + return Object.freeze({ version: 2 as const, peerRef: origin.source.peerRef, sourceProof: origin.source.sourceProof, authorityGeneration: origin.source.authorityGeneration, + routingEndpointEpoch: origin.source.endpoint.routingEndpointEpoch, observedAt: origin.observedAt, expiresAt: origin.expiresAt, claims: origin.claims, + physicalUniquenessVerified: false as const, cloneResistanceVerified: false as const }) + } catch (error) { if (error instanceof BandwidthPeerObservationError) throw error; throw new BandwidthPeerObservationError("SOURCE_UNAVAILABLE") } + finally { owned.dispose() } + }, + async inspectMetadata(token:unknown,input:BandwidthNodeObservationScope):Promise{ + // Capture the FIRST horizon. A fresh caller scope cannot renew retained + // evidence, adopt a new source or request a credential-reading fallback. + const wall=Date.now(),mono=performance.now() + active() + const origin=token&&typeof token==="object"?observations.get(token):undefined + if(!origin)fail("NOT_GENUINE") + if(!assertMetadataCurrent)fail("SOURCE_UNAVAILABLE") + if(!input||types.isProxy(input)||Object.getPrototypeOf(input)!==Object.prototype)fail("INVALID_INPUT") + const fields=Object.getOwnPropertyDescriptors(input) + if(Reflect.ownKeys(fields).some(key=>typeof key!=="string"||!["deadlineAt","signal"].includes(key)) + ||Object.values(fields).some(field=>!("value"in field)||!field.enumerable))fail("INVALID_INPUT") + const requested:unknown=fields.deadlineAt?.value,signal:unknown=fields.signal?.value + if(typeof requested!=="number"||!Number.isSafeInteger(requested)||signal!==undefined&&(types.isProxy(signal)||!(signal instanceof AbortSignal)))fail("INVALID_INPUT") + const external=signal as AbortSignal|undefined,aborted=Object.getOwnPropertyDescriptor(AbortSignal.prototype,"aborted")!.get! + const isAborted=(value?:AbortSignal)=>value?Reflect.apply(aborted,value,[]):false + try{isAborted(external);isAborted(origin.signal)}catch{return fail("INVALID_INPUT")} + const duration=Math.min(5000,requested-wall,origin.expiresAt-wall,origin.monotonicEnd-mono) + if(duration<=0||wall!!value))] + const abort=()=>controller.abort(),timer=setTimeout(abort,Math.max(1,Math.ceil(duration))) + const live=()=>{const now=Date.now();active();if(controller.signal.aborted||signals.some(isAborted)||now=deadlineAt||performance.now()>=end)fail("CANCELLED")} + windows.add(controller) + for(const signal of signals)Reflect.apply(EventTarget.prototype.addEventListener,signal,["abort",abort,{once:true}]) + let rejectStopped!:(error:BandwidthPeerObservationError)=>void + const stopped=new Promise((_resolve,reject)=>{rejectStopped=reject}) + const stop=()=>rejectStopped(new BandwidthPeerObservationError("CANCELLED")) + controller.signal.addEventListener("abort",stop,{once:true}) + try{ + live() + await Promise.race([stopped,Promise.resolve().then(()=>{live();return assertMetadataCurrent(origin.source,scope)})]) + live() + const evidence=Object.freeze({version:2 as const,peerRef:origin.source.peerRef,sourceProof:origin.source.sourceProof,authorityGeneration:origin.source.authorityGeneration, + routingEndpointEpoch:origin.source.endpoint.routingEndpointEpoch,observedAt:origin.observedAt,expiresAt:origin.expiresAt,claims:origin.claims,physicalUniquenessVerified:false as const,cloneResistanceVerified:false as const}) + return Object.freeze({format:"mmapp-bandwidth-peer-public-metadata-v1" as const,evidence}) + }catch(error){if(error instanceof BandwidthPeerObservationError)throw error;throw new BandwidthPeerObservationError("SOURCE_UNAVAILABLE")} + finally{ + clearTimeout(timer);controller.signal.removeEventListener("abort",stop) + for(const signal of signals)Reflect.apply(EventTarget.prototype.removeEventListener,signal,["abort",abort]) + windows.delete(controller);controller.abort() + } + }, + /** Only a managed application source can execute a protected btest. An + * external destination remains observable without gaining source authority. */ + async strictClient(token: unknown, scope: BandwidthNodeObservationScope) { + active() + const origin = token && typeof token === "object" ? observations.get(token) : undefined + if (!origin) fail("NOT_GENUINE") + if (origin.source.peerRef.kind !== "server") fail("INVALID_INPUT") + const sourceServerId = origin.source.peerRef.serverId, owned = window(scope, origin) + const fresh = async () => { if (!isDeepStrictEqual(snapshot(await owned.wait(() => read(origin.source.peerRef, owned.scope)), origin.source.peerRef), origin.source)) fail("SOURCE_CHANGED") } + try { + await fresh() + const { MikrotikClient } = await owned.wait(() => import("./mikrotik.js")) + await fresh(); owned.live() + return new MikrotikClient(origin.source.endpoint, sourceServerId, { requireBandwidthPermit: true }) + } catch (error) { if (error instanceof BandwidthPeerObservationError) throw error; throw new BandwidthPeerObservationError("SOURCE_UNAVAILABLE") } + finally { owned.dispose() } + }, + close() { closed = true; for (const controller of windows) controller.abort() }, + }) +} diff --git a/backend/src/services/bandwidth-peer-reference.ts b/backend/src/services/bandwidth-peer-reference.ts new file mode 100644 index 00000000..081f187d --- /dev/null +++ b/backend/src/services/bandwidth-peer-reference.ts @@ -0,0 +1,31 @@ +import { z } from "zod" + +/** An enrollment is an independent operator-owned identity, never an invented + * application server or an alias for the source of bandwidth credentials. */ +export type BandwidthPeerRef = Readonly<{ kind: "server"; serverId: number } | { kind: "external"; enrollmentId: string }> +const uuid = z.string().uuid().regex(/^[a-f0-9-]+$/) +export class BandwidthPeerReferenceError extends Error { + readonly code = "BANDWIDTH_PEER_REFERENCE_REFUSED" + constructor() { super("Bandwidth peer reference refused") } +} +function fail(): never { throw new BandwidthPeerReferenceError() } +export function parseBandwidthPeerRef(raw: unknown): BandwidthPeerRef { + if (!raw || typeof raw !== "object" || Object.getPrototypeOf(raw) !== Object.prototype) fail() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== 2 || Object.keys(fields).length !== 2 + || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) fail() + if (fields.kind?.value === "server" && fields.serverId && Object.keys(fields).every(key => ["kind", "serverId"].includes(key))) { + const serverId: unknown = fields.serverId.value + if (!Number.isSafeInteger(serverId) || Number(serverId) < 1) fail() + return Object.freeze({ kind: "server", serverId: serverId as number }) + } + if (fields.kind?.value === "external" && fields.enrollmentId && Object.keys(fields).every(key => ["kind", "enrollmentId"].includes(key))) { + if (!uuid.safeParse(fields.enrollmentId.value).success) fail() + return Object.freeze({ kind: "external", enrollmentId: fields.enrollmentId.value as string }) + } + return fail() +} +export function bandwidthPeerRefKey(raw: unknown): string { + const peer = parseBandwidthPeerRef(raw) + return peer.kind === "server" ? `server:${peer.serverId}` : `external:${peer.enrollmentId}` +} diff --git a/backend/src/services/bandwidth-peer-resolver.test.ts b/backend/src/services/bandwidth-peer-resolver.test.ts new file mode 100644 index 00000000..448b64a6 --- /dev/null +++ b/backend/src/services/bandwidth-peer-resolver.test.ts @@ -0,0 +1,221 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createHmac, randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import { createBandwidthPeerResolver, BandwidthPeerResolverError, type BandwidthPeerResolverDependencies } from "./bandwidth-peer-resolver.js" +import type { BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +type Row=Record +const canonical=(value:unknown):string=>Array.isArray(value)?"["+value.map(canonical).join(",")+"]":value&&typeof value==="object"?"{"+Object.entries(value).sort(([a],[b])=>ab?1:0).map(([key,value])=>JSON.stringify(key)+":"+canonical(value)).join(",")+"}":JSON.stringify(value) +const mac=(purpose:string,value:unknown)=>createHmac("sha256","synthetic-owned-resolver-issuer").update("mmapp-bandwidth-node-"+purpose+"-v1:"+canonical(value)).digest("hex") +const root={installationId:"11111111-1111-4111-8111-111111111111",issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64)} +const canonicalNodeId="22222222-2222-4222-8222-222222222222" +const deferred=()=>{let resolve!:()=>void;const promise=new Promise(done=>{resolve=done});return{promise,resolve}} +const scope=(ms=2000)=>({deadlineAt:Date.now()+ms}) +const refused=(error:unknown)=>error instanceof BandwidthPeerResolverError&&error.automaticReplay===false +function fixture(kind:"server"|"external"="external"){ + const peerRef:BandwidthPeerRef=kind==="server"?{kind:"server",serverId:71}:{kind:"external",enrollmentId:randomUUID()} + const token=Object.freeze({kind:"owned-private-observation"}),now=Date.now() + let evidence:BandwidthPeerObservationEvidence={version:2,peerRef,sourceProof:"c".repeat(64),authorityGeneration:"source-v2",routingEndpointEpoch:randomUUID(),observedAt:now,expiresAt:now+60000,claims:[{kind:"system-id",value:"SHARED-SYSTEM"},{kind:"software-id",value:"SHARED-SOFTWARE"}],physicalUniquenessVerified:false,cloneResistanceVerified:false} + let authority={...root,authorityProof:"d".repeat(64),authorityGeneration:"original-operator-grant",expiresAt:now+60000} + const subject=kind==="server"?71:(peerRef as {enrollmentId:string}).enrollmentId,incarnation=randomUUID(),eventId=randomUUID(),external=kind==="external" + const claimProof=mac("claim",evidence.claims),id=external?{enrollmentId:String(subject)}:{serverId:Number(subject)} + const event={event_id:eventId,incarnation,node_id:canonicalNodeId,kind:"enroll",old_status:null,new_status:"active",authority_proof:authority.authorityProof,event_mac:mac(external?"external-event":"event",{...root,eventId,...id,incarnation,nodeId:canonicalNodeId,kind:"enroll",oldStatus:null,newStatus:"active",authorityProof:authority.authorityProof})} + let rows:Row[]=[{subject:String(subject),incarnation,node_id:canonicalNodeId,status:"active",record_mac:mac(external?"external-alias":"alias",{...root,...id,incarnation,nodeId:canonicalNodeId,status:"active"}),claim_proof:claimProof,enrollment_mac:mac("node",{...root,nodeId:canonicalNodeId,claimProof}),installation_id:root.installationId,issuer_key_sha256:root.issuerKeySha256,quiescence_proof:root.quiescenceProof,authority_status:"active",events:[event]}] + let command:unknown="SELECT",revoked=false,inspections=0,authorizations=0,connections=0 + const queries:Array<{text:string;values?:unknown[];options?:{signal?:AbortSignal;timeoutMs:number}}>=[],releases:unknown[]=[],proofs:Array<{purpose:string;value:unknown}>=[] + let inspectHook:((count:number)=>Promise)|undefined,authorizeHook:((count:number,e:Readonly)=>Promise)|undefined,connectHook:(()=>Promise)|undefined,queryHook:(()=>Promise)|undefined,proofHook:((purpose:string)=>Promise)|undefined + const connection:ProbeStorageConnection={release(unsafe){releases.push(unsafe)},async query(text,values,options){queries.push({text,values,options});await queryHook?.();return{rows:structuredClone(rows),command}}} + const dependencies:BandwidthPeerResolverDependencies={ + async inspect(value){inspections++;await inspectHook?.(inspections);if(value!==token)throw Error("not genuine");return structuredClone(evidence)}, + async authorize(peer,_scope,e){authorizations++;assert.deepEqual(peer,peerRef);await authorizeHook?.(authorizations,e);if(revoked)throw Error("original grant revoked");return{...authority}}, + async connect(){connections++;await connectHook?.();return connection}, + async keyedProof(purpose,value){proofs.push({purpose,value:structuredClone(value)});await proofHook?.(purpose);return mac(purpose,value)}, + } + const resolver=createBandwidthPeerResolver(dependencies) + return{resolver,dependencies,token,peerRef,queries,releases,proofs,row:()=>rows[0],event:()=>((rows[0].events as Row[])[0]), + setRows:(value:Row[])=>{rows=value},command:(value:unknown)=>{command=value},revoke:()=>{revoked=true}, + setEvidence:(change:Partial)=>{evidence={...evidence,...change}},setAuthority:(change:Partial)=>{authority={...authority,...change}}, + onInspect:(hook:typeof inspectHook)=>{inspectHook=hook},onAuthorize:(hook:typeof authorizeHook)=>{authorizeHook=hook},onConnect:(hook:typeof connectHook)=>{connectHook=hook},onQuery:(hook:typeof queryHook)=>{queryHook=hook},onProof:(hook:typeof proofHook)=>{proofHook=hook}, + inspections:()=>inspections,authorizations:()=>authorizations,connections:()=>connections} +} + +test("managed/external genuine v2 refs resolve through exactly one SELECT snapshot, original MAC domains, and frozen existing receipt",async()=>{ + const fixtures=[fixture("server"),fixture("external")] + for(const f of fixtures){const result=await f.resolver.resolve(f.token,scope());assert.equal(result.canonicalNodeId,canonicalNodeId);assert.equal(result.outcome,"existing");assert.equal(result.status,"active");assert.deepEqual(result.peerRef,f.peerRef);assert(Object.isFrozen(result));assert(Object.isFrozen(result.peerRef));assert.equal(f.connections(),1);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]) + const q=f.queries[0];assert(q.text.startsWith("WITH alias AS MATERIALIZED"));assert(q.text.includes("events AS MATERIALIZED"));assert(q.text.includes("JOIN mmapp_control.bandwidth_nodes"));assert(q.text.includes("JOIN mmapp_control.bandwidth_node_authority"));assert(!/\b(?:BEGIN|COMMIT|ROLLBACK|INSERT|UPDATE|DELETE|TRUNCATE|CREATE|ALTER)\b/.test(q.text));assert(!q.text.includes("bandwidth_node_occupancy"));assert.deepEqual(q.values,[f.peerRef.kind==="server"?f.peerRef.serverId:f.peerRef.enrollmentId]);assert(q.options?.signal instanceof AbortSignal);assert(q.options!.timeoutMs>0&&q.options!.timeoutMs<=5000) + const external=f.peerRef.kind==="external";assert(q.text.includes(external?"bandwidth_external_node_aliases":"bandwidth_node_aliases"));assert(q.text.includes(external?"$1::pg_catalog.uuid":"$1::pg_catalog.int8"));assert.deepEqual(f.proofs.map(p=>p.purpose),["claim","node",external?"external-alias":"alias",external?"external-event":"event"]) + } + assert.deepEqual(fixtures[0].proofs[0],fixtures[1].proofs[0]);assert.deepEqual(fixtures[0].proofs[1],fixtures[1].proofs[1]);assert.notDeepEqual(fixtures[0].proofs[2],fixtures[1].proofs[2]) +}) +test("absent/duplicate snapshot, wrong SQL command and rows-only facade never auto-enroll or return a receipt",async()=>{ + for(const mode of ["absent","duplicate","rows-only","wrong-command"]){const f=fixture();if(mode==="absent")f.setRows([]);if(mode==="duplicate")f.setRows([f.row(),structuredClone(f.row())]);if(mode==="rows-only")f.command(undefined);if(mode==="wrong-command")f.command("UPDATE");await assert.rejects(f.resolver.resolve(f.token,scope()),refused);assert.equal(f.queries.length,1);assert.equal(f.proofs.length,0);assert.deepEqual(f.releases,[true])} +}) +test("typed evidence remains a private same-reader capability: copies, public evidence and another token refuse before checkout",async()=>{ + const f=fixture();for(const token of [{...f.token},JSON.parse(JSON.stringify(f.token)),{version:2,peerRef:f.peerRef},fixture().token])await assert.rejects(f.resolver.resolve(token,scope()),refused) + assert.equal(f.connections(),0);assert.equal(f.queries.length,0) +}) +test("snapshot authority/key/quiescence/status and original alias/node/event linkage are exact",async()=>{ + for(const change of [ + (f:ReturnType)=>{f.row().subject=randomUUID()},(f:ReturnType)=>{f.row().installation_id=randomUUID()},(f:ReturnType)=>{f.row().issuer_key_sha256="f".repeat(64)},(f:ReturnType)=>{f.row().quiescence_proof="f".repeat(64)},(f:ReturnType)=>{f.row().authority_status="quarantined"},(f:ReturnType)=>{f.row().status="tombstoned"},(f:ReturnType)=>{f.row().node_id=randomUUID()},(f:ReturnType)=>{f.event().incarnation=randomUUID()},(f:ReturnType)=>{f.event().node_id=randomUUID()},(f:ReturnType)=>{f.event().old_status="active"},(f:ReturnType)=>{f.event().kind="quarantine"},(f:ReturnType)=>{f.row().events=[]},(f:ReturnType)=>{f.row().events=[f.event(),structuredClone(f.event())]},(f:ReturnType)=>{f.row().unexpected="trusted"}, + ]){const f=fixture();change(f);await assert.rejects(f.resolver.resolve(f.token,scope()),refused);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true])} +}) +test("claim/node/managed-or-external alias/event MAC tampering cannot create a replacement identity",async()=>{ + for(const kind of ["server","external"] as const)for(const field of ["claim_proof","enrollment_mac","record_mac","event_mac","authority_proof"]){const f=fixture(kind);if(field==="event_mac"||field==="authority_proof")f.event()[field]="f".repeat(64);else f.row()[field]="f".repeat(64);await assert.rejects(f.resolver.resolve(f.token,scope()),refused);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true])} +}) +test("source/native claim/routing epoch and original grant/key/generation/expiry drift during SELECT refuse before private proof or return",async()=>{ + for(const mutate of [ + (f:ReturnType)=>f.setEvidence({sourceProof:"f".repeat(64)}), + (f:ReturnType)=>f.setEvidence({claims:[{kind:"system-id",value:"CHANGED"}]}), + (f:ReturnType)=>f.setEvidence({routingEndpointEpoch:randomUUID()}), + (f:ReturnType)=>f.revoke(), + (f:ReturnType)=>f.setAuthority({issuerKeySha256:"f".repeat(64)}), + (f:ReturnType)=>f.setAuthority({authorityGeneration:"new-grant"}), + (f:ReturnType)=>f.setAuthority({expiresAt:Date.now()+120000}), + ]){const f=fixture();f.onQuery(async()=>mutate(f));await assert.rejects(f.resolver.resolve(f.token,scope()),refused);assert.equal(f.proofs.length,0);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true])} +}) +test("the original grant is the last async guard after inspection and private signing; revocation does not admit another IO",async()=>{ + for(const phase of ["inspect-after-sql","private-node-proof"]){const f=fixture();if(phase==="inspect-after-sql")f.onInspect(async()=>{if(f.queries.length)f.revoke()});else f.onProof(async purpose=>{if(purpose==="node")f.revoke()});await assert.rejects(f.resolver.resolve(f.token,scope()),refused);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]);if(phase==="inspect-after-sql")assert.equal(f.proofs.length,0);else assert.deepEqual(f.proofs.map(p=>p.purpose),["claim","node"])} +}) +test("genuine evidence is deeply immutable for authorizer/private claim proof; returned source objects cannot be rewritten",async()=>{ + const f=fixture();let checked=0 + f.onAuthorize(async(_count,evidence)=>{checked++;assert(Object.isFrozen(evidence));assert(Object.isFrozen(evidence.peerRef));assert(Object.isFrozen(evidence.claims));assert(evidence.claims.every(Object.isFrozen))}) + await f.resolver.resolve(f.token,scope());assert(checked>2) + const hostile=fixture();hostile.onAuthorize(async(_count,evidence)=>{(evidence as unknown as Row).sourceProof="f".repeat(64)}) + await assert.rejects(hostile.resolver.resolve(hostile.token,scope()),refused);assert.equal(hostile.connections(),0) +}) +test("construction captures server-owned dependency methods once; later request-side field replacements cannot select a SQL/source/key callback",async()=>{ + const f=fixture();const forbidden=async()=>{assert.fail("replacement callback executed")} + f.dependencies.inspect=forbidden;f.dependencies.authorize=forbidden;f.dependencies.connect=forbidden;f.dependencies.keyedProof=forbidden + assert.equal((await f.resolver.resolve(f.token,scope())).canonicalNodeId,canonicalNodeId);assert.equal(f.queries.length,1) +}) +test("invalid, accessor-bearing, symbol-bearing, expired or already-aborted scopes refuse before source/SQL",async()=>{ + const controller=new AbortController();controller.abort() + const inputs:unknown[]=[null,{}, {deadlineAt:NaN},{deadlineAt:Date.now()-1},{deadlineAt:Date.now()+1000,extra:true},{deadlineAt:Date.now()+1000,signal:controller.signal},{deadlineAt:Date.now()+1000,signal:{aborted:false}}] + let accessors=0;inputs.push(Object.defineProperty({},"deadlineAt",{enumerable:true,get(){accessors++;return Date.now()+1000}}));const symbolic={deadlineAt:Date.now()+1000};Object.defineProperty(symbolic,Symbol("proof"),{value:true});inputs.push(symbolic) + for(const raw of inputs){const f=fixture();await assert.rejects(f.resolver.resolve(f.token,raw as {deadlineAt:number}),refused);assert.equal(f.inspections(),0);assert.equal(f.connections(),0)}assert.equal(accessors,0) +}) +test("a stalled authorizer is bounded; late successful original authority never checks out a connection",{timeout:2000},async()=>{ + const f=fixture(),entered=deferred(),gate=deferred();f.onAuthorize(async()=>{entered.resolve();await gate.promise}) + const pending=f.resolver.resolve(f.token,scope(60));await entered.promise;await assert.rejects(pending,refused);gate.resolve();await delay(10);assert.equal(f.connections(),0);assert.equal(f.queries.length,0) +}) +test("abort during owned checkout destroys a late lease exactly once and sends no SQL",{timeout:2000},async()=>{ + const f=fixture(),entered=deferred(),gate=deferred(),controller=new AbortController();f.onConnect(async()=>{entered.resolve();await gate.promise}) + const pending=f.resolver.resolve(f.token,{...scope(),signal:controller.signal});await entered.promise;controller.abort();await assert.rejects(pending,refused);assert.deepEqual(f.releases,[]);gate.resolve();await delay(10);assert.deepEqual(f.releases,[true]);assert.equal(f.queries.length,0) +}) +test("abort during blocked SELECT bounds the operation, destroys lease, and late rows cannot run proofs or return success",{timeout:2000},async()=>{ + const f=fixture(),entered=deferred(),gate=deferred(),controller=new AbortController();f.onQuery(async()=>{entered.resolve();await gate.promise}) + const pending=f.resolver.resolve(f.token,{...scope(),signal:controller.signal});await entered.promise;assert.notEqual(f.queries[0].options?.signal,controller.signal);controller.abort();await assert.rejects(pending,refused);assert.equal(f.queries[0].options?.signal?.aborted,true);assert.deepEqual(f.releases,[true]);gate.resolve();await delay(10);assert.equal(f.proofs.length,0);assert.deepEqual(f.releases,[true]);assert.equal(f.queries.length,1) +}) +test("original monotonic expiry survives a 60-second wall clock rollback during blocked SQL; late result cannot renew budget",{timeout:2000},async()=>{ + const f=fixture(),entered=deferred(),gate=deferred(),realNow=Date.now;f.onQuery(async()=>{entered.resolve();await gate.promise}) + const began=performance.now(),pending=f.resolver.resolve(f.token,scope(100)) + try{await entered.promise;Date.now=()=>realNow()-60000;await assert.rejects(pending,refused);assert(performance.now()-began<1000);assert.deepEqual(f.releases,[true]);gate.resolve();await delay(10);assert.equal(f.proofs.length,0);assert.equal(f.queries.length,1)}finally{Date.now=realNow;gate.resolve()} +}) +test("an earlier inspected capability expiry reschedules the original timer and cannot be renewed by later authority",{timeout:2000},async()=>{ + const f=fixture(),entered=deferred(),gate=deferred();f.setEvidence({expiresAt:Date.now()+80});f.onQuery(async()=>{entered.resolve();await gate.promise}) + const pending=f.resolver.resolve(f.token,scope(1500));await entered.promise;await assert.rejects(pending,refused);assert.deepEqual(f.releases,[true]);f.setEvidence({expiresAt:Date.now()+10000});gate.resolve();await delay(10);assert.equal(f.proofs.length,0);assert.equal(f.queries.length,1) +}) + +test("earlier original grant expiry and captured input deadline cannot be extended while a lease or SQL is pending",{timeout:2500},async()=>{ + for(const phase of ["grant-sql","input-checkout"] as const){ + const f=fixture(),entered=deferred(),gate=deferred(),originalScope=scope(phase==="input-checkout"?80:1500) + if(phase==="grant-sql"){f.setAuthority({expiresAt:Date.now()+80});f.onQuery(async()=>{entered.resolve();await gate.promise})} + else f.onConnect(async()=>{entered.resolve();await gate.promise}) + const pending=f.resolver.resolve(f.token,originalScope);await entered.promise;originalScope.deadlineAt=Date.now()+60000 + await assert.rejects(pending,refused);f.setAuthority({expiresAt:Date.now()+60000});gate.resolve();await delay(10) + assert.equal(f.proofs.length,0);assert.equal(f.queries.length,phase==="grant-sql"?1:0);assert.deepEqual(f.releases,[true]) + } +}) +test("driver/source failure exposes only stable refusal, destroys lease and never retries SELECT",async()=>{ + const f=fixture();f.onQuery(async()=>{throw Error("private endpoint/password/key")}) + await assert.rejects(f.resolver.resolve(f.token,scope()),error=>refused(error)&&!String(error).includes("password"));assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]);assert.equal(f.proofs.length,0) +}) + +test("managed/external atomic issuer uses one frozen four-material batch with identical scalar domains and linkage",async()=>{ + for(const kind of ["server","external"] as const){ + const f=fixture(kind),batches:Array=[] + const dependencies={...f.dependencies,keyedProofs:async(inputs:readonly Readonly<{purpose:string;value:unknown}>[],current:{deadlineAt:number;signal?:AbortSignal})=>{ + assert(Object.isFrozen(inputs));assert.equal(inputs.length,4);assert(current.signal instanceof AbortSignal) + for(const input of inputs){assert(Object.isFrozen(input));assert(Object.isFrozen(input.value))} + const claims=inputs[0].value as readonly object[];assert(Object.isFrozen(claims));assert(claims.every(Object.isFrozen)) + batches.push(structuredClone(inputs));return Object.freeze(inputs.map(input=>mac(input.purpose,input.value))) + }} + const resolver=createBandwidthPeerResolver(dependencies) + dependencies.keyedProofs=async()=>{assert.fail("replacement batch issuer executed")} + const receipt=await resolver.resolve(f.token,scope()) + assert.equal(receipt.canonicalNodeId,canonicalNodeId);assert(Object.isFrozen(receipt));assert.equal(batches.length,1);assert.equal(f.proofs.length,0) + assert.deepEqual(batches[0].map(input=>input.purpose),["claim","node",kind==="external"?"external-alias":"alias",kind==="external"?"external-event":"event"]) + const row=f.row(),event=f.event(),values=batches[0].map(input=>mac(input.purpose,input.value)) + assert.deepEqual(values,[row.claim_proof,row.enrollment_mac,row.record_mac,event.event_mac]);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]) + assert.equal((batches[0][1].value as Row).claimProof,row.claim_proof) + } +}) + +test("atomic issuer rejects wrong length, mutable/sparse/accessor/symbol/nonenumerable outputs and invalid hashes without scalar fallback",async()=>{ + let getterCalls=0 + const outputs:Array<(values:string[])=>unknown>=[ + values=>Object.freeze(values.slice(0,3)),values=>Object.freeze([...values,values[0]]),values=>values, + values=>Object.freeze([...values.slice(0,2),"g".repeat(64),values[3]]),values=>Object.freeze([...values.slice(0,2),null,values[3]]), + values=>{const result=[...values];delete result[1];return Object.freeze(result)}, + values=>{const result=[...values];Object.defineProperty(result,"2",{get(){getterCalls++;return values[2]},enumerable:true});return Object.freeze(result)}, + values=>{const result=[...values];Object.defineProperty(result,"2",{value:values[2],enumerable:false});return Object.freeze(result)}, + values=>{const result=[...values];Object.defineProperty(result,Symbol("issuer-proof"),{value:true});return Object.freeze(result)}, + ] + for(const output of outputs){const f=fixture();let calls=0 + const resolver=createBandwidthPeerResolver({...f.dependencies,keyedProofs:async inputs=>{calls++;return output(inputs.map(input=>mac(input.purpose,input.value))) as readonly string[]}}) + await assert.rejects(resolver.resolve(f.token,scope()),refused);assert.equal(calls,1);assert.equal(f.proofs.length,0);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]) + } + assert.equal(getterCalls,0) +}) + +test("atomic claim/node/alias/event MAC tampering still refuses every original material",async()=>{ + for(const kind of ["server","external"] as const)for(const index of [0,1,2,3]){ + const f=fixture(kind),resolver=createBandwidthPeerResolver({...f.dependencies,keyedProofs:async inputs=>Object.freeze(inputs.map((input,i)=>i===index?"f".repeat(64):mac(input.purpose,input.value)))}) + await assert.rejects(resolver.resolve(f.token,scope()),refused);assert.equal(f.proofs.length,0);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]) + } +}) + +test("atomic issuer captures parsed immutable materials before await; later driver object changes cannot rewrite them",async()=>{ + const f=fixture(),entered=deferred(),gate=deferred();let inputsSeen:readonly Readonly<{purpose:string;value:unknown}>[]|undefined + const resolver=createBandwidthPeerResolver({...f.dependencies,keyedProofs:async inputs=>{ + inputsSeen=inputs;entered.resolve();await gate.promise + return Object.freeze(inputs.map(input=>mac(input.purpose,input.value))) + }}) + const pending=resolver.resolve(f.token,scope());await entered.promise + const originalNode=(inputsSeen![1].value as Row).nodeId + f.row().node_id=randomUUID();f.row().claim_proof="f".repeat(64);f.event().authority_proof="f".repeat(64) + assert.equal((inputsSeen![1].value as Row).nodeId,originalNode) + assert.throws(()=>{(inputsSeen![1].value as Row).nodeId=randomUUID()}) + gate.resolve();assert.equal((await pending).canonicalNodeId,canonicalNodeId);assert.deepEqual(f.releases,[true]) +}) + +test("atomic post-batch original actor/key/generation/expiry and native evidence revocation cannot return a receipt",async()=>{ + for(const drift of ["actor","key","generation","expiry","source"] as const){const f=fixture() + const resolver=createBandwidthPeerResolver({...f.dependencies,keyedProofs:async inputs=>{ + const values=Object.freeze(inputs.map(input=>mac(input.purpose,input.value))) + if(drift==="actor")f.revoke() + if(drift==="key")f.setAuthority({issuerKeySha256:"f".repeat(64)}) + if(drift==="generation")f.setAuthority({authorityGeneration:"replacement"}) + if(drift==="expiry")f.setAuthority({expiresAt:Date.now()+120000}) + if(drift==="source")f.setEvidence({sourceProof:"f".repeat(64)}) + return values + }}) + await assert.rejects(resolver.resolve(f.token,scope()),refused);assert.equal(f.proofs.length,0);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]) + } +}) + +test("abort/expiry during blocked atomic issuer destroys lease once; late MACs cause zero new SQL or success",{timeout:2500},async()=>{ + for(const kind of ["abort","expiry"] as const){const f=fixture(),entered=deferred(),gate=deferred(),controller=new AbortController();let calls=0,issuerScope:{signal?:AbortSignal}|undefined + const resolver=createBandwidthPeerResolver({...f.dependencies,keyedProofs:async(inputs,current)=>{ + calls++;issuerScope=current;entered.resolve();await gate.promise;return Object.freeze(inputs.map(input=>mac(input.purpose,input.value))) + }}) + const pending=resolver.resolve(f.token,{...scope(kind==="expiry"?80:2000),signal:controller.signal});await entered.promise + if(kind==="abort")controller.abort() + await assert.rejects(pending,refused);assert.equal(issuerScope!.signal!.aborted,true);assert.deepEqual(f.releases,[true]) + gate.resolve();await delay(10);assert.equal(calls,1);assert.equal(f.queries.length,1);assert.equal(f.proofs.length,0);assert.deepEqual(f.releases,[true]) + } +}) diff --git a/backend/src/services/bandwidth-peer-resolver.ts b/backend/src/services/bandwidth-peer-resolver.ts new file mode 100644 index 00000000..68391ec9 --- /dev/null +++ b/backend/src/services/bandwidth-peer-resolver.ts @@ -0,0 +1,132 @@ +/** Read-only peer resolution on the caller-owned verified node session. One + * SELECT captures alias/node/event/authority in the same MVCC snapshot. It + * never enrolls, retires, mutates occupancy or acquires a second connection. */ +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { parseBandwidthPeerRef, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" +import type { BandwidthPeerRegistryReceipt } from "./bandwidth-peer-fence.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +const authoritySchema = z.object({ installationId: uuid, issuerKeySha256: hash, quiescenceProof: hash, authorityProof: hash, + authorityGeneration: z.string().min(1).max(200), expiresAt: z.number().int().positive().safe() }).strict() +type Authority = z.infer +const claim = z.object({ kind: z.enum(["system-id", "software-id"]), value: z.string().min(1).refine(value => Buffer.byteLength(value) <= 256 && value.trim() === value && !/[\u0000-\u001f\u007f]/.test(value)) }).strict() +const evidenceSchema = z.object({ version: z.literal(2), peerRef: z.unknown().transform(parseBandwidthPeerRef), sourceProof: hash, authorityGeneration: z.string().min(1).max(200), + routingEndpointEpoch: uuid, observedAt: z.number().int().nonnegative().safe(), expiresAt: z.number().int().positive().safe(), + claims: z.array(claim).min(1).max(2).refine(value => value.length === 1 || value[0].kind === "system-id" && value[1].kind === "software-id"), + physicalUniquenessVerified: z.literal(false), cloneResistanceVerified: z.literal(false) }).strict() +const eventSchema = z.object({ event_id: uuid, incarnation: uuid, node_id: uuid, kind: z.literal("enroll"), old_status: z.null(), new_status: z.literal("active"), authority_proof: hash, event_mac: hash }).strict() +const rowSchema = z.object({ subject: z.string().min(1).max(64), incarnation: uuid, node_id: uuid, status: z.literal("active"), record_mac: hash, claim_proof: hash, + enrollment_mac: hash, installation_id: uuid, issuer_key_sha256: hash, quiescence_proof: hash, authority_status: z.literal("active"), events: z.array(eventSchema).length(1) }).strict() +export interface BandwidthPeerResolverDependencies { + inspect(observation: unknown, scope: BandwidthScope): Promise + authorize(peer: BandwidthPeerRef, scope: BandwidthScope, evidence: Readonly): Promise + connect(scope: BandwidthScope): Promise + keyedProof(purpose: string, value: unknown, scope: BandwidthScope): Promise + /** Optional genuine node-session atomic 1..4 issuer, captured at construction. + * All MACs use one freshly verified key with no awaits between materials. */ + keyedProofs?(inputs: readonly Readonly<{ purpose: string; value: unknown }>[], scope: BandwidthScope): Promise +} +export class BandwidthPeerResolverError extends Error { + readonly code = "BANDWIDTH_PEER_RESOLUTION_REFUSED" + readonly automaticReplay = false + constructor() { super("Текущая постоянная привязка bandwidth peer не подтверждена") } +} +function fail(): never { throw new BandwidthPeerResolverError() } +const select = (external: boolean) => external ? EXTERNAL : MANAGED +const query = (external: boolean) => `WITH alias AS MATERIALIZED ( + SELECT ${external ? "enrollment_id::pg_catalog.text" : "server_id::pg_catalog.text"} AS subject,incarnation,node_id,status,record_mac + FROM mmapp_control.${external ? "bandwidth_external_node_aliases" : "bandwidth_node_aliases"} + WHERE ${external ? "enrollment_id=$1::pg_catalog.uuid" : "server_id=$1::pg_catalog.int8"} LIMIT 2 +), events AS MATERIALIZED ( + SELECT event_id::pg_catalog.text,incarnation::pg_catalog.text,node_id::pg_catalog.text,kind,old_status,new_status,authority_proof,event_mac + FROM mmapp_control.${external ? "bandwidth_external_node_events" : "bandwidth_node_events"} + WHERE ${external ? "enrollment_id=$1::pg_catalog.uuid" : "server_id=$1::pg_catalog.int8"} ORDER BY kind LIMIT 3 +) +SELECT a.subject,a.incarnation::pg_catalog.text,a.node_id::pg_catalog.text,a.status,a.record_mac,n.claim_proof,n.enrollment_mac, + r.installation_id::pg_catalog.text,r.issuer_key_sha256,r.quiescence_proof,r.status AS authority_status, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(e)) FROM events e),'[]'::pg_catalog.jsonb) AS events +FROM alias a JOIN mmapp_control.bandwidth_nodes n ON n.node_id=a.node_id +JOIN mmapp_control.bandwidth_node_authority r ON r.id=1 LIMIT 2` +// The only two SQL statements are constructed at module load from literals. +// Request values select a validated reference variant, never SQL identifiers. +const MANAGED = query(false), EXTERNAL = query(true) + +export function createBandwidthPeerResolver(dependencies: BandwidthPeerResolverDependencies) { + const inspect = dependencies.inspect.bind(dependencies), authorize = dependencies.authorize.bind(dependencies), connect = dependencies.connect.bind(dependencies), keyedProof = dependencies.keyedProof.bind(dependencies) + const keyedProofs = dependencies.keyedProofs?.bind(dependencies) + return Object.freeze({ async resolve(observation: unknown, rawScope: BandwidthScope): Promise> { + const enteredWall = Date.now(), enteredMono = performance.now() + if (!rawScope || typeof rawScope !== "object" || Object.getPrototypeOf(rawScope) !== Object.prototype || Reflect.ownKeys(rawScope).length !== Object.keys(rawScope).length + || Object.values(Object.getOwnPropertyDescriptors(rawScope)).some(field => !("value" in field) || !field.enumerable) + || Object.keys(rawScope).some(key => !["deadlineAt", "signal"].includes(key)) || !Number.isSafeInteger(rawScope.deadlineAt) + || rawScope.signal !== undefined && !(rawScope.signal instanceof AbortSignal)) fail() + let expiry = Math.min(rawScope.deadlineAt, enteredWall + 10000), active = true, connection: ProbeStorageConnection | undefined, released = false + const signal = rawScope.signal, controller = new AbortController() + const scope = () => Object.freeze({ deadlineAt: expiry, signal: controller.signal }) + const live = () => { if (!active || signal?.aborted || controller.signal.aborted || Date.now() >= expiry || performance.now() >= enteredMono + Math.max(0, expiry - enteredWall)) fail() } + const release = () => { if (connection && !released) { released = true; try { connection.release(true) } catch { /* Never reuse a potentially stale lease. */ } } } + let reject!: (error: Error) => void + const stopped = new Promise((_resolve, rejectPromise) => { reject = rejectPromise }) + const stop = () => { active = false; controller.abort(); release(); reject(new BandwidthPeerResolverError()) } + let timer: ReturnType | undefined + const schedule = () => { clearTimeout(timer); timer = setTimeout(stop, Math.max(1, Math.min(expiry - Date.now(), enteredMono + Math.max(0, expiry - enteredWall) - performance.now()))) } + schedule(); signal?.addEventListener("abort", stop, { once: true }) + const work = async () => { + try { + live(); const evidence = evidenceSchema.parse(await inspect(observation, scope())); live() + evidence.claims.forEach(Object.freeze); Object.freeze(evidence.claims); Object.freeze(evidence) + expiry = Math.min(expiry, evidence.expiresAt); schedule(); live() + const authority = Object.freeze(authoritySchema.parse(await authorize(evidence.peerRef, scope(), evidence))); live() + expiry = Math.min(expiry, authority.expiresAt); schedule(); live() + const guard = async () => { + live(); if (!isDeepStrictEqual(evidenceSchema.parse(await inspect(observation, scope())), evidence)) fail() + live(); if (!isDeepStrictEqual(authoritySchema.parse(await authorize(evidence.peerRef, scope(), evidence)), authority)) fail() + live() + } + const proof = async (purpose: string, value: unknown) => { await guard(); const mac = await keyedProof(purpose, value, scope()); await guard(); if (!hash.safeParse(mac).success) fail(); return mac } + await guard(); const opened = await connect(scope()); connection = opened + if (!active) { release(); fail() }; live() + const peerRef = evidence.peerRef, external = peerRef.kind === "external", subject = peerRef.kind === "external" ? peerRef.enrollmentId : peerRef.serverId + await guard(); const result: { rows: Record[]; command?: unknown } = await opened.query(select(external), [subject], { signal: controller.signal, timeoutMs: Math.max(1, Math.min(5000, expiry - Date.now())) }); await guard() + if (result.command !== "SELECT" || result.rows.length !== 1) fail() + const row = rowSchema.parse(result.rows[0]), event = row.events[0] + if (row.subject !== String(subject) || row.installation_id !== authority.installationId || row.issuer_key_sha256 !== authority.issuerKeySha256 || row.quiescence_proof !== authority.quiescenceProof + || event.incarnation !== row.incarnation || event.node_id !== row.node_id) fail() + const root = { installationId: authority.installationId, issuerKeySha256: authority.issuerKeySha256, quiescenceProof: authority.quiescenceProof } + if (keyedProofs) { + // Every material is parsed/captured before the first issuer await. + // The dependent node material uses the exact persisted claim proof, + // preserving the original domain/root/linkage validation unchanged. + const inputs = Object.freeze([ + Object.freeze({ purpose: "claim", value: evidence.claims }), + Object.freeze({ purpose: "node", value: Object.freeze({ ...root, nodeId: row.node_id, claimProof: row.claim_proof }) }), + Object.freeze({ purpose: external ? "external-alias" : "alias", value: Object.freeze({ ...root, ...(external ? { enrollmentId: String(subject) } : { serverId: Number(subject) }), incarnation: row.incarnation, nodeId: row.node_id, status: "active" }) }), + Object.freeze({ purpose: external ? "external-event" : "event", value: Object.freeze({ ...root, eventId: event.event_id, ...(external ? { enrollmentId: String(subject) } : { serverId: Number(subject) }), incarnation: row.incarnation, nodeId: row.node_id, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: event.authority_proof }) }), + ]) + await guard(); const raw = await keyedProofs(inputs, scope()); await guard() + if (!Array.isArray(raw) || !Object.isFrozen(raw) || raw.length !== 4 || Reflect.ownKeys(raw).length !== 5) fail() + const descriptors = Object.getOwnPropertyDescriptors(raw) + const values = Object.freeze([0, 1, 2, 3].map(index => { + const field = descriptors[String(index)] + if (!field || !("value" in field) || !field.enumerable || !hash.safeParse(field.value).success) fail() + return field.value as string + })) + if (row.claim_proof !== values[0] || row.enrollment_mac !== values[1] || row.record_mac !== values[2] || event.event_mac !== values[3]) fail() + } else if (row.claim_proof !== await proof("claim", evidence.claims) + || row.enrollment_mac !== await proof("node", { ...root, nodeId: row.node_id, claimProof: row.claim_proof }) + || row.record_mac !== await proof(external ? "external-alias" : "alias", { ...root, ...(external ? { enrollmentId: String(subject) } : { serverId: Number(subject) }), incarnation: row.incarnation, nodeId: row.node_id, status: "active" }) + || event.event_mac !== await proof(external ? "external-event" : "event", { ...root, eventId: event.event_id, ...(external ? { enrollmentId: String(subject) } : { serverId: Number(subject) }), incarnation: row.incarnation, nodeId: row.node_id, kind: "enroll", oldStatus: null, newStatus: "active", authorityProof: event.authority_proof })) fail() + await guard() + return Object.freeze({ peerRef: parseBandwidthPeerRef(evidence.peerRef), canonicalNodeId: row.node_id, incarnation: row.incarnation, status: "active" as const, outcome: "existing" as const }) + } catch { throw new BandwidthPeerResolverError() } + finally { release() } + } + try { return await Promise.race([work(), stopped]) } + finally { active = false; clearTimeout(timer); signal?.removeEventListener("abort", stop); controller.abort() } + } }) +} diff --git a/backend/src/services/bandwidth-public-registry.sql-helper.ts b/backend/src/services/bandwidth-public-registry.sql-helper.ts new file mode 100644 index 00000000..436aefa2 --- /dev/null +++ b/backend/src/services/bandwidth-public-registry.sql-helper.ts @@ -0,0 +1,146 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +/** Owned PostgreSQL acceptance for PUBLIC metadata leases only. + * No Docker Q, secret-key verification, native observation or enrollment authority. */ +import assert from "node:assert/strict" +import { readFile, rename, writeFile } from "node:fs/promises" +import { resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setImmediate as immediate } from "node:timers/promises" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import type { createOwnedExternalRegistryHarness } from "./bandwidth-external-node-registry.sql-helper.js" + +type Harness = Awaited> +type Signed = Awaited> + +export async function acceptOwnedPublicRegistryMetadata(f: Harness, signed: Signed): Promise { + assertOwnedBandwidthSql() + assert.equal(process.env.MMAPP_OWN_PUBLIC_REGISTRY_METADATA, "true") + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + const state = resolve(process.env.MCP_STATE_DIR!) + const path = (name: string) => resolve(state, "external-source-" + name) + const secrets = [path("event.key"), path("issuer.key")] + const moved: string[] = [], checks: string[] = [] + const rootTables = ["bandwidth_source_identity", "bandwidth_node_authority", "bandwidth_nodes", "bandwidth_node_aliases", "bandwidth_node_events", "bandwidth_external_node_aliases", "bandwidth_external_node_events"] + const before = await f.digest(rootTables), gets = f.nativeGets(), posts = f.nativePosts() + const { pool: application } = await import("../db/index.js") + const migrationCount = async () => Number((await application.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n) + assert.equal(await migrationCount(), MIGRATION_MANIFEST.length) + const privileges = async () => (await f.owner!.query(`SELECT pg_catalog.md5(pg_catalog.jsonb_build_object( + 'schema',(SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_array(nspname,nspacl::text) ORDER BY nspname) FROM pg_catalog.pg_namespace WHERE nspname='mmapp_control'), + 'relations',(SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_array(c.relname,c.relacl::text) ORDER BY c.relname) FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='mmapp_control' AND c.relkind IN ('r','S')), + 'functions',(SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_array(p.proname,p.proacl::text) ORDER BY p.proname,p.oid) FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace WHERE n.nspname='mmapp_control') + )::text) AS digest`)).rows[0].digest + const beforePrivileges = await privileges() + const scope = (signal?: AbortSignal) => ({ deadlineAt: Date.now() + 5000, ...(signal ? { signal } : {}) }) + const gone = async (pid: number) => { + const end = performance.now() + 2000 + for (;;) { + await f.owner!.query("SELECT pg_catalog.pg_stat_clear_snapshot()") + const result = await f.owner!.query("SELECT count(*)::int AS n FROM pg_catalog.pg_stat_activity WHERE pid=$1", [pid]) + if (result.rows[0].n === 0) return + assert(performance.now() < end, "retired public lease physical PID must disappear within bounded cleanup") + await immediate() + } + } + const held = async (pid: number) => { + const result = await f.owner!.query("SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0::pg_catalog.oid AND objid=8723116::pg_catalog.oid AND objsubid=1 AND mode='ExclusiveLock' AND granted) AS held", [pid]) + assert.equal(result.rows[0].held, true) + } + const assertPublic = (value: Awaited>["inspect"]>>) => { + assert.equal(value.format, "mmapp-bandwidth-public-registry-metadata-v1") + assert(Number.isSafeInteger(value.backendPid) && value.backendPid > 0) + assert(Object.isFrozen(value)); assert(Object.isFrozen(value.metadata)) + assert.equal(value.metadata.source.controlGeneration, signed.controlGeneration) + assert.equal(value.metadata.bootstrap.installationId, f.root.installationId) + assert(!Object.keys(value).some(key => /quiescence|credential|dispatch|keyCurrent/i.test(key))) + } + try { + // These paths belong exclusively to this harness; move, do not destroy, and + // restore even after a failed assertion. ENOENT proves a real read would fail. + for (const secret of secrets) { await rename(secret, secret + ".public-only-held"); moved.push(secret) } + console.log("Owned public registry stage:", "same-lease-without-secrets") + const lease = await signed.node.openPublicRegistryMetadata(scope()) + let pid = 0 + try { + assert.deepEqual(Object.keys(lease).sort(), ["close", "inspect"]) + const first = await lease.inspect(); assertPublic(first); pid = first.backendPid + const second = await lease.inspect(); assertPublic(second) + assert.equal(second.backendPid, pid); assert.deepEqual(second.metadata, first.metadata) + await held(pid) + // Actual rival exclusive acquisition refuses; no replacement lock result. + const rival = await f.owner!.connect() + try { + const result = await rival.query("SELECT pg_catalog.pg_try_advisory_lock(8723116) AS acquired") + assert.equal(result.rows[0].acquired, false) + } finally { rival.release(true) } + await assert.rejects(signed.node.openPublicRegistryMetadata(scope())) + await held(pid) + assert.equal((await lease.inspect()).backendPid, pid) + } finally { lease.close(); lease.close() } + await gone(pid); await assert.rejects(lease.inspect()) + checks.push("actual public node lease: missing both secret files, repeated same PID, real exclusive lock/rival refusal, bounded physical close; no native or SQL mutation") + + console.log("Owned public registry stage:", "original-cancellation") + const controller = new AbortController(), canceled = await signed.node.openPublicRegistryMetadata(scope(controller.signal)) + const canceledPid = (await canceled.inspect()).backendPid + controller.abort(); await assert.rejects(canceled.inspect()); canceled.close(); await gone(canceledPid) + checks.push("original cancellation permanently retires the real physical public metadata lease") + + console.log("Owned public registry stage:", "overlapping-inspect") + const concurrent = await signed.node.openPublicRegistryMetadata(scope()) + const concurrentPid = (await concurrent.inspect()).backendPid + const concurrentResults = await Promise.allSettled([concurrent.inspect(), concurrent.inspect()]) + assert(concurrentResults.every(result => result.status === "rejected")) + concurrent.close(); await gone(concurrentPid); await assert.rejects(concurrent.inspect()) + checks.push("overlapping inspect operations fail closed and retire instead of sharing mutable frame state") + + console.log("Owned public registry stage:", "current-source-role-revocation") + const changed = await signed.node.openPublicRegistryMetadata(scope()) + const changedPid = (await changed.inspect()).backendPid + // Source identity generation is genuinely immutable. Revoke the existing + // SELECT privilege instead; never disable its ALWAYS trigger for a fixture. + assert(/^bex_source_[a-f0-9]{32}$/.test(signed.runtimeRole)) + const sourceRole = '"' + signed.runtimeRole + '"' + try { + await f.owner!.query("REVOKE SELECT ON mmapp_control.bandwidth_source_identity FROM " + sourceRole) + await assert.rejects(changed.inspect()) + } finally { + await f.owner!.query("GRANT SELECT ON mmapp_control.bandwidth_source_identity TO " + sourceRole) + changed.close() + } + await gone(changedPid) + checks.push("actual current source-role privilege revocation refuses on the held same lease; exact original SELECT privilege restored without identity-trigger bypass") + + console.log("Owned public registry stage:", "current-catalog-drift") + const catalog = await signed.node.openPublicRegistryMetadata(scope()) + const catalogPid = (await catalog.inspect()).backendPid + try { + await f.owner!.query("CREATE INDEX owned_public_metadata_drift ON mmapp_control.bandwidth_node_aliases(server_id)") + await assert.rejects(catalog.inspect()) + } finally { + await f.owner!.query("DROP INDEX IF EXISTS mmapp_control.owned_public_metadata_drift") + catalog.close() + } + await gone(catalogPid) + checks.push("actual catalog drift is rejected by unchanged registry identity validators, not replaced SQL rows") + + console.log("Owned public registry stage:", "current-public-bootstrap-drift") + const file = path("bootstrap.json"), original = await readFile(file) + const rotated = await signed.node.openPublicRegistryMetadata(scope()) + const rotatedPid = (await rotated.inspect()).backendPid + try { await writeFile(file, "{}"); await assert.rejects(rotated.inspect()) } + finally { await writeFile(file, original); rotated.close() } + await gone(rotatedPid) + checks.push("current public bootstrap corruption retires the physical lease; original signed bytes restored") + + console.log("Owned public registry stage:", "unchanged-final-state") + assert.equal(await f.digest(rootTables), before) + assert.equal(await privileges(), beforePrivileges) + assert.equal(await migrationCount(), MIGRATION_MANIFEST.length) + assert.equal(f.nativeGets(), gets); assert.equal(f.nativePosts(), posts) + checks.push("all registry rows and schema/table/function privileges unchanged after restored owned fault DDL; original 39 migrations unchanged; zero native GET/POST") + return Object.freeze(checks) + } finally { + for (const secret of moved.reverse()) await rename(secret + ".public-only-held", secret) + } +} diff --git a/backend/src/services/bandwidth-public-registry.sql.test.ts b/backend/src/services/bandwidth-public-registry.sql.test.ts new file mode 100644 index 00000000..aae66b71 --- /dev/null +++ b/backend/src/services/bandwidth-public-registry.sql.test.ts @@ -0,0 +1,26 @@ +/** Exclusive disposable-cluster profile. No RouterOS/Docker or runtime wiring. */ +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +async function main() { + assertOwnedBandwidthSql() + assert.equal(process.env.MMAPP_OWN_PUBLIC_REGISTRY_METADATA, "true") + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + const { createOwnedExternalRegistryHarness } = await import("./bandwidth-external-node-registry.sql-helper.js") + const { acceptOwnedPublicRegistryMetadata } = await import("./bandwidth-public-registry.sql-helper.js") + const fixture = await createOwnedExternalRegistryHarness() + try { + const signed = await fixture.createSignedSource() + const checks = await acceptOwnedPublicRegistryMetadata(fixture, signed) + console.log("Owned public registry metadata accepted:", JSON.stringify({ + groups: checks.length, checks, actualSql: true, publicMetadataOnly: true, + currentSecretKeyVerification: false, quiescenceProof: false, + operatorWindowAcceptance: false, productionWired: false, + })) + } finally { await fixture.close() } +} + +void main().catch(() => { + console.error("Owned public registry metadata acceptance failed; no real backend/CHR actions") + process.exitCode = 1 +}) diff --git a/backend/src/services/bandwidth-registry-combined.test.ts b/backend/src/services/bandwidth-registry-combined.test.ts new file mode 100644 index 00000000..9aba52e0 --- /dev/null +++ b/backend/src/services/bandwidth-registry-combined.test.ts @@ -0,0 +1,105 @@ +/** Actual registry guards; explicit inert SQL/issuer/provenance fixture only. + * No PG, Docker, external Q, native transport or operating acceptance claim. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { createBandwidthNodeRegistry, type BandwidthNodeRegistryDependencies } from "./bandwidth-node-registry.js" +import { createBandwidthExternalNodeRegistry, type BandwidthExternalNodeRegistryDependencies } from "./bandwidth-external-node-registry.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { BandwidthNodeObservationEvidence } from "./bandwidth-node-observation.js" +import type { BandwidthPeerObservationEvidence } from "./bandwidth-peer-observation.js" + +type Kind = "managed" | "external" +type Fault = "action" | "peer" | "copy-token" | "scope" | "accessor" | "nested-accessor" | "reuse" | "root-drift" | "late" +function fixture(kind: Kind, useCombined = true) { + const token = Object.freeze(Object.create(null)), subject = kind === "managed" ? 71 : randomUUID(), expiry = Date.now() + 10000 + const root = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) } + const authority = { ...root, authorityProof: "c".repeat(64), authorityGeneration: "synthetic-original", expiresAt: expiry } + const evidence = { version: kind === "managed" ? 1 : 2, ...(kind === "managed" ? { serverId: subject } : { peerRef: { kind: "external", enrollmentId: subject } }), + sourceProof: "d".repeat(64), authorityGeneration: "synthetic-source", routingEndpointEpoch: randomUUID(), observedAt: Date.now(), expiresAt: expiry, + claims: [{ kind: "system-id", value: "synthetic-node" }], physicalUniquenessVerified: false, cloneResistanceVerified: false } + const queries: string[] = [], releases: boolean[] = [] + let frames = 0, inspections = 0, authorizations = 0, getters = 0, revoked = false, saved: unknown, fault: Fault | undefined + let queryHook: ((sql: string) => void) | undefined + const inspect = async (value: unknown) => { inspections++; assert.equal(value, token); return structuredClone(evidence) } + const authorize = async () => { authorizations++; if (revoked) throw Error("synthetic original revoked"); return { ...authority } } + const fresh = async (action: string, observation: unknown, requestedId: number | string | undefined, scope: BandwidthScope): Promise => { + frames++; assert.equal(observation, token); if (revoked) throw Error("synthetic original revoked") + assert(requestedId === undefined || requestedId === subject) + if (fault === "reuse" && saved) return saved as never + const frame: Record = { action, observation, scope, [kind === "managed" ? "serverId" : "enrollmentId"]: subject, + evidence: structuredClone(evidence), authority: { ...authority } } + if (fault === "action") frame.action = "quarantine" + if (fault === "peer") frame[kind === "managed" ? "serverId" : "enrollmentId"] = kind === "managed" ? 72 : randomUUID() + if (fault === "copy-token") frame.observation = { ...token } + if (fault === "scope") frame.scope = { ...scope } + if (fault === "accessor") Object.defineProperty(frame, "authority", { enumerable: true, get() { getters++; return authority } }) + if (fault === "nested-accessor") Object.defineProperty(frame.authority, "authorityProof", { enumerable: true, get() { getters++; return authority.authorityProof } }) + if (fault === "root-drift" && frames > 1) (frame.authority as typeof authority).installationId = randomUUID() + if (fault === "late") { (frame.evidence as typeof evidence).expiresAt = Date.now() + 5; await delay(15) } + saved = Object.freeze(frame); return saved as never + } + const common = { now: Date.now, authorize, async connect() { return { + release(unsafe?: boolean) { releases.push(unsafe === true) }, + async query(sql: string) { + queries.push(sql); queryHook?.(sql) + if (sql === "BEGIN" || sql === "COMMIT" || sql === "ROLLBACK") return { command: sql, rows: [] } + if (sql.startsWith("DO $mmapp_bandwidth_register$")) return { command: "DO", rows: [] } + if (sql.startsWith("WITH aliases AS MATERIALIZED")) return { command: "SELECT", rows: [{ aliases: [], nodes: [], events: [], claim_nodes: [] }] } + if (sql.includes("FROM mmapp_control.bandwidth_node_authority")) return { command: "SELECT", rows: [{ installation_id: root.installationId, + issuer_key_sha256: root.issuerKeySha256, quiescence_proof: root.quiescenceProof, status: "active" }] } + if (sql.includes("set_config") || sql.includes("pg_advisory_xact_lock")) return { command: "SELECT", rows: [] } + throw Error("Unexpected fixed fixture SQL") + }, + } }, async keyedProof() { return "e".repeat(64) } } + const dependencies = kind === "managed" + ? { ...common, inspect: inspect as (value: unknown) => Promise, inspectAndAuthorize: useCombined ? fresh : undefined } satisfies BandwidthNodeRegistryDependencies + : { ...common, inspect: inspect as (value: unknown) => Promise, inspectAndAuthorize: useCombined ? fresh : undefined } satisfies BandwidthExternalNodeRegistryDependencies + const registry = kind === "managed" ? createBandwidthNodeRegistry(dependencies as BandwidthNodeRegistryDependencies) : createBandwidthExternalNodeRegistry(dependencies as BandwidthExternalNodeRegistryDependencies) + return { registry, token, queries, releases, dependencies, setFault(value: Fault) { fault = value }, revoke() { revoked = true }, + hook(value: typeof queryHook) { queryHook = value }, counts: () => ({ frames, inspections, authorizations, getters }) } +} +const scope = () => ({ deadlineAt: Date.now() + 5000 }) +for (const kind of ["managed", "external"] as const) { + test(kind + ": fresh combined frames replace only paired inspect/authorize, generic fallback remains", async () => { + for (const combined of [true, false]) { + const f = fixture(kind, combined) + const receipt = await f.registry.register(f.token, scope()) + assert.equal(receipt.outcome, "new"); assert.equal(f.queries.filter(sql => sql === "COMMIT").length, 1); assert.deepEqual(f.releases, [true]) + if (combined) { assert(f.counts().frames > f.queries.length); assert.equal(f.counts().inspections + f.counts().authorizations, 0) } + else { assert.equal(f.counts().frames, 0); assert(f.counts().inspections > f.queries.length); assert.equal(f.counts().inspections, f.counts().authorizations) } + } + }) + test(kind + ": captured constructor callback cannot be replaced", async () => { + const f = fixture(kind) + f.dependencies.inspectAndAuthorize = async () => { throw Error("replacement") } + assert.equal((await f.registry.register(f.token, scope())).status, "active") + }) + test(kind + ": action/peer/copy/scope/accessor/late frames refuse before SQL", async () => { + for (const fault of ["action", "peer", "copy-token", "scope", "accessor", "nested-accessor", "late"] as const) { + const f = fixture(kind); f.setFault(fault) + await assert.rejects(f.registry.register(f.token, scope())) + assert.equal(f.queries.length, 0); assert.equal(f.counts().getters, 0) + } + }) + test(kind + ": stale reused frame/root drift and copied caller token refuse before BEGIN", async () => { + for (const fault of ["reuse", "root-drift"] as const) { + const f = fixture(kind); f.setFault(fault) + await assert.rejects(f.registry.register(f.token, scope())); assert.equal(f.queries.length, 0); assert.deepEqual(f.releases, [true]) + } + const f = fixture(kind); await assert.rejects(f.registry.register({ ...f.token }, scope())); assert.equal(f.queries.length, 0) + }) + test(kind + ": every post-IO frame rechecks revocation before subsequent SQL or COMMIT", async () => { + for (const boundary of ["BEGIN", "DO"] as const) { + const f = fixture(kind) + f.hook(sql => { if (boundary === "BEGIN" ? sql === "BEGIN" : sql.startsWith("DO $mmapp_bandwidth_register$")) f.revoke() }) + await assert.rejects(f.registry.register(f.token, scope())) + assert(!f.queries.includes("COMMIT")); assert.deepEqual(f.releases, [true]) + // BEGIN's post-ACK guard can fail before transactionStarted is assigned; + // existing fallback destroys that lease, so PG rolls it back on close. + if (boundary === "BEGIN") assert.deepEqual(f.queries, ["BEGIN"]) + else assert.equal(f.queries.at(-1), "ROLLBACK") + } + }) +} diff --git a/backend/src/services/bandwidth-rest-speed-original.sql-helper.ts b/backend/src/services/bandwidth-rest-speed-original.sql-helper.ts new file mode 100644 index 00000000..f88b4042 --- /dev/null +++ b/backend/src/services/bandwidth-rest-speed-original.sql-helper.ts @@ -0,0 +1,215 @@ +/** Owned application SQL + genuine Fastify/JWT + actual localhost TCP only. + * This does not dispatch bandwidth, publish history, activate a caller or use + * signed operator/node control state. No fake SQL rows or native methods. */ +import assert from "node:assert/strict" +import { createHash, randomBytes, randomUUID } from "node:crypto" +import { createServer, request as httpRequest, type ClientRequest } from "node:http" +import { writeFile, readFile, mkdir, unlink } from "node:fs/promises" +import { join } from "node:path" +import type { FastifyInstance } from "fastify" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +export async function acceptRestSpeedBandwidthOriginalSql() { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_REST_SPEED_ORIGINAL, "true") + const { pool } = await import("../db/index.js") + const { default: Fastify } = await import("fastify") + const { SignJWT } = await import("jose") + const { default: authPlugin, requireAuth } = await import("../plugins/auth.js") + const { env } = await import("../config.js") + const { createRestSpeedBandwidthOriginalProvider } = await import("./bandwidth-rest-speed-original.js") + assert.equal(env.authRequired, true) + const directory = process.env.MCP_STATE_DIR!, keyPath = join(directory, "encryption-key"), generationPath = join(directory, "access-generation") + const key = randomBytes(32), generation = randomBytes(32) + await writeFile(keyPath, key, { flag: "wx", mode: 0o600 }); await writeFile(generationPath, generation, { flag: "wx", mode: 0o600 }) + const actor = "owned-rest-speed-" + randomUUID(), probeId = "owned-rest-speed-probe-" + randomUUID() + const users = ["OWNED_REST_SPEED_SOURCE_USER", "OWNED_REST_SPEED_DESTINATION_USER"], passwords = ["OWNED_REST_SPEED_SOURCE_PASSWORD", "OWNED_REST_SPEED_DESTINATION_PASSWORD"] + const privateValues = [...users, ...passwords], checks: string[] = [] + const hidden = (value: string) => assert(!privateValues.some(secret => value.includes(secret))) + const barrier = async (pending: Promise, message: string): Promise => { + let timer: ReturnType | undefined + try { return await Promise.race([pending, new Promise((_yes, no) => { timer = setTimeout(() => no(Error(message)), 10000) })]) } + finally { clearTimeout(timer) } + } + let getCalls = 0, postCalls = 0, protocolErrors = 0, holdNative = false + let heldStarted!: () => void, heldClosed!: () => void, releaseHeld!: () => void + let started = Promise.resolve(), closed = Promise.resolve(), hold = Promise.resolve() + const resetHeld = () => { + started = new Promise(yes => { heldStarted = yes }); closed = new Promise(yes => { heldClosed = yes }); hold = new Promise(yes => { releaseHeld = yes }) + } + resetHeld() + const getOrder: number[] = [] + const routers = [0, 1].map(index => createServer(async (request, response) => { + try { + if (request.method !== "GET") { postCalls++; throw Error("Only read-only GET is allowed") } + assert.equal(request.url, "/rest/ip/address"); assert.equal(request.socket.remoteAddress, "127.0.0.1") + assert.equal(request.headers.authorization === "Basic " + Buffer.from(users[index] + ":" + passwords[index]).toString("base64"), true) + getCalls++; getOrder.push(index) + response.setHeader("content-type", "application/json") + if (holdNative && index === 1) { response.once("close", heldClosed); heldStarted(); await hold; if (response.destroyed) return } + const rows = index === 0 ? [{ interface: "gre-source", address: "198.51.100.1/30" }] : [ + { interface: "gre-destination", address: "198.51.100.99/30", dynamic: "true" }, + { interface: "gre-destination", address: "198.51.100.77/30", disabled: "true" }, + { interface: "gre-destination", address: "198.51.100.66/30", invalid: "true" }, + { interface: "other", address: "198.51.100.55/30" }, + { interface: "gre-destination", address: "198.51.100.2/30", dynamic: "false" }, + ] + response.end(JSON.stringify(rows)) + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned read-only fixture protocol mismatch"}') } + })) + const owner = await pool.connect() + let caller: ClientRequest | undefined + let app: FastifyInstance | undefined, sourceId = 0, destinationId = 0, restoredQuery = false + const actualQuery = pool.query + let credentialQueries = 0, credentialPayloadRows = 0, metadataQueries = 0 + let afterMetadata: (() => Promise) | undefined, beforeCredentials: (() => Promise) | undefined + let lastCredentialRows = -1, routeMode: "positive" | "capture" | "pin-drift" | "hold" = "positive" + let drift: (() => Promise) | undefined, handlerFinished!: () => void + let handlerDone = Promise.resolve() + const command = (interfaces = true) => ({ runId: "owned-private-run", probeId, srcServerId: sourceId, dstServerId: destinationId, + srcInterface: interfaces ? "gre-source" : "", dstInterface: interfaces ? "gre-destination" : "", protocol: "udp", direction: "receive", durationSec: 3 }) + const restoreProbe = async (interfaces = true) => owner.query("UPDATE public.uptime_speed_probes SET src_interface=$2,dst_interface=$3,protocol='udp',direction='receive',duration_sec=3,enabled=true WHERE id=$1", [probeId, interfaces ? "gre-source" : "", interfaces ? "gre-destination" : ""]) + const cutOff = async () => owner.query("INSERT INTO public.mcp_owner_revocations(owner_id,invalid_before) VALUES($1,now()) ON CONFLICT(owner_id) DO UPDATE SET invalid_before=EXCLUDED.invalid_before", [actor]) + const clearCutoff = async () => owner.query("DELETE FROM public.mcp_owner_revocations WHERE owner_id=$1", [actor]) + const rotateEndpoint = async (id: number) => { + const before = (await owner.query("SELECT verify_ssl,routing_endpoint_epoch::text AS epoch FROM public.servers WHERE id=$1", [id])).rows[0] + assert(before); assert.equal(typeof before.verify_ssl, "boolean") + // Migration 0029 deliberately discards an epoch-only UPDATE. Change an + // actual endpoint field and let the unchanged trigger mint its own epoch. + const after = (await owner.query("UPDATE public.servers SET verify_ssl=NOT verify_ssl WHERE id=$1 RETURNING verify_ssl,routing_endpoint_epoch::text AS epoch", [id])).rows[0] + assert(after); assert.equal(after.verify_ssl, !before.verify_ssl); assert.notEqual(after.epoch, before.epoch) + } + const history = async () => (await owner.query("SELECT (SELECT count(*)::int FROM public.uptime_speed_test_runs) AS speed,(SELECT count(*)::int FROM public.scheduler_runs) AS scheduler,(SELECT count(*)::int FROM public.server_probe_executions) AS diagnostic")).rows[0] + try { + for (const router of routers) await new Promise((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) }) + const ids = [] + for (let index = 0; index < routers.length; index++) ids.push(Number((await owner.query("INSERT INTO public.servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES($1,'127.0.0.1',$2,false,false,true,$3,$4) RETURNING id", ["owned-rest-speed-" + index, (routers[index].address() as { port: number }).port, users[index], passwords[index]])).rows[0].id)) + ;[sourceId, destinationId] = ids + assert.notEqual(sourceId, destinationId) + await owner.query("INSERT INTO public.uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,dst_interface,protocol,direction,duration_sec,enabled) VALUES($1,$2,$3,'gre-source','gre-destination','udp','receive',3,true)", [probeId, sourceId, destinationId]) + const baselineHistory = await history() + // Observation only: original query/config/parameters and actual PG rows are + // forwarded unchanged. Fixture mutations use a different explicit client. + pool.query = (function(this: typeof pool, ...args: unknown[]) { + const config = args[0] as string | { text: string; query_timeout?: number }, text = typeof config === "string" ? config : config.text + return (async () => { + const credentials = text.startsWith("/* rest-speed-original:credentials */"), metadata = text.startsWith("/* rest-speed-original:metadata */") + if (credentials) { credentialQueries++; assert(typeof config !== "string" && config.query_timeout! > 0); const hook = beforeCredentials; beforeCredentials = undefined; await hook?.() } + const result = await Reflect.apply(actualQuery, this, args) + if (credentials) { lastCredentialRows = result.rows.length; credentialPayloadRows += result.rows.length } + if (metadata) { metadataQueries++; const hook = afterMetadata; afterMetadata = undefined; await hook?.() } + return result + })() + }) as typeof pool.query + const provider = createRestSpeedBandwidthOriginalProvider() + app = Fastify(); await app.register(authPlugin); app.addHook("preHandler", requireAuth) + app.setErrorHandler((_error, _request, reply) => { reply.code(403).send({ refused: true }) }) + app.post("/api/uptime/speed-test", async request => { + try { + const scope = { deadlineAt: Date.now() + 30000 }, token = await provider.capture(request, request.body, scope) + if (routeMode === "hold") throw Error("Held native request unexpectedly completed") + if (routeMode === "pin-drift") { await drift?.(); await assert.rejects(provider.readOriginal(token, scope)); return { ok: true, driftRefused: true } } + if (routeMode === "positive") { + const intent = await provider.readOriginal(token, scope), context = await provider.readSpeedContext(token, scope) + assert.deepEqual(intent.nativeBody, { address: "198.51.100.2", user: users[1], password: passwords[1], protocol: "udp", direction: "receive", duration: "3s" }) + assert.equal(intent.socketTimeoutMs, 30000); assert.equal(intent.callerKind, "rest-speed"); assert.equal(intent.credentialServerId, destinationId) + assert.deepEqual(intent.actualRemotePeer, { kind: "server", serverId: destinationId }); assert(intent.expiresAt <= scope.deadlineAt) + assert.equal(context.srcAddress, "198.51.100.1"); assert.equal(context.dstAddress, "198.51.100.2") + assert.equal((await provider.readPeer(token, { kind: "server", serverId: sourceId }, scope)).endpoint.password, passwords[0]) + await assert.rejects(provider.capture(request, request.body, scope)) + } + return { ok: true } + } finally { handlerFinished?.() } + }) + const jwt = await new SignJWT({ sub: actor, apps: ["mm"], permissions: ["mm:uptime:write"], iat: Math.floor(Date.now() / 1000) - 60, exp: Math.floor(Date.now() / 1000) + 3600 }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).sign(new TextEncoder().encode(env.jwtSecret)) + const send = async (interfaces = true, withProbe = true) => { + const payload: Record = command(interfaces); if (!withProbe) delete payload.probeId + const result = await app!.inject({ method: "POST", url: "/api/uptime/speed-test", headers: { authorization: "Bearer " + jwt }, payload }); hidden(result.body); return result + } + assert.equal((await send()).statusCode, 200); assert.deepEqual(getOrder, [1, 0]); assert.equal(postCalls, 0) + checks.push("actual signed JWT/Fastify request -> original provider -> real bounded metadata/credentials/probe SQL -> two actual localhost Basic-auth GETs; exact static-first body/address/interfaces/30s baseline; private cap cannot recapture") + await owner.query("UPDATE public.uptime_speed_probes SET enabled=false WHERE id=$1", [probeId]) + try { assert.equal((await send()).statusCode, 200, "unchanged disabled manual probe remains eligible") } finally { await restoreProbe() } + assert.equal((await send(true, false)).statusCode, 200, "ad-hoc NULL probe predicate preserves baseline") + checks.push("actual credential SELECT preserves exact disabled manual-probe snapshot and NULL ad-hoc probe; no enabled=true restriction added") + routeMode = "capture" + for (const fault of ["key", "generation", "revoke", "probe", "disabled"] as const) { + const beforeGets = getCalls, beforeSecrets = credentialPayloadRows + afterMetadata = async () => { + if (fault === "key") await writeFile(keyPath, randomBytes(32)) + if (fault === "generation") await writeFile(generationPath, randomBytes(32)) + if (fault === "revoke") await cutOff() + if (fault === "probe") await owner.query("UPDATE public.uptime_speed_probes SET duration_sec=4 WHERE id=$1", [probeId]) + if (fault === "disabled") await owner.query("UPDATE public.servers SET enabled=false WHERE id=$1", [sourceId]) + } + try { assert.equal((await send()).statusCode, 403, fault); assert.equal(getCalls, beforeGets, fault); assert.equal(credentialPayloadRows, beforeSecrets, fault) } + finally { afterMetadata = undefined; await writeFile(keyPath, key); await writeFile(generationPath, generation); await clearCutoff(); await restoreProbe(); await owner.query("UPDATE public.servers SET enabled=true WHERE id=$1", [sourceId]) } + } + checks.push("actual SQL/file changes after metadata await: key/generation/owner revocation/probe projection/disabled participant refuse before any native GET or credential payload transfer") + { + const beforeGets = getCalls, beforeSecrets = credentialPayloadRows + beforeCredentials = () => rotateEndpoint(destinationId) + try { assert.equal((await send()).statusCode, 403); assert.equal(lastCredentialRows, 0); assert.equal(getCalls, beforeGets); assert.equal(credentialPayloadRows, beforeSecrets) } + finally { beforeCredentials = undefined; await owner.query("UPDATE public.servers SET verify_ssl=false WHERE id=$1", [destinationId]) } + checks.push("real separate PG session rotates endpoint epoch at actual credential SELECT boundary; MATERIALIZED complete-pair gate returns zero credential rows and zero native GET") + } + for (const change of ["duration", "enabled"] as const) { + const beforeGets = getCalls, beforeSecrets = credentialPayloadRows, beforeQueries = credentialQueries + beforeCredentials = async () => { + await owner.query(change === "duration" ? "UPDATE public.uptime_speed_probes SET duration_sec=4 WHERE id=$1" + : "UPDATE public.uptime_speed_probes SET enabled=false WHERE id=$1", [probeId]) + } + try { + assert.equal((await send()).statusCode, 403, change) + assert.equal(credentialQueries, beforeQueries + 1); assert.equal(lastCredentialRows, 0) + assert.equal(credentialPayloadRows, beforeSecrets); assert.equal(getCalls, beforeGets) + } finally { beforeCredentials = undefined; await restoreProbe() } + } + checks.push("actual separate-session probe duration/enabled UPDATE immediately before unchanged credential SQL dispatch returns zero credential payload rows and zero native GET; complete-pair and exact captured probe share one MATERIALIZED SELECT") + await restoreProbe(false); routeMode = "pin-drift" + for (const fault of ["key", "generation", "epoch", "probe", "revoke"] as const) { + const beforeGets = getCalls + drift = async () => { + if (fault === "key") await writeFile(keyPath, randomBytes(32)) + if (fault === "generation") await writeFile(generationPath, randomBytes(32)) + if (fault === "epoch") await rotateEndpoint(sourceId) + if (fault === "probe") await owner.query("UPDATE public.uptime_speed_probes SET direction='both' WHERE id=$1", [probeId]) + if (fault === "revoke") await cutOff() + } + try { assert.equal((await send(false)).statusCode, 200, fault); assert.equal(getCalls, beforeGets, fault) } + finally { drift = undefined; await writeFile(keyPath, key); await writeFile(generationPath, generation); await clearCutoff(); await restoreProbe(false); await owner.query("UPDATE public.servers SET verify_ssl=false WHERE id=$1", [sourceId]) } + } + checks.push("private capability retains original key/generation/epoch/probe/owner after capture; real subsequent changes refuse readOriginal without renewing intent or performing a native call") + routeMode = "capture" + const external = join(directory, "owner-revocations", createHash("sha256").update(actor).digest("hex") + ".json") + await mkdir(join(directory, "owner-revocations"), { mode: 0o700 }) + const beforeExternal = getCalls + await writeFile(external, JSON.stringify({ invalidBefore: Date.now() }), { flag: "wx", mode: 0o600 }) + try { assert.equal((await send(false)).statusCode, 403); assert.equal(getCalls, beforeExternal) } finally { await unlink(external) } + await restoreProbe(); routeMode = "hold"; holdNative = true; resetHeld() + handlerDone = new Promise(yes => { handlerFinished = yes }) + const base = await app.listen({ host: "127.0.0.1", port: 0 }), beforePendingGets = getCalls + caller = httpRequest(base + "/api/uptime/speed-test", { method: "POST", headers: { authorization: "Bearer " + jwt, "content-type": "application/json" } }) + caller.on("error", () => {}) + caller.end(JSON.stringify(command())) + await barrier(started, "Actual native GET did not enter held responder") + caller.destroy(Error("Owned original REST client disconnected")) + await barrier(closed, "Actual original REST disconnect did not close pending native TCP response") + await barrier(handlerDone, "Original provider handler remained pending after actual TCP disconnect") + assert.equal(getCalls, beforePendingGets + 1); assert.equal(postCalls, 0) + holdNative = false; releaseHeld() + checks.push("real Fastify TCP caller disconnect aborts original private request signal and closes a genuinely pending native HTTP GET socket; no second GET, POST, history or automatic replay") + assert.deepEqual(await history(), baselineHistory); assert.equal(postCalls, 0); assert.equal(protocolErrors, 0) + assert.equal((await readFile(keyPath)).length, 32); assert.equal((await readFile(generationPath)).length, 32) + pool.query = actualQuery; restoredQuery = true + return { checks, actualReadOnlyNativeGets: getCalls, actualNativePosts: postCalls, metadataQueries, credentialQueries, credentialPayloadRows, + actualSql: true, actualFastifyJwt: true, actualPendingTcpClosed: true, historyUnchanged: true, automaticReplay: false, realChrCalls: 0, runtimeCallerActivation: false } + } finally { + caller?.destroy() + holdNative = false; releaseHeld?.() + if (!restoredQuery) pool.query = actualQuery + await app?.close() + for (const router of routers) { router.closeAllConnections(); if (router.listening) await new Promise((yes, no) => router.close(error => error ? no(error) : yes())) } + owner.release(true) + } +} diff --git a/backend/src/services/bandwidth-rest-speed-original.sql.test.ts b/backend/src/services/bandwidth-rest-speed-original.sql.test.ts new file mode 100644 index 00000000..6e682006 --- /dev/null +++ b/backend/src/services/bandwidth-rest-speed-original.sql.test.ts @@ -0,0 +1,13 @@ +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +assert.equal(process.env.MMAPP_OWN_REST_SPEED_ORIGINAL, "true") +await prepareOwnedBandwidthState() +const { pool, closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +try { + await applySqlMigrations(pool) + const { acceptRestSpeedBandwidthOriginalSql } = await import("./bandwidth-rest-speed-original.sql-helper.js") + console.log("Owned REST speed original SQL/TCP:", JSON.stringify(await acceptRestSpeedBandwidthOriginalSql())) +} finally { await closePool() } diff --git a/backend/src/services/bandwidth-rest-speed-original.test.ts b/backend/src/services/bandwidth-rest-speed-original.test.ts new file mode 100644 index 00000000..9cbcd62e --- /dev/null +++ b/backend/src/services/bandwidth-rest-speed-original.test.ts @@ -0,0 +1,365 @@ +import assert from "node:assert/strict" +import Fastify, { type FastifyRequest } from "fastify" +import { SignJWT } from "jose" +import { mkdtemp, realpath, writeFile, readFile, unlink, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, dirname, basename } from "node:path" +import { randomUUID } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { mock } from "node:test" +import { performance } from "node:perf_hooks" + +process.env.AUTH_REQUIRED = "true" +process.env.AUTH_JWT_SECRET = "rest-speed-original-test-secret" +process.env.AUTH_ISSUER = "https://speed-original.example.invalid" +process.env.NODE_ENV = "test" +const temporaryParent = await realpath(tmpdir()), directory = await mkdtemp(join(temporaryParent, "mm-rest-speed-original-")) +process.env.MCP_STATE_DIR = directory +const { default: authPlugin, requireAuth } = await import("../plugins/auth.js") +const { createRestSpeedBandwidthOriginalProvider, assertRestSpeedBandwidthOriginalProvider } = await import("./bandwidth-rest-speed-original.js") +const { MikrotikClient } = await import("./mikrotik.js") +const { pool, closePool } = await import("../db/index.js") +const { speedTestCommandSchema } = await import("@mmapp/contracts") +const { createBandwidthSpeedRunIdentity } = await import("./bandwidth-speed-run-identity.js") +const provider = createRestSpeedBandwidthOriginalProvider(), foreign = createRestSpeedBandwidthOriginalProvider() +const assertProvider: (value: unknown) => void = assertRestSpeedBandwidthOriginalProvider +assertProvider(provider); assert.throws(() => assertProvider({ ...provider })) +const key = Buffer.alloc(32, 19), generation = Buffer.alloc(32, 29), keyPath = join(directory, "encryption-key"), generationPath = join(directory, "access-generation") +await writeFile(keyPath, key); await writeFile(generationPath, generation) +const query = pool.query, connect = pool.connect, nativeGet = MikrotikClient.prototype.get, nativePost = MikrotikClient.prototype.post +let endpoints: Array> = [], probe: Record | undefined, credentialReads = 0, credentialPayloadRows = 0, nativeReads = 0, posts = 0, revoked = false +let metadataReads = 0, portalReads = 0, nativeFailure = false, metadataHook: (() => void) | undefined, credentialHook: (() => void) | undefined, nativeHook: (() => void) | undefined +let sourceRows: Record[] = [], destinationRows: Record[] = [], nativeOrder: number[] = [], saved: unknown +let pendingNative = false, transportAborted = false, nativeStarted: (() => void) | undefined +let snapshotReads = 0, snapshotBefore: (() => Promise) | undefined, snapshotAfter: (() => Promise) | undefined, snapshotResult: unknown +let historyIds = new Set(), historyReads = 0 +const scope = (milliseconds = 10000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + milliseconds, ...(signal ? { signal } : {}) }) +pool.connect = (() => { throw Error("No real PostgreSQL checkout permitted") }) as never +pool.query = (async (raw: string | { text: string; values: unknown[]; query_timeout: number }, values?: unknown[]) => { + const text = typeof raw === "string" ? raw : raw.text, parameters = typeof raw === "string" ? values : raw.values + if (text.includes("FROM mcp_owner_revocations")) { portalReads++; return { rows: revoked ? [{ invalid_before: new Date() }] : [] } } + assert(typeof raw !== "string"); assert(raw.query_timeout > 0 && raw.query_timeout <= 5000) + if (text.startsWith("/* bandwidth-speed-run:history */")) { + historyReads++; const rows = historyIds.has(String(parameters![0])) ? [{ id: parameters![0] }] : [] + return { command: "SELECT", rowCount: rows.length, rows } + } + if (text.startsWith("/* rest-speed-original:current-snapshot */") || text.startsWith("/* rest-speed-original:completion-snapshot */")) { + snapshotReads++; await snapshotBefore?.() + const withProbe = text.startsWith("/* rest-speed-original:current-snapshot */") + assert(text.includes("expected AS MATERIALIZED")); assert(text.includes("s.enabled=true")); assert(text.includes("pg_catalog.to_jsonb(e)")) + assert.equal(text.includes("public.uptime_speed_probes"), withProbe) + const expected = JSON.parse(String(parameters![0])) as Array> + const matches = expected.map(row => endpoints.filter(endpoint => endpoint.id === row.id)) + const pair = expected.length === 2 && new Set(expected.map(row => row.id)).size === 2 && matches.every((rows, index) => rows.length === 1 + && rows[0].enabled === true && isDeepStrictEqual(Object.fromEntries(Object.keys(expected[index]).map(key => [key, rows[0][key]])), expected[index])) + const expectedProbe = withProbe && parameters![1] !== null ? JSON.parse(String(parameters![1])) as Record : undefined + const selected = !expectedProbe || !!probe && probe.id === parameters![2] + && isDeepStrictEqual(Object.fromEntries(Object.keys(expectedProbe).map(key => [key, probe![key]])), expectedProbe) + const result = { command: "SELECT", rowCount: 1, rows: [{ current: pair && selected }] } + await snapshotAfter?.() + return snapshotResult === undefined ? result : snapshotResult + } + if (text.startsWith("/* rest-speed-original:metadata */")) { + metadataReads++; metadataHook?.(); return { rows: endpoints.filter(endpoint => (parameters![0] as number[]).includes(Number(endpoint.id))).map(endpoint => ({ id: endpoint.id, enabled: endpoint.enabled, routingEndpointEpoch: endpoint.routingEndpointEpoch })) } + } + if (text.startsWith("/* rest-speed-original:probe */")) return { rows: probe && probe.id === parameters![0] ? [structuredClone(probe)] : [] } + assert(text.startsWith("/* rest-speed-original:credentials */")); assert(text.includes("matched AS MATERIALIZED")); assert(text.includes("WHERE a.complete")); assert(text.includes("budget.bytes<=1048576")) + credentialReads++; credentialHook?.() + const allowed = JSON.parse(String(parameters![0])) as Array<{ id: number; epoch: string }> + const matches = allowed.map(expected => endpoints.find(endpoint => endpoint.id === expected.id && endpoint.enabled === true && endpoint.routingEndpointEpoch === expected.epoch)) + assert(text.includes("$2::jsonb IS NULL OR EXISTS")); assert(text.includes("p.id=$3::text")); assert(text.includes("'enabled',p.enabled")); assert(!text.includes("p.enabled=true")) + const expectedProbe = parameters![1] === null ? undefined : JSON.parse(String(parameters![1])) as Record + if (!expectedProbe) assert.equal(parameters![2], null) + const selected = !expectedProbe || !!probe && probe.id === parameters![2] + && isDeepStrictEqual(Object.fromEntries(Object.keys(expectedProbe).map(key => [key, probe![key]])), expectedProbe) + const rows = matches.every(Boolean) && selected ? matches.map(endpoint => ({ id: endpoint!.id, endpoint: structuredClone(endpoint), overflow: false })) : [] + credentialPayloadRows += rows.length; return { rows } +}) as never +MikrotikClient.prototype.get = (async function(this: InstanceType, path: string, timeout: number, signal: AbortSignal | undefined, _budget: unknown, guard: () => Promise) { + assert.equal(path, "/ip/address"); assert(timeout > 0 && timeout <= 10000); signal?.throwIfAborted(); await guard() + const id = Number((this as unknown as { serverId?: number }).serverId) + nativeReads++; nativeOrder.push(id); nativeHook?.() + if (pendingNative) return new Promise((_resolve, reject) => { + assert(signal) + signal.addEventListener("abort", () => { transportAborted = true; reject(Error("Actual request signal aborted native read")) }, { once: true }); nativeStarted?.() + }) + if (nativeFailure) throw Error("Synthetic read-only native failure") + return structuredClone(id === 101 ? sourceRows : destinationRows) +}) as never +MikrotikClient.prototype.post = (async () => { posts++; throw Error("No native POST permitted") }) as never +function fixture() { + revoked = false; nativeFailure = false; metadataHook = undefined; credentialHook = undefined; nativeHook = undefined; nativeOrder = [] + snapshotBefore = undefined; snapshotAfter = undefined; snapshotResult = undefined + historyIds = new Set() + endpoints = [101, 102].map(id => ({ id, enabled: true, host: "192.0.2." + (id - 100), port: 443, useSsl: true, verifySsl: true, username: "fixture-user-" + id, password: "fixture-secret-" + id, routingEndpointEpoch: randomUUID() })) + probe = undefined; sourceRows = [{ interface: "gre-src", address: "198.51.100.1/30" }] + destinationRows = [ + { interface: "gre-dst", address: "198.51.100.99/30", dynamic: "true" }, + { interface: "gre-dst", address: "198.51.100.77/30", disabled: "true" }, + { interface: "gre-dst", address: "198.51.100.66/30", invalid: "true" }, + { interface: "other", address: "198.51.100.55/30" }, + { interface: "gre-dst", address: "198.51.100.2/30", dynamic: "false" }, + { interface: "gre-dst", address: "198.51.100.3/30" }, + ] + return { srcServerId: 101, dstServerId: 102, srcInterface: "gre-src", dstInterface: "gre-dst", protocol: "udp", direction: "receive", durationSec: 3 } +} +const app = Fastify() +await app.register(authPlugin); app.addHook("preHandler", requireAuth) +app.setErrorHandler((_error, _request, reply) => { reply.code(403).send({ refused: true }) }) +const scenario = "positive" +let routeWork: ((request: FastifyRequest) => Promise) | undefined +// Handler ownership is fixed in this test; the request JSON never supplies a +// source, actor, callback, signer, SQL or native dispatch capability. +app.post("/api/uptime/speed-test", async request => { + if (routeWork) return routeWork(request as never) + const originalScope = scope(), token = await provider.capture(request, request.body, originalScope) + saved = token + if (scenario === "positive") { + const intent = await provider.readOriginal(token, scope()), context = await provider.readSpeedContext(token, scope()) + assert.equal(intent.callerKind, "rest-speed"); assert.equal(intent.socketTimeoutMs, 30000); assert.equal(intent.nativeBody.duration, "3s") + const run = createBandwidthSpeedRunIdentity(context.command.runId) + assert.equal(intent.attemptId, run.attemptId); assert.equal(intent.operationId, run.operationId) + assert.equal((intent.request.command as { runId: string }).runId, run.runId) + assert.equal((request.body as { runId?: string }).runId, undefined) + assert(intent.expiresAt <= originalScope.deadlineAt) + const lifetime = await provider.readLifetime(token, scope()) + assert.equal(lifetime.expiresAt, intent.expiresAt) + assert(lifetime.signal instanceof AbortSignal); assert.equal(lifetime.signal.aborted, false) + assert(Number.isFinite(lifetime.monotonicDeadline) && lifetime.monotonicDeadline > performance.now()) + assert(Object.isFrozen(lifetime)) + assert.equal((await provider.readLifetime(token, scope())).monotonicDeadline, lifetime.monotonicDeadline) + assert.deepEqual(intent.nativeBody, { address: "198.51.100.2", user: "fixture-user-102", password: "fixture-secret-102", protocol: "udp", direction: "receive", duration: "3s" }) + assert.equal(context.srcAddress, "198.51.100.1"); assert.equal(context.dstAddress, "198.51.100.2"); assert.equal(context.srcInterfaceAddress, context.srcAddress); assert.equal(context.dstInterfaceAddress, context.dstAddress) + assert(!("password" in context)); assert.deepEqual(nativeOrder, [102, 101]) + assert(Object.isFrozen(intent)); assert(Object.isFrozen(intent.nativeBody)); assert(Object.isFrozen(intent.request)); assert(Object.isFrozen(context.command)) + const source = await provider.readPeer(token, { kind: "server", serverId: 101 }, scope()), destination = await provider.readPeer(token, { kind: "server", serverId: 102 }, scope()) + assert.equal(source.endpoint.password, "fixture-secret-101"); assert.equal(destination.endpoint.password, "fixture-secret-102"); assert.equal(source.authorityGeneration, generation.toString("hex")) + assert.equal((await provider.readOriginal(token, scope())).attemptId, intent.attemptId) + for (const other of [{}, { ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(provider.readOriginal(other, scope())) + await assert.rejects(foreign.readOriginal(token, scope())); await assert.rejects(provider.readPeer(token, { kind: "server", serverId: 103 }, scope())) + await assert.rejects(provider.readPeer(token, { kind: "external", enrollmentId: randomUUID() }, scope())) + const beforeForgedCompletion = credentialReads + for (const result of [{}, { remoteCompletionConfirmed: true, raw: [{ status: "done testing" }] }]) { + await assert.rejects(provider.readPeerAfterCompletion(token, result, { kind: "server", serverId: 101 }, scope())) + } + assert.equal(credentialReads, beforeForgedCompletion) + const before = credentialReads + await assert.rejects(provider.capture(request, request.body, originalScope)); assert.equal(credentialReads, before) + return { ok: true } + } + throw Error("Unexpected scenario") +}) +app.post("/api/uptime/wrong-route", request => provider.capture(request, request.body, scope())) +const mint = (permissions = ["mm:uptime:write"], expires = Math.floor(Date.now() / 1000) + 3600) => new SignJWT({ sub: "original-speed-owner", apps: ["mm"], permissions, iat: Math.floor(Date.now() / 1000) - 10, exp: expires }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(process.env.AUTH_ISSUER!).sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET!)) +const jwt = await mint() +const send = (payload: unknown, token = jwt, url = "/api/uptime/speed-test") => app.inject({ method: "POST", url, headers: { authorization: "Bearer " + token }, payload: payload as Record }) +try { + let command = fixture(), beforeCredentials = credentialReads + await assert.rejects(provider.capture({ body: command, jwtVerify: async () => ({}) } as never, command, scope())); assert.equal(credentialReads, beforeCredentials) + assert.equal((await send(command)).statusCode, 200) + await assert.rejects(provider.readOriginal(saved, scope())) + for (const fault of ["permission", "expiry", "wrong-route", "extra-body", "source-disabled", "duplicate-metadata", "missing-key", "missing-generation"] as const) { + command = fixture(); beforeCredentials = credentialReads + let payload: unknown = command, token = jwt, url = "/api/uptime/speed-test" + if (fault === "permission") token = await mint(["mm:uptime:read"]) + if (fault === "expiry") token = await mint(undefined, Math.floor(Date.now() / 1000) - 1) + if (fault === "wrong-route") url = "/api/uptime/wrong-route" + if (fault === "extra-body") payload = { ...command, nativeBody: { address: "outside.invalid" }, actor: { id: "forged" } } + if (fault === "source-disabled") endpoints[0].enabled = false + if (fault === "duplicate-metadata") endpoints[1].id = 101 + if (fault === "missing-key") await unlink(keyPath) + if (fault === "missing-generation") await unlink(generationPath) + try { + const result = await send(payload, token, url); assert(result.statusCode >= 400, fault); assert.equal(credentialReads, beforeCredentials, fault) + if (fault === "missing-key") await assert.rejects(readFile(keyPath)) + if (fault === "missing-generation") await assert.rejects(readFile(generationPath)) + } + finally { await writeFile(keyPath, key); await writeFile(generationPath, generation) } + } + command = fixture() + routeWork = async request => { + const parsed = speedTestCommandSchema.parse(request.body), commandCopy = { ...parsed }, firstScope = scope() + const pending = provider.capture(request, commandCopy, firstScope); commandCopy.dstServerId = 999 + const token = await pending, intent = await provider.readOriginal(token, scope()) + assert.equal(intent.credentialServerId, 102); return { ok: true } + } + assert.equal((await send(command)).statusCode, 200); routeWork = undefined + command = fixture() + const fixedRunId = "owned-exact-run-id" + const runAttempts: string[] = [] + routeWork = async request => { + const token = await provider.capture(request, request.body, scope()), intent = await provider.readOriginal(token, scope()) + runAttempts.push(intent.attemptId); assert.equal(intent.operationId, createBandwidthSpeedRunIdentity(fixedRunId).operationId) + return { ok: true } + } + try { + assert.equal((await send({ ...command, runId: fixedRunId })).statusCode, 200) + const otherJwt = await new SignJWT({ sub: "another-speed-owner", apps: ["mm"], permissions: ["mm:uptime:write"], iat: Math.floor(Date.now() / 1000) - 10, exp: Math.floor(Date.now() / 1000) + 3600 }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(process.env.AUTH_ISSUER!).sign(new TextEncoder().encode(process.env.AUTH_JWT_SECRET!)) + assert.equal((await send({ ...command, runId: fixedRunId }, otherJwt)).statusCode, 200) + assert.equal(runAttempts.length, 2); assert.equal(runAttempts[0], runAttempts[1]) + } finally { routeWork = undefined } + // A known legacy/history ID is supplemental refusal before new credential + // projection/native. The durable control reserve remains a separate barrier. + command = fixture(); historyIds.add(fixedRunId) + const beforeLegacyCredentials = credentialReads, beforeLegacyNative = nativeReads, beforeHistory = historyReads + assert.equal((await send({ ...command, runId: fixedRunId })).statusCode, 403) + assert.equal(historyReads, beforeHistory + 1); assert.equal(credentialReads, beforeLegacyCredentials); assert.equal(nativeReads, beforeLegacyNative) + for (const fault of ["source-password", "destination-password", "source-host", "destination-port", "source-useSsl", "destination-verifySsl", "source-username", "missing-endpoint", "duplicate-endpoint", + "epoch", "enabled", "probe", "probe-enabled", "probe-deleted", "key", "generation", "directory", "revoked", "request-body", "abort"] as const) { + command = fixture() + if (fault.startsWith("probe")) { command = { ...command, probeId: "fixed-probe" } as never; probe = { id: "fixed-probe", ...command, enabled: false }; delete probe.probeId } + routeWork = async request => { + const controller = new AbortController(), token = await provider.capture(request, request.body, scope(10000, controller.signal)) + if (fault === "source-password") endpoints[0].password = "changed" + if (fault === "destination-password") endpoints[1].password = "changed" + if (fault === "source-host") endpoints[0].host = "203.0.113.1" + if (fault === "destination-port") endpoints[1].port = 8080 + if (fault === "source-useSsl") endpoints[0].useSsl = false + if (fault === "destination-verifySsl") endpoints[1].verifySsl = false + if (fault === "source-username") endpoints[0].username = "changed" + if (fault === "missing-endpoint") endpoints.pop() + if (fault === "duplicate-endpoint") endpoints.push({ ...endpoints[0] }) + if (fault === "epoch") endpoints[0].routingEndpointEpoch = randomUUID() + if (fault === "enabled") endpoints[1].enabled = false + if (fault === "probe") probe!.durationSec = 4 + if (fault === "probe-enabled") probe!.enabled = true + if (fault === "probe-deleted") probe = undefined + if (fault === "key") await writeFile(keyPath, Buffer.alloc(32, 41)) + if (fault === "generation") await writeFile(generationPath, Buffer.alloc(32, 43)) + if (fault === "directory") process.env.MCP_STATE_DIR = join(directory, "foreign-source") + if (fault === "revoked") revoked = true + if (fault === "request-body") (request.body as Record).durationSec = 4 + if (fault === "abort") controller.abort() + const before = credentialReads + await assert.rejects(provider.readOriginal(token, scope())) + assert.equal(credentialReads, before, fault) + return { ok: true } + } + try { assert.equal((await send(command)).statusCode, 200, fault) } finally { routeWork = undefined; process.env.MCP_STATE_DIR = directory; await writeFile(keyPath, key); await writeFile(generationPath, generation) } + } + command = fixture() + routeWork = async request => { + const token = await provider.capture(request, request.body, scope()) + const counts = { portal: portalReads, snapshot: snapshotReads, credentials: credentialReads, metadata: metadataReads } + await provider.readOriginal(token, scope()) + assert.equal(portalReads - counts.portal, 2); assert.equal(snapshotReads - counts.snapshot, 1) + assert.equal(credentialReads, counts.credentials); assert.equal(metadataReads, counts.metadata) + return { ok: true } + } + assert.equal((await send(command)).statusCode, 200); routeWork = undefined + // The query fixture computes current from actual mutable endpoint/probe + // projections above. These overrides exercise driver contract rejection only. + for (const malformed of [null, { command: "UPDATE", rowCount: 1, rows: [{ current: true }] }, + { command: "SELECT", rowCount: null, rows: [{ current: true }] }, { command: "SELECT", rowCount: 1, rows: [] }, + { command: "SELECT", rowCount: 1, rows: null }, { command: "SELECT", rowCount: 1, rows: [{ current: null }] }, + { command: "SELECT", rowCount: 1, rows: [{ current: false }] }, { command: "SELECT", rowCount: 1, rows: [{ current: "true" }] }, + { command: "SELECT", rowCount: 1, rows: [{ current: 1 }] }, { command: "SELECT", rowCount: 2, rows: [{ current: true }, { current: true }] }, + { command: "SELECT", rowCount: 1, rows: [{ current: true, endpoint: "must-never-return-secrets" }] }, + { command: "SELECT", rowCount: 1, rows: [{ current: "x".repeat(1048577) }] }, + { command: "SELECT", rowCount: 1, rows: [Object.defineProperty({}, "current", { get() { assert.fail("current getter executed") }, enumerable: true })] }, + ]) { + command = fixture() + routeWork = async request => { + const token = await provider.capture(request, request.body, scope()) + snapshotResult = malformed + await assert.rejects(provider.readOriginal(token, scope())); return { ok: true } + } + try { assert.equal((await send(command)).statusCode, 200) } finally { routeWork = undefined; snapshotResult = undefined } + } + for (const drift of ["key", "generation", "revoke", "body", "directory", "abort", "wall-expiry", "monotonic-expiry"] as const) { + command = fixture() + routeWork = async request => { + const controller = new AbortController(), token = await provider.capture(request, request.body, scope(10000, controller.signal)) + let restoreClock: (() => void) | undefined + snapshotAfter = async () => { + if (drift === "key") await writeFile(keyPath, Buffer.alloc(32, 51)) + if (drift === "generation") await writeFile(generationPath, Buffer.alloc(32, 53)) + if (drift === "revoke") revoked = true + if (drift === "body") (request.body as Record).direction = "both" + if (drift === "directory") process.env.MCP_STATE_DIR = join(directory, "changed-after-snapshot") + if (drift === "abort") controller.abort() + if (drift === "wall-expiry") { const original = Date.now; const spy = mock.method(Date, "now", () => original() + 60000); restoreClock = () => spy.mock.restore() } + if (drift === "monotonic-expiry") { const original = performance.now.bind(performance); const spy = mock.method(performance, "now", () => original() + 60000); restoreClock = () => spy.mock.restore() } + } + try { await assert.rejects(provider.readOriginal(token, scope())) } finally { snapshotAfter = undefined; restoreClock?.() } + return { ok: true } + } + try { assert.equal((await send(command)).statusCode, 200, drift) } + finally { routeWork = undefined; process.env.MCP_STATE_DIR = directory; await writeFile(keyPath, key); await writeFile(generationPath, generation) } + } + command = fixture() + routeWork = async request => { + const controller = new AbortController(), token = await provider.capture(request, request.body, scope(10000, controller.signal)) + let entered!: () => void, settle!: () => void + const started = new Promise(resolve => { entered = resolve }), held = new Promise(resolve => { settle = resolve }) + snapshotBefore = async () => { entered(); await held } + const pending = provider.readOriginal(token, scope()) + await started; controller.abort(); await assert.rejects(pending) + const beforePortal = portalReads; settle(); await new Promise(resolve => setImmediate(resolve)) + assert.equal(portalReads, beforePortal); snapshotBefore = undefined; return { ok: true } + } + try { assert.equal((await send(command)).statusCode, 200) } finally { routeWork = undefined; snapshotBefore = undefined } + command = fixture(); command.dstInterface = ""; command.srcInterface = "" + routeWork = async request => { + const token = await provider.capture(request, request.body, scope()), intent = await provider.readOriginal(token, scope()), context = await provider.readSpeedContext(token, scope()) + assert.equal(intent.nativeBody.address, endpoints[1].host); assert.equal(context.srcAddress, endpoints[0].host); assert.equal(context.srcInterfaceAddress, null); assert.equal(context.dstInterfaceAddress, null); assert.equal(nativeOrder.length, 0) + return { ok: true } + } + assert.equal((await send(command)).statusCode, 200); routeWork = undefined + command = fixture(); command.dstInterface = ""; nativeFailure = true + routeWork = async request => { + const token = await provider.capture(request, request.body, scope()), context = await provider.readSpeedContext(token, scope()) + assert.equal(context.srcAddress, ""); assert.equal(context.srcInterfaceAddress, null); assert.equal(context.address, endpoints[1].host); return { ok: true } + } + assert.equal((await send(command)).statusCode, 200); routeWork = undefined + command = fixture(); nativeFailure = true + assert.equal((await send(command)).statusCode, 403) + command = fixture(); beforeCredentials = credentialReads + metadataHook = () => { revoked = true } + assert.equal((await send(command)).statusCode, 403); assert.equal(credentialReads, beforeCredentials) + command = fixture(); credentialHook = () => { endpoints[1].routingEndpointEpoch = randomUUID() } + assert.equal((await send(command)).statusCode, 403) + for (const fault of ["duration", "source-interface", "destination-interface", "protocol", "direction", "source-id", "destination-id", "enabled", "deleted"] as const) { + command = fixture() + probe = { id: "credential-boundary-probe", ...command, enabled: false } + const beforePayload = credentialPayloadRows, beforeNative = nativeReads + credentialHook = () => { + if (fault === "duration") probe!.durationSec = 4 + if (fault === "source-interface") probe!.srcInterface = "other-source" + if (fault === "destination-interface") probe!.dstInterface = "other-destination" + if (fault === "protocol") probe!.protocol = "tcp" + if (fault === "direction") probe!.direction = "both" + if (fault === "source-id") probe!.srcServerId = 103 + if (fault === "destination-id") probe!.dstServerId = 103 + if (fault === "enabled") probe!.enabled = true + if (fault === "deleted") probe = undefined + } + assert.equal((await send({ ...command, probeId: "credential-boundary-probe" })).statusCode, 403, fault) + assert.equal(credentialPayloadRows, beforePayload, fault); assert.equal(nativeReads, beforeNative, fault) + } + command = fixture(); probe = { id: "disabled-manual-probe", ...command, enabled: false } + routeWork = async request => { + const token = await provider.capture(request, request.body, scope()) + assert.equal((await provider.readOriginal(token, scope())).nativeBody.duration, "3s"); return { ok: true } + } + try { assert.equal((await send({ ...command, probeId: "disabled-manual-probe" })).statusCode, 200) } finally { routeWork = undefined } + command = fixture(); nativeHook = () => { revoked = true } + assert.equal((await send(command)).statusCode, 403) + command = fixture(); pendingNative = true + routeWork = async request => { + const started = new Promise(resolve => { nativeStarted = resolve }) + const pending = provider.capture(request, request.body, scope()) + await Promise.race([started, pending.then(() => { throw Error("Pending native fixture unexpectedly completed") })]) + request.raw.emit("aborted") + await assert.rejects(pending); assert.equal(transportAborted, true) + return { ok: true } + } + try { assert.equal((await send(command)).statusCode, 200) } finally { routeWork = undefined; pendingNative = false; nativeStarted = undefined } + assert.equal(posts, 0); assert(nativeReads > 0); assert(portalReads > 0); assert(metadataReads > 0); assert.equal((await readFile(keyPath)).length, 32) + console.log("REST speed original: genuine Fastify/JWT capability, exact baseline body/address selection, current rights/key/generation/metadata/probe before credentials, immutable intent and drift/cancellation refusal; fake SQL/native reads only, zero BW/history activation") +} finally { + await app.close(); pool.query = query; pool.connect = connect; MikrotikClient.prototype.get = nativeGet; MikrotikClient.prototype.post = nativePost; await closePool() + assert.equal(dirname(directory), temporaryParent); assert(basename(directory).startsWith("mm-rest-speed-original-")); await rm(directory, { recursive: true, force: true }) +} diff --git a/backend/src/services/bandwidth-rest-speed-original.ts b/backend/src/services/bandwidth-rest-speed-original.ts new file mode 100644 index 00000000..1dc8fd7e --- /dev/null +++ b/backend/src/services/bandwidth-rest-speed-original.ts @@ -0,0 +1,291 @@ +/** Original REST uptime speed command only. No bandwidth dispatch, enrollment, + * runtime wiring, history publication or substitute diagnostic workflow. */ +import type { FastifyRequest } from "fastify" +import type { QueryConfig } from "pg" +import { readFile } from "node:fs/promises" +import { join } from "node:path" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { speedTestCommandSchema, type SpeedTestCommand } from "@mmapp/contracts" +import { pool } from "../db/index.js" +import { captureVerifiedRestRequest, inspectVerifiedRestRequest, type VerifiedRestRequestAuthority } from "../plugins/auth.js" +import { fingerprint, McpError, mcpStateDirectory, privateFingerprinterExisting } from "../modules/mcp/security.js" +import type { RoutingEndpoint } from "../modules/mcp/routing-observation-source.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import type { BandwidthPeerExecutionIntent } from "./bandwidth-peer-execution-host.js" +import { readBandwidthPeerCompletion } from "./bandwidth-peer-execution-host.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import { parseBandwidthPeerRef, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import { assertSpeedProbeContext } from "./speed-run-persistence.js" +import { MikrotikClient } from "./mikrotik.js" +import { createBandwidthSpeedRunIdentity, BANDWIDTH_SPEED_RUN_HISTORY_SQL, assertBandwidthSpeedRunHistoryAbsent, BandwidthSpeedRunIdUnavailable } from "./bandwidth-speed-run-identity.js" + +declare const restSpeedBrand: unique symbol +export type RestSpeedBandwidthOriginal = Readonly<{ [restSpeedBrand]: true }> +export interface RestSpeedBandwidthContext { + readonly command: Readonly; readonly srcAddress: string; readonly dstAddress: string + readonly address: string; readonly srcInterfaceAddress: string | null; readonly dstInterfaceAddress: string | null +} +const providers = new WeakSet() +const deny = (): never => { throw new McpError("PERMISSION_DENIED", "Исходный REST speed-контекст изменён или недоступен", 403) } +export function assertRestSpeedBandwidthOriginalProvider(value: unknown): asserts value is ReturnType { + if (!value || typeof value !== "object" || !providers.has(value)) deny() +} +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} +const commandFields = new Set(["runId", "probeId", "srcServerId", "dstServerId", "srcInterface", "dstInterface", "protocol", "direction", "durationSec"]) +function commandSnapshot(raw: unknown): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype) return deny() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== Object.keys(fields).length || Object.keys(fields).some(key => !commandFields.has(key)) + || Object.values(fields).some(field => !("value" in field) || !field.enumerable || field.value !== undefined && !["string", "number"].includes(typeof field.value))) deny() + const parsed = speedTestCommandSchema.parse(Object.fromEntries(Object.entries(fields).map(([key, field]) => [key, field.value]))) + // Keep optional absent IDs out of admission JSON while preserving the exact + // shared contract's coercion/defaults/trim behavior. + return freeze(Object.fromEntries(Object.entries(parsed).filter(([, value]) => value !== undefined)) as SpeedTestCommand) +} +function requestCommand(request: FastifyRequest): Readonly { + const field = Object.getOwnPropertyDescriptor(request, "body") + if (!field || !("value" in field)) return deny() + return commandSnapshot(field.value) +} +function scopeSnapshot(raw: Readonly): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype || Reflect.ownKeys(raw).length !== Object.keys(raw).length) deny() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Object.keys(fields).some(key => !["deadlineAt", "signal"].includes(key)) || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) deny() + const deadlineAt = fields.deadlineAt?.value, signal = fields.signal?.value + if (typeof deadlineAt !== "number" || !Number.isSafeInteger(deadlineAt) || signal !== undefined && !(signal instanceof AbortSignal)) return deny() + return Object.freeze({ deadlineAt, signal }) +} +const metadataSchema = z.array(z.object({ id: z.number().int().positive().safe(), enabled: z.literal(true), routingEndpointEpoch: z.string().uuid() }).strict()).length(2) +const endpointSchema = z.object({ id: z.number().int().positive().safe(), enabled: z.literal(true), routingEndpointEpoch: z.string().uuid(), host: z.string().min(1).max(1000), + port: z.number().int().min(1).max(65535), useSsl: z.boolean(), verifySsl: z.boolean(), username: z.string().max(1000), password: z.string().max(65536) }).strict() +const probeSchema = z.object({ id: z.string(), srcServerId: z.number().int().positive().safe(), dstServerId: z.number().int().positive().safe(), + srcInterface: z.string(), dstInterface: z.string(), protocol: z.enum(["tcp", "udp"]), direction: z.enum(["both", "transmit", "receive"]), durationSec: z.number().int().min(3).max(2147483647), enabled: z.boolean() }).strict() +type Metadata = z.infer +type Probe = z.infer | undefined +const purpose = "rest-speed-bandwidth-original-v1", keyMessage = "rest-speed-bandwidth-original-purpose-key-epoch-v1" +async function existingGeneration(directory: string) { + const bytes = await readFile(join(directory, "access-generation")) + if (bytes.length !== 32) return deny() + return bytes.toString("hex") +} +/** Fixed current-facts projection. Captured credentials are private parameters + * compared INSIDE PostgreSQL; ongoing validation returns only one Boolean. + * Completion uses a separate fixed query omitting ONLY the selected probe. */ +function currentSnapshotSql(includeProbe: boolean): string { + return `/* rest-speed-original:${includeProbe ? "current-snapshot" : "completion-snapshot"} */ WITH expected AS MATERIALIZED ( + SELECT * FROM pg_catalog.jsonb_to_recordset($1::jsonb) AS e(id bigint,enabled boolean,"routingEndpointEpoch" uuid, + host text,port integer,"useSsl" boolean,"verifySsl" boolean,username text,password text)), + matched AS MATERIALIZED ( + SELECT s.id FROM public.servers s JOIN expected e ON e.id=s.id WHERE s.enabled=true + AND pg_catalog.jsonb_build_object('id',s.id,'enabled',s.enabled,'routingEndpointEpoch',s.routing_endpoint_epoch, + 'host',s.host,'port',s.port,'useSsl',s.use_ssl,'verifySsl',s.verify_ssl,'username',s.username,'password',s.password)=pg_catalog.to_jsonb(e)), + endpoints_current AS MATERIALIZED (SELECT count(*)=2 AND count(DISTINCT id)=2 + AND (SELECT count(*) FROM matched)=2 AS current FROM expected)${includeProbe ? `, + chosen_probe AS MATERIALIZED (SELECT pg_catalog.jsonb_build_object('id',p.id,'srcServerId',p.src_server_id,'dstServerId',p.dst_server_id, + 'srcInterface',p.src_interface,'dstInterface',p.dst_interface,'protocol',p.protocol,'direction',p.direction,'durationSec',p.duration_sec,'enabled',p.enabled) AS projection + FROM public.uptime_speed_probes p WHERE p.id=$3::text LIMIT 2)` : ""} + SELECT (e.current${includeProbe ? ` AND ($2::jsonb IS NULL OR ((SELECT count(*) FROM chosen_probe)=1 + AND (SELECT projection FROM chosen_probe LIMIT 1)=$2::jsonb))` : ""}) IS TRUE AS current FROM endpoints_current e` +} +const CURRENT_SNAPSHOT_SQL = currentSnapshotSql(true), COMPLETION_SNAPSHOT_SQL = currentSnapshotSql(false) +/** Same static-first/filter/order selection as executeBandwidthSpeedTestInner. */ +function firstUsableIp(rows: Record[], iface: string): string { + const candidates = rows.filter(row => String(row.interface ?? "").trim() === iface.trim() + && String(row.disabled ?? "false").toLowerCase() !== "true" && String(row.invalid ?? "false").toLowerCase() !== "true" && String(row.address ?? "").trim().includes("/")) + candidates.sort((a, b) => (String(a.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0) - (String(b.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0)) + return candidates[0]?.address ? String(candidates[0].address).split("/")[0]?.trim() ?? "" : "" +} +async function window(input: Readonly, authority: Readonly, originalEnd: number, originalSignal: AbortSignal | undefined, work: (scope: Readonly, live: () => void) => Promise): Promise { + const scope = Object.freeze({ deadlineAt: Math.min(input.deadlineAt, authority.expiresAt), signal: input.signal }) + const end = Math.min(originalEnd, performance.now() + Math.max(0, scope.deadlineAt - Date.now())) + const signals = [...new Set([scope.signal, originalSignal, authority.signal].filter((signal): signal is AbortSignal => !!signal))] + let active = true, rejectStopped!: (error: Error) => void + const live = () => { if (!active || Date.now() >= scope.deadlineAt || performance.now() >= end || signals.some(signal => signal.aborted)) deny() } + const stopped = new Promise((_resolve, reject) => { rejectStopped = reject }) + const stop = () => { active = false; rejectStopped(new McpError("PERMISSION_DENIED", "REST speed-контекст завершён", 403)) } + const timer = setTimeout(stop, Math.max(1, Math.min(scope.deadlineAt - Date.now(), end - performance.now()))) + for (const signal of signals) signal.addEventListener("abort", stop, { once: true }) + try { live(); return await Promise.race([Promise.resolve().then(async () => { live(); const result = await work(scope, live); live(); return result }), stopped]) } + finally { active = false; clearTimeout(timer); for (const signal of signals) signal.removeEventListener("abort", stop) } +} + +/** Fixed production dependencies only. Constructor does no I/O and accepts no + * actor/client/SQL/proof callbacks. Capabilities remain bound to the live HTTP + * request; they are not background job or independent wire-mapping authority. */ +export function createRestSpeedBandwidthOriginalProvider() { + type Entry = { intent: BandwidthPeerExecutionIntent; context: Readonly; peers: ReadonlyMap> + authority: Readonly; end: number; signal?: AbortSignal; assertCurrent(scope: Readonly, live: () => void): Promise + assertAuthorityCurrent(scope: Readonly, live: () => void): Promise } + const entries = new WeakMap() + const captures = new WeakSet() + const read = async (token: unknown, rawScope: Readonly) => { + const scope = scopeSnapshot(rawScope), entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry) return deny() + await window(scope, entry.authority, entry.end, entry.signal, (current, live) => entry.assertCurrent(current, live)) + return entry + } + const provider = Object.freeze({ + async capture(request: FastifyRequest, rawCommand: unknown, rawScope: Readonly): Promise { + const command = commandSnapshot(rawCommand), body = requestCommand(request), scope = scopeSnapshot(rawScope), entered = Date.now(), enteredMono = performance.now() + if (!isDeepStrictEqual(command, body)) return deny() + if (captures.has(request)) return deny() + captures.add(request) + const run = createBandwidthSpeedRunIdentity(command.runId) + const sealedCommand = freeze({ ...command, runId: run.runId }) + const original = await captureVerifiedRestRequest(request, scope) + const authority = await inspectVerifiedRestRequest(original, scope, "mm:uptime:write") + if (authority.method !== "POST" || authority.path !== "/api/uptime/speed-test") return deny() + const end = Math.min(authority.monotonicDeadline, enteredMono + Math.max(0, authority.expiresAt - entered)) + return window(scope, authority, end, scope.signal, async (current, live) => { + const ids = [command.srcServerId, command.dstServerId].sort((a, b) => a - b) + const sourceDirectory = mcpStateDirectory(), generation = await existingGeneration(sourceDirectory); live() + const sign = await privateFingerprinterExisting(purpose); live(); const keyProof = sign(keyMessage) + const basic = async (next: Readonly, checkLive: () => void) => { + checkLive(); if (mcpStateDirectory() !== sourceDirectory || !isDeepStrictEqual(requestCommand(request), command)) deny() + if (!isDeepStrictEqual(await inspectVerifiedRestRequest(original, next, "mm:uptime:write"), authority)) deny(); checkLive() + if (await existingGeneration(sourceDirectory) !== generation || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + if ((await privateFingerprinterExisting(purpose))(keyMessage) !== keyProof || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + if (!isDeepStrictEqual(await inspectVerifiedRestRequest(original, next, "mm:uptime:write"), authority)) deny(); checkLive() + } + const query = async (text: string, values: unknown[], next: Readonly, checkLive: () => void) => { + checkLive(); const config: QueryConfig & { query_timeout: number } = { text, values, query_timeout: Math.max(1, Math.min(5000, next.deadlineAt - Date.now())) } + const result = await pool.query(config); checkLive(); return result + } + const metadata = async (next: Readonly, checkLive: () => void): Promise => { + await basic(next, checkLive) + const result = await query('/* rest-speed-original:metadata */ SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM public.servers WHERE id=ANY($1::bigint[]) ORDER BY id LIMIT 3', [ids], next, checkLive) + await basic(next, checkLive) + const rows = metadataSchema.parse(result.rows) + if (!isDeepStrictEqual(rows.map(row => row.id), ids)) deny() + return rows + } + const probe = async (next: Readonly, checkLive: () => void): Promise => { + if (!command.probeId) return undefined + await basic(next, checkLive) + const rows = (await query('/* rest-speed-original:probe */ SELECT id,src_server_id AS "srcServerId",dst_server_id AS "dstServerId",src_interface AS "srcInterface",dst_interface AS "dstInterface",protocol,direction,duration_sec AS "durationSec",enabled FROM public.uptime_speed_probes WHERE id=$1 LIMIT 2', [command.probeId], next, checkLive)).rows + await basic(next, checkLive); if (rows.length !== 1) return deny() + const value = probeSchema.parse(rows[0]); assertSpeedProbeContext(value, { ...command, id: command.probeId }); return value + } + const endpoints = async (next: Readonly, expected: Metadata, expectedProbe: Probe, checkLive: () => void) => { + await basic(next, checkLive) + // Pair epochs AND the exact selected probe gate credential projection + // in this SAME SQL snapshot, including drift since the prior reads. + // REST manual probes may be disabled: compare captured enabled exactly. + const result = await query(`/* rest-speed-original:credentials */ WITH expected AS MATERIALIZED ( + SELECT * FROM pg_catalog.jsonb_to_recordset($1::jsonb) AS e(id bigint,epoch uuid)), + matched AS MATERIALIZED (SELECT s.id FROM public.servers s JOIN expected e ON e.id=s.id AND e.epoch=s.routing_endpoint_epoch WHERE s.enabled=true), + allowed AS MATERIALIZED (SELECT count(*)=2 AND ($2::jsonb IS NULL OR EXISTS ( + SELECT 1 FROM public.uptime_speed_probes p WHERE p.id=$3::text + AND pg_catalog.jsonb_build_object('id',p.id,'srcServerId',p.src_server_id,'dstServerId',p.dst_server_id, + 'srcInterface',p.src_interface,'dstInterface',p.dst_interface,'protocol',p.protocol,'direction',p.direction, + 'durationSec',p.duration_sec,'enabled',p.enabled)=$2::jsonb)) AS complete FROM matched), + chosen AS MATERIALIZED (SELECT s.id,jsonb_build_object('id',s.id,'host',s.host,'port',s.port,'username',s.username,'password',s.password, + 'useSsl',s.use_ssl,'verifySsl',s.verify_ssl,'enabled',s.enabled,'routingEndpointEpoch',s.routing_endpoint_epoch) AS endpoint + FROM public.servers s JOIN matched m ON m.id=s.id CROSS JOIN allowed a WHERE a.complete), + budget AS (SELECT coalesce(sum(octet_length(endpoint::text)),0) AS bytes FROM chosen) + SELECT id,CASE WHEN budget.bytes<=1048576 THEN endpoint END AS endpoint,budget.bytes>1048576 AS overflow FROM chosen CROSS JOIN budget ORDER BY id LIMIT 3`, + [JSON.stringify(expected.map(row => ({ id: row.id, epoch: row.routingEndpointEpoch }))), expectedProbe ? JSON.stringify(expectedProbe) : null, expectedProbe?.id ?? null], next, checkLive) + await basic(next, checkLive) + if (result.rows.some(row => row.overflow)) deny() + const rows = z.array(endpointSchema).length(2).parse(result.rows.map(row => row.endpoint)) + if (rows.length !== 2 || !isDeepStrictEqual(rows.map(row => ({ id: row.id, enabled: row.enabled, routingEndpointEpoch: row.routingEndpointEpoch })), expected)) deny() + return freeze(rows) + } + const originalMetadata = freeze(await metadata(current, live)), originalProbe = freeze(await probe(current, live)) + await basic(current, live) + let history: unknown + try { history = await query(BANDWIDTH_SPEED_RUN_HISTORY_SQL, [run.runId], current, live) } + catch { throw new BandwidthSpeedRunIdUnavailable(run.runId) } + assertBandwidthSpeedRunHistoryAbsent(history, run.runId) + await basic(current, live) + const originalEndpoints = await endpoints(current, originalMetadata, originalProbe, live) + const expectedEndpoints = JSON.stringify(originalEndpoints), expectedProbe = originalProbe ? JSON.stringify(originalProbe) : null + const filesCurrent = async (checkLive: () => void) => { + checkLive(); if (mcpStateDirectory() !== sourceDirectory || !isDeepStrictEqual(requestCommand(request), command)) deny() + if (await existingGeneration(sourceDirectory) !== generation || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + if ((await privateFingerprinterExisting(purpose))(keyMessage) !== keyProof || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + if (!isDeepStrictEqual(requestCommand(request), command)) deny() + } + const assertSnapshot = async (next: Readonly, checkLive: () => void, includeProbe: boolean) => { + checkLive() + if (!isDeepStrictEqual(await inspectVerifiedRestRequest(original, next, "mm:uptime:write"), authority)) deny(); checkLive() + await filesCurrent(checkLive) + const result = await query(includeProbe ? CURRENT_SNAPSHOT_SQL : COMPLETION_SNAPSHOT_SQL, + includeProbe ? [expectedEndpoints, expectedProbe, originalProbe?.id ?? null] : [expectedEndpoints], next, checkLive) + if (!result || typeof result !== "object" || result.command !== "SELECT" || result.rowCount !== 1 || !Array.isArray(result.rows) || result.rows.length !== 1) deny() + const row = result.rows[0] + if (!row || typeof row !== "object" || Array.isArray(row) || Reflect.ownKeys(row).length !== 1) deny() + const field = Object.getOwnPropertyDescriptor(row, "current") + if (!field || !("value" in field) || !field.enumerable || field.value !== true) deny() + await filesCurrent(checkLive) + // Current actor/portal inspection is LAST, after the final file awaits. + if (!isDeepStrictEqual(await inspectVerifiedRestRequest(original, next, "mm:uptime:write"), authority)) deny(); checkLive() + } + const assertCurrent = (next: Readonly, checkLive: () => void) => assertSnapshot(next, checkLive, true) + const assertAuthorityCurrent = (next: Readonly, checkLive: () => void) => assertSnapshot(next, checkLive, false) + const source = originalEndpoints.find(endpoint => endpoint.id === command.srcServerId)!, destination = originalEndpoints.find(endpoint => endpoint.id === command.dstServerId)! + const addresses = async (endpoint: RoutingEndpoint): Promise[]> => { + let guardFailed = false + const guard = async () => { try { await assertCurrent(current, live) } catch (error) { guardFailed = true; throw error } } + await guard() + let result: unknown + const signal = current.signal ? AbortSignal.any([current.signal, authority.signal]) : authority.signal + try { result = await new MikrotikClient(endpoint, endpoint.id).get("/ip/address", Math.max(1, Math.min(10000, current.deadlineAt - Date.now())), signal, undefined, guard) } + catch { if (guardFailed) return deny(); await guard(); return [] } + await guard(); return z.array(z.record(z.string(), z.unknown())).max(10000).parse(result) + } + // Preserve existing order and fallbacks: destination first, then source. + const destinationRows = command.dstInterface ? await addresses(destination) : [] + const sourceRows = command.srcInterface ? await addresses(source) : [] + const dstAddress = command.dstInterface ? firstUsableIp(destinationRows, command.dstInterface) : destination.host + if (command.dstInterface && !dstAddress) throw new Error(`На интерфейсе назначения '${command.dstInterface}' нет активного IP (проверьте /ip/address — не disabled).`) + const srcAddress = command.srcInterface ? firstUsableIp(sourceRows, command.srcInterface) : source.host + const context = freeze({ command: sealedCommand, srcAddress, dstAddress, address: dstAddress || destination.host, + srcInterfaceAddress: command.srcInterface ? srcAddress || null : null, dstInterfaceAddress: command.dstInterface ? dstAddress || null : null }) + const nativeBody = { address: context.address, user: destination.username, password: destination.password, protocol: command.protocol, direction: command.direction, duration: command.durationSec + "s" } + const intent = freeze({ attemptId: run.attemptId, operationId: run.operationId, callerKind: "rest-speed" as const, sourceServerId: source.id, + actualRemotePeer: { kind: "server" as const, serverId: destination.id }, credentialServerId: destination.id, + request: { command: sealedCommand } as BandwidthPeerExecutionIntent["request"], nativeBody, durationSec: command.durationSec, + // MikrotikClient.bandwidthTest's existing fixed timeout is 30_000ms. + socketTimeoutMs: 30000, authorityProof: sign("authority:" + fingerprint({ authority, command, run, generation })), authorityGeneration: generation, expiresAt: authority.expiresAt }) + await assertCurrent(current, live) + const peers = new Map(originalEndpoints.map(endpoint => [endpoint.id, freeze({ peerRef: { kind: "server" as const, serverId: endpoint.id }, enabled: endpoint.enabled, + sourceProof: sign("endpoint:" + fingerprint({ generation, endpoint })), authorityGeneration: generation, + endpoint: { host: endpoint.host, port: endpoint.port, useSsl: endpoint.useSsl, verifySsl: endpoint.verifySsl, username: endpoint.username, password: endpoint.password, routingEndpointEpoch: endpoint.routingEndpointEpoch } })])) + live(); const token = Object.freeze(Object.create(null)) as RestSpeedBandwidthOriginal + entries.set(token, { intent, context, peers, authority, end, signal: scope.signal, assertCurrent, assertAuthorityCurrent }); return token + }) + }, + async readOriginal(token: unknown, scope: Readonly): Promise { return (await read(token, scope)).intent }, + /** Internal transport lifetime only. The genuine context is checked before + * exposing its fixed horizon and cancellation; this data cannot mint a cap. */ + async readLifetime(token: unknown, scope: Readonly) { + const entry = await read(token, scope) + return Object.freeze({ expiresAt: entry.intent.expiresAt, monotonicDeadline: entry.end, signal: entry.authority.signal }) + }, + /** Internal full speed workflow metadata only; no credentials/public DTO. */ + async readSpeedContext(token: unknown, scope: Readonly): Promise> { return (await read(token, scope)).context }, + async readPeer(token: unknown, rawPeer: BandwidthPeerRef, scope: Readonly): Promise> { + const peer = parseBandwidthPeerRef(rawPeer) + const entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry || peer.kind !== "server" || !entry.peers.has(peer.serverId)) return deny() + await read(token, scope); return entry.peers.get(peer.serverId)! + }, + /** Probe edits after a genuinely completed run affect history association, + * not the already sealed command. Actor/key/endpoint checks remain current. */ + async readPeerAfterCompletion(token: unknown, result: unknown, rawPeer: BandwidthPeerRef, rawScope: Readonly): Promise> { + const scope = scopeSnapshot(rawScope), peer = parseBandwidthPeerRef(rawPeer) + const entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry || peer.kind !== "server" || !entry.peers.has(peer.serverId)) return deny() + if (!isDeepStrictEqual(readBandwidthPeerCompletion(result, token), entry.intent)) return deny() + await window(scope, entry.authority, entry.end, entry.signal, (current, live) => entry.assertAuthorityCurrent(current, live)) + return entry.peers.get(peer.serverId)! + }, + }) + providers.add(provider); return provider +} diff --git a/backend/src/services/bandwidth-scheduled-speed-original.sql-helper.ts b/backend/src/services/bandwidth-scheduled-speed-original.sql-helper.ts new file mode 100644 index 00000000..8706703f --- /dev/null +++ b/backend/src/services/bandwidth-scheduled-speed-original.sql-helper.ts @@ -0,0 +1,198 @@ +/** Owned SQL + actual fixed scheduler/root/ALS + localhost HTTP only. No module + * mocks, origin fabrication, substituted rows, timers, bandwidth or history. + * A forwarding SQL observer enters the genuine scheduler's first settings read; + * genuine pause closes its generation gate before the legacy executor body. */ +import assert from "node:assert/strict" +import { randomBytes, randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { readFile, writeFile, unlink } from "node:fs/promises" +import { join } from "node:path" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +export async function acceptScheduledSpeedBandwidthOriginalSql() { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_SCHEDULED_SPEED_ORIGINAL, "true") + const { pool } = await import("../db/index.js") + const root = await import("./scheduler-automatic-root.js"), scheduler = await import("./scheduler.js") + const { pauseSchedulerJobsAndWait, resumeSchedulerJobs } = await import("./scheduler-running.js") + const { createScheduledSpeedBandwidthOriginalProvider } = await import("./bandwidth-scheduled-speed-original.js") + // Bootstrap establishes the real AsyncResource without refresh/start timers. + root.initializeAutomaticSchedulerRoot() + let currentGeneration = 0 + assert.equal(scheduler.isAutomaticSchedulerGenerationCurrent(currentGeneration), true) + const directory = process.env.MCP_STATE_DIR!, keyPath = join(directory, "encryption-key"), generationPath = join(directory, "access-generation") + const key = randomBytes(32), generation = randomBytes(32) + await writeFile(keyPath, key, { flag: "wx", mode: 0o600 }); await writeFile(generationPath, generation, { flag: "wx", mode: 0o600 }) + const users = ["OWNED_SCHEDULED_SOURCE_USER", "OWNED_SCHEDULED_DESTINATION_USER"], passwords = ["OWNED_SCHEDULED_SOURCE_PASSWORD", "OWNED_SCHEDULED_DESTINATION_PASSWORD"] + const probeId = "owned-scheduled-speed-" + randomUUID(), secondProbeId = probeId + "-second", checks: string[] = [] + let gets = 0, posts = 0, protocolErrors = 0, held = false + const getOrder: number[] = [] + let startHeld!: () => void, closeHeld!: () => void, releaseHeld!: () => void + let started = Promise.resolve(), closed = Promise.resolve(), release = Promise.resolve() + const resetHeld = () => { started = new Promise(yes => { startHeld = yes }); closed = new Promise(yes => { closeHeld = yes }); release = new Promise(yes => { releaseHeld = yes }) } + resetHeld() + async function bounded(promise: Promise, message: string): Promise { + let timer: ReturnType | undefined + try { return await Promise.race([promise, new Promise((_yes, no) => { timer = setTimeout(() => no(Error(message)), 10000) })]) } + finally { clearTimeout(timer) } + } + const routers = [0, 1].map(index => createServer(async (request, response) => { + try { + if (request.method !== "GET") { posts++; throw Error("Owned fixture forbids POST") } + assert.equal(request.url, "/rest/ip/address"); assert.equal(request.socket.remoteAddress, "127.0.0.1") + assert.equal(request.headers.authorization === "Basic " + Buffer.from(users[index] + ":" + passwords[index]).toString("base64"), true) + gets++; getOrder.push(index); response.setHeader("content-type", "application/json") + if (held && index === 1) { response.once("close", closeHeld); startHeld(); await release; if (response.destroyed) return } + response.end(JSON.stringify(index === 0 ? [{ interface: "gre-source", address: "198.51.100.1/30" }] : [ + { interface: "gre-destination", address: "198.51.100.99/30", dynamic: "true" }, + { interface: "gre-destination", address: "198.51.100.77/30", disabled: "true" }, + { interface: "gre-destination", address: "198.51.100.66/30", invalid: "true" }, + { interface: "gre-destination", address: "198.51.100.2/30", dynamic: "false" }, + ])) + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned GET fixture mismatch"}') } + })) + const owner = await pool.connect(), actualQuery = pool.query + let sourceId = 0, destinationId = 0, metadataQueries = 0, credentialQueries = 0, credentialRows = 0, schedulerEntries = 0 + let entry: (() => Promise) | undefined, beforeCredentials: (() => Promise) | undefined + let saved: unknown, savedSignal: AbortSignal | undefined, pendingCapture: Promise | undefined + const provider = createScheduledSpeedBandwidthOriginalProvider() + const scope = (milliseconds = 30000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + milliseconds, ...(signal ? { signal } : {}) }) + const history = async () => (await owner.query("SELECT (SELECT count(*)::int FROM public.uptime_speed_test_runs) AS speed,(SELECT count(*)::int FROM public.scheduler_runs) AS scheduler,(SELECT count(*)::int FROM public.server_probe_executions) AS diagnostic")).rows[0] + const restore = async (interfaces = false) => { + await owner.query("INSERT INTO public.uptime_settings(id,enabled,speed_enabled,speed_interval_sec) VALUES(1,true,true,60) ON CONFLICT(id) DO UPDATE SET enabled=true,speed_enabled=true,speed_interval_sec=60") + for (const id of [probeId, secondProbeId]) await owner.query("INSERT INTO public.uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,dst_interface,protocol,direction,duration_sec,enabled,last_run_at) VALUES($1,$2,$3,$4,$5,'udp','receive',3,true,now()+interval '1 day') ON CONFLICT(id) DO UPDATE SET src_server_id=$2,dst_server_id=$3,src_interface=$4,dst_interface=$5,protocol='udp',direction='receive',duration_sec=3,enabled=true,last_run_at=now()+interval '1 day'", [id, sourceId, destinationId, interfaces ? " gre-source " : "", interfaces ? " gre-destination " : ""]) + await owner.query("UPDATE public.servers SET enabled=true,verify_ssl=false WHERE id=ANY($1::bigint[])", [[sourceId, destinationId]]) + await writeFile(keyPath, key); await writeFile(generationPath, generation); process.env.MCP_STATE_DIR = directory + } + const rotateEpoch = async () => { + const before = (await owner.query("SELECT routing_endpoint_epoch::text AS epoch FROM public.servers WHERE id=$1", [sourceId])).rows[0].epoch + const after = (await owner.query("UPDATE public.servers SET verify_ssl=NOT verify_ssl WHERE id=$1 RETURNING routing_endpoint_epoch::text AS epoch", [sourceId])).rows[0].epoch + assert.notEqual(after, before) + } + const revokeGeneration = () => { scheduler.stopScheduler(); currentGeneration++ } + // The only callback seam is a fixture observer around an actual SQL result, + // not an authority/executor callback or a fabricated automatic capability. + async function run(work: () => Promise) { + resumeSchedulerJobs(); assert.equal(scheduler.isAutomaticSchedulerGenerationCurrent(currentGeneration), true) + assert.equal(entry, undefined); let entered = false + const initialEntries = schedulerEntries + entry = async () => { entered = true; await work() } + try { await root.runAutomaticSchedulerTicket(root.createAutomaticSchedulerTicket("uptime_speed", currentGeneration)); assert(entered); assert.equal(schedulerEntries, initialEntries + 1) } + finally { entry = undefined; await pauseSchedulerJobsAndWait(); resumeSchedulerJobs() } + } + try { + for (const router of routers) await new Promise((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) }) + const ids = [] + for (let index = 0; index < 2; index++) ids.push(Number((await owner.query("INSERT INTO public.servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES($1,'127.0.0.1',$2,false,false,true,$3,$4) RETURNING id", ["owned-scheduled-speed-" + index, (routers[index].address() as { port: number }).port, users[index], passwords[index]])).rows[0].id)) + ;[sourceId, destinationId] = ids; await restore(true) + const baselineHistory = await history() + pool.query = (function(this: typeof pool, ...args: unknown[]) { + const config = args[0] as string | { text: string; query_timeout?: number }, text = typeof config === "string" ? config : config.text + return (async () => { + const credentials = text.startsWith("/* scheduled-speed-original:credentials */") + if (credentials) { credentialQueries++; assert(typeof config !== "string" && config.query_timeout! > 0); const hook = beforeCredentials; beforeCredentials = undefined; await hook?.() } + const result = await Reflect.apply(actualQuery, this, args) + if (credentials) credentialRows += result.rows.length + if (text.startsWith("/* scheduled-speed-original:metadata */")) metadataQueries++ + if (entry && text.includes('from "uptime_settings"') && !text.startsWith("/*")) { + const work = entry; entry = undefined; schedulerEntries++ + assert.equal(scheduler.isJobRunning("uptime_speed"), false) + try { await work() } finally { await pauseSchedulerJobsAndWait() } + assert.equal(scheduler.isJobRunning("uptime_speed"), false) + } + return result // Same actual pg result, never replacement rows/metadata. + })() + }) as typeof pool.query + const outsideCredentials = credentialQueries + await assert.rejects(provider.capture(probeId, scope())); await assert.rejects(provider.capture({ job: "uptime_speed", probeId } as never, scope())) + assert.equal(credentialQueries, outsideCredentials) + await run(async () => { + const jobScope = scope(), token = await provider.capture(probeId, jobScope); saved = token + const intent = await provider.readOriginal(token, jobScope), context = await provider.readSpeedContext(token, jobScope), lifetime = await provider.readLifetime(token, jobScope) + const genuine = await root.inspectAutomaticSchedulerAuthority(root.captureAutomaticSchedulerAuthority(jobScope)) + assert.equal(genuine.job, "uptime_speed"); assert.equal(genuine.generation, currentGeneration) + assert.equal(intent.callerKind, "scheduled-speed"); assert.equal(intent.socketTimeoutMs, 30000); assert.equal(intent.expiresAt, jobScope.deadlineAt) + assert.deepEqual(intent.nativeBody, { address: "198.51.100.2", user: users[1], password: passwords[1], protocol: "udp", direction: "receive", duration: "3s" }) + assert.equal(context.srcAddress, "198.51.100.1"); assert.equal(context.dstAddress, "198.51.100.2"); assert.deepEqual(getOrder, [1, 0]) + assert(lifetime.monotonicDeadline <= genuine.monotonicDeadline); assert.equal(lifetime.signal.aborted, false) + savedSignal = lifetime.signal + assert.equal((await provider.readPeer(token, { kind: "server", serverId: sourceId }, jobScope)).endpoint.password, passwords[0]) + const beforeCopies = credentialQueries + for (const fake of [{}, { ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(provider.readOriginal(fake, jobScope)) + await assert.rejects(createScheduledSpeedBandwidthOriginalProvider().readOriginal(token, jobScope)); assert.equal(credentialQueries, beforeCopies) + const before = credentialQueries + await assert.rejects(provider.capture(probeId, jobScope)); await assert.rejects(createScheduledSpeedBandwidthOriginalProvider().capture(probeId, jobScope)) + await assert.rejects(provider.readPeer(token, { kind: "server", serverId: 9999999 }, jobScope)); assert.equal(credentialQueries, before) + const second = await provider.capture(secondProbeId, jobScope) + assert.notEqual((await provider.readOriginal(second, jobScope)).operationId, intent.operationId) + await assert.rejects(provider.capture("third", { deadlineAt: jobScope.deadlineAt + 1 })) + }) + assert.equal(savedSignal?.aborted, true); await assert.rejects(provider.readOriginal(saved, scope())) + checks.push("genuine actual fixed uptime_speed ticket/executor/root ALS enters through real settings SELECT; exact body/address/30s and original mono expiry, all eligible probes despite future last_run_at, cross-factory one-probe issuance and fake/copy/foreign refusal") + await restore(false) + for (const fault of ["settings-missing", "settings-disabled", "probe-missing", "probe-disabled", "endpoint-disabled", "key-missing", "generation-missing"] as const) { + await run(async () => { + if (fault === "settings-missing") await owner.query("DELETE FROM public.uptime_settings WHERE id=1") + if (fault === "settings-disabled") await owner.query("UPDATE public.uptime_settings SET speed_enabled=false WHERE id=1") + if (fault === "probe-missing") await owner.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) + if (fault === "probe-disabled") await owner.query("UPDATE public.uptime_speed_probes SET enabled=false WHERE id=$1", [probeId]) + if (fault === "endpoint-disabled") await owner.query("UPDATE public.servers SET enabled=false WHERE id=$1", [sourceId]) + if (fault === "key-missing") await unlink(keyPath) + if (fault === "generation-missing") await unlink(generationPath) + const before = credentialQueries, beforeGets = gets + await assert.rejects(provider.capture(probeId, scope())); assert.equal(credentialQueries, before); assert.equal(gets, beforeGets) + if (fault === "key-missing") await assert.rejects(readFile(keyPath)); if (fault === "generation-missing") await assert.rejects(readFile(generationPath)) + }); await restore(false) + } + checks.push("actual missing/disabled settings/probe/endpoint and missing existing key/generation refuse before credential projection or native GET; no default settings/key creation") + for (const fault of ["settings", "probe", "epoch"] as const) { + await run(async () => { + const before = credentialRows, beforeGets = gets + beforeCredentials = async () => { if (fault === "settings") await owner.query("UPDATE public.uptime_settings SET speed_enabled=false WHERE id=1"); if (fault === "probe") await owner.query("UPDATE public.uptime_speed_probes SET duration_sec=4 WHERE id=$1", [probeId]); if (fault === "epoch") await rotateEpoch() } + await assert.rejects(provider.capture(probeId, scope())); assert.equal(credentialRows, before); assert.equal(gets, beforeGets) + }); await restore(false) + } + checks.push("actual SQL same-snapshot credential predicates disclose no payload on settings/probe/trigger-generated endpoint epoch drift at projection boundary") + for (const fault of ["settings", "probe", "epoch", "key", "generation", "directory", "scheduler-revoke", "abort"] as const) { + await run(async () => { + const controller = new AbortController(), jobScope = scope(30000, controller.signal), token = await provider.capture(probeId, jobScope), before = credentialQueries, beforeGets = gets + if (fault === "settings") await owner.query("UPDATE public.uptime_settings SET speed_interval_sec=61 WHERE id=1") + if (fault === "probe") await owner.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) + if (fault === "epoch") await rotateEpoch() + if (fault === "key") await writeFile(keyPath, randomBytes(32)) + if (fault === "generation") await writeFile(generationPath, randomBytes(32)) + if (fault === "directory") process.env.MCP_STATE_DIR = join(directory, "foreign") + if (fault === "scheduler-revoke") revokeGeneration() + if (fault === "abort") controller.abort() + await assert.rejects(provider.readOriginal(token, jobScope)); assert.equal(credentialQueries, before); assert.equal(gets, beforeGets) + }); await restore(false) + } + checks.push("real current settings/probe/epoch/key/generation/directory changes, actual scheduler stop generation revocation and original signal cancellation reject retained private original before credentials") + await owner.query("UPDATE public.uptime_speed_probes SET protocol='tcp',direction='transmit',duration_sec=0 WHERE id=$1", [probeId]) + await run(async () => { const token = await provider.capture(probeId, scope()), intent = await provider.readOriginal(token, scope()); assert.equal(intent.nativeBody.protocol, "tcp"); assert.equal(intent.nativeBody.direction, "transmit"); assert.equal(intent.nativeBody.duration, "10s"); assert.equal(intent.nativeBody.address, "127.0.0.1") }) + await restore(false); await owner.query("UPDATE public.uptime_speed_probes SET duration_sec=-5 WHERE id=$1", [probeId]) + await run(async () => { const token = await provider.capture(probeId, scope()); assert.equal((await provider.readOriginal(token, scope())).nativeBody.duration, "3s") }) + checks.push("unchanged real SQL protocol/direction and zero/negative duration normalization: tcp/transmit/10s and minimum3s, host fallback when no interface") + await restore(true); held = true; resetHeld(); const beforePending = gets + await run(async () => { + pendingCapture = provider.capture(probeId, scope()); pendingCapture.catch(() => {}) + await bounded(started, "Actual scheduled GET did not reach held TCP responder") + // Return without awaiting capture: actual executor sees genuine pause, + // exits and retires its private original signal in root's real finally. + }) + await bounded(closed, "Genuine scheduler completion did not close native TCP") + await assert.rejects(bounded(pendingCapture!, "Capture remained pending after actual job completion")) + assert.equal(gets, beforePending + 1); held = false; releaseHeld() + checks.push("actual fixed execution completion aborts genuine private authority signal and closes a pending native TCP GET without second GET, POST, history or replay") + assert.deepEqual(await history(), baselineHistory); assert.equal(posts, 0); assert.equal(protocolErrors, 0) + return { checks, actualSql: true, actualSchedulerRoot: true, actualFixedSchedulerExecutor: true, moduleMocks: false, actualReadOnlyNativeGets: gets, actualNativePosts: posts, + metadataQueries, credentialQueries, credentialPayloadRows: credentialRows, genuineSchedulerEntries: schedulerEntries, actualPendingTcpClosed: true, + historyUnchanged: true, automaticReplay: false, realChrCalls: 0, runtimeCallerActivation: false } + } finally { + entry = undefined; beforeCredentials = undefined; held = false; releaseHeld(); pool.query = actualQuery; process.env.MCP_STATE_DIR = directory + try { + scheduler.stopScheduler(); await pauseSchedulerJobsAndWait(); resumeSchedulerJobs() + for (const router of routers) { router.closeAllConnections(); if (router.listening) await new Promise((yes, no) => router.close(error => error ? no(error) : yes())) } + if (pendingCapture) await bounded(pendingCapture.catch(() => {}), "Pending original capture cleanup did not settle").catch(() => {}) + } finally { owner.release(true) } + } +} diff --git a/backend/src/services/bandwidth-scheduled-speed-original.sql.test.ts b/backend/src/services/bandwidth-scheduled-speed-original.sql.test.ts new file mode 100644 index 00000000..55f6e010 --- /dev/null +++ b/backend/src/services/bandwidth-scheduled-speed-original.sql.test.ts @@ -0,0 +1,15 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +assert.equal(process.env.MMAPP_OWN_SCHEDULED_SPEED_ORIGINAL, "true") +await prepareOwnedBandwidthState() +const { pool, closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + const { acceptScheduledSpeedBandwidthOriginalSql } = await import("./bandwidth-scheduled-speed-original.sql-helper.js") + console.log("Owned scheduled speed original SQL/TCP:", JSON.stringify(await acceptScheduledSpeedBandwidthOriginalSql())) +} finally { await closePool() } diff --git a/backend/src/services/bandwidth-scheduled-speed-original.test.ts b/backend/src/services/bandwidth-scheduled-speed-original.test.ts new file mode 100644 index 00000000..a259122b --- /dev/null +++ b/backend/src/services/bandwidth-scheduled-speed-original.test.ts @@ -0,0 +1,269 @@ +/** Actual automatic-root source/ALS/AsyncResource, fixed scheduler fixture and + * real existing key files. SQL/native and host receipt boundary are synthetic; + * genuine host receipts are checked by the full workflow acceptance fixture. */ +import assert from "node:assert/strict" +import { readFile, mkdtemp, realpath, writeFile, unlink, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, dirname, basename } from "node:path" +import { mock } from "node:test" +import { randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import type { BandwidthPeerRef } from "./bandwidth-peer-reference.js" + +process.env.NODE_ENV = "test" +const temporaryParent = await realpath(tmpdir()), directory = await mkdtemp(join(temporaryParent, "mm-scheduled-speed-original-")) +process.env.MCP_STATE_DIR = directory +const realNow = Date.now +const state = { generation: 0, paused: false, work: async (_job: string) => {} } +mock.module("./scheduler.js", { namedExports: { + isAutomaticSchedulerGenerationCurrent: (generation: number) => generation === state.generation && !state.paused, + executeAutomaticSchedulerJob: async (job: string) => { const work = state.work; await work(job) }, +} }) +// Explicit synthetic completion boundary, separate from actual host acceptance. +const completionReceipts = new WeakMap() +mock.module("./bandwidth-peer-execution-host.js", { namedExports: { + readBandwidthPeerCompletion: (result: unknown, token: unknown) => { + const value = result && typeof result === "object" ? completionReceipts.get(result) : undefined + if (!value || value.token !== token) throw Error("Synthetic completion refused") + return value.intent + }, +} }) +const complete = (token: unknown, intent: unknown) => { const result = Object.freeze({ outcome: "complete" }); completionReceipts.set(result, { token, intent }); return result } +const automatic = await import("./scheduler-automatic-root.js") +const { MikrotikClient } = await import("./mikrotik.js"), { pool, closePool } = await import("../db/index.js") +automatic.initializeAutomaticSchedulerRoot() +const key = Buffer.alloc(32, 47), generation = Buffer.alloc(32, 53), keyPath = join(directory, "encryption-key"), generationPath = join(directory, "access-generation") +await writeFile(keyPath, key); await writeFile(generationPath, generation) +let settings: Record | undefined, probes = new Map>(), endpoints: Array> = [] +let credentialQueries = 0, credentialPayloadRows = 0, nativeReads = 0, nativePosts = 0, metadataQueries = 0, settingsQueries = 0, nativeOrder: number[] = [] +let sqlHook: ((text: string) => void) | undefined, nativeHook: (() => void) | undefined, nativeFailure = false, pendingNative = false, transportAborted = false +let nativeStarted: (() => void) | undefined +let historyOccupied = false, historyQueries = 0 +const probeProjection = (row: Record) => Object.fromEntries(["id", "enabled", "srcServerId", "dstServerId", "srcInterface", "dstInterface", "protocol", "direction", "durationSec"].map(key => [key, row[key]])) +const sql = { async query(config: { text: string; values: unknown[]; query_timeout: number }) { + const { text, values } = config + assert(config.query_timeout > 0 && config.query_timeout <= 5000); assert(!/\b(?:INSERT|UPDATE|DELETE|CREATE|ALTER)\b/.test(text)) + sqlHook?.(text) + if (text.startsWith("/* bandwidth-speed-run:history */")) { historyQueries++; const rows = historyOccupied ? [{ id: values[0] }] : []; return { command: "SELECT", rowCount: rows.length, rows } } + if (text.startsWith("/* scheduled-speed-original:settings */")) { settingsQueries++; return { rows: settings ? [structuredClone(settings)] : [] } } + if (text.startsWith("/* scheduled-speed-original:probe */")) { const row = probes.get(String(values[0])); return { rows: row?.enabled === true ? [structuredClone(probeProjection(row))] : [] } } + if (text.startsWith("/* scheduled-speed-original:metadata */")) { metadataQueries++; return { rows: endpoints.filter(row => (values[0] as number[]).includes(Number(row.id))).map(row => ({ id: row.id, enabled: row.enabled, routingEndpointEpoch: row.routingEndpointEpoch })) } } + const afterCompletion = text.startsWith("/* scheduled-speed-original:completion-credentials */") + assert(afterCompletion || text.startsWith("/* scheduled-speed-original:credentials */")); assert(text.includes("coalesce(s.speed_enabled,s.enabled)")); assert.equal(text.includes("public.uptime_speed_probes"), !afterCompletion); assert(text.includes("WHERE a.complete")) + credentialQueries++ + const expected = JSON.parse(String(values[0])) as Array<{ id: number; epoch: string }>, originalProbe = afterCompletion ? undefined : JSON.parse(String(values[1])), originalSettings = JSON.parse(String(values[afterCompletion ? 1 : 2])) + const row = probes.get(String(values[3])), matches = expected.map(value => endpoints.find(endpoint => endpoint.id === value.id && endpoint.enabled === true && endpoint.routingEndpointEpoch === value.epoch)) + const complete = settings && Boolean(settings.speedEnabled ?? settings.enabled) && JSON.stringify(settings) === JSON.stringify(originalSettings) + && (afterCompletion || row?.enabled === true && JSON.stringify(probeProjection(row)) === JSON.stringify(originalProbe)) && matches.every(Boolean) + const rows = complete ? matches.map(endpoint => ({ id: endpoint!.id, endpoint: structuredClone(endpoint), overflow: false })) : [] + credentialPayloadRows += rows.length; return { rows } +} } +const originalQuery = pool.query, originalConnect = pool.connect +pool.query = sql.query as never; pool.connect = (() => { throw Error("No physical PostgreSQL checkout permitted") }) as never +const api = await import("./bandwidth-scheduled-speed-original.js") +const { createBandwidthSpeedRunIdentity } = await import("./bandwidth-speed-run-identity.js") +const assertProvider: (value: unknown) => void = api.assertScheduledSpeedBandwidthOriginalProvider +const originalGet = MikrotikClient.prototype.get, originalPost = MikrotikClient.prototype.post +MikrotikClient.prototype.get = (async function(this: InstanceType, path: string, timeout: number, signal: AbortSignal, _budget: unknown, guard: () => Promise) { + assert.equal(path, "/ip/address"); assert(timeout > 0 && timeout <= 10000); await guard(); signal.throwIfAborted() + const id = Number((this as unknown as { serverId: number }).serverId); nativeReads++; nativeOrder.push(id); nativeHook?.() + if (pendingNative) return new Promise((_yes, no) => { signal.addEventListener("abort", () => { transportAborted = true; no(Error("Original job lifetime cancelled native GET")) }, { once: true }); nativeStarted?.() }) + if (nativeFailure) throw Error("Synthetic read-only GET failed") + return id === 101 ? [{ interface: "gre-source", address: "198.51.100.1/30" }] : [ + { interface: "gre-destination", address: "198.51.100.99/30", dynamic: "true" }, { interface: "gre-destination", address: "198.51.100.77/30", disabled: "true" }, + { interface: "gre-destination", address: "198.51.100.66/30", invalid: "true" }, { interface: "gre-destination", address: "198.51.100.2/30" }, + ] +}) as never +MikrotikClient.prototype.post = (async () => { nativePosts++; throw Error("No bandwidth/ping POST allowed") }) as never +const provider = api.createScheduledSpeedBandwidthOriginalProvider(), scope = (milliseconds = 10000, signal?: AbortSignal) => ({ deadlineAt: Date.now() + milliseconds, ...(signal ? { signal } : {}) }) +const run = async (work: (job: string) => Promise, job: Parameters[0] = "uptime_speed") => { state.work = work; return automatic.runAutomaticSchedulerTicket(automatic.createAutomaticSchedulerTicket(job, state.generation)) } +function fixture(interfaces = true) { + historyOccupied = false + state.generation = 0; state.paused = false; Date.now = realNow; sqlHook = undefined; nativeHook = undefined; nativeFailure = false; pendingNative = false; transportAborted = false; nativeOrder = [] + settings = { enabled: true, speedEnabled: true, speedIntervalSec: 60 } + probes = new Map(["probe-a", "probe-b"].map(id => [id, { id, enabled: true, srcServerId: 101, dstServerId: 102, + srcInterface: interfaces ? " gre-source " : "", dstInterface: interfaces ? " gre-destination " : "", protocol: "udp", direction: "receive", durationSec: 3 }])) + endpoints = [101, 102].map(id => ({ id, enabled: true, host: "192.0.2." + (id - 100), port: 443, username: "fixture-user-" + id, password: "fixture-private-" + id, useSsl: true, verifySsl: true, routingEndpointEpoch: randomUUID() })) +} +const counter = () => ({ credentials: credentialQueries, payloads: credentialPayloadRows, native: nativeReads }) +let saved: unknown +let savedLifetime: Readonly<{ expiresAt: number; monotonicDeadline: number; signal: AbortSignal }> | undefined +try { + assertProvider(provider); assert.throws(() => assertProvider({ ...provider })) + fixture(); const beforeOutside = counter() + await assert.rejects(provider.capture("probe-a", scope())); await assert.rejects(provider.capture({ id: "probe-a", job: "uptime_speed" } as never, scope())) + assert.deepEqual(counter(), beforeOutside) + await run(async () => { await assert.rejects(provider.capture("probe-a", scope())) }, "backups"); assert.deepEqual(counter(), beforeOutside) + fixture() + await run(async () => { + const jobScope = scope(10000, new AbortController().signal), token = await provider.capture("probe-a", jobScope); saved = token + const intent = await provider.readOriginal(token, scope()), context = await provider.readSpeedContext(token, scope()) + assert.equal(intent.callerKind, "scheduled-speed"); assert.equal(intent.socketTimeoutMs, 30000); assert.equal(intent.expiresAt, jobScope.deadlineAt) + assert.deepEqual(intent.nativeBody, { address: "198.51.100.2", user: "fixture-user-102", password: "fixture-private-102", protocol: "udp", direction: "receive", duration: "3s" }) + assert.deepEqual(nativeOrder, [102, 101]); assert.equal(context.command.srcInterface, "gre-source"); assert.equal(context.command.dstInterface, "gre-destination") + assert.equal(context.srcAddress, "198.51.100.1"); assert.equal(context.dstAddress, "198.51.100.2"); assert.equal(context.srcInterfaceAddress, context.srcAddress); assert(!("password" in context)) + assert(Object.isFrozen(intent)); assert(Object.isFrozen(intent.nativeBody)); assert(Object.isFrozen(context.command)) + const runIdentity = createBandwidthSpeedRunIdentity(context.command.runId) + assert.equal(intent.attemptId, runIdentity.attemptId); assert.equal(intent.operationId, runIdentity.operationId) + assert.equal((intent.request.command as { runId: string }).runId, runIdentity.runId) + savedLifetime = await provider.readLifetime(token, scope()) + const rootAuthority = await automatic.inspectAutomaticSchedulerAuthority(automatic.captureAutomaticSchedulerAuthority(jobScope)) + assert.equal(savedLifetime.expiresAt, jobScope.deadlineAt); assert(savedLifetime.monotonicDeadline <= rootAuthority.monotonicDeadline) + assert.equal(savedLifetime.signal.aborted, false); assert(Object.isFrozen(savedLifetime)) + assert.equal((await provider.readLifetime(token, scope())).signal, savedLifetime.signal) + for (const fake of [{}, { ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(provider.readOriginal(fake, scope())) + await assert.rejects(api.createScheduledSpeedBandwidthOriginalProvider().readOriginal(token, scope())) + for (const peer of [{ kind: "server", serverId: 103 }, { kind: "external", enrollmentId: randomUUID() }]) await assert.rejects(provider.readPeer(token, peer as BandwidthPeerRef, scope())) + assert.equal((await provider.readPeer(token, { kind: "server", serverId: 101 }, scope())).endpoint.password, "fixture-private-101") + const before = counter() + await assert.rejects(provider.capture("probe-a", jobScope)); await assert.rejects(api.createScheduledSpeedBandwidthOriginalProvider().capture("probe-a", jobScope)); assert.deepEqual(counter(), before) + const other = await provider.capture("probe-b", jobScope), second = await provider.readOriginal(other, scope()) + assert.notEqual(second.operationId, intent.operationId); assert.notEqual(second.attemptId, intent.attemptId) + await assert.rejects(provider.capture("other", { ...jobScope, deadlineAt: jobScope.deadlineAt + 1 })) + }) + assert.equal(savedLifetime?.signal.aborted, true) + await assert.rejects(provider.readOriginal(saved, scope())) + fixture(); historyOccupied = true + const beforeLegacy = counter(), beforeHistory = historyQueries + await run(async () => { await assert.rejects(provider.capture("probe-a", scope())) }) + assert.equal(historyQueries, beforeHistory + 1); assert.deepEqual(counter(), beforeLegacy) + for (const fault of ["settings-missing", "disabled-settings", "probe-missing", "probe-disabled", "same-node", "source-disabled", "epoch-missing", "key-missing", "generation-missing"] as const) { + fixture(); const before = counter() + if (fault === "settings-missing") settings = undefined + if (fault === "disabled-settings") settings!.speedEnabled = false + if (fault === "probe-missing") probes.delete("probe-a") + if (fault === "probe-disabled") probes.get("probe-a")!.enabled = false + if (fault === "same-node") probes.get("probe-a")!.dstServerId = 101 + if (fault === "source-disabled") endpoints[0].enabled = false + if (fault === "epoch-missing") delete endpoints[0].routingEndpointEpoch + if (fault === "key-missing") await unlink(keyPath) + if (fault === "generation-missing") await unlink(generationPath) + try { await run(async () => { await assert.rejects(provider.capture("probe-a", scope()), fault) }); assert.deepEqual(counter(), before, fault) } + finally { if (fault === "key-missing") await assert.rejects(readFile(keyPath)); if (fault === "generation-missing") await assert.rejects(readFile(generationPath)); await writeFile(keyPath, key); await writeFile(generationPath, generation) } + } + for (const fault of ["settings", "probe", "epoch"] as const) { + fixture(); const before = counter() + sqlHook = text => { if (text.startsWith("/* scheduled-speed-original:credentials */")) { if (fault === "settings") settings!.speedEnabled = false; if (fault === "probe") probes.get("probe-a")!.durationSec = 4; if (fault === "epoch") endpoints[1].routingEndpointEpoch = randomUUID() } } + await run(async () => { await assert.rejects(provider.capture("probe-a", scope())) }) + assert.equal(nativeReads, before.native); assert.equal(credentialPayloadRows, before.payloads) + } + for (const fault of ["settings", "probe", "enabled", "epoch", "credentials", "source-key", "source-generation", "directory", "scheduler-generation", "paused", "abort"] as const) { + fixture(false) + await run(async () => { + const owner = new AbortController(), jobScope = scope(10000, owner.signal), token = await provider.capture("probe-a", jobScope), before = counter() + if (fault === "settings") settings!.speedIntervalSec = 61 + if (fault === "probe") probes.get("probe-a")!.direction = "both" + if (fault === "enabled") endpoints[0].enabled = false + if (fault === "epoch") endpoints[0].routingEndpointEpoch = randomUUID() + if (fault === "credentials") endpoints[1].password = "changed" + if (fault === "source-key") await writeFile(keyPath, Buffer.alloc(32, 61)) + if (fault === "source-generation") await writeFile(generationPath, Buffer.alloc(32, 67)) + if (fault === "directory") process.env.MCP_STATE_DIR = join(directory, "foreign") + if (fault === "scheduler-generation") state.generation++ + if (fault === "paused") state.paused = true + if (fault === "abort") owner.abort() + await assert.rejects(provider.readOriginal(token, scope())) + if (fault !== "credentials") assert.equal(credentialQueries, before.credentials, fault) + assert.equal(nativeReads, before.native) + }) + process.env.MCP_STATE_DIR = directory; await writeFile(keyPath, key); await writeFile(generationPath, generation) + } + for (const selection of ["edited", "deleted", "disabled"] as const) { + fixture(false) + await run(async () => { + const token = await provider.capture("probe-a", scope()), intent = await provider.readOriginal(token, scope()), result = complete(token, intent), before = counter() + for (const forged of [{ outcome: "complete" }, { ...result }, JSON.parse(JSON.stringify(result))]) + await assert.rejects(provider.readPeerAfterCompletion(token, forged, { kind: "server", serverId: 101 }, scope())) + await assert.rejects(provider.readPeerAfterCompletion({ ...token }, result, { kind: "server", serverId: 101 }, scope())) + await assert.rejects(api.createScheduledSpeedBandwidthOriginalProvider().readPeerAfterCompletion(token, result, { kind: "server", serverId: 101 }, scope())) + await assert.rejects(provider.readPeerAfterCompletion(token, result, { kind: "server", serverId: 999 }, scope())) + await assert.rejects(provider.readPeerAfterCompletion(token, complete({}, intent), { kind: "server", serverId: 101 }, scope())) + await assert.rejects(provider.readPeerAfterCompletion(token, complete(token, { ...intent, operationId: "wrong" }), { kind: "server", serverId: 101 }, scope())) + assert.deepEqual(counter(), before) + if (selection === "edited") probes.get("probe-a")!.durationSec = 88 + if (selection === "deleted") probes.delete("probe-a") + if (selection === "disabled") probes.get("probe-a")!.enabled = false + await assert.rejects(provider.readOriginal(token, scope())) + await assert.rejects(provider.readPeer(token, { kind: "server", serverId: 101 }, scope())) + for (const id of [101, 102]) assert.equal((await provider.readPeerAfterCompletion(token, result, { kind: "server", serverId: id }, scope())).endpoint.password, "fixture-private-" + id) + assert.equal(nativeReads, before.native) + }) + } + for (const fault of ["settings", "enabled", "epoch", "credentials", "source-key", "source-generation", "directory", "scheduler-generation", "paused", "abort", "expiry"] as const) { + fixture(false) + await run(async () => { + const owner = new AbortController(), token = await provider.capture("probe-a", scope(10000, owner.signal)), result = complete(token, await provider.readOriginal(token, scope())), before = counter() + probes.delete("probe-a") + if (fault === "settings") settings!.speedIntervalSec = 61 + if (fault === "enabled") endpoints[0].enabled = false + if (fault === "epoch") endpoints[0].routingEndpointEpoch = randomUUID() + if (fault === "credentials") endpoints[1].password = "changed" + if (fault === "source-key") await writeFile(keyPath, Buffer.alloc(32, 61)) + if (fault === "source-generation") await writeFile(generationPath, Buffer.alloc(32, 67)) + if (fault === "directory") process.env.MCP_STATE_DIR = join(directory, "foreign") + if (fault === "scheduler-generation") state.generation++ + if (fault === "paused") state.paused = true + if (fault === "abort") owner.abort() + if (fault === "expiry") Date.now = () => realNow() + 20000 + await assert.rejects(provider.readPeerAfterCompletion(token, result, { kind: "server", serverId: 101 }, scope())) + if (fault !== "credentials") assert.equal(credentialQueries, before.credentials, fault) + assert.equal(nativeReads, before.native) + }) + Date.now = realNow; process.env.MCP_STATE_DIR = directory; await writeFile(keyPath, key); await writeFile(generationPath, generation) + } + for (const fault of ["settings", "epoch"] as const) { + fixture(false) + await run(async () => { + const token = await provider.capture("probe-a", scope()), result = complete(token, await provider.readOriginal(token, scope())), before = counter() + probes.delete("probe-a") + sqlHook = text => { if (text.startsWith("/* scheduled-speed-original:completion-credentials */")) { if (fault === "settings") settings!.speedEnabled = false; else endpoints[1].routingEndpointEpoch = randomUUID() } } + await assert.rejects(provider.readPeerAfterCompletion(token, result, { kind: "server", serverId: 101 }, scope())) + assert.equal(credentialPayloadRows, before.payloads); assert.equal(nativeReads, before.native) + }) + } + fixture(false); settings = { enabled: false, speedEnabled: true, speedIntervalSec: 0 } + probes.get("probe-a")!.protocol = "legacy"; probes.get("probe-a")!.direction = "legacy"; probes.get("probe-a")!.durationSec = 0 + await run(async () => { + const token = await provider.capture("probe-a", scope()), intent = await provider.readOriginal(token, scope()), context = await provider.readSpeedContext(token, scope()) + assert.equal(intent.nativeBody.protocol, "tcp"); assert.equal(intent.nativeBody.direction, "both"); assert.equal(intent.nativeBody.duration, "10s"); assert.equal(intent.nativeBody.address, endpoints[1].host) + assert.equal(context.srcAddress, endpoints[0].host); assert.equal(context.srcInterfaceAddress, null); assert.equal(nativeOrder.length, 0) + }) + fixture(false); settings!.speedEnabled = null + await run(async () => { await provider.capture("probe-a", scope()) }) + fixture(false); probes.get("probe-a")!.durationSec = -4; probes.get("probe-a")!.direction = "transmit"; probes.get("probe-a")!.lastRunAt = "2999-01-01T00:00:00Z" + await run(async () => { + const token = await provider.capture("probe-a", scope()), intent = await provider.readOriginal(token, scope()) + assert.equal(intent.nativeBody.duration, "3s"); assert.equal(intent.nativeBody.direction, "transmit") + }) + fixture(); nativeHook = () => { state.generation++ } + await run(async () => { await assert.rejects(provider.capture("probe-a", scope())) }) + fixture(); probes.get("probe-a")!.dstInterface = ""; nativeFailure = true + await run(async () => { const token = await provider.capture("probe-a", scope()), context = await provider.readSpeedContext(token, scope()); assert.equal(context.srcAddress, ""); assert.equal(context.srcInterfaceAddress, null); assert.equal(context.address, endpoints[1].host) }) + fixture(); pendingNative = true + let pending!: Promise + await run(async () => { + const started = new Promise(yes => { nativeStarted = yes }) + pending = provider.capture("probe-a", scope()); void pending.catch(() => {}) + await Promise.race([started, pending.then(() => { throw Error("Pending GET unexpectedly completed") })]) + // Actual automatic-root completion retires its own private controller. + }) + await assert.rejects(pending); assert.equal(transportAborted, true) + fixture(); pendingNative = true + await run(async () => { + const jobScope = scope(1000), original = automatic.captureAutomaticSchedulerAuthority(jobScope), snapshot = await automatic.inspectAutomaticSchedulerAuthority(original) + Date.now = () => realNow() - 100000 + const started = new Promise(yes => { nativeStarted = yes }), pending = provider.capture("probe-a", jobScope) + void pending.catch(() => {}) + await Promise.race([started, pending.then(() => { throw Error("Pending GET unexpectedly completed") })]) + let timer: ReturnType | undefined + try { await Promise.race([assert.rejects(pending), new Promise((_yes, no) => { timer = setTimeout(() => no(Error("Original monotonic job deadline was renewed after wall rollback")), 3000) })]) } + finally { clearTimeout(timer) } + assert(snapshot.monotonicDeadline < performance.now() + 1000); assert.equal(transportAborted, true) + }) + assert.equal(nativePosts, 0); assert(nativeReads > 0); assert(metadataQueries > 0); assert(settingsQueries > 0) + console.log("Scheduled speed original: actual automatic-root/ALS tickets, fixed SQL selection and normalization, all eligible probes without due logic, cross-factory one-probe issuance, current settings/probe/key/generation/epoch gates, original mono/signal cancellation; synthetic SQL/native/completion boundary, strict predispatch and completion-bound probe drift, zero bandwidth/history activation") +} finally { + Date.now = realNow; pool.query = originalQuery; pool.connect = originalConnect + MikrotikClient.prototype.get = originalGet; MikrotikClient.prototype.post = originalPost; await closePool(); mock.restoreAll() + process.env.MCP_STATE_DIR = directory; assert.equal(dirname(directory), temporaryParent); assert(basename(directory).startsWith("mm-scheduled-speed-original-")); await rm(directory, { recursive: true, force: true }) +} diff --git a/backend/src/services/bandwidth-scheduled-speed-original.ts b/backend/src/services/bandwidth-scheduled-speed-original.ts new file mode 100644 index 00000000..3a1bd5bd --- /dev/null +++ b/backend/src/services/bandwidth-scheduled-speed-original.ts @@ -0,0 +1,254 @@ +/** Automatic uptime_speed original only. No scheduler refresh, enrollment, + * bandwidth dispatch, due-time policy, history publication or runtime wiring. */ +import { readFile } from "node:fs/promises" +import { join } from "node:path" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import type { QueryConfig } from "pg" +import { z } from "zod" +import { speedTestCommandSchema, type SpeedTestCommand } from "@mmapp/contracts" +import { pool } from "../db/index.js" +import { fingerprint, McpError, mcpStateDirectory, privateFingerprinterExisting } from "../modules/mcp/security.js" +import { captureAutomaticSchedulerAuthority, inspectAutomaticSchedulerAuthority, type AutomaticSchedulerAuthoritySnapshot } from "./scheduler-automatic-root.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { readBandwidthPeerCompletion, type BandwidthPeerExecutionIntent } from "./bandwidth-peer-execution-host.js" +import type { BandwidthPeerSourceSnapshot } from "./bandwidth-peer-observation.js" +import { parseBandwidthPeerRef, type BandwidthPeerRef } from "./bandwidth-peer-reference.js" +import type { RestSpeedBandwidthContext } from "./bandwidth-rest-speed-original.js" +import { MikrotikClient } from "./mikrotik.js" +import { createBandwidthSpeedRunIdentity, BANDWIDTH_SPEED_RUN_HISTORY_SQL, assertBandwidthSpeedRunHistoryAbsent, BandwidthSpeedRunIdUnavailable } from "./bandwidth-speed-run-identity.js" + +declare const scheduledSpeedBrand: unique symbol +export type ScheduledSpeedBandwidthOriginal = Readonly<{ [scheduledSpeedBrand]: true }> +export type ScheduledSpeedBandwidthContext = RestSpeedBandwidthContext +const providers = new WeakSet() +// Shared across provider instances: one genuine execution cannot reissue the +// same probe under another factory. It is not durable restart uniqueness. +const issuedProbes = new WeakMap>() +const deny = (): never => { throw new McpError("PERMISSION_DENIED", "Исходный automatic speed-контекст изменён или недоступен", 403) } +export function assertScheduledSpeedBandwidthOriginalProvider(value: unknown): asserts value is ReturnType { + if (!value || typeof value !== "object" || !providers.has(value)) deny() +} +function freeze(value: T): T { + if (value && typeof value === "object") { Object.values(value).forEach(freeze); Object.freeze(value) } + return value +} +function scopeSnapshot(raw: Readonly): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype || Reflect.ownKeys(raw).length !== Object.keys(raw).length) deny() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Object.keys(fields).some(key => !["deadlineAt", "signal"].includes(key)) || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) deny() + const deadlineAt = fields.deadlineAt?.value, signal = fields.signal?.value + if (typeof deadlineAt !== "number" || !Number.isSafeInteger(deadlineAt) || signal !== undefined && !(signal instanceof AbortSignal)) return deny() + return Object.freeze({ deadlineAt, signal }) +} +const settingsSchema = z.object({ enabled: z.boolean(), speedEnabled: z.boolean().nullable(), speedIntervalSec: z.number().int().min(-2147483648).max(2147483647) }).strict() +const probeSchema = z.object({ id: z.string().min(1).max(1000), enabled: z.literal(true), srcServerId: z.number().int().positive().safe(), dstServerId: z.number().int().positive().safe(), + srcInterface: z.string(), dstInterface: z.string(), protocol: z.string(), direction: z.string(), durationSec: z.number().int().min(-2147483648).max(2147483647) }).strict() + .refine(row => row.srcServerId !== row.dstServerId) +const metadataSchema = z.array(z.object({ id: z.number().int().positive().safe(), enabled: z.literal(true), routingEndpointEpoch: z.string().uuid() }).strict()).length(2) +const endpointSchema = z.object({ id: z.number().int().positive().safe(), enabled: z.literal(true), routingEndpointEpoch: z.string().uuid(), host: z.string().min(1).max(1000), + port: z.number().int().min(1).max(65535), useSsl: z.boolean(), verifySsl: z.boolean(), username: z.string().max(1000), password: z.string().max(65536) }).strict() +type Settings = z.infer +type Probe = z.infer +type Metadata = z.infer +type Endpoint = z.infer +const purpose = "scheduled-speed-bandwidth-original-v1", keyMessage = "scheduled-speed-bandwidth-original-purpose-key-epoch-v1" +const identity = (authority: Readonly) => ({ job: authority.job, generation: authority.generation, executionId: authority.executionId, deadlineAt: authority.deadlineAt, monotonicDeadline: authority.monotonicDeadline }) +async function existingGeneration(directory: string) { + const bytes = await readFile(join(directory, "access-generation")); if (bytes.length !== 32) return deny(); return bytes.toString("hex") +} +/** Exactly the existing tick's normalization, with no per-probe due rule. */ +function normalize(probe: Probe): Readonly { + return freeze(speedTestCommandSchema.parse({ probeId: probe.id, srcServerId: probe.srcServerId, dstServerId: probe.dstServerId, + srcInterface: probe.srcInterface || undefined, dstInterface: probe.dstInterface || undefined, + protocol: probe.protocol === "udp" ? "udp" : "tcp", direction: probe.direction === "transmit" || probe.direction === "receive" ? probe.direction : "both", + durationSec: Math.max(3, probe.durationSec || 10) })) +} +function firstUsableIp(rows: Record[], iface: string): string { + const candidates = rows.filter(row => String(row.interface ?? "").trim() === iface.trim() + && String(row.disabled ?? "false").toLowerCase() !== "true" && String(row.invalid ?? "false").toLowerCase() !== "true" && String(row.address ?? "").trim().includes("/")) + candidates.sort((a, b) => (String(a.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0) - (String(b.dynamic ?? "false").toLowerCase() === "true" ? 1 : 0)) + return candidates[0]?.address ? String(candidates[0].address).split("/")[0]?.trim() ?? "" : "" +} +async function window(input: Readonly, authority: Readonly, originalEnd: number, + work: (scope: Readonly, live: () => void) => Promise): Promise { + const controller = new AbortController() + const scope = Object.freeze({ deadlineAt: Math.min(input.deadlineAt, authority.deadlineAt), signal: AbortSignal.any([controller.signal, authority.signal, ...(input.signal ? [input.signal] : [])]) }) + const end = Math.min(originalEnd, performance.now() + Math.max(0, scope.deadlineAt - Date.now())) + const signals = [...new Set([scope.signal, authority.signal].filter((signal): signal is AbortSignal => !!signal))] + let active = true, rejectStopped!: (error: Error) => void + const live = () => { if (!active || Date.now() >= scope.deadlineAt || performance.now() >= end || signals.some(signal => signal.aborted)) deny() } + const stopped = new Promise((_yes, reject) => { rejectStopped = reject }) + const stop = () => { if (!active) return; active = false; controller.abort(); rejectStopped(new McpError("PERMISSION_DENIED", "Automatic speed-контекст завершён", 403)) } + const timer = setTimeout(stop, Math.max(1, Math.min(scope.deadlineAt - Date.now(), end - performance.now()))) + for (const signal of signals) signal.addEventListener("abort", stop, { once: true }) + try { live(); return await Promise.race([Promise.resolve().then(async () => { live(); const value = await work(scope, live); live(); return value }), stopped]) } + finally { active = false; clearTimeout(timer); for (const signal of signals) signal.removeEventListener("abort", stop); controller.abort() } +} + +/** Fixed source assembly only. Call with the SAME original job deadline/signal + * for every eligible probe in a genuine uptime_speed execution. A probe ID is a + * selector, never authority or a supplied command. Missing settings refuse; the + * read-only provider does not call getSettings' default INSERT path. */ +export function createScheduledSpeedBandwidthOriginalProvider() { + type Entry = { intent: BandwidthPeerExecutionIntent; context: Readonly; peers: ReadonlyMap> + authority: Readonly; end: number; assertCurrent(scope: Readonly, live: () => void): Promise + assertAuthorityCurrent(scope: Readonly, live: () => void): Promise } + const entries = new WeakMap() + const read = async (token: unknown, rawScope: Readonly) => { + const scope = scopeSnapshot(rawScope), entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry) return deny() + await window(scope, entry.authority, entry.end, (current, live) => entry.assertCurrent(current, live)); return entry + } + const provider = Object.freeze({ + async capture(rawProbeId: string, rawScope: Readonly): Promise { + if (typeof rawProbeId !== "string" || !rawProbeId || rawProbeId.length > 1000 || rawProbeId.trim() !== rawProbeId || /[\u0000-\u001f\u007f]/.test(rawProbeId)) return deny() + const probeId = rawProbeId, scope = scopeSnapshot(rawScope), entered = Date.now(), enteredMono = performance.now() + const automatic = captureAutomaticSchedulerAuthority(scope), authority = await inspectAutomaticSchedulerAuthority(automatic) + if (authority.job !== "uptime_speed") return deny() + const issued = issuedProbes.get(automatic) ?? new Set(); if (issued.has(probeId)) return deny(); issued.add(probeId); issuedProbes.set(automatic, issued) + const originalAuthority = freeze(identity(authority)), end = Math.min(authority.monotonicDeadline, enteredMono + Math.max(0, authority.deadlineAt - entered)) + return window(scope, authority, end, async (current, live) => { + const sourceDirectory = mcpStateDirectory(), generation = await existingGeneration(sourceDirectory); live() + const sign = await privateFingerprinterExisting(purpose); live(); const keyProof = sign(keyMessage) + const basic = async (next: Readonly, checkLive: () => void) => { + checkLive(); if (mcpStateDirectory() !== sourceDirectory) deny() + const fresh = await inspectAutomaticSchedulerAuthority(automatic); checkLive() + if (fresh.signal.aborted || !isDeepStrictEqual(identity(fresh), originalAuthority)) deny() + if (await existingGeneration(sourceDirectory) !== generation || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + if ((await privateFingerprinterExisting(purpose))(keyMessage) !== keyProof || mcpStateDirectory() !== sourceDirectory) deny(); checkLive() + const final = await inspectAutomaticSchedulerAuthority(automatic); checkLive() + if (final.signal.aborted || !isDeepStrictEqual(identity(final), originalAuthority) || next.signal?.aborted) deny() + } + const query = async (text: string, values: unknown[], next: Readonly, checkLive: () => void) => { + checkLive(); const config: QueryConfig & { query_timeout: number } = { text, values, query_timeout: Math.max(1, Math.min(5000, next.deadlineAt - Date.now())) } + const result = await pool.query(config); checkLive(); return result + } + const settings = async (next: Readonly, checkLive: () => void): Promise => { + await basic(next, checkLive) + const rows = (await query('/* scheduled-speed-original:settings */ SELECT enabled,speed_enabled AS "speedEnabled",speed_interval_sec AS "speedIntervalSec" FROM public.uptime_settings WHERE id=1 LIMIT 2', [], next, checkLive)).rows + await basic(next, checkLive); if (rows.length !== 1) return deny() + const value = settingsSchema.parse(rows[0]); if (!(value.speedEnabled ?? value.enabled)) deny(); return value + } + const probe = async (next: Readonly, checkLive: () => void): Promise => { + await basic(next, checkLive) + const rows = (await query('/* scheduled-speed-original:probe */ SELECT id,src_server_id AS "srcServerId",dst_server_id AS "dstServerId",src_interface AS "srcInterface",dst_interface AS "dstInterface",protocol,direction,duration_sec AS "durationSec",enabled FROM public.uptime_speed_probes WHERE id=$1 AND enabled=true LIMIT 2', [probeId], next, checkLive)).rows + await basic(next, checkLive); if (rows.length !== 1) return deny() + const value = probeSchema.parse(rows[0]); if (value.id !== probeId) deny(); return value + } + const originalSettings = freeze(await settings(current, live)), originalProbe = freeze(await probe(current, live)) + const run = createBandwidthSpeedRunIdentity(), command = freeze({ ...normalize(originalProbe), runId: run.runId }) + const ids = [command.srcServerId, command.dstServerId].sort((a, b) => a - b) + const metadata = async (next: Readonly, checkLive: () => void): Promise => { + await basic(next, checkLive) + const rows = (await query('/* scheduled-speed-original:metadata */ SELECT id,enabled,routing_endpoint_epoch AS "routingEndpointEpoch" FROM public.servers WHERE id=ANY($1::bigint[]) ORDER BY id LIMIT 3', [ids], next, checkLive)).rows + await basic(next, checkLive); const value = metadataSchema.parse(rows); if (!isDeepStrictEqual(value.map(row => row.id), ids)) deny(); return value + } + const originalMetadata = freeze(await metadata(current, live)) + await basic(current, live) + let history: unknown + try { history = await query(BANDWIDTH_SPEED_RUN_HISTORY_SQL, [run.runId], current, live) } + catch { throw new BandwidthSpeedRunIdUnavailable(run.runId) } + assertBandwidthSpeedRunHistoryAbsent(history, run.runId) + await basic(current, live) + const endpoints = async (next: Readonly, checkLive: () => void): Promise> => { + await basic(next, checkLive) + const result = await query(`/* scheduled-speed-original:credentials */ WITH expected AS MATERIALIZED ( + SELECT * FROM pg_catalog.jsonb_to_recordset($1::jsonb) AS e(id bigint,epoch uuid)), + matched AS MATERIALIZED (SELECT s.id FROM public.servers s JOIN expected e ON e.id=s.id AND e.epoch=s.routing_endpoint_epoch WHERE s.enabled=true), + allowed AS MATERIALIZED (SELECT count(*)=2 + AND EXISTS(SELECT 1 FROM public.uptime_settings s WHERE s.id=1 AND coalesce(s.speed_enabled,s.enabled) AND jsonb_build_object('enabled',s.enabled,'speedEnabled',s.speed_enabled,'speedIntervalSec',s.speed_interval_sec)=$3::jsonb) + AND EXISTS(SELECT 1 FROM public.uptime_speed_probes p WHERE p.id=$4 AND p.enabled=true AND jsonb_build_object('id',p.id,'enabled',p.enabled,'srcServerId',p.src_server_id,'dstServerId',p.dst_server_id, + 'srcInterface',p.src_interface,'dstInterface',p.dst_interface,'protocol',p.protocol,'direction',p.direction,'durationSec',p.duration_sec)=$2::jsonb) AS complete FROM matched), + chosen AS MATERIALIZED (SELECT s.id,jsonb_build_object('id',s.id,'host',s.host,'port',s.port,'username',s.username,'password',s.password, + 'useSsl',s.use_ssl,'verifySsl',s.verify_ssl,'enabled',s.enabled,'routingEndpointEpoch',s.routing_endpoint_epoch) AS endpoint FROM public.servers s JOIN matched m ON m.id=s.id CROSS JOIN allowed a WHERE a.complete), + budget AS (SELECT coalesce(sum(octet_length(endpoint::text)),0) AS bytes FROM chosen) + SELECT id,CASE WHEN budget.bytes<=1048576 THEN endpoint END AS endpoint,budget.bytes>1048576 AS overflow FROM chosen CROSS JOIN budget ORDER BY id LIMIT 3`, + [JSON.stringify(originalMetadata.map(row => ({ id: row.id, epoch: row.routingEndpointEpoch }))), JSON.stringify(originalProbe), JSON.stringify(originalSettings), probeId], next, checkLive) + await basic(next, checkLive); if (result.rows.some(row => row.overflow)) deny() + const value = z.array(endpointSchema).length(2).parse(result.rows.map(row => row.endpoint)) + if (!isDeepStrictEqual(value.map(row => ({ id: row.id, enabled: row.enabled, routingEndpointEpoch: row.routingEndpointEpoch })), originalMetadata)) deny(); return freeze(value) + } + const endpointsAfterCompletion = async (next: Readonly, checkLive: () => void): Promise> => { + await basic(next, checkLive) + const result = await query(`/* scheduled-speed-original:completion-credentials */ WITH expected AS MATERIALIZED ( + SELECT * FROM pg_catalog.jsonb_to_recordset($1::jsonb) AS e(id bigint,epoch uuid)), + matched AS MATERIALIZED (SELECT s.id FROM public.servers s JOIN expected e ON e.id=s.id AND e.epoch=s.routing_endpoint_epoch WHERE s.enabled=true), + allowed AS MATERIALIZED (SELECT count(*)=2 + AND EXISTS(SELECT 1 FROM public.uptime_settings s WHERE s.id=1 AND coalesce(s.speed_enabled,s.enabled) AND jsonb_build_object('enabled',s.enabled,'speedEnabled',s.speed_enabled,'speedIntervalSec',s.speed_interval_sec)=$2::jsonb) AS complete FROM matched), + chosen AS MATERIALIZED (SELECT s.id,jsonb_build_object('id',s.id,'host',s.host,'port',s.port,'username',s.username,'password',s.password, + 'useSsl',s.use_ssl,'verifySsl',s.verify_ssl,'enabled',s.enabled,'routingEndpointEpoch',s.routing_endpoint_epoch) AS endpoint FROM public.servers s JOIN matched m ON m.id=s.id CROSS JOIN allowed a WHERE a.complete), + budget AS (SELECT coalesce(sum(octet_length(endpoint::text)),0) AS bytes FROM chosen) + SELECT id,CASE WHEN budget.bytes<=1048576 THEN endpoint END AS endpoint,budget.bytes>1048576 AS overflow FROM chosen CROSS JOIN budget ORDER BY id LIMIT 3`, + [JSON.stringify(originalMetadata.map(row => ({ id: row.id, epoch: row.routingEndpointEpoch }))), JSON.stringify(originalSettings)], next, checkLive) + await basic(next, checkLive); if (result.rows.some(row => row.overflow)) deny() + const value = z.array(endpointSchema).length(2).parse(result.rows.map(row => row.endpoint)) + if (!isDeepStrictEqual(value.map(row => ({ id: row.id, enabled: row.enabled, routingEndpointEpoch: row.routingEndpointEpoch })), originalMetadata)) deny(); return freeze(value) + } + const originalEndpoints = await endpoints(current, live) + const assertCurrent = async (next: Readonly, checkLive: () => void) => { + if (!isDeepStrictEqual(await settings(next, checkLive), originalSettings) || !isDeepStrictEqual(await probe(next, checkLive), originalProbe) + || !isDeepStrictEqual(await metadata(next, checkLive), originalMetadata) || !isDeepStrictEqual(await endpoints(next, checkLive), originalEndpoints)) deny() + await basic(next, checkLive) + } + // Only a genuine completion reaches this closure; selection alone may drift. + const assertAuthorityCurrent = async (next: Readonly, checkLive: () => void) => { + if (!isDeepStrictEqual(await settings(next, checkLive), originalSettings) + || !isDeepStrictEqual(await metadata(next, checkLive), originalMetadata) + || !isDeepStrictEqual(await endpointsAfterCompletion(next, checkLive), originalEndpoints)) deny() + await basic(next, checkLive) + } + const source = originalEndpoints.find(endpoint => endpoint.id === command.srcServerId)!, destination = originalEndpoints.find(endpoint => endpoint.id === command.dstServerId)! + const addresses = async (endpoint: Endpoint): Promise[]> => { + let guardFailed = false + const guard = async () => { try { await assertCurrent(current, live) } catch (error) { guardFailed = true; throw error } } + await guard(); let result: unknown + const signal = current.signal ? AbortSignal.any([current.signal, authority.signal]) : authority.signal + try { result = await new MikrotikClient(endpoint, endpoint.id).get("/ip/address", Math.max(1, Math.min(10000, current.deadlineAt - Date.now())), signal, undefined, guard) } + catch { if (guardFailed) return deny(); await guard(); return [] } + await guard(); return z.array(z.record(z.string(), z.unknown())).max(10000).parse(result) + } + const destinationRows = command.dstInterface ? await addresses(destination) : [], sourceRows = command.srcInterface ? await addresses(source) : [] + const dstAddress = command.dstInterface ? firstUsableIp(destinationRows, command.dstInterface) : destination.host + if (command.dstInterface && !dstAddress) throw new Error(`На интерфейсе назначения '${command.dstInterface}' нет активного IP (проверьте /ip/address — не disabled).`) + const srcAddress = command.srcInterface ? firstUsableIp(sourceRows, command.srcInterface) : source.host + const context = freeze({ command, srcAddress, dstAddress, address: dstAddress || destination.host, + srcInterfaceAddress: command.srcInterface ? srcAddress || null : null, dstInterfaceAddress: command.dstInterface ? dstAddress || null : null }) + const authorityGeneration = generation + ":" + authority.generation, operationId = run.operationId + const intent = freeze({ attemptId: run.attemptId, operationId, callerKind: "scheduled-speed" as const, sourceServerId: source.id, actualRemotePeer: { kind: "server" as const, serverId: destination.id }, credentialServerId: destination.id, + request: { command, job: originalAuthority }, nativeBody: { address: context.address, user: destination.username, password: destination.password, protocol: command.protocol, direction: command.direction, duration: command.durationSec + "s" }, + durationSec: command.durationSec, socketTimeoutMs: 30000, authorityProof: sign("authority:" + fingerprint({ run, authority: originalAuthority, settings: originalSettings, probe: originalProbe, generation })), authorityGeneration, expiresAt: authority.deadlineAt }) + await assertCurrent(current, live) + const peers = new Map(originalEndpoints.map(endpoint => [endpoint.id, freeze({ peerRef: { kind: "server" as const, serverId: endpoint.id }, enabled: true, + sourceProof: sign("endpoint:" + fingerprint({ generation, endpoint })), authorityGeneration, + endpoint: { host: endpoint.host, port: endpoint.port, useSsl: endpoint.useSsl, verifySsl: endpoint.verifySsl, username: endpoint.username, password: endpoint.password, routingEndpointEpoch: endpoint.routingEndpointEpoch } })])) + live(); const token = Object.freeze(Object.create(null)) as ScheduledSpeedBandwidthOriginal + entries.set(token, { intent, context, peers, authority, end, assertCurrent, assertAuthorityCurrent }); return token + }) + }, + async readOriginal(token: unknown, scope: Readonly): Promise { return (await read(token, scope)).intent }, + /** Trusted full workflow snapshot only. Strict selection remains pinned. */ + async readSpeedContext(token: unknown, scope: Readonly): Promise> { return (await read(token, scope)).context }, + /** Genuine original job lifetime only; an inspection's transient signal or + * caller JSON cannot replace the execution-completion cancellation. */ + async readLifetime(token: unknown, scope: Readonly): Promise> { + const entry = await read(token, scope) + return Object.freeze({ expiresAt: entry.intent.expiresAt, monotonicDeadline: entry.end, signal: entry.authority.signal }) + }, + async readPeer(token: unknown, rawPeer: BandwidthPeerRef, scope: Readonly): Promise> { + const peer = parseBandwidthPeerRef(rawPeer), entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry || peer.kind !== "server" || !entry.peers.has(peer.serverId)) return deny() + await read(token, scope); return entry.peers.get(peer.serverId)! + }, + /** Genuine native + durable completion ACK permits only probe selection drift. */ + async readPeerAfterCompletion(token: unknown, result: unknown, rawPeer: BandwidthPeerRef, rawScope: Readonly): Promise> { + const scope = scopeSnapshot(rawScope), peer = parseBandwidthPeerRef(rawPeer) + const entry = token && typeof token === "object" ? entries.get(token) : undefined + if (!entry || peer.kind !== "server" || !entry.peers.has(peer.serverId)) return deny() + if (!isDeepStrictEqual(readBandwidthPeerCompletion(result, token), entry.intent)) return deny() + await window(scope, entry.authority, entry.end, (current, live) => entry.assertAuthorityCurrent(current, live)) + return entry.peers.get(peer.serverId)! + }, + }) + providers.add(provider); return provider +} diff --git a/backend/src/services/bandwidth-speed-run-identity.test.ts b/backend/src/services/bandwidth-speed-run-identity.test.ts new file mode 100644 index 00000000..a9ebc211 --- /dev/null +++ b/backend/src/services/bandwidth-speed-run-identity.test.ts @@ -0,0 +1,84 @@ +/** Pure identity + actual journal plumbing with a synthetic unique-index driver. + * No PostgreSQL is used here; real cross-process/restore acceptance is separate. */ +import assert from "node:assert/strict" +import { test } from "node:test" +import { promisify } from "node:util" +import { execFile } from "node:child_process" +import { readFile } from "node:fs/promises" +import { z } from "zod" +import { createBandwidthSpeedRunIdentity, assertBandwidthSpeedRunHistoryAbsent, BandwidthSpeedRunIdUnavailable } from "./bandwidth-speed-run-identity.js" +import { SpeedRunPersistenceUnknown } from "./speed-run-persistence.js" +import { createBandwidthAdmissionSqlJournal } from "./bandwidth-admission-storage.js" +import type { BandwidthBinding } from "./bandwidth-admission.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" + +test("one normalized run ID has global stable custom UUIDv8/fullhash identity; actor/caller/generation cannot salt it", () => { + const first = createBandwidthSpeedRunIdentity(" owned-speed-α/原 "), second = createBandwidthSpeedRunIdentity("owned-speed-α/原") + assert.deepEqual(first, second); assert(Object.isFrozen(first)); assert.equal(first.runId, "owned-speed-α/原") + assert.match(first.attemptId, /^[a-f0-9]{8}-[a-f0-9]{4}-8[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/) + assert(z.string().uuid().safeParse(first.attemptId).success); assert.match(first.operationId, /^speed-run-v1:[a-f0-9]{64}$/) + assert.notEqual(createBandwidthSpeedRunIdentity("other").attemptId, first.attemptId) + assert.equal(createBandwidthSpeedRunIdentity("x".repeat(10000)).operationId.length, first.operationId.length) + const fresh = Array.from({ length: 30 }, () => createBandwidthSpeedRunIdentity()) + assert.equal(new Set(fresh.map(value => value.runId)).size, 30) + for (const value of fresh) assert.deepEqual(createBandwidthSpeedRunIdentity(value.runId), value) +}) +test("independent Node process derives identical identity without keys, caller state or application-history access", async () => { + const url = new URL("./bandwidth-speed-run-identity.ts", import.meta.url).href + const code = `import {createBandwidthSpeedRunIdentity} from ${JSON.stringify(url)}; process.stdout.write(JSON.stringify(createBandwidthSpeedRunIdentity("owned-cross-process-run")))` + const child = await promisify(execFile)(process.execPath, ["--import", "tsx", "--input-type=module", "-e", code]) + assert.deepEqual(JSON.parse(child.stdout), createBandwidthSpeedRunIdentity("owned-cross-process-run")) +}) +test("legacy lookup is fail closed and suppresses second failure publication, without pretending to reserve a run", () => { + assert.doesNotThrow(() => assertBandwidthSpeedRunHistoryAbsent({ command: "SELECT", rowCount: 0, rows: [] }, "owned-run")) + for (const result of [null, {}, { command: "UPDATE", rowCount: 0, rows: [] }, { command: "SELECT", rowCount: null, rows: [] }, + { command: "SELECT", rowCount: 1, rows: [{ id: "owned-run" }] }, { command: "SELECT", rowCount: 0, rows: [{}] }]) { + assert.throws(() => assertBandwidthSpeedRunHistoryAbsent(result, "owned-run"), error => { + assert(error instanceof BandwidthSpeedRunIdUnavailable); assert(error instanceof SpeedRunPersistenceUnknown) + assert.equal(error.automaticReplay, false); return true + }) + } + assert.throws(() => createBandwidthSpeedRunIdentity("bad\0run")); assert.throws(() => createBandwidthSpeedRunIdentity({ toString() { assert.fail("coercion executed") } } as never)) +}) +test("existing plain INSERT journal refuses another caller/new host for the same global attempt before arm/native; history erasure cannot clear this driver ledger", async () => { + // Explicit synthetic database uniqueness boundary: this exercises the ACTUAL + // SQL journal reserve/error path. It is not a real durability/restore proof. + let committedAttempt: string | undefined + const statements: string[] = [] + const connect = async (): Promise => { + let inserted: string | undefined + return { release() {}, async query(text, values = []) { + statements.push(text) + if (text === "BEGIN" || text === "ROLLBACK") return { command: text, rows: [] } + if (text === "COMMIT") { if (inserted) committedAttempt = inserted; return { command: "COMMIT", rows: [] } } + if (text.includes("pg_try_advisory_xact_lock")) return { command: "SELECT", rows: [{ locked: true }] } + if (text.includes("pg_catalog.set_config")) return { command: "SELECT", rows: [{}] } + if (text.startsWith("INSERT INTO mmapp_control.bandwidth_attempts(")) { + assert(!/ON CONFLICT/i.test(text)); if (committedAttempt === values[0]) throw Object.assign(Error("synthetic unique violation"), { code: "23505" }) + inserted = String(values[0]); return { command: "INSERT", rows: [{ attempt_id: values[0] }] } + } + if (text.startsWith("INSERT INTO mmapp_control.bandwidth_node_occupancy")) return { command: "INSERT", rows: [{ node_id: values[0] }, { node_id: values[1] }] } + if (text.startsWith("INSERT INTO mmapp_control.bandwidth_attempt_events")) return { command: "INSERT", rows: [{ ack_id: values[0] }] } + assert.fail("Unexpected journal statement") + } } + } + const identity = createBandwidthSpeedRunIdentity("owned-caller-independent-run"), now = Date.now() + const binding = (callerKind: "rest-speed" | "scheduled-speed", suffix: string): BandwidthBinding => ({ attemptId: identity.attemptId, + operationId: identity.operationId, callerKind, source: { canonicalNodeId: "source-" + suffix, endpointProof: "a".repeat(64) }, + destination: { canonicalNodeId: "dest-" + suffix, endpointProof: "b".repeat(64) }, authorityProof: "c".repeat(64), authorityGeneration: "different-actor-" + suffix, + controlGeneration: "owned-control", expiresAt: now + 60000, durationSec: 3, requestProof: "d".repeat(64), commandProof: "e".repeat(64), bindingProof: "f".repeat(64) }) + const journal = () => createBandwidthAdmissionSqlJournal({ now: Date.now, connect, authorize: async () => {}, verifyEvidence: async () => {}, eventProof: async () => "a".repeat(64) }) + const first = await journal().reserve(binding("rest-speed", "first"), { deadlineAt: now + 5000 }) + assert.equal(first.outcome, "new"); assert.equal(committedAttempt, identity.attemptId) + // Simulate application history absence/restore independently from control. + assertBandwidthSpeedRunHistoryAbsent({ command: "SELECT", rowCount: 0, rows: [] }, identity.runId) + await assert.rejects(journal().reserve(binding("scheduled-speed", "foreign"), { deadlineAt: now + 5000 })) + assert.equal(statements.filter(sql => sql.startsWith("INSERT INTO mmapp_control.bandwidth_attempts(")).length, 2) + assert.equal(statements.filter(sql => sql.startsWith("INSERT INTO mmapp_control.bandwidth_attempt_events")).length, 1) + assert.equal(statements.filter(sql => sql.startsWith("INSERT INTO mmapp_control.bandwidth_node_occupancy")).length, 1) +}) +test("required durable global key and permanent-attempt guards are existing control DDL, not an application-history uniqueness claim", async () => { + const source = await readFile(new URL("./bandwidth-admission-schema.ts", import.meta.url), "utf8") + assert(source.includes("attempt_id uuid PRIMARY KEY")); assert(source.includes("UNIQUE (caller_kind, operation_id)")) + assert(source.includes("bandwidth attempts cannot be deleted")); assert(source.includes("bandwidth_attempts_no_truncate")) +}) diff --git a/backend/src/services/bandwidth-speed-run-identity.ts b/backend/src/services/bandwidth-speed-run-identity.ts new file mode 100644 index 00000000..921972c7 --- /dev/null +++ b/backend/src/services/bandwidth-speed-run-identity.ts @@ -0,0 +1,49 @@ +/** One permanent control-journal attempt per normalized speed run ID. + * This is identity DATA, never an authority/capability or readiness assertion. + * Genuine originals seal it; the existing control journal's global attempt_id + * PRIMARY KEY, immutable/nondeletable rows and plain INSERT reserve enforce + * durable cross-process exclusion BEFORE arm/native, including after release. + * UNIQUE(caller_kind,operation_id) alone would not cover different callers. + * Application-history lookup is only a legacy supplement: restoring history + * without backfilled control attempts remains a cutover/backfill constraint. + * Restoring/resetting the control ledger itself is outside this guarantee. + */ +import { createHash, randomUUID } from "node:crypto" +import { SpeedRunPersistenceUnknown } from "./speed-run-persistence.js" + +export interface BandwidthSpeedRunIdentity { + readonly runId: string + readonly attemptId: string + readonly operationId: string +} +export class BandwidthSpeedRunIdUnavailable extends SpeedRunPersistenceUnknown { + readonly code = "BANDWIDTH_SPEED_RUN_ID_UNAVAILABLE" + readonly automaticReplay = false + constructor(runId: string) { + super(runId) + this.message = "Идентификатор speed-запуска уже использован или его состояние не подтверждено; повторная отправка запрещена" + } +} + +/** No actor/caller/key/generation salt: the SAME run ID must collide globally. + * Custom UUIDv8 retains 122 SHA-256 bits; any truncated collision fails closed + * at the primary key. Full SHA-256 remains in the bounded operation label. */ +export function createBandwidthSpeedRunIdentity(rawRunId?: string): Readonly { + if (rawRunId !== undefined && (typeof rawRunId !== "string" || rawRunId.includes("\0"))) throw new Error("Speed run identity unavailable") + const runId = rawRunId?.trim() || `sr-${Date.now()}-${randomUUID()}` + const digest = createHash("sha256").update("mmapp-bandwidth-speed-run-id-v1\0").update(runId, "utf8").digest() + const uuid = Buffer.from(digest.subarray(0, 16)) + uuid[6] = (uuid[6] & 15) | 128; uuid[8] = (uuid[8] & 63) | 128 + const text = uuid.toString("hex") + return Object.freeze({ runId, attemptId: `${text.slice(0, 8)}-${text.slice(8, 12)}-${text.slice(12, 16)}-${text.slice(16, 20)}-${text.slice(20)}`, + operationId: "speed-run-v1:" + digest.toString("hex") }) +} + +/** Fixed initial capture query only. This SELECT is not the durable reservation; + * only the existing independently retained control journal INSERT supplies it. */ +export const BANDWIDTH_SPEED_RUN_HISTORY_SQL = "/* bandwidth-speed-run:history */ SELECT id FROM public.uptime_speed_test_runs WHERE id=$1 LIMIT 1" +export function assertBandwidthSpeedRunHistoryAbsent(result: unknown, runId: string): void { + if (!result || typeof result !== "object") throw new BandwidthSpeedRunIdUnavailable(runId) + const value = result as { command?: unknown; rowCount?: unknown; rows?: unknown } + if (value.command !== "SELECT" || value.rowCount !== 0 || !Array.isArray(value.rows) || value.rows.length !== 0) throw new BandwidthSpeedRunIdUnavailable(runId) +} diff --git a/backend/src/services/bandwidth-speed-workflow.sql-helper.ts b/backend/src/services/bandwidth-speed-workflow.sql-helper.ts new file mode 100644 index 00000000..1334917c --- /dev/null +++ b/backend/src/services/bandwidth-speed-workflow.sql-helper.ts @@ -0,0 +1,365 @@ +/** Owned SQL/HTTP acceptance only; requires an exclusive disposable runner. No runtime entrypoint. + * Actual Fastify/JWT + application/control PG + signed v2 source + peer host + + * completion receipt + workflow + localhost HTTP. Enrollment Q/wire mapping and + * RouterOS responses are explicitly synthetic owned fixtures, never readiness. + */ +import assert from "node:assert/strict" +import { randomBytes, randomUUID } from "node:crypto" +import { ClientRequest, createServer } from "node:http" +import { writeFile } from "node:fs/promises" +import { join } from "node:path" +import { performance } from "node:perf_hooks" +import { AsyncLocalStorage } from "node:async_hooks" +import type { FastifyInstance } from "fastify" +import type { Client } from "pg" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +export async function acceptOwnedRestSpeedWorkflow() { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_BANDWIDTH_SPEED_WORKFLOW, "true") + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + await writeFile(join(process.env.MCP_STATE_DIR!, "encryption-key"), randomBytes(32), { flag: "wx", mode: 0o600 }) + await writeFile(join(process.env.MCP_STATE_DIR!, "access-generation"), randomBytes(32), { flag: "wx", mode: 0o600 }) + const { createOwnedExternalRegistryHarness } = await import("./bandwidth-external-node-registry.sql-helper.js") + const { pool } = await import("../db/index.js") + const { Client: PgClient } = await import("pg") + const { default: Fastify } = await import("fastify") + const { SignJWT } = await import("jose") + const { default: authPlugin, requireAuth } = await import("../plugins/auth.js") + const { env } = await import("../config.js") + const { speedTestCommandSchema } = await import("@mmapp/contracts") + const { createRestSpeedBandwidthOriginalProvider } = await import("./bandwidth-rest-speed-original.js") + const { createBandwidthPeerObservationReader } = await import("./bandwidth-peer-observation.js") + const { createBandwidthPeerExecutionHost } = await import("./bandwidth-peer-execution-host.js") + const { createBandwidthSpeedWorkflow } = await import("./bandwidth-speed-workflow.js") + const { SpeedRunPersistenceUnknown } = await import("./speed-run-persistence.js") + const { recordSpeedTestFailure } = await import("./uptime-speed-service.js") + const { createBandwidthSpeedRunIdentity } = await import("./bandwidth-speed-run-identity.js") + assert.equal(env.authRequired, true) + const f = await createOwnedExternalRegistryHarness() + const application = await pool.connect().catch(async error => { await f.close(); throw error }) + let app: FastifyInstance | undefined + const checks: string[] = [], runIdChecks: string[] = [], metrics: Record[] = [], runIdResults: Record[] = [] + const requestSql = new AsyncLocalStorage<{ applicationSql: number; controlSql: number }>() + const claims = [ + { "system-id": "OWNED-WORKFLOW-SOURCE-" + randomUUID(), "software-id": "OWNED-WORKFLOW-SOURCE" }, + { "system-id": "OWNED-WORKFLOW-DESTINATION-" + randomUUID(), "software-id": "OWNED-WORKFLOW-DESTINATION" }, + ] + let mode: "positive" | "zero" | "probe-edit" | "probe-delete" | "commit-loss" | "native-drop" | "runid-legacy" | "runid-history-delete" | "runid-concurrent" = "positive" + let nativePosts = 0, nativeGets = 0, pingPosts = 0, protocolErrors = 0, failureWrites = 0, sqlCalls = 0, coreSqlCalls = 0, credentialPayloadRows = 0 + let beforeResponse: (() => Promise) | undefined, lastNativeFinished = 0, pingDelay = 0 + let actualNativeBody: unknown, actualPingBody: unknown, expectedNativeBody: unknown + let rawRows: Array> = [] + const socketTimeouts: Array<{ path: string; timeoutMs: number }> = [] + const ids: number[] = [], users: string[] = [] + const probeId = "owned-workflow-probe-" + randomUUID() + let commitLossTarget: string | undefined, commitLossInjected = false, historyPid: number | undefined, positiveRunId = "" + const ownedRunIds = new Set() + let concurrentRunId: string | undefined, concurrentArrivals = 0, releaseConcurrent: (() => void) | undefined + let concurrentGate = Promise.resolve() + let historyCommitsObserved = 0 + const transactions = new WeakMap() + const query = PgClient.prototype.query, poolQuery = pool.query, setSocketTimeout = ClientRequest.prototype.setTimeout + const routers = claims.map((license, index) => createServer(async (request, response) => { + try { + assert.equal(request.socket.remoteAddress, "127.0.0.1") + assert.equal(request.headers.authorization, "Basic " + Buffer.from(users[index] + ":owned-localhost-only").toString("base64")) + response.setHeader("content-type", "application/json") + if (request.method === "GET" && request.url === "/rest/system/license") { nativeGets++; response.end(JSON.stringify(license)); return } + if (request.method === "GET" && request.url === "/rest/ip/address") { + nativeGets++ + response.end(JSON.stringify(index === 0 ? [{ interface: "source-if", address: "198.51.100.1/30" }] : [ + { interface: "dest-if", address: "198.51.100.99/30", dynamic: "true" }, + { interface: "dest-if", address: "198.51.100.77/30", disabled: "true" }, + { interface: "dest-if", address: "198.51.100.66/30", invalid: "true" }, + { interface: "dest-if", address: "198.51.100.2/30", dynamic: "false" }, + ])); return + } + assert.equal(request.method, "POST"); assert.equal(index, 0) + let text = ""; for await (const chunk of request) { text += String(chunk); assert(Buffer.byteLength(text) <= 65536) } + const body = JSON.parse(text) + if (request.url === "/rest/tool/bandwidth-test") { + nativePosts++; actualNativeBody = body; assert.deepEqual(body, expectedNativeBody) + if (mode === "native-drop") { response.destroy(); return } + await beforeResponse?.() + rawRows = [{ status: "testing", "tx-current": mode === "zero" ? "0" : "1000000", "rx-current": mode === "zero" ? "0" : "2M", retained: "owned-extra-sample" }, + { status: "done testing", "tx-total-average": mode === "zero" ? "0" : "3000000", "rx-total-average": mode === "zero" ? "0" : "4M", retained: "owned-extra-terminal" }] + lastNativeFinished = performance.now(); response.end(JSON.stringify(rawRows)); return + } + assert.equal(request.url, "/rest/tool/ping") + pingPosts++; actualPingBody = body + assert.deepEqual(body, { address: "198.51.100.2", count: "4", interface: "source-if", interval: "1s" }) + pingDelay = performance.now() - lastNativeFinished; assert(pingDelay >= 2950, "actual workflow must retain its 3-second pause") + response.end(JSON.stringify(mode === "zero" ? [] : [{ sent: "4", received: "3", "packet-loss": "25%", "avg-rtt": "2ms" }])) + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned workflow HTTP fixture protocol mismatch"}') } + })) + try { + // These are ACTUAL enrollment observations using the harness's own stable + // license vectors. Distinct source/destination claims cannot alias one node. + for (const license of claims) ids.push(await f.managedEndpoint(license)) + users.push(...ids.map(id => "managed-" + id)) + const receipts: Array<{ canonicalNodeId: string }> = [] + for (const id of ids) receipts.push(await f.managedHost.register(await f.managedObservation(id), f.scope())) + assert.notEqual(receipts[0].canonicalNodeId, receipts[1].canonicalNodeId) + for (let index = 0; index < routers.length; index++) { + await new Promise((resolve, reject) => { routers[index].once("error", reject); routers[index].listen(0, "127.0.0.1", resolve) }) + // Let the actual trigger mint epochs; all post-change endpoint snapshots + // come ONLY from the genuine REST provider's real application SQL. + await application.query("UPDATE public.servers SET port=$2 WHERE id=$1", [ids[index], (routers[index].address() as { port: number }).port]) + } + const signed = await f.createSignedSource(), provider = createRestSpeedBandwidthOriginalProvider() + const restoreProbe = async () => { + await application.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) + await application.query("INSERT INTO public.uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,dst_interface,protocol,direction,duration_sec,enabled) VALUES($1,$2,$3,'source-if','dest-if','udp','receive',3,true)", [probeId, ids[0], ids[1]]) + } + await restoreProbe() + // OBSERVATION ONLY except one explicitly selected real history COMMIT loss. + // All original SQL/configs/parameters execute unchanged on actual PG. The + // control DB can never satisfy this hook's application database/PID/INSERT. + PgClient.prototype.query = function(this: Client, ...args: unknown[]) { + const client = this as unknown as { connectionParameters: { database: string; user: string }; processID: number } + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "" + // Drizzle calls client.query(QueryConfig, params): the separate params + // array takes precedence, just as it does in pg's normalizeQueryConfig. + const values = (Array.isArray(args[1]) ? args[1] : (args[0] as { values?: unknown[] })?.values) as unknown[] | undefined + const applicationConnection = client.connectionParameters.database === "postgres" && client.connectionParameters.user === "mcp_test" + const counters = requestSql.getStore() + if (applicationConnection) { sqlCalls++; if (counters && typeof args.at(-1) !== "function") counters.applicationSql++ } + else if (client.connectionParameters.database === f.databaseName) { coreSqlCalls++; if (counters) counters.controlSql++ } + const result = Reflect.apply(query, this, args) + if (!result || typeof (result as Promise).then !== "function") return result + return (result as Promise).then(async value => { + if (!applicationConnection) return value + if (/^begin\b/i.test(text.trim())) transactions.set(this, { pid: client.processID }) + const tx = transactions.get(this) + if (tx && /^insert\s+into\s+"?uptime_speed_test_runs"?/i.test(text.trim())) { + const matching = values?.filter((value): value is string => typeof value === "string" && ownedRunIds.has(value)) ?? [] + assert.equal(matching.length, 1, "one exact owned history run ID per actual INSERT") + tx.historyRunId = matching[0] + } + if (/^(?:commit|rollback)\b/i.test(text.trim())) { + transactions.delete(this) + if (/^commit\b/i.test(text.trim()) && tx?.historyRunId) historyCommitsObserved++ + if (/^commit\b/i.test(text.trim()) && tx && tx.historyRunId === commitLossTarget && commitLossTarget !== undefined && !commitLossInjected) { + commitLossInjected = true; historyPid = tx.pid + // COMMIT really ACKed; only its return to the history writer is lost. + throw Error("Owned application history COMMIT return intentionally lost") + } + } + return value + }) + } as typeof query + // pg-pool forwards queries to Client.query with a callback, so observing + // only Client.query's Promise branch misses provider SELECT completions. + // Preserve pool SQL/config/params/results; synchronize only two real empty + // history SELECT results in the explicit concurrency case. + pool.query = function(this: typeof pool, ...args: unknown[]) { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text ?? "" + const values = (Array.isArray(args[1]) ? args[1] : (args[0] as { values?: unknown[] })?.values) as unknown[] | undefined + const counters = requestSql.getStore(); if (counters) counters.applicationSql++ + const result = Reflect.apply(poolQuery, this, args) + if (!result || typeof (result as Promise).then !== "function") return result + return (result as Promise).then(async value => { + if (text.startsWith("/* rest-speed-original:credentials */")) credentialPayloadRows += (value as { rows: unknown[] }).rows.length + if (concurrentRunId && text.startsWith("/* bandwidth-speed-run:history */") && values?.[0] === concurrentRunId) { + assert.equal((value as { rows: unknown[] }).rows.length, 0) + concurrentArrivals++; if (concurrentArrivals === 2) releaseConcurrent?.() + await concurrentGate + } + return value + }) + } as typeof pool.query + ClientRequest.prototype.setTimeout = function(this: ClientRequest, ...args: Parameters) { + if (this.path === "/rest/tool/bandwidth-test" || this.path === "/rest/tool/ping") socketTimeouts.push({ path: this.path, timeoutMs: args[0] }) + return Reflect.apply(setSocketTimeout, this, args) + } + app = Fastify(); await app.register(authPlugin); app.addHook("preHandler", requireAuth) + app.setErrorHandler((_error, _request, reply) => { reply.code(403).send({ refused: true }) }) + app.post("/api/uptime/speed-test", async (request, reply) => { + const counters = { applicationSql: 0, controlSql: 0 }, requestMode = mode + return requestSql.run(counters, async () => { + const command = speedTestCommandSchema.parse(request.body), scope = { deadlineAt: Date.now() + 60000 } + const began = performance.now() + let phase = "capture", failureCode: string | undefined, peerPhase: string | undefined + let observer: ReturnType | undefined + try { + const token = await provider.capture(request, command, scope) + phase = "compose" + observer = createBandwidthPeerObservationReader({ read: (peer, current) => provider.readPeer(token, peer, current) }) + const host = createBandwidthPeerExecutionHost({ coreSource: signed.core, nodeSource: signed.node, observer, + readOriginal: (original, current) => provider.readOriginal(original, current), + verifyRemote: async (original, intent, evidence, current) => { + assert.equal(original, token) + assert.deepEqual(intent.actualRemotePeer, { kind: "server", serverId: ids[1] }) + assert.equal(evidence.sourceReceipt.canonicalNodeId, receipts[0].canonicalNodeId) + assert.equal(evidence.remoteReceipt.canonicalNodeId, receipts[1].canonicalNodeId) + assert.equal(intent.nativeBody.address, (await provider.readSpeedContext(token, current)).address) + return { proof: f.mac("SYNTHETIC-OWNED-WORKFLOW-WIRE-MAPPING", { source: receipts[0].canonicalNodeId, remote: receipts[1].canonicalNodeId, + address: intent.nativeBody.address, credentialServerId: intent.credentialServerId, generation: "owned-mapping-1" }), expiresAt: intent.expiresAt } + } }) + phase = "workflow" + const result = await createBandwidthSpeedWorkflow({ originals: provider, peerHost: host }).run(token, scope) + phase = "done"; return { ok: true, result } + } catch (error) { + const cause = error instanceof Error && error.cause ? error.cause : error + const marker = cause as { code?: unknown; phase?: unknown } + if (typeof marker?.code === "string" && /^[A-Z_]{1,80}$/.test(marker.code)) failureCode = marker.code + if (typeof marker?.phase === "string" && /^[a-z-]{1,80}$/.test(marker.phase)) peerPhase = marker.phase + if (error instanceof SpeedRunPersistenceUnknown) { phase = "unknown"; return reply.code(502).send({ outcome: "unknown", runId: error.runId }) } + phase += "-refused"; failureWrites++ + await recordSpeedTestFailure({ ...command, message: error instanceof Error ? error.message : String(error) }) + return reply.code(502).send({ outcome: "refused" }) + } finally { + observer?.close(); metrics.push({ mode: requestMode, requestId: request.id, runId: command.runId, phase, failureCode, peerPhase, + elapsedMs: Math.round(performance.now() - began), applicationSql: counters.applicationSql, controlSql: counters.controlSql }) + } + }) + }) + const mintJwt = () => new SignJWT({ sub: "owned-workflow-actor-" + randomUUID(), apps: ["mm"], permissions: ["mm:uptime:write"], iat: Math.floor(Date.now() / 1000) - 60, exp: Math.floor(Date.now() / 1000) + 3600 }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).sign(new TextEncoder().encode(env.jwtSecret)) + const jwt = await mintJwt() + const send = async (options: { durationSec?: number; runId?: string; jwt?: string } = {}) => { + const durationSec = options.durationSec ?? 3, runId = options.runId ?? "owned-workflow-run-" + randomUUID() + ownedRunIds.add(runId) + expectedNativeBody = { address: "198.51.100.2", user: users[1], password: "owned-localhost-only", protocol: "udp", direction: "receive", duration: durationSec + "s" } + const response = await app!.inject({ method: "POST", url: "/api/uptime/speed-test", headers: { authorization: "Bearer " + (options.jwt ?? jwt) }, + payload: { runId, probeId, srcServerId: ids[0], dstServerId: ids[1], srcInterface: "source-if", dstInterface: "dest-if", protocol: "udp", direction: "receive", durationSec } }) + return { response, runId } + } + const history = async (runId: string) => (await application.query("SELECT id,probe_id,status,tx_avg_mbps,rx_avg_mbps,ping_rtt_ms,ping_loss_pct,ping_error FROM public.uptime_speed_test_runs WHERE id=$1", [runId])).rows + for (const next of ["positive", "zero", "probe-edit", "probe-delete", "commit-loss"] as const) { + mode = next; await restoreProbe(); commitLossInjected = false + const caseRunId = "owned-workflow-run-" + randomUUID(); commitLossTarget = next === "commit-loss" ? caseRunId : undefined + beforeResponse = next === "probe-edit" ? async () => { await application.query("UPDATE public.uptime_speed_probes SET duration_sec=4 WHERE id=$1", [probeId]) } + : next === "probe-delete" ? async () => { await application.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) } : undefined + const beforeNative = nativePosts, beforePing = pingPosts, beforeFailures = failureWrites, beforeCommits = historyCommitsObserved + const { response, runId } = await send({ runId: caseRunId }), rows = await history(runId), body = response.json() + console.log("Owned wholeflow case timing:", JSON.stringify(metrics.at(-1))) + assert.equal(nativePosts, beforeNative + 1, next); assert.equal(pingPosts, beforePing + 1, next) + assert.equal(failureWrites, beforeFailures, next); assert.equal(rows.length, 1, next); assert.equal(rows[0].status, "done") + assert.equal(historyCommitsObserved, beforeCommits + 1, "observe exactly one real history COMMIT for " + next) + assert.equal(rows[0].probe_id, next === "probe-edit" || next === "probe-delete" ? null : probeId) + if (next === "commit-loss") { assert.equal(response.statusCode, 502); assert.equal(body.outcome, "unknown"); assert(commitLossInjected); assert(historyPid); assert.equal(body.runId, runId) } + else { + assert.equal(response.statusCode, 200, next); assert.equal(body.ok, true); assert.deepEqual(body.result.raw, rawRows) + assert.equal(body.result.txAvgMbps, next === "zero" ? 0 : 3); assert.equal(body.result.rxAvgMbps, next === "zero" ? 0 : 4) + assert.equal(Number(rows[0].tx_avg_mbps), next === "zero" ? 0 : 3) + if (next === "zero") { assert.equal(body.result.afterBtPing.rttMs, null); assert(body.result.afterBtPing.error); assert(rows[0].ping_error) } + else { assert.equal(body.result.afterBtPing.rttMs, 2); assert.equal(body.result.afterBtPing.lossPct, 25); assert.equal(body.result.afterBtPing.error, null) } + } + assert(pingDelay >= 2950); assert.deepEqual(actualNativeBody, expectedNativeBody) + assert.deepEqual(actualPingBody, { address: "198.51.100.2", count: "4", interface: "source-if", interval: "1s" }) + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy")).rows[0].n), 0) + checks.push(next + ": genuine REST + signed v2 actual broker completion receipt + exact native/ping + real atomic history; no second failure/native replay") + if (next === "positive") positiveRunId = runId + } + // Separate run-ID acceptance. Original six workflow checks remain intact. + // All fault injection is disabled before overlapping/new actor requests. + commitLossTarget = undefined; beforeResponse = undefined; await restoreProbe() + { + mode = "runid-legacy" + const legacyId = "owned-workflow-run-legacy-" + randomUUID(), identity = createBandwidthSpeedRunIdentity(legacyId) + await application.query("INSERT INTO public.uptime_speed_test_runs(id,src_server_id,dst_server_id,status) VALUES($1,$2,$3,'done')", [legacyId, ids[0], ids[1]]) + assert.equal((await f.owner.query("SELECT attempt_id FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [identity.attemptId])).rows.length, 0) + const before = { nativePosts, nativeGets, pingPosts, credentialPayloadRows, failureWrites } + const { response } = await send({ runId: legacyId, jwt: await mintJwt() }) + assert.equal(response.statusCode, 502); assert.equal(response.json().outcome, "unknown") + assert.deepEqual({ nativePosts, nativeGets, pingPosts, credentialPayloadRows, failureWrites }, before) + assert.equal((await history(legacyId)).length, 1) + assert.equal((await f.owner.query("SELECT attempt_id FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [identity.attemptId])).rows.length, 0) + runIdChecks.push("legacy app-history ID with no control attempt refuses before credential projection/native and writes no failure") + runIdResults.push({ case: "legacy-history", nativePosts: nativePosts - before.nativePosts, nativeGets: nativeGets - before.nativeGets, + credentialPayloadRows: credentialPayloadRows - before.credentialPayloadRows, failureWrites: failureWrites - before.failureWrites }) + } + { + mode = "runid-history-delete" + assert(positiveRunId) + const identity = createBandwidthSpeedRunIdentity(positiveRunId) + const first = (await f.owner.query("SELECT caller_kind,state,resolution FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [identity.attemptId])).rows + assert.equal(first.length, 1); assert.equal(first[0].caller_kind, "rest-speed"); assert.equal(first[0].state, "released"); assert.equal(first[0].resolution, "remote_completion") + const removed = await application.query("DELETE FROM public.uptime_speed_test_runs WHERE id=$1 RETURNING id", [positiveRunId]) + assert.equal(removed.rowCount, 1); assert.equal(removed.rows[0].id, positiveRunId) + const before = { nativePosts, pingPosts, failureWrites, historyCommitsObserved } + const { response } = await send({ runId: positiveRunId, jwt: await mintJwt() }) + assert.equal(response.statusCode, 502); assert.equal(response.json().outcome, "unknown") + assert.deepEqual({ nativePosts, pingPosts, failureWrites, historyCommitsObserved }, before) + assert.equal((await history(positiveRunId)).length, 0) + const remaining = (await f.owner.query("SELECT caller_kind,state,resolution FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [identity.attemptId])).rows + assert.deepEqual(remaining, first) + runIdChecks.push("after DELETE of exactly one owned history row, fresh genuine JWT actor/same run ID cannot send again; permanent released control attempt remains unchanged (not a database restore claim)") + runIdResults.push({ case: "owned-history-row-delete", removedHistoryRows: removed.rowCount, nativePosts: nativePosts - before.nativePosts, + pingPosts: pingPosts - before.pingPosts, failureWrites: failureWrites - before.failureWrites, preservedControlAttempts: remaining.length }) + } + { + mode = "runid-concurrent" + const sharedId = "owned-workflow-run-concurrent-" + randomUUID() + concurrentRunId = sharedId; concurrentArrivals = 0 + concurrentGate = new Promise(resolve => { releaseConcurrent = resolve }) + const before = { nativePosts, pingPosts, failureWrites, historyCommitsObserved } + const tokens = await Promise.all([mintJwt(), mintJwt()]) + let replies: Awaited>[] + try { replies = await Promise.all(tokens.map(jwt => send({ runId: sharedId, jwt }))) } + finally { releaseConcurrent?.(); concurrentRunId = undefined; releaseConcurrent = undefined } + assert.equal(concurrentArrivals, 2, "both genuine requests observed empty history before either continued") + assert.equal(nativePosts - before.nativePosts, 1); assert.equal(pingPosts - before.pingPosts, 1) + assert.equal(failureWrites, before.failureWrites); assert.equal(historyCommitsObserved - before.historyCommitsObserved, 1) + const successes = replies.filter(value => value.response.statusCode === 200).length + assert(successes <= 1) + for (const value of replies) assert(value.response.statusCode === 200 || value.response.statusCode === 502 && value.response.json().outcome === "unknown") + const rows = await history(sharedId); assert.equal(rows.length, 1); assert.equal(rows[0].status, "done") + const identity = createBandwidthSpeedRunIdentity(sharedId) + const attempts = (await f.owner.query("SELECT state FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [identity.attemptId])).rows + assert.equal(attempts.length, 1); assert.equal(attempts[0].state, "released") + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy")).rows[0].n), 0) + runIdChecks.push("two synchronized actual REST/JWT requests with one new run ID yield exactly one native POST/history COMMIT, at most one success, no failure write or automatic replay") + runIdResults.push({ case: "concurrent-real-requests", requests: replies.length, emptyHistoryArrivals: concurrentArrivals, + nativePosts: nativePosts - before.nativePosts, pingPosts: pingPosts - before.pingPosts, successes, historyRows: rows.length, + historyCommits: historyCommitsObserved - before.historyCommitsObserved, failureWrites: failureWrites - before.failureWrites }) + } + { + // Actual control PG journal, explicitly synthetic fixed authority scope; + // this supplements the genuine REST cases without a fake driver/native. + const { createOwnedBandwidthHarness, fixtureBinding } = await import("./bandwidth-admission-process.sql-helper.js") + const identity = createBandwidthSpeedRunIdentity("owned-crosscaller-run-" + randomUUID()) + const firstHost = await createOwnedBandwidthHarness(f.owner), secondHost = await createOwnedBandwidthHarness(f.owner) + const first = fixtureBinding("owned-crosscaller-source-a", "owned-crosscaller-dest-a", { attemptId: identity.attemptId, operationId: identity.operationId, callerKind: "rest-speed" }) + const second = fixtureBinding("owned-crosscaller-source-b", "owned-crosscaller-dest-b", { attemptId: identity.attemptId, operationId: identity.operationId, callerKind: "scheduled-speed" }) + firstHost.register(first); secondHost.register(second) + const before = nativePosts + assert.equal((await firstHost.journal.reserve(first, f.scope())).outcome, "new") + firstHost.markNoSend(first) + await firstHost.journal.releaseBeforeDispatch(first, { from: "reserved", proof: "private-no-send" }, f.scope()) + await assert.rejects(secondHost.journal.reserve(second, f.scope())) + assert.equal(nativePosts, before) + const rows = (await f.owner.query("SELECT caller_kind,state,resolution FROM mmapp_control.bandwidth_attempts WHERE attempt_id=$1", [identity.attemptId])).rows + assert.equal(rows.length, 1); assert.equal(rows[0].caller_kind, "rest-speed"); assert.equal(rows[0].state, "released"); assert.equal(rows[0].resolution, "proven_not_dispatched") + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy")).rows[0].n), 0) + runIdChecks.push("actual control journal global attempt PK rejects same run ID across rest-speed/scheduled-speed after first release; explicit synthetic fixture authority, no fake SQL driver") + runIdResults.push({ case: "crosscaller-actual-control-journal", callerKinds: ["rest-speed", "scheduled-speed"], preservedAttempts: rows.length, nativePosts: nativePosts - before, + syntheticAuthority: true, actualSql: true }) + } + // Domain acceptance without an impractical native wait. The actual shared + // schema retains int32 duration; CPU workflow covers its full native flow. + assert.equal(speedTestCommandSchema.parse({ srcServerId: ids[0], dstServerId: ids[1], durationSec: 2147483647 }).durationSec, 2147483647) + mode = "native-drop"; beforeResponse = undefined; commitLossTarget = undefined; await restoreProbe() + const beforeNative = nativePosts, beforePing = pingPosts, beforeFailures = failureWrites + const { response: dropped, runId: droppedRunId } = await send(); assert.equal(dropped.statusCode, 502); assert.equal(dropped.json().outcome, "unknown") + assert.equal(nativePosts, beforeNative + 1); assert.equal(pingPosts, beforePing); assert.equal(failureWrites, beforeFailures); assert.equal((await history(droppedRunId)).length, 0) + assert.equal(Number((await f.owner.query("SELECT count(*) AS n FROM mmapp_control.bandwidth_node_occupancy")).rows[0].n), 2) + checks.push("real native socket drop leaves unknown-busy pair occupancy; no ping/failure row/replay") + assert.equal(protocolErrors, 0) + assert(socketTimeouts.some(value => value.path === "/rest/tool/bandwidth-test" && value.timeoutMs === 30000)) + assert(socketTimeouts.some(value => value.path === "/rest/tool/ping" && value.timeoutMs === 20000)) + assert.equal(checks.length, 6); assert.equal(runIdChecks.length, 4) + return { checks, runIdChecks, runIdResults, metrics, nativePosts, nativeGets, pingPosts, protocolErrors, failureWrites, credentialPayloadRows, historyCommitsObserved, commitLossInjected: historyPid !== undefined, + actualFastifyJwt: true, actualApplicationAndControlSql: true, actualNativeHttp: true, actualPrivateCompletionReceipt: true, + syntheticEnrollmentQuiescence: true, syntheticWireMapping: true, productionMappingVerified: false, runtimeAllCallersActivated: false, realChrCalls: 0 } + } catch (error) { + console.error("Owned wholeflow failure context:", JSON.stringify({ metrics, nativePosts, pingPosts, protocolErrors, failureWrites, sqlCalls, coreSqlCalls })) + throw error + } finally { + releaseConcurrent?.(); pool.query = poolQuery; PgClient.prototype.query = query; ClientRequest.prototype.setTimeout = setSocketTimeout + await app?.close() + for (const router of routers) { router.closeAllConnections(); if (router.listening) await new Promise(resolve => router.close(() => resolve())) } + application.release(true); await f.close() + } +} diff --git a/backend/src/services/bandwidth-speed-workflow.sql.test.ts b/backend/src/services/bandwidth-speed-workflow.sql.test.ts new file mode 100644 index 00000000..68779c8f --- /dev/null +++ b/backend/src/services/bandwidth-speed-workflow.sql.test.ts @@ -0,0 +1,16 @@ +/** Owned exclusive SQL/HTTP acceptance entry; never loaded by runtime. */ +import assert from "node:assert/strict" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState } from "./bandwidth-admission-process.sql-helper.js" +async function main() { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_BANDWIDTH_SPEED_WORKFLOW, "true") + assert.equal(process.env.MMAPP_OWN_BANDWIDTH_EXTERNAL_REGISTRY, "true") + await prepareOwnedBandwidthState() + const { acceptOwnedRestSpeedWorkflow } = await import("./bandwidth-speed-workflow.sql-helper.js") + console.log("Owned genuine REST whole speed workflow:", JSON.stringify(await acceptOwnedRestSpeedWorkflow())) +} +void main().catch(error => { + // Assertion details may contain private RouterOS credentials. Per-case + // sanitized phase/count diagnostics are emitted by the owned helper instead. + console.error("Owned whole workflow acceptance failed", { name: error?.name, code: error?.code }) + process.exitCode = 1 +}) diff --git a/backend/src/services/bandwidth-speed-workflow.test.ts b/backend/src/services/bandwidth-speed-workflow.test.ts new file mode 100644 index 00000000..82b2ec30 --- /dev/null +++ b/backend/src/services/bandwidth-speed-workflow.test.ts @@ -0,0 +1,243 @@ +/** Actual ESM workflow; fixed dependencies mocked HERE ONLY. + * No production DI hooks, native transport, actual DB or waiting three seconds. */ +import assert from "node:assert/strict" +import { afterEach, mock, test } from "node:test" +import { assertBandwidthTestCompleted } from "./bandwidth-test-status.js" +import { readBandwidthTestRates } from "./bandwidth-test-rates.js" +import { persistSpeedRun, SpeedRunPersistenceUnknown } from "./speed-run-persistence.js" +import { assertMcpSpeedBandwidthWorkflowRequest } from "../modules/mcp/speed-bandwidth-original.js" +let fixtureSequence = 0 +afterEach(() => mock.restoreAll()) +type Command = { runId?: string; probeId?: string; srcServerId: number; dstServerId: number; srcInterface: string; dstInterface: string; + protocol: "tcp" | "udp"; direction: "both" | "transmit" | "receive"; durationSec: number } +type Row = Record +class HostError extends Error { constructor(readonly code: "REFUSED" | "OUTCOME_UNKNOWN") { super(code) } } +async function fixture(kind: "rest-speed" | "scheduled-speed" | "mcp-speed" = "rest-speed", overrides: Partial = {}) { + const initialWall = Date.now() + const owner = new AbortController() + let enteredNative!: () => void + const nativeEntered = new Promise(resolve => { enteredNative = resolve }) + const command: Command = { runId: "owned-run", probeId: "owned-probe", srcServerId: 1, dstServerId: 2, + srcInterface: "ether1", dstInterface: "ether2", protocol: "tcp", direction: "both", durationSec: 10, ...overrides } + const context = { command, address: "198.51.100.2", srcAddress: "192.0.2.1", dstAddress: "198.51.100.2", + srcInterfaceAddress: "192.0.2.1", dstInterfaceAddress: "198.51.100.2" } + const request: Record = kind === "rest-speed" ? { command } : kind === "scheduled-speed" ? { command, + job: { job: "uptime_speed", generation: 4, executionId: "12345678-1234-4234-8234-123456789abc", deadlineAt: initialWall + 60000, monotonicDeadline: 60100 } } : { command, + mcp: { operationName: "uptime.speed-test", serviceName: "uptime.speed-test", operationVersion: 1, phase: "forward", originalExpiresAt: initialWall + 60000, + serverIds: [1, 2], objects: [1, 2].map(serverId => ({ kind: "server", serverId, id: String(serverId) })), + serviceInput: { command, expectedEndpoints: [1, 2].map(serverId => ({ serverId, proof: "a".repeat(64) })), + expectedProbe: command.probeId ? { id: command.probeId, srcServerId: 1, dstServerId: 2, srcInterface: command.srcInterface, dstInterface: command.dstInterface, + protocol: command.protocol, direction: command.direction, durationSec: command.durationSec, enabled: true } : null } } } + const intent = { callerKind: kind, sourceServerId: 1, credentialServerId: 2, actualRemotePeer: { kind: "server", serverId: 2 }, + request, expiresAt: initialWall + 60000, durationSec: command.durationSec, socketTimeoutMs: 30000, + nativeBody: { address: context.address, protocol: command.protocol, direction: command.direction, duration: `${command.durationSec}s`, user: "owned-peer-user", password: "private-peer-password" } } + const source = { endpoint: { host: "source-host", username: "owned-source-user", password: "private-source-password" } } + const token = Object.freeze(Object.create(null)) + // Synthetic completion identity for this unit harness ONLY; production uses + // the actual peer host's private receipt, covered by its owned acceptance. + const completions = new WeakMap() + const state = { probe: command.probeId ? { ...command, id: command.probeId, telemetry: "old" } as Row : null, + runs: [] as Row[], transactions: 0, native: 0, pings: 0, contexts: 0, peers: 0, sleeps: [] as number[], events: [] as string[], + wall: initialWall, mono: 100, waitForAbort: false, observedAbort: false, nativeDeadline: 0, pingSignal: undefined as AbortSignal | undefined, + forgedCompletion: false, receiptIntentChanged: false, postPeerCredentialReads: 0, + nativeError: undefined as unknown, afterNative: undefined as (() => void) | undefined, pingError: undefined as unknown, + freshError: false, measured: true, failWrite: "" as "" | "update" | "insert" | "commit", + raw: [{ status: "testing", "tx-current": "1000000", "rx-current": "2M", extra: "retained" }, + { status: "done testing", "tx-total-average": "3000000", "rx-total-average": "4M", extra: "terminal-retained" }] as Row[] } + const lifetime = { expiresAt: state.wall + 60000, monotonicDeadline: state.mono + 60000, signal: owner.signal } + const originals = { async readLifetime(current: unknown) { assert.equal(current, token); return lifetime }, + async readOriginal(current: unknown) { assert.equal(current, token); state.events.push("original"); return intent }, + async readSpeedContext(current: unknown) { assert.equal(current, token); state.contexts++; return context }, + async readPeer(current: unknown, peer: unknown) { assert.equal(current, token); assert.equal((peer as { kind: string }).kind, "server"); assert.equal((peer as { serverId: number }).serverId, 1); state.peers++ + if (state.native && state.freshError) throw Error("original source/actor/selection drift") + if (command.probeId && (!state.probe || state.probe.durationSec !== command.durationSec)) throw Error("probe selection changed") + return source }, + async readPeerAfterCompletion(current: unknown, result: object, peer: unknown) { + assert.equal(current, token) + if (completions.get(result) !== token) throw Error("completion receipt unavailable") + assert.equal((peer as { kind: string }).kind, "server"); assert.equal((peer as { serverId: number }).serverId, 1) + // Only selection is omitted after a genuine result. Actor/source changes + // still reject; credentials are not projected for an unrecognized receipt. + if (state.freshError) throw Error("original source/actor drift") + state.postPeerCredentialReads++ + return source } } + const host = { async execute(current: unknown, scope: { deadlineAt: number; signal: AbortSignal }) { assert.equal(current, token); state.native++; state.events.push("native") + state.nativeDeadline = scope.deadlineAt; enteredNative() + if (state.waitForAbort) return new Promise((_resolve, reject) => { + const abort = () => { state.observedAbort = true; reject(new HostError("OUTCOME_UNKNOWN")) } + if (scope.signal.aborted) abort(); else scope.signal.addEventListener("abort", abort, { once: true }) + }) + if (state.nativeError) throw state.nativeError + state.afterNative?.() + const result = Object.freeze({ remoteCompletionConfirmed: true, raw: state.raw }) + completions.set(result, token) + return state.forgedCompletion ? { ...result } : result } } + const fields = ["id", "srcServerId", "dstServerId", "srcInterface", "dstInterface", "protocol", "direction", "durationSec"] + const schema = { uptimeSpeedProbes: Object.fromEntries(fields.map(field => [field, field])), uptimeSpeedTestRuns: {} } + const db = { async transaction(work: (tx: unknown) => Promise) { + state.transactions++; state.events.push("transaction") + const pending = { probe: structuredClone(state.probe), runs: structuredClone(state.runs) } + const tx = { update() { return { set(values: Row) { return { where(conditions: Array<{ field: string; value: unknown }>) { return { async returning() { + if (state.failWrite === "update") throw Error("update failed") + const matched = !!pending.probe && conditions.every(condition => pending.probe![condition.field] === condition.value) + if (matched) { Object.assign(pending.probe!, values); return [{ id: command.probeId }] } return [] + } } } } } } }, insert() { return { async values(value: Row) { pending.runs.push(value); if (state.failWrite === "insert") throw Error("insert failed") } } } } + await work(tx); if (state.failWrite === "commit") throw Error("commit unknown") + state.probe = pending.probe; state.runs = pending.runs + } } + class Client { + constructor(endpoint: unknown, id: number, options: unknown) { assert.equal(endpoint, source.endpoint); assert.equal(id, 1); assert.equal((options as { requireBandwidthPermit: boolean }).requireBandwidthPermit, true) } + async ping(address: string, count: number, iface: string | undefined, options: { interval: string; signal: AbortSignal; beforeDispatch: () => Promise }) { + state.pings++; state.events.push("ping"); assert.equal(address, context.address); assert.equal(count, 4) + state.pingSignal = options.signal + assert.equal(iface, command.srcInterface || undefined); assert.equal(options.interval, "1s") + await options.beforeDispatch(); if (state.pingError) throw state.pingError + return [{ sent: "4", received: "3", "avg-rtt": "2ms", "packet-loss": "25%" }] + } + } + const assertOriginal = (value: unknown, expected: string) => { if (value !== originals || kind !== expected) throw Error("not genuine provider") } + const providers: Record = { + "drizzle-orm": { and: (...conditions: unknown[]) => conditions, eq: (field: string, value: unknown) => ({ field, value }) }, + "../db/index.js": { db }, "../db/schema.js": schema, + "node:perf_hooks": { performance: { now: () => state.mono } }, + "./bandwidth-peer-execution-host.js": { BandwidthPeerExecutionHostError: HostError, assertBandwidthPeerExecutionHost(value: unknown) { if (value !== host) throw Error("not genuine host") }, + readBandwidthPeerCompletion(value: object, current: object) { if (completions.get(value) !== current) throw Error("completion receipt unavailable"); return state.receiptIntentChanged ? { ...intent, durationSec: intent.durationSec + 1 } : intent } }, + "./bandwidth-rest-speed-original.js": { assertRestSpeedBandwidthOriginalProvider: (value: unknown) => assertOriginal(value, "rest-speed") }, + "./bandwidth-scheduled-speed-original.js": { assertScheduledSpeedBandwidthOriginalProvider: (value: unknown) => assertOriginal(value, "scheduled-speed") }, + "../modules/mcp/speed-bandwidth-original.js": { assertMcpSpeedBandwidthOriginalProvider: (value: unknown) => assertOriginal(value, "mcp-speed"), assertMcpSpeedBandwidthWorkflowRequest }, + "./mikrotik.js": { MikrotikClient: Client }, + "./uptime-collector.js": { parsePing(rows: unknown[]) { assert.equal(rows.length, 1); return { measured: state.measured, avgRtt: 2, loss: 25 } } }, + "./bandwidth-test-status.js": { assertBandwidthTestCompleted }, "./bandwidth-test-rates.js": { readBandwidthTestRates }, + "./speed-run-persistence.js": { persistSpeedRun, SpeedRunPersistenceUnknown }, + } + for (const [name, namedExports] of Object.entries(providers)) mock.module(name, { namedExports: namedExports as Record }) + mock.method(Date, "now", () => state.wall) + mock.method(globalThis, "setTimeout", ((callback: () => void, ms: number) => { + assert.equal(ms, 3000); state.sleeps.push(ms); state.events.push("pause"); queueMicrotask(callback); return 1 + }) as unknown as typeof setTimeout) + const api = await import("./bandwidth-speed-workflow.js?workflow-fixture=" + ++fixtureSequence) + type Workflow = { run(token: unknown, scope: { deadlineAt: number }): Promise } + const create = api.createBandwidthSpeedWorkflow as (deps: unknown) => Workflow + return { state, command, context, intent, token, create, originals, host, owner, lifetime, nativeEntered, scope: { deadlineAt: Date.now() + 60000 }, + run: () => create({ originals, peerHost: host }).run(token, { deadlineAt: Date.now() + 60000 }) } +} +for (const kind of ["rest-speed", "scheduled-speed", "mcp-speed"] as const) test(`${kind}: complete baseline raw/rates/pause/ping/current-probe transaction`, async () => { + const f = await fixture(kind), result = await f.run() + assert.equal(f.state.native, 1); assert.deepEqual(f.state.sleeps, [3000]); assert.equal(f.state.pings, 1); assert.equal(f.state.contexts, 1) + assert.deepEqual(f.state.events.filter(event => event !== "original"), ["native", "pause", "ping", "transaction"]) + assert.equal(result.raw, f.state.raw); assert.equal(result.txAvgMbps, 3); assert.equal(result.rxAvgMbps, 4) + assert.deepEqual(result.txSeries, [1, 3]); assert.deepEqual(result.rxSeries, [2, 4]) + assert.equal(result.afterBtPing.rttMs, 2); assert.equal(result.afterBtPing.lossPct, 25) + assert.equal(result.srcInterfaceAddress, "192.0.2.1"); assert.equal(result.dstInterfaceAddress, "198.51.100.2") + assert.equal(f.state.runs[0].probeId, "owned-probe"); assert.equal(f.state.runs[0].status, "done") + assert.equal(f.state.probe!.lastStatus, "done"); assert.equal(f.state.probe!.lastPingLossPct, 25) +}) +test("factory denies structural arbitrary run/provider replacements; copies cannot execute", async () => { + const f = await fixture() + assert.throws(() => f.create({ originals: { ...f.originals }, peerHost: f.host })) + assert.throws(() => f.create({ originals: f.originals, peerHost: { ...f.host } })) + await assert.rejects(f.create({ originals: f.originals, peerHost: f.host }).run({ ...f.token }, f.scope)); assert.equal(f.state.native, 0) +}) +for (const drift of ["missing", "wrong-job", "expiry", "monotonic", "unknown-field", "command"] as const) test(`scheduled request ${drift} refuses before native`, async () => { + const f = await fixture("scheduled-speed") + const job = f.intent.request.job as Record + if (drift === "missing") delete f.intent.request.job + if (drift === "wrong-job") job.job = "uptime_ping" + if (drift === "expiry") job.deadlineAt = Number(job.deadlineAt) + 1 + if (drift === "monotonic") job.monotonicDeadline = f.lifetime.monotonicDeadline - 1 + if (drift === "unknown-field") job.extraAuthority = true + if (drift === "command") f.intent.request.command = { ...f.command, durationSec: 11 } + await assert.rejects(f.run(), /original context unavailable/) + assert.equal(f.state.native, 0); assert.equal(f.state.transactions, 0); assert.equal(f.state.pings, 0) +}) +for (const drift of ["missing", "service", "version", "expiry", "scope", "command", "unknown-field"] as const) test(`MCP sealed request ${drift} refuses before native`, async () => { + const f = await fixture("mcp-speed"), metadata = f.intent.request.mcp as Record + if (drift === "missing") delete f.intent.request.mcp + if (drift === "service") metadata.serviceName = "routing.measure" + if (drift === "version") metadata.operationVersion = 2 + if (drift === "expiry") metadata.originalExpiresAt = Number(metadata.originalExpiresAt) + 1 + if (drift === "scope") metadata.objects = [{ kind: "server", serverId: 1, id: "1" }] + if (drift === "command") metadata.serviceInput = { ...(metadata.serviceInput as object), command: { ...f.command, direction: "receive" } } + if (drift === "unknown-field") f.intent.request.extra = true + await assert.rejects(f.run(), /original context unavailable/) + assert.equal(f.state.native, 0); assert.equal(f.state.transactions, 0); assert.equal(f.state.pings, 0) +}) +for (const durationSec of [3, 2147483647]) test(`no duration range narrowing: ${durationSec}`, async () => { + const f = await fixture("scheduled-speed", { durationSec, protocol: "udp", direction: "receive" }) + const result = await f.run(); assert.equal(result.durationSec, durationSec); assert.equal(result.protocol, "udp"); assert.equal(result.direction, "receive") +}) +for (const drift of ["changed", "deleted"] as const) test(`after native ${drift} probe persists real success without stale association`, async () => { + const f = await fixture() + f.state.afterNative = () => { if (drift === "deleted") f.state.probe = null; else f.state.probe!.durationSec = 99 } + const result = await f.run(); assert.equal(result.txAvgMbps, 3) + assert.equal(f.state.pings, 1); assert.equal(result.afterBtPing.error, null); assert.equal(result.afterBtPing.rttMs, 2) + assert.equal(f.state.postPeerCredentialReads, 2) + assert.equal(f.state.runs[0].probeId, null); assert.equal(f.state.runs[0].status, "done") + if (f.state.probe) assert.equal(f.state.probe.telemetry, "old") +}) +test("ad-hoc/interface fallbacks preserve result and history without probe update", async () => { + const f = await fixture("rest-speed", { probeId: undefined, srcInterface: "", dstInterface: "" }) + f.context.srcAddress = ""; f.context.srcInterfaceAddress = null as unknown as string; f.context.dstInterfaceAddress = null as unknown as string + const result = await f.run(); assert.equal(result.srcAddress, "source-host"); assert.equal(result.srcInterface, null) + assert.equal(result.dstInterface, null); assert.equal(f.state.runs[0].probeId, null) +}) +for (const mode of ["transport", "unmeasured", "freshness"] as const) test(`after-ping ${mode} is telemetry error, native success remains success`, async () => { + const f = await fixture() + if (mode === "transport") f.state.pingError = Error("ping refused") + if (mode === "unmeasured") f.state.measured = false + if (mode === "freshness") f.state.freshError = true + const result = await f.run(); assert.equal(result.afterBtPing.rttMs, null); assert(result.afterBtPing.error) + assert.equal(result.txAvgMbps, 3); assert.equal(f.state.runs[0].status, "done") +}) +test("predispatch refusal leaves legacy failure handling to caller", async () => { + const f = await fixture(); f.state.nativeError = new HostError("REFUSED") + await assert.rejects(f.run(), HostError); assert.equal(f.state.transactions, 0); assert.equal(f.state.pings, 0) +}) +test("possibly sent native outcome suppresses second failure publication/replay", async () => { + const f = await fixture(); f.state.nativeError = new HostError("OUTCOME_UNKNOWN") + await assert.rejects(f.run(), error => { + assert(error instanceof SpeedRunPersistenceUnknown); assert.equal(error.runId, "owned-run") + assert.equal((error as unknown as { automaticReplay: boolean }).automaticReplay, false); assert(!error.message.includes("завершён")); return true + }) + assert.equal(f.state.transactions, 0); assert.equal(f.state.pings, 0) +}) +test("unconfirmed native rows never publish done/ping", async () => { + const f = await fixture(); f.state.raw = [{ status: "testing" }] + await assert.rejects(f.run(), SpeedRunPersistenceUnknown); assert.equal(f.state.transactions, 0); assert.equal(f.state.pings, 0) +}) +for (const stage of ["update", "insert", "commit"] as const) test(`${stage} persistence uncertainty has no partial/second failure row`, async () => { + const f = await fixture(); f.state.failWrite = stage + await assert.rejects(f.run(), SpeedRunPersistenceUnknown) + assert.equal(f.state.runs.length, 0); assert.equal(f.state.probe!.telemetry, "old"); assert.equal(f.state.probe!.lastStatus, undefined) + assert.equal(f.state.transactions, 1); assert.equal(f.state.native, 1) +}) +test("genuine original lifecycle abort reaches an already pending peer host without caller signal", async () => { + const f = await fixture(); f.state.waitForAbort = true + const pending = f.run(); await f.nativeEntered; f.owner.abort() + await assert.rejects(pending, SpeedRunPersistenceUnknown) + assert.equal(f.state.observedAbort, true); assert.equal(f.state.transactions, 0); assert.equal(f.state.pings, 0) +}) +test("genuine original lifecycle is passed to after-ping transport", async () => { + const f = await fixture(); await f.run() + assert.equal(f.state.pingSignal, f.owner.signal); f.owner.abort(); assert.equal(f.state.pingSignal!.aborted, true) +}) +test("earlier original monotonic horizon clips native deadline despite wall rollback", async () => { + const f = await fixture(); f.lifetime.monotonicDeadline = f.state.mono + 25 + f.state.wall -= 100000 + await f.run(); assert.equal(f.state.nativeDeadline, f.state.wall + 25) + mock.restoreAll() + const expired = await fixture(); expired.lifetime.monotonicDeadline = expired.state.mono - 1 + await assert.rejects(expired.run(), /lifetime unavailable/); assert.equal(expired.state.native, 0) +}) +test("copied completion is rejected before ping or post-completion credential projection", async () => { + const f = await fixture(); f.state.forgedCompletion = true + await assert.rejects(f.run(), SpeedRunPersistenceUnknown) + assert.equal(f.state.pings, 0); assert.equal(f.state.postPeerCredentialReads, 0) + assert.equal(f.state.transactions, 0); assert.equal(f.state.runs.length, 0); assert.equal(f.state.sleeps.length, 0) +}) +test("completion receipt for another exact intent cannot publish history", async () => { + const f = await fixture(); f.state.receiptIntentChanged = true + await assert.rejects(f.run(), SpeedRunPersistenceUnknown) + assert.equal(f.state.transactions, 0); assert.equal(f.state.pings, 0); assert.equal(f.state.postPeerCredentialReads, 0) +}) + diff --git a/backend/src/services/bandwidth-speed-workflow.ts b/backend/src/services/bandwidth-speed-workflow.ts new file mode 100644 index 00000000..73521564 --- /dev/null +++ b/backend/src/services/bandwidth-speed-workflow.ts @@ -0,0 +1,176 @@ +/** Explicit complete speed workflow for genuine REST/scheduler originals. + * No default/runtime registration or arbitrary run/send/SQL callback injection. + * The peer host owns privileged bandwidth dispatch and durable occupancy; this + * module preserves native rows/rates, after-ping and transactional run history. + * Production activation additionally requires reviewed held mapping/all callers. + */ +import { and, eq } from "drizzle-orm" +import { isDeepStrictEqual } from "node:util" +import { performance } from "node:perf_hooks" +import { z } from "zod" +import { db } from "../db/index.js" +import { uptimeSpeedProbes, uptimeSpeedTestRuns } from "../db/schema.js" +import type { SpeedTestRunResult } from "./uptime-speed-service.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { assertBandwidthPeerExecutionHost, readBandwidthPeerCompletion, BandwidthPeerExecutionHostError, type createBandwidthPeerExecutionHost } from "./bandwidth-peer-execution-host.js" +import { assertRestSpeedBandwidthOriginalProvider, type createRestSpeedBandwidthOriginalProvider } from "./bandwidth-rest-speed-original.js" +import { assertScheduledSpeedBandwidthOriginalProvider, type createScheduledSpeedBandwidthOriginalProvider } from "./bandwidth-scheduled-speed-original.js" +import { assertMcpSpeedBandwidthOriginalProvider, assertMcpSpeedBandwidthWorkflowRequest, type createMcpSpeedBandwidthOriginalProvider } from "../modules/mcp/speed-bandwidth-original.js" +import { MikrotikClient } from "./mikrotik.js" +import { parsePing } from "./uptime-collector.js" +import { assertBandwidthTestCompleted } from "./bandwidth-test-status.js" +import { readBandwidthTestRates } from "./bandwidth-test-rates.js" +import { persistSpeedRun, SpeedRunPersistenceUnknown, type SpeedProbeContext } from "./speed-run-persistence.js" + +type Originals = ReturnType | ReturnType | ReturnType +type SpeedCaller = "rest-speed" | "scheduled-speed" | "mcp-speed" +type WriteTransaction = Parameters[0]>[0] +export interface BandwidthSpeedWorkflowDependencies { + originals: Originals + peerHost: ReturnType +} +/** Existing REST/scheduler catches recognize this superclass and MUST NOT write + * a second failure row or automatically replay a possibly sent native command. */ +export class BandwidthSpeedWorkflowUnknown extends SpeedRunPersistenceUnknown { + readonly automaticReplay = false + constructor(runId: string, cause?: unknown) { + super(runId, cause) + this.message = `Результат сетевого теста неизвестен. Проверьте историю по runId ${runId} перед повтором` + } +} +const newRunId = () => `sr-${Date.now()}-${Math.random().toString(36).slice(2, 8)}` +const scheduledRequestSchema = z.object({ command: z.unknown(), job: z.object({ + job: z.literal("uptime_speed"), generation: z.number().int().nonnegative().safe(), executionId: z.string().uuid(), + deadlineAt: z.number().int().positive().safe(), monotonicDeadline: z.number().finite().nonnegative(), +}).strict() }).strict() +function matchesOriginalRequest(caller: SpeedCaller, intent: Awaited>, command: Parameters[1], monotonicDeadline: number) { + if (caller === "rest-speed") return isDeepStrictEqual(intent.request, { command }) + if (caller === "mcp-speed") { + try { assertMcpSpeedBandwidthWorkflowRequest(intent.request, command, intent.expiresAt); return true } catch { return false } + } + const parsed = scheduledRequestSchema.safeParse(intent.request) + // The genuine provider authenticates this exact sealed job on every read. + // Retain it here rather than projecting scheduled requests into REST shape. + return parsed.success && isDeepStrictEqual(parsed.data.command, command) && parsed.data.job.deadlineAt === intent.expiresAt + && parsed.data.job.monotonicDeadline >= monotonicDeadline +} +function workflowScope(raw: Readonly): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.keys(raw).some(key => key !== "deadlineAt" && key !== "signal") + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(value => !("value" in value) || !value.enumerable) + || !Number.isSafeInteger(raw.deadlineAt) || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) throw new Error("Speed workflow scope unavailable") + return Object.freeze({ deadlineAt: raw.deadlineAt, signal: raw.signal }) +} +function whereProbe(target: SpeedProbeContext) { + return and(eq(uptimeSpeedProbes.id, target.id), eq(uptimeSpeedProbes.srcServerId, target.srcServerId), eq(uptimeSpeedProbes.dstServerId, target.dstServerId), + eq(uptimeSpeedProbes.srcInterface, target.srcInterface), eq(uptimeSpeedProbes.dstInterface, target.dstInterface), + eq(uptimeSpeedProbes.protocol, target.protocol), eq(uptimeSpeedProbes.direction, target.direction), eq(uptimeSpeedProbes.durationSec, target.durationSec)) +} + +/** Intended service callsite: trusted bootstrap constructs ONE workflow for each + * genuine original provider and its actual peer host; REST passes its captured + * request original, scheduler passes its fixed-job selected-probe original. + * run accepts NO command JSON: every parameter comes from that genuine original. + * Failure publication remains the existing caller's recordSpeedTestFailure; + * persistence/native unknown subclasses suppress that second write as before. + */ +export function createBandwidthSpeedWorkflow(dependencies: BandwidthSpeedWorkflowDependencies) { + const originals = dependencies.originals + let callerKind: SpeedCaller + try { assertRestSpeedBandwidthOriginalProvider(originals); callerKind = "rest-speed" } + catch { + try { assertScheduledSpeedBandwidthOriginalProvider(originals); callerKind = "scheduled-speed" } + catch { assertMcpSpeedBandwidthOriginalProvider(originals); callerKind = "mcp-speed" } + } + assertBandwidthPeerExecutionHost(dependencies.peerHost) + const readOriginal = originals.readOriginal.bind(originals), readContext = originals.readSpeedContext.bind(originals), readPeer = originals.readPeer.bind(originals) + const readLifetime = originals.readLifetime.bind(originals) + const readPeerAfterCompletion = originals.readPeerAfterCompletion.bind(originals) + const execute = dependencies.peerHost.execute.bind(dependencies.peerHost) + return Object.freeze({ + async run(originalContext: unknown, rawScope: Readonly): Promise { + const input = workflowScope(rawScope) + const lifetime = await readLifetime(originalContext, input) + if (!Number.isSafeInteger(lifetime.expiresAt) || !Number.isFinite(lifetime.monotonicDeadline) || !(lifetime.signal instanceof AbortSignal)) throw new Error("Speed workflow lifetime unavailable") + const signal = input.signal ? AbortSignal.any([lifetime.signal, input.signal]) : lifetime.signal + // Translate the SAME original monotonic end into a current wall deadline. + // Recompute at dispatch boundaries so wall rollback during provider I/O + // cannot turn the original remaining lifetime into a longer native lease. + const currentScope = (): Readonly => { + const now = Date.now(), remaining = lifetime.monotonicDeadline - performance.now() + if (signal.aborted || remaining <= 0 || now >= Math.min(input.deadlineAt, lifetime.expiresAt)) throw new Error("Speed workflow lifetime unavailable") + return Object.freeze({ deadlineAt: Math.floor(Math.min(input.deadlineAt, lifetime.expiresAt, now + remaining)), signal }) + } + let scope = currentScope() + const intent = await readOriginal(originalContext, scope), context = await readContext(originalContext, scope) + const command = context.command + if (intent.callerKind !== callerKind || intent.sourceServerId !== command.srcServerId || intent.credentialServerId !== command.dstServerId + || intent.actualRemotePeer.kind !== "server" || intent.actualRemotePeer.serverId !== command.dstServerId + || !matchesOriginalRequest(callerKind, intent, command, lifetime.monotonicDeadline) || intent.durationSec !== command.durationSec || intent.socketTimeoutMs !== 30000 + || intent.nativeBody.address !== context.address || intent.nativeBody.duration !== `${command.durationSec}s` + || intent.nativeBody.protocol !== command.protocol || intent.nativeBody.direction !== command.direction) throw new Error("Speed workflow original context unavailable") + const sourceRef = Object.freeze({ kind: "server" as const, serverId: command.srcServerId }) + const source = await readPeer(originalContext, sourceRef, scope) + const sourceClient = new MikrotikClient(source.endpoint, command.srcServerId, { requireBandwidthPermit: true }) + let native: Awaited> + try { scope = currentScope(); native = await execute(originalContext, scope) } + catch (error) { + if (error instanceof BandwidthPeerExecutionHostError && error.code === "OUTCOME_UNKNOWN") throw new BandwidthSpeedWorkflowUnknown(command.runId || newRunId(), error) + throw error + } + // The actual peer host returns only after native terminal confirmation and + // durable completion ACK. Keep complete raw rows, including extra fields. + try { + if (!isDeepStrictEqual(readBandwidthPeerCompletion(native, originalContext), intent)) throw new Error("Speed workflow completion context changed") + } catch (error) { throw new BandwidthSpeedWorkflowUnknown(command.runId || newRunId(), error) } + if (native.remoteCompletionConfirmed !== true) throw new BandwidthSpeedWorkflowUnknown(command.runId || newRunId()) + const rows = native.raw + try { assertBandwidthTestCompleted(rows) } catch (error) { throw new BandwidthSpeedWorkflowUnknown(command.runId || newRunId(), error) } + const { txAvgMbps: txFinal, rxAvgMbps: rxFinal, txSeries, rxSeries } = readBandwidthTestRates(rows) + await new Promise(resolve => setTimeout(resolve, 3000)) + const pingNow = new Date().toISOString() + let afterBtPing: SpeedTestRunResult["afterBtPing"] = { rttMs: null, lossPct: null, error: null } + try { + const fresh = async () => { + if (!isDeepStrictEqual(await readPeerAfterCompletion(originalContext, native, sourceRef, currentScope()), source)) throw new Error("Speed workflow source changed") + } + await fresh() + const pingRows = await sourceClient.ping(context.address, 4, command.srcInterface || undefined, { interval: "1s", signal: scope.signal, beforeDispatch: fresh }) + const parsed = parsePing(pingRows) + if (!parsed.measured) throw new Error("RouterOS не вернул подтверждённые результаты контрольного Ping") + afterBtPing = { rttMs: parsed.avgRtt, lossPct: parsed.loss, error: null } + } catch (error) { + afterBtPing = { rttMs: null, lossPct: null, error: error instanceof Error ? error.message : String(error) } + } + const runId = command.runId || newRunId(), probeId = command.probeId || "" + const target: SpeedProbeContext = { id: probeId, srcServerId: command.srcServerId, dstServerId: command.dstServerId, + srcInterface: command.srcInterface, dstInterface: command.dstInterface, protocol: command.protocol, direction: command.direction, durationSec: command.durationSec } + try { + await persistSpeedRun(probeId ? target : null, { + transaction: work => db.transaction(work), + updateProbe: async (tx, current) => { + const updated = await tx.update(uptimeSpeedProbes).set({ lastRunAt: pingNow, lastTxAvgMbps: txFinal, lastRxAvgMbps: rxFinal, + lastStatus: "done", lastError: "", lastPingRttMs: afterBtPing.rttMs, lastPingLossPct: afterBtPing.lossPct, + lastPingAt: pingNow, lastPingError: afterBtPing.error ?? "", updatedAt: pingNow, + }).where(whereProbe(current)).returning({ id: uptimeSpeedProbes.id }) + return updated.length === 1 + }, + insertRun: async (tx, associatedProbeId) => { + await tx.insert(uptimeSpeedTestRuns).values({ id: runId, probeId: associatedProbeId, + srcServerId: command.srcServerId, dstServerId: command.dstServerId, srcInterface: command.srcInterface || "", dstInterface: command.dstInterface || "", + srcAddress: context.srcAddress || source.endpoint.host, dstAddress: context.address, + srcInterfaceAddress: context.srcInterfaceAddress, dstInterfaceAddress: context.dstInterfaceAddress, + protocol: command.protocol, direction: command.direction, durationSec: command.durationSec, + txAvgMbps: txFinal, rxAvgMbps: rxFinal, pingRttMs: afterBtPing.rttMs, pingLossPct: afterBtPing.lossPct, + pingError: afterBtPing.error ?? null, status: "done", error: null, createdAt: pingNow }) + }, + }) + } catch (error) { throw new SpeedRunPersistenceUnknown(runId, error) } + return { createdAt: pingNow, runId, srcInterface: command.srcInterface || null, dstInterface: command.dstInterface || null, + address: context.address, srcAddress: context.srcAddress || source.endpoint.host, dstAddress: context.address, + srcInterfaceAddress: context.srcInterfaceAddress, dstInterfaceAddress: context.dstInterfaceAddress, + protocol: command.protocol, direction: command.direction, durationSec: command.durationSec, + txAvgMbps: txFinal, rxAvgMbps: rxFinal, txSeries, rxSeries, raw: rows as Array>, afterBtPing } + }, + }) +} diff --git a/backend/src/services/bgp-community-policy-model.test.ts b/backend/src/services/bgp-community-policy-model.test.ts new file mode 100644 index 00000000..1222a150 --- /dev/null +++ b/backend/src/services/bgp-community-policy-model.test.ts @@ -0,0 +1,37 @@ +import assert from "node:assert/strict" +import { bgpCommunityPolicySchema, canonicalBgpPolicyPrefix, compileBgpCommunityPolicy, readCompiledBgpCommunityPolicy } from "./bgp-community-policy-model.js" +for (const [input, expected] of [["192.0.2.255/24", "192.0.2.0/24"], ["255.255.255.255/0", "0.0.0.0/0"], ["192.0.2.1/32", "192.0.2.1/32"], ["2001:0DB8:0000:0001::ffff/64", "2001:db8:0:1::/64"], ["ffff::1/0", "::/0"], ["::1/128", "::1/128"], ["2001:db8:0:1:0:0:1:1/128", "2001:db8:0:1::1:1/128"], ["::ffff:192.0.2.129/120", "::ffff:c000:200/120"]]) assert.equal(canonicalBgpPolicyPrefix(input), expected) +for (const prefix of ["192.0.2.1", "192.0.2.1/33", "::/129", "fe80::1%gre/64", "example.test/24", "192.0.2.1/24/1", "::1/128); reject;", "010.1.1.1/24"]) { + assert.throws(() => canonicalBgpPolicyPrefix(prefix)) + assert.equal(bgpCommunityPolicySchema.safeParse({ policyId: "invalid", rules: [{ prefix }] }).success, false) +} +const basic = { policyId: "fixture", rules: [{ prefix: "192.0.2.17/24", communityMode: "set", communities: ["064512:001"] }] } +// Community pair components are at most five decimal digits. +assert.equal(bgpCommunityPolicySchema.safeParse(basic).success, false) +basic.rules[0].communities = ["64512:001"] +const compiled = compileBgpCommunityPolicy(basic) +assert.equal(compiled.chain, "mmapp-bgp-out-fixture"); assert.ok(compiled.body.includes("dst == 192.0.2.0/24")); assert.ok(compiled.body.includes("set bgp-communities 64512:1;")); assert.ok(compiled.body.endsWith("reject;")) +assert.equal(compileBgpCommunityPolicy({ policyId: "empty", rules: [] }).body, "reject;") +const sequence = compileBgpCommunityPolicy({ policyId: "sequence", defaultAction: "accept", rules: [ + { prefix: "2001:db8::/32", match: "subnet", minLength: 48, maxLength: 64, communityMode: "append", communities: ["64512:2"], action: "continue" }, + { prefix: "2001:db8::/32", match: "subnet", communityMode: "clear" }, { prefix: "0.0.0.0/0", match: "subnet", minLength: 32, maxLength: 32, action: "reject" }, +] }) +assert.ok(sequence.body.includes("dst-len in 48-64")); assert.ok(sequence.body.includes("append bgp-communities 64512:2;")); assert.ok(sequence.body.includes("delete bgp-communities wk,other;")); assert.ok(sequence.body.includes("dst-len == 32")) +assert.ok(sequence.body.indexOf("append") < sequence.body.indexOf("delete")); assert.ok(sequence.body.endsWith("accept;")) +for (const program of [compiled, sequence, compileBgpCommunityPolicy({ policyId: "empty", rules: [] }), compileBgpCommunityPolicy({ policyId: "keep", rules: [{ prefix: "::/0", action: "reject" }, { prefix: "192.0.2.1/32" }] })]) { + assert.deepEqual(readCompiledBgpCommunityPolicy(program.policy.policyId, program.body), program.policy) +} +for (const unknown of [undefined, "NEVER_NATIVE_SCRIPT", compiled.body + "\n", compiled.body.replace("192.0.2.0/24", "192.0.2.1/24"), compiled.body.replace("64512:1", "64512:01"), compiled.body.replace(" accept;", " log info NEVER_NATIVE_SCRIPT;\n accept;"), compiled.body.replace("64512:1", "65536:1"), sequence.body.replace("delete bgp-communities wk,other", "delete bgp-communities other,wk")]) { + assert.equal(readCompiledBgpCommunityPolicy("unknown", unknown), null, "Unknown or noncanonical native scripts must never be guessed") +} +for (const rule of [ + { prefix: "192.0.2.0/24", match: "exact", maxLength: 32 }, { prefix: "192.0.2.0/24", match: "subnet", minLength: 16 }, + { prefix: "192.0.2.0/24", match: "subnet", maxLength: 33 }, { prefix: "::/64", match: "subnet", minLength: 100, maxLength: 90 }, + { prefix: "::/64", communityMode: "set" }, { prefix: "::/64", communityMode: "clear", communities: ["1:1"] }, + { prefix: "::/64", communityMode: "set", communities: ["1:1", "01:01"] }, { prefix: "::/64", communityMode: "set", communities: ["65536:1"] }, + { prefix: "::/64", action: "reject", communityMode: "set", communities: ["1:1"] }, { prefix: "::/64", action: "continue" }, + { prefix: "::/64", rule: "accept;" }, +]) assert.throws(() => compileBgpCommunityPolicy({ policyId: "invalid", rules: [rule] })) +assert.throws(() => compileBgpCommunityPolicy({ ...basic, policyId: "in; reject;" })) +assert.throws(() => compileBgpCommunityPolicy({ policyId: "limit", rules: Array.from({ length: 500 }, (_, index) => ({ prefix: `2001:db8:${index.toString(16)}::/48`, communityMode: "append", communities: Array.from({ length: 32 }, (_, n) => `64512:${n}`) })) }), /64 KiB/) +console.log("Community export compiler: IPv4/IPv6 CIDR canonicalization, exact/subnet lengths, ordered keep/set/append/clear and accept/reject/continue, uint16 communities, default action, injection and body bounds; native parser acceptance pending") diff --git a/backend/src/services/bgp-community-policy-model.ts b/backend/src/services/bgp-community-policy-model.ts new file mode 100644 index 00000000..6c9bdcea --- /dev/null +++ b/backend/src/services/bgp-community-policy-model.ts @@ -0,0 +1,94 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { standardCommunitySchema } from "./filter-rules-write-model.js" + +/** Canonical network address; never embed unchecked text in routing-filter code. */ +export function canonicalBgpPolicyPrefix(value: string) { + const [address, mask, extra] = value.trim().split("/") + const family = isIP(address) + if (extra !== undefined || !family || address.includes("%") || !/^\d{1,3}$/.test(mask ?? "") || Number(mask) > (family === 4 ? 32 : 128)) throw new Error("Нужен literal IPv4/IPv6 CIDR") + const length = Number(mask), width = family === 4 ? 32 : 128 + let numeric: bigint + if (family === 4) numeric = address.split(".").reduce((sum, part) => (sum << 8n) + BigInt(part), 0n) + else { + let text = address.toLowerCase() + if (text.includes(".")) { + const end = text.lastIndexOf(":"), v4 = text.slice(end + 1).split(".").map(Number) + text = text.slice(0, end + 1) + ((v4[0] << 8) + v4[1]).toString(16) + ":" + ((v4[2] << 8) + v4[3]).toString(16) + } + const halves = text.split("::"), left = halves[0] ? halves[0].split(":") : [], right = halves[1] ? halves[1].split(":") : [] + const words = halves.length === 1 ? left : [...left, ...Array(8 - left.length - right.length).fill("0"), ...right] + numeric = words.reduce((sum, part) => (sum << 16n) + BigInt("0x" + part), 0n) + } + const shift = BigInt(width - length), network = numeric >> shift << shift + if (family === 4) return Array.from({ length: 4 }, (_, index) => Number(network >> BigInt((3 - index) * 8) & 255n)).join(".") + "/" + length + const words = Array.from({ length: 8 }, (_, index) => (network >> BigInt((7 - index) * 16) & 65535n).toString(16)) + let bestStart = -1, bestLength = 1 + for (let index = 0; index < words.length;) { + if (words[index] !== "0") { index++; continue } + const start = index; while (index < words.length && words[index] === "0") index++ + if (index - start > bestLength) { bestStart = start; bestLength = index - start } + } + const canonical = bestStart < 0 ? words.join(":") : words.slice(0, bestStart).join(":") + "::" + words.slice(bestStart + bestLength).join(":") + return canonical + "/" + length +} +const prefix = z.string().trim().max(100).transform((value, context) => { + try { return canonicalBgpPolicyPrefix(value) } catch { context.addIssue({ code: "custom", message: "Нужен literal IPv4/IPv6 CIDR" }); return z.NEVER } +}) +const ruleSchema = z.object({ + prefix, match: z.enum(["exact", "subnet"]).default("exact"), minLength: z.number().int().min(0).max(128).optional(), maxLength: z.number().int().min(0).max(128).optional(), + communityMode: z.enum(["keep", "set", "append", "clear"]).default("keep"), communities: z.array(standardCommunitySchema).max(32).default([]), + action: z.enum(["accept", "reject", "continue"]).default("accept"), +}).strict().superRefine((rule, context) => { + const [address, bits] = rule.prefix.split("/"), length = Number(bits), width = isIP(address) === 4 ? 32 : 128 + const min = rule.minLength ?? length, max = rule.maxLength ?? (rule.match === "exact" ? length : width) + if (min < length || max > width || min > max || rule.match === "exact" && (min !== length || max !== length)) context.addIssue({ code: "custom", path: ["minLength"], message: "Некорректные границы длины префикса" }) + if (new Set(rule.communities).size !== rule.communities.length) context.addIssue({ code: "custom", path: ["communities"], message: "Community повторяется после нормализации" }) + if (["set", "append"].includes(rule.communityMode) ? !rule.communities.length : !!rule.communities.length) context.addIssue({ code: "custom", path: ["communities"], message: "Set/append требуют непустой список; keep/clear — пустой" }) + if (rule.action === "reject" && rule.communityMode !== "keep" || rule.action === "continue" && rule.communityMode === "keep") context.addIssue({ code: "custom", path: ["action"], message: "Reject не меняет attributes; continue должен менять Community" }) +}).transform(rule => ({ ...rule, minLength: rule.minLength ?? Number(rule.prefix.split("/")[1]), maxLength: rule.maxLength ?? (rule.match === "exact" ? Number(rule.prefix.split("/")[1]) : isIP(rule.prefix.split("/")[0]) === 4 ? 32 : 128) })) +export const bgpCommunityPolicySchema = z.object({ policyId: z.string().regex(/^[a-z][a-z0-9_-]{0,47}$/), rules: z.array(ruleSchema).max(500), defaultAction: z.enum(["accept", "reject"]).default("reject") }).strict() +export type BgpCommunityPolicy = z.output +export const bgpCommunityPolicyChain = (policyId: string) => "mmapp-bgp-out-" + bgpCommunityPolicySchema.shape.policyId.parse(policyId) +export const bgpCommunityPolicyComment = (policyId: string) => "MikrotikManager: MCP BGP output " + bgpCommunityPolicySchema.shape.policyId.parse(policyId) +export function compileBgpCommunityPolicy(input: unknown) { + const policy = bgpCommunityPolicySchema.parse(input) + const body = policy.rules.map(rule => { + const condition = rule.match === "exact" ? `dst == ${rule.prefix}` : `dst in ${rule.prefix} && dst-len ${rule.minLength === rule.maxLength ? `== ${rule.minLength}` : `in ${rule.minLength}-${rule.maxLength}`}` + const lines = [`if (${condition}) {`] + if (rule.communityMode === "set" || rule.communityMode === "append") lines.push(` ${rule.communityMode === "set" ? "set" : "append"} bgp-communities ${rule.communities.join(",")};`) + else if (rule.communityMode === "clear") lines.push(" delete bgp-communities wk,other;") + if (rule.action !== "continue") lines.push(` ${rule.action};`) + return [...lines, "}"].join("\n") + }).concat(policy.defaultAction + ";").join("\n") + if (Buffer.byteLength(body, "utf8") > 65536) throw new Error("Тело Community export policy превышает 64 KiB") + return { policy, chain: bgpCommunityPolicyChain(policy.policyId), comment: bgpCommunityPolicyComment(policy.policyId), body } +} + +/** Decode only an exact program emitted by this compiler. Never interpret arbitrary + * RouterOS code or return unknown native text in the human/agent plan preview. */ +export function readCompiledBgpCommunityPolicy(policyId: string, body: unknown): BgpCommunityPolicy | null { + if (typeof body !== "string" || Buffer.byteLength(body, "utf8") > 65536) return null + try { + let remaining = body + const rules: Record[] = [] + while (remaining.startsWith("if (")) { + const block = /^if \(([^)\n]+)\) \{\n((?: [^\n]+\n)*)\}\n/.exec(remaining) + if (!block || rules.length >= 500) return null + const exact = /^dst == (\S+)$/.exec(block[1]), subnet = /^dst in (\S+) && dst-len (?:== (\d+)|in (\d+)-(\d+))$/.exec(block[1]) + if (!exact && !subnet) return null + const rule: Record = exact ? { prefix: exact[1] } : { prefix: subnet![1], match: "subnet", minLength: Number(subnet![2] ?? subnet![3]), maxLength: Number(subnet![2] ?? subnet![4]) } + const lines = block[2].trimEnd().split("\n") + const assignment = /^ (set|append) bgp-communities (\d{1,5}:\d{1,5}(?:,\d{1,5}:\d{1,5})*);$/.exec(lines[0]) + if (assignment) { rule.communityMode = assignment[1]; rule.communities = assignment[2].split(","); lines.shift() } + else if (lines[0] === " delete bgp-communities wk,other;") { rule.communityMode = "clear"; lines.shift() } + if (!lines.length) rule.action = "continue" + else if (lines.length === 1 && /^ (accept|reject);$/.test(lines[0])) rule.action = lines[0].trim().slice(0, -1) + else return null + rules.push(rule); remaining = remaining.slice(block[0].length) + } + if (remaining !== "accept;" && remaining !== "reject;") return null + const compiled = compileBgpCommunityPolicy({ policyId, rules, defaultAction: remaining.slice(0, -1) }) + return compiled.body === body ? compiled.policy : null + } catch { return null } +} diff --git a/backend/src/services/bgp-connection-read.test.ts b/backend/src/services/bgp-connection-read.test.ts new file mode 100644 index 00000000..0e81e210 --- /dev/null +++ b/backend/src/services/bgp-connection-read.test.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import { readStaticBgpConnection } from "./bgp-connection-read.js" +import type { MikrotikClient } from "./mikrotik.js" + +let rows: unknown = [{ ".id": "*p", disabled: "false" }], dynamic: unknown = [], disabled: unknown = { ret: "false" }, selection: unknown = [{ ".id": "*p" }], posts = 0 +const client: Pick = { + get: async () => structuredClone(rows) as T, + post: async (path: string, body: Record) => { + posts++ + if (path.endsWith("/print")) { assert.deepEqual(body, { ".proplist": ".id,dynamic", ".query": [".id=*p", "-dynamic", "#&"] }); return structuredClone(selection) as T } + return structuredClone(body["value-name"] === "dynamic" ? dynamic : disabled) as T + }, +} +assert.equal((await readStaticBgpConnection(client, "*p")).connection.dynamic, "false") +rows = [{ ".id": "*p", dynamic: false }]; assert.equal((await readStaticBgpConnection(client, "*p")).connection.disabled, "false") +for (const value of [true, "true", "yes"]) { rows = [{ ".id": "*p", disabled: "false", dynamic: value }]; await assert.rejects(readStaticBgpConnection(client, "*p"), (error: { code?: string }) => error.code === "STATE_CONFLICT") } +for (const value of ["?", "", 0, null]) { rows = [{ ".id": "*p", disabled: "false", dynamic: value }]; await assert.rejects(readStaticBgpConnection(client, "*p"), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") } +rows = [{ ".id": "*p", disabled: "?", dynamic: "false" }]; await assert.rejects(readStaticBgpConnection(client, "*p")) +rows = [{ ".id": "*p", disabled: "false" }]; selection = [{ ".id": "*other" }]; await assert.rejects(readStaticBgpConnection(client, "*p")) +selection = [{ ".id": "*p" }]; dynamic = { ret: false }; await assert.rejects(readStaticBgpConnection(client, "*p")) +dynamic = { ret: "false" }; assert.equal((await readStaticBgpConnection(client, "*p")).connection.dynamic, "false") +rows = [{ ".id": "*p", dynamic: "false" }]; disabled = []; await assert.rejects(readStaticBgpConnection(client, "*p")) +const before = posts; await assert.rejects(readStaticBgpConnection(client, "*missing")); assert.equal(posts, before) +console.log("Static BGP reader: exact D-flag negative selection, known bool normalization, dynamic vs unknown refusal, missing effective disabled, malformed get/selection and no property reads for a missing peer") diff --git a/backend/src/services/bgp-connection-read.ts b/backend/src/services/bgp-connection-read.ts new file mode 100644 index 00000000..d8841eec --- /dev/null +++ b/backend/src/services/bgp-connection-read.ts @@ -0,0 +1,28 @@ +import type { MikrotikClient } from "./mikrotik.js" +import { nativeBgpRows, type NativeBgpRow } from "./bgp-export-model.js" +import { readOspfEffectiveDisabled } from "./ospf-effective-disabled.js" +import { McpError } from "../modules/mcp/security.js" + +/** Prove a configured peer, including RouterOS releases that omit the D flag. */ +export async function readStaticBgpConnection(client: Pick, id: string) { + const connections = nativeBgpRows(await client.get("/routing/bgp/connection")), target = connections.find(row => row[".id"] === id) + if (!target) throw new McpError("NOT_FOUND", "BGP-подключение не найдено", 404) + const [connection] = await readOspfEffectiveDisabled([target], nativeId => client.post("/routing/bgp/connection/get", { ".id": nativeId, "value-name": "disabled" })) + if (!["false", "no", false, "true", "yes", true].includes(connection.disabled as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён disabled BGP-подключения", 409) + connection.disabled = ["true", "yes", true].includes(connection.disabled as string | boolean) ? "true" : "false" + if (connection.dynamic === undefined) { + const value = await client.post("/routing/bgp/connection/get", { ".id": id, "value-name": "dynamic" }) + if (Array.isArray(value) && value.length === 0) { + const selected = nativeBgpRows(await client.post("/routing/bgp/connection/print", { ".proplist": ".id,dynamic", ".query": [".id=" + id, "-dynamic", "#&"] })) + if (selected.length !== 1 || selected[0][".id"] !== id || selected[0].dynamic !== undefined && !["false", "no", false].includes(selected[0].dynamic as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён static/dynamic BGP-подключения по native query", 409) + connection.dynamic = "false" + } else { + if (!value || typeof value !== "object" || !("ret" in value) || typeof value.ret !== "string" || !["true", "false", "yes", "no"].includes(value.ret)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён static/dynamic BGP-подключения", 409) + connection.dynamic = value.ret + } + } + if (["true", "yes", true].includes(connection.dynamic as string | boolean)) throw new McpError("STATE_CONFLICT", "Dynamic BGP connection не изменяется", 409) + if (!["false", "no", false].includes(connection.dynamic as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён static/dynamic BGP-подключения", 409) + connection.dynamic = "false" + return { connection, connections } +} diff --git a/backend/src/services/bgp-export-model.test.ts b/backend/src/services/bgp-export-model.test.ts new file mode 100644 index 00000000..c9bc27fb --- /dev/null +++ b/backend/src/services/bgp-export-model.test.ts @@ -0,0 +1,32 @@ +import assert from "node:assert/strict" +import { bgpExportConnection, bgpExportSources, bgpRedistributionMode, bgpTemplateReferences, nativeBgpExportValue, nativeBgpRows } from "./bgp-export-model.js" + +assert.equal(bgpRedistributionMode({ ret: "" }), "disabled") +assert.equal(bgpRedistributionMode([]), null) +assert.deepEqual(bgpTemplateReferences([]), []) +assert.deepEqual(bgpTemplateReferences({ ret: "default" }), ["default"]) +assert.deepEqual(bgpTemplateReferences({ ret: ["parent-a", "parent-b"] }), ["parent-a", "parent-b"]) +for (const value of [{}, { ret: null }, { ret: "a,,b" }, { ret: "a,a" }, [{}]]) assert.throws(() => bgpTemplateReferences(value)) +assert.equal(bgpRedistributionMode({ ret: "static" }), "static") +assert.equal(bgpRedistributionMode({ ret: "connected" }), "connected") +for (const value of [{}, { ret: null }, { ret: false }, { ret: "connected,static" }, { ret: "ospf" }]) assert.throws(() => bgpRedistributionMode(value)) +assert.equal(nativeBgpExportValue("disabled"), "") +for (const id of ["all", "..", "*bad/id", "*bad?x", "*", "0"]) assert.throws(() => nativeBgpRows([{ ".id": id }])) +const connected = { ".id": "*c", "routing-table": "lab", "dst-address": "192.0.2.0/30", connect: "true", dynamic: "true", active: "true", "debug.ptr": "ignored" } +const configured = { ".id": "*s", "routing-table": "lab", "dst-address": "198.18.0.1/32", static: "true", dynamic: "false" } +const bgp = { ".id": "*b", "routing-table": "lab", bgp: "true", dynamic: "true" } +const outside = { ".id": "*x", "routing-table": "main", "dst-address": "invalid" } +let sources = bgpExportSources([outside, bgp, configured, connected], "lab", ["connected"], 4) +assert.equal(sources.length, 1); assert.equal(sources[0].active, true); assert.equal(sources[0].kind, "connected"); assert.equal("debug.ptr" in sources[0].proof, false) +sources = bgpExportSources([connected, configured], "lab", ["static"], 4) +assert.equal(sources[0].active, null); assert.equal(sources[0].kind, "static") +assert.equal(bgpExportSources([connected, configured], "lab", ["connected", "static"], 4).length, 2) +assert.equal(bgpExportSources([connected], "lab", ["disabled"], 4).length, 0) +for (const payload of [[connected, connected], [{ ...connected, dynamic: "false" }], [{ ...connected, connect: undefined }], [{ ...connected, "dst-address": "192.0.2.0/33" }], [{ ...connected, "routing-table": undefined }]]) assert.throws(() => bgpExportSources(payload, "lab", ["connected"], 4)) +assert.equal(bgpExportSources([{ ...connected, "dst-address": "fd00::/64" }], "lab", ["connected"], 6).length, 1) +const connection = { ".id": "*peer", name: "lab-peer", dynamic: "false", disabled: "false", instance: "lab", vrf: "lab", "routing-table": "lab", inactive: "true" } +assert.equal(bgpExportConnection(connection, "static")["output.redistribute"], "static") +assert.equal("inactive" in bgpExportConnection(connection, "static"), false) +assert.equal(bgpExportConnection({ ...connection, disabled: "true" }, "static").disabled, "true") +for (const change of [{ disabled: undefined }, { vrf: undefined }, { instance: undefined }, { dynamic: "true" }, { dynamic: undefined }]) assert.throws(() => bgpExportConnection({ ...connection, ...change }, "static")) +console.log("BGP export sources: effective empty/connected/static values, explicit VRF/table, native origin provenance, v4/v6 prefixes, unknown and duplicate rejection, stable config versus session status") diff --git a/backend/src/services/bgp-export-model.ts b/backend/src/services/bgp-export-model.ts new file mode 100644 index 00000000..3639da72 --- /dev/null +++ b/backend/src/services/bgp-export-model.ts @@ -0,0 +1,65 @@ +import { isIP } from "node:net" + +export type BgpExportMode = "disabled" | "connected" | "static" +export type NativeBgpRow = Record & { ".id": string } +const flag = (value: unknown) => value === true || value === "true" || value === "yes" ? true : value === false || value === "false" || value === "no" ? false : null +const fail = () => new Error("Не подтверждён источник экспорта BGP") + +export function nativeBgpRows(payload: unknown): NativeBgpRow[] { + if (!Array.isArray(payload) || payload.length > 10000 || payload.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !/^\*[A-Za-z0-9]+$/.test(row[".id"]) || row[".id"].length > 200)) throw fail() + if (new Set(payload.map(row => row[".id"])).size !== payload.length) throw fail() + return payload.map(row => ({ ...row })) +} +/** RouterOS /get supplies the effective value, including an explicit empty value. */ +export function bgpRedistributionMode(response: unknown): BgpExportMode | null { + // A successful /get with no !done data is nil, not an explicit empty override. + if (Array.isArray(response) && response.length === 0) return null + if (!response || typeof response !== "object" || !("ret" in response) || typeof response.ret !== "string") throw fail() + if (response.ret === "") return "disabled" + if (response.ret === "connected" || response.ret === "static") return response.ret + throw new Error("Текущий экспорт BGP содержит неподдерживаемый или смешанный набор источников") +} +export function bgpTemplateReferences(response: unknown): string[] { + if (Array.isArray(response) && response.length === 0) return [] + if (!response || typeof response !== "object" || !("ret" in response)) throw fail() + const value = response.ret + const refs = typeof value === "string" ? value === "" ? [] : value.split(",") : Array.isArray(value) ? value : null + if (!refs || refs.length > 100 || refs.some(ref => typeof ref !== "string" || !ref.trim() || ref.length > 200)) throw fail() + const result = refs.map(ref => (ref as string).trim()) + if (new Set(result).size !== result.length) throw fail() + return result +} +export function nativeBgpExportValue(mode: BgpExportMode): string { return mode === "disabled" ? "" : mode } + +/** Positive native origin flags select sources; a missing flag is not a known false. + * Unknown origins in the selected table fail closed, instead of disappearing. */ +export function bgpExportSources(payload: unknown, table: string, kinds: readonly BgpExportMode[], family: 4 | 6) { + const rows = nativeBgpRows(payload), modes = new Set(kinds.filter(mode => mode !== "disabled")) + if (!modes.size) return [] + const selected: { id: string; family: 4 | 6; table: string; prefix: string; kind: "connected" | "static"; active: boolean | null; proof: Record }[] = [] + for (const row of rows) { + if (typeof row["routing-table"] !== "string" || !row["routing-table"]) throw fail() + if (row["routing-table"] !== table) continue + const connected = flag(row.connect), configured = flag(row.static), dynamic = flag(row.dynamic) + if (connected === true && configured === true || connected === true && dynamic !== true || configured === true && dynamic !== false) throw fail() + const kind = connected === true ? "connected" : configured === true ? "static" : null + if (!kind) { + if (!["bgp", "ospf", "rip", "dhcp", "vpn", "modem", "isis", "bgp-mpls-vpn"].some(key => flag(row[key]) === true)) throw fail() + continue + } + if (!modes.has(kind)) continue + if (typeof row["dst-address"] !== "string" || row["dst-address"].length > 300) throw fail() + const [address, prefix, extra] = row["dst-address"].split("/") + if (extra !== undefined || isIP(address) !== family || prefix === undefined || !/^\d+$/.test(prefix) || Number(prefix) > (family === 4 ? 32 : 128)) throw fail() + // Preserve all source attributes in a private proof except provider debug pointers. + const proof = Object.fromEntries(Object.entries(row).filter(([key]) => !key.startsWith("debug."))) + selected.push({ id: row[".id"], family, table, prefix: row["dst-address"], kind, active: flag(row.active), proof }) + } + return selected.sort((a, b) => a.id.localeCompare(b.id)) +} + +export function bgpExportConnection(row: NativeBgpRow, mode: BgpExportMode): NativeBgpRow { + if (flag(row.dynamic) !== false || flag(row.disabled) === null || [row.instance, row.vrf, row["routing-table"]].some(value => typeof value !== "string" || !value.trim() || value.length > 100)) throw new Error("Экспорт BGP требует подтверждённое статическое подключение с известным disabled и явными instance, VRF и таблицей") + // Inactive is a session status; it is not a change to connection configuration. + return { ...Object.fromEntries(Object.entries(row).filter(([key]) => !["inactive", "invalid", "running"].includes(key) && !key.startsWith("debug."))), ".id": row[".id"], dynamic: "false", disabled: flag(row.disabled) ? "true" : "false", "output.redistribute": nativeBgpExportValue(mode) } +} diff --git a/backend/src/services/bgp-filter-binding-model.test.ts b/backend/src/services/bgp-filter-binding-model.test.ts new file mode 100644 index 00000000..88fb33d4 --- /dev/null +++ b/backend/src/services/bgp-filter-binding-model.test.ts @@ -0,0 +1,10 @@ +import assert from "node:assert/strict" +import { bgpFilterName, bgpFilterProperty, effectiveBgpFilter, stableBgpPolicyRow } from "./bgp-filter-binding-model.js" +assert.equal(bgpFilterProperty("input"), "input.filter"); assert.equal(bgpFilterProperty("output"), "output.filter-chain") +assert.equal(bgpFilterName([]), null); assert.equal(bgpFilterName({ ret: "" }), ""); assert.equal(bgpFilterName({ ret: "Chain Case" }), "Chain Case") +for (const value of [null, {}, ["x"], { ret: false }, { ret: "x\n" }, { ret: "x".repeat(101) }]) assert.throws(() => bgpFilterName(value)) +assert.equal(effectiveBgpFilter(null, []), ""); assert.equal(effectiveBgpFilter(null, ["in", "in"]), "in") +assert.equal(effectiveBgpFilter("", ["in"]), ""); assert.equal(effectiveBgpFilter("override", ["a", "b"]), "override") +assert.throws(() => effectiveBgpFilter(null, ["a", "b"])) +assert.deepEqual(stableBgpPolicyRow({ ".id": "*1", rule: "opaque", disabled: "false", packets: "123", "debug.ptr": "x", running: "true" }), { ".id": "*1", rule: "opaque", disabled: "false" }) +console.log("BGP filter binding: native property names, nil vs explicit empty, inherited conflict/local override, strict bounded names and stable opaque policy proof") diff --git a/backend/src/services/bgp-filter-binding-model.ts b/backend/src/services/bgp-filter-binding-model.ts new file mode 100644 index 00000000..096160c7 --- /dev/null +++ b/backend/src/services/bgp-filter-binding-model.ts @@ -0,0 +1,13 @@ +export type BgpFilterDirection = "input" | "output" +export const bgpFilterProperty = (direction: BgpFilterDirection) => direction === "input" ? "input.filter" : "output.filter-chain" +export function bgpFilterName(response: unknown): string | null { + if (Array.isArray(response) && response.length === 0) return null + if (!response || typeof response !== "object" || !("ret" in response) || typeof response.ret !== "string" || response.ret.length > 100 || /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(response.ret)) throw new Error("Не подтверждена привязка BGP filter") + return response.ret +} +export function effectiveBgpFilter(local: string | null, parents: readonly string[]) { + if (local !== null) return local + if (new Set(parents).size > 1) throw new Error("Шаблоны задают неоднозначную цепочку BGP") + return parents[0] ?? "" +} +export const stableBgpPolicyRow = (row: Record) => Object.fromEntries(Object.entries(row).filter(([key]) => !["inactive", "invalid", "running", "match-count", "matched", "hits", "packets", "bytes"].includes(key) && !key.startsWith("debug."))) diff --git a/backend/src/services/bgp-prefix-model.test.ts b/backend/src/services/bgp-prefix-model.test.ts new file mode 100644 index 00000000..2d48d6df --- /dev/null +++ b/backend/src/services/bgp-prefix-model.test.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict" +import { bgpPrefixRows } from "./bgp-prefix-model.js" +const row = { ".id": "*b", "routing-table": "lab", "dst-address": "192.0.2.0/24", bgp: "true", dynamic: "true", distance: "0", password: "NEVER_PREFIX_SECRET" } +assert.deepEqual(bgpPrefixRows([row], "lab", 4), [{ id: "*b", family: 4, table: "lab", prefix: "192.0.2.0/24", active: null, distance: 0 }]) +assert.equal(bgpPrefixRows([{ ...row, active: "false" }], "lab", 4)[0].active, false) +assert.equal(bgpPrefixRows([{ ...row, active: "true", distance: "20" }], "lab", 4)[0].active, true) +assert.equal(bgpPrefixRows([{ ...row, "dst-address": "fd00::1/128" }], "lab", 6).length, 1) +assert.deepEqual(bgpPrefixRows([{ ...row, "routing-table": "other", "dst-address": "invalid" }, { ...row, ".id": "*c", bgp: undefined, connect: "true" }], "lab", 4), []) +for (const change of [{ dynamic: undefined }, { dynamic: "false" }, { static: "true" }, { "dst-address": "192.0.2.0/33" }, { "dst-address": "::1/128" }, { bgp: undefined }, { "routing-table": undefined }]) assert.throws(() => bgpPrefixRows([{ ...row, ...change }], "lab", 4)) +assert.throws(() => bgpPrefixRows([row, row], "lab", 4)) +assert.throws(() => bgpPrefixRows(Array.from({ length: 10001 }, (_, index) => ({ ...row, ".id": "*" + index })), "lab", 4)) +console.log("BGP prefixes: native provenance, known zero/unknown active, bounded complete source, v4/v6, duplicate/ambiguous rejection and private field omission") diff --git a/backend/src/services/bgp-prefix-model.ts b/backend/src/services/bgp-prefix-model.ts new file mode 100644 index 00000000..107172d8 --- /dev/null +++ b/backend/src/services/bgp-prefix-model.ts @@ -0,0 +1,21 @@ +import { isIP } from "node:net" +import { nativeBgpRows } from "./bgp-export-model.js" + +const flag = (value: unknown) => value === true || value === "true" || value === "yes" ? true : value === false || value === "false" || value === "no" ? false : null +/** A bounded route view, not a session's received/advertised attributes table. */ +export function bgpPrefixRows(payload: unknown, table: string, family: 4 | 6) { + return nativeBgpRows(payload).flatMap(row => { + if (typeof row["routing-table"] !== "string" || !row["routing-table"]) throw new Error("Unknown route table") + if (row["routing-table"] !== table) return [] + const other = ["static", "connect", "ospf", "rip", "dhcp", "vpn", "modem", "isis", "bgp-mpls-vpn", "slaac", "fantasy"].some(key => flag(row[key]) === true) + if (flag(row.bgp) !== true) { + if (!other && flag(row.bgp) !== false) throw new Error("Unknown route origin") + return [] + } + if (flag(row.dynamic) !== true || other || typeof row["dst-address"] !== "string" || row["dst-address"].length > 300) throw new Error("Unconfirmed BGP route") + const [address, bits, extra] = row["dst-address"].split("/") + if (extra !== undefined || isIP(address) !== family || address.includes("%") || bits === undefined || !/^\d+$/.test(bits) || Number(bits) > (family === 4 ? 32 : 128)) throw new Error("Invalid BGP prefix") + const distance = typeof row.distance === "string" && /^\d+$/.test(row.distance) && Number(row.distance) <= 255 ? Number(row.distance) : null + return [{ id: row[".id"], family, table, prefix: row["dst-address"], active: flag(row.active), distance }] + }).sort((a, b) => a.prefix.localeCompare(b.prefix) || a.id.localeCompare(b.id)) +} diff --git a/backend/src/services/bgp-session-read.ts b/backend/src/services/bgp-session-read.ts new file mode 100644 index 00000000..83423ce9 --- /dev/null +++ b/backend/src/services/bgp-session-read.ts @@ -0,0 +1,19 @@ +import { MikrotikClient } from "./mikrotik.js" +import { parseBgpSessions, parseBgpCatalogSessions } from "./bgp-parse-sessions.js" +import { readInternetPathRows } from "./internet-path-snapshot.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import type { RosBgpSession } from "../types/server.js" + +export async function readRawBgpSessionsForServer(server: ServerRow): Promise[]> { + if (!server.enabled) throw Object.assign(new Error("Сервер выключен. Опрос BGP не выполнялся"), { statusCode: 409 }) + const raw: unknown = await MikrotikClient.fromServer(server).getBgpSessionsRaw() + if (!Array.isArray(raw) || raw.some(row => !row || typeof row !== "object" || Array.isArray(row))) throw new Error("Некорректный список BGP sessions") + return raw +} +async function rowsForParsing(server: ServerRow) { + const rows = readInternetPathRows(await readRawBgpSessionsForServer(server), "BGP sessions", [".id"]) + if (new Set(rows.map(row => row[".id"])).size !== rows.length) throw new Error("Повторяющиеся ID BGP-сессий") + return rows as unknown as RosBgpSession[] +} +export async function readBgpSessionsForServer(server: ServerRow) { return parseBgpSessions(server, await rowsForParsing(server)) } +export async function readBgpCatalogForServer(server: ServerRow) { return parseBgpCatalogSessions(server, await rowsForParsing(server)) } diff --git a/backend/src/services/bounded-database-dump.test.ts b/backend/src/services/bounded-database-dump.test.ts new file mode 100644 index 00000000..4dc17f52 --- /dev/null +++ b/backend/src/services/bounded-database-dump.test.ts @@ -0,0 +1,35 @@ +import assert from "node:assert/strict" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import os from "node:os" +import path from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { boundedDatabaseCommand, MAX_DATABASE_DUMP_BYTES } from "./bounded-database-dump.js" + +const run = (script: string, options: Parameters[2] = {}) => boundedDatabaseCommand(process.execPath, ["-e", script], options) +assert.deepEqual(await run("process.stdout.write(Buffer.from([80,71,68,77,80,0,255,128]))"), Buffer.from([80,71,68,77,80,0,255,128])) +assert.throws(() => run("", { maxBytes: MAX_DATABASE_DUMP_BYTES + 1 })) +const cancelled = new AbortController(); cancelled.abort() +await assert.rejects(run("throw Error('must not run')", { signal: cancelled.signal }), /отменён/) +await assert.rejects(run("process.stderr.write('postgresql://user:password@host/database');process.exit(2)"), error => error instanceof Error && !/password|postgresql|database/.test(error.message)) +await assert.rejects(boundedDatabaseCommand(path.join(os.tmpdir(), "missing-db-command-" + Date.now()), []), /недоступна/) +const directory = await mkdtemp(path.join(os.tmpdir(), "mm-bounded-dump-")) +try { + for (const reason of ["cancel", "timeout", "stdout", "stderr", "authority"] as const) { + const pidfile = path.join(directory, reason), controller = new AbortController() + let checks = 0 + const pending = run(`require('node:fs').writeFileSync(${JSON.stringify(pidfile)},String(process.pid));setInterval(()=>{${reason === "stdout" ? "process.stdout.write(Buffer.alloc(4096))" : reason === "stderr" ? "process.stderr.write(Buffer.alloc(4096))" : ""}},10)`, { + signal: controller.signal, maxBytes: reason === "stdout" ? 1024 : undefined, timeoutMs: reason === "timeout" ? 1500 : 10000, + authorize: reason === "authority" ? async () => { checks++; throw Error("credential secret must never surface") } : undefined, + }) + // Attach immediately so a fast child failure is never an unhandled rejection. + const rejected = assert.rejects(pending, error => error instanceof Error && !error.message.includes("secret")) + let pid = 0 + for (let i = 0; i < 100; i++) { try { pid = Number(await readFile(pidfile, "utf8")); break } catch { await delay(10) } } + assert.ok(pid > 0, "Real child reached startup") + if (reason === "cancel") controller.abort() + await rejected + assert.throws(() => process.kill(pid, 0), "Cancellation must settle only after the child has closed") + if (reason === "authority") assert.ok(checks > 0) + } +} finally { await rm(directory, { recursive: true, force: true }) } +console.log("bounded database dump real-child tests passed: binary, limits, cancellation, deadline, authority, sanitized diagnostics and drained child") diff --git a/backend/src/services/bounded-database-dump.ts b/backend/src/services/bounded-database-dump.ts new file mode 100644 index 00000000..9066d2ba --- /dev/null +++ b/backend/src/services/bounded-database-dump.ts @@ -0,0 +1,43 @@ +import { spawn } from "node:child_process" + +export const MAX_DATABASE_DUMP_BYTES = 64 * 1024 * 1024 +export class DatabaseDumpCommandError extends Error { + constructor(readonly code: string, message: string, readonly diagnosticKinds: string[] = [], readonly exitCode?: number | null) { super(message) } +} +/** Collect only bounded output; every cancellation kills and drains before settling. */ +export function boundedDatabaseCommand(command: string, args: string[], options: { env?: NodeJS.ProcessEnv; signal?: AbortSignal; maxBytes?: number; timeoutMs?: number; authorize?: () => Promise } = {}): Promise { + const maxBytes = options.maxBytes ?? MAX_DATABASE_DUMP_BYTES + if (!Number.isSafeInteger(maxBytes) || maxBytes < 1 || maxBytes > MAX_DATABASE_DUMP_BYTES) throw new Error("Invalid database command limit") + return new Promise((resolve, reject) => { + if (options.signal?.aborted) { reject(new Error("Экспорт базы отменён")); return } + const child = spawn(command, args, { env: options.env, stdio: ["ignore", "pipe", "pipe"], shell: false, windowsHide: true }) + const chunks: Buffer[] = [] + let bytes = 0, stderrBytes = 0, failure: Error | undefined + const diagnosticKinds = new Set() + const stop = (message: string, code: string) => { if (!failure) { failure = new DatabaseDumpCommandError(code, message); chunks.length = 0; child.kill("SIGKILL") } } + const abort = () => stop("Экспорт базы отменён", "DATABASE_DUMP_CANCELLED") + const timer = setTimeout(() => stop("Истёк срок экспорта базы", "DATABASE_DUMP_TIMEOUT"), options.timeoutMs ?? 120000) + let checking = false + const accessTimer = options.authorize ? setInterval(() => { + if (checking || failure) return + checking = true + void options.authorize!().catch(() => stop("Допуск к экспорту базы отозван", "DATABASE_DUMP_ACCESS_REVOKED")).finally(() => { checking = false }) + }, 250) : undefined + options.signal?.addEventListener("abort", abort, { once: true }) + child.stdout.on("data", (chunk: Buffer) => { bytes += chunk.length; if (bytes > maxBytes) stop("Архив базы превышает 64 МиБ", "DATABASE_DUMP_SIZE_LIMIT"); else if (!failure) chunks.push(chunk) }) + // Drain stderr without retaining PostgreSQL diagnostics, URLs or passwords. + child.stderr.on("data", (chunk: Buffer) => { + stderrBytes += chunk.length + // Retain only a fixed allowlist of categories, never raw diagnostics. + const text = chunk.toString("utf8").toLowerCase() + for (const [kind, pattern] of [["version", /version mismatch|server version/], ["authentication", /authentication failed|no password supplied/], ["connection", /could not connect|connection refused|connection to server/], ["tls", /certificate verify failed|certificate verification failed/], ["permission", /permission denied/], ["lock", /lock timeout|deadlock detected/]] as const) if (pattern.test(text)) diagnosticKinds.add(kind) + if (stderrBytes > 65536) stop("Диагностика экспорта базы превысила предел", "DATABASE_DUMP_STDERR_LIMIT") + }) + child.on("error", () => { failure ??= new DatabaseDumpCommandError("DATABASE_DUMP_TOOL_UNAVAILABLE", "Утилита PostgreSQL недоступна") }) + child.on("close", code => { + clearTimeout(timer); if (accessTimer) clearInterval(accessTimer); options.signal?.removeEventListener("abort", abort) + if (failure || code !== 0) reject(failure ?? new DatabaseDumpCommandError("DATABASE_DUMP_EXIT", "Экспорт PostgreSQL не завершён", [...diagnosticKinds], code)) + else resolve(Buffer.concat(chunks, bytes)) + }) + }) +} diff --git a/backend/src/services/bridge-vlan-configuration.test.ts b/backend/src/services/bridge-vlan-configuration.test.ts new file mode 100644 index 00000000..f0c652ec --- /dev/null +++ b/backend/src/services/bridge-vlan-configuration.test.ts @@ -0,0 +1,332 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { bridgeVlanConfigurationRequestSchema } from "@mmapp/contracts/bridge-vlan" +import { buildBridgeVlanConfiguration, bridgeVlanCurrentDesired, bridgeVlanInverseDesired, parseBridgeVlanIds, type BridgeVlanDesired, type BridgeVlanState } from "./bridge-vlan-configuration.js" + +const cpu = { resource: "bridge", id: "*1" }, access = { resource: "ethernet", id: "*2" }, trunk = { resource: "ethernet", id: "*3" } +function fixture() { + const state: BridgeVlanState = { + bridges: [{ ".id": "*1", name: "br-users", disabled: "false", pvid: "1", "vlan-filtering": "false", "frame-types": "admit-all", "ingress-filtering": "true", mvrp: "false" }], + interfaces: [ + { ...cpu, name: "br-users", disabled: "false" }, { ...access, name: "ether2", disabled: "false" }, { ...trunk, name: "ether3", disabled: "false" }, + { resource: "ethernet", id: "*4", name: "ether-mgmt", disabled: "false" }, + { resource: "vlan", id: "*5", name: "mgmt-vlan", disabled: "false" }, + ], + ports: [{ ".id": "*10", bridge: "br-users", interface: "ether2", pvid: "10", "ingress-filtering": "true", "frame-types": "admit-all", disabled: "false" }], + vlans: [], vlanInterfaces: [], + ipv4Addresses: [{ ".id": "*20", address: "192.0.2.1/24", interface: "ether-mgmt", disabled: "false" }], ipv6Addresses: [], + } + const desired: BridgeVlanDesired = { + filtering: true, pvid: 1, + ports: [{ id: "*10", interface: access, pvid: 10, ingressFiltering: true, frameTypes: "admit-all", disabled: false }, { interface: trunk, pvid: 1, ingressFiltering: true, frameTypes: "admit-only-vlan-tagged", disabled: false }], + vlans: [{ vlanIds: "10", tagged: [cpu, trunk], untagged: [access], disabled: false }], removePortIds: [], removeVlanIds: [], + } + return structuredClone({ serverId: 1, bridgeId: "*1", state, desired, management: { serverId: 1, channelId: "verified-channel", host: "192.0.2.1", expiresAt: "2026-11-05T01:00:00Z" }, now: Date.parse("2026-11-05T00:00:00Z") }) +} +const build = (change?: (value: ReturnType) => void) => { const value = fixture(); change?.(value); return buildBridgeVlanConfiguration(value) } + +test("enable creates VLAN and port before filtering, stable IDs and foreign rows retained", () => { + const value = fixture(); value.state.ports.push({ ".id": "*99", bridge: "foreign", interface: "foreign-port" }) + const original = structuredClone(value), plan = buildBridgeVlanConfiguration(value) + assert.deepEqual(plan.steps.map(step => [step.resource, step.action]), [["bridge_vlan", "create"], ["bridge_port", "create"], ["bridge", "update"]]) + assert.deepEqual(plan.steps.at(-1)?.fields, { "vlan-filtering": "true" }) + assert.equal(plan.steps[0].fields.tagged, "br-users,ether3") + assert.ok(plan.objects.some(object => object.kind === "bridge_port" && object.id === "*10")) + assert.deepEqual(value, original, "preparation must be pure") +}) +test("strict VID parsing includes arbitrary ranges and rejects overlaps and invalid syntax", () => { + assert.deepEqual(parseBridgeVlanIds("4094,11-13,1"), [1, 11, 12, 13, 4094]) + for (const bad of ["", "0", "4095", "10-9", "1,1", "1-3,3-4", "01", "1, 2", "1-", "+1", "1.1", "1,,2"]) assert.throws(() => parseBridgeVlanIds(bad), undefined, bad) +}) +test("public schema rejects management evidence, unknown flags, and fractional PVID", () => { + const value = fixture(), request = { serverId: value.serverId, bridgeId: value.bridgeId, desired: value.desired } + assert.ok(bridgeVlanConfigurationRequestSchema.safeParse(request).success) + assert.ok(!bridgeVlanConfigurationRequestSchema.safeParse({ ...request, management: value.management }).success) + assert.ok(!bridgeVlanConfigurationRequestSchema.safeParse({ ...request, desired: { ...request.desired, managementSafe: true } }).success) + assert.ok(!bridgeVlanConfigurationRequestSchema.safeParse({ ...request, desired: { ...request.desired, pvid: 1.5 } }).success) +}) +test("full desired set cannot silently omit existing rows or claim a foreign row", () => { + assert.throws(() => build(value => { value.desired.ports.shift() }), /полный список/) + assert.throws(() => build(value => { value.desired.ports[0].id = "*99" }), /полный список/) + assert.throws(() => build(value => { value.desired.removePortIds.push("*10") }), /Повтор/) +}) +test("cross-bridge memberships and foreign slaves are refused", () => { + assert.throws(() => build(value => { value.desired.vlans[0].tagged.push({ resource: "ethernet", id: "*4" }) }), /не принадлежит/) + assert.throws(() => build(value => { value.state.ports.push({ ".id": "*98", interface: "ether3", bridge: "foreign" }) }), /другому bridge/) + assert.throws(() => build(value => { value.desired.ports[1].interface = cpu }), /является bridge/) +}) +test("effective tagged/untagged overlap and duplicate membership are refused", () => { + assert.throws(() => build(value => { value.desired.vlans[0].tagged.push(access) }), /Повтор/) + assert.throws(() => build(value => { value.desired.vlans.push(structuredClone(value.desired.vlans[0])) }), /Неоднозначная/) +}) +test("explicit untagged VLAN must agree with PVID; omitted untagged uses native PVID membership", () => { + assert.throws(() => build(value => { value.desired.ports[0].pvid = 11 }), /не соответствует PVID/) + assert.ok(build(value => { value.desired.vlans[0].untagged = [] }).steps.length > 0) + assert.throws(() => build(value => { value.desired.vlans[0].vlanIds = "10-11" }), /не соответствует PVID/) +}) + +function implicitPvidManagement() { + const value = fixture() + value.state.bridges[0]["vlan-filtering"] = "true"; value.state.bridges[0].pvid = "10" + value.state.ipv4Addresses[0].interface = "br-users" + value.state.vlans = [{ ".id": "*30", bridge: "br-users", dynamic: "true", "vlan-ids": "10", "current-untagged": "br-users,ether2" }] + value.desired = bridgeVlanCurrentDesired(value.state, value.bridgeId) + return value +} +test("implicit PVID CPU and access membership supports a safe edit without static VLAN commands", () => { + const value = implicitPvidManagement(); value.desired.ports[0].ingressFiltering = false + const plan = buildBridgeVlanConfiguration(value) + assert.deepEqual(plan.steps, [{ serverId: 1, resource: "bridge_port", action: "update", id: "*10", fields: { "ingress-filtering": "false" } }]) + assert.equal(plan.managementPath.bridgeName, "br-users") + assert.deepEqual(plan.inverse.desired.vlans, []) +}) +test("implicit management rejects CPU/port PVID migration and CPU frame admission loss", () => { + for (const change of [ + (value: ReturnType) => { value.desired.pvid = 11 }, + (value: ReturnType) => { value.desired.ports[0].pvid = 11 }, + (value: ReturnType) => { value.desired.frameTypes = "admit-only-vlan-tagged" }, + ]) { + const value = implicitPvidManagement(); change(value) + assert.throws(() => buildBridgeVlanConfiguration(value), /CPU management|management/) + } +}) +test("CPU observation is required and MVRP dynamic registration is unsupported", () => { + for (const field of ["frame-types", "ingress-filtering"]) { + const value = implicitPvidManagement(); delete value.state.bridges[0][field] + assert.throws(() => buildBridgeVlanConfiguration(value)) + } + const value = implicitPvidManagement(); value.state.bridges[0].mvrp = "true" + assert.throws(() => buildBridgeVlanConfiguration(value), error => error instanceof Error && "code" in error && error.code === "UNSUPPORTED_CAPABILITY") +}) +test("tagged-only CPU does not acquire implicit PVID membership and untagged-only CPU cannot serve a VLAN interface", () => { + const value = fixture(); value.state.bridges[0]["frame-types"] = "admit-only-vlan-tagged"; value.desired.pvid = 10 + assert.ok(buildBridgeVlanConfiguration(value).steps.length > 0) + const tagged = implicitPvidManagement() + tagged.state.vlans = [{ ".id": "*11", bridge: "br-users", "vlan-ids": "10", tagged: "br-users,ether2", disabled: "false" }] + tagged.state.ports[0]["frame-types"] = "admit-only-vlan-tagged" + tagged.state.bridges[0]["frame-types"] = "admit-only-untagged-and-priority-tagged"; tagged.state.bridges[0].pvid = "1" + tagged.state.vlanInterfaces = [{ ".id": "*5", name: "mgmt-vlan", interface: "br-users", "vlan-id": "10", disabled: "false" }] + tagged.state.ipv4Addresses[0].interface = "mgmt-vlan" + tagged.desired = bridgeVlanCurrentDesired(tagged.state, tagged.bridgeId); tagged.desired.ports[0].ingressFiltering = false + assert.throws(() => buildBridgeVlanConfiguration(tagged), /CPU management/) +}) +test("multi-VLAN tagged ranges are supported without fabricated access ports", () => { + const plan = build(value => { value.desired.vlans.push({ vlanIds: "20-30,40", tagged: [cpu, trunk], untagged: [], disabled: false }) }) + assert.equal(plan.steps.filter(step => step.resource === "bridge_vlan" && step.action === "create").length, 2) +}) +test("independent management disables filtering first for edits and restores last", () => { + const plan = build(value => { value.state.bridges[0]["vlan-filtering"] = "true" }) + assert.deepEqual(plan.steps[0].fields, { "vlan-filtering": "false" }) + assert.deepEqual(plan.steps.at(-1)?.fields, { "vlan-filtering": "true" }) +}) +test("explicit deletion is supported, inverse recreates old configuration with original fields", () => { + const value = fixture() + value.state.vlans = [{ ".id": "*11", bridge: "br-users", "vlan-ids": "10", tagged: "br-users", untagged: "ether2", disabled: "false", comment: "original" }] + value.desired.filtering = false; value.desired.ports = []; value.desired.vlans = [] + value.desired.removePortIds = ["*10"]; value.desired.removeVlanIds = ["*11"] + const plan = buildBridgeVlanConfiguration(value) + assert.deepEqual(plan.steps.map(step => [step.resource, step.action, step.id]), [["bridge_vlan", "delete", "*11"], ["bridge_port", "delete", "*10"]]) + const inverse = bridgeVlanInverseDesired(plan, []) + assert.equal(inverse.ports[0].id, undefined); assert.equal(inverse.vlans[0].id, undefined) + assert.equal(inverse.vlans[0].comment, "original"); assert.equal(inverse.ports[0].pvid, 10) + assert.ok(plan.objects.some(object => object.kind === "ethernet" && object.id === access.id), "Removed old physical interface remains protected") + assert.ok(plan.objects.some(object => object.kind === "bridge" && object.id === cpu.id), "Removed old CPU membership remains protected") +}) +test("inverse removes only exact executor ACK IDs, never predicted native IDs", () => { + const plan = build(), inverse = bridgeVlanInverseDesired(plan, [{ stepIndex: 0, id: "*A1" }, { stepIndex: 1, id: "*A2" }]) + assert.deepEqual(inverse.removeVlanIds, ["*A1"]); assert.deepEqual(inverse.removePortIds, ["*A2"]) + assert.throws(() => bridgeVlanInverseDesired(plan, []), /ACK/) + assert.throws(() => bridgeVlanInverseDesired(plan, [{ stepIndex: 0, id: "*A1" }, { stepIndex: 0, id: "*A2" }]), /Повтор/) +}) +test("dynamic rows cannot be modified or deleted and never become commands", () => { + const plan = build(value => { value.state.vlans.push({ ".id": "*30", bridge: "br-users", dynamic: "true", "vlan-ids": "10", "current-untagged": "ether2" }) }) + assert.ok(plan.steps.every(step => step.id !== "*30")) + assert.throws(() => build(value => { value.state.ports[0].dynamic = "true" }), /полный список/) +}) +test("channel host must be an actual enabled configured address with live same-server receipt", () => { + assert.throws(() => build(value => { value.management.host = "198.51.100.1" }), /Адрес канала/) + assert.throws(() => build(value => { value.management.serverId = 2 }), /канала управления/) + assert.throws(() => build(value => { value.management.expiresAt = new Date(value.now).toISOString() }), /канала управления/) + assert.throws(() => build(value => { value.state.ipv4Addresses[0].disabled = "true" }), /Адрес канала/) + assert.throws(() => build(value => { value.state.ipv4Addresses.push({ ...value.state.ipv4Addresses[0], ".id": "*21" }) }), /однозначно/) +}) +test("equivalent IPv6 channel addresses resolve without textual spelling assumptions", () => { + const plan = build(value => { value.management.host = "2001:db8::1"; value.state.ipv4Addresses = []; value.state.ipv6Addresses = [{ ".id": "*22", address: "2001:0db8:0:0:0:0:0:1/64", interface: "ether-mgmt", disabled: "false" }] }) + assert.equal(plan.managementPath.addressId, "*22") +}) +function bridgeManagement() { + const value = fixture() + value.state.bridges[0]["vlan-filtering"] = "true" + value.state.bridges[0].pvid = "10" + value.state.ipv4Addresses[0].interface = "br-users" + value.state.vlans = [{ ".id": "*11", bridge: "br-users", "vlan-ids": "10", untagged: "br-users,ether2", disabled: "false" }] + value.desired.pvid = 10 + value.desired.vlans[0] = { id: "*11", vlanIds: "10", tagged: [trunk], untagged: [cpu, access], disabled: false } + return value +} +test("same-bridge CPU management allows multiple safe changes without a filtering outage", () => { + const value = bridgeManagement(), plan = buildBridgeVlanConfiguration(value) + assert.equal(plan.managementPath.bridgeName, "br-users") + assert.ok(plan.steps.length >= 2) + assert.ok(plan.steps.every(step => step.fields["vlan-filtering"] === undefined)) +}) +test("removing redundant explicit CPU untagged declaration preserves derived PVID membership", () => { + const value = bridgeManagement(); value.desired.vlans[0].untagged = [access] + assert.ok(buildBridgeVlanConfiguration(value).steps.length > 0) +}) +test("port removal and untagged migration cannot fabricate management safety", () => { + for (const change of [ + (value: ReturnType) => { value.desired.ports.shift(); value.desired.removePortIds = ["*10"]; value.desired.vlans[0].untagged = [cpu] }, + (value: ReturnType) => { value.desired.pvid = 20 }, + ]) { const value = bridgeManagement(); change(value); assert.throws(() => buildBridgeVlanConfiguration(value)) } +}) +test("a direct bridge CPU address also preserves existing tagged external management paths", () => { + const value = implicitPvidManagement() + value.state.ports.push({ ".id": "*12", bridge: "br-users", interface: "ether3", pvid: "1", "ingress-filtering": "true", "frame-types": "admit-only-vlan-tagged", disabled: "false" }) + value.state.vlans = [{ ".id": "*11", bridge: "br-users", "vlan-ids": "10", tagged: "ether3", disabled: "false" }] + value.desired = bridgeVlanCurrentDesired(value.state, value.bridgeId) + value.desired.ports[0].ingressFiltering = false + assert.equal(buildBridgeVlanConfiguration(value).steps.length, 1) + value.desired.vlans[0].tagged = [] + assert.throws(() => buildBridgeVlanConfiguration(value), /CPU management/) +}) +test("VLAN management derives VID and CPU membership from the configured parent", () => { + const value = fixture() + value.state.bridges[0]["vlan-filtering"] = "true" + value.state.ports[0]["frame-types"] = "admit-only-vlan-tagged" + value.state.ports[0].pvid = "1" + value.state.vlans = [{ ".id": "*11", bridge: "br-users", "vlan-ids": "10", tagged: "br-users,ether2", disabled: "false" }] + value.state.vlanInterfaces = [{ ".id": "*5", name: "mgmt-vlan", interface: "br-users", "vlan-id": "10", disabled: "false" }] + value.state.ipv4Addresses[0].interface = "mgmt-vlan" + value.desired.ports[0].pvid = 1; value.desired.ports[0].frameTypes = "admit-only-vlan-tagged" + value.desired.vlans[0] = { id: "*11", vlanIds: "10", tagged: [cpu, access, trunk], untagged: [], disabled: false } + const plan = buildBridgeVlanConfiguration(value) + assert.equal(plan.managementPath.vlanId, 10) + for (const [kind, id] of [["ipv4_address", "*20"], ["vlan", "*5"], ["bridge", "*1"], ["bridge_port", "*10"], ["ethernet", "*2"]]) assert.ok(plan.objects.some(object => object.kind === kind && object.id === id), `Missing protected management dependency ${kind}/${id}`) + assert.ok(plan.steps.every(step => step.fields["vlan-filtering"] === undefined)) + value.desired.vlans[0].tagged = [access, trunk] + assert.throws(() => buildBridgeVlanConfiguration(value), /CPU management/) +}) +test("unknown PVID/frame types/flags and duplicate source identities refuse preparation", () => { + assert.throws(() => build(value => { delete value.state.ports[0].pvid })) + assert.throws(() => build(value => { value.state.ports[0]["frame-types"] = "unknown" })) + assert.throws(() => build(value => { value.state.bridges[0]["vlan-filtering"] = "unknown" })) + assert.throws(() => build(value => { value.state.interfaces.push({ ...value.state.interfaces[0] }) }), /Повтор/) +}) +test("unchanged full desired configuration has zero writes and omitted comments stay omitted", () => { + const value = bridgeManagement() + value.desired.ports.pop(); value.desired.vlans[0].tagged = [] + const plan = buildBridgeVlanConfiguration(value) + assert.deepEqual(plan.steps, []) + assert.equal(plan.inverse.desired.ports[0].comment, undefined) + assert.equal(plan.inverse.desired.vlans[0].comment, undefined) +}) +test("native projection to desired retains IDs and optional empty-field semantics", () => { + const value = bridgeManagement(), desired = bridgeVlanCurrentDesired(value.state, value.bridgeId) + assert.deepEqual(desired.vlans[0].tagged, []) + assert.equal(desired.ports[0].id, "*10") + assert.equal(desired.ports[0].comment, undefined) + assert.deepEqual(buildBridgeVlanConfiguration({ ...value, desired }).steps, []) +}) +test("unchanged native VID range syntax does not generate formatting-only mutations", () => { + const value = fixture() + value.state.ports[0]["frame-types"] = "admit-only-vlan-tagged" + value.state.vlans = [{ ".id": "*11", bridge: "br-users", "vlan-ids": "20-30,40", tagged: "br-users,ether2", disabled: "false" }] + const desired = bridgeVlanCurrentDesired(value.state, value.bridgeId) + assert.deepEqual(buildBridgeVlanConfiguration({ ...value, desired }).steps, []) +}) +test("dynamic PVID admission cannot conflict with an explicit tagged membership", () => { + assert.throws(() => build(value => { + value.desired.ports[1].frameTypes = "admit-all"; value.desired.ports[1].pvid = 10 + }), /PVID создаёт конфликт/) + assert.throws(() => build(value => { value.desired.pvid = 10 }), /CPU PVID/) +}) +test("unfiltered management ignores ingress flags: every existing L2 port is preserved", () => { + const value = fixture() + value.state.ipv4Addresses[0].interface = "br-users" + value.state.bridges[0].pvid = "10" + value.state.ports[0].pvid = "99" + value.desired.pvid = 10 + value.desired.vlans[0].untagged = [cpu, access]; value.desired.vlans[0].tagged = [trunk] + assert.ok(buildBridgeVlanConfiguration(value).steps.length > 0) + value.desired.ports[0].frameTypes = "admit-only-vlan-tagged" + value.desired.vlans[0].untagged = [cpu]; value.desired.vlans[0].tagged = [access, trunk] + assert.throws(() => buildBridgeVlanConfiguration(value), /CPU management/) +}) +test("adding a management parent as a bridge port cannot be presented as independence", () => { + assert.throws(() => build(value => { + value.desired.ports.push({ interface: { resource: "ethernet", id: "*4" }, pvid: 10, ingressFiltering: true, frameTypes: "admit-all", disabled: false }) + value.desired.vlans[0].untagged.push({ resource: "ethernet", id: "*4" }) + }), /management интерфейс в slave/) +}) +test("inverse explicitly clears a newly assigned previously absent comment", () => { + const plan = build(value => { value.desired.ports[0].comment = "new comment" }) + assert.equal(plan.inverse.desired.ports[0].comment, "") +}) +test("independent management address, bridge and actual port dependencies enter protection policy", () => { + const value = fixture() + value.state.bridges.push({ ".id": "*40", name: "br-mgmt", disabled: "false", pvid: "1", "vlan-filtering": "false" }) + value.state.interfaces.push({ resource: "bridge", id: "*40", name: "br-mgmt", disabled: "false" }) + value.state.ports.push({ ".id": "*41", bridge: "br-mgmt", interface: "ether-mgmt", pvid: "1", "frame-types": "admit-all", disabled: "false" }) + value.state.ipv4Addresses[0].interface = "br-mgmt" + const plan = buildBridgeVlanConfiguration(value) + for (const [kind, id] of [["ipv4_address", "*20"], ["bridge", "*40"], ["bridge_port", "*41"], ["ethernet", "*4"]]) assert.ok(plan.objects.some(object => object.kind === kind && object.id === id), `Missing protected management dependency ${kind}/${id}`) +}) +test("reassigned old port and removed old VLAN members remain in protected references", () => { + const value = fixture() + value.desired.filtering = false + value.state.vlans = [{ ".id": "*11", bridge: "br-users", "vlan-ids": "10", tagged: "ether2", disabled: "false" }] + value.desired.ports = [{ ...value.desired.ports[0], interface: trunk }] + value.desired.vlans = [{ id: "*11", vlanIds: "10", tagged: [trunk], untagged: [], disabled: false }] + const plan = buildBridgeVlanConfiguration(value) + assert.ok(plan.objects.some(object => object.kind === "ethernet" && object.id === access.id)) + assert.ok(plan.objects.some(object => object.kind === "ethernet" && object.id === trunk.id)) +}) +test("L3 GRE/WireGuard native bridge ports are rejected before any plan is emitted", () => { + for (const resource of ["gre", "wireguard"]) { + const value = fixture() + value.state.interfaces.find(iface => iface.id === trunk.id)!.resource = resource + value.desired.ports[1].interface.resource = resource + value.desired.vlans[0].tagged[1].resource = resource + assert.throws(() => buildBridgeVlanConfiguration(value), error => error instanceof Error && "code" in error && error.code === "UNSUPPORTED_CAPABILITY") + } +}) +test("catalogued L2 VLAN and VXLAN ports remain available", () => { + for (const resource of ["vlan", "vxlan"]) { + const value = fixture() + value.state.interfaces.find(iface => iface.id === trunk.id)!.resource = resource + value.desired.ports[1].interface.resource = resource + value.desired.vlans[0].tagged[1].resource = resource + if (resource === "vlan") value.state.vlanInterfaces.push({ ".id": trunk.id, name: "ether3", interface: "ether-mgmt", "vlan-id": "101", disabled: "false" }) + assert.ok(buildBridgeVlanConfiguration(value).steps.some(step => step.resource === "bridge_port" && step.fields.interface === "ether3")) + } +}) +test("a VLAN over the selected bridge cannot be fed back into that bridge", () => { + const value = fixture() + value.state.interfaces.find(iface => iface.id === trunk.id)!.resource = "vlan" + value.desired.ports[1].interface.resource = "vlan"; value.desired.vlans[0].tagged[1].resource = "vlan" + value.state.vlanInterfaces.push({ ".id": trunk.id, name: "ether3", interface: "br-users", "vlan-id": "101", disabled: "false" }) + assert.throws(() => buildBridgeVlanConfiguration(value), /собственные порты/) +}) +test("explicit port replacement deletes old interface claim before native CREATE", () => { + const value = fixture() + delete value.desired.ports[0].id; value.desired.removePortIds = ["*10"] + const plan = buildBridgeVlanConfiguration(value) + const removal = plan.steps.findIndex(step => step.resource === "bridge_port" && step.action === "delete" && step.id === "*10") + const addition = plan.steps.findIndex(step => step.resource === "bridge_port" && step.action === "create" && step.fields.interface === "ether2") + assert.ok(removal >= 0 && addition > removal) + const acks = plan.steps.flatMap((step, stepIndex) => step.action === "create" ? [{ stepIndex, id: `*${(stepIndex + 100).toString(16)}` }] : []) + const inverse = bridgeVlanInverseDesired(plan, acks) + assert.equal(inverse.ports[0].id, undefined) + assert.ok(inverse.removePortIds.includes(acks.find(ack => ack.stepIndex === addition)!.id)) +}) +test("swapping existing claimed interfaces has no executable order and refuses before publication", () => { + const value = fixture() + value.state.ports.push({ ".id": "*12", bridge: "br-users", interface: "ether3", pvid: "1", "ingress-filtering": "true", "frame-types": "admit-only-vlan-tagged", disabled: "true" }) + value.desired.ports[1].id = "*12" + value.desired.ports[0].interface = trunk; value.desired.ports[1].interface = access + value.desired.filtering = false; value.desired.vlans = [] + const before = structuredClone(value) + assert.throws(() => buildBridgeVlanConfiguration(value), /конфликта интерфейсов/) + assert.deepEqual(value, before) +}) diff --git a/backend/src/services/bridge-vlan-configuration.ts b/backend/src/services/bridge-vlan-configuration.ts new file mode 100644 index 00000000..451dd7a3 --- /dev/null +++ b/backend/src/services/bridge-vlan-configuration.ts @@ -0,0 +1,338 @@ +import { isIP } from "node:net" +import type { z } from "zod" +import { bridgeVlanDesiredSchema, bridgeVlanRowIdSchema as id, bridgeVlanInterfaceReferenceSchema as reference, bridgeVlanIdSchema as vid, bridgeVlanFrameTypesSchema as frames, type BridgeVlanDesired, type BridgeVlanState } from "@mmapp/contracts/bridge-vlan" +import type { ConfigurationRow, RosStep } from "../modules/mcp/resources.js" +export { bridgeVlanDesiredSchema, type BridgeVlanDesired, type BridgeVlanState } +export type BridgeVlanInterface = { resource: string; id: string; name: string; disabled?: unknown } +/** This argument belongs to the trusted server adapter, never the public request. + * The adapter verifies channel identity/revocation and decrypts its literal host. + * These values and all source snapshots must be sealed and rechecked on apply. */ +export type BridgeVlanManagement = { channelId: string; serverId: number; expiresAt: string; host: string } +export class BridgeVlanConfigurationError extends Error { + constructor(readonly code: "INVALID_INPUT" | "STATE_CONFLICT" | "MANAGEMENT_UNSAFE" | "UNSUPPORTED_CAPABILITY", message: string) { super(message) } +} +/** L2 port kinds available in the actual typed configuration catalogue. + * RouterOS also supports EoIP/bond/Wi-Fi, but they need their own indexed + * configuration resource before this planner can prove their stable identity. + * GRE and WireGuard carry L3 traffic and cannot be native bridge ports. + * https://manual.mikrotik.com/docs/bridging-and-switching/ + * https://help.mikrotik.com/docs/spaces/ROS/pages/24805531/GRE + * https://help.mikrotik.com/docs/spaces/ROS/pages/69664792/WireGuard */ +export const BRIDGE_VLAN_L2_PORT_RESOURCES = ["ethernet", "vlan", "vxlan"] as const +function reject(message: string, code: BridgeVlanConfigurationError["code"] = "STATE_CONFLICT"): never { throw new BridgeVlanConfigurationError(code, message) } +const text = (row: ConfigurationRow, key: string): string => typeof row[key] === "string" ? row[key] as string : reject(`Не подтверждено поле ${key}`) +function flag(value: unknown): boolean { + if ([true, "true", "yes"].includes(value as boolean | string)) return true + if ([false, "false", "no"].includes(value as boolean | string)) return false + return reject("Не подтверждено логическое свойство") +} +const mutable = (row: ConfigurationRow) => !["dynamic", "builtin", "default"].some(key => row[key] !== undefined && flag(row[key])) +const enabled = (row: ConfigurationRow) => !flag(row.disabled) +const rowId = (row: ConfigurationRow) => id.parse(row[".id"]) +const keyOf = (value: { resource: string; id: string }) => `${value.resource}:${value.id}` +const unique = (values: string[], label: string) => { if (new Set(values).size !== values.length) reject(`Повтор ${label}`, "INVALID_INPUT") } + +/** Ranges are inclusive. Reject overlaps rather than silently deduplicating. */ +export function parseBridgeVlanIds(value: string): number[] { + const found = new Set() + for (const part of value.split(",")) { + if (!/^[1-9]\d{0,3}(?:-[1-9]\d{0,3})?$/.test(part)) reject("Некорректный VLAN ID/диапазон", "INVALID_INPUT") + const [start, rawEnd] = part.split("-").map(Number), end = rawEnd ?? start + if (start < 1 || end > 4094 || end < start) reject("VLAN ID вне 1..4094", "INVALID_INPUT") + for (let n = start; n <= end; n++) { if (found.has(n)) reject("Повтор VLAN ID", "INVALID_INPUT"); found.add(n) } + } + return [...found].sort((a, b) => a - b) +} +const names = (row: ConfigurationRow, field: string): string[] => { + if (row[field] === undefined && ["tagged", "untagged"].includes(field)) return [] + const value = text(row, field) + if (!value) return [] + const result = value.split(",") + if (result.some(name => !name || name.trim() !== name)) reject("Неоднозначный список интерфейсов") + unique(result, "интерфейса") + return result +} +const numberVid = (value: unknown) => vid.parse(typeof value === "string" && /^\d+$/.test(value) ? Number(value) : value) +type Topology = { bridge: ConfigurationRow; ports: ConfigurationRow[]; vlans: ConfigurationRow[] } +type Admission = { interface: string; mode: "tagged" | "untagged" } +export type BridgeVlanConfigurationPlan = { + serverId: number; bridgeId: string; steps: RosStep[]; + objects: Array<{ kind: string; id: string; serverId: number }>; + managementPath: { channelId: string; addressId: string; interfaceName: string; bridgeName?: string; vlanId?: number }; + inverse: { desired: BridgeVlanDesired; created: Array<{ stepIndex: number; resource: "bridge_port" | "bridge_vlan" }> }; +} + +/** Convert observed static rows without replacing unknown native properties + * with assumed defaults. Used by the editor and generic effective PATCH guard. */ +export function bridgeVlanCurrentDesired(state: BridgeVlanState, bridgeId: string): BridgeVlanDesired { + const bridges = state.bridges.filter(row => row[".id"] === bridgeId) + if (bridges.length !== 1) reject("Bridge не найден однозначно") + const bridge = bridges[0], bridgeName = text(bridge, "name") + const refFor = (name: string) => { + const found = state.interfaces.filter(value => value.name === name) + if (found.length !== 1) reject("Не подтверждена идентичность интерфейса") + return { resource: found[0].resource, id: id.parse(found[0].id) } + } + return { + filtering: flag(bridge["vlan-filtering"]), pvid: numberVid(bridge.pvid), frameTypes: frames.parse(bridge["frame-types"]), ingressFiltering: flag(bridge["ingress-filtering"]), + ports: state.ports.filter(row => row.bridge === bridgeName && mutable(row)).map(row => ({ id: rowId(row), interface: refFor(text(row, "interface")), pvid: numberVid(row.pvid), ingressFiltering: flag(row["ingress-filtering"]), frameTypes: frames.parse(row["frame-types"]), disabled: flag(row.disabled), ...(row.comment === undefined ? {} : { comment: text(row, "comment") }) })), + vlans: state.vlans.filter(row => row.bridge === bridgeName && mutable(row)).map(row => ({ id: rowId(row), vlanIds: text(row, "vlan-ids"), tagged: names(row, "tagged").map(refFor), untagged: names(row, "untagged").map(refFor), disabled: flag(row.disabled), ...(row.comment === undefined ? {} : { comment: text(row, "comment") }) })), + removePortIds: [], removeVlanIds: [], + } +} + +export function buildBridgeVlanConfiguration(input: { + serverId: number; bridgeId: string; desired: BridgeVlanDesired; state: BridgeVlanState; management: BridgeVlanManagement; now: number; +}): BridgeVlanConfigurationPlan { + const { serverId, bridgeId, state, management } = input, desired = bridgeVlanDesiredSchema.parse(input.desired) + if (!Number.isSafeInteger(serverId) || serverId <= 0 || management.serverId !== serverId || !management.channelId || !Number.isFinite(input.now) || !(Date.parse(management.expiresAt) > input.now) || !isIP(management.host)) reject("Нет актуального канала управления", "MANAGEMENT_UNSAFE") + const bridges = state.bridges.filter(row => row[".id"] === bridgeId) + if (bridges.length !== 1 || !mutable(bridges[0]) || !enabled(bridges[0])) reject("Bridge не подтверждён или отключён") + const bridge = bridges[0], bridgeName = text(bridge, "name") + // A dynamic VLAN table is not evidence of caller-owned rows. PVID-generated + // untagged membership is derived below; protocol registrations are unsupported. + if (bridge.mvrp !== undefined && flag(bridge.mvrp)) reject("MVRP создаёт программируемые VLAN memberships; этот режим не поддерживается", "UNSUPPORTED_CAPABILITY") + const cpuFrames = frames.parse(bridge["frame-types"]), cpuIngress = flag(bridge["ingress-filtering"]) + const byRef = new Map(state.interfaces.map(iface => [keyOf(iface), iface])) + unique(state.interfaces.map(keyOf), "идентичности интерфейса") + unique(state.interfaces.map(iface => iface.name), "имени интерфейса") + const bridgeRef = state.interfaces.find(iface => iface.resource === "bridge" && iface.id === bridgeId && iface.name === bridgeName) + if (!bridgeRef) reject("Не подтверждена идентичность CPU bridge") + const resolve = (ref: z.infer) => byRef.get(keyOf(ref)) ?? reject("Неизвестный стабильный ID интерфейса", "INVALID_INPUT") + const oldPorts = state.ports.filter(row => row.bridge === bridgeName), oldVlans = state.vlans.filter(row => row.bridge === bridgeName) + unique(state.ports.map(rowId), "ID порта"); unique(state.vlans.map(rowId), "ID membership") + // Dynamic rows remain owned by RouterOS; they are never converted to commands. + const staticPorts = oldPorts.filter(mutable), staticVlans = oldVlans.filter(mutable) + const account = (rows: ConfigurationRow[], keep: Array<{ id?: string }>, remove: string[], label: string) => { + const requested = [...keep.flatMap(row => row.id ? [row.id] : []), ...remove] + unique(requested, label) + const existing = rows.map(rowId) + if (requested.length !== existing.length || requested.some(value => !existing.includes(value))) reject(`Нужен явный полный список ${label}`, "INVALID_INPUT") + } + account(staticPorts, desired.ports, desired.removePortIds, "портов") + account(staticVlans, desired.vlans, desired.removeVlanIds, "VLAN memberships") + unique(desired.ports.map(port => keyOf(port.interface)), "порта bridge") + const portRows = desired.ports.map((port, index) => { + const iface = resolve(port.interface) + if (iface.name === bridgeName || iface.resource === "bridge" || state.ports.some(row => row.interface === iface.name && row.bridge !== bridgeName)) reject("Интерфейс уже принадлежит другому bridge или является bridge", "INVALID_INPUT") + if (!(BRIDGE_VLAN_L2_PORT_RESOURCES as readonly string[]).includes(iface.resource)) reject("Тип интерфейса не поддерживает подтверждённый L2 bridge port", "UNSUPPORTED_CAPABILITY") + let parent = iface.name + const ancestors = new Set() + while (true) { + if (ancestors.has(parent)) reject("Цикл VLAN родителей bridge-порта", "INVALID_INPUT") + ancestors.add(parent) + const vlans = state.vlanInterfaces.filter(row => row.name === parent) + if (!vlans.length) { + if (parent === iface.name && iface.resource === "vlan") reject("Не подтверждён VLAN parent bridge-порта", "UNSUPPORTED_CAPABILITY") + break + } + if (vlans.length !== 1) reject("Неоднозначный VLAN parent bridge-порта", "STATE_CONFLICT") + parent = text(vlans[0], "interface") + if (parent === bridgeName) reject("VLAN над выбранным bridge нельзя вернуть в его собственные порты", "INVALID_INPUT") + } + if (!port.disabled && iface.disabled !== undefined && flag(iface.disabled)) reject("Порт ссылается на отключённый интерфейс", "INVALID_INPUT") + const previous = staticPorts.find(row => row[".id"] === port.id) + return { ".id": port.id ?? `new-port-${index}`, bridge: bridgeName, interface: iface.name, pvid: String(port.pvid), "ingress-filtering": String(port.ingressFiltering), "frame-types": port.frameTypes, disabled: String(port.disabled), ...(port.comment !== undefined ? { comment: port.comment } : previous?.comment !== undefined ? { comment: text(previous, "comment") } : {}) } + }) + const vlanRows = desired.vlans.map((vlan, index) => { + const tagged = vlan.tagged.map(ref => resolve(ref).name), untagged = vlan.untagged.map(ref => resolve(ref).name) + unique([...tagged, ...untagged], "tagged/untagged участника") + parseBridgeVlanIds(vlan.vlanIds) + const previous = staticVlans.find(row => row[".id"] === vlan.id) + return { ".id": vlan.id ?? `new-vlan-${index}`, bridge: bridgeName, "vlan-ids": vlan.vlanIds, ...(tagged.length || previous?.tagged !== undefined ? { tagged: tagged.join(",") } : {}), ...(untagged.length || previous?.untagged !== undefined ? { untagged: untagged.join(",") } : {}), disabled: String(vlan.disabled), ...(vlan.comment !== undefined ? { comment: vlan.comment } : previous?.comment !== undefined ? { comment: text(previous, "comment") } : {}) } + }) + const target: Topology = { bridge: { ...bridge, pvid: String(desired.pvid), "vlan-filtering": String(desired.filtering), "frame-types": desired.frameTypes ?? cpuFrames, "ingress-filtering": String(desired.ingressFiltering ?? cpuIngress) }, ports: [...oldPorts.filter(row => !mutable(row)), ...portRows], vlans: [...oldVlans.filter(row => !mutable(row)), ...vlanRows] } + const activePorts = (topology: Topology) => topology.ports.filter(enabled) + const membership = (topology: Topology, vlanId: number, mode: "tagged" | "untagged") => { + const members = new Set(topology.vlans.filter(row => mutable(row) && enabled(row) && parseBridgeVlanIds(text(row, "vlan-ids")).includes(vlanId)).flatMap(row => names(row, mode))) + // RouterOS automatically includes CPU/ports that accept untagged ingress + // as untagged PVID members. Never synthesize a VLAN table command for it. + if (mode === "untagged") for (const port of [topology.bridge, ...activePorts(topology)]) { + if (enabled(port) && frames.parse(port["frame-types"]) !== "admit-only-vlan-tagged" && numberVid(port.pvid) === vlanId) members.add(port === topology.bridge ? bridgeName : text(port, "interface")) + } + return members + } + // Validate effective static state; dynamic PVID membership is derived from + // the actual port PVID, never accepted as caller-owned VLAN configuration. + const validate = (topology: Topology) => { + const ports = activePorts(topology), allowed = new Set([bridgeName, ...topology.ports.map(row => text(row, "interface"))]) + const used = new Set(), configuredVids = new Set() + for (const vlan of topology.vlans.filter(row => mutable(row) && enabled(row))) { + const vids = parseBridgeVlanIds(text(vlan, "vlan-ids")), tagged = names(vlan, "tagged"), untagged = names(vlan, "untagged") + unique([...tagged, ...untagged], "tagged/untagged участника") + if ([...tagged, ...untagged].some(name => !allowed.has(name))) reject("Участник VLAN не принадлежит выбранному bridge", "INVALID_INPUT") + for (const n of vids) { if (configuredVids.has(n)) reject("Неоднозначная эффективная VLAN membership", "INVALID_INPUT"); configuredVids.add(n) } + for (const n of vids) for (const name of [...tagged, ...untagged]) { + const key = `${n}:${name}` + if (used.has(key)) reject("Неоднозначная эффективная VLAN membership", "INVALID_INPUT") + used.add(key) + const pvid = name === bridgeName ? numberVid(topology.bridge.pvid) : numberVid(topology.ports.find(row => row.interface === name)!.pvid) + if (untagged.includes(name) && (vids.length !== 1 || pvid !== n)) reject("Untagged VLAN не соответствует PVID", "INVALID_INPUT") + } + } + if (flag(topology.bridge["vlan-filtering"])) for (const port of ports) { + const frameTypes = frames.parse(port["frame-types"]) + if (frameTypes !== "admit-only-vlan-tagged" && membership(topology, numberVid(port.pvid), "tagged").has(text(port, "interface"))) reject("PVID создаёт конфликт tagged/untagged membership", "INVALID_INPUT") + } + if (flag(topology.bridge["vlan-filtering"]) && frames.parse(topology.bridge["frame-types"]) !== "admit-only-vlan-tagged" && membership(topology, numberVid(topology.bridge.pvid), "tagged").has(bridgeName)) reject("CPU PVID создаёт конфликт tagged/untagged membership", "INVALID_INPUT") + } + validate(target) + const addresses = [...state.ipv4Addresses, ...state.ipv6Addresses].filter(row => typeof row.address === "string" && isIP(row.address.split("/")[0]) === isIP(management.host) && sameAddress(row.address.split("/")[0], management.host) && enabled(row)) + if (addresses.length !== 1) reject("Адрес канала не найден однозначно в конфигурации", "MANAGEMENT_UNSAFE") + const address = addresses[0], addressInterface = text(address, "interface") + let managementInterface = addressInterface, managementVid: number | undefined + const visited = new Set() + while (true) { + if (visited.has(managementInterface)) reject("Цикл родителей management", "MANAGEMENT_UNSAFE") + visited.add(managementInterface) + const candidates = state.vlanInterfaces.filter(row => row.name === managementInterface) + if (!candidates.length) break + if (candidates.length !== 1 || !enabled(candidates[0]) || managementVid !== undefined) reject("Не подтверждён management VLAN parent", "MANAGEMENT_UNSAFE") + managementVid = numberVid(candidates[0]["vlan-id"]); managementInterface = text(candidates[0], "interface") + } + const managementPort = state.ports.find(row => row.interface === managementInterface) + const affected = managementInterface === bridgeName || managementPort?.bridge === bridgeName + // An IP directly assigned to a bridge slave does not prove CPU delivery. + if (managementPort?.bridge === bridgeName) reject("Management IP на slave-порту не доказывает CPU-доступ", "MANAGEMENT_UNSAFE") + if (target.ports.some(row => row.interface === managementInterface)) reject("План превращает management интерфейс в slave-порт", "MANAGEMENT_UNSAFE") + const managementBase = state.interfaces.find(iface => iface.name === managementInterface) + if (!managementBase || !["ethernet", "bridge"].includes(managementBase.resource)) reject("Не подтверждён физический management parent", "MANAGEMENT_UNSAFE") + if (managementPort) reject("Management IP на slave-порту не доказывает CPU-доступ", "MANAGEMENT_UNSAFE") + // A different bridge is independent only if none of its actual member + // interfaces depends on this bridge (for example a VLAN over this bridge). + const dependentOnTarget = (name: string, visiting = new Set()): boolean => { + if (name === bridgeName || target.ports.some(row => row.interface === name)) return true + if (visiting.has(name)) reject("Цикл management зависимостей", "MANAGEMENT_UNSAFE") + const next = new Set(visiting).add(name) + const vlan = state.vlanInterfaces.find(row => row.name === name) + if (vlan) return dependentOnTarget(text(vlan, "interface"), next) + const iface = state.interfaces.find(item => item.name === name) + if (!iface || !["ethernet", "bridge"].includes(iface.resource)) reject("Не подтверждены management зависимости", "MANAGEMENT_UNSAFE") + return iface.resource === "bridge" && state.ports.filter(row => row.bridge === name && enabled(row)).some(row => dependentOnTarget(text(row, "interface"), next)) + } + if (!affected && dependentOnTarget(managementInterface)) reject("Management зависит от изменяемого bridge", "MANAGEMENT_UNSAFE") + if (!state.interfaces.some(iface => iface.name === managementInterface && (iface.disabled === undefined || !flag(iface.disabled)))) reject("Не подтверждён management интерфейс", "MANAGEMENT_UNSAFE") + const admissions = (topology: Topology): Admission[] => { + if (!affected) return [] + const filtering = flag(topology.bridge["vlan-filtering"]), vlanId = managementVid ?? numberVid(topology.bridge.pvid) + if (!filtering) return activePorts(topology).map(port => ({ interface: text(port, "interface"), mode: managementVid === undefined ? "untagged" as const : "tagged" as const })) + const cpuMode = managementVid === undefined ? "untagged" : "tagged" + const cpuFrameTypes = frames.parse(topology.bridge["frame-types"]) + if (cpuMode === "untagged" && cpuFrameTypes === "admit-only-vlan-tagged" || cpuMode === "tagged" && cpuFrameTypes === "admit-only-untagged-and-priority-tagged") return [] + if (filtering && !membership(topology, vlanId, cpuMode).has(bridgeName)) return [] + if (membership(topology, vlanId, "tagged").has(bridgeName) && membership(topology, vlanId, "untagged").has(bridgeName)) return [] + return activePorts(topology).flatMap(port => { + const name = text(port, "interface"), frameTypes = frames.parse(port["frame-types"]), result: Admission[] = [] + if (membership(topology, vlanId, "tagged").has(name) && membership(topology, vlanId, "untagged").has(name)) return [] + if (frameTypes !== "admit-only-untagged-and-priority-tagged" && membership(topology, vlanId, "tagged").has(name)) result.push({ interface: name, mode: "tagged" }) + if (frameTypes !== "admit-only-vlan-tagged" && numberVid(port.pvid) === vlanId && (!filtering || membership(topology, vlanId, "untagged").has(name))) result.push({ interface: name, mode: "untagged" }) + return result + }) + } + let current: Topology = structuredClone({ bridge, ports: oldPorts, vlans: oldVlans }) + const baseline = admissions(current) + if (affected && !baseline.length) reject("Не подтверждён исходный CPU management-доступ", "MANAGEMENT_UNSAFE") + const preservesManagement = (topology: Topology) => !affected || baseline.every(old => admissions(topology).some(next => old.interface === next.interface && old.mode === next.mode)) + if (!preservesManagement(target)) reject("План изменяет подтверждённые пути CPU management", "MANAGEMENT_UNSAFE") + const inverseDesired = bridgeVlanCurrentDesired(state, bridgeId) + for (const port of inverseDesired.ports) { + if (desired.removePortIds.includes(port.id!)) delete port.id + else if (port.comment === undefined && desired.ports.some(next => next.id === port.id && next.comment !== undefined)) port.comment = "" + } + for (const vlan of inverseDesired.vlans) { + if (desired.removeVlanIds.includes(vlan.id!)) delete vlan.id + else if (vlan.comment === undefined && desired.vlans.some(next => next.id === vlan.id && next.comment !== undefined)) vlan.comment = "" + } + const steps: RosStep[] = [], created: BridgeVlanConfigurationPlan["inverse"]["created"] = [] + const append = (step: RosStep) => { if (step.action === "create") created.push({ stepIndex: steps.length, resource: step.resource as "bridge_port" | "bridge_vlan" }); steps.push(step) } + const mutation = (resource: "bridge_port" | "bridge_vlan", old: ConfigurationRow[], next: ConfigurationRow[], deleted: string[]) => { + const result: Array<{ step: RosStep; modelId: string }> = [] + for (const row of next) { + const nativeId = String(row[".id"]), previous = old.find(value => value[".id"] === nativeId) + const fields = Object.fromEntries(Object.entries(row).filter(([key, value]) => key !== ".id" && (!previous || String(previous[key]) !== value)).map(([key, value]) => [key, String(value)])) + if (Object.keys(fields).length) result.push({ step: { serverId, resource, action: previous ? "update" : "create", ...(previous ? { id: nativeId } : {}), fields }, modelId: nativeId }) + } + for (const nativeId of deleted) result.push({ step: { serverId, resource, action: "delete", id: nativeId, fields: {} }, modelId: nativeId }) + return result + } + const pending = [...mutation("bridge_vlan", staticVlans, vlanRows, desired.removeVlanIds), ...mutation("bridge_port", staticPorts, portRows, desired.removePortIds)] + // Independent management permits a disabled intermediate topology. A channel + // on this bridge instead requires every acknowledged prefix to preserve it. + if (flag(bridge["vlan-filtering"]) && (!desired.filtering || pending.length > 0) && !affected) { + append({ serverId, resource: "bridge", action: "update", id: bridgeId, fields: { "vlan-filtering": "false" } }); current.bridge["vlan-filtering"] = "false" + } + while (pending.length) { + let selected = -1, next: Topology | undefined + for (let index = 0; index < pending.length; index++) { + const item = pending[index], candidate = structuredClone(current), field = item.step.resource === "bridge_port" ? "ports" : "vlans" + if (item.step.action === "delete") candidate[field] = candidate[field].filter(row => row[".id"] !== item.modelId) + else if (item.step.action === "create") candidate[field].push({ ".id": item.modelId, ...item.step.fields }) + else candidate[field] = candidate[field].map(row => row[".id"] === item.modelId ? { ...row, ...item.step.fields } : row) + // RouterOS permits only one bridge-port row per interface, including + // disabled rows. Explicit replacement must DELETE before CREATE, and + // swaps requiring an invented temporary interface are not admitted. + const claimed = candidate.ports.map(row => text(row, "interface")) + if (new Set(claimed).size !== claimed.length) continue + if (preservesManagement(candidate)) { selected = index; next = candidate; break } + } + if (selected < 0 || !next) reject("Не существует порядка без конфликта интерфейсов или потери management", "STATE_CONFLICT") + append(pending.splice(selected, 1)[0].step); current = next + } + const finalFields: Record = {} + if (numberVid(current.bridge.pvid) !== desired.pvid) finalFields.pvid = String(desired.pvid) + if (desired.frameTypes !== undefined && frames.parse(current.bridge["frame-types"]) !== desired.frameTypes) finalFields["frame-types"] = desired.frameTypes + if (desired.ingressFiltering !== undefined && flag(current.bridge["ingress-filtering"]) !== desired.ingressFiltering) finalFields["ingress-filtering"] = String(desired.ingressFiltering) + if (flag(current.bridge["vlan-filtering"]) !== desired.filtering) finalFields["vlan-filtering"] = String(desired.filtering) + if (Object.keys(finalFields).length) { + if (!preservesManagement({ ...current, bridge: { ...current.bridge, ...finalFields } })) reject("Финальное переключение теряет management", "MANAGEMENT_UNSAFE") + append({ serverId, resource: "bridge", action: "update", id: bridgeId, fields: finalFields }) + } + const objectMap = new Map() + for (const [kind, rows] of [["bridge", [bridge]], ["bridge_port", oldPorts], ["bridge_vlan", oldVlans]] as const) for (const row of rows) objectMap.set(`${kind}:${rowId(row)}`, { kind, id: rowId(row), serverId }) + for (const ref of [...desired.ports.map(port => port.interface), ...desired.vlans.flatMap(vlan => [...vlan.tagged, ...vlan.untagged])]) objectMap.set(keyOf(ref), { kind: ref.resource, id: ref.id, serverId }) + const oldMemberNames = new Set([...oldPorts.map(row => text(row, "interface")), ...oldVlans.flatMap(row => [...names(row, "tagged"), ...names(row, "untagged")])]) + for (const name of oldMemberNames) { + const iface = state.interfaces.find(value => value.name === name) ?? reject("Не подтверждена прежняя зависимость bridge") + objectMap.set(keyOf(iface), { kind: iface.resource, id: iface.id, serverId }) + } + const addressResource = state.ipv4Addresses.includes(address) ? "ipv4_address" : "ipv6_address" + objectMap.set(`${addressResource}:${rowId(address)}`, { kind: addressResource, id: rowId(address), serverId }) + const managementVisited = new Set() + const includeManagement = (name: string) => { + if (managementVisited.has(name)) return + managementVisited.add(name) + const iface = state.interfaces.find(value => value.name === name) ?? reject("Не подтверждена идентичность management dependency") + objectMap.set(keyOf(iface), { kind: iface.resource, id: iface.id, serverId }) + const vlan = state.vlanInterfaces.find(row => row.name === name) + if (vlan) includeManagement(text(vlan, "interface")) + if (iface.resource === "bridge") for (const port of state.ports.filter(row => row.bridge === name && enabled(row))) { + objectMap.set(`bridge_port:${rowId(port)}`, { kind: "bridge_port", id: rowId(port), serverId }) + includeManagement(text(port, "interface")) + } + } + includeManagement(addressInterface) + return { serverId, bridgeId, steps, objects: [...objectMap.values()], managementPath: { channelId: management.channelId, addressId: rowId(address), interfaceName: addressInterface, ...(affected ? { bridgeName, ...(managementVid === undefined ? {} : { vlanId: managementVid }) } : {}) }, inverse: { desired: inverseDesired, created } } +} + +function sameAddress(left: string, right: string): boolean { + if (isIP(left) === 4) return left === right + // WHATWG URL canonicalizes equivalent compressed IPv6 spellings. + try { return new URL(`http://[${left}]/`).hostname === new URL(`http://[${right}]/`).hostname } catch { return false } +} + +/** ACKs must come from successful executor read-back, never from user JSON. + * Re-prepare this desired state against fresh snapshots and obtain approval; + * this function grants no automatic compensation or replay authority. */ +export function bridgeVlanInverseDesired(plan: BridgeVlanConfigurationPlan, createAcks: Array<{ stepIndex: number; id: string }>): BridgeVlanDesired { + unique(createAcks.map(ack => String(ack.stepIndex)), "create ACK") + if (createAcks.length !== plan.inverse.created.length) reject("Нет точных ACK всех созданных объектов") + const desired = structuredClone(plan.inverse.desired) + for (const created of plan.inverse.created) { + const ack = createAcks.find(value => value.stepIndex === created.stepIndex) + if (!ack) reject("Нет create ACK") + const nativeId = id.parse(ack.id) + if (created.resource === "bridge_port") desired.removePortIds.push(nativeId) + else desired.removeVlanIds.push(nativeId) + } + return desired +} diff --git a/backend/src/services/bridge-vlan-values.ts b/backend/src/services/bridge-vlan-values.ts new file mode 100644 index 00000000..73efa10f --- /dev/null +++ b/backend/src/services/bridge-vlan-values.ts @@ -0,0 +1,31 @@ +/** RouterOS VLAN IDs and membership are sets, not row-order-sensitive strings. + * Never apply this normalization to interface names or arbitrary properties. */ +export function canonicalBridgeVlanValue(field: string, value: unknown): unknown { + if (["tagged", "untagged"].includes(field)) { + if (value === undefined || value === "") return "" + if (typeof value !== "string") throw Error("Invalid VLAN members") + const names = value.split(",") + if (names.some(name => !name || name.trim() !== name) || new Set(names).size !== names.length) throw Error("Ambiguous VLAN members") + return names.sort().join(",") + } + if (field !== "vlan-ids") return value + if (typeof value !== "string" || !value.length) throw Error("Invalid VLAN IDs") + const ids = new Set() + for (const part of value.split(",")) { + if (!/^[1-9]\d{0,3}(?:-[1-9]\d{0,3})?$/.test(part)) throw Error("Invalid VLAN IDs") + const [start, rawEnd] = part.split("-").map(Number), end = rawEnd ?? start + if (start < 1 || end > 4094 || end < start) throw Error("Invalid VLAN IDs") + for (let id = start; id <= end; id++) { + if (ids.has(id)) throw Error("Overlapping VLAN IDs") + ids.add(id) + } + } + const sorted = [...ids].sort((a, b) => a - b), ranges: string[] = [] + for (let index = 0; index < sorted.length;) { + const start = sorted[index] + let end = start + while (++index < sorted.length && sorted[index] === end + 1) end = sorted[index] + ranges.push(start === end ? String(start) : `${start}-${end}`) + } + return ranges.join(",") +} diff --git a/backend/src/services/btest-node-locks.test.ts b/backend/src/services/btest-node-locks.test.ts new file mode 100644 index 00000000..ce6a037c --- /dev/null +++ b/backend/src/services/btest-node-locks.test.ts @@ -0,0 +1,107 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { setImmediate as turn, setTimeout as delay } from "node:timers/promises" +import { withBtestNodeLocks, BtestNodeLockError } from "./btest-node-locks.js" + +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const code = (value: string) => (error: unknown) => error instanceof BtestNodeLockError && error.code === value + +test("overlapping reversed pairs serialize while unrelated pairs progress", async () => { + const held = deferred(), entered = deferred(), events: string[] = [] + const first = withBtestNodeLocks([2, 1, 2], async () => { events.push("first"); entered.resolve(); await held.promise; events.push("first done") }) + await entered.promise + const second = withBtestNodeLocks([1, 2], async () => { events.push("second") }) + await withBtestNodeLocks([3, 4], async () => { events.push("unrelated") }) + assert.deepEqual(events, ["first", "unrelated"]) + held.resolve(); await Promise.all([first, second]) + assert.deepEqual(events, ["first", "unrelated", "first done", "second"]) +}) + +test("cancelled first-node waiter does not let successors overtake its active predecessor", async () => { + const held = deferred(), entered = deferred(), controller = new AbortController() + let cancelledRan = false, successorRan = false + const first = withBtestNodeLocks([10], async () => { entered.resolve(); await held.promise }) + await entered.promise + const cancelled = withBtestNodeLocks([10, 11], async () => { cancelledRan = true }, { signal: controller.signal }) + const successor = withBtestNodeLocks([10], async () => { successorRan = true }) + const rejected = assert.rejects(cancelled, error => error === controller.signal.reason) + controller.abort(); await rejected; await turn() + assert.equal(cancelledRan, false); assert.equal(successorRan, false) + await withBtestNodeLocks([11], async () => {}) + held.resolve(); await Promise.all([first, successor]); assert.equal(successorRan, true) +}) + +test("cancellation at the second node releases only owned partial tickets", async () => { + const held = deferred(), entered = deferred(), controller = new AbortController() + let cancelledRan = false, secondNodeRan = false + const first = withBtestNodeLocks([22], async () => { entered.resolve(); await held.promise }) + await entered.promise + const cancelled = withBtestNodeLocks([21, 22], async () => { cancelledRan = true }, { signal: controller.signal }) + await turn() // The first ticket is acquired; the second is queued behind node 22. + const successor = withBtestNodeLocks([22], async () => { secondNodeRan = true }) + const rejected = assert.rejects(cancelled) + controller.abort(); await rejected + await withBtestNodeLocks([21], async () => {}) + assert.equal(cancelledRan, false); assert.equal(secondNodeRan, false) + held.resolve(); await Promise.all([first, successor]); assert.equal(secondNodeRan, true) +}) + +test("queued expiry rejects without dispatch and preserves the holder", async () => { + const held = deferred(), entered = deferred() + let expiredRan = false, nextRan = false + const holder = withBtestNodeLocks([30], async () => { entered.resolve(); await held.promise }) + await entered.promise + await assert.rejects(withBtestNodeLocks([30], async () => { expiredRan = true }, { deadlineAt: Date.now() + 20 }), code("BTEST_NODE_LOCK_DEADLINE_EXPIRED")) + const next = withBtestNodeLocks([30], async () => { nextRan = true }) + await turn(); assert.equal(expiredRan, false); assert.equal(nextRan, false) + held.resolve(); await Promise.all([holder, next]) +}) + +test("running callbacks keep locks until settlement despite abort and expiry", async () => { + for (const mode of ["abort", "expiry"]) { + const held = deferred(), entered = deferred(), controller = new AbortController() + let nextRan = false, holderSettled = false + const holder = withBtestNodeLocks([40, 41], async () => { entered.resolve(); await held.promise; return 7 }, { signal: controller.signal, deadlineAt: mode === "expiry" ? Date.now() + 30 : undefined }) + void holder.then(() => { holderSettled = true }) + await entered.promise + const next = withBtestNodeLocks([41], async () => { nextRan = true }) + if (mode === "abort") controller.abort() + else await delay(40) + await turn(); assert.equal(nextRan, false); assert.equal(holderSettled, false) + held.resolve(); assert.equal(await holder, 7); await next + } +}) + +test("callback rejection releases all tickets and preserves the original error", async () => { + const failure = new Error("callback failure") + await assert.rejects(withBtestNodeLocks([51, 50], async () => { throw failure }), error => error === failure) + assert.equal(await withBtestNodeLocks([50, 51], async () => "reused"), "reused") +}) + +test("invalid IDs and expired or aborted admission never invoke callbacks", async () => { + let ran = false + for (const id of [0, -1, NaN, Infinity, 1.5, Number.MAX_SAFE_INTEGER + 1]) await assert.rejects(withBtestNodeLocks([1, id], async () => { ran = true }), code("BTEST_NODE_LOCK_INVALID_INPUT")) + await assert.rejects(withBtestNodeLocks(new Array(2), async () => { ran = true }), code("BTEST_NODE_LOCK_INVALID_INPUT")) + for (const deadlineAt of [NaN, Infinity, -1, 0.5]) await assert.rejects(withBtestNodeLocks([1], async () => { ran = true }, { deadlineAt }), code("BTEST_NODE_LOCK_INVALID_INPUT")) + await assert.rejects(withBtestNodeLocks([1], async () => { ran = true }, { deadlineAt: Date.now() - 1 }), code("BTEST_NODE_LOCK_DEADLINE_EXPIRED")) + const controller = new AbortController(); controller.abort() + await assert.rejects(withBtestNodeLocks([1], async () => { ran = true }, { signal: controller.signal })) + assert.equal(ran, false) +}) + +test("capacity preflight is atomic and cancelled tickets clean up after predecessors", async () => { + const held = deferred(), entered = deferred() + const ids = Array.from({ length: 1023 }, (_, index) => 1000 + index) + const holder = withBtestNodeLocks(ids, async () => { entered.resolve(); await held.promise }) + await entered.promise + await assert.rejects(withBtestNodeLocks([9000, 9001], async () => assert.fail("full queue dispatched")), code("BTEST_NODE_LOCK_QUEUE_FULL")) + // One slot is still usable; failed pair admission left no partial ticket behind. + await withBtestNodeLocks([9000], async () => {}) + const controller = new AbortController() + const cancelled = withBtestNodeLocks([1000], async () => assert.fail("cancelled waiter dispatched"), { signal: controller.signal }) + const rejected = assert.rejects(cancelled); controller.abort(); await rejected + await assert.rejects(withBtestNodeLocks([9000], async () => {}), code("BTEST_NODE_LOCK_QUEUE_FULL")) + held.resolve(); await holder; await turn() + // Reserve the complete budget again: no active, waiting, or cancelled ticket leaked. + await withBtestNodeLocks(Array.from({ length: 1024 }, (_, index) => 10000 + index), async () => {}) +}) diff --git a/backend/src/services/btest-node-locks.ts b/backend/src/services/btest-node-locks.ts index 6d3535ca..eb3107ed 100644 --- a/backend/src/services/btest-node-locks.ts +++ b/backend/src/services/btest-node-locks.ts @@ -1,25 +1,84 @@ -/** Серийный доступ к bandwidth-test по node id (пара узлов не пересекаются). */ +/** Process-local serialization only; this does not exclude another replica. */ const btestNodeLocks = new Map>() +const MAX_TICKETS = 1024 +let liveTickets = 0 -export async function withBtestNodeLocks(nodeIds: number[], fn: () => Promise): Promise { - const ids = [...new Set(nodeIds.filter((id) => Number.isFinite(id)))].sort((a, b) => a - b) +export interface BtestNodeLockOptions { signal?: AbortSignal; deadlineAt?: number } +export class BtestNodeLockError extends Error { + constructor(readonly code: "BTEST_NODE_LOCK_INVALID_INPUT" | "BTEST_NODE_LOCK_QUEUE_FULL" | "BTEST_NODE_LOCK_DEADLINE_EXPIRED") { super(code) } +} +function check(options: BtestNodeLockOptions): void { + options.signal?.throwIfAborted() + if (options.deadlineAt !== undefined && Date.now() >= options.deadlineAt) throw new BtestNodeLockError("BTEST_NODE_LOCK_DEADLINE_EXPIRED") +} +function waitFor(predecessor: Promise, options: BtestNodeLockOptions): Promise { + return new Promise((resolve, reject) => { + let settled = false + let timer: ReturnType | undefined + const finish = (error?: unknown, failed = false) => { + if (settled) return + settled = true + if (timer) clearTimeout(timer) + options.signal?.removeEventListener("abort", aborted) + if (failed) reject(error) + else resolve() + } + const aborted = () => finish(options.signal!.reason, true) + const expiry = () => { + if (settled || options.deadlineAt === undefined) return + const remaining = options.deadlineAt - Date.now() + if (remaining <= 0) finish(new BtestNodeLockError("BTEST_NODE_LOCK_DEADLINE_EXPIRED"), true) + else timer = setTimeout(expiry, Math.min(remaining, 2147483647)) + } + options.signal?.addEventListener("abort", aborted, { once: true }) + if (options.signal?.aborted) aborted() + expiry() + void predecessor.then(() => finish(), error => finish(error, true)) + }) +} + +export async function withBtestNodeLocks(nodeIds: number[], fn: () => Promise, options: BtestNodeLockOptions = {}): Promise { + if (!Array.isArray(nodeIds) || nodeIds.length > MAX_TICKETS || Array.from(nodeIds).some(id => !Number.isSafeInteger(id) || id <= 0)) throw new BtestNodeLockError("BTEST_NODE_LOCK_INVALID_INPUT") + if (options.deadlineAt !== undefined && (!Number.isSafeInteger(options.deadlineAt) || options.deadlineAt < 0)) throw new BtestNodeLockError("BTEST_NODE_LOCK_INVALID_INPUT") + const scope = { signal: options.signal, deadlineAt: options.deadlineAt } + const ids = [...new Set(nodeIds)].sort((a, b) => a - b) + check(scope) + // Reserve the entire scope before enqueueing anything, including partial pairs. + if (liveTickets + ids.length > MAX_TICKETS) throw new BtestNodeLockError("BTEST_NODE_LOCK_QUEUE_FULL") + liveTickets += ids.length + let registered = 0 const releases: Array<() => void> = [] try { for (const id of ids) { - const prev = btestNodeLocks.get(id) + check(scope) + const predecessor = btestNodeLocks.get(id) ?? Promise.resolve() let resolveCurrent!: () => void - const current = new Promise((resolve) => { - resolveCurrent = resolve - }) + const current = new Promise(resolve => { resolveCurrent = resolve }) btestNodeLocks.set(id, current) - if (prev) await prev - releases.push(() => { + registered++ + let released = false + const release = () => { + if (released) return + released = true if (btestNodeLocks.get(id) === current) btestNodeLocks.delete(id) + liveTickets-- resolveCurrent() - }) + } + try { + await waitFor(predecessor, scope) + check(scope) + releases.push(release) + } catch (error) { + // Keep a cancelled ticket behind its predecessor; never release another holder. + void predecessor.then(release, release) + throw error + } } + check(scope) + // Running callbacks retain ownership until settlement, including after abort. return await fn() } finally { - for (let i = releases.length - 1; i >= 0; i -= 1) releases[i]() + liveTickets -= ids.length - registered + for (let index = releases.length - 1; index >= 0; index--) releases[index]() } } diff --git a/backend/src/services/certificate-issue-execution.test.ts b/backend/src/services/certificate-issue-execution.test.ts index 5b9d4d7b..14e6fc1e 100644 --- a/backend/src/services/certificate-issue-execution.test.ts +++ b/backend/src/services/certificate-issue-execution.test.ts @@ -14,6 +14,7 @@ function fixture() { updateJob: async (_id: string, patch: Parameters[0]["updateJob"]>[1]) => { writes.push(patch); Object.assign(row, patch) }, issue: async (_input: Parameters[0]["issue"]>[0]) => { calls += 1 }, appendEvent: async (input: Parameters[0]["appendEvent"]>[0]) => { events.push(input.eventType) }, + admitQueued: (run: () => Promise) => run(), warn: (error: unknown) => { warnings.push(error) }, } return { row, writes, events, warnings, deps, calls: () => calls } @@ -92,6 +93,42 @@ test("queue handles rejected setup without unhandled rejection", async () => { assert.equal(f.warnings.length, 1); assert.equal(f.calls(), 0) }) +test("denied queue admission leaves the receipt untouched", async () => { + const f = fixture() + const denial = new Error("admission denied") + let read = false + f.deps.getJob = async () => { read = true; return f.row } + f.deps.admitQueued = async () => { throw denial } + createCertificateIssueRunner(f.deps).queue("job") + await new Promise(resolve => setImmediate(resolve)) + assert.deepEqual(f.warnings, [denial]) + assert.equal(read, false) + assert.equal(f.calls(), 0) + assert.equal(f.row.status, "queued") + assert.deepEqual(f.writes, []) + assert.deepEqual(f.events, []) +}) + +test("queued admission spans reads, progress, outcome and event persistence", async () => { + const f = fixture() + const order: Array = [] + const { getJob, updateJob, appendEvent } = f.deps + f.deps.admitQueued = async run => { order.push("acquire"); await run(); order.push("release") } + f.deps.getJob = async () => { order.push("read"); return getJob() } + f.deps.updateJob = async (id, patch) => { await updateJob(id, patch); order.push(patch.step) } + f.deps.issue = async input => { order.push("issue"); input.onStep?.("import") } + f.deps.appendEvent = async input => { + await new Promise(resolve => setImmediate(resolve)) + await appendEvent(input) + order.push(input.eventType) + } + createCertificateIssueRunner(f.deps).queue("job") + await new Promise(resolve => setImmediate(resolve)) + assert.deepEqual(order, ["acquire", "read", "acme_order", "issue", "import", "done"]) + await new Promise(resolve => setImmediate(resolve)) + assert.deepEqual(order, ["acquire", "read", "acme_order", "issue", "import", "done", "certificates.issue.done", "release"]) +}) + test("disabled server fails before DNS or device issue", async () => { const f = fixture(); f.deps.getServer = async () => ({ id: 1, name: "router", enabled: false }) await createCertificateIssueRunner(f.deps).run("job") diff --git a/backend/src/services/certificate-issue-execution.ts b/backend/src/services/certificate-issue-execution.ts index f65e6867..fd2e1a01 100644 --- a/backend/src/services/certificate-issue-execution.ts +++ b/backend/src/services/certificate-issue-execution.ts @@ -13,6 +13,7 @@ type Dependencies = { updateJob: typeof updateIssueJobRecord issue: (input: IssueInput) => Promise appendEvent: typeof appendEvent + admitQueued: (run: () => Promise) => Promise warn: (error: unknown) => void } @@ -71,7 +72,8 @@ export function createCertificateIssueRunner run(jobId))).catch(report) + const task = Promise.resolve().then(() => deps.admitQueued(() => run(jobId))) + void databaseBackgroundWork.track(task).catch(report) } return { run, queue } } diff --git a/backend/src/services/certificate-issue-runner.ts b/backend/src/services/certificate-issue-runner.ts index 40f25106..9a683046 100644 --- a/backend/src/services/certificate-issue-runner.ts +++ b/backend/src/services/certificate-issue-runner.ts @@ -1,4 +1,5 @@ import { appendEvent } from "../modules/events/service/events-service.js" +import { withDetachedDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" import { issueCertificateWithCloudflareDns } from "./acme-cloudflare.js" import { getIssueJobRecord, getServerRowByIdString, updateIssueJobRecord } from "./certificates-service.js" import { createCertificateIssueRunner } from "./certificate-issue-execution.js" @@ -9,7 +10,8 @@ const runner = createCertificateIssueRunner({ updateJob: updateIssueJobRecord, issue: issueCertificateWithCloudflareDns, appendEvent, - warn: error => { console.warn("Certificate issue metadata error", error) }, + admitQueued: withDetachedDatabaseApiRequest, + warn: error => { console.warn("Certificate issue job error", error) }, }) export const runCertificateIssueJob = runner.run export const queueCertificateIssueJob = runner.queue diff --git a/backend/src/services/certificates-service.ts b/backend/src/services/certificates-service.ts index 282e73be..c214b6b9 100644 --- a/backend/src/services/certificates-service.ts +++ b/backend/src/services/certificates-service.ts @@ -1,7 +1,10 @@ +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" import { and, desc, eq, inArray } from "drizzle-orm" import { parseDbServerId } from "../utils/server-id.js" import type { CertificateDto, CertificateRenewSettingsDto } from "@mmapp/contracts/certificates" import { db } from "../db/index.js" +import { mcpDatabaseRequestSignal } from "../modules/mcp/database-request-lease.js" +import { withPartitionWrite } from "./partition-write-transaction.js" import { parseJsonArray } from "../db/json.js" import { acmeSettings, @@ -24,25 +27,24 @@ export type ManagedCertificateTarget = { finishedAt: string } -export async function listCertificatesFromServers(): Promise<{ +export async function listCertificatesFromServers(selectedServers?: (typeof servers.$inferSelect)[]): Promise<{ certificates: CertificateDto[] failures: Array<{ serverId: string; serverName?: string; error: string }> }> { - const allServers = await db.select().from(servers).where(eq(servers.enabled, true)) + const allServers = (selectedServers ?? await db.select().from(servers).where(eq(servers.enabled, true))).filter(server => server.enabled) const certificates: CertificateDto[] = [] const failures: Array<{ serverId: string; serverName?: string; error: string }> = [] await Promise.all( allServers.map(async (server) => { try { - const client = MikrotikClient.fromServer(server) - const rows = await client.getCertificates() + const { rows } = await readCertificateCatalogSource(server) certificates.push(...mapRosCertificates(server.id, server.name, rows)) } catch (e) { failures.push({ serverId: String(server.id), serverName: server.name, - error: e instanceof Error ? e.message : String(e), + error: e instanceof Error && e.name === "ZodError" ? "Некорректный ответ каталога сертификатов" : e instanceof Error ? e.message : "Ошибка RouterOS", }) } }), @@ -159,7 +161,9 @@ export async function updateIssueJobRecord( error: string | null }>, ) { - await db.update(certificateIssueJobs).set(patch).where(eq(certificateIssueJobs.id, id)) + await withPartitionWrite(async ({ db: writer }) => writer.update(certificateIssueJobs).set(patch).where(eq(certificateIssueJobs.id, id)), { + signal: mcpDatabaseRequestSignal(), + }) } export async function getIssueJobRecord(id: string) { @@ -203,7 +207,8 @@ export async function getCertificateRenewSettings(): Promise[0]) { return executeGuardedApplicationWrite({ kind: "certificate-renewal", id: "1" }, "all", () => updateCertificateRenewSettingsImpl(patch)) } +async function updateCertificateRenewSettingsImpl(patch: { enabled?: boolean intervalSec?: number renewBeforeDays?: number @@ -268,8 +273,15 @@ export async function hasActiveIssueJob(serverId: string, certName: string): Pro return rows.some((r) => r.serverId === sid && r.certName === certName) } -export async function listCertificatesState() { - const { collectCertificatesState,readCertificateRowsWithClock } = await import("./certificates-state.js") - const enabledServers = await db.select().from(servers).where(eq(servers.enabled, true)) - return collectCertificatesState(enabledServers, server => { const client=MikrotikClient.fromServer(server); return () => readCertificateRowsWithClock(()=>client.get("/certificate"),()=>client.get("/system/clock")) }) +export async function listCertificatesState(selectedServers?: (typeof servers.$inferSelect)[]) { + const { collectCertificatesState } = await import("./certificates-state.js") + const enabledServers = (selectedServers ?? await db.select().from(servers).where(eq(servers.enabled, true))).filter(server => server.enabled) + return collectCertificatesState(enabledServers, server => () => readCertificateCatalogSource(server)) +} + +async function readCertificateCatalogSource(server: typeof servers.$inferSelect) { + if (!server.enabled) throw new Error("Сервер выключен. Опрос сертификатов недоступен.") + const { parseCertificateCatalogInput, readCertificateRowsWithClock } = await import("./certificates-state.js") + const client = MikrotikClient.fromServer(server) + return parseCertificateCatalogInput(await readCertificateRowsWithClock(() => client.get("/certificate"), () => client.get("/system/clock"))) } diff --git a/backend/src/services/certificates-state.ts b/backend/src/services/certificates-state.ts index 931b608e..5b4b1bd1 100644 --- a/backend/src/services/certificates-state.ts +++ b/backend/src/services/certificates-state.ts @@ -20,6 +20,10 @@ function observedDate(raw:string|undefined,gmtOffset?:string|null) { const qualified=value&&nativeDate.test(value)&&validOffset(gmtOffset)?value.replace(" ","T")+gmtOffset:value return qualified&&dateSchema.safeParse(qualified).success?new Date(qualified):null } +export function parseCertificateCatalogInput(raw: unknown) { + const envelope = Array.isArray(raw) ? null : z.object({rows:rawSchema,gmtOffset:z.string().nullable()}).strict().parse(raw) + return { rows: rawSchema.parse(envelope ? envelope.rows : raw), gmtOffset: envelope?.gmtOffset ?? null } +} export function mapObservedCertificate(server:ServerContext,row:RosCertificateRow,now=new Date(),gmtOffset?:string|null) { const base=mapRosCertificateRow(server.id,server.name,row) if(!base)throw new Error("Отсутствует сертификат") @@ -40,9 +44,8 @@ export function mapObservedCertificate(server:ServerContext,row:RosCertificateRo } export async function readCertificateServerState(server:ServerContext,read:()=>Promise):Promise { try{ - const raw=await read(),envelope=Array.isArray(raw)?null:z.object({rows:rawSchema,gmtOffset:z.string().nullable()}).strict().parse(raw) - const rows=rawSchema.parse(envelope?envelope.rows:raw),now=new Date() - const result={serverId:String(server.id),serverName:server.name,collectedAt:now.toISOString(),certificates:rows.map(row=>mapObservedCertificate(server,row,now,envelope?.gmtOffset)),error:null} + const {rows,gmtOffset}=parseCertificateCatalogInput(await read()),now=new Date() + const result={serverId:String(server.id),serverName:server.name,collectedAt:now.toISOString(),certificates:rows.map(row=>mapObservedCertificate(server,row,now,gmtOffset)),error:null} return certificateServerStateSchema.parse(result) }catch(error){ return certificateServerStateSchema.parse({serverId:String(server.id),serverName:server.name,collectedAt:new Date().toISOString(),certificates:[],error:error instanceof z.ZodError?"Некорректный ответ каталога сертификатов":error instanceof Error?error.message.slice(0,500)||"Ошибка RouterOS":"Ошибка RouterOS"}) diff --git a/backend/src/services/collector-partition-writes.sql-helper.ts b/backend/src/services/collector-partition-writes.sql-helper.ts new file mode 100644 index 00000000..adcf7d92 --- /dev/null +++ b/backend/src/services/collector-partition-writes.sql-helper.ts @@ -0,0 +1,187 @@ +import assert from "node:assert/strict" +import http from "node:http" +import { randomUUID } from "node:crypto" +import { basename,dirname,resolve } from "node:path" +import type { PoolClient,QueryConfig } from "pg" +import type { PartitionWriteOptions } from "./partition-write-transaction.js" +import type { PingProbeWrite } from "@mmapp/contracts" + +/** Owned disposable PostgreSQL + actual exported application collectors. All + * RouterOS traffic goes to this function's exact authenticated loopback HTTP + * endpoint. Optional internal SQL options instrument real checked-out clients; + * neither global pools nor the production writer helper are replaced. */ +export async function acceptCollectorPartitionWritesSql(){ + assert(process.env.MMAPP_OWN_COLLECTOR_PARTITION_WRITERS==="true"||process.env.MMAPP_OWN_PROBE_JOURNAL==="true","Owned disposable collector writer runner required") + const database=new URL(process.env.DATABASE_URL??"") + assert.equal(database.hostname,"127.0.0.1");assert.equal(database.pathname,"/postgres") + assert(["postgres:","postgresql:"].includes(database.protocol)) + assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const {pool}=await import("../db/index.js") + const {withPartitionWrite,isPartitionWriteOutcomeUnknown,PartitionWriteError,PARTITION_WRITER_ADVISORY_KEY:key}=await import("./partition-write-transaction.js") + const {PARTITION_SPECS,ensurePartitionFor}=await import("../db/partitions.js") + const {collectTrafficOnce}=await import("./traffic-collector.js") + const {collectResourceSamplesOnce,collectPingProbesOnce,collectPingForProbeIds,replaceProbes}=await import("./uptime-collector.js") + const {collectServersRestPingOnce}=await import("./servers-rest-ping-collector.js") + const {collectInternetPathSnapshotOnce}=await import("./internet-path-collector.js") + const {deleteServerRowById}=await import("../modules/servers/repository/servers-repository.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name,"postgres") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE enabled=true")).rows[0].n,0,"Isolated profile must disable other native targets before invoking actual collectors") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_probes")).rows[0].n,0,"Dedicated empty probe catalogue required; foreign probes are never replaced") + const settingsTables=["traffic_settings","uptime_settings","servers_api_ping_settings","internet_path_settings"] + const savedSettings=new Map>() + for(const table of settingsTables){const row=(await pool.query(`SELECT to_jsonb(s) AS row FROM ${table} s WHERE id=1`)).rows[0]?.row;assert(row,"Existing singleton settings required");savedSettings.set(table,row)} + const ownedServers:number[]=[],ownedProbes:string[]=[],watched=new WeakSet() + let sourceId=0,activeProbe="",nativeFailure=false,nativeRequests=0,cases=0,observedWaits=0,actualKnownAdmissionFailures=0,lostCommitAcks=0 + const username="owned-collector-user",password="owned-collector-"+randomUUID(),name="Owned collector SQL fixture" + const auth="Basic "+Buffer.from(username+":"+password).toString("base64") + const responses:Record={ + "/rest/system/identity":[{name}], + "/rest/system/resource":[{"cpu-load":"10","free-memory":"1048576","total-memory":"2097152","free-hdd-space":"1048576","total-hdd-space":"4194304",uptime:"1d2h3m4s","board-name":"owned-fixture",version:"7.20"}], + "/rest/interface":[{".id":"*1",name:"ether1",type:"ether",ifindex:"1",running:"true",disabled:"false","rx-byte":"1234","tx-byte":"4321"}], + "/rest/interface/wireguard/peers":[],"/rest/interface/gre":[], + "/rest/ip/address":[{address:"192.0.2.2/24",interface:"ether1"}], + "/rest/ip/dhcp-client":[{interface:"ether1",address:"192.0.2.2/24",gateway:"192.0.2.254",status:"bound"}], + "/rest/ip/route":[{".id":"*1","dst-address":"0.0.0.0/0",gateway:"192.0.2.254","immediate-gw":"192.0.2.254%ether1",active:"true","routing-table":"main",disabled:"false",inactive:"false",distance:"1"}], + "/rest/tool/ping":[{sent:"4",received:"4","packet-loss":"0%","avg-rtt":"1ms"}], + } + const server=http.createServer((req,res)=>{ + nativeRequests++;req.resume();res.setHeader("Content-Type","application/json") + if(req.headers.authorization!==auth){res.statusCode=401;res.end(JSON.stringify({error:"owned fixture authorization refused"}));return} + if(!req.url||!(req.url in responses)||!(req.method==="GET"||req.method==="POST"&&req.url==="/rest/tool/ping")){res.statusCode=404;res.end(JSON.stringify({error:"unapproved fixture request"}));return} + if(nativeFailure){res.statusCode=503;res.end(JSON.stringify({error:"synthetic native measurement failure"}));return} + res.end(JSON.stringify(responses[req.url])) + }) + await new Promise((accept,reject)=>{server.once("error",reject);server.listen(0,"127.0.0.1",()=>{server.removeListener("error",reject);accept()})}) + const address=server.address();assert(address&&typeof address!=="string"&&address.address==="127.0.0.1") + const port=address.port + const sql=(client:Pick,text:string,values:unknown[]=[])=>client.query({text,values,query_timeout:6000}as QueryConfig&{query_timeout:number}) + const waitFor=async(label:string,inspect:()=>Promise)=>{const until=Date.now()+10000;while(Date.now()setTimeout(resolve,10))};assert.fail("Timed out observing "+label)} + let holder!:PoolClient + const watchedSource=async(id:number,enabled:boolean,committed=false)=>{ + const rows=(await pool.query("SELECT host,port,username,password,use_ssl,verify_ssl,enabled FROM servers WHERE id=$1",[id])).rows + assert.deepEqual((await pool.query("SELECT id FROM servers WHERE enabled=true ORDER BY id LIMIT 1001")).rows.map(row=>Number(row.id)),[sourceId]) + if(!rows.length&&committed)return + assert.equal(rows.length,1,"Owned source disappeared before confirmed commit") + assert.deepEqual(rows[0],{host:"127.0.0.1",port,username,password,use_ssl:false,verify_ssl:true,enabled}) + } + const instrument=(id=sourceId,enabled=true,mode:"normal"|"known"|"unknown"="normal")=>{ + const trace:string[]=[],codes:string[]=[],releases:boolean[]=[],pids:number[]=[] + let committed=false + const options:PartitionWriteOptions={deadlineAt:Date.now()+20000,authorize:()=>watchedSource(id,enabled,committed),connect:async()=>{ + const client=await pool.connect();if(!watched.has(client)){watched.add(client);client.on("error",()=>{})} + const pid=Number((await sql(client,"SELECT pg_backend_pid() AS pid")).rows[0].pid);pids.push(pid) + return new Proxy(client,{get(target,property){ + if(property==="release")return(destroy?:boolean)=>{releases.push(!!destroy);target.release(destroy)} + if(property==="query")return async(...args:unknown[])=>{ + const input=args[0],text=typeof input==="string"?input:String((input as {text:string}).text);trace.push(text) + if(mode==="known"&&text==="SELECT pg_advisory_xact_lock_shared($1::bigint)")await sql(target,"SET LOCAL lock_timeout='100ms'") + if(/^\s*(INSERT|UPDATE|DELETE)\b/i.test(text))assert.equal((await sql(pool,"SELECT count(*)::int AS n FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0 AND objid=$2 AND objsubid=1 AND mode='ShareLock' AND granted",[pid,key])).rows[0].n,1,"Target writer must own shared admission on that same backend") + let result:unknown + try{result=await Reflect.apply(target.query,target,args)}catch(error){if(error&&typeof error==="object"&&"code"in error)codes.push(String(error.code));throw error} + if(text==="COMMIT"){committed=true;if(mode==="unknown"){lostCommitAcks++;throw Error("Synthetic lost ACK AFTER actual application collector COMMIT")}} + return result + } + const value=Reflect.get(target,property,target);return typeof value==="function"?value.bind(target):value + }}) + }} + return {options,trace,codes,releases,pids} + } + const waiting=async(f:ReturnType)=>{ + const pid=await waitFor("actual collector backend",async()=>f.pids[0]||undefined) + await waitFor("collector shared wait before relation SQL",async()=>{const rows=(await sql(pool,"SELECT pid FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0 AND objid=$2 AND objsubid=1 AND mode='ShareLock' AND NOT granted",[pid,key])).rows;return rows.length===1?true:undefined}) + observedWaits++ + assert.equal((await sql(pool,"SELECT count(*)::int AS n FROM pg_locks l JOIN pg_class c ON c.oid=l.relation JOIN pg_namespace n ON n.oid=c.relnamespace WHERE l.pid=$1 AND l.locktype='relation' AND n.nspname='public'",[pid])).rows[0].n,0,"Blocked collector must hold no application relation locks") + assert(!f.trace.some(text=>/^\s*(INSERT|UPDATE|DELETE)\b/i.test(text))) + } + const freshProbe=async()=>{ + if(ownedProbes.length)await pool.query("UPDATE uptime_probes SET enabled=false WHERE id=ANY($1::text[])",[ownedProbes]) + activeProbe="owned-collector-probe-"+randomUUID();ownedProbes.push(activeProbe) + await pool.query("INSERT INTO uptime_probes(id,src_server_id,name,target,enabled,interval_sec) VALUES($1,$2,'Owned actual ping','192.0.2.1',true,0)",[activeProbe,sourceId]) + } + const count=async(table:string)=>Number((await pool.query(table==="internet_path_snapshots"?"SELECT count(*)::int AS n FROM internet_path_snapshots":table==="uptime_probe_samples"?"SELECT count(*)::int AS n FROM uptime_probe_samples WHERE probe_id=$1":`SELECT count(*)::int AS n FROM ${table} WHERE server_id=$1`,table==="internet_path_snapshots"?[]:[table==="uptime_probe_samples"?activeProbe:sourceId])).rows[0].n) + const held=async(work:()=>Promise)=>{await sql(holder,"SELECT pg_advisory_lock($1::bigint)",[key]);try{return await work()}finally{await sql(holder,"SELECT pg_advisory_unlock($1::bigint)",[key])}} + const positive=async(run:(options:PartitionWriteOptions)=>Promise,f:ReturnType)=>{ + await sql(holder,"SELECT pg_advisory_lock($1::bigint)",[key]);const pending=run(f.options);void pending.catch(()=>{}) + let failure:unknown + try{await waiting(f)}catch(error){failure=error}finally{await sql(holder,"SELECT pg_advisory_unlock($1::bigint)",[key])} + try{await pending}catch(error){if(!failure)failure=error} + if(failure)throw failure + assert.equal(f.pids.length,1);assert.equal(f.trace.filter(text=>text==="COMMIT").length,1);assert.deepEqual(f.releases,[false]) + } + try{ + holder=await pool.connect() + sourceId=Number((await pool.query("INSERT INTO servers(name,host,port,username,password,use_ssl,verify_ssl,enabled,type) VALUES($1,'127.0.0.1',$2,$3,$4,false,true,true,'home-router') RETURNING id",[name,port,username,password])).rows[0].id);ownedServers.push(sourceId) + for(const spec of PARTITION_SPECS)await ensurePartitionFor(pool,spec.parent,spec.kind,new Date()) + const entries:{id:string;table:string;ping?:boolean;manual?:boolean;run:(options:PartitionWriteOptions)=>Promise}[]=[ + {id:"traffic",table:"traffic_samples",run:collectTrafficOnce}, + {id:"resources",table:"uptime_resource_samples",run:collectResourceSamplesOnce}, + {id:"scheduled-ping",table:"uptime_probe_samples",ping:true,run:collectPingProbesOnce}, + {id:"manual-ping",table:"uptime_probe_samples",ping:true,manual:true,run:options=>collectPingForProbeIds([activeProbe],options)}, + {id:"REST-ping",table:"servers_rest_ping_samples",run:collectServersRestPingOnce}, + {id:"internet-path",table:"internet_path_snapshots",run:collectInternetPathSnapshotOnce}, + ] + for(const entry of entries){ + nativeFailure=false;if(entry.ping)await freshProbe() + let before=await count(entry.table);const requests=nativeRequests + const admitted=instrument();await positive(entry.run,admitted);assert.equal(await count(entry.table),before+1,entry.id+" actual publication");assert(nativeRequests>requests);cases++ + if(entry.ping)await freshProbe();before=await count(entry.table) + const denied=instrument(sourceId,true,"known") + await held(async()=>{if(entry.manual)await assert.rejects(entry.run(denied.options),(error:unknown)=>error instanceof PartitionWriteError&&!error.outcomeUnknown);else {const result=await entry.run(denied.options);assert.equal(typeof Reflect.get(result as object,"fatalError"),"string")}}) + assert(denied.codes.includes("55P03"));actualKnownAdmissionFailures++;assert.equal(denied.pids.length,1);assert(!denied.trace.some(text=>/^\s*(INSERT|UPDATE|DELETE)\b/i.test(text)));assert(!denied.trace.includes("COMMIT"));assert.equal(await count(entry.table),before,entry.id+" must not fabricate offline/down after admission refusal");cases++ + if(entry.ping)await freshProbe();before=await count(entry.table) + const unknown=instrument(sourceId,true,"unknown");await assert.rejects(entry.run(unknown.options),isPartitionWriteOutcomeUnknown) + assert.equal(unknown.pids.length,1);assert.equal(unknown.trace.filter(text=>text==="COMMIT").length,1);assert(!unknown.trace.includes("ROLLBACK"));assert.deepEqual(unknown.releases,[true]);assert.equal(await count(entry.table),before+1,entry.id+" actually committed exactly once");cases++ + } + // Real provider-error publication lanes, using actual loopback failures. + // REST is last so its historical multi-row recovery lane is not conflated + // with writer-admission acceptance or silently changed by this fixture. + for(const id of ["resources","scheduled-ping","manual-ping","REST-ping"]){ + const entry=entries.find(entry=>entry.id===id)!;if(entry.ping)await freshProbe();const before=await count(entry.table),f=instrument();nativeFailure=true + await entry.run(f.options);nativeFailure=false + assert.equal(await count(entry.table),before+1);assert.equal(f.pids.length,1);assert.equal(f.trace.filter(text=>text==="COMMIT").length,1);cases++ + } + const probeWithSample=async()=>{await freshProbe();await withPartitionWrite(async({client})=>{await client.query("INSERT INTO uptime_probe_samples(probe_id,sampled_at,rtt_ms,loss_pct,status) VALUES($1,$2,1,0,'up')",[activeProbe,new Date().toISOString()])});return activeProbe} + const replacement=async(remove:string)=>{ + const rows=(await pool.query("SELECT * FROM uptime_probes ORDER BY sort_order,id")).rows;assert(rows.every(row=>ownedProbes.includes(String(row.id))),"Never replace foreign probe IDs") + return rows.filter(row=>row.id!==remove).map((row):PingProbeWrite=>({id:row.id,srcServerId:Number(row.src_server_id),srcInterface:row.src_interface,name:row.name,target:row.target,filter:row.probe_filter,enabled:row.enabled,intervalSec:row.interval_sec,showOnDashboard:row.show_on_dashboard})) + } + let remove=await probeWithSample(),replaceRows=await replacement(remove),f=instrument(sourceId,true,"known") + await held(()=>assert.rejects(replaceProbes(replaceRows,f.options),(error:unknown)=>error instanceof PartitionWriteError&&!error.outcomeUnknown));assert(f.codes.includes("55P03"));actualKnownAdmissionFailures++;assert.equal(await count("uptime_probe_samples"),1);cases++ + f=instrument();await positive(options=>replaceProbes(replaceRows,options),f);assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_probe_samples WHERE probe_id=$1",[remove])).rows[0].n,0);assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_probes WHERE id=$1",[remove])).rows[0].n,0);cases++ + remove=await probeWithSample();replaceRows=await replacement(remove);f=instrument(sourceId,true,"unknown");await assert.rejects(replaceProbes(replaceRows,f.options),isPartitionWriteOutcomeUnknown);assert.equal(f.trace.filter(text=>text==="COMMIT").length,1);assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_probe_samples WHERE probe_id=$1",[remove])).rows[0].n,0);cases++ + const cascadeFixture=async()=>{ + const id=Number((await pool.query("INSERT INTO servers(name,host,port,username,password,use_ssl,verify_ssl,enabled) VALUES('Owned collector FK fixture','127.0.0.1',$1,$2,$3,false,true,false) RETURNING id",[port,username,password])).rows[0].id);ownedServers.push(id) + const probe="owned-cascade-"+randomUUID();ownedProbes.push(probe);const now=new Date().toISOString(),day=now.slice(0,10) + await withPartitionWrite(async({client})=>{ + await client.query("INSERT INTO uptime_probes(id,src_server_id,name,target,enabled) VALUES($1,$2,'Owned cascade','192.0.2.1',false)",[probe,id]) + await client.query("INSERT INTO uptime_probe_samples(probe_id,sampled_at,status) VALUES($1,$2,'up')",[probe,now]) + await client.query("INSERT INTO server_snapshots(server_id,polled_at,status) VALUES($1,$2,'online')",[id,now]) + await client.query("INSERT INTO traffic_samples(server_id,interface_name,sampled_at,rx_bytes) VALUES($1,'owned-fk',$2,1)",[id,now]) + await client.query("INSERT INTO servers_rest_ping_samples(server_id,sampled_at,ok) VALUES($1,$2,true)",[id,now]) + await client.query("INSERT INTO uptime_resource_samples(server_id,sampled_at,status) VALUES($1,$2,'online')",[id,now]) + await client.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst) VALUES($1,$2,'192.0.2.1','192.0.2.2')",[id,now]) + await client.query("INSERT INTO flow_minute_stats(server_id,bucket_at) VALUES($1,$2)",[id,now]) + await client.query("INSERT INTO flow_minute_dims(server_id,bucket_at,dim,key) VALUES($1,$2,'owned','owned')",[id,now]) + await client.query("INSERT INTO flow_daily_dims(server_id,day,dim,key) VALUES($1,$2,'owned','owned')",[id,day]) + await client.query("INSERT INTO flow_hour_facts(server_id,bucket_at,iface,country,service,asn) VALUES($1,$2,'owned','XX','owned',0)",[id,now]) + await client.query("INSERT INTO flow_daily_facts(server_id,day,iface,country,service,asn) VALUES($1,$2,'owned','XX','owned',0)",[id,day]) + }) + return {id,probe} + } + const assertCascade=async(id:number,probe:string,expected:number)=>{for(const table of PARTITION_SPECS.map(spec=>spec.parent).filter(table=>table!=="internet_path_snapshots"&&table!=="uptime_probe_samples"))assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table} WHERE server_id=$1`,[id])).rows[0].n,expected,table);assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_probe_samples WHERE probe_id=$1",[probe])).rows[0].n,expected)} + let cascade=await cascadeFixture();await assertCascade(cascade.id,cascade.probe,1);f=instrument(cascade.id,false,"known") + await held(()=>assert.rejects(deleteServerRowById(cascade.id,f.options),(error:unknown)=>error instanceof PartitionWriteError&&!error.outcomeUnknown));assert(f.codes.includes("55P03"));actualKnownAdmissionFailures++;await assertCascade(cascade.id,cascade.probe,1);cases++ + f=instrument(cascade.id,false);await positive(options=>deleteServerRowById(cascade.id,options),f);await assertCascade(cascade.id,cascade.probe,0);cases++ + cascade=await cascadeFixture();f=instrument(cascade.id,false,"unknown");await assert.rejects(deleteServerRowById(cascade.id,f.options),isPartitionWriteOutcomeUnknown);assert.equal(f.trace.filter(text=>text==="COMMIT").length,1);assert(!f.trace.includes("ROLLBACK"));await assertCascade(cascade.id,cascade.probe,0);cases++ + assert.equal(cases,28);assert.equal(observedWaits,8);assert.equal(actualKnownAdmissionFailures,8);assert.equal(lostCommitAcks,8) + return {cases,actualCollectorEntryPoints:6,actualProbeReplacement:true,actualServerRepositoryDelete:true,actualApplicationFkCascadeParents:11,observedPreRelationAdvisoryWaits:observedWaits,actualKnownAdmissionFailures,lostActualCommitAcks:lostCommitAcks,realDrizzle:true,routerOsTransport:"owned authenticated 127.0.0.1 HTTP fixture",poolMonkeypatch:false,productionHelperReplacement:false,nativeExternalIo:false,limits:["No HTTP route or user authentication middleware acceptance; trusted internal entrypoints only","REST multi-sample recovery behavior is outside this writer-admission scope","No claim of durable cross-restart collector/delete replay reconciliation","Fixture IDs remain disabled; append-only evidence is never deleted"]} + }finally{ + nativeFailure=false + try{ + if(holder){await sql(holder,"SELECT pg_advisory_unlock_all()").catch(()=>{});holder.release(true)} + if(ownedProbes.length)await pool.query("UPDATE uptime_probes SET enabled=false WHERE id=ANY($1::text[])",[ownedProbes]) + if(ownedServers.length)await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])",[ownedServers]) + for(const [table,row]of savedSettings)await pool.query(`UPDATE ${table} SET last_collected_at=$1,last_duration_ms=$2,last_error=$3,updated_at=$4 WHERE id=1`,[row.last_collected_at,row.last_duration_ms,row.last_error,row.updated_at]) + }finally{server.closeAllConnections();await new Promise((accept,reject)=>server.close(error=>error?reject(error):accept()))} + } +} diff --git a/backend/src/services/collector-partition-writes.sql.test.ts b/backend/src/services/collector-partition-writes.sql.test.ts new file mode 100644 index 00000000..773c3033 --- /dev/null +++ b/backend/src/services/collector-partition-writes.sql.test.ts @@ -0,0 +1,22 @@ +import assert from "node:assert/strict" +import { basename,dirname,resolve } from "node:path" + +// Standalone private harness admission precedes all configured pool/runtime +// imports. This profile never runs against an existing application database. +assert.equal(process.env.MMAPP_OWN_COLLECTOR_PARTITION_WRITERS,"true") +const database=new URL(process.env.DATABASE_URL??"") +assert.equal(database.hostname,"127.0.0.1");assert.equal(database.pathname,"/postgres") +assert(["postgres:","postgresql:"].includes(database.protocol)) +assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const {pool,closePool}=await import("../db/index.js") +try{ + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name,"postgres") + const {applySqlMigrations}=await import("../db/migrate.js") + await applySqlMigrations(pool) + const {acceptCollectorPartitionWritesSql}=await import("./collector-partition-writes.sql-helper.js") + const result=await acceptCollectorPartitionWritesSql() + assert.equal(result.cases,28) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE enabled=true")).rows[0].n,0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM uptime_probes WHERE enabled=true")).rows[0].n,0) + console.log("Actual collector writer/application FK acceptance:",JSON.stringify(result)) +}finally{await closePool()} diff --git a/backend/src/services/collector-partition-writes.test.ts b/backend/src/services/collector-partition-writes.test.ts new file mode 100644 index 00000000..9b6b44e2 --- /dev/null +++ b/backend/src/services/collector-partition-writes.test.ts @@ -0,0 +1,87 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import { replacePingProbes } from "./ping-probe-replacement.js" + +/** Execute the actual collector source with inert module providers. Production + * imports, configured pools, sockets and native clients are never initialized. + * The real shared-lock transaction helper has its own boundary/SQL tests; this + * contract verifies collectors use its owned ORM and propagate unknown commits. */ +type Row=Record +const partitionTables=new Set(["trafficSamples","uptimeResourceSamples","uptimeProbeSamples","serversRestPingSamples","internetPathSnapshots"]) +const tables=new Proxy({} as Record,{get(target,key:string){return target[key]??=new Proxy({__table:key},{get(row,column:string){return column==="__table"?row.__table:{table:key,column}}})}}) +function tableName(value:unknown){assert(value&&typeof value==="object");return String(Reflect.get(value,"__table"))} +class FakePartitionWriteError extends Error {constructor(readonly outcomeUnknown=false){super("Synthetic partition admission/write failure")}} +class UnknownCommit extends FakePartitionWriteError {constructor(_message:string){super(true)}} +type CollectorExports=RecordPromise> +async function fixture(file:string){ + let failNative=false,unknown=false,previousRestDown=false,admissionDenied=false + const source={id:1,name:"Owned fixture",host:"127.0.0.1",enabled:true,updatedAt:"2026-10-25T00:00:00.000Z"},probe={id:"keep",name:"Ping",target:"192.0.2.1",srcServerId:1,srcInterface:"",enabled:true,sortOrder:0,intervalSec:0} + const writes:{table:string;value:unknown;operation:string}[]=[],events:string[]=[],globalWrites:string[]=[] + const read=(table:string)=>table==="servers"?[source]:table==="uptimeProbes"?[probe,{...probe,id:"remove"}]:["uptimeSettings","trafficSettings","serversApiPingSettings","internetPathSettings"].includes(table)?[{id:1,enabled:true,intervalSec:300,probeIntervalSec:15,retentionDays:14}]:[] + const makeDb=(owned:boolean)=>{ + const mutation=(operation:string,table:unknown)=>{ + const name=tableName(table);let value:unknown + const query={values(input:unknown){value=input;return query},where(){return query},set(input:unknown){value=input;return query},onConflictDoUpdate(){return query},returning(){return query},then(resolve:(rows:unknown[])=>unknown,reject?:(error:unknown)=>unknown){ + return Promise.resolve().then(()=>{if(owned){events.push(operation+" "+name);writes.push({table:name,value,operation})}else {globalWrites.push(name);assert(!partitionTables.has(name),"Partition write escaped owned backend: "+name)};return []}).then(resolve,reject) + }} + return query + } + return {insert:(table:unknown)=>mutation("INSERT",table),update:(table:unknown)=>mutation("UPDATE",table),delete:(table:unknown)=>mutation("DELETE",table),execute:async()=>{assert(owned);events.push("probe catalogue lock")},select:()=>{ + let name="" + const query={from(table:unknown){name=tableName(table);return query},where(){return query},orderBy(){return query},limit(){return query},groupBy(){return query},$dynamic(){return query},then(resolve:(rows:Row[])=>unknown,reject?:(error:unknown)=>unknown){if(owned)events.push("SELECT "+name);return Promise.resolve(read(name)).then(resolve,reject)}} + return query + }} + } + const ownerDb=makeDb(true),globalDb=makeDb(false) + const withPartitionWrite=async(work:(owner:{db:typeof ownerDb;client:object})=>Promise,options?:{authorize?:()=>Promise})=>{ + await options?.authorize?.();events.push("BEGIN","SHARED 8723110");if(admissionDenied)throw new FakePartitionWriteError();const result=await work({db:ownerDb,client:{}});events.push("COMMIT");if(unknown)throw new UnknownCommit("actual COMMIT ACK lost");return result + } + const native=async(value:unknown)=>{if(failNative)throw Error("Synthetic native measurement failed");return value} + const mikrotik={fromServer:()=>({getIdentity:()=>native({name:"fixture"}),getResource:()=>native({"cpu-load":"10","free-memory":"100","total-memory":"200","free-hdd-space":"100","total-hdd-space":"200",uptime:"100"}),ping:()=>native([{sent:"4",received:"4","packet-loss":"0%","avg-rtt":"1ms"}]),get:(path:string)=>native(path==="/interface"?[{".id":"*1",name:"ether1","rx-byte":"100","tx-byte":"200",running:"true",disabled:"false"}]:[])})} + const modules:Record={ + "./partition-write-transaction.js":{withPartitionWrite,PartitionWriteError:FakePartitionWriteError,isPartitionWriteOutcomeUnknown:(error:unknown)=>error instanceof FakePartitionWriteError&&error.outcomeUnknown}, + "../db/index.js":{db:globalDb,pool:{connect:()=>{throw Error("Real SQL acquisition forbidden")}},dbAll:async(sql:string)=>previousRestDown&&sql.includes("servers_rest_ping_samples")?[{serverId:1,ok:false}]:[]}, + "../db/schema.js":tables, + "drizzle-orm":{and:()=>null,asc:()=>null,desc:()=>null,eq:()=>null,gte:()=>null,lte:()=>null,inArray:()=>null,max:()=>null,sql:()=>null}, + "./config-mutation-lock.js":{withApplicationCollectorLock:(work:()=>Promise)=>work()}, + "./config-mutation-policy.js":{executeGuardedApplicationWrite:(_target:unknown,_scope:unknown,work:()=>Promise)=>work()}, + "./monitoring-server-catalog.js":{listEnabledMonitoringServers:async()=>[source]}, + "./mikrotik.js":{MikrotikClient:mikrotik}, + "../types/scheduler-run-snapshot.js":{SCHEDULER_RUN_SNAPSHOT_VERSION:1}, + "../constants/ping-probe.js":{PING_PROBE_WARN_RTT_MS:100}, + "./ros-metric-parse.js":{parseRosCpuLoadPercent:Number,parseRosDataSizeBytes:Number,parseRosUptimeToSeconds:Number}, + "./ping-probe-replacement.js":{replacePingProbes}, + "./traffic-collection-lock.js":{withTrafficCollectionLock:async(_connect:unknown,run:(active:()=>void)=>Promise)=>({acquired:true,value:await run(()=>{})})}, + "../db/traffic-flags.js":{decodeTrafficSampleFlags:()=>({countersKnown:true}),encodeTrafficFlags:()=>0,trafficCounterContinuity:()=>false}, + "./traffic-rate.js":{bpsToMbps:(n:number)=>n/1e6,shouldIncludeIface:()=>true}, + "./traffic-observation.js":{observedTrafficRate:()=>({rxBps:0,txBps:0,measured:false}),parseTrafficCounter:Number,parseTrafficFlag:(v:string)=>v==="true",nativeTrafficCounterIdentity:()=>"identity"}, + "./traffic-flow-ifindex.js":{rememberServerIfaces:()=>{}}, + "./traffic-collection-state.js":{recordTrafficCollectionAttempt:async()=>{}}, + "./traffic-peer-observation.js":{parseTrafficPeers:()=>[]}, + "./internet-path-read.js":{readLatestInternetPathSnapshot:async()=>null}, + "../modules/servers/service/servers-service.js":{listServersRead:async()=>[source]}, + "./internet-path-snapshot.js":{collectConfirmedInternetPath:()=>native({servers:[{...source,type:"home-router"}]}),readInternetPathRows:()=>[]}, + } + const code=await readFile(new URL(file,import.meta.url),"utf8"),compiled=ts.transpileModule(code,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText + const exports={} as CollectorExports + runInNewContext(compiled,{exports,require:(name:string)=>{assert(name in modules,"Unapproved production dependency: "+name);return modules[name]},console:{warn:()=>{}},Date,performance,Error,Set,Map,Promise,Number,String,Math,Object,JSON},{filename:file}) + return {exports,writes,events,globalWrites,nativeFailure:()=>{failNative=true},loseCommit:()=>{unknown=true},denyAdmission:()=>{admissionDenied=true},restore:()=>{failNative=false;unknown=false;admissionDenied=false},restWasDown:()=>{previousRestDown=true}} +} +let cases=0 +async function test(name:string,work:()=>Promise){await work();cases++;console.log("ok",name)} +for(const [file,method,table]of [["traffic-collector.ts","collectTrafficOnce","trafficSamples"],["uptime-collector.ts","collectResourceSamplesOnce","uptimeResourceSamples"],["uptime-collector.ts","collectPingProbesOnce","uptimeProbeSamples"],["uptime-collector.ts","collectPingForProbeIds","uptimeProbeSamples"],["servers-rest-ping-collector.ts","collectServersRestPingOnce","serversRestPingSamples"],["internet-path-collector.ts","collectInternetPathSnapshotOnce","internetPathSnapshots"]]){ + const args=method==="collectPingForProbeIds"?[["keep"]]:[] + await test(method+" uses admitted owned ORM",async()=>{const f=await fixture(file);await f.exports[method](...args);assert(f.writes.some(w=>w.table===table));assert.equal(f.events[0],"BEGIN");assert.equal(f.events[1],"SHARED 8723110");assert(!f.globalWrites.includes(table))}) + await test(method+" propagates unknown commit without fallback write",async()=>{const f=await fixture(file);f.loseCommit();await assert.rejects(f.exports[method](...args),UnknownCommit);assert.equal(f.events.filter(e=>e==="COMMIT").length,1);const rows=f.writes.filter(w=>w.table===table);assert.equal(rows.length,1);assert(!rows.some(w=>typeof w.value==="object"&&w.value!==null&&["offline","down"].includes(String(Reflect.get(w.value,"status")))));f.restore();await f.exports[method](...args);assert.equal(f.events.filter(e=>e==="COMMIT").length,method==="collectPingProbesOnce"||method==="collectPingForProbeIds"?3:2)}) + await test(method+" known admission failure publishes no network fallback",async()=>{const f=await fixture(file);f.denyAdmission();if(method==="collectPingForProbeIds")await assert.rejects(f.exports[method](...args),FakePartitionWriteError);else {const result=await f.exports[method](...args);assert.equal(typeof Reflect.get(result as object,"fatalError"),"string")};assert.equal(f.writes.length,0);assert.equal(f.events.filter(e=>e==="BEGIN").length,1);assert.equal(f.events.filter(e=>e==="COMMIT").length,0)}) +} +for(const [file,method,table,status]of [["uptime-collector.ts","collectResourceSamplesOnce","uptimeResourceSamples","offline"],["uptime-collector.ts","collectPingProbesOnce","uptimeProbeSamples","down"],["uptime-collector.ts","collectPingForProbeIds","uptimeProbeSamples","down"]])await test(method+" preserves ordinary failure sample on owned ORM",async()=>{const f=await fixture(file);f.nativeFailure();await f.exports[method](...(method==="collectPingForProbeIds"?[["keep"]]:[]));const row=f.writes.find(w=>w.table===table);assert(row&&row.value&&typeof row.value==="object");assert.equal(Reflect.get(row.value,"status"),status);assert.equal(f.events[1],"SHARED 8723110")}) +await test("REST recovery retains both samples within one admitted transaction",async()=>{const f=await fixture("servers-rest-ping-collector.ts");f.restWasDown();await f.exports.collectServersRestPingOnce();assert.equal(f.writes.filter(w=>w.table==="serversRestPingSamples").length,2);assert.equal(f.events.filter(e=>e==="COMMIT").length,1)}) +await test("REST native failure retains the owned failure observation",async()=>{const f=await fixture("servers-rest-ping-collector.ts");f.nativeFailure();await f.exports.collectServersRestPingOnce();const rows=f.writes.filter(w=>w.table==="serversRestPingSamples");assert.equal(rows.length,1);assert.equal(Reflect.get(rows[0].value as object,"ok"),false);assert.equal(f.events[1],"SHARED 8723110")}) +await test("traffic native failure publishes no fabricated partition sample",async()=>{const f=await fixture("traffic-collector.ts");f.nativeFailure();await f.exports.collectTrafficOnce();assert.equal(f.writes.length,0)}) +await test("internet path native failure retains snapshotSaved=false",async()=>{const f=await fixture("internet-path-collector.ts");f.nativeFailure();const result=await f.exports.collectInternetPathSnapshotOnce();assert.equal(Reflect.get(result as object,"snapshotSaved"),false);assert.equal(f.writes.length,0)}) +await test("probe replacement admits before catalogue lock/read/cascading delete",async()=>{const f=await fixture("uptime-collector.ts");await f.exports.replaceProbes([]);assert.deepEqual(f.events.slice(0,4),["BEGIN","SHARED 8723110","probe catalogue lock","SELECT uptimeProbes"]);assert.equal(f.writes.filter(w=>w.table==="uptimeProbes"&&w.operation==="DELETE").length,2)}) +await test("probe replacement never swallows uncertain cascade commit",async()=>{const f=await fixture("uptime-collector.ts");f.loseCommit();await assert.rejects(f.exports.replaceProbes([]),UnknownCommit);assert.equal(f.events.filter(e=>e==="COMMIT").length,1)}) +console.log(`Collector partition writer adoption: ${cases} inert source-execution scenarios passed`) diff --git a/backend/src/services/config-apply-plan.test.ts b/backend/src/services/config-apply-plan.test.ts index 8d72f6bd..848e9ded 100644 --- a/backend/src/services/config-apply-plan.test.ts +++ b/backend/src/services/config-apply-plan.test.ts @@ -46,6 +46,8 @@ import { mapRosManagedRoutes } from "./config-apply-plan.js" const noop = planBgpInApply([], 0) assert.equal(noop.action, "noop") + assert.equal(planBgpInApply([{ ".id": "*case", chain: "BGP-IN", comment: "MikrotikManager: other", rule: "bgp-communities" }], 1).action, "create") + assert.deepEqual(planBgpInApply([{ ".id": "*case", chain: "BGP-IN", rule: "bgp-communities" }], 1).conflictIds, []) } { diff --git a/backend/src/services/config-apply-plan.ts b/backend/src/services/config-apply-plan.ts index c82f81cf..308469e0 100644 --- a/backend/src/services/config-apply-plan.ts +++ b/backend/src/services/config-apply-plan.ts @@ -20,7 +20,7 @@ export interface BgpInApplyPlan { } function isInBgpIn(rule: RosFilterRuleLike): boolean { - return (rule.chain ?? "").trim().toLowerCase() === "bgp-in" + return rule.chain === "bgp-in" } export function planBgpInApply( diff --git a/backend/src/services/config-mcp-service-execution.test.ts b/backend/src/services/config-mcp-service-execution.test.ts new file mode 100644 index 00000000..d8ab298d --- /dev/null +++ b/backend/src/services/config-mcp-service-execution.test.ts @@ -0,0 +1,143 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { pool, closePool } from "../db/index.js" +import { withVerifiedConfigurationChange, assertVerifiedConfigurationAccess, captureVerifiedMcpServiceExecution as capture, inspectVerifiedMcpServiceExecution as inspect, type VerifiedMcpServiceMetadata, type VerifiedMcpServiceExecution } from "./config-mutation-policy.js" + +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function fixture(milliseconds = 5000) { + const connectionId = randomUUID(), expiry = Date.now() + milliseconds + const metadata: VerifiedMcpServiceMetadata = { operationId: randomUUID(), planId: randomUUID(), operationName: "diagnostics.bandwidth", serviceName: "routing.measure", phase: "forward", stepIndex: 2, + operationVersion: 3, policyVersion: 4, generation: "original-generation", sealedEvidenceProof:"e".repeat(64),sealedPayloadProof:"f".repeat(64),originalTemporaryGrantProof:"a".repeat(64),originalTemporaryGrantExpiresAt:null,principal: { connectionId, credentialId: randomUUID(), credentialExpiresAt: new Date(expiry + 1000).toISOString() }, + originalExpiresAt: expiry, serviceInput: { srcServerId: 101, dstServerId: 102, command: { durationSec: 2147483647, body: { address: "192.0.2.2", password: "SYNTHETIC-PRIVATE-STEP" } }, list: [1, { flag: true }] } } + let calls = 0, allowed = true, hook: (() => Promise) | undefined + const input = { actorId: "owned-test-actor", connectionId, approved: false, serverIds: [101, 102], objects: [{ kind: "server", id: "101", serverId: 101 }], serverWide: true, + serviceExecution: metadata, authorize: async () => { calls++; if (hook) await hook(); if (!allowed) throw Error("Original authority revoked") } } + return { metadata, input, calls: () => calls, revoke: () => { allowed = false }, onAuthorize: (value: () => Promise) => { hook = value } } +} + +test("genuine MCP service execution capability lifecycle; no DB or network", async t => { + const originalQuery = pool.query, originalConnect = pool.connect + let sqlCalls = 0 + pool.query = (() => { sqlCalls++; throw Error("Service token unit must not issue SQL") }) as never + pool.connect = (() => { sqlCalls++; throw Error("Service token unit must not acquire DB") }) as never + try { + await t.test("capture and inspect expose exact deeply frozen private metadata, not token JSON or extra authority", async () => { + const f = fixture(), expected = structuredClone(f.metadata) + await withVerifiedConfigurationChange(f.input, async () => { + const token = await capture(); assert.equal(f.calls(), 1); assert(Object.isFrozen(token)); assert.equal(Object.getPrototypeOf(token), null) + assert.equal(JSON.stringify(token), "{}"); assert.equal(Reflect.ownKeys(token).length, 0); assert(!JSON.stringify(token).includes("SYNTHETIC-PRIVATE-STEP")) + const value = await inspect(token); assert.equal(f.calls(), 2) + assert.deepEqual(value, { ...expected, ownerId: f.input.actorId, connectionId: f.input.connectionId, approved: false, serverWide: true, serverIds: [101, 102], objects: f.input.objects }) + assert(Object.isFrozen(value)); assert(Object.isFrozen(value.principal)); assert(Object.isFrozen(value.serviceInput)); assert(Object.isFrozen(value.serviceInput.command)); assert(Object.isFrozen(value.serverIds)); assert(Object.isFrozen(value.objects[0])) + assert.throws(() => { value.serviceInput.srcServerId = 999 }) + assert.equal(Object.hasOwn(value, "ready"), false); assert.equal(Object.hasOwn(value, "enrollmentAllowed"), false) + }) + }) + await t.test("legacy contexts retain callbacks and returned effects but cannot capture an origin token", async () => { + const f = fixture(), { serviceExecution: _metadata, ...legacy } = f.input + let effects = 0 + assert.equal(await withVerifiedConfigurationChange(legacy, async () => { effects++; await assertVerifiedConfigurationAccess(); await assert.rejects(capture()); return "old-result" }), "old-result") + assert.equal(effects, 1); assert.equal(f.calls(), 1); await assert.rejects(capture()) + await assert.rejects(withVerifiedConfigurationChange(legacy, async () => { throw Error("original-effect-error") }), /original-effect-error/) + }) + await t.test("copied, serialized and forged capabilities refuse without running original authorization", async () => { + const f = fixture() + await withVerifiedConfigurationChange(f.input, async () => { + const token = await capture(), before = f.calls() + for (const fake of [undefined, null, {}, { ...token }, JSON.parse(JSON.stringify(token)), { operationId: f.metadata.operationId }, Object.create(token)]) await assert.rejects(inspect(fake)) + assert.equal(f.calls(), before); await inspect(token) + }) + }) + await t.test("concurrent equal-field contexts cannot transfer genuine tokens", async () => { + const f = fixture(), aReady = deferred(), bReady = deferred() + let a!: VerifiedMcpServiceExecution, b!: VerifiedMcpServiceExecution + await Promise.all([ + withVerifiedConfigurationChange(f.input, async () => { a = await capture(); aReady.resolve(); await bReady.promise; await assert.rejects(inspect(b)); await inspect(a) }), + withVerifiedConfigurationChange(f.input, async () => { b = await capture(); bReady.resolve(); await aReady.promise; await assert.rejects(inspect(a)); await inspect(b) }), + ]) + await assert.rejects(inspect(a)); await assert.rejects(inspect(b)) + }) + await t.test("scope exit invalidates detached continuations and later equal/approved contexts", async () => { + const f = fixture(), later = deferred(); let token!: VerifiedMcpServiceExecution, detached!: Promise + await withVerifiedConfigurationChange(f.input, async () => { + token = await capture() + detached = (async () => { await later.promise; await assert.rejects(inspect(token)); await assert.rejects(capture()) })() + }) + await assert.rejects(inspect(token)); later.resolve(); await detached + for (const approved of [false, true]) await withVerifiedConfigurationChange({ ...f.input, approved }, async () => { await assert.rejects(inspect(token)); await inspect(await capture()) }) + }) + await t.test("fresh authorization rejection or revoke during async authorization cannot mint/inspect", async () => { + for (const phase of ["capture", "inspect"] as const) { + const f = fixture(), entered = deferred(), gate = deferred() + await withVerifiedConfigurationChange(f.input, async () => { + const token = phase === "inspect" ? await capture() : undefined + f.onAuthorize(async () => { entered.resolve(); await gate.promise }) + const pending = phase === "capture" ? capture() : inspect(token), rejected = assert.rejects(pending) + await entered.promise; f.revoke(); gate.resolve(); await rejected + }) + } + const f = fixture(); f.revoke(); await withVerifiedConfigurationChange(f.input, async () => { await assert.rejects(capture(), /revoked/) }) + }) + await t.test("stalled authorization completing after owning scope ends cannot mint or inspect a token", async () => { + for (const phase of ["capture", "inspect"] as const) { + const f = fixture(), entered = deferred(), gate = deferred(); let rejected!: Promise + await withVerifiedConfigurationChange(f.input, async () => { + const token = phase === "inspect" ? await capture() : undefined + f.onAuthorize(async () => { entered.resolve(); await gate.promise }) + rejected = assert.rejects(phase === "capture" ? capture() : inspect(token)); await entered.promise + }) + gate.resolve(); await rejected + } + }) + await t.test("caller mutation of principal/service input/metadata cannot rewrite the captured original", async () => { + const f = fixture(), expected = structuredClone(f.metadata) + await withVerifiedConfigurationChange(f.input, async () => { + f.metadata.principal.credentialId = randomUUID(); f.metadata.principal.credentialExpiresAt = new Date(Date.now() + 600000).toISOString() + f.metadata.operationId = randomUUID(); f.metadata.originalExpiresAt += 600000 + const command = f.metadata.serviceInput.command as { body: { password: string } }; command.body.password = "MUTATED" + f.metadata.serviceInput.list = []; f.input.serviceExecution = fixture().metadata; f.input.approved = true + const value = await inspect(await capture()) + assert.equal(value.operationId, expected.operationId); assert.deepEqual(value.principal, expected.principal); assert.deepEqual(value.serviceInput, expected.serviceInput) + assert.equal(value.originalExpiresAt, expected.originalExpiresAt); assert.equal(value.approved, false) + }) + }) + await t.test("mutating referenced server/protection closure denies rather than broadening scope", async () => { + for (const mutate of [(f: ReturnType) => f.input.serverIds.push(999), (f: ReturnType) => { f.input.objects[0].id = "999" }, (f: ReturnType) => { f.input.objects[0].serverId = 999 }]) { + const f = fixture() + await withVerifiedConfigurationChange(f.input, async () => { const token = await capture(); mutate(f); await assert.rejects(inspect(token)); await assert.rejects(capture()) }) + } + const f = fixture() + await withVerifiedConfigurationChange(f.input, async () => { + const original = await capture() + await withVerifiedConfigurationChange({ ...f.input, approved: true }, async () => { await assert.rejects(inspect(original)); assert.equal((await inspect(await capture())).approved, true) }) + assert.equal((await inspect(original)).approved, false) + }) + }) + await t.test("original wall expiry cannot be renewed by later input changes", async () => { + const f = fixture(35) + await withVerifiedConfigurationChange(f.input, async () => { + const token = await capture(); f.metadata.originalExpiresAt = Date.now() + 600000 + await delay(55); await assert.rejects(inspect(token)); await assert.rejects(capture()) + }) + }) + await t.test("monotonic original expiry survives a backwards wall clock jump", async () => { + const f = fixture(35), originalNow = Date.now, baseline = Date.now() + await withVerifiedConfigurationChange(f.input, async () => { + const token = await capture() + try { Date.now = () => baseline - 60000; await delay(55); await assert.rejects(inspect(token)); await assert.rejects(capture()) } + finally { Date.now = originalNow } + }) + }) + await t.test("malformed runtime metadata and missing authorizer cannot issue capabilities", async () => { + const mutations: Array<(m: VerifiedMcpServiceMetadata) => void> = [m => { m.operationId = "model-id" }, m => { m.principal.connectionId = randomUUID() }, m => { m.originalExpiresAt = Date.parse(m.principal.credentialExpiresAt) + 1 }, + m => { m.originalTemporaryGrantProof = "" }, m => { m.originalTemporaryGrantExpiresAt = m.originalExpiresAt - 1 }, + m => { m.stepIndex = -1 }, m => { m.operationVersion = 0 }, m => { m.policyVersion = 0 }, m => { m.generation = "bad\nvalue" }, m => { m.phase = "recovery" as "forward" }, m => { m.principal.credentialExpiresAt = "invalid" }] + for (const mutate of mutations) { const f = fixture(); mutate(f.metadata); await withVerifiedConfigurationChange(f.input, async () => { await assert.rejects(capture()) }) } + const f = fixture(); await withVerifiedConfigurationChange({ ...f.input, authorize: undefined }, async () => { await assert.rejects(capture()) }) + }) + assert.equal(sqlCalls, 0) + console.log("MCP service origin token: 12 groups; same-active-ALS, exact immutable original, fresh authorization and original monotonic expiry; 0 SQL/0 native calls; no bandwidth/enrollment authority") + } finally { pool.query = originalQuery; pool.connect = originalConnect; await closePool() } +}) diff --git a/backend/src/services/config-mutation-lock.ts b/backend/src/services/config-mutation-lock.ts new file mode 100644 index 00000000..48e61675 --- /dev/null +++ b/backend/src/services/config-mutation-lock.ts @@ -0,0 +1,174 @@ +import { AsyncLocalStorage } from "node:async_hooks" +import type { PoolClient } from "pg" +import { assertRuntimeDatabaseActivationCurrent, pool } from "../db/index.js" +import { SYSTEM_DATABASE_REQUEST_LOCK } from "../db/operation-lock.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" +import { McpError } from "../modules/mcp/security.js" +import { withActiveDatabaseRequest } from "../modules/mcp/database-request-lease.js" +import { assertOperationConfigurationLockScope, borrowOperationConfigurationLockClient, captureOperationConfigurationLockHandoff, operationConfigurationSignal, operationDatabaseRequestSignal } from "./operation-config-lock-lease.js" + +type LockCompletion = { active: boolean; readonly completed: Promise } +type ServerLockCompletion = LockCompletion & { ids: ReadonlySet; readonly parent: ServerLockCompletion | undefined; children: Set>; signal: AbortSignal } +const held = new AsyncLocalStorage() +const applicationLease = new AsyncLocalStorage() +const collectorLease = new AsyncLocalStorage() +const applicationLockKey = "hashtextextended('mm-config:application',0)" + +export async function acquireApplicationMutationTransactionLock(client: Pick): Promise { + assertOperationConfigurationLockScope() + const { rows: [row] } = await client.query<{ acquired: boolean }>(`SELECT pg_try_advisory_xact_lock(${applicationLockKey}) AS acquired`) + if (row?.acquired !== true) throw Object.assign(new Error("Глобальная конфигурация изменяется другой операцией"), { statusCode: 409 }) +} + +export function configurationMutationSignal(): AbortSignal | undefined { + return held.getStore()?.signal +} +/** Read-only scheduler handoff; never exposes a client or admits arbitrary work. */ +export function captureConfigurationLockHandoff(): Promise | undefined { + const completions: Promise[] = [] + const application = applicationLease.getStore() + if (application) { + if (!application.active) throw new Error("Configuration lock scope has completed") + completions.push(application.completed) + } + const collector = collectorLease.getStore() + if (collector) { + if (!collector.active) throw new Error("Collector lock scope has completed") + completions.push(collector.completed) + } + for (let server = held.getStore(); server; server = server.parent) { + if (!server.active) throw new Error("Configuration lock scope has completed") + completions.push(server.completed) + } + return completions.length ? Promise.all(completions).then(() => {}) : undefined +} +export function assertNoConfigurationLockContext(): void { + if (held.getStore() || applicationLease.getStore() || collectorLease.getStore()) throw new Error("Scheduler bootstrap inside configuration lock context") +} +export async function withApplicationMutationLock(work: () => Promise): Promise { + assertOperationConfigurationLockScope() + if (applicationLease.getStore()?.active) return work() + if (collectorLease.getStore()?.active) throw Object.assign(new Error("Изменение конфигурации внутри общего сбора запрещено"), { statusCode: 409 }) + let complete!: () => void + const completed = new Promise(resolve => { complete = resolve }) + const client = borrowOperationConfigurationLockClient() ?? await pool.connect(), context = { active: true, completed } + let acquired = false, unsafe = false + try { + acquired = (await client.query(`SELECT pg_try_advisory_lock(${applicationLockKey}) AS acquired`)).rows[0].acquired + if (!acquired) throw Object.assign(new Error("Глобальная конфигурация изменяется другой операцией"), { statusCode: 409 }) + return await applicationLease.run(context, work) + } finally { + context.active = false + if (acquired) try { await client.query(`SELECT pg_advisory_unlock(${applicationLockKey})`) } catch { unsafe = true } + try { client.release(unsafe) } finally { complete() } + } +} +/** Collectors coexist with each other but exclude configuration mutation for + * the complete native/sample/status operation. This lock grants no authority. + * Verified operations retain their original exact exclusive client protocol. */ +export async function withApplicationCollectorLock(work: () => Promise): Promise { + assertOperationConfigurationLockScope() + if (applicationLease.getStore()?.active || captureOperationConfigurationLockHandoff()) return withApplicationMutationLock(work) + if (collectorLease.getStore()?.active) return work() + let complete!: () => void + const completed = new Promise(resolve => { complete = resolve }) + const client = await pool.connect(), context = { active: true, completed } + let acquired = false, unsafe = false + try { + acquired = (await client.query(`SELECT pg_try_advisory_lock_shared(${applicationLockKey}) AS acquired`)).rows[0].acquired + if (!acquired) throw Object.assign(new Error("Глобальная конфигурация изменяется другой операцией"), { statusCode: 409 }) + return await collectorLease.run(context, work) + } finally { + context.active = false + if (acquired) try { await client.query(`SELECT pg_advisory_unlock_shared(${applicationLockKey})`) } catch { unsafe = true } + try { client.release(unsafe) } finally { complete() } + } +} +/** Shared by UI, MCP and background tasks; sorted acquisition avoids cross-router deadlocks. */ +export function withConfigMutationLock(serverIds: number[], work: () => Promise): Promise { + const scope = held.getStore() + const parent = scope?.active ? scope : undefined + if (!parent) { + const admitted = withActiveDatabaseRequest(signal => runConfigMutationLock(serverIds, work, undefined, signal)) + if (admitted) return admitted + } + const child = runConfigMutationLock(serverIds, work, parent) + if (parent) { + parent.children.add(child) + const settled = () => { parent.children.delete(child) } + void child.then(settled, settled) + } + return child +} + +async function runConfigMutationLock(serverIds: number[], work: () => Promise, parent: ServerLockCompletion | undefined, requestSignal?: AbortSignal): Promise { + assertOperationConfigurationLockScope() + const inherited = parent?.ids ?? new Set() + const requested = [...new Set(serverIds)].sort((a, b) => a - b).filter(id => !inherited.has(id)) + parent?.signal.throwIfAborted() + if (!requested.length) return work() + // Never extend an existing multi-router lease out of order. + if (inherited.size && requested[0] < Math.max(...inherited)) throw new Error("Некорректный порядок блокировок серверов") + const borrowed = borrowOperationConfigurationLockClient() + const owned = borrowed ? undefined : await pool.connect() + const client = borrowed ?? owned! + const acquired: number[] = [] + let unsafe = false, databaseRequest = false + let complete!: () => void + const completed = new Promise(resolve => { complete = resolve }) + const lifetime = new AbortController() + const lostLease = new McpError("PROTOCOL_STORAGE_BUSY", "Соединение с блокировкой конфигурации потеряно", 503) + const inheritedSignal = parent?.signal ?? requestSignal ?? operationDatabaseRequestSignal() + const signal = inheritedSignal ? AbortSignal.any([inheritedSignal, lifetime.signal]) : lifetime.signal + const context: ServerLockCompletion = { ids: new Set([...inherited, ...requested]), active: true, completed, parent, children: new Set(), signal } + const disconnected = () => { + unsafe = true + lifetime.abort(lostLease) + } + const operationSignal = borrowed ? operationConfigurationSignal() : undefined + operationSignal?.addEventListener("abort", disconnected, { once: true }) + owned?.on?.("error", disconnected) + owned?.on?.("end", disconnected) + try { + if (operationSignal?.aborted) disconnected() + signal.throwIfAborted() + // An ordinary MCP runner already holds its database request lease. + if (!borrowed && !parent && !requestSignal) { + unsafe = true + const { rows: [row] } = await client.query<{ acquired: boolean }>("SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired", [SYSTEM_DATABASE_REQUEST_LOCK]) + signal.throwIfAborted() + if (typeof row?.acquired !== "boolean") throw new McpError("AUDIT_UNAVAILABLE", "Допуск записи RouterOS не подтверждён", 503) + unsafe = false + if (!row.acquired) throw new McpError("PROTOCOL_STORAGE_BUSY", "Выполняется операция с базой приложения", 503) + databaseRequest = true + if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client) + } + for (const id of requested) { + const { rows: [row] } = await client.query<{ acquired: boolean }>("SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired", ["mm-config:" + id]) + if (!row.acquired) throw Object.assign(new Error("Конфигурация сервера изменяется другой операцией"), { statusCode: 409 }) + acquired.push(id) + signal.throwIfAborted() + } + assertRuntimeDatabaseActivationCurrent() + return await held.run(context, work) + } finally { + // Started nested work owns its parent's lease until it settles. + while (context.children.size) await Promise.allSettled(context.children) + context.active = false + for (const id of acquired.reverse()) { + if (lifetime.signal.aborted) break + try { await client.query("SELECT pg_advisory_unlock(hashtextextended($1,0))", ["mm-config:" + id]) } catch { unsafe = true } + } + if (databaseRequest && !unsafe) { + try { + const { rows: [row] } = await client.query<{ released: boolean }>("SELECT pg_catalog.pg_advisory_unlock_shared($1::bigint) AS released", [SYSTEM_DATABASE_REQUEST_LOCK]) + unsafe = row?.released !== true + } catch { unsafe = true } + } + owned?.off?.("error", disconnected) + owned?.off?.("end", disconnected) + operationSignal?.removeEventListener("abort", disconnected) + lifetime.abort(lostLease) + try { client.release(unsafe) } finally { complete() } + } +} diff --git a/backend/src/services/config-mutation-policy.ts b/backend/src/services/config-mutation-policy.ts new file mode 100644 index 00000000..274123cf --- /dev/null +++ b/backend/src/services/config-mutation-policy.ts @@ -0,0 +1,228 @@ +import { assertActiveRoutingProfileWriter } from "../modules/mcp/routing-controller-active-writer.js" +import { AsyncLocalStorage } from "node:async_hooks" +import { randomUUID } from "node:crypto" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import type { McpPrincipal } from "../modules/mcp/store.js" +import type { McpObjectRef } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import { audit, requestAccountingWrite } from "../modules/mcp/store.js" +import { McpError } from "../modules/mcp/security.js" +import { configurationMutationSignal, withApplicationMutationLock } from "./config-mutation-lock.js" + +const actor = new AsyncLocalStorage() +export interface VerifiedMcpServiceMetadata { + operationId:string;planId:string;operationName:string;serviceName:string;stepIndex:number;phase:"forward" + operationVersion:number;policyVersion:number;generation:string;principal:McpPrincipal;originalExpiresAt:number + /** Digests of authenticated RANDOMIZED CIPHERTEXT, never plaintext secrets. */ + sealedEvidenceProof:string;sealedPayloadProof:string + originalTemporaryGrantProof:string;originalTemporaryGrantExpiresAt:number|null + /** Snapshot of the exact server-decrypted sealed step, never public plan JSON. */ + serviceInput:Record +} +interface VerifiedChange { active: boolean; actorId: string; connectionId: string; approved: boolean; serverIds: number[]; objects: McpObjectRef[]; serverWide: boolean; authorize?: () => Promise; beforeDispatch?: () => Promise; serviceExecution?:VerifiedMcpServiceMetadata } +const verified = new AsyncLocalStorage() +declare const serviceExecutionBrand:unique symbol +export type VerifiedMcpServiceExecution=Readonly<{[serviceExecutionBrand]:true}> +type ServiceExecutionScope={context:VerifiedChange;metadata:Readonly;scope:{actorId:string;connectionId:string;approved:boolean;serverIds:number[];objects:McpObjectRef[];serverWide:boolean};monotonicEnd:number;validatedInput:boolean;controller:AbortController;expiryTimer?:ReturnType} +const serviceScopes=new WeakMap(),serviceTokens=new WeakMap() +const deniedService=():never=>{throw new McpError("PERMISSION_DENIED","Исходный контекст MCP service step не подтверждён",403)} +function freezeService(value:T):T{ + const queue:unknown[]=[value],seen=new Set() + while(queue.length){const item=queue.pop();if(!item||typeof item!=="object"||seen.has(item))continue;seen.add(item);for(const value of Object.values(item))queue.push(value);Object.freeze(item)} + return value +} +const serviceScope=(context:VerifiedChange)=>({actorId:context.actorId,connectionId:context.connectionId,approved:context.approved,serverIds:context.serverIds,objects:context.objects,serverWide:context.serverWide}) +function checkService(entry:ServiceExecutionScope){ + const context=verified.getStore(),m=entry.metadata + if(!context)return deniedService() + if(context!==entry.context||!context.active||serviceScopes.get(context)!==entry||typeof context.authorize!=="function" + ||entry.controller.signal.aborted||!isDeepStrictEqual(serviceScope(context),entry.scope)||Date.now()>=m.originalExpiresAt||performance.now()>=entry.monotonicEnd)deniedService() + if(!m.principal||typeof m.principal!=="object"||m.phase!=="forward"||m.principal.connectionId!==context.connectionId||!Number.isSafeInteger(m.originalExpiresAt) + ||!Number.isSafeInteger(m.stepIndex)||m.stepIndex<0||!Number.isSafeInteger(m.operationVersion)||m.operationVersion<1 + ||!Number.isSafeInteger(m.policyVersion)||m.policyVersion<1)return deniedService() + const uuid=/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i + if(typeof context.actorId!=="string"||!context.actorId.length||typeof context.approved!=="boolean"||typeof context.serverWide!=="boolean" + ||!Array.isArray(context.serverIds)||context.serverIds.some(id=>!Number.isSafeInteger(id)||id<=0)||!Array.isArray(context.objects))return deniedService() + if(![m.operationId,m.planId,m.principal.connectionId,m.principal.credentialId].every(id=>typeof id==="string"&&uuid.test(id)) + ||![m.sealedEvidenceProof,m.sealedPayloadProof,m.originalTemporaryGrantProof].every(proof=>typeof proof==="string"&&/^[a-f0-9]{64}$/.test(proof)) + ||m.originalTemporaryGrantExpiresAt!==null&&(!Number.isSafeInteger(m.originalTemporaryGrantExpiresAt)||m.originalTemporaryGrantExpiresAttypeof value==="string"&&value.length>0&&value.length<=200&&!/[\u0000-\u001f\u007f]/.test(value)) + ||!m.serviceInput||Object.getPrototypeOf(m.serviceInput)!==Object.prototype||!Number.isFinite(Date.parse(m.principal.credentialExpiresAt)) + ||m.originalExpiresAt>Date.parse(m.principal.credentialExpiresAt))return deniedService() + if(!entry.validatedInput){ + const pending:unknown[]=[m.serviceInput],seen=new Set() + while(pending.length){ + const value=pending.pop() + if(value===null||typeof value==="string"||typeof value==="boolean")continue + if(typeof value==="number"&&Number.isFinite(value))continue + if(!value||typeof value!=="object"||seen.has(value)||!Array.isArray(value)&&Object.getPrototypeOf(value)!==Object.prototype)return deniedService() + seen.add(value);for(const child of Object.values(value))pending.push(child) + } + entry.validatedInput=true + } +} +/** Private origin capability, not bandwidth/enrollment permission. Fresh runtime + * authorization is checked before and after every capture/inspection. */ +export async function captureVerifiedMcpServiceExecution():Promise{ + const context=verified.getStore(),entry=context&&serviceScopes.get(context) + if(!entry)return deniedService();checkService(entry) + await entry.context.authorize!();checkService(entry) + const token=Object.freeze(Object.create(null)) as VerifiedMcpServiceExecution + serviceTokens.set(token,entry);return token +} +export async function inspectVerifiedMcpServiceExecution(token:unknown){ + const entry=token&&typeof token==="object"?serviceTokens.get(token):undefined + if(!entry)return deniedService();checkService(entry) + await entry.context.authorize!();checkService(entry) + return freezeService({...entry.metadata,ownerId:entry.scope.actorId,connectionId:entry.scope.connectionId, + approved:entry.scope.approved,serverWide:entry.scope.serverWide,serverIds:[...entry.scope.serverIds],objects:structuredClone(entry.scope.objects)}) +} +/** Separate transport lifetime, never inserted into service metadata/SQL proof. + * The fixed original horizon/signal cannot be renewed by later inspection. */ +export async function inspectVerifiedMcpServiceExecutionLifetime(token:unknown):Promise>{ + const entry=token&&typeof token==="object"?serviceTokens.get(token):undefined + if(!entry)return deniedService();checkService(entry) + await entry.context.authorize!();checkService(entry) + return Object.freeze({expiresAt:entry.metadata.originalExpiresAt,monotonicDeadline:entry.monotonicEnd,signal:entry.controller.signal}) +} +/** Scheduler handoff may leave an active original scope, never a retired one. */ +export function assertConfigurationHandoffContext(): void { + const context = verified.getStore() + if (context && !context.active) throw new Error("Verified configuration scope has completed") +} +export function assertNoVerifiedConfigurationContext(): void { + if (verified.getStore() || actor.getStore() !== undefined) throw new Error("Scheduler bootstrap inside actor or verified context") +} +export function withConfigurationActor(actorId: string, work: () => Promise) { return actor.run(actorId, work) } +export async function withVerifiedConfigurationChange(input: Omit, work: () => Promise) { + const context = { ...input, active: true } + let service:ServiceExecutionScope|undefined + if(input.serviceExecution){ + const now=Date.now(),metadata=freezeService(structuredClone(input.serviceExecution)),scope=freezeService(structuredClone(serviceScope(context))) + const controller=new AbortController() + service={context,metadata,scope,monotonicEnd:performance.now()+Math.max(0,metadata.originalExpiresAt-now),validatedInput:false,controller} + const entry=service + const expire=()=>{ + const remaining=Math.min(metadata.originalExpiresAt-Date.now(),entry.monotonicEnd-performance.now()) + if(!Number.isFinite(remaining)||remaining<=0){controller.abort();return} + entry.expiryTimer=setTimeout(expire,Math.max(1,Math.min(2147483647,remaining)));entry.expiryTimer.unref() + } + expire() + serviceScopes.set(context,service) + } + try { return await verified.run(context, work) } finally { context.active = false;if(service){clearTimeout(service.expiryTimer);service.controller.abort()}serviceScopes.delete(context) } +} +/** Long diagnostics recheck their live grant before another request or before + * publishing telemetry; this does not create a configuration-write intent. */ +export async function assertVerifiedConfigurationAccess(): Promise { + const context = verified.getStore() + if (!context?.active || !context.authorize) throw new McpError("PERMISSION_DENIED", "Нет действующего контекста операции измерения", 403) + await context.authorize() +} + +/** A full database replacement affects every protected domain, including non-router objects. */ +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) { + 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) + } + } +} + +function auditConfiguration(actorId: string, action: string, connectionId: string | null, payload: unknown) { + return requestAccountingWrite(client => audit(client, actorId, action, connectionId, payload)) +} + +/** Common RouterOS boundary: legacy REST/scheduler cannot bypass protected objects. */ +export async function executeGuardedConfigurationWrite(serverId: number, method: string, path: string, work: (signal?: AbortSignal) => Promise): Promise { + const signal = configurationMutationSignal() + const outcomeUnknown = () => new McpError("MUTATION_OUTCOME_UNKNOWN", "Исход записи не подтверждён. Не повторяйте изменение; требуется сверка.", 503) + signal?.throwIfAborted() + const inherited = verified.getStore(), context = inherited?.active ? inherited : undefined + // A service step can contain many native writes. Revalidate its live access + // for each dispatch, rather than only once at the beginning of that step. + await context?.authorize?.() + const actorId = context?.actorId ?? actor.getStore() ?? "system" + const { rows } = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1)", [String(serverId)]) + for (const row of rows) { + const protection = row.object_ref as McpObjectRef & { protection: string } + // Existing UI handlers do not provide a dependency closure. Require a reviewed + // 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) { + 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) + } + } + if (context && !context.serverIds.includes(serverId)) throw new McpError("SERVER_DENIED", "Шаг вышел за область плана") + await assertActiveRoutingProfileWriter(serverId) + const writeId = randomUUID(), connectionId = context?.connectionId ?? null + // Must commit before network I/O. Do not include raw RouterOS payloads/secrets. + await auditConfiguration(actorId, "configuration.intent", connectionId, { writeId, serverId, method, path }) + let result: T + let dispatched = false + try { + await context?.beforeDispatch?.() + signal?.throwIfAborted() + dispatched = true + result = await work(signal) + if (signal?.aborted) throw outcomeUnknown() + } + catch (error) { + try { await auditConfiguration(actorId, "configuration.unconfirmed", connectionId, { writeId, serverId }) } + catch { if (dispatched && !(signal?.aborted && error === signal.reason)) throw outcomeUnknown() } + if (dispatched && signal?.aborted && error !== signal.reason) throw outcomeUnknown() + throw error + } + try { await auditConfiguration(actorId, "configuration.acknowledged", connectionId, { writeId, serverId }) } + catch { throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Запись отправлена, но сохранение подтверждения недоступно. Не повторяйте изменение; требуется сверка.", 503) } + if (signal?.aborted) throw outcomeUnknown() + return result +} +/** Shared DB/host boundary. Caller supplies a fixed domain ID and real dependencies. */ +export async function executeGuardedApplicationWrite(object: { kind: string; id: string }, dependencies: "all" | number[] | (() => Promise), work: () => Promise, affectedObjects?: () => Promise<{ kind: string; id: string }[]>): Promise { + return withApplicationMutationLock(async () => { + const serverIds = dependencies === "all" ? (await pool.query("SELECT id FROM servers")).rows.map(row => Number(row.id)) : typeof dependencies === "function" ? await dependencies() : dependencies + if (serverIds.some(id => !Number.isSafeInteger(id) || id <= 0)) throw new McpError("INVALID_INPUT", "Некорректная зависимость сервера", 400) + const inherited = verified.getStore(), context = inherited?.active ? inherited : undefined, actorId = context?.actorId ?? actor.getStore() ?? "system" + await context?.authorize?.() + if (context && serverIds.some(id => !context.serverIds.includes(id))) throw new McpError("SERVER_DENIED", "Сервис затрагивает зависимость вне проверенного плана") + const objects = affectedObjects ? await affectedObjects() : [] + 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) { + await auditConfiguration(actorId, "configuration.denied", context?.connectionId ?? null, { domain: "application", object, serverIds }) + throw new McpError(protection.protection === "deny" ? "PROTECTED_OBJECT" : "APPROVAL_REQUIRED", "Изменение приложения затрагивает защищённый объект; нужен проверенный план", 409) + } + } + const writeId = randomUUID() + await auditConfiguration(actorId, "configuration.intent", context?.connectionId ?? null, { writeId, domain: "application", object, serverIds }) + let result: T + try { result = await work() } + catch (error) { + try { await auditConfiguration(actorId, "configuration.unconfirmed", context?.connectionId ?? null, { writeId, domain: "application" }) } + catch { throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Исход изменения не подтверждён, квитанция аудита недоступна. Требуется сверка.", 503) } + throw error + } + try { await auditConfiguration(actorId, "configuration.acknowledged", context?.connectionId ?? null, { writeId, domain: "application" }) } + catch { throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Изменение сохранено, квитанция аудита недоступна; не повторяйте операцию", 503) } + return result + }) +} + + +/** Local origin only; fixed runtime continuation supplies every original IO. */ +export function inspectCurrentVerifiedServiceOrigin() { + const context = verified.getStore(), entry = context && serviceScopes.get(context) + if (!entry) return deniedService() + checkService(entry) + return entry.metadata +} diff --git a/backend/src/services/config-revisions.ts b/backend/src/services/config-revisions.ts index 814af8d7..58b1636a 100644 --- a/backend/src/services/config-revisions.ts +++ b/backend/src/services/config-revisions.ts @@ -60,6 +60,7 @@ export function persistablePayload(payload: unknown): unknown { export function canonicalFilterRules( rules: Array<{ community?: string + communityName?: string action?: string gateway?: string gatewayTunnelId?: string @@ -68,6 +69,7 @@ export function canonicalFilterRules( ): unknown[] { return rules.map((r) => ({ community: (r.community ?? "").trim(), + communityName: r.communityName ?? "", action: r.action === "blackhole" ? "blackhole" : "route", gateway: r.gateway ?? "", gatewayTunnelId: r.gatewayTunnelId ?? "", @@ -130,8 +132,8 @@ export async function listRevisions( return rows.map(toRevisionDto) } -export async function getRevisionById(id: string): Promise { - return (await db.select().from(configRevisions).where(eq(configRevisions.id, id)).limit(1))[0] +export async function getRevisionById(id: string, scope?: { serverId: number; section: ConfigSection }): Promise { + return (await db.select().from(configRevisions).where(and(eq(configRevisions.id, id), ...(scope ? [eq(configRevisions.serverId, scope.serverId), eq(configRevisions.section, scope.section)] : []))).limit(1))[0] } export async function pruneRevisions( diff --git a/backend/src/services/containers-live.ts b/backend/src/services/containers-live.ts index 0898d911..e8c8dcc9 100644 --- a/backend/src/services/containers-live.ts +++ b/backend/src/services/containers-live.ts @@ -127,17 +127,21 @@ export function mapContainerRow( async function fetchContainersForServer(server: ServerRow): Promise { if (!server.enabled) throw new Error("Сервер выключен. Опрос контейнеров недоступен.") - const client = MikrotikClient.fromServer(server) - const { readContainerServerState } = await import("./containers-state.js") - const state = await readContainerServerState(server, path => client.get(path)) - if (state.availability === "error" || state.failures.length) throw new Error("Не удалось полностью получить контейнеры сервера " + server.name + ": " + state.failures.map(f => f.error).join("; ")) - return state.containers + return requireCompleteContainerCatalog(await listContainersState([server]), [server]) } export async function listContainers(): Promise { - const enabledServers = await db.select().from(servers).where(eq(servers.enabled, true)) - const results = await Promise.all(enabledServers.map(fetchContainersForServer)) - return results.flat() + const selected = await db.select().from(servers).where(eq(servers.enabled, true)) + return requireCompleteContainerCatalog(await listContainersState(selected), selected) +} + +/** Legacy REST is fail-closed on partial reads. MCP retains the same observed + * rows together with explicit per-device unsupported/error evidence. */ +function requireCompleteContainerCatalog(state: Awaited>, selected: ServerRow[]): ContainerLive[] { + for (const server of state.servers) { + if (server.availability === "error" || server.failures.length) throw new Error("Не удалось полностью получить контейнеры сервера " + selected.find(row => row.id === server.serverId)?.name + ": " + server.failures.map(f => f.error).join("; ")) + } + return state.servers.flatMap(server => server.containers) } export async function listContainersForServer(server: ServerRow): Promise { @@ -173,9 +177,9 @@ export async function getEnabledServerById(serverId: string | number) { return (await db.select().from(servers).where(eq(servers.id, id)).limit(1))[0] ?? null } -export async function listContainersState() { +export async function listContainersState(selectedServers?: ServerRow[]) { const { collectContainersState } = await import("./containers-state.js") - const enabledServers = await db.select().from(servers).where(eq(servers.enabled, true)) + const enabledServers = selectedServers ?? await db.select().from(servers).where(eq(servers.enabled, true)) return collectContainersState(enabledServers, server => { const client = MikrotikClient.fromServer(server) return path => client.get(path) diff --git a/backend/src/services/database-capture-inspection.ts b/backend/src/services/database-capture-inspection.ts new file mode 100644 index 00000000..3c3f5116 --- /dev/null +++ b/backend/src/services/database-capture-inspection.ts @@ -0,0 +1,33 @@ +import pg from "pg" +import { X509Certificate } from "node:crypto" +import { checkServerIdentity } from "node:tls" +import { readRuntimeDatabaseArchivePeer } from "../db/index.js" +import { captureRuntimeRestoreData, DatabaseRestoreDataRefused } from "../db/database-restore-data.js" +import type { DatabaseOperationLockClient } from "../db/operation-lock.js" +import { readDatabaseArchivePassword } from "./database-dump-connection.js" +import { withDatabaseRestoreTarget } from "./database-restore-target.js" + +export async function inspectProtectedDatabaseData(runtime: DatabaseOperationLockClient, discard: () => void, passwordFile: string) { + const peer = readRuntimeDatabaseArchivePeer(runtime), leaf = new X509Certificate(peer.leaf) + const password = await readDatabaseArchivePassword(passwordFile) + return withDatabaseRestoreTarget(runtime, discard, async targetCheckSql => { + const client = new pg.Client({ + host: peer.host, port: peer.port, database: peer.database, user: peer.capture.name, password, + connectionTimeoutMillis: 5_000, statement_timeout: 10_000, options: "-c default_transaction_read_only=on", + ssl: { ca: peer.leaf, servername: peer.host, rejectUnauthorized: true, checkServerIdentity(host, certificate) { + if (!certificate.raw.equals(leaf.raw)) return new DatabaseRestoreDataRefused() + return checkServerIdentity(host, certificate) + } }, + }) + try { + await client.connect() + const { rows } = await client.query(`SELECT oid::text AS role_oid FROM pg_catalog.pg_roles + WHERE rolname=$1::pg_catalog.name AND rolname=SESSION_USER AND rolname=CURRENT_USER`, [peer.capture.name]) + if (rows.length !== 1 || rows[0].role_oid !== peer.capture.signedOid) throw new DatabaseRestoreDataRefused() + const snapshot = await captureRuntimeRestoreData(client, () => {}, targetCheckSql) + readRuntimeDatabaseArchivePeer(runtime) + return snapshot + } catch { throw new DatabaseRestoreDataRefused() } + finally { await client.end() } + }) +} diff --git a/backend/src/services/database-control-archive-boundary.test.ts b/backend/src/services/database-control-archive-boundary.test.ts new file mode 100644 index 00000000..b36b2d86 --- /dev/null +++ b/backend/src/services/database-control-archive-boundary.test.ts @@ -0,0 +1,19 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { assertDatabaseControlArchiveBoundary, DatabaseControlArchiveBoundaryError, DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG } from "./database-control-archive-boundary.js" + +test("exact shared exclusion argv preserves public TOC", () => { + assert.equal(DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG, "--exclude-schema=mmapp_control") + assertDatabaseControlArchiveBoundary(Buffer.from("; Archive mmapp_control filename\n1; 2615 2200 SCHEMA - public owner\n2; 1259 1 TABLE public servers owner\n3; 1259 2 TABLE DATA public servers owner\n")) + assertDatabaseControlArchiveBoundary("1; 1259 1 TABLE public other_mmapp_control_archive owner\n") +}) +test("all reserved namespace/object descriptors refuse including quoted tokens and case", () => { + for (const description of ["SCHEMA - mmapp_control owner", "TABLE mmapp_control database_schema_state owner", "TABLE DATA mmapp_control database_schema_intents owner", "SEQUENCE mmapp_control sequence owner", "COMMENT - SCHEMA mmapp_control owner", "ACL - SCHEMA mmapp_control owner", 'TABLE "mmapp_control" state owner', "FUNCTION MMAPP_CONTROL f() owner", "FUTURE UNKNOWN mmapp_control object owner", "TABLE public mmapp_control owner"]) { + assert.throws(() => assertDatabaseControlArchiveBoundary("1; 1259 1 " + description + "\n"), DatabaseControlArchiveBoundaryError) + } +}) +test("unsupported TOC encoding, control bytes, malformed entries and bounds refuse", () => { + for (const value of [Buffer.from([0xff]), "1; 0 1 TABLE public a\0 owner", "not a TOC entry", "1; 0 1 TABLE public a\x1b owner", "x".repeat(16 * 1024 * 1024 + 1), "\n".repeat(200000)]) { + assert.throws(() => assertDatabaseControlArchiveBoundary(value), DatabaseControlArchiveBoundaryError) + } +}) diff --git a/backend/src/services/database-control-archive-boundary.ts b/backend/src/services/database-control-archive-boundary.ts new file mode 100644 index 00000000..165a60fb --- /dev/null +++ b/backend/src/services/database-control-archive-boundary.ts @@ -0,0 +1,27 @@ +/** Shared pg_dump argv boundary for REST and private MCP exports. No store, + * configured pool, application bootstrap, or DDL is imported here. */ +export const DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG = "--exclude-schema=mmapp_control" + +export class DatabaseControlArchiveBoundaryError extends Error { + readonly code = "DATABASE_CONTROL_ARCHIVE_REFUSED" +} +const refuse = (): never => { throw new DatabaseControlArchiveBoundaryError("Архив содержит служебное пространство управления базой или неподдерживаемый список объектов") } + +/** Inspect the actual pg_restore --list output BEFORE archive decoding or + * target replacement. This is a conservative TOC namespace barrier, not an + * SQL parser: functions, dependencies and archive SQL are not certified safe. + * A public object whose description contains this standalone reserved token + * also refuses. No TOC entry or archive SQL is removed or rewritten. */ +export function assertDatabaseControlArchiveBoundary(toc: Buffer | string): void { + if (!(typeof toc === "string" || Buffer.isBuffer(toc)) || Buffer.byteLength(toc) > 16 * 1024 * 1024) return refuse() + let text: string + try { text = typeof toc === "string" ? toc : new TextDecoder("utf-8", { fatal: true }).decode(toc) } + catch { return refuse() } + if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(text)) return refuse() + const lines = text.split(/\r?\n/) + if (lines.length > 200000) return refuse() + for (const line of lines) { + if (!line.trim() || /^\s*;/.test(line)) continue + if (!/^\d+;\s+\d+\s+\d+\s+\S/.test(line) || /(?:^|[^A-Za-z0-9_$])mmapp_control(?=$|[^A-Za-z0-9_$])/i.test(line)) return refuse() + } +} diff --git a/backend/src/services/database-dump-connection.test.ts b/backend/src/services/database-dump-connection.test.ts new file mode 100644 index 00000000..56a28364 --- /dev/null +++ b/backend/src/services/database-dump-connection.test.ts @@ -0,0 +1,166 @@ +import assert from "node:assert/strict" +import { access, chmod, mkdtemp, readFile, realpath, readdir, rm, writeFile } from "node:fs/promises" +import { test } from "node:test" +import os from "node:os" +import path from "node:path" +import { databaseDumpServiceFile, readRestoreOwnerConnection, runPinnedDatabaseSnapshotDump, runPinnedLegacyProbeSnapshotDump, runPrivateDatabaseDump, validatePrivateRestoreConnection, withPrivateRestoreService } from "./database-dump-connection.js" +import { DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG } from "./database-control-archive-boundary.js" + +const uri = "postgresql://uri_user:p%40ss%3Aword@127.0.0.1:12345/uri%2Fdatabase?user=query_user&dbname=query_database&sslmode=verify-full&sslrootcert=C%3A%2Fprivate%2Froot.pem&options=-c%20statement_timeout%3D1000&application_name=backup%20client" +const serialized = databaseDumpServiceFile(uri) +for (const expected of ["user=query_user\n", "password=p@ss:word\n", "dbname=query_database\n", "sslmode=verify-full\n", "sslrootcert=C:/private/root.pem\n", "options=-c statement_timeout=1000\n", "application_name=backup client\n"]) assert.ok(serialized.includes(expected), expected) +assert.ok(databaseDumpServiceFile("postgres://u:p@[::1]:5432/db").includes("host=::1\n")) +assert.ok(databaseDumpServiceFile("postgres://host/ordinary%20database").includes("dbname=ordinary database\n")) +assert.ok(databaseDumpServiceFile("postgres://u:p%40ss@host/db").includes("password=p@ss\n")) +const literalPlus = databaseDumpServiceFile("postgres://host/db?password=a+b%2Bc&options=-c%20custom.setting%3Da+b%2Bc") +assert.ok(literalPlus.includes("password=a+b+c\n")) +assert.ok(literalPlus.includes("options=-c custom.setting=a+b+c\n")) +assert.ok(databaseDumpServiceFile("postgres://host/db?future_libpq_key=value").includes("future_libpq_key=value\n"), "Unknown parameters reach libpq instead of being discarded") +for (const invalid of [ + "host=localhost dbname=db", "postgres://host1,host2/db", "postgres:///db", "postgres://%2Ftmp/db", "postgres://host/db#fragment", + "postgres://host/db?host=%2Ftmp", "postgres://host/db?host=host1,host2", "postgres://host/db?port=1,2", + "postgres://host/db?sslmode=require&sslmode=disable", "postgres://host/db?service=other", "postgres://host/db?ssl=true", "postgres://host/db?sslmode=", + "postgres://u:p%0A%5Bevil%5D@host/db", "postgres://host/db?options=%0D%0Apassword%3Dstolen", "postgres://host/db?options=%00", + "postgres://u:%20password@host/db", "postgres://host/db?application_name=trailing%20", "postgres://u:%ZZ@host/db", + "postgres://u:raw\ncontrol@host/db", "postgres://host/db?options=%ZZ", "postgres://host/db?options=%FF", "postgres://host/db#", + "postgres://host/a/../db", "postgres://host/a/./db", "postgres://host/a/%2e%2e/db", "postgres://host/a/%2E/db", "postgres://host/a/.%2e/db", + "postgres://host/.", "postgres://host/%2e%2e", "postgres://user:raw@password@host/db", "postgres://user@other@host/db", +]) assert.throws(() => databaseDumpServiceFile(invalid), error => error instanceof Error && !error.message.includes("stolen") && !error.message.includes("password")) + +const directory = await mkdtemp(path.join(os.tmpdir(), "mm-private-dump-test-")), oldDirectory = process.env.MCP_STATE_DIR +process.env.MCP_STATE_DIR = directory +let privateFile = "", calls = 0 +try { + const data = Buffer.from("PGDMP\0fixture") + const authorize = async () => {}, controller = new AbortController() + const inspect = async (command: string, args: string[], options: Parameters[1] & { env?: NodeJS.ProcessEnv }) => { + calls++; assert.equal(command, "pg_dump"); assert.deepEqual(args, ["-Fc", DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG]) + assert.ok(!JSON.stringify(args).includes("p@ss")); assert.ok(!JSON.stringify(args).includes(uri)) + assert.equal(options.authorize, authorize); assert.equal(options.signal, controller.signal) + assert.equal(options.env?.PGSERVICE, "mmapp_database_export") + privateFile = String(options.env?.PGSERVICEFILE) + assert.ok(privateFile.startsWith(directory + path.sep)) + assert.equal(await readFile(privateFile, "utf8"), serialized) + return data + } + assert.deepEqual(await runPrivateDatabaseDump(uri, { authorize, signal: controller.signal }, inspect), data) + await assert.rejects(access(privateFile)); assert.deepEqual(await readdir(directory), []) + await assert.rejects(runPrivateDatabaseDump(uri, { authorize }, async () => { throw Error("safe child failure") })) + assert.deepEqual(await readdir(directory), []) + await assert.rejects(runPrivateDatabaseDump("postgres://host/db?options=%0Ainjection", {}, inspect)) + assert.equal(calls, 1, "Unsupported connection syntax never dispatches a child") + assert.deepEqual(await readdir(directory), []) +} finally { + if (oldDirectory === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = oldDirectory + await rm(directory, { recursive: true, force: true }) +} +console.log("private database dump service-file tests passed: encoded credentials, query precedence/options, explicit refusal, secret-free argv, private lifetime and cleanup") + +test("restore service keeps credentials in the temporary file and strips inherited connection settings", async () => { + const directory = await mkdtemp(path.join(os.tmpdir(), "mm-private-restore-test-")) + const keys = ["MCP_STATE_DIR", "DATABASE_URL", "PGPASSWORD", "PGOPTIONS", "PGSERVICEFILE"] as const + const previous = Object.fromEntries(keys.map(key => [key, process.env[key]])) + Object.assign(process.env, { MCP_STATE_DIR: directory, DATABASE_URL: "inherited-fixture-uri", PGPASSWORD: "inherited-fixture-password", + PGOPTIONS: "-c role=unrelated", PGSERVICEFILE: "inherited-fixture-file" }) + try { + assert.throws(() => validatePrivateRestoreConnection("postgres://127.0.0.1/db")) + assert.throws(() => validatePrivateRestoreConnection("postgres://owner@127.0.0.1")) + const failure = new Error("owned subprocess failed") + await assert.rejects(withPrivateRestoreService(uri, async ({ dsn, env }) => { + assert.equal(dsn, "service=mmapp_database_restore") + assert.equal(env.PGSERVICE, "mmapp_database_restore") + assert.equal(env.DATABASE_URL, undefined) + assert.equal(env.PGPASSWORD, undefined) + assert.equal(env.PGOPTIONS, undefined) + assert(env.PGSERVICEFILE?.startsWith(directory + path.sep)) + assert.equal(await readFile(env.PGSERVICEFILE, "utf8"), serialized.replace("[mmapp_database_export]", "[mmapp_database_restore]")) + throw failure + }), failure) + assert.deepEqual(await readdir(directory), []) + } finally { + for (const key of keys) { + if (previous[key] === undefined) delete process.env[key] + else process.env[key] = previous[key] + } + await rm(directory, { recursive: true, force: true }) + } +}) + +test("owner file binds the explicit libpq login and refuses malformed or unsafe files", async () => { + const directory = await realpath(await mkdtemp(path.join(os.tmpdir(), "mm-restore-owner-test-"))) + const file = path.join(directory, "owner-url") + try { + await writeFile(file, uri + "\n", { mode: 0o600 }) + assert.deepEqual(await readRestoreOwnerConnection(file), { connection: uri, owner: "query_user" }) + await assert.rejects(readRestoreOwnerConnection("owner-url")) + if (typeof process.getuid === "function") { + await chmod(file, 0o640) + await assert.rejects(readRestoreOwnerConnection(file)) + await chmod(file, 0o600) + } + await writeFile(file, "postgres://owner%27escape:synthetic@localhost/base\n") + await assert.rejects(readRestoreOwnerConnection(file), error => error instanceof Error && !error.message.includes("synthetic")) + } finally { + await rm(directory, { recursive: true, force: true }) + } +}) + +test("protected dump imports the snapshot, uses a private leaf and refuses profile injections", async () => { + const root = await realpath(await mkdtemp(path.join(os.tmpdir(), "mm-archive-transport-test-"))) + const file = path.join(root, "capture-password"), state = path.join(root, "state") + const previous = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = state + try { + const password = "capture-fixture%password", leaf = "explicit-leaf-fixture" + await writeFile(file, password + "\n", { mode: 0o600 }) + const peer = { host: "source.example", port: 5432, database: "app", capture: { name: "capture", signedOid: "123" }, leaf } + const snapshot = "00000003-000004F1-1" + const archive = Buffer.from("PGDMP") + const inspect: NonNullable[4]> = async (command, args, options) => { + assert.equal(command, "pg_dump") + assert.deepEqual(args, ["-Fc", "--no-password", "--schema=public", "--strict-names", `--snapshot=${snapshot}`]) + assert(options?.env?.PGSERVICEFILE) + assert.equal(options.env.DATABASE_URL, undefined) + assert.equal(options.env.PGOPTIONS, undefined) + const service = await readFile(options.env.PGSERVICEFILE, "utf8") + assert(service.includes(`user=capture\npassword=${password}\ndbname=app\nsslmode=verify-full\n`)) + const leafFile = service.match(/^sslrootcert=(.+)$/m)?.[1] + assert(leafFile) + assert.equal(await readFile(leafFile, "utf8"), leaf) + assert.equal(path.dirname(leafFile), path.dirname(options.env.PGSERVICEFILE)) + return archive + } + assert.equal(await runPinnedDatabaseSnapshotDump(peer, file, snapshot, {}, inspect), archive) + await assert.rejects(runPinnedDatabaseSnapshotDump(peer, file, "--dbname=unrelated", {}, inspect)) + await writeFile(file, password + "\nsslmode=disable") + await assert.rejects(runPinnedDatabaseSnapshotDump(peer, file, snapshot, {}, inspect)) + assert.deepEqual(await readdir(state), []) + } finally { + if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous + await rm(root, { recursive: true, force: true }) + } +}) + +test("legacy dump selects exactly the four journal tables and isolates its service file", async () => { + const directory = await realpath(await mkdtemp(path.join(os.tmpdir(), "mm-legacy-dump-test-"))) + const peer = { host: "source.example", port: 5432, database: "legacy", capture: { name: "publisher", signedOid: "123" }, leaf: "retained leaf" } + const snapshot = "00000003-000004F1-1", archive = Buffer.from("PGDMP") + try { + const result = await runPinnedLegacyProbeSnapshotDump(peer, "protected password", snapshot, directory, {}, async (command, args, options) => { + assert.equal(command, "pg_dump") + assert.deepEqual(args, ["-Fc", "--no-password", "--data-only", "--table=public.mcp_diagnostic_cancellations", + "--table=public.server_probe_execution_quarantines", "--table=public.server_probe_execution_stages", "--table=public.server_probe_executions", + "--strict-names", `--snapshot=${snapshot}`]) + const serviceFile = options?.env?.PGSERVICEFILE + assert(serviceFile) + assert.equal(path.dirname(path.dirname(serviceFile)), directory) + const service = await readFile(serviceFile, "utf8") + assert.match(service, /user=publisher\npassword=protected password\ndbname=legacy\nsslmode=verify-full\n/) + assert.equal(await readFile(path.join(path.dirname(serviceFile), "postgresql-leaf.pem"), "utf8"), peer.leaf) + return archive + }) + assert.equal(result, archive) + await assert.rejects(runPinnedLegacyProbeSnapshotDump(peer, "bad\nsslmode=disable", snapshot, directory, {}, async () => assert.fail("libpq must not start"))) + assert.deepEqual(await readdir(directory), []) + } finally { await rm(directory, { recursive: true, force: true }) } +}) diff --git a/backend/src/services/database-dump-connection.ts b/backend/src/services/database-dump-connection.ts new file mode 100644 index 00000000..7e9ce660 --- /dev/null +++ b/backend/src/services/database-dump-connection.ts @@ -0,0 +1,199 @@ +import { constants } from "node:fs" +import { chmod, lstat, mkdir, mkdtemp, open, realpath, rm, rmdir, writeFile } from "node:fs/promises" +import path from "node:path" +import { mcpStateDirectory } from "../modules/mcp/security.js" +import { boundedDatabaseCommand, DatabaseDumpCommandError } from "./bounded-database-dump.js" +import { DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG } from "./database-control-archive-boundary.js" +import { LEGACY_PROBE_RELATIONS } from "./server-probe-legacy-source-inventory.js" +import type { DatabaseArchivePeer } from "../db/database-runtime-source.js" + +const SERVICE = "mmapp_database_export" +const RESTORE_SERVICE = "mmapp_database_restore" +const unsupported = () => new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_UNSUPPORTED", "Нужен PostgreSQL URI с одним host и значениями, представимыми в service file") + +/** Deliberately bounded URI subset. Unsupported syntax fails, never loses options. */ +function databaseConnectionFields(connection: string): Map { + if (connection.trim() !== connection || /[\u0000-\u0020\u007f]/.test(connection)) throw unsupported() + // URL parsing can otherwise normalize invalid escape sequences or strip raw + // controls. Reject those inputs before constructing any service parameters. + try { decodeURIComponent(connection) } catch { throw unsupported() } + const raw = connection.match(/^[a-z]+:\/\/([^/?#]*)(\/[^?#]*)?(?:\?[^#]*)?(?:#.*)?$/i) + if (!raw || (raw[1].match(/@/g)?.length ?? 0) > 1) throw unsupported() + // WHATWG URL removes dot segments before exposing pathname; libpq treats + // the URI path as a database name. Refuse normalization instead of changing DB. + for (const segment of (raw[2] ?? "").split("/")) { + const decoded = decodeURIComponent(segment) + if (decoded === "." || decoded === "..") throw unsupported() + } + let uri: URL + try { uri = new URL(connection) } catch { throw unsupported() } + if (!["postgres:", "postgresql:"].includes(uri.protocol) || connection.includes("#") || !uri.hostname || /[,/%]/.test(uri.hostname)) throw unsupported() + const fields = new Map() + const put = (key: string, value: string, required = false) => { + if (!/^[a-z_][a-z0-9_]*$/.test(key) || key === "service" || key === "servicefile" || key === "ssl" || /[\u0000-\u001f\u007f]/.test(value) || value.trim() !== value || (required && !value)) throw unsupported() + if (value) fields.set(key, value) + } + const decode = (value: string) => { try { return decodeURIComponent(value) } catch { throw unsupported() } } + put("host", uri.hostname.replace(/^\[|\]$/g, ""), true) + put("port", uri.port) + put("user", decode(uri.username)) + put("password", decode(uri.password)) + put("dbname", decode(uri.pathname.replace(/^\//, ""))) + const seen = new Set() + // Query parameters override URI authority/path, as in libpq URI parsing. + // Keep every key for libpq validation; do not reinterpret SSL or options. + // libpq uses URI percent decoding, not HTML form decoding: literal '+' stays + // '+'. URLSearchParams would silently turn it into a space (even in passwords). + for (const pair of uri.search.slice(1).split("&").filter(Boolean)) { + const separator = pair.indexOf("=") + if (separator < 1) throw unsupported() + const key = decode(pair.slice(0, separator)), value = decode(pair.slice(separator + 1)) + if (seen.has(key)) throw unsupported() + seen.add(key); put(key, value, true) + } + if (fields.get("host")?.includes(",") || fields.get("host")?.includes("/") || fields.get("hostaddr")?.includes(",") || fields.get("port")?.includes(",")) throw unsupported() + return fields +} + +function databaseServiceFile(connection: string, service: typeof SERVICE | typeof RESTORE_SERVICE): string { + const fields = databaseConnectionFields(connection) + if (service === RESTORE_SERVICE && (!fields.get("user") || !fields.get("dbname"))) throw unsupported() + return `[${service}]\n` + [...fields].map(([key, value]) => `${key}=${value}\n`).join("") +} + +export const databaseDumpServiceFile = (connection: string) => databaseServiceFile(connection, SERVICE) + +export function restoreDatabaseToolEnvironment(): NodeJS.ProcessEnv { + return Object.fromEntries(["PATH", "SystemRoot", "WINDIR", "TEMP", "TMP", "LANG", "LC_ALL"] + .map(key => [key, process.env[key]]).filter(([, value]) => value !== undefined)) +} + +async function withPrivateDatabaseService(connection: string, service: typeof SERVICE | typeof RESTORE_SERVICE, + run: (file: string) => Promise): Promise { + const contents = databaseServiceFile(connection, service) + return withPrivateDatabaseServiceFiles(service, () => contents, run) +} + +async function withPrivateDatabaseServiceFiles(service: typeof SERVICE | typeof RESTORE_SERVICE, + contents: (leafFile: string) => string, run: (file: string) => Promise, leaf?: string, root = mcpStateDirectory()): Promise { + await mkdir(root, { recursive: true, mode: 0o700 }) + const directory = await mkdtemp(path.join(root, service === SERVICE ? "database-export-" : "database-restore-")) + const file = path.join(directory, "pg_service.conf") + const leafFile = path.join(directory, "postgresql-leaf.pem") + try { + await chmod(directory, 0o700) + if (leaf) await writeFile(leafFile, leaf, { flag: "wx", mode: 0o600 }) + await writeFile(file, contents(leafFile), { flag: "wx", mode: 0o600 }) + return await run(file) + } finally { + try { await Promise.all([rm(file, { force: true }), rm(leafFile, { force: true })]); await rmdir(directory) } + catch { throw new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_CLEANUP", "Не удалось удалить временные параметры подключения к базе") } + } +} + +/** No connection URI or password enters argv. The file outlives child draining only. */ +export async function runPrivateDatabaseDump(connection: string, options: Omit[2]>, "env">, + command: typeof boundedDatabaseCommand = boundedDatabaseCommand): Promise { + return withPrivateDatabaseService(connection, SERVICE, file => + command("pg_dump", ["-Fc", DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG], { ...options, env: { ...process.env, PGSERVICEFILE: file, PGSERVICE: SERVICE } })) +} + +type DumpOptions = Omit[2]>, "env"> + +export async function runPinnedDatabaseSnapshotDump(peer: DatabaseArchivePeer, passwordFile: string, snapshot: string, + options: DumpOptions, + command: typeof boundedDatabaseCommand = boundedDatabaseCommand): Promise { + return runPinnedSnapshotDump(peer, await readDatabaseArchivePassword(passwordFile), snapshot, ["--schema=public"], options, command) +} + +/** Publisher's transport already owns the password and the protected output directory. */ +export async function runPinnedLegacyProbeSnapshotDump(peer: DatabaseArchivePeer, password: string, snapshot: string, + directory: string, options: DumpOptions, command: typeof boundedDatabaseCommand = boundedDatabaseCommand): Promise { + return runPinnedSnapshotDump(peer, password, snapshot, ["--data-only", ...LEGACY_PROBE_RELATIONS.map(name => `--table=public.${name}`)], + options, command, directory) +} + +async function runPinnedSnapshotDump(peer: DatabaseArchivePeer, password: string, snapshot: string, selection: string[], + options: DumpOptions, command: typeof boundedDatabaseCommand, directory?: string): Promise { + const refused = () => new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_UNSUPPORTED", "Не подтверждены параметры защищённого экспорта PostgreSQL") + if (!/^[0-9A-F]{8,16}-[0-9A-F]{8,16}-[1-9][0-9]*$/.test(snapshot)) throw refused() + const value = (field: string) => { + if (!field || field.trim() !== field || /[\x00-\x1f\x7f]/.test(field)) throw refused() + return field + } + return withPrivateDatabaseServiceFiles(SERVICE, leafFile => `[${SERVICE}] +host=${value(peer.host)} +port=${peer.port} +user=${value(peer.capture.name)} +password=${value(password)} +dbname=${value(peer.database)} +sslmode=verify-full +sslrootcert=${value(leafFile)} +sslcertmode=disable +gssencmode=disable +connect_timeout=5 +options=-c default_transaction_read_only=on +`, file => command("pg_dump", ["-Fc", "--no-password", ...selection, "--strict-names", `--snapshot=${snapshot}`], { + ...options, env: { ...restoreDatabaseToolEnvironment(), PGSERVICEFILE: file, PGSERVICE: SERVICE }, + }), peer.leaf, directory) +} + +export async function readDatabaseArchivePassword(file: string): Promise { + const refused = () => new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_UNSUPPORTED", "Не подтверждён файл пароля роли архивирования PostgreSQL") + const password = await readPrivateConnectionFile(file).catch(() => { throw refused() }) + if (!password || password.length > 4096 || password.trim() !== password || /[\x00-\x1f\x7f]/.test(password)) throw refused() + return password +} + +export function validatePrivateRestoreConnection(connection: string): void { + databaseServiceFile(connection, RESTORE_SERVICE) +} + +/** The optional owner secret stays on disk until an admitted restore needs it. */ +export async function readRestoreOwnerConnection(file: string): Promise<{ connection: string; owner: string }> { + const refused = () => new Error("Не удалось подтвердить файл подключения владельца базы") + try { + const connection = await readPrivateConnectionFile(file) + validatePrivateRestoreConnection(connection) + const owner = databaseConnectionFields(connection).get("user")! + if (!/^[a-z_][a-z0-9_]{0,62}$/.test(owner)) throw refused() + return { connection, owner } + } catch { + throw refused() + } +} + +async function readPrivateConnectionFile(file: string): Promise { + const refused = () => new Error("Не удалось подтвердить файл подключения PostgreSQL") + if (!path.isAbsolute(file) || await realpath(file) !== path.resolve(file)) throw refused() + const entry = await lstat(file, { bigint: true }) + if (!entry.isFile()) throw refused() + const handle = await open(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0)) + try { + const before = await handle.stat({ bigint: true }) + if (!before.isFile() || before.dev !== entry.dev || before.ino !== entry.ino || before.size < 1n || before.size > 8192n || + (typeof process.getuid === "function" && (before.uid !== BigInt(process.getuid()) || (before.mode & 0o077n) !== 0n))) throw refused() + const buffer = Buffer.alloc(Number(before.size) + 1) + try { + let bytes = 0 + while (bytes < buffer.length) { + const next = await handle.read(buffer, bytes, buffer.length - bytes, bytes) + if (!next.bytesRead) break + bytes += next.bytesRead + } + const after = await handle.stat({ bigint: true }) + if (BigInt(bytes) !== before.size || after.size !== before.size || + after.mtimeNs !== before.mtimeNs || after.ctimeNs !== before.ctimeNs) throw refused() + return new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, bytes)).replace(/\r?\n$/, "") + } finally { + buffer.fill(0) + } + } finally { + await handle.close() + } +} + +export function withPrivateRestoreService(connection: string, run: (service: { dsn: string; env: NodeJS.ProcessEnv }) => Promise): Promise { + return withPrivateDatabaseService(connection, RESTORE_SERVICE, file => + run({ dsn: `service=${RESTORE_SERVICE}`, env: { ...restoreDatabaseToolEnvironment(), PGSERVICEFILE: file, PGSERVICE: RESTORE_SERVICE } })) +} diff --git a/backend/src/services/database-maintenance-resolution.test.ts b/backend/src/services/database-maintenance-resolution.test.ts new file mode 100644 index 00000000..c9bebb83 --- /dev/null +++ b/backend/src/services/database-maintenance-resolution.test.ts @@ -0,0 +1,150 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import path from 'node:path' +import {mkdtemp,mkdir,writeFile,readFile,appendFile,rm,lstat,chmod,symlink} from 'node:fs/promises' +import * as filesystem from 'node:fs/promises' +import {fileURLToPath} from 'node:url' +import {createRequire} from 'node:module' +import {randomUUID,createHash} from 'node:crypto' +import {runInNewContext} from 'node:vm' +import {parseMaintenanceJournal,inspectMaintenanceJournal,evaluateMaintenanceAcceptances,runtimeDatabaseTargetSha256,MAINTENANCE_ACCEPTANCE_FORMAT,RESTORE_DATA_JOURNAL_DOMAIN,type MaintenanceAcceptance,type RestoreDataJournalRecord} from './database-maintenance-resolution.js' +import {inspectDatabaseMaintenanceStartup,readMaintenanceRecoveryFacts} from './database-maintenance-startup.js' +const here=path.dirname(fileURLToPath(import.meta.url)),backend=path.resolve(here,'../..'),temp=path.resolve(backend,'../tmp'),require=createRequire(import.meta.url),ts=require('typescript'),digest=(v:Buffer|string)=>createHash('sha256').update(v).digest('hex') +const target='postgresql://service:old-password@db.example.invalid:5432/managed?sslmode=require' +const record=(operationId:string,state:string)=>JSON.stringify({operationId,state,at:'2026-11-01T00:00:00.000Z'})+'\n' +const source={systemIdentifier:'1234567890',databaseOid:'12345',databaseName:'managed',serverVersionNum:180004,inRecovery:false as const} +const knownCurrent={transport:{mode:'tls' as const,endpointSha256:'f'.repeat(64)},source,schemaSha256:'a'.repeat(64),rawSchemaSha256:'b'.repeat(64),physicalSchemaSha256:'c'.repeat(64),manifestSha256:'d'.repeat(64),markersSha256:'e'.repeat(64)} +async function own(work:(root:string,uncertain:string,applied:string)=>Promise){await mkdir(temp,{recursive:true});const root=await mkdtemp(path.join(temp,'owned-maintenance-resolution-'));try{ + await mkdir(path.join(root,'restore-vault'));await writeFile(path.join(root,'access-generation'),Buffer.alloc(32,7));const uncertain=randomUUID(),applied=randomUUID() + await writeFile(path.join(root,'database-maintenance.jsonl'),record(uncertain,'intent')+record(uncertain,'unconfirmed')+record(applied,'intent')+record(applied,'applied')) + for(const [id,state]of [[uncertain,'unknown'],[applied,'applied']]){const archiveRef=randomUUID(),at='2026-11-01T00:00:00.000Z';await writeFile(path.join(root,'restore-vault',archiveRef+'.used'),JSON.stringify({operationId:id,at}));await writeFile(path.join(root,'restore-vault',id+'.receipt.json'),JSON.stringify({operationId:id,archiveRef,state,startedAt:at,automaticRetry:false,...(state==='applied'?{finishedAt:at,accessRevoked:true,warnings:[]}:{errorCode:'RESTORE_UNCONFIRMED',finishedAt:at})}))} + await work(root,uncertain,applied) + }finally{assert(path.dirname(root)===temp&&path.basename(root).startsWith('owned-maintenance-resolution-'));await rm(root,{recursive:true,force:true})}} +export async function modeledStartup(){ + // This is deliberately a MODELED POSIX ownership adapter over actual owned + // files on Windows, not evidence that Windows/Linux protection was accepted. + const sourceText=await readFile(path.join(backend,'src/services/database-maintenance-startup.ts'),'utf8'),exports:Record={} + const modeledStat=(stat:Awaited>)=>new Proxy(stat,{get(object,key){if(key==='uid')return typeof object.uid==='bigint'?0n:0;if(key==='mode')return typeof object.mode==='bigint'?(object.mode&~0o777n)|(object.isDirectory()?0o700n:0o600n):(object.mode&~0o777)|(object.isDirectory()?0o700:0o600);const value=Reflect.get(object,key);return typeof value==='function'?value.bind(object):value}}) + const fs={...filesystem,lstat:async(...args:Parameters)=>modeledStat(await Reflect.apply(filesystem.lstat,filesystem,args)),open:async(...args:Parameters)=>{const handle=await Reflect.apply(filesystem.open,filesystem,args);return new Proxy(handle,{get(object,key){if(key==='stat')return async(...args:Parameters)=>modeledStat(await Reflect.apply(handle.stat,handle,args));const value=Reflect.get(object,key);return typeof value==='function'?value.bind(object):value}})}} + const code=ts.transpileModule(sourceText,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText + runInNewContext(code,{exports,require:(name:string)=>name==='node:fs/promises'?fs:name==='node:process'?{platform:'linux',getuid:()=>0,env:{DATABASE_URL:target}}:require(name),Buffer,TextDecoder,Date,Map,Set,Object,Error}) + return exports as unknown as {inspectDatabaseMaintenanceStartup:typeof inspectDatabaseMaintenanceStartup;readAppliedRestoreAuditSources:(root:string)=>Promise} +} +async function acceptance(root:string,previousResolutionSha256:string|null=null):Promise{const facts=await readMaintenanceRecoveryFacts(root);assert(facts.generation);return {format:MAINTENANCE_ACCEPTANCE_FORMAT,resolutionId:randomUUID(),decision:'accept-observed-current-state',historicOutcomeChanged:false,automaticRestore:false,actor:{kind:'host-operator',id:'explicit-test-operator'},approvalSha256:'1'.repeat(64),inspectionSha256:'2'.repeat(64),approvedAt:'2026-11-02T00:00:00.000Z',approvalExpiresAt:'2026-11-02T00:15:00.000Z',acceptedAt:'2026-11-02T00:01:00.000Z',runtimeTargetSha256:runtimeDatabaseTargetSha256(target),previousResolutionSha256,journalCoverage:{prefixBytes:facts.journal.length,sha256:digest(facts.journal),unresolvedOperations:facts.operations.filter(op=>op.state!=='applied')},vaultCoverage:facts.vault.evidence.filter(marker=>facts.vault.unresolvedFiles.includes(marker.file)),generationObservedSha256:digest(facts.generation),knownCurrent}} +async function publish(root:string,value:MaintenanceAcceptance){await mkdir(path.join(root,'maintenance-acceptances'),{recursive:true});const raw=Buffer.from(JSON.stringify(value));await writeFile(path.join(root,'maintenance-acceptances',value.resolutionId+'.json'),raw);return raw} + +test('explicit historical acceptance allows restart after approval expiry, preserves unknown and imports only actual applied',()=>own(async(root,uncertain,applied)=>{ + const modeled=await modeledStartup(),before=await readFile(path.join(root,'restore-vault',uncertain+'.receipt.json')) + assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') + await publish(root,await acceptance(root));const result=await modeled.inspectDatabaseMaintenanceStartup(root) + assert.equal(result.classification,'accepted-current-state');assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root,target.replace('old-password','rotated-password'))).classification,'accepted-current-state');assert.equal(result.acceptedHistoricResolutions,1);assert.equal(result.historicOutcomesChanged,false) + assert.equal(JSON.stringify((await modeled.readAppliedRestoreAuditSources(root)).map(value=>value.operationId)),JSON.stringify([applied]));assert.deepEqual(await readFile(path.join(root,'restore-vault',uncertain+'.receipt.json')),before) +})) +test('completed suffix and legitimate later schema/partition changes do not require a perpetual SQL fingerprint gate',()=>own(async root=>{ + const modeled=await modeledStartup();await publish(root,await acceptance(root));const later=randomUUID();await appendFile(path.join(root,'database-maintenance.jsonl'),record(later,'intent')+record(later,'applied')) + // Reader has no SQL pool/schema query at all. External generation can rotate + // without reviving old credentials or invalidating historical acceptance. + await writeFile(path.join(root,'access-generation'),Buffer.alloc(32,9));assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'accepted-current-state') + await appendFile(path.join(root,'database-maintenance.jsonl'),record(randomUUID(),'intent'));assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') +})) +test('new explicit acceptance can cover a later uncertainty without altering old records',()=>own(async root=>{ + const modeled=await modeledStartup(),first=await publish(root,await acceptance(root));await appendFile(path.join(root,'database-maintenance.jsonl'),record(randomUUID(),'intent')) + assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required');const second=await acceptance(root,digest(first));second.acceptedAt='2026-11-02T00:02:00.000Z';await publish(root,second) + assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'accepted-current-state');assert.deepEqual(await readFile(path.join(root,'maintenance-acceptances',JSON.parse(first.toString()).resolutionId+'.json')),first) +})) +test('covered bytes drift, missing proof, forks and malformed tail never unlock startup',()=>own(async(root,uncertain)=>{ + const modeled=await modeledStartup(),value=await acceptance(root),raw=await publish(root,value),filename=path.join(root,'maintenance-acceptances',value.resolutionId+'.json'),journal=path.join(root,'database-maintenance.jsonl'),original=await readFile(journal) + for(const mutation of ['receipt','tail','record','oversized-record','fork','target','generation']){ + const receipt=path.join(root,'restore-vault',uncertain+'.receipt.json'),receiptBefore=await readFile(receipt) + if(mutation==='receipt')await writeFile(receipt,receiptBefore.toString().replace('RESTORE_UNCONFIRMED','OTHER_UNCONFIRMED')) + if(mutation==='tail')await appendFile(journal,'{"operationId":') + if(mutation==='record')await writeFile(filename,raw.subarray(0,raw.length-1));if(mutation==='oversized-record')await writeFile(filename,Buffer.alloc(1048577)) + let forkFile='';if(mutation==='fork'){const fork={...value,resolutionId:randomUUID()};forkFile=path.join(root,'maintenance-acceptances',fork.resolutionId+'.json');await writeFile(forkFile,JSON.stringify(fork))} + if(mutation==='generation')await writeFile(path.join(root,'access-generation'),Buffer.alloc(31)) + const result=await modeled.inspectDatabaseMaintenanceStartup(root,mutation==='target'?target.replace('/managed','/other'):target);assert.equal(result.classification,'inspection-required',mutation) + await writeFile(journal,original);await writeFile(filename,raw);await writeFile(receipt,receiptBefore);await writeFile(path.join(root,'access-generation'),Buffer.alloc(32,7));if(forkFile)await rm(forkFile) + } +})) +test('credential rotation is excluded; host/database/TLS identity changes are bound',()=>{ + const expected=runtimeDatabaseTargetSha256(target);assert.equal(runtimeDatabaseTargetSha256(target.replace('old-password','new-password')),expected) + for(const changed of [target.replace('db.example','other.example'),target.replace('/managed','/different'),target.replace('sslmode=require','sslmode=disable')])assert.notEqual(runtimeDatabaseTargetSha256(changed),expected) + assert.throws(()=>runtimeDatabaseTargetSha256(target+'&unreviewed_target_option=true')) +}) +test('native Windows never treats modeled POSIX acceptance as real protected authorization',()=>own(async root=>{ + if(process.platform!=='win32')return + await publish(root,await acceptance(root));assert.equal((await inspectDatabaseMaintenanceStartup(root,target)).classification,'inspection-required') +})) +test('acceptance never accepts torn/duplicate transition grammar or fabricated current proof',()=>own(async root=>{ + const facts=await readMaintenanceRecoveryFacts(root),recordValue=await acceptance(root),raw=Buffer.from(JSON.stringify(recordValue)) + assert.throws(()=>parseMaintenanceJournal(Buffer.from(record(randomUUID(),'applied'))));assert.throws(()=>parseMaintenanceJournal(Buffer.from('{bad}'))) + const wrong={...recordValue,knownCurrent:{...knownCurrent,source:{...source,inRecovery:true}}};assert.throws(()=>evaluateMaintenanceAcceptances([Buffer.from(JSON.stringify(wrong))],facts.journal,facts.vault.evidence,recordValue.runtimeTargetSha256)) + const empty={...recordValue,journalCoverage:{...recordValue.journalCoverage,unresolvedOperations:[]},vaultCoverage:[]};assert.throws(()=>evaluateMaintenanceAcceptances([Buffer.from(JSON.stringify(empty))],facts.journal,facts.vault.evidence,recordValue.runtimeTargetSha256));assert(evaluateMaintenanceAcceptances([raw],facts.journal,facts.vault.evidence,recordValue.runtimeTargetSha256).acceptedCoreIds.size) +})) + +test('bound restore data rows preserve signed payloads and require the same binding through the transition', () => { + const intent: RestoreDataJournalRecord = { + format: RESTORE_DATA_JOURNAL_DOMAIN, + operationId: randomUUID(), + state: 'intent', + at: '2026-11-01T00:00:00.000Z', + binding: { + archiveSha256: 'a'.repeat(64), + dataSha256: 'b'.repeat(64), + runtimeTargetSha256: 'c'.repeat(64), + activationSha256: null, + }, + } + const terminal = {...intent, state: 'applied' as const} + const mac = 'd'.repeat(64) + const row = (value: RestoreDataJournalRecord) => JSON.stringify({...value, mac}) + '\n' + const legacy = randomUUID() + const journal = Buffer.from(record(legacy, 'intent') + row(intent) + record(legacy, 'unconfirmed') + row(terminal)) + assert.deepEqual(inspectMaintenanceJournal(journal), { + operations: [ + {operationId: legacy, state: 'unconfirmed'}, + {operationId: intent.operationId, state: 'applied'}, + ], + authenticatedRecords: [intent, terminal].map(value => ({payload: JSON.stringify(value), mac})), + }) + assert.deepEqual(parseMaintenanceJournal(Buffer.from(row(intent))), [{operationId: intent.operationId, state: 'intent'}]) + for (const bindingKey of Object.keys(intent.binding) as (keyof typeof intent.binding)[]) { + assert.throws(() => inspectMaintenanceJournal(Buffer.from(row(intent) + row({ + ...terminal, binding: {...intent.binding, [bindingKey]: 'e'.repeat(64)}, + }))), bindingKey) + } + for (const invalid of [ + row(intent) + record(intent.operationId, 'applied'), + record(intent.operationId, 'intent') + row(terminal), + row(terminal), + row(intent) + row(terminal) + row(terminal), + row(intent).replace(mac, 'invalid'), + row(intent).replace(mac, ''), + row(intent).padStart(1026, ' '), + record(legacy, 'intent').padStart(258, ' '), + ]) assert.throws(() => inspectMaintenanceJournal(Buffer.from(invalid))) +}) + +test('without an acceptance file existing completed-history startup gains no new target-parser prerequisite',()=>own(async(root,uncertain,applied)=>{ + await writeFile(path.join(root,'database-maintenance.jsonl'),record(applied,'intent')+record(applied,'applied')) + const receipt=JSON.parse((await readFile(path.join(root,'restore-vault',uncertain+'.receipt.json'))).toString());await rm(path.join(root,'restore-vault',uncertain+'.receipt.json'));await rm(path.join(root,'restore-vault',receipt.archiveRef+'.used')) + assert.equal((await inspectDatabaseMaintenanceStartup(root,'postgres:///managed?host=%2Ftmp&options=existing')).classification,'completed-core-maintenance') +})) + +test('new unresolved vault marker blocks even without a new core journal record',()=>own(async root=>{ + const modeled=await modeledStartup();await publish(root,await acceptance(root));const operationId=randomUUID(),archiveRef=randomUUID(),startedAt='2026-11-03T00:00:00.000Z',file=path.join(root,'restore-vault',operationId+'.receipt.json') + await writeFile(file,JSON.stringify({operationId,archiveRef,state:'running',startedAt,automaticRetry:false}));assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') + await writeFile(file,JSON.stringify({operationId,archiveRef,state:'blocked',startedAt,automaticRetry:false,errorCode:'PERMISSION_DENIED',finishedAt:startedAt}));assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'accepted-current-state') + const dangling=randomUUID();await writeFile(path.join(root,'restore-vault',dangling+'.used'),JSON.stringify({operationId:randomUUID(),at:startedAt}));assert.equal((await modeled.inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') +})) + +test('native POSIX immutable fsynced acceptance is consumed by actual startup; mode and symlink drift refuse',async context=>{ + if(process.platform==='win32'){context.skip('Native POSIX filesystem acceptance must run directly on Linux; Windows model is not proof');return} + await own(async root=>{ + const {writeFile:writeProtected}=await import('../../scripts/writer-profile/operator-files.mjs'),value=await acceptance(root),directory=path.join(root,'maintenance-acceptances'),filename=path.join(directory,value.resolutionId+'.json') + await mkdir(directory,{mode:0o700});await writeProtected(filename,Buffer.from(JSON.stringify(value)),{flag:'wx',mode:0o600}) + assert.equal((await inspectDatabaseMaintenanceStartup(root,target)).classification,'accepted-current-state') + await chmod(filename,0o644);assert.equal((await inspectDatabaseMaintenanceStartup(root,target)).classification,'inspection-required');await chmod(filename,0o600) + const alias=path.join(directory,randomUUID()+'.json');await symlink(filename,alias);assert.equal((await inspectDatabaseMaintenanceStartup(root,target)).classification,'inspection-required');await rm(alias) + const later=randomUUID();await appendFile(path.join(root,'database-maintenance.jsonl'),record(later,'intent')+record(later,'applied'));assert.equal((await inspectDatabaseMaintenanceStartup(root,target)).classification,'accepted-current-state') + }) +}) diff --git a/backend/src/services/database-maintenance-resolution.ts b/backend/src/services/database-maintenance-resolution.ts new file mode 100644 index 00000000..145c959d --- /dev/null +++ b/backend/src/services/database-maintenance-resolution.ts @@ -0,0 +1,121 @@ +/** Explicit historical acceptance, not restore success or permanent schema/data admission. */ +import {createHash} from 'node:crypto' +export const MAINTENANCE_ACCEPTANCE_FORMAT='database-maintenance-current-state-acceptance-v1' +export const RESTORE_DATA_JOURNAL_DOMAIN = 'database-restore-data-journal-v1' +const sha=(value:Buffer|string)=>createHash('sha256').update(value).digest('hex') +const hex=(v:unknown):v is string=>typeof v==='string'&&/^[a-f0-9]{64}$/.test(v) +const uuid=(v:unknown):v is string=>typeof v==='string'&&/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(v) +function fail():never{throw Error('Explicit maintenance acceptance unavailable or conflicting')} +function exact(value:unknown,keys:string[]):asserts value is Record{if(!value||typeof value!=='object'||Array.isArray(value)||Object.keys(value).sort().join('\0')!==keys.sort().join('\0'))fail()} +const instant=(v:unknown):v is string=>typeof v==='string'&&Number.isFinite(Date.parse(v))&&new Date(v).toISOString()===v +export function parseMaintenanceJSON(raw:Buffer):unknown{const text=new TextDecoder('utf8',{fatal:true}).decode(raw),value:unknown=JSON.parse(text);if(JSON.stringify(value)!==text.trim())fail();return value} +export interface MaintenanceOperation {operationId:string;state:'intent'|'applied'|'unconfirmed'} +export interface RestoreDataJournalBinding { + archiveSha256: string + dataSha256: string + runtimeTargetSha256: string + activationSha256: string | null + claimId?: string +} +export interface RestoreDataJournalRecord extends MaintenanceOperation { + format: typeof RESTORE_DATA_JOURNAL_DOMAIN + at: string + binding: RestoreDataJournalBinding +} + +const restoreBindingKeys = ['archiveSha256', 'dataSha256', 'runtimeTargetSha256', 'activationSha256'] as const + +/** Parses transition grammar. Callers admitting journal evidence must verify every returned MAC. */ +export function inspectMaintenanceJournal(raw: Buffer) { + if (!raw.length || raw.length > 8388608) fail() + const text = new TextDecoder('utf8', {fatal: true}).decode(raw) + if (!text.endsWith('\n')) fail() + const lines = text.slice(0, -1).split('\n') + if (lines.length > 65536) fail() + const operations = new Map() + const authenticatedRecords: {payload: string; mac: string}[] = [] + for (const line of lines) { + const bytes = Buffer.from(line) + if (!bytes.length || bytes.length > 1024) fail() + const value = parseMaintenanceJSON(bytes) + let bindingKey: string | null = null + if (value && typeof value === 'object' && Object.hasOwn(value, 'format')) { + exact(value, ['format', 'operationId', 'state', 'at', 'binding', 'mac']) + if (value.format !== RESTORE_DATA_JOURNAL_DOMAIN || !hex(value.mac)) fail() + const withClaim = value.binding !== null && typeof value.binding === 'object' && Object.hasOwn(value.binding, 'claimId') + exact(value.binding, withClaim ? [...restoreBindingKeys, 'claimId'] : [...restoreBindingKeys]) + const {archiveSha256, dataSha256, runtimeTargetSha256, activationSha256, claimId} = value.binding + if (!hex(archiveSha256) || !hex(dataSha256) || !hex(runtimeTargetSha256) || + (activationSha256 !== null && !hex(activationSha256)) || (withClaim && !hex(claimId))) fail() + bindingKey = JSON.stringify([archiveSha256, dataSha256, runtimeTargetSha256, activationSha256, claimId]) + const {mac, ...unsigned} = value + authenticatedRecords.push({payload: JSON.stringify(unsigned), mac}) + } else { + if (bytes.length > 256) fail() + exact(value, ['operationId', 'state', 'at']) + } + + const {operationId, state, at} = value + if (!uuid(operationId) || !instant(at)) fail() + const previous = operations.get(operationId) + if (state === 'intent') { + if (previous) fail() + } else if (state === 'applied' || state === 'unconfirmed') { + if (previous?.state !== 'intent' || previous.bindingKey !== bindingKey) fail() + } else fail() + operations.set(operationId, {state, bindingKey}) + } + return {operations: [...operations].map(([operationId, {state}]) => ({operationId, state})), authenticatedRecords} +} + +export function parseMaintenanceJournal(raw: Buffer): MaintenanceOperation[] { + return inspectMaintenanceJournal(raw).operations +} +export function runtimeDatabaseTargetSha256(raw:string):string{ + const url=new URL(raw);if(!['postgres:','postgresql:'].includes(url.protocol)||!url.hostname||url.hash)fail() + const port=Number(url.port||5432);if(!Number.isInteger(port)||port<1||port>65535)fail();const database=decodeURIComponent(url.pathname.slice(1));if(!database||/[\x00-\x1f\x7f/]/.test(database))fail() + const relevant=new Set(['sslmode','ssl','sslrootcert','sslcert','sslkey','channel_binding','gssencmode','target_session_attrs']),ignored=new Set(['user','password','application_name','connect_timeout','statement_timeout','lock_timeout']) + const tls:Record={};for(const [key,value]of url.searchParams){if(relevant.has(key)){if(Object.hasOwn(tls,key))fail();tls[key]=value}else if(!ignored.has(key))fail()} + return sha(JSON.stringify({format:'database-target-selector-v1',host:url.hostname.toLowerCase(),port,database,tls:Object.fromEntries(Object.entries(tls).sort())})) +} +export interface MaintenanceMarker {file:string;bytes:number;sha256:string} +export interface MaintenanceKnownCurrent {transport:{mode:'tls'|'local-loopback';endpointSha256:string};source:{systemIdentifier:string;databaseOid:string;databaseName:string;serverVersionNum:number;inRecovery:false};schemaSha256:string;rawSchemaSha256:string;physicalSchemaSha256:string;manifestSha256:string;markersSha256:string} +export interface MaintenanceAcceptance { + format:typeof MAINTENANCE_ACCEPTANCE_FORMAT;resolutionId:string;decision:'accept-observed-current-state';historicOutcomeChanged:false;automaticRestore:false; + actor:{kind:'host-operator';id:string};approvalSha256:string;inspectionSha256:string;approvedAt:string;approvalExpiresAt:string;acceptedAt:string; + runtimeTargetSha256:string;previousResolutionSha256:string|null;journalCoverage:{prefixBytes:number;sha256:string;unresolvedOperations:MaintenanceOperation[]};vaultCoverage:MaintenanceMarker[];generationObservedSha256:string;knownCurrent:MaintenanceKnownCurrent +} +export function validateMaintenanceKnownCurrent(value:unknown):asserts value is MaintenanceKnownCurrent{ + exact(value,['transport','source','schemaSha256','rawSchemaSha256','physicalSchemaSha256','manifestSha256','markersSha256']);if(!['schemaSha256','rawSchemaSha256','physicalSchemaSha256','manifestSha256','markersSha256'].every(key=>hex(value[key])))fail() + exact(value.transport,['mode','endpointSha256']);if(!['tls','local-loopback'].includes(String(value.transport.mode))||!hex(value.transport.endpointSha256))fail() + exact(value.source,['systemIdentifier','databaseOid','databaseName','serverVersionNum','inRecovery']);const s=value.source + if(typeof s.systemIdentifier!=='string'||!/^[1-9][0-9]{0,19}$/.test(s.systemIdentifier)||BigInt(s.systemIdentifier)>18446744073709551615n||typeof s.databaseOid!=='string'||!/^[1-9][0-9]{0,9}$/.test(s.databaseOid)||BigInt(s.databaseOid)>4294967295n||typeof s.databaseName!=='string'||!/^[^\x00-\x1f\x7f]{1,63}$/.test(s.databaseName)||!Number.isInteger(s.serverVersionNum)||Number(s.serverVersionNum)<180000||Number(s.serverVersionNum)>=190000||s.inRecovery!==false)fail() +} +export function validateMaintenanceAcceptance(value:unknown):asserts value is MaintenanceAcceptance{ + exact(value,['format','resolutionId','decision','historicOutcomeChanged','automaticRestore','actor','approvalSha256','inspectionSha256','approvedAt','approvalExpiresAt','acceptedAt','runtimeTargetSha256','previousResolutionSha256','journalCoverage','vaultCoverage','generationObservedSha256','knownCurrent']) + if(value.format!==MAINTENANCE_ACCEPTANCE_FORMAT||!uuid(value.resolutionId)||value.decision!=='accept-observed-current-state'||value.historicOutcomeChanged!==false||value.automaticRestore!==false)fail() + exact(value.actor,['kind','id']);if(value.actor.kind!=='host-operator'||typeof value.actor.id!=='string'||!/^[^\x00-\x1f\x7f]{1,160}$/.test(value.actor.id))fail() + if(!['approvalSha256','inspectionSha256','runtimeTargetSha256','generationObservedSha256'].every(key=>hex(value[key]))||(value.previousResolutionSha256!==null&&!hex(value.previousResolutionSha256)))fail() + if(!instant(value.approvedAt)||!instant(value.approvalExpiresAt)||!instant(value.acceptedAt)||Date.parse(value.approvedAt)>Date.parse(value.acceptedAt)||Date.parse(value.acceptedAt)>=Date.parse(value.approvalExpiresAt))fail() + exact(value.journalCoverage,['prefixBytes','sha256','unresolvedOperations']);const coverage=value.journalCoverage + if(!Number.isSafeInteger(coverage.prefixBytes)||Number(coverage.prefixBytes)<1||Number(coverage.prefixBytes)>8388608||!hex(coverage.sha256)||!Array.isArray(coverage.unresolvedOperations)||coverage.unresolvedOperations.length>65536)fail() + const ids=new Set();for(const operation of coverage.unresolvedOperations){exact(operation,['operationId','state']);if(!uuid(operation.operationId)||!['intent','unconfirmed'].includes(String(operation.state))||ids.has(operation.operationId))fail();ids.add(operation.operationId)} + if(!Array.isArray(value.vaultCoverage)||value.vaultCoverage.length>4096)fail();const files=new Set();for(const marker of value.vaultCoverage){exact(marker,['file','bytes','sha256']);if(typeof marker.file!=='string'||!/^([a-f0-9-]{36})\.(?:used|receipt\.json)$/.test(marker.file)||!uuid(marker.file.slice(0,36))||!Number.isSafeInteger(marker.bytes)||Number(marker.bytes)<1||Number(marker.bytes)>65536||!hex(marker.sha256)||files.has(marker.file))fail();files.add(marker.file)} + if(!ids.size&&!files.size)fail();validateMaintenanceKnownCurrent(value.knownCurrent) +} +export function evaluateMaintenanceAcceptances(rawRecords:readonly Buffer[],journal:Buffer,markers:readonly MaintenanceMarker[],targetSha256:string){ + if(rawRecords.length>64)fail();const records=new Map(),ids=new Set() + for(const raw of rawRecords){if(!raw.length||raw.length>1048576)fail();const value=parseMaintenanceJSON(raw);validateMaintenanceAcceptance(value);const digest=sha(raw);if(records.has(digest)||ids.has(value.resolutionId)||value.runtimeTargetSha256!==targetSha256)fail();records.set(digest,value);ids.add(value.resolutionId) + const prefix=journal.subarray(0,value.journalCoverage.prefixBytes);if(prefix.length!==value.journalCoverage.prefixBytes||sha(prefix)!==value.journalCoverage.sha256)fail() + const unresolved=parseMaintenanceJournal(prefix).filter(op=>op.state!=='applied');if(JSON.stringify(unresolved)!==JSON.stringify(value.journalCoverage.unresolvedOperations))fail() + for(const marker of value.vaultCoverage){if(!markers.some(actual=>JSON.stringify(actual)===JSON.stringify(marker)))fail()} + } + if(!records.size)return {headSha256:null as string|null,acceptedCoreIds:new Set(),acceptedVaultFiles:new Set(),count:0} + const parents=new Set([...records.values()].map(value=>value.previousResolutionSha256).filter((v):v is string=>v!==null)),heads=[...records.keys()].filter(key=>!parents.has(key));if(heads.length!==1)fail() + let current:string|null=heads[0],child:MaintenanceAcceptance|undefined;const visited=new Set(),acceptedCoreIds=new Set(),acceptedVaultFiles=new Set() + while(current!==null){if(visited.has(current))fail();const value=records.get(current);if(!value)fail();visited.add(current) + if(child){if(child.journalCoverage.prefixBytes!child!.vaultCoverage.some(later=>JSON.stringify(later)===JSON.stringify(marker))))fail()} + value.journalCoverage.unresolvedOperations.forEach(op=>acceptedCoreIds.add(op.operationId));value.vaultCoverage.forEach(marker=>acceptedVaultFiles.add(marker.file));child=value;current=value.previousResolutionSha256 + } + if(visited.size!==records.size)fail();return {headSha256:heads[0],acceptedCoreIds,acceptedVaultFiles,count:records.size} +} diff --git a/backend/src/services/database-maintenance-startup.test.ts b/backend/src/services/database-maintenance-startup.test.ts new file mode 100644 index 00000000..f5f1bbe5 --- /dev/null +++ b/backend/src/services/database-maintenance-startup.test.ts @@ -0,0 +1,249 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import {mkdtemp,readFile,writeFile,appendFile,mkdir,rm,readdir,link} from 'node:fs/promises' +import * as filesystem from 'node:fs/promises' +import {randomUUID} from 'node:crypto' +import path from 'node:path' +import {fileURLToPath,pathToFileURL} from 'node:url' +import {spawnSync} from 'node:child_process' +import {createRequire} from 'node:module' +import {runInNewContext} from 'node:vm' +import {inspectDatabaseMaintenanceStartup,assertDatabaseMaintenanceStartup,DatabaseMaintenanceStartupError} from './database-maintenance-startup.js' +import {executeMcpDatabaseMaintenance} from '../modules/mcp/database-maintenance.js' +import {SYSTEM_DATABASE_OPERATION_LOCK,SYSTEM_DATABASE_REQUEST_LOCK,withSystemDatabaseOperationLock} from '../db/operation-lock.js' +const testFile=fileURLToPath(import.meta.url),here=path.dirname(testFile),backend=path.resolve(here,'../..'),area=path.resolve(backend,'../tmp'),require=createRequire(import.meta.url),ts=require('typescript') +const stamp='2026-11-03T00:00:00.000Z' +// Same installed loader as the backend's owned test conventions, resolved to +// an absolute file URL rather than assuming a child's package resolution. +if(process.argv[2]==='--inspect-owned-maintenance'){ + const value=await inspectDatabaseMaintenanceStartup(process.argv[3]) + process.stdout.write(JSON.stringify(value)+'\n');process.exit(value.classification==='inspection-required'?1:0) +} +if(process.argv[2]==='--build-owned-app'){ + const {buildApp}=await import('../index.js') + try{ + const app=await buildApp({logger:false,startScheduler:process.argv[3]==='consumers'}) + try{process.stdout.write(JSON.stringify({health:(await app.inject('/health')).statusCode})+'\n')} + finally{await app.close()} + process.exit(0) + }catch(error){ + if(!(error instanceof DatabaseMaintenanceStartupError))throw error + process.stdout.write(JSON.stringify({reason:error.inspection.reason})+'\n');process.exit(1) + } +} +const record=(operationId:string,state:string)=>JSON.stringify({operationId,state,at:stamp})+'\n' +async function own(work:(root:string)=>Promise){ + await mkdir(area,{recursive:true});const root=await mkdtemp(path.join(area,'owned-maintenance-startup-')) + try{await work(root)}finally{assert(path.dirname(root)===area&&path.basename(root).startsWith('owned-maintenance-startup-'));await rm(root,{recursive:true,force:true})} +} +async function generation(root:string){await writeFile(path.join(root,'access-generation'),Buffer.alloc(32,7))} +async function completed(root:string){const id=randomUUID();await generation(root);await writeFile(path.join(root,'database-maintenance.jsonl'),record(id,'intent')+record(id,'applied'));return id} +async function outer(root:string,state:string,used=true,wrongClaim=false,warnings:string[]=[]){ + const vault=path.join(root,'restore-vault');await mkdir(vault,{recursive:true}) + const operationId=randomUUID(),archiveRef=randomUUID() + const value:Record={operationId,archiveRef,state,startedAt:stamp,automaticRetry:false} + if(state==='applied')Object.assign(value,{finishedAt:stamp,accessRevoked:true,warnings}) + else if(state!=='running')Object.assign(value,{errorCode:'RESTORE_UNCONFIRMED',finishedAt:stamp}) + await writeFile(path.join(vault,operationId+'.receipt.json'),JSON.stringify(value)) + if(used)await writeFile(path.join(vault,archiveRef+'.used'),JSON.stringify({operationId:wrongClaim?randomUUID():operationId,at:stamp})) + return {vault,operationId,archiveRef} +} +function child(root:string){ + const value=spawnSync(process.execPath,['--import',pathToFileURL(require.resolve('tsx')).href,testFile,'--inspect-owned-maintenance',root],{cwd:backend,encoding:'utf8',timeout:10000}) + assert.equal(value.error,undefined);assert.equal(value.signal,null) + return {code:value.status,inspection:JSON.parse(value.stdout)} +} +test('missing external state is read-only fresh legacy; empty existing state does not create authority',()=>own(async root=>{ + const absent=path.join(root,'not-created'),before=await readdir(root) + assert.equal((await inspectDatabaseMaintenanceStartup(absent)).classification,'fresh-legacy') + assert.deepEqual(await readdir(root),before) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'fresh-legacy') + assert.deepEqual(await readdir(root),before) +})) +test('empty journal is only before-intent when an existing external access generation is readable',()=>own(async root=>{ + await writeFile(path.join(root,'database-maintenance.jsonl'),'') + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') + await generation(root) + const result=await inspectDatabaseMaintenanceStartup(root);assert.equal(result.classification,'empty-before-intent');assert.equal(result.accessGenerationObserved,true) +})) +test('actual fresh process refuses dangling intent and unconfirmed without replay or rewriting',()=>own(async root=>{ + await generation(root);const file=path.join(root,'database-maintenance.jsonl'),id=randomUUID() + await writeFile(file,record(id,'intent')) + const before=await readFile(file) + const first=child(root);assert.equal(first.code,1);assert.equal(first.inspection.reason,'UNFINISHED_MAINTENANCE') + assert.deepEqual(await readFile(file),before) + await appendFile(file,record(id,'unconfirmed')) + const unknown=child(root);assert.equal(unknown.code,1);assert.equal(unknown.inspection.reason,'UNCONFIRMED_MAINTENANCE') + assert.equal(unknown.inspection.automaticRestore,false) +})) +test('application entrypoint checks persisted maintenance before HTTP or consumers start',()=>own(async root=>{ + const run=(mode:'http'|'consumers')=>{ + const result=spawnSync(process.execPath,['--import',pathToFileURL(require.resolve('tsx')).href,testFile,'--build-owned-app',mode],{ + cwd:backend,encoding:'utf8',timeout:20000, + env:{...process.env,MCP_STATE_DIR:root,DATABASE_URL:'postgres://test:test@127.0.0.1:1/test'}, + }) + assert.equal(result.error,undefined);assert.equal(result.signal,null) + return {code:result.status,value:JSON.parse(result.stdout)} + } + const id=await completed(root) + assert.deepEqual(run('http'),{code:0,value:{health:200}}) + await appendFile(path.join(root,'database-maintenance.jsonl'),record(randomUUID(),'intent')) + assert.deepEqual(run('http'),{code:1,value:{reason:'UNFINISHED_MAINTENANCE'}}) + await writeFile(path.join(root,'database-maintenance.jsonl'),record(id,'intent')+record(id,'unconfirmed')) + assert.deepEqual(run('consumers'),{code:1,value:{reason:'UNCONFIRMED_MAINTENANCE'}}) +})) +test('migration failure recorded by the restore writer refuses startup in a new process',()=>own(async root=>{ + const file=path.join(root,'database-maintenance.jsonl'),id=randomUUID() + await executeMcpDatabaseMaintenance(async()=>({migrationError:'Synthetic failure'}),{ + record:state=>appendFile(file,record(id,state)),invalidateAccess:()=>generation(root),close:async()=>{}, + }) + const result=child(root) + assert.equal(result.code,1) + assert.equal(result.inspection.reason,'UNCONFIRMED_MAINTENANCE') + assert.equal(await readFile(file,'utf8'),record(id,'intent')+record(id,'unconfirmed')) +})) +test('core applied allows ordinary bootstrap but never infers outer success, audit import or repaired schema',()=>own(async root=>{ + await completed(root) + const result=child(root);assert.equal(result.code,0);assert.equal(result.inspection.classification,'completed-core-maintenance') + assert.equal(result.inspection.outerOperationInferred,false);assert.equal(result.inspection.auditImportStatus,'not-verified');assert.equal(result.inspection.postRepairSchemaStatus,'not-inspected') +})) +for(const state of ['running','unknown'])test('core applied cannot promote outer '+state+' receipt to completed',()=>own(async root=>{ + await completed(root);await outer(root,state) + const result=await inspectDatabaseMaintenanceStartup(root);assert.equal(result.classification,'inspection-required');assert.equal(result.reason,'EXTERNAL_OPERATION_UNRESOLVED') +})) +test('outer applied receipt requires exact matching persisted used claim',()=>own(async root=>{ + await completed(root);const f=await outer(root,'applied',true) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).outerAppliedReceipts,1) + await writeFile(path.join(f.vault,f.archiveRef+'.used'),JSON.stringify({operationId:randomUUID(),at:stamp})) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') +})) +test('historical applied receipts with schema or journal warnings refuse ordinary startup',()=>own(async root=>{ + await completed(root) + for(const warning of ['migrationError','maintenanceError']){ + const fixture=await outer(root,'applied',true,false,[warning]) + const result=await inspectDatabaseMaintenanceStartup(root) + assert.equal(result.classification,'inspection-required') + assert.equal(result.reason,'EXTERNAL_OPERATION_UNRESOLVED') + assert.equal(result.outerAppliedReceipts,1) + await rm(path.join(fixture.vault,fixture.operationId+'.receipt.json')) + await rm(path.join(fixture.vault,fixture.archiveRef+'.used')) + } + await outer(root,'applied',true,false,['cleanupError']) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'completed-core-maintenance') +})) +test('blocked predispatch operation may share an archive later successfully consumed by a different operation',()=>own(async root=>{ + await completed(root);const a=await outer(root,'blocked',false),operationB=randomUUID() + await writeFile(path.join(a.vault,operationB+'.receipt.json'),JSON.stringify({operationId:operationB,archiveRef:a.archiveRef,state:'applied',startedAt:stamp,automaticRetry:false,finishedAt:stamp,accessRevoked:true,warnings:[]})) + await writeFile(path.join(a.vault,a.archiveRef+'.used'),JSON.stringify({operationId:operationB,at:stamp})) + const result=await inspectDatabaseMaintenanceStartup(root) + assert.equal(result.classification,'completed-core-maintenance');assert.equal(result.blockedReceipts,1);assert.equal(result.outerAppliedReceipts,1) + await writeFile(path.join(a.vault,a.archiveRef+'.used'),JSON.stringify({operationId:a.operationId,at:stamp})) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') +})) +test('missing journal with attempt markers is not a new clean install',()=>own(async root=>{ + await outer(root,'applied',true) + const value=await inspectDatabaseMaintenanceStartup(root);assert.equal(value.classification,'inspection-required');assert.equal(value.reason,'JOURNAL_ABSENT_WITH_ATTEMPT_MARKERS') +})) +test('orphan used claim, unknown publication temporary file and blocked-with-used refuse',()=>own(async root=>{ + await completed(root);const f=await outer(root,'blocked',true) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') + await rm(path.join(f.vault,f.operationId+'.receipt.json')) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).reason,'EXTERNAL_OPERATION_UNRESOLVED') + await rm(path.join(f.vault,f.archiveRef+'.used'));await writeFile(path.join(f.vault,'receipt-publication.tmp'),'partial') + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') +})) +test('malformed tails, duplicates, unknown fields/states and impossible transitions never admit startup',()=>own(async root=>{ + await generation(root);const id=randomUUID(),good=record(id,'intent') + const bad=[good.slice(0,-1),good+'{',good+record(id,'intent'),good+record(id,'applied')+record(id,'applied'),record(id,'applied'),good+record(id,'future'),JSON.stringify({operationId:id,state:'intent',at:stamp,ready:true})+'\n',`{"operationId":"${id}","state":"intent","state":"applied","at":"${stamp}"}\n`] + for(const content of bad){await writeFile(path.join(root,'database-maintenance.jsonl'),content);assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required')} +})) +test('journal byte bound refuses without accepting a truncated history',()=>own(async root=>{ + await generation(root);const file=path.join(root,'database-maintenance.jsonl') + await writeFile(file,Buffer.alloc(8*1024*1024+1,32));assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') +})) +test('hardlinked external files are not accepted as complete history',async t=>own(async root=>{ + await generation(root);const file=path.join(root,'database-maintenance.jsonl') + await writeFile(file,record(randomUUID(),'intent')) + try{await link(file,path.join(root,'other-link'))}catch(error){if((error as NodeJS.ErrnoException).code==='EPERM'){t.skip('Host filesystem policy refuses creating an owned hardlink');return}throw error} + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') +})) +test('bootstrap checks recovery before connecting and holds both maintenance locks through startup writes',()=>own(async root=>{ + const journal=path.join(root,'database-maintenance.jsonl'),intent=record(randomUUID(),'intent') + await generation(root);await writeFile(journal,intent) + const source=await readFile(path.join(backend,'src/db/bootstrap.ts'),'utf8'),events:string[]=[],exports:Record={} + const held=new Set();let appearedWhileWaiting=false,missingMigration=false + const write=(phase:string)=>{assert(held.has(SYSTEM_DATABASE_REQUEST_LOCK)&&held.has(SYSTEM_DATABASE_OPERATION_LOCK));events.push(phase)} + const client={query:async(sql:string,[key]:number[])=>{ + if(sql.includes('pg_advisory_lock(')||sql.includes('pg_try_advisory_lock(')){ + assert(!held.has(key));held.add(key) + if(key===SYSTEM_DATABASE_REQUEST_LOCK&&appearedWhileWaiting)await writeFile(journal,intent) + return {rows:[{acquired:true}]} + } + if(sql.includes('pg_advisory_unlock('))return {rows:[{released:held.delete(key)}]} + return {rows:[]} + },release:(discard:boolean)=>{assert.equal(discard,false);assert.equal(held.size,0);events.push('release')}} + const modules:Record={ + './index.js':{pool:{connect:async()=>{events.push('connect');return client}}}, + './migrate.js':{applySqlMigrations:async()=>write('migrations'),assertSqlMigrationsApplied:async()=>{ + write('verify-migrations');if(missingMigration)throw Error('Missing migration') + }}, + './operation-lock.js':{withSystemDatabaseOperationLock}, + './partitions.js':{dropExpiredPartitions:async()=>write('drop'),ensurePartitionsAround:async()=>write('partitions')}, + './sqlite-import.js':{shouldImportSqlite:async()=>true,importSqliteToPostgres:async()=>{write('import');return {durationMs:0,tables:{}}}}, + '../config.js':{env:{DATABASE_PATH:'synthetic-only'}},'../modules/mcp/security.js':{mcpStateDirectory:()=>root}, + '../services/database-maintenance-startup.js':{assertDatabaseMaintenanceStartup:async(directory:string)=>{events.push('guard');return assertDatabaseMaintenanceStartup(directory)}}, + '../services/database-restore-audit.js':{reconcileDatabaseRestoreAudits:async()=>write('audit-reconcile')}, + '../services/database-restore-checkpoint.js':{readDatabaseRestoreCheckpoint:async()=>null}, + './database-restore-quarantine.js':{databaseRestoreQuarantineEnabled:()=>false}, + } + const code=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText + runInNewContext(code,{exports,require:(name:string)=>{assert(name in modules);return modules[name]},console,process:{env:{}}}) + const initialize=exports.initDatabase as (mode?:'operator-partitions')=>Promise + await assert.rejects(initialize(),error=>error instanceof DatabaseMaintenanceStartupError) + assert.deepEqual(events,['guard']) + await writeFile(journal,'');events.length=0;appearedWhileWaiting=true + await assert.rejects(initialize(),error=>error instanceof DatabaseMaintenanceStartupError) + assert.deepEqual(events,['guard','connect','guard','release']) + await writeFile(journal,'');events.length=0;appearedWhileWaiting=false + await initialize() + assert.deepEqual(events,['guard','connect','guard','migrations','partitions','import','drop','partitions','audit-reconcile','release']) + events.length=0;missingMigration=true + await assert.rejects(initialize('operator-partitions'),/Missing migration/) + assert.deepEqual(events,['guard','connect','guard','verify-migrations','release']) + events.length=0;missingMigration=false + await initialize('operator-partitions') + assert.deepEqual(events,['guard','connect','guard','verify-migrations','drop','partitions','release']) + const main=await readFile(path.join(backend,'src/index.ts'),'utf8') + const start=main.indexOf('if (isMain)');assert(start>=0) + const body=main.slice(start),init=body.indexOf('await initDatabase()'),build=body.indexOf('await buildApp()') + assert(init>=0&&initown(async root=>{ + await generation(root);const operation=randomUUID(),file=path.join(root,'database-maintenance.jsonl') + await writeFile(file,record(operation,'intent')) + const source=await readFile(path.join(backend,'src/services/database-maintenance-startup.ts'),'utf8'),exports:Record={};let changed=false + const model={...filesystem,lstat:async(...args:Parameters)=>{ + const value=await Reflect.apply(filesystem.lstat,filesystem,args) + if(!changed&&args[0]===file){changed=true;await appendFile(file,record(operation,'applied'))} + return value + }} + const code=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText + runInNewContext(code,{exports,require:(name:string)=>name==='node:fs/promises'?model:require(name),Buffer,TextDecoder,Date,Map,Set,Object,Error}) + const inspect=exports.inspectDatabaseMaintenanceStartup as typeof inspectDatabaseMaintenanceStartup + const value=await inspect(root);assert.equal(changed,true);assert.equal(value.classification,'inspection-required') + assert.equal(value.reason,'EXTERNAL_STATE_UNAVAILABLE_OR_MALFORMED') + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'completed-core-maintenance','a later distinct stable inspection may observe actual terminal bytes without rewriting them') +})) +test('existing non-directory or unverifiable ancestor never becomes a missing clean state',()=>own(async root=>{ + await writeFile(path.join(root,'ordinary-file'),'not-directory') + assert.equal((await inspectDatabaseMaintenanceStartup(path.join(root,'ordinary-file','state'))).classification,'inspection-required') + const source=await readFile(path.join(backend,'src/services/database-maintenance-startup.ts'),'utf8'),exports:Record={} + const model={...filesystem,realpath:async(file:Parameters[0])=>{ + if(file===root)throw Object.assign(Error('Synthetic ancestor became unverifiable'),{code:'ENOENT'}) + return filesystem.realpath(file) + }} + const code=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,esModuleInterop:true}}).outputText + runInNewContext(code,{exports,require:(name:string)=>name==='node:fs/promises'?model:require(name),Buffer,TextDecoder,Date,Map,Set,Object,Error}) + const inspect=exports.inspectDatabaseMaintenanceStartup as typeof inspectDatabaseMaintenanceStartup + assert.equal((await inspect(path.join(root,'not-created-state'))).classification,'inspection-required') +})) diff --git a/backend/src/services/database-maintenance-startup.ts b/backend/src/services/database-maintenance-startup.ts new file mode 100644 index 00000000..b57afe3d --- /dev/null +++ b/backend/src/services/database-maintenance-startup.ts @@ -0,0 +1,235 @@ +/** Read-only classification of the EXISTING external maintenance grammar. + * No restore, journal rewrite, generation rotation, SQL, role issuer or READY. */ +import process from 'node:process' +import {constants,type BigIntStats} from 'node:fs' +import {lstat,open,readdir,realpath} from 'node:fs/promises' +import path from 'node:path' +import {createHash,createHmac,timingSafeEqual} from 'node:crypto' +import {readDatabaseRestoreJournalHead,type RestoreJournalHead} from '../db/database-restore-quarantine.js' +import {inspectMaintenanceJournal,parseMaintenanceJSON,evaluateMaintenanceAcceptances,runtimeDatabaseTargetSha256,RESTORE_DATA_JOURNAL_DOMAIN,type MaintenanceMarker,type RestoreDataJournalBinding,type RestoreDataJournalRecord} from './database-maintenance-resolution.js' + +const MAX_JOURNAL_BYTES=8*1024*1024,MAX_VAULT_ENTRIES=4096,MAX_RECEIPT_BYTES=65536 +const uuid=/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/ +type Reason='NONE'|'UNFINISHED_MAINTENANCE'|'UNCONFIRMED_MAINTENANCE'|'EXTERNAL_OPERATION_UNRESOLVED'|'JOURNAL_ABSENT_WITH_ATTEMPT_MARKERS'|'EXTERNAL_STATE_UNAVAILABLE_OR_MALFORMED' +export interface MaintenanceStartupInspection { + readonly classification:'fresh-legacy'|'empty-before-intent'|'completed-core-maintenance'|'accepted-current-state'|'inspection-required' + readonly reason:Reason + readonly journalOperations:number + readonly coreApplied:number + readonly outerAppliedReceipts:number + readonly blockedReceipts:number + readonly accessGenerationObserved:boolean + readonly outerOperationInferred:false + readonly auditImportStatus:'not-verified' + readonly postRepairSchemaStatus:'not-inspected' + readonly acceptedHistoricResolutions:number + readonly historicOutcomesChanged:false + readonly automaticRestore:false +} +class Refusal extends Error {constructor(readonly reason:Reason='EXTERNAL_STATE_UNAVAILABLE_OR_MALFORMED'){super('External maintenance state requires inspection')}} +function refuse(reason?:Reason):never{throw new Refusal(reason)} +function plain(value:unknown):value is Record{return value!==null&&typeof value==='object'&&!Array.isArray(value)} +function exact(value:unknown,keys:readonly string[]):asserts value is Record{if(!plain(value)||Object.keys(value).sort().join('\0')!==[...keys].sort().join('\0'))refuse()} +function id(value:unknown):asserts value is string{if(typeof value!=='string'||!uuid.test(value))refuse()} +function instant(value:unknown):asserts value is string{if(typeof value!=='string'||!Number.isFinite(Date.parse(value))||new Date(value).toISOString()!==value)refuse()} +function json(raw:Buffer):unknown{ + const text=new TextDecoder('utf8',{fatal:true}).decode(raw) + // All existing writers produce JSON.stringify output. Exact roundtrip also + // refuses duplicate keys and ambiguous/trailing non-JSON representations. + const value:unknown=JSON.parse(text) + if(JSON.stringify(value)!==text.trim())refuse() + return value +} +function identity(s:BigIntStats){return [s.dev,s.ino,s.size,s.mode,s.nlink,s.mtimeNs,s.ctimeNs].map(String).join(':')} +async function directory(file:string,optional=false){ + let s + try{s=await lstat(file,{bigint:true})} + catch(error){if(optional&&(error as NodeJS.ErrnoException).code==='ENOENT')return null;throw error} + if(!s.isDirectory()||s.isSymbolicLink())refuse() + // Only initial lstat absence is a new/legacy absence. An existing ancestor + // whose realpath disappears or cannot be verified is ambiguous, not fresh. + try{if(await realpath(file)!==file)refuse()}catch{refuse()} + return identity(s) +} +async function existingAncestors(file:string){ + for(let current=path.dirname(file);;current=path.dirname(current)){ + await directory(current,true) + if(current===path.dirname(current))break + } +} +async function bounded(file:string,maximum:number,optional=false,sync=false){ + let before + try{before=await lstat(file,{bigint:true})}catch(error){if(optional&&(error as NodeJS.ErrnoException).code==='ENOENT')return null;throw error} + if(!before.isFile()||before.isSymbolicLink()||before.nlink!==1n||before.size>BigInt(maximum)||await realpath(file)!==file)refuse() + const signature=(s:typeof before)=>[s.dev,s.ino,s.size,s.mode,s.nlink,s.mtimeNs,s.ctimeNs].map(String).join(':') + const handle=await open(file,(sync?constants.O_RDWR:constants.O_RDONLY)|(constants.O_NOFOLLOW??0)) + try{ + const current=await handle.stat({bigint:true});if(signature(current)!==signature(before))refuse() + const data=Buffer.alloc(Number(current.size)+1);let size=0 + while(size>} +interface VaultFacts {markers:number;applied:number;blocked:number;unresolved:boolean;auditSources:readonly AppliedRestoreAuditSource[];evidence:readonly MaintenanceMarker[];unresolvedFiles:readonly string[]} +async function vaultFacts(stateDirectory:string):Promise{ + const root=path.join(stateDirectory,'restore-vault'),before=await directory(root,true) + if(before===null)return {markers:0,applied:0,blocked:0,unresolved:false,auditSources:[],evidence:[],unresolvedFiles:[]} + const auditSources:AppliedRestoreAuditSource[]=[],evidence:MaintenanceMarker[]=[],unresolvedFiles=new Set() + const names=(await readdir(root)).sort();if(names.length>MAX_VAULT_ENTRIES)refuse() + const signatures=new Map(),used=new Map(),receipts=new Map() + for(const name of names){ + const match=/^([a-f0-9-]{36})\.(used|receipt\.json|archive\.json|archive\.bin)$/.exec(name) + if(!match)refuse();id(match[1]) + const file=path.join(root,name),stat=await lstat(file,{bigint:true});if(!stat.isFile()||stat.isSymbolicLink()||stat.nlink!==1n||await realpath(file)!==file)refuse() + signatures.set(name,identity(stat)) + if(match[2]==='used'){ + const raw=(await bounded(file,4096))!;evidence.push({file:name,bytes:raw.length,sha256:createHash('sha256').update(raw).digest('hex')});const value=json(raw);exact(value,['operationId','at']);id(value.operationId);instant(value.at) + used.set(match[1],{operationId:value.operationId}) + }else if(match[2]==='receipt.json'){ + const raw=(await bounded(file,MAX_RECEIPT_BYTES))!,value=json(raw);evidence.push({file:name,bytes:raw.length,sha256:createHash('sha256').update(raw).digest('hex')}) + if(!plain(value)||!['running','applied','unknown','blocked'].includes(String(value.state)))refuse() + const fields=['operationId','archiveRef','state','startedAt','automaticRetry'] + if(value.state==='applied')fields.push('finishedAt','accessRevoked','warnings') + else if(value.state!=='running')fields.push('errorCode','finishedAt') + exact(value,fields);id(value.operationId);id(value.archiveRef);instant(value.startedAt) + if(value.operationId!==match[1]||value.automaticRetry!==false)refuse() + if(value.state!=='running')instant(value.finishedAt) + if(value.state==='applied'){ + const allowed=new Set(['listenerError','schedulerError','cleanupError','migrationError','maintenanceError']) + if(value.accessRevoked!==true||!Array.isArray(value.warnings)||value.warnings.length>5||value.warnings.some(w=>typeof w!=='string'||!allowed.has(w))||new Set(value.warnings).size!==value.warnings.length)refuse() + // An older writer could record successful archive application despite an unconfirmed schema or journal. + if(value.warnings.includes('migrationError')||value.warnings.includes('maintenanceError')){ + unresolvedFiles.add(name) + } + auditSources.push(Object.freeze({operationId:value.operationId,archiveRef:value.archiveRef,finishedAt:value.finishedAt as string,receiptSha256:createHash('sha256').update(raw).digest('hex'),receipt:Object.freeze({...value})})) + }else if(value.state!=='running'&&(typeof value.errorCode!=='string'||!/^[A-Z][A-Z0-9_]{0,79}$/.test(value.errorCode)))refuse() + receipts.set(match[1],{state:String(value.state),archiveRef:value.archiveRef}) + } + } + let applied=0,blocked=0 + for(const [operationId,receipt]of receipts){ + const claim=used.get(receipt.archiveRef) + if(receipt.state==='applied'){if(!claim||claim.operationId!==operationId)refuse();applied++} + else if(receipt.state==='blocked'){if(claim?.operationId===operationId)refuse();blocked++} + else unresolvedFiles.add(operationId+'.receipt.json') + } + for(const [archiveRef,claim]of used){const receipt=receipts.get(claim.operationId);if(!receipt||receipt.archiveRef!==archiveRef||receipt.state!=='applied')unresolvedFiles.add(archiveRef+'.used')} + if(await directory(root)!==before||(await readdir(root)).sort().join('\0')!==[...names].sort().join('\0'))refuse() + for(const [name,signature]of signatures)if(identity(await lstat(path.join(root,name),{bigint:true}))!==signature)refuse() + return {markers:used.size+receipts.size,applied,blocked,unresolved:unresolvedFiles.size>0,auditSources:Object.freeze(auditSources),evidence:Object.freeze(evidence),unresolvedFiles:Object.freeze([...unresolvedFiles].sort())} +} +/** Strict inspection facts; no acceptance decision and no SQL. Operator commands + * use the same grammar, not a looser recovery parser. Existing single managed + * host/operator assumption applies; bounded scans are not a concurrent fence. */ +export async function readMaintenanceRecoveryFacts(rawDirectory:string,syncJournal=false){ + if(typeof rawDirectory!=='string'||!path.isAbsolute(rawDirectory)||path.resolve(rawDirectory)!==rawDirectory)refuse() + await existingAncestors(rawDirectory);const stateBefore=await directory(rawDirectory,true) + if(stateBefore===null)return {kind:'fresh-legacy' as const,journal:Buffer.alloc(0),operations:[],vault:{markers:0,applied:0,blocked:0,unresolved:false,auditSources:[],evidence:[],unresolvedFiles:[]} as VaultFacts,generation:null} + const journalFile=path.join(rawDirectory,'database-maintenance.jsonl'),raw=await bounded(journalFile,MAX_JOURNAL_BYTES,true,syncJournal),vault=await vaultFacts(rawDirectory) + if(await directory(rawDirectory)!==stateBefore)refuse() + if(raw===null||raw.length===0){if(vault.markers)refuse('JOURNAL_ABSENT_WITH_ATTEMPT_MARKERS');if(raw===null)return {kind:'fresh-legacy' as const,journal:Buffer.alloc(0),operations:[],vault,generation:null} + const generation=await bounded(path.join(rawDirectory,'access-generation'),32);if(generation?.length!==32)refuse();return {kind:'empty-before-intent' as const,journal:raw,operations:[],vault,generation} + } + const generation=await bounded(path.join(rawDirectory,'access-generation'),32);if(generation?.length!==32)refuse() + const {operations,authenticatedRecords}=inspectMaintenanceJournal(raw) + if(authenticatedRecords.length){ + const material=await bounded(path.join(rawDirectory,'encryption-key'),32) + if(material?.length!==32)refuse() + const key=createHmac('sha256',material).update('MCP private fingerprint:'+RESTORE_DATA_JOURNAL_DOMAIN).digest() + material.fill(0) + try { + for(const {payload,mac} of authenticatedRecords){ + const expected=createHmac('sha256',key).update(payload).digest() + if(!timingSafeEqual(expected,Buffer.from(mac,'hex')))refuse() + } + } finally {key.fill(0)} + } + if(!(await bounded(journalFile,MAX_JOURNAL_BYTES))!.equals(raw)||await directory(rawDirectory)!==stateBefore)refuse() + return {kind:'journal' as const,journal:raw,operations,vault,generation,authenticated:authenticatedRecords.length>0} +} +export function databaseRestoreJournalHead(journal:Buffer):RestoreJournalHead{ + return {bytes:journal.length,sha256:createHash('sha256').update(journal).digest('hex')} +} +export async function readPendingRestoreCompletion(directory:string,operationId:string,intentHead:RestoreJournalHead,syncJournal=false):Promise<{ + appliedHead:RestoreJournalHead;previousHead:RestoreJournalHead|null;binding:RestoreDataJournalBinding;appliedAt:number +}>{ + const facts=await readMaintenanceRecoveryFacts(directory,syncJournal) + if(facts.kind!=='journal'||!facts.authenticated)refuse() + const {journal}=facts + const {bytes,sha256}=intentHead + if(!Number.isInteger(bytes)||bytes<2||bytes>=journal.length||journal[bytes-1]!==0x0a)refuse() + if(databaseRestoreJournalHead(journal.subarray(0,bytes)).sha256!==sha256)refuse() + if(journal.indexOf(0x0a,bytes)!==journal.length-1)refuse() + + // The full scan validates each record, authenticates signed entries and matches the transition binding. + const intentStart=journal.lastIndexOf(0x0a,bytes-2)+1 + const intent=parseMaintenanceJSON(journal.subarray(intentStart,bytes-1)) as RestoreDataJournalRecord + const applied=parseMaintenanceJSON(journal.subarray(bytes,journal.length-1)) as RestoreDataJournalRecord + if(intent.format!==RESTORE_DATA_JOURNAL_DOMAIN||intent.state!=='intent'||intent.operationId!==operationId|| + applied.format!==RESTORE_DATA_JOURNAL_DOMAIN||applied.state!=='applied'||applied.operationId!==operationId)refuse() + const previous=journal.subarray(0,intentStart) + const previousHead=previous.length&&inspectMaintenanceJournal(previous).authenticatedRecords.length?databaseRestoreJournalHead(previous):null + return {appliedHead:databaseRestoreJournalHead(journal),previousHead,binding:applied.binding,appliedAt:Date.parse(applied.at)} +} +/** The caller holds the system database operation lock and has checked the active claim. */ +export async function assertDatabaseRestoreJournalCurrent(client:Parameters[0],directory:string){ + const head=await readDatabaseRestoreJournalHead(client) + const facts=await readMaintenanceRecoveryFacts(directory) + const authenticated=facts.kind==='journal'&&facts.authenticated + if(head){ + const actual=databaseRestoreJournalHead(facts.journal) + if(!authenticated||actual.bytes!==head.bytes||actual.sha256!==head.sha256)refuse() + }else if(authenticated)refuse() + return head +} +/** Acceptance grants startup admission, so POSIX ownership/modes are verified. + * Native Windows protection has no implementation and refuses, never modeled. */ +export async function readMaintenanceAcceptanceRecords(stateDirectory:string):Promise{ + const root=path.join(stateDirectory,'maintenance-acceptances'),before=await directory(root,true);if(before===null)return [] + if(process.platform==='win32'||typeof process.getuid!=='function')refuse() + const uid=BigInt(process.getuid()),protectedStat=async(file:string,isDirectory:boolean)=>{const s=await lstat(file,{bigint:true});if(s.uid!==uid||(s.mode&0o077n)!==0n||(isDirectory?!s.isDirectory():!s.isFile()))refuse()} + await protectedStat(root,true) + for(let current=path.dirname(root);;current=path.dirname(current)){const stat=await lstat(current,{bigint:true});if(stat.isSymbolicLink()||!stat.isDirectory()||(stat.uid!==uid&&stat.uid!==0n)||((stat.mode&0o022n)!==0n&&!(stat.uid===0n&&(stat.mode&0o1000n)!==0n)))refuse();if(current===path.dirname(current))break} + const names=(await readdir(root)).sort();if(names.length>64)refuse();const records:Buffer[]=[] + for(const name of names){if(!/^[a-f0-9-]{36}\.json$/.test(name))refuse();id(name.slice(0,36));const file=path.join(root,name);await protectedStat(file,false);const raw=(await bounded(file,1048576))!,value=json(raw);if(!plain(value)||value.resolutionId!==name.slice(0,36))refuse();records.push(raw)} + if(await directory(root)!==before||(await readdir(root)).sort().join('\0')!==names.join('\0'))refuse();return records +} +const configuredTarget=()=>process.env.DATABASE_URL??'postgres://mmapp:mmapp@127.0.0.1:5432/mmapp' +export async function inspectDatabaseMaintenanceStartup(rawDirectory:string,runtimeTarget=configuredTarget()):Promise{ + const facts={journalOperations:0,coreApplied:0,outerAppliedReceipts:0,blockedReceipts:0,accessGenerationObserved:false,outerOperationInferred:false as const,auditImportStatus:'not-verified' as const,postRepairSchemaStatus:'not-inspected' as const,acceptedHistoricResolutions:0,historicOutcomesChanged:false as const,automaticRestore:false as const} + const result=(classification:MaintenanceStartupInspection['classification'],reason:Reason):MaintenanceStartupInspection=>Object.freeze({classification,reason,...facts}) + try{ + const state=await readMaintenanceRecoveryFacts(rawDirectory),records=await readMaintenanceAcceptanceRecords(rawDirectory) + facts.outerAppliedReceipts=state.vault.applied;facts.blockedReceipts=state.vault.blocked;facts.accessGenerationObserved=state.generation!==null + if(state.kind!=='journal'){if(records.length)refuse();return result(state.kind,'NONE')} + facts.journalOperations=state.operations.length;facts.coreApplied=state.operations.filter(op=>op.state==='applied').length + const accepted=evaluateMaintenanceAcceptances(records,state.journal,state.vault.evidence,records.length?runtimeDatabaseTargetSha256(runtimeTarget):'0'.repeat(64));facts.acceptedHistoricResolutions=accepted.count + const unresolved=state.operations.filter(op=>op.state!=='applied'&&!accepted.acceptedCoreIds.has(op.operationId)) + if(unresolved.some(op=>op.state==='intent'))return result('inspection-required','UNFINISHED_MAINTENANCE') + if(unresolved.some(op=>op.state==='unconfirmed'))return result('inspection-required','UNCONFIRMED_MAINTENANCE') + if(state.vault.unresolvedFiles.some(file=>!accepted.acceptedVaultFiles.has(file)))return result('inspection-required','EXTERNAL_OPERATION_UNRESOLVED') + return result(accepted.count?'accepted-current-state':'completed-core-maintenance','NONE') + }catch(error){return result('inspection-required',error instanceof Refusal?error.reason:'EXTERNAL_STATE_UNAVAILABLE_OR_MALFORMED')} +} +export class DatabaseMaintenanceStartupError extends Error { + readonly code='DATABASE_MAINTENANCE_INSPECTION_REQUIRED' + constructor(readonly inspection:MaintenanceStartupInspection){super('External database maintenance requires read-only inspection before managed startup')} +} +export async function assertDatabaseMaintenanceStartup(directory:string,runtimeTarget=configuredTarget()):Promise{ + const result=await inspectDatabaseMaintenanceStartup(directory,runtimeTarget) + if(result.classification==='inspection-required')throw new DatabaseMaintenanceStartupError(result) + return result +} +/** Audit recovery consumes the same strict grammar/claims as startup. No core + * applied record can manufacture an applied outer receipt or original actor. */ +export async function readAppliedRestoreAuditSources(directory:string,runtimeTarget=configuredTarget()):Promise{ + await assertDatabaseMaintenanceStartup(directory,runtimeTarget) + const before=await vaultFacts(directory) + await assertDatabaseMaintenanceStartup(directory,runtimeTarget) + const after=await vaultFacts(directory) + if(JSON.stringify(before)!==JSON.stringify(after))throw new DatabaseMaintenanceStartupError(await inspectDatabaseMaintenanceStartup(directory)) + return after.auditSources +} diff --git a/backend/src/services/database-partition-restore-keys.ts b/backend/src/services/database-partition-restore-keys.ts new file mode 100644 index 00000000..8b8f91ec --- /dev/null +++ b/backend/src/services/database-partition-restore-keys.ts @@ -0,0 +1,53 @@ +/** An attached standalone index keeps its original connoinherit flag in PG18. + * Recreate only the reviewed parents' affected keys through native recursion. */ +export function restoreManagedPartitionPrimaryKeysSql(parents: readonly string[]): string { + if (!parents.length) return "SELECT 1;" + if (parents.some(name => !/^[a-z_][a-z_0-9]*$/.test(name))) throw new Error("Invalid managed partition parent") + return `DO $mm_restore_keys$ +DECLARE key record; original_children int; repaired_keys int; +BEGIN + FOR key IN + SELECT p.oid AS relation_oid, c.oid AS constraint_oid, p.relname, c.conname, + pg_catalog.pg_get_constraintdef(c.oid) AS definition + FROM pg_catalog.pg_constraint c + JOIN pg_catalog.pg_class p ON p.oid=c.conrelid + JOIN pg_catalog.pg_namespace n ON n.oid=p.relnamespace + WHERE n.nspname='public' AND p.relkind='p' AND c.contype='p' AND c.conparentid=0 + AND p.relname IN (${parents.map(name => `'${name}'`).join(", ")}) + AND EXISTS (SELECT 1 FROM pg_catalog.pg_constraint child WHERE child.conparentid=c.oid AND child.connoinherit) + LOOP + SELECT count(*) INTO original_children FROM pg_catalog.pg_inherits + WHERE inhparent=key.relation_oid AND NOT inhdetachpending; + IF original_children <> (SELECT count(*) FROM pg_catalog.pg_constraint WHERE conparentid=key.constraint_oid) + OR EXISTS ( + SELECT 1 FROM pg_catalog.pg_constraint c + JOIN pg_catalog.pg_class rel ON rel.oid=c.conrelid + JOIN pg_catalog.pg_class idx ON idx.oid=c.conindid + JOIN pg_catalog.pg_index i ON i.indexrelid=idx.oid + JOIN pg_catalog.pg_am am ON am.oid=idx.relam + WHERE (c.oid=key.constraint_oid OR c.conparentid=key.constraint_oid) + AND (c.contype<>'p' OR NOT c.convalidated OR NOT c.conenforced OR c.condeferrable OR c.condeferred OR c.conperiod + OR c.conname<>rel.relname||'_pkey' OR idx.relname<>c.conname OR am.amname<>'btree' + OR NOT (i.indisprimary AND i.indisvalid AND i.indisready AND i.indislive) + OR i.indisclustered OR i.indisreplident + OR idx.reltablespace<>0 OR idx.reloptions IS NOT NULL + OR pg_catalog.obj_description(c.oid,'pg_constraint') IS NOT NULL + OR pg_catalog.obj_description(idx.oid,'pg_class') IS NOT NULL + OR EXISTS (SELECT 1 FROM pg_catalog.pg_attribute a WHERE a.attrelid=idx.oid AND (a.attoptions IS NOT NULL OR a.attstattarget IS NOT NULL)) + OR (c.oid<>key.constraint_oid AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_inherits h + WHERE h.inhrelid=rel.oid AND h.inhparent=key.relation_oid AND NOT h.inhdetachpending))) + ) THEN RAISE EXCEPTION 'Unsupported primary key repair on restored partition' USING ERRCODE='55000'; + END IF; + + EXECUTE pg_catalog.format('ALTER TABLE public.%I DROP CONSTRAINT %I RESTRICT',key.relname,key.conname); + EXECUTE pg_catalog.format('ALTER TABLE public.%I ADD CONSTRAINT %I %s',key.relname,key.conname,key.definition); + SELECT count(*) INTO repaired_keys FROM pg_catalog.pg_constraint c + JOIN pg_catalog.pg_constraint p ON p.oid=c.conparentid + WHERE p.conrelid=key.relation_oid AND p.contype='p' AND c.contype='p' + AND c.coninhcount=1 AND NOT (c.connoinherit OR c.conislocal); + IF repaired_keys<>original_children THEN + RAISE EXCEPTION 'Native primary key repair was not confirmed' USING ERRCODE='55000'; + END IF; + END LOOP; +END $mm_restore_keys$;` +} diff --git a/backend/src/services/database-protected-archive.ts b/backend/src/services/database-protected-archive.ts new file mode 100644 index 00000000..a4c8581f --- /dev/null +++ b/backend/src/services/database-protected-archive.ts @@ -0,0 +1,38 @@ +import { readRuntimeDatabaseArchivePeer } from "../db/index.js" +import { withRuntimeCatalogSnapshot } from "../db/database-runtime-schema.js" +import type { DatabaseOperationLockClient } from "../db/operation-lock.js" +import { DatabaseDumpCommandError } from "./bounded-database-dump.js" +import { runPinnedDatabaseSnapshotDump } from "./database-dump-connection.js" + +export async function exportProtectedDatabaseSnapshot( + client: DatabaseOperationLockClient, discard: () => void, passwordFile: string | undefined, + options?: { signal?: AbortSignal; authorize?: () => Promise }, +): Promise { + const refused = () => new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_UNSUPPORTED", "Не подтверждено подключение защищённого экспорта PostgreSQL") + if (!passwordFile) throw refused() + const peer = readRuntimeDatabaseArchivePeer(client) + // Hold the writer drain and export transaction until the libpq child has drained. + const result = await withRuntimeCatalogSnapshot(client, discard, async sql => { + const { rows } = await sql.query(`SELECT pg_catalog.pg_export_snapshot() AS snapshot + FROM pg_catalog.pg_roles WHERE rolname=$1::pg_catalog.name AND oid=$2::pg_catalog.oid + AND rolcanlogin AND NOT (rolsuper OR rolreplication OR rolbypassrls OR rolcreaterole OR rolcreatedb)`, + [peer.capture.name, peer.capture.signedOid]) + if (rows.length !== 1 || typeof rows[0].snapshot !== "string") throw refused() + let archive: Buffer + try { + archive = await runPinnedDatabaseSnapshotDump(peer, passwordFile, rows[0].snapshot, { + signal: options?.signal, + authorize: async () => { + readRuntimeDatabaseArchivePeer(client) + await options?.authorize?.() + }, + }) + } catch (error) { + return { error } as const + } + readRuntimeDatabaseArchivePeer(client) + return { archive } as const + }) + if ("error" in result) throw result.error + return result.archive +} diff --git a/backend/src/services/database-request-gate.test.ts b/backend/src/services/database-request-gate.test.ts index 6c9309a1..d2898bca 100644 --- a/backend/src/services/database-request-gate.test.ts +++ b/backend/src/services/database-request-gate.test.ts @@ -3,6 +3,7 @@ import { test } from "node:test" import Fastify from "fastify" import { DatabaseRequestGate } from "./database-request-gate.js" import { installDatabaseRequestGate, registerDatabaseStreamAbort } from "../plugins/database-request-gate.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" function deferred() { let resolve!: () => void @@ -46,7 +47,7 @@ test("stream cancellation waits for its asynchronous cleanup", async () => { }) test("Fastify gate covers GET, keeps health available and drains late stream registration", async () => { - const app = Fastify(); installDatabaseRequestGate(app) + const app = Fastify(); installDatabaseRequestGate(app, work => work()) const entered = deferred(), read = deferred(), cleanup = deferred(), command = deferred(), leave = deferred() app.get('/health', async () => ({ ok: true })) await app.register(async child => { @@ -57,7 +58,8 @@ test("Fastify gate covers GET, keeps health available and drains late stream reg return { closed: true } }) child.get('/servers', async () => ({ servers: [] })) - child.post('/system/database/restore', async () => { command.resolve(); await leave.promise; return { ok: true } }) + // The restore service now owns the shared gate, including non-HTTP callers. + child.post('/system/database/restore', async () => sharedDatabaseGate.exclusive(async () => { command.resolve(); await leave.promise; return { ok: true } })) }, { prefix: '/api' }) try { const stream = app.inject('/api/traffic/flow/live'); await entered.promise diff --git a/backend/src/services/database-request-gate.ts b/backend/src/services/database-request-gate.ts index 4ec77dc8..750a4c4d 100644 --- a/backend/src/services/database-request-gate.ts +++ b/backend/src/services/database-request-gate.ts @@ -1,19 +1,64 @@ +import { AsyncLocalStorage } from "node:async_hooks" function conflict(message: string): Error & { statusCode: number } { return Object.assign(new Error(message), { statusCode: 409 }) } +type DatabaseAdmission = { active: boolean; kind: "request" | "exclusive"; requestAncestry: boolean; children: Set> } + /** Handler lifetime, not socket lifetime: disconnect does not finish an active write. */ export class DatabaseRequestGate { + private readonly requestContext = new AsyncLocalStorage() + assertNoRequestContext(): void { if (this.requestContext.getStore()?.requestAncestry) throw conflict("Scheduler bootstrap inside request context") } + /** Live private scope only; detached children cannot retain a settled admission. */ + hasActiveAdmission(): boolean { return this.requestContext.getStore()?.active === true } + /** Registration is synchronous; the parent drains this continuation and its + * descendants even if the caller deliberately does not await its result. */ + async withinAdmission(run: () => Promise): Promise { + const parent = this.requestContext.getStore() + if (!parent?.active) throw conflict("Database admission is no longer active") + const admission: DatabaseAdmission = { active: true, kind: parent.kind, requestAncestry: parent.requestAncestry, children: new Set() } + let release!: () => void + const settled = new Promise(resolve => { release = resolve }) + parent.children.add(settled) + try { return await this.requestContext.run(admission, run) } + finally { + await this.drainAdmission(admission) + parent.children.delete(settled) + release() + } + } + private async drainAdmission(admission: DatabaseAdmission): Promise { + admission.active = false + await Promise.all(admission.children) + } + private openWaiters = new Set<() => void>() + // Replacement uncertainty survives the temporary exclusive operation. + private admissionClosed = false + closeAdmission(): void { this.admissionClosed = true } + openAdmission(): void { this.admissionClosed = false; this.notifyOpen() } + private notifyOpen(): void { + if (this.isPaused()) return + const waiting = [...this.openWaiters]; this.openWaiters.clear() + for (const resolve of waiting) resolve() + } + isPaused(): boolean { return this.paused || this.admissionClosed } + whenOpen(): Promise { + if (!this.isPaused()) return Promise.resolve() + return new Promise(resolve => { this.openWaiters.add(resolve) }) + } private paused = false private active = new Map void) | undefined>() private idle: (() => void) | undefined async request(run: () => Promise, cancelStream?: () => void): Promise { if (this.paused) throw conflict("Выполняется операция с базой приложения; повторите запрос позже") + if (this.admissionClosed) throw Object.assign(new Error("Схема базы после восстановления не подтверждена; требуется проверка или явное восстановление"), { statusCode: 503 }) const lease = Symbol() this.active.set(lease, cancelStream) - try { return await run() } + const admission: DatabaseAdmission = { active: true, kind: "request", requestAncestry: true, children: new Set() } + try { return await this.requestContext.run(admission, run) } finally { + await this.drainAdmission(admission) this.active.delete(lease) if (!this.active.size) this.idle?.() } @@ -29,11 +74,13 @@ export class DatabaseRequestGate { this.idle = resolve timer = setTimeout(() => reject(conflict("Не удалось дождаться завершения API-запросов; операция с базой отменена")), timeoutMs) }) - return await run() + const admission: DatabaseAdmission = { active: true, kind: "exclusive", requestAncestry: this.requestContext.getStore()?.requestAncestry === true, children: new Set() } + try { return await this.requestContext.run(admission, run) } finally { await this.drainAdmission(admission) } } finally { if (timer) clearTimeout(timer) this.idle = undefined this.paused = false + this.notifyOpen() } } } diff --git a/backend/src/services/database-restore-audit.test.ts b/backend/src/services/database-restore-audit.test.ts new file mode 100644 index 00000000..383cd4f3 --- /dev/null +++ b/backend/src/services/database-restore-audit.test.ts @@ -0,0 +1,111 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import {mkdtemp,mkdir,writeFile,readFile,rm} from 'node:fs/promises' +import path from 'node:path' +import {fileURLToPath} from 'node:url' +import {randomUUID} from 'node:crypto' +import {EventEmitter} from 'node:events' +import type {Pool} from 'pg' +import {createRequire} from 'node:module' +import {runInNewContext} from 'node:vm' +import {reconcileRestoreReceiptAudit,inspectRestoreReceiptAudit,reconcileDatabaseRestoreAudits} from './database-restore-audit.js' +const here=path.dirname(fileURLToPath(import.meta.url)),backend=path.resolve(here,'../..'),temp=path.resolve(backend,'../tmp'),require=createRequire(import.meta.url),ts=require('typescript') +async function own(work:(root:string,operation:string,receipt:Record)=>Promise,old=false){ + await mkdir(temp,{recursive:true});const root=await mkdtemp(path.join(temp,'owned-restore-audit-')) + try{ + const operation=randomUUID(),archive=randomUUID(),core=randomUUID(),stamp=new Date(Date.now()-(old?181*86400000:1000)).toISOString() + const receipt={operationId:operation,archiveRef:archive,state:'applied',startedAt:stamp,automaticRetry:false,finishedAt:stamp,accessRevoked:true,warnings:[]} + await writeFile(path.join(root,'access-generation'),Buffer.alloc(32,11));await mkdir(path.join(root,'restore-vault')) + await writeFile(path.join(root,'database-maintenance.jsonl'),['intent','applied'].map(state=>JSON.stringify({operationId:core,state,at:stamp})).join('\n')+'\n') + await writeFile(path.join(root,'restore-vault',archive+'.used'),JSON.stringify({operationId:operation,at:stamp})) + await writeFile(path.join(root,'restore-vault',operation+'.receipt.json'),JSON.stringify(receipt)) + await work(root,operation,receipt) + }finally{assert(path.dirname(root)===temp&&path.basename(root).startsWith('owned-restore-audit-'));await rm(root,{recursive:true,force:true})} +} +type Row={actor_id:string;connection_id:null;action:string;payload:unknown;created_at?:string} +function model(){ + const rows:Row[]=[],queries:string[]=[],releases:boolean[]=[];let lostCommit=false,insertFailure=false,releaseFailure=false,offFailure=false,inserts=0 + const pool={connect:async()=>{ + let pending:Row[]=[] + const client=new EventEmitter(),originalOff=client.off.bind(client) + return Object.assign(client,{off:(name:string,listener:()=>void)=>{if(offFailure)throw Error('Synthetic listener cleanup failure');return originalOff(name,listener)},release:(bad:boolean)=>{releases.push(bad);if(releaseFailure)throw Error('Synthetic release failure')},query:async(config:{text:string;values?:unknown[]})=>{ + const text=config.text,values=config.values??[];queries.push(text) + if(text.startsWith('SELECT audit_retention'))return {rows:[{days:180,now:String(Date.now())}],command:'SELECT'} + if(text.startsWith('SELECT actor_id'))return {rows:rows.filter(row=>(row.payload as {operationId:string}).operationId===values[1]).slice(0,2),command:'SELECT'} + if(text.startsWith('INSERT')){if(insertFailure)throw Error('Synthetic audit unavailable');inserts++;pending.push({actor_id:String(values[0]),action:String(values[1]),connection_id:null,payload:JSON.parse(String(values[2])),created_at:String(values[3])});return {rows:[],command:'INSERT'}} + if(text==='COMMIT'){rows.push(...pending);pending=[];if(lostCommit){lostCommit=false;throw Error('Synthetic COMMIT delivery loss')}return {rows:[],command:'COMMIT'}} + if(text==='ROLLBACK')pending=[] + return {rows:[],command:text.split(' ')[0]} + }}) + }} as unknown as Pick + return {pool,rows,queries,releases,inserts:()=>inserts,loseCommit:()=>{lostCommit=true},failInsert:()=>{insertFailure=true},failRelease:()=>{releaseFailure=true},failOff:()=>{offFailure=true}} +} +test('live and startup audit writers converge through read-first without changing external receipt',()=>own(async(root,id,receipt)=>{ + const m=model(),file=path.join(root,'restore-vault',id+'.receipt.json'),before=await readFile(file) + const live=await reconcileRestoreReceiptAudit(m.pool,root,id,'actual-owner');assert.equal(live.status,'imported');assert.equal(live.originalActorKnown,true) + assert.equal(m.rows[0].action,'database.restore.receipt');assert.deepEqual(m.rows[0].payload,receipt);assert.equal(m.rows[0].actor_id,'actual-owner') + await reconcileDatabaseRestoreAudits(m.pool,root);assert.equal(m.inserts(),1) + const status=await inspectRestoreReceiptAudit(m.pool,root,id);assert.equal(status.status,'already-recorded');assert.equal(m.inserts(),1) + assert(m.queries.some(q=>q==='BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY'));assert(m.queries.every(q=>!/^CREATE|^ALTER|^DROP|pg_restore|psql/.test(q))) + assert.deepEqual(await readFile(file),before) +})) +test('legacy missing audit imports exact receipt evidence under system importer without inventing human',()=>own(async(root,id)=>{ + const m=model();await reconcileDatabaseRestoreAudits(m.pool,root) + assert.equal(m.rows.length,1);assert.equal(m.rows[0].actor_id,'database-maintenance-reconciler');assert.equal(m.rows[0].action,'database.restore.receipt.recovered') + assert.equal((m.rows[0].payload as {originalActorKnown:boolean}).originalActorKnown,false) + assert.equal((await reconcileRestoreReceiptAudit(m.pool,root,id,'actual-owner')).status,'already-recorded');assert.equal(m.inserts(),1) +})) +test('lost audit COMMIT destroys owner and reconciles observed existing row without another insert',()=>own(async(root,id)=>{ + const m=model();m.loseCommit() + assert.equal((await reconcileRestoreReceiptAudit(m.pool,root,id)).status,'unconfirmed');assert.equal(m.releases[0],true) + assert.equal(m.queries.filter(value=>value==='ROLLBACK').length,0,'Uncertain COMMIT owner must be destroyed, not rolled back or reused') + assert.equal((await reconcileRestoreReceiptAudit(m.pool,root,id)).status,'already-recorded');assert.equal(m.inserts(),1) +})) +test('conflicting or duplicate audit evidence is never treated as imported',()=>own(async(root,id)=>{ + const m=model();m.rows.push({actor_id:'foreign',connection_id:null,action:'database.restore.receipt',payload:{operationId:id,foreign:true}}) + assert.equal((await reconcileRestoreReceiptAudit(m.pool,root,id)).status,'conflict');assert.equal(m.inserts(),0) + await assert.rejects(reconcileDatabaseRestoreAudits(m.pool,root),error=>error instanceof Error&&'code'in error&&error.code==='DATABASE_RESTORE_AUDIT_PENDING') +})) +test('audit retention is not reset by receipt reimport',()=>own(async(root,id)=>{ + const m=model();assert.equal((await reconcileRestoreReceiptAudit(m.pool,root,id)).status,'outside-retention');assert.equal(m.inserts(),0) +},true)) +test('audit outage remains pending and never overwrites fsynced applied receipt',()=>own(async(root,id)=>{ + const m=model();m.failInsert();const file=path.join(root,'restore-vault',id+'.receipt.json'),before=await readFile(file) + assert.equal((await reconcileRestoreReceiptAudit(m.pool,root,id)).status,'pending');assert.deepEqual(await readFile(file),before) +})) +for(const fault of ['release','listener'])test('confirmed audit COMMIT survives '+fault+' cleanup failure without relabeling restore',()=>own(async(root,id)=>{ + const m=model();if(fault==='release')m.failRelease();else m.failOff() + const file=path.join(root,'restore-vault',id+'.receipt.json'),before=await readFile(file) + const result=await reconcileRestoreReceiptAudit(m.pool,root,id,'actual-owner') + assert.equal(result.status,'imported');assert.equal(result.cleanupUnconfirmed,true);assert.equal(m.rows.length,1) + assert.equal(m.releases.length,1,'Listener cleanup failure must not bypass actual owner release') + assert.deepEqual(await readFile(file),before) +})) +for(const legacyImport of [false,true])test('actual bootstrap audit follows migrations and legacy import='+legacyImport+' before business startup',()=>own(async(root)=>{ + const source=await readFile(path.join(backend,'src/db/bootstrap.ts'),'utf8'),events:string[]=[],exports:Record={} + const query=async()=>{events.push('sql');return {rows:[{sqlite_imported_at:'done',released:true}]}},client={query} + const modules:Record={ + './index.js':{pool:{query,connect:async()=>client}},'./migrate.js':{applySqlMigrations:async()=>events.push('migrations')}, + './operation-lock.js':{withSystemDatabaseOperationLock:async(_connect:unknown,run:(owner:typeof client,discard:()=>void)=>Promise)=>run(client,()=>{})}, + './partitions.js':{dropExpiredPartitions:async()=>events.push('drop'),ensurePartitionsAround:async()=>events.push('partitions')}, + './sqlite-import.js':{shouldImportSqlite:async()=>legacyImport,sqliteFileLooksPresent:()=>false,importSqliteToPostgres:async()=>{events.push('import');return {durationMs:0,tables:{}}}}, + '../config.js':{env:{DATABASE_PATH:'synthetic'}},'../modules/mcp/security.js':{mcpStateDirectory:()=>root}, + '../services/database-maintenance-startup.js':{assertDatabaseMaintenanceStartup:async()=>events.push('guard')}, + '../services/database-restore-audit.js':{reconcileDatabaseRestoreAudits:async()=>{events.push('audit');throw Error('Pending audit import')}}, + } + runInNewContext(ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,{exports,require:(name:string)=>{assert(name in modules);return modules[name]},console}) + await assert.rejects((exports.initDatabase as ()=>Promise)(),/Pending audit import/) + assert.equal(events[0],'guard');assert.equal(events.at(-1),'audit');assert(events.indexOf('migrations'){ + const source=await readFile(path.join(backend,'src/modules/mcp/database-restore.ts'),'utf8'),tree=ts.createSourceFile('restore.ts',source,ts.ScriptTarget.ES2022,true) + let callAncestors:unknown[]=[] + function visit(node:{kind:number;expression?:{getText:()=>string};getChildren:()=>unknown[]},ancestors:unknown[]=[]){ + if(node.kind===ts.SyntaxKind.CallExpression&&node.expression?.getText()==='reconcileRestoreReceiptAudit')callAncestors=ancestors + for(const child of node.getChildren())visit(child as typeof node,[...ancestors,node]) + } + visit(tree) + const tries=callAncestors.filter((node:unknown)=>(node as {kind:number}).kind===ts.SyntaxKind.TryStatement) as {getText:()=>string}[] + assert.equal(tries.length,1);assert(!tries[0].getText().includes('state: attempted ? "unknown"')) + assert(source.includes('restoreActorId=connection.owner_id')) +}) diff --git a/backend/src/services/database-restore-audit.ts b/backend/src/services/database-restore-audit.ts new file mode 100644 index 00000000..9ab173bb --- /dev/null +++ b/backend/src/services/database-restore-audit.ts @@ -0,0 +1,80 @@ +/** Specific external receipt -> audit import. Never dispatches restore or + * changes journal/generation/receipt. Both live and startup writers use it. */ +import type {Pool,PoolClient,QueryConfig,QueryResultRow} from 'pg' +import {isDeepStrictEqual} from 'node:util' +import {env} from '../config.js' +import {readAppliedRestoreAuditSources,type AppliedRestoreAuditSource} from './database-maintenance-startup.js' + +const LIVE='database.restore.receipt',RECOVERED='database.restore.receipt.recovered',IMPORTER='database-maintenance-reconciler' +export type RestoreAuditStatus='imported'|'already-recorded'|'missing'|'outside-retention'|'pending'|'unconfirmed'|'conflict' +export interface RestoreAuditInspection {readonly operationId:string;readonly receiptSha256:string;readonly status:RestoreAuditStatus; + /** Recorded audit actor or live validated owner; not cryptographic provenance. */ + readonly originalActorKnown:boolean;readonly cleanupUnconfirmed:boolean;readonly automaticRestore:false} +const boundedQuery=(text:string,values:unknown[]=[]):QueryConfig&{query_timeout:number}=>({text,values,query_timeout:10000}) + +type AuditRow=QueryResultRow&{actor_id:string;connection_id:string|null;action:string;payload:unknown} +function recoveryPayload(source:AppliedRestoreAuditSource){return {operationId:source.operationId,externalReceiptSha256:source.receiptSha256,originalActorKnown:false,receipt:source.receipt}} +function status(source:AppliedRestoreAuditSource,value:RestoreAuditStatus,known=false):RestoreAuditInspection{return Object.freeze({operationId:source.operationId,receiptSha256:source.receiptSha256,status:value,originalActorKnown:known,cleanupUnconfirmed:false,automaticRestore:false})} + +async function one(pool:Pick,source:AppliedRestoreAuditSource,write:boolean,actorId?:string):Promise{ + let client:PoolClient|undefined,begun=false,commitSent=false,committed=false,bad=false,faulted=false,cleanupUnconfirmed=false,result:RestoreAuditInspection + const fault=()=>{faulted=true} + const check=()=>{if(faulted)throw Error('Audit owner unavailable')} + try{ + result=await(async()=>{ + client=await pool.connect();client.on('error',fault);client.on('end',fault) + const query=async(text:string,values:unknown[]=[])=>{check();const result=await client!.query(boundedQuery(text,values));check();return result} + await query(write?'BEGIN ISOLATION LEVEL READ COMMITTED READ WRITE':'BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY');begun=true + await query("SET LOCAL statement_timeout='10s'");await query("SET LOCAL lock_timeout='5s'") + await query('SELECT pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended($1,0))',['mcp-restore-audit:'+source.operationId]) + const settings=await query<{days:number;now:string}>('SELECT audit_retention_days AS days,(EXTRACT(EPOCH FROM pg_catalog.clock_timestamp())*1000)::bigint::text AS now FROM public.mcp_settings WHERE id=1 LIMIT 2') + const config=settings.rows[0],finished=Date.parse(source.finishedAt) + if(settings.rows.length!==1||!config||!Number.isInteger(config.days)||config.days<30||config.days>3650||!/^\d{10,16}$/.test(config.now)||!Number.isSafeInteger(Number(config.now))||!Number.isFinite(finished)||finished>Number(config.now)+5000)throw Error('Audit retention/clock unavailable') + if(finished("SELECT actor_id,connection_id,action,payload FROM public.mcp_audit WHERE action=ANY($1::text[]) AND payload->>'operationId'=$2 ORDER BY id LIMIT 2",[[LIVE,RECOVERED],source.operationId]) + if(existing.rows.length>1)throw Object.assign(Error('Audit receipt conflict'),{auditConflict:true}) + if(existing.rows.length){ + const row=existing.rows[0] + const live=row.action===LIVE&&typeof row.actor_id==='string'&&row.actor_id.length>0&&row.connection_id===null&&isDeepStrictEqual(row.payload,source.receipt)&&(!actorId||row.actor_id===actorId) + const recovered=row.action===RECOVERED&&row.actor_id===IMPORTER&&row.connection_id===null&&isDeepStrictEqual(row.payload,recoveryPayload(source)) + if(!live&&!recovered)throw Object.assign(Error('Audit receipt conflict'),{auditConflict:true}) + const end=await query('ROLLBACK');if(end.command!=='ROLLBACK')throw Error('Audit rollback unconfirmed');begun=false + return status(source,'already-recorded',live) + } + if(!write){const end=await query('ROLLBACK');if(end.command!=='ROLLBACK')throw Error('Audit rollback unconfirmed');begun=false;return status(source,'missing')} + if(actorId!==undefined&&(typeof actorId!=='string'||!actorId))throw Error('Audit actor unavailable') + const actor=actorId??IMPORTER,action=actorId?LIVE:RECOVERED,payload=actorId?source.receipt:recoveryPayload(source) + await query('INSERT INTO public.mcp_audit(actor_id,action,connection_id,payload,created_at) VALUES($1,$2,NULL,$3::jsonb,$4::timestamptz)',[actor,action,JSON.stringify(payload),source.finishedAt]) + commitSent=true;const end=await client!.query(boundedQuery('COMMIT'));if(end.command!=='COMMIT')throw Error('Audit COMMIT acknowledgement unavailable');committed=true;begun=false + if(faulted){bad=true;cleanupUnconfirmed=true} + return status(source,'imported',!!actorId) + })() + }catch(error){ + bad=true + result=status(source,commitSent&&!committed?'unconfirmed':error&&typeof error==='object'&&'auditConflict'in error?'conflict':'pending') + }finally{ + if(client){ + if(begun&&!commitSent&&!faulted)try{const end=await client.query(boundedQuery('ROLLBACK'));if(end.command!=='ROLLBACK'){bad=true;cleanupUnconfirmed=true}}catch{bad=true;cleanupUnconfirmed=true} + try{client.off('error',fault)}catch{bad=true;cleanupUnconfirmed=true} + try{client.off('end',fault)}catch{bad=true;cleanupUnconfirmed=true} + try{client.release(bad||faulted||commitSent&&!committed)}catch{cleanupUnconfirmed=true} + } + } + return cleanupUnconfirmed?Object.freeze({...result,cleanupUnconfirmed:true}):result +} +export async function reconcileRestoreReceiptAudit(pool:Pick,directory:string,operationId:string,actorId?:string):Promise{ + const sources=await readAppliedRestoreAuditSources(directory,env.DATABASE_URL),source=sources.find(value=>value.operationId===operationId) + if(!source)throw Error('Confirmed external receipt unavailable') + return one(pool,source,true,actorId) +} +export async function inspectRestoreReceiptAudit(pool:Pick,directory:string,operationId:string):Promise{ + const sources=await readAppliedRestoreAuditSources(directory,env.DATABASE_URL),source=sources.find(value=>value.operationId===operationId) + if(!source)throw Error('Confirmed external receipt unavailable') + return one(pool,source,false) +} +export async function reconcileDatabaseRestoreAudits(pool:Pick,directory:string):Promise{ + for(const source of await readAppliedRestoreAuditSources(directory,env.DATABASE_URL)){ + const result=await one(pool,source,true) + if(result.cleanupUnconfirmed||!['imported','already-recorded','outside-retention'].includes(result.status))throw Object.assign(Error('External restore audit requires reconciliation before managed startup'),{code:'DATABASE_RESTORE_AUDIT_PENDING',auditStatus:result.cleanupUnconfirmed?'cleanup-unconfirmed':result.status}) + } +} diff --git a/backend/src/services/database-restore-checkpoint-claim.ts b/backend/src/services/database-restore-checkpoint-claim.ts new file mode 100644 index 00000000..72affd96 --- /dev/null +++ b/backend/src/services/database-restore-checkpoint-claim.ts @@ -0,0 +1,88 @@ +import { randomBytes } from "node:crypto" +import { chmod, realpath } from "node:fs/promises" +import { createConnection, createServer, type Server, type Socket } from "node:net" +import path from "node:path" +import { + DatabaseRestoreCheckpointRefused, parseDatabaseRestoreCheckpoint, type RestoreCheckpoint, +} from "./database-restore-checkpoint.js" + +type Pending = Extract +type Consumed = Extract +const maxRequest = 4096 +const deadlineMs = 10_000 + +async function assertLocalAddress(address: string) { + if (process.platform === "win32") { + const prefix = "\\\\.\\pipe\\" + if (address.startsWith(prefix) && /^[a-zA-Z0-9_-]{1,96}$/.test(address.slice(prefix.length))) return + } else if (path.isAbsolute(address) && path.resolve(address) === address && !address.includes("\0") && + Buffer.byteLength(address) < 100 && await realpath(path.dirname(address)) === path.dirname(address)) return + throw new DatabaseRestoreCheckpointRefused() +} + +function readSocketFrame(socket: Socket, maximum: number): Promise { + return new Promise((resolve, reject) => { + let buffer = Buffer.alloc(0) + const fail = () => settle(new DatabaseRestoreCheckpointRefused()) + const data = (chunk: Buffer) => { + if (buffer.length + chunk.length > maximum) return fail() + buffer = Buffer.concat([buffer, chunk]) + const end = buffer.indexOf(0x0a) + if (end < 0) return + if (end !== buffer.length - 1) return fail() + settle() + } + const settle = (error?: Error) => { + socket.off("data", data).off("end", fail).off("close", fail) + if (error) reject(error) + else resolve(buffer) + } + socket.on("data", data).once("end", fail).once("close", fail).once("error", fail) + }) +} + +export async function consumeRestoreCheckpoint(pending: Pending): Promise { + try { + const address = process.env.DATABASE_RESTORE_CHECKPOINT_CLAIM_SOCKET + if (!address) throw new DatabaseRestoreCheckpointRefused() + await assertLocalAddress(address) + const request: Consumed = { ...pending, status: "consumed", claimId: randomBytes(32).toString("hex") } + const socket = createConnection(address) + const timer = setTimeout(() => socket.destroy(new DatabaseRestoreCheckpointRefused()), deadlineMs) + try { + const reply = readSocketFrame(socket, 3) + socket.write(JSON.stringify(request) + "\n") + if (!(await reply).equals(Buffer.from("ok\n"))) throw new DatabaseRestoreCheckpointRefused() + return request + } finally { clearTimeout(timer); socket.destroy() } + } catch { throw new DatabaseRestoreCheckpointRefused() } +} + +/** Run on the operator side, with exclusive write access to the independent directory. */ +export async function listenForRestoreCheckpointClaims(file: string, address: string): Promise { + await assertLocalAddress(address) + const { consumeRestoreCheckpointFile } = await import("./database-restore-checkpoint-operator.js") + const server = createServer(socket => { + const timer = setTimeout(() => socket.destroy(new DatabaseRestoreCheckpointRefused()), deadlineMs) + // One bounded line per connection, including transports that cannot half-close. + void readSocketFrame(socket, maxRequest).then(async raw => { + const request = parseDatabaseRestoreCheckpoint(raw) + if (request.status !== "consumed") throw new DatabaseRestoreCheckpointRefused() + const { claimId, ...permit } = request + await consumeRestoreCheckpointFile(file, { ...permit, status: "pending" }, claimId) + if (!socket.destroyed) socket.end("ok\n") + }).catch(() => socket.destroy()).finally(() => clearTimeout(timer)) + }) + server.maxConnections = 16 + try { + await new Promise((resolve, reject) => { + server.once("error", reject) + server.listen(address, () => { server.off("error", reject); resolve() }) + }) + if (process.platform !== "win32") await chmod(address, 0o660) + return server + } catch (error) { + if (server.listening) server.close() + throw error + } +} diff --git a/backend/src/services/database-restore-checkpoint-operator.test.ts b/backend/src/services/database-restore-checkpoint-operator.test.ts new file mode 100644 index 00000000..9a7149e1 --- /dev/null +++ b/backend/src/services/database-restore-checkpoint-operator.test.ts @@ -0,0 +1,122 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import { lstat, mkdtemp, readFile, readdir, realpath, rm, writeFile } from "node:fs/promises" +import os from "node:os" +import path from "node:path" +import test from "node:test" +import { + consumeRestoreCheckpointFile, publishSealedRestoreCheckpointFile, stagePendingRestoreCheckpointFile, +} from "./database-restore-checkpoint-operator.js" +import { parseDatabaseRestoreCheckpoint, restoreWriterHeadIsCurrent, type RestoreCheckpoint } from "./database-restore-checkpoint.js" + +const checkpoint: Extract = { + format: "database-restore-checkpoint-v1", status: "pending", head: null, + runtimeTargetSha256: "a".repeat(64), activationSha256: null, + operationId: "00000000-0000-0000-0000-000000000001", archiveSha256: "b".repeat(64), dataSha256: "c".repeat(64), +} +const claim = "d".repeat(64) +const refused = { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" } + +test("checkpoint staging requires the exact sealed file and can succeed only once", async (t) => { + const directory = await mkdtemp(path.join(await realpath(os.tmpdir()), "restore-checkpoint-")) + t.after(() => rm(directory, { recursive: true })) + const file = path.join(directory, "checkpoint.json") + const { operationId, archiveSha256, dataSha256, ...baseline } = checkpoint + const sealed = { ...baseline, status: "sealed" as const, head: { bytes: 50, sha256: claim } } + const permit = { operationId, archiveSha256, dataSha256 } + const bytes = JSON.stringify(sealed) + const sha256 = createHash("sha256").update(bytes).digest("hex") + await writeFile(file, bytes + "\n") + await assert.rejects(stagePendingRestoreCheckpointFile(file, sealed, permit, sha256), refused) + assert.equal(await readFile(file, "utf8"), bytes + "\n") + await writeFile(file, bytes) + await assert.rejects(stagePendingRestoreCheckpointFile(file, { ...sealed, head: null }, permit, sha256), refused) + await assert.rejects(stagePendingRestoreCheckpointFile(file, sealed, { ...permit, dataSha256: "invalid" }, sha256), refused) + + const permits = [permit, { ...permit, operationId: "00000000-0000-0000-0000-000000000002" }] + const outcomes = await Promise.allSettled(permits.map((one) => stagePendingRestoreCheckpointFile(file, sealed, one, sha256))) + assert.equal(outcomes.filter((outcome) => outcome.status === "fulfilled").length, 1) + const winner = permits[outcomes.findIndex((outcome) => outcome.status === "fulfilled")] + await assert.rejects(stagePendingRestoreCheckpointFile(file, sealed, permit, sha256), refused) + assert.deepEqual(parseDatabaseRestoreCheckpoint(await readFile(file)), { ...sealed, status: "pending", ...winner }) + assert.deepEqual(await readdir(directory), ["checkpoint.json"]) +}) + +test("checkpoint consumption refuses evidence conflicts and can succeed only once", async (t) => { + const directory = await mkdtemp(path.join(await realpath(os.tmpdir()), "restore-checkpoint-")) + t.after(() => rm(directory, { recursive: true })) + const file = path.join(directory, "checkpoint.json") + const original = JSON.stringify(checkpoint) + await writeFile(file, original, { mode: 0o640 }) + const before = await lstat(file) + await assert.rejects(consumeRestoreCheckpointFile(file, { ...checkpoint, archiveSha256: "e".repeat(64) }, claim), refused) + + for (const suffix of [".lock", ".pending"]) { + const residue = `${file}${suffix}` + await writeFile(residue, "prior attempt", { flag: "wx" }) + await assert.rejects(consumeRestoreCheckpointFile(file, checkpoint, claim), refused) + assert.equal(await readFile(residue, "utf8"), "prior attempt") + assert.equal(await readFile(file, "utf8"), original) + await rm(residue) + } + + const outcomes = await Promise.allSettled([claim, "f".repeat(64)].map((id) => consumeRestoreCheckpointFile(file, checkpoint, id))) + assert.equal(outcomes.filter((outcome) => outcome.status === "fulfilled").length, 1) + const winner = outcomes[0].status === "fulfilled" ? claim : "f".repeat(64) + const consumed = { ...checkpoint, status: "consumed", claimId: winner } + assert.deepEqual(parseDatabaseRestoreCheckpoint(await readFile(file)), consumed) + if (process.platform !== "win32") { + const after = await lstat(file) + assert.equal(after.mode & 0o777, before.mode & 0o777) + assert.equal(after.uid, before.uid) + assert.equal(after.gid, before.gid) + } + await assert.rejects(consumeRestoreCheckpointFile(file, checkpoint, winner), refused) + assert.deepEqual(parseDatabaseRestoreCheckpoint(await readFile(file)), consumed) + assert.deepEqual(await readdir(directory), ["checkpoint.json"]) +}) + +test("checkpoint sealing requires the exact consumed snapshot and one advancing journal head", async (t) => { + const directory = await mkdtemp(path.join(await realpath(os.tmpdir()), "restore-checkpoint-")) + t.after(() => rm(directory, { recursive: true })) + const file = path.join(directory, "checkpoint.json") + const consumed = { + ...checkpoint, status: "consumed" as const, claimId: claim, activationSha256: "e".repeat(64), + head: { bytes: 50, sha256: "f".repeat(64) }, + } + const appliedHeads = [{ bytes: 100, sha256: "1".repeat(64) }, { bytes: 110, sha256: "2".repeat(64) }] + await writeFile(file, JSON.stringify(consumed)) + await assert.rejects(publishSealedRestoreCheckpointFile(file, consumed, consumed.head), refused) + await assert.rejects(publishSealedRestoreCheckpointFile(file, { ...consumed, claimId: "a".repeat(64) }, appliedHeads[0]), refused) + assert.deepEqual(parseDatabaseRestoreCheckpoint(await readFile(file)), consumed) + + const outcomes = await Promise.allSettled(appliedHeads.map((head) => publishSealedRestoreCheckpointFile(file, consumed, head))) + assert.equal(outcomes.filter((outcome) => outcome.status === "fulfilled").length, 1) + const sealed = { + format: consumed.format, status: "sealed", runtimeTargetSha256: consumed.runtimeTargetSha256, + activationSha256: consumed.activationSha256, head: appliedHeads[outcomes.findIndex((outcome) => outcome.status === "fulfilled")], + } + await assert.rejects(publishSealedRestoreCheckpointFile(file, consumed, appliedHeads[0]), refused) + assert.deepEqual(parseDatabaseRestoreCheckpoint(await readFile(file)), sealed) + assert.deepEqual(await readdir(directory), ["checkpoint.json"]) +}) + +test("protected checkpoint requires a newer writer epoch and rejects rollback after sealing", async (t) => { + const directory = await mkdtemp(path.join(await realpath(os.tmpdir()), "restore-checkpoint-")) + t.after(() => rm(directory, { recursive: true })) + const file = path.join(directory, "checkpoint.json") + const before = { epoch: "7", sha256: "1".repeat(64) } + const after = { epoch: "8", sha256: "2".repeat(64) } + const consumed = { ...checkpoint, status: "consumed" as const, claimId: claim, writerHead: before } + const applied = { bytes: 100, sha256: "3".repeat(64) } + await writeFile(file, JSON.stringify(consumed)) + await assert.rejects(publishSealedRestoreCheckpointFile(file, consumed, applied), refused) + await assert.rejects(publishSealedRestoreCheckpointFile(file, consumed, applied, before), refused) + assert.deepEqual(parseDatabaseRestoreCheckpoint(await readFile(file)), consumed) + await publishSealedRestoreCheckpointFile(file, consumed, applied, after) + const sealed = parseDatabaseRestoreCheckpoint(await readFile(file)) + assert.deepEqual(sealed.writerHead, after) + assert.ok(restoreWriterHeadIsCurrent(sealed.writerHead, after)) + assert.ok(!restoreWriterHeadIsCurrent(sealed.writerHead, before)) + assert.ok(!restoreWriterHeadIsCurrent(sealed.writerHead, { ...after, sha256: before.sha256 })) +}) diff --git a/backend/src/services/database-restore-checkpoint-operator.ts b/backend/src/services/database-restore-checkpoint-operator.ts new file mode 100644 index 00000000..cb376dd8 --- /dev/null +++ b/backend/src/services/database-restore-checkpoint-operator.ts @@ -0,0 +1,144 @@ +import { constants, type BigIntStats } from "node:fs" +import { lstat, open, realpath, rename, unlink } from "node:fs/promises" +import path from "node:path" +import { isDeepStrictEqual } from "node:util" +import type { RestoreJournalHead } from "../db/database-restore-quarantine.js" +import { + DatabaseRestoreCheckpointRefused, parseDatabaseRestoreCheckpoint, readRestoreCheckpointFile, + sameRestoreCheckpointFile, type RestoreCheckpoint, type RestoreWriterHead, +} from "./database-restore-checkpoint.js" + +const createExclusive = constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW ?? 0) + +function sameEntry(left: BigIntStats, right: BigIntStats): boolean { + return left.dev === right.dev && left.ino === right.ino +} + +async function requireOwnedFile(file: string, identity: BigIntStats): Promise { + const current = await lstat(file, { bigint: true }) + if (!current.isFile() || current.nlink !== 1n || !sameEntry(current, identity)) throw new DatabaseRestoreCheckpointRefused() +} + +export async function stagePendingRestoreCheckpointFile( + file: string, expected: Extract, + permit: { operationId: string; archiveSha256: string; dataSha256: string; writerHead?: RestoreWriterHead }, expectedFileSha256: string, +): Promise { + if (expected.status !== "sealed" || !expectedFileSha256) throw new DatabaseRestoreCheckpointRefused() + const { operationId, archiveSha256, dataSha256 } = permit + const writerHead = permit.writerHead && { epoch: permit.writerHead.epoch, sha256: permit.writerHead.sha256 } + await replaceRestoreCheckpointFile(file, expected, { + ...expected, status: "pending", operationId, archiveSha256, dataSha256, writerHead, + }, expectedFileSha256) +} + +/** Consumes the operator-owned pending checkpoint before the restore worker can begin. */ +export async function consumeRestoreCheckpointFile( + file: string, expected: Extract, claimId: string, +): Promise { + if (expected.status !== "pending") throw new DatabaseRestoreCheckpointRefused() + await replaceRestoreCheckpointFile(file, expected, { ...expected, status: "consumed", claimId }) +} + +export async function publishSealedRestoreCheckpointFile( + file: string, expected: Extract, appliedHead: RestoreJournalHead, writerHead?: RestoreWriterHead, +): Promise { + if (expected.status !== "consumed" || !appliedHead || appliedHead.bytes <= (expected.head?.bytes ?? 0)) { + throw new DatabaseRestoreCheckpointRefused() + } + if (expected.writerHead ? !writerHead || BigInt(writerHead.epoch) <= BigInt(expected.writerHead.epoch) : writerHead !== undefined) { + throw new DatabaseRestoreCheckpointRefused() + } + const { format, runtimeTargetSha256, activationSha256 } = expected + await replaceRestoreCheckpointFile(file, expected, { + format, status: "sealed", runtimeTargetSha256, activationSha256, head: appliedHead, + writerHead: writerHead && { epoch: writerHead.epoch, sha256: writerHead.sha256 }, + }) +} + +async function replaceRestoreCheckpointFile( + file: string, expected: RestoreCheckpoint, next: RestoreCheckpoint, expectedFileSha256?: string, +): Promise { + const parent = path.dirname(file) + const lockFile = `${file}.lock` + const pendingFile = `${file}.pending` + try { + if (!path.isAbsolute(file) || path.resolve(file) !== file) throw new DatabaseRestoreCheckpointRefused() + const canonicalParent = await realpath(parent) + const windows = process.platform === "win32" + if ((windows ? canonicalParent.toLowerCase() !== parent.toLowerCase() : canonicalParent !== parent)) { + throw new DatabaseRestoreCheckpointRefused() + } + const parentIdentity = await lstat(parent, { bigint: true }) + if (!parentIdentity.isDirectory()) throw new DatabaseRestoreCheckpointRefused() + const bytes = Buffer.from(JSON.stringify(next)) + parseDatabaseRestoreCheckpoint(bytes) + + // shortcut: Windows test runs lack directory fsync, use POSIX for power-loss durability. + const directory = windows ? undefined : await open(parent, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW) + const requireParent = async () => { + const current = await lstat(parent, { bigint: true }) + if (!current.isDirectory() || !sameEntry(current, parentIdentity)) throw new DatabaseRestoreCheckpointRefused() + } + try { + if (directory && !sameEntry(await directory.stat({ bigint: true }), parentIdentity)) throw new DatabaseRestoreCheckpointRefused() + await requireParent() + const lock = await open(lockFile, createExclusive, 0o600) + let preserveSidecars = false + let lockIdentity: BigIntStats | undefined + let pendingIdentity: BigIntStats | undefined + try { + lockIdentity = await lock.stat({ bigint: true }) + await lock.sync() + await directory?.sync() + const before = await readRestoreCheckpointFile(file) + if (!isDeepStrictEqual(before.checkpoint, expected) || + (expectedFileSha256 !== undefined && before.sha256 !== expectedFileSha256)) { + throw new DatabaseRestoreCheckpointRefused() + } + + const pending = await open(pendingFile, createExclusive, 0o600) + try { + pendingIdentity = await pending.stat({ bigint: true }) + await pending.writeFile(bytes) + if (!windows) { + const { uid, gid, mode } = before.identity + if (pendingIdentity.uid !== uid || pendingIdentity.gid !== gid) await pending.chown(Number(uid), Number(gid)) + await pending.chmod(Number(mode & 0o777n)) + } + await pending.sync() + pendingIdentity = await pending.stat({ bigint: true }) + } finally { await pending.close() } + + await requireParent() + await requireOwnedFile(lockFile, lockIdentity) + if (!sameRestoreCheckpointFile(pendingIdentity, await lstat(pendingFile, { bigint: true }))) { + throw new DatabaseRestoreCheckpointRefused() + } + const current = await readRestoreCheckpointFile(file) + if (!sameRestoreCheckpointFile(before.identity, current.identity) || !isDeepStrictEqual(current.checkpoint, expected) || + (expectedFileSha256 !== undefined && current.sha256 !== expectedFileSha256)) { + throw new DatabaseRestoreCheckpointRefused() + } + + // A failed rename or directory sync leaves the operator lease as an unresolved attempt. + preserveSidecars = true + await rename(pendingFile, file) + pendingIdentity = undefined + await directory?.sync() + preserveSidecars = false + } finally { + await lock.close() + if (!preserveSidecars && lockIdentity) { + await requireParent() + await requireOwnedFile(lockFile, lockIdentity) + if (pendingIdentity) { + await requireOwnedFile(pendingFile, pendingIdentity) + await unlink(pendingFile) + } + await unlink(lockFile) + await directory?.sync() + } + } + } finally { await directory?.close() } + } catch { throw new DatabaseRestoreCheckpointRefused() } +} diff --git a/backend/src/services/database-restore-checkpoint.ts b/backend/src/services/database-restore-checkpoint.ts new file mode 100644 index 00000000..3d194411 --- /dev/null +++ b/backend/src/services/database-restore-checkpoint.ts @@ -0,0 +1,117 @@ +import { createHash } from "node:crypto" +import { constants, type BigIntStats } from "node:fs" +import { lstat, open, realpath } from "node:fs/promises" +import path from "node:path" +import { z } from "zod" +import type { RestoreJournalHead } from "../db/database-restore-quarantine.js" +import { epoch } from "../db/application-writer-profile-format.js" +import { parseMaintenanceJSON, runtimeDatabaseTargetSha256 } from "./database-maintenance-resolution.js" + +const MAX_CHECKPOINT_BYTES = 4096 +const digest = z.string().length(64).regex(/^[a-f0-9]{64}$/) +const head = z.object({ bytes: z.number().int().min(1).max(8_388_608), sha256: digest }).strict().nullable() +const writerHead = z.object({ epoch: z.string().refine(value => { try { return epoch(value) === value } catch { return false } }), sha256: digest }).strict() +const fields = { format: z.literal("database-restore-checkpoint-v1"), runtimeTargetSha256: digest, activationSha256: digest.nullable(), head, + writerHead: writerHead.optional() } +const permit = { + ...fields, + operationId: z.string().length(36).regex(/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/), + archiveSha256: digest, dataSha256: digest, +} + +const checkpointSchema = z.discriminatedUnion("status", [ + z.object({ ...fields, status: z.literal("sealed") }).strict(), + z.object({ ...permit, status: z.literal("pending") }).strict(), + z.object({ ...permit, status: z.literal("consumed"), claimId: digest }).strict(), +]) + +export type RestoreCheckpoint = z.infer +export type RestoreWriterHead = z.infer + +export function restoreWriterHeadIsCurrent(expected: RestoreWriterHead | undefined, observed: RestoreWriterHead | undefined): boolean { + if (!expected || !observed) return expected === observed + return BigInt(observed.epoch) > BigInt(expected.epoch) || + (observed.epoch === expected.epoch && observed.sha256 === expected.sha256) +} + +export class DatabaseRestoreCheckpointRefused extends Error { + readonly code = "DATABASE_RESTORE_CHECKPOINT_REFUSED" + readonly statusCode = 503 + constructor() { super("Контрольная точка восстановления PostgreSQL не подтверждена") } +} + +const refused = () => new DatabaseRestoreCheckpointRefused() + +export function parseDatabaseRestoreCheckpoint(bytes: Buffer): RestoreCheckpoint { + if (bytes.length > MAX_CHECKPOINT_BYTES) throw refused() + try { return checkpointSchema.parse(parseMaintenanceJSON(bytes)) } + catch { throw refused() } +} + +export function restoreJournalHeadsEqual(left: RestoreJournalHead | null | undefined, right: RestoreJournalHead | null | undefined): boolean { + return left === right || (!!left && !!right && left.bytes === right.bytes && left.sha256 === right.sha256) +} + +const comparablePath = (value: string) => process.platform === "win32" ? value.toLowerCase() : value + +function containsFile(directory: string, file: string): boolean { + const relative = path.relative(comparablePath(directory), comparablePath(file)) + return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)) +} + +export function sameRestoreCheckpointFile(left: BigIntStats, right: BigIntStats): boolean { + return right.isFile() && right.nlink === 1n && left.dev === right.dev && left.ino === right.ino && + left.size === right.size && left.mode === right.mode && left.mtimeNs === right.mtimeNs && left.ctimeNs === right.ctimeNs +} + +export async function readRestoreCheckpointFile(file: string) { + if (!path.isAbsolute(file) || path.resolve(file) !== file || comparablePath(await realpath(file)) !== comparablePath(file)) throw refused() + const identity = await lstat(file, { bigint: true }) + if (!identity.isFile() || identity.nlink !== 1n || identity.size < 1n || identity.size > BigInt(MAX_CHECKPOINT_BYTES)) throw refused() + const handle = await open(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0)) + try { + if (!sameRestoreCheckpointFile(identity, await handle.stat({ bigint: true }))) throw refused() + const bytes = Buffer.alloc(Number(identity.size) + 1) + let size = 0 + while (size < bytes.length) { + const { bytesRead } = await handle.read(bytes, size, bytes.length - size, size) + if (!bytesRead) break + size += bytesRead + } + if (BigInt(size) !== identity.size || !sameRestoreCheckpointFile(identity, await handle.stat({ bigint: true })) || + !sameRestoreCheckpointFile(identity, await lstat(file, { bigint: true })) || comparablePath(await realpath(file)) !== comparablePath(file)) throw refused() + const raw = bytes.subarray(0, size) + return { checkpoint: parseDatabaseRestoreCheckpoint(raw), identity, sha256: createHash("sha256").update(raw).digest("hex") } + } finally { await handle.close() } +} + +async function assertNoCheckpointTransition(file: string) { + for (const suffix of [".lock", ".pending"]) { + try { await lstat(file + suffix) } + catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") continue; throw error } + throw refused() + } +} + +export async function readDatabaseRestoreCheckpointSnapshot(stateDirectory: string, databaseUrl: string) { + const file = process.env.DATABASE_RESTORE_CHECKPOINT_FILE + if (file === undefined) return undefined + try { + if (!path.isAbsolute(file) || path.resolve(file) !== file || containsFile(path.resolve(stateDirectory), file)) throw refused() + const canonicalState = await realpath(stateDirectory).catch((error: NodeJS.ErrnoException) => { + if (error.code !== "ENOENT") throw error + }) + if (canonicalState && containsFile(canonicalState, file)) throw refused() + await assertNoCheckpointTransition(file) + const { checkpoint, identity, sha256 } = await readRestoreCheckpointFile(file) + if (checkpoint.runtimeTargetSha256 !== runtimeDatabaseTargetSha256(databaseUrl) || + checkpoint.activationSha256 !== (process.env.DATABASE_RUNTIME_ACTIVATION_SHA256 ?? null)) throw refused() + await assertNoCheckpointTransition(file) + if (!sameRestoreCheckpointFile(identity, await lstat(file, { bigint: true }))) throw refused() + return { file, checkpoint, sha256 } + } catch { throw refused() } +} + +export async function readDatabaseRestoreCheckpoint(stateDirectory: string, databaseUrl: string): Promise { + return (await readDatabaseRestoreCheckpointSnapshot(stateDirectory, databaseUrl))?.checkpoint +} diff --git a/backend/src/services/database-restore-target.ts b/backend/src/services/database-restore-target.ts new file mode 100644 index 00000000..082ff26c --- /dev/null +++ b/backend/src/services/database-restore-target.ts @@ -0,0 +1,63 @@ +import { randomBytes } from "node:crypto" +import { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK } from "../db/operation-lock.js" + +interface RestoreLockClient { + query(sql: string, params: number[]): Promise<{ rows: Record[] }> +} + +const witnessSql = "SELECT pg_catalog.pg_backend_pid() AS pid, pg_catalog.pg_try_advisory_lock($1::int,$2::int) AS acquired" +const releaseSql = "SELECT pg_catalog.pg_advisory_unlock($1::int,$2::int) AS released" + +export function databaseRestoreOwnerCheckSql(owner: string): string { + if (!/^[a-z_][a-z0-9_]{0,62}$/.test(owner)) throw new Error("Некорректный логин владельца базы") + return `DO $mm_restore_owner$ +BEGIN + IF SESSION_USER <> '${owner}' OR CURRENT_USER <> '${owner}' OR NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_namespace + WHERE nspname='public' AND nspowner=CURRENT_USER::pg_catalog.regrole + ) THEN + RAISE EXCEPTION 'Restore database owner is unconfirmed' USING ERRCODE='55000'; + END IF; +END $mm_restore_owner$;` +} + +export async function withDatabaseRestoreTarget( + client: RestoreLockClient, + discard: () => void, + run: (targetCheckSql: string) => Promise, +): Promise { + const nonce = randomBytes(8) + const keys = [nonce.readInt32BE(0), nonce.readInt32BE(4)] + let acquired = false + try { + const { rows } = await client.query(witnessSql, keys) + if (rows.length !== 1 || rows[0].acquired !== true || typeof rows[0].pid !== "number" || + !Number.isSafeInteger(rows[0].pid) || rows[0].pid < 1 || rows[0].pid > 0x7fffffff) { + throw new Error("Не подтверждено подключение к базе для восстановления") + } + acquired = true + const pid = rows[0].pid + const [high, low] = keys.map(key => key >>> 0) + const proof = `DO $mm_restore_target$ +BEGIN + IF (SELECT count(*) FROM pg_catalog.pg_locks + WHERE locktype='advisory' AND database=(SELECT oid FROM pg_catalog.pg_database WHERE datname=pg_catalog.current_database()) + AND pid=${pid} AND granted AND mode='ExclusiveLock' + AND ((classid=${high}::pg_catalog.oid AND objid=${low}::pg_catalog.oid AND objsubid=2) + OR (classid=0 AND objsubid=1 AND objid IN (${SYSTEM_DATABASE_OPERATION_LOCK},${SYSTEM_DATABASE_REQUEST_LOCK})))) <> 3 THEN + RAISE EXCEPTION 'Restore target or maintenance lease is unconfirmed' USING ERRCODE='55000'; + END IF; +END $mm_restore_target$;` + return await run(proof) + } finally { + if (!acquired) discard() + else { + try { + const { rows } = await client.query(releaseSql, keys) + if (rows.length !== 1 || rows[0].released !== true) discard() + } catch { + discard() + } + } + } +} diff --git a/backend/src/services/detached-database-admission.test.ts b/backend/src/services/detached-database-admission.test.ts new file mode 100644 index 00000000..65131e58 --- /dev/null +++ b/backend/src/services/detached-database-admission.test.ts @@ -0,0 +1,221 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import { AsyncLocalStorage } from "node:async_hooks" +import { EventEmitter } from "node:events" +import * as crypto from "node:crypto" +import { performance } from "node:perf_hooks" +import path from "node:path" +import os from "node:os" +import { z } from "zod" +import ts from "typescript" +import type { PoolClient } from "pg" +import type { McpPrincipal } from "../modules/mcp/store.js" +import { DatabaseRequestGate } from "./database-request-gate.js" +import * as operation from "./system-database-operation.js" +import * as maintenance from "../modules/mcp/database-maintenance.js" + +async function load(file: URL, modules: Record): Promise { + const exports = {} + const source = ts.transpileModule(await readFile(file, "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, esModuleInterop: true } }).outputText + runInNewContext(source, { exports, require: (name: string) => { assert(name in modules, `Unapproved import ${name}`); return modules[name] }, Buffer, Date, Error, Promise, structuredClone, AbortController, setTimeout, clearTimeout, queueMicrotask, console, process: { env: {} } }) + return exports as T +} +function deferred() { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +async function bounded(work: Promise): Promise { + let timer: ReturnType | undefined + try { return await Promise.race([work, new Promise((_, reject) => { timer = setTimeout(() => reject(Error("Owned pure check deadline")), 1500) })]) } + finally { clearTimeout(timer) } +} +const principal: McpPrincipal = { connectionId: "00000000-0000-4000-8000-000000000001", credentialId: "00000000-0000-4000-8000-000000000002", credentialExpiresAt: new Date(Date.now() + 600000).toISOString() } +class McpError extends Error { constructor(readonly code: string, message: string, readonly statusCode = 403) { super(message) } } + +async function originFixture(gate: DatabaseRequestGate) { + let checkouts = 0, sqlCalls = 0, commits = 0 + let hold: (() => Promise) | undefined + const committed = deferred(), events: string[] = [], generation = Buffer.alloc(32, 71) + const cleanupQuery = async (raw: string | { text: string }) => { + assert(gate.hasActiveAdmission(), "Cleanup SQL must own live admission") + const text = typeof raw === "string" ? raw : raw.text; sqlCalls++; events.push(text) + if (text === "COMMIT") { commits++; if (commits === 2) committed.resolve() } + return { command: text === "BEGIN" || text === "COMMIT" ? text : "UPDATE", rows: text.startsWith("UPDATE public.mcp_operations") ? [{ plan_id: "owned-plan" }] : [] } + } + const pool = { connect: async () => { assert(gate.hasActiveAdmission()); checkouts++; await hold?.(); return { query: cleanupQuery, release() {} } } } + const connection = { generation: generation.toString("hex"), policy: { version: 1, expiresAt: new Date(Date.now() + 600000).toISOString() }, owner_valid_until: new Date(Date.now() + 600000) } + const origin = await load(new URL("../modules/mcp/operation-execution-origin.ts", import.meta.url), { + "node:async_hooks": { AsyncLocalStorage }, "node:crypto": crypto, "node:path": path, "node:perf_hooks": { performance }, + "node:fs": { readFileSync: (file: string) => Buffer.alloc(32, file.endsWith("access-generation") ? 71 : 72) }, + "node:fs/promises": { readFile: async () => Buffer.from(generation) }, + "@mmapp/contracts/mcp": { mcpTemporaryGrantSchema: { parse: (value: unknown) => value } }, + "../../db/index.js": { pool }, "../../services/shared-database-gate.js": { sharedDatabaseGate: gate }, + "../../services/config-mutation-lock.js": { assertNoConfigurationLockContext() {} }, "../../services/operation-config-lock-lease.js": { assertNoOperationConfigurationLockContext() {} }, + "./store.js": { validatePrincipal: async () => connection, audit: async (db: { query: typeof cleanupQuery }) => db.query("INSERT INTO inert_audit") }, + "./security.js": { McpError, mcpStateDirectory: () => "/inert-owned-state", fingerprint: () => "inert", decryptSecret: async (value: string) => value, privateFingerprinterExisting: async () => () => "inert-key" }, + "./actions.js": { findActionByOperation: () => ({ operation: { version: 1 } }) }, "./policy.js": { evaluateMcpPolicy: () => ({ decision: "allowed" }) }, "./change-cost.js": { changeMutationCost: () => 1 }, + "./temporary-grant-origin.js": { assertOriginalMcpTemporaryGrant: async () => {} }, + }) + const mint = async () => { + let id = "", planId = "" + const result = (command: string, rows: unknown[]) => ({ command, rows, rowCount: rows.length }) + const client = { query: async (sql: string, args: string[] = []) => { + if (sql.startsWith("SELECT pg_current_xact_id")) return result("SELECT", [{ creating_xid: "123" }]) + if (sql.startsWith("INSERT INTO mcp_operations")) { [id, planId] = args; return result("INSERT", [{ id, plan_id: planId, connection_id: principal.connectionId, state: "pending" }]) } + assert(sql.includes("AS creating_transaction")); return result("SELECT", [{ id, plan_id: planId, connection_id: principal.connectionId, creating_transaction: true, operation_id: "users.create", operation_version: 1, policy_version: 1, generation: connection.generation, expires_at: new Date(Date.now() + 600000), evidence: { sealed: "owned-evidence" }, payload: { sealed: "owned-payload" } }]) + } } + const start = origin.beginMcpOperationAdmission(principal) + return (await origin.insertMcpOperationAdmission(client as unknown as PoolClient, start, crypto.randomUUID(), crypto.randomUUID(), { proof: "inert", expiresAt: null, monotonicEnd: Infinity, admissionExpiresAt: Date.now() + 600000, admissionMonotonicEnd: performance.now() + 600000 })).admission + } + return { origin, mint, events, committed, counts: () => ({ checkouts, sqlCalls, commits }), hold: (work?: () => Promise) => { hold = work } } +} +async function actualBoundedRunner() { + const runtime = await readFile(new URL("../modules/mcp/runtime.ts", import.meta.url), "utf8") + const begin = runtime.indexOf("let activeRunners = 0"), end = runtime.indexOf("async function policyInput", begin) + assert(begin > 0 && end > begin) + // Execute the existing private slot owner directly, not a mirrored semaphore. + const code = ts.transpileModule(runtime.slice(begin, end) + "\nexports.runBounded = runBounded", { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText + const exports = {} as { runBounded(work: () => Promise): Promise } + runInNewContext(code, { exports, Promise }); return exports.runBounded +} + +test("deferred one-shot cleanup owns no bounded runner slot and emits no SQL until repair admission", async () => { + const gate = new DatabaseRequestGate(), f = await originFixture(gate), runBounded = await actualBoundedRunner(), first = await f.mint(), second = await f.mint() + gate.closeAdmission() + await bounded(Promise.all([runBounded(() => f.origin.retireMcpOperationAdmission(first)), runBounded(() => f.origin.retireMcpOperationAdmission(second))])) + assert.deepEqual(f.counts(), { checkouts: 0, sqlCalls: 0, commits: 0 }) + await bounded(runBounded(() => gate.exclusive(async () => { assert.equal(f.counts().sqlCalls, 0); gate.openAdmission() }))) + await bounded(f.committed.promise); assert.equal(f.counts().commits, 2) + const before = f.counts(); await f.origin.retireMcpOperationAdmission(first); await f.origin.retireMcpOperationAdmission(second); assert.deepEqual(f.counts(), before) +}) +test("cleanup already inside admitted work completes while exclusive drain waits, without inversion", async () => { + const gate = new DatabaseRequestGate(), f = await originFixture(gate), cap = await f.mint(), entered = deferred(), release = deferred() + f.hold(async () => { entered.resolve(); await release.promise }) + const request = gate.request(() => f.origin.retireMcpOperationAdmission(cap)); await entered.promise + let replaced = false; const exclusive = gate.exclusive(async () => { assert.equal(f.counts().commits, 1); replaced = true }) + assert.equal(replaced, false); release.resolve(); await bounded(Promise.all([request, exclusive])); assert.equal(replaced, true) +}) +test("temporary exclusive also defers outside cleanup until its callback has completed", async () => { + const gate = new DatabaseRequestGate(), f = await originFixture(gate), cap = await f.mint(), entered = deferred(), release = deferred() + const exclusive = gate.exclusive(async () => { entered.resolve(); await release.promise }); await entered.promise + await bounded(f.origin.retireMcpOperationAdmission(cap)); assert.equal(f.counts().checkouts, 0) + release.resolve(); await exclusive + await bounded(gate.request(async () => { while (f.counts().commits !== 1) await new Promise(resolve => setTimeout(resolve, 0)) })) +}) +test("settled request ancestry cannot grant cleanup admission or be laundered into scheduler bootstrap", async () => { + const gate = new DatabaseRequestGate(), entered = deferred(), release = deferred() + let stale: Promise | undefined + await gate.request(async () => { stale = (async () => { entered.resolve(); await release.promise; assert.equal(gate.hasActiveAdmission(), false); assert.throws(() => gate.assertNoRequestContext(), /request context/); await gate.exclusive(async () => { assert(gate.hasActiveAdmission()); assert.throws(() => gate.assertNoRequestContext(), /request context/) }) })() }) + await entered.promise; release.resolve(); await stale + await gate.exclusive(async () => { assert(gate.hasActiveAdmission()); assert.doesNotThrow(() => gate.assertNoRequestContext()) }) +}) + +async function serviceFixture(gate: DatabaseRequestGate) { + const calls: string[] = [], readiness = operation.createDatabaseSchemaReadiness() + const spawn = (command: string) => { + calls.push(command); const child = new EventEmitter() as EventEmitter & { stdout: EventEmitter; stderr: EventEmitter; stdin: { end(): void } } + child.stdout = new EventEmitter(); child.stderr = new EventEmitter(); child.stdin = { end: () => queueMicrotask(() => child.emit("close", 0)) }; return child + } + const service = await load(new URL("./system-database-backup.ts", import.meta.url), { + "node:crypto": crypto, "node:async_hooks": { AsyncLocalStorage }, "node:fs/promises": { open: async () => ({ writeFile: async () => {}, sync: async () => {}, close: async () => {} }), chmod: async () => {}, mkdir: async () => {}, rm: async () => {}, writeFile: async () => {} }, "node:os": os, "node:path": path, "node:child_process": { spawn }, + pg: { Pool: class { constructor() { assert.fail("Owner pool not configured in this scenario") } } }, + "../modules/events/service/events-service.js": { appendEvent: async () => {} }, + "./database-dump-connection.js": { runPrivateDatabaseDump: async () => Buffer.alloc(0), validatePrivateRestoreConnection() {}, restoreDatabaseToolEnvironment: () => ({}), + withPrivateRestoreService: async (_url: string, run: (service: { dsn: string; env: object }) => Promise) => run({ dsn: "service=owned", env: {} }) }, + "./bounded-database-dump.js": { boundedDatabaseCommand: async (command: string) => { calls.push(command); return Buffer.alloc(0) } }, + "./database-control-archive-boundary.js": { DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG: "--exclude-schema=mmapp_control" }, + "./shared-database-gate.js": { sharedDatabaseGate: gate, databaseSchemaReadiness: readiness }, "./config-mutation-policy.js": { assertDatabaseRestoreAllowed: async () => {} }, "../modules/mcp/security.js": { accessGeneration: async () => "inert", invalidateMcpAccess: async () => {}, mcpStateDirectory: () => "/inert-owned-state" }, "../modules/mcp/database-maintenance.js": maintenance, + "../db/migrate.js": { applySqlMigrations: async (_pool: unknown, proof: string) => { assert.equal(proof, "owned fixture proof"); assert(gate.hasActiveAdmission()); calls.push("migrations") } }, "./system-database-restore-execution.js": { validateSystemDatabaseRestoreArchive() {}, executeSystemDatabaseRestore: async (_buffer: Buffer, _url: string, deps: { run(args: string[]): Promise }) => { await deps.run(["owned.dump"]); return {} } }, + "../config.js": { env: { DATABASE_URL: "inert-owned-database" } }, "../db/index.js": { pool: { connect: () => assert.fail("Native SQL forbidden") } }, + "../db/operation-lock.js": { withSystemDatabaseOperationLock: async (_connect: unknown, work: (client: object, discard: () => void) => Promise) => { assert(gate.hasActiveAdmission()); calls.push("operation-lock"); return work({}, () => assert.fail("Unexpected discard")) } }, + "../db/database-restore-quarantine.js": { databaseRestoreQuarantineEnabled: () => false }, + "./database-restore-target.js": { withDatabaseRestoreTarget: async (_client: unknown, _discard: unknown, work: (proof: string) => Promise) => work("owned fixture proof") }, "./system-database-operation.js": operation, + "./scheduler.js": { stopSchedulerForDatabaseOperation: async () => {}, restartSchedulerAfterDatabaseOperation: async () => { calls.push("scheduler-restart") } }, "./traffic-flow-ripe.js": { pauseRipeBackgroundWork: async () => {}, resumeRipeBackgroundWork() {} }, "./system-database-cache.js": { invalidateSystemDatabaseCaches() {} }, + "./system-database-partition-restore.js": { restorePartitionedDatabase: async (_file: string, _url: string, deps: { beforeTargetReplacement(): void; targetCheckSql: string }) => { assert.equal(deps.targetCheckSql, "owned fixture proof"); assert(gate.hasActiveAdmission()); assert(gate.isPaused()); deps.beforeTargetReplacement(); calls.push("replacement") } }, "./traffic-flow-ingest.js": { stopTrafficFlowListenerForDatabaseOperation: async () => {}, restartTrafficFlowListenerAfterDatabaseOperation: async () => {} }, + }) + return { service, calls } +} +test("terminal helper grants matching inner restore exactly one exclusive admission and revokes stale handoff", async () => { + const gate = new DatabaseRequestGate(), f = await serviceFixture(gate), exclusive = gate.exclusive.bind(gate); let entries = 0 + Object.defineProperty(gate, "exclusive", { value: (work: () => Promise) => { entries++; return exclusive(work) } }) + let late: Promise | undefined; const release = deferred() + await f.service.withSystemDatabaseRestoreAdmission(async () => { await f.service.restoreSystemDatabaseBackup(Buffer.from("PGDMP")); late = release.promise.then(() => f.service.restoreSystemDatabaseBackup(Buffer.from("PGDMP"))) }) + assert.equal(entries, 1); assert(f.calls.includes("replacement")); release.resolve(); await late; assert.equal(entries, 2) + await f.service.withSystemDatabaseBackupAdmission(async () => { await assert.rejects(f.service.restoreSystemDatabaseBackup(Buffer.from("PGDMP")), /уже выполняется/) }) +}) +test("actual terminal first pool checkout and claim are inside exclusive, never during another restore", async () => { + const gate = new DatabaseRequestGate(), f = await serviceFixture(gate); let checkouts = 0, claims = 0 + const terminal = await load(new URL("../modules/mcp/database-restore.ts", import.meta.url), { + "node:crypto": crypto, "node:fs/promises": {}, "node:path": path, zod: { z }, "../../db/index.js": { pool: { connect: async () => { assert(gate.hasActiveAdmission()); assert(gate.isPaused()); checkouts++; return { query: async () => { assert(gate.hasActiveAdmission()); claims++; return { rows: [{ locked: false }] } }, release: () => assert(gate.hasActiveAdmission()) } } } }, + "../../lib/permissions.js": {}, "../../services/system-database-restore-execution.js": {}, "../../services/system-database-backup.js": f.service, "../../services/config-mutation-policy.js": {}, "./security.js": {}, "./store.js": {}, "./state-file-publish.js": {}, "../../services/config-mutation-lock.js": {}, "./temporary-grant-origin.js": {}, "../../services/database-restore-audit.js": {}, + "../../services/partition-write-transaction.js": {}, + }) + const entered = deferred(), release = deferred(), other = gate.exclusive(async () => { entered.resolve(); await release.promise }); await entered.promise + await assert.rejects(terminal.executeMcpDatabaseRestore(crypto.randomUUID(), principal), /уже выполняется/); assert.equal(checkouts, 0) + release.resolve(); await other + await terminal.executeMcpDatabaseRestore(crypto.randomUUID(), principal); assert.equal(checkouts, 1); assert.equal(claims, 1) +}) + + +test("detached cleanup begun in a live parent pins admission after that parent callback returns", async () => { + const gate = new DatabaseRequestGate(), f = await originFixture(gate), cap = await f.mint() + const checkout = deferred(), releaseCheckout = deferred(), parentReturned = deferred() + f.hold(async () => { checkout.resolve(); await releaseCheckout.promise }) + let cleanup!: Promise + const request = gate.request(async () => { + cleanup = f.origin.retireMcpOperationAdmission(cap) + await checkout.promise + parentReturned.resolve() + // Deliberately do not await the detached cleanup. + }) + await parentReturned.promise + let replaced = false + const exclusive = gate.exclusive(async () => { assert.equal(f.counts().commits, 1); replaced = true }) + await new Promise(resolve => setTimeout(resolve, 10)) + assert.equal(replaced, false); assert.equal(f.counts().sqlCalls, 0) + releaseCheckout.resolve() + await bounded(Promise.all([request, cleanup, exclusive])) + assert.equal(replaced, true) +}) + +test("detached matching restore handoff pins exclusive through an already-started inner restore", async () => { + const gate = new DatabaseRequestGate(), f = await serviceFixture(gate), entered = deferred(), release = deferred() + const scoped = gate.withinAdmission.bind(gate) + Object.defineProperty(gate, "withinAdmission", { value: (work: () => Promise) => scoped(async () => { + entered.resolve(); await release.promise; return work() + }) }) + let inner!: Promise + const outer = f.service.withSystemDatabaseRestoreAdmission(async () => { + inner = f.service.restoreSystemDatabaseBackup(Buffer.from("PGDMP")) + await entered.promise + }) + await entered.promise + await new Promise(resolve => setTimeout(resolve, 10)) + assert(gate.isPaused()) + await assert.rejects(gate.exclusive(async () => {}), /уже выполняется/) + assert(!f.calls.includes("operation-lock")); assert(!f.calls.includes("replacement")) + release.resolve(); await bounded(Promise.all([outer, inner])) + assert(f.calls.includes("replacement")); assert.equal(gate.isPaused(), false) +}) + + +test("retained child rejection preserves drain and never grants a stale child admission", async () => { + const gate = new DatabaseRequestGate(), started = deferred(), release = deferred() + let child!: Promise, late!: () => Promise + const request = gate.request(async () => { + child = gate.withinAdmission(async () => { + const inherited = AsyncLocalStorage.snapshot() + late = () => inherited(() => gate.withinAdmission(async () => assert.fail("Stale work cannot run"))) + started.resolve(); await release.promise; throw Error("Expected child rejection") + }) + void child.catch(() => {}) + await started.promise + }) + await started.promise + let replaced = false; const exclusive = gate.exclusive(async () => { replaced = true }) + assert.equal(replaced, false); release.resolve() + await assert.rejects(child, /Expected child rejection/) + await bounded(Promise.all([request, exclusive])) + assert.equal(replaced, true) + // Invoke in the inherited child context, not this outside test context. + await assert.rejects(late(), /no longer active/) +}) diff --git a/backend/src/services/entity-snapshots.ts b/backend/src/services/entity-snapshots.ts index 72a5960a..cea11b5a 100644 --- a/backend/src/services/entity-snapshots.ts +++ b/backend/src/services/entity-snapshots.ts @@ -2,6 +2,8 @@ import { isIpsecManagedComment, isManagedIpsecModeConfig } from "./ipsec-config.js" import { readGreComment, writeGreComment } from "./gre-comment.js" import { isIP } from "node:net" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" export type FirewallFamily = "ip" | "ip6" export type FirewallTable = "filter" | "nat" | "mangle" | "raw" @@ -16,6 +18,7 @@ export type RosWriteOp = | { op: "move"; path: string; body: Record } export interface FirewallSnapshotRule { + native?: Record family: FirewallFamily table: FirewallTable chain: string @@ -39,6 +42,7 @@ export interface FirewallSnapshotRule { } export interface FirewallSnapshotList { + native?: Record family: FirewallFamily list: string address: string @@ -72,6 +76,7 @@ export interface WgSnapshotPeer { name: string disabled: boolean privateKey: string + presharedKeyConfigured?: true clientAddress: string clientDns: string clientEndpoint: string @@ -115,12 +120,13 @@ export interface WgLivePeer { name: string disabled: boolean privateKey: string + presharedKeyConfigured?: true clientAddress: string clientDns: string clientEndpoint: string } -export interface WgLiveAddr extends GreLiveAddr {} +export type WgLiveAddr = GreLiveAddr export interface GreSnapshotTunnel { name: string @@ -131,7 +137,7 @@ export interface GreSnapshotTunnel { remoteInnerIp: string comment: string disabled: boolean - mtu: number + mtu: number | "auto" keepalive: string dscp: string clampTcpMss: boolean @@ -150,7 +156,7 @@ export interface GreLiveIface { remoteAddress: string comment: string disabled: boolean - mtu: number + mtu: number | "auto" keepalive: string dscp: string clampTcpMss: boolean @@ -343,6 +349,7 @@ export function canonicalFirewallSnapshot(input: { addressLists?: Array> }): FirewallSnapshot { const rules = (input.rules ?? []).map((r) => ({ + ...(r.native ? { native: { ...r.native } } : {}), family: r.family === "ip6" ? "ip6" as const : "ip" as const, table: (["filter", "nat", "mangle", "raw"] as const).includes(r.table as FirewallTable) ? (r.table as FirewallTable) @@ -367,6 +374,7 @@ export function canonicalFirewallSnapshot(input: { layer7Proto: str(r.layer7Proto), })) const addressLists = (input.addressLists ?? []).map((e) => ({ + ...(e.native ? { native: { ...e.native } } : {}), family: e.family === "ip6" ? "ip6" as const : "ip" as const, list: str(e.list), address: str(e.address), @@ -388,21 +396,8 @@ export function parseFirewallSnapshot(payload: unknown): FirewallSnapshot { }) } -function firewallRuleKey(r: FirewallSnapshotRule): string { - return [ - r.family, r.table, r.chain, r.action, r.protocol, - r.srcAddress, r.dstAddress, r.srcAddressList, r.dstAddressList, - r.srcPort, r.dstPort, r.inInterface, r.outInterface, r.connectionState, - r.comment, r.disabled ? "1" : "0", r.log ? "1" : "0", r.logPrefix, r.tlsHost, r.layer7Proto, - ].join("\0") -} - -function firewallListKey(e: FirewallSnapshotList): string { - return [e.family, e.list, e.address, e.comment, e.disabled ? "1" : "0", e.timeout].join("\0") -} - -function firewallRuleBody(r: FirewallSnapshotRule): Record { - return compactBody({ +export function firewallRuleBody(r: FirewallSnapshotRule): Record { + const body = { ...r.native, ...compactBody({ chain: r.chain, action: r.action, protocol: r.protocol && r.protocol !== "all" ? r.protocol : undefined, @@ -421,71 +416,31 @@ function firewallRuleBody(r: FirewallSnapshotRule): Record { "log-prefix": r.logPrefix, "tls-host": r.tlsHost, "layer7-protocol": r.layer7Proto, - }) + }) } + if (body.protocol === "all") delete body.protocol + return body } -export function planFirewallRestore( - desiredInput: FirewallSnapshot, - current: { rules: FirewallLiveRule[]; addressLists: FirewallLiveList[] }, -): RosWriteOp[] { - const desired = canonicalFirewallSnapshot(desiredInput) - const ops: RosWriteOp[] = [] - const usedRules = new Set() - const usedLists = new Set() +export function firewallListBody(entry: FirewallSnapshotList): Record { + const body = { ...entry.native, ...compactBody({ list: entry.list, address: entry.address, comment: entry.comment, timeout: entry.timeout, disabled: rosYesNo(entry.disabled) }) } + if (["", "none-static", "0", "0s", "00:00:00"].includes(body.timeout ?? "")) delete body.timeout + return body +} - for (const live of current.rules) { - if (live.dynamic) continue - const key = firewallRuleKey(live) - const stillWanted = desired.rules.some((d) => firewallRuleKey(d) === key) - if (!stillWanted) { - ops.push({ - op: "delete", - path: `${firewallRestPath(live.family, live.table)}/${live.rosId}`, - }) - } else { - usedRules.add(key) - } - } +/** Rebuild the static subset exactly, including multiplicity and per-table order. + * Existing dynamic rows are never removed and precede the rebuilt static rows. */ +export function planExactFirewallRestore(desired: FirewallSnapshot, current: { rules: FirewallLiveRule[]; addressLists: FirewallLiveList[] }): RosWriteOp[] { + return [ + ...current.rules.filter(row => !row.dynamic).map(row => ({ op: "delete" as const, path: `${firewallRestPath(row.family, row.table)}/${row.rosId}` })), + ...current.addressLists.filter(row => !row.dynamic).map(row => ({ op: "delete" as const, path: `${firewallRestPath(row.family, "address-list")}/${row.rosId}` })), + ...desired.addressLists.map(row => ({ op: "put" as const, path: firewallRestPath(row.family, "address-list"), body: firewallListBody(row) })), + ...desired.rules.map(row => ({ op: "put" as const, path: firewallRestPath(row.family, row.table), body: firewallRuleBody(row) })), + ] +} - for (const live of current.addressLists) { - if (live.dynamic) continue - const key = firewallListKey(live) - const stillWanted = desired.addressLists.some((d) => firewallListKey(d) === key) - if (!stillWanted) { - ops.push({ - op: "delete", - path: `${firewallRestPath(live.family, "address-list")}/${live.rosId}`, - }) - } else { - usedLists.add(key) - } - } - - for (const rule of desired.rules) { - if (usedRules.has(firewallRuleKey(rule))) continue - ops.push({ - op: "put", - path: firewallRestPath(rule.family, rule.table), - body: firewallRuleBody(rule), - }) - } - - for (const entry of desired.addressLists) { - if (usedLists.has(firewallListKey(entry))) continue - ops.push({ - op: "put", - path: firewallRestPath(entry.family, "address-list"), - body: compactBody({ - list: entry.list, - address: entry.address, - comment: entry.comment, - timeout: entry.timeout, - disabled: rosYesNo(entry.disabled), - }), - }) - } - - return ops +/** Compatibility entry point; execution must use the staged restore service. */ +export function planFirewallRestore(desiredInput: FirewallSnapshot, current: { rules: FirewallLiveRule[]; addressLists: FirewallLiveList[] }): RosWriteOp[] { + return planExactFirewallRestore(canonicalFirewallSnapshot(desiredInput), current) } function canonicalPeer(p: Partial): WgSnapshotPeer { @@ -505,6 +460,7 @@ function canonicalPeer(p: Partial): WgSnapshotPeer { name: str(p.name), disabled: Boolean(p.disabled), privateKey: str(p.privateKey), + ...(p.presharedKeyConfigured === true ? { presharedKeyConfigured: true as const } : {}), clientAddress: str(p.clientAddress), clientDns: str(p.clientDns), clientEndpoint: str(p.clientEndpoint), @@ -654,7 +610,7 @@ function canonicalIpsecIdentityRaw(i: Partial): IpsecSnap certificate: str(i.certificate), remoteCertificate: str(i.remoteCertificate), matchBy: str(i.matchBy), - secret: str(i.secret), + secret: typeof i.secret === "string" ? i.secret : "", remoteId: str(i.remoteId), modeConfig: str(i.modeConfig), generatePolicy: str(i.generatePolicy), @@ -708,18 +664,27 @@ export function canonicalIpsecSnapshot(input: { } export function parseIpsecSnapshot(payload: unknown): IpsecSnapshot { - if (!payload || typeof payload !== "object" || Array.isArray(payload)) { - return canonicalIpsecSnapshot({}) - } - const o = payload as Record - return canonicalIpsecSnapshot({ - peers: Array.isArray(o.peers) ? o.peers as Array> : [], - identities: Array.isArray(o.identities) ? o.identities as Array> : [], - modeConfigs: Array.isArray(o.modeConfigs) ? o.modeConfigs as Array> : [], - pools: Array.isArray(o.pools) ? o.pools as Array> : [], - policies: Array.isArray(o.policies) ? o.policies as Array> : [], - nat: Array.isArray(o.nat) ? o.nat as Array> : [], - }) + // A damaged or partial revision must never become an empty desired section: + // that would delete the current managed configuration during rollback. + let bytes: number + try { bytes = Buffer.byteLength(JSON.stringify(payload)) } catch { throw new IpsecRestoreConflict("Повреждена ревизия IPsec") } + if (bytes > 1048576) throw new IpsecRestoreConflict("Превышен размер ревизии IPsec") + const text = z.string().max(8192), name = text.min(1).refine(value => value === value.trim()), bool = z.boolean() + const parsed = z.object({ + peers: z.array(z.object({ name, address: text, exchangeMode: text, passive: bool, certificate: text, profile: text, comment: text, disabled: bool }).strict()).max(1000), + identities: z.array(z.object({ peerName: name, authMethod: text, certificate: text, remoteCertificate: text, matchBy: text, secret: text, remoteId: text, modeConfig: text, generatePolicy: text, comment: name, disabled: bool, policyTemplateGroup: name.optional() }).strict()).max(1000), + modeConfigs: z.array(z.object({ name, addressPool: text, address: text, staticDns: text, comment: text }).strict()).max(1000), + pools: z.array(z.object({ name, ranges: text, comment: text }).strict()).max(1000), + policies: z.array(z.object({ srcAddress: text, dstAddress: name, proposal: text, comment: text }).strict()).max(1000), + nat: z.array(z.object({ chain: text, action: text, srcAddress: text, comment: name, outInterface: name.optional(), outInterfaceList: name.optional(), ipsecPolicy: name.optional() }).strict()).max(1000), + }).strict().safeParse(payload) + if (!parsed.success) throw new IpsecRestoreConflict("Неполная или повреждённая схема ревизии IPsec") + const value = parsed.data + for (const rows of [value.peers, value.modeConfigs, value.pools]) if (new Set(rows.map(row => row.name)).size !== rows.length) throw new IpsecRestoreConflict("Неоднозначные имена в ревизии IPsec") + for (const rows of [value.identities, value.nat]) if (new Set(rows.map(row => row.comment)).size !== rows.length) throw new IpsecRestoreConflict("Неоднозначные маркеры в ревизии IPsec") + const canonical = canonicalIpsecSnapshot(value) + if (!isDeepStrictEqual(canonical, value)) throw new IpsecRestoreConflict("Неканонические или неоднозначные поля ревизии IPsec") + return canonical } function ipsecPeerBody(p: IpsecSnapshotPeer): Record { @@ -887,7 +852,7 @@ export function canonicalGreSnapshot(input: { tunnels?: Array> }): GreSnapshot { const tunnels = (input.tunnels ?? []) - .map((t) => ({ + .map((t) => ({ name: str(t.name), localAddress: str(t.localAddress), remoteAddress: str(t.remoteAddress), @@ -906,12 +871,12 @@ export function canonicalGreSnapshot(input: { remoteInnerIp: str(t.remoteInnerIp), comment: str(t.comment), disabled: Boolean(t.disabled), - mtu: num(t.mtu, 1476), + mtu: t.mtu === "auto" ? t.mtu : num(t.mtu, 1476), keepalive: str(t.keepalive) || "0", dscp: str(t.dscp) || "inherit", clampTcpMss: t.clampTcpMss !== false, allowFastPath: t.allowFastPath !== false, - ipsecSecret: str(t.ipsecSecret), + ipsecSecret: typeof t.ipsecSecret === "string" ? t.ipsecSecret : "", })) .filter((t) => t.name) .sort((a, b) => a.name.localeCompare(b.name)) diff --git a/backend/src/services/evobgp-catalog-model.ts b/backend/src/services/evobgp-catalog-model.ts new file mode 100644 index 00000000..6a15fcc6 --- /dev/null +++ b/backend/src/services/evobgp-catalog-model.ts @@ -0,0 +1,97 @@ +import { aggregateCommunityRouteCounts, type EvoCatalogRaw } from "./evobgp-catalog.js" + +export function formatEvoBgpCatalog(catalog: EvoCatalogRaw, serversByCommunityStr: Map, now = new Date()) { + const modules = catalog.modules.items + const modMap = new Map(modules.map((m) => [m.id, m])) + const commItems = catalog.communities.items + const commLabel = new Map() + for (const c of commItems) { + const label = (c.community && c.title) + ? `${c.title} (${c.community})` + : (c.title ?? c.community ?? c.id) + commLabel.set(c.id, label) + } + + const fmtTime = now.toLocaleString("ru-RU") + const fetchedAt = now.toISOString() + + const domains = catalog.domains.items.map((rowItem) => { + const mod = modMap.get(rowItem.module_id) + const cid = rowItem.entry.community_id + return { + id: `${rowItem.module_id}:${rowItem.entry.id}`, + domain: rowItem.entry.fqdn, + resolvedIp: "—", + asn: "—", + purpose: mod?.name ?? "—", + filter: cid ? (commLabel.get(cid) ?? cid) : "—", + updated: fmtTime, + enabled: true, + } + }) + + const ipRanges = catalog.ip_ranges.items.map((rowItem) => { + const mod = modMap.get(rowItem.module_id) + const cid = rowItem.entry.community_id + return { + id: `${rowItem.module_id}:${rowItem.entry.id}`, + cidr: rowItem.entry.prefix, + asn: "—", + country: "—", + purpose: mod?.name ?? "—", + filter: commLabel.get(cid) ?? cid, + updated: fmtTime, + enabled: true, + } + }) + + const asns = catalog.asns.items.map((rowItem) => { + const cid = rowItem.entry.community_id + const e = rowItem.entry + const name = (e.asn_name ?? "").trim() + const prefixes = e.prefix_count ?? null + let updated = fmtTime + const rawResolved = (e.asn_resolved_at ?? "").trim() + if (rawResolved) { + const d = new Date(rawResolved) + if (!Number.isNaN(d.getTime())) updated = d.toLocaleString("ru-RU") + } + return { + id: `${rowItem.module_id}:${e.id}`, + asn: `AS${e.asn}`, + org: name || "—", + country: "—", + prefixes, + filter: cid ? (commLabel.get(cid) ?? cid) : "—", + updated, + enabled: true, + } + }) + + const routeCountsByCommUuid = aggregateCommunityRouteCounts(catalog) + + const communities = commItems.map((c) => { + const valueStr = (c.community ?? "").trim() + return { + id: c.id, + value: c.community ?? c.id, + name: c.title ?? c.community ?? c.id, + description: "", + type: "custom" as const, + filterIds: [] as string[], + serverCount: valueStr ? (serversByCommunityStr.get(valueStr) ?? 0) : 0, + prefixCount: routeCountsByCommUuid.has(c.id) ? routeCountsByCommUuid.get(c.id)! : 0, + action: "permit" as const, + enabled: true, + } + }) + + return { + fetchedAt, + modules: modules.map((m) => ({ id: m.id, name: m.name, type: m.type })), + domains, + ipRanges, + asns, + communities, + } +} diff --git a/backend/src/services/evobgp-client.test.ts b/backend/src/services/evobgp-client.test.ts new file mode 100644 index 00000000..9e84888b --- /dev/null +++ b/backend/src/services/evobgp-client.test.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { fetchBoundedEvoCatalog, fetchBoundedEvoModules } from "./evobgp-client.js" + +test("connectivity uses only the fixed modules GET and a smaller body bound", async () => { + assert.deepEqual(await fetchBoundedEvoModules("https://evo.invalid", "fixture", async (url, init) => { + assert.equal(url, "https://evo.invalid/v1/modules?limit=1") + assert.equal(init?.method, "GET"); assert.equal(init?.redirect, "error") + return new Response('{"items":[]}') + }), { items: [] }) + await assert.rejects(fetchBoundedEvoModules("https://evo.invalid", "fixture", async () => new Response("{}", { headers: { "content-length": String(1024 * 1024 + 1) } }), { maxBytes: 8000000 })) +}) + +test("fixed authenticated catalog GET forbids redirects and accepts known empty JSON", async () => { + const result = await fetchBoundedEvoCatalog("https://evo.invalid", "private-fixture", async (url, init) => { + assert.equal(url, "https://evo.invalid/v1/router-lists/catalog") + assert.equal(init?.method, "GET"); assert.equal(init?.redirect, "error") + assert.deepEqual(init?.headers, { Authorization: "Bearer private-fixture", Accept: "application/json" }) + return new Response("{}") + }) + assert.deepEqual(result, {}) +}) + +test("HTTP errors and malformed JSON never echo upstream credential text", async () => { + for (const response of [new Response("private-fixture", { status: 502 }), new Response("not-json-private-fixture")]) { + await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "private-fixture", async () => response), error => error instanceof Error && !error.message.includes("private-fixture")) + } +}) + +test("announced and streamed sizes are bounded and cancel their bodies", async () => { + for (const announced of [true, false]) { + let canceled = false + const response = new Response(new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array(20)) }, cancel() { canceled = true } }), { headers: announced ? { "content-length": "20" } : {} }) + await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "fixture", async () => response, { maxBytes: 10 })) + assert.equal(canceled, true) + } +}) + +test("the deadline also aborts a stalled streaming body", async () => { + const fetcher: typeof fetch = async (_url, init) => new Response(new ReadableStream({ start(controller) { init?.signal?.addEventListener("abort", () => controller.error(new Error("aborted-private-fixture")), { once: true }) } })) + await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "private-fixture", fetcher, { timeoutMs: 20 }), error => error instanceof Error && !error.message.includes("private-fixture")) +}) + +test("an already canceled MCP call dispatches no upstream request", async () => { + const controller = new AbortController(); controller.abort() + let calls = 0 + await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "fixture", async () => { calls++; return new Response("{}") }, { signal: controller.signal })) + assert.equal(calls, 0) +}) diff --git a/backend/src/services/evobgp-client.ts b/backend/src/services/evobgp-client.ts new file mode 100644 index 00000000..268d860c --- /dev/null +++ b/backend/src/services/evobgp-client.ts @@ -0,0 +1,41 @@ +/** Fixed GET, no redirects or response-body errors containing credentials. + * Both headers and the entire response body share one deadline and byte bound. */ +type EvoRequestOptions = { timeoutMs?: number; maxBytes?: number; signal?: AbortSignal } +export function fetchBoundedEvoCatalog(root: string, key: string, fetcher: typeof fetch = fetch, options: EvoRequestOptions = {}): Promise { + return fetchBoundedEvoJson(root, "/v1/router-lists/catalog", key, fetcher, options) +} +export function fetchBoundedEvoModules(root: string, key: string, fetcher: typeof fetch = fetch, options: EvoRequestOptions = {}): Promise { + return fetchBoundedEvoJson(root, "/v1/modules?limit=1", key, fetcher, { ...options, maxBytes: Math.min(options.maxBytes ?? 1024 * 1024, 1024 * 1024) }) +} +async function fetchBoundedEvoJson(root: string, path: string, key: string, fetcher: typeof fetch, options: EvoRequestOptions): Promise { + const controller = new AbortController(), timer = setTimeout(() => controller.abort(), options.timeoutMs ?? 15000) + const cancel = () => controller.abort() + options.signal?.addEventListener("abort", cancel, { once: true }) + try { + if (options.signal?.aborted) throw new Error("Запрос отменён") + const response = await fetcher(`${root}${path}`, { method: "GET", redirect: "error", headers: { Authorization: `Bearer ${key}`, Accept: "application/json" }, signal: controller.signal }) + if (!response.ok || !response.body) { + await response.body?.cancel().catch(() => {}) + throw new Error("Ответ EvoBGP не подтверждён") + } + const maxBytes = options.maxBytes ?? 8 * 1024 * 1024 + if (Number(response.headers.get("content-length")) > maxBytes) { + await response.body.cancel().catch(() => {}) + throw new Error("Ответ EvoBGP превышает предел размера") + } + const reader = response.body.getReader(), chunks: Buffer[] = [] + let received = 0 + try { + for (;;) { + const { done, value } = await reader.read() + if (done) break + received += value.byteLength + if (received > maxBytes) { await reader.cancel().catch(() => {}); throw new Error("Ответ EvoBGP превышает предел размера") } + chunks.push(Buffer.from(value)) + } + } finally { reader.releaseLock() } + return JSON.parse(Buffer.concat(chunks, received).toString("utf8")) + } catch { + throw new Error("Каталог EvoBGP недоступен, повреждён или превышает ограничения запроса") + } finally { clearTimeout(timer); options.signal?.removeEventListener("abort", cancel) } +} diff --git a/backend/src/services/evobgp-community-counts.ts b/backend/src/services/evobgp-community-counts.ts new file mode 100644 index 00000000..ce60b0e3 --- /dev/null +++ b/backend/src/services/evobgp-community-counts.ts @@ -0,0 +1,15 @@ +import { db } from "../db/index.js" +import { filterRules } from "../db/schema.js" + +/** Callers must first authorize the complete local server catalog. */ +export async function distinctServersByFilterCommunityValue(): Promise> { + const rows = await db.select({ serverId: filterRules.serverId, community: filterRules.community }).from(filterRules) + const values = new Map>() + for (const row of rows) { + const value = row.community.trim() + if (!value) continue + const ids = values.get(value) ?? new Set() + ids.add(row.serverId); values.set(value, ids) + } + return new Map([...values].map(([value, ids]) => [value, ids.size])) +} diff --git a/backend/src/services/evobgp-probe.ts b/backend/src/services/evobgp-probe.ts new file mode 100644 index 00000000..0aa0d7b7 --- /dev/null +++ b/backend/src/services/evobgp-probe.ts @@ -0,0 +1,9 @@ +import { z } from "zod" +import { fetchBoundedEvoModules } from "./evobgp-client.js" + +/** Never return upstream data or reflected credentials from a connectivity check. */ +export async function probeEvoBgp(root: string, key: string, signal?: AbortSignal) { + const result = await fetchBoundedEvoModules(root, key, undefined, { signal }) + if (!z.object({ items: z.array(z.unknown()) }).safeParse(result).success) throw new Error("Соединение с EvoBGP не подтверждено: некорректный ответ API") + return { items: [] } +} diff --git a/backend/src/services/evobgp-settings-model.ts b/backend/src/services/evobgp-settings-model.ts new file mode 100644 index 00000000..a5952225 --- /dev/null +++ b/backend/src/services/evobgp-settings-model.ts @@ -0,0 +1,9 @@ +import { evoBgpSettingsSchema, normalizeEvoBgpApiKey } from "@mmapp/contracts/evobgp" + +export type EvoBgpSettingsRow = { baseUrl: string; enabled: boolean; apiKey: string } + +export function publicEvoBgpSettings(row: EvoBgpSettingsRow) { + const parsed = evoBgpSettingsSchema.safeParse({ baseUrl: row.baseUrl, enabled: row.enabled, secretConfigured: Boolean(normalizeEvoBgpApiKey(row.apiKey)) }) + if (!parsed.success) throw new Error("Настройки EvoBGP не подтверждены сервером") + return parsed.data +} diff --git a/backend/src/services/evobgp-settings.ts b/backend/src/services/evobgp-settings.ts new file mode 100644 index 00000000..02691a90 --- /dev/null +++ b/backend/src/services/evobgp-settings.ts @@ -0,0 +1,28 @@ +import { eq, sql } from "drizzle-orm" +import { evoBgpSettingsPatchSchema, type EvoBgpSavePayload } from "@mmapp/contracts/evobgp" +import { db } from "../db/index.js" +import { evobgpSettings } from "../db/schema.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" + +import type { EvoBgpSettingsRow } from "./evobgp-settings-model.js" +export { publicEvoBgpSettings } from "./evobgp-settings-model.js" + +/** A GET must not create a configuration row or emit a mutation. */ +export async function readEvoBgpSettings(): Promise { + const [row] = await db.select().from(evobgpSettings).where(eq(evobgpSettings.id, 1)).limit(1) + return row ? { baseUrl: row.baseUrl, enabled: row.enabled, apiKey: row.apiKey } : { baseUrl: "", enabled: false, apiKey: "" } +} + +export async function writeEvoBgpSettings(input: EvoBgpSavePayload): Promise { + const patch = evoBgpSettingsPatchSchema.parse(input) + return executeGuardedApplicationWrite({ kind: "evobgp-settings", id: "1" }, "all", async () => { + await db.insert(evobgpSettings).values({ id: 1 }).onConflictDoNothing() + const values: Partial = {} + if (patch.baseUrl !== undefined) values.baseUrl = patch.baseUrl + if (patch.enabled !== undefined) values.enabled = patch.enabled + if (patch.apiKey !== undefined) values.apiKey = patch.apiKey ?? "" + const [row] = await db.update(evobgpSettings).set({ ...values, updatedAt: sql`now()` }).where(eq(evobgpSettings.id, 1)).returning() + if (!row) throw new Error("Сохранение EvoBGP не подтверждено сервером") + return { baseUrl: row.baseUrl, enabled: row.enabled, apiKey: row.apiKey } + }) +} diff --git a/backend/src/services/filter-rules-mutation.test.ts b/backend/src/services/filter-rules-mutation.test.ts new file mode 100644 index 00000000..1716a484 --- /dev/null +++ b/backend/src/services/filter-rules-mutation.test.ts @@ -0,0 +1,38 @@ +import assert from "node:assert/strict" +import type { MikrotikClient } from "./mikrotik.js" +import { mutateManagedFilter } from "./filter-rules-mutation.js" + +let rows: Record[] = [], writes = 0, ignored = false, corrupt = false, lost = false, disabledReply: unknown = { ret: "false" } +const foreign = { ".id": "*foreign", chain: "other", rule: "accept;", comment: "NEVER_UNMANAGED_SECRET", packets: "1" } +const managed = { ".id": "*own", chain: "bgp-in", rule: "old;", comment: "MikrotikManager: fixture", disabled: "false" } +const client = { + get: async () => structuredClone(rows), + post: async (path: string, fields: Record) => { + if (path.endsWith("/get")) return disabledReply + writes++; if (!ignored) rows.push({ ...fields, ".id": "*new", disabled: "false" }) + if (lost) throw new Error("lost response") + return { ret: "*new" } + }, + patch: async (_path: string, fields: Record) => { + writes++; if (!ignored) Object.assign(rows.find(row => row[".id"] === "*own")!, fields, { disabled: "false" }) + if (corrupt) rows.find(row => row[".id"] === "*foreign")!.rule = "changed;" + return {} + }, + delete: async () => { writes++; if (!ignored) rows = rows.filter(row => row[".id"] !== "*own" && row[".id"] !== "*new"); return {} }, +} as unknown as Pick +const apply = (body = "if (bgp-communities includes 64512:1) { set blackhole yes; accept; }") => mutateManagedFilter(client, body, "MikrotikManager: fixture") +rows = [{ ...foreign }]; assert.equal((await apply()).action, "create"); assert.equal(rows.length, 2); assert.equal(writes, 1) +rows = [{ ...foreign }, { ...managed }]; assert.equal((await apply()).action, "patch"); assert.equal(rows[0].rule, "accept;") +const beforeSame = writes; assert.equal((await apply()).action, "noop"); assert.equal(writes, beforeSame) +assert.equal((await apply("")).action, "delete"); assert.equal(rows.length, 1) +const beforeNoop = writes; assert.equal((await apply("")).action, "noop"); assert.equal(writes, beforeNoop) +rows = [{ ...managed }, { ...managed, ".id": "*second" }]; await assert.rejects(apply()); assert.equal(writes, beforeNoop) +rows = [{ ...foreign, chain: "bgp-in", rule: "if (bgp-communities includes 1:1) {reject;}" }]; await assert.rejects(apply()); assert.equal(writes, beforeNoop) +rows = [{ ...foreign }, { ...managed }]; ignored = true; await assert.rejects(apply(), /read-back/); ignored = false +rows = [{ ...foreign }, { ...managed }]; corrupt = true; await assert.rejects(apply(), /другие фильтры/); corrupt = false +rows = []; lost = true; const beforeLost = writes; await assert.rejects(apply(), error => error instanceof Error && "code" in error && error.code === "MUTATION_OUTCOME_UNKNOWN"); assert.equal(writes, beforeLost + 1); lost = false +rows = [{ ...foreign }, { ...managed, disabled: "true" }]; ignored = true; await assert.rejects(apply()); ignored = false +rows = [{ ...foreign }, { ...managed }]; delete rows[1].disabled; ignored = true; disabledReply = []; await assert.rejects(apply()); ignored = false; disabledReply = { ret: "false" } +rows = [{ ...foreign, chain: "BGP-IN", comment: "MikrotikManager: other chain" }]; assert.equal((await apply()).action, "create"); assert.equal(rows[0].chain, "BGP-IN") +assert.equal(JSON.stringify(await apply("")).includes("NEVER_UNMANAGED_SECRET"), false) +console.log("Community mutation: read-back before metadata, create/patch/delete/no-op, foreign/duplicate ownership protection, preserved chain case/order, ignored/unexpected/unknown writes and no automatic replay") diff --git a/backend/src/services/filter-rules-mutation.ts b/backend/src/services/filter-rules-mutation.ts new file mode 100644 index 00000000..cb518939 --- /dev/null +++ b/backend/src/services/filter-rules-mutation.ts @@ -0,0 +1,72 @@ +import type { MikrotikClient } from "./mikrotik.js" +import { nativeBgpRows, type NativeBgpRow } from "./bgp-export-model.js" +import { planBgpInApply } from "./config-apply-plan.js" +import { hasManagedCommentPrefix } from "../managed-markers.js" +import { readOspfEffectiveDisabled } from "./ospf-effective-disabled.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { routingFilterBodiesMatch } from "./routing-filter-body.js" + +type Client = Pick +const owned = (row: NativeBgpRow) => row.chain === "bgp-in" && typeof row.comment === "string" && hasManagedCommentPrefix(row.comment) +const stable = (row: NativeBgpRow) => Object.fromEntries(Object.entries(row).filter(([key]) => !["invalid", "inactive", "match-count", "matched", "hits", "packets", "bytes"].includes(key) && !key.startsWith("debug."))) +export async function writeRoutingFilter(work: () => Promise, body = "") { + try { return await work() } + catch (error) { + if (error instanceof McpError) throw error + const status = error && typeof error === "object" && "statusCode" in error && typeof error.statusCode === "number" ? error.statusCode : null + if (status && status >= 400 && status < 500) { + let detail = "" + try { const value = JSON.parse(error && typeof error === "object" && "body" in error && typeof error.body === "string" ? error.body : "{}"); detail = typeof value.detail === "string" ? value.detail.slice(0, 8192) : "" } catch { /* No raw provider data in diagnostics. */ } + const line = Number(detail.match(/line\s+(\d+)/i)?.[1]) || null, column = Number(detail.match(/column\s+(\d+)/i)?.[1]) || null + const text = line ? body.split("\n")[line - 1]?.trim() ?? "" : "" + const category = text.startsWith("#") ? "comment" : text.startsWith("set blackhole") ? "blackhole" : text.startsWith("set gw-interface") ? "interface" : text.startsWith("set gw ") ? "gateway" : text.startsWith("if ") ? "match" : "unknown" + console.warn("filter.native.rejected", { status, line, column, category, keywords: detail.match(/\b(?:expected|unknown|invalid|syntax|blackhole|comment|gw-interface|out-interface|command)\b/gi)?.slice(0, 10) ?? [] }) + throw new McpError("ROUTER_REJECTED", `Устройство отклонило запись фильтра (HTTP ${status}); native payload не отражается`, 409) + } + throw new McpError("MUTATION_OUTCOME_UNKNOWN", "Запись фильтра не подтверждена; сверяйте состояние перед новым планом, автоматический повтор запрещён", 503) + } +} +export async function readManagedFilterState(client: Client) { + let rows: NativeBgpRow[] + try { rows = nativeBgpRows(await client.get("/routing/filter/rule")) } + catch (error) { if (error instanceof McpError) throw error; throw new McpError("STATE_UNAVAILABLE", "Чтение routing filters не подтверждено", 409) } + if (rows.length > 1000 || rows.some(row => typeof row.chain !== "string" || typeof row.rule !== "string" || row.comment !== undefined && typeof row.comment !== "string")) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён полный каталог routing filters", 409) + const managed = rows.filter(owned) + if (managed.length > 1) throw new McpError("STATE_CONFLICT", "Несколько управляемых bgp-in правил; замена неоднозначна", 409) + return { rows, managed, proofRows: rows.map(stable) } +} +export function managedFilterNeedsWrite(state: Awaited>, body: string, comment: string) { + if (!body) return state.managed.length > 0 + const row = state.managed[0] + return state.managed.length !== 1 || !routingFilterBodiesMatch(row.rule, body) || row.comment !== comment || !["false", "no"].includes(String(row.disabled)) +} +/** One managed rule, no foreign deletions, and read-back before DB/revision work. */ +export async function mutateManagedFilter(client: Client, ruleBody: string, managedComment: string, beforeWrite?: (state: Awaited>) => Promise) { + const before = await readManagedFilterState(client), plan = planBgpInApply(before.rows, ruleBody ? 1 : 0) + if (plan.conflictIds.length) throw new McpError("STATE_CONFLICT", "Обнаружены неуправляемые Community-фильтры; автоматическое удаление запрещено", 409) + await beforeWrite?.(before) + const needsWrite = managedFilterNeedsWrite(before, ruleBody, managedComment) + let createdId: string | undefined + if (plan.action === "patch" && needsWrite) await writeRoutingFilter(() => client.patch(`/routing/filter/rule/${encodeURIComponent(plan.managedId!)}`, { chain: "bgp-in", comment: managedComment, rule: ruleBody, disabled: "no" }), ruleBody) + else if (plan.action === "create") { + const result = await writeRoutingFilter(() => client.post("/routing/filter/rule/add", { chain: "bgp-in", comment: managedComment, rule: ruleBody, disabled: "no" }), ruleBody) + if (!result || typeof result !== "object" || !("ret" in result) || typeof result.ret !== "string" || !result.ret.trim()) throw new McpError("NATIVE_ID_UNCONFIRMED", "Создание фильтра отправлено без подтверждённого native ID; повтор запрещён", 503) + createdId = result.ret + } else if (plan.action === "delete") await writeRoutingFilter(() => client.delete(`/routing/filter/rule/${encodeURIComponent(plan.managedId!)}`)) + const after = await readManagedFilterState(client) + if (fingerprint(before.rows.filter(row => !owned(row)).map(stable)) !== fingerprint(after.rows.filter(row => !owned(row)).map(stable))) throw new McpError("VERIFICATION_FAILED", "После записи изменились другие фильтры или их порядок", 409) + if (ruleBody) { + const row = after.managed[0] + if (after.managed.length !== 1 || row[".id"] !== (createdId ?? plan.managedId) || !routingFilterBodiesMatch(row.rule, ruleBody) || row.comment !== managedComment || row.invalid === "true") { + console.warn("filters.native-readback.mismatch", { managedCount: after.managed.length, idMatches: row?.[".id"] === (createdId ?? plan.managedId), bodyMatches: routingFilterBodiesMatch(row?.rule, ruleBody), rawBodyMatches: row?.rule === ruleBody, commentMatches: row?.comment === managedComment, + knownInvalid: [true, "true", "yes"].includes(row?.invalid as string | boolean), expectedBodyBytes: Buffer.byteLength(ruleBody, "utf8"), actualBodyBytes: typeof row?.rule === "string" ? Buffer.byteLength(row.rule, "utf8") : null, + expectedQuotedInterfaceCount: (ruleBody.match(/set\s+gw-interface\s+"/g) ?? []).length, actualQuotedInterfaceCount: typeof row?.rule === "string" ? (row.rule.match(/set\s+gw-interface\s+"/g) ?? []).length : null }) + throw new McpError("VERIFICATION_FAILED", "Native read-back управляемого фильтра не совпал", 409) + } + if (plan.action === "create" && after.rows.at(-1)?.[".id"] !== row[".id"]) throw new McpError("VERIFICATION_FAILED", "Новый фильтр появился в неожиданной позиции", 409) + if (plan.action === "patch" && before.rows.findIndex(value => value[".id"] === row[".id"]) !== after.rows.findIndex(value => value[".id"] === row[".id"])) throw new McpError("VERIFICATION_FAILED", "Позиция управляемого фильтра изменилась", 409) + const [effective] = await readOspfEffectiveDisabled([row], nativeId => client.post("/routing/filter/rule/get", { ".id": nativeId, "value-name": "disabled" })).catch(() => { throw new McpError("VERIFICATION_FAILED", "Не подтверждён effective disabled фильтра после записи", 409) }) + if (!["false", "no"].includes(String(effective.disabled))) throw new McpError("VERIFICATION_FAILED", "Фильтр не подтверждён как включённый", 409) + } else if (after.managed.length) throw new McpError("VERIFICATION_FAILED", "Удаление фильтра не подтверждено", 409) + return { action: needsWrite ? plan.action : "noop", conflictsRemoved: 0, verified: true } +} diff --git a/backend/src/services/filter-rules-observed.test.ts b/backend/src/services/filter-rules-observed.test.ts new file mode 100644 index 00000000..5307a8ca --- /dev/null +++ b/backend/src/services/filter-rules-observed.test.ts @@ -0,0 +1,16 @@ +import assert from "node:assert/strict" +import { observeBgpFilterRules } from "./filter-rules-observed.js" + +for (const raw of [{}, [{}], [{".id":"*A",chain:"bgp-in",rule:false}], [{".id":"*A",chain:"bgp-in",rule:"accept;"},{".id":"*A",chain:"other",rule:"reject;"}]]) assert.throws(()=>observeBgpFilterRules(raw)) +const rows = observeBgpFilterRules([ + {".id":"*B",chain:"other",rule:"reject;"}, + {".id":"*case",chain:"BGP-IN",rule:"reject;"}, + {".id":"*C",chain:"bgp-in",rule:"if (bgp-communities includes 64512:1) { accept; }",disabled:"true","private-key":"never-return"}, + {".id":"*D",chain:"bgp-in",rule:"jump other;"}, + {".id":"*E",chain:"bgp-in",rule:"accept;",disabled:"false"}, +]) +assert.deepEqual(rows.map(row=>row.rosId),["*C","*D","*E"]) +assert.deepEqual(rows.map(row=>row.order),[0,1,2]); assert.deepEqual(rows.map(row=>row.enabled),[false,null,true]) +assert.ok(rows.every(row=>row.semanticsVerified===false)); assert.ok(!JSON.stringify(rows).includes("never-return")) +assert.deepEqual(observeBgpFilterRules([]),[]) +console.log("BGP filter observed: strict source and unique identity, bgp-in relative order, disabled/unknown, no inferred script effects or unknown native fields") diff --git a/backend/src/services/filter-rules-observed.ts b/backend/src/services/filter-rules-observed.ts new file mode 100644 index 00000000..09d086f9 --- /dev/null +++ b/backend/src/services/filter-rules-observed.ts @@ -0,0 +1,17 @@ +import { z } from "zod" + +const nativeFiltersSchema = z.array(z.object({ + ".id": z.string().min(1), chain: z.string(), rule: z.string(), comment: z.string().optional(), disabled: z.string().optional(), +})).superRefine((rows, context) => { + if (new Set(rows.map(row => row[".id"])).size !== rows.length) context.addIssue({ code: "custom", message: "Повторяющийся native ID фильтра" }) +}) + +/** The business parser extracts hints; it cannot evaluate RouterOS script flow, + * chained rules, prefix propagation, or actual packet forwarding. */ +export function observeBgpFilterRules(raw: unknown) { + return nativeFiltersSchema.parse(raw).filter(row => row.chain === "bgp-in").map((row, order) => ({ + rosId: row[".id"], order, chain: row.chain, rule: row.rule, comment: row.comment ?? "", + enabled: row.disabled === "false" || row.disabled === "no" ? true : row.disabled === "true" || row.disabled === "yes" ? false : null, + semanticsVerified: false, + })) +} diff --git a/backend/src/services/filter-rules-write-model.test.ts b/backend/src/services/filter-rules-write-model.test.ts new file mode 100644 index 00000000..ddce33d4 --- /dev/null +++ b/backend/src/services/filter-rules-write-model.test.ts @@ -0,0 +1,23 @@ +import assert from "node:assert/strict" +import { compileManagedFilterRules, filterRuleBranches, filterRuleToken, validatedFilterRules } from "./filter-rules-write-model.js" + +const route = { community: "64512:1", action: "route" as const, gateway: "192.0.2.2", gatewayTunnelId: "GRE test", description: "first }\rset gw 127.0.0.1;" } +const blackhole = { community: "64512:2", action: "blackhole" as const, gateway: "", gatewayTunnelId: "", description: "drop" } +const rules = validatedFilterRules([route, blackhole]) +const body = compileManagedFilterRules(rules.map(rule => ({ ...rule, outIface: rule.gatewayTunnelId ? "GRE test" : "" }))) +assert.ok(body.includes('set gw-interface "GRE test";')); assert.ok(!body.includes("$x")) +assert.ok(body.includes("set blackhole yes;")); assert.ok(!body.includes("set type blackhole")); assert.ok(!body.includes("set out-interface")) +assert.ok(!body.includes("first }")); assert.ok(!body.includes("127.0.0.1")); assert.ok(!body.includes("# 64512")) +assert.equal(filterRuleToken(body, "interface"), "GRE test") +assert.equal(filterRuleToken(body, "gw"), route.gateway) +assert.equal(filterRuleBranches(body).length, 2) +assert.equal(filterRuleBranches('if (bgp-communities includes 1:1) {set gw-interface "name } ( # \\" {"; accept;}').length, 1) +assert.equal(filterRuleBranches("# if (fake) { accept;}\nif (protocol bgp) { if (active) { accept;} }").length, 1) +assert.equal(filterRuleToken('set out-interface "legacy tunnel";', "interface"), "legacy tunnel") +assert.equal(filterRuleToken("set gw 192.0.2.2;", "gw"), "192.0.2.2") +assert.equal(validatedFilterRules([{ ...route, community: "00001:00002" }])[0].community, "1:2") +for (const gatewayTunnelId of ['name"', "name$", "name\\", "*4A", "name;accept;", "name }", "name\n"]) assert.throws(() => validatedFilterRules([{ ...route, gatewayTunnelId }])) +for (const value of [[{ ...route, community: "65536:1" }], [{ ...route, community: "1:1); reject;" }], [{ ...route, gateway: "192.0.2.2; reject;" }], [{ ...route, gateway: "fe80::1%gre" }], [{ ...route, gatewayTunnelId: "bad\nname" }], [{ ...route, description: "a\u2028reject;" }], [{ ...blackhole, gateway: "192.0.2.2" }], [{ ...route, gatewayTunnelId: "rec:0" }], [{ ...route, community: "1:2" }, { ...blackhole, community: "01:02" }]]) assert.throws(() => validatedFilterRules(value)) +assert.throws(() => compileManagedFilterRules([{ community: "1:1); reject;", action: "route", gateway: "192.0.2.2", outIface: "gre", description: "" }])) +assert.throws(() => compileManagedFilterRules(Array.from({ length: 1000 }, (_, index) => ({ community: "64512:" + index, action: "route", gateway: "192.0.2.2", outIface: "gre-" + index.toString(16), description: "" }))), /64 KiB/) +console.log("Community compiler: RouterOS 7 gw-interface/blackhole, verified safe interface names, annotations omitted from native code, standard uint16 pairs, rec references, duplicate/control/injection and body size guards") diff --git a/backend/src/services/filter-rules-write-model.ts b/backend/src/services/filter-rules-write-model.ts new file mode 100644 index 00000000..050d0c3f --- /dev/null +++ b/backend/src/services/filter-rules-write-model.ts @@ -0,0 +1,85 @@ +import { isIP } from "node:net" +import { z } from "zod" + +const text = z.string().max(500).refine(value => !/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f\u0085\u2028\u2029]/.test(value)) +// RouterOS routing-filter strings are not RouterOS CLI strings. Do not assume +// CLI escapes or native menu IDs can represent arbitrary interface names. +const interfaceName = z.string().max(100).refine(value => value === "" || /^rec:[1-9]\d*$/.test(value) || /^[A-Za-z0-9_.:@/ -]{1,100}$/.test(value), "Имя GRE не поддерживается языком фильтров; требуется отдельное переименование интерфейса") +export const standardCommunitySchema = z.string().trim().regex(/^\d{1,5}:\d{1,5}$/).refine(value => value.split(":").every(part => Number(part) <= 65535)).transform(value => value.split(":").map(Number).join(":")) +export const filterRuleWriteSchema = z.object({ + id: z.string().max(200).optional(), community: standardCommunitySchema, + communityName: text.optional(), action: z.enum(["route", "blackhole"]).default("route"), + gateway: z.string().trim().max(300).default(""), gatewayTunnelId: interfaceName.default(""), description: text.default(""), +}).strict() +export const filterRulesWriteSchema = z.array(filterRuleWriteSchema).max(1000).superRefine((rules, context) => { + const seen = new Set() + rules.forEach((rule, index) => { + if (seen.has(rule.community)) context.addIssue({ code: "custom", path: [index, "community"], message: "Community повторяется" }) + seen.add(rule.community) + if (rule.action === "blackhole") { + if (rule.gateway || rule.gatewayTunnelId) context.addIssue({ code: "custom", path: [index], message: "Blackhole не принимает gateway/interface" }) + } else if (rule.gatewayTunnelId.startsWith("rec:")) { + if (!/^rec:[1-9]\d*$/.test(rule.gatewayTunnelId) || !Number.isSafeInteger(Number(rule.gatewayTunnelId.slice(4))) || !isIP(rule.gateway) || rule.gateway.includes("%")) context.addIssue({ code: "custom", path: [index], message: "Нужны корректные rec:ID и literal gateway" }) + } else if (!isIP(rule.gateway) || rule.gateway.includes("%")) context.addIssue({ code: "custom", path: [index, "gateway"], message: "Gateway должен быть literal IPv4/IPv6; интерфейс задаётся отдельно" }) + }) +}) +export function validatedFilterRules(value: unknown) { + return filterRulesWriteSchema.parse(value).map(rule => ({ ...rule, id: rule.id ?? rule.community, description: rule.description.replace(/\r\n?/g, "\n") })) +} +export function filterRuleToken(body: string, property: "gw" | "interface") { + const expression = property === "gw" ? /set\s+gw(?:ateway)?\s+("(?:\\.|[^"\\])*"|[^\s;]+)/i : /set\s+(?:gw-interface|out-interface)\s+("(?:\\.|[^"\\])*"|[^\s;]+)/i + const value = filterSyntaxMask(body, false).match(expression)?.[1] ?? "" + return value.startsWith('"') ? value.slice(1, -1).replace(/\\([\\"$])/g, "$1") : value +} +export function filterRuleDescriptions(text: string) { + const descriptions = new Map() + for (const row of text.matchAll(/^[ \t]*#[ \t]*(\d+:\d+):[ \t]*([^\r\n]*)/gm)) descriptions.set(row[1], descriptions.has(row[1]) ? descriptions.get(row[1]) + "\n" + row[2].trim() : row[2].trim()) + return descriptions +} +function filterSyntaxMask(text: string, maskStrings: boolean) { + const mask = text.split(""); let quoted = false, comment = false + for (let index = 0; index < text.length; index++) { + const char = text[index] + if (comment) { if (char === "\n" || char === "\r") comment = false; else mask[index] = " "; continue } + if (quoted) { if (maskStrings) mask[index] = " "; if (char === "\\") { if (index + 1 < text.length) { index++; if (maskStrings) mask[index] = " " } } else if (char === '"') quoted = false; continue } + if (char === '"') { quoted = true; if (maskStrings) mask[index] = " " } + else if (char === "#") { comment = true; mask[index] = " " } + } + return mask.join("") +} +/** Locate balanced if bodies while ignoring quoted names and line comments. */ +export function filterRuleBranches(text: string) { + const source = filterSyntaxMask(text, true), expression = /\b(?:else\s+)?if\s*\(/g, branches: { condition: string; body: string }[] = [] + while (expression.exec(source)) { + const open = expression.lastIndex - 1; let cursor = open + 1, depth = 1 + for (; cursor < source.length && depth; cursor++) { if (source[cursor] === "(") depth++; else if (source[cursor] === ")") depth-- } + if (depth) break + const conditionEnd = cursor - 1 + while (/\s/.test(source[cursor] ?? "") && cursor < source.length) cursor++ + if (source[cursor] !== "{") continue + const bodyStart = ++cursor; depth = 1 + for (; cursor < source.length && depth; cursor++) { if (source[cursor] === "{") depth++; else if (source[cursor] === "}") depth-- } + if (depth) break + branches.push({ condition: text.slice(open + 1, conditionEnd), body: text.slice(bodyStart, cursor - 1) }); expression.lastIndex = cursor + } + return branches +} +type CompiledRule = { community: string; action: "route" | "blackhole"; description: string; gateway: string; outIface: string } +export function compileManagedFilterRules(rules: readonly CompiledRule[]) { + const validated = validatedFilterRules(rules.map(({ outIface, ...rule }) => ({ ...rule, gatewayTunnelId: outIface }))).map(rule => ({ ...rule, outIface: rule.gatewayTunnelId })) + const groups = new Map() + for (const rule of validated) { + if (rule.action === "route" && (!isIP(rule.gateway) || rule.outIface !== "" && !/^[A-Za-z0-9_.:@/ -]{1,100}$/.test(rule.outIface))) throw new Error("Имя GRE нельзя безопасно представить в языке routing filter; требуется отдельное переименование интерфейса") + const key = JSON.stringify([rule.action, rule.gateway, rule.outIface]), group = groups.get(key) ?? [] + group.push(rule); groups.set(key, group) + } + const body = [...groups.values()].map(group => { + const first = group[0] + // The routing filter language does not accept RouterOS script '#' comments. + // Labels/descriptions remain manager metadata, never executable text. + return [`if (${group.map(rule => `bgp-communities includes ${rule.community}`).join(" || ")}) {`, + ...(first.action === "blackhole" ? [" set blackhole yes;"] : [` set gw ${first.gateway};`, ...(first.outIface ? [` set gw-interface "${first.outIface}";`] : [])]), " accept;", "}"].join("\n") + }).join("\n") + if (Buffer.byteLength(body, "utf8") > 65536) throw new Error("Тело управляемого фильтра превышает 64 KiB") + return body +} diff --git a/backend/src/services/firewall-address-input.ts b/backend/src/services/firewall-address-input.ts new file mode 100644 index 00000000..4a4a4ea3 --- /dev/null +++ b/backend/src/services/firewall-address-input.ts @@ -0,0 +1,42 @@ +import { z } from "zod" +const FamilySchema = z.enum(["ip", "ip6"]) +const NativeIdSchema = z.string().regex(/^\*[A-Za-z0-9]+$/).max(100) +export const AddressKeySchema = z.object({ + serverId: z.string().min(1), + family: FamilySchema, + rosId: NativeIdSchema, +}) + +export const AddressWriteSchema = z.object({ + serverId: z.string().min(1), + family: FamilySchema, + rosId: NativeIdSchema.optional(), + list: z.string().min(1), + address: z.string().min(1), + comment: z.string().optional(), + timeout: z.string().optional(), + disabled: z.boolean().optional(), +}) + +export const AddressPatchSchema = AddressKeySchema.extend({ + disabled: z.boolean(), +}) + +function toRosBody(obj: Record): Record { + const out: Record = {} + for (const [k, v] of Object.entries(obj)) { + if (v !== undefined && v !== "") out[k] = v + } + return out +} + +export function addressToRos(d: z.infer): Record { + return toRosBody({ + list: d.list, + address: d.address, + comment: d.comment, + timeout: d.timeout, + disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined, + }) +} + diff --git a/backend/src/services/firewall-address-write.test.ts b/backend/src/services/firewall-address-write.test.ts new file mode 100644 index 00000000..9b897253 --- /dev/null +++ b/backend/src/services/firewall-address-write.test.ts @@ -0,0 +1,38 @@ +import assert from "node:assert/strict" +import { firewallAddressIdentity, expiryMatches, verifyFirewallAddressMutation, executeFirewallAddressMutation, firewallAddressMutationSchema, type AddressState, type FirewallAddressMutation } from "./firewall-address-write.js" +import { fingerprint } from "../modules/mcp/security.js" +const row = { ".id": "*A", list: "fixture", address: "192.0.2.1", disabled: "true", dynamic: "false", timeout: "none-static", comment: "opaque" } +const before: AddressState = { stable: { serverConfiguration: "source", rows: [row] }, timers: {} } +const toggle: FirewallAddressMutation = { family: "ip", operation: "toggle", rosId: "*A", disabled: false } +const changed: AddressState = { ...before, stable: { ...before.stable, rows: [{ ...row, disabled: "false" }] } } +assert.equal(verifyFirewallAddressMutation(toggle, before, changed, { earliest: 0, latest: 0 }), true) +assert.equal(verifyFirewallAddressMutation(toggle, before, { ...changed, stable: { ...changed.stable, rows: [{ ...changed.stable.rows[0], comment: "drift" }] } }, { earliest: 0, latest: 0 }), false) +assert.equal(firewallAddressIdentity("192.0.2.1"), firewallAddressIdentity("192.0.2.1/32")) +assert.equal(firewallAddressIdentity("192.0.2.0-192.0.2.255"), firewallAddressIdentity("192.0.2.1/24")) +assert.equal(firewallAddressIdentity("2001:db8:0::1/64"), firewallAddressIdentity("2001:db8::/64")) +assert.notEqual(firewallAddressIdentity("::ffff:192.0.2.1"), firewallAddressIdentity("192.0.2.1")) +const expiry = { earliest: 100000, latest: 102000 } +assert.equal(expiryMatches(expiry, { earliest: 101000, latest: 103000 }), true) +assert.equal(expiryMatches(expiry, { earliest: 105000, latest: 106000 }), false) +assert.equal(expiryMatches(expiry, undefined), false) +const finiteBefore: AddressState = { stable: { ...before.stable, rows: [{ ...row, dynamic: "true", timeout: "finite" }] }, timers: { "*A": expiry } } +const finiteNext: AddressState = { ...finiteBefore, stable: { ...finiteBefore.stable, rows: [{ ...finiteBefore.stable.rows[0], disabled: "false" }] }, timers: { "*A": { earliest: 101000, latest: 103000 } } } +assert.equal(verifyFirewallAddressMutation(toggle, finiteBefore, finiteNext, { earliest: 0, latest: 0 }), true) +assert.equal(verifyFirewallAddressMutation(toggle, finiteBefore, { ...finiteNext, timers: { "*A": { earliest: 110000, latest: 111000 } } }, { earliest: 0, latest: 0 }), false) +const create: FirewallAddressMutation = { family: "ip", operation: "create", fields: { list: "fixture", address: "192.0.2.2/32", disabled: true, timeout: "1m" } } +const created: AddressState = { stable: { ...before.stable, rows: [row, { ...row, ".id": "*B", address: "192.0.2.2", dynamic: "true", timeout: "finite" }] }, timers: { "*B": { earliest: 59900, latest: 61200 } } } +assert.equal(verifyFirewallAddressMutation(create, before, created, { earliest: 0, latest: 1000 }), true) +assert.equal(verifyFirewallAddressMutation(create, before, { ...created, timers: {} }, { earliest: 0, latest: 1000 }), false) +const remove: FirewallAddressMutation = { family: "ip", operation: "delete", rosId: "*A" } +assert.equal(verifyFirewallAddressMutation(remove, before, { stable: { ...before.stable, rows: [] }, timers: {} }, { earliest: 0, latest: 0 }), true) +let reads = 0, commands = 0, revisions = 0 +const deps = { read: async () => ++reads === 1 ? before : changed, command: async () => { commands++ }, recordRevision: async () => { revisions++; return true } } +assert.equal((await executeFirewallAddressMutation(toggle, fingerprint(before.stable), undefined, deps)).verified, true); assert.equal(commands, 1); assert.equal(revisions, 1) +commands = 0; reads = 0 +await assert.rejects(executeFirewallAddressMutation(toggle, "stale", undefined, deps), /изменился/); assert.equal(commands, 0) +await assert.rejects(executeFirewallAddressMutation(toggle, fingerprint(finiteBefore.stable), { earliest: 1, latest: 2 }, { ...deps, read: async () => finiteBefore }), /Срок/); assert.equal(commands, 0) +assert.equal((await executeFirewallAddressMutation(toggle, undefined, undefined, { ...deps, read: async () => before })).verified, false); assert.equal(commands, 1) +commands = 0 +await assert.rejects(executeFirewallAddressMutation(toggle, undefined, undefined, { ...deps, read: async () => before, command: async () => { commands++; throw Error("lost response") } }), /lost response/); assert.equal(commands, 1) +assert.equal(firewallAddressMutationSchema.safeParse({ ...toggle, rosId: "*A,*B" }).success, false) +console.log("Address-list writes: v4/v6/range canonical identity, permanent/timed CRUD, natural countdown versus expiry drift, unknown/partial/lost response and no replay passed") diff --git a/backend/src/services/firewall-address-write.ts b/backend/src/services/firewall-address-write.ts new file mode 100644 index 00000000..d9adf076 --- /dev/null +++ b/backend/src/services/firewall-address-write.ts @@ -0,0 +1,136 @@ +import { z } from "zod" +import { isIP } from "node:net" +import { ipv4ToInt, ipv6ToInt } from "./traffic-flow-ip.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { MikrotikClient, firewallRestPath, encodeRosId } from "./mikrotik.js" +import { observedBoolean, observedDurationMs } from "./ospf-observed-values.js" +import { fingerprint } from "../modules/mcp/security.js" +import { AddressWriteSchema, addressToRos } from "./firewall-address-input.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { fetchFirewallState } from "./firewall-live.js" + +const key = { family: z.enum(["ip", "ip6"]) }, nativeId = z.string().regex(/^\*[A-Za-z0-9]+$/).max(100) +export const firewallAddressFieldsSchema = AddressWriteSchema.omit({ serverId: true, family: true, rosId: true }).strict() +export const firewallAddressMutationSchema = z.discriminatedUnion("operation", [ + z.object({ ...key, operation: z.literal("create"), fields: firewallAddressFieldsSchema }).strict(), + z.object({ ...key, operation: z.literal("update"), rosId: nativeId, fields: firewallAddressFieldsSchema }).strict(), + z.object({ ...key, operation: z.literal("toggle"), rosId: nativeId, disabled: z.boolean() }).strict(), + z.object({ ...key, operation: z.literal("delete"), rosId: nativeId }).strict(), +]) +export type FirewallAddressMutation = z.infer +export type AddressExpiry = { earliest: number; latest: number } +export type AddressState = { stable: { serverConfiguration: string; rows: Record[] }; timers: Record } +const problem = (message: string) => Object.assign(new Error(message), { statusCode: 409 }) +const same = (a: unknown, b: unknown) => fingerprint(a) === fingerprint(b) +const bodyFor = (input: FirewallAddressMutation): Record => input.operation === "delete" ? {} : input.operation === "toggle" ? { disabled: input.disabled ? "yes" : "no" } : addressToRos({ ...input.fields, serverId: "", family: input.family }) + +/** Compare host, subnet and IPv4 range identities without DNS lookup or mapped-v6 coercion. */ +export function firewallAddressIdentity(raw: string) { + const range = raw.split("-") + if (range.length === 2 && range.every(ip => isIP(ip) === 4)) return `4:${ipv4ToInt(range[0])}:${ipv4ToInt(range[1])}` + const [ip, prefix, extra] = raw.split("/"), family = isIP(ip) + if (!family || extra !== undefined || prefix !== undefined && !/^\d+$/.test(prefix)) return raw.toLowerCase() + const bits = family === 4 ? 32 : 128, size = prefix === undefined ? bits : Number(prefix) + if (size < 0 || size > bits) return raw + const value = family === 4 ? BigInt(ipv4ToInt(ip)!) : ipv6ToInt(ip)! + const shift = BigInt(bits - size), start = value >> shift << shift + return `${family}:${start}:${start + (1n << shift) - 1n}` +} +function lifetime(value: string | undefined, dynamic?: string) { + if (!value || ["none-static", "none-dynamic"].includes(value)) return value || (observedBoolean(dynamic) === false ? "none-static" : "unknown") + const ms = observedDurationMs(value) + if (ms === null) throw problem("Не подтверждён timeout address-list") + return ms > 0 ? "finite" : observedBoolean(dynamic) === false ? "none-static" : "none-dynamic" +} +function normalized(row: Record) { + return Object.fromEntries(Object.entries(row).map(([key, value]) => [key, ["disabled", "dynamic", "invalid"].includes(key) ? observedBoolean(value) : key === "address" && typeof value === "string" ? firewallAddressIdentity(value) : value])) +} +export async function readFirewallAddressState(server: ServerRow, input: FirewallAddressMutation): Promise { + if (!server.enabled) throw problem("Сервер отключён") + const client = MikrotikClient.fromServer(server), path = firewallRestPath(input.family, "address-list"), start = Date.now() + const raw = await client.get(path), end = Date.now() + if (!Array.isArray(raw) || raw.length > 1000 || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || [".id", "list", "address"].some(key => typeof row[key] !== "string" || !row[key].trim()))) throw problem("Недостоверный каталог address-list") + const rows = raw as Record[] + if (new Set(rows.map(row => row[".id"])).size !== rows.length) throw problem("Повторяющиеся ID address-list") + const targets = rows.filter(row => input.operation === "create" ? row.list === input.fields.list && firewallAddressIdentity(row.address) === firewallAddressIdentity(input.fields.address) : row[".id"] === input.rosId) + if (targets.length > 50) throw problem("Слишком много совпадающих записей address-list") + for (const row of targets) for (const field of ["dynamic", "disabled"]) if (row[field] === undefined) { + const value = await client.post<{ ret?: string }>(`${path}/get`, { ".id": row[".id"], "value-name": field }) + if (typeof value?.ret === "string") row[field] = value.ret + } + const timers: AddressState["timers"] = {} + const stable = rows.map(row => { + const mode = lifetime(row.timeout, row.dynamic), remaining = mode === "finite" ? observedDurationMs(row.timeout)! : 0 + if (mode === "finite") { + if (!Number.isSafeInteger(end + remaining + 1000)) throw problem("Срок address-list выходит за допустимый диапазон") + timers[row[".id"]] = { earliest: start + remaining - 1000, latest: end + remaining + 1000 } + } + return { ...row, timeout: mode } + }) + return { stable: { serverConfiguration: fingerprint(server), rows: stable }, timers } +} +export function assertFirewallAddressTarget(input: FirewallAddressMutation, state: AddressState) { + if (input.operation === "create") return + const row = state.stable.rows.find(row => row[".id"] === input.rosId) + if (!row || observedBoolean(row.dynamic) === null) throw problem("Не подтверждена идентичность/происхождение address-list") +} +export function expiryMatches(expected: AddressExpiry | undefined, actual: AddressExpiry | undefined) { + return !expected ? !actual : !!actual && expected.earliest <= actual.latest && actual.earliest <= expected.latest +} +export function verifyFirewallAddressMutation(input: FirewallAddressMutation, before: AddressState, next: AddressState, commandWindow: AddressExpiry) { + if (before.stable.serverConfiguration !== next.stable.serverConfiguration) return false + const body = bodyFor(input), patch = { ...body } + if (patch.timeout) patch.timeout = lifetime(patch.timeout, patch.timeout === "none-static" ? "false" : "true") + let targetId: string + if (input.operation === "create") { + const added = next.stable.rows.filter(row => !before.stable.rows.some(previous => previous[".id"] === row[".id"])) + if (added.length !== 1 || !same(next.stable.rows.filter(row => row !== added[0]), before.stable.rows) || !Object.entries(patch).every(([key, value]) => same(normalized({ [key]: added[0][key] })[key], normalized({ [key]: value })[key]))) return false + targetId = added[0][".id"] + } else { + targetId = input.rosId + const expected = before.stable.rows.filter(row => input.operation !== "delete" || row[".id"] !== targetId).map(row => row[".id"] === targetId ? { ...row, ...patch, ...(patch.timeout ? { dynamic: patch.timeout === "none-static" ? "false" : "true" } : {}) } : row) + if (!same(next.stable.rows.map(normalized), expected.map(normalized))) return false + } + if (input.operation === "delete") return true + if (patch.timeout === "finite") { + const duration = observedDurationMs(body.timeout)! + return expiryMatches({ earliest: commandWindow.earliest + duration - 1000, latest: commandWindow.latest + duration + 1000 }, next.timers[targetId]) + } + if (patch.timeout) return !next.timers[targetId] + return input.operation === "create" ? !next.timers[targetId] : expiryMatches(before.timers[targetId], next.timers[targetId]) +} +export async function executeFirewallAddressMutation(input: FirewallAddressMutation, expectedFingerprint: string | undefined, expectedExpiry: AddressExpiry | undefined, deps: { + read: () => Promise; command: (body: Record) => Promise; recordRevision: () => Promise; beforeDispatch?: () => Promise +}) { + const before = await deps.read() + if (expectedFingerprint && fingerprint(before.stable) !== expectedFingerprint) throw problem("Address-list изменился после подготовки") + assertFirewallAddressTarget(input, before) + const body = bodyFor(input) + if (body.timeout) lifetime(body.timeout, body.timeout === "none-static" ? "false" : "true") + if (expectedFingerprint && input.operation !== "create" && !expiryMatches(expectedExpiry, before.timers[input.rosId])) throw problem("Срок address-list изменился после подтверждения") + await deps.beforeDispatch?.() + const start = Date.now(); await deps.command(body); const end = Date.now() + const next = await deps.read(), verified = verifyFirewallAddressMutation(input, before, next, { earliest: start, latest: end }) + const affectedId = input.operation === "create" ? next.stable.rows.find(row => !before.stable.rows.some(previous => previous[".id"] === row[".id"]))?.[".id"] : input.rosId + return { acknowledged: true, verified, afterFingerprint: fingerprint(next.stable), affectedId, afterExpiry: affectedId ? next.timers[affectedId] : undefined, revisionRecorded: verified ? await deps.recordRevision() : false } +} +export async function writeFirewallAddress(server: ServerRow, raw: FirewallAddressMutation, expectedFingerprint?: string, expectedExpiry?: AddressExpiry, beforeDispatch?: () => Promise) { + const input = firewallAddressMutationSchema.parse(raw) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], async () => { + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || !same(fresh, server)) throw problem("Источник address-list изменился") + const client = MikrotikClient.fromServer(fresh), path = firewallRestPath(input.family, "address-list") + return executeFirewallAddressMutation(input, expectedFingerprint, expectedExpiry, { + read: () => readFirewallAddressState(fresh, input), + command: body => input.operation === "create" ? client.put(path, body) : input.operation === "delete" ? client.delete(`${path}/${encodeRosId(input.rosId)}`) : client.patch(`${path}/${encodeRosId(input.rosId)}`, body), + beforeDispatch, + recordRevision: async () => (await captureAndAppendRevision({ serverId: fresh.id, section: "firewall", source: "apply", capture: async () => { + const state = await fetchFirewallState(fresh) + if (!state.snapshotFlagsVerified) throw problem("Flags ревизии Firewall не подтверждены") + return state.snapshot + } })) !== null, + }) + })) +} diff --git a/backend/src/services/firewall-live.ts b/backend/src/services/firewall-live.ts index a909ec84..4b45010b 100644 --- a/backend/src/services/firewall-live.ts +++ b/backend/src/services/firewall-live.ts @@ -1,4 +1,5 @@ import { pollFirewallChunks, type FirewallChunkFailure } from "./firewall-poll.js" +import { observedBoolean, observedInteger } from "./ospf-observed-values.js" import { eq } from "drizzle-orm" import { db } from "../db/index.js" import { servers } from "../db/schema.js" @@ -69,7 +70,7 @@ function dash(v: string | undefined): string { } function rosDisabled(v: string | undefined): boolean { - return v === "true" || v === "yes" + return observedBoolean(v) === true } function parseHits(raw: RosFirewallFilter): number { @@ -123,7 +124,7 @@ export function mapFirewallRule( comment: raw.comment ?? "", enabled: !rosDisabled(raw.disabled), hits: parseHits(raw), - log: raw.log === "true" || raw.log === "yes", + log: observedBoolean(raw.log) === true, logPrefix: raw["log-prefix"] ?? "", tlsHost: raw["tls-host"], layer7Proto: raw["layer7-protocol"], @@ -151,8 +152,19 @@ export function mapAddressList( } } +export function mapObservedFirewallRule(server: ServerRow, family: FirewallFamily, table: FirewallTable, raw: RosFirewallFilter, idx: number) { + const disabled = observedBoolean(raw.disabled) + return { ...mapFirewallRule(server, family, table, raw, idx), enabled: disabled === null ? null : !disabled, + dynamic: observedBoolean(raw.dynamic), invalid: observedBoolean(raw.invalid), hits: observedInteger(raw.packets), log: observedBoolean(raw.log), proto: raw.protocol?.trim() || null, + matches: { srcAddress: raw["src-address"] ?? null, dstAddress: raw["dst-address"] ?? null, srcAddressList: raw["src-address-list"] ?? null, dstAddressList: raw["dst-address-list"] ?? null, + srcPort: raw["src-port"] ?? null, dstPort: raw["dst-port"] ?? null, inInterface: raw["in-interface"] ?? null, outInterface: raw["out-interface"] ?? null, connectionState: raw["connection-state"] ?? null } } +} +export function mapObservedFirewallAddress(server: ServerRow, family: FirewallFamily, raw: RosFirewallAddressList, idx: number) { + return { ...mapAddressList(server, family, raw, idx), disabled: observedBoolean(raw.disabled), dynamic: observedBoolean(raw.dynamic) } +} + function rosYes(v: string | undefined): boolean { - return v === "true" || v === "yes" + return observedBoolean(v) === true } export function mapFirewallSnapshotRule( @@ -161,6 +173,7 @@ export function mapFirewallSnapshotRule( raw: RosFirewallFilter, ): FirewallLiveRule { return { + native: firewallWritableNative(raw as unknown as Record), rosId: raw[".id"] || "", dynamic: rosYes(raw.dynamic), family, @@ -191,6 +204,7 @@ export function mapFirewallSnapshotList( raw: RosFirewallAddressList, ): FirewallLiveList { return { + native: firewallWritableNative(raw as unknown as Record), rosId: raw[".id"] || "", dynamic: rosYes(raw.dynamic), family, @@ -202,6 +216,12 @@ export function mapFirewallSnapshotList( } } +/** Retain native body/matcher fields. IDs, runtime data and creation timestamps + * are excluded; disabled/log already have canonical modeled fields. */ +export function firewallWritableNative(raw: Record) { + return Object.fromEntries(Object.entries(raw).filter(([key, value]) => /^[a-z][a-z0-9-]{0,99}$/.test(key) && !["dynamic", "invalid", "bytes", "packets", "creation-time", "disabled", "log"].includes(key) && typeof value === "string")) +} + export async function fetchFirewallState(server: ServerRow, allowPartial = false): Promise<{ rules: FirewallRuleDto[] addressLists: FirewallAddressListDto[] @@ -209,25 +229,32 @@ export async function fetchFirewallState(server: ServerRow, allowPartial = false liveLists: FirewallLiveList[] snapshot: FirewallSnapshot failures: FirewallChunkFailure[] + observedRules: ReturnType[] + observedAddressLists: ReturnType[] + snapshotFlagsVerified: boolean }> { const client = MikrotikClient.fromServer(server) const { ruleChunks, listChunks, failures } = await pollFirewallChunks(client) if (!allowPartial && failures.length > 0) throw new Error("Не удалось полностью прочитать Firewall: " + failures.map(f => f.family + "/" + f.table + ": " + f.error).join("; ")) const rules: FirewallRuleDto[] = [] + const observedRules: ReturnType[] = [] const liveRules: FirewallLiveRule[] = [] for (const chunk of ruleChunks) { chunk.raw.forEach((row, idx) => { rules.push(mapFirewallRule(server, chunk.family, chunk.table, row, idx)) + observedRules.push(mapObservedFirewallRule(server, chunk.family, chunk.table, row, idx)) liveRules.push(mapFirewallSnapshotRule(chunk.family, chunk.table, row)) }) } const addressLists: FirewallAddressListDto[] = [] + const observedAddressLists: ReturnType[] = [] const liveLists: FirewallLiveList[] = [] for (const chunk of listChunks) { chunk.raw.forEach((row, idx) => { addressLists.push(mapAddressList(server, chunk.family, row, idx)) + observedAddressLists.push(mapObservedFirewallAddress(server, chunk.family, row, idx)) liveLists.push(mapFirewallSnapshotList(chunk.family, row)) }) } @@ -238,6 +265,8 @@ export async function fetchFirewallState(server: ServerRow, allowPartial = false liveRules, liveLists, failures, + observedRules, observedAddressLists, + snapshotFlagsVerified: observedRules.every(row => row.enabled !== null && row.dynamic !== null && row.log !== null) && observedAddressLists.every(row => row.disabled !== null && row.dynamic !== null), snapshot: canonicalFirewallSnapshot({ rules: liveRules.filter((r) => !r.dynamic), addressLists: liveLists.filter((e) => !e.dynamic), @@ -255,28 +284,31 @@ export async function fetchServerFirewall(server: ServerRow): Promise<{ export async function captureFirewallSnapshot(server: ServerRow): Promise { const state = await fetchFirewallState(server) + if (!state.snapshotFlagsVerified) throw new Error("Flags снимка Firewall не подтверждены") return state.snapshot } -export async function listFirewallAll(): Promise<{ +export async function listFirewallAll(selectedServers?: readonly ServerRow[], includeObservations = false, recordRevision = true): Promise<{ rules: FirewallRuleDto[] addressLists: FirewallAddressListDto[] failures: Array & { serverId: string; serverName: string; error: string }> + observed?: { rules: ReturnType[]; addressLists: ReturnType[] } }> { - const allServers = await db.select().from(servers).where(eq(servers.enabled, true)) + const allServers = selectedServers ? selectedServers.filter(server => server.enabled !== false) : await db.select().from(servers).where(eq(servers.enabled, true)) const perServer = await Promise.all(allServers.map(async server => { const context = { serverId: String(server.id), serverName: server.name || server.host } try { const state = await fetchFirewallState(server, true) - if (state.failures.length === 0) { + if (recordRevision && state.failures.length === 0 && state.snapshotFlagsVerified) { await captureAndAppendRevision({ serverId: server.id, section: "firewall", source: "observed", capture: async () => state.snapshot }) } - return { rules: state.rules, addressLists: state.addressLists, failures: state.failures.map(f => ({ ...context, ...f })) } + return { rules: state.rules, addressLists: state.addressLists, observedRules: state.observedRules, observedAddressLists: state.observedAddressLists, failures: state.failures.map(f => ({ ...context, ...f })) } } catch (e) { - return { rules: [] as FirewallRuleDto[], addressLists: [] as FirewallAddressListDto[], failures: [{ ...context, error: e instanceof Error ? e.message : "Ошибка опроса Firewall" }] } + return { rules: [] as FirewallRuleDto[], addressLists: [] as FirewallAddressListDto[], observedRules: [], observedAddressLists: [], failures: [{ ...context, error: e instanceof Error ? e.message : "Ошибка опроса Firewall" }] } } })) - return { rules: perServer.flatMap(r => r.rules), addressLists: perServer.flatMap(r => r.addressLists), failures: perServer.flatMap(r => r.failures) } + return { rules: perServer.flatMap(r => r.rules), addressLists: perServer.flatMap(r => r.addressLists), failures: perServer.flatMap(r => r.failures), + ...(includeObservations ? { observed: { rules: perServer.flatMap(r => r.observedRules), addressLists: perServer.flatMap(r => r.observedAddressLists) } } : {}) } } export { firewallRestPath, FAMILIES, TABLES } diff --git a/backend/src/services/firewall-poll.test.ts b/backend/src/services/firewall-poll.test.ts index d3ba9524..edaaaa77 100644 --- a/backend/src/services/firewall-poll.test.ts +++ b/backend/src/services/firewall-poll.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict" import { pollFirewallChunks } from "./firewall-poll.js" -import { fetchFirewallState, captureFirewallSnapshot } from "./firewall-live.js" +import { fetchFirewallState, captureFirewallSnapshot, mapObservedFirewallRule, mapObservedFirewallAddress, listFirewallAll } from "./firewall-live.js" import { MikrotikClient } from "./mikrotik.js" const client = { @@ -33,5 +33,28 @@ try { const state = await fetchFirewallState(server, true) assert.equal(state.rules.length, 7) assert.deepEqual(state.failures, partial.failures) + assert.equal(state.observedRules[0].enabled, null); assert.equal(state.observedRules[0].dynamic, null) + assert.equal(state.observedRules[0].hits, 1); assert.equal(state.snapshotFlagsVerified, false) + const known = mapObservedFirewallRule(server, "ip", "filter", { ".id": "*1", chain: "forward", action: "accept", disabled: "no", dynamic: "false", log: "false", packets: "0", "src-address-list": "sources", "dst-address": "192.0.2.0/24", "in-interface": "ether1", "out-interface": "ether2" }, 0) + assert.equal(known.enabled, true); assert.equal(known.hits, 0); assert.equal(known.log, false) + assert.equal(known.matches.srcAddress, null); assert.equal(known.matches.srcAddressList, "sources") + assert.equal(known.matches.inInterface, "ether1"); assert.equal(known.matches.outInterface, "ether2") + assert.equal(known.proto, null) + assert.equal(mapObservedFirewallRule(server, "ip", "filter", { ".id": "*1", chain: "f", action: "accept", packets: "12bad" }, 0).hits, null) + assert.equal(mapObservedFirewallAddress(server, "ip", { ".id": "*a", list: "l", address: "192.0.2.1" }, 0).disabled, null) + const legacy = await listFirewallAll([server]) + assert.deepEqual(Object.keys(legacy).sort(), ["addressLists", "failures", "rules"]) + assert.equal(legacy.rules[0].enabled, true); assert.equal(legacy.rules[0].hits, 1) + assert.equal(legacy.failures.length, 1) + assert.deepEqual(await listFirewallAll([], true), { rules: [], addressLists: [], failures: [], observed: { rules: [], addressLists: [] } }) + assert.deepEqual(await listFirewallAll([{ ...server, enabled: false }], true), { rules: [], addressLists: [], failures: [], observed: { rules: [], addressLists: [] } }) + MikrotikClient.fromServer = () => ({ getFirewallRules: async () => [{ ".id": "*known", chain: "fixture", action: "accept", disabled: "false", dynamic: "false" }], getFirewallAddressList: async () => [] }) as unknown as ReturnType + await assert.rejects(captureFirewallSnapshot(server), /Flags/) + const unknownLog = await fetchFirewallState(server) + assert.equal(unknownLog.observedRules[0].log, null); assert.equal(unknownLog.snapshotFlagsVerified, false, "Unknown log cannot become a confirmed revision flag") } finally { MikrotikClient.fromServer = fromServer } +for (const rows of [[null], [{ ".id": "*1" }], [{ ".id": "*1", chain: "f", action: "accept" }, { ".id": "*1", chain: "f", action: "drop" }]]) { + const invalid = await pollFirewallChunks({ getFirewallRules: async () => rows as never, getFirewallAddressList: async () => [] }) + assert.equal(invalid.failures.length, 8) +} console.log("Firewall polling: mixed/empty/all-failed/malformed and strict snapshot rejection passed") diff --git a/backend/src/services/firewall-poll.ts b/backend/src/services/firewall-poll.ts index 3d8acc38..52bf7cbf 100644 --- a/backend/src/services/firewall-poll.ts +++ b/backend/src/services/firewall-poll.ts @@ -9,7 +9,8 @@ export async function pollFirewallChunks(client: { async function read(family: FirewallFamily, table: FirewallChunkFailure["table"], fetch: () => Promise): Promise { try { const rows = await fetch() - if (!Array.isArray(rows)) throw new Error("RouterOS вернул некорректный список") + const required = table === "address-list" ? [".id", "list", "address"] : [".id", "chain", "action"] + if (!Array.isArray(rows) || rows.length > 10000 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row) || required.some(key => typeof (row as Record)[key] !== "string" || !String((row as Record)[key]).trim())) || new Set(rows.map(row => (row as Record)[".id"])).size !== rows.length) throw new Error("RouterOS вернул некорректный список или идентификаторы Firewall") return rows } catch (e) { failures.push({ family, table, error: e instanceof Error ? e.message : "Ошибка опроса Firewall" }) diff --git a/backend/src/services/firewall-revision-restore.test.ts b/backend/src/services/firewall-revision-restore.test.ts new file mode 100644 index 00000000..82da332a --- /dev/null +++ b/backend/src/services/firewall-revision-restore.test.ts @@ -0,0 +1,52 @@ +import assert from "node:assert/strict" +import { canonicalFirewallSnapshot } from "./entity-snapshots.js" +import { mapFirewallSnapshotRule } from "./firewall-live.js" +import { validateFirewallRevision, firewallRestoreOps, firewallRestoreProof, firewallRestoreSnapshot, verifyFirewallRestore, executeVerifiedFirewallRestoreOps, firewallRestoreCommandCount, type FirewallRestoreState } from "./firewall-revision-restore.js" +import type { MikrotikClient } from "./mikrotik.js" +const old = { ".id": "*A", chain: "fixture", action: "accept", disabled: "false", dynamic: "false", log: "false", invalid: "false", "connection-mark": "old", packets: "1" } +const dynamic = { ...old, ".id": "*D", dynamic: "true", "connection-mark": "dynamic" } +const baseline: FirewallRestoreState = { serverConfiguration: "server", chunks: (["ip", "ip6"] as const).flatMap(family => (["filter", "nat", "mangle", "raw", "address-list"] as const).map(table => ({ family, table, rows: family === "ip" && table === "filter" ? [dynamic, old] : [] }))) } +const desiredRow = mapFirewallSnapshotRule("ip", "filter", { ...old, "connection-mark": "opaque", comment: "desired", packets: "1000" }) +const desired = canonicalFirewallSnapshot({ rules: [desiredRow, desiredRow], addressLists: [{ family: "ip6", list: "fixture", address: "2001:db8::1/128", disabled: true }] }) +assert.deepEqual(validateFirewallRevision(desired), desired) +assert.equal(desired.rules[0].native?.["connection-mark"], "opaque"); assert.equal("packets" in desired.rules[0].native!, false) +for (const payload of [null, {}, { rules: [], addressLists: "bad" }, { ...desired, rules: [{ ...desired.rules[0], disabled: "false" }] }, { ...desired, rules: [{ ...desired.rules[0], native: { ".id": "*other" } }] }, { ...desired, addressLists: [{ ...desired.addressLists[0], timeout: "1m" }] }]) assert.throws(() => validateFirewallRevision(payload)) +let state = structuredClone(baseline), nextId = 10, calls: string[] = [], ignored = false, lost = false +const locate = (path: string) => { const match = /^\/(ip|ipv6)\/firewall\/([^/]+)(?:\/(.*))?$/.exec(decodeURIComponent(path))!; return { chunk: state.chunks.find(row => row.family === (match[1] === "ip" ? "ip" : "ip6") && row.table === match[2])!, id: match[3] } } +const fake = { + get: async (path: string) => structuredClone(locate(path).chunk.rows), + post: async () => ({ ret: "false" }), + put: async (path: string, fields: Record) => { calls.push("put:" + path); if (lost) throw Error("response lost"); if (!ignored) locate(path).chunk.rows.push({ ".id": "*N" + nextId++, dynamic: "false", disabled: "false", log: "false", invalid: "false", ...Object.fromEntries(Object.entries(fields).map(([key, value]) => [key, ["disabled", "log"].includes(key) ? value === "yes" ? "true" : "false" : value])) }) }, + delete: async (path: string) => { calls.push("delete:" + path); const { chunk, id } = locate(path); chunk.rows = chunk.rows.filter(row => row[".id"] !== id) }, + patch: async (path: string, fields: Record) => { calls.push("patch:" + path); const { chunk, id } = locate(path); Object.assign(chunk.rows.find(row => row[".id"] === id)!, { disabled: fields.disabled === "yes" ? "true" : "false" }) }, +} as unknown as MikrotikClient +const ops = firewallRestoreOps(desired, baseline) +assert.equal(ops.filter(op => op.op === "delete").length, 1); assert.equal(ops.filter(op => op.op === "put").length, 3) +await assert.rejects(executeVerifiedFirewallRestoreOps(fake, baseline, [{ op: "post", path: "/system/reboot", body: {} }]), /Недопустимый/) +assert.equal(calls.length, 0) +const result = await executeVerifiedFirewallRestoreOps(fake, baseline, ops) +assert.equal(result.verified, true); assert.equal(result.acknowledgedCommands, result.completedCommands); assert.equal(result.completedCommands, firewallRestoreCommandCount(ops)) +assert.ok(calls.slice(0, 3).every(call => call.startsWith("put:"))); assert.ok(calls[3].startsWith("delete:")); assert.equal(calls.filter(call => call.startsWith("patch:")).length, 2) +assert.equal(verifyFirewallRestore(desired, baseline, state), true) +assert.equal(state.chunks[0].rows[0][".id"], "*D"); assert.equal(state.chunks[0].rows.filter(row => row.dynamic === "false").length, 2) +assert.equal(firewallRestoreSnapshot(state).rules[0].native?.["connection-mark"], "opaque") +const counters = structuredClone(state); counters.chunks[0].rows[0].packets = "999"; assert.deepEqual(firewallRestoreProof(counters), firewallRestoreProof(state)) +const invalidRule = structuredClone(state); invalidRule.chunks[0].rows[1].invalid = "true"; assert.equal(verifyFirewallRestore(desired, baseline, invalidRule), false) +const changed = structuredClone(state); changed.chunks[0].rows[1]["connection-mark"] = "foreign"; assert.equal(verifyFirewallRestore(desired, baseline, changed), false) +const clearingBefore = structuredClone(state), empty = canonicalFirewallSnapshot({}) +assert.equal((await executeVerifiedFirewallRestoreOps(fake, clearingBefore, firewallRestoreOps(empty, clearingBefore))).verified, true) +assert.equal(verifyFirewallRestore(empty, clearingBefore, state), true); assert.deepEqual(state.chunks[0].rows.map(row => row[".id"]), ["*D"]) +state = structuredClone(baseline); calls = []; ignored = true +const ignoredResult = await executeVerifiedFirewallRestoreOps(fake, baseline, ops) +assert.equal(ignoredResult.verified, false); assert.equal(ignoredResult.completedCommands, 0); assert.equal(ignoredResult.acknowledgedCommands, 1); assert.equal(calls.length, 1); assert.ok(!calls.some(call => call.startsWith("delete:"))); assert.deepEqual(state, baseline) +ignored = false; lost = true; calls = [] +await assert.rejects(executeVerifiedFirewallRestoreOps(fake, baseline, ops), /response lost/); assert.equal(calls.length, 1) +lost = false; calls = [] +await assert.rejects(executeVerifiedFirewallRestoreOps(fake, baseline, ops, async () => false), /до первой команды/) +assert.deepEqual(calls, []) +const interrupted = await executeVerifiedFirewallRestoreOps(fake, baseline, ops, async () => calls.length === 0) +assert.deepEqual(interrupted, { verified: false, completedCommands: 1, acknowledgedCommands: 1 }) +assert.deepEqual(calls, ["put:/ipv6/firewall/address-list"]) +assert.ok(state.chunks[0].rows.some(row => row[".id"] === "*A")) +assert.equal(state.chunks.find(chunk => chunk.family === "ip6" && chunk.table === "address-list")?.rows[0]?.disabled, "true") +console.log("Firewall restore: native matcher capture, strict revisions, duplicate order, disabled staging before deletion, activation read-back, dynamic preservation, counters, ignored/lost command stop and no retry passed") diff --git a/backend/src/services/firewall-revision-restore.ts b/backend/src/services/firewall-revision-restore.ts new file mode 100644 index 00000000..0cc6a753 --- /dev/null +++ b/backend/src/services/firewall-revision-restore.ts @@ -0,0 +1,164 @@ +import type { RosFirewallFilter, RosFirewallAddressList } from "../types/server.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { getRevisionById, fingerprintPayload, captureAndAppendRevision } from "./config-revisions.js" +import { MikrotikClient, firewallRestPath } from "./mikrotik.js" +import { observedBoolean, observedDurationMs } from "./ospf-observed-values.js" +import { fingerprint } from "../modules/mcp/security.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { canonicalFirewallSnapshot, planExactFirewallRestore, firewallRuleBody, firewallListBody, type FirewallSnapshot, type FirewallFamily, type FirewallTable, type RosWriteOp } from "./entity-snapshots.js" +import { mapFirewallSnapshotRule, mapFirewallSnapshotList, mapFirewallRule, mapAddressList } from "./firewall-live.js" +import { firewallAddressIdentity } from "./firewall-address-write.js" +import { executeRosOps } from "./ros-ops.js" + +const problem = (message: string) => Object.assign(new Error(message), { statusCode: 409 }) +const families = ["ip", "ip6"] as const, tables = ["filter", "nat", "mangle", "raw", "address-list"] as const +export type FirewallRestoreChunk = { family: FirewallFamily; table: FirewallTable | "address-list"; rows: Record[] } +export interface FirewallRestoreState { serverConfiguration: string; chunks: FirewallRestoreChunk[] } +export function validateFirewallRevision(payload: unknown): FirewallSnapshot { + if (!payload || typeof payload !== "object" || Array.isArray(payload) || !Array.isArray((payload as FirewallSnapshot).rules) || !Array.isArray((payload as FirewallSnapshot).addressLists) || JSON.stringify(payload).length > 1000000) throw problem("Повреждён снимок Firewall") + const source = payload as FirewallSnapshot + if (source.rules.length + source.addressLists.length > 50) throw problem("Ревизия Firewall превышает 50 записей") + let desired: FirewallSnapshot + try { desired = canonicalFirewallSnapshot(source) } catch { throw problem("Повреждены поля ревизии Firewall") } + if (fingerprint(desired) !== fingerprint(source)) throw problem("Ревизия Firewall не соответствует канонической схеме") + for (const row of [...desired.rules, ...desired.addressLists]) { + if (row.native && (Array.isArray(row.native) || Object.entries(row.native).some(([key, value]) => typeof value !== "string" || value.length > 10000 || !/^[a-z][a-z0-9-]{0,99}$/.test(key) || ["dynamic", "invalid", "bytes", "packets", "creation-time"].includes(key)))) throw problem("Недопустимые native-поля Firewall") + } + if (desired.rules.some(row => !row.chain.trim() || !row.action.trim()) || desired.addressLists.some(row => !row.list.trim() || !row.address.trim() || !["", "none-static", "0", "0s", "00:00:00"].includes(row.timeout))) throw problem("Ревизия должна содержать правила и постоянные address-list") + return desired +} +export async function readFirewallRestoreRevision(serverId: number, revisionId: string) { + const row = await getRevisionById(revisionId, { serverId, section: "firewall" }) + if (!row || row.fingerprint !== fingerprintPayload(row.payload)) throw problem("Ревизия отсутствует, принадлежит другой области или повреждена") + const desired = validateFirewallRevision(row.payload) + return { desired, revisionFingerprint: fingerprint({ id: row.id, serverId: row.serverId, section: row.section, payload: row.payload, fingerprint: row.fingerprint }) } +} +async function readRestoreChunk(client: MikrotikClient, family: FirewallFamily, table: FirewallTable | "address-list"): Promise { + const path = firewallRestPath(family, table), raw = await client.get(path) + const required = table === "address-list" ? [".id", "list", "address"] : [".id", "chain", "action"] + if (!Array.isArray(raw) || raw.length > 150 || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || required.some(key => typeof row[key] !== "string" || !row[key].trim()) || !/^\*[A-Za-z0-9]+$/.test(row[".id"]))) throw problem("Неполный native-каталог Firewall") + if ( new Set(raw.map(row => row[".id"])).size !== raw.length) throw problem("Слишком большой или неоднозначный каталог Firewall") + const rows = raw as Record[] + for (const row of rows) for (const key of table === "address-list" ? ["dynamic", "disabled"] : ["dynamic", "disabled", "log", "invalid"]) { + if (row[key] === undefined) { + const value = await client.post<{ ret?: string }>(`${path}/get`, { ".id": row[".id"], "value-name": key }) + if (typeof value?.ret === "string") row[key] = value.ret + } + if (observedBoolean(row[key]) === null) throw problem("Не подтверждены native flags Firewall") + } + return { family, table, rows } +} +export async function readFirewallRestoreState(server: ServerRow): Promise { + if (!server.enabled) throw problem("Сервер отключён") + const client = MikrotikClient.fromServer(server), chunks: FirewallRestoreChunk[] = [] + for (const family of families) for (const table of tables) chunks.push(await readRestoreChunk(client, family, table)) + if (chunks.reduce((sum, chunk) => sum + chunk.rows.length, 0) > 100) throw problem("Каталог Firewall превышает 100 записей") + return { serverConfiguration: fingerprint(server), chunks } +} +export function firewallRestoreProof(state: FirewallRestoreState) { + return { ...state, chunks: state.chunks.map(chunk => ({ ...chunk, rows: chunk.rows.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !["bytes", "packets", "invalid"].includes(key)).map(([key, value]) => [key, ["disabled", "dynamic", "log"].includes(key) ? String(observedBoolean(value)) : chunk.table === "address-list" && row.dynamic !== "false" && row.dynamic !== "no" && key === "timeout" && (observedDurationMs(value) ?? 0) > 0 ? "finite" : value]))) })) } +} +export function firewallRestoreSnapshot(state: FirewallRestoreState): FirewallSnapshot { + return canonicalFirewallSnapshot({ + rules: state.chunks.filter(chunk => chunk.table !== "address-list").flatMap(chunk => chunk.rows.filter(row => observedBoolean(row.dynamic) === false).map(row => mapFirewallSnapshotRule(chunk.family, chunk.table as FirewallTable, row as unknown as RosFirewallFilter))), + addressLists: state.chunks.filter(chunk => chunk.table === "address-list").flatMap(chunk => chunk.rows.filter(row => observedBoolean(row.dynamic) === false).map(row => mapFirewallSnapshotList(chunk.family, row as unknown as RosFirewallAddressList))), + }) +} +export function firewallRestoreOps(desired: FirewallSnapshot, before: FirewallRestoreState) { + return planExactFirewallRestore(desired, { + rules: before.chunks.filter(chunk => chunk.table !== "address-list").flatMap(chunk => chunk.rows.map(row => mapFirewallSnapshotRule(chunk.family, chunk.table as FirewallTable, row as unknown as RosFirewallFilter))), + addressLists: before.chunks.filter(chunk => chunk.table === "address-list").flatMap(chunk => chunk.rows.map(row => mapFirewallSnapshotList(chunk.family, row as unknown as RosFirewallAddressList))), + }) +} +function nativeValue(key: string, value = "") { + if (["disabled", "log"].includes(key)) return observedBoolean(value) + if (key === "timeout" && ["", "none-static", "0s", "0", "00:00:00"].includes(value)) return "permanent" + if (["address", "src-address", "dst-address"].includes(key)) return value.startsWith("!") ? "!" + firewallAddressIdentity(value.slice(1)) : firewallAddressIdentity(value) + return value +} +export function verifyFirewallRestore(desired: FirewallSnapshot, before: FirewallRestoreState, after: FirewallRestoreState) { + if (before.serverConfiguration !== after.serverConfiguration) return false + for (const chunk of after.chunks) { + const previous = before.chunks.find(row => row.family === chunk.family && row.table === chunk.table) + if (!previous) return false + const dynamic = (state: FirewallRestoreState) => firewallRestoreProof(state).chunks.find(row => row.family === chunk.family && row.table === chunk.table)!.rows.filter(row => observedBoolean(row.dynamic) === true) + if (fingerprint(dynamic(before)) !== fingerprint(dynamic(after))) return false + const actual = chunk.rows.filter(row => observedBoolean(row.dynamic) === false) + if (chunk.table !== "address-list" && actual.some(row => observedBoolean(row.invalid) !== false)) return false + const expected = chunk.table === "address-list" ? desired.addressLists.filter(row => row.family === chunk.family).map(firewallListBody) : desired.rules.filter(row => row.family === chunk.family && row.table === chunk.table).map(firewallRuleBody) + if (actual.length !== expected.length || expected.some((body, index) => Object.entries(body).some(([key, value]) => nativeValue(key, actual[index][key]) !== nativeValue(key, value)))) return false + const firstStatic = chunk.rows.findIndex(row => observedBoolean(row.dynamic) === false) + if (firstStatic >= 0 && chunk.rows.slice(firstStatic).some(row => observedBoolean(row.dynamic) !== false)) return false + } + return after.chunks.length === before.chunks.length +} +/** Read each stage back before the next command; acknowledgement alone cannot + * advance a bulk replacement. Never resend an unconfirmed native command. */ +export async function executeVerifiedFirewallRestoreOps(client: MikrotikClient, before: FirewallRestoreState, ops: RosWriteOp[], beforeDispatch?: () => Promise) { + for (const op of ops) { + if (op.op === "put" ? !/^\/(ip|ipv6)\/firewall\/(filter|nat|mangle|raw|address-list)$/.test(op.path) || observedBoolean(op.body.disabled) === null : op.op !== "delete" || !/^\/(ip|ipv6)\/firewall\/(filter|nat|mangle|raw|address-list)\/\*[A-Za-z0-9]+$/.test(op.path)) throw problem("Недопустимый набор стадий Firewall restore") + } + const expected: FirewallRestoreState = structuredClone(before) + let completedCommands = 0, acknowledgedCommands = 0 + const run = async (op: RosWriteOp) => { + const match = /^\/(ip|ipv6)\/firewall\/(filter|nat|mangle|raw|address-list)(?:\/(\*[A-Za-z0-9]+))?$/.exec(op.path) + if (!match || !["delete", "put", "patch"].includes(op.op)) throw problem("Недопустимая стадия Firewall restore") + const family = match[1] === "ip" ? "ip" : "ip6", table = match[2] as FirewallTable | "address-list" + const index = expected.chunks.findIndex(chunk => chunk.family === family && chunk.table === table) + if (index < 0) throw problem("Стадия вне исходного снимка") + const proof = (chunk: FirewallRestoreChunk) => firewallRestoreProof({ serverConfiguration: before.serverConfiguration, chunks: [chunk] }).chunks[0] + const current = await readRestoreChunk(client, family, table) + if (fingerprint(proof(current)) !== fingerprint(proof(expected.chunks[index]))) return { verified: false } + if (beforeDispatch && !await beforeDispatch()) { + if (!acknowledgedCommands) throw problem("Привязки prefix-list OSPF изменились до первой команды Firewall") + return { verified: false } + } + await executeRosOps(client, [op]) + acknowledgedCommands++ + const next = await readRestoreChunk(client, family, table) + if (op.op === "delete") { + const wanted = { ...current, rows: current.rows.filter(row => row[".id"] !== match[3]) } + if (wanted.rows.length === current.rows.length || fingerprint(proof(next)) !== fingerprint(proof(wanted))) return { verified: false } + } else if (op.op === "put") { + const added = next.rows.filter(row => !current.rows.some(previous => previous[".id"] === row[".id"])) + if (added.length !== 1 || observedBoolean(added[0].dynamic) !== false || table !== "address-list" && observedBoolean(added[0].invalid) !== false || next.rows.at(-1) !== added[0] + || fingerprint(proof({ ...next, rows: next.rows.slice(0, -1) })) !== fingerprint(proof(current)) + || Object.entries(op.body).some(([key, value]) => nativeValue(key, added[0][key]) !== nativeValue(key, value))) return { verified: false } + } + else if (op.op === "patch") { + const wanted = { ...current, rows: current.rows.map(row => row[".id"] === match[3] ? { ...row, ...op.body } : row) } + if (!current.rows.some(row => row[".id"] === match[3]) || fingerprint(proof(next)) !== fingerprint(proof(wanted))) return { verified: false } + } + expected.chunks[index] = next + completedCommands++ + return { verified: true, addedId: op.op === "put" ? next.rows.find(row => !current.rows.some(previous => previous[".id"] === row[".id"]))?.[".id"] : undefined } + } + const staged: Array<{ path: string; id: string; disabled: string }> = [] + for (const op of ops.filter(op => op.op === "put")) { + if (op.op !== "put") continue + const result = await run({ ...op, body: { ...op.body, disabled: "yes" } }) + if (!result.verified || !result.addedId) return { verified: false, completedCommands, acknowledgedCommands } + staged.push({ path: op.path, id: result.addedId, disabled: op.body.disabled }) + } + for (const op of ops.filter(op => op.op === "delete")) if (!(await run(op)).verified) return { verified: false, completedCommands, acknowledgedCommands } + for (const row of staged) if (observedBoolean(row.disabled) === false && !(await run({ op: "patch", path: row.path + "/" + row.id, body: { disabled: "no" } })).verified) return { verified: false, completedCommands, acknowledgedCommands } + return { verified: true, completedCommands, acknowledgedCommands } +} +export function firewallRestoreCommandCount(ops: RosWriteOp[]) { return ops.length + ops.filter(op => op.op === "put" && observedBoolean(op.body.disabled) === false).length } +export async function restoreFirewallRevision(server: ServerRow, revisionId: string, expectedBefore?: string, expectedRevision?: string, checkDependencies?: () => Promise) { + return withApplicationMutationLock(() => withConfigMutationLock([server.id], async () => { + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || fingerprint(fresh) !== fingerprint(server)) throw problem("Источник Firewall изменился") + const revision = await readFirewallRestoreRevision(server.id, revisionId), before = await readFirewallRestoreState(fresh) + if (expectedRevision && revision.revisionFingerprint !== expectedRevision || expectedBefore && fingerprint(firewallRestoreProof(before)) !== expectedBefore) throw problem("Ревизия или Firewall изменились после подготовки") + const ops = firewallRestoreOps(revision.desired, before) + const staged = await executeVerifiedFirewallRestoreOps(MikrotikClient.fromServer(fresh), before, ops, checkDependencies) + if (!staged.verified) return { verified: false, acknowledged: staged.acknowledgedCommands > 0, commandCount: firewallRestoreCommandCount(ops), completedCommands: staged.completedCommands, acknowledgedCommands: staged.acknowledgedCommands, revisionRecorded: false } + const after = await readFirewallRestoreState(fresh) + const verified = verifyFirewallRestore(revision.desired, before, after) && (!checkDependencies || await checkDependencies()) + const revisionRecorded = verified && (await captureAndAppendRevision({ serverId: fresh.id, section: "firewall", source: "rollback", capture: async () => firewallRestoreSnapshot(after) })) !== null + const catalog = { rules: after.chunks.filter(chunk => chunk.table !== "address-list").flatMap(chunk => chunk.rows.map((row, index) => mapFirewallRule(fresh, chunk.family, chunk.table as FirewallTable, row as unknown as RosFirewallFilter, index))), addressLists: after.chunks.filter(chunk => chunk.table === "address-list").flatMap(chunk => chunk.rows.map((row, index) => mapAddressList(fresh, chunk.family, row as unknown as RosFirewallAddressList, index))) } + return { verified, acknowledged: staged.acknowledgedCommands > 0, commandCount: firewallRestoreCommandCount(ops), completedCommands: staged.completedCommands, acknowledgedCommands: staged.acknowledgedCommands, afterFingerprint: fingerprint(firewallRestoreProof(after)), revisionRecorded, catalog } + })) +} diff --git a/backend/src/services/firewall-rule-input.ts b/backend/src/services/firewall-rule-input.ts new file mode 100644 index 00000000..4d449161 --- /dev/null +++ b/backend/src/services/firewall-rule-input.ts @@ -0,0 +1,73 @@ +import { z } from "zod" +const FamilySchema = z.enum(["ip", "ip6"]) +const TableSchema = z.enum(["filter", "nat", "mangle", "raw"]) +const NativeIdSchema = z.string().regex(/^\*[A-Za-z0-9]+$/).max(100) + +export const RuleKeySchema = z.object({ + serverId: z.string().min(1), + family: FamilySchema, + table: TableSchema, + rosId: NativeIdSchema, +}) + +export const RuleWriteSchema = z.object({ + serverId: z.string().min(1), + family: FamilySchema, + table: TableSchema, + rosId: NativeIdSchema.optional(), + chain: z.string().min(1), + action: z.string().min(1), + protocol: z.string().optional(), + srcAddress: z.string().optional(), + dstAddress: z.string().optional(), + srcAddressList: z.string().optional(), + dstAddressList: z.string().optional(), + srcPort: z.string().optional(), + dstPort: z.string().optional(), + inInterface: z.string().optional(), + outInterface: z.string().optional(), + connectionState: z.string().optional(), + comment: z.string().optional(), + disabled: z.boolean().optional(), + log: z.boolean().optional(), + logPrefix: z.string().optional(), + tlsHost: z.string().optional(), + layer7Proto: z.string().optional(), +}) + +export const RulePatchSchema = RuleKeySchema.extend({ + disabled: z.boolean(), +}) + + +function toRosBody(obj: Record): Record { + const out: Record = {} + for (const [k, v] of Object.entries(obj)) { + if (v !== undefined && v !== "") out[k] = v + } + return out +} + +export function ruleToRos(d: z.infer): Record { + return toRosBody({ + chain: d.chain, + action: d.action, + protocol: d.protocol && d.protocol !== "all" ? d.protocol : undefined, + "src-address": d.srcAddress, + "dst-address": d.dstAddress, + "src-address-list": d.srcAddressList, + "dst-address-list": d.dstAddressList, + "src-port": d.srcPort, + "dst-port": d.dstPort, + "in-interface": d.inInterface, + "out-interface": d.outInterface, + "connection-state": d.connectionState, + comment: d.comment, + disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined, + log: d.log === true ? "yes" : d.log === false ? "no" : undefined, + "log-prefix": d.logPrefix, + "tls-host": d.tlsHost, + "layer7-protocol": d.layer7Proto, + }) +} + diff --git a/backend/src/services/firewall-rule-move.test.ts b/backend/src/services/firewall-rule-move.test.ts new file mode 100644 index 00000000..3ea3f96f --- /dev/null +++ b/backend/src/services/firewall-rule-move.test.ts @@ -0,0 +1,45 @@ +import assert from "node:assert/strict" +import { MikrotikClient } from "./mikrotik.js" +import { firewallMoveSchema, movedFirewallRows, readFirewallMoveState, executeFirewallMove } from "./firewall-rule-move.js" +import { fingerprint } from "../modules/mcp/security.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" + +const server = { id: 1, enabled: true } as ServerRow +const input = firewallMoveSchema.parse({ family: "ip", table: "filter", rosId: "*1", destinationRosId: "*3" }) +const original = MikrotikClient.fromServer +let rows = [1, 2, 3].map(id => ({ ".id": "*" + id, chain: "fixture", action: "accept", disabled: "true", dynamic: "false", packets: "0", "connection-mark": "keep-exactly" })) +let gets = 0, commands = 0, omitDynamic = false, unknownDynamic = false, ignore = false, missingChain = false +const client = { + get: async () => { gets++; return rows.map(row => { const copy: Record = { ...row, packets: String(gets) }; if (omitDynamic) delete copy.dynamic; if (missingChain) delete copy.chain; return copy }) }, + post: async (path: string, body: Record) => { + if (path.endsWith("/get")) { assert.equal(body["value-name"], "dynamic"); return { ret: unknownDynamic ? "" : "false" } } + assert.equal(path, "/ip/firewall/filter/move"); assert.deepEqual(body, { numbers: "*1", destination: "*3" }); commands++ + if (!ignore) rows = movedFirewallRows(rows, input) + return {} + }, + getFirewallRules: async () => { throw new Error("Revision source unavailable") }, getFirewallAddressList: async () => [], +} +try { + MikrotikClient.fromServer = () => client as unknown as MikrotikClient + const execute = (expected?: string) => executeFirewallMove(input, expected, { read: () => readFirewallMoveState(server, input), command: () => client.post("/ip/firewall/filter/move", { numbers: "*1", destination: "*3" }), recordRevision: async () => false }) + assert.deepEqual(movedFirewallRows(rows, input).map(row => row[".id"]), ["*2", "*1", "*3"]) + assert.deepEqual(movedFirewallRows(rows, { ...input, destinationRosId: undefined }).map(row => row[".id"]), ["*2", "*3", "*1"]) + for (const rosId of ["*1,*2", "[find]", "*1/other"]) assert.equal(firewallMoveSchema.safeParse({ ...input, rosId }).success, false) + assert.equal(firewallMoveSchema.safeParse({ ...input, destinationRosId: "*1" }).success, false) + const before = await readFirewallMoveState(server, input) + assert.equal(before.rows[0]["connection-mark"], "keep-exactly"); assert.equal(before.rows[0].packets, undefined) + await assert.rejects(execute("stale")); assert.equal(commands, 0) + const result = await execute(fingerprint(before)) + assert.equal(result.verified, true); assert.equal(result.revisionRecorded, false); assert.equal(commands, 1) + rows.reverse(); ignore = true + assert.equal((await execute()).verified, false); assert.equal(commands, 2) + const beforeDisabled = gets + await assert.rejects(readFirewallMoveState({ ...server, enabled: false }, input)); assert.equal(gets, beforeDisabled) + omitDynamic = true; unknownDynamic = true + await assert.rejects(readFirewallMoveState(server, input), /статического/); assert.equal(commands, 2) + unknownDynamic = false + assert.equal((await readFirewallMoveState(server, input)).rows.find(row => row[".id"] === "*1")?.dynamic, "false") + missingChain = true; await assert.rejects(readFirewallMoveState(server, input), /Недостоверный/); missingChain = false + rows.push(rows[0]); await assert.rejects(readFirewallMoveState(server, input), /Повторяющиеся/) +} finally { MikrotikClient.fromServer = original } +console.log("Firewall move: exact order/matcher preservation, counters excluded, stale/no-write, static identity, disabled, no replay and ignored move verification passed") diff --git a/backend/src/services/firewall-rule-move.ts b/backend/src/services/firewall-rule-move.ts new file mode 100644 index 00000000..bc55ecc2 --- /dev/null +++ b/backend/src/services/firewall-rule-move.ts @@ -0,0 +1,80 @@ +import { z } from "zod" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { MikrotikClient, firewallRestPath } from "./mikrotik.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { fetchFirewallState } from "./firewall-live.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { fingerprint } from "../modules/mcp/security.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" + +export const firewallMoveSchema = z.object({ + family: z.enum(["ip", "ip6"]), table: z.enum(["filter", "nat", "mangle", "raw"]), + rosId: z.string().regex(/^\*[A-Za-z0-9]+$/).max(100), destinationRosId: z.string().regex(/^\*[A-Za-z0-9]+$/).max(100).optional(), +}).strict().refine(input => input.rosId !== input.destinationRosId, "Правило нельзя переместить перед самим собой") +export type FirewallMove = z.infer +const problem = (message: string) => Object.assign(new Error(message), { statusCode: 409 }) + +/** Order and all native matcher fields are retained; only volatile counters are + * excluded. No sorting and no UI-derived reconstruction of a firewall rule. */ +export async function readFirewallMoveState(server: ServerRow, input: FirewallMove) { + if (!server.enabled) throw problem("Сервер отключён; перемещение Firewall не выполнялось") + const client = MikrotikClient.fromServer(server), path = firewallRestPath(input.family, input.table) + const payload = await client.get(path) + if (!Array.isArray(payload) || payload.length > 1000 || payload.some(row => !row || typeof row !== "object" || Array.isArray(row) || [".id", "chain", "action"].some(key => typeof row[key] !== "string" || !row[key].trim()) || Object.values(row).some(value => typeof value !== "string"))) throw problem("Недостоверный каталог правил Firewall") + const rows = payload as Record[] + if (new Set(rows.map(row => row[".id"])).size !== rows.length) throw problem("Повторяющиеся ID Firewall") + for (const id of [input.rosId, input.destinationRosId].filter((id): id is string => !!id)) { + const row = rows.find(row => row[".id"] === id) + if (!row) throw problem("Правило или позиция назначения отсутствуют в выбранной таблице") + if (row.dynamic === undefined) { + const result = await client.post<{ ret?: string }>(`${path}/get`, { ".id": id, "value-name": "dynamic" }) + if (typeof result?.ret === "string") row.dynamic = result.ret + } + if (observedBoolean(row.dynamic) !== false) throw problem("Перемещение требует подтверждённо статического правила и позиции") + } + return { serverConfiguration: fingerprint(server), rows: rows.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !["bytes", "packets"].includes(key)))) } +} + +export function movedFirewallRows(rows: T[], input: FirewallMove): T[] { + const moved = rows.find(row => row[".id"] === input.rosId) + if (!moved) throw problem("Перемещаемое правило отсутствует") + const rest = rows.filter(row => row !== moved) + const index = input.destinationRosId ? rest.findIndex(row => row[".id"] === input.destinationRosId) : rest.length + if (index < 0) throw problem("Позиция назначения отсутствует") + return [...rest.slice(0, index), moved, ...rest.slice(index)] +} + +export async function executeFirewallMove(input: FirewallMove, expectedFingerprint: string | undefined, dependencies: { + read: () => Promise>>; + command: () => Promise; + recordRevision: () => Promise; +}) { + const before = await dependencies.read() + if (expectedFingerprint && fingerprint(before) !== expectedFingerprint) throw problem("Порядок или конфигурация Firewall изменились перед перемещением") + const expected = movedFirewallRows(before.rows, input) + if (fingerprint(expected) === fingerprint(before.rows)) return { acknowledged: true, changed: false, verified: true, revisionRecorded: false } + await dependencies.command() + const next = await dependencies.read() + if (fingerprint(next) !== fingerprint({ ...before, rows: expected })) return { acknowledged: true, changed: true, verified: false, revisionRecorded: false } + return { acknowledged: true, changed: true, verified: true, revisionRecorded: await dependencies.recordRevision() } +} + +export async function moveFirewallRule(server: ServerRow, raw: FirewallMove, expectedFingerprint?: string) { + const input = firewallMoveSchema.parse(raw) + if (!server.enabled) throw problem("Сервер отключён; перемещение Firewall не выполнялось") + return withApplicationMutationLock(() => withConfigMutationLock([server.id], async () => { + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || fingerprint(fresh) !== fingerprint(server)) throw problem("Источник Firewall изменился; обновите каталог перед перемещением") + const client = MikrotikClient.fromServer(fresh) + return executeFirewallMove(input, expectedFingerprint, { + read: () => readFirewallMoveState(fresh, input), + command: () => client.post(`${firewallRestPath(input.family, input.table)}/move`, { numbers: input.rosId, ...(input.destinationRosId ? { destination: input.destinationRosId } : {}) }), + recordRevision: async () => (await captureAndAppendRevision({ serverId: fresh.id, section: "firewall", source: "apply", capture: async () => { + const state = await fetchFirewallState(fresh) + if (!state.snapshotFlagsVerified) throw problem("Flags снимка Firewall не подтверждены") + return state.snapshot + } })) !== null, + }) + })) +} diff --git a/backend/src/services/firewall-rule-write.test.ts b/backend/src/services/firewall-rule-write.test.ts new file mode 100644 index 00000000..668e76b5 --- /dev/null +++ b/backend/src/services/firewall-rule-write.test.ts @@ -0,0 +1,31 @@ +import assert from "node:assert/strict" +import { executeFirewallRuleMutation, verifyFirewallRuleMutation, firewallRuleMutationSchema, type FirewallRuleMutation } from "./firewall-rule-write.js" +import { fingerprint } from "../modules/mcp/security.js" +const row = { ".id": "*A", chain: "fixture", action: "accept", disabled: "true", dynamic: "false", "connection-mark": "opaque", comment: "old" } +const before = { serverConfiguration: "source", rows: [row, { ...row, ".id": "*B" }] } +const toggle: FirewallRuleMutation = { family: "ip", table: "filter", operation: "toggle", rosId: "*A", disabled: false } +const changed = { ...before, rows: [{ ...row, disabled: "false" }, before.rows[1]] } +assert.equal(verifyFirewallRuleMutation(toggle, before, changed), true) +assert.equal(verifyFirewallRuleMutation(toggle, before, { ...changed, rows: [...changed.rows].reverse() }), false) +assert.equal(verifyFirewallRuleMutation(toggle, before, { ...changed, rows: [{ ...changed.rows[0], "connection-mark": "drift" }, changed.rows[1]] }), false) +assert.equal(verifyFirewallRuleMutation(toggle, before, { ...changed, serverConfiguration: "other" }), false) +const create: FirewallRuleMutation = { family: "ip", table: "filter", operation: "create", fields: { chain: "fixture", action: "drop", disabled: true } } +assert.equal(verifyFirewallRuleMutation(create, before, { ...before, rows: [...before.rows, { ".id": "*C", chain: "fixture", action: "drop", disabled: "true", dynamic: "false" }] }), true) +assert.equal(verifyFirewallRuleMutation(create, before, before), false) +const update: FirewallRuleMutation = { family: "ip", table: "filter", operation: "update", rosId: "*A", fields: { chain: "fixture", action: "drop", comment: "" } } +assert.equal(verifyFirewallRuleMutation(update, before, { ...before, rows: [{ ...row, action: "drop" }, before.rows[1]] }), true) +const remove: FirewallRuleMutation = { family: "ip", table: "filter", operation: "delete", rosId: "*A" } +assert.equal(verifyFirewallRuleMutation(remove, before, { ...before, rows: [before.rows[1]] }), true) +let reads = 0, commands = 0, revisions = 0 +const deps = { read: async () => ++reads === 1 ? before : changed, command: async () => { commands++ }, recordRevision: async () => { revisions++; return true } } +assert.equal((await executeFirewallRuleMutation(toggle, fingerprint(before), deps)).verified, true); assert.equal(commands, 1); assert.equal(revisions, 1) +reads = 0; commands = 0 +await assert.rejects(executeFirewallRuleMutation(toggle, "stale", deps), /изменилась/); assert.equal(commands, 0) +await assert.rejects(executeFirewallRuleMutation(toggle, undefined, { ...deps, read: async () => ({ ...before, rows: [{ ...row, dynamic: "true" }] }) }), /статическое/); assert.equal(commands, 0) +commands = 0; revisions = 0 +assert.equal((await executeFirewallRuleMutation(toggle, undefined, { ...deps, read: async () => before })).verified, false); assert.equal(commands, 1); assert.equal(revisions, 0) +commands = 0 +await assert.rejects(executeFirewallRuleMutation(toggle, undefined, { ...deps, read: async () => before, command: async () => { commands++; throw Error("response lost") } }), /response lost/); assert.equal(commands, 1) +for (const rosId of ["*A,*B", "*A/../../", ""]) assert.equal(firewallRuleMutationSchema.safeParse({ ...toggle, rosId }).success, false) +assert.equal(firewallRuleMutationSchema.safeParse({ ...create, fields: { ...create.fields, arbitrary: "no" } }).success, false) +console.log("Firewall form writes: create/update/toggle/delete, exact matcher/order, empty-field parity, boolean normalization, stale/static/partial/lost response and no replay passed") diff --git a/backend/src/services/firewall-rule-write.ts b/backend/src/services/firewall-rule-write.ts new file mode 100644 index 00000000..62c624b2 --- /dev/null +++ b/backend/src/services/firewall-rule-write.ts @@ -0,0 +1,90 @@ +import { z } from "zod" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { MikrotikClient, firewallRestPath, encodeRosId } from "./mikrotik.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { fingerprint } from "../modules/mcp/security.js" +import { RuleWriteSchema, ruleToRos } from "./firewall-rule-input.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { fetchFirewallState } from "./firewall-live.js" + +const key = { family: z.enum(["ip", "ip6"]), table: z.enum(["filter", "nat", "mangle", "raw"]) } +const nativeId = z.string().regex(/^\*[A-Za-z0-9]+$/).max(100) +export const firewallRuleFieldsSchema = RuleWriteSchema.omit({ serverId: true, family: true, table: true, rosId: true }).strict() +export const firewallRuleMutationSchema = z.discriminatedUnion("operation", [ + z.object({ ...key, operation: z.literal("create"), fields: firewallRuleFieldsSchema }).strict(), + z.object({ ...key, operation: z.literal("update"), rosId: nativeId, fields: firewallRuleFieldsSchema }).strict(), + z.object({ ...key, operation: z.literal("toggle"), rosId: nativeId, disabled: z.boolean() }).strict(), + z.object({ ...key, operation: z.literal("delete"), rosId: nativeId }).strict(), +]) +export type FirewallRuleMutation = z.infer +export type FirewallRuleState = { serverConfiguration: string; rows: Record[] } +const conflict = (message: string) => Object.assign(new Error(message), { statusCode: 409 }) +const bodyFor = (input: FirewallRuleMutation): Record => input.operation === "delete" ? {} : input.operation === "toggle" ? { disabled: input.disabled ? "yes" : "no" } : ruleToRos({ ...input.fields, serverId: "", family: input.family, table: input.table }) +const same = (a: unknown, b: unknown) => fingerprint(a) === fingerprint(b) +const normalized = (value: string | undefined, key: string) => ["disabled", "log", "dynamic", "invalid"].includes(key) ? observedBoolean(value) : value + +export async function readFirewallRuleState(server: ServerRow, input: FirewallRuleMutation): Promise { + if (!server.enabled) throw conflict("Сервер отключён") + const client = MikrotikClient.fromServer(server), path = firewallRestPath(input.family, input.table) + const raw = await client.get(path) + if (!Array.isArray(raw) || raw.length > 1000 || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || [".id", "chain", "action"].some(key => typeof row[key] !== "string" || !row[key].trim()))) throw conflict("Недостоверная таблица Firewall") + if (new Set(raw.map(row => row[".id"])).size !== raw.length) throw conflict("Повторяющиеся ID Firewall") + const rows = (raw as Record[]).map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !["bytes", "packets"].includes(key)))) + const target = input.operation === "create" ? undefined : rows.find(row => row[".id"] === input.rosId) + if (target && target.dynamic === undefined) { + const result = await client.post<{ ret?: string }>(`${path}/get`, { ".id": target[".id"], "value-name": "dynamic" }) + if (typeof result?.ret === "string") target.dynamic = result.ret + } + return { serverConfiguration: fingerprint(server), rows } +} + +export function assertFirewallRuleTarget(input: FirewallRuleMutation, before: FirewallRuleState) { + if (input.operation === "create") return + const row = before.rows.find(row => row[".id"] === input.rosId) + if (!row || observedBoolean(row.dynamic) !== false) throw conflict("Требуется существующее подтверждённо статическое правило") +} + +/** Verify only the requested mutation while preserving every opaque matcher and order. */ +export function verifyFirewallRuleMutation(input: FirewallRuleMutation, before: FirewallRuleState, next: FirewallRuleState) { + if (before.serverConfiguration !== next.serverConfiguration) return false + const body = bodyFor(input) + if (input.operation === "create") { + const added = next.rows.filter(row => !before.rows.some(previous => previous[".id"] === row[".id"])) + return added.length === 1 && next.rows.at(-1) === added[0] && same(next.rows.slice(0, -1), before.rows) + && Object.entries(body).every(([key, value]) => normalized(added[0][key], key) === normalized(value, key)) + } + const expected = before.rows.filter(row => input.operation !== "delete" || row[".id"] !== input.rosId).map(row => row[".id"] === input.rosId ? { ...row, ...body } : row) + const canonical = (rows: Record[]) => rows.map(row => Object.fromEntries(Object.entries(row).map(([key, value]) => [key, normalized(value, key)]))) + return same(canonical(next.rows), canonical(expected)) +} + +export async function executeFirewallRuleMutation(input: FirewallRuleMutation, expectedFingerprint: string | undefined, deps: { + read: () => Promise; command: (body: Record) => Promise; recordRevision: () => Promise; +}) { + const before = await deps.read() + if (expectedFingerprint && fingerprint(before) !== expectedFingerprint) throw conflict("Конфигурация Firewall изменилась после подготовки") + assertFirewallRuleTarget(input, before) + await deps.command(bodyFor(input)) + const next = await deps.read(), verified = verifyFirewallRuleMutation(input, before, next) + return { acknowledged: true, verified, afterFingerprint: fingerprint(next), revisionRecorded: verified ? await deps.recordRevision() : false } +} + +export async function writeFirewallRule(server: ServerRow, raw: FirewallRuleMutation, expectedFingerprint?: string) { + const input = firewallRuleMutationSchema.parse(raw) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], async () => { + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || !same(fresh, server)) throw conflict("Источник Firewall изменился") + const client = MikrotikClient.fromServer(fresh), path = firewallRestPath(input.family, input.table) + return executeFirewallRuleMutation(input, expectedFingerprint, { + read: () => readFirewallRuleState(fresh, input), + command: body => input.operation === "create" ? client.put(path, body) : input.operation === "delete" ? client.delete(`${path}/${encodeRosId(input.rosId)}`) : client.patch(`${path}/${encodeRosId(input.rosId)}`, body), + recordRevision: async () => (await captureAndAppendRevision({ serverId: fresh.id, section: "firewall", source: "apply", capture: async () => { + const state = await fetchFirewallState(fresh) + if (!state.snapshotFlagsVerified) throw conflict("Flags ревизии Firewall не подтверждены") + return state.snapshot + } })) !== null, + }) + })) +} diff --git a/backend/src/services/fixtures/vrf-bridge-delete-ll-v33.json b/backend/src/services/fixtures/vrf-bridge-delete-ll-v33.json new file mode 100644 index 00000000..6bb003a1 --- /dev/null +++ b/backend/src/services/fixtures/vrf-bridge-delete-ll-v33.json @@ -0,0 +1,223 @@ +{ + "kind": "V33 selected loop bridge DELETE observation; not raw17", + "serverId": 2, + "inputs": { + "beforeSha256": "09a4a1a16dee2554bf809d88328b276f0289e81e29fa94b18f7a61ffa709d0e4", + "afterSha256": "b5116e76452573cb0b6e532afb81d16ffc8360710da5e59ac40da67341e1adae" + }, + "limitations": [ + "Safe projected selected rows only; omitted native fields unavailable", + "Tests reconstruct main VRF/table/singleton settings and restrict target membership to this bridge", + "Full admission/classifier on reconstructed test graph is synthetic, not full actual raw17 replay" + ], + "before": { + "bridge": [ + { + ".id": "*2C", + "actual-mtu": "1500", + "comment": "mcp-route-3efd451365d2", + "disabled": "false", + "dynamic": "false", + "mac-address": "82:97:C3:74:4C:A5", + "mtu": "auto", + "name": "mcp-route-3efd451365d2-loop", + "protocol-mode": "none", + "running": "true" + } + ], + "interface_inventory": [ + { + ".id": "*2C", + "actual-mtu": "1500", + "caps": " ARP DHCP USERARP VLAN IS_BRIDGE MESH", + "comment": "mcp-route-3efd451365d2", + "disabled": "false", + "dynamic": "false", + "flags": " UP BROADCAST RUNNING MULTICAST LOWER_UP (0x11043)", + "fp-rps-drop": "0", + "id": "44", + "ifindex": "46", + "ifname": "br0", + "last-link-up-time": "2026-11-05 19:52:16", + "link-downs": "0", + "mac-address": "82:97:C3:74:4C:A5", + "mtu": "auto", + "name": "mcp-route-3efd451365d2-loop", + "running": "true", + "status": "--LERUI-O-----------V---", + "type": "bridge", + "vrf": "mcp-route-3efd451365d2-vrf" + }, + { + ".id": "*2D", + "actual-mtu": "65536", + "caps": "", + "disabled": "false", + "flags": " UP RUNNING NOARP MASTER LOWER_UP (0x104c1)", + "fp-rps-drop": "0", + "id": "45", + "ifindex": "47", + "ifname": "vrf1", + "last-link-up-time": "2026-11-05 19:55:16", + "link-downs": "0", + "mac-address": "7A:F0:19:DD:88:39", + "mtu": "65536", + "name": "mcp-route-3efd451365d2-vrf", + "running": "true", + "status": "--LERU--O---------------", + "type": "vrf", + "vrf": "main" + } + ], + "vrf": [ + { + ".id": "*1", + "comment": "mcp-route-3efd451365d2", + "disabled": "false", + "interfaces": "mcp-route-3efd451365d2-gre-a,mcp-route-3efd451365d2-gre-b,mcp-route-3efd451365d2-loop", + "name": "mcp-route-3efd451365d2-vrf" + } + ], + "ipv4_address": [ + { + ".id": "*31", + "actual-interface": "mcp-route-3efd451365d2-loop", + "address": "198.18.245.17/32", + "comment": "mcp-route-3efd451365d2", + "disabled": "false", + "dynamic": "false", + "interface": "mcp-route-3efd451365d2-loop", + "invalid": "false", + "vrf": "mcp-route-3efd451365d2-vrf" + } + ], + "ipv6_address": [ + { + ".id": "*3D", + "actual-interface": "mcp-route-3efd451365d2-loop", + "address": "fe80::8097:c3ff:fe74:4ca5/64", + "auto-link-local": "true", + "disabled": "false", + "dynamic": "true", + "interface": "mcp-route-3efd451365d2-loop", + "invalid": "false", + "link-local": "true", + "vrf": "main" + } + ], + "ipv4_route": [ + { + ".id": "*20145080", + "connect": "true", + "distance": "0", + "dst-address": "198.18.245.17/32", + "dynamic": "true", + "gateway": "mcp-route-3efd451365d2-loop@mcp-route-3efd451365d2-vrf", + "immediate-gw": "mcp-route-3efd451365d2-loop", + "local-address": "198.18.245.17%mcp-route-3efd451365d2-loop@mcp-route-3efd451365d2-vrf", + "routing-table": "mcp-route-3efd451365d2-vrf", + "scope": "10", + "target-scope": "5" + } + ], + "ipv6_route": [ + { + ".id": "*20145070", + "connect": "true", + "distance": "0", + "dst-address": "fe80::/64", + "dynamic": "true", + "gateway": "mcp-route-3efd451365d2-loop@mcp-route-3efd451365d2-vrf", + "immediate-gw": "mcp-route-3efd451365d2-loop", + "routing-table": "mcp-route-3efd451365d2-vrf", + "scope": "10", + "target-scope": "5" + } + ], + "routing_table": [ + { + ".id": "*1", + "dynamic": "true", + "fib": "", + "invalid": "false", + "name": "mcp-route-3efd451365d2-vrf" + } + ], + "bridge_port": [], + "bridge_vlan": [] + }, + "after": { + "bridge": [ + { + ".id": "*2C", + "actual-mtu": "1500", + "comment": "mcp-route-3efd451365d2", + "disabled": "false", + "dynamic": "false", + "mac-address": "82:97:C3:74:4C:A5", + "mtu": "auto", + "name": "mcp-route-3efd451365d2-loop", + "protocol-mode": "none", + "running": "true" + } + ], + "interface_inventory": [ + { + ".id": "*2C", + "actual-mtu": "1500", + "caps": " ARP DHCP USERARP VLAN IS_BRIDGE MESH", + "comment": "mcp-route-3efd451365d2", + "disabled": "false", + "dynamic": "false", + "flags": " UP BROADCAST RUNNING MULTICAST LOWER_UP (0x11043)", + "fp-rps-drop": "0", + "id": "44", + "ifindex": "46", + "ifname": "br0", + "last-link-up-time": "2026-11-05 19:52:16", + "link-downs": "0", + "mac-address": "82:97:C3:74:4C:A5", + "mtu": "auto", + "name": "mcp-route-3efd451365d2-loop", + "running": "true", + "status": "--LERUI-O---------------", + "type": "bridge", + "vrf": "main" + } + ], + "vrf": [], + "ipv4_address": [ + { + ".id": "*31", + "actual-interface": "mcp-route-3efd451365d2-loop", + "address": "198.18.245.17/32", + "comment": "mcp-route-3efd451365d2", + "disabled": "false", + "dynamic": "false", + "interface": "mcp-route-3efd451365d2-loop", + "invalid": "false", + "vrf": "main" + } + ], + "ipv6_address": [], + "ipv4_route": [ + { + ".id": "*201450D0", + "connect": "true", + "distance": "0", + "dst-address": "198.18.245.17/32", + "dynamic": "true", + "gateway": "mcp-route-3efd451365d2-loop", + "immediate-gw": "mcp-route-3efd451365d2-loop", + "local-address": "198.18.245.17%mcp-route-3efd451365d2-loop", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + } + ], + "ipv6_route": [], + "routing_table": [], + "bridge_port": [], + "bridge_vlan": [] + } +} diff --git a/backend/src/services/fixtures/vrf-gre-inverse-cycle-v32.json b/backend/src/services/fixtures/vrf-gre-inverse-cycle-v32.json new file mode 100644 index 00000000..c3d5f1c4 --- /dev/null +++ b/backend/src/services/fixtures/vrf-gre-inverse-cycle-v32.json @@ -0,0 +1,349 @@ +{ + "kind": "actual-selected-V32-guarded-inverse-pair-with-reconstructed-root-config", + "inputs": { + "preDetail": "12195049ae548776d47449a86f849ebf19776c9d3d562bb425f138c24a0a0922", + "postDetail": "d96200bd6590d69e484d4535f28b113bdd49944de161eadfe6ab4fc506affbe8", + "cleanupV2": "094f4b59569386149288f4696fbcd7105a0d049a035bc825f7753c1ffc537c88", + "receipt": "ebbe523caa1217ea4fab393b7d9238e23f164df8544fe6a29f3752400d81dd63" + }, + "before": { + "catalogs": { + "vrf": [ + { + ".id": "*1", + "name": "mcp-route-46e4f3b60294-vrf", + "interfaces": "mcp-route-46e4f3b60294-gre-a,mcp-route-46e4f3b60294-gre-b", + "disabled": "false" + }, + { + ".id": "*0", + "name": "main", + "interfaces": "all", + "disabled": "false" + } + ], + "interface_inventory": [ + { + ".id": "*30", + "name": "mcp-route-46e4f3b60294-gre-a", + "type": "gre-tunnel", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "vrf": "mcp-route-46e4f3b60294-vrf", + "caps": "", + "flags": " UP POINTOPOINT RUNNING NOARP LOWER_UP (0x100d1)", + "fp-rps-drop": "0", + "id": "48", + "ifindex": "49", + "ifname": "greip4-48", + "last-link-up-time": "2026-11-05 19:42:15", + "last-link-down-time": "2026-11-05 19:42:15", + "link-downs": "1", + "status": "--L-RUI-O-----------V---" + }, + { + ".id": "*31", + "name": "mcp-route-46e4f3b60294-gre-b", + "type": "gre-tunnel", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "vrf": "mcp-route-46e4f3b60294-vrf", + "caps": "", + "flags": " UP POINTOPOINT RUNNING NOARP LOWER_UP (0x100d1)", + "fp-rps-drop": "0", + "id": "49", + "ifindex": "50", + "ifname": "greip4-49", + "last-link-up-time": "2026-11-05 19:42:15", + "last-link-down-time": "2026-11-05 19:42:15", + "link-downs": "1", + "status": "--L-RUI-O-----------V---" + }, + { + ".id": "*32", + "name": "mcp-route-46e4f3b60294-vrf", + "type": "vrf", + "mac-address": "16:A1:FC:19:3A:0B", + "disabled": "false", + "running": "true", + "mtu": "65536", + "actual-mtu": "65536", + "vrf": "main", + "caps": "", + "flags": " UP RUNNING NOARP MASTER LOWER_UP (0x104c1)", + "fp-rps-drop": "0", + "id": "50", + "ifindex": "51", + "ifname": "vrf1", + "last-link-up-time": "2026-11-05 19:42:15", + "link-downs": "0", + "status": "--LERU--O---------------" + }, + { + ".id": "*2F", + "name": "mcp-route-46e4f3b60294-wg", + "type": "wg", + "disabled": "false", + "running": "true", + "mtu": "1420", + "actual-mtu": "1420", + "vrf": "main", + "caps": "", + "flags": " UP POINTOPOINT RUNNING NOARP LOWER_UP (0x100d1)", + "fp-rps-drop": "0", + "id": "47", + "ifindex": "48", + "ifname": "wg47", + "last-link-up-time": "2026-11-05 19:32:05", + "link-downs": "0", + "status": "--L-RUI-O---------------" + } + ], + "gre": [ + { + ".id": "*30", + "name": "mcp-route-46e4f3b60294-gre-a", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "local-address": "198.18.245.9", + "remote-address": "198.18.245.10", + "allow-fast-path": "false", + "clamp-tcp-mss": "true", + "keepalive": "10s,10", + "comment": "mcp-route-46e4f3b60294" + }, + { + ".id": "*31", + "name": "mcp-route-46e4f3b60294-gre-b", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "local-address": "198.18.245.13", + "remote-address": "198.18.245.14", + "allow-fast-path": "false", + "clamp-tcp-mss": "true", + "keepalive": "10s,10", + "comment": "mcp-route-46e4f3b60294" + } + ], + "ipv4_address": [ + { + ".id": "*3A", + "address": "198.18.245.9/30", + "interface": "mcp-route-46e4f3b60294-wg", + "actual-interface": "mcp-route-46e4f3b60294-wg", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "main", + "network": "198.18.245.8" + }, + { + ".id": "*3B", + "address": "198.18.245.13/30", + "interface": "mcp-route-46e4f3b60294-wg", + "actual-interface": "mcp-route-46e4f3b60294-wg", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "main", + "network": "198.18.245.12" + }, + { + ".id": "*3C", + "address": "198.18.245.1/30", + "interface": "mcp-route-46e4f3b60294-gre-a", + "actual-interface": "mcp-route-46e4f3b60294-gre-a", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "mcp-route-46e4f3b60294-vrf", + "network": "198.18.245.0" + }, + { + ".id": "*3D", + "address": "198.18.245.5/30", + "interface": "mcp-route-46e4f3b60294-gre-b", + "actual-interface": "mcp-route-46e4f3b60294-gre-b", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "mcp-route-46e4f3b60294-vrf", + "network": "198.18.245.4" + } + ], + "wireguard": [ + { + ".id": "*2F", + "name": "mcp-route-46e4f3b60294-wg" + } + ] + } + }, + "after": { + "catalogs": { + "vrf": [ + { + ".id": "*0", + "name": "main", + "interfaces": "all", + "disabled": "false" + } + ], + "interface_inventory": [ + { + ".id": "*30", + "name": "mcp-route-46e4f3b60294-gre-a", + "type": "gre-tunnel", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "vrf": "main", + "caps": "", + "flags": " UP POINTOPOINT RUNNING NOARP LOWER_UP (0x100d1)", + "fp-rps-drop": "0", + "id": "48", + "ifindex": "49", + "ifname": "greip4-48", + "last-link-up-time": "2026-11-05 20:30:42", + "last-link-down-time": "2026-11-05 20:30:41", + "link-downs": "2", + "status": "--L-RUI-O---------------" + }, + { + ".id": "*31", + "name": "mcp-route-46e4f3b60294-gre-b", + "type": "gre-tunnel", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "vrf": "main", + "caps": "", + "flags": " UP POINTOPOINT RUNNING NOARP LOWER_UP (0x100d1)", + "fp-rps-drop": "0", + "id": "49", + "ifindex": "50", + "ifname": "greip4-49", + "last-link-up-time": "2026-11-05 20:30:42", + "last-link-down-time": "2026-11-05 20:30:41", + "link-downs": "2", + "status": "--L-RUI-O---------------" + }, + { + ".id": "*2F", + "name": "mcp-route-46e4f3b60294-wg", + "type": "wg", + "disabled": "false", + "running": "true", + "mtu": "1420", + "actual-mtu": "1420", + "vrf": "main", + "caps": "", + "flags": " UP POINTOPOINT RUNNING NOARP LOWER_UP (0x100d1)", + "fp-rps-drop": "0", + "id": "47", + "ifindex": "48", + "ifname": "wg47", + "last-link-up-time": "2026-11-05 19:32:05", + "link-downs": "0", + "status": "--L-RUI-O---------------" + } + ], + "gre": [ + { + ".id": "*30", + "name": "mcp-route-46e4f3b60294-gre-a", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "local-address": "198.18.245.9", + "remote-address": "198.18.245.10", + "allow-fast-path": "false", + "clamp-tcp-mss": "true", + "keepalive": "10s,10", + "comment": "mcp-route-46e4f3b60294" + }, + { + ".id": "*31", + "name": "mcp-route-46e4f3b60294-gre-b", + "disabled": "false", + "running": "true", + "mtu": "1476", + "actual-mtu": "1476", + "local-address": "198.18.245.13", + "remote-address": "198.18.245.14", + "allow-fast-path": "false", + "clamp-tcp-mss": "true", + "keepalive": "10s,10", + "comment": "mcp-route-46e4f3b60294" + } + ], + "ipv4_address": [ + { + ".id": "*3A", + "address": "198.18.245.9/30", + "interface": "mcp-route-46e4f3b60294-wg", + "actual-interface": "mcp-route-46e4f3b60294-wg", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "main", + "network": "198.18.245.8" + }, + { + ".id": "*3B", + "address": "198.18.245.13/30", + "interface": "mcp-route-46e4f3b60294-wg", + "actual-interface": "mcp-route-46e4f3b60294-wg", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "main", + "network": "198.18.245.12" + }, + { + ".id": "*3C", + "address": "198.18.245.1/30", + "interface": "mcp-route-46e4f3b60294-gre-a", + "actual-interface": "mcp-route-46e4f3b60294-gre-a", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "main", + "network": "198.18.245.0" + }, + { + ".id": "*3D", + "address": "198.18.245.5/30", + "interface": "mcp-route-46e4f3b60294-gre-b", + "actual-interface": "mcp-route-46e4f3b60294-gre-b", + "dynamic": "false", + "disabled": "false", + "invalid": "false", + "vrf": "main", + "network": "198.18.245.4" + } + ], + "wireguard": [ + { + ".id": "*2F", + "name": "mcp-route-46e4f3b60294-wg" + } + ] + } + }, + "rootWireguardConfigurationReconstructed": true, + "fullRaw17": false, + "forwardCreateCycleProven": false, + "exclusiveCausalAttributionProven": false +} diff --git a/backend/src/services/fixtures/vrf-projection-v2.json b/backend/src/services/fixtures/vrf-projection-v2.json new file mode 100644 index 00000000..f3746194 --- /dev/null +++ b/backend/src/services/fixtures/vrf-projection-v2.json @@ -0,0 +1,811 @@ +{ + "kind": "genuine-pre-V3-output-with-synthetic-GRE-WG-peer", + "sourceProjectionSha256": "01366cbf771dec96af7441696763e19b0276cfb312df4715bc3bb0941cdd2b80", + "before": { + "projection": "raw-native-v1", + "serverId": 7, + "sourceProof": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "catalogs": { + "bridge": [], + "bridge_port": [], + "bridge_vlan": [], + "vrf": [ + { + ".id": "*0", + "name": "main", + "interfaces": "all", + "disabled": "false", + "builtin": "true" + } + ], + "ipv4_address": [ + { + ".id": "*A", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "address": "198.18.137.1/32", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "invalid": "false", + "network": "198.18.137.1", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*B", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "address": "198.18.137.2/32", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "invalid": "false", + "network": "198.18.137.2", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*ff20", + "interface": "gre-source-wg", + "address": "198.18.245.9/30", + "dynamic": "false", + "disabled": "false", + "vrf": "main" + }, + { + ".id": "*ff21", + "interface": "gre-source-wg", + "address": "198.18.245.13/30", + "dynamic": "false", + "disabled": "false", + "vrf": "main" + } + ], + "ipv6_address": [ + { + ".id": "*19", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "address": "fe80::2:c612:f50a/64", + "advertise": "false", + "auto-link-local": "true", + "deprecated": "false", + "disabled": "false", + "dynamic": "true", + "eui-64": "false", + "from-pool": "", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "invalid": "false", + "link-local": "true", + "no-dad": "false", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*1A", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "address": "fe80::2:c612:f50e/64", + "advertise": "false", + "auto-link-local": "true", + "deprecated": "false", + "disabled": "false", + "dynamic": "true", + "eui-64": "false", + "from-pool": "", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "invalid": "false", + "link-local": "true", + "no-dad": "false", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*1B", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "address": "fd6d:6370:2043:fbc2::1/128", + "advertise": "false", + "auto-link-local": "true", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "deprecated": "false", + "disabled": "false", + "dynamic": "false", + "eui-64": "false", + "from-pool": "", + "global": "true", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "invalid": "false", + "no-dad": "false", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*1C", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "address": "fd6d:6370:2043:fbc2::2/128", + "advertise": "false", + "auto-link-local": "true", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "deprecated": "false", + "disabled": "false", + "dynamic": "false", + "eui-64": "false", + "from-pool": "", + "global": "true", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "invalid": "false", + "no-dad": "false", + "slave": "false", + "vrf": "main" + } + ], + "ipv4_route": [ + { + ".id": "*20145060", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "198.18.137.1/32", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "inactive": "false", + "local-address": "198.18.137.1%mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*20145080", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "198.18.137.2/32", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "inactive": "false", + "local-address": "198.18.137.2%mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + } + ], + "ipv6_route": [ + { + ".id": "*20145070", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fd6d:6370:2043:fbc2::1/128", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "inactive": "false", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*20145090", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fd6d:6370:2043:fbc2::2/128", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "inactive": "false", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*20145040", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fe80::/64", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "inactive": "false", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*20145050", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fe80::/64", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "inactive": "false", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + } + ], + "routing_table": [ + { + ".id": "*0", + "name": "main", + "dynamic": "true", + "fib": "true" + } + ], + "ethernet": [], + "ip_filter": [], + "ipv6_filter": [], + "routing_rule": [], + "ip_settings": [ + {} + ], + "ipv6_settings": [ + {} + ], + "interface_inventory": [ + { + ".id": "*D", + "actual-mtu": "1500", + "caps": " ARP DHCP USERARP VLAN IS_BRIDGE MESH", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "flags": " UP BROADCAST RUNNING MULTICAST LOWER_UP (0x11043)", + "fp-rps-drop": "0", + "fp-rx-byte": "0", + "fp-rx-packet": "0", + "fp-tx-byte": "0", + "fp-tx-packet": "0", + "id": "13", + "ifindex": "16", + "ifname": "br0", + "l2mtu": "65535", + "last-link-up-time": "2026-10-24 15:32:46", + "link-downs": "0", + "mtu": "auto", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "running": "true", + "rx-byte": "0", + "rx-drop": "0", + "rx-error": "0", + "rx-packet": "0", + "status": "--LERUI-O---------------", + "tx-byte": "1194", + "tx-drop": "0", + "tx-error": "0", + "tx-packet": "11", + "tx-queue-drop": "0", + "type": "gre-tunnel", + "vrf": "main" + }, + { + ".id": "*E", + "actual-mtu": "1500", + "caps": " ARP DHCP USERARP VLAN IS_BRIDGE MESH", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "flags": " UP BROADCAST RUNNING MULTICAST LOWER_UP (0x11043)", + "fp-rps-drop": "0", + "fp-rx-byte": "0", + "fp-rx-packet": "0", + "fp-tx-byte": "0", + "fp-tx-packet": "0", + "id": "14", + "ifindex": "17", + "ifname": "br1", + "l2mtu": "65535", + "last-link-up-time": "2026-10-24 15:33:04", + "link-downs": "0", + "mtu": "auto", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "running": "true", + "rx-byte": "0", + "rx-drop": "0", + "rx-error": "0", + "rx-packet": "0", + "status": "--LERUI-O---------------", + "tx-byte": "1194", + "tx-drop": "0", + "tx-error": "0", + "tx-packet": "11", + "tx-queue-drop": "0", + "type": "gre-tunnel", + "vrf": "main" + }, + { + ".id": "*ff11", + "name": "gre-source-wg", + "type": "wg", + "vrf": "main" + } + ], + "interface_list_members": [], + "gre": [ + { + ".id": "*D", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "local-address": "198.18.245.9", + "remote-address": "198.18.245.10" + }, + { + ".id": "*E", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "local-address": "198.18.245.13", + "remote-address": "198.18.245.14" + } + ], + "wireguard": [ + { + ".id": "*ff11", + "name": "gre-source-wg", + "private-key": "TEST_PRIVATE_KEY" + } + ], + "wireguard_peer": [ + { + ".id": "*ff31", + "interface": "gre-source-wg", + "public-key": "TEST_PUBLIC_KEY", + "preshared-key": "TEST_PSK", + "endpoint-address": "192.0.2.2", + "endpoint-port": "55101", + "current-endpoint-address": "192.0.2.2", + "current-endpoint-port": "55101", + "allowed-address": "198.18.245.10/32", + "persistent-keepalive": "10s", + "disabled": "false", + "running": "true", + "rx": "100", + "tx": "200", + "last-handshake": "1s", + "opaque": "configuration" + } + ] + } + }, + "after": { + "projection": "raw-native-v1", + "serverId": 7, + "sourceProof": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "catalogs": { + "bridge": [], + "bridge_port": [], + "bridge_vlan": [], + "vrf": [ + { + ".id": "*4", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "interfaces": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v" + }, + { + ".id": "*0", + "name": "main", + "interfaces": "all", + "disabled": "false", + "builtin": "true" + } + ], + "ipv4_address": [ + { + ".id": "*A", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "address": "198.18.137.1/32", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "invalid": "false", + "network": "198.18.137.1", + "slave": "false", + "vrf": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v" + }, + { + ".id": "*B", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "address": "198.18.137.2/32", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "invalid": "false", + "network": "198.18.137.2", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*ff20", + "interface": "gre-source-wg", + "address": "198.18.245.9/30", + "dynamic": "false", + "disabled": "false", + "vrf": "main" + }, + { + ".id": "*ff21", + "interface": "gre-source-wg", + "address": "198.18.245.13/30", + "dynamic": "false", + "disabled": "false", + "vrf": "main" + } + ], + "ipv6_address": [ + { + ".id": "*1A", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "address": "fe80::2:c612:f50e/64", + "advertise": "false", + "auto-link-local": "true", + "deprecated": "false", + "disabled": "false", + "dynamic": "true", + "eui-64": "false", + "from-pool": "", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "invalid": "false", + "link-local": "true", + "no-dad": "false", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*1B", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "address": "fd6d:6370:2043:fbc2::1/128", + "advertise": "false", + "auto-link-local": "true", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "deprecated": "false", + "disabled": "false", + "dynamic": "false", + "eui-64": "false", + "from-pool": "", + "global": "true", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "invalid": "false", + "no-dad": "false", + "slave": "false", + "vrf": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v" + }, + { + ".id": "*1C", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "address": "fd6d:6370:2043:fbc2::2/128", + "advertise": "false", + "auto-link-local": "true", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "deprecated": "false", + "disabled": "false", + "dynamic": "false", + "eui-64": "false", + "from-pool": "", + "global": "true", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "invalid": "false", + "no-dad": "false", + "slave": "false", + "vrf": "main" + }, + { + ".id": "*1D", + "actual-interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "address": "fe80::2:c612:f50a/64", + "advertise": "false", + "auto-link-local": "true", + "deprecated": "false", + "disabled": "false", + "dynamic": "true", + "eui-64": "false", + "from-pool": "", + "interface": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "invalid": "false", + "link-local": "true", + "no-dad": "false", + "slave": "false", + "vrf": "main" + } + ], + "ipv4_route": [ + { + ".id": "*20145080", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "198.18.137.2/32", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "inactive": "false", + "local-address": "198.18.137.2%mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*201450B0", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "198.18.137.1/32", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a@mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "inactive": "false", + "local-address": "198.18.137.1%mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a@mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "routing-table": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "scope": "10", + "target-scope": "5" + } + ], + "ipv6_route": [ + { + ".id": "*20145090", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fd6d:6370:2043:fbc2::2/128", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "inactive": "false", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*20145050", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fe80::/64", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "inactive": "false", + "routing-table": "main", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*201450C0", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fd6d:6370:2043:fbc2::1/128", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a@mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "inactive": "false", + "routing-table": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "scope": "10", + "target-scope": "5" + }, + { + ".id": "*20145040", + "active": "true", + "connect": "true", + "distance": "0", + "dst-address": "fe80::/64", + "dynamic": "true", + "gateway": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a@mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "immediate-gw": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "inactive": "false", + "routing-table": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "scope": "10", + "target-scope": "5" + } + ], + "routing_table": [ + { + ".id": "*0", + "name": "main", + "dynamic": "true", + "fib": "true" + }, + { + ".id": "*4", + "dynamic": "true", + "fib": "", + "invalid": "false", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v" + } + ], + "ethernet": [], + "ip_filter": [], + "ipv6_filter": [], + "routing_rule": [], + "ip_settings": [ + {} + ], + "ipv6_settings": [ + {} + ], + "interface_inventory": [ + { + ".id": "*D", + "actual-mtu": "1500", + "caps": " ARP DHCP USERARP VLAN IS_BRIDGE MESH", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "flags": " UP BROADCAST RUNNING MULTICAST LOWER_UP (0x11043)", + "fp-rps-drop": "0", + "fp-rx-byte": "0", + "fp-rx-packet": "0", + "fp-tx-byte": "0", + "fp-tx-packet": "0", + "id": "13", + "ifindex": "16", + "ifname": "br0", + "l2mtu": "65535", + "last-link-up-time": "2026-10-24 15:32:46", + "link-downs": "0", + "mtu": "auto", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "running": "true", + "rx-byte": "0", + "rx-drop": "0", + "rx-error": "0", + "rx-packet": "0", + "status": "--LERUI-O-----------V---", + "tx-byte": "2146", + "tx-drop": "0", + "tx-error": "0", + "tx-packet": "19", + "tx-queue-drop": "0", + "type": "gre-tunnel", + "vrf": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v" + }, + { + ".id": "*E", + "actual-mtu": "1500", + "caps": " ARP DHCP USERARP VLAN IS_BRIDGE MESH", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797", + "disabled": "false", + "dynamic": "false", + "flags": " UP BROADCAST RUNNING MULTICAST LOWER_UP (0x11043)", + "fp-rps-drop": "0", + "fp-rx-byte": "0", + "fp-rx-packet": "0", + "fp-tx-byte": "0", + "fp-tx-packet": "0", + "id": "14", + "ifindex": "17", + "ifname": "br1", + "l2mtu": "65535", + "last-link-up-time": "2026-10-24 15:33:04", + "link-downs": "0", + "mtu": "auto", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "running": "true", + "rx-byte": "0", + "rx-drop": "0", + "rx-error": "0", + "rx-packet": "0", + "status": "--LERUI-O---------------", + "tx-byte": "1194", + "tx-drop": "0", + "tx-error": "0", + "tx-packet": "11", + "tx-queue-drop": "0", + "type": "gre-tunnel", + "vrf": "main" + }, + { + ".id": "*F", + "actual-mtu": "65536", + "caps": "", + "disabled": "false", + "flags": " UP RUNNING NOARP MASTER LOWER_UP (0x104c1)", + "fp-rps-drop": "0", + "fp-rx-byte": "0", + "fp-rx-packet": "0", + "fp-tx-byte": "0", + "fp-tx-packet": "0", + "id": "15", + "ifindex": "18", + "ifname": "vrf4", + "last-link-up-time": "2026-10-24 15:34:48", + "link-downs": "0", + "mac-address": "FA:D1:FD:E0:45:DD", + "mtu": "65536", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "running": "true", + "rx-byte": "0", + "rx-drop": "0", + "rx-error": "0", + "rx-packet": "0", + "status": "--LERU--O---------------", + "tx-byte": "0", + "tx-drop": "0", + "tx-error": "0", + "tx-packet": "0", + "tx-queue-drop": "0", + "type": "vrf", + "vrf": "main" + }, + { + ".id": "*ff11", + "name": "gre-source-wg", + "type": "wg", + "vrf": "main" + } + ], + "interface_list_members": [], + "gre": [ + { + ".id": "*D", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "local-address": "198.18.245.9", + "remote-address": "198.18.245.10" + }, + { + ".id": "*E", + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-b", + "local-address": "198.18.245.13", + "remote-address": "198.18.245.14" + } + ], + "wireguard": [ + { + ".id": "*ff11", + "name": "gre-source-wg", + "private-key": "TEST_PRIVATE_KEY" + } + ], + "wireguard_peer": [ + { + ".id": "*ff31", + "interface": "gre-source-wg", + "public-key": "TEST_PUBLIC_KEY", + "preshared-key": "TEST_PSK", + "endpoint-address": "192.0.2.2", + "endpoint-port": "55101", + "current-endpoint-address": "192.0.2.2", + "current-endpoint-port": "55101", + "allowed-address": "198.18.245.10/32", + "persistent-keepalive": "10s", + "disabled": "false", + "running": "true", + "rx": "100", + "tx": "200", + "last-handshake": "1s", + "opaque": "configuration" + } + ] + } + }, + "binding": { + "serverId": 7, + "sourceProof": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "actorId": "legacy-human", + "planId": "legacy-plan" + }, + "request": { + "action": "create", + "fields": { + "name": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-v", + "interfaces": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797-a", + "comment": "mcp-test-vrf-companion-3890a4fa-2f69-4fcc-b11e-5ea4d1245797" + }, + "placeBeforeId": "*0" + }, + "scope": { + "serverId": 7, + "sourceProof": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "actorId": "legacy-human", + "nativeId": "*4" + }, + "now": 1000, + "secretFill": 7, + "acknowledgedId": "*4", + "admissionV2": "eyJiaW5kaW5nIjp7ImFjdG9ySWQiOiJsZWdhY3ktaHVtYW4iLCJwbGFuSWQiOiJsZWdhY3ktcGxhbiIsInNlcnZlcklkIjo3LCJzb3VyY2VQcm9vZiI6ImFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWEifSwiY29tcGFuaW9uRXhlY3V0aW9uIjoicmVxdWlyZXMtbmF0aXZlLXRyYW5zaXRpb24tcHJvb2YiLCJleHBpcmVzQXQiOjMwMTAwMCwiaXNzdWVkQXQiOjEwMDAsIm1lbWJlcnMiOlt7ImFkZHJlc3NlcyI6W3siY29uZmlndXJhdGlvbkhhc2giOiI1MDI3ZDZiZTMwN2ZlYTYxYTY3NTE2N2FhMjExMjUwZDc1NTNhMTViNThjY2FmMDAxNDE3YzIyYjgzOTdkOGVmIiwiaWQiOiIqQSIsInBhcmVudElkIjoiKkQiLCJyZXNvdXJjZSI6ImlwdjRfYWRkcmVzcyJ9LHsiY29uZmlndXJhdGlvbkhhc2giOiI2MDU1NzBmYmE1MzBhYmQ3YzM5NjFiODhmMWZkN2YyNjhiNzA1MTUxZTlhNDY1NzA2MmFiMWVhMmY4MmE3NTU5IiwiaWQiOiIqZmYyMCIsInBhcmVudElkIjoiKmZmMTEiLCJyZXNvdXJjZSI6ImlwdjRfYWRkcmVzcyJ9LHsiY29uZmlndXJhdGlvbkhhc2giOiI4YjJmZjMzYmE4MTUxNDQyZmI0Yjk5Y2Q5Y2YxMWVkOWJlYjE0ZTI4NTgxNTJjZDI0YTk0ZDdlYThmY2ViM2IxIiwiaWQiOiIqZmYyMSIsInBhcmVudElkIjoiKmZmMTEiLCJyZXNvdXJjZSI6ImlwdjRfYWRkcmVzcyJ9LHsiY29uZmlndXJhdGlvbkhhc2giOiIxMTIwZGE0OTViMjVmN2RhN2U0NDQwNzUxMjNhOWU0NzhhZjlhOTFjYjk5ZTE2MTcwNDhhNDAwZTQ1MzYzZjRmIiwiaWQiOiIqMTkiLCJwYXJlbnRJZCI6IipEIiwicmVzb3VyY2UiOiJpcHY2X2FkZHJlc3MifSx7ImNvbmZpZ3VyYXRpb25IYXNoIjoiNTdlMWZiYmMwOGMzZTNiYWNkZTg0YjcyZDM2NWQ3YjVjNDY4MTEyNTM0MWZhNDM1NTI4ZmRmMzAzOTllMDdhMiIsImlkIjoiKjFCIiwicGFyZW50SWQiOiIqRCIsInJlc291cmNlIjoiaXB2Nl9hZGRyZXNzIn1dLCJjb25maWd1cmF0aW9uSGFzaCI6ImVjZDYxMDllZTJkMzQ1NjAxOGM3Mzg2ZmZkY2FmZTRjNzMyZDZjMDdlMGVhMDNhOTVjOTYwMWM4ZTczYTM2ZjAiLCJpZCI6IipEIiwibmFtZSI6Im1jcC10ZXN0LXZyZi1jb21wYW5pb24tMzg5MGE0ZmEtMmY2OS00ZmNjLWIxMWUtNWVhNGQxMjQ1Nzk3LWEiLCJwYXJlbnRzIjpbeyJjb25maWd1cmF0aW9uSGFzaCI6ImNhYjg0MzdlYzY5OWE0N2Y1NjI1YTk2ZmQ0NzM4YzNiNDhlZDNhNTI4YjNlMTZkMzM3NjE5YjY2YmUwOWY2YjMiLCJjb25maWd1cmF0aW9uSWQiOiIqRCIsImlkIjoiKkQiLCJuYW1lIjoibWNwLXRlc3QtdnJmLWNvbXBhbmlvbi0zODkwYTRmYS0yZjY5LTRmY2MtYjExZS01ZWE0ZDEyNDU3OTctYSIsInJlc291cmNlIjoiZ3JlIn0seyJjb25maWd1cmF0aW9uSGFzaCI6ImZiNGExZmMyZWVjYzZlYWRmM2U0Y2YwNDE0ZjVhMDMwNjI3ODkxNjY1NWYxMDVjOGM5YmQwODU5ODU2ODdmNzUiLCJjb25maWd1cmF0aW9uSWQiOiIqZmYxMSIsImlkIjoiKmZmMTEiLCJuYW1lIjoiZ3JlLXNvdXJjZS13ZyIsInJlc291cmNlIjoid2lyZWd1YXJkIn1dfV0sInJlcXVlc3QiOnsiYWN0aW9uIjoiY3JlYXRlIiwiZmllbGRzIjp7ImNvbW1lbnQiOiJtY3AtdGVzdC12cmYtY29tcGFuaW9uLTM4OTBhNGZhLTJmNjktNGZjYy1iMTFlLTVlYTRkMTI0NTc5NyIsImludGVyZmFjZXMiOiJtY3AtdGVzdC12cmYtY29tcGFuaW9uLTM4OTBhNGZhLTJmNjktNGZjYy1iMTFlLTVlYTRkMTI0NTc5Ny1hIiwibmFtZSI6Im1jcC10ZXN0LXZyZi1jb21wYW5pb24tMzg5MGE0ZmEtMmY2OS00ZmNjLWIxMWUtNWVhNGQxMjQ1Nzk3LXYifSwicGxhY2VCZWZvcmVJZCI6IiowIn0sInNuYXBzaG90SGFzaCI6IjYxZDRjZmM2MTEyNjdlYjgwMWUyMTJhOTk1ZjE2NTY4ZGQyZjFhOWNhZWU0M2FmN2JiNmQ1MTIxYzgyM2UxYjIiLCJ0aWNrZXRJZCI6ImNlNTI1MWViLTUwY2ItNDU2YS05ZjM0LTFmNWE5YjIwZTBhMyIsInZlcnNpb24iOjJ9.962f7d4246e57f9e660c3e250fbd088e7b73b3ddd5863b5456004fc53a8f6908", + "checkpointProofV1": "eyJ2ZXJzaW9uIjoyLCJzY29wZSI6eyJzZXJ2ZXJJZCI6Nywic291cmNlUHJvb2YiOiJhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhIiwiYWN0b3JJZCI6ImxlZ2FjeS1odW1hbiIsIm5hdGl2ZUlkIjoiKjQifSwiaGlzdG9yeSI6eyJzbmFwc2hvdCI6eyJraW5kIjoiY29uZmlndXJhdGlvbi1zaGEyNTYtdjEiLCJzZXJ2ZXJJZCI6Nywic291cmNlUHJvb2YiOiJhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhIiwiY2F0YWxvZ0RpZ2VzdHMiOnsiYnJpZGdlIjoiMzlmNzdkYjVlMzdlM2IzMGRiMjlhZTU3MjBkYTFjZTk1ODdkNzBlM2M4NTEyNmJhMTA2ZjJmYzM1NjI5MzUyNyIsImJyaWRnZV9wb3J0IjoiYWExY2IwZjI0ZjE5ZWVlYThiMTZkNjU4NWY3Mzg4ZTQ4MzI3NTczMjY3NmJhYzJlZTcxMWMxMGFmNmE3YjI2NCIsImJyaWRnZV92bGFuIjoiNzZjYzJmMTM5Nzk4Mzk0ODEwZDBkOTA5MzMzNmZmYzI1ZWMxOWMzNzM3ZTU5ZDgxYmNmMTJiM2NjNmZhMWMyOCIsImV0aGVybmV0IjoiZmYxOWVkYWIxODFkYzUyYmUyYTg3M2UyOGExYTUzODA5YTU1YWMwYjgwY2M3YTQ4MTIzNWM2NWU2ZGZkNTk1YyIsImdyZSI6ImI4OTEyNzA4YjAzNDk4NzA4NjQ4NTMzMGEzMjVlYTMzNWFiYjhkMmY0OWQyOGNkNjFhMjVmOTYzZDg1NDljYmYiLCJpbnRlcmZhY2VfaW52ZW50b3J5IjoiYmEzZWVlOGQxZGZkODA4MmUxZjZlY2Y5ZjdlYzA4NDFkNWNhMDc1ZmVkZTdiYmNmNzYyMzZjMmVmY2U0MmY4ZiIsImludGVyZmFjZV9saXN0X21lbWJlcnMiOiI1ODlmMmNkMzA5OWY1MWE3MzM4OGRkMzQxZGRjOWY4MmIxMTIyNjg4NDgxZTdhMjcxOTk5OWUxODYyZWFjZDkwIiwiaXBfZmlsdGVyIjoiNWI2NmI1YmZmYzNkOGU0MjVlMjRlNmJmYTNkYjE2MDkyYmQ3MWFmODVlMjc3NGFjNzFhOWE4YTk4ZGU0ZDVjOSIsImlwX3NldHRpbmdzIjoiZjc1NTNjODdiZThjZmViMTNhNWY1MTIwMjliZGEwZmMzNGFkMDUzY2UyZjcxMjg5ZTIwODk0OTI0MDNhMzliNyIsImlwdjRfYWRkcmVzcyI6ImI0YTQyYTQzN2NlZTgzYmZlY2RjNDQwZWVmNGEwNDA1NWY5MzYyNzI5M2VjNmExMTMyOWMyMWFjYjNhNzJhMWMiLCJpcHY0X3JvdXRlIjoiYTZiM2JlZTQyMWJlYzEyNzViNThlODMwYWY4MTY0NjI1N2NhOWRjNGY2YjY3NmE0ZTg0YzQxOTk4NDU3NzJlOCIsImlwdjZfYWRkcmVzcyI6ImY1YTJlMWFjN2NhNThhMmYxMTFlN2I0ZmFmMmZmNTE4MmNhM2Q0YzRiYjM3MWExZDRjODAyY2EzNjk0NTYzZWUiLCJpcHY2X2ZpbHRlciI6IjIzNzVmMDJmMDhlZTc2NjdlYWY5OWE3OTQwZjA3ZWVmOGI4OTMwNmIwMGI0YzQ5NTU4ODc3ZTM2YmMwN2Q2ZjQiLCJpcHY2X3JvdXRlIjoiOGY0ZDA3NjY0NzI0NmQyY2IxNTg2NTg4NjE5ZjUzNWQ0NWMwMTczYWQyOWRkYzNlZjExNmI0NjViYjY4NGUyMCIsImlwdjZfc2V0dGluZ3MiOiIzZDVhZWFjYzgxZjZjMTAxYzk5MTFmZmE5NmZjZmY5YmQ2MDM0M2UxZDFkNTlmNTRkNTNjZmJlM2JjMzYxYjIwIiwicm91dGluZ19ydWxlIjoiMDY3MGQ1OTIzMGVkNmJhNTczYjhlMzkwMDdiODJiMGI2ODNhMDkwYmFmZTY1YTBmNzEyMzBhZWZjYjhhZDhhMiIsInJvdXRpbmdfdGFibGUiOiJlOTMzMWRiMjU2ODczNjIwZmRkY2NmYzk2MmE3MjlmNjU5NjU2MzhjZjBiZWJiYzRmYTg0MjdlMGRiYjQ0OTEwIiwidnJmIjoiMzQ5OTlkZTIyM2FjNDU5MDc2OGUwNzY3ODE3NmNkMTVmYTA4OTgxMzRlYTU0ZTlmNDhmZWJkYTA3NTI5OGJiMyIsIndpcmVndWFyZCI6ImFjZTYyZGIyNTBlYjkxOGRjOWY0YWI3MzU2N2U5ODRhY2UyYjE2ZDhhN2U0OWIzYmM3MGY1MWQ1NmJlNWI0MzkiLCJ3aXJlZ3VhcmRfcGVlciI6IjBlOTg3MmQ5NWQzNTAwMTRkNjgzN2EyZjk0YTY1NzczYzA3NDZiMzVmMzJlNWM3ZmYyNzBlNjZlNzE0OGNlZTcifX0sInZyZklkIjoiKjQiLCJhY3RpdmVWaXJ0dWFsIjp7Ii5pZCI6IipGIiwiYWN0dWFsLW10dSI6IjY1NTM2IiwiY2FwcyI6IiIsImRpc2FibGVkIjoiZmFsc2UiLCJmbGFncyI6IiBVUCBSVU5OSU5HIE5PQVJQIE1BU1RFUiBMT1dFUl9VUCAoMHgxMDRjMSkiLCJmcC1ycHMtZHJvcCI6IjAiLCJmcC1yeC1ieXRlIjoiMCIsImZwLXJ4LXBhY2tldCI6IjAiLCJmcC10eC1ieXRlIjoiMCIsImZwLXR4LXBhY2tldCI6IjAiLCJpZCI6IjE1IiwiaWZpbmRleCI6IjE4IiwiaWZuYW1lIjoidnJmNCIsImxhc3QtbGluay11cC10aW1lIjoiMjAyNi0xMC0yNCAxNTozNDo0OCIsImxpbmstZG93bnMiOiIwIiwibWFjLWFkZHJlc3MiOiJGQTpEMTpGRDpFMDo0NTpERCIsIm10dSI6IjY1NTM2IiwibmFtZSI6Im1jcC10ZXN0LXZyZi1jb21wYW5pb24tMzg5MGE0ZmEtMmY2OS00ZmNjLWIxMWUtNWVhNGQxMjQ1Nzk3LXYiLCJydW5uaW5nIjoidHJ1ZSIsInJ4LWJ5dGUiOiIwIiwicngtZHJvcCI6IjAiLCJyeC1lcnJvciI6IjAiLCJyeC1wYWNrZXQiOiIwIiwic3RhdHVzIjoiLS1MRVJVLS1PLS0tLS0tLS0tLS0tLS0tIiwidHgtYnl0ZSI6IjAiLCJ0eC1kcm9wIjoiMCIsInR4LWVycm9yIjoiMCIsInR4LXBhY2tldCI6IjAiLCJ0eC1xdWV1ZS1kcm9wIjoiMCIsInR5cGUiOiJ2cmYiLCJ2cmYiOiJtYWluIn0sImFic2VudExsIjpbXSwicHJvdmVuYW5jZSI6eyJtaXNzaW5nTWVtYmVycyI6W10sImdyZU1lbWJlcnMiOlt7Ii5pZCI6IipEIiwibmFtZSI6Im1jcC10ZXN0LXZyZi1jb21wYW5pb24tMzg5MGE0ZmEtMmY2OS00ZmNjLWIxMWUtNWVhNGQxMjQ1Nzk3LWEiLCJ0eXBlIjoiZ3JlLXR1bm5lbCIsImdyZVBhcmVudCI6eyJjb25maWd1cmF0aW9uIjp7Ii5pZCI6IipEIiwibmFtZSI6Im1jcC10ZXN0LXZyZi1jb21wYW5pb24tMzg5MGE0ZmEtMmY2OS00ZmNjLWIxMWUtNWVhNGQxMjQ1Nzk3LWEiLCJsb2NhbC1hZGRyZXNzIjoiMTk4LjE4LjI0NS45IiwicmVtb3RlLWFkZHJlc3MiOiIxOTguMTguMjQ1LjEwIn0sInNvdXJjZUFkZHJlc3MiOnsiLmlkIjoiKmZmMjAiLCJpbnRlcmZhY2UiOiJncmUtc291cmNlLXdnIiwiYWRkcmVzcyI6IjE5OC4xOC4yNDUuOS8zMCIsImR5bmFtaWMiOiJmYWxzZSIsImRpc2FibGVkIjoiZmFsc2UiLCJ2cmYiOiJtYWluIn0sInNvdXJjZUludmVudG9yeSI6eyIuaWQiOiIqZmYxMSIsIm5hbWUiOiJncmUtc291cmNlLXdnIiwidHlwZSI6IndnIn0sInNvdXJjZUNvbmZpZ3VyYXRpb24iOnsiLmlkIjoiKmZmMTEiLCJuYW1lIjoiZ3JlLXNvdXJjZS13ZyJ9LCJzb3VyY2VSZXNvdXJjZSI6IndpcmVndWFyZCJ9fV19fX0.5e08c0fe549e780a6f0662649078b173eda76e166d254f8565e645f2504915e7" +} diff --git a/backend/src/services/flow-purge-partitions.test.ts b/backend/src/services/flow-purge-partitions.test.ts new file mode 100644 index 00000000..69d0e302 --- /dev/null +++ b/backend/src/services/flow-purge-partitions.test.ts @@ -0,0 +1,30 @@ +import assert from "node:assert/strict" +import pg, { type PoolClient } from "pg" +import { PartitionWriteError } from "./partition-write-transaction.js" +import { listExpiredPartitions, dropExpiredPartitions } from "../db/partitions.js" + +const drops:string[]=[],visited:string[]=[] +let fail=false +const fake=new pg.Pool() +fake.query=(async(sql:string,params?:unknown[])=>{ + if(["BEGIN","COMMIT","ROLLBACK"].includes(sql))return {rows:[],command:sql} + if(sql.startsWith("SET LOCAL")||sql.startsWith("SELECT pg_advisory_xact_lock_shared"))return {rows:[],command:sql.startsWith("SET LOCAL")?"SET":"SELECT"} + if(sql.includes("retention_days"))return{rows:[{retention_days:14}]} + if(sql.includes("FROM pg_inherits"))return{rows:params?.[0]==="flow_buckets"?[ + {relname:"flow_buckets_d_20260101"},{relname:"flow_buckets_d_20261024"}, + {relname:"unrelated_flow_buckets_d_20260101"},{relname:'flow_buckets_d_20260101";DROP TABLE servers;--'}, + {relname:"flow_buckets_d_20260230"},{relname:"flow_buckets_m_202601"}, + ]:[]} + if(sql.startsWith("DROP TABLE")){drops.push(sql);if(fail)throw Error("lost DROP response");return{rows:[]}} + throw Error("Unexpected SQL in owned partition fixture") +})as typeof fake.query +fake.connect=(async()=>({query:fake.query.bind(fake),release:()=>{}}as unknown as PoolClient))as typeof fake.connect +const around=new Date("2026-10-24T00:00:00Z") +assert.deepEqual(await listExpiredPartitions(fake,around),[{parent:"flow_buckets",name:"flow_buckets_d_20260101"}]) +await dropExpiredPartitions(fake,around,async partition=>{visited.push(`${partition.parent}/${partition.name}`)}) +assert.deepEqual(visited,["flow_buckets/flow_buckets_d_20260101"]) +assert.deepEqual(drops,['DROP TABLE IF EXISTS "public"."flow_buckets_d_20260101"']) +await assert.rejects(dropExpiredPartitions(fake,around,async()=>{throw Error("unapproved partition")}),PartitionWriteError) +assert.equal(drops.length,1) +fail=true;await assert.rejects(dropExpiredPartitions(fake,around),PartitionWriteError);assert.equal(drops.length,2) +console.log("Purge partition manifest: exact parent/kind/date names, quoted identifiers, bounded preview shared with cleanup, pre-dispatch fence and no DROP retry") diff --git a/backend/src/services/flow-purge-service.ts b/backend/src/services/flow-purge-service.ts new file mode 100644 index 00000000..889e9d13 --- /dev/null +++ b/backend/src/services/flow-purge-service.ts @@ -0,0 +1,12 @@ +import type { FlowPurgeDto } from "@mmapp/contracts/traffic-flow" +import { appendEvent } from "../modules/events/service/events-service.js" +import { completeTrafficFlowPurge, type FlowPurgeOptions } from "./traffic-flow-purge-execution.js" +import { purgeTrafficFlowStore } from "./traffic-flow-ingest.js" + +/** Internal journal only: this does not schedule alert-engine or the outbox. */ +export async function recordTrafficFlowPurge(result: FlowPurgeDto) { + await appendEvent({level:"warning",eventType:"traffic.flow.purge",sourceModule:"traffic",title:"Сброшены данные NetFlow", + message:`Удалены сессии ${result.deleted.buckets}, minute ${result.deleted.minuteStats}, daily ${result.deleted.dailyDims}`, + entityType:"traffic_flow",entityId:"purge",payload:{buckets:result.deleted.buckets,minuteStats:result.deleted.minuteStats,minuteDims:result.deleted.minuteDims,dailyDims:result.deleted.dailyDims,fileBytesBefore:result.fileBytesBefore,fileBytesAfter:result.fileBytesAfter,vacuumed:result.vacuumed}}) +} +export const purgeTrafficFlowWithEvent = (options: FlowPurgeOptions = {}) => completeTrafficFlowPurge({purge:()=>purgeTrafficFlowStore(options),record:async result=>{await options.beforeRecord?.();await recordTrafficFlowPurge(result)}}) diff --git a/backend/src/services/geoip-download.ts b/backend/src/services/geoip-download.ts index d8877230..26f58fab 100644 --- a/backend/src/services/geoip-download.ts +++ b/backend/src/services/geoip-download.ts @@ -4,12 +4,12 @@ const MAX_DOWNLOAD_BYTES = 64 * 1024 * 1024 /** A single deadline covers headers and streaming; a mirror cannot allocate an unbounded body. */ export async function downloadGeoipDatabase( url: string, headers: Record, fetcher: typeof fetch, - timeoutMs = DOWNLOAD_TIMEOUT_MS, maxBytes = MAX_DOWNLOAD_BYTES, + timeoutMs = DOWNLOAD_TIMEOUT_MS, maxBytes = MAX_DOWNLOAD_BYTES, signal?: AbortSignal, ): Promise<{ response: Response; body: Buffer }> { const controller = new AbortController() const timer = setTimeout(() => controller.abort(), timeoutMs) try { - const response = await fetcher(url, { headers, signal: controller.signal }) + const response = await fetcher(url, { headers, signal: signal ? AbortSignal.any([controller.signal, signal]) : controller.signal }) if (!response.ok || !response.body) { await response.body?.cancel().catch(() => {}) return { response, body: Buffer.alloc(0) } diff --git a/backend/src/services/geoip-file-proofs.ts b/backend/src/services/geoip-file-proofs.ts new file mode 100644 index 00000000..b8495e9e --- /dev/null +++ b/backend/src/services/geoip-file-proofs.ts @@ -0,0 +1,26 @@ +import { createHash } from "node:crypto" +import { open } from "node:fs/promises" +import path from "node:path" +import { GEOIP_COUNTRY_FILE, GEOIP_ASN_FILE } from "./traffic-flow-geoip.js" + +export type GeoipFileProof={name:string;present:false}|{name:string;present:true;bytes:number;sha256:string} +/** At most two fixed 64 MiB regular files; no caller-controlled filenames. */ +export async function readGeoipFileProofs(directory:string,signal?:AbortSignal):Promise{ + const files:GeoipFileProof[]=[] + for(const name of [GEOIP_COUNTRY_FILE,GEOIP_ASN_FILE]){ + signal?.throwIfAborted() + let file + try{file=await open(path.join(directory,name),"r")} + catch(error){if((error as NodeJS.ErrnoException).code==="ENOENT"){files.push({name,present:false});continue}throw Error("Локальная база GeoIP недоступна")} + try{ + const before=await file.stat() + if(!before.isFile()||before.size>64*1024*1024)throw Error("Локальная база GeoIP превышает предел или не является файлом") + const digest=createHash("sha256"),buffer=Buffer.alloc(64*1024);let bytes=0 + for(;;){signal?.throwIfAborted();const chunk=await file.read(buffer,0,buffer.length,null);if(!chunk.bytesRead)break;bytes+=chunk.bytesRead;if(bytes>64*1024*1024)throw Error("Размер базы GeoIP изменился во время чтения");digest.update(buffer.subarray(0,chunk.bytesRead))} + const after=await file.stat() + if(before.size!==after.size||before.mtimeMs!==after.mtimeMs||before.ctimeMs!==after.ctimeMs)throw Error("База GeoIP изменилась во время чтения") + files.push({name,present:true,bytes,sha256:digest.digest("hex")}) + }finally{await file.close()} + } + return files +} diff --git a/backend/src/services/geoip-manual-update.test.ts b/backend/src/services/geoip-manual-update.test.ts new file mode 100644 index 00000000..9a89b9d7 --- /dev/null +++ b/backend/src/services/geoip-manual-update.test.ts @@ -0,0 +1,73 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { mkdtemp, readFile, rm, writeFile, access } from "node:fs/promises" +import path from "node:path" +import { tmpdir } from "node:os" +import { setTimeout as delay } from "node:timers/promises" +import { collectGeoipUpdateOnce, getGeoipUpdateState, setGeoipFetchForTests, setGeoipValidateForTests, resetGeoipUpdateForTests } from "./geoip-update-collector.js" +import { disableGeoipDbForTests, resetGeoipSettingsForTests } from "./geoip-settings.js" +import { GEOIP_COUNTRY_FILE, resetGeoipForTests } from "./traffic-flow-geoip.js" + +const dir=await mkdtemp(path.join(tmpdir(),"geoip-dispatch-fence-")),oldDir=process.env.GEOIP_DIR +let calls=0,closed=false,started:()=>void=()=>{} +const server=createServer((_req,res)=>{calls++;res.writeHead(200);res.write("partial fixture");started();res.on("close",()=>{closed=true})}) +await new Promise(resolve=>server.listen(0,"127.0.0.1",resolve)) +const local=`http://127.0.0.1:${(server.address()as{port:number}).port}` +process.env.GEOIP_DIR=dir;disableGeoipDbForTests() +try{ + const denied=Error("access revoked before dispatch") + setGeoipFetchForTests(async()=>{calls++;return new Response("fixture")}) + await assert.rejects(collectGeoipUpdateOnce({force:true,beforeDispatch:async()=>{throw denied}}),error=>error===denied) + assert.equal(calls,0);assert.equal(getGeoipUpdateState().running,false) + setGeoipFetchForTests(((_url,options)=>fetch(local,options))as typeof fetch) + const controller=new AbortController(),firstStarted=new Promise(resolve=>{started=resolve}) + const pending=collectGeoipUpdateOnce({force:true,signal:controller.signal}) + const rejected=assert.rejects(pending);await firstStarted;controller.abort(Error("cancelled during body"));await rejected + await delay(50);assert.equal(calls,1,"Cancellation must forbid the ASN request");assert.equal(closed,true,"The in-flight response must close");assert.equal(getGeoipUpdateState().running,false) + await assert.rejects(access(path.join(dir,GEOIP_COUNTRY_FILE))) + let revoked=false + await writeFile(path.join(dir,GEOIP_COUNTRY_FILE),"original database") + setGeoipFetchForTests(async()=>{calls++;return new Response("fixture replacement")}) + setGeoipValidateForTests({country:async()=>{revoked=true;return "2026-01-01T00:00:00.000Z"},asn:async()=>"2026-01-01T00:00:00.000Z"}) + await assert.rejects(collectGeoipUpdateOnce({force:true,beforeDispatch:async()=>{if(revoked)throw denied}}),error=>error===denied) + assert.equal(calls,2,"Revocation after validation must forbid the ASN request") + assert.equal(await readFile(path.join(dir,GEOIP_COUNTRY_FILE),"utf8"),"original database","Revocation must fence the file replacement") + await assert.rejects(access(path.join(dir,GEOIP_COUNTRY_FILE)+".tmp")) + assert.equal(getGeoipUpdateState().running,false) + // A live environment change must never redirect the collector's publication. + revoked=false + const redirected=path.join(dir,"redirected") + setGeoipFetchForTests(async()=>{calls++;process.env.GEOIP_DIR=redirected;return new Response("fixture replacement")}) + await assert.rejects(collectGeoipUpdateOnce({force:true,beforeDispatch:async()=>{}}),/Источники GeoIP изменились/) + assert.equal(calls,3);assert.equal(await readFile(path.join(dir,GEOIP_COUNTRY_FILE),"utf8"),"original database") + await assert.rejects(access(redirected));process.env.GEOIP_DIR=dir + // Drift in another actor's file must be preserved, not overwritten by a + // download whose source fingerprint was approved earlier. + setGeoipValidateForTests({country:async()=>"2026-01-01T00:00:00.000Z",asn:async()=>"2026-01-01T00:00:00.000Z"}) + setGeoipFetchForTests(async()=>{calls++;await writeFile(path.join(dir,GEOIP_COUNTRY_FILE),"external file changed during download");return new Response("fixture replacement")}) + await assert.rejects(collectGeoipUpdateOnce({force:true,beforeDispatch:async()=>{}}),/Источники GeoIP изменились/) + assert.equal(calls,4);assert.equal(await readFile(path.join(dir,GEOIP_COUNTRY_FILE),"utf8"),"external file changed during download") + // Normal publication changes only its own expected file proof, so the next + // source and final metadata fences must continue to work. + setGeoipFetchForTests(async()=>{calls++;return new Response("fixture replacement")}) + const completed=await collectGeoipUpdateOnce({force:true,beforeDispatch:async()=>{}}) + assert.equal(completed.downloaded,2);assert.deepEqual(completed.errors,[]);assert.equal(calls,6) + const original=await readFile(path.join(dir,GEOIP_COUNTRY_FILE),"utf8"),previous=path.join(dir,GEOIP_COUNTRY_FILE)+".prev" + await rm(previous,{force:true}) + const denyBeforePublication=Error("access revoked after backup preparation") + await assert.rejects(collectGeoipUpdateOnce({force:true,beforeDispatch:async()=>{ + let backup:string|undefined + try{backup=await readFile(previous,"utf8")}catch(error){if((error as NodeJS.ErrnoException).code!=="ENOENT")throw error} + if(backup!==undefined){assert.equal(backup,original);throw denyBeforePublication} + }}),error=>error===denyBeforePublication) + assert.equal(calls,7,"Revocation after backup copy must forbid the ASN request") + assert.equal(await readFile(path.join(dir,GEOIP_COUNTRY_FILE),"utf8"),original,"The installed database must remain available until atomic publication") + await assert.rejects(access(path.join(dir,GEOIP_COUNTRY_FILE)+".tmp")) + console.log("GeoIP dispatch fences: preflight denial makes no request; body cancellation closes HTTP and skips next source; post-validation revocation preserves original file and removes temporary file") + console.log("GeoIP source fences: directory/file drift during download prevents publication and next source; own two-file replacements advance only their expected proofs") + console.log("GeoIP publication: revocation after preparing the previous backup preserves the original installed database and removes the uncommitted temporary file") +}finally{ + resetGeoipUpdateForTests();resetGeoipSettingsForTests();resetGeoipForTests();server.closeAllConnections();await new Promise(resolve=>server.close(()=>resolve())) + if(oldDir===undefined)delete process.env.GEOIP_DIR;else process.env.GEOIP_DIR=oldDir + assert.ok(path.resolve(dir).startsWith(path.resolve(tmpdir())+path.sep));await rm(dir,{recursive:true,force:true}) +} diff --git a/backend/src/services/geoip-manual-update.ts b/backend/src/services/geoip-manual-update.ts new file mode 100644 index 00000000..5601eb86 --- /dev/null +++ b/backend/src/services/geoip-manual-update.ts @@ -0,0 +1,38 @@ +import { collectGeoipUpdateOnce, getGeoipUpdateState } from "./geoip-update-collector.js" +import { pool } from "../db/index.js" +import { GEOIP_COUNTRY_FILE, GEOIP_ASN_FILE, geoipDir } from "./traffic-flow-geoip.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { McpError } from "../modules/mcp/security.js" +import { readGeoipFileProofs } from "./geoip-file-proofs.js" + +export async function readGeoipManualMetadata(){ + const row=(await pool.query<{value:Record|null}>("SELECT CASE WHEN octet_length(row_to_json(g)::text)<=65536 THEN row_to_json(g) ELSE NULL END AS value FROM geoip_settings g WHERE id=1")).rows[0] + if(row&&row.value===null)throw new McpError("STATE_UNAVAILABLE","Метаданные GeoIP превышают предел",503) + const value=row?.value + const etags=value?.etags_json??{} + if(!etags||typeof etags!=="object"||Array.isArray(etags))throw new McpError("STATE_UNAVAILABLE","Некорректные метаданные GeoIP",503) + const timestamp=(key:string)=>{const entry=value?.[key];if(entry===undefined||entry===null)return null;if(typeof entry!=="string"||entry.length>100)throw new McpError("STATE_UNAVAILABLE","Некорректное время GeoIP",503);return entry} + if(value&&(typeof value.enabled!=="boolean"||!Number.isSafeInteger(value.update_interval_sec)))throw new McpError("STATE_UNAVAILABLE","Некорректные настройки GeoIP",503) + return {etags:etags as Record,settings:{enabled:value?.enabled??true,updateIntervalSec:value?.update_interval_sec??604800,lastCheckAt:timestamp("last_check_at"),lastSuccessAt:timestamp("last_success_at"),countryBuildAt:timestamp("country_build_at"),asnBuildAt:timestamp("asn_build_at")}} +} +/** Fixed local sources only; never follows input paths or contacts the mirror. */ +export async function readGeoipManualUpdateState(signal?:AbortSignal){ + signal?.throwIfAborted() + const {settings,etags}=await readGeoipManualMetadata() + const files=await readGeoipFileProofs(geoipDir(),signal) + const tags=Object.fromEntries([GEOIP_COUNTRY_FILE,GEOIP_ASN_FILE].map(name=>[name,etags[name]??null])) + if(Object.values(tags).some(value=>value!==null&&(typeof value!=="string"||value.length>4096)))throw new McpError("STATE_UNAVAILABLE","ETag GeoIP превышает предел",503) + signal?.throwIfAborted() + return {running:getGeoipUpdateState().running,directory:geoipDir(),files,etags:tags,settings:{enabled:settings.enabled,updateIntervalSec:settings.updateIntervalSec,lastCheckAt:settings.lastCheckAt,lastSuccessAt:settings.lastSuccessAt,countryBuildAt:settings.countryBuildAt,asnBuildAt:settings.asnBuildAt}} +} + +/** Same forced collector for REST and MCP, including partial per-file results. */ +export async function updateGeoipManually(options:{beforeStart?:()=>Promise;beforeDispatch?:()=>Promise;signal?:AbortSignal;onDispatch?:()=>void}={}){ + await options.beforeDispatch?.();options.signal?.throwIfAborted() + return executeGuardedApplicationWrite({kind:"geoip-update",id:"1"},"all",async()=>{ + await options.beforeDispatch?.();options.signal?.throwIfAborted() + await options.beforeStart?.() + const snapshot=await collectGeoipUpdateOnce({force:true,...options}) + return {ok:!snapshot.fatalError&&snapshot.errors.length===0,snapshot} + },async()=>[{kind:"geoip-settings",id:"1"}]) +} diff --git a/backend/src/services/geoip-settings.ts b/backend/src/services/geoip-settings.ts index db0eb20b..ede664cc 100644 --- a/backend/src/services/geoip-settings.ts +++ b/backend/src/services/geoip-settings.ts @@ -2,6 +2,8 @@ import { eq } from "drizzle-orm" import { db } from "../db/index.js" import { geoipSettings } from "../db/schema.js" import type { GeoipSettingsDto, GeoipSettingsPatch } from "@mmapp/contracts/geoip" +import { geoipSettingsPatchSchema } from "@mmapp/contracts/geoip" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" const SETTINGS_ID = 1 const DEFAULT_INTERVAL_SEC = 604800 @@ -57,18 +59,22 @@ export async function getGeoipEtags(): Promise> { } export async function updateGeoipSettings(patch: GeoipSettingsPatch): Promise { - const prev = await getGeoipSettingsRow() - const next = { - enabled: patch.enabled ?? prev?.enabled ?? true, - updateIntervalSec: patch.updateIntervalSec ?? prev?.updateIntervalSec ?? DEFAULT_INTERVAL_SEC, - updatedAt: new Date().toISOString(), - } - if (prev) { - await db.update(geoipSettings).set(next).where(eq(geoipSettings.id, SETTINGS_ID)) - } else { - await db.insert(geoipSettings).values({ id: SETTINGS_ID, ...next }) - } - return getGeoipSettings() + const settings = geoipSettingsPatchSchema.parse(patch) + if (!dbEnabled) return getGeoipSettings() + return executeGuardedApplicationWrite({ kind: "geoip-settings", id: "1" }, "all", async () => { + const prev = await getGeoipSettingsRow() + const next = { + enabled: settings.enabled ?? prev?.enabled ?? true, + updateIntervalSec: settings.updateIntervalSec ?? prev?.updateIntervalSec ?? DEFAULT_INTERVAL_SEC, + updatedAt: new Date().toISOString(), + } + if (prev) { + await db.update(geoipSettings).set(next).where(eq(geoipSettings.id, SETTINGS_ID)) + } else { + await db.insert(geoipSettings).values({ id: SETTINGS_ID, ...next }) + } + return getGeoipSettings() + }) } export async function touchGeoipRunMeta(patch: { @@ -79,6 +85,7 @@ export async function touchGeoipRunMeta(patch: { asnBuildAt?: string | null etags?: Record }): Promise { + if (!dbEnabled) return const prev = await getGeoipSettingsRow() const set: Partial = { updatedAt: new Date().toISOString(), diff --git a/backend/src/services/geoip-status.ts b/backend/src/services/geoip-status.ts new file mode 100644 index 00000000..d29da1bc --- /dev/null +++ b/backend/src/services/geoip-status.ts @@ -0,0 +1,19 @@ +import { getGeoipSettings } from "./geoip-settings.js" +import { GEOIP_ASN_FILE, GEOIP_COUNTRY_FILE, geoipReadersStatus, initGeoip } from "./traffic-flow-geoip.js" +import { getGeoipUpdateState } from "./geoip-update-collector.js" + +/** Reads local readers and persisted update metadata; never starts a download. */ +export async function buildGeoipStatus() { + await initGeoip() + const readers = geoipReadersStatus() + return { + ready: readers.countryLoaded && readers.asnLoaded, + countryLoaded: readers.countryLoaded, + asnLoaded: readers.asnLoaded, + countryFile: GEOIP_COUNTRY_FILE, + asnFile: GEOIP_ASN_FILE, + dir: readers.dir, + running: getGeoipUpdateState().running, + settings: await getGeoipSettings(), + } +} diff --git a/backend/src/services/geoip-update-collector.ts b/backend/src/services/geoip-update-collector.ts index 43462ed9..00fc995c 100644 --- a/backend/src/services/geoip-update-collector.ts +++ b/backend/src/services/geoip-update-collector.ts @@ -1,5 +1,7 @@ -import { rename, rm, mkdir, writeFile } from "node:fs/promises" +import { copyFile, rename, rm, mkdir, writeFile } from "node:fs/promises" import path from "node:path" +import { createHash } from "node:crypto" +import { readGeoipFileProofs } from "./geoip-file-proofs.js" import { open, type AsnResponse, type CountryResponse } from "maxmind" import type { GeoipUpdateRunSnapshot } from "../types/scheduler-run-snapshot.js" import { SCHEDULER_RUN_SNAPSHOT_VERSION } from "../types/scheduler-run-snapshot.js" @@ -73,8 +75,15 @@ export function resetGeoipUpdateForTests(): void { * (304 = не меняем файл), валидация пробоем 8.8.8.8, атомарная подмена через rename. */ export async function collectGeoipUpdateOnce( - opts: { force?: boolean } = {}, + opts: { force?: boolean; signal?: AbortSignal; beforeDispatch?: () => Promise; onDispatch?: () => void } = {}, ): Promise { + let fenceFailed = false + let checkSources: (() => Promise) | undefined + const fence = async () => { + try { opts.signal?.throwIfAborted(); await opts.beforeDispatch?.(); await checkSources?.(); opts.signal?.throwIfAborted() } + catch (error) { fenceFailed = true; throw error } + } + await fence() const sampledAt = new Date().toISOString() if (updating) { if (opts.force) { @@ -100,8 +109,16 @@ export async function collectGeoipUpdateOnce( const settings = await getGeoipSettings() if (!settings.enabled && !opts.force) return emptySnapshot(sampledAt, true) const dir = geoipDir() - await mkdir(dir, { recursive: true }) const storedEtags = await getGeoipEtags() + let expectedFiles = opts.beforeDispatch ? await readGeoipFileProofs(dir,opts.signal) : undefined + const metadata = (value:typeof settings) => ({enabled:value.enabled,updateIntervalSec:value.updateIntervalSec,lastCheckAt:value.lastCheckAt,lastSuccessAt:value.lastSuccessAt,lastError:value.lastError,countryBuildAt:value.countryBuildAt,asnBuildAt:value.asnBuildAt}) + const expectedMetadata = JSON.stringify(metadata(settings)) + if(opts.beforeDispatch)checkSources=async()=>{ + if(geoipDir()!==dir||JSON.stringify(await readGeoipFileProofs(dir,opts.signal))!==JSON.stringify(expectedFiles)||JSON.stringify(await getGeoipEtags())!==JSON.stringify(storedEtags)||JSON.stringify(metadata(await getGeoipSettings()))!==expectedMetadata)throw Object.assign(new Error("Источники GeoIP изменились перед dispatch"),{statusCode:409}) + } + await fence() + opts.onDispatch?.() + await mkdir(dir, { recursive: true }) const etags: Record = {} const buildAt: Partial> = {} @@ -119,10 +136,12 @@ export async function collectGeoipUpdateOnce( cachedFileValid = true } catch { /* A missing or invalid local cache requires a full download. */ } } + await fence() const { response: res, body } = await downloadGeoipDatabase( `${MIRROR_BASE}/${file}`, prevEtag && cachedFileValid ? { "If-None-Match": prevEtag } : {}, fetchImpl, + undefined, undefined, opts.signal, ) if (res.status === 304) { if (!cachedFileValid || !prevEtag) throw new Error("Зеркало вернуло HTTP 304 без проверенной локальной базы") @@ -133,31 +152,39 @@ export async function collectGeoipUpdateOnce( if (!res.ok) throw new Error(`HTTP ${res.status}`) const etag = res.headers.get("etag") ?? "" snapshot.bytes += body.byteLength - await writeFile(tmp, body) + await fence() + await writeFile(tmp, body, { flag: "wx" }) buildAt[kind] = kind === "country" ? await validateCountry(tmp) : await validateAsn(tmp) const prevFile = `${target}.prev` + await fence() await rm(prevFile, { force: true }) - await rename(target, prevFile).catch(() => { - /* текущего файла могло ещё не быть */ + await fence() + await copyFile(target, prevFile).catch(error => { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error }) + await fence() await rename(tmp, target) + if(expectedFiles)expectedFiles=expectedFiles.map(proof=>proof.name===file?{name:file,present:true,bytes:body.byteLength,sha256:createHash("sha256").update(body).digest("hex")}:proof) snapshot.downloaded += 1 if (etag) etags[file] = etag } catch (e) { await rm(tmp, { force: true }).catch(() => { /* best-effort */ }) + if (fenceFailed || opts.signal?.aborted) throw e const message = e instanceof Error ? e.message : String(e) snapshot.errors.push(`${file}: ${message}`) } } if (snapshot.downloaded > 0) { + await fence() await reloadGeoipReaders() } + await fence() await touchGeoipRunMeta({ lastCheckAt: sampledAt, ...(snapshot.errors.length ? {} : { lastSuccessAt: sampledAt }), @@ -168,8 +195,10 @@ export async function collectGeoipUpdateOnce( }) return snapshot } catch (e) { + if (fenceFailed || opts.signal?.aborted) throw e const message = e instanceof Error ? e.message : String(e) snapshot.fatalError = message + await fence() await touchGeoipRunMeta({ lastCheckAt: sampledAt, lastError: message, diff --git a/backend/src/services/gre-live.ts b/backend/src/services/gre-live.ts index 98da3072..97598bf9 100644 --- a/backend/src/services/gre-live.ts +++ b/backend/src/services/gre-live.ts @@ -13,6 +13,7 @@ import { } from "./entity-snapshots.js" import { captureAndAppendRevision } from "./config-revisions.js" import { readGreObservedState, readGreNativeRows } from "./gre-observed-state.js" +import { observeGreCatalog } from "./gre-observed-read.js" type ServerRow = typeof servers.$inferSelect @@ -143,8 +144,9 @@ export function mapGreLive( const ifaceAddrs = addrsByIface.get(name) ?? [] ifaceAddrs.sort((a, b) => Number(Boolean(a.disabled)) - Number(Boolean(b.disabled)) || a.address.localeCompare(b.address)) const localInnerIp = ifaceAddrs[0]?.address || fromComment.localInnerIp - const secret = (g["ipsec-secret"] ?? "").trim() + const secret = g["ipsec-secret"] ?? "" const { disabled, status } = readGreObservedState(g, name) + const numericMtu = Number.parseInt(g.mtu ?? "1476", 10) || 1476 gre.push({ name, @@ -153,7 +155,7 @@ export function mapGreLive( remoteAddress: g["remote-address"] ?? "", comment: g.comment ?? "", disabled, - mtu: Number.parseInt(g.mtu ?? "1476", 10) || 1476, + mtu: g.mtu === "auto" ? g.mtu : numericMtu, keepalive: g.keepalive ?? "0", dscp: g.dscp ?? "inherit", clampTcpMss: g["clamp-tcp-mss"] !== "false" && g["clamp-tcp-mss"] !== "no", @@ -176,7 +178,7 @@ export function mapGreLive( remoteInnerIp: fromComment.remoteInnerIp, poolId: "live", ipsec: secret ? { secret } : null, - mtu: Number.parseInt(g.mtu ?? "1476", 10) || 1476, + mtu: numericMtu, keepaliveInterval: keepalive.interval, keepaliveRetries: keepalive.retries, dscp: parseDscp(g.dscp), @@ -215,7 +217,7 @@ export function mapGreLive( } } -export async function fetchGreState(server: ServerRow) { +export async function fetchGreNativeState(server: ServerRow) { const client = MikrotikClient.fromServer(server) const [greRaw, addrsRaw, ipv6Raw] = await Promise.all([ client.get("/interface/gre"), @@ -223,6 +225,11 @@ export async function fetchGreState(server: ServerRow) { client.get("/ipv6/address"), ]) if (![greRaw, addrsRaw, ipv6Raw].every(Array.isArray)) throw new Error("RouterOS: неполный каталог GRE/адресов") + return { client, greRaw, addrsRaw, ipv6Raw } +} + +export async function fetchGreState(server: ServerRow) { + const { client, greRaw, addrsRaw, ipv6Raw } = await fetchGreNativeState(server) return { client, ...mapGreLive(server, Array.isArray(greRaw) ? greRaw : [], [...(Array.isArray(addrsRaw) ? addrsRaw : []), ...(Array.isArray(ipv6Raw) ? ipv6Raw : [])]), @@ -250,27 +257,37 @@ export async function listGreTunnels(opts?: { serverId?: string }): Promise<{ serverRows = await db.select().from(servers).where(eq(servers.enabled, true)) } + const { tunnels, failures } = await pollGreServers(serverRows, true) + return { tunnels, failures } +} + +/** REST retains observed revisions; MCP reads deliberately do not store secret-bearing history. */ +export async function pollGreServers(serverRows: ServerRow[], recordHistory = false) { const failures: Array<{ serverId: string; serverName?: string; error: string }> = [] const chunks = await Promise.all( - serverRows.map(async (server) => { + serverRows.filter(server => server.enabled).map(async (server) => { try { + if (!recordHistory) { + const { greRaw, addrsRaw, ipv6Raw } = await fetchGreNativeState(server) + return { tunnels: [] as LiveGreTunnel[], observed: observeGreCatalog(server.id, greRaw, addrsRaw, ipv6Raw) } + } const state = await fetchGreState(server) - await captureAndAppendRevision({ + if (recordHistory) await captureAndAppendRevision({ serverId: server.id, section: "gre", source: "observed", capture: async () => state.snapshot, }) - return state.tunnels + return { tunnels: state.tunnels, observed: [] as ReturnType } } catch (e) { failures.push({ serverId: String(server.id), serverName: server.name ?? undefined, - error: e instanceof Error ? e.message : String(e), + error: recordHistory ? e instanceof Error ? e.message : String(e) : "Не удалось подтвердить каталог GRE/адресов", }) - return [] as LiveGreTunnel[] + return { tunnels: [] as LiveGreTunnel[], observed: [] as ReturnType } } }), ) - return { tunnels: chunks.flat(), failures } + return { tunnels: chunks.flatMap(chunk => chunk.tunnels), observed: chunks.flatMap(chunk => chunk.observed), failures } } diff --git a/backend/src/services/gre-mutation-input.ts b/backend/src/services/gre-mutation-input.ts new file mode 100644 index 00000000..d6bb654a --- /dev/null +++ b/backend/src/services/gre-mutation-input.ts @@ -0,0 +1,61 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { canonicalGreSnapshot, greAddressPath } from "./entity-snapshots.js" +import { formatKeepalive, parseKeepalive } from "./gre-live.js" +import { readGreComment, writeGreComment } from "./gre-comment.js" + +const name = z.string().trim().min(1).max(100) +const outer = z.string().refine(value => isIP(value) === 4, "Требуется IPv4 endpoint GRE") +const localOuter = z.union([z.literal(""), outer]) +const nativeRemote = name.refine(value => { + const [host, vrf, extra] = value.split("@") + if (extra !== undefined || vrf === "" || vrf !== undefined && /[\u0000-\u001f\u007f]/.test(vrf)) return false + if (isIP(host) === 4) return true + return !/^[\d.]+$/.test(host) && host.replace(/\.$/, "").split(".").every(label => /^[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?$/i.test(label)) +}, "Требуется IPv4 или DNS endpoint GRE") +const nativeKeepalive = z.string().max(100).refine(value => { + if (!/^[^,\s]+,\d+$/.test(value)) return false + const { interval, retries } = parseKeepalive(value) + return interval >= 1 && retries >= 1 +}, "Укажите keepalive в формате интервал от 1 секунды,число попыток от 1") +const inner = z.string().max(100).refine(value => { if (!value) return true; try { greAddressPath(value); return true } catch { return false } }, "Требуется IPv4/IPv6 с префиксом") +export const greMtuSchema = z.union([z.number().int().min(68).max(65535), z.literal("auto")]) +export const greNativeFieldsSchema = z.object({ + name: name.optional(), + comment: z.string().max(1000).optional(), + disabled: z.boolean().optional(), + "local-address": localOuter.optional(), + "remote-address": nativeRemote.optional(), + mtu: greMtuSchema.optional(), + keepalive: nativeKeepalive.optional(), +}).strict() +const fields = { + name, localAddress: localOuter.optional(), remoteAddress: outer, + localInnerIp: inner.optional(), remoteInnerIp: z.string().max(100).optional(), comment: z.string().max(1000).optional(), enabled: z.boolean().optional(), + mtu: greMtuSchema.optional(), keepaliveInterval: z.number().int().min(1).max(4294967295).optional(), keepaliveRetries: z.number().int().min(1).max(4294967295).optional(), + dscp: z.union([z.literal("inherit"), z.number().int().min(0).max(63), z.string().regex(/^\d{1,2}$/).refine(value => Number(value) <= 63)]).optional(), + clampTcpMss: z.boolean().optional(), allowFastPath: z.boolean().optional(), ipsecSecret: z.string().max(1024).optional(), +} +export const greCreateFieldsSchema = z.object(fields).strict() +export const greUpdateFieldsSchema = greCreateFieldsSchema.omit({ name: true }).partial().strict() +export const greMutationSchema = z.discriminatedUnion("operation", [ + z.object({ operation: z.literal("create"), fields: greCreateFieldsSchema }).strict(), + z.object({ operation: z.literal("update"), rosId: z.string().regex(/^\*[A-Za-z0-9]+$/).optional(), name: name.optional(), fields: greUpdateFieldsSchema }).strict().refine(value => !!(value.rosId || value.name), "Требуется native ID или имя GRE"), + z.object({ operation: z.literal("delete"), rosId: z.string().regex(/^\*[A-Za-z0-9]+$/).optional(), name: name.optional() }).strict().refine(value => !!(value.rosId || value.name), "Требуется native ID или имя GRE"), +]).superRefine((value, context) => { + if (value.operation !== "delete" && value.fields.ipsecSecret?.trim() && value.fields.allowFastPath === true) context.addIssue({code:"custom",message:"IPsec несовместим с FastPath",path:["fields","allowFastPath"]}) +}) +export type GreMutation = z.infer +export const GreTunnelWriteSchema = greCreateFieldsSchema.extend({ serverId: z.string().min(1), rosId: z.string().optional() }).strip() +export const GreTunnelKeySchema = z.object({ serverId: z.string().min(1), rosId: z.string().optional(), name: z.string().optional(), disabled: z.boolean().optional() }) + +export function greTunnelFromBody(body: z.infer) { + if (body.ipsecSecret?.trim() && body.allowFastPath === true) throw Object.assign(new Error("IPsec несовместим с FastPath"), { statusCode: 400 }) + return canonicalGreSnapshot({ tunnels: [{ + name: body.name, localAddress: body.localAddress ?? "", remoteAddress: body.remoteAddress, localInnerIp: body.localInnerIp ?? "", + remoteInnerIp: body.remoteInnerIp ?? readGreComment(body.comment).remoteInnerIp, + comment: body.remoteInnerIp === undefined ? body.comment ?? "" : writeGreComment(body.comment ?? "", body.remoteInnerIp), disabled: body.enabled === false, + mtu: body.mtu ?? 1476, keepalive: formatKeepalive(body.keepaliveInterval ?? 10, body.keepaliveRetries ?? 10), dscp: body.dscp === undefined ? "inherit" : String(body.dscp), + clampTcpMss: body.clampTcpMss ?? true, allowFastPath: body.allowFastPath ?? !body.ipsecSecret?.trim(), ipsecSecret: body.ipsecSecret ?? "", + }] }).tunnels[0]! +} diff --git a/backend/src/services/gre-mutation-state.ts b/backend/src/services/gre-mutation-state.ts new file mode 100644 index 00000000..fc11bfdf --- /dev/null +++ b/backend/src/services/gre-mutation-state.ts @@ -0,0 +1,202 @@ +import { pool } from "../db/index.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint } from "../modules/mcp/security.js" +import { MikrotikClient, MikrotikError } from "./mikrotik.js" +import { observedBoolean, observedDurationMs, observedInteger } from "./ospf-observed-values.js" +import { readGreNativeRows } from "./gre-observed-state.js" +import { greAddressKey } from "./entity-snapshots.js" +import type { GreMutation } from "./gre-mutation-input.js" +import { greDependencyPaths } from "./routeros-native-paths.js" +export { greDependencyPaths } from "./routeros-native-paths.js" + +export const greMutationProblem = (message: string) => Object.assign(new Error(message), { statusCode: 409 }) +export type GreNativeRow = Record +export interface GreMutationState { + serverConfiguration: string; gre: GreNativeRow[]; ipv4: GreNativeRow[]; ipv6: GreNativeRow[]; + restoreProtectionProof?:string; + dependencies: Record; + bindings: { id: string; interfaceName: string }[]; filters: { id: number; gatewayTunnelId: string; gateway: string }[]; +} +const bools = new Set(["disabled", "dynamic", "invalid", "clamp-tcp-mss", "allow-fast-path", "advertise", "no-dad", "eui-64"]) +const runtime = new Set(["running", "invalid", "actual-mtu", "l2mtu", "last-link-down-time", "last-link-up-time", "link-downs", "active", "inactive", "immediate-gw", "gateway-status", "belongs-to", "hw-offloaded", "debug-info", "uptime", "last-update", "bytes", "packets"]) +function rows(raw: unknown, required: string[], maximum = 2000): GreNativeRow[] { + if (!Array.isArray(raw) || raw.length > maximum || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || required.some(key => typeof row[key] !== "string" || !row[key].trim()) || typeof row[".id"] !== "string" || !/^\*[A-Za-z0-9]+$/.test(row[".id"]))) throw greMutationProblem("Неполный native-каталог зависимостей GRE") + if (new Set(raw.map(row => row[".id"])).size !== raw.length) throw greMutationProblem("Повторяющиеся native ID GRE/зависимостей") + return raw as GreNativeRow[] +} +export function greTarget(input: GreMutation, state: GreMutationState) { + const name = input.operation === "create" ? input.fields.name : input.name + const id = input.operation === "create" ? undefined : input.rosId + const target = state.gre.find(row => id ? row[".id"] === id : row.name === name) + if (target && name && target.name !== name) throw greMutationProblem("ID и имя GRE указывают на разные объекты") + return target +} +async function hydrate(client: MikrotikClient, path: string, row: GreNativeRow, keys: string[]) { + for (const key of keys) if (row[key] === undefined) { + let value: {ret?:string} + try { value = await client.post<{ ret?: string }>(path + "/get", { ".id": row[".id"], "value-name": key }) } + catch { throw greMutationProblem("Не подтверждено свойство GRE/адреса: " + path + "/" + key) } + if (typeof value?.ret !== "string") throw greMutationProblem("Не подтверждено свойство GRE/адреса: " + path + "/" + key) + row[key] = value.ret + } +} +/** An empty /get response is unknown. Native flags can be absent on static + * interfaces rather than carry a false value. Use the documented API property + * presence/absence queries, with exact identity and complementary results. */ +async function hydrateGreDynamic(client:MikrotikClient,parent:GreNativeRow){ + const value=await client.post("/interface/get",{".id":parent[".id"],"value-name":"dynamic"}) + if(value&&typeof value==="object"&&"ret"in value&&typeof value.ret==="string"){ + parent.dynamic=value.ret;return + } + if(!Array.isArray(value)||value.length!==0)throw greMutationProblem("Не подтверждено свойство GRE/адреса: /interface/dynamic") + const query=async(flag:boolean)=>rows(await client.post("/interface/print",{".proplist":".id,name,type,dynamic",".query":[".id="+parent[".id"],flag?"dynamic":"-dynamic","#&"]}),[".id","name"],1) + const staticRows=await query(false),dynamicRows=await query(true) + if(staticRows.length!==1||dynamicRows.length!==0||staticRows[0][".id"]!==parent[".id"]||staticRows[0].name!==parent.name||staticRows[0].type!==parent.type||staticRows[0].dynamic!==undefined&&observedBoolean(staticRows[0].dynamic)!==false)throw greMutationProblem("Не подтверждено статическое происхождение GRE через native query") + parent.dynamic="false" +} +/** Read only the explicitly addressed sensitive property; absence is unknown, + * not an empty PSK. The print flag also handles masked get/print defaults. */ +export async function readGrePskValue(row: GreNativeRow, print: (body: Record)=>Promise, options?: {get:()=>Promise;capability:()=>Promise}) { + if (typeof row["ipsec-secret"] === "string" && !/^\*+$|^\(hidden\)$/.test(row["ipsec-secret"])) return row["ipsec-secret"] + if (options) { + const capability = await options.capability().catch(()=>undefined) + const value = await options.get().catch(()=>undefined) + if (value && typeof value === "object" && "ret" in value && typeof value.ret === "string" && !/^\*+$|^\(hidden\)$/.test(value.ret)) return value.ret + // RouterOS returns [] for the successful get of an unset string. Accept + // that only with unchanged, positively observed sensitive access; a hidden + // or otherwise unverified property remains unknown. + if (Array.isArray(value) && value.length===0 && capability && await options.capability().catch(()=>undefined)===capability) return "" + } + let raw: unknown + try { raw = await print({"show-sensitive":"", ".proplist":".id,ipsec-secret", ".query":[".id="+row[".id"]]}) } + catch { throw greMutationProblem("Не подтверждено чувствительное свойство GRE: /interface/gre/ipsec-secret") } + if (!Array.isArray(raw)||raw.length!==1||raw[0]?.[".id"]!==row[".id"]||typeof raw[0]?.["ipsec-secret"]!=="string"||/^\*+$|^\(hidden\)$/.test(raw[0]["ipsec-secret"])) throw greMutationProblem("Не подтверждено чувствительное свойство GRE: /interface/gre/ipsec-secret") + return raw[0]["ipsec-secret"] as string +} +async function sensitiveAccessFingerprint(server: ServerRow, client: MikrotikClient) { + const users = await client.get("/user?name="+encodeURIComponent(server.username)+"&.proplist=.id,name,group") + if (!Array.isArray(users)||users.length!==1||users[0]?.name!==server.username||typeof users[0]?.group!=="string") return undefined + const group = users[0].group + const groups = await client.get("/user/group?name="+encodeURIComponent(group)+"&.proplist=.id,name,policy") + if (!Array.isArray(groups)||groups.length!==1||groups[0]?.name!==group||typeof groups[0]?.policy!=="string"||!groups[0].policy.split(",").includes("sensitive")) return undefined + return fingerprint({user:users[0],group:groups[0]}) +} +/** Reject only after every sibling has settled; a failed source aborts the batch. */ +async function settledGreReads(tasks:{[K in keyof T]:Promise},stop:(error:unknown)=>unknown):Promise{ + const results=await Promise.allSettled(tasks.map(task=>task.catch(error=>{throw stop(error)}))) + const rejected=results.find((result):result is PromiseRejectedResult=>result.status==="rejected") + if(rejected)throw stop(rejected.reason) + return results.map(result=>(result as PromiseFulfilledResult).value)as unknown as T +} +export async function readGreMutationState(server: ServerRow, input: GreMutation|null, options: {identityOnly?:boolean;allTargets?:boolean;beforeRead?:()=>Promise;signal?:AbortSignal;deadlineAt?:number} = {}): Promise { + if (!server.enabled) throw greMutationProblem("Сервер отключён") + if(!input&&!options.allTargets)throw greMutationProblem("Не указан GRE") + await options.beforeRead?.();options.signal?.throwIfAborted() + const client = MikrotikClient.fromServer(server) + const readAbort=new AbortController();let failed=false,firstFailure:unknown,batchActive=true + const stop=(error:unknown)=>{if(!failed){failed=true;firstFailure=options.signal?.aborted?options.signal.reason:error;readAbort.abort(firstFailure)}return firstFailure} + { + const limited=!!(options.beforeRead||options.signal||options.deadlineAt||options.allTargets) + const get=client.get.bind(client),post=client.post.bind(client);let requests=0,bytes=0 + const signalFor=(signal?:AbortSignal)=>AbortSignal.any([readAbort.signal,...(options.signal?[options.signal]:[]),...(signal?[signal]:[])]) + const check=async()=>{readAbort.signal.throwIfAborted();options.signal?.throwIfAborted();if(limited&&++requests>512||options.deadlineAt!==undefined&&Date.now()>=options.deadlineAt)throw greMutationProblem("Истёк бюджет чтения GRE");await options.beforeRead?.();readAbort.signal.throwIfAborted();options.signal?.throwIfAborted()} + const bounded=(value:T)=>{if(!limited)return value;const size=Buffer.byteLength(JSON.stringify(value)??"null");bytes+=size;if(size>1048576||bytes>8388608)throw greMutationProblem("Превышен размер native-источников GRE");return value} + const optionalFailure=(error:unknown,optional:boolean)=>optional&&!batchActive&&!readAbort.signal.aborted&&!options.signal?.aborted&&error instanceof MikrotikError&&[400,404].includes(error.statusCode) + client.get=async(path:string,timeout=10000,signal?:AbortSignal)=>{ + await check().catch(error=>{throw stop(error)}) + let raw:T + try{raw=await get(path,Math.max(1,Math.min(timeout,options.deadlineAt===undefined?timeout:options.deadlineAt-Date.now())),signalFor(signal))} + catch(error){if(optionalFailure(error,/^\/user(?:\/group)?\?name=/.test(path)))throw error;throw stop(error)} + try{const value=bounded(raw);await check();return value}catch(error){throw stop(error)} + } + client.post=async(path:string,body:Record,timeout=15000,signal?:AbortSignal)=>{ + try{if(!path.endsWith("/get")&&!path.endsWith("/print"))throw greMutationProblem("В чтении GRE запрещена команда записи");await check()}catch(error){throw stop(error)} + let raw:T + try{raw=await post(path,body,Math.max(1,Math.min(timeout,options.deadlineAt===undefined?timeout:options.deadlineAt-Date.now())),signalFor(signal))} + catch(error){if(optionalFailure(error,path==="/interface/gre/get"&&body["value-name"]==="ipsec-secret"))throw error;throw stop(error)} + try{const value=bounded(raw);await check();return value}catch(error){throw stop(error)} + } + } + const [greRaw, ipv4Raw, ipv6Raw, dependencies, bindings, filters] = await settledGreReads([ + client.get("/interface/gre").then(value=>readGreNativeRows(rows(value,[".id","name"],100))),client.get("/ip/address").then(value=>rows(value,[".id","address","interface"])),client.get("/ipv6/address").then(value=>rows(value,[".id","address","interface"])), + settledGreReads(greDependencyPaths.map(async path => [path, rows(await client.get(path), path === "/interface" ? [".id", "name"] : [".id"])] as const),stop), + pool.query('SELECT id,interface_name AS "interfaceName" FROM user_interface_bindings WHERE server_id=$1 ORDER BY id LIMIT 1001', [server.id]), + pool.query('SELECT id,gateway_tunnel_id AS "gatewayTunnelId",gateway FROM filter_rules WHERE server_id=$1 ORDER BY id LIMIT 1001', [server.id]), + ]as const,stop) + batchActive=false + if (bindings.rows.length > 1000 || filters.rows.length > 1000) throw greMutationProblem("Превышен предел DB-зависимостей GRE") + const state: GreMutationState = { serverConfiguration: fingerprint(server), gre:greRaw,ipv4:ipv4Raw,ipv6:ipv6Raw,dependencies: Object.fromEntries(dependencies), bindings: bindings.rows, filters: filters.rows } + const selected=options.allTargets?state.gre:input?[greTarget(input,state)].filter((row):row is GreNativeRow=>!!row):[] + for(const target of selected){ + if (!options.identityOnly) { + await hydrate(client, "/interface/gre", target, ["local-address", "remote-address", "mtu", "keepalive", "dscp", "disabled", "running", "clamp-tcp-mss", "allow-fast-path"]) + if(options.allTargets){ + const parent=state.dependencies["/interface"].find(row=>row[".id"]===target[".id"]&&row.name===target.name) + if(!parent||parent.type!==undefined&&!["gre","gre-tunnel"].includes(parent.type))throw greMutationProblem("Не подтверждено соответствие GRE общему native-каталогу") + if(target.dynamic===undefined){if(parent.dynamic===undefined)await hydrateGreDynamic(client,parent);target.dynamic=parent.dynamic} + else if(parent.dynamic!==undefined&&observedBoolean(parent.dynamic)!==observedBoolean(target.dynamic))throw greMutationProblem("Противоречивое происхождение GRE в native-каталогах") + } + // A sensitive property may be omitted even for an otherwise readable row. + // Preserve it without an empty-string assumption when the operation does + // not write the key. Explicit PSK writes still require observed read-back. + if (options.allTargets||input&&input.operation !== "delete" && input.fields.ipsecSecret !== undefined) target["ipsec-secret"] = await readGrePskValue(target,body=>client.post("/interface/gre/print",body),{get:()=>client.post("/interface/gre/get",{".id":target[".id"],"value-name":"ipsec-secret"}),capability:()=>sensitiveAccessFingerprint(server,client)}) + if(options.allTargets&&observedBoolean(target.dynamic)!==false)throw greMutationProblem("Restore требует подтверждённые статические GRE") + const mtuKnown = target.mtu === "auto" || observedInteger(target.mtu, 68, 65535) !== null + if (["disabled", "running", "clamp-tcp-mss", "allow-fast-path"].some(key => observedBoolean(target[key]) === null) || !mtuKnown || target.dscp !== "inherit" && observedInteger(target.dscp,0,63) === null) throw greMutationProblem("Не подтверждены native-свойства GRE") + for (const [path, addresses] of [["/ip/address", state.ipv4], ["/ipv6/address", state.ipv6]] as const) for (const address of addresses) if ([target.name, target[".id"]].includes(address.interface)) { + address.interface = target.name + await hydrate(client, path, address, ["dynamic", "disabled", "invalid"]) + if (["dynamic", "disabled", "invalid"].some(key => observedBoolean(address[key]) === null)) throw greMutationProblem("Не подтверждено происхождение/состояние адреса GRE") + if (path === "/ipv6/address" && observedBoolean(address.dynamic) === false) { + await hydrate(client, path, address, ["advertise", "no-dad", "eui-64"]) + if (["advertise", "no-dad", "eui-64"].some(key => observedBoolean(address[key]) === null)) throw greMutationProblem("Не подтверждены IPv6-свойства GRE") + } + } + } + if (options.identityOnly) for (const address of [...state.ipv4,...state.ipv6]) if ([target.name,target[".id"]].includes(address.interface)) address.interface=target.name + } + await options.beforeRead?.();options.signal?.throwIfAborted() + return state +} +export function greNativeValue(key: string, value: string | undefined) { + if (bools.has(key)) return value === undefined ? undefined : observedBoolean(value) ?? value + if (key === "local-address" && [undefined, "", "0.0.0.0"].includes(value)) return "0.0.0.0" + if (["comment", "ipsec-secret", "from-pool"].includes(key)) return value ?? "" + if (key === "address" && value) return greAddressKey(value) + if (["mtu", "dscp"].includes(key) && value !== "inherit") return value === undefined ? undefined : observedInteger(value) ?? value + if (key === "keepalive" && ["0", "none"].includes(value ?? "")) return "none" + if (key === "keepalive" && value) { + // Invalid values stay opaque, rather than collapsing into disabled timers. + const [interval, retries] = value.split(",") + const duration = interval && /^\d+$/.test(interval) ? Number(interval)*1000 : observedDurationMs(interval) + const attempts = observedInteger(retries) + if (duration !== null && Number.isFinite(duration) && attempts !== null) return duration === 0 ? "none" : [duration,attempts] + } + return value +} +export function greStableRows(values: GreNativeRow[],preserveOrder=false,defaults?:"gre"|"address") { + const rows=values.map(row => Object.fromEntries(Object.entries(defaults?{comment:"",...(defaults==="gre"?{"local-address":"0.0.0.0"}:{}),...row}:row).filter(([key]) => !runtime.has(key)).map(([key, value]) => [key, greNativeValue(key, value)]))) + return preserveOrder?rows:rows.sort((a,b) => String(a[".id"]).localeCompare(String(b[".id"]))) +} +export function greMutationProof(state: GreMutationState) { + const names = new Set(state.gre.map(row => row.name)) + const addresses = (values: GreNativeRow[]) => greStableRows(values.filter(row => !(observedBoolean(row.dynamic) === true && /^fe80:/i.test(row.address))),false,"address") + return { serverConfiguration: state.serverConfiguration,...(state.restoreProtectionProof?{restoreProtectionProof:state.restoreProtectionProof}:{}), gre: greStableRows(state.gre,false,"gre"), ipv4: addresses(state.ipv4), ipv6: addresses(state.ipv6), + dependencies: Object.fromEntries(Object.entries(state.dependencies).map(([path, values]) => [path, greStableRows(values.filter(row => observedBoolean(row.dynamic) !== true && !(path === "/interface" && names.has(row.name))).map(row => path === "/interface" ? { ".id": row[".id"], name: row.name, ...(row.type === undefined ? {} : {type:row.type}) } : row),["/ip/vrf","/routing/rule","/routing/filter/rule","/routing/bfd/configuration"].includes(path)||/^\/(?:ip|ipv6)\/firewall\//.test(path))])), bindings: state.bindings, filters: state.filters } +} +export function assertGreDeleteDependencies(state: Pick, target: GreNativeRow, label = "GRE") { + const escaped = [target.name,target[".id"]].map(value=>value.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|") + const boundary = "[\\s,%\"';=(){}@\\[\\]]", token = new RegExp(`(?:^|${boundary})(?:${escaped})(?=$|${boundary})`) + // Literal names may be escaped inside a RouterOS filter/script string. + // Decode one layer only: a quoted literal backslash must not become an escape twice. + const refs = (value: string) => token.test(value) || token.test(value.replace(/\\([\\$"'])|((?:\\[0-9a-f]{2})+)/gi, (_match, literal: string | undefined, hex: string | undefined) => { + if (literal !== undefined) return literal + const bytes = Buffer.from(hex!.split("\\").filter(Boolean).map(value => parseInt(value, 16))), utf8 = bytes.toString("utf8") + return utf8.includes("\ufffd") ? bytes.toString("latin1") : utf8 + })) + for (const [path, values] of Object.entries(state.dependencies)) { + if (path === "/interface") continue + if (values.some(row => !(path === "/ip/vrf" && row.name === "main" && row.interfaces === "all") && !(observedBoolean(row.dynamic) === true && (path === "/interface/list/member" || ["/ip/route", "/ipv6/route"].includes(path) && (row.connect === "true" || row["belongs-to"]?.startsWith("connected")))) && Object.entries(row).some(([key,value]) => key !== ".id" && refs(value)))) throw greMutationProblem(label + " используется: сначала измените явные зависимости") + } + if (state.bindings.some(row => refs(row.interfaceName)) || state.filters.some(row => refs(row.gatewayTunnelId) || refs(row.gateway))) throw greMutationProblem(label + " используется привязкой пользователя или BGP-фильтром приложения") +} diff --git a/backend/src/services/gre-mutation.test.ts b/backend/src/services/gre-mutation.test.ts new file mode 100644 index 00000000..e60f7478 --- /dev/null +++ b/backend/src/services/gre-mutation.test.ts @@ -0,0 +1,98 @@ +import assert from "node:assert/strict" +import { greMutationSchema } from "./gre-mutation-input.js" +import { planGreMutation, verifyGreMutationOp, executeGreMutationOps } from "./gre-mutation.js" +import { assertGreDeleteDependencies, greDependencyPaths, greMutationProof, readGrePskValue, type GreMutationState } from "./gre-mutation-state.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import type { RosWriteOp } from "./entity-snapshots.js" + +const server = {id:1,enabled:true} as ServerRow +const empty = ():GreMutationState => ({serverConfiguration:"source",gre:[],ipv4:[],ipv6:[],dependencies:Object.fromEntries(greDependencyPaths.map(path=>[path,[]])),bindings:[],filters:[]}) +const native = {".id":"*G",name:"own-gre","local-address":"0.0.0.0","remote-address":"192.0.2.2",mtu:"1476",keepalive:"1m,3",dscp:"inherit",disabled:"true",running:"false","clamp-tcp-mss":"true","allow-fast-path":"true","ipsec-secret":"",comment:"opaque network=10.0.0.2/32","dont-fragment":"no"} +const hiddenSecretRow={".id":"*G",name:"own-gre"} +assert.equal(await readGrePskValue(hiddenSecretRow,async body=>{assert.deepEqual(body,{"show-sensitive":"",".proplist":".id,ipsec-secret",".query":[".id=*G"]});return[{".id":"*G","ipsec-secret":""}]}),"") +for(const raw of [[],{},[{".id":"*OTHER","ipsec-secret":"key"}],[{".id":"*G"}],[{".id":"*G","ipsec-secret":"***"}]])await assert.rejects(readGrePskValue(hiddenSecretRow,async()=>raw),/чувствительное/) +let capabilityReads=0 +assert.equal(await readGrePskValue(hiddenSecretRow,async()=>[],{get:async()=>[],capability:async()=>{capabilityReads++;return"sensitive-role"}}),"");assert.equal(capabilityReads,2) +await assert.rejects(readGrePskValue(hiddenSecretRow,async()=>[],{get:async()=>[],capability:async()=>undefined}),/чувствительное/) +let changedRole=0 +await assert.rejects(readGrePskValue(hiddenSecretRow,async()=>[],{get:async()=>[],capability:async()=>"role-"+(changedRole++)}),/чувствительное/) +function apply(state:GreMutationState,op:RosWriteOp) { + const next = structuredClone(state), path = ["/interface/gre","/ip/address","/ipv6/address"].find(path=>op.path===path||op.path.startsWith(path+"/"))! + const key = path==="/interface/gre"?"gre":path==="/ip/address"?"ipv4":"ipv6", id = op.path.slice(path.length+1) + const body = "body" in op ? Object.fromEntries(Object.entries(op.body).map(([key,value])=>[key,value==="yes"?"true":value==="no"?"false":value])) : {} + if (op.op === "put") { + const row = key==="gre"?{...native,...body,".id":"*C",comment:body.comment??""}:{".id":key==="ipv4"?"*A":"*B",dynamic:"false",invalid:"false","no-dad":"false","eui-64":"false","from-pool":"",...body} + next[key].push(row) + if (key==="gre") next.dependencies["/interface"].push({".id":"*C",name:row.name,type:"gre"}) + } else if (op.op === "patch") Object.assign(next[key].find(row=>row[".id"]===id)!,body) + else if (op.op === "delete") { + next[key]=next[key].filter(row=>row[".id"]!==id) + if (key==="gre") next.dependencies["/interface"]=next.dependencies["/interface"].filter(row=>row[".id"]!==id) + } + return next +} +for (const fields of [{name:"x",remoteAddress:"bad"},{name:"x",remoteAddress:"192.0.2.2",mtu:1},{name:"x",remoteAddress:"192.0.2.2",dscp:64},{name:"x",remoteAddress:"192.0.2.2",ipsecSecret:"secret",allowFastPath:true}]) assert.equal(greMutationSchema.safeParse({operation:"create",fields}).success,false) +assert.equal(greMutationSchema.safeParse({operation:"update",fields:{}}).success,false) +const create = greMutationSchema.parse({operation:"create",fields:{name:"own-gre",remoteAddress:"192.0.2.2",localInnerIp:"198.18.1.1/30",enabled:true}}) +let state = empty(), commands = 0 +const ops = planGreMutation(server,create,state) +assert.equal(ops.length,3); assert.equal(ops[0].op,"put"); assert.equal("body" in ops[0]&&ops[0].body.disabled,"yes"); assert.equal(ops[2].path,"/interface/gre/@created") +assert.equal("body" in ops[0] && ops[0].body.mtu, "1476") +const autoCreate = greMutationSchema.parse({operation:"create",fields:{name:"own-gre",remoteAddress:"192.0.2.2",mtu:"auto"}}) +const autoPut = planGreMutation(server,autoCreate,state)[0] +assert.equal("body" in autoPut && autoPut.body.mtu, "auto") +const receipt = await executeGreMutationOps(create,state,ops,{read:async()=>structuredClone(state),command:async op=>{commands++;state=apply(state,op)}}) +assert.equal(receipt.verified,true); assert.equal(receipt.completedCommands,3); assert.equal(state.gre[0].disabled,"false"); assert.equal(state.ipv4[0].interface,"own-gre") +const update = greMutationSchema.parse({operation:"update",rosId:"*C",fields:{mtu:1400,localInnerIp:"fd00::1/64"}}) +const oldComment = state.gre[0].comment +const updateOps = planGreMutation(server,update,state) +const updated = await executeGreMutationOps(update,state,updateOps,{read:async()=>structuredClone(state),command:async op=>{commands++;state=apply(state,op)}}) +assert.equal(updated.verified,true); assert.equal(state.ipv4.length,0); assert.equal(state.ipv6.length,1); assert.equal(state.gre[0].mtu,"1400"); assert.equal(state.gre[0].comment,oldComment); assert.equal(state.gre[0].disabled,"false") +const preserve = empty(); preserve.gre=[{...native}] +const autoState = structuredClone(preserve); autoState.gre[0].mtu = "auto" +const autoComment = greMutationSchema.parse({operation:"update",rosId:"*G",fields:{comment:"new comment"}}) +const autoPatch = planGreMutation(server, autoComment, autoState)[0] +assert.equal("body" in autoPatch && autoPatch.body.mtu, "auto") +const autoAfter = apply(autoState, autoPatch) +assert.ok(verifyGreMutationOp(autoState, autoAfter, autoPatch)) +autoAfter.gre[0].mtu = "1476" +assert.equal(verifyGreMutationOp(autoState, autoAfter, autoPatch), false) +const commentPlan = planGreMutation(server,greMutationSchema.parse({operation:"update",rosId:"*G",fields:{mtu:1400}}),preserve) +assert.equal(commentPlan[0].op,"patch"); assert.equal("body" in commentPlan[0]&&commentPlan[0].body.keepalive,"1m,3"); assert.equal("body" in commentPlan[0]&&commentPlan[0].body.comment,native.comment); assert.ok(!("body" in commentPlan[0]&&"ipsec-secret" in commentPlan[0].body)) +const clear = planGreMutation(server,greMutationSchema.parse({operation:"update",rosId:"*G",fields:{comment:"",remoteInnerIp:"",localAddress:"",ipsecSecret:""}}),preserve) +assert.equal(clear[0].op,"patch"); assert.equal("body" in clear[0]&&clear[0].body.comment,""); assert.equal("body" in clear[0]&&clear[0].body["ipsec-secret"],"") +const collision = empty(); collision.dependencies["/interface"]=[{".id":"*E",name:"own-gre",type:"ether"}] +assert.throws(()=>planGreMutation(server,create,collision),/используется/) +assert.throws(()=>planGreMutation(server,greMutationSchema.parse({operation:"delete",rosId:"*G",name:"different"}),preserve),/разные/) +for(const dependency of ["/interface/vlan","/routing/ospf/interface-template","/routing/bfd/configuration","/routing/bgp/connection","/ip/vrf","/routing/filter/rule","/ip/route"]) { + const referenced = structuredClone(preserve); referenced.dependencies[dependency]=[{".id":"*D",interface:"own-gre"}] + assert.throws(()=>assertGreDeleteDependencies(referenced,native),/используется/) +} +const bound = structuredClone(preserve); bound.bindings=[{id:"user-binding",interfaceName:"own-gre"}]; assert.throws(()=>assertGreDeleteDependencies(bound,native),/привязкой/) +const opaque = structuredClone(preserve); opaque.gre[0]["dont-fragment"]="yes"; assert.notDeepEqual(greMutationProof(opaque),greMutationProof(preserve)) +const hiddenPsk=structuredClone(preserve);delete hiddenPsk.gre[0]["ipsec-secret"] +assert.ok(planGreMutation(server,greMutationSchema.parse({operation:"update",rosId:"*G",fields:{mtu:1400}}),hiddenPsk).length>0) +assert.throws(()=>planGreMutation(server,greMutationSchema.parse({operation:"update",rosId:"*G",fields:{allowFastPath:true}}),hiddenPsk),/PSK/) +const unknownTimer=structuredClone(preserve);unknownTimer.gre[0].keepalive="unverified" +assert.throws(()=>planGreMutation(server,greMutationSchema.parse({operation:"update",rosId:"*G",fields:{keepaliveRetries:5}}),unknownTimer),/таймер/) +const dynamicAddress=structuredClone(preserve);dynamicAddress.ipv4=[{".id":"*DHCP",address:"192.0.2.1/24",interface:"own-gre",dynamic:"true",disabled:"false"}] +assert.throws(()=>planGreMutation(server,greMutationSchema.parse({operation:"delete",rosId:"*G"}),dynamicAddress),/динамический/) +const stagedAddress=empty();stagedAddress.gre=[{...native}] +const putAddress:RosWriteOp={op:"put",path:"/ip/address",body:{interface:"own-gre",address:"198.18.1.1/30",disabled:"no"}} +const invalidOnDisabled=apply(stagedAddress,putAddress);invalidOnDisabled.ipv4[0].invalid="true" +assert.equal(verifyGreMutationOp(stagedAddress,invalidOnDisabled,putAddress),true) +invalidOnDisabled.ipv4[0].dynamic="true";assert.equal(verifyGreMutationOp(stagedAddress,invalidOnDisabled,putAddress),false) +const changed = apply(preserve,{op:"patch",path:"/interface/gre/*G",body:{mtu:"1400"}}) +assert.equal(verifyGreMutationOp(preserve,changed,{op:"patch",path:"/interface/gre/*G",body:{mtu:"1400"}}),true) +changed.gre[0]["dont-fragment"]="yes"; assert.equal(verifyGreMutationOp(preserve,changed,{op:"patch",path:"/interface/gre/*G",body:{mtu:"1400"}}),false) +const dynamic = structuredClone(preserve); dynamic.ipv6=[{".id":"*LL",address:"fe80::1/64",interface:"own-gre",dynamic:"true"}]; assert.deepEqual(greMutationProof(dynamic),greMutationProof(preserve)) +state=empty();commands=0 +const ignored = await executeGreMutationOps(create,state,ops,{read:async()=>structuredClone(state),command:async op=>{commands++;if(commands===1)state=apply(state,op)}}) +assert.equal(ignored.verified,false); assert.equal(ignored.completedCommands,1); assert.equal(commands,2); assert.equal(state.gre[0].disabled,"true") +state=empty();commands=0 +await assert.rejects(executeGreMutationOps(create,state,ops,{read:async()=>structuredClone(state),command:async op=>{commands++;state=apply(state,op);throw new Error("lost reply")}}),/lost reply/) +assert.equal(commands,1); assert.equal(state.gre[0].disabled,"true") +const deletion = greMutationSchema.parse({operation:"delete",name:"own-gre"}) +const deleteOps = planGreMutation(server,deletion,state) +assert.equal(deleteOps.at(-1)?.path,"/interface/gre/*C") +console.log("GRE mutations: disabled staging, v4→v6 primary replacement, opaque/timer/secret preservation, native identity, parent/application dependencies, link-local exclusion, per-command proof and ignored/lost stop") diff --git a/backend/src/services/gre-mutation.ts b/backend/src/services/gre-mutation.ts new file mode 100644 index 00000000..b457bee2 --- /dev/null +++ b/backend/src/services/gre-mutation.ts @@ -0,0 +1,137 @@ +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { fingerprint } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { mapGreLive, parseKeepalive } from "./gre-live.js" +import { readGreComment } from "./gre-comment.js" +import { greAddressPath, planGreCreate, planGreDelete, planGreRestore, type RosWriteOp } from "./entity-snapshots.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { greMutationSchema, greTunnelFromBody, type GreMutation } from "./gre-mutation-input.js" +import { assertGreDeleteDependencies, greMutationProblem, greMutationProof, greNativeValue, greStableRows, greTarget, readGreMutationState, type GreMutationState } from "./gre-mutation-state.js" + +const same = (a: unknown,b: unknown) => fingerprint(a) === fingerprint(b) +export async function recordGreMutationRevision(server:ServerRow){ + try{ + const revisionRecorded=(await captureAndAppendRevision({serverId:server.id,section:"gre",source:"apply",capture:async()=>{ + const {captureGreSnapshot}=await import("./gre-live.js") + return captureGreSnapshot(server) + }}))!==null + return{revisionRecorded,revisionError:null as string|null} + }catch{return{revisionRecorded:false,revisionError:"Изменение GRE проверено, запись полной ревизии не подтверждена"}} +} +export function planGreMutation(server: ServerRow, input: GreMutation, before: GreMutationState): RosWriteOp[] { + const target = greTarget(input,before) + if (input.operation === "create") { + if (target || before.dependencies["/interface"].some(row=>row.name === input.fields.name)) throw greMutationProblem("Имя GRE уже используется интерфейсом") + const desired = greTunnelFromBody(input.fields), ops = planGreCreate(desired) + if (ops[0].op !== "put") throw greMutationProblem("Не подготовлено создание GRE") + ops[0].body.disabled = "yes" + if (!desired.disabled) ops.push({op:"patch",path:"/interface/gre/@created",body:{disabled:"no"}}) + return ops + } + if (!target || observedBoolean(target.dynamic) === true) throw greMutationProblem("Требуется существующий статический GRE с native ID") + const selected = mapGreLive(server,[target],[...before.ipv4,...before.ipv6].filter(row=>[target.name,target[".id"]].includes(row.interface))) + const live = selected.gre[0] + if (input.operation === "delete") { + assertGreDeleteDependencies(before,target) + if ([...before.ipv4,...before.ipv6].some(row=>row.interface===target.name&&observedBoolean(row.dynamic)===true&&!/^fe80:/i.test(row.address))) throw greMutationProblem("GRE имеет динамический адрес; сначала отключите создающий его сервис") + const ops = planGreDelete(live.name,selected) + if (!live.disabled) ops.unshift({op:"patch",path:"/interface/gre/"+live.rosId,body:{disabled:"yes"}}) + return ops + } + const fields = input.fields, original = selected.snapshot.tunnels[0], timer = parseKeepalive(live.keepalive) + if ((fields.keepaliveInterval !== undefined || fields.keepaliveRetries !== undefined) && (fields.keepaliveInterval === undefined || fields.keepaliveRetries === undefined)) { + const observedTimer = greNativeValue("keepalive",target.keepalive) + if (observedTimer!=="none"&&!Array.isArray(observedTimer)) throw greMutationProblem("Для изменения части keepalive требуется подтверждённый исходный таймер") + } + if (fields.allowFastPath === true && fields.ipsecSecret === undefined && (target["ipsec-secret"] === undefined || /^\*+$|^\(hidden\)$/.test(target["ipsec-secret"]))) throw greMutationProblem("Состояние PSK не подтверждено; включение FastPath требует явной проверенной настройки ключа") + const desired = greTunnelFromBody({ name: live.name, localAddress: fields.localAddress ?? live.localAddress, remoteAddress: fields.remoteAddress ?? live.remoteAddress, + localInnerIp: fields.localInnerIp ?? original.localInnerIp, remoteInnerIp: fields.remoteInnerIp ?? readGreComment(live.comment).remoteInnerIp, comment: fields.comment ?? readGreComment(live.comment).comment, + enabled: fields.enabled ?? !live.disabled, mtu: fields.mtu ?? live.mtu, keepaliveInterval: fields.keepaliveInterval ?? timer.interval, keepaliveRetries: fields.keepaliveRetries ?? timer.retries, + dscp: fields.dscp ?? live.dscp, clampTcpMss: fields.clampTcpMss ?? live.clampTcpMss, allowFastPath: fields.allowFastPath ?? (fields.ipsecSecret?.trim() ? false : live.allowFastPath), ipsecSecret: fields.ipsecSecret ?? live.ipsecSecret, + }) + if (fields.keepaliveInterval === undefined && fields.keepaliveRetries === undefined) desired.keepalive = original.keepalive + desired.innerAddresses = original.innerAddresses?.map(row=>({...row})) + if (fields.localInnerIp !== undefined && fields.localInnerIp !== original.localInnerIp) { + desired.innerAddresses = (desired.innerAddresses ?? []).filter(row=>row.address !== original.localInnerIp) + if (fields.localInnerIp && !desired.innerAddresses.some(row=>row.address === fields.localInnerIp)) desired.innerAddresses.push({address:fields.localInnerIp,disabled:false,...(greAddressPath(fields.localInnerIp)==="/ipv6/address"?{advertise:false}:{})}) + } + const ops = planGreRestore({tunnels:[desired]},selected) + if (ops[0]?.op !== "patch") throw greMutationProblem("Не подготовлено изменение GRE") + const body = ops[0].body + if (fields.ipsecSecret === undefined) delete body["ipsec-secret"] + else body["ipsec-secret"] = fields.ipsecSecret + if (fields.localAddress !== undefined) body["local-address"] = fields.localAddress || "0.0.0.0" + body.comment = fields.comment !== undefined || fields.remoteInnerIp !== undefined ? desired.comment : target.comment ?? "" + if (ops.length === 1 && Object.entries(body).every(([key,value])=>same(greNativeValue(key,target[key]),greNativeValue(key,value)))) throw greMutationProblem("GRE уже имеет запрошенную конфигурацию") + body.disabled = "yes" + if (!desired.disabled) ops.push({op:"patch",path:"/interface/gre/"+live.rosId,body:{disabled:"no"}}) + return ops +} + +export function verifyGreMutationOp(before: GreMutationState, after: GreMutationState, op: RosWriteOp) { + const paths = {"/interface/gre":"gre","/ip/address":"ipv4","/ipv6/address":"ipv6"} as const + const path = Object.keys(paths).find(path=>op.path===path||op.path.startsWith(path+"/")) as keyof typeof paths|undefined + if (!path || !["put","patch","delete"].includes(op.op)) return false + const key = paths[path], prior = greMutationProof(before), next = greMutationProof(after) + const id = op.path === path ? undefined : decodeURIComponent(op.path.slice(path.length+1)) + if (op.op === "put") { + const added = next[key].filter(row=>!prior[key].some(previous=>previous[".id"]===row[".id"])) + if (added.length !== 1 || !same(next[key].filter(row=>row!==added[0]),prior[key]) || !Object.entries(op.body).every(([field,value])=>same(greNativeValue(field,after[key].find(row=>row[".id"]===added[0][".id"])?.[field]),greNativeValue(field,value)))) return false + if (key!=="gre"&&observedBoolean(after[key].find(row=>row[".id"]===added[0][".id"])?.dynamic)!==false) return false + // An address on our deliberately disabled GRE can legitimately be invalid. + // This proof confirms configuration, not link/DAD/forwarding readiness. + } else { + if (!prior[key].some(row=>row[".id"]===id)) return false + const expected = before[key].filter(row=>!(op.op==="delete"&&row[".id"]===id)).map(row=>row[".id"]===id&&op.op==="patch"?{...row,...op.body}:row) + // Automatic IPv6 link-local rows are not independently configurable. + const stable = greStableRows(expected.filter(row=>!(observedBoolean(row.dynamic)===true&&/^fe80:/i.test(row.address))),false,key==="gre"?"gre":"address") + if (!same(next[key],stable)) return false + } + return same({...prior,[key]:next[key]},next) +} + +export async function executeGreMutationOps(input: GreMutation, before: GreMutationState, ops: RosWriteOp[], deps: { read:()=>Promise; command:(op:RosWriteOp)=>Promise; validate?:(state:GreMutationState)=>void }) { + let current = before, completedCommands = 0, acknowledgedCommands = 0 + for (const planned of ops) { + const fresh = await deps.read() + if (!same(greMutationProof(fresh),greMutationProof(current))) throw greMutationProblem("GRE или его зависимости изменились между командами") + deps.validate?.(fresh) + let op = planned + if (planned.path.endsWith("/@created")) { + const target = greTarget(input,current) + if (!target) throw greMutationProblem("Не подтверждён native ID нового GRE") + op = {...planned,path:"/interface/gre/"+target[".id"]} + } + await deps.command(op); acknowledgedCommands++ + const next = await deps.read() + if (!verifyGreMutationOp(current,next,op)) return {verified:false,completedCommands,acknowledgedCommands,after:next} + current = next; completedCommands++ + } + return {verified:true,completedCommands,acknowledgedCommands,after:current} +} +export async function mutateGreTunnel(server: ServerRow, raw: GreMutation, expectedBefore?: string) { + const input = greMutationSchema.parse(raw) + return withApplicationMutationLock(()=>withConfigMutationLock([server.id],async()=>{ + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || !same(server,fresh)) throw greMutationProblem("Источник GRE изменился") + const before = await readGreMutationState(fresh,input) + if (expectedBefore && fingerprint(greMutationProof(before)) !== expectedBefore) throw greMutationProblem("GRE изменился после подготовки") + const ops = planGreMutation(fresh,input,before) + if (!ops.length || ops.length > 50) throw greMutationProblem("Операция GRE должна содержать от 1 до 50 команд") + const client = MikrotikClient.fromServer(fresh) + const receipt = await executeGreMutationOps(input,before,ops,{read:()=>readGreMutationState(fresh,input),...(input.operation==="delete"?{validate:(state:GreMutationState)=>{planGreMutation(fresh,input,state)}}:{}),command:op=>{ + if (op.op === "put") return client.put(op.path,op.body) + const slash = op.path.lastIndexOf("/"), path = op.path.slice(0,slash+1)+encodeURIComponent(op.path.slice(slash+1)) + if (op.op === "delete") return client.delete(path) + if (op.op === "patch") return client.patch(path,op.body) + throw greMutationProblem("Недопустимая команда GRE") + }}) + const target = greTarget(input,receipt.after) + const final = target ? mapGreLive(fresh,[target],[...receipt.after.ipv4,...receipt.after.ipv6].filter(row=>row.interface===target.name)) : undefined + const revision=receipt.verified?await recordGreMutationRevision(fresh):{revisionRecorded:false,revisionError:null} + return {verified:receipt.verified,acknowledged:receipt.acknowledgedCommands>0,commandCount:ops.length,completedCommands:receipt.completedCommands,acknowledgedCommands:receipt.acknowledgedCommands,afterFingerprint:fingerprint(greMutationProof(receipt.after)),...revision,addressReadinessVerified:false,forwardingVerified:false,tunnel:final?.tunnels[0]} + })) +} diff --git a/backend/src/services/gre-native-mutation.test.ts b/backend/src/services/gre-native-mutation.test.ts new file mode 100644 index 00000000..15d05632 --- /dev/null +++ b/backend/src/services/gre-native-mutation.test.ts @@ -0,0 +1,75 @@ +import assert from "node:assert/strict" +import { greNativeMutationSchema, planGreNativeMutation, greNativeSelector } from "./gre-native-mutation.js" +import { greDependencyPaths, greMutationProof, type GreMutationState } from "./gre-mutation-state.js" +import { executeGreMutationOps, verifyGreMutationOp } from "./gre-mutation.js" +import { greNativeFieldsSchema } from "./gre-mutation-input.js" + +const state=():GreMutationState=>({serverConfiguration:"source",gre:[{".id":"*G",name:"own-gre",disabled:"true","dont-fragment":"no"}],ipv4:[],ipv6:[],dependencies:Object.fromEntries(greDependencyPaths.map(path=>[path,[]])),bindings:[],filters:[]}) +const rename=greNativeMutationSchema.parse({operation:"update",id:"*G",fields:{name:"renamed-gre"}}),remove=greNativeMutationSchema.parse({operation:"delete",id:"*G"}) +const before=state(),op=planGreNativeMutation(rename,before) +assert.deepEqual(op,{op:"patch",path:"/interface/gre/*G",body:{name:"renamed-gre"}}) +assert.deepEqual(planGreNativeMutation(remove,before),{op:"delete",path:"/interface/gre/*G"}) +for(const mutation of [{operation:"update",id:"*G",fields:{}},{operation:"update",id:"*G",fields:{"ipsec-secret":"raw"}},{operation:"delete",id:"*G,*OTHER"}])assert.equal(greNativeMutationSchema.safeParse(mutation).success,false) +for (const fields of [ + { "local-address": "2001:db8::1" }, { "local-address": "127.0.0.999" }, + { "remote-address": "2001:db8::1" }, { "remote-address": "192.0.2.1/24" }, { "remote-address": "192.0.2.1@" }, + { keepalive: "0s,10" }, { keepalive: "500ms,2" }, { keepalive: "1s,0" }, { keepalive: "1s,2,3" }, +]) { + assert.equal(greNativeFieldsSchema.safeParse(fields).success, false) + assert.equal(greNativeMutationSchema.safeParse({ operation: "update", id: "*G", fields }).success, false) +} +for (const fields of [ + { "local-address": "", "remote-address": "192.0.2.1", keepalive: "1s,1" }, + { "local-address": "0.0.0.0", "remote-address": "192.0.2.1@vrf", keepalive: "1m30s,10" }, + { "remote-address": "gre.example.net", keepalive: "00:01:00,3" }, +]) { + assert(greNativeFieldsSchema.safeParse(fields).success) + assert(greNativeMutationSchema.safeParse({ operation: "update", id: "*G", fields }).success) +} +for(const input of [rename,remove]){ + const bound=state();bound.bindings=[{id:"binding",interfaceName:"own-gre"}];assert.throws(()=>planGreNativeMutation(input,bound),/привязкой/) + const filter=state();filter.filters=[{id:1,gatewayTunnelId:"own-gre",gateway:""}];assert.throws(()=>planGreNativeMutation(input,filter),/фильтром/) + const address=state();address.ipv4=[{".id":"*A",address:"192.0.2.1/24",interface:"own-gre",dynamic:"false"}];assert.throws(()=>planGreNativeMutation(input,address),/адресом/) + const lla=state();lla.ipv6=[{".id":"*LL",address:"fe80::1/64",interface:"own-gre",dynamic:"true"}];assert.doesNotThrow(()=>planGreNativeMutation(input,lla)) + for(const path of ["/interface/bridge/port","/interface/vlan","/ip/vrf","/routing/ospf/interface-template","/routing/bfd/configuration","/routing/bgp/connection","/ip/route","/ip/firewall/nat","/ipv6/firewall/filter"]){ + const referenced=state();referenced.dependencies[path]=[{".id":"*D","in-interface":"own-gre"}];assert.throws(()=>planGreNativeMutation(input,referenced),/используется/) + } + const nativeIdList=state();nativeIdList.dependencies["/interface/list/member"]=[{".id":"*M",interface:"*G,*OTHER"}];assert.throws(()=>planGreNativeMutation(input,nativeIdList),/используется/) +} +const scalar=greNativeMutationSchema.parse({operation:"update",id:"*G",fields:{mtu:1400}}),bound=state();bound.bindings=[{id:"binding",interfaceName:"own-gre"}] +for (const name of ["own-gre", "туннель-gre", 'quoted"name', "literal\\name"]) { + const referenced = state(); referenced.gre[0].name = name + const encoded = [...Buffer.from(name)].map(byte => "\\" + byte.toString(16).padStart(2, "0")).join("") + referenced.dependencies["/routing/filter/rule"] = [{ ".id": "*D", rule: `set gw "${encoded}";` }] + assert.throws(() => planGreNativeMutation(remove, referenced), /используется/) +} +assert.deepEqual(planGreNativeMutation(scalar,bound),{op:"patch",path:"/interface/gre/*G",body:{mtu:"1400"}}) +const collision=state();collision.dependencies["/interface"]=[{".id":"*E",name:"renamed-gre"}];assert.throws(()=>planGreNativeMutation(rename,collision),/уже используется/) +const counters=state();counters.dependencies["/ip/firewall/filter"]=[{".id":"*F",chain:"forward",action:"accept",packets:"1",bytes:"100"}] +const tick=structuredClone(counters);tick.dependencies["/ip/firewall/filter"][0].packets="9000";assert.deepEqual(greMutationProof(counters),greMutationProof(tick)) +tick.dependencies["/ip/firewall/filter"][0].action="drop";assert.notDeepEqual(greMutationProof(counters),greMutationProof(tick)) +const after=structuredClone(before);after.gre[0].name="renamed-gre";assert.equal(verifyGreMutationOp(before,after,op),true) +const hasComment=state();hasComment.gre[0].comment="clear me" +const omittedComment=structuredClone(hasComment);delete omittedComment.gre[0].comment +assert.equal(verifyGreMutationOp(hasComment,omittedComment,{op:"patch",path:"/interface/gre/*G",body:{comment:""}}),true) +const hasLocal=state();hasLocal.gre[0]["local-address"]="192.0.2.1" +const omittedLocal=structuredClone(hasLocal);delete omittedLocal.gre[0]["local-address"] +assert.equal(verifyGreMutationOp(hasLocal,omittedLocal,{op:"patch",path:"/interface/gre/*G",body:{"local-address":""}}),true) +const omittedSecret=state(),explicitEmptySecret=state();explicitEmptySecret.gre[0]["ipsec-secret"]="";assert.notDeepEqual(greMutationProof(omittedSecret),greMutationProof(explicitEmptySecret)) +after.gre[0]["dont-fragment"]="yes";assert.equal(verifyGreMutationOp(before,after,op),false) +const current=state();let commands=0 +const ignored=await executeGreMutationOps(greNativeSelector(rename),current,[op],{read:async()=>structuredClone(current),command:async()=>{commands++}}) +assert.equal(ignored.verified,false);assert.equal(commands,1) +commands=0 +await assert.rejects(executeGreMutationOps(greNativeSelector(rename),current,[op],{read:async()=>structuredClone(current),command:async()=>{commands++;current.gre[0].name="renamed-gre";throw new Error("lost reply")}}),/lost reply/) +assert.equal(commands,1) +const lateDynamic=state(),initialDynamic=structuredClone(lateDynamic);commands=0 +lateDynamic.dependencies["/ip/route"]=[{".id":"*LEARNED",dynamic:"true",gateway:"192.0.2.2%own-gre"}] +assert.deepEqual(greMutationProof(lateDynamic),greMutationProof(initialDynamic)) +await assert.rejects(executeGreMutationOps(greNativeSelector(remove),initialDynamic,[planGreNativeMutation(remove,initialDynamic)],{read:async()=>structuredClone(lateDynamic),validate:fresh=>{planGreNativeMutation(remove,fresh)},command:async()=>{commands++}}),/используется/) +assert.equal(commands,0) +const ordered=state();ordered.dependencies["/ip/firewall/nat"]=[{".id":"*F1",action:"accept"},{".id":"*F2",action:"drop"}] +const reordered=structuredClone(ordered);reordered.dependencies["/ip/firewall/nat"].reverse();assert.notDeepEqual(greMutationProof(ordered),greMutationProof(reordered)) +const opaqueMtu=state();opaqueMtu.gre[0].mtu="auto" +const changedOpaqueMtu=structuredClone(opaqueMtu);changedOpaqueMtu.gre[0].mtu="unverified";assert.notDeepEqual(greMutationProof(opaqueMtu),greMutationProof(changedOpaqueMtu)) +console.log("Native GRE aliases: exact one-interface mutation, user/filter/address/protocol/firewall references, identity lists, opaque proof, counter tolerance, explicit rename and no retry") diff --git a/backend/src/services/gre-native-mutation.ts b/backend/src/services/gre-native-mutation.ts new file mode 100644 index 00000000..aecb5396 --- /dev/null +++ b/backend/src/services/gre-native-mutation.ts @@ -0,0 +1,55 @@ +import { z } from "zod" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { fingerprint } from "../modules/mcp/security.js" +import { greNativeFieldsSchema } from "./gre-mutation-input.js" +import { assertGreDeleteDependencies, greMutationProblem, greMutationProof, greTarget, readGreMutationState, type GreMutationState } from "./gre-mutation-state.js" +import { executeGreMutationOps, recordGreMutationRevision } from "./gre-mutation.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { MikrotikClient } from "./mikrotik.js" +import type { RosWriteOp } from "./entity-snapshots.js" + +const id=z.string().regex(/^\*[A-Za-z0-9]+$/).max(100) +export const greNativeMutationSchema=z.discriminatedUnion("operation",[ + z.object({operation:z.literal("update"),id,fields:greNativeFieldsSchema.refine(fields=>Object.keys(fields).length>0,"Нет параметров изменения")}).strict(), + z.object({operation:z.literal("delete"),id}).strict(), +]) +export type GreNativeMutation=z.infer +export const greNativeSelector=(input:GreNativeMutation)=>({operation:"delete" as const,rosId:input.id}) +export async function readGreNativeMutationState(server:ServerRow,input:GreNativeMutation){return readGreMutationState(server,greNativeSelector(input),{identityOnly:true})} + +/** Native update/delete keeps its original one-interface meaning. In particular, + * deletion never acquires implicit permission to remove configured addresses. */ +export function planGreNativeMutation(input:GreNativeMutation,before:GreMutationState):RosWriteOp { + const target=greTarget(greNativeSelector(input),before) + if(!target)throw greMutationProblem("GRE с этим native ID не найден") + if([target.dynamic,target.builtin,target.default].some(value=>observedBoolean(value)===true))throw greMutationProblem("Системный/динамический GRE нельзя изменять") + const rename=input.operation==="update"&&input.fields.name!==undefined&&input.fields.name!==target.name + if(input.operation==="delete"||rename){ + assertGreDeleteDependencies(before,target) + if([...before.ipv4,...before.ipv6].some(row=>[target.name,target[".id"]].includes(row.interface)&&!(observedBoolean(row.dynamic)===true&&/^fe80:/i.test(row.address))))throw greMutationProblem("GRE используется адресом; сначала измените адреса отдельным планом") + } + if(rename&&input.operation==="update"&&(before.gre.some(row=>row[".id"]!==target[".id"]&&row.name===input.fields.name)||before.dependencies["/interface"].some(row=>row[".id"]!==target[".id"]&&row.name===input.fields.name)))throw greMutationProblem("Новое имя GRE уже используется") + if(input.operation==="delete")return{op:"delete",path:"/interface/gre/"+input.id} + return{op:"patch",path:"/interface/gre/"+input.id,body:Object.fromEntries(Object.entries(input.fields).map(([key,value])=>[key,String(value)]))} +} + +export async function mutateGreNativeTunnel(server:ServerRow,raw:GreNativeMutation,expectedBefore?:string,options:{recordRevision?:boolean}={}){ + const input=greNativeMutationSchema.parse(raw) + return withApplicationMutationLock(()=>withConfigMutationLock([server.id],async()=>{ + const fresh=await getServerRowById(server.id) + if(!fresh?.enabled||fingerprint(server)!==fingerprint(fresh))throw greMutationProblem("Источник GRE изменился") + const before=await readGreNativeMutationState(fresh,input) + if(expectedBefore&&fingerprint(greMutationProof(before))!==expectedBefore)throw greMutationProblem("GRE или его зависимости изменились после подготовки") + const op=planGreNativeMutation(input,before),client=MikrotikClient.fromServer(fresh) + const receipt=await executeGreMutationOps(greNativeSelector(input),before,[op],{read:()=>readGreNativeMutationState(fresh,input),validate:state=>{planGreNativeMutation(input,state)},command:op=>{ + const path="/interface/gre/"+encodeURIComponent(input.id) + if(op.op==="delete")return client.delete(path) + if(op.op==="patch")return client.patch(path,op.body) + throw greMutationProblem("Недопустимая native-команда GRE") + }}) + const revision=receipt.verified&&options.recordRevision!==false?await recordGreMutationRevision(fresh):{revisionRecorded:false,revisionError:null} + return{verified:receipt.verified,acknowledged:receipt.acknowledgedCommands>0,commandCount:1,completedCommands:receipt.completedCommands,acknowledgedCommands:receipt.acknowledgedCommands,afterFingerprint:fingerprint(greMutationProof(receipt.after)),...revision,forwardingVerified:false} + })) +} diff --git a/backend/src/services/gre-observed-read.test.ts b/backend/src/services/gre-observed-read.test.ts new file mode 100644 index 00000000..420a6f35 --- /dev/null +++ b/backend/src/services/gre-observed-read.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict" +import { observeGreCatalog } from "./gre-observed-read.js" +import { writeGreComment } from "./gre-comment.js" +const row = { ".id": "*G", name: "gre1", disabled: "false", running: "true", mtu: "1400", keepalive: "10s,10", dscp: "0", "allow-fast-path": "false", "clamp-tcp-mss": "true", "ipsec-secret": "fixture-psk-value", comment: writeGreComment("fixture-psk-value", "192.0.2.2/30") } +const address = { ".id": "*A", interface: "*G", address: "192.0.2.1/30", disabled: "false", dynamic: "false", invalid: "false" } +const observed = observeGreCatalog(1, [row], [address], [{ ...address, interface: "gre1", address: "fd00::1/64" }])[0] +assert.equal(observed.localAddresses.length, 2); assert.equal(observed.localAddresses[0].configurationVerified, true) +assert.equal(observed.remoteInnerIp, "192.0.2.2/30"); assert.equal(observed.remoteInnerIpVerified, false) +assert.equal(observed.status, "up"); assert.equal(observed.dscp, 0); assert.equal(observed.keepaliveInterval, 10) +assert.equal(observed.keepaliveRetries, 10); assert.equal(observed.allowFastPath, false); assert.equal(observed.ipsecConfigured, true) +assert.ok(!JSON.stringify(observed).includes("fixture-psk-value")); assert.equal(observed.comment, "[redacted]") +const unknown = observeGreCatalog(1, [{ ".id": "*U", name: "unknown" }], [], [])[0] +for (const key of ["enabled", "running", "mtu", "dscp", "keepaliveInterval", "keepaliveRetries", "allowFastPath", "clampTcpMss", "ipsecConfigured"] as const) assert.equal(unknown[key], null, key) +assert.equal(unknown.status, "unknown") +const metadata = observeGreCatalog(1, [{ ...row, comment: "address=192.0.2.1/30 gateway=192.0.2.2/30" }], [], [])[0] +assert.equal(metadata.localInnerIpMetadata, "192.0.2.1/30"); assert.equal(metadata.localInnerIpMetadataVerified, false); assert.deepEqual(metadata.localAddresses, []) +for (const keepalive of ["none", "0"]) assert.equal(observeGreCatalog(1, [{ ...row, keepalive }], [], [])[0].keepaliveInterval, 0) +for (const keepalive of ["invalid,10", "10s", "10s,ten", "10s,10,10"]) assert.equal(observeGreCatalog(1, [{ ...row, keepalive }], [], [])[0].keepaliveInterval, null) +for (const update of [{ invalid: "true" }, { disabled: "true" }, { dynamic: "true" }, { invalid: "" }, { address: "192.0.2.1/33" }, { address: "fd00::1/64" }]) assert.equal(observeGreCatalog(1, [row], [{ ...address, ...update }], [])[0].localAddresses[0].configurationVerified, false) +for (const raw of [[row, row], [{ ...row, ".id": "" }], [{ ...row, name: 1 }]]) assert.throws(() => observeGreCatalog(1, raw, [], [])) +assert.throws(() => observeGreCatalog(1, [row], [address, address], [])) +assert.throws(() => observeGreCatalog(1, [row], [{ ".id": "*A" }], [])) +assert.throws(() => observeGreCatalog(1, [row, { ".id": "*B", name: "*G" }], [address], []), /Неоднозначная/) +assert.throws(() => observeGreCatalog(1, Array(10001).fill(row), [], [])) +console.log("GRE observations: native identity, v4/v6 parent IDs, nullable flags, keepalive, metadata provenance, PSK reflection and malformed snapshots checked") diff --git a/backend/src/services/gre-observed-read.ts b/backend/src/services/gre-observed-read.ts new file mode 100644 index 00000000..16053037 --- /dev/null +++ b/backend/src/services/gre-observed-read.ts @@ -0,0 +1,51 @@ +import { isIP } from "node:net" +import { readGreComment } from "./gre-comment.js" +import { readGreNativeRows } from "./gre-observed-state.js" +import { maskNetworkSecrets, nativeNetworkRows, type NativeNetworkRow } from "./network-observed-read.js" +import { observedBoolean, observedDurationMs, observedInteger } from "./ospf-observed-values.js" + +/** Project observations independently from the defaults used by the legacy editor. */ +export function observeGreCatalog(serverId: number, raw: unknown, ipv4: unknown, ipv6: unknown) { + const rows = readGreNativeRows(nativeNetworkRows(raw, [".id", "name"])) + const addresses: Array<{ raw: NativeNetworkRow; family: 4 | 6 }> = [nativeNetworkRows(ipv4, [".id", "address", "interface"]).map(row => ({ raw: row, family: 4 as const })), nativeNetworkRows(ipv6, [".id", "address", "interface"]).map(row => ({ raw: row, family: 6 as const }))].flat() + const identities = new Map>() + for (const row of rows) for (const ref of [row.name, row[".id"]]) identities.set(ref, new Set([...(identities.get(ref) ?? []), row[".id"]])) + const addressesById = new Map() + for (const address of addresses) { + const candidates = identities.get(address.raw.interface!) + if (candidates && candidates.size > 1) throw new Error("Неоднозначная привязка адреса GRE") + const parent = candidates?.values().next().value + if (parent) { const values = addressesById.get(parent) ?? []; values.push(address); addressesById.set(parent, values) } + } + const secrets = rows.map(row => row["ipsec-secret"]).filter(Boolean) + const tunnels = rows.map(row => { + const disabled = observedBoolean(row.disabled), running = observedBoolean(row.running) + const metadata = readGreComment(row.comment) + const keepalive = row.keepalive?.trim() + const parts = keepalive?.split(",") + const interval = parts?.length === 2 ? /^\d+$/.test(parts[0].trim()) ? observedInteger(parts[0].trim()) : (observedDurationMs(parts[0].trim()) ?? NaN) / 1000 : null + const retries = parts?.length === 2 ? observedInteger(parts[1].trim()) : null + const keepaliveDisabled = keepalive === "none" || keepalive === "0" + const keepaliveKnown = keepaliveDisabled || interval !== null && Number.isFinite(interval) && interval >= 0 && retries !== null + const localAddresses = (addressesById.get(row[".id"]) ?? []).sort((a, b) => a.family - b.family || (a.raw[".id"] ?? "").localeCompare(b.raw[".id"] ?? "")).map(({ raw: address, family: addressFamily }) => { + const [host, prefix, extra] = (address.address ?? "").split("/"), family = isIP(host) + const validCidr = family === addressFamily && extra === undefined && /^\d+$/.test(prefix ?? "") && Number(prefix) <= (family === 4 ? 32 : 128) + return { rosId: address[".id"], address: address.address ?? null, family: addressFamily, + disabled: observedBoolean(address.disabled), dynamic: observedBoolean(address.dynamic), invalid: observedBoolean(address.invalid), + configurationVerified: validCidr && observedBoolean(address.disabled) === false && observedBoolean(address.dynamic) === false && observedBoolean(address.invalid) === false } + }) + return { id: `${serverId}:${row[".id"]}`, rosId: row[".id"], serverId: String(serverId), name: row.name, + localAddress: row["local-address"] ?? null, remoteAddress: row["remote-address"] ?? null, + localInnerIpMetadata: row.comment?.match(/address\s*=\s*([0-9a-fA-F:.]+\/\d+)/)?.[1] ?? null, localInnerIpMetadataVerified: false, + localAddresses, remoteInnerIp: metadata.remoteInnerIp || null, remoteInnerIpSource: "comment-metadata", remoteInnerIpVerified: false, + comment: metadata.comment, enabled: disabled === null ? null : !disabled, running, + status: disabled === null || !disabled && running === null ? "unknown" : disabled ? "down" : running ? "up" : "degraded", + mtu: observedInteger(row.mtu, 68, 65535), dscp: row.dscp === "inherit" ? "inherit" : observedInteger(row.dscp, 0, 63), + keepalive: keepalive ?? null, keepaliveInterval: keepaliveKnown ? keepaliveDisabled ? 0 : interval : null, + keepaliveRetries: keepaliveKnown ? keepaliveDisabled ? 0 : retries : null, + clampTcpMss: observedBoolean(row["clamp-tcp-mss"]), allowFastPath: observedBoolean(row["allow-fast-path"]), + ipsecConfigured: row["ipsec-secret"] === undefined ? null : row["ipsec-secret"].trim().length > 0, + forwardingVerified: false } + }) + return maskNetworkSecrets(tunnels, secrets) +} diff --git a/backend/src/services/gre-revision-restore.test.ts b/backend/src/services/gre-revision-restore.test.ts new file mode 100644 index 00000000..cf90116e --- /dev/null +++ b/backend/src/services/gre-revision-restore.test.ts @@ -0,0 +1,103 @@ +import assert from "node:assert/strict" +import {validateGreRestoreSnapshot,materializeGreRestore,planGreRevisionRestore,executeGreRevisionSteps,readGreRestoreState,greRestoreWireOp} from "./gre-revision-restore.js" +import {canonicalGreSnapshot,type GreSnapshot,type RosWriteOp} from "./entity-snapshots.js" +import {greDependencyPaths,type GreMutationState} from "./gre-mutation-state.js" +import {mapGreLive} from "./gre-live.js" +import type {ServerRow} from "../modules/servers/repository/servers-repository.js" + +const server={id:1,name:"lab"}as ServerRow +let expiredAccess=0 +await assert.rejects(readGreRestoreState(server,async()=>{expiredAccess++},undefined,Date.now()-1),/срок/) +assert.equal(expiredAccess,0) +const empty=():GreMutationState=>({serverConfiguration:"source",restoreProtectionProof:"protections",gre:[],ipv4:[],ipv6:[],dependencies:Object.fromEntries(greDependencyPaths.map(p=>[p,[]])),bindings:[],filters:[]}) +const native={".id":"*G",name:"old","local-address":"192.0.2.1","remote-address":"192.0.2.2",mtu:"1476",keepalive:"1m,3",dscp:"inherit",disabled:"false",dynamic:"false",running:"true","clamp-tcp-mss":"true","allow-fast-path":"false","ipsec-secret":" key ",comment:"old-comment","dont-fragment":"yes"} +const wanted=(name:string)=>({name,localAddress:"",remoteAddress:"192.0.2.2",localInnerIp:"",remoteInnerIp:"",innerAddresses:[],comment:"",disabled:false,mtu:1400,keepalive:"0",dscp:"inherit",clampTcpMss:true,allowFastPath:false,ipsecSecret:""}) +const defaultCreate:RosWriteOp={op:"put",path:"/interface/gre",body:{name:"gre","ipsec-secret":"","allow-fast-path":"yes"}} +const defaultWire=greRestoreWireOp(defaultCreate,empty()) +assert.ok("body"in defaultWire&&!("ipsec-secret"in defaultWire.body));assert.equal(defaultCreate.body["ipsec-secret"],"") +const keyState=empty();keyState.gre=[{...native}] +const clearKey:RosWriteOp={op:"patch",path:"/interface/gre/*G",body:{"ipsec-secret":"","allow-fast-path":"no"}} +assert.equal(greRestoreWireOp(clearKey,keyState),clearKey,"A real key removal must not be silently omitted") +keyState.gre[0]["ipsec-secret"]="";assert.ok("body"in greRestoreWireOp(clearKey,keyState)&&!("ipsec-secret"in(greRestoreWireOp(clearKey,keyState)as{body:Record}).body)) +const snapshot=(...names:string[])=>canonicalGreSnapshot({tunnels:names.map(wanted)}) +let sequence=0 +function apply(state:GreMutationState,op:RosWriteOp){ + const next=structuredClone(state),path=["/interface/gre","/ip/address","/ipv6/address"].find(p=>op.path===p||op.path.startsWith(p+"/"))! + const key=path==="/interface/gre"?"gre":path==="/ip/address"?"ipv4":"ipv6",id=op.path.slice(path.length+1) + const body="body"in op?Object.fromEntries(Object.entries(op.body).map(([k,v])=>[k,v==="yes"?"true":v==="no"?"false":v])):{} + if(op.op==="put"){ + const added={...(key==="gre"?native:{dynamic:"false",invalid:"false","no-dad":"false","eui-64":"false","from-pool":""}),...body,".id":"*N"+(++sequence)} + next[key].push(added) + if(key==="gre")next.dependencies["/interface"].push({".id":added[".id"],name:added.name,type:"gre"}) + }else if(op.op==="patch")Object.assign(next[key].find(r=>r[".id"]===id)!,body) + else if(op.op==="delete"){ + next[key]=next[key].filter(r=>r[".id"]!==id) + if(key==="gre")next.dependencies["/interface"]=next.dependencies["/interface"].filter(r=>r[".id"]!==id) + } + return next +} +for(const invalid of [null,{},[],{tunnels:"oops"},{tunnels:[{}]},{tunnels:[wanted("a"),wanted("a")]},{...snapshot("a"),extra:true}])assert.throws(()=>validateGreRestoreSnapshot(invalid)) +assert.deepEqual(validateGreRestoreSnapshot({tunnels:[]}),{tunnels:[]}) +assert.throws(()=>validateGreRestoreSnapshot({tunnels:Array.from({length:51},(_,i)=>wanted("gre-"+i))})) +assert.throws(()=>planGreRevisionRestore(snapshot(...Array.from({length:26},(_,i)=>"gre-"+i)),empty()),/50 native/) +assert.equal(validateGreRestoreSnapshot(canonicalGreSnapshot({tunnels:[{...wanted("a"),ipsecSecret:" "}]})).tunnels[0].ipsecSecret," ") +assert.equal(materializeGreRestore(canonicalGreSnapshot({tunnels:[{...wanted("a"),ipsecSecret:" "}]}),empty()).tunnels[0].ipsecSecret," ") +assert.throws(()=>validateGreRestoreSnapshot(canonicalGreSnapshot({tunnels:[{...wanted("a"),ipsecSecret:" ",allowFastPath:true}]})),/FastPath/) +const original=empty();original.gre=[{...native}];original.ipv4=[{".id":"*A",interface:"old",address:"198.18.1.1/30",dynamic:"false",disabled:"false",comment:"old-address"}] +original.dependencies["/interface"]=[{".id":"*G",name:"old",type:"gre"},{".id":"*E",name:"ether1",type:"ether"}] +original.ipv6=[{".id":"*F",interface:"ether1",address:"fd00:99::1/64",dynamic:"false",disabled:"false"}] +const current=mapGreLive(server,original.gre,[...original.ipv4,...original.ipv6]).snapshot +assert.equal(validateGreRestoreSnapshot(current).tunnels[0].mtu, 1476) +const automatic = structuredClone(original); automatic.gre[0].mtu = "auto" +const automaticSnapshot = mapGreLive(server,automatic.gre,[...automatic.ipv4,...automatic.ipv6]).snapshot +assert.equal(validateGreRestoreSnapshot(automaticSnapshot).tunnels[0].mtu, "auto") +assert.deepEqual(planGreRevisionRestore(materializeGreRestore(automaticSnapshot, automatic), automatic), []) +const restoreAuto = planGreRevisionRestore(materializeGreRestore(automaticSnapshot, original), original)[0] +assert.equal(restoreAuto.kind === "native" && "body" in restoreAuto.op && restoreAuto.op.body.mtu, "auto") +assert.equal(current.tunnels[0].ipsecSecret," key ") +assert.equal(validateGreRestoreSnapshot(current).tunnels[0].ipsecSecret," key ") +const oldMasked=structuredClone(current);oldMasked.tunnels[0].ipsecSecret="*****" +assert.equal(materializeGreRestore(oldMasked,original).tunnels[0].ipsecSecret," key ") +oldMasked.tunnels[0].ipsecSecret="";assert.equal(materializeGreRestore(oldMasked,original).tunnels[0].ipsecSecret," key ") +assert.throws(()=>materializeGreRestore(canonicalGreSnapshot({tunnels:[{...wanted("new"),ipsecSecret:"(hidden)"}]}),original),/PSK/) +assert.deepEqual(planGreRevisionRestore(materializeGreRestore(current,original),original),[]) +const desired=snapshot("old","new");desired.tunnels.find(r=>r.name==="old")!.innerAddresses=[{address:"fd00::1/64",disabled:false,advertise:false,noDad:true,comment:"new-address"}] +desired.tunnels.find(r=>r.name==="new")!.innerAddresses=[{address:"198.18.2.1/30",disabled:false,comment:""}] +const fixed=materializeGreRestore(validateGreRestoreSnapshot(canonicalGreSnapshot(desired)),original) +let state=structuredClone(original),commands:RosWriteOp[]=[],gates=0 +const execute=async(target:GreSnapshot)=>executeGreRevisionSteps(structuredClone(state),planGreRevisionRestore(target,state),{read:async()=>structuredClone(state),authorizeRevision:async()=>{gates++},validate:s=>{planGreRevisionRestore(target,s)},command:async(op,gate)=>{await gate();commands.push(op);state=apply(state,op)}}) +const receipt=await execute(fixed) +assert.equal(receipt.verified,true);assert.equal(receipt.completedCommands,commands.length);assert.ok(gates>=commands.length*2) +assert.equal(state.gre.length,2);assert.ok(state.gre.every(r=>r.disabled==="false"));assert.equal(state.gre.find(r=>r.name==="old")!["ipsec-secret"]," key ") +assert.equal(state.gre.find(r=>r.name==="old")!["dont-fragment"],"yes");assert.equal(state.gre.find(r=>r.name==="old")!["local-address"],"0.0.0.0");assert.equal(state.gre.find(r=>r.name==="old")!.comment,"") +assert.deepEqual(state.ipv6.find(r=>r.interface==="ether1"),original.ipv6[0]);assert.equal(state.ipv6.find(r=>r.interface==="old")!["no-dad"],"true") +const firstAddress=commands.findIndex(op=>!op.path.startsWith("/interface/gre")),oldDisable=commands.findIndex(op=>op.path==="/interface/gre/*G"&&"body"in op&&op.body.disabled==="yes") +assert.ok(oldDisable>=0&&oldDisable"body"in op&&op.body.disabled==="no")) +assert.deepEqual(planGreRevisionRestore(fixed,state),[]) +const deletion=planGreRevisionRestore({tunnels:[]},original) +assert.deepEqual(deletion[0],{kind:"native",op:{op:"patch",path:"/interface/gre/*G",body:{disabled:"yes"}}}) +assert.equal(deletion.at(-1)?.kind,"native");state=structuredClone(original);commands=[] +assert.equal((await execute({tunnels:[]})).verified,true);assert.equal(state.gre.length,0);assert.equal(state.ipv4.length,0);assert.deepEqual(state.ipv6,original.ipv6) +for(const path of ["/routing/ospf/interface-template","/routing/bgp/connection","/ip/vrf","/ipv6/firewall/filter"]){const bound=structuredClone(original);bound.dependencies[path]=[{".id":"*D",interface:"old"}];assert.throws(()=>planGreRevisionRestore({tunnels:[]},bound),/используется/)} +const bound=structuredClone(original);bound.bindings=[{id:"user",interfaceName:"old"}];assert.throws(()=>planGreRevisionRestore({tunnels:[]},bound),/привязкой/) +const collision=empty();collision.dependencies["/interface"]=[{".id":"*E",name:"new",type:"ether"}];assert.throws(()=>planGreRevisionRestore(snapshot("new"),collision),/занято/) +const pooled=structuredClone(original);pooled.ipv6.push({".id":"*P",interface:"old",address:"fd00::1/64",dynamic:"false",disabled:"false","from-pool":"pool"});assert.throws(()=>materializeGreRestore(current,pooled),/пула/) +const legacy=structuredClone(current);delete legacy.tunnels[0].innerAddresses +const multiple=structuredClone(original);multiple.ipv4.push({...original.ipv4[0],".id":"*B",address:"198.18.3.1/30"});assert.throws(()=>materializeGreRestore(legacy,multiple),/Старая/) +const duplicate=structuredClone(current);duplicate.tunnels[0].innerAddresses!.push({...duplicate.tunnels[0].innerAddresses![0]});assert.throws(()=>validateGreRestoreSnapshot(duplicate)) +for(const failure of ["ignored","collateral","lost","late-binding","after-intent"]){ + state=structuredClone(original);let writes=0,reads=0 + const steps=planGreRevisionRestore(fixed,state) + const run=()=>executeGreRevisionSteps(structuredClone(state),steps,{read:async()=>{reads++;return structuredClone(state)},authorizeRevision:async()=>{},validate:s=>{planGreRevisionRestore(fixed,s)},command:async(op,gate)=>{ + if(failure==="after-intent"){state.restoreProtectionProof="changed";await gate();assert.fail("Late protection must refuse dispatch")} + await gate();writes++ + if(failure!=="ignored")state=apply(state,op) + if(failure==="collateral")state.gre[0]["dont-fragment"]="no" + if(failure==="late-binding")state.bindings.push({id:"late",interfaceName:"old"}) + if(failure==="lost")throw Error("Lost response") + }}) + if(["lost","after-intent"].includes(failure))await assert.rejects(run()) + else assert.equal((await run()).verified,false) + assert.equal(writes,failure==="after-intent"?0:1);assert.ok(reads>0) +} +console.log("GRE revision restore: strict payload, exact PSK/opaque preservation, multi-GRE/v4/v6, disable before removals and enable last, full deletion/no-op, dependencies/legacy ambiguity, per-command drift, lost/ignored/collateral and after-intent refusal without replay") diff --git a/backend/src/services/gre-revision-restore.ts b/backend/src/services/gre-revision-restore.ts new file mode 100644 index 00000000..9ac86571 --- /dev/null +++ b/backend/src/services/gre-revision-restore.ts @@ -0,0 +1,148 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { pool } from "../db/index.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { fingerprint,privateSecretFingerprint } from "../modules/mcp/security.js" +import { fingerprintPayload,captureAndAppendRevision } from "./config-revisions.js" +import { canonicalGreSnapshot,greAddressKey,greAddressPath,greInterfaceBody,isHiddenSecret,planGreRestore,type GreSnapshot,type RosWriteOp } from "./entity-snapshots.js" +import { mapGreLive } from "./gre-live.js" +import { greMtuSchema } from "./gre-mutation-input.js" +import { assertGreDeleteDependencies,greMutationProblem,greMutationProof,greNativeValue,readGreMutationState,type GreMutationState } from "./gre-mutation-state.js" +import { verifyGreMutationOp } from "./gre-mutation.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock,withConfigMutationLock } from "./config-mutation-lock.js" + +const problem=greMutationProblem,same=(a:unknown,b:unknown)=>fingerprint(a)===fingerprint(b) +// Empty old revisions preserve a key; a nonempty whitespace key is still a key. +const unknownRevisionPsk=(value:string)=>value===""||value.trim().length>0&&isHiddenSecret(value) +const cidr=z.string().max(100).refine(value=>{try{greAddressPath(value);return true}catch{return false}}) +const address=z.object({address:cidr,disabled:z.boolean(),comment:z.string().max(1000).optional(),advertise:z.boolean().optional(),noDad:z.boolean().optional(),fromPool:z.string().max(100).optional(),eui64:z.boolean().optional()}).strict() +const tunnel=z.object({name:z.string().trim().min(1).max(100),localAddress:z.string().refine(v=>v===""||isIP(v)===4),remoteAddress:z.string().refine(v=>isIP(v)===4),localInnerIp:z.union([z.literal(""),cidr]),innerAddresses:z.array(address).max(50).optional(),remoteInnerIp:z.string().max(100),comment:z.string().max(1000),disabled:z.boolean(),mtu:greMtuSchema, + keepalive:z.string().max(100).refine(v=>{const n=greNativeValue("keepalive",v);return n==="none"||Array.isArray(n)&&n[0]>0&&n[1]>0}),dscp:z.string().refine(v=>v==="inherit"||/^\d{1,2}$/.test(v)&&Number(v)<=63),clampTcpMss:z.boolean(),allowFastPath:z.boolean(),ipsecSecret:z.string().max(1024)}).strict() +export function validateGreRestoreSnapshot(payload:unknown):GreSnapshot{ + let size:number;try{size=Buffer.byteLength(JSON.stringify(payload))}catch{throw problem("Повреждена ревизия GRE")} + const parsed=z.object({tunnels:z.array(tunnel).max(50)}).strict().safeParse(payload) + if(size>1048576||!parsed.success)throw problem("Повреждена схема или превышен размер ревизии GRE") + const desired=canonicalGreSnapshot(parsed.data) + if(!same(desired,payload)||new Set(desired.tunnels.map(row=>row.name)).size!==desired.tunnels.length||desired.tunnels.reduce((n,row)=>n+(row.innerAddresses?.length??(row.localInnerIp?1:0)),0)>50)throw problem("Неполная или неоднозначная ревизия GRE") + for(const row of desired.tunnels){ + if(row.innerAddresses?.some(a=>a.fromPool||a.eui64))throw problem("Ревизия содержит неподдерживаемый IPv6 pool/EUI-64") + if(row.innerAddresses&&new Set(row.innerAddresses.map(a=>greAddressKey(a.address))).size!==row.innerAddresses.length)throw problem("В ревизии GRE повторяется адрес") + if(!unknownRevisionPsk(row.ipsecSecret)&&row.allowFastPath)throw problem("Ревизия IPsec несовместима с FastPath") + } + return desired +} +export async function readGreRestoreRevision(serverId:number,id:string,authorize:()=>Promise=async()=>{}){ + await authorize() + const row=(await pool.query("SELECT id,server_id,section,fingerprint,created_at::text AS created_at,CASE WHEN octet_length(payload::text)<=1048576 THEN payload ELSE NULL END AS payload FROM config_revisions WHERE id=$1 AND server_id=$2 AND section='gre'",[id,serverId])).rows[0] + await authorize() + if(!row||!row.payload||row.fingerprint!==fingerprintPayload(row.payload))throw problem("Ревизия GRE отсутствует, повреждена или принадлежит другой области") + const desired=validateGreRestoreSnapshot(row.payload) + return {desired,revisionProof:await privateSecretFingerprint(fingerprint(row),"gre-restore-revision-v1")} +} +export async function readGreRestoreState(server:ServerRow,authorize:()=>Promise=async()=>{},signal?:AbortSignal,executionDeadlineAt=Infinity){ + const deadlineAt=Math.min(Date.now()+45000,executionDeadlineAt) + const check=async()=>{signal?.throwIfAborted();if(Date.now()>=deadlineAt)throw problem("Истёк срок чтения GRE restore");await authorize();const fresh=await getServerRowById(server.id);if(!fresh?.enabled||!same(fresh,server))throw problem("Источник GRE restore изменился");signal?.throwIfAborted()} + const protections=async()=>{await check();const rows=(await pool.query("SELECT id,object_ref,created_at::text AS created_at FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) ORDER BY id LIMIT 1001",[String(server.id)])).rows;if(rows.length>1000||Buffer.byteLength(JSON.stringify(rows))>1048576)throw problem("Превышен бюджет защит GRE restore");return privateSecretFingerprint(fingerprint(rows),"gre-restore-protections-v1")} + const proof=await protections(),state=await readGreMutationState(server,null,{allTargets:true,beforeRead:check,deadlineAt,signal}) + if(await protections()!==proof)throw problem("Защиты GRE restore изменились во время чтения") + return {...state,restoreProtectionProof:proof} +} +export function greRestoreStateProof(state:GreMutationState){return privateSecretFingerprint(fingerprint(greMutationProof(state)),"gre-restore-state-v1")} +/** Materialize values omitted by old revisions from the confirmed original state only. */ +export function materializeGreRestore(desired:GreSnapshot,before:GreMutationState):GreSnapshot{ + const mapped=mapGreLive({id:0,name:""}as ServerRow,before.gre,[...before.ipv4,...before.ipv6]) + // The shared legacy planner rejects ambiguous old single-address snapshots. + planGreRestore(desired,mapped) + return canonicalGreSnapshot({tunnels:desired.tunnels.map(want=>{ + const live=before.gre.find(row=>row.name===want.name),body=greInterfaceBody(want) + if(!live&&want.ipsecSecret!==""&&unknownRevisionPsk(want.ipsecSecret))throw problem("Нельзя создать GRE с неизвестным PSK") + const secret=unknownRevisionPsk(want.ipsecSecret)?live?.["ipsec-secret"]??"":want.ipsecSecret + if(secret&&want.allowFastPath)throw problem("Сохранённый PSK несовместим с FastPath ревизии") + const addresses=want.innerAddresses??(want.localInnerIp?[{address:want.localInnerIp,disabled:false}]:[]) + return {...want,comment:body.comment??"",ipsecSecret:secret,innerAddresses:addresses.map(a=>{ + const old=[...before.ipv4,...before.ipv6].find(row=>row.interface===want.name&&greAddressKey(row.address)===greAddressKey(a.address)&&observedBoolean(row.dynamic)===false) + return {...a,comment:a.comment??old?.comment??"",...(greAddressPath(a.address)==="/ipv6/address"?{advertise:a.advertise===true,noDad:a.noDad??(old?observedBoolean(old["no-dad"])===true:false)}:{})} + })} + })}) +} +export type GreRestoreStep={kind:"native";op:RosWriteOp}|{kind:"enable";name:string} +/** RouterOS treats presence of an empty ipsec-secret as an IPsec write and + * rejects it together with FastPath. Omit a redundant empty assignment on the + * wire only; per-command verification still uses the original expected body. */ +export function greRestoreWireOp(op:RosWriteOp,before:GreMutationState):RosWriteOp{ + if((op.op!=="put"&&op.op!=="patch")||op.body["ipsec-secret"]!=="")return op + const creating=op.op==="put"&&op.path==="/interface/gre" + const live=op.op==="patch"&&op.path.startsWith("/interface/gre/")?before.gre.find(row=>row[".id"]===decodeURIComponent(op.path.slice("/interface/gre/".length))):undefined + if(!creating&&live?.["ipsec-secret"]!=="")return op + const body={...op.body};delete body["ipsec-secret"] + return{...op,body} +} +export function planGreRevisionRestore(desired:GreSnapshot,before:GreMutationState):GreRestoreStep[]{ + const wanted=new Set(desired.tunnels.map(row=>row.name)) + for(const live of before.gre){ + if(!wanted.has(live.name)){ + assertGreDeleteDependencies(before,live) + if([...before.ipv4,...before.ipv6].some(a=>a.interface===live.name&&observedBoolean(a.dynamic)===true&&!/^fe80:/i.test(a.address)))throw problem("Удаляемый GRE имеет динамический адрес") + } + } + for(const want of desired.tunnels)if(!before.gre.some(row=>row.name===want.name)&&before.dependencies["/interface"].some(row=>row.name===want.name))throw problem("Имя GRE ревизии занято другим интерфейсом") + const mapped=mapGreLive({id:0,name:""}as ServerRow,before.gre,[...before.ipv4,...before.ipv6]),legacy=planGreRestore(desired,mapped) + const staging:GreRestoreStep[]=before.gre.filter(row=>!wanted.has(row.name)&&observedBoolean(row.disabled)===false).map(row=>({kind:"native",op:{op:"patch",path:"/interface/gre/"+row[".id"],body:{disabled:"yes"}}})),steps:GreRestoreStep[]=[],enable=new Set() + for(const item of legacy){ + let op=item + if((op.op==="put"&&op.path==="/interface/gre")||(op.op==="patch"&&op.path.startsWith("/interface/gre/"))){ + const want=desired.tunnels.find(row=>row.name===("body"in op?op.body.name:undefined))! + const body:Record={...op.body,"local-address":want.localAddress||"0.0.0.0",comment:greInterfaceBody(want).comment??"","ipsec-secret":want.ipsecSecret} + const live=before.gre.find(row=>row.name===want.name) + const addressChanges=legacy.some(row=>!row.path.startsWith("/interface/gre")&&("body"in row?row.body.interface===want.name:mapped.addrs.some(a=>a.interfaceName===want.name&&row.path===greAddressPath(a.address)+"/"+a.rosId))) + if(live&&Object.entries(body).every(([key,value])=>same(greNativeValue(key,live[key]),greNativeValue(key,value)))&&!addressChanges)continue + body.disabled="yes";op={...op,body};if(!want.disabled)enable.add(want.name) + staging.push({kind:"native",op});continue + } + steps.push({kind:"native",op}) + } + for(const name of enable)steps.push({kind:"enable",name}) + if(staging.length+steps.length>50)throw problem("GRE restore превышает 50 native-команд") + return [...staging,...steps] +} +export async function executeGreRevisionSteps(before:GreMutationState,steps:GreRestoreStep[],deps:{read:()=>Promise;command:(op:RosWriteOp,gate:()=>Promise)=>Promise;authorizeRevision:()=>Promise;validate:(state:GreMutationState)=>void}){ + let current=before,completedCommands=0,acknowledgedCommands=0 + for(const step of steps){ + const gate=async()=>{await deps.authorizeRevision();const fresh=await deps.read();if(!same(greMutationProof(fresh),greMutationProof(current)))throw problem("GRE или зависимости изменились между командами");deps.validate(fresh)} + await gate() + const live=step.kind==="enable"?current.gre.find(row=>row.name===step.name):undefined + if(step.kind==="enable"&&!live)throw problem("Не подтверждён созданный GRE для включения") + const op:RosWriteOp=step.kind==="native"?step.op:{op:"patch",path:"/interface/gre/"+live![".id"],body:{disabled:"no"}} + await deps.command(op,gate);acknowledgedCommands++ + const next=await deps.read() + if(!verifyGreMutationOp(current,next,op))return {verified:false,completedCommands,acknowledgedCommands,after:next} + current=next;completedCommands++ + } + return {verified:true,completedCommands,acknowledgedCommands,after:current} +} +export async function restoreGreRevision(server:ServerRow,id:string,options:{beforeProof?:string;revisionProof?:string;desired?:GreSnapshot;authorize?:()=>Promise}={}){ + return withApplicationMutationLock(()=>withConfigMutationLock([server.id],async()=>{ + const deadlineAt=Date.now()+120000 + const authorize=async()=>{if(Date.now()>=deadlineAt)throw problem("Истёк срок выполнения GRE restore");await options.authorize?.();if(Date.now()>=deadlineAt)throw problem("Истёк срок выполнения GRE restore")} + const revision=await readGreRestoreRevision(server.id,id,authorize),before=await readGreRestoreState(server,authorize,undefined,deadlineAt) + if(options.revisionProof&&revision.revisionProof!==options.revisionProof||options.beforeProof&&await greRestoreStateProof(before)!==options.beforeProof)throw problem("GRE или ревизия изменились после подтверждения") + const desired=options.desired?validateGreRestoreSnapshot(options.desired):materializeGreRestore(revision.desired,before),steps=planGreRevisionRestore(desired,before),client=MikrotikClient.fromServer(server) + const checkRevision=async()=>{if((await readGreRestoreRevision(server.id,id,authorize)).revisionProof!==revision.revisionProof)throw problem("Ревизия изменилась во время GRE restore")} + const receipt=await executeGreRevisionSteps(before,steps,{read:()=>readGreRestoreState(server,authorize,undefined,deadlineAt),authorizeRevision:checkRevision,validate:state=>{planGreRevisionRestore(desired,state)},command:(op,gate)=>{ + const wire=greRestoreWireOp(op,before) + const slash=op.path.lastIndexOf("/"),path=op.path.slice(0,slash+1)+encodeURIComponent(op.path.slice(slash+1)) + const timeout=Math.max(1,Math.min(op.op==="delete"?10000:15000,deadlineAt-Date.now())) + if(wire.op==="put")return client.put(wire.path,wire.body,timeout,gate) + if(op.op==="delete")return client.delete(path,timeout,gate) + if(wire.op==="patch")return client.patch(path,wire.body,timeout,gate) + throw problem("Недопустимая команда GRE restore") + }}) + const verified=receipt.verified&&planGreRevisionRestore(desired,receipt.after).length===0 + const mapped=mapGreLive(server,receipt.after.gre,[...receipt.after.ipv4,...receipt.after.ipv6]) + const revisionRecorded=verified&&(await captureAndAppendRevision({serverId:server.id,section:"gre",source:"rollback",capture:async()=>{await authorize();return mapped.snapshot}}))!==null + return {verified,acknowledged:receipt.acknowledgedCommands>0,commandCount:steps.length,completedCommands:receipt.completedCommands,acknowledgedCommands:receipt.acknowledgedCommands,afterFingerprint:await greRestoreStateProof(receipt.after),revisionRecorded,tunnels:mapped.tunnels,forwardingVerified:false,addressReadinessVerified:false} + })) +} diff --git a/backend/src/services/installation-co-load.owned.test.ts b/backend/src/services/installation-co-load.owned.test.ts new file mode 100644 index 00000000..155b26da --- /dev/null +++ b/backend/src/services/installation-co-load.owned.test.ts @@ -0,0 +1,219 @@ +/** Backend co-load fixture for the full-installation resource matrix. + * Real app bootstrap/scheduler, public MCP poll, main-owned UDP worker and HTTP. + * External frontend/portal are separate phases, not implied by a local PASS. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createSocket } from "node:dgram" +import { existsSync } from "node:fs" +import { createServer } from "node:http" +import { basename, dirname, resolve } from "node:path" +import { monitorEventLoopDelay, performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" + +assert.equal(process.env.MMAPP_OWN_INSTALLATION_COLOAD, "true") +assert.equal(process.env.NODE_ENV, "test") +const endpoint = new URL(process.env.DATABASE_URL ?? "") +assert(["postgres:", "postgresql:"].includes(endpoint.protocol)) +assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.username, "mcp_test"); assert(endpoint.password) +assert.equal(endpoint.pathname, "/postgres"); assert.equal(endpoint.search, ""); assert.equal(endpoint.hash, "") +assert(Number.isInteger(Number(endpoint.port)) && Number(endpoint.port) > 0 && Number(endpoint.port) <= 65535) +const state = resolve(process.env.MCP_STATE_DIR ?? "") +assert.equal(basename(state), "state"); assert.match(basename(dirname(state)), /^mm-mcp-test-[A-Za-z0-9-]+$/) +assert.equal(resolve(process.cwd()), dirname(state)) +assert.match(basename(dirname(dirname(new URL(import.meta.url).pathname))), /^dist-installation-memory(?:-[a-z0-9-]{3,64})?$/) +assert(!/tsx|--import|--require|--loader/.test(process.execArgv.join(" "))) +assert(process.execArgv.includes("--max-old-space-size=192") && process.execArgv.includes("--max-semi-space-size=16")) +assert.equal(process.env.AUTH_REQUIRED, "true"); assert(process.env.AUTH_JWT_SECRET) +assert.equal(process.env.MM_FLOW_READ_RESOURCE_PROFILE, "constrained") +assert.equal(existsSync(resolve(process.env.DATABASE_PATH ?? "")), false) +process.env.FLOW_LISTEN_HOST = "127.0.0.1" +process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH = "true" +process.env.MMAPP_OWN_SQL_DB = "postgres" + +const { pool, closePool } = await import("../db/index.js") +const { initDatabase } = await import("../db/bootstrap.js") +const { buildApp } = await import("../index.js") +const ingest = await import("./traffic-flow-ingest.js") +const settings = await import("./traffic-flow-settings.js") +const { pauseRipeBackgroundWork } = await import("./traffic-flow-ripe.js") +const { acceptServerPollPublicRuntimeSql } = await import("../modules/mcp/server-poll-http.sql-helper.js") +const { createMcpConnection, authenticateMcpToken } = await import("../modules/mcp/store.js") +const { invokeMcpTool } = await import("../modules/mcp/runtime.js") +const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") +const uptime = await import("./uptime-collector.js") +const traffic = await import("./traffic-collector.js") +const internetPath = await import("./internet-path-collector.js") +const { isAnyAlertSnapshotSourceJobRunning } = await import("./scheduler-running.js") +const { withApplicationCollectorLock, withApplicationMutationLock, captureConfigurationLockHandoff, assertNoConfigurationLockContext } = await import("./config-mutation-lock.js") +let nativeRequests = 0 +const sender = createSocket("udp4"), native = createServer((request, response) => { + nativeRequests++ + if (request.method !== "GET" || request.url !== "/rest/interface") { response.writeHead(404).end(); return } + response.setHeader("content-type", "application/json"); response.end("[]") +}) +const controller = new AbortController(), loop = monitorEventLoopDelay({ resolution: 20 }) +const metrics = { sent: 0, reads: 0, readFailures: 0, maxReadMs: 0, readerRefusals: 0, samples: 0, + peakRss: 0, peakHeap: 0, peakExternal: 0, maxPoolWaiting: 0, maxPoolTotal: 0, maxFlowPending: 0 } +let app: Awaited> | undefined, flowServerId: number | undefined +let background: Promise[] = [], backgroundError: unknown, sampler: ReturnType | undefined +const sample = () => { + const memory = process.memoryUsage(), health = ingest.getFlowWorkerHealth() + metrics.samples++; metrics.peakRss = Math.max(metrics.peakRss, memory.rss); metrics.peakHeap = Math.max(metrics.peakHeap, memory.heapUsed) + metrics.peakExternal = Math.max(metrics.peakExternal, memory.external); metrics.maxFlowPending = Math.max(metrics.maxFlowPending, health.pendingSize) + metrics.maxPoolWaiting = Math.max(metrics.maxPoolWaiting, pool.waitingCount); metrics.maxPoolTotal = Math.max(metrics.maxPoolTotal, pool.totalCount) +} +const until = async (work: () => boolean | Promise, description: string) => { + const deadline = performance.now() + 15000 + while (!await work()) { if (backgroundError) throw backgroundError; assert(performance.now() < deadline, description); await delay(25) } +} +function packet(sequence: number) { + const bytes = Buffer.alloc(72), identity = sequence % 1024 + bytes.writeUInt16BE(5,0); bytes.writeUInt16BE(1,2); bytes.writeUInt32BE(Math.floor(Date.now()/1000),8); bytes.writeUInt32BE(sequence,16) + bytes.set([10,144,identity>>8,identity&255],24); bytes.set([10,145,0,1],28) + bytes.writeUInt16BE(1,36); bytes.writeUInt32BE(3,40); bytes.writeUInt32BE(1500,44) + bytes.writeUInt16BE(49152+identity,56); bytes.writeUInt16BE(443,58); bytes.writeUInt8(6,62) + return bytes +} +try { + loop.enable(); sampler = setInterval(sample,250); sampler.unref() + await initDatabase() + assert.equal(existsSync(resolve(process.env.DATABASE_PATH!)), false) + await new Promise(done => native.listen(0,"127.0.0.2",done)) + flowServerId = Number((await pool.query(`INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) + VALUES('owned-coload-flow','127.0.0.2',$1,false,false,'owned-flow','owned-flow',false) RETURNING id`, [(native.address() as {port:number}).port])).rows[0].id) + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,event_at,received_at,effective_at,topics,level,message,generation) + SELECT $1,'owned-coload-flow','127.0.0.1','owned-'||n::text,'owned-time',now()-n*interval '1 millisecond',now(),now()-n*interval '1 millisecond', + ARRAY['info','routing'],'info',repeat('owned log ',52),1 FROM generate_series(1,2048) AS n`,[flowServerId]) + const portSocket = createSocket("udp4") + await new Promise(done => portSocket.bind(0,"127.0.0.1",done)) + const port = portSocket.address().port; await new Promise(done => portSocket.close(done)) + await new Promise(done=>sender.bind(0,"127.0.0.2",done)) + await settings.getTrafficFlowSettingsRow() + await pool.query(`UPDATE traffic_flow_settings SET enabled=true,collector_ip='127.0.0.1',flow_listen_port=$1, + host_private_key='owned-coload-private',host_public_key='owned-coload-public',peers_json=$2::jsonb WHERE id=1`, + [port,JSON.stringify([{serverId:flowServerId,name:"Owned co-load",publicKey:"owned-coload-peer",address:"127.0.0.2",allowedIps:["127.0.0.2/32"]}])]) + settings.invalidateTrafficFlowSettingsCache() + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + const settingsSnapshot = async () => (await pool.query(`SELECT jsonb_build_object( + 'uptime',(SELECT to_jsonb(t) FROM uptime_settings t WHERE id=1), + 'traffic',(SELECT to_jsonb(t) FROM traffic_settings t WHERE id=1), + 'internetPath',(SELECT to_jsonb(t) FROM internet_path_settings t WHERE id=1)) AS value`)).rows[0].value + const beforeBusy = await settingsSnapshot(), owner = await pool.connect() + let unsafeOwner = false + try { + await owner.query("SELECT pg_advisory_lock(hashtextextended('mm-config:application',0))") + for (const collect of [uptime.collectResourceSamplesOnce,uptime.collectPingProbesOnce,traffic.collectTrafficOnce,internetPath.collectInternetPathSnapshotOnce]) + await assert.rejects(collect(),error=>(error as {statusCode?:number}).statusCode===409) + assert.equal(nativeRequests,0,"Busy collector must refuse before native work") + assert.deepEqual(await settingsSnapshot(),beforeBusy,"Busy collector must not publish success or failure settings") + assert.equal(uptime.getUptimeCollectorsState().resources,false) + assert.equal(traffic.isTrafficCollecting(),false); assert.equal(internetPath.isInternetPathCollecting(),false) + } finally { + try { assert.equal((await owner.query("SELECT pg_advisory_unlock(hashtextextended('mm-config:application',0)) AS released")).rows[0].released,true) } + catch { unsafeOwner=true } + owner.release(unsafeOwner) + } + let entered = 0, releaseReaders!: () => void + const readerGate = new Promise(done => { releaseReaders=done }), handoffs: Promise[] = [] + const readerTasks = [0,1].map(()=>withApplicationCollectorLock(async()=>{ + entered++; assert.throws(assertNoConfigurationLockContext) + const handoff=captureConfigurationLockHandoff(); assert(handoff); handoffs.push(handoff) + await withApplicationCollectorLock(async()=>{}) + await assert.rejects(withApplicationMutationLock(async()=>assert.fail("Shared collector cannot upgrade to configuration mutation")),error=>(error as {statusCode?:number}).statusCode===409) + await readerGate + })) + for(const task of readerTasks)void task.catch(()=>{}) + try { + await until(()=>entered===2,"Independent collector readers must coexist") + await assert.rejects(withApplicationMutationLock(async()=>assert.fail("Mutation must exclude active collectors")),error=>(error as {statusCode?:number}).statusCode===409) + } finally { releaseReaders(); await Promise.all(readerTasks) } + await Promise.all(handoffs) + assert.equal(await withApplicationMutationLock(()=>withApplicationCollectorLock(async()=>"exclusive-owner-reentry")),"exclusive-owner-reentry") + app = await buildApp({logger:false,startScheduler:true}) + await app.listen({port:0,host:"127.0.0.1"}) + await until(()=>ingest.getFlowListenerState().bound,"Production main-owned flow worker did not bind") + await until(()=>!isAnyAlertSnapshotSourceJobRunning(),"Initial automatic source jobs did not settle before the measured phase") + const base = app.server.address(); assert(base && typeof base !== "string") + const origin = `http://127.0.0.1:${base.port}` + const actor = {id:"owned-coload-"+randomUUID(),email:"coload@example.invalid",name:"Owned co-load",apps:["mm"], + permissions:["mm:servers:read","mm:servers:write","mm:network:read","mm:traffic:read","mm:mcp:read","mm:mcp:manage","mm:mcp:admin"],issuedAt:Date.now()-1000,expiresAt:Date.now()+3600000} + const jwt = app.jwt.sign({iss:process.env.AUTH_ISSUER??"https://auth.shnt.top",sub:actor.id,email:actor.email,name:actor.name,apps:actor.apps,permissions:actor.permissions,iat:Math.floor(actor.issuedAt/1000),exp:Math.floor(actor.expiresAt/1000)}) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Owned co-load observer",mode:"observer",expiresAt:new Date(actor.expiresAt).toISOString(),permissions:["mm:servers:read"],scope:{serverIds:[flowServerId],operationIds:["servers.list"],allowGlobal:false},limits:{callsPerMinute:120,mutationsPerHour:1,serversPerPlan:1,objectsPerPlan:1}}),actor) + const observer = await authenticateMcpToken(access.token) + const watch = (work: Promise) => work.catch(error => { backgroundError ??= error; controller.abort() }) + background = [watch((async()=>{ while(!controller.signal.aborted) { + await new Promise((done,reject)=>sender.send(packet(metrics.sent+1),port,"127.0.0.1",error=>error?reject(error):done())) + metrics.sent++; await delay(5) + } })())] + const read = async (path: string) => { + const began = performance.now(), response = await fetch(origin+path,{headers:{authorization:"Bearer "+jwt},signal:AbortSignal.timeout(10000)}) + assert.equal(response.status,200,`HTTP co-load read failed: ${path}`) + const reader = response.body?.getReader(); assert(reader); let bytes = 0 + for (;;) { const part=await reader.read(); if(part.done)break; bytes+=part.value.byteLength; assert(bytes<=2*1024*1024,"Bounded HTTP co-load response overflow") } + metrics.reads++; metrics.maxReadMs=Math.max(metrics.maxReadMs,performance.now()-began) + } + for (const path of ["/api/mcp/overview",`/api/logs?serverId=${flowServerId}`]) background.push(watch((async()=>{ + while(!controller.signal.aborted) { try { await read(path) } catch(error) { metrics.readFailures++; throw error }; await delay(1000) } + })())) + background.push(watch((async()=>{ while(!controller.signal.aborted) { + try { await invokeMcpTool(observer,"mm_servers_list",{}); metrics.reads++ } catch(error) { + if ((error as {code?:string}).code!=="RATE_LIMITED") throw error + metrics.readerRefusals++ + } + await delay(2000) + } })())) + await until(()=>ingest.getFlowWorkerHealth().packetsReceived>0,"No genuine UDP arrived") + const delayedApproval = process.env.MMAPP_OWN_COLOAD_DELAYED_APPROVAL === "true" + let beforeApprovalRuntimeChanged = false + const poll = await acceptServerPollPublicRuntimeSql({concurrentApplicationReads:true,...(delayedApproval ? { + beforeHumanApproval: async () => { + const before = await settingsSnapshot() + await delay(20000) + await until(()=>!uptime.getUptimeCollectorsState().resources,"Resource collector did not settle before delayed approval") + const sample = await uptime.collectResourceSamplesOnce() + assert.equal(sample.skipped, undefined) + beforeApprovalRuntimeChanged = JSON.stringify(before) !== JSON.stringify(await settingsSnapshot()) + assert(beforeApprovalRuntimeChanged,"Delayed approval must observe actual collector runtime writes after planning") + console.log("Owned delayed approval:",JSON.stringify({waitMs:20000,actualCollectorRuntimeChanged:true})) + } + } : {})}) + if(delayedApproval){assert.equal(poll.delayedApprovalUsabilityAccepted,true);assert("innerPlanVersion" in poll);assert.equal(poll.innerPlanVersion,3)} + controller.abort(); await Promise.all(background) + if(backgroundError)throw backgroundError + await until(()=>ingest.getFlowWorkerHealth().packetsReceived===metrics.sent,"UDP receive count did not catch up before drain") + const drain = await ingest.drainExistingTrafficFlowWriters(async()=>{},[flowServerId]) + assert(drain.existingWorker.workerPresent) + const aggregateTotals = async () => (await pool.query("SELECT coalesce(sum(bytes),0)::text AS bytes,coalesce(sum(packets),0)::text AS packets FROM flow_minute_stats WHERE server_id=$1",[flowServerId])).rows[0] + await until(async()=>{ + const totals=await aggregateTotals() + return Number(totals.bytes)===metrics.sent*1500&&Number(totals.packets)===metrics.sent*3 + },"Lossless minute counters diverged during MCP maintenance") + const totals=await aggregateTotals(), detail=(await pool.query("SELECT coalesce(sum(bytes),0)::text AS bytes,coalesce(sum(packets),0)::text AS packets,count(*)::int AS buckets FROM flow_buckets WHERE server_id=$1",[flowServerId])).rows[0] + assert(Number(detail.bytes)>0&&Number(detail.bytes)<=Number(totals.bytes)) + assert(Number(detail.packets)>0&&Number(detail.packets)<=Number(totals.packets)) + const health=ingest.getFlowWorkerHealth(); assert.equal(health.dropped,0) + assert(metrics.reads>=4 && metrics.sent>=10) + sample() + console.log("Owned installation backend co-load:",JSON.stringify({backendCoLoadAccepted:true,staleApprovalSafetyAccepted:false,delayedApprovalUsabilityAccepted:delayedApproval&&beforeApprovalRuntimeChanged&&poll.delayedApprovalUsabilityAccepted,wholeInstallation2GbAccepted:false, + delayedHumanApprovalScenario:delayedApproval,beforeApprovalRuntimeChanged,schedulerStarted:true,genuineMainWorker:true,collectorBusyChecks:4,collectorReadersConcurrent:true,collectorUpgradeRejected:true,busyRefusedBeforeNativeAndSettings:true,udpTargetPerSecond:200,flowCardinality:1024,distinctSyntheticExporterAddress:true,seededRouterLogEntries:2048,metrics,poll,health, + losslessCounterSource:"flow_minute_stats",exactAggregateTotals:totals,boundedTopNDetail:detail, + eventLoopP99Ms:loop.percentile(99)/1e6,eventLoopMaxMs:loop.max/1e6,frontendExercised:false,portalExercised:false, + geoipDatabaseLoaded:false,sqlObserverInstalled:false,productionDatabaseTouched:false,realRouterTouched:false})) +} catch(error) { + let storedTotalsByServer:unknown="unavailable" + try { storedTotalsByServer=(await pool.query("SELECT server_id,sum(bytes)::text AS bytes,sum(packets)::text AS packets FROM flow_minute_stats GROUP BY server_id ORDER BY server_id LIMIT 10")).rows } catch { /* Keep original failure; diagnostic queries are never admission or recovery. */ } + console.log("Owned installation co-load failure:",JSON.stringify({metrics,health:ingest.getFlowWorkerHealth(),flowServerId,storedTotalsByServer,wholeInstallation2GbAccepted:false})) + throw error +} finally { + controller.abort(); await Promise.allSettled(background) + if(sampler)clearInterval(sampler);loop.disable() + try { + await ingest.stopTrafficFlowListenerForDatabaseOperation() + await pauseRipeBackgroundWork() + await app?.close() + } finally { + try { sender.close() } catch { /* setup may precede socket use */ } + native.closeAllConnections(); if(native.listening)await new Promise(done=>native.close(()=>done())) + await closePool() + } +} diff --git a/backend/src/services/internet-path-collector.ts b/backend/src/services/internet-path-collector.ts index cae3cb34..98cfc0ba 100644 --- a/backend/src/services/internet-path-collector.ts +++ b/backend/src/services/internet-path-collector.ts @@ -1,4 +1,8 @@ -import { asc, desc, eq } from "drizzle-orm" +import { withPartitionWrite, isPartitionWriteOutcomeUnknown, type PartitionWriteOptions } from "./partition-write-transaction.js" +import { asc, eq } from "drizzle-orm" +import { readLatestInternetPathSnapshot } from "./internet-path-read.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { withApplicationCollectorLock } from "./config-mutation-lock.js" import { db } from "../db/index.js" import { filterRules, @@ -215,7 +219,10 @@ export async function getInternetPathSettings() { return await getSettingsRow() } -export async function updateInternetPathSettings(patch: { enabled?: boolean; intervalSec?: number; retentionDays?: number }) { +export function updateInternetPathSettings(patch: { enabled?: boolean; intervalSec?: number; retentionDays?: number }) { + return executeGuardedApplicationWrite({ kind: "internet-path-settings", id: "1" }, "all", () => updateInternetPathSettingsImpl(patch)) +} +async function updateInternetPathSettingsImpl(patch: { enabled?: boolean; intervalSec?: number; retentionDays?: number }) { const prev = await getSettingsRow() await db.update(internetPathSettings).set({ enabled: patch.enabled ?? prev.enabled, @@ -227,11 +234,14 @@ export async function updateInternetPathSettings(patch: { enabled?: boolean; int } export async function getLatestInternetPathSnapshot() { - const rows = await db.select().from(internetPathSnapshots).orderBy(desc(internetPathSnapshots.id)).limit(1) - return rows[0] ?? null + return readLatestInternetPathSnapshot() } -export async function collectInternetPathSnapshotOnce(): Promise { +export async function collectInternetPathSnapshotOnce(partitionWriteOptions:PartitionWriteOptions={}): Promise { + return withApplicationCollectorLock(() => collectInternetPathUnderApplicationLock(partitionWriteOptions)) +} + +async function collectInternetPathUnderApplicationLock(partitionWriteOptions:PartitionWriteOptions={}): Promise { const sampledAt = new Date().toISOString() if (collecting) { return { @@ -251,10 +261,10 @@ export async function collectInternetPathSnapshotOnce(): Promise writerDb.insert(internetPathSnapshots).values({ sampledAt, payloadJson: payload, - }) + }),partitionWriteOptions) await db.update(internetPathSettings).set({ lastCollectedAt: sampledAt, lastDurationMs: Date.now() - started, @@ -269,6 +279,7 @@ export async function collectInternetPathSnapshotOnce(): Promiseproject({...row,payloadJson:bad},[1],0,10,900,now)) +assert.throws(()=>project({...row,sampledAt:"bad"},[1],0,10,900,now)) +console.log("Stored Internet path: scoped identities, nested pages, zero/unknown values, secrets omitted, timestamp/age/skew, missing servers, duplicate and malformed rejection; no current-path claim passed") diff --git a/backend/src/services/internet-path-observation.ts b/backend/src/services/internet-path-observation.ts new file mode 100644 index 00000000..45e5f6ec --- /dev/null +++ b/backend/src/services/internet-path-observation.ts @@ -0,0 +1,48 @@ +import { z } from "zod" +const id = z.union([z.number().int().positive().safe(),z.string().regex(/^[1-9]\d*$/)]).transform(Number).refine(Number.isSafeInteger) +const text = z.string().nullable().optional().transform(value => value ?? null) +const number = z.number().finite().nonnegative().nullable().optional().transform(value => value ?? null) +const flag = z.boolean().nullable().optional().transform(value => value ?? null) +const uplink = z.object({ id:text,iface:text,name:text,isp:text,configuredIp:text,leasedIp:text,dhcpStatus:text,isDefault:flag }) +const configurationUplink = z.object({id:text,iface:text,name:text,isp:text,ip:text,maxDl:number,maxUl:number}) +const server = z.object({ id,name:text,type:z.enum(["home-router","exit-node","jump-host"]).nullish(),enabled:flag,wanUplinks:z.array(configurationUplink).optional() }) +const gre = z.object({id:z.string().min(1),serverId:id,name:text,localAddress:text,remoteAddress:text,localInnerIp:text,remoteInnerIp:text,mtu:number,enabled:flag,status:z.enum(["up","down","degraded"]).nullish()}) +const rule = z.object({id:z.string().min(1),community:z.string(),action:z.enum(["route","blackhole"]),gateway:text,gatewayTunnelId:text}) +const ruleset = z.object({serverId:id,rules:z.array(rule)}) +const speed = z.object({id:z.string().min(1),srcServerId:id,dstServerId:id,srcInterface:text,dstInterface:text,protocol:z.enum(["tcp","udp"]).nullish(),direction:z.enum(["transmit","receive","both"]).nullish(),enabled:flag,lastRunAt:text,lastTxAvgMbps:number,lastRxAvgMbps:number,lastPingRttMs:number,lastPingLossPct:number,lastPingAt:text}) +const snapshotSchema = z.object({ + bindingShapeValid:z.literal(true), + collectionComplete:z.boolean().nullable(),sampledAt:z.string().nullable(),internetTarget:z.string().nullable(), + servers:z.array(server),greTunnels:z.array(gre),filtersRulesets:z.array(ruleset),speedProbes:z.array(speed), + routeLookupByServerId:z.record(z.string(),z.object({gateway:text,routingMark:text})), + wanRuntimeByHomeId:z.record(z.string(),z.object({defaultGateway:text,defaultInterface:text,uplinks:z.array(uplink)})), +}) +function unique(rows:T[],key:(row:T)=>string) { + if(new Set(rows.map(key)).size!==rows.length)throw Error("Неоднозначные объекты сохранённого Internet path") +} + +/** This is an application interpretation saved by the legacy collector. Nullable + * fields and provenance prevent it being presented as a current path probe. */ +export function projectInternetPathObservation(row:{id:number|string;sampledAt:string;payloadJson:unknown}, serverIds:number[], offset:number,limit:number,maxAgeSec:number, now=Date.now()) { + const sampledAt=new Date(row.sampledAt).toISOString(), timestamp=Date.parse(sampledAt) + const source=snapshotSchema.parse(row.payloadJson), allowed=new Set(serverIds) + if(source.sampledAt!==null && Date.parse(source.sampledAt)!==timestamp)throw Error("Дата содержимого Internet path не совпадает со снимком") + const allIds=[...source.servers.map(item=>item.id),...source.greTunnels.map(item=>item.serverId),...source.filtersRulesets.map(item=>item.serverId),...source.speedProbes.flatMap(item=>[item.srcServerId,item.dstServerId]),...Object.keys(source.routeLookupByServerId).map(Number),...Object.keys(source.wanRuntimeByHomeId).map(Number)] + if(allIds.some(value=>!allowed.has(value)))throw Error("Снимок Internet path выходит за выбранную область") + unique(source.servers,item=>String(item.id));unique(source.greTunnels,item=>item.serverId+":"+item.id) + unique(source.filtersRulesets,item=>String(item.serverId));unique(source.speedProbes,item=>item.id) + for(const group of source.filtersRulesets)unique(group.rules,item=>item.id) + const page=(items:T[])=>({items:items.slice(offset,offset+limit),total:items.length,nextOffset:offset+limit({...item,wanUplinks:item.wanUplinks===undefined?null:page(item.wanUplinks)}))) + const greTunnels=page(source.greTunnels),speedProbes=page(source.speedProbes) + const filtersRulesets=page(source.filtersRulesets.map(item=>({...item,rules:page(item.rules)}))) + const wanRuntimeByHomeId=Object.fromEntries(Object.entries(source.wanRuntimeByHomeId).map(([key,value])=>[key,{...value,uplinks:page(value.uplinks)}])) + const pages=[servers,greTunnels,speedProbes,filtersRulesets,...filtersRulesets.items.map(item=>item.rules),...servers.items.flatMap(item=>item.wanUplinks?[item.wanUplinks]:[]),...Object.values(wanRuntimeByHomeId).map(item=>item.uplinks)] + const missingServerIds=serverIds.filter(value=>!source.servers.some(item=>item.id===value)) + const clockSkew=timestamp>now+30000,ageSec=Math.max(0,Math.floor((now-timestamp)/1000)) + return {snapshotId:String(row.id),sampledAt,sourceCollectionComplete:source.collectionComplete, + internetTarget:source.internetTarget,servers,greTunnels,filtersRulesets,speedProbes,routeLookupByServerId:source.routeLookupByServerId,wanRuntimeByHomeId, + missingServerIds,ageSec,clockSkew,stale:clockSkew||ageSec>maxAgeSec,live:false,forwardingVerified:false,currentRoutingVerified:false, + selectedSubgraph:true,source:"stored-application-interpretation",incomplete:source.collectionComplete!==true||missingServerIds.length>0||pages.some(value=>value.items.length `CASE WHEN jsonb_typeof(payload_json->'${field}')='array' THEN + (SELECT coalesce(jsonb_agg(value ORDER BY ord),'[]'::jsonb) FROM jsonb_array_elements(payload_json->'${field}') WITH ORDINALITY AS items(value,ord) + WHERE ${selector}) ELSE NULL END` +const selectedMap = (field: string) => `CASE WHEN jsonb_typeof(payload_json->'${field}')='object' THEN + (SELECT coalesce(jsonb_object_agg(key,value),'{}'::jsonb) FROM jsonb_each(payload_json->'${field}') WHERE key=ANY($1::text[])) ELSE NULL END` +const bindingValid = (field:string,keys:string[]) => `CASE WHEN jsonb_typeof(payload_json->'${field}')='array' THEN + NOT EXISTS(SELECT 1 FROM jsonb_array_elements(payload_json->'${field}') AS items(value) WHERE jsonb_typeof(value)<>'object' + OR ${keys.map(key=>`coalesce(value->>'${key}','') !~ '^[1-9][0-9]*$'`).join(" OR ")}) ELSE false END` +const mapBindingsValid = (field:string) => `CASE WHEN jsonb_typeof(payload_json->'${field}')='object' THEN + NOT EXISTS(SELECT 1 FROM jsonb_object_keys(payload_json->'${field}') AS keys(key) WHERE key !~ '^[1-9][0-9]*$') ELSE false END` + +/** Shared latest-row source. Scoped calls project in SQL, before JSON is parsed + * by application models; malformed hidden records never reach that boundary. */ +export async function readLatestInternetPathSnapshot(serverIds?: number[]): Promise { + if (serverIds === undefined) return (await pool.query('SELECT id,sampled_at::text AS "sampledAt",payload_json AS "payloadJson" FROM internet_path_snapshots ORDER BY id DESC,sampled_at DESC LIMIT 1')).rows[0] ?? null + if (!serverIds.length) return null + if (serverIds.some(id => !Number.isSafeInteger(id) || id <= 0)) throw new Error("Некорректная область Internet path") + const projection = `jsonb_build_object( + 'bindingShapeValid',(${bindingValid("servers",["id"])} AND ${bindingValid("greTunnels",["serverId"])} AND ${bindingValid("filtersRulesets",["serverId"])} + AND ${bindingValid("speedProbes",["srcServerId","dstServerId"])} AND ${mapBindingsValid("routeLookupByServerId")} AND ${mapBindingsValid("wanRuntimeByHomeId")}), + 'collectionComplete',payload_json->'collectionComplete','sampledAt',payload_json->'sampledAt','internetTarget',payload_json->'internetTarget', + 'servers',${selectedArray("servers", "value->>'id'=ANY($1::text[])")}, + 'greTunnels',${selectedArray("greTunnels", "value->>'serverId'=ANY($1::text[])")}, + 'filtersRulesets',${selectedArray("filtersRulesets", "value->>'serverId'=ANY($1::text[])")}, + 'speedProbes',${selectedArray("speedProbes", "value->>'srcServerId'=ANY($1::text[]) AND value->>'dstServerId'=ANY($1::text[])")}, + 'routeLookupByServerId',${selectedMap("routeLookupByServerId")},'wanRuntimeByHomeId',${selectedMap("wanRuntimeByHomeId")})` + return (await pool.query(`SELECT id,sampled_at::text AS "sampledAt",${projection} AS "payloadJson" + FROM (SELECT id,sampled_at,payload_json FROM internet_path_snapshots ORDER BY id DESC,sampled_at DESC LIMIT 1) latest`, [serverIds.map(String)])).rows[0] ?? null +} diff --git a/backend/src/services/ipsec-ca.ts b/backend/src/services/ipsec-ca.ts index 115f374c..a9b780c2 100644 --- a/backend/src/services/ipsec-ca.ts +++ b/backend/src/services/ipsec-ca.ts @@ -1,15 +1,11 @@ import type { MikrotikClient } from "./mikrotik.js" -import { IPSEC_CA_CERT, IPSEC_SERVER_CERT, clientCertName, ipsecManagedComment } from "./ipsec-config.js" +import { IPSEC_CA_CERT, IPSEC_SERVER_CERT, clientCertName, ipsecManagedComment, ipsecServerCertificateNames } from "./ipsec-config.js" type RosCertRow = Record const SIGN_POLL_TIMEOUT_MS = 90_000 const SIGN_POLL_INTERVAL_MS = 1_000 -function isIp(value: string): boolean { - return /^\d{1,3}(?:\.\d{1,3}){3}$/.test(value.trim()) -} - export async function findCertificate(client: MikrotikClient, name: string): Promise { const certs = await client.getCertificates() return certs.find((c) => String(c.name ?? "").trim() === name) @@ -58,8 +54,9 @@ export async function ensureServerCertificate( client: MikrotikClient, args: { serverEndpoint: string; caCertName: string; daysValid: number }, ): Promise { - const endpoint = args.serverEndpoint.trim() - const san = isIp(endpoint) ? `IP:${endpoint}` : `DNS:${endpoint}` + const names = ipsecServerCertificateNames(args.serverEndpoint) + if (!names) throw new Error("Некорректный endpoint IPsec") + const { endpoint, san } = names const existing = await findCertificate(client, IPSEC_SERVER_CERT) if (!existing) { await client.addCertificate({ diff --git a/backend/src/services/ipsec-certificate-delete.test.ts b/backend/src/services/ipsec-certificate-delete.test.ts new file mode 100644 index 00000000..a37f46fa --- /dev/null +++ b/backend/src/services/ipsec-certificate-delete.test.ts @@ -0,0 +1,22 @@ +import assert from "node:assert/strict" +import { ipsecPeerDeletePaths, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { ipsecCertificateDeleteTarget, projectIpsecCertificateDelete, ipsecCertificateDeleteSchema, IpsecCertificateUsedConflict } from "./ipsec-certificate-delete.js" +const fixture = (): IpsecPeerDeleteState => ({ source: "source", proof: "private", protections: [], catalogs: { ...Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])), "/certificate": [{ ".id": "*a", name: "foreign-leaf", ca: "foreign-ca", issued: "false", "private-key": "true", opaque: "preserve" }, { ".id": "*b", name: "foreign-ca", crl: "true", authority: "true" }] } }) +assert.equal(ipsecCertificateDeleteSchema.parse({ serverId: 1, name: "foreign-leaf" }).force, false) +const lonelyCa = fixture(); lonelyCa.catalogs["/certificate"] = [lonelyCa.catalogs["/certificate"][1]]; assert.equal(ipsecCertificateDeleteTarget(lonelyCa, "foreign-ca").cert[".id"], "*b") +const state = fixture(); assert.equal(ipsecCertificateDeleteTarget(state, "foreign-leaf").cert[".id"], "*a") +const projected = projectIpsecCertificateDelete(state, "*a"); assert.deepEqual(projected.catalogs["/certificate"], [state.catalogs["/certificate"][1]]); assert.equal(state.catalogs["/certificate"].length, 2) +for (const force of [false, true]) assert.throws(() => ipsecCertificateDeleteTarget(state, "foreign-ca", force), /всю цепочку/) +for (const key of ["issued", "revoked"]) { const blocked = fixture(); blocked.catalogs["/certificate"][0][key] = "true"; for (const force of [false, true]) assert.throws(() => ipsecCertificateDeleteTarget(blocked, "foreign-leaf", force), /CRL/) } +for(const crl of ["true","yes"])for(const issued of ["true","yes","unknown"]){const blocked=fixture();blocked.catalogs["/certificate"][1].crl=crl;blocked.catalogs["/certificate"][0].issued=issued;assert.throws(()=>ipsecCertificateDeleteTarget(blocked,"foreign-leaf",true),/CRL/)} +const ambiguousIssuer = fixture(); ambiguousIssuer.catalogs["/certificate"].push({ ".id": "*d", name: "foreign-ca", crl: "false" }); for (const force of [false, true]) assert.throws(() => ipsecCertificateDeleteTarget(ambiguousIssuer, "foreign-leaf", force), /CA-издателя/) +const refs = fixture(); refs.catalogs["/ip/ipsec/peer"] = [{ ".id": "*p", name: "site", certificate: "foreign-leaf" }]; refs.catalogs["/ip/ipsec/identity"] = [{ ".id": "*i", "remote-certificate": "foreign-leaf", comment: "user" }] +assert.throws(() => ipsecCertificateDeleteTarget(refs, "foreign-leaf"), error => error instanceof IpsecCertificateUsedConflict && error.usedBy.length === 2) +assert.equal(ipsecCertificateDeleteTarget(refs, "foreign-leaf", true).usedBy.length, 2) +assert.deepEqual(projectIpsecCertificateDelete(refs, "*a").catalogs["/ip/ipsec/identity"], refs.catalogs["/ip/ipsec/identity"]) +const reflected=structuredClone(refs);reflected.catalogs["/ip/ipsec/identity"][0].secret="NEVER_REFLECTED_PSK";reflected.catalogs["/ip/ipsec/identity"][0].comment="NEVER_REFLECTED_PSK";reflected.catalogs["/ip/ipsec/peer"][0].name="NEVER_REFLECTED_PSK" +assert.throws(()=>ipsecCertificateDeleteTarget(reflected,"foreign-leaf"),error=>error instanceof IpsecCertificateUsedConflict&&!error.message.includes("NEVER_REFLECTED_PSK")&&!JSON.stringify(error.usedBy).includes("NEVER_REFLECTED_PSK")) +assert.throws(() => ipsecCertificateDeleteTarget(state, " foreign-leaf"), /не найден/) +const duplicate = fixture(); duplicate.catalogs["/certificate"].push({ ".id": "*c", name: "foreign-leaf" }); assert.throws(() => ipsecCertificateDeleteTarget(duplicate, "foreign-leaf", true), /Неоднозначное/) +const system = fixture(); system.catalogs["/certificate"][0].builtin = "unknown"; assert.throws(() => ipsecCertificateDeleteTarget(system, "foreign-leaf", true), /Системный/) +console.log("✓ Certificate delete: any foreign cert, force references only, no CA cascade/CRL bypass, exact native target/name, immutable single-row projection") diff --git a/backend/src/services/ipsec-certificate-delete.ts b/backend/src/services/ipsec-certificate-delete.ts new file mode 100644 index 00000000..08c7c3f5 --- /dev/null +++ b/backend/src/services/ipsec-certificate-delete.ts @@ -0,0 +1,72 @@ +import { z } from "zod" +import { ipsecCertDeleteRequestSchema } from "@mmapp/contracts/ipsec" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError } from "../modules/mcp/security.js" +import { readIpsecPeerDeleteState, ipsecPeerDeleteProof, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { identityDisplayName } from "./ipsec-config.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { maskNetworkSecrets, networkSecretValues } from "./network-observed-read.js" +import { MikrotikClient } from "./mikrotik.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureIpsecSnapshot } from "./ipsec-live.js" + +export const ipsecCertificateDeleteSchema = ipsecCertDeleteRequestSchema.extend({ serverId: z.number().int().positive().safe(), force: z.boolean().default(false) }).strict() +export const readIpsecCertificateDeleteState = readIpsecPeerDeleteState +const fail = (code: string, message: string, status = ["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED", "STATE_UNAVAILABLE"].includes(code) ? 502 : 409) => new McpError(code, message, status) +export class IpsecCertificateUsedConflict extends McpError { + constructor(public readonly usedBy: string[], name: string) { super("DEPENDENCY_CONFLICT", `Сертификат «${name}» используется: ${usedBy.join(", ")}.`, 409) } +} +export function ipsecCertificateDeleteTarget(state: IpsecPeerDeleteState, name: string, force = false) { + ipsecCertDeleteRequestSchema.parse({ name }) + const certs = state.catalogs["/certificate"], matches = certs.filter(row => String(row.name ?? "").trim() === name) + if (!matches.length) throw fail("NOT_FOUND", `Сертификат ${name} не найден`, 404) + if (matches.length !== 1) throw fail("STATE_CONFLICT", "Неоднозначное имя сертификата") + const cert = matches[0] + if (!/^\*[a-z0-9]+$/i.test(cert[".id"]) || cert[".id"].length > 100) throw fail("STATE_UNAVAILABLE", "Не подтверждён точный native ID сертификата") + for (const flag of ["dynamic", "builtin", "default"]) if (cert[flag] !== undefined && observedBoolean(cert[flag]) !== false) throw fail("PROTECTED_OBJECT", "Системный или неподтверждённый сертификат нельзя удалить") + if (certs.some(row => (row.ca ?? "").trim() === name)) throw fail("DEPENDENCY_CONFLICT", "CA содержит выданные сертификаты. Его удаление затронет всю цепочку; отдельное удаление CA здесь недоступно") + const issuerName = (cert.ca ?? "").trim(), issuers = issuerName ? certs.filter(row => (row.name ?? "").trim() === issuerName) : [] + if (issuers.length > 1) throw fail("STATE_CONFLICT", "Неоднозначное имя CA-издателя сертификата") + const issuer = issuers[0] + const revoked = cert.revoked && !["false", "no", "0"].includes(cert.revoked) + if (observedBoolean(issuer?.crl) === true && (observedBoolean(cert.issued) !== false || revoked)) throw fail("DEPENDENCY_CONFLICT", "Сертификат выдан CA с CRL и не удаляется отдельно в RouterOS. Удалите только identity, сохранив сертификат. Отзыв выполняется отдельно") + const secrets = networkSecretValues(Object.values(state.catalogs).flat()) + const usedBy = maskNetworkSecrets([ + ...state.catalogs["/ip/ipsec/peer"].filter(row => (row.certificate ?? "").trim() === name).map(row => `peer ${(row.name ?? "").trim()}`), + ...state.catalogs["/ip/ipsec/identity"].filter(row => (row.certificate ?? "").trim() === name || (row["remote-certificate"] ?? "").trim() === name).map(row => `identity ${identityDisplayName(row, certs)}`), + ], secrets) + if (usedBy.length && !force) throw new IpsecCertificateUsedConflict(usedBy, maskNetworkSecrets(name, secrets)) + return { cert, usedBy } +} +export function projectIpsecCertificateDelete(state: IpsecPeerDeleteState, certificateId: string) { + const next = structuredClone(state) + if (!next.catalogs["/certificate"].some(row => row[".id"] === certificateId)) throw fail("STATE_CONFLICT", "Native сертификат не найден") + next.catalogs["/certificate"] = next.catalogs["/certificate"].filter(row => row[".id"] !== certificateId) + return next +} +export async function deleteIpsecCertificateGuarded(server: ServerRow, name: string, force = false, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: typeof captureAndAppendRevision; receiptId?: string; checkpoint?: (value: { certificateId: string; commandCount: number; completedCommands: number; afterProof: string }) => Promise } = {}) { + ipsecCertDeleteRequestSchema.parse({ name }) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "certificate", id: name }, [server.id], async () => { + const before = await readIpsecCertificateDeleteState(server, authorize) + if (expectedProof && before.proof !== expectedProof) throw fail("STATE_CONFLICT", "Сертификат, источник или зависимости изменились после подготовки") + const { cert } = ipsecCertificateDeleteTarget(before, name, force), certificateId = cert[".id"], desired = await ipsecPeerDeleteProof(projectIpsecCertificateDelete(before, certificateId)) + let dispatched = false + try { await MikrotikClient.fromServer(server).delete("/certificate/" + encodeURIComponent(certificateId), 10000, async () => { + const fresh = await readIpsecCertificateDeleteState(server, authorize) + if (fresh.proof !== before.proof || ipsecCertificateDeleteTarget(fresh, name, force).cert[".id"] !== certificateId) throw fail("STATE_CONFLICT", "Сертификат изменился после durable intent; DELETE не отправлен") + await authorize(); dispatched = true + }) } catch (error) { if (!dispatched && error instanceof McpError) throw error; throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход удаления сертификата не подтверждён; повтор запрещён, требуется сверка") } + let after: IpsecPeerDeleteState + try { after = await readIpsecCertificateDeleteState(server, authorize) } catch { throw fail("MUTATION_OUTCOME_UNKNOWN", "DELETE отправлен, но read-back сертификатов недоступен; повтор запрещён") } + if (after.proof !== desired) throw fail("VERIFICATION_FAILED", "Удаление сертификата или сохранность остальной конфигурации не подтверждены; повтор запрещён") + try { await authorize(); await options.checkpoint?.({ certificateId, commandCount: 1, completedCommands: 1, afterProof: after.proof }) } catch { throw fail("MUTATION_OUTCOME_UNKNOWN", "Удаление сертификата проверено, но checkpoint не сохранён; повтор запрещён") } + const historySource = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw fail("STATE_CONFLICT", "Источник истории IPsec изменился") } + const reader = { get: async (path: string) => { await historySource(); if (!Object.hasOwn(after.catalogs, path)) throw fail("STATE_UNAVAILABLE", "Неизвестный каталог истории IPsec"); return structuredClone(after.catalogs[path]) } } as unknown as MikrotikClient + let historyRecorded = false + try { await historySource(); historyRecorded = await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "apply", capture: () => captureIpsecSnapshot(server, reader) }) !== null } catch { /* A history failure must never replay DELETE. */ } + return { ok: true, verified: true, name, certificateId, force, receiptId: options.receiptId ?? null, commandCount: 1, completedCommands: 1, afterProof: after.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", automaticReplay: false, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/ipsec-certificate-update-model.test.ts b/backend/src/services/ipsec-certificate-update-model.test.ts new file mode 100644 index 00000000..7811c9e8 --- /dev/null +++ b/backend/src/services/ipsec-certificate-update-model.test.ts @@ -0,0 +1,66 @@ +import assert from "node:assert/strict" +import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { ipsecCertificateUpdateNativePatch, planIpsecCertificateUpdate } from "./ipsec-certificate-update-model.js" + +const paths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] +const fixture = (): IpsecPeerDeleteState => ({ source: "endpoint-seal", proof: "proof", protections: [{ object_ref: { kind: "certificate", id: "*a" } }], catalogs: { + ...Object.fromEntries(paths.map(path => [path, []])), + "/certificate": [ + { ".id": "*a", name: "leaf", ca: "issuer", trusted: "false", "private-key": "true", opaque: "leaf" }, + { ".id": "*b", name: "issuer", authority: "true", opaque: "issuer" }, + { ".id": "*c", name: "child", ca: "leaf", opaque: "child" }, + { ".id": "*d", name: "id-child", ca: "*a" }, + ], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "site", certificate: "leaf", "remote-certificate": "*a", secret: "opaque-secret", comment: "leaf" }, { ".id": "*j", peer: "*p", "remote-certificate": "child" }], + "/ip/ipsec/peer": [{ ".id": "*p", name: "site", opaque: "leaf" }, { ".id": "*q", name: "direct", certificate: "leaf", "remote-certificate": "*a" }], + "/ip/ipsec/profile": [{ ".id": "*f", name: "profile", opaque: "leaf", certificate: "leaf" }], +} }) +const original = fixture(), saved = structuredClone(original) +const plan = planIpsecCertificateUpdate(original, "*a", { name: "renamed", trusted: true }) +assert.deepEqual(original, saved) +assert.deepEqual(plan.body, { name: "renamed", trusted: "true" }) +assert.equal(plan.commandCount, 1) +assert.deepEqual(plan.target, { kind: "certificate", id: "*a" }) +assert.deepEqual(new Set(plan.companions.map(row => `${row.kind}:${row.id}`)), new Set(["certificate:*b", "certificate:*c", "certificate:*d", "ipsec_identity:*i", "ipsec_identity:*j", "ipsec_peer:*p", "ipsec_peer:*q"])) +const expected = structuredClone(saved) +Object.assign(expected.catalogs["/certificate"][0], { name: "renamed", trusted: "true" }) +expected.catalogs["/certificate"][2].ca = "renamed" +expected.catalogs["/ip/ipsec/identity"][0].certificate = "renamed" +expected.catalogs["/ip/ipsec/peer"][1].certificate = "renamed" +assert.deepEqual(plan.projected, expected) +assert.equal(plan.projected.catalogs["/ip/ipsec/identity"][0]["remote-certificate"], "*a") +assert.equal(plan.projected.catalogs["/ip/ipsec/profile"][0].certificate, "leaf") +assert.equal(planIpsecCertificateUpdate(original, "*a", {}).commandCount, 0) +assert.deepEqual(ipsecCertificateUpdateNativePatch({ trusted: false, name: " trimmed " }), { trusted: "false", name: "trimmed" }) +for (const raw of [null, [], { ca: "issuer" }, { trusted: "true" }, { trusted: 1 }, { name: "" }, { name: "a\nb" }, { name: 1 }, { ".id": "*a" }]) assert.throws(() => ipsecCertificateUpdateNativePatch(raw), { code: "INVALID_INPUT" }) +for (const id of ["leaf", "*a/b", " *a", "", "*" + "a".repeat(101)]) assert.throws(() => planIpsecCertificateUpdate(original, id, {}), { code: "INVALID_INPUT" }) +assert.throws(() => planIpsecCertificateUpdate(original, "*z", {}), { code: "NOT_FOUND" }) +for (const flag of ["dynamic", "builtin", "default"]) for (const value of ["true", "yes", "unknown", "0"]) { + const state = fixture(); state.catalogs["/certificate"][0][flag] = value + assert.throws(() => planIpsecCertificateUpdate(state, "*a", { trusted: true }), { code: "PROTECTED_OBJECT" }) +} +for (const change of [ + (state: IpsecPeerDeleteState) => state.catalogs["/certificate"].push({ ".id": "*z", name: "leaf" }), + (state: IpsecPeerDeleteState) => state.catalogs["/certificate"].push({ ".id": "*z", name: "issuer" }), + (state: IpsecPeerDeleteState) => { state.catalogs["/certificate"][0].ca = "missing" }, + (state: IpsecPeerDeleteState) => state.catalogs["/ip/ipsec/peer"].push({ ".id": "*z", name: "site" }), +]) { const state = fixture(); change(state); assert.throws(() => planIpsecCertificateUpdate(state, "*a", { trusted: true }), { code: "STATE_CONFLICT" }) } +for (const path of paths) { + const missing = fixture(); delete missing.catalogs[path] + assert.throws(() => planIpsecCertificateUpdate(missing, "*a", {}), { code: "STATE_UNAVAILABLE" }) + const duplicate = fixture(); duplicate.catalogs[path] = [{ ".id": "*x" }, { ".id": "*x" }] + assert.throws(() => planIpsecCertificateUpdate(duplicate, "*a", {}), { code: "STATE_UNAVAILABLE" }) +} +for (const reference of ["leaf,child", "leaf;child", "(leaf)", " leaf "]) { + const state = fixture(); state.catalogs["/ip/ipsec/identity"][0].certificate = reference + assert.throws(() => planIpsecCertificateUpdate(state, "*a", { name: "renamed" }), { code: "STATE_UNAVAILABLE" }) +} +for (const reference of ["renamed", "other,renamed"]) { + const state = fixture(); state.catalogs["/ip/ipsec/peer"].push({ ".id": "*z", certificate: reference }) + assert.throws(() => planIpsecCertificateUpdate(state, "*a", { name: "renamed" }), { code: "STATE_CONFLICT" }) +} +assert.throws(() => planIpsecCertificateUpdate(original, "*a", { name: "issuer" }), { code: "STATE_CONFLICT" }) +assert.throws(() => planIpsecCertificateUpdate(original, "*a", { name: "*b" }), { code: "STATE_CONFLICT" }) +const opaque = fixture(); opaque.catalogs["/ip/ipsec/identity"].push({ ".id": "*u", certificate: "opaque-value", note: "leaf" }) +assert.equal(planIpsecCertificateUpdate(opaque, "*a", { name: "renamed" }).projected.catalogs["/ip/ipsec/identity"][2].certificate, "opaque-value") +console.log("✓ Certificate update model: exact native target, strict typed fields, complete immutable state, chain/identity/peer closure, guarded expected rename references, stable ID aliases and no late binding") diff --git a/backend/src/services/ipsec-certificate-update-model.ts b/backend/src/services/ipsec-certificate-update-model.ts new file mode 100644 index 00000000..a8b91aaa --- /dev/null +++ b/backend/src/services/ipsec-certificate-update-model.ts @@ -0,0 +1,107 @@ +import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { McpError } from "../modules/mcp/security.js" +import { observedBoolean } from "./ospf-observed-values.js" + +type Row = Record +export interface CertificateUpdateObject { kind: "certificate" | "ipsec_identity" | "ipsec_peer"; id: string } +const paths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] +const fail = (code: string, message: string) => new McpError(code, message, code === "INVALID_INPUT" ? 400 : code === "NOT_FOUND" ? 404 : code === "STATE_UNAVAILABLE" ? 502 : 409) +const exactId = (id: unknown): id is string => typeof id === "string" && /^\*[a-z0-9]+$/i.test(id) && id.length <= 100 + +/** Input is the generic action's typed fields, never an arbitrary RouterOS patch. */ +export function ipsecCertificateUpdateNativePatch(raw: unknown): Record { + if (!raw || typeof raw !== "object" || Array.isArray(raw) || ![Object.prototype, null].includes(Object.getPrototypeOf(raw))) throw fail("INVALID_INPUT", "Некорректные поля сертификата") + const body: Record = {} + for (const [key, value] of Object.entries(raw)) { + if (key === "name") { + if (typeof value !== "string" || !value.trim() || value.trim().length > 100 || /[\x00-\x1f\x7f]/.test(value)) throw fail("INVALID_INPUT", "Некорректное имя сертификата") + body.name = value.trim() + } else if (key === "trusted") { + if (typeof value !== "boolean") throw fail("INVALID_INPUT", "trusted должен быть boolean") + body.trusted = String(value) + } else throw fail("INVALID_INPUT", "Неизвестное поле сертификата") + } + return body +} + +function validateState(state: IpsecPeerDeleteState) { + if (!state || typeof state.source !== "string" || !state.source || !Array.isArray(state.protections) || !state.catalogs) throw fail("STATE_UNAVAILABLE", "Неполное состояние сертификатов") + for (const path of paths) { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row) || !exactId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(rows.map(row => row[".id"])).size !== rows.length) throw fail("STATE_UNAVAILABLE", "Неполный или неоднозначный native-каталог") + } +} + +/** No native/SQL calls. All opaque fields, catalog order, source and protections survive. */ +export function planIpsecCertificateUpdate(state: IpsecPeerDeleteState, id: string, raw: unknown) { + const body = ipsecCertificateUpdateNativePatch(raw) + if (!exactId(id)) throw fail("INVALID_INPUT", "Требуется точный native ID сертификата") + validateState(state) + const certs = state.catalogs["/certificate"], cert = certs.find(row => row[".id"] === id) + const certificateNames = certs.map(row => row.name).filter(name => name !== undefined) + if (new Set(certificateNames).size !== certificateNames.length) throw fail("STATE_CONFLICT", "Неоднозначное имя сертификата") + if (!cert) throw fail("NOT_FOUND", "Сертификат не найден") + for (const flag of ["dynamic", "builtin", "default"]) if (cert[flag] !== undefined && observedBoolean(cert[flag]) !== false) throw fail("PROTECTED_OBJECT", "Системный или неподтверждённый сертификат нельзя изменять") + const oldName = cert.name + const byName = (name: string): Row | undefined => { + const matches = certs.filter(row => row.name === name) + if (matches.length > 1) throw fail("STATE_CONFLICT", "Неоднозначное имя сертификата") + return matches[0] + } + if (!oldName || oldName !== oldName.trim() || byName(oldName) !== cert) throw fail("STATE_CONFLICT", "Не подтверждено точное имя сертификата") + const rename = body.name !== undefined && body.name !== oldName + if (rename && byName(body.name)) throw fail("STATE_CONFLICT", "Имя сертификата уже используется") + const byReference = (reference: string) => { + const matches = certs.filter(row => row.name === reference || row[".id"] === reference) + if (matches.length > 1) throw fail("STATE_CONFLICT", "Неоднозначная ссылка сертификата") + return matches[0] + } + if (byReference(oldName) !== cert || (rename && byReference(body.name))) throw fail("STATE_CONFLICT", "Неоднозначное имя или native ID сертификата") + const referenceRows: Array<{ row: Row; fields: string[] }> = [ + ...certs.map(row => ({ row, fields: ["ca"] })), + ...["/ip/ipsec/identity", "/ip/ipsec/peer"].flatMap(path => state.catalogs[path].map(row => ({ row, fields: ["certificate", "remote-certificate"] }))), + ] + for (const { row, fields } of referenceRows) for (const field of fields) { + const reference = row[field] + if (!reference) continue + if (reference !== oldName && reference !== id && !byReference(reference) && (reference.includes(oldName) || reference.includes(id))) throw fail("STATE_UNAVAILABLE", "Не подтверждён scalar-контракт ссылки сертификата") + if (rename && reference.includes(body.name)) throw fail("STATE_CONFLICT", "Новое имя сертификата уже встречается в ссылках") + } + const target: CertificateUpdateObject = { kind: "certificate", id } + const companions: CertificateUpdateObject[] = [], seen = new Set([`certificate:${id}`]) + const add = (kind: CertificateUpdateObject["kind"], row: Row) => { const key = `${kind}:${row[".id"]}`; if (!seen.has(key)) { seen.add(key); companions.push({ kind, id: row[".id"] }) } } + // Traverse the entire issuer/child component; trust and rename can affect a chain. + const queue = [cert] + for (let index = 0; index < queue.length; index++) { + const current = queue[index], issuer = current.ca ? byReference(current.ca) : undefined + if (current.ca && !issuer) throw fail("STATE_CONFLICT", "Не подтверждён CA-издатель сертификата") + for (const related of [...(issuer ? [issuer] : []), ...certs.filter(row => row.ca === current.name || row.ca === current[".id"])]) { + if (queue.some(row => row[".id"] === related[".id"])) continue + if (!related.name || byName(related.name) !== related) throw fail("STATE_CONFLICT", "Неоднозначная цепочка сертификатов") + queue.push(related); add("certificate", related) + } + } + const chainNames = new Set(queue.flatMap(row => [row.name, row[".id"]])) + const identities = state.catalogs["/ip/ipsec/identity"].filter(row => chainNames.has(row.certificate) || chainNames.has(row["remote-certificate"])) + for (const identity of identities) { + add("ipsec_identity", identity) + if (identity.peer) { + const peers = state.catalogs["/ip/ipsec/peer"].filter(row => row.name === identity.peer || row[".id"] === identity.peer) + if (peers.length !== 1) throw fail("STATE_CONFLICT", "Не подтверждён peer зависимой identity") + add("ipsec_peer", peers[0]) + } + } + for (const peer of state.catalogs["/ip/ipsec/peer"]) if (chainNames.has(peer.certificate) || chainNames.has(peer["remote-certificate"])) add("ipsec_peer", peer) + const projected = structuredClone(state) + Object.assign(projected.catalogs["/certificate"].find(row => row[".id"] === id)!, body) + // Expected native rename effects, never companion writes. Real RouterOS acceptance + // remains unproven until a live fixture; whole-state read-back must match exactly. + // Only explicitly modeled scalar reference fields participate. ID aliases remain stable. + if (rename) { + for (const row of projected.catalogs["/certificate"]) if (row.ca === oldName) row.ca = body.name + for (const path of ["/ip/ipsec/identity", "/ip/ipsec/peer"]) for (const row of projected.catalogs[path]) { + for (const field of ["certificate", "remote-certificate"]) if (row[field] === oldName) row[field] = body.name + } + } + return { body, projected, target, companions, rename, commandCount: Object.keys(body).length ? 1 : 0 } +} diff --git a/backend/src/services/ipsec-config.ts b/backend/src/services/ipsec-config.ts index 1b2b5418..a6abc085 100644 --- a/backend/src/services/ipsec-config.ts +++ b/backend/src/services/ipsec-config.ts @@ -1,7 +1,23 @@ import { randomUUID } from "node:crypto" +import { isIP } from "node:net" +import { domainToASCII } from "node:url" import { PRODUCT_NAME } from "../managed-markers.js" import { isCaCertificate } from "./certificate-parse.js" +export function ipsecServerCertificateNames(raw: string): { endpoint: string; san: string } | null { + const host = raw.trim() + if (host.includes("%")) return null + const family = isIP(host) + if (family) { + const endpoint = family === 6 ? new URL(`http://[${host}]`).hostname.slice(1, -1) : host + return { endpoint, san: `IP:${endpoint}` } + } + if (!/^[\p{L}\p{M}\p{N}.-]+$/u.test(host)) return null + const endpoint = domainToASCII(host).replace(/\.$/, "") + if (/^[\d.]+$/.test(endpoint) || !/^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i.test(endpoint)) return null + return { endpoint, san: `DNS:${endpoint}` } +} + // ── managed-маркеры (как у filters/recursive, см. managed-markers.ts) ───────── export const IPSEC_MARKER = `${PRODUCT_NAME}:ipsec` diff --git a/backend/src/services/ipsec-existing-certificate-export-artifact.test.ts b/backend/src/services/ipsec-existing-certificate-export-artifact.test.ts new file mode 100644 index 00000000..4a01d164 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-artifact.test.ts @@ -0,0 +1,192 @@ +import assert from "node:assert/strict" +import { mkdtemp, readdir, unlink, rmdir } from "node:fs/promises" +import os from "node:os" +import path from "node:path" +import type { PoolClient } from "pg" +import type { AuthUser } from "../lib/permissions.js" +import type { IpsecExistingCertificateExportAuthority as Authority } from "./ipsec-existing-certificate-export-model.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED } from "./ipsec-existing-certificate-export-model.js" +import { createIpsecExistingCertificateExportArtifactStore, createIpsecExistingCertificateExportArtifact, + verifyIpsecExistingCertificateExportArtifact, humanIpsecExistingCertificateExportMetadata, readHumanIpsecExistingCertificateExportArtifact, + type IpsecExistingCertificateExportArtifactStoreDependencies as Dependencies } from "./ipsec-existing-certificate-export-artifact.js" +import { fingerprint, privateSecretFingerprint, McpError } from "../modules/mcp/security.js" +import { pool, closePool } from "../db/index.js" + +// Synthetic bytes and real crypto in an isolated temporary key directory. All +// SQL/access/source/admission are injected; no RouterOS/PKI/export is executed. +const directory = await mkdtemp(path.join(os.tmpdir(), "mm-ipsec-artifact-test-")) +const originalState = process.env.MCP_STATE_DIR +process.env.MCP_STATE_DIR = directory +const id = "fa58fa40-4bcb-4616-9316-cb69463b3150", connectionId = "fa58fa40-4bcb-4616-9316-cb69463b3151" +const actor: AuthUser = { id: "artifact-owner", email: "synthetic@example.invalid", name: "Synthetic owner", apps: ["mm"], permissions: ["mm:network:write"], expiresAt: Date.now() + 3600000, issuedAt: Date.now() - 1000 } +const input = { serverId: 207, receiptId: id, bindingCiphertext: "[redacted]" } +const authority: Authority = { principal: { connectionId, credentialId: id, credentialExpiresAt: new Date(Date.now() + 3600000).toISOString() }, ownerId: actor.id, + ownerProof: "a".repeat(64), policyProof: "b".repeat(64), generation: "synthetic-generation", request: { serverId: 207, + selector: { kind: "certificate", name: "synthetic-certificate" }, privateKeyExport: true, caPrivateKeyExport: false, passphrase: { kind: "generate" } }, + receiptId: id, certificateId: "*c1", certificateName: "synthetic-certificate", role: "client", sourceProof: "c".repeat(64), expectedProof: "d".repeat(64), + referenceProof: "e".repeat(64), requestProof: "f".repeat(64), inputProof: fingerprint(input), proofRevision: 1 } +const bundle = { user: "synthetic-user", filename: "synthetic.p12", mime: "application/x-pkcs12", contentB64: "SYNTHETIC_PRIVATE_BYTES", passphrase: "SYNTHETIC_PASSPHRASE" } +const serialized = JSON.stringify(bundle), encoded = Buffer.from(serialized).toString("base64") +type Row = Record +function fixture() { + let stored: Row | undefined, pending: Row | undefined + let result: Row | undefined + let checkpointRows = 1, permit = true, denied = false, claimsDenied = false, sourceProof = authority.expectedProof + let sourceCalls = 0, payloadReads = 0, queries = 0, admissions = 0, failAfterInsert = false + let usage = { files: "0", own_bytes: "0", total_bytes: "0" } + let claimHook: (() => void) | undefined, sourceHook: (() => void) | undefined + const query: Dependencies["query"] = async (sql, values = []) => { + queries++ + if (sql.includes("SELECT id,connection_id,filename")) { + assert(!sql.includes("SELECT ciphertext")); assert.match(sql, /octet_length\(access_scope::text\)<=32768/) + if (!stored || values[0] !== stored.id || (stored.expires_at as Date).getTime() <= Date.now()) return { rows: [] } + const { ciphertext, ...meta } = stored + return { rows: [{ ...structuredClone(meta), encrypted_bytes: Buffer.byteLength(String(ciphertext)) }] } + } + if (sql.includes("SELECT s.intended_state,s.result")) { + assert.match(sql, /p.operation_id='network.ipsec.existing-certificates.export'/); assert.match(sql, /p.approved_by IS NOT NULL/) + assert.match(sql, /s.state='verified'/); assert.match(sql, /LIMIT 2/) + assert.deepEqual(values, [connectionId, stored?.id]) + return { rows: Array.from({ length: checkpointRows }, () => ({ intended_state: { input }, result: structuredClone(result) })) } + } + if (sql.startsWith("SELECT ciphertext")) { + payloadReads++; assert.match(sql, /octet_length\(ciphertext\)<=2097152/) + return { rows: stored && values[0] === stored.id && values[1] === (stored.access_scope as Row).proof ? [{ ciphertext: stored.ciphertext }] : [] } + } + throw Error("Unexpected synthetic query") + } + const deps: Dependencies = { query, transaction: async work => { + const client = { query: async (sql: string, values: unknown[] = []) => { + if (sql.includes("FROM mcp_connections")) { assert.match(sql, /FOR UPDATE/); return { rows: [{ id: connectionId }] } } + if (sql.includes("pg_advisory_xact_lock") || sql.startsWith("DELETE FROM mcp_artifacts")) return { rows: [] } + if (sql.startsWith("SELECT count(*)")) return { rows: [usage] } + assert.match(sql, /^INSERT INTO mcp_artifacts/) + pending = { id: values[0], connection_id: values[1], filename: values[2], media_type: values[3], ciphertext: values[4], access_scope: JSON.parse(String(values[5])), expires_at: values[6] } + if (failAfterInsert) throw Error("synthetic transaction failure") + return { rows: [] } + } } as Pick + try { const value = await work(client); stored = pending; pending = undefined; return value } + catch (error) { pending = undefined; throw error } + }, authorizeExport: () => { admissions++; if (!permit) throw new McpError("APPROVAL_REQUIRED", "Synthetic admission closed", 409) }, + access: async a => { assert.equal(fingerprint(a), fingerprint(authority)); if (denied) throw new McpError("PERMISSION_DENIED", "Synthetic access revoked") }, + source: async (_a, authorize) => { sourceCalls++; await authorize(); sourceHook?.(); return { proof: sourceProof } }, + checkClaims: async () => { claimHook?.(); if (claimsDenied) throw new McpError("PERMISSION_DENIED", "Synthetic portal claims revoked") } } + const store = createIpsecExistingCertificateExportArtifactStore(deps) + return { store, deps, row: () => { assert(stored); return stored }, result: () => { assert(result); return result }, + stats: () => ({ queries, admissions, sourceCalls, payloadReads }), setCheckpointRows(n: number) { checkpointRows = n }, + revoke() { denied = true }, revokeClaims() { claimsDenied = true }, closeAdmission() { permit = false }, + changeSource() { sourceProof = "0".repeat(64) }, setSourceHook(hook: () => void) { sourceHook = hook }, setClaimsHook(hook: () => void) { claimHook = hook }, + fullQuota() { usage = { files: "8", own_bytes: "0", total_bytes: "0" } }, failTransaction() { failAfterInsert = true }, + async create() { const receipt = await store.create(authority, bundle, authority.expectedProof, async () => {}); result = { ...receipt, ok: true, verified: true, + receiptId: authority.receiptId, certificateId: authority.certificateId, serverId: 207, commandCount: 2, completedCommands: 2, confirmedWrites: 2, + afterProof: authority.expectedProof, automaticReplay: false, forwardingVerified: false }; return receipt }, + async resign() { const row = this.row(), scope = row.access_scope as Row, { proof: _proof, ...unsigned } = scope + scope.proof = await privateSecretFingerprint(fingerprint({ id: row.id, connection_id: row.connection_id, filename: row.filename, media_type: row.media_type, + expires_at: (row.expires_at as Date).toISOString(), scope: unsigned }), "ipsec-existing-certificate-bundle-artifact-v1") } + } +} +async function trackBuffers(work: (tracked: { created?: Buffer; decoded?: Buffer }) => Promise) { + const tracked: { created?: Buffer; decoded?: Buffer } = {}, originalFrom = Buffer.from + Buffer.from = ((...args: unknown[]) => { + const output = (originalFrom as (...values: unknown[]) => Buffer)(...args) + if (args[0] === serialized && args[1] === undefined) tracked.created = output + if (args[0] === encoded && args[1] === "base64") tracked.decoded = output + return output + }) as typeof Buffer.from + try { await work(tracked) } finally { Buffer.from = originalFrom } +} + +try { + const originalQuery = pool.query, originalConnect = pool.connect + let productionSql = 0 + pool.query = (async () => { productionSql++; return { rows: [] } }) as unknown as typeof pool.query + pool.connect = (() => { productionSql++; throw Error("Production connection forbidden") }) as typeof pool.connect + try { + for (const operation of [() => createIpsecExistingCertificateExportArtifact(authority, bundle, authority.expectedProof, async () => {}), + () => verifyIpsecExistingCertificateExportArtifact(id, authority, authority.expectedProof), + () => humanIpsecExistingCertificateExportMetadata(id, actor), () => readHumanIpsecExistingCertificateExportArtifact(id, actor)]) { + await assert.rejects(operation, error => error instanceof McpError && error.code !== "APPROVAL_REQUIRED") + } + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true); assert.ok(productionSql >= 4) + assert.deepEqual(await readdir(directory), []) + } finally { pool.query = originalQuery; pool.connect = originalConnect } + { + const f = fixture(); assert.equal(f.stats().queries, 0); assert.equal(f.stats().admissions, 0) + await trackBuffers(async tracked => { + const receipt = await f.create(); assert(tracked.created); assert(tracked.created.every(value => value === 0)) + assert.equal(receipt.humanDownloadOnly, true); assert.equal(receipt.ipsecContentReturned, false) + assert.doesNotMatch(JSON.stringify(receipt), /SYNTHETIC_|contentB64|passphrase/) + }) + assert(!String(f.row().ciphertext).includes("SYNTHETIC_")) + assert.equal(await f.store.verify(String(f.row().id), authority, authority.expectedProof), true) + const reads = f.stats().payloadReads + const scope = await f.store.metadata(String(f.row().id), actor) + assert.equal(scope.kind, "ipsec-existing-certificate-bundle"); assert.equal(f.stats().payloadReads, reads) + await trackBuffers(async tracked => { + const delivered = await f.store.read(String(f.row().id), actor) + assert.equal(delivered.content, tracked.decoded); assert.equal(delivered.content.toString(), serialized) + assert.equal(delivered.mediaType, "application/json"); delivered.content.fill(0) + }) + for (const badActor of [{ ...actor, id: "foreign" }, { ...actor, permissions: ["mm:network:read"] }, { ...actor, apps: [] }, + { ...actor, expiresAt: Date.now() - 1 }, { connectionId, credentialId: id } as unknown as AuthUser]) { + const before = f.stats().payloadReads + await assert.rejects(f.store.metadata(String(f.row().id), badActor)); await assert.rejects(f.store.read(String(f.row().id), badActor)) + assert.equal(f.stats().payloadReads, before) + } + } + for (const mutate of [(f: ReturnType) => { f.row().filename = "changed.json" }, + (f: ReturnType) => { f.row().connection_id = id }, + (f: ReturnType) => { (f.row().access_scope as Row).proof = "0".repeat(64) }, + (f: ReturnType) => { (f.row().access_scope as Row).kind = "ipsec-user-bundle" }, + (f: ReturnType) => { f.row().expires_at = new Date(Date.now() - 1) }, + (f: ReturnType) => { f.revoke() }, (f: ReturnType) => { f.revokeClaims() }, + (f: ReturnType) => { f.setCheckpointRows(0) }, (f: ReturnType) => { f.setCheckpointRows(2) }, + (f: ReturnType) => { f.result().humanDownloadOnly = false }, + (f: ReturnType) => { f.result().serverId = 208 }, + (f: ReturnType) => { f.result().downloadPath = "https://unsafe.invalid" }]) { + const f = fixture(); await f.create(); mutate(f) + await assert.rejects(f.store.metadata(String(f.row().id), actor)); assert.equal(f.stats().payloadReads, 0) + } + for (const failure of ["checksum", "source", "claims", "aborted"] as const) { + const f = fixture(); await f.create() + if (failure === "checksum") { (f.row().access_scope as Row).sha256 = "0".repeat(64); f.result().sha256 = "0".repeat(64); await f.resign() } + const abort = new AbortController() + await trackBuffers(async tracked => { + if (failure === "source") f.setSourceHook(() => { if (tracked.decoded) f.changeSource() }) + if (failure === "claims") f.setClaimsHook(() => { if (tracked.decoded) f.revokeClaims() }) + if (failure === "aborted") f.setSourceHook(() => { if (tracked.decoded) abort.abort() }) + await assert.rejects(f.store.read(String(f.row().id), actor, undefined, abort.signal)) + assert(tracked.decoded, `${failure}: read must reach decoding`); assert(tracked.decoded.every(value => value === 0), `${failure}: decoded buffer must be erased`) + }) + } + for (const failure of ["quota", "transaction", "source"] as const) { + const f = fixture() + if (failure === "quota") f.fullQuota() + if (failure === "transaction") f.failTransaction() + if (failure === "source") f.changeSource() + await trackBuffers(async tracked => { await assert.rejects(f.create()); assert(tracked.created); assert(tracked.created.every(value => value === 0)) }) + assert.throws(() => f.row()) + } + { + const f = fixture(); await f.create(); f.row().ciphertext = String(f.row().ciphertext) + "changed" + assert.equal(await f.store.verify(String(f.row().id), authority, authority.expectedProof), false) + await trackBuffers(async tracked => { await assert.rejects(f.store.read(String(f.row().id), actor)); assert.equal(tracked.decoded, undefined) }) + } + { + const f = fixture() + await assert.rejects(f.store.create(authority, { ...bundle, contentB64: "x".repeat(1048576) }, authority.expectedProof, async () => {}), error => error instanceof McpError && error.code === "EXPORT_LIMIT") + assert.throws(() => f.row()) + } + { + const f = fixture(); await f.create(); f.closeAdmission(); const before = f.stats().queries + for (const operation of [() => f.create(), () => f.store.verify(String(f.row().id), authority, authority.expectedProof), + () => f.store.metadata(String(f.row().id), actor), () => f.store.read(String(f.row().id), actor)]) await assert.rejects(operation) + assert.equal(f.stats().queries, before) + } + console.log("Existing IPsec artifact store: active default rejects missing SQL authority before crypto; injected admission, real crypto/HMAC, metadata-only human policy, exact checkpoint/source/TTL/budget, rollback and plaintext buffer lifetime passed") +} finally { + if (originalState === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = originalState + await unlink(path.join(directory, "encryption-key")).catch(() => {}) + await rmdir(directory) + await closePool() +} diff --git a/backend/src/services/ipsec-existing-certificate-export-artifact.ts b/backend/src/services/ipsec-existing-certificate-export-artifact.ts new file mode 100644 index 00000000..05c7c3fb --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-artifact.ts @@ -0,0 +1,157 @@ +import { createHash, randomUUID, timingSafeEqual } from "node:crypto" +import { z } from "zod" +import type { PoolClient } from "pg" +import type { IpsecCertBundle } from "@mmapp/contracts/ipsec" +import { pool } from "../db/index.js" +import { hasPermission, type AuthUser } from "../lib/permissions.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { ipsecPeerDeleteSourceProof } from "./ipsec-peer-delete.js" +import { readIpsecExistingCertificateExportState, assertIpsecExistingCertificateExportAccess } from "./ipsec-existing-certificate-export.js" +import { assertLogArtifactBudget } from "../modules/mcp/log-artifacts.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { connectionWrite } from "../modules/mcp/store.js" +import { decryptSecret, encryptSecret, fingerprint, privateSecretFingerprint, McpError } from "../modules/mcp/security.js" + +const digest = z.string().regex(/^[a-f0-9]{64}$/) +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, ipsecExistingCertificateExportAuthoritySchema, type IpsecExistingCertificateExportAuthority } from "./ipsec-existing-certificate-export-model.js" +function activation() { if (!IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED) throw new McpError("APPROVAL_REQUIRED", "Выдача существующих IPsec сертификатов/CA через портал выключена", 409) } +export const ipsecExistingCertificateExportArtifactScopeSchema = z.object({ version: z.literal(1), kind: z.literal("ipsec-existing-certificate-bundle"), authority: ipsecExistingCertificateExportAuthoritySchema, afterProof: digest, bytes: z.number().int().positive().max(1048576), sha256: digest, encryptedSha256: digest, proof: digest }).strict() +const scopeSchema = ipsecExistingCertificateExportArtifactScopeSchema +type Header = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date } +type Meta = Header & { access_scope: z.infer; encrypted_bytes: number } +const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "ipsec-existing-certificate-bundle-artifact-v1") +const signed = (header: Header, scope: Omit) => ({ id: header.id, connection_id: header.connection_id, filename: header.filename, media_type: header.media_type, expires_at: header.expires_at.toISOString(), scope }) +const fail = (message: string, code = "ARTIFACT_INVALID") => new McpError(code, message, 409) +export const ipsecExistingCertificateExportArtifactReceipt = (header: Pick, scope: Pick) => ({ artifactId: header.id, downloadPath: `/api/mcp/artifacts/${header.id}`, expiresInSeconds: 900, bytes: scope.bytes, sha256: scope.sha256, humanDownloadOnly: true as const, ipsecContentReturned: false as const }) + +type ArtifactClient = Pick +export interface IpsecExistingCertificateExportArtifactStoreDependencies { + query(sql: string, values?: unknown[]): Promise<{ rows: Record[] }> + transaction(work: (client: ArtifactClient) => Promise): Promise + access(authority: IpsecExistingCertificateExportAuthority, client?: ArtifactClient): Promise + source(authority: IpsecExistingCertificateExportAuthority, authorize: () => Promise): Promise<{ proof: string }> + checkClaims(actor: AuthUser): Promise + /** Mandatory trusted admission. Production always uses the closed gate. */ + authorizeExport(): void | Promise +} + +async function access(authority: IpsecExistingCertificateExportAuthority, client: Pick = pool) { await assertIpsecExistingCertificateExportAccess(authority, client) } +async function source(authority: IpsecExistingCertificateExportAuthority, authorize: () => Promise = async () => {}) { + const check = async () => { await authorize(); await access(authority) } + await check() + const server = await getServerRowById(authority.request.serverId) + if (!server || await ipsecPeerDeleteSourceProof(server) !== authority.sourceProof) throw fail("Источник пакета IPsec изменился", "STATE_CONFLICT") + return readIpsecExistingCertificateExportState(server, check) +} +/** Constructing a store performs no I/O and grants no admission. Crypto, its + * purpose-separated HMAC domain, storage budgets and human policy stay fixed. */ +export function createIpsecExistingCertificateExportArtifactStore(deps: IpsecExistingCertificateExportArtifactStoreDependencies) { + async function create(authority: IpsecExistingCertificateExportAuthority, bundle: IpsecCertBundle, afterProof: string, authorize: () => Promise) { + await deps.authorizeExport() + ipsecExistingCertificateExportAuthoritySchema.parse(authority) + if (afterProof !== authority.expectedProof) throw fail("Proof пакета IPsec не совпал с допуском", "STATE_CONFLICT") + await authorize(); await deps.access(authority) + const content = Buffer.from(JSON.stringify(bundle)) + try { + if (!content.length || content.length > 1048576) throw fail("Приватный пакет IPsec превышает предел", "EXPORT_LIMIT") + const ciphertext = await encryptSecret(content.toString("base64")) + if (!content.length || content.length > 1048576 || Buffer.byteLength(ciphertext) > 2097152) throw fail("Приватный пакет IPsec превышает предел", "EXPORT_LIMIT") + if ((await deps.source(authority, authorize)).proof !== afterProof) throw fail("IPsec изменился перед сохранением пакета", "STATE_CONFLICT") + const header: Header = { id: randomUUID(), connection_id: authority.principal.connectionId, filename: "ipsec-existing-certificate-bundle.json", media_type: "application/json", expires_at: new Date(Date.now() + 900000) } + const unsigned = { version: 1 as const, kind: "ipsec-existing-certificate-bundle" as const, authority, afterProof, bytes: content.length, sha256: hash(content), encryptedSha256: hash(ciphertext) } + const scope = scopeSchema.parse({ ...unsigned, proof: await sign(signed(header, unsigned)) }) + await deps.transaction(async client => { + await authorize() + await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [header.connection_id]); await deps.access(authority, client) + await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))") + await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()") + const usage = (await client.query("SELECT count(*) FILTER(WHERE connection_id=$1)::text AS files,COALESCE(sum(octet_length(ciphertext)) FILTER(WHERE connection_id=$1),0)::text AS own_bytes,COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts", [header.connection_id])).rows[0] + assertLogArtifactBudget(BigInt(usage.files), BigInt(usage.own_bytes), BigInt(usage.total_bytes), Buffer.byteLength(ciphertext)) + await authorize() + await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)", [header.id, header.connection_id, header.filename, header.media_type, ciphertext, JSON.stringify(scope), header.expires_at]) + await authorize() + }) + await authorize(); await deps.access(authority) + return ipsecExistingCertificateExportArtifactReceipt(header, scope) + } finally { content.fill(0) } // JS strings passed to crypto cannot be erased here. + } + async function meta(id: string): Promise { + if (!z.string().uuid().safeParse(id).success) throw fail("Некорректный ID пакета IPsec") + const row = (await deps.query("SELECT id,connection_id,filename,media_type,expires_at,CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope,octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND expires_at>now() AND octet_length(filename)<=1024 AND octet_length(media_type)<=200", [id])).rows[0] as Meta | undefined + if (!row) throw new McpError("NOT_FOUND", "Пакет IPsec отсутствует или срок истёк", 404) + const scope = scopeSchema.safeParse(row.access_scope) + if (!scope.success || row.id !== id || row.filename !== "ipsec-existing-certificate-bundle.json" || row.media_type !== "application/json" || !(row.expires_at instanceof Date) || !Number.isFinite(row.expires_at.getTime()) || row.expires_at.getTime() <= Date.now() || !Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes <= 0 || row.encrypted_bytes > 2097152) throw fail("Метаданные пакета IPsec не подтверждены") + const { proof, ...unsigned } = scope.data + if (row.connection_id !== unsigned.authority.principal.connectionId || !timingSafeEqual(Buffer.from(proof, "hex"), Buffer.from(await sign(signed(row, unsigned)), "hex"))) throw fail("Подпись пакета IPsec не совпала") + return { ...row, access_scope: scope.data } + } + async function checkpoint(row: Meta) { + const scope = row.access_scope + const rows = (await deps.query("SELECT s.intended_state,s.result FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id JOIN mcp_plans p ON p.id=o.plan_id WHERE o.connection_id=$1 AND o.state='applied' AND p.operation_id='network.ipsec.existing-certificates.export' AND p.approved_by IS NOT NULL AND s.state='verified' AND s.intended_state->>'kind'='service' AND s.intended_state->>'service'='network.ipsec.existing-certificates.export' AND s.result->>'artifactId'=$2 LIMIT 2", [row.connection_id, row.id])).rows as { intended_state?: { input?: unknown }; result?: Record }[] + const candidate = rows[0], input = candidate?.intended_state?.input, result = candidate?.result + if (rows.length !== 1 || fingerprint(input) !== scope.authority.inputProof || !result || result.receiptId !== scope.authority.receiptId || result.serverId !== scope.authority.request.serverId || result.certificateId !== scope.authority.certificateId || result.verified !== true || result.ok !== true || result.commandCount !== 2 || result.completedCommands !== 2 || result.confirmedWrites !== 2 || result.afterProof !== scope.afterProof || result.automaticReplay !== false || result.forwardingVerified !== false || Object.entries(ipsecExistingCertificateExportArtifactReceipt(row, scope)).some(([key, value]) => result[key] !== value)) throw fail("Нет точной проверенной квитанции приватного экспорта IPsec", "STATE_CONFLICT") + } + function human(actor: AuthUser, ownerId: string) { + if (!actor || actor.id !== ownerId || !Array.isArray(actor.apps) || !actor.apps.includes("mm") || !Array.isArray(actor.permissions) || + !hasPermission(actor.permissions, "mm:network:write") || (actor.expiresAt !== undefined && actor.expiresAt <= Date.now())) { + throw new McpError("PERMISSION_DENIED", "Пакет IPsec доступен только владельцу", 403) + } + } + async function verify(id: string, authority: IpsecExistingCertificateExportAuthority, afterProof: string) { + await deps.authorizeExport() + const row = await meta(id), scope = row.access_scope + await deps.access(authority) + if (fingerprint(authority) !== fingerprint(scope.authority) || scope.afterProof !== afterProof) return false + const payload = (await deps.query("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152", [id, scope.proof])).rows[0] + await deps.access(authority) + return !!payload && typeof payload.ciphertext === "string" && hash(payload.ciphertext) === scope.encryptedSha256 && (await deps.source(authority, async () => { await deps.access(authority) })).proof === afterProof + } + async function metadata(id: string, actor: AuthUser, checkClaims = deps.checkClaims) { + await deps.authorizeExport() + const row = await meta(id), scope = row.access_scope + await checkClaims(actor) + human(actor, scope.authority.ownerId) + await deps.access(scope.authority); await checkpoint(row) + await checkClaims(actor); human(actor, scope.authority.ownerId) + if (row.expires_at.getTime() <= Date.now()) throw fail("Срок пакета IPsec истёк") + return scope + } + async function read(id: string, actor: AuthUser, checkClaims = deps.checkClaims, signal?: AbortSignal) { + await deps.authorizeExport() + const bounded = signal ? AbortSignal.any([signal, AbortSignal.timeout(45000)]) : AbortSignal.timeout(45000) + const row = await meta(id), scope = row.access_scope + const authorize = async () => { bounded.throwIfAborted(); await checkClaims(actor); human(actor, scope.authority.ownerId); await deps.access(scope.authority); await checkpoint(row); if (row.expires_at.getTime() <= Date.now()) throw fail("Срок пакета IPsec истёк") } + await authorize() + if ((await deps.source(scope.authority, authorize)).proof !== scope.afterProof) throw fail("IPsec изменился после создания пакета", "STATE_CONFLICT") + const payload = (await deps.query("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=2097152", [id, scope.proof])).rows[0] + await authorize() + if (!payload || typeof payload.ciphertext !== "string" || hash(payload.ciphertext) !== scope.encryptedSha256) throw fail("Шифротекст пакета IPsec изменился") + let content: Buffer + try { content = Buffer.from(await decryptSecret(payload.ciphertext), "base64") } catch { throw fail("Пакет IPsec не расшифрован") } + let delivered = false + try { + if (content.length !== scope.bytes || hash(content) !== scope.sha256) throw fail("Размер или SHA-256 пакета IPsec не совпал") + if ((await deps.source(scope.authority, authorize)).proof !== scope.afterProof) throw fail("IPsec изменился при выдаче пакета", "STATE_CONFLICT") + await authorize() + delivered = true + return { filename: row.filename, mediaType: row.media_type, content } + } finally { if (!delivered) content.fill(0) } + } + return { create, verify, metadata, read } +} + +const defaultStore = createIpsecExistingCertificateExportArtifactStore({ query: (sql, values) => pool.query(sql, values), transaction: connectionWrite, + access, source, checkClaims: assertPortalClaimsCurrent, authorizeExport: activation }) +export async function createIpsecExistingCertificateExportArtifact(authority: IpsecExistingCertificateExportAuthority, bundle: IpsecCertBundle, afterProof: string, authorize: () => Promise) { + activation(); return defaultStore.create(authority, bundle, afterProof, authorize) +} +export async function verifyIpsecExistingCertificateExportArtifact(id: string, authority: IpsecExistingCertificateExportAuthority, afterProof: string) { + activation(); return defaultStore.verify(id, authority, afterProof) +} +export async function humanIpsecExistingCertificateExportMetadata(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent) { + activation(); return defaultStore.metadata(id, actor, checkClaims) +} +export async function readHumanIpsecExistingCertificateExportArtifact(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent, signal?: AbortSignal) { + activation(); return defaultStore.read(id, actor, checkClaims, signal) +} diff --git a/backend/src/services/ipsec-existing-certificate-export-model.ts b/backend/src/services/ipsec-existing-certificate-export-model.ts new file mode 100644 index 00000000..c8483536 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-model.ts @@ -0,0 +1,112 @@ +import { z } from "zod" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError } from "../modules/mcp/security.js" +import { ipsecPeerDeletePaths, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { IPSEC_COMMON_NAME, IPSEC_SERVER_CERT, parseIpsecUserComment, resolveIke2ServerCert, selectIke2Peers } from "./ipsec-config.js" +import { certificateRole } from "./certificate-parse.js" +import { resolveIpsecClientTarget, type IpsecServerState } from "./ipsec-live.js" +import { MAX_OWNED_FILE_BYTES } from "./mikrotik.js" + +export const IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION = "network.ipsec.existing-certificates.export" +/** Explicit existing client/server/other/CA export approval received. + * Every request still requires its own scoped intent and human authorization. */ +export const IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED = true +const selector = z.discriminatedUnion("kind", [z.object({ kind: z.literal("client"), clientId: z.string().min(1).max(256) }).strict(), z.object({ kind: z.literal("certificate"), name: z.string().min(1).max(256) }).strict()]) +export const ipsecExistingCertificateExportRequestSchema = z.object({ serverId: z.number().int().positive().safe(), selector, privateKeyExport: z.literal(true), caPrivateKeyExport: z.boolean().default(false), passphrase: z.discriminatedUnion("kind", [z.object({ kind: z.literal("reference"), secretRef: z.string().uuid() }).strict(), z.object({ kind: z.literal("generate") }).strict()]) }).strict() +export type IpsecExistingCertificateExportRequest = z.infer +export const ipsecExistingCertificateExportSelectionSchema = ipsecExistingCertificateExportRequestSchema.omit({ passphrase: true }).strict() +export type IpsecExistingCertificateExportSelection = z.infer +const digest = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +export const ipsecExistingCertificateExportAuthoritySchema = z.object({ + principal: z.object({ connectionId: uuid, credentialId: uuid, credentialExpiresAt: z.string().datetime() }).strict(), ownerId: z.string().min(1), ownerProof: digest, policyProof: digest, generation: z.string().min(1), + request: ipsecExistingCertificateExportRequestSchema, receiptId: uuid, certificateId: z.string().regex(/^\*[a-z0-9]+$/i).max(100), certificateName: z.string().min(1).max(256), role: z.enum(["ca", "server", "client", "other"]), + sourceProof: digest, expectedProof: digest, referenceProof: digest, requestProof: digest, inputProof: digest, proofRevision: z.literal(1), +}).strict() +export type IpsecExistingCertificateExportAuthority = z.infer +export type IpsecExistingCertificateExportState = IpsecPeerDeleteState +type Row = Record +export const ipsecExistingCertificateExportSecretPurpose = (serverId: number, target: IpsecExistingCertificateExportRequest["selector"]) => `ipsec-existing-export-passphrase:${serverId}:${target.kind}:${encodeURIComponent(target.kind === "client" ? target.clientId : target.name)}` +const fail = (message: string, code = "STATE_CONFLICT", status = code === "INVALID_INPUT" ? 400 : code === "STATE_UNAVAILABLE" || code === "VERIFICATION_FAILED" ? 502 : 409) => new McpError(code, message, status) +const idKey = (value: string) => value.toLowerCase().replace(/^\*0+(?=[a-z0-9])/, "*") +const nativeId = (value: unknown): value is string => typeof value === "string" && /^\*[a-z0-9]+$/i.test(value) && value.length <= 100 +export function validateIpsecExistingCertificateExportState(state: IpsecExistingCertificateExportState) { + if (!state?.source || !Array.isArray(state.protections) || !state.catalogs || Buffer.byteLength(JSON.stringify(state)) > 1048576) throw fail("Неполное состояние экспорта IPsec", "STATE_UNAVAILABLE") + for (const path of [...ipsecPeerDeletePaths, "/file"]) { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000 || rows.some(row => !row || !nativeId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(rows.map(row => idKey(row[".id"]))).size !== rows.length) throw fail("Неполный native каталог экспорта IPsec", "STATE_UNAVAILABLE") + const names = rows.map(row => row.name?.trim()).filter(Boolean) + if (path !== "/ip/ipsec/active-peers" && new Set(names).size !== names.length) throw fail("Неоднозначные имена native объектов IPsec") + for (const row of rows) if (row.name && nativeId(row.name.trim()) && rows.some(other => other !== row && idKey(other[".id"]) === idKey(row.name.trim()))) throw fail("Неоднозначное имя/native ID экспорта") + } +} +export function planIpsecExistingCertificateExport(state: IpsecExistingCertificateExportState, server: ServerRow, raw: unknown, receiptId: string) { + const parsed = ipsecExistingCertificateExportRequestSchema.safeParse(raw) + if (!parsed.success) throw fail("Некорректный запрос приватного экспорта IPsec", "INVALID_INPUT") + const { passphrase: _passphrase, ...selection } = parsed.data + return { ...planIpsecExistingCertificateExportSelection(state, server, selection, receiptId), request: parsed.data } +} +/** Selection and state semantics are shared; material is bound independently by + * each trusted transport, so REST never pretends to own an MCP secret reference. */ +export function planIpsecExistingCertificateExportSelection(state: IpsecExistingCertificateExportState, server: ServerRow, raw: unknown, receiptId: string) { + const parsed = ipsecExistingCertificateExportSelectionSchema.safeParse(raw) + if (!parsed.success || !z.string().uuid().safeParse(receiptId).success) throw fail("Некорректный запрос приватного экспорта IPsec", "INVALID_INPUT") + const request = parsed.data + if (request.serverId !== server.id) throw fail("Сервер экспорта не совпал", "INVALID_INPUT") + validateIpsecExistingCertificateExportState(state) + const c = state.catalogs + const live = { server, client: undefined, peers: c["/ip/ipsec/peer"], identities: c["/ip/ipsec/identity"], modeConfigs: c["/ip/ipsec/mode-config"], pools: c["/ip/pool"], policies: c["/ip/ipsec/policy"], nat: c["/ip/firewall/nat"], active: c["/ip/ipsec/active-peers"], certs: c["/certificate"] } as unknown as IpsecServerState + let cert: Row | undefined, userName: string, serverEndpoint: string + if (request.selector.kind === "client") { + const target = resolveIpsecClientTarget(live, request.selector.clientId) + if (target.ambiguous) throw fail("Неоднозначный ID клиента IPsec") + if (!target.identity) throw fail("Клиент IPsec не найден", "NOT_FOUND", 404) + const identity = target.identity, managedName = parseIpsecUserComment(identity.comment) + if (!managedName) throw fail("Не managed-клиент IPsec", "INVALID_INPUT") + const name = identity["remote-certificate"]?.trim() + if (!name || identity["auth-method"]?.trim().toLowerCase() === "pre-shared-key") throw fail("У клиента нет привязанного клиентского сертификата; используйте экспорт по имени") + cert = c["/certificate"].find(row => row.name === name) + userName = managedName + serverEndpoint = String(c["/certificate"].find(row => row.name === IPSEC_SERVER_CERT)?.["common-name"] ?? "").trim() || server.host + } else { + const name = request.selector.name + cert = c["/certificate"].find(row => row.name?.trim() === name) + userName = String(cert?.["common-name"] ?? "").trim() || name + serverEndpoint = String(resolveIke2ServerCert(live.certs, selectIke2Peers(live.peers))?.["common-name"] ?? "").trim() || server.host + } + if (!cert?.name || cert.name !== cert.name.trim()) throw fail("Сертификат экспорта не найден или имя неоднозначно", "NOT_FOUND", 404) + const role = certificateRole(cert, { peerCertNames: c["/ip/ipsec/peer"].map(row => row.certificate ?? ""), identityCertNames: c["/ip/ipsec/identity"].map(row => row["remote-certificate"] ?? "") }) + // By-name REST currently permits all roles, including CA. Preserve that + // coverage explicitly; future approval must specifically include CA keys. + if (role === "ca" && !request.caPrivateKeyExport) throw fail("Экспорт CA может раскрыть закрытый ключ центра сертификации и требует отдельного явного caPrivateKeyExport", "APPROVAL_REQUIRED") + const exportPrefix = `mm-ipsec-existing-${receiptId}`, filename = `${exportPrefix}.p12` + if (c["/file"].some(row => row.name?.toLowerCase() === filename.toLowerCase())) throw fail("Receipt-owned имя экспорта уже занято; повтор запрещён") + const dns = c["/ip/ipsec/mode-config"].find(row => row.name?.trim() === IPSEC_COMMON_NAME)?.["static-dns"]?.trim() || undefined + const kinds: Record = { "/certificate": "certificate", "/ip/ipsec/peer": "ipsec_peer", "/ip/ipsec/identity": "ipsec_identity", "/file": "router_file" } + return { request, certificateId: cert[".id"], certificateName: cert.name, role, userName, serverEndpoint, dns, exportPrefix, filename, commandCount: 2 as const, companions: Object.entries(c).flatMap(([path, rows]) => rows.map(row => ({ kind: kinds[path] ?? path, id: row[".id"] }))) } +} +/** Legacy REST trims a supplied passphrase and generates for whitespace or a + * too-short trimmed value (formerly after a rejected export). Decide once + * before dispatch; never repeat export with another passphrase. */ +export function normalizeIpsecExistingExportPassphrase(raw: string | undefined, generate: () => string) { + if (raw !== undefined && (!z.string().min(8).max(16384).safeParse(raw).success)) throw fail("Некорректный секрет passphrase", "INVALID_INPUT") + const trimmed = raw?.trim() + const value = trimmed && trimmed.length >= 8 ? trimmed : generate() + if (!z.string().min(8).max(16384).safeParse(value).success || value !== value.trim()) throw fail("Не подтверждён пароль приватного экспорта", "INVALID_INPUT") + return value +} +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"]))) + 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)) + return { projected, file: { id: row[".id"], name: filename, size } } +} +export function projectIpsecExistingCertificateCleanup(state: IpsecExistingCertificateExportState, file: { id: string; name: string; size: number }) { + const projected = structuredClone(state), rows = projected.catalogs["/file"], matches = rows.filter(row => row[".id"] === file.id || row.name === file.name) + if (matches.length !== 1 || matches[0][".id"] !== file.id || matches[0].name !== file.name || Number(matches[0].size) !== file.size) throw fail("Ownership временного файла изменился; удаление запрещено", "VERIFICATION_FAILED") + rows.splice(rows.indexOf(matches[0]), 1) + return projected +} diff --git a/backend/src/services/ipsec-existing-certificate-export-rest-http.test.ts b/backend/src/services/ipsec-existing-certificate-export-rest-http.test.ts new file mode 100644 index 00000000..7724ae03 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest-http.test.ts @@ -0,0 +1,133 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { ipsecManagedComment, ipsecUserComment } from "./ipsec-config.js" +import { ipsecPeerDeletePaths } from "./ipsec-peer-delete.js" +import { ipsecUserUpdateValue } from "./ipsec-user-update.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, type IpsecExistingCertificateExportState } from "./ipsec-existing-certificate-export-model.js" +import { createIpsecExistingCertificateExportRestAdapter, type IpsecExistingCertificateExportRestAdapterDependencies } from "./ipsec-existing-certificate-export-rest.js" +import { createIpsecExistingCertificateExportRestHttpHandlers, type IpsecExistingCertificateExportRestHttpAdapter, type IpsecExistingCertificateExportRestHttpInput } from "./ipsec-existing-certificate-export-rest-http.js" +import { IpsecExistingCertificateExportExecutionError } from "./ipsec-existing-certificate-export.js" + +const secret = "NEVER_REST_HTTP_PASSPHRASE", bytes = "NEVER_HTTP_PRIVATE_P12", source = "a".repeat(64) +const server = { id: 1, host: "router.example.test" } as ServerRow +const body = () => ({ passphrase: secret, privateKeyExport: true, caPrivateKeyExport: false }) +const actor: AuthUser = { id: "http-owner", email: "owner@example.test", name: "Owner", apps: ["mm"], permissions: ["mm:network:write"], issuedAt: 99000, expiresAt: 999999 } +const base = (): IpsecExistingCertificateExportState => ({ source, proof: "unused", protections: [], catalogs: { + ...Object.fromEntries([...ipsecPeerDeletePaths, "/file"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", certificate: "ipsec-server", comment: ipsecManagedComment() }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "remote-certificate": "actual-bound-cert", "mode-config": "ipsec-vpn", comment: ipsecUserComment("Display-label-not-certificate-name") }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "ipsec-vpn", "static-dns": "192.0.2.53" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign,crl-sign", authority: "true", "private-key": "true" }, { ".id": "*s", name: "ipsec-server", "common-name": "vpn.example.test", "key-usage": "tls-server" }, { ".id": "*c", name: "actual-bound-cert", "common-name": "Certificate CN", "key-usage": "tls-client", "private-key": "true" }, { ".id": "*o", name: "foreign-other", "common-name": "Other CN" }], +} }) +const proof = async (state: IpsecExistingCertificateExportState) => fingerprint(ipsecUserUpdateValue(state)) +function fixture() { + const state = base(), commands: string[] = [], keys: string[] = [], audits: unknown[] = [], claimed = new Set() + let mode = "normal", prepareCalls = 0, executeCalls = 0 + const deps: IpsecExistingCertificateExportRestAdapterDependencies = { + server: async () => server, sourceProof: async () => mode === "source" ? "f".repeat(64) : source, proof, + privateProof: async (value, domain) => createHmac("sha256", "synthetic-private-key").update(domain).update(value).digest("hex"), + checkClaims: async () => { if (mode === "revoked") throw new McpError("OWNER_CLAIMS_REVOKED", secret, 401) }, + read: async (_s, authorize, control) => { assert.ok(control?.signal); assert.ok(control?.deadlineAt); await authorize(); const value = structuredClone(state); value.proof = await proof(value); await authorize(); return value }, + boundary: async (a, work) => { assert.equal(a.transport, "rest"); assert.equal("principal" in a, false); return work() }, + claim: async a => { if (claimed.has(a.requestKey)) throw new McpError("STATE_CONFLICT", secret, 409); claimed.add(a.requestKey); keys.push(a.requestKey); audits.push(a) }, + checkpoint: async (_a, progress) => { audits.push(progress) }, + publish: async (_a, bundle, _proof, authorize) => { await authorize(); return bundle }, + generatePassphrase: () => "GENERATED_PRIVATE_PASSPHRASE", clock: () => 100000, + client: () => ({ + post: async (_path: string, fields: Record, _timeout: unknown, _signal: unknown, dispatch: () => Promise) => { await dispatch(); commands.push(fields.name); state.catalogs["/file"].push({ ".id": "*new", name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(bytes)) }); if (mode === "lostAck") throw Error(secret); return {} }, + downloadOwnedFile: async (_file: unknown, control: { authorize: () => Promise }) => { await control.authorize(); return Buffer.from(bytes) }, + delete: async (path: string, _timeout: unknown, dispatch: () => Promise) => { await dispatch(); commands.push(path); state.catalogs["/file"] = [] }, + }) as never, + } + const actual = createIpsecExistingCertificateExportRestAdapter(deps) + const adapter: IpsecExistingCertificateExportRestHttpAdapter = { + prepare: async (...args) => { prepareCalls++; assert.equal(args[3]?.timeoutMs, 180000); return actual.prepare(...args) }, + execute: async (...args) => { executeCalls++; assert.equal(args[2]?.timeoutMs, 180000); if (mode === "source") mode = "normal"; return actual.execute(...args) }, + } + return { state, commands, keys, audits, adapter, handlers: createIpsecExistingCertificateExportRestHttpHandlers(adapter), counts: () => ({ prepareCalls, executeCalls }), setMode: (value: string) => { mode = value } } +} +const input = (): IpsecExistingCertificateExportRestHttpInput => ({ actor, serverId: "1", body: body(), requestKey: randomUUID() }) +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Real SQL forbidden") }) as never +pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +let cases = 0 +try { + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) + { + const f = fixture(), req = { ...input(), clientId: "identity:*i" }, result = await f.handlers.client(req) + assert.equal(result.user, "Display-label-not-certificate-name"); assert.equal(result.passphrase, secret) + assert.deepEqual(f.commands, ["actual-bound-cert", "/file/*new"]); assert.deepEqual(f.keys, [req.requestKey]) + assert.equal(result.serverEndpoint, "vpn.example.test"); assert.equal(result.contentB64, Buffer.from(bytes).toString("base64")); assert.ok(result.sswanContent); assert.ok(result.instructions?.includes("192.0.2.53")) + for (const key of ["receiptId", "publication", "artifactId", "principal", "humanDownloadOnly", "ipsecContentReturned"]) assert.equal(key in result, false) + await assert.rejects(() => f.handlers.client(req)); assert.equal(f.commands.length, 2); assert.deepEqual(f.keys, [req.requestKey]); assert.equal(JSON.stringify(f.audits).includes(secret), false); cases++ + } + for (const [name, user] of [["actual-bound-cert", "Certificate CN"], ["ipsec-server", "vpn.example.test"], ["foreign-other", "Other CN"], ["ca", "ca"]]) { + const f = fixture(), result = await f.handlers.certificate({ ...input(), body: { ...body(), name, caPrivateKeyExport: name === "ca" } }) + assert.equal(result.user, user); assert.equal(f.commands[0], name); assert.equal(f.commands.length, 2); cases++ + } + for (const variation of ["server", "unsafeServer", "key", "missingKey", "arrayKey", "clientId", "missingPrivate", "falsePrivate", "short", "long", "extra", "bodyServer", "bodyClient", "bodySelector", "clientName", "certName", "certClient", "caFlag"]) { + const f = fixture(), req = { ...input(), clientId: "identity:*i" }, raw = req.body as Record + let certificate = false + if (variation === "server") req.serverId = "srv1" + if (variation === "unsafeServer") req.serverId = Number.MAX_SAFE_INTEGER + 1 + if (variation === "key") req.requestKey = "bad-key" + if (variation === "missingKey") req.requestKey = undefined + if (variation === "arrayKey") req.requestKey = [randomUUID()] as never + if (variation === "clientId") req.clientId = "" + if (variation === "missingPrivate") delete raw.privateKeyExport + if (variation === "falsePrivate") raw.privateKeyExport = false + if (variation === "short") raw.passphrase = "short" + if (variation === "long") raw.passphrase = "x".repeat(16385) + if (variation === "extra") raw.unknown = secret + if (variation === "bodyServer") raw.serverId = 2 + if (variation === "bodyClient") raw.clientId = "identity:*foreign" + if (variation === "bodySelector") raw.selector = { kind: "certificate", name: "ca" } + if (variation === "clientName") raw.name = "ca" + if (variation === "certName") { certificate = true; raw.name = "" } + if (variation === "certClient") { certificate = true; raw.name = "actual-bound-cert"; raw.clientId = "identity:*i" } + if (variation === "caFlag") { certificate = true; raw.name = "ca" } + await assert.rejects(() => certificate ? f.handlers.certificate(req) : f.handlers.client(req), (error: unknown) => error instanceof McpError && !error.message.includes(secret)) + if (variation !== "caFlag") assert.deepEqual(f.counts(), { prepareCalls: 0, executeCalls: 0 }, variation) + assert.equal(f.commands.length, 0); cases++ + } + for (const variation of ["abort", "source", "revoked", "lostAck"]) { + const f = fixture(), controller = new AbortController() + if (variation === "abort") controller.abort(secret) + else f.setMode(variation) + await assert.rejects(() => f.handlers.client({ ...input(), clientId: "identity:*i", signal: controller.signal }), (error: unknown) => { + assert.ok(error instanceof McpError); assert.equal(error.message.includes(secret), false) + if (variation === "lostAck") { assert.ok(error instanceof IpsecExistingCertificateExportExecutionError); assert.equal(error.confirmedWrites, 0); assert.equal(error.code, "MUTATION_OUTCOME_UNKNOWN") } + return true + }) + assert.equal(f.commands.length, variation === "lostAck" ? 1 : 0); cases++ + } + for (const mutation of ["missingBundle", "missingField", "extraField", "extraReceipt", "server", "count", "automaticReplay", "forwarding", "certId", "proof", "receiptId", "filename", "mime", "base64"]) { + const f = fixture() + const adapter: IpsecExistingCertificateExportRestHttpAdapter = { prepare: f.adapter.prepare, execute: async (...args) => { + const raw = structuredClone(await f.adapter.execute(...args)) as unknown as Record, bundle = raw.publication as Record + if (mutation === "missingBundle") delete raw.publication + if (mutation === "missingField") delete bundle.contentB64 + if (mutation === "extraField") bundle.secretUnexpected = secret + if (mutation === "extraReceipt") raw.secretUnexpected = secret + if (mutation === "server") raw.serverId = 2 + if (mutation === "count") raw.completedCommands = 1 + if (mutation === "automaticReplay") raw.automaticReplay = true + if (mutation === "forwarding") raw.forwardingVerified = true + if (mutation === "certId") raw.certificateId = "not-native" + if (mutation === "proof") raw.afterProof = "bad-proof" + if (mutation === "receiptId") raw.receiptId = "bad-id" + if (mutation === "filename") bundle.filename = "foreign.p12" + if (mutation === "mime") bundle.mime = "text/plain" + if (mutation === "base64") bundle.contentB64 = "not-valid-private-file!" + return raw as never + } } + const handlers = createIpsecExistingCertificateExportRestHttpHandlers(adapter) + await assert.rejects(() => handlers.client({ ...input(), clientId: "identity:*i" }), (error: unknown) => error instanceof IpsecExistingCertificateExportExecutionError && !error.message.includes(secret)) + assert.equal(f.commands.length, 2); cases++ + } + console.log("Active HTTP existing certificate export mapping:", JSON.stringify({ cases, actualRestAdapterAndSharedCore: true, bothPaths: true, roles: 4, realRouterCalls: 0, realSql: 0, activated: true })) +} finally { pool.query = oldQuery; pool.connect = oldConnect } diff --git a/backend/src/services/ipsec-existing-certificate-export-rest-http.ts b/backend/src/services/ipsec-existing-certificate-export-rest-http.ts new file mode 100644 index 00000000..701b2a87 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest-http.ts @@ -0,0 +1,64 @@ +import { z } from "zod" +import { ipsecCertBundleSchema, ipsecExistingCertificateExportBodySchema, ipsecCertExportByNameRequestSchema, ipsecExistingCertificateExportRequestKeySchema, type IpsecCertBundle } from "@mmapp/contracts/ipsec" +import type { AuthUser } from "../lib/permissions.js" +import { McpError } from "../modules/mcp/security.js" +import { parseDbServerId } from "../utils/server-id.js" +import { IpsecExistingCertificateExportExecutionError } from "./ipsec-existing-certificate-export.js" +import type { createIpsecExistingCertificateExportRestAdapter } from "./ipsec-existing-certificate-export-rest.js" + +const bodySchema = ipsecExistingCertificateExportBodySchema +const certificateBodySchema = ipsecCertExportByNameRequestSchema +const uuid = ipsecExistingCertificateExportRequestKeySchema, clientIdSchema = z.string().min(1).max(256) +const bundleSchema = ipsecCertBundleSchema.extend({ + user: z.string().min(1), serverEndpoint: z.string().min(1), filename: z.string().min(1), + contentB64: z.string().min(4).max(1048576).regex(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/), + mime: z.literal("application/x-pkcs12"), passphrase: z.string().min(8).max(16384), + sswanFilename: z.string().min(1), sswanContent: z.string().min(1), instructions: z.string().min(1), +}).strict() +const receiptSchema = z.object({ + ok: z.literal(true), verified: z.literal(true), receiptId: uuid, certificateId: z.string().regex(/^\*[a-z0-9]+$/i).max(100), serverId: z.number().int().positive().safe(), + commandCount: z.literal(2), completedCommands: z.literal(2), confirmedWrites: z.literal(2), afterProof: z.string().regex(/^[a-f0-9]{64}$/), automaticReplay: z.literal(false), forwardingVerified: z.literal(false), publication: bundleSchema, +}).strict() +export type IpsecExistingCertificateExportRestHttpAdapter = Pick, "prepare" | "execute"> +export interface IpsecExistingCertificateExportRestHttpInput { + actor: AuthUser + serverId: string | number + body: unknown + /** Stable UUID Idempotency-Key from the caller; never generated or rotated. */ + requestKey: string | undefined + signal?: AbortSignal +} +export interface IpsecExistingCertificateExportRestHttpClientInput extends IpsecExistingCertificateExportRestHttpInput { clientId: string } +const invalid = () => new McpError("INVALID_INPUT", "Некорректный запрос REST-экспорта или отсутствует UUID Idempotency-Key", 400) +function sanitized(error: unknown): never { + if (error instanceof IpsecExistingCertificateExportExecutionError) throw new IpsecExistingCertificateExportExecutionError(error.code === "VERIFICATION_FAILED" ? "VERIFICATION_FAILED" : "MUTATION_OUTCOME_UNKNOWN", error.confirmedWrites, error.completedCommands) + if (error instanceof McpError) throw new McpError(error.code, "REST-экспорт не подтверждён; автоматический повтор запрещён", error.statusCode) + throw new McpError("STATE_UNAVAILABLE", "REST-экспорт не подтверждён; требуется сверка перед новой операцией", 502) +} + +/** HTTP mapping only. No routes, activation, actor defaults, native algorithm, + * MCP artifacts or retry. Production activation remains a separate boundary. */ +export function createIpsecExistingCertificateExportRestHttpHandlers(adapter: IpsecExistingCertificateExportRestHttpAdapter) { + const execute = async (input: IpsecExistingCertificateExportRestHttpInput, kind: "client" | "certificate", clientId?: string): Promise => { + try { + // Validate every URL/header/body field before touching the adapter. + const serverId = typeof input.serverId === "string" || typeof input.serverId === "number" ? parseDbServerId(input.serverId) : null + const key = uuid.safeParse(input.requestKey) + const body = (kind === "client" ? bodySchema : certificateBodySchema).safeParse(input.body) + if (serverId === null || !key.success || !body.success || (kind === "client" && !clientIdSchema.safeParse(clientId).success)) throw invalid() + const parsed = body.data + const selection = kind === "client" ? { kind: "client" as const, clientId: clientId! } : { kind: "certificate" as const, name: (parsed as z.infer).name } + const request = { serverId, selector: selection, privateKeyExport: parsed.privateKeyExport, caPrivateKeyExport: parsed.caPrivateKeyExport, passphrase: parsed.passphrase } + const options = { signal: input.signal, timeoutMs: 180000 } + const token = await adapter.prepare(input.actor, request, key.data, options) + const raw = await adapter.execute(token, input.actor, options) + const checked = receiptSchema.safeParse(raw) + if (!checked.success || checked.data.serverId !== serverId || checked.data.publication.filename !== `mm-ipsec-existing-${checked.data.receiptId}.p12`) throw new IpsecExistingCertificateExportExecutionError("VERIFICATION_FAILED", 0, 0) + return checked.data.publication + } catch (error) { sanitized(error) } + } + return { + client: (input: IpsecExistingCertificateExportRestHttpClientInput) => execute(input, "client", input.clientId), + certificate: (input: IpsecExistingCertificateExportRestHttpInput) => execute(input, "certificate"), + } +} diff --git a/backend/src/services/ipsec-existing-certificate-export-rest-journal.sql.test.ts b/backend/src/services/ipsec-existing-certificate-export-rest-journal.sql.test.ts new file mode 100644 index 00000000..6705a99c --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest-journal.sql.test.ts @@ -0,0 +1,136 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { PoolClient } from "pg" +import type { IpsecExistingCertificateExportRestAuthority as Authority } from "./ipsec-existing-certificate-export-rest.js" + +// Guard before importing any configured pool or migration/audit runtime. +const url = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") +assert.equal(process.env.MMAPP_OWN_IPSEC_EXPORT_JOURNAL, "true") +assert.equal(url.hostname, "127.0.0.1") +assert.equal(url.username, "mcp_test") +assert.equal(url.pathname, "/postgres") +assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres") +assert.match(process.env.MCP_STATE_DIR ?? "", /mm-mcp-test-/) +const { pool, closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +const { audit } = await import("../modules/mcp/store.js") +const { fingerprint } = await import("../modules/mcp/security.js") +const { createIpsecExistingCertificateExportRestJournal } = await import("./ipsec-existing-certificate-export-rest-journal.js") +type Dependencies = Parameters[0] +async function transaction(work: (client: Pick) => Promise): Promise { + const client = await pool.connect() + let committed = false + try { + await client.query("BEGIN") + const result = await work(client) + await client.query("COMMIT"); committed = true + return result + } catch (error) { if (!committed) await client.query("ROLLBACK"); throw error } + finally { client.release() } +} +function authority(): Authority { + const actor = { id: "rest-export-owned-" + randomUUID(), email: "NEVER_EMAIL", name: "NEVER_NAME", apps: ["mm"], permissions: ["mm:network:write"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 600000 } + const request = { serverId: 1, selector: { kind: "certificate" as const, name: "NEVER_CERTIFICATE_NAME" }, privateKeyExport: true as const, caPrivateKeyExport: false } + return { transport: "rest", actor, ownerProof: fingerprint(actor), requestKey: randomUUID(), receiptId: randomUUID(), deadlineAt: Date.now() + 180000, + request, requestProof: fingerprint(request), certificateId: "*owned", certificateName: "NEVER_CERTIFICATE_NAME", role: "client", sourceProof: "a".repeat(64), expectedProof: "b".repeat(64), referenceProof: "c".repeat(64) } +} +const journal = (overrides: Partial = {}) => createIpsecExistingCertificateExportRestJournal({ transaction, audit, authorize: async () => {}, ...overrides }) +const claimRows = async (a: Authority) => (await pool.query("SELECT payload FROM mcp_audit WHERE actor_id=$1 AND connection_id IS NULL AND action='ipsec.existing.export.rest.claim' AND payload->>'requestKey'=$2", [a.actor.id, a.requestKey])).rows +const stageRows = async (a: Authority) => (await pool.query("SELECT payload FROM mcp_audit WHERE actor_id=$1 AND connection_id IS NULL AND action='ipsec.existing.export.rest.checkpoint' AND payload->>'requestKey'=$2 ORDER BY id", [a.actor.id, a.requestKey])).rows +const stageOne = (a: Authority) => ({ completedCommands: 1, afterProof: "d".repeat(64), file: { id: "*file", name: `mm-ipsec-existing-${a.receiptId}.p12`, size: 128 } }) +const stageTwo = (a: Authority) => ({ completedCommands: 2, afterProof: a.expectedProof }) +const cases: string[] = [] +async function check(name: string, work: () => Promise) { await work(); cases.push(name) } +try { + await applySqlMigrations(pool) + await check("claim persists only exact safe metadata and refuses same actor/key replay", async () => { + const a = authority(), store = journal(); await store.claim(a) + assert.equal((await claimRows(a)).length, 1) + assert.ok(!JSON.stringify(await claimRows(a)).includes("NEVER_")) + await assert.rejects(store.claim(a)); assert.equal((await claimRows(a)).length, 1) + }) + await check("changing receipt or target under a spent key cannot dispatch again", async () => { + const a = authority(), store = journal(); await store.claim(a) + const request = { ...a.request, selector: { kind: "certificate" as const, name: "another" } } + await assert.rejects(store.claim({ ...a, receiptId: randomUUID(), request, requestProof: fingerprint(request) })) + assert.equal((await claimRows(a)).length, 1) + }) + await check("stable keys are actor scoped", async () => { + const a = authority(), other = authority(); other.requestKey = a.requestKey + await journal().claim(a); await journal().claim(other) + assert.equal((await claimRows(a)).length, 1); assert.equal((await claimRows(other)).length, 1) + }) + await check("concurrent claims serialize on the real transaction advisory lock", async () => { + const a = authority(), store = journal() + const results = await Promise.allSettled([store.claim(a), store.claim(a)]) + assert.equal(results.filter(r => r.status === "fulfilled").length, 1) + assert.equal((await claimRows(a)).length, 1) + }) + await check("audit failure rolls back claim before any native work", async () => { + const a = authority(), store = journal({ audit: async (...args) => { await audit(...args); throw Error("synthetic audit failure") } }) + await assert.rejects(store.claim(a)); assert.equal((await claimRows(a)).length, 0) + }) + await check("lost COMMIT acknowledgement leaves a spent key and forbids replay", async () => { + const a = authority(), store = journal({ transaction: async work => { await transaction(work); throw Error("synthetic lost COMMIT acknowledgement") } }) + await assert.rejects(store.claim(a)); assert.equal((await claimRows(a)).length, 1) + await assert.rejects(journal().claim(a)); assert.equal((await claimRows(a)).length, 1) + }) + await check("revocation after audit insert rolls back atomically", async () => { + const a = authority(); let revoked = false + const store = journal({ audit: async (...args) => { await audit(...args); revoked = true }, authorize: async () => { if (revoked) throw Error("revoked") } }) + await assert.rejects(store.claim(a)); assert.equal((await claimRows(a)).length, 0) + }) + await check("revocation after an observed real advisory-lock wait blocks the claim", async () => { + const a = authority(), blocker = await pool.connect(); let revoked = false + let result: Promise | undefined + try { + await blocker.query("BEGIN") + await blocker.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [`ipsec-existing-export-rest:${a.actor.id}:${a.requestKey}`]) + result = journal({ authorize: async () => { if (revoked) throw Error("revoked after lock wait") } }).claim(a).then(() => true, () => false) + let observed = false + for (let i = 0; i < 50; i++) { + const waiting = (await pool.query("SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND NOT granted AND database=(SELECT oid FROM pg_catalog.pg_database WHERE datname=current_database()) LIMIT 1")).rows.length + if (waiting) { observed = true; break } + await new Promise(resolve => setTimeout(resolve, 10)) + } + assert.equal(observed, true, "actual waiting backend must be observed before revocation") + revoked = true + } finally { await blocker.query("ROLLBACK"); blocker.release() } + assert.equal(await result, false); assert.equal((await claimRows(a)).length, 0) + }) + await check("two exact checkpoint stages bind the committed claim without private values", async () => { + const a = authority(), store = journal(); await store.claim(a) + await store.checkpoint(a, stageOne(a)); await store.checkpoint(a, stageTwo(a)) + assert.equal((await stageRows(a)).length, 2) + assert.ok(!JSON.stringify(await stageRows(a)).includes("NEVER_")) + }) + await check("missing claim or skipped stage cannot manufacture completion", async () => { + const a = authority(), store = journal() + await assert.rejects(store.checkpoint(a, stageOne(a))) + await store.claim(a); await assert.rejects(store.checkpoint(a, stageTwo(a))) + assert.equal((await stageRows(a)).length, 0) + }) + await check("checkpoint identity must match all original claim proofs", async () => { + const a = authority(), store = journal(); await store.claim(a) + await assert.rejects(store.checkpoint({ ...a, referenceProof: "f".repeat(64) }, stageOne(a))) + assert.equal((await stageRows(a)).length, 0) + }) + await check("duplicate or regressing checkpoint stages remain single use", async () => { + const a = authority(), store = journal(); await store.claim(a); await store.checkpoint(a, stageOne(a)) + await assert.rejects(store.checkpoint(a, stageOne(a))) + await store.checkpoint(a, stageTwo(a)); await assert.rejects(store.checkpoint(a, stageTwo(a))) + await assert.rejects(store.checkpoint(a, stageOne(a))); assert.equal((await stageRows(a)).length, 2) + }) + await check("invalid owned file and false completion evidence fail before append", async () => { + const a = authority(), store = journal(); await store.claim(a) + for (const file of [{ ...stageOne(a).file, size: 61439 }, { ...stageOne(a).file, name: "foreign.p12" }, { ...stageOne(a).file, id: "unsafe" }]) await assert.rejects(store.checkpoint(a, { ...stageOne(a), file })) + await assert.rejects(store.checkpoint(a, { ...stageTwo(a), afterProof: "f".repeat(64) })) + assert.equal((await stageRows(a)).length, 0) + }) + await check("concurrent checkpoints cannot duplicate the same stage", async () => { + const a = authority(), store = journal(); await store.claim(a) + const result = await Promise.allSettled([store.checkpoint(a, stageOne(a)), store.checkpoint(a, stageOne(a))]) + assert.equal(result.filter(r => r.status === "fulfilled").length, 1); assert.equal((await stageRows(a)).length, 1) + }) + console.log("Owned REST export journal SQL:", JSON.stringify({ cases: cases.length, names: cases, actualSql: true, nativeCalls: 0, productionDatabaseTouched: false, nativeExportInvoked: false })) +} finally { await closePool() } diff --git a/backend/src/services/ipsec-existing-certificate-export-rest-journal.test.ts b/backend/src/services/ipsec-existing-certificate-export-rest-journal.test.ts new file mode 100644 index 00000000..eab49cd3 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest-journal.test.ts @@ -0,0 +1,166 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import type { PoolClient } from "pg" +import { createIpsecExistingCertificateExportRestJournal, IpsecExistingCertificateExportRestJournalError, + type IpsecExistingCertificateExportRestJournalDependencies as Dependencies, + type IpsecExistingCertificateExportRestAuthority as Authority } from "./ipsec-existing-certificate-export-rest-journal.js" + +// Entirely inert SQL/transaction model. No config, production factory, native +// transport, private material, pool or server is imported/constructed. +const canonical = (value: unknown): string => Array.isArray(value) ? "[" + value.map(canonical).join(",") + "]" : + value && typeof value === "object" ? "{" + Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value) +const proof = (value: unknown) => createHash("sha256").update(canonical(value)).digest("hex") +const actor = { id: "journal-owner", email: "NEVER_EMAIL", name: "NEVER_ACTOR_NAME", apps: ["mm"], permissions: ["mm:network:write"], + issuedAt: Date.now() - 1000, expiresAt: Date.now() + 3600000 } +const request = { serverId: 207, selector: { kind: "certificate" as const, name: "NEVER_CERTIFICATE_NAME" }, privateKeyExport: true as const, caPrivateKeyExport: false } +const authority: Authority = { transport: "rest", actor, ownerProof: proof(actor), requestKey: "fa58fa40-4bcb-4616-9316-cb69463b3151", + deadlineAt: Date.now() + 180000, request, receiptId: "fa58fa40-4bcb-4616-9316-cb69463b3150", certificateId: "*a1", certificateName: "NEVER_CERTIFICATE_NAME", + role: "client", sourceProof: "a".repeat(64), expectedProof: "b".repeat(64), referenceProof: "c".repeat(64), requestProof: proof(request) } +const first = { completedCommands: 1, afterProof: "d".repeat(64), file: { id: "*f1", name: `mm-ipsec-existing-${authority.receiptId}.p12`, size: 61438 } } +const second = { completedCommands: 2, afterProof: authority.expectedProof } +type Entry = { actorId: string; action: string; connectionId: null; payload: Record } +function fixture() { + const committed: Entry[] = [], trace: string[] = [] + let staged: Entry[] | undefined, active: Pick | undefined + let authCount = 0, transactionCount = 0, rollbacks = 0, lostCommitAck = false + let authorizeHook: ((count: number) => void) | undefined + let auditHook: (() => void) | undefined + const deps: Dependencies = { + transaction: async work => { + transactionCount++; trace.push("begin"); assert.equal(staged, undefined); staged = [] + const client = { query: async (sql: string, values?: unknown[]) => { + assert.equal(client, active) + if (sql.startsWith("SET LOCAL")) { + assert(["SET LOCAL lock_timeout='2s'", "SET LOCAL statement_timeout='5s'"].includes(sql)); trace.push(sql); return { rows: [] } + } + if (sql.includes("pg_advisory_xact_lock")) { + assert.match(sql, /hashtextextended\(\$1,0\)/); assert.equal(values?.length, 1); assert.equal(typeof values?.[0], "string") + trace.push("lock"); return { rows: [] } + } + assert.match(sql, /FROM mcp_audit WHERE actor_id=\$1 AND connection_id IS NULL AND action=\$2/) + assert.match(sql, /payload->>'requestKey'=\$3/); assert.match(sql, /LIMIT 2/) + assert.match(sql, /octet_length\(payload::text\)<=8192/) + assert.equal(values?.length, 3) + trace.push(String(values?.[1]).endsWith(".claim") ? "read-claims" : "read-stages") + return { rows: [...committed, ...staged!].filter(row => row.actorId === values?.[0] && row.action === values?.[1] && row.payload.requestKey === values?.[2]).slice(0, 2).map(row => ({ payload: structuredClone(row.payload) })) } + } } as Pick + active = client + try { + const result = await work(client) + committed.push(...staged); staged = undefined; trace.push("commit") + if (lostCommitAck) { lostCommitAck = false; throw Error("NEVER_DRIVER_COMMIT_DETAILS") } + return result + } catch (error) { if (staged) { staged = undefined; rollbacks++; trace.push("rollback") }; throw error } + finally { active = undefined } + }, + audit: async (client, actorId, action, connectionId, payload) => { + assert.equal(client, active); assert(staged); assert.equal(connectionId, null) + assert.doesNotMatch(JSON.stringify(payload), /NEVER_|email|certificateName|passphrase|principal|password|contents/) + staged.push({ actorId, action, connectionId, payload: structuredClone(payload) as Record }) + trace.push("audit"); auditHook?.() + }, + authorize: async a => { + assert(Object.isFrozen(a)); assert(Object.isFrozen(a.actor)); assert(Object.isFrozen(a.request)) + trace.push("authorize"); authCount++; authorizeHook?.(authCount) + }, + } + return { journal: createIpsecExistingCertificateExportRestJournal(deps), deps, committed, trace, + stats: () => ({ authCount, transactionCount, rollbacks }), revokeAt(count: number) { authorizeHook = n => { if (n === count) throw Error("NEVER_REVOKED_DETAILS") } }, + failAudit() { auditHook = () => { throw Error("NEVER_AUDIT_DETAILS") } }, loseCommitAck() { lostCommitAck = true } } +} +async function rejected(work: () => Promise) { + await assert.rejects(work, error => { + assert(error instanceof IpsecExistingCertificateExportRestJournalError) + assert.equal(error.code, "STATE_CONFLICT"); assert.equal(error.statusCode, 409) + assert.doesNotMatch(error.message, /NEVER_|passphrase|password|certificateName/) + return true + }) +} + +{ + const f = fixture() + await f.journal.claim(authority) + assert.deepEqual(f.trace, ["authorize", "begin", "SET LOCAL lock_timeout='2s'", "SET LOCAL statement_timeout='5s'", "lock", "authorize", "read-claims", "audit", "authorize", "commit"]) + assert.deepEqual(Object.keys(f.committed[0].payload).sort(), "receiptId requestKey serverId certificateId role ownerProof requestProof referenceProof sourceProof expectedProof".split(" ").sort()) + await f.journal.checkpoint(authority, first); await f.journal.checkpoint(authority, second) + assert.equal(f.committed.length, 3) + assert.deepEqual(f.committed[1].payload.file, { id: "*f1", size: 61438 }) + assert(!Object.hasOwn(f.committed[2].payload, "file")) + await rejected(() => f.journal.claim(authority)) + await rejected(() => f.journal.claim({ ...authority, receiptId: authority.requestKey, referenceProof: "e".repeat(64) })) + await rejected(() => f.journal.checkpoint(authority, first)) + await rejected(() => f.journal.checkpoint(authority, second)) + assert.equal(f.committed.length, 3) +} +for (const patch of [ + { principal: { connectionId: authority.requestKey } }, { passphrase: "NEVER_PASSWORD" }, { unknown: true }, + { transport: "mcp" }, { requestKey: "../invalid" }, { certificateId: "native-name" }, { ownerProof: "0".repeat(64) }, + { requestProof: "0".repeat(64) }, { expectedProof: "not-a-proof" }, { role: "unknown" }, { deadlineAt: Date.now() - 1 }, + { actor: { ...actor, extra: "NEVER_PII" } }, { request: { ...request, passphrase: "NEVER_PASSWORD" } }, + { request: { ...request, serverId: "207" } }, +]) { + const f = fixture(); await rejected(() => f.journal.claim({ ...authority, ...patch } as Authority)) + assert.deepEqual(f.stats(), { authCount: 0, transactionCount: 0, rollbacks: 0 }); assert.equal(f.committed.length, 0) +} +for (const value of [ + { ...first, completedCommands: 0 }, { ...first, completedCommands: 3 }, { ...first, afterProof: "invalid" }, + { ...first, file: undefined }, { ...first, file: { ...first.file, name: "wrong.p12" } }, + { ...first, file: { ...first.file, id: "not-native" } }, { ...first, file: { ...first.file, size: 0 } }, + { ...first, file: { ...first.file, size: 61439 } }, { ...first, file: { ...first.file, contents: "NEVER_BYTES" } }, + { ...second, file: first.file }, { ...second, afterProof: "e".repeat(64) }, { ...second, privateKey: "NEVER_KEY" }, +]) { + const f = fixture(); await f.journal.claim(authority) + const before = f.stats(); await rejected(() => f.journal.checkpoint(authority, value)) + assert.deepEqual(f.stats(), before); assert.equal(f.committed.length, 1) +} +{ + const f = fixture(); await rejected(() => f.journal.checkpoint(authority, first)); assert.equal(f.committed.length, 0) + await f.journal.claim(authority) + await rejected(() => f.journal.checkpoint(authority, second)) // no skipped stage + await f.journal.checkpoint(authority, first) + await rejected(() => f.journal.checkpoint(authority, first)) // no repeated stage + await f.journal.checkpoint(authority, second) +} +for (const patch of [{ receiptId: authority.requestKey }, { referenceProof: "e".repeat(64) }, { sourceProof: "e".repeat(64) }, + { expectedProof: "e".repeat(64) }, { certificateId: "*a2" }, { role: "server" as const }]) { + const f = fixture(); await f.journal.claim(authority) + await rejected(() => f.journal.checkpoint({ ...authority, ...patch }, first)); assert.equal(f.committed.length, 1) +} +for (const corrupt of ["duplicate", "extra", "different-proof"] as const) { + const f = fixture(); await f.journal.claim(authority) + if (corrupt === "duplicate") f.committed.push(structuredClone(f.committed[0])) + else if (corrupt === "extra") f.committed[0].payload.unknown = true + else f.committed[0].payload.referenceProof = "e".repeat(64) + const count = f.committed.length; await rejected(() => f.journal.checkpoint(authority, first)); assert.equal(f.committed.length, count) +} +for (const revokeAt of [1, 2, 3]) { + const f = fixture(); f.revokeAt(revokeAt); await rejected(() => f.journal.claim(authority)); assert.equal(f.committed.length, 0) + assert.equal(f.stats().rollbacks, revokeAt === 1 ? 0 : 1) +} +{ + const f = fixture(); await f.journal.claim(authority); f.revokeAt(6) + await rejected(() => f.journal.checkpoint(authority, first)); assert.equal(f.committed.length, 1); assert.equal(f.stats().rollbacks, 1) +} +{ + const f = fixture(); f.failAudit(); await rejected(() => f.journal.claim(authority)); assert.equal(f.committed.length, 0) +} +{ + const f = fixture(); f.loseCommitAck(); await rejected(() => f.journal.claim(authority)); assert.equal(f.committed.length, 1) + await rejected(() => f.journal.claim(authority)); assert.equal(f.committed.length, 1) // committed key stays spent +} +{ + const f = fixture(); await f.journal.claim(authority); f.loseCommitAck() + await rejected(() => f.journal.checkpoint(authority, first)); assert.equal(f.committed.length, 2) + await rejected(() => f.journal.checkpoint(authority, first)); assert.equal(f.committed.length, 2) +} +{ + const f = fixture(); await f.journal.claim(authority) + const otherActor = { ...actor, id: "another-journal-owner" } + const other = { ...authority, actor: otherActor, ownerProof: proof(otherActor) } + // Stable keys are actor-scoped, while every stage still binds that actor's + // own committed claim and every authority proof. + await f.journal.claim(other); await f.journal.checkpoint(other, first) + assert.equal(f.committed.length, 3) + assert.equal(f.committed[2].actorId, otherActor.id) +} +console.log("REST existing certificate journal: strict metadata, actor/key no-replay, exact committed claim binding, monotonic owned-file stages, late-revocation rollback and lost COMMIT acknowledgement passed") diff --git a/backend/src/services/ipsec-existing-certificate-export-rest-journal.ts b/backend/src/services/ipsec-existing-certificate-export-rest-journal.ts new file mode 100644 index 00000000..b873c99f --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest-journal.ts @@ -0,0 +1,138 @@ +import { createHash } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import type { PoolClient } from "pg" +import type { IpsecExistingCertificateExportRestAuthority } from "./ipsec-existing-certificate-export-rest.js" + +export type { IpsecExistingCertificateExportRestAuthority } from "./ipsec-existing-certificate-export-rest.js" +type Client = Pick +export interface IpsecExistingCertificateExportRestJournalDependencies { + transaction(work: (client: Client) => Promise): Promise + audit(client: Client, actor: string, action: string, connection: null, payload: unknown): Promise + authorize(authority: IpsecExistingCertificateExportRestAuthority): Promise +} +export interface IpsecExistingCertificateExportRestJournalCheckpoint { + completedCommands: number + afterProof: string + file?: { id: string; name: string; size: number } +} +export class IpsecExistingCertificateExportRestJournalError extends Error { + readonly code = "STATE_CONFLICT" + readonly statusCode = 409 + constructor() { super("REST export journal refused; no automatic replay") } +} +function fail(): never { throw new IpsecExistingCertificateExportRestJournalError() } +const digest = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +const nativeId = z.string().regex(/^\*[a-z0-9]+$/i).max(100) +const actorSchema = z.object({ id: z.string().min(1).max(200), email: z.string(), name: z.string(), apps: z.array(z.string()).max(1000), + permissions: z.array(z.string()).max(1000), isAdmin: z.boolean().optional(), expiresAt: z.number().int().positive().safe(), issuedAt: z.number().int().nonnegative().safe() }).strict() +const selectionSchema = z.object({ serverId: z.number().int().positive().safe(), + selector: z.discriminatedUnion("kind", [z.object({ kind: z.literal("client"), clientId: z.string().min(1).max(256) }).strict(), + z.object({ kind: z.literal("certificate"), name: z.string().min(1).max(256) }).strict()]), + privateKeyExport: z.literal(true), caPrivateKeyExport: z.boolean() }).strict() +const authoritySchema = z.object({ transport: z.literal("rest"), actor: actorSchema, ownerProof: digest, requestKey: uuid, + deadlineAt: z.number().int().positive().safe(), request: selectionSchema, receiptId: uuid, certificateId: nativeId, + certificateName: z.string().min(1).max(256), role: z.enum(["ca", "server", "client", "other"]), + sourceProof: digest, expectedProof: digest, referenceProof: digest, requestProof: digest }).strict() +const ownedFile = z.object({ id: nativeId, name: z.string(), size: z.number().int().positive().max(61438) }).strict() +const checkpointSchema = z.discriminatedUnion("completedCommands", [ + z.object({ completedCommands: z.literal(1), afterProof: digest, file: ownedFile }).strict(), + z.object({ completedCommands: z.literal(2), afterProof: digest }).strict(), +]) +const identitySchema = z.object({ receiptId: uuid, requestKey: uuid, serverId: z.number().int().positive().safe(), certificateId: nativeId, + role: z.enum(["ca", "server", "client", "other"]), ownerProof: digest, requestProof: digest, referenceProof: digest, + sourceProof: digest, expectedProof: digest }).strict() +const firstCheckpointSchema = identitySchema.extend({ completedCommands: z.literal(1), afterProof: digest, + file: z.object({ id: nativeId, size: z.number().int().positive().max(61438) }).strict() }).strict() +function canonical(value: unknown): string { + if (Array.isArray(value)) return "[" + value.map(canonical).join(",") + "]" + if (value && typeof value === "object") return "{" + Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, entry]) => JSON.stringify(key) + ":" + canonical(entry)).join(",") + "}" + return JSON.stringify(value) ?? "null" +} +const fingerprint = (value: unknown) => createHash("sha256").update(canonical(value)).digest("hex") +function freeze(value: T): T { + if (value && typeof value === "object") { for (const child of Object.values(value)) freeze(child); Object.freeze(value) } + return value +} +function authority(raw: IpsecExistingCertificateExportRestAuthority): IpsecExistingCertificateExportRestAuthority { + const parsed = authoritySchema.safeParse(raw) + if (!parsed.success) fail() + const a = parsed.data, now = Date.now() + if (a.deadlineAt <= now || a.actor.expiresAt <= now || a.deadlineAt > a.actor.expiresAt || a.actor.issuedAt > now || + a.ownerProof !== fingerprint(a.actor) || a.requestProof !== fingerprint(a.request)) fail() + return freeze(a) +} +function identity(a: IpsecExistingCertificateExportRestAuthority) { + return identitySchema.parse({ receiptId: a.receiptId, requestKey: a.requestKey, serverId: a.request.serverId, + certificateId: a.certificateId, role: a.role, ownerProof: a.ownerProof, requestProof: a.requestProof, + referenceProof: a.referenceProof, sourceProof: a.sourceProof, expectedProof: a.expectedProof }) +} +const claimAction = "ipsec.existing.export.rest.claim", checkpointAction = "ipsec.existing.export.rest.checkpoint" + +/** Inert mandatory journal: dependencies own transactions and authorization. + * Only actor/key serialization and metadata reach SQL; no private material, + * transport principal, actor PII or certificate names enter audit payloads. */ +export function createIpsecExistingCertificateExportRestJournal(deps: IpsecExistingCertificateExportRestJournalDependencies) { + async function authorize(a: IpsecExistingCertificateExportRestAuthority) { + if (a.deadlineAt <= Date.now() || !a.actor.expiresAt || a.actor.expiresAt <= Date.now()) fail() + await deps.authorize(a) + if (a.deadlineAt <= Date.now() || a.actor.expiresAt <= Date.now()) fail() + } + async function locked(a: IpsecExistingCertificateExportRestAuthority, work: (client: Client) => Promise) { + await authorize(a) + return deps.transaction(async client => { + await client.query("SET LOCAL lock_timeout='2s'") + await client.query("SET LOCAL statement_timeout='5s'") + await client.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [`ipsec-existing-export-rest:${a.actor.id}:${a.requestKey}`]) + await authorize(a) + const result = await work(client) + await authorize(a) + return result + }) + } + async function rows(client: Client, a: IpsecExistingCertificateExportRestAuthority, action: string) { + const result = await client.query(`SELECT CASE WHEN octet_length(payload::text)<=8192 THEN payload ELSE NULL END AS payload + FROM mcp_audit WHERE actor_id=$1 AND connection_id IS NULL AND action=$2 AND payload->>'requestKey'=$3 ORDER BY id LIMIT 2`, + [a.actor.id, action, a.requestKey]) + if (!Array.isArray(result.rows) || result.rows.length > 2) fail() + return result.rows as { payload: unknown }[] + } + return { + async claim(raw: IpsecExistingCertificateExportRestAuthority): Promise { + try { + const a = authority(raw), payload = identity(a) + await locked(a, async client => { + // Any committed claim spends the actor/key, even with another receipt + // or changed payload. An uncertain COMMIT must never be replayed. + if ((await rows(client, a, claimAction)).length) fail() + await deps.audit(client, a.actor.id, claimAction, null, payload) + }) + } catch { fail() } + }, + async checkpoint(raw: IpsecExistingCertificateExportRestAuthority, rawValue: IpsecExistingCertificateExportRestJournalCheckpoint): Promise { + try { + const a = authority(raw), parsed = checkpointSchema.safeParse(rawValue) + if (!parsed.success) fail() + const value = freeze(parsed.data), expected = identity(a) + if (value.completedCommands === 1 ? value.file.name !== `mm-ipsec-existing-${a.receiptId}.p12` : value.afterProof !== a.expectedProof) fail() + await locked(a, async client => { + const claims = await rows(client, a, claimAction) + if (claims.length !== 1 || !identitySchema.safeParse(claims[0].payload).success || !isDeepStrictEqual(claims[0].payload, expected)) fail() + const previous = await rows(client, a, checkpointAction) + if (value.completedCommands === 1) { + if (previous.length) fail() + } else { + if (previous.length !== 1) fail() + const first = firstCheckpointSchema.safeParse(previous[0].payload) + if (!first.success) fail() + const { completedCommands: _completedCommands, afterProof: _afterProof, file: _file, ...priorIdentity } = first.data + if (!isDeepStrictEqual(priorIdentity, expected)) fail() + } + await deps.audit(client, a.actor.id, checkpointAction, null, { ...expected, + completedCommands: value.completedCommands, afterProof: value.afterProof, + ...(value.completedCommands === 1 ? { file: { id: value.file.id, size: value.file.size } } : {}) }) + }) + } catch { fail() } + }, + } +} diff --git a/backend/src/services/ipsec-existing-certificate-export-rest-production.test.ts b/backend/src/services/ipsec-existing-certificate-export-rest-production.test.ts new file mode 100644 index 00000000..2aa2892d --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest-production.test.ts @@ -0,0 +1,42 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { McpError } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED } from "./ipsec-existing-certificate-export-model.js" + +const originalQuery = pool.query, originalConnect = pool.connect, originalClient = MikrotikClient.fromServer, originalFetch = globalThis.fetch +let sql = 0, native = 0, network = 0 +pool.query = ((statement: string) => { + sql++ + assert.equal(statement.startsWith("SELECT invalid_before FROM mcp_owner_revocations"), true) + throw new McpError("OWNER_CLAIMS_REVOKED", "Synthetic owner revoked", 401) +}) as never +pool.connect = (() => { throw Error("Real SQL transaction forbidden") }) as never +MikrotikClient.fromServer = (() => { native++; throw Error("Real native client forbidden") }) as never +globalThis.fetch = (async () => { network++; throw Error("Real network forbidden") }) as typeof fetch +try { + const { createIpsecExistingCertificateExportRestProductionAdapter } = await import("./ipsec-existing-certificate-export-rest-production.js") + const actor: AuthUser = { id: "synthetic-owner", email: "fixture@example.test", name: "Fixture", apps: ["mm"], permissions: ["mm:network:write"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 600000 } + const request = { serverId: 1, selector: { kind: "certificate", name: "ca" }, privateKeyExport: true, caPrivateKeyExport: true, passphrase: "NEVER_PRIVATE_INPUT" } + const rejects = (code: string) => (error: unknown) => error instanceof McpError && error.code === code && !error.message.includes("NEVER_") + for (let i = 0; i < 2; i++) { + const prior = sql, adapter = createIpsecExistingCertificateExportRestProductionAdapter() + assert.equal(sql, prior); assert.equal(native, 0); assert.equal(network, 0) + for (const denied of [{ ...actor, permissions: [] }, { ...actor, apps: ["other"] }]) await assert.rejects(adapter.prepare(denied, request, randomUUID()), rejects("PERMISSION_DENIED")) + // The transport bounds its deadline by JWT expiry before actor lookup. + await assert.rejects(adapter.prepare({ ...actor, expiresAt: Date.now() - 1000 }, request, randomUUID()), rejects("READ_CANCELLED")) + await assert.rejects(adapter.prepare(actor, {}, "invalid")) + await assert.rejects(adapter.prepare(actor, { ...request, privateKeyExport: false }, randomUUID())) + await assert.rejects(adapter.execute({}, actor), rejects("STATE_CONFLICT")) + assert.equal(sql, prior) + await assert.rejects(adapter.prepare(actor, request, randomUUID()), rejects("OWNER_CLAIMS_REVOKED")) + assert.equal(sql, prior + 1) + assert.equal(native, 0); assert.equal(network, 0) + } + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) + console.log("Activated REST existing-IPsec assembly: import/construction inert; invalid actor/intent/forged token and revoked owner reject before native, material or writes; only intercepted owner-revocation SQL") +} finally { + pool.query = originalQuery; pool.connect = originalConnect; MikrotikClient.fromServer = originalClient; globalThis.fetch = originalFetch +} diff --git a/backend/src/services/ipsec-existing-certificate-export-rest-production.ts b/backend/src/services/ipsec-existing-certificate-export-rest-production.ts new file mode 100644 index 00000000..2e865720 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest-production.ts @@ -0,0 +1,54 @@ +import { randomBytes } from "node:crypto" +import { hasPermission } from "../lib/permissions.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { audit, transaction } from "../modules/mcp/store.js" +import { McpError, privateSecretFingerprint } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite, withConfigurationActor } from "./config-mutation-policy.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED } from "./ipsec-existing-certificate-export-model.js" +import { ipsecExistingCertificateExportProof, readIpsecExistingCertificateExportState } from "./ipsec-existing-certificate-export.js" +import { ipsecPeerDeleteSourceProof } from "./ipsec-peer-delete.js" +import { createIpsecExistingCertificateExportRestAdapter, type IpsecExistingCertificateExportRestAuthority } from "./ipsec-existing-certificate-export-rest.js" +import { createIpsecExistingCertificateExportRestJournal, IpsecExistingCertificateExportRestJournalError } from "./ipsec-existing-certificate-export-rest-journal.js" + +function activation() { + if (!IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED) throw new McpError("APPROVAL_REQUIRED", "REST-экспорт существующих сертификатов/CA выключен", 409) +} +async function current(authority: IpsecExistingCertificateExportRestAuthority) { + const actor = authority.actor + if (!actor.expiresAt || actor.expiresAt <= Date.now() || !actor.apps.includes("mm") || !hasPermission(actor.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет актуального допуска REST-экспорта", 403) + await assertPortalClaimsCurrent(actor) +} + +/** Explicit assembly: importing/constructing it performs no I/O. + * A stable actor-scoped request key is spent durably before native I/O; + * neither a lost COMMIT acknowledgement nor a new invocation replays that key. */ +export function createIpsecExistingCertificateExportRestProductionAdapter() { + const journal = createIpsecExistingCertificateExportRestJournal({ transaction: work => transaction(client => work(client)), audit, authorize: current }) + const journalCall = async (work: () => Promise) => { + try { return await work() } + catch (error) { + if (error instanceof IpsecExistingCertificateExportRestJournalError) throw new McpError(error.code, error.message, error.statusCode) + throw error + } + } + const adapter = createIpsecExistingCertificateExportRestAdapter({ + server: getServerRowById, + sourceProof: ipsecPeerDeleteSourceProof, + read: readIpsecExistingCertificateExportState, + proof: ipsecExistingCertificateExportProof, + privateProof: privateSecretFingerprint, + checkClaims: assertPortalClaimsCurrent, + client: MikrotikClient.fromServer, + generatePassphrase: () => randomBytes(18).toString("base64url"), + boundary: (a, work) => withConfigurationActor(a.actor.id, () => withApplicationMutationLock(() => withConfigMutationLock([a.request.serverId], () => executeGuardedApplicationWrite({ kind: "certificate", id: a.certificateId }, [a.request.serverId], work)))), + claim: a => journalCall(() => journal.claim(a)), + checkpoint: (a, value) => journalCall(() => journal.checkpoint(a, value)), + publish: async (a, bundle, _afterProof, authorize) => { await current(a); await authorize(); return bundle }, + }) + const prepare: typeof adapter.prepare = async (...args) => { activation(); return adapter.prepare(...args) } + const execute: typeof adapter.execute = async (...args) => { activation(); return adapter.execute(...args) } + return { prepare, execute } +} diff --git a/backend/src/services/ipsec-existing-certificate-export-rest.test.ts b/backend/src/services/ipsec-existing-certificate-export-rest.test.ts new file mode 100644 index 00000000..9d2f9db4 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest.test.ts @@ -0,0 +1,166 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { ipsecManagedComment, ipsecUserComment } from "./ipsec-config.js" +import { ipsecPeerDeletePaths } from "./ipsec-peer-delete.js" +import { ipsecUserUpdateValue } from "./ipsec-user-update.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, type IpsecExistingCertificateExportState } from "./ipsec-existing-certificate-export-model.js" +import { createIpsecExistingCertificateExportRestAdapter, type IpsecExistingCertificateExportRestAdapterDependencies } from "./ipsec-existing-certificate-export-rest.js" + +const secret = "NEVER_INLINE_PRIVATE_PASSPHRASE", bytes = "NEVER_PRIVATE_PKCS12" +const source = "a".repeat(64), server = { id: 1, host: "router.example.test" } as ServerRow +const proof = async (state: IpsecExistingCertificateExportState) => fingerprint(ipsecUserUpdateValue(state)) +const base = (): IpsecExistingCertificateExportState => ({ source, proof: "unused", protections: [], catalogs: { + ...Object.fromEntries([...ipsecPeerDeletePaths, "/file"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", certificate: "ipsec-server", comment: ipsecManagedComment() }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "remote-certificate": "client-cert", "mode-config": "ipsec-vpn", comment: ipsecUserComment("Alice") }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "ipsec-vpn", "static-dns": "192.0.2.53" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign,crl-sign", authority: "true", "private-key": "true" }, { ".id": "*s", name: "ipsec-server", "common-name": "vpn.example.test", "key-usage": "tls-server" }, { ".id": "*c", name: "client-cert", "common-name": "Alice", "key-usage": "tls-client", "private-key": "true" }, { ".id": "*o", name: "foreign-other", "common-name": "Other" }], + "/file": [{ ".id": "*f", name: "foreign.p12", size: "5" }], +} }) +const request = () => ({ serverId: 1, selector: { kind: "client", clientId: "identity:*i" }, privateKeyExport: true, caPrivateKeyExport: false, passphrase: ` ${secret} ` }) +function fixture() { + const state = base() + let now = 100000, sourceCurrent = source, revoked = false, materialChanged = false, mode = "normal", reads = 0, generated = 0 + const actor: AuthUser = { id: "real-rest-owner", email: "owner@example.test", name: "Owner", apps: ["mm"], permissions: ["mm:network:write"], issuedAt: now - 1000, expiresAt: now + 600000 } + const commands: string[] = [], audits: unknown[] = [], claims = new Set() + let downloaded: Buffer | undefined + const deps: IpsecExistingCertificateExportRestAdapterDependencies = { + server: async () => server, + sourceProof: async () => sourceCurrent, + proof, + checkClaims: async current => { assert.equal(current.id, actor.id); if (revoked) throw new McpError("OWNER_CLAIMS_REVOKED", secret, 401) }, + privateProof: async (value, domain) => createHmac("sha256", "inert-test-private-key").update(domain).update(value).update(materialChanged ? "changed" : "").digest("hex"), + read: async (_server, authorize, control) => { + assert.ok(control?.signal); assert.ok(control?.deadlineAt) + await authorize(); reads++ + if (mode === "prepareDrift" && reads === 2) state.catalogs["/certificate"][0].comment = "concurrent change" + const result = structuredClone(state); result.proof = await proof(result) + await authorize(); return result + }, + boundary: async (a, work) => { assert.equal(a.transport, "rest"); assert.equal("principal" in a, false); assert.equal("policyProof" in a, false); return work() }, + claim: async a => { + assert.equal(a.actor.id, actor.id) + const identity = `${a.actor.id}:${a.requestKey}` + if (claims.has(identity)) throw new McpError("STATE_CONFLICT", "already claimed", 409) + if (mode === "claimFailure") throw Error(secret) + claims.add(identity); audits.push({ claim: a }) + }, + checkpoint: async (a, progress) => { audits.push({ receiptId: a.receiptId, progress }); if (mode === "checkpointFailure") throw Error(secret); if (mode === "deadlineAfterExport") now += 180001 }, + publish: async (_a, bundle, _after, authorize) => { await authorize(); if (mode === "publishFailure") throw Error(secret); return bundle }, + generatePassphrase: () => { generated++; return "GENERATED_PRIVATE_PASSPHRASE" }, + clock: () => now, + client: () => ({ + post: async (path: string, fields: Record, _timeout: unknown, _signal: unknown, beforeDispatch: () => Promise) => { + await beforeDispatch(); commands.push(path) + state.catalogs["/file"].push({ ".id": "*new", name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(bytes)) }) + if (mode === "collateral") state.catalogs["/certificate"][0].comment = "unexpected mutation" + if (mode === "revokeAfterExport") revoked = true + if (mode === "exportLostAck") throw Error(secret) + return {} + }, + downloadOwnedFile: async (_file: unknown, control: { authorize: () => Promise }) => { await control.authorize(); if (mode === "downloadFailure") throw Error(secret); downloaded = Buffer.from(bytes); return downloaded }, + delete: async (path: string, _timeout: unknown, beforeDispatch: () => Promise) => { await beforeDispatch(); commands.push(path); state.catalogs["/file"] = state.catalogs["/file"].filter(row => row[".id"] !== "*new"); if (mode === "cleanupLostAck") throw Error(secret) }, + }) as never, + } + return { actor, deps, adapter: createIpsecExistingCertificateExportRestAdapter(deps), commands, audits, claims, + state: () => state, downloaded: () => downloaded, generated: () => generated, + setMode: (next: string) => { mode = next }, revoke: () => { revoked = true }, changeSource: () => { sourceCurrent = "f".repeat(64) }, changeMaterial: () => { materialChanged = true }, advance: (ms: number) => { now += ms }, + } +} +let cases = 0 +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Real SQL forbidden") }) as never +pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +try { + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) + for (const target of ["client", "client-cert", "ipsec-server", "foreign-other", "ca"]) { + const f = fixture(), raw = target === "client" ? request() : { ...request(), selector: { kind: "certificate", name: target }, caPrivateKeyExport: target === "ca" } + const before = structuredClone(f.state()), key = randomUUID(), token = await f.adapter.prepare(f.actor, raw, key) + assert.equal(Object.isFrozen(token), true); assert.deepEqual(JSON.parse(JSON.stringify(token)), {}) + assert.equal(JSON.stringify(token).includes(secret), false); assert.equal(JSON.stringify(token).includes(key), false) + const result = await f.adapter.execute(token, f.actor) + assert.equal(result.verified, true); assert.equal(result.automaticReplay, false); assert.equal(result.forwardingVerified, false) + assert.equal(result.publication.contentB64, Buffer.from(bytes).toString("base64")); assert.equal(result.publication.passphrase, secret) + assert.equal(result.publication.serverEndpoint, "vpn.example.test"); assert.ok(result.publication.sswanContent); assert.ok(result.publication.instructions?.includes("192.0.2.53")) + assert.equal("artifactId" in result, false); assert.equal("ipsecContentReturned" in result, false); assert.equal("humanDownloadOnly" in result, false) + assert.equal(f.commands.length, 2); assert.deepEqual(f.state(), before); assert.ok(f.downloaded()?.every(byte => byte === 0)) + assert.equal(JSON.stringify(f.audits).includes(secret), false) + await assert.rejects(() => f.adapter.execute(token, f.actor)); assert.equal(f.commands.length, 2) + const replay = await f.adapter.prepare(f.actor, raw, key) + await assert.rejects(() => f.adapter.execute(replay, f.actor)); assert.equal(f.commands.length, 2) + cases++ + } + for (const passphrase of [" ", " 123456 "]) { + const f = fixture(), token = await f.adapter.prepare(f.actor, { ...request(), passphrase }, randomUUID()) + const result = await f.adapter.execute(token, f.actor) + assert.equal(result.publication.passphrase, "GENERATED_PRIVATE_PASSPHRASE"); assert.equal(f.generated(), 1); assert.equal(f.commands.length, 2); cases++ + } + { + const f = fixture(), raw = request(), originalActor = structuredClone(f.actor), token = await f.adapter.prepare(f.actor, raw, randomUUID()) + raw.selector.clientId = "identity:*wrong"; raw.passphrase = "CHANGED_PRIVATE_PASSPHRASE"; f.actor.permissions.push("mm:network:read") + // Prepared selection/material/actor are immutable snapshots, independent of caller mutation. + const result = await f.adapter.execute(token, originalActor) + assert.equal(result.certificateId, "*c"); assert.equal(result.publication.passphrase, secret); cases++ + } + for (const variation of ["json", "crossFactory", "actor", "permissions", "issuedAt", "expiry", "revoke", "source", "material", "target", "protection", "deadline", "abort", "invalidTimeout"]) { + const f = fixture(), token = await f.adapter.prepare(f.actor, request(), randomUUID()), controller = new AbortController() + let candidate: unknown = token, actor = f.actor, adapter = f.adapter + if (variation === "json") candidate = JSON.parse(JSON.stringify(token)) + if (variation === "crossFactory") adapter = createIpsecExistingCertificateExportRestAdapter(f.deps) + if (variation === "actor") actor = { ...f.actor, id: "other-owner" } + if (variation === "permissions") actor = { ...f.actor, permissions: [] } + if (variation === "issuedAt") actor = { ...f.actor, issuedAt: f.actor.issuedAt! + 1 } + if (variation === "expiry") actor = { ...f.actor, expiresAt: 1 } + if (variation === "revoke") f.revoke() + if (variation === "source") f.changeSource() + if (variation === "material") f.changeMaterial() + if (variation === "target") f.state().catalogs["/certificate"][2].name = "changed" + if (variation === "protection") f.state().protections.push({ object_ref: { kind: "certificate", id: "*c", protection: "deny" } }) + if (variation === "deadline") f.advance(180000) + if (variation === "abort") controller.abort() + await assert.rejects(() => adapter.execute(candidate, actor, { signal: controller.signal, ...(variation === "invalidTimeout" ? { timeoutMs: Infinity } : {}) }), (error: unknown) => error instanceof Error && !error.message.includes(secret)) + assert.equal(f.commands.length, 0); cases++ + } + for (const variation of ["caFlag", "short", "long", "unknown", "generateLie", "key", "apps", "noIssuedAt", "noExpiry", "expired", "future", "revoked", "deny", "protected", "prepareDrift", "abort"]) { + const f = fixture(), raw: Record = request(), controller = new AbortController() + let key: string = randomUUID(), actor = f.actor + if (variation === "caFlag") raw.selector = { kind: "certificate", name: "ca" } + if (variation === "short") raw.passphrase = "short" + if (variation === "long") raw.passphrase = "x".repeat(16385) + if (variation === "unknown") raw.principal = { connectionId: randomUUID() } + if (variation === "generateLie") raw.passphrase = { kind: "generate" } + if (variation === "key") key = "not-a-request-key" + if (variation === "apps") actor = { ...f.actor, apps: [] } + if (variation === "noIssuedAt") actor = { ...f.actor, issuedAt: undefined } + if (variation === "noExpiry") actor = { ...f.actor, expiresAt: undefined } + if (variation === "expired") actor = { ...f.actor, expiresAt: 99999 } + if (variation === "future") actor = { ...f.actor, issuedAt: 100001 } + if (variation === "revoked") f.revoke() + if (variation === "deny" || variation === "protected") f.state().protections.push({ object_ref: { kind: "certificate", id: "*c", protection: variation === "deny" ? "deny" : "approval" } }) + if (variation === "prepareDrift") f.setMode("prepareDrift") + if (variation === "abort") controller.abort() + await assert.rejects(() => f.adapter.prepare(actor, raw, key, { signal: controller.signal }), (error: unknown) => error instanceof Error && !error.message.includes(secret)) + assert.equal(f.commands.length, 0); cases++ + } + for (const [mode, count] of [["claimFailure", 0], ["exportLostAck", 1], ["collateral", 1], ["revokeAfterExport", 1], ["checkpointFailure", 1], ["deadlineAfterExport", 1], ["downloadFailure", 1], ["cleanupLostAck", 2], ["publishFailure", 2]] as const) { + const f = fixture(), token = await f.adapter.prepare(f.actor, request(), randomUUID()) + f.setMode(mode) + await assert.rejects(() => f.adapter.execute(token, f.actor), (error: unknown) => error instanceof Error && !error.message.includes(secret)) + assert.equal(f.commands.length, count, mode) + await assert.rejects(() => f.adapter.execute(token, f.actor)); assert.equal(f.commands.length, count) + if (f.downloaded()) assert.ok(f.downloaded()!.every(byte => byte === 0)) + assert.equal(JSON.stringify(f.audits).includes(secret), false); cases++ + } + { + const f = fixture(), token = await f.adapter.prepare(f.actor, request(), randomUUID()) + const first = f.adapter.execute(token, f.actor) + await assert.rejects(() => f.adapter.execute(token, f.actor)) + await first; assert.equal(f.commands.length, 2); cases++ + } + console.log("Synthetic REST existing certificate export:", JSON.stringify({ cases, actualSharedExecutor: true, syntheticDependenciesOnly: true, bothSelectors: true, roles: 4, activated: true, realRouterCalls: 0, realSql: 0 })) +} finally { pool.query = oldQuery; pool.connect = oldConnect } + diff --git a/backend/src/services/ipsec-existing-certificate-export-rest.ts b/backend/src/services/ipsec-existing-certificate-export-rest.ts new file mode 100644 index 00000000..5ecc2136 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export-rest.ts @@ -0,0 +1,142 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import type { IpsecCertBundle } from "@mmapp/contracts/ipsec" +import { hasPermission, type AuthUser } from "../lib/permissions.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { createIpsecExistingCertificateExportExecutor, IpsecExistingCertificateExportExecutionError, type IpsecExistingCertificateExportBinding, type IpsecExistingCertificateExportTransportDependencies } from "./ipsec-existing-certificate-export.js" +import { ipsecExistingCertificateExportSelectionSchema, planIpsecExistingCertificateExportSelection, validateIpsecExistingCertificateExportState } from "./ipsec-existing-certificate-export-model.js" + +export const ipsecExistingCertificateExportRestRequestSchema = ipsecExistingCertificateExportSelectionSchema.extend({ passphrase: z.string().min(8).max(16384) }).strict() +export interface IpsecExistingCertificateExportRestAuthority extends IpsecExistingCertificateExportBinding { + transport: "rest" + actor: AuthUser + ownerProof: string + requestKey: string + deadlineAt: number +} +export interface IpsecExistingCertificateExportRestAdapterDependencies extends Omit, "access" | "material"> { + /** Mandatory purpose-separated keyed proof; never a public password hash. */ + privateProof: (value: string, domain: string) => Promise + checkClaims: (actor: AuthUser) => Promise +} +declare const tokenBrand: unique symbol +export type IpsecExistingCertificateExportRestToken = Readonly<{ [tokenBrand]: true }> +type Options = { signal?: AbortSignal; timeoutMs?: number } +const digest = z.string().regex(/^[a-f0-9]{64}$/) +const actorSchema = z.object({ id: z.string().min(1).max(200), email: z.string(), name: z.string(), apps: z.array(z.string()).max(1000), permissions: z.array(z.string()).max(1000), isAdmin: z.boolean().optional(), expiresAt: z.number().int().positive(), issuedAt: z.number().int().nonnegative() }).strict() +const protectionSchema = z.array(z.object({ object_ref: z.object({ kind: z.string().min(1), id: z.string().min(1), protection: z.string().optional() }).passthrough() }).strict()).max(1000) +const fail = (code = "STATE_CONFLICT", status = 409) => new McpError(code, "Допуск или состояние существующего REST-экспорта не подтверждены; повтор запрещён", status) +const freeze = (value: T): T => { if (value && typeof value === "object") { for (const child of Object.values(value)) freeze(child); Object.freeze(value) }; return value } +function sanitized(error: unknown): never { + if (error instanceof IpsecExistingCertificateExportExecutionError) throw error + throw error instanceof McpError ? fail(error.code, error.statusCode) : fail("STATE_UNAVAILABLE", 502) +} + +/** Closed injectable transport. No registration, activation, production defaults, + * MCP principal or serializable secret capability. Only publication gets bytes. */ +export function createIpsecExistingCertificateExportRestAdapter(deps: IpsecExistingCertificateExportRestAdapterDependencies) { + const clock = deps.clock ?? Date.now + type Entry = { authority: IpsecExistingCertificateExportRestAuthority; passphrase: string | undefined; signal: AbortSignal } + const prepared = new WeakMap() + const checkActor = async (actor: AuthUser, expected?: string) => { + if (!actor.apps.includes("mm") || !hasPermission(actor.permissions, "mm:network:write") || !actor.expiresAt || actor.expiresAt <= clock() || actor.issuedAt === undefined || actor.issuedAt > clock() || (expected && fingerprint(actor) !== expected)) throw fail("PERMISSION_DENIED", 403) + await deps.checkClaims(actor) + if (actor.expiresAt <= clock()) throw fail("PERMISSION_DENIED", 403) + } + const timing = (deadlineAt: number, signal: AbortSignal) => { + const check = () => { if (signal.aborted || clock() >= deadlineAt) throw fail("READ_CANCELLED") } + const run = async (work: () => Promise): Promise => { + check() + let abort!: () => void + const cancelled = new Promise((_resolve, reject) => { abort = () => reject(fail("READ_CANCELLED")); signal.addEventListener("abort", abort, { once: true }) }) + try { const value = await Promise.race([work(), cancelled]); check(); return value } finally { signal.removeEventListener("abort", abort) } + } + return { check, run } + } + const materialProof = (authority: Pick, passphrase: string) => deps.privateProof(JSON.stringify({ actorId: authority.actor.id, requestKey: authority.requestKey, request: authority.request, passphrase }), "ipsec-existing-export-rest-material-v1") + return { + async prepare(rawActor: AuthUser, rawRequest: unknown, rawRequestKey: string, options: Options = {}): Promise { + let passphrase: string | undefined + try { + const actor = freeze(actorSchema.parse(rawActor)) + const parsed = ipsecExistingCertificateExportRestRequestSchema.parse(rawRequest) + const { passphrase: providedPassphrase, ...selection } = parsed + const request = freeze(ipsecExistingCertificateExportSelectionSchema.parse(selection)) + // Selection is strict: remove the transport-only material explicitly. + passphrase = providedPassphrase + const requestKey = z.string().uuid().parse(rawRequestKey), receiptId = randomUUID() + const timeoutMs = options.timeoutMs ?? 180000 + if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 180000) throw fail("INVALID_INPUT", 400) + const deadlineAt = Math.min(clock() + timeoutMs, actor.expiresAt) + const remaining = deadlineAt - clock() + if (remaining <= 0) throw fail("READ_CANCELLED") + const timer = AbortSignal.timeout(remaining), signal = options.signal ? AbortSignal.any([options.signal, timer]) : timer + const t = timing(deadlineAt, signal), ownerProof = fingerprint(actor) + const access = async () => { await t.run(() => checkActor(actor, ownerProof)) } + await access() + const server = await t.run(() => deps.server(request.serverId)) + if (!server) throw fail("NOT_FOUND", 404) + const sourceProof = await t.run(() => deps.sourceProof(server)) + digest.parse(sourceProof) + const source = async () => { + await access() + const fresh = await t.run(() => deps.server(request.serverId)) + if (!fresh || await t.run(() => deps.sourceProof(fresh)) !== sourceProof) throw fail() + } + const read = async () => { + await source() + const state = await t.run(() => deps.read(server, source, { signal, deadlineAt })) + validateIpsecExistingCertificateExportState(state) + const protections = protectionSchema.parse(state.protections) + if (protections.some(row => row.object_ref.protection === "deny")) throw fail("PROTECTED_OBJECT") + if (protections.length) throw fail("APPROVAL_REQUIRED") + if (state.source !== sourceProof || state.proof !== await t.run(() => deps.proof(state))) throw fail() + await source() + return state + } + const before = await read(), plan = planIpsecExistingCertificateExportSelection(before, server, request, receiptId) + const referenceProof = await t.run(() => materialProof({ actor, requestKey, request }, passphrase!)) + digest.parse(referenceProof) + const final = await read() + if (final.proof !== before.proof) throw fail() + const authority: IpsecExistingCertificateExportRestAuthority = freeze({ transport: "rest", actor, ownerProof, requestKey, deadlineAt, request, receiptId, certificateId: plan.certificateId, certificateName: plan.certificateName, role: plan.role, sourceProof, expectedProof: before.proof, referenceProof, requestProof: fingerprint(request) }) + const token = Object.freeze({}) as IpsecExistingCertificateExportRestToken + prepared.set(token, { authority, passphrase, signal }) + passphrase = undefined + return token + } catch (error) { sanitized(error) } + finally { passphrase = undefined } + }, + async execute(token: unknown, rawActor: AuthUser, options: Options = {}) { + const entry = token && typeof token === "object" ? prepared.get(token) : undefined + if (!entry) throw fail() + // Consume synchronously, before authorization or any async work. An + // uncertain first attempt cannot race a second attempt or be retried. + prepared.delete(token as object) + try { + const currentActor = freeze(actorSchema.parse(rawActor)), a = entry.authority + if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs < 1 || options.timeoutMs > 180000)) throw fail("INVALID_INPUT", 400) + const remaining = a.deadlineAt - clock(), timeoutMs = options.timeoutMs === undefined ? remaining : Math.min(options.timeoutMs, remaining) + if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 180000) throw fail("READ_CANCELLED") + const signal = options.signal ? AbortSignal.any([entry.signal, options.signal]) : entry.signal + const t = timing(a.deadlineAt, signal) + const access = async (candidate: IpsecExistingCertificateExportRestAuthority) => { + t.check() + if (candidate !== a) throw fail() + await t.run(() => checkActor(currentActor, a.ownerProof)) + } + const execute = createIpsecExistingCertificateExportExecutor({ ...deps, access, + material: async (candidate, authorize) => { + await authorize() + if (candidate !== a || entry.passphrase === undefined) throw fail() + const referenceProof = await t.run(() => materialProof(a, entry.passphrase!)) + if (referenceProof !== a.referenceProof) throw fail() + return { passphrase: entry.passphrase, referenceProof } + }, + }, raw => { if (raw !== a) throw fail(); return a }) + return await execute(a, { signal, timeoutMs }) + } catch (error) { sanitized(error) } + finally { entry.passphrase = undefined } + }, + } +} diff --git a/backend/src/services/ipsec-existing-certificate-export.test.ts b/backend/src/services/ipsec-existing-certificate-export.test.ts new file mode 100644 index 00000000..2111e336 --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export.test.ts @@ -0,0 +1,160 @@ +import assert from "node:assert/strict" +import { randomUUID, createDecipheriv } from "node:crypto" +import { pool } from "../db/index.js" +import { fingerprint, seal, McpError } from "../modules/mcp/security.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import type { AuthUser } from "../lib/permissions.js" +import { ipsecPeerDeletePaths } from "./ipsec-peer-delete.js" +import { ipsecUserUpdateValue } from "./ipsec-user-update.js" +import { ipsecManagedComment, ipsecUserComment } from "./ipsec-config.js" +import { createIpsecExistingCertificateExportFoundation, exportIpsecExistingCertificateGuarded, IpsecExistingCertificateExportExecutionError, type IpsecExistingCertificateExportDependencies } from "./ipsec-existing-certificate-export.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, ipsecExistingCertificateExportRequestSchema, ipsecExistingCertificateExportSecretPurpose, planIpsecExistingCertificateExport, normalizeIpsecExistingExportPassphrase, projectIpsecExistingCertificateExport, type IpsecExistingCertificateExportAuthority, type IpsecExistingCertificateExportState } from "./ipsec-existing-certificate-export-model.js" +import { createIpsecExistingCertificateExportArtifact, humanIpsecExistingCertificateExportMetadata, readHumanIpsecExistingCertificateExportArtifact, verifyIpsecExistingCertificateExportArtifact, ipsecExistingCertificateExportArtifactReceipt, ipsecExistingCertificateExportArtifactScopeSchema } from "./ipsec-existing-certificate-export-artifact.js" + +type Row = Record +const source = "a".repeat(64), reference = "b".repeat(64), server = { id: 1, host: "router.example.test" } as ServerRow +const base = (): IpsecExistingCertificateExportState => ({ source, proof: "unused", protections: [], catalogs: { + ...Object.fromEntries([...ipsecPeerDeletePaths, "/file"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", certificate: "ipsec-server", comment: ipsecManagedComment() }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "remote-certificate": "client-cert", "mode-config": "ipsec-vpn", comment: ipsecUserComment("Alice") }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "ipsec-vpn", "static-dns": "192.0.2.53" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign,crl-sign", authority: "true", "private-key": "true", opaque: "NEVER_CA" }, { ".id": "*s", name: "ipsec-server", "common-name": "vpn.example.test", ca: "ca", "key-usage": "tls-server", opaque: "NEVER_SERVER" }, { ".id": "*c", name: "client-cert", "common-name": "Alice", ca: "ca", "key-usage": "tls-client", "private-key": "true", opaque: "NEVER_CLIENT" }, { ".id": "*o", name: "foreign-other", "common-name": "Other", opaque: "NEVER_OTHER" }], + "/file": [{ ".id": "*f", name: "foreign.p12", size: "5", opaque: "NEVER_FOREIGN_FILE" }], +} }) +const proof = async (state: IpsecExistingCertificateExportState) => fingerprint(ipsecUserUpdateValue(state)) +const request = ipsecExistingCertificateExportRequestSchema.parse({ serverId: 1, selector: { kind: "client", clientId: "identity:*i" }, privateKeyExport: true, passphrase: { kind: "reference", secretRef: randomUUID() } }) +const receiptId = randomUUID(), clientPlan = planIpsecExistingCertificateExport(base(), server, request, receiptId) +assert.equal(clientPlan.certificateId, "*c"); assert.equal(clientPlan.role, "client"); assert.equal(clientPlan.userName, "Alice"); assert.equal(clientPlan.serverEndpoint, "vpn.example.test"); assert.equal(clientPlan.dns, "192.0.2.53") +for (const name of ["client-cert", "ipsec-server", "foreign-other", "ca"]) { + const plan = planIpsecExistingCertificateExport(base(), server, { ...request, selector: { kind: "certificate", name }, caPrivateKeyExport: name === "ca" }, receiptId) + assert.equal(plan.certificateName, name); assert.equal(plan.commandCount, 2) +} +assert.throws(() => planIpsecExistingCertificateExport(base(), server, { ...request, selector: { kind: "certificate", name: "ca" } }, receiptId), (error: unknown) => error instanceof McpError && error.code === "APPROVAL_REQUIRED") +assert.throws(() => planIpsecExistingCertificateExport(base(), server, { ...request, selector: { kind: "client", clientId: "cert:*c" } }, receiptId)) +const psk = base(); psk.catalogs["/ip/ipsec/identity"][0]["auth-method"] = "pre-shared-key"; assert.throws(() => planIpsecExistingCertificateExport(psk, server, request, receiptId)) +const unmanaged = base(); unmanaged.catalogs["/ip/ipsec/identity"][0].comment = "foreign"; assert.throws(() => planIpsecExistingCertificateExport(unmanaged, server, request, receiptId)) +for (const key of ["passphraseValue", "psk", "privateKey", "unknown"]) assert.equal(ipsecExistingCertificateExportRequestSchema.safeParse({ ...request, [key]: "NEVER_RAW" }).success, false) +assert.equal(ipsecExistingCertificateExportRequestSchema.safeParse({ ...request, privateKeyExport: false }).success, false) +assert.notEqual(ipsecExistingCertificateExportSecretPurpose(1, request.selector), ipsecExistingCertificateExportSecretPurpose(2, request.selector)) +assert.notEqual(ipsecExistingCertificateExportSecretPurpose(1, request.selector), ipsecExistingCertificateExportSecretPurpose(1, { kind: "certificate", name: "client-cert" })) +let generated = 0 +const generate = () => { generated++; return "NEVER_GENERATED_PASSPHRASE" } +assert.equal(normalizeIpsecExistingExportPassphrase(" NEVER_PASSPHRASE ", generate), "NEVER_PASSPHRASE") +assert.equal(normalizeIpsecExistingExportPassphrase(" ", generate), "NEVER_GENERATED_PASSPHRASE") +assert.equal(normalizeIpsecExistingExportPassphrase(" 123456 ", generate), "NEVER_GENERATED_PASSPHRASE"); assert.equal(generated, 2) +assert.throws(() => normalizeIpsecExistingExportPassphrase("short", generate)) +const after = base(); after.catalogs["/file"].push({ ".id": "*new", name: clientPlan.filename, size: "7", type: "file", "opaque-default": "NEVER_NEW_FILE" }) +assert.equal(projectIpsecExistingCertificateExport(base(), clientPlan.filename, after).file.id, "*new") +for (const mutate of [(state: IpsecExistingCertificateExportState) => { state.catalogs["/file"].at(-1)!.size = "61439" }, (state: IpsecExistingCertificateExportState) => { state.catalogs["/file"].at(-1)![".id"] = "*f" }, (state: IpsecExistingCertificateExportState) => { state.catalogs["/file"].at(-1)!.name = "wrong.p12" }]) { const changed = structuredClone(after); mutate(changed); assert.throws(() => projectIpsecExistingCertificateExport(base(), clientPlan.filename, changed)) } + +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Real SQL forbidden") }) as never; pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +try { + let state = base(), mode = "normal", sourceCurrent = source, referenceCurrent = reference, revoked = false, generatedCount = 0, downloaded: Buffer | undefined, clockNow = 1000 + let delayNextRead = false, releaseDelayedFence: (() => void) | undefined + let releaseDownload: (() => void) | undefined, downloadStarted: (() => void) | undefined + const claims = new Set(), commands: Array<{ method: string; path: string }> = [], checkpoints: unknown[] = [], artifacts = new Map(), key = Buffer.alloc(32, 7) + const fakeBytes = "NEVER_SYNTHETIC_PRIVATE_PKCS12" + const authority: IpsecExistingCertificateExportAuthority = { principal: { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 600000).toISOString() }, ownerId: "fixture-owner", ownerProof: "c".repeat(64), policyProof: "d".repeat(64), generation: "fixture-generation", request, receiptId, certificateId: "*c", certificateName: "client-cert", role: "client", sourceProof: source, expectedProof: await proof(state), referenceProof: reference, requestProof: fingerprint(request), inputProof: "e".repeat(64), proofRevision: 1 } + const access = async (current: IpsecExistingCertificateExportAuthority) => { assert.equal(current.ownerId, authority.ownerId); if (revoked) throw new McpError("PERMISSION_DENIED", "synthetic revoked", 403) } + const deps: IpsecExistingCertificateExportDependencies = { + server: async () => server, sourceProof: async () => sourceCurrent, access, + material: async (_authority, authorize) => { await authorize(); return { passphrase: mode === "whitespace" ? " " : " NEVER_PASSPHRASE ", referenceProof: referenceCurrent } }, + read: async (_server, authorize) => { if (delayNextRead) { delayNextRead = false; await new Promise(resolve => { releaseDelayedFence = resolve }) }; if (mode === "deadlineRead") clockNow += 1000; await authorize(); const result = structuredClone(state); result.proof = await proof(result); await authorize(); return result }, proof, + boundary: async (_authority, work) => work(), + claim: async current => { if (claims.has(current.receiptId)) throw new McpError("STATE_CONFLICT", "Receipt already claimed", 409); claims.add(current.receiptId); if (mode === "lateSource") sourceCurrent = "f".repeat(64) }, + checkpoint: async (_authority, value) => { checkpoints.push(structuredClone(value)); if (mode === "deadlineAfterAck") clockNow += 1000; if (mode === "checkpointFailure") throw Error("synthetic checkpoint failure") }, + generatePassphrase: () => { generatedCount++; return "NEVER_GENERATED_PASSPHRASE" }, + clock: () => clockNow, + publish: async (_authority, bundle, afterProof, authorize) => { if (mode === "deadlinePublish") clockNow += 1000; await authorize(); assert.equal(afterProof, authority.expectedProof); const id = randomUUID(); artifacts.set(id, seal(JSON.stringify(bundle), key)); return { artifactId: id, downloadPath: `/api/mcp/artifacts/${id}`, humanDownloadOnly: true, ipsecContentReturned: false } }, + client: () => ({ + post: async (path: string, fields: Row, _timeout: unknown, _signal: unknown, beforeDispatch: () => Promise) => { + if (mode === "missingPostFence") return {} + if (mode === "parallelPostFence") { delayNextRead = true; await Promise.all([beforeDispatch().then(() => { commands.push({ method: "POST", path }) }), beforeDispatch()]); return {} } + await beforeDispatch(); if (mode === "duplicatePostFence") await beforeDispatch(); commands.push({ method: "POST", path }); assert.equal(path, "/certificate/export-certificate"); assert.equal(fields.type, "pkcs12"); assert.equal(fields.name, "client-cert"); assert.equal(fields["file-name"], clientPlan.exportPrefix) + if (mode !== "ignored") state.catalogs["/file"].push({ ".id": "*new", name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeBytes)), type: "file", opaque: "NEVER_NEW_FILE" }) + if (mode === "collateral") state.catalogs["/certificate"][0].opaque = "NEVER_CHANGED_CA" + if (mode === "revoked") revoked = true + if (mode === "secretRevoked") referenceCurrent = "f".repeat(64) + if (mode === "lost") throw Error("synthetic lost export ACK") + return {} + }, + downloadOwnedFile: async (file: { id: string; name: string; size: number }, control: { authorize: () => Promise }) => { + assert.deepEqual(file, { id: "*new", name: clientPlan.filename, size: Buffer.byteLength(fakeBytes) }) + await control.authorize() + if (mode === "downloadFailure") throw Error("synthetic download failed") + downloaded = Buffer.from(mode === "shortDownload" ? fakeBytes.slice(1) : fakeBytes) + await control.authorize() + if (mode === "postDownloadRevoke") revoked = true + if (mode === "lateDownload") await new Promise(resolve => { releaseDownload = resolve; downloadStarted!() }) + return downloaded + }, + delete: async (path: string, _timeout: unknown, beforeDispatch: () => Promise) => { if (mode === "missingDeleteFence") return; if (mode === "parallelDeleteFence") { delayNextRead = true; await Promise.all([beforeDispatch().then(() => { commands.push({ method: "DELETE", path }) }), beforeDispatch()]); return }; await beforeDispatch(); if (mode === "duplicateDeleteFence") await beforeDispatch(); commands.push({ method: "DELETE", path }); assert.equal(path, "/file/*new"); state.catalogs["/file"] = state.catalogs["/file"].filter(row => row[".id"] !== "*new"); if (mode === "cleanupLost") throw Error("synthetic lost cleanup ACK") }, + }) as never, + } + const run = createIpsecExistingCertificateExportFoundation(deps) + const reset = () => { state = base(); mode = "normal"; sourceCurrent = source; referenceCurrent = reference; revoked = false; claims.clear(); commands.length = 0; checkpoints.length = 0; downloaded = undefined; generatedCount = 0; artifacts.clear(); clockNow = 1000; delayNextRead = false; releaseDelayedFence = undefined } + for (const whitespace of [false, true]) { + reset(); if (whitespace) mode = "whitespace" + const receipt = await run(authority) + assert.equal(receipt.verified, true); assert.equal(receipt.commandCount, 2); assert.equal(receipt.completedCommands, 2); assert.equal(commands.length, 2); assert.deepEqual(state, base()); assert.equal(downloaded!.every(byte => byte === 0), true) + assert(!JSON.stringify(receipt).includes("NEVER_")); assert(!JSON.stringify(checkpoints).includes("NEVER_")); assert(!JSON.stringify([...artifacts.values()]).includes("NEVER_")) + const encrypted = Buffer.from(artifacts.get(receipt.artifactId)!, "base64"), decipher = createDecipheriv("aes-256-gcm", key, encrypted.subarray(0, 12)); decipher.setAuthTag(encrypted.subarray(12, 28)); const bundle = JSON.parse(Buffer.concat([decipher.update(encrypted.subarray(28)), decipher.final()]).toString()) + assert.equal(Buffer.from(bundle.contentB64, "base64").toString(), fakeBytes); assert.equal(bundle.passphrase, whitespace ? "NEVER_GENERATED_PASSPHRASE" : "NEVER_PASSPHRASE"); assert.equal(generatedCount, whitespace ? 1 : 0) + await assert.rejects(run(authority)); assert.equal(commands.length, 2) + } + for (const fault of ["lost", "ignored", "collateral", "revoked", "secretRevoked", "downloadFailure", "checkpointFailure", "cleanupLost", "lateSource"]) { + reset(); mode = fault + await assert.rejects(run(authority), error => error instanceof McpError) + assert.equal(commands.length, fault === "lateSource" ? 0 : fault === "cleanupLost" ? 2 : 1, fault); assert.equal(artifacts.size, 0) + const count: number = commands.length; await assert.rejects(run(authority)); assert.equal(commands.length, count, `${fault}: no replay/fallback/inverse`) + if (fault === "cleanupLost") assert.equal(downloaded!.every(byte => byte === 0), true) + } + for (const fault of ["shortDownload", "postDownloadRevoke"]) { + reset(); mode = fault + await assert.rejects(run(authority), error => error instanceof McpError && error.code === "MUTATION_OUTCOME_UNKNOWN") + assert.equal(downloaded!.every(byte => byte === 0), true, `${fault}: downloaded bytes must be cleared before cleanup begins`) + assert.equal(commands.length, 1); assert.equal(artifacts.size, 0) + } + reset(); mode = "lateDownload" + const downloading = new Promise(resolve => { downloadStarted = resolve }) + const cancelDownload = new AbortController() + const lateRun = run(authority, { signal: cancelDownload.signal }) + await downloading + cancelDownload.abort() + await assert.rejects(lateRun, error => error instanceof McpError && error.code === "MUTATION_OUTCOME_UNKNOWN") + releaseDownload!() + await new Promise(resolve => setImmediate(resolve)) + assert.equal(downloaded!.every(byte => byte === 0), true, "buffer arriving after outer finally is also cleared") + assert.equal(commands.length, 1); assert.equal(artifacts.size, 0) + for (const fault of ["missingPostFence", "duplicatePostFence", "missingDeleteFence", "duplicateDeleteFence"]) { + reset(); mode = fault; await assert.rejects(run(authority), error => error instanceof McpError && error.code === "VERIFICATION_FAILED") + assert.equal(commands.length, fault.endsWith("PostFence") ? 0 : 1); assert.equal(artifacts.size, 0) + const count: number = commands.length; await assert.rejects(run(authority)); assert.equal(commands.length, count) + } + for (const fault of ["parallelPostFence", "parallelDeleteFence"]) { + reset(); mode = fault; await assert.rejects(run(authority), error => error instanceof McpError && error.code === "VERIFICATION_FAILED") + assert.equal(commands.length, fault === "parallelPostFence" ? 0 : 1); assert.equal(typeof releaseDelayedFence, "function") + releaseDelayedFence!(); await new Promise(resolve => setImmediate(resolve)) + assert.equal(commands.length, fault === "parallelPostFence" ? 0 : 1, "first callback must not send after parallel duplicate already failed"); assert.equal(artifacts.size, 0) + } + for (const fault of ["deadlineRead", "deadlineAfterAck", "deadlinePublish"]) { + reset(); mode = fault; await assert.rejects(run(authority, { timeoutMs: 100 }), error => error instanceof McpError && error.code === (fault === "deadlineRead" ? "READ_CANCELLED" : "MUTATION_OUTCOME_UNKNOWN")) + assert.equal(commands.length, fault === "deadlineRead" ? 0 : fault === "deadlineAfterAck" ? 1 : 2); assert.equal(artifacts.size, 0) + } + reset(); const aborted = new AbortController(); aborted.abort(); await assert.rejects(run(authority, { signal: aborted.signal })); assert.equal(commands.length, 0) + reset(); state.catalogs["/file"].push({ ".id": "*old", name: clientPlan.filename, size: "9" }); await assert.rejects(run({ ...authority, expectedProof: await proof(state) })); assert.equal(commands.length, 0) + reset(); await assert.rejects(exportIpsecExistingCertificateGuarded(authority, deps.publish), error => error instanceof McpError && error.code === "PERMISSION_DENIED"); assert.equal(commands.length, 0) + const receipt = ipsecExistingCertificateExportArtifactReceipt({ id: randomUUID() }, { bytes: 100, sha256: "a".repeat(64) }); assert.equal(receipt.humanDownloadOnly, true); assert.equal(receipt.ipsecContentReturned, false) + const scope = { version: 1, kind: "ipsec-existing-certificate-bundle", authority, afterProof: authority.expectedProof, bytes: 100, sha256: "a".repeat(64), encryptedSha256: "b".repeat(64), proof: "c".repeat(64) } + assert.equal(ipsecExistingCertificateExportArtifactScopeSchema.safeParse(scope).success, true); assert.equal(ipsecExistingCertificateExportArtifactScopeSchema.safeParse({ ...scope, kind: "ipsec-user-bundle" }).success, false) + await assert.rejects(createIpsecExistingCertificateExportArtifact(authority, { user: "synthetic", filename: "synthetic.p12", contentB64: "synthetic", mime: "application/x-pkcs12" }, authority.expectedProof, async () => {})) + await assert.rejects(verifyIpsecExistingCertificateExportArtifact(randomUUID(), authority, authority.expectedProof)) + await assert.rejects(humanIpsecExistingCertificateExportMetadata(randomUUID(), {} as AuthUser)) + await assert.rejects(readHumanIpsecExistingCertificateExportArtifact(randomUUID(), {} as AuthUser)) + assert.equal(IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, true) + assert.equal(IpsecExistingCertificateExportExecutionError.prototype instanceof McpError, true) + console.log("Synthetic existing-IPsec export foundations: both REST selectors/all certificate roles incl explicit CA, exact receipt-owned file/read/cleanup, one attempt, passphrase references/generation, opaque conservation, source/owner/secret revoke, no replay/fallback/inverses, encrypted synthetic human bundle; real SQL/network/PKI forbidden") +} finally { pool.query = oldQuery; pool.connect = oldConnect } + + diff --git a/backend/src/services/ipsec-existing-certificate-export.ts b/backend/src/services/ipsec-existing-certificate-export.ts new file mode 100644 index 00000000..afecd6ac --- /dev/null +++ b/backend/src/services/ipsec-existing-certificate-export.ts @@ -0,0 +1,184 @@ +import { randomBytes } from "node:crypto" +import type { IpsecCertBundle } from "@mmapp/contracts/ipsec" +import { pool } from "../db/index.js" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { hasPermission } from "../lib/permissions.js" +import { effectiveMcpConnection, audit, transaction } from "../modules/mcp/store.js" +import { fingerprint, privateSecretFingerprint, McpError, decryptSecret } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { ipsecPeerDeleteRows, ipsecPeerDeletePaths, ipsecPeerDeleteSourceProof } from "./ipsec-peer-delete.js" +import { ipsecUserUpdateValue } from "./ipsec-user-update.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { assertVerifiedConfigurationAccess, executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { buildClientInstructions, buildSswanConfig } from "./ipsec-config.js" +import { planIpsecExistingCertificateExportSelection, type IpsecExistingCertificateExportSelection } from "./ipsec-existing-certificate-export-model.js" +import { IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION, ipsecExistingCertificateExportAuthoritySchema, ipsecExistingCertificateExportSecretPurpose, normalizeIpsecExistingExportPassphrase, projectIpsecExistingCertificateExport, projectIpsecExistingCertificateCleanup, validateIpsecExistingCertificateExportState, type IpsecExistingCertificateExportAuthority, type IpsecExistingCertificateExportState } from "./ipsec-existing-certificate-export-model.js" + +export async function ipsecExistingCertificateExportProof(state: IpsecExistingCertificateExportState) { return privateSecretFingerprint(fingerprint(ipsecUserUpdateValue(state)), "ipsec-existing-certificate-export-v1") } +type ExportReadControl = { signal?: AbortSignal; deadlineAt?: number } +export async function readIpsecExistingCertificateExportState(server: ServerRow, authorize: () => Promise, control: ExportReadControl = {}) { + const deadlineAt = control.deadlineAt ?? Date.now() + 45000 + const remaining = () => { control.signal?.throwIfAborted(); const ms = deadlineAt - Date.now(); if (ms <= 0) throw new McpError("READ_CANCELLED", "Общий срок приватного экспорта истёк", 409); return Math.min(ms, 10000) } + const source = async () => { remaining(); await authorize(); const fresh = await getServerRowById(server.id); remaining(); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new McpError("STATE_CONFLICT", "Источник приватного экспорта изменился", 409) } + const catalogs: Record[]> = {}, client = MikrotikClient.fromServer(server) + let bytes = 0 + for (const path of [...ipsecPeerDeletePaths, "/file"]) { + await source(); catalogs[path] = ipsecPeerDeleteRows(await client.get(path, remaining(), control.signal, 1048576)); await source() + bytes += Buffer.byteLength(JSON.stringify(catalogs[path])); if (bytes > 1048576) throw new McpError("STATE_UNAVAILABLE", "Полный каталог экспорта превышает 1 МиБ", 502) + } + await source() + const protections = (await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) ORDER BY id LIMIT 1001", [String(server.id)])).rows + await source() + if (protections.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен бюджет защит экспорта", 502) + const state = { source: await ipsecPeerDeleteSourceProof(server), catalogs, protections, proof: "" } + validateIpsecExistingCertificateExportState(state) + return { ...state, proof: await ipsecExistingCertificateExportProof(state) } +} +export async function assertIpsecExistingCertificateExportAccess(authority: IpsecExistingCertificateExportAuthority, client: Pick = pool) { + ipsecExistingCertificateExportAuthoritySchema.parse(authority) + const current = await effectiveMcpConnection(authority.principal, client), request = authority.request + if (Date.parse(authority.principal.credentialExpiresAt) <= Date.now() || current.owner_id !== authority.ownerId || current.owner_valid_until.getTime() <= Date.now() || current.policy.status !== "active" || current.policy.mode === "observer" || Date.parse(current.policy.expiresAt) <= Date.now() || !current.policy.scope.operationIds.includes(IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION) || !current.policy.scope.serverIds.includes(request.serverId) || !hasPermission(current.owner_permissions, "mm:network:write") || !hasPermission(current.policy.permissions, "mm:network:write")) throw new McpError("PERMISSION_DENIED", "Нет отдельного актуального допуска экспорта существующего сертификата", 403) + if (current.generation !== authority.generation || fingerprint(current.owner_permissions) !== authority.ownerProof || fingerprint(current.policy) !== authority.policyProof || fingerprint(request) !== authority.requestProof || !request.privateKeyExport || (authority.role === "ca" && !request.caPrivateKeyExport)) throw new McpError("STATE_CONFLICT", "Owner, запрос, роль или допуск существующего экспорта изменились", 409) + const denied = (await client.query("SELECT 1 FROM mcp_protections WHERE object_ref->>'protection'='deny' AND (object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1)) LIMIT 1", [String(request.serverId)])).rows[0] + if (denied) throw new McpError("PROTECTED_OBJECT", "Экспорт существующего сертификата заблокирован защитой", 409) +} +export async function resolveIpsecExistingCertificateExportPassphrase(authority: IpsecExistingCertificateExportAuthority, authorize: () => Promise) { + await authorize() + const request = authority.request + if (request.passphrase.kind === "generate") return { referenceProof: await privateSecretFingerprint(fingerprint({ kind: "generate", receiptId: authority.receiptId }), "ipsec-existing-export-reference-v1"), passphrase: undefined } + const rows = (await pool.query("SELECT purpose,server_id,created_by,xmin::text AS revision,CASE WHEN octet_length(ciphertext)<=32768 THEN ciphertext ELSE NULL END AS ciphertext FROM mcp_secrets WHERE id=$1 AND created_by=$2 LIMIT 2", [request.passphrase.secretRef, authority.ownerId])).rows + await authorize() + const row = rows[0] + if (rows.length !== 1 || row.created_by !== authority.ownerId || row.server_id !== request.serverId || row.purpose !== ipsecExistingCertificateExportSecretPurpose(request.serverId, request.selector) || typeof row.ciphertext !== "string") throw new McpError("SECRET_UNAVAILABLE", "Passphrase reference не принадлежит owner/server/target/purpose существующего экспорта", 409) + const passphrase = await decryptSecret(row.ciphertext).catch(() => { throw new McpError("SECRET_UNAVAILABLE", "Passphrase существующего экспорта недоступен", 409) }) + if (passphrase.length < 8 || passphrase.length > 16384) throw new McpError("SECRET_UNAVAILABLE", "Формат passphrase reference не поддержан", 409) + await authorize() + return { passphrase, referenceProof: await privateSecretFingerprint(fingerprint({ id: request.passphrase.secretRef, ...row }), "ipsec-existing-export-reference-v1") } +} +export interface IpsecExistingCertificateExportReceipt { ok: true; verified: true; receiptId: string; certificateId: string; serverId: number; commandCount: 2; completedCommands: 2; confirmedWrites: 2; afterProof: string; automaticReplay: false; forwardingVerified: false; artifactId: string; downloadPath: string; humanDownloadOnly: true; ipsecContentReturned: false } +type Client = Pick +export type IpsecExistingCertificateExportBinding = Pick & { request: IpsecExistingCertificateExportSelection } +type ArtifactPublication = Pick +type ExecutionReceipt = Omit +export interface IpsecExistingCertificateExportTransportDependencies { + server: (id: number) => Promise; sourceProof: (server: ServerRow) => Promise; access: (authority: A) => Promise; + material: (authority: A, authorize: () => Promise) => Promise<{ passphrase?: string; referenceProof: string }>; + read: (server: ServerRow, authorize: () => Promise, control?: ExportReadControl) => Promise; proof: (state: IpsecExistingCertificateExportState) => Promise; client: (server: ServerRow) => Client; + boundary: (authority: A, work: () => Promise) => Promise; claim: (authority: A) => Promise; + checkpoint: (authority: A, value: { completedCommands: number; afterProof: string; file?: { id: string; name: string; size: number } }) => Promise; + publish: (authority: A, bundle: IpsecCertBundle, afterProof: string, authorize: () => Promise) => Promise

; + generatePassphrase: () => string; + clock?: () => number; +} +export type IpsecExistingCertificateExportDependencies = IpsecExistingCertificateExportTransportDependencies +export class IpsecExistingCertificateExportExecutionError extends McpError { + constructor(code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", readonly confirmedWrites: number, readonly completedCommands: number) { super(code, "Экспорт существующего сертификата остановлен; повтор, fallback и автоматические обратные команды запрещены. Требуется сверка receipt и временного файла", 502) } +} +/** MCP transport wrapper preserves the persisted authority and receipt shape. */ +export function createIpsecExistingCertificateExportFoundation(deps: IpsecExistingCertificateExportDependencies) { + const execute = createIpsecExistingCertificateExportExecutor(deps, raw => ipsecExistingCertificateExportAuthoritySchema.parse(raw)) + return async (raw: unknown, options: { signal?: AbortSignal; timeoutMs?: number } = {}): Promise => { + const { publication, ...receipt } = await execute(raw, options) + return { ...receipt, ...publication } + } +} +/** One native algorithm for independently authenticated transports. Parsing, + * durable claims and publication remain mandatory trusted I/O dependencies; + * this injectable core has no default activation, actor or MCP principal. */ +export function createIpsecExistingCertificateExportExecutor(deps: IpsecExistingCertificateExportTransportDependencies, parseAuthority: (raw: unknown) => A) { + return async (raw: unknown, options: { signal?: AbortSignal; timeoutMs?: number } = {}): Promise => { + const authority = parseAuthority(raw) + if (fingerprint(authority.request) !== authority.requestProof) throw new McpError("STATE_CONFLICT", "Запрос экспорта не соответствует sealed authority", 409) + const timeoutMs = options.timeoutMs ?? 180000 + if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 180000) throw new McpError("INVALID_INPUT", "Некорректный общий срок приватного экспорта", 400) + const clock = deps.clock ?? Date.now + const deadlineAt = clock() + timeoutMs, timerSignal = AbortSignal.timeout(timeoutMs), signal = options.signal ? AbortSignal.any([options.signal, timerSignal]) : timerSignal + const remaining = (limit: number) => { signal.throwIfAborted(); const ms = deadlineAt - clock(); if (ms <= 0) throw new McpError("READ_CANCELLED", "Общий срок экспорта истёк", 409); return Math.min(ms, limit) } + const bounded = async (work: () => Promise): Promise => { remaining(timeoutMs); let abort!: () => void; const cancelled = new Promise((_resolve, reject) => { abort = () => reject(signal.reason); signal.addEventListener("abort", abort, { once: true }) }); try { const value = await Promise.race([work(), cancelled]); remaining(timeoutMs); return value } finally { signal.removeEventListener("abort", abort) } } + let confirmedWrites = 0, completedCommands = 0, nativeAttempted = false + const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED") => new IpsecExistingCertificateExportExecutionError(code, confirmedWrites, completedCommands) + return bounded(() => deps.boundary(authority, async () => { + const access = async () => { await bounded(() => deps.access(authority)) } + await access() + const material = await bounded(() => deps.material(authority, access)) + if (material.referenceProof !== authority.referenceProof) throw new McpError("STATE_CONFLICT", "Passphrase reference экспорта изменился", 409) + const passphrase = normalizeIpsecExistingExportPassphrase(material.passphrase, deps.generatePassphrase) + const source = async () => { + await access() + const current = await bounded(() => deps.server(authority.request.serverId)) + if (!current || await bounded(() => deps.sourceProof(current)) !== authority.sourceProof) throw new McpError("STATE_CONFLICT", "Источник экспорта изменился", 409) + if ((await bounded(() => deps.material(authority, access))).referenceProof !== authority.referenceProof) throw new McpError("STATE_CONFLICT", "Passphrase reference изменился до native запроса", 409) + } + await source(); const server = await bounded(() => deps.server(authority.request.serverId)) + if (!server) throw new McpError("STATE_CONFLICT", "Сервер экспорта отсутствует", 409) + const read = () => bounded(() => deps.read(server, source, { signal, deadlineAt })) + let state = await read() + if (state.source !== authority.sourceProof || state.proof !== authority.expectedProof) throw new McpError("STATE_CONFLICT", "Полное состояние экспорта изменилось", 409) + const { serverId, selector, privateKeyExport, caPrivateKeyExport } = authority.request + const plan = planIpsecExistingCertificateExportSelection(state, server, { serverId, selector, privateKeyExport, caPrivateKeyExport }, authority.receiptId) + if (plan.certificateId !== authority.certificateId || plan.certificateName !== authority.certificateName || plan.role !== authority.role) throw new McpError("STATE_CONFLICT", "Target сертификата или роль изменились", 409) + await source(); await bounded(() => deps.claim(authority)) + const client = deps.client(server) + const fence = async () => { const current = await read(); if (current.proof !== state.proof) throw new McpError("STATE_CONFLICT", "Состояние экспорта изменилось после durable intent", 409); await source() } + let dispatched = false, exportFences = 0, cleanupFences = 0, duplicateFence = false + try { await bounded(() => client.post("/certificate/export-certificate", { name: plan.certificateName, type: "pkcs12", "file-name": plan.exportPrefix, "export-passphrase": passphrase }, remaining(60000), signal, async () => { if (++exportFences !== 1) { duplicateFence = true; throw new McpError("VERIFICATION_FAILED", "Повтор export dispatch fence запрещён", 502) }; await fence(); if (duplicateFence || exportFences !== 1) throw new McpError("VERIFICATION_FAILED", "Export fence отозван параллельным callback", 502); dispatched = true; nativeAttempted = true })); if (!dispatched || exportFences !== 1) throw stopped("VERIFICATION_FAILED"); confirmedWrites++ } + catch (error) { if (duplicateFence || error instanceof IpsecExistingCertificateExportExecutionError) throw stopped("VERIFICATION_FAILED"); if (!dispatched && error instanceof McpError) throw error; throw stopped("MUTATION_OUTCOME_UNKNOWN") } + let file: { id: string; name: string; size: number } + let afterExport: IpsecExistingCertificateExportState + try { afterExport = await read() } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN") } + try { const expected = projectIpsecExistingCertificateExport(state, plan.filename, afterExport); if (await bounded(() => deps.proof(expected.projected)) !== afterExport.proof) throw Error("whole state mismatch"); file = expected.file; state = afterExport; completedCommands++ } + catch { throw stopped("VERIFICATION_FAILED") } + try { await source(); await bounded(() => deps.checkpoint(authority, { completedCommands, afterProof: state.proof, file })) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN") } + const assertOwned = async () => { await fence() } + let content: Buffer | undefined + try { + try { + await assertOwned() + await bounded(async () => { + const bytes = await client.downloadOwnedFile(file, { authorize: assertOwned, signal, timeoutMs: remaining(30000) }) + // A timed-out download may still resolve. Its buffer must be cleared + // here too, after the outer finally has already finished. + if (signal.aborted || clock() >= deadlineAt) { bytes.fill(0); remaining(30000) } + content = bytes + }) + if (!content || content.length !== file.size) throw Error("incomplete private file") + await assertOwned() + } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN") } + if (!content) throw stopped("MUTATION_OUTCOME_UNKNOWN") + dispatched = false + try { await bounded(() => client.delete(`/file/${encodeURIComponent(file.id)}`, remaining(10000), async () => { if (++cleanupFences !== 1) { duplicateFence = true; throw new McpError("VERIFICATION_FAILED", "Повтор cleanup dispatch fence запрещён", 502) }; await fence(); if (duplicateFence || cleanupFences !== 1) throw new McpError("VERIFICATION_FAILED", "Cleanup fence отозван параллельным callback", 502); projectIpsecExistingCertificateCleanup(state, file); dispatched = true })); if (!dispatched || cleanupFences !== 1) throw stopped("VERIFICATION_FAILED"); confirmedWrites++ } + catch (error) { if (duplicateFence || error instanceof IpsecExistingCertificateExportExecutionError || (!dispatched && error instanceof McpError)) throw stopped("VERIFICATION_FAILED"); throw stopped("MUTATION_OUTCOME_UNKNOWN") } + let afterCleanup: IpsecExistingCertificateExportState + try { afterCleanup = await read() } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN") } + try { if (await bounded(() => deps.proof(projectIpsecExistingCertificateCleanup(state, file))) !== afterCleanup.proof || afterCleanup.proof !== authority.expectedProof) throw Error("cleanup mismatch"); state = afterCleanup; completedCommands++ } + catch { throw stopped("VERIFICATION_FAILED") } + try { await source(); await bounded(() => deps.checkpoint(authority, { completedCommands, afterProof: state.proof })) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN") } + const p12B64 = content.toString("base64") + const bundle: IpsecCertBundle = { user: plan.userName, serverEndpoint: plan.serverEndpoint, filename: plan.filename, contentB64: p12B64, mime: "application/x-pkcs12", passphrase, sswanFilename: `${plan.certificateName}.sswan`, sswanContent: buildSswanConfig({ name: `IKEv2 ${plan.serverEndpoint}`, serverEndpoint: plan.serverEndpoint, serverId: plan.serverEndpoint, p12B64 }), instructions: buildClientInstructions({ userName: plan.userName, serverEndpoint: plan.serverEndpoint, p12Filename: plan.filename, passphrase, dns: plan.dns }) } + try { await fence(); const publication = await bounded(() => deps.publish(authority, bundle, state.proof, source)); await fence(); return { ok: true, verified: true, receiptId: authority.receiptId, certificateId: authority.certificateId, serverId: server.id, commandCount: 2, completedCommands: 2, confirmedWrites: 2, afterProof: state.proof, automaticReplay: false, forwardingVerified: false, publication } as const } + catch { throw stopped("MUTATION_OUTCOME_UNKNOWN") } + } finally { content?.fill(0) } + })).catch(error => { if (signal.aborted) { if (nativeAttempted || confirmedWrites) throw stopped("MUTATION_OUTCOME_UNKNOWN"); throw new McpError("READ_CANCELLED", "Экспорт отменён или общий срок истёк до native отправки", 409) }; throw error }) + } +} +export async function claimIpsecExistingCertificateExport(authority: IpsecExistingCertificateExportAuthority) { + await assertVerifiedConfigurationAccess(); await assertIpsecExistingCertificateExportAccess(authority) + await transaction(async client => { + await client.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [`ipsec-existing-export:${authority.receiptId}`]) + await assertIpsecExistingCertificateExportAccess(authority, client) + const rows = (await client.query("SELECT s.intended_state FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id JOIN mcp_plans p ON p.id=o.plan_id WHERE o.connection_id=$1 AND o.state='running' AND p.operation_id=$2 AND p.approved_by IS NOT NULL AND s.state='intent' AND s.intended_state->>'service'=$2 AND s.intended_state->'input'->>'receiptId'=$3 LIMIT 2", [authority.principal.connectionId, IPSEC_EXISTING_CERTIFICATE_EXPORT_OPERATION, authority.receiptId])).rows + if (rows.length !== 1 || fingerprint(rows[0].intended_state.input) !== authority.inputProof || (await client.query("SELECT 1 FROM mcp_audit WHERE connection_id=$1 AND action='ipsec.existing.export.claim' AND payload->>'receiptId'=$2 LIMIT 1", [authority.principal.connectionId, authority.receiptId])).rows.length) throw new McpError("STATE_CONFLICT", "Нет единственного human-approved intent или receipt уже использован; повтор запрещён", 409) + await audit(client, authority.ownerId, "ipsec.existing.export.claim", authority.principal.connectionId, { receiptId: authority.receiptId, inputProof: authority.inputProof }) + }) +} +/** The fixed activation gate is independent of request flags. A live verified + * plan context is required before locks, configuration audit or native I/O. */ +export async function exportIpsecExistingCertificateGuarded(authority: IpsecExistingCertificateExportAuthority, publish: IpsecExistingCertificateExportDependencies["publish"], options: { signal?: AbortSignal; timeoutMs?: number } = {}): Promise { + if (!IPSEC_EXISTING_CERTIFICATE_EXPORT_ACTIVATED) throw new McpError("APPROVAL_REQUIRED", "Экспорт существующих сертификатов/CA выключен", 409) + await assertVerifiedConfigurationAccess() + return createIpsecExistingCertificateExportFoundation({ server: getServerRowById, sourceProof: ipsecPeerDeleteSourceProof, access: async value => { await assertVerifiedConfigurationAccess(); await assertIpsecExistingCertificateExportAccess(value) }, material: resolveIpsecExistingCertificateExportPassphrase, read: readIpsecExistingCertificateExportState, proof: ipsecExistingCertificateExportProof, client: MikrotikClient.fromServer, + boundary: (value, work) => withApplicationMutationLock(() => withConfigMutationLock([value.request.serverId], () => executeGuardedApplicationWrite({ kind: "certificate", id: value.certificateId }, [value.request.serverId], work))), claim: claimIpsecExistingCertificateExport, + checkpoint: (value, progress) => audit(pool, value.ownerId, "ipsec.existing.export.checkpoint", value.principal.connectionId, { receiptId: value.receiptId, inputProof: value.inputProof, ...progress }), publish, generatePassphrase: () => randomBytes(24).toString("base64url"), + })(authority, options) +} diff --git a/backend/src/services/ipsec-live.ts b/backend/src/services/ipsec-live.ts index f506293a..4ef4f646 100644 --- a/backend/src/services/ipsec-live.ts +++ b/backend/src/services/ipsec-live.ts @@ -4,6 +4,8 @@ import { eq } from "drizzle-orm" import { db } from "../db/index.js" import { servers } from "../db/schema.js" import { MikrotikClient } from "./mikrotik.js" +import { observeIpsecRead, type ObservedIpsecRead } from "./ipsec-observed-read.js" +import { nativeNetworkRows } from "./network-observed-read.js" import type { IpsecCertInfoDto, IpsecClientDto, @@ -216,6 +218,7 @@ export async function fetchIpsecState(server: ServerRow, reader?: MikrotikClient const collection = async (path: string): Promise => { const raw = await client.get(path) if (!Array.isArray(raw) || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !row[".id"])) throw new Error(`Некорректный ответ IPsec: ${path}`) + try { nativeNetworkRows(raw) } catch { throw new Error(`Некорректный ответ IPsec: ${path}`) } return raw as T[] } const [peers, identities, modeConfigs, pools, policies, nat, active, certs] = await Promise.all([ @@ -233,7 +236,7 @@ export async function fetchIpsecState(server: ServerRow, reader?: MikrotikClient * 2) PSK-identity. * Общая listener-identity (без remote-certificate) клиентом не считается — она обслуживает всех. */ -export function mapClients(state: IpsecServerState): IpsecClientDto[] { +export function mapClients(state: Pick): IpsecClientDto[] { const server = state.server const ike2Peers = selectIke2Peers(state.peers) const ike2PeerNames = ike2Peers.map((p) => (p.name ?? "").trim()).filter(Boolean) @@ -411,6 +414,13 @@ export function mapClients(state: IpsecServerState): IpsecClientDto[] { return out.sort((a, b) => a.name.localeCompare(b.name)) } +/** Binding selectors reuse the existing client eligibility/name/CN rules, + * including PSK and shared-listener exclusion. No online/static-IP observation + * is claimed: this projection returns keys only from the three native menus. */ +export function ipsecClientBindingKeys(state: Pick): string[] { + return mapClients({ ...state, modeConfigs: [], active: [] }).map(client => client.commonName ?? client.name) +} + /** Explicit namespaces prevent cross-menu RouterOS ID collisions. Legacy ambiguous IDs fail closed. */ export function resolveIpsecClientTarget(state: IpsecServerState, token: string) { const kind = token.startsWith("cert:") ? "cert" : token.startsWith("identity:") ? "identity" : undefined @@ -495,8 +505,8 @@ export function mapServerSummary(state: IpsecServerState, clients: IpsecClientDt * Snapshot для истории/отката намеренно остаётся managed-only (`MikrotikManager:ipsec`): * restore не должен трогать/пересоздавать уже существующие на роутере (не наших) объекты. */ -export async function captureIpsecSnapshot(server: ServerRow): Promise { - const state = await fetchIpsecState(server) +export async function captureIpsecSnapshot(server: ServerRow, reader?: MikrotikClient): Promise { + const state = await fetchIpsecState(server, reader) return canonicalIpsecSnapshot({ peers: state.peers .filter((p) => isIpsecManagedComment(p.comment)) @@ -518,7 +528,7 @@ export async function captureIpsecSnapshot(server: ServerRow): Promise = [] const summaries: IpsecServerSummaryDto[] = [] const clients: IpsecClientDto[] = [] + const observed: { servers: ObservedIpsecRead["server"][]; clients: ObservedIpsecRead["clients"] } = { servers: [], clients: [] } await Promise.all( serverRows.map(async (server) => { try { if (!server.enabled) throw new Error("Сервер выключен. Опрос IPsec не выполнялся") - const state = await dependencies.readState(server) + const state = await readState(server) const serverClients = mapClients(state) - summaries.push(mapServerSummary(state, serverClients)) + const summary = mapServerSummary(state, serverClients), safe = observeIpsecRead(state, summary, serverClients) + summaries.push(summary) clients.push(...serverClients) + observed.servers.push(safe.server); observed.clients.push(...safe.clients) } catch (e) { failures.push({ serverId: String(server.id), @@ -680,7 +698,8 @@ export async function listIpsec(opts?: { serverId?: string }, dependencies = { ) summaries.sort((a, b) => a.serverName.localeCompare(b.serverName)) clients.sort((a, b) => a.name.localeCompare(b.name)) - return { servers: summaries, clients, failures } + observed.servers.sort((a, b) => a.serverId.localeCompare(b.serverId)); observed.clients.sort((a, b) => a.id.localeCompare(b.id)) + return { servers: summaries, clients, failures, observed } } export async function countIpsecClients():Promise{return boundedCompleteCount(()=>listIpsec(),value=>completeCollectionCount(value,"clients"))} @@ -707,8 +726,8 @@ export async function listIpsecClientsForCatalog(serverId: number): Promise<{ }), ]) return { clients: mapClients(state) } - } catch (e) { - return { clients: [], error: e instanceof Error ? e.message : String(e) } + } catch { + return { clients: [], error: "Не удалось получить список клиентов IPsec." } } } diff --git a/backend/src/services/ipsec-observed-read.ts b/backend/src/services/ipsec-observed-read.ts new file mode 100644 index 00000000..b9bf67e5 --- /dev/null +++ b/backend/src/services/ipsec-observed-read.ts @@ -0,0 +1,32 @@ +import type { IpsecClientDto, IpsecServerSummaryDto, IpsecCertInfoDto, IpsecPeerDto } from "@mmapp/contracts/ipsec" +import type { IpsecServerState } from "./ipsec-live.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { maskNetworkSecrets, networkSecretValues } from "./network-observed-read.js" + +export function observeIpsecRead(state: IpsecServerState, summary: IpsecServerSummaryDto, clients: IpsecClientDto[]) { + const cert = (dto: IpsecCertInfoDto | undefined) => { + if (!dto) return undefined + const matches = state.certs.filter(row => row.name === dto.name), raw = matches.length === 1 ? matches[0] : undefined + const { expiresAt, hasPrivateKey: _hasPrivateKey, ...rest } = dto + return { ...rest, trusted: observedBoolean(raw?.trusted), keyPairAvailable: observedBoolean(raw?.["private-key"]), nativeExpiresAt: expiresAt, expiryTimestampVerified: false } + } + const peer = (dto: IpsecPeerDto | undefined) => { + if (!dto) return undefined + const matches = state.peers.filter(row => row[".id"] === dto.rosId), raw = matches.length === 1 ? matches[0] : undefined + return { ...dto, disabled: observedBoolean(raw?.disabled), passive: observedBoolean(raw?.passive) } + } + const knownSessionStates = new Set(["starting", "established", "dying"]) + const unknownSessionStates = state.active.filter(row => !knownSessionStates.has(row.state ?? "")).length + const observedClients = clients.map(dto => { + const identityId = dto.rosId.startsWith("identity:") ? dto.rosId.slice("identity:".length) : undefined + const matches = identityId ? state.identities.filter(row => row[".id"] === identityId) : [] + return { ...dto, disabled: observedBoolean(matches.length === 1 ? matches[0].disabled : undefined), online: dto.sessionAmbiguous ? null : dto.online === true ? true : unknownSessionStates ? null : false } + }) + const observedSummary = { ...summary, peer: peer(summary.peer), peers: summary.peers?.map(peer), caCert: cert(summary.caCert), serverCert: cert(summary.serverCert), certs: (summary.certs ?? []).map(cert), + clientsOnline: observedClients.some(row => row.online === null) ? null : observedClients.filter(row => row.online).length, + clientsOnlineConfirmed: observedClients.filter(row => row.online === true).length, clientsSessionUnknown: observedClients.filter(row => row.online === null).length, + unknownSessionStates, readinessScope: "configuration-presence", sessionEvidence: "native-active-peers", dataPlaneVerified: false } + const secrets = networkSecretValues([...state.identities, ...state.peers, ...state.certs] as Record[]) + return maskNetworkSecrets({ server: observedSummary, clients: observedClients }, secrets) +} +export type ObservedIpsecRead = ReturnType diff --git a/backend/src/services/ipsec-peer-delete.test.ts b/backend/src/services/ipsec-peer-delete.test.ts new file mode 100644 index 00000000..2b6eb5fc --- /dev/null +++ b/backend/src/services/ipsec-peer-delete.test.ts @@ -0,0 +1,23 @@ +import assert from "node:assert/strict" +import { ipsecPeerDeletePaths, ipsecPeerDeleteRows, ipsecPeerDeleteTarget, ipsecPeerDeleteValue, projectIpsecPeerDelete, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +const state: IpsecPeerDeleteState = { source: "private-source", proof: "private-proof", protections: [], catalogs: Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])) } +state.catalogs["/ip/ipsec/peer"] = [{ ".id": "*a1", name: "selected", disabled: "true", opaque: "private" }, { ".id": "*a2", name: "preserve" }] +state.catalogs["/ip/ipsec/identity"] = [{ ".id": "*b1", peer: "selected", secret: "NEVER_SECRET", comment: "identity" }] +assert.throws(() => ipsecPeerDeleteTarget(state, "selected", true), /native ID/) +assert.throws(() => ipsecPeerDeleteTarget(state, "*a1"), /identity/) +assert.equal(ipsecPeerDeleteTarget(state, "*a1", true).linked.length, 1) +const reflected=structuredClone(state);reflected.catalogs["/ip/ipsec/identity"][0].comment="NEVER_SECRET" +assert.throws(()=>ipsecPeerDeleteTarget(reflected,"*a1"),error=>error instanceof Error&&!error.message.includes("NEVER_SECRET")) +const projected = projectIpsecPeerDelete(state, "*a1") +assert.deepEqual(projected.catalogs["/ip/ipsec/identity"], state.catalogs["/ip/ipsec/identity"]) +assert.deepEqual(projected.catalogs["/ip/ipsec/peer"], [state.catalogs["/ip/ipsec/peer"][1]]) +assert.equal(state.catalogs["/ip/ipsec/peer"].length, 2) +assert.throws(() => ipsecPeerDeleteRows([{ ".id": "*1" }, { ".id": "*1" }]), /Повторяющийся/) +assert.throws(() => ipsecPeerDeleteRows([{ ".id": "*1", secret: {} }]), /Неполный/) +assert.throws(() => ipsecPeerDeleteRows(Array.from({ length: 2001 }, (_, n) => ({ ".id": "*" + n }))), /Неполный/) +const runtime = structuredClone(state); runtime.catalogs["/ip/ipsec/active-peers"] = [{ ".id": "*e1", uptime: "5" }] +assert.deepEqual(ipsecPeerDeleteValue(runtime), ipsecPeerDeleteValue(state)) +const opaque = structuredClone(state); opaque.catalogs["/ip/ipsec/identity"][0].secret = "different" +assert.notDeepEqual(ipsecPeerDeleteValue(opaque), ipsecPeerDeleteValue(state)) +for (const flag of ["dynamic", "builtin", "default"]) { const guarded = structuredClone(state); guarded.catalogs["/ip/ipsec/peer"][0][flag] = "unknown"; assert.throws(() => ipsecPeerDeleteTarget(guarded, "*a1", true), /Системный/) } +console.log("✓ IPsec peer deletion exact native ID, force noncascade, malformed/duplicate/oversized catalogs, private opaque preservation, runtime exclusions and system guards") diff --git a/backend/src/services/ipsec-peer-delete.ts b/backend/src/services/ipsec-peer-delete.ts new file mode 100644 index 00000000..edbe77d5 --- /dev/null +++ b/backend/src/services/ipsec-peer-delete.ts @@ -0,0 +1,100 @@ +import { pool } from "../db/index.js" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError, privateFingerprinter } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { sameWireguardEndpoint, wireguardEndpointSource } from "./wireguard-source.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { identityDisplayName } from "./ipsec-config.js" +import { captureIpsecSnapshot } from "./ipsec-live.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { maskNetworkSecrets, networkSecretValues } from "./network-observed-read.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" + +type Row = Record +export const ipsecPeerDeletePaths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] as const +export interface IpsecPeerDeleteState { source: string; catalogs: Record; protections: unknown[]; proof: string } +const fail = (code: string, message: string, status = ["STATE_UNAVAILABLE", "MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED", "HISTORY_UNAVAILABLE"].includes(code) ? 502 : code === "INVALID_INPUT" ? 400 : 409) => new McpError(code, message, status) +export class IpsecPeerLinkedConflict extends McpError { + constructor(public readonly identities: string[], name: string) { super("DEPENDENCY_CONFLICT", `На peer «${name}» ссылаются identity: ${identities.join(", ")}. Удаление разорвёт их.`, 409) } +} +const validateId = (id: string) => { if (!/^\*[a-z0-9]+$/i.test(id) || id.length > 100) throw fail("INVALID_INPUT", "Требуется точный native ID IPsec peer") } +async function source(server: ServerRow, authorize: () => Promise) { + await authorize() + const fresh = await getServerRowById(server.id) + if (!fresh || !sameWireguardEndpoint(fresh, server)) throw fail("STATE_CONFLICT", "Источник IPsec изменился") +} +export function ipsecPeerDeleteRows(value: unknown): Row[] { + if (!Array.isArray(value) || value.length > 2000 || value.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]) || Object.values(row).some(v => typeof v !== "string"))) throw fail("STATE_UNAVAILABLE", "Неполный native-каталог IPsec") + if (new Set(value.map(row => row[".id"])).size !== value.length) throw fail("STATE_UNAVAILABLE", "Повторяющийся native ID IPsec") + return value as Row[] +} +/** Seal every opaque configuration field and list order. Exclude only explicit + * runtime observations, including the documented certificate expiry countdown; + * absolute validity dates, trust, CRL, references and unknown fields stay sealed. */ +export function ipsecPeerDeleteValue(state: Omit) { + return { source: state.source, protections: state.protections, catalogs: Object.fromEntries(Object.entries(state.catalogs).filter(([path]) => path !== "/ip/ipsec/active-peers").map(([path, rows]) => [path, rows.filter(row => !(path === "/ip/ipsec/policy" && observedBoolean(row.dynamic) === true)).map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !(path === "/ip/ipsec/policy" && ["active", "ph2-count", "ph2-state"].includes(key)) && !(path === "/certificate" && key === "expires-after"))))])) } +} +export async function ipsecPeerDeleteSourceProof(server: ServerRow) { return (await privateFingerprinter("ipsec-peer-delete-source-v1"))(fingerprint(wireguardEndpointSource(server))) } +export async function ipsecPeerDeleteProof(state: Omit) { return (await privateFingerprinter("ipsec-peer-delete-v2"))(fingerprint(ipsecPeerDeleteValue(state))) } +export async function readIpsecPeerDeleteState(server: ServerRow, authorize: () => Promise = async () => {}): Promise { + const catalogs: Record = {}, client = MikrotikClient.fromServer(server) + let bytes = 0 + for (const path of ipsecPeerDeletePaths) { + await source(server, authorize) + let raw: unknown + try { raw = await client.get(path, 10000, undefined, 1048576) } catch { throw fail("STATE_UNAVAILABLE", "Native-каталог IPsec недоступен") } + bytes += Buffer.byteLength(JSON.stringify(raw)) + if (bytes > 1048576) throw fail("STATE_UNAVAILABLE", "Полный каталог IPsec превышает 1 МиБ") + catalogs[path] = ipsecPeerDeleteRows(raw) + } + await source(server, authorize) + const protections = (await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) ORDER BY id LIMIT 1001", [String(server.id)])).rows + if (protections.length > 1000 || bytes + Buffer.byteLength(JSON.stringify(protections)) > 1048576) throw fail("STATE_UNAVAILABLE", "Превышен полный бюджет зависимостей IPsec") + await source(server, authorize) + const state = { source: await ipsecPeerDeleteSourceProof(server), catalogs, protections } + return { ...state, proof: await ipsecPeerDeleteProof(state) } +} +export function ipsecPeerDeleteTarget(state: IpsecPeerDeleteState, id: string, force = false) { + validateId(id) + const peer = state.catalogs["/ip/ipsec/peer"].find(row => row[".id"] === id) + if (!peer) throw fail("NOT_FOUND", "Peer не найден", 404) + for (const flag of ["dynamic", "builtin", "default"]) if (peer[flag] !== undefined && observedBoolean(peer[flag]) !== false) throw fail("PROTECTED_OBJECT", "Системный или неподтверждённый IPsec peer нельзя удалить") + const name = (peer.name ?? "").trim() + const secrets = networkSecretValues(Object.values(state.catalogs).flat()) + const linked = maskNetworkSecrets(state.catalogs["/ip/ipsec/identity"].filter(row => (row.peer ?? "").trim() === name).map(row => identityDisplayName(row, state.catalogs["/certificate"])), secrets) + if (linked.length && !force) throw new IpsecPeerLinkedConflict(linked, maskNetworkSecrets(name, secrets)) + return { peer, linked } +} +export function projectIpsecPeerDelete(state: IpsecPeerDeleteState, id: string) { + const projected = structuredClone(state) + projected.catalogs["/ip/ipsec/peer"] = projected.catalogs["/ip/ipsec/peer"].filter(row => row[".id"] !== id) + return projected +} +/** Exact REST force semantics: one DELETE only; identities and other config are never cascaded. */ +export async function deleteIpsecPeerGuarded(server: ServerRow, id: string, force = false, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: typeof captureAndAppendRevision; receiptId?: string } = {}) { + validateId(id) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "ipsec_peer", id }, [server.id], async () => { + const before = await readIpsecPeerDeleteState(server, authorize) + if (expectedProof && before.proof !== expectedProof) throw fail("STATE_CONFLICT", "IPsec, источник или зависимости изменились после подготовки") + ipsecPeerDeleteTarget(before, id, force) + const desired = await ipsecPeerDeleteProof(projectIpsecPeerDelete(before, id)) + await source(server, authorize) + try { await MikrotikClient.fromServer(server).delete("/ip/ipsec/peer/" + encodeURIComponent(id), 10000, async () => { + const fenced = await readIpsecPeerDeleteState(server, authorize) + if (fenced.proof !== before.proof) throw fail("STATE_CONFLICT", "IPsec изменился перед отправкой DELETE") + ipsecPeerDeleteTarget(fenced, id, force) + await source(server, authorize) + }) } + catch (error) { if (error instanceof McpError && !["MUTATION_OUTCOME_UNKNOWN"].includes(error.code)) throw error; throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход DELETE IPsec не подтверждён; повтор запрещён, требуется сверка") } + let after: IpsecPeerDeleteState + try { after = await readIpsecPeerDeleteState(server, authorize) } catch { throw fail("MUTATION_OUTCOME_UNKNOWN", "DELETE отправлен, но сверка IPsec недоступна; повтор запрещён") } + if (after.proof !== desired) throw fail("VERIFICATION_FAILED", "Удаление peer или сохранность остальной конфигурации не подтверждены; повтор запрещён") + // Reuse precisely the REST managed-only history mapping, reading our bounded verified state. + const reader = { get: async (path: string) => { await source(server, authorize); if (!Object.hasOwn(after.catalogs, path)) throw fail("STATE_UNAVAILABLE", "Неизвестный каталог истории IPsec"); return structuredClone(after.catalogs[path]) } } as unknown as MikrotikClient + let historyRecorded = false + try { await source(server, authorize); historyRecorded = await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "apply", capture: () => captureIpsecSnapshot(server, reader) }) !== null } + catch { /* A history failure cannot justify replaying an acknowledged DELETE. */ } + return { ok: true, verified: true, receiptId: options.receiptId ?? null, commandCount: 1, completedCommands: 1, peerId: id, force, afterProof: after.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", forwardingVerified: false } + }))) +} diff --git a/backend/src/services/ipsec-peer-update.test.ts b/backend/src/services/ipsec-peer-update.test.ts new file mode 100644 index 00000000..fd2e42f5 --- /dev/null +++ b/backend/src/services/ipsec-peer-update.test.ts @@ -0,0 +1,34 @@ +import assert from "node:assert/strict" +import { ipsecPeerDeletePaths, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { ipsecPeerUpdateBody, ipsecPeerUpdateValue, planIpsecPeerUpdate, ipsecPeerUpdateNativePatch } from "./ipsec-peer-update.js" +const state:IpsecPeerDeleteState={source:"private",proof:"private",protections:[],catalogs:Object.fromEntries(ipsecPeerDeletePaths.map(path=>[path,[]]))} +state.catalogs["/ip/ipsec/peer"]=[{".id":"*p1",name:"old",disabled:"false",passive:"no",opaque:"NEVER_PEER"},{".id":"*p2",name:"other"}] +state.catalogs["/ip/ipsec/identity"]=[{".id":"*i1",peer:"old",secret:"NEVER_PSK"},{".id":"*i2",peer:"*p1"}] +state.catalogs["/ip/ipsec/policy"]=[{".id":"*s1",peer:"old,other",dynamic:"false",opaque:"preserved"},{".id":"*s2",peer:"old",dynamic:"true"}] +assert.deepEqual(ipsecPeerUpdateBody({address:"",profile:"",exchangeMode:"",passive:false,disabled:true}),{address:"",profile:"","exchange-mode":"",passive:"no",disabled:"yes"}) +assert.deepEqual(ipsecPeerUpdateNativePatch({address:"",profile:"","exchange-mode":"",passive:"no",disabled:"true"}),{address:"",profile:"",exchangeMode:"",passive:false,disabled:true}) +assert.deepEqual(ipsecPeerUpdateNativePatch({comment:"native comment"}),{comment:"native comment"}) +assert.throws(()=>ipsecPeerUpdateNativePatch({certificate:""})) +for(const invalid of["bad1","bad2",null,undefined]){ + assert.throws(()=>ipsecPeerUpdateNativePatch({passive:invalid})) + const unknown=structuredClone(state);unknown.catalogs["/ip/ipsec/peer"][0].passive=String(invalid) + assert.throws(()=>ipsecPeerUpdateValue(unknown),/Не подтверждён/) +} +for(const certificate of["","any",undefined])assert.throws(()=>ipsecPeerUpdateBody({certificate}),(error:unknown)=>(error as{statusCode:number}).statusCode===400) +assert.equal(planIpsecPeerUpdate(state,"*p1",{}).commandCount,0) +assert.equal(planIpsecPeerUpdate(state,"*p1",{name:"old"}).commandCount,1) +const renamed=planIpsecPeerUpdate(state,"*p1",{name:"new",address:"",exchangeMode:"",profile:"",passive:true}) +assert.deepEqual(renamed.companions,[{kind:"ipsec_identity",id:"*i1"},{kind:"ipsec_policy",id:"*s1"}]) +assert.equal(renamed.projected.catalogs["/ip/ipsec/identity"][0].peer,"new") +assert.equal(renamed.projected.catalogs["/ip/ipsec/identity"][1].peer,"*p1") +assert.equal(renamed.projected.catalogs["/ip/ipsec/policy"][0].peer,"new,other") +assert.equal(renamed.projected.catalogs["/ip/ipsec/policy"][1].peer,"old") +assert.equal(renamed.projected.catalogs["/ip/ipsec/identity"][0].secret,"NEVER_PSK") +assert.equal(state.catalogs["/ip/ipsec/peer"][0].name,"old") +assert.throws(()=>planIpsecPeerUpdate(state,"*p1",{name:"other"})) +for(const flag of["dynamic","builtin","default"]){const bad=structuredClone(state);bad.catalogs["/ip/ipsec/peer"][0][flag]="unknown";assert.throws(()=>planIpsecPeerUpdate(bad,"*p1",{}))} +const alternate=structuredClone(state);alternate.catalogs["/ip/ipsec/peer"][0].passive="false";alternate.catalogs["/ip/ipsec/peer"][0].disabled="no" +assert.deepEqual(ipsecPeerUpdateValue(alternate),ipsecPeerUpdateValue(state)) +const hidden=structuredClone(state);hidden.catalogs["/ip/ipsec/identity"][0].secret="changed" +assert.notDeepEqual(ipsecPeerUpdateValue(hidden),ipsecPeerUpdateValue(state)) +console.log("IPsec peer update model: full fields/empty clears, certificate rejection, same-value versus empty-patch history, exact expected reference projection and private preservation; native rename remains unverified") diff --git a/backend/src/services/ipsec-peer-update.ts b/backend/src/services/ipsec-peer-update.ts new file mode 100644 index 00000000..1da124ad --- /dev/null +++ b/backend/src/services/ipsec-peer-update.ts @@ -0,0 +1,111 @@ +import { ipsecPeerPatchSchema } from "@mmapp/contracts/ipsec" +import { z } from "zod" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError, privateFingerprinter } from "../modules/mcp/security.js" +import { readIpsecPeerDeleteState, ipsecPeerDeleteValue, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { MikrotikClient } from "./mikrotik.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { captureIpsecSnapshot } from "./ipsec-live.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" + +export const ipsecPeerUpdatePatchSchema=ipsecPeerPatchSchema.omit({certificate:true}).extend({address:z.string().max(16384).optional(),exchangeMode:z.string().max(16384).optional(),profile:z.string().max(16384).optional(),comment:z.string().max(16384).optional()}).strict() +type State=IpsecPeerDeleteState +const fail=(code:string,message:string,status=code==="INVALID_INPUT"?400:code==="NOT_FOUND"?404:["MUTATION_OUTCOME_UNKNOWN","VERIFICATION_FAILED"].includes(code)?502:409)=>new McpError(code,message,status) +export function ipsecPeerUpdateBody(raw:unknown){ + if(raw&&typeof raw==="object"&&Object.hasOwn(raw,"certificate"))throw fail("INVALID_INPUT","Сертификат задаётся в IPsec identity, а не в peer") + const parsed=ipsecPeerUpdatePatchSchema.safeParse(raw) + if(!parsed.success)throw fail("INVALID_INPUT","Некорректные параметры IPsec peer") + const d=parsed.data,body:Record={} + for(const key of["name","address","profile","comment"]as const)if(d[key]!==undefined)body[key]=d[key] + if(d.exchangeMode!==undefined)body["exchange-mode"]=d.exchangeMode + if(d.passive!==undefined)body.passive=d.passive?"yes":"no" + if(d.disabled!==undefined)body.disabled=d.disabled?"yes":"no" + return body +} +/** Native generic alias must enter this same REST contract, never raw rosPatch. */ +export function ipsecPeerUpdateNativePatch(raw:unknown){ + if(!raw||typeof raw!=="object"||Array.isArray(raw))throw fail("INVALID_INPUT","Некорректные native поля IPsec peer") + const fields=raw as Record,patch:Record={} + for(const[key,value]of Object.entries(fields)){ + if(key==="certificate")throw fail("INVALID_INPUT","Сертификат задаётся в IPsec identity, а не в peer") + if(!["name","address","exchange-mode","passive","profile","disabled","comment"].includes(key))throw fail("INVALID_INPUT","Неизвестное native поле IPsec peer") + if(key==="passive"||key==="disabled"){ + const observed=typeof value==="boolean"?value:typeof value==="string"?observedBoolean(value):null + if(observed===null)throw fail("INVALID_INPUT","Не подтверждён native boolean IPsec peer") + patch[key]=observed + }else patch[key==="exchange-mode"?"exchangeMode":key]=value + } + ipsecPeerUpdateBody(patch) + return ipsecPeerUpdatePatchSchema.parse(patch) +} +/** Only native boolean spellings are normalized; opaque properties/order stay sealed. */ +export function ipsecPeerUpdateValue(state:State){ + const value=ipsecPeerDeleteValue(state) + for(const row of value.catalogs["/ip/ipsec/peer"]??[])for(const key of["passive","disabled"]){ + if(row[key]!==undefined){const observed=observedBoolean(row[key]);if(observed===null)throw fail("STATE_UNAVAILABLE","Не подтверждён native boolean IPsec peer");row[key]=String(observed)} + } + return value +} +export async function ipsecPeerUpdateProof(state:State){return(await privateFingerprinter("ipsec-peer-update-v2"))(fingerprint(ipsecPeerUpdateValue(state)))} +export async function readIpsecPeerUpdateState(server:ServerRow,authorize:()=>Promise=async()=>{}){ + const state=await readIpsecPeerDeleteState(server,authorize) + return{...state,proof:await ipsecPeerUpdateProof(state)} +} +export function planIpsecPeerUpdate(state:State,id:string,raw:unknown){ + const body=ipsecPeerUpdateBody(raw) + if(!/^\*[a-z0-9]+$/i.test(id)||id.length>100)throw fail("INVALID_INPUT","Требуется точный native ID IPsec peer") + const peer=state.catalogs["/ip/ipsec/peer"].find(row=>row[".id"]===id) + if(!peer)throw fail("NOT_FOUND","Peer не найден") + for(const flag of["dynamic","builtin","default"])if(peer[flag]!==undefined&&observedBoolean(peer[flag])!==false)throw fail("PROTECTED_OBJECT","Системный или неподтверждённый IPsec peer нельзя изменять") + const rename=body.name!==undefined&&body.name!==peer.name,oldName=peer.name??"" + if(rename&&(!oldName||state.catalogs["/ip/ipsec/peer"].filter(row=>row.name===oldName).length!==1||state.catalogs["/ip/ipsec/peer"].some(row=>row[".id"]!==id&&row.name===body.name)))throw fail("STATE_CONFLICT","Неоднозначное имя IPsec peer") + const projected=structuredClone(state),target=projected.catalogs["/ip/ipsec/peer"].find(row=>row[".id"]===id)! + Object.assign(target,body) + const companions:Array<{kind:string;id:string}>=[] + if(rename){ + // Expected RouterOS named-reference projection, NOT companion writes. Native + // acceptance remains a separate gate; any different result fails read-back. + for(const row of projected.catalogs["/ip/ipsec/identity"])if(row.peer===oldName){row.peer=body.name;companions.push({kind:"ipsec_identity",id:row[".id"]})} + for(const row of projected.catalogs["/ip/ipsec/policy"]){ + if(observedBoolean(row.dynamic)===true)continue + const tokens=(row.peer??"").split(",") + if(tokens.some(token=>token.trim()===oldName)){ + row.peer=tokens.map(token=>token.trim()===oldName?token.replace(oldName,()=>body.name):token).join(",") + companions.push({kind:"ipsec_policy",id:row[".id"]}) + } + } + } + return{body,projected,companions,rename,commandCount:Object.keys(body).length?1:0} +} +export async function updateIpsecPeerGuarded(server:ServerRow,id:string,patch:unknown,expectedProof?:string,authorize:()=>Promise=async()=>{},options:{recordRevision?:typeof captureAndAppendRevision;receiptId?:string;checkpoint?:(value:{receiptId:string;completedCommands:number;commandCount:number;afterProof:string})=>Promise}={}){ + ipsecPeerUpdateBody(patch) // Reject certificate presence before SQL, native reads or history. + return withApplicationMutationLock(()=>withConfigMutationLock([server.id],()=>executeGuardedApplicationWrite({kind:"ipsec_peer",id},[server.id],async()=>{ + const source=async()=>{await authorize();const fresh=await getServerRowById(server.id);if(!fresh||!sameWireguardEndpoint(fresh,server))throw fail("STATE_CONFLICT","Источник IPsec изменился")} + const before=await readIpsecPeerUpdateState(server,source) + if(expectedProof&&before.proof!==expectedProof)throw fail("STATE_CONFLICT","IPsec или зависимости изменились после подготовки") + const plan=planIpsecPeerUpdate(before,id,patch),desired=await ipsecPeerUpdateProof(plan.projected) + let completedCommands=0 + if(plan.commandCount){ + let dispatched=false + try{ + await MikrotikClient.fromServer(server).patch(`/ip/ipsec/peer/${encodeURIComponent(id)}`,plan.body,10000,async()=>{ + const fresh=await readIpsecPeerUpdateState(server,source) + if(fresh.proof!==before.proof)throw fail("STATE_CONFLICT","IPsec изменился после durable intent; PATCH не отправлен") + await source();dispatched=true + }) + completedCommands=1 + }catch(error){if(!dispatched&&error instanceof McpError)throw error;throw fail("MUTATION_OUTCOME_UNKNOWN","Исход PATCH IPsec не подтверждён; повтор запрещён")} + } + let after:State + try{after=await readIpsecPeerUpdateState(server,source)}catch{throw fail("MUTATION_OUTCOME_UNKNOWN","Read-back IPsec недоступен; повтор запрещён")} + if(after.proof!==desired)throw fail("VERIFICATION_FAILED","PATCH или точная сохранность конфигурации и ссылок IPsec не подтверждены; повтор запрещён") + try{await source();await options.checkpoint?.({receiptId:options.receiptId??"",commandCount:plan.commandCount,completedCommands,afterProof:after.proof})}catch{throw fail("MUTATION_OUTCOME_UNKNOWN","Проверенный PATCH checkpoint не сохранён; повтор запрещён")} + const reader={get:async(path:string)=>{await source();if(!Object.hasOwn(after.catalogs,path))throw fail("STATE_UNAVAILABLE","Неизвестный каталог истории IPsec");return structuredClone(after.catalogs[path])}}as unknown as MikrotikClient + let historyRecorded=false + try{await source();historyRecorded=Boolean(await(options.recordRevision??captureAndAppendRevision)({serverId:server.id,section:"ipsec",source:"apply",capture:()=>captureIpsecSnapshot(server,reader)}))}catch{/* A history failure never replays the native PATCH. */} + return{ok:true,verified:true,receiptId:options.receiptId??null,peerId:id,commandCount:plan.commandCount,completedCommands,afterProof:after.proof,historyRecorded,revisionRecorded:historyRecorded,renameCompanions:plan.companions.length,nativeRenameAcceptanceVerified:false,forwardingVerified:false,automaticReplay:false,state:"applied"} + }))) +} diff --git a/backend/src/services/ipsec-pool-dependencies.test.ts b/backend/src/services/ipsec-pool-dependencies.test.ts new file mode 100644 index 00000000..35b38c1a --- /dev/null +++ b/backend/src/services/ipsec-pool-dependencies.test.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import { ipsecPoolDependencyPaths,assertPoolMutationDependencies,ipsecPoolDependencies } from "./ipsec-pool-dependencies.js" + +type Row=Record +const state=()=>{const c:Record=Object.fromEntries(ipsecPoolDependencyPaths.map(path=>[path,[]]));c["/ip/pool"]=[{".id":"*a",name:"owned"},{".id":"*b",name:"fallback"}];return c} +for(const path of ipsecPoolDependencyPaths){const c=state();delete c[path];assert.throws(()=>ipsecPoolDependencies(c),"No unsupported/missing source is inferred to be empty")} +for(const [path,fields]of [["/ip/dhcp-server",["address-pool"]],["/ppp/profile",["local-address","remote-address"]],["/ppp/secret",["local-address","remote-address"]],["/ip/hotspot",["address-pool"]],["/ip/hotspot/user/profile",["address-pool"]],["/ip/ipsec/mode-config",["address-pool"]],["/ip/pool",["next-pool"]]]as [string,string[]][]){ + for(const field of fields)for(const reference of ["owned","*A","*000a","fallback, owned"]){ + const c=state();c[path].push({".id":"*c",name:"consumer",disabled:"true",[field]:reference}) + assert.throws(()=>assertPoolMutationDependencies(c,[c["/ip/pool"][0]])) + const affected=assertPoolMutationDependencies(c,[c["/ip/pool"][0]],[{path,id:"*000C"}]);assert.ok(affected.some(edge=>edge.field===field)) + } +} +const scalar=state();scalar["/ip/pool"].push({".id":"*c",name:"a,b"});scalar["/ip/dhcp-server"].push({".id":"*d","address-pool":"a,b"});assert.equal(ipsecPoolDependencies(scalar)[0].poolId,"*c") +const literal=state();literal["/ppp/profile"].push({".id":"*c","local-address":"192.0.2.1","remote-address":"2001:db8::1","remote-ipv6-prefix-pool":"owned","dhcpv6-pd-pool":"owned"});literal["/ipv6/address"]=[{".id":"*d","from-pool":"owned"}];assert.equal(assertPoolMutationDependencies(literal,[literal["/ip/pool"][0]]).length,0,"IPv6 pool namespace and literal addresses do not alias /ip/pool") +const cycle=state();cycle["/ip/pool"][0]["next-pool"]="fallback";cycle["/ip/pool"][1]["next-pool"]="owned";assert.throws(()=>ipsecPoolDependencies(cycle)) +const indirect=state();indirect["/ip/pool"][0]["next-pool"]="fallback";indirect["/ip/dhcp-server"].push({".id":"*c","address-pool":"owned"});assert.throws(()=>assertPoolMutationDependencies(indirect,[indirect["/ip/pool"][1]],[{path:"/ip/pool",id:"*a"}]),"An allowed predecessor pool cannot hide its foreign allocator") +assert.equal(assertPoolMutationDependencies(indirect,[indirect["/ip/pool"][1]],[{path:"/ip/pool",id:"*a"},{path:"/ip/dhcp-server",id:"*c"}]).length,2) +const absent=state();absent["/ip/dhcp-server"].push({".id":"*c","address-pool":"unresolved"});assert.throws(()=>ipsecPoolDependencies(absent)) +const ambiguous=state();ambiguous["/ip/pool"].push({".id":"*c",name:"*000A"});assert.throws(()=>ipsecPoolDependencies(ambiguous)) +const duplicate=state();duplicate["/ip/pool"].push({".id":"*00A",name:"duplicate"});assert.throws(()=>ipsecPoolDependencies(duplicate)) +const malformed=state();malformed["/ip/dhcp-server"].push({".id":"*c","address-pool":"owned,"});assert.throws(()=>ipsecPoolDependencies(malformed)) +assert.deepEqual(ipsecPoolDependencies(state()),[]) +console.log("IPsec IPv4 pool dependency model: complete sources, DHCP/PPP/Hotspot/IPsec/fallback edges, aliases/scalar priority, explicit closure, cycles and IPv6 namespace passed") diff --git a/backend/src/services/ipsec-pool-dependencies.ts b/backend/src/services/ipsec-pool-dependencies.ts new file mode 100644 index 00000000..69394d5e --- /dev/null +++ b/backend/src/services/ipsec-pool-dependencies.ts @@ -0,0 +1,94 @@ +import { isIP } from "node:net" +import { McpError } from "../modules/mcp/security.js" + +type Row=Record +export interface IpsecPoolConsumer {path:string;id:string} +export interface IpsecPoolDependency extends IpsecPoolConsumer {field:string;poolId:string} +/** IPv4 address-pool consumers. VPN servers inherit /ppp/profile; reading every + * profile and secret covers those direct and inherited pool selections without + * inventing a separate edge from a server's default-profile to an IPv4 pool. + * /ipv6/pool is a distinct namespace and is deliberately not resolved here. */ +export const ipsecPoolDependencyFields={ + "/ip/pool":["next-pool"], + "/ip/ipsec/mode-config":["address-pool"], + "/ip/dhcp-server":["address-pool"], + "/ppp/profile":["local-address","remote-address"], + "/ppp/secret":["local-address","remote-address"], + "/ip/hotspot":["address-pool"], + "/ip/hotspot/user/profile":["address-pool"], +} as const +export const ipsecPoolDependencyPaths=Object.keys(ipsecPoolDependencyFields) as Array +export const ipsecExternalPoolDependencyPaths=ipsecPoolDependencyPaths.filter(path=>path!=="/ip/pool"&&path!=="/ip/ipsec/mode-config") +const id=(value:string)=>/^\*[a-z0-9]+$/i.test(value)&&value.length<=100 +const idKey=(value:string)=>value.toLowerCase().replace(/^\*0+(?=[a-z0-9])/ ,"*") +const fail=(message:string,code="STATE_UNAVAILABLE"):never=>{throw new McpError(code,message,code==="STATE_UNAVAILABLE"?502:409)} +const objectKey=(path:string,value:string)=>path+":"+idKey(value) + +/** Pure, fail-closed graph inventory. Callers must collect every required raw + * catalog under their live source/permission guard, bound and seal all rows, + * and repeat the same graph check immediately before every native dispatch. + * Missing/unsupported endpoints must remain errors rather than empty arrays. */ +export function ipsecPoolDependencies(catalogs:Record):IpsecPoolDependency[]{ + let count=0,bytes=0 + for(const path of ipsecPoolDependencyPaths){ + const rows=catalogs[path] + if(!Array.isArray(rows)||rows.length>2000||rows.some(row=>!row||typeof row!=="object"||Array.isArray(row)||typeof row[".id"]!=="string"||!id(row[".id"])||Object.values(row).some(value=>typeof value!=="string"))||new Set(rows.map(row=>idKey(row[".id"]))).size!==rows.length)fail("Неполный каталог потребителей пула IPsec") + count+=rows.length;bytes+=Buffer.byteLength(JSON.stringify(rows)) + if(count>10000||bytes>1048576)fail("Превышен бюджет графа потребителей пула IPsec") + } + const pools=catalogs["/ip/pool"] + if(pools.some(row=>!row.name||row.name.trim()!==row.name)||new Set(pools.map(row=>row.name)).size!==pools.length)fail("Неоднозначные имена IPv4 pools") + const resolve=(value:string)=>{ + const matches=pools.filter(pool=>pool.name===value||id(value)&&idKey(pool[".id"])===idKey(value)) + if(matches.length>1)fail("Неоднозначная ссылка на IPv4 pool") + return matches[0] + } + // A name that aliases another native ID makes all reference interpretation ambiguous. + for(const pool of pools)if(id(pool.name)&&pools.some(other=>other!==pool&&idKey(other[".id"])===idKey(pool.name)))fail("Имя IPv4 pool совпадает с native ID другого объекта") + const edges:IpsecPoolDependency[]=[] + for(const path of ipsecPoolDependencyPaths)for(const row of catalogs[path])for(const field of ipsecPoolDependencyFields[path]){ + const raw=row[field]?.trim() + if(!raw)continue + // An exact scalar name (including a comma) takes precedence over CSV parsing. + const exact=resolve(raw),tokens=exact?[raw]:raw.split(",").map(value=>value.trim()) + if(tokens.some(token=>!token))fail("Повреждена ссылка потребителя IPv4 pool") + for(const token of tokens){ + const pool=exact??resolve(token) + if(pool){edges.push({path,id:row[".id"],field,poolId:pool[".id"]});continue} + if(["none","static-only","default",""].includes(token))continue + // PPP per-secret/profile values can be literal addresses. Missing values + // inherit a profile, whose own row is included in the same graph. + if((path==="/ppp/profile"||path==="/ppp/secret")&&isIP(token)>0)continue + fail("Не подтверждена ссылка потребителя IPv4 pool") + } + } + // Pool fallback chains must terminate; cycles defeat a finite deletion closure. + const children=new Map(pools.map(pool=>[idKey(pool[".id"]),[]as string[]])) + for(const edge of edges)if(edge.path==="/ip/pool")children.get(idKey(edge.id))!.push(idKey(edge.poolId)) + const done=new Set(),active=new Set() + for(const root of children.keys()){ + const stack=[{key:root,exit:false}] + while(stack.length){const item=stack.pop()!;if(done.has(item.key))continue;if(item.exit){active.delete(item.key);done.add(item.key);continue}if(active.has(item.key))fail("Циклическая цепочка next-pool","STATE_CONFLICT");active.add(item.key);stack.push({key:item.key,exit:true});for(const key of children.get(item.key)!)stack.push({key,exit:false})} + } + return edges +} + +/** An allow-list is the caller's explicit reviewed mutation/dependency closure, + * never an inference from a marker/comment. A pool range update affects every + * consumer just as deletion does, including disabled foreign consumers. */ +export function assertPoolMutationDependencies(catalogs:Record,poolRows:Row[],allowedConsumers:IpsecPoolConsumer[]=[]):IpsecPoolDependency[]{ + const edges=ipsecPoolDependencies(catalogs),targets=new Set(),allowed=new Set(allowedConsumers.map(ref=>objectKey(ref.path,ref.id))) + for(const target of poolRows){ + if(!target||!id(target[".id"]??""))fail("Не подтверждён native ID изменяемого IPv4 pool") + const actual=catalogs["/ip/pool"].filter(row=>idKey(row[".id"])===idKey(target[".id"])) + if(actual.length!==1||actual[0].name!==target.name)fail("Изменяемый IPv4 pool отсутствует или неоднозначен") + targets.add(idKey(target[".id"])) + } + // A changed fallback pool also affects allocations requested from every + // predecessor pool, so inspect those predecessors' external consumers too. + let changed=true + while(changed){changed=false;for(const edge of edges)if(edge.path==="/ip/pool"&&targets.has(idKey(edge.poolId))&&!targets.has(idKey(edge.id))){targets.add(idKey(edge.id));changed=true}} + const affected=edges.filter(edge=>targets.has(idKey(edge.poolId))) + if(affected.some(edge=>!allowed.has(objectKey(edge.path,edge.id))))fail("Изменяемый IPv4 pool используется объектом вне подтверждённого плана","DEPENDENCY_CONFLICT") + return affected +} diff --git a/backend/src/services/ipsec-proof-telemetry.test.ts b/backend/src/services/ipsec-proof-telemetry.test.ts new file mode 100644 index 00000000..22682fb6 --- /dev/null +++ b/backend/src/services/ipsec-proof-telemetry.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict" +import { ipsecPeerDeletePaths, ipsecPeerDeleteValue, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { ipsecPeerUpdateValue } from "./ipsec-peer-update.js" +import { ipsecUserUpdateValue } from "./ipsec-user-update.js" + +const state: IpsecPeerDeleteState = { source: "fixed-source", proof: "unused", protections: [], catalogs: Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])) } +state.catalogs["/certificate"] = [{ ".id": "*a", name: "certificate", "expires-after": "1d", "invalid-before": "2026-01-01", "invalid-after": "2027-01-01", trusted: "true", crl: "false", secret: "private", opaque: "preserve" }] +state.catalogs["/ip/ipsec/identity"] = [{ ".id": "*b", certificate: "certificate", "expires-after": "opaque-not-telemetry-here", disabled: "false" }] +const frozen = structuredClone(state) +for (const value of [ipsecPeerDeleteValue, ipsecPeerUpdateValue, ipsecUserUpdateValue]) { + const countdown = structuredClone(state); countdown.catalogs["/certificate"][0]["expires-after"] = "23h59m59s" + assert.deepEqual(value(countdown), value(state)) + for (const key of ["invalid-before", "invalid-after", "trusted", "crl", "secret", "opaque", "expires-after-extra", "ca", "expired"]) { + const changed = structuredClone(state); changed.catalogs["/certificate"][0][key] = "changed" + assert.notDeepEqual(value(changed), value(state), key) + } + const otherPath = structuredClone(state); otherPath.catalogs["/ip/ipsec/identity"][0]["expires-after"] = "changed" + assert.notDeepEqual(value(otherPath), value(state)) + assert.deepEqual(state, frozen) +} +console.log("IPsec proof v2: only certificate expires-after countdown is excluded; absolute validity, trust, CRL, secrets, references, unknown fields and same-named fields on other paths remain sealed") diff --git a/backend/src/services/ipsec-restore.test.ts b/backend/src/services/ipsec-restore.test.ts index 3747e0c5..15d107b3 100644 --- a/backend/src/services/ipsec-restore.test.ts +++ b/backend/src/services/ipsec-restore.test.ts @@ -1,5 +1,5 @@ import assert from "node:assert/strict" -import { canonicalIpsecSnapshot, planIpsecRestore } from "./entity-snapshots.js" +import { canonicalIpsecSnapshot, parseIpsecSnapshot, planIpsecRestore } from "./entity-snapshots.js" const snapshot = canonicalIpsecSnapshot({ peers: [{ name: "ipsec-vpn", comment: "MikrotikManager:ipsec", exchangeMode: "ike2", profile: "ipsec-vpn" }], pools: [{ name: "ipsec-vpn", ranges: "198.18.80.2-198.18.80.254", comment: "MikrotikManager:ipsec" }], @@ -35,3 +35,22 @@ assert.throws(()=>planIpsecRestore(canonicalIpsecSnapshot({nat:[{chain:"srcnat", console.log("IPsec NAT history: retains egress/policy, rejects legacy unscoped masquerade") assert.throws(()=>planIpsecRestore(scopedNat,{peers:[],identities:[],modeConfigs:[],pools:[],nat:[{...scopedNat.nat[0]!,outInterface:undefined,outInterfaceList:"WAN",rosId:"*N"}]}),/Снимите его вручную/) assert(natOp&&"body" in natOp&&natOp.body["out-interface-list"]===undefined,"Never send empty ambiguous RouterOS list value") + +// Corrupt persisted data must fail before a planner can interpret it as an +// intentional empty section and remove managed objects. +const complete = canonicalIpsecSnapshot(snapshot) +assert.deepEqual(parseIpsecSnapshot(complete), complete) +assert.deepEqual(parseIpsecSnapshot(canonicalIpsecSnapshot({})), canonicalIpsecSnapshot({}), "Explicit complete empty snapshot remains a valid rollback") +for (const invalid of [null, undefined, [], "{}", {}, { ...complete, nat: undefined }, { ...complete, pools: {} }, { ...complete, extra: true }, + { ...complete, peers: [{ ...complete.peers[0]!, disabled: "false" }] }, + { ...complete, peers: [{ ...complete.peers[0]!, name: "" }] }, + { ...complete, peers: [{ ...complete.peers[0]!, address: " 0.0.0.0/0 " }] }, + { ...complete, peers: [{ ...complete.peers[0]!, extra: "ignored" }] }, + { ...complete, peers: [complete.peers[0]!, complete.peers[0]!] }, + { ...complete, identities: [complete.identities[0]!, complete.identities[0]!] }, + { ...complete, identities: [{ ...complete.identities[0]!, comment: "" }] }, + { ...complete, identities: [{ ...complete.identities[0]!, secret: "x".repeat(1048577) }] }, +]) assert.throws(() => parseIpsecSnapshot(invalid), /ревизи[ия] IPsec/) +const circular: Record = { ...complete }; circular.self = circular +assert.throws(() => parseIpsecSnapshot(circular), /ревизия IPsec/) +console.log("IPsec restore revision validation: malformed/partial/oversized/ambiguous payloads refuse before planning") diff --git a/backend/src/services/ipsec-revision-restore-model.test.ts b/backend/src/services/ipsec-revision-restore-model.test.ts new file mode 100644 index 00000000..dd22b741 --- /dev/null +++ b/backend/src/services/ipsec-revision-restore-model.test.ts @@ -0,0 +1,81 @@ +import assert from "node:assert/strict" +import { canonicalIpsecSnapshot } from "./entity-snapshots.js" +import { ipsecPeerDeletePaths } from "./ipsec-peer-delete.js" +import { ipsecExternalPoolDependencyPaths } from "./ipsec-pool-dependencies.js" +import { ipsecManagedComment } from "./ipsec-config.js" +import { planIpsecRevisionRestore, projectIpsecRevisionRestore, type IpsecRevisionRestoreState } from "./ipsec-revision-restore-model.js" +import { ipsecServerInitValue } from "./ipsec-server-init.js" +const comment = ipsecManagedComment("restore") +const base = (): IpsecRevisionRestoreState => ({ source: "source", proof: "unused", protections: [], catalogs: { + ...Object.fromEntries([...ipsecPeerDeletePaths, ...ipsecExternalPoolDependencyPaths, "/ip/route", "/interface", "/interface/list", "/interface/list/member"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*1", name: "owned-peer", address: "0.0.0.0/0", "exchange-mode": "ike2", profile: "existing-profile", passive: "true", disabled: "false", comment, opaque: "NEVER_PEER" }], + "/ip/ipsec/identity": [{ ".id": "*2", peer: "owned-peer", "auth-method": "pre-shared-key", secret: "NEVER_PSK", "remote-id": "old-id", "mode-config": "ipsec-vpn", comment, disabled: "false", opaque: "NEVER_IDENTITY" }, { ".id": "*3", peer: "owned-peer", "auth-method": "digital-signature", certificate: "server-cert", "mode-config": "ipsec-vpn", "generate-policy": "port-strict", "policy-template-group": "group", comment: ipsecManagedComment("cert"), disabled: "false" }], + "/ip/ipsec/mode-config": [{ ".id": "*4", name: "ipsec-vpn", "address-pool": "owned-pool", responder: "true", "static-dns": "192.0.2.53", opaque: "NEVER_MC" }], + "/ip/pool": [{ ".id": "*5", name: "owned-pool", ranges: "10.77.0.2-10.77.0.254", comment, opaque: "NEVER_POOL" }], + "/ip/ipsec/policy": [{ ".id": "*6", template: "true", group: "group", "src-address": "0.0.0.0/0", "dst-address": "10.77.0.0/24", proposal: "proposal", comment, opaque: "NEVER_POLICY" }], + "/ip/firewall/nat": [{ ".id": "*7", chain: "srcnat", action: "masquerade", "src-address": "10.77.0.0/24", "out-interface-list": "WAN", "ipsec-policy": "out,none", comment, opaque: "NEVER_NAT" }], + "/ip/ipsec/profile": [{ ".id": "*8", name: "existing-profile", opaque: "NEVER_PROFILE" }], + "/ip/ipsec/proposal": [{ ".id": "*9", name: "proposal", opaque: "NEVER_PROPOSAL" }], + "/ip/ipsec/policy/group": [{ ".id": "*a", name: "group", opaque: "NEVER_GROUP" }], + "/certificate": [{ ".id": "*b", name: "ca", fingerprint: "signed-ca", "invalid-after": "2030-01-01" }, { ".id": "*c", name: "server-cert", ca: "ca", fingerprint: "signed", "invalid-after": "2030-01-01", "private-key": "true", opaque: "NEVER_CERT" }], + "/interface": [{ ".id": "*d", name: "ether1" }], "/interface/list": [{ ".id": "*e", name: "WAN" }], +} }) +const desired = canonicalIpsecSnapshot({ + peers: [{ name: "owned-peer", address: "", exchangeMode: "ike2", profile: "existing-profile", passive: true, disabled: false, comment }], + identities: [{ peerName: "owned-peer", authMethod: "pre-shared-key", secret: "(hidden)", modeConfig: "ipsec-vpn", comment }, { peerName: "owned-peer", authMethod: "digital-signature", certificate: "server-cert", modeConfig: "ipsec-vpn", generatePolicy: "port-strict", policyTemplateGroup: "group", comment: ipsecManagedComment("cert") }], + modeConfigs: [{ name: "ipsec-vpn", addressPool: "owned-pool", staticDns: "" }], pools: [{ name: "owned-pool", ranges: "10.77.0.2-10.77.0.200", comment }], + policies: [{ srcAddress: "0.0.0.0/0", dstAddress: "10.78.0.0/24", proposal: "proposal", comment }], + nat: [{ chain: "srcnat", action: "masquerade", srcAddress: "10.78.0.0/24", outInterface: "ether1", comment }], +}) +const state = base(), before = structuredClone(state), plan = planIpsecRevisionRestore(state, desired) +assert.deepEqual(state, before); assert.equal(plan.commands.length, 7) +const identity = plan.commands.find(command => command.path === "/ip/ipsec/identity" && command.id === "*2")! +assert(!Object.hasOwn(identity.fields, "secret")); assert.equal(identity.fields["remote-id"], "") +const nat = plan.commands.find(command => command.path === "/ip/firewall/nat")! +const mcCommand = plan.commands.find(command => command.path === "/ip/ipsec/mode-config")! +assert(!Object.hasOwn(mcCommand.fields, "responder")); assert(!Object.hasOwn(mcCommand.fields, "address-prefix-length")) +assert.equal(nat.fields["out-interface-list"], ""); assert.equal(nat.fields["ipsec-policy"], "") +assert(!plan.commands.some(command => /certificate|profile|proposal|group/.test(command.path))) +let projected = state +for (const command of plan.commands) projected = projectIpsecRevisionRestore(projected, command) +assert.equal(projected.catalogs["/ip/ipsec/identity"][0].secret, "NEVER_PSK") +assert.equal(projected.catalogs["/ip/ipsec/policy"][0]["dst-address"], "10.78.0.0/24") +assert.equal(projected.catalogs["/ip/ipsec/policy"][0].opaque, "NEVER_POLICY") +assert.deepEqual(projected.catalogs["/certificate"], state.catalogs["/certificate"]) +const unusualMc = base(); unusualMc.catalogs["/ip/ipsec/mode-config"][0].responder = "false"; unusualMc.catalogs["/ip/ipsec/mode-config"][0]["address-prefix-length"] = "24" +let unusualAfter = unusualMc; for (const command of planIpsecRevisionRestore(unusualMc, desired).commands) unusualAfter = projectIpsecRevisionRestore(unusualAfter, command) +assert.equal(unusualAfter.catalogs["/ip/ipsec/mode-config"][0].responder, "false"); assert.equal(unusualAfter.catalogs["/ip/ipsec/mode-config"][0]["address-prefix-length"], "24") +const omitted = projectIpsecRevisionRestore(state, nat, { observedRow: { ".id": "*7" } }) +assert(!Object.hasOwn(omitted.catalogs["/ip/firewall/nat"][0], "out-interface-list")); assert.equal(omitted.catalogs["/ip/firewall/nat"][0].opaque, "NEVER_NAT") +for (const mutate of [ + (s: IpsecRevisionRestoreState) => { s.catalogs["/certificate"] = [] }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/certificate"][1]["private-key"] = "false" }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/profile"] = [] }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/proposal"] = [] }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/policy/group"] = [] }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/policy"] = [] }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/mode-config"] = [] }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/identity"][0]["auth-method"] = "digital-signature" }, + (s: IpsecRevisionRestoreState) => { delete s.catalogs["/ip/ipsec/identity"][0].secret }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/pool"][0].dynamic = "true" }, + (s: IpsecRevisionRestoreState) => { s.catalogs["/ip/ipsec/policy"].push({ ".id": "*foreign", peer: "other, *0001", comment: "foreign" }) }, +]) { const bad = base(); mutate(bad); assert.throws(() => planIpsecRevisionRestore(bad, desired)) } +for (const path of ipsecExternalPoolDependencyPaths) { + const bad = base(); bad.catalogs[path].push({ ".id": "*allocator", [path.startsWith("/ppp/") ? "remote-address" : "address-pool"]: "*0005" }); assert.throws(() => planIpsecRevisionRestore(bad, desired)) +} +for (const change of [{ peers: [{ ...desired.peers[0], certificate: "server-cert" }] }, { modeConfigs: [{ ...desired.modeConfigs[0], comment }] }, { policies: [...desired.policies, { ...desired.policies[0], comment: ipsecManagedComment("missing") }] }, { identities: [...desired.identities, { ...desired.identities.find(row => row.authMethod === "pre-shared-key")!, comment: ipsecManagedComment("new-hidden") }] }]) assert.throws(() => planIpsecRevisionRestore(state, { ...desired, ...change })) +const create = { ...desired, pools: [...desired.pools, { name: "new-pool", ranges: "192.0.2.2-192.0.2.10", comment: ipsecManagedComment("new") }] } +const creation = planIpsecRevisionRestore(state, canonicalIpsecSnapshot(create)).commands.find(command => command.action === "create")! +assert.throws(() => projectIpsecRevisionRestore(state, creation, { observedRow: { ".id": "*new", ...creation.fields } })) +const created = projectIpsecRevisionRestore(state, creation, { assignedId: "*new", observedRow: { ".id": "*new", ...creation.fields, "opaque-default": "allowed-only-new" } }) +assert.equal(created.catalogs["/ip/pool"].at(-1)!["opaque-default"], "allowed-only-new") +const identityCreatePlan = planIpsecRevisionRestore(state, canonicalIpsecSnapshot({ ...desired, identities: [...desired.identities, { ...desired.identities.find(row => row.authMethod === "pre-shared-key")!, secret: "NEVER_NEW_PSK", comment: ipsecManagedComment("new-explicit") }] })) +const identityCreate = identityCreatePlan.commands.find(command => command.action === "create" && command.path === "/ip/ipsec/identity")! +assert.equal(identityCreate.expectedFields!["remote-id"], ""); assert(!Object.hasOwn(identityCreate.fields, "remote-id")) +assert.throws(() => projectIpsecRevisionRestore(state, identityCreate, { assignedId: "*new", observedRow: { ".id": "*new", ...identityCreate.fields, "remote-id": "auto" } })) +projectIpsecRevisionRestore(state, identityCreate, { assignedId: "*new", observedRow: { ".id": "*new", ...identityCreate.fields } }) +assert.throws(() => planIpsecRevisionRestore(state, { ...desired, peers: [...desired.peers, { ...desired.peers[0], name: "new-peer", profile: "" }] })) +const drift = structuredClone(state); drift.catalogs["/ip/ipsec/profile"][0].opaque = "changed"; assert.notDeepEqual(ipsecServerInitValue(drift), ipsecServerInitValue(state)) +assert.throws(() => planIpsecRevisionRestore(state, {})) +assert.throws(() => planIpsecRevisionRestore(state, { ...desired, identities: [{ ...desired.identities[0], authMethod: "unknown-secret-mode", comment: ipsecManagedComment("unknown"), secret: "(hidden)" }] })) +console.log("IPsec revision model: complete recorded fields/policies, exact clears, hidden PSK preservation, ACK defaults, PKI/profile/group/proposal/NAT/pool/foreign/system preflight and explicit unsupported policy creation") diff --git a/backend/src/services/ipsec-revision-restore-model.ts b/backend/src/services/ipsec-revision-restore-model.ts new file mode 100644 index 00000000..78a7c48e --- /dev/null +++ b/backend/src/services/ipsec-revision-restore-model.ts @@ -0,0 +1,161 @@ +import { McpError } from "../modules/mcp/security.js" +import { isHiddenSecret, parseIpsecSnapshot } from "./entity-snapshots.js" +import { isIpsecManagedComment, isManagedIpsecModeConfig } from "./ipsec-config.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { validateIpsecServerInitState, type IpsecServerInitState } from "./ipsec-server-init-model.js" +import { assertPoolMutationDependencies, ipsecPoolDependencies } from "./ipsec-pool-dependencies.js" + +type Row = Record +export type IpsecRevisionRestoreState = IpsecServerInitState +export interface IpsecRevisionRestoreCommand { action: "create" | "update" | "delete"; path: string; id?: string; fields: Row; expectedFields?: Row } +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, code === "STATE_UNAVAILABLE" ? 502 : 409) +const idKey = (value: string) => value.toLowerCase().replace(/^\*0+(?=[a-z0-9])/, "*") +const nativeId = (value: string) => /^\*[a-z0-9]+$/i.test(value) && value.length <= 100 +const paths = { peers: "/ip/ipsec/peer", identities: "/ip/ipsec/identity", modeConfigs: "/ip/ipsec/mode-config", pools: "/ip/pool", policies: "/ip/ipsec/policy", nat: "/ip/firewall/nat" } +const kinds: Record = { "/ip/ipsec/peer": "ipsec_peer", "/ip/ipsec/identity": "ipsec_identity", "/ip/ipsec/mode-config": "ipsec_mode_config", "/ip/pool": "ip_pool", "/ip/ipsec/policy": "ipsec_policy", "/ip/firewall/nat": "ip_nat", "/certificate": "certificate", "/ip/ipsec/profile": "ipsec_profile", "/ip/ipsec/proposal": "ipsec_proposal", "/ip/ipsec/policy/group": "ipsec_policy_group", "/interface": "interface", "/interface/list": "interface_list" } +const refs: Record = { peer: "/ip/ipsec/peer", "mode-config": "/ip/ipsec/mode-config", "address-pool": "/ip/pool", "next-pool": "/ip/pool", certificate: "/certificate", "remote-certificate": "/certificate", ca: "/certificate", profile: "/ip/ipsec/profile", proposal: "/ip/ipsec/proposal", group: "/ip/ipsec/policy/group", "policy-template-group": "/ip/ipsec/policy/group", "out-interface": "/interface", "out-interface-list": "/interface/list" } +const owned = (path: string, row: Row) => path === paths.modeConfigs ? isManagedIpsecModeConfig(row) : isIpsecManagedComment(row.comment) +const matches = (value: string, row: Row) => value.trim() === row.name || (nativeId(value.trim()) && idKey(value.trim()) === idKey(row[".id"])) +const tokens = (catalogs: Record, path: string, value: string) => catalogs[path]?.some(row => matches(value, row)) ? [value] : value.split(",").map(part => part.trim()) +function protect(row: Row) { for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) throw fail("Системный объект не может быть целью отката IPsec", "PROTECTED_OBJECT") } + +/** Restores every recorded field. Snapshot policy rows omit native identity and + * template/security context: they may update only a uniquely matched existing + * managed policy. Missing policies are rejected before any native effect. */ +export function planIpsecRevisionRestore(state: IpsecRevisionRestoreState, raw: unknown) { + const desired = parseIpsecSnapshot(raw) + validateIpsecServerInitState(state, true) + for (const path of ["/interface/list", "/interface/list/member"]) { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000 || rows.some(row => !row || !nativeId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(rows.map(row => idKey(row[".id"]))).size !== rows.length) throw fail("Неполный каталог зависимостей NAT", "STATE_UNAVAILABLE") + } + const c = state.catalogs, commands: IpsecRevisionRestoreCommand[] = [], upserts: IpsecRevisionRestoreCommand[] = [], deletes: IpsecRevisionRestoreCommand[] = [] + const targets = new Set(), desiredRows = new Map(), desiredKeys = new Map>() + for (const [section, path] of Object.entries(paths)) { + const rows: Array> = desired[section as keyof typeof paths].map(row => ({ ...row })) + const keyField = ["identities", "policies", "nat"].includes(section) ? "comment" : "name" + if (new Set(rows.map(row => row[keyField])).size !== rows.length) throw fail("Неоднозначный маркер объекта ревизии IPsec") + desiredKeys.set(path, new Set(rows.map(row => String(row[keyField])))) + const current = c[path].filter(row => !(path === paths.policies && observedBoolean(row.dynamic) === true)) + const currentKeys = current.map(row => row[keyField]?.trim()).filter(Boolean) + if (new Set(currentKeys).size !== currentKeys.length) throw fail("Неоднозначное имя или маркер native объекта IPsec") + const planned: Row[] = [] + for (const want of rows) { + const key = String(want[keyField]), live = current.find(row => row[keyField]?.trim() === key) + const marker = section === "modeConfigs" ? isManagedIpsecModeConfig({ name: String(want.name), comment: String(want.comment) }) : isIpsecManagedComment(String(want.comment)) + if (!marker) throw fail("Ревизия содержит чужой объект IPsec", "PROTECTED_OBJECT") + if (live) { if (live[keyField] !== key || !owned(path, live)) throw fail("Имя или маркер ревизии занят чужим объектом", "PROTECTED_OBJECT"); protect(live) } + let fields: Row + if (section === "peers") { + // RouterOS versions differ on legacy peer.certificate support. Avoid + // silently discarding the recorded value or guessing a native capability. + if (String(want.certificate) !== (live?.certificate ?? "")) throw fail("Изменение legacy peer.certificate не подтверждено данной схемой RouterOS; нужен отдельный native план", "UNSUPPORTED_CAPABILITY") + if (!live && (!want.profile || !want.address || !want.exchangeMode)) throw fail("Новый peer не может подтвердить пустые profile/address/exchange-mode: RouterOS подставляет native defaults", "UNSUPPORTED_CAPABILITY") + fields = { name: String(want.name), address: String(want.address), "exchange-mode": String(want.exchangeMode), passive: want.passive ? "yes" : "no", profile: String(want.profile), comment: String(want.comment), disabled: want.disabled ? "yes" : "no" } + } else if (section === "identities") { + if (!["pre-shared-key", "digital-signature"].includes(String(want.authMethod))) throw fail("Метод auth-method ревизии не поддержан проверенным откатом; скрытый секрет неизвестного метода восстановить нельзя", "UNSUPPORTED_CAPABILITY") + if (want.authMethod === "digital-signature" && (!want.certificate || want.certificate === "none")) throw fail("Ревизия digital-signature identity не содержит серверный сертификат", "DEPENDENCY_CONFLICT") + if (want.authMethod === "pre-shared-key" && isHiddenSecret(String(want.secret)) && (!live || live["auth-method"] !== "pre-shared-key" || !live.secret)) throw fail("Скрытый PSK новой identity или неизвестный прежний PSK нельзя восстановить без значения секрета", "UNSUPPORTED_CAPABILITY") + fields = { peer: String(want.peerName), "auth-method": String(want.authMethod), certificate: String(want.certificate), "remote-certificate": String(want.remoteCertificate), "match-by": String(want.matchBy), "remote-id": String(want.remoteId), "mode-config": String(want.modeConfig), "generate-policy": String(want.generatePolicy), ...(want.policyTemplateGroup !== undefined ? { "policy-template-group": String(want.policyTemplateGroup) } : {}), comment: String(want.comment), disabled: want.disabled ? "yes" : "no", ...(!isHiddenSecret(String(want.secret)) ? { secret: String(want.secret) } : {}) } + } else if (section === "modeConfigs") { + if (!live) throw fail("Снимок mode-config не содержит responder/address-prefix-length; отсутствующий объект нельзя безопасно восстановить", "UNSUPPORTED_CAPABILITY") + if (String(want.comment) !== (live?.comment ?? "")) throw fail("Схема RouterOS mode-config не подтверждает запись comment; поле ревизии требует отдельного native плана", "UNSUPPORTED_CAPABILITY") + fields = { name: String(want.name), "address-pool": String(want.addressPool), address: String(want.address), "static-dns": String(want.staticDns) } + } else if (section === "pools") { + if (!live && !want.ranges) throw fail("Новый IP pool не может восстановить пустые ranges", "UNSUPPORTED_CAPABILITY") + fields = { name: String(want.name), ranges: String(want.ranges), comment: String(want.comment) } + } + else if (section === "policies") { + if (!live) throw fail("Ревизия политики не содержит template/group/action/tunnel/peer: отсутствующую native policy невозможно безопасно восстановить", "UNSUPPORTED_CAPABILITY") + fields = { "src-address": String(want.srcAddress), "dst-address": String(want.dstAddress), proposal: String(want.proposal), comment: String(want.comment) } + } else { + if (want.action === "masquerade" && !want.outInterface && !want.outInterfaceList) throw fail("Ревизия NAT не содержит исходящий интерфейс", "UNSUPPORTED_CAPABILITY") + fields = { chain: String(want.chain), action: String(want.action), "src-address": String(want.srcAddress), comment: String(want.comment), "out-interface": String(want.outInterface ?? ""), "out-interface-list": String(want.outInterfaceList ?? ""), "ipsec-policy": String(want.ipsecPolicy ?? "") } + } + // Empty fields in a create are omitted only when no previous value exists; + // exact PATCH clears remain explicit and are checked after dispatch. + const expectedFields = { ...fields, ...(section === "peers" ? { certificate: String(want.certificate) } : {}) } + if (!live) fields = Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== "")) + const command = { action: live ? "update" : "create", path, fields, ...(live ? { id: live[".id"] } : { expectedFields }) } as IpsecRevisionRestoreCommand + upserts.push(command) + if (live) targets.add(`${path}:${live[".id"]}`) + planned.push({ ...(live ?? { ".id": `*planned${upserts.length}` }), ...fields }) + } + desiredRows.set(path, planned) + for (const live of current) if (owned(path, live) && !desiredKeys.get(path)!.has(live[keyField])) { protect(live); deletes.push({ action: "delete", path, id: live[".id"], fields: {} }); targets.add(`${path}:${live[".id"]}`) } + } + // Every foreign consumer of any changed native target blocks the whole plan, + // including CSV references and case/zero-normalized native aliases. + for (const [path, rows] of Object.entries(c)) for (const row of rows) { + if (path === paths.policies && observedBoolean(row.dynamic) === true) continue + if (owned(path, row)) continue + for (const [field, parentPath] of Object.entries(refs)) { + if (!row[field]) continue + for (const token of tokens(c, parentPath, row[field])) if (c[parentPath]?.some(parent => matches(token, parent) && targets.has(`${parentPath}:${parent[".id"]}`))) throw fail("Откат затрагивает ссылку чужой конфигурации IPsec", "DEPENDENCY_CONFLICT") + } + } + const final = structuredClone(c) + const poolTargets = c[paths.pools].filter(row => targets.has(`${paths.pools}:${row[".id"]}`)) + const allowedPoolConsumers = [paths.pools, paths.modeConfigs].flatMap(path => c[path].filter(row => owned(path, row)).map(row => ({ path, id: row[".id"] }))) + assertPoolMutationDependencies(c, poolTargets, allowedPoolConsumers) + for (const [path, rows] of desiredRows) final[path] = [...c[path].filter(row => !owned(path, row)), ...rows] + const resolve = (path: string, value: string) => { const found = final[path]?.filter(row => matches(value, row)) ?? []; if (found.length !== 1) throw fail("Не подтверждена точная зависимость ревизии IPsec", "DEPENDENCY_CONFLICT"); return found[0] } + for (const [path, rows] of desiredRows) for (const row of rows) for (const [field, parentPath] of Object.entries(refs)) { + const value = row[field] + if (!value) continue + for (const token of tokens(final, parentPath, value)) { + const sentinels: Record = { peer: ["none"], "mode-config": ["none", "request-only"], "address-pool": ["none", "static-only"], "next-pool": ["none"], certificate: ["none"], "remote-certificate": ["none"] } + if ((sentinels[field] ?? []).includes(token) && !final[parentPath]?.some(parent => matches(token, parent))) continue + const parent = resolve(parentPath, token) + if (parentPath === "/certificate") { + if (!parent["invalid-after"]?.trim() || !parent.fingerprint?.trim()) throw fail("Сертификат ревизии не подписан или недоступен", "DEPENDENCY_CONFLICT") + if (field === "certificate" && (path === paths.peers || (path === paths.identities && row["auth-method"] === "digital-signature")) && observedBoolean(parent["private-key"]) !== true) throw fail("Приватный ключ серверного сертификата ревизии отсутствует", "DEPENDENCY_CONFLICT") + if (parent.ca) for (const issuer of tokens(final, "/certificate", parent.ca)) resolve("/certificate", issuer) + } + } + } + const early = deletes.filter(command => [paths.identities, paths.nat, paths.policies].includes(command.path)) + const late = deletes.filter(command => !early.includes(command)) + const order = [paths.pools, paths.modeConfigs, paths.peers, paths.policies, paths.identities, paths.nat] + const poolDeletes = late.filter(command => command.path === paths.pools), orderedPoolDeletes: IpsecRevisionRestoreCommand[] = [] + const poolEdges = ipsecPoolDependencies(c).filter(edge => edge.path === paths.pools) + while (poolDeletes.length) { + const index = poolDeletes.findIndex(command => !poolEdges.some(edge => idKey(edge.poolId) === idKey(command.id!) && poolDeletes.some(other => idKey(other.id!) === idKey(edge.id)))) + if (index < 0) throw fail("Не подтверждён порядок удаления цепочки пулов IPsec") + orderedPoolDeletes.push(poolDeletes.splice(index, 1)[0]) + } + commands.push(...early, ...upserts.sort((a, b) => order.indexOf(a.path) - order.indexOf(b.path)), ...late.filter(command => command.path !== paths.pools).sort((a, b) => [paths.modeConfigs, paths.peers].indexOf(a.path) - [paths.modeConfigs, paths.peers].indexOf(b.path)), ...orderedPoolDeletes) + if (commands.length > 1000) throw fail("Откат IPsec превышает полный бюджет native команд", "STATE_UNAVAILABLE") + const companions = Object.entries(c).flatMap(([path, rows]) => rows.map(row => ({ kind: kinds[path] ?? path, id: row[".id"] }))) + return { commands, companions, desired } +} + +/** Only ACK-assigned new rows may admit native defaults. Explicit PATCH clears + * may read back as an absent property; no other omission is admitted. */ +export function projectIpsecRevisionRestore(state: IpsecRevisionRestoreState, command: IpsecRevisionRestoreCommand, options: { assignedId?: string; observedRow?: Row } = {}) { + const next = structuredClone(state), rows = next.catalogs[command.path] + if (!rows) throw fail("Неизвестный каталог отката", "STATE_UNAVAILABLE") + if (command.action === "delete") { + const index = rows.findIndex(row => row[".id"] === command.id); if (index < 0) throw fail("Объект удаления отсутствует") + rows.splice(index, 1) + } else if (command.action === "update") { + const row = rows.find(item => item[".id"] === command.id) + if (!row) throw fail("Объект изменения отсутствует") + for (const [key, value] of Object.entries(command.fields)) { + if (value === "" && options.observedRow && !Object.hasOwn(options.observedRow, key)) delete row[key] + else row[key] = value + } + } else { + const row = options.observedRow, id = options.assignedId + if (!id || !nativeId(id) || !row || row[".id"] !== id || rows.some(existing => idKey(existing[".id"]) === idKey(id)) || Object.values(row).some(value => typeof value !== "string")) throw fail("Не подтверждён ACK новой строки отката", "VERIFICATION_FAILED") + protect(row) + for (const [key, value] of Object.entries(command.expectedFields ?? command.fields)) { + // Explicitly recorded emptiness may be represented as an absent optional + // native property. A nonempty provider default is never equivalent. + if (value === "" && (row[key] === undefined || row[key] === "")) continue + if (row[key] !== value && !(["disabled", "responder", "passive"].includes(key) && observedBoolean(row[key]) !== null && observedBoolean(row[key]) === observedBoolean(value))) throw fail("Записанные поля нового объекта отката не подтверждены", "VERIFICATION_FAILED") + } + rows.push(structuredClone(row)) + } + return next +} diff --git a/backend/src/services/ipsec-revision-restore.test.ts b/backend/src/services/ipsec-revision-restore.test.ts new file mode 100644 index 00000000..edebeb01 --- /dev/null +++ b/backend/src/services/ipsec-revision-restore.test.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import { canonicalIpsecSnapshot } from "./entity-snapshots.js" +import { fingerprintPayload } from "./config-revisions.js" +import { readIpsecRevisionRestoreRevision } from "./ipsec-revision-restore.js" +const revisionId = randomUUID(), payload = canonicalIpsecSnapshot({}) +const original = pool.query, connect = pool.connect +let row: Record, sqlCalls = 0, checks = 0 +const valid = () => ({ id: revisionId, server_id: 1, section: "ipsec", source: "copy", fingerprint: fingerprintPayload(payload), payload, created_at: new Date("2026-10-24T00:00:00Z"), note: null, revision: "123" }) +pool.query = (async (sql: string, values: unknown[]) => { assert(sql.startsWith("SELECT id,server_id,section,source,fingerprint,created_at,note,xmin::text AS revision,CASE WHEN octet_length(payload::text)<=1048576")); assert.deepEqual(values, [revisionId, 1]); sqlCalls++; return { rows: [row] } }) as never +pool.connect = (() => { throw Error("Real SQL forbidden") }) as never +try { + for (const invalid of ["bad", "-".repeat(36), "1".repeat(36)]) { const before = sqlCalls; await assert.rejects(readIpsecRevisionRestoreRevision(1, invalid)); assert.equal(sqlCalls, before) } + for (const change of [{ server_id: 2 }, { section: "wireguard" }, { fingerprint: "0".repeat(64) }, { fingerprint: null }, { payload: {} }, { payload: null }, { payload: { ...payload, unknown: true } }, { source: "unknown" }, { revision: "unknown" }, { created_at: "invalid" }, { note: "x".repeat(16385) }]) { + row = { ...valid(), ...change }; checks = 0 + await assert.rejects(readIpsecRevisionRestoreRevision(1, revisionId, async () => { checks++ })) + assert.equal(checks, 2) + } + row = valid(); const before = sqlCalls + await assert.rejects(readIpsecRevisionRestoreRevision(1, revisionId, async () => { throw Error("access revoked before SQL") })); assert.equal(sqlCalls, before) + checks = 0; await assert.rejects(readIpsecRevisionRestoreRevision(1, revisionId, async () => { if (++checks === 2) throw Error("access revoked after SQL") })); assert.equal(checks, 2) + console.log("IPsec revision row preflight: exact UUID/server/section, bounded schema and metadata, original fingerprint integrity, live authorization before/after SQL; zero real SQL or native access") +} finally { pool.query = original; pool.connect = connect } diff --git a/backend/src/services/ipsec-revision-restore.ts b/backend/src/services/ipsec-revision-restore.ts new file mode 100644 index 00000000..d5f86f9f --- /dev/null +++ b/backend/src/services/ipsec-revision-restore.ts @@ -0,0 +1,84 @@ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { pool } from "../db/index.js" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError, fingerprint, privateFingerprinter } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { ipsecPeerDeleteRows } from "./ipsec-peer-delete.js" +import { readIpsecServerInitState, ipsecServerInitValue } from "./ipsec-server-init.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { parseIpsecSnapshot } from "./entity-snapshots.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision, fingerprintPayload } from "./config-revisions.js" +import { captureIpsecSnapshot } from "./ipsec-live.js" +import { planIpsecRevisionRestore, projectIpsecRevisionRestore, type IpsecRevisionRestoreState } from "./ipsec-revision-restore-model.js" + +export async function readIpsecRevisionRestoreRevision(serverId: number, revisionId: string, authorize: () => Promise = async () => {}) { + if (!Number.isSafeInteger(serverId) || serverId <= 0 || !z.string().uuid().safeParse(revisionId).success) throw new McpError("INVALID_INPUT", "Некорректная ссылка ревизии IPsec", 400) + await authorize() + const rows = (await pool.query("SELECT id,server_id,section,source,fingerprint,created_at,note,xmin::text AS revision,CASE WHEN octet_length(payload::text)<=1048576 THEN payload ELSE NULL END AS payload FROM config_revisions WHERE id=$1 AND server_id=$2 AND section='ipsec' LIMIT 2", [revisionId, serverId])).rows + await authorize() + if (rows.length !== 1 || !rows[0].payload) throw new McpError("STATE_CONFLICT", "Ревизия IPsec отсутствует, повреждена или принадлежит другому серверу", 409) + const row = rows[0] + if (row.id !== revisionId || row.server_id !== serverId || row.section !== "ipsec" || !["apply", "rollback", "observed", "copy"].includes(row.source) || typeof row.revision !== "string" || !/^\d{1,20}$/.test(row.revision) || (row.note !== null && (typeof row.note !== "string" || Buffer.byteLength(row.note) > 16384)) || !Number.isFinite(new Date(row.created_at).getTime()) || typeof row.fingerprint !== "string" || !/^[a-f0-9]{64}$/.test(row.fingerprint) || Buffer.byteLength(JSON.stringify(row.payload)) > 1048576 || row.fingerprint !== fingerprintPayload(row.payload)) throw new McpError("STATE_CONFLICT", "Fingerprint, метаданные или принадлежность ревизии IPsec не подтверждены", 409) + const payload = parseIpsecSnapshot(row.payload) + return { payload, proof: (await privateFingerprinter("ipsec-revision-row-v1"))(fingerprint(row)) } +} +export async function ipsecRevisionRestoreProof(state: IpsecRevisionRestoreState) { return (await privateFingerprinter("ipsec-revision-restore-v1"))(fingerprint(ipsecServerInitValue(state))) } +export async function readIpsecRevisionRestoreState(server: ServerRow, authorize: () => Promise = async () => {}) { + const source = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new McpError("STATE_CONFLICT", "Источник отката IPsec изменился", 409) } + const state = await readIpsecServerInitState(server, source) + for (const path of ["/interface/list", "/interface/list/member"]) { + await source(); state.catalogs[path] = ipsecPeerDeleteRows(await MikrotikClient.fromServer(server).get(path, 10000, undefined, 1048576)); await source() + if (Buffer.byteLength(JSON.stringify(state)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Полный каталог отката IPsec превышает 1 МиБ", 502) + } + await source(); return { ...state, proof: await ipsecRevisionRestoreProof(state) } +} +export class IpsecRevisionRestoreExecutionError extends McpError { + constructor(code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string, readonly confirmedWrites: number, readonly completedCommands: number, readonly commandCount: number) { super(code, message, 502) } +} +export async function restoreIpsecRevisionGuarded(server: ServerRow, revisionId: string, expectedProof?: string, authorize: () => Promise = async () => {}, options: { expectedRevisionProof?: string; recordRevision?: typeof captureAndAppendRevision; receiptId?: string; checkpoint?: (value: { receiptId: string; commandCount: number; completedCommands: number; afterProof: string }) => Promise } = {}) { + const receiptId = options.receiptId ?? randomUUID() + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "ipsec_revision", id: revisionId }, [server.id], async () => { + await authorize() + const revision = await readIpsecRevisionRestoreRevision(server.id, revisionId, authorize) + if (options.expectedRevisionProof !== undefined && revision.proof !== options.expectedRevisionProof) throw new McpError("STATE_CONFLICT", "Ревизия IPsec изменилась после подготовки", 409) + const source = async () => { + await authorize(); const fresh = await getServerRowById(server.id) + if (!fresh || !sameWireguardEndpoint(fresh, server) || (await readIpsecRevisionRestoreRevision(server.id, revisionId, authorize)).proof !== revision.proof) throw new McpError("STATE_CONFLICT", "Источник или ревизия IPsec изменились", 409) + } + let state = await readIpsecRevisionRestoreState(server, source) + if (expectedProof !== undefined && state.proof !== expectedProof) throw new McpError("STATE_CONFLICT", "IPsec или зависимости отката изменились после подготовки", 409) + const plan = planIpsecRevisionRestore(state, revision.payload), client = MikrotikClient.fromServer(server) + let completedCommands = 0, confirmedWrites = 0 + const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string) => new IpsecRevisionRestoreExecutionError(code, message, confirmedWrites, completedCommands, plan.commands.length) + for (const command of plan.commands) { + let dispatched = false, response: unknown, assignedId: string | undefined + const fence = async () => { const fresh = await readIpsecRevisionRestoreState(server, source); if (fresh.proof !== state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился после durable intent; команда отката не отправлена", 409); await source(); dispatched = true } + try { + if (command.action === "create") response = await client.put(command.path, command.fields, 30000, fence) + else if (command.action === "update") response = await client.patch(`${command.path}/${encodeURIComponent(command.id!)}`, command.fields, 15000, fence) + else response = await client.delete(`${command.path}/${encodeURIComponent(command.id!)}`, 15000, fence) + confirmedWrites++ + } catch (error) { + if (!dispatched && !completedCommands && error instanceof McpError) throw error + throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Откат IPsec остановлен; повтор native-команды и автоматический откат запрещены, требуется сверка") + } + let after: IpsecRevisionRestoreState + try { after = await readIpsecRevisionRestoreState(server, source) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда отката отправлена, но read-back недоступен; повтор запрещён") } + try { + if (command.action === "create") { const ack = response as Record | null; if (typeof ack?.[".id"] !== "string") throw Error("missing create ACK"); assignedId = ack[".id"] } + const observedRow = after.catalogs[command.path].find(row => row[".id"] === (command.id ?? assignedId)) + const projected = projectIpsecRevisionRestore(state, command, { assignedId, observedRow }) + if (await ipsecRevisionRestoreProof(projected) !== after.proof) throw Error("configuration mismatch") + } catch { throw stopped("VERIFICATION_FAILED", "Не подтверждены точные поля отката и сохранность полного native состояния; повтор запрещён") } + state = after; completedCommands++ + try { await source(); await options.checkpoint?.({ receiptId, commandCount: plan.commands.length, completedCommands, afterProof: state.proof }) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Откат команды проверен, но checkpoint не сохранён; повтор запрещён") } + } + 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 + try { await source(); historyRecorded = (await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "rollback", capture: () => captureIpsecSnapshot(server, reader) })) !== null } catch { /* Never replay native writes for history. */ } + return { ok: true, verified: historyRecorded, receiptId, serverId: server.id, revisionId, commandCount: plan.commands.length, completedCommands, confirmedWrites, afterProof: state.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", automaticReplay: false, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/ipsec-revision-secret.test.ts b/backend/src/services/ipsec-revision-secret.test.ts new file mode 100644 index 00000000..5cf33a1a --- /dev/null +++ b/backend/src/services/ipsec-revision-secret.test.ts @@ -0,0 +1,20 @@ +import assert from "node:assert/strict" +import { canonicalIpsecSnapshot, parseIpsecSnapshot, planIpsecRestore } from "./entity-snapshots.js" +import { captureIpsecSnapshot } from "./ipsec-live.js" +import type { MikrotikClient } from "./mikrotik.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" + +const secret = " test\tshared secret ", comment = "MikrotikManager:ipsec user=exact" +const snapshot = canonicalIpsecSnapshot({ identities: [{ peerName: "peer", authMethod: "pre-shared-key", secret, comment }] }) +assert.equal(snapshot.identities[0].secret, secret) +assert.equal(parseIpsecSnapshot(structuredClone(snapshot)).identities[0].secret, secret) +const commands = planIpsecRestore(snapshot, { peers: [], identities: [], modeConfigs: [], pools: [], nat: [] }) +const identity = commands.find(command => command.op === "put" && command.path === "/ip/ipsec/identity") +assert.ok(identity && "body" in identity) +assert.equal(identity.body.secret, secret) +let reads = 0 +const reader = { get: async (path: string) => { reads++; return path === "/ip/ipsec/identity" ? [{ ".id": "*2", peer: "peer", "auth-method": "pre-shared-key", secret, comment, disabled: "false" }] : [] } } as unknown as MikrotikClient +const captured = await captureIpsecSnapshot({ id: 1, enabled: true } as ServerRow, reader) +assert.equal(captured.identities[0].secret, secret) +assert.ok(reads > 0) +console.log("IPsec revisions: capture, canonical payload, strict parse and restore preserve exact PSK whitespace; injected reader only") diff --git a/backend/src/services/ipsec-server-delete-model.test.ts b/backend/src/services/ipsec-server-delete-model.test.ts new file mode 100644 index 00000000..175ae9f2 --- /dev/null +++ b/backend/src/services/ipsec-server-delete-model.test.ts @@ -0,0 +1,136 @@ +import { ipsecExternalPoolDependencyPaths } from "./ipsec-pool-dependencies.js" +import assert from "node:assert/strict" +import { planIpsecServerDelete as plan, projectIpsecServerDelete as project } from "./ipsec-server-delete-model.js" +import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +const marker = "MikrotikManager:ipsec" +const paths = [...ipsecExternalPoolDependencyPaths,"/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] +function base(): IpsecPeerDeleteState { + return { source: "source", proof: "proof", protections: [{ object_ref: { kind: "server", id: "1" } }], catalogs: { ...Object.fromEntries(paths.map(path => [path, []])), + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "mode-config": "ipsec-vpn", certificate: "ipsec-server", "remote-certificate": "ipsec-user-a", "policy-template-group": "mmapp-ipsec-vpn", comment: marker, opaque: "identity" }], + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", certificate: "ipsec-server", profile: "profile", comment: marker }], + "/ip/ipsec/policy": [{ ".id": "*y", peer: "ike", group: "mmapp-ipsec-vpn", proposal: "proposal", comment: marker }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "ipsec-vpn", "address-pool": "pool" }], + "/ip/pool": [{ ".id": "*l", name: "pool", comment: marker }], + "/ip/firewall/nat": [{ ".id": "*n", comment: marker, opaque: "nat" }], + "/ip/ipsec/policy/group": [{ ".id": "*g", name: "mmapp-ipsec-vpn" }, { ".id": "*d", name: "default", default: "true" }], + "/ip/ipsec/profile": [{ ".id": "*f", name: "profile", comment: marker, opaque: "keep-profile" }], + "/ip/ipsec/proposal": [{ ".id": "*o", name: "proposal", comment: marker, opaque: "keep-proposal" }], + "/certificate": [{ ".id": "*a", name: "ipsec-ca", authority: "true", crl: "false" }, { ".id": "*s", name: "ipsec-server", ca: "ipsec-ca" }, { ".id": "*c", name: "ipsec-user-a", ca: "ipsec-ca" }], + } } +} +const s = base(), before = structuredClone(s), full = plan(s, true) +assert.deepEqual(full.commands.map(c => [c.path, c.id]), [["/ip/ipsec/identity", "*i"], ["/ip/ipsec/policy", "*y"], ["/ip/ipsec/peer", "*p"], ["/ip/ipsec/mode-config", "*m"], ["/ip/pool", "*l"], ["/ip/firewall/nat", "*n"], ["/ip/ipsec/policy/group", "*g"], ["/certificate", "*s"], ["/certificate", "*c"], ["/certificate", "*a"]]) +assert.equal(full.objects.length, 10) +assert.deepEqual(full.companions, full.objects) +let next = s +for (const command of full.commands) next = project(next, command) +assert.deepEqual(s, before) +assert.deepEqual(next.protections, s.protections) +for (const path of ["/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/active-peers"]) assert.deepEqual(next.catalogs[path], s.catalogs[path]) +assert.deepEqual(next.catalogs["/ip/ipsec/policy/group"], [s.catalogs["/ip/ipsec/policy/group"][1]]) +const keepCerts = plan(s, false) +assert.equal(keepCerts.commands.length, 7) +assert(!keepCerts.objects.some(o => o.kind === "certificate")) +assert.deepEqual(keepCerts.commands.reduce(project, s).catalogs["/certificate"], s.catalogs["/certificate"]) + +const mixed = base() +mixed.catalogs["/ip/ipsec/peer"].push({ ".id": "*foreign", name: "foreign", profile: "profile", opaque: "unknown vendor value" }) +mixed.catalogs["/ip/ipsec/identity"].push({ ".id": "*fi", peer: "foreign", opaque: "foreign secret" }) +mixed.catalogs["/ip/firewall/nat"].push({ ".id": "*fn", comment: "foreign", vendor: "retained" }) +const mixedAfter = plan(mixed, true).commands.reduce(project, mixed) +assert.deepEqual(mixedAfter.catalogs["/ip/ipsec/peer"], [mixed.catalogs["/ip/ipsec/peer"][1]]) +assert.deepEqual(mixedAfter.catalogs["/ip/ipsec/identity"], [mixed.catalogs["/ip/ipsec/identity"][1]]) +assert.deepEqual(mixedAfter.catalogs["/ip/firewall/nat"], [mixed.catalogs["/ip/firewall/nat"][1]]) + +for (const [path, row] of [ + ["/ip/ipsec/mode-config", { ".id": "*foreign", name: "foreign", "address-pool": "*l" }], + ["/ip/ipsec/peer", { ".id": "*foreign", name: "foreign", certificate: "*s" }], + ["/ip/ipsec/identity", { ".id": "*foreign", peer: "*p" }], + ["/ip/ipsec/policy", { ".id": "*foreign", peer: " *p " }], + ["/certificate", { ".id": "*foreign", name: "foreign", ca: "*a" }], + ["/ip/pool", { ".id": "*foreign", name: "foreign", "next-pool": "*l" }], +] as [string, Record][]) { + const conflict = base(); conflict.catalogs[path].push(row) + assert.throws(() => plan(conflict, true), /чужой/) +} +const aliases = base() +aliases.catalogs["/ip/ipsec/identity"][0].peer = "*P" +aliases.catalogs["/ip/ipsec/policy"][0].group = "*g" +aliases.catalogs["/ip/ipsec/mode-config"][0]["address-pool"] = "*L" +assert.equal(plan(aliases, true).objects.length, 10) +const runtime = base() +runtime.catalogs["/ip/ipsec/policy"].push({ ".id": "*runtime", peer: "ike", group: "mmapp-ipsec-vpn", dynamic: "yes", comment: marker, opaque: "runtime" }) +assert(!plan(runtime, true).commands.some(c => c.id === "*runtime")) +assert.deepEqual(plan(runtime, true).commands.reduce(project, runtime).catalogs["/ip/ipsec/policy"], [runtime.catalogs["/ip/ipsec/policy"][1]]) +for (const mutation of [ + (state: IpsecPeerDeleteState) => { state.catalogs["/ip/ipsec/peer"].push({ ".id": "*x", name: " ike " }) }, + (state: IpsecPeerDeleteState) => { state.catalogs["/ip/ipsec/peer"].push({ ".id": "*P", name: "different" }) }, + (state: IpsecPeerDeleteState) => { state.catalogs["/ip/ipsec/peer"].push({ ".id": "*x", name: "*p" }) }, + (state: IpsecPeerDeleteState) => { delete state.catalogs["/ip/pool"] }, + (state: IpsecPeerDeleteState) => { state.catalogs["/certificate"][1].ca = "missing-ca" }, + (state: IpsecPeerDeleteState) => { state.catalogs["/certificate"][0].ca = "ipsec-server" }, + (state: IpsecPeerDeleteState) => { state.catalogs["/ip/ipsec/identity"][0].certificate = "ipsec-server,,ipsec-ca" }, +]) { const invalid = base(); mutation(invalid); assert.throws(() => plan(invalid, true)) } +for (const flag of ["dynamic", "builtin", "default"]) for (const value of ["yes", "unknown", ""]) { + const protectedState = base(); protectedState.catalogs["/ip/firewall/nat"][0][flag] = value + assert.throws(() => plan(protectedState, true), /Системный/) +} + +const crl = base(); crl.catalogs["/certificate"][0].crl = "true" +const crlPlan = plan(crl, true), cascade = crlPlan.commands.find(c => c.path === "/certificate")! +assert.deepEqual(cascade, { path: "/certificate", id: "*a", cascadeIds: ["*s", "*c"] }) +assert.equal(crlPlan.commands.filter(c => c.path === "/certificate").length, 1) +assert.equal(crlPlan.objects.filter(o => o.kind === "certificate").length, 3) +assert.deepEqual(crlPlan.commands.reduce(project, crl).catalogs["/certificate"], []) +assert.equal(plan(crl, false).commands.length, 7) +const nested = base() +nested.catalogs["/certificate"].push({ ".id": "*inter", name: "ipsec-user-intermediate", ca: "ipsec-ca", authority: "true", crl: "true" }) +nested.catalogs["/certificate"][2].ca = "*inter" +const nestedCascade = plan(nested, true).commands.find(c => c.cascadeIds)! +assert.deepEqual(nestedCascade.cascadeIds, ["*s", "*c", "*inter"]) +const foreignCrl = base() +foreignCrl.catalogs["/certificate"][0].name = "foreign-ca" +foreignCrl.catalogs["/certificate"][0].crl = "true" +for (const row of foreignCrl.catalogs["/certificate"].slice(1)) row.ca = "foreign-ca" +assert.throws(() => plan(foreignCrl, true), /чужим CA с CRL/) +const unknownCrl = base(); unknownCrl.catalogs["/certificate"][0].crl = "perhaps" +assert.throws(() => plan(unknownCrl, true), /Неоднозначный CRL/) +const foreignNested = structuredClone(nested) +foreignNested.catalogs["/certificate"].push({ ".id": "*foreign", name: "foreign", ca: "*inter" }) +assert.throws(() => plan(foreignNested, true), /чужой/) + +const empty = { source: "source", proof: "proof", protections: [], catalogs: Object.fromEntries(paths.map(path => [path, []])) } +assert.deepEqual(plan(empty, true).commands, []) +assert.deepEqual(plan(empty, false).objects, []) +assert.throws(() => project(s, { path: "/certificate", id: "*missing" }), /не подтверждены/) +assert.throws(() => project(s, { path: "/certificate", id: "*a", cascadeIds: ["*a"] }), /не подтверждены/) +assert.throws(() => project(crl, { path: "/certificate", id: "*a", cascadeIds: ["*s"] }), /манифестом/) +assert.throws(() => project(crl, { path: "/certificate", id: "*a" }), /манифестом/) +const foreignExpanded = base() +foreignExpanded.catalogs["/certificate"].push({ ".id": "*foreign", name: "foreign" }) +assert.throws(() => project(foreignExpanded, { path: "/certificate", id: "*a", cascadeIds: ["*s", "*c", "*foreign"] }), /манифестом/) +const commaName = base() +commaName.catalogs["/certificate"][0].name = "ipsec-user-ca,with-comma" +for (const row of commaName.catalogs["/certificate"].slice(1)) row.ca = "ipsec-user-ca,with-comma" +assert.equal(plan(commaName, true).objects.length, 10) +const notCa = base(); notCa.catalogs["/certificate"][0].authority = "false" +assert.throws(() => plan(notCa, true), /CA-издатель/) +const poolChain = base() +poolChain.catalogs["/ip/pool"].push({ ".id": "*backup", name: "backup", comment: marker }) +poolChain.catalogs["/ip/pool"][0]["next-pool"] = "*backup" +assert.deepEqual(plan(poolChain, false).commands.filter(c => c.path === "/ip/pool").map(c => c.id), ["*l", "*backup"]) +assert.deepEqual(project(crl, cascade).catalogs["/ip/ipsec/identity"], crl.catalogs["/ip/ipsec/identity"]) +console.log("ipsec server delete model tests passed") + +const runtimeGhost = base() +runtimeGhost.catalogs["/ip/ipsec/policy"].push({".id":"*ghost",dynamic:"true",peer:"vanished-runtime-peer",group:"vanished-runtime-group",comment:marker}) +assert.deepEqual(plan(runtimeGhost,true).commands,full.commands) + +// v2 never treats an absent/unsupported external pool catalog as empty. +for(const path of ipsecExternalPoolDependencyPaths){const missing=base();delete missing.catalogs[path];assert.throws(()=>plan(missing,false))} +for(const path of ipsecExternalPoolDependencyPaths)for(const alias of ["pool","*L","*000l"]){ + const foreign=base();foreign.catalogs[path].push({".id":"*consumer",name:"consumer",disabled:"true",...(path.startsWith("/ppp/")?{"remote-address":alias}:{"address-pool":alias})});assert.throws(()=>plan(foreign,false),/пула|pool/) +} +const unknownExternal=base();unknownExternal.catalogs["/ppp/profile"].push({".id":"*consumer","remote-address":"192.0.2.1",opaque:"NEVER_EXTERNAL_OPAQUE"}); +assert.ok(plan(unknownExternal,false).companions.some(ref=>ref.kind==="ppp_profile"&&ref.id==="*consumer")) +const indirectExternal=base();indirectExternal.catalogs["/ip/pool"].push({".id":"*fallback",name:"fallback",comment:marker});indirectExternal.catalogs["/ip/pool"][0]["next-pool"]="fallback";indirectExternal.catalogs["/ppp/secret"].push({".id":"*consumer","remote-address":"pool"});assert.throws(()=>plan(indirectExternal,false),/пула|pool/) diff --git a/backend/src/services/ipsec-server-delete-model.ts b/backend/src/services/ipsec-server-delete-model.ts new file mode 100644 index 00000000..17f3cdf8 --- /dev/null +++ b/backend/src/services/ipsec-server-delete-model.ts @@ -0,0 +1,185 @@ +import { assertPoolMutationDependencies,ipsecExternalPoolDependencyPaths } from "./ipsec-pool-dependencies.js" +import { McpError } from "../modules/mcp/security.js" +import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { IPSEC_CA_CERT, IPSEC_SERVER_CERT, IPSEC_POLICY_GROUP, isIpsecManagedComment, isManagedIpsecModeConfig } from "./ipsec-config.js" +import { observedBoolean } from "./ospf-observed-values.js" + +type Row = Record +const paths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] as const +export type IpsecServerDeletePath = "/ip/ipsec/identity" | "/ip/ipsec/peer" | "/ip/ipsec/policy" | "/ip/ipsec/mode-config" | "/ip/pool" | "/ip/firewall/nat" | "/ip/ipsec/policy/group" | "/certificate" +export interface IpsecServerDeleteCommand { path: IpsecServerDeletePath; id: string; cascadeIds?: string[] } +export interface IpsecServerDeleteObject { kind: string; id: string } +export interface IpsecServerDeletePlan { commands: IpsecServerDeleteCommand[]; objects: IpsecServerDeleteObject[]; companions: IpsecServerDeleteObject[]; removeCertificates: boolean } +const kinds: Record = { "/ip/ipsec/identity": "ipsec_identity", "/ip/ipsec/peer": "ipsec_peer", "/ip/ipsec/policy": "ipsec_policy", "/ip/ipsec/mode-config": "ipsec_mode_config", "/ip/pool": "ip_pool", "/ip/firewall/nat": "ip_nat", "/ip/ipsec/policy/group": "ipsec_policy_group", "/certificate": "certificate" } +const fail = (code: string, message: string): never => { throw new McpError(code, message, code === "STATE_UNAVAILABLE" ? 502 : 409) } +const exactId = (value: unknown): value is string => typeof value === "string" && /^\*[a-z0-9]+$/i.test(value) && value.length <= 100 +const idKey=(value:string)=>value.toLowerCase().replace(/^\*0+(?=[a-z0-9])/,"*") +function validate(state: IpsecPeerDeleteState) { + if (!state?.source || !Array.isArray(state.protections) || !state.catalogs) fail("STATE_UNAVAILABLE", "Неполное состояние IPsec") + const indexes = new Map>() + for (const path of paths) { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000) fail("STATE_UNAVAILABLE", "Неполный native-каталог IPsec") + const aliases = new Map(), ids = new Set() + for (const row of rows) { + if (!row || typeof row !== "object" || Array.isArray(row) || !exactId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) fail("STATE_UNAVAILABLE", "Некорректная строка native-каталога IPsec") + if (ids.has(idKey(row[".id"]))) fail("STATE_CONFLICT", "Повторяющийся native ID IPsec") + ids.add(idKey(row[".id"])) + for (const alias of [row[".id"], row.name?.trim()].filter((value): value is string => !!value)) { + const key = exactId(alias) ? idKey(alias) : alias + if (aliases.has(key) && aliases.get(key) !== row) fail("STATE_CONFLICT", "Неоднозначное имя или native ID IPsec") + aliases.set(key, row) + } + } + indexes.set(path, aliases) + } + return indexes +} +function protect(row: Row) { + for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) fail("PROTECTED_OBJECT", "Системный или неподтверждённый объект IPsec нельзя удалить") +} +/** Full server removal, with the same managed markers and reserved names as REST cleanup. */ +export function planIpsecServerDelete(state: IpsecPeerDeleteState, removeCertificates = false): IpsecServerDeletePlan { + if (typeof removeCertificates !== "boolean") fail("STATE_UNAVAILABLE", "Некорректный режим удаления сертификатов") + const indexes = validate(state), catalogs = state.catalogs + const resolve = (path: string, raw: string): Row => { + const key = exactId(raw) ? idKey(raw) : raw + return indexes.get(path)?.get(key) ?? fail("STATE_UNAVAILABLE", "Не найден родитель зависимости IPsec") + } + const owned = new Map>() + for (const path of paths) owned.set(path, new Set()) + for (const path of ["/ip/ipsec/identity", "/ip/ipsec/peer", "/ip/ipsec/policy", "/ip/ipsec/mode-config", "/ip/pool", "/ip/firewall/nat"]) { + for (const row of catalogs[path]) { + // Generated policies are runtime observations, excluded by the common state proof. + if (path === "/ip/ipsec/policy" && observedBoolean(row.dynamic) === true) continue + if (isIpsecManagedComment(row.comment) || (path === "/ip/ipsec/mode-config" && isManagedIpsecModeConfig(row))) owned.get(path)!.add(row) + } + } + // A reserved policy group is owned only when a managed policy actually links it. + for (const policy of owned.get("/ip/ipsec/policy")!) if (policy.group?.trim()) { + const group = resolve("/ip/ipsec/policy/group", policy.group.trim()) + if (group.name === IPSEC_POLICY_GROUP) owned.get("/ip/ipsec/policy/group")!.add(group) + } + if (removeCertificates) for (const cert of catalogs["/certificate"]) if (isIpsecManagedComment(cert.comment) || cert.name === IPSEC_CA_CERT || cert.name === IPSEC_SERVER_CERT || cert.name?.startsWith("ipsec-user-")) owned.get("/certificate")!.add(cert) + for (const set of owned.values()) for (const row of set) protect(row) + // Resolve exact scalar names first; only policy.peer has CSV reference semantics. + const edges: { path: string; row: Row; parentPath: string; parent: Row }[] = [] + const refs = (path: string, row: Row, field: string, parentPath: string, sentinels: string[] = ["none"]) => { + const raw = row[field]?.trim() + if (!raw) return + const exactAlias = indexes.get(parentPath)!.has(exactId(raw) ? idKey(raw) : raw) + const parts = path === "/ip/ipsec/policy" && field === "peer" && !exactAlias ? raw.split(",").map(value => value.trim()) : [raw] + if (parts.some(value => !value)) fail("STATE_UNAVAILABLE", "Некорректная зависимость IPsec") + const resolved = new Set() + for (const value of parts) { + if (sentinels.includes(value) && !indexes.get(parentPath)!.has(value)) continue + const parent = resolve(parentPath, value) + if (path === "/certificate" && field === "ca" && parent.authority !== undefined && observedBoolean(parent.authority) !== true) fail("STATE_UNAVAILABLE", "Не подтверждён CA-издатель сертификата") + if (resolved.has(parent)) fail("STATE_CONFLICT", "Повторяющаяся или неоднозначная зависимость IPsec") + resolved.add(parent) + edges.push({ path, row, parentPath, parent }) + if (!owned.get(path)!.has(row) && owned.get(parentPath)!.has(parent) && !(path === "/ip/ipsec/policy" && observedBoolean(row.dynamic) === true)) fail("DEPENDENCY_CONFLICT", "Управляемый объект IPsec используется чужой конфигурацией") + } + } + for (const row of catalogs["/ip/ipsec/identity"]) { + refs("/ip/ipsec/identity", row, "peer", "/ip/ipsec/peer") + refs("/ip/ipsec/identity", row, "mode-config", "/ip/ipsec/mode-config", ["none", "request-only"]) + refs("/ip/ipsec/identity", row, "policy-template-group", "/ip/ipsec/policy/group", ["default"]) + for (const field of ["certificate", "remote-certificate"]) refs("/ip/ipsec/identity", row, field, "/certificate") + } + for (const row of catalogs["/ip/ipsec/peer"]) { + refs("/ip/ipsec/peer", row, "certificate", "/certificate") + refs("/ip/ipsec/peer", row, "profile", "/ip/ipsec/profile", ["default"]) + } + for (const row of catalogs["/ip/ipsec/policy"]) { + if (observedBoolean(row.dynamic) === true) continue // Generated policies are runtime, not independently managed dependencies. + refs("/ip/ipsec/policy", row, "peer", "/ip/ipsec/peer") + refs("/ip/ipsec/policy", row, "group", "/ip/ipsec/policy/group", ["default"]) + refs("/ip/ipsec/policy", row, "proposal", "/ip/ipsec/proposal", ["default"]) + } + for (const row of catalogs["/ip/ipsec/mode-config"]) refs("/ip/ipsec/mode-config", row, "address-pool", "/ip/pool", ["none", "static-only"]) + for (const row of catalogs["/ip/pool"]) refs("/ip/pool", row, "next-pool", "/ip/pool") + for (const row of catalogs["/certificate"]) refs("/certificate", row, "ca", "/certificate", []) + const descendantsByPath = new Map>() + // Iterative graph walk bounds stack use for the maximum 2,000-row catalog. + for (const path of ["/certificate", "/ip/pool"]) { + const children = new Map(), done = new Map(), active = new Set() + for (const row of catalogs[path]) children.set(row, []) + for (const edge of edges) if (edge.path === path && edge.parentPath === path) children.get(edge.parent)!.push(edge.row) + for (const root of catalogs[path]) { + if (done.has(root)) continue + const stack: { row: Row; exit: boolean }[] = [{ row: root, exit: false }] + while (stack.length) { + const { row, exit } = stack.pop()! + if (done.has(row)) continue + if (exit) { + done.set(row, children.get(row)!.flatMap(child => [...done.get(child)!, child])) + active.delete(row) + } else { + if (active.has(row)) fail("STATE_CONFLICT", "Циклическая зависимость IPsec") + active.add(row); stack.push({ row, exit: true }) + for (const child of children.get(row)!) stack.push({ row: child, exit: false }) + } + } + } + descendantsByPath.set(path, done) + } + const descendants = (path: string, row: Row): Row[] => descendantsByPath.get(path)!.get(row)! + const commands: IpsecServerDeleteCommand[] = [], objects: IpsecServerDeleteObject[] = [] + for (const path of ["/ip/ipsec/identity", "/ip/ipsec/policy", "/ip/ipsec/peer", "/ip/ipsec/mode-config", "/ip/pool", "/ip/firewall/nat", "/ip/ipsec/policy/group"] as IpsecServerDeletePath[]) { + const rows = [...owned.get(path)!] + if (path === "/ip/pool") rows.sort((a, b) => descendants(path, a).length - descendants(path, b).length) + for (const row of rows) { commands.push({ path, id: row[".id"] }); objects.push({ kind: kinds[path], id: row[".id"] }) } + } + const certs = owned.get("/certificate")!, covered = new Set() + const crlBlocks = (row: Row) => { + const issuer = edges.find(edge => edge.path === "/certificate" && edge.row === row)?.parent + if (!issuer) return false + if (issuer.crl !== undefined && observedBoolean(issuer.crl) === null) fail("STATE_UNAVAILABLE", "Неоднозначный CRL CA") + return observedBoolean(issuer.crl) === true && (observedBoolean(row.issued) !== false || (row.revoked !== undefined && observedBoolean(row.revoked) !== false)) + } + // RouterOS CA removal cascades the entire issued chain. Only a closed, fully owned + // subtree may use that operation, and every indirect removal is in the manifest. + const roots = [...certs].filter(row => !edges.some(edge => edge.path === "/certificate" && edge.row === row && certs.has(edge.parent))) + for (const root of roots) { + const children = descendants("/certificate", root) + if (children.some(row => !certs.has(row))) fail("DEPENDENCY_CONFLICT", "Управляемый CA подписал чужой сертификат") + if (crlBlocks(root)) fail("DEPENDENCY_CONFLICT", "Сертификат выдан чужим CA с CRL; удаление CA выходит за границы операции") + if (children.some(crlBlocks)) { + commands.push({ path: "/certificate", id: root[".id"], cascadeIds: children.map(row => row[".id"]) }) + for (const row of [...children, root]) covered.add(row) + } + } + for (const row of [...certs].sort((a, b) => descendants("/certificate", a).length - descendants("/certificate", b).length)) { + if (!covered.has(row)) commands.push({ path: "/certificate", id: row[".id"] }) + objects.push({ kind: "certificate", id: row[".id"] }) + } + // A marker on an external allocator never authorizes its indirect disruption. + assertPoolMutationDependencies(catalogs,[...owned.get("/ip/pool")!],commands.filter(cmd=>["/ip/ipsec/mode-config","/ip/pool"].includes(cmd.path)).map(cmd=>({path:cmd.path,id:cmd.id}))) + const externalKinds:Record={"/ip/dhcp-server":"dhcp_server","/ppp/profile":"ppp_profile","/ppp/secret":"ppp_secret","/ip/hotspot":"hotspot_server","/ip/hotspot/user/profile":"hotspot_user_profile"} + return { commands, objects, companions: [...objects.map(object => ({ ...object })),...ipsecExternalPoolDependencyPaths.flatMap(path=>catalogs[path].map(row=>({kind:externalKinds[path],id:row[".id"]})))], removeCertificates } +} + +/** Exact declared removal only; opaque fields, foreign rows and order survive unchanged. */ +export function projectIpsecServerDelete(state: IpsecPeerDeleteState, command: IpsecServerDeleteCommand): IpsecPeerDeleteState { + if (!Object.hasOwn(kinds, command.path) || !exactId(command.id) || (command.cascadeIds && (command.path !== "/certificate" || command.cascadeIds.some(id => !exactId(id))))) fail("STATE_CONFLICT", "Некорректная команда удаления IPsec") + const ids = [command.id, ...(command.cascadeIds ?? [])], rows = state.catalogs[command.path] + if (!Array.isArray(rows) || new Set(ids).size !== ids.length || ids.some(id => rows.filter(row => row[".id"] === id).length !== 1)) fail("STATE_CONFLICT", "Точные объекты удаления IPsec не подтверждены") + for (const row of rows.filter(row => ids.includes(row[".id"]))) protect(row) + if (command.path === "/certificate") { + const aliases = validate(state).get("/certificate")!, closure = new Set([command.id]) + let changed = true + while (changed) { + changed = false + for (const row of rows) { + const raw = row.ca?.trim(), parent = raw ? aliases.get(exactId(raw) ? idKey(raw) : raw) : undefined + if (parent && closure.has(parent[".id"]) && !closure.has(row[".id"])) { closure.add(row[".id"]); changed = true } + } + } + if (closure.size !== ids.length || ids.some(id => !closure.has(id))) fail("STATE_CONFLICT", "Каскад удаления CA не совпадает с точным манифестом") + if (command.cascadeIds) for (const row of rows.filter(row => closure.has(row[".id"]))) if (!(isIpsecManagedComment(row.comment) || row.name === IPSEC_CA_CERT || row.name === IPSEC_SERVER_CERT || row.name?.startsWith("ipsec-user-"))) fail("DEPENDENCY_CONFLICT", "Каскад удаления CA затрагивает чужой сертификат") + } + const next = structuredClone(state) + next.catalogs[command.path] = next.catalogs[command.path].filter(row => !ids.includes(row[".id"])) + return next +} diff --git a/backend/src/services/ipsec-server-delete.ts b/backend/src/services/ipsec-server-delete.ts new file mode 100644 index 00000000..2da34450 --- /dev/null +++ b/backend/src/services/ipsec-server-delete.ts @@ -0,0 +1,72 @@ +import { ipsecExternalPoolDependencyPaths, ipsecPoolDependencies } from "./ipsec-pool-dependencies.js" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { readIpsecPeerDeleteState, ipsecPeerDeleteProof, ipsecPeerDeleteRows, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureIpsecSnapshot } from "./ipsec-live.js" +import { planIpsecServerDelete, projectIpsecServerDelete, type IpsecServerDeleteCommand } from "./ipsec-server-delete-model.js" + +// Draft guard only: add external consumers to the existing whole-state proof. +export async function readIpsecServerDeleteState(server:ServerRow,authorize:()=>Promise=async()=>{}):Promise{ + const deadline=Date.now()+45000 + const check=async()=>{if(Date.now()>=deadline)throw new McpError("STATE_UNAVAILABLE","Истёк бюджет чтения IPsec",502);await authorize();const fresh=await getServerRowById(server.id);if(!fresh||!sameWireguardEndpoint(fresh,server))throw new McpError("STATE_CONFLICT","Источник IPsec изменился",409)} + const state=await readIpsecPeerDeleteState(server,check),client=MikrotikClient.fromServer(server) + for(const path of ipsecExternalPoolDependencyPaths){ + await check();let raw:unknown + try{raw=await client.get(path,Math.max(1,Math.min(10000,deadline-Date.now())),undefined,1048576)}catch{throw new McpError("STATE_UNAVAILABLE","Обязательный каталог потребителей IPv4 pool недоступен",502)} + await check() + state.catalogs[path]=ipsecPeerDeleteRows(raw) + if(Buffer.byteLength(JSON.stringify(state))>1048576||Object.values(state.catalogs).reduce((sum,rows)=>sum+rows.length,0)>10000)throw new McpError("STATE_UNAVAILABLE","Превышен полный бюджет зависимостей IPsec",502) + } + ipsecPoolDependencies(state.catalogs) + await check();state.proof=await ipsecPeerDeleteProof(state);return state +} +export async function ipsecServerDeleteStageProofs(state: IpsecPeerDeleteState, commands: IpsecServerDeleteCommand[]) { + if (commands.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Удаление IPsec превышает предел полного плана", 502) + const proofs = [state.proof] + for (const command of commands) { state = projectIpsecServerDelete(state, command); proofs.push(await ipsecPeerDeleteProof(state)) } + return proofs +} +export class IpsecServerDeleteExecutionError extends McpError { + constructor(code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string, readonly confirmedWrites: number, readonly completedCommands: number, readonly commandCount: number) { super(code, message, 502) } +} +/** Full shared REST/MCP operation. The sealed manifest includes every indirect + * certificate removal; no CA command can expand its footprint after approval. */ +export async function deleteIpsecServerGuarded(server: ServerRow, removeCertificates = true, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: typeof captureAndAppendRevision; receiptId?: string; checkpoint?: (value: { receiptId: string; commandCount: number; completedCommands: number; afterProof: string }) => Promise } = {}) { + if (typeof removeCertificates !== "boolean") throw new McpError("INVALID_INPUT", "Некорректный режим удаления сертификатов", 400) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "ipsec_server", id: String(server.id) }, [server.id], async () => { + const source = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился", 409) } + let state = await readIpsecServerDeleteState(server, source) + if (expectedProof !== undefined && state.proof !== expectedProof) throw new McpError("STATE_CONFLICT", "IPsec или зависимости изменились после подготовки", 409) + const plan = planIpsecServerDelete(state, removeCertificates), proofs = await ipsecServerDeleteStageProofs(state, plan.commands), client = MikrotikClient.fromServer(server) + const cascadeIndirectObjectCount = plan.commands.reduce((total, command) => total + (command.cascadeIds?.length ?? 0), 0) + let completedCommands = 0, confirmedWrites = 0 + const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string) => new IpsecServerDeleteExecutionError(code, message, confirmedWrites, completedCommands, plan.commands.length) + for (const command of plan.commands) { + let dispatched = false + try { + await client.delete(command.path + "/" + encodeURIComponent(command.id), 10000, async () => { + const fresh = await readIpsecServerDeleteState(server, source) + if (fresh.proof !== state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился после durable intent; DELETE не отправлен", 409) + await source(); dispatched = true + }) + confirmedWrites++ + } catch (error) { + if (!dispatched && !completedCommands && error instanceof McpError) throw error + throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Удаление сервера IPsec остановлено; повтор команды запрещён, требуется сверка") + } + try { state = await readIpsecServerDeleteState(server, source) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "DELETE отправлен, но read-back недоступен; повтор запрещён") } + if (state.proof !== proofs[completedCommands + 1]) throw stopped("VERIFICATION_FAILED", "Не подтверждено точное удаление manifest и сохранность остальных объектов IPsec; повтор запрещён") + completedCommands++ + try { await source(); await options.checkpoint?.({ receiptId: options.receiptId ?? "", commandCount: plan.commands.length, completedCommands, afterProof: state.proof }) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Удаление проверено, но checkpoint не сохранён; повтор запрещён") } + } + 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 + try { await source(); historyRecorded = (await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "apply", capture: () => captureIpsecSnapshot(server, reader) })) !== null } catch { /* Never replay acknowledged DELETEs for history. */ } + return { ok: true, verified: true, receiptId: options.receiptId ?? null, removeCertificates, commandCount: plan.commands.length, completedCommands, confirmedWrites, affectedObjects: plan.objects.length, affectedObjectCount: plan.objects.length, cascadeIndirectObjectCount, afterProof: state.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", automaticReplay: false, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/ipsec-server-init-model.test.ts b/backend/src/services/ipsec-server-init-model.test.ts new file mode 100644 index 00000000..510cbf3c --- /dev/null +++ b/backend/src/services/ipsec-server-init-model.test.ts @@ -0,0 +1,87 @@ +import assert from "node:assert/strict" +import { ipsecPeerDeletePaths } from "./ipsec-peer-delete.js" +import { IPSEC_CA_CERT, IPSEC_SERVER_CERT, IPSEC_COMMON_NAME, IPSEC_POLICY_GROUP, ipsecManagedComment } from "./ipsec-config.js" +import { planIpsecServerInit, projectIpsecServerInit, type IpsecServerInitState } from "./ipsec-server-init-model.js" +import { ipsecServerInitValue } from "./ipsec-server-init.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { ipsecExternalPoolDependencyPaths } from "./ipsec-pool-dependencies.js" + +const server = { id: 1, host: "vpn.example.test" } as ServerRow +const request = { serverId: 1, serverEndpoint: "vpn.example.test", poolCidr: "10.77.0.0/24", dns: "192.0.2.53", caDaysValid: 3650, serverDaysValid: 900, clientDaysValid: 180, createNatRule: true } +const empty = (): IpsecServerInitState => ({ source: "source", proof: "unused", protections: [], catalogs: { ...Object.fromEntries([...ipsecPeerDeletePaths, ...ipsecExternalPoolDependencyPaths].map(path => [path, []])), "/ip/route": [{ ".id": "*r", "dst-address": "0.0.0.0/0", active: "true", "immediate-gw": "192.0.2.1%ether1" }], "/interface": [{ ".id": "*i", name: "ether1", disabled: "false" }] } }) +const state = empty(), frozen = structuredClone(state), plan = planIpsecServerInit(state, server, request) +assert.deepEqual(state, frozen) +assert.equal(plan.commands.length, 14) +assert.equal(plan.commands.filter(row => row.action === "sign").length, 2) +assert(!plan.commands.some(row => /export|^\/file/.test(row.path))) +assert.equal(plan.commands.at(-1)!.fields["out-interface"], "ether1") +assert.equal(plan.commands.find(row => row.fields.name === IPSEC_SERVER_CERT && row.action === "create")!.fields["days-valid"], "900") +for (const [serverEndpoint, expectedEndpoint, san] of [ + ["vpn.example.test", "vpn.example.test", "DNS:vpn.example.test"], + [" VPN.Example.Test. ", "vpn.example.test", "DNS:vpn.example.test"], + ["vpn.bücher.test", "vpn.xn--bcher-kva.test", "DNS:vpn.xn--bcher-kva.test"], + ["192.0.2.10", "192.0.2.10", "IP:192.0.2.10"], + ["2001:0DB8:0000:0000:0000:0000:0000:0001", "2001:db8::1", "IP:2001:db8::1"], +]) { + const fields = planIpsecServerInit(state, server, { ...request, serverEndpoint }).commands.find(row => row.fields.name === IPSEC_SERVER_CERT && row.action === "create")!.fields + assert.equal(fields["common-name"], expectedEndpoint) + assert.equal(fields["subject-alt-name"], san) +} +for (const serverEndpoint of ["192.0.2.999", "192.000.2.10", "127.1", "fe80::1%ether1", "[2001:db8::1]", "192.0.2.10/24", "https://vpn.example.test", "vpn.example.test:500", "vpn.example.test,DNS:other.test", "bad..example.test"]) { + assert.throws(() => planIpsecServerInit(state, server, { ...request, serverEndpoint }), { code: "INVALID_INPUT" }) +} +assert.equal(plan.commands.find(row => row.path === "/ip/ipsec/mode-config")!.fields["static-dns"], request.dns) +assert.equal(planIpsecServerInit(state, server, { ...request, createNatRule: false }).commands.length, 13) +assert.equal(planIpsecServerInit(state, server, { ...request, clientDaysValid: 77 }).commands.length, 14) +for (const raw of [{ ...request, psk: "NEVER" }, { ...request, passphrase: "NEVER" }, { ...request, poolCidr: "invalid" }, { ...request, serverId: 2 }, { ...request, serverEndpoint: " " }]) assert.throws(() => planIpsecServerInit(state, server, raw)) +for (const path of ipsecPeerDeletePaths) { const bad = empty(); delete bad.catalogs[path]; assert.throws(() => planIpsecServerInit(bad, server, request)) } +for (const change of [(s: IpsecServerInitState) => s.catalogs["/certificate"].push({ ".id": "*a", name: IPSEC_CA_CERT, comment: "foreign" }), (s: IpsecServerInitState) => s.catalogs["/certificate"].push({ ".id": "*a", name: IPSEC_CA_CERT, comment: ipsecManagedComment("CA") }), (s: IpsecServerInitState) => s.catalogs["/ip/ipsec/profile"].push({ ".id": "*p", name: IPSEC_COMMON_NAME }), (s: IpsecServerInitState) => s.catalogs["/ip/ipsec/policy/group"].push({ ".id": "*g", name: IPSEC_POLICY_GROUP }), (s: IpsecServerInitState) => s.catalogs["/interface"].push({ ".id": "*i2", name: "ether1" })]) { const bad = empty(); change(bad); assert.throws(() => planIpsecServerInit(bad, server, request)) } +let projected = empty(), serial = 0 +const owned = new Set() +for (const command of plan.commands) { + const input = projected, prior = structuredClone(projected) + if (command.action === "create") { + const id = `*n${++serial}`, row = { ".id": id, ...command.fields, "opaque-default": "NEVER_DEFAULT" } + assert.throws(() => projectIpsecServerInit(projected, command, { observedRow: row })) + assert.throws(() => projectIpsecServerInit(projected, command, { assignedId: id, observedRow: { ...row, [Object.keys(command.fields)[0]]: "changed" } })) + projected = projectIpsecServerInit(projected, command, { assignedId: id, observedRow: row }) + if (command.path === "/certificate") owned.add(id) + } else if (command.action === "sign") { + const row = projected.catalogs["/certificate"].find(row => row.name === command.name)! + const signed = { ...row, ...(command.fields.ca ? { ca: command.fields.ca } : {}), fingerprint: "synthetic", "invalid-after": "2030-01-01" } + assert.throws(() => projectIpsecServerInit(projected, command, { observedRow: signed, ownedCertificateIds: new Set() })) + assert.throws(() => projectIpsecServerInit(projected, command, { observedRow: { ...signed, "opaque-default": "changed" }, ownedCertificateIds: owned })) + projected = projectIpsecServerInit(projected, command, { observedRow: signed, ownedCertificateIds: owned }) + } else projected = projectIpsecServerInit(projected, command, { ownedCertificateIds: owned }) + assert.deepEqual(input, prior) +} +const rerun = planIpsecServerInit(projected, server, request) +assert(!rerun.commands.some(row => row.action === "sign")) +assert(!rerun.commands.some(row => row.action === "create")) +assert.equal(rerun.commands.length, 7) +const foreign = structuredClone(projected); foreign.catalogs["/ip/ipsec/peer"].push({ ".id": "*foreign", name: "foreign", profile: IPSEC_COMMON_NAME }); assert.throws(() => planIpsecServerInit(foreign, server, request)) +for (const [path, field, parentPath, parentName] of [ + ["/ip/ipsec/identity", "certificate", "/certificate", IPSEC_SERVER_CERT], + ["/ip/ipsec/identity", "remote-certificate", "/certificate", IPSEC_SERVER_CERT], + ["/ip/ipsec/policy", "peer", "/ip/ipsec/peer", IPSEC_COMMON_NAME], + ["/ip/ipsec/peer", "profile", "/ip/ipsec/profile", IPSEC_COMMON_NAME], +] as const) { + const parent = projected.catalogs[parentPath].find(row => row.name === parentName)! + for (const reference of [parentName, parent[".id"].toUpperCase(), `*00${parent[".id"].slice(1).toUpperCase()}`, `other, ${parentName}`, `other,*00${parent[".id"].slice(1).toUpperCase()}`]) { + const bad = structuredClone(projected); bad.catalogs[path].push({ ".id": "*consumer", name: "foreign-consumer", [field]: reference }) + assert.throws(() => planIpsecServerInit(bad, server, request), `${field}: ${reference}`) + } +} +for (const child of [{ ca: IPSEC_CA_CERT }, { ca: `*00${projected.catalogs["/certificate"][0][".id"].slice(1).toUpperCase()}` }, { issuer: "CN=MikrotikManager IPsec CA,O=foreign" }]) { + const bad = structuredClone(projected); bad.catalogs["/certificate"].push({ ".id": "*child", name: "foreign-child", ...child }); assert.throws(() => planIpsecServerInit(bad, server, request)) +} +const duplicateId = empty(); duplicateId.catalogs["/certificate"].push({ ".id": "*a" }, { ".id": "*00A" }); assert.throws(() => planIpsecServerInit(duplicateId, server, request)) +for (const path of ipsecExternalPoolDependencyPaths) { + const bad = structuredClone(projected), pool = bad.catalogs["/ip/pool"].find(row => row.name === IPSEC_COMMON_NAME)! + bad.catalogs[path].push({ ".id": "*allocator", name: "foreign-allocator", [path.startsWith("/ppp/") ? "remote-address" : "address-pool"]: `*00${pool[".id"].slice(1).toUpperCase()}` }) + assert.throws(() => planIpsecServerInit(bad, server, request)) +} +const system = structuredClone(projected); system.catalogs["/ip/ipsec/profile"][0].dynamic = "true"; assert.throws(() => planIpsecServerInit(system, server, request)) +const unknown = structuredClone(projected); unknown.catalogs["/ip/ipsec/profile"][0].opaque = "changed"; assert.notDeepEqual(ipsecServerInitValue(unknown), ipsecServerInitValue(projected)) +const expiry = structuredClone(projected); expiry.catalogs["/certificate"][0]["expires-after"] = "123d"; assert.deepEqual(ipsecServerInitValue(expiry), ipsecServerInitValue(projected)) +console.log("IPsec init model: all REST fields, all raw11 catalogs, NAT egress, owned reuse, foreign/system/dependency denial, ACK-only defaults, new-owned-only sign, full opaque proof") diff --git a/backend/src/services/ipsec-server-init-model.ts b/backend/src/services/ipsec-server-init-model.ts new file mode 100644 index 00000000..271d76f0 --- /dev/null +++ b/backend/src/services/ipsec-server-init-model.ts @@ -0,0 +1,145 @@ +import { ipsecInitRequestSchema } from "@mmapp/contracts/ipsec" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError } from "../modules/mcp/security.js" +import { ipsecPeerDeletePaths, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { IPSEC_CA_CERT, IPSEC_COMMON_NAME, IPSEC_SERVER_CERT, IPSEC_POLICY_GROUP, ipsecManagedComment, ipsecServerCertificateNames, isIpsecManagedComment, poolRangesFromCidr, selectIke2Peers, selectSharedIke2Identity } from "./ipsec-config.js" +import { identityRosBody, selectIpsecNatInterface } from "./ipsec-ros.js" +import { assertPoolMutationDependencies } from "./ipsec-pool-dependencies.js" + +type Row = Record +export type IpsecServerInitState = IpsecPeerDeleteState +export const ipsecServerInitRequest = ipsecInitRequestSchema.strict() +export interface IpsecServerInitCommand { action: "create" | "update" | "sign"; path: string; fields: Row; id?: string; name?: string } +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, code === "INVALID_INPUT" ? 400 : code === "STATE_UNAVAILABLE" ? 502 : 409) +const exactId = (value: unknown): value is string => typeof value === "string" && /^\*[a-z0-9]+$/i.test(value) && value.length <= 100 +const idKey = (value: string) => value.toLowerCase().replace(/^\*0+(?=[a-z0-9])/, "*") +export function validateIpsecServerInitState(state: IpsecServerInitState, nat: boolean) { + if (!state?.source || !state.catalogs || !Array.isArray(state.protections)) throw fail("Неполное состояние IPsec", "STATE_UNAVAILABLE") + for (const path of [...ipsecPeerDeletePaths, ...(nat ? ["/ip/route", "/interface"] : [])]) { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000 || rows.some(row => !row || !exactId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(rows.map(row => idKey(row[".id"]))).size !== rows.length) throw fail("Неполный native-каталог IPsec", "STATE_UNAVAILABLE") + const names = rows.map(row => row.name?.trim()).filter(Boolean) + if (path !== "/ip/ipsec/active-peers" && new Set(names).size !== names.length) throw fail("Неоднозначное имя IPsec") + for (const row of rows) if (row.name && exactId(row.name.trim()) && rows.some(other => other !== row && idKey(other[".id"]) === idKey(row.name.trim()))) throw fail("Неоднозначное имя/native ID IPsec") + } +} +export function planIpsecServerInit(state: IpsecServerInitState, server: ServerRow, raw: unknown) { + const parsed = ipsecServerInitRequest.safeParse(raw) + if (!parsed.success) throw fail("Некорректные поля инициализации IPsec", "INVALID_INPUT") + const body = parsed.data, ranges = poolRangesFromCidr(body.poolCidr), namesForCertificate = ipsecServerCertificateNames(body.serverEndpoint) + if (Number(body.serverId) !== server.id || !ranges || !namesForCertificate || (body.dns !== undefined && /[\x00-\x1f\x7f]/.test(body.dns))) throw fail("Некорректный сервер, endpoint, DNS или пул IPsec", "INVALID_INPUT") + validateIpsecServerInitState(state, body.createNatRule) + const c = state.catalogs, commands: IpsecServerInitCommand[] = [], companions: Array<{ kind: string; id: string }> = [] + const kinds: Record = { "/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", "/ip/firewall/nat": "firewall_nat" } + // Include the full raw closure, including foreign consumers and every protection. + for (const path of ipsecPeerDeletePaths) for (const row of c[path]) companions.push({ kind: kinds[path] ?? path, id: row[".id"] }) + const named = (path: string, name: string) => { const row = c[path].find(item => item.name?.trim() === name); if (row && row.name !== name) throw fail("Не подтверждено точное имя IPsec"); return row } + const system = (row: Row) => { for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) throw fail("Системный объект IPsec нельзя изменять", "PROTECTED_OBJECT") } + const peer = named("/ip/ipsec/peer", IPSEC_COMMON_NAME) + const managedPeer = peer && isIpsecManagedComment(peer.comment) + const refs: Record = { 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", group: "/ip/ipsec/policy/group", "policy-template-group": "/ip/ipsec/policy/group" } + const alias = (value: string, row: Row) => value.trim() === row.name || (exactId(value.trim()) && idKey(value.trim()) === idKey(row[".id"])) + const references = (value: string | undefined, path: string, row: Row): boolean => { + if (!value?.trim()) return false + // An exact scalar name wins over CSV interpretation (certificate names may + // themselves contain commas). Otherwise inspect every list member. + if (c[path].some(item => alias(value, item))) return alias(value, row) + return value.split(",").some(part => alias(part, row)) + } + const issuedBy = (child: Row, parent: Row) => { + if (references(child.ca, "/certificate", parent) || references(child.issuer, "/certificate", parent)) return true + const issuer = child.issuer?.trim(), cn = parent["common-name"]?.trim() + return !!issuer && !!cn && (issuer === cn || issuer.split(/[,/]/).some(part => part.trim().replace(/^CN\s*=\s*/i, "") === cn)) + } + const consumers = (path: string, row: Row) => Object.entries(c).flatMap(([p, rows]) => rows.filter(item => Object.entries(refs).some(([field, target]) => target === path && references(item[field], path, row)) || (path === "/certificate" && p === "/certificate" && issuedBy(item, row))).map(item => ({ path: p, row: item }))) + const pool = named("/ip/pool", IPSEC_COMMON_NAME) + const allowedPoolConsumers = [ + ...c["/ip/pool"].filter(row => isIpsecManagedComment(row.comment)).map(row => ({ path: "/ip/pool", id: row[".id"] })), + ...c["/ip/ipsec/mode-config"].filter(row => isIpsecManagedComment(row.comment) || (row.name === IPSEC_COMMON_NAME && managedPeer) || c["/ip/ipsec/identity"].some(identity => isIpsecManagedComment(identity.comment) && references(identity["mode-config"], "/ip/ipsec/mode-config", row))).map(row => ({ path: "/ip/ipsec/mode-config", id: row[".id"] })), + ] + assertPoolMutationDependencies(c, pool ? [pool] : [], allowedPoolConsumers) + const owned = (path: string, row: Row) => { + system(row) + const legacy = row.name === IPSEC_COMMON_NAME && ["/ip/ipsec/profile", "/ip/ipsec/mode-config"].includes(path) && managedPeer + const group = path === "/ip/ipsec/policy/group" && row.name === IPSEC_POLICY_GROUP && c["/ip/ipsec/policy"].some(p => references(p.group, path, row) && isIpsecManagedComment(p.comment)) + if (!isIpsecManagedComment(row.comment) && !legacy && !group) throw fail("Имя IPsec занято чужим объектом", "PROTECTED_OBJECT") + for (const consumer of consumers(path, row)) { + if (consumer.path === "/certificate" && consumer.row[".id"] === row[".id"]) continue + if (consumer.path === "/ip/ipsec/policy" && observedBoolean(consumer.row.dynamic) === true) continue + const legacyMc = consumer.path === "/ip/ipsec/mode-config" && consumer.row.name === IPSEC_COMMON_NAME && managedPeer + if (!isIpsecManagedComment(consumer.row.comment) && !legacyMc) throw fail("Объект IPsec используется чужой конфигурацией", "DEPENDENCY_CONFLICT") + } + } + const upsert = (path: string, fields: Row, row = fields.name ? named(path, fields.name) : undefined) => { + if (row) owned(path, row) + commands.push({ action: row ? "update" : "create", path, fields, ...(row ? { id: row[".id"] } : {}) }) + } + const certificate = (name: string, fields: Row, ca?: string) => { + const row = named("/certificate", name) + if (row) { owned("/certificate", row); if (!row["invalid-after"]?.trim() || !row.fingerprint?.trim()) throw fail("Существующий неподписанный сертификат требует отдельного проверенного плана; подпись разрешена только новому ACK-owned сертификату"); if (ca && row.ca !== ca) throw fail("Серверный сертификат подписан другим CA"); return row } + commands.push({ action: "create", path: "/certificate", fields: { name, ...fields } }, { action: "sign", path: "/certificate/sign", name, fields: { name, ...(ca ? { ca } : {}), "days-valid": fields["days-valid"] } }) + return undefined + } + const ca = certificate(IPSEC_CA_CERT, { "common-name": "MikrotikManager IPsec CA", "key-size": "4096", "key-usage": "key-cert-sign,crl-sign", "days-valid": String(body.caDaysValid), comment: ipsecManagedComment("CA") }) + if (!ca || observedBoolean(ca.trusted) !== true) commands.push({ action: "update", path: "/certificate", ...(ca ? { id: ca[".id"] } : { name: IPSEC_CA_CERT }), fields: { trusted: "yes" } }) + certificate(IPSEC_SERVER_CERT, { "common-name": namesForCertificate.endpoint, "subject-alt-name": namesForCertificate.san, "key-size": "2048", "key-usage": "digital-signature,key-encipherment,tls-server", "days-valid": String(body.serverDaysValid), comment: ipsecManagedComment("server") }, IPSEC_CA_CERT) + const comment = ipsecManagedComment("IKEv2 road-warrior") + upsert("/ip/ipsec/profile", { name: IPSEC_COMMON_NAME, "hash-algorithm": "sha256", "enc-algorithm": "aes-256,aes-192,aes-128", "dh-group": "modp2048", "nat-traversal": "yes" }) + upsert("/ip/ipsec/proposal", { name: IPSEC_COMMON_NAME, "auth-algorithms": "sha256", "enc-algorithms": "aes-256-cbc,aes-192-cbc,aes-128-cbc", "pfs-group": "modp2048", comment }) + upsert("/ip/pool", { name: IPSEC_COMMON_NAME, ranges, comment }) + upsert("/ip/ipsec/mode-config", { name: IPSEC_COMMON_NAME, "address-pool": IPSEC_COMMON_NAME, responder: "yes", ...(body.dns ? { "static-dns": body.dns } : {}) }) + const group = named("/ip/ipsec/policy/group", IPSEC_POLICY_GROUP) + if (group) owned("/ip/ipsec/policy/group", group); else commands.push({ action: "create", path: "/ip/ipsec/policy/group", fields: { name: IPSEC_POLICY_GROUP } }) + const templates = c["/ip/ipsec/policy"].filter(row => row.comment?.trim() === comment && observedBoolean(row.template) === true) + if (templates.length > 1) throw fail("Неоднозначный шаблон IPsec") + upsert("/ip/ipsec/policy", { "src-address": "0.0.0.0/0", "dst-address": body.poolCidr, proposal: IPSEC_COMMON_NAME, group: IPSEC_POLICY_GROUP, comment, ...(!templates.length ? { template: "yes" } : {}) }, templates[0]) + upsert("/ip/ipsec/peer", { name: IPSEC_COMMON_NAME, address: "0.0.0.0/0", "exchange-mode": "ike2", passive: "yes", profile: IPSEC_COMMON_NAME, comment }) + const names = [...new Set([...selectIke2Peers(c["/ip/ipsec/peer"]).map(row => row.name!), IPSEC_COMMON_NAME])] + const listener = selectSharedIke2Identity(c["/ip/ipsec/identity"], names) as Row | undefined + if (listener) { owned("/ip/ipsec/identity", listener); if (listener["policy-template-group"] !== IPSEC_POLICY_GROUP) commands.push({ action: "update", path: "/ip/ipsec/identity", id: listener[".id"], fields: { "policy-template-group": IPSEC_POLICY_GROUP } }) } + else commands.push({ action: "create", path: "/ip/ipsec/identity", fields: identityRosBody({ peerName: IPSEC_COMMON_NAME, modeConfig: IPSEC_COMMON_NAME, certificate: IPSEC_SERVER_CERT, comment: ipsecManagedComment("IKEv2 listener"), authMethod: "certificate", policyTemplateGroup: IPSEC_POLICY_GROUP }) }) + for (const identity of c["/ip/ipsec/identity"]) if (identity !== listener && identity.peer === IPSEC_COMMON_NAME && isIpsecManagedComment(identity.comment) && (!identity["policy-template-group"] || identity["policy-template-group"] === "default")) { owned("/ip/ipsec/identity", identity); commands.push({ action: "update", path: "/ip/ipsec/identity", id: identity[".id"], fields: { "policy-template-group": IPSEC_POLICY_GROUP } }) } + if (body.createNatRule) { + const out = selectIpsecNatInterface(c["/ip/route"], c["/interface"]), natComment = ipsecManagedComment("интернет клиентам VPN") + const matches = c["/ip/firewall/nat"].filter(row => row.comment?.trim() === natComment && row.chain === "srcnat") + if (matches.length > 1) throw fail("Неоднозначное правило NAT IPsec") + upsert("/ip/firewall/nat", { chain: "srcnat", action: "masquerade", "src-address": body.poolCidr, "out-interface": out, "ipsec-policy": "out,none", comment: natComment }, matches[0]) + } + // Every existing dependency of every command must resolve exactly. New names are + // permitted only if an earlier command creates them within this reviewed plan. + const created = new Set() + for (const command of commands) { + for (const [field, path] of Object.entries(refs)) { + const value = command.fields[field] + if (!value || ["none", "auto", "ignore"].includes(value) || created.has(`${path}:${value}`)) continue + if (c[path].filter(row => row.name === value || row[".id"] === value).length !== 1) throw fail("Не подтверждена зависимость IPsec") + } + if (command.action === "create" && command.fields.name) created.add(`${command.path}:${command.fields.name}`) + } + return { commands, companions, target: { kind: "ipsec_server", id: String(server.id) } } +} + +export function projectIpsecServerInit(state: IpsecServerInitState, command: IpsecServerInitCommand, options: { assignedId?: string; observedRow?: Row; ownedCertificateIds?: Set } = {}) { + const next = structuredClone(state), rows = next.catalogs[command.action === "sign" ? "/certificate" : command.path] + if (!rows) throw fail("Неизвестный каталог IPsec", "STATE_UNAVAILABLE") + if (command.action === "update") { + const row = rows.find(item => command.id ? item[".id"] === command.id : item.name === command.name) + if (!row || (!command.id && !options.ownedCertificateIds?.has(row[".id"]))) throw fail("Не подтверждён объект изменения IPsec", "VERIFICATION_FAILED") + Object.assign(row, command.fields) + } else if (command.action === "sign") { + const row = rows.find(item => item.name === command.name), observed = options.observedRow + if (!row || !options.ownedCertificateIds?.has(row[".id"]) || !observed || observed[".id"] !== row[".id"] || !observed["invalid-after"]?.trim() || !observed.fingerprint?.trim()) throw fail("Не подтверждена подпись нового сертификата", "VERIFICATION_FAILED") + const generated = new Set(["ca", "ca-crl-host", "serial-number", "fingerprint", "invalid-before", "invalid-after", "expires-after", "akid", "skid", "issuer", "private-key", "issued", "status", "trusted"]) + for (const key of new Set([...Object.keys(row), ...Object.keys(observed)])) if (!generated.has(key) && observed[key] !== row[key]) throw fail("При подписи изменены исходные поля сертификата", "VERIFICATION_FAILED") + if (command.fields.ca && observed.ca !== command.fields.ca) throw fail("CA подписи не подтверждён", "VERIFICATION_FAILED") + rows[rows.indexOf(row)] = structuredClone(observed) + } else { + const row = options.observedRow, id = options.assignedId + if (!row || !exactId(id) || row[".id"] !== id || rows.some(item => idKey(item[".id"]) === idKey(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") + for (const [key, value] of Object.entries(command.fields)) if (row[key] !== value && !(["disabled", "responder", "template", "nat-traversal", "passive", "trusted"].includes(key) && observedBoolean(row[key]) !== null && observedBoolean(row[key]) === observedBoolean(value))) throw fail("Поля нового объекта не подтверждены", "VERIFICATION_FAILED") + rows.push(structuredClone(row)) + } + return next +} diff --git a/backend/src/services/ipsec-server-init.ts b/backend/src/services/ipsec-server-init.ts new file mode 100644 index 00000000..38677984 --- /dev/null +++ b/backend/src/services/ipsec-server-init.ts @@ -0,0 +1,97 @@ +import { randomUUID } from "node:crypto" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { McpError, fingerprint, privateFingerprinter } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { readIpsecPeerDeleteState, ipsecPeerDeleteRows, ipsecPeerDeleteValue } from "./ipsec-peer-delete.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureIpsecSnapshot, mapClients, mapServerSummary, type IpsecServerState } from "./ipsec-live.js" +import { ipsecServerInitRequest, planIpsecServerInit, projectIpsecServerInit, validateIpsecServerInitState, type IpsecServerInitState } from "./ipsec-server-init-model.js" +import { ipsecExternalPoolDependencyPaths, ipsecPoolDependencies } from "./ipsec-pool-dependencies.js" + +export function ipsecServerInitValue(state: IpsecServerInitState) { + const value = ipsecPeerDeleteValue(state) + for (const rows of Object.values(value.catalogs)) for (const row of rows) for (const key of ["disabled", "responder", "template", "nat-traversal", "passive", "trusted"]) { + if (row[key] === undefined) continue + const boolean = observedBoolean(row[key]) + if (boolean === null) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён boolean IPsec", 502) + row[key] = String(boolean) + } + return value +} +export async function ipsecServerInitProof(state: IpsecServerInitState) { return (await privateFingerprinter("ipsec-server-init-v2"))(fingerprint(ipsecServerInitValue(state))) } +export async function readIpsecServerInitState(server: ServerRow, authorize: () => Promise = async () => {}) { + const source = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился во время полного чтения", 409) } + const state = await readIpsecPeerDeleteState(server, source) + // Always bind NAT source catalogs, including plans which opt out of NAT. + for (const path of ["/ip/route", "/interface", ...ipsecExternalPoolDependencyPaths]) { + await source() + state.catalogs[path] = ipsecPeerDeleteRows(await MikrotikClient.fromServer(server).get(path, 10000, undefined, 1048576)) + await source() + if (Buffer.byteLength(JSON.stringify(state)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Полное состояние инициализации IPsec превышает 1 МиБ", 502) + } + if (Buffer.byteLength(JSON.stringify(state)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Полное состояние инициализации IPsec превышает 1 МиБ", 502) + validateIpsecServerInitState(state, true) + ipsecPoolDependencies(state.catalogs) + await source() + return { ...state, proof: await ipsecServerInitProof(state) } +} +export class IpsecServerInitExecutionError extends McpError { + constructor(code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string, readonly confirmedWrites: number, readonly completedCommands: number, readonly commandCount: number) { super(code, message, 502) } +} +export async function initIpsecServerGuarded(server: ServerRow, raw: unknown, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: typeof captureAndAppendRevision; receiptId?: string; checkpoint?: (value: { receiptId: string; commandCount: number; completedCommands: number; afterProof: string }) => Promise } = {}) { + const body = ipsecServerInitRequest.parse(raw), receiptId = options.receiptId ?? randomUUID() + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "ipsec_server", id: String(server.id) }, [server.id], async () => { + const source = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился", 409) } + let state = await readIpsecServerInitState(server, source) + if (expectedProof !== undefined && state.proof !== expectedProof) throw new McpError("STATE_CONFLICT", "IPsec или зависимости изменились после подготовки", 409) + const plan = planIpsecServerInit(state, server, body), client = MikrotikClient.fromServer(server), ownedCertificateIds = new Set() + let completedCommands = 0, confirmedWrites = 0 + const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string) => new IpsecServerInitExecutionError(code, message, confirmedWrites, completedCommands, plan.commands.length) + for (const command of plan.commands) { + let dispatched = false, response: unknown, assignedId: string | undefined + const fence = async () => { const fresh = await readIpsecServerInitState(server, source); if (fresh.proof !== state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился после durable intent; команда не отправлена", 409); await source(); dispatched = true } + try { + if (command.action === "create") response = await client.put(command.path, command.fields, 30000, fence) + else if (command.action === "update") { + const id = command.id ?? state.catalogs[command.path].find(row => row.name === command.name && ownedCertificateIds.has(row[".id"]))?.[".id"] + if (!id) throw new McpError("STATE_CONFLICT", "Не подтверждён объект изменения IPsec", 409) + response = await client.patch(`${command.path}/${encodeURIComponent(id)}`, command.fields, 15000, fence) + } else response = await client.post(command.path, command.fields, 120000, undefined, fence) + confirmedWrites++ + } catch (error) { + if (!dispatched && !completedCommands && error instanceof McpError) throw error + throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Инициализация IPsec остановлена; повтор native-команды запрещён, требуется сверка") + } + let after: IpsecServerInitState + try { + after = await readIpsecServerInitState(server, source) + if (command.action === "sign") { + 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 readIpsecServerInitState(server, source) } + } + } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена, но read-back недоступен; повтор запрещён") } + try { + if (command.action === "create") { const ack = response as Record | null; if (typeof ack?.[".id"] !== "string") throw Error("missing create ACK"); assignedId = ack[".id"] } + const path = command.action === "sign" ? "/certificate" : command.path + const observedRow = after.catalogs[path].find(row => command.action === "sign" ? row.name === command.name : row[".id"] === assignedId) + const projected = projectIpsecServerInit(state, command, { assignedId, observedRow, ownedCertificateIds }) + if (await ipsecServerInitProof(projected) !== after.proof) throw Error("configuration mismatch") + if (command.action === "create" && command.path === "/certificate") ownedCertificateIds.add(assignedId!) + } catch { throw stopped("VERIFICATION_FAILED", "Не подтверждены команда и сохранность полного состояния IPsec; повтор запрещён") } + state = after; completedCommands++ + try { await source(); await options.checkpoint?.({ receiptId, commandCount: plan.commands.length, completedCommands, afterProof: state.proof }) } + catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда проверена, но checkpoint не сохранён; повтор запрещён") } + } + 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 + try { await source(); historyRecorded = (await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "apply", capture: () => captureIpsecSnapshot(server, reader) })) !== null } catch { /* Never replay native writes for history. */ } + const c = state.catalogs + const live = { server, client, peers: c["/ip/ipsec/peer"], identities: c["/ip/ipsec/identity"], modeConfigs: c["/ip/ipsec/mode-config"], pools: c["/ip/pool"], policies: c["/ip/ipsec/policy"], nat: c["/ip/firewall/nat"], active: c["/ip/ipsec/active-peers"], certs: c["/certificate"] } as IpsecServerState + return { ok: true, verified: historyRecorded, receiptId, serverId: server.id, commandCount: plan.commands.length, completedCommands, confirmedWrites, afterProof: state.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", summary: mapServerSummary(live, mapClients(live)), automaticReplay: false, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/ipsec-state.test.ts b/backend/src/services/ipsec-state.test.ts index 83e9cfb8..61eca7a5 100644 --- a/backend/src/services/ipsec-state.test.ts +++ b/backend/src/services/ipsec-state.test.ts @@ -1,5 +1,5 @@ import assert from "node:assert/strict" -import { fetchIpsecState, getEnabledIpsecServerById, listIpsec, mapClients, mapServerSummary, resolveIpsecClientTarget } from "./ipsec-live.js" +import { fetchIpsecState, getEnabledIpsecServerById, listIpsec, mapClients, mapServerSummary, resolveIpsecClientTarget, ipsecClientBindingKeys } from "./ipsec-live.js" import { removeManagedIpsecServer } from "./ipsec-cleanup.js" import { deleteUserModeConfig, ensureIpsecPeer, identityRosBody, putOrPatchByName } from "./ipsec-ros.js" import { ipsecManagedComment, isIpsecManagedComment } from "./ipsec-config.js" @@ -244,4 +244,7 @@ assert.equal(mapClients(sanState)[0]?.online,false); checks++ sanState.certs.pop() sanState.identities=[] assert.equal(mapClients(sanState)[0]?.online,false); checks++ +assert.deepEqual(ipsecClientBindingKeys(state), mapClients(state).map(client => client.commonName ?? client.name)); checks++ +assert(ipsecClientBindingKeys(state).includes("PSK"), "Managed PSK selector survives the keys-only projection"); checks++ +assert.deepEqual(ipsecClientBindingKeys(sessionState), mapClients(sessionState).map(client => client.commonName ?? client.name)); checks++ console.log(`ipsec-state: ${checks} checks passed`) diff --git a/backend/src/services/ipsec-user-create-model.test.ts b/backend/src/services/ipsec-user-create-model.test.ts new file mode 100644 index 00000000..798babc4 --- /dev/null +++ b/backend/src/services/ipsec-user-create-model.test.ts @@ -0,0 +1,73 @@ +import assert from "node:assert/strict" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { ipsecPeerDeletePaths } from "./ipsec-peer-delete.js" +import { planIpsecUserCreate as plan, projectIpsecUserCreate as project, type IpsecUserCreateState } from "./ipsec-user-create-model.js" + +const server = { id: 1, host: "vpn.example.test" } as ServerRow +const base = (): IpsecUserCreateState => ({ source: "source", proof: "proof", protections: [{ object_ref: { kind: "server", id: "1" } }], catalogs: { ...Object.fromEntries([...ipsecPeerDeletePaths, "/file"].map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", passive: "yes", profile: "profile", opaque: "peer" }], + "/ip/ipsec/profile": [{ ".id": "*r", name: "profile", opaque: "profile" }], + "/ip/ipsec/identity": [{ ".id": "*l", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "mode-config": "ipsec-vpn", "policy-template-group": "group", opaque: "listener" }], + "/ip/ipsec/policy/group": [{ ".id": "*g", name: "group" }], + "/ip/ipsec/policy": [{ ".id": "*y", group: "group", proposal: "proposal", opaque: "policy" }], + "/ip/ipsec/proposal": [{ ".id": "*q", name: "proposal", opaque: "proposal" }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "ipsec-vpn", "address-pool": "pool", "static-dns": "192.0.2.53", opaque: "shared" }], + "/ip/pool": [{ ".id": "*o", name: "pool", ranges: "192.0.2.1-192.0.2.40" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign,crl-sign", authority: "true", "private-key": "true" }, { ".id": "*s", name: "ipsec-server", "common-name": "vpn.example.test", "key-usage": "tls-server", ca: "ca", opaque: "server" }], + "/file": [{ ".id": "*f", name: "foreign.p12", size: "100", opaque: "preserve" }], +} }) +const request = { serverId: 1, name: "Alice", authMethod: "pre-shared-key", psk: "NEVER_TEST_SECRET" } +const state = base(), original = structuredClone(state), psk = plan(state, server, request, "mm-ipsec-fixture") +assert.equal(psk.commands.length, 1) +assert.deepEqual(psk.commands[0].fields, { peer: "ike", "auth-method": "pre-shared-key", secret: "NEVER_TEST_SECRET", "remote-id": "key-id:Alice", "mode-config": "ipsec-vpn", "generate-policy": "port-strict", "policy-template-group": "group", comment: "MikrotikManager:ipsec user=Alice" }) +for (const id of ["*p", "*r", "*l", "*g", "*y", "*q", "*m", "*o"]) assert(psk.companions.some(row => row.id === id)) +const staticPlan = plan(state, server, { ...request, staticIp: "192.0.2.7", remoteId: "fqdn:alice.example.test", peerName: "ike" }, "mm-ipsec-fixture") +assert.deepEqual(staticPlan.commands.map(row => row.action), ["create", "create"]) +let projected = state +for (const [i, command] of staticPlan.commands.entries()) projected = project(projected, command, { assignedId: `*new${i}`, observedRow: { ".id": `*new${i}`, ...command.fields, "native-default": "preserve" } }) +for (const path of [...ipsecPeerDeletePaths, "/file"].filter(path => !["/ip/ipsec/identity", "/ip/ipsec/mode-config"].includes(path))) assert.deepEqual(projected.catalogs[path], state.catalogs[path]) +assert.deepEqual(state, original) +assert.throws(() => plan(state, server, { ...request, staticIp: "" }, "mm-ipsec-fixture"), /IPv4/) +assert.throws(() => plan(state, server, { ...request, peerName: "missing" }, "mm-ipsec-fixture"), /peer/) +assert.throws(() => plan(state, server, { ...request, serverId: 2 }, "mm-ipsec-fixture"), /сервер/) +assert.throws(() => plan(state, server, { ...request, unexpected: true }, "mm-ipsec-fixture"), /поля/) +const duplicate = base(); duplicate.catalogs["/ip/ipsec/identity"].push({ ".id": "*d", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:Alice" }) +assert.throws(() => plan(duplicate, server, request, "mm-ipsec-fixture"), /Remote ID/) +const missingDependency = base(); missingDependency.catalogs["/ip/ipsec/proposal"] = [] +assert.throws(() => plan(missingDependency, server, request, "mm-ipsec-fixture"), /зависимость/) +const foreign = base(); foreign.catalogs["/ip/ipsec/mode-config"].push({ ".id": "*x", name: "mc-ipsec-alice", address: "192.0.2.8", opaque: "conserve" }) +foreign.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", "mode-config": "mc-ipsec-alice", comment: "Other" }) +assert.throws(() => plan(foreign, server, { ...request, staticIp: "192.0.2.7" }, "mm-ipsec-fixture"), /уже используется/) +foreign.catalogs["/ip/ipsec/identity"].at(-1)!["mode-config"] = "*X" +assert.throws(() => plan(foreign, server, { ...request, staticIp: "192.0.2.7" }, "mm-ipsec-fixture"), /уже используется/) +foreign.catalogs["/ip/ipsec/identity"].pop() +const reuse = plan(foreign, server, { ...request, staticIp: "192.0.2.7" }, "mm-ipsec-fixture") +assert.equal(reuse.commands[0].action, "update") +assert.equal(project(foreign, reuse.commands[0]).catalogs["/ip/ipsec/mode-config"][1].opaque, "conserve") +const aliases = base(); aliases.catalogs["/ip/ipsec/peer"].push({ ".id": "*P", name: "alias" }) +assert.throws(() => plan(aliases, server, request, "mm-ipsec-fixture"), /каталог/) +const chain = base(); chain.catalogs["/certificate"].push({ ".id": "*child", name: "other-child", ca: "ca" }) +chain.catalogs["/ip/ipsec/identity"].push({ ".id": "*consumer", peer: "ike", "remote-certificate": "other-child" }) +const closure = plan(chain, server, { serverId: 1, name: "Alice" }, "mm-ipsec-fixture") +assert(closure.companions.some(row => row.id === "*child")); assert(closure.companions.some(row => row.id === "*consumer")) +chain.catalogs["/certificate"][0].ca = "missing-issuer" +assert.throws(() => plan(chain, server, { serverId: 1, name: "Alice" }, "mm-ipsec-fixture"), /зависимость/) +const cert = plan(state, server, { serverId: 1, name: "Alice", daysValid: 365, passphrase: "export-secret" }, "mm-ipsec-fixture") +assert.deepEqual(cert.commands.map(row => row.action), ["create", "sign", "export", "cleanup"]) +assert.equal(cert.commands[0].fields["days-valid"], "365") +const certCommand = cert.commands[0], createdRow = { ".id": "*new", ...certCommand.fields, "private-key": "true", opaque: "new" } +const withCert = project(state, certCommand, { assignedId: "*new", observedRow: createdRow }) +const signed = { ...createdRow, ca: "ca", fingerprint: "1234", "invalid-after": "2030-01-01", "invalid-before": "2026-01-01", "serial-number": "123" } +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" } }) +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") +console.log("ipsec-user-create-model: full PSK/certificate/static/export plans and raw conservation passed") diff --git a/backend/src/services/ipsec-user-create-model.ts b/backend/src/services/ipsec-user-create-model.ts new file mode 100644 index 00000000..3ad34832 --- /dev/null +++ b/backend/src/services/ipsec-user-create-model.ts @@ -0,0 +1,136 @@ +import { isIP } from "node:net" +import { ipsecUserCreateRequestSchema } from "@mmapp/contracts/ipsec" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError } from "../modules/mcp/security.js" +import { ipsecPeerDeletePaths, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { mapClients, type IpsecServerState } from "./ipsec-live.js" +import { IPSEC_COMMON_NAME, clientCertName, ipsecManagedComment, ipsecUserComment, isIpsecManagedComment, isManagedIpsecModeConfig, isIke2RemoteAccessIdentity, normalizeIpsecRemoteId, resolveIke2CaName, resolveIke2ServerCert, selectIke2Peers, selectSharedIke2Identity, userModeConfigName } from "./ipsec-config.js" +import { identityRosBody } from "./ipsec-ros.js" +import { observedBoolean } from "./ospf-observed-values.js" + +type Row = Record +export type IpsecUserCreateState = IpsecPeerDeleteState +export const ipsecUserCreateRequest = ipsecUserCreateRequestSchema.strict() +export interface IpsecUserCreateCommand { action: "create" | "update" | "sign" | "export" | "cleanup"; path: string; fields: Row; id?: string; name?: string } +const fail = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, code === "INVALID_INPUT" ? 400 : code === "STATE_UNAVAILABLE" ? 502 : 409) +const exactId = (value: unknown): value is string => typeof value === "string" && /^\*[a-z0-9]+$/i.test(value) && value.length <= 100 +export function validateIpsecUserCreateState(state: IpsecUserCreateState) { + if (!state?.source || !state.catalogs || !Array.isArray(state.protections)) throw fail("Неполное состояние IPsec", "STATE_UNAVAILABLE") + for (const path of [...ipsecPeerDeletePaths, "/file"]) { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000 || rows.some(row => !row || !exactId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(rows.map(row => row[".id"].toLowerCase())).size !== rows.length) throw fail("Неполный native-каталог IPsec", "STATE_UNAVAILABLE") + const names = rows.map(row => row.name?.trim()).filter(Boolean) + if (path !== "/ip/ipsec/active-peers" && new Set(names).size !== names.length) throw fail("Неоднозначное имя IPsec") + } +} +/** All dependencies and every branch are resolved before the first native effect. */ +export function planIpsecUserCreate(state: IpsecUserCreateState, server: ServerRow, raw: unknown, exportPrefix: string) { + const parsed = ipsecUserCreateRequest.safeParse(raw) + if (!parsed.success) throw fail("Некорректные поля создания клиента IPsec", "INVALID_INPUT") + const body = parsed.data + if (Number(body.serverId) !== server.id || !body.name.trim() || /[\x00-\x1f\x7f]/.test(body.name) || !/^mm-ipsec-[a-z0-9-]{1,80}$/i.test(exportPrefix)) throw fail("Некорректный сервер, имя или идентификатор экспорта IPsec", "INVALID_INPUT") + if (body.staticIp !== undefined && isIP(body.staticIp.trim()) !== 4) throw fail("Некорректный статический IPv4 адрес", "INVALID_INPUT") + if (body.authMethod === "pre-shared-key" && !body.psk) throw fail("Для PSK клиента требуется secret", "INVALID_INPUT") + validateIpsecUserCreateState(state) + const c = state.catalogs + const live = { server, client: undefined, peers: c["/ip/ipsec/peer"], identities: c["/ip/ipsec/identity"], modeConfigs: c["/ip/ipsec/mode-config"], pools: c["/ip/pool"], policies: c["/ip/ipsec/policy"], nat: c["/ip/firewall/nat"], active: c["/ip/ipsec/active-peers"], certs: c["/certificate"] } as unknown as IpsecServerState + const peers = selectIke2Peers(live.peers), names = peers.map(row => (row.name ?? "").trim()).filter(Boolean) + const peer = body.peerName ? live.peers.find(row => row.name === body.peerName!.trim()) : peers.find(row => isIpsecManagedComment(row.comment)) ?? peers.find(row => row.name === IPSEC_COMMON_NAME) ?? peers[0] + if (!peer || !peer.name || peer.name !== peer.name.trim()) throw fail("Выбранный peer IPsec не найден") + const mcName = live.identities.filter(row => isIpsecManagedComment(row.comment) || isIke2RemoteAccessIdentity(row, names)).map(row => (row["mode-config"] ?? "").trim()).find(name => name && !name.startsWith("mc-ipsec-")) + const sharedMc = (mcName ? live.modeConfigs.find(row => row.name === mcName) : undefined) ?? live.modeConfigs.find(row => row.name === IPSEC_COMMON_NAME) + const serverCert = resolveIke2ServerCert(live.certs, peers, live.identities), caName = resolveIke2CaName(live.certs, serverCert) + const certName = body.authMethod === "certificate" ? clientCertName(body.name) : undefined + if (body.authMethod === "certificate" && (!caName || !serverCert?.name)) throw fail("Нет CA или серверного сертификата IKEv2") + 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 }> = [] + 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) + seed("/ip/ipsec/identity", sharedIdentity) + let modeConfig = sharedMc?.name ?? "" + if (body.staticIp) { + if (live.modeConfigs.some(row => (row.name ?? "").startsWith("mc-ipsec-") && (row.address ?? row["address-prefix"] ?? "").replace(/\/\d+$/, "").trim() === body.staticIp!.trim())) throw fail("IP уже назначен другому клиенту") + modeConfig = userModeConfigName(body.name) + const existing = c["/ip/ipsec/mode-config"].find(row => row.name?.trim() === modeConfig) + if (existing && existing.name !== modeConfig) throw fail("Не подтверждено точное имя mode-config") + 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" } }) + } + 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), comment: ipsecManagedComment(`client ${body.name.trim()}`) } }) + commands.push({ action: "sign", path: "/certificate/sign", name: certName, fields: { name: certName, ca: caName!, "days-valid": String(body.daysValid ?? 1825) } }) + } + 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`, fields: { name: certName, type: "pkcs12", "file-name": exportPrefix, "export-passphrase": body.passphrase ?? "" } }, { action: "cleanup", path: "/file", name: `${exportPrefix}.p12`, fields: {} }) + } + const kinds: Record = { "/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 = { 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)) { + const value = command.fields[field] + if (!value || ["none", "auto", "ignore"].includes(value) || commands.some(item => item.action === "create" && item.path === refPath && item.fields.name === value)) continue + const matches = c[refPath].filter(row => row.name === value || row[".id"] === value) + if (matches.length !== 1) throw fail("Не подтверждена зависимость создаваемого объекта IPsec") + seed(refPath, matches[0]) + } + const companions: Array<{ kind: string; id: string }> = [], seen = new Set() + for (let i = 0; i < seeds.length; i++) { + const { path, row } = seeds[i], key = `${path}:${row[".id"]}` + if (seen.has(key)) continue + seen.add(key); companions.push({ kind: kinds[path] ?? path, id: row[".id"] }) + for (const [field, refPath] of Object.entries(refs)) { + const value = row[field] + if (!value || ["none", "auto", "ignore"].includes(value)) continue + const matches = c[refPath].filter(item => item.name === value || item[".id"] === value) + if (matches.length !== 1) throw fail("Не подтверждена однозначная зависимость IPsec") + seed(refPath, matches[0]) + } + const reference = (value: string | undefined) => value === row.name || value?.toLowerCase() === row[".id"].toLowerCase() + if (path === "/ip/ipsec/mode-config") for (const consumer of c["/ip/ipsec/identity"].filter(item => reference(item["mode-config"]))) seed("/ip/ipsec/identity", consumer) + if (path === "/ip/ipsec/policy/group") for (const policy of c["/ip/ipsec/policy"].filter(item => reference(item.group))) seed("/ip/ipsec/policy", policy) + if (path === "/certificate") { + for (const child of c["/certificate"].filter(item => reference(item.ca))) seed("/certificate", child) + for (const dependentPath of ["/ip/ipsec/identity", "/ip/ipsec/peer"]) for (const consumer of c[dependentPath].filter(item => reference(item.certificate) || reference(item["remote-certificate"]))) seed(dependentPath, consumer) + } + } + return { commands, companions, target: { kind: "ipsec_user", id: body.name.trim() }, certName, serverEndpoint: String(serverCert?.["common-name"] ?? "").trim() || server.host, dns: sharedMc?.["static-dns"]?.trim() || undefined } +} + +/** 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 } = {}) { + const next = structuredClone(state) + const path = command.action === "sign" ? "/certificate" : command.action === "export" || command.action === "cleanup" ? "/file" : command.path + const rows = next.catalogs[path] + if (!rows) throw fail("Неизвестный каталог IPsec", "STATE_UNAVAILABLE") + if (command.action === "cleanup") { + const matches = rows.filter(row => row.name === command.name) + if (matches.length !== 1 || matches[0][".id"] !== options.assignedId) throw fail("Не подтверждён файл очистки", "VERIFICATION_FAILED") + rows.splice(rows.indexOf(matches[0]), 1) + } else if (command.action === "update") { + const row = rows.find(item => item[".id"] === command.id) + if (!row) throw fail("Mode-config отсутствует") + Object.assign(row, command.fields) + } else if (command.action === "sign") { + const row = rows.find(item => item.name === command.name), observed = options.observedRow + if (!row || !options.ownedCertificateIds?.has(row[".id"]) || !observed || observed[".id"] !== row[".id"] || !observed["invalid-after"]?.trim() || !observed.fingerprint?.trim()) throw fail("Не подтверждена подпись нового сертификата", "VERIFICATION_FAILED") + const generated = new Set(["ca", "ca-crl-host", "serial-number", "fingerprint", "invalid-before", "invalid-after", "expires-after", "akid", "skid", "issuer", "private-key", "issued", "status", "trusted"]) + for (const key of new Set([...Object.keys(row), ...Object.keys(observed)])) if (!generated.has(key) && observed[key] !== row[key]) throw fail("При подписи изменены исходные поля сертификата", "VERIFICATION_FAILED") + if (observed.ca !== command.fields.ca) throw fail("CA подписи не подтверждён", "VERIFICATION_FAILED") + 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") + 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)) + } + return next +} diff --git a/backend/src/services/ipsec-user-create.ts b/backend/src/services/ipsec-user-create.ts new file mode 100644 index 00000000..94d7631e --- /dev/null +++ b/backend/src/services/ipsec-user-create.ts @@ -0,0 +1,108 @@ +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 { readIpsecPeerDeleteState, ipsecPeerDeleteRows } from "./ipsec-peer-delete.js" +import { ipsecUserUpdateValue } from "./ipsec-user-update.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureIpsecSnapshot, mapClients, type IpsecServerState } from "./ipsec-live.js" +import { buildClientInstructions, buildSswanConfig } from "./ipsec-config.js" +import { ipsecUserCreateRequest, planIpsecUserCreate, projectIpsecUserCreate, type IpsecUserCreateState } from "./ipsec-user-create-model.js" + +export async function ipsecUserCreateProof(state: IpsecUserCreateState) { return (await privateFingerprinter("ipsec-user-create-v1"))(fingerprint(ipsecUserUpdateValue(state))) } +export async function readIpsecUserCreateState(server: ServerRow, authorize: () => Promise = async () => {}) { + const state = await readIpsecPeerDeleteState(server, authorize) + await authorize() + state.catalogs["/file"] = ipsecPeerDeleteRows(await MikrotikClient.fromServer(server).get("/file", 10000, undefined, 1048576)) + if (Buffer.byteLength(JSON.stringify(state)) > 1048576) throw new McpError("STATE_UNAVAILABLE", "Полное состояние создания IPsec превышает 1 МиБ", 502) + await authorize() + return { ...state, proof: await ipsecUserCreateProof(state) } +} +export class IpsecUserCreateExecutionError extends McpError { + constructor(code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string, readonly confirmedWrites: number, readonly completedCommands: number, readonly commandCount: number) { super(code, message, 502) } +} +export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: typeof captureAndAppendRevision; receiptId?: string; exportPrefix?: string; checkpoint?: (value: { receiptId: string; commandCount: number; completedCommands: number; afterProof: string }) => Promise; saveBundle?: (bundle: IpsecCertBundle, afterProof: string) => Promise> } = {}) { + const parsed = ipsecUserCreateRequest.parse(raw) + const body = { ...parsed, ...(parsed.authMethod === "certificate" && !parsed.passphrase?.trim() ? { passphrase: randomBytes(24).toString("base64url") } : {}) } + const receiptId = options.receiptId ?? randomUUID(), exportPrefix = options.exportPrefix ?? `mm-ipsec-${receiptId}` + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "ipsec_user", id: body.name.trim() }, [server.id], async () => { + const source = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился", 409) } + let state = await readIpsecUserCreateState(server, source) + if (expectedProof !== undefined && state.proof !== expectedProof) throw new McpError("STATE_CONFLICT", "IPsec или зависимости изменились после подготовки", 409) + const plan = planIpsecUserCreate(state, server, body, exportPrefix), client = MikrotikClient.fromServer(server), ownedCertificateIds = new Set() + let completedCommands = 0, confirmedWrites = 0, bundle: IpsecCertBundle | undefined, artifactReceipt: Record | 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) { + 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 { + if (command.action === "cleanup") { + const row = state.catalogs["/file"].find(item => item.name === command.name) + if (!row) throw new McpError("STATE_CONFLICT", "Файл экспорта отсутствует", 409) + assignedId = row[".id"] + response = await client.delete(`/file/${encodeURIComponent(assignedId)}`, 10000, fence) + } else if (command.action === "create") response = await client.put(command.path, command.fields, 30000, fence) + else if (command.action === "update") response = await client.patch(`${command.path}/${encodeURIComponent(command.id!)}`, command.fields, 15000, fence) + else response = await client.post(command.path, command.fields, command.action === "sign" ? 120000 : 60000, undefined, fence) + confirmedWrites++ + } catch (error) { + if (!dispatched && !completedCommands && error instanceof McpError) throw error + throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Создание IPsec остановлено; повтор native-команды запрещён, требуется сверка") + } + let after: IpsecUserCreateState + try { + after = await readIpsecUserCreateState(server, source) + // sign is asynchronous. Read-only polling never repeats the sign command. + if (command.action === "sign") { + 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 недоступен; повтор запрещён") } + try { + const path = command.action === "sign" ? "/certificate" : command.action === "export" ? "/file" : command.path + if (command.action === "create") { + const ack = response as Record | 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 }) + 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; повтор запрещён") } + state = after; completedCommands++ + try { + await source(); await options.checkpoint?.({ receiptId, commandCount: plan.commands.length, completedCommands, afterProof: state.proof }) + if (command.action === "export") { + const file = state.catalogs["/file"].find(row => row[".id"] === assignedId)!, size = Number(file.size) + if (!Number.isSafeInteger(size) || size <= 0 || size > MAX_OWNED_FILE_BYTES) throw Error("private export exceeds the exact-ID /file/get limit of 61438 bytes") + await source() + const content = await client.downloadOwnedFile({ name: file.name, id: assignedId!, size }, { authorize: source }) + try { + if (content.length !== size) throw Error("incomplete private export") + await source() + if ((await readIpsecUserCreateState(server, source)).proof !== state.proof) throw Error("export changed") + const p12B64 = content.toString("base64"), passphrase = body.passphrase! + 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 или приватный артефакт не сохранён; повтор запрещён") } + } + 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 + try { await source(); historyRecorded = (await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "apply", capture: () => captureIpsecSnapshot(server, reader) })) !== null } catch { /* Never replay native writes for history. */ } + if (options.saveBundle && bundle && historyRecorded) { try { await source(); artifactReceipt = await options.saveBundle(bundle, state.proof); bundle = undefined } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "IPsec применён, но приватный артефакт не сохранён; повтор запрещён") } } + const c = state.catalogs + const live = { server, client, peers: c["/ip/ipsec/peer"], identities: c["/ip/ipsec/identity"], modeConfigs: c["/ip/ipsec/mode-config"], pools: c["/ip/pool"], policies: c["/ip/ipsec/policy"], nat: c["/ip/firewall/nat"], active: c["/ip/ipsec/active-peers"], certs: c["/certificate"] } as IpsecServerState + const created = mapClients(live).find(row => row.name === body.name.trim()) ?? null + return { ok: true, verified: true, receiptId, clientId: body.name.trim(), commandCount: plan.commands.length, completedCommands, confirmedWrites, afterProof: state.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", client: created, ...(bundle ? { bundle } : {}), ...(artifactReceipt ?? {}), automaticReplay: false, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/ipsec-user-delete.test.ts b/backend/src/services/ipsec-user-delete.test.ts new file mode 100644 index 00000000..bea64654 --- /dev/null +++ b/backend/src/services/ipsec-user-delete.test.ts @@ -0,0 +1,35 @@ +import assert from "node:assert/strict" +import { planIpsecUserDelete, projectIpsecUserDelete } from "./ipsec-user-delete.js" +import { ipsecPeerDeletePaths, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +const server = { id: 1, enabled: true, host: "fixture", name: "fixture" } as ServerRow +const base = (): IpsecPeerDeleteState => ({ source: "private", proof: "private", protections: [], catalogs: { ...Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", disabled: "true" }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "digital-signature", "remote-certificate": "ipsec-user-Alice", "mode-config": "mc-ipsec-alice", comment: "MikrotikManager:ipsec user=Alice", disabled: "true" }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "mc-ipsec-alice", address: "198.18.0.1" }], + "/certificate": [{ ".id": "*c", name: "ipsec-user-Alice", "common-name": "Alice", "key-usage": "tls-client", ca: "ca", opaque: "secret" }, { ".id": "*a", name: "ca", "key-usage": "key-cert-sign", authority: "true" }] +} }) +const state = base(), commands = planIpsecUserDelete(state, server, "identity:*i") +assert.deepEqual(commands, [{ path: "/ip/ipsec/identity", id: "*i" }, { path: "/ip/ipsec/mode-config", id: "*m" }, { path: "/certificate", id: "*c" }]) +assert.equal(planIpsecUserDelete(state, server, "cert:*c").length, 3) +assert.equal(planIpsecUserDelete(state, server, "identity:*i", false).length, 2) +let projected = state; for (const command of commands) projected = projectIpsecUserDelete(projected, command) +assert.deepEqual(projected.catalogs["/certificate"], [state.catalogs["/certificate"][1]]) +assert.equal(state.catalogs["/ip/ipsec/identity"].length, 1) +const shared = base(); shared.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", "mode-config": "mc-ipsec-alice", "remote-certificate": "ipsec-user-Alice" }) +assert.throws(() => planIpsecUserDelete(shared, server, "identity:*i"), /другой identity/) +assert.deepEqual(planIpsecUserDelete(shared, server, "identity:*i", false), [{ path: "/ip/ipsec/identity", id: "*i" }]) +const crl = base(); crl.catalogs["/certificate"][0].issued = "true"; crl.catalogs["/certificate"][1].crl = "true" +assert.throws(() => planIpsecUserDelete(crl, server, "identity:*i"), /CRL/) +crl.catalogs["/certificate"][1].crl="yes";crl.catalogs["/certificate"][0].issued="yes" +assert.throws(()=>planIpsecUserDelete(crl,server,"identity:*i"),/CRL/) +const children = base(); children.catalogs["/certificate"].push({ ".id": "*d", name: "descendant", ca: "ipsec-user-Alice" }) +assert.throws(() => planIpsecUserDelete(children, server, "identity:*i"), /цепочку/) +const ambiguous = base(); ambiguous.catalogs["/certificate"][0][".id"] = "*i" +assert.throws(() => planIpsecUserDelete(ambiguous, server, "*i"), /Неоднозначный/) +assert.equal(planIpsecUserDelete(ambiguous, server, "identity:*i").length, 3) +const certOnly = base(); certOnly.catalogs["/ip/ipsec/identity"] = [] +assert.deepEqual(planIpsecUserDelete(certOnly, server, "cert:*c"), [{ path: "/certificate", id: "*c" }]) +assert.deepEqual(planIpsecUserDelete(certOnly, server, "cert:*c", false), []) +const system = base(); system.catalogs["/ip/ipsec/identity"][0].dynamic = "true"; assert.throws(() => planIpsecUserDelete(system, server, "identity:*i"), /Системный/) +console.log("✓ IPsec user delete planner: full optional certificate cascade, exact namespace ambiguity, shared refs/mode-config, CA descendants/CRL, cert-only no-op, immutable projection, native/system guards") diff --git a/backend/src/services/ipsec-user-delete.ts b/backend/src/services/ipsec-user-delete.ts new file mode 100644 index 00000000..1c917fa9 --- /dev/null +++ b/backend/src/services/ipsec-user-delete.ts @@ -0,0 +1,105 @@ +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError } from "../modules/mcp/security.js" +import { MikrotikClient } from "./mikrotik.js" +import { readIpsecPeerDeleteState, ipsecPeerDeleteProof, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { captureIpsecSnapshot, resolveIpsecClientTarget, type IpsecServerState } from "./ipsec-live.js" +import { isManagedIpsecModeConfig, selectIke2Peers, resolveIke2CaName, resolveIke2ServerCert, IPSEC_COMMON_NAME } from "./ipsec-config.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { ipsecCertificateDeleteTarget } from "./ipsec-certificate-delete.js" + +type State = IpsecPeerDeleteState +export interface IpsecUserDeleteCommand { path: "/ip/ipsec/identity" | "/ip/ipsec/mode-config" | "/certificate"; id: string } +export interface IpsecUserDeleteCheckpoint { receiptId: string; commandCount: number; completedCommands: number; afterProof: string } +const fail = (code: string, message: string, status = ["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(code) ? 502 : code === "INVALID_INPUT" ? 400 : 409) => new McpError(code, message, status) +export class IpsecUserDeleteExecutionError extends McpError { + constructor(code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string, public readonly confirmedWrites: number, public readonly completedCommands: number, public readonly commandCount: number) { super(code, message, 502) } +} +export const readIpsecUserDeleteState = readIpsecPeerDeleteState +export function ipsecUserDeleteLiveState(state: State, server: ServerRow): IpsecServerState { + const c = state.catalogs + return { server, client: MikrotikClient.fromServer(server), peers: c["/ip/ipsec/peer"], identities: c["/ip/ipsec/identity"], modeConfigs: c["/ip/ipsec/mode-config"], pools: c["/ip/pool"], policies: c["/ip/ipsec/policy"], nat: c["/ip/firewall/nat"], active: c["/ip/ipsec/active-peers"], certs: c["/certificate"] } +} +export function planIpsecUserDelete(state: State, server: ServerRow, clientId: string, removeCertificate = true): IpsecUserDeleteCommand[] { + if (!clientId || clientId.length > 200 || /[\u0000-\u001f\u007f]/.test(clientId)) throw fail("INVALID_INPUT", "Некорректный ID клиента IPsec") + const live = ipsecUserDeleteLiveState(state, server), target = resolveIpsecClientTarget(live, clientId) + if (target.ambiguous) throw fail("STATE_CONFLICT", "Неоднозначный ID клиента. Обновите список IPsec") + const identity = target.identity ?? (target.certRow ? live.identities.find(row => row["remote-certificate"] === target.certRow?.name) : undefined) + const certRow = identity ? undefined : target.certRow + if (!identity && !certRow) throw fail("NOT_FOUND", "Клиент не найден", 404) + const personal = (identity?.["mode-config"] ?? "").trim(), shared = (live.modeConfigs.find(row => (row.name ?? "").trim() === IPSEC_COMMON_NAME)?.name ?? "").trim() + const remoteCert = identity ? (identity["remote-certificate"] ?? "").trim() : String(certRow?.name ?? "").trim() + const certs = live.certs.filter(row => row.name === remoteCert), cert = certs[0] + if (removeCertificate && remoteCert) { + if (certs.length > 1) throw fail("STATE_CONFLICT", "Неоднозначное имя сертификата") + if (live.identities.some(row => row[".id"] !== identity?.[".id"] && [(row["remote-certificate"] ?? "").trim(), (row.certificate ?? "").trim()].includes(remoteCert)) || live.peers.some(row => (row.certificate ?? "").trim() === remoteCert)) throw fail("DEPENDENCY_CONFLICT", "Сертификат используется другой identity или peer. Удалите только выбранную identity, сохранив сертификат") + if (cert) ipsecCertificateDeleteTarget(state, remoteCert, true) + } + const commands: IpsecUserDeleteCommand[] = [] + const add = (path: IpsecUserDeleteCommand["path"], id?: string) => { + if (!id || !/^\*[a-z0-9]+$/i.test(id)) throw fail("STATE_CONFLICT", "Не подтверждён native ID удаления IPsec") + const row = state.catalogs[path].find(row => row[".id"] === id)! + for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) throw fail("PROTECTED_OBJECT", "Системный или неподтверждённый объект IPsec нельзя удалить") + commands.push({ path, id }) + } + if (identity) { + add("/ip/ipsec/identity", identity[".id"]) + if (personal && personal !== shared) { + const matches = live.modeConfigs.filter(row => row.name === personal) + if (matches.length > 1) throw fail("STATE_CONFLICT", "Неоднозначное имя mode-config") + if (matches[0] && isManagedIpsecModeConfig(matches[0]) && !live.identities.some(row => row[".id"] !== identity[".id"] && row["mode-config"] === personal)) add("/ip/ipsec/mode-config", matches[0][".id"]) + } + } + if (removeCertificate && cert) { + const peers = selectIke2Peers(live.peers), serverCert = resolveIke2ServerCert(live.certs, peers, live.identities), ca = resolveIke2CaName(live.certs, serverCert) + if (remoteCert !== ca && remoteCert !== String(serverCert?.name ?? "").trim()) add("/certificate", cert[".id"]) + } + return commands +} +export function projectIpsecUserDelete(state: State, command: IpsecUserDeleteCommand): State { + const next = structuredClone(state), rows = next.catalogs[command.path] + if (!rows.some(row => row[".id"] === command.id)) throw fail("STATE_CONFLICT", "Native объект запланированного удаления отсутствует") + next.catalogs[command.path] = rows.filter(row => row[".id"] !== command.id) + return next +} +export async function ipsecUserDeleteStageProofs(state: State, commands: IpsecUserDeleteCommand[]) { + const proofs = [state.proof] + for (const command of commands) { state = projectIpsecUserDelete(state, command); proofs.push(await ipsecPeerDeleteProof(state)) } + return proofs +} +export async function deleteIpsecUserGuarded(server: ServerRow, clientId: string, removeCertificate = true, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: typeof captureAndAppendRevision; receiptId?: string; checkpoint?: (value: IpsecUserDeleteCheckpoint) => Promise } = {}) { + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "ipsec_user", id: clientId }, [server.id], async () => { + let state = await readIpsecUserDeleteState(server, authorize) + if (expectedProof && state.proof !== expectedProof) throw fail("STATE_CONFLICT", "IPsec или его зависимости изменились после подготовки") + const commands = planIpsecUserDelete(state, server, clientId, removeCertificate), proofs = await ipsecUserDeleteStageProofs(state, commands), client = MikrotikClient.fromServer(server) + let completedCommands = 0, confirmedWrites = 0 + const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string) => new IpsecUserDeleteExecutionError(code, message, confirmedWrites, completedCommands, commands.length) + for (const command of commands) { + let dispatched = false + try { + await client.delete(command.path + "/" + encodeURIComponent(command.id), 10000, async () => { + const fresh = await readIpsecUserDeleteState(server, authorize) + if (fresh.proof !== state.proof) throw fail("STATE_CONFLICT", "IPsec изменился после durable intent; DELETE не отправлен") + await authorize(); dispatched = true + }) + confirmedWrites++ + } catch (error) { + if (!dispatched && error instanceof McpError && !completedCommands) throw error + throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Удаление IPsec остановлено; исход команды не подтверждён. Повтор запрещён, требуется сверка") + } + try { state = await readIpsecUserDeleteState(server, authorize) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "DELETE отправлен, но read-back недоступен; повтор запрещён") } + if (state.proof !== proofs[completedCommands + 1]) throw stopped("VERIFICATION_FAILED", "Не подтверждено удаление или сохранность остальных объектов IPsec; повтор запрещён") + completedCommands++ + try { await authorize(); await options.checkpoint?.({ receiptId: options.receiptId ?? "", commandCount: commands.length, completedCommands, afterProof: state.proof }) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Удаление проверено, но checkpoint не сохранён; повтор запрещён") } + } + const historySource = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw fail("STATE_CONFLICT", "Источник истории IPsec изменился") } + const reader = { get: async (path: string) => { await historySource(); if (!Object.hasOwn(state.catalogs, path)) throw fail("STATE_CONFLICT", "Неизвестный каталог истории IPsec"); return structuredClone(state.catalogs[path]) } } as unknown as MikrotikClient + let historyRecorded = false + try { await historySource(); historyRecorded = await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "apply", capture: () => captureIpsecSnapshot(server, reader) }) !== null } catch { /* Never replay acknowledged native commands for history. */ } + return { ok: true, verified: true, clientId, removeCertificate, receiptId: options.receiptId ?? null, commandCount: commands.length, completedCommands, afterProof: state.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", forwardingVerified: false, automaticReplay: false } + }))) +} diff --git a/backend/src/services/ipsec-user-update-model.test.ts b/backend/src/services/ipsec-user-update-model.test.ts new file mode 100644 index 00000000..31a41bf8 --- /dev/null +++ b/backend/src/services/ipsec-user-update-model.test.ts @@ -0,0 +1,92 @@ +import assert from "node:assert/strict" +import { planIpsecUserUpdate as plan, projectIpsecUserUpdate as project } from "./ipsec-user-update-model.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +const server = { id: 1, name: "fixture", host: "fixture", enabled: true } as ServerRow +const paths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] +const base = (): IpsecPeerDeleteState => ({ source: "source", proof: "proof", protections: [{ object_ref: { kind: "server", id: "1" } }], catalogs: { ...Object.fromEntries(paths.map(path => [path, []])), + "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", passive: "yes", opaque: "peer" }], + "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:alice", "mode-config": "mc-ipsec-alice", comment: "MikrotikManager:ipsec user=Alice", disabled: "true", opaque: "identity" }], + "/ip/ipsec/mode-config": [{ ".id": "*m", name: "mc-ipsec-alice", address: "192.0.2.1", opaque: "old" }, { ".id": "*s", name: "ipsec-vpn", "address-pool": "pool", opaque: "shared" }], + "/ip/pool": [{ ".id": "*l", name: "pool", ranges: "192.0.2.1-192.0.2.20" }], + "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign", authority: "true" }, { ".id": "*c", name: "ipsec-user-Alice", "common-name": "Alice", "key-usage": "tls-client", ca: "ca", opaque: "private" }, { ".id": "*v", name: "ipsec-server", "key-usage": "tls-server", ca: "ca" }], +} }) +const s = base(), before = structuredClone(s) +assert.deepEqual(plan(s, server, "identity:*i", {}).commands, []) +const full = plan(s, server, "identity:*i", { name: "Bob", staticIp: "192.0.2.9", psk: "new-secret", remoteId: "bob", disabled: false }) +assert.deepEqual(full.commands.map(c => [c.action, c.path]), [["update", "/ip/ipsec/identity"], ["create", "/ip/ipsec/mode-config"], ["update", "/ip/ipsec/identity"], ["delete", "/ip/ipsec/mode-config"], ["update", "/ip/ipsec/identity"]]) +let next = s +for (const [_index, command] of full.commands.entries()) next = project(next, command, command.action === "create" ? { assignedId: "*new", createdRow: { ".id": "*new", ...command.fields, responder: "true", "native-default": "retained" } } : {}) +assert.deepEqual(s, before) +assert.deepEqual(next.protections, s.protections) +assert.deepEqual(next.catalogs["/ip/ipsec/identity"][0], { ...s.catalogs["/ip/ipsec/identity"][0], comment: "MikrotikManager:ipsec user=Bob", "mode-config": "mc-ipsec-bob", secret: "new-secret", "remote-id": "key-id:bob", disabled: "no" }) +assert.deepEqual(next.catalogs["/ip/ipsec/mode-config"].map(row => row[".id"]), ["*s", "*new"]) +assert.equal(next.catalogs["/ip/ipsec/mode-config"][1]["native-default"], "retained") +for (const path of paths.filter(p => !["/ip/ipsec/identity", "/ip/ipsec/mode-config"].includes(p))) assert.deepEqual(next.catalogs[path], s.catalogs[path]) +assert.throws(() => plan(s, server, "identity:*i", { name: "Bob", staticIp: "" }), /IPv4/) +assert.throws(() => plan(s, server, "identity:*i", { name: "Bob", staticIp: " " }), /IPv4/) +assert.throws(() => project(s, full.commands[1], { createdRow: { ".id": "*new", ...full.commands[1].fields, address: "192.0.2.99" } }), /не подтверждены/) +assert.throws(() => project(s, full.commands[1], { assignedId: "*m", createdRow: { ".id": "*m", ...full.commands[1].fields } }), /Не подтверждён/) +const pooled = plan(s, server, "identity:*i", { staticIp: null }) +assert.deepEqual(pooled.commands.map(c => c.action), ["update", "delete"]) +assert.equal(pooled.commands[0].fields["mode-config"], "ipsec-vpn") +const noShared = base(); noShared.catalogs["/ip/ipsec/mode-config"].pop() +assert.deepEqual(plan(noShared, server, "identity:*i", { staticIp: null }).commands, []) +const shared = base(); shared.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", comment: "Other", "mode-config": "mc-ipsec-alice" }) +assert.equal(plan(shared, server, "identity:*i", { staticIp: null }).commands.length, 1) +const foreign = base(); foreign.catalogs["/ip/ipsec/mode-config"].push({ ".id": "*f", name: "mc-ipsec-bob", address: "192.0.2.8", "address-pool": "pool", "static-dns": "192.0.2.53", opaque: "foreign" }) +foreign.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", comment: "Other", "mode-config": "mc-ipsec-bob" }) +const foreignPlan = plan(foreign, server, "identity:*i", { name: "Bob", staticIp: "192.0.2.9" }) +assert.equal(foreignPlan.commands[1].action, "update") +assert.equal(foreignPlan.commands[1].id, "*f") +const foreignProjected = project(foreign, foreignPlan.commands[1]) +assert.deepEqual(foreignProjected.catalogs["/ip/ipsec/mode-config"][2], { ...foreign.catalogs["/ip/ipsec/mode-config"][2], ...foreignPlan.commands[1].fields }) +assert(foreignPlan.companions.some(row => row.kind === "ipsec_identity" && row.id === "*j")) +assert(foreignPlan.companions.some(row => row.kind === "ip_pool" && row.id === "*l")) +const unmanaged = base(); unmanaged.catalogs["/ip/ipsec/identity"][0].comment = "Alice" +assert.equal(plan(unmanaged, server, "identity:*i", { name: "Bob" }).commands[0].fields.comment, "Bob") +assert.equal(plan(s, server, "identity:*i", { name: " " }).commands[0].fields.comment, "MikrotikManager:ipsec user=") +assert.deepEqual(plan(s, server, "identity:*i", { name: "Alice" }).commands, []) +const collision = base(); collision.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:bob", comment: "MikrotikManager:ipsec user=Bob" }) +assert.throws(() => plan(collision, server, "identity:*i", { name: "Bob" }), /Имя/) +assert.throws(() => plan(collision, server, "identity:*i", { remoteId: "bob" }), /Remote ID/) +const certOnly = base(); certOnly.catalogs["/ip/ipsec/identity"] = [{ ".id": "*listener", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "mode-config": "ipsec-vpn" }] +const renameCert = plan(certOnly, server, "cert:*c", { name: "Bob", psk: "ignored-psk", remoteId: "ignored", disabled: true, staticIp: null }) +assert.equal(renameCert.commands.length, 1) +assert.equal(renameCert.commands[0].action, "create") +assert.equal(renameCert.commands[0].fields["remote-certificate"], "ipsec-user-Alice") +assert.equal(renameCert.commands[0].fields.comment, "MikrotikManager:ipsec user=Bob") +assert.equal(renameCert.commands[0].fields["mode-config"], "ipsec-vpn") +assert.equal(renameCert.commands[0].fields.secret, undefined) +assert.deepEqual(renameCert.ignoredFields, ["psk", "remoteId", "disabled", "staticIp"]) +const staticCert = plan(certOnly, server, "cert:*c", { name: "Bob", staticIp: "192.0.2.9", disabled: false }) +assert.deepEqual(staticCert.commands.map(c => c.action), ["create", "create"]) +assert.equal(staticCert.commands[1].fields["mode-config"], "mc-ipsec-bob") +assert.equal(staticCert.commands[1].fields.disabled, undefined) +const missingServerCert = structuredClone(certOnly) +missingServerCert.catalogs["/ip/ipsec/identity"] = [] +missingServerCert.catalogs["/certificate"] = missingServerCert.catalogs["/certificate"].filter(row => row.name !== "ipsec-server") +assert.throws(() => plan(missingServerCert, server, "cert:*c", { staticIp: "192.0.2.9" }), /сертификат/) +const fallbackServerCert = structuredClone(missingServerCert) +fallbackServerCert.catalogs["/certificate"].push({ ".id": "*fallback", name: "ipsec-server" }) +assert.equal(plan(fallbackServerCert, server, "cert:*c", { staticIp: "192.0.2.9" }).commands[1].fields.certificate, "ipsec-server") +const paddedServerCert = structuredClone(fallbackServerCert) +paddedServerCert.catalogs["/certificate"].find(row => row[".id"] === "*fallback")!.name = " ipsec-server " +assert.throws(() => plan(paddedServerCert, server, "cert:*c", { staticIp: "192.0.2.9" }), /точный серверный/) +const boolNamedCommand = { ...renameCert.commands[0], fields: { ...renameCert.commands[0].fields, comment: "yes" } } +assert.throws(() => project(certOnly, boolNamedCommand, { createdRow: { ".id": "*new", ...boolNamedCommand.fields, comment: "true" } }), /не подтверждены/) +for (const flag of ["dynamic", "builtin", "default"]) for (const value of ["true", "yes", "unknown", ""]) { + assert.throws(() => project(certOnly, renameCert.commands[0], { createdRow: { ".id": "*new", ...renameCert.commands[0].fields, [flag]: value } }), /флаг/) +} +const acknowledged = project(certOnly, renameCert.commands[0], { createdRow: { ".id": "*new", ...renameCert.commands[0].fields, dynamic: "false", builtin: "no", default: "false", opaque: "read-only native defaults" } }) +assert.equal(acknowledged.catalogs["/ip/ipsec/identity"].at(-1)!.opaque, "read-only native defaults") +assert.deepEqual(plan(certOnly, server, "cert:*c", { disabled: false }).commands, []) +const ambiguous = base(); ambiguous.catalogs["/ip/ipsec/mode-config"].push({ ".id": "*dup", name: "mc-ipsec-alice" }) +assert.throws(() => plan(ambiguous, server, "identity:*i", {}), /Неоднозначное/) +const missingCatalog = base(); delete missingCatalog.catalogs["/ip/ipsec/profile"] +assert.throws(() => plan(missingCatalog, server, "identity:*i", {}), /Неполный/) +const certAlias = base(); certAlias.catalogs["/certificate"].push({ ".id": "*d", name: "*c", "key-usage": "tls-client", ca: "ca" }) +assert.throws(() => plan(certAlias, server, "cert:*c", {}), /Неоднозначный/) +const protectedState = base(); protectedState.catalogs["/ip/ipsec/identity"][0].dynamic = "unknown" +assert.throws(() => plan(protectedState, server, "identity:*i", { disabled: true }), /Системный/) +console.log("✓ IPsec user update planner: full PATCH branches, cert promotion, no-op/ignored fields, upsert consumers, managed cleanup, immutable raw projection, ACK defaults and ambiguity guards") diff --git a/backend/src/services/ipsec-user-update-model.ts b/backend/src/services/ipsec-user-update-model.ts new file mode 100644 index 00000000..3476047f --- /dev/null +++ b/backend/src/services/ipsec-user-update-model.ts @@ -0,0 +1,159 @@ +import { isIP } from "node:net" +import { ipsecUserPatchSchema } from "@mmapp/contracts/ipsec" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { McpError } from "../modules/mcp/security.js" +import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { mapClients, resolveIpsecClientTarget, type IpsecServerState } from "./ipsec-live.js" +import { IPSEC_COMMON_NAME, IPSEC_SERVER_CERT, isIpsecManagedComment, isManagedIpsecModeConfig, ipsecUserComment, normalizeIpsecRemoteId, parseIpsecUserComment, resolveIke2ServerCert, selectIke2Peers, selectSharedIke2Identity, userModeConfigName } from "./ipsec-config.js" +import { identityRosBody } from "./ipsec-ros.js" +import { observedBoolean } from "./ospf-observed-values.js" + +type Row = Record +type State = IpsecPeerDeleteState +export interface IpsecUserUpdateCommand { action: "create" | "update" | "delete"; path: "/ip/ipsec/identity" | "/ip/ipsec/mode-config"; id?: string; fields: Row } +export interface IpsecUserUpdateObject { kind: string; id: string } +const paths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] +const fail = (code: string, message: string) => new McpError(code, message, code === "INVALID_INPUT" ? 400 : code === "NOT_FOUND" ? 404 : code === "STATE_UNAVAILABLE" ? 502 : 409) +const exactId = (id: unknown): id is string => typeof id === "string" && /^\*[a-z0-9]+$/i.test(id) && id.length <= 100 +function validateState(state: State) { + if (!state?.source || !Array.isArray(state.protections) || !state.catalogs) throw fail("STATE_UNAVAILABLE", "Неполное состояние IPsec") + for (const path of paths) { + const rows = state.catalogs[path] + if (!Array.isArray(rows) || rows.length > 2000 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row) || !exactId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(rows.map(row => row[".id"])).size !== rows.length) throw fail("STATE_UNAVAILABLE", "Неполный или неоднозначный native-каталог IPsec") + const names = rows.map(row => row.name?.trim()).filter(Boolean) + if (path !== "/ip/ipsec/active-peers" && new Set(names).size !== names.length) throw fail("STATE_CONFLICT", "Неоднозначное имя объекта IPsec") + } +} +function mutable(row: Row) { + for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) throw fail("PROTECTED_OBJECT", "Системный или неподтверждённый объект IPsec нельзя изменять") +} +/** An inert view: client is never constructed or used by the pure mapping functions. */ +function liveState(state: State, server: ServerRow): IpsecServerState { + const c = state.catalogs + return { server, client: undefined as unknown as IpsecServerState["client"], peers: c["/ip/ipsec/peer"], identities: c["/ip/ipsec/identity"], modeConfigs: c["/ip/ipsec/mode-config"], pools: c["/ip/pool"], policies: c["/ip/ipsec/policy"], nat: c["/ip/firewall/nat"], active: c["/ip/ipsec/active-peers"], certs: c["/certificate"] } +} +const taken = (rows: Array<{ name?: string; address?: string; "address-prefix"?: string }>) => rows.filter(row => (row.name ?? "").startsWith("mc-ipsec-")).map(row => (row.address ?? row["address-prefix"] ?? "").replace(/\/\d+$/, "").trim()).filter(Boolean) + +/** Plans every legacy PATCH branch before the first effect, including certificate promotion. */ +export function planIpsecUserUpdate(state: State, server: ServerRow, clientId: string, raw: unknown) { + const parsed = ipsecUserPatchSchema.safeParse(raw) + if (!parsed.success) throw fail("INVALID_INPUT", "Некорректные поля клиента IPsec") + const d = parsed.data + if (typeof d.staticIp === "string" && isIP(d.staticIp.trim()) !== 4) throw fail("INVALID_INPUT", "Некорректный статический IPv4 адрес") + if (!clientId || clientId.length > 200 || /[\x00-\x1f\x7f]/.test(clientId)) throw fail("INVALID_INPUT", "Некорректный ID клиента IPsec") + validateState(state) + const certToken = clientId.startsWith("cert:") ? clientId.slice(5) : clientId.startsWith("identity:") ? undefined : clientId + if (certToken && state.catalogs["/certificate"].filter(row => row[".id"] === certToken || row.name === certToken).length > 1) throw fail("STATE_CONFLICT", "Неоднозначный ID или имя сертификата клиента") + const live = liveState(state, server), resolved = resolveIpsecClientTarget(live, clientId) + if (resolved.ambiguous) throw fail("STATE_CONFLICT", "Неоднозначный ID клиента. Обновите список IPsec") + let { identity, certRow } = resolved + if (certRow) { + identity = live.identities.find(row => row["remote-certificate"] === certRow?.name) + if (identity) certRow = undefined + } + if (!identity && !certRow) throw fail("NOT_FOUND", "Клиент не найден") + const peers = selectIke2Peers(live.peers) + const serverCert = resolveIke2ServerCert(live.certs, peers, live.identities) + const oldName = identity ? (parseIpsecUserComment(identity.comment) ?? identity.comment ?? "") : String(certRow?.["common-name"] ?? certRow?.name ?? "").trim() + const newName = d.name?.trim() || oldName, sharedMc = live.modeConfigs.find(row => (row.name ?? "").trim() === IPSEC_COMMON_NAME), sharedName = (sharedMc?.name ?? "").trim() + const remoteId = d.remoteId === undefined ? undefined : normalizeIpsecRemoteId(d.remoteId) + if (d.name && newName !== oldName && mapClients(live).some(row => row.name === newName && (identity ? row.rosId !== `identity:${identity[".id"]}` : row.certName !== certRow?.name))) throw fail("STATE_CONFLICT", "Имя уже используется другим клиентом") + if (identity && remoteId !== undefined && live.identities.some(row => row[".id"] !== identity[".id"] && row.peer === identity.peer && row["auth-method"] === "pre-shared-key" && row["remote-id"] === remoteId)) throw fail("STATE_CONFLICT", "Remote ID уже используется клиентом этого peer") + const currentMc = (identity?.["mode-config"] ?? "").trim() + if (d.staticIp && taken(live.modeConfigs.filter(row => !currentMc || row.name !== currentMc)).includes(d.staticIp.trim())) throw fail("STATE_CONFLICT", "IP уже назначен другому клиенту") + const commands: IpsecUserUpdateCommand[] = [], seeds: Array<{ path: string; row: Row }> = [] + const seed = (path: string, row: unknown) => { if (row) seeds.push({ path, row: row as Row }) } + seed(identity ? "/ip/ipsec/identity" : "/certificate", identity ?? certRow) + const add = (action: IpsecUserUpdateCommand["action"], path: IpsecUserUpdateCommand["path"], fields: Row, row?: { ".id"?: string }) => { + if (row) { mutable(row as Row); seed(path, row) } + commands.push({ action, path, ...(row ? { id: row[".id"]! } : {}), fields }) + } + const patchIdentity = (fields: Row) => add("update", "/ip/ipsec/identity", fields, identity) + const upsertMc = (name: string, ip: string) => { + const matches = state.catalogs["/ip/ipsec/mode-config"].filter(row => row.name?.trim() === name) + if (matches[0] && matches[0].name !== name) throw fail("STATE_CONFLICT", "Не подтверждено точное имя mode-config") + add(matches.length ? "update" : "create", "/ip/ipsec/mode-config", { name, address: ip, responder: "yes", "address-prefix-length": "32" }, matches[0]) + } + const deletePersonal = (name: string, projectedMc: string) => { + if (!name || name === sharedName) return + const row = state.catalogs["/ip/ipsec/mode-config"].find(row => row.name === name) + seed("/ip/ipsec/mode-config", row) + if (row && isManagedIpsecModeConfig(row) && !live.identities.some(other => other[".id"] === identity?.[".id"] ? projectedMc === name : other["mode-config"] === name)) add("delete", "/ip/ipsec/mode-config", {}, row) + } + const createCertificateIdentity = (modeConfig: string, requireCert: boolean) => { + const peer = peers.find(row => isIpsecManagedComment(row.comment)) ?? peers[0] + if (!peer || (requireCert && !serverCert)) throw fail("STATE_CONFLICT", "Нет IKEv2 peer или серверного сертификата") + const certificateName = serverCert?.name ?? (requireCert ? undefined : IPSEC_SERVER_CERT) + const certificateMatches = state.catalogs["/certificate"].filter(row => row.name === certificateName) + if (!certificateName || certificateName !== certificateName.trim() || certificateMatches.length !== 1) throw fail("STATE_CONFLICT", "Не подтверждён точный серверный сертификат IKEv2") + const shared = selectSharedIke2Identity(live.identities, [(peer.name ?? "").trim()]) + seed("/ip/ipsec/peer", peer); seed("/certificate", certificateMatches[0]); seed("/ip/ipsec/identity", shared) + seed("/ip/ipsec/mode-config", state.catalogs["/ip/ipsec/mode-config"].find(row => row.name === modeConfig)) + add("create", "/ip/ipsec/identity", identityRosBody({ peerName: requireCert ? (peer.name ?? "") : (peer.name ?? "").trim(), modeConfig, comment: ipsecUserComment(requireCert ? d.name! : newName), authMethod: "certificate", certificate: certificateName, remoteCertificate: requireCert ? certRow!.name : String(certRow!.name ?? "").trim(), policyTemplateGroup: shared?.["policy-template-group"] })) + } + if (d.name && d.name !== oldName) { + if (identity) patchIdentity({ comment: isIpsecManagedComment(identity.comment) ? ipsecUserComment(d.name) : d.name.trim() }) + if (certRow && !d.staticIp) createCertificateIdentity(sharedName, true) + } + if (identity && d.staticIp !== undefined) { + seed("/ip/ipsec/mode-config", sharedMc) + if (d.staticIp == null) { + if (sharedName) patchIdentity({ "mode-config": sharedName }) + deletePersonal(currentMc, sharedName || currentMc) + } else { + const name = userModeConfigName(newName) + upsertMc(name, d.staticIp.trim()); patchIdentity({ "mode-config": name }) + if (currentMc !== name) deletePersonal(currentMc, name) + } + } else if (!identity && certRow && d.staticIp) { + // Validate peer before adding either command; the returned plan is always complete. + if (!peers.length) throw fail("STATE_CONFLICT", "На роутере нет IKEv2 peer для привязки клиента") + const name = userModeConfigName(newName) + upsertMc(name, d.staticIp.trim()); createCertificateIdentity(name, false) + } + if (identity) { + const fields: Row = {} + if (d.psk) fields.secret = d.psk + if (remoteId !== undefined) fields["remote-id"] = remoteId + if (d.disabled !== undefined) fields.disabled = d.disabled ? "yes" : "no" + if (Object.keys(fields).length) patchIdentity(fields) + } + const kinds: Record = { "/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" } + const references: Record = { 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" } + const companions: IpsecUserUpdateObject[] = [], seen = new Set() + for (let index = 0; index < seeds.length; index++) { + const { path, row } = seeds[index], key = `${path}:${row[".id"]}` + if (seen.has(key)) continue + seen.add(key); companions.push({ kind: kinds[path] ?? path, id: row[".id"] }) + for (const [field, refPath] of Object.entries(references)) { + const ref = row[field] + if (!ref || ["none", "auto", "ignore"].includes(ref)) continue + const matches = state.catalogs[refPath].filter(item => item.name === ref || item[".id"] === ref) + if (matches.length !== 1) throw fail("STATE_CONFLICT", "Не подтверждена однозначная зависимость IPsec") + seed(refPath, matches[0]) + } + if (path === "/ip/ipsec/mode-config") for (const consumer of state.catalogs["/ip/ipsec/identity"].filter(item => item["mode-config"] === row.name)) seed("/ip/ipsec/identity", consumer) + } + return { commands, target: { kind: "ipsec_user", id: clientId }, companions, objects: companions, ignoredFields: identity ? [] : ["psk", "remoteId", "disabled", ...(d.staticIp === null ? ["staticIp"] : [])].filter(key => Object.hasOwn(d, key)), oldName, newName } +} + +/** Preserve all prior raw fields and ordering. Only a newly acknowledged row admits defaults. */ +export function projectIpsecUserUpdate(state: State, command: IpsecUserUpdateCommand, options: { assignedId?: string; createdRow?: Row } = {}): State { + const next = structuredClone(state), rows = next.catalogs[command.path] + if (!rows) throw fail("STATE_UNAVAILABLE", "Неизвестный каталог IPsec") + if (command.action === "create") { + const row = options.createdRow, id = options.assignedId ?? row?.[".id"] + if (!row || !exactId(id) || row[".id"] !== id || rows.some(existing => existing[".id"] === id) || Object.values(row).some(value => typeof value !== "string")) throw fail("VERIFICATION_FAILED", "Не подтверждён созданный native объект IPsec") + for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) throw fail("VERIFICATION_FAILED", "Созданный объект IPsec имеет системный или неподтверждённый флаг") + for (const [key, value] of Object.entries(command.fields)) { + if (row[key] !== value && !(["disabled", "responder"].includes(key) && ["yes", "no", "true", "false"].includes(value) && observedBoolean(row[key]) === observedBoolean(value))) throw fail("VERIFICATION_FAILED", "Поля созданного объекта IPsec не подтверждены") + } + rows.push(structuredClone(row)) + } else { + const index = rows.findIndex(row => row[".id"] === command.id) + if (index < 0 || rows.filter(row => row[".id"] === command.id).length !== 1) throw fail("STATE_CONFLICT", "Запланированный native объект IPsec отсутствует") + if (command.action === "delete") rows.splice(index, 1) + else Object.assign(rows[index], command.fields) + } + return next +} diff --git a/backend/src/services/ipsec-user-update.ts b/backend/src/services/ipsec-user-update.ts new file mode 100644 index 00000000..9abf2573 --- /dev/null +++ b/backend/src/services/ipsec-user-update.ts @@ -0,0 +1,69 @@ +import { ipsecUserPatchSchema } 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 { MikrotikClient } from "./mikrotik.js" +import { readIpsecPeerDeleteState, ipsecPeerDeleteValue, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureIpsecSnapshot } from "./ipsec-live.js" +import { planIpsecUserUpdate, projectIpsecUserUpdate } from "./ipsec-user-update-model.js" + +export const ipsecUserUpdatePatchSchema = ipsecUserPatchSchema.strict() +export function ipsecUserUpdateValue(state: IpsecPeerDeleteState) { + const value = ipsecPeerDeleteValue(state) + for (const [path, key] of [["/ip/ipsec/identity", "disabled"], ["/ip/ipsec/mode-config", "responder"]]) for (const row of value.catalogs[path] ?? []) { + if (row[key] === undefined) continue + const observed = observedBoolean(row[key]) + if (observed === null) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён boolean IPsec", 502) + row[key] = String(observed) + } + return value +} +export async function ipsecUserUpdateProof(state: IpsecPeerDeleteState) { return (await privateFingerprinter("ipsec-user-update-v2"))(fingerprint(ipsecUserUpdateValue(state))) } +export async function readIpsecUserUpdateState(server: ServerRow, authorize: () => Promise = async () => {}) { + const state = await readIpsecPeerDeleteState(server, authorize) + return { ...state, proof: await ipsecUserUpdateProof(state) } +} +export class IpsecUserUpdateExecutionError extends McpError { + constructor(code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string, readonly confirmedWrites: number, readonly completedCommands: number, readonly commandCount: number) { super(code, message, 502) } +} +export async function updateIpsecUserGuarded(server: ServerRow, clientId: string, raw: unknown, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: typeof captureAndAppendRevision; receiptId?: string; checkpoint?: (value: { receiptId: string; commandCount: number; completedCommands: number; afterProof: string }) => Promise } = {}) { + const patch = ipsecUserUpdatePatchSchema.parse(raw) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "ipsec_user", id: clientId }, [server.id], async () => { + const source = async () => { await authorize(); const fresh = await getServerRowById(server.id); if (!fresh || !sameWireguardEndpoint(fresh, server)) throw new McpError("STATE_CONFLICT", "Источник IPsec изменился", 409) } + let state = await readIpsecUserUpdateState(server, source) + if (expectedProof !== undefined && state.proof !== expectedProof) throw new McpError("STATE_CONFLICT", "IPsec или зависимости изменились после подготовки", 409) + const plan = planIpsecUserUpdate(state, server, clientId, patch), client = MikrotikClient.fromServer(server) + let completedCommands = 0, confirmedWrites = 0 + const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string) => new IpsecUserUpdateExecutionError(code, message, confirmedWrites, completedCommands, plan.commands.length) + for (const command of plan.commands) { + let dispatched = false, response: unknown + const fence = async () => { const fresh = await readIpsecUserUpdateState(server, source); if (fresh.proof !== state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился после durable intent; команда не отправлена", 409); await source(); dispatched = true } + try { + response = command.action === "create" ? await client.put(command.path, command.fields, 15000, fence) : command.action === "update" ? await client.patch(command.path + "/" + encodeURIComponent(command.id!), command.fields, 15000, fence) : await client.delete(command.path + "/" + encodeURIComponent(command.id!), 10000, fence) + confirmedWrites++ + } catch (error) { + if (!dispatched && !completedCommands && error instanceof McpError) throw error + throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Изменение клиента IPsec остановлено; повтор команды запрещён, требуется сверка") + } + let after: IpsecPeerDeleteState + try { after = await readIpsecUserUpdateState(server, source) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена, но read-back недоступен; повтор запрещён") } + try { + const assignedId = command.action === "create" ? (response as Record | null)?.[".id"] : undefined + const createdRow = command.action === "create" ? after.catalogs[command.path].find(row => row[".id"] === assignedId) : undefined + if (command.action === "create" && typeof assignedId !== "string") throw Error("native create ACK missing") + const projected = projectIpsecUserUpdate(state, command, { assignedId: typeof assignedId === "string" ? assignedId : undefined, createdRow }) + if (await ipsecUserUpdateProof(projected) !== after.proof) throw Error("configuration mismatch") + } catch { throw stopped("VERIFICATION_FAILED", "Не подтверждены изменение клиента и сохранность остальных объектов IPsec; повтор запрещён") } + state = after; completedCommands++ + try { await source(); await options.checkpoint?.({ receiptId: options.receiptId ?? "", commandCount: plan.commands.length, completedCommands, afterProof: state.proof }) } catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда проверена, но checkpoint не сохранён; повтор запрещён") } + } + 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 + try { await source(); historyRecorded = (await (options.recordRevision ?? captureAndAppendRevision)({ serverId: server.id, section: "ipsec", source: "apply", capture: () => captureIpsecSnapshot(server, reader) })) !== null } catch { /* Never replay a native command for history. */ } + return { ok: true, verified: true, receiptId: options.receiptId ?? null, clientId, commandCount: plan.commands.length, completedCommands, confirmedWrites, afterProof: state.proof, historyRecorded, revisionRecorded: historyRecorded, state: "applied", ignoredFields: plan.ignoredFields, automaticReplay: false, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/ipv6-nd-configuration.ts b/backend/src/services/ipv6-nd-configuration.ts new file mode 100644 index 00000000..aeb3e386 --- /dev/null +++ b/backend/src/services/ipv6-nd-configuration.ts @@ -0,0 +1,83 @@ +import { isIP } from "node:net" +import { z } from "zod" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { observedBoolean, observedDurationMs } from "./ospf-observed-values.js" + +const iface = z.string().trim().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f,]/.test(value)) +function duration(maxMs: number, allowMilliseconds = true, special?: string) { + return z.string().max(64).transform((value, context) => { + if (special && value === special) return value + const ms = observedDurationMs(value) + if (ms !== null && Number.isSafeInteger(ms) && ms >= 0 && ms <= maxMs && (allowMilliseconds || ms % 1000 === 0)) { + if (ms === 0 && special && special !== "infinity") return special + return ms % 1000 === 0 ? `${ms / 1000}s` : `${ms}ms` + } + context.addIssue({ code: "custom", message: "Некорректный интервал IPv6 ND" }); return z.NEVER + }) +} +const raInterval = z.string().max(128).transform((value, context) => { + const parts = value.split("-"), min = observedDurationMs(parts[0]), max = observedDurationMs(parts[1]) + if (parts.length === 2 && min !== null && max !== null && min % 1000 === 0 && max % 1000 === 0 && min >= 3000 && min <= 1250000 && max >= 4000 && max <= 1800000 && min <= max * 0.75) return `${min / 1000}s-${max / 1000}s` + context.addIssue({ code: "custom", message: "Некорректная пара интервалов Router Advertisement" }); return z.NEVER +}) +const prefix = z.string().max(64).transform((value, context) => { + if (value === "none") return value + try { if (isIP(value.split("/")[0]) === 6) return canonicalBgpPolicyPrefix(value) } catch { /* Invalid CIDR. */ } + context.addIssue({ code: "custom", message: "Требуется префикс IPv6 или none" }); return z.NEVER +}) +const dns = z.string().max(1000).refine(value => value === "" || value.split(",").every(address => isIP(address) === 6)) +const pref64 = z.string().max(1000).transform((value, context) => { + if (value === "") return value + try { + const entries = value.split(",").map(entry => { + const [address, length] = entry.trim().split("/") + if (isIP(address) !== 6 || !["32", "40", "48", "56", "64", "96"].includes(length)) throw new Error("Invalid PREF64") + return canonicalBgpPolicyPrefix(entry.trim()) + }) + if (new Set(entries).size === entries.length) return entries.join(",") + } catch { /* Invalid native prefix shape. */ } + context.addIssue({ code: "custom", message: "Требуются уникальные префиксы PREF64 с допустимыми длинами" }); return z.NEVER +}) +const ignoredOptions = z.string().max(12).transform((value, context) => { + if (value === "") return value + const options = value.split(",").map(part => part.trim()) + if (options.every(part => part === "dns" || part === "mtu") && new Set(options).size === options.length) return options.sort().join(",") + context.addIssue({ code: "custom", message: "Требуется список опций dns и mtu без повторов" }); return z.NEVER +}) +export const ipv6NdSettingsFields = z.object({ + "router-advertisement-route-distance": z.number().int().min(0).max(255).optional(), + "router-advertisement-ignored-options": ignoredOptions.optional(), +}).strict() +const optionalNumber = (min: number, max: number) => z.union([ + z.literal("unspecified"), z.number().int().min(min).max(max), + z.string().regex(/^\d{1,10}$/).transform(Number).pipe(z.number().int().min(min).max(max)), +]) +const common = { interface: iface.optional(), disabled: z.boolean().optional(), comment: z.string().max(1000).optional() } +export const ipv6NdFields = z.object({ + ...common, "ra-interval": raInterval.optional(), "ra-delay": duration(Number.MAX_SAFE_INTEGER).optional(), + "ra-lifetime": duration(9000000, false, "none").optional(), "ra-preference": z.enum(["low", "medium", "high"]).optional(), + mtu: optionalNumber(1280, 4294967295).optional(), + "hop-limit": optionalNumber(1, 255).optional(), + "reachable-time": duration(3600000, true, "unspecified").optional(), "retransmit-interval": duration(4294967295, true, "unspecified").optional(), + "advertise-dns": z.enum(["no", "yes", "self"]).optional(), dns: dns.optional(), pref64: pref64.optional(), "advertise-mac-address": z.boolean().optional(), + "managed-address-configuration": z.boolean().optional(), "other-configuration": z.boolean().optional(), +}).strict() +export const ipv6NdPrefixFields = z.object({ + ...common, prefix: prefix.optional(), "6to4-interface": iface.optional(), "on-link": z.boolean().optional(), autonomous: z.boolean().optional(), + "dhcp6-pd-preferred": z.boolean().optional(), "valid-lifetime": duration(4294967294000, false, "infinity").optional(), + "preferred-lifetime": duration(4294967294000, false, "infinity").optional(), +}).strict() + +export function normalizeIpv6NdRow(resource: string, row: Record) { + const schema = resource === "ipv6_nd_prefix" ? ipv6NdPrefixFields : resource === "ipv6_nd_settings" ? ipv6NdSettingsFields : ipv6NdFields + for (const [key, validator] of Object.entries(schema.shape)) { + if (!Object.hasOwn(row, key)) continue + const value = row[key] + const input = typeof value === "string" && /^\d+$/.test(value) && (key === "mtu" || key === "hop-limit" || key === "router-advertisement-route-distance") + ? Number(value) === 0 && key !== "router-advertisement-route-distance" ? "unspecified" : Number(value) + : typeof value === "string" && validator.safeParse(true).success ? observedBoolean(value) ?? value : value + const parsed = validator.safeParse(input) + if (parsed.success && parsed.data !== undefined) row[key] = String(parsed.data) + } + if (resource === "ipv6_nd" && ["true", "false"].includes(String(row["advertise-dns"]))) row["advertise-dns"] = row["advertise-dns"] === "true" ? "yes" : "no" +} diff --git a/backend/src/services/manual-data-collection-execution.test.ts b/backend/src/services/manual-data-collection-execution.test.ts new file mode 100644 index 00000000..ba1ced34 --- /dev/null +++ b/backend/src/services/manual-data-collection-execution.test.ts @@ -0,0 +1,35 @@ +import assert from "node:assert/strict" +import { createManualDataCollection } from "./manual-data-collection-execution.js" + +const events: string[] = [] +let polled = 2, failReadAt=0, reads=0, failCollect = false +const settings = { lastCollectedAt: "2026-10-24T00:00:00Z", lastDurationMs: 17, lastError: "" } +const operation = createManualDataCollection({ + collectServersRestPing: async () => { events.push("rest"); if (failCollect) throw Error("collector failed"); return { skipped: true } }, + collectUptime: async () => { events.push("uptime");return {resources:{servers:[]},ping:{probes:[],skippedByInterval:2}} }, + collectPingGroup: async ids => { events.push(`group:${ids.join(",")}`); return { polled } }, + readServersRestPingSettings: async () => { events.push("rest-settings"); if (++reads===failReadAt) throw Error("read failed"); return settings }, + readUptimeSettings: async () => { events.push("uptime-settings"); return {} }, + scheduleGlobalAlerts: () => { events.push("global-alert-engine") }, +}) +assert.deepEqual(await operation.collectServersRestPing(), { ...settings, ok: true, lastError: null,skipped:true }) +assert.deepEqual(events.splice(0), ["rest-settings","rest", "global-alert-engine", "rest-settings"], "Initialize settings first; a skipped collector still schedules global alerts") +assert.deepEqual(await operation.collectUptime(), { ok: true, lastCollectedAt: null, lastDurationMs: null, lastError: null,skipped:false,partial:false, + parts:{resources:{reported:true,skipped:false,fatalError:false,count:0},ping:{reported:true,skipped:false,fatalError:false,count:0,skippedByInterval:2}} }) +assert.deepEqual(events.splice(0), ["uptime", "global-alert-engine", "uptime-settings"]) +assert.deepEqual(await operation.collectPingGroup(["disabled-probe", "disabled-probe"]), { ok: true, polled: 2 }) +assert.deepEqual(events.splice(0), ["group:disabled-probe,disabled-probe", "global-alert-engine"], "Probe normalization and enabled policy remain in the shared collector") +polled = 0 +assert.deepEqual(await operation.collectPingGroup([]), { ok: true, polled: 0 }) +assert.deepEqual(events.splice(0), ["group:"]) +reads=0;failReadAt=2 +await assert.rejects(operation.collectServersRestPing(), /read failed/) +assert.deepEqual(events.splice(0), ["rest-settings","rest", "global-alert-engine", "rest-settings"], "Receipt failure cannot retry collection or the already queued global engine") +reads=0;failReadAt=1 +await assert.rejects(operation.collectServersRestPing(), /read failed/) +assert.deepEqual(events.splice(0),["rest-settings"],"Initial settings failure must not collect or queue alerts") +reads=0;failReadAt=0 +failCollect = true +await assert.rejects(operation.collectServersRestPing(), /collector failed/) +assert.deepEqual(events.splice(0), ["rest-settings","rest"], "Thrown collector failure preserves existing alert scheduling behavior") +console.log("Manual full collectors: exact receipts, global alert scheduling, group no-op and no automatic replay") diff --git a/backend/src/services/manual-data-collection-execution.ts b/backend/src/services/manual-data-collection-execution.ts new file mode 100644 index 00000000..1965b652 --- /dev/null +++ b/backend/src/services/manual-data-collection-execution.ts @@ -0,0 +1,56 @@ +/** The manual REST contracts include a deferred, global alert-engine run. + * A scoped collector alone is therefore not an equivalent implementation. */ +export interface ManualCollectionSettings { + lastCollectedAt?: string | null + lastDurationMs?: number | null + lastError?: string | null +} +export interface ManualDataCollectionDependencies { + collectServersRestPing: () => Promise + collectUptime: () => Promise + collectPingGroup: (probeIds: string[]) => Promise<{ polled: number }> + readServersRestPingSettings: () => Promise + readUptimeSettings: () => Promise + scheduleGlobalAlerts: () => void +} + +function settingsReceipt(row: ManualCollectionSettings) { + return { ok: true as const, lastCollectedAt: row.lastCollectedAt ?? null, + lastDurationMs: row.lastDurationMs ?? null, lastError: row.lastError || null } +} +function uptimePart(raw:unknown,rows:"servers"|"probes"){ + const value=raw&&typeof raw==="object"?raw as Record:undefined + return {reported:!!value&&Array.isArray(value[rows]),skipped:value?.skipped===true,fatalError:!!value?.fatalError,count:Array.isArray(value?.[rows])?value[rows].length:0, + ...(rows==="probes"?{skippedByInterval:typeof value?.skippedByInterval==="number"&&Number.isSafeInteger(value.skippedByInterval)&&value.skippedByInterval>=0?value.skippedByInterval:0}:{})} +} + +/** No retries: collector completion may already have written samples, and the + * alert hook may already be queued even when reading the receipt fails. */ +export function createManualDataCollection(deps: ManualDataCollectionDependencies) { + return { + collectServersRestPing: async () => { + // Ensure the singleton before the collector updates its run receipt. + await deps.readServersRestPingSettings() + const snapshot=await deps.collectServersRestPing() + deps.scheduleGlobalAlerts() + const skipped=!!snapshot&&typeof snapshot==="object"&&"skipped"in snapshot&&snapshot.skipped===true + return {...settingsReceipt(await deps.readServersRestPingSettings()),skipped} + }, + collectUptime: async () => { + const snapshot=await deps.collectUptime() + deps.scheduleGlobalAlerts() + const value=snapshot&&typeof snapshot==="object"?snapshot as Record:undefined + const parts={resources:uptimePart(value?.resources,"servers"),ping:uptimePart(value?.ping,"probes")} + const skipped=parts.resources.skipped||parts.ping.skipped + return {...settingsReceipt(await deps.readUptimeSettings()),parts,skipped,partial:skipped||Object.values(parts).some(part=>!part.reported||part.fatalError)} + }, + collectPingGroup: async (probeIds: string[]) => { + // Deduplication, the 64-ID cap, shared ping mutex and source-enabled + // filtering belong to collectPingForProbeIds, for every caller. + const { polled } = await deps.collectPingGroup(probeIds) + if (polled > 0) deps.scheduleGlobalAlerts() + return { ok: true as const, polled } + }, + } +} +export type ManualDataCollection = ReturnType diff --git a/backend/src/services/manual-data-collection.ts b/backend/src/services/manual-data-collection.ts new file mode 100644 index 00000000..fbf7a4d1 --- /dev/null +++ b/backend/src/services/manual-data-collection.ts @@ -0,0 +1,15 @@ +import { scheduleAlertEngineAfterDataCollectors } from "./alert-collector-hooks.js" +import { collectServersRestPingOnce, getServersApiPingSettings } from "./servers-rest-ping-collector.js" +import { collectPingForProbeIds, collectUptimeOnce, readUptimeSettings } from "./uptime-collector.js" +import { createManualDataCollection } from "./manual-data-collection-execution.js" + +/** Shared full manual operations. These retain global alert/outbox effects; + * callers must not advertise them as scoped, read-only diagnostics. */ +export const manualDataCollection = createManualDataCollection({ + collectServersRestPing: collectServersRestPingOnce, + collectUptime: collectUptimeOnce, + collectPingGroup: collectPingForProbeIds, + readServersRestPingSettings: getServersApiPingSettings, + readUptimeSettings, + scheduleGlobalAlerts: scheduleAlertEngineAfterDataCollectors, +}) diff --git a/backend/src/services/manual-traffic-collection.ts b/backend/src/services/manual-traffic-collection.ts new file mode 100644 index 00000000..aa44088b --- /dev/null +++ b/backend/src/services/manual-traffic-collection.ts @@ -0,0 +1,33 @@ +/** Complete existing manual traffic operation shared by REST and MCP. + * Scheduler owns collection locks, samples, journal/retention and alert hook. */ +import { runSchedulerJobNow } from "./scheduler.js" +import { getTrafficSettings } from "./traffic-collector.js" +import { appendEvent } from "../modules/events/service/events-service.js" + +export async function collectTrafficManually() { + try { + const receipt = await runSchedulerJobNow("traffic") + if (!receipt.result || receipt.result.job !== "traffic") return { statusCode: 502, body: { ok: false, error: receipt.error ?? "Нет подтверждённого результата сбора" } } + const result = receipt.result + if (result.skipped) return { statusCode: 409, body: { error: "Сбор трафика уже выполняется" } } + const failures = result.servers.filter(s => !s.ok) + if (result.fatalError || failures.length) { + const error = result.fatalError ?? failures.map(s => `${s.name}: ${s.error ?? "Ошибка опроса"}`).join("; ") + await appendEvent({ level: "warning", eventType: "traffic.collect.manual.failed", sourceModule: "traffic", title: "Сбор трафика завершён с ошибками", message: error }) + return { statusCode: 502, body: { ok: false, error, failures, succeeded: result.servers.filter(s => s.ok).map(s => s.serverId) } } + } + const updated = await getTrafficSettings() + const warnings = result.servers.filter(s => s.warning).map(s => ({ serverId: s.serverId, warning: s.warning })) + await appendEvent({ level: warnings.length ? "warning" : "info", eventType: "traffic.collect.manual.ok", sourceModule: "traffic", + title: warnings.length ? "Ручной сбор трафика завершён с ограничениями" : "Ручной сбор трафика завершен", + message: `Длительность: ${updated.lastDurationMs ?? 0} мс`, payload: { lastCollectedAt: updated.lastCollectedAt ?? null } }) + return { statusCode: 200, body: { ok: true, warnings, lastCollectedAt: updated.lastCollectedAt ?? null, + lastDurationMs: updated.lastDurationMs ?? null, lastError: updated.lastError || null } } + } catch (error) { + if (typeof error === "object" && error && "statusCode" in error && error.statusCode === 409) + return { statusCode: 409, body: { error: error instanceof Error ? error.message : "Сбор трафика уже выполняется" } } + const msg = error instanceof Error ? error.message : String(error) + await appendEvent({ level: "critical", eventType: "traffic.collect.manual.failed", sourceModule: "traffic", title: "Ошибка ручного сбора трафика", message: msg }) + return { statusCode: 500, body: { error: msg } } + } +} diff --git a/backend/src/services/mikrotik-get-admission.test.ts b/backend/src/services/mikrotik-get-admission.test.ts new file mode 100644 index 00000000..a42949d0 --- /dev/null +++ b/backend/src/services/mikrotik-get-admission.test.ts @@ -0,0 +1,52 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { MikrotikClient } from "./mikrotik.js" + +// Actual loopback HTTP verifies the optional boundary, not a mocked get(). +let requests = 0, admissions = 0, large = false, malformed = false +const server = createServer((req, res) => { + requests++ + assert.equal(req.method, "GET") + assert.equal(req.url, "/rest/ip/route") + res.writeHead(200, { "content-type": "application/json" }) + res.end(malformed ? "{" : JSON.stringify(large ? "x".repeat(2048) : [{ ".id": "*1", active: "true" }])) +}) +await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) +const address = server.address() +assert.ok(address && typeof address !== "string") +const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture" }) +const admit = async () => { admissions++ } +try { + assert.deepEqual(await client.get("/ip/route", 1000, undefined, 1024, admit), [{ ".id": "*1", active: "true" }]) + assert.equal(admissions, 1); assert.equal(requests, 1) + const denial = new Error("denied") + await assert.rejects(client.get("/ip/route", 1000, undefined, 1024, async () => { admissions++; throw denial }), error => error === denial) + assert.equal(admissions, 2); assert.equal(requests, 1) + const aborted = new AbortController(); aborted.abort() + await assert.rejects(client.get("/ip/route", 1000, aborted.signal, 1024, admit)) + assert.equal(admissions, 2); assert.equal(requests, 1) + const late = new AbortController() + await assert.rejects(client.get("/ip/route", 1000, late.signal, 1024, async () => { admissions++; late.abort() })) + assert.equal(admissions, 3); assert.equal(requests, 1) + let enter!: () => void, release!: () => void + const entered = new Promise(resolve => { enter = resolve }) + const resume = new Promise(resolve => { release = resolve }) + const blocked = client.get("/ip/route", 1000, undefined, 1024, async () => { admissions++; enter(); await resume }) + await entered + assert.equal(requests, 1, "No request is constructed while admission is pending") + release(); await blocked + assert.equal(requests, 2); assert.equal(admissions, 4) + large = true + await assert.rejects(client.get("/ip/route", 1000, undefined, 1024, admit), /предел байтов/) + assert.equal(requests, 3); assert.equal(admissions, 5) + large = false; malformed = true + await assert.rejects(client.get("/ip/route", 1000, undefined, 1024, admit), /Invalid JSON/) + assert.equal(requests, 4); assert.equal(admissions, 6, "Failure never retries admission or the HTTP request") + malformed = false + await client.get("/ip/route", 1000, undefined, 1024) + assert.equal(requests, 5); assert.equal(admissions, 6, "Legacy GET without an admission callback is preserved") + console.log("GET admission loopback: allow/deny, pre/post admission abort, pending gate, byte/JSON failures without retry, legacy calls passed; DNS/TLS/socket queue are outside this hook") +} finally { + server.closeAllConnections() + await new Promise(resolve => server.close(() => resolve())) +} diff --git a/backend/src/services/mikrotik-get-cancel.test.ts b/backend/src/services/mikrotik-get-cancel.test.ts new file mode 100644 index 00000000..205a02e8 --- /dev/null +++ b/backend/src/services/mikrotik-get-cancel.test.ts @@ -0,0 +1,29 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { MikrotikClient } from "./mikrotik.js" + +let entered!:()=>void,closed!:()=>void +const reading=new Promise(resolve=>{entered=resolve}),responseClosed=new Promise(resolve=>{closed=resolve}),paths:string[]=[] +const server=createServer((req,res)=>{ + paths.push(req.url??"") + res.writeHead(200,{"content-type":"application/json"});res.write("[") + res.on("close",closed);entered() +}) +await new Promise(resolve=>server.listen(0,"127.0.0.1",resolve)) +const params={host:"127.0.0.1",port:(server.address() as {port:number}).port,useSsl:false,verifySsl:false,username:"fixture",password:"fixture-only"} +try{ + const client=new MikrotikClient(params) + await assert.rejects(client.get("/not-issued",30000,AbortSignal.abort())) + const controller=new AbortController(),pending=client.get("/stalled",30000,controller.signal) + await reading + controller.abort() + await assert.rejects(pending) + await Promise.race([responseClosed,delay(2000).then(()=>{throw Error("Отмена GET не закрыла HTTP-ответ")})]) + assert.deepEqual(paths,["/rest/stalled"]) + await assert.rejects(new MikrotikClient({...params,port:70000}).get("/invalid-port",5)) + await delay(20) // A constructor failure must not leave a timer dereferencing an unassigned request. +}finally{ + server.closeAllConnections();await new Promise(resolve=>server.close(()=>resolve())) +} +console.log("RouterOS GET: pre-abort prevents dispatch, in-flight abort closes body/socket, synchronous constructor failure leaves no crashing timer") diff --git a/backend/src/services/mikrotik-managed-source.test.ts b/backend/src/services/mikrotik-managed-source.test.ts new file mode 100644 index 00000000..da54727c --- /dev/null +++ b/backend/src/services/mikrotik-managed-source.test.ts @@ -0,0 +1,71 @@ +/** Real localhost HTTP, fixed mocked configuration boundary, no database. */ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { once } from "node:events" +import { mock, test } from "node:test" + +test("mutable RouterOS transport requires explicit valid managed source before locks/hooks/network", async t => { + const locks: number[][] = [], writes: { serverId: number; method: string; path: string }[] = [], requests: { method: string; path: string }[] = [] + let refusePolicy = false + mock.module("./config-mutation-lock.js", { namedExports: { + withConfigMutationLock: async (ids: number[], work: () => Promise) => { locks.push([...ids]); return work() }, + withApplicationMutationLock: async (work: () => Promise) => work(), + } }) + mock.module("./config-mutation-policy.js", { namedExports: { + executeGuardedConfigurationWrite: async (serverId: number, method: string, path: string, work: () => Promise) => { writes.push({ serverId, method, path }); if (refusePolicy) throw Error("Original policy refused"); return work() }, + } }) + const { MikrotikClient } = await import("./mikrotik.js") + const router = createServer((req, res) => { requests.push({ method: req.method!, path: req.url! }); req.resume(); res.setHeader("content-type", "application/json"); res.end('{"ack":"fixture"}') }) + router.listen(0, "127.0.0.1"); await once(router, "listening"); const address = router.address(); assert(address && typeof address !== "string") + const endpoint = { host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture" } + try { + await t.test("missing/invalid IDs and params.id cannot authorize any mutable verb or beforeDispatch callback", async () => { + let hooks = 0 + for (const id of [undefined, null, 0, -1, 1.5, Number.NaN, Infinity, Number.MAX_SAFE_INTEGER + 1, "71"]) { + const client = new MikrotikClient({ ...endpoint, id: 71 } as typeof endpoint, id as number | undefined) + for (const write of [() => client.post("/interface/bridge/add", {}), () => client.put("/ip/address", {}, 1000, async () => { hooks++ }), + () => client.patch("/ip/address/*1", {}, 1000, async () => { hooks++ }), () => client.delete("/ip/address/*1", 1000, async () => { hooks++ })]) { + await assert.rejects(write(), error => !!error && typeof error === "object" && "code" in error && error.code === "MIKROTIK_MANAGED_SOURCE_REQUIRED") + } + } + assert.equal(hooks, 0); assert.equal(locks.length, 0); assert.equal(writes.length, 0); assert.equal(requests.length, 0) + }) + await t.test("strict diagnostics still require a genuine private permit; forged tokens cannot borrow the id-less exception", async () => { + const strict = new MikrotikClient(endpoint, undefined, { requireBandwidthPermit: true }), before = requests.length + await assert.rejects(strict.post("/tool/bandwidth-test", { address: "192.0.2.2", duration: "3s" })) + for (const path of ["/tool/bandwidth-test", "/interface/bridge/add"]) await assert.rejects(strict.post(path, { address: "192.0.2.2", duration: "3s" }, 1000, undefined, undefined, {} as never)) + assert.equal(requests.length, before); assert.equal(writes.length, 0); assert.equal(locks.length, 0) + await assert.rejects(new MikrotikClient(endpoint).post("/tool/bandwidth-test", {}), error => !!error && typeof error === "object" && "code" in error && error.code === "MIKROTIK_MANAGED_SOURCE_REQUIRED") + }) + await t.test("unscoped GET and existing read/diagnostic POST allowlist retain transport behavior", async () => { + const client = new MikrotikClient(endpoint) + assert.deepEqual(await client.get("/interface"), { ack: "fixture" }) + for (const path of ["/ip/address/get", "/ip/route/print", "/interface/monitor", "/ping", "/tool/ping", "/tool/traceroute", "/interface/monitor-traffic"]) assert.deepEqual(await client.post(path, {}), { ack: "fixture" }) + assert.equal(requests.length, 8); assert.equal(writes.length, 0); assert.equal(locks.length, 0) + }) + await t.test("apparent read suffix in a query or fragment never exempts a POST from write policy", async () => { + const before = requests.length, unbound = new MikrotikClient(endpoint) + const disguised = "/routing/filter/rule/set?x=/print" + for (const [client, path] of [ + [unbound, disguised], [unbound, "/routing/filter/rule/set#x=/get"], + [new MikrotikClient({ ...endpoint, apiPath: "/rest/routing/filter/rule/set?x=" }), "/ip/route/print"], + ] as const) { + await assert.rejects(client.post(path, {}), { code: "MIKROTIK_MANAGED_SOURCE_REQUIRED" }) + } + refusePolicy = true + try { await assert.rejects(new MikrotikClient(endpoint, 71).post(disguised, {}), /Original policy refused/) } + finally { refusePolicy = false } + assert.equal(requests.length, before) + assert.deepEqual(locks.splice(0), [[71]]) + assert.deepEqual(writes.splice(0), [{ serverId: 71, method: "POST", path: disguised }]) + }) + await t.test("actual constructor binding reaches both original lock and policy before each successful mutation", async () => { + const client = new MikrotikClient({ ...endpoint, id: 999 } as typeof endpoint, 71), before = requests.length + await client.post("/interface/bridge/add", {}); await client.put("/ip/address", {}); await client.patch("/ip/address/*1", {}); await client.delete("/ip/address/*1") + assert.deepEqual(locks, [[71], [71], [71], [71]]); assert(writes.every(write => write.serverId === 71)) + assert.deepEqual(writes.map(write => write.method), ["POST", "PUT", "PATCH", "DELETE"]); assert.equal(requests.length - before, 4) + refusePolicy = true; const count = requests.length + await assert.rejects(client.patch("/ip/address/*1", {}), /Original policy refused/); assert.equal(requests.length, count) + }) + } finally { router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())); mock.restoreAll(); const { closePool } = await import("../db/index.js"); await closePool() } +}) diff --git a/backend/src/services/mikrotik-owned-file-zeroization.test.ts b/backend/src/services/mikrotik-owned-file-zeroization.test.ts new file mode 100644 index 00000000..3380ae4f --- /dev/null +++ b/backend/src/services/mikrotik-owned-file-zeroization.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import test from "node:test" +import ts from "typescript" + +// Execute the actual method with inert I/O and retained buffer references, so +// cleanup is observable without intercepting global Buffer methods or networking. +const source = readFileSync(new URL("./mikrotik.ts", import.meta.url), "utf8") +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; + class Reader { params={}; get=deps.get; ${source.slice(begin, end)} } + return new Reader(); +`, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None } }).outputText + +for (const mode of ["success", "raw-revoked", "decode-error", "wrong-size", "metadata-drift", "final-revoked"]) { + test(`private owned file clears its buffers: ${mode}`, async () => { + const secret = "synthetic-private-file" + const raw = Buffer.from(JSON.stringify({ ret: secret })) + const decoded = Buffer.from(mode === "wrong-size" ? "short" : secret) + let metadataCalls = 0, authorizations = 0, reads = 0 + const reader = new Function("deps", script)({ + MAX_OWNED_FILE_BYTES: 61438, + MikrotikError: class extends Error {}, + 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 }, + }) + const execute = () => reader.downloadOwnedFile({ id: "*owned", name: "owned.p12", size: Buffer.byteLength(secret) }, { + authorize: async () => { + authorizations++ + if ((mode === "raw-revoked" && authorizations === 4) || (mode === "final-revoked" && authorizations === 7)) throw Error("revoked") + }, + }) + if (mode === "success") { + const result = await execute() + assert.equal(result, decoded) + assert.equal(result.toString(), secret, "successful ownership transfer keeps caller bytes intact") + result.fill(0) + } else { + await assert.rejects(execute(), error => error instanceof Error && !error.message.includes(secret)) + if (!["raw-revoked", "decode-error"].includes(mode)) assert.ok(decoded.every(byte => byte === 0), "decoded bytes must be cleared after failure") + } + assert.equal(reads, 1, "no read replay") + assert.ok(raw.every(byte => byte === 0), "raw private response must always be cleared") + }) +} + +const createSource = readFileSync(new URL("./ipsec-user-create.ts", import.meta.url), "utf8") +const privateBegin = createSource.indexOf(" const content = await client.downloadOwnedFile(") +const privateEnd = createSource.indexOf(" } finally { content.fill(0) }", privateBegin) +assert.ok(privateBegin > 0 && privateEnd > privateBegin) +const createScript = ts.transpileModule(` + const {client,file,assignedId,size,source,readIpsecUserCreateState,server,state,plan,body,buildSswanConfig,buildClientInstructions}=deps; + let bundle; + ${createSource.slice(privateBegin, privateEnd + " } finally { content.fill(0) }".length)} + return bundle; +`, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None } }).outputText +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor +for (const mode of ["success", "wrong-size", "source-revoked", "state-drift"]) { + test(`IPsec CREATE clears transferred private file: ${mode}`, async () => { + const secret = "synthetic-create-key", bytes = Buffer.from(mode === "wrong-size" ? "short" : secret) + const run = new AsyncFunction("deps", createScript) + const execute = () => run({ + client: { downloadOwnedFile: async () => bytes }, file: { name: "synthetic.p12" }, assignedId: "*owned", size: Buffer.byteLength(secret), + source: async () => { if (mode === "source-revoked") throw Error("revoked") }, + readIpsecUserCreateState: async () => ({ proof: mode === "state-drift" ? "changed" : "expected" }), + server: {}, state: { proof: "expected" }, plan: { serverEndpoint: "example.invalid", certName: "synthetic" }, body: { name: "synthetic", passphrase: "fixture-passphrase" }, + buildSswanConfig: () => "synthetic-config", buildClientInstructions: () => "synthetic-instructions", + }) + if (mode === "success") assert.equal(Buffer.from((await execute()).contentB64, "base64").toString(), secret) + else await assert.rejects(execute()) + assert.ok(bytes.every(byte => byte === 0)) + }) +} diff --git a/backend/src/services/mikrotik-owned-file.test.ts b/backend/src/services/mikrotik-owned-file.test.ts new file mode 100644 index 00000000..33c371c8 --- /dev/null +++ b/backend/src/services/mikrotik-owned-file.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +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) } +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++ + 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 + } + let raw = ""; for await (const chunk of req) raw += chunk + const body = JSON.parse(raw); bodies.push(body) + res.on("close", () => { closed = true }) + if (mode === "401") { res.statusCode = 401; res.end("{}"); return } + if (mode === "400") { res.statusCode = 400; res.end('{"detail":"unknown parameter unrelated"}'); return } + if (mode.startsWith("compat") && !body[".proplist"]) { if (mode === "compat-revoke") allowed = false; res.statusCode = mode === "compat-404" ? 404 : 400; res.end('{"detail":"unknown parameter value-name"}'); return } + if (mode === "malformed") { res.end('{"ret":"unterminated'); return } + if (mode === "wrong-size") { res.end(JSON.stringify({ ret: "short" })); return } + if (mode === "timeout") { res.writeHead(200); res.write('{"ret":"'); return } + if (mode === "abort") { res.writeHead(200); res.write('{"ret":"'); controller!.abort(); return } + if (mode === "huge") { + res.writeHead(200); res.write('{"ret":"') + for (let i = 0; i < 512 && !res.destroyed; i++) { sent += 16384; res.write("x".repeat(16384)); await new Promise(resolve => setImmediate(resolve)) } + if (!res.destroyed) res.end('"}'); return + } + if (mode === "revoked") allowed = false + res.end(JSON.stringify({ ret: content })) +}) +await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) +const client = new MikrotikClient({ host: "127.0.0.1", port: (router.address() as { port: number }).port, username: "fixture", password: "fixture", useSsl: false, verifySsl: false }) +const reset = (next: string) => { mode = next; allowed = true; requests = []; bodies = []; metadataCalls = 0; closed = false; sent = 0; controller = undefined } +const authorize = async () => { if (!allowed) throw Error("fixture grant revoked") } +try { + reset("normal") + 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 compatibility of ["compat-400", "compat-404"]) { + reset(compatibility) + assert.equal((await client.downloadOwnedFile(target, { authorize })).toString(), content) + assert.deepEqual(requests, ["/rest/file", "/rest/file/get", "/rest/file", "/rest/file/get", "/rest/file"]) + assert.deepEqual(bodies, [{ ".id": target.id, "value-name": "contents" }, { ".id": target.id, ".proplist": "contents" }]) + } + reset("compat-revoke") + await assert.rejects(client.downloadOwnedFile(target, { authorize }), /revoked/) + assert.deepEqual(requests, ["/rest/file", "/rest/file/get"]) + reset("compat-replace") + await assert.rejects(client.downloadOwnedFile(target, { authorize }), /ID\/name\/size/) + assert.deepEqual(requests, ["/rest/file", "/rest/file/get", "/rest/file"]) + assert.equal(bodies.length, 1) + for (const fault of ["401", "400", "malformed", "wrong-size", "revoked", "huge", "timeout", "abort"]) { + reset(fault); if (fault === "abort") controller = new AbortController() + await assert.rejects(client.downloadOwnedFile(target, { authorize, ...(controller ? { signal: controller.signal } : {}), timeoutMs: fault === "timeout" ? 75 : 5000 })) + await delay(25) + assert.deepEqual(requests, ["/rest/file", "/rest/file/get"], `${fault} must issue no compatibility retry or metadata follow-up`) + assert.equal(bodies.length, 1) + if (["huge", "timeout", "abort"].includes(fault)) assert.equal(closed, true, `${fault} must close the response`) + if (fault === "huge") assert(sent < 1048576, "oversize stream must stop while chunks arrive") + } + reset("replace-before") + await assert.rejects(client.downloadOwnedFile(target, { authorize }), /ID\/name\/size/) + assert.deepEqual(requests, ["/rest/file"]) + reset("replace-after") + await assert.rejects(client.downloadOwnedFile(target, { authorize }), /ID\/name\/size/) + assert.deepEqual(requests, ["/rest/file", "/rest/file/get", "/rest/file"]) + assert.equal(bodies.length, 1, "replacement ID must never be read") + reset("normal") + await assert.rejects(client.downloadOwnedFile({ ...target, size: MAX_OWNED_FILE_BYTES + 1 }, { authorize })) + await assert.rejects(client.downloadOwnedFile(target, { authorize, signal: AbortSignal.abort() })) + await assert.rejects(client.downloadOwnedFile(target, { authorize: () => new Promise(() => {}), timeoutMs: 50 })) + assert.deepEqual(requests, []) +} finally { router.closeAllConnections(); await new Promise(resolve => router.close(() => resolve())) } +console.log("owned private file loopback: exact ID/size, streamed response bound, 401/unsupported/malformed/timeout/revocation/abort stop without retry or retarget") diff --git a/backend/src/services/mikrotik-response-bounds.test.ts b/backend/src/services/mikrotik-response-bounds.test.ts new file mode 100644 index 00000000..3a9ecf6f --- /dev/null +++ b/backend/src/services/mikrotik-response-bounds.test.ts @@ -0,0 +1,43 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { once } from "node:events" +import { MikrotikClient } from "./mikrotik.js" + +const unicode = JSON.stringify({ value: "я".repeat(64) }), exact = Buffer.byteLength(unicode) +let calls = 0, closed = 0 +const server = createServer((req, res) => { + calls++ + res.once("close", () => { closed++ }) + if (req.url === "/rest/length") { res.writeHead(200, { "content-type": "application/json", "content-length": "1000000" }); res.flushHeaders(); return } + if (req.url === "/rest/stall") { res.writeHead(200, { "content-type": "application/json" }); res.flushHeaders(); return } + if (req.url?.startsWith("/rest/default-overflow")) { res.end(JSON.stringify({ value: "я".repeat(5 * 1024 * 1024) })); return } + if (req.url === "/rest/chunks") { res.writeHead(200, { "content-type": "application/json" }); const bytes = Buffer.from(unicode); res.write(bytes.subarray(0, 11)); res.write(bytes.subarray(11, 73)); res.end(bytes.subarray(73)); return } + res.setHeader("content-type", "application/json"); res.end(unicode) +}) +await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) +try { + const client = new MikrotikClient({ host: "127.0.0.1", port: (server.address() as { port: number }).port, username: "fixture", password: "NEVER_PASSWORD", useSsl: false, verifySsl: false }) + assert.deepEqual(await client.get("/exact", 1000, undefined, exact), JSON.parse(unicode)) + for (const path of ["/exact", "/chunks", "/length"]) { + const before = calls + await assert.rejects(client.get(path, 1000, undefined, exact - 1), /предел байтов/) + assert.equal(calls, before + 1, "A bounded read is never retried") + } + assert.deepEqual(await client.get("/chunks", 1000, undefined, exact), JSON.parse(unicode)) + await assert.rejects(client.get("/default-overflow", 2000), /предел байтов/) + const postBefore = calls + await assert.rejects(client.post("/default-overflow/get", {}, 2000), /8 МиБ/) + assert.equal(calls, postBefore + 1) + // Abort an active response, then drain repeated bounded failures through the same two-socket pool. + const abort = new AbortController(), before = calls + const pending = client.get("/stall", 2000, abort.signal, exact) + while (calls === before) await new Promise(resolve => setTimeout(resolve, 1)) + abort.abort() + await assert.rejects(pending) + for (let index = 0; index < 4; index++) { await assert.rejects(client.get("/length", 1000, undefined, exact)); assert.deepEqual(await client.get("/exact", 1000, undefined, exact), JSON.parse(unicode)) } + const preInvalid = calls + for (const limit of [0, -1, 1.5, 9 * 1024 * 1024]) await assert.rejects(client.get("/exact", 1000, undefined, limit), RangeError) + assert.equal(calls, preInvalid) + assert.ok(closed >= calls - 1) + console.log("RouterOS GET bounds: actual UTF-8 bytes, exact limit, content-length, split chunks, default 8MiB, cancellation, socket drainage and no retry checked") +} finally { server.closeAllConnections(); server.close(); await once(server, "close") } diff --git a/backend/src/services/mikrotik-response.test.ts b/backend/src/services/mikrotik-response.test.ts index e8801071..78bed764 100644 --- a/backend/src/services/mikrotik-response.test.ts +++ b/backend/src/services/mikrotik-response.test.ts @@ -1,7 +1,30 @@ -import { test } from "node:test" +import { after, test } from "node:test" import assert from "node:assert/strict" import { createServer } from "node:http" import { MikrotikClient, MikrotikError, routerOsAccessHint } from "./mikrotik.js" +import { pool, closePool } from "../db/index.js" +import { configurationAuditTransactionFixture, emptyActiveRoutingProfilesSqlFixture } from "../test/fixtures/managed-mutation-sql.js" + +// Explicit managed fixture source; execute the original lock/policy path over +// strict synthetic SQL instead of using the former unscoped transport bypass. +const originalQuery = pool.query, originalConnect = pool.connect +pool.connect = (async () => ({ + async query(text: string, values?: unknown[]) { + const admission = configurationAuditTransactionFixture(text, values) + if (admission) return admission + if (text.startsWith("INSERT INTO mcp_audit(")) return pool.query(text, values) + assert(["SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired", "SELECT pg_advisory_unlock(hashtextextended($1,0))"].includes(text)) + assert.deepEqual(values, ["mm-config:71"]); return { rows: [{ acquired: true }] } + }, release() {}, +})) as typeof pool.connect +pool.query = (async (text: string, values?: unknown[]) => { + const routing = emptyActiveRoutingProfilesSqlFixture(text, values, 71) + if (routing) return routing + if (text.startsWith("SELECT object_ref FROM mcp_protections WHERE")) { assert.deepEqual(values, ["71"]); return { rows: [] } } + assert.equal(text, "INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)") + assert.equal(JSON.parse(String(values?.[3])).serverId, 71); return { rows: [] } +}) as typeof pool.query +after(async () => { pool.query = originalQuery; pool.connect = originalConnect; await closePool() }) test("RouterOS access diagnostics distinguish login saturation without assuming a wrong password or retrying", async () => { let requests = 0 @@ -9,7 +32,7 @@ test("RouterOS access diagnostics distinguish login saturation without assuming const server = createServer((req, res) => { requests++; req.resume(); res.writeHead(500); res.end(body) }) await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) const address = server.address(); assert(address && typeof address !== "string") - const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }) + const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }, 71) try { for (const operation of [() => client.get("/interface"), () => client.put("/own", {})]) { await assert.rejects(operation, (error: unknown) => { @@ -41,7 +64,7 @@ test("RouterOS print preserves native query arrays and property selection", asyn }) await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) const address = server.address(); assert(address && typeof address !== "string") - const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }) + const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }, 71) try { const result = await client.post("/ip/route/print", { ".query": ["comment"], ".proplist": [".id", "gateway"] }) assert.deepEqual(received, { ".query": ["comment"], ".proplist": [".id", "gateway"] }) @@ -59,7 +82,7 @@ test("RouterOS shared REST pool bounds logins across client instances and closes server.on("connection",socket=>{connections.add(socket);socket.on("close",()=>connections.delete(socket))}) await new Promise(resolve=>server.listen(0,"127.0.0.1",resolve)) const address=server.address();assert(address&&typeof address!=="string") - const clients=Array.from({length:3},()=>new MikrotikClient({host:"127.0.0.1",port:address.port,useSsl:false,verifySsl:false,username:"fixture",password:"fixture"})) + const clients=Array.from({length:3},()=>new MikrotikClient({host:"127.0.0.1",port:address.port,useSsl:false,verifySsl:false,username:"fixture",password:"fixture"}, 71)) try { await Promise.all(Array.from({length:12},(_,i)=>i%2?clients[i%3]!.post("/own",{},2000):clients[i%3]!.get("/own",2000))) assert.equal(requests,12);assert.equal(peak,2) @@ -72,7 +95,7 @@ test("RouterOS queued mutation that times out never reaches the device later", a const server=createServer((req)=>{req.resume();if(req.method!=="GET")writes++}) await new Promise(resolve=>server.listen(0,"127.0.0.1",resolve)) const address=server.address();assert(address&&typeof address!=="string") - const client=new MikrotikClient({host:"127.0.0.1",port:address.port,useSsl:false,verifySsl:false,username:"fixture",password:"fixture"}) + const client=new MikrotikClient({host:"127.0.0.1",port:address.port,useSsl:false,verifySsl:false,username:"fixture",password:"fixture"}, 71) try { const first=client.get("/hold1",300),second=client.get("/hold2",300),write=client.put("/own",{},40) const results=await Promise.allSettled([first,second,write]);assert(results.every(result=>result.status==="rejected"));assert.equal(writes,0) @@ -95,7 +118,7 @@ for (const method of methods) { await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) const address = server.address() assert.ok(address && typeof address !== "string") - const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }) + const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }, 71) let deadline: ReturnType | undefined try { const operation = method === "file" ? client.downloadFile({ id: "*A", name: "", size: 8 }, 500) @@ -123,7 +146,7 @@ test("RouterOS complete response: success, explicit HTTP rejection and body surv await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) const address = server.address() assert.ok(address && typeof address !== "string") - const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }) + const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }, 71) try { for (const method of ["get", "post", "put", "patch"] as const) { assert.deepEqual(await (method === "get" ? client.get("/fixture", 500) : client[method]("/fixture", {}, 500)), { ack: "confirmed" }) @@ -147,7 +170,7 @@ test("RouterOS nonempty malformed JSON cannot confirm PATCH; explicit empty succ await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) const address = server.address() assert.ok(address && typeof address !== "string") - const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }) + const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }, 71) try { for (const method of ["get", "post", "put", "patch"] as const) { await assert.rejects(method === "get" ? client.get("/malformed", 500) : client[method]("/malformed", {}, 500), /JSON|подтверждение/) diff --git a/backend/src/services/mikrotik-speed-admission.test.ts b/backend/src/services/mikrotik-speed-admission.test.ts new file mode 100644 index 00000000..35cc7175 --- /dev/null +++ b/backend/src/services/mikrotik-speed-admission.test.ts @@ -0,0 +1,81 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { MikrotikClient } from "./mikrotik.js" +import { pool } from "../db/index.js" +import { installManagedMutationSqlFixture } from "../test/fixtures/managed-mutation-sql.js" + +// Real loopback transport. No production credentials, SQL or RouterOS. +const received: { path: string; body: Record }[] = [] +let hold = false, dispatched: (() => void) | undefined +const server = createServer(async (request, response) => { + let body = "" + for await (const chunk of request) body += chunk.toString() + assert.equal(request.method, "POST") + received.push({ path: request.url!, body: JSON.parse(body) }); dispatched?.() + if (hold) return + response.writeHead(200, { "content-type": "application/json" }) + response.end(JSON.stringify([{ status: "done testing", "tx-total-average": "0" }])) +}) +await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) +const address = server.address(); assert(address && typeof address !== "string") +const managed = installManagedMutationSqlFixture(71) +const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture" }, 71) +const command = { address: "192.0.2.2", user: "destination-fixture", password: "synthetic-only", protocol: "udp" as const, direction: "receive" as const, durationSec: 2147483647 } +try { + const beforeQuery = pool.query, beforeConnect = pool.connect + let sqlCalls = 0 + pool.query = (() => { sqlCalls++; throw Error("Pre-aborted command must not enter configuration SQL") }) as never + pool.connect = (() => { sqlCalls++; throw Error("Pre-aborted command must not acquire configuration backend") }) as never + try { + const protectedClient = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: true, username: "fixture", password: "fixture" }, 123) + const controller = new AbortController(); controller.abort() + await assert.rejects(protectedClient.bandwidthTest(command, controller.signal)) + assert.equal(sqlCalls, 0); assert.equal(received.length, 0) + } finally { pool.query = beforeQuery; pool.connect = beforeConnect } + for (const kind of ["bandwidth", "ping"] as const) { + let admissions = 0 + const run = (signal?: AbortSignal, beforeDispatch?: () => Promise) => kind === "bandwidth" + ? client.bandwidthTest(command, signal, beforeDispatch) + : client.ping(command.address, 4, "ether-fixture", { interval: "1s", signal, beforeDispatch }) + const before: number = received.length + const denied = Error("admission refused") + await assert.rejects(run(undefined, async () => { admissions++; throw denied }), error => error === denied) + assert.equal(received.length, before) + const aborted = new AbortController(); aborted.abort() + await assert.rejects(run(aborted.signal, async () => { admissions++ })) + assert.equal(admissions, 1); assert.equal(received.length, before) + const late = new AbortController() + await assert.rejects(run(late.signal, async () => { admissions++; late.abort() })) + assert.equal(admissions, 2); assert.equal(received.length, before) + let enter!: () => void, release!: () => void + const entered = new Promise(resolve => { enter = resolve }) + const gate = new Promise(resolve => { release = resolve }) + const pending = run(undefined, async () => { admissions++; enter(); await gate }) + await entered; assert.equal(received.length, before) + release(); await pending + assert.equal(admissions, 3); assert.equal(received.length, before + 1) + assert.deepEqual(received.at(-1), kind === "bandwidth" + ? { path: "/rest/tool/bandwidth-test", body: { address: command.address, user: command.user, password: command.password, protocol: "udp", direction: "receive", duration: "2147483647s" } } + : { path: "/rest/tool/ping", body: { address: command.address, count: "4", interval: "1s", interface: "ether-fixture" } }) + hold = true + const controller = new AbortController() + const sent = new Promise(resolve => { dispatched = resolve }) + const active = run(controller.signal, async () => { admissions++ }) + const outcome = active.then(() => false, () => true) + await sent; controller.abort(); assert.equal(await outcome, true) + assert.equal(received.length, before + 2, "Closing a local request never retries or proves remote completion") + hold = false; dispatched = undefined + await run() + assert.equal(received.length, before + 3); assert.equal(admissions, 4, "Legacy calls remain valid without a hook") + } + const beforeCap=received.length + for(const maxMbps of [0,1001,Number.NaN,1.5])await assert.rejects(client.bandwidthTest({...command,maxMbps})) + assert.equal(received.length,beforeCap) + await client.bandwidthTest({...command,durationSec:3,maxMbps:1}) + assert.deepEqual(received.at(-1),{path:"/rest/tool/bandwidth-test",body:{address:command.address,user:command.user,password:command.password,protocol:"udp",direction:"receive",duration:"3s","local-tx-speed":"1000000","remote-tx-speed":"1000000"}}) + console.log("Speed native admission: legacy bodies and bounded RouterOS rate caps; no retries or remote stop claim") +} finally { + managed.restore() + server.closeAllConnections() + await new Promise(resolve => server.close(() => resolve())) +} diff --git a/backend/src/services/mikrotik.ts b/backend/src/services/mikrotik.ts index 749fcdd9..7ea65ecf 100644 --- a/backend/src/services/mikrotik.ts +++ b/backend/src/services/mikrotik.ts @@ -1,10 +1,16 @@ import http from "node:http" import https from "node:https" import { randomBytes } from "node:crypto" +import { dispatchBandwidthNativePermit, BandwidthAdmissionError, type BandwidthNativePermit } from "./bandwidth-admission.js" +import type { BandwidthNativeRequestBinder } from "./bandwidth-native-dispatch.js" +import { withConfigMutationLock, withApplicationMutationLock } from "./config-mutation-lock.js" +import { executeGuardedConfigurationWrite } from "./config-mutation-policy.js" import { exportRouterConfiguration } from "./router-config-export.js" import type { Server } from "../db/schema.js" import { parseRosDataSizeBytes } from "./ros-metric-parse.js" +import { readOspfTemplateFlags } from "./ospf-template-flags.js" import { readOspfEffectiveDisabled } from "./ospf-effective-disabled.js" +import { writeOspfTemplateCost, ospfCostConnectionProof, OspfCostWriteError } from "./ospf-template-cost-write.js" import type { RosIdentity, RosInterface, RosIpAddress, RosResource, RosBgpSession, @@ -27,7 +33,7 @@ function watchRosResponseFailure(res: http.IncomingMessage, reject: (error: Erro } function appendRosBody(body: string, chunk: string, req?: http.ClientRequest): string { - if (body.length + chunk.length > MAX_ROS_BODY_BYTES) { + if (Buffer.byteLength(body) + Buffer.byteLength(chunk) > MAX_ROS_BODY_BYTES) { req?.destroy(new Error("RouterOS: ответ больше 8 МиБ")) return body } @@ -44,15 +50,29 @@ export interface MikrotikConnectParams { username: string password: string apiPath?: string // defaults to "/rest" + routingEndpointEpoch?: string + /** Opt-in pinned diagnostic DNS; original host remains Host/TLS SNI. */ + lookup?: typeof import("node:dns").lookup + servername?: string } // ── low-level HTTP helpers ──────────────────────────────────────────────────── +const preHeaderGetResets = new WeakSet() + +/** Transport provenance for scoped GET callers. The original error stays intact. */ +export function isRouterOsPreHeaderGetReset(error: unknown): boolean { + return error instanceof Error && preHeaderGetResets.has(error) +} + function rosRequest( params: MikrotikConnectParams, path: string, timeoutMs: number, + signal?: AbortSignal, + maxResponseBytes = MAX_ROS_BODY_BYTES, ): Promise { + if (!Number.isSafeInteger(maxResponseBytes) || maxResponseBytes < 1 || maxResponseBytes > MAX_ROS_BODY_BYTES) return Promise.reject(new RangeError("Некорректный предел ответа RouterOS")) return new Promise((resolve, reject) => { const basePath = params.apiPath ?? "/rest" const authHeader = "Basic " + Buffer.from(`${params.username}:${params.password}`).toString("base64") @@ -64,22 +84,48 @@ function rosRequest( method: "GET", headers: { Authorization: authHeader, "Content-Type": "application/json" }, rejectUnauthorized: params.useSsl ? params.verifySsl : undefined, + signal, + lookup: params.lookup, + servername: params.servername, } const lib = params.useSsl ? https : http options.agent = params.useSsl ? rosHttpsAgent : rosHttpAgent + let req: ReturnType | undefined + let headersReceived = false, timedOut = false const timer = setTimeout(() => { - req.destroy(new Error(`Connection to ${params.host}:${params.port} timed out after ${timeoutMs / 1000}s`)) + timedOut = true + req?.destroy(new Error(`Connection to ${params.host}:${params.port} timed out after ${timeoutMs / 1000}s`)) }, timeoutMs) - const req = lib.request(options, (res) => { + try { + req = lib.request(options, (res) => { + headersReceived = true watchRosResponseFailure(res, error => { clearTimeout(timer); reject(error) }) - let body = "" - res.setEncoding("utf8") - res.on("data", (chunk: string) => { body = appendRosBody(body, chunk, req) }) + let body = "", bytes = 0, failed = false + const failSize = () => { + failed = true + clearTimeout(timer) + const error = new Error("RouterOS: превышен предел байтов ответа") + reject(error) + res.destroy(error) + req?.destroy(error) + } + const length = res.headers["content-length"] + if (typeof length === "string" && /^\d+$/.test(length) && Number(length) > maxResponseBytes) { failSize(); return } + // Count transport bytes before decoding: multibyte text and split UTF-8 chunks share one budget. + const chunks: Buffer[] = [] + res.on("data", (chunk: Buffer) => { + if (failed) return + bytes += chunk.length + if (bytes > maxResponseBytes) { failSize(); return } + chunks.push(chunk) + }) res.on("end", () => { clearTimeout(timer) + if (failed) return + body = Buffer.concat(chunks, bytes).toString("utf8") if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) { reject(new MikrotikError(res.statusCode ?? 0, path, body)) return @@ -92,12 +138,15 @@ function rosRequest( }) }) + req.once("information", () => { headersReceived = true }) req.on("error", (err) => { clearTimeout(timer) + if (!headersReceived && !timedOut && !signal?.aborted && (err as NodeJS.ErrnoException).code === "ECONNRESET") preHeaderGetResets.add(err) reject(err) }) req.end() + } catch(error) {clearTimeout(timer);reject(error)} }) } @@ -107,6 +156,7 @@ function rosPost( body: Record, timeoutMs: number, externalSignal?: AbortSignal, + bindNativeRequest?: BandwidthNativeRequestBinder, ): Promise { return new Promise((resolve, reject) => { const basePath = params.apiPath ?? "/rest" @@ -170,6 +220,11 @@ function rosPost( if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) { reject(new MikrotikError(res.statusCode ?? 0, path, buf)); return } + // A settings command with no return data can acknowledge with an empty body. + if (!buf.trim() && (path === "/ip/settings/set" || path === "/ipv6/settings/set")) { + resolve([]) + return + } try { resolve(JSON.parse(buf)) } catch { reject(new Error(`Invalid JSON from RouterOS: ${buf.slice(0, 200)}`)) } @@ -181,8 +236,32 @@ function rosPost( req.on("error", (err) => { settle(() => reject(err)) }) - req.write(payload) - req.end() + if (!bindNativeRequest) { + req.write(payload) + req.end() + return + } + let bytesStarted = false + const sendBytesOnce = () => { + try { + if (bytesStarted) throw new BandwidthAdmissionError("REFUSED") + bytesStarted = true + externalSignal?.throwIfAborted() + req!.write(payload) + req!.end() + } catch (error) { + req!.destroy() + settle(() => reject(error)) + throw error + } + } + try { + // The private gate may defer bytes until socket-ready reauthorization. + bindNativeRequest(req, sendBytesOnce) + } catch (error) { + req.destroy() + settle(() => reject(error)) + } }) } @@ -191,6 +270,7 @@ function rosPut( path: string, body: Record, timeoutMs: number, + signal?: AbortSignal, ): Promise { return new Promise((resolve, reject) => { const basePath = params.apiPath ?? "/rest" @@ -202,6 +282,7 @@ function rosPut( port: params.port, path: basePath + path, method: "PUT", + signal, headers: { Authorization: authHeader, "Content-Type": "application/json", @@ -248,6 +329,8 @@ function rosDelete( params: MikrotikConnectParams, path: string, timeoutMs: number, + onAcknowledgedBody?: (body: string) => void, + signal?: AbortSignal, ): Promise { return new Promise((resolve, reject) => { const basePath = params.apiPath ?? "/rest" @@ -258,6 +341,7 @@ function rosDelete( port: params.port, path: basePath + path, method: "DELETE", + signal, headers: { Authorization: authHeader, "Content-Type": "application/json" }, rejectUnauthorized: params.useSsl ? params.verifySsl : undefined, } @@ -278,7 +362,7 @@ function rosDelete( if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) { reject(new MikrotikError(res.statusCode ?? 0, path, body)); return } - resolve() + try { onAcknowledgedBody?.(body); resolve() } catch (error) { reject(error) } }) }) @@ -293,7 +377,7 @@ function rosDelete( /** Запрос к RouterOS с сырым (не обязательно JSON) ответом — для содержимого файлов. */ function rosRawRequest( params: MikrotikConnectParams, - opts: { method: "GET" | "POST"; path: string; body?: Record; timeoutMs: number }, + opts: { method: "GET" | "POST"; path: string; body?: Record; timeoutMs: number; signal?: AbortSignal; maxResponseBytes?: number }, ): Promise { return new Promise((resolve, reject) => { const basePath = params.apiPath ?? "/rest" @@ -317,28 +401,43 @@ function rosRawRequest( const lib = params.useSsl ? https : http options.agent = params.useSsl ? rosHttpsAgent : rosHttpAgent + let settled = false + const finish = (work: () => void) => { + if (settled) return + settled = true; clearTimeout(timer); opts.signal?.removeEventListener("abort", abort); work() + } + const abort = () => req.destroy(new DOMException("Aborted", "AbortError")) + const limit = opts.maxResponseBytes ?? MAX_ROS_BODY_BYTES const req = lib.request(options, (res) => { - watchRosResponseFailure(res, error => { clearTimeout(timer); reject(error) }) + watchRosResponseFailure(res, error => finish(() => reject(error))) const chunks: Buffer[] = [] - res.on("data", (chunk: Buffer) => { chunks.push(chunk) }) + let bytes = 0 + res.on("data", (chunk: Buffer) => { + if (settled) return + bytes += chunk.length + if (bytes > limit) { req.destroy(new Error("RouterOS raw response exceeds the byte limit")); return } + chunks.push(chunk) + }) res.on("end", () => { + if (settled) return const buf = Buffer.concat(chunks) if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) { - reject(new MikrotikError(res.statusCode ?? 0, opts.path, buf.toString("utf8").slice(0, 200))) + finish(() => reject(new MikrotikError(res.statusCode ?? 0, opts.path, buf.toString("utf8").slice(0, 200)))) return } - resolve(buf) + finish(() => resolve(buf)) }) }) const timer = setTimeout(() => { req.destroy(new Error(`Connection to ${params.host}:${params.port} timed out after ${opts.timeoutMs / 1000}s`)) }, opts.timeoutMs) - req.on("close", () => clearTimeout(timer)) + req.on("close", () => { clearTimeout(timer); opts.signal?.removeEventListener("abort", abort) }) req.on("error", (err) => { - clearTimeout(timer) - reject(err) + finish(() => reject(err)) }) + if (opts.signal?.aborted) { abort(); return } + opts.signal?.addEventListener("abort", abort, { once: true }) if (payload) req.write(payload) req.end() }) @@ -349,6 +448,7 @@ function rosPatch( path: string, body: Record, timeoutMs: number, + signal?: AbortSignal, ): Promise { return new Promise((resolve, reject) => { const basePath = params.apiPath ?? "/rest" @@ -360,6 +460,7 @@ function rosPatch( port: params.port, path: basePath + path, method: "PATCH", + signal, headers: { Authorization: authHeader, "Content-Type": "application/json", @@ -432,6 +533,8 @@ function matchesUploadedFile(entryName: string, requested: string): boolean { /** Лимит команды `/file/get`: выше 61439 байт RouterOS молча отдаёт 0 байт. */ const MAX_FILE_GET_BYTES = 61439 +/** Exact-ID /file/get supports strictly less than 61439 decoded bytes. */ +export const MAX_OWNED_FILE_BYTES = MAX_FILE_GET_BYTES - 1 const RET_MARKER = Buffer.from('"ret":', "latin1") const CONTENTS_MARKER = Buffer.from('"contents":', "latin1") @@ -537,10 +640,7 @@ export function firewallRestPath( return `${root}/${table}` } -export function encodeRosId(rosId: string): string { - const id = rosId.startsWith("*") ? rosId : `*${rosId.replace(/^\*/, "")}` - return encodeURIComponent(id) -} +export { encodeRosId } from "./routeros-native-paths.js" // ── MikrotikClient ───────────────────────────────────────────────────────────── @@ -577,10 +677,26 @@ const EXPORT_PASSPHRASE_HINT = "(в группе full она уже есть)." export class MikrotikClient { - constructor(private readonly params: MikrotikConnectParams) {} + private readonly requireBandwidthPermit:boolean + constructor(private readonly params: MikrotikConnectParams, private readonly serverId?: number, options?:{requireBandwidthPermit:true}) { + if(options!==undefined&&(Object.keys(options).join(",")!=="requireBandwidthPermit"||options.requireBandwidthPermit!==true))throw new BandwidthAdmissionError("INVALID_INPUT") + this.requireBandwidthPermit=options?.requireBandwidthPermit===true + if(this.requireBandwidthPermit)this.params=Object.freeze({...params}) + } + + private mutation(method: string, path: string, work: (signal?: AbortSignal) => Promise): Promise { + const serverId = this.serverId + // Endpoint parameters (including an incidental params.id) never establish + // the managed source of a configuration write. Production factories pass + // the actual selected server row's ID as the explicit constructor binding. + if (typeof serverId !== "number" || !Number.isSafeInteger(serverId) || serverId <= 0) { + return Promise.reject(Object.assign(new Error("Для изменения RouterOS требуется привязка к управляемому серверу"), { code: "MIKROTIK_MANAGED_SOURCE_REQUIRED" })) + } + return withConfigMutationLock([serverId], () => executeGuardedConfigurationWrite(serverId, method, path, work)) + } /** Convenience factory from a DB Server row */ - static fromServer(server: Server): MikrotikClient { + static fromServer(server: Server, options?:{requireBandwidthPermit:true}): MikrotikClient { return new MikrotikClient({ host: server.host, port: server.port, @@ -588,29 +704,91 @@ export class MikrotikClient { verifySsl: server.verifySsl, username: server.username, password: server.password, + routingEndpointEpoch: server.routingEndpointEpoch, + }, server.id, options) + } + + /** Optional live admission runs once immediately before constructing the GET. + * It is not a socket-send hook: DNS/TLS and agent queueing follow admission. */ + async get(path: string, timeoutMs = 10_000, signal?: AbortSignal, maxResponseBytes?: number, beforeDispatch?: () => Promise): Promise { + if (beforeDispatch) { + signal?.throwIfAborted() + await beforeDispatch() + signal?.throwIfAborted() + } + return rosRequest(this.params, path, timeoutMs, signal, maxResponseBytes) as Promise + } + + async post(path: string, body: Record, timeoutMs = 15_000, signal?: AbortSignal, beforeDispatch?: () => Promise, bandwidthPermit?:BandwidthNativePermit): Promise { + signal?.throwIfAborted() + const bandwidthPrefix=/(?:^|\/)tool\/bandwidth-test(?:\/|$)/i + // A malformed query escape must not hide an otherwise valid command path. + // Classify its raw prefix even when percent decoding later fails. + let bandwidthPath=false + // rosPost concatenates apiPath + path. Check that final request target as + // well, otherwise a custom base can split the command across both inputs. + for(const target of [path,(this.params.apiPath??"/rest")+path]){ + const pathOnly=target.split(/[?#]/,1)[0].replace(/\/{2,}/g,"/") + bandwidthPath||=bandwidthPrefix.test(pathOnly) + try{ + let decoded=pathOnly + for(let pass=0;pass<2;pass++){ + // Decode segments independently: a literal percent in an unrelated + // proxy prefix must not hide an encoded command further down. + decoded=decoded.split("/").map(segment=>{try{return decodeURIComponent(segment)}catch{return segment}}).join("/") + const normalized=decoded.replace(/\/{2,}/g,"/") + bandwidthPath||=bandwidthPrefix.test(normalized)||bandwidthPrefix.test(new URL(normalized,"http://router.invalid").pathname) + } + }catch{/* The raw canonical prefix remains guarded on malformed escapes. */} + } + const guardedBandwidth=bandwidthPermit!==undefined||this.requireBandwidthPermit&&bandwidthPath + if(guardedBandwidth&&bandwidthPermit===undefined)throw new BandwidthAdmissionError("REFUSED") + let nativeBody=body + if(guardedBandwidth){ + if(!body||Object.getPrototypeOf(body)!==Object.prototype)throw new BandwidthAdmissionError("REFUSED") + const descriptors=Object.getOwnPropertyDescriptors(body) + if(Object.values(descriptors).some(value=>!("value" in value)||!value.enumerable||typeof value.value!=="string"))throw new BandwidthAdmissionError("REFUSED") + nativeBody=Object.freeze(Object.fromEntries(Object.entries(descriptors).map(([key,value])=>[key,value.value as string]))) + } + const work = async (leaseSignal?: AbortSignal) => { + const nativeSignal = leaseSignal && signal ? AbortSignal.any([leaseSignal, signal]) : leaseSignal ?? signal + nativeSignal?.throwIfAborted();await beforeDispatch?.();nativeSignal?.throwIfAborted() + // Private dispatcher owns any permit-bound gate through actual socket + // handoff. Public post arguments, mutation audit and response stay intact. + if(guardedBandwidth)return dispatchBandwidthNativePermit(bandwidthPermit,this,path,nativeBody,signal, bind => { + nativeSignal?.throwIfAborted() + return rosPost(this.params,path,nativeBody,timeoutMs,nativeSignal,bind) as Promise + }) + return rosPost(this.params,path,nativeBody,timeoutMs,nativeSignal) as Promise + } + // POST read selectors belong in the body. A suffix in a query string or + // fragment must never exempt a write pathname from the mutation guard. + const readCommand = !/[?#]/.test((this.params.apiPath ?? "/rest") + path) && ( + path.endsWith("/get") || path.endsWith("/print") || path.endsWith("/monitor") || + ["/ping", "/tool/ping", "/tool/traceroute", "/interface/monitor-traffic"].includes(path)) + // The private permit dispatcher independently authenticates exact source, + // body and bandwidth path before sending. This existing id-less diagnostic + // path grants no unscoped configuration write or legacy bandwidth bypass. + if (guardedBandwidth && this.serverId === undefined) return work() + return readCommand ? work() : this.mutation("POST", path, work) + } + + async put(path: string, body: Record, timeoutMs = 15_000, beforeDispatch?:()=>Promise): Promise { + return this.mutation("PUT", path, async signal => { await beforeDispatch?.(); signal?.throwIfAborted(); return rosPut(this.params, path, body, timeoutMs, signal) as Promise }) + } + + async delete(path: string, timeoutMs = 10_000, beforeDispatch?:()=>Promise, onAcknowledgedBody?: (body: string) => void): Promise { + return this.mutation("DELETE", path, async signal => { await beforeDispatch?.(); signal?.throwIfAborted(); return rosDelete(this.params, path, timeoutMs, onAcknowledgedBody, signal) }) + } + + async patch(path: string, body: Record, timeoutMs = 15_000, beforeDispatch?: () => Promise): Promise { + return this.mutation("PATCH", path, async signal => { + await beforeDispatch?.() + signal?.throwIfAborted() + return rosPatch(this.params, path, body, timeoutMs, signal) as Promise }) } - async get(path: string, timeoutMs = 10_000): Promise { - return rosRequest(this.params, path, timeoutMs) as Promise - } - - async post(path: string, body: Record, timeoutMs = 15_000, signal?: AbortSignal): Promise { - return rosPost(this.params, path, body, timeoutMs, signal) as Promise - } - - async put(path: string, body: Record, timeoutMs = 15_000): Promise { - return rosPut(this.params, path, body, timeoutMs) as Promise - } - - async delete(path: string, timeoutMs = 10_000): Promise { - return rosDelete(this.params, path, timeoutMs) - } - - async patch(path: string, body: Record, timeoutMs = 15_000): Promise { - return rosPatch(this.params, path, body, timeoutMs) as Promise - } - // ── typed helpers ────────────────────────────────────────────────────────── async getIdentity(): Promise { @@ -674,9 +852,16 @@ export class MikrotikClient { this.post("/routing/ospf/area/get", { ".id": id, "value-name": "disabled" })) } - async getOspfInterfaceTemplates(): Promise { - return readOspfEffectiveDisabled(await this.get("/routing/ospf/interface-template"), id => - this.post("/routing/ospf/interface-template/get", { ".id": id, "value-name": "disabled" })) + async getOspfInterfaceTemplates(beforeRead: () => Promise = async () => {}): Promise { + await beforeRead() + return readOspfTemplateFlags(await this.get("/routing/ospf/interface-template"), (id, key) => + this.post("/routing/ospf/interface-template/get", { ".id": id, "value-name": key }), beforeRead, async () => { + await beforeRead() + const present = await this.post("/routing/ospf/interface-template/print", { ".proplist": [".id"], ".query": ["passive"] }) + await beforeRead() + const absent = await this.post("/routing/ospf/interface-template/print", { ".proplist": [".id"], ".query": ["passive", "#!"] }) + return { present, absent } + }) } async getOspfInstances(): Promise { @@ -688,9 +873,27 @@ export class MikrotikClient { return this.get("/routing/bfd/session") } - async setOspfInterfaceTemplateCost(templateId: string, cost: number): Promise { + async setOspfInterfaceTemplateCost(templateId: string, cost: number, expectedCost?: number, expectedServerProof?: string, beforeWrite?: () => Promise): Promise { const safeId = templateId.startsWith("*") ? templateId : `*${templateId.replace(/^\*/, "")}` - await this.patch(`/routing/ospf/interface-template/${encodeURIComponent(safeId)}`, { cost: String(cost) }) + const assertSource = async () => { + if (expectedServerProof !== undefined && (this.serverId === undefined || expectedServerProof !== await ospfCostConnectionProof(this.serverId, this.params))) throw new OspfCostWriteError("Источник OSPF изменился после подготовки плана", false) + if (this.serverId === undefined) return + const { pool } = await import("../db/index.js") + const row = (await pool.query("SELECT host,port,use_ssl,verify_ssl,username,password,enabled,routing_endpoint_epoch FROM servers WHERE id=$1", [this.serverId])).rows[0] + if (!row || row.enabled !== true || row.host !== this.params.host || Number(row.port) !== this.params.port || row.use_ssl !== this.params.useSsl || row.verify_ssl !== this.params.verifySsl || row.username !== this.params.username || row.password !== this.params.password || row.routing_endpoint_epoch !== this.params.routingEndpointEpoch) throw new OspfCostWriteError("Параметры подключения OSPF изменились; запись не отправлена", false) + } + const work = async () => { await writeOspfTemplateCost(safeId, cost, { expectedCost, + // The cost writer preserves raw native fields, including omitted defaults. + // It needs no per-row disabled getter and performs two bounded GETs only. + read: async () => { await assertSource(); return this.get("/routing/ospf/interface-template") }, + write: async (id, next) => { + await assertSource() + return this.patch(`/routing/ospf/interface-template/${encodeURIComponent(id)}`, { cost: String(next) },15000,beforeWrite?async()=>{ + try{await beforeWrite()}catch{throw new OspfCostWriteError("Допуск или источники OSPF изменились перед отправкой; запись не отправлена",false)} + await assertSource() + }:undefined) + } }) } + await (this.serverId === undefined ? work() : withApplicationMutationLock(() => withConfigMutationLock([this.serverId!], work))) } // ── extra endpoints for exec route ──────────────────────────────────────── @@ -737,7 +940,7 @@ export class MikrotikClient { address: string, count = 4, interfaceName?: string, - options?: { interval?: string }, + options?: { interval?: string; signal?: AbortSignal; beforeDispatch?: () => Promise }, ): Promise { const body: Record = { address, @@ -746,7 +949,7 @@ export class MikrotikClient { if (options?.interval && options.interval.trim()) body.interval = options.interval.trim() else body.interval = "0.2s" if (interfaceName && interfaceName.trim()) body.interface = interfaceName.trim() - return this.post("/tool/ping", body, 20_000) + return this.post("/tool/ping", body, 20_000, options?.signal, options?.beforeDispatch) } async bandwidthTest(params: { @@ -756,7 +959,9 @@ export class MikrotikClient { protocol?: "tcp" | "udp" direction?: "transmit" | "receive" | "both" durationSec?: number - }, signal?: AbortSignal): Promise>> { + maxMbps?: number + }, signal?: AbortSignal, beforeDispatch?: () => Promise, bandwidthPermit?:BandwidthNativePermit): Promise>> { + if(params.maxMbps!==undefined&&(!Number.isSafeInteger(params.maxMbps)||params.maxMbps<1||params.maxMbps>1000))throw new Error("Лимит скорости должен быть целым числом от 1 до 1000 Мбит/с") const body: Record = { address: params.address, user: params.user, @@ -765,7 +970,8 @@ export class MikrotikClient { direction: params.direction ?? "both", duration: `${Math.max(3, params.durationSec ?? 10)}s`, } - return this.post>>("/tool/bandwidth-test", body, 30_000, signal) + if(params.maxMbps!==undefined){body["local-tx-speed"]=String(params.maxMbps*1000000);body["remote-tx-speed"]=String(params.maxMbps*1000000)} + return this.post>>("/tool/bandwidth-test", body, 30_000, signal, beforeDispatch, bandwidthPermit) } async getCertificates(): Promise>> { @@ -807,25 +1013,48 @@ export class MikrotikClient { } async uploadTextFile(fileName: string, contents: string, timeoutMs = 30_000): Promise { - const normalized = routerFileBasename(fileName) const payload = contents.endsWith("\n") ? contents : `${contents}\n` + return this.resolveUploadedFileName(await this.writeTextFile(routerFileBasename(fileName), payload, timeoutMs)) + } + + async uploadConfigurationScript(fileName: string, contents: string, timeoutMs = 30_000): Promise { + const payload = contents.endsWith("\n") ? contents : `${contents}\n` + const expected = Buffer.from(payload, "utf8") + if (expected.length > MAX_ROS_BODY_BYTES) throw new Error("Файл восстановления RouterOS больше 8 МиБ") + const name = await this.writeTextFile(routerFileBasename(fileName), payload, timeoutMs) + const rows = (await this.listFiles()).filter(row => row.name === name) + const file = rows[0] + if (rows.length !== 1 || !/^\*[a-z0-9]+$/i.test(file.id) || file.size !== expected.length) { + throw new Error("RouterOS не подтвердил имя, ID или размер файла восстановления") + } + const actual = await this.downloadConfigurationFile(file) + if (!actual.equals(expected)) throw new Error("Содержимое файла восстановления RouterOS отличается от резервной копии") + return name + } + + private async writeTextFile(normalized: string, payload: string, timeoutMs: number): Promise { const candidates = [`flash/${normalized}`, normalized] let lastError: unknown for (const name of candidates) { + let uploaded = true try { await this.put("/file", { name, contents: payload }, timeoutMs) - return await this.resolveUploadedFileName(name) } catch (error) { + if (!(error instanceof MikrotikError) || ![404, 405].includes(error.statusCode)) throw error + uploaded = false lastError = error } + if (uploaded) return name try { await this.post("/file/add", { name, contents: payload, type: "file" }, timeoutMs) - return await this.resolveUploadedFileName(name) } catch (error) { + if (!(error instanceof MikrotikError) || ![404, 405].includes(error.statusCode)) throw error lastError = error + continue } + return name } throw lastError instanceof Error @@ -932,6 +1161,67 @@ export class MikrotikClient { return this.readFileContents({ id: entry.id, name: entry.name }, timeoutMs) } + /** Private export reader: exact-ID contents requests; only an explicit + * 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; signal?: AbortSignal; timeoutMs?: number }): Promise { + 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") + 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) + const signal = control.signal ? AbortSignal.any([control.signal, timerSignal]) : timerSignal + const authorize = async () => { + signal.throwIfAborted() + let abort!: () => void + const cancelled = new Promise((_resolve, reject) => { abort = () => reject(signal.reason); signal.addEventListener("abort", abort, { once: true }) }) + try { await Promise.race([control.authorize(), cancelled]); signal.throwIfAborted() } + finally { signal.removeEventListener("abort", abort) } + } + const remaining = () => { signal.throwIfAborted(); const ms = deadline - Date.now(); if (ms <= 0) throw new Error("Private export deadline expired"); return ms } + const metadata = async () => { + await authorize() + const rows = await this.get("/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)) + 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]) + } + const before = await metadata() + const read = async (body: Record) => { + await authorize() + const raw = await rosRawRequest(this.params, { method: "POST", path: "/file/get", body, timeoutMs: remaining(), signal, maxResponseBytes: target.size * 6 + 4096 }) + try { await authorize(); return raw } + catch (error) { raw.fill(0); throw error } + } + let raw: Buffer + try { raw = await read({ ".id": target.id, "value-name": "contents" }) } + catch (error) { + // A known parameter rejection confirms that the contents read did not run. + // Authentication, timeout, broken response, missing item and unknown errors stop. + let unsupported = false + if (error instanceof MikrotikError && [400, 404].includes(error.statusCode)) { + try { const detail = JSON.parse(error.body)?.detail; unsupported = typeof detail === "string" && /^(?:unknown|unsupported) (?:parameter|argument)[: ]+["']?value-name["']?[.!]?$/i.test(detail.trim()) } catch { /* Unknown response is never a compatibility signal. */ } + } + if (!unsupported) throw error + if (await metadata() !== before) throw new Error("Private export metadata changed before compatibility read") + raw = await read({ ".id": target.id, ".proplist": "contents" }) + } + let content: Buffer | undefined, transferred = false + try { + try { content = extractRosContentsField(raw) } + catch { throw new Error("Private export contents response is invalid") } + if (content.length !== target.size) throw new Error("Private export decoded size mismatch") + if (await metadata() !== before) throw new Error("Private export metadata changed during download") + await authorize() + transferred = true + return content + } finally { + raw.fill(0) + if (!transferred) content?.fill(0) + } + } + /** Native text exports larger than /file/get's limit use bounded byte offsets. */ private async downloadConfigurationFile(target: { id: string; name: string; size: number }): Promise { if (!Number.isSafeInteger(target.size) || target.size > MAX_ROS_BODY_BYTES) { diff --git a/backend/src/services/network-disabled-server.test.ts b/backend/src/services/network-disabled-server.test.ts index 8f963cf8..e6343ce6 100644 --- a/backend/src/services/network-disabled-server.test.ts +++ b/backend/src/services/network-disabled-server.test.ts @@ -7,17 +7,35 @@ import { initDatabase } from "../db/bootstrap.js" import { buildApp } from "../index.js" try { - for (const id of ["srv1", "1abc", "", "0", "-1", "1.5", "9007199254740993", "9007199254740992"]) { assert.equal(await getEnabledServerById(id), null); assert.equal(await getServerRowByIdString(id), null) } - const own = process.env.MMAPP_OWN_TEST_DB + const own = process.env.MMAPP_OWN_SQL_DB ?? process.env.MMAPP_OWN_TEST_DB + if (process.env.MMAPP_OWN_SQL_DB && process.env.MMAPP_OWN_TEST_DB) assert.equal(process.env.MMAPP_OWN_SQL_DB, process.env.MMAPP_OWN_TEST_DB) + if (own) { + assert(/^mm_accept_(152|156|166)_\d+$/.test(own)) + assert.equal(process.env.NODE_ENV, "test") + const endpoint = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(endpoint.protocol)) + assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.username, "mcp_test"); assert(endpoint.password) + assert.equal(endpoint.pathname, "/" + own); assert.equal(endpoint.search, ""); assert.equal(endpoint.hash, "") + } + const originalQuery = pool.query, originalConnect = pool.connect + let invalidSql = 0 + pool.query = (() => { invalidSql++; throw Error("Invalid server ID attempted SQL") }) as typeof pool.query + pool.connect = (() => { invalidSql++; throw Error("Invalid server ID attempted checkout") }) as typeof pool.connect + try { + for (const id of ["srv1", "1abc", "", "0", "-1", "1.5", "9007199254740993", "9007199254740992"]) { assert.equal(await getEnabledServerById(id), null); assert.equal(await getServerRowByIdString(id), null) } + assert.equal(invalidSql, 0) + } finally { pool.query = originalQuery; pool.connect = originalConnect } if (own) { - assert(/^mm_accept_(152|156)_\d+$/.test(own)) assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, own) await initDatabase() - const id = Number((await pool.query("INSERT INTO servers (name,host,enabled) VALUES ('disabled-network-fixture','127.0.0.1',false) RETURNING id")).rows[0].id) - const app = await buildApp({ logger: false, startScheduler: false }) + const ownedIds: number[] = [] + let app: Awaited> | undefined const original = MikrotikClient.fromServer let contacts = 0, checks = 0 try { + const id = Number((await pool.query("INSERT INTO servers (name,host,enabled) VALUES ('disabled-network-fixture','127.0.0.1',false) RETURNING id")).rows[0].id) + ownedIds.push(id) + app = await buildApp({ logger: false, startScheduler: false }) MikrotikClient.fromServer = () => { contacts++; throw new Error("Disabled server was contacted") } const key = Buffer.alloc(32, 7).toString("base64") for (const type of ["home-router", "jump-host", "exit-node"]) { @@ -39,15 +57,16 @@ try { { method: "PATCH", path: `/wireguard/interfaces/${id}/*none`, payload: { address: "fd00::1/64" } }, { method: "DELETE", path: `/wireguard/interfaces/${id}/*none`, payload: undefined }, { method: "POST", path: "/wireguard/peers", payload: { serverId: id, interfaceName: "own", publicKey: key, allowedAddresses: ["fd00::2/128"] } }, - { method: "POST", path: "/wireguard/export", payload: { serverId: id, interfaceName: "own", format: "rsc" } }, - { method: "POST", path: "/wireguard/import", payload: { serverId: id, format: "conf", content: `[Interface]\nPrivateKey = ${key}\nAddress = fd00::1/64\n` } }, + { method: "POST", path: "/wireguard/export", payload: { serverId: id, interfaceName: "own", format: "rsc", includePrivateKey: false }, expectedStatus: 409, expectedCode: "DEVICE_UNAVAILABLE" }, + { method: "POST", path: "/wireguard/import", payload: { serverId: id, format: "conf", dryRun: false, content: `[Interface]\nPrivateKey = ${key}\nAddress = fd00::1/64\n` } }, { method: "POST", path: "/gre/tunnels", payload: { serverId: String(id), name: "own", remoteAddress: "192.0.2.1", localInnerIp: "fd00::1/64" } }, { method: "PATCH", path: "/gre/tunnels", payload: { serverId: String(id), name: "own", localInnerIp: "fd00::1/64" } }, { method: "DELETE", path: "/gre/tunnels", payload: { serverId: String(id), name: "own" } }, ] as const for (const item of cases) { const r = await app.inject({ method: item.method, url: "/api" + item.path, payload: item.payload }) - assert.equal(r.statusCode, 404, type + " " + item.path + " " + r.body) + assert.equal(r.statusCode, "expectedStatus" in item ? item.expectedStatus : 404, type + " " + item.path + " " + r.body) + if ("expectedCode" in item) assert.equal(r.json().code, item.expectedCode) checks++ } } @@ -58,12 +77,20 @@ try { assert.equal((await getServerRowByIdString(id))?.id, id) for (const largeId of [2147483648, Number.MAX_SAFE_INTEGER]) { await pool.query("INSERT INTO servers (id,name,host,enabled) VALUES ($1,'large-id-fixture','127.0.0.1',true)", [largeId]) + ownedIds.push(largeId) assert.equal((await getEnabledServerById(String(largeId)))?.id, largeId) assert.equal((await getServerRowByIdString(String(largeId)))?.id, largeId) } assert.equal(contacts, 0) console.log("network-disabled-server.test.ts: 39 API/SQL checks for three types including certificates, zero RouterOS contacts, enabled positive control passed") console.log("network-disabled-server.test.ts: real bigint IDs above int32 and MAX_SAFE_INTEGER passed without rounding") - } finally { MikrotikClient.fromServer = original; await app.close() } - } else console.log("network-disabled-server.test.ts: invalid IDs passed; own SQL/disabled API checks skipped (MMAPP_OWN_TEST_DB required)") + } finally { + MikrotikClient.fromServer = original + try { await app?.close() } + finally { if (ownedIds.length) await pool.query("DELETE FROM servers WHERE id=ANY($1::bigint[])", [ownedIds]) } + } + } else { + assert.notEqual(process.env.REQUIRE_TEST_POSTGRES, "1", "Owned SQL/disabled API checks are mandatory in PostgreSQL regression") + console.log("network-disabled-server.test.ts: invalid IDs passed; optional SQL checks need an explicit owned database") + } } finally { await closePool() } diff --git a/backend/src/services/network-host-resolution.test.ts b/backend/src/services/network-host-resolution.test.ts new file mode 100644 index 00000000..4f71268f --- /dev/null +++ b/backend/src/services/network-host-resolution.test.ts @@ -0,0 +1,89 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { createNetworkRoutes } from "../routes/network.js" +import { createNetworkHostResolver, resolveHostsSchema } from "./network-host-resolution.js" +import { permissionForRequest } from "../lib/permissions.js" + +assert.equal(permissionForRequest("POST", "/api/network/resolve-hosts"), "mm:network:write") + +for (const host of ["https://example.com", "example.com:80", "x\ny", "../x", "a;b", "__proto__", "a..b", "-bad", "bad-", "256.1.1.1", "001.2.3.4", "", "x".repeat(64) + ".test"]) assert.equal(resolveHostsSchema.safeParse({ hosts: [host] }).success, false, host) +assert.equal(resolveHostsSchema.safeParse({ hosts: Array(33).fill("127.0.0.1") }).success, false) +let calls = 0, clock = Date.now() +const resolver = createNetworkHostResolver({ now: () => clock, cacheLimit: 1, lookup: async () => { calls++; return { address: "192.0.2.1", family: 4 } } }) +let result = await resolver({ hosts: [" Example.Test ", "EXAMPLE.test", "127.0.0.1"] }) +assert.equal(calls, 1); assert.equal(Object.keys(result.results).length, 2); assert.equal(result.observations["127.0.0.1"].state, "literal") +assert.equal(result.routerResolverVerified, false); assert.equal(result.forwardingVerified, false) +assert.equal((await resolver({ hosts: ["::1"] })).observations["::1"].state, "unsupported-family"); assert.equal(calls, 1) +let idnCalls = 0 +const idn = createNetworkHostResolver({ lookup: async host => { idnCalls++; assert.equal(host, "xn--e1afmkfd.xn--p1ai"); return { address: "192.0.2.10", family: 4 } } }) +assert.equal((await idn({ hosts: ["пример.рф", "xn--e1afmkfd.xn--p1ai"] })).results["пример.рф"], "192.0.2.10"); assert.equal(idnCalls, 1) +assert.equal((await resolver({ hosts: ["example.test"] })).observations["example.test"].source, "cache"); assert.equal(calls, 1) +await resolver({ hosts: ["other.test"] }); await resolver({ hosts: ["example.test"] }); assert.equal(calls, 3) +clock += 300001; await resolver({ hosts: ["example.test"] }); assert.equal(calls, 4) +result = await resolver({ hosts: ["constructor"] }); assert.equal(Object.getPrototypeOf(result.results), null); assert.equal(result.results.constructor, "192.0.2.1") +let negativeCalls = 0 +const negative = createNetworkHostResolver({ lookup: async () => { negativeCalls++; throw Error("NEVER_DNS_PROVIDER_SECRET") } }) +assert.equal((await negative({ hosts: ["unresolved.test"] })).observations["unresolved.test"].state, "unresolved") +assert.equal((await negative({ hosts: ["unresolved.test"] })).observations["unresolved.test"].source, "cache"); assert.equal(negativeCalls, 1) +assert.ok(!JSON.stringify(await negative({ hosts: ["unresolved.test"] })).includes("NEVER_")) +assert.equal((await createNetworkHostResolver({ lookup: async () => ({ address: "::1", family: 6 }) })({ hosts: ["wrong-family.test"] })).results["wrong-family.test"], null) + +let release!: (value: { address: string; family: number }) => void, pendingCalls = 0 +const pending = new Promise<{ address: string; family: number }>(resolve => { release = resolve }) +const bounded = createNetworkHostResolver({ timeoutMs: 10, maxPending: 1, lookup: async host => { pendingCalls++; return host === "held.test" ? pending : { address: "192.0.2.2", family: 4 } } }) +result = await bounded({ hosts: ["held.test", "second.test"] }) +assert.equal(result.observations["held.test"].state, "timeout"); assert.equal(result.observations["second.test"].state, "busy"); assert.equal(pendingCalls, 1) +release({ address: "192.0.2.3", family: 4 }); await delay(0) +assert.equal((await bounded({ hosts: ["held.test"] })).observations["held.test"].source, "cache") +assert.equal((await bounded({ hosts: ["second.test"] })).results["second.test"], "192.0.2.2"); assert.equal(pendingCalls, 2) + +let dedupCalls = 0, complete!: (value: { address: string; family: number }) => void +const shared = new Promise<{ address: string; family: number }>(resolve => { complete = resolve }) +const dedup = createNetworkHostResolver({ timeoutMs: 1000, lookup: async () => { dedupCalls++; return shared } }) +const reads = [dedup({ hosts: ["same.test"] }), dedup({ hosts: ["SAME.test"] })] +await delay(0); assert.equal(dedupCalls, 1); complete({ address: "192.0.2.4", family: 4 }); await Promise.all(reads) + +let permitted = true, gatedCalls = 0, allow!: (value: { address: string; family: number }) => void +const gate = new Promise<{ address: string; family: number }>(resolve => { allow = resolve }) +const guarded = createNetworkHostResolver({ lookup: async () => { gatedCalls++; return gate } }) +const read = guarded({ hosts: ["first.test", "next.test"] }, { assertReadable: async () => { if (!permitted) throw Error("Access revoked") } }).then(() => null, error => error) +await delay(0); assert.equal(gatedCalls, 1); permitted = false; allow({ address: "192.0.2.5", family: 4 }); assert.match(String(await read), /Access revoked/); assert.equal(gatedCalls, 1) +const cancelled = new AbortController(); cancelled.abort() +await assert.rejects(guarded({ hosts: ["next.test"] }, { signal: cancelled.signal })); assert.equal(gatedCalls, 1) +let budgetClock = 0, budgetCalls = 0 +const budgeted = createNetworkHostResolver({ now: () => budgetClock, totalTimeoutMs: 10, lookup: async () => { budgetCalls++; budgetClock += 11; return new Promise(() => {}) } }) +result = await budgeted({ hosts: ["budget-one.test", "budget-two.test", "budget-three.test"] }) +assert.equal(budgetCalls, 1); assert.ok(Object.values(result.observations).every(row => row.state === "timeout")) +let settleAbort!: (value: { address: string; family: number }) => void, abortCalls = 0 +const abortGate = new Promise<{ address: string; family: number }>(resolve => { settleAbort = resolve }) +const abortResolver = createNetworkHostResolver({ maxPending: 1, lookup: async () => { abortCalls++; return abortGate } }) +const midAbort = new AbortController(), abortRead = abortResolver({ hosts: ["abort-one.test", "abort-two.test"] }, { signal: midAbort.signal }).then(() => null, error => error) +await delay(0); assert.equal(abortCalls, 1); midAbort.abort(); assert.match(String(await abortRead), /cancelled/) +assert.equal((await abortResolver({ hosts: ["abort-two.test"] })).observations["abort-two.test"].state, "busy"); assert.equal(abortCalls, 1) +settleAbort({ address: "192.0.2.6", family: 4 }); await delay(0) + +const app = Fastify(); await app.register(createNetworkRoutes(resolver), { prefix: "/api" }) +const beforeInvalid = calls +assert.equal((await app.inject({ method: "POST", url: "/api/network/resolve-hosts", payload: { hosts: ["http://invalid.test"] } })).statusCode, 400); assert.equal(calls, beforeInvalid) +const http = await app.inject({ method: "POST", url: "/api/network/resolve-hosts", payload: { hosts: ["127.0.0.1", "127.0.0.1"] } }) +assert.equal(http.statusCode, 200); assert.deepEqual(http.json(), { results: { "127.0.0.1": "127.0.0.1" } }); await app.close() +let httpCalls = 0, started!: () => void, finishHttp!: (value: { address: string; family: number }) => void +const httpStarted = new Promise(resolve => { started = resolve }) +const httpGate = new Promise<{ address: string; family: number }>(resolve => { finishHttp = resolve }) +const httpResolver = createNetworkHostResolver({ maxPending: 1, lookup: async () => { httpCalls++; started(); return httpGate } }) +const live = Fastify(); await live.register(createNetworkRoutes(httpResolver), { prefix: "/api" }) +const url = await live.listen({ host: "127.0.0.1", port: 0 }) +const disconnect = new AbortController() +try { + const request = fetch(url + "/api/network/resolve-hosts", { method: "POST", headers: { "content-type": "application/json", connection: "close" }, body: JSON.stringify({ hosts: ["http-first.test", "http-next.test"] }), signal: disconnect.signal }).then(() => null, error => error) + let startTimer: ReturnType | undefined + try { await Promise.race([httpStarted, new Promise((_resolve, reject) => { startTimer = setTimeout(() => reject(new Error("REST request did not reach DNS lookup")), 5000) })]) } + finally { clearTimeout(startTimer) } + disconnect.abort(); assert.ok(await request) + await delay(20) + assert.equal(httpCalls, 1) + assert.equal((await httpResolver({ hosts: ["http-next.test"] })).observations["http-next.test"].state, "busy") + finishHttp({ address: "192.0.2.7", family: 4 }); await delay(0) +} finally { disconnect.abort(); finishHttp({ address: "192.0.2.7", family: 4 }); live.server.closeAllConnections(); await live.close() } +console.log("Host resolution: strict IPv4/hostname, dedup, bounded cache/TTL, safe unknown, no prototype/provider payload, retained pending capacity after timeout, shared in-flight lookup, revoke/cancel and actual REST parity; no external DNS") diff --git a/backend/src/services/network-host-resolution.ts b/backend/src/services/network-host-resolution.ts new file mode 100644 index 00000000..17b73cc6 --- /dev/null +++ b/backend/src/services/network-host-resolution.ts @@ -0,0 +1,73 @@ +import { promises as dns } from "node:dns" +import { isIP } from "node:net" +import { domainToASCII } from "node:url" +import { z } from "zod" + +const validHost = (host: string) => { + if (isIP(host)) return true + if (!/^[\p{L}\p{M}\p{N}.-]+$/u.test(host)) return false + const ascii = domainToASCII(host) + return !/^[\d.]+$/.test(ascii) && /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*\.?$/i.test(ascii) +} +export const resolveHostsSchema = z.object({ hosts: z.array(z.string().trim().min(1).max(253).refine(validHost, "Нужен literal IP или DNS hostname без URL/порта")).max(32) }).strict() +type Lookup = (host: string) => Promise<{ address: string; family: number }> +type State = "literal" | "resolved" | "unresolved" | "timeout" | "busy" | "unsupported-family" +type Observation = { address: string | null; state: State; source: "literal" | "cache" | "manager-system-resolver"; observedAt: string; cacheExpiresAt: string | null } +type Cached = { address: string | null; observedAt: string; expires: number } +export type ResolveHostsOptions = { signal?: AbortSignal; assertReadable?: () => Promise } + +function boundedWait(pending: Promise, timeoutMs: number, signal?: AbortSignal): Promise { + return new Promise((resolve, reject) => { + const finish = (value: T | null, cancelled = false) => { clearTimeout(timer); signal?.removeEventListener("abort", abort); if (cancelled) reject(new Error("Network resolution cancelled")); else resolve(value) } + const abort = () => finish(null, true) + const timer = setTimeout(() => finish(null), Math.max(0, timeoutMs)) + signal?.addEventListener("abort", abort, { once: true }) + if (signal?.aborted) abort() + // OS lookup cannot be cancelled. Its pending slot remains occupied until + // actual settlement; a caller timeout must not release that capacity. + void pending.then(value => finish(value), () => finish(null)) + }) +} + +export function createNetworkHostResolver(options: { lookup?: Lookup; now?: () => number; timeoutMs?: number; totalTimeoutMs?: number; cacheLimit?: number; maxPending?: number } = {}) { + const lookup = options.lookup ?? (host => dns.lookup(host, { family: 4 })), now = options.now ?? Date.now + const cache = new Map(), pending = new Map>() + const timeoutMs = options.timeoutMs ?? 2000, totalTimeoutMs = options.totalTimeoutMs ?? 10000, cacheLimit = options.cacheLimit ?? 512, maxPending = options.maxPending ?? 8 + const remember = (host: string, address: string | null) => { + const row = { address, observedAt: new Date(now()).toISOString(), expires: now() + 300000 } + cache.delete(host); cache.set(host, row) + while (cache.size > cacheLimit) cache.delete(cache.keys().next().value!) + return row + } + return async (input: unknown, context: ResolveHostsOptions = {}) => { + const { hosts } = resolveHostsSchema.parse(input), unique = [...new Set(hosts.map(host => host.toLowerCase()))] + const results: Record = Object.create(null), observations: Record = Object.create(null), deadline = now() + totalTimeoutMs + const guard = async () => { if (context.signal?.aborted) throw new Error("Network resolution cancelled"); await context.assertReadable?.() } + for (const host of unique) { + await guard() + let observation: Observation + const key = isIP(host) ? host : domainToASCII(host).toLowerCase() + const cached = cache.get(key) + if (isIP(host) === 4) observation = { address: host, state: "literal", source: "literal", observedAt: new Date(now()).toISOString(), cacheExpiresAt: null } + else if (isIP(host) === 6) observation = { address: null, state: "unsupported-family", source: "literal", observedAt: new Date(now()).toISOString(), cacheExpiresAt: null } + else if (cached && cached.expires > now()) observation = { address: cached.address, state: cached.address ? "resolved" : "unresolved", source: "cache", observedAt: cached.observedAt, cacheExpiresAt: new Date(cached.expires).toISOString() } + else if (now() >= deadline) observation = { address: null, state: "timeout", source: "manager-system-resolver", observedAt: new Date(now()).toISOString(), cacheExpiresAt: null } + else { + cache.delete(key) + let active = pending.get(key) + if (!active && pending.size < maxPending) { + active = Promise.resolve().then(() => lookup(key)).then(result => remember(key, result?.family === 4 && typeof result.address === "string" && isIP(result.address) === 4 ? result.address : null), () => remember(key, null)).finally(() => pending.delete(key)) + pending.set(key, active) + } + const result = active ? await boundedWait(active, Math.min(timeoutMs, deadline - now()), context.signal) : null + observation = { address: result?.address ?? null, state: !active ? "busy" : !result ? "timeout" : result.address ? "resolved" : "unresolved", source: "manager-system-resolver", + observedAt: result?.observedAt ?? new Date(now()).toISOString(), cacheExpiresAt: result ? new Date(result.expires).toISOString() : null } + } + await guard() + results[host] = observation.address; observations[host] = observation + } + await guard() + return { results, observations, resolverLocation: "manager-host", family: "ipv4", cacheTtlMeaning: "manager-policy-not-DNS-record-TTL", routerResolverVerified: false, addressOwnershipVerified: false, forwardingVerified: false } + } +} +export const resolveNetworkHosts = createNetworkHostResolver() diff --git a/backend/src/services/network-observed-read.test.ts b/backend/src/services/network-observed-read.test.ts new file mode 100644 index 00000000..abbf9c41 --- /dev/null +++ b/backend/src/services/network-observed-read.test.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { MikrotikClient } from "./mikrotik.js" +import { pollWireGuardServers } from "./wireguard-live.js" +import { pollIpsecServers, fetchIpsecState } from "./ipsec-live.js" +import { maskNetworkSecrets, nativeNetworkRows } from "./network-observed-read.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" + +const server = { id: 1, name: "Router", enabled: true } as ServerRow +const original = MikrotikClient.fromServer +let requests = 0, broken = false, duplicate = false, known = false +const privateKey = "wireguard-private-fixture", psk = "ipsec-psk-fixture" +const rows = (path: string) => { + if (broken) throw new Error("upstream failed") + if (path === "/interface/wireguard") { + const row = { ".id": "*1", name: "wg1", "private-key": privateKey, comment: privateKey, ...(known ? { disabled: "false", running: "false", "listen-port": "13231", mtu: "1420" } : {}) } + return duplicate ? [row, row] : [row] + } + if (path === "/interface/wireguard/peers") return [{ ".id": "*2", interface: "*1", "public-key": "public", rx: "0", tx: "12bad", disabled: "false", "persistent-keepalive": "0", "preshared-key": psk, name: psk }] + if (path === "/ip/address") return [{ ".id": "*a", interface: "wg1", address: "192.0.2.1/24", disabled: "false", dynamic: "false" }] + return [] +} +try { + MikrotikClient.fromServer = () => ({ get: async (path: string) => { requests++; return rows(path) } }) as unknown as MikrotikClient + const unknown = await pollWireGuardServers([server]) + assert.equal(requests, 4); assert.deepEqual(unknown.failures, []) + assert.equal(unknown.observed[0].enabled, null); assert.equal(unknown.observed[0].status, "unknown"); assert.equal(unknown.observed[0].mtu, null) + assert.equal(unknown.observed[0].peers[0].transferRx, 0); assert.equal(unknown.observed[0].peers[0].transferTx, null) + assert.equal(unknown.observed[0].peers[0].persistentKeepalive, 0); assert.equal(unknown.observed[0].peers[0].persistent, false) + assert.equal(unknown.observed[0].address, "192.0.2.1/24") + assert.ok(!JSON.stringify(unknown.observed).includes(privateKey)); assert.ok(!JSON.stringify(unknown.observed).includes(psk)) + assert.equal("privateKey" in unknown.observed[0], false) + known = true + const present = await pollWireGuardServers([server]); assert.equal(present.observed[0].enabled, true); assert.equal(present.observed[0].status, "down"); assert.equal(present.observed[0].listenPort, 13231) + const beforeDisabled = requests + assert.deepEqual((await pollWireGuardServers([{ ...server, enabled: false }])).observed, []); assert.equal(requests, beforeDisabled) + duplicate = true; assert.equal((await pollWireGuardServers([server])).failures.length, 1); duplicate = false + broken = true; assert.equal((await pollWireGuardServers([server])).observed.length, 0) + assert.deepEqual(maskNetworkSecrets({ title: "prefix " + psk, nested: [privateKey] }, [psk, privateKey]), { title: "[redacted]", nested: ["[redacted]"] }) + for (const invalid of [[{ ".id": "*1" }, { ".id": "*1" }], [null], {}]) assert.throws(() => nativeNetworkRows(invalid)) + const emptyReader = { get: async () => [] } as unknown as MikrotikClient + const result = await pollIpsecServers([server], source => fetchIpsecState(source, emptyReader)) + assert.deepEqual(result.failures, []); assert.equal(result.observed.servers[0].clientsOnline, 0); assert.equal(result.observed.servers[0].dataPlaneVerified, false) + const sessionReader = (active: Record[]) => ({ get: async (path: string) => path === "/ip/ipsec/identity" ? [{ ".id": "*i", "auth-method": "pre-shared-key", "remote-id": "key-id:alice", comment: "MikrotikManager:ipsec user=Alice" }] : path === "/ip/ipsec/active-peers" ? active : [] }) as unknown as MikrotikClient + const unknownSession = await pollIpsecServers([server], source => fetchIpsecState(source, sessionReader([{ ".id": "*s", id: "alice", state: "future-unknown-state" }]))) + assert.equal(unknownSession.observed.clients[0].online, null); assert.equal(unknownSession.observed.servers[0].unknownSessionStates, 1) + const knownSession = await pollIpsecServers([server], source => fetchIpsecState(source, sessionReader([{ ".id": "*s", id: "alice", state: "established" }, { ".id": "*other", id: "other", state: "future-unknown-state" }]))) + assert.equal(knownSession.observed.clients[0].online, true) +} finally { MikrotikClient.fromServer = original } +console.log("Observed VPN reads: scoped sources, stable identity, nullable flags/counters, known zero, native parent IDs, secret reflection masking, duplicates/failures and empty IPsec passed") diff --git a/backend/src/services/network-observed-read.ts b/backend/src/services/network-observed-read.ts new file mode 100644 index 00000000..85c4eb63 --- /dev/null +++ b/backend/src/services/network-observed-read.ts @@ -0,0 +1,41 @@ +import { observedBoolean, observedInteger } from "./ospf-observed-values.js" +import type { WgIfaceDto } from "@mmapp/contracts/wireguard" +import { isIP } from "node:net" + +export type NativeNetworkRow = Record +export function networkSecretValues(rows: readonly NativeNetworkRow[]) { + return rows.flatMap(row => ["private-key", "preshared-key", "secret", "password"].flatMap(key => { + const value = row[key] + return value && !(key === "private-key" && ["true", "false", "yes", "no"].includes(value)) ? [value] : [] + })) +} +export function nativeNetworkRows(raw: unknown, required = [".id"]): NativeNetworkRow[] { + if (!Array.isArray(raw) || raw.length > 10000 || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || required.some(key => typeof row[key] !== "string" || !row[key].trim()))) throw new Error("Недостоверный сетевой каталог RouterOS") + if (new Set(raw.map(row => row[".id"])).size !== raw.length) throw new Error("Повторяющиеся native ID сетевого каталога") + return raw as NativeNetworkRow[] +} +/** The upstream may reflect a known key in an ordinary title/comment field. */ +export function maskNetworkSecrets(value: T, secrets: string[]): T { + const keys = [...new Set(secrets.filter(Boolean))] + const visit = (input: unknown): unknown => typeof input === "string" ? keys.some(key => input.includes(key)) ? "[redacted]" : input + : Array.isArray(input) ? input.map(visit) : input && typeof input === "object" ? Object.fromEntries(Object.entries(input).map(([key, item]) => [key, visit(item)])) : input + return visit(value) as T +} +export function observeWireGuardInterface(dto: WgIfaceDto, raw: NativeNetworkRow, peers: NativeNetworkRow[], addresses: NativeNetworkRow[]) { + const disabled = observedBoolean(raw.disabled), running = observedBoolean(raw.running) + const { privateKey: _privateKey, ...publicDto } = dto + const stableAddresses = addresses.filter(row => { + const [ip, prefix] = (row.address ?? "").split("/"), family = isIP(ip) + return row.interface === raw.name && observedBoolean(row.disabled) === false && observedBoolean(row.dynamic) === false && observedBoolean(row.invalid) !== true && !!family && /^\d+$/.test(prefix ?? "") && Number(prefix) <= (family === 4 ? 32 : 128) + }) + return { ...publicDto, listenPort: observedInteger(raw["listen-port"], 1, 65535), mtu: observedInteger(raw.mtu, 68, 65535), + enabled: disabled === null ? null : !disabled, running, status: disabled === null || !disabled && running === null ? "unknown" : disabled || !running ? "down" : "up", + address: stableAddresses.some(row => row.address === dto.address) ? dto.address : null, + addressStateVerified: stableAddresses.some(row => row.address === dto.address && observedBoolean(row.invalid) === false), + peers: dto.peers.map(peer => { + const source = peers.find(row => row[".id"] === peer.rosId)! + const interval = observedInteger(source["persistent-keepalive"], 0, 65535) + return { ...peer, disabled: observedBoolean(source.disabled), transferRx: peer.transferRx ?? null, transferTx: peer.transferTx ?? null, persistentKeepalive: interval, persistent: interval === null ? null : interval > 0 } + }) } +} +export type ObservedWireGuardInterface = ReturnType diff --git a/backend/src/services/operation-config-lock-lease.sql-helper.ts b/backend/src/services/operation-config-lock-lease.sql-helper.ts new file mode 100644 index 00000000..869556b1 --- /dev/null +++ b/backend/src/services/operation-config-lock-lease.sql-helper.ts @@ -0,0 +1,178 @@ +/** Owned PostgreSQL acceptance. Only advisory locks are created; no table, + * RouterOS, enrollment, native cancellation or atomic-owner-barrier claim. */ +import assert from "node:assert/strict" +import { AsyncResource } from "node:async_hooks" +import { randomInt, randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import type { PoolClient, QueryResult } from "pg" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +export async function acceptOperationLockLifetimeSql() { + assertOwnedBandwidthSql() + const { Pool, Client } = await import("pg") + const { withOperationConfigurationLockClient, borrowOperationConfigurationLockClient, assertOperationConfigurationLockScope } = await import("./operation-config-lock-lease.js") + const { withApplicationMutationLock, withConfigMutationLock } = await import("./config-mutation-lock.js") + const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 4, connectionTimeoutMillis: 3000, query_timeout: 5000 }) + pool.on("error", () => {}) + const admin = new Client({ connectionString: process.env.DATABASE_URL, connectionTimeoutMillis: 3000, query_timeout: 5000 }) + const independent = new AsyncResource("owned-operation-lock-outsider") + const releaseGates: Array<() => void> = [], checks: string[] = [] + const ids = [700000000 + randomInt(10000000), 720000000 + randomInt(10000000)] + const acquire = "SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired" + const unlock = "SELECT pg_advisory_unlock(hashtextextended($1,0))" + const gate = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); releaseGates.push(resolve); return { promise, resolve } } + type Seen = { text: string; values: unknown[] | undefined; result: QueryResult } + type Owner = { pid: number; signal: AbortSignal; releases: boolean[]; queries: Array<{ text: string; values?: unknown[] }>; errorEvents: number } + const owners: Owner[] = [] + const countLocks = async (pid: number) => Number((await admin.query("SELECT count(*) AS n FROM pg_catalog.pg_locks WHERE locktype='advisory' AND granted AND pid=$1", [pid])).rows[0].n) + const clean = async (pid: number) => { + for (const until = Date.now() + 3000; Date.now() < until;) { if (await countLocks(pid) === 0) return; await delay(10) } + assert.fail("Owned advisory locks did not disappear after release/destruction") + } + const run = async (body: (owner: Owner) => Promise, afterQuery?: (seen: Seen) => Promise) => { + const client = await pool.connect(), originalQuery = client.query + const owner: Owner = { pid: 0, signal: undefined as unknown as AbortSignal, releases: [], queries: [], errorEvents: 0 } + const controller = new AbortController(); owner.signal = controller.signal; owners.push(owner) + let released = false, locked = false + const operationKey = "mcp-operation:owned-lock-lifetime:" + randomUUID() + // Instance-local observer only: ALWAYS await the genuine PostgreSQL result + // first. Faults hold/reject that ACK; no SQL result row is synthesized. + client.query = function (...args: unknown[]) { + const text = String(args[0]), values = Array.isArray(args[1]) ? [...args[1]] : undefined + owner.queries.push({ text, values }) + const result = Reflect.apply(originalQuery, client, args) as Promise + return result.then(async actual => { await afterQuery?.({ text, values, result: actual }); return actual }) + } as PoolClient["query"] + const retire = () => { + if (released) return + released = true; owner.releases.push(true); controller.abort() + try { client.release(true) } catch { /* Never return an uncertain owner healthy. */ } + } + const onError = () => { owner.errorEvents++; retire() } + client.on("error", onError) + try { + owner.pid = Number((await client.query("SELECT pg_catalog.pg_backend_pid() AS pid")).rows[0].pid) + assert(Number.isSafeInteger(owner.pid) && owner.pid > 0) + try { locked = (await client.query("SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS locked", [operationKey])).rows[0].locked } + catch (error) { retire(); throw error } + assert(locked); assert(!released) + return await withOperationConfigurationLockClient(client, retire, controller.signal, () => body(owner)) + } finally { + if (!released && locked) { + try { await client.query(unlock, [operationKey]) } catch { retire() } + } + if (!released) { + client.removeListener("error", onError); client.query = originalQuery + released = true; owner.releases.push(false); client.release() + } + } + } + const latest = () => { assert(owners.length); return owners[owners.length - 1] } + const assertDestroyed = async (owner = latest()) => { assert.deepEqual(owner.releases, [true]); await clean(owner.pid) } + const nested = (work: () => Promise) => withApplicationMutationLock(() => withConfigMutationLock(ids, work)) + try { + await admin.connect() + await run(async owner => nested(async () => { + const locks = (await admin.query("SELECT pid FROM pg_catalog.pg_locks WHERE locktype='advisory' AND granted AND pid=$1", [owner.pid])).rows + assert.equal(locks.length, 4); assert(locks.every(row => Number(row.pid) === owner.pid)) + // No second owner connection is required for nested reentrant calls. + await withApplicationMutationLock(() => withConfigMutationLock(ids, async () => { assert.equal(await countLocks(owner.pid), 4) })) + const spare = await Promise.allSettled(Array.from({ length: 3 }, () => pool.connect())) + try { assert(spare.every(result => result.status === "fulfilled")) } + finally { for (const result of spare) if (result.status === "fulfilled") result.value.release() } + })) + assert.deepEqual(latest().releases, [false]); await clean(latest().pid) + checks.push("nested application/two-server/operation advisory locks share one genuine backend; all three spare pool4 slots available and exact unlock cleanup") + + let outsiderWork = 0 + await run(async owner => nested(async () => { + await assert.rejects(independent.runInAsyncScope(() => withConfigMutationLock([ids[0]], async () => { outsiderWork++ }))) + const result = await admin.query("SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired", ["mm-config:" + ids[0]]) + if (result.rows[0].acquired) await admin.query(unlock, ["mm-config:" + ids[0]]) + assert.equal(result.rows[0].acquired, false); assert.equal(await countLocks(owner.pid), 4) + })) + assert.equal(outsiderWork, 0); await clean(latest().pid) + checks.push("standalone genuine configuration-lock caller and independent admin backend conflict with the owned server lock; no caller enters protected work") + + await assert.rejects(run(async () => nested(async () => { throw Error("Owned normal work failure") }))) + const normal = latest(), unlocks = normal.queries.filter(row => row.text.includes("pg_advisory_unlock")) + assert.equal(unlocks.length, 4); assert.deepEqual(normal.releases, [false]); await clean(normal.pid) + checks.push("normal work rejection executes both server unlocks, application unlock and operation unlock before exactly one healthy owner release") + + let initialAcquired = false, initialWork = 0 + await assert.rejects(run(async () => { initialWork++ }, async seen => { + if (seen.values?.[0] && String(seen.values[0]).startsWith("mcp-operation:")) { + assert.equal(seen.result.rows[0].locked, true); initialAcquired = true + assert.equal(await countLocks(latest().pid), 1) + throw Error("Owned lost operation acquire acknowledgement") + } + })) + assert(initialAcquired); assert.equal(initialWork, 0); assert.equal(latest().errorEvents, 0); await assertDestroyed() + checks.push("real operation acquire followed by rejected ACK without error event destroys once and leaves no stranded operation lock") + + { + const entered = gate(), held = gate(); let pending: Promise | undefined, accepted = false, realAcquired = false, exitAt = 0 + await assert.rejects(run(async owner => { + const client = borrowOperationConfigurationLockClient()! + pending = client.query(acquire, ["mm-config:" + ids[0]]).then(value => { accepted = true; return value }); void pending.catch(() => {}) + await Promise.race([entered.promise, pending]); assert.equal(await countLocks(owner.pid), 2) + exitAt = Date.now() + // Deliberately return while a real acquired lock's ACK is withheld. + }, async seen => { + if (seen.text === acquire) { assert.equal(seen.result.rows[0].acquired, true); realAcquired = true; entered.resolve(); await held.promise } + })) + assert(realAcquired); assert(exitAt > 0 && Date.now() - exitAt < 1000); await assert.rejects(pending!); await assertDestroyed() + const physical = latest().queries.length; held.resolve(); await delay(20) + assert.equal(accepted, false); assert.equal(latest().queries.length, physical) + checks.push("scope exit promptly rejects a withheld genuine lock ACK and destroys ownership; late actual result cannot dispatch SQL or become accepted") + } + + { + const entered = gate(), held = gate(); let firstAccepted = false, realAcquired = false + await assert.rejects(run(async () => { + const first = borrowOperationConfigurationLockClient()!, second = borrowOperationConfigurationLockClient()! + const pending = first.query(acquire, ["mm-config:" + ids[0]]).then(value => { firstAccepted = true; return value }); void pending.catch(() => {}) + await Promise.race([entered.promise, pending]) + await assert.rejects(second.query(acquire, ["mm-config:" + ids[1]])); await assert.rejects(pending) + first.release(true); second.release(true) + }, async seen => { + if (seen.text === acquire) { assert.equal(seen.result.rows[0].acquired, true); realAcquired = true; entered.resolve(); await held.promise } + })) + assert(realAcquired); assert.equal(latest().queries.filter(row => row.text === acquire).length, 1); await assertDestroyed() + held.resolve(); await delay(20); assert.equal(firstAccepted, false) + checks.push("concurrent borrower refuses before a second physical SQL; first real acquired lock's late ACK never authorizes work and owner destroys once") + } + + { + const entered = gate(), child = gate(); let lateWork = 0, pending: Promise | undefined + await assert.rejects(run(async owner => { + pending = nested(async () => { + entered.resolve(); await child.promise + return withApplicationMutationLock(() => withConfigMutationLock(ids, async () => { lateWork++ })) + }); void pending.catch(() => {}) + await Promise.race([entered.promise, pending]); assert.equal(await countLocks(owner.pid), 4) + })) + await assertDestroyed(); const physical = latest().queries.length + child.resolve(); await assert.rejects(pending!); assert.equal(lateWork, 0); assert.equal(latest().queries.length, physical) + assert.deepEqual(latest().releases, [true]) + checks.push("dangling genuine application/server facades destroy on outer exit; late inherited fast paths refuse before work/SQL, with no duplicate release") + } + + await assert.rejects(run(async owner => nested(async () => { + const aborted = new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(Error("Owned backend termination did not invalidate owner")), 3000) + owner.signal.addEventListener("abort", () => { clearTimeout(timer); resolve() }, { once: true }) + }) + const result = await admin.query("SELECT pg_catalog.pg_terminate_backend($1) AS terminated", [owner.pid]) + assert.equal(result.rows[0].terminated, true); await aborted + assert.throws(assertOperationConfigurationLockScope) + }))) + assert(latest().errorEvents >= 1); await assertDestroyed() + checks.push("independent admin terminates only the owned backend; real socket error aborts the owner, rejects inherited authorization and frees every advisory lock exactly once") + return { checks, cases: checks.length, poolMax: 4, nativeRequests: 0, applicationRowsWritten: 0, atomicOwnerBarrier: false } + } finally { + for (const resolve of releaseGates) resolve() + independent.emitDestroy() + try { await pool.end() } finally { await admin.end() } + } +} diff --git a/backend/src/services/operation-config-lock-lease.sql.test.ts b/backend/src/services/operation-config-lock-lease.sql.test.ts new file mode 100644 index 00000000..6206adbf --- /dev/null +++ b/backend/src/services/operation-config-lock-lease.sql.test.ts @@ -0,0 +1,20 @@ +import {assertOwnedBandwidthSql,prepareOwnedBandwidthState} from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql();await prepareOwnedBandwidthState() +const {pool,closePool}=await import("../db/index.js") +const {applySqlMigrations}=await import("../db/migrate.js") +const ids:number[]=[] +try{ + await applySqlMigrations(pool) + const {acceptOperationLockLifetimeSql}=await import("./operation-config-lock-lease.sql-helper.js") + console.log("Owned operation lock lifetime: "+JSON.stringify(await acceptOperationLockLifetimeSql())) + for(let index=0;index<2;index++)ids.push(Number((await pool.query("INSERT INTO public.servers(name,host,enabled) VALUES('owned-routing-pool','127.0.0.1',false) RETURNING id")).rows[0].id)) + const {acceptRoutingMeasurementPoolSql}=await import("../modules/mcp/routing-measurement-pool.sql-helper.js") + const routing=await acceptRoutingMeasurementPoolSql(ids) + console.log("Owned operation lock routing: "+JSON.stringify(routing)) + const {acceptAutomaticSchedulerSql}=await import("./scheduler-automatic-root.sql-helper.js") + console.log("Owned automatic scheduler: "+JSON.stringify(await acceptAutomaticSchedulerSql(ids))) +}finally{ + try{if(ids.length)await pool.query("DELETE FROM public.servers WHERE id=ANY($1::bigint[])",[ids])} + finally{await closePool()} +} diff --git a/backend/src/services/operation-config-lock-lease.test.ts b/backend/src/services/operation-config-lock-lease.test.ts new file mode 100644 index 00000000..cbef6780 --- /dev/null +++ b/backend/src/services/operation-config-lock-lease.test.ts @@ -0,0 +1,162 @@ +/** Only the pure lifetime module is imported. No pool/config/runtime. */ +import assert from "node:assert/strict" +import { test } from "node:test" +import { readFileSync } from "node:fs" +import { fileURLToPath } from "node:url" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import type { PoolClient } from "pg" +import { assertOperationConfigurationLockScope,assertOperationConfigurationLockCheckpoint,borrowOperationConfigurationLockClient,withOperationConfigurationLockClient } from "./operation-config-lock-lease.js" +const acquire="SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired" +const unlock="SELECT pg_advisory_unlock(hashtextextended($1,0))" +test("accessor or decorated argument arrays refuse without evaluating getters or dispatching SQL",async()=>{ + const f=fixture();let reads=0 + await f.run(async()=>{ + const client=borrowOperationConfigurationLockClient()! + const accessor:unknown[]=[];Object.defineProperty(accessor,"0",{enumerable:true,get(){reads++;return "mm-config:11"}}) + await assert.rejects(client.query(acquire,accessor)) + const decorated:unknown[]=["mm-config:11"];Object.defineProperty(decorated,Symbol("hidden"),{value:true}) + await assert.rejects(client.query(acquire,decorated)) + assert.equal(reads,0);assert.equal(f.calls.length,0);client.release() + }) +}) +const gate=()=>{let resolve!:()=>void;const promise=new Promise(done=>{resolve=done});return{promise,resolve}} +function fixture(physical:(text:string,values?:unknown[])=>Promise=async()=>({rows:[{acquired:true}]})){ + const calls:Array<{text:string;values?:unknown[]}>=[],releases:boolean[]=[],controller=new AbortController() + let released=false + const client={query:async(text:string,values?:unknown[])=>{calls.push({text,values});return physical(text,values)}} as unknown as PoolClient + const retire=()=>{if(released)return;released=true;releases.push(true);controller.abort()} + const run=async(work:()=>Promise)=>{ + try{return await withOperationConfigurationLockClient(client,retire,controller.signal,work)} + finally{if(!released){released=true;releases.push(false)}} + } + return{calls,releases,run,retire,controller} +} +test("only active genuine ALS owns borrowers; exact fixed SQL captures primitive parameters",async()=>{ + assert.equal(borrowOperationConfigurationLockClient(),undefined);assertOperationConfigurationLockScope() + const f=fixture() + await f.run(async()=>{ + const client=borrowOperationConfigurationLockClient()!;const values=["mm-config:11"] + const result=client.query(acquire,values);values[0]="mm-config:12";await result + await client.query(unlock,["mm-config:11"]);client.release();client.release(true) + }) + assert.deepEqual(f.calls.map(row=>row.values),[["mm-config:11"],["mm-config:11"]]);assert.deepEqual(f.releases,[false]) +}) +test("caller SQL, operation aliases, unsafe IDs and extra parameters never reach physical driver",async()=>{ + const f=fixture() + await f.run(async()=>{ + const client=borrowOperationConfigurationLockClient()! + for(const[text,values]of[["SELECT 1",undefined],[acquire,["mcp-operation:11"]],[acquire,["mm-config:01"]],[acquire,["mm-config:9007199254740992"]],[acquire,["mm-config:1","extra"]],[unlock+";SELECT 1",["mm-config:1"]]]as const){ + await assert.rejects(client.query(text,values?[...values]:undefined)) + } + client.release() + }) + assert.equal(f.calls.length,0);assert.deepEqual(f.releases,[false]) +}) +test("retained released facade and late inherited child refuse without raw-client access",async()=>{ + const f=fixture(),late=gate();let saved:ReturnType,child:Promise|undefined + await f.run(async()=>{ + saved=borrowOperationConfigurationLockClient()!;saved.release() + child=late.promise.then(()=>{assertOperationConfigurationLockScope();return borrowOperationConfigurationLockClient()}) + void child.catch(()=>{}) + }) + await assert.rejects(saved!.query(acquire,["mm-config:1"]));late.resolve();await assert.rejects(child!) + assert.equal(f.calls.length,0);assert.deepEqual(f.releases,[false]) +}) +test("outstanding borrower at scope exit retires owner once and cannot release a reused client",async()=>{ + const f=fixture();let saved:ReturnType + await assert.rejects(f.run(async()=>{saved=borrowOperationConfigurationLockClient()})) + assert.deepEqual(f.releases,[true]);await assert.rejects(saved!.query(acquire,["mm-config:1"]));saved!.release(true);f.retire() + assert.deepEqual(f.releases,[true]);assert.equal(f.calls.length,0) +}) +test("scope exit rejects a stalled lock query before its late result and never dispatches another SQL",async()=>{ + const entered=gate(),driver=gate(),f=fixture(async()=>{entered.resolve();await driver.promise;return{rows:[{acquired:true}]}}) + let pending:Promise|undefined + await assert.rejects(f.run(async()=>{const client=borrowOperationConfigurationLockClient()!;pending=client.query(acquire,["mm-config:1"]);void pending.catch(()=>{});await entered.promise})) + await assert.rejects(pending!);assert.deepEqual(f.releases,[true]);driver.resolve();await Promise.resolve() + assert.equal(f.calls.length,1);assert.deepEqual(f.releases,[true]) +}) +test("concurrent borrowers destroy once before the second physical dispatch; neither receives ACK",async()=>{ + const entered=gate(),driver=gate(),f=fixture(async()=>{entered.resolve();await driver.promise;return{rows:[{acquired:true}]}}) + await assert.rejects(f.run(async()=>{ + const first=borrowOperationConfigurationLockClient()!,second=borrowOperationConfigurationLockClient()! + const pending=first.query(acquire,["mm-config:1"]);void pending.catch(()=>{});await entered.promise + await assert.rejects(second.query(acquire,["mm-config:2"]));await assert.rejects(pending) + first.release(true);second.release(true) + })) + driver.resolve();assert.equal(f.calls.length,1);assert.deepEqual(f.releases,[true]) +}) +test("physical owner error aborts stalled children and inherited guards immediately",async()=>{ + const entered=gate(),driver=gate(),f=fixture(async()=>{entered.resolve();await driver.promise;return{rows:[]}}) + await assert.rejects(f.run(async()=>{ + const client=borrowOperationConfigurationLockClient()!,pending=client.query(acquire,["mm-config:1"]);void pending.catch(()=>{}) + await entered.promise;f.retire();await assert.rejects(pending);assert.throws(assertOperationConfigurationLockScope) + await assert.rejects(client.query(unlock,["mm-config:1"]));client.release(true) + })) + driver.resolve();assert.equal(f.calls.length,1);assert.deepEqual(f.releases,[true]) +}) +test("rejected lock acquire cannot strand an unknown lock in a healthy pooled session",async()=>{ + const f=fixture(async()=>{throw Error("private driver failure")}) + await assert.rejects(f.run(async()=>{const client=borrowOperationConfigurationLockClient()!;await assert.rejects(client.query(acquire,["mm-config:1"]));client.release()})) + assert.deepEqual(f.releases,[true]);assert.equal(f.calls.length,1) +}) +test("normal work exception retains exact unlock before the single healthy outer release",async()=>{ + const f=fixture() + await assert.rejects(f.run(async()=>{ + const client=borrowOperationConfigurationLockClient()! + try{await client.query(acquire,["mm-config:1"]);throw Error("work failed")} + finally{await client.query(unlock,["mm-config:1"]);client.release()} + })) + assert.deepEqual(f.calls.map(row=>row.text),[acquire,unlock]);assert.deepEqual(f.releases,[false]) +}) +test("a fresh service authorizer refuses owner loss across its async observation",async()=>{ + const f=fixture(),entered=gate(),metadata=gate();let serviceWrites=0 + await assert.rejects(f.run(async()=>{ + const authorize=async()=>{assertOperationConfigurationLockScope();entered.resolve();await metadata.promise;assertOperationConfigurationLockScope()} + const work=authorize().then(()=>{serviceWrites++});void work.catch(()=>{}) + await entered.promise;f.retire();metadata.resolve();await assert.rejects(work) + })) + assert.equal(serviceWrites,0);assert.deepEqual(f.releases,[true]) +}) +test("terminal checkpoint refuses dangling borrowers before a success publication",async()=>{ + const f=fixture();let applied=0 + assert.throws(assertOperationConfigurationLockCheckpoint) + await assert.rejects(f.run(async()=>{ + borrowOperationConfigurationLockClient() + assertOperationConfigurationLockCheckpoint();applied++ + })) + assert.equal(applied,0);assert.deepEqual(f.releases,[true]) +}) +test("owner loss caught by service work cannot become successful scope completion",async()=>{ + const f=fixture() + await assert.rejects(f.run(async()=>{assertOperationConfigurationLockCheckpoint();f.retire();return{otherwiseSuccessful:true}})) + assert.deepEqual(f.releases,[true]) +}) +test("ACTUAL runtime outer acquire: backend lock plus rejected ACK without error event destroys once",async()=>{ + // Execute only the exact runtime function AST with inert dependencies. + // The complete runtime module (and therefore DB/config) is NEVER imported. + const file=fileURLToPath(new URL("../modules/mcp/runtime.ts",import.meta.url)) + const source=ts.createSourceFile(file,readFileSync(file,"utf8"),ts.ScriptTarget.ES2022,true) + const declaration=source.statements.find((node):node is ts.FunctionDeclaration=>ts.isFunctionDeclaration(node)&&node.name?.text==="executeOperation") + assert(declaration) + const compiled=ts.transpileModule(declaration.getText(source)+"\nglobalThis.acceptanceExecute=executeOperation;",{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.None}}).outputText + const releases:boolean[]=[];let backendHasLock=false,work=0,unlocks=0,errorEvents=0 + const lease={ + query:async(text:string)=>{ + if(text.includes("pg_try_advisory_lock")){backendHasLock=true;throw Error("Lost acquire ACK without error event")} + unlocks++;backendHasLock=false;return{rows:[]} + }, + on:()=>{},removeListener:()=>{}, + release:(unsafe=false)=>{releases.push(unsafe);if(unsafe)backendHasLock=false}, + } + const sandbox={pool:{connect:async()=>lease},AbortController,withOperationConfigurationLockClient, + // Provenance is independently accepted through the actual owned SQL runner; + // this AST fixture isolates only the outer physical lock acquire/retirement. + withMcpOperationExecutionOrigin:async(_admission:unknown,callback:()=>Promise)=>callback(), + executeOperationWithLease:async()=>{work++},emitError:()=>{errorEvents++}} + runInNewContext(compiled,sandbox) + const execute=(sandbox as unknown as{acceptanceExecute:(id:string,principal:unknown)=>Promise}).acceptanceExecute + await assert.rejects(execute("owned-operation-fixture",{})) + assert.deepEqual(releases,[true]);assert.equal(backendHasLock,false) + assert.equal(work,0);assert.equal(unlocks,0);assert.equal(errorEvents,0) +}) diff --git a/backend/src/services/operation-config-lock-lease.ts b/backend/src/services/operation-config-lock-lease.ts new file mode 100644 index 00000000..74ce6ccd --- /dev/null +++ b/backend/src/services/operation-config-lock-lease.ts @@ -0,0 +1,123 @@ +/** Trusted runner-only reuse of its OWN operation advisory connection. + * No arbitrary SQL, checkout, healthy release or raw-client access is exported. */ +import { AsyncLocalStorage } from "node:async_hooks" +import type { PoolClient, QueryResultRow } from "pg" + +export interface OperationConfigurationLockClient { + query(text: string, values?: unknown[]): Promise<{ rows: T[] }> + release(unsafe?: boolean): void +} +type Entry = { + active: boolean; client: PoolClient; retireOwned: () => void + ownerSignal: AbortSignal + requestSignal?: AbortSignal + borrowers: Set; pending: symbol | undefined + stopped: Promise; rejectStopped: () => void + completed: Promise +} +const scope = new AsyncLocalStorage() +function refused(): never { throw new Error("Operation configuration lock lease is closed or busy") } +function capture(text: string, values?: unknown[]): unknown[] | undefined { + if (text === "SELECT pg_try_advisory_lock(hashtextextended('mm-config:application',0)) AS acquired" + || text === "SELECT pg_advisory_unlock(hashtextextended('mm-config:application',0))") { + if (values !== undefined && (!Array.isArray(values) || values.length !== 0 || Reflect.ownKeys(values).length !== 1)) refused() + return undefined + } + if (text !== "SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired" + && text !== "SELECT pg_advisory_unlock(hashtextextended($1,0))") refused() + if (!Array.isArray(values) || values.length !== 1 || Reflect.ownKeys(values).length !== 2) refused() + const descriptor=Object.getOwnPropertyDescriptor(values,"0") + if(!descriptor||!("value" in descriptor)||!descriptor.enumerable||typeof descriptor.value!=="string")refused() + const key=descriptor.value as string + const match = /^mm-config:([1-9][0-9]*)$/.exec(key) + if (!match || !Number.isSafeInteger(Number(match[1]))) refused() + return [key] +} +function close(entry: Entry, unsafe = false): void { + if (!entry.active) return + entry.active = false + entry.rejectStopped() + // A pending query, or a child still owning a lock facade, cannot return the + // operation client to the pool. The runner is the sole physical releaser. + if (unsafe || entry.pending !== undefined || entry.borrowers.size) entry.retireOwned() +} +export function assertNoOperationConfigurationLockContext(): void { if (scope.getStore()) refused() } +/** Fixed automatic-job handoff waits for original runner WORK to settle. This + * exposes no client, scope clearing function or arbitrary execution callback. */ +export function captureOperationConfigurationLockHandoff(): Promise | undefined { + const entry = scope.getStore() + if (!entry) return undefined + if (!entry.active) refused() + return entry.completed +} +export function assertOperationConfigurationLockScope(): void { + const entry = scope.getStore() + if (entry && !entry.active) refused() + entry?.requestSignal?.throwIfAborted() +} +export function operationConfigurationSignal(): AbortSignal | undefined { return scope.getStore()?.ownerSignal } +export function operationDatabaseRequestSignal(): AbortSignal | undefined { return scope.getStore()?.requestSignal } +function checkpoint(entry: Entry): void { + if (!entry.active) refused() + if (entry.pending !== undefined || entry.borrowers.size) { close(entry, true); refused() } +} +/** A terminal acknowledgement cannot adopt dangling borrowed lock work. */ +export function assertOperationConfigurationLockCheckpoint(): void { + const entry = scope.getStore() + if (!entry) refused() + checkpoint(entry) + entry.requestSignal?.throwIfAborted() +} +export async function withOperationConfigurationLockClient(client: PoolClient, retireOwned: () => void, ownerSignal: AbortSignal, work: () => Promise, requestSignal?: AbortSignal): Promise { + if (scope.getStore()?.active) refused() + let rejectStopped!: () => void + const stopped = new Promise((_resolve, reject) => { rejectStopped = () => reject(new Error("Operation configuration lock scope ended")) }) + // A scope with no borrowed query still closes this lifetime promise. + void stopped.catch(() => {}) + let complete!: () => void + const completed = new Promise(resolve => { complete = resolve }) + const entry: Entry = { active: true, client, retireOwned, ownerSignal, requestSignal, borrowers: new Set(), pending: undefined, stopped, rejectStopped, completed } + const ownerStopped = () => close(entry, true) + ownerSignal.addEventListener("abort", ownerStopped, { once: true }) + try { + if (ownerSignal.aborted) { close(entry, true); refused() } + const result = await scope.run(entry, work) + checkpoint(entry) + return result + } finally { + ownerSignal.removeEventListener("abort", ownerStopped) + try { close(entry) } finally { complete() } + } +} +export function borrowOperationConfigurationLockClient(): OperationConfigurationLockClient | undefined { + const entry = scope.getStore() + if (!entry) return undefined + assertOperationConfigurationLockScope() + const owner = Symbol() + let active = true + entry.borrowers.add(owner) + const live = () => { if (!entry.active || !active) refused() } + return Object.freeze({ + async query(text: string, values?: unknown[]) { + live() + const captured = capture(text, values) + if (entry.pending !== undefined) { close(entry, true); refused() } + entry.pending = owner + try { + const result = await Promise.race([ + Promise.resolve().then(() => { live(); return entry.client.query(text, captured) }), + entry.stopped, + ]) + live() + return result + } catch { close(entry, true); refused() } + finally { if (entry.pending === owner) entry.pending = undefined } + }, + release(unsafe = false) { + if (!active) return + active = false + entry.borrowers.delete(owner) + if (entry.active && (unsafe || entry.pending === owner)) close(entry, true) + }, + }) +} diff --git a/backend/src/services/ospf-catalog-poll.ts b/backend/src/services/ospf-catalog-poll.ts index df432330..0a7ff653 100644 --- a/backend/src/services/ospf-catalog-poll.ts +++ b/backend/src/services/ospf-catalog-poll.ts @@ -1,9 +1,9 @@ import type { MikrotikClient } from "./mikrotik.js" import type { OspfNeighborRead, OspfInterfaceRead, OspfInstanceRead, BfdSessionRead, OspfRouteRead } from "../types/server.js" export type NullableFields = Omit & { [P in K]: T[P] | null } -export type CatalogNeighbor = NullableFields +export type CatalogNeighbor = NullableFields export type CatalogBfdSession = NullableFields -export type CatalogInterface = NullableFields +export type CatalogInterface = NullableFields & { instanceId: number | null } export type CatalogInstance = NullableFields export type CatalogRoute = Omit, "type"> & { type: OspfRouteRead["type"] | "Unknown" } export interface OspfCatalogCollections { @@ -11,13 +11,33 @@ export interface OspfCatalogCollections { bfdSessions: CatalogBfdSession[]; routes: CatalogRoute[]; } export const emptyOspfCatalog = (): OspfCatalogCollections => ({ neighbors: [], interfaces: [], instances: [], bfdSessions: [], routes: [] }) -export async function readOspfCatalogRaw(client: Pick) { - const [neighbors, areas, ifaceTemplates, instances, bfdSessions, v4Routes, v6Routes] = await Promise.all([ - client.getOspfNeighbors(), client.getOspfAreas(), client.getOspfInterfaceTemplates(), client.getOspfInstances(), client.getBfdSessions(), client.getIpRoutes(), client.getIpv6Routes(), +type OspfCatalogClient = Pick + +function validateNativeIds(collections: readonly unknown[]) { + for (const rows of collections) { + if (!Array.isArray(rows)) throw new Error("Некорректный ответ RouterOS OSPF") + if (rows.some(row => !row || typeof row !== "object" || typeof row[".id"] !== "string" || !row[".id"].trim())) throw new Error("Недостоверный native ID RouterOS OSPF/BFD") + } +} + +export async function readOspfCoreRaw(client: OspfCatalogClient) { + const [neighbors, areas, ifaceTemplates, instances] = await Promise.all([ + client.getOspfNeighbors(), client.getOspfAreas(), client.getOspfInterfaceTemplates(), client.getOspfInstances(), ]) - if ([neighbors, areas, ifaceTemplates, instances, bfdSessions, v4Routes, v6Routes].some(rows => !Array.isArray(rows))) throw new Error("Некорректный ответ RouterOS OSPF") - const ipRoutes = [...v4Routes, ...v6Routes.map((route, index) => ({ ...route, ".id": `ipv6:${route[".id"] ?? index}` }))] - return { neighbors, areas, ifaceTemplates, instances, bfdSessions, ipRoutes } + validateNativeIds([neighbors, areas, ifaceTemplates, instances]) + return { neighbors, areas, ifaceTemplates, instances } +} + +export async function readOspfRoutesRaw(client: OspfCatalogClient) { + const [v4Routes, v6Routes] = await Promise.all([client.getIpRoutes(), client.getIpv6Routes()]) + validateNativeIds([v4Routes, v6Routes]) + return [...v4Routes, ...v6Routes.map(route => ({ ...route, ".id": `ipv6:${route[".id"]}` }))] +} + +export async function readOspfCatalogRaw(client: OspfCatalogClient) { + const [core, bfdSessions, ipRoutes] = await Promise.all([readOspfCoreRaw(client), client.getBfdSessions(), readOspfRoutesRaw(client)]) + validateNativeIds([bfdSessions]) + return { ...core, bfdSessions, ipRoutes } } export async function pollOspfCatalog(devices: readonly S[], read: (device: S) => Promise) { const results = await Promise.all(devices.map(async device => { diff --git a/backend/src/services/ospf-cost-optimization.test.ts b/backend/src/services/ospf-cost-optimization.test.ts new file mode 100644 index 00000000..3f6623ce --- /dev/null +++ b/backend/src/services/ospf-cost-optimization.test.ts @@ -0,0 +1,47 @@ +import assert from "node:assert/strict" +import { captureOspfOptimizationSnapshot } from "./ospf-cost-optimization.js" +import { configurationRows, resourceById, ROS_SNAPSHOT_VERSION, type RosSnapshot } from "../modules/mcp/resources.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { addOspfPrefixListDependencies } from "../modules/mcp/routing-dependencies.js" + +for (const invalid of [null, 1, " announced "]) { + await assert.rejects(addOspfPrefixListDependencies(1, undefined, invalid, [], []), { code: "STATE_UNAVAILABLE" }) +} + +const catalog = [{ ".id": "*A", name: "v2", "router-id": "192.0.2.1", version: "2", vrf: "main", disabled: "false", opaque: "ranking-native-extra" }] +const projected = configurationRows(resourceById("ospf_instance"), catalog) +const fresh: RosSnapshot = { serverId: 1, resource: "ospf_instance", rows: projected, fingerprint: fingerprint(projected), projectionVersion: ROS_SNAPSHOT_VERSION, sourceProof: "a".repeat(64) } +let fences = 0, reads = 0 +const authorize = async () => { fences++ } +const read = async (serverId: number, resource: string, beforeRead: () => Promise = async () => {}) => { + assert.equal(serverId, 1); assert.equal(resource, "ospf_instance") + assert.equal(beforeRead, authorize, "Live ranking/source fence must reach the real snapshot reader") + await beforeRead(); reads++; await beforeRead() + return fresh +} +const sealed = await captureOspfOptimizationSnapshot(1, "ospf_instance", catalog, authorize, read) +assert.equal(sealed, fresh, "Return the freshly attested snapshot rather than adding metadata to stale ranking rows") +assert.equal(sealed.projectionVersion, ROS_SNAPSHOT_VERSION) +assert.equal(sealed.sourceProof, "a".repeat(64)) +assert.equal(fences, 2); assert.equal(reads, 1) +assert.equal(Object.hasOwn(sealed.rows[0], "opaque"), false) +assert.equal(catalog[0].opaque, "ranking-native-extra") + +for (const changed of [{ ...catalog[0], "router-id": "192.0.2.2" }, { ...catalog[0], disabled: "true" }, { ...catalog[0], vrf: "other" }, { ...catalog[0], ".id": "*B" }]) { + await assert.rejects(captureOspfOptimizationSnapshot(1, "ospf_instance", [changed], authorize, read), error => error instanceof McpError && error.code === "STATE_CONFLICT") +} +const revoked = async () => { throw new McpError("PERMISSION_DENIED", "Revoked after ranking") } +let nativeReads = 0 +await assert.rejects(captureOspfOptimizationSnapshot(1, "ospf_instance", catalog, revoked, async (_serverId, _resource, beforeRead = async () => {}) => { + await beforeRead(); nativeReads++; return fresh +}), error => error instanceof McpError && error.code === "PERMISSION_DENIED") +assert.equal(nativeReads, 0) +for (const resource of ["ospf_neighbor", "ospf_area", "bfd_session"] as const) { + const old = [{ ".id": "*A", ...(resource === "ospf_neighbor" ? { state: "Full", interface: "gre-owned" } : resource === "ospf_area" ? { name: "backbone", instance: "v2", disabled: "false" } : { state: "up", interface: "gre-owned" }) }] + const rows = configurationRows(resourceById(resource), old) + const attested: RosSnapshot = { ...fresh, resource, rows, fingerprint: fingerprint(rows) } + assert.equal(await captureOspfOptimizationSnapshot(1, resource, old, authorize, async () => attested), attested) + const driftedRows = [{ ...rows[0], interface: "foreign" }] + await assert.rejects(captureOspfOptimizationSnapshot(1, resource, old, authorize, async () => ({ ...attested, rows: driftedRows, fingerprint: fingerprint(driftedRows) })), error => error instanceof McpError && error.code === "STATE_CONFLICT") +} +console.log("OSPF evidence capture: fresh v2 source-bound snapshots, exact ranking/identity/dependency projection retained, drift and late revocation rejected without re-attesting old state") diff --git a/backend/src/services/ospf-cost-optimization.ts b/backend/src/services/ospf-cost-optimization.ts new file mode 100644 index 00000000..40ae0829 --- /dev/null +++ b/backend/src/services/ospf-cost-optimization.ts @@ -0,0 +1,177 @@ +import { readEditableOspfTemplates } from "./ospf-optimizer-plan.js" +import { z } from "zod" +import { readOspfLinkCatalog, resolveOspfLinkOwner, type OspfLinkCatalog } from "./ospf-link-catalog.js" +import { readOspfIdentityCatalog } from "./ospf-identity-catalog.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { ospfCostConnectionProof } from "./ospf-template-cost-write.js" +import { readRoutingObservations } from "../modules/mcp/routing-observations.js" +import { assertRoutingChecksFresh, latestRoutingMetric } from "../modules/mcp/routing-observations-model.js" +import { createRoutingSourceContext, readRoutingEndpoints } from "../modules/mcp/routing-observation-source.js" +import { fresh } from "../modules/mcp/routing-model.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { configurationRows, readRosSnapshot, resourceById, routerClient, type RosSnapshot } from "../modules/mcp/resources.js" +import { ospfTable, requireOspfBfdSession } from "../modules/mcp/ospf-health.js" +import type { ChangeEvidence } from "../modules/mcp/action-types.js" +import { addOspfPrefixListDependencies, ospfInterfaceResources, resolveOspfInterfaceSelectors } from "../modules/mcp/routing-dependencies.js" + +export interface OspfRank { id: string; interface: string; currentCost: number; proposedCost: number; score: number; pingMs: number | null; lossPct: number | null; dlMbps: number | null; ulMbps: number | null; observationId: string; bandwidthObservationId: string | null; peerId: number; validUntil: string } + +/** Seal a current source-bound snapshot only if it still matches the catalog + * used for ranking/identity resolution. Never attach new provenance to old rows. */ +export async function captureOspfOptimizationSnapshot(serverId: number, resource: string, payload: unknown, beforeRead: () => Promise, read: typeof readRosSnapshot = readRosSnapshot): Promise { + const originalFingerprint = fingerprint(configurationRows(resourceById(resource), payload)) + const current = await read(serverId, resource, beforeRead) + if (current.fingerprint !== originalFingerprint) throw new McpError("STATE_CONFLICT", "Каталог OSPF изменился после построения рейтинга или графа зависимостей", 409) + return current +} + +export async function buildOspfCostOptimizationEvidence(input: Record, allowedServerIds: number[], authorize: () => Promise = async () => {}): Promise { + if (!allowedServerIds.includes(Number(input.serverId))) throw new McpError("SERVER_DENIED", "Сервер оптимизации OSPF вне разрешённой области", 403) + await authorize() + const family = z.enum(["ipv4", "ipv6"]).parse(input.family) + const candidateIds = [...new Set(allowedServerIds)].sort((a, b) => a - b) + const sourceContext = await createRoutingSourceContext(await readRoutingEndpoints(candidateIds)) + // App monitoring disabled does not prove that a router stopped owning its ID. + if (fingerprint(sourceContext.expected.map(row => row.serverId)) !== fingerprint(candidateIds)) throw new McpError("DEVICE_UNAVAILABLE", "Не все источники графа OSPF доступны и включены; отключение опроса не доказывает отсутствие владельца Router ID", 409) + const beforeRead = async () => { await authorize(); await sourceContext.assertCurrent() } + await beforeRead() + const serverId = Number(input.serverId), { client, server } = await routerClient(serverId), expectedServerProof = await ospfCostConnectionProof(serverId, server), snapshot = await readRosSnapshot(serverId, "ospf_template", beforeRead) + await beforeRead() + const neighbors = await client.getOspfNeighbors(), primaryIdentity = await readOspfIdentityCatalog(client, beforeRead), instances = primaryIdentity.instances + await beforeRead() + const areas = await client.getOspfAreas() + const selectedTemplates = snapshot.rows.filter((template): template is typeof template & { ".id": string } => { + if (typeof template[".id"] !== "string" || typeof template.disabled !== "string" || typeof template.passive !== "string") return false + const area = areas.find(area => area.name === template.area || area[".id"] === template.area), instance = instances.find(instance => instance.name === area?.instance || instance[".id"] === area?.instance) + return !!instance && observedBoolean(template.disabled) === false && observedBoolean(template.passive) === false && observedBoolean(instance.disabled) === false && observedBoolean(area?.disabled) === false && String(instance.version) === (family === "ipv4" ? "2" : "3") && ospfTable(instance) === input.table + }) + const templateContext = (template: { id: string; interface: string }) => { + const native = snapshot.rows.find(row => row[".id"] === template.id), area = areas.find(area => area.name === native?.area || area[".id"] === native?.area), instance = instances.find(instance => instance.name === area?.instance || instance[".id"] === area?.instance) + const matches = instance ? neighbors.filter(neighbor => neighbor.interface === template.interface && [instance.name, instance[".id"]].includes(neighbor.instance) && String(neighbor.state).toLowerCase() === "full") : [] + return { instance, matches } + } + const selectorSnapshots = [snapshot] + const selectorObjects = new Map() + const templateInterfaces = new Map>() + const affected = new Map() + const scopedTemplates = [] + const knownInterfaces = new Set(neighbors.map(neighbor => neighbor.interface).filter((name): name is string => typeof name === "string")) + for (const template of selectedTemplates) { + if (typeof template.interfaces !== "string" || !template.interfaces.trim()) throw new McpError("MEASUREMENT_UNKNOWN", "Область действия активного шаблона OSPF не подтверждена", 409) + const objects: ChangeEvidence["objects"] = [] + const names = await resolveOspfInterfaceSelectors(serverId, template.interfaces.split(",").map(name => name.trim()), knownInterfaces, selectorSnapshots, objects, beforeRead) + for (const name of names) affected.set(name, (affected.get(name) ?? 0) + 1) + if (!names.size || (template.networks !== undefined && (typeof template.networks !== "string" || template.networks.trim()))) continue + const [name] = names + templateInterfaces.set(template[".id"], names) + if (objects.length) selectorObjects.set(template[".id"], objects) + scopedTemplates.push({ ...template, interfaces: name }) + } + const templates = readEditableOspfTemplates(scopedTemplates.filter(template => + [...templateInterfaces.get(template[".id"])!].every(name => affected.get(name) === 1), + )).flatMap(template => [...templateInterfaces.get(template.id)!].map(name => ({ ...template, interface: name }))).filter(template => { + const { instance, matches } = templateContext(template) + return instance && ospfTable(instance) === input.table && matches.length === 1 && matches[0][".id"] + }) + const eligibleIds = new Set(templates.map(template => template.id)) + const bfdFlags = new Map() + for (const template of selectedTemplates.filter(template => eligibleIds.has(template[".id"]))) { + const useBfd = template["use-bfd"] + let enabled = typeof useBfd === "string" ? observedBoolean(useBfd) : null + if (useBfd === undefined) { + await beforeRead() + const value = await client.post<{ ret?: unknown }>("/routing/ospf/interface-template/get", { ".id": template[".id"], "value-name": "use-bfd" }) + enabled = typeof value?.ret === "string" ? observedBoolean(value.ret) : null + } + if (enabled === null) throw new McpError("MEASUREMENT_UNKNOWN", "Не подтверждён use-bfd шаблона OSPF", 409) + bfdFlags.set(template[".id"], enabled) + } + await beforeRead() + const bfdRequired = [...bfdFlags.values()].some(Boolean), bfdSessions = bfdRequired ? await client.getBfdSessions() : [] + const capture = (id: number, resource: string, payload: unknown) => captureOspfOptimizationSnapshot(id, resource, payload, beforeRead) + const ownerSnapshots = new Map() + const identityFingerprints = new Map(), links = new Map() + let ownerRows = 0, ownerBytes = 0 + for (const id of candidateIds) { + await beforeRead() + const link = await readOspfLinkCatalog(id === serverId ? client : (await routerClient(id)).client, family, beforeRead, id === serverId ? { identity: primaryIdentity, neighbors } : undefined) + ownerRows += link.rows; ownerBytes += link.bytes + if (ownerRows > 10000 || ownerBytes > 1048576) throw new McpError("STATE_UNAVAILABLE", "Граф владельцев OSPF превышает 10000 строк или 1 МиБ", 409) + links.set(id, link); ownerSnapshots.set(id, await capture(id, "ospf_instance", link.identity.raw)); identityFingerprints.set(id, link.fingerprint) + } + await beforeRead() + const observations = await readRoutingObservations(allowedServerIds), now = Date.now() + const measured = templates.flatMap(template => { + const { instance, matches } = templateContext(template) + if (!instance || ospfTable(instance) !== input.table) return [] + if (matches.length !== 1 || !matches[0][".id"]) return [] + const owner = resolveOspfLinkOwner(serverId, links.get(serverId)!, instance[".id"], matches[0][".id"], [...links].map(([id, catalog]) => ({ serverId: id, catalog }))) + if (!owner) return [] + const peer = owner.serverId + const linkObservations = observations.filter(row => row.srcServerId === serverId && row.dstServerId === peer && row.srcInterface === template.interface && row.dstInterface === owner.interface && row.family === input.family && row.table === input.table) + const ping = latestRoutingMetric(linkObservations, "pingAt") + if (!ping?.contextVerified || !fresh(ping.pingAt, 300, now) || ping.pingMs === null || ping.lossPct === null || ping.lossPct > Number(input.maxLossPct)) return [] + const pingWeight = Number(input.pingWeight) / 100 + const bandwidth = pingWeight === 1 ? undefined : latestRoutingMetric(linkObservations, "bandwidthAt") + if (pingWeight !== 1 && (!bandwidth?.contextVerified || !fresh(bandwidth.bandwidthAt, 3600, now) || bandwidth.dlMbps === null || bandwidth.ulMbps === null)) return [] + const score = pingWeight * Math.max(0, 100 - ping.pingMs * 0.6) + (1 - pingWeight) * Math.min(100, ((bandwidth?.dlMbps ?? 0) + (bandwidth?.ulMbps ?? 0)) / 18) + const pingUntil = Date.parse(ping.pingAt!) + 300000 + const bandwidthUntil = bandwidth ? Date.parse(bandwidth.bandwidthAt!) + 3600000 : Infinity + const validUntil = Math.min(pingUntil, bandwidthUntil) + const bfd = bfdFlags.get(template.id) ? requireOspfBfdSession(bfdSessions as unknown as Record[], matches[0].address, template.interface, String(input.table)) : null + return [{ template, owner, score, ping, bandwidth, pingUntil, bandwidthUntil, validUntil, peerId: peer, neighborId: String(matches[0][".id"]), bfd }] + }) + const measuredCounts = new Map() + for (const item of measured) measuredCounts.set(item.template.id, (measuredCounts.get(item.template.id) ?? 0) + 1) + const ranked = measured.filter(item => measuredCounts.get(item.template.id) === templateInterfaces.get(item.template.id)!.size) + .sort((a, b) => b.score - a.score || a.template.interface.localeCompare(b.template.interface)) + if (!ranked.length) throw new McpError("MEASUREMENT_UNKNOWN", "Нет однозначных шаблонов и свежих измерений нужного instance/family/VRF", 409) + const optimalCost = (index: number) => index < Number(input.ecmpPaths) ? 10 : (index - Number(input.ecmpPaths) + 2) * 10 + const before = { ranking: ranked.map((item, index) => ({ id: item.template.id, interface: item.template.interface, currentCost: item.template.cost, proposedCost: optimalCost(index), score: item.score, pingMs: item.ping.pingMs, lossPct: item.ping.lossPct, dlMbps: item.bandwidth?.dlMbps ?? null, ulMbps: item.bandwidth?.ulMbps ?? null, observationId: item.ping.id, bandwidthObservationId: item.bandwidth?.id ?? null, peerId: item.peerId, peerInstance: item.owner.instanceId, peerVrf: item.owner.vrf, peerInterface: item.owner.interface, validUntil: new Date(item.validUntil).toISOString() })) } + const costsByTemplate = new Map() + for (const item of before.ranking) { + const previous = costsByTemplate.get(item.id) + if (previous && previous.proposed !== item.proposedCost) { + throw new McpError("STATE_CONFLICT", "Для интерфейсов одного шаблона OSPF рассчитаны разные cost", 409) + } + costsByTemplate.set(item.id, { current: item.currentCost, proposed: item.proposedCost }) + } + const steps: ChangeEvidence["steps"] = [] + for (const [id, cost] of costsByTemplate) { + if (cost.current !== cost.proposed) steps.push({ kind: "ros", step: { serverId, resource: "ospf_template", action: "update", id, fields: { cost: String(cost.proposed) }, expectedCost: cost.current, expectedServerProof } }) + } + await beforeRead() + const dependencyIds = candidateIds + const dependencySnapshots = candidateIds.filter(id => id !== serverId).map(id => ownerSnapshots.get(id)!) + if (dependencySnapshots.some(value => !value)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждены OSPF instances соседнего устройства", 409) + const healthChecks: NonNullable = ranked.map(item => ({ serverId, resource: "ospf_neighbor", id: item.neighborId, field: "state", identity: { interface: item.template.interface, instance: String(neighbors.find(row => row[".id"] === item.neighborId)!.instance) } })) + for (const item of ranked) if (item.bfd) healthChecks.push({ serverId, resource: "bfd_session", id: String(item.bfd[".id"]), field: "bfd-up", identity: Object.fromEntries(["remote-address", "local-address", "vrf", "interface"].filter(key => typeof item.bfd![key] === "string").map(key => [key, String(item.bfd![key])])) }) + const bfdSnapshots = bfdRequired ? [await readRosSnapshot(serverId, "bfd_configuration", beforeRead), await capture(serverId, "bfd_session", bfdSessions)] : [] + const objects: ChangeEvidence["objects"] = ranked.flatMap(item => [{ kind: "ospf_template", id: item.template.id, serverId }, ...selectorObjects.get(item.template.id) ?? []]) + if (family === "ipv6") { + const lists = new Set(selectedTemplates.filter(template => costsByTemplate.has(template[".id"])).map(template => template["prefix-list"])) + for (const list of lists) await addOspfPrefixListDependencies(serverId, undefined, list, selectorSnapshots, objects, beforeRead) + } + const namedInterfaces = new Set(ranked.filter(item => selectorObjects.has(item.template.id)).map(item => item.template.interface)) + if (namedInterfaces.size) for (const kind of ospfInterfaceResources) { + const dependency = await readRosSnapshot(serverId, kind, beforeRead, snapshot.sourceProof) + selectorSnapshots.push(dependency) + for (const row of dependency.rows) { + if (typeof row.name !== "string" || !namedInterfaces.has(row.name)) continue + if (typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"])) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён ID интерфейса OSPF", 409) + objects.push({ kind, id: row[".id"], serverId }) + } + } + const uniqueObjects = [...new Map(objects.map(object => [`${object.kind}/${object.id}`, object])).values()] + if (uniqueObjects.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Слишком много защищаемых зависимостей OSPF", 409) + const observationChecks = ranked.flatMap[number]>(item => { + const link = { srcServerId: serverId, dstServerId: item.peerId, srcInterface: item.template.interface, dstInterface: item.owner.interface, family, table: String(input.table) } + const ping = { ...link, id: item.ping.id, metric: "pingAt" as const, fingerprint: fingerprint(item.ping), validUntil: new Date(item.pingUntil).toISOString() } + if (!item.bandwidth) return [ping] + return [ping, { ...link, id: item.bandwidth.id, metric: "bandwidthAt" as const, fingerprint: fingerprint(item.bandwidth), validUntil: new Date(item.bandwidthUntil).toISOString() }] + }) + const snapshots = [...selectorSnapshots, await capture(serverId, "ospf_neighbor", neighbors), await capture(serverId, "ospf_instance", primaryIdentity.raw), await capture(serverId, "ospf_area", areas), ...dependencySnapshots, ...bfdSnapshots] + await beforeRead() + assertRoutingChecksFresh(observationChecks) + return { before, observationChecks, ospfOwnerGraph: { family, sourceSecretProofs: sourceContext.expected, instances: [...ownerSnapshots.values()].map(row => ({ serverId: row.serverId, fingerprint: fingerprint({ native: row.fingerprint, identity: identityFingerprints.get(row.serverId) }) })) }, target: { scope: "servers", serverIds: dependencyIds }, objects: uniqueObjects, snapshots, steps, healthChecks, validUntil: new Date(Math.min(...ranked.map(item => item.validUntil))).toISOString(), warnings: ["Рейтинг локальных интерфейсов не доказывает глобальный оптимум.", "Неизвестные шаблоны не меняются.", "Full выбранных соседств и Up включённых BFD-сессий проверяются до и после cost-записей. Сквозная связность требует контрольных проб; после истечения измерений нужен новый план."], requiresHuman: true } +} diff --git a/backend/src/services/ospf-identity-catalog.test.ts b/backend/src/services/ospf-identity-catalog.test.ts new file mode 100644 index 00000000..a2734302 --- /dev/null +++ b/backend/src/services/ospf-identity-catalog.test.ts @@ -0,0 +1,43 @@ +import assert from "node:assert/strict" +import { readOspfIdentityCatalog } from "./ospf-identity-catalog.js" + +const instance = { ".id": "*1", name: "ospf", "router-id": "main", version: "2", vrf: "tenant-a", disabled: "false" } +const reference = { ".id": "*2", name: "main", "dynamic-id": "192.0.2.1", disabled: "false", inactive: "false" } +let instances: Record[] = [instance], refs: Record[] = [reference], requests: string[] = [], properties: Record = {} +const client = { + async get(path: string): Promise { requests.push(path); return structuredClone(path === "/routing/id" ? refs : instances) as T }, + async post(path: string, body: Record): Promise { requests.push(path + ":" + body["value-name"]); return { ret: properties[path + ":" + body["value-name"]] } as T }, +} +let catalog = await readOspfIdentityCatalog(client) +assert.equal(catalog.instances[0]["router-id"], "192.0.2.1"); assert.equal(catalog.raw[0]["router-id"], "main") +const initial = catalog.fingerprint +refs = [{ ...reference, "dynamic-id": "192.0.2.2" }] +assert.notEqual((await readOspfIdentityCatalog(client)).fingerprint, initial) +refs = [{ ...reference, id: "192.0.2.9", "dynamic-id": undefined }] +assert.equal((await readOspfIdentityCatalog(client)).instances[0]["router-id"], "192.0.2.9") +for (const invalid of [[], [reference, { ...reference, ".id": "*3" }], [{ ...reference, disabled: "true" }], [{ ...reference, inactive: "true" }], [{ ...reference, "dynamic-id": "0.0.0.0" }], [{ ...reference, id: "192.0.2.2" }], [{ ...reference, "dynamic-id": "192.00.2.1" }]]) { + refs = invalid; await assert.rejects(readOspfIdentityCatalog(client)) +} +instances = [{ ...instance, "router-id": "192.0.2.1", disabled: "no" }]; requests = [] +catalog = await readOspfIdentityCatalog(client) +assert.equal(catalog.raw[0].disabled, "no"); assert.equal(catalog.instances[0].disabled, "false"); assert.deepEqual(requests, ["/routing/ospf/instance"]) +instances = [{ ".id": "*1", name: "ospf" }]; refs = [{ ".id": "*2", name: "main", "dynamic-id": "192.0.2.1" }] +properties = { "/routing/ospf/instance/get:disabled": "false", "/routing/ospf/instance/get:version": "3", "/routing/ospf/instance/get:vrf": "remote-different-name", "/routing/ospf/instance/get:router-id": "main", "/routing/id/get:disabled": "false", "/routing/id/get:inactive": "false" } +requests = []; let grants = 0 +catalog = await readOspfIdentityCatalog(client, async () => { grants++ }) +assert.equal(catalog.instances[0].version, "3"); assert.equal(catalog.instances[0].vrf, "remote-different-name"); assert.equal(grants, requests.length) +properties["/routing/id/get:inactive"] = "unknown" +await assert.rejects(readOspfIdentityCatalog(client)) +instances = [{ ".id": "*1", disabled: "true" }]; requests = [] +assert.equal((await readOspfIdentityCatalog(client)).instances.length, 1); assert.deepEqual(requests, ["/routing/ospf/instance"]) +instances = Array.from({ length: 129 }, (_, index) => ({ ...instance, ".id": "*" + index.toString(16), disabled: undefined, "router-id": "192.0.2.1" })); requests = [] +await assert.rejects(readOspfIdentityCatalog(client), /бюджет/) +assert.equal(requests.filter(path => path.endsWith(":disabled")).length, 128) +instances = [instance]; refs = [reference]; requests = [] +await assert.rejects(readOspfIdentityCatalog(client, async () => { if (requests.length === 1) throw new Error("Revoked") })) +assert.deepEqual(requests, ["/routing/ospf/instance"]) +instances = Array.from({ length: 10001 }, (_, index) => ({ ...instance, ".id": "*" + index })) +await assert.rejects(readOspfIdentityCatalog(client)) +instances = [{ ...instance, opaque: "x".repeat(1048576) }] +await assert.rejects(readOspfIdentityCatalog(client)) +console.log("OSPF identities: effective property receipts, literal/static/elected Router IDs, changed elected ID, disabled/inactive/ambiguous/unknown rejection, no defaults guessed, read grants and row/byte/getter caps") diff --git a/backend/src/services/ospf-identity-catalog.ts b/backend/src/services/ospf-identity-catalog.ts new file mode 100644 index 00000000..71bb77f8 --- /dev/null +++ b/backend/src/services/ospf-identity-catalog.ts @@ -0,0 +1,77 @@ +import { z } from "zod" +import type { MikrotikClient } from "./mikrotik.js" +import type { RosOspfInstance } from "../types/server.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { fingerprint } from "../modules/mcp/security.js" + +export function ospfLiteralRouterId(value: unknown): string | null { + if (typeof value !== "string") return null + const parts = value.split(".") + return value !== "0.0.0.0" && parts.length === 4 && parts.every(part => /^(0|[1-9]\d{0,2})$/.test(part) && Number(part) <= 255) ? value : null +} +const rowsSchema = z.array(z.record(z.string(), z.unknown())).max(10000) +const boolean = (value: unknown) => observedBoolean(typeof value === "boolean" ? String(value) : typeof value === "string" ? value : undefined) +function rows(raw: unknown) { + const result = rowsSchema.parse(raw) + if (Buffer.byteLength(JSON.stringify(result)) > 1048576 || result.some(row => typeof row[".id"] !== "string" || !row[".id"]) || new Set(result.map(row => row[".id"])).size !== result.length) throw new Error("Недостоверный или чрезмерный каталог идентификаторов OSPF") + return result +} + +export function resolveOspfRouterIdRow(reference: Record) { + if (boolean(reference.disabled) !== false || boolean(reference.inactive) !== false) throw new Error("Ссылка Router ID OSPF отключена, неактивна или неизвестна") + const explicit = reference.id === undefined || reference.id === "0.0.0.0" ? null : ospfLiteralRouterId(reference.id) + const dynamic = reference["dynamic-id"] === undefined || reference["dynamic-id"] === "0.0.0.0" ? null : ospfLiteralRouterId(reference["dynamic-id"]) + if (reference.id !== undefined && reference.id !== "0.0.0.0" && !explicit || reference["dynamic-id"] !== undefined && reference["dynamic-id"] !== "0.0.0.0" && !dynamic || explicit && dynamic && explicit !== dynamic) throw new Error("Противоречивый выбранный Router ID OSPF") + const routerId = explicit ?? dynamic + if (!routerId) throw new Error("Не выбран фактический Router ID OSPF") + return { routerId, explicit, dynamic } +} + +/** Read effective values instead of inventing omitted defaults or treating a + * named Router ID as an IPv4 address. Native configuration stays unchanged. */ +export async function readOspfIdentityCatalog(client: Pick, beforeRead: () => Promise = async () => {}) { + let getters = 0 + const get = async (path: string) => { await beforeRead(); return rows(await client.get(path)) } + const property = async (path: string, id: unknown, name: string): Promise => { + if (typeof id !== "string" || !/^\*[a-z0-9]+$/i.test(id) || ++getters > 128) throw new Error("Превышен бюджет уточнения идентификаторов OSPF или неизвестен native ID") + await beforeRead() + const response = await client.post<{ ret?: unknown }>(path + "/get", { ".id": id, "value-name": name }) + if (typeof response?.ret !== "string" || response.ret.length > 1000) throw new Error("Не подтверждено свойство идентификатора OSPF") + return response.ret + } + const raw = await get("/routing/ospf/instance"), effective: RosOspfInstance[] = [], references = new Map>() + let referenceRows: Record[] | undefined + for (const row of raw) { + let disabled = boolean(row.disabled) + if (row.disabled === undefined) disabled = boolean(await property("/routing/ospf/instance", row[".id"], "disabled")) + if (disabled === null) throw new Error("Не подтверждён disabled экземпляра OSPF") + if (row.disabled === undefined) row.disabled = String(disabled) + if (disabled) { effective.push({ ...row, disabled: "true" } as unknown as RosOspfInstance); continue } + const version = row.version === undefined ? await property("/routing/ospf/instance", row[".id"], "version") : row.version + const vrf = row.vrf === undefined ? await property("/routing/ospf/instance", row[".id"], "vrf") : row.vrf + if (typeof version !== "string" || !["2", "3"].includes(version) || typeof vrf !== "string" || !vrf.trim() || vrf.length > 200 || typeof row.name !== "string" || !row.name) throw new Error("Не подтверждён контекст экземпляра OSPF") + const configured = row["router-id"] === undefined ? await property("/routing/ospf/instance", row[".id"], "router-id") : row["router-id"] + let routerId = ospfLiteralRouterId(configured) + if (!routerId) { + if (typeof configured !== "string" || !configured || configured.length > 200 || configured.includes(".") && /^[\d.]+$/.test(configured)) throw new Error("Недостоверная ссылка Router ID OSPF") + referenceRows ??= await get("/routing/id") + const matching = referenceRows.filter(reference => reference.name === configured) + if (matching.length !== 1) throw new Error("Не подтверждена единственная ссылка Router ID OSPF") + const reference = matching[0] + for (const flag of ["disabled", "inactive"]) { + const value = reference[flag] === undefined ? await property("/routing/id", reference[".id"], flag) : reference[flag] + if (boolean(value) !== false) throw new Error("Ссылка Router ID OSPF отключена, неактивна или неизвестна") + reference[flag] = "false" + } + const { routerId: resolved, explicit, dynamic } = resolveOspfRouterIdRow(reference) + routerId = resolved + references.set(configured, { nativeId: reference[".id"], name: configured, id: explicit, dynamicId: dynamic, disabled: false, inactive: false }) + } + effective.push({ ...row, disabled: "false", version, vrf, "router-id": routerId } as unknown as RosOspfInstance) + } + const identities = effective.map(row => ({ id: row[".id"], name: row.name, disabled: row.disabled, ...(row.disabled === "false" ? { version: row.version, vrf: row.vrf, routerId: row["router-id"] } : {}) })).sort((a, b) => a.id.localeCompare(b.id)) + const usedReferences = [...references.values()].sort((a, b) => String(a.name).localeCompare(String(b.name))) + const count = raw.length + (referenceRows?.length ?? 0), bytes = Buffer.byteLength(JSON.stringify({ raw, identities, references: referenceRows ?? [] })) + if (count > 10000 || bytes > 1048576) throw new Error("Каталог идентификаторов OSPF превышает 10000 строк или 1 МиБ") + return { raw: raw as unknown as RosOspfInstance[], instances: effective, fingerprint: fingerprint({ identities, references: usedReferences }), rows: count, bytes } +} diff --git a/backend/src/services/ospf-legacy-optimization.test.ts b/backend/src/services/ospf-legacy-optimization.test.ts new file mode 100644 index 00000000..a83e13a9 --- /dev/null +++ b/backend/src/services/ospf-legacy-optimization.test.ts @@ -0,0 +1,30 @@ +import assert from "node:assert/strict" +import { prepareLegacyOspfOptimization,applyLegacyOspfOptimization,LegacyOspfApplyError } from "./ospf-legacy-optimization.js" +import { OspfCostWriteError } from "./ospf-template-cost-write.js" + +const server={id:1,name:"fixture",host:"192.0.2.1",enabled:true} +const rows=[{id:"*1",interface:"gre-a",currentCost:10,optimalCost:20,pingMs:0,dlMbps:0,ulMbps:0,score:0},{id:"*2",interface:"gre-b",currentCost:30,optimalCost:10,pingMs:1,dlMbps:10,ulMbps:10,score:1}] +const plan=await prepareLegacyOspfOptimization(server,60,async()=>rows) +assert.deepEqual(plan.interfaces,rows) +let reads=0 +await assert.rejects(prepareLegacyOspfOptimization({...server,enabled:false},60,async()=>{reads++;return rows})) +await assert.rejects(prepareLegacyOspfOptimization(server,101,async()=>{reads++;return rows})) +assert.equal(reads,0) +const writes:string[]=[],prefixes:number[]=[] +const completed=await applyLegacyOspfOptimization(plan,async row=>{writes.push(row.id)},async(_row,index,prefix)=>{assert.equal(prefix.length,index);prefixes.push(prefix.length)}) +assert.deepEqual(writes,["*1","*2"]);assert.deepEqual(prefixes,[0,1]);assert.equal(completed.optimizedCount,2) +const before=writes.length +await assert.rejects(applyLegacyOspfOptimization(plan,async row=>{writes.push(row.id)},async(_row,index)=>{if(index===1)throw Error("source revoked")}),error=>{ + assert.ok(error instanceof LegacyOspfApplyError);assert.equal(error.state,"partial");assert.deepEqual(error.applied,[{interface:"gre-a",from:10,to:20}]);return true +}) +assert.equal(writes.length,before+1) +for(const attempted of [false,true]){ + let calls=0 + await assert.rejects(applyLegacyOspfOptimization(plan,async()=>{calls++;throw new OspfCostWriteError("fixture read-back",attempted)}),error=>{ + assert.ok(error instanceof LegacyOspfApplyError);assert.equal(error.state,attempted?"unknown":"rejected");assert.equal(error.applied.length,0);return true + });assert.equal(calls,1) +} +let noops=0 +assert.equal((await applyLegacyOspfOptimization({...plan,interfaces:rows.map(row=>({...row,optimalCost:row.currentCost}))},async()=>{noops++})).optimizedCount,0) +assert.equal(noops,0) +console.log("Legacy OSPF shared pipeline: exact ranking, rejected input before provider read, ordered prefix gates, source revocation stops later writes, unknown/rejected read-back without retry and no-op passed") diff --git a/backend/src/services/ospf-legacy-optimization.ts b/backend/src/services/ospf-legacy-optimization.ts new file mode 100644 index 00000000..bbd94b88 --- /dev/null +++ b/backend/src/services/ospf-legacy-optimization.ts @@ -0,0 +1,37 @@ +import { z } from "zod" +import { validateOspfOptimizationPlan } from "./ospf-optimizer-plan.js" +import { OspfCostWriteError } from "./ospf-template-cost-write.js" +import { MikrotikError } from "./mikrotik.js" + +export type LegacyOspfRanked = ReturnType[number] +export interface LegacyOspfPreparation {serverId:number;serverName:string;pingWeight:number;interfaces:LegacyOspfRanked[]} +export interface LegacyOspfApplied {interface:string;from:number;to:number} +export class LegacyOspfApplyError extends Error { + constructor(readonly original:unknown,readonly applied:LegacyOspfApplied[],readonly failedInterface:string,readonly state:"rejected"|"partial"|"unknown"){ + super("Применение legacy OSPF плана остановлено",{cause:original}) + } +} +/** Preserve the legacy ranking/provider; callers supply their own access/source fences. */ +export async function prepareLegacyOspfOptimization(server:{id:number;name:string;host:string;enabled:boolean},pingWeight:number,readRanked:()=>Promise,authorize:()=>Promise=async()=>{}):Promise{ + if(!server.enabled)throw Error("Устройство OSPF отключено") + z.number().int().positive().safe().parse(server.id) + z.number().int().min(0).max(100).parse(pingWeight) + await authorize() + const interfaces=validateOspfOptimizationPlan(await readRanked()) + if(interfaces.length>1000)throw Error("Legacy OSPF план превышает 1000 интерфейсов") + await authorize() + return {serverId:server.id,serverName:server.name||server.host,pingWeight,interfaces} +} +/** Ordered, single-attempt writes with a fresh caller fence before each command. */ +export async function applyLegacyOspfOptimization(plan:LegacyOspfPreparation,write:(item:LegacyOspfRanked)=>Promise,beforeEach:(item:LegacyOspfRanked,index:number,applied:readonly LegacyOspfApplied[])=>Promise=async()=>{}){ + const ranked=validateOspfOptimizationPlan(plan.interfaces),changed=ranked.filter(item=>item.currentCost!==item.optimalCost),applied:LegacyOspfApplied[]=[] + for(const [index,item]of changed.entries()){ + try{await beforeEach(item,index,applied)}catch(error){throw new LegacyOspfApplyError(error,[...applied],item.interface,applied.length?"partial":"rejected")} + try{await write(item)}catch(error){ + const rejected=error instanceof OspfCostWriteError&&!error.writeAttempted||error instanceof MikrotikError&&error.statusCode>=400&&error.statusCode<500 + throw new LegacyOspfApplyError(error,[...applied],item.interface,rejected?(applied.length?"partial":"rejected"):"unknown") + } + applied.push({interface:item.interface,from:item.currentCost,to:item.optimalCost}) + } + return {serverId:plan.serverId,serverName:plan.serverName,pingWeight:plan.pingWeight,optimizedCount:changed.length,applied,interfaces:ranked} +} diff --git a/backend/src/services/ospf-link-catalog.test.ts b/backend/src/services/ospf-link-catalog.test.ts new file mode 100644 index 00000000..bc9904ec --- /dev/null +++ b/backend/src/services/ospf-link-catalog.test.ts @@ -0,0 +1,52 @@ +import assert from "node:assert/strict" +import { readOspfLinkCatalog, resolveOspfLinkOwner } from "./ospf-link-catalog.js" + +function fixture(id: number, vrf: string, own: string, remote: string, iface: string, ownAddress: string, remoteAddress: string, family: "ipv4" | "ipv6" = "ipv4") { + const version = family === "ipv4" ? "2" : "3", name = "instance" + id + const data: Record[]> = { + "/routing/ospf/instance": [{ ".id": "*i" + id, name, "router-id": own, version, vrf, disabled: "false" }], + "/routing/ospf/neighbor": [{ ".id": "*n" + id, instance: name, interface: iface, "router-id": remote, address: remoteAddress, state: "Full" }], + [family === "ipv4" ? "/ip/address" : "/ipv6/address"]: [{ ".id": "*a" + id, interface: iface, address: ownAddress, disabled: "false", invalid: "false" }], + } + const requests: string[] = [] + const client = { + async get(path: string): Promise { requests.push(path); return structuredClone(data[path] ?? []) as T }, + async post(path: string, body: Record): Promise { requests.push(path + ":" + body["value-name"]); return { ret: "false" } as T }, + } + return { data, requests, client, read: () => readOspfLinkCatalog(client, family) } +} +const local = fixture(1, "source-vrf", "192.0.2.1", "192.0.2.2", "left", "198.18.0.1/30", "198.18.0.2") +const peer = fixture(2, "different-peer-vrf", "192.0.2.2", "192.0.2.1", "right", "198.18.0.2/30", "198.18.0.1") +const other = fixture(3, "isolated", "192.0.2.2", "192.0.2.1", "other", "198.51.100.3/24", "198.51.100.1") +const source = await local.read() +let target = await peer.read(), isolated = await other.read() +const owner = () => resolveOspfLinkOwner(1, source, "*i1", "*n1", [{ serverId: 1, catalog: source }, { serverId: 2, catalog: target }, { serverId: 3, catalog: isolated }]) +assert.deepEqual(owner(), { serverId: 2, instanceId: "*i2", vrf: "different-peer-vrf", interface: "right" }) +other.data["/ip/address"][0].address = "198.18.0.2/30"; other.data["/routing/ospf/neighbor"][0].address = "198.18.0.1" +isolated = await other.read(); assert.throws(owner, /Неоднозначное/) +other.data["/ip/address"][0].address = "198.51.100.3/24"; isolated = await other.read() +peer.data["/routing/ospf/neighbor"][0].state = "Down"; target = await peer.read(); assert.equal(owner(), null) +peer.data["/routing/ospf/neighbor"][0].state = "Full"; peer.data["/routing/ospf/neighbor"][0].address = "198.18.0.9"; target = await peer.read(); assert.equal(owner(), null) +peer.data["/routing/ospf/neighbor"][0].address = "198.18.0.1"; peer.data["/ip/address"][0].invalid = "true"; target = await peer.read(); assert.equal(owner(), null) +peer.data["/ip/address"][0].invalid = "false"; target = await peer.read() +const stable = target.fingerprint +Object.assign(peer.data["/routing/ospf/neighbor"][0], { adjacency: "9h", "state-changes": "999" }) +assert.equal((await peer.read()).fingerprint, stable, "Runtime counters do not alter link identity") +const second = { ...peer.data["/routing/ospf/instance"][0], ".id": "*i4", name: "another-vrf", vrf: "another-vrf" } +peer.data["/routing/ospf/instance"].push(second) +peer.data["/routing/ospf/neighbor"].push({ ...peer.data["/routing/ospf/neighbor"][0], ".id": "*n4", instance: "another-vrf", interface: "another-iface" }) +peer.data["/ip/address"].push({ ...peer.data["/ip/address"][0], ".id": "*a4", interface: "another-iface" }) +target = await peer.read(); assert.throws(owner, /Неоднозначное/, "Two VRFs on one device remain two contexts") +const local6 = fixture(1, "left-vrf", "192.0.2.1", "192.0.2.2", "left", "fe80::1/64", "fe80::2%left@left-vrf", "ipv6") +const peer6 = fixture(2, "right-vrf", "192.0.2.2", "192.0.2.1", "right", "fe80:0:0:0:0:0:0:2/64", "fe80::1%right@right-vrf", "ipv6") +const a6 = await local6.read(), b6 = await peer6.read() +assert.equal(resolveOspfLinkOwner(1, a6, "*i1", "*n1", [{ serverId: 2, catalog: b6 }])?.vrf, "right-vrf") +peer6.data["/routing/ospf/neighbor"][0].address = "fe80::1%wrong@right-vrf"; await assert.rejects(peer6.read()) +const defaults = fixture(1, "main", "192.0.2.1", "192.0.2.2", "left", "198.18.0.1/30", "198.18.0.2") +delete defaults.data["/ip/address"][0].disabled; delete defaults.data["/ip/address"][0].invalid +assert.equal((await defaults.read()).addresses[0].invalid, false) +assert.ok(defaults.requests.some(path => path.endsWith(":invalid"))) +const guarded = fixture(1, "main", "192.0.2.1", "192.0.2.2", "left", "198.18.0.1/30", "198.18.0.2") +await assert.rejects(readOspfLinkCatalog(guarded.client, "ipv4", async () => { if (guarded.requests.length === 1) throw new Error("Revoked") })) +assert.deepEqual(guarded.requests, ["/routing/ospf/instance"]) +console.log("OSPF links: reciprocal Full and two address/interface proofs, different VRF names, excluded isolated duplicate, ambiguous devices/VRFs refused, IPv6 scope/canonical IP, stable counters and guarded effective address flags") diff --git a/backend/src/services/ospf-link-catalog.ts b/backend/src/services/ospf-link-catalog.ts new file mode 100644 index 00000000..0519cc2f --- /dev/null +++ b/backend/src/services/ospf-link-catalog.ts @@ -0,0 +1,80 @@ +import { isIP } from "node:net" +import { z } from "zod" +import type { MikrotikClient } from "./mikrotik.js" +import { readOspfIdentityCatalog, ospfLiteralRouterId } from "./ospf-identity-catalog.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { ipv6ToInt } from "./traffic-flow-ip.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" + +type Family = "ipv4" | "ipv6" +const text = z.string().min(1).max(200) +const rowsSchema = z.array(z.record(z.string(), z.unknown())).max(10000) +const boolean = (value: unknown) => observedBoolean(typeof value === "boolean" ? String(value) : typeof value === "string" ? value : undefined) +function endpoint(value: unknown, allowPrefix = false) { + const raw = text.parse(value), parts = /^([^%@/]+)(?:%([^@/]+))?(?:@([^/]+))?(?:\/(\d+))?$/.exec(raw) + if (!parts || !allowPrefix && parts[4] !== undefined) throw new Error("Не подтверждён адрес соседства OSPF") + const family = isIP(parts[1]) + if (!family || parts[4] !== undefined && Number(parts[4]) > (family === 4 ? 32 : 128)) throw new Error("Недостоверный IP соседства OSPF") + return { key: family + ":" + (family === 4 ? parts[1] : ipv6ToInt(parts[1])!.toString(16)), family, interface: parts[2] ?? null, vrf: parts[3] ?? null, prefix: parts[4] === undefined ? null : Number(parts[4]) } +} + +export async function readOspfLinkCatalog(client: Pick, family: Family, beforeRead: () => Promise = async () => {}, initial?: { + identity: Awaited>; neighbors: unknown; +}) { + const identity = initial?.identity ?? await readOspfIdentityCatalog(client, beforeRead) + const read = async (path: string) => { await beforeRead(); return client.get(path) } + const rawNeighbors = rowsSchema.parse(initial ? initial.neighbors : await read("/routing/ospf/neighbor")) + const path = family === "ipv4" ? "/ip/address" : "/ipv6/address", rawAddresses = rowsSchema.parse(await read(path)) + for (const rows of [rawNeighbors, rawAddresses]) { + if (rows.some(row => typeof row[".id"] !== "string" || !row[".id"]) || new Set(rows.map(row => row[".id"])).size !== rows.length) throw new Error("Не подтверждены native IDs контекста соседства OSPF") + } + const count = identity.rows + rawNeighbors.length + rawAddresses.length, bytes = identity.bytes + Buffer.byteLength(JSON.stringify({ rawNeighbors, rawAddresses })) + if (count > 10000 || bytes > 1048576) throw new Error("Контекст соседства OSPF превышает 10000 строк или 1 МиБ") + const neighbors = rawNeighbors.map(row => { + const state = text.parse(row.state).toLowerCase(), address = endpoint(row.address), iface = text.parse(row.interface), routerId = ospfLiteralRouterId(row["router-id"]) + if (!routerId || !["down", "attempt", "init", "2-way", "exstart", "exchange", "loading", "full"].includes(state) || address.interface && address.interface !== iface || boolean(row.inactive) === true && state === "full") throw new Error("Не подтверждён контекст native OSPF соседа") + const instance = text.parse(row.instance), owners = identity.instances.filter(item => [item[".id"], item.name].includes(instance)) + if (owners.length !== 1 || state === "full" && (owners[0].disabled !== "false" || address.vrf && address.vrf !== owners[0].vrf)) throw new Error("Не подтверждён единственный экземпляр native OSPF соседа") + return { id: String(row[".id"]), instance, instanceId: owners[0][".id"], interface: iface, routerId, address, state } + }).sort((a, b) => a.id.localeCompare(b.id)) + let getters = 0 + const flag = async (row: Record, name: string) => { + let value = boolean(row[name]) + if (row[name] === undefined) { + if (!/^\*[a-z0-9]+$/i.test(String(row[".id"])) || ++getters > 128) throw new Error("Не подтверждён native ID адреса или превышен бюджет уточнения OSPF") + await beforeRead() + const response = await client.post<{ ret?: unknown }>(path + "/get", { ".id": String(row[".id"]), "value-name": name }) + value = boolean(response?.ret) + } + if (value === null) throw new Error("Не подтверждён флаг адреса контекста OSPF") + return value + } + const addresses = [] + for (const row of rawAddresses) { + const address = endpoint(row.address, true), iface = text.parse(row.interface), disabled = await flag(row, "disabled") + if (address.family !== (family === "ipv4" ? 4 : 6) || address.interface && address.interface !== iface) throw new Error("Семейство или интерфейс адреса OSPF не подтверждены") + addresses.push({ id: String(row[".id"]), address, interface: iface, disabled, invalid: disabled ? boolean(row.invalid) : await flag(row, "invalid") }) + } + addresses.sort((a, b) => a.id.localeCompare(b.id)) + return { identity, neighbors, addresses, rows: count, bytes, fingerprint: fingerprint({ family, identity: identity.fingerprint, neighbors, addresses }) } +} +export type OspfLinkCatalog = Awaited> + +/** Resolve an observed adjacency, not a globally unique ID or equal VRF names. */ +export function resolveOspfLinkOwner(localId: number, local: OspfLinkCatalog, instanceId: string, neighborId: string, candidates: readonly { serverId: number; catalog: OspfLinkCatalog }[]) { + const instance = local.identity.instances.find(row => row[".id"] === instanceId), neighbor = local.neighbors.find(row => row.id === neighborId) + if (!instance || !neighbor || neighbor.instanceId !== instanceId || instance.disabled !== "false" || neighbor.state !== "full" || neighbor.routerId === instance["router-id"] || neighbor.address.vrf && neighbor.address.vrf !== instance.vrf) return null + const localAddresses = local.addresses.filter(row => row.interface === neighbor.interface && !row.disabled && row.invalid === false && (!row.address.vrf || row.address.vrf === instance.vrf)).map(row => row.address.key) + const matches: { serverId: number; instanceId: string; vrf: string; interface: string }[] = [] + for (const { serverId, catalog } of candidates) { + if (serverId === localId) continue + for (const peer of catalog.identity.instances) { + if (peer.disabled !== "false" || peer.version !== instance.version || peer["router-id"] !== neighbor.routerId) continue + const peerAddresses = catalog.addresses.filter(row => row.address.key === neighbor.address.key && !row.disabled && row.invalid === false && (!row.address.vrf || row.address.vrf === peer.vrf)) + const reverse = catalog.neighbors.filter(row => row.instanceId === peer[".id"] && row.state === "full" && row.routerId === instance["router-id"] && localAddresses.includes(row.address.key) && (!row.address.vrf || row.address.vrf === peer.vrf) && peerAddresses.some(address => address.interface === row.interface)) + for (const row of reverse) matches.push({ serverId, instanceId: peer[".id"], vrf: peer.vrf!, interface: row.interface }) + } + } + if (matches.length > 1) throw new McpError("MEASUREMENT_UNKNOWN", "Неоднозначное двустороннее соседство OSPF между устройствами или VRF", 409) + return matches[0] ?? null +} diff --git a/backend/src/services/ospf-optimizer-identity.test.ts b/backend/src/services/ospf-optimizer-identity.test.ts index b993cfc8..5c16cb9f 100644 --- a/backend/src/services/ospf-optimizer-identity.test.ts +++ b/backend/src/services/ospf-optimizer-identity.test.ts @@ -11,4 +11,9 @@ for(const raw of [null,{},[{"router-id":"main",disabled:"false"}],[{"router-id": assert.equal((await resolveOptimizerRouterOwners(devices,needed,async()=>[{disabled:"true"}])).size,0) assert.equal((await resolveOptimizerRouterOwners(devices,needed,async()=>[])).size,0) calls=[];await resolveOptimizerRouterOwners(devices,new Set(),async id=>{calls.push(id);return []});assert.equal(calls.length,0) +const dual = async (id: number) => [{ ...instance, version: id === 1 ? "2" : "3" }] +assert.equal((await resolveOptimizerRouterOwners(devices, needed, dual, { version: "2" })).get("192.0.2.1"), 1) +assert.equal((await resolveOptimizerRouterOwners(devices, needed, dual, { version: "3" })).get("192.0.2.1"), 2) +await assert.rejects(resolveOptimizerRouterOwners(devices, needed, async () => [instance], { version: "2" })) +await assert.rejects(resolveOptimizerRouterOwners(devices, needed, async () => [{ ...instance, version: "2" }], { version: "2" }), /нескольким/) console.log("OSPF optimizer identity: complete inventory, literal IDs, duplicate ownership/unknown/failed refusal; no early first-match") diff --git a/backend/src/services/ospf-optimizer-identity.ts b/backend/src/services/ospf-optimizer-identity.ts index 8c8557b9..54ebbfa2 100644 --- a/backend/src/services/ospf-optimizer-identity.ts +++ b/backend/src/services/ospf-optimizer-identity.ts @@ -1,11 +1,11 @@ import {z} from "zod" import {observedBoolean} from "./ospf-observed-values.js" -const schema=z.array(z.object({"router-id":z.string().optional(),disabled:z.string().optional()})) +const schema=z.array(z.object({"router-id":z.string().optional(),disabled:z.string().optional(),version:z.string().optional()})) function literal(value:string|undefined){ const id=value?.trim(),parts=id?.split(".") return id && id!=="0.0.0.0" && parts?.length===4 && parts.every(part=>/^(0|[1-9]\d{0,2})$/.test(part)&&Number(part)<=255)?id:null } -export async function resolveOptimizerRouterOwners(devices:readonly {id:number}[],needed:Set,read:(id:number)=>Promise):Promise>{ +export async function resolveOptimizerRouterOwners(devices:readonly {id:number}[],needed:Set,read:(id:number)=>Promise,context?:{version:"2"|"3"}):Promise>{ if(needed.size===0)return new Map() const owners=new Map>() for(const device of devices){ @@ -16,6 +16,10 @@ export async function resolveOptimizerRouterOwners(devices:readonly {id:number}[ for(const instance of parsed.data){ const disabled=observedBoolean(instance.disabled) if(disabled===true)continue + if(context){ + if(!["2","3"].includes(instance.version??""))throw new Error("Неизвестна версия OSPF устройства "+device.id) + if(instance.version!==context.version)continue + } const id=literal(instance["router-id"]) if(disabled===null||id===null)throw new Error("Неизвестен Router ID или disabled устройства "+device.id) const set=owners.get(id)??new Set();set.add(device.id);owners.set(id,set) diff --git a/backend/src/services/ospf-read-model.ts b/backend/src/services/ospf-read-model.ts new file mode 100644 index 00000000..6ef83a6a --- /dev/null +++ b/backend/src/services/ospf-read-model.ts @@ -0,0 +1,246 @@ +import type { servers } from "../db/schema.js" +import type { RosOspfNeighbor, RosOspfArea, RosOspfInterfaceTemplate, RosOspfInstance, RosBfdSession, RosIpRoute, OspfNeighborRead, OspfInterfaceRead, OspfInstanceRead, OspfRouteRead, BfdSessionRead } from "../types/server.js" +import { parseOspfGateway, parseOspfRouteType, parseOspfCatalogRouteType } from "./ospf-route-parse.js" +import { observedInteger, observedDurationMs, observedBoolean } from "./ospf-observed-values.js" +type ServerRow = typeof servers.$inferSelect + +// ── helpers ─────────────────────────────────────────────────────────────────── + +/** Parse RouterOS duration string like "1h30m17s", "40s", "10m" → seconds */ +function parseDuration(s: string | undefined): number { + if (!s) return 0 + let total = 0 + for (const m of s.matchAll(/(\d+)(w|d|h|m(?!s)|s)/g)) { + const n = parseInt(m[1]) + switch (m[2]) { + case "w": total += n * 604800; break + case "d": total += n * 86400; break + case "h": total += n * 3600; break + case "m": total += n * 60; break + case "s": total += n; break + } + } + return total +} + +/** Build area-name → area-id lookup from a list of raw areas */ +export function buildAreaMap(areas: RosOspfArea[]): Map { + const map = new Map() + for (const a of areas) { + map.set(a.name, a["area-id"] ?? "0.0.0.0") + } + return map +} + +export function buildObservedAreaMap(areas: RosOspfArea[]): Map { + const candidates = new Map>() + for (const area of areas) { + const value = area["area-id"]?.trim() + if (!value) continue + for (const key of [area.name, area[".id"]]) if (key) candidates.set(key, new Set([...(candidates.get(key) ?? []), value])) + } + return new Map([...candidates].flatMap(([key, values]) => values.size === 1 ? [[key, [...values][0]] as [string, string]] : [])) +} + +/** Parse "200ms" | "1s" | "500ms" → milliseconds */ +function parseMs(s: string | undefined): number { + if (!s) return 0 + const ms = s.match(/^(\d+)ms$/) + if (ms) return parseInt(ms[1]) + const sec = s.match(/^(\d+)s$/) + if (sec) return parseInt(sec[1]) * 1000 + return parseInt(s) || 0 +} + +/** Normalize BFD state: "up" → "Up", "down" → "Down", etc. */ +function normalizeBfdState(state: string | undefined): string { + const map: Record = { + up: "Up", down: "Down", init: "Init", admindown: "AdminDown", + } + return map[(state ?? "").toLowerCase()] ?? (state ?? "Down") +} + +/** Extract IP and interface from RouterOS address format "10.0.0.1%eth0" */ +function parseAddrIface(addr: string): { ip: string; iface: string } { + const [ip, iface = ""] = addr.split("%") + return { ip, iface } +} + +/** Fetch all OSPF + BFD data for one server */ + + +// ── BFD parser ──────────────────────────────────────────────────────────────── + +export function parseBfdSessions(server: ServerRow, raw: RosBfdSession[], strictState = false): BfdSessionRead[] { + return raw.map((s, idx) => { + const local = parseAddrIface(s["local-address"]) + const remote = parseAddrIface(s["remote-address"]) + // Prefer interface from local-address; fallback to remote-address suffix + const iface = local.iface || remote.iface + + return { + id: s[".id"] ?? String(idx), + serverId: server.id, + serverName: server.name || server.host, + serverSite: server.site, + serverCountry: server.country, + localAddr: local.ip, + remoteAddr: remote.ip, + interface: iface, + state: strictState && !s["state"]?.trim() ? "Unknown" : normalizeBfdState(s["state"]), + uptime: s["uptime"] ?? null, + multihop: s["multihop"] === "true", + multiplier: parseInt(s["multiplier"] ?? "3") || 3, + txInterval: parseMs(s["actual-tx-interval"] ?? s["desired-tx-interval"]), + rxInterval: parseMs(s["required-min-rx"]), + holdTime: parseMs(s["hold-time"]), + packetsRx: parseInt(s["packets-rx"] ?? "0") || 0, + packetsTx: parseInt(s["packets-tx"] ?? "0") || 0, + stateChanges: parseInt(s["state-changes"] ?? "0") || 0, + } + }) +} + +export function parseBfdCatalogSessions(server: ServerRow, raw: RosBfdSession[]) { + return parseBfdSessions(server, raw, true).map((row, index) => { + const source = raw[index] + return { ...row, multihop: observedBoolean(source.multihop), multiplier: observedInteger(source.multiplier, 0, 255), txInterval: observedDurationMs(source["actual-tx-interval"]), rxInterval: observedDurationMs(source["required-min-rx"]), holdTime: observedDurationMs(source["hold-time"]), packetsRx: observedInteger(source["packets-rx"]), packetsTx: observedInteger(source["packets-tx"]), stateChanges: observedInteger(source["state-changes"]) } + }) +} +export function parseCatalogNeighbors(server: ServerRow, raw: RosOspfNeighbor[], areaMap: Map) { + return parseNeighbors(server, raw, areaMap).map((row, index) => ({ ...row, areaId: areaMap.get(raw[index].area) ?? null, state: raw[index].state?.trim() || "Unknown", priority: observedInteger(raw[index].priority, 0, 255), stateChanges: observedInteger(raw[index]["state-changes"]) })) +} + +// ── parsers ─────────────────────────────────────────────────────────────────── + +export function parseNeighbors( + server: ServerRow, + neighbors: RosOspfNeighbor[], + areaMap: Map, +): OspfNeighborRead[] { + return neighbors.map((n, idx) => ({ + id: n[".id"] ?? String(idx), + serverId: server.id, + serverName: server.name || server.host, + serverSite: server.site, + serverCountry: server.country, + address: n.address, + routerId: n["router-id"], + instance: n.instance, + area: n.area, + areaId: areaMap.get(n.area) ?? n.area, + interface: n.interface, + state: n.state, + uptime: n.adjacency ?? null, + stateChanges: parseInt(n["state-changes"] ?? "0") || 0, + priority: parseInt(n.priority ?? "1") || 1, + })) +} + +function indexOspfReferences(rows: T[]) { + const index = new Map() + for (const row of rows) for (const key of new Set([row[".id"], row.name])) { + if (key) index.set(key, index.has(key) ? null : row) + } + return index +} + +export function parseInterfaces( + server: ServerRow, + templates: RosOspfInterfaceTemplate[], + areas: RosOspfArea[], + instances: RosOspfInstance[], + areaMap: Map, +): OspfInterfaceRead[] { + const areaByRef = indexOspfReferences(areas) + const instanceByRef = indexOspfReferences(instances) + + return templates.map((t, idx) => { + // Resolve interface name: may be a "*ID" reference (RouterOS internal ID) + const ifaceName = (t.interfaces ?? "").startsWith("*") + ? `(ref ${t.interfaces})` + : (t.interfaces ?? "—") + const area = areaByRef.get(t.area) + const instance = area && instanceByRef.get(area.instance) + + return { + id: t[".id"] ?? String(idx), + serverId: server.id, + serverName: server.name || server.host, + serverSite: server.site, + serverCountry: server.country, + instance: instance?.name ?? "", + area: t.area, + areaId: areaMap.get(t.area) ?? t.area, + interface: ifaceName, + cost: parseInt(t.cost ?? "10") || 10, + type: t.type ?? "broadcast", + disabled: t.disabled === "true", + inactive: t.inactive === "true", + priority: parseInt(t.priority ?? "1") || 1, + helloInterval: parseDuration(t["hello-interval"]), + deadInterval: parseDuration(t["dead-interval"]), + useBfd: t["use-bfd"] === "true", + } + }) +} + +export function parseCatalogInterfaces(server: ServerRow, raw: RosOspfInterfaceTemplate[], areas: RosOspfArea[], instances: RosOspfInstance[], areaMap: Map) { + return parseInterfaces(server, raw, areas, instances, areaMap).map((row, index) => { + const source = raw[index] + const helloMs = observedDurationMs(source["hello-interval"]), deadMs = observedDurationMs(source["dead-interval"]) + return { ...row, networks: typeof source.networks === "string" ? source.networks : null, instance: row.instance || null, instanceId: observedInteger(source["instance-id"], 0, 255), areaId: areaMap.get(source.area) ?? null, cost: observedInteger(source.cost, 0, 65535), priority: observedInteger(source.priority, 0, 255), type: source.type?.trim() || null, disabled: observedBoolean(source.disabled), inactive: observedBoolean(source.inactive), useBfd: observedBoolean(source["use-bfd"]), helloInterval: helloMs === null ? null : helloMs / 1000, deadInterval: deadMs === null ? null : deadMs / 1000 } + }) +} +export function parseCatalogInstances(server: ServerRow, raw: RosOspfInstance[]) { + return parseInstances(server, raw).map((row, index) => ({ ...row, version: ["2", "3"].includes(raw[index].version) ? Number(raw[index].version) : null, routerId: raw[index]["router-id"]?.trim() || null, disabled: observedBoolean(raw[index].disabled), inactive: observedBoolean(raw[index].inactive) })) +} +export function parseCatalogRoutes(server: ServerRow, raw: RosIpRoute[]) { + return raw.flatMap((source, index) => { + const rows = parseOspfRoutes(server, [{ ...source, ".id": source[".id"] ?? String(index) }]) + return rows.map(row => ({ ...row, type: parseOspfCatalogRouteType(source) ?? "Unknown", cost: observedInteger(source["ospf-metric"], 0, 4294967295) })) + }) +} + +export function parseInstances( + server: ServerRow, + instances: RosOspfInstance[], +): OspfInstanceRead[] { + return instances.map((inst, idx) => ({ + id: inst[".id"] ?? String(idx), + serverId: server.id, + serverName: server.name || server.host, + serverSite: server.site, + serverCountry: server.country, + name: inst.name, + routerId: inst["router-id"], + version: parseInt(inst.version ?? "2") || 2, + disabled: inst.disabled === "true", + inactive: inst.inactive === "true", + redistribute: inst.redistribute ?? "", + })) +} + +export function parseOspfRoutes(server: ServerRow, routes: RosIpRoute[]): OspfRouteRead[] { + const out: OspfRouteRead[] = [] + for (const [idx, r] of routes.entries()) { + const type = parseOspfRouteType(r) + if (!type) continue + const { nextHop, via } = parseOspfGateway(r) + const metric = parseInt(r["ospf-metric"] ?? r.distance ?? "0") || 0 + out.push({ + id: r[".id"] ?? String(idx), + serverId: server.id, + serverName: server.name || server.host, + serverSite: server.site, + destination: r["dst-address"] ?? "", + type, + cost: metric, + nextHop, + via, + area: r["ospf-area"] ?? "", + }) + } + return out +} + diff --git a/backend/src/services/ospf-route-proof.test.ts b/backend/src/services/ospf-route-proof.test.ts new file mode 100644 index 00000000..57e1bba4 --- /dev/null +++ b/backend/src/services/ospf-route-proof.test.ts @@ -0,0 +1,42 @@ +import assert from "node:assert/strict" +import { 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" } +let rows: Record[] = [{ ".id": "*a", "dst-address": selection.prefix, "routing-table": selection.table, ospf: "true", active: "true", "ospf.metric": "60", "immediate-gw": "198.18.1.2%owned-gre@local-vrf", contribution: "active", secret: "NEVER_RIB_PRIVATE" }] +let getters: string[] = [] +let getterValues: Record = { ospf: "true", active: "true", "ospf.metric": "40" } +const client = { get: async () => rows, post: async (_path: string, body: Record) => { getters.push(body["value-name"]); return { ret: getterValues[body["value-name"]] } } } as unknown as Parameters[0] +const proof = await readOspfRouteProof(client, selection) +assert.equal(proof.metric, 60); assert.equal(JSON.stringify(proof).includes("NEVER_"), false) +const original = structuredClone(rows) +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" }]) { + rows = [{ ...original[0], ...patch }]; await assert.rejects(readOspfRouteProof(client, selection)) +} +rows = [...original, ...original]; await assert.rejects(readOspfRouteProof(client, selection)) +rows = [original[0], { ...original[0], ".id": "*b" }]; await assert.rejects(readOspfRouteProof(client, selection)) +rows = original.map(row => ({ ...row, "routing-table": "foreign-vrf" })); await assert.rejects(readOspfRouteProof(client, selection)) +rows = original.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !["ospf", "active", "ospf.metric"].includes(key)))) +getters = []; assert.equal((await readOspfRouteProof(client, selection)).metric, 40); assert.deepEqual(getters, ["ospf", "active", "ospf.metric"]) +getterValues = {}; await assert.rejects(readOspfRouteProof(client, selection), "Missing getter receipts must never imply active OSPF") +rows = [{ ...original[0], "dst-address": "fd6d:6370:1:2::2/128", "immediate-gw": "fe80::2%owned-gre" }] +assert.equal((await readOspfRouteProof(client, { ...selection, prefix: "fd6d:6370:1:2::2/128" })).family, "ipv6") +const ipv6Selection = { ...selection, prefix: "FD6D:6370:1:2:0:0:0:2/128" } +const activeRoute = rows[0] +const alternative = { ".id": "*b", "dst-address": ipv6Selection.prefix, "routing-table": selection.table } +rows = [activeRoute, { ...alternative, contribution: "best-candidate" }, { ...alternative, ".id": "*c", active: "false" }] +getters = [] +assert.equal((await readOspfRouteProof(client, ipv6Selection)).id, "*a") +assert.deepEqual(getters, []) +for (const patch of [{}, { active: "true", contribution: "best-candidate" }, { active: "false", contribution: "active" }, { active: "unknown", contribution: "candidate" }, { active: "true", ospf: "false" }]) { + rows = [activeRoute, { ...alternative, ...patch }] + await assert.rejects(readOspfRouteProof(client, ipv6Selection), /ambiguous/) +} +rows = [{ ...original[0], opaque: "x".repeat(1048576) }]; await assert.rejects(readOspfRouteProof(client, selection)) +rows = Array.from({ length: 10001 }, (_, index) => ({ ".id": "*" + index.toString(16) })); await assert.rejects(readOspfRouteProof(client, selection)) +assert.equal(chrFixtureVrf("mcp-test-1234abcd", 1, false), "mcp-test-1234abcd-vrf") +assert.notEqual(chrFixtureVrf("mcp-test-1234abcd", 1, true), chrFixtureVrf("mcp-test-1234abcd", 2, true)) +assert.throws(() => chrFixtureVrf("foreign", 1, true)); assert.throws(() => chrFixtureVrf("mcp-test-1234abcd", 3, true)) +console.log("OSPF operational proof: explicit active/protocol/metric, scoped prefix/VRF/interface, getters, IPv6 and bounded private projection") diff --git a/backend/src/services/ospf-route-proof.ts b/backend/src/services/ospf-route-proof.ts new file mode 100644 index 00000000..23067c10 --- /dev/null +++ b/backend/src/services/ospf-route-proof.ts @@ -0,0 +1,80 @@ +import { isIP } from "node:net" +import { z } from "zod" +import type { MikrotikClient } from "./mikrotik.js" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { observedBoolean, observedInteger } from "./ospf-observed-values.js" + +export class OspfRouteProofError extends Error {} + +const inactiveContributions = new Set(["filtered", "unreachable", "candidate", "best-candidate"]) +const flag = (value: unknown) => observedBoolean(typeof value === "boolean" ? String(value) : typeof value === "string" ? value : undefined) +const nativeRows = z.array(z.record(z.string(), z.unknown())).max(10000) +const nonIpRouteFamilies = new Set(["link", "vpnv4", "vpnv6", "l2vpn", "l2vpn-cisco", "l2vpn-link", "evpn", "mip4", "mip6"]) + +export function canonicalOspfRouteProofPrefix(value: unknown) { + if (typeof value !== "string") return null + try { return canonicalBgpPolicyPrefix(value) } catch { return null } +} + +async function readBoundedRows(client: Pick, path: string, beforeRead: () => Promise) { + await beforeRead() + const raw = await client.get(path, 10_000, undefined, 1_048_576) + if (Buffer.byteLength(JSON.stringify(raw) ?? "") > 1048576) throw new OspfRouteProofError("OSPF RIB proof exceeds 1 MiB") + const rows = nativeRows.parse(raw) + if (rows.some(row => typeof row[".id"] !== "string" || !row[".id"]) || new Set(rows.map(row => row[".id"])).size !== rows.length) throw new OspfRouteProofError("Invalid OSPF RIB identities") + return rows +} + +export async function readOspfRouteAbsence(client: Pick, selection: { prefix: string; table: string }, beforeRead: () => Promise) { + 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) + for (const row of routes) { + const observed = row["dst-address"] + const scoped = typeof observed === "string" ? /^([0-9a-f:]+)%[^/%]+(\/\d+)$/i.exec(observed) : null + const destination = canonicalOspfRouteProofPrefix(scoped ? scoped[1] + scoped[2] : observed) + if (destination === null && nonIpRouteFamilies.has(row.afi)) continue + const table = row["routing-table"] + const unknownTable = typeof table !== "string" || !table.trim() + if ((unknownTable || table === selection.table) && (destination === null || destination === prefix)) { + throw new OspfRouteProofError("A matching route is present or cannot be excluded") + } + } + const tables = await readBoundedRows(client, "/routing/table?.proplist=.id,name,disabled,invalid", beforeRead) + const matching = tables.filter(row => row.name === selection.table) + if (matching.length !== 1 || ["disabled", "invalid"].some(key => matching[0][key] !== undefined && flag(matching[0][key]) !== false)) throw new OspfRouteProofError("Routing table is absent, disabled, invalid or ambiguous") + await beforeRead() + return { prefix, table: selection.table, absenceVerified: true as const } +} + +/** A bounded operational proof. Missing protocol/active/metric values require native receipts. */ +export async function readOspfRouteProof(client: Pick, selection: { prefix: string; table: string; interface: string }, beforeRead: () => Promise = async () => {}) { + 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 matches = rows.filter(row => { + if (row["routing-table"] !== selection.table || canonicalOspfRouteProofPrefix(row["dst-address"]) !== prefix) return false + const active = flag(row.active) + if (active === false && row.contribution === undefined) return false + if ((active === false || row.active === undefined || row.active === "") && inactiveContributions.has(row.contribution)) return false + return true + }) + if (matches.length !== 1) throw new OspfRouteProofError("OSPF route is missing or ambiguous") + const row = matches[0] + const property = async (key: string) => { + if (row[key] !== undefined && row[key] !== "") return row[key] + if (!/^\*[a-z0-9]+$/i.test(String(row[".id"]))) throw new OspfRouteProofError("Invalid native route getter identity") + await beforeRead() + return (await client.post<{ ret?: unknown }>("/routing/route/get", { ".id": String(row[".id"]), "value-name": key })).ret + } + if (flag(await property("ospf")) !== true || flag(await property("active")) !== true || row.contribution !== undefined && row.contribution !== "active") throw new OspfRouteProofError("Active OSPF route not confirmed") + const value = await property("ospf.metric") + const metric = observedInteger(typeof value === "number" ? String(value) : typeof value === "string" ? value : undefined, 0, 0xffffff) + 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") + 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 } +} diff --git a/backend/src/services/ospf-state-read.test.ts b/backend/src/services/ospf-state-read.test.ts new file mode 100644 index 00000000..fc1b2828 --- /dev/null +++ b/backend/src/services/ospf-state-read.test.ts @@ -0,0 +1,104 @@ +import assert from "node:assert/strict" +import { MikrotikClient } from "./mikrotik.js" +import { readOspfStateForServer, readOspfMcpStateForServer, readBfdStateForServer } from "./ospf-state-read.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { buildObservedAreaMap } from "./ospf-read-model.js" + +const server = { id: 1, name: "Router", enabled: true } as ServerRow +const fromServer = MikrotikClient.fromServer +let calls = 0, fail = false, duplicate = false, knownArea = false, invalidV6 = false +let bfdUnavailable = false, v4Unavailable = false +const read = async () => { calls++; if (fail) throw new Error("fixture unavailable") } +const client = { + getOspfNeighbors: async () => { await read(); return [{ ".id": "*n", area: "area1", priority: "0" }] }, + getOspfAreas: async () => { await read(); return [{ ".id": "*a", name: "area1", ...(knownArea ? { "area-id": "0.0.0.0", instance: "*i" } : {}) }] }, + getOspfInterfaceTemplates: async () => { await read(); return [{ ".id": "*t", area: "area1" }] }, + getOspfInstances: async () => { await read(); const row = { ".id": "*i", name: "ospf1" }; return duplicate ? [row, row] : [row] }, + getBfdSessions: async () => { await read(); if (bfdUnavailable) throw new Error("BFD unavailable"); return [{ ".id": "*b", "local-address": "192.0.2.1%ether1", "remote-address": "192.0.2.2", "packets-rx": "0", "desired-tx-interval": "200ms" }] }, + getIpRoutes: async () => { await read(); if (v4Unavailable) throw new Error("IPv4 unavailable"); return [] }, getIpv6Routes: async () => { await read(); return invalidV6 ? [{ "dst-address": "fd00::/64" }] : [] }, + get: async (path: string) => { + if (path.startsWith("/routing/ospf/lsa?")) { + assert.doesNotMatch(path, /body/) + return lsaRows + } + assert.match(path, /^\/routing\/ospf\/interface\?\.proplist=/) + assert.doesNotMatch(path, /auth-key/) + if (runtimeRows === null) throw new Error("fixture menu unavailable") + return runtimeRows + }, +} +const runtimeRow = { ".id": "*r", interface: "gre-1", state: "ptp", priority: "0", passive: "false", "auth-key": "fixture-secret" } +let runtimeRows: unknown = [runtimeRow] +const lsaRow = { ".id": "*lsa", id: "0.0.0.1", age: "invalid", body: "fixture-body" } +let lsaRows = [lsaRow] +try { + MikrotikClient.fromServer = () => client as unknown as MikrotikClient + const state = await readOspfStateForServer(server) + assert.equal(calls, 7) + assert.equal(state.legacy.interfaces[0].cost, 10); assert.equal(state.observed.interfaces[0].cost, null) + assert.equal(state.legacy.instances[0].version, 2); assert.equal(state.observed.instances[0].version, null) + assert.equal(state.legacy.neighbors[0].priority, 1); assert.equal(state.observed.neighbors[0].priority, 0) + assert.equal(state.legacy.bfdSessions[0].state, "Down"); assert.equal(state.observed.bfdSessions[0].state, "Unknown") + assert.equal(state.legacy.bfdSessions[0].txInterval, 200); assert.equal(state.observed.bfdSessions[0].txInterval, null) + assert.equal(state.observed.bfdSessions[0].packetsRx, 0) + assert.equal(state.observed.areas[0].areaId, null); assert.equal(state.legacy.areas[0].areaId, "0.0.0.0") + assert.equal(state.observed.areas[0].disabled, null) + assert.equal(state.observed.interfaces[0].areaId, null); assert.equal(state.observed.neighbors[0].areaId, null) + assert.equal(state.observed.interfaces[0].instance, null); assert.equal(state.observed.neighbors[0].state, "Unknown") + const bfd = await readBfdStateForServer(server) + assert.equal(calls, 8); assert.deepEqual(bfd.observed, state.observed.bfdSessions) + await assert.rejects(readOspfStateForServer({ ...server, enabled: false }), /выключен/) + await assert.rejects(readBfdStateForServer({ ...server, enabled: false }), /выключен/); assert.equal(calls, 8) + knownArea = true + const linked = await readOspfStateForServer(server) + assert.equal(linked.observed.interfaces[0].instance, "ospf1"); assert.equal(linked.observed.interfaces[0].areaId, "0.0.0.0") + const ambiguous = buildObservedAreaMap([{ ".id": "*a", name: "same", instance: "i1", "area-id": "0.0.0.0" }, { ".id": "*b", name: "same", instance: "i2", "area-id": "0.0.0.1" }]) + assert.equal(ambiguous.has("same"), false); assert.equal(ambiguous.get("*a"), "0.0.0.0"); assert.equal(ambiguous.get("*b"), "0.0.0.1") + invalidV6 = true; await assert.rejects(readOspfStateForServer(server), /native ID/); invalidV6 = false + duplicate = true; await assert.rejects(readOspfStateForServer(server), /идентификаторы/) + duplicate = false + const mcpState = await readOspfMcpStateForServer(server) + assert.equal(Object.hasOwn(mcpState, "lsas"), false) + const lsa = (await readOspfMcpStateForServer(server, undefined, true)).lsas?.[0] + assert.equal(lsa?.linkStateId, "0.0.0.1"); assert.equal(lsa?.ageSeconds, null) + assert.equal(Object.hasOwn(lsa!, "body"), false) + lsaRows = [lsaRow, lsaRow] + assert.equal((await readOspfMcpStateForServer(server, undefined, true)).lsas, null) + assert.equal(mcpState.interfaces[0].id, "*t") + assert.deepEqual(mcpState.runtimeInterfaces, [{ id: "*r", serverId: 1, interface: "gre-1", address: null, area: null, type: null, state: "ptp", dr: null, bdr: null, cost: null, priority: 0, dynamic: null, disabled: null, passive: false, useBfd: null }]) + runtimeRows = [] + assert.deepEqual((await readOspfMcpStateForServer(server)).runtimeInterfaces, []) + for (const missing of [null, [runtimeRow, runtimeRow]]) { + runtimeRows = missing + const partial = await readOspfMcpStateForServer(server) + assert.equal(partial.runtimeInterfaces, null) + assert.equal(partial.neighbors[0].id, "*n") + } + runtimeRows = [runtimeRow] + bfdUnavailable = true + const partialBfd = await readOspfMcpStateForServer(server) + assert.equal(partialBfd.bfdSessions, null) + assert.equal(partialBfd.neighbors[0].id, "*n") + assert.equal(partialBfd.runtimeInterfaces?.[0].id, "*r") + assert.deepEqual(partialBfd.routes, []) + await assert.rejects(readBfdStateForServer(server), /BFD unavailable/) + await assert.rejects(readOspfStateForServer(server), /BFD unavailable/) + bfdUnavailable = false + for (const family of [4, 6]) { + v4Unavailable = family === 4 + invalidV6 = family === 6 + const partialRoutes = await readOspfMcpStateForServer(server) + assert.equal(partialRoutes.routes, null) + assert.equal(partialRoutes.bfdSessions?.[0].id, "*b") + assert.equal(partialRoutes.neighbors[0].id, "*n") + } + invalidV6 = false + const abort = new AbortController() + abort.abort() + const beforeAbort = calls + await assert.rejects(readOspfMcpStateForServer(server, abort.signal), { name: "AbortError" }) + assert.equal(calls, beforeAbort) + duplicate = false; fail = true; await assert.rejects(readOspfStateForServer(server), /fixture unavailable/) + await assert.rejects(readBfdStateForServer(server), /fixture unavailable/) +} finally { MikrotikClient.fromServer = fromServer } +console.log("Shared OSPF/BFD state: one poll, legacy parity, nullable observations/known zero, areas, BFD-only source, disabled/duplicate/failure passed") diff --git a/backend/src/services/ospf-state-read.ts b/backend/src/services/ospf-state-read.ts new file mode 100644 index 00000000..caacbbb2 --- /dev/null +++ b/backend/src/services/ospf-state-read.ts @@ -0,0 +1,100 @@ +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { MikrotikClient } from "./mikrotik.js" +import { readOspfCatalogRaw, readOspfCoreRaw, readOspfRoutesRaw, type OspfCatalogCollections } from "./ospf-catalog-poll.js" +import { buildAreaMap, buildObservedAreaMap, parseNeighbors, parseInterfaces, parseInstances, parseOspfRoutes, parseBfdSessions, parseBfdCatalogSessions, parseCatalogNeighbors, parseCatalogInterfaces, parseCatalogInstances, parseCatalogRoutes } from "./ospf-read-model.js" +import { observedBoolean, observedInteger } from "./ospf-observed-values.js" + +function assertSource(server: ServerRow) { + if (server.enabled === false) throw new Error("Сервер выключен. Опрос OSPF/BFD не выполнялся") +} +function validateRows(rows: unknown): asserts rows is (Record & { ".id": string })[] { + if (!Array.isArray(rows) || rows.length > 10000 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !row[".id"].trim()) || new Set(rows.map(row => row[".id"])).size !== rows.length) throw new Error("Недостоверные идентификаторы OSPF/BFD") +} + +function observeCore(server: ServerRow, { neighbors, areas, ifaceTemplates, instances }: Awaited>) { + const areaMap = buildObservedAreaMap(areas) + return { + neighbors: parseCatalogNeighbors(server, neighbors, areaMap), interfaces: parseCatalogInterfaces(server, ifaceTemplates, areas, instances, areaMap), + instances: parseCatalogInstances(server, instances), + areas: areas.map(area => ({ id: area[".id"], serverId: server.id, name: area.name, areaId: area["area-id"]?.trim() || null, type: area.type?.trim() || null, disabled: observedBoolean(area.disabled), inactive: observedBoolean(area.inactive), instance: area.instance })), + } +} + +/** One poll and shared models for the legacy REST views and nullable MCP DTOs. + * Compatibility defaults stay in legacy only; observed never fabricates them. */ +export async function readOspfStateForServer(server: ServerRow, client?: MikrotikClient) { + assertSource(server) + const raw = await readOspfCatalogRaw(client ?? MikrotikClient.fromServer(server)) + for (const rows of Object.values(raw)) validateRows(rows) + const { neighbors, areas, ifaceTemplates, instances, bfdSessions, ipRoutes } = raw, areaMap = buildAreaMap(areas) + const legacy = { + neighbors: parseNeighbors(server, neighbors, areaMap), interfaces: parseInterfaces(server, ifaceTemplates, areas, instances, areaMap), + instances: parseInstances(server, instances), bfdSessions: parseBfdSessions(server, bfdSessions), routes: parseOspfRoutes(server, ipRoutes), + areas: areas.map(area => ({ name: area.name, areaId: area["area-id"] ?? "0.0.0.0", type: area.type, disabled: area.disabled === "true", inactive: area.inactive === "true", instance: area.instance })), + } + const observed = { + ...observeCore(server, raw), bfdSessions: parseBfdCatalogSessions(server, bfdSessions), routes: parseCatalogRoutes(server, ipRoutes), + } + return { legacy, observed } +} + +const runtimeStates = new Set(["down", "loopback", "ptp", "passive", "waiting", "standby", "dr-other", "bdr", "dr"]) +const runtimeFields = ".id,interface,address,area,type,state,dr,bdr,cost,priority,dynamic,disabled,passive,use-bfd" +const lsaFields = ".id,id,instance,area,type,originator,sequence,age,self-originated,flushing" + +async function readOspfLsaMetadata(client: MikrotikClient, serverId: number, signal?: AbortSignal) { + const rows = await client.get(`/routing/ospf/lsa?.proplist=${lsaFields}`, 10_000, signal, 1_048_576) + validateRows(rows) + return rows.map(row => { + const text = (key: string) => typeof row[key] === "string" ? row[key].trim() || null : null + return { + id: row[".id"], serverId, linkStateId: text("id"), instance: text("instance"), area: text("area"), + type: text("type"), originator: text("originator"), sequence: text("sequence"), + ageSeconds: observedInteger(text("age") ?? undefined), + selfOriginated: observedBoolean(text("self-originated") ?? undefined), flushing: observedBoolean(text("flushing") ?? undefined), + } + }) +} + +export async function readOspfMcpStateForServer(server: ServerRow, signal?: AbortSignal, includeLsas = false) { + assertSource(server) + signal?.throwIfAborted() + const client = MikrotikClient.fromServer(server) + const runtimeRead = async () => { + const rows = await client.get(`/routing/ospf/interface?.proplist=${runtimeFields}`, 10_000, signal, 1_048_576) + validateRows(rows) + return rows.map(row => { + const text = (key: string) => typeof row[key] === "string" ? row[key].trim() || null : null + const state = text("state") + return { + id: row[".id"], serverId: server.id, interface: text("interface"), address: text("address"), area: text("area"), + type: text("type"), state: state && runtimeStates.has(state) ? state : null, dr: text("dr"), bdr: text("bdr"), + cost: observedInteger(text("cost") ?? undefined), priority: observedInteger(text("priority") ?? undefined, 0, 255), + dynamic: observedBoolean(text("dynamic") ?? undefined), disabled: observedBoolean(text("disabled") ?? undefined), + passive: observedBoolean(text("passive") ?? undefined), useBfd: observedBoolean(text("use-bfd") ?? undefined), + } + }) + } + const [core, runtimeInterfaces, bfdSessions, routes, lsas] = await Promise.all([ + readOspfCoreRaw(client), + runtimeRead().catch(() => null), + client.getBfdSessions().then(rows => { validateRows(rows); return parseBfdCatalogSessions(server, rows) }).catch(() => null), + readOspfRoutesRaw(client).then(rows => { validateRows(rows); return parseCatalogRoutes(server, rows) }).catch(() => null), + includeLsas ? readOspfLsaMetadata(client, server.id, signal).catch(() => null) : undefined, + ]) + signal?.throwIfAborted() + for (const rows of Object.values(core)) validateRows(rows) + return { ...observeCore(server, core), runtimeInterfaces, bfdSessions, routes, ...(lsas === undefined ? {} : { lsas }) } +} + +export async function readBfdStateForServer(server: ServerRow) { + assertSource(server) + const raw = await MikrotikClient.fromServer(server).getBfdSessions() + validateRows(raw) + return { legacy: parseBfdSessions(server, raw), observed: parseBfdCatalogSessions(server, raw) } +} + +export async function readOspfCatalogForServer(server: ServerRow): Promise { + const { areas: _areas, ...collections } = (await readOspfStateForServer(server)).observed + return collections +} diff --git a/backend/src/services/ospf-template-cost-http.test.ts b/backend/src/services/ospf-template-cost-http.test.ts new file mode 100644 index 00000000..62f3ea76 --- /dev/null +++ b/backend/src/services/ospf-template-cost-http.test.ts @@ -0,0 +1,57 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { MikrotikClient } from "./mikrotik.js" +import { OspfCostWriteError } from "./ospf-template-cost-write.js" +import { pool, closePool } from "../db/index.js" +import { configurationAuditTransactionFixture, emptyActiveRoutingProfilesSqlFixture } from "../test/fixtures/managed-mutation-sql.js" +let writes = 0 +let reads = 0 +const rows = [{ ".id": "*1", cost: "10", disabled: "false", "auth-key": "NEVER_OSPF_AUTH" }] + +const http = createServer(async (request, response) => { + if (request.method === "GET" && request.url === "/rest/routing/ospf/interface-template") { reads++; response.setHeader("Content-Type", "application/json"); response.end(JSON.stringify(rows)); return } + if (request.method === "PATCH" && request.url?.endsWith("/interface-template/*1")) { + let body = ""; for await (const chunk of request) body += chunk + writes++; rows[0].cost = JSON.parse(body).cost + response.setHeader("Content-Type", "application/json"); response.end("{}"); return + } + response.statusCode = 404; response.end("{}") +}) +await new Promise(resolve => http.listen(0, "127.0.0.1", resolve)) +const originalQuery = pool.query, originalConnect = pool.connect +const address = http.address(); assert.ok(address && typeof address !== "string") +pool.connect = (async () => ({ async query(text: string, values?: unknown[]) { + const admission = configurationAuditTransactionFixture(text, values) + if (admission) return admission + if (text.startsWith("INSERT INTO mcp_audit(")) return pool.query(text, values) + if (text.includes("'mm-config:application'")) { assert.equal(values, undefined); return { rows: [{ acquired: true }] } } + assert(["SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired", "SELECT pg_advisory_unlock(hashtextextended($1,0))"].includes(text)); assert.deepEqual(values, ["mm-config:71"]); return { rows: [{ acquired: true }] } +}, release() {} })) as typeof pool.connect +pool.query = (async (text: string, values?: unknown[]) => { + const routing = emptyActiveRoutingProfilesSqlFixture(text, values, 71) + if (routing) return routing + if (text.startsWith("SELECT host,port,use_ssl,verify_ssl,username,password,enabled,routing_endpoint_epoch FROM servers")) { assert.deepEqual(values, [71]); return { rows: [{ host: "127.0.0.1", port: address.port, use_ssl: false, verify_ssl: false, username: "fixture", password: "fixture", enabled: true, routing_endpoint_epoch: undefined }] } } + if (text.startsWith("SELECT object_ref FROM mcp_protections WHERE")) { assert.deepEqual(values, ["71"]); return { rows: [] } } + assert.equal(text, "INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)"); assert.equal(JSON.parse(String(values?.[3])).serverId, 71); return { rows: [] } +}) as typeof pool.query +try { + const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, username: "fixture", password: "fixture", useSsl: false, verifySsl: false }, 71) + await client.setOspfInterfaceTemplateCost("*1", 20, 10) + assert.equal(writes, 1); assert.equal(rows[0].cost, "20") + await assert.rejects(client.setOspfInterfaceTemplateCost("*1", 30, 10)); assert.equal(writes, 1) + let gates=0 + const beforeGateReads=reads + await assert.rejects(client.setOspfInterfaceTemplateCost("*1",30,20,undefined,async()=>{ + gates++;assert.equal(reads,beforeGateReads+1);assert.equal(writes,1) + throw Error("NEVER_PUBLISH_SOURCE_ERROR") + }),error=>error instanceof OspfCostWriteError&&!error.writeAttempted&&!error.message.includes("NEVER_PUBLISH")) + assert.equal(writes,1);assert.equal(gates,1);assert.equal(rows[0].cost,"20") + const beforeAcceptedReads=reads + await client.setOspfInterfaceTemplateCost("*1",30,20,undefined,async()=>{ + gates++;assert.equal(reads,beforeAcceptedReads+1);assert.equal(writes,1);assert.equal(rows[0].cost,"20") + }) + assert.equal(writes,2);assert.equal(gates,2);assert.equal(reads,beforeAcceptedReads+2) + await client.setOspfInterfaceTemplateCost("*1",30,30,undefined,async()=>{throw Error("No-op must not dispatch")}) + assert.equal(writes,2);assert.equal(gates,2) +} finally { pool.query = originalQuery; pool.connect = originalConnect; await new Promise(resolve => http.close(() => resolve())); await closePool() } +console.log("✓ OSPF cost shared writer: expected cost, whole native read-back, opaque/auth preservation, ignored/lost/rejected stop, no replay and actual client HTTP") diff --git a/backend/src/services/ospf-template-cost-write.test.ts b/backend/src/services/ospf-template-cost-write.test.ts new file mode 100644 index 00000000..bb0a5522 --- /dev/null +++ b/backend/src/services/ospf-template-cost-write.test.ts @@ -0,0 +1,72 @@ +import assert from "node:assert/strict" +import { writeOspfTemplateCost, OspfCostWriteError, ospfCostConnectionProof } from "./ospf-template-cost-write.js" +import { MikrotikClient, MikrotikError } from "./mikrotik.js" +import { mkdtemp, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, resolve, sep, basename } from "node:path" + +const baseline = [{ ".id": "*1", interfaces: "gre-one", cost: "10", disabled: "false", "auth-key": "NEVER_OSPF_AUTH", opaque: "preserve" }, { ".id": "*2", interfaces: "gre-two", cost: "30", disabled: "false" }] +let rows = structuredClone(baseline), writes = 0, reads = 0, mode = "normal" +const dependencies = { expectedCost: 10, read: async () => { + reads++ + if (mode === "lost-read" && writes) throw new MikrotikError(401, "/routing/ospf/interface-template", "read refused") + if (mode === "malformed" && writes) return { error: "invalid" } + return structuredClone(rows) +}, write: async (id: string, cost: number) => { + writes++ + if (mode === "lost-write") throw new Error("Lost PATCH acknowledgement") + if (mode === "rejected-write") throw new MikrotikError(403, "/routing/ospf/interface-template", "refused") + if (mode !== "ignored" && !(mode === "second-ignored" && id === "*2")) rows.find(row => row[".id"] === id)!.cost = String(cost) + if (mode === "foreign") rows[1].interfaces = "unexpected" + if (mode === "secret") rows[0]["auth-key"] = "changed" + if (mode === "added") rows.push({ ...baseline[1], ".id": "*3" }) + if (mode === "reordered") rows.reverse() +} } +const reset = (next = "normal") => { rows = structuredClone(baseline); reads = 0; writes = 0; mode = next } +assert.deepEqual(await writeOspfTemplateCost("*1", 20, dependencies), { id: "*1", from: 10, to: 20, changed: true }) +assert.equal(writes, 1); assert.equal(rows[0]["auth-key"], "NEVER_OSPF_AUTH") +reset(); assert.equal((await writeOspfTemplateCost("*1", 10, dependencies)).changed, false); assert.equal(writes, 0) +for (const next of ["ignored", "foreign", "secret", "added", "reordered", "lost-read", "malformed"]) { + reset(next) + await assert.rejects(writeOspfTemplateCost("*1", 20, dependencies), error => error instanceof OspfCostWriteError && error.writeAttempted && !error.message.includes("NEVER_OSPF")) + assert.equal(writes, 1, "Unconfirmed write must not be replayed") +} +reset(); rows.reverse() +assert.deepEqual(await writeOspfTemplateCost("*1", 20, dependencies), { id: "*1", from: 10, to: 20, changed: true }) +assert.deepEqual(rows.map(row => row[".id"]), ["*2", "*1"], "The native order is preserved even when it differs from ID sorting") +reset() +await assert.rejects(writeOspfTemplateCost("*1", 10, { + expectedCost: 10, + read: async () => ++reads === 1 ? structuredClone(rows) : structuredClone(rows).reverse(), + write: async () => { writes++ }, +}), error => error instanceof OspfCostWriteError && !error.writeAttempted) +assert.equal(writes, 0, "A no-op with changed listing order is refused without issuing a PATCH") +reset("lost-write"); await assert.rejects(writeOspfTemplateCost("*1", 20, dependencies), /Lost PATCH/); assert.equal(writes, 1); assert.equal(reads, 1) +reset("rejected-write"); await assert.rejects(writeOspfTemplateCost("*1", 20, dependencies), error => error instanceof MikrotikError && error.statusCode === 403); assert.equal(writes, 1) +for (const [id, cost] of [["../1", 20], ["*1", 0], ["*1", 65536], ["*1", NaN]] as const) { reset(); await assert.rejects(writeOspfTemplateCost(id, cost, dependencies)); assert.equal(reads, 0); assert.equal(writes, 0) } +reset(); await assert.rejects(writeOspfTemplateCost("*1", 20, { ...dependencies, expectedCost: 11 }), error => error instanceof OspfCostWriteError && !error.writeAttempted); assert.equal(writes, 0) +reset(); rows.push({ ...baseline[0] }); await assert.rejects(writeOspfTemplateCost("*1", 20, dependencies)); assert.equal(writes, 0) +reset("second-ignored") +await assert.rejects(async () => { for (const [id, cost] of [["*1", 20], ["*2", 40], ["*1", 50]] as const) await writeOspfTemplateCost(id, cost, { ...dependencies, expectedCost: undefined }) }) +assert.equal(writes, 2); assert.equal(rows[0].cost, "20"); assert.equal(rows[1].cost, "30") +const unidentifiedClient = new MikrotikClient({ host: "127.0.0.1", port: 1, username: "fixture", password: "fixture", useSsl: false, verifySsl: false }) +let forbiddenReads = 0 +unidentifiedClient.get = async () => { forbiddenReads++; throw new Error("Native read forbidden") } +await assert.rejects(unidentifiedClient.setOspfInterfaceTemplateCost("*1", 20, 10, "foreign-proof"), error => error instanceof OspfCostWriteError && !error.writeAttempted) +assert.equal(forbiddenReads, 0, "A source proof requires a bound server before any native read") +const privateState = await mkdtemp(join(tmpdir(), "mm-ospf-proof-")), previousState = process.env.MCP_STATE_DIR +try { + process.env.MCP_STATE_DIR = privateState + const source = { host: "192.0.2.1", port: 80, username: "fixture", password: "NEVER_OSPF_PROOF", useSsl: false, verifySsl: false } + const proof = await ospfCostConnectionProof(1, source) + assert.equal(proof, await ospfCostConnectionProof(1, { ...source })) + assert.match(proof, /^[a-f0-9]{64}$/); assert.ok(!proof.includes(source.password)) + assert.notEqual(proof, await ospfCostConnectionProof(2, source)) + for (const patch of [{ host: "192.0.2.2" }, { port: 443 }, { username: "other" }, { password: "changed" }, { useSsl: true }, { verifySsl: true }]) assert.notEqual(proof, await ospfCostConnectionProof(1, { ...source, ...patch })) +} finally { + if (previousState === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previousState + if (!resolve(privateState).startsWith(resolve(tmpdir()) + sep) || !basename(privateState).startsWith("mm-ospf-proof-")) throw new Error("Unsafe temporary proof path") + await rm(privateState, { recursive: true, force: true }) +} + +console.log('OSPF cost pure model: expected value, preserved fields, ignored/lost/rejected stop and no replay') diff --git a/backend/src/services/ospf-template-cost-write.ts b/backend/src/services/ospf-template-cost-write.ts new file mode 100644 index 00000000..e8697aef --- /dev/null +++ b/backend/src/services/ospf-template-cost-write.ts @@ -0,0 +1,36 @@ +import { isDeepStrictEqual } from "node:util" +import { observedInteger } from "./ospf-observed-values.js" +import { privateSecretFingerprint, fingerprint } from "../modules/mcp/security.js" + +interface Connection { host: string; port: number; useSsl: boolean; verifySsl: boolean; username: string; password: string; routingEndpointEpoch?: string } +export function ospfCostConnectionProof(serverId: number, source: Connection) { + return privateSecretFingerprint(fingerprint({ serverId, host: source.host, port: source.port, useSsl: source.useSsl, verifySsl: source.verifySsl, username: source.username, password: source.password, routingEndpointEpoch: source.routingEndpointEpoch ?? null }), "ospf-cost-connection") +} + +export class OspfCostWriteError extends Error { + constructor(message: string, readonly writeAttempted: boolean) { super(message) } +} +type Row = Record & { ".id": string } +function configuration(payload: unknown): Row[] { + if (!Array.isArray(payload) || payload.length > 10000 || payload.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !row[".id"].trim()) || new Set(payload.map(row => row[".id"])).size !== payload.length) throw new OspfCostWriteError("Не подтверждён каталог шаблонов OSPF", false) + return payload.map(row => ({ ...row, ...(typeof row.cost === "number" ? { cost: String(row.cost) } : {}) })) +} + +/** Compare the entire native template collection, including fields the UI does + * not edit. No write or read-back failure is retried. Call under the router lock. */ +export async function writeOspfTemplateCost(id: string, cost: number, dependencies: { + read: () => Promise; write: (id: string, cost: number) => Promise; expectedCost?: number +}) { + if (!/^\*[a-z0-9]+$/i.test(id) || !Number.isInteger(cost) || cost < 1 || cost > 65535 || dependencies.expectedCost !== undefined && (!Number.isInteger(dependencies.expectedCost) || dependencies.expectedCost < 1 || dependencies.expectedCost > 65535)) throw new OspfCostWriteError("Некорректный ID или cost шаблона OSPF", false) + const before = configuration(await dependencies.read()), target = before.find(row => row[".id"] === id) + const current = target && typeof target.cost === "string" ? observedInteger(target.cost, 1, 65535) : null + if (current === null || dependencies.expectedCost !== undefined && current !== dependencies.expectedCost) throw new OspfCostWriteError("Cost или шаблон OSPF изменился после подготовки", false) + const changed = current !== cost + if (changed) await dependencies.write(id, cost) + let after: Row[] + try { after = configuration(await dependencies.read()) } + catch { throw new OspfCostWriteError("Чтение OSPF после записи не подтверждено; проверьте состояние перед новым планом", changed) } + const expected = before.map(row => row[".id"] === id ? { ...row, cost: String(cost) } : row) + if (!isDeepStrictEqual(after, expected)) throw new OspfCostWriteError("После записи OSPF не подтверждён cost или изменились другие параметры; дальнейшие записи остановлены", changed) + return { id, from: current, to: cost, changed } +} diff --git a/backend/src/services/ospf-template-flags.test.ts b/backend/src/services/ospf-template-flags.test.ts new file mode 100644 index 00000000..08004e5b --- /dev/null +++ b/backend/src/services/ospf-template-flags.test.ts @@ -0,0 +1,41 @@ +import assert from "node:assert/strict" +import { readOspfTemplateFlags } from "./ospf-template-flags.js" + +const native = [{ ".id": "*8", passive: "", disabled: "false", cost: "30", opaque: "preserve" }, { ".id": "*9", disabled: "false" }] +const calls: string[] = [] +const rows = await readOspfTemplateFlags(native, async (id, key) => { calls.push(id + ":" + key); return { ret: id === "*8" ? "true" : "false" } }) +assert.equal(rows[0].passive, "true"); assert.equal(rows[1].passive, "false"); assert.equal(rows[0].opaque, "preserve") +assert.equal(native[0].passive, ""); assert.equal("passive" in native[1], false) +assert.deepEqual(calls.sort(), ["*8:passive", "*9:passive"]) +for (const ret of ["", undefined, null, "unknown", true]) await assert.rejects(readOspfTemplateFlags(native, async () => ({ ret }))) +let dispatched = 0 +await assert.rejects(readOspfTemplateFlags(Array.from({ length: 129 }, (_, index) => ({ ".id": "*" + index.toString(16), disabled: "false" })), async () => { dispatched++; return { ret: "false" } })) +assert.equal(dispatched, 0) +await assert.rejects(readOspfTemplateFlags([native[0]], async () => { dispatched++; return { ret: "true" } }, async () => { throw new Error("grant revoked") }), /grant revoked/) +assert.equal(dispatched, 0, "Revoked access must stop before the effective getter") +const unset = [{ ".id": "*9", disabled: "false" }] +const noValue = async () => [] +const negativeReceipt = async () => ({ present: [], absent: [{ ".id": "*9" }] }) +assert.equal((await readOspfTemplateFlags(unset, noValue, undefined, negativeReceipt))[0].passive, "false") +await assert.rejects(readOspfTemplateFlags(unset, noValue), "An empty getter alone is not false") +for (const receipt of [ + { present: [], absent: [] }, + { present: [{ ".id": "*9" }], absent: [{ ".id": "*9" }] }, + { present: [{ ".id": "*foreign" }], absent: [{ ".id": "*9" }] }, + { present: {}, absent: [{ ".id": "*9" }] }, +]) await assert.rejects(readOspfTemplateFlags(unset, noValue, undefined, async () => receipt)) +let fences = 0, membershipCalls = 0 +await assert.rejects(readOspfTemplateFlags(unset, noValue, async () => { if (++fences === 1) throw new Error("paused before native query") }, async () => { membershipCalls++; return negativeReceipt() }), /paused/) +assert.equal(membershipCalls, 0) +const many = Array.from({ length: 129 }, (_, index) => ({ ".id": "*" + index.toString(16), disabled: "false" })) +let unusedGetters = 0 +assert.equal((await readOspfTemplateFlags(many, async () => { unusedGetters++; return [] }, undefined, async () => ({ present: [], absent: many.map(row => ({ ".id": row[".id"] })) }))).length, 129) +assert.equal(unusedGetters, 0, "Bulk switch receipts avoid one getter per unset template") +const mixed = [{ ".id": "*8", disabled: "false", passive: "true" }, ...unset] +assert.equal((await readOspfTemplateFlags(mixed, noValue, undefined, async () => ({ present: [{ ".id": "*8" }], absent: [{ ".id": "*9" }] })))[1].passive, "false") +await assert.rejects(readOspfTemplateFlags(mixed, noValue, undefined, async () => ({ present: [], absent: [{ ".id": "*8" }, { ".id": "*9" }] }))) +await assert.rejects(readOspfTemplateFlags([{ ".id": "*8", passive: "garbage", disabled: "false" }], async () => ({ ret: "true" }))) +let active = 0, maximum = 0 +await readOspfTemplateFlags(Array.from({ length: 20 }, (_, index) => ({ ".id": "*" + index.toString(16) })), async () => { maximum = Math.max(maximum, ++active); await new Promise(resolve => setTimeout(resolve, 1)); active--; return { ret: "false" } }) +assert.equal(maximum, 4) +console.log("OSPF template flags: observed empty passive switch resolved through native receipt, missing false verified, unknown rejected and getter budget bounded") diff --git a/backend/src/services/ospf-template-flags.ts b/backend/src/services/ospf-template-flags.ts new file mode 100644 index 00000000..83ebe6c5 --- /dev/null +++ b/backend/src/services/ospf-template-flags.ts @@ -0,0 +1,51 @@ +import { observedBoolean } from "./ospf-observed-values.js" + +/** Resolve flags with native getters or a complete complementary switch-query partition; never invent defaults. */ +export async function readOspfTemplateFlags(payload: unknown, readProperty: (id: string, key: "disabled" | "passive") => Promise, beforeRead: () => Promise = async () => {}, readPassiveMembership?: () => Promise<{ present: unknown; absent: unknown }>) { + if (!Array.isArray(payload) || payload.length > 10000 || Buffer.byteLength(JSON.stringify(payload)) > 1048576 || payload.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !row[".id"])) throw new Error("Недостоверный каталог флагов OSPF") + if (new Set(payload.map(row => row[".id"])).size !== payload.length) throw new Error("Повторяющиеся ID шаблонов OSPF") + const rows: T[] = payload.map(row => ({ ...row })) + const missing: { row: T; key: "disabled" | "passive" }[] = [] + const unsetPassive: T[] = [] + for (const row of rows) for (const key of ["disabled", "passive"] as const) { + const raw = row[key], value = observedBoolean(raw) + if (value !== null) row[key] = String(value) + else { + if (raw !== undefined && raw !== "" || !/^\*[a-z0-9]+$/i.test(row[".id"])) throw new Error("Не подтверждён флаг шаблона OSPF") + if (key === "passive" && raw === undefined && readPassiveMembership) { unsetPassive.push(row); continue } + missing.push({ row, key }) + } + } + if (missing.length > 128) throw new Error("Превышен бюджет уточнения флагов OSPF") + let cursor = 0, failed = false + await Promise.all(Array.from({ length: Math.min(4, missing.length) }, async () => { + while (!failed && cursor < missing.length) { + const { row, key } = missing[cursor++] + try { + await beforeRead() + if (failed) return + const response = await readProperty(row[".id"], key) + const value = response && typeof response === "object" && "ret" in response && typeof response.ret === "string" ? observedBoolean(response.ret) : null + if (value === null) throw new Error("Не подтверждён effective флаг шаблона OSPF") + row[key] = String(value) + } catch (error) { failed = true; throw error } + } + })) + if (unsetPassive.length) { + if (!readPassiveMembership || missing.length + 2 > 128) throw new Error("Не подтверждён unset passive шаблона OSPF") + await beforeRead() + const membership = await readPassiveMembership() + const ids = (value: unknown) => { + if (!Array.isArray(value) || value.length > 10000 || Buffer.byteLength(JSON.stringify(value)) > 1048576 || value.some(row => !row || typeof row !== "object" || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]))) throw new Error("Недостоверный ответ native switch query OSPF") + const result = new Set(value.map(row => row[".id"])) + if (result.size !== value.length) throw new Error("Повторяющиеся ID native switch query OSPF") + return result + } + const present = ids(membership.present), absent = ids(membership.absent), all = new Set([...present, ...absent]) + if ([...present].some(id => absent.has(id)) || all.size !== rows.length || rows.some(row => !all.has(row[".id"]))) throw new Error("Native switch query OSPF неполон, противоречив или каталог изменился") + if (rows.some(row => row.passive === "true" && !present.has(row[".id"]))) throw new Error("Native switch query противоречит подтверждённому passive") + for (const row of unsetPassive) row.passive = present.has(row[".id"]) ? "true" : "false" + await beforeRead() + } + return rows +} diff --git a/backend/src/services/owned-routing-policy.test.ts b/backend/src/services/owned-routing-policy.test.ts new file mode 100644 index 00000000..5b453ab5 --- /dev/null +++ b/backend/src/services/owned-routing-policy.test.ts @@ -0,0 +1,40 @@ +import assert from "node:assert/strict" +import { mutateOwnedRoutingPolicy, readOwnedRoutingPolicy } from "./owned-routing-policy.js" + +const chain = "mmapp-bgp-out-fixture", comment = "MikrotikManager: MCP BGP output fixture" +function fixture(fault?: "content" | "order") { + let rows: Record[] = [ + { ".id": "*a", ".nextid": "*b", chain: "input", comment: "first", rule: "accept;", disabled: "false" }, + { ".id": "*b", chain: "input", comment: "second", rule: "reject;", disabled: "false" }, + ] + let writes = 0 + const client = { + get: async () => structuredClone(rows), + post: async () => { + writes++; rows.at(-1)![".nextid"] = "*owned" + rows.push({ ".id": "*owned", chain, comment, rule: "reject;", disabled: "false" }) + if (fault === "content") rows[0].comment = "changed" + if (fault === "order") [rows[0], rows[1]] = [rows[1], rows[0]] + return { ret: "*owned" } + }, + patch: async () => { throw Error("No patch expected") }, + delete: async () => { writes++; rows = rows.filter(row => row[".id"] !== "*owned"); delete rows.at(-1)![".nextid"] }, + } as unknown as Parameters[0] + return { client, writes: () => writes } +} +const good = fixture() +let fences = 0 +const fence = async () => { fences++ } +assert.equal((await mutateOwnedRoutingPolicy(good.client, chain, comment, "reject;", fence)).action, "create") +const observed = await readOwnedRoutingPolicy(good.client, chain, comment) +assert.equal(observed.proofRows[1][".nextid"], "*owned", "Source proofs retain the raw adjacency link") +assert.equal((await mutateOwnedRoutingPolicy(good.client, chain, comment, "reject;", fence)).action, "noop") +assert.equal(good.writes(), 1) +assert.equal((await mutateOwnedRoutingPolicy(good.client, chain, comment, null, fence)).action, "delete") +assert.equal(good.writes(), 2); assert.equal(fences, 3) +for (const fault of ["content", "order"] as const) { + const bad = fixture(fault) + await assert.rejects(mutateOwnedRoutingPolicy(bad.client, chain, comment, "reject;", fence), (error: { code?: string }) => error.code === "VERIFICATION_FAILED") + assert.equal(bad.writes(), 1, "An uncertain or incorrect result must not be retried") +} +console.log("Owned routing policy: native adjacency updates allowed after create/delete, raw source links retained, actual foreign row order/content strictly preserved, no-op fenced and no replay") diff --git a/backend/src/services/owned-routing-policy.ts b/backend/src/services/owned-routing-policy.ts new file mode 100644 index 00000000..2c7e0fb4 --- /dev/null +++ b/backend/src/services/owned-routing-policy.ts @@ -0,0 +1,67 @@ +import type { MikrotikClient } from "./mikrotik.js" +import { nativeBgpRows, type NativeBgpRow } from "./bgp-export-model.js" +import { stableBgpPolicyRow } from "./bgp-filter-binding-model.js" +import { readOspfEffectiveDisabled } from "./ospf-effective-disabled.js" +import { writeRoutingFilter } from "./filter-rules-mutation.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { routingFilterBodiesMatch } from "./routing-filter-body.js" + +type Client = Pick +export async function readOwnedRoutingPolicy(client: Client, chain: string, comment: string) { + const rows = nativeBgpRows(await client.get("/routing/filter/rule")) + if (rows.length > 1000) throw new McpError("LIMIT_EXCEEDED", "Каталог routing filters превышает 1000 строк", 409) + const candidates = rows.filter(row => row.chain === chain || row.comment === comment) + if (candidates.length > 1 || candidates.some(row => row.chain !== chain || row.comment !== comment)) throw new McpError("STATE_CONFLICT", "Цепочка/маркер policy заняты чужими или неоднозначными правилами", 409) + let current: NativeBgpRow | null = candidates[0] ?? null + if (current) { + if (typeof current.rule !== "string" || Buffer.byteLength(current.rule, "utf8") > 65536) throw new McpError("STATE_UNAVAILABLE", "Не подтверждено тело owned policy", 409) + const [hydrated] = await readOspfEffectiveDisabled([current], id => client.post("/routing/filter/rule/get", { ".id": id, "value-name": "disabled" })) + if (!["false", "no", false, "true", "yes", true].includes(hydrated.disabled as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён disabled policy", 409) + current = { ...hydrated, disabled: [true, "true", "yes"].includes(hydrated.disabled as string | boolean) ? "true" : "false" } + rows[rows.findIndex(row => row[".id"] === current![".id"])] = current + } + return { rows, current, proofRows: rows.map(stableBgpPolicyRow) } +} +export async function mutateOwnedRoutingPolicy(client: Client, chain: string, comment: string, body: string | null, beforeWrite: (state: Awaited>) => Promise) { + const before = await readOwnedRoutingPolicy(client, chain, comment) + const dispatch = body === null ? !!before.current : !before.current || !routingFilterBodiesMatch(before.current.rule, body) || before.current.disabled !== "false" + await beforeWrite(before) + let id = before.current?.[".id"] + const fields = { chain, comment, rule: body ?? "", disabled: "no" } + if (dispatch && body === null) await writeRoutingFilter(() => client.delete("/routing/filter/rule/" + encodeURIComponent(id!))) + else if (dispatch && id) await writeRoutingFilter(() => client.patch("/routing/filter/rule/" + encodeURIComponent(id!), fields), body!) + else if (dispatch) { + const response = await writeRoutingFilter(() => client.post("/routing/filter/rule/add", fields), body!) + if (!response || typeof response !== "object" || !("ret" in response) || typeof response.ret !== "string" || !/^\*[A-Za-z0-9]+$/.test(response.ret)) throw new McpError("NATIVE_ID_UNCONFIRMED", "Policy отправлена без подтверждённого native ID; не повторяйте запись", 503) + id = response.ret + } + const after = await readOwnedRoutingPolicy(client, chain, comment) + // RouterOS updates the predecessor's .nextid when adding/removing a rule. + // Compare the actual row sequence (including IDs) and every configuration field; + // this derived adjacency pointer is not an independent configuration change. + // Keep raw .nextid in source/receipt proofs: only this post-write comparison omits it. + const untouched = (state: typeof before) => state.rows.filter(row => row[".id"] !== state.current?.[".id"]).map(row => { + const stable = stableBgpPolicyRow(row) + delete stable[".nextid"] + return stable + }) + if (fingerprint(untouched(before)) !== fingerprint(untouched(after))) { + const previous = untouched(before), current = untouched(after) + console.warn("bgp-community-policy.other-filters.mismatch", { beforeCount: previous.length, afterCount: current.length, + changedFields: previous.map((row, index) => [...new Set([...Object.keys(row), ...Object.keys(current[index] ?? {})])].filter(key => fingerprint(row[key] ?? null) !== fingerprint(current[index]?.[key] ?? null))) }) + throw new McpError("VERIFICATION_FAILED", "Изменились другие routing filters или их порядок", 409) + } + if (body === null) { if (after.current) throw new McpError("VERIFICATION_FAILED", "Удаление policy не подтверждено", 409) } + else { + if (!after.current || after.current[".id"] !== id || !routingFilterBodiesMatch(after.current.rule, body) || after.current.disabled !== "false" || [true, "true", "yes"].includes(after.current.invalid as string | boolean)) { + console.warn("bgp-community-policy.native-readback.mismatch", { exists: !!after.current, idMatches: after.current?.[".id"] === id, bodyMatches: routingFilterBodiesMatch(after.current?.rule, body), enabled: after.current?.disabled === "false", invalid: [true, "true", "yes"].includes(after.current?.invalid as string | boolean), expectedBytes: Buffer.byteLength(body, "utf8"), actualBytes: typeof after.current?.rule === "string" ? Buffer.byteLength(after.current.rule, "utf8") : null }) + throw new McpError("VERIFICATION_FAILED", "Policy read-back не совпал", 409) + } + const position = after.rows.findIndex(row => row[".id"] === id) + if (before.current ? position !== before.rows.findIndex(row => row[".id"] === id) : position !== after.rows.length - 1) { + console.warn("bgp-community-policy.position.mismatch", { creating: !before.current, position, beforePosition: before.rows.findIndex(row => row[".id"] === id), afterCount: after.rows.length }) + throw new McpError("VERIFICATION_FAILED", "Позиция policy изменилась", 409) + } + } + return { after, nativeDispatch: dispatch, action: !dispatch ? "noop" : body === null ? "delete" : before.current ? "patch" : "create", configurationVerified: true } +} diff --git a/backend/src/services/partition-write-transaction.sql-helper.ts b/backend/src/services/partition-write-transaction.sql-helper.ts new file mode 100644 index 00000000..85dad733 --- /dev/null +++ b/backend/src/services/partition-write-transaction.sql-helper.ts @@ -0,0 +1,181 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import type { PoolClient, QueryConfig } from "pg" +import type { PartitionWriteOwner } from "./partition-write-transaction.js" + +/** Explicit owned PostgreSQL acceptance. Import is inert; no production + * migration, cleanup, journal deletion, native I/O or global pool replacement. */ +export async function acceptPartitionWriteTransactionSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { trafficSamples } = await import("../db/schema.js") + const { withPartitionWrite, PartitionWriteError, isPartitionWriteOutcomeUnknown, PARTITION_WRITER_ADVISORY_KEY: partitionKey } = await import("./partition-write-transaction.js") + const { commitFlowWrite, flowWriteQuery, FLOW_WRITE_LOCK_KEY: flowKey } = await import("./traffic-flow-write-transaction.js") + const { sql: drizzleSql } = await import("drizzle-orm") + const { ensurePartitionFor } = await import("../db/partitions.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + const serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('owned-partition-writer','127.0.0.1',9,false) RETURNING id")).rows[0].id) + const sampledAt = new Date("2101-01-04T12:00:00.000Z") + const query = (client: Pick, text: string, values: unknown[] = []) => client.query({ text, values, query_timeout: 8000 } as QueryConfig & { query_timeout: number }) + const pidOf = async (client: Pick) => Number((await query(client, "SELECT pg_backend_pid() AS pid")).rows[0].pid) + const waitFor = async (label: string, inspect: () => Promise) => { + const deadline = Date.now() + 10000 + while (Date.now() < deadline) { + const value = await inspect(); if (value !== undefined) return value + await new Promise(resolve => setTimeout(resolve, 10)) + } + assert.fail("Timed out observing " + label) + } + const waitLock = (pid: number, key: number, mode: "ShareLock" | "ExclusiveLock") => waitFor("actual waiting advisory lock", async () => { + const rows = (await query(pool, "SELECT mode FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0 AND objid=$2 AND objsubid=1 AND mode=$3 AND NOT granted", [pid, key, mode])).rows + return rows.length === 1 ? true : undefined + }) + const noRelationLock = async (pid: number) => assert.equal((await query(pool, "SELECT count(*)::int AS n FROM pg_locks l JOIN pg_class c ON c.oid=l.relation WHERE l.pid=$1 AND l.locktype='relation' AND (c.relname='traffic_samples' OR c.relname LIKE 'traffic_samples_w_%')", [pid])).rows[0].n, 0) + const noFlowLock = async (pid: number) => assert.equal((await query(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0 AND objid=$2", [pid, flowKey])).rows[0].n, 0) + const rowCount = async (name: string) => Number((await query(pool, "SELECT count(*)::int AS n FROM traffic_samples WHERE server_id=$1 AND interface_name=$2", [serverId, name])).rows[0].n) + const write = (owner: PartitionWriteOwner, name: string) => owner.db.insert(trafficSamples).values({ serverId, interfaceName: name, sampledAt: sampledAt.toISOString(), rxBytes: 1 }) + let cases = 0, waitingProofs = 0 + const maintenance = await pool.connect() + const maintenancePid = await pidOf(maintenance) + await query(maintenance, "SET statement_timeout='12000ms'") + const holder = () => { + let pid = 0, lose: "BEGIN" | "COMMIT" | undefined + const released: boolean[] = [] + const trace: string[] = [] + const connect = async () => { + const client = await pool.connect(); pid = await pidOf(client) + client.on("error", () => {}) + return new Proxy(client, { get(target, property) { + if (property === "release") return (destroy?: boolean) => { released.push(!!destroy); target.release(destroy) } + if (property === "query") return async (...args: unknown[]) => { + const raw = args[0], text = typeof raw === "string" ? raw : String((raw as { text: string }).text) + trace.push(text) + const result = await Reflect.apply(target.query, target, args) + if (text === lose) { lose = undefined; throw Error("Synthetic lost ACK after actual SQL") } + return result + } + const value = Reflect.get(target, property, target) + return typeof value === "function" ? value.bind(target) : value + } }) + } + return { connect, trace, released, pid: () => pid, lose: (boundary: "BEGIN" | "COMMIT") => { lose = boundary } } + } + try { + await query(maintenance, "SELECT pg_advisory_lock($1::bigint)", [partitionKey]) + const f = holder(), name = randomUUID(); let entered = false, captured!: PartitionWriteOwner + const pending = withPartitionWrite(async owner => { + entered = true; captured = owner + assert.equal(Number((await owner.client.query("SELECT pg_backend_pid() AS pid")).rows[0].pid), f.pid()) + const ormPid = await owner.db.execute<{ pid: number }>(drizzleSql`SELECT pg_backend_pid() AS pid`) + assert.equal(Number(ormPid.rows[0].pid), f.pid()) + await write(owner, name) + }, { connect: f.connect, deadlineAt: Date.now() + 20000 }) + void pending.catch(() => {}) + try { + const pid = await waitFor("ORM writer backend", async () => f.pid() || undefined) + await waitLock(pid, partitionKey, "ShareLock"); waitingProofs++ + assert.equal(entered, false); await noRelationLock(pid) + // Harmless owned future partition DDL really succeeds while the writer + // waits before its first target relation statement. No DROP/TRUNCATE. + // This explicit maintenance backend already owns the session exclusive + // admission. Do not present a bare PoolClient as an ordinary writer. + const from = new Date(sampledAt); from.setUTCHours(0, 0, 0, 0); from.setUTCDate(from.getUTCDate() - (from.getUTCDay() || 7) + 1) + const to = new Date(from); to.setUTCDate(to.getUTCDate() + 7) + const leaf = "traffic_samples_w_" + from.toISOString().slice(0, 10).replaceAll("-", "") + await query(maintenance, `CREATE TABLE IF NOT EXISTS public."${leaf}" PARTITION OF public.traffic_samples FOR VALUES FROM ('${from.toISOString()}') TO ('${to.toISOString()}')`) + assert(/^[a-z0-9_]+$/.test(leaf)) + await query(maintenance, `ALTER TABLE public."${leaf}" SET (fillfactor=90)`) + assert.equal((await query(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND objid=$2 AND mode='ExclusiveLock' AND granted", [maintenancePid, partitionKey])).rows[0].n, 1) + } finally { await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [partitionKey]); await pending } + assert.equal(await rowCount(name), 1); assert.deepEqual(f.released, [false]); cases++ + const beforeClosed = f.trace.length + await assert.rejects(captured.client.query("SELECT 1"), PartitionWriteError) + await assert.rejects(captured.db.execute(drizzleSql`SELECT 1`)) + assert.equal(f.trace.length, beforeClosed); cases++ + + // Exercise the actual DDL helper through its live owner capability, then + // its Pool entrypoint. An arbitrary checked-out client cannot claim this + // capability. Both positive calls only touch this owned future leaf. + const ddl = holder() + await withPartitionWrite(async owner => { + await ensurePartitionFor(owner.client, "traffic_samples", "week", sampledAt) + }, { connect: ddl.connect }) + assert.equal(ddl.trace.filter(text => text === "BEGIN").length, 1) + assert.equal(ddl.trace.filter(text => text === "COMMIT").length, 1) + assert.equal(ddl.trace.filter(text => text.includes("pg_advisory_xact_lock_shared")).length, 1) + assert(ddl.trace.some(text => text.startsWith("CREATE TABLE"))) + await ensurePartitionFor(pool, "traffic_samples", "week", sampledAt) + await assert.rejects(ensurePartitionFor(maintenance, "traffic_samples", "week", sampledAt), /authenticated/) + cases++ + + for (const exclusive of [false, true]) { + // Maintenance first: flow admission waits on partition key BEFORE its + // shared/exclusive flow key. Maintenance can still acquire the flow key. + await query(maintenance, "SELECT pg_advisory_lock($1::bigint)", [partitionKey]) + const flow = holder(), batch = randomUUID(); let entered = false + const pending = commitFlowWrite({ id: batch, kind: "ingest", sourceProof: "a".repeat(64), exclusive }, async () => { entered = true; await flowWriteQuery("SELECT 1"); return { synthetic: true } }, async () => {}, flow.connect) + void pending.catch(() => {}) + try { + const pid = await waitFor("flow backend", async () => flow.pid() || undefined) + await waitLock(pid, partitionKey, "ShareLock"); waitingProofs++; await noFlowLock(pid); await noRelationLock(pid); assert.equal(entered, false) + assert.equal((await query(maintenance, "SELECT pg_try_advisory_lock($1::bigint) AS acquired", [flowKey])).rows[0].acquired, true) + await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [flowKey]) + } finally { await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [partitionKey]); await pending } + assert.equal(entered, true) + assert(flow.trace.findIndex(text => text.includes("pg_advisory_xact_lock_shared($1::bigint)")) < flow.trace.findIndex(text => text.includes(exclusive ? "pg_try_advisory_xact_lock($1)" : "pg_advisory_xact_lock_shared($1)"))) + assert.equal((await query(pool, "SELECT count(*)::int AS n FROM flow_write_receipts WHERE batch_id=$1", [batch])).rows[0].n, 1); cases++ + + // Flow first: a real flow transaction owns both keys; maintenance waits + // on partition exclusion until that exact transaction commits. + const active = holder(); let release!: () => void, enteredActive!: () => void + const gate = new Promise(resolve => { release = resolve }), began = new Promise(resolve => { enteredActive = resolve }) + const running = commitFlowWrite({ id: randomUUID(), kind: "ingest", sourceProof: "b".repeat(64), exclusive }, async () => { enteredActive(); await Promise.race([gate, new Promise((_resolve, reject) => { const timer = setTimeout(() => reject(Error("Owned flow gate timeout")), 10000); timer.unref() })]); return { synthetic: true } }, async () => {}, active.connect) + void running.catch(() => {}) + await Promise.race([began, new Promise((_resolve, reject) => { const timer = setTimeout(() => reject(Error("Flow callback barrier timeout")), 10000); timer.unref() })]) + const acquire = query(maintenance, "SELECT pg_advisory_lock($1::bigint)", [partitionKey]); void acquire.catch(() => {}) + try { await waitLock(maintenancePid, partitionKey, "ExclusiveLock"); waitingProofs++; release(); await running; await acquire } + finally { release(); await running.catch(() => {}); await acquire.catch(() => {}); await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [partitionKey]) } + cases++ + } + + // FK cascade acceptance touches exclusively temporary synthetic parents + // and children created inside this already-admitted writer transaction. + await withPartitionWrite(async owner => { + await owner.client.query("CREATE TEMP TABLE partition_writer_fk_parent(id int PRIMARY KEY) ON COMMIT DROP") + await owner.client.query("CREATE TEMP TABLE partition_writer_fk_child(id int PRIMARY KEY,parent_id int REFERENCES partition_writer_fk_parent(id) ON DELETE CASCADE) ON COMMIT DROP") + await owner.client.query("INSERT INTO partition_writer_fk_parent VALUES(1)") + await owner.client.query("INSERT INTO partition_writer_fk_child VALUES(1,1)") + await owner.client.query("DELETE FROM partition_writer_fk_parent WHERE id=1") + assert.equal((await owner.client.query("SELECT count(*)::int AS n FROM partition_writer_fk_child")).rows[0].n, 0) + }); cases++ + + for (const boundary of ["BEGIN", "COMMIT"] as const) { + const f = holder(), name = randomUUID(); f.lose(boundary) + await assert.rejects(withPartitionWrite(async owner => { await write(owner, name) }, { connect: f.connect }), isPartitionWriteOutcomeUnknown) + assert.equal(f.trace.filter(text => text === boundary).length, 1); assert(!f.trace.includes("ROLLBACK")); assert.deepEqual(f.released, [true]) + assert.equal(await rowCount(name), boundary === "COMMIT" ? 1 : 0); cases++ + } + for (const stop of ["abort", "deadline"] as const) { + const f = holder(), name = randomUUID(), controller = new AbortController(); let entered!: () => void + const began = new Promise(resolve => { entered = resolve }) + const pending = withPartitionWrite(async owner => { await write(owner, name); entered(); await new Promise(() => {}) }, { connect: f.connect, signal: controller.signal, deadlineAt: Date.now() + 1500 }) + void pending.catch(() => {}) + await Promise.race([began, pending.then(() => assert.fail("Unexpected writer completion"))]) + if (stop === "abort") controller.abort() + await assert.rejects(pending, (error: unknown) => error instanceof PartitionWriteError && !error.outcomeUnknown) + assert.deepEqual(f.released, [true]); assert(!f.trace.includes("COMMIT")) + await waitFor("destroyed transaction backend", async () => (await query(pool, "SELECT count(*)::int AS n FROM pg_stat_activity WHERE pid=$1", [f.pid()])).rows[0].n === 0 ? true : undefined) + assert.equal(await rowCount(name), 0); cases++ + } + return { cases, observedAdvisoryWaits: waitingProofs, actualPostgresAndDrizzle: true, preRelationAdmission: true, flowBothOrdersAndExclusive: true, syntheticTemporaryFkCascade: true, nativeIo: false, productionMigrationApplied: false } + } finally { + // Unlock this owned maintenance backend only; no data cleanup is needed. + await query(maintenance, "SELECT pg_advisory_unlock_all()").catch(() => {}); maintenance.release(true) + await query(pool, "UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + } +} diff --git a/backend/src/services/partition-write-transaction.test.ts b/backend/src/services/partition-write-transaction.test.ts new file mode 100644 index 00000000..60e8974e --- /dev/null +++ b/backend/src/services/partition-write-transaction.test.ts @@ -0,0 +1,152 @@ +import assert from "node:assert/strict" +import type { PoolClient } from "pg" +import { sql } from "drizzle-orm" +import { admitPartitionWriter, withBackgroundPartitionWrite, withPartitionWrite, PartitionWriteError, PartitionWriterBusy, isPartitionWriteOutcomeUnknown, isPartitionWriteClient, partitionWriteFailureDiagnostic, PARTITION_WRITER_ADVISORY_KEY, type PartitionWriteOwner } from "./partition-write-transaction.js" + +const tryAdmission = "SELECT pg_catalog.pg_try_advisory_xact_lock_shared($1::bigint) AS acquired" +function fixture(acquired?: boolean) { + const statements: Array<{ text: string; values: unknown[] }> = [], releases: boolean[] = [] + let fail: string | undefined, committed = 0, connects = 0 + let commitCommand: string | undefined = "COMMIT" + const client = { + query: async (raw: string | { text: string; values?: unknown[] }, values: unknown[] = []) => { + const text = typeof raw === "string" ? raw : raw.text + statements.push({ text, values: typeof raw === "string" ? values : raw.values ?? values }) + if (text === "COMMIT") committed++ + if (text === fail) throw Object.assign(Error("Injected uncertain SQL ACK"), { code: "55P03" }) + return { rows: [text === tryAdmission ? { acquired } : { value: 1 }], rowCount: 1, command: text === "COMMIT" ? commitCommand : text === "ROLLBACK" ? "ROLLBACK" : "SELECT" } + }, + release: (destroy: boolean) => { releases.push(destroy) }, + } as unknown as PoolClient + return { client, statements, releases, connect: async () => { connects++; return client }, fail: (text: string) => { fail = text }, committed: () => committed, connects: () => connects, command: (value: string | undefined) => { commitCommand = value } } +} +let cases = 0 +async function test(name: string, work: () => Promise) { await work(); cases++; console.log("ok", name) } +await test("raw and Drizzle use same backend after bounded shared admission", async () => { + const f = fixture() + const result = await withPartitionWrite(async owner => { await owner.client.query("INSERT INTO traffic_samples VALUES ($1)", [7]); await owner.db.execute(sql`SELECT ${9} AS value`); return "done" }, { connect: f.connect }) + assert.equal(result, "done"); assert.equal(f.connects(), 1); assert.deepEqual(f.releases, [false]) + assert.equal(f.statements[0].text, "BEGIN") + assert.match(f.statements[1].text, /^SET LOCAL lock_timeout='\d+ms'$/) + assert.match(f.statements[2].text, /^SET LOCAL statement_timeout='\d+ms'$/) + assert.equal(f.statements[3].text, "SELECT pg_advisory_xact_lock_shared($1::bigint)"); assert.deepEqual(f.statements[3].values, [PARTITION_WRITER_ADVISORY_KEY]) + assert.equal(f.statements[4].text, "INSERT INTO traffic_samples VALUES ($1)"); assert.equal(f.statements.at(-1)?.text, "COMMIT") +}) +await test("caller-owned transaction admission never starts or commits lifecycle", async () => { + const f = fixture(); await admitPartitionWriter(f.client) + assert.equal(f.statements.length, 3); assert(f.statements[2].text.includes("pg_advisory_xact_lock_shared")); assert.equal(f.releases.length, 0) +}) +await test("a definite busy writer rolls back without running scheduled work", async () => { + const f = fixture(false) + await assert.rejects(withBackgroundPartitionWrite(async () => assert.fail("scheduled SQL ran"), { connect: f.connect }), PartitionWriterBusy) + assert.deepEqual(f.statements.slice(3).map(statement => statement.text), [tryAdmission, "ROLLBACK"]) + assert.deepEqual(f.releases, [false]) +}) +await test("background admission validates the lock reply and surfaces SQL failures", async () => { + for (const failure of [undefined, tryAdmission, "ROLLBACK"]) { + const f = fixture(failure === undefined ? undefined : false) + if (failure) f.fail(failure) + await assert.rejects(withBackgroundPartitionWrite(async () => assert.fail("scheduled SQL ran"), { connect: f.connect }), PartitionWriteError) + assert.deepEqual(f.releases, [true]) + } + const f = fixture(true) + await withBackgroundPartitionWrite(async owner => { await owner.client.query("SELECT 1") }, { connect: f.connect }) + assert.deepEqual(f.statements.slice(3).map(statement => statement.text), [tryAdmission, "SELECT 1", "COMMIT"]) +}) +for (const boundary of ["BEGIN", "COMMIT"] as const) await test("lost " + boundary + " ACK destroys without rollback or retry", async () => { + const f = fixture(); f.fail(boundary) + await assert.rejects(withPartitionWrite(async owner => { await owner.client.query("INSERT INTO traffic_samples VALUES (1)") }, { connect: f.connect }), error => isPartitionWriteOutcomeUnknown(error) && error.automaticReplay === false && error.code === "PARTITION_WRITE_UNKNOWN") + assert.deepEqual(f.releases, [true]); assert.equal(f.statements.filter(row => row.text === boundary).length, 1); assert(!f.statements.some(row => row.text === "ROLLBACK")) + assert.equal(f.committed(), boundary === "COMMIT" ? 1 : 0) +}) +await test("detached raw and Drizzle handles expire before backend returns to pool", async () => { + const f = fixture(); let captured!: PartitionWriteOwner + await withPartitionWrite(async owner => { captured = owner }, { connect: f.connect }) + const before = f.statements.length + await assert.rejects(captured.client.query("SELECT 1"), PartitionWriteError) + await assert.rejects(captured.db.execute(sql`SELECT 1`)) + assert.equal(f.statements.length, before); assert.deepEqual(f.releases, [false]) +}) +for (const text of ["COMMIT", "/* hidden */ COMMIT", "-- leading\nROLLBACK", "SELECT 1; COMMIT", "SAVEPOINT bypass"]) await test("scope refuses transaction escape " + text, async () => { + const f = fixture() + await assert.rejects(withPartitionWrite(async owner => { await owner.client.query(text) }, { connect: f.connect }), PartitionWriteError) + assert(!f.statements.some(row => row.text === text)); assert.deepEqual(f.releases, [true]) +}) +await test("caught query failure still prevents commit", async () => { + const f = fixture(); f.fail("SELECT fail") + await assert.rejects(withPartitionWrite(async owner => { await owner.client.query("SELECT fail").catch(() => {}) }, { connect: f.connect }), error => error instanceof PartitionWriteError && !error.outcomeUnknown) + assert(!f.statements.some(row => row.text === "COMMIT")); assert.deepEqual(f.releases, [true]) +}) +for (const [statement, stage] of [ + ["SELECT pg_advisory_xact_lock_shared($1::bigint)", "admission"], + ["SELECT 1", "work"], + ["COMMIT", "commit"], +] as const) await test("private SQLSTATE identifies transaction stage " + stage, async () => { + const f = fixture(); f.fail(statement) + await assert.rejects(withPartitionWrite(async owner => { + await owner.client.query("SELECT 1") + }, { connect: f.connect }), (error: unknown) => { + assert(error instanceof PartitionWriteError) + assert.deepEqual(partitionWriteFailureDiagnostic(error), { stage, sqlState: "55P03" }) + assert.equal(error.outcomeUnknown, stage === "commit") + assert.equal(error.automaticReplay, false) + assert.doesNotMatch(JSON.stringify(error), /55P03|Injected|SELECT/) + return true + }) +}) +await test("callback raw query remains guarded and completes before commit", async () => { + const f = fixture() + await withPartitionWrite(async owner => { await new Promise((resolve, reject) => { owner.client.query("SELECT 1", error => error ? reject(error) : resolve()) }) }, { connect: f.connect }) + assert.equal(f.statements.at(-2)?.text, "SELECT 1"); assert.equal(f.statements.at(-1)?.text, "COMMIT") +}) +await test("abort destroys owner and settles hanging work without waiting for callback", async () => { + const f = fixture(), controller = new AbortController(); let entered!: () => void + const started = new Promise(resolve => { entered = resolve }) + const pending = withPartitionWrite(async () => { entered(); await new Promise(() => {}) }, { connect: f.connect, signal: controller.signal }) + await started; controller.abort() + await assert.rejects(pending, PartitionWriteError); assert.deepEqual(f.releases, [true]); assert(!f.statements.some(row => row.text === "COMMIT")) +}) +await test("deadline destroys owner and settles hanging work", async () => { + const f = fixture() + await assert.rejects(withPartitionWrite(async () => new Promise(() => {}), { connect: f.connect, deadlineAt: Date.now() + 30 }), PartitionWriteError) + assert.deepEqual(f.releases, [true]); assert(!f.statements.some(row => row.text === "COMMIT")) +}) +await test("abort settles an authority callback that never returns", async () => { + const f = fixture(), controller = new AbortController(); let entered!: () => void + const started = new Promise(resolve => { entered = resolve }) + const pending = withPartitionWrite(async () => {}, { connect: f.connect, signal: controller.signal, authorize: async () => { entered(); await new Promise(() => {}) } }) + await started; controller.abort(); await assert.rejects(pending, PartitionWriteError) + assert.deepEqual(f.releases, [true]); assert.equal(f.statements.length, 0) +}) +await test("expired, overly long and pre-aborted admission never connects", async () => { + const f = fixture(), controller = new AbortController(); controller.abort() + for (const options of [{ deadlineAt: Date.now() - 1 }, { deadlineAt: Date.now() + 130000 }, { signal: controller.signal }]) await assert.rejects(withPartitionWrite(async () => {}, { ...options, connect: f.connect }), PartitionWriteError) + assert.equal(f.connects(), 0) +}) +await test("revoked authority before commit cannot publish a success", async () => { + const f = fixture(); let active = true + await assert.rejects(withPartitionWrite(async owner => { await owner.client.query("SELECT 1"); active = false }, { connect: f.connect, authorize: async () => { assert(active) } }), PartitionWriteError) + assert(!f.statements.some(row => row.text === "COMMIT")); assert.deepEqual(f.releases, [true]) +}) +await test("late connected backend after abort is destroyed rather than leaked", async () => { + const f = fixture(), controller = new AbortController(); let connected!: (client: PoolClient) => void + const pending = withPartitionWrite(async () => {}, { signal: controller.signal, connect: () => new Promise(resolve => { connected = resolve }) }) + controller.abort(); await assert.rejects(pending, PartitionWriteError) + connected(f.client); await new Promise(resolve => setImmediate(resolve)) + assert.deepEqual(f.releases, [true]); assert.equal(f.statements.length, 0) +}) +await test("owned client identity cannot be forged and expires before pooling", async () => { + const f=fixture();let client:PoolClient|undefined + assert.equal(isPartitionWriteClient(f.client),false) + assert.equal(isPartitionWriteClient({query:f.client.query}),false) + await withPartitionWrite(async owner=>{client=owner.client;assert.equal(isPartitionWriteClient(client),true)}, {connect:f.connect}) + assert.equal(isPartitionWriteClient(client),false) +}) +for (const command of ["ROLLBACK", "INSERT", undefined]) await test("resolved non-COMMIT acknowledgement refuses " + String(command), async () => { + const f = fixture(); f.command(command) + await assert.rejects(withPartitionWrite(async owner => { await owner.client.query("INSERT INTO traffic_samples VALUES (1)") }, { connect: f.connect }), error => isPartitionWriteOutcomeUnknown(error) && error.automaticReplay === false) + assert.equal(f.statements.filter(row => row.text === "COMMIT").length, 1) + assert.equal(f.statements.filter(row => row.text.startsWith("INSERT")).length, 1) + assert(!f.statements.some(row => row.text === "ROLLBACK")); assert.deepEqual(f.releases, [true]) +}) +console.log(`Partition writer transaction: ${cases} tests passed`) diff --git a/backend/src/services/partition-write-transaction.ts b/backend/src/services/partition-write-transaction.ts new file mode 100644 index 00000000..2e2808fc --- /dev/null +++ b/backend/src/services/partition-write-transaction.ts @@ -0,0 +1,224 @@ +import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres" +import type { PoolClient } from "pg" +import * as schema from "../db/schema.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" + +// Independent of the optional trigger proposal/migration and maintenance +// runtime: collectors can acquire this key before their first relation access. +export const PARTITION_WRITER_ADVISORY_KEY = 8723110 +const admittedClients = new WeakMap boolean>() +/** Identity proof for the guarded client owned by this module. A caller's + * query property or serialized marker can never manufacture this capability. */ +export function isPartitionWriteClient(value: unknown): value is PoolClient { + return !!value && typeof value === "object" && admittedClients.get(value)?.() === true +} +export interface PartitionWriteOptions { + authorize?: () => Promise + signal?: AbortSignal + deadlineAt?: number + connect?: () => Promise +} +export interface PartitionWriteOwner { client: PoolClient; db: NodePgDatabase } +export class PartitionWriterBusy extends Error { + constructor() { super("Partition writer lock is busy") } +} +export class PartitionWriteError extends Error { + readonly automaticReplay = false + readonly code: "PARTITION_WRITE_UNAVAILABLE" | "PARTITION_WRITE_UNKNOWN" + constructor(readonly outcomeUnknown = false) { + super(outcomeUnknown ? "Partition write outcome unknown; automatic replay forbidden" : "Partition write transaction unavailable or closed") + this.code = outcomeUnknown ? "PARTITION_WRITE_UNKNOWN" : "PARTITION_WRITE_UNAVAILABLE" + } +} +export const isPartitionWriteOutcomeUnknown = (error: unknown): error is PartitionWriteError => error instanceof PartitionWriteError && error.outcomeUnknown +type PartitionWriteStage = "checkout" | "begin" | "admission" | "work" | "commit" +const failureDiagnostics = new WeakMap() + +export function partitionWriteFailureDiagnostic(error: unknown) { + return error instanceof PartitionWriteError ? failureDiagnostics.get(error) : undefined +} + +function safeSqlState(error: unknown): string | undefined { + if (!(error instanceof Error)) return undefined + const code = Object.getOwnPropertyDescriptor(error, "code")?.value + // SQLSTATE excludes transport codes such as EPIPE and ECONNRESET. + return typeof code === "string" && /^[0-9A-Z]{5}$/.test(code) && /[0-9]/.test(code) ? code : undefined +} +const boundary = /^\s*(?:BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|START\s+TRANSACTION|PREPARE\s+TRANSACTION)\b/i +function settings(options: PartitionWriteOptions) { + const now = Date.now(), deadlineAt = options.deadlineAt ?? now + 30000 + if (!Number.isSafeInteger(deadlineAt) || deadlineAt <= now || deadlineAt - now > 120000 || options.signal?.aborted) throw new PartitionWriteError() + return { ...options, deadlineAt } +} +function remaining(deadlineAt: number) { + const value = deadlineAt - Date.now() + if (value <= 0) throw new PartitionWriteError() + return Math.max(1, Math.min(5000, value)) +} + +/** Caller MUST already own an open SQL transaction on this exact backend and + * MUST call before any partition/table query or secondary writer lock. This + * helper never starts/commits a caller's transaction and never retries SQL. + * Its bounded server timeout also applies when a surrounding Drizzle tx owns + * the lifecycle. The surrounding owner must roll back/destroy on rejection. + */ +type PartitionAdmissionClient = { query(text: string, values?: [number]): Promise } + +async function acquirePartitionWriter(client: PartitionAdmissionClient, options: Omit, acquire: () => Promise) { + const current = settings(options) + const check = async () => { + if (current.signal?.aborted) throw new PartitionWriteError() + remaining(current.deadlineAt); await current.authorize?.() + if (current.signal?.aborted) throw new PartitionWriteError() + remaining(current.deadlineAt) + } + await check() + await client.query(`SET LOCAL lock_timeout='${remaining(current.deadlineAt)}ms'`) + await check() + await client.query(`SET LOCAL statement_timeout='${remaining(current.deadlineAt)}ms'`) + await check() + const acquired = await acquire() + if (acquired && databaseRestoreQuarantineEnabled()) { + await assertDatabaseRestoreQuarantineOpen({ query: text => client.query(text) }) + } + if (acquired && process.env.DATABASE_RUNTIME_ACTIVATION_DIRECTORY !== undefined) { + const { assertRuntimeDatabaseActivationCurrent } = await import("../db/index.js") + assertRuntimeDatabaseActivationCurrent() + } + await check() + return acquired +} + +export async function admitPartitionWriter(client: PartitionAdmissionClient, options: Omit = {}): Promise { + await acquirePartitionWriter(client, options, async () => { + await client.query("SELECT pg_advisory_xact_lock_shared($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + return true + }) +} + +export function tryAdmitPartitionWriter(client: Pick, options: Omit = {}): Promise { + return acquirePartitionWriter(client, options, async () => { + const { rows } = await client.query<{ acquired: boolean }>( + "SELECT pg_catalog.pg_try_advisory_xact_lock_shared($1::bigint) AS acquired", [PARTITION_WRITER_ADVISORY_KEY]) + if (rows.length !== 1 || typeof rows[0].acquired !== "boolean") throw new PartitionWriteError() + return rows[0].acquired + }) +} + +/** Owns exactly one checked-out backend for raw SQL AND Drizzle. Admission is + * BEGIN → bounded LOCAL timeouts → shared writer key → target relation SQL. + * Query handles expire before COMMIT; detached work cannot use a pooled client + * after release. Any failed/uncertain boundary destroys the owner. No retries + * and no ROLLBACK is sent after an uncertain BEGIN/COMMIT acknowledgement. + */ +async function runPartitionWrite(work: (owner: PartitionWriteOwner) => Promise, options: PartitionWriteOptions, deferIfBusy: boolean): Promise { + const current = settings(options) + let client: PoolClient | undefined, released = false, active = false, accepting = false + let beginSent = false, beginAcknowledged = false, commitSent = false, committed = false, ended = false + let queryFailed = false + let stage: PartitionWriteStage = "checkout" + let querySqlState: string | undefined + const inflight = new Set>() + const release = (destroy: boolean) => { active = false; accepting = false; if (client && !released) { released = true; client.release(destroy) } } + const check = async () => { + if (ended || current.signal?.aborted) throw new PartitionWriteError(beginSent && !beginAcknowledged || commitSent) + remaining(current.deadlineAt); await Promise.race([current.authorize?.() ?? Promise.resolve(), stopped]) + if (ended || current.signal?.aborted) throw new PartitionWriteError(beginSent && !beginAcknowledged || commitSent) + remaining(current.deadlineAt) + } + let stop!: (error: PartitionWriteError) => void + const stopped = new Promise((_resolve, reject) => { stop = reject }) + void stopped.catch(() => {}) + const destroy = () => { ended = true; release(true); stop(new PartitionWriteError(beginSent && !beginAcknowledged || commitSent)) } + const timer = setTimeout(destroy, Math.max(1, current.deadlineAt - Date.now())) + current.signal?.addEventListener("abort", destroy, { once: true }) + try { + const connect = current.connect ?? (async () => (await import("../db/index.js")).pool.connect()) + const pending = connect() + client = await Promise.race([pending, stopped, new Promise((_resolve, reject) => { + const admissionTimer = setTimeout(() => reject(new PartitionWriteError()), Math.min(5000, remaining(current.deadlineAt))) + pending.finally(() => clearTimeout(admissionTimer)).catch(() => {}) + })]).catch(error => { ended = true; void pending.then(late => late.release(true), () => {}); throw error }) + if (ended) { release(true); throw new PartitionWriteError() } + await check() + stage = "begin" + beginSent = true + await Promise.race([client.query("BEGIN"), stopped]) + beginAcknowledged = true + stage = "admission" + await check() + if (deferIfBusy) { + const acquired = await Promise.race([tryAdmitPartitionWriter(client, current), stopped]) + if (!acquired) { + const rollback = await Promise.race([client.query("ROLLBACK"), stopped]) + if (rollback.command !== "ROLLBACK") throw new PartitionWriteError() + await check() + release(false) + throw new PartitionWriterBusy() + } + } else await Promise.race([admitPartitionWriter(client, current), stopped]) + stage = "work" + active = true; accepting = true + const rawClient = client + const guardedQuery = (...args: unknown[]) => { + const callback = typeof args.at(-1) === "function" ? args.pop() as (error: Error | null, value?: unknown) => void : undefined + const run = async () => { + if (!active || !accepting || released) throw new PartitionWriteError() + const text = typeof args[0] === "string" ? args[0] : args[0] && typeof args[0] === "object" && "text" in args[0] ? String(args[0].text) : "" + // Scope clients only run application statements. The owner keeps all + // transaction boundaries and cannot be escaped through Drizzle SQL. + const statement = text.replace(/\/\*[\s\S]*?\*\/|--[^\r\n]*/g, " ").trim() + if (!statement || boundary.test(statement) || /;\s*\S/.test(statement)) throw new PartitionWriteError() + await check() + if (!active || !accepting || released) throw new PartitionWriteError() + let result: unknown + try { result = await Reflect.apply(rawClient.query, rawClient, args) } + catch (error) { querySqlState ??= safeSqlState(error); throw error } + await check() + return result + } + const pending = run(); inflight.add(pending) + void pending.then(() => inflight.delete(pending), () => { queryFailed = true; inflight.delete(pending) }) + if (callback) { void pending.then(value => callback(null, value), error => callback(error)); return } + return pending + } + const guarded = new Proxy(rawClient, { get(target, property) { + if (property === "query") return guardedQuery + if (property === "release" || property === "end" || property === "connect" || property === "connection") throw new PartitionWriteError() + const value = Reflect.get(target, property, target) + return typeof value === "function" ? value.bind(target) : value + } }) + const owner: PartitionWriteOwner = { client: guarded, db: drizzle(guarded, { schema }) } + admittedClients.set(guarded, () => active && accepting && !released && !ended && current.deadlineAt > Date.now() && !current.signal?.aborted) + const result = await Promise.race([work(owner), stopped]) + accepting = false + // Await already-started work, then close both SQL handles BEFORE COMMIT. + await Promise.race([Promise.all([...inflight]), stopped]); active = false + if (queryFailed) throw new PartitionWriteError() + await check(); stage = "commit"; commitSent = true + const commitResult = await Promise.race([client.query("COMMIT"), stopped]) + if (commitResult.command !== "COMMIT") throw new PartitionWriteError(true) + committed = true + await check(); release(false) + return result + } catch (error) { + if (error instanceof PartitionWriterBusy && released && stage === "admission") throw error + const unknown = beginSent && !beginAcknowledged || commitSent || committed + destroy() + const failure = error instanceof PartitionWriteError && error.outcomeUnknown === unknown ? error : new PartitionWriteError(unknown) + failureDiagnostics.set(failure, { stage, sqlState: querySqlState ?? safeSqlState(error) }) + throw failure + } finally { + clearTimeout(timer); current.signal?.removeEventListener("abort", destroy) + if (!released) release(true) + } +} + +export function withPartitionWrite(work: (owner: PartitionWriteOwner) => Promise, options: PartitionWriteOptions = {}): Promise { + return runPartitionWrite(work, options, false) +} + +/** Defer scheduled maintenance only on a definite negative lock result. */ +export function withBackgroundPartitionWrite(work: (owner: PartitionWriteOwner) => Promise, options: PartitionWriteOptions = {}): Promise { + return runPartitionWrite(work, options, true) +} diff --git a/backend/src/services/ping-probe-configuration.ts b/backend/src/services/ping-probe-configuration.ts new file mode 100644 index 00000000..579b2a37 --- /dev/null +++ b/backend/src/services/ping-probe-configuration.ts @@ -0,0 +1,9 @@ +import { and, eq } from "drizzle-orm" +import { db } from "../db/index.js" +import { uptimeProbes } from "../db/schema.js" + +/** Only configuration participates in a reviewed plan, not collector metadata. */ +export async function readPingProbeConfiguration(probeId: string, serverId: number) { + return (await db.select({ id: uptimeProbes.id, srcServerId: uptimeProbes.srcServerId, srcInterface: uptimeProbes.srcInterface, name: uptimeProbes.name, target: uptimeProbes.target, probeFilter: uptimeProbes.probeFilter, enabled: uptimeProbes.enabled, intervalSec: uptimeProbes.intervalSec, showOnDashboard: uptimeProbes.showOnDashboard }) + .from(uptimeProbes).where(and(eq(uptimeProbes.id, probeId), eq(uptimeProbes.srcServerId, serverId))).limit(1))[0] ?? null +} diff --git a/backend/src/services/poller.ts b/backend/src/services/poller.ts index 9d29f35b..2b42f4ce 100644 --- a/backend/src/services/poller.ts +++ b/backend/src/services/poller.ts @@ -1,6 +1,6 @@ import { databaseBackgroundWork } from "./database-background-work.js" import { eq } from "drizzle-orm" -import { db, dbQuery, pool } from "../db/index.js" +import { db, pool } from "../db/index.js" import { dropExpiredPartitions, ensurePartitionsAround } from "../db/partitions.js" import { servers, serverSnapshots } from "../db/schema.js" import type { SnapshotInsert } from "../db/schema.js" @@ -8,6 +8,7 @@ import type { SnapshotRead } from "../types/server.js" import { MikrotikClient } from "./mikrotik.js" import { parseRosCpuLoadPercent, parseRosDataSizeBytes } from "./ros-metric-parse.js" import { invalidateFlowCatalogCache } from "./traffic-flow-topology.js" +import { withPartitionWrite } from "./partition-write-transaction.js" // ── pollServer ───────────────────────────────────────────────────────────────── @@ -78,21 +79,23 @@ export async function pollServer(serverId: number): Promise { console.warn(`[poller] server id=${serverId} unreachable:`, (err as Error).message) } - const [inserted] = await db + const [inserted] = await withPartitionWrite(({ db: writer }) => writer .insert(serverSnapshots) .values(partialSnap as SnapshotInsert) - .returning() + .returning()) if (inserted) { - await dbQuery( + await withPartitionWrite(({ client }) => client.query( `UPDATE server_snapshots SET raw_interfaces = NULL, raw_ip_addresses = NULL WHERE server_id = $1 AND polled_at < $2 AND (raw_interfaces IS NOT NULL OR raw_ip_addresses IS NOT NULL)`, [serverId, inserted.polledAt], - ) + )) + } + if (process.env.DATABASE_RUNTIME_REQUEST_FILE === undefined) { + void databaseBackgroundWork.track(dropExpiredPartitions(pool).then(() => ensurePartitionsAround(pool))).catch(() => {}) } - void databaseBackgroundWork.track(dropExpiredPartitions(pool).then(() => ensurePartitionsAround(pool))).catch(() => {}) return toSnapshotRead(inserted!) } diff --git a/backend/src/services/probe-interface-model.test.ts b/backend/src/services/probe-interface-model.test.ts new file mode 100644 index 00000000..28f22d09 --- /dev/null +++ b/backend/src/services/probe-interface-model.test.ts @@ -0,0 +1,30 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { buildProbeInterfaces } from "./probe-interface-model.js" + +const iface = { ".id": "*1", name: "ether1", running: "true", disabled: "false" } +test("only observed active enabled interfaces are eligible; unknown flags remain visible", () => { + const result = buildProbeInterfaces([iface, { ...iface, ".id": "*2", name: "unknown", running: "" }, { ...iface, ".id": "*3", name: "down", running: "false" }, { ...iface, ".id": "*4", name: "disabled", disabled: "true" }]) + assert.deepEqual(result.interfaces.map(row => row.name), ["ether1"]) + assert.equal(result.unknownInterfaces[0].name, "unknown"); assert.equal(result.incomplete, true) + assert.equal(result.forwardingVerified, false) +}) +test("known static/dynamic IPv4 address rows retain order, uniqueness and observed flags", () => { + const result = buildProbeInterfaces([iface], [ + { interface: "ether1", address: "192.0.2.2/24", dynamic: "true", disabled: "false", invalid: "false" }, + { interface: "ether1", address: "192.0.2.1/24", dynamic: "false", disabled: "false", invalid: "false" }, + { interface: "ether1", address: "192.0.2.3/24", disabled: "true" }, + ]) + assert.deepEqual(result.interfaces[0].addresses, ["192.0.2.1", "192.0.2.2"]) + assert.equal(result.interfaces[0].addressStateVerified, true); assert.equal(result.incomplete, false) +}) +test("omitted address flags and invalid IPv4 are not confirmed address availability", () => { + const result = buildProbeInterfaces([iface], [{ interface: "ether1", address: "192.0.2.1/24" }, { interface: "ether1", address: "192.0.2.2/24junk" }]) + assert.deepEqual(result.interfaces[0].addresses, ["192.0.2.1"]) + assert.equal(result.interfaces[0].invalidAddressRows, 1); assert.equal(result.interfaces[0].addressStateVerified, false); assert.equal(result.incomplete, true) +}) +test("known empty sources stay empty, malformed/duplicate identities are rejected", () => { + assert.equal(buildProbeInterfaces([], []).incomplete, false) + for (const payload of [{}, [null], [iface, iface], [iface, { ...iface, name: "ether2" }], [iface, { ...iface, ".id": "*2" }]]) assert.throws(() => buildProbeInterfaces(payload)) + assert.throws(() => buildProbeInterfaces([iface], {})) +}) diff --git a/backend/src/services/probe-interface-model.ts b/backend/src/services/probe-interface-model.ts new file mode 100644 index 00000000..e86057c6 --- /dev/null +++ b/backend/src/services/probe-interface-model.ts @@ -0,0 +1,24 @@ +import { isIP } from "node:net" +import { readInternetPathRows } from "./internet-path-snapshot.js" +import { observedBoolean } from "./ospf-observed-values.js" + +export function buildProbeInterfaces(raw: unknown, addressPayload?: unknown) { + const rows = readInternetPathRows(raw, "probe interfaces", [".id", "name"]) + if (new Set(rows.map(row => row.name.trim())).size !== rows.length) throw new Error("Неоднозначные имена интерфейсов проб") + if (new Set(rows.map(row => row[".id"])).size !== rows.length) throw new Error("Повторяющиеся ID интерфейсов проб") + const addresses = addressPayload === undefined ? undefined : readInternetPathRows(addressPayload, "probe addresses", ["interface", "address"]) + const unknownInterfaces: { id: string; name: string; running: boolean | null; disabled: boolean | null }[] = [] + const interfaces = rows.flatMap(row => { + const name = row.name.trim(), running = observedBoolean(row.running), disabled = observedBoolean(row.disabled) + if (running === false || disabled === true) return [] + if (running === null || disabled === null) { unknownInterfaces.push({ id: row[".id"], name, running, disabled }); return [] } + let invalidAddressRows = 0 + const candidates = addresses?.filter(address => address.interface.trim() === name && observedBoolean(address.disabled) !== true && observedBoolean(address.invalid) !== true).flatMap(address => { + const [ip, prefix, extra] = address.address.split("/") + if (isIP(ip) !== 4 || extra !== undefined || prefix !== undefined && (!/^\d+$/.test(prefix) || Number(prefix) > 32)) { invalidAddressRows++; return [] } + return [{ address: ip, disabled: observedBoolean(address.disabled), invalid: observedBoolean(address.invalid), dynamic: observedBoolean(address.dynamic) }] + }).sort((a, b) => Number(a.dynamic === true) - Number(b.dynamic === true)) + return [{ name, running, disabled, ...(candidates ? { addresses: [...new Set(candidates.map(candidate => candidate.address))], addressCandidates: candidates, invalidAddressRows, addressStateVerified: invalidAddressRows === 0 && candidates.every(candidate => candidate.disabled === false && candidate.invalid === false) } : {}) }] + }).sort((a, b) => a.name.localeCompare(b.name)) + return { interfaces, unknownInterfaces, incomplete: unknownInterfaces.length > 0 || interfaces.some(row => row.addressStateVerified === false), forwardingVerified: false } +} diff --git a/backend/src/services/probe-interface-read.ts b/backend/src/services/probe-interface-read.ts new file mode 100644 index 00000000..defdbcbb --- /dev/null +++ b/backend/src/services/probe-interface-read.ts @@ -0,0 +1,28 @@ +import { MikrotikClient } from "./mikrotik.js" +import { buildProbeInterfaces } from "./probe-interface-model.js" +import { readInternetPathRows } from "./internet-path-snapshot.js" +import { observedBoolean } from "./ospf-observed-values.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" + +export async function readProbeInterfacesForServer(server: ServerRow, withAddresses = false) { + if (!server.enabled) throw Object.assign(new Error("Сервер отключён: включите его перед опросом интерфейсов"), { statusCode: 409 }) + const client = MikrotikClient.fromServer(server) + const [raw, addresses] = await Promise.all([ + client.get("/interface?.proplist=.id,name,running,disabled"), + withAddresses ? client.getIpAddresses() : undefined, + ]) + const rows = readInternetPathRows(raw, "probe interfaces", [".id", "name"]) + if (rows.length > 1000) throw new Error("Каталог интерфейсов превышает предел проверки") + let cursor = 0 + const missing = rows.filter(row => row.disabled === undefined && observedBoolean(row.running) === true) + await Promise.all(Array.from({ length: Math.min(4, missing.length) }, async () => { + while (cursor < missing.length) { + const row = missing[cursor++]! + try { + const result = await client.post<{ ret?: string }>("/interface/get", { ".id": row[".id"], "value-name": "disabled" }) + if (typeof result?.ret === "string") row.disabled = result.ret + } catch { /* Preserve an unknown flag; it cannot become an eligible interface. */ } + } + })) + return buildProbeInterfaces(rows, addresses) +} diff --git a/backend/src/services/recursive-apply.test.ts b/backend/src/services/recursive-apply.test.ts index fc2c8b28..f0886845 100644 --- a/backend/src/services/recursive-apply.test.ts +++ b/backend/src/services/recursive-apply.test.ts @@ -23,4 +23,38 @@ reset();badAck=true;await expectState("unknown",0);assert.deepEqual(events,["put reset();failDelete=true;await expectState("partial",2);assert.deepEqual(events,["put:1","put:2","delete:/ip/route/*old"]) reset();failPersist=true;await expectState("applied",3) reset();rows=[];assert.deepEqual(await executeRecursiveApply(client,[],persist),{pushed:0,deleted:0});assert.deepEqual(events,["persist"]) +reset();await assert.rejects(executeRecursiveApply(client,payloads,persist,async()=>{throw Error("Observed set changed before first write")}));assert.deepEqual(events,[]) +reset();rows={};assert.deepEqual(await executeRecursiveApply(client,payloads,persist,async()=>[owned]),{pushed:2,deleted:1});assert.deepEqual(events,["put:1","put:2","delete:/ip/route/*old","persist"]) +// Production uses fresh read-back after each command and rechecks the original +// target before deleting its ID. A new route must work as requested first. +for (const scenario of ["success", "ignored-put", "reused-ack", "changed-target", "ignored-delete", "lost-read"] as const) { + let live: Record[] = [{...owned}], reads=0, persisted=false + const calls: string[]=[] + const observe = async () => { + reads++ + if(scenario==="lost-read") throw Error("Чтение потеряно после PUT") + if(scenario==="changed-target" && reads===2) live[0]={...live[0],gateway:"192.0.2.99"} + return live.map(row=>({...row})) + } + const guarded: Pick = { + get:async()=>live as T, + put:async(_path,body)=>{ + calls.push("put") + const id=scenario==="reused-ack"?owned[".id"]:"*new" + live.push({...owned,...body as Record,".id":id,...(scenario==="ignored-put"?{distance:"99"}:{})}) + return {".id":id} as T + }, + delete:async path=>{calls.push("delete");if(scenario!=="ignored-delete")live=live.filter(row=>row[".id"]!==path.split("/").at(-1))}, + } + const {".id":_id,...payload}=owned + const run=()=>executeRecursiveApply(guarded,[{...payload,distance:"2"}],async()=>{persisted=true},async()=>live.map(row=>({...row})),observe) + if(scenario==="success") { + assert.deepEqual(await run(),{pushed:1,deleted:1});assert.equal(persisted,true);assert.deepEqual(calls,["put","delete"]) + } else { + await assert.rejects(run(),(error:unknown)=>{assert(error instanceof RecursiveApplyError);assert.equal(error.receipt.state,scenario==="lost-read"?"unknown":"partial");return true}) + assert.equal(persisted,false) + assert.deepEqual(calls,scenario==="ignored-delete"?["put","delete"]:["put"]) + assert.ok(live.some(row=>row[".id"]===owned[".id"])) + } +} console.log("Recursive apply: strict ownership before PUT, foreign retention, partial/unknown/rejected/applied, no retry/delete after rejection") diff --git a/backend/src/services/recursive-apply.ts b/backend/src/services/recursive-apply.ts index c16d5af0..0608a951 100644 --- a/backend/src/services/recursive-apply.ts +++ b/backend/src/services/recursive-apply.ts @@ -2,6 +2,17 @@ import type { MikrotikClient } from "./mikrotik.js" import { MikrotikError } from "./mikrotik.js" import { parseRecursiveCatalog } from "./recursive-route-catalog.js" +class RecursiveVerificationError extends Error {} +function matches(expected: ReturnType[number], actual: ReturnType[number], allowDefaults = false) { + const destination = (value: string) => value.includes("/") ? value : value + "/32" + return expected.id === actual.id && destination(expected.dstAddress) === destination(actual.dstAddress) + && expected.gateway === actual.gateway && expected.distance === actual.distance + && (allowDefaults && expected.scope === null || expected.scope === actual.scope) + && (allowDefaults && expected.targetScope === null || expected.targetScope === actual.targetScope) + && expected.routingTable === actual.routingTable && expected.checkGateway === actual.checkGateway + && expected.comment === actual.comment && expected.disabled === actual.disabled +} + export class RecursiveApplyError extends Error { constructor(message: string, public readonly receipt: { state: "partial" | "unknown" | "rejected" | "applied" @@ -17,9 +28,11 @@ export async function executeRecursiveApply( client: Pick, payloads: Record[], persist: () => Promise, + observeExisting?: () => Promise, + observeCurrent?: () => Promise, ) { // The editor replaces every owned row: reject unsupported ownership before any write. - const existing = parseRecursiveCatalog(await client.get("/ip/route")) + const existing = parseRecursiveCatalog(observeExisting ? await observeExisting() : await client.get("/ip/route")) const createdIds: string[] = [], deletedIds: string[] = [] let nativeComplete = false try { @@ -27,16 +40,31 @@ export async function executeRecursiveApply( const result = await client.put("/ip/route", payload) if (!result || typeof result !== "object" || !(".id" in result) || typeof result[".id"] !== "string" || !result[".id"].trim()) throw new Error("RouterOS не подтвердил ID созданного маршрута") createdIds.push(result[".id"]) + if (existing.some(row => row.id === result[".id"]) || new Set(createdIds).size !== createdIds.length) throw new RecursiveVerificationError("RouterOS вернул уже используемый ID созданного маршрута") + if (observeCurrent) { + const expected = parseRecursiveCatalog([{ ...payload, ".id": result[".id"], static: "true" }])[0] + const observed = parseRecursiveCatalog(await observeCurrent()).find(row => row.id === result[".id"]) + if (!expected || !observed || !matches(expected, observed, true)) throw new RecursiveVerificationError("Созданный маршрут не подтверждён чтением; удаление прежнего набора остановлено") + } } for (const route of existing) { + if (observeCurrent) { + const target = parseRecursiveCatalog(await observeCurrent()).find(row => row.id === route.id) + if (!target || !matches(route, target)) throw new RecursiveVerificationError("Маршрут изменился или его ID использован повторно; удаление остановлено") + } await client.delete("/ip/route/" + encodeURIComponent(route.id)) deletedIds.push(route.id) + if (observeCurrent) { + const observed = await observeCurrent() + parseRecursiveCatalog(observed) + if ((observed as Record[]).some(row => row[".id"] === route.id)) throw new RecursiveVerificationError("Удаление маршрута не подтверждено чтением") + } } nativeComplete = true await persist() return { pushed: createdIds.length, deleted: deletedIds.length } } catch (error) { - const rejected = error instanceof MikrotikError && error.statusCode >= 400 && error.statusCode < 500 + const rejected = error instanceof RecursiveVerificationError || error instanceof MikrotikError && error.statusCode >= 400 && error.statusCode < 500 const confirmedWrites = createdIds.length + deletedIds.length const state = nativeComplete ? "applied" : rejected ? (confirmedWrites ? "partial" : "rejected") : "unknown" throw new RecursiveApplyError(`Подтверждено записей RouterOS: ${confirmedWrites}. Обновите каталог перед повторной попыткой. ${String(error)}`, { diff --git a/backend/src/services/recursive-route-catalog.test.ts b/backend/src/services/recursive-route-catalog.test.ts index 39054113..0a2c2761 100644 --- a/backend/src/services/recursive-route-catalog.test.ts +++ b/backend/src/services/recursive-route-catalog.test.ts @@ -14,6 +14,10 @@ assert.equal(parseRecursiveCatalog([{...row,disabled:"true"}])[0].disabled,true) const gateway=(id:string,active?:string)=>({".id":id,gateway:"192.0.2.1%ether1",active}) assert.equal(parseRecursiveGatewayCatalog([gateway("a")])[0].status,"unknown") assert.equal(parseRecursiveGatewayCatalog([gateway("a","false")])[0].status,"inactive") +assert.equal(parseRecursiveGatewayCatalog([{...gateway("a"),inactive:"true"}])[0].status,"inactive") +assert.equal(parseRecursiveGatewayCatalog([{...gateway("a"),inactive:true}])[0].status,"inactive") +assert.equal(parseRecursiveGatewayCatalog([{...gateway("a"),inactive:"false"}])[0].status,"unknown") +assert.equal(parseRecursiveGatewayCatalog([{...gateway("a","true"),inactive:"true"}])[0].status,"unknown") assert.equal(parseRecursiveGatewayCatalog([gateway("a","false"),gateway("b")])[0].status,"unknown") assert.equal(parseRecursiveGatewayCatalog([gateway("a"),gateway("b","true")])[0].status,"active") assert.deepEqual(parseRecursiveGatewayCatalog([{".id":"a",gateway:"999.0.0.1"}]),[]) diff --git a/backend/src/services/recursive-route-catalog.ts b/backend/src/services/recursive-route-catalog.ts index ea82268e..c12a6928 100644 --- a/backend/src/services/recursive-route-catalog.ts +++ b/backend/src/services/recursive-route-catalog.ts @@ -75,8 +75,12 @@ export function parseRecursiveGatewayCatalog(payload: unknown): RecursiveGateway if (typeof row.gateway !== "string") continue const [ip, name = ip] = row.gateway.trim().split("%") if (!/^\d{1,3}(\.\d{1,3}){3}$/.test(ip) || ip.split(".").some(part => Number(part) > 255) || !name) continue - const id = ip + "%" + name, active = observedBoolean(rosText(row.active)) - const status = active === true ? "active" : active === false ? "inactive" : "unknown" + const id = ip + "%" + name + const active = typeof row.active === "boolean" ? row.active : observedBoolean(rosText(row.active)) + const inactive = typeof row.inactive === "boolean" ? row.inactive : observedBoolean(rosText(row.inactive)) + let status: RecursiveGatewayObservation["status"] = "unknown" + if (active === true && inactive !== true) status = "active" + if (active !== true && (active === false || inactive === true)) status = "inactive" const previous = result.get(id) result.set(id, { id, ip, name, status: previous?.status === "active" || status === "active" ? "active" : previous?.status === "unknown" || status === "unknown" ? "unknown" : "inactive" }) } diff --git a/backend/src/services/recursive-route-identity.test.ts b/backend/src/services/recursive-route-identity.test.ts new file mode 100644 index 00000000..4461f2ce --- /dev/null +++ b/backend/src/services/recursive-route-identity.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict" +import { planRecursiveIdentities as plan, type RecursiveIdentityRow } from "./recursive-route-identity.js" +const row: RecursiveIdentityRow = { id: "1", dstAddress: "192.0.2.1/32", gateway: "198.51.100.1", distance: 0, scope: 0, targetScope: 0, routingTable: "main", checkGateway: "none", country: "Метка", comment: "Маршрут", disabled: false } +const second = { ...row, id: "2", gateway: "198.51.100.2", country: "Другая" } +const refs = new Set(["1"]) +assert.deepEqual(plan([row], [{ ...row, id: "*A", dstAddress: "192.0.2.1", country: "" }], refs, "observed").rows[0], { ...row, dstAddress: "192.0.2.1" }) +assert.deepEqual(plan([row, second], [{ ...second, id: "*B", country: "" }, { ...row, id: "*A", country: "" }], refs, "observed").rows.map(r => [r.id, r.country]), [["2", "Другая"], ["1", "Метка"]]) +for (const change of [{ distance: 17 }, { gateway: "198.51.100.99" }, { disabled: true }, { scope: null, targetScope: null }]) { + assert.equal(plan([row], [{ ...row, ...change, id: "*new" }], refs, "observed").rows[0].id, "1") +} +assert.throws(() => plan([row], [], refs, "observed"), /BGP/) +assert.throws(() => plan([{ ...row, dstAddress: "192.0.2.1/24" }], [{ ...row, dstAddress: "192.0.2.2/24" }], refs, "apply"), /BGP/) +for (const change of [{ dstAddress: "192.0.2.2/32" }, { routingTable: "other" }]) assert.throws(() => plan([row], [{ ...row, ...change }], refs, "apply"), /BGP/) +assert.equal(plan([row], [{ ...row, country: "" }], refs, "apply").rows[0].country, "") +assert.throws(() => plan([row], [{ ...row, id: "999" }], refs, "apply"), /сервере/) +assert.equal(plan([row], [{ ...row, id: "999" }], refs, "copy").rows[0].id, "1") +assert.throws(() => plan([row, second], [{ ...row, id: "*A", gateway: "198.51.100.3" }, { ...second, id: "*B", gateway: "198.51.100.4" }], refs, "observed"), /Неоднозначное/) +assert.throws(() => plan([row, { ...row, id: "2", country: "Другая" }], [{ ...row, id: "*A", country: "" }], refs, "observed"), /Неоднозначные/) +assert.deepEqual(plan([row], [], new Set(), "apply").deletedIds, ["1"]) +assert.equal(plan([], [{ ...row, id: "*A" }], new Set(), "observed").rows[0].id, "") +console.log("recursive identities: stable IDs, host aliases, reorder, next-hop changes, country ownership, scoped IDs and referenced/ambiguous removal guards passed") diff --git a/backend/src/services/recursive-route-identity.ts b/backend/src/services/recursive-route-identity.ts new file mode 100644 index 00000000..8f5a8cb8 --- /dev/null +++ b/backend/src/services/recursive-route-identity.ts @@ -0,0 +1,62 @@ +import type { z } from "zod" +import type { recursiveInputRowSchema } from "./recursive-route-validation.js" + +export type RecursiveIdentityRow = Omit, "checkGateway"> & { checkGateway: string } +export type RecursiveIdentityMode = "observed" | "apply" | "copy" | "rollback" + +// RouterOS may spell a host route with or without /32. Keep host bits for other +// prefixes: BGP filters derive their gateway from the literal destination IP. +function destination(value: string) { + const [ip, suffix] = value.split("/") + const bits = suffix === undefined ? 32 : Number(suffix) + const address = ip.split(".").reduce((n, octet) => n * 256 + Number(octet), 0) + return `${address}/${bits}` +} +function prefix(row: RecursiveIdentityRow) { return JSON.stringify([destination(row.dstAddress), row.routingTable]) } +function exact(row: RecursiveIdentityRow) { + return JSON.stringify([prefix(row), row.gateway, row.distance, row.scope, row.targetScope, row.checkGateway, row.comment, row.disabled]) +} + +/** Match logical routes without letting observation or RouterOS recreation change + * rec: references. Ambiguous changes require explicit DB IDs from the editor. */ +export function planRecursiveIdentities( + current: RecursiveIdentityRow[], desired: RecursiveIdentityRow[], + referenced: ReadonlySet, mode: RecursiveIdentityMode, +) { + const available = new Map(current.map(row => [row.id, row])) + const matches = new Map() + const bind = (index: number, row: RecursiveIdentityRow) => { + matches.set(index, row); available.delete(row.id) + } + if (mode === "apply") desired.forEach((row, index) => { + if (!/^\d+$/.test(row.id)) return + const old = available.get(row.id) + if (!old) throw new Error("Идентификатор маршрута отсутствует на выбранном сервере или повторяется") + bind(index, old) + }) + // Match unchanged entries before considering changed next hops or priorities. + desired.forEach((row, index) => { + if (matches.has(index)) return + const candidates = [...available.values()].filter(old => exact(old) === exact(row)) + if (new Set(candidates.map(old => old.country)).size > 1) throw new Error("Неоднозначные метки одинаковых рекурсивных маршрутов") + if (candidates.length) bind(index, candidates[0]) + }) + for (const key of new Set(desired.map(prefix))) { + const pending = desired.flatMap((row, index) => !matches.has(index) && prefix(row) === key ? [index] : []) + const candidates = [...available.values()].filter(old => prefix(old) === key) + if (!pending.length || !candidates.length) continue + if (pending.length !== 1 || candidates.length !== 1) throw new Error("Неоднозначное изменение рекурсивных маршрутов: нужны сохранённые идентификаторы") + bind(pending[0], candidates[0]) + } + for (const [index, old] of matches) { + if (referenced.has(old.id) && prefix(old) !== prefix(desired[index])) throw new Error("Префикс или таблица маршрута используются BGP-фильтром") + } + for (const row of available.values()) if (referenced.has(row.id)) throw new Error("Маршрут используется BGP-фильтром: сначала измените ссылку rec:") + return { + rows: desired.map((row, index) => { + const old = matches.get(index) + return { ...row, id: old?.id ?? "", country: mode === "observed" && !row.country ? old?.country ?? "" : row.country } + }), + deletedIds: [...available.keys()], + } +} diff --git a/backend/src/services/recursive-route-store.ts b/backend/src/services/recursive-route-store.ts new file mode 100644 index 00000000..56eab3ce --- /dev/null +++ b/backend/src/services/recursive-route-store.ts @@ -0,0 +1,38 @@ +import { pool } from "../db/index.js" +import { planRecursiveIdentities, type RecursiveIdentityRow, type RecursiveIdentityMode } from "./recursive-route-identity.js" + +const columns = `id::text AS id, dst_address AS "dstAddress", gateway, distance, scope, + target_scope AS "targetScope", routing_table AS "routingTable", check_gateway AS "checkGateway", + country, comment, disabled` + +/** Validation is also run before sending any native mutation. Persistence repeats + * it under table/row locks so a late filter binding cannot be silently orphaned. */ +export async function storeRecursiveRoutes(serverId: number, desired: RecursiveIdentityRow[], mode: RecursiveIdentityMode, validateOnly = false) { + const client = await pool.connect() + try { + await client.query("BEGIN") + await client.query("LOCK TABLE filter_rules IN SHARE ROW EXCLUSIVE MODE") + const current = (await client.query(`SELECT ${columns} FROM recursive_routes WHERE server_id=$1 ORDER BY sort_order,id FOR UPDATE`, [serverId])).rows + const references = (await client.query<{ id: string }>(`SELECT r.id::text AS id FROM recursive_routes r JOIN filter_rules f + ON f.server_id=r.server_id AND f.gateway_tunnel_id='rec:'||r.id::text WHERE r.server_id=$1`, [serverId])).rows + const plan = planRecursiveIdentities(current, desired, new Set(references.map(row => row.id)), mode) + if (!validateOnly) { + for (const [index, row] of plan.rows.entries()) { + const values = [serverId, index, row.dstAddress, row.gateway, row.distance, row.scope, row.targetScope, row.routingTable, row.checkGateway, row.country, row.comment, row.disabled] + if (row.id) await client.query(`UPDATE recursive_routes SET sort_order=$2,dst_address=$3,gateway=$4,distance=$5,scope=$6,target_scope=$7, + routing_table=$8,check_gateway=$9,country=$10,comment=$11,disabled=$12,updated_at=now() + WHERE server_id=$1 AND id=$13 AND ROW(sort_order,dst_address,gateway,distance,scope,target_scope,routing_table,check_gateway,country,comment,disabled) + IS DISTINCT FROM ROW($2::integer,$3::text,$4::text,$5::integer,$6::integer,$7::integer,$8::text,$9::text,$10::text,$11::text,$12::boolean)`, [...values, row.id]) + else row.id = String((await client.query(`INSERT INTO recursive_routes + (server_id,sort_order,dst_address,gateway,distance,scope,target_scope,routing_table,check_gateway,country,comment,disabled) + VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) RETURNING id`, values)).rows[0].id) + } + if (plan.deletedIds.length) await client.query("DELETE FROM recursive_routes WHERE server_id=$1 AND id=ANY($2::bigint[])", [serverId, plan.deletedIds]) + } + await client.query("COMMIT") + return plan.rows + } catch (error) { + await client.query("ROLLBACK") + throw error + } finally { client.release() } +} diff --git a/backend/src/services/router-config-export.test.ts b/backend/src/services/router-config-export.test.ts index 58e9f9a2..d997d6b7 100644 --- a/backend/src/services/router-config-export.test.ts +++ b/backend/src/services/router-config-export.test.ts @@ -1,7 +1,18 @@ import assert from 'node:assert/strict' import {createServer} from 'node:http' import {MikrotikClient} from './mikrotik.js' +import {pool,closePool} from '../db/index.js' +import {configurationAuditTransactionFixture,emptyActiveRoutingProfilesSqlFixture} from '../test/fixtures/managed-mutation-sql.js' +const originalQuery=pool.query,originalConnect=pool.connect +pool.connect=(async()=>({async query(text:string,values?:unknown[]){ + const admission=configurationAuditTransactionFixture(text,values) + if(admission)return admission + if(text.startsWith('INSERT INTO mcp_audit('))return pool.query(text,values) + assert(['SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired','SELECT pg_advisory_unlock(hashtextextended($1,0))'].includes(text));assert.deepEqual(values,['mm-config:71']);return{rows:[{acquired:true}]} +},release(){}})) as typeof pool.connect +pool.query=(async(text:string,values?:unknown[])=>{const routing=emptyActiveRoutingProfilesSqlFixture(text,values,71);if(routing)return routing;if(text.startsWith('SELECT object_ref FROM mcp_protections WHERE')){assert.deepEqual(values,['71']);return{rows:[]}}assert.equal(text,'INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)');assert.equal(JSON.parse(String(values?.[3])).serverId,71);return{rows:[]}}) as typeof pool.query const good='# oct/14/2026 by RouterOS 7.24.5\n/system identity\nset name=export-test\n' +try{ for(const mode of ['success','malformed-list','export-error','truncated','section-error','oversize','cleanup-error','foreign-id','short-chunk','missing-data','changed-file','utf8-boundary','max-size']){ let ownName='',exportCalls=0,readCalls=0,deletes=0 const original={'.id':'*ORIG',name:'important.rsc',size:'2'} @@ -14,8 +25,34 @@ for(const mode of ['success','malformed-list','export-error','truncated','sectio if(req.method==='POST'&&req.url==='/rest/file/get'){readCalls++;return reply(200,[{ret:mode==='truncated'?content.slice(0,20):content}])} if(req.method==='DELETE'&&req.url==='/rest/file/*TEMP'){deletes++;if(mode==='cleanup-error')return reply(500,{error:'cleanup denied'});own=null;return reply(200,{})} assert.fail('Unexpected request '+req.method+' '+req.url) - });await new Promise(r=>fixture.listen(0,'127.0.0.1',r));const address=fixture.address();assert(address&&typeof address!=='string');const c=new MikrotikClient({host:'127.0.0.1',port:address.port,useSsl:false,verifySsl:false,username:'fixture',password:'fixture'}); + });await new Promise(r=>fixture.listen(0,'127.0.0.1',r));const address=fixture.address();assert(address&&typeof address!=='string');const c=new MikrotikClient({host:'127.0.0.1',port:address.port,useSsl:false,verifySsl:false,username:'fixture',password:'fixture'}, 71); try{if(['success','oversize','utf8-boundary'].includes(mode)){assert.equal(await c.exportConfigScript(),content);assert.equal(readCalls,mode==='success'?1:3)}else await assert.rejects(c.exportConfigScript(), mode);assert.equal(original.name,'important.rsc');if(mode==='malformed-list')assert.equal(exportCalls,0);else assert.equal(exportCalls,1);if(!['malformed-list','foreign-id'].includes(mode))assert.equal(deletes,1);if(!['cleanup-error','foreign-id'].includes(mode))assert.equal(own,null);if(mode==='max-size')assert.equal(readCalls,0);if(['short-chunk','missing-data'].includes(mode))assert.equal(readCalls,1)}finally{await new Promise(r=>fixture.close(()=>r()))} } console.log('native configuration export: 13 HTTP scenarios passed; original file never deleted') +for (const mode of ['unsupported-put', 'lost-put-response', 'missing-readback'] as const) { + const methods: string[] = [] + const fixture = createServer(async (request, response) => { + methods.push(`${request.method} ${request.url}`) + for await (const _chunk of request) { /* Drain the request. */ } + if (mode === 'lost-put-response' && request.method === 'PUT') { + request.socket.destroy() + return + } + if (mode === 'unsupported-put' && request.method === 'PUT') response.statusCode = 405 + response.setHeader('content-type', 'application/json') + response.end(JSON.stringify(request.method === 'GET' ? (mode === 'unsupported-put' + ? [{'.id':'*NEW', name:'flash/test.rsc', size:'5'}] : []) : {})) + }) + await new Promise(resolve => fixture.listen(0, '127.0.0.1', resolve)) + const address = fixture.address() + assert(address && typeof address !== 'string') + const router = new MikrotikClient({ host:'127.0.0.1', port:address.port, useSsl:false, verifySsl:false, username:'fixture', password:'fixture' }, 71) + try { + if (mode === 'unsupported-put') assert.equal(await router.uploadTextFile('test.rsc', 'test'), 'flash/test.rsc') + else await assert.rejects(router.uploadTextFile('test.rsc', 'test')) + assert.deepEqual(methods, mode === 'unsupported-put' ? ['PUT /rest/file', 'POST /rest/file/add', 'GET /rest/file'] + : mode === 'missing-readback' ? ['PUT /rest/file', 'GET /rest/file'] : ['PUT /rest/file']) + } finally { await new Promise(resolve => fixture.close(() => resolve())) } +} +}finally{pool.query=originalQuery;pool.connect=originalConnect;await closePool()} diff --git a/backend/src/services/router-configuration-guard-compatibility.sql-helper.ts b/backend/src/services/router-configuration-guard-compatibility.sql-helper.ts new file mode 100644 index 00000000..5d5f2915 --- /dev/null +++ b/backend/src/services/router-configuration-guard-compatibility.sql-helper.ts @@ -0,0 +1,90 @@ +/** Opt-in owned acceptance on the genuine existing signed v1/v2 control + * fixture. No substitute core schema, source signature or renewed TTL. */ +import assert from "node:assert/strict" +import { createHash, randomBytes } from "node:crypto" +import type { Pool } from "pg" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" +import type { createConfiguredBandwidthControlSource } from "./bandwidth-control-source.js" +import type { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import { BANDWIDTH_CONTROL_ROLE_SQL } from "./bandwidth-control-role.js" +import { BANDWIDTH_CONTROL_CATALOG_SQL } from "./bandwidth-control-catalog.js" +import { BANDWIDTH_NODE_REGISTRY_ROLE_SQL } from "./bandwidth-node-registry-role.js" +import { BANDWIDTH_NODE_REGISTRY_CATALOG_SQL, BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL, BANDWIDTH_PEER_REGISTRY_INCOMING_SQL } from "./bandwidth-node-registry-catalog.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL } from "./bandwidth-external-node-registry-role.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL } from "./bandwidth-external-node-registry-catalog.js" +import { ROUTER_GUARD_DDL } from "./router-configuration-guard-schema.js" + +const quote = (value: string) => { assert(/^[a-z_]+[a-f0-9]{16,32}$/.test(value)); return '"' + value + '"' } +const scope = () => ({ deadlineAt: Date.now() + 10000 }) + +export async function acceptOwnedRouterGuardControlCompatibility(input: { + admin: Pool; databaseName: string; runtimeRole: string; ownerRole: string; v2: boolean + core: ReturnType; node: ReturnType +}) { + assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_TEST_ROUTER_GUARD_COMPATIBILITY, "true") + assert(/^(bns|bex)_control_[a-f0-9]{32}$/.test(input.databaseName)) + const target = (await input.admin.query("SELECT current_database() AS name,r.rolsuper AS super FROM pg_catalog.pg_roles r WHERE r.rolname=current_user")).rows[0] + assert.equal(target.name, input.databaseName); assert.equal(target.super, true) + assert.equal((await input.admin.query("SELECT pg_catalog.to_regnamespace('mmapp_router_guard') IS NULL AS absent")).rows[0].absent, true) + const suffix = randomBytes(8).toString("hex"), owner = "rg_compat_owner_" + suffix, runtime = "rg_compat_runtime_" + suffix + let ownerCreated = false, runtimeCreated = false, installed = false, cleaned = false + const cleanup = async () => { + if (cleaned) return; cleaned = true + // This scope owns exactly this fresh namespace and these generated roles; + // it never drops/reseals the genuine pre-existing core/source namespace. + if (installed) { + const actual = (await input.admin.query("SELECT r.rolname AS owner FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_roles r ON r.oid=n.nspowner WHERE n.nspname='mmapp_router_guard'")).rows + assert.equal(actual.length, 1); assert.equal(actual[0].owner, owner) + await input.admin.query("DROP SCHEMA mmapp_router_guard CASCADE") + } + if (runtimeCreated) await input.admin.query(`DROP ROLE ${quote(runtime)}`) + if (ownerCreated) { await input.admin.query(`REVOKE CREATE ON DATABASE ${quote(input.databaseName)} FROM ${quote(owner)}`); await input.admin.query(`DROP ROLE ${quote(owner)}`) } + } + const observe = async () => { + const window = scope(), connection = await input.core.connect(window) + let snapshot: unknown[] + try { + const values = [input.databaseName, input.runtimeRole, input.ownerRole] + snapshot = [ + (await connection.query(BANDWIDTH_CONTROL_ROLE_SQL, values)).rows, + (await connection.query(BANDWIDTH_CONTROL_CATALOG_SQL)).rows, + (await connection.query(BANDWIDTH_NODE_REGISTRY_ROLE_SQL, values)).rows, + (await connection.query(input.v2 ? BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL : BANDWIDTH_NODE_REGISTRY_CATALOG_SQL)).rows, + ] + if (input.v2) snapshot.push((await connection.query(BANDWIDTH_EXTERNAL_NODE_REGISTRY_ROLE_SQL, values)).rows, + (await connection.query(BANDWIDTH_EXTERNAL_NODE_REGISTRY_CATALOG_SQL)).rows, (await connection.query(BANDWIDTH_PEER_REGISTRY_INCOMING_SQL)).rows) + assert.equal((await connection.query("SELECT 1 AS genuine_control_query")).rows[0].genuine_control_query, 1) + } finally { connection.release(true) } + const identity = await input.core.identity(scope()), session = await input.node.open(scope()) + try { + const binding = await session.binding() + assert.equal((await session.connection.query("SELECT 1 AS genuine_node_query")).rows[0].genuine_node_query, 1) + return { digest: createHash("sha256").update(canonical(snapshot)).digest("hex"), identity, binding } + } finally { session.close() } + } + try { + const before = await observe() + await input.admin.query(`CREATE ROLE ${quote(owner)} NOLOGIN NOINHERIT NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS`); ownerCreated = true + await input.admin.query(`CREATE ROLE ${quote(runtime)} NOLOGIN NOINHERIT NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS`); runtimeCreated = true + await input.admin.query(`GRANT CREATE ON DATABASE ${quote(input.databaseName)} TO ${quote(owner)}`) + const physical = await input.admin.connect() + try { await physical.query(`SET ROLE ${quote(owner)}`); await physical.query(ROUTER_GUARD_DDL); installed = true } + finally { await physical.query("RESET ROLE"); physical.release(true) } + await input.admin.query(`REVOKE CREATE ON DATABASE ${quote(input.databaseName)} FROM ${quote(owner)}`) + await input.admin.query(`GRANT USAGE ON SCHEMA mmapp_router_guard TO ${quote(runtime)}; + GRANT SELECT ON ALL TABLES IN SCHEMA mmapp_router_guard TO ${quote(runtime)}; + GRANT INSERT ON mmapp_router_guard.attempts,mmapp_router_guard.events TO ${quote(runtime)}; + GRANT UPDATE ON mmapp_router_guard.head TO ${quote(runtime)}; + GRANT EXECUTE ON FUNCTION mmapp_router_guard.protect_rows() TO ${quote(runtime)}`) + const role = await input.admin.connect() + try { + await role.query(`SET ROLE ${quote(runtime)}`) + assert.equal((await role.query("SELECT count(*)::integer AS count FROM mmapp_router_guard.head")).rows[0].count, 0) + await assert.rejects(role.query("SELECT * FROM mmapp_control.bandwidth_source_identity"), error => (error as { code?: string }).code === "42501") + } finally { await role.query("RESET ROLE"); role.release(true) } + const after = await observe() + assert.deepEqual(after, before) + return { cleanup, report: { version: input.v2 ? 2 : 1, exactRoleCatalogPinsUnchanged: true, genuineConfiguredCoreAndNodeQueriesPassed: true, guardActivated: false, schemaMigrationsChanged: false } } + } catch (error) { await cleanup(); throw error } +} diff --git a/backend/src/services/router-configuration-guard-schema.ts b/backend/src/services/router-configuration-guard-schema.ts new file mode 100644 index 00000000..a990ecf3 --- /dev/null +++ b/backend/src/services/router-configuration-guard-schema.ts @@ -0,0 +1,199 @@ +/** Explicit EXTERNAL owner installation, never an application migration or + * startup repair. No cross-schema FK, trigger, grant or inherited role. */ +import { createHash } from "node:crypto" +export const ROUTER_GUARD_LOCK = 8723129 +export const ROUTER_GUARD_ARCHIVE_EXCLUSION = "--exclude-schema=mmapp_router_guard" +export const ROUTER_GUARD_DDL = ` +CREATE SCHEMA mmapp_router_guard; +REVOKE ALL ON SCHEMA mmapp_router_guard FROM PUBLIC; +CREATE TABLE mmapp_router_guard.source_identity ( + id integer PRIMARY KEY CHECK(id=1), installation_id uuid NOT NULL, + guard_generation uuid NOT NULL, source_record_sha256 text NOT NULL CHECK(source_record_sha256~'^[a-f0-9]{64}$'), + event_key_sha256 text NOT NULL CHECK(event_key_sha256~'^[a-f0-9]{64}$'), + status text NOT NULL CHECK(status IN ('active','quarantined')) +); +CREATE TABLE mmapp_router_guard.attempts ( + attempt_id uuid PRIMARY KEY, installation_id uuid NOT NULL, guard_generation uuid NOT NULL, + parent_attempt uuid REFERENCES mmapp_router_guard.attempts(attempt_id) ON DELETE RESTRICT, + kind text NOT NULL CHECK(kind IN ('native','application','compound')), + endpoint_mac text NOT NULL CHECK(endpoint_mac~'^[a-f0-9]{64}$'), + command_mac text NOT NULL CHECK(command_mac~'^[a-f0-9]{64}$'), + actor_mac text NOT NULL CHECK(actor_mac~'^[a-f0-9]{64}$'), + binding_mac text NOT NULL CHECK(binding_mac~'^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE TABLE mmapp_router_guard.events ( + event_id uuid PRIMARY KEY, attempt_id uuid NOT NULL REFERENCES mmapp_router_guard.attempts(attempt_id) ON DELETE RESTRICT, + sequence bigint NOT NULL UNIQUE CHECK(sequence>0), + transition text NOT NULL CHECK(transition IN ('intent','acknowledged','no-send','unknown')), + evidence_mac text NOT NULL CHECK(evidence_mac~'^[a-f0-9]{64}$'), + event_mac text NOT NULL CHECK(event_mac~'^[a-f0-9]{64}$') +); +CREATE TABLE mmapp_router_guard.head ( + id integer PRIMARY KEY CHECK(id=1), installation_id uuid NOT NULL, guard_generation uuid NOT NULL, + sequence bigint NOT NULL CHECK(sequence>=0), state text NOT NULL CHECK(state IN ('idle','pending','unknown')), + current_attempt uuid REFERENCES mmapp_router_guard.attempts(attempt_id) ON DELETE RESTRICT, + last_event uuid REFERENCES mmapp_router_guard.events(event_id) ON DELETE RESTRICT, + last_event_mac text CHECK(last_event_mac~'^[a-f0-9]{64}$'), + head_mac text NOT NULL CHECK(head_mac~'^[a-f0-9]{64}$'), + CHECK((sequence=0 AND state='idle' AND current_attempt IS NULL AND last_event IS NULL AND last_event_mac IS NULL) + OR(sequence>0 AND last_event IS NOT NULL AND last_event_mac IS NOT NULL AND + ((state='idle' AND current_attempt IS NULL) OR(state IN ('pending','unknown') AND current_attempt IS NOT NULL)))) +); +CREATE FUNCTION mmapp_router_guard.protect_rows() RETURNS trigger +LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_router_guard AS $guard$ +DECLARE event_row mmapp_router_guard.events%ROWTYPE; identity_row mmapp_router_guard.source_identity%ROWTYPE; +BEGIN + IF TG_OP IN ('DELETE','TRUNCATE') THEN RAISE EXCEPTION 'router guard rows cannot be removed' USING ERRCODE='23514'; END IF; + IF TG_TABLE_NAME='source_identity' THEN + IF TG_OP='INSERT' THEN IF NEW.status<>'active' THEN RAISE EXCEPTION 'explicit source must start active'; END IF; RETURN NEW; END IF; + IF ROW(NEW.id,NEW.installation_id,NEW.guard_generation,NEW.source_record_sha256,NEW.event_key_sha256) + IS DISTINCT FROM ROW(OLD.id,OLD.installation_id,OLD.guard_generation,OLD.source_record_sha256,OLD.event_key_sha256) + OR OLD.status<>'active' OR NEW.status<>'quarantined' THEN RAISE EXCEPTION 'source identity immutable'; END IF; + RETURN NEW; + END IF; + IF NOT EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND pid=pg_catalog.pg_backend_pid() + AND classid=0::pg_catalog.oid AND objid=8723129::pg_catalog.oid AND objsubid=1 AND mode='ExclusiveLock' AND granted) + THEN RAISE EXCEPTION 'exclusive router guard ownership required' USING ERRCODE='23514'; END IF; + SELECT * INTO STRICT identity_row FROM mmapp_router_guard.source_identity WHERE id=1 AND status='active'; + IF TG_TABLE_NAME IN ('attempts','events') THEN + IF TG_OP<>'INSERT' THEN RAISE EXCEPTION 'router guard history immutable'; END IF; + IF TG_TABLE_NAME='attempts' THEN + IF NEW.installation_id<>identity_row.installation_id OR NEW.guard_generation<>identity_row.guard_generation + THEN RAISE EXCEPTION 'foreign writer source'; END IF; + END IF; + RETURN NEW; + END IF; + IF TG_TABLE_NAME<>'head' THEN RAISE EXCEPTION 'unknown guarded relation'; END IF; + IF TG_OP='INSERT' THEN + IF NEW.sequence<>0 OR NEW.state<>'idle' OR NEW.installation_id<>identity_row.installation_id OR NEW.guard_generation<>identity_row.guard_generation + THEN RAISE EXCEPTION 'head must be explicitly owner sealed'; END IF; + RETURN NEW; + END IF; + IF ROW(NEW.id,NEW.installation_id,NEW.guard_generation) IS DISTINCT FROM ROW(OLD.id,OLD.installation_id,OLD.guard_generation) + OR NEW.sequence<>OLD.sequence+1 OR OLD.state='unknown' THEN RAISE EXCEPTION 'head history cannot rewind or resolve unknown'; END IF; + SELECT * INTO STRICT event_row FROM mmapp_router_guard.events WHERE event_id=NEW.last_event; + IF event_row.sequence<>NEW.sequence OR event_row.event_mac<>NEW.last_event_mac THEN RAISE EXCEPTION 'head/event mismatch'; END IF; + IF OLD.state='idle' THEN + IF NEW.state<>'pending' OR event_row.transition<>'intent' OR NEW.current_attempt IS DISTINCT FROM event_row.attempt_id + THEN RAISE EXCEPTION 'writer intent required'; END IF; + ELSE + IF event_row.attempt_id IS DISTINCT FROM OLD.current_attempt OR + NOT((NEW.state='idle' AND NEW.current_attempt IS NULL AND event_row.transition IN ('acknowledged','no-send')) + OR(NEW.state='unknown' AND NEW.current_attempt=OLD.current_attempt AND event_row.transition='unknown')) + THEN RAISE EXCEPTION 'invalid writer terminal transition'; END IF; + END IF; + RETURN NEW; +END; +$guard$; +CREATE TRIGGER source_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_router_guard.source_identity FOR EACH ROW EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +CREATE TRIGGER source_no_truncate BEFORE TRUNCATE ON mmapp_router_guard.source_identity FOR EACH STATEMENT EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +CREATE TRIGGER attempts_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_router_guard.attempts FOR EACH ROW EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +CREATE TRIGGER attempts_no_truncate BEFORE TRUNCATE ON mmapp_router_guard.attempts FOR EACH STATEMENT EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +CREATE TRIGGER events_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_router_guard.events FOR EACH ROW EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +CREATE TRIGGER events_no_truncate BEFORE TRUNCATE ON mmapp_router_guard.events FOR EACH STATEMENT EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +CREATE TRIGGER head_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_router_guard.head FOR EACH ROW EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +CREATE TRIGGER head_no_truncate BEFORE TRUNCATE ON mmapp_router_guard.head FOR EACH STATEMENT EXECUTE FUNCTION mmapp_router_guard.protect_rows(); +ALTER TABLE mmapp_router_guard.source_identity ENABLE ALWAYS TRIGGER source_guard; +ALTER TABLE mmapp_router_guard.source_identity ENABLE ALWAYS TRIGGER source_no_truncate; +ALTER TABLE mmapp_router_guard.attempts ENABLE ALWAYS TRIGGER attempts_guard; +ALTER TABLE mmapp_router_guard.attempts ENABLE ALWAYS TRIGGER attempts_no_truncate; +ALTER TABLE mmapp_router_guard.events ENABLE ALWAYS TRIGGER events_guard; +ALTER TABLE mmapp_router_guard.events ENABLE ALWAYS TRIGGER events_no_truncate; +ALTER TABLE mmapp_router_guard.head ENABLE ALWAYS TRIGGER head_guard; +ALTER TABLE mmapp_router_guard.head ENABLE ALWAYS TRIGGER head_no_truncate; +REVOKE ALL ON ALL TABLES IN SCHEMA mmapp_router_guard FROM PUBLIC; +REVOKE ALL ON ALL FUNCTIONS IN SCHEMA mmapp_router_guard FROM PUBLIC; +` +export const routerGuardSchemaSourceSha256 = () => createHash("sha256").update(ROUTER_GUARD_DDL).digest("hex") + +/** Fixed complete own-namespace pin. Includes functions, trigger enablement, + * constraints/indexes/columns/ACL, rejecting extra objects and foreign FKs by + * changing the signed digest. No mmapp_control object is inspected or changed. */ +export const ROUTER_GUARD_CATALOG_SQL = `SELECT + (SELECT pg_catalog.to_jsonb(n) FROM (SELECT ns.nspname,owner.rolname AS owner,ns.nspacl::pg_catalog.text AS acl FROM pg_catalog.pg_namespace ns JOIN pg_catalog.pg_roles owner ON owner.oid=ns.nspowner WHERE ns.nspname='mmapp_router_guard') n) AS namespace, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(t) ORDER BY t.name) FROM ( + SELECT c.relname AS name,c.relkind,c.relpersistence,c.relrowsecurity,c.relforcerowsecurity,c.relacl::pg_catalog.text AS acl, + o.rolname AS owner,c.reloptions, + (SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object('name',a.attname,'type',pg_catalog.format_type(a.atttypid,a.atttypmod),'acl',a.attacl::pg_catalog.text,'notNull',a.attnotnull,'identity',a.attidentity,'generated',a.attgenerated,'default',pg_catalog.pg_get_expr(d.adbin,d.adrelid)) ORDER BY a.attnum) FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum WHERE a.attrelid=c.oid AND a.attnum>0 AND NOT a.attisdropped) AS columns, + (SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object('name',k.conname,'definition',pg_catalog.pg_get_constraintdef(k.oid,true)) ORDER BY k.conname) FROM pg_catalog.pg_constraint k WHERE k.conrelid=c.oid) AS constraints, + (SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object('name',ic.relname,'definition',pg_catalog.pg_get_indexdef(i.indexrelid),'valid',i.indisvalid,'ready',i.indisready) ORDER BY ic.relname) FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class ic ON ic.oid=i.indexrelid WHERE i.indrelid=c.oid) AS indexes, + (SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object('name',tg.tgname,'enabled',tg.tgenabled,'definition',pg_catalog.pg_get_triggerdef(tg.oid,true)) ORDER BY tg.tgname) FROM pg_catalog.pg_trigger tg WHERE tg.tgrelid=c.oid) AS triggers + FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace JOIN pg_catalog.pg_roles o ON o.oid=c.relowner WHERE n.nspname='mmapp_router_guard' AND c.relkind IN ('r','p','v','m','f') LIMIT 5 + ) t),'[]'::pg_catalog.jsonb) AS tables, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.to_jsonb(f) ORDER BY f.name) FROM ( + SELECT p.proname AS name,pg_catalog.pg_get_functiondef(p.oid) AS definition,p.prosrc AS source,p.prosecdef,p.proconfig,p.proacl::pg_catalog.text AS acl,o.rolname AS owner + FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid=p.pronamespace JOIN pg_catalog.pg_roles o ON o.oid=p.proowner WHERE n.nspname='mmapp_router_guard' LIMIT 2 + ) f),'[]'::pg_catalog.jsonb) AS functions` + +export const ROUTER_GUARD_ROLE_SQL = `SELECT pg_catalog.current_database() AS database_name,SESSION_USER::pg_catalog.text AS session_role,CURRENT_USER::pg_catalog.text AS current_role, + pg_catalog.current_setting('fsync') AS fsync,pg_catalog.current_setting('full_page_writes') AS full_page_writes, + (SELECT appdb.oid::pg_catalog.int8::pg_catalog.text FROM pg_catalog.pg_database appdb WHERE appdb.datname=$2) AS application_database_oid, + pg_catalog.pg_backend_pid() AS backend_pid,d.oid::pg_catalog.int8::pg_catalog.text AS database_oid,o.rolname AS database_owner, + r.rolsuper,r.rolcreatedb,r.rolcreaterole,r.rolreplication,r.rolbypassrls,r.rolinherit, + EXISTS(SELECT 1 FROM pg_catalog.pg_auth_members m WHERE m.member=r.oid) AS memberships, + pg_catalog.has_schema_privilege(r.oid,'mmapp_router_guard','USAGE') AS schema_usage, + pg_catalog.has_schema_privilege(r.oid,'mmapp_router_guard','CREATE') AS schema_create, + COALESCE(pg_catalog.has_schema_privilege(r.oid,pg_catalog.to_regnamespace('mmapp_control')::pg_catalog.oid,'USAGE'),false) AS core_usage, + COALESCE(pg_catalog.has_schema_privilege(r.oid,pg_catalog.to_regnamespace('mmapp_control')::pg_catalog.oid,'CREATE'),false) AS core_create, + EXISTS(SELECT 1 FROM pg_catalog.pg_roles app WHERE app.rolname=$3 AND + (app.rolsuper OR app.rolcreatedb OR app.rolcreaterole OR app.rolreplication OR app.rolbypassrls OR app.oid=d.datdba + OR pg_catalog.pg_has_role(app.oid,r.oid,'MEMBER') OR pg_catalog.pg_has_role(app.oid,d.datdba,'MEMBER') + OR EXISTS(SELECT 1 FROM pg_catalog.pg_namespace ns WHERE ns.nspname='mmapp_router_guard' AND + (pg_catalog.pg_has_role(app.oid,ns.nspowner,'MEMBER') OR pg_catalog.has_schema_privilege(app.oid,ns.oid,'USAGE') OR pg_catalog.has_schema_privilege(app.oid,ns.oid,'CREATE'))) + OR EXISTS(SELECT 1 FROM pg_catalog.pg_namespace core CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(core.nspacl,pg_catalog.acldefault('n',core.nspowner))) acl WHERE core.nspname='mmapp_control' AND acl.grantee<>0 AND pg_catalog.pg_has_role(app.oid,acl.grantee,'MEMBER')) + OR COALESCE(pg_catalog.has_schema_privilege(app.oid,pg_catalog.to_regnamespace('mmapp_control')::pg_catalog.oid,'USAGE'),false) + OR COALESCE(pg_catalog.has_schema_privilege(app.oid,pg_catalog.to_regnamespace('mmapp_control')::pg_catalog.oid,'CREATE'),false))) AS application_role_unsafe, + EXISTS(SELECT 1 FROM pg_catalog.pg_namespace ns CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(ns.nspacl,pg_catalog.acldefault('n',ns.nspowner))) acl WHERE ns.nspname='mmapp_router_guard' AND acl.grantee=0 AND acl.privilege_type IN ('USAGE','CREATE')) AS public_schema_privileges, + (EXISTS(SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace ns ON ns.oid=c.relnamespace CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(c.relacl,pg_catalog.acldefault('r',c.relowner))) acl WHERE ns.nspname='mmapp_router_guard' AND c.relkind='r' AND acl.grantee=0) + OR EXISTS(SELECT 1 FROM pg_catalog.pg_proc f JOIN pg_catalog.pg_namespace ns ON ns.oid=f.pronamespace CROSS JOIN LATERAL pg_catalog.aclexplode(COALESCE(f.proacl,pg_catalog.acldefault('f',f.proowner))) acl WHERE ns.nspname='mmapp_router_guard' AND acl.grantee=0)) AS public_object_privileges, + (SELECT owner.rolname FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_roles owner ON owner.oid=n.nspowner WHERE n.nspname='mmapp_router_guard') AS schema_owner, + COALESCE((SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object('table',name,'privilege',privilege,'held',pg_catalog.has_table_privilege(r.oid,'mmapp_router_guard.'||name,privilege)) ORDER BY name,privilege) + FROM pg_catalog.unnest(ARRAY['source_identity','attempts','events','head']) names(name) CROSS JOIN pg_catalog.unnest(ARRAY['SELECT','INSERT','UPDATE','DELETE','TRUNCATE','REFERENCES','TRIGGER','SELECT WITH GRANT OPTION','INSERT WITH GRANT OPTION','UPDATE WITH GRANT OPTION']) privileges(privilege)),'[]'::pg_catalog.jsonb) AS privileges + FROM pg_catalog.pg_roles r CROSS JOIN pg_catalog.pg_database d JOIN pg_catalog.pg_roles o ON o.oid=d.datdba WHERE r.rolname=$1 AND d.datname=pg_catalog.current_database()` + +/** Validate critical fixed enforcement bodies/trigger topology as well as the + * independently signed full catalogue pin. A signed digest alone cannot turn + * an arbitrary trigger implementation into this protocol. */ +export function validateRouterGuardEnforcementCatalog(value: Record, ownerRole: string) { + const namespace = value.namespace as Record | undefined + if (!namespace || namespace.nspname !== "mmapp_router_guard" || namespace.owner !== ownerRole || !Array.isArray(value.tables) || value.tables.length !== 4 || !Array.isArray(value.functions) || value.functions.length !== 1) throw Error("Router guard catalogue shape refused") + const expectedBody = ROUTER_GUARD_DDL.match(/AS \$guard\$([\s\S]*?)\$guard\$;/)?.[1]?.trim() + const fn = value.functions[0] as Record + if (fn.name !== "protect_rows" || fn.owner !== ownerRole || fn.prosecdef !== false || typeof fn.source !== "string" || fn.source.replace(/\r\n?/g, "\n").trim() !== expectedBody || !Array.isArray(fn.proconfig) || fn.proconfig.length !== 1 || String(fn.proconfig[0]).replace(/\s/g, "") !== "search_path=pg_catalog,mmapp_router_guard") throw Error("Router guard enforcement body refused") + const names = new Set() + for (const raw of value.tables) { + const table = raw as Record, name = String(table.name), prefix = name === "source_identity" ? "source" : name + if (!["source_identity", "attempts", "events", "head"].includes(name) || names.has(name) || table.owner !== ownerRole || table.relkind !== "r" || table.relpersistence !== "p" || table.relrowsecurity !== false || table.relforcerowsecurity !== false || !Array.isArray(table.triggers)) throw Error("Router guard relation refused") + names.add(name) + const triggers = table.triggers.filter(raw => !String((raw as Record).name).startsWith("RI_ConstraintTrigger")) as Record[] + if (triggers.length !== 2 || triggers.some(trigger => trigger.enabled !== "A" || typeof trigger.definition !== "string" || !trigger.definition.includes("mmapp_router_guard.protect_rows()")) || triggers.map(trigger => trigger.name).sort().join(",") !== [prefix + "_guard", prefix + "_no_truncate"].sort().join(",")) throw Error("Router guard ALWAYS topology refused") + } +} + +export const ROUTER_GUARD_INTEGRATION_REQUIREMENTS = Object.freeze([ + "External owner provisions NOLOGIN owner + separate NOINHERIT runtime, owns namespace/tables/functions; runtime gets USAGE, source_identity SELECT, attempts/events SELECT+INSERT, head SELECT+UPDATE, protect_rows EXECUTE only; no core/public memberships/grants.", + "Signed record pins DB endpoint/OID/owner, runtime/schema-owner roles, installation/generation, complete own catalogue/schema source and distinct event key. Initial identity/head MAC and signed cutover inventory are owner-installed, never automatic empty readiness.", + "Both control/application DSNs pass the existing strict endpoint parser before pool creation. Decoded database and role identities must differ, including percent encodings; query authority overrides refuse. Guard metadata rejects an aliased local app OID; an existing application driver client verifies actual app DB/role/OID, including optional signed app-OID pin. App role cannot inherit guard/core ownership or use the guard namespace; no PUBLIC schema/object grants.", + "Order is guard GLOBAL -> application -> sorted server locks. Acquire guard at original service entry BEFORE inherited MCP app/server locks. Native child writes borrow SAME guard PID via private ALS/refcount, never acquire X below own mapping S.", + "Mapping retains its 5s maximum. Writer authority LOOKUP is bounded to 5s, but the writer itself uses the minimum ORIGINAL trusted actor expiry, command absolute deadline and original monotonic end, further capped by the captured caller cancellation/deadline. Request JSON never supplies writer authority; unchanged trusted snapshots are rechecked. No 5s writer cap that would truncate existing imports/exports.", + "Mapping holds shared session lock until ClientRequest.finish. Writer holds exclusive session lock across durable synchronous intent COMMIT, actual socket/appSQL work and terminal COMMIT. PID/loss fences abort queued HTTP; pending/unknown head outlives connection.", + "Root transport integration must supply private receipts from actual response/parser or application driver COMMIT. Arbitrary Promise/JSON/HTTP200 for async commands is not completion; unknown stays blocked without TTL/GC.", + "Root must include --exclude-schema=mmapp_router_guard in all application archive/restore allowlists as defense in depth; physically separate control DB already excludes application restore. No normal migration/core catalogue/39-migration change.", + "All actual application metadata/native writers must participate before activation; explicit old executor stop/restart cutover plus resolution of pre-protocol requests required. Full control-provider rollback remains outside this self-verification trust boundary.", +]) + +/** Exact next db/index integration API, NOT an implemented acknowledgement + * channel and NOT permission to activate this component. No public markAck. */ +export const ROUTER_GUARD_APPLICATION_POOL_HOOK = Object.freeze({ + privateInstallSignature: "installRouterGuardApplicationPoolHooks(pool: Pool, privateWriterFrameReader: () => OpaqueGuardFrame | undefined): void", + supportedExistingCallSignature: "runObservedApplication(binding, ORIGINAL trusted scope, work: () => Promise): Promise", + installPoint: "db/index.ts immediately after creating the actual application Pool and before drizzle(pool), exported pool aliases or any checkout can escape; only THAT pool is instrumented, never the guard/core pools.", + physicalObservation: "Wrap actual Pool.connect callback+Promise forms and install a once-per-physical-client wrapper around its captured original query function. Pool.query internally uses connect/client.query, so count dispatch once rather than wrapping both as independent writes.", + privateFrameChecks: "Only a live guard-exclusive ALS frame may record app writes; acquire guard before app/server locks, count outstanding queries/clients and capture source command+params under HMAC. Outside a guard frame existing behavior is unchanged until coordinated mandatory activation.", + actualAcknowledgement: "Observe the original pg client query result/error and its actual connection readyForQuery packet status I/T/E. CommandComplete alone is insufficient to distinguish ROLLBACK TO SAVEPOINT from transaction termination. ReadyForQuery I plus successful actual COMMIT/autocommit completion mints a module-private receipt; transaction T/E, in-flight query, lost client or uncertain COMMIT cannot mint one.", + noJsonIssuer: "No exported markAck/verified boolean, no callback return value as evidence, no request-selected pool/client/provider. The instrumentation itself closes over the actual installed raw driver and private WeakMap receipt registry.", + nestedAndFailure: "Every nested writer borrows the same guard PID; any failed/late/unawaited child/query latches the cohort. Unknown private driver states keep the authenticated control head pending/unknown, regardless of existing audit retention or callback success.", + legacyFacade: "Current configured runApplication(queryFacade) remains an owned-test source of real driver COMMIT receipts. The future private installed bridge avoids migrating every service to that facade, but cannot be simulated by trusting existing work() success.", + implementationStatus: "not installed, no db/index.ts edit, no production activation", +}) diff --git a/backend/src/services/router-configuration-guard.sql-fixture.ts b/backend/src/services/router-configuration-guard.sql-fixture.ts new file mode 100644 index 00000000..56843f67 --- /dev/null +++ b/backend/src/services/router-configuration-guard.sql-fixture.ts @@ -0,0 +1,206 @@ +/** Disposable owned acceptance ONLY. Never reads .env/DATABASE_URL, never + * connects outside literal loopback, never installs into an existing DB. + * Root runner supplies its newly-created isolated cluster admin URL explicitly. */ +import assert from "node:assert/strict" +import { createHash, createHmac, generateKeyPairSync, randomBytes, randomUUID, sign } from "node:crypto" +import { lstat, mkdtemp, realpath, rm, writeFile } from "node:fs/promises" +import { createServer as httpServer, request } from "node:http" +import { createConnection, createServer as tcpServer, type Socket } from "node:net" +import { tmpdir } from "node:os" +import { basename, dirname, isAbsolute, join, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { Pool } from "pg" +import { bandwidthControlEndpointSha256 } from "./bandwidth-control-source.js" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import { createConfiguredRouterGuard, routerGuardRecordSchema, type RouterGuardDependencies, type RouterGuardNativeBinding, type RouterGuardWriteBinding } from "./router-configuration-guard.js" +import { createConfiguredRouterConfigurationWriter, type RouterConfigurationWriter, type TrustedRouterWriterOriginProvider } from "./router-configuration-writer-context.js" +import { ROUTER_GUARD_CATALOG_SQL, ROUTER_GUARD_DDL, ROUTER_GUARD_LOCK, routerGuardSchemaSourceSha256 } from "./router-configuration-guard-schema.js" + +const sha = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const mac = (key: Buffer, purpose: string, value: unknown) => createHmac("sha256", key).update("mmapp-router-guard-" + purpose + "-v1:" + canonical(value)).digest("hex") +const quote = (value: string) => '"' + value.replaceAll('"', '""') + '"' +const literal = (value: string) => "'" + value.replaceAll("'", "''") + "'" +const local = (host: string) => host === "127.0.0.1" || host === "[::1]" + +/** The proxy does not replace any query/result. It drops the actual server's + * COMMIT CommandComplete and all following bytes, AFTER PostgreSQL committed. + * SSL is intentionally disabled in this disposable loopback-only fixture. */ +async function commitLossProxy(target: URL, dropCommitNumber: number) { + const sockets = new Set(); let commits = 0, dropped = 0 + const server = tcpServer(front => { + const back = createConnection({ host: target.hostname.replace(/^\[|\]$/g, ""), port: Number(target.port || 5432) }) + sockets.add(front); sockets.add(back); let buffer = Buffer.alloc(0), cut = false + front.on("data", bytes => back.write(bytes)) + back.on("data", bytes => { + if (cut) return + buffer = Buffer.concat([buffer, bytes]) + while (buffer.length >= 5) { + const length = buffer.readInt32BE(1) + if (length < 4 || length > 16777216) { front.destroy(); back.destroy(); return } + if (buffer.length < length + 1) return + const packet = buffer.subarray(0, length + 1); buffer = buffer.subarray(length + 1) + if (packet[0] === 67 && packet.subarray(5).toString("utf8") === "COMMIT\0" && ++commits === dropCommitNumber) { + cut = true; dropped++; front.destroy(); back.destroy(); return + } + front.write(packet) + } + }) + front.on("error", () => back.destroy()); back.on("error", () => front.destroy()) + front.on("close", () => { sockets.delete(front); back.destroy() }); back.on("close", () => { sockets.delete(back); front.destroy() }) + }) + await new Promise((resolveListen, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolveListen) }) + const address = server.address(); assert(address && typeof address !== "string") + return { port: address.port, dropped: () => dropped, async close() { for (const socket of sockets) socket.destroy(); await new Promise(done => server.close(() => done())) } } +} + +export function createOwnedRouterGuardFixtureFactory(adminDsn: string, nativeTransportOverride?: RouterGuardDependencies["nativeTransport"]) { + if (process.env.MMAPP_TEST_ROUTER_GUARD_OWNED !== "true" || process.env.MMAPP_TEST_ROUTER_GUARD_CLUSTER_DISPOSABLE !== "true") throw Error("Explicit disposable owned cluster required") + bandwidthControlEndpointSha256(adminDsn) + const adminUrl = new URL(adminDsn) + if (!local(adminUrl.hostname) || decodeURIComponent(adminUrl.pathname.slice(1)) !== "postgres" || adminUrl.search) throw Error("Dedicated loopback postgres admin URL required") + if (nativeTransportOverride !== undefined && typeof nativeTransportOverride !== "function") throw Error("Fixed owned transport required") + const transportOverride = nativeTransportOverride + // This factory is only called by the dedicated root-owned embedded-PG runner. + // The URL is never taken from the application's environment configuration. + return async function makeFixture(name: string) { + if (!/^[a-z-]{1,40}$/.test(name)) throw Error("Fixed acceptance scenario required") + const prefix = "rg_owned_" + randomBytes(8).toString("hex"), guardDb = prefix + "_g", appDb = prefix + "_a" + const ownerRole = prefix + "_o", runtimeRole = prefix + "_r", appRole = prefix + "_a" + const password = randomBytes(24).toString("hex"), admin = new Pool({ connectionString: adminDsn, max: 1 }) + const at = (database: string, username?: string, port?: number) => { + const url = new URL(adminUrl); url.pathname = "/" + database + if (username) { url.username = username; url.password = password } + if (port) url.port = String(port) + return url.toString() + } + const directory = await mkdtemp(join(tmpdir(), "router-guard-owned-")) + const owner = new Pool({ connectionString: at(guardDb), max: 2 }), runtime = new Pool({ connectionString: at(guardDb, runtimeRole), max: 2 }) + let appPool: Pool | undefined, guard: ReturnType | undefined + const writers = new Set() + let proxy: Awaited> | undefined, requests = 0, fault = "success", closed = false + const pids: number[] = [], eventKey = randomBytes(32), authorityEnd = Date.now() + 120000, authorityMono = performance.now() + 120000 + const actorProof = sha("owned-test-actor:" + prefix) + const native = httpServer(async (incoming, response) => { + try { + assert.equal(incoming.url, "/rest/ip/address/*1"); assert(["PATCH", "PUT", "DELETE", "POST"].includes(incoming.method!)) + assert.equal(incoming.headers.authorization, "Basic " + Buffer.from("owned:fixture").toString("base64")) + const body: Buffer[] = []; for await (const chunk of incoming) body.push(Buffer.from(chunk)) + if (incoming.method === "DELETE") assert.equal(Buffer.concat(body).length, 0) + else assert.deepEqual(JSON.parse(Buffer.concat(body).toString("utf8")), { comment: "owned-fixture" }) + requests++ + const locks = (await owner.query(`SELECT pid FROM pg_catalog.pg_locks WHERE locktype='advisory' AND objid=${ROUTER_GUARD_LOCK}::pg_catalog.oid AND classid=0::pg_catalog.oid AND mode='ExclusiveLock' AND granted`)).rows + assert.equal(locks.length, 1); pids.push(locks[0].pid) + if (fault === "drop-after-write") { incoming.socket.destroy(); return } + if (fault === "http-error") { response.writeHead(503, { "content-type": "application/json" }); response.end('{"error":"owned-http-error"}'); return } + if (fault === "malformed-json") { response.writeHead(200, { "content-type": "application/json" }); response.end("{"); return } + if (incoming.method === "DELETE") { response.writeHead(204); response.end(); return } + response.writeHead(200, { "content-type": "application/json" }); response.end(fault === "error-json" ? '{"error":400,"message":"fixture"}' : '{".id":"*1","comment":"owned-fixture"}') + } catch { incoming.socket.destroy() } + }) + const close = async () => { + if (closed) return; closed = true + for (const writer of writers) await writer.close() + await guard?.close(); await appPool?.end(); await runtime.end(); await owner.end(); await proxy?.close() + native.closeAllConnections(); if (native.listening) await new Promise(done => native.close(() => done())) + // Names are generated constants checked immediately before destructive + // cleanup, never caller paths or application configuration names. + for (const database of [guardDb, appDb]) { assert(/^rg_owned_[a-f0-9]{16}_[ga]$/.test(database)); await admin.query(`DROP DATABASE IF EXISTS ${quote(database)} WITH (FORCE)`) } + for (const role of [runtimeRole, ownerRole, appRole]) { assert(/^rg_owned_[a-f0-9]{16}_[ora]$/.test(role)); await admin.query(`DROP ROLE IF EXISTS ${quote(role)}`) } + await admin.end(); eventKey.fill(0) + const directoryInfo = await lstat(directory), actualDirectory = resolve(await realpath(directory)), actualTmp = resolve(await realpath(tmpdir())) + assert(directoryInfo.isDirectory() && !directoryInfo.isSymbolicLink(), "Owned cleanup directory must not be a symlink") + assert(isAbsolute(actualDirectory) && isAbsolute(actualTmp) && dirname(actualDirectory) === actualTmp && /^router-guard-owned-[A-Za-z0-9]+$/.test(basename(actualDirectory)), "Owned cleanup must remain a direct temporary-directory child") + await rm(actualDirectory, { recursive: true, force: true }) + } + try { + const adminIdentity = (await admin.query("SELECT CURRENT_USER AS role,r.rolsuper AS super FROM pg_catalog.pg_roles r WHERE r.rolname=CURRENT_USER")).rows[0] + assert.equal(adminIdentity.super, true) + await admin.query(`CREATE ROLE ${quote(ownerRole)} NOLOGIN NOINHERIT`) + await admin.query(`CREATE ROLE ${quote(runtimeRole)} LOGIN NOINHERIT PASSWORD ${literal(password)}`) + await admin.query(`CREATE ROLE ${quote(appRole)} LOGIN NOINHERIT PASSWORD ${literal(password)}`) + await admin.query(`CREATE DATABASE ${quote(guardDb)}`); await admin.query(`CREATE DATABASE ${quote(appDb)}`) + // No mmapp_control stub: this independent guard fixture proves only its + // own protocol. Real existing core/node catalogue compatibility is a + // separate before/after acceptance on the genuine bandwidth installer. + // Guard schema owner needs temporary CREATE solely for this explicit + // owner installation. It is revoked before the signed catalogue exists. + await owner.query(`GRANT CREATE ON DATABASE ${quote(guardDb)} TO ${quote(ownerRole)}`) + const install = await owner.connect() + try { await install.query(`SET ROLE ${quote(ownerRole)}`); await install.query(ROUTER_GUARD_DDL); await install.query("RESET ROLE") } finally { install.release(true) } + await owner.query(`REVOKE CREATE ON DATABASE ${quote(guardDb)} FROM ${quote(ownerRole)}`) + await owner.query(`GRANT USAGE ON SCHEMA mmapp_router_guard TO ${quote(runtimeRole)}; + GRANT SELECT ON ALL TABLES IN SCHEMA mmapp_router_guard TO ${quote(runtimeRole)}; + GRANT INSERT ON mmapp_router_guard.attempts,mmapp_router_guard.events TO ${quote(runtimeRole)}; + GRANT UPDATE ON mmapp_router_guard.head TO ${quote(runtimeRole)}; + GRANT EXECUTE ON FUNCTION mmapp_router_guard.protect_rows() TO ${quote(runtimeRole)}`) + const appAdmin = new Pool({ connectionString: at(appDb), max: 1 }) + try { await appAdmin.query(`CREATE TABLE public.owned_writes(id integer PRIMARY KEY); GRANT SELECT,INSERT ON public.owned_writes TO ${quote(appRole)}`) } finally { await appAdmin.end() } + if (["intent-commit-loss", "terminal-commit-loss", "application-commit-loss"].includes(name)) proxy = await commitLossProxy(adminUrl, name === "terminal-commit-loss" ? 2 : 1) + const controlDsn = at(guardDb, runtimeRole, name === "application-commit-loss" ? undefined : proxy?.port) + const applicationDsn = at(appDb, appRole, name === "application-commit-loss" ? proxy?.port : undefined) + appPool = new Pool({ connectionString: applicationDsn, max: 2 }); appPool.on("error", () => {}) + const identity = (await owner.query("SELECT d.oid::int AS oid,o.rolname AS owner FROM pg_catalog.pg_database d JOIN pg_catalog.pg_roles o ON o.oid=d.datdba WHERE d.datname=current_database()")).rows[0] + const appOid = (await admin.query("SELECT oid::int AS oid FROM pg_catalog.pg_database WHERE datname=$1", [appDb])).rows[0].oid + const catalog = (await runtime.query(ROUTER_GUARD_CATALOG_SQL)).rows[0] + const keys = generateKeyPairSync("ed25519"), publicDer = keys.publicKey.export({ type: "spki", format: "der" }) + const pin = routerGuardRecordSchema.parse({ format: "mmapp-router-guard-source-v1", installationId: randomUUID(), guardGeneration: randomUUID(), databaseName: guardDb, databaseOid: identity.oid, databaseOwner: identity.owner, runtimeRole, ownerRole, applicationDatabaseOid: appOid, + endpointSha256: bandwidthControlEndpointSha256(controlDsn), schemaSourceSha256: routerGuardSchemaSourceSha256(), catalogSha256: sha(canonical(catalog)), eventKeySha256: sha(eventKey), cutoverEvidenceSha256: sha("owned empty newly-created DB, no old executors:" + prefix), previousExecutorsQuiesced: true }) + const payload = Buffer.from(canonical(pin)), signed = { purpose: "mmapp-router-guard-source-v1", payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") } + const bootstrap = await owner.connect() + try { + await bootstrap.query(`SELECT pg_catalog.pg_advisory_lock(${ROUTER_GUARD_LOCK})`) + await bootstrap.query("INSERT INTO mmapp_router_guard.source_identity VALUES(1,$1,$2,$3,$4,'active')", [pin.installationId, pin.guardGeneration, sha(payload), pin.eventKeySha256]) + const head = { installation_id: pin.installationId, guard_generation: pin.guardGeneration, sequence: "0", state: "idle", current_attempt: null, last_event: null, last_event_mac: null } + await bootstrap.query("INSERT INTO mmapp_router_guard.head VALUES(1,$1,$2,0,'idle',NULL,NULL,NULL,$3)", [pin.installationId, pin.guardGeneration, mac(eventKey, "head", head)]) + } finally { await bootstrap.query(`SELECT pg_catalog.pg_advisory_unlock(${ROUTER_GUARD_LOCK})`); bootstrap.release(true) } + const sourceFile = resolve(directory, "source.json"), trustKeyFile = resolve(directory, "issuer.pem"), eventKeyFile = resolve(directory, "event.key") + await writeFile(sourceFile, JSON.stringify(signed)); await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" })); await writeFile(eventKeyFile, eventKey) + await new Promise((done, reject) => { native.once("error", reject); native.listen(0, "127.0.0.1", done) }) + const address = native.address(); assert(address && typeof address !== "string") + const nativeBinding: RouterGuardNativeBinding = { kind: "native", managedServerId: 71, method: name === "async-post" ? "POST" : "PATCH", requestPath: "/rest/ip/address/*1", endpoint: { host: "127.0.0.1", port: address.port, username: "owned", password: "fixture", useSsl: false, verifySsl: true }, command: { comment: "owned-fixture" }, actorProof } + const applicationBinding: RouterGuardWriteBinding = { kind: "application", endpoint: { database: appDb }, command: { operation: "owned insert" }, actorProof } + const guardConfig = { databaseUrl: controlDsn, applicationDatabaseUrl: applicationDsn, sourceFile, trustKeyFile, trustKeySha256: sha(publicDer), eventKeyFile } + const nativeTransport: RouterGuardDependencies["nativeTransport"] = transportOverride ?? (async (binding, _scope, bind) => { + if (fault === "json-without-request") return { claimedAck: true } + return new Promise((done, reject) => { + const outgoing = request({ host: binding.endpoint.host, port: binding.endpoint.port, path: binding.requestPath, method: binding.method, auth: binding.endpoint.username + ":" + binding.endpoint.password, agent: false }, response => { + const chunks: Buffer[] = []; response.on("data", (chunk: Buffer) => chunks.push(chunk)); response.once("error", reject); response.once("aborted", () => reject(Error("actual HTTP aborted"))) + response.once("end", () => { try { assert(response.complete); assert.equal(response.statusCode, 200); const body: unknown = JSON.parse(Buffer.concat(chunks).toString("utf8")); if (body && typeof body === "object" && "error" in body) throw Error("actual native parser error"); done(body) } catch (error) { reject(error) } }) + }) + outgoing.once("error", reject); bind(outgoing, () => outgoing.end(JSON.stringify(binding.command))) + }) + }) + const build = () => createConfiguredRouterGuard(guardConfig, { + applicationPool: appPool!, assertNoLowerLockContext() {}, + async authorize(binding, scope) { assert(scope.deadlineAt > Date.now()); if (binding) assert.equal(binding.actorProof, actorProof) }, + async readOriginalWriterAuthority(binding) { assert.equal(binding.actorProof, actorProof); return { actorProof, actorExpiresAt: authorityEnd, commandDeadlineAt: authorityEnd, monotonicEnd: authorityMono } }, + nativeTransport, + }) + guard = build() + return { guard, owner, runtime, nativeBinding, applicationBinding, applicationCommitQuery: "INSERT INTO public.owned_writes VALUES(1)", + /** Test-only opaque REST origin, not real REST authentication. Config, + * pools, transport and authority remain private fixed fixture state. */ + createWriterForFixture() { + const issued = new WeakSet() + const original = Object.freeze({ binding: { kind: "compound" as const, endpoint: { database: appDb, serverIds: [71] }, command: { operation: "owned outer writer" }, actorProof }, + authority: { actorProof, actorExpiresAt: authorityEnd, commandDeadlineAt: authorityEnd, monotonicEnd: authorityMono }, serverIds: [71] }) + const rest: TrustedRouterWriterOriginProvider = { async capture() { const token = Object.freeze(Object.create(null)) as object; issued.add(token); return token }, + async inspect(token) { assert(token && typeof token === "object" && issued.has(token)); return original } } + const writer = createConfiguredRouterConfigurationWriter(guardConfig, { applicationPool: appPool!, nativeTransport, assertNoLowerLockContext() {}, rest }) + writers.add(writer); return writer + }, + setNativeFault(value: string) { fault = value }, nativeRequests: () => requests, commitPacketsDropped: () => proxy?.dropped() ?? 0, + async restart() { await guard!.close(); guard = build(); return guard }, + async observedGuardPids() { return [...pids] }, + async sampleGuardPid() { const rows = (await owner.query(`SELECT pid FROM pg_catalog.pg_locks WHERE locktype='advisory' AND objid=${ROUTER_GUARD_LOCK}::pg_catalog.oid AND classid=0::pg_catalog.oid AND mode='ExclusiveLock' AND granted`)).rows; assert.equal(rows.length, 1); pids.push(rows[0].pid); return rows[0].pid as number }, + async applicationRows() { return (await appPool!.query("SELECT id FROM public.owned_writes")).rows.length }, + async assertJournalMacs() { + const head = (await owner.query("SELECT installation_id::text,guard_generation::text,sequence::text,state,current_attempt::text,last_event::text,last_event_mac,head_mac FROM mmapp_router_guard.head WHERE id=1")).rows[0] + const { head_mac, ...body } = head; assert.equal(head_mac, mac(eventKey, "head", body)) + const events = (await owner.query("SELECT event_id::text,attempt_id::text,sequence::text,transition,evidence_mac,event_mac FROM mmapp_router_guard.events ORDER BY sequence")).rows + for (const event of events) { const { event_mac, ...eventBody } = event; assert.equal(event_mac, mac(eventKey, "event", eventBody)) } + if (events.length) { assert.equal(head.last_event, events.at(-1)!.event_id); assert.equal(head.last_event_mac, events.at(-1)!.event_mac); assert.equal(head.sequence, events.at(-1)!.sequence) } + }, close } + } catch (error) { await close(); throw error } + } +} diff --git a/backend/src/services/router-configuration-guard.sql-helper.ts b/backend/src/services/router-configuration-guard.sql-helper.ts new file mode 100644 index 00000000..05129c52 --- /dev/null +++ b/backend/src/services/router-configuration-guard.sql-helper.ts @@ -0,0 +1,162 @@ +/** Owned PostgreSQL/localhost acceptance; execution is explicitly root-owned. + * The factory must explicitly provision each isolated source/head/key/role; + * this helper never initializes a source merely because its tables are empty. */ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import type { Pool } from "pg" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import { BANDWIDTH_CONTROL_ROLE_SQL } from "./bandwidth-control-role.js" +import { BANDWIDTH_CONTROL_CATALOG_SQL } from "./bandwidth-control-catalog.js" +import { BANDWIDTH_NODE_REGISTRY_ROLE_SQL } from "./bandwidth-node-registry-role.js" +import { BANDWIDTH_NODE_REGISTRY_CATALOG_SQL, BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL, BANDWIDTH_PEER_REGISTRY_INCOMING_SQL } from "./bandwidth-node-registry-catalog.js" +import { ROUTER_GUARD_LOCK, ROUTER_GUARD_ARCHIVE_EXCLUSION, ROUTER_GUARD_INTEGRATION_REQUIREMENTS } from "./router-configuration-guard-schema.js" +import { RouterGuardRefused, RouterGuardUnknown, validateRouterGuardDatabaseIsolation, type createConfiguredRouterGuard, type RouterGuardWriteBinding, type RouterGuardNativeBinding } from "./router-configuration-guard.js" + +export const ROUTER_GUARD_ACCEPTANCE_STATUS = Object.freeze({ wired: false, actualPgRun: false, appMigrationsChanged: false, unknownGc: false, + archiveExclusionRequired: ROUTER_GUARD_ARCHIVE_EXCLUSION, integrationRequirements: ROUTER_GUARD_INTEGRATION_REQUIREMENTS }) +type Guard = ReturnType +type Fixture = { guard: Guard; owner: Pool; runtime: Pool; nativeBinding: RouterGuardNativeBinding; applicationBinding: RouterGuardWriteBinding; + /** These setters affect the fixed LOCALHOST native fixture or actual owned + * PG backend. They cannot mint private terminal receipts. */ + setNativeFault(fault: "success" | "drop-after-write" | "json-without-request" | "async-post" | "error-json"): void + nativeRequests(): number; applicationCommitQuery: string; close(): Promise + restart(): Promise; sampleGuardPid(): Promise; observedGuardPids(): Promise; + assertJournalMacs(): Promise; applicationRows(): Promise; commitPacketsDropped(): number } +const scope = () => ({ deadlineAt: Date.now() + 20000 }) // mapping retains its internal original 5s cap +function owned() { if (process.env.MMAPP_TEST_ROUTER_GUARD_OWNED !== "true") throw Error("Explicit owned router-guard fixture required") } +async function head(fixture: Fixture) { return (await fixture.owner.query("SELECT state,sequence::pg_catalog.text,current_attempt::pg_catalog.text FROM mmapp_router_guard.head WHERE id=1")).rows[0] } +/** Same pure constructor validator; no pool, filesystem or network is touched. */ +export function assertRouterGuardDsnIsolationRefusals() { + const control = "postgresql://guard_runtime:fixture@127.0.0.1/router_control", app = "postgresql://app_runtime:fixture@127.0.0.1/application" + assert.equal(validateRouterGuardDatabaseIsolation(control, app).runtimeRole, "guard_runtime") + for (const [guardDsn, appDsn] of [ + ["postgresql://guard_runtime:fixture@127.0.0.1/%61pplication", app], + [control, "postgresql://app_runtime:fixture@127.0.0.1/%72outer_control"], + [control, "postgresql://%67uard_runtime:fixture@127.0.0.1/application"], + ["postgresql://%61pp_runtime:fixture@127.0.0.1/router_control", app], + [control + "?user=app_runtime", app], [control, app + "?user=guard_runtime"], + [control, app + "?database=router_control"], [control, app + "?sslmode=require&sslmode=disable"], + ]) assert.throws(() => validateRouterGuardDatabaseIsolation(guardDsn, appDsn), RouterGuardRefused) + return { cases: 8, realDatabaseCalls: 0 } +} + +/** Every scenario gets a new OWNER-provisioned isolated fixture. In particular, + * no test cleans pending/unknown by DELETE, TRUNCATE, TTL or signature replay. */ +export async function acceptOwnedRouterGuard(makeFixture: (name: string) => Promise) { + owned(); assertRouterGuardDsnIsolationRefusals(); const checks: string[] = ["constructor rejects encoded same DB/role and query authority overrides before any checkout"] + for (const scenario of ["shared-exclusion", "physical-commit", "nested-same-pid", "lost-native-ack", "forged-json", "async-post", "error-json", "unknown-persist", "head-forgery", "forged-terminal-idle", "no-send", "shared-cannot-write", "sql-semicolon-control", "sql-comment-control", "sql-caught-error", "intent-commit-loss", "application-commit-loss", "terminal-commit-loss"] as const) { + const fixture = await makeFixture(scenario) + try { + if (scenario === "shared-exclusion") { + const mapping = await fixture.guard.acquireMapping(scope()) + try { await mapping.verify(); assert.equal((await fixture.owner.query(`SELECT pg_catalog.pg_try_advisory_lock(${ROUTER_GUARD_LOCK}) AS acquired`)).rows[0].acquired, false) } + finally { mapping.close() } + checks.push("actual held shared mapping ownership excludes the global writer on another backend") + } else if (scenario === "physical-commit") { + await fixture.guard.runApplication(fixture.applicationBinding, scope(), async client => { await client.query(fixture.applicationCommitQuery) }) + assert.equal((await head(fixture)).state, "idle") + const mapping = await fixture.guard.acquireMapping(scope()); await mapping.verify(); mapping.close() + checks.push("actual application driver COMMIT is the private terminal source; journal and authenticated head become idle") + } else if (scenario === "nested-same-pid") { + await fixture.guard.runCompound({ ...fixture.applicationBinding, kind: "compound" }, scope(), async () => { + const originalPid = await fixture.sampleGuardPid() + await fixture.guard.runApplication(fixture.applicationBinding, scope(), async client => { await client.query(fixture.applicationCommitQuery); assert.equal(await fixture.sampleGuardPid(), originalPid); await fixture.guard.runNative(fixture.nativeBinding, scope()); assert.equal(await fixture.sampleGuardPid(), originalPid) }) + }) + assert.equal(new Set(await fixture.observedGuardPids()).size, 1) + assert.equal(fixture.nativeRequests(), 1); assert.equal((await head(fixture)).state, "idle") + const attempts = (await fixture.owner.query("SELECT parent_attempt IS NULL AS root FROM mmapp_router_guard.attempts")).rows + assert.equal(attempts.filter(row => row.root).length, 1); assert.equal(attempts.length, 3) + checks.push("nested compound/application/native work uses one physical exclusive guard lease and records child bindings") + } else if (["lost-native-ack", "forged-json", "async-post", "error-json"].includes(scenario)) { + fixture.setNativeFault(scenario === "lost-native-ack" ? "drop-after-write" : scenario === "forged-json" ? "json-without-request" : scenario === "error-json" ? "error-json" : "async-post") + await assert.rejects(fixture.guard.runNative(fixture.nativeBinding, scope()), RouterGuardUnknown) + assert.equal((await head(fixture)).state, "pending"); await assert.rejects(fixture.guard.acquireMapping(scope())) + if (scenario === "forged-json") assert.equal(fixture.nativeRequests(), 0) + checks.push(scenario + ": no synthetic terminal receipt, unresolved head blocks mapping") + } else if (scenario === "unknown-persist") { + fixture.setNativeFault("drop-after-write"); await assert.rejects(fixture.guard.runNative(fixture.nativeBinding, scope())) + await fixture.guard.close() + assert.equal((await head(fixture)).state, "pending") + assert.equal((await fixture.owner.query("SELECT count(*)::pg_catalog.int4 AS count FROM mmapp_router_guard.attempts")).rows[0].count, 1) + const restarted = await fixture.restart(); await assert.rejects(restarted.acquireMapping(scope())) + checks.push("guard restart/close never erases unresolved WAL-backed intent; separate configured source must refuse it") + } else if (scenario === "head-forgery") { + const client = await fixture.owner.connect() + try { await client.query(`SELECT pg_catalog.pg_advisory_lock(${ROUTER_GUARD_LOCK})`); await assert.rejects(client.query("UPDATE mmapp_router_guard.head SET sequence=sequence-1,state='idle',current_attempt=NULL WHERE id=1")) } + finally { try { await client.query(`SELECT pg_catalog.pg_advisory_unlock(${ROUTER_GUARD_LOCK})`) } finally { client.release(true) } } + checks.push("ALWAYS trigger refuses authenticated-head rewind instead of trusting a filtered empty pending query") + } else if (scenario === "forged-terminal-idle") { + // Create a REAL durable pending intent by losing a REAL native ACK. + fixture.setNativeFault("drop-after-write") + await assert.rejects(fixture.guard.runNative(fixture.nativeBinding, scope()), RouterGuardUnknown) + const client = await fixture.runtime.connect() + try { + await client.query(`SELECT pg_catalog.pg_advisory_lock(${ROUTER_GUARD_LOCK})`) + const before = (await client.query("SELECT sequence::pg_catalog.text,current_attempt::pg_catalog.text,last_event::pg_catalog.text FROM mmapp_router_guard.head WHERE id=1")).rows[0] + const originalEvent = (await client.query("SELECT sequence::pg_catalog.text,transition,attempt_id::pg_catalog.text FROM mmapp_router_guard.events WHERE event_id=$1", [before.last_event])).rows[0] + assert.equal(originalEvent.transition, "intent"); assert.equal(originalEvent.sequence, before.sequence); assert.equal(originalEvent.attempt_id, before.current_attempt) + const sequence = String(Number(before.sequence) + 1), fakeEventId = randomUUID(), invalidMac = "f".repeat(64) + await client.query("INSERT INTO mmapp_router_guard.events(event_id,attempt_id,sequence,transition,evidence_mac,event_mac) VALUES($1,$2,$3,'acknowledged',$4,$4)", [fakeEventId, before.current_attempt, sequence, invalidMac]) + const changed = await client.query("UPDATE mmapp_router_guard.head SET sequence=$1,state='idle',current_attempt=NULL,last_event=$2,last_event_mac=$3,head_mac=$3 WHERE id=1 AND sequence=$4 RETURNING id", [sequence, fakeEventId, invalidMac, before.sequence]) + assert.equal(changed.rows.length, 1) + const after = (await client.query("SELECT state,sequence::pg_catalog.text,last_event::pg_catalog.text,last_event_mac FROM mmapp_router_guard.head WHERE id=1")).rows[0] + assert.equal(after.state, "idle"); assert.equal(after.sequence, sequence); assert.equal(after.last_event, fakeEventId); assert.equal(after.last_event_mac, invalidMac) + } finally { try { await client.query(`SELECT pg_catalog.pg_advisory_unlock(${ROUTER_GUARD_LOCK})`) } finally { client.release(true) } } + // A filtered pending query is now empty. Source MUST nevertheless + // authenticate and refuse the forged idle head and final event. + await assert.rejects(fixture.guard.acquireMapping(scope())) + checks.push("genuine pending -> DB-role valid-shape forged ACK/idle with preserved sequence/event binding refuses mapping because MAC is invalid") + } else if (scenario === "no-send") { + const before = fixture.nativeRequests() + await fixture.guard.runCompound({ ...fixture.applicationBinding, kind: "compound" }, scope(), async () => ({ claimedAck: true })) + assert.equal(fixture.nativeRequests(), before); assert.equal((await head(fixture)).state, "idle") + assert.equal((await fixture.owner.query("SELECT transition FROM mmapp_router_guard.events ORDER BY sequence DESC LIMIT 1")).rows[0].transition, "no-send") + checks.push("no-send derives from zero private transport/driver activity; returned JSON never becomes an acknowledged receipt") + } else if (scenario.startsWith("sql-")) { + const text = scenario === "sql-semicolon-control" ? fixture.applicationCommitQuery + "; COMMIT; BEGIN" : scenario === "sql-comment-control" ? "/* hidden control */\nCOMMIT" : "SELECT 1/0" + await assert.rejects(fixture.guard.runApplication(fixture.applicationBinding, scope(), async client => { await client.query(text).catch(() => {}) }), RouterGuardUnknown) + assert.equal((await head(fixture)).state, "pending"); await assert.rejects(fixture.guard.acquireMapping(scope())) + checks.push(scenario + ": actual driver command results/ReadyForQuery refuse transaction escape, even when callback catches error") + } else if (scenario.endsWith("commit-loss")) { + if (scenario === "application-commit-loss") await assert.rejects(fixture.guard.runApplication(fixture.applicationBinding, scope(), async client => { await client.query(fixture.applicationCommitQuery) }), RouterGuardUnknown) + else await assert.rejects(fixture.guard.runNative(fixture.nativeBinding, scope()), RouterGuardUnknown) + assert.equal(fixture.commitPacketsDropped(), 1) + if (scenario === "terminal-commit-loss") { + // Actual terminal WAL may have committed although its ACK was lost. + // Private completed native receipt already exists: authentic idle is + // safe, but this caller MUST still get unknown and never replay. + assert.equal((await head(fixture)).state, "idle"); assert.equal(fixture.nativeRequests(), 1) + const mapping = await fixture.guard.acquireMapping(scope()); await mapping.verify(); mapping.close() + } else { + assert.equal((await head(fixture)).state, "pending"); await assert.rejects(fixture.guard.acquireMapping(scope())) + if (scenario === "intent-commit-loss") assert.equal(fixture.nativeRequests(), 0) + else assert.equal(await fixture.applicationRows(), 1) + } + checks.push(scenario + ": real server COMMIT followed by wire ACK loss; no driver-result replacement or automatic replay") + } else { + const mapping = await fixture.guard.acquireMapping(scope()) + try { await assert.rejects(mapping.run(() => fixture.guard.runNative(fixture.nativeBinding, scope())), RouterGuardRefused) } + finally { mapping.close() } + checks.push("own mapping-shared context cannot upgrade itself into an exclusive writer or borrow a new pool client") + } + if (scenario !== "forged-terminal-idle") await fixture.assertJournalMacs() + } finally { await fixture.close() } + } + return { checks, actualPgRun: true, activation: false, realChrCalls: 0 } +} + +/** Root captures these on existing v1/v2 runtime roles BEFORE and AFTER owner + * installation of the independent guard namespace in the SAME control DB. + * Exact digest equality is the compatibility proof; this function itself is + * not a claim that the future owned acceptance has already been executed. */ +export async function captureExistingBandwidthCompatibility(input: { core: Pool; nodes: Pool; databaseName: string; coreRuntime: string; coreOwner: string; nodeRuntime: string; nodeOwner: string; v2: boolean }) { + owned(); const values = [input.databaseName, input.coreRuntime, input.coreOwner] + const snapshots = [ + (await input.core.query(BANDWIDTH_CONTROL_ROLE_SQL, values)).rows, + (await input.core.query(BANDWIDTH_CONTROL_CATALOG_SQL)).rows, + (await input.nodes.query(BANDWIDTH_NODE_REGISTRY_ROLE_SQL, [input.databaseName, input.nodeRuntime, input.nodeOwner])).rows, + (await input.nodes.query(input.v2 ? BANDWIDTH_PEER_MANAGED_REGISTRY_CATALOG_SQL : BANDWIDTH_NODE_REGISTRY_CATALOG_SQL)).rows, + input.v2 ? (await input.nodes.query(BANDWIDTH_PEER_REGISTRY_INCOMING_SQL)).rows : [], + ] + return createHash("sha256").update(canonical(snapshots)).digest("hex") +} diff --git a/backend/src/services/router-configuration-guard.sql.test.ts b/backend/src/services/router-configuration-guard.sql.test.ts new file mode 100644 index 00000000..3b7ea563 --- /dev/null +++ b/backend/src/services/router-configuration-guard.sql.test.ts @@ -0,0 +1,27 @@ +/** Invoked ONLY by the dedicated root-owned embedded-PG profile. No fallback + * to application DATABASE_URL, .env or production/control configuration. */ +import { acceptOwnedRouterGuard } from "./router-configuration-guard.sql-helper.js" +import { createOwnedRouterGuardFixtureFactory } from "./router-configuration-guard.sql-fixture.js" + +async function main() { + const admin = process.env.MMAPP_TEST_ROUTER_GUARD_ADMIN_URL + if (!admin) throw Error("Missing explicit disposable owned-cluster admin URL") + const make = createOwnedRouterGuardFixtureFactory(admin) + const report = await acceptOwnedRouterGuard(async name => { + process.stdout.write(JSON.stringify({ scenario: name, phase: "owner-provision" }) + "\n") + const fixture = await make(name) + process.stdout.write(JSON.stringify({ scenario: name, phase: "actual-acceptance" }) + "\n") + return fixture + }) + process.stdout.write(JSON.stringify(report, null, 2) + "\n") + const { acceptOwnedRouterConfigurationWriter } = await import("./router-configuration-writer-context.sql-helper.js") + console.log("Owned router writer assembly:", JSON.stringify(await acceptOwnedRouterConfigurationWriter(make))) + const { acceptOwnedRouterConfigurationNativeTransport } = await import("./router-configuration-native-transport.sql-helper.js") + console.log("Owned fixed router native transport:", JSON.stringify(await acceptOwnedRouterConfigurationNativeTransport(admin))) +} +void main().catch(error => { + const name = error instanceof Error && /^[A-Za-z]+$/.test(error.name) ? error.name : "Error" + const code: unknown = error && typeof error === "object" ? error.code : undefined + process.stderr.write(JSON.stringify({ status: "refused", error: name, sqlState: typeof code === "string" && /^[0-9A-Z]{5}$/.test(code) ? code : undefined }) + "\n") + process.exitCode = 1 +}) diff --git a/backend/src/services/router-configuration-guard.ts b/backend/src/services/router-configuration-guard.ts new file mode 100644 index 00000000..382bfb2e --- /dev/null +++ b/backend/src/services/router-configuration-guard.ts @@ -0,0 +1,422 @@ +/** Explicitly configured UNACTIVATED component. Own control-role pool; no core-source pool, + * application migration, owner bootstrap, empty-store activation or unknown GC. */ +import { AsyncLocalStorage } from "node:async_hooks" +import { createHash, createHmac, createPublicKey, randomUUID, verify } from "node:crypto" +import { ClientRequest } from "node:http" +import type { EventEmitter } from "node:events" +import { isIP } from "node:net" +import { isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { TLSSocket } from "node:tls" +import { Pool, type PoolClient, type QueryConfig } from "pg" +import { z } from "zod" +import { readBandwidthControlFile, bandwidthControlEndpointSha256 } from "./bandwidth-control-source.js" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { ROUTER_GUARD_LOCK, ROUTER_GUARD_ROLE_SQL, ROUTER_GUARD_CATALOG_SQL, routerGuardSchemaSourceSha256, validateRouterGuardEnforcementCatalog } from "./router-configuration-guard-schema.js" + +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid(), role = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]{0,62}$/) +export const routerGuardRecordSchema = z.object({ format: z.literal("mmapp-router-guard-source-v1"), installationId: uuid, guardGeneration: uuid, + databaseName: z.string().min(1).max(63), databaseOid: z.number().int().positive().safe(), databaseOwner: role, runtimeRole: role, ownerRole: role, + applicationDatabaseOid: z.number().int().positive().safe().optional(), + endpointSha256: hash, schemaSourceSha256: hash, catalogSha256: hash, eventKeySha256: hash, + /** Explicit reviewed cutover evidence, never a statement inferred from rows. */ + cutoverEvidenceSha256: hash, previousExecutorsQuiesced: z.literal(true) }).strict() +type RecordPin = z.infer +const envelope = z.object({ purpose: z.literal("mmapp-router-guard-source-v1"), payload: z.string().max(32768).regex(/^[A-Za-z0-9_-]+$/), signature: z.string().length(86).regex(/^[A-Za-z0-9_-]+$/) }).strict() +export interface RouterGuardConfig { databaseUrl: string; applicationDatabaseUrl: string; sourceFile: string; trustKeyFile: string; trustKeySha256: string; eventKeyFile: string } +export interface RouterGuardWriteBinding { kind: "native" | "application" | "compound"; endpoint: unknown; command: unknown; actorProof: string } +export interface RouterGuardNativeBinding extends RouterGuardWriteBinding { + kind: "native"; managedServerId: number; method: "POST" | "PUT" | "PATCH" | "DELETE"; requestPath: string + endpoint: { host: string; port: number; useSsl: boolean; verifySsl: boolean; username: string; password: string } +} +export interface RouterGuardWriterAuthority { + readonly actorProof: string + readonly actorExpiresAt: number + readonly commandDeadlineAt: number + /** Original process-local bound supplied by the trusted original-context + * provider, not recomputed from request JSON or a renewed grant. */ + readonly monotonicEnd: number +} +export type RouterGuardNativeBinder = (request: ClientRequest, sendBytesOnce: () => void) => void +type NativeInvocation = { active: boolean; used: boolean; scope: Readonly; bind: RouterGuardNativeBinder; live(): void; current(): boolean } +const nativeInvocations = new WeakMap() +/** Fixed transport bridge only. Plain bindings, copied objects, a different + * binder/signal or a retired cohort cannot authorize raw HTTP construction. + * The kernel issues this private identity only after the real intent COMMIT. */ +export function consumeRouterGuardNativeInvocation(binding: Readonly, scope: Readonly, bind: RouterGuardNativeBinder): void { + const invocation = binding && typeof binding === "object" ? nativeInvocations.get(binding) : undefined + if (!invocation || !invocation.active || invocation.used || invocation.scope !== scope || invocation.bind !== bind || !invocation.current()) throw new RouterGuardRefused() + invocation.live(); invocation.used = true +} +export interface RouterGuardDependencies { + /** Must reject acquisition below pre-existing app/server locks. Root runner + * integration acquires guard first, then borrows its SAME PID via ALS. */ + assertNoLowerLockContext(): void + authorize(binding: Readonly | undefined, scope: Readonly): Promise + readOriginalWriterAuthority(binding: Readonly, lookupScope: Readonly): Promise> + /** Fixed trusted native parser/transport, captured at construction. Request + * JSON cannot supply this callback or manufacture a terminal receipt. */ + nativeTransport(binding: Readonly, scope: Readonly, bind: RouterGuardNativeBinder): Promise + applicationPool: Pool +} +export class RouterGuardRefused extends Error { constructor() { super("Router guard refused") } } +export class RouterGuardUnknown extends Error { readonly automaticReplay = false; constructor() { super("Router writer outcome unknown") } } +const fail = (): never => { throw new RouterGuardRefused() } +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +const mac = (key: Buffer, purpose: string, value: unknown) => createHmac("sha256", key).update("mmapp-router-guard-" + purpose + "-v1:" + canonical(value)).digest("hex") +/** Constructor-only pure isolation validation. Parse BOTH DSNs with the same + * strict override policy before creating a pool; compare decoded DB/role names. */ +export function validateRouterGuardDatabaseIsolation(controlDsn: string, applicationDsn: string) { + try { + const controlEndpointSha256 = bandwidthControlEndpointSha256(controlDsn), applicationEndpointSha256 = bandwidthControlEndpointSha256(applicationDsn) + const control = new URL(controlDsn), application = new URL(applicationDsn) + const databaseName = decodeURIComponent(control.pathname.slice(1)), applicationDatabaseName = decodeURIComponent(application.pathname.slice(1)) + const runtimeRole = decodeURIComponent(control.username), applicationRole = decodeURIComponent(application.username) + const validDatabase = (name: string) => name.length > 0 && Buffer.byteLength(name) <= 63 && !/[\u0000-\u001f\u007f]/.test(name) + if (!validDatabase(databaseName) || !validDatabase(applicationDatabaseName) || databaseName === applicationDatabaseName || runtimeRole === applicationRole || !role.safeParse(runtimeRole).success || !role.safeParse(applicationRole).success) fail() + return Object.freeze({ databaseName, applicationDatabaseName, runtimeRole, applicationRole, controlEndpointSha256, applicationEndpointSha256 }) + } catch { return fail() } +} +function capture(raw: unknown): unknown { + const seen = new Set(); let count = 0 + const copy = (value: unknown, depth = 0): unknown => { + if (++count > 20000 || depth > 16) return fail() + if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return value + if (!value || typeof value !== "object" || seen.has(value)) return fail() + const array = Array.isArray(value), fields = Object.getOwnPropertyDescriptors(value) + if (Object.getPrototypeOf(value) !== (array ? Array.prototype : Object.prototype) || Reflect.ownKeys(value).some(key => typeof key !== "string")) fail() + seen.add(value) + try { + if (array) { const length = fields.length.value as number; if (length > 10000 || Object.keys(fields).length !== length + 1) fail(); return Object.freeze(Array.from({ length }, (_, index) => { const field = fields[String(index)]; if (!field || !("value" in field) || !field.enumerable) return fail(); return copy(field.value, depth + 1) })) } + const result: { [key: string]: unknown } = {} + for (const [key, field] of Object.entries(fields)) { if (!("value" in field) || !field.enumerable || key === "__proto__") fail(); Object.defineProperty(result, key, { value: copy(field.value, depth + 1), enumerable: true }) } + return Object.freeze(result) + } finally { seen.delete(value) } + } + const value = copy(raw); if (Buffer.byteLength(JSON.stringify(value)) > 1048576) fail(); return value +} +function captureScope(raw: BandwidthScope): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype) return fail() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).length !== Object.keys(fields).length || Object.keys(fields).some(key => !["deadlineAt", "signal"].includes(key)) || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) fail() + const deadlineAt = fields.deadlineAt?.value as number, signal = fields.signal?.value as AbortSignal | undefined + if (!Number.isSafeInteger(deadlineAt) || signal !== undefined && !(signal instanceof AbortSignal)) fail() + return Object.freeze({ deadlineAt, signal }) +} +type Head = { installation_id: string; guard_generation: string; sequence: string; state: "idle" | "pending" | "unknown"; current_attempt: string | null; last_event: string | null; last_event_mac: string | null; head_mac: string } +const HEAD_SQL = `SELECT installation_id::pg_catalog.text,guard_generation::pg_catalog.text,sequence::pg_catalog.text,state,current_attempt::pg_catalog.text,last_event::pg_catalog.text,last_event_mac,head_mac FROM mmapp_router_guard.head WHERE id=1` +const headBody = ({ head_mac: _mac, ...head }: Head) => head +type Lease = { client: PoolClient; scope: Readonly; mode: "shared" | "exclusive"; pid: number; pin: RecordPin; sourceHash: string; active: boolean; end: number; refs: number; failed: boolean; activity: number; rootAttempt?: string; head?: Head; receiptMacs: string[]; writerAuthority?: Readonly; originalBinding?: Readonly; stop(): void; live(): void } +type Frame = { lease: Lease; active: boolean; child: boolean } + +export function createConfiguredRouterGuard(configInput: RouterGuardConfig, dependencies: RouterGuardDependencies) { + const config = capture(configInput) as RouterGuardConfig, authorize = dependencies.authorize.bind(dependencies), native = dependencies.nativeTransport.bind(dependencies), lower = dependencies.assertNoLowerLockContext.bind(dependencies), appPool = dependencies.applicationPool + const readWriterAuthority = dependencies.readOriginalWriterAuthority.bind(dependencies) + const authoritySchema = z.object({ actorProof: hash, actorExpiresAt: z.number().int().positive().safe(), commandDeadlineAt: z.number().int().positive().safe(), monotonicEnd: z.number().finite().nonnegative() }).strict() + async function writerAuthority(binding: Readonly, scope: Readonly) { + const value = Object.freeze(authoritySchema.parse(capture(await readWriterAuthority(binding, scope)))) + if (value.actorProof !== binding.actorProof || Date.now() >= Math.min(value.actorExpiresAt, value.commandDeadlineAt) || performance.now() >= value.monotonicEnd) fail() + return value + } + const isolation = validateRouterGuardDatabaseIsolation(config.databaseUrl, config.applicationDatabaseUrl) + if (!hash.safeParse(config.trustKeySha256).success + || [config.sourceFile, config.trustKeyFile, config.eventKeyFile].some(file => !isAbsolute(file) || resolve(file) !== file) || new Set([config.sourceFile, config.trustKeyFile, config.eventKeyFile]).size !== 3) fail() + // URL override/query validation is inherited from the existing strict parser. + const endpointHash = isolation.controlEndpointSha256 + const pool = new Pool({ connectionString: config.databaseUrl, max: 4, connectionTimeoutMillis: 2000, query_timeout: 5000, options: "-c search_path=pg_catalog", application_name: "mmapp-router-guard" }); pool.on("error", () => {}) + const contexts = new AsyncLocalStorage(), active = new Set(); let closed = false, closing: Promise | undefined + async function material(live: () => void) { + const publicBytes = await readBandwidthControlFile(config.trustKeyFile, 16384, live), publicKey = createPublicKey(publicBytes) + live(); if (publicKey.asymmetricKeyType !== "ed25519" || sha(publicKey.export({ type: "spki", format: "der" })) !== config.trustKeySha256) fail() + const signed = envelope.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(await readBandwidthControlFile(config.sourceFile, 65536, live)))) + const payload = Buffer.from(signed.payload, "base64url"), signature = Buffer.from(signed.signature, "base64url") + if (payload.toString("base64url") !== signed.payload || signature.toString("base64url") !== signed.signature || !verify(null, payload, publicKey, signature)) fail() + const pin = routerGuardRecordSchema.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(payload))) + if (canonical(pin) !== payload.toString("utf8") || pin.endpointSha256 !== endpointHash || pin.schemaSourceSha256 !== routerGuardSchemaSourceSha256() || pin.databaseName !== isolation.databaseName || pin.runtimeRole !== isolation.runtimeRole || pin.runtimeRole === pin.ownerRole || pin.ownerRole === isolation.applicationRole || pin.databaseOwner === isolation.applicationRole || pin.applicationDatabaseOid === pin.databaseOid) fail() + const key = await readBandwidthControlFile(config.eventKeyFile, 32, live) + try { live(); if (key.length !== 32 || sha(key) !== pin.eventKeySha256 || pin.eventKeySha256 === config.trustKeySha256) fail(); return { pin, sourceHash: sha(payload), key } } + catch (error) { key.fill(0); throw error } + } + async function query(lease: Lease, text: string, values?: unknown[]) { + const config: QueryConfig & { query_timeout: number } = { text, values, query_timeout: Math.max(1, Math.min(5000, lease.scope.deadlineAt - Date.now())) } + lease.live(); const result = await waitLease(lease, () => lease.client.query(config)); lease.live(); return result + } + async function waitLease(lease: Lease, work: () => Promise, late?: (value: T) => void): Promise { + lease.live(); let cancel!: () => void, abandoned = false + const stopped = new Promise((_resolve, reject) => { cancel = () => { abandoned = true; reject(new RouterGuardRefused()) }; lease.scope.signal!.addEventListener("abort", cancel, { once: true }) }) + const pending = Promise.resolve().then(() => { lease.live(); return work() }) + void pending.then(value => { if (abandoned) late?.(value) }, () => {}) + try { const value = await Promise.race([pending, stopped]); lease.live(); return value } finally { lease.scope.signal!.removeEventListener("abort", cancel) } + } + async function verifiedHead(lease: Lease, key: Buffer) { + const rows = (await query(lease, HEAD_SQL)).rows + if (rows.length !== 1) fail(); const head = rows[0] as Head + if (head.installation_id !== lease.pin.installationId || head.guard_generation !== lease.pin.guardGeneration || !/^(0|[1-9][0-9]{0,15})$/.test(head.sequence) || !Number.isSafeInteger(Number(head.sequence)) || head.head_mac !== mac(key, "head", headBody(head))) fail() + if (Number(head.sequence) === 0) { if (head.state !== "idle" || head.current_attempt !== null || head.last_event !== null || head.last_event_mac !== null) fail() } + else { + const events = (await query(lease, "SELECT event_id::pg_catalog.text,attempt_id::pg_catalog.text,sequence::pg_catalog.text,transition,evidence_mac,event_mac FROM mmapp_router_guard.events WHERE event_id=$1", [head.last_event])).rows + if (events.length !== 1) fail(); const { event_mac, ...body } = events[0] + if (event_mac !== head.last_event_mac || event_mac !== mac(key, "event", body) || body.sequence !== head.sequence || head.state === "idle" && !["acknowledged", "no-send"].includes(String(body.transition)) || head.state !== "idle" && body.attempt_id !== head.current_attempt) fail() + } + return head + } + async function authenticateUnchecked(lease: Lease, binding?: Readonly) { + lease.live(); const first = await material(lease.live) + try { + if (first.sourceHash !== lease.sourceHash) fail() + const rows = (await query(lease, ROUTER_GUARD_ROLE_SQL, [lease.pin.runtimeRole, isolation.applicationDatabaseName, isolation.applicationRole])).rows + if (rows.length !== 1) fail(); const row = rows[0] + if (row.database_name !== lease.pin.databaseName || row.database_oid !== String(lease.pin.databaseOid) || row.database_owner !== lease.pin.databaseOwner || row.session_role !== lease.pin.runtimeRole || row.current_role !== lease.pin.runtimeRole || row.backend_pid !== lease.pid || row.schema_owner !== lease.pin.ownerRole || row.schema_usage !== true || row.fsync !== "on" || row.full_page_writes !== "on" + || row.application_database_oid !== null && row.application_database_oid === String(lease.pin.databaseOid) + || ["rolsuper", "rolcreatedb", "rolcreaterole", "rolreplication", "rolbypassrls", "rolinherit", "memberships", "schema_create", "core_usage", "core_create", "application_role_unsafe", "public_schema_privileges", "public_object_privileges"].some(name => row[name] !== false) || !Array.isArray(row.privileges) || row.privileges.length !== 40) fail() + for (const value of row.privileges as { table: string; privilege: string; held: boolean }[]) { + const expected = value.privilege === "SELECT" || value.privilege === "INSERT" && ["attempts", "events"].includes(value.table) || value.privilege === "UPDATE" && value.table === "head" + if (value.held !== expected) fail() + } + const catalog = (await query(lease, ROUTER_GUARD_CATALOG_SQL)).rows + if (catalog.length !== 1 || Buffer.byteLength(canonical(catalog[0])) > 2097152 || sha(canonical(catalog[0])) !== lease.pin.catalogSha256) fail() + validateRouterGuardEnforcementCatalog(catalog[0], lease.pin.ownerRole) + const identities = (await query(lease, "SELECT installation_id::pg_catalog.text,guard_generation::pg_catalog.text,source_record_sha256,event_key_sha256,status FROM mmapp_router_guard.source_identity WHERE id=1")).rows + if (identities.length !== 1 || identities[0].installation_id !== lease.pin.installationId || identities[0].guard_generation !== lease.pin.guardGeneration || identities[0].source_record_sha256 !== lease.sourceHash || identities[0].event_key_sha256 !== lease.pin.eventKeySha256 || identities[0].status !== "active") fail() + const locks = (await query(lease, "SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE locktype='advisory' AND pid=pg_catalog.pg_backend_pid() AND classid=0::pg_catalog.oid AND objid=8723129::pg_catalog.oid AND objsubid=1 AND mode=$1 AND granted) AS held", [lease.mode === "exclusive" ? "ExclusiveLock" : "ShareLock"])).rows + if (locks.length !== 1 || locks[0].held !== true) fail() + const last = await material(lease.live) + try { + if (last.sourceHash !== first.sourceHash) fail(); const head = await verifiedHead(lease, last.key) + const final = await material(lease.live) + try { + if (final.sourceHash !== last.sourceHash) fail() + if (lease.writerAuthority) { + const current = await writerAuthority(lease.originalBinding!, lease.scope) + if (canonical(current) !== canonical(lease.writerAuthority)) fail() + } + await authorize(binding, lease.scope); lease.live(); return head + } finally { final.key.fill(0) } + } finally { last.key.fill(0) } + } finally { first.key.fill(0) } + } + const authenticate = (lease: Lease, binding?: Readonly) => waitLease(lease, () => authenticateUnchecked(lease, binding)) + async function open(mode: Lease["mode"], inputScope: BandwidthScope, binding?: Readonly) { + const rawScope = captureScope(inputScope) + lower(); if (closed || !Number.isSafeInteger(rawScope.deadlineAt) || rawScope.signal?.aborted) fail() + const enteredWall = Date.now(), enteredMono = performance.now(), controller = new AbortController(), abort = () => controller.abort() + // Only authority LOOKUP and mapping acquisition use the five-second cap. + // Writer lifetime then comes from the ORIGINAL trusted actor/command span. + let deadlineAt = Math.min(rawScope.deadlineAt, enteredWall + 5000), end = enteredMono + Math.max(0, deadlineAt - enteredWall) + let scope = Object.freeze({ deadlineAt, signal: controller.signal }) + rawScope.signal?.addEventListener("abort", abort, { once: true }) + let lease: Lease | undefined, client: PoolClient | undefined + const disposed = new WeakSet() + const dispose = (actual: PoolClient) => { if (!disposed.has(actual)) { disposed.add(actual); actual.release(true) } } + const stopped = new Promise((_resolve, reject) => controller.signal.addEventListener("abort", () => { if (lease) lease.stop(); else if (client) dispose(client); reject(new RouterGuardRefused()) }, { once: true })); void stopped.catch(() => {}) + let timer = setTimeout(abort, Math.max(1, end - performance.now())) + const live = () => { if (closed || controller.signal.aborted || rawScope.signal?.aborted || Date.now() >= deadlineAt || performance.now() >= end) fail() } + try { return await Promise.race([(async () => { + const authority = mode === "exclusive" ? await writerAuthority(binding ?? fail(), scope) : undefined + live() + if (authority) { + deadlineAt = Math.min(rawScope.deadlineAt, authority.actorExpiresAt, authority.commandDeadlineAt) + end = Math.min(enteredMono + Math.max(0, deadlineAt - enteredWall), authority.monotonicEnd) + clearTimeout(timer); timer = setTimeout(abort, Math.max(1, end - performance.now())); live() + scope = Object.freeze({ deadlineAt, signal: controller.signal }) + } + const first = await material(live); first.key.fill(0) + const checkout = pool.connect().then(actual => { + // Checked-out clients have no idle-pool error handler. Install before + // PID/lock/source queries, including a late checkout after cancellation. + actual.on("error", abort); actual.once("end", abort) + if (controller.signal.aborted) { dispose(actual); throw new RouterGuardRefused() } + return actual + }); void checkout.then(late => { if (controller.signal.aborted) dispose(late) }, () => {}) + client = await Promise.race([checkout, stopped]); live() + const pid = (await client.query("SELECT pg_catalog.pg_backend_pid() AS pid")).rows[0]?.pid + live() + if (!Number.isSafeInteger(pid)) fail() + const locked = (await client.query(`SELECT pg_catalog.pg_try_advisory_lock${mode === "shared" ? "_shared" : ""}(${ROUTER_GUARD_LOCK}) AS held`)).rows + live() + if (locked.length !== 1 || locked[0].held !== true) fail() + lease = { client, scope, mode, pid, pin: first.pin, sourceHash: first.sourceHash, active: true, end, refs: 1, failed: false, activity: 0, receiptMacs: [], + writerAuthority: authority, originalBinding: binding, + live() { live(); if (!lease!.active) fail() }, + stop() { if (!lease!.active) return; lease!.active = false; active.delete(lease!); clearTimeout(timer); rawScope.signal?.removeEventListener("abort", abort); dispose(lease!.client); controller.abort() } } + active.add(lease) + await authenticate(lease); return lease + })(), stopped]) } catch (error) { clearTimeout(timer); rawScope.signal?.removeEventListener("abort", abort); lease?.stop(); if (!lease && client) dispose(client); throw error } + } + async function append(lease: Lease, transition: "intent" | "acknowledged" | "no-send" | "unknown", evidence: unknown, attempt?: string) { + const current = await authenticate(lease), key = (await material(lease.live)).key + try { + const sequence = String(Number(current.sequence) + 1), event = { event_id: randomUUID(), attempt_id: attempt ?? lease.rootAttempt!, sequence, transition, evidence_mac: mac(key, "receipt", evidence) }, eventMac = mac(key, "event", event) + const next: Head = { installation_id: lease.pin.installationId, guard_generation: lease.pin.guardGeneration, sequence, state: transition === "intent" ? "pending" : transition === "unknown" ? "unknown" : "idle", current_attempt: transition === "intent" ? event.attempt_id : transition === "unknown" ? current.current_attempt : null, last_event: event.event_id, last_event_mac: eventMac, head_mac: "" }; next.head_mac = mac(key, "head", headBody(next)) + await query(lease, "INSERT INTO mmapp_router_guard.events(event_id,attempt_id,sequence,transition,evidence_mac,event_mac) VALUES($1,$2,$3,$4,$5,$6)", [event.event_id, event.attempt_id, sequence, transition, event.evidence_mac, eventMac]) + const update = await query(lease, "UPDATE mmapp_router_guard.head SET sequence=$1,state=$2,current_attempt=$3,last_event=$4,last_event_mac=$5,head_mac=$6 WHERE id=1 AND sequence=$7 RETURNING id", [sequence, next.state, next.current_attempt, event.event_id, eventMac, next.head_mac, current.sequence]) + if (update.rows.length !== 1) fail(); lease.head = next + } finally { key.fill(0) } + } + async function transaction(lease: Lease, work: () => Promise) { + const begin = await query(lease, "BEGIN"); if (begin.command !== "BEGIN") fail() + try { await query(lease, "SET LOCAL synchronous_commit=on"); await work(); const commit = await query(lease, "COMMIT"); if (commit.command !== "COMMIT") fail() } + catch (error) { lease.failed = true; throw error } // destroy/rollback by stop; never replay COMMIT + } + async function attempt(lease: Lease, binding: Readonly, parent?: string) { + const value = await material(lease.live) + try { + const id = randomUUID(), endpoint = mac(value.key, "endpoint", binding.endpoint), command = mac(value.key, "command", binding.command), actor = mac(value.key, "actor", binding.actorProof), bindingMac = mac(value.key, "binding", { id, parent: parent ?? null, endpoint, command, actor, kind: binding.kind }) + await query(lease, "INSERT INTO mmapp_router_guard.attempts(attempt_id,installation_id,guard_generation,parent_attempt,kind,endpoint_mac,command_mac,actor_mac,binding_mac) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)", [id, lease.pin.installationId, lease.pin.guardGeneration, parent ?? null, binding.kind, endpoint, command, actor, bindingMac]); return id + } finally { value.key.fill(0) } + } + async function writer(rawBinding: RouterGuardWriteBinding, inputScope: BandwidthScope, work: (lease: Lease) => Promise) { + const rawScope = captureScope(inputScope) + const binding = capture(rawBinding) as Readonly; if (!hash.safeParse(binding.actorProof).success) fail() + const inherited = contexts.getStore() + if (inherited) { + const lease = inherited.lease + lease.live(); if (!inherited.active || inherited.child || lease.mode !== "exclusive" || !lease.rootAttempt || rawScope.signal?.aborted || Date.now() >= rawScope.deadlineAt) fail() + inherited.child = true; lease.refs++ + const child: Frame = { lease, active: true, child: false }, abort = () => lease.stop() + const timer = setTimeout(abort, Math.max(1, Math.min(rawScope.deadlineAt - Date.now(), lease.end - performance.now()))) + rawScope.signal?.addEventListener("abort", abort, { once: true }) + try { return await waitLease(lease, async () => { await authorize(binding, lease.scope); await transaction(lease, async () => { await attempt(lease, binding, lease.rootAttempt) }); return contexts.run(child, () => work(lease)) }) } + catch (error) { lease.failed = true; throw error } finally { child.active = false; inherited.child = false; lease.refs--; clearTimeout(timer); rawScope.signal?.removeEventListener("abort", abort) } + } + const lease = await open("exclusive", rawScope, binding) + try { return await waitLease(lease, async () => { + const head = await authenticate(lease, binding); if (head.state !== "idle") fail() + await transaction(lease, async () => { lease.rootAttempt = await attempt(lease, binding); await append(lease, "intent", { binding }, lease.rootAttempt) }) + // Only a genuine physical intent COMMIT ACK can reach any native/app IO. + await authenticate(lease, binding) + const frame: Frame = { lease, active: true, child: false } + let result: T + try { result = await contexts.run(frame, () => work(lease)) } finally { frame.active = false } + if (lease.refs !== 1 || lease.failed || lease.receiptMacs.length !== lease.activity) throw new RouterGuardUnknown() + await transaction(lease, () => append(lease, lease.activity ? "acknowledged" : "no-send", { privateReceiptMacs: lease.receiptMacs, observedActivity: lease.activity })) + return result + }) + } catch { throw new RouterGuardUnknown() } + finally { lease.stop() } + } + return Object.freeze({ + async acquireMapping(scope: BandwidthScope) { + const inherited = contexts.getStore(); if (inherited) fail() // no S->X or X->S self-deadlock + const lease = await open("shared", scope) + try { if ((await authenticate(lease)).state !== "idle") fail() } catch (error) { lease.stop(); throw error } + return Object.freeze({ async verify() { try { if ((await authenticate(lease)).state !== "idle") fail() } catch (error) { lease.stop(); throw error } }, + async run(work: () => Promise) { lease.live(); const frame: Frame = { lease, active: true, child: false }; try { return await contexts.run(frame, work) } finally { frame.active = false } }, close: lease.stop, get signal() { return lease.scope.signal! } }) + }, + runCompound(binding: RouterGuardWriteBinding, scope: BandwidthScope, work: () => Promise) { return writer(binding, scope, async () => work()) }, + async runNative(binding: RouterGuardNativeBinding, scope: BandwidthScope): Promise { + const captured = capture(binding) as Readonly + if (!Number.isSafeInteger(captured.managedServerId) || captured.managedServerId <= 0) fail() + return writer(captured, scope, async lease => { + lease.activity++ + let bound = false, finished = false, resolveReceipt!: (value: unknown) => void, rejectReceipt!: (error: Error) => void + const receipt = new Promise((resolve, reject) => { resolveReceipt = resolve; rejectReceipt = reject }); void receipt.catch(() => {}) + const bind: RouterGuardNativeBinder = (request, send) => { + if (!(request instanceof ClientRequest) || bound || request.method !== captured.method || request.path !== captured.requestPath || request.getHeader("authorization") !== "Basic " + Buffer.from(captured.endpoint.username + ":" + captured.endpoint.password).toString("base64")) fail() + const host = isIP(captured.endpoint.host) === 6 ? "[" + captured.endpoint.host + "]" : captured.endpoint.host + if (![host, host + ":" + captured.endpoint.port].includes(String(request.getHeader("host")))) fail() + bound = true; request.once("finish", () => { finished = true }); request.once("error", rejectReceipt) + request.once("response", response => { const bytes: Buffer[] = []; let size = 0 + response.on("data", (chunk: Buffer) => { size += chunk.length; if (size > 8388608) { request.destroy(); rejectReceipt(new RouterGuardUnknown()) } else bytes.push(chunk) }) + response.once("error", rejectReceipt); response.once("aborted", () => rejectReceipt(new RouterGuardUnknown())) + response.once("end", () => { try { + if (!finished || !response.complete || !response.statusCode || response.statusCode < 200 || response.statusCode >= 300 || captured.method === "POST") throw new RouterGuardUnknown() + const body = Buffer.concat(bytes) + if (body.length) { + const parsed: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body)) + // A genuine complete response still is not an acknowledgement + // when RouterOS reports an operation error in its JSON body. + if (parsed && typeof parsed === "object" && !Array.isArray(parsed) && ("error" in parsed || "message" in parsed || "detail" in parsed)) throw new RouterGuardUnknown() + } + resolveReceipt({ kind: "physical-native-rest-ack", responseBodySha256: sha(body), requestPath: captured.requestPath, method: captured.method }) + } catch { rejectReceipt(new RouterGuardUnknown()) } }) + }) + lease.scope.signal?.addEventListener("abort", () => request.destroy(new RouterGuardUnknown()), { once: true }) + let socketHandled = false, sent = false + const ready = () => { if (sent) return; sent = true; void (async () => { await authenticate(lease, captured); send() })().catch(() => request.destroy(new RouterGuardUnknown())) } + const socketReady = (socket: NonNullable) => { + if (socketHandled) return; socketHandled = true + if (socket instanceof TLSSocket && !socket.getPeerFinished()) socket.once("secureConnect", ready) + else if (socket.connecting) socket.once("connect", ready) + else ready() + } + request.once("socket", socketReady) + if (request.socket) socketReady(request.socket) + } + const invocation: NativeInvocation = { active: true, used: false, scope: lease.scope, bind, live: lease.live, + current: () => contexts.getStore()?.lease === lease && contexts.getStore()?.active === true } + nativeInvocations.set(captured, invocation) + try { + const result = await native(captured, lease.scope, bind) + if (!bound) throw new RouterGuardUnknown() // a returned JSON value is not a receipt + const observed = await receipt, key = (await material(lease.live)).key + try { lease.receiptMacs.push(mac(key, "private-native-receipt", observed)) } finally { key.fill(0) } + return result + } finally { invocation.active = false; nativeInvocations.delete(captured) } + }) + }, + async runApplication(binding: RouterGuardWriteBinding, scope: BandwidthScope, work: (client: Pick) => Promise) { + return writer(binding, scope, async lease => { + lease.activity++ + let began = false, released = false, pending = 0, accepting = true + const disposed = new WeakSet() + const dispose = (actual: PoolClient) => { if (!disposed.has(actual)) { disposed.add(actual); actual.release(true) } } + const client = await waitLease(lease, () => appPool.connect().then(actual => { + const lost = () => { + if (disposed.has(actual)) return // deliberate physical close already accounted for + lease.failed = true; accepting = false; lease.stop(); dispose(actual); released = true + } + // Keep error handling on the destroyed physical client until GC: + // pg can emit error while rejecting an in-flight COMMIT, before end. + actual.on("error", lost); actual.once("end", lost) + return actual + }), dispose) + // Observe the ACTUAL protocol packet, before pg's own handler resolves + // the query. Neither SQL text nor a callback result supplies tx state. + const connection = (client as PoolClient & { connection: EventEmitter }).connection + let readySequence = 0, transactionStatus: unknown + const onReady = (message: { status?: unknown }) => { readySequence++; transactionStatus = message.status } + connection.prependListener("readyForQuery", onReady) + const actualQuery = async (text: string, values?: unknown[]) => { + const before = readySequence + const result = await client.query(text, values) + if (readySequence !== before + 1 || !["I", "T", "E"].includes(String(transactionStatus))) fail() + return result + } + const free = () => { if (!released) { released = true; dispose(client) } } + lease.scope.signal!.addEventListener("abort", free, { once: true }) + try { + // This uses the ALREADY borrowed application client, never another + // core/guard checkout. Validate the actual driver target when present. + const identity = (await actualQuery("SELECT pg_catalog.current_database() AS database_name,SESSION_USER::pg_catalog.text AS session_role,CURRENT_USER::pg_catalog.text AS current_role,d.oid::pg_catalog.int8::pg_catalog.text AS database_oid FROM pg_catalog.pg_database d WHERE d.datname=pg_catalog.current_database()")).rows + lease.live() + if (identity.length !== 1 || identity[0].database_name !== isolation.applicationDatabaseName || identity[0].session_role !== isolation.applicationRole || identity[0].current_role !== isolation.applicationRole || identity[0].database_oid === String(lease.pin.databaseOid) || lease.pin.applicationDatabaseOid !== undefined && identity[0].database_oid !== String(lease.pin.applicationDatabaseOid)) fail() + if ((await actualQuery("BEGIN")).command !== "BEGIN" || transactionStatus !== "T") fail(); began = true + const facade = Object.freeze({ query: (async (text: unknown, values?: unknown[]) => { + let counted = false + try { + lease.live(); if (!accepting || pending || typeof text !== "string") return fail() + pending++; counted = true + const result = await actualQuery(text, values) + lease.live() + // Multiple statements may hide COMMIT; BEGIN behind comments or + // literals. The real driver result and ReadyForQuery must attest + // exactly one ordinary command still inside OUR transaction. + if (Array.isArray(result) || transactionStatus !== "T" || /^(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|PREPARE)$/i.test(result.command)) fail() + return result + } catch (error) { lease.failed = true; throw error } + finally { if (counted) pending-- } + }) as PoolClient["query"] }) + const value = await work(facade); accepting = false + if (pending !== 0 || contexts.getStore()?.child || lease.failed || transactionStatus !== "T") fail() + const commit = await actualQuery("COMMIT"); began = false; lease.live(); if (commit.command !== "COMMIT" || String(transactionStatus) !== "I") fail() + const key = (await material(lease.live)).key + try { lease.receiptMacs.push(mac(key, "private-app-commit", { guardAttempt: lease.rootAttempt, actualDriverCommand: commit.command })) } finally { key.fill(0) } + return value + } finally { accepting = false; if (began && !released && pending === 0) try { await client.query("ROLLBACK") } catch {} connection.removeListener("readyForQuery", onReady); lease.scope.signal!.removeEventListener("abort", free); free() } + }) + }, + async close() { closed = true; for (const lease of active) lease.stop(); closing ??= pool.end(); await closing }, + }) +} diff --git a/backend/src/services/router-configuration-native-transport.sql-helper.ts b/backend/src/services/router-configuration-native-transport.sql-helper.ts new file mode 100644 index 00000000..5ae47293 --- /dev/null +++ b/backend/src/services/router-configuration-native-transport.sql-helper.ts @@ -0,0 +1,56 @@ +/** Actual owned SQL/kernel/HTTP bridge acceptance; fixture authority only. + * No working database, RouterOS device or all-writer activation claim. */ +import assert from "node:assert/strict" +import { createOwnedRouterGuardFixtureFactory } from "./router-configuration-guard.sql-fixture.js" +import { createRouterConfigurationNativeTransport } from "./router-configuration-native-transport.js" +import { RouterGuardRefused, RouterGuardUnknown, type RouterGuardDependencies, type RouterGuardNativeBinding, type RouterGuardNativeBinder } from "./router-configuration-guard.js" +import type { BandwidthScope } from "./bandwidth-admission.js" + +const scope = () => ({ deadlineAt: Date.now() + 10000 }) +export async function acceptOwnedRouterConfigurationNativeTransport(adminUrl: string) { + assert.equal(process.env.MMAPP_TEST_ROUTER_GUARD_OWNED, "true") + const checks: string[] = [], transport = createRouterConfigurationNativeTransport() + for (const method of ["PUT", "PATCH", "DELETE"] as const) { + const fixture = await createOwnedRouterGuardFixtureFactory(adminUrl, transport)("fixed-transport-" + method.toLowerCase()) + try { + const result = await fixture.guard.runNative({ ...fixture.nativeBinding, method, command: method === "DELETE" ? {} : fixture.nativeBinding.command }, scope()) + assert.equal(fixture.nativeRequests(), 1); assert.equal(new Set(await fixture.observedGuardPids()).size, 1) + if (method === "DELETE") assert.equal(result, undefined) + else assert.deepEqual(result, { ".id": "*1", comment: "owned-fixture" }) + await fixture.assertJournalMacs() + assert.equal((await fixture.owner.query("SELECT state FROM mmapp_router_guard.head WHERE id=1")).rows[0].state, "idle") + const mapping = await fixture.guard.acquireMapping(scope()); await mapping.verify(); mapping.close() + checks.push(method + ": fixed production transport, actual private intent/physical HTTP/parser receipt and valid idle head; exact owned PID") + } finally { await fixture.close() } + } + { + let captured: { binding: Readonly; scope: Readonly; bind: RouterGuardNativeBinder } | undefined + let denied = 0 + const observed: RouterGuardDependencies["nativeTransport"] = async (binding, current, bind) => { + await assert.rejects(transport({ ...binding }, current, bind), RouterGuardRefused); denied++ + const pending = transport(binding, current, bind) + await assert.rejects(transport(binding, current, bind), RouterGuardRefused); denied++ + const result = await pending; captured = { binding, scope: current, bind }; return result + } + const fixture = await createOwnedRouterGuardFixtureFactory(adminUrl, observed)("fixed-transport-copy-replay") + try { + await fixture.guard.runNative(fixture.nativeBinding, scope()); assert(captured) + await assert.rejects(transport(captured.binding, captured.scope, captured.bind), RouterGuardRefused); denied++ + assert.equal(denied, 3); assert.equal(fixture.nativeRequests(), 1); await fixture.assertJournalMacs() + checks.push("genuine active invocation refuses copy and second consume; retired exact binding cannot send again; one physical HTTP request") + } finally { await fixture.close() } + } + for (const fault of ["error-json", "malformed-json", "http-error", "unsupported-post"] as const) { + const fixture = await createOwnedRouterGuardFixtureFactory(adminUrl, transport)("fixed-transport-" + fault) + try { + if (fault !== "unsupported-post") fixture.setNativeFault(fault) + await assert.rejects(fixture.guard.runNative({ ...fixture.nativeBinding, method: fault === "unsupported-post" ? "POST" : "PATCH" }, scope()), RouterGuardUnknown) + assert.equal(fixture.nativeRequests(), fault === "unsupported-post" ? 0 : 1) + assert.equal((await fixture.owner.query("SELECT state FROM mmapp_router_guard.head WHERE id=1")).rows[0].state, "pending") + await fixture.assertJournalMacs(); await assert.rejects(fixture.guard.acquireMapping(scope())) + checks.push(fault + ": no successful terminal receipt, durable pending blocks mapping; unsupported asynchronous verb sends zero HTTP") + } finally { await fixture.close() } + } + return { checks, actualDatabase: true, actualKernelIntentAndReceipt: true, actualLocalhostHttp: true, syntheticFixtureAuthority: true, + nativeRouterOsCompatibility: false, allWriterActivation: false, realChrCalls: 0 } +} diff --git a/backend/src/services/router-configuration-native-transport.test.ts b/backend/src/services/router-configuration-native-transport.test.ts new file mode 100644 index 00000000..d276b4ff --- /dev/null +++ b/backend/src/services/router-configuration-native-transport.test.ts @@ -0,0 +1,26 @@ +import assert from "node:assert/strict" +import http from "node:http" +import https from "node:https" +import { test } from "node:test" +import { createRouterConfigurationNativeTransport } from "./router-configuration-native-transport.js" +import { consumeRouterGuardNativeInvocation, RouterGuardRefused, type RouterGuardNativeBinding } from "./router-configuration-guard.js" + +test("fixed transport cannot construct HTTP from structural JSON, copied binding or a caller-supplied binder", async () => { + const beforeHttp = http.request, beforeHttps = https.request + let requests = 0, bindCalls = 0 + const forbidden = () => { requests++; throw Error("No private kernel invocation") } + http.request = forbidden as typeof http.request; https.request = forbidden as typeof https.request + try { + const transport = createRouterConfigurationNativeTransport(), scope = Object.freeze({ deadlineAt: Date.now() + 5000, signal: new AbortController().signal }) + const binding: RouterGuardNativeBinding = { kind: "native", managedServerId: 1, method: "PATCH", requestPath: "/rest/ip/address/*1", + endpoint: { host: "127.0.0.1", port: 9, username: "owned", password: "fixture", useSsl: false, verifySsl: true }, + command: { comment: "owned" }, actorProof: "a".repeat(64) } + const bind = () => { bindCalls++ } + for (const candidate of [binding, { ...binding }, JSON.parse(JSON.stringify(binding))]) { + assert.throws(() => consumeRouterGuardNativeInvocation(candidate, scope, bind), RouterGuardRefused) + await assert.rejects(transport(candidate, scope, bind), RouterGuardRefused) + } + await assert.rejects(transport({ ...binding, endpoint: { ...binding.endpoint, useSsl: true } }, scope, bind), RouterGuardRefused) + assert.equal(requests, 0); assert.equal(bindCalls, 0) + } finally { http.request = beforeHttp; https.request = beforeHttps } +}) diff --git a/backend/src/services/router-configuration-native-transport.ts b/backend/src/services/router-configuration-native-transport.ts new file mode 100644 index 00000000..66332646 --- /dev/null +++ b/backend/src/services/router-configuration-native-transport.ts @@ -0,0 +1,74 @@ +/** Fixed synchronous RouterOS REST mutation transport for the configured guard. + * It cannot be called with request JSON: the actual kernel must first issue a + * one-shot private invocation after durable intent. No registration/default + * transport, unmanaged client, redirect or async command completion shortcut. */ +import http from "node:http" +import https from "node:https" +import { performance } from "node:perf_hooks" +import { consumeRouterGuardNativeInvocation, RouterGuardRefused, RouterGuardUnknown, type RouterGuardDependencies } from "./router-configuration-guard.js" + +export function createRouterConfigurationNativeTransport(): RouterGuardDependencies["nativeTransport"] { + return async (binding, scope, bind) => { + consumeRouterGuardNativeInvocation(binding, scope, bind) + const { endpoint, method, requestPath } = binding + if (!["PUT", "PATCH", "DELETE"].includes(method) || !Number.isSafeInteger(binding.managedServerId) || binding.managedServerId <= 0 + || !endpoint || typeof endpoint.host !== "string" || !endpoint.host.length || endpoint.host.length > 1024 || /[\u0000-\u0020\u007f/?#]/.test(endpoint.host) + || !Number.isSafeInteger(endpoint.port) || endpoint.port < 1 || endpoint.port > 65535 || typeof endpoint.useSsl !== "boolean" || typeof endpoint.verifySsl !== "boolean" + || typeof endpoint.username !== "string" || typeof endpoint.password !== "string" || endpoint.username.length > 8192 || endpoint.password.length > 8192 + || typeof requestPath !== "string" || requestPath.length > 4096 || !requestPath.startsWith("/rest/") || /[\u0000-\u0020\u007f\\?#]/.test(requestPath) || requestPath.includes("//")) throw new RouterGuardRefused() + const target = new URL(requestPath, "http://router.invalid") + if (target.origin !== "http://router.invalid" || target.pathname !== requestPath) throw new RouterGuardRefused() + let decoded = requestPath + for (let index = 0; index < 2; index++) { + try { decoded = decodeURIComponent(decoded) } catch { throw new RouterGuardRefused() } + if (/[\u0000-\u001f\u007f\\?#]/.test(decoded) || decoded.includes("//") || decoded.split("/").some(segment => segment === "." || segment === "..")) throw new RouterGuardRefused() + } + const command = binding.command + if (!command || typeof command !== "object" || Array.isArray(command) || Object.getPrototypeOf(command) !== Object.prototype) throw new RouterGuardRefused() + const fields = Object.getOwnPropertyDescriptors(command) + if (Reflect.ownKeys(command).length !== Object.keys(fields).length || Object.values(fields).some(field => !("value" in field) || !field.enumerable || typeof field.value !== "string") + || method === "DELETE" && Object.keys(fields).length !== 0) throw new RouterGuardRefused() + const body = method === "DELETE" ? undefined : Buffer.from(JSON.stringify(command)) + if (body && body.length > 1048576) throw new RouterGuardRefused() + const enteredWall = Date.now(), enteredMono = performance.now(), timeoutMs = Math.max(0, Math.min(30000, scope.deadlineAt - enteredWall)) + if (timeoutMs <= 0 || scope.signal?.aborted) throw new RouterGuardRefused() + return new Promise((resolve, reject) => { + const transport = endpoint.useSsl ? https : http + let ended = false + const request = transport.request({ hostname: endpoint.host, port: endpoint.port, path: requestPath, method, agent: false, + signal: scope.signal, rejectUnauthorized: endpoint.useSsl ? endpoint.verifySsl : undefined, + headers: { authorization: "Basic " + Buffer.from(endpoint.username + ":" + endpoint.password).toString("base64"), + "content-type": "application/json", ...(body ? { "content-length": body.length } : {}) }, + }, response => { + const chunks: Buffer[] = []; let bytes = 0 + response.on("error", fail); response.once("aborted", () => fail(new RouterGuardUnknown())) + if (Number(response.headers["content-length"] ?? 0) > 8388608) { response.destroy(); fail(new RouterGuardUnknown()); return } + response.on("data", (chunk: Buffer) => { + bytes += chunk.length + if (bytes > 8388608) { response.destroy(); fail(new RouterGuardUnknown()) } else chunks.push(Buffer.from(chunk)) + }) + response.once("end", () => { + try { + if (!response.complete || !response.statusCode || response.statusCode < 200 || response.statusCode >= 300) throw new RouterGuardUnknown() + const raw = new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks)) + const value: unknown = raw.trim() ? JSON.parse(raw) : {} + if (value && typeof value === "object" && !Array.isArray(value) && ("error" in value || "message" in value || "detail" in value)) throw new RouterGuardUnknown() + if (!ended) { ended = true; clearTimeout(timer); resolve(method === "DELETE" ? undefined : value) } + } catch { fail(new RouterGuardUnknown()) } + }) + }) + const fail = (error: Error) => { + if (!ended) { ended = true; clearTimeout(timer); request.destroy(); reject(error instanceof RouterGuardRefused ? error : new RouterGuardUnknown()) } + } + const timer = setTimeout(() => fail(new RouterGuardUnknown()), timeoutMs) + request.on("error", fail) + try { + bind(request, () => { + if (ended || scope.signal?.aborted || Date.now() >= scope.deadlineAt || performance.now() >= enteredMono + timeoutMs) { fail(new RouterGuardRefused()); return } + if (body) request.write(body) + request.end() + }) + } catch (error) { fail(error instanceof Error ? error : new RouterGuardUnknown()) } + }) + } +} diff --git a/backend/src/services/router-configuration-writer-context.sql-helper.ts b/backend/src/services/router-configuration-writer-context.sql-helper.ts new file mode 100644 index 00000000..0255f30d --- /dev/null +++ b/backend/src/services/router-configuration-writer-context.sql-helper.ts @@ -0,0 +1,91 @@ +/** Real owned PostgreSQL/kernel/HTTP assembly, explicitly synthetic opaque REST + * origin ONLY. No fake SQL driver, kernel mock or full REST/MCP provenance claim. + * The caller provisions a fresh isolated fixture for each pending-state case. */ +import assert from "node:assert/strict" +import { Client } from "pg" +import { setTimeout as delay } from "node:timers/promises" +import type { RouterGuardNativeBinding } from "./router-configuration-guard.js" +import type { createOwnedRouterGuardFixtureFactory } from "./router-configuration-guard.sql-fixture.js" + +type Make = ReturnType +type Fixture = Awaited> +const scope = () => ({ deadlineAt: Date.now() + 20000 }) +const childNative = (binding: RouterGuardNativeBinding): Omit => ({ kind: binding.kind, + managedServerId: binding.managedServerId, endpoint: binding.endpoint, command: binding.command, method: binding.method, requestPath: binding.requestPath }) +const head = async (fixture: Fixture) => (await fixture.owner.query("SELECT state,sequence::text FROM mmapp_router_guard.head WHERE id=1")).rows[0] +async function zeroQueries(work: () => Promise): Promise { + const query = Client.prototype.query; let calls = 0 + // Observation only: every original query/arguments/result is forwarded. The + // origin failures under test must happen before ANY actual driver call. + Client.prototype.query = function(this: Client, ...args: unknown[]) { calls++; return Reflect.apply(query, this, args) } as typeof query + try { await work(); assert.equal(calls, 0) } finally { Client.prototype.query = query } +} + +export async function acceptOwnedRouterConfigurationWriter(make: Make) { + if (process.env.MMAPP_TEST_ROUTER_GUARD_OWNED !== "true") throw Error("Explicit disposable owned writer acceptance required") + const checks: string[] = [] + for (const scenario of ["writer-physical", "writer-nested", "writer-caught-drop", "writer-orphan", "writer-first-scope"] as const) { + const fixture = await make(scenario), writer = fixture.createWriterForFixture() + try { + if (scenario === "writer-physical" || scenario === "writer-nested") { + const token = await writer.captureRest(scope()), pids: number[] = [] + await writer.run(token, scope(), async facade => { + pids.push(await fixture.sampleGuardPid()) + if (scenario === "writer-nested") { + await writer.run(token, scope(), async nested => { + pids.push(await fixture.sampleGuardPid()) + await nested.application(async client => { pids.push(await fixture.sampleGuardPid()); await client.query(fixture.applicationCommitQuery) }) + }) + } else await facade.application(async client => { pids.push(await fixture.sampleGuardPid()); await client.query(fixture.applicationCommitQuery) }) + await facade.native(childNative(fixture.nativeBinding)) + pids.push(await fixture.sampleGuardPid()) + }) + assert.equal((await head(fixture)).state, "idle"); assert.equal(await fixture.applicationRows(), 1); assert.equal(fixture.nativeRequests(), 1) + assert.equal(new Set([...pids, ...await fixture.observedGuardPids()]).size, 1, "all root/borrow/native children retain ONE physical CONTROL PID; application DB is separate") + await zeroQueries(async () => { + for (const copied of [{ ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(writer.run(copied, scope(), async () => {})) + await assert.rejects(writer.run(token, scope(), async () => {})) + }) + checks.push(scenario + ": real root+application COMMIT+native private HTTP ACK use one control PID; nested root borrows; copied/used origins cause zero SQL") + } else if (scenario === "writer-caught-drop") { + fixture.setNativeFault("drop-after-write") + const token = await writer.captureRest(scope()) + await assert.rejects(writer.run(token, scope(), async facade => { + await facade.native(childNative(fixture.nativeBinding)).catch(() => ({ claimedAcknowledgement: true })) + return { acknowledged: true } + })) + assert.equal(fixture.nativeRequests(), 1); assert.equal((await head(fixture)).state, "pending"); assert.equal(await fixture.applicationRows(), 0) + checks.push("real native write/drop caught into JSON cannot mint a child/root terminal; journal remains pending") + } else if (scenario === "writer-orphan") { + let enter!: () => void, release!: () => void, child: Promise | undefined + const entered = new Promise(resolve => { enter = resolve }), held = new Promise(resolve => { release = resolve }) + const token = await writer.captureRest(scope()) + try { + await assert.rejects(writer.run(token, scope(), async facade => { + child = facade.application(async client => { enter(); await held; await client.query(fixture.applicationCommitQuery) }) + void child.catch(() => {}) + await entered; return { droppedChild: true } + })) + } finally { release() } + await assert.rejects(child!) + assert.equal((await head(fixture)).state, "pending"); assert.equal(await fixture.applicationRows(), 0); assert.equal(fixture.nativeRequests(), 0) + checks.push("actual begun application child dropped by parent cannot commit late or clear pending with JSON") + } else { + const caller = new AbortController(), token = await writer.captureRest({ deadlineAt: Date.now() + 5000, signal: caller.signal }) + caller.abort() + await zeroQueries(async () => { await assert.rejects(writer.run(token, { deadlineAt: Date.now() + 60000, signal: new AbortController().signal }, async () => {})) }) + const short = await writer.captureRest({ deadlineAt: Date.now() + 20 }); await delay(30) + const originalNow = Date.now + try { + Date.now = () => originalNow() - 60000 + await zeroQueries(async () => { await assert.rejects(writer.run(short, { deadlineAt: originalNow() + 60000 }, async () => {})) }) + } finally { Date.now = originalNow } + assert.equal((await head(fixture)).state, "idle"); assert.equal(fixture.nativeRequests(), 0) + checks.push("first caller abort and original short monotonic capture cannot be revived by larger/new-signal run or wall rollback; zero SQL") + } + await fixture.assertJournalMacs() + } finally { await fixture.close() } + } + return { checks, actualPgRun: true, actualKernelAndHttp: true, syntheticOpaqueRestOrigin: true, genuineRestProvenance: false, genuineMcpProvenance: false, + runtimeActivation: false, allWriterCoverage: false, realChrCalls: 0 } +} diff --git a/backend/src/services/router-configuration-writer-context.sql.test.ts b/backend/src/services/router-configuration-writer-context.sql.test.ts new file mode 100644 index 00000000..ec8fffff --- /dev/null +++ b/backend/src/services/router-configuration-writer-context.sql.test.ts @@ -0,0 +1,13 @@ +/** Exclusive owned profile entry only; no runtime/default writer installation. */ +import { createOwnedRouterGuardFixtureFactory } from "./router-configuration-guard.sql-fixture.js" +import { acceptOwnedRouterConfigurationWriter } from "./router-configuration-writer-context.sql-helper.js" +async function main() { + const admin = process.env.MMAPP_TEST_ROUTER_GUARD_ADMIN_URL + if (!admin) throw Error("Missing explicit disposable owned-cluster admin URL") + const report = await acceptOwnedRouterConfigurationWriter(createOwnedRouterGuardFixtureFactory(admin)) + process.stdout.write(JSON.stringify(report, null, 2) + "\n") +} +void main().catch(error => { + const name = error instanceof Error && /^[A-Za-z]+$/.test(error.name) ? error.name : "Error" + process.stderr.write(JSON.stringify({ status: "refused", error: name }) + "\n"); process.exitCode = 1 +}) diff --git a/backend/src/services/router-configuration-writer-context.test.ts b/backend/src/services/router-configuration-writer-context.test.ts new file mode 100644 index 00000000..d150fee3 --- /dev/null +++ b/backend/src/services/router-configuration-writer-context.test.ts @@ -0,0 +1,272 @@ +/** Genuine MCP private provenance/current-state/key checks; explicitly mocked + * guard kernel and SQL transport. No DB, sockets or all-writer coverage claim. */ +import assert from "node:assert/strict" +import { AsyncLocalStorage } from "node:async_hooks" +import { randomUUID } from "node:crypto" +import { lstat, mkdir, mkdtemp, readFile, realpath, rm, unlink, writeFile } from "node:fs/promises" +import { basename, dirname, join, resolve } from "node:path" +import { tmpdir } from "node:os" +import { mock, test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +import { performance } from "node:perf_hooks" +import { pool, closePool } from "../db/index.js" +import { withVerifiedConfigurationChange, type VerifiedMcpServiceMetadata } from "./config-mutation-policy.js" +import { redact } from "../modules/mcp/security.js" +import type { RouterGuardDependencies, RouterGuardWriteBinding } from "./router-configuration-guard.js" +import type { TrustedRouterWriterOriginProvider, TrustedRouterWriterOriginSnapshot } from "./router-configuration-writer-context.js" + +class Refused extends Error {} +class Unknown extends Error {} +const events: Array<{ event: string; pid: number; binding?: RouterGuardWriteBinding }> = [] +let nextPid = 0, factoryCalls = 0, childGate: Promise | undefined +const observedLifetimes: Array<{ deadlineAt: number; actorExpiresAt: number; commandDeadlineAt: number; monotonicEnd: number }> = [] +const kernelFrame = new AsyncLocalStorage<{ pid: number; receipts: number }>() +mock.module("./router-configuration-guard.js", { namedExports: { RouterGuardRefused: Refused, RouterGuardUnknown: Unknown, + createConfiguredRouterGuard: (_config: unknown, deps: RouterGuardDependencies) => { + factoryCalls++ + const authorize = async (binding: RouterGuardWriteBinding, scope: { deadlineAt: number }) => { + await deps.authorize(binding, scope); const authority = await deps.readOriginalWriterAuthority(binding, scope) + observedLifetimes.push({ deadlineAt: scope.deadlineAt, ...authority }) + } + return { + async runCompound(binding: RouterGuardWriteBinding, scope: { deadlineAt: number }, work: () => Promise) { + await authorize(binding, scope) + const parent = kernelFrame.getStore(), frame = parent ?? { pid: ++nextPid, receipts: 0 } + events.push({ event: parent ? "nested-intent" : "intent", pid: frame.pid, binding }) + try { + const value = await kernelFrame.run(frame, work); await authorize(binding, scope) + events.push({ event: parent ? "nested-complete" : frame.receipts ? "terminal" : "no-send", pid: frame.pid }); return value + } catch (error) { events.push({ event: "pending", pid: frame.pid }); throw error } + }, + async runApplication(binding: RouterGuardWriteBinding, scope: { deadlineAt: number }, work: (client: { query(text: string): Promise }) => Promise) { + await authorize(binding, scope); const frame = kernelFrame.getStore(); assert(frame) + events.push({ event: "physical-app", pid: frame.pid, binding }); await childGate + const value = await work({ async query(text) { assert.equal(text, "INSERT owned"); return { rows: [], command: "INSERT" } } }) + await authorize(binding, scope); frame.receipts++; return value + }, + async runNative(binding: RouterGuardWriteBinding, scope: { deadlineAt: number }) { + await authorize(binding, scope); const frame = kernelFrame.getStore(); assert(frame) + events.push({ event: "physical-native", pid: frame.pid, binding }); frame.receipts++; return { physicalMock: true } + }, async close() {}, + } + }, +} }) + +test("explicit outer writer with genuine MCP origin and mocked guard/SQL", async t => { + const directory = await mkdtemp(join(tmpdir(), "router-writer-context-")), oldState = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = directory + const keyFile = join(directory, "encryption-key"), generationFile = join(directory, "access-generation") + const originalQuery = pool.query, originalConnect = pool.connect + let row: Record | undefined, queryHook: (() => Promise) | undefined, lowerHeld = false + pool.query = (async () => { await queryHook?.(); return { rows: row ? [structuredClone(row)] : [] } }) as never + pool.connect = (() => { throw Error("No physical DB in context unit") }) as never + const { createConfiguredRouterConfigurationWriter } = await import("./router-configuration-writer-context.js") + type Writer = ReturnType + const all: Writer[] = [], scope = () => ({ deadlineAt: Date.now() + 10000 }) + const config = { databaseUrl: "not-read-by-mocked-kernel", applicationDatabaseUrl: "not-read-by-mocked-kernel", sourceFile: "fixture", trustKeyFile: "fixture", trustKeySha256: "f".repeat(64), eventKeyFile: "fixture" } + const make = (external?: TrustedRouterWriterOriginProvider) => { const writer = createConfiguredRouterConfigurationWriter(config, { applicationPool: pool, rest: external, scheduler: external, + nativeTransport: async () => { throw Error("No native in context unit") }, assertNoLowerLockContext() { assert.equal(lowerHeld, false) } }); all.push(writer); return writer } + async function fixture() { + process.env.MCP_STATE_DIR = directory + childGate = undefined; queryHook = undefined; events.length = 0; observedLifetimes.length = 0; lowerHeld = false + await writeFile(keyFile, Buffer.alloc(32, 17)); await writeFile(generationFile, Buffer.alloc(32, 23)) + const expiry = Date.now() + 20000, connectionId = randomUUID(), input = { serverId: 71, reviewedValue: "sealed-only" } + const metadata: VerifiedMcpServiceMetadata = { operationId: randomUUID(), planId: randomUUID(), operationName: "owned.writer", serviceName: "owned.write", stepIndex: 0, phase: "forward", operationVersion: 1, policyVersion: 1, + generation: "original-generation", originalExpiresAt: expiry, sealedEvidenceProof: "e".repeat(64), sealedPayloadProof: "f".repeat(64), originalTemporaryGrantProof: "a".repeat(64), originalTemporaryGrantExpiresAt: null, + principal: { connectionId, credentialId: randomUUID(), credentialExpiresAt: new Date(expiry + 1000).toISOString() }, serviceInput: input } + let authorized = true, authorizations = 0 + const context = { actorId: "owned-original-owner", connectionId, approved: false, serverIds: [71], objects: [{ kind: "server" as const, serverId: 71, id: "71" }], serverWide: true, serviceExecution: metadata, + authorize: async () => { authorizations++; if (!authorized) throw Error("original authority revoked") } } + row = { operation_state: "running", plan_state: "executing", step_state: "intent", server_id: null, operation_id: metadata.operationName, operation_version: 1, policy_version: 1, generation: metadata.generation, + owner_id: context.actorId, approved_by: null, expires_at: new Date(expiry), evidence_proof: metadata.sealedEvidenceProof, payload_proof: metadata.sealedPayloadProof, intended_state: redact({ kind: "service", service: metadata.serviceName, input }) } + return { context, input, revoke() { authorized = false }, authorizations: () => authorizations } + } + const write = (client: { query: (text: string) => Promise }) => client.query("INSERT owned") + try { + await t.test("no origin callback/JSON/fake guard can manufacture a live MCP writer", async () => { + await fixture(); const writer = make(), before = factoryCalls + await assert.rejects(writer.captureMcp(scope())) + await assert.rejects(writer.captureRest(scope())); await assert.rejects(writer.captureScheduler(scope())) + await assert.rejects(writer.run({ verified: true }, scope(), async () => ({ acknowledged: true }))) + assert.equal(events.length, 0); assert.equal(factoryCalls, before) + }) + await t.test("capture occurs before lower locks; binding derives from sealed current metadata", async () => { + const f = await fixture(), writer = make() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp(scope()) + await writer.run(token, scope(), async facade => { lowerHeld = true; try { await facade.application(write) } finally { lowerHeld = false } }) + }) + assert.deepEqual(events.map(event => event.event), ["intent", "physical-app", "terminal"]) + const command = events[0].binding!.command as { serviceInput: unknown } + assert.deepEqual(command.serviceInput, f.input); assert.match(events[0].binding!.actorProof, /^[a-f0-9]{64}$/) + }) + await t.test("zero-activity legacy callback JSON leaves pending instead of minting no-send", async () => { + const f = await fixture(), writer = make() + await withVerifiedConfigurationChange(f.context, async () => { const token = await writer.captureMcp(scope()); await assert.rejects(writer.run(token, scope(), async () => ({ actualAck: true })), Unknown) }) + assert.deepEqual(events.map(event => event.event), ["intent", "pending"]) + }) + await t.test("foreign/serialized/copy origins, replay and retired facade refuse", async () => { + const f = await fixture(), writer = make(), foreign = make() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp(scope()) + for (const other of [JSON.parse(JSON.stringify(token)), { ...token }]) await assert.rejects(writer.run(other, scope(), async () => {})) + await assert.rejects(foreign.run(token, scope(), async () => {})) + let saved!: { application: (work: typeof write) => Promise } + await writer.run(token, scope(), async facade => { saved = facade; await facade.application(write) }) + await assert.rejects(writer.run(token, scope(), async () => {})); await assert.rejects(saved.application(write)) + }) + }) + await t.test("nested writer borrows same kernel cohort and cannot dispatch outside original server IDs", async () => { + const f = await fixture(), writer = make() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp(scope()) + await writer.run(token, scope(), async () => { await writer.run(token, scope(), async facade => { await facade.application(write) }) }) + }) + assert.equal(new Set(events.map(event => event.pid)).size, 1); assert.equal(events.filter(event => event.event === "terminal").length, 1) + const second = await fixture() + await withVerifiedConfigurationChange(second.context, async () => { + const token = await writer.captureMcp(scope()) + await assert.rejects(writer.run(token, scope(), async facade => { await facade.native({ kind: "native", managedServerId: 72, endpoint: { host: "127.0.0.1", port: 80, username: "fixture", password: "fixture", useSsl: false, verifySsl: true }, command: {}, method: "PATCH", requestPath: "/rest/ip/address/*1" }).catch(() => {}) }), Unknown) + }) + assert.equal(events.some(event => event.event === "physical-native"), false) + }) + await t.test("revoked genuine actor/current SQL state or changed existing key/generation refuse", async () => { + for (const fault of ["actor", "row", "key", "generation", "deleted-key"] as const) { + const f = await fixture(), writer = make() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp(scope()) + await assert.rejects(writer.run(token, scope(), async facade => { + await facade.application(write) + if (fault === "actor") f.revoke() + else if (fault === "row") row!.step_state = "acknowledged" + else if (fault === "deleted-key") await unlink(keyFile) + else await writeFile(fault === "key" ? keyFile : generationFile, Buffer.alloc(32, 99)) + })) + }) + assert.equal(events.at(-1)!.event, "pending"); assert.equal(events.some(event => event.event === "terminal"), false) + if (fault === "deleted-key") await assert.rejects(readFile(keyFile)) + } + }) + await t.test("lower-lock capture, pre-abort and original scope expiry refuse before intent", async () => { + const f = await fixture(), writer = make() + await withVerifiedConfigurationChange(f.context, async () => { + lowerHeld = true; await assert.rejects(writer.captureMcp(scope())); lowerHeld = false + const controller = new AbortController(); controller.abort() + await assert.rejects(writer.captureMcp({ ...scope(), signal: controller.signal })) + await assert.rejects(writer.captureMcp({ deadlineAt: Date.now() - 1 })) + }) + assert.equal(events.length, 0) + }) + await t.test("missing existing key or generation refuses before generic authorizer without bootstrap", async () => { + for (const missing of [keyFile, generationFile]) { + const f = await fixture(), writer = make() + await unlink(missing) + await withVerifiedConfigurationChange(f.context, async () => { + const before = f.authorizations() + await assert.rejects(writer.captureMcp(scope())) + assert.equal(f.authorizations(), before) + await assert.rejects(readFile(missing), { code: "ENOENT" }) + }) + assert.equal(events.length, 0) + } + }) + await t.test("same-byte state-directory clone cannot revive captured origin before intent", async () => { + const f = await fixture(), writer = make(), clone = join(directory, "same-byte-clone") + await mkdir(clone) + await writeFile(join(clone, "encryption-key"), await readFile(keyFile)) + await writeFile(join(clone, "access-generation"), await readFile(generationFile)) + try { + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp(scope()), before = f.authorizations() + process.env.MCP_STATE_DIR = clone + await assert.rejects(writer.run(token, scope(), async facade => { await facade.application(write) })) + assert.equal(f.authorizations(), before) + }) + assert.equal(events.length, 0) + } finally { process.env.MCP_STATE_DIR = directory } + }) + await t.test("deleted captured material refuses current checks before generic authorizer can bootstrap", async () => { + for (const missing of [keyFile, generationFile]) { + const f = await fixture(), writer = make() + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp(scope()), before = f.authorizations() + await unlink(missing) + await assert.rejects(writer.run(token, scope(), async facade => { await facade.application(write) })) + assert.equal(f.authorizations(), before) + await assert.rejects(readFile(missing), { code: "ENOENT" }) + }) + assert.equal(events.length, 0) + } + }) + await t.test("genuine MCP first caller abort cannot be replaced by a new run signal or extended scope", async () => { + const f = await fixture(), writer = make(), caller = new AbortController() + await withVerifiedConfigurationChange(f.context, async () => { + const first = { deadlineAt: Date.now() + 5000, signal: caller.signal }, token = await writer.captureMcp(first) + first.deadlineAt = Date.now() + 60000; first.signal = new AbortController().signal; caller.abort() + await assert.rejects(writer.run(token, { deadlineAt: Date.now() + 60000, signal: new AbortController().signal }, async facade => { await facade.application(write) })) + }) + assert.equal(events.length, 0) + }) + await t.test("genuine MCP original short capture monotonic end survives wall rollback before run", async () => { + const f = await fixture(), writer = make(), originalNow = Date.now + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp({ deadlineAt: Date.now() + 100 }) + await delay(110) + try { + Date.now = () => originalNow() - 60000 + await assert.rejects(writer.run(token, { deadlineAt: originalNow() + 60000 }, async facade => { await facade.application(write) })) + } finally { Date.now = originalNow } + }) + assert.equal(events.length, 0) + }) + await t.test("adapter pins first external scope even if synthetic opaque provider ignores all requested lifetimes", async () => { + // Explicit opaque TEST provider, not genuine REST/scheduler provenance. + // Its deliberate scope blindness proves the adapter's independent bound. + for (const kind of ["rest", "scheduler"] as const) { + await fixture(); const issued = new WeakSet(), now = Date.now(), end = performance.now() + 60000 + const original: TrustedRouterWriterOriginSnapshot = { binding: { kind: "compound", endpoint: { owned: kind }, command: { owned: "scope-fixture" }, actorProof: "a".repeat(64) }, + authority: { actorProof: "a".repeat(64), actorExpiresAt: now + 60000, commandDeadlineAt: now + 60000, monotonicEnd: end }, serverIds: [71] } + const provider: TrustedRouterWriterOriginProvider = { async capture() { const value = Object.freeze(Object.create(null)) as object; issued.add(value); return value }, + async inspect(value) { assert(value && typeof value === "object" && issued.has(value)); return original } } + const writer = make(provider), first = { deadlineAt: Date.now() + 5000, signal: new AbortController().signal } + const token = kind === "rest" ? await writer.captureRest(first) : await writer.captureScheduler(first) + await writer.run(token, { deadlineAt: Date.now() + 60000 }, async facade => { await facade.application(write) }) + assert(observedLifetimes.length > 0) + for (const lifetime of observedLifetimes) { + assert(lifetime.deadlineAt <= first.deadlineAt); assert(lifetime.actorExpiresAt <= first.deadlineAt) + assert(lifetime.commandDeadlineAt <= first.deadlineAt); assert(lifetime.monotonicEnd < end) + } + events.length = 0 + const caller = new AbortController(), doomed = kind === "rest" ? await writer.captureRest({ deadlineAt: Date.now() + 5000, signal: caller.signal }) : await writer.captureScheduler({ deadlineAt: Date.now() + 5000, signal: caller.signal }) + caller.abort(); await assert.rejects(writer.run(doomed, { deadlineAt: Date.now() + 60000, signal: new AbortController().signal }, async facade => { await facade.application(write) })) + assert.equal(events.length, 0) + } + }) + await t.test("aborted or expired capture cannot inspect/enroll a late synthetic opaque origin", async () => { + for (const mode of ["abort", "expiry"] as const) { + await fixture(); let enter!: () => void, release!: () => void, inspections = 0 + const started = new Promise(resolve => { enter = resolve }), held = new Promise(resolve => { release = resolve }) + const provider: TrustedRouterWriterOriginProvider = { async capture() { enter(); await held; return Object.freeze(Object.create(null)) }, async inspect() { inspections++; assert.fail("late capture inspected") } } + const writer = make(provider), caller = new AbortController(), pending = writer.captureRest({ deadlineAt: Date.now() + (mode === "expiry" ? 30 : 5000), signal: caller.signal }) + await started; if (mode === "abort") caller.abort(); else await delay(40) + release(); await assert.rejects(pending); assert.equal(inspections, 0); assert.equal(events.length, 0) + } + }) + await t.test("unawaited child cannot finish outer writer; late continuation cannot mint a terminal", async () => { + const f = await fixture(), writer = make(); let release!: () => void + childGate = new Promise(done => { release = done }) + await withVerifiedConfigurationChange(f.context, async () => { + const token = await writer.captureMcp(scope()); let child: Promise | undefined + await assert.rejects(writer.run(token, scope(), async facade => { child = facade.application(write); void child.catch(() => {}) }), Unknown) + release(); await assert.rejects(child!) + }) + assert.equal(events.some(event => event.event === "terminal"), false) + }) + } finally { + childGate = undefined; await Promise.all(all.map(writer => writer.close())); pool.query = originalQuery; pool.connect = originalConnect; await closePool() + if (oldState === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = oldState + const actual = resolve(await realpath(directory)), root = resolve(await realpath(tmpdir())), stat = await lstat(directory) + assert(stat.isDirectory() && !stat.isSymbolicLink()); assert.equal(dirname(actual), root); assert.match(basename(actual), /^router-writer-context-[A-Za-z0-9]+$/) + await rm(actual, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/router-configuration-writer-context.ts b/backend/src/services/router-configuration-writer-context.ts new file mode 100644 index 00000000..5a8ebfea --- /dev/null +++ b/backend/src/services/router-configuration-writer-context.ts @@ -0,0 +1,198 @@ +/** Explicit unactivated outer-writer assembly. No startup singleton, lock hook, + * environment fallback, pool instrumentation or all-writer coverage claim. */ +import { AsyncLocalStorage } from "node:async_hooks" +import { readFile } from "node:fs/promises" +import { join } from "node:path" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import type { PoolClient } from "pg" +import { captureVerifiedMcpServiceExecution, inspectVerifiedMcpServiceExecutionLifetime } from "./config-mutation-policy.js" +import { readCurrentMcpServiceExecution } from "../modules/mcp/service-execution-origin.js" +import { mcpStateDirectory, privateFingerprinterExisting } from "../modules/mcp/security.js" +import { canonicalBandwidthNodeValue as canonical } from "./bandwidth-node-bootstrap.js" +import type { BandwidthScope } from "./bandwidth-admission.js" +import { createConfiguredRouterGuard, RouterGuardRefused, RouterGuardUnknown, type RouterGuardConfig, type RouterGuardDependencies, type RouterGuardNativeBinding, type RouterGuardWriteBinding, type RouterGuardWriterAuthority } from "./router-configuration-guard.js" + +declare const originBrand: unique symbol +declare const facadeBrand: unique symbol +export type RouterConfigurationWriterOrigin = Readonly<{ [originBrand]: K }> +export interface TrustedRouterWriterOriginSnapshot { + readonly binding: RouterGuardWriteBinding + readonly authority: RouterGuardWriterAuthority + readonly serverIds: readonly number[] +} +/** Server-owned assembly dependency, not a request callback or verified JSON. + * REST/scheduler integrations must supply their genuine opaque-origin issuer; + * no provider means that entrypoint fails before a control intent is written. */ +export interface TrustedRouterWriterOriginProvider { + capture(scope: Readonly): Promise + inspect(token: unknown, scope: Readonly): Promise +} +export type RouterConfigurationWriterDependencies = Pick & { + rest?: TrustedRouterWriterOriginProvider + scheduler?: TrustedRouterWriterOriginProvider +} +export interface RouterConfigurationWriterFacade { + readonly [facadeBrand]: true + native(binding: Omit): Promise + application(work: (client: Pick) => Promise): Promise +} +export interface RouterConfigurationWriter { + captureMcp(scope: Readonly): Promise> + captureRest(scope: Readonly): Promise> + captureScheduler(scope: Readonly): Promise> + run(token: unknown, scope: Readonly, work: (writer: RouterConfigurationWriterFacade) => Promise): Promise + close(): Promise +} +type Entry = { snapshot: Readonly; + first: Readonly<{ deadlineAt: number; monotonicEnd: number; signal?: AbortSignal }>; + assertCurrent(scope: Readonly): Promise; used: boolean } +type Frame = { entry: Entry; scope: Readonly; monotonicEnd: number; active: boolean; completed: number; outstanding: number; failed: boolean; child: boolean } +const deny = (): never => { throw new RouterGuardRefused() } +function scopeSnapshot(raw: Readonly): Readonly { + if (!raw || Object.getPrototypeOf(raw) !== Object.prototype) return deny() + const fields = Object.getOwnPropertyDescriptors(raw) + if (Reflect.ownKeys(raw).some(key => typeof key !== "string" || !["deadlineAt", "signal"].includes(key)) || Object.values(fields).some(field => !("value" in field) || !field.enumerable)) return deny() + const deadlineAt: unknown = fields.deadlineAt?.value, signal: unknown = fields.signal?.value + if (!Number.isSafeInteger(deadlineAt) || signal !== undefined && !(signal instanceof AbortSignal)) return deny() + return Object.freeze({ deadlineAt: deadlineAt as number, signal: signal as AbortSignal | undefined }) +} +function snapshot(raw: TrustedRouterWriterOriginSnapshot): Readonly { + // Providers are trusted code, but their mutable results must not become a + // revocable/renewable reference after an await. + const value = structuredClone(raw) + const a = value.authority + if (!a || !/^[a-f0-9]{64}$/.test(a.actorProof) || value.binding?.actorProof !== a.actorProof || ![a.actorExpiresAt, a.commandDeadlineAt].every(Number.isSafeInteger) || !Number.isFinite(a.monotonicEnd) + || !Array.isArray(value.serverIds) || value.serverIds.some(id => !Number.isSafeInteger(id) || id <= 0) || new Set(value.serverIds).size !== value.serverIds.length || Buffer.byteLength(canonical(value)) > 1048576) return deny() + const freeze = (item: unknown): void => { if (item && typeof item === "object") { Object.values(item).forEach(freeze); Object.freeze(item) } } + freeze(value); return value +} + +export function createConfiguredRouterConfigurationWriter(config: RouterGuardConfig, dependencies: RouterConfigurationWriterDependencies): RouterConfigurationWriter { + const lower = dependencies.assertNoLowerLockContext.bind(dependencies), contexts = new AsyncLocalStorage(), origins = new WeakMap() + const captureProvider = (provider: TrustedRouterWriterOriginProvider | undefined) => provider ? Object.freeze({ capture: provider.capture.bind(provider), inspect: provider.inspect.bind(provider) }) : undefined + const providers = { rest: captureProvider(dependencies.rest), scheduler: captureProvider(dependencies.scheduler) } + let closed = false + const startCapture = (raw: Readonly) => { + const scope = scopeSnapshot(raw), wall = Date.now(), mono = performance.now(), end = mono + Math.max(0, scope.deadlineAt - wall) + const preflight = () => { if (closed || scope.signal?.aborted || Date.now() >= scope.deadlineAt || performance.now() >= end) deny() } + preflight(); return Object.freeze({ scope, end, preflight }) + } + const pin = (original: Readonly, started: ReturnType, signal?: AbortSignal) => ({ + first: Object.freeze({ deadlineAt: started.scope.deadlineAt, monotonicEnd: started.end, signal }), + snapshot: snapshot({ ...original, authority: { ...original.authority, actorExpiresAt: Math.min(original.authority.actorExpiresAt, started.scope.deadlineAt), + commandDeadlineAt: Math.min(original.authority.commandDeadlineAt, started.scope.deadlineAt), monotonicEnd: Math.min(original.authority.monotonicEnd, started.end) } }), + }) + const live = (entry: Entry, scope: Readonly) => { + const a = entry.snapshot.authority + if (closed || scope.signal?.aborted || entry.first.signal?.aborted || Date.now() >= Math.min(scope.deadlineAt, entry.first.deadlineAt, a.actorExpiresAt, a.commandDeadlineAt) + || performance.now() >= Math.min(a.monotonicEnd, entry.first.monotonicEnd)) deny() + } + const current = async (binding: Readonly | undefined, scope: Readonly) => { + const frame = contexts.getStore(); if (!frame?.active) return deny() + live(frame.entry, scope); if (performance.now() >= frame.monotonicEnd) deny() + if (binding && binding.actorProof !== frame.entry.snapshot.authority.actorProof) return deny() + await frame.entry.assertCurrent(scope); live(frame.entry, scope); if (performance.now() >= frame.monotonicEnd) deny() + return frame.entry + } + // Internally create the genuine configured kernel. A caller cannot substitute + // a structural fake guard or manufacture its private terminal receipts. + const guard = createConfiguredRouterGuard(config, { applicationPool: dependencies.applicationPool, nativeTransport: dependencies.nativeTransport, + assertNoLowerLockContext: lower, authorize: async (binding, scope) => { await current(binding, scope) }, + readOriginalWriterAuthority: async (binding, scope) => (await current(binding, scope)).snapshot.authority }) + const enroll = (entry: Entry): RouterConfigurationWriterOrigin => { + const token = Object.freeze(Object.create(null)) as RouterConfigurationWriterOrigin; origins.set(token, entry); return token + } + const externalCapture = async (kind: K, raw: Readonly): Promise> => { + lower(); if (closed || contexts.getStore()) return deny() + const started = startCapture(raw), { scope, preflight } = started, provider = providers[kind]; if (!provider) return deny() + const token = await provider.capture(scope); preflight() + const original = snapshot(await provider.inspect(token, scope)); preflight() + const entry: Entry = { ...pin(original, started, scope.signal), used: false, async assertCurrent(currentScope) { + live(entry, currentScope); if (!isDeepStrictEqual(snapshot(await provider.inspect(token, currentScope)), original)) deny(); live(entry, currentScope) + } } + live(entry, scope); await entry.assertCurrent(scope); live(entry, scope); return enroll(entry) + } + const facade = (frame: Frame, scope: Readonly): RouterConfigurationWriterFacade => { + const check = () => { if (!frame.active || contexts.getStore() !== frame || frame.child || performance.now() >= frame.monotonicEnd) deny(); live(frame.entry, scope) } + const child = async (work: () => Promise): Promise => { + check(); if (frame.outstanding) { frame.failed = true; return deny() }; frame.outstanding++ + try { const result = await work(); check(); frame.completed++; return result } + catch (error) { frame.failed = true; throw error } finally { frame.outstanding-- } + } + return Object.freeze({ + native(binding: Omit) { + check(); if (!frame.entry.snapshot.serverIds.includes(binding.managedServerId)) { frame.failed = true; return Promise.reject(new RouterGuardRefused()) } + return child(() => guard.runNative({ ...binding, actorProof: frame.entry.snapshot.authority.actorProof }, scope)) + }, + application(work: (client: Pick) => Promise) { + return child(() => guard.runApplication({ ...frame.entry.snapshot.binding, kind: "application" }, scope, work)) + }, + }) as RouterConfigurationWriterFacade + } + return Object.freeze({ + /** Genuine current MCP service origin: no input/approved flag supplied by + * caller, and existing key/generation are READ ONLY (never bootstrapped). */ + async captureMcp(raw: Readonly): Promise> { + lower(); if (closed || contexts.getStore()) return deny() + const sourceDirectory = mcpStateDirectory(), started = startCapture(raw), { scope } = started + const preflight = () => { started.preflight(); if (mcpStateDirectory() !== sourceDirectory) deny() } + const purpose = "router-configuration-writer-origin-v1", keyMessage = "router-configuration-writer-origin-key-epoch-v1" + // Existing material must be present before the generic authorizer, which + // may use bootstrap-capable MCP security helpers for other operations. + preflight(); const generation = await readFile(join(sourceDirectory, "access-generation")); preflight(); if (generation.length !== 32) return deny() + const sign = await privateFingerprinterExisting(purpose); preflight(); const keyProof = sign(keyMessage) + const token = await captureVerifiedMcpServiceExecution(); preflight() + const metadata = await readCurrentMcpServiceExecution(token, scope); preflight() + const lifetime = await inspectVerifiedMcpServiceExecutionLifetime(token); preflight() + const command = { operationId: metadata.operationId, planId: metadata.planId, operationName: metadata.operationName, serviceName: metadata.serviceName, stepIndex: metadata.stepIndex, + operationVersion: metadata.operationVersion, policyVersion: metadata.policyVersion, generation: metadata.generation, sealedEvidenceProof: metadata.sealedEvidenceProof, + sealedPayloadProof: metadata.sealedPayloadProof, originalTemporaryGrantProof: metadata.originalTemporaryGrantProof, serviceInput: metadata.serviceInput } + const actorProof = sign(canonical({ ownerId: metadata.ownerId, connectionId: metadata.connectionId, command })) + const original = snapshot({ binding: { kind: "compound", endpoint: { domain: "mcp-service", serverIds: metadata.serverIds }, command, actorProof }, + authority: { actorProof, actorExpiresAt: lifetime.expiresAt, commandDeadlineAt: metadata.originalExpiresAt, monotonicEnd: lifetime.monotonicDeadline }, serverIds: metadata.serverIds }) + const signal = scope.signal ? AbortSignal.any([scope.signal, lifetime.signal]) : lifetime.signal + const entry: Entry = { ...pin(original, started, signal), used: false, async assertCurrent(currentScope) { + const check = () => { live(entry, currentScope); if (mcpStateDirectory() !== sourceDirectory) deny() } + const existingMaterial = async () => { + check(); if (!(await readFile(join(sourceDirectory, "access-generation"))).equals(generation)) return deny(); check() + if ((await privateFingerprinterExisting(purpose))(keyMessage) !== keyProof) return deny(); check() + } + check() + await existingMaterial(); check() + if (!isDeepStrictEqual(await readCurrentMcpServiceExecution(token, currentScope), metadata)) return deny(); check() + const currentLifetime = await inspectVerifiedMcpServiceExecutionLifetime(token); check() + if (currentLifetime.expiresAt !== lifetime.expiresAt || currentLifetime.monotonicDeadline !== lifetime.monotonicDeadline || currentLifetime.signal !== lifetime.signal) return deny() + await existingMaterial(); check() + if (!isDeepStrictEqual(await readCurrentMcpServiceExecution(token, currentScope), metadata)) return deny(); check() + } } + await entry.assertCurrent(scope); live(entry, scope); return enroll<"mcp">(entry) + }, + captureRest: (scope: Readonly) => externalCapture("rest", scope), + captureScheduler: (scope: Readonly) => externalCapture("scheduler", scope), + async run(token: unknown, raw: Readonly, work: (writer: RouterConfigurationWriterFacade) => Promise): Promise { + const entry = token && typeof token === "object" ? origins.get(token) : undefined + if (!entry) return deny() + const captured = scopeSnapshot(raw), parent = contexts.getStore() + if (parent) { if (!parent.active || parent.entry !== entry || parent.child || parent.outstanding) return deny(); parent.child = true } + else { lower(); if (entry.used) return deny(); entry.used = true } + const deadlineAt = Math.min(captured.deadlineAt, entry.first.deadlineAt, entry.snapshot.authority.actorExpiresAt, entry.snapshot.authority.commandDeadlineAt, parent?.scope.deadlineAt ?? Infinity) + const signals = [...new Set([captured.signal, entry.first.signal, parent?.scope.signal].filter((signal): signal is AbortSignal => !!signal))] + const scope = Object.freeze({ deadlineAt, signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0] }) + const monotonicEnd = Math.min(entry.first.monotonicEnd, entry.snapshot.authority.monotonicEnd, parent?.monotonicEnd ?? Infinity, performance.now() + Math.max(0, deadlineAt - Date.now())) + const frame: Frame = { entry, scope, monotonicEnd, active: true, completed: 0, outstanding: 0, failed: false, child: false } + try { + live(entry, scope); await entry.assertCurrent(scope); live(entry, scope) + const result = await contexts.run(frame, () => guard.runCompound(entry.snapshot.binding, scope, async () => { + const value = await work(facade(frame, scope)) + if (frame.failed || frame.outstanding || frame.child || frame.completed === 0) throw new RouterGuardUnknown() + await entry.assertCurrent(scope); live(entry, scope); return value + })) + if (parent) parent.completed += frame.completed + return result + } catch (error) { if (parent) parent.failed = true; throw error } + finally { frame.active = false; if (parent) parent.child = false } + }, + async close() { closed = true; await guard.close() }, + }) +} diff --git a/backend/src/services/router-logs-collector.ts b/backend/src/services/router-logs-collector.ts index 1ce99648..1763e897 100644 --- a/backend/src/services/router-logs-collector.ts +++ b/backend/src/services/router-logs-collector.ts @@ -1,3 +1,5 @@ +import { createAutomaticSchedulerTicket, runAutomaticSchedulerTicket } from "./scheduler-automatic-root.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" import type { LogSettings } from "@mmapp/contracts" import { pool, dbAll } from "../db/index.js" import type { Server } from "../db/schema.js" @@ -5,6 +7,7 @@ import { listEnabledMonitoringServers } from "./monitoring-server-catalog.js" import { MikrotikClient } from "./mikrotik.js" import { logLevel, logSignature, parseLogTime, reconcileLogSnapshot } from "./router-logs-model.js" import { endSchedulerJob, isSchedulerJobsPaused, tryBeginSchedulerJob } from "./scheduler-running.js" +import { PartitionWriterBusy, tryAdmitPartitionWriter, withBackgroundPartitionWrite } from "./partition-write-transaction.js" import type { RouterLogsRunSnapshot } from "../types/scheduler-run-snapshot.js" export async function getLogSettings(): Promise { @@ -12,7 +15,8 @@ export async function getLogSettings(): Promise { if (!rows[0]) throw new Error("Настройки логов не найдены; проверьте миграцию БД") return rows[0] } -export async function updateLogSettings(patch: Partial) { +export function updateLogSettings(patch: Partial) { return executeGuardedApplicationWrite({ kind: "logs-settings", id: "1" }, "all", () => updateLogSettingsImpl(patch)) } +async function updateLogSettingsImpl(patch: Partial) { await pool.query(`UPDATE router_log_settings SET enabled=COALESCE($1,enabled), interval_sec=COALESCE($2,interval_sec), retention_days=COALESCE($3,retention_days) WHERE id=1`, [patch.enabled, patch.intervalSec, patch.retentionDays]) return getLogSettings() } @@ -34,6 +38,7 @@ async function collectDevice(server: Server, settings: LogSettings, force: boole const client = await pool.connect().catch(error => { release(); throw error }) try { await client.query("BEGIN") + if (!await tryAdmitPartitionWriter(client)) { await client.query("ROLLBACK"); return { added: 0, gap: false } } const lock = await client.query<{ locked: boolean }>("SELECT pg_try_advisory_xact_lock(872314,$1) AS locked", [server.id]) if (!lock.rows[0].locked || isSchedulerJobsPaused()) { await client.query("ROLLBACK"); return { added: 0, gap: false } } const admission = await client.query<{ enabled: boolean }>("SELECT enabled FROM router_log_settings WHERE id=1") @@ -54,8 +59,6 @@ async function collectDevice(server: Server, settings: LogSettings, force: boole await client.query("COMMIT") return { added: 0, gap: false, error: message } } - const current = await client.query("SELECT id FROM servers WHERE id=$1 AND enabled=true AND updated_at=$2", [server.id, server.updatedAt]) - if (!current.rowCount) { await client.query("ROLLBACK"); return { added: 0, gap: false } } const snapshot = rows.map(logSignature) const { offset, gap } = reconcileLogSnapshot(prev.snapshot, snapshot) const generation = prev.generation + (gap ? 1 : 0) @@ -65,6 +68,10 @@ async function collectDevice(server: Server, settings: LogSettings, force: boole const eventAt = parseLogTime(row.time) return { server_id: server.id, server_name: server.name, host: server.host, router_id: row[".id"], raw_time: row.time, event_at: eventAt, received_at: receivedAt, effective_at: eventAt ?? receivedAt, topics, level: logLevel(topics), message: row.message, generation } }) + // Keep global history watermarks stable by ordering ID allocation through commit. + if (newRows.length) await client.query("SELECT pg_advisory_xact_lock(872315,1)") + const current = await client.query("SELECT id FROM servers WHERE id=$1 AND enabled=true AND updated_at=$2", [server.id, server.updatedAt]) + if (!current.rowCount) { await client.query("ROLLBACK"); return { added: 0, gap: false } } if (newRows.length) await client.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,event_at,received_at,effective_at,topics,level,message,generation) SELECT server_id,server_name,host,router_id,raw_time,event_at,received_at,effective_at,topics,level,message,generation FROM jsonb_to_recordset($1::jsonb) AS x(server_id int,server_name text,host text,router_id text,raw_time text,event_at timestamptz,received_at timestamptz,effective_at timestamptz,topics text[],level text,message text,generation int)`, [JSON.stringify(newRows)]) @@ -93,36 +100,52 @@ export async function collectRouterLogsOnce(): Promise { catch (error) { return { serverId: server.id, name: server.name, added: 0, gap: false, error: error instanceof Error ? error.message : String(error) } } })) result.errors = result.servers.flatMap(row => row.error ? [`${row.name}: ${row.error}`] : []) - await pruneRouterLogHistory(settings.retentionDays) + await pruneRouterLogHistory() return result } let timer: ReturnType | undefined let retentionTimer: ReturnType | undefined -export async function pruneRouterLogHistory(retentionDays: number) { - await pool.query(`DELETE FROM router_log_entries WHERE id IN (SELECT id FROM router_log_entries WHERE received_at < now() - ($1 * interval '1 day') ORDER BY received_at LIMIT 10000)`, [retentionDays]) +async function pruneRouterLogHistory() { + try { + await withBackgroundPartitionWrite(async ({ client }) => { + // A concurrent settings update must commit before this cutoff is read or after deletion. + const { rows: [settings] } = await client.query<{ retention_days: number }>("SELECT retention_days FROM router_log_settings WHERE id=1 FOR SHARE") + if (!settings) throw new Error("Настройки логов не найдены; проверьте миграцию БД") + const retentionDays = settings.retention_days + await client.query(`DELETE FROM router_log_entries WHERE id IN (SELECT id FROM router_log_entries WHERE received_at < now() - ($1 * interval '1 day') ORDER BY received_at LIMIT 10000)`, [retentionDays]) + }) + } catch (error) { + if (!(error instanceof PartitionWriterBusy)) throw error + } } export function stopLogsLiveTimer() { if (timer) clearInterval(timer); if (retentionTimer) clearInterval(retentionTimer); timer = undefined; retentionTimer = undefined } -export function startLogsLiveTimer() { +export async function runRouterLogsRetentionOnce(): Promise { + if (!tryBeginSchedulerJob("router_logs_retention")) return + try { await pruneRouterLogHistory() } + finally { endSchedulerJob("router_logs_retention") } +} +export async function runRouterLogsLiveOnce(): Promise { + if (!liveDevices.size || !tryBeginSchedulerJob("router_logs_live")) return + try { + const settings = await getLogSettings() + if (!settings.enabled) return + const servers = (await listEnabledMonitoringServers()).filter(s => liveDevices.has(s.id)) + await Promise.allSettled(servers.map(server => collectShared(server, settings))) + } finally { endSchedulerJob("router_logs_live") } +} +export function startLogsLiveTimer(generation: number) { stopLogsLiveTimer() - // Retention continues even when collection is disabled, and drains before DB maintenance. - const prune = () => { - if (!tryBeginSchedulerJob("router_logs_retention")) return - void getLogSettings().then(settings => pruneRouterLogHistory(settings.retentionDays)).catch(() => {}).finally(() => endSchedulerJob("router_logs_retention")) - } - retentionTimer = setInterval(prune, 60_000); retentionTimer.unref() - timer = setInterval(() => { - if (!liveDevices.size || !tryBeginSchedulerJob("router_logs_live")) return - void (async () => { - try { - const settings = await getLogSettings() - if (!settings.enabled) return - const servers = (await listEnabledMonitoringServers()).filter(s => liveDevices.has(s.id)) - await Promise.allSettled(servers.map(server => collectShared(server, settings))) - } finally { endSchedulerJob("router_logs_live") } - })().catch(() => {}) - }, 1000) - timer.unref() + const retention = createAutomaticSchedulerTicket("router_logs_retention", generation) + const live = createAutomaticSchedulerTicket("router_logs_live", generation) + // Retention remains active when collection is disabled; both own fresh DB/job lifetimes. + retentionTimer = setInterval(() => { void runAutomaticSchedulerTicket(retention).catch(() => {}) }, 60_000); retentionTimer.unref() + timer = setInterval(() => { void runAutomaticSchedulerTicket(live).catch(() => {}) }, 1000); timer.unref() } export const logStateSql = `SELECT server_id AS "serverId", server_name AS "serverName", host, generation, last_collected_at AS "lastCollectedAt", last_error AS "lastError", gap_at AS "gapAt", next_poll_at AS "nextPollAt", failures FROM router_log_collection_state` -export async function getLogStates(serverId?: number) { return dbAll(logStateSql + (serverId ? " WHERE server_id=$1" : " ORDER BY server_name"), serverId ? [serverId] : []) } +export async function getLogStates(serverId?: number, allowedServerIds?: number[]) { + const values: unknown[] = [], clauses: string[] = [] + if (serverId !== undefined) { values.push(serverId); clauses.push(`server_id=$${values.length}`) } + if (allowedServerIds !== undefined) { values.push(allowedServerIds); clauses.push(`server_id=ANY($${values.length}::bigint[])`) } + return dbAll(logStateSql + (clauses.length ? " WHERE " + clauses.join(" AND ") : "") + " ORDER BY server_name", values) +} diff --git a/backend/src/services/router-logs-export.test.ts b/backend/src/services/router-logs-export.test.ts new file mode 100644 index 00000000..7ffa380b --- /dev/null +++ b/backend/src/services/router-logs-export.test.ts @@ -0,0 +1,36 @@ +import assert from "node:assert/strict" +import { MAX_LOG_EXPORT_BYTES, serializeLogExport, LogExportLimitError, withLogExportSlot, LogExportBusyError } from "./router-logs-export.js" +import { MAX_LOG_ARTIFACT_CHUNK_BYTES, assertLogArtifactBudget, MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION, MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES } from "../modules/mcp/log-artifacts.js" +import { MAX_MCP_PUBLIC_STRING_LENGTH, redact } from "../modules/mcp/security.js" + +const date = new Date("2026-10-21T00:00:00.123Z") +const rows = [{ effectiveAt: date, receivedAt: date, serverName: "=untrusted", host: "192.0.2.1", message: 'line "one"\n😀', topics: ["system", "info"], level: "info", rawTime: "00:00:00", timeKind: "receipt" }] +assert.equal(JSON.parse(serializeLogExport(rows, "json"))[0].effectiveAt, date.toISOString()) +assert.equal(JSON.parse(serializeLogExport(rows, "json"))[0].host, "192.0.2.1") +const [scopedJson] = JSON.parse(serializeLogExport(rows, "json", false)) +assert.equal(Object.hasOwn(scopedJson, "host"), false) +assert.equal(scopedJson.message, rows[0].message) +const csv = serializeLogExport(rows, "csv"), scopedCsv = serializeLogExport(rows, "csv", false) +assert.ok(csv.startsWith("\uFEFF")); assert.ok(csv.split("\r\n")[0].includes('"host"')) +assert.ok(!scopedCsv.split("\r\n")[0].includes('"host"')); assert.ok(csv.includes("'=untrusted")); assert.ok(csv.includes('""one""')) +assert.throws(() => serializeLogExport([{ message: "x".repeat(MAX_LOG_EXPORT_BYTES + 1) }], "json"), (error: unknown) => error instanceof LogExportLimitError && error.kind === "bytes") +for (const length of [0, 1, 2, 100, MAX_LOG_ARTIFACT_CHUNK_BYTES - 1, MAX_LOG_ARTIFACT_CHUNK_BYTES]) { + const data = Buffer.alloc(length, 255).toString("base64") + assert.ok(data.length <= MAX_MCP_PUBLIC_STRING_LENGTH) + assert.equal((redact({ dataBase64: data }) as { dataBase64: string }).dataBase64, data, "Binary chunks survive the actual public response/audit redactor") +} +let release!: () => void +const gate = new Promise(resolve => { release = resolve }), first = withLogExportSlot(() => gate), second = withLogExportSlot(() => gate) +await assert.rejects(withLogExportSlot(async () => {}), LogExportBusyError) +release(); await Promise.all([first, second]) +await assert.rejects(withLogExportSlot(async () => { throw Error("owned failure") }), /owned failure/) +await withLogExportSlot(async () => {}) +const quotaError = (error: { code?: string }) => error.code === "EXPORT_QUOTA_EXCEEDED" +assertLogArtifactBudget(7n, 0n, 0n, 1) +assert.throws(() => assertLogArtifactBudget(8n, 0n, 0n, 1), quotaError) +assertLogArtifactBudget(0n, BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION - 1), BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION - 1), 1) +assert.throws(() => assertLogArtifactBudget(0n, BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION), BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION), 1), quotaError) +assertLogArtifactBudget(0n, 0n, BigInt(MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES - 1), 1) +assert.throws(() => assertLogArtifactBudget(0n, 0n, BigInt(MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES), 1), quotaError) +assert.throws(() => assertLogArtifactBudget(0n, 2n, 1n, 1), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") +console.log("Log export serialization: native timestamp/CSV parity, host projection, formula/quote safety, byte budget and full-sized base64 response survive redaction") diff --git a/backend/src/services/router-logs-export.ts b/backend/src/services/router-logs-export.ts new file mode 100644 index 00000000..8ef085f3 --- /dev/null +++ b/backend/src/services/router-logs-export.ts @@ -0,0 +1,53 @@ +import { pool } from "../db/index.js" +import { logColumns } from "./router-logs-query.js" +import type { logWhere } from "./router-logs-model.js" +import { csvCell } from "./router-logs-model.js" + +export const MAX_LOG_EXPORT_ROWS = 50000 +export const MAX_LOG_EXPORT_BYTES = 8 * 1024 * 1024 +export class LogExportBusyError extends Error { + readonly statusCode = 429 + constructor() { super("Экспорт уже выполняется; повторите запрос позже") } +} +let activeExports = 0 +export async function withLogExportSlot(run: () => Promise): Promise { + if (activeExports >= 2) throw new LogExportBusyError() + activeExports++ + try { return await run() } finally { activeExports-- } +} +export class LogExportLimitError extends Error { + readonly statusCode = 422 + constructor(readonly kind: "rows" | "bytes" = "bytes") { super(kind === "rows" ? "Найдено более 50 000 событий; сузьте период или фильтры" : "Экспорт превышает 8 MiB; сузьте период или фильтры") } +} +/** One SQL snapshot computes the source budget before sending any event payload + * to Node. The lateral gate also preserves native timestamp/column types. */ +export async function readBoundedLogExportRows(where: ReturnType): Promise[]> { + const { rows } = await pool.query(`WITH chosen AS MATERIALIZED ( + SELECT ${logColumns} FROM router_log_entries WHERE ${where.sql} + ORDER BY effective_at DESC,router_log_entries.id DESC LIMIT ${MAX_LOG_EXPORT_ROWS + 1} + ), budget AS ( + SELECT count(*)::int AS n,COALESCE(sum(octet_length(row_to_json(chosen)::text)),0)::text AS bytes FROM chosen + ) SELECT budget.n AS "__exportRows",budget.bytes AS "__exportBytes",chosen.* FROM budget + LEFT JOIN LATERAL (SELECT * FROM chosen WHERE budget.n<=${MAX_LOG_EXPORT_ROWS} + AND budget.bytes::numeric<=${MAX_LOG_EXPORT_BYTES}) AS chosen ON true + ORDER BY chosen."effectiveAt" DESC,chosen.id::bigint DESC`, where.values) + const count = rows[0]?.__exportRows, bytes = rows[0]?.__exportBytes + if (!Number.isSafeInteger(count) || count < 0 || !/^\d+$/.test(String(bytes))) throw Error("Не подтверждён бюджет экспорта журнала") + if (count > MAX_LOG_EXPORT_ROWS || BigInt(bytes) > BigInt(MAX_LOG_EXPORT_BYTES)) { + if (rows.length !== 1 || rows[0].id !== null) throw Error("Источник вернул события вне бюджета экспорта") + throw new LogExportLimitError(count > MAX_LOG_EXPORT_ROWS ? "rows" : "bytes") + } + if (count === 0) { if (rows.length !== 1 || rows[0].id !== null) throw Error("Не подтверждён пустой экспорт"); return [] } + if (rows.length !== count || rows.some(row => row.id === null || row.__exportRows !== count || row.__exportBytes !== bytes)) throw Error("Не подтверждён полный источник экспорта журнала") + return rows.map(({ __exportRows: _count, __exportBytes: _bytes, ...row }) => row) +} + +export function serializeLogExport(rows: Record[], format: "json" | "csv", includeHost = true) { + const columns = ["effectiveAt", "serverName", ...(includeHost ? ["host"] : []), "level", "topics", "message", "rawTime", "receivedAt", "timeKind"] + const content = format === "json" ? JSON.stringify(includeHost ? rows : rows.map(({ host: _host, ...row }) => row)) : "\uFEFF" + [columns.map(csvCell).join(","), ...rows.map(row => columns.map(key => { + const value = row[key] + return csvCell(Array.isArray(value) ? value.join(",") : value instanceof Date ? value.toISOString() : value) + }).join(","))].join("\r\n") + if (Buffer.byteLength(content, "utf8") > MAX_LOG_EXPORT_BYTES) throw new LogExportLimitError() + return content +} diff --git a/backend/src/services/router-logs-model.ts b/backend/src/services/router-logs-model.ts index 74e65164..5d6b43cf 100644 --- a/backend/src/services/router-logs-model.ts +++ b/backend/src/services/router-logs-model.ts @@ -31,10 +31,19 @@ export function parseLogTime(raw: string): string | null { const ms = Date.parse(raw.replace(" ", "T")) return Number.isFinite(ms) ? new Date(ms).toISOString() : null } -export function logWhere(filters: LogFilters, extra: string[] = [], initial: unknown[] = []) { +type LogServerScope = Pick & { allowedServerIds?: number[] } + +export function logServerConditions({ allowedServerIds, serverId }: LogServerScope, bind: (value: unknown) => string) { + const clauses: string[] = [] + if (allowedServerIds !== undefined) clauses.push(`server_id = ANY(${bind(allowedServerIds)}::bigint[])`) + if (serverId) clauses.push(`server_id = ${bind(serverId)}`) + return clauses +} + +export function logWhere(filters: LogFilters & LogServerScope, extra: string[] = [], initial: unknown[] = []) { const values = [...initial], clauses = [...extra] const bind = (value: unknown) => { values.push(value); return `$${values.length}` } - if (filters.serverId) clauses.push(`server_id = ${bind(filters.serverId)}`) + clauses.push(...logServerConditions(filters, bind)) if (filters.from) clauses.push(`effective_at >= ${bind(filters.from)}::timestamptz`) if (filters.to) clauses.push(`effective_at <= ${bind(filters.to)}::timestamptz`) if (filters.levels) clauses.push(`level = ANY(${bind(filters.levels.split(","))}::text[])`) diff --git a/backend/src/services/router-logs-query.ts b/backend/src/services/router-logs-query.ts index f2a61680..20f73dd0 100644 --- a/backend/src/services/router-logs-query.ts +++ b/backend/src/services/router-logs-query.ts @@ -1,37 +1,45 @@ import type { PoolClient } from "pg" -import { logLevels, type LogEntry, type LogFilters } from "@mmapp/contracts" +import { isLogSequenceId, logLevels, type LogEntry, type LogFilters } from "@mmapp/contracts" import { pool } from "../db/index.js" import { getLogSettings, getLogStates } from "./router-logs-collector.js" -import { logWhere } from "./router-logs-model.js" +import { logServerConditions, logWhere } from "./router-logs-model.js" export const logColumns = `id::text, server_id AS "serverId",server_name AS "serverName",host,router_id AS "routerId",raw_time AS "rawTime",event_at AS "eventAt",received_at AS "receivedAt",effective_at AS "effectiveAt",CASE WHEN event_at IS NULL THEN 'receipt' ELSE 'router' END AS "timeKind",topics,level,message,generation` export function encodeLogCursor(row: LogEntry) { return Buffer.from(JSON.stringify([row.effectiveAt, row.id])).toString("base64url") } export function decodeLogCursor(cursor: string): [string, string] { try { const value = JSON.parse(Buffer.from(cursor, "base64url").toString()) - if (Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && Number.isFinite(Date.parse(value[0])) && /^\d+$/.test(value[1])) return value as [string, string] + if (Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && Number.isFinite(Date.parse(value[0])) && isLogSequenceId(value[1])) return [value[0], value[1]] } catch { /* invalid cursor */ } throw Object.assign(new Error("Некорректный курсор журнала"), { statusCode: 400 }) } -export async function logWatermark(client: PoolClient, serverId?: number) { - const res = await client.query<{ id: string }>(`SELECT COALESCE(max(id),0)::text AS id FROM router_log_entries${serverId ? " WHERE server_id=$1" : ""}`, serverId ? [serverId] : []) +export async function logWatermark(client: PoolClient, serverId?: number, allowedServerIds?: number[]) { + const values: unknown[] = [] + const clauses = logServerConditions({ serverId, allowedServerIds }, value => { values.push(value); return `$${values.length}` }) + const where = clauses.length ? ` WHERE ${clauses.join(" AND ")}` : "" + const res = await client.query<{ id: string }>(`SELECT COALESCE(max(id),0)::text AS id FROM router_log_entries${where}`, values) return res.rows[0].id } -export async function readLogPage(filters: LogFilters, options: { cursor?: string; watermark?: string; limit: number }) { +export async function readLogPage(filters: LogFilters & { allowedServerIds?: number[] }, options: { cursor?: string; watermark?: string; limit: number }) { + const cursor = options.cursor ? decodeLogCursor(options.cursor) : undefined const client = await pool.connect() try { await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") - const watermark = options.watermark ?? await logWatermark(client, filters.serverId) + const watermark = options.watermark ?? await logWatermark(client, filters.serverId, filters.allowedServerIds) const conditions = ["id <= $1::bigint"], values: unknown[] = [watermark] - if (options.cursor) { const [at, id] = decodeLogCursor(options.cursor); conditions.push("(effective_at,id) < ($2::timestamptz,$3::bigint)"); values.push(at, id) } + if (cursor) { conditions.push("(effective_at,id) < ($2::timestamptz,$3::bigint)"); values.push(...cursor) } const where = logWhere(filters, conditions, values) - const rows = (await client.query(`SELECT ${logColumns} FROM router_log_entries WHERE ${where.sql} ORDER BY effective_at DESC,id DESC LIMIT $${where.values.length + 1}`, [...where.values, options.limit + 1])).rows + const rows = (await client.query(`SELECT ${logColumns}, + to_char(effective_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS "cursorTime" + FROM router_log_entries WHERE ${where.sql} ORDER BY effective_at DESC,router_log_entries.id DESC LIMIT $${where.values.length + 1}`, [...where.values, options.limit + 1])).rows await client.query("COMMIT") - const entries = rows.slice(0, options.limit) - return { entries, nextCursor: rows.length > options.limit ? encodeLogCursor(entries[entries.length - 1]) : null, watermark } + const page = rows.slice(0, options.limit) + const last = page.at(-1) + const nextCursor = rows.length > options.limit && last ? encodeLogCursor({ ...last, effectiveAt: last.cursorTime }) : null + return { entries: page.map(({ cursorTime: _cursorTime, ...entry }) => entry), nextCursor, watermark } } catch (error) { await client.query("ROLLBACK"); throw error } finally { client.release() } } -export async function readLogSummary(filters: LogFilters) { +export async function readLogSummary(filters: LogFilters & { allowedServerIds?: number[] }) { const where = logWhere(filters) const from = filters.from ? Date.parse(filters.from) : Date.now() - 900_000 const to = filters.to ? Date.parse(filters.to) : Date.now() @@ -46,23 +54,33 @@ export async function readLogSummary(filters: LogFilters) { if (!buckets.has(at)) buckets.set(at, { at, ...Object.fromEntries(logLevels.map(l => [l, 0])) } as { at: string } & typeof levels) buckets.get(at)![level] += row.count } - return { total: Object.values(levels).reduce((a, b) => a + b, 0), levels, buckets: [...buckets.values()], settings: await getLogSettings(), states: await getLogStates(filters.serverId) } + return { total: Object.values(levels).reduce((a, b) => a + b, 0), levels, buckets: [...buckets.values()], settings: await getLogSettings(), states: await getLogStates(filters.serverId, filters.allowedServerIds) } } -export async function readLogEntry(id: string) { - return (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE id=$1`, [id])).rows[0] ?? null +export async function readLogEntry(id: string, serverId?: number) { + const scoped = serverId !== undefined + return (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE id=$1${scoped ? " AND server_id=$2" : ""}`, scoped ? [id, serverId] : [id])).rows[0] ?? null } -export async function readLogContext(id: string) { - const entry = await readLogEntry(id) +export async function readLogContext(id: string, serverId?: number) { + const entry = await readLogEntry(id, serverId) if (!entry) return null - const before = (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE server_id=$1 AND (effective_at,id)<($2::timestamptz,$3::bigint) ORDER BY effective_at DESC,id DESC LIMIT 10`, [entry.serverId, entry.effectiveAt, id])).rows - const after = (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE server_id=$1 AND (effective_at,id)>($2::timestamptz,$3::bigint) ORDER BY effective_at,id LIMIT 10`, [entry.serverId, entry.effectiveAt, id])).rows + // Keep the anchor in PostgreSQL so its timestamp retains microsecond precision. + const anchor = "(SELECT effective_at,id FROM router_log_entries WHERE server_id=$1 AND id=$2)" + const before = (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE server_id=$1 AND (effective_at,id)<${anchor} ORDER BY effective_at DESC,router_log_entries.id DESC LIMIT 10`, [entry.serverId, id])).rows + const after = (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE server_id=$1 AND (effective_at,id)>${anchor} ORDER BY effective_at,router_log_entries.id LIMIT 10`, [entry.serverId, id])).rows return { entry, before: before.reverse(), after } } -export async function readLogStream(filters: LogFilters, cursor: string) { +export async function readLogStream(filters: LogFilters, cursor: string, limit = 200, signal?: AbortSignal) { // A committed upper bound is safe per device: writers are serialized by its advisory lock. // Advance across non-matching events too, so a quiet filtered stream never rescans its backlog. + signal?.throwIfAborted() const { rows: [upper] } = await pool.query<{ watermark: string }>(`SELECT GREATEST(COALESCE(max(id),0),$2::bigint)::text AS watermark FROM router_log_entries WHERE server_id=$1`, [filters.serverId, cursor]) + signal?.throwIfAborted() const where = logWhere(filters, ["id>$1::bigint", "id<=$2::bigint"], [cursor, upper.watermark]) - const entries = (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE ${where.sql} ORDER BY id LIMIT 200`, where.values)).rows - return { entries, cursor: entries.length === 200 ? entries[entries.length - 1].id : upper.watermark, state: (await getLogStates(filters.serverId))[0] ?? null, enabled: (await getLogSettings()).enabled } + const entries = (await pool.query(`SELECT ${logColumns} FROM router_log_entries WHERE ${where.sql} ORDER BY router_log_entries.id LIMIT $${where.values.length + 1}`, [...where.values, limit])).rows + signal?.throwIfAborted() + const state = (await getLogStates(filters.serverId))[0] ?? null + signal?.throwIfAborted() + const { enabled } = await getLogSettings() + signal?.throwIfAborted() + return { entries, cursor: entries.length === limit ? entries[entries.length - 1].id : upper.watermark, state, enabled } } diff --git a/backend/src/services/router-logs-sql.test.ts b/backend/src/services/router-logs-sql.test.ts index 08f73ee5..0f99f354 100644 --- a/backend/src/services/router-logs-sql.test.ts +++ b/backend/src/services/router-logs-sql.test.ts @@ -3,12 +3,14 @@ import assert from "node:assert/strict" import { readFile } from "node:fs/promises" import { createServer } from "node:http" import { randomBytes } from "node:crypto" +import { setTimeout as sleep } from "node:timers/promises" import pg from "pg" import { logEntrySchema, logFiltersSchema, logPageSchema, logSummarySchema } from "@mmapp/contracts" import { pool } from "../db/index.js" import { env } from "../config.js" import { collectRouterLogsOnce, getLogStates, subscribeLogDevice, updateLogSettings } from "./router-logs-collector.js" -import { readLogContext, readLogPage, readLogStream, readLogSummary } from "./router-logs-query.js" +import { readLogContext, readLogEntry, readLogPage, readLogStream, readLogSummary } from "./router-logs-query.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" const schema = "test_router_logs_" + randomBytes(6).toString("hex") const admin = new pg.Pool({ connectionString: env.DATABASE_URL, connectionTimeoutMillis: 3000 }) @@ -18,31 +20,146 @@ await admin.query(`CREATE SCHEMA ${schema}`) pool.options.options = `-c search_path=${schema}` let entries = Array.from({ length: 301 }, (_, i) => ({ ".id": `*${i}`, time: "12:00:00", topics: "system,info", message: "entry " + i })) let requests = 0, denied = false -const router = createServer((req, res) => { +let beforeLogResponse: (() => Promise) | undefined +const router = createServer(async (req, res) => { requests++ const url = new URL(req.url!, "http://127.0.0.1") assert.equal(url.pathname, "/rest/log") assert.equal(url.searchParams.get("buffer"), "memory") + const beforeResponse = beforeLogResponse + beforeLogResponse = undefined + try { await beforeResponse?.() } + catch (error) { res.destroy(error as Error); return } res.writeHead(denied ? 401 : 200, { "content-type": "application/json" }); res.end(JSON.stringify(denied ? { error: "denied" } : entries)) }) await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) const address = router.address() assert.ok(address && typeof address !== "string") +const routerPort = address.port + +async function verifyMultiServerHistory() { + const addServer = async (name: string) => Number((await pool.query<{ id: number }>( + "INSERT INTO servers(name,host,port,use_ssl) VALUES($1,'127.0.0.1',$2,false) RETURNING id", [name, routerPort], + )).rows[0].id) + const firstId = await addServer("logs-commit-first") + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + SELECT $1,'logs-commit-first','127.0.0.1',i::text,'12:00:00',now(),now()-(i * interval '1 second'),ARRAY['system'],'info','history sequence baseline '||i,0 + FROM generate_series(1,2) AS i`, [firstId]) + const filters = logFiltersSchema.parse({ q: "history sequence" }) + const initial = await readLogPage(filters, { limit: 1 }) + const continuation = { limit: 1, cursor: initial.nextCursor!, watermark: initial.watermark } + const expected = await readLogPage(filters, continuation) + await pool.query(`CREATE FUNCTION hold_first_log_append() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN PERFORM pg_advisory_xact_lock(872316,1); RETURN NEW; END $$; + CREATE TRIGGER hold_first_log_append AFTER INSERT ON router_log_entries FOR EACH ROW + WHEN (NEW.server_id = ${firstId}) EXECUTE FUNCTION hold_first_log_append()`) + const blocker = await pool.connect() + const pending: ReturnType[] = [] + let secondId!: number + const waitForLock = async (classid: number, objid: number, granted: boolean) => { + for (let i = 0; i < 200; i++) { + const { rows: [waiter] } = await pool.query<{ pid: number }>(`SELECT pid FROM pg_locks + WHERE locktype='advisory' AND database=(SELECT oid FROM pg_database WHERE datname=current_database()) + AND classid=$1::oid AND objid=$2::oid AND objsubid=2 AND granted=$3`, [classid, objid, granted]) + if (waiter) return waiter.pid + if (secondId !== undefined && (await pool.query("SELECT 1 FROM router_log_entries WHERE server_id=$1", [secondId])).rowCount) { + assert.fail("The later collector committed while an earlier reserved log ID was still invisible") + } + await sleep(25) + } + assert.fail(`Timed out waiting for test collector ${classid}/${objid}`) + } + try { + await blocker.query("BEGIN") + await blocker.query("SELECT pg_advisory_xact_lock(872316,1)") + entries = [{ ".id": "*1", time: new Date(Date.now() - 1500).toISOString(), topics: "system,info", message: "history sequence concurrent" }] + pending.push(collectRouterLogsOnce()) + const firstPid = await waitForLock(872316, 1, false) + secondId = await addServer("logs-commit-second") + pending.push(collectRouterLogsOnce()) + const secondPid = await waitForLock(872315, 1, false) + assert.ok((await pool.query<{ blocks: boolean }>("SELECT $1=ANY(pg_blocking_pids($2)) AS blocks", [firstPid, secondPid])).rows[0].blocks) + assert.deepEqual(await readLogPage(filters, { limit: 1 }), initial) + } finally { + await blocker.query("ROLLBACK") + blocker.release() + await Promise.all(pending) + await pool.query("DROP TRIGGER hold_first_log_append ON router_log_entries") + await pool.query("DROP FUNCTION hold_first_log_append()") + } + const visible = (await readLogPage(filters, { limit: 200 })).entries.filter(row => row.message === "history sequence concurrent") + assert.equal(visible.length, 2) + assert.ok(BigInt(visible.find(row => row.serverId === firstId)!.id) < BigInt(visible.find(row => row.serverId === secondId)!.id)) + assert.deepEqual(await readLogPage(filters, continuation), expected) + console.log("PASS multi-CHR history: later write waits for prior commit, and the existing page excludes both later entries") +} + try { const initial = await readFile(new URL("../../drizzle/0000_postgresql.sql", import.meta.url), "utf8") const serverSql = initial.match(/CREATE TABLE IF NOT EXISTS servers \([\s\S]*?\n\);/)![0] await pool.query(serverSql) await pool.query(await readFile(new URL("../../drizzle/0014_router_logs.sql", import.meta.url), "utf8")) + // Shared configuration writes require the real protections and write-ahead audit + // tables even when this test owns only a disposable logs schema. + await pool.query(await readFile(new URL("../../drizzle/0015_mcp.sql", import.meta.url), "utf8")) + await pool.query(await readFile(new URL("../../drizzle/0016_mcp_routing_observations.sql", import.meta.url), "utf8")) + const sourceMigration = await readFile(new URL("../../drizzle/0029_mcp_routing_sources.sql", import.meta.url), "utf8") + await pool.query(sourceMigration) + await pool.query(sourceMigration) const { rows: [server] } = await pool.query<{ id: number }>(`INSERT INTO servers(name,host,port,use_ssl) VALUES('logs-fixture','127.0.0.1',$1,false) RETURNING id`, [address.port]) const serverId = Number(server.id) - await updateLogSettings({ enabled: true, intervalSec: 5 }) + await updateLogSettings({ enabled: true, intervalSec: 5, retentionDays: 30 }) const filter = logFiltersSchema.parse({ serverId }) - await collectRouterLogsOnce() + const { rows: [expired] } = await pool.query<{ id: string }>(`INSERT INTO router_log_entries + (server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + VALUES($1,'logs-fixture','127.0.0.1','*expired','12:00:00',now()-interval '400 days',now()-interval '400 days',ARRAY['system'],'info','expired',0) RETURNING id::text`, [serverId]) + const maintenance = await pool.connect() + try { + await maintenance.query("BEGIN") + await maintenance.query("SELECT pg_advisory_xact_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + const before = requests + const deferred = await collectRouterLogsOnce() + assert.equal(requests, before) + assert.equal(deferred.servers[0].added, 0) + assert.deepEqual(deferred.errors, []) + assert.deepEqual(await getLogStates(serverId), []) + assert.equal((await readLogEntry(expired.id, serverId))?.message, "expired") + } finally { await maintenance.query("ROLLBACK"); maintenance.release() } + const { rows: [keep] } = await pool.query<{ id: string }>(`INSERT INTO router_log_entries + (server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + VALUES($1,'logs-fixture','127.0.0.1','*keep','12:00:00',now()-interval '45 days',now()-interval '45 days',ARRAY['system'],'info','keep after retention increase',0) RETURNING id::text`, [serverId]) + beforeLogResponse = () => updateLogSettings({ retentionDays: 90 }) + const collected = await collectRouterLogsOnce() + assert.deepEqual(collected.errors, []) + assert.equal(await readLogEntry(expired.id, serverId), null) + assert.equal((await readLogEntry(keep.id, serverId))?.message, "keep after retention increase") + await pool.query("DELETE FROM router_log_entries WHERE id=$1", [keep.id]) + await updateLogSettings({ retentionDays: 14 }) + console.log("PASS retention: cleanup uses the setting committed during RouterOS collection") let page = logPageSchema.parse(JSON.parse(JSON.stringify(await readLogPage(filter, { limit: 200 })))) assert.equal(page.entries.length, 200); assert.ok(page.nextCursor) const older = await readLogPage(filter, { limit: 200, cursor: page.nextCursor!, watermark: page.watermark }) assert.equal(older.entries.length, 101) assert.equal(new Set([...older.entries, ...page.entries].map(e => e.id)).size, 301) + const allServers = logFiltersSchema.parse({}) + const scoped = { ...allServers, allowedServerIds: [serverId] } + const scopedFirst = await readLogPage(scoped, { limit: 200 }) + const hiddenServerId = serverId + 1 + const { rows: [hidden] } = await pool.query<{ id: string }>(`INSERT INTO router_log_entries + (server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + VALUES($1,'hidden','192.0.2.2','*1','12:00:00',now(),now(),ARRAY['system'],'info','hidden event',0) RETURNING id::text`, [hiddenServerId]) + assert.equal((await readLogPage(allServers, { limit: 200 })).watermark, hidden.id, "Unscoped history keeps its global watermark") + assert.deepEqual(await readLogPage(scoped, { limit: 200 }), scopedFirst, "Hidden writes cannot advance a scoped watermark") + assert.deepEqual(await readLogPage({ ...scoped, serverId }, { limit: 200 }), scopedFirst) + const emptyPage = { entries: [], nextCursor: null, watermark: "0" } + assert.deepEqual(await readLogPage({ ...scoped, serverId: hiddenServerId }, { limit: 200 }), emptyPage) + assert.deepEqual(await readLogPage({ ...scoped, allowedServerIds: [] }, { limit: 200 }), emptyPage) + assert.deepEqual(await readLogPage(scoped, { limit: 200, cursor: scopedFirst.nextCursor!, watermark: scopedFirst.watermark }), older) + assert.equal((await readLogEntry(hidden.id))?.serverId, hiddenServerId) + assert.equal(await readLogEntry(hidden.id, serverId), null) + assert.equal(await readLogContext(hidden.id, serverId), null) + assert.equal((await readLogContext(hidden.id, hiddenServerId))?.entry.serverId, hiddenServerId) + await pool.query("DELETE FROM router_log_entries WHERE id=$1", [hidden.id]) await pool.query("UPDATE router_log_collection_state SET last_collected_at=now()-interval '1 minute'") await collectRouterLogsOnce() assert.equal((await readLogSummary(filter)).total, 301, "Repeated snapshot is not reimported") @@ -89,6 +206,7 @@ try { assert.equal((await pool.query("SELECT id FROM router_log_entries WHERE id=$1", [critical.id])).rowCount, 0) await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) assert.ok((await readLogSummary(filter)).total > 0, "Deleting device preserves collected history") + await verifyMultiServerHistory() const count = process.env.LOGS_PERFORMANCE === "1" ? 1_000_000 : 5000 const started = performance.now() await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) diff --git a/backend/src/services/routeros-native-paths.ts b/backend/src/services/routeros-native-paths.ts new file mode 100644 index 00000000..445c45cf --- /dev/null +++ b/backend/src/services/routeros-native-paths.ts @@ -0,0 +1,17 @@ +export function encodeRosId(rosId: string): string { + const id = rosId.startsWith("*") ? rosId : `*${rosId.replace(/^\*/, "")}` + return encodeURIComponent(id) +} + +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", + ...["ip", "ipv6"].flatMap(family => ["filter", "nat", "mangle", "raw"].map(table => `/${family}/firewall/${table}`))] as const + +export const wireguardInterfaceRuntimeDependencyPaths = ["/routing/bgp/session", "/routing/ospf/neighbor", + "/routing/ospf/interface", "/routing/bfd/session"] as const + +export const wireguardInterfaceDependencyPaths = [...new Set([...greDependencyPaths, "/interface/vxlan", + "/interface/vrrp", "/interface/bonding", "/interface/eoip", "/interface/gre", "/interface/ipip", "/ip/dhcp-server", + "/ip/dhcp-client", "/ipv6/dhcp-server", "/ipv6/dhcp-client", "/ip/ipsec/peer", "/ip/ipsec/policy", + ...wireguardInterfaceRuntimeDependencyPaths])] diff --git a/backend/src/services/routeros-snapshot-read.test.ts b/backend/src/services/routeros-snapshot-read.test.ts new file mode 100644 index 00000000..0fb8a859 --- /dev/null +++ b/backend/src/services/routeros-snapshot-read.test.ts @@ -0,0 +1,130 @@ +import assert from "node:assert/strict" +import { createServer, type RequestListener } from "node:http" +import { test } from "node:test" +import { MikrotikClient, isRouterOsPreHeaderGetReset } from "./mikrotik.js" +import { readRouterOsSnapshotGet } from "./routeros-snapshot-read.js" + +async function loopback(work: (client: MikrotikClient, received: string[]) => Promise, respond: RequestListener) { + const received: string[] = [] + const server = createServer((request, response) => { + assert.equal(request.method, "GET") + received.push(request.url!) + respond(request, response) + }) + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const address = server.address(); assert(address && typeof address !== "string") + const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }) + try { await work(client, received) } + finally { server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) } +} + +async function brandedReset(): Promise { + let error: unknown + await loopback(async client => { + try { await client.get("/interface/ethernet") } catch (failure) { error = failure } + }, request => request.destroy()) + assert(error instanceof Error && isRouterOsPreHeaderGetReset(error)) + return error +} + +test("actual pre-header reset retries the same GET once with fresh source/authority admission", async () => { + let requests = 0, guards = 0 + await loopback(async (client, received) => { + assert.deepEqual(await readRouterOsSnapshotGet({ client, path: "/interface/ethernet", assertCurrent: async () => { guards++ } }), [{ ".id": "*1", name: "ether1" }]) + assert.deepEqual(received, ["/rest/interface/ethernet", "/rest/interface/ethernet"]) + assert.equal(guards, 4) + }, (request, response) => { + if (++requests === 1) request.destroy() + else response.end(JSON.stringify([{ ".id": "*1", name: "ether1" }])) + }) +}) + +test("repeated actual resets stop at two GETs and preserve the final transport error", async () => { + await loopback(async (client, received) => { + const failures: unknown[] = [] + const wrapped = { async get(...args: Parameters) { + try { return await client.get(...args) } catch (error) { failures.push(error); throw error } + } } + await assert.rejects(readRouterOsSnapshotGet({ client: wrapped, path: "/interface/ethernet", assertCurrent: async () => {} }), error => error === failures[1]) + assert.equal(failures.length, 2); assert.equal(received.length, 2) + assert(failures.every(isRouterOsPreHeaderGetReset)) + }, request => request.destroy()) +}) + +for (const outcome of ["headers-reset", "information-reset", "body-reset", "429", "bad-json", "timeout", "abort"] as const) { + test(`${outcome} cannot spend a retry slot`, async () => { + const abort = new AbortController() + await loopback(async (client, received) => { + let failure: unknown + try { await readRouterOsSnapshotGet({ client, path: "/interface/ethernet", timeoutMs: 80, signal: abort.signal, assertCurrent: async () => {} }) } + catch (error) { failure = error } + assert(failure instanceof Error) + assert.equal(isRouterOsPreHeaderGetReset(failure), false) + assert.equal(received.length, 1) + }, (request, response) => { + if (outcome === "timeout") return + if (outcome === "abort") { abort.abort(new Error("caller cancelled")); return } + if (outcome === "information-reset") { response.writeContinue(); setTimeout(() => request.destroy(), 10); return } + if (outcome === "headers-reset" || outcome === "body-reset") { + response.writeHead(200, { "content-type": "application/json", "content-length": "1000" }); response.flushHeaders() + if (outcome === "body-reset") response.write("[") + setTimeout(() => response.destroy(), 10); return + } + if (outcome === "429") { response.writeHead(429); response.end("{}"); return } + response.end("!") + }) + }) +} + +test("spoofed reset, pre-abort and initial authority denial dispatch at most one or zero GETs", async () => { + const spoof = Object.assign(new Error("ECONNRESET"), { code: "ECONNRESET" }) + let calls = 0 + const client = { async get(): Promise { calls++; throw spoof } } + await assert.rejects(readRouterOsSnapshotGet({ client, path: "/interface", assertCurrent: async () => {} }), error => error === spoof) + assert.equal(calls, 1) + const abort = new AbortController(); const cancelled = new Error("cancelled"); abort.abort(cancelled) + await assert.rejects(readRouterOsSnapshotGet({ client, path: "/interface", signal: abort.signal, assertCurrent: async () => {} }), error => error === cancelled) + const denied = new Error("authority denied") + await assert.rejects(readRouterOsSnapshotGet({ client, path: "/interface", assertCurrent: async () => { throw denied } }), error => error === denied) + assert.equal(calls, 1) +}) + +test("both attempts share one decreasing budget including admission work", async () => { + const reset = await brandedReset() + let clock = 0, guards = 0, dispatches = 0 + const timeouts: number[] = [] + const client = { async get(_path: string, timeout?: number, _signal?: AbortSignal, _maxBytes?: number, beforeDispatch?: () => Promise): Promise { + timeouts.push(timeout!) + await beforeDispatch!() + dispatches++ + if (dispatches === 1) { clock += 20; throw reset } + return [] as T + } } + await readRouterOsSnapshotGet({ client, path: "/interface", timeoutMs: 100, now: () => clock, assertCurrent: async () => { guards++; clock += 10 } }) + assert.deepEqual(timeouts, [90, 50]); assert.equal(guards, 4); assert.equal(dispatches, 2) +}) + +test("authority/source revocation and expiry between attempts forbid the second GET", async () => { + const reset = await brandedReset() + for (const outcome of ["authority", "source", "expiry"] as const) { + let clock = 0, guards = 0, calls = 0 + const denied = new Error(outcome + " changed") + const client = { async get(_path: string, _timeout?: number, _signal?: AbortSignal, _maxBytes?: number, beforeDispatch?: () => Promise): Promise { + await beforeDispatch!(); calls++; throw reset + } } + await assert.rejects(readRouterOsSnapshotGet({ client, path: "/interface", timeoutMs: 100, now: () => clock, assertCurrent: async () => { + if (++guards === 3) { if (outcome === "expiry") clock = 100; else throw denied } + } }), error => error === (outcome === "expiry" ? reset : denied)) + assert.equal(calls, 1); assert.equal(guards, 3) + } +}) + +test("deadline expiry during the final dispatch admission starts no HTTP request", async () => { + let clock = 0, guards = 0 + await loopback(async (client, received) => { + await assert.rejects(readRouterOsSnapshotGet({ client, path: "/interface", timeoutMs: 100, now: () => clock, assertCurrent: async () => { + if (++guards === 2) clock = 100 + } }), { name: "TimeoutError" }) + assert.equal(received.length, 0) + }, (_request, response) => response.end("[]")) +}) diff --git a/backend/src/services/routeros-snapshot-read.ts b/backend/src/services/routeros-snapshot-read.ts new file mode 100644 index 00000000..5dff0c18 --- /dev/null +++ b/backend/src/services/routeros-snapshot-read.ts @@ -0,0 +1,49 @@ +import { isRouterOsPreHeaderGetReset } from "./mikrotik.js" + +interface SnapshotGetClient { + get(path: string, timeoutMs?: number, signal?: AbortSignal, maxResponseBytes?: number, beforeDispatch?: () => Promise): Promise +} + +/** A single catalogue read, pinned to the original client/path. Only a transport- + * branded reset before any response headers can spend its one retry slot. */ +export async function readRouterOsSnapshotGet(input: { + client: SnapshotGetClient + path: string + assertCurrent(): Promise + timeoutMs?: number + signal?: AbortSignal + now?: () => number +}): Promise { + const { client, path, assertCurrent, signal } = input + const timeoutMs = input.timeoutMs ?? 10_000, now = input.now ?? (() => performance.now()) + if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2_147_483_647) throw new RangeError("Invalid RouterOS snapshot read timeout") + const deadline = now() + timeoutMs + let originalReset: unknown + const remaining = () => { + signal?.throwIfAborted() + const budget = deadline - now() + if (budget <= 0) throw originalReset ?? new DOMException("RouterOS snapshot read deadline expired", "TimeoutError") + return Math.ceil(budget) + } + for (let attempt = 0; attempt < 2; attempt++) { + remaining() + await assertCurrent() + const budget = remaining() + const deadlineSignal = AbortSignal.timeout(budget) + const requestSignal = signal ? AbortSignal.any([signal, deadlineSignal]) : deadlineSignal + try { + const result = await client.get(path, budget, requestSignal, undefined, async () => { + await assertCurrent() + remaining() + requestSignal.throwIfAborted() + }) + remaining() + return result + } catch (error) { + if (attempt !== 0 || !isRouterOsPreHeaderGetReset(error) || signal?.aborted) throw error + originalReset = error + remaining() + } + } + throw originalReset +} diff --git a/backend/src/services/routing-filter-body.test.ts b/backend/src/services/routing-filter-body.test.ts new file mode 100644 index 00000000..4b416c36 --- /dev/null +++ b/backend/src/services/routing-filter-body.test.ts @@ -0,0 +1,10 @@ +import assert from "node:assert/strict" +import { routingFilterBodiesMatch } from "./routing-filter-body.js" +const body = 'if (bgp-communities includes 64512:1) {\n set gw 192.0.2.2;\n set gw-interface "gre-test";\n accept;\n}' +assert.ok(routingFilterBodiesMatch(body.replace('"gre-test"', "gre-test"), body)) +for (const name of ["gre.vlan", "gre:vrf", "gre@test", "gre/one", "123"]) assert.ok(routingFilterBodiesMatch(`set gw-interface ${name};`, `set gw-interface "${name}";`)) +for (const changed of [body.replace("192.0.2.2", "192.0.2.3"), body.replace("64512:1", "64512:2"), body.replace("gre-test", "gre-other"), body.replace("accept", "reject"), body + "\nreject;", body.replaceAll("\n", "\r\n"), body.replace("includes", "not includes")]) assert.equal(routingFilterBodiesMatch(changed, body), false) +assert.equal(routingFilterBodiesMatch('set gw-interface GRE test;', 'set gw-interface "GRE test";'), false) +assert.equal(routingFilterBodiesMatch('set gw-interface "gre\\test";', 'set gw-interface "gretest";'), false) +assert.equal(routingFilterBodiesMatch(undefined, body), false) +console.log("Routing filter read-back: only quotes around safe single-token interface names normalize; commands/values/order/conditions/whitespace and multi-word names remain exact") diff --git a/backend/src/services/routing-filter-body.ts b/backend/src/services/routing-filter-body.ts new file mode 100644 index 00000000..273c3187 --- /dev/null +++ b/backend/src/services/routing-filter-body.ts @@ -0,0 +1,15 @@ +/** RouterOS 7.24 prints single-token gw-interface names without quotes. + * Normalize only that observed representation: preserve every other byte, + * command, value, condition, ordering and names that contain whitespace. */ +export function canonicalRoutingFilterBody(body: string) { + return body.replace(/(\bset[ \t]+gw-interface[ \t]+)"([A-Za-z0-9_.:@/-]+)"/g, "$1$2") +} +export function routingFilterBodiesMatch(actual: unknown, expected: string) { + return typeof actual === "string" && canonicalRoutingFilterBody(actual) === canonicalRoutingFilterBody(expected) +} + +/** Compare complete managed bodies, preserving bytes outside the compiler's interface assignment. */ +export function managedRoutingFilterBodiesMatch(actual: unknown, expected: string) { + const normalize = (body: string) => body.replace(/^ set gw-interface "([A-Za-z_][A-Za-z0-9_.-]{0,99})";$/gm, " set gw-interface $1;") + return typeof actual === "string" && (actual === expected || normalize(actual) === normalize(expected)) +} diff --git a/backend/src/services/routing-gateway-reference.test.ts b/backend/src/services/routing-gateway-reference.test.ts new file mode 100644 index 00000000..bb9e1c95 --- /dev/null +++ b/backend/src/services/routing-gateway-reference.test.ts @@ -0,0 +1,16 @@ +import assert from "node:assert/strict" +import { gatewayReferencesTable, isDynamicRouteInsideTable } from "./routing-gateway-reference.js" + +const refs = ["owned-vrf", "*1D"] +for (const value of ["gre@owned-vrf", "192.0.2.2%gre@owned-vrf", "2001:db8::2@owned-vrf", "gre@*1D", "192.0.2.2@other, gre@owned-vrf", ["192.0.2.2@other", "gre@*1D"]]) assert.equal(gatewayReferencesTable(value, refs), true) +for (const value of [undefined, "", [], "192.0.2.2", "gre", "gre@owned-vrf-other", "gre@*1D0", "gre@other-owned-vrf", "owned-vrf", "@owned-vrf"]) assert.equal(gatewayReferencesTable(value, refs), false) +for (const value of [null, {}, 1, ["gre@owned-vrf", 1], Array(257).fill("gre")]) assert.throws(() => gatewayReferencesTable(value, refs)) +assert.equal(gatewayReferencesTable("gre@owned-vrf ", ["owned-vrf "]), true) +assert.equal(gatewayReferencesTable("gre@owned-vrf , next@other", refs), true) +assert.equal(gatewayReferencesTable("gre@comma,table", ["comma,table"]), true) +for (const value of ['gre@"owned-vrf"', "gre@other,".repeat(257), "x".repeat(65537)]) assert.throws(() => gatewayReferencesTable(value, refs)) +assert.throws(() => gatewayReferencesTable("gre@other", ["comma,table"])) +for (const dynamic of ["true", true]) assert.equal(isDynamicRouteInsideTable({ dynamic, "routing-table": "owned-vrf" }, refs), true) +for (const dynamic of ["false", false, undefined, "unknown"]) assert.equal(isDynamicRouteInsideTable({ dynamic, "routing-table": "owned-vrf" }, refs), false) +assert.equal(isDynamicRouteInsideTable({ dynamic: "true", "routing-table": "main" }, refs), false) +console.log("VRF gateway references: exact name/ID, IPv4/IPv6/interface, ECMP string/array, suffix isolation and malformed source rejection") diff --git a/backend/src/services/routing-gateway-reference.ts b/backend/src/services/routing-gateway-reference.ts new file mode 100644 index 00000000..8ec95b9d --- /dev/null +++ b/backend/src/services/routing-gateway-reference.ts @@ -0,0 +1,24 @@ +/** Native ECMP gateways can be a comma list or an array. Match exact table + * names/IDs after @, including address%interface@table and interface@table. */ +export function gatewayReferencesTable(value: unknown, references: readonly string[]) { + if (value === undefined || value === "") return false + const values = typeof value === "string" ? [value] : value + if (!Array.isArray(values) || values.length > 256 || values.some(item => typeof item !== "string" || item.length > 65536)) throw new Error("Не подтверждены gateway references маршрута") + const matches = (token: string) => references.some(reference => reference.length > 0 && token.length > reference.length + 1 && token.endsWith("@" + reference)) + // Preserve literal whitespace in names before accepting separator whitespace. + if (values.some(item => matches(item) || matches(item.trim()))) return true + if (!values.some(item => item.includes("@"))) return false + if (references.some(reference => /[,"'\\\r\n]/.test(reference)) || values.some(item => /["'\\\r\n]/.test(item))) throw new Error("Не подтверждён синтаксис gateway/table references") + const tokens: string[] = [] + for (const item of values) for (const token of item.split(",")) { + if (tokens.length >= 256) throw new Error("Превышен предел ECMP gateway references") + tokens.push(token) + } + return tokens.some(token => matches(token) || matches(token.trim())) +} + +/** Derived routes inside the target table are removed/moved by VRF membership; + * keep them sealed for drift checks without treating them as external users. */ +export function isDynamicRouteInsideTable(row: Readonly>, references: readonly string[]) { + return [true, "true"].includes(row.dynamic as boolean | string) && typeof row["routing-table"] === "string" && references.includes(row["routing-table"]) +} diff --git a/backend/src/services/routing-table-fib.test.ts b/backend/src/services/routing-table-fib.test.ts new file mode 100644 index 00000000..536a02c5 --- /dev/null +++ b/backend/src/services/routing-table-fib.test.ts @@ -0,0 +1,13 @@ +import assert from "node:assert/strict" +import test from "node:test" +import { isConfirmedRoutingFibTable } from "./routing-table-fib.js" + +test("native FIB switch and explicit enabled representations preserve the table", () => { + for (const fib of ["", true, "true", "yes"]) assert.equal(isConfirmedRoutingFibTable({ name: "main", fib, invalid: "false" }), true) +}) +test("absent or false FIB never confirms a forwarding table", () => { + for (const fib of [undefined, null, false, "false", "no", "unknown", "0", 1]) assert.equal(isConfirmedRoutingFibTable({ fib }), false) +}) +test("disabled, invalid and ambiguous status cannot be promoted by the FIB switch", () => { + for (const key of ["disabled", "invalid"]) for (const value of [true, "true", "yes", "", null, "unknown"]) assert.equal(isConfirmedRoutingFibTable({ fib: "", [key]: value }), false) +}) diff --git a/backend/src/services/routing-table-fib.ts b/backend/src/services/routing-table-fib.ts new file mode 100644 index 00000000..be95e527 --- /dev/null +++ b/backend/src/services/routing-table-fib.ts @@ -0,0 +1,8 @@ +/** RouterOS fib is a CLI switch: its enabled REST representation may be "". + * Do not interpret empty strings as true for general boolean properties. + * https://manual.mikrotik.com/docs/cli-reference/routing/table/ */ +export function isConfirmedRoutingFibTable(row: Readonly>): boolean { + const fib = row.fib + if (![true, "true", "yes", ""].includes(fib as string | boolean)) return false + return ["disabled", "invalid"].every(key => row[key] === undefined || [false, "false", "no"].includes(row[key] as string | boolean)) +} diff --git a/backend/src/services/routing-topology-observation.test.ts b/backend/src/services/routing-topology-observation.test.ts new file mode 100644 index 00000000..5ff30e72 --- /dev/null +++ b/backend/src/services/routing-topology-observation.test.ts @@ -0,0 +1,56 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { projectRoutingImmediateGateways, readRoutingTopology } from "./routing-topology-observation.js" +const route = { ".id": "*r", afi: "ip", "dst-address": "192.0.2.0/24", "routing-table": "main", active: "true", contribution: "active", gateway: "198.51.100.1", "immediate-gw": "198.51.100.1%wan", comment: "NEVER_PRIVATE", "bgp.peer-cache-id": "*opaque", "bgp.session": "*opaque" } +const ports = [{ name: "wan", running: "true" }], tables = [{ name: "main", fib: "true" }] +const project = (row = route, version = "7.24.5 (stable)", interfaces: unknown = ports, catalogue: unknown = tables) => projectRoutingImmediateGateways(version, [row], interfaces, catalogue) +test("documented IPv4 final gateway is reachable without operation/BGP namespace assertions", () => { + const result = project(); assert.equal(result.status, "observed"); assert.equal(result.items[0].status, "observed") + const item = result.items[0]; if (item.status !== "observed") throw Error("Missing observation") + assert.deepEqual(item.immediateGateway, { address: "198.51.100.1", interface: "wan" }) + assert.equal(item.routeOriginVerified, false); assert.equal(item.recursiveChainVerified, false); assert.equal(item.semanticProof, false) + assert(!JSON.stringify(result).includes("NEVER_PRIVATE")); assert(!JSON.stringify(result).includes("opaque")) +}) +test("IPv6 endpoint and directly connected interface are observed separately from full recursion", () => { + const ipv6 = project({ ...route, afi: "ipv6", "dst-address": "2001:db8::/64", "immediate-gw": "2001:db8::1%wan" }) + assert.equal(ipv6.items[0].status, "observed") + const direct = project({ ...route, connect: "true", "immediate-gw": "wan" } as typeof route) + assert.equal(direct.items[0].status, "observed") +}) +for (const [name, changed] of Object.entries({ multipath: { ecmp: "true" }, absentActive: { active: undefined }, disabled: { disabled: "true" }, unavailable: { "immediate-gw": "" }, numericOnly: { "immediate-gw": "17" }, wrongFamily: { "immediate-gw": "2001:db8::1%wan" }, wrongVrf: { "immediate-gw": "198.51.100.1%wan@other" } })) test(name + " stays unknown", () => { + assert.equal(project({ ...route, ...changed } as typeof route).items[0].status, "unknown") +}) +test("unsupported version, missing/duplicate/down interface and missing/nonfib table stay unknown", () => { + assert.equal(project(route, "6.49").status, "unknown") + for (const ports of [[], [{ name: "wan", running: "false" }], [{ name: "wan", running: "true" }, { name: "wan", running: "true" }]]) assert.equal(project(route, "7.24.5", ports).items[0].status, "unknown") + for (const tables of [[], [{ name: "main", fib: "false" }]]) assert.equal(project(route, "7.24.5", ports, tables).items[0].status, "unknown") + assert.equal(projectRoutingImmediateGateways("7.24.5", [route, route], ports, tables).status, "unknown") +}) +test("actual topology reader uses fixed GETs/fences/signal/wire bound and publishes no raw RIB", async () => { + const calls: string[] = []; let fences = 0 + const client = { async get(path: string, timeout?: number, signal?: AbortSignal, bytes?: number, beforeDispatch?: () => Promise) { + assert(timeout! <= 10000); assert.equal(bytes, 1048576); assert(signal); await beforeDispatch?.(); calls.push(path) + return structuredClone(path === "/system/resource" ? [{ version: "7.24.5" }] : path === "/interface" ? ports : path === "/routing/table" ? tables : path === "/routing/route" ? [route] : []) as T + } } + const result = await readRoutingTopology(client, async () => { fences++ }, new AbortController().signal) + assert.equal(calls.length, 11); assert.equal(result.routeResolution.items[0].status, "observed"); assert(fences >= calls.length * 3) + assert(!JSON.stringify(result).includes("NEVER_PRIVATE")); assert.equal(result.observedPath, false); assert.equal(result.routeResolution.captureAtomic, false) +}) +test("post-read access failure cannot be swallowed as provider unknown", async () => { + let denied = false + const client = { async get() { denied = true; return [] as T } } + await assert.rejects(readRoutingTopology(client, async () => { if (denied) throw Error("revoked") }), /revoked/) +}) + +test("cancellation during the last asynchronous publication fence refuses the result", async () => { + const cancel = new AbortController(); let fences = 0 + const client = { async get(path: string) { return (path === "/system/resource" ? [{ version: "7.24.5" }] : path === "/interface" ? ports : path === "/routing/table" ? tables : path === "/routing/route" ? [route] : []) as T } } + await assert.rejects(readRoutingTopology(client, async () => { if (++fences === 23) { await Promise.resolve(); cancel.abort() } }, cancel.signal), /Topology read cancelled/) + assert.equal(fences, 23) +}) + +test("actual RouterOS FIB switch encoding is distinct from an absent or invalid table", () => { + const native = [{ ".id": "*0", name: "main", dynamic: "true", fib: "", invalid: "false" }] + assert.equal(project(route, "7.24.5 (stable)", ports, native).items[0].status, "observed") + for (const table of [{ name: "main" }, { name: "main", fib: "false" }, { name: "main", fib: "", invalid: "true" }, { name: "main", fib: "", disabled: "true" }]) assert.equal(project(route, "7.24.5", ports, [table]).items[0].status, "unknown") +}) diff --git a/backend/src/services/routing-topology-observation.ts b/backend/src/services/routing-topology-observation.ts new file mode 100644 index 00000000..25c6a5e3 --- /dev/null +++ b/backend/src/services/routing-topology-observation.ts @@ -0,0 +1,82 @@ +import { isConfirmedRoutingFibTable } from "./routing-table-fib.js" +import { isIP } from "node:net" +import type { MikrotikClient } from "./mikrotik.js" + +type Row = Record +const paths = { interfaces: "/interface", ipv4: "/ip/route", ipv6: "/ipv6/route", tables: "/routing/table", rules: "/routing/rule", vrfs: "/ip/vrf", ospfNeighbors: "/routing/ospf/neighbor", bgpSessions: "/routing/bgp/session" } as const +const flag = (value: unknown) => value === true || value === "true" || value === "yes" ? true : value === false || value === "false" || value === "no" ? false : undefined +const literal = (value: unknown, max = 200): value is string => typeof value === "string" && value.length > 0 && value.length <= max && !/[\u0000-\u001f\u007f]/.test(value) +const rows = (raw: unknown): Row[] => { + const values = Array.isArray(raw) ? raw : [raw] + if (values.length > 10000 || values.some(row => !row || typeof row !== "object" || Array.isArray(row))) throw Error("Unsupported rows") + return values as Row[] +} +const limits = { semanticProof: false, recursiveChainVerified: false, routeOriginVerified: false, forwardingVerified: false } as const +/** Project documented immediate-gw only. Numeric nexthop/link/interface indexes + * and BGP peer-cache/session namespaces are deliberately never joined. + * https://manual.mikrotik.com/docs/cli-reference/routing/route/ */ +export function projectRoutingImmediateGateways(version: unknown, rib: unknown, interfaces: unknown, tables: unknown) { + const unknown = (reason: string) => ({ status: "unknown" as const, reason, items: [], truncated: false, ...limits }) + if (!literal(version, 128) || !/^7\.2[0-6](?:\.\d{1,3})?(?: \((?:stable|long-term)\))?$/.test(version)) return unknown("unsupported-routeros-version") + let routes: Row[], ports: Row[], catalogue: Row[] + try { routes = rows(rib); ports = rows(interfaces); catalogue = rows(tables) } catch { return unknown("unavailable-or-malformed-catalogue") } + if (routes.some(row => !literal(row[".id"], 100)) || new Set(routes.map(row => row[".id"])).size !== routes.length) return unknown("ambiguous-route-identity") + const selected = routes.filter(row => ["ip", "ip4", "ipv6", "ip6"].includes(String(row.afi))) + const items = selected.slice(0, 500).map(row => { + const identity = { nativeId: String(row[".id"]), ...limits } + const fail = (reason: string) => ({ ...identity, status: "unknown" as const, reason }) + const prefix = row["dst-address"], table = row["routing-table"] + if (!literal(prefix) || !literal(table)) return fail("unconfirmed-prefix-or-table") + const [address, mask, extra] = prefix.split("/"), family = isIP(address) + if (!family || extra !== undefined || !/^\d+$/.test(mask ?? "") || Number(mask) > (family === 4 ? 32 : 128) || family !== (["ip", "ip4"].includes(String(row.afi)) ? 4 : 6)) return fail("unconfirmed-route-family") + if (flag(row.active) !== true || row.contribution !== "active" || ["disabled", "filtered", "unreachable", "blackhole"].some(key => row[key] !== undefined && flag(row[key]) !== false)) return fail("route-not-confirmed-active") + if (row.ecmp !== undefined && flag(row.ecmp) !== false) return fail("multipath-selection-unknown") + const matchingTables = catalogue.filter(entry => entry.name === table) + if (matchingTables.length !== 1 || !isConfirmedRoutingFibTable(matchingTables[0])) return fail("unconfirmed-fib-table") + const immediate = row["immediate-gw"] + if (!literal(immediate)) return fail("immediate-gateway-unavailable") + const endpoint = /^([^%@,\s]+)%([^%@,]+)(?:@([^%@,]+))?$/.exec(immediate) + const connected = flag(row.connect) === true + const iface = endpoint?.[2] ?? (connected && !/[%@,]/.test(immediate) ? immediate : undefined) + if (!iface || endpoint && (isIP(endpoint[1]) !== family || endpoint[3] !== undefined && endpoint[3] !== table)) return fail("unconfirmed-immediate-endpoint") + const matchingPorts = ports.filter(entry => entry.name === iface) + if (matchingPorts.length !== 1 || flag(matchingPorts[0].running) !== true || matchingPorts[0].disabled !== undefined && flag(matchingPorts[0].disabled) !== false) return fail("unconfirmed-immediate-interface") + return { ...identity, status: "observed" as const, family: family === 4 ? "ipv4" as const : "ipv6" as const, prefix, table, + immediateGateway: { address: endpoint?.[1] ?? null, interface: iface }, observationScope: "routeros-reported-final-next-hop" as const } + }) + return { status: "observed" as const, routerOsVersion: version, items, truncated: selected.length > 500, ...limits } +} + +/** Existing topology reader: bounded GETs, same caller/endpoint fence at actual + * dispatch and publication. Failure remains per-resource observational unknown. */ +export async function readRoutingTopology(client: Pick, beforeRead: () => Promise, signal?: AbortSignal) { + const deadline = Date.now() + 30000 + const acquire = async (path: string) => { + await beforeRead() + const remaining = deadline - Date.now() + if (remaining <= 0 || signal?.aborted) throw Error("Topology read cancelled") + try { + const value = await client.get(path, Math.min(10000, remaining), signal, 1048576, beforeRead) + await beforeRead() + return { available: true as const, value: rows(value) } + } catch { + await beforeRead() + return { available: false as const } + } + } + const first = await acquire("/system/resource") + const captured: Partial> = {}, resources: Record = {} + for (const [key, path] of Object.entries(paths) as Array<[keyof typeof paths, string]>) { + const result = await acquire(path) + if (result.available) { + captured[key] = result.value + resources[key] = { items: result.value.slice(0, 500), truncated: result.value.length > 500, observed: true } + } else resources[key] = { items: [], observed: false, reason: "Устройство не подтвердило возможность чтения" } + } + const rib = await acquire("/routing/route"), last = await acquire("/system/resource") + await beforeRead() + if (signal?.aborted || Date.now() >= deadline) throw Error("Topology read cancelled") + const version = first.available && last.available && first.value.length === 1 && last.value.length === 1 && first.value[0].version === last.value[0].version ? first.value[0].version : undefined + const routeResolution = projectRoutingImmediateGateways(version, rib.available ? rib.value : undefined, captured.interfaces, captured.tables) + return { observedAt: new Date().toISOString(), observedPath: false, routeResolution: { ...routeResolution, captureAtomic: false, sourceGenerationIssued: false }, resources } +} diff --git a/backend/src/services/s3-backup-client.ts b/backend/src/services/s3-backup-client.ts index b5a3eb76..522d55a3 100644 --- a/backend/src/services/s3-backup-client.ts +++ b/backend/src/services/s3-backup-client.ts @@ -1,3 +1,4 @@ +import { checkBackupRead, collectBackupBytes, type BackupReadOptions } from "./backup-content-bounds.js" import { DeleteObjectCommand, GetObjectCommand, @@ -89,8 +90,20 @@ export async function s3GetObject( client: S3Client, bucket: string, key: string, + options: BackupReadOptions = {}, ): Promise { - const out = await client.send(new GetObjectCommand({ Bucket: bucket, Key: key })) + await checkBackupRead(options) + const out = await client.send(new GetObjectCommand({ Bucket: bucket, Key: key }), { abortSignal: options.signal }) + try { + await checkBackupRead(options, out.ContentLength ?? 0) + if (options.maxBytes !== undefined) { + if (!out.Body || !(Symbol.asyncIterator in out.Body)) throw new Error("S3 body is not a bounded stream") + return await collectBackupBytes(out.Body as AsyncIterable, options) + } + } catch (error) { + if (out.Body && "destroy" in out.Body && typeof out.Body.destroy === "function") out.Body.destroy() + throw error + } const bytes = await out.Body?.transformToByteArray() if (!bytes) throw new Error("Пустой объект S3") return Buffer.from(bytes) diff --git a/backend/src/services/scheduler-automatic-authority.test.mjs b/backend/src/services/scheduler-automatic-authority.test.mjs new file mode 100644 index 00000000..afe777e8 --- /dev/null +++ b/backend/src/services/scheduler-automatic-authority.test.mjs @@ -0,0 +1,153 @@ +/** Genuine ALS/AsyncResource ownership; synthetic scheduler and no DB/native I/O. */ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { createRequire } from "node:module" +import { runInNewContext } from "node:vm" +import { AsyncLocalStorage, AsyncResource } from "node:async_hooks" +import { test } from "node:test" +const require = createRequire(import.meta.url), ts = require("typescript") +const source = await readFile(new URL("./scheduler-automatic-root.ts", import.meta.url), "utf8") +const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, esModuleInterop: true } }).outputText +const deferred = () => { let resolve; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function fixture() { + const exports = {}, state = { generation: 0, wall: Date.now(), mono: 100, hook: async () => {}, runs: 0 } + const noop = () => {}, handoff = () => Promise.resolve() + const execute = async job => { state.runs++; await state.hook(job) } + const providers = { + "node:async_hooks": { AsyncLocalStorage, AsyncResource }, + "../modules/mcp/database-request-lease.js": { withDatabaseApiRequest: work => work() }, + "./operation-config-lock-lease.js": { assertNoOperationConfigurationLockContext: noop, captureOperationConfigurationLockHandoff: handoff }, + "./config-mutation-lock.js": { assertNoConfigurationLockContext: noop, captureConfigurationLockHandoff: handoff }, + "./config-mutation-policy.js": { assertNoVerifiedConfigurationContext: noop, assertConfigurationHandoffContext: noop }, + "./shared-database-gate.js": { sharedDatabaseGate: { assertNoRequestContext: noop, isPaused: () => false, request: work => work() } }, + "./database-background-work.js": { databaseBackgroundWork: { track: work => work } }, + "./alert-collector-hooks.js": { wireAlertEngineRunner: noop }, + "./scheduler.js": { isAutomaticSchedulerGenerationCurrent: generation => generation === state.generation, + executeAutomaticSchedulerJob: execute }, + "./router-logs-collector.js": { runRouterLogsLiveOnce: () => execute("router_logs_live"), runRouterLogsRetentionOnce: () => execute("router_logs_retention") }, + } + class Clock extends Date { static now() { return state.wall } } + runInNewContext(compiled, { exports, require(name) { assert(name in providers, name); return providers[name] }, + AbortController, AbortSignal, Date: Clock, performance: { now: () => state.mono } }) + exports.initializeAutomaticSchedulerRoot() + return { api: exports, state, scope: () => ({ deadlineAt: state.wall + 1000 }), + run: (job = "uptime_speed", ticket = exports.createAutomaticSchedulerTicket(job, state.generation)) => exports.runAutomaticSchedulerTicket(ticket) } +} +const refused = error => error?.code === "AUTOMATIC_SCHEDULER_AUTHORITY_REFUSED" + +test("no authority outside actual fixed-ticket work or from JSON/copied tickets", async () => { + const f = fixture() + assert.throws(() => f.api.captureAutomaticSchedulerAuthority(f.scope()), refused) + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority({ job: "uptime_speed", generation: 0 }), refused) + const ticket = f.api.createAutomaticSchedulerTicket("uptime_speed", 0) + await assert.rejects(f.api.runAutomaticSchedulerTicket({ ...ticket })); assert.equal(f.state.runs, 0) + await f.run(); assert.equal(f.state.runs, 1) +}) +test("capture exposes immutable genuine job/generation/process-local execution and first scope", async () => { + const f = fixture(), ids = [] + f.state.hook = async job => { + const scope = f.scope(), originalDeadline = scope.deadlineAt, originalMonoEnd = f.state.mono + scope.deadlineAt - f.state.wall + const cap = f.api.captureAutomaticSchedulerAuthority(scope) + assert.equal(f.api.captureAutomaticSchedulerAuthority({ ...scope }), cap) + scope.deadlineAt += 10000 + const snapshot = await f.api.inspectAutomaticSchedulerAuthority(cap) + assert.equal(snapshot.job, job); assert.equal(snapshot.generation, 0); assert.equal(snapshot.deadlineAt, originalDeadline) + assert.equal(snapshot.monotonicDeadline, originalMonoEnd) + f.state.wall -= 100; f.state.mono += 10 + assert.equal((await f.api.inspectAutomaticSchedulerAuthority(cap)).monotonicDeadline, originalMonoEnd) + assert(Object.isFrozen(snapshot)); assert(Object.isFrozen(cap)); assert.equal(Object.keys(cap).length, 0) + assert.match(snapshot.executionId, /^automatic-process-execution-\d+$/); ids.push(snapshot.executionId) + assert.throws(() => f.api.captureAutomaticSchedulerAuthority(scope), refused) + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority({ ...cap }), refused) + } + const ticket = f.api.createAutomaticSchedulerTicket("uptime_speed", 0) + await f.run("uptime_speed", ticket); await f.run("uptime_speed", ticket) + await f.run("router_logs_live"); await f.run("router_logs_retention") + assert.equal(new Set(ids).size, 4) +}) +test("requested deadline/signal clip inspection without renewing first capture", async () => { + const f = fixture() + f.state.hook = async () => { + const owner = new AbortController(), scope = { ...f.scope(), signal: owner.signal } + const cap = f.api.captureAutomaticSchedulerAuthority(scope), short = new AbortController() + const clipped = await f.api.inspectAutomaticSchedulerAuthority(cap, { deadlineAt: scope.deadlineAt - 500, signal: short.signal }) + assert.equal(clipped.deadlineAt, scope.deadlineAt - 500); short.abort(); assert.equal(clipped.signal.aborted, true) + const original = await f.api.inspectAutomaticSchedulerAuthority(cap, { deadlineAt: scope.deadlineAt + 10000 }) + assert.equal(original.deadlineAt, scope.deadlineAt); assert.equal(original.signal.aborted, false) + assert.throws(() => f.api.captureAutomaticSchedulerAuthority({ ...scope, signal: new AbortController().signal }), refused) + owner.abort(); assert.equal(original.signal.aborted, true); await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused) + assert.throws(() => f.api.captureAutomaticSchedulerAuthority(scope), refused) + } + await f.run() +}) +test("nonfinite/accessor/inherited/expired/aborted scopes cannot capture", async () => { + const f = fixture() + f.state.hook = async () => { + const abort = new AbortController(); abort.abort() + for (const scope of [{ deadlineAt: Infinity }, { deadlineAt: NaN }, { deadlineAt: 1.5 }, { deadlineAt: f.state.wall }, + { ...f.scope(), signal: abort.signal }, { ...f.scope(), job: "backups" }, Object.create({ deadlineAt: f.state.wall + 1000 }), + { get deadlineAt() { assert.fail("getter executed") } }]) assert.throws(() => f.api.captureAutomaticSchedulerAuthority(scope), refused) + const cap = f.api.captureAutomaticSchedulerAuthority(f.scope()) + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap, { deadlineAt: Infinity }), refused) + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap, { deadlineAt: f.state.wall }), refused) + } + await f.run() +}) +test("generation and abort are checked again after inspect await", async () => { + for (const change of ["generation", "abort", "deadline"]) { + const f = fixture() + f.state.hook = async () => { + const controller = new AbortController(), cap = f.api.captureAutomaticSchedulerAuthority({ ...f.scope(), signal: controller.signal }) + const pending = f.api.inspectAutomaticSchedulerAuthority(cap) + if (change === "generation") f.state.generation++ + if (change === "abort") controller.abort() + if (change === "deadline") f.state.mono += 1001 + await assert.rejects(pending, refused) + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused) + } + await f.run() + } +}) +test("wall rollback cannot extend original monotonic horizon", async () => { + const f = fixture() + f.state.hook = async () => { + const cap = f.api.captureAutomaticSchedulerAuthority(f.scope()) + f.state.wall -= 100000; f.state.mono += 1001 + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused) + } + await f.run() +}) +test("foreign ALS/resource and parent context cannot inspect another execution", async () => { + const f = fixture(), entered = deferred(), release = deferred(), foreign = new AsyncResource("foreign-root") + let cap + f.state.hook = async job => { + if (job === "backups") { await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused); return } + cap = f.api.captureAutomaticSchedulerAuthority(f.scope()); entered.resolve() + await foreign.runInAsyncScope(() => assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused)) + await release.promise + assert.equal((await f.api.inspectAutomaticSchedulerAuthority(cap)).job, "uptime_speed") + } + const work = f.run(); await entered.promise + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused) + await f.run("backups"); release.resolve(); await work; foreign.emitDestroy() +}) +test("completion/failure invalidate inherited detached child authority", async () => { + for (const failure of [false, true]) { + const f = fixture(), release = deferred(); let late, cap, snapshot + f.state.hook = async () => { + cap = f.api.captureAutomaticSchedulerAuthority(f.scope()) + snapshot = await f.api.inspectAutomaticSchedulerAuthority(cap) + assert.equal(snapshot.signal.aborted, false) + late = release.promise.then(async () => { + assert.equal(snapshot.signal.aborted, true) + assert.throws(() => f.api.captureAutomaticSchedulerAuthority(f.scope()), refused) + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused) + }) + if (failure) throw Error("owned failure") + } + if (failure) await assert.rejects(f.run()); else await f.run() + assert.equal(snapshot.signal.aborted, true) + release.resolve(); await late + await assert.rejects(f.api.inspectAutomaticSchedulerAuthority(cap), refused) + } +}) diff --git a/backend/src/services/scheduler-automatic-root.sql-helper.ts b/backend/src/services/scheduler-automatic-root.sql-helper.ts new file mode 100644 index 00000000..a2e7e935 --- /dev/null +++ b/backend/src/services/scheduler-automatic-root.sql-helper.ts @@ -0,0 +1,197 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +/** Finite owned acceptance of the REAL MCP settings -> automatic scheduler + * handoff. Native responses come only from localhost HTTP, never fabricated SQL. */ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { once } from "node:events" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { isDeepStrictEqual } from "node:util" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +export async function acceptAutomaticSchedulerSql(ownServerIds: number[]) { + assertOwnedBandwidthSql() + assert.equal(ownServerIds.length, 2); assert.equal(new Set(ownServerIds).size, 2) + assert(ownServerIds.every(id => Number.isSafeInteger(id) && id > 0)) + const ids = [...ownServerIds], { pool } = await import("../db/index.js") + const { initializeAutomaticSchedulerRoot } = await import("./scheduler-automatic-root.js") + const { stopSchedulerForDatabaseOperation } = await import("./scheduler.js") + const { resumeSchedulerJobs, isSchedulerJobsPaused } = await import("./scheduler-running.js") + const { databaseBackgroundWork } = await import("./database-background-work.js") + const { assertNoOperationConfigurationLockContext } = await import("./operation-config-lock-lease.js") + const { assertNoConfigurationLockContext } = await import("./config-mutation-lock.js") + const { assertNoVerifiedConfigurationContext } = await import("./config-mutation-policy.js") + const { MikrotikClient } = await import("./mikrotik.js") + const { createMcpConnection, authenticateMcpToken } = await import("../modules/mcp/store.js") + const { invokeMcpTool, applyPlan, approvePlan } = await import("../modules/mcp/runtime.js") + const { findAction } = await import("../modules/mcp/actions.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { mcpStateDirectory } = await import("../modules/mcp/security.js") + const { readFile, lstat, unlink } = await import("node:fs/promises") + const { join } = await import("node:path") + // Bootstrap is explicit and occurs before entering ANY operation/verified scope. + initializeAutomaticSchedulerRoot() + const wasPaused = isSchedulerJobsPaused() + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM public.schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + const singletonTables = ["router_log_settings", "traffic_settings", "servers_api_ping_settings", "uptime_settings", "internet_path_settings", "certificate_renew_settings", "backup_schedule_settings", "geoip_settings", "mcp_settings", "alert_engine_cursor"] as const + const snapshots = new Map | undefined>() + for (const table of singletonTables) snapshots.set(table, (await pool.query(`SELECT to_jsonb(t) AS row FROM public.${table} t WHERE id=1`)).rows[0]?.row) + const serverRows = (await pool.query("SELECT to_jsonb(t) AS row FROM public.servers t WHERE id=ANY($1::bigint[]) ORDER BY id", [ids])).rows.map(row => row.row as Record) + assert.equal(serverRows.length, 2); assert(serverRows.every(row => row.enabled === false)) + assert.equal((await pool.query("SELECT id FROM public.servers WHERE enabled AND NOT(id=ANY($1::bigint[])) LIMIT 1", [ids])).rows.length, 0) + assert.equal((await pool.query("SELECT id FROM public.uptime_speed_probes WHERE enabled LIMIT 1")).rows.length, 0) + assert.equal((await pool.query("SELECT id FROM public.alert_rules LIMIT 1")).rows.length, 0) + assert.equal((await pool.query("SELECT id FROM public.alert_groups LIMIT 1")).rows.length, 0) + assert.equal((await pool.query("SELECT id FROM public.alert_outbox LIMIT 1")).rows.length, 0) + const telegram = (await pool.query("SELECT bot_token,chat_id FROM public.alert_telegram_settings WHERE id=1")).rows[0] + assert(!telegram?.bot_token && !telegram?.chat_id, "Owned scheduler fixture must have no Telegram delivery target") + let baselineRuns = new Set(), baselineEvents = new Set(), baselineAudits = new Set(), baselineCaptured = false + const keySnapshots = new Map(), createdKeys = new Map() + for (const name of ["access-generation", "encryption-key"]) { + const file = join(mcpStateDirectory(), name) + try { const stat = await lstat(file); assert(stat.isFile() && !stat.isSymbolicLink() && stat.size === 32); keySnapshots.set(file, await readFile(file)) } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; keySnapshots.set(file, undefined) } + } + const probeId = "owned-automatic-speed-" + randomUUID(), protectionId = randomUUID(), connections: string[] = [], checks: string[] = [] + const captureCreatedKeys = async () => { + for (const [file, previous] of keySnapshots) if (!previous && !createdKeys.has(file)) { + try { const stat = await lstat(file); assert(stat.isFile() && !stat.isSymbolicLink() && stat.size === 32); createdKeys.set(file, await readFile(file)) } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error } + } + } + const originalBandwidth = MikrotikClient.prototype.bandwidthTest + let bandwidthPosts = 0, pingPosts = 0, nativeGetRequests = 0, scopeChecks = 0, protocolErrors = 0, currentOperation: string | undefined + const router = createServer(async (request, response) => { + response.setHeader("content-type", "application/json") + try { + const auth = Buffer.from((request.headers.authorization ?? "").slice(6), "base64").toString() + assert(ids.some(id => auth === `scheduler-${id}:OWNED_LOCALHOST_PRIVATE`)) + if (request.method === "GET") { nativeGetRequests++; response.end("[]"); return } + assert.equal(request.method, "POST"); assert.equal(auth, `scheduler-${ids[0]}:OWNED_LOCALHOST_PRIVATE`) + let text = ""; for await (const chunk of request) { text += String(chunk); assert(Buffer.byteLength(text) <= 65536) } + const body = JSON.parse(text) + if (request.url === "/rest/tool/bandwidth-test") { + assert(body.address === "127.0.0.1" && body.user === "scheduler-" + ids[1] && body.password === "OWNED_LOCALHOST_PRIVATE") + assert.equal(body.duration, "3s"); assert.equal(body.protocol, "tcp"); assert.equal(body.direction, "both") + bandwidthPosts++; response.end(JSON.stringify([{ status: "done testing", "tx-total-average": "1000000", "rx-total-average": "2000000" }])) + } else { + assert.equal(request.url, "/rest/tool/ping"); assert.equal(body.count, "4") + pingPosts++; response.end(JSON.stringify([{ sent: "4", received: "4", "packet-loss": "0%", "avg-rtt": "1ms" }])) + } + } catch { protocolErrors++; response.destroy() } + }) + const restoreRow = async (table: string, row: Record | undefined) => { + assert([...singletonTables, "servers"].includes(table)) + if (!row) { await pool.query(`DELETE FROM public.${table} WHERE id=1`); return } + const columns = Object.keys(row).filter(name => name !== "id" && name !== "routing_endpoint_epoch") + assert(columns.every(name => /^[a-z_][a-z_0-9]*$/.test(name))) + await pool.query(`UPDATE public.${table} SET (${columns.join(",")})=(SELECT ${columns.join(",")} FROM pg_catalog.jsonb_populate_record(NULL::public.${table},$1::jsonb)) WHERE id=$2`, [JSON.stringify(row), row.id]) + } + const terminal = async (operationId: string) => { + for (const until = Date.now() + 60000; Date.now() < until;) { + const row = (await pool.query("SELECT state FROM public.mcp_operations WHERE id=$1", [operationId])).rows[0] + assert(row); if (!["pending", "running"].includes(row.state)) { assert.equal(row.state, "applied"); return } + await delay(20) + } + assert.fail("Owned settings operation did not complete") + } + const waitSpeed = async (expectedCount: number, status: string) => { + for (const until = Date.now() + 60000; Date.now() < until;) { + const rows = (await pool.query("SELECT status,error,ping_error,duration_sec,src_interface,dst_interface FROM public.uptime_speed_test_runs WHERE probe_id=$1 ORDER BY created_at,id", [probeId])).rows + if (rows.length >= expectedCount) { + assert.equal(rows.length, expectedCount); const row = rows[expectedCount - 1] + assert.equal(row.status, status); assert.equal(row.duration_sec, 3); assert.equal(row.src_interface, ""); assert.equal(row.dst_interface, "") + if (status === "done") assert.equal(row.ping_error, null) + else assert(typeof row.error === "string" && row.error.length > 0) + return + } + await delay(20) + } + assert.fail("Owned automatic speed run did not publish history") + } + try { + await stopSchedulerForDatabaseOperation(); await databaseBackgroundWork.drain() + baselineRuns = new Set((await pool.query("SELECT id FROM public.scheduler_runs")).rows.map(row => row.id as string)) + baselineEvents = new Set((await pool.query("SELECT id FROM public.events")).rows.map(row => row.id as string)) + baselineAudits = new Set((await pool.query("SELECT id::text AS id FROM public.mcp_audit")).rows.map(row => row.id as string)) + baselineCaptured = true + router.listen(0, "127.0.0.1"); await once(router, "listening") + const address = router.address(); assert(address && typeof address !== "string") + for (const table of singletonTables.filter(table => table !== "mcp_settings" && table !== "alert_engine_cursor")) { + await pool.query(`INSERT INTO public.${table}(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false`) + } + await pool.query("UPDATE public.uptime_settings SET resources_enabled=false,ping_enabled=false,speed_enabled=false WHERE id=1") + await pool.query("UPDATE public.mcp_settings SET enabled=true WHERE id=1") + await pool.query("UPDATE public.servers SET host='127.0.0.1',port=$2,use_ssl=false,verify_ssl=true,username='scheduler-'||id::text,password='OWNED_LOCALHOST_PRIVATE',enabled=true WHERE id=ANY($1::bigint[])", [ids, address.port]) + await pool.query("INSERT INTO public.uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,dst_interface,protocol,direction,duration_sec,enabled) VALUES($1,$2,$3,'','','tcp','both',3,true)", [probeId, ids[0], ids[1]]) + MikrotikClient.prototype.bandwidthTest = async function (...args) { + assertNoOperationConfigurationLockContext(); assertNoConfigurationLockContext(); assertNoVerifiedConfigurationContext(); scopeChecks++ + assert(currentOperation) + assert.equal((await pool.query("SELECT state FROM public.mcp_operations WHERE id=$1", [currentOperation])).rows[0]?.state, "applied") + return originalBandwidth.apply(this, args) + } + const actor = { id: randomUUID(), email: "owned-scheduler@example.test", name: "Owned scheduler handoff", apps: ["mm"], + permissions: ["mm:uptime:read", "mm:uptime:write", "mm:servers:read", "mm:mcp:approve"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 300000 } + const allIds = (await pool.query("SELECT id FROM public.servers ORDER BY id")).rows.map(row => Number(row.id)) + const action = findAction("mm_uptime_settings_update_plan"); assert.equal(action.operation.id, "uptime.settings.update") + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned automatic scheduler MCP parent", mode: "approval", expiresAt: new Date(actor.expiresAt).toISOString(), + permissions: ["mm:uptime:read", "mm:uptime:write", "mm:servers:read"], scope: { serverIds: allIds, operationIds: [action.operation.id], allowGlobal: true }, limits: { callsPerMinute: 120, mutationsPerHour: 1000 } }), actor) + connections.push(access.connection.id) + await captureCreatedKeys() + const principal = await authenticateMcpToken(access.token) + const planRefresh = async () => { + const prepared = await invokeMcpTool(principal, "mm_uptime_settings_update_plan", { settings: { resourcesEnabled: false, pingEnabled: false, speedEnabled: true, speedIntervalSec: 3600 } }) as { planId: string; state: string } + await captureCreatedKeys() + assert.equal(prepared.state, "awaiting-approval"); await approvePlan(prepared.planId, actor, true) + const operation = await applyPlan(principal, prepared.planId, randomUUID()); currentOperation = operation.id + await terminal(operation.id) + } + resumeSchedulerJobs() + await planRefresh(); await waitSpeed(1, "done") + assert.equal(bandwidthPosts, 1); assert.equal(pingPosts, 1); assert.equal(protocolErrors, 0) + checks.push("real human-approved global uptime settings operation applies; immediate automatic root runs bandwidth plus after-ping once with original duration/body and no inherited operation/configuration/verified context") + await stopSchedulerForDatabaseOperation(); await databaseBackgroundWork.drain() + await pool.query("INSERT INTO public.mcp_protections(id,object_ref,reason,created_by) VALUES($1,$2::jsonb,'owned scheduler no approval inheritance',$3)", [protectionId, JSON.stringify({ kind: "server", id: String(ids[0]), serverId: ids[0], protection: "human-approval-required" }), actor.id]) + await pool.query("UPDATE public.uptime_speed_probes SET last_run_at=NULL WHERE id=$1", [probeId]) + resumeSchedulerJobs(); await planRefresh(); await waitSpeed(2, "error") + assert.equal(bandwidthPosts, 1); assert.equal(pingPosts, 1); assert.equal(scopeChecks, 2); assert.equal(protocolErrors, 0) + checks.push("a second approved settings parent does not grant approval to automatic speed work: protected source yields real error history before another bandwidth or ping POST") + return { checks, cases: 2, bandwidthPosts, afterPingPosts: pingPosts, automaticScopeChecks: scopeChecks, actualLocalhostGets: nativeGetRequests, realChrCalls: 0, productionActivation: false, restoredEndpointEpochPolicy: "new-genuine-epoch" } + } finally { + // Never restore/delete fixture state while a real collector still runs. + await stopSchedulerForDatabaseOperation(); await databaseBackgroundWork.drain() + try { + MikrotikClient.prototype.bandwidthTest = originalBandwidth + router.closeAllConnections(); if (router.listening) await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + await pool.query("DELETE FROM public.mcp_protections WHERE id=$1", [protectionId]) + await pool.query("DELETE FROM public.uptime_speed_test_runs WHERE probe_id=$1", [probeId]) + await pool.query("DELETE FROM public.uptime_speed_probes WHERE id=$1", [probeId]) + if (connections.length) { + for (const table of ["mcp_operation_checkpoints", "mcp_operation_steps"]) await pool.query(`DELETE FROM public.${table} WHERE operation_id IN(SELECT id FROM public.mcp_operations WHERE connection_id=ANY($1::uuid[]))`, [connections]) + for (const table of ["mcp_operations", "mcp_plans", "mcp_requests", "mcp_audit"]) await pool.query(`DELETE FROM public.${table} WHERE connection_id=ANY($1::uuid[])`, [connections]) + await pool.query("DELETE FROM public.mcp_connections WHERE id=ANY($1::uuid[])", [connections]) + } + if (baselineCaptured) { + const ownAudits = (await pool.query("SELECT id::text AS id FROM public.mcp_audit WHERE connection_id IS NULL AND payload->>'serverId'=ANY($1::text[])", [ids.map(String)])).rows.map(row => row.id as string).filter(id => !baselineAudits.has(id)) + if (ownAudits.length) await pool.query("DELETE FROM public.mcp_audit WHERE id=ANY($1::bigint[])", [ownAudits]) + const runIds = (await pool.query("SELECT id FROM public.scheduler_runs")).rows.map(row => row.id as string).filter(id => !baselineRuns.has(id)) + const eventIds = (await pool.query("SELECT id FROM public.events")).rows.map(row => row.id as string).filter(id => !baselineEvents.has(id)) + if (runIds.length) await pool.query("DELETE FROM public.scheduler_runs WHERE id=ANY($1::text[])", [runIds]) + if (eventIds.length) await pool.query("DELETE FROM public.events WHERE id=ANY($1::text[])", [eventIds]) + } + for (const row of serverRows) await restoreRow("servers", row) + const restoredServers = (await pool.query("SELECT to_jsonb(t) AS row FROM public.servers t WHERE id=ANY($1::bigint[]) ORDER BY id", [ids])).rows.map(row => row.row as Record) + assert.equal(restoredServers.length, serverRows.length) + for (const [index, restored] of restoredServers.entries()) { + const { routing_endpoint_epoch: oldEpoch, ...original } = serverRows[index], { routing_endpoint_epoch: freshEpoch, ...current } = restored + assert(isDeepStrictEqual(current, original), "Owned server configuration restoration mismatch") + assert(typeof freshEpoch === "string" && freshEpoch !== oldEpoch, "Restoration must retain a new genuine endpoint epoch") + } + for (const table of singletonTables) await restoreRow(table, snapshots.get(table)) + for (const [file, previous] of keySnapshots) { + if (previous) { assert((await readFile(file)).equals(previous), "Original fixture MCP key unexpectedly changed"); previous.fill(0) } + else { const created = createdKeys.get(file); if (created) { assert((await readFile(file)).equals(created), "Created fixture MCP key unexpectedly changed"); await unlink(file); created.fill(0) } } + } + } finally { if (!wasPaused) resumeSchedulerJobs() } + } +} diff --git a/backend/src/services/scheduler-automatic-root.test.mjs b/backend/src/services/scheduler-automatic-root.test.mjs new file mode 100644 index 00000000..c1d57ba0 --- /dev/null +++ b/backend/src/services/scheduler-automatic-root.test.mjs @@ -0,0 +1,291 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { createRequire } from "node:module" +import { runInNewContext } from "node:vm" +import { AsyncLocalStorage, AsyncResource } from "node:async_hooks" +import { EventEmitter } from "node:events" +import { test } from "node:test" +import { setTimeout as delay } from "node:timers/promises" +const require = createRequire(new URL("../../package.json", import.meta.url)), ts = require("typescript") +const source = name => readFile(new URL("../" + name, import.meta.url), "utf8") +const actual = name => readFile(new URL("../" + name, import.meta.url), "utf8") +const deferred = () => { let resolve; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function load(text, providers, globals = {}) { + const compiled = ts.transpileModule(text, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, esModuleInterop: true } }).outputText + const exports = {} + runInNewContext(compiled, { exports, require(name) { assert(name in providers, "Unexpected dependency " + name); return providers[name] }, + Buffer, AbortController, AbortSignal, setTimeout, clearTimeout, setInterval, clearInterval, structuredClone, console, ...globals }) + return exports +} + +test("fixed scheduler ownership: real ALS/AsyncResource, fake I/O", async t => { + const freshPool = { checkouts: 0, queries: 0, async connect() { this.checkouts++; return { query: async text => { await freshPool.queryHook?.(text); freshPool.queries++; return { rows: [{ acquired: true }] } }, release() {} } } } + const asyncProvider = { AsyncLocalStorage, AsyncResource } + const op = load(await source("services/operation-config-lock-lease.ts"), { "node:async_hooks": asyncProvider }) + const { DatabaseRequestGate } = load(await source("services/database-request-gate.ts"), { "node:async_hooks": asyncProvider }) + const databaseGate = new DatabaseRequestGate() + const requestLock = { SYSTEM_DATABASE_REQUEST_LOCK: 8_723_130 } + class McpError extends Error { constructor(code, message) { super(message); this.code = code } } + const leaseState = { holders: 0, checkouts: 0, maintenance: false } + const leases = load(await source("modules/mcp/database-request-lease.ts"), { + "node:async_hooks": asyncProvider, "../../db/operation-lock.js": requestLock, "./security.js": { McpError }, + "../../db/index.js": { createPool: () => ({ on() {}, async connect() { + assert(databaseGate.active.size > 0, "SQL admission follows the local request gate") + leaseState.checkouts++ + const client = new EventEmitter() + let held = false + client.query = async (sql, args) => { + assert.deepEqual(Array.from(args), [requestLock.SYSTEM_DATABASE_REQUEST_LOCK]) + if (sql.includes("pg_try_advisory_lock_shared")) { + if (leaseState.maintenance) return { rows: [{ acquired: false }] } + held = true; leaseState.holders++ + return { rows: [{ acquired: true }] } + } + assert.match(sql, /pg_advisory_unlock_shared/); assert(held) + held = false; leaseState.holders-- + return { rows: [{ released: true }] } + } + client.release = () => assert.equal(held, false) + return client + } }) }, + }) + const locks = load(await source("services/config-mutation-lock.ts"), { "node:async_hooks": asyncProvider, "../db/index.js": { pool: freshPool }, "../db/operation-lock.js": requestLock, "../modules/mcp/security.js": { McpError }, "../modules/mcp/database-request-lease.js": leases, "./operation-config-lock-lease.js": op }) + const policy = load(await source("services/config-mutation-policy.ts"), { "node:async_hooks": asyncProvider, "node:crypto": require("node:crypto"), "node:perf_hooks": require("node:perf_hooks"), "node:util": require("node:util"), + "../db/index.js": { pool: freshPool }, "../modules/mcp/store.js": { audit: async () => { throw Error("Unexpected audit I/O") } }, "../modules/mcp/security.js": { McpError }, "./config-mutation-lock.js": locks, + "../modules/mcp/routing-controller-active-writer.js": { assertActiveRoutingProfileWriter: async () => { throw Error("Unexpected native writer in fake scheduler collectors") } } }) + const running = load(await actual("services/scheduler-running.ts"), {}) + const background = load(await actual("services/database-background-work.ts"), {}) + const alert = load(await source("services/alert-collector-hooks.ts"), { "./scheduler-running.js": running }) + let scheduler, logsRuns = 0 + const rootProviders = { "node:async_hooks": asyncProvider, "./operation-config-lock-lease.js": op, "./config-mutation-lock.js": locks, "./config-mutation-policy.js": policy, + "../modules/mcp/database-request-lease.js": leases, + "./shared-database-gate.js": { sharedDatabaseGate: databaseGate }, "./database-background-work.js": background, "./alert-collector-hooks.js": alert, + get "./scheduler.js"() { return scheduler }, "./router-logs-collector.js": { async runRouterLogsLiveOnce() { policy.assertNoVerifiedConfigurationContext(); op.assertNoOperationConfigurationLockContext(); logsRuns++ }, async runRouterLogsRetentionOnce() { policy.assertNoVerifiedConfigurationContext(); op.assertNoOperationConfigurationLockContext(); logsRuns++ } } } + const automatic = load(await source("services/scheduler-automatic-root.ts"), rootProviders) + const state = { enabled: true, work: 0, auto: 0, manual: 0, alert: 0, hook: undefined } + const collect = async () => { + let inherited = false + try { policy.assertNoVerifiedConfigurationContext() } catch { inherited = true } + if (inherited) { state.manual++; await policy.assertVerifiedConfigurationAccess() } else { state.auto++; op.assertNoOperationConfigurationLockContext(); assert.equal(databaseGate.active.size, 1, "Automatic work owns a fresh request admission") } + return locks.withApplicationMutationLock(() => locks.withConfigMutationLock([11], async () => { state.work++; await state.hook?.(); return { job: "backups", sampledAt: new Date().toISOString(), skipped: false } })) + } + const settings = async () => ({ enabled: state.enabled, intervalSec: 60, resourcesEnabled: state.enabled, pingEnabled: state.enabled, speedEnabled: state.enabled }) + const schedulerText = await source("services/scheduler.ts") + const schedulerProviders = { + "drizzle-orm": {}, "../db/index.js": { db: {} }, "../db/schema.js": {}, "../types/scheduler-run-snapshot.js": { SCHEDULER_RUN_SNAPSHOT_VERSION: 1 }, + "./servers-rest-ping-collector.js": { getServersApiPingSettings: settings }, "./traffic-collector.js": { getTrafficSettings: settings }, "./uptime-collector.js": { getSettings: settings }, + "./uptime-speed-service.js": {}, "./gre-bgp-snapshot-collector.js": {}, "./gre-bgp-timing.js": { GRE_BGP_INTERVAL_MS: 60000 }, + "./alert-engine/run-once.js": { async runAlertEngineOnce() { policy.assertNoVerifiedConfigurationContext(); op.assertNoOperationConfigurationLockContext(); state.alert++; return { sampledAt: "owned", rulesChecked: 0, standaloneFires: 0, groupFires: 0, skippedNoTelegram: 0, ruleDiag: [], errors: [] } } }, + "./alert-collector-hooks.js": alert, "./internet-path-collector.js": { getInternetPathSettings: settings }, "./certificate-renew-collector.js": {}, "./certificates-service.js": { getCertificateRenewSettings: settings }, + "./backup-scheduler-collector.js": { collectScheduledBackupsOnce: collect }, "./backup-service.js": { getBackupScheduleSettings: settings }, "./geoip-update-collector.js": {}, "./geoip-settings.js": { getGeoipSettings: settings }, + "./router-logs-collector.js": { getLogSettings: settings, startLogsLiveTimer() {}, stopLogsLiveTimer() {} }, "./scheduler-running.js": running, + "../modules/events/service/events-service.js": {}, "./scheduler-recorded-run.js": { async runRecordedSchedulerJob(deps) { return { status: "ok", result: await deps.collect() } } }, "./scheduler-automatic-root.js": automatic, + } + scheduler = load(schedulerText, schedulerProviders) + const parent = work => databaseGate.request(() => policy.withConfigurationActor("owned-mcp-actor", () => policy.withVerifiedConfigurationChange({ actorId: "owned-mcp-actor", connectionId: "owned", approved: false, serverIds: [11], objects: [], serverWide: true, authorize: async () => {} }, async () => { + const owner = new AbortController(), client = { query: async () => ({ rows: [{ acquired: true }] }) } + return op.withOperationConfigurationLockClient(client, () => owner.abort(), owner.signal, () => locks.withApplicationMutationLock(() => locks.withConfigMutationLock([11], work))) + }))) + await t.test("root must exist before refresh and bootstrap rejects every inherited context even after initialization", async () => { + assert.throws(() => automatic.requireAutomaticSchedulerRoot()); await assert.rejects(scheduler.refreshScheduler()) + automatic.initializeAutomaticSchedulerRoot() + await assert.rejects(databaseGate.request(async () => automatic.initializeAutomaticSchedulerRoot())) + await assert.rejects(policy.withConfigurationActor("request", async () => automatic.initializeAutomaticSchedulerRoot())) + await assert.rejects(policy.withVerifiedConfigurationChange({ actorId: "owned", connectionId: "owned", approved: false, serverIds: [], objects: [], serverWide: false, authorize: async () => {} }, async () => automatic.initializeAutomaticSchedulerRoot())) + await assert.rejects(locks.withApplicationMutationLock(async () => automatic.initializeAutomaticSchedulerRoot())) + const release = deferred(); let closedLock + await locks.withApplicationMutationLock(async () => { closedLock = assert.rejects(release.promise.then(() => automatic.initializeAutomaticSchedulerRoot())) }) + release.resolve(); await closedLock + await parent(async () => { assert.throws(() => automatic.initializeAutomaticSchedulerRoot()) }) + }) + await t.test("parent retired: genuine automatic next tick gets fresh locks; arbitrary child and bootstrap still reject", async () => { + const release = deferred(); let automaticChild, lateChild, bootstrapChild + await parent(async () => { + const ticket = automatic.createAutomaticSchedulerTicket("backups", 0) + automaticChild = release.promise.then(() => automatic.runAutomaticSchedulerTicket(ticket)) + lateChild = assert.rejects(release.promise.then(() => locks.withConfigMutationLock([11], async () => { throw Error("Stale child reached work") }))) + bootstrapChild = assert.rejects(release.promise.then(() => automatic.initializeAutomaticSchedulerRoot())) + }) + const checkouts = freshPool.checkouts; release.resolve(); await Promise.all([automaticChild, lateChild, bootstrapChild]) + assert(freshPool.checkouts > checkouts); assert.equal(state.work, 1); assert.equal(state.auto, 1) + }) + await t.test("manual run-now keeps original caller/approval scope and performs no automatic handoff", async () => { + const before = state.manual + await parent(async () => { await scheduler.runSchedulerJobNow("backups") }) + assert.equal(state.manual, before + 1) + }) + await t.test("immediate automatic handoff waits for parent advisory lock retirement and then runs once", async () => { + const before = state.work, checkouts = leaseState.checkouts; let immediate + await parent(async () => { + const ticket = automatic.createAutomaticSchedulerTicket("backups", 0) + immediate = automatic.runAutomaticSchedulerTicket(ticket) + await delay(5); assert.equal(state.work, before, "Immediate job must not race parent locks") + assert.equal(background.databaseBackgroundWork.pending.size, 0, "Queued handoff must not join its parent's background drain") + assert.equal(databaseGate.active.size, 1, "Only original parent request is admitted before handoff") + assert.equal(leaseState.checkouts, checkouts, "Handoff must not own a SQL lease") + await background.databaseBackgroundWork.drain(50) + }) + await immediate; assert.equal(state.work, before + 1) + }) + await t.test("shared SQL lease spans admitted collection and refuses a tick during remote maintenance", async () => { + const ticket = automatic.createAutomaticSchedulerTicket("backups", 0) + const before = state.work, entered = deferred(), finish = deferred() + state.hook = async () => { assert.equal(leaseState.holders, 1); entered.resolve(); await finish.promise } + const job = automatic.runAutomaticSchedulerTicket(ticket) + await Promise.race([entered.promise, job.then(() => assert.fail("Collector did not enter"))]) + assert.equal(leaseState.holders, 1) + finish.resolve(); await job + assert.equal(leaseState.holders, 0) + state.hook = undefined + leaseState.maintenance = true + try { await assert.rejects(automatic.runAutomaticSchedulerTicket(ticket), error => error.code === "PROTOCOL_STORAGE_BUSY") } + finally { leaseState.maintenance = false } + assert.equal(state.work, before + 1) + await automatic.runAutomaticSchedulerTicket(ticket) + assert.equal(state.work, before + 2) + }) + await t.test("owner abort does not complete handoff while parent work/native await is still stalled", async () => { + const before = state.work, owner = new AbortController(), entered = deferred(), settle = deferred(), late = deferred() + const client = { query: async () => ({ rows: [{ acquired: true }] }) } + let job, lateRejected + const original = op.withOperationConfigurationLockClient(client, () => {}, owner.signal, async () => { + job = automatic.runAutomaticSchedulerTicket(automatic.createAutomaticSchedulerTicket("backups", 0)) + lateRejected = assert.rejects(late.promise.then(() => locks.withConfigMutationLock([11], async () => { throw Error("Late child entered work") }))) + entered.resolve(); await settle.promise + }) + const rejected = assert.rejects(original) + await entered.promise; owner.abort(); late.resolve(); await lateRejected; await delay(5) + assert.equal(state.work, before); assert.equal(background.databaseBackgroundWork.pending.size, 0) + await background.databaseBackgroundWork.drain(50) + settle.resolve(); await rejected; await job; assert.equal(state.work, before + 1) + }) + await t.test("automatic ticks coalesce outside exclusive admission; manual requests still reject 409", async () => { + const before = state.work, exclusiveEntered = deferred(), releaseExclusive = deferred() + let jobs + const exclusive = databaseGate.exclusive(async () => { + const owner = new AbortController(), client = { query: async () => ({ rows: [] }) } + await op.withOperationConfigurationLockClient(client, () => {}, owner.signal, async () => { + const ticket = automatic.createAutomaticSchedulerTicket("backups", 0) + jobs = Array.from({ length: 20 }, () => automatic.runAutomaticSchedulerTicket(ticket)) + }) + exclusiveEntered.resolve(); await releaseExclusive.promise + }) + await exclusiveEntered.promise; await delay(10) + assert.equal(state.work, before); assert.equal(background.databaseBackgroundWork.pending.size, 0) + assert.equal(databaseGate.active.size, 0) + await assert.rejects(databaseGate.request(async () => assert.fail("Manual request admitted during exclusive")), e => e.statusCode === 409) + await background.databaseBackgroundWork.drain(50) + releaseExclusive.resolve(); await exclusive; await Promise.all(jobs) + assert.equal(state.work, before + 1, "Twenty pending ticks must cause exactly one collection") + for (const mode of ["disabled", "paused", "stale"]) { + const entered = deferred(), release = deferred(); let queued + const maintenance = databaseGate.exclusive(async () => { + queued = automatic.runAutomaticSchedulerTicket(automatic.createAutomaticSchedulerTicket("backups", mode === "stale" ? 999 : 0)) + entered.resolve(); await release.promise + }) + await entered.promise; await delay(2) + if (mode === "disabled") state.enabled = false + if (mode === "paused") await running.pauseSchedulerJobsAndWait() + release.resolve(); await maintenance; await queued + assert.equal(state.work, before + 1) + state.enabled = true; running.resumeSchedulerJobs() + } + }) + await t.test("REST without operation waits for nested app/server unlock and DB gate opening", async () => { + const before = state.work, releaseParent = deferred(), gateOpened = deferred(), entered = deferred() + let job, lateChild; const late = deferred() + const parentWork = locks.withApplicationMutationLock(() => locks.withConfigMutationLock([11], () => locks.withConfigMutationLock([12], async () => { + await databaseGate.exclusive(async () => { + const ticket = automatic.createAutomaticSchedulerTicket("backups", 0) + job = automatic.runAutomaticSchedulerTicket(ticket) + lateChild = late.promise.then(() => assert.throws(() => automatic.createAutomaticSchedulerTicket("backups", 0))) + }) + gateOpened.resolve(); entered.resolve(); await releaseParent.promise + }))) + await entered.promise; await gateOpened.promise; await delay(10) + assert.equal(databaseGate.isPaused(), false) + assert.equal(state.work, before); assert.equal(background.databaseBackgroundWork.pending.size, 0) + assert.equal(databaseGate.active.size, 0) + const unlocks = Array.from({ length: 3 }, () => ({ entered: deferred(), release: deferred() })); let unlockIndex = 0 + freshPool.queryHook = async text => { + if (text.includes("pg_advisory_unlock") && unlockIndex < unlocks.length) { + const gate = unlocks[unlockIndex++]; gate.entered.resolve(); await gate.release.promise + } + } + releaseParent.resolve() + for (const gate of unlocks) { + await gate.entered.promise; await delay(2) + assert.equal(state.work, before, "Ticket must wait until each server/application unlock finishes") + gate.release.resolve() + } + await parentWork; freshPool.queryHook = undefined; await job + assert.equal(unlockIndex, 3); assert.equal(state.work, before + 1) + late.resolve(); await lateChild + }) + await t.test("forged/stale/paused/disabled/busy tickets do zero collector work", async () => { + const ticket = automatic.createAutomaticSchedulerTicket("backups", 0), before = state.work + await assert.rejects(automatic.runAutomaticSchedulerTicket({ ...ticket })); assert.throws(() => automatic.createAutomaticSchedulerTicket("arbitrary-code", 0)) + state.enabled = false; await automatic.runAutomaticSchedulerTicket(ticket); state.enabled = true + await running.pauseSchedulerJobsAndWait(); await automatic.runAutomaticSchedulerTicket(ticket); running.resumeSchedulerJobs() + assert(running.tryBeginSchedulerJob("backups")); await automatic.runAutomaticSchedulerTicket(ticket); running.endSchedulerJob("backups") + let staleQueued + await parent(async () => { + staleQueued = automatic.runAutomaticSchedulerTicket(automatic.createAutomaticSchedulerTicket("backups", 0)) + await background.databaseBackgroundWork.drain(50) + scheduler.stopScheduler() + }) + await staleQueued; await automatic.runAutomaticSchedulerTicket(ticket) + assert.equal(state.work, before) + }) + await t.test("automatic work has its own request admission and participates in background/exclusive drain", async () => { + const ticket = automatic.createAutomaticSchedulerTicket("backups", 1), entered = deferred(), release = deferred() + state.hook = async () => { entered.resolve(); await release.promise } + const job = automatic.runAutomaticSchedulerTicket(ticket); await entered.promise + assert.equal(databaseGate.active.size, 1); assert.equal(background.databaseBackgroundWork.pending.size, 1) + let exclusive = false + const maintenance = databaseGate.exclusive(async () => { exclusive = true }) + await delay(5); assert.equal(exclusive, false); const coalesced = automatic.runAutomaticSchedulerTicket(ticket) + release.resolve(); await job; await coalesced; await maintenance; await background.databaseBackgroundWork.drain(); state.hook = undefined + assert.equal(databaseGate.active.size, 0); assert.equal(exclusive, true) + }) + await t.test("logs timer callbacks use fixed tickets, and alert debounce preserves captured generation ownership", async () => { + const callbacks = [] + const logs = load(await source("services/router-logs-collector.ts"), { "./scheduler-automatic-root.js": automatic, "./config-mutation-policy.js": policy, "../db/index.js": { pool: freshPool }, "./monitoring-server-catalog.js": {}, "./mikrotik.js": {}, "./router-logs-model.js": {}, "./scheduler-running.js": running, "./partition-write-transaction.js": {} }, + { setInterval(callback) { callbacks.push(callback); return { unref() {} } }, clearInterval() {} }) + await parent(async () => { logs.startLogsLiveTimer(1) }) + for (const callback of callbacks) callback() + for (const end = Date.now() + 1000; logsRuns < 2 && Date.now() < end;) await delay(1) + await background.databaseBackgroundWork.drain(); assert.equal(logsRuns, 2); logs.stopLogsLiveTimer() + const ticket = automatic.createAutomaticSchedulerTicket("alert_engine", 1) + alert.wireAlertEngineRunner(() => automatic.runAutomaticSchedulerTicket(ticket)) + await parent(async () => { alert.scheduleAlertEngineAfterDataCollectors() }) + await delay(300); assert.equal(state.alert, 1) + assert(running.tryBeginSchedulerJob("alert_engine")); alert.scheduleAlertEngineAfterDataCollectors(); await delay(270) + scheduler.stopScheduler(); running.endSchedulerJob("alert_engine"); await delay(130) + assert.equal(state.alert, 1, "Old already queued debounce cannot adopt a new generation") + }) + await t.test("retired verified-only child cannot change current generation or legitimate timers", async () => { + scheduler.stopScheduler(); state.enabled = false + const live = new Set(); let clears = 0, reads = 0 + const oldSettings = schedulerProviders["./backup-service.js"].getBackupScheduleSettings + schedulerProviders["./backup-service.js"].getBackupScheduleSettings = async () => { reads++; return oldSettings() } + scheduler = load(schedulerText, schedulerProviders, { + setInterval(callback) { const handle = { callback }; live.add(handle); return handle }, + clearInterval(handle) { clears++; live.delete(handle) }, + }) + await scheduler.refreshScheduler(); await background.databaseBackgroundWork.drain() + const before = { count: live.size, clears, reads, work: state.work, alert: state.alert } + assert(before.count > 0); assert(scheduler.isAutomaticSchedulerGenerationCurrent(1)) + const release = deferred(); let late + await policy.withVerifiedConfigurationChange({ actorId: "verified-only", connectionId: "owned", approved: false, serverIds: [], objects: [], serverWide: false, authorize: async () => {} }, async () => { + late = release.promise.then(() => assert.rejects(scheduler.refreshScheduler())) + }) + release.resolve(); await late + assert(scheduler.isAutomaticSchedulerGenerationCurrent(1)) + assert.equal(live.size, before.count); assert.equal(clears, before.clears) + assert.equal(reads, before.reads); assert.equal(state.work, before.work); assert.equal(state.alert, before.alert) + scheduler.stopScheduler(); state.enabled = true + }) + alert.clearAlertCollectorHooksTimer(); scheduler.stopScheduler() + console.log("Automatic scheduler handoff: actual ALS/lifetime/gate modules and scheduler/manual functions; fake pool/collectors only; 0 real SQL/native/notifications") +}) diff --git a/backend/src/services/scheduler-automatic-root.ts b/backend/src/services/scheduler-automatic-root.ts new file mode 100644 index 00000000..903b4fb9 --- /dev/null +++ b/backend/src/services/scheduler-automatic-root.ts @@ -0,0 +1,166 @@ +/** Process-bootstrap-owned execution path for fixed automatic scheduler jobs. + * No arbitrary callback, detached child or request JSON can clear a scope. */ +import { AsyncLocalStorage, AsyncResource } from "node:async_hooks" +import { withDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" +import { assertNoOperationConfigurationLockContext, captureOperationConfigurationLockHandoff } from "./operation-config-lock-lease.js" +import { assertNoConfigurationLockContext, captureConfigurationLockHandoff } from "./config-mutation-lock.js" +import { assertNoVerifiedConfigurationContext, assertConfigurationHandoffContext } from "./config-mutation-policy.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" +import { databaseBackgroundWork } from "./database-background-work.js" +import { wireAlertEngineRunner } from "./alert-collector-hooks.js" +import type { SchedulerJobKey } from "./scheduler.js" +const JOBS = ["router_logs", "traffic", "uptime_resources", "uptime_ping", "uptime_speed", "servers_rest_ping", "gre_bgp", "internet_path", "certificates_renew", "backups", "geoip_update", "alert_engine", "router_logs_live", "router_logs_retention"] as const +type Job = typeof JOBS[number] +declare const ticketBrand: unique symbol +export type AutomaticSchedulerTicket = Readonly<{ [ticketBrand]: true }> +const tickets = new WeakMap | undefined }>>() +const pending = new WeakMap>() +declare const authorityBrand: unique symbol +export interface AutomaticSchedulerAuthority { readonly [authorityBrand]: true } +export interface AutomaticSchedulerAuthorityScope { deadlineAt: number; signal?: AbortSignal } +export interface AutomaticSchedulerAuthoritySnapshot { + readonly job: Job + readonly generation: number + /** Process-local execution identity only. Never durable uniqueness or a + * restart-generation proof; admission still requires a fresh durable attempt. */ + readonly executionId: string + readonly deadlineAt: number + /** Original process-local monotonic horizon; never reconstructed from a later + * wall-clock reading or treated as a durable cross-process timestamp. */ + readonly monotonicDeadline: number + /** Always includes the private execution-completion/failure cancellation. */ + readonly signal: AbortSignal +} +type Execution = { readonly job: Job; readonly generation: number; readonly executionId: string; readonly isCurrent: () => boolean; + readonly controller: AbortController; active: boolean; capability?: AutomaticSchedulerAuthority; scope?: Readonly; end?: number } +const executions = new AsyncLocalStorage() +const authorities = new WeakMap() +let executionSequence = 0 +export class AutomaticSchedulerAuthorityError extends Error { + readonly code = "AUTOMATIC_SCHEDULER_AUTHORITY_REFUSED" + constructor() { super("Current automatic scheduler authority unavailable") } +} +function refuseAuthority(): never { throw new AutomaticSchedulerAuthorityError() } +function authorityScope(raw: Readonly): Readonly { + if (!raw || typeof raw !== "object" || Array.isArray(raw) + || !Object.hasOwn(raw, "deadlineAt") + || "signal" in raw && !Object.hasOwn(raw, "signal") + || Reflect.ownKeys(raw).length !== Object.keys(raw).length + || Object.keys(raw).some(key => key !== "deadlineAt" && key !== "signal") + || Object.values(Object.getOwnPropertyDescriptors(raw)).some(value => !("value" in value) || !value.enumerable) + || !Number.isSafeInteger(raw.deadlineAt) || raw.signal !== undefined && !(raw.signal instanceof AbortSignal)) refuseAuthority() + return Object.freeze({ deadlineAt: raw.deadlineAt, signal: raw.signal }) +} +function currentExecution(): Execution { + const state = executions.getStore() + if (!state?.active || !state.isCurrent()) return refuseAuthority() + return state +} +function liveAuthority(state: Execution, requested?: Readonly, requestedEnd?: number): void { + if (currentExecution() !== state || state.controller.signal.aborted || !state.scope || state.end === undefined || state.scope.signal?.aborted + || Date.now() >= state.scope.deadlineAt || globalThis.performance.now() >= state.end + || requested && (requested.signal?.aborted || Date.now() >= requested.deadlineAt) + || requestedEnd !== undefined && globalThis.performance.now() >= requestedEnd) refuseAuthority() +} +/** Capture only inside the genuine fixed-ticket execution. The first finite + * deadline/signal is immutable for that execution; recapture cannot renew it. + * No job, generation, execution ID, ticket or callback can be supplied here. */ +export function captureAutomaticSchedulerAuthority(raw: Readonly): AutomaticSchedulerAuthority { + const state = currentExecution(), scope = authorityScope(raw) + if (state.capability) { + liveAuthority(state) + if (scope.deadlineAt !== state.scope!.deadlineAt || scope.signal !== state.scope!.signal) refuseAuthority() + return state.capability + } + const wall = Date.now(), mono = globalThis.performance.now() + if (scope.signal?.aborted || scope.deadlineAt <= wall) refuseAuthority() + state.scope = scope; state.end = mono + (scope.deadlineAt - wall) + const capability = Object.freeze(Object.create(null)) as AutomaticSchedulerAuthority + state.capability = capability; authorities.set(capability, state) + liveAuthority(state); return capability +} +/** Inspect from the SAME live ALS execution, checking generation/lifetime on + * both sides of the await. Optional requested scope only clips this inspection; + * it never replaces/extends the original capture. Snapshot is data, not authority. + * Consumers must re-inspect at their own await/dispatch boundaries. */ +export async function inspectAutomaticSchedulerAuthority(capability: unknown, + rawRequested?: Readonly): Promise> { + const state = capability && typeof capability === "object" ? authorities.get(capability) : undefined + if (!state) return refuseAuthority() + const requested = rawRequested === undefined ? undefined : authorityScope(rawRequested) + const end = requested ? Math.min(state.end!, globalThis.performance.now() + Math.max(0, requested.deadlineAt - Date.now())) : state.end + liveAuthority(state, requested, end) + await Promise.resolve() + liveAuthority(state, requested, end) + const signals = [...new Set([state.controller.signal, state.scope!.signal, requested?.signal].filter((value): value is AbortSignal => !!value))] + const signal = signals.length > 1 ? AbortSignal.any(signals) : signals[0] + return Object.freeze({ job: state.job, generation: state.generation, executionId: state.executionId, + deadlineAt: Math.min(state.scope!.deadlineAt, requested?.deadlineAt ?? state.scope!.deadlineAt), monotonicDeadline: state.end!, signal }) +} +let root: AsyncResource | undefined +function fail(): never { throw new Error("Automatic scheduler bootstrap/ticket unavailable") } +export function initializeAutomaticSchedulerRoot(): void { + // Validate on EVERY call, including after initialization and in stale children. + assertNoOperationConfigurationLockContext(); assertNoConfigurationLockContext() + assertNoVerifiedConfigurationContext(); sharedDatabaseGate.assertNoRequestContext() + if (!root) { + root = new AsyncResource("mmapp-automatic-scheduler-root") + // Bootstrap wires the fixed hook without starting any timer. A refresh + // replaces this initial generation ticket alongside its intervals. + const alert = createAutomaticSchedulerTicket("alert_engine", 0) + wireAlertEngineRunner(() => runAutomaticSchedulerTicket(alert)) + } +} +export function requireAutomaticSchedulerRoot(): void { if (!root) fail() } +export function createAutomaticSchedulerTicket(job: Job, generation: number): AutomaticSchedulerTicket { + requireAutomaticSchedulerRoot() + assertConfigurationHandoffContext() + if (!JOBS.includes(job) || !Number.isSafeInteger(generation) || generation < 0) fail() + const ticket = Object.freeze(Object.create(null)) as AutomaticSchedulerTicket + tickets.set(ticket, Object.freeze({ job, generation, handoff: Promise.all([captureOperationConfigurationLockHandoff(), captureConfigurationLockHandoff()]).then(() => {}) })); return ticket +} +export async function runAutomaticSchedulerTicket(ticket: AutomaticSchedulerTicket): Promise { + const entry = ticket && typeof ticket === "object" ? tickets.get(ticket) : undefined + if (!root || !entry) fail() + const current = pending.get(ticket) + if (current) return current + const owner = root + // This root was captured ONLY at trusted bootstrap. Each job obtains a new + // DB request admission and is separately tracked for maintenance draining. + const work = owner.runInAsyncScope(async () => { + // Immediate automatic jobs must not race the still-held source operation's + // advisory locks. The parent schedules this handoff without awaiting it. + // Waiting is NOT tracked/request work: that would deadlock a later drain + // inside this same parent operation against its own completion promise. + await entry.handoff + const scheduler = await import("./scheduler.js") + // Exclusive maintenance may outlive parent completion. This wait owns no + // request/background lease. Repeated ticks coalesce on this same ticket. + while (sharedDatabaseGate.isPaused()) { + await sharedDatabaseGate.whenOpen() + if (!scheduler.isAutomaticSchedulerGenerationCurrent(entry.generation)) return + } + if (!scheduler.isAutomaticSchedulerGenerationCurrent(entry.generation)) return + // Count locally first, then share the SQL lock with requests from other replicas. + return databaseBackgroundWork.track(sharedDatabaseGate.request(() => withDatabaseApiRequest(async () => { + if (!scheduler.isAutomaticSchedulerGenerationCurrent(entry.generation)) return + const execute = async (work: () => Promise) => { + if (!scheduler.isAutomaticSchedulerGenerationCurrent(entry.generation)) return + const execution: Execution = { job: entry.job, generation: entry.generation, + executionId: `automatic-process-execution-${++executionSequence}`, + isCurrent: () => scheduler.isAutomaticSchedulerGenerationCurrent(entry.generation), controller: new AbortController(), active: true } + return executions.run(execution, async () => { + try { await work() } finally { execution.active = false; execution.controller.abort() } + }) + } + if (entry.job === "router_logs_live" || entry.job === "router_logs_retention") { + const logs = await import("./router-logs-collector.js") + if (!scheduler.isAutomaticSchedulerGenerationCurrent(entry.generation)) return + if (entry.job === "router_logs_live") await execute(() => logs.runRouterLogsLiveOnce()) + else await execute(() => logs.runRouterLogsRetentionOnce()) + } else await execute(() => scheduler.executeAutomaticSchedulerJob(entry.job as SchedulerJobKey, entry.generation)) + }))) + }) + pending.set(ticket, work) + try { await work } finally { if (pending.get(ticket) === work) pending.delete(ticket) } +} diff --git a/backend/src/services/scheduler.ts b/backend/src/services/scheduler.ts index da1945f0..a3b43f4a 100644 --- a/backend/src/services/scheduler.ts +++ b/backend/src/services/scheduler.ts @@ -77,6 +77,8 @@ export const JOB_KEYS = [ ] as const export type SchedulerJobKey = (typeof JOB_KEYS)[number] +import { createAutomaticSchedulerTicket, runAutomaticSchedulerTicket, requireAutomaticSchedulerRoot } from "./scheduler-automatic-root.js" + const timers = new Map>() let schedulerGeneration = 0 @@ -233,30 +235,65 @@ function clearAllTimers() { timers.clear() } +export function isAutomaticSchedulerGenerationCurrent(generation: number): boolean { + return generation === schedulerGeneration && !isSchedulerJobsPaused() +} +export async function executeAutomaticSchedulerJob(jobKey: SchedulerJobKey, generation: number): Promise { + if (!isAutomaticSchedulerGenerationCurrent(generation)) return + let enabled: boolean + switch (jobKey) { + case "router_logs": enabled = (await getLogSettings()).enabled; break + case "traffic": enabled = (await getTrafficSettings()).enabled; break + case "servers_rest_ping": enabled = (await getServersApiPingSettings()).enabled; break + case "internet_path": enabled = (await getInternetPathSettings()).enabled; break + case "certificates_renew": enabled = (await getCertificateRenewSettings()).enabled; break + case "backups": enabled = (await getBackupScheduleSettings()).enabled; break + case "geoip_update": enabled = (await getGeoipSettings()).enabled; break + case "uptime_resources": { const current = await getUptimeSettings(); enabled = current.resourcesEnabled ?? current.enabled; break } + case "uptime_ping": { const current = await getUptimeSettings(); enabled = current.pingEnabled ?? current.enabled; break } + case "uptime_speed": { const current = await getUptimeSettings(); enabled = current.speedEnabled ?? current.enabled; break } + case "gre_bgp": case "alert_engine": enabled = true; break + default: return + } + if (!enabled || !isAutomaticSchedulerGenerationCurrent(generation)) return + await executeSchedulerJob(jobKey) +} + /** Пересоздать интервалы после смены настроек. */ export async function refreshScheduler(): Promise { if (isSchedulerJobsPaused()) return - const generation = ++schedulerGeneration + requireAutomaticSchedulerRoot() + const generation = schedulerGeneration + 1 + // Handoff preflight precedes all timer/generation side effects. + const alertTicket = createAutomaticSchedulerTicket("alert_engine", generation) + schedulerGeneration = generation const superseded = () => generation !== schedulerGeneration || isSchedulerJobsPaused() clearAllTimers() + const tickets = new Map>() + const automatic = (job: SchedulerJobKey) => { + let ticket = tickets.get(job) + if (!ticket) { ticket = createAutomaticSchedulerTicket(job, generation); tickets.set(job, ticket) } + return runAutomaticSchedulerTicket(ticket) + } + wireAlertEngineRunner(() => runAutomaticSchedulerTicket(alertTicket)) const logs = await getLogSettings() if (superseded()) return - startLogsLiveTimer() + startLogsLiveTimer(generation) if (logs.enabled) { - void executeSchedulerJob("router_logs") - timers.set("router_logs", setInterval(() => { void executeSchedulerJob("router_logs") }, logs.intervalSec * 1000)) + void automatic("router_logs").catch(() => {}) + timers.set("router_logs", setInterval(() => { void automatic("router_logs").catch(() => {}) }, logs.intervalSec * 1000)) } const traffic = await getTrafficSettings() if (superseded()) return if (traffic.enabled) { const ms = Math.max(5_000, traffic.intervalSec * 1000) - void executeSchedulerJob("traffic").catch(() => {}) + void automatic("traffic").catch(() => {}) timers.set( "traffic", setInterval(() => { - void executeSchedulerJob("traffic").catch(() => {}) + void automatic("traffic").catch(() => {}) }, ms), ) } @@ -267,11 +304,11 @@ export async function refreshScheduler(): Promise { if (superseded()) return if (apiPing.enabled) { const apiMs = Math.max(10_000, apiPing.intervalSec * 1000) - void executeSchedulerJob("servers_rest_ping").catch(() => {}) + void automatic("servers_rest_ping").catch(() => {}) timers.set( "servers_rest_ping", setInterval(() => { - void executeSchedulerJob("servers_rest_ping").catch(() => {}) + void automatic("servers_rest_ping").catch(() => {}) }, apiMs), ) } @@ -284,54 +321,54 @@ export async function refreshScheduler(): Promise { if (resOn) { const resMs = Math.max(5_000, uptime.intervalSec * 1000) - void executeSchedulerJob("uptime_resources").catch(() => {}) + void automatic("uptime_resources").catch(() => {}) timers.set( "uptime_resources", setInterval(() => { - void executeSchedulerJob("uptime_resources").catch(() => {}) + void automatic("uptime_resources").catch(() => {}) }, resMs), ) } if (pingOn) { const pingMs = Math.max(1_000, uptime.probeIntervalSec * 1000) - void executeSchedulerJob("uptime_ping").catch(() => {}) + void automatic("uptime_ping").catch(() => {}) timers.set( "uptime_ping", setInterval(() => { - void executeSchedulerJob("uptime_ping").catch(() => {}) + void automatic("uptime_ping").catch(() => {}) }, pingMs), ) } if (spdOn) { const spdMs = Math.max(10_000, uptime.speedIntervalSec * 1000) - void executeSchedulerJob("uptime_speed").catch(() => {}) + void automatic("uptime_speed").catch(() => {}) timers.set( "uptime_speed", setInterval(() => { - void executeSchedulerJob("uptime_speed").catch(() => {}) + void automatic("uptime_speed").catch(() => {}) }, spdMs), ) } if (internetPath.enabled) { const internetPathMs = Math.max(30_000, internetPath.intervalSec * 1000) - void executeSchedulerJob("internet_path").catch(() => {}) + void automatic("internet_path").catch(() => {}) timers.set( "internet_path", setInterval(() => { - void executeSchedulerJob("internet_path").catch(() => {}) + void automatic("internet_path").catch(() => {}) }, internetPathMs), ) } const greBgpMs = GRE_BGP_INTERVAL_MS - void executeSchedulerJob("gre_bgp").catch(() => {}) + void automatic("gre_bgp").catch(() => {}) timers.set( "gre_bgp", setInterval(() => { - void executeSchedulerJob("gre_bgp").catch(() => {}) + void automatic("gre_bgp").catch(() => {}) }, greBgpMs), ) @@ -339,11 +376,11 @@ export async function refreshScheduler(): Promise { if (superseded()) return if (certRenew.enabled) { const certRenewMs = Math.max(300_000, certRenew.intervalSec * 1000) - void executeSchedulerJob("certificates_renew").catch(() => {}) + void automatic("certificates_renew").catch(() => {}) timers.set( "certificates_renew", setInterval(() => { - void executeSchedulerJob("certificates_renew").catch(() => {}) + void automatic("certificates_renew").catch(() => {}) }, certRenewMs), ) } @@ -352,11 +389,11 @@ export async function refreshScheduler(): Promise { if (superseded()) return if (backupSchedule.enabled) { const backupMs = 60_000 - void executeSchedulerJob("backups").catch(() => {}) + void automatic("backups").catch(() => {}) timers.set( "backups", setInterval(() => { - void executeSchedulerJob("backups").catch(() => {}) + void automatic("backups").catch(() => {}) }, backupMs), ) } @@ -365,21 +402,21 @@ export async function refreshScheduler(): Promise { if (superseded()) return if (geoip.enabled) { const geoipMs = Math.max(6 * 3600_000, geoip.updateIntervalSec * 1000) - void executeSchedulerJob("geoip_update").catch(() => {}) + void automatic("geoip_update").catch(() => {}) timers.set( "geoip_update", setInterval(() => { - void executeSchedulerJob("geoip_update").catch(() => {}) + void automatic("geoip_update").catch(() => {}) }, geoipMs), ) } const alertMs = 20_000 - void executeSchedulerJob("alert_engine").catch(() => {}) + void automatic("alert_engine").catch(() => {}) timers.set( "alert_engine", setInterval(() => { - void executeSchedulerJob("alert_engine").catch(() => {}) + void automatic("alert_engine").catch(() => {}) }, alertMs), ) } @@ -479,4 +516,3 @@ export async function listSchedulerRuns(opts: { jobKey?: string; limit: number; } } -wireAlertEngineRunner(() => executeSchedulerJob("alert_engine")) diff --git a/backend/src/services/server-connection-test.test.ts b/backend/src/services/server-connection-test.test.ts new file mode 100644 index 00000000..f082d298 --- /dev/null +++ b/backend/src/services/server-connection-test.test.ts @@ -0,0 +1,69 @@ +import assert from "node:assert/strict" +import test from "node:test" +import http, { createServer } from "node:http" +import https from "node:https" +import { performServerConnectionTest } from "./server-connection-test.js" +import { isAllowedServerConnectionAddress, serverConnectionActionSchema, serverConnectionSecretPurpose } from "../modules/mcp/server-connection-actions.js" +import { isAllowedServerConnectionAddress as pureAddressPolicy, serverConnectionHostSchema } from "../modules/mcp/server-connection-destination-policy.js" + +test("connection schema refuses raw passwords and binds the full endpoint", () => { + assert.equal(isAllowedServerConnectionAddress, pureAddressPolicy) + assert.equal(serverConnectionActionSchema.shape.host, serverConnectionHostSchema) + const endpoint = { host:"router.example",port:443,useSsl:true,verifySsl:true,apiPath:"/rest",username:"admin" } + const purpose = serverConnectionSecretPurpose(endpoint) + for (const patch of [{host:"else.example"},{port:80},{useSsl:false},{verifySsl:false},{apiPath:"/api"},{username:"other"}]) assert.notEqual(serverConnectionSecretPurpose({...endpoint,...patch}),purpose) + const reference = "00000000-0000-4000-8000-000000000001" + for (const patch of [{password:"plaintext"},{host:"http://router.example"},{apiPath:"//else"},{apiPath:"/rest/../secret"},{username:"a:b"},{port:65536}]) assert.equal(serverConnectionActionSchema.safeParse({...endpoint,passwordRef:reference,...patch}).success,false) + for (const ip of ["0.0.0.0","169.254.169.254","224.0.0.1","255.255.255.255","100.100.100.200","::","fe80::1","ff02::1","::ffff:a9fe:a9fe","64:ff9b::a9fe:a9fe"]) assert.equal(isAllowedServerConnectionAddress(ip),false,ip) + for (const ip of ["127.0.0.1","10.0.0.1","192.168.88.1","::1","fd00::1"]) assert.equal(isAllowedServerConnectionAddress(ip),true,ip) +}) + +test("bounded shared test pins both GETs, retains Host/SNI, refuses redirect, sanitizes reflection and fences publication", async () => { + let mode = "ok", requests = 0, dns = 0, before = 0, after = 0 + const seen: string[] = [], optionsSeen: https.RequestOptions[] = [] + const secret = "NEVER_TEST_PASSWORD" + const server = createServer((request,response) => { + requests++; seen.push(request.headers.host ?? ""); assert.equal(request.method,"GET") + assert.equal(request.headers.authorization,"Basic "+Buffer.from(`admin:${secret}`).toString("base64")) + response.setHeader("content-type","application/json") + if (mode === "redirect") { response.statusCode=302; response.setHeader("location","http://169.254.169.254/"); response.end("{}"); return } + if (mode === "oversized") { response.end(" ".repeat(65537)); return } + if (mode === "malformed") { response.end(`{${secret}`); return } + if (mode === "lost") { request.socket.destroy(); return } + if (request.url === "/rest/system/identity") response.end(JSON.stringify({name:"fixture-"+secret})) + else { assert.equal(request.url,"/rest/system/resource"); response.end(JSON.stringify([{version:"7.22", "board-name":"fixture",uptime:"1h","total-memory":"128MiB"},{version:"wrong","board-name":"small",uptime:"0","total-memory":"1MiB"}])) } + }) + await new Promise(resolve => server.listen(0,"127.0.0.1",resolve)) + const port = (server.address() as {port:number}).port + const original = https.request + // Capture real TLS options but route this isolated fixture through plain loopback + // HTTP; DNS lookup is still consumed by Node's actual request path twice. + https.request = ((options: https.RequestOptions, callback: (res: http.IncomingMessage)=>void) => { optionsSeen.push(options); return http.request({...options,agent:false},callback) }) as typeof https.request + const params = {host:"router.fixture.invalid",port,useSsl:true,verifySsl:true,apiPath:"/rest",username:"admin",password:secret,servername:"router.fixture.invalid",lookup: ((_host: string, opts: {all?:boolean}, callback:(...args:unknown[])=>void) => {dns++; if(opts.all)callback(null,[{address:"127.0.0.1",family:4}]);else callback(null,"127.0.0.1",4)}) as typeof import("node:dns").lookup} + const fences = {beforeDispatch:async()=>{before++},afterDispatch:async()=>{after++}} + try { + const result = await performServerConnectionTest(params,fences) + assert.equal(result.success,true); assert.equal(result.version,"7.22"); assert.equal(result.identity,"fixture-[redacted]") + assert.equal(requests,2); assert.equal(dns,2); assert.equal(before,2); assert.equal(after,1) + assert.deepEqual(seen,[`router.fixture.invalid:${port}`,`router.fixture.invalid:${port}`]) + for (const options of optionsSeen) { assert.equal(options.servername,"router.fixture.invalid"); assert.equal(options.rejectUnauthorized,true) } + for (const fault of ["redirect","oversized","malformed","lost"]) { + mode=fault; const count=requests + const result=await performServerConnectionTest(params,fences) + assert.equal(result.success,false); assert.equal(result.outcome,fault==="redirect"?"rejected":"unknown") + assert.ok(!JSON.stringify(result).includes(secret)); assert.equal(requests,count+2) + } + const count=requests + await assert.rejects(performServerConnectionTest(params,{beforeDispatch:async()=>{throw Error("revoked")}}),/revoked/) + assert.equal(requests,count) + mode="ok" + await assert.rejects(performServerConnectionTest(params,{afterDispatch:async()=>{throw Error("source drift")}}),/source drift/) + assert.equal(requests,count+2) + const controller=new AbortController();controller.abort() + await assert.rejects(performServerConnectionTest(params,{signal:controller.signal}),/Aborted/) + assert.equal(requests,count+2) + } finally { https.request=original; await new Promise(resolve=>server.close(()=>resolve())) } +}) + + + diff --git a/backend/src/services/server-connection-test.ts b/backend/src/services/server-connection-test.ts new file mode 100644 index 00000000..bec28485 --- /dev/null +++ b/backend/src/services/server-connection-test.ts @@ -0,0 +1,55 @@ +import { MikrotikClient, MikrotikError, routerOsAccessHint, type MikrotikConnectParams } from "./mikrotik.js" + +export interface ServerConnectionTestResult { + success: boolean; message: string; outcome: "accepted" | "rejected" | "unknown"; latencyMs?: number; identity?: string; version?: string; boardName?: string; uptime?: string +} +/** The inherited typed helpers retain RouterOS singleton/aggregate selection parity. + * Only these two bounded GETs are available; no redirects or automatic retries. */ +class ConnectionTestClient extends MikrotikClient { + constructor(params: MikrotikConnectParams, private readonly fence?: () => Promise, private readonly signal?: AbortSignal) { super(params) } + override async get(path: string): Promise { + if (!["/system/identity", "/system/resource"].includes(path)) throw Error("Unsupported connection test resource") + await this.fence?.() + if (this.signal?.aborted) throw new DOMException("Aborted", "AbortError") + return super.get(path, 10_000, this.signal, 65_536) + } +} +export async function performServerConnectionTest(params: MikrotikConnectParams, options: { beforeDispatch?: () => Promise; afterDispatch?: () => Promise; signal?: AbortSignal } = {}): Promise { + const safe = (value: unknown) => { + if (typeof value !== "string" || value.length > 512 || /[\u0000-\u001f\u007f]/.test(value)) throw Error("Malformed RouterOS test result") + const protectedValues = [params.password, Buffer.from(`${params.username}:${params.password}`).toString("base64")].filter(Boolean) + return protectedValues.reduce((text, secret) => text.split(secret).join("[redacted]"), value) + } + // A dispatch fence failure must escape unchanged: access revocation is not a + // negative connection result and cannot be published as successful evidence. + let dispatchFailure: unknown + const guarded = async () => { try { await options.beforeDispatch?.() } catch (error) { dispatchFailure = error; throw error } } + const guardedClient = new ConnectionTestClient(params, guarded, options.signal) + let outcome: ServerConnectionTestResult + try { + const started = performance.now() + const responses = await Promise.allSettled([guardedClient.getIdentity(), guardedClient.getResource()]) + const failure = responses.find(response => response.status === "rejected") + if (failure?.status === "rejected") throw failure.reason + const identity = (responses[0] as PromiseFulfilledResult>>).value + const resource = (responses[1] as PromiseFulfilledResult>>).value + const result = { success: true, latencyMs: Math.round(performance.now() - started), identity: safe(identity.name), version: safe(resource.version), boardName: safe(resource["board-name"]), uptime: safe(resource.uptime) } + outcome = { ...result, outcome: "accepted", message: `Подключено · RouterOS ${result.version} · ${result.boardName}` } + } catch (error) { + if (dispatchFailure) throw dispatchFailure + if (options.signal?.aborted) throw new DOMException("Aborted", "AbortError") + // Never return raw RouterOS bodies, JSON parser snippets, or transport errors. + let message = "Не удалось подключиться" + if (error instanceof MikrotikError) message = routerOsAccessHint(error.statusCode, "") ?? `Ошибка RouterOS API: ${error.statusCode}` + else if (error instanceof Error) { + const text = error.message.toLowerCase() + if (text.includes("timeout") || text.includes("abort")) message = "Нет ответа от RouterOS — проверьте адрес и сервис www/www-ssl" + else if (text.includes("econnrefused")) message = "Соединение отклонено — порт закрыт" + else if (text.includes("cert") || text.includes("ssl") || text.includes("tls")) message = "Ошибка SSL-сертификата — отключите «Проверять SSL» для self-signed" + else if (text.includes("enotfound") || text.includes("getaddrinfo")) message = "Хост не найден" + } + outcome = { success: false, outcome: error instanceof MikrotikError ? "rejected" : "unknown", message } + } + await options.afterDispatch?.() + return outcome +} diff --git a/backend/src/services/server-partition-maintenance-lease.sql-helper.ts b/backend/src/services/server-partition-maintenance-lease.sql-helper.ts new file mode 100644 index 00000000..29c6ddd8 --- /dev/null +++ b/backend/src/services/server-partition-maintenance-lease.sql-helper.ts @@ -0,0 +1,197 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import type { PoolClient, QueryConfig } from "pg" +import type { ServerPartitionLeaseOptions } from "./server-partition-maintenance-lease.js" + +/** Proposal acceptance only. Called explicitly by the owned disposable SQL + * profile after migrations; import is inert. Installs no production migration, + * deletes no rows and does not claim independent DDL/TRUNCATE writers fenced. */ +export async function acceptServerPartitionMaintenanceLeaseSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") + const database = new URL(process.env.DATABASE_URL ?? "") + assert.equal(database.hostname, "127.0.0.1"); assert.equal(database.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(database.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { PARTITION_SPECS, ensurePartitionFor } = await import("../db/partitions.js") + const { createServerPartitionMaintenanceLease, SERVER_PARTITION_WRITER_DDL, SERVER_PARTITION_WRITER_LOCK, SERVER_PARTITION_WRITER_INVENTORY_SQL, ServerPartitionLeaseError } = await import("./server-partition-maintenance-lease.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + assert.equal((await pool.query("SELECT to_regprocedure('public.server_partition_writer_guard()') AS name")).rows[0].name, null, "Proposal must not already be installed") + await pool.query(SERVER_PARTITION_WRITER_DDL) + const sampledAt = "2099-04-01T12:00:00.000Z" + const leaf = await ensurePartitionFor(pool, "traffic_samples", "week", new Date(sampledAt)) + assert(/^[a-z0-9_]+$/.test(leaf)) + const serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('partition-lease-owned-fixture','127.0.0.1',9,false) RETURNING id")).rows[0].id) + const options = (): ServerPartitionLeaseOptions => ({ deadlineAt: Date.now() + 20000, serverIds: [serverId] }) + const sql = (client: Pick, text: string, values: unknown[] = []) => client.query({ text, values, query_timeout: 6000 } as QueryConfig & { query_timeout: number }) + const pidOf = async (client: Pick) => Number((await sql(client, "SELECT pg_backend_pid() AS pid")).rows[0].pid) + const waitFor = async (name: string, inspect: () => Promise): Promise => { + const deadline = Date.now() + 10000 + while (Date.now() < deadline) { + const result = await inspect(); if (result !== undefined) return result + // Poll scheduling only; success always requires the SQL predicate below. + await new Promise(resolve => setTimeout(resolve, 10)) + } + assert.fail("Timed out observing " + name) + } + const waiting = (pid: number, mode: "ShareLock" | "ExclusiveLock") => waitFor("actual waiting advisory " + mode, async () => { + const rows = (await sql(pool, "SELECT mode,granted FROM pg_locks WHERE locktype='advisory' AND pid=$1 AND classid=0 AND objid=$2 AND objsubid=1 AND mode=$3 AND NOT granted", [pid, SERVER_PARTITION_WRITER_LOCK, mode])).rows + return rows.length === 1 ? rows[0] : undefined + }) + const gone = (pid: number) => waitFor("released backend lock", async () => (await sql(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE locktype='advisory' AND pid=$1 AND classid=0 AND objid=$2", [pid, SERVER_PARTITION_WRITER_LOCK])).rows[0].n === 0 ? true : undefined) + const insert = "INSERT INTO traffic_samples(server_id,interface_name,sampled_at,rx_bytes) VALUES($1,$2,$3,1)" + const trace: Array<{ pid: number; text: string }> = [] + const handledClients = new WeakSet() + const sqlErrorCodes: string[] = [] + let ownerPid = 0, loseCommitAck = false, committedAcksLost = 0, cases = 0 + const lease = createServerPartitionMaintenanceLease({ + assertAuthority: async opts => { assert(opts.serverIds.length === 0 || opts.serverIds.length === 1 && opts.serverIds[0] === serverId); assert(opts.deadlineAt > Date.now()); assert(!opts.signal?.aborted) }, + connect: async () => { + const client = await pool.connect(), pid = await pidOf(client); ownerPid = pid + // An intentionally terminated owned backend must not emit an unhandled + // client error; the next command still rejects at the actual SQL boundary. + if (!handledClients.has(client)) { handledClients.add(client); client.on("error", () => {}) } + return { + query: async (text, values, opts) => { + trace.push({ pid, text }) + const result = await client.query({ text, values, query_timeout: Math.min(6000, opts.timeoutMs) } as QueryConfig & { query_timeout: number }).catch((error: unknown) => { + if (error && typeof error === "object" && "code" in error) sqlErrorCodes.push(String(error.code)) + throw error + }) + if (text === "COMMIT" && loseCommitAck) { loseCommitAck = false; committedAcksLost++; throw Error("Synthetic lost ACK after actual committed SQL") } + return result + }, + release: destroy => client.release(destroy), + } + }, + }) + try { + const inventory = (await sql(pool, SERVER_PARTITION_WRITER_INVENTORY_SQL, [PARTITION_SPECS.map(spec => spec.parent)])).rows + assert.equal(PARTITION_SPECS.length, 12) + assert.equal(new Set(inventory.map(row => row.parent)).size, 12) + assert(inventory.length > 12 && inventory.some(row => row.relname === leaf), "Future partition must inherit the trigger") + assert(inventory.every(row => row.trigger_enabled === "A" && row.trigger_type === 31 && row.unconditional && row.function_name === "server_partition_writer_guard")) + await lease.withLease(async () => {}, options()); cases++ + + for (const boundary of ["COMMIT", "ROLLBACK"] as const) { + const writer = await pool.connect() + let pending: Promise | undefined + try { + await sql(writer, "BEGIN"); await sql(writer, "SET LOCAL statement_timeout='15000ms'") + await sql(writer, insert, [serverId, randomUUID(), sampledAt]) + const writerPid = await pidOf(writer) + assert.equal((await sql(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE locktype='advisory' AND pid=$1 AND classid=0 AND objid=$2 AND mode='ShareLock' AND granted", [writerPid, SERVER_PARTITION_WRITER_LOCK])).rows[0].n, 1) + let entered = false; ownerPid = 0 + pending = lease.withLease(async () => { entered = true }, options()) + void pending.catch(() => {}) + const pid = await waitFor("maintenance backend", async () => ownerPid || undefined) + await waiting(pid, "ExclusiveLock"); assert.equal(entered, false) + await sql(writer, boundary); await pending; assert.equal(entered, true); await gone(pid); cases++ + } finally { await sql(writer, "ROLLBACK").catch(() => {}); writer.release(); await pending?.catch(() => {}) } + } + + const writer = await pool.connect() + let blockedInsert: Promise | undefined + try { + await sql(writer, "SET statement_timeout='15000ms'") + const otherPid = await pidOf(writer), name = randomUUID() + await lease.withLease(async () => { + const pid = ownerPid + assert.equal((await sql(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE locktype='advisory' AND pid=$1 AND classid=0 AND objid=$2 AND mode='ExclusiveLock' AND granted", [pid, SERVER_PARTITION_WRITER_LOCK])).rows[0].n, 1) + const start = trace.length + await lease.transaction(false, async () => {}, options(), async query => { + assert.equal(Number((await query("SELECT pg_backend_pid() AS pid")).rows[0].pid), pid) + await query(insert, [serverId, name, sampledAt]) // Same backend's shared row trigger is reentrant. + await lease.transaction(true, async () => {}, options(), async read => { assert.equal(Number((await read("SELECT pg_backend_pid() AS pid")).rows[0].pid), pid) }) + let nestedWrite = false + await assert.rejects(lease.transaction(false, async () => {}, options(), async () => { nestedWrite = true }), ServerPartitionLeaseError) + assert.equal(nestedWrite, false) + }) + assert.equal(new Set(trace.slice(start).map(row => row.pid)).size, 1) + assert(trace.slice(start).some(row => row.text.startsWith("SAVEPOINT "))) + assert(trace.slice(start).some(row => row.text.startsWith("RELEASE SAVEPOINT "))) + assert.equal((await sql(pool, "SELECT count(*)::int AS n FROM traffic_samples WHERE server_id=$1 AND interface_name=$2", [serverId, name])).rows[0].n, 1) + blockedInsert = sql(writer, insert, [serverId, randomUUID(), sampledAt]); void blockedInsert.catch(() => {}) + await waiting(otherPid, "ShareLock") + }, options()) + await blockedInsert; cases++ + } finally { writer.release(); await blockedInsert?.catch(() => {}) } + + // Expected integration failure: a row trigger takes the shared advisory + // lock AFTER INSERT already owns relation locks. Never prove this with an + // actual DROP: NOWAIT safely exposes the reverse lock ordering as 55P03. + const lateWriter = await pool.connect() + let lateInsert: Promise | undefined + try { + await sql(lateWriter, "SET statement_timeout='15000ms'") + const pid = await pidOf(lateWriter), beforeCodes = sqlErrorCodes.length + await assert.rejects(lease.withLease(async () => { + lateInsert = sql(lateWriter, insert, [serverId, randomUUID(), sampledAt]); void lateInsert.catch(() => {}) + await waiting(pid, "ShareLock") + assert.equal((await sql(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE locktype='relation' AND pid=$1 AND relation='public.traffic_samples'::regclass AND mode='RowExclusiveLock' AND granted", [pid])).rows[0].n, 1) + await lease.transaction(false, async () => {}, options(), async query => { await query("LOCK TABLE public.traffic_samples IN ACCESS EXCLUSIVE MODE NOWAIT") }) + }, options()), (error: unknown) => error instanceof ServerPartitionLeaseError && error.outcomeUnknown) + assert(sqlErrorCodes.slice(beforeCodes).includes("55P03"), "Actual SQL must expose row-trigger DDL lock-order gap") + await lateInsert; cases++ + } finally { lateWriter.release(); await lateInsert?.catch(() => {}) } + + // Positive protocol proof requires application admission BEFORE table SQL. + // This instrumented writer has no traffic relation lock while waiting. + const earlyWriter = await pool.connect() + let earlyAdmission: Promise | undefined + try { + await sql(earlyWriter, "BEGIN"); await sql(earlyWriter, "SET LOCAL statement_timeout='15000ms'") + const pid = await pidOf(earlyWriter) + await lease.withLease(async () => { + earlyAdmission = sql(earlyWriter, "SELECT pg_advisory_xact_lock_shared($1::bigint)", [SERVER_PARTITION_WRITER_LOCK]); void earlyAdmission.catch(() => {}) + await waiting(pid, "ShareLock") + assert.equal((await sql(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE locktype='relation' AND pid=$1 AND relation='public.traffic_samples'::regclass", [pid])).rows[0].n, 0) + await lease.transaction(false, async () => {}, options(), async query => { await query("LOCK TABLE public.traffic_samples IN ACCESS EXCLUSIVE MODE NOWAIT") }) + }, options()) + await earlyAdmission + await sql(earlyWriter, insert, [serverId, randomUUID(), sampledAt]); await sql(earlyWriter, "COMMIT"); cases++ + } finally { await earlyAdmission?.catch(() => {}); await sql(earlyWriter, "ROLLBACK").catch(() => {}); earlyWriter.release() } + + let detached: Promise | undefined, releaseDetached!: () => void + const gate = new Promise(resolve => { releaseDetached = resolve }) + await lease.withLease(async () => { detached = gate.then(() => lease.transaction(true, async () => {}, options(), async () => {})) }, options()) + const beforeDetached = trace.length + releaseDetached(); await assert.rejects(detached!, ServerPartitionLeaseError); assert.equal(trace.length, beforeDetached); cases++ + + // Reversible proposal-only schema fault on our newly created leaf. No data + // or audit rows are removed, and the trigger is restored even on failure. + await sql(pool, `ALTER TABLE public."${leaf}" DISABLE TRIGGER server_partition_writer_guard`) + try { + let entered = false + await assert.rejects(lease.withLease(async () => { entered = true }, options()), ServerPartitionLeaseError) + assert.equal(entered, false); cases++ + } finally { await sql(pool, `ALTER TABLE public."${leaf}" ENABLE ALWAYS TRIGGER server_partition_writer_guard`) } + + const lostName = randomUUID(), beforeLost = trace.length + loseCommitAck = true + await assert.rejects(lease.withLease(() => lease.transaction(false, async () => {}, options(), async query => { await query(insert, [serverId, lostName, sampledAt]) }), options()), (error: unknown) => error instanceof ServerPartitionLeaseError && error.outcomeUnknown) + assert.equal(committedAcksLost, 1) + const lostTrace = trace.slice(beforeLost) + assert.equal(lostTrace.filter(row => row.text === "COMMIT").length, 1) + assert.equal(lostTrace.filter(row => row.text.startsWith("INSERT INTO traffic_samples")).length, 1) + assert(!lostTrace.some(row => row.text === "ROLLBACK" || row.text.includes("partition-lease:unlock"))) + assert.equal((await sql(pool, "SELECT count(*)::int AS n FROM traffic_samples WHERE server_id=$1 AND interface_name=$2", [serverId, lostName])).rows[0].n, 1) + await gone(ownerPid); cases++ + + const disconnectedName = randomUUID(), beforeDisconnect = trace.length + await assert.rejects(lease.withLease(() => lease.transaction(false, async () => {}, options(), async query => { + await query(insert, [serverId, disconnectedName, sampledAt]) + assert.equal((await sql(pool, "SELECT pg_terminate_backend($1) AS terminated", [ownerPid])).rows[0].terminated, true) + await query("SELECT 1") + }), options()), (error: unknown) => error instanceof ServerPartitionLeaseError && error.outcomeUnknown) + assert.equal(trace.slice(beforeDisconnect).filter(row => row.text.startsWith("INSERT INTO traffic_samples")).length, 1) + assert(!trace.slice(beforeDisconnect).some(row => row.text === "COMMIT" || row.text === "ROLLBACK")) + await gone(ownerPid) + assert.equal((await sql(pool, "SELECT count(*)::int AS n FROM traffic_samples WHERE server_id=$1 AND interface_name=$2", [serverId, disconnectedName])).rows[0].n, 0) + cases++ + return { cases, roots: 12, descendants: inventory.length - 12, actualPostgres: true, observedAdvisoryWaits: 5, rowTriggerOnlyDdlSafe: false, observedLateWriterDdlRefusal: "55P03", preSqlWriterAdmissionRequired: true, independentDdlAndTruncateFenced: false, migrationApplied: false, productionWiring: false } + } finally { + await sql(pool, "UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + } +} diff --git a/backend/src/services/server-partition-maintenance-lease.test.ts b/backend/src/services/server-partition-maintenance-lease.test.ts new file mode 100644 index 00000000..c95b2d4c --- /dev/null +++ b/backend/src/services/server-partition-maintenance-lease.test.ts @@ -0,0 +1,36 @@ +import assert from "node:assert/strict" +import { PARTITION_SPECS } from "../db/partitions.js" +import { createServerPartitionMaintenanceLease, SERVER_PARTITION_WRITER_DDL, SERVER_PARTITION_WRITER_LOCK, ServerPartitionLeaseError, type ServerPartitionLeaseOptions, type ServerPartitionQuery } from "./server-partition-maintenance-lease.js" +import type { ServerPollSqlConnection } from "./server-poll-production.js" + +const options=():ServerPartitionLeaseOptions=>({deadlineAt:11000,serverIds:[1,2]}) +const inventory=()=>PARTITION_SPECS.map((p,i)=>({parent:p.parent,oid:String(i+1),relname:p.parent,nspname:"public",trigger_type:31,trigger_enabled:"A",trigger_name:"server_partition_writer_guard",trigger_args:0,unconditional:true,constraint_oid:"0",function_schema:"public",function_name:"server_partition_writer_guard",function_language:"plpgsql",security_definer:false,function_source:"BEGIN PERFORM pg_advisory_xact_lock_shared(8723110::bigint); IF TG_OP='DELETE' THEN RETURN OLD; END IF; RETURN NEW; END;"})) +function fixture(){ + let authority=true,time=1000,fail:string|undefined,unlock=true,count=0,rows:Record[]=inventory() + const log:{connection:number;sql:string;values:unknown[];timeout:number}[]=[],releases:{connection:number;destroy:boolean}[]=[],authorizations:number[][]=[] + const factory=createServerPartitionMaintenanceLease({now:()=>time,assertAuthority:async opts=>{authorizations.push([...opts.serverIds]);if(!authority)throw Error("revoked")},connect:async()=>{const connection=++count;const client:ServerPollSqlConnection={release:destroy=>{releases.push({connection,destroy:!!destroy})},query:async(sql,values,opts)=>{log.push({connection,sql,values,timeout:opts.timeoutMs});if(fail&&sql.includes(fail)){fail=undefined;throw Error("SQL ACK lost")};if(sql.includes("partition-lease:inventory"))return {rows};if(sql.includes("partition-lease:unlock"))return {rows:[{unlocked:unlock}]};return {rows:[{value:1}]}}};return client}}) + return {factory,log,releases,authorizations,revoke:()=>{authority=false},clock:(v:number)=>{time=v},fail:(v:string)=>{fail=v},badUnlock:()=>{unlock=false},inventory:(v:Record[])=>{rows=v},count:()=>count} +} +let checks=0 +async function test(name:string,work:()=>Promise|void){await work();checks++;console.log("ok",name)} +await test("schema proposes always-enabled row trigger for every parent",()=>{assert.equal(PARTITION_SPECS.length,12);assert.equal((SERVER_PARTITION_WRITER_DDL.match(/CREATE TRIGGER/g)??[]).length,12);assert.equal((SERVER_PARTITION_WRITER_DDL.match(/ENABLE ALWAYS TRIGGER/g)??[]).length,12);assert.match(SERVER_PARTITION_WRITER_DDL,/pg_advisory_xact_lock_shared\(8723110::bigint\)/);assert.equal(SERVER_PARTITION_WRITER_LOCK,8723110)}) +await test("session lock, mutations and unlock use exactly one SQL backend",async()=>{const f=fixture();await f.factory.withLease(async()=>{await f.factory.transaction(false,async()=>{},options(),async query=>{await query("INSERT INTO server_snapshots VALUES (1)")});await f.factory.transaction(true,async()=>{},options(),async query=>{await query("SELECT 1")})},options());assert.equal(f.count(),1);assert.equal(new Set(f.log.map(c=>c.connection)).size,1);assert(f.log[0].sql.includes("partition-lease:acquire"));assert(f.log.at(-1)?.sql.includes("partition-lease:unlock"));assert.deepEqual(f.releases,[{connection:1,destroy:false}]);assert(f.authorizations.length>f.log.length)}) +await test("preparation uses owned read-only transaction without session lock",async()=>{const f=fixture();await f.factory.transaction(true,async()=>{},options(),async query=>{await query("SELECT 1")});assert.equal(f.log[0].sql,"BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY");assert(!f.log.some(r=>r.sql.includes("advisory_lock")));assert.deepEqual(f.releases,[{connection:1,destroy:false}])}) +await test("nested read uses a savepoint and no second pool backend",async()=>{const f=fixture();await f.factory.withLease(()=>f.factory.transaction(false,async()=>{},options(),async query=>{await query("SELECT 'before'");await f.factory.transaction(true,async()=>{},options(),async read=>{await read("SELECT 'nested'")});await query("INSERT INTO server_snapshots VALUES (1)")}),options());assert.equal(f.count(),1);assert.equal(f.log.filter(r=>r.sql==="BEGIN").length,1);assert(f.log.some(r=>r.sql==="SAVEPOINT poll_nested_1"));assert(f.log.some(r=>r.sql==="RELEASE SAVEPOINT poll_nested_1"));assert.equal(f.log.filter(r=>r.sql==="COMMIT").length,1)}) +await test("nested write is rejected before dispatch",async()=>{const f=fixture();await assert.rejects(f.factory.withLease(()=>f.factory.transaction(false,async()=>{},options(),async()=>f.factory.transaction(false,async()=>{},options(),async()=>{})),options()),ServerPartitionLeaseError);assert(!f.log.some(r=>r.sql.startsWith("SAVEPOINT")));assert.equal(f.releases.at(-1)?.destroy,true)}) +await test("outer transactions are FIFO and never overlap",async()=>{const f=fixture(),seen:string[]=[];let release!:()=>void,started!:()=>void;const began=new Promise(resolve=>{started=resolve}),gate=new Promise(resolve=>{release=resolve});await f.factory.withLease(async()=>{const first=f.factory.transaction(false,async()=>{},options(),async()=>{seen.push("first-enter");started();await gate;seen.push("first-exit")});await began;const second=f.factory.transaction(true,async()=>{},options(),async()=>{seen.push("second-enter")});assert.deepEqual(seen,["first-enter"]);release();await Promise.all([first,second])},options());assert.deepEqual(seen,["first-enter","first-exit","second-enter"])}) +await test("closed captured query cannot dispatch after commit",async()=>{const f=fixture();let captured:ServerPartitionQuery|undefined;await f.factory.withLease(async()=>{await f.factory.transaction(false,async()=>{},options(),async query=>{captured=query});const count=f.log.length;await assert.rejects(captured!("SELECT 1"),ServerPartitionLeaseError);assert.equal(f.log.length,count)},options())}) +await test("detached work inherits a closed lease and cannot reacquire",async()=>{const f=fixture();let invoke!:()=>void,detached!:Promise;const gate=new Promise(resolve=>{invoke=resolve});await f.factory.withLease(async()=>{detached=gate.then(()=>f.factory.transaction(true,async()=>{},options(),async()=>{}))},options());const count=f.log.length;invoke();await assert.rejects(detached,ServerPartitionLeaseError);assert.equal(f.log.length,count)}) +await test("nested lease rejected",async()=>{const f=fixture();await assert.rejects(f.factory.withLease(()=>f.factory.withLease(async()=>{},options()),options()),ServerPartitionLeaseError);assert.equal(f.count(),1)}) +await test("missing parent trigger refuses maintenance",async()=>{const f=fixture();f.inventory(inventory().slice(1));let called=false;await assert.rejects(f.factory.withLease(async()=>{called=true},options()),ServerPartitionLeaseError);assert.equal(called,false);assert.equal(f.releases[0].destroy,true)}) +await test("unprotected leaf refuses maintenance",async()=>{const f=fixture();f.inventory([...inventory(),{...inventory()[0],oid:"99",relname:"flow_buckets_d_20260101",trigger_enabled:"D"}]);await assert.rejects(f.factory.withLease(async()=>{},options()),ServerPartitionLeaseError)}) +await test("wrong lock trigger body refuses maintenance",async()=>{const f=fixture();const rows=inventory();rows[0].function_source="BEGIN RETURN NEW; END;";f.inventory(rows);await assert.rejects(f.factory.withLease(async()=>{},options()),ServerPartitionLeaseError)}) +await test("lost BEGIN ACK destroys holder with no rollback",async()=>{const f=fixture();f.fail("BEGIN");await assert.rejects(f.factory.withLease(()=>f.factory.transaction(false,async()=>{},options(),async()=>{}),options()),(e:unknown)=>e instanceof ServerPartitionLeaseError&&e.outcomeUnknown);assert(!f.log.some(r=>r.sql.includes("ROLLBACK")||r.sql.includes("partition-lease:unlock")));assert.equal(f.releases[0].destroy,true)}) +await test("lost COMMIT ACK destroys holder with no rollback or retry",async()=>{const f=fixture();f.fail("COMMIT");await assert.rejects(f.factory.withLease(()=>f.factory.transaction(false,async()=>{},options(),async query=>{await query("INSERT INTO server_snapshots VALUES (1)")}),options()),(e:unknown)=>e instanceof ServerPartitionLeaseError&&e.outcomeUnknown);assert.equal(f.log.filter(r=>r.sql==="COMMIT").length,1);assert(!f.log.some(r=>r.sql.includes("ROLLBACK")||r.sql.includes("partition-lease:unlock")));assert.equal(f.releases[0].destroy,true)}) +await test("unknown unlock destroys instead of pooling session owner",async()=>{const f=fixture();f.badUnlock();await assert.rejects(f.factory.withLease(async()=>{},options()),(e:unknown)=>e instanceof ServerPartitionLeaseError&&e.outcomeUnknown);assert.deepEqual(f.releases,[{connection:1,destroy:true}])}) +await test("authority revocation between SQL boundaries blocks next dispatch",async()=>{const f=fixture();await assert.rejects(f.factory.withLease(()=>f.factory.transaction(false,async()=>{},options(),async query=>{f.revoke();await query("INSERT INTO server_snapshots VALUES (1)")}),options()),ServerPartitionLeaseError);assert(!f.log.some(r=>r.sql.startsWith("INSERT")));assert.equal(f.releases[0].destroy,true)}) +await test("abort destroys SQL holder and rejects later work",async()=>{const f=fixture(),controller=new AbortController(),opts={...options(),signal:controller.signal};await assert.rejects(f.factory.withLease(()=>f.factory.transaction(false,async()=>{},opts,async query=>{controller.abort();await query("SELECT 1")}),opts),ServerPartitionLeaseError);assert.equal(f.releases[0].destroy,true)}) +await test("expired deadline prevents next SQL",async()=>{const f=fixture();await assert.rejects(f.factory.withLease(()=>f.factory.transaction(false,async()=>{},options(),async query=>{f.clock(11000);await query("SELECT 1")}),options()),ServerPartitionLeaseError);assert(!f.log.some(r=>r.sql==="SELECT 1"))}) +await test("deadline above180 seconds refused before connect",async()=>{const f=fixture();await assert.rejects(f.factory.withLease(async()=>{},{...options(),deadlineAt:182000}),ServerPartitionLeaseError);assert.equal(f.count(),0)}) +await test("work cannot override factory transaction boundary",async()=>{const f=fixture();await assert.rejects(f.factory.withLease(()=>f.factory.transaction(false,async()=>{},options(),async query=>{await query("COMMIT")}),options()),ServerPartitionLeaseError);assert(!f.log.some(r=>r.sql==="COMMIT"))}) +console.log(`server-partition-maintenance-lease: ${checks} inert SQL scenarios passed`) diff --git a/backend/src/services/server-partition-maintenance-lease.ts b/backend/src/services/server-partition-maintenance-lease.ts new file mode 100644 index 00000000..1736afb0 --- /dev/null +++ b/backend/src/services/server-partition-maintenance-lease.ts @@ -0,0 +1,209 @@ +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +import { AsyncLocalStorage } from "node:async_hooks" +import { PARTITION_SPECS } from "../db/partitions.js" +import type { ServerPollSqlConnection } from "./server-poll-production.js" +import type { ServerPollBoundaryCoordinator } from "./server-poll-boundary.js" +import type { ProbeStorageOptions, ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { createProbeStorageConnectionConnector } from "./server-probe-storage-connection.js" +import { runPollStorageTransaction } from "./server-poll-storage.js" + +export const SERVER_PARTITION_WRITER_LOCK=8723110 +export interface ServerPartitionLeaseOptions {signal?:AbortSignal;deadlineAt:number;serverIds:number[]} +export interface ServerPartitionTransactionOptions {signal?:AbortSignal;deadlineAt:number} +export type ServerPartitionQuery=(sql:string,values?:unknown[])=>Promise<{rows:Record[];rowCount?:number|null}> +export type ServerPartitionTransaction=(readOnly:boolean,fence:()=>Promise,options:ServerPartitionTransactionOptions,work:(query:ServerPartitionQuery)=>Promise)=>Promise +export interface ServerPartitionMaintenanceLeaseDependencies { + /** Same fixed session issuer as production reader/executor, not a flag. */ + boundary?:ServerPollBoundaryCoordinator + connect(options:ServerPartitionTransactionOptions):Promise + /** Fresh owner, full global/server scope, generation, protections and cancel. */ + assertAuthority(options:Readonly):Promise + now?:()=>number +} +export class ServerPartitionLeaseError extends Error {readonly automaticReplay=false;constructor(readonly outcomeUnknown=false){super(outcomeUnknown?"Partition SQL outcome unknown; replay forbidden":"Partition writer lease unavailable or closed")}} +const parents=PARTITION_SPECS.map(v=>v.parent) +const functionBody="BEGIN PERFORM pg_advisory_xact_lock_shared(8723110::bigint); IF TG_OP='DELETE' THEN RETURN OLD; END IF; RETURN NEW; END;" +const canonical=(value:string)=>value.replace(/\s+/g," ").trim() +/** Verify roots and every current descendant. ENABLE ALWAYS also guards replica + * mode writers; parent row triggers clone onto existing and future partitions. + * This protocol does NOT fence independent partition DDL or TRUNCATE: those + * helpers must explicitly acquire this same advisory key on their own backend. + * A row trigger runs AFTER DML acquired relation locks. For deadlock-free DDL, + * ordinary writers MUST first acquire this shared transaction advisory lock + * BEFORE their target DML statement; the trigger is a backstop, not sufficient + * writer integration. Otherwise a blocked writer can hold RowExclusive while + * the maintenance holder needs AccessExclusive on that same partition. */ +export const SERVER_PARTITION_WRITER_INVENTORY_SQL=`/* partition-lease:inventory */ WITH RECURSIVE roots AS ( + SELECT c.oid,c.relname FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relname=ANY($1::text[]) AND c.relkind='p' +), tree AS (SELECT oid,relname AS parent FROM roots UNION ALL SELECT i.inhrelid,t.parent FROM tree t JOIN pg_inherits i ON i.inhparent=t.oid) +SELECT tree.parent,c.oid::text AS oid,c.relname,n.nspname,t.tgtype::int AS trigger_type,t.tgenabled AS trigger_enabled, + t.tgname AS trigger_name,t.tgnargs::int AS trigger_args,t.tgqual IS NULL AS unconditional,t.tgconstraint::text AS constraint_oid, + pn.nspname AS function_schema,p.proname AS function_name,p.prosrc AS function_source,p.prosecdef AS security_definer,l.lanname AS function_language +FROM tree JOIN pg_class c ON c.oid=tree.oid JOIN pg_namespace n ON n.oid=c.relnamespace +LEFT JOIN pg_trigger t ON t.tgrelid=c.oid AND t.tgname='server_partition_writer_guard' +LEFT JOIN pg_proc p ON p.oid=t.tgfoid LEFT JOIN pg_namespace pn ON pn.oid=p.pronamespace LEFT JOIN pg_language l ON l.oid=p.prolang +ORDER BY tree.parent,c.oid LIMIT 2001` + +export const SERVER_PARTITION_WRITER_DDL=` +CREATE FUNCTION public.server_partition_writer_guard() RETURNS trigger LANGUAGE plpgsql AS $$ ${functionBody} $$; +${parents.map(parent=>`CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR UPDATE OR DELETE ON public."${parent}" FOR EACH ROW EXECUTE FUNCTION public.server_partition_writer_guard(); +ALTER TABLE public."${parent}" ENABLE ALWAYS TRIGGER server_partition_writer_guard;`).join("\n")} +` +interface Holder {connection:ServerPollSqlConnection;options:ServerPartitionLeaseOptions;active:boolean;accepting:boolean;released:boolean;tail:Promise;destroy:()=>void} +interface TransactionScope {holder:Holder;active:boolean;readOnly:boolean;savepoint:number;tail:Promise;parent?:TransactionScope} +export interface ServerPollJournalOwner { + transaction(options:ProbeStorageOptions,authorize:()=>Promise,work:(sql:ProbeStorageSql)=>Promise):Promise + assertClosedForQuarantine():void +} +const journalOwners=new WeakSet() +export const isServerPollJournalOwner=(owner:ServerPollJournalOwner)=>journalOwners.has(owner) +/** One backend owns the session exclusive lock AND every maintenance SQL + * transaction. PostgreSQL shared advisory acquisition in its row triggers is + * reentrant on that same backend. No GUC, pool patch or bypass token exists. */ +export function createServerPartitionMaintenanceLease(deps:ServerPartitionMaintenanceLeaseDependencies){ + const now=deps.now??Date.now,held=new AsyncLocalStorage(),transactions=new AsyncLocalStorage(),journal=new AsyncLocalStorage() + const fail=(unknown=false):never=>{throw new ServerPartitionLeaseError(unknown)} + const checkOptions=(options:ServerPartitionTransactionOptions)=>{if(!Number.isSafeInteger(options.deadlineAt)||options.deadlineAt<=now()||options.deadlineAt-now()>SERVER_POLL_WALL_BUDGET_MS||options.signal?.aborted)fail()} + const local=(holder:Holder,options:ServerPartitionTransactionOptions)=>{ + if(!holder.active)fail();checkOptions(options);checkOptions(holder.options) + } + const authority=async(holder:Holder,options:ServerPartitionTransactionOptions)=>{ + if(!holder.active)fail();checkOptions(options);checkOptions(holder.options) + try{await deps.assertAuthority({...holder.options,serverIds:[...holder.options.serverIds]})}catch{fail()} + if(!holder.active)fail();checkOptions(options);checkOptions(holder.options) + } + const raw=async(holder:Holder,text:string,values:unknown[],options:ServerPartitionTransactionOptions,fence:()=>Promise,scopeLive:()=>boolean=()=>true)=>{ + const complete=deps.boundary?.combine([fence],holder.options.serverIds,holder.options) + const boundary=async()=>{ + local(holder,options);if(!scopeLive())fail() + if(complete)await complete() + else{await authority(holder,options);await fence();await authority(holder,options)} + local(holder,options);if(!scopeLive())fail() + } + await boundary() + let result!:Awaited> + try{result=await holder.connection.query(text,values,{signal:options.signal,timeoutMs:Math.max(1,Math.min(5000,options.deadlineAt-now(),holder.options.deadlineAt-now()))})}catch{holder.destroy();fail(true)} + await boundary();return result + } + const enqueue=async(owner:{tail:Promise},work:()=>Promise):Promise=>{ + const previous=owner.tail;let unlock!:()=>void;owner.tail=new Promise(resolve=>{unlock=resolve});await previous + try{return await work()}finally{unlock()} + } + const acquire=async(options:ServerPartitionTransactionOptions)=>{ + checkOptions(options);let expired=false,timer:ReturnType|undefined + const pending=deps.connect(options) + try{return await Promise.race([pending,new Promise((_resolve,reject)=>{timer=setTimeout(()=>{expired=true;reject(new ServerPartitionLeaseError())},Math.max(1,Math.min(5000,options.deadlineAt-now())))})])} + finally{if(timer)clearTimeout(timer);if(expired)void pending.then(c=>c.release(true),()=>{})} + } + const holderFor=async(options:ServerPartitionLeaseOptions):Promise=>{ + checkOptions(options);await deps.assertAuthority({...options,serverIds:[...options.serverIds]});checkOptions(options) + const connection=await acquire(options) + const holder:Holder={connection,options,active:true,accepting:true,released:false,tail:Promise.resolve(),destroy(){holder.active=false;holder.accepting=false;if(!holder.released){holder.released=true;connection.release(true)}}} + return holder + } + const queryFor=(scope:TransactionScope,options:ServerPartitionTransactionOptions,fence:()=>Promise):ServerPartitionQuery=>async(text,values=[])=>{ + if(!scope.active||scope.parent&&!scope.parent.active)fail() + // Only this factory owns transaction boundaries. Prevent accidental escape + // from the current transaction/savepoint through a caller's work callback. + if(/^\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|START\s+TRANSACTION)\b/i.test(text))fail() + return raw(scope.holder,text,values,options,fence,()=>scope.active&&(!scope.parent||scope.parent.active)) + } + const transact=async(holder:Holder,readOnly:boolean,fence:()=>Promise,options:ServerPartitionTransactionOptions,work:(query:ServerPartitionQuery)=>Promise):Promise=>{ + await authority(holder,options);const scope:TransactionScope={holder,active:true,readOnly,savepoint:0,tail:Promise.resolve()} + try{ + await raw(holder,readOnly?"BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY":"BEGIN",[],options,fence) + // Both settings belong to this transaction. One statement preserves the + // same before/after authority boundary without another metadata round trip. + await raw(holder,"SELECT set_config('TimeZone','UTC',true),set_config('statement_timeout',$1,true)",[`${Math.max(1,Math.min(5000,options.deadlineAt-now()))}ms`],options,fence) + const result=await transactions.run(scope,()=>work(queryFor(scope,options,fence))) + await scope.tail;scope.active=false + // Any failure after sending COMMIT immediately destroys the owner; there + // is no ROLLBACK/retry after the uncertain boundary. + await raw(holder,"COMMIT",[],options,fence);return result + }catch(error){holder.destroy();throw error}finally{scope.active=false} + } + const transaction=async(readOnly:boolean,fence:()=>Promise,options:ServerPartitionTransactionOptions,work:(query:ServerPartitionQuery)=>Promise):Promise=>{ + checkOptions(options);const holder=held.getStore(),parent=transactions.getStore() + if(holder){ + if(journal.getStore()===holder)fail() + if(!holder.active||!holder.accepting&&!parent)fail() + if(parent){ + if(!parent.active||parent.holder!==holder||!readOnly)fail() + return enqueue(parent,async()=>{ + if(!parent.active)fail();await authority(holder,options) + const name="poll_nested_"+(++parent.savepoint),scope:TransactionScope={holder,active:true,readOnly:true,savepoint:0,tail:Promise.resolve(),parent} + try{await raw(holder,"SAVEPOINT "+name,[],options,fence);const value=await transactions.run(scope,()=>work(queryFor(scope,options,fence)));await scope.tail;scope.active=false;await raw(holder,"RELEASE SAVEPOINT "+name,[],options,fence);return value} + catch(error){holder.destroy();throw error}finally{scope.active=false} + }) + } + return enqueue(holder,()=>transact(holder,readOnly,fence,options,work)) + } + // Preparation reads before maintenance use an independently owned backend. + // A server scope is still mandatory; callers provide it through the factory + // authority closure. No session lock is claimed by this read transaction. + if(!readOnly)fail() + const standalone=await holderFor({...options,serverIds:[]}) + try{return await transact(standalone,true,fence,options,work)}finally{if(!standalone.released){standalone.active=false;standalone.released=true;standalone.connection.release(false)}} + } + const journalOwner:ServerPollJournalOwner=Object.freeze({ + async transaction(options:ProbeStorageOptions,authorize:()=>Promise,work:(sql:ProbeStorageSql)=>Promise):Promise{ + const holder=held.getStore() + if(!holder?.accepting||transactions.getStore()||journal.getStore())return fail() + const signals=[...new Set([options.signal,holder.options.signal].filter((signal):signal is AbortSignal=>!!signal))] + const limits={deadlineAt:Math.min(options.deadlineAt,holder.options.deadlineAt),signal:signals.length?AbortSignal.any(signals):undefined} + local(holder,limits) + let borrowed=false + const connect=createProbeStorageConnectionConnector(async()=>{ + if(borrowed)fail();borrowed=true + const previous=holder.tail + let finish!:()=>void + const done=new Promise(resolve=>{finish=resolve}) + holder.tail=previous.then(()=>done) + let lent=false,closed=false + try{ + await previous;local(holder,limits) + if(!holder.accepting)fail() + lent=true + return { + async query({text,values,name,query_timeout}){ + local(holder,limits);if(closed)fail() + const result=await holder.connection.query(text,values??[],{signal:limits.signal,timeoutMs:query_timeout,statementName:name}) + if(typeof result.command!=="string")return fail(true) + return {...result,command:result.command} + }, + release(unsafe=false){if(closed)return;closed=true;try{if(unsafe)holder.destroy()}finally{finish()}}, + on:(event,listener)=>holder.connection.on?.(event,listener), + removeListener:(event,listener)=>holder.connection.removeListener?.(event,listener), + } + }finally{if(!lent)finish()} + }) + // Only the existing durable runner owns BEGIN/COMMIT/ROLLBACK. The + // connector checks the actual PostgreSQL COMMIT ACK before releasing this turn. + return journal.run(holder,()=>runPollStorageTransaction({connect,now},limits,authorize,work)) + }, + assertClosedForQuarantine(){ + const holder=held.getStore() + if(!holder||holder.active||!holder.released||journal.getStore())fail() + }, + }) + journalOwners.add(journalOwner) + const withLease=async(work:()=>Promise,options:ServerPartitionLeaseOptions):Promise=>{ + if(held.getStore()||transactions.getStore())fail() + const frozen={...options,serverIds:[...options.serverIds]};checkOptions(frozen) + if(!frozen.serverIds.length||frozen.serverIds.length>1000||new Set(frozen.serverIds).size!==frozen.serverIds.length||frozen.serverIds.some(id=>!Number.isSafeInteger(id)||id<=0))fail() + const holder=await holderFor(frozen),abort=()=>holder.destroy(),timer=setTimeout(abort,Math.max(1,frozen.deadlineAt-now())) + frozen.signal?.addEventListener("abort",abort,{once:true}) + try{ + await raw(holder,"/* partition-lease:acquire */ SELECT pg_advisory_lock($1::bigint)",[SERVER_PARTITION_WRITER_LOCK],frozen,async()=>{}) + const inventory=await raw(holder,SERVER_PARTITION_WRITER_INVENTORY_SQL,[parents],frozen,async()=>{}) + const rows=inventory.rows + if(rows.length2000||new Set(rows.map(r=>r.parent)).size!==parents.length||parents.some(parent=>!rows.some(r=>r.parent===parent&&r.relname===parent))||rows.some(r=>!parents.includes(String(r.parent))||r.nspname!=="public"||r.trigger_type!==31||r.trigger_enabled!=="A"||r.trigger_name!=="server_partition_writer_guard"||r.trigger_args!==0||r.unconditional!==true||r.constraint_oid!=="0"||r.function_schema!=="public"||r.function_name!=="server_partition_writer_guard"||r.function_language!=="plpgsql"||r.security_definer!==false||typeof r.function_source!=="string"||canonical(r.function_source)!==canonical(functionBody)))fail() + const result=await held.run(holder,work) + holder.accepting=false;await holder.tail;await authority(holder,frozen) + const unlocked=await raw(holder,"/* partition-lease:unlock */ SELECT pg_advisory_unlock($1::bigint) AS unlocked",[SERVER_PARTITION_WRITER_LOCK],frozen,async()=>{}) + if(unlocked.rows.length!==1||unlocked.rows[0].unlocked!==true)fail(true) + holder.active=false;holder.released=true;holder.connection.release(false);return result + }catch(error){holder.destroy();throw error}finally{clearTimeout(timer);frozen.signal?.removeEventListener("abort",abort);if(!holder.released)holder.destroy()} + } + return {withLease,transaction,journalOwner} +} diff --git a/backend/src/services/server-poll-approval-context.test.ts b/backend/src/services/server-poll-approval-context.test.ts new file mode 100644 index 00000000..5e4bd6b3 --- /dev/null +++ b/backend/src/services/server-poll-approval-context.test.ts @@ -0,0 +1,88 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { projectServerPollApprovalContext } from "./server-poll-approval-context.js" +import { ServerPollProblem, type ServerPollContext } from "./server-poll-execution-model.js" + +const initial = (): ServerPollContext & { partitionParents: { oid: string }[] } => ({ + servers: [{ id: 1, name: "synthetic", enabled: true, routingEndpointEpoch: randomUUID(), host: "192.0.2.1" }], + settings: { traffic: { retentionDays: 14, pollConfigurationEpoch: randomUUID() }, + uptime: { retentionDays: 14, pollConfigurationEpoch: randomUUID() }, + internetPath: { retentionDays: 14, pollConfigurationEpoch: randomUUID() } }, + partitions: [{ namespace: "public", parent: "server_snapshots", name: "server_snapshots_w_20000103", + from: "2000-01-03T00:00:00.000Z", to: "2000-01-10T00:00:00.000Z", storage: {}, catalog: { oid: "555" } }], + snapshots: [], protections: [], partitionParents: [{ oid: "777" }], +}) +const project = (context: ServerPollContext) => projectServerPollApprovalContext(context, 1) + +test("collector runtime-only changes preserve approval comparison and leave full source untouched", () => { + const source = initial(), before = structuredClone(source), expected = project(source) + for (const row of Object.values(source.settings)) { + assert(row) + row.lastCollectedAt = "2026-10-30T01:00:00.000Z"; row.lastDurationMs = 17 + row.lastError = "synthetic collector state"; row.updatedAt = "2026-10-30T01:00:01.000Z" + } + const completeSource = structuredClone(source) + assert.deepEqual(project(source), expected) + assert.deepEqual(source, completeSource) + assert.notDeepEqual(source, before) + assert.notDeepEqual(expected, source) +}) + +test("every settings group retains epoch, retention, unknown configuration and createdAt", () => { + const source = initial(), expected = project(source) + for (const key of ["traffic", "uptime", "internetPath"] as const) { + for (const [field, value] of Object.entries({ pollConfigurationEpoch: randomUUID(), retentionDays: 15, + enabled: false, intervalSec: 42, createdAt: "2026-10-30T01:00:00.000Z", future: { nested: [1, true] } })) { + const changed = structuredClone(source) + changed.settings[key]![field] = value + assert.notDeepEqual(project(changed), expected, key + ":" + field) + } + } +}) + +test("configuration ABA remains changed even when all visible values were restored", () => { + const source = initial(), expected = project(source), changed = structuredClone(source) + changed.settings.uptime!.retentionDays = 15 + changed.settings.uptime!.pollConfigurationEpoch = randomUUID() + changed.settings.uptime!.retentionDays = 14 + changed.settings.uptime!.pollConfigurationEpoch = randomUUID() + assert.notDeepEqual(project(changed), expected) +}) + +test("servers, snapshots, protections and unknown catalog sources remain exact", () => { + const source = initial(), expected = project(source) + const modifications: ((value: ReturnType) => void)[] = [ + value => { value.servers[0].updatedAt = "2026-10-30T01:00:00.000Z" }, + value => { value.servers[0].host = "192.0.2.2" }, + value => { value.servers[0].routingEndpointEpoch = randomUUID() }, + value => { value.snapshots.push({ id: 1, serverId: 1, polledAt: "2026-10-30T01:00:00.000Z" }) }, + value => { value.protections.push({ kind: "server", id: "1" }) }, + value => { value.partitionParents[0].oid = "778" }, + value => { value.partitions[0].catalog = { oid: "556" } }, + value => { value.partitions[0].storage = { fillfactor: "70" } }, + value => { value.servers[0].lastCollectedAt = "2026-10-30T01:00:00.000Z" }, + ] + for (const modify of modifications) { + const changed = structuredClone(source); modify(changed) + assert.notDeepEqual(project(changed), expected) + } + const result = project(source) + result.context.servers[0].host = "192.0.2.3" + assert.equal(source.servers[0].host, "192.0.2.1") +}) + +test("missing/null/forged epochs and incomplete source refuse without fallback", () => { + for (const key of ["traffic", "uptime", "internetPath"] as const) { + for (const epoch of [undefined, null, "", "00000000-0000-0000-0000-000000000000", "caller-selected"]) { + const source = initial() + if (epoch === undefined) delete source.settings[key]!.pollConfigurationEpoch + else source.settings[key]!.pollConfigurationEpoch = epoch + assert.throws(() => project(source), error => error instanceof ServerPollProblem && error.code === "STATE_UNAVAILABLE") + } + const source = initial(); source.settings[key] = null + assert.throws(() => project(source), error => error instanceof ServerPollProblem && error.code === "STATE_UNAVAILABLE") + } + const source = initial(); source.servers = [] + assert.throws(() => project(source), error => error instanceof ServerPollProblem && error.code === "STATE_UNAVAILABLE") +}) diff --git a/backend/src/services/server-poll-approval-context.ts b/backend/src/services/server-poll-approval-context.ts new file mode 100644 index 00000000..7aa96846 --- /dev/null +++ b/backend/src/services/server-poll-approval-context.ts @@ -0,0 +1,23 @@ +import { pollProblem, validateServerPollContext, type ServerPollContext } from "./server-poll-execution-model.js" + +/** Candidate projection for waiting on a human. Not an authority, signature, + * catalog attestation or replacement for the exact in-flight context proof. + * The fixed production reader authenticates the DB epoch guard in the same + * protected snapshot before its private callback can use this projection. */ +export function projectServerPollApprovalContext(context: ServerPollContext, serverId: number) { + validateServerPollContext(context, serverId) + const projection = structuredClone(context) + for (const row of Object.values(projection.settings)) { + if (!row || typeof row.pollConfigurationEpoch !== "string" + || !/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(row.pollConfigurationEpoch)) { + pollProblem("STATE_UNAVAILABLE", "Отсутствует версия конфигурации настроек опроса") + } + delete row.lastCollectedAt + delete row.lastDurationMs + delete row.lastError + delete row.updatedAt + } + // Separate from the whole execution-context domain even when no runtime + // fields happened to be present. Preserve every unknown top-level field. + return { domain: "server-poll-approval-context-v1" as const, context: projection } +} diff --git a/backend/src/services/server-poll-archive-accounting.test.ts b/backend/src/services/server-poll-archive-accounting.test.ts new file mode 100644 index 00000000..a1960d24 --- /dev/null +++ b/backend/src/services/server-poll-archive-accounting.test.ts @@ -0,0 +1,234 @@ +/** Complete ledger component checks with real AES-GCM/HMAC archive metadata. + * Fixed SQL doubles are not PostgreSQL/retained-storage acceptance. */ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHmac, createHash, randomBytes, randomUUID } from "node:crypto" +import { test } from "node:test" +import { assertServerPollArchiveAccountingForTests } from "./server-poll-archive-accounting.js" +import type { ServerPollRetiredArchive } from "./server-poll-retired-archive.js" +import { createServerPollTerminalArchiveForTests, type ServerPollTerminalArchive, type ServerPollArchiveSourceMetadata } from "./server-poll-terminal-archive.js" +import { PollStorageRefused, SERVER_POLL_STORAGE_POLICY as policy } from "./server-poll-storage-model.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" + +const canonical = (value: unknown): string => value && typeof value === "object" ? Array.isArray(value) ? "[" + value.map(canonical).join(",") + "]" + : "{" + Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([name, item]) => JSON.stringify(name) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value) +const secret = Buffer.from("5c".repeat(32), "hex"), sha = (value: string) => createHash("sha256").update(value).digest("hex") +const proof = async (purpose: string, value: unknown) => createHmac("sha256", secret).update(purpose + canonical(value)).digest("hex") +const codec = createServerPollTerminalArchiveForTests({ + encrypt: async value => { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", secret, iv), ciphertext = Buffer.concat([cipher.update(value), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), ciphertext]) }, + decrypt: async value => { const cipher = createDecipheriv("aes-256-gcm", secret, value.subarray(0, 12)); cipher.setAuthTag(value.subarray(12, 28)); return Buffer.concat([cipher.update(value.subarray(28)), cipher.final()]) }, + proof, + verifySource: async source => source.admission.metadata as unknown as ServerPollArchiveSourceMetadata, +}) +const id = (n: number) => "00000000-0000-4000-8000-" + n.toString(16).padStart(12, "0") +interface Entry { row: Record; archive?: ServerPollTerminalArchive | ServerPollRetiredArchive } +const full = (n: number, owner: string): Entry => ({ row: { admission_receipt_id: id(n), identity_kind: "rest", admission_execution_id: randomUUID(), admission_connection_id: null, + admission_owner_id: owner, admission_binding_proof: "a".repeat(64), admission_context_proof: "b".repeat(64), storage_state: "full", compact_at: null, + admission_archive_bytes: null, admission_activity_at: null, receipt_id: null, has_children: true } }) +async function compact(n: number, owner: string): Promise { + const metadata: ServerPollArchiveSourceMetadata = { receiptId: id(n), executionId: randomUUID(), connectionId: randomUUID(), ownerId: owner, + bindingProof: "a".repeat(64), contextProof: "b".repeat(64), originalActivityAt: "2026-10-29T01:00:00.123456Z", originalExpiresAt: "2026-10-29T01:00:00.123000Z", retentionDays: 30, safetyMs: 60000, terminalDigest: "c".repeat(64) } + const archive = await codec.create(JSON.stringify({ admission: { metadata }, journal: { execution: {}, intents: [], acks: [], receipt: {}, quarantines: [] }, terminal: { operation: {}, plan: {}, step: {} } }), 1) + assert(archive) + const { ciphertext: _ciphertext, ...columns } = archive + return { archive, row: { ...columns, admission_receipt_id: archive.receipt_id, identity_kind: "mcp", admission_execution_id: archive.execution_id, + admission_connection_id: archive.connection_id, admission_owner_id: archive.owner_id, admission_binding_proof: archive.binding_proof, admission_context_proof: archive.context_proof, + storage_state: "compacted", compact_at: new Date("2026-10-29T02:00:00Z"), admission_archive_bytes: archive.ciphertext_bytes, + admission_activity_at: archive.original_activity_at, admission_expires_at: archive.original_expires_at, asserted_ciphertext_bytes: archive.ciphertext_bytes, has_children: false } } +} +function fixture(entries: Entry[], retiredFactory?: Parameters[2]) { + const totals = new Map() + for (const entry of entries) { + for (const scope of ["global", "owner:" + entry.row.admission_owner_id]) { + const total = totals.get(scope) ?? { heavy: 0, compact: 0, archiveBytes: 0, bytes: 0, rows: 0 } + if (entry.row.storage_state === "full") { total.heavy++; total.bytes += 8 * 1024 * 1024 + 32768; total.rows += 4019 } + else { total.compact++; total.archiveBytes += entry.archive!.ciphertext_bytes; total.bytes += entry.archive!.ciphertext_bytes + 32768; total.rows += 2 } + totals.set(scope, total) + } + } + if (!totals.has("global")) totals.set("global", { heavy: 0, compact: 0, archiveBytes: 0, bytes: 0, rows: 0 }) + const usage: Record[] = [...totals].map(([scope, total]) => ({ scope, admissions: total.heavy, compacted_admissions: total.compact, archive_bytes: String(total.archiveBytes), reserved_rows: total.rows, reserved_bytes: String(total.bytes) })) + const state = { orphan: false, duplicate: false }, queries: string[] = [], batches: number[] = [], usageBatches: number[] = [] + let factories = 0, charges = 0 + const sql: ProbeStorageSql = { query: async (text, args = []) => { + queries.push(text) + if (text.startsWith("SELECT a.receipt_id AS admission_receipt_id")) { + assert(text.endsWith("LIMIT 128")); assert(text.includes("octet_length(r.ciphertext) AS ciphertext_bytes")) + assert(text.includes("sha256(pg_catalog.convert_to(r.ciphertext,'UTF8'))")) + assert(!/(?:SELECT|,)\s*r\.ciphertext\s*(?:AS\s+ciphertext\b|,|FROM)/i.test(text), "No full ciphertext payload may cross the JS boundary") + assert(!text.includes("r.*")); assert(!text.includes("SELECT *")) + const cursor = args[0] as string | null + const rows = entries.filter(entry => cursor === null || String(entry.row.admission_receipt_id) > cursor).sort((a, b) => String(a.row.admission_receipt_id).localeCompare(String(b.row.admission_receipt_id))).slice(0, 128).map(entry => { + const row = structuredClone(entry.row) + if (entry.archive) { row.ciphertext_bytes = Buffer.byteLength(entry.archive.ciphertext); row.ciphertext_sha256 = sha(entry.archive.ciphertext) } + assert.equal(Object.hasOwn(row, "ciphertext"), false) + return row + }) + if (state.duplicate && rows.length >= 3) rows[2] = structuredClone(rows[0]) + batches.push(rows.length); return { rows } + } + if (text.startsWith("SELECT r.receipt_id FROM server_poll_archives")) return { rows: state.orphan ? [{ receipt_id: id(999999) }] : [] } + assert(text.startsWith("SELECT scope,admissions,compacted_admissions,archive_bytes,reserved_rows,reserved_bytes")) + assert(text.endsWith("LIMIT 128 FOR SHARE")) + const cursor = args[0] as string | null, rows = usage.filter(row => cursor === null || String(row.scope) > cursor).sort((a, b) => String(a.scope) < String(b.scope) ? -1 : 1).slice(0, 128) + usageBatches.push(rows.length); return { rows: structuredClone(rows) } + } } + return { entries, usage, state, queries, batches, usageBatches, counts: () => ({ factories, charges }), run: () => assertServerPollArchiveAccountingForTests(sql, async () => { + factories++; return async row => { charges++; return codec.verifyCharge(row) } + }, retiredFactory) } +} +const refused = (work: Promise) => assert.rejects(work, PollStorageRefused) + +test("mixed V1/V2 allocations authenticate actual stored ciphertext and never fall back across formats", async () => { + const { createServerPollRetiredArchiveForTests } = await import("./server-poll-retired-archive.js") + const unexpected = async () => { throw new Error("Metadata accounting must not open or write archives") } + const v2 = createServerPollRetiredArchiveForTests({ + proof, decrypt: unexpected, encrypt: unexpected, openV1: unexpected, verifyHistory: unexpected, + }) + const first = await compact(1, "owner"), second = await compact(2, "owner") + const { archive_proof: _v1Proof, ciphertext, ...previous } = second.archive! + const metadata = { ...previous, version: 2 as const } + const archiveProof = await proof("server-poll-retired-archive-record-v2", { ...metadata, identity_kind: "mcp", + storage_state: "compacted", admission_bytes: 32768, charged_rows: 2 }) + second.archive = { ...metadata, ciphertext, archive_proof: archiveProof } + Object.assign(second.row, { version: 2, archive_proof: archiveProof }) + let retiredFactories = 0, retiredCharges = 0 + const f = fixture([first, second], async () => { + retiredFactories++ + return async row => { retiredCharges++; return v2.verifyCharge(row) } + }) + await f.run() + assert.deepEqual(f.counts(), { factories: 1, charges: 1 }) + assert.deepEqual({ retiredFactories, retiredCharges }, { retiredFactories: 1, retiredCharges: 1 }) + second.archive.ciphertext = "A" + ciphertext.slice(1) + await refused(f.run()) + assert.deepEqual(f.counts(), { factories: 2, charges: 2 }) + assert.deepEqual({ retiredFactories, retiredCharges }, { retiredFactories: 2, retiredCharges: 2 }) + second.archive.ciphertext = ciphertext + second.row.version = 3 + await refused(f.run()) + assert.deepEqual(f.counts(), { factories: 3, charges: 3 }) + assert.deepEqual({ retiredFactories, retiredCharges }, { retiredFactories: 2, retiredCharges: 2 }) +}) + +test("empty/global0 and full128 ledgers use complete exact global/owner counters without archive crypto", async () => { + const empty = fixture([]); await empty.run(); assert.deepEqual(empty.counts(), { factories: 0, charges: 0 }) + const f = fixture(Array.from({ length: 128 }, (_, n) => full(n + 1, "owner-" + Math.floor(n / 8)))) + await f.run(); assert.deepEqual(f.batches, [128, 0]); assert.deepEqual(f.counts(), { factories: 0, charges: 0 }) + assert.equal(Number(f.usage.find(row => row.scope === "global")!.reserved_bytes), policy.globalBytes) +}) + +test("compact336 plus full reservations and multiowner usage paginate in128-row bounded metadata batches", async () => { + const entries = await Promise.all(Array.from({ length: 336 }, (_, n) => compact(n + 1, "compact-owner-" + n))) + const { createServerPollRetiredArchiveForTests } = await import("./server-poll-retired-archive.js") + const unexpected = async () => { throw new Error("Metadata accounting must not open or write archives") } + const v2 = createServerPollRetiredArchiveForTests({ proof, decrypt: unexpected, encrypt: unexpected, openV1: unexpected, verifyHistory: unexpected }) + for (const index of [127, 128, 335]) { + const entry = entries[index], { archive_proof: _v1Proof, ciphertext, ...previous } = entry.archive! + const metadata = { ...previous, version: 2 as const } + const archiveProof = await proof("server-poll-retired-archive-record-v2", { ...metadata, identity_kind: "mcp", + storage_state: "compacted", admission_bytes: 32768, charged_rows: 2 }) + entry.archive = { ...metadata, ciphertext, archive_proof: archiveProof } + Object.assign(entry.row, { version: 2, archive_proof: archiveProof }) + } + entries.push(...Array.from({ length: 8 }, (_, n) => full(1000 + n, "full-owner"))) + let retiredFactories = 0, retiredCharges = 0 + const f = fixture(entries, async () => { + retiredFactories++ + return async row => { retiredCharges++; return v2.verifyCharge(row) } + }) + await f.run() + assert.deepEqual(f.batches, [128, 128, 88]); assert.deepEqual(f.usageBatches, [128, 128, 82]) + assert.deepEqual(f.counts(), { factories: 1, charges: 333 }) + assert.deepEqual({ retiredFactories, retiredCharges }, { retiredFactories: 1, retiredCharges: 3 }) +}) + +test("one owner can retain 336 archives and a full poll, with final-page tampering refused", async () => { + const owner = "capacity-owner" + const entries = await Promise.all(Array.from({ length: 336 }, (_, index) => compact(index + 1, owner))) + const f = fixture([...entries, full(337, owner)]) + await f.run() + assert.deepEqual(f.batches, [128, 128, 81]) + assert.deepEqual(f.counts(), { factories: 1, charges: 336 }) + const usage = f.usage.find(row => row.scope === "owner:" + owner)! + assert.equal(usage.admissions, 1) + assert.equal(usage.compacted_admissions, 336) + const bytes = usage.reserved_bytes + usage.reserved_bytes = String(Number(bytes) - 1) + await refused(f.run()) + usage.reserved_bytes = bytes + const archive = entries[335].archive! + archive.ciphertext = archive.ciphertext.replace(/^./, letter => letter === "A" ? "B" : "A") + await refused(f.run()) +}) + +test("forged full/compacted flags, lowering counters, missing global/owner usage and orphan archive cannot create credit", async () => { + for (const fault of ["full-flag", "compacted-flag", "heavy-counter", "compact-counter", "bytes-counter", "rows-counter", "missing-global", "missing-owner", "orphan", "children"] as const) { + const f = fixture([await compact(1, "owner-a"), full(2, "owner-b")]) + if (fault === "full-flag") f.entries[0].row.storage_state = "full" + if (fault === "compacted-flag") f.entries[1].row.storage_state = "compacted" + if (fault === "heavy-counter") f.usage[0].admissions = 0 + if (fault === "compact-counter") f.usage[0].compacted_admissions = 0 + if (fault === "bytes-counter") f.usage[0].reserved_bytes = Number(f.usage[0].reserved_bytes) - 1 + if (fault === "rows-counter") f.usage[0].reserved_rows = Number(f.usage[0].reserved_rows) - 1 + if (fault === "missing-global") f.usage.splice(f.usage.findIndex(row => row.scope === "global"), 1) + if (fault === "missing-owner") f.usage.splice(f.usage.findIndex(row => row.scope === "owner:owner-a"), 1) + if (fault === "orphan") f.state.orphan = true + if (fault === "children") f.entries[0].row.has_children = true + await refused(f.run()) + } +}) + +test("invalid MAC and ACTUAL ciphertext SHA/length changes refuse despite unchanged stored assertions", async () => { + for (const fault of ["mac", "hash", "size", "asserted-size", "admission-size"] as const) { + const f = fixture([await compact(1, "owner")]), row = f.entries[0].row, archive = f.entries[0].archive! + if (fault === "mac") row.archive_proof = "d".repeat(64) + if (fault === "hash") archive.ciphertext = archive.ciphertext.replace(/^./, letter => letter === "A" ? "B" : "A") + if (fault === "size") archive.ciphertext += "AAAA" + if (fault === "asserted-size") row.asserted_ciphertext_bytes = Number(row.asserted_ciphertext_bytes) + 1 + if (fault === "admission-size") row.admission_archive_bytes = Number(row.admission_archive_bytes) + 1 + await refused(f.run()) + } +}) + +test("archive/admission identity, binding, context, timestamps and retention metadata must match the MAC", async () => { + for (const [field, value] of Object.entries({ identity_kind: "rest", admission_execution_id: randomUUID(), admission_connection_id: randomUUID(), admission_owner_id: "foreign", + admission_binding_proof: "e".repeat(64), admission_context_proof: "e".repeat(64), admission_activity_at: "2026-10-29T01:00:00.123457Z", admission_expires_at: "2026-10-29T01:00:00.123001Z", retention_days: 31, safety_ms: 60001 })) { + const f = fixture([await compact(1, "owner")]); f.entries[0].row[field] = value; await refused(f.run()) + } +}) + +test("full heavy-count global129 and owner9 refuse even if usage counters claim conservation", async () => { + await refused(fixture(Array.from({ length: 129 }, (_, n) => full(n + 1, "owner-" + Math.floor(n / 8)))).run()) + await refused(fixture(Array.from({ length: 9 }, (_, n) => full(n + 1, "one-owner"))).run()) +}) + +test("compacted rows still pay retained bytes and cannot share an already fully charged owner", async () => { + const f = fixture([...Array.from({ length: 8 }, (_, n) => full(n + 1, "owner")), await compact(100, "owner")]) + await refused(f.run()) +}) + +test("nonadjacent duplicate receipt IDs cannot count twice even when cardinality/owner counters agree", async () => { + const f = fixture([full(1, "owner"), full(2, "owner"), full(3, "owner")]) + f.state.duplicate = true; await refused(f.run()) +}) + +test("compacted timestamp must be an actual finite SQL timestamp", async () => { + for (const value of ["not-a-date", new Date(NaN), undefined, 0]) { + const f = fixture([await compact(1, "owner")]); f.entries[0].row.compact_at = value; await refused(f.run()) + } +}) + +test("retained archive bytes can exhaust global budget before heavy-count or row-count limits", async () => { + const compacts = await Promise.all(Array.from({ length: 336 }, (_, n) => compact(1000 + n, "archive-owner-" + n))) + const f = fixture([...Array.from({ length: 127 }, (_, n) => full(n + 1, "full-owner-" + Math.floor(n / 8))), ...compacts]) + const global = f.usage.find(row => row.scope === "global")! + assert.equal(global.admissions, 127); assert(Number(global.reserved_rows) < policy.globalRows); assert(Number(global.reserved_bytes) > policy.globalBytes) + await refused(f.run()) +}) + +test("invalid numeric counters cannot hide through coercion or extra scopes", async () => { + for (const value of ["01", "-1", -0, 1.5, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1]) { + const f = fixture([]); f.usage[0].reserved_bytes = value; await refused(f.run()) + } + const f = fixture([]); f.usage.push({ ...f.usage[0], scope: "foreign-scope" }); await refused(f.run()) +}) diff --git a/backend/src/services/server-poll-archive-accounting.ts b/backend/src/services/server-poll-archive-accounting.ts new file mode 100644 index 00000000..3c15f26a --- /dev/null +++ b/backend/src/services/server-poll-archive-accounting.ts @@ -0,0 +1,121 @@ +/** Verify the COMPLETE retained allocation ledger under the caller's existing + * global poll-storage transaction lock. No payload hydration or new pool. */ +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { SERVER_POLL_STORAGE_POLICY as policy, PollStorageRefused } from "./server-poll-storage-model.js" +import { createServerPollArchiveChargeVerifier, type verifyServerPollArchiveCharge } from "./server-poll-terminal-archive.js" +import type { createServerPollRetiredArchiveChargeVerifier } from "./server-poll-retired-archive.js" + +const BATCH = 128 +const MAX_ENTRIES = Math.floor(policy.globalBytes / policy.admissionBytes) +const fail: () => never = () => { throw new PollStorageRefused() } +const uuid = /^[a-f\d]{8}-[a-f\d]{4}-[1-8][a-f\d]{3}-[89ab][a-f\d]{3}-[a-f\d]{12}$/i +function integer(value: unknown): number { + if (typeof value === "string" && /^(?:0|[1-9]\d{0,15})$/.test(value)) value = Number(value) + if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || Object.is(value, -0)) return fail() + return value +} +interface Total { heavy: number; compact: number; archiveBytes: number; rows: number; bytes: number } +const zero = (): Total => ({ heavy: 0, compact: 0, archiveBytes: 0, rows: 0, bytes: 0 }) +const createRetiredChargeVerifier: typeof createServerPollRetiredArchiveChargeVerifier = async () => + (await import("./server-poll-retired-archive.js")).createServerPollRetiredArchiveChargeVerifier() +const QUERY = `SELECT a.receipt_id AS admission_receipt_id,a.identity_kind,a.execution_id AS admission_execution_id, + a.connection_id AS admission_connection_id,a.owner_id AS admission_owner_id,a.binding_proof AS admission_binding_proof, + a.context_proof AS admission_context_proof,a.storage_state,a.compact_at,a.archive_bytes AS admission_archive_bytes, + to_char(a.original_activity_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS admission_activity_at, + to_char(a.authority_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS admission_expires_at, + a.retention_days,a.safety_ms,r.receipt_id,r.execution_id,r.connection_id,r.owner_id,r.version,r.binding_proof,r.context_proof, + r.source_digest,r.source_sha256,r.terminal_digest,r.decoded_bytes,r.compressed_bytes,r.archive_proof, + to_char(r.original_activity_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_activity_at, + to_char(r.original_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_expires_at, + r.ciphertext_bytes AS asserted_ciphertext_bytes,octet_length(r.ciphertext) AS ciphertext_bytes, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8')),'hex') AS ciphertext_sha256, + (EXISTS(SELECT 1 FROM server_poll_executions e WHERE e.receipt_id=a.receipt_id) + OR EXISTS(SELECT 1 FROM server_poll_intents i WHERE i.receipt_id=a.receipt_id) + OR EXISTS(SELECT 1 FROM server_poll_acks k WHERE k.receipt_id=a.receipt_id) + OR EXISTS(SELECT 1 FROM server_poll_receipts t WHERE t.receipt_id=a.receipt_id) + OR EXISTS(SELECT 1 FROM server_poll_quarantines q WHERE q.receipt_id=a.receipt_id)) AS has_children + FROM server_poll_storage_admissions a LEFT JOIN server_poll_archives r ON r.receipt_id=a.receipt_id + WHERE ($1::uuid IS NULL OR a.receipt_id>$1::uuid) ORDER BY a.receipt_id LIMIT 128` + +async function verify(sql: ProbeStorageSql, factory: () => Promise, + retiredFactory: typeof createServerPollRetiredArchiveChargeVerifier) { + const global = zero(), owners = new Map() + const seenReceipts = new Set() + let cursor: string | null = null, count = 0, charge: typeof verifyServerPollArchiveCharge | undefined + let retiredCharge: Awaited> | undefined + // All queries are fixed. Only metadata, hashes and actual lengths cross the + // driver boundary; even a full global history never becomes a JS blob. + for (;;) { + const rows: Record[] = (await sql.query(QUERY, [cursor])).rows + if (!Array.isArray(rows) || rows.length > BATCH) fail() + for (const raw of rows) { + const row: Record = structuredClone(raw) + if (++count > MAX_ENTRIES || typeof row.admission_receipt_id !== "string" || !uuid.test(row.admission_receipt_id) || + seenReceipts.has(row.admission_receipt_id) || typeof row.admission_owner_id !== "string" || !row.admission_owner_id.length || + row.admission_owner_id.length > 200 || row.admission_owner_id.trim() !== row.admission_owner_id || /[\x00-\x1f\x7f]/.test(row.admission_owner_id)) fail() + cursor = row.admission_receipt_id + seenReceipts.add(cursor) + let addition: Total + if (row.storage_state === "full") { + if (row.receipt_id !== null || row.compact_at !== null || row.admission_archive_bytes !== null || row.admission_activity_at !== null) fail() + addition = { heavy: 1, compact: 0, archiveBytes: 0, rows: policy.attemptRows, bytes: policy.attemptBytes + policy.admissionBytes } + } else if (row.storage_state === "compacted") { + if (row.identity_kind !== "mcp" || row.has_children !== false || !(typeof row.compact_at === "string" || row.compact_at instanceof Date) || + !Number.isFinite(new Date(row.compact_at).getTime()) || row.receipt_id !== row.admission_receipt_id || + row.execution_id !== row.admission_execution_id || row.connection_id !== row.admission_connection_id || row.owner_id !== row.admission_owner_id || + row.binding_proof !== row.admission_binding_proof || row.context_proof !== row.admission_context_proof || + row.original_activity_at !== row.admission_activity_at || row.original_expires_at !== row.admission_expires_at || + integer(row.ciphertext_bytes) !== integer(row.asserted_ciphertext_bytes) || integer(row.ciphertext_bytes) !== integer(row.admission_archive_bytes)) fail() + let verified + if (row.version === 1) { + charge ??= await factory() + verified = await charge(row) + } else if (row.version === 2) { + retiredCharge ??= await retiredFactory() + verified = await retiredCharge(row) + } else return fail() + addition = { heavy: 0, compact: 1, archiveBytes: verified.metadata.ciphertext_bytes, rows: verified.rows, bytes: verified.bytes } + } else return fail() + const owned = owners.get(row.admission_owner_id) ?? zero() + owners.set(row.admission_owner_id, owned) + for (const total of [global, owned]) for (const key of Object.keys(total) as (keyof Total)[]) total[key] += addition[key] + if (global.heavy > policy.globalAttempts || global.bytes > policy.globalBytes || global.rows > policy.globalRows || + owned.heavy > policy.ownerAttempts || owned.bytes > policy.ownerBytes || owned.rows > policy.ownerRows) fail() + } + if (rows.length < BATCH) break + } + // Orphan archives cannot disappear from the allocation scan and manufacture + // credit. Admission identity is retained until the original retention GC. + if ((await sql.query("SELECT r.receipt_id FROM server_poll_archives r LEFT JOIN server_poll_storage_admissions a USING(receipt_id) WHERE a.receipt_id IS NULL LIMIT 1")).rows.length) fail() + let scopeCursor: string | null = null, seenGlobal = false, usageCount = 0 + const seenOwners = new Set() + for (;;) { + const rows: Record[] = (await sql.query("SELECT scope,admissions,compacted_admissions,archive_bytes,reserved_rows,reserved_bytes FROM server_poll_storage_usage WHERE ($1::text IS NULL OR scope>$1) ORDER BY scope LIMIT 128 FOR SHARE", [scopeCursor])).rows + if (!Array.isArray(rows) || rows.length > BATCH) fail() + for (const row of rows) { + if (++usageCount > MAX_ENTRIES + 1 || typeof row.scope !== "string" || row.scope === scopeCursor) fail() + scopeCursor = row.scope + let expected: Total + if (row.scope === "global") { if (seenGlobal) fail(); seenGlobal = true; expected = global } + else { + if (!row.scope.startsWith("owner:") || row.scope.length < 7 || row.scope.length > 206) fail() + const id = row.scope.slice(6) + if (seenOwners.has(id)) fail() + seenOwners.add(id); expected = owners.get(id) ?? zero() + } + if (integer(row.admissions) !== expected.heavy || integer(row.compacted_admissions) !== expected.compact || integer(row.archive_bytes) !== expected.archiveBytes || + integer(row.reserved_rows) !== expected.rows || integer(row.reserved_bytes) !== expected.bytes) fail() + } + if (rows.length < BATCH) break + } + if (!seenGlobal || [...owners.keys()].some(id => !seenOwners.has(id))) fail() +} +/** Caller must already own server-poll-storage-v1. Any malformed/missing MAC or + * counter refuses admission before an INSERT, credential read or native I/O. */ +export async function assertServerPollArchiveAccounting(sql: ProbeStorageSql): Promise { + try { await verify(sql, createServerPollArchiveChargeVerifier, createRetiredChargeVerifier) } catch { return fail() } +} +export async function assertServerPollArchiveAccountingForTests(sql: ProbeStorageSql, factory: () => Promise, + retiredFactory = createRetiredChargeVerifier): Promise { + try { await verify(sql, factory, retiredFactory) } catch { return fail() } +} diff --git a/backend/src/services/server-poll-atomic-owned-helper.ts b/backend/src/services/server-poll-atomic-owned-helper.ts new file mode 100644 index 00000000..77ffd945 --- /dev/null +++ b/backend/src/services/server-poll-atomic-owned-helper.ts @@ -0,0 +1,98 @@ +/** Actual private v4 storage/history acceptance on the original owned runner. + * This does not activate a public issuer or claim whole-installation sizing. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import type { AuthUser } from "../lib/permissions.js" + +export async function acceptAtomicServerPollOwned() { + assert.equal(process.env.MMAPP_OWN_SERVER_POLL_ATOMIC_MAINTENANCE, "true") + assert.equal(process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH, "true") + assert.equal(process.env.NODE_ENV, "test") + const endpoint = new URL(process.env.DATABASE_URL ?? "") + assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.username, "mcp_test"); assert.equal(endpoint.pathname, "/postgres") + const { pool } = await import("../db/index.js") + const { encryptSecret, fingerprint, privateSecretFingerprint } = await import("../modules/mcp/security.js") + const { serverPollMaintenancePublications } = await import("./server-poll-maintenance-groups.js") + const { createRestServerPollAuthority } = await import("./server-poll-authority.js") + const { createAtomicStorageDurableServerPollSession } = await import("./server-poll-durable-session.js") + const { verifyServerPollHistory } = await import("./server-poll-history-verification.js") + let nativeGets = 0 + const router = createServer((request, response) => { + nativeGets++; assert.equal(request.method, "GET") + const values: Record = { + "/rest/system/identity": { name: "owned-atomic-router" }, + "/rest/system/resource": { "cpu-load": "2", "free-memory": "1024", "total-memory": "2048", version: "7.owned", "board-name": "CHR", uptime: "1h" }, + "/rest/interface": [{ type: "ether", name: "ether1" }], + "/rest/ip/address": [{ interface: "ether1", address: "192.0.2.1/24" }], + } + const value = values[request.url ?? ""] + response.writeHead(value ? 200 : 404, { "content-type": "application/json" }); response.end(JSON.stringify(value ?? { error: "not found" })) + }) + let serverId: number | undefined + try { + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const address = router.address(); assert(address && typeof address === "object") + const around = new Date(), from = new Date(around); from.setUTCHours(0, 0, 0, 0); from.setUTCDate(from.getUTCDate() - ((from.getUTCDay() + 6) % 7)) + const to = new Date(from.getTime() + 7 * 86400000), name = "server_snapshots_w_" + from.toISOString().slice(0, 10).replaceAll("-", "") + assert.match(name, /^server_snapshots_w_\d{8}$/) + await pool.query(`CREATE TABLE public."${name}" PARTITION OF public.server_snapshots FOR VALUES FROM ('${from.toISOString()}') TO ('${to.toISOString()}')`) + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,username,password,use_ssl,verify_ssl,enabled) VALUES('owned-atomic-router','127.0.0.1',$1,'owned',$2,false,false,true) RETURNING id", [address.port, await encryptSecret("owned-synthetic-only")])).rows[0].id) + const issuedAt = Date.now(), requestId = randomUUID(), executionId = randomUUID(), request = { serverId, receiptId: randomUUID() } + const actor: AuthUser = { id: "owned-atomic-" + randomUUID(), email: "owned@example.invalid", name: "Owned atomic", apps: ["mm"], permissions: ["mm:servers:write"], issuedAt: issuedAt - 1000, expiresAt: issuedAt + 3600000 } + const identity = { kind: "rest" as const, ownerId: actor.id, executionId, requestId, authentication: "jwt" as const, issuedAt, expiresAt: issuedAt + 180000 } + const authorize = createRestServerPollAuthority(actor, { authRequired: true, acceptedAt: issuedAt, requestId, deadlineAt: identity.expiresAt }) + const input = { request, identity, authorize }, session = createAtomicStorageDurableServerPollSession(input) + const started = performance.now(), plan = await session.prepare() + const count=plan.maintenance.length,groups=Math.ceil(count/8),publications=groups+2,rows=2*(publications+4)+2 + assert.equal(plan.version, 4); assert.equal(plan.maintenanceGroupSize, 8); assert(count>100) + assert.equal(plan.mutationCost, count+8) + await delay(20000) + await pool.query("UPDATE uptime_settings SET last_error='owned collector completed during human wait',updated_at=clock_timestamp() WHERE id=1") + const receipt = await session.execute(plan) + assert(receipt.complete && receipt.maintenanceComplete && receipt.configurationVerified && receipt.observation === "online") + assert.equal(nativeGets, 4); assert.equal(receipt.confirmedPublications, publications); assert.equal(receipt.completedPublications, publications) + const { rows: [admission] } = await pool.query("SELECT * FROM server_poll_storage_admissions WHERE receipt_id=$1", [request.receiptId]) + assert.equal(admission.max_stages, groups+8) + const {rows:[usage]}=await pool.query("SELECT reserved_rows,reserved_bytes FROM server_poll_storage_usage WHERE scope=$1",["owner:"+actor.id]) + assert.equal(usage.reserved_rows,4019);assert.equal(Number(usage.reserved_bytes),8421376) + assert.equal(admission.used_rows, rows) + assert.equal(new Date(admission.authority_expires_at).getTime(),identity.expiresAt) + const table = async (name: string) => (await pool.query(`SELECT * FROM ${name} WHERE receipt_id=$1`, [request.receiptId])).rows + const history = { execution: (await table("server_poll_executions"))[0], intents: await table("server_poll_intents"), acks: await table("server_poll_acks"), receipt: (await table("server_poll_receipts"))[0], quarantines: await table("server_poll_quarantines") } + const verified = await verifyServerPollHistory(history) + assert.notEqual(history.intents.find(row=>row.stage==="native.identity")!.before_proof,plan.beforeProof) + assert.equal(verified.context.plan.version, 4); assert.deepEqual(verified.context.plan, plan) + assert.equal(verified.source.publications, publications); assert.deepEqual(verified.receipt, receipt) + const fresh = createAtomicStorageDurableServerPollSession(input) + assert.deepEqual(await fresh.readExisting(plan), receipt); assert.equal(nativeGets, 4) + const bad = structuredClone(history); bad.intents.at(-1)!.effect_proof = "0".repeat(64) + await assert.rejects(verifyServerPollHistory(bad)) + const lastGroup=serverPollMaintenancePublications(plan).at(-1)! + assert(lastGroup.kind==="partition.atomic") + const originalIntent=history.intents.find(row=>row.stage==="publish."+(publications-1))! + assert.equal(originalIntent.effect_proof,await privateSecretFingerprint(fingerprint(lastGroup),"server-poll-production-v1")) + assert.equal(originalIntent.intent_proof,await privateSecretFingerprint(fingerprint({receiptId:request.receiptId,stage:originalIntent.stage,beforeProof:originalIntent.before_proof,effectProof:originalIntent.effect_proof}),"server-poll-journal-v1:intent")) + // Trusted synthetic test-host signing only. An authentic stage MAC must + // still not turn a truncated, shifted or individual effect into the sealed + // full manifest group. These clones never replace persisted rows. + for(const changed of [{...lastGroup,offset:lastGroup.offset+8},{...lastGroup,effects:lastGroup.effects.slice(0,-1)},lastGroup.effects[0]]){ + const substituted=structuredClone(history),intent=substituted.intents.find(row=>row.stage==="publish."+(publications-1))! + intent.effect_proof=await privateSecretFingerprint(fingerprint(changed),"server-poll-production-v1") + intent.intent_proof=await privateSecretFingerprint(fingerprint({receiptId:request.receiptId,stage:intent.stage,beforeProof:intent.before_proof,effectProof:intent.effect_proof}),"server-poll-journal-v1:intent") + await assert.rejects(verifyServerPollHistory(substituted)) + } + assert.equal(nativeGets,4) + console.log("Owned atomic durable poll:", JSON.stringify({ maintenance: count, groups, logicalMutationCost: count+8, maxStages: groups+8, + reservedRows: 4019, reservedBytes: 8421376, journalRows: rows, publications, nativeGets, + actualPrivateDurableFactory: true, actualRestAuthority: true, actualSignedJwtHook: false, + original20sWait: true, exactSourceAfterStart: true, actualCryptoHistory: true, newInstanceReadExisting: true, tamperedGroupRefused: true, authenticWrongGroupMacRefusals:3, + original180sBudget: true, elapsedMs: performance.now() - started, automaticReplay: false, + actualPublicMcpRegistry: false, actualMcpMutationDebit: false, archiveNaturalExpiry: false, performanceAccepted: false, wholeInstallation2GbAccepted: false })) + } finally { + await new Promise(resolve => router.close(() => resolve())) + // The original runner removes the complete disposable cluster. Do not + // delete journal/admission rows or forgive unresolved storage here. + } +} diff --git a/backend/src/services/server-poll-atomic-production.ts b/backend/src/services/server-poll-atomic-production.ts new file mode 100644 index 00000000..10670886 --- /dev/null +++ b/backend/src/services/server-poll-atomic-production.ts @@ -0,0 +1,73 @@ +/** Explicit private v4 production assembly; no public registration or admission issuance. */ +import { AsyncLocalStorage } from "node:async_hooks" +import { createServerPollProductionDependencies, type ServerPollProductionInput } from "./server-poll-production.js" +import { pollProblem } from "./server-poll-execution-model.js" +import { createServerPollExecution, type ServerPollDependencies } from "./server-poll-execution.js" +import { compileAtomicMaintenanceGroup, type AtomicMaintenanceAck } from "./server-poll-maintenance-groups.js" + +interface Scope { active: boolean; ids: number[]; proof: string; deadlineAt: number; signal?: AbortSignal } +/** Existing private production factories own all reads/native/individual + * effects and locking. Group SQL can use only the supplied ORIGINAL maintenance + * owner's transaction; there is no fallback checkout or metadata policy loan. */ +export function createAtomicServerPollProductionDependencies(input: ServerPollProductionInput): ServerPollDependencies { + const transaction = input.io?.transaction + if (typeof transaction !== "function") return pollProblem("STATE_UNAVAILABLE", "Atomic maintenance requires the original transaction owner") + const original = createServerPollProductionDependencies({ ...input, approvalProtocol: "configuration-epoch-v3" }) + const held = new AsyncLocalStorage(), request = structuredClone(input.request), now = input.now ?? Date.now + const fail = (): never => pollProblem("PERMISSION_DENIED", "Atomic candidate owner is absent, expired or changed") + const local = (scope: Scope) => { scope.signal?.throwIfAborted(); if (!scope.active || now() >= scope.deadlineAt) fail() } + const fence = async (scope: Scope) => { + local(scope) + const grant = await original.authorize(request, [...scope.ids], scope.signal) + local(scope) + if (!grant.global || grant.authorityProof !== scope.proof || scope.ids.some(id => !grant.serverIds.includes(id))) fail() + } + const deps: ServerPollDependencies = { + ...original, + withGlobalLock: (work, options) => original.withGlobalLock(async () => { + if (held.getStore()) fail() + const grant = await original.authorize(request, [request.serverId], options.signal) + const scope: Scope = { active: true, ids: [...grant.serverIds], proof: grant.authorityProof, ...options } + try { return await held.run(scope, work) } finally { scope.active = false } + }, options), + maintenanceGroup: async (group, options) => { + const scope = held.getStore() + if (!scope || options.receiptId !== request.receiptId || options.deadlineAt !== scope.deadlineAt || !Number.isSafeInteger(options.stage) || options.stage < 0 || options.stage > 2007) return fail() + local(scope) + // Compilation validates the complete fixed manifest and exact group slice. + const statement = compileAtomicMaintenanceGroup(options.manifest, group) + let sent = false + const access = () => fence(scope) + try { + const ack = await transaction(false, access, { deadlineAt: scope.deadlineAt, signal: scope.signal }, async query => { + await options.beforeDispatch(); await access() + if (sent) fail(); sent = true + await query(statement) + // The core's full read uses a nested savepoint on this transaction's + // owner. Use that same private snapshot to obtain the partition rows + // whose observed defaults the core checks before allowing COMMIT. + const result = await options.verifyBeforeCommit(after => { + const effects: AtomicMaintenanceAck["effects"] = group.effects.map(effect => { + if (effect.kind !== "partition.ensure") return {} + const rows = after.partitions.filter(row => row.name === effect.name && row.parent === effect.parent) + if (rows.length !== 1) return pollProblem("VERIFICATION_FAILED", "Atomic partition readback is absent or ambiguous") + return { row: rows[0] } + }) + return { effects } + }) + await access() + return result + }) + await access() + return ack + } catch (error) { + if (sent) return pollProblem("MUTATION_OUTCOME_UNKNOWN", "Atomic group outcome requires reconciliation; automatic replay forbidden") + throw error + } + }, + } + return deps +} +export function createAtomicServerPollProductionExecution(input: ServerPollProductionInput) { + return createServerPollExecution(createAtomicServerPollProductionDependencies(input)) +} diff --git a/backend/src/services/server-poll-authority.test.ts b/backend/src/services/server-poll-authority.test.ts new file mode 100644 index 00000000..d6993014 --- /dev/null +++ b/backend/src/services/server-poll-authority.test.ts @@ -0,0 +1,202 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { mcpConnectionPolicySchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import type { ConnectionRow, McpPrincipal } from "../modules/mcp/store.js" +import { McpError } from "../modules/mcp/security.js" +import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import { createMcpServerPollAuthorityForTests, createRestServerPollAuthorityForTests } from "./server-poll-authority.js" + +const actor = (): AuthUser => ({ id: "operator", email: "", name: "", apps: ["mm"], permissions: ["mm:servers:write"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000 }) +const context = () => ({ acceptedAt: Date.now(), requestId: randomUUID(), authRequired: true, deadlineAt: Date.now() + 2000 }) +const request = () => ({ serverId: 1, receiptId: randomUUID() }) +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const safe = (error: unknown) => error instanceof McpError && !error.message.includes("PRIVATE_PROVIDER") +test("REST original request expiry during metadata refuses before any later source query", async () => { + const f = fixture(); let active = true + const authorize = createRestServerPollAuthorityForTests(actor(), { ...context(), assertRequestActive: () => { if (!active) throw new McpError("READ_CANCELLED", "expired", 409) } }, f.dependencies) + f.onProtections(() => { active = false }) + await assert.rejects(authorize(request(), [1]), { code: "READ_CANCELLED" }) + assert.equal(f.portal(), 1) + assert.equal(f.leases[0].commands.at(-1), "SELECT object_ref FROM mcp_protections ORDER BY id LIMIT 1001") + assert.deepEqual(f.leases[0].releases, [true]) +}) +test("REST original request expiry during checkout destroys the lease without SQL", async () => { + const f = fixture(); let active = true + const dependencies = { ...f.dependencies, connect: async (...args: Parameters) => { + const lease = await f.dependencies.connect(...args); active = false; return lease + } } + const authorize = createRestServerPollAuthorityForTests(actor(), { ...context(), assertRequestActive: () => { if (!active) throw new McpError("READ_CANCELLED", "expired", 409) } }, dependencies) + await assert.rejects(authorize(request(), [1]), { code: "READ_CANCELLED" }) + assert.deepEqual(f.leases[0].commands, []); assert.deepEqual(f.leases[0].releases, [true]) +}) +function fixture() { + const principal: McpPrincipal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 60000).toISOString() } + let connection = { id: principal.connectionId, name: "owned", owner_id: "operator", owner_permissions: ["mm:servers:write"], generation: "a".repeat(64), + policy: mcpConnectionPolicySchema.parse({ id: principal.connectionId, status: "active", version: 1, mode: "approval", permissions: ["mm:servers:write"], expiresAt: principal.credentialExpiresAt, + scope: { serverIds: [1, 2], operationIds: ["servers.poll"], allowGlobal: true } }), owner_valid_until: new Date(principal.credentialExpiresAt), owner_claims_issued_at: new Date(Date.now() - 1000) } as ConnectionRow + let servers: Record[] = [{ id: 1 }, { id: 2 }], protections: Record[] = [], grants: Record[] = [], generation = connection.generation + const leases: { commands: string[]; releases: boolean[]; timeouts: number[] }[] = [] + let reads = 0, portal = 0, changed: (() => void) | undefined + const dependencies: Parameters[2] = { + connect: async () => { + const value = { commands: [] as string[], releases: [] as boolean[], timeouts: [] as number[] }; leases.push(value) + const sql: ProbeStorageConnection = { query: async (text, _values, options) => { + value.commands.push(text); value.timeouts.push(options!.timeoutMs) + if (text.startsWith("SELECT id FROM servers")) return { rows: structuredClone(servers) } + if (text.startsWith("SELECT object_ref")) { changed?.(); return { rows: structuredClone(protections) } } + if (text.startsWith("SELECT payload")) return { rows: structuredClone(grants) } + return { rows: [] } + }, release: unsafe => { value.releases.push(unsafe ?? false) } } + return sql + }, + connectionSource: async (_principal: McpPrincipal) => { reads++; return { connection: structuredClone(connection), grantRows: structuredClone(grants) } }, + portal: async () => { portal++ }, generation: async () => generation, + } + return { principal, dependencies, leases, reads: () => reads, portal: () => portal, + setConnection: (work: (row: ConnectionRow) => void) => work(connection), changeConnection: () => { connection = { ...connection, owner_id: "changed" } }, + setServers: (rows: Record[]) => { servers = rows }, setProtections: (rows: Record[]) => { protections = rows }, + setGrants: (rows: Record[]) => { grants = rows }, setGeneration: (value: string) => { generation = value }, onProtections: (callback: () => void) => { changed = callback } } +} + +test("MCP current grant is checked before IDs, full fleet required, proof stable and immutable", async () => { + const f = fixture(), authorize = createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies) + const first = await authorize(request(), [1]), second = await authorize(request(), [1, 2]) + assert.equal(first.global, true); assert.deepEqual(first.serverIds, [1, 2]); assert.equal(first.authorityProof, second.authorityProof) + assert(Object.isFrozen(first)); assert(Object.isFrozen(first.serverIds)); assert.equal(f.reads(), 4) + assert(f.leases.every(lease => lease.commands[0] === "BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY" && lease.commands.at(-1) === "ROLLBACK" && lease.releases.join() === "false")) + assert(f.leases.every(lease => lease.timeouts.every(timeout => timeout > 0 && timeout <= 10000))) + assert(!f.leases.flatMap(lease => lease.commands).some(sql => /SELECT \*|password|username|INSERT|UPDATE|DELETE/.test(sql))) + f.setConnection(row => { row.policy.scope.serverIds = [1] }) + await assert.rejects(authorize(request(), [1]), safe) +}) + +test("MCP permission/global/mode/generation/operation denial precedes metadata and credentials", async () => { + const mutations: ((row: ConnectionRow) => void)[] = [row => { row.owner_permissions = [] }, row => { row.policy.permissions = [] }, row => { row.policy.mode = "observer" }, + row => { row.policy.scope.allowGlobal = false }, row => { row.policy.scope.operationIds = [] }, row => { row.generation = "b".repeat(64) }, row => { row.owner_valid_until = new Date(0) }] + for (const mutate of mutations) { + const f = fixture(); f.setConnection(mutate) + await assert.rejects(createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies)(request(), [1]), safe) + assert(!f.leases[0].commands.some(sql => sql.startsWith("SELECT id FROM servers") || sql.startsWith("SELECT object_ref"))) + } +}) + +test("MCP original principal frozen, current temporary grant and source drift change/refuse proof", async () => { + const f = fixture(), originalId = f.principal.credentialId + let seen = "" + const source = f.dependencies.connectionSource + f.dependencies.connectionSource = async (principal, client) => { seen = principal.credentialId; return { ...(await source(principal, client)), connection: fixtureConnection } } + const fixtureConnection = (await fixture().dependencies.connectionSource(f.principal)).connection + fixtureConnection.id = f.principal.connectionId; fixtureConnection.policy.id = f.principal.connectionId + const authorize = createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies) + f.principal.credentialId = randomUUID() + const first = await authorize(request(), [1]); assert.equal(seen, originalId) + f.setGrants([{ payload: { id: "grant-a", expiresAt: "2099-01-01T00:00:00Z" } }]) + const second = await authorize(request(), [1]); assert.notEqual(first.authorityProof, second.authorityProof) + f.onProtections(() => { fixtureConnection.policy.version++ }) + // The captured before row must not share a mutable provider object. + await assert.rejects(authorize(request(), [1]), safe) +}) + +test("global protections: deny blocks MCP and REST; human-required permits only MCP planning lane", async () => { + const f = fixture(); f.setProtections([{ object_ref: { kind: "server", id: "2", serverId: 2, protection: "human-approval-required" } }]) + await createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies)(request(), [1]) + await assert.rejects(createRestServerPollAuthorityForTests(actor(), context(), f.dependencies)(request(), [1]), { code: "APPROVAL_REQUIRED" }) + f.setProtections([{ object_ref: { kind: "server", id: "2", serverId: 2, protection: "deny" } }]) + await assert.rejects(createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies)(request(), [1]), { code: "PROTECTED_OBJECT" }) +}) + +test("REST rejects missing/expired JWT or app/permission before any I/O", () => { + const f = fixture() + for (const value of [undefined, { ...actor(), permissions: [], isAdmin: true }, { ...actor(), issuedAt: undefined }, { ...actor(), expiresAt: undefined }, + { ...actor(), apps: [] }, { ...actor(), expiresAt: Date.now() - 1 }, { ...actor(), id: "x".repeat(201) }]) { + assert.throws(() => createRestServerPollAuthorityForTests(value, context(), f.dependencies), safe) + } + assert.equal(f.leases.length, 0); assert.equal(f.portal(), 0) +}) + +test("REST current portal fences cloned original JWT and authorized IDs before safe 404", async () => { + const f = fixture(), original = actor(), seen: AuthUser[] = [] + f.dependencies.portal = async value => { seen.push(value as AuthUser); assert(Object.isFrozen(value)) } + const authorize = createRestServerPollAuthorityForTests(original, context(), f.dependencies) + original.id = "changed"; original.permissions.length = 0; original.expiresAt = 0 + const grant = await authorize(request(), [1]); assert.deepEqual(grant.serverIds, [1, 2]); assert.equal(seen.length, 2); assert.equal(seen[0].id, "operator") + f.setServers([{ id: 2 }]); await assert.rejects(authorize(request(), [1]), { code: "NOT_FOUND" }) + assert.equal(seen.length, 3) +}) + +test("local mode requires trusted authRequired=false and never ignores supplied stale actors", async () => { + const f = fixture(), value = { ...context(), authRequired: false } + assert.equal((await createRestServerPollAuthorityForTests(undefined, value, f.dependencies)(request(), [1])).global, true) + assert.equal(f.portal(), 0) + assert.throws(() => createRestServerPollAuthorityForTests({ ...actor(), expiresAt: 0 }, value, f.dependencies), safe) +}) + +test("portal revocation before/after source metadata and generation drift refuse safe grant", async () => { + for (const at of [1, 2]) { + const f = fixture(); let count = 0 + f.dependencies.portal = async () => { if (++count === at) throw new McpError("OWNER_CLAIMS_REVOKED", "PRIVATE_PROVIDER", 401) } + await assert.rejects(createRestServerPollAuthorityForTests(actor(), context(), f.dependencies)(request(), [1]), safe) + if (at === 1) assert(!f.leases[0].commands.some(sql => sql.startsWith("SELECT id FROM servers"))) + } + const f = fixture(); f.onProtections(() => f.setGeneration("changed")) + await assert.rejects(createRestServerPollAuthorityForTests(actor(), context(), f.dependencies)(request(), [1]), { code: "STATE_CONFLICT" }) +}) + +test("catalog overflow/duplicates/order/extra fields and malformed protections fail closed", async () => { + for (const rows of [[{ id: 1 }, { id: 1 }], [{ id: 2 }, { id: 1 }], [{ id: 1, password: "PRIVATE_PROVIDER" }], Array.from({ length: 1001 }, (_, index) => ({ id: index + 1 }))]) { + const f = fixture(); f.setServers(rows) + await assert.rejects(createRestServerPollAuthorityForTests(actor(), context(), f.dependencies)(request(), [1]), safe) + } + const f = fixture(); f.setProtections([{ object_ref: { protection: "unknown" } }]) + await assert.rejects(createRestServerPollAuthorityForTests(actor(), context(), f.dependencies)(request(), [1]), safe) +}) + +test("whole callback bounds crypto gap, destroys once and rejects late facade SQL", async () => { + const f = fixture(), started = deferred(), resume = deferred(), late = deferred() + f.dependencies.portal = async (_actor, client) => { + assert(client); started.resolve(); await resume.promise + await assert.rejects(client.query("SELECT late_private_metadata"), safe); late.resolve() + } + const authorize = createRestServerPollAuthorityForTests(actor(), { ...context(), deadlineAt: Date.now() + 40 }, f.dependencies) + const pending = authorize(request(), [1]); await started.promise; await assert.rejects(pending, safe) + assert.deepEqual(f.leases[0].releases, [true]); assert(!f.leases[0].commands.includes("ROLLBACK")) + resume.resolve(); await late.promise; assert(!f.leases[0].commands.includes("SELECT late_private_metadata")); assert.deepEqual(f.leases[0].releases, [true]) +}) + +test("pre-aborted/original expiry refuses without checkout and provider details sanitized", async () => { + const f = fixture(), controller = new AbortController(); controller.abort() + assert.throws(() => createMcpServerPollAuthorityForTests({ ...f.principal, credentialExpiresAt: new Date(0).toISOString() }, {}, f.dependencies), safe) + assert.throws(() => createRestServerPollAuthorityForTests(actor(), { ...context(), signal: controller.signal }, f.dependencies), safe) + assert.equal(f.leases.length, 0) + f.dependencies.connectionSource = async () => { throw new Error("PRIVATE_PROVIDER") } + await assert.rejects(createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies)(request(), [1]), safe) + assert.deepEqual(f.leases[0].releases, [false]) +}) + +test("MCP source is mandatory, read before/after on each invocation, with no second grant lookup", async () => { + const f = fixture(), authorize = createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies) + await authorize(request(), [1]); await authorize(request(), [1]) + assert.equal(f.reads(), 4) + assert.equal(f.leases.flatMap(lease => lease.commands).filter(sql => sql.startsWith("SELECT payload")).length, 0, + "The coherent provider already supplies exact raw grant rows; authority must not requery them") + assert(f.leases.every(lease => lease.commands.length === 5)) + f.onProtections(() => f.setGrants([{ payload: { credentialId: f.principal.credentialId, opaque: "changed-source" } }])) + await assert.rejects(authorize(request(), [1]), { code: "STATE_CONFLICT" }) +}) + +test("MCP duplicate coherent grant source refuses rather than using a fallback read", async () => { + const f = fixture(); f.setGrants([{ payload: {} }, { payload: {} }]) + await assert.rejects(createMcpServerPollAuthorityForTests(f.principal, {}, f.dependencies)(request(), [1]), safe) + assert.equal(f.reads(), 1) + assert(!f.leases[0].commands.some(sql => sql.startsWith("SELECT payload"))) +}) + +test("MCP missing coherent source has no legacy connection fallback", async () => { + const f = fixture(); let fallbackCalls = 0 + const dependencies = { ...f.dependencies, connectionSource: undefined, connection: async () => { fallbackCalls++; return {} } } as unknown as typeof f.dependencies + await assert.rejects(createMcpServerPollAuthorityForTests(f.principal, {}, dependencies)(request(), [1]), { code: "STATE_UNAVAILABLE" }) + assert.equal(fallbackCalls, 0) + assert(!f.leases[0].commands.some(sql => sql.startsWith("SELECT payload"))) +}) diff --git a/backend/src/services/server-poll-authority.ts b/backend/src/services/server-poll-authority.ts new file mode 100644 index 00000000..6d7e933b --- /dev/null +++ b/backend/src/services/server-poll-authority.ts @@ -0,0 +1,195 @@ +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +/** Current authority for complete poll/partition maintenance. Trusted constructors + * only; importing this module neither enrolls schema nor performs I/O. */ +import type { PoolClient } from "pg" +import { z } from "zod" +import { mcpProtectedObjectSchema } from "@mmapp/contracts/mcp" +import { hasPermission, type AuthUser } from "../lib/permissions.js" +import { fixedPrincipalBoundarySource, readSynchronousPrincipalBoundary, principalBoundaryProjection, type PrincipalBoundaryPacket, effectiveMcpConnectionSource, type McpPrincipal } from "../modules/mcp/store.js" +import { captureRuntimePollPrincipalBoundary, coordinateRuntimePollPrincipalBoundary, type RuntimePollBoundary } from "../modules/mcp/runtime-poll-principal-boundary.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { accessGeneration, mcpStateDirectory, fingerprint, McpError } from "../modules/mcp/security.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import type { ProbeStorageConnection, ProbeStorageOptions, ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import type { ServerPollDependencies, ServerPollGrant } from "./server-poll-execution.js" + +export const SERVER_POLL_OPERATION = "servers.poll" +export interface ServerPollAuthorityOptions { deadlineAt?: number; signal?: AbortSignal } +export interface ServerPollRestAuthorityContext extends ServerPollAuthorityOptions { authRequired: boolean; acceptedAt: number; requestId: string; assertRequestActive?: () => void } +type Dependencies = { connect: (options: ProbeStorageOptions) => Promise; connectionSource: typeof effectiveMcpConnectionSource; + portal: typeof assertPortalClaimsCurrent; generation: typeof accessGeneration } +const production: Dependencies = { connect: connectProbeStorage, connectionSource: effectiveMcpConnectionSource, portal: assertPortalClaimsCurrent, generation: accessGeneration } +const fail = (code = "PERMISSION_DENIED", status = 403): never => { throw new McpError(code, + code === "NOT_FOUND" ? "Server not found" : code === "INVALID_INPUT" ? "Некорректный контекст полного опроса" : "Актуальный допуск полного опроса не подтверждён", status) } +const live = (deadlineAt: number, signal?: AbortSignal) => { if (signal?.aborted || Date.now() >= deadlineAt) fail("READ_CANCELLED", 409) } +const identifier = z.string().min(1).max(200).refine(value => value.trim() === value && !/[\x00-\x1f\x7f]/.test(value)) +const principalSchema = z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.string().datetime() }).strict() +function deadline(options: ServerPollAuthorityOptions, expiresAt: number) { + const now = Date.now(), value = Math.min(options.deadlineAt ?? now + SERVER_POLL_WALL_BUDGET_MS, now + SERVER_POLL_WALL_BUDGET_MS, expiresAt) + if (!Number.isSafeInteger(value) || options.deadlineAt !== undefined && !Number.isSafeInteger(options.deadlineAt)) fail("INVALID_INPUT", 400) + live(value, options.signal); return value +} +function ids(value: unknown): number[] { + if (!Array.isArray(value) || value.length > 1000 || value.some(id => !Number.isSafeInteger(id) || id <= 0) || new Set(value).size !== value.length) fail("STATE_UNAVAILABLE", 503) + return [...value as number[]].sort((left, right) => left - right) +} +function safe(error: unknown): never { + const code = error instanceof McpError ? error.code : "STATE_UNAVAILABLE" + if (["NOT_FOUND", "INVALID_INPUT", "READ_CANCELLED", "STATE_CONFLICT", "STATE_UNAVAILABLE", "PROTECTED_OBJECT", "APPROVAL_REQUIRED"].includes(code)) { + return fail(code, code === "NOT_FOUND" ? 404 : code === "INVALID_INPUT" ? 400 : code === "STATE_UNAVAILABLE" ? 503 : 409) + } + return fail("PERMISSION_DENIED", 403) +} + +/** Owns a fresh READ COMMITTED read-only metadata lease, never the maintenance + * backend. Whole callback/checkout includes crypto and portal filesystem gaps. + * Completion/abort expires the facade; late work cannot reacquire or issue SQL. */ +async function metadata(deps: Dependencies, options: ProbeStorageOptions & { assertRequestActive?: () => void }, work: (sql: ProbeStorageSql, client: Pick, owner: { signal: AbortSignal; assertActive(): void; retireUnsafeOwner(): void }) => Promise) { + let active = true, released = false, broken = false, busy = false, transaction = false + const lifetime = new AbortController() + let physical: ProbeStorageConnection | undefined + const close = (unsafe: boolean) => { lifetime.abort(); if (physical && !released) { released = true; try { physical.release(unsafe) } catch { /* No driver details escape. */ } } } + const fence = () => { options.assertRequestActive?.(); live(options.deadlineAt, options.signal); if (!active || broken) fail("STATE_UNAVAILABLE", 503) } + let stop!: () => void + const stopped = new Promise((_yes, reject) => { stop = () => { active = false; broken = true; close(true); reject(new McpError("READ_CANCELLED", "Полный опрос отменён или истёк", 409)) } }) + const timer = setTimeout(stop, Math.max(1, options.deadlineAt - Date.now())) + options.signal?.addEventListener("abort", stop, { once: true }) + const sql: ProbeStorageSql = Object.freeze({ async query(text: string, values?: unknown[]) { + fence(); const connection = physical + if (!connection || busy) { stop(); return fail("STATE_UNAVAILABLE", 503) } + busy = true + try { const result = await connection.query(text, values, { signal: options.signal, timeoutMs: Math.max(1, Math.min(10000, options.deadlineAt - Date.now())) }); fence(); return result } + catch (error) { broken = true; close(true); throw error } + finally { busy = false } + } }) + const completion = Promise.resolve().then(async () => { + fence(); physical = await deps.connect({ deadlineAt: options.deadlineAt, signal: options.signal }) + try { fence() } catch (error) { close(true); throw error } + if (!active || options.signal?.aborted || Date.now() >= options.deadlineAt) { close(true); fail("READ_CANCELLED", 409) } + try { + await sql.query("BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY"); transaction = true + await sql.query("SET LOCAL statement_timeout='10s'") + const value = await work(sql, { query: sql.query as PoolClient["query"] }, { + signal: lifetime.signal, assertActive() { fence(); if (!transaction || busy) fail("STATE_UNAVAILABLE", 503) }, retireUnsafeOwner: stop, + }); fence(); return value + } finally { + if (transaction && active && !broken && !busy && !options.signal?.aborted && Date.now() < options.deadlineAt) { await sql.query("ROLLBACK"); transaction = false } + } + }) + try { if (options.signal?.aborted || Date.now() >= options.deadlineAt) stop(); const value = await Promise.race([completion, stopped]); fence(); return value } + finally { active = false; clearTimeout(timer); options.signal?.removeEventListener("abort", stop); close(broken || busy || transaction) } +} + +async function catalogue(sql: ProbeStorageSql, serverId: number, affected: number[]) { + if (!Number.isSafeInteger(serverId) || serverId <= 0) fail("INVALID_INPUT", 400) + const rows = (await sql.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows + if (!Array.isArray(rows) || rows.some(row => Object.keys(row).join(" ") !== "id")) fail("STATE_UNAVAILABLE", 503) + const all = ids(rows.map(row => row.id)) + if (rows.some((row, index) => row.id !== all[index])) fail("STATE_UNAVAILABLE", 503) + if (!all.includes(serverId)) fail("NOT_FOUND", 404) + if (affected.some(id => !all.includes(id))) fail("STATE_CONFLICT", 409) + return all +} +async function protections(sql: ProbeStorageSql, humanPlan: boolean) { + const rows = (await sql.query("SELECT object_ref FROM mcp_protections ORDER BY id LIMIT 1001")).rows + if (!Array.isArray(rows) || rows.length > 1000) fail("STATE_UNAVAILABLE", 503) + const refs = rows.map(row => { if (Object.keys(row).join(" ") !== "object_ref") fail("STATE_UNAVAILABLE", 503); return mcpProtectedObjectSchema.parse(row.object_ref) }) + // Poll maintenance is global. A scoped deny must not be bypassed by dropping + // history/partitions outside the source server selected in the REST URL. + if (refs.some(ref => ref.protection === "deny")) fail("PROTECTED_OBJECT", 409) + if (!humanPlan && refs.length) fail("APPROVAL_REQUIRED", 409) + return refs +} +const grant = (serverIds: number[], proof: unknown): ServerPollGrant => Object.freeze({ global: true, + serverIds: Object.freeze([...serverIds]) as unknown as number[], authorityProof: fingerprint(proof) }) + +function mcpAuthority(rawPrincipal: McpPrincipal, options: ServerPollAuthorityOptions, deps: Dependencies, runtimeBoundary?: RuntimePollBoundary): ServerPollDependencies["authorize"] { + const principal = Object.freeze(principalSchema.parse(structuredClone(rawPrincipal))), signal = options.signal + const deadlineAt = deadline(options, Date.parse(principal.credentialExpiresAt)) + return async (request, affected, extraSignal) => { + const combined = signal && extraSignal ? AbortSignal.any([signal, extraSignal]) : signal ?? extraSignal + try { + live(deadlineAt, combined) + return await metadata(deps, { deadlineAt: Math.min(deadlineAt, Date.now() + 10000), signal: combined }, async (sql, client, owner) => { + const validate = ({ connection, grantRows: payloads }: Awaited>, generation: string) => { + live(deadlineAt, combined) + if (payloads.length > 1 || connection.id !== principal.connectionId || connection.generation !== generation + || connection.policy.status !== "active" || connection.policy.mode === "observer" || !connection.policy.scope.allowGlobal + || !connection.policy.scope.operationIds.includes(SERVER_POLL_OPERATION) + || !hasPermission(connection.owner_permissions, "mm:servers:write") || !hasPermission(connection.policy.permissions, "mm:servers:write") + || Date.parse(connection.policy.expiresAt) <= Date.now() || connection.owner_valid_until.getTime() <= Date.now()) fail() + const allowed = ids(connection.policy.scope.serverIds) + live(deadlineAt, combined) + return { ownerId: connection.owner_id, ownerPermissions: [...connection.owner_permissions], policy: structuredClone(connection.policy), generation, + ownerValidUntil: connection.owner_valid_until.toISOString(), ownerClaimsIssuedAt: connection.owner_claims_issued_at.toISOString(), grants: payloads, allowed } + } + const read = async () => { + live(deadlineAt, combined) + const source = await deps.connectionSource(principal, client), generation = await deps.generation() + return validate(source, generation) + } + const poll = async (before: ReturnType) => { + const all = await catalogue(sql, request.serverId, ids(affected)) + if (all.some(id => !before.allowed.includes(id))) fail() + const refs = await protections(sql, true), after = await read() + if (fingerprint(before) !== fingerprint(after)) fail("STATE_CONFLICT", 409) + return grant(all, { transport: "mcp", principal, authority: before, serverIds: all, protections: refs }) + } + if (runtimeBoundary) { + const bound = await readSynchronousPrincipalBoundary(principal, client, (packet: PrincipalBoundaryPacket) => { + const source = principalBoundaryProjection(packet, principal, mcpStateDirectory(), true) + const before = validate(source, source.generation) + return { result: coordinateRuntimePollPrincipalBoundary(runtimeBoundary, packet, { ...owner, client }, () => poll(before)) } + }) + return await bound.result + } + return await poll(await read()) + }) + } catch (error) { return safe(error) } + } +} + +function restAuthority(rawActor: AuthUser | undefined, context: ServerPollRestAuthorityContext, deps: Dependencies): ServerPollDependencies["authorize"] { + const acceptedAt = context.acceptedAt, requestId = identifier.parse(context.requestId), signal = context.signal, assertRequestActive = context.assertRequestActive + if (assertRequestActive !== undefined && typeof assertRequestActive !== "function") fail("INVALID_INPUT", 400) + assertRequestActive?.() + if (typeof context.authRequired !== "boolean" || !Number.isSafeInteger(acceptedAt) || acceptedAt <= 0 || acceptedAt > Date.now()) fail("INVALID_INPUT", 400) + const local = context.authRequired === false && rawActor === undefined + if (!local && (!rawActor || !identifier.safeParse(rawActor.id).success || !Array.isArray(rawActor.apps) || !rawActor.apps.includes("mm") + || !Array.isArray(rawActor.permissions) || !hasPermission(rawActor.permissions, "mm:servers:write") + || !Number.isSafeInteger(rawActor.issuedAt) || rawActor.issuedAt! <= 0 || rawActor.issuedAt! > Date.now() + || !Number.isSafeInteger(rawActor.expiresAt) || rawActor.expiresAt! <= Date.now() || rawActor.expiresAt! <= rawActor.issuedAt!)) fail() + const actor = rawActor ? Object.freeze({ ...rawActor, apps: Object.freeze([...rawActor.apps]) as unknown as string[], permissions: Object.freeze([...rawActor.permissions]) as unknown as string[] }) : undefined + const deadlineAt = deadline(context, Math.min(acceptedAt + SERVER_POLL_WALL_BUDGET_MS, actor?.expiresAt ?? Infinity)) + return async (request, affected, extraSignal) => { + const combined = signal && extraSignal ? AbortSignal.any([signal, extraSignal]) : signal ?? extraSignal + try { + live(deadlineAt, combined) + return await metadata(deps, { deadlineAt: Math.min(deadlineAt, Date.now() + 10000), signal: combined, assertRequestActive }, async (sql, client) => { + const read = async () => { live(deadlineAt, combined); if (actor) await deps.portal(actor, client); const generation = await deps.generation(); live(deadlineAt, combined); return generation } + const before = await read(), all = await catalogue(sql, request.serverId, ids(affected)), refs = await protections(sql, false), after = await read() + if (before !== after) fail("STATE_CONFLICT", 409) + return grant(all, { transport: "rest", requestId, acceptedAt, actor: actor ? { id: actor.id, issuedAt: actor.issuedAt, expiresAt: actor.expiresAt, permissions: actor.permissions } : { id: "local-operator" }, generation: before, serverIds: all, protections: refs }) + }) + } catch (error) { return safe(error) } + } +} + +function construct(work: () => T): T { try { return work() } catch (error) { return safe(error) } } +export const createMcpServerPollAuthority = (principal: McpPrincipal, options: ServerPollAuthorityOptions = {}) => construct(() => mcpAuthority(principal, options, production)) +export const createRestServerPollAuthority = (actor: AuthUser | undefined, context: ServerPollRestAuthorityContext) => construct(() => restAuthority(actor, context, production)) +/** Explicit trusted unit composition only, never deserialized from requests. */ +export const createMcpServerPollAuthorityForTests = (principal: McpPrincipal, options: ServerPollAuthorityOptions, dependencies: Dependencies) => construct(() => mcpAuthority(principal, options, dependencies)) +export const createRestServerPollAuthorityForTests = (actor: AuthUser | undefined, context: ServerPollRestAuthorityContext, dependencies: Dependencies) => construct(() => restAuthority(actor, context, dependencies)) + +/** Fixed production composition. Mismatch falls back before sharing; failures + * of a minted active-origin capability never downgrade into another path. */ +const combinedRuntimeAuthorities = new WeakSet() +export function isCombinedRuntimeMcpServerPollAuthority(value: unknown): value is ServerPollDependencies["authorize"] { return typeof value === "function" && combinedRuntimeAuthorities.has(value as ServerPollDependencies["authorize"]) } +export function createCombinedRuntimeMcpServerPollAuthority(principal: McpPrincipal, options: ServerPollAuthorityOptions = {}): ServerPollDependencies["authorize"] | undefined { + const token = captureRuntimePollPrincipalBoundary(principal, fixedPrincipalBoundarySource) + if (!token) return undefined + const authority = construct(() => mcpAuthority(principal, options, production, token)) + combinedRuntimeAuthorities.add(authority) + return authority +} diff --git a/backend/src/services/server-poll-boundary.test.ts b/backend/src/services/server-poll-boundary.test.ts new file mode 100644 index 00000000..331d2adb --- /dev/null +++ b/backend/src/services/server-poll-boundary.test.ts @@ -0,0 +1,82 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createServerPollBoundaryCoordinator } from "./server-poll-boundary.js" +import { createServerPartitionMaintenanceLease,SERVER_PARTITION_WRITER_INVENTORY_SQL } from "./server-partition-maintenance-lease.js" +import { PARTITION_SPECS } from "../db/partitions.js" +import type { ServerPollGrant } from "./server-poll-execution.js" + +const request={serverId:1,receiptId:"11111111-1111-4111-8111-111111111111"},proof="a".repeat(64) +const options={deadlineAt:10000,serverIds:[1,2,3]} +function fixture(){ + const events:string[]=[],requirements:number[][]=[],released:boolean[]=[] + let clock=1000,grant:ServerPollGrant={global:true,serverIds:[1,2,3],authorityProof:proof},closed=false,hook:(()=>Promise)|undefined + const boundary=createServerPollBoundaryCoordinator(async(_request,ids)=>{events.push("fresh");requirements.push(ids);await hook?.();return structuredClone(grant)},request,()=>clock) + const inventory=PARTITION_SPECS.map((spec,index)=>({parent:spec.parent,oid:String(index+1),relname:spec.parent,nspname:"public",trigger_type:31,trigger_enabled:"A",trigger_name:"server_partition_writer_guard",trigger_args:0,unconditional:true,constraint_oid:"0",function_schema:"public",function_name:"server_partition_writer_guard",function_language:"plpgsql",security_definer:false,function_source:"BEGIN PERFORM pg_advisory_xact_lock_shared(8723110::bigint); IF TG_OP='DELETE' THEN RETURN OLD; END IF; RETURN NEW; END;"})) + const lease=createServerPartitionMaintenanceLease({boundary,now:()=>clock,assertAuthority:async()=>{events.push("legacy")},connect:async()=>({release:unsafe=>{released.push(!!unsafe)},query:async text=>{events.push(text);if(text===SERVER_PARTITION_WRITER_INVENTORY_SQL)return {rows:inventory};if(text.includes("partition-lease:unlock"))return {rows:[{unlocked:true}]};return {rows:[]}}})}) + return {boundary,lease,events,requirements,released,fence:()=>boundary.fence([1],proof,options,()=>{if(closed)throw Error("closed local holder")}), + clock:(value:number)=>{clock=value},grant:(value:ServerPollGrant)=>{grant=value},close:()=>{closed=true},hook:(value:(()=>Promise)|undefined)=>{hook=value}} +} +test("one source SQL obtains exactly one fresh complete grant on each side, with all holder IDs",async()=>{ + const f=fixture(),fence=f.fence() + await f.lease.withLease(()=>f.lease.transaction(true,fence,options,async query=>{ + const start=f.events.length;await query("SELECT owned_target") + assert.deepEqual(f.events.slice(start),["fresh","SELECT owned_target","fresh"]) + assert.deepEqual(f.requirements.slice(-2),[[1,2,3],[1,2,3]]) + }),options) + assert.deepEqual(f.released,[false]) +}) +test("every later SQL obtains new decisions; grant is never cached across queries",async()=>{ + const f=fixture(),fence=f.fence() + await assert.rejects(f.lease.withLease(()=>f.lease.transaction(true,fence,options,async query=>{ + await query("SELECT first") + f.grant({global:true,serverIds:[1,2],authorityProof:proof}) + await query("SELECT forbidden_second") + }),options)) + assert(f.events.includes("SELECT first"));assert(!f.events.includes("SELECT forbidden_second"));assert.deepEqual(f.released,[true]) +}) +test("epoch drift after a target SQL refuses its acknowledgement and destroys ownership",async()=>{ + const f=fixture(),fence=f.fence();let target=false + f.hook(async()=>{if(target)f.grant({global:true,serverIds:[1,2,3],authorityProof:"b".repeat(64)})}) + await assert.rejects(f.lease.withLease(()=>f.lease.transaction(true,fence,options,async query=>{ + // Set the post-dispatch hook from the physical result, not a wall clock. + const original=f.events.push.bind(f.events) + f.events.push=(...items)=>{if(items.includes("SELECT epoch_target"))target=true;return original(...items)} + await query("SELECT epoch_target") + }),options)) + assert.equal(f.events.filter(item=>item==="SELECT epoch_target").length,1);assert.deepEqual(f.released,[true]);assert(!f.events.includes("COMMIT")) +}) +test("unowned/wrapped fences retain the original generic checks and are never silently merged",async()=>{ + const f=fixture(),owned=f.fence(),wrapped=async()=>owned() + assert.equal(f.boundary.combine([wrapped],[1],options),undefined) + await f.lease.withLease(()=>f.lease.transaction(true,wrapped,options,async query=>{ + const start=f.events.length;await query("SELECT generic") + assert.equal(f.events.slice(start).filter(item=>item==="legacy").length,4) + assert.equal(f.events.slice(start).filter(item=>item==="fresh").length,2) + }),options) +}) +test("abort, original deadline and closed local scope still fence async authority gaps",async()=>{ + for(const fault of ["abort","expiry","local"]){ + const f=fixture(),controller=new AbortController() + const fence=f.boundary.fence([1],proof,{...options,signal:controller.signal},()=>{if(fault==="local"&&f.events.includes("close"))throw Error("closed")}) + f.hook(async()=>{if(fault==="abort")controller.abort();else if(fault==="expiry")f.clock(10001);else f.events.push("close")}) + await assert.rejects(fence());assert.equal(f.events.filter(item=>item==="fresh").length,1) + } +}) +test("requirements merge only within the same session and preserve original proof/deadline",async()=>{ + const f=fixture(),other=fixture(),a=f.boundary.fence([1],proof,{deadlineAt:5000}),b=f.boundary.fence([2],proof,{deadlineAt:6000}) + assert.equal(f.boundary.combine([other.fence()],[3],options),undefined) + const combined=f.boundary.combine([a,b],[3],options)! + await combined();assert.deepEqual(f.requirements.at(-1),[1,2,3]);f.clock(5000);await assert.rejects(combined()) + assert.throws(()=>f.boundary.combine([a,f.boundary.fence([1],"b".repeat(64),options)],[1],options)) +}) +test("an abandoned callback query cannot dispatch after its transaction already completed",async()=>{ + const f=fixture(),fence=f.fence();let leaked:Promise|undefined,release!:()=>void,blocked=false + const gate=new Promise(resolve=>{release=resolve}) + await f.lease.withLease(()=>f.lease.transaction(true,fence,options,async query=>{ + f.hook(async()=>{if(!blocked){blocked=true;await gate}}) + leaked=query("SELECT abandoned");void leaked.catch(()=>{}) + // Let the query enter its authority await, then return without awaiting it. + await Promise.resolve();await Promise.resolve() + }),options) + release();await assert.rejects(leaked!);assert(!f.events.includes("SELECT abandoned"));assert.deepEqual(f.released,[false]) +}) diff --git a/backend/src/services/server-poll-boundary.ts b/backend/src/services/server-poll-boundary.ts new file mode 100644 index 00000000..978c46ab --- /dev/null +++ b/backend/src/services/server-poll-boundary.ts @@ -0,0 +1,50 @@ +/** Session-private composition of complete authority checks. Only immutable + * scope/deadline requirements are merged; EVERY invocation obtains a new grant. + * The WeakMap brand cannot be supplied by a request or copied across sessions. */ +import type { ServerPollDependencies,ServerPollRequest } from "./server-poll-execution.js" +import { pollProblem } from "./server-poll-execution-model.js" + +export interface PollBoundaryOptions {signal?:AbortSignal;deadlineAt:number} +export type PollBoundaryFence=()=>Promise +export interface ServerPollBoundaryCoordinator { + fence(ids:readonly number[],expectedProof:string,options:PollBoundaryOptions,check?:()=>void):PollBoundaryFence + combine(fences:readonly PollBoundaryFence[],ids:readonly number[],options:PollBoundaryOptions):PollBoundaryFence|undefined + owns(fence:PollBoundaryFence):boolean +} +interface Requirement {ids:readonly number[];proof:string;options:PollBoundaryOptions;check?:()=>void} +export function createServerPollBoundaryCoordinator(provider:ServerPollDependencies["authorize"],rawRequest:ServerPollRequest,now=Date.now):ServerPollBoundaryCoordinator{ + const request=Object.freeze(structuredClone(rawRequest)),authorize=provider,clock=now,owned=new WeakMap() + const fail=(code:"PERMISSION_DENIED"|"STATE_CONFLICT"|"ABORTED"="STATE_CONFLICT"):never=>{throw pollProblem(code,"Допуск границы полного poll изменился или истёк")} + const ids=(values:readonly number[])=>{ + const result=[...new Set([request.serverId,...values])].sort((a,b)=>a-b) + if(result.length>1000||result.some(id=>!Number.isSafeInteger(id)||id<=0))fail() + return Object.freeze(result) + } + const local=(requirements:readonly Requirement[])=>{for(const item of requirements){item.options.signal?.throwIfAborted();if(clock()>=item.options.deadlineAt)fail("ABORTED");if(item.check?.()!==undefined)fail()}} + const create=(requirements:readonly Requirement[]):PollBoundaryFence=>{ + if(!requirements.length||requirements.some(item=>!Number.isSafeInteger(item.options.deadlineAt)||!/^[a-f0-9]{64}$/.test(item.proof)))fail() + const captured=Object.freeze(requirements.map(item=>Object.freeze({ids:ids(item.ids),proof:item.proof,check:item.check,options:Object.freeze({signal:item.options.signal,deadlineAt:item.options.deadlineAt})}))) + if(new Set(captured.map(item=>item.proof)).size!==1)fail() + const required=ids(captured.flatMap(item=>[...item.ids])) + const signals=[...new Set(captured.flatMap(item=>item.options.signal?[item.options.signal]:[]))] + const signal=signals.length?AbortSignal.any(signals):undefined + const fence:PollBoundaryFence=async()=>{ + local(captured) + const grant=await authorize({...request},[...required],signal) + local(captured) + if(!grant.global||!Array.isArray(grant.serverIds)||required.some(id=>!grant.serverIds.includes(id)))fail("PERMISSION_DENIED") + if(grant.authorityProof!==captured[0].proof)fail() + } + owned.set(fence,captured);return fence + } + const coordinator:ServerPollBoundaryCoordinator={ + fence:(selected,proof,options,check)=>create([{ids:ids(selected),proof,options,check}]), + combine(fences,selected,options){ + if(!fences.length||fences.some(fence=>!owned.has(fence)))return undefined + const requirements=fences.flatMap(fence=>[...owned.get(fence)!]) + return create([...requirements,{ids:ids(selected),proof:requirements[0].proof,options}]) + }, + owns:fence=>owned.has(fence), + } + return Object.freeze(coordinator) +} diff --git a/backend/src/services/server-poll-capacity.test.ts b/backend/src/services/server-poll-capacity.test.ts new file mode 100644 index 00000000..a609c950 --- /dev/null +++ b/backend/src/services/server-poll-capacity.test.ts @@ -0,0 +1,124 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { readServerPollCapacity, type ServerPollCapacitySql } from "./server-poll-capacity.js" +import { SERVER_POLL_STORAGE_POLICY as policy } from "./server-poll-storage-model.js" +import { mcpPollStorageCapacitySchema } from "@mmapp/contracts/mcp" + +const row = (owner: number | null = 2, global = 10) => ({ retention_days: 30, + global_compacted_admissions:"0",global_archive_bytes:"0",owner_compacted_admissions:owner===null?null:"0",owner_archive_bytes:owner===null?null:"0", + global_admissions: String(global), global_reserved_rows: String(global * policy.attemptRows), global_reserved_bytes: String(global * (policy.attemptBytes + policy.admissionBytes)), + owner_admissions: owner === null ? null : String(owner), owner_reserved_rows: owner === null ? null : String(owner * policy.attemptRows), owner_reserved_bytes: owner === null ? null : String(owner * (policy.attemptBytes + policy.admissionBytes)) }) +function fixture(rows: Record[] = [row()]) { + const calls: Array<{ text: string; values: unknown[] }> = [] + const sql: ServerPollCapacitySql = { async query(text, values) { calls.push({ text, values }); return { rows } } } + return { sql, calls } +} + +test("one parameterized bounded snapshot reads only settings and exact owner/global usage without maintenance or locks", async () => { + const f = fixture(), owner = "operator' OR true--" + const result = await readServerPollCapacity(f.sql, owner, true) + assert.deepEqual(result, { status: "observed", owner: { used: 2, limit: 8, remaining: 6 }, global: { exhausted: false, usage: { used: 10, limit: 128, remaining: 118 } }, retentionDays: 30 }) + assert.equal(f.calls.length, 1); const { text, values } = f.calls[0] + assert.deepEqual(values, ["owner:" + owner]); assert(!text.includes(owner)); assert.match(text, /^SELECT\b/) + assert.match(text, /FROM public\.mcp_settings\b/); assert.equal((text.match(/LEFT JOIN public\.server_poll_storage_usage\b/g) ?? []).length, 2) + assert.match(text, /o\.scope=\$1/); assert.match(text, /LIMIT 2$/) + assert(!/\b(?:UPDATE|INSERT|DELETE|TRUNCATE|LOCK|FOR|CALL|gc|admissions_list)\b|;/i.test(text)) + assert(!text.includes("server_poll_storage_admissions")) +}) + +test("non-admin hides all global counts while reporting exhaustion; admin alone receives numbers", async () => { + for (const used of [0, 127, 128]) { + const f = fixture([row(0, used)]), result = await readServerPollCapacity(f.sql, "one-owner", false) + assert.deepEqual(result, { status: "observed", owner: { used: 0, limit: 8, remaining: 8 }, global: { exhausted: used === 128, usage: null }, retentionDays: 30 }) + assert(!JSON.stringify(result).includes("one-owner")); assert(!JSON.stringify(result).includes('"ready"')) + } +}) + +test("owner eight/full global128 expose zero remaining and only entirely null missing owner is zero", async () => { + const full = await readServerPollCapacity(fixture([row(8, 128)]).sql, "owner", true) + assert.deepEqual(full, { status: "observed", owner: { used: 8, limit: 8, remaining: 0 }, global: { exhausted: true, usage: { used: 128, limit: 128, remaining: 0 } }, retentionDays: 30 }) + assert.deepEqual(await readServerPollCapacity(fixture([row(null, 0)]).sql, "new-owner", false), { status: "observed", owner: { used: 0, limit: 8, remaining: 8 }, global: { exhausted: false, usage: null }, retentionDays: 30 }) +}) + +test("SQL failure/missing relation or settings/global accounting is unavailable and never zero", async () => { + for (const code of ["42P01", "08006", "57014"]) { + let calls = 0 + assert.deepEqual(await readServerPollCapacity({ async query() { calls++; throw Object.assign(Error("private SQL failure"), { code }) } }, "owner", true), { status: "unavailable" }) + assert.equal(calls, 1) + } + for (const rows of [[], [row(), row()], [{}], [{ ...row(), global_admissions: null, global_reserved_rows: null, global_reserved_bytes: null }], [{ ...row(), retention_days: undefined }]]) + assert.deepEqual(await readServerPollCapacity(fixture(rows).sql, "owner", true), { status: "unavailable" }) +}) + +test("invalid owner/admin values refuse before query; trimmed Unicode identity remains exact", async () => { + for (const owner of ["", " ", " owner", "owner ", "a\nb", "a\0b", "a\x7fb", "a\x85b", "a".repeat(201), undefined, 42]) { + const f = fixture(); assert.deepEqual(await readServerPollCapacity(f.sql, owner as string, false), { status: "unavailable" }); assert.equal(f.calls.length, 0) + } + const invalid = fixture(); assert.deepEqual(await readServerPollCapacity(invalid.sql, "owner", "true" as unknown as boolean), { status: "unavailable" }); assert.equal(invalid.calls.length, 0) + const unicode = fixture(); assert.equal((await readServerPollCapacity(unicode.sql, "Оператор😀", false)).status, "observed"); assert.deepEqual(unicode.calls[0].values, ["owner:Оператор😀"]) +}) + +test("counter triples reject malformed canonical bigint values, partial NULL, mismatch, excessive limits and owner greater than global", async () => { + const invalid = [undefined, null, true, false, {}, [], 1n, NaN, Infinity, -1, -0, 1.5, Number.MAX_SAFE_INTEGER + 1, "", " 0", "0 ", "00", "01", "+1", "-0", "1.0", "1e0", "0x1", "9007199254740992"] + for (const field of ["global_admissions", "global_reserved_rows", "global_reserved_bytes", "owner_admissions", "owner_reserved_rows", "owner_reserved_bytes","global_compacted_admissions","global_archive_bytes","owner_compacted_admissions","owner_archive_bytes"]) + for (const value of invalid) assert.deepEqual(await readServerPollCapacity(fixture([{ ...row(), [field]: value }]).sql, "owner", true), { status: "unavailable" }, field + ":" + String(value)) + for (const candidate of [row(9, 10), row(8, 7), row(0, 129), { ...row(), owner_reserved_rows: "0" }, { ...row(), global_reserved_bytes: "0" }, { ...row(null), owner_reserved_bytes: "0" }]) + assert.deepEqual(await readServerPollCapacity(fixture([candidate]).sql, "owner", true), { status: "unavailable" }) +}) + +function compactRow(ownerHeavy:number,ownerCompact:number,globalHeavy=ownerHeavy,globalCompact=ownerCompact,encoded=59075){ + return {...row(ownerHeavy,globalHeavy),owner_compacted_admissions:String(ownerCompact),owner_archive_bytes:String(ownerCompact*encoded), + owner_reserved_rows:String(ownerHeavy*4019+ownerCompact*2),owner_reserved_bytes:String(ownerHeavy*8421376+ownerCompact*(32768+encoded)), + global_compacted_admissions:String(globalCompact),global_archive_bytes:String(globalCompact*encoded), + global_reserved_rows:String(globalHeavy*4019+globalCompact*2),global_reserved_bytes:String(globalHeavy*8421376+globalCompact*(32768+encoded))} +} + +test("336 retained histories remain paid while DTO used counts heavy slots and remaining obeys all caps",async()=>{ + const result=await readServerPollCapacity(fixture([compactRow(1,336)]).sql,"owner",true) + assert.deepEqual(result,{status:"observed",owner:{used:1,limit:8,remaining:3},global:{exhausted:false,usage:{used:1,limit:128,remaining:123}},retentionDays:30}) +}) + +test("largest permitted archives still allow one new poll after336 histories; shared contract accepts the byte-limited remainder",async()=>{ + const before=await readServerPollCapacity(fixture([compactRow(0,336,0,336,131072)]).sql,"owner",true) + assert(before.status==="observed");assert.equal(before.owner.remaining,1) + assert.deepEqual(mcpPollStorageCapacitySchema.parse(before),before) + const occupied=await readServerPollCapacity(fixture([compactRow(1,336,1,336,131072)]).sql,"owner",true) + assert(occupied.status==="observed");assert.equal(occupied.owner.used,1);assert.equal(occupied.owner.remaining,0) + assert.deepEqual(mcpPollStorageCapacitySchema.parse(occupied),occupied) + assert.equal(mcpPollStorageCapacitySchema.safeParse({...occupied,owner:{used:8,limit:8,remaining:1}}).success,false) +}) + +test("byte-full retained history cannot report available admission despite unused heavy slots",async()=>{ + const ownerFull=await readServerPollCapacity(fixture([compactRow(0,2000,0,2000,40)]).sql,"owner",false) + assert(ownerFull.status==="observed");assert.equal(ownerFull.owner.used,0);assert.equal(ownerFull.owner.remaining,0) + assert.equal(ownerFull.global.exhausted,false);assert.equal(ownerFull.global.usage,null) + const globalFull=await readServerPollCapacity(fixture([compactRow(0,0,0,32600,40)]).sql,"owner",false) + assert(globalFull.status==="observed");assert.equal(globalFull.global.exhausted,true);assert.equal(globalFull.global.usage,null) +}) + +test("missing 0041 fields, malformed archive accounting and owner/global mismatch fail unavailable",async()=>{ + const legacy={...row()};delete (legacy as Partial).global_compacted_admissions + for(const value of [legacy,{...row(),owner_compacted_admissions:null},{...row(),global_archive_bytes:"1"}, + compactRow(0,1,0,1,39),compactRow(0,1,0,1,131073),compactRow(0,2,0,1), + {...compactRow(0,1),owner_reserved_rows:"0"},{...compactRow(0,1),global_reserved_bytes:"0"}]) + assert.deepEqual(await readServerPollCapacity(fixture([value]).sql,"owner",true),{status:"unavailable"}) +}) + +test("canonical PostgreSQL bigint strings and safe integer numbers are accepted; retention current1..365 only", async () => { + const numeric = Object.fromEntries(Object.entries(row()).map(([key, value]) => [key, Number(value)])) + assert.equal((await readServerPollCapacity(fixture([numeric]).sql, "owner", true)).status, "observed") + for (const days of [1, "1", 365, "365"]) { + const result = await readServerPollCapacity(fixture([{ ...row(), retention_days: days }]).sql, "owner", false) + assert(result.status === "observed"); assert.equal(result.retentionDays, Number(days)) + } + for (const days of [0, 366, -1, 1.5, "01", null, undefined]) assert.deepEqual(await readServerPollCapacity(fixture([{ ...row(), retention_days: days }]).sql, "owner", false), { status: "unavailable" }) +}) + +test("returned DTO is deeply frozen and independent of later SQL row/result mutations", async () => { + const original = row(), rows = [original], f = fixture(rows), result = await readServerPollCapacity(f.sql, "owner", true) + assert(result.status === "observed"); const expected = structuredClone(result) + Object.assign(original, row(8, 128), { retention_days: 365 }); rows.splice(0) + assert.deepEqual(result, expected); assert(Object.isFrozen(result)); assert(Object.isFrozen(result.owner)); assert(Object.isFrozen(result.global)); assert(Object.isFrozen(result.global.usage)) + assert.throws(() => { (result.owner as { used: number }).used = 7 }) + assert(Object.isFrozen(await readServerPollCapacity(fixture([]).sql, "owner", false))) +}) diff --git a/backend/src/services/server-poll-capacity.ts b/backend/src/services/server-poll-capacity.ts new file mode 100644 index 00000000..2eb308b2 --- /dev/null +++ b/backend/src/services/server-poll-capacity.ts @@ -0,0 +1,63 @@ +import { pollStorageUsageRemaining, type PollStorageUsage } from "./server-poll-storage-model.js" + +export type ServerPollCapacity = + | Readonly<{ status: "unavailable" }> + | Readonly<{ status: "observed"; owner: Readonly<{ used: number; limit: 8; remaining: number }>; + global: Readonly<{ exhausted: boolean; usage: Readonly<{ used: number; limit: 128; remaining: number }> | null }>; + retentionDays: number }> +export interface ServerPollCapacitySql { query(text: string, values: unknown[]): Promise<{ rows: Record[] }> } + +// A single bounded MVCC snapshot. No admission enumeration, maintenance, locks +// or claim/readiness mutation is part of this observation. +const QUERY = `SELECT s.requests_retention_days AS retention_days, + g.admissions AS global_admissions,g.compacted_admissions AS global_compacted_admissions,g.archive_bytes AS global_archive_bytes,g.reserved_rows AS global_reserved_rows,g.reserved_bytes AS global_reserved_bytes, + o.admissions AS owner_admissions,o.compacted_admissions AS owner_compacted_admissions,o.archive_bytes AS owner_archive_bytes,o.reserved_rows AS owner_reserved_rows,o.reserved_bytes AS owner_reserved_bytes +FROM public.mcp_settings s +LEFT JOIN public.server_poll_storage_usage g ON g.scope='global' +LEFT JOIN public.server_poll_storage_usage o ON o.scope=$1 +WHERE s.id=1 LIMIT 2` +const unavailable: ServerPollCapacity = Object.freeze({ status: "unavailable" }) +function integer(value: unknown): number | undefined { + if (typeof value === "number") return Number.isSafeInteger(value) && value >= 0 && !Object.is(value, -0) ? value : undefined + if (typeof value !== "string" || !/^(?:0|[1-9][0-9]*)$/.test(value) || value.length > 16) return undefined + const parsed = Number(value) + return Number.isSafeInteger(parsed) ? parsed : undefined +} +function usage(admissions: unknown, rows: unknown, bytes: unknown, compacted:unknown, archive:unknown, scope:"global"|"owner") { + const used=integer(admissions),reservedRows=integer(rows),reservedBytes=integer(bytes),compactedAttempts=integer(compacted),archiveBytes=integer(archive) + if(used===undefined||reservedRows===undefined||reservedBytes===undefined||compactedAttempts===undefined||archiveBytes===undefined)return undefined + const value:PollStorageUsage={attempts:used,rows:reservedRows,bytes:reservedBytes,compactedAttempts,archiveBytes} + const remaining=pollStorageUsageRemaining(value,scope) + return remaining===undefined?undefined:{value,remaining} +} + +/** Read-only heavy reservation availability with paid retained history. Used + * counts heavy slots; remaining also accounts for archive bytes/rows. This + * observation does not verify archive MACs and is never an admission promise. + * Missing or + * inconsistent accounting is unavailable rather than a fabricated free slot. + * Non-admin callers receive no global usage numbers or other owner identities. */ +export async function readServerPollCapacity(sql: ServerPollCapacitySql, ownerId: string, admin: boolean): Promise { + if (typeof ownerId !== "string" || ownerId.trim() !== ownerId || !ownerId.length || [...ownerId].length > 200 + || /[\u0000-\u001f\u007f-\u009f]/.test(ownerId) || typeof admin !== "boolean") return unavailable + try { + const result = await sql.query(QUERY, ["owner:" + ownerId]) + if (!Array.isArray(result.rows) || result.rows.length !== 1) return unavailable + const row = result.rows[0] + if (!row || typeof row !== "object" || Array.isArray(row)) return unavailable + // Capture primitive fields synchronously. DTOs never alias mutable driver + // rows and are frozen independently of the SQL adapter's result lifetime. + const retentionDays = integer(row.retention_days) + const globalUsed=usage(row.global_admissions,row.global_reserved_rows,row.global_reserved_bytes,row.global_compacted_admissions,row.global_archive_bytes,"global") + const ownerFields=[row.owner_admissions,row.owner_reserved_rows,row.owner_reserved_bytes,row.owner_compacted_admissions,row.owner_archive_bytes] + const ownerUsed=ownerFields.every(value=>value===null)?usage(0,0,0,0,0,"owner"):usage(...ownerFields as [unknown,unknown,unknown,unknown,unknown],"owner") + if(retentionDays===undefined||retentionDays<1||retentionDays>365||!globalUsed||!ownerUsed)return unavailable + for(const field of ["attempts","rows","bytes","compactedAttempts","archiveBytes"] as const) + if(ownerUsed.value[field]! > globalUsed.value[field]!)return unavailable + const otherCount=globalUsed.value.compactedAttempts!-ownerUsed.value.compactedAttempts!,otherBytes=globalUsed.value.archiveBytes!-ownerUsed.value.archiveBytes! + if(otherBytesotherCount*131072)return unavailable + return Object.freeze({status:"observed",owner:Object.freeze({used:ownerUsed.value.attempts,limit:8,remaining:ownerUsed.remaining}), + global:Object.freeze({exhausted:globalUsed.remaining===0, + usage:admin?Object.freeze({used:globalUsed.value.attempts,limit:128,remaining:globalUsed.remaining}):null}),retentionDays}) + } catch { return unavailable } +} diff --git a/backend/src/services/server-poll-cost-diagnostics.test.ts b/backend/src/services/server-poll-cost-diagnostics.test.ts new file mode 100644 index 00000000..bae046e0 --- /dev/null +++ b/backend/src/services/server-poll-cost-diagnostics.test.ts @@ -0,0 +1,114 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { installServerPollCostDiagnostics } from "./server-poll-cost-diagnostics.js" + +test("promise identity, receiver, arguments and exact descriptor survive instrumentation", async () => { + const result = Promise.resolve({ secret: "result" }), values = ["private-parameter"] + const config = { text: "SELECT * FROM server_poll_acks WHERE secret=$1", values } + const prototype = { query(this: unknown, ...args: unknown[]) { assert.equal(this, client); assert.deepEqual(args, [config]); return result } } + const descriptor = Object.getOwnPropertyDescriptor(prototype, "query") + const client = Object.create(prototype) as typeof prototype + let time = 10 + const diagnostics = installServerPollCostDiagnostics({ prototype, now: () => time }) + try { + assert.equal(client.query(config), result); time = 17; await result + assert.deepEqual(diagnostics.snapshot().categories, [{ category: "server_poll_acks:SELECT", count: 1, totalMs: 7, maxMs: 7, errors: 0 }]) + assert(!JSON.stringify(diagnostics.snapshot()).includes("private")) + const snapshot = diagnostics.snapshot(); snapshot.categories[0].count = 99 + assert.equal(diagnostics.snapshot().categories[0].count, 1) + } finally { diagnostics.restore() } + assert.deepEqual(Object.getOwnPropertyDescriptor(prototype, "query"), descriptor) +}) + +test("callback receiver, all arguments, return and original query return are preserved", () => { + const receiver = {}, error = new Error("private-error"), result = { secret: "private-result" }, queryReturn = {} + const callbackReturn = {}, params = ["private-param"] + const prototype = { query(this: unknown, ...args: unknown[]) { + assert.equal(this, client); assert.equal(args[0], "INSERT INTO server_poll_intents VALUES($1)"); assert.equal(args[1], params) + assert.equal(Reflect.apply(args[2] as (...values: unknown[]) => unknown, receiver, [error, result, 3]), callbackReturn) + return queryReturn + } } + const client = Object.create(prototype) as typeof prototype + const diagnostics = installServerPollCostDiagnostics({ prototype }) + try { + const callback = function(this: unknown, ...args: unknown[]) { assert.equal(this, receiver); assert.deepEqual(args, [error, result, 3]); return callbackReturn } + assert.equal(client.query("INSERT INTO server_poll_intents VALUES($1)", params, callback), queryReturn) + const snapshot = diagnostics.snapshot(); assert.equal(snapshot.categories[0].errors, 1) + assert(!JSON.stringify(snapshot).includes("private")) + } finally { diagnostics.restore() } +}) + +test("rejected promise and synchronous throw keep original identities and are counted once", async () => { + const failure = new Error("do-not-log"), promise = Promise.reject(failure) + let sync = false + const prototype = { query() { if (sync) throw failure; return promise } } + const diagnostics = installServerPollCostDiagnostics({ prototype }) + try { + assert.equal(prototype.query(), promise); await assert.rejects(promise, error => error === failure) + sync = true; assert.throws(() => prototype.query(), error => error === failure) + assert.equal(diagnostics.snapshot().categories[0].count, 2); assert.equal(diagnostics.snapshot().categories[0].errors, 2) + assert(!JSON.stringify(diagnostics.snapshot()).includes("do-not-log")) + } finally { diagnostics.restore() } +}) + +test("deferred callbacks aggregate completion costs and preserve receiver", () => { + let pending: ((...values: unknown[]) => unknown) | undefined, time = 0 + const returned = {}, receiver = {} + const prototype = { query(...args: unknown[]) { pending = args[1] as typeof pending; return returned } } + const diagnostics = installServerPollCostDiagnostics({ prototype, now: () => time }) + try { + const callback = function(this: unknown, error: unknown, value: unknown) { assert.equal(this, receiver); assert.equal(error, null); return value } + assert.equal(prototype.query("UPDATE servers SET name=$1", callback), returned) + assert.equal(diagnostics.snapshot().categories.length, 0) + time = 5; assert.equal(Reflect.apply(pending!, receiver, [null, "first"]), "first") + time = 10; prototype.query("UPDATE servers SET name=$1", callback) + time = 13; assert.equal(Reflect.apply(pending!, receiver, [null, "second"]), "second") + assert.deepEqual(diagnostics.snapshot().categories, [{ category: "servers:UPDATE", count: 2, totalMs: 8, maxMs: 5, errors: 0 }]) + } finally { diagnostics.restore() } +}) + +test("callback throw propagates without double counting or changing error", () => { + const failure = new Error("callback"), prototype = { query(...args: unknown[]) { return Reflect.apply(args[1] as (...values: unknown[]) => unknown, null, [null, {}]) } } + const diagnostics = installServerPollCostDiagnostics({ prototype }) + try { + assert.throws(() => prototype.query("SELECT 1", () => { throw failure }), error => error === failure) + assert.equal(diagnostics.snapshot().categories[0].count, 1) + assert.equal(diagnostics.snapshot().categories[0].errors, 0) + } finally { diagnostics.restore() } +}) + +test("bounded categories use only fixed tags, including overflow", () => { + const prototype = { query(...args: unknown[]) { return Reflect.apply(args[1] as (...values: unknown[]) => unknown, null, [null, {}]) } } + const diagnostics = installServerPollCostDiagnostics({ prototype }) + try { + const tables = ["server_poll_storage_admissions", "server_poll_storage_usage", "server_poll_archives", "server_poll_executions", "server_poll_intents", "server_poll_acks", "server_poll_receipts", "server_poll_quarantines", "mcp_operations", "mcp_operation_steps", "mcp_plans", "mcp_connections", "mcp_credentials", "mcp_settings"] + for (const table of tables) for (const command of ["SELECT", "INSERT", "UPDATE", "DELETE", "BEGIN", "COMMIT", "ROLLBACK", "SET", "WITH", "SHOW"]) { + // Callback completion is synchronous for this test double. + prototype.query(`${command} ${table} private-name`, () => {}) + } + const categories = diagnostics.snapshot().categories + assert.equal(categories.length, 128) + assert.equal(categories.reduce((sum, item) => sum + item.count, 0), 140) + assert.equal(categories.find(item => item.category === "overflow:OTHER")?.count, 13) + assert(!JSON.stringify(categories).includes("private")) + } finally { diagnostics.restore() } +}) + +test("opaque config getters and unsupported return values remain untouched", () => { + const config = { get text(): string { throw Error("must not inspect getter") } }, returned = { opaque: true } + const prototype = { query(...args: unknown[]) { assert.equal(args[0], config); return returned } } + const diagnostics = installServerPollCostDiagnostics({ prototype, now: () => { throw Error("clock") } }) + try { + assert.equal(prototype.query(config), returned) + assert.deepEqual(diagnostics.snapshot().categories, []) + } finally { diagnostics.restore() } +}) + +test("current authority and portal tables have fixed diagnostic categories", () => { + const prototype = { query(...args: unknown[]) { return Reflect.apply(args[1] as (...values: unknown[]) => unknown, null, [null, {}]) } } + const diagnostics = installServerPollCostDiagnostics({ prototype }) + try { + for (const table of ["mcp_grants", "mcp_protections", "mcp_owner_revocations"]) prototype.query(`SELECT * FROM ${table}`, () => {}) + assert.deepEqual(diagnostics.snapshot().categories.map(row => row.category), ["mcp_grants:SELECT", "mcp_owner_revocations:SELECT", "mcp_protections:SELECT"]) + } finally { diagnostics.restore() } +}) diff --git a/backend/src/services/server-poll-cost-diagnostics.ts b/backend/src/services/server-poll-cost-diagnostics.ts new file mode 100644 index 00000000..ca7eb9ba --- /dev/null +++ b/backend/src/services/server-poll-cost-diagnostics.ts @@ -0,0 +1,69 @@ +/** Opt-in owned-run diagnostics. Wrapping query is not unwrapped acceptance. */ +import pg from "pg" +import { performance } from "node:perf_hooks" + +const TABLES = ["server_poll_storage_admissions", "server_poll_storage_usage", "server_poll_archives", + "server_poll_executions", "server_poll_intents", "server_poll_acks", "server_poll_receipts", "server_poll_quarantines", + "mcp_operations", "mcp_operation_steps", "mcp_plans", "mcp_connections", "mcp_credentials", "mcp_settings", + "mcp_grants", "mcp_protections", "mcp_owner_revocations", + "operation_config_locks", "settings", "servers", "server_interfaces", "server_ip_addresses"] as const +const TABLE_PATTERNS = TABLES.map(name => ({ name, pattern: new RegExp(`\\b${name}\\b`, "i") })) +const COMMANDS = new Set(["SELECT", "INSERT", "UPDATE", "DELETE", "BEGIN", "COMMIT", "ROLLBACK", "SET", "WITH", "SHOW"]) +export interface ServerPollQueryCost { category: string; count: number; totalMs: number; maxMs: number; errors: number } + +function category(args: unknown[]): string { + // Inspect only a bounded SQL prefix, never values, results or error text. + const first = args[0] + const descriptor = first && typeof first === "object" ? Object.getOwnPropertyDescriptor(first, "text") : undefined + const sql = typeof first === "string" ? first.slice(0, 65536) : typeof descriptor?.value === "string" ? descriptor.value.slice(0, 65536) : "" + const token = /^\s*([a-z]+)/i.exec(sql)?.[1]?.toUpperCase() ?? "OTHER" + const command = COMMANDS.has(token) ? token : "OTHER" + const table = TABLE_PATTERNS.find(entry => entry.pattern.test(sql))?.name ?? "other" + return `${table}:${command}` +} + +export function installServerPollCostDiagnostics(options: { prototype?: object; now?: () => number } = {}) { + const prototype = options.prototype ?? pg.Client.prototype + const original = Object.getOwnPropertyDescriptor(prototype, "query") + if (!original || typeof original.value !== "function") throw new Error("Expected an own query method") + const query = original.value as (this: unknown, ...args: unknown[]) => unknown + const now = options.now ?? (() => performance.now()) + const costs = new Map() + // Instrumentation must never replace a query's success or failure. + const clock = () => { try { const value = now(); return Number.isFinite(value) ? value : 0 } catch { return 0 } } + function wrapped(this: unknown, ...args: unknown[]) { + const started = clock() + let key = "other:OTHER" + try { key = category(args) } catch { /* Exotic query objects remain untouched. */ } + let completed = false + const finish = (error: boolean) => { + if (completed) return + completed = true + const elapsed = Math.max(0, clock() - started) + if (!costs.has(key) && costs.size >= 127) key = "overflow:OTHER" + const cost = costs.get(key) ?? { category: key, count: 0, totalMs: 0, maxMs: 0, errors: 0 } + cost.count++; cost.totalMs += elapsed; cost.maxMs = Math.max(cost.maxMs, elapsed); cost.errors += Number(error) + costs.set(key, cost) + } + const callback = args[args.length - 1] + const hasCallback = typeof callback === "function" + if (hasCallback) args[args.length - 1] = function(this: unknown, ...values: unknown[]) { + finish(Boolean(values[0])) + return Reflect.apply(callback, this, values) + } + try { + const returned = Reflect.apply(query, this, args) + if (!hasCallback && returned instanceof Promise) { + // Observe without returning a chained promise or creating an unhandled rejection. + void Promise.prototype.then.call(returned, () => finish(false), () => finish(true)).catch(() => {}) + } + return returned + } catch (error) { finish(true); throw error } + } + Object.defineProperty(prototype, "query", { ...original, value: wrapped }) + return { + snapshot: () => ({ diagnosticOnly: true as const, wrappedPgQuery: true as const, + categories: [...costs.values()].map(value => ({ ...value })).sort((a, b) => a.category.localeCompare(b.category)) }), + restore: () => { Object.defineProperty(prototype, "query", original) }, + } +} diff --git a/backend/src/services/server-poll-durable-session.sql.test.ts b/backend/src/services/server-poll-durable-session.sql.test.ts new file mode 100644 index 00000000..91791c32 --- /dev/null +++ b/backend/src/services/server-poll-durable-session.sql.test.ts @@ -0,0 +1,246 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { AsyncLocalStorage } from "node:async_hooks" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, resolve } from "node:path" +import { readFile } from "node:fs/promises" +import { gzipSync, gunzipSync } from "node:zlib" +import type { DurableServerPollSessionInput } from "./server-poll-durable-session.js" + +// Never load the configured pool before admitting a private owned DB. +assert.equal(process.env.MMAPP_OWN_SERVER_POLL_DURABLE,"true") +const endpoint=new URL(process.env.DATABASE_URL??"") +assert.equal(endpoint.hostname,"127.0.0.1");assert.equal(endpoint.pathname,"/postgres") +assert(["postgres:","postgresql:"].includes(endpoint.protocol)) +assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const {pool,closePool}=await import("../db/index.js") +const {applySqlMigrations}=await import("../db/migrate.js") +const {createStorageDurableServerPollSession:createDurableServerPollSession}=await import("./server-poll-durable-session.js") +const {SERVER_PARTITION_WRITER_LOCK}=await import("./server-partition-maintenance-lease.js") +const {withPartitionWrite,PartitionWriteError}=await import("./partition-write-transaction.js") +const {ensurePartitionFor,PARTITION_SPECS}=await import("../db/partitions.js") +const {compactCompletedServerPollStorage}=await import("./server-poll-terminal-compaction.js") +const {collectExpiredServerPollStorage}=await import("./server-poll-storage-retention.js") +const {tickMcpWorkers}=await import("../modules/mcp/workers.js") +const {ServerPollJournalOutcomeUnknown}=await import("./server-poll-journal.js") +const {Client}=await import("pg") +const security=await import("../modules/mcp/security.js") +let serverId:number|undefined,requests=0,releasedNative=false,writerPid=0 +let releaseNative!:()=>void,firstNative!:()=>void +const nativeGate=new Promise(resolve=>{releaseNative=()=>{releasedNative=true;resolve()}}) +const started=new Promise(resolve=>{firstNative=resolve}) +const paths:string[]=[] +const listener=createServer(async(req,res)=>{ + requests++;paths.push(req.url??"") + // Keep the lease observable without exhausting RouterOS's two HTTP sockets. + if(requests===1){firstNative();await nativeGate} + res.setHeader("content-type","application/json") + const value=req.url==="/rest/system/identity"?{name:"poll-owned-renamed"}:req.url==="/rest/system/resource"?{version:"7.synthetic","board-name":"owned","uptime":"1h","cpu-load":"3","free-memory":"1024","total-memory":"2048"}:req.url==="/rest/interface"?[{name:"owned-interface",type:"ether"}]:req.url==="/rest/ip/address"?[{address:"192.0.2.1/24",interface:"owned-interface"}]:null + res.statusCode=value===null?404:200;res.end(JSON.stringify(value)) +}) +const waitFor=async(label:string,inspect:()=>Promise)=>{const deadline=Date.now()+10000;while(Date.now()setTimeout(resolve,10))};assert.fail("Timed out: "+label)} +try{ + await applySqlMigrations(pool) + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n,MIGRATION_MANIFEST.length) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relname=ANY($1) AND c.relkind='r'",[["server_poll_executions","server_poll_intents","server_poll_acks","server_poll_receipts","server_poll_quarantines"]])).rows[0].n,5) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM pg_trigger t JOIN pg_class c ON c.oid=t.tgrelid WHERE t.tgname='server_partition_writer_guard' AND t.tgenabled='A' AND c.relkind='p'")).rows[0].n,PARTITION_SPECS.length) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n,0,"Fresh isolated database required; never remove foreign fixtures") + await pool.query(`CREATE TABLE owned_poll_journal_pid(relation text,pid int,xid text,exclusive_writer boolean); + CREATE FUNCTION owned_poll_journal_observe() RETURNS trigger LANGUAGE plpgsql AS $$BEGIN + INSERT INTO public.owned_poll_journal_pid SELECT TG_TABLE_NAME,pg_backend_pid(),pg_current_xact_id()::text, + EXISTS(SELECT 1 FROM pg_catalog.pg_locks WHERE pid=pg_backend_pid() AND locktype='advisory' AND classid=0 + AND objid=8723110 AND objsubid=1 AND mode='ExclusiveLock' AND granted); + RETURN NULL; + END$$;`) + for(const name of ["server_poll_storage_admissions","server_poll_storage_usage","server_poll_executions","server_poll_intents","server_poll_acks","server_poll_receipts","server_poll_quarantines"]){ + await pool.query(`CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR UPDATE OR DELETE ON public.${name} + FOR EACH STATEMENT EXECUTE FUNCTION public.server_partition_writer_guard(); + ALTER TABLE public.${name} ENABLE ALWAYS TRIGGER candidate_application_statement_guard; + CREATE TRIGGER owned_poll_journal_observe AFTER INSERT OR UPDATE OR DELETE ON public.${name} + FOR EACH STATEMENT EXECUTE FUNCTION public.owned_poll_journal_observe();`) + } + await new Promise((resolve,reject)=>{listener.once("error",reject);listener.listen(0,"127.0.0.1",()=>{listener.off("error",reject);resolve()})}) + const port=(listener.address()as{port:number}).port + serverId=Number((await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES('poll-owned-before','127.0.0.1',$1,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id",[port])).rows[0].id) + const oldAt=new Date(Date.now()-86400000) + await ensurePartitionFor(pool,"server_snapshots","week",oldAt) + await ensurePartitionFor(pool,"server_snapshots","week",new Date()) + const expired=await ensurePartitionFor(pool,"traffic_samples","week",new Date("2000-01-03T00:00:00Z")) + const oldId=Number((await pool.query("INSERT INTO server_snapshots(server_id,polled_at,status,raw_interfaces,raw_ip_addresses) VALUES($1,$2,'offline','[]'::jsonb,'[]'::jsonb) RETURNING id",[serverId,oldAt])).rows[0].id) + const issuedAt=Date.now()-1000,expiresAt=Date.now()+600000,ownerId="owned-local-poll-"+randomUUID(),receiptId=randomUUID(),executionId=randomUUID() + const identity={kind:"rest"as const,authentication:"local"as const,ownerId,executionId,requestId:randomUUID(),issuedAt,expiresAt} + const generation=await security.accessGeneration(),initialEpoch=(await pool.query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1",[serverId])).rows[0].routing_endpoint_epoch + const connectionId=randomUUID(),expiredLeaseId=randomUUID() + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation) VALUES($1,'owned-poll-worker',$2,'[]','{}',$3)",[connectionId,ownerId,generation]) + await pool.query("INSERT INTO mcp_read_leases(id,connection_id,expires_at) VALUES($1,$2,now()-interval '1 minute')",[expiredLeaseId,connectionId]) + const hasExpiredLease=async()=>(await pool.query("SELECT id FROM mcp_read_leases WHERE id=$1",[expiredLeaseId])).rowCount===1 + const authorityProof=await security.privateSecretFingerprint(security.fingerprint({ownerId,issuedAt,expiresAt,generation,serverId,initialEpoch,port,permissions:["mm:servers:write"],global:true}),"owned-full-poll-authority-v1") + let revoked=false,authorityReads=0 + const sessionRequest={serverId,receiptId} + const sessionInput:DurableServerPollSessionInput={request:sessionRequest,identity,authorize:async(request,required,signal)=>{ + signal?.throwIfAborted();authorityReads++ + assert(!revoked&&Date.now()>=issuedAt&&Date.now()Number(row.id)),[serverId]) + assert.equal(rows[0].host,"127.0.0.1");assert.equal(rows[0].port,port);assert.equal(rows[0].enabled,true);assert.equal(rows[0].routing_endpoint_epoch,initialEpoch) + assert.deepEqual(rows[0].catalogue.map(Number),[serverId]);assert(required.every(id=>id===serverId));assert.equal(rows[0].protections,0,"Fixture refuses any protection rather than bypassing it") + // Every immutable grant component is freshly checked above; reuse only + // its keyed identifier, never a cached admission/revocation decision. + return {global:true,serverIds:[serverId!],authorityProof} + }} + const session=createDurableServerPollSession(sessionInput) + const plan=await session.prepare() + console.log("Full durable poll prepared:",JSON.stringify({maintenanceEffects:plan.maintenance.length,parents:PARTITION_SPECS.length})) + assert.equal(requests,0) + assert.equal(new Set(plan.maintenance.map(effect=>"parent"in effect?effect.parent:"")).size,PARTITION_SPECS.length) + assert(plan.maintenance.some(effect=>effect.kind==="partition.drop"&&effect.name===expired)) + const pending=session.execute(plan);void pending.catch(()=>{}) + await Promise.race([started,pending.then(()=>assert.fail("Native request must occur before completion"))]) + const {rows:[holder]}=await pool.query(`SELECT pid FROM pg_catalog.pg_locks WHERE locktype='advisory' AND classid=0 + AND objid=$1::oid AND objsubid=1 AND mode='ExclusiveLock' AND granted`,[SERVER_PARTITION_WRITER_LOCK]) + assert(Number.isInteger(holder?.pid)) + const prefix=(await pool.query("SELECT relation,pid,xid,exclusive_writer FROM owned_poll_journal_pid")).rows + assert(prefix.some(row=>row.relation==="server_poll_executions")) + assert(prefix.some(row=>row.relation==="server_poll_intents")) + assert(prefix.every(row=>row.pid===holder.pid&&row.exclusive_writer===true)) + const anchor=prefix.find(row=>row.relation==="server_poll_executions")! + assert(prefix.some(row=>row.relation==="server_poll_storage_admissions"&&row.xid!==anchor.xid),"Reservation and consume/anchor are independently committed") + assert(prefix.some(row=>row.relation==="server_poll_storage_admissions"&&row.xid===anchor.xid),"Consume and anchor share one transaction") + const background = new AsyncLocalStorage() + const workerQuery = Client.prototype.query + let storageLockAttempts=0 + const backgroundAdmissions:boolean[]=[] + try { + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + const request=args[0] as string|{text?:string;values?:unknown[]} + const text=typeof request==="string"?request:request.text + const values=typeof request==="string"?args[1] as unknown[]|undefined:request.values + const result=Reflect.apply(workerQuery,this,args) + if(!background.getStore())return result + if(text==="SELECT pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0))")storageLockAttempts++ + if(values?.[0]===SERVER_PARTITION_WRITER_LOCK&&( + text==="SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired"|| + text==="SELECT pg_catalog.pg_try_advisory_xact_lock_shared($1::bigint) AS acquired")){ + return Promise.resolve(result).then((observation:{rows:{acquired:boolean}[]})=>{ + backgroundAdmissions.push(observation.rows[0].acquired);return observation + }) + } + return result + } as typeof Client.prototype.query + await background.run(true,async()=>{ + await tickMcpWorkers() + assert(await hasExpiredLease(),"Expired lease remains while background admission is deferred") + assert.equal((await compactCompletedServerPollStorage()).compacted,0) + assert.equal((await collectExpiredServerPollStorage()).collected,0) + }) + } finally { Client.prototype.query=workerQuery } + assert(backgroundAdmissions.length>=2&&backgroundAdmissions.every(acquired=>acquired===false),"Actual maintenance writer admissions must be refused") + assert.equal(storageLockAttempts,0,"Background maintenance must defer before requesting the storage lock") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM pg_locks WHERE locktype='advisory' AND pid=$1 AND objid=$2::oid AND mode='ExclusiveLock' AND granted",[holder.pid,SERVER_PARTITION_WRITER_LOCK])).rows[0].n,1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM pg_locks WHERE locktype='advisory' AND objid=$1::oid AND mode='ShareLock' AND NOT granted",[SERVER_PARTITION_WRITER_LOCK])).rows[0].n,0) + // Actual ordinary writer arrives while FULL poll holds the exclusive lease. + // Inspect lock evidence before releasing HTTP; no target relation is touched. + const controller=new AbortController() + const blocked=withPartitionWrite(async owner=>{await owner.client.query("INSERT INTO traffic_samples(server_id,interface_name,sampled_at,rx_bytes) VALUES($1,'must-not-dispatch',now(),1)",[serverId])},{signal:controller.signal,connect:async()=>{const client=await pool.connect();writerPid=Number((await client.query("SELECT pg_backend_pid() AS pid")).rows[0].pid);return client}}) + void blocked.catch(()=>{}) + try{ + await waitFor("ordinary writer waiting before target SQL",async()=>writerPid>0&&(await pool.query("SELECT count(*)::int AS n FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND objid=$2 AND mode='ShareLock' AND NOT granted",[writerPid,SERVER_PARTITION_WRITER_LOCK])).rows[0].n===1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM pg_locks l JOIN pg_class c ON c.oid=l.relation WHERE l.pid=$1 AND l.locktype='relation' AND (c.relname='traffic_samples' OR c.relname LIKE 'traffic_samples_w_%')",[writerPid])).rows[0].n,0) + }finally{controller.abort();await assert.rejects(blocked,(error:unknown)=>error instanceof PartitionWriteError&&!error.outcomeUnknown);releaseNative()} + const receipt=await pending + await tickMcpWorkers() + assert.equal(await hasExpiredLease(),false,"The next tick collects the expired lease after POLL releases the writer lock") + assert.equal((await compactCompletedServerPollStorage()).compacted,0) + assert.equal((await collectExpiredServerPollStorage()).collected,0) + assert.equal(receipt.complete,true);assert.equal(receipt.maintenanceComplete,true);assert.equal(receipt.nativeReads,4) + assert.equal(receipt.observation,"online") + assert.equal(receipt.completedPublications,plan.maintenance.length+4) + assert.deepEqual([...paths].sort(),["/rest/interface","/rest/ip/address","/rest/system/identity","/rest/system/resource"].sort()) + assert.equal((await pool.query("SELECT name FROM servers WHERE id=$1",[serverId])).rows[0].name,"poll-owned-renamed") + const old=(await pool.query("SELECT raw_interfaces,raw_ip_addresses FROM server_snapshots WHERE id=$1",[oldId])).rows[0] + assert.equal(old.raw_interfaces,null);assert.equal(old.raw_ip_addresses,null) + assert.equal((await pool.query("SELECT to_regclass($1) AS name",["public."+expired])).rows[0].name,null) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1",[receiptId])).rows[0].n,receipt.completedPublications+4) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_acks WHERE receipt_id=$1",[receiptId])).rows[0].n,receipt.completedPublications+4) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_acks WHERE receipt_id=$1 AND stage LIKE 'native.%' AND outcome='confirmed'",[receiptId])).rows[0].n,4) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_receipts WHERE receipt_id=$1",[receiptId])).rows[0].n,1) + const fullJournal=(await pool.query("SELECT relation,pid,xid,exclusive_writer FROM owned_poll_journal_pid")).rows + assert(fullJournal.every(row=>row.pid===holder.pid&&row.exclusive_writer===true)) + assert(fullJournal.some(row=>row.relation==="server_poll_acks")&&fullJournal.some(row=>row.relation==="server_poll_receipts")) + assert(new Set(fullJournal.filter(row=>row.relation==="server_poll_intents").map(row=>row.xid)).size===receipt.completedPublications+4) + assert.deepEqual(await session.readExisting(plan),receipt) + // Measure a COMPLETE real journal, including ciphertext, proofs, timestamps, + // admission and every stage. This is sizing evidence, not an archive format + // or permission to delete/compact rows. Never print the captured payload. + const stored=(await pool.query(`SELECT jsonb_build_object( + 'admission',(SELECT to_jsonb(a) FROM server_poll_storage_admissions a WHERE receipt_id=$1), + 'execution',(SELECT to_jsonb(e) FROM server_poll_executions e WHERE receipt_id=$1), + 'intents',(SELECT jsonb_agg(to_jsonb(i) ORDER BY created_at,stage) FROM server_poll_intents i WHERE receipt_id=$1), + 'acks',(SELECT jsonb_agg(to_jsonb(a) ORDER BY created_at,stage) FROM server_poll_acks a WHERE receipt_id=$1), + 'receipt',(SELECT to_jsonb(r) FROM server_poll_receipts r WHERE receipt_id=$1), + 'quarantine',(SELECT to_jsonb(q) FROM server_poll_quarantines q WHERE receipt_id=$1)) AS value`,[receiptId])).rows[0].value + assert(stored.admission&&stored.execution&&stored.receipt);assert.equal(stored.quarantine,null) + assert.equal(stored.intents.length,receipt.completedPublications+4);assert.equal(stored.acks.length,stored.intents.length) + const journalRows=stored.intents.length+stored.acks.length+2 + assert.equal(stored.admission.used_rows,journalRows) + const fullBytes=Buffer.from(JSON.stringify(stored)),compressed=gzipSync(fullBytes,{level:6}) + assert.deepEqual(gunzipSync(compressed),fullBytes,"Sizing must retain every original field and byte") + console.log("Full durable poll journal sizing:",JSON.stringify({maintenanceEffects:plan.maintenance.length,journalRows,admissionRows:1, + chargedBytes:Number(stored.admission.used_bytes),fullJsonBytes:fullBytes.length,gzipBytes:compressed.length,gzipRoundTripExact:true, + sampleFits128KiB:compressed.length<=131072,identityKind:stored.admission.identity_kind,compactionPerformed:false, + archiveAuthenticationAccepted:false,historicalVerifierAccepted:false,productionCapacityAccepted:false})) + const lostRequest={serverId,receiptId:randomUUID()},lostIdentity={...identity,executionId:randomUUID(),requestId:randomUUID()} + const originalAuthority=sessionInput.authorize + const lostSession=createDurableServerPollSession({request:lostRequest,identity:lostIdentity,authorize:async(request,ids,signal)=>{ + assert.deepEqual(request,lostRequest) + return originalAuthority(sessionRequest,ids,signal) + }}) + const lostPlan=await lostSession.prepare(),originalQuery=Client.prototype.query + type IdentifiedClient=InstanceType&{processID?:number} + let reservedBackend:IdentifiedClient|undefined,acksLost=0 + try{ + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + const request=args[0] as {text?:string;values?:unknown[]} + if(request?.text?.startsWith("INSERT INTO server_poll_storage_admissions")&&request.values?.[0]===lostRequest.receiptId)reservedBackend=this + if(this===reservedBackend&&request?.text==="COMMIT"&&acksLost===0){ + return Promise.resolve(Reflect.apply(originalQuery,this,args)).then((result:{command:string})=>{ + assert.equal(result.command,"COMMIT");acksLost++;throw Error("owned real reservation COMMIT ACK lost") + }) + } + return Reflect.apply(originalQuery,this,args) + } as typeof Client.prototype.query + await assert.rejects(lostSession.execute(lostPlan),error=>error instanceof ServerPollJournalOutcomeUnknown&&error.automaticReplay===false&&!error.requiresDurableQuarantine) + }finally{Client.prototype.query=originalQuery} + assert.equal(acksLost,1);assert.equal(requests,4) + const {rows:[lostState]}=await pool.query(`SELECT + (SELECT count(*)::int FROM server_poll_storage_admissions WHERE receipt_id=$1) AS charged, + (SELECT count(*)::int FROM server_poll_executions WHERE receipt_id=$1) AS anchored, + (SELECT count(*)::int FROM server_poll_quarantines WHERE receipt_id=$1) AS quarantined`,[lostRequest.receiptId]) + assert.deepEqual(lostState,{charged:1,anchored:0,quarantined:1}) + const {rows:[quarantine]}=await pool.query("SELECT pid,exclusive_writer FROM owned_poll_journal_pid WHERE relation='server_poll_quarantines'") + assert(Number.isInteger(reservedBackend?.processID)) + assert.notEqual(quarantine.pid,reservedBackend?.processID);assert.equal(quarantine.exclusive_writer,false) + assert.equal(await lostSession.readExisting(lostPlan),undefined) + console.log("Guarded durable POLL journal acceptance:",JSON.stringify({sameHolder:true,independentCommits:true,concurrentMaintenanceNoEarlyChanges:true,workerErrors:0,backgroundStorageLockAttempts:storageLockAttempts,readExisting:true,realLostCommitAckNoReplay:true,independentQuarantine:true})) + sessionRequest.serverId++ + identity.ownerId="mutated-outer-input";identity.expiresAt=0 + sessionInput.authorize=async()=>{throw Error("Outer caller replaced authority provider")} + await assert.rejects(session.execute(plan),{code:"STATE_CONFLICT"});assert.equal(requests,4) + assert.deepEqual(await session.readExisting(plan),receipt) + await assert.rejects(session.execute({...plan,bindingProof:"0".repeat(64)}));assert.equal(requests,4) + await withPartitionWrite(async owner=>{await owner.client.query("INSERT INTO traffic_samples(server_id,interface_name,sampled_at,rx_bytes) VALUES($1,'after-full-poll',now(),1)",[serverId])}) + revoked=true;await assert.rejects(session.execute(plan));assert.equal(requests,4) + console.log("Full durable poll actual SQL/HTTP acceptance:",JSON.stringify({fourNativeGets:requests,maintenanceEffects:plan.maintenance.length,parents:PARTITION_SPECS.length,completedPublications:receipt.completedPublications,sameBackendLeaseAndTransactions:true,guardedSameHolderJournal:true,separateDurableJournalCommits:true,independentQuarantineAfterRealLostCommitAck:true,ordinaryWriterBlockedBeforeRelationSql:true,backgroundTickDeferredAndResumed:true,realJournal:true,repeatedApplyNoNewNative:true,outerInputMutationCannotReplaceCapturedIdentityOrAuthority:true,changedPlanBindingRefused:true,lateRevocationRefused:true,actualHttpAuthenticationMiddleware:false,productionRegistration:false,externalNetwork:false,authorityReads})) +}finally{ + if(!releasedNative)releaseNative() + listener.closeAllConnections();if(listener.listening)await new Promise(resolve=>listener.close(()=>resolve())) + if(serverId!==undefined)await pool.query("UPDATE servers SET enabled=false WHERE id=$1",[serverId]) + await closePool() +} diff --git a/backend/src/services/server-poll-durable-session.ts b/backend/src/services/server-poll-durable-session.ts new file mode 100644 index 00000000..dfe96b9a --- /dev/null +++ b/backend/src/services/server-poll-durable-session.ts @@ -0,0 +1,97 @@ +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +import { createServerPollProductionExecution, createServerPollProductionIo } from "./server-poll-production.js" +import { createAtomicServerPollProductionExecution } from "./server-poll-atomic-production.js" +import { createServerPartitionMaintenanceLease } from "./server-partition-maintenance-lease.js" +import { createSqlServerPollJournal, parseServerPollJournalIdentity, ServerPollJournalConflict, type ServerPollJournalIdentity } from "./server-poll-journal.js" +import { pollProblem } from "./server-poll-execution-model.js" +import type { ServerPollDependencies, ServerPollPlan, ServerPollRequest } from "./server-poll-execution.js" +import { createHolderBoundServerPollStorageJournal, createServerPollStorageJournal } from "./server-poll-storage-journal.js" +import { createServerPollBoundaryCoordinator } from "./server-poll-boundary.js" + +export interface DurableServerPollSessionInput { + request:ServerPollRequest + /** Accepted trusted REST identity or the actual MCP operation identity. + * This private API does not authenticate HTTP, approve a plan or issue grants. */ + identity:ServerPollJournalIdentity + /** Caller must validate current owner/permissions/global full-server scope, + * original credential/generation, protections and cancellation. Required; + * it is never replaced by a permissive or process-memory default. */ + authorize:ServerPollDependencies["authorize"] +} + +/** Private, unregistered complete assembly. ONE factory owns the maintenance + * exclusion AND every production SQL transaction. No caller-supplied pool, + * transaction, native transport, journal, signer or lease can split ownership. + * Construction/import performs no SQL, native requests or schema provisioning. + * The journal/trigger migration must already exist or execution fails closed. */ +function durableSession(input:DurableServerPollSessionInput,storageBound:boolean,atomicMaintenance=false) { + const request=structuredClone(input.request),identity=parseServerPollJournalIdentity(structuredClone(input.identity)),authorityProvider=input.authorize + if(!request||!Number.isSafeInteger(request.serverId)||request.serverId<=0||!/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i.test(request.receiptId)||typeof input.authorize!=="function"||identity.expiresAt<=identity.issuedAt)pollProblem("INVALID_INPUT","Нужны доверенные request, identity и authority полного poll") + // Derive the purpose key once for this private batch. Current access and + // generation are still checked at every authority boundary; no admission + // decision is cached. The server-owned signer is never serialized/exposed. + let signer:Promise<(value:string)=>string>|undefined + const proof:ServerPollDependencies["proof"]=async value=>{ + const security=await import("../modules/mcp/security.js") + signer??=security.privateFingerprinter("server-poll-production-v1") + return (await signer)(security.fingerprint(value)) + } + const authorize:ServerPollDependencies["authorize"]=async(candidate,ids,signal)=>{ + if(candidate.serverId!==request.serverId||candidate.receiptId!==request.receiptId)pollProblem("STATE_CONFLICT","Запрос не принадлежит durable poll session") + signal?.throwIfAborted() + if(Date.now()=identity.expiresAt)pollProblem("PERMISSION_DENIED","Исходная identity poll истекла") + const required=[...new Set([request.serverId,...ids])] + const grant=await authorityProvider({...request},required,signal) + if(!grant.global||required.some(id=>!grant.serverIds.includes(id))||!/^[a-f0-9]{64}$/.test(grant.authorityProof))pollProblem("PERMISSION_DENIED","Требуется полный живой global допуск poll") + signal?.throwIfAborted() + if(Date.now()>=identity.expiresAt)pollProblem("PERMISSION_DENIED","Исходная identity poll истекла") + return grant + } + const productionIo=createServerPollProductionIo() + const boundary=createServerPollBoundaryCoordinator(authorize,request) + const lease=createServerPartitionMaintenanceLease({ + boundary, + connect:productionIo.connect, + assertAuthority:async options=>{await authorize(request,options.serverIds,options.signal)}, + }) + const io={...productionIo,transaction:lease.transaction} + const build=(journal:Pick)=>{ + const dependencies={...(storageBound?{approvalProtocol:"configuration-epoch-v3" as const}:{}),request,authorize,proof,boundary,io,maintenanceLease:lease.withLease,journal} + if(atomicMaintenance){if(!storageBound)pollProblem("STATE_UNAVAILABLE","Atomic maintenance requires durable quota-bound storage");return createAtomicServerPollProductionExecution(dependencies)} + return createServerPollProductionExecution(dependencies) + } + // Preparation has no journal effects. These throwing boundaries make an + // accidental unbound execution impossible; they never invent an ACK/claim. + const unavailable=async():Promise=>{throw new ServerPollJournalConflict()} + const prepare=async(signal?:AbortSignal)=>build({claim:unavailable,intent:unavailable,acknowledge:unavailable,complete:unavailable}).prepare(request,signal) + const journalFor=async(raw:ServerPollPlan,mode:"execute"|"read",signal?:AbortSignal)=>{ + const plan=structuredClone(raw) + if(plan.request.serverId!==request.serverId||plan.request.receiptId!==request.receiptId)pollProblem("STATE_CONFLICT","План не принадлежит durable poll session") + const deadlineAt=Math.min(identity.expiresAt,Date.now()+SERVER_POLL_WALL_BUDGET_MS) + const assertAuthority=async(context:{plan:ServerPollPlan})=>{ + signal?.throwIfAborted() + const grant=await authorize(request,context.plan.affectedServerIds,signal) + if(grant.authorityProof!==context.plan.authorityProof)pollProblem("STATE_CONFLICT","Полномочия durable poll изменились") + } + const context={identity,plan} + const storageOptions={assertAuthority,deadlineAt,signal} + const storage=storageBound?await (mode==="execute" + ?createHolderBoundServerPollStorageJournal(context,storageOptions,lease.journalOwner) + :createServerPollStorageJournal(context,storageOptions)):undefined + const journal=createSqlServerPollJournal(context,{ + executionProof:proof,deadlineAt,assertAuthority,storage, + }) + return {journal,plan} + } + const execute=async(raw:ServerPollPlan,signal?:AbortSignal)=>{const {journal,plan}=await journalFor(raw,"execute",signal);return build(journal).execute(plan,signal)} + const readExisting=async(raw:ServerPollPlan,signal?:AbortSignal)=>{const {journal}=await journalFor(raw,"read",signal);return journal.readExisting()} + return {prepare,execute,readExisting} +} +/** Historical private assembly for the enrolled 0038 fixture; public adapters + * must use the quota-bound constructor below after storage enrollment. */ +export const createDurableServerPollSession=(input:DurableServerPollSessionInput)=>durableSession(input,false) +/** Opt-in quota-bound assembly. Missing storage schema fails closed. */ +export const createStorageDurableServerPollSession=(input:DurableServerPollSessionInput)=>durableSession(input,true) +/** Fixed quota-bound v4 issuer for public adapters. Legacy constructors retain + * their original protocol; existing plans are never grouped automatically. */ +export const createAtomicStorageDurableServerPollSession=(input:DurableServerPollSessionInput)=>durableSession(input,true,true) diff --git a/backend/src/services/server-poll-error-provenance.test.ts b/backend/src/services/server-poll-error-provenance.test.ts new file mode 100644 index 00000000..e56f5224 --- /dev/null +++ b/backend/src/services/server-poll-error-provenance.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { DatabaseError } from "pg" +import { capturePollError, carryCapturedPollError, carryPollError, pollErrorLog, tagPollProbeGuard } from "./server-poll-error-provenance.js" +const id = "11111111-1111-4111-8111-111111111111" + +test("genuine PG origin survives replacements without changing public error identity or properties", () => { + const original = new DatabaseError("PRIVATE SQL/password", 1, "error"); original.code = "57014" + const replacement = Object.assign(new Error("same public message"), { code: "STATE_UNAVAILABLE", statusCode: 503 }) + const before = Object.getOwnPropertyDescriptors(replacement), json = JSON.stringify(replacement) + assert.equal(carryPollError(replacement, original, "probe.query"), replacement) + assert.deepEqual(Object.getOwnPropertyDescriptors(replacement), before); assert.equal(JSON.stringify(replacement), json) + const final = carryPollError(new Error("same final"), replacement, "poll.safe") + const record = pollErrorLog(final, id, "servers.poll")! + assert.equal(record.provenance.origin.sqlState, "57014"); assert.deepEqual(record.provenance.sites, ["probe.query", "poll.safe"]) + assert(!JSON.stringify(record).includes("PRIVATE")); assert(!JSON.stringify(record).includes("password")) +}) +test("unknown SQLSTATE and inherited/non-driver codes never become SQL provenance", () => { + const pg = new DatabaseError("private", 1, "error"); pg.code = "ZZ999" + assert.equal(capturePollError(pg).origin.sqlState, null) + assert.equal(capturePollError(Object.assign(new Error(), { code: "57014" })).origin.sqlState, null) + assert.equal(capturePollError(Object.assign(new Error(), { code: "ETIMEDOUT" })).origin.nodeCategory, "timeout") +}) +test("malicious getters/proxies are unread and cannot replace a new error or leak raw fields", () => { + let calls = 0; const original = new Error() + for (const name of ["code", "message", "stack", "cause"]) Object.defineProperty(original, name, { get() { calls++; throw Error("secret") } }) + const target = new Error("public") + assert.equal(carryPollError(target, original, "probe.query"), target); assert.equal(calls, 0) + const proxy = new Proxy({}, { getOwnPropertyDescriptor() { calls++; throw Error("secret") }, getPrototypeOf() { calls++; throw Error("secret") } }) + assert.equal(carryPollError(new Error(), proxy, "poll.safe") instanceof Error, true); assert.equal(calls, 0) + assert(!JSON.stringify(pollErrorLog(target, id, "servers.poll")).includes("secret")) +}) +test("only a trusted Poll operation type formats an event; shared Probe metadata alone is silent", () => { + const error = carryPollError(new Error(), Object.assign(new Error(), { code: "ECONNRESET" }), "probe.query") + assert.equal(pollErrorLog(error, id, "wireguard.export"), null); assert.equal(pollErrorLog(error, id, "ipsec.create"), null) + assert.equal(pollErrorLog(error, "invalid", "servers.poll"), null); assert.equal(pollErrorLog(new Error(), id, "servers.poll"), null) + assert.equal(pollErrorLog(error, id, "servers.poll")?.operationId, id) +}) +test("closed captured native provenance has no raw error and chains stay <=8 and <=1KiB", () => { + const original = Object.assign(new Error("RAW native/URL"), { code: "EPIPE" }) + let error = carryCapturedPollError(new Error(), capturePollError(original), "native.fence") + for (let i = 0; i < 20; i++) error = carryPollError(new Error(), error, "poll.safe") + const result = pollErrorLog(error, id, "servers.poll")! + assert.equal(result.provenance.sites.length, 8); assert.equal(result.provenance.truncated, true) + assert.equal(result.provenance.origin.nodeCategory, "pipe"); assert(Buffer.byteLength(JSON.stringify(result)) <= 1024) + assert(!JSON.stringify(result).includes("RAW")); assert(!Object.hasOwn(error, "cause")); assert(Object.isFrozen(result.provenance.origin)) +}) +test("forged captured extras are not spread into logs and projection failure leaves original throw untouched", () => { + const target = new Error("original") + const proxy = new Proxy({}, { getOwnPropertyDescriptor() { throw Error("private") } }) + assert.equal(carryCapturedPollError(target, proxy as never, "poll.safe"), target) + assert.doesNotThrow(() => pollErrorLog(proxy, id, "servers.poll")) + const fake = { version: 1, origin: { kind: "pg", sqlState: "SECRET", nodeCategory: "secret", raw: "PRIVATE" }, sites: ["poll.safe"], truncated: false, message: "PRIVATE" } + const result = pollErrorLog(carryCapturedPollError(new Error(), fake as never, "poll.safe"), id, "servers.poll")! + assert.equal(result.provenance.origin.sqlState, null); assert(!JSON.stringify(result).includes("PRIVATE")); assert(!JSON.stringify(result).includes("SECRET")) +}) + +test("probe guard closed projection survives full chain; raw fields/getters/proxies cannot enter stderr",()=>{ + let calls=0;const target=new Error("public"),before=Object.getOwnPropertyDescriptors(target) + const detail={phase:"query",queryKind:"principal",branch:"driver-reject",raw:"NEVER_PRIVATE",message:"NEVER_PRIVATE"} + tagPollProbeGuard(target,Error("NEVER_PRIVATE password query"),detail as never) + assert.deepEqual(Object.getOwnPropertyDescriptors(target),before) + const final=carryPollError(new Error(),target,"poll.safe"),log=pollErrorLog(final,id,"servers.poll")! + assert.deepEqual(log.provenance.probe,{phase:"query",queryKind:"principal",branch:"driver-reject"});assert(Object.isFrozen(log.provenance.probe)) + assert(!JSON.stringify(log).includes("NEVER_PRIVATE"));assert(Buffer.byteLength(JSON.stringify(log))<=1024) + const hostile=Object.defineProperty({},"branch",{get(){calls++;throw Error("NEVER_PRIVATE")}}) + assert.equal(tagPollProbeGuard(target,undefined,hostile as never),target) + const proxy=new Proxy({},{getOwnPropertyDescriptor(){calls++;throw Error("NEVER_PRIVATE")}}) + assert.equal(tagPollProbeGuard(target,undefined,proxy as never),target);assert.equal(calls,0) +}) +test("forged probe enum and unknown phase are omitted rather than interpreted",()=>{ + for(const probe of [{phase:"query",queryKind:"SECRET",branch:"driver-reject"},{phase:"SECRET",queryKind:"meta",branch:"abort"},{phase:"query",queryKind:"commit",branch:"SECRET"}]){ + const captured={version:1,origin:{kind:"other",sqlState:null,nodeCategory:null},sites:[],truncated:false,probe} + const final=carryCapturedPollError(new Error(),captured as never,"poll.safe"),log=pollErrorLog(final,id,"servers.poll")! + assert.equal(log.provenance.probe,undefined);assert(!JSON.stringify(log).includes("SECRET")) + } +}) diff --git a/backend/src/services/server-poll-error-provenance.ts b/backend/src/services/server-poll-error-provenance.ts new file mode 100644 index 00000000..272e0113 --- /dev/null +++ b/backend/src/services/server-poll-error-provenance.ts @@ -0,0 +1,110 @@ +/** Error-path provenance only. Never authority, public payload or raw cause. */ +import { types } from "node:util" +import { DatabaseError } from "pg" + +const sites = ["probe.query", "poll.meta", "poll.sql", "poll.connect", "storage", "journal.auth", "journal.tx", "native.fence", "poll.exec", "poll.safe"] as const +type Site = typeof sites[number] +const sqlStates = new Set(["40001", "40P01", "57014", "53300", "08003", "08006", "57P01", "23505", "23503", "23514", "22P02", "42P01", "42501", "25P02", "55P03", "53200", "53100", "XX000"]) +const nodeCategories: Readonly> = Object.freeze({ ECONNRESET: "reset", ECONNREFUSED: "refused", ETIMEDOUT: "timeout", ENOTFOUND: "dns", EAI_AGAIN: "dns", EPIPE: "pipe", ERR_STREAM_DESTROYED: "stream" }) +const probeBranches = ["deadline", "abort", "driver-throw", "driver-reject", "closed", "busy", "input", "inactive", "lease-error", "lease-abort", "lease-timeout", "lease-closed", "result-closed", "result-inactive", "result-shape", "commit-ack", "result-access"] as const +const probeKinds = ["principal", "meta", "storage-lock", "commit", "other"] as const +export interface PollProbeGuard { readonly phase: "checkout" | "query"; readonly queryKind: typeof probeKinds[number]; readonly branch: typeof probeBranches[number] } +const executionPhases = ["native", "server.name", "flow-cache.invalidate", "snapshot.insert", "snapshot.raw.clear", + "partition.drop", "partition.ensure", "partition.storage", "partition.atomic", "receipt"] as const +const executionBoundaries = ["gate", "journal-intent", "dispatch-fence", "effect", "precommit", "after-precommit", + "independent-readback", "journal-ack", "receipt-write", "receipt-authority"] as const +export interface PollExecutionProgress { + readonly phase: typeof executionPhases[number] + readonly boundary: typeof executionBoundaries[number] + readonly confirmedPublications: number + readonly completedPublications: number +} +export interface PollErrorDiagnostic { + readonly version: 1 + readonly origin: Readonly<{ kind: "pg" | "node" | "other"; sqlState: string | null; nodeCategory: string | null }> + readonly sites: readonly Site[] + readonly truncated: boolean + readonly probe?: Readonly + readonly execution?: PollExecutionProgress +} +const errors = new WeakMap() +const unknownDiagnostic: PollErrorDiagnostic = Object.freeze({ version: 1, origin: Object.freeze({ kind: "other", sqlState: null, nodeCategory: null }), sites: Object.freeze([]), truncated: false }) +const own = (value: object, key: string): unknown => Object.getOwnPropertyDescriptor(value, key)?.value +const key = (value: unknown): value is Error => !!value && typeof value === "object" && !types.isProxy(value) && types.isNativeError(value) + +function closedProbe(raw: unknown): Readonly | undefined { + if (!raw || typeof raw !== "object" || types.isProxy(raw)) return undefined + const phase = own(raw, "phase"), queryKind = own(raw, "queryKind"), branch = own(raw, "branch") + if ((phase !== "checkout" && phase !== "query") || !probeKinds.includes(queryKind as PollProbeGuard["queryKind"]) || !probeBranches.includes(branch as PollProbeGuard["branch"])) return undefined + return Object.freeze({ phase, queryKind: queryKind as PollProbeGuard["queryKind"], branch: branch as PollProbeGuard["branch"] }) +} +function closedExecution(raw: unknown): PollExecutionProgress | undefined { + if (!raw || typeof raw !== "object" || types.isProxy(raw)) return undefined + const phase = own(raw, "phase") as PollExecutionProgress["phase"] + const boundary = own(raw, "boundary") as PollExecutionProgress["boundary"] + const confirmedPublications = own(raw, "confirmedPublications"), completedPublications = own(raw, "completedPublications") + if (!executionPhases.includes(phase) || !executionBoundaries.includes(boundary) || + typeof confirmedPublications !== "number" || !Number.isSafeInteger(confirmedPublications) || confirmedPublications > 2004 || + typeof completedPublications !== "number" || !Number.isSafeInteger(completedPublications) || + completedPublications < 0 || completedPublications > confirmedPublications) return undefined + return Object.freeze({ phase, boundary, confirmedPublications, completedPublications }) +} +/** Error-path annotation only. It never changes the error, stores a raw cause, + * or adds a site; the existing replacement pipeline adds its original site. */ +export function tagPollProbeGuard(replacement: T, original: unknown, detail: PollProbeGuard): T { + try { + const probe = closedProbe(detail) + if (!key(replacement) || !probe) return replacement + const captured = capturePollError(original) + const value: PollErrorDiagnostic = Object.freeze({ ...captured, probe }) + if (Buffer.byteLength(JSON.stringify(value)) <= 768) errors.set(replacement, value) + } catch { /* Diagnostics cannot replace/throw/retry. */ } + return replacement +} + +export function capturePollError(error: unknown): PollErrorDiagnostic { + try { + if (key(error)) { + const previous = errors.get(error) + if (previous) return previous + const code = own(error, "code") + const pg = Object.getPrototypeOf(error) === DatabaseError.prototype + const nodeCategory = typeof code === "string" && Object.hasOwn(nodeCategories, code) ? nodeCategories[code] : null + return Object.freeze({ version: 1, origin: Object.freeze({ kind: pg ? "pg" : nodeCategory ? "node" : "other", sqlState: pg && typeof code === "string" && sqlStates.has(code) ? code : null, nodeCategory: pg ? null : nodeCategory }), sites: Object.freeze([]), truncated: false }) + } + } catch { /* Diagnostic projection cannot change the original outcome. */ } + return unknownDiagnostic +} + +export function carryCapturedPollError(replacement: T, captured: PollErrorDiagnostic | undefined, site: Site): T { + try { + if (!key(replacement) || !sites.includes(site)) return replacement + // Rebuild only closed fields: never spread caller objects or retain errors. + const value = captured && !types.isProxy(captured) ? captured : undefined + const rawOrigin = value ? own(value, "origin") : undefined + const origin = rawOrigin && typeof rawOrigin === "object" && !types.isProxy(rawOrigin) ? rawOrigin : undefined + const kind = origin ? own(origin, "kind") : undefined, sql = origin ? own(origin, "sqlState") : undefined, node = origin ? own(origin, "nodeCategory") : undefined + const old = value ? own(value, "sites") : undefined + const previous = Array.isArray(old) && !types.isProxy(old) ? old.filter((item): item is Site => sites.includes(item)).slice(0, 8) : [] + const probe = value ? closedProbe(own(value, "probe")) : undefined + const execution = value ? closedExecution(own(value, "execution")) : undefined + const diagnostic: PollErrorDiagnostic = Object.freeze({ version: 1, origin: Object.freeze({ kind: kind === "pg" || kind === "node" ? kind : "other", sqlState: kind === "pg" && typeof sql === "string" && sqlStates.has(sql) ? sql : null, nodeCategory: kind === "node" && typeof node === "string" && Object.values(nodeCategories).includes(node) ? node : null }), sites: Object.freeze([...previous.slice(0, 7), site]), truncated: previous.length >= 8 || !!value && own(value, "truncated") === true, ...(probe ? { probe } : {}), ...(execution ? { execution } : {}) }) + if (Buffer.byteLength(JSON.stringify(diagnostic)) <= 768) errors.set(replacement, diagnostic) + } catch { /* No retry, raw logging, property mutation or replacement error. */ } + return replacement +} +export function carryPollError(replacement: T, original: unknown, site: Site): T { + return carryCapturedPollError(replacement, capturePollError(original), site) +} +export function recordPollExecutionError(replacement: T, original: unknown, execution: PollExecutionProgress): T { + return carryCapturedPollError(replacement, { ...capturePollError(original), execution }, "poll.exec") +} +export function pollErrorLog(error: unknown, operationId: string, operationType: string): Readonly<{ event: "mcp.poll.error-origin"; operationId: string; provenance: PollErrorDiagnostic }> | null { + try { + if (operationType !== "servers.poll" || typeof operationId !== "string" || !key(error) || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(operationId)) return null + const provenance = errors.get(error) + if (!provenance) return null + const result = Object.freeze({ event: "mcp.poll.error-origin" as const, operationId, provenance }) + return Buffer.byteLength(JSON.stringify(result)) <= 1024 ? result : null + } catch { return null } +} diff --git a/backend/src/services/server-poll-execution-model.ts b/backend/src/services/server-poll-execution-model.ts new file mode 100644 index 00000000..2c236f68 --- /dev/null +++ b/backend/src/services/server-poll-execution-model.ts @@ -0,0 +1,149 @@ +import { PARTITION_SPECS, type PartitionKind } from "../db/partitions.js" +import { parseRosCpuLoadPercent, parseRosDataSizeBytes } from "./ros-metric-parse.js" +import { isDeepStrictEqual } from "node:util" + +export type PollValue=null|boolean|number|string|PollValue[]|{[key:string]:PollValue} +export type PollRow=Record +export interface ServerPollContext { + /** Complete JSON-normalized rows, including endpoint epoch and credentials. + * Date values must be normalized by the future authoritative SQL reader. */ + servers:PollRow[]; settings:{traffic:PollRow|null;uptime:PollRow|null;internetPath:PollRow|null} + /** Complete bounded public-partition inventory, not just expired names. */ + partitions:PollRow[]; snapshots:PollRow[]; protections:PollRow[] +} +export const SERVER_POLL_NATIVE_READS=["identity","resource","interfaces","addresses"] as const +export type ServerPollNativeRead=typeof SERVER_POLL_NATIVE_READS[number] +export type ServerPollEffect= + |{kind:"server.name";serverId:number;name:string;updatedAt:string} + |{kind:"flow-cache.invalidate";serverId:number} + |{kind:"snapshot.insert";snapshot:PollRow} + |{kind:"snapshot.raw.clear";serverId:number;beforeAt:string;ids:PollValue[]} + |{kind:"partition.drop";parent:string;name:string} + |{kind:"partition.ensure";parent:string;name:string;from:string;to:string} + |{kind:"partition.storage";parent:string;name:string;settings:Record} +export interface ServerPollAck {row?:PollRow} +export class ServerPollProblem extends Error { + constructor(readonly code:"INVALID_INPUT"|"STATE_UNAVAILABLE"|"STATE_CONFLICT"|"PERMISSION_DENIED"|"ABORTED"|"MUTATION_OUTCOME_UNKNOWN"|"VERIFICATION_FAILED",message:string){super(message)} +} +export function pollProblem(code:ServerPollProblem["code"],message:string):never{throw new ServerPollProblem(code,message)} +const sid=(value:unknown):value is number=>typeof value==="number"&&Number.isSafeInteger(value)&&value>0 +function boundedJson(value:unknown,maximum:number){let text:string;try{text=JSON.stringify(value)}catch{return false}return typeof text==="string"&&Buffer.byteLength(text)<=maximum} +function json(value:unknown,depth=0):value is PollValue { + if(depth>16)return false + if(value===null||typeof value==="string"||typeof value==="boolean")return true + if(typeof value==="number")return Number.isFinite(value) + if(Array.isArray(value))return value.length<=2000&&value.every(item=>json(item,depth+1)) + return !!value&&typeof value==="object"&&Object.getPrototypeOf(value)===Object.prototype&&Object.keys(value).length<=256&&Object.values(value).every(item=>json(item,depth+1)) +} +const iso=(value:unknown):value is string=>typeof value==="string"&&Number.isFinite(Date.parse(value))&&new Date(value).toISOString()===value +export function validateServerPollContext(value:ServerPollContext,serverId:number){ + if(!sid(serverId)||!json(value)||!boundedJson(value,1048576)||!value.settings||Object.keys(value.settings).sort().join(",")!=="internetPath,traffic,uptime")pollProblem("STATE_UNAVAILABLE","Неполный bounded контекст server poll") + for(const rows of [value.servers,value.partitions,value.snapshots,value.protections])if(!Array.isArray(rows)||rows.length>2000)pollProblem("STATE_UNAVAILABLE","Неполный каталог server poll") + if(value.servers.length>1000||value.partitions.length>1000||value.protections.length>1000||value.servers.some(row=>!sid(row.id))||new Set(value.servers.map(row=>row.id)).size!==value.servers.length)pollProblem("STATE_UNAVAILABLE","Некорректный каталог серверов poll") + const selected=value.servers.filter(row=>row.id===serverId) + if(selected.length!==1)pollProblem("STATE_UNAVAILABLE","Сервер poll отсутствует или неоднозначен") + const server=selected[0] + if(typeof server.name!=="string"||typeof server.enabled!=="boolean"||typeof server.routingEndpointEpoch!=="string"||!/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i.test(server.routingEndpointEpoch))pollProblem("STATE_UNAVAILABLE","Не подтверждён endpoint epoch poll") + for(const row of value.partitions)if(row.namespace!=="public"||typeof row.parent!=="string"||!PARTITION_SPECS.some(spec=>spec.parent===row.parent)||typeof row.name!=="string"||!/^[a-z_][a-z\d_]{0,99}$/.test(row.name)||!row.storage||Array.isArray(row.storage)||typeof row.storage!=="object"||Object.values(row.storage).some(v=>typeof v!=="string"))pollProblem("STATE_UNAVAILABLE","Не подтверждён полный каталог partitions") + if(new Set(value.partitions.map(row=>row.name)).size!==value.partitions.length)pollProblem("STATE_UNAVAILABLE","Повторяющиеся partitions") + if(value.snapshots.some(row=>row.serverId!==serverId||!(typeof row.id==="string"&&row.id.length>0||sid(row.id))||!iso(row.polledAt))||new Set(value.snapshots.map(row=>row.id)).size!==value.snapshots.length)pollProblem("STATE_UNAVAILABLE","Неполный каталог raw snapshots") + return server +} +const day=(d:Date)=>new Date(Date.UTC(d.getUTCFullYear(),d.getUTCMonth(),d.getUTCDate())) +const addDays=(d:Date,n:number)=>new Date(day(d).getTime()+n*86400000) +function range(kind:PartitionKind,ts:Date){const start=day(ts);if(kind==="week")start.setUTCDate(start.getUTCDate()-((start.getUTCDay()||7)-1));if(kind==="month")start.setUTCDate(1);const end=kind==="month"?new Date(Date.UTC(start.getUTCFullYear(),start.getUTCMonth()+1,1)):addDays(start,kind==="week"?7:1);return {start,end}} +function partName(parent:string,kind:PartitionKind,start:Date){return parent+"_"+kind[0]+"_"+(kind==="month"?start.toISOString().slice(0,7):start.toISOString().slice(0,10)).replaceAll("-","")} +const factSettings={fillfactor:"70",autovacuum_vacuum_scale_factor:"0.05",autovacuum_vacuum_cost_limit:"2000"} +/** Same fixed parents, retention overrides +7 days, windows, per-parent order, + * IF-NOT-EXISTS ensure and fact-leaf ALTER as db/partitions.ts. Every individual + * DDL command is manifest-bound; this function performs no SQL or native I/O. */ +export function planServerPollMaintenance(context:ServerPollContext,around:string):ServerPollEffect[]{ + if(!iso(around))pollProblem("INVALID_INPUT","Некорректное время maintenance") + const now=new Date(around),keep=new Map(PARTITION_SPECS.map(spec=>[spec.parent,spec.keepDays])) + for(const [key,parents]of [["traffic",["traffic_samples"]],["uptime",["uptime_probe_samples","uptime_resource_samples"]],["internetPath",["internet_path_snapshots"]]]as const){const raw=context.settings[key]?.retentionDays,days=Number(raw);if(Number.isFinite(days)&&days>0){if(days>3660)pollProblem("STATE_UNAVAILABLE","Retention превышает bounded maintenance budget");for(const parent of parents)keep.set(parent,days+7)}} + const drops:ServerPollEffect[]=[],ensure:ServerPollEffect[]=[] + for(const spec of PARTITION_SPECS){ + const cutoff=addDays(now,-keep.get(spec.parent)!) + for(const row of context.partitions.filter(row=>row.parent===spec.parent).sort((a,b)=>String(a.name).localeCompare(String(b.name)))){ + const match=String(row.name).match(new RegExp("^"+spec.parent+"_"+spec.kind[0]+"_(\\d{8}|\\d{6})$"));if(!match)continue + const stamp=match[1],start=new Date(stamp.length===6?`${stamp.slice(0,4)}-${stamp.slice(4,6)}-01T00:00:00.000Z`:`${stamp.slice(0,4)}-${stamp.slice(4,6)}-${stamp.slice(6,8)}T00:00:00.000Z`) + if(Number.isFinite(start.getTime())&&partName(spec.parent,spec.kind,start)===row.name&&start2000)pollProblem("STATE_UNAVAILABLE","Превышен бюджет полного maintenance manifest") + t=spec.kind==="month"?new Date(Date.UTC(t.getUTCFullYear(),t.getUTCMonth()+1,1)):addDays(t,spec.kind==="week"?7:1) + } + } + return [...drops,...ensure] +} +export function serverPollSnapshot(serverId:number,polledAt:string,results:Record,latencyMs:number):PollRow { + const object=(value:unknown):value is Record=>!!value&&typeof value==="object"&&!Array.isArray(value)&&Object.values(value).every(v=>typeof v==="string") + if(!object(results.identity)||typeof results.identity.name!=="string"||!object(results.resource)||![results.interfaces,results.addresses].every(value=>Array.isArray(value)&&value.length<=2000&&value.every(object))||!boundedJson(results,1048576))pollProblem("STATE_UNAVAILABLE","Неполный ответ server poll") + const r=results.resource + return {serverId,polledAt,status:"online",latencyMs,identityName:results.identity.name,rosVersion:r.version??null,boardName:r["board-name"]??null,uptime:r.uptime??null,cpuLoad:parseRosCpuLoadPercent(r["cpu-load"]),freeMemory:parseRosDataSizeBytes(r["free-memory"]),totalMemory:parseRosDataSizeBytes(r["total-memory"]),rawInterfaces:results.interfaces as PollValue,rawIpAddresses:results.addresses as PollValue} +} +/** Exact whole-context projection; opaque fields and row order are conserved. + * Only an acknowledged new snapshot/partition may admit observed defaults. */ +export function projectServerPollEffect(before:ServerPollContext,effect:ServerPollEffect,ack:ServerPollAck):ServerPollContext{ + const after=structuredClone(before) + applyServerPollEffectProjection(after, effect, ack) + return after +} + +export function projectServerPollEffects(before: ServerPollContext, effects: readonly { effect: ServerPollEffect; ack: ServerPollAck }[]): ServerPollContext { + const after = structuredClone(before) + for (const { effect, ack } of effects) applyServerPollEffectProjection(after, effect, ack) + return after +} + +function applyServerPollEffectProjection(after: ServerPollContext, effect: ServerPollEffect, ack: ServerPollAck): void { + if(effect.kind==="server.name"){const row=after.servers.find(row=>row.id===effect.serverId);if(!row)pollProblem("STATE_CONFLICT","Исчез сервер poll");row.name=effect.name;row.updatedAt=effect.updatedAt} + if(effect.kind==="snapshot.insert"){ + const row=ack.row;if(!row||!(sid(row.id)||typeof row.id==="string"&&row.id.length>0)||after.snapshots.some(old=>old.id===row.id)||!json(row)||!boundedJson(row,1048576)||Object.entries(effect.snapshot).some(([key,value])=>!isDeepStrictEqual(row[key]??null,value)))pollProblem("VERIFICATION_FAILED","Не подтверждён вставленный snapshot") + after.snapshots.push(structuredClone(row)) + } + if(effect.kind==="snapshot.raw.clear")for(const row of after.snapshots)if(effect.ids.includes(row.id)){if(row.serverId!==effect.serverId||String(row.polledAt)>=effect.beforeAt)pollProblem("STATE_CONFLICT","Изменился inventory raw cleanup");row.rawInterfaces=null;row.rawIpAddresses=null} + if(effect.kind==="partition.drop")after.partitions=after.partitions.filter(row=>row.parent!==effect.parent||row.name!==effect.name) + if(effect.kind==="partition.ensure"){ + const found=after.partitions.find(row=>row.name===effect.name) + if(found){if(found.parent!==effect.parent||found.from!==effect.from||found.to!==effect.to)pollProblem("VERIFICATION_FAILED","Не подтверждены границы существующей partition")} + else{const row=ack.row;if(!row||row.name!==effect.name||row.parent!==effect.parent||row.namespace!=="public"||row.from!==effect.from||row.to!==effect.to||!json(row)||!boundedJson(row,16384))pollProblem("VERIFICATION_FAILED","Не подтверждена новая partition");after.partitions.push(structuredClone(row))} + } + if(effect.kind==="partition.storage"){const row=after.partitions.find(row=>row.name===effect.name&&row.parent===effect.parent);if(!row||!row.storage||Array.isArray(row.storage)||typeof row.storage!=="object")pollProblem("VERIFICATION_FAILED","Не подтверждена fact partition");row.storage={...row.storage,...effect.settings}} +} + +export function planServerPollMaintenanceV2(context: ServerPollContext, around: string): ServerPollEffect[] { + const legacy = planServerPollMaintenance(context, around) + const original = new Map(context.partitions.map(row => [String(row.name), row])) + // Reject a conflicting required bound even if retention will drop that name. + // This preserves the existing production reader's conservative refusal. + for (const effect of legacy) if (effect.kind === "partition.ensure") { + const row = original.get(effect.name) + if (row && (row.namespace !== "public" || row.parent !== effect.parent || row.from !== effect.from || row.to !== effect.to)) + pollProblem("STATE_UNAVAILABLE", "Существующая partition имеет другие границы") + } + const available = new Map(original) + const created = new Set() + return legacy.filter(effect => { + if (effect.kind === "partition.drop") available.delete(effect.name) + if (effect.kind === "partition.ensure") { + if (available.has(effect.name)) return false + created.add(effect.name) + // This is projected planner state only; a new leaf's actual storage is + // unknown until its SQL ACK, so created leaves always keep fact ALTER. + available.set(effect.name, { namespace: "public", parent: effect.parent, name: effect.name, from: effect.from, to: effect.to, storage: {} }) + } + if (effect.kind === "partition.storage" && !created.has(effect.name)) { + const row = available.get(effect.name), storage = row?.storage + // Exact known-key equality only. Extra storage keys remain untouched by + // either ALTER SET or elision; missing/noncanonical keys retain ALTER. + if (row?.parent === effect.parent && storage && typeof storage === "object" && !Array.isArray(storage) + && Object.entries(effect.settings).every(([key, value]) => storage[key] === value)) return false + } + return true + }) +} diff --git a/backend/src/services/server-poll-execution.test.ts b/backend/src/services/server-poll-execution.test.ts new file mode 100644 index 00000000..2a879fd2 --- /dev/null +++ b/backend/src/services/server-poll-execution.test.ts @@ -0,0 +1,314 @@ +import assert from "node:assert/strict" +import { projectServerPollApprovalContext } from "./server-poll-approval-context.js" +import { createHmac,randomUUID } from "node:crypto" +import { createServerPollExecution,ServerPollExecutionError,type ServerPollDependencies,type ServerPollReceipt } from "./server-poll-execution.js" +import { carryPollError, pollErrorLog } from "./server-poll-error-provenance.js" +import { planServerPollMaintenance,projectServerPollEffect,ServerPollProblem,type ServerPollContext,type ServerPollEffect,type PollRow } from "./server-poll-execution-model.js" + +// All boundaries below are inert memory functions. No DB pool, native client, +// filesystem key material, scheduler, notifications or network is imported. +const around="2026-10-25T12:00:00.000Z",epoch="11111111-2222-3333-4444-555555555555" +const initial=():ServerPollContext=>({ + servers:[{id:1,name:"before",host:"192.0.2.1",enabled:false,password:"NEVER_POLL_ENDPOINT",routingEndpointEpoch:epoch,updatedAt:"2026-10-24T12:00:00.000Z",opaque:"NEVER_POLL_SERVER"},{id:2,name:"foreign",enabled:true,password:"NEVER_FOREIGN",routingEndpointEpoch:epoch}], + settings:{traffic:{retentionDays:14,opaque:"NEVER_TRAFFIC"},uptime:{retentionDays:14},internetPath:{retentionDays:14}}, + partitions:[{namespace:"public",parent:"server_snapshots",name:"server_snapshots_w_20000103",from:"2000-01-03T00:00:00.000Z",to:"2000-01-10T00:00:00.000Z",storage:{},opaque:"NEVER_OLD_PARTITION"}], + snapshots:[{id:1,serverId:1,polledAt:"2026-10-24T12:00:00.000Z",rawInterfaces:[{name:"old",opaque:"NEVER_RAW"}],rawIpAddresses:[],opaque:"NEVER_HISTORY"}],protections:[{kind:"database",id:"all",protection:"approval"}], +}) +function canonical(value:unknown):unknown{if(Array.isArray(value))return value.map(canonical);if(value&&typeof value==="object")return Object.fromEntries(Object.entries(value).sort(([a],[b])=>a.localeCompare(b)).map(([k,v])=>[k,canonical(v)]));return value} +const proof=async(value:unknown)=>createHmac("sha256","inert-private-fixture-only").update(JSON.stringify(canonical(value))).digest("hex") +let state:ServerPollContext=initial(),mode="normal",clock=Date.parse(around),global=true,allowed:number[]=[1,2],authority="a".repeat(64),nativeCalls:string[]=[],effects:ServerPollEffect[]=[],intents:unknown[]=[],acks:unknown[]=[],completed:ServerPollReceipt|undefined,claimed:{bindingProof:string;receipt?:ServerPollReceipt}|undefined,reads=0 +let afterIntent:(()=>void)|undefined,afterEffect:(()=>void)|undefined +function reset(){state=initial();mode="normal";clock=Date.parse(around);global=true;allowed=[1,2];authority="a".repeat(64);nativeCalls=[];effects=[];intents=[];acks=[];completed=undefined;claimed=undefined;reads=0;afterIntent=undefined;afterEffect=undefined} +reset() +const deps:ServerPollDependencies={ + now:()=>clock, + authorize:async(_request,_ids,signal)=>{signal?.throwIfAborted();if(mode==="revoked")throw new ServerPollProblem("PERMISSION_DENIED","inert revoke");return {global,serverIds:allowed,authorityProof:authority}}, + readContext:async(_request,check,permitted)=>{await check();reads++;assert.ok(permitted.includes(1));const value=structuredClone(state);await check();return value}, + proof,withGlobalLock:async work=>work(), + native:async(read,_server,options)=>{await options.beforeDispatch();if(mode==="duplicate-native")await options.beforeDispatch();nativeCalls.push(read);if(mode==="native-failed"&&read==="resource")throw Error("NEVER_PROVIDER_PAYLOAD");if(mode==="native-oversize"&&read==="identity")return{name:"x".repeat(1048577)};if(read==="identity")return{name:mode==="secret-reflection"?"router-NEVER_POLL_ENDPOINT":"after"};if(read==="resource")return{"cpu-load":"5","free-memory":"1024","total-memory":"2048",version:"7.24.5","board-name":"CHR",uptime:"1d"};if(read==="interfaces")return[{".id":"*a",name:"ether1",opaque:"NEVER_NATIVE_RAW"}];return[{".id":"*b",interface:"ether1",address:"192.0.2.1/24"}]}, + effect:async(effect,options)=>{ + await options.beforeDispatch();if(mode==="duplicate-effect")await options.beforeDispatch();effects.push(structuredClone(effect)) + let row:PollRow|undefined + if(effect.kind==="snapshot.insert")row={id:2,...effect.snapshot,opaque:"NEVER_NEW_DEFAULT"} + if(effect.kind==="partition.ensure")row={namespace:"public",parent:effect.parent,name:effect.name,from:effect.from,to:effect.to,storage:{},opaque:"NEVER_PARTITION_DEFAULT"} + const ack={row};state=projectServerPollEffect(state,effect,ack) + if(mode==="lost")throw Error("NEVER_LOST_ACK") + if(mode==="collateral")state.servers[1].password="NEVER_CHANGED_FOREIGN" + if(afterEffect){const hook=afterEffect;afterEffect=undefined;hook()} + return ack + }, + intent:async value=>{intents.push(structuredClone(value));if(afterIntent){const hook=afterIntent;afterIntent=undefined;hook()}}, + acknowledge:async value=>{if(mode==="ack-failed"||mode==="publication-ack-failed"&&value.stage.startsWith("publish."))throw Error("NEVER_JOURNAL_FAILURE");acks.push(structuredClone(value))}, + claim:async(_request,bindingProof)=>{if(claimed)return claimed.receipt?{state:"known",bindingProof:claimed.bindingProof,receipt:claimed.receipt}:{state:"unresolved",bindingProof:claimed.bindingProof};claimed={bindingProof};return{state:"new"}}, + complete:async(receipt,bindingProof)=>{assert.equal(claimed?.bindingProof,bindingProof);if(mode==="receipt-failed")throw Error("NEVER_RECEIPT_FAILURE");completed=structuredClone(receipt);claimed!.receipt=structuredClone(receipt)}, +} +const executor=createServerPollExecution(deps),request=()=>({serverId:1,receiptId:randomUUID()}) +let plan=await executor.prepare(request()) +assert.equal(plan.requiresHuman,true);assert.equal(plan.scope,"global");assert.deepEqual(plan.affectedServerIds,[1,2]);assert.ok(!JSON.stringify(plan).includes("NEVER_"));assert.ok(plan.maintenance.some(effect=>effect.kind==="partition.drop"));assert.ok(plan.maintenance.some(effect=>effect.kind==="partition.storage")) +const before=structuredClone(state),receipt=await executor.execute(plan) +assert.deepEqual(nativeCalls.sort(),["addresses","identity","interfaces","resource"]) +assert.equal(receipt.nativeReads,4);assert.equal(receipt.complete,true);assert.equal(receipt.maintenanceComplete,true);assert.equal(receipt.observation,"online");assert.equal(receipt.snapshot.identityName,"after");assert.ok(!JSON.stringify(receipt).includes("NEVER_")) +assert.equal(state.servers[0].name,"after");assert.equal(state.servers[0].enabled,false,"Existing REST poll permits a disabled target; source identity is still sealed") +assert.deepEqual(state.servers[1],before.servers[1]);assert.deepEqual(state.settings,before.settings);assert.deepEqual(state.protections,before.protections);assert.equal(state.snapshots[0].rawInterfaces,null);assert.equal(state.snapshots[0].opaque,"NEVER_HISTORY") +assert.deepEqual(effects.slice(0,4).map(effect=>effect.kind),["server.name","flow-cache.invalidate","snapshot.insert","snapshot.raw.clear"]) +assert.equal(effects.length,plan.maintenance.length+4);assert.equal(intents.length,effects.length+4);assert.equal(acks.length,intents.length);assert.equal(completed?.completedPublications,effects.length) +const nativeBefore=nativeCalls.length,effectsBefore=effects.length;assert.deepEqual(await executor.execute(plan),receipt);assert.equal(nativeCalls.length,nativeBefore);assert.equal(effects.length,effectsBefore,"Known durable receipt cannot replay poll or maintenance") +state.servers[1].name="later";assert.equal((await executor.execute(plan)).configurationVerified,false,"Historical receipt does not assert current state after later drift") + +reset() +let activeJournalWrites=0,nativeArrivals=0 +const nativeTogether=Promise.withResolvers() +const nativeTimeout=setTimeout(()=>nativeTogether.reject(new Error("Four native reads must remain concurrent")),1500) +const journalWrite=async(write:()=>Promise)=>{ + assert.equal(activeJournalWrites,0,"One poll must not queue its own writes against the global journal lock") + activeJournalWrites++ + try{ + await new Promise(resolve=>setImmediate(resolve)) + await write() + }finally{activeJournalWrites--} +} +const noSelfContention=createServerPollExecution({...deps, + intent:value=>journalWrite(()=>deps.intent(value)), + acknowledge:value=>journalWrite(()=>deps.acknowledge(value)), + native:async(...args)=>{ + if(++nativeArrivals===4)nativeTogether.resolve() + await nativeTogether.promise + return deps.native(...args) + }, +}) +try{ + assert.equal((await noSelfContention.execute(await noSelfContention.prepare(request()))).nativeReads,4) +}finally{clearTimeout(nativeTimeout)} + +reset() +let attemptedNativeIntents=0 +const uncertainJournal=createServerPollExecution({...deps,intent:async()=>{ + attemptedNativeIntents++ + throw new Error("Unconfirmed native intent") +}}) +await assert.rejects(uncertainJournal.execute(await uncertainJournal.prepare(request())),/Unconfirmed native intent/) +assert.equal(attemptedNativeIntents,1,"An unconfirmed intent must stop the queued journal writes") +assert.equal(nativeCalls.length,0) +assert.equal(effects.length,0) + +for(const mutate of [()=>{global=false},()=>{allowed=[1]},()=>{state.settings.traffic!.retentionDays=30},()=>{state.servers[0].routingEndpointEpoch=randomUUID()},()=>{state.partitions[0].opaque="changed"},()=>{authority="b".repeat(64)}]){ + reset();plan=await executor.prepare(request());mutate();await assert.rejects(executor.execute(plan));assert.equal(nativeCalls.length,0);assert.equal(effects.length,0) +} +reset();global=false;await assert.rejects(executor.prepare(request()));assert.equal(reads,0,"Global refusal precedes private context reads") +reset();plan=await executor.prepare(request());afterIntent=()=>{mode="revoked"};await assert.rejects(executor.execute(plan));assert.equal(nativeCalls.length,0);assert.equal(effects.length,0,"Revocation after durable read intent prevents native dispatch") +reset();plan=await executor.prepare(request());mode="native-failed";const offline=await executor.execute(plan);assert.equal(offline.observation,"offline");assert.equal(offline.maintenanceComplete,true);assert.ok(!effects.some(effect=>effect.kind==="server.name"));assert.ok(effects.some(effect=>effect.kind==="snapshot.insert"));assert.ok(effects.some(effect=>effect.kind==="partition.drop"));assert.equal(state.servers[0].name,"before") +for(const [fault, boundary] of [["lost","effect"],["collateral","independent-readback"], + ["publication-ack-failed","journal-ack"],["receipt-failed","receipt-write"]] as const){ + reset();plan=await executor.prepare(request());mode=fault + await assert.rejects(executor.execute(plan),(error:unknown)=>{ + assert(error instanceof ServerPollExecutionError) + assert.equal(error.code,fault==="collateral"?"VERIFICATION_FAILED":"MUTATION_OUTCOME_UNKNOWN") + const log=pollErrorLog(error,request().receiptId,"servers.poll") + assert(log) + assert.deepEqual(log.provenance.execution,{ + phase:fault==="receipt-failed"?"receipt":"server.name",boundary, + confirmedPublications:fault==="lost"?0:effects.length,completedPublications:fault==="receipt-failed"?effects.length:0, + }) + assert(!JSON.stringify(log).includes("NEVER_")) + return true + }) + const count:number=effects.length;await assert.rejects(executor.execute(plan));assert.equal(effects.length,count,"Unknown/partial run never starts a second dispatch sequence") + if(fault!=="receipt-failed")assert.equal(count,1) +} +reset();plan=await executor.prepare(request());afterEffect=()=>{mode="revoked"};await assert.rejects(executor.execute(plan),ServerPollExecutionError);assert.equal(effects.length,1) +reset();plan=await executor.prepare(request());mode="native-oversize";await assert.rejects(executor.execute(plan));assert.equal(effects.length,0) +reset();plan=await executor.prepare(request());mode="secret-reflection";assert.ok(!JSON.stringify(await executor.execute(plan)).includes("NEVER_POLL_ENDPOINT"),"Known private values reflected by a native public-looking field remain private") +reset();plan=await executor.prepare(request());clock+=60000;assert.equal((await executor.execute(plan)).snapshot.polledAt,new Date(clock).toISOString(),"Sample time is execution time, not approval time") +reset();plan=await executor.prepare(request());clock+=86400000;await assert.rejects(executor.execute(plan));assert.equal(nativeCalls.length,0,"Changed UTC partition window requires a fresh manifest") +reset();plan=await executor.prepare(request());mode="duplicate-native";await assert.rejects(executor.execute(plan));assert.equal(nativeCalls.length,0);assert.equal(effects.length,0) +reset();plan=await executor.prepare(request());mode="duplicate-effect";await assert.rejects(executor.execute(plan),ServerPollExecutionError);assert.equal(effects.length,0) +reset();let sawFullCatalog=false;await assert.rejects(createServerPollExecution({...deps,authorize:async(_request,ids)=>{if(ids.length===2)sawFullCatalog=true;return {global:true,serverIds:[1,2],authorityProof:ids.length===2?"b".repeat(64):"a".repeat(64)}}}).prepare(request()));assert.equal(sawFullCatalog,true,"Drift specifically at authorize(all) is rejected") +reset();plan=await executor.prepare(request());const cancel=new AbortController();cancel.abort();await assert.rejects(executor.execute(plan,cancel.signal));assert.equal(nativeCalls.length,0);assert.equal(effects.length,0) +reset();plan=await executor.prepare(request());afterEffect=()=>{clock+=121000};assert.equal((await executor.execute(plan)).complete,true,"Bounded metadata/DDL reserve allows a full poll beyond the old 120s envelope") +reset();plan=await executor.prepare(request());afterEffect=()=>{clock+=180001};await assert.rejects(executor.execute(plan),ServerPollExecutionError);assert.equal(effects.length,1,"Hard total budget still prevents any next publication") +reset();const originalExpiry=clock+90000,expiring=createServerPollExecution({...deps,authorize:async(...args)=>{if(clock>=originalExpiry)throw new ServerPollProblem("PERMISSION_DENIED","Original actor expired");return deps.authorize(...args)}});plan=await expiring.prepare(request());afterEffect=()=>{clock+=90001};await assert.rejects(expiring.execute(plan),ServerPollExecutionError);assert.equal(effects.length,1,"Metadata reserve never extends original authority expiry") +reset();const missing=structuredClone(state);delete (missing.settings as Partial).traffic;await assert.rejects(createServerPollExecution({...deps,readContext:async()=>missing}).prepare(request())) +assert.throws(()=>planServerPollMaintenance({...initial(),settings:{traffic:{retentionDays:99999},uptime:null,internetPath:null}},around)) +// V1/V2 planner compatibility below uses the existing inert authenticated +// fixture only. It does not manufacture a production journal or terminal. +const populateWarmPartitions=()=>{ + const planned=planServerPollMaintenance(state,new Date(clock).toISOString()) + for(const effect of planned){ + const row=effect.kind==="partition.ensure"?{namespace:"public",parent:effect.parent,name:effect.name,from:effect.from,to:effect.to,storage:{},opaque:"NEVER_PARTITION_DEFAULT"}:undefined + state=projectServerPollEffect(state,effect,{row}) + } +} + +reset();populateWarmPartitions();const warmBefore=structuredClone(state);plan=await executor.prepare(request()) +assert.equal(plan.version,2,"New plans explicitly seal the differential maintenance version") +assert.deepEqual(state,warmBefore,"Planning must not mutate original full catalogue or opaque fields") +const warmLegacy=planServerPollMaintenance(warmBefore,new Date(clock).toISOString()) +assert(plan.maintenance.length[...value.partitions].sort((a,b)=>String(a.name).localeCompare(String(b.name))) +assert.deepEqual(sortedPartitions(state),sortedPartitions(warmExpected),"V2 reaches the exact V1 required final partition catalogue") + +reset();populateWarmPartitions();const legacySeed=await executor.prepare(request()) +const {bindingProof:discardNewBinding,...legacySeedCore}=legacySeed +void discardNewBinding +const originalV1Maintenance=planServerPollMaintenance(state,new Date(clock).toISOString()) +const originalV1Core={...legacySeedCore,version:1 as const,maintenance:originalV1Maintenance,mutationCost:originalV1Maintenance.length+8} +const originalV1Plan={...originalV1Core,bindingProof:await proof(originalV1Core)},originalV1Copy=structuredClone(originalV1Plan) +const legacyReceipt=await executor.execute(originalV1Plan) +assert.equal(legacyReceipt.complete,true);assert.deepEqual(originalV1Plan,originalV1Copy) +assert.deepEqual(effects.slice(4),originalV1Maintenance,"A sealed historical V1 manifest executes legacy semantics, even on a warm catalogue") +assert(effects.length>warmPlanBefore.maintenance.length+4,"V1 is not silently upgraded to V2") + +for(const change of ["catalogue","required-storage","missing-leaf"]as const){ + reset();populateWarmPartitions();plan=await executor.prepare(request()) + const dispatchedNames=new Set(plan.maintenance.flatMap(effect=>"name"in effect?[effect.name]:[])) + const omitted=state.partitions.find(row=>row.parent==="flow_hour_facts"&&!dispatchedNames.has(String(row.name)))! + assert(omitted,"Test must target a leaf whose CREATE/ALTER were elided") + if(change==="catalogue")omitted.opaque="changed-after-approval" + if(change==="required-storage")(omitted.storage as Record).fillfactor="80" + if(change==="missing-leaf")state.partitions=state.partitions.filter(row=>row.name!==omitted.name) + await assert.rejects(executor.execute(plan));assert.equal(nativeCalls.length,0);assert.equal(effects.length,0,"Elided leaves remain covered by the full fresh source proof") +} + +for(const badVersion of [1,3]){ + reset();populateWarmPartitions();plan=await executor.prepare(request()) + const tampered={...plan,version:badVersion}as typeof plan + await assert.rejects(executor.execute(tampered));assert.equal(nativeCalls.length,0);assert.equal(effects.length,0);assert.equal(claimed,undefined,"Changing only the inner sealed version is rejected before claim") +} +reset();populateWarmPartitions() +await assert.rejects(executor.execute({...originalV1Plan,version:2})) +assert.equal(nativeCalls.length,0);assert.equal(effects.length,0);assert.equal(claimed,undefined,"A stored V1 proof cannot be upgraded by changing its inner version") +reset();populateWarmPartitions();const wrongPlannerSeed=await executor.prepare(request()) +const {bindingProof:discardWrongBinding,...wrongPlannerCore}=wrongPlannerSeed +void discardWrongBinding +const wrongMaintenance=planServerPollMaintenance(state,new Date(clock).toISOString()) +const wronglySealedCore={...wrongPlannerCore,maintenance:wrongMaintenance,mutationCost:wrongMaintenance.length+8} +await assert.rejects(executor.execute({...wronglySealedCore,bindingProof:await proof(wronglySealedCore)})) +assert.equal(nativeCalls.length,0);assert.equal(effects.length,0,"Even a fixture-authenticated V2 plan must match the current V2 planner, not legacy recomputation") + +// Version3 fixtures explicitly supply a trusted approval reader. No JSON flag +// or omitted capability can activate the relaxed waiting-time comparison. +const v3 = createServerPollExecution({...deps,approvalProof:async context=>proof(projectServerPollApprovalContext(context,1))}) +function version3State(){reset();populateWarmPartitions();for(const row of Object.values(state.settings))if(row)row.pollConfigurationEpoch=randomUUID()} +version3State();const delayed=await v3.prepare(request());assert.equal(delayed.version,3) +const delayedSource=delayed.beforeProof +state.settings.uptime!.lastCollectedAt="2026-10-25T12:00:20.000Z";state.settings.uptime!.lastError="collector finished";state.settings.uptime!.updatedAt="2026-10-25T12:00:20.000Z";clock+=20000 +let actualStart:Parameters[2] +const delayedExecutor=createServerPollExecution({...deps,approvalProof:async context=>proof(projectServerPollApprovalContext(context,1)),claim:async(...args)=>{actualStart=args[2];return deps.claim(...args)}}) +const delayedReceipt=await delayedExecutor.execute(delayed) +assert.equal(delayedReceipt.complete,true);assert.equal(nativeCalls.length,4) +assert(actualStart);assert.notEqual(actualStart.fullProof,delayedSource);assert.equal(actualStart.approvalProof,delayed.approvalProof) +for(const change of ["configuration","aba","server","catalog"]){ + version3State();const prepared=await v3.prepare(request()) + if(change==="configuration")state.settings.uptime!.retentionDays=15 + if(change==="aba"){state.settings.uptime!.retentionDays=15;state.settings.uptime!.pollConfigurationEpoch=randomUUID();state.settings.uptime!.retentionDays=14;state.settings.uptime!.pollConfigurationEpoch=randomUUID()} + if(change==="server")state.servers[0].name="external change" + if(change==="catalog")state.protections.push({kind:"database",id:"new"}) + await assert.rejects(v3.execute(prepared));assert.equal(claimed,undefined,change+" refuses before admission");assert.equal(nativeCalls.length,0) +} +version3State();const beforeRace=await v3.prepare(request()) +afterIntent=()=>{state.settings.uptime!.lastError="changed during execution"} +await assert.rejects(v3.execute(beforeRace));assert.equal(nativeCalls.length,0,"Runtime drift after the anchored start is still rejected by the exact full proof") +version3State();const unsupported=await v3.prepare(request()) +await assert.rejects(executor.execute(unsupported));assert.equal(claimed,undefined) + +console.log("Server poll inert foundations: four native reads, online/offline snapshots, name/cache/raw cleanup, complete global partition maintenance, full private proofs, intent/ACK fences, scope/revoke/deadline/unknown no replay passed") + +// Candidate-only in-memory transaction model. Genuine SQL evidence is separate. +let atomicFault="",groupSends=0,groupCommits=0,precommitChecks=0,precommitReads=0,inPrecommit=false +const coldV4=()=>{reset();for(const row of Object.values(state.settings))if(row)row.pollConfigurationEpoch=randomUUID();atomicFault="";groupSends=0;groupCommits=0;precommitChecks=0;precommitReads=0;inPrecommit=false} +const atomicDeps:ServerPollDependencies={...deps,readContext:async (...args)=>{ + if(inPrecommit)precommitReads++ + return deps.readContext(...args) +},approvalProof:async context=>proof(projectServerPollApprovalContext(context,1)),maintenanceGroup:async(group,options)=>{ + const originalPartitions=structuredClone(state.partitions),members:{row?:PollRow}[]=[] + let verifiedAck:{effects:{row?:PollRow}[]}|undefined + if(atomicFault==="skip-dispatch"){await options.verifyBeforeCommit(()=>({effects:[]}));throw Error("Unreachable skipped dispatch")} + await options.beforeDispatch();groupSends++ + try{ + for(const effect of group.effects){ + const row=effect.kind==="partition.ensure"?{namespace:"public",parent:effect.parent,name:effect.name,from:effect.from,to:effect.to,storage:{},opaque:"NEVER_ATOMIC_DEFAULT"}:undefined + const ack=row?{row}:{};members.push(ack);state=projectServerPollEffect(state,effect,ack) + } + if(atomicFault==="configuration-drift")state.settings.uptime!.retentionDays=37 + if(atomicFault==="runtime-drift")state.settings.uptime!.lastError="changed in group" + if(atomicFault==="foreign-drift")state.servers[1].password="changed in group" + if(atomicFault==="revoked")mode="revoked" + if(atomicFault==="expired")clock+=180001 + if(atomicFault!=="skip-precommit"){ + const observed = (context: ServerPollContext) => ({ + effects: group.effects.map((effect, index) => { + const expected = members[index].row + if (!expected) return {} + assert.equal(effect.kind, "partition.ensure") + const row = context.partitions.find(candidate => candidate.name === expected.name) + assert.equal(row?.opaque, expected.opaque) + return { row } + }), + }) + inPrecommit=true + try{ + verifiedAck=await options.verifyBeforeCommit(observed);precommitChecks++ + if(atomicFault==="double-precommit")await options.verifyBeforeCommit(observed) + }finally{inPrecommit=false} + } + groupCommits++ + }catch(error){state.partitions=originalPartitions;throw error} + if(atomicFault==="lost-commit-ack")throw Error("Inert lost ack; genuine wire fault is tested separately") + return verifiedAck??{effects:members} +}} +const atomicExecutor=createServerPollExecution(atomicDeps) +coldV4();const atomicPlan=await atomicExecutor.prepare(request()),atomicBefore=structuredClone(state) +assert.equal(atomicPlan.version,4);assert.equal(atomicPlan.maintenanceGroupSize,8) +assert.equal(atomicPlan.mutationCost,atomicPlan.maintenance.length+8) +const atomicReceipt=await atomicExecutor.execute(atomicPlan) +assert(atomicReceipt.complete&&atomicReceipt.maintenanceComplete) +assert.equal(nativeCalls.length,4);assert.equal(effects.length,4) +assert.equal(groupSends,Math.ceil(atomicPlan.maintenance.length/8));assert.equal(groupCommits,groupSends);assert.equal(precommitChecks,groupSends) +assert.equal(precommitReads,groupSends,"One full post-DDL read serves observed defaults and independent precommit proof") +assert.equal(atomicReceipt.confirmedPublications,groupSends+4) +assert.equal(intents.length,groupSends+8);assert.equal(acks.length,intents.length) +assert.deepEqual(state.settings,atomicBefore.settings);assert.deepEqual(state.servers[1],atomicBefore.servers[1]);assert.deepEqual(state.protections,atomicBefore.protections) +assert(!JSON.stringify(atomicReceipt).includes("NEVER_")) +for(const fault of ["configuration-drift","runtime-drift","foreign-drift","revoked","expired","double-precommit"]){ + coldV4();const prepared=await atomicExecutor.prepare(request());const partitions=structuredClone(state.partitions);atomicFault=fault + await assert.rejects(atomicExecutor.execute(prepared),(error:unknown)=>{ + assert(error instanceof ServerPollExecutionError) + const log=pollErrorLog(error,request().receiptId,"servers.poll") + assert.deepEqual(log?.provenance.execution,{ + phase:"partition.atomic",boundary:"precommit",confirmedPublications:4,completedPublications:4, + }) + return true + }) + assert.equal(groupSends,1);assert.equal(groupCommits,0);assert.deepEqual(state.partitions,partitions) + assert.equal(completed,undefined);assert.equal(acks.length,8,"No grouped ACK is published before successful commit") + const previousSends=groupSends;await assert.rejects(atomicExecutor.execute(prepared));assert.equal(groupSends,previousSends) +} +for(const fault of ["skip-precommit","lost-commit-ack","skip-dispatch"]){ + coldV4();const prepared=await atomicExecutor.prepare(request());atomicFault=fault + await assert.rejects(atomicExecutor.execute(prepared), (error: unknown) => { + assert(error instanceof ServerPollExecutionError) + if (fault === "lost-commit-ack") { + const safe = carryPollError(new Error("sanitized MCP failure"), error, "poll.safe") + const log = pollErrorLog(safe, request().receiptId, "servers.poll") + assert(log) + assert.deepEqual(log.provenance.execution, { + phase: "partition.atomic", boundary: "after-precommit", confirmedPublications: 4, completedPublications: 4, + }) + assert(!JSON.stringify(log).includes("Inert lost ack")) + } + return true + }) + assert.equal(groupSends,fault==="skip-dispatch"?0:1);assert.equal(completed,undefined);assert.equal(acks.length,8) + const previousSends=groupSends;await assert.rejects(atomicExecutor.execute(prepared));assert.equal(groupSends,previousSends) +} +coldV4();const onlyV3=await v3.prepare(request());assert.equal(onlyV3.version,3) +await atomicExecutor.execute(onlyV3);assert.equal(groupSends,0);assert.equal(effects.length,onlyV3.maintenance.length+4,"Legacy v3 stays ungrouped even on a group-capable executor") +coldV4();const unsupportedAtomic=await atomicExecutor.prepare(request()) +await assert.rejects(v3.execute(unsupportedAtomic));assert.equal(claimed,undefined);assert.equal(nativeCalls.length,0) +console.log("Atomic engine components: all original V1/V2/V3 cases plus V4 complete cold manifest, unchanged logical quota, original four native reads, mandatory precommit full proof, runtime/config/foreign drift, revoke, expiry, missing/double check, lost ACK and no replay PASS; no SQL, registration or public admission") diff --git a/backend/src/services/server-poll-execution.ts b/backend/src/services/server-poll-execution.ts new file mode 100644 index 00000000..414e8601 --- /dev/null +++ b/backend/src/services/server-poll-execution.ts @@ -0,0 +1,193 @@ +import { capturePollError, carryCapturedPollError, recordPollExecutionError, type PollExecutionProgress } from "./server-poll-error-provenance.js" +/** Atomic maintenance is available only through an explicit private executor capability. */ +import {groupAtomicMaintenance,publicationLayout,projectAtomicMaintenance,type AtomicMaintenanceGroup,type AtomicMaintenanceAck} from "./server-poll-maintenance-groups.js" +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +import type { ServerPollBoundaryCoordinator } from "./server-poll-boundary.js" +import { SERVER_POLL_NATIVE_READS, ServerPollProblem, pollProblem, validateServerPollContext, planServerPollMaintenance, planServerPollMaintenanceV2, serverPollSnapshot, projectServerPollEffect, type ServerPollContext,type ServerPollNativeRead,type ServerPollEffect,type ServerPollAck,type PollRow } from "./server-poll-execution-model.js" + +export interface ServerPollGrant {global:boolean;serverIds:number[];authorityProof:string} +export interface ServerPollRequest {serverId:number;receiptId:string} +export interface ServerPollPlan {version:1|2|3|4;approvalProof?:string;maintenanceGroupSize?:number;requiresHuman:true;scope:"global";request:ServerPollRequest;around:string;beforeProof:string;authorityProof:string;bindingProof:string;maintenance:ServerPollEffect[];affectedServerIds:number[];mutationCost:number} +export interface ServerPollExecutionStart {fullProof:string;approvalProof:string} +export interface ServerPollReceipt {receiptId:string;configurationVerified:boolean;complete:boolean;maintenanceComplete:boolean;nativeReads:number;confirmedPublications:number;completedPublications:number;afterProof:string;observation:"online"|"offline";snapshot:PollRow} +export class ServerPollExecutionError extends ServerPollProblem { + constructor(code:"MUTATION_OUTCOME_UNKNOWN"|"VERIFICATION_FAILED",readonly phase:string,readonly nativeReads:number,readonly confirmedPublications:number,readonly completedPublications:number){super(code,"Server poll завершён не полностью; повтор без новой сверки запрещён")} +} +export interface ServerPollDependencies { + /** Private opt-in candidate; never derived from an external request. */ + maintenanceGroup?:(group:AtomicMaintenanceGroup,options:{manifest:ServerPollEffect[];receiptId:string;stage:number;deadlineAt:number;beforeDispatch:()=>Promise;verifyBeforeCommit:(observe:(context:ServerPollContext)=>AtomicMaintenanceAck)=>Promise})=>Promise + /** Fixed private session coordinator; never obtained from request data. */ + boundary?:ServerPollBoundaryCoordinator + now:()=>number + /** Checks owner/policy/generation/global+all-server admission/protections and + * cancellation. Must not read privileged sources before validating access. */ + authorize:(request:ServerPollRequest,affectedServerIds:number[],signal?:AbortSignal)=>Promise + /** Bounded complete authoritative reader; every internal SQL read must invoke + * beforeRead. First read only bounded catalogue IDs and refuse any ID outside + * permittedServerIds BEFORE loading private server/settings/partition rows. + * No implicit empty catalog/fallback after a provider failure. */ + readContext:(request:ServerPollRequest,beforeRead:()=>Promise,permittedServerIds:readonly number[],options:{signal?:AbortSignal;deadlineAt:number})=>Promise + /** A private, purpose-separated HMAC over canonical complete input. */ + proof:(value:unknown)=>Promise + /** Fixed authoritative reader capability; never supplied by plan JSON. */ + approvalProof?:(context:ServerPollContext)=>Promise + /** Future production implementation must own a cross-replica, DB-wide + * maintenance exclusion covering affected collectors/background writers and + * drain owned work. An in-memory mutex alone does not satisfy this contract. */ + withGlobalLock:(work:()=>Promise,options:{signal?:AbortSignal;deadlineAt:number})=>Promise + native:(read:ServerPollNativeRead,server:PollRow,options:{signal?:AbortSignal;deadlineAt:number;beforeDispatch:()=>Promise})=>Promise + /** Persist intent before invoking beforeDispatch; every actual publication, + * including individual DDL and cache invalidation, must invoke it immediately + * at dispatch. Return only after ACK; unknown dispatch outcomes must throw. */ + effect:(effect:ServerPollEffect,options:{receiptId:string;stage:number;deadlineAt:number;beforeDispatch:()=>Promise})=>Promise + intent:(value:{receiptId:string;stage:string;beforeProof:string;effectProof:string})=>Promise + acknowledge:(value:{receiptId:string;stage:string;resultProof:string;outcome:"confirmed"|"read-failed"})=>Promise + claim:(request:ServerPollRequest,bindingProof:string,start?:ServerPollExecutionStart)=>Promise<{state:"new"}|{state:"known";bindingProof:string;receipt:ServerPollReceipt}|{state:"unresolved";bindingProof:string}> + complete:(receipt:ServerPollReceipt,bindingProof:string)=>Promise +} +const input=(request:ServerPollRequest)=>{if(!Number.isSafeInteger(request.serverId)||request.serverId<=0||!/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i.test(request.receiptId))pollProblem("INVALID_INPUT","Некорректный server poll request")} +function grantScope(grant:ServerPollGrant,ids:number[]){if(!grant.global||ids.some(id=>!grant.serverIds.includes(id)))pollProblem("PERMISSION_DENIED","Полный server poll требует global scope и все затронутые серверы")} +function safeSnapshotReceipt(row:PollRow,privateSources:unknown):PollRow{ + const secrets=new Set() + const visit=(value:unknown,key="")=>{if(typeof value==="string"&&value.length&&/password|secret|private.?key|token|authorization/i.test(key))secrets.add(value);else if(Array.isArray(value))for(const item of value)visit(item,key);else if(value&&typeof value==="object")for(const [name,item]of Object.entries(value))visit(item,name)} + visit(privateSources) + return Object.fromEntries(Object.entries(row).filter(([key])=>["id","serverId","polledAt","status","latencyMs","rosVersion","boardName","uptime","cpuLoad","freeMemory","totalMemory","identityName"].includes(key)).map(([key,value])=>[key,typeof value==="string"?[...secrets].reduce((text,secret)=>text.replaceAll(secret,"[redacted]"),value):value])) +} +/** Inert injectable foundation ONLY. No production reader, SQL, native client, + * partition executor, route or MCP registration is wired by this module. + * Maintenance is awaited under the same authority instead of escaping into a + * detached background task; the snapshot alone is never a full-run receipt. */ +export function createServerPollExecution(deps:ServerPollDependencies){ + const capture=async(request:ServerPollRequest,ids:number[],signal?:AbortSignal,deadline=Infinity,forApproval=false)=>{ + const required=[...new Set([request.serverId,...ids])],initial=await deps.authorize(request,required,signal) + grantScope(initial,required) + if(typeof initial.authorityProof!=="string"||!/^[a-f0-9]{64}$/.test(initial.authorityProof))pollProblem("PERMISSION_DENIED","Не подтверждён private authority proof server poll") + const local=()=>{signal?.throwIfAborted();if(deps.now()>=deadline)pollProblem("ABORTED","Истёк deadline server poll")} + const access=deps.boundary?.fence(required,initial.authorityProof,{signal,deadlineAt:deadline})??(async()=>{local();const grant=await deps.authorize(request,required,signal);grantScope(grant,required);if(grant.authorityProof!==initial.authorityProof)pollProblem("STATE_CONFLICT","Допуск poll изменился");local()}) + local();const context=await deps.readContext(request,access,initial.serverIds,{signal,deadlineAt:deadline});local();validateServerPollContext(context,request.serverId) + const all=context.servers.map(row=>Number(row.id)).sort((a,b)=>a-b),allGrant=await deps.authorize(request,all,signal);grantScope(allGrant,all) + if(allGrant.authorityProof!==initial.authorityProof)pollProblem("STATE_CONFLICT","Допуск poll изменился после полного каталога") + local() + const approvalProof=forApproval?await deps.approvalProof?.(context):undefined + if(forApproval&&(typeof approvalProof!=="string"||!/^[a-f0-9]{64}$/.test(approvalProof)))pollProblem("STATE_UNAVAILABLE","Не подтверждена версия конфигурации server poll") + local() + return {context,ids:all,proof:await deps.proof(context),authorityProof:initial.authorityProof,approvalProof} + } + const prepare=async(request:ServerPollRequest,signal?:AbortSignal):Promise=>{ + input(request);const around=new Date(deps.now()).toISOString(),before=await capture(request,[],signal,deps.now()+45000,!!deps.approvalProof),maintenance=planServerPollMaintenanceV2(before.context,around) + if(deps.maintenanceGroup&&!before.approvalProof)pollProblem("STATE_UNAVAILABLE","Atomic maintenance requires a current configuration approval proof") + const core={version:(deps.maintenanceGroup?4:before.approvalProof?3:2) as 2|3|4,...(deps.maintenanceGroup?{maintenanceGroupSize:8}:{}),...(before.approvalProof?{approvalProof:before.approvalProof}:{}),requiresHuman:true as const,scope:"global" as const,request:{...request},around,beforeProof:before.proof,authorityProof:before.authorityProof,maintenance,affectedServerIds:before.ids,mutationCost:maintenance.length+4+4} + return {...core,bindingProof:await deps.proof(core)} + } + const execute=async(plan:ServerPollPlan,signal?:AbortSignal):Promise=>{ + const executionDeadline=deps.now()+SERVER_POLL_WALL_BUDGET_MS + signal=AbortSignal.any([...(signal?[signal]:[]),AbortSignal.timeout(SERVER_POLL_WALL_BUDGET_MS)]) + return deps.withGlobalLock(async()=>{ + input(plan.request) + if(!Array.isArray(plan.maintenance)||plan.maintenance.length>2000||!Array.isArray(plan.affectedServerIds)||plan.affectedServerIds.length>1000)pollProblem("INVALID_INPUT","Превышен полный бюджет плана server poll") + const {bindingProof,...core}=plan + const epochPlan=plan.version===3||plan.version===4 + if((plan.version!==1&&plan.version!==2&&plan.version!==3&&plan.version!==4)||(epochPlan?typeof plan.approvalProof!=="string"||!/^[a-f0-9]{64}$/.test(plan.approvalProof)||!deps.approvalProof:Object.hasOwn(plan,"approvalProof"))||plan.requiresHuman!==true||plan.scope!=="global"||bindingProof!==await deps.proof(core)||plan.mutationCost!==plan.maintenance.length+8)pollProblem("STATE_CONFLICT","Изменён полный план server poll") + publicationLayout(plan) + if(plan.version===4&&!deps.maintenanceGroup)pollProblem("STATE_UNAVAILABLE","Atomic maintenance executor is unavailable") + const executionAround=new Date(deps.now()).toISOString(),deadline=executionDeadline,read=(forApproval=false)=>capture(plan.request,plan.affectedServerIds,signal,deadline,forApproval) + let current=await read(epochPlan) + if(current.authorityProof!==plan.authorityProof)pollProblem("STATE_CONFLICT","Допуск server poll изменился после подготовки") + if(epochPlan&&(current.approvalProof!==plan.approvalProof||await deps.proof(current.ids)!==await deps.proof(plan.affectedServerIds)||await deps.proof(planServerPollMaintenanceV2(current.context,executionAround))!==await deps.proof(plan.maintenance)))pollProblem("STATE_CONFLICT","Изменилась конфигурация или окно обслуживания после подтверждения poll") + const claim=await deps.claim(plan.request,bindingProof,epochPlan?{fullProof:current.proof,approvalProof:current.approvalProof!}:undefined) + if(claim.state!=="new"){ + if(claim.bindingProof!==bindingProof)pollProblem("STATE_CONFLICT","Receipt poll принадлежит другому плану") + if(claim.state==="known"){if(claim.receipt.receiptId!==plan.request.receiptId||!claim.receipt.complete||!claim.receipt.maintenanceComplete)pollProblem("STATE_CONFLICT","Повреждён сохранённый receipt poll");return {...claim.receipt,configurationVerified:claim.receipt.afterProof===current.proof}} + pollProblem("MUTATION_OUTCOME_UNKNOWN","Незавершённый server poll не повторяется автоматически") + } + if((!epochPlan&¤t.proof!==plan.beforeProof)||await deps.proof(current.ids)!==await deps.proof(plan.affectedServerIds)||await deps.proof((plan.version===1?planServerPollMaintenance:planServerPollMaintenanceV2)(current.context,executionAround))!==await deps.proof(plan.maintenance))pollProblem("STATE_CONFLICT","Изменились server/settings/partition источники или maintenance window poll") + let nativeReads=0,confirmedPublications=0,completedPublications=0,dispatchStarted=false + let phase: PollExecutionProgress["phase"] = "native" + let boundary: PollExecutionProgress["boundary"] = "gate" + const access=async()=>{signal?.throwIfAborted();if(deps.now()>=deadline)pollProblem("ABORTED","Истёк deadline server poll");const grant=await deps.authorize(plan.request,plan.affectedServerIds,signal);grantScope(grant,plan.affectedServerIds);if(grant.authorityProof!==plan.authorityProof)pollProblem("STATE_CONFLICT","Допуск server poll изменился");signal?.throwIfAborted()} + const gate=async()=>{const fresh=await read();if(fresh.proof!==current.proof)pollProblem("STATE_CONFLICT","Изменился полный контекст server poll перед эффектом");await access()} + const nativeGate=async()=>{await gate()} + try{ + const start=deps.now(),server=current.context.servers.find(row=>row.id===plan.request.serverId)! + // Native reads can overlap. Their durable journal writes share one storage lock. + let journalTail=Promise.resolve() + const writeNativeJournal=(write:()=>Promise)=>{ + journalTail=journalTail.then(async()=>{ + await access() + await write() + }) + return journalTail + } + const observations=await Promise.allSettled(SERVER_POLL_NATIVE_READS.map(async key=>{ + await writeNativeJournal(async()=>deps.intent({receiptId:plan.request.receiptId,stage:"native."+key,beforeProof:current.proof,effectProof:await deps.proof({read:key,source:server})}));await nativeGate() + let dispatched=false + let result:unknown,readFailed=false,nativeDiagnostic:ReturnType|undefined + try{result=await deps.native(key,structuredClone(server),{signal,deadlineAt:deadline,beforeDispatch:async()=>{if(dispatched)pollProblem("STATE_CONFLICT","Повторный dispatch native poll запрещён");await nativeGate();if(dispatched)pollProblem("STATE_CONFLICT","Повторный dispatch native poll запрещён");dispatched=true;nativeReads++}})}catch(error){if(error instanceof ServerPollProblem||signal?.aborted)throw error;nativeDiagnostic=capturePollError(error);readFailed=true} + if(!dispatched)throw carryCapturedPollError(new ServerPollProblem("STATE_UNAVAILABLE","Native transport пропустил dispatch fence"),nativeDiagnostic,"native.fence") + await writeNativeJournal(async()=>{ + if(!readFailed){let bytes:number;try{bytes=Buffer.byteLength(JSON.stringify(result))}catch{pollProblem("STATE_UNAVAILABLE","Некорректный native ответ poll")};if(bytes>1048576)pollProblem("STATE_UNAVAILABLE","Превышен native budget poll")} + await deps.acknowledge({receiptId:plan.request.receiptId,stage:"native."+key,resultProof:await deps.proof(readFailed?{unavailable:true}:result),outcome:readFailed?"read-failed":"confirmed"}) + }) + return readFailed?undefined:result + })) + const failed=observations.find(result=>result.status==="rejected");if(failed?.status==="rejected")throw failed.reason + await gate() + const values=Object.fromEntries(SERVER_POLL_NATIVE_READS.map((key,index)=>[key,(observations[index]as PromiseFulfilledResult).value]))as Record + let snapshot:PollRow={serverId:plan.request.serverId,polledAt:executionAround,status:"offline",latencyMs:null} + if(Object.values(values).every(value=>value!==undefined))snapshot=serverPollSnapshot(plan.request.serverId,executionAround,values,Math.max(0,deps.now()-start)) + let stage=0,inserted:PollRow|undefined + const publish=async(effect:ServerPollEffect|AtomicMaintenanceGroup)=>{ + phase=effect.kind;boundary="gate";await gate() + boundary="journal-intent";await deps.intent({receiptId:plan.request.receiptId,stage:"publish."+stage,beforeProof:current.proof,effectProof:await deps.proof(effect)}) + let sent=false + const atomic=effect.kind==="partition.atomic" + const project=(ack:ServerPollAck|AtomicMaintenanceAck)=>atomic?projectAtomicMaintenance(current.context,plan.maintenance,effect as AtomicMaintenanceGroup,ack as AtomicMaintenanceAck):projectServerPollEffect(current.context,effect as ServerPollEffect,ack as ServerPollAck) + const options={receiptId:plan.request.receiptId,stage,deadlineAt:deadline,beforeDispatch:async()=>{ + boundary="dispatch-fence" + if(sent)pollProblem("STATE_CONFLICT","Повторный dispatch publication poll запрещён");await gate();if(sent)pollProblem("STATE_CONFLICT","Повторный dispatch publication poll запрещён") + sent=true;dispatchStarted=true;boundary="effect" + }} + let precommitStarted=false,verifiedBeforeCommit=false + boundary="dispatch-fence" + const ack:ServerPollAck|AtomicMaintenanceAck=atomic?await deps.maintenanceGroup!(structuredClone(effect as AtomicMaintenanceGroup),{...options,manifest:structuredClone(plan.maintenance),verifyBeforeCommit:async observe=>{ + boundary="precommit" + if(precommitStarted||!sent)pollProblem("STATE_CONFLICT","Invalid atomic precommit verification boundary") + precommitStarted=true + const fresh=await read() + const value=structuredClone(observe(structuredClone(fresh.context))) + const expected=project(value) + if(await deps.proof(expected)!==fresh.proof)pollProblem("VERIFICATION_FAILED","Atomic full source changed before COMMIT") + await access();verifiedBeforeCommit=true;boundary="after-precommit";return value + }}):await deps.effect(structuredClone(effect as ServerPollEffect),options) + if(!sent)pollProblem("STATE_UNAVAILABLE","Publication пропустила dispatch fence") + if(atomic&&!verifiedBeforeCommit)pollProblem("STATE_UNAVAILABLE","Atomic executor skipped mandatory precommit full source verification") + confirmedPublications++ + boundary="independent-readback" + const projected=project(ack),after=await read() + if(await deps.proof(projected)!==after.proof)throw new ServerPollExecutionError("VERIFICATION_FAILED",phase,nativeReads,confirmedPublications,completedPublications) + boundary="journal-ack" + await access();await deps.acknowledge({receiptId:plan.request.receiptId,stage:"publish."+stage,resultProof:after.proof,outcome:"confirmed"}) + current=after;completedPublications++;stage++ + if(effect.kind==="snapshot.insert")inserted=(ack as ServerPollAck).row + } + if(snapshot.status==="online"&&snapshot.identityName!==server.name){await publish({kind:"server.name",serverId:plan.request.serverId,name:String(snapshot.identityName),updatedAt:executionAround});await publish({kind:"flow-cache.invalidate",serverId:plan.request.serverId})} + await publish({kind:"snapshot.insert",snapshot}) + if(!inserted)pollProblem("VERIFICATION_FAILED","Не подтверждён snapshot receipt") + const old=current.context.snapshots.filter(row=>row.serverId===plan.request.serverId&&String(row.polledAt)row.id)}) + if(plan.version===4){for(const group of groupAtomicMaintenance(plan.maintenance))await publish(group)}else for(const effect of plan.maintenance)await publish(effect) + phase="receipt";boundary="gate";await gate() + boundary="receipt-write" + const safeSnapshot=safeSnapshotReceipt(inserted,{context:current.context,observations:values}) + const receipt:ServerPollReceipt={receiptId:plan.request.receiptId,configurationVerified:true,complete:true,maintenanceComplete:true,nativeReads,confirmedPublications,completedPublications,afterProof:current.proof,observation:snapshot.status==="online"?"online":"offline",snapshot:safeSnapshot} + await deps.complete(receipt,bindingProof);boundary="receipt-authority";await access();return receipt + }catch(error){ + const progress = { phase, boundary, confirmedPublications, completedPublications } + if(error instanceof ServerPollExecutionError)throw recordPollExecutionError(error,error,progress) + if(dispatchStarted)throw recordPollExecutionError(new ServerPollExecutionError("MUTATION_OUTCOME_UNKNOWN",phase,nativeReads,confirmedPublications,completedPublications),error,progress) + throw error + } + },{signal,deadlineAt:executionDeadline}) + } + return {prepare,execute} +} diff --git a/backend/src/services/server-poll-history-reader.test.ts b/backend/src/services/server-poll-history-reader.test.ts new file mode 100644 index 00000000..30373e6f --- /dev/null +++ b/backend/src/services/server-poll-history-reader.test.ts @@ -0,0 +1,171 @@ +/** Reader orchestration components: real original-binding AES/HMAC, SQL and + * already-verified archive dependency doubles. The archive's own tests cover + * its fixed decrypt/history/terminal pipeline; these are not public SQL proofs. */ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHmac, randomBytes, randomUUID } from "node:crypto" +import { test } from "node:test" +import { fingerprint } from "../modules/mcp/security.js" +import { readRetiredServerPollHistoryForTests, readServerPollArchivedHistoryForTests } from "./server-poll-history-reader.js" +import type { openServerPollRetiredArchive } from "./server-poll-retired-archive.js" +import type { openServerPollTerminalArchive, ServerPollArchiveSource } from "./server-poll-terminal-archive.js" + +function fixture(){ + const now=Date.now(),secret=randomBytes(32),connectionId=randomUUID(),receiptId=randomUUID(),executionId=randomUUID() + const events:string[]=[],calls:{sql:string;values:unknown[]}[]=[] + let revoked=false,mode="normal",opens=0,decrypts=0 + const denied=Object.assign(Error("Current reader permission revoked"),{code:"PERMISSION_DENIED"}) + const proof=async(value:unknown)=>createHmac("sha256",secret).update(fingerprint(value)).digest("hex") + const encrypt=(text:string)=>{ + const nonce=randomBytes(12),cipher=createCipheriv("aes-256-gcm",secret,nonce),value=Buffer.concat([cipher.update(text),cipher.final()]) + return Buffer.concat([nonce,cipher.getAuthTag(),value]).toString("base64") + } + const decrypt=async(text:string)=>{ + events.push("decrypt");decrypts++ + const value=Buffer.from(text,"base64"),cipher=createDecipheriv("aes-256-gcm",secret,value.subarray(0,12));cipher.setAuthTag(value.subarray(12,28)) + const result=Buffer.concat([cipher.update(value.subarray(28)),cipher.final()]).toString("utf8") + if(mode==="revoke-decrypt")revoked=true + return result + } + const owner="original-owner",binding={version:1,operation:"servers.poll",ownerId:owner,issuedAt:now-600000,expiresAt:now-300000, + principal:{connectionId,credentialId:randomUUID(),credentialExpiresAt:new Date(now-200000).toISOString()},plan:{request:{serverId:1,receiptId},requiresHuman:true}} + let input:Record + const originalAdmission={receipt_id:receiptId,execution_id:executionId,connection_id:connectionId,owner_id:owner,storage_state:"full", + compact_at:null,archive_bytes:null,original_activity_at:null,binding_proof:"a".repeat(64),context_proof:"b".repeat(64),future_opaque:{preserve:true}} + let original:ServerPollArchiveSource,row:Record + const authorize=async()=>{events.push("authorize");if(revoked)throw denied} + const deps={now:()=>now,decrypt,bindingProof:proof, + query:async(sql:string,values:unknown[])=>{ + events.push("query");calls.push({sql,values});if(mode==="revoke-sql")revoked=true + return mode==="missing"||mode==="late-gc"&&calls.length===2?[]:mode==="duplicate"?[structuredClone(row),structuredClone(row)]:[structuredClone(row)] + }, + openArchive:async()=>{ + events.push("archive");opens++;if(mode==="bad-archive")throw Error("Private archive proof failed") + if(mode==="revoke-archive")revoked=true + if(mode==="late-source") (row.current_terminal as ServerPollArchiveSource["terminal"]).operation.result={changed:true} + return {source:structuredClone(original),sourceText:"private full archive",metadata:{}} as Awaited> + }, + } + const setup=async()=>{ + input={serverId:1,receiptId,inputDigest:await proof(binding),bindingCiphertext:encrypt(JSON.stringify(binding))} + const terminal={operation:{id:executionId,state:"applied",verification:"passed",result:{confirmedWrites:1}}, + plan:{operation_id:"servers.poll",evidence:{sealed:encrypt(JSON.stringify({steps:[{kind:"service",service:"servers.poll",input}]}))}}, + step:{operation_id:executionId,step_index:0,state:"verified",result:{receiptId,complete:true}}} + original={admission:originalAdmission,journal:{execution:{},intents:[],acks:[],receipt:{},quarantines:[]},terminal} + row={receipt_id:receiptId,execution_id:executionId,connection_id:connectionId,owner_id:owner, + current_admission:{...structuredClone(originalAdmission),storage_state:"compacted",compact_at:new Date(now-100000).toISOString(),archive_bytes:100,original_activity_at:new Date(now-200000).toISOString()}, + current_terminal:structuredClone(terminal)} + } + const run=()=>readServerPollArchivedHistoryForTests(input,authorize,{deadlineAt:now+20000},deps) + return {setup,run,events,calls,binding,denied,encrypt,proof,raw:()=>input,row:()=>row,source:()=>original,mode:(value:string)=>{mode=value},revoke:()=>{revoked=true},counts:()=>({opens,decrypts}),now,deps,authorize} +} + +test("expired original identity reads a verified archive under current authority without constructing a live session",async()=>{ + const f=fixture();await f.setup();assert.equal(await f.run(),true) + assert.deepEqual(f.calls[0].values,[f.raw().receiptId,f.binding.principal.connectionId,f.binding.ownerId]) + const sql=f.calls[0].sql + assert.match(sql,/FROM public\.server_poll_archives/);assert.match(sql,/a\.storage_state='compacted'/);assert.match(sql,/LIMIT 2$/) + assert.match(sql,/count\(\*\).*mcp_operation_steps/);assert.match(sql,/to_jsonb\(o\).*to_jsonb\(p\).*to_jsonb\(s\)/) + assert(!/\b(?:UPDATE|INSERT|DELETE|LOCK|FOR\s+UPDATE)\b/i.test(sql));assert(!sql.includes("mcp_credentials"));assert(!sql.includes("mcp_grants")) + for(const event of ["query","archive","decrypt"])for(const [index,value]of f.events.entries())if(value===event){assert.equal(f.events[index-1],"authorize");assert.equal(f.events[index+1],"authorize")} + assert.equal(f.events.at(-1),"authorize");assert.equal(f.counts().opens,1) +}) + +for(const mode of ["missing","duplicate","bad-archive","digest","owner","input","current-evidence","current-result","admission-opaque","malformed-binding","late-source","late-gc"]) + test("historical reader fails safely on "+mode,async()=>{ + const f=fixture();await f.setup();f.mode(mode) + if(mode==="digest")f.raw().inputDigest="0".repeat(64) + if(mode==="owner")f.row().owner_id="other" + if(mode==="input"){ + f.source().terminal.plan.evidence={sealed:f.encrypt(JSON.stringify({steps:[{kind:"service",service:"servers.poll",input:{...f.raw(),inputDigest:"0".repeat(64)}}]}))} + f.row().current_terminal=structuredClone(f.source().terminal) + } + if(mode==="current-evidence")(f.row().current_terminal as ServerPollArchiveSource["terminal"]).plan.evidence={sealed:"tampered"} + if(mode==="current-result")(f.row().current_terminal as ServerPollArchiveSource["terminal"]).step.result={forged:true} + if(mode==="admission-opaque")(f.row().current_admission as Record).future_opaque={preserve:false} + if(mode==="malformed-binding")f.raw().bindingCiphertext="invalid" + assert.equal(await f.run(),false) + if(mode==="digest"||mode==="malformed-binding")assert.equal(f.calls.length,0) + }) + +for(const mode of ["revoke-decrypt","revoke-sql","revoke-archive"]) + test("current authority revocation after "+mode+" propagates and cannot publish success",async()=>{ + const f=fixture();await f.setup();f.mode(mode) + await assert.rejects(f.run(),error=>error===f.denied) + if(mode==="revoke-decrypt")assert.equal(f.calls.length,0) + }) + +test("already revoked current authority refuses before crypto/SQL and deadline/cancel never becomes historical authority",async()=>{ + const f=fixture();await f.setup();f.revoke();await assert.rejects(f.run(),error=>error===f.denied) + assert.deepEqual(f.counts(),{opens:0,decrypts:0});assert.equal(f.calls.length,0) + const g=fixture();await g.setup() + assert.equal(await readServerPollArchivedHistoryForTests(g.raw(),g.authorize,{deadlineAt:g.now},g.deps),false) + assert.equal(await readServerPollArchivedHistoryForTests(g.raw(),g.authorize,{deadlineAt:g.now+20000,signal:AbortSignal.abort()},g.deps),false) + assert.equal(g.calls.length,0) +}) + +test("caller mutation while decrypt awaits cannot replace authenticated lookup identity",async()=>{ + const f=fixture();await f.setup();const raw=structuredClone(f.raw()) + const decrypt=f.deps.decrypt + assert.equal(await readServerPollArchivedHistoryForTests(raw,f.authorize,{deadlineAt:f.now+20000},{...f.deps,decrypt:async value=>{ + raw.receiptId=randomUUID();return decrypt(value) + }}),true) + assert.equal(f.calls[0].values[0],f.raw().receiptId) +}) + +function retiredFixture() { + const now = Date.now(), operationId = randomUUID(), connectionId = randomUUID(), planId = randomUUID(), ownerId = "retired-owner" + const identity = { operationId, connectionId, ownerId } + const row = { plan_id: planId, target_server_ids: ["2", "1"], gc_not_before: "2026-10-31T01:00:00.123456Z" } + const failure = Error("authority revoked") + let queries = 0, opens = 0, checks = 0 + let mode = "" + const run = () => readRetiredServerPollHistoryForTests(identity, async () => { + checks++ + if (mode === "revoked-after-open" && opens) throw failure + }, { deadlineAt: now + 1000 }, { + now: () => now, + query: async (sql, parameters) => { + queries++ + assert.deepEqual(parameters, [operationId, connectionId, ownerId]) + assert.match(sql, /mcp_operation_replay_tombstones/) + assert.match(sql, /r\.version=2/) + assert.match(sql, /NOT EXISTS\(SELECT 1 FROM public\.mcp_operations/) + if (mode === "gone" && queries === 2) return [] + if (mode === "changed" && queries === 2) return [{ ...row, target_server_ids: ["1"] }] + return [row] + }, + open: async () => { + opens++ + if (mode === "bad-proof") throw Error("private proof failure") + return { metadata: { execution_id: operationId, connection_id: connectionId, owner_id: ownerId }, + history: { planId, affectedServerIds: [1, 2], gcNotBefore: "2026-10-31T01:00:00.123456Z" } } as Awaited> + }, + }) + return { row, failure, run, mode: (value: string) => { mode = value }, counts: () => ({ queries, opens, checks }) } +} + +test("retired lookup verifies a current tombstone, archive proof and a second SQL snapshot before returning only scope", async () => { + const f = retiredFixture() + assert.deepEqual(await f.run(), { affectedServerIds: [1, 2] }) + assert.deepEqual(f.counts(), { queries: 2, opens: 1, checks: 4 }) +}) + +test("retired lookup refuses proof, plan, scope or retention drift and GC between reads", async () => { + for (const mode of ["bad-proof", "changed", "gone"]) { + const f = retiredFixture() + f.mode(mode) + assert.equal(await f.run(), undefined) + } + for (const row of [{ plan_id: randomUUID() }, { target_server_ids: ["1", "1"] }, { gc_not_before: "2026-10-31T01:00:00.123457Z" }]) { + const f = retiredFixture() + Object.assign(f.row, row) + assert.equal(await f.run(), undefined) + } +}) + +test("retired lookup propagates exact current authorization failure after archive open", async () => { + const f = retiredFixture() + f.mode("revoked-after-open") + await assert.rejects(f.run(), error => error === f.failure) + assert.equal(f.counts().queries, 1) +}) diff --git a/backend/src/services/server-poll-history-reader.ts b/backend/src/services/server-poll-history-reader.ts new file mode 100644 index 00000000..21559538 --- /dev/null +++ b/backend/src/services/server-poll-history-reader.ts @@ -0,0 +1,171 @@ +/** Fixed read-only archive lookup. Its current-authority callback is supplied + * by the already authorized global reconciliation action, never request JSON. + * Original credentials authenticate history only; they are never renewed. */ +import { z } from "zod" +import { decryptSecret, fingerprint, privateSecretFingerprint } from "../modules/mcp/security.js" +import { canonicalPollArchiveTimestamp, openServerPollTerminalArchive } from "./server-poll-terminal-archive.js" +import { openServerPollRetiredArchive } from "./server-poll-retired-archive.js" + +export interface ServerPollHistoryReadWindow { deadlineAt: number; signal?: AbortSignal } +type Query = (text: string, values: unknown[], options: ServerPollHistoryReadWindow) => Promise[]> +interface Dependencies { + query: Query; decrypt: typeof decryptSecret; bindingProof(value: unknown): Promise + openArchive: typeof openServerPollTerminalArchive; now(): number +} +const uuid=z.string().uuid(),hash=z.string().regex(/^[a-f0-9]{64}$/),sid=z.number().int().positive().safe() +const inputSchema=z.object({serverId:sid,receiptId:uuid,inputDigest:hash,bindingCiphertext:z.string().min(1).max(2097152)}).strict() +const bindingSchema=z.object({version:z.literal(1),operation:z.literal("servers.poll"), + principal:z.object({connectionId:uuid,credentialId:uuid,credentialExpiresAt:z.string().datetime()}).strict(), + ownerId:z.string().min(1).max(200),issuedAt:z.number().int().nonnegative().safe(),expiresAt:z.number().int().positive().safe(), + plan:z.object({request:z.object({serverId:sid,receiptId:uuid}).strict()}).passthrough()}).strict() +const QUERY=`SELECT r.version,r.receipt_id,r.execution_id,r.connection_id,r.owner_id,r.binding_proof,r.context_proof, + r.source_digest,r.source_sha256,r.terminal_digest,r.decoded_bytes,r.compressed_bytes,r.archive_proof, + to_char(r.original_activity_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_activity_at, + to_char(r.original_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_expires_at, + a.retention_days,a.safety_ms,octet_length(r.ciphertext) AS ciphertext_bytes, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8')),'hex') AS ciphertext_sha256, + CASE WHEN octet_length(r.ciphertext)<=131072 THEN r.ciphertext END AS ciphertext, + CASE WHEN octet_length(to_jsonb(a)::text)<=1048576 THEN to_jsonb(a) END AS current_admission, + CASE WHEN octet_length(to_jsonb(o)::text)+octet_length(to_jsonb(p)::text)+octet_length(to_jsonb(s)::text)<=16777216 + THEN jsonb_build_object('operation',to_jsonb(o),'plan',to_jsonb(p),'step',to_jsonb(s)) END AS current_terminal + FROM public.server_poll_archives r JOIN public.server_poll_storage_admissions a USING(receipt_id) + JOIN public.mcp_connections c ON c.id=a.connection_id AND c.owner_id=a.owner_id + JOIN public.mcp_operations o ON o.id=a.execution_id AND o.connection_id=a.connection_id + JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + WHERE a.receipt_id=$1 AND a.connection_id=$2 AND a.owner_id=$3 AND a.identity_kind='mcp' AND a.storage_state='compacted' + AND r.execution_id=a.execution_id AND r.connection_id=a.connection_id AND r.owner_id=a.owner_id + AND r.binding_proof=a.binding_proof AND r.context_proof=a.context_proof AND r.ciphertext_bytes=a.archive_bytes AND r.ciphertext_bytes=octet_length(r.ciphertext) + AND r.original_activity_at=a.original_activity_at AND r.original_expires_at=a.authority_expires_at + AND o.state='applied' AND o.verification='passed' AND o.error_code IS NULL AND o.finished_at IS NOT NULL + AND p.operation_id='servers.poll' AND p.operation_version=1 AND p.state='completed' AND p.approved_by IS NOT NULL + AND s.state='verified' AND s.server_id IS NULL AND s.finished_at IS NOT NULL + AND (SELECT count(*) FROM public.mcp_operation_steps all_steps WHERE all_steps.operation_id=o.id)=1 LIMIT 2` +const fail=():never=>{throw Error("Archived poll history is unavailable")} +const record=(value:unknown):value is Record=>!!value&&typeof value==="object"&&!Array.isArray(value) +function withoutPhase(raw:Record){ + const result={...raw};for(const name of ["storage_state","compact_at","archive_bytes","original_activity_at"])delete result[name] + return result +} +async function read(raw:Record,currentAuthorize:()=>Promise,rawWindow:ServerPollHistoryReadWindow,deps:Dependencies):Promise{ + let authFailed=false,authError:unknown + try{ + const input=inputSchema.parse(structuredClone(raw)),options=Object.freeze({...rawWindow,deadlineAt:Math.min(rawWindow.deadlineAt,deps.now()+180000)}) + const live=()=>{if(!Number.isSafeInteger(options.deadlineAt)||deps.now()>=options.deadlineAt||options.signal?.aborted)fail()} + const authorize=async()=>{live();try{await currentAuthorize()}catch(error){authFailed=true;authError=error;throw error};live()} + const decrypt=async(text:string,limit:number)=>{ + if(typeof text!=="string"||Buffer.byteLength(text)>limit*2)fail() + await authorize();const opened=await deps.decrypt(text);await authorize() + if(Buffer.byteLength(opened)>limit)fail();return JSON.parse(opened) as unknown + } + const binding=bindingSchema.parse(await decrypt(input.bindingCiphertext,1048576)) + await authorize();const proof=await deps.bindingProof(binding);await authorize() + if(proof!==input.inputDigest||binding.plan.request.receiptId!==input.receiptId||binding.plan.request.serverId!==input.serverId|| + binding.expiresAt<=binding.issuedAt||binding.issuedAt>deps.now()||binding.expiresAt>Date.parse(binding.principal.credentialExpiresAt))fail() + await authorize() + const rows=await deps.query(QUERY,[input.receiptId,binding.principal.connectionId,binding.ownerId],options) + await authorize() + if(rows.length!==1)return false + const row=structuredClone(rows[0]) + if(row.receipt_id!==input.receiptId||row.connection_id!==binding.principal.connectionId||row.owner_id!==binding.ownerId|| + !record(row.current_admission)||!record(row.current_terminal))fail() + await authorize() + // One bounded private decrypt/history/terminal verification boundary. It + // performs no SQL, native effects or publication while authority is checked. + const opened=await deps.openArchive(row,deps.now()) + await authorize() + const original=opened.source,current=row.current_admission as Record + if(original.admission.storage_state!=="full"||current.storage_state!=="compacted"|| + fingerprint(withoutPhase(original.admission))!==fingerprint(withoutPhase(current))|| + fingerprint(original.terminal)!==fingerprint(row.current_terminal))fail() + const originalEvidence=original.terminal.plan.evidence + if(!record(originalEvidence)||typeof originalEvidence.sealed!=="string")return fail() + const evidence=await decrypt(originalEvidence.sealed,2097152) + if(!record(evidence)||!Array.isArray(evidence.steps)||evidence.steps.length!==1||!record(evidence.steps[0]))return fail() + const step=evidence.steps[0] + if(step.kind!=="service"||step.service!=="servers.poll"||fingerprint(step.input)!==fingerprint(input))fail() + await authorize() + const fresh=await deps.query(QUERY,[input.receiptId,binding.principal.connectionId,binding.ownerId],options) + await authorize() + if(fresh.length!==1||fingerprint(fresh[0])!==fingerprint(row))fail() + await authorize() + return true + }catch{if(authFailed)throw authError;return false} +} +const production:Dependencies={ + query:async(text,values,options)=>(await import("../modules/mcp/server-poll-production.js")).queryServerPollMcpMetadata(text,values,options), + decrypt:decryptSecret,bindingProof:value=>privateSecretFingerprint(fingerprint(value),"server-poll-mcp-binding-v1"), + openArchive:openServerPollTerminalArchive,now:Date.now, +} +export const readServerPollArchivedHistory=(raw:Record,authorize:()=>Promise,options:ServerPollHistoryReadWindow)=>read(raw,authorize,options,production) +/** Explicit component-only composition. No public input selects these hooks. */ +export const readServerPollArchivedHistoryForTests=(raw:Record,authorize:()=>Promise,options:ServerPollHistoryReadWindow,deps:Dependencies)=>read(raw,authorize,options,deps) + +const RETIRED_QUERY = `SELECT r.version,r.receipt_id,r.execution_id,r.connection_id,r.owner_id,r.binding_proof,r.context_proof, + r.source_digest,r.source_sha256,r.terminal_digest,r.decoded_bytes,r.compressed_bytes,r.archive_proof, + to_char(r.original_activity_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_activity_at, + to_char(r.original_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_expires_at, + to_char(a.gc_not_before AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS gc_not_before, + a.retention_days,a.safety_ms,octet_length(r.ciphertext) AS ciphertext_bytes, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8')),'hex') AS ciphertext_sha256, + CASE WHEN octet_length(r.ciphertext)<=33000 THEN r.ciphertext END AS ciphertext, + t.plan_id,t.target_server_ids + FROM public.server_poll_archives r JOIN public.server_poll_storage_admissions a USING(receipt_id) + JOIN public.mcp_connections c ON c.id=a.connection_id AND c.owner_id=a.owner_id + JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=a.execution_id AND t.connection_id=a.connection_id + WHERE r.execution_id=$1 AND a.execution_id=$1 AND a.connection_id=$2 AND a.owner_id=$3 + AND a.identity_kind='mcp' AND a.storage_state='compacted' AND r.version=2 + AND r.connection_id=a.connection_id AND r.owner_id=a.owner_id AND r.binding_proof=a.binding_proof AND r.context_proof=a.context_proof + AND r.ciphertext_bytes=a.archive_bytes AND r.ciphertext_bytes=octet_length(r.ciphertext) + AND r.original_activity_at=a.original_activity_at AND r.original_expires_at=a.authority_expires_at + AND t.action_id='servers.poll' AND t.target_global IS TRUE AND cardinality(t.target_server_ids) BETWEEN 1 AND 1000 + AND NOT EXISTS(SELECT 1 FROM public.mcp_operations o WHERE o.id=t.operation_id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_plans p WHERE p.id=t.plan_id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_operation_steps s WHERE s.operation_id=t.operation_id) + LIMIT 2` + +interface RetiredDependencies { + query: Query; now(): number; open: typeof openServerPollRetiredArchive +} +export interface RetiredSourceIdentity { operationId: string; connectionId: string; ownerId: string } + +async function readRetired(identity: RetiredSourceIdentity, currentAuthorize: () => Promise, + rawWindow: ServerPollHistoryReadWindow, deps: RetiredDependencies): Promise<{ affectedServerIds: number[] } | undefined> { + let authFailure: { error: unknown } | undefined + try { + const { operationId, connectionId, ownerId } = identity + if (!uuid.safeParse(operationId).success || !uuid.safeParse(connectionId).success || !ownerId || ownerId.length > 200) fail() + const options = Object.freeze({ ...rawWindow, deadlineAt: Math.min(rawWindow.deadlineAt, deps.now() + 180000) }) + const live = () => { if (!Number.isSafeInteger(options.deadlineAt) || deps.now() >= options.deadlineAt || options.signal?.aborted) fail() } + const authorize = async () => { + live() + try { await currentAuthorize() } catch (error) { authFailure = { error }; throw error } + live() + } + const values = [operationId, connectionId, ownerId] + await authorize() + const rows = await deps.query(RETIRED_QUERY, values, options) + await authorize() + if (rows.length !== 1) return undefined + const row = structuredClone(rows[0]) + const { metadata, history } = await deps.open(row) + await authorize() + if (metadata.execution_id !== operationId || metadata.connection_id !== connectionId || metadata.owner_id !== ownerId + || history.planId !== row.plan_id || history.gcNotBefore !== canonicalPollArchiveTimestamp(row.gc_not_before)) fail() + const scope = z.array(z.union([sid, z.string().regex(/^[1-9]\d{0,15}$/).transform(Number).pipe(sid)])).min(1).max(1000).parse(row.target_server_ids) + if (scope.length !== history.affectedServerIds.length || new Set(scope).size !== scope.length + || scope.some(id => !history.affectedServerIds.includes(id))) fail() + const fresh = await deps.query(RETIRED_QUERY, values, options) + await authorize() + if (fresh.length !== 1 || fingerprint(fresh[0]) !== fingerprint(row)) fail() + return { affectedServerIds: [...history.affectedServerIds] } + } catch { + if (authFailure) throw authFailure.error + return undefined + } +} + +/** The caller owns current authority and independently checks returned scope. */ +export const readRetiredServerPollHistory = (identity: RetiredSourceIdentity, authorize: () => Promise, options: ServerPollHistoryReadWindow) => + readRetired(identity, authorize, options, { query: production.query, now: Date.now, open: openServerPollRetiredArchive }) +export const readRetiredServerPollHistoryForTests = readRetired diff --git a/backend/src/services/server-poll-history-verification.test.ts b/backend/src/services/server-poll-history-verification.test.ts new file mode 100644 index 00000000..a3b04469 --- /dev/null +++ b/backend/src/services/server-poll-history-verification.test.ts @@ -0,0 +1,156 @@ +/** Component cryptographic/history verification; these bundles model locked + * SQL row shapes, not execution authority or actual PostgreSQL acceptance. */ +import assert from "node:assert/strict" +import { createHmac } from "node:crypto" +import { test } from "node:test" +import { verifyServerPollHistoryForTests, ServerPollHistoryInvalid, type ServerPollHistoryRows } from "./server-poll-history-verification.js" +import { encodeServerPollJournalAnchor } from "./server-poll-journal-anchor.js" +import { validateServerPollJournalReceipt } from "./server-poll-journal-validation.js" +import type { ServerPollJournalContext } from "./server-poll-journal.js" +import type { ServerPollReceipt } from "./server-poll-execution.js" + +const canonical = (value: unknown): string => value && typeof value === "object" ? Array.isArray(value) ? "[" + value.map(canonical).join(",") + "]" + : "{" + Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, item]) => JSON.stringify(key) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value) +const mac = (domain: string, value: unknown) => createHmac("sha256", "component-secret:" + domain).update(canonical(value)).digest("hex") +const executionProof = async (value: unknown) => mac("execution", value) +const crypto = { decrypt: async (text: string) => Buffer.from(text, "base64").toString(), proof: async (domain: string, value: unknown) => mac("journal:" + domain, value) } +const encrypt = (value: unknown) => Buffer.from(JSON.stringify(value)).toString("base64") +const id = "11111111-1111-4111-8111-111111111111", operation = "22222222-2222-4222-8222-222222222222" +const hash = "a".repeat(64) + +async function fixture(kind: "mcp" | "rest" = "rest", version: 1 | 2 | 3 = 1) { + const context: ServerPollJournalContext = { identity: kind === "rest" + ? { kind, ownerId: "owned-history", executionId: operation, issuedAt: 0, expiresAt: 6000, requestId: "accepted-request", authentication: "jwt" } + : { kind, ownerId: "owned-history", executionId: operation, issuedAt: 0, expiresAt: 6000, connectionId: "33333333-3333-4333-8333-333333333333", credentialId: "44444444-4444-4444-8444-444444444444", operationName: "servers.poll" }, + plan: { version, ...(version === 3 ? { approvalProof: "b".repeat(64) } : {}), requiresHuman: true, scope: "global", request: { serverId: 1, receiptId: id }, around: new Date(1000).toISOString(), beforeProof: hash, authorityProof: hash, bindingProof: hash, + maintenance: [{ kind: "partition.drop", parent: "flow_buckets", name: "flow_buckets_d_20000101" }], affectedServerIds: [1, 2], mutationCost: 9 } } + const { bindingProof: _binding, ...core } = context.plan; context.plan.bindingProof = await executionProof(core) + const start = version === 3 ? "c".repeat(64) : hash + const anchorText = () => encodeServerPollJournalAnchor(context, version === 3 ? { fullProof: start, approvalProof: context.plan.approvalProof! } : undefined, crypto) + const rows: ServerPollHistoryRows = { execution: { receipt_id: id, identity_kind: kind, execution_id: operation, binding_proof: context.plan.bindingProof, context_ciphertext: Buffer.from(await anchorText()).toString("base64"), context_proof: await crypto.proof("context", context), created_at: new Date(1000) }, + intents: [], acks: [], receipt: {}, quarantines: [] } + let chain = start + for (const stage of ["native.identity", "native.resource", "native.interfaces", "native.addresses", "publish.0", "publish.1", "publish.2"]) { + const before = stage.startsWith("native.") ? start : chain + const effect = stage === "publish.2" ? await executionProof(context.plan.maintenance[0]) : await executionProof({ stage, source: "private-native-payload-not-persisted" }) + const result = await executionProof({ stage, complete: true }) + const startAt = stage.startsWith("native.") ? 1100 : 1300 + Number(stage.slice(8)) * 200 + rows.intents.push({ receipt_id: id, stage, before_proof: before, effect_proof: effect, intent_proof: await crypto.proof("intent", { receiptId: id, stage, beforeProof: before, effectProof: effect }), created_at: new Date(startAt) }) + rows.acks.push({ receipt_id: id, stage, result_proof: result, outcome: "confirmed", ack_proof: await crypto.proof("ack", { receiptId: id, stage, resultProof: result, outcome: "confirmed" }), created_at: new Date(startAt + 100) }) + if (stage.startsWith("publish.")) chain = result + } + const receipt: ServerPollReceipt = { receiptId: id, configurationVerified: true, complete: true, maintenanceComplete: true, nativeReads: 4, confirmedPublications: 3, completedPublications: 3, afterProof: chain, + observation: "online", snapshot: { id: 91, serverId: 1, status: "online", polledAt: new Date(1100).toISOString() } } + rows.receipt = { receipt_id: id, receipt_ciphertext: encrypt(receipt), receipt_proof: await crypto.proof("receipt", JSON.stringify(receipt)), created_at: new Date(1900) } + const resealContext = async () => { rows.execution.context_ciphertext = Buffer.from(await anchorText()).toString("base64"); rows.execution.context_proof = await crypto.proof("context", context) } + const resealReceipt = async () => { rows.receipt.receipt_ciphertext = encrypt(receipt); rows.receipt.receipt_proof = await crypto.proof("receipt", JSON.stringify(receipt)) } + const reack = async (index: number) => { const ack = rows.acks[index]; ack.ack_proof = await crypto.proof("ack", { receiptId: ack.receipt_id, stage: ack.stage, resultProof: ack.result_proof, outcome: ack.outcome }) } + const reintent = async (index: number) => { const intent = rows.intents[index]; intent.intent_proof = await crypto.proof("intent", { receiptId: intent.receipt_id, stage: intent.stage, beforeProof: intent.before_proof, effectProof: intent.effect_proof }) } + return { rows, context, receipt, start, resealContext, resealReceipt, reack, reintent, verify: () => verifyServerPollHistoryForTests(rows, { crypto, executionProof }) } +} + +for (const kind of ["rest", "mcp"] as const) test(`${kind}: v3 expired history authenticates a distinct fresh start without changing the original context proof`, async () => { + const f = await fixture(kind, 3), result = await f.verify() + assert.equal(result.context.plan.version, 3) + assert.notEqual(f.start, f.context.plan.beforeProof) + assert.equal(f.rows.execution.context_proof, await crypto.proof("context", f.context)) +}) +test("v2 historical payload remains exactly the original context", async () => { + const f = await fixture("rest", 2); await f.verify() + assert.deepEqual(JSON.parse(await crypto.decrypt(String(f.rows.execution.context_ciphertext))), f.context) +}) +test("v3 missing, replaced or cross-context start seal refuses even with otherwise authentic stages", async () => { + for (const change of ["missing", "proof", "full", "approval", "identity", "extra"]) { + const f = await fixture("rest", 3) + const payload = JSON.parse(await crypto.decrypt(String(f.rows.execution.context_ciphertext))) + if (change === "missing") delete payload.executionStart + if (change === "proof") payload.executionStart.proof = "d".repeat(64) + if (change === "full") payload.executionStart.fullProof = f.context.plan.beforeProof + if (change === "approval") payload.executionStart.approvalProof = "d".repeat(64) + if (change === "identity") payload.identity.ownerId = "another-owner" + if (change === "extra") payload.executionStart.unknown = true + f.rows.execution.context_ciphertext = encrypt(payload) + await assert.rejects(f.verify(), ServerPollHistoryInvalid) + } +}) +test("v3 native chain cannot fall back to prepared proof even with valid stage MACs", async () => { + const f = await fixture("rest", 3) + for (let index = 0; index < 4; index++) { f.rows.intents[index].before_proof = f.context.plan.beforeProof; await f.reintent(index) } + await assert.rejects(f.verify(), ServerPollHistoryInvalid) +}) + +for (const kind of ["rest", "mcp"] as const) test(`${kind}: authentic expired history verifies without authorizing an old grant or creating capabilities`, async () => { + const f = await fixture(kind), result = await f.verify() + assert(f.context.identity.expiresAt < Date.now()) + assert.deepEqual(result.context, f.context); assert.deepEqual(result.receipt, f.receipt) + assert.deepEqual(result.source, { createdAt: new Date(1000).toISOString(), completedAt: new Date(1900).toISOString(), stageCount: 7, publications: 3, + nativeOutcomes: f.rows.acks.slice(0, 4).map(row => ({ stage: row.stage, outcome: row.outcome, resultProof: row.result_proof })) }) + assert.deepEqual(Object.keys(result).sort(), ["context", "receipt", "source"]) +}) + +test("each stored context, intent, ACK and receipt authenticator is independently required", async () => { + for (const mutate of [ + (f: Awaited>) => { f.rows.execution.context_proof = hash }, + (f: Awaited>) => { f.rows.intents[0].intent_proof = hash }, + (f: Awaited>) => { f.rows.acks[0].ack_proof = hash }, + (f: Awaited>) => { f.rows.receipt.receipt_proof = hash }, + ]) { const f = await fixture(); mutate(f); await assert.rejects(f.verify(), ServerPollHistoryInvalid) } +}) + +test("authenticated context does not replace original plan core binding", async () => { + const f = await fixture(); f.context.plan.maintenance[0] = { kind: "partition.drop", parent: "flow_buckets", name: "flow_buckets_d_20990101" } + await f.resealContext(); await assert.rejects(f.verify(), ServerPollHistoryInvalid) +}) + +test("cross-receipt/execution rows, duplicates, orphan ACK and strange stages cannot hide in a join", async () => { + for (const mutate of [ + (f: Awaited>) => { f.rows.execution.execution_id = id }, + (f: Awaited>) => { f.rows.intents[0].receipt_id = operation }, + (f: Awaited>) => { f.rows.acks[0].receipt_id = operation }, + (f: Awaited>) => { f.rows.receipt.receipt_id = operation }, + (f: Awaited>) => { f.rows.intents[1] = structuredClone(f.rows.intents[0]) }, + (f: Awaited>) => { f.rows.acks[1] = structuredClone(f.rows.acks[0]) }, + (f: Awaited>) => { f.rows.acks[0].stage = "native.orphan" }, + (f: Awaited>) => { f.rows.intents[0].stage = "publish.00" }, + ]) { const f = await fixture(); mutate(f); await assert.rejects(f.verify(), ServerPollHistoryInvalid) } +}) + +test("missing native ACK or quarantine refuses complete history", async () => { + const missing = await fixture(); missing.rows.acks.shift(); await assert.rejects(missing.verify(), ServerPollHistoryInvalid) + const quarantined = await fixture(); quarantined.rows.quarantines.push({ receipt_id: id }); await assert.rejects(quarantined.verify(), ServerPollHistoryInvalid) +}) + +test("valid stage MACs cannot replace publication chaining or the exact maintenance effect", async () => { + const chain = await fixture(); chain.rows.intents[5].before_proof = hash; await chain.reintent(5) + await assert.rejects(chain.verify(), ServerPollHistoryInvalid) + const maintenance = await fixture(); maintenance.rows.intents[6].effect_proof = hash; await maintenance.reintent(6) + await assert.rejects(maintenance.verify(), ServerPollHistoryInvalid) +}) + +test("online requires all confirmed native outcomes; offline read-failed requires the genuine unavailable proof", async () => { + const f = await fixture(); f.rows.acks[0].outcome = "read-failed"; f.rows.acks[0].result_proof = await executionProof({ unavailable: true }); await f.reack(0) + await assert.rejects(f.verify(), ServerPollHistoryInvalid) + f.receipt.observation = "offline"; f.receipt.snapshot.status = "offline"; await f.resealReceipt() + assert.equal((await f.verify()).receipt.observation, "offline", "Historical authentication is distinct from online-only compaction eligibility") + f.rows.acks[0].result_proof = hash; await f.reack(0); await assert.rejects(f.verify(), ServerPollHistoryInvalid) +}) + +test("source timestamps, row/ciphertext bounds, exact context and receipt shapes refuse", async () => { + for (const mutate of [ + async (f: Awaited>) => { f.rows.acks[0].created_at = new Date(2000) }, + async (f: Awaited>) => { f.rows.intents[4].created_at = new Date(1150) }, + async (f: Awaited>) => { f.rows.intents[5].created_at = new Date(1350) }, + async (f: Awaited>) => { f.rows.execution.context_ciphertext = "x".repeat(2097153) }, + async (f: Awaited>) => { f.rows.intents = Array(2011).fill(f.rows.intents[0]) }, + async (f: Awaited>) => { f.receipt.snapshot.rawInterfaces = []; await f.resealReceipt() }, + async (f: Awaited>) => { (f.context as unknown as Record).authority = true; await f.resealContext() }, + ]) { const f = await fixture(); await mutate(f); await assert.rejects(f.verify(), ServerPollHistoryInvalid) } +}) + +test("common receipt validator retains original completion flags and maintenance counts", async () => { + const f = await fixture() + const stages = f.rows.intents.map(intent => ({ ...intent, ...f.rows.acks.find(ack => ack.stage === intent.stage) })) as Parameters[2] + validateServerPollJournalReceipt(f.context.plan, f.receipt, stages) + for (const patch of [{ complete: false }, { nativeReads: 3 }, { completedPublications: 2 }, { afterProof: hash }]) + assert.throws(() => validateServerPollJournalReceipt(f.context.plan, { ...f.receipt, ...patch }, stages)) +}) diff --git a/backend/src/services/server-poll-history-verification.ts b/backend/src/services/server-poll-history-verification.ts new file mode 100644 index 00000000..48e415fb --- /dev/null +++ b/backend/src/services/server-poll-history-verification.ts @@ -0,0 +1,122 @@ +import { publicationLayout, serverPollMaintenancePublications } from "./server-poll-maintenance-groups.js" +import { z } from "zod" +import { openServerPollJournalAnchor } from "./server-poll-journal-anchor.js" +import { parseServerPollJournalIdentity, type ServerPollJournalContext } from "./server-poll-journal.js" +import { SERVER_POLL_NATIVE_READS } from "./server-poll-execution-model.js" +import { serverPollPublications, validateServerPollJournalReceipt, type ServerPollJournalStage } from "./server-poll-journal-validation.js" +import type { ServerPollDependencies, ServerPollReceipt } from "./server-poll-execution.js" +import type { ProbeJournalCrypto } from "./server-probe-journal.js" + +/** Internal raw rows read under the storage owner's SQL locks. Not a public + * JSON request or a capability. Current read/maintenance access and transaction + * ownership must be established by that trusted caller, outside this verifier. */ +export interface ServerPollHistoryRows { + execution: Record; intents: Record[]; acks: Record[] + receipt: Record; quarantines: Record[] +} +export interface VerifiedServerPollHistory { + context: ServerPollJournalContext; receipt: ServerPollReceipt + source: { createdAt: string; completedAt: string; stageCount: number; publications: number + nativeOutcomes: { stage: string; outcome: "confirmed" | "read-failed"; resultProof: string }[] } +} +export class ServerPollHistoryInvalid extends Error { + readonly code = "SERVER_POLL_HISTORY_INVALID" + constructor() { super("Complete authenticated server poll history unavailable") } +} +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid(), sid = z.number().int().positive().safe() +const timestamp = z.union([z.date(), z.string().min(1).max(100)]).refine(value => Number.isFinite(new Date(value).getTime())) +const executionSchema = z.object({ receipt_id: uuid, identity_kind: z.enum(["mcp", "rest"]), execution_id: uuid, binding_proof: hash, + context_ciphertext: z.string().min(1).max(2097152), context_proof: hash, created_at: timestamp }).strict() +const intentSchema = z.object({ receipt_id: uuid, stage: z.string().min(1).max(64), before_proof: hash, effect_proof: hash, intent_proof: hash, created_at: timestamp }).strict() +const ackSchema = z.object({ receipt_id: uuid, stage: z.string().min(1).max(64), result_proof: hash, outcome: z.enum(["confirmed", "read-failed"]), ack_proof: hash, created_at: timestamp }).strict() +const receiptSchema = z.object({ receipt_id: uuid, receipt_ciphertext: z.string().min(1).max(262144), receipt_proof: hash, created_at: timestamp }).strict() +const partition = { parent: z.string().min(1).max(100), name: z.string().min(1).max(100) } +const maintenanceSchema = z.discriminatedUnion("kind", [ + z.object({ kind: z.literal("partition.drop"), ...partition }).strict(), + z.object({ kind: z.literal("partition.ensure"), ...partition, from: z.string().datetime(), to: z.string().datetime() }).strict(), + z.object({ kind: z.literal("partition.storage"), ...partition, settings: z.record(z.string(), z.string()) }).strict(), +]) +const planSchema = z.object({ version: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]), approvalProof: hash.optional(), maintenanceGroupSize: z.literal(8).optional(), requiresHuman: z.literal(true), scope: z.literal("global"), + request: z.object({ serverId: sid, receiptId: uuid }).strict(), around: z.string().datetime(), beforeProof: hash, authorityProof: hash, bindingProof: hash, + maintenance: z.array(maintenanceSchema).max(2000), affectedServerIds: z.array(sid).min(1).max(1000), mutationCost: z.number().int().min(8).max(2008) }).strict().refine(plan => ((plan.version === 3 || plan.version === 4) ? plan.approvalProof !== undefined : !Object.hasOwn(plan, "approvalProof")) && (plan.version === 4 ? plan.maintenanceGroupSize === 8 : !Object.hasOwn(plan, "maintenanceGroupSize"))) +interface HistoryDependencies { crypto: Pick; executionProof: ServerPollDependencies["proof"] } +const must = (value: unknown): void => { if (!value) throw new ServerPollHistoryInvalid() } +const at = (value: Date | string) => new Date(value).getTime() + +async function verify(raw: ServerPollHistoryRows, deps: HistoryDependencies): Promise { + try { + must(raw && Object.keys(raw).sort().join(",") === "acks,execution,intents,quarantines,receipt") + must(Array.isArray(raw.intents) && raw.intents.length <= 2010 && Array.isArray(raw.acks) && raw.acks.length <= 2010 && Array.isArray(raw.quarantines) && raw.quarantines.length === 0) + must(Buffer.byteLength(JSON.stringify(raw)) <= 8 * 1048576) + const anchor = executionSchema.parse(raw.execution), terminal = receiptSchema.parse(raw.receipt) + must(Buffer.byteLength(anchor.context_ciphertext) <= 2097152 && Buffer.byteLength(terminal.receipt_ciphertext) <= 262144) + const contextText = await deps.crypto.decrypt(anchor.context_ciphertext) + must(Buffer.byteLength(contextText) <= 1048576) + const parsed: unknown = JSON.parse(contextText) + must(parsed && typeof parsed === "object" && !Array.isArray(parsed) && ["identity,plan", "executionStart,identity,plan"].includes(Object.keys(parsed).sort().join(","))) + const body = parsed as Record + const context: ServerPollJournalContext = { identity: parseServerPollJournalIdentity(body.identity), plan: planSchema.parse(body.plan) } + const executionStartProof = await openServerPollJournalAnchor(parsed, context, deps.crypto) + const { identity, plan } = context, { bindingProof, ...core } = plan + const layout = publicationLayout(plan), maintenance = serverPollMaintenancePublications(plan) + must(identity.expiresAt > identity.issuedAt && new Set(plan.affectedServerIds).size === plan.affectedServerIds.length && plan.affectedServerIds.includes(plan.request.serverId) && plan.mutationCost === plan.maintenance.length + 8) + must(anchor.receipt_id === plan.request.receiptId && anchor.identity_kind === identity.kind && anchor.execution_id === identity.executionId && anchor.binding_proof === bindingProof && terminal.receipt_id === anchor.receipt_id) + must(anchor.context_proof === hash.parse(await deps.crypto.proof("context", context)) && bindingProof === hash.parse(await deps.executionProof(core))) + const receiptText = await deps.crypto.decrypt(terminal.receipt_ciphertext) + must(Buffer.byteLength(receiptText) <= 131072 && terminal.receipt_proof === hash.parse(await deps.crypto.proof("receipt", receiptText))) + const receipt = JSON.parse(receiptText) as ServerPollReceipt + const intents = raw.intents.map(row => intentSchema.parse(row)), acks = raw.acks.map(row => ackSchema.parse(row)) + must(intents.length === acks.length && intents.length <= layout.journalMaxStages) + must(new Set(intents.map(row => row.stage)).size === intents.length && new Set(acks.map(row => row.stage)).size === acks.length) + const ackByStage = new Map(acks.map(row => [row.stage, row])), stages: ServerPollJournalStage[] = [] + const native = new Set(SERVER_POLL_NATIVE_READS.map(value => "native." + value)) + for (const intent of intents) { + const ack = ackByStage.get(intent.stage) + must(ack && intent.receipt_id === anchor.receipt_id && ack.receipt_id === anchor.receipt_id) + if (!ack) throw new ServerPollHistoryInvalid() + must(native.has(intent.stage) || /^publish\.(0|[1-9]\d{0,3})$/.test(intent.stage)) + must(at(anchor.created_at) <= at(intent.created_at) && at(intent.created_at) <= at(ack.created_at) && at(ack.created_at) <= at(terminal.created_at)) + must(intent.intent_proof === hash.parse(await deps.crypto.proof("intent", { receiptId: anchor.receipt_id, stage: intent.stage, beforeProof: intent.before_proof, effectProof: intent.effect_proof }))) + must(ack.ack_proof === hash.parse(await deps.crypto.proof("ack", { receiptId: anchor.receipt_id, stage: ack.stage, resultProof: ack.result_proof, outcome: ack.outcome }))) + if (native.has(intent.stage)) { + must(intent.before_proof === executionStartProof) + if (ack.outcome === "read-failed") must(ack.result_proof === hash.parse(await deps.executionProof({ unavailable: true }))) + } else must(ack.outcome === "confirmed") + stages.push({ ...intent, result_proof: ack.result_proof, outcome: ack.outcome, ack_proof: ack.ack_proof }) + } + // Duplicate/orphan ACKs cannot be hidden by a join or a count-only check. + must(acks.every(ack => intents.some(intent => intent.stage === ack.stage))) + validateServerPollJournalReceipt(plan, receipt, stages, executionStartProof) + const pubs = serverPollPublications(stages) + const nativeAckAt = Math.max(...acks.filter(row => native.has(row.stage)).map(row => at(row.created_at))) + must(at(pubs[0].created_at as Date | string) >= nativeAckAt) + for (let index = 0; index < pubs.length; index++) must(pubs[index].before_proof === (index === 0 ? executionStartProof : pubs[index - 1].result_proof)) + for (let index = 1; index < pubs.length; index++) must(at(pubs[index].created_at as Date | string) >= at(ackByStage.get(pubs[index - 1].stage)!.created_at)) + const offset = pubs.length - maintenance.length + for (let index = 0; index < maintenance.length; index++) must(pubs[offset + index].effect_proof === hash.parse(await deps.executionProof(maintenance[index]))) + const nativeOutcomes = SERVER_POLL_NATIVE_READS.map(value => { + const row = stages.find(row => row.stage === "native." + value) + must(row && (row.outcome === "confirmed" || row.outcome === "read-failed")) + if (!row) throw new ServerPollHistoryInvalid() + return { stage: row.stage, outcome: row.outcome as "confirmed" | "read-failed", resultProof: String(row.result_proof) } + }) + return { context, receipt, source: { createdAt: new Date(anchor.created_at).toISOString(), completedAt: new Date(terminal.created_at).toISOString(), + stageCount: stages.length, publications: pubs.length, nativeOutcomes } } + } catch { throw new ServerPollHistoryInvalid() } +} + +/** Fixed historical cryptographic purposes. Does not consult or renew the old + * grant, execute SQL/native work, or issue any execution/maintenance capability. */ +export async function verifyServerPollHistory(rows: ServerPollHistoryRows): Promise { + const security = await import("../modules/mcp/security.js") + const signers = new Map string>>() + const signer = (domain: string) => { + let value = signers.get(domain) + if (!value) { value = security.privateFingerprinter(domain); signers.set(domain, value) } + return value + } + return verify(rows, { crypto: { decrypt: security.decryptSecret, proof: async (domain, value) => (await signer("server-poll-journal-v1:" + domain))(security.fingerprint(value)) }, + executionProof: async value => (await signer("server-poll-production-v1"))(security.fingerprint(value)) }) +} +/** Isolated test dependencies only; never a caller signer or production input. */ +export const verifyServerPollHistoryForTests = (rows: ServerPollHistoryRows, deps: HistoryDependencies) => verify(rows, deps) diff --git a/backend/src/services/server-poll-http.test.ts b/backend/src/services/server-poll-http.test.ts new file mode 100644 index 00000000..0fb786fd --- /dev/null +++ b/backend/src/services/server-poll-http.test.ts @@ -0,0 +1,96 @@ +/** Genuine Fastify/JWT provenance; only the host execution is a test double. + * Configured pools cannot dispatch anything except the exact revocation read. */ +import assert from "node:assert/strict" +import Fastify, { type FastifyInstance } from "fastify" +import { SignJWT } from "jose" +import { mkdtemp, realpath, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join, dirname, basename } from "node:path" +import { spawn } from "node:child_process" +import { fileURLToPath } from "node:url" +const local = process.env.MM_TEST_POLL_HTTP_LOCAL === "true" +process.env.NODE_ENV = "test"; process.env.AUTH_REQUIRED = String(!local) +process.env.AUTH_JWT_SECRET = "owned-poll-http-acceptance-secret" +process.env.AUTH_ISSUER = "https://owned-poll-http.example.invalid" +const parent = await realpath(tmpdir()), root = await mkdtemp(join(parent,"mm-poll-http-")) +process.env.MCP_STATE_DIR = root +const { default: auth, requireApiAuth } = await import("../plugins/auth.js") +const { createDurableServerPollHttpForTests } = await import("./server-poll-http.js") +const { pool, closePool } = await import("../db/index.js") +const originalQuery = pool.query, cutoff = new Map() +let portalReads = 0, dispatches = 0, afterExecution: (()=>void|Promise) | undefined +const contexts: Parameters[0]>[1][] = [] +pool.query = (async (sql:string,args:unknown[]) => { + assert.equal(sql,"SELECT invalid_before FROM mcp_owner_revocations WHERE owner_id=$1") + portalReads++; const at = cutoff.get(String(args[0])); return {rows:at?[{invalid_before:new Date(at)}]:[]} +}) as typeof pool.query +const apps:FastifyInstance[]=[] +const secret = new TextEncoder().encode(process.env.AUTH_JWT_SECRET) +const mint = (permissions=["mm:servers:write"]) => new SignJWT({sub:"original-owner",apps:["mm"],permissions, + iat:Math.floor(Date.now()/1000)-10,exp:Math.floor(Date.now()/1000)+3600}).setProtectedHeader({alg:"HS256"}).setIssuer(process.env.AUTH_ISSUER!).sign(secret) +let cases=0 +async function check(name:string,work:()=>Promise){await work();cases++;console.log("ok",name)} +async function app(withPlugin=true) { + const instance=Fastify();apps.push(instance) + if(withPlugin){await instance.register(auth);instance.addHook("preHandler",requireApiAuth)} + instance.addHook("preHandler",async request=>{ + // A mutable convenience field cannot replace the signed original claims. + request.authUser={id:"forged-owner",email:"",name:"",apps:["mm"],permissions:["mm:admin"],issuedAt:1,expiresAt:9999999999999} + }) + instance.post("/api/servers/:id/poll",createDurableServerPollHttpForTests(async (id,context)=>{ + dispatches++;contexts.push(context);assert.equal(id,"7");context.assertRequestActive?.();await afterExecution?.() + return {statusCode:200,body:{id:3,serverId:7,status:"online"}} + })) + return instance +} +try { + const server=await app(),token=await mint(),headers={authorization:"Bearer "+token,"x-request-id":"forged-request-id"} + if(!local)await check("401/403 before host, including valid read-only JWT",async()=>{ + const before=dispatches + assert.equal((await server.inject({method:"POST",url:"/api/servers/7/poll"})).statusCode,401) + assert.equal((await server.inject({method:"POST",url:"/api/servers/7/poll",headers:{authorization:"Bearer "+await mint(["mm:servers:read"])}})).statusCode,403) + assert.equal(dispatches,before) + }) + await check("original JWT/local identity; body/headers/authUser cannot assign authority",async()=>{ + const reply=await server.inject({method:"POST",url:"/api/servers/7/poll",headers,payload:{serverId:999,authRequired:false,actor:{id:"injected"},requestId:"injected",deadlineAt:9999999999999}}) + assert.equal(reply.statusCode,200,reply.body);assert.deepEqual(reply.json(),{id:3,serverId:7,status:"online"}) + const context=contexts.at(-1)!;assert.equal(context.authRequired,!local);assert.notEqual(context.requestId,"forged-request-id") + assert.match(context.requestId,/^[a-f0-9-]{36}$/) + assert(context.deadlineAt!<=context.acceptedAt+180000) + if(local)assert.equal(context.actor,undefined) + else{assert.equal(context.actor?.id,"original-owner");assert.deepEqual(context.actor?.permissions,["mm:servers:write"])} + assert(context.signal?.aborted,"The route must retire its own signal after completion") + assert.throws(context.assertRequestActive!) + }) + if(!local)await check("late owner revocation blocks HTTP publication after host completion",async()=>{ + afterExecution=()=>cutoff.set("original-owner",Date.now()) + try{const reply=await server.inject({method:"POST",url:"/api/servers/7/poll",headers});assert.equal(reply.statusCode,401);assert(!reply.body.includes('"serverId"'))} + finally{afterExecution=undefined;cutoff.clear()} + }) + await check("a handler without genuine auth hooks refuses before execution",async()=>{ + const isolated=await app(false),before=dispatches + const reply=await isolated.inject({method:"POST",url:"/api/servers/7/poll",headers,payload:{authRequired:false}}) + assert.equal(reply.statusCode,403);assert.equal(dispatches,before) + }) + await check("actual HTTP disconnect retires the original host context",async()=>{ + let ready!:()=>void + const entered = new Promise(done=>{ready=done}) + afterExecution=async()=>{const signal=contexts.at(-1)!.signal!;ready();if(!signal.aborted)await new Promise(done=>signal.addEventListener("abort",()=>done(),{once:true}))} + try{ + await server.listen({host:"127.0.0.1",port:0}) + const address=server.server.address();assert(address&&typeof address!=="string") + const controller=new AbortController(),response=fetch(`http://127.0.0.1:${address.port}/api/servers/7/poll`,{method:"POST",headers,signal:controller.signal}) + await entered;controller.abort();await assert.rejects(response) + const context=contexts.at(-1)! + for(let index=0;!context.signal?.aborted&&index<100;index++)await new Promise(done=>setTimeout(done,10)) + assert(context.signal?.aborted);assert.throws(context.assertRequestActive!) + }finally{afterExecution=undefined} + }) + console.log("Full poll HTTP provenance:",JSON.stringify({cases,mode:local?"local":"jwt",actualFastify:true,actualSignedJwt:!local,hostExecutionTestDouble:true,portalReads,productionDatabaseTouched:false,nativeRequests:0})) + if(!local){const code=await new Promise((done,reject)=>{ + const child=spawn(process.execPath,["--import","tsx",fileURLToPath(import.meta.url)],{windowsHide:true,env:{...process.env,MM_TEST_POLL_HTTP_LOCAL:"true"},stdio:"inherit"}) + child.once("error",reject);child.once("exit",code=>done(code??1)) + });assert.equal(code,0,"Local auth mode must be verified in an independent process")} +}finally{ + try{for(const instance of apps)await instance.close()}finally{pool.query=originalQuery;await closePool();const actual=await realpath(root);assert.equal(dirname(actual),parent);assert.match(basename(actual),/^mm-poll-http-/);await rm(actual,{recursive:true,force:true})} +} diff --git a/backend/src/services/server-poll-http.ts b/backend/src/services/server-poll-http.ts new file mode 100644 index 00000000..a89c382b --- /dev/null +++ b/backend/src/services/server-poll-http.ts @@ -0,0 +1,58 @@ +import type { FastifyReply, FastifyRequest } from "fastify" +import { performance } from "node:perf_hooks" +import { captureVerifiedRestRequest, inspectVerifiedRestRequest } from "../plugins/auth.js" +import { McpError } from "../modules/mcp/security.js" +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +import { executeDurableServerPollRest } from "./server-poll-rest.js" + +function handler(execute: typeof executeDurableServerPollRest) { + return async (request: FastifyRequest, reply: FastifyReply) => { + const serverId = String((request.params as { id?: unknown })?.id ?? "") + const disconnected = new AbortController() + const aborted = () => disconnected.abort() + const closed = () => { if (!reply.raw.writableEnded) disconnected.abort() } + request.raw.once("aborted", aborted); reply.raw.once("close", closed) + let timer: ReturnType | undefined + if (request.raw.aborted || reply.raw.destroyed) disconnected.abort() + try { + const scope = Object.freeze({ deadlineAt: Date.now() + SERVER_POLL_WALL_BUDGET_MS, signal: disconnected.signal }) + const token = await captureVerifiedRestRequest(request, scope) + const authority = await inspectVerifiedRestRequest(token, scope, "mm:servers:write") + if (authority.method !== "POST" || !/^\/api\/servers\/(?::id|[1-9]\d*)\/poll$/.test(authority.path)) throw new McpError("PERMISSION_DENIED", "Недопустимый источник полного опроса", 403) + const deadlineAt = Math.min(authority.expiresAt, authority.acceptedAt + SERVER_POLL_WALL_BUDGET_MS) + const monotonicDeadline = authority.monotonicDeadline - (authority.expiresAt - deadlineAt) + const signal = AbortSignal.any([authority.signal, disconnected.signal]) + const assertRequestActive = () => { + if (signal.aborted || Date.now() < authority.acceptedAt || Date.now() >= deadlineAt || performance.now() >= monotonicDeadline) { + throw new McpError("READ_CANCELLED", "Исходный запрос полного опроса завершён", 409) + } + } + assertRequestActive() + timer = setTimeout(aborted, Math.max(1, Math.min(deadlineAt - Date.now(), monotonicDeadline - performance.now()))) + // Identity/configuration come from the genuine authentication hooks, never + // request JSON, headers, a mutable authUser or a caller's request ID. + const actor = authority.authentication === "jwt" ? { id: authority.ownerId, email: "", name: "", apps: [...authority.apps], + permissions: [...authority.permissions], issuedAt: authority.issuedAt, expiresAt: deadlineAt } : undefined + const response = await execute(serverId, { actor, authRequired: authority.authentication === "jwt", acceptedAt: authority.acceptedAt, + requestId: authority.requestId, deadlineAt, signal, assertRequestActive }) + assertRequestActive() + const current = await inspectVerifiedRestRequest(token, scope, "mm:servers:write") + if (current !== authority) throw new McpError("STATE_CONFLICT", "Изменился исходный запрос полного опроса", 409) + assertRequestActive() + return reply.status(response.statusCode).send(response.body) + } catch (error) { + if (request.raw.aborted || reply.raw.destroyed) return + const code = error && typeof error === "object" && "code" in error ? error.code : undefined + const status = code === "READ_CANCELLED" ? 409 : code === "OWNER_CLAIMS_REVOKED" ? 401 + : code === "PERMISSION_DENIED" || code === "REST_REQUEST_PROVENANCE_REFUSED" ? 403 : 503 + return reply.status(status).send({ error: status === 401 ? "Права изменены; войдите заново" : status === 403 ? "Нет подтверждённого права полного опроса" + : status === 409 ? "Полный опрос отменён или истёк; обновите данные перед повтором" : "Полный опрос недоступен; автоматический повтор запрещён" }) + } finally { + clearTimeout(timer); request.raw.off("aborted", aborted); reply.raw.off("close", closed); disconnected.abort() + } + } +} + +export const handleDurableServerPollHttp = handler(executeDurableServerPollRest) +/** Trusted composition for HTTP/auth tests; never derived from a request. */ +export const createDurableServerPollHttpForTests = (execute: typeof executeDurableServerPollRest) => handler(execute) diff --git a/backend/src/services/server-poll-journal-anchor.ts b/backend/src/services/server-poll-journal-anchor.ts new file mode 100644 index 00000000..124f709c --- /dev/null +++ b/backend/src/services/server-poll-journal-anchor.ts @@ -0,0 +1,42 @@ +import { isDeepStrictEqual } from "node:util" +import type { ServerPollExecutionStart } from "./server-poll-execution.js" +import type { ServerPollJournalContext } from "./server-poll-journal.js" +import type { ProbeJournalCrypto } from "./server-probe-journal.js" + +const hash = (value: unknown): value is string => typeof value === "string" && /^[a-f0-9]{64}$/.test(value) +const must = (value: unknown): void => { if (!value) throw Error("Invalid immutable poll execution start") } +const record = (value: unknown): value is Record => !!value && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype +const domain = "execution-start-v1" +type Signer = Pick + +/** Original context/admission proof remains unchanged. Only version3/4 + * encrypted anchor carries an independently purpose-sealed fresh full start. + * No row, quota increase, authority renewal or caller-selected signer. */ +export async function encodeServerPollJournalAnchor(context: ServerPollJournalContext, start: ServerPollExecutionStart | undefined, crypto: Signer): Promise { + if (context.plan.version !== 3 && context.plan.version !== 4) { must(start === undefined); return JSON.stringify(context) } + must(start && Object.keys(start).sort().join(",")==="approvalProof,fullProof" && hash(start.fullProof) && hash(start.approvalProof) && start.approvalProof === context.plan.approvalProof) + if (!start) throw Error("Missing immutable poll execution start") + const value = { context, fullProof: start.fullProof, approvalProof: start.approvalProof } + const proof = await crypto.proof(domain, value); must(hash(proof)) + return JSON.stringify({ ...context, executionStart: { ...start, proof } }) +} + +/** Expected context is independently validated by the fixed journal/history + * reader. This function authenticates the extra start; it issues no authority. */ +export async function openServerPollJournalAnchor(parsed: unknown, context: ServerPollJournalContext, crypto: Signer): Promise { + must(record(parsed)) + if (!record(parsed)) throw Error("Missing poll journal anchor") + if (context.plan.version !== 3 && context.plan.version !== 4) { + must(isDeepStrictEqual(parsed, context)); return context.plan.beforeProof + } + must(Object.keys(parsed).sort().join(",") === "executionStart,identity,plan") + const { executionStart: start, ...original } = parsed + must(isDeepStrictEqual(original, context) && record(start)) + if (!record(start)) throw Error("Missing immutable poll execution start") + must(Object.keys(start).sort().join(",") === "approvalProof,fullProof,proof" + && hash(start.fullProof) && hash(start.approvalProof) && hash(start.proof) + && start.approvalProof === context.plan.approvalProof) + const expected = await crypto.proof(domain, { context, fullProof: start.fullProof, approvalProof: start.approvalProof }) + must(hash(expected) && start.proof === expected) + return start.fullProof as string +} diff --git a/backend/src/services/server-poll-journal-snapshot.owned.test.ts b/backend/src/services/server-poll-journal-snapshot.owned.test.ts new file mode 100644 index 00000000..90d69486 --- /dev/null +++ b/backend/src/services/server-poll-journal-snapshot.owned.test.ts @@ -0,0 +1,193 @@ +/** Owned PostgreSQL lock evidence for the fixed journal snapshot. Synthetic + * REST journal fixtures are admitted through actual storage/crypto factories; + * no native/MCP/terminal success is manufactured or claimed. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import type { ServerPollJournalContext } from "./server-poll-journal.js" + +assert.equal(process.env.MMAPP_OWN_POLL_JOURNAL_SNAPSHOT, "true") +assert.equal(process.env.NODE_ENV, "test") +const endpoint = new URL(process.env.DATABASE_URL ?? "") +assert(["postgres:", "postgresql:"].includes(endpoint.protocol)); assert.equal(endpoint.hostname, "127.0.0.1") +assert.equal(endpoint.username, "mcp_test"); assert(endpoint.password); assert.equal(endpoint.pathname, "/postgres") +assert.equal(endpoint.search, ""); assert.equal(endpoint.hash, ""); assert(Number(endpoint.port) > 0 && Number(endpoint.port) <= 65535) +assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) +assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state"); assert.match(basename(dirname(resolve(process.env.MCP_STATE_DIR))), /^mm-mcp-test-[A-Za-z0-9-]+$/) +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 { fingerprint, privateSecretFingerprint } = await import("../modules/mcp/security.js") +const { createServerPollStorageJournal } = await import("./server-poll-storage-journal.js") +const { createSqlServerPollJournal, ServerPollJournalConflict } = await import("./server-poll-journal.js") +const { queryServerPollMcpMetadata } = await import("../modules/mcp/server-poll-production.js") +const bounded = async (work: Promise, ms: number, message: string): Promise => { + let timer: ReturnType | undefined + try { return await Promise.race([work, new Promise((_resolve, reject) => { timer = setTimeout(() => reject(Error(message)), ms) })]) } + finally { clearTimeout(timer) } +} +const serverIds: number[] = [] +async function fixture(kind: "rest" | "mcp" = "rest") { + const now = Date.now(), id = Number((await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES('owned-journal-snapshot','127.0.0.1',1,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id")).rows[0].id) + serverIds.push(id) + // Each fixture's catalogue is the actual current complete owned catalogue. + const ids = (await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id)) + const context: ServerPollJournalContext = { identity: { kind: "rest", ownerId: "owned-journal-snapshot-" + randomUUID(), executionId: randomUUID(), requestId: randomUUID(), authentication: "local", issuedAt: now - 1000, expiresAt: now + 180000 }, + plan: { version: 1, requiresHuman: true, scope: "global", request: { serverId: id, receiptId: randomUUID() }, around: new Date(now).toISOString(), beforeProof: "a".repeat(64), authorityProof: "b".repeat(64), bindingProof: "c".repeat(64), maintenance: [], affectedServerIds: ids, mutationCost: 8 } } + const connectionId=randomUUID(),credentialId=randomUUID(),planId=randomUUID() + if(kind==="mcp"){ + context.identity={kind:"mcp",ownerId:context.identity.ownerId,executionId:context.identity.executionId,connectionId,credentialId,operationName:"servers.poll",issuedAt:now-1000,expiresAt:now+180000} + // These are explicitly synthetic owner rows, not approved runtime evidence. + // The separate public fixture validates real authorization composition. + const policy={status:"active",mode:"approval",expiresAt:new Date(now+180000).toISOString(),scope:{serverIds:ids,allowGlobal:true,operationIds:["servers.poll"]}} + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until) VALUES($1,'SYNTHETIC',$2,'[]',$3::jsonb,'synthetic',$4)",[connectionId,context.identity.ownerId,JSON.stringify(policy),new Date(now+180000)]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)",[credentialId,connectionId,randomUUID(),new Date(now+180000)]) + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) VALUES($1,$2,'servers.poll',1,1,'synthetic','{}','{}','executing',$3)",[planId,connectionId,new Date(now+180000)]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'running')",[context.identity.executionId,planId,connectionId,randomUUID()]) + } + const { bindingProof: _binding, ...core } = context.plan + const executionProof = (value: unknown) => privateSecretFingerprint(fingerprint(value), "server-poll-production-v1") + context.plan.bindingProof = await executionProof(core) + const authorize = async () => { assert(Date.now() < context.identity.expiresAt); if(kind==="rest")assert.equal((await pool.query("SELECT enabled FROM servers WHERE id=$1", [id])).rows[0].enabled, true) } + const storage = await createServerPollStorageJournal(context, { deadlineAt: context.identity.expiresAt, assertAuthority: authorize }) + const journal = createSqlServerPollJournal(context, { storage, assertAuthority: authorize, executionProof }) + await journal.claim(context.plan.request, context.plan.bindingProof) + const intent = () => journal.intent({ receiptId: context.plan.request.receiptId, stage: "native.identity", beforeProof: context.plan.beforeProof, effectProof: "d".repeat(64) }) + const contextProof = await privateSecretFingerprint(fingerprint(context), "server-poll-journal-v1:context") + return { context, journal, intent, contextProof,connectionId,credentialId,planId } +} +try { + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length) + const metadataSql = "SELECT pg_backend_pid() AS pid,current_setting('statement_timeout') AS timeout,current_setting('TimeZone') AS timezone,current_setting('transaction_read_only') AS readonly,current_setting('transaction_isolation') AS isolation" + const originalIsolation = (await pool.query("SHOW default_transaction_isolation")).rows[0].default_transaction_isolation + await pool.query("SET default_transaction_isolation='repeatable read'") + try { + const beforeMetadata = (await pool.query(metadataSql)).rows[0] + assert.equal(beforeMetadata.isolation, "repeatable read") + const configured = (await queryServerPollMcpMetadata(metadataSql, [], { deadlineAt: Date.now() + 10000 }))[0] + assert.equal(configured.pid, beforeMetadata.pid) + assert.equal(configured.timeout, "10s"); assert.equal(configured.timezone, "UTC"); assert.equal(configured.readonly, "on") + assert.equal(configured.isolation, "read committed") + const afterMetadata = (await pool.query(metadataSql)).rows[0] + assert.deepEqual(afterMetadata, beforeMetadata, "Local settings and read-only transaction must not leak into the pooled backend") + } finally { + await pool.query("SELECT set_config('default_transaction_isolation',$1,false)", [originalIsolation]) + } + // Fixture1 proves the actual fused query can append through actual PostgreSQL + // JSONB, row locking and genuine factory-issued ownership, without native I/O. + const first = await fixture(); await first.intent() + await first.journal.acknowledge({ receiptId: first.context.plan.request.receiptId, stage: "native.identity", resultProof: "e".repeat(64), outcome: "confirmed" }) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_acks WHERE receipt_id=$1", [first.context.plan.request.receiptId])).rows[0].n, 1) + + const ownedMcp=await fixture("mcp");await ownedMcp.intent() + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1",[ownedMcp.context.plan.request.receiptId])).rows[0].n,1) + const refusals=[ + "UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", + "UPDATE mcp_credentials SET expires_at=clock_timestamp()-interval '1 second' WHERE id=$1", + "UPDATE mcp_connections SET owner_id='foreign' WHERE id=$2", + "UPDATE mcp_connections SET owner_valid_until=clock_timestamp()-interval '1 second' WHERE id=$2", + "UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"') WHERE id=$2", + "UPDATE mcp_connections SET policy=jsonb_set(policy,'{mode}','\"observer\"') WHERE id=$2", + "UPDATE mcp_connections SET policy=jsonb_set(policy,'{expiresAt}',to_jsonb((clock_timestamp()-interval '1 second')::text)) WHERE id=$2", + "UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$2", + "UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,allowGlobal}','false') WHERE id=$2", + "UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,operationIds}','[]') WHERE id=$2", + "UPDATE mcp_plans SET operation_id='foreign' WHERE id=$3", + "UPDATE servers SET enabled=false WHERE id=$4", + ] + for(const change of refusals){ + const f=await fixture("mcp") + // The fixed statement uses only its indicated parameter; PostgreSQL still + // needs types for the unused slots in the shared test parameter vector. + const values=[f.credentialId,f.connectionId,f.planId,f.context.plan.request.serverId] + const slot=Number(change.match(/\$(\d)/)![1])-1 + await pool.query(change.replaceAll("$"+(slot+1),"$1"),[values[slot]]) + await assert.rejects(f.intent(),ServerPollJournalConflict) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,0) + } + + const waiting=await fixture("mcp"),executionBlocker=await pool.connect(),receiptBlocker=await pool.connect(),lockObserver=await pool.connect() + let waitingIntent:Promise|undefined + try{ + await executionBlocker.query("BEGIN");await receiptBlocker.query("BEGIN") + await executionBlocker.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))",["server-poll-execution-v1:mcp:"+waiting.context.identity.executionId]) + await receiptBlocker.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))",["server-poll-receipt-v1:"+waiting.context.plan.request.receiptId]) + const executionPid=Number((await executionBlocker.query("SELECT pg_backend_pid() AS pid")).rows[0].pid),receiptPid=Number((await receiptBlocker.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + const blockedBy=async(pid:number)=>{ + for(const until=Date.now()+3000;Date.now()(value.blockers as number[]).includes(pid));if(row)return Number(row.pid) + await delay(10) + } + assert.fail("Original journal lock was not observed") + } + waitingIntent=waiting.intent();void waitingIntent.catch(()=>{}) + const executionOwner=await blockedBy(executionPid) + await executionBlocker.query("ROLLBACK") + assert.equal(await blockedBy(receiptPid),executionOwner,"Both ordered journal locks belong to the same original SQL backend") + await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1",[waiting.credentialId]) + await receiptBlocker.query("ROLLBACK") + await bounded(assert.rejects(waitingIntent,ServerPollJournalConflict),5000,"Current owner validation did not settle after lock release") + waitingIntent=undefined + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1",[waiting.context.plan.request.receiptId])).rows[0].n,0) + }finally{ + await executionBlocker.query("ROLLBACK").catch(()=>{});await receiptBlocker.query("ROLLBACK").catch(()=>{}) + if(waitingIntent)await waitingIntent.catch(()=>{}) + executionBlocker.release(true);receiptBlocker.release(true);lockObserver.release(true) + } + + const target = await fixture(), receipt = target.context.plan.request.receiptId + const blocker = await pool.connect(), writer = await pool.connect(), observer = await pool.connect() + let pending: Promise | undefined, released = false + try { + await blocker.query("BEGIN"); await blocker.query("SELECT pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0))") + await blocker.query("SELECT receipt_id FROM server_poll_storage_admissions WHERE receipt_id=$1 FOR UPDATE", [receipt]) + const blockerPid = Number((await blocker.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + const writerPid = Number((await writer.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + await writer.query("BEGIN"); await writer.query("SET LOCAL statement_timeout='10s'") + // No manually acquired lock in the writer: its actual enrolled INSERT guard + // must obtain the quota lock before it can publish a quarantine marker. + pending = writer.query("INSERT INTO server_poll_quarantines(receipt_id,identity_kind,execution_id,context_proof) VALUES($1,'rest',$2,$3)", [receipt, target.context.identity.executionId, target.contextProof]) + void pending.catch(() => {}) + let blocked = false + for (const until = Date.now() + 5000; Date.now() < until;) { + const rows = (await observer.query("SELECT pg_blocking_pids(pid) AS blockers,wait_event_type,query FROM pg_stat_activity WHERE pid=$1 AND datname=current_database()", [writerPid])).rows + if (rows.length === 1 && rows[0].wait_event_type === "Lock" && rows[0].blockers.includes(blockerPid) && rows[0].query.startsWith("INSERT INTO server_poll_quarantines")) { blocked = true; break } + await delay(20) + } + assert(blocked, "Actual quarantine INSERT must be blocked by the exact owned quota/admission transaction") + assert.equal((await observer.query("SELECT count(*)::int AS n FROM server_poll_quarantines WHERE receipt_id=$1", [receipt])).rows[0].n, 0) + await blocker.query("ROLLBACK"); released = true + await bounded(pending, 5000, "Quarantine writer failed to settle after releasing the owned blocker"); await writer.query("COMMIT"); pending = undefined + await assert.rejects(target.intent(), ServerPollJournalConflict) + assert.equal((await observer.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1", [receipt])).rows[0].n, 0) + } finally { + if (!released) await blocker.query("ROLLBACK") + // Release the blocker BEFORE joining a waiting writer on every failure. + if (pending) await pending.catch(() => {}) + await writer.query("ROLLBACK").catch(() => {}) + blocker.release(true); writer.release(true); observer.release(true) + } + // Preexisting quarantine must refuse without acquiring/waiting for an anchor + // row held by an independent transaction. This checks the CTE dependency. + const anchorOwner = await pool.connect() + let attempt: Promise | undefined + try { + await anchorOwner.query("BEGIN"); await anchorOwner.query("SELECT receipt_id FROM server_poll_executions WHERE receipt_id=$1 FOR UPDATE", [receipt]) + attempt = target.intent(); void attempt.catch(() => {}) + await bounded(assert.rejects(attempt, ServerPollJournalConflict), 2000, "Quarantined snapshot waited on an anchor row") + } finally { + await anchorOwner.query("ROLLBACK"); anchorOwner.release(true) + if (attempt) await attempt.catch(() => {}) + } + console.log("Owned journal snapshot SQL:", JSON.stringify({ groups: 7, currentMigrations: MIGRATION_MANIFEST.length, + actualMetadataLocalSettingsAndReadOnly: true, pooledSettingsAndTransactionRestored: true, explicitReadCommittedOverridesSessionRepeatableRead: true, + actualPrivateFactoryAndSnapshotAppend: true, actualQuotaGuardBlocksQuarantine: true, exactBlockingBackendChecked: true, + committedQuarantineRefusesNextAppend: true, preexistingQuarantineSkipsLockedAnchor: true, + actualMcpOwnedValidationPositive:true,actualMcpOwnedValidationRefusals:refusals.length,bothOriginalLocksObservedOnSameBackend:true,credentialRevokedWhileWaitingRefused:true, + nativeRequests: 0, syntheticRestJournalFixture: true,syntheticMcpOwnerRows:true, actualMcpAcceptance: false, productionDatabaseTouched: false })) +} finally { + try { for (const id of serverIds) await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [id]) } finally { await closePool() } +} diff --git a/backend/src/services/server-poll-journal-snapshot.test.ts b/backend/src/services/server-poll-journal-snapshot.test.ts new file mode 100644 index 00000000..a5986e00 --- /dev/null +++ b/backend/src/services/server-poll-journal-snapshot.test.ts @@ -0,0 +1,187 @@ +/** Component control flow with actual journal/private bridge/storage factories. + * SQL is an explicit in-memory double; AES/HMAC are real but use fixture keys. + * This does not claim PostgreSQL lock or production policy acceptance. */ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, randomUUID } from "node:crypto" +import { readFileSync } from "node:fs" +import { test } from "node:test" +import { runInNewContext } from "node:vm" +import { isDeepStrictEqual } from "node:util" +import { publicationLayout, serverPollMaintenancePublications } from "./server-poll-maintenance-groups.js" +import ts from "typescript" +import { z } from "zod" +import * as errorProvenance from "./server-poll-error-provenance.js" +import { createServerPollStorageForTests, PollStorageUnknown, PollStorageRefused } from "./server-poll-storage.js" +import { SERVER_POLL_NATIVE_READS } from "./server-poll-execution-model.js" +import { encodeServerPollJournalAnchor, openServerPollJournalAnchor } from "./server-poll-journal-anchor.js" +import { validateServerPollJournalReceipt } from "./server-poll-journal-validation.js" +import type { ServerPollJournalContext, ServerPollJournal } from "./server-poll-journal.js" +import type { ServerPollJournalStorage, ServerPollStorageJournalOptions } from "./server-poll-storage-journal.js" +import type { ProbeJournalCrypto } from "./server-probe-journal.js" + +type Row = Record +type State = { admission?: Row; anchors: Row[]; intents: Row[]; acks: Row[]; receipts: Row[]; blocked: Row[] } +const canonical = (value: unknown): string => JSON.stringify(value, (_key, item: unknown) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item) +const fingerprint = (value: unknown) => createHash("sha256").update(canonical(value)).digest("hex") +const compile = (name: string) => ts.transpileModule(readFileSync(new URL(name, import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText +const bridgeCode = compile("./server-poll-storage-journal.ts"), journalCode = compile("./server-poll-journal.ts") +function load(code: string, modules: Record) { + const exports: Record = {} + runInNewContext(code, { exports, require(name: string) { assert(Object.hasOwn(modules, name), name); return modules[name] }, + Date, Error, Object, Promise, Number, String, Buffer, JSON, RegExp, Map, Set, WeakMap, structuredClone, setTimeout, clearTimeout }, { filename: "actual-poll-snapshot-component.js" }) + return exports +} +async function fixture() { + const now = Date.now(), key = randomBytes(32), id = randomUUID(), hash = "a".repeat(64), controller = new AbortController() + const proof = async (value: unknown, domain: string) => createHmac("sha256", key).update(domain).update(fingerprint(value)).digest("hex") + const raw: ServerPollJournalContext = { identity: { kind: "rest", ownerId: "owned-component", executionId: randomUUID(), requestId: randomUUID(), authentication: "local", issuedAt: now - 1000, expiresAt: now + 60000 }, + plan: { version: 1, requiresHuman: true, scope: "global", request: { serverId: 1, receiptId: id }, around: new Date(now).toISOString(), beforeProof: hash, authorityProof: hash, bindingProof: hash, + affectedServerIds: [1], maintenance: [{ kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_d_20200101" }], mutationCost: 9 } } + const { bindingProof: _binding, ...core } = raw.plan; raw.plan.bindingProof = await proof(core, "production") + let state: State = { anchors: [], intents: [], acks: [], receipts: [], blocked: [] }, authority = true + let decryptHook: (() => void) | undefined, snapshotHook: ((value: Row) => void) | undefined, authorityBeforeSnapshot = false, authorityAfterSnapshot = false + const calls: string[] = [], releases: boolean[] = [], verifiedMacs: { domain: string; stage?: string }[] = [], events: string[] = [] + const authorityCheck = async () => { events.push("authority"); if (!authority || controller.signal.aborted) throw new PollStorageRefused() } + const crypto: ProbeJournalCrypto = { + async encrypt(text) { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv); const data = Buffer.concat([cipher.update(text), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), data]).toString("base64") }, + async decrypt(text) { const data = Buffer.from(text, "base64"), cipher = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)); cipher.setAuthTag(data.subarray(12, 28)); const plain = Buffer.concat([cipher.update(data.subarray(28)), cipher.final()]).toString(); decryptHook?.(); return plain }, + async proof(domain, value) { verifiedMacs.push({ domain, stage: value && typeof value === "object" && "stage" in value ? String(value.stage) : undefined }); return proof(value, "server-poll-journal-v1:" + domain) }, + } + const connect: Parameters[1]["connect"] = async () => { + let before: State | undefined + return { release(unsafe = false) { releases.push(unsafe) }, async query(text, input = []) { + const values = Array.from(input); calls.push(text); events.push(text) + if (text === "BEGIN") { before = structuredClone(state); return { rows: [] } } + if (text === "COMMIT") { before = undefined; return { rows: [] } } + if (text === "ROLLBACK") { assert(before); state = before; before = undefined; return { rows: [] } } + if (text.startsWith("SELECT set_config(") || text.includes("pg_advisory_xact_lock") || text.startsWith("SET LOCAL")) return { rows: [] } + if (text.startsWith("SELECT a.receipt_id AS admission_receipt_id") || text.startsWith("SELECT r.receipt_id FROM server_poll_archives")) return { rows: [] } + if (text.startsWith("SELECT scope,admissions,compacted_admissions")) return { rows: [{ scope: "global", admissions: 0, compacted_admissions: 0, archive_bytes: 0, reserved_rows: 0, reserved_bytes: 0 }] } + if (text.startsWith("SELECT * FROM server_poll_storage_admissions")) return { rows: state.admission ? [structuredClone(state.admission)] : [] } + if (text.startsWith("INSERT INTO server_poll_storage_admissions")) { const names = text.slice(text.indexOf("(") + 1, text.indexOf(")")).split(","); state.admission = Object.fromEntries(names.slice(0, values.length).map((name, index) => [name, values[index]])); state.admission.storage_state = "full"; state.admission.claim_attempted_at = null; return { rows: [{ receipt_id: id }] } } + if (text.startsWith("UPDATE server_poll_storage_admissions SET claim_token=NULL")) { assert(state.admission); state.admission.claim_token = null; state.admission.claim_attempted_at = new Date(now); return { rows: [{ receipt_id: id }] } } + if (text.includes("poll-storage-journal:existing-anchor")) return { rows: state.anchors.map(row => ({ receipt_id: row.receipt_id, identity_kind: row.identity_kind, execution_id: row.execution_id })) } + const tag = text.match(/poll-journal:([\w-]+)/)?.[1] + const stages = () => state.intents.map(row => ({ ...row, result_proof: null, outcome: null, ack_proof: null, ...state.acks.find(ack => ack.stage === row.stage) })) + if (tag === "servers") return { rows: [{ id: 1, enabled: true }] } + if (tag === "blocked") return { rows: structuredClone(state.blocked) } + if (tag === "anchor") return { rows: structuredClone(state.anchors) } + if (tag === "terminal") return { rows: structuredClone(state.receipts) } + if (tag === "stages") return { rows: structuredClone(stages()) } + if (tag === "write-snapshot") { + authorityBeforeSnapshot = events.at(-2) === "authority" + const value: Row = structuredClone({ blocked: state.blocked, anchors: state.anchors, terminal: state.receipts, stages: stages() }) + snapshotHook?.(value); return { rows: [{ snapshot: value }] } + } + const row = (names: string[]) => Object.fromEntries(names.map((name, index) => [name, values[index]])) + if (tag === "claim") { const value = row(["receipt_id", "identity_kind", "execution_id", "binding_proof", "context_ciphertext", "context_proof"]); state.anchors.push(value); return { rows: [value] } } + if (tag === "intent") { authorityAfterSnapshot = events.lastIndexOf("authority") > events.reduce((last, event, index) => event.includes("write-snapshot") ? index : last, -1); const value = row(["receipt_id", "stage", "before_proof", "effect_proof", "intent_proof"]); state.intents.push(value); return { rows: [value] } } + if (tag === "ack") { const value = row(["receipt_id", "stage", "result_proof", "outcome", "ack_proof"]); state.acks.push(value); return { rows: [value] } } + if (tag === "complete") { const value = row(["receipt_id", "receipt_ciphertext", "receipt_proof"]); state.receipts.push(value); return { rows: [value] } } + throw Error("Unexpected component SQL: " + text) + } } + } + // The private WeakMap entry is issued ONLY by the actual production bridge + // factory. Its actual storage factory owns transaction fences over SQL double. + const groupMethods = { + publicationLayout: (plan: Parameters[0]) => publicationLayout(structuredClone(plan)), + serverPollMaintenancePublications: (plan: Parameters[0]) => serverPollMaintenancePublications(structuredClone(plan)), + } + const bridge = load(bridgeCode, { "../modules/mcp/security.js": { fingerprint, privateSecretFingerprint: proof }, + "./server-poll-maintenance-groups.js": groupMethods, + "./server-partition-maintenance-lease.js": { isServerPollJournalOwner: () => assert.fail("Snapshot fixture uses standalone journal storage") }, + "./server-poll-storage.js": { createServerPollStorage: (binding: Parameters[0]) => createServerPollStorageForTests(binding, { now: Date.now, connect }), PollStorageRefused } }) as unknown as { + createServerPollStorageJournal(context: ServerPollJournalContext, options: ServerPollStorageJournalOptions): Promise + bindServerPollJournalAuthority(storage: ServerPollJournalStorage, context: ServerPollJournalContext, provider: ServerPollStorageJournalOptions["assertAuthority"]): unknown + PollStorageSpent: typeof PollStorageRefused + } + const storage = await bridge.createServerPollStorageJournal(raw, { deadlineAt: now + 60000, signal: controller.signal, assertAuthority: authorityCheck }) + assert.equal(typeof bridge.bindServerPollJournalAuthority(storage, raw, authorityCheck), "function") + assert.equal(bridge.bindServerPollJournalAuthority({ ...storage }, raw, authorityCheck), undefined, "Copying the facade must not forge private ownership") + // Normalize VM array/object prototypes before the real strict value comparer; + // production receives one realm, while this harness crosses a VM boundary. + const journalModule = load(journalCode, { "./server-poll-error-provenance.js": errorProvenance, zod: { z }, "node:util": { isDeepStrictEqual: (left: unknown, right: unknown) => isDeepStrictEqual(structuredClone(left), structuredClone(right)) }, "./server-poll-limits.js": { SERVER_POLL_WALL_BUDGET_MS: 180000 }, + "./server-poll-maintenance-groups.js": groupMethods, + "./server-poll-storage.js": { PollStorageUnknown }, "./server-poll-storage-journal.js": bridge, + "./server-poll-journal-anchor.js": { + encodeServerPollJournalAnchor: (context: Parameters[0], start: Parameters[1], signer: Parameters[2]) => encodeServerPollJournalAnchor(structuredClone(context), structuredClone(start), signer), + openServerPollJournalAnchor: (value: unknown, context: Parameters[1], signer: Parameters[2]) => openServerPollJournalAnchor(structuredClone(value), structuredClone(context), signer), + }, + "./server-poll-execution-model.js": { SERVER_POLL_NATIVE_READS }, "./server-poll-journal-validation.js": { validateServerPollJournalReceipt } }) as unknown as { createSqlServerPollJournal: (context: ServerPollJournalContext, deps: Record) => ServerPollJournal; ServerPollJournalConflict: new () => Error } + const journal = journalModule.createSqlServerPollJournal(raw, { storage, crypto, now: Date.now, executionProof: (value: unknown) => proof(value, "production"), assertAuthority: authorityCheck }) + await journal.claim(raw.plan.request, raw.plan.bindingProof) + const next = () => journal.intent({ receiptId: id, stage: "publish.0", beforeProof: hash, effectProof: hash }) + return { raw, journal, next, calls, releases, events, verifiedMacs, state: () => state, Conflict: journalModule.ServerPollJournalConflict, + revoke: () => { authority = false }, abort: () => controller.abort(), duringDecrypt: (hook: () => void) => { decryptHook = hook }, snapshot: (hook: (value: Row) => void) => { snapshotHook = hook }, + flags: () => ({ authorityBeforeSnapshot, authorityAfterSnapshot }), proof, hash } +} +async function natives(f: Awaited>) { + for (const read of SERVER_POLL_NATIVE_READS) { const value = { receiptId: f.raw.plan.request.receiptId, stage: "native." + read, beforeProof: f.hash, effectProof: f.hash }; await f.journal.intent(value); await f.journal.acknowledge({ receiptId: value.receiptId, stage: value.stage, resultProof: f.hash, outcome: "confirmed" }) } +} +test("actual private factory snapshot validates every retained row MAC and fences actual DML", async () => { + const f = await fixture(); await natives(f); f.calls.length = 0; f.verifiedMacs.length = 0 + await f.next() + assert.equal(f.calls.filter(sql => sql.includes("write-snapshot")).length, 1) + assert(!f.calls.some(sql => /poll-journal:(?:blocked|anchor|terminal|stages)\s/.test(sql))) + for (const read of SERVER_POLL_NATIVE_READS) for (const domain of ["intent", "ack"]) assert(f.verifiedMacs.some(value => value.domain === domain && value.stage === "native." + read)) + assert.deepEqual(f.flags(), { authorityBeforeSnapshot: true, authorityAfterSnapshot: true }) + const snapshot = f.calls.find(sql => sql.includes("write-snapshot"))! + assert(snapshot.includes("NOT EXISTS(SELECT 1 FROM blocked) FOR UPDATE OF e")); assert(snapshot.includes("ORDER BY i.stage LIMIT 2009")) +}) + +test("each old retained intent and ACK corruption refuses a later append", async () => { + for (const index of [0, 1, 2, 3]) for (const kind of ["intent", "ack"] as const) { + const f = await fixture(); await natives(f) + if (kind === "intent") f.state().intents[index].intent_proof = "b".repeat(64); else f.state().acks[index].ack_proof = "b".repeat(64) + const before = f.state().intents.length; await assert.rejects(f.next(), f.Conflict); assert.equal(f.state().intents.length, before) + } +}) +test("snapshot preconditions and malformed complete prefix never write", async () => { + for (const mutate of [ + (value: Row) => { value.blocked = [{}] }, (value: Row) => { value.anchors = [] }, + (value: Row) => { value.anchors = [{ ...(value.anchors as Row[])[0], execution_id: randomUUID() }] }, + (value: Row) => { value.anchors = [...value.anchors as Row[], ...(value.anchors as Row[])] }, + (value: Row) => { value.terminal = [{}] }, (value: Row) => { value.stages = null }, + (value: Row) => { value.stages = Array(10).fill({}) }, + (value: Row) => { (value.anchors as Row[])[0].context_ciphertext = "invalid" }, + (value: Row) => { (value.anchors as Row[])[0].context_proof = "b".repeat(64) }, + ]) { const f = await fixture(); await natives(f); f.snapshot(mutate); await assert.rejects(f.next(), f.Conflict); assert.equal(f.state().intents.length, 4) } +}) +test("abort or revocation during actual anchor decrypt refuses before INSERT", async () => { + for (const mode of ["abort", "revoke"] as const) { + const f = await fixture(); await natives(f); f.calls.length = 0; f.duringDecrypt(() => mode === "abort" ? f.abort() : f.revoke()) + await assert.rejects(f.next(), f.Conflict) + assert(!f.calls.some(sql => sql.includes("poll-journal:intent"))); assert(!f.calls.includes("COMMIT")) + if (mode === "abort") assert.equal(f.releases.at(-1), true); else assert(f.calls.includes("ROLLBACK")) + } +}) +test("sequence, duplicate and missing native ACK semantics remain enforced", async () => { + const f = await fixture(); await assert.rejects(f.next(), f.Conflict); await natives(f) + await assert.rejects(f.journal.intent({ receiptId: f.raw.plan.request.receiptId, stage: "publish.0", beforeProof: "b".repeat(64), effectProof: f.hash }), f.Conflict) + await assert.rejects(f.journal.intent({ receiptId: f.raw.plan.request.receiptId, stage: "publish.1", beforeProof: f.hash, effectProof: f.hash }), f.Conflict) + await f.next(); await assert.rejects(f.next(), f.Conflict) + const ack = { receiptId: f.raw.plan.request.receiptId, stage: "publish.0", resultProof: f.hash, outcome: "confirmed" as const } + await f.journal.acknowledge(ack); await assert.rejects(f.journal.acknowledge(ack), f.Conflict) + const missing = await fixture(); await natives(missing); missing.state().acks.pop(); await assert.rejects(missing.next(), missing.Conflict) +}) +test("revocation before or immediately after the snapshot never reaches INSERT", async () => { + for (const mode of ["before", "after"] as const) { + const f = await fixture(); await natives(f); f.calls.length = 0 + if (mode === "before") f.revoke(); else f.snapshot(() => f.revoke()) + await assert.rejects(f.next(), f.Conflict) + assert(!f.calls.some(sql => sql.includes("poll-journal:intent"))) + if (mode === "before") assert(!f.calls.some(sql => sql.includes("write-snapshot"))) + } +}) +test("authentic retained MACs cannot disguise a wrong maintenance effect", async () => { + const f = await fixture(); await natives(f) + for (let index = 0; index < 3; index++) { + const stage = "publish." + index + await f.journal.intent({ receiptId: f.raw.plan.request.receiptId, stage, beforeProof: f.hash, effectProof: f.hash }) + await f.journal.acknowledge({ receiptId: f.raw.plan.request.receiptId, stage, resultProof: f.hash, outcome: "confirmed" }) + } + await assert.rejects(f.journal.complete({ receiptId: f.raw.plan.request.receiptId, configurationVerified: true, complete: true, maintenanceComplete: true, + nativeReads: 4, confirmedPublications: 3, completedPublications: 3, afterProof: f.hash, observation: "online", + snapshot: { id: 1, serverId: 1, status: "online", polledAt: f.raw.plan.around } }, f.raw.plan.bindingProof), f.Conflict) + assert.equal(f.state().receipts.length, 0) +}) diff --git a/backend/src/services/server-poll-journal-validation.ts b/backend/src/services/server-poll-journal-validation.ts new file mode 100644 index 00000000..d3ba8af7 --- /dev/null +++ b/backend/src/services/server-poll-journal-validation.ts @@ -0,0 +1,26 @@ +import { publicationLayout } from "./server-poll-maintenance-groups.js" +import type { ServerPollPlan, ServerPollReceipt } from "./server-poll-execution.js" +import { SERVER_POLL_NATIVE_READS } from "./server-poll-execution-model.js" + +export interface ServerPollJournalStage extends Record { + stage: string; before_proof: string; effect_proof: string; intent_proof: string + result_proof: string | null; outcome: string | null; ack_proof: string | null +} +export const serverPollPublications = (rows: ServerPollJournalStage[]) => rows.filter(row => row.stage.startsWith("publish.")).sort((a, b) => Number(a.stage.slice(8)) - Number(b.stage.slice(8))) + +/** Shared completion rules only. No authority, expiry, SQL or execution path. + * Callers separately authenticate context, stage MACs and maintenance effects. */ +export function validateServerPollJournalReceipt(plan: ServerPollPlan, receipt: ServerPollReceipt, rows: ServerPollJournalStage[], executionStartProof?: string): void { + const must = (value: unknown) => { if (!value) throw Error("Invalid complete server poll receipt") } + must(receipt.receiptId === plan.request.receiptId && /^[a-f0-9]{64}$/.test(receipt.afterProof)) + const native = new Set(SERVER_POLL_NATIVE_READS.map(value => "native." + value)) + const pubs = serverPollPublications(rows), natives = rows.filter(row => native.has(row.stage)) + const epochPlan = plan.version === 3 || plan.version === 4, count = publicationLayout(plan).maintenancePublications + const start = executionStartProof ?? (epochPlan ? undefined : plan.beforeProof) + must(typeof start === "string" && /^[a-f0-9]{64}$/.test(start) && (epochPlan || start === plan.beforeProof)) + must(new Set(natives.map(row => row.stage)).size === 4 && natives.every(row => row.before_proof === start)) + must(pubs.every((row, index) => row.before_proof === (index === 0 ? start : pubs[index - 1].result_proof))) + must(receipt.configurationVerified === true && receipt.complete === true && receipt.maintenanceComplete === true && receipt.nativeReads === 4 && natives.length === 4 && rows.every(row => row.ack_proof !== null) && pubs.length === receipt.confirmedPublications && pubs.length === receipt.completedPublications && [count + 2, count + 4].includes(pubs.length) && pubs.every((row, index) => row.stage === "publish." + index && row.outcome === "confirmed") && pubs.at(-1)?.result_proof === receipt.afterProof && ["online", "offline"].includes(receipt.observation)) + must((receipt.observation !== "online" || natives.every(row => row.outcome === "confirmed")) && receipt.snapshot.serverId === plan.request.serverId && receipt.snapshot.status === receipt.observation && Object.keys(receipt.snapshot).every(key => ["id", "serverId", "polledAt", "status", "latencyMs", "rosVersion", "boardName", "uptime", "cpuLoad", "freeMemory", "totalMemory", "identityName"].includes(key)) && Buffer.byteLength(JSON.stringify(receipt)) <= 131072) + must(Object.keys(receipt).every(key => ["receiptId", "configurationVerified", "complete", "maintenanceComplete", "nativeReads", "confirmedPublications", "completedPublications", "afterProof", "observation", "snapshot"].includes(key)) && Object.values(receipt.snapshot).every(value => value === null || typeof value === "string" || typeof value === "number" && Number.isFinite(value))) +} diff --git a/backend/src/services/server-poll-journal.sql-helper.ts b/backend/src/services/server-poll-journal.sql-helper.ts new file mode 100644 index 00000000..51f81064 --- /dev/null +++ b/backend/src/services/server-poll-journal.sql-helper.ts @@ -0,0 +1,156 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import type { QueryConfig } from "pg" +import type { ServerPollJournalContext } from "./server-poll-journal.js" +import type { ServerPollReceipt } from "./server-poll-execution.js" +import type { ProbeJournalConnections } from "./server-probe-journal.js" + +/** Explicitly called by the disposable runner only. No runtime import, SQL, + * migration, native request, deletion or registry mutation occurs on import. */ +export async function acceptServerPollJournalSql(){ + assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL,"true") + const url=new URL(process.env.DATABASE_URL??"") + assert.equal(url.hostname,"127.0.0.1");assert.equal(url.pathname,"/postgres") + assert(["postgres:","postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const {pool}=await import("../db/index.js") + const security=await import("../modules/mcp/security.js") + const {createSqlServerPollJournal,SERVER_POLL_JOURNAL_DDL,ServerPollJournalConflict,ServerPollJournalOutcomeUnknown}=await import("./server-poll-journal.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name,"postgres") + assert.equal((await pool.query("SELECT to_regclass('public.server_poll_executions') AS name")).rows[0].name,null,"Poll journal proposal must not already be installed") + const previousMcpEnabled=(await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean + const ids:number[]=[],proof=(value:unknown)=>security.privateSecretFingerprint(security.fingerprint(value),"server-poll-production-v1") + const statements:string[]=[],releases:boolean[]=[] + const handledClients=new WeakSet() + let cases=0,loseCommit=false,lostCommits=0 + const connections:ProbeJournalConnections={async connect(){ + const client=await pool.connect();if(!handledClients.has(client)){handledClients.add(client);client.on("error",()=>{})} + return {query:async(text,values)=>{statements.push(text);const query:QueryConfig&{query_timeout:number}={text,values,query_timeout:5000};const result=await client.query>(query);if(text==="COMMIT"&&loseCommit){loseCommit=false;lostCommits++;throw Error("Synthetic lost ACK AFTER actual COMMIT")};return result},release:destroy=>{releases.push(!!destroy);client.release(destroy)}} + }} + const reject=async(promise:Promise)=>{await assert.rejects(promise);cases++} + try{ + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + await pool.query(SERVER_POLL_JOURNAL_DDL) + ids.push(...(await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('poll-journal-owned-source','127.0.0.1',9,true),('poll-journal-owned-peer','127.0.0.1',9,true) RETURNING id")).rows.map(row=>Number(row.id))) + const allServerIds=(await pool.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>Number(row.id));assert(allServerIds.length<=1000) + const fixture=async(kind:"mcp"|"jwt"|"local"="local")=>{ + const issuedAt=Date.now()-1000,expiresAt=Date.now()+600000,executionId=randomUUID(),ownerId="poll-owned-"+randomUUID() + const identity:ServerPollJournalContext["identity"]=kind==="mcp"?{kind,ownerId,executionId,connectionId:randomUUID(),credentialId:randomUUID(),operationName:"servers.poll.run",issuedAt,expiresAt}:{kind:"rest",authentication:kind,ownerId,executionId,requestId:"accepted-owned-request-"+randomUUID(),issuedAt,expiresAt} + const around=new Date().toISOString(),beforeProof=await proof({ownedSource:ids,version:1}) + const plan:ServerPollJournalContext["plan"]={version:1,requiresHuman:true,scope:"global",request:{serverId:ids[0],receiptId:randomUUID()},around,beforeProof,authorityProof:await proof({identity,scope:ids}),bindingProof:"a".repeat(64),maintenance:[ + {kind:"partition.ensure",parent:"server_snapshots",name:"server_snapshots_w_20990101",from:"2099-01-01T00:00:00.000Z",to:"2099-01-08T00:00:00.000Z"}, + {kind:"partition.storage",parent:"flow_daily_facts",name:"flow_daily_facts_m_209901",settings:{fillfactor:"70",autovacuum_vacuum_scale_factor:"0.05",autovacuum_vacuum_cost_limit:"2000"}}, + {kind:"partition.drop",parent:"traffic_samples",name:"traffic_samples_w_20000101"}, + ],affectedServerIds:[...allServerIds],mutationCost:11} + const {bindingProof:_initial,...core}=plan;plan.bindingProof=await proof(core) + let planId:string|undefined,live=true + if(identity.kind==="mcp"){ + const permissions=["mm:servers:write"],policy={id:identity.connectionId,version:1,status:"active",mode:"approval",permissions,expiresAt:new Date(expiresAt).toISOString(),scope:{serverIds:allServerIds,operationIds:[identity.operationName],allowGlobal:true}} + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned poll journal SQL',$2,$3::jsonb,$4::jsonb,$5,$6,$7)",[identity.connectionId,ownerId,JSON.stringify(permissions),JSON.stringify(policy),await security.accessGeneration(),new Date(expiresAt),new Date(issuedAt)]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)",[identity.credentialId,identity.connectionId,randomUUID(),new Date(expiresAt)]) + planId=randomUUID() + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) VALUES($1,$2,$3,1,1,$4,$5::jsonb,$6::jsonb,'executing',$7)",[planId,identity.connectionId,identity.operationName,await security.accessGeneration(),JSON.stringify({sealed:await security.encryptSecret(JSON.stringify(plan))}),JSON.stringify({sealed:await security.encryptSecret(JSON.stringify({authorityProof:plan.authorityProof}))}),new Date(expiresAt)]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'running')",[executionId,planId,identity.connectionId,randomUUID()]) + } + const context:ServerPollJournalContext={identity,plan} + const deps={connections,executionProof:proof,assertAuthority:async(actual:Readonly)=>{ + assert(live,"Owned trusted JWT/local/MCP request revoked");assert.deepEqual(actual,context);assert(Date.now()Number(r.id)),allServerIds);assert.equal(servers.find(r=>Number(r.id)===ids[0])?.enabled,true) + if(identity.kind==="mcp"){ + const rows=(await pool.query("SELECT c.generation,c.policy,p.payload,p.evidence FROM mcp_operations o JOIN mcp_connections c ON c.id=o.connection_id JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND p.id=$2",[identity.executionId,planId])).rows + assert.equal(rows.length,1);assert.equal(rows[0].generation,await security.accessGeneration());assert.equal(rows[0].policy.scope.allowGlobal,true) + assert.deepEqual(JSON.parse(await security.decryptSecret(rows[0].payload.sealed)),plan);assert.deepEqual(JSON.parse(await security.decryptSecret(rows[0].evidence.sealed)),{authorityProof:plan.authorityProof}) + } + }} + const journal=createSqlServerPollJournal(context,deps),receiptId=plan.request.receiptId + const claim=(instance=journal)=>instance.claim(plan.request,plan.bindingProof) + const native=async(instance=journal)=>{ + const results=await Promise.allSettled(["identity","resource","interfaces","addresses"].map(async name=>{const stage="native."+name;await instance.intent({receiptId,stage,beforeProof,effectProof:await proof({read:name,source:ids[0]})});await instance.acknowledge({receiptId,stage,resultProof:await proof({name,synthetic:true}),outcome:"confirmed"})})) + const failed=results.find(result=>result.status==="rejected");if(failed?.status==="rejected")throw failed.reason + } + const effects=[{kind:"server.name",serverId:ids[0],name:"Owned synthetic name",updatedAt:around},{kind:"flow-cache.invalidate",serverId:ids[0]},{kind:"snapshot.insert",snapshot:{serverId:ids[0],polledAt:around,status:"online"}},{kind:"snapshot.raw.clear",serverId:ids[0],beforeAt:around,ids:[]},...plan.maintenance] + let afterProof=beforeProof + const publications=async(instance=journal,wrongMaintenance=false)=>{for(const [index,effect]of effects.entries()){ + const stage="publish."+index,effectProof=await proof(wrongMaintenance&&index===effects.length-1?{kind:"wrong-maintenance"}:effect) + await instance.intent({receiptId,stage,beforeProof:afterProof,effectProof});afterProof=await proof({prior:afterProof,effect,index}) + await instance.acknowledge({receiptId,stage,resultProof:afterProof,outcome:"confirmed"}) + }} + const receipt=():ServerPollReceipt=>({receiptId,configurationVerified:true,complete:true,maintenanceComplete:true,nativeReads:4,confirmedPublications:effects.length,completedPublications:effects.length,afterProof,observation:"online",snapshot:{id:1,serverId:ids[0],polledAt:around,status:"online",identityName:"PUBLIC_SYNTHETIC_POLL_OBSERVATION"}}) + return {context,deps,journal,claim,native,publications,receipt,revoke:()=>{live=false},restore:()=>{live=true}} + } + for(const kind of ["jwt","local","mcp"] as const){ + const f=await fixture(kind);assert.equal((await f.claim()).state,"new");await f.native();await f.publications();await f.journal.complete(f.receipt(),f.context.plan.bindingProof) + const restarted=createSqlServerPollJournal(f.context,f.deps),stored=await f.claim(restarted);assert.equal(stored.state,"known");if(stored.state==="known")assert.deepEqual(stored.receipt,f.receipt()) + const anchor=(await pool.query("SELECT * FROM server_poll_executions WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0] + assert.equal(anchor.identity_kind,f.context.identity.kind);assert.equal(anchor.execution_id,f.context.identity.executionId);assert(!JSON.stringify(anchor).includes(f.context.identity.ownerId));const row=(await pool.query("SELECT * FROM server_poll_receipts WHERE receipt_id=$1",[anchor.receipt_id])).rows[0];assert(!JSON.stringify(row).includes("PUBLIC_SYNTHETIC_POLL_OBSERVATION")) + await reject(f.journal.complete(f.receipt(),f.context.plan.bindingProof));await reject(f.journal.intent({receiptId:anchor.receipt_id,stage:"publish.7",beforeProof:f.receipt().afterProof,effectProof:"a".repeat(64)}));cases++ + } + { + const f=await fixture("mcp"),other=createSqlServerPollJournal(f.context,f.deps) + assert.deepEqual((await Promise.all([f.claim(),f.claim(other)])).map(v=>v.state).sort(),["new","unresolved"]) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE execution_id=$1",[f.context.identity.executionId])).rows[0].n,1);cases++ + } + { + const f=await fixture();await f.claim();await reject(f.journal.complete({...f.receipt(),confirmedPublications:1,completedPublications:1},f.context.plan.bindingProof));await f.native();await f.publications(f.journal,true);await reject(f.journal.complete(f.receipt(),f.context.plan.bindingProof));cases++ + } + { + const f=await fixture();await f.claim();const stage={receiptId:f.context.plan.request.receiptId,stage:"native.identity",beforeProof:f.context.plan.beforeProof,effectProof:"a".repeat(64)};await f.journal.intent(stage);await reject(f.journal.intent(stage));const ack={receiptId:stage.receiptId,stage:stage.stage,resultProof:"b".repeat(64),outcome:"confirmed" as const};await f.journal.acknowledge(ack);await reject(f.journal.acknowledge(ack));cases++ + } + for(const mutation of ["scope","global","owner","credential","credential-expiry","operation","operation-state"] as const){ + const f=await fixture("mcp"),identity=f.context.identity;assert(identity.kind==="mcp") + if(mutation==="scope")await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$1",[identity.connectionId]) + if(mutation==="global")await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,allowGlobal}','false') WHERE id=$1",[identity.connectionId]) + if(mutation==="owner")await pool.query("UPDATE mcp_connections SET owner_id='foreign' WHERE id=$1",[identity.connectionId]) + if(mutation==="credential")await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1",[identity.credentialId]) + if(mutation==="credential-expiry")await pool.query("UPDATE mcp_credentials SET expires_at=clock_timestamp()-interval '1 second' WHERE id=$1",[identity.credentialId]) + if(mutation==="operation")await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,operationIds}','[]') WHERE id=$1",[identity.connectionId]) + if(mutation==="operation-state")await pool.query("UPDATE mcp_operations SET state='applied' WHERE id=$1",[identity.executionId]) + // Deliberately permissive trusted callback isolates actual SQL fences. + await reject(createSqlServerPollJournal(f.context,{...f.deps,assertAuthority:async()=>{}}).claim(f.context.plan.request,f.context.plan.bindingProof)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,0) + } + { + const f=await fixture("mcp");await f.claim();const identity=f.context.identity;assert(identity.kind==="mcp");await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1",[identity.credentialId]);await reject(f.native());assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,0) + } + { + const f=await fixture("jwt");await f.claim();f.revoke();await reject(f.native());f.restore();assert.equal((await f.claim()).state,"unresolved");cases++ + } + { + const f=await fixture("local");await pool.query("UPDATE servers SET enabled=false WHERE id=$1",[ids[0]]);try{await reject(f.claim())}finally{await pool.query("UPDATE servers SET enabled=true WHERE id=$1",[ids[0]])} + } + { + const f=await fixture();await f.claim();await f.native();await f.publications();await f.journal.complete(f.receipt(),f.context.plan.bindingProof) + for(const [table,column]of [["server_poll_executions","binding_proof"],["server_poll_intents","before_proof"],["server_poll_acks","result_proof"],["server_poll_receipts","receipt_proof"]])await reject(pool.query(`UPDATE ${table} SET ${column}=$2 WHERE receipt_id=$1`,[f.context.plan.request.receiptId,"c".repeat(64)])) + const other={...f.context,identity:{...f.context.identity,ownerId:"foreign-owner"}};await reject(createSqlServerPollJournal(other,{...f.deps,assertAuthority:async()=>{}}).claim(other.plan.request,other.plan.bindingProof));cases++ + } + for(const boundary of ["claim","intent","complete"] as const){ + const f=await fixture(),start=statements.length + if(boundary!=="claim")await f.claim() + if(boundary==="complete"){await f.native();await f.publications()} + const beforeLoss=statements.length;loseCommit=true + const work=boundary==="claim"?f.claim():boundary==="intent"?f.journal.intent({receiptId:f.context.plan.request.receiptId,stage:"native.identity",beforeProof:f.context.plan.beforeProof,effectProof:"a".repeat(64)}):f.journal.complete(f.receipt(),f.context.plan.bindingProof) + await assert.rejects(work,(error:unknown)=>error instanceof ServerPollJournalOutcomeUnknown&&!error.requiresDurableQuarantine) + assert(!statements.slice(beforeLoss).includes("ROLLBACK"));assert.equal(statements.slice(beforeLoss).filter(s=>s==="COMMIT").length,2) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_quarantines WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,1) + await assert.rejects(f.claim(createSqlServerPollJournal(f.context,f.deps)),ServerPollJournalConflict) + assert(statements.length>start);assert(releases.includes(true));await reject(pool.query("UPDATE server_poll_quarantines SET context_proof=$2 WHERE receipt_id=$1",[f.context.plan.request.receiptId,"d".repeat(64)]));cases++ + } + assert.equal(lostCommits,3) + { + const f=await fixture(),extra=Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('poll-journal-unbound-catalogue','127.0.0.1',9,false) RETURNING id")).rows[0].id);ids.push(extra) + await reject(createSqlServerPollJournal(f.context,{...f.deps,assertAuthority:async()=>{}}).claim(f.context.plan.request,f.context.plan.bindingProof)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,0) + } + assert(!statements.some(sql=>/^\s*(UPDATE|DELETE|DROP|ALTER)\b/i.test(sql)),"Journal must never issue inverse/configuration mutation statements") + // Observe append-only protection without issuing any diagnostic DELETE. + assert.equal((await pool.query("SELECT count(*)::int AS n FROM pg_trigger WHERE tgname=ANY($1::text[]) AND (tgtype::int & 8)=8",[["server_poll_execution_immutable","server_poll_intent_immutable","server_poll_ack_immutable","server_poll_receipt_immutable","server_poll_quarantine_immutable"]])).rows[0].n,5) + assert(cases>=25&&cases<=60);console.log(`Server poll journal SQL proposal: ${cases} scenarios passed; no routes or production migration activated`) + return cases + }finally{ + loseCommit=false + if(ids.length)await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])",[ids]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1",[previousMcpEnabled]) + } +} diff --git a/backend/src/services/server-poll-journal.test.ts b/backend/src/services/server-poll-journal.test.ts new file mode 100644 index 00000000..ae88ab8f --- /dev/null +++ b/backend/src/services/server-poll-journal.test.ts @@ -0,0 +1,165 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import { createSqlServerPollJournal, ServerPollJournalConflict, ServerPollJournalOutcomeUnknown, SERVER_POLL_JOURNAL_DDL, type ServerPollJournalContext } from "./server-poll-journal.js" +import type { ServerPollReceipt } from "./server-poll-execution.js" +import type { ProbeJournalConnections, ProbeJournalCrypto } from "./server-probe-journal.js" +import { PollStorageUnknown } from "./server-poll-storage.js" + +const digest=(v:unknown)=>createHash("sha256").update(JSON.stringify(v)).digest("hex") +const crypto:ProbeJournalCrypto={encrypt:async s=>Buffer.from(s).toString("base64"),decrypt:async s=>Buffer.from(s,"base64").toString(),proof:async(d,v)=>digest([d,v])} +const id="11111111-1111-4111-8111-111111111111",execution="22222222-2222-4222-8222-222222222222",hash="a".repeat(64) +const context=():ServerPollJournalContext=>{ + const plan:ServerPollJournalContext["plan"]={version:1,requiresHuman:true,scope:"global",request:{serverId:1,receiptId:id},around:new Date(1000).toISOString(),beforeProof:hash,authorityProof:hash,bindingProof:hash,maintenance:[{kind:"partition.drop",parent:"server_snapshots",name:"server_snapshots_d_20200101"}],affectedServerIds:[1,2],mutationCost:9} + const {bindingProof:_binding,...core}=plan;plan.bindingProof=digest(core) + return {identity:{kind:"rest",ownerId:"local-actor",executionId:execution,requestId:"accepted-request",authentication:"local",issuedAt:0,expiresAt:100000},plan} +} +type Row=Record +function fixture(raw=context()){ + const data={anchors:[] as Row[],intents:[] as Row[],acks:[] as Row[],receipts:[] as Row[],blocked:[] as Row[]} + const calls:string[]=[],released:boolean[]=[],parameters:unknown[][]=[] + let failCommit=false,failQuarantine=false,authority=true,owner=true,clock=1000 + const connections:ProbeJournalConnections={async connect(){let snapshot=structuredClone(data);return {release(v){released.push(!!v)},async query(text,values=[]){calls.push(text);parameters.push(values) + if(text==="BEGIN"||text==="BEGIN READ ONLY"){snapshot=structuredClone(data);return {rows:[]}} + if(text==="COMMIT"){if(failCommit){failCommit=false;throw Error("lost commit ACK")};return {rows:[]}} + if(text==="ROLLBACK"){Object.assign(data,snapshot);return {rows:[]}} + const tag=text.match(/poll-journal:(\w+)/)?.[1] + const row=(keys:string[])=>Object.fromEntries(keys.map((key,i)=>[key,values[i]])) + if(tag==="servers")return {rows:raw.plan.affectedServerIds.map(id=>({id,enabled:true}))} + if(tag==="owner"||tag==="active")return {rows:owner?[{id:execution}]:[]} + if(tag==="blocked")return {rows:data.blocked} + if(tag==="anchor")return {rows:data.anchors} + if(tag==="terminal")return {rows:data.receipts} + if(tag==="stages")return {rows:data.intents.map(r=>({...r,result_proof:null,outcome:null,ack_proof:null,...data.acks.find(a=>a.stage===r.stage)}))} + if(tag==="claim"){const r=row(["receipt_id","identity_kind","execution_id","binding_proof","context_ciphertext","context_proof"]);data.anchors.push(r);return {rows:[r]}} + if(tag==="intent"){const r=row(["receipt_id","stage","before_proof","effect_proof","intent_proof"]);data.intents.push(r);return {rows:[r]}} + if(tag==="ack"){const r=row(["receipt_id","stage","result_proof","outcome","ack_proof"]);data.acks.push(r);return {rows:[r]}} + if(tag==="complete"){const r=row(["receipt_id","receipt_ciphertext","receipt_proof"]);data.receipts.push(r);return {rows:[r]}} + if(tag==="quarantine"){if(failQuarantine)throw Error("marker unavailable");data.blocked.push(row(["receipt_id","identity_kind","execution_id","context_proof"]));return {rows:[]}} + if(tag==="lock"||text.startsWith("SET LOCAL"))return {rows:[]} + throw Error("unexpected SQL "+text) + }}}} + const deps={connections,crypto,now:()=>clock,executionProof:async(v:unknown)=>digest(v),assertAuthority:async()=>{if(!authority)throw Error("revoked")}} + const journal=createSqlServerPollJournal(raw,deps) + return {raw,journal,data,calls,parameters,released,deps,failCommit:()=>{failCommit=true},failMarker:()=>{failQuarantine=true},revoke:()=>{authority=false},denyOwner:()=>{owner=false},expire:()=>{clock=100000}} +} +async function claim(f:ReturnType){return f.journal.claim(f.raw.plan.request,f.raw.plan.bindingProof)} +async function native(f:ReturnType){for(const name of ["identity","resource","interfaces","addresses"]){const stage="native."+name;await f.journal.intent({receiptId:id,stage,beforeProof:hash,effectProof:hash});await f.journal.acknowledge({receiptId:id,stage,resultProof:hash,outcome:"confirmed"})}} +async function publications(f:ReturnType){for(let i=0;i<3;i++){const stage="publish."+i;await f.journal.intent({receiptId:id,stage,beforeProof:hash,effectProof:i===2?digest(f.raw.plan.maintenance[0]):hash});await f.journal.acknowledge({receiptId:id,stage,resultProof:hash,outcome:"confirmed"})}} +const receipt=():ServerPollReceipt=>({receiptId:id,configurationVerified:true,complete:true,maintenanceComplete:true,nativeReads:4,confirmedPublications:3,completedPublications:3,afterProof:hash,observation:"online",snapshot:{id:1,serverId:1,status:"online",polledAt:new Date(1000).toISOString()}}) +let checks=0 +async function test(name:string,work:()=>Promise|void){await work();checks++;console.log("ok",name)} +await test("inert import exposes proposal without applying schema",()=>{assert.match(SERVER_POLL_JOURNAL_DDL,/CREATE TABLE server_poll_quarantines/);assert.equal((SERVER_POLL_JOURNAL_DDL.match(/BEFORE UPDATE OR DELETE/g)??[]).length,5)}) +await test("real REST identity claim and unresolved attempt",async()=>{const f=fixture();assert.deepEqual(await claim(f),{state:"new"});assert.equal((await claim(f)).state,"unresolved");assert(!f.calls.some(s=>s.includes("poll-journal:owner")))}) +await test("all four reads and maintenance required before complete",async()=>{const f=fixture();await claim(f);await assert.rejects(f.journal.complete(receipt(),f.raw.plan.bindingProof),ServerPollJournalConflict);await native(f);await publications(f);await f.journal.complete(receipt(),f.raw.plan.bindingProof);assert.deepEqual((await claim(f)),{state:"known",bindingProof:f.raw.plan.bindingProof,receipt:receipt()})}) +await test("snapshot-only receipt rejected",async()=>{const f=fixture();await claim(f);await native(f);await assert.rejects(f.journal.complete({...receipt(),confirmedPublications:1,completedPublications:1},f.raw.plan.bindingProof),ServerPollJournalConflict)}) +await test("native intents may coexist but publications await all ACKs",async()=>{const f=fixture();await claim(f);for(const name of ["identity","resource","interfaces","addresses"])await f.journal.intent({receiptId:id,stage:"native."+name,beforeProof:hash,effectProof:hash});await assert.rejects(f.journal.intent({receiptId:id,stage:"publish.0",beforeProof:hash,effectProof:hash}),ServerPollJournalConflict)}) +await test("duplicate intent and ACK refused",async()=>{const f=fixture();await claim(f);const intent={receiptId:id,stage:"native.identity",beforeProof:hash,effectProof:hash};await f.journal.intent(intent);await assert.rejects(f.journal.intent(intent),ServerPollJournalConflict);const ack={receiptId:id,stage:intent.stage,resultProof:hash,outcome:"read-failed" as const};await f.journal.acknowledge(ack);await assert.rejects(f.journal.acknowledge(ack),ServerPollJournalConflict)}) +await test("publication sequence cannot skip",async()=>{const f=fixture();await claim(f);await native(f);await assert.rejects(f.journal.intent({receiptId:id,stage:"publish.1",beforeProof:hash,effectProof:hash}),ServerPollJournalConflict)}) +await test("maintenance effect hash cannot be replaced",async()=>{const f=fixture();await claim(f);await native(f);await publications(f);f.raw.plan.maintenance[0]={kind:"partition.drop",parent:"server_snapshots",name:"server_snapshots_d_20210101"};const replacement=createSqlServerPollJournal(f.raw,f.deps);await assert.rejects(replacement.complete(receipt(),f.raw.plan.bindingProof),ServerPollJournalConflict)}) +await test("stage tampering is detected",async()=>{const f=fixture();await claim(f);await native(f);f.data.intents[0].effect_proof="b".repeat(64);await assert.rejects(f.journal.intent({receiptId:id,stage:"publish.0",beforeProof:hash,effectProof:hash}),ServerPollJournalConflict)}) +await test("receipt ciphertext tampering is detected",async()=>{const f=fixture();await claim(f);await native(f);await publications(f);await f.journal.complete(receipt(),f.raw.plan.bindingProof);f.data.receipts[0].receipt_ciphertext=Buffer.from(JSON.stringify({...receipt(),observation:"offline"})).toString("base64");await assert.rejects(claim(f),ServerPollJournalConflict)}) +await test("authority revocation stops before SQL",async()=>{const f=fixture();f.revoke();await assert.rejects(claim(f),ServerPollJournalConflict);assert.equal(f.calls.length,0)}) +await test("expired trusted identity stops before SQL",async()=>{const f=fixture();f.expire();await assert.rejects(claim(f),ServerPollJournalConflict);assert.equal(f.calls.length,0)}) +await test("MCP SQL checks exact actual operation credential connection",async()=>{const raw=context();raw.identity={kind:"mcp",ownerId:"owner",executionId:execution,connectionId:"33333333-3333-4333-8333-333333333333",credentialId:"44444444-4444-4444-8444-444444444444",operationName:"server.poll",issuedAt:0,expiresAt:100000};const f=fixture(raw);await claim(f);const i=f.calls.findIndex(s=>s.includes("poll-journal:owner"));assert.deepEqual(f.parameters[i].slice(0,4),[execution,raw.identity.connectionId,"owner",raw.identity.credentialId]);f.denyOwner();await assert.rejects(claim(f),ServerPollJournalConflict)}) +await test("lost COMMIT ACK is quarantined with no rollback or replay",async()=>{const f=fixture();f.failCommit();await assert.rejects(claim(f),(e:unknown)=>e instanceof ServerPollJournalOutcomeUnknown&&!e.requiresDurableQuarantine);assert.equal(f.calls.filter(s=>s==="COMMIT").length,2);assert(!f.calls.includes("ROLLBACK"));assert.equal(f.data.anchors.length,1);assert.equal(f.data.blocked.length,1);await assert.rejects(claim(f),ServerPollJournalConflict);await assert.rejects(createSqlServerPollJournal(f.raw,f.deps).claim(f.raw.plan.request,f.raw.plan.bindingProof),ServerPollJournalConflict)}) +await test("quarantine outage remains explicit unresolved failure",async()=>{const f=fixture();f.failCommit();f.failMarker();await assert.rejects(claim(f),(e:unknown)=>e instanceof ServerPollJournalOutcomeUnknown&&e.requiresDurableQuarantine);assert(!f.calls.includes("ROLLBACK"));await assert.rejects(claim(f),ServerPollJournalConflict)}) +await test("lost intent ACK cannot dispatch again on a fresh instance",async()=>{const f=fixture();await claim(f);f.failCommit();await assert.rejects(f.journal.intent({receiptId:id,stage:"native.identity",beforeProof:hash,effectProof:hash}),ServerPollJournalOutcomeUnknown);assert.equal(f.data.intents.length,1);await assert.rejects(createSqlServerPollJournal(f.raw,f.deps).claim(f.raw.plan.request,f.raw.plan.bindingProof),ServerPollJournalConflict)}) +await test("lost terminal COMMIT ACK quarantines even an otherwise valid receipt",async()=>{const f=fixture();await claim(f);await native(f);await publications(f);f.failCommit();await assert.rejects(f.journal.complete(receipt(),f.raw.plan.bindingProof),ServerPollJournalOutcomeUnknown);assert.equal(f.data.receipts.length,1);await assert.rejects(createSqlServerPollJournal(f.raw,f.deps).claim(f.raw.plan.request,f.raw.plan.bindingProof),ServerPollJournalConflict)}) +await test("wrong maintenance hash is refused despite authentic stage HMAC",async()=>{const f=fixture();await claim(f);await native(f);for(let i=0;i<3;i++){const stage="publish."+i;await f.journal.intent({receiptId:id,stage,beforeProof:hash,effectProof:hash});await f.journal.acknowledge({receiptId:id,stage,resultProof:hash,outcome:"confirmed"})};await assert.rejects(f.journal.complete(receipt(),f.raw.plan.bindingProof),ServerPollJournalConflict)}) +await test("immutable restored context rejects another REST owner",async()=>{const f=fixture();await claim(f);const other=structuredClone(f.raw);other.identity.ownerId="another-owner";await assert.rejects(createSqlServerPollJournal(other,f.deps).claim(other.plan.request,other.plan.bindingProof),ServerPollJournalConflict)}) +await test("new publication before proof must equal last confirmed proof",async()=>{const f=fixture();await claim(f);await native(f);await assert.rejects(f.journal.intent({receiptId:id,stage:"publish.0",beforeProof:"b".repeat(64),effectProof:hash}),ServerPollJournalConflict)}) +async function completed(){const f=fixture();await claim(f);await native(f);await publications(f);await f.journal.complete(receipt(),f.raw.plan.bindingProof);f.calls.length=0;return f} +await test("readExisting missing anchor or terminal never claims or mutates",async()=>{ + const f=fixture();assert.equal(await f.journal.readExisting(),undefined);assert.equal(f.data.anchors.length,0) + await claim(f);f.calls.length=0;assert.equal(await f.journal.readExisting(),undefined) + assert(f.calls.includes("BEGIN READ ONLY"));assert(!f.calls.some(s=>/INSERT|UPDATE|DELETE|FOR UPDATE/.test(s))) +}) +await test("readExisting authenticates complete receipt without native replay or ledger mutation",async()=>{ + const f=await completed(),before=structuredClone(f.data) + assert.deepEqual(await f.journal.readExisting(),receipt());assert.deepEqual(f.data,before) + assert(!f.calls.some(s=>/INSERT|UPDATE|DELETE|FOR UPDATE/.test(s)));assert.equal(f.calls.filter(s=>s==="COMMIT").length,1) +}) +await test("readExisting refuses context, intent, ACK, terminal MAC tamper and quarantine",async()=>{ + for(const change of [(f:Awaited>)=>{f.data.anchors[0].context_proof=hash}, + (f:Awaited>)=>{f.data.anchors[0].context_ciphertext=Buffer.from(JSON.stringify({...f.raw,identity:{...f.raw.identity,ownerId:"foreign"}})).toString("base64")}, + (f:Awaited>)=>{f.data.intents[0].effect_proof="b".repeat(64)}, + (f:Awaited>)=>{f.data.acks[0].ack_proof=hash}, + (f:Awaited>)=>{f.data.receipts[0].receipt_proof=hash}, + (f:Awaited>)=>{f.data.blocked.push({receipt_id:id})}]){ + const f=await completed();change(f);await assert.rejects(f.journal.readExisting(),ServerPollJournalConflict) + assert(!f.calls.some(s=>/INSERT|UPDATE|DELETE/.test(s))) + } +}) +await test("readExisting revocation or original expiry refuses before I/O",async()=>{ + for(const stop of [(f:ReturnType)=>f.revoke(),(f:ReturnType)=>f.expire()]){ + const f=await completed();stop(f);await assert.rejects(f.journal.readExisting(),ServerPollJournalConflict);assert.equal(f.calls.length,0) + } +}) +await test("readExisting rechecks authority after private decrypt and never returns a partial receipt",async()=>{ + const f=await completed();const reader=createSqlServerPollJournal(f.raw,{...f.deps,crypto:{...crypto,decrypt:async value=>{const result=await crypto.decrypt(value);f.revoke();return result}}}) + await assert.rejects(reader.readExisting(),ServerPollJournalConflict);assert(!f.calls.some(s=>s.includes("poll-journal:quarantine"))) +}) +await test("readExisting uncertain COMMIT destroys once without quarantine, rollback or retry",async()=>{ + const f=await completed(),releases=f.released.length;f.failCommit() + await assert.rejects(f.journal.readExisting(),ServerPollJournalConflict) + assert.deepEqual(f.released.slice(releases),[true]);assert.equal(f.calls.filter(s=>s==="COMMIT").length,1) + assert(!f.calls.some(s=>/INSERT|UPDATE|DELETE|ROLLBACK/.test(s)));assert.equal(f.data.blocked.length,0) +}) +await test("storage read unknown never enters write transaction or quarantine",async()=>{ + const raw=context();let quarantines=0,writes=0 + const journal=createSqlServerPollJournal(raw,{crypto,now:()=>1000,executionProof:async(v:unknown)=>digest(v),assertAuthority:async()=>{},storage:{ + read:async()=>{throw new PollStorageUnknown()},transaction:async()=>{writes++;throw Error("No transaction allowed")},quarantine:async()=>{quarantines++;return true}, + }}) + await assert.rejects(journal.readExisting(),ServerPollJournalConflict);assert.equal(quarantines,0);assert.equal(writes,0) +}) +await test("legacy read deadline destroys its backend and refuses SQL after a late crypto gap",async()=>{ + const f=await completed();let resume!:()=>void,entered!:()=>void + const waiting=new Promise(done=>{resume=done}),started=new Promise(done=>{entered=done}) + const reader=createSqlServerPollJournal(f.raw,{...f.deps,deadlineAt:1040,crypto:{...crypto,decrypt:async value=>{entered();await waiting;return crypto.decrypt(value)}}}) + const releases=f.released.length,pending=reader.readExisting();await started + await assert.rejects(pending,ServerPollJournalConflict);assert.deepEqual(f.released.slice(releases),[true]) + const statements=f.calls.length;resume();await new Promise(done=>setImmediate(done));assert.equal(f.calls.length,statements) + assert(!f.calls.some(s=>/INSERT|UPDATE|DELETE|COMMIT|ROLLBACK/.test(s))) +}) +function v3Fixture(){ + const raw=context();raw.plan.version=3;raw.plan.approvalProof="b".repeat(64) + const {bindingProof:_old,...core}=raw.plan;void _old;raw.plan.bindingProof=digest(core) + return fixture(raw) +} +const freshStart={fullProof:"e".repeat(64),approvalProof:"b".repeat(64)} +await test("v3 missing or mismatched start refuses before SQL and old versions reject an added start",async()=>{ + const f=v3Fixture() + await assert.rejects(claim(f),ServerPollJournalConflict) + await assert.rejects(f.journal.claim(f.raw.plan.request,f.raw.plan.bindingProof,{...freshStart,approvalProof:hash}),ServerPollJournalConflict) + assert.equal(f.calls.length,0) + const old=fixture();await assert.rejects(old.journal.claim(old.raw.plan.request,old.raw.plan.bindingProof,freshStart),ServerPollJournalConflict) + assert.equal(old.calls.length,0) +}) +await test("v3 persists a fresh signed start and a new journal instance authenticates it without process state",async()=>{ + const f=v3Fixture() + assert.deepEqual(await f.journal.claim(f.raw.plan.request,f.raw.plan.bindingProof,freshStart),{state:"new"}) + const persistedPayload=JSON.parse(await crypto.decrypt(String(f.data.anchors[0].context_ciphertext))) + const {executionStart:_start,...originalContext}=persistedPayload;void _start + assert.deepEqual(originalContext,f.raw) + assert.equal(f.data.anchors[0].context_proof,await crypto.proof("context",originalContext)) + const writer=createSqlServerPollJournal(f.raw,f.deps) + await assert.rejects(writer.intent({receiptId:id,stage:"native.identity",beforeProof:hash,effectProof:hash}),ServerPollJournalConflict) + for(const name of ["identity","resource","interfaces","addresses"]){const stage="native."+name;await writer.intent({receiptId:id,stage,beforeProof:freshStart.fullProof,effectProof:hash});await writer.acknowledge({receiptId:id,stage,resultProof:hash,outcome:"confirmed"})} + for(let i=0;i<3;i++){const stage="publish."+i;await writer.intent({receiptId:id,stage,beforeProof:i===0?freshStart.fullProof:hash,effectProof:i===2?digest(f.raw.plan.maintenance[0]):hash});await writer.acknowledge({receiptId:id,stage,resultProof:hash,outcome:"confirmed"})} + await writer.complete(receipt(),f.raw.plan.bindingProof) + const reader=createSqlServerPollJournal(f.raw,f.deps) + assert.deepEqual(await reader.readExisting(),receipt()) + const encoded=JSON.parse(await crypto.decrypt(String(f.data.anchors[0].context_ciphertext))) + assert.equal(encoded.executionStart.fullProof,freshStart.fullProof);assert.deepEqual(encoded.plan,f.raw.plan) + encoded.executionStart.fullProof=hash + f.data.anchors[0].context_ciphertext=await crypto.encrypt(JSON.stringify(encoded)) + await assert.rejects(createSqlServerPollJournal(f.raw,f.deps).readExisting(),ServerPollJournalConflict) +}) +await test("v3 lost claim acknowledgement quarantines the anchored start and forbids replay",async()=>{ + const f=v3Fixture();f.failCommit() + await assert.rejects(f.journal.claim(f.raw.plan.request,f.raw.plan.bindingProof,freshStart),ServerPollJournalOutcomeUnknown) + assert.equal(f.data.anchors.length,1);assert.equal(f.data.blocked.length,1) + await assert.rejects(f.journal.claim(f.raw.plan.request,f.raw.plan.bindingProof,freshStart),ServerPollJournalConflict) + assert.equal(f.data.anchors.length,1) +}) +console.log(`server-poll-journal: ${checks} inert SQL scenarios passed`) diff --git a/backend/src/services/server-poll-journal.ts b/backend/src/services/server-poll-journal.ts new file mode 100644 index 00000000..26d4396f --- /dev/null +++ b/backend/src/services/server-poll-journal.ts @@ -0,0 +1,278 @@ +import { carryPollError } from "./server-poll-error-provenance.js" +import { serverPollMaintenancePublications } from "./server-poll-maintenance-groups.js" +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +import { isDeepStrictEqual } from "node:util" +import { encodeServerPollJournalAnchor, openServerPollJournalAnchor } from "./server-poll-journal-anchor.js" +import { z } from "zod" +import type { QueryConfig } from "pg" +import type { ServerPollDependencies, ServerPollPlan, ServerPollReceipt } from "./server-poll-execution.js" +import { SERVER_POLL_NATIVE_READS } from "./server-poll-execution-model.js" +import type { ProbeJournalConnections, ProbeJournalCrypto, ProbeJournalSql } from "./server-probe-journal.js" +import { PollStorageUnknown } from "./server-poll-storage.js" +import { PollStorageSpent, bindServerPollJournalAuthority, type ServerPollJournalStorage } from "./server-poll-storage-journal.js" +import { validateServerPollJournalReceipt, type ServerPollJournalStage } from "./server-poll-journal-validation.js" + +const hash=z.string().regex(/^[a-f0-9]{64}$/),uuid=z.string().uuid(),actor=z.string().min(1).max(200).refine(v=>v.trim()===v&&!/[\x00-\x1f\x7f]/.test(v)) +const base={ownerId:actor,executionId:uuid,issuedAt:z.number().int().nonnegative().safe(),expiresAt:z.number().int().positive().safe()} +const identitySchema=z.discriminatedUnion("kind",[ + z.object({...base,kind:z.literal("mcp"),connectionId:uuid,credentialId:uuid,operationName:z.string().regex(/^[a-z][a-z0-9_.]{0,199}$/)}).strict(), + z.object({...base,kind:z.literal("rest"),requestId:actor,authentication:z.enum(["jwt","local"])}).strict(), +]) +/** executionId must be the real mcp_operations.id or a trusted REST execution. + * REST identity comes from the accepted JWT/local request, never its JSON body. */ +export type ServerPollJournalIdentity=z.infer +export function parseServerPollJournalIdentity(value:unknown):ServerPollJournalIdentity{return identitySchema.parse(value)} +export interface ServerPollJournalContext {identity:ServerPollJournalIdentity;plan:ServerPollPlan} +export interface ServerPollJournalDependencies { + /** Validate current owner, credential, policy generation, global scope, ALL + * affected servers, protections and cancellation before every SQL boundary. */ + assertAuthority(context:Readonly):Promise + /** Exactly the same private proof function supplied to the poll executor. */ + executionProof:ServerPollDependencies["proof"] + connections?:ProbeJournalConnections + /** Opt-in bounded storage owns BEGIN, global quota lock, admission and COMMIT. + * Legacy fixture connectors and this owner are mutually exclusive. */ + storage?:ServerPollJournalStorage + crypto?:ProbeJournalCrypto + now?:()=>number + /** A trusted execution deadline. Default is 180 seconds from construction. */ + deadlineAt?:number +} +export class ServerPollJournalConflict extends Error {readonly automaticReplay=false;constructor(){super("Server poll journal identity, authority or immutable state mismatch; replay forbidden")}} +export class ServerPollJournalOutcomeUnknown extends Error {readonly automaticReplay=false;constructor(readonly requiresDurableQuarantine:boolean){super("Server poll journal outcome unknown; replay forbidden")}} +function must(value:unknown):asserts value {if(!value)throw new ServerPollJournalConflict()} +function createDefaultCrypto():ProbeJournalCrypto { + // A journal repeatedly authenticates its whole immutable prefix. Derive + // purpose keys once for THIS private batch instead of reopening the same + // key file for every historical stage. This caches no authority, generation, + // decrypted context, receipt or proof result; guard() remains fresh per SQL. + const signers=new Mapstring>>() + return { + async encrypt(value){return (await import("../modules/mcp/security.js")).encryptSecret(value)}, + async decrypt(value){return (await import("../modules/mcp/security.js")).decryptSecret(value)}, + async proof(domain,value){const s=await import("../modules/mcp/security.js");let signer=signers.get(domain);if(!signer){signer=s.privateFingerprinter("server-poll-journal-v1:"+domain);signers.set(domain,signer)};return (await signer)(s.fingerprint(value))}, + } +} +const defaultConnections:ProbeJournalConnections={async connect(){const c=await (await import("../db/index.js")).pool.connect();return {query:async(text,values)=>{const query:QueryConfig&{query_timeout:number}={text,values,query_timeout:5000};return c.query>(query)},release:destroy=>c.release(destroy)}}} +export interface ServerPollJournal extends Pick { + /** Authenticates existing immutable evidence; never creates or resumes work. */ + readExisting():Promise +} +type Stage = ServerPollJournalStage + +/** Append-only SQL journal, one independent transaction per durable boundary. + * No import starts SQL. Normal migration 0038 enrolls the inert schema. */ +export function createSqlServerPollJournal(raw:ServerPollJournalContext,deps:ServerPollJournalDependencies):ServerPollJournal { + const context=structuredClone(raw),identity=identitySchema.parse(context.identity),plan=context.plan + context.identity=identity + uuid.parse(plan.request.receiptId);hash.parse(plan.bindingProof) + const epochPlan=plan.version===3||plan.version===4 + must(epochPlan?hash.safeParse(plan.approvalProof).success:!Object.hasOwn(plan,"approvalProof")) + must(identity.expiresAt>identity.issuedAt&&(plan.version===1||plan.version===2||plan.version===3||plan.version===4)&&plan.requiresHuman===true&&plan.scope==="global"&&Number.isSafeInteger(plan.request.serverId)&&plan.request.serverId>0&&plan.maintenance.length<=2000&&plan.affectedServerIds.length<=1000&&plan.affectedServerIds.includes(plan.request.serverId)&&new Set(plan.affectedServerIds).size===plan.affectedServerIds.length&&plan.affectedServerIds.every(id=>Number.isSafeInteger(id)&&id>0)&&typeof deps.assertAuthority==="function"&&typeof deps.executionProof==="function") + let maintenance:ReturnType + try{maintenance=serverPollMaintenancePublications(plan)}catch{throw new ServerPollJournalConflict()} + const codec=deps.crypto??createDefaultCrypto(),connections=deps.connections??defaultConnections,now=deps.now??Date.now + must(!(deps.storage&&deps.connections)) + const deadlineAt=Math.min(identity.expiresAt,deps.deadlineAt??now()+SERVER_POLL_WALL_BUDGET_MS) + must(Number.isSafeInteger(deadlineAt)&&deadlineAt>now()) + let poisoned=false + let executionStartProof:string|undefined + const firstProof=()=>{must(!epochPlan||executionStartProof);return executionStartProof??plan.beforeProof} + const ownedStorage=deps.storage?bindServerPollJournalAuthority(deps.storage,context,deps.assertAuthority):undefined + const local=()=>{must(!poisoned&&now()>=identity.issuedAt&&now(){local();try{await deps.assertAuthority(structuredClone(context))}catch(error){throw carryPollError(new ServerPollJournalConflict(), error, "journal.auth")};local()} + const sqlGuard=async()=>{if(ownedStorage)local();else await guard()} + const acquire=async()=>{ + let timeout:ReturnType|undefined,abandoned=false + const pending=connections.connect() + try{return await Promise.race([pending,new Promise((_resolve,reject)=>{timeout=setTimeout(()=>{abandoned=true;reject(new ServerPollJournalConflict())},Math.max(1,Math.min(5000,deadlineAt-now())))})])} + finally{if(timeout)clearTimeout(timeout);if(abandoned)void pending.then(c=>c.release(true),()=>{})} + } + const seal=async(domain:string,value:unknown)=>{const proof=await codec.proof(domain,value);hash.parse(proof);return proof} + const contextProof=()=>seal("context",context) + const request=(receiptId:string,bindingProof=plan.bindingProof)=>must(receiptId===plan.request.receiptId&&bindingProof===plan.bindingProof) + const quarantine=async()=>{ + // This is a NEW append-only uncertainty marker, never a retry of COMMIT or + // of the original intent/ACK/publication. Revoked authority cannot erase it. + let c:Awaited>|undefined + try{c=await acquire();await c.query("BEGIN");await c.query("SET LOCAL statement_timeout='5000ms'");await c.query("/* poll-journal:quarantine */ INSERT INTO server_poll_quarantines(receipt_id,identity_kind,execution_id,context_proof) VALUES($1,$2,$3,$4) ON CONFLICT DO NOTHING",[plan.request.receiptId,identity.kind,identity.executionId,await contextProof()]);await c.query("COMMIT");return true}catch{return false}finally{c?.release(true)} + } + const validateTransaction=async(sql:ProbeJournalSql)=>{ + await sql.query("/* poll-journal:lock */ SELECT pg_advisory_xact_lock(hashtextextended($1,0))",["server-poll-execution-v1:"+identity.kind+":"+identity.executionId]) + await sql.query("/* poll-journal:lock */ SELECT pg_advisory_xact_lock(hashtextextended($1,0))",["server-poll-receipt-v1:"+plan.request.receiptId]) + const servers=await sql.query("/* poll-journal:servers */ SELECT id,enabled FROM servers ORDER BY id LIMIT 1001");must(servers.rows.length<=1000&&isDeepStrictEqual(servers.rows.map(r=>Number(r.id)).sort((a,b)=>a-b),[...plan.affectedServerIds].sort((a,b)=>a-b))&&servers.rows.find(r=>Number(r.id)===plan.request.serverId)?.enabled===true) + if(identity.kind==="mcp"){ + const owner=await sql.query(`/* poll-journal:owner */ SELECT o.id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_connections c ON c.id=o.connection_id JOIN mcp_credentials k ON k.connection_id=c.id + WHERE o.id=$1 AND c.id=$2 AND c.owner_id=$3 AND k.id=$4 AND k.revoked_at IS NULL AND k.expires_at>clock_timestamp() + AND c.owner_valid_until>clock_timestamp() AND c.policy->>'status'='active' AND c.policy->>'mode'<>'observer' + AND (c.policy->>'expiresAt')::timestamptz>clock_timestamp() AND c.policy#>'{scope,serverIds}' @> $5::jsonb + AND c.policy#>>'{scope,allowGlobal}'='true' + AND c.policy#>'{scope,operationIds}' @> to_jsonb(ARRAY[$6::text]) AND p.operation_id=$6`,[identity.executionId,identity.connectionId,identity.ownerId,identity.credentialId,JSON.stringify(plan.affectedServerIds),identity.operationName]);must(owner.rows.length===1) + } + } + const transaction=async(work:(sql:ProbeJournalSql)=>Promise,phase:"claim"|"write"="write"):Promise=>{ + if(deps.storage){ + await guard() + try{return await deps.storage.transaction(phase,async owner=>{ + const sql:ProbeJournalSql={async query(text,values){await sqlGuard();const result=await owner.query(text,values);await sqlGuard();return result}} + await validateTransaction(sql);const result=await work(sql);await guard();return result + })}catch(error){ + if(error instanceof PollStorageUnknown||error instanceof PollStorageSpent){poisoned=true;throw carryPollError(new ServerPollJournalOutcomeUnknown(!await deps.storage.quarantine()), error, "journal.tx")} + throw carryPollError(error instanceof ServerPollJournalConflict?error:new ServerPollJournalConflict(), error, "journal.tx") + } + } + + await guard();let c:Awaited> + try{c=await acquire()}catch{throw new ServerPollJournalConflict()} + let commitSent=false,committed=false,begun=false,destroy=false,released=false + const sql:ProbeJournalSql={async query(text,values){await guard();const result=await c.query(text,values);await guard();return result}} + try{ + await sql.query("BEGIN");begun=true;await sql.query("SET LOCAL statement_timeout='5000ms'") + await validateTransaction(sql) + const result=await work(sql);await guard();commitSent=true;await c.query("COMMIT");committed=true;await guard();return result + }catch(error){ + if(commitSent){poisoned=true;destroy=true;released=true;c.release(true);throw carryPollError(new ServerPollJournalOutcomeUnknown(!await quarantine()), error, "journal.tx")} + if(begun)try{await c.query("ROLLBACK")}catch{destroy=true} + throw carryPollError(error instanceof ServerPollJournalConflict?error:new ServerPollJournalConflict(), error, "journal.tx") + }finally{if(!released)c.release(destroy||!committed)} + } + const verifyAnchor=async(rows:Record[],required=true)=>{ + must(rows.length<=1&&(!required||rows.length===1)) + if(rows.length){const r=rows[0];must(r.receipt_id===plan.request.receiptId&&r.identity_kind===identity.kind&&r.execution_id===identity.executionId&&r.binding_proof===plan.bindingProof&&r.context_proof===await contextProof());must(typeof r.context_ciphertext==="string"&&Buffer.byteLength(r.context_ciphertext)<=2097152);const text=await codec.decrypt(r.context_ciphertext);must(Buffer.byteLength(text)<=1048576);const start=await openServerPollJournalAnchor(JSON.parse(text),context,codec);must(!executionStartProof||executionStartProof===start);executionStartProof=start} + return rows[0] + } + const anchor=async(sql:ProbeJournalSql,required=true,readOnly=false)=>{ + const blocked=await sql.query("/* poll-journal:blocked */ SELECT receipt_id FROM server_poll_quarantines WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) LIMIT 2",[plan.request.receiptId,identity.kind,identity.executionId]);must(!blocked.rows.length) + const rows=(await sql.query("/* poll-journal:anchor */ SELECT * FROM server_poll_executions WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3)"+(readOnly?"":" FOR UPDATE"),[plan.request.receiptId,identity.kind,identity.executionId])).rows + return verifyAnchor(rows,required) + } + const terminal=async(sql:ProbeJournalSql)=>(await sql.query("/* poll-journal:terminal */ SELECT * FROM server_poll_receipts WHERE receipt_id=$1",[plan.request.receiptId])).rows + const verifyStages=async(rows:Stage[])=>{ + must(rows.length<=maintenance.length+8) + for(const r of rows){must(r.intent_proof===await seal("intent",{receiptId:plan.request.receiptId,stage:r.stage,beforeProof:r.before_proof,effectProof:r.effect_proof}));if(r.ack_proof!==null)must(r.ack_proof===await seal("ack",{receiptId:plan.request.receiptId,stage:r.stage,resultProof:r.result_proof,outcome:r.outcome}))} + return rows + } + const stages=async(sql:ProbeJournalSql)=>verifyStages((await sql.query("/* poll-journal:stages */ SELECT i.*,a.result_proof,a.outcome,a.ack_proof FROM server_poll_intents i LEFT JOIN server_poll_acks a USING(receipt_id,stage) WHERE i.receipt_id=$1 ORDER BY i.stage LIMIT 2009",[plan.request.receiptId])).rows as Stage[]) + // Only the existing private production storage owner proves that the global + // quota/admission locks already fence ALL normal writes, including quarantine. + // Preserve historical 0038 connector semantics rather than infer that fence. + const writeStages=async(sql:ProbeJournalSql)=>{ + if(!ownedStorage){await anchor(sql);must(!(await terminal(sql)).length);return stages(sql)} + const result=(await sql.query(`/* poll-journal:write-snapshot */ WITH + blocked AS MATERIALIZED ( + SELECT receipt_id FROM server_poll_quarantines WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) LIMIT 2 + ), locked_anchor AS MATERIALIZED ( + SELECT e.* FROM server_poll_executions e WHERE (e.receipt_id=$1 OR (e.identity_kind=$2 AND e.execution_id=$3)) + AND NOT EXISTS(SELECT 1 FROM blocked) FOR UPDATE OF e + ), terminal_rows AS MATERIALIZED ( + SELECT r.* FROM server_poll_receipts r WHERE r.receipt_id=$1 AND EXISTS(SELECT 1 FROM locked_anchor) + ), stage_rows AS MATERIALIZED ( + SELECT i.*,a.result_proof,a.outcome,a.ack_proof FROM server_poll_intents i LEFT JOIN server_poll_acks a USING(receipt_id,stage) + WHERE i.receipt_id=$1 AND EXISTS(SELECT 1 FROM locked_anchor) ORDER BY i.stage LIMIT 2009 + ) SELECT jsonb_build_object( + 'blocked',COALESCE((SELECT jsonb_agg(to_jsonb(b)) FROM blocked b),'[]'::jsonb), + 'anchors',COALESCE((SELECT jsonb_agg(to_jsonb(e)) FROM locked_anchor e),'[]'::jsonb), + 'terminal',COALESCE((SELECT jsonb_agg(to_jsonb(r)) FROM terminal_rows r),'[]'::jsonb), + 'stages',COALESCE((SELECT jsonb_agg(to_jsonb(s) ORDER BY s.stage) FROM stage_rows s),'[]'::jsonb) + ) AS snapshot`,[plan.request.receiptId,identity.kind,identity.executionId])).rows + must(result.length===1) + const value=result[0].snapshot as Record + must(value&&typeof value==='object'&&!Array.isArray(value)&&Array.isArray(value.blocked)&&Array.isArray(value.anchors)&&Array.isArray(value.terminal)&&Array.isArray(value.stages)) + must(!value.blocked.length) + await verifyAnchor(value.anchors as Record[]) + must(!value.terminal.length) + return verifyStages(value.stages as Stage[]) + } + const native=new Set(SERVER_POLL_NATIVE_READS.map(v=>"native."+v)) + const publish=(rows:Stage[])=>rows.filter(r=>r.stage.startsWith("publish.")).sort((a,b)=>Number(a.stage.slice(8))-Number(b.stage.slice(8))) + const validateReceipt=(receipt:ServerPollReceipt,rows:Stage[])=>{ + try { validateServerPollJournalReceipt(plan,receipt,rows,firstProof()) } catch { throw new ServerPollJournalConflict() } + } + const storedReceipt=async(sql:ProbeJournalSql)=>{ + const receipts=await terminal(sql);must(receipts.length<=1) + if(!receipts.length)return undefined + const stored=receipts[0];must(typeof stored.receipt_ciphertext==="string"&&Buffer.byteLength(stored.receipt_ciphertext)<=262144) + const text=await codec.decrypt(stored.receipt_ciphertext);await guard() + must(Buffer.byteLength(text)<=131072&&stored.receipt_proof===await seal("receipt",text)) + const receipt=JSON.parse(text) as ServerPollReceipt,rows=await stages(sql);validateReceipt(receipt,rows) + const pubs=publish(rows),offset=pubs.length-maintenance.length + for(let i=0;i=>{ + await guard() + const read=async(sql:ProbeJournalSql)=>{ + await validateTransaction(sql) + if(!await anchor(sql,false,true))return undefined + return storedReceipt(sql) + } + if(deps.storage){ + try{return await deps.storage.read(async owner=>{ + const sql:ProbeJournalSql={async query(text,values){await sqlGuard();const result=await owner.query(text,values);await sqlGuard();return result}} + const result=await read(sql);await guard();return result + })}catch{throw new ServerPollJournalConflict()} + } + // Historical connector ownership: read-only work cannot admit, mutate or + // quarantine even if its final COMMIT acknowledgement is lost. + let c:Awaited>|undefined + let active=true,busy=false,released=false,begun=false,commitSent=false,committed=false + const close=(unsafe:boolean)=>{if(c&&!released){released=true;c.release(unsafe)}} + const live=()=>must(active&&!released&&now()void + const stopped=new Promise((_yes,reject)=>{stop=()=>{active=false;close(true);reject(new ServerPollJournalConflict())}}) + const timer=setTimeout(stop,Math.max(1,deadlineAt-now())) + const sql:ProbeJournalSql={async query(text,values){live();await guard();live();must(c&&!busy);busy=true;try{const result=await c.query(text,values);live();await guard();live();return result}finally{busy=false}}} + const completion=Promise.resolve().then(async()=>{ + c=await acquire();if(!active){close(true);throw new ServerPollJournalConflict()} + await sql.query("BEGIN READ ONLY");begun=true;await sql.query("SET LOCAL statement_timeout='5000ms'") + const result=await read(sql);await guard();live();commitSent=true;await c.query("COMMIT");committed=true;await guard();live();return result + }) + try{return await Promise.race([completion,stopped])} + catch{if(begun&&!commitSent&&active&&!busy&&c&&!released)try{await Promise.race([c.query("ROLLBACK"),stopped])}catch{/* Destroy below. */};throw new ServerPollJournalConflict()} + finally{active=false;clearTimeout(timer);close(!committed||busy)} + } + return { + readExisting, + async claim(value,bindingProof,rawStart){const start=rawStart?structuredClone(rawStart):undefined;must(epochPlan?start&&hash.safeParse(start.fullProof).success&&start.approvalProof===plan.approvalProof:start===undefined);request(value.receiptId,bindingProof);must(value.serverId===plan.request.serverId);const {bindingProof:binding,...core}=plan;must(binding===await deps.executionProof(core));return transaction(async sql=>{ + const existing=await anchor(sql,false) + if(!existing){if(identity.kind==="mcp"){const active=await sql.query("/* poll-journal:active */ SELECT o.id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND o.state IN ('pending','running') AND p.state='executing'",[identity.executionId]);must(active.rows.length===1)};const text=await encodeServerPollJournalAnchor(context,start,codec);executionStartProof=start?.fullProof;must(Buffer.byteLength(text)<=1048576);const encrypted=await codec.encrypt(text);must(Buffer.byteLength(encrypted)<=2097152);const inserted=await sql.query("/* poll-journal:claim */ INSERT INTO server_poll_executions(receipt_id,identity_kind,execution_id,binding_proof,context_ciphertext,context_proof) VALUES($1,$2,$3,$4,$5,$6) RETURNING receipt_id",[value.receiptId,identity.kind,identity.executionId,bindingProof,encrypted,await contextProof()]);must(inserted.rows.length===1);return {state:"new" as const}} + const receipts=await terminal(sql);must(receipts.length<=1) + if(!receipts.length)return {state:"unresolved" as const,bindingProof} + const stored=receipts[0];must(typeof stored.receipt_ciphertext==="string"&&Buffer.byteLength(stored.receipt_ciphertext)<=262144);const text=await codec.decrypt(stored.receipt_ciphertext);must(Buffer.byteLength(text)<=131072&&stored.receipt_proof===await seal("receipt",text));const receipt=JSON.parse(text) as ServerPollReceipt;validateReceipt(receipt,await stages(sql));return {state:"known" as const,bindingProof,receipt} + },"claim")}, + async intent(value){request(value.receiptId);hash.parse(value.beforeProof);hash.parse(value.effectProof);await transaction(async sql=>{const rows=await writeStages(sql);must(!rows.some(r=>r.stage===value.stage)) + if(native.has(value.stage))must(rows.every(r=>native.has(r.stage))&&value.beforeProof===firstProof()) + else {const pubs=publish(rows);must(value.stage==="publish."+pubs.length&&rows.filter(r=>native.has(r.stage)).length===4&&rows.every(r=>r.ack_proof!==null)&&pubs.length{const rows=await writeStages(sql),stage=rows.find(r=>r.stage===value.stage);must(stage&&stage.ack_proof===null&&(native.has(value.stage)||value.outcome==="confirmed"));const inserted=await sql.query("/* poll-journal:ack */ INSERT INTO server_poll_acks(receipt_id,stage,result_proof,outcome,ack_proof) VALUES($1,$2,$3,$4,$5) RETURNING stage",[value.receiptId,value.stage,value.resultProof,value.outcome,await seal("ack",value)]);must(inserted.rows.length===1)})}, + async complete(receipt,bindingProof){request(receipt.receiptId,bindingProof);await transaction(async sql=>{await anchor(sql);must(!(await terminal(sql)).length);const rows=await stages(sql);validateReceipt(receipt,rows);const pubs=publish(rows),offset=pubs.length-maintenance.length;for(let i=0;i { + const groups = groupAtomicMaintenance(manifest) + assert.deepEqual(groups.flatMap(group => group.effects), manifest) + assert(groups.every((group, index) => group.kind === "partition.atomic" && group.offset === index * 8 && group.effects.length <= 8 && group.effects.length > 0)) + assert.equal(groups.length, Math.ceil(manifest.length / 8)) + groups[0].effects[0].name = "changed-copy" + assert.notEqual(groups[0].effects[0].name, (manifest[0] as { name: string }).name) +}) +test("143 and maximum2000 logical effects retain original mutation cost and full coverage", () => { + for (const count of [0, 1, 8, 9, 143, 336, 2000]) { + const maintenance = Array.from({ length: count }, (_, index) => manifest[index % manifest.length]) + const groups = groupAtomicMaintenance(maintenance) + assert.deepEqual(groups.flatMap(group => group.effects), maintenance) + assert.deepEqual(publicationLayout({ version: 4, maintenance, mutationCost: count + 8, maintenanceGroupSize: 8 }), + { logicalMutationCost: count + 8, journalMaxStages: Math.ceil(count / 8) + 8, maintenancePublications: Math.ceil(count / 8) }) + } +}) +test("legacy1/2/3 never group and reject a new field, even explicit undefined", () => { + for (const version of [1, 2, 3]) { + const plan = { version, maintenance: manifest, mutationCost: manifest.length + 8 } + assert.equal(publicationLayout(plan).maintenancePublications, manifest.length) + assert.throws(() => publicationLayout({ ...plan, maintenanceGroupSize: 8 })) + assert.throws(() => publicationLayout({ ...plan, maintenanceGroupSize: undefined })) + } +}) +test("new protocol rejects missing/custom group size, old cost substitution and unknown version", () => { + const plan = { version: 4, maintenance: manifest, mutationCost: manifest.length + 8, maintenanceGroupSize: 8 } + for (const maintenanceGroupSize of [undefined, 0, 1, 7, 9, 2000, Infinity]) assert.throws(() => publicationLayout({ ...plan, maintenanceGroupSize })) + assert.throws(() => publicationLayout({ ...plan, mutationCost: Math.ceil(manifest.length / 8) + 8 })) + assert.throws(() => publicationLayout({ ...plan, version: 5 })) +}) +test("nonmaintenance, foreign identifiers, injected SQL and unknown effect fields refuse", () => { + const invalid: unknown[] = [null, { kind: "flow-cache.invalidate", serverId: 1 }, { kind: "snapshot.insert", snapshot: {} }, + { kind: "partition.drop", parent: "mcp_connections", name: "mcp_connections_d_20261030" }, + { kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_w_20261026;DROP TABLE servers" }, + { kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_w_20261026", sql: "SELECT 1" }, + { kind: "partition.storage", parent: "server_snapshots", name: "server_snapshots_w_20261026", settings: {} }] + for (const item of invalid) assert.throws(() => groupAtomicMaintenance([item as ServerPollEffect])) + assert.throws(() => groupAtomicMaintenance(Array(2001).fill(manifest[0]))) + assert.throws(() => groupAtomicMaintenance(Array(1))) +}) +test("range/leaf name disagreement, invalid date, noncanonical time and arbitrary storage refuse", () => { + const valid = { kind: "partition.ensure", parent: "server_snapshots", name: "server_snapshots_w_20261026", from: "2026-10-26T00:00:00.000Z", to: "2026-11-02T00:00:00.000Z" } as const + assert.equal(groupAtomicMaintenance([valid]).length, 1) + for (const changed of [{ to: "2026-11-03T00:00:00.000Z" }, { from: "2026-10-27T00:00:00.000Z" }, + { from: "2026-10-26T01:00:00.000Z" }, { name: "server_snapshots_w_20261102" }, { from: "2026-10-26" }, { from: "invalid" }]) { + assert.throws(() => groupAtomicMaintenance([{ ...valid, ...changed }])) + } + const storage = { kind: "partition.storage", parent: "flow_hour_facts", name: "flow_hour_facts_d_20261030", + settings: { fillfactor: "70", autovacuum_vacuum_scale_factor: "0.05", autovacuum_vacuum_cost_limit: "2000" } } as const + assert.equal(groupAtomicMaintenance([storage]).length, 1) + assert.throws(() => groupAtomicMaintenance([{ ...storage, settings: { ...storage.settings, fillfactor: "100" } }])) + assert.throws(() => groupAtomicMaintenance([{ ...storage, settings: { ...storage.settings, arbitrary: "1" } }])) +}) +test("compiler accepts only the exact group slice, without caller SQL, omission or reordering", () => { + const groups = groupAtomicMaintenance(manifest), group = groups[0] + const sql = compileAtomicMaintenanceGroup(manifest, group) + assert(sql.startsWith("DO $poll_atomic_maintenance$ BEGIN ")) + assert(sql.endsWith(" END $poll_atomic_maintenance$;")) + assert.equal((sql.match(/CREATE TABLE IF NOT EXISTS|ALTER TABLE|DROP TABLE IF EXISTS/g) ?? []).length, group.effects.length) + assert(!/\b(COMMIT|ROLLBACK|GRANT|TRUNCATE|CASCADE)\b/.test(sql)) + for (const changed of [{ ...group, offset: 1 }, { ...group, offset: 8 }, { ...group, sql: "SELECT 1" }, + { ...group, effects: group.effects.slice(1) }, { ...group, effects: [...group.effects].reverse() }, + { ...group, effects: [group.effects[0], ...group.effects] }, { ...group, offset: -8 }, { ...group, offset: Infinity }]) { + assert.throws(() => compileAtomicMaintenanceGroup(manifest, changed)) + } +}) +test("compiler preserves the final short group and fixed storage/drop statements", () => { + const maintenance: ServerPollEffect[] = [ + { kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_w_20261026" }, + { kind: "partition.storage", parent: "flow_hour_facts", name: "flow_hour_facts_d_20261030", settings: { fillfactor: "70", autovacuum_vacuum_scale_factor: "0.05", autovacuum_vacuum_cost_limit: "2000" } }] + const sql = compileAtomicMaintenanceGroup(maintenance, groupAtomicMaintenance(maintenance)[0]) + assert(sql.includes('DROP TABLE IF EXISTS "public"."server_snapshots_w_20261026";')) + assert(sql.includes('ALTER TABLE "public"."flow_hour_facts_d_20261030" SET (fillfactor=70,autovacuum_vacuum_scale_factor=0.05,autovacuum_vacuum_cost_limit=2000);')) + assert.throws(() => compileAtomicMaintenanceGroup([], { kind: "partition.atomic", offset: 0, effects: [] })) +}) + +test("group projection preserves effect order and isolates source and ACK rows", () => { + const maintenance = manifest.filter(effect => "parent" in effect && effect.parent === "flow_hour_facts").slice(0, 8) + const [group] = groupAtomicMaintenance(maintenance) + const effects = group.effects.map(effect => effect.kind === "partition.ensure" + ? { row: { parent: effect.parent, name: effect.name, from: effect.from, to: effect.to, namespace: "public", storage: {} } } + : {}) + const expected = group.effects.reduce((current, effect, index) => projectServerPollEffect(current, effect, effects[index]), context) + assert.deepEqual(projectAtomicMaintenance(context, maintenance, group, { effects }), expected) + assert.deepEqual(context.partitions, []) + assert(effects.every(ack => !ack.row || Object.keys(ack.row.storage).length === 0)) + const incomplete = effects.map((ack, index) => index === effects.length - 2 ? {} : ack) + assert.throws(() => projectAtomicMaintenance(context, maintenance, group, { effects: incomplete })) + assert.deepEqual(context.partitions, []) +}) diff --git a/backend/src/services/server-poll-maintenance-groups.ts b/backend/src/services/server-poll-maintenance-groups.ts new file mode 100644 index 00000000..cbab1f59 --- /dev/null +++ b/backend/src/services/server-poll-maintenance-groups.ts @@ -0,0 +1,103 @@ +/** Fixed explicit v4 maintenance layout and SQL construction. No I/O or authority issuance. */ +import { PARTITION_SPECS } from "../db/partitions.js" +import { pollProblem, projectServerPollEffects, type ServerPollEffect, type ServerPollContext, type ServerPollAck } from "./server-poll-execution-model.js" +import { isDeepStrictEqual } from "node:util" + +export const ATOMIC_MAINTENANCE_GROUP_SIZE = 8 as const +type PartitionEffect = Extract +export interface AtomicMaintenanceGroup { kind: "partition.atomic"; offset: number; effects: PartitionEffect[] } +export interface PublicationLayout { logicalMutationCost: number; journalMaxStages: number; maintenancePublications: number } +export interface AtomicMaintenanceAck { effects: ServerPollAck[] } +const factStorage = { fillfactor: "70", autovacuum_vacuum_scale_factor: "0.05", autovacuum_vacuum_cost_limit: "2000" } +const invalid = (): never => pollProblem("INVALID_INPUT", "Изменён фиксированный manifest обслуживания poll") +const record = (value: unknown): value is Record => !!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype +const keys = (value: Record, expected: string[]) => Object.keys(value).sort().join(",") === [...expected].sort().join(",") +const iso = (value: unknown): value is string => typeof value === "string" && Number.isFinite(Date.parse(value)) && new Date(value).toISOString() === value + +function checkedEffect(value: unknown): PartitionEffect { + if (!record(value) || typeof value.parent !== "string" || typeof value.name !== "string") return invalid() + const spec = PARTITION_SPECS.find(spec => spec.parent === value.parent) + if (!spec || !new RegExp("^" + spec.parent + "_" + spec.kind[0] + "_" + (spec.kind === "month" ? "\\d{6}" : "\\d{8}") + "$").test(value.name)) return invalid() + if (value.kind === "partition.drop") { + if (!keys(value, ["kind", "parent", "name"])) return invalid() + } else if (value.kind === "partition.ensure") { + if (!keys(value, ["kind", "parent", "name", "from", "to"]) || !iso(value.from) || !iso(value.to)) return invalid() + const from = new Date(value.from), to = new Date(from) + if (!value.from.endsWith("T00:00:00.000Z") || spec.kind === "month" && from.getUTCDate() !== 1 || spec.kind === "week" && from.getUTCDay() !== 1) return invalid() + if (spec.kind === "month") to.setUTCMonth(to.getUTCMonth() + 1) + else to.setUTCDate(to.getUTCDate() + (spec.kind === "week" ? 7 : 1)) + const stamp = from.toISOString().slice(0, spec.kind === "month" ? 7 : 10).replaceAll("-", "") + if (value.to !== to.toISOString() || value.name !== spec.parent + "_" + spec.kind[0] + "_" + stamp) return invalid() + } else if (value.kind === "partition.storage") { + if (!keys(value, ["kind", "parent", "name", "settings"]) || !["flow_hour_facts", "flow_daily_facts"].includes(value.parent) + || !record(value.settings) || !keys(value.settings, Object.keys(factStorage)) + || Object.entries(factStorage).some(([key, expected]) => value.settings && (value.settings as Record)[key] !== expected)) return invalid() + } else return invalid() + return structuredClone(value) as PartitionEffect +} + +/** Validates and copies the complete original ordered manifest. A public caller + * cannot provide SQL, parent/schema, a different group size or non-DB effects. + * Original plan HMAC and fresh full-source binding remain separate requirements. */ +export function groupAtomicMaintenance(maintenance: readonly ServerPollEffect[]): AtomicMaintenanceGroup[] { + if (!Array.isArray(maintenance) || maintenance.length > 2000 || Buffer.byteLength(JSON.stringify(maintenance)) > 524288) return invalid() + const effects = Array.from(maintenance, checkedEffect), groups: AtomicMaintenanceGroup[] = [] + for (let offset = 0; offset < effects.length; offset += ATOMIC_MAINTENANCE_GROUP_SIZE) { + groups.push({ kind: "partition.atomic", offset, effects: effects.slice(offset, offset + ATOMIC_MAINTENANCE_GROUP_SIZE) }) + } + return groups +} + +/** Strict signed-version distinction. A smaller journal never grants a smaller + * logical mutation admission; legacy versions retain individual publications. */ +export function publicationLayout(plan: { version: number; maintenance: readonly ServerPollEffect[]; mutationCost: number; maintenanceGroupSize?: number }): PublicationLayout { + if (!Array.isArray(plan.maintenance) || plan.maintenance.length > 2000 || plan.mutationCost !== plan.maintenance.length + 8) return invalid() + let maintenancePublications: number + if (plan.version === 4) { + if (plan.maintenanceGroupSize !== ATOMIC_MAINTENANCE_GROUP_SIZE) return invalid() + maintenancePublications = groupAtomicMaintenance(plan.maintenance).length + } else { + if (![1, 2, 3].includes(plan.version) || Object.hasOwn(plan, "maintenanceGroupSize")) return invalid() + maintenancePublications = plan.maintenance.length + } + return { logicalMutationCost: plan.mutationCost, journalMaxStages: maintenancePublications + 8, maintenancePublications } +} + +/** Immutable journal/history effects keep original manifest semantics for old + * versions and bind every member plus its original offset for explicit v4. */ +export function serverPollMaintenancePublications(plan: { version: number; maintenance: readonly ServerPollEffect[]; mutationCost: number; maintenanceGroupSize?: number }): (ServerPollEffect | AtomicMaintenanceGroup)[] { + publicationLayout(plan) + return plan.version === 4 ? groupAtomicMaintenance(plan.maintenance) : structuredClone([...plan.maintenance]) +} + +/** SQL construction only, never dispatch. The group must equal its exact slice + * of the full signed manifest; callers still own a live writer lease, original + * authority and full source verification before COMMIT. No transaction control + * is generated, no intermediate success is published. */ +export function compileAtomicMaintenanceGroup(maintenance: readonly ServerPollEffect[], group: unknown): string { + if (!record(group) || !keys(group, ["kind", "offset", "effects"]) || group.kind !== "partition.atomic" + || !Number.isSafeInteger(group.offset) || Number(group.offset) < 0 || Number(group.offset) % ATOMIC_MAINTENANCE_GROUP_SIZE !== 0) return invalid() + const expected = groupAtomicMaintenance(maintenance)[Number(group.offset) / ATOMIC_MAINTENANCE_GROUP_SIZE] + if (!expected || !isDeepStrictEqual(expected, group)) return invalid() + const sql = expected.effects.map(effect => { + const table = '"public"."' + effect.name + '"' + if (effect.kind === "partition.drop") return "DROP TABLE IF EXISTS " + table + ";" + if (effect.kind === "partition.ensure") return `CREATE TABLE IF NOT EXISTS ${table} PARTITION OF "public"."${effect.parent}" FOR VALUES FROM ('${effect.from}') TO ('${effect.to}');` + return `ALTER TABLE ${table} SET (fillfactor=70,autovacuum_vacuum_scale_factor=0.05,autovacuum_vacuum_cost_limit=2000);` + }) + return "DO $poll_atomic_maintenance$ BEGIN " + sql.join(" ") + " END $poll_atomic_maintenance$;" +} + +/** Observed defaults are admitted only by the original exact projection, one + * member at a time. The resulting full context still needs an authenticated + * same-transaction read before COMMIT and an independent read after COMMIT. */ +export function projectAtomicMaintenance(before: ServerPollContext, manifest: readonly ServerPollEffect[], group: AtomicMaintenanceGroup, ack: AtomicMaintenanceAck): ServerPollContext { + compileAtomicMaintenanceGroup(manifest, group) + if (!record(ack) || !keys(ack, ["effects"]) || !Array.isArray(ack.effects) || ack.effects.length !== group.effects.length) return invalid() + const effects = group.effects.map((effect, index) => { + const value = ack.effects[index] + if (!record(value) || Object.keys(value).some(key => key !== "row")) return invalid() + return { effect, ack: value } + }) + return projectServerPollEffects(before, effects) +} diff --git a/backend/src/services/server-poll-maintenance-planner.test.ts b/backend/src/services/server-poll-maintenance-planner.test.ts new file mode 100644 index 00000000..18937420 --- /dev/null +++ b/backend/src/services/server-poll-maintenance-planner.test.ts @@ -0,0 +1,95 @@ +/** Inert unit fixtures only: not evidence of real startup, PG, runtime or speed. */ +import assert from "node:assert/strict" +import { test } from "node:test" +import { planServerPollMaintenance, planServerPollMaintenanceV2, projectServerPollEffect, type ServerPollContext, type ServerPollEffect, type PollRow } from "./server-poll-execution-model.js" + +const around = "2026-10-29T12:00:00.000Z" +const cold = (): ServerPollContext => ({ servers: [], settings: { traffic: null, uptime: null, internetPath: null }, partitions: [], snapshots: [], protections: [] }) +function apply(context: ServerPollContext, effects: ServerPollEffect[]) { + let current = structuredClone(context) + for (const effect of effects) { + const row = effect.kind === "partition.ensure" ? { namespace: "public", parent: effect.parent, name: effect.name, from: effect.from, to: effect.to, storage: {}, opaqueUnitField: "kept" } : undefined + current = projectServerPollEffect(current, effect, { row }) + } + return current +} +function catalogue(context: ServerPollContext) { return [...context.partitions].sort((a, b) => String(a.name).localeCompare(String(b.name))) } +test("snapshot JSONB object key order does not change values or hide field/type/array drift", () => { + const before = cold(), effect: ServerPollEffect = { kind: "snapshot.insert", snapshot: { + serverId: 1, polledAt: "2026-10-30T00:00:00.000Z", status: "online", + rawIpAddresses: [{ ".id": "*b", interface: "ether1", address: "192.0.2.1/24" }], + rawInterfaces: [{ name: "ether1", flags: { running: true, disabled: false } }], + } } + const row: PollRow = { id: 1, ...effect.snapshot, + rawIpAddresses: [{ ".id": "*b", address: "192.0.2.1/24", interface: "ether1" }], + rawInterfaces: [{ flags: { disabled: false, running: true }, name: "ether1" }], + } + assert.deepEqual(projectServerPollEffect(before, effect, { row }).snapshots, [row]) + for (const mutate of [ + (row: PollRow) => { row.rawIpAddresses = [{ ".id": "*b", address: "192.0.2.2/24", interface: "ether1" }] }, + (row: PollRow) => { row.rawInterfaces = [{ flags: { disabled: false, running: true, opaque: "changed" }, name: "ether1" }] }, + (row: PollRow) => { row.serverId = 2 }, + (row: PollRow) => { row.rawInterfaces = [{ flags: { disabled: false, running: "true" }, name: "ether1" }] }, + (row: PollRow) => { row.rawIpAddresses = [] }, + ]) { + const changed = structuredClone(row); mutate(changed) + assert.throws(() => projectServerPollEffect(before, effect, { row: changed })) + } + assert.throws(() => projectServerPollEffect(before, { ...effect, snapshot: { ...effect.snapshot, rawInterfaces: [{ name: "a" }, { name: "b" }] } }, + { row: { ...row, rawInterfaces: [{ name: "b" }, { name: "a" }] } })) + assert.deepEqual(before.snapshots, []) +}) +function warm() { const source = cold(); return apply(source, planServerPollMaintenance(source, around)) } + +test("cold V2 retains every actual legacy CREATE/ALTER and does not mutate input", () => { + const source = cold(), before = structuredClone(source) + assert.deepEqual(planServerPollMaintenanceV2(source, around), planServerPollMaintenance(source, around)) + assert.deepEqual(source, before) +}) +test("complete warm catalogue retains first-partial-window DROP/CREATE, while eliding only exact live noops", () => { + const source = warm(), old = planServerPollMaintenance(source, around), next = planServerPollMaintenanceV2(source, around) + assert(old.some(e => e.kind === "partition.ensure")); assert(next.length < old.length) + for (const drop of old.filter(e => e.kind === "partition.drop")) { + assert(next.some(e => e.kind === "partition.drop" && e.name === drop.name)) + for (const recreation of old.filter(e => e.kind !== "partition.drop" && "name" in e && e.name === drop.name)) assert(next.includes(recreation) || next.some(e => JSON.stringify(e) === JSON.stringify(recreation))) + } + assert.deepEqual(catalogue(apply(source, next)), catalogue(apply(source, old))) +}) +test("missing fact leaf retains CREATE and ALTER even when predecessor's storage matched", () => { + const source = warm(), fact = source.partitions.find(r => r.parent === "flow_hour_facts")! + source.partitions = source.partitions.filter(r => r.name !== fact.name) + const effects = planServerPollMaintenanceV2(source, around) + assert(effects.some(e => e.kind === "partition.ensure" && e.name === fact.name)) + assert(effects.some(e => e.kind === "partition.storage" && e.name === fact.name)) + assert.deepEqual(catalogue(apply(source, effects)), catalogue(apply(source, planServerPollMaintenance(source, around)))) +}) +test("wrong bound and wrong parent refuse even when same name is scheduled for drop/recreate", () => { + for (const field of ["from", "to", "parent", "namespace"] as const) { + const source = warm(), target = source.partitions.find(r => r.parent === "flow_hour_facts")! + target[field] = "wrong" + assert.throws(() => planServerPollMaintenanceV2(source, around), { code: "STATE_UNAVAILABLE" }) + } +}) +test("changed and missing required storage preserve ALTER; exact extra keys survive elision", () => { + for (const mode of ["changed", "missing", "extra"] as const) { + const source = warm(), legacy = planServerPollMaintenance(source, around) + const drops = new Set(legacy.filter(e => e.kind === "partition.drop").map(e => e.name)) + const target = source.partitions.find(r => r.parent === "flow_hour_facts" && !drops.has(String(r.name)))! + assert(target) + const storage = target.storage as Record + if (mode === "changed") storage.fillfactor = "80" + if (mode === "missing") delete storage.fillfactor + if (mode === "extra") storage.autovacuum_enabled = "false" + const before = structuredClone(source), next = planServerPollMaintenanceV2(source, around) + assert.equal(next.some(e => e.kind === "partition.storage" && e.name === target.name), mode !== "extra") + assert.deepEqual(source, before) + assert.deepEqual(catalogue(apply(source, next)), catalogue(apply(source, planServerPollMaintenance(source, around)))) + } +}) +test("arbitrary unrelated partitions and opaque storage/catalogue fields survive unchanged", () => { + const source = warm(); source.partitions.push({ namespace: "public", parent: "server_snapshots", name: "server_snapshots_custom", from: null, to: null, storage: { custom: "opaque" }, catalogue: { exact: 1 } }) + const legacy = planServerPollMaintenance(source, around) + const before = structuredClone(legacy), next = planServerPollMaintenanceV2(source, around) + assert.deepEqual(planServerPollMaintenance(source, around), before, "V1 meaning must remain byte-for-byte stable") + assert.deepEqual(catalogue(apply(source, next)), catalogue(apply(source, legacy))) +}) diff --git a/backend/src/services/server-poll-memory.owned.test.ts b/backend/src/services/server-poll-memory.owned.test.ts new file mode 100644 index 00000000..635685cd --- /dev/null +++ b/backend/src/services/server-poll-memory.owned.test.ts @@ -0,0 +1,117 @@ +/** Compiled-only candidate sizing: one genuine public MCP poll. This is not a + * whole-installation 2GB acceptance and does not change any production budget. */ +import assert from "node:assert/strict" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { getHeapStatistics } from "node:v8" +import { performance } from "node:perf_hooks" +import { existsSync } from "node:fs" + +assert.equal(process.env.MMAPP_OWN_POLL_MEMORY, "true", "MMAPP_OWN_POLL_MEMORY is required before DB imports") +assert.equal(process.env.NODE_ENV, "test") +const endpoint = new URL(process.env.DATABASE_URL ?? "") +assert(["postgres:", "postgresql:"].includes(endpoint.protocol)) +assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.username, "mcp_test"); assert(endpoint.password) +assert.equal(endpoint.pathname, "/postgres"); assert.equal(endpoint.search, ""); assert.equal(endpoint.hash, "") +assert(Number.isInteger(Number(endpoint.port)) && Number(endpoint.port) > 0 && Number(endpoint.port) <= 65535) +assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) +assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") +assert.match(basename(dirname(resolve(process.env.MCP_STATE_DIR))), /^mm-mcp-test-[A-Za-z0-9-]+$/) +assert.equal(resolve(process.cwd()), dirname(resolve(process.env.MCP_STATE_DIR)), "Use the private owned working directory so dotenv cannot read backend/.env") +assert.equal(basename(dirname(dirname(new URL(import.meta.url).pathname))), "dist-mcp-memory", "Run the emitted compiled entry, never source via tsx") +assert(!process.execArgv.some(argument => /tsx|--import|--require|--loader|^-r$/.test(argument)), "No TS loader or preload may enter the measured child") +assert(!/tsx|--import|--require|--loader|(?:^|\s)-r(?:\s|$)/.test(process.env.NODE_OPTIONS ?? "")) +assert(process.execArgv.includes("--max-old-space-size=192"), "Explicit --max-old-space-size=192 is required before DB imports") +assert(process.execArgv.includes("--max-semi-space-size=16"), "Explicit --max-semi-space-size=16 is required before DB imports") +assert.equal(process.execArgv.filter(argument => /^--max[-_]old[-_]space[-_]size/.test(argument)).length, 1) +assert.equal(process.execArgv.filter(argument => /^--max[-_]semi[-_]space[-_]size/.test(argument)).length, 1) +assert(!/--max[-_](?:old|semi)[-_]space[-_]size/.test(process.env.NODE_OPTIONS ?? ""), "Sizing flags must be explicit and independently inspectable") +const heapLimitBytes = getHeapStatistics().heap_size_limit +const candidateBackendRssBudgetBytes = 512 * 1024 * 1024 +assert(heapLimitBytes >= 192 * 1024 * 1024 && heapLimitBytes <= 256 * 1024 * 1024, "Observed V8 heap must match the explicit candidate cap") +assert.notEqual(process.env.MMAPP_OWN_POLL_EXPIRY_DIAGNOSTICS, "true", "Memory acceptance uses the unwrapped production service") +process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH = "true" +process.env.MMAPP_OWN_SQL_DB = "postgres" + +const started = performance.now(), before = process.memoryUsage() +const costDiagnostic = process.env.MMAPP_OWN_POLL_COST_DIAGNOSTICS === "true" +const cpuDiagnostic = process.execArgv.some(argument => argument === "--cpu-prof" || argument.startsWith("--cpu-prof=")) +const diagnosticOnly = costDiagnostic || cpuDiagnostic +assert([undefined,"false","true"].includes(process.env.MMAPP_OWN_POLL_BOOTSTRAP),"Explicit owned bootstrap selection required") +const bootstrap = process.env.MMAPP_OWN_POLL_BOOTSTRAP === "true" +let bootstrapElapsedMs: number | undefined +const cpuBefore = process.cpuUsage() +let costs: { snapshot(): unknown; restore(): void } | undefined +let costTimer: ReturnType | undefined +const peak = { ...before }, maximumSamples = 10000, intervalMs = 250 +let samples = 0 +const sample = () => { + if (samples >= maximumSamples) return + samples++ + const current = process.memoryUsage() + for (const key of Object.keys(peak) as (keyof NodeJS.MemoryUsage)[]) peak[key] = Math.max(peak[key], current[key]) +} +sample() +const sampler = setInterval(sample, intervalMs); sampler.unref() +let closePool: (() => Promise) | undefined, closeApp: (() => Promise) | undefined +try { + if (costDiagnostic) { + const { installServerPollCostDiagnostics } = await import("./server-poll-cost-diagnostics.js") + costs = installServerPollCostDiagnostics() + console.log("Owned poll COST DIAGNOSTIC ONLY: driver observers installed; not unwrapped acceptance") + costTimer = setInterval(() => console.log("Owned poll COST DIAGNOSTIC ONLY progress:", JSON.stringify({ elapsedMs: performance.now() - started, cpu: process.cpuUsage(cpuBefore), sql: costs?.snapshot() })), 15000) + costTimer.unref() + } + // All configured production imports occur after owned endpoint/state/runtime + // guards. The normal app is assembled without scheduler/GeoIP/listener jobs. + const database = await import("../db/index.js"); closePool = database.closePool + const { applySqlMigrations } = await import("../db/migrate.js") + const { MIGRATION_MANIFEST } = await import("../db/migration-manifest.js") + const { buildApp } = await import("../index.js") + const { acceptServerPollPublicRuntimeSql } = await import("../modules/mcp/server-poll-http.sql-helper.js") + if(bootstrap){ + const sqlite=resolve(process.env.DATABASE_PATH??"") + assert.equal(dirname(sqlite),process.cwd());assert.equal(existsSync(sqlite),false,"Bootstrap may not import a SQLite source") + const { initDatabase }=await import("../db/bootstrap.js") + const began=performance.now();await initDatabase();bootstrapElapsedMs=performance.now()-began + assert.equal(existsSync(sqlite),false) + }else await applySqlMigrations(database.pool) + assert.equal((await database.pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length) + assert.equal((await database.pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions")).rows[0].n, 0) + const app = await buildApp({ logger: false, startScheduler: false }); closeApp = () => app.close() + await app.ready(); sample() + const appReady = process.memoryUsage() + const runtime = await acceptServerPollPublicRuntimeSql() + assert("actualDefaultPublicRegistry" in runtime,"Sizing requires the positive full poll result") + assert.equal(runtime.actualDefaultPublicRegistry, true); assert.equal(runtime.actualNativeGets, 4) + assert.equal(runtime.dispatchesAfterConsumedAdmission, 4) + assert.equal(runtime.persistedReadAndVerifyNoMutation, true); assert.equal(runtime.idempotentApplyNoNewNativeOrCharge, true) + sample(); const after = process.memoryUsage() + assert(samples < maximumSamples, "The bounded sampler must cover the entire measured workload") + console.log("Owned compiled poll memory:", JSON.stringify({ compiledProductionApp: true, tsxLoaded: false, + nodeVersion: process.version, platform: process.platform, arch: process.arch, + execArgv: process.execArgv, heapLimitBytes, heapLimitMiB: heapLimitBytes / 1024 / 1024, + catalogueInitialization:bootstrap?"actual-initDatabase":"migrations-only",bootstrapElapsedMs, + diagnosticOnly, cpuProfilerEnabled: cpuDiagnostic, sqlCosts: costs?.snapshot(), cpu: process.cpuUsage(cpuBefore), + before, appReady, after, sampledPeak: peak, samples, sampleIntervalMs: intervalMs, maximumSamples, + maxRssKiB: process.resourceUsage().maxRSS, candidateBackendRssBudgetBytes, elapsedMs: performance.now() - started, + currentMigrations: MIGRATION_MANIFEST.length, actualNativeGets: runtime.actualNativeGets, + completedPublications: runtime.completedPublications, journalRows: runtime.journalRows, + schedulerStarted: false, productionDatabaseTouched: false, wholeInstallation2GbAcceptance: false })) + assert(process.resourceUsage().maxRSS * 1024 <= candidateBackendRssBudgetBytes, "Observed backend process RSS exceeds the fixed 512MiB candidate budget") +} catch (error) { + sample() + console.log("Owned compiled poll memory failure:", JSON.stringify({ + nodeVersion: process.version, platform: process.platform, arch: process.arch, + compiledProductionApp: true, execArgv: process.execArgv, heapLimitBytes, + catalogueInitialization:bootstrap?"actual-initDatabase":"migrations-only",bootstrapElapsedMs, + sampledPeak: peak, samples, maxRssKiB: process.resourceUsage().maxRSS, + candidateBackendRssBudgetBytes, elapsedMs: performance.now() - started, + diagnosticOnly, cpuProfilerEnabled: cpuDiagnostic, sqlCosts: costs?.snapshot(), cpu: process.cpuUsage(cpuBefore), + workloadAccepted: false, wholeInstallation2GbAcceptance: false, + })) + throw error +} finally { + clearInterval(sampler) + if (costTimer) clearInterval(costTimer) + try { await closeApp?.() } finally { try { await closePool?.() } finally { costs?.restore() } } +} diff --git a/backend/src/services/server-poll-operation-retirement.owned.test.ts b/backend/src/services/server-poll-operation-retirement.owned.test.ts new file mode 100644 index 00000000..ab1d3794 --- /dev/null +++ b/backend/src/services/server-poll-operation-retirement.owned.test.ts @@ -0,0 +1,372 @@ +/** Owned-only historical fixture. Synthetically seeds a historical timeline + * before signing. The real V1 compactor, V2 retirement, read and GC run with + * production crypto and all database guards enabled. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +assert.equal(process.env.MMAPP_OWN_MCP_OPERATION_ORIGIN,"true") +const { pool,closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +const { createMcpConnection,authenticateMcpToken } = await import("../modules/mcp/store.js") +const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") +const { invokeMcpTool } = await import("../modules/mcp/runtime.js") +const { encryptSecret,privateSecretFingerprint,fingerprint,redact } = await import("../modules/mcp/security.js") +const { encodeServerPollJournalAnchor } = await import("./server-poll-journal-anchor.js") +const { compactCompletedServerPollStorage,compactCompletedServerPollStorageForTests,SERVER_POLL_ARCHIVE_READ_SQL } = await import("./server-poll-terminal-compaction.js") +const { openServerPollTerminalArchive } = await import("./server-poll-terminal-archive.js") +const { retireCompletedServerPollOperation, retireCompletedServerPollOperations } = await import("./server-poll-operation-retirement.js") +const { PARTITION_WRITER_ADVISORY_KEY } = await import("./partition-write-transaction.js") +const { collectExpiredServerPollStorage,collectExpiredServerPollStorageForTests } = await import("./server-poll-storage-retention.js") +const { connectProbeStorage } = await import("./server-probe-storage-connection.js") +const { createServerPollStorage,PollStorageRefused,PollStorageUnknown } = await import("./server-poll-storage.js") +const { readServerPollCapacity } = await import("./server-poll-capacity.js") +const { tickMcpWorkers } = await import("../modules/mcp/workers.js") +const { Client } = await import("pg") +const proof=(domain:string,value:unknown)=>privateSecretFingerprint(fingerprint(value),domain) +const encrypt=(value:unknown)=>encryptSecret(JSON.stringify(value)) + +function loseMaintenanceCommit(at:number,afterFirst?:()=>Promise){ + const transactions:{commands:string[];release?:boolean}[]=[] + let commits=0 + return {transactions,deps:{now:Date.now,connect:async(options:Parameters[0])=>{ + const physical=await connectProbeStorage(options),trace:{commands:string[];release?:boolean}={commands:[]} + transactions.push(trace) + return {async query(...args:Parameters){ + const [text]=args + trace.commands.push(text) + const result=await physical.query(...args) + if(text==="COMMIT"){ + commits++ + if(commits===1)await afterFirst?.() + if(commits===at)throw Error("owned POLL maintenance COMMIT ACK lost") + } + return result + },release(unsafe?:boolean){trace.release=unsafe;physical.release(unsafe)}} + }}} +} +function assertMaintenanceOrder(fault:ReturnType,phaseCount:number){ + assert.deepEqual(fault.transactions.map(tx=>tx.release),[true],"One physical owner, destroyed once on unknown COMMIT") + const [{commands}]=fault.transactions + assert.equal(commands.includes("ROLLBACK"),false) + assert.equal(commands.some(text=>text.includes("pg_advisory_unlock_shared")),false) + assert.equal(commands.filter(text=>text==="BEGIN").length,phaseCount) + assert.equal(commands.filter(text=>text==="COMMIT").length,phaseCount) + let previousCommit=-1 + for(let phase=0;phase0&&writer>}) { + const base=Date.now()-days*86400000, stamp=(offset:number)=>new Date(base+offset).toISOString() + const ownerId=existing?.ownerId??randomUUID(),executionId=randomUUID(),receiptId=randomUUID(),planId=randomUUID() + const actor={id:ownerId,email:"owned-historical-poll@example.test",name:"Owned historical poll",apps:["mm"], + permissions:["mm:servers:write","mm:servers:read"],issuedAt:Date.now()-1000,expiresAt:Date.now()+300000} + const current=existing?.current??await authenticateMcpToken((await createMcpConnection(mcpConnectionCreateSchema.parse({name:actor.name,mode:"approval", + permissions:actor.permissions,expiresAt:new Date(actor.expiresAt).toISOString(), + scope:{serverIds:[serverId],operationIds:["servers.poll","operations.global.reconcile"],allowGlobal:true}, + limits:{callsPerMinute:100,mutationsPerHour:10}}),actor)).token) + const connectionId=current.connectionId,credentialId=randomUUID() + const core={version:1 as const,requiresHuman:true as const,scope:"global" as const,request:{serverId,receiptId},around:stamp(0), + beforeProof:"a".repeat(64),authorityProof:"b".repeat(64),maintenance:[],affectedServerIds:[serverId],mutationCost:8} + const plan={...core,bindingProof:await proof("server-poll-production-v1",core)} + const context={identity:{kind:"mcp" as const,ownerId,executionId,connectionId,credentialId,operationName:"servers.poll", + issuedAt:base,expiresAt:base+180000},plan} + const contextProof=await proof("server-poll-journal-v1:context",context) + const anchor=await encodeServerPollJournalAnchor(context,undefined,{proof:(domain,value)=>proof("server-poll-journal-v1:"+domain,value)}) + const binding={version:1,operation:"servers.poll",principal:{connectionId,credentialId,credentialExpiresAt:stamp(240000)}, + ownerId,issuedAt:base,expiresAt:base+180000,plan} + const input={serverId,receiptId,inputDigest:await proof("server-poll-mcp-binding-v1",binding),bindingCiphertext:await encrypt(binding)} + const step={kind:"service",service:"servers.poll",input} + const evidence={target:{scope:"global"},objects:[{kind:"server",id:String(serverId),serverId}],snapshots:[],steps:[step], + before:{proof:plan.beforeProof},mutationCost:8,requiresHuman:true,verificationScope:"configuration",validUntil:stamp(180000), + warnings:["Полный опрос читает устройство и изменяет общие снимки и разделы истории; требуется подтверждение человеком. Автоматический повтор запрещён."]} + const snapshot={id:42,serverId,polledAt:stamp(1000),status:"online" as const,latencyMs:0,rosVersion:null,boardName:null, + uptime:null,cpuLoad:null,freeMemory:null,totalMemory:null,identityName:null} + const result={operationId:executionId,receiptId,nativeReads:4,confirmedPublications:2,completedPublications:2,complete:true, + maintenanceComplete:true,configurationVerified:true,automaticReplay:false,snapshot} + const receipt={receiptId,nativeReads:4,confirmedPublications:2,completedPublications:2,complete:true, + maintenanceComplete:true,configurationVerified:true,afterProof:"f".repeat(64),observation:"online",snapshot} + const db=await pool.connect() + try{ + await db.query("BEGIN") + await db.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by,approved_at,created_at) + VALUES($1,$2,'servers.poll',1,1,'owned-historical',$3,$4,'completed',$5,$6,$7,$8)`, + [planId,connectionId,{sealed:await encrypt({serverId})},{sealed:await encrypt(evidence)},stamp(190000),ownerId,stamp(50),stamp(25)]) + await db.query(`INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,verification,result,created_at,finished_at) + VALUES($1,$2,$3,$4,'applied','passed',$5,$6,$7)`, + [executionId,planId,connectionId,"owned-historical-"+executionId,{confirmedWrites:1,verificationScope:"configuration"},stamp(100),stamp(1600)]) + await db.query(`INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state,result,created_at,finished_at) + VALUES($1,0,'verified',$2,$3,$4,$5)`,[executionId,redact(step),result,stamp(110),stamp(1500)]) + // Historical journal admission is impossible via live writes by design. + // Only this private fixture transaction seeds the signed, pre-existing rows. + await db.query("SET LOCAL session_replication_role='replica'") + await db.query(`INSERT INTO server_poll_storage_admissions(receipt_id,identity_kind,execution_id,connection_id,owner_id,binding_proof, + context_proof,authority_issued_at,authority_expires_at,max_stages,claim_attempted_at,created_at,retention_days,gc_not_before,used_rows,used_bytes) + VALUES($1,'mcp',$2,$3,$4,$5,$6,$7,$8,8,$9,$10,365,$11,14,4000)`, + [receiptId,executionId,connectionId,ownerId,plan.bindingProof,contextProof,stamp(0),stamp(180000),stamp(300),stamp(200),stamp(180000+365*86400000+60000)]) + await db.query(`INSERT INTO server_poll_executions(receipt_id,identity_kind,execution_id,binding_proof,context_ciphertext,context_proof,created_at) + VALUES($1,'mcp',$2,$3,$4,$5,$6)`,[receiptId,executionId,plan.bindingProof,await encryptSecret(anchor),contextProof,stamp(400)]) + const stages=["native.identity","native.resource","native.interfaces","native.addresses","publish.0","publish.1"] + for(const [index,stage] of stages.entries()){ + const beforeProof=index>4?"e".repeat(64):plan.beforeProof,effectProof="c".repeat(64) + const resultProof=index===stages.length-1?receipt.afterProof:"e".repeat(64),outcome="confirmed" + await db.query("INSERT INTO server_poll_intents VALUES($1,$2,$3,$4,$5,$6)", + [receiptId,stage,beforeProof,effectProof,await proof("server-poll-journal-v1:intent",{receiptId,stage,beforeProof,effectProof}),stamp(500+index*100)]) + await db.query("INSERT INTO server_poll_acks VALUES($1,$2,$3,$4,$5,$6)", + [receiptId,stage,resultProof,outcome,await proof("server-poll-journal-v1:ack",{receiptId,stage,resultProof,outcome}),stamp(550+index*100)]) + } + const receiptText=JSON.stringify(receipt) + await db.query("INSERT INTO server_poll_receipts VALUES($1,$2,$3,$4)", + [receiptId,await encryptSecret(receiptText),await proof("server-poll-journal-v1:receipt",receiptText),stamp(1400)]) + await db.query(`INSERT INTO server_poll_storage_usage(scope,admissions,reserved_rows,reserved_bytes) VALUES($1,1,4019,8421376) + ON CONFLICT(scope) DO UPDATE SET admissions=server_poll_storage_usage.admissions+1, + reserved_rows=server_poll_storage_usage.reserved_rows+4019,reserved_bytes=server_poll_storage_usage.reserved_bytes+8421376`,["owner:"+ownerId]) + await db.query("UPDATE server_poll_storage_usage SET admissions=admissions+1,reserved_rows=reserved_rows+4019,reserved_bytes=reserved_bytes+8421376 WHERE scope='global'") + await db.query("SET LOCAL session_replication_role='origin'") + assert.equal((await db.query("SELECT server_poll_storage_accounting_valid() AS valid")).rows[0].valid,true) + await db.query("COMMIT") + }catch(error){await db.query("ROLLBACK");throw error}finally{db.release()} + return {receiptId,executionId,planId,ownerId,connectionId,current} +} + +try{ + await applySqlMigrations(pool) + await pool.query("UPDATE mcp_settings SET enabled=true,requests_retention_days=365 WHERE id=1") + const {rows:[server]}=await pool.query("INSERT INTO servers(name,host,enabled) VALUES('owned historical poll','127.0.0.1',false) RETURNING id") + const retained=await historical(181,Number(server.id)), expired=await historical(366,Number(server.id)) + for(const table of ["server_poll_storage_admissions","server_poll_storage_usage","server_poll_archives","server_poll_retirement_claims"]){ + await pool.query(`CREATE TRIGGER owned_poll_worker_fence BEFORE INSERT OR UPDATE OR DELETE ON public.${table} + FOR EACH STATEMENT EXECUTE FUNCTION public.server_partition_writer_guard(); + ALTER TABLE public.${table} ENABLE ALWAYS TRIGGER owned_poll_worker_fence`) + } + for(const fixture of [expired,retained]){ + if(fixture===expired){ + const rival=await pool.connect() + let waiting:Promise|undefined + try{ + const {rows:[owner]}=await rival.query("SELECT pg_backend_pid() AS pid") + const fault=loseMaintenanceCommit(2,async()=>{ + waiting=rival.query("SELECT pg_advisory_lock($1::bigint)",[PARTITION_WRITER_ADVISORY_KEY]);void waiting.catch(()=>{}) + let queued=false + for(const end=Date.now()+2000;!queued&&Date.now()error instanceof PollStorageUnknown&&!error.automaticReplay) + assertMaintenanceOrder(fault,2) + await waiting + assert.equal((await rival.query("SELECT pg_advisory_unlock($1::bigint) AS released",[PARTITION_WRITER_ADVISORY_KEY])).rows[0].released,true) + }finally{rival.release(true)} + }else assert.equal((await compactCompletedServerPollStorage()).compacted,1) + const {rows:[v1]}=await pool.query(SERVER_POLL_ARCHIVE_READ_SQL,[fixture.receiptId]) + await openServerPollTerminalArchive(v1,Date.now()) + const before=(await pool.query("SELECT gc_not_before,authority_expires_at,retention_days,original_activity_at FROM server_poll_storage_admissions WHERE receipt_id=$1",[fixture.receiptId])).rows[0] + if(fixture===expired){ + const exclusive=await pool.connect() + let blocked:ReturnType|undefined + try{ + await exclusive.query("SELECT pg_advisory_lock($1::bigint)",[PARTITION_WRITER_ADVISORY_KEY]) + await exclusive.query("BEGIN") + await exclusive.query("SELECT pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0))") + assert.deepEqual(await retireCompletedServerPollOperations(),{scanned:0,retired:0,automaticReplay:false}) + blocked=retireCompletedServerPollOperation({receiptId:fixture.receiptId,oldArchiveProof:v1.archive_proof}) + void blocked.catch(()=>{}) + let waiting=false + for(const deadline=Date.now()+3000;!waiting&&Date.now(){})}finally{ + try{ + await exclusive.query("ROLLBACK") + assert.equal((await exclusive.query("SELECT pg_advisory_unlock($1::bigint) AS unlocked",[PARTITION_WRITER_ADVISORY_KEY])).rows[0].unlocked,true) + }finally{exclusive.release(true)} + } + } + } + await assert.rejects(retireCompletedServerPollOperation({receiptId:fixture.receiptId,oldArchiveProof:"0".repeat(64)})) + const hold=randomUUID() + await pool.query("INSERT INTO mcp_drift(id,operation_id,expected,observed) VALUES($1,$2,'{}','{}')",[hold,fixture.executionId]) + await assert.rejects(retireCompletedServerPollOperation({receiptId:fixture.receiptId,oldArchiveProof:v1.archive_proof})) + assert.equal((await pool.query(SERVER_POLL_ARCHIVE_READ_SQL,[fixture.receiptId])).rows[0].archive_proof,v1.archive_proof) + assert.equal((await pool.query("SELECT server_poll_storage_accounting_valid() AS valid")).rows[0].valid,true) + assert.equal((await pool.query("SELECT 1 FROM server_poll_retirement_claims WHERE receipt_id=$1",[fixture.receiptId])).rows.length,0) + await pool.query("DELETE FROM mcp_drift WHERE id=$1",[hold]) + if(fixture===expired){ + const rival=await pool.connect() + const query=Client.prototype.query + const tryExclusive=async()=>(await rival.query("SELECT pg_try_advisory_lock($1::bigint) AS acquired", + [PARTITION_WRITER_ADVISORY_KEY])).rows[0].acquired + const unlock=async()=>assert.equal((await rival.query("SELECT pg_advisory_unlock($1::bigint) AS released", + [PARTITION_WRITER_ADVISORY_KEY])).rows[0].released,true) + let commits=0,acquiredBetween: boolean|undefined + try{ + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + if((args[0] as {text?:string})?.text==="COMMIT"&&++commits===1){ + return Promise.resolve(Reflect.apply(query,this,args)).then(async result=>{ + acquiredBetween=await tryExclusive() + if(acquiredBetween)await unlock() + return result + }) + } + return Reflect.apply(query,this,args) + } as typeof Client.prototype.query + assert.equal((await retireCompletedServerPollOperation({receiptId:fixture.receiptId,oldArchiveProof:v1.archive_proof})).retired,true) + assert.equal(commits,2) + assert.equal(acquiredBetween,false,"Exclusive POLL must wait across the acknowledged retirement claim") + assert.equal(await tryExclusive(),true,"Retirement must release the physical writer fence after publication") + await unlock() + }finally{Client.prototype.query=query;rival.release(true)} + }else assert.equal((await retireCompletedServerPollOperation({receiptId:fixture.receiptId,oldArchiveProof:v1.archive_proof})).retired,true) + const {rows:[v2]}=await pool.query(SERVER_POLL_ARCHIVE_READ_SQL,[fixture.receiptId]) + assert.equal(v2.version,2);assert(v2.ciphertext_bytes(error as {code?:string}).code==="NOT_FOUND") + } + const expiredV1=await historical(367,Number(server.id)) + assert.equal((await compactCompletedServerPollStorage()).compacted,1) + assert.equal((await pool.query(SERVER_POLL_ARCHIVE_READ_SQL,[expiredV1.receiptId])).rows[0].version,1) + const gcFault=loseMaintenanceCommit(1) + await assert.rejects(collectExpiredServerPollStorageForTests(gcFault.deps),error=>error instanceof PollStorageUnknown&&!error.automaticReplay) + assertMaintenanceOrder(gcFault,1) + assert.equal((await collectExpiredServerPollStorage()).collected,0) + assert.equal((await pool.query("SELECT 1 FROM mcp_operation_replay_tombstones WHERE operation_id=$1",[expired.executionId])).rows.length,1) + assert.equal((await pool.query("SELECT 1 FROM mcp_operations WHERE id=$1",[expiredV1.executionId])).rows.length,1) + const {rows}=await pool.query("SELECT r.receipt_id,r.ciphertext_bytes,u.reserved_bytes,u.reserved_rows FROM server_poll_archives r CROSS JOIN server_poll_storage_usage u WHERE u.scope='global'") + assert.equal(rows.length,1);assert.equal(rows[0].receipt_id,retained.receiptId) + assert.equal(Number(rows[0].reserved_bytes),32768+rows[0].ciphertext_bytes);assert.equal(Number(rows[0].reserved_rows),2) + assert.equal((await pool.query("SELECT 1 FROM server_poll_retirement_claims WHERE receipt_id=$1",[expired.receiptId])).rows.length,0) + + const held=await historical(190,Number(server.id)), spent=await historical(189,Number(server.id)) + const ready=await historical(188,Number(server.id)) + // The first two are already compacted. The actual worker has to compact the + // next eligible archive, skip older held/spent V1 rows, then retire it. + assert.equal((await compactCompletedServerPollStorage()).compacted,1) + assert.equal((await compactCompletedServerPollStorage()).compacted,1) + await pool.query("INSERT INTO mcp_drift(id,operation_id,expected,observed) VALUES($1,$2,'{}','{}')",[randomUUID(),held.executionId]) + const {rows:[spentV1]}=await pool.query(SERVER_POLL_ARCHIVE_READ_SQL,[spent.receiptId]) + const originalQuery=Client.prototype.query + const admissionOrder:string[]=[] + let commits=0 + try{ + Client.prototype.query=function(this:InstanceType,...args:unknown[]){ + const text=(args[0] as {text?:string})?.text + if(text==="SELECT pg_advisory_xact_lock_shared($1::bigint)")admissionOrder.push("common") + if(text==="SELECT pg_catalog.set_config('lock_timeout',$1,true)")admissionOrder.push("restore timeout") + if(text==="SELECT pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0))")admissionOrder.push("storage") + if(text==="COMMIT" && ++commits===2) { + // No COMMIT ACK can reach the owner. Closing the real checked-out + // backend rolls back uncommitted work, leaving the earlier claim spent. + return Promise.reject(Error("owned poll retirement COMMIT transport loss")) + } + return Reflect.apply(originalQuery,this,args) + } as typeof Client.prototype.query + await assert.rejects(retireCompletedServerPollOperation({receiptId:spent.receiptId,oldArchiveProof:spentV1.archive_proof}), + (error:unknown)=>error instanceof PollStorageUnknown && error.automaticReplay===false) + }finally{Client.prototype.query=originalQuery} + assert.equal(commits,2) + assert.deepEqual(admissionOrder,["common","restore timeout","storage","common","restore timeout","storage"]) + assert.equal((await pool.query(SERVER_POLL_ARCHIVE_READ_SQL,[spent.receiptId])).rows[0].version,1) + assert.equal((await pool.query("SELECT 1 FROM server_poll_retirement_claims WHERE receipt_id=$1",[spent.receiptId])).rows.length,1) + const workerProjection=async()=>(await pool.query(`SELECT a.receipt_id,r.version,r.archive_proof,a.archive_bytes, + o.id AS live_operation,t.operation_id AS tombstone,c.claim_token + FROM server_poll_storage_admissions a LEFT JOIN server_poll_archives r USING(receipt_id) + LEFT JOIN mcp_operations o ON o.id=a.execution_id + LEFT JOIN mcp_operation_replay_tombstones t ON t.operation_id=a.execution_id + LEFT JOIN server_poll_retirement_claims c USING(receipt_id) + WHERE a.receipt_id=ANY($1::uuid[]) ORDER BY a.receipt_id`,[[held.receiptId,spent.receiptId,ready.receiptId]])).rows + const beforeWorker=await workerProjection() + await tickMcpWorkers() + const afterWorker=await workerProjection() + assert.deepEqual(afterWorker.filter(row=>row.receipt_id!==ready.receiptId),beforeWorker.filter(row=>row.receipt_id!==ready.receiptId)) + const retiredByWorker=afterWorker.find(row=>row.receipt_id===ready.receiptId) + assert.equal(retiredByWorker?.version,2);assert.equal(retiredByWorker?.live_operation,null) + assert.equal(retiredByWorker?.tombstone,ready.executionId);assert(retiredByWorker?.claim_token) + await tickMcpWorkers() + assert.deepEqual(await workerProjection(),afterWorker) + assert.equal((await pool.query("SELECT server_poll_storage_accounting_valid() AS valid")).rows[0].valid,true) + + const lostClaim=await historical(187,Number(server.id)),claimFault=loseMaintenanceCommit(1) + await assert.rejects(compactCompletedServerPollStorageForTests(claimFault.deps),error=>error instanceof PollStorageUnknown&&!error.automaticReplay) + assertMaintenanceOrder(claimFault,1) + const {rows:[claimState]}=await pool.query(`SELECT storage_state,compaction_claim IS NOT NULL AND compaction_claim_xid IS NOT NULL AS spent, + NOT EXISTS(SELECT 1 FROM server_poll_archives WHERE receipt_id=$1) AS unarchived, + server_poll_storage_accounting_valid() AS accounting FROM server_poll_storage_admissions WHERE receipt_id=$1`,[lostClaim.receiptId]) + assert.deepEqual(claimState,{storage_state:"full",spent:true,unarchived:true,accounting:true}) + assert.deepEqual(await compactCompletedServerPollStorage(),{scanned:0,compacted:0,skipped:0,automaticReplay:false}) + console.log("Owned PostgreSQL: guarded compaction and mixed V1/V2 GC, physical claim/publish/refund ACK losses, retained history, and actual worker skips held/spent rows") + + if (process.env.MMAPP_OWN_SKIP_POLL_CAPACITY === "true") { + console.log("Focused POLL retirement: 336-archive capacity scenario skipped") + } else { + const started = performance.now() + const first = await historical(2, Number(server.id)) + for (let index = 0; index < 336; index++) { + const source = index === 0 ? first : await historical(2, Number(server.id), first) + assert.equal((await compactCompletedServerPollStorage()).compacted, 1) + assert.equal((await pool.query("SELECT 1 FROM server_poll_archives WHERE receipt_id=$1 AND owner_id=$2", + [source.receiptId, first.ownerId])).rows.length, 1) + if ((index + 1) % 64 === 0) console.log("Owned PostgreSQL POLL capacity progress:", index + 1) + } + const usage = async () => (await pool.query(`SELECT admissions,compacted_admissions,archive_bytes,reserved_rows,reserved_bytes + FROM server_poll_storage_usage WHERE scope=$1`, ["owner:" + first.ownerId])).rows[0] + const before = await usage() + assert.equal(Number(before.compacted_admissions), 336) + assert.equal(Number(before.admissions), 0) + const capacity = await readServerPollCapacity(pool, first.ownerId, true) + assert(capacity.status === "observed" && capacity.owner.remaining >= 1) + const now = Date.now() + const scope = { receiptId: randomUUID(), executionId: randomUUID(), kind: "mcp" as const, ownerId: first.ownerId, + issuedAt: now - 1000, expiresAt: now + 300000, bindingProof: "a".repeat(64), contextProof: "b".repeat(64) } + const storage = createServerPollStorage({ scope, connectionId: first.connectionId, maxStages: 8, authorize: async () => {} }) + await storage.claim({ deadlineAt: now + 30000 }, async sql => { + await sql.query(`INSERT INTO server_poll_executions + (receipt_id,identity_kind,execution_id,binding_proof,context_ciphertext,context_proof) + VALUES($1,'mcp',$2,$3,$4,$5)`, [scope.receiptId, scope.executionId, scope.bindingProof, + await encryptSecret("owned capacity fixture"), scope.contextProof]) + }) + const after = await usage() + assert.equal(Number(after.admissions), 1) + assert.equal(after.compacted_admissions, before.compacted_admissions) + assert.equal(after.archive_bytes, before.archive_bytes) + assert.equal(Number(after.reserved_rows) - Number(before.reserved_rows), 4019) + assert.equal(Number(after.reserved_bytes) - Number(before.reserved_bytes), 8421376) + assert.equal((await pool.query("SELECT server_poll_storage_accounting_valid() AS valid")).rows[0].valid, true) + console.log("Owned PostgreSQL POLL capacity:", JSON.stringify({ oneOwnerArchives: 336, additionalStorageClaim: true, + historicalSyntheticSources: true, elapsedMs: Math.round(performance.now() - started) })) + } +}finally{await closePool()} diff --git a/backend/src/services/server-poll-operation-retirement.ts b/backend/src/services/server-poll-operation-retirement.ts new file mode 100644 index 00000000..dd8d6f9b --- /dev/null +++ b/backend/src/services/server-poll-operation-retirement.ts @@ -0,0 +1,177 @@ +/** Scheduled/internal maintenance only. No public HTTP or MCP registration. */ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { operationIdempotencyDigest } from "../modules/mcp/operation-replay-tombstones.js" +import { fingerprint } from "../modules/mcp/security.js" +import { assertServerPollArchiveAccounting } from "./server-poll-archive-accounting.js" +import { SERVER_POLL_ARCHIVE_READ_SQL } from "./server-poll-terminal-compaction.js" +import { openServerPollRetiredArchive, projectServerPollRetiredArchive, type LockedServerPollRetirementSource } from "./server-poll-retired-archive.js" +import { admitPollStorageWriter, createPollMaintenanceWriter, PollStorageRefused, runPollStorageTransaction, tryAdmitPollStorageMaintenanceWriter } from "./server-poll-storage.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" + +const request = z.object({ receiptId: z.string().uuid(), oldArchiveProof: z.string().regex(/^[a-f0-9]{64}$/) }).strict() +const requireThat: (value: unknown) => asserts value = value => { + if (!value) throw new PollStorageRefused() +} + +/** The storage owner already holds its global xact advisory lock. Do not wait + * for a connection writer or runner that might need the storage lock. */ +const ELIGIBLE = `FROM public.server_poll_storage_admissions a JOIN public.server_poll_archives r USING(receipt_id) + JOIN public.mcp_operations o ON o.id=a.execution_id AND o.connection_id=a.connection_id + JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + WHERE a.identity_kind='mcp' AND a.storage_state='compacted' + AND r.version=1 AND r.execution_id=a.execution_id AND r.connection_id=a.connection_id AND r.owner_id=a.owner_id + AND o.state='applied' AND o.verification='passed' AND o.error_code IS NULL AND isfinite(o.finished_at) + AND o.finished_at($1::timestamptz,$2::uuid)) + ORDER BY o.finished_at,a.receipt_id LIMIT 8` + +const deps = { connect:connectProbeStorage,now:Date.now } +type Target = z.infer + +async function prepare(sql: ProbeStorageSql,{receiptId,oldArchiveProof}:Target) { + const identity = (await sql.query(`SELECT a.connection_id,a.execution_id,a.owner_id FROM public.server_poll_storage_admissions a + JOIN public.server_poll_archives r USING(receipt_id) WHERE a.receipt_id=$1 AND a.storage_state='compacted' + AND a.identity_kind='mcp' AND r.version=1 AND r.archive_proof=$2`, [receiptId,oldArchiveProof])).rows + requireThat(identity.length===1) + const { connection_id: connectionId,execution_id: operationId,owner_id: ownerId } = identity[0] + requireThat((await sql.query("SELECT id FROM public.mcp_connections WHERE id=$1 AND owner_id=$2 FOR UPDATE NOWAIT", + [connectionId,ownerId])).rows.length===1) + for (const key of ["mcp-operation:"+operationId,"server-poll-execution-v1:mcp:"+operationId,"server-poll-receipt-v1:"+receiptId]) { + requireThat((await sql.query("SELECT pg_try_advisory_xact_lock(hashtextextended($1,0)) AS locked",[key])).rows[0]?.locked===true) + } + const rows = (await sql.query(SOURCE,[receiptId,oldArchiveProof])).rows + requireThat(rows.length===1 && rows[0].operation_id===operationId && rows[0].admission && rows[0].terminal) + const { admission,terminal,plan_id: planId,idempotency_key: key,observed_at: observedAt } = rows[0] + requireThat(typeof key==="string" && typeof connectionId==="string" && typeof planId==="string") + await assertServerPollArchiveAccounting(sql) + const oldArchive = (await sql.query(SERVER_POLL_ARCHIVE_READ_SQL,[receiptId])).rows + requireThat(oldArchive.length===1 && oldArchive[0].archive_proof===oldArchiveProof) + const archived = await projectServerPollRetiredArchive({ archive:oldArchive[0],admission,terminal } as LockedServerPollRetirementSource, + new Date(observedAt as string | Date).getTime()) + requireThat(archived) + const projection = await openServerPollRetiredArchive(archived) + requireThat(projection.metadata.receipt_id===receiptId && projection.metadata.execution_id===operationId + && projection.metadata.connection_id===connectionId && projection.metadata.owner_id===ownerId && projection.history.planId===planId) + return {connectionId,operationId,planId,key,archived,projection,previousBytes:oldArchive[0].ciphertext_bytes} +} + +async function retire(target:Target,deadlineAt:number) { + const writer=createPollMaintenanceWriter(deps,deadlineAt) + try{ + const result=await retireOnWriter(target,deadlineAt,writer) + await writer.finish(deadlineAt) + return result + }finally{writer.destroy()} +} + +async function retireOnWriter(target:Target,deadlineAt:number,writer:ReturnType) { + const {receiptId,oldArchiveProof}=target + const token=randomUUID() + await runPollStorageTransaction(writer.deps,{deadlineAt},async()=>{},async sql=>{ + // Check the live hold predicates, all locks and both archive proofs before + // spending the irreversible one-shot claim. + await prepare(sql,target) + const claim=await sql.query(`INSERT INTO public.server_poll_retirement_claims(receipt_id,old_archive_proof,claim_token) + VALUES($1,$2,$3) ON CONFLICT DO NOTHING RETURNING receipt_id`,[receiptId,oldArchiveProof,token]) + requireThat(claim.rows.length===1&&claim.rows[0].receipt_id===receiptId) + },async sql=>{ + await admitPollStorageWriter(sql,deadlineAt) + // Extend the shared writer fence from this transaction through publication. + requireThat(await writer.tryAcquire(sql)) + }) + // No destructive transaction after an unacknowledged claim. A claim is + // never cleared, even when a later checkout/hold/COMMIT is inconclusive. + return runPollStorageTransaction(writer.deps,{deadlineAt},async()=>{},async sql=>{ + const claimed=await sql.query(`SELECT receipt_id FROM public.server_poll_retirement_claims + WHERE receipt_id=$1 AND old_archive_proof=$2 AND claim_token=$3 AND claim_xid<>pg_current_xact_id()`, + [receiptId,oldArchiveProof,token]) + requireThat(claimed.rows.length===1&&claimed.rows[0].receipt_id===receiptId) + const {connectionId,operationId,planId,key,archived,projection,previousBytes}=await prepare(sql,target) + const tombstone = await sql.query(`INSERT INTO public.mcp_operation_replay_tombstones + (operation_id,plan_id,connection_id,idempotency_digest,action_id,target_server_ids,target_global) + VALUES($1,$2,$3,$4,'servers.poll',$5::bigint[],true) RETURNING operation_id`, + [operationId,planId,connectionId,operationIdempotencyDigest(connectionId,key),projection.history.affectedServerIds]) + requireThat(tombstone.rows.length===1 && tombstone.rows[0].operation_id===operationId) + await sql.query("SELECT set_config('mm.poll_retire_receipt',$1,true),set_config('mm.poll_retire_claim',$2,true)",[receiptId,token]) + const updated = await sql.query(`UPDATE public.server_poll_archives SET version=2,source_digest=$3,source_sha256=$4, + decoded_bytes=$5,compressed_bytes=$6,ciphertext=$7,ciphertext_bytes=$8,archive_proof=$9 + WHERE receipt_id=$1 AND version=1 AND archive_proof=$2 RETURNING receipt_id`, + [receiptId,oldArchiveProof,archived.source_digest,archived.source_sha256,archived.decoded_bytes,archived.compressed_bytes, + archived.ciphertext,archived.ciphertext_bytes,archived.archive_proof]) + requireThat(updated.rows.length===1 && updated.rows[0].receipt_id===receiptId) + const charged = await sql.query(`UPDATE public.server_poll_storage_admissions SET archive_bytes=$2 + WHERE receipt_id=$1 AND storage_state='compacted' AND archive_bytes=$3 RETURNING receipt_id`, + [receiptId,archived.ciphertext_bytes,previousBytes]) + requireThat(charged.rows.length===1 && charged.rows[0].receipt_id===receiptId) + const readback = (await sql.query(SERVER_POLL_ARCHIVE_READ_SQL,[receiptId])).rows + requireThat(readback.length===1 && readback[0].archive_proof===archived.archive_proof + && fingerprint(await openServerPollRetiredArchive(readback[0]))===fingerprint(projection)) + const step = await sql.query("DELETE FROM public.mcp_operation_steps WHERE operation_id=$1 AND step_index=0 RETURNING operation_id",[operationId]) + requireThat(step.rows.length===1 && step.rows[0].operation_id===operationId) + const operation = await sql.query("DELETE FROM public.mcp_operations WHERE id=$1 AND connection_id=$2 AND plan_id=$3 RETURNING id", + [operationId,connectionId,planId]) + requireThat(operation.rows.length===1 && operation.rows[0].id===operationId) + const plan = await sql.query("DELETE FROM public.mcp_plans WHERE id=$1 AND connection_id=$2 RETURNING id",[planId,connectionId]) + requireThat(plan.rows.length===1 && plan.rows[0].id===planId) + await assertServerPollArchiveAccounting(sql) + return { retired:true as const, automaticReplay:false as const } + },sql=>admitPollStorageWriter(sql,deadlineAt)) +} + +/** Exact internal entry used by owned fixtures. It cannot bypass the claim. */ +export function retireCompletedServerPollOperation(raw:Target) { + const target=request.parse(raw) + return sharedDatabaseGate.request(()=>retire(target,Date.now()+20000)) +} + +let cursor:{finishedAt:string;receiptId:string}|undefined +export function retireCompletedServerPollOperations() { + return sharedDatabaseGate.request(async()=>{ + const deadlineAt=Date.now()+20000 + // A live POLL may hold both locks. Skip before requesting its storage key. + const rows=await runPollStorageTransaction[]|null>(deps,{deadlineAt},async()=>{},async sql=>(await sql.query(CANDIDATES, + [cursor?.finishedAt??null,cursor?.receiptId??null])).rows,async sql=>{ + if(!await tryAdmitPollStorageMaintenanceWriter(sql,"transaction"))return {deferred:null} + }) + if(rows===null)return {scanned:0,retired:0,automaticReplay:false as const} + requireThat(rows.length<=8) + let retired=0,scanned=0 + for(const row of rows){ + if(Date.now()>=deadlineAt-2000||retired>=2)break + const target=request.parse({receiptId:row.receipt_id,oldArchiveProof:row.archive_proof}) + requireThat(typeof row.finished_at==="string"&&Number.isFinite(Date.parse(row.finished_at))) + cursor={finishedAt:row.finished_at,receiptId:target.receiptId} + scanned++ + try{await retire(target,deadlineAt);retired++} + catch(error){if(!(error instanceof PollStorageRefused))throw error} + } + // Keep the cursor for any unvisited rows. On exhaustion the next tick + // starts at the beginning, revisiting formerly held or busy candidates. + if(scanned===rows.length&&rows.length<8)cursor=undefined + return {scanned,retired,automaticReplay:false as const} + }) +} diff --git a/backend/src/services/server-poll-production.test.ts b/backend/src/services/server-poll-production.test.ts new file mode 100644 index 00000000..108a4840 --- /dev/null +++ b/backend/src/services/server-poll-production.test.ts @@ -0,0 +1,101 @@ +import assert from "node:assert/strict" +import { createHmac,randomUUID } from "node:crypto" +import { PARTITION_SPECS } from "../db/partitions.js" +import { createServerPollProductionDependencies,createServerPollProductionExecution,type ServerPollProductionInput,type ServerPollProductionIo } from "./server-poll-production.js" +import { ServerPollProblem } from "./server-poll-execution-model.js" +import { ServerPollExecutionError,type ServerPollReceipt } from "./server-poll-execution.js" + +// Pure query/native/lock doubles. No real pool, SQL, DNS, sockets or collectors. +type Row=Record +const time="2026-10-25T12:00:00.000Z",epoch="11111111-2222-3333-4444-555555555555" +const server=(id:number):Row=>({id,name:id===1?"before":"foreign",host:`192.0.2.${id}`,port:443,username:"fixture-admin",password:"NEVER_POLL_PASSWORD_"+id,use_ssl:true,verify_ssl:true,enabled:true,routing_endpoint_epoch:epoch,updated_at:"2026-10-24T12:00:00+00:00",opaque:"NEVER_POLL_SERVER"}) +const canonical=(value:unknown):unknown=>Array.isArray(value)?value.map(canonical):value&&typeof value==="object"?Object.fromEntries(Object.entries(value).sort(([a],[b])=>a.localeCompare(b)).map(([key,item])=>[key,canonical(item)])):value +const proof=async(value:unknown)=>createHmac("sha256","inert-production-adapter-fixture-only").update(JSON.stringify(canonical(value))).digest("hex") +let servers:Row[],snapshots:Row[],partitions:Row[],allowed:number[],revoked:boolean,sql:string[],native:string[],locks:string[],events:string[],cache:number,mode:string,afterQuery:(()=>void)|undefined,claim:{proof:string;receipt?:ServerPollReceipt}|undefined,unsafeReleases:number +const initialPartition=():Row=>({parent:"server_snapshots",name:"server_snapshots_w_20000103",namespace:"public",bound:"FOR VALUES FROM ('2000-01-03T00:00:00.000Z') TO ('2000-01-10T00:00:00.000Z')",reloptions:[],catalog_json:JSON.stringify({oid:"100",relname:"server_snapshots_w_20000103",opaque:"NEVER_PARTITION"})}) +function reset(){servers=[server(1),server(2)];snapshots=[{id:1,server_id:1,polled_at:"2026-10-24T12:00:00+00:00",status:"online",raw_interfaces:[{name:"old",opaque:"NEVER_RAW"}],raw_ip_addresses:[],opaque:"NEVER_HISTORY"}];partitions=[initialPartition()];allowed=[1,2];revoked=false;sql=[];native=[];locks=[];events=[];cache=0;mode="normal";afterQuery=undefined;claim=undefined;unsafeReleases=0} +const isBundle=(text:string)=>text.includes("/* poll-context:bundle */") +function contextBundle():Row[]{ + const tagged=(source:string,rows:Row[])=>rows.map((row,index)=>({source,row_order:String(index+1),raw_json:JSON.stringify(row)})) + const parents=PARTITION_SPECS.map((spec,index)=>({name:spec.parent,namespace:"public",relkind:mode==="bad-parent"&&index===0?"r":"p",oid:String(500+index),owner:"1",partition_key:"RANGE ("+(["flow_buckets","flow_minute_stats","flow_minute_dims","flow_hour_facts"].includes(spec.parent)?"bucket_at":["flow_daily_dims","flow_daily_facts"].includes(spec.parent)?"day":spec.parent==="server_snapshots"?"polled_at":"sampled_at")+")"})) + const history=mode==="snapshot-overflow"?Array.from({length:2001},(_,index)=>({...snapshots[0],id:index+1})):snapshots.map(row=>mode==="snapshot-oversize"?{...row,raw_interfaces:[{opaque:"x".repeat(1048577)}]}:row) + const settings={id:1,retention_days:14,opaque:"NEVER_SETTINGS",updated_at:time} + const rows:Row[]=[...tagged("servers",servers),...tagged("traffic",[settings]),...tagged("uptime",[settings]),...tagged("internetPath",[settings]),...tagged("parents",parents),...tagged("partitions",partitions),...tagged("snapshots",history),...tagged("protections",[{id:"guard",object_ref:{kind:"server",id:"1",serverId:1,protection:"approval"},created_by:"fixture-owner"}])] + if(mode==="oversize")rows[0].raw_json=null + if(mode==="unknown-source")rows[0].source="unexpected" + if(mode==="duplicate-ordinal")rows[1].row_order=rows[0].row_order + if(mode==="ordinal-gap")rows[1].row_order="3" + if(mode==="numeric-ordinal")rows[0].row_order=1 + if(mode==="non-string-payload")rows[0].raw_json={id:1} + if(mode==="partition-null-sentinel")rows.find(row=>row.source==="partitions")!.raw_json=null + if(mode==="parent-null-key")rows.find(row=>row.source==="parents")!.raw_json=JSON.stringify({...parents[0],partition_key:null}) + if(mode==="settings-overflow")rows.push({...rows.find(row=>row.source==="traffic")!,row_order:"2"}) + return mode==="shuffled"?rows.reverse():rows +} +reset() +const io:ServerPollProductionIo={ + connect:async options=>{options.signal?.throwIfAborted();return {release:unsafe=>{if(unsafe)unsafeReleases++},query:async(text,values,options)=>{ + options.signal?.throwIfAborted();assert.ok(options.timeoutMs>0&&options.timeoutMs<=10000);sql.push(text);events.push(isBundle(text)?"bundle":text) + let rows:Row[]=[] + if(isBundle(text)){assert.deepEqual(values,[[1,2],PARTITION_SPECS.map(spec=>spec.parent),1]);rows=contextBundle();if(mode==="revoke-after-bundle")revoked=true} + else if(text==="SELECT id FROM servers ORDER BY id LIMIT 1001"){rows=servers.map(row=>({id:row.id}));if(mode==="revoke-after-metadata")revoked=true} + else if(text.includes("FROM servers s WHERE"))rows=servers.map(row=>({raw_json:mode==="oversize"?null:JSON.stringify(row)})) + else if(/FROM (traffic_settings|uptime_settings|internet_path_settings) s/.test(text))rows=[{raw_json:JSON.stringify({id:1,retention_days:14,opaque:"NEVER_SETTINGS",updated_at:time})}] + else if(text.includes("pg_get_partkeydef"))rows=PARTITION_SPECS.map((spec,index)=>({name:spec.parent,namespace:"public",relkind:mode==="bad-parent"&&index===0?"r":"p",oid:String(500+index),owner:"1",partition_key:"RANGE ("+(["flow_buckets","flow_minute_stats","flow_minute_dims","flow_hour_facts"].includes(spec.parent)?"bucket_at":["flow_daily_dims","flow_daily_facts"].includes(spec.parent)?"day":spec.parent==="server_snapshots"?"polled_at":"sampled_at")+")"})) + else if(text.includes("FROM pg_inherits"))rows=partitions.filter(row=>values[1]===undefined||row.name===values[1]) + else if(text.includes("FROM server_snapshots s WHERE"))rows=mode==="snapshot-overflow"?Array.from({length:2001},(_,index)=>({raw_json:JSON.stringify({...snapshots[0],id:index+1})})):snapshots.map(row=>({raw_json:JSON.stringify(mode==="snapshot-oversize"?{...row,raw_interfaces:[{opaque:"x".repeat(1048577)}]}:row)})) + else if(text.includes("FROM mcp_protections s"))rows=[{raw_json:JSON.stringify({id:"guard",object_ref:{kind:"server",id:"1",serverId:1,protection:"approval"},created_by:"fixture-owner"})}] + else if(text.startsWith("UPDATE servers s SET")){const row=servers.find(row=>row.id===values[0])!;row.name=values[1];row.updated_at=values[2];rows=[{raw_json:JSON.stringify(row)}]} + else if(text.startsWith("INSERT INTO server_snapshots")){const columns=text.match(/\(([^)]+)\) VALUES/)![1].split(",");const row:Row={id:2};for(let index=0;indexrow.name!==name)} + else if(text.startsWith("CREATE TABLE")){const match=text.match(/"public"\."([^"]+)" PARTITION OF "public"\."([^"]+)" FOR VALUES FROM \('([^']+)'\) TO \('([^']+)'\)/)!;if(!partitions.some(row=>row.name===match[1]))partitions.push({parent:match[2],name:match[1],namespace:"public",bound:`FOR VALUES FROM ('${match[3]}') TO ('${match[4]}')`,reloptions:[],catalog_json:JSON.stringify({oid:String(1000+partitions.length),relname:match[1],opaque:"NEVER_NEW_PARTITION"})})} + else if(text.startsWith("ALTER TABLE")){const name=text.match(/"public"\."([^"]+)"/)![1],row=partitions.find(row=>row.name===name)!;row.reloptions=["fillfactor=70","autovacuum_vacuum_scale_factor=0.05","autovacuum_vacuum_cost_limit=2000"]} + else if(!["BEGIN","BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY","COMMIT","SET LOCAL TIME ZONE 'UTC'"].includes(text)&&!text.startsWith("SET LOCAL statement_timeout="))throw Error("Unexpected inert SQL") + if(afterQuery){const hook=afterQuery;afterQuery=undefined;hook()} + if(mode==="lost-commit"&&text==="COMMIT"&&servers[0].name==="after")throw Error("NEVER_COMMIT_ACK") + return {rows,rowCount:rows.length} + }}}, + applicationLock:async work=>{locks.push("global");return work()},serverLocks:async(ids,work)=>{assert.deepEqual(ids,[1,2]);locks.push("servers");return work()}, + native:async(read,_server,options)=>{await options.beforeDispatch();native.push(read);if(read==="identity")return{name:"after"};if(read==="resource")return{"cpu-load":"5","free-memory":"1024","total-memory":"2048",version:"7","board-name":"CHR",uptime:"1d"};if(read==="interfaces")return[{".id":"*1",name:"ether1",opaque:"NEVER_NATIVE"}];return[{".id":"*2",interface:"ether1",address:"192.0.2.1/24"}]}, + invalidateCache:async()=>{cache++}, +} +const request=()=>({serverId:1,receiptId:randomUUID()}) +const input=(bound=request()):ServerPollProductionInput=>({request:bound,io,now:()=>Date.parse(time),proof,authorize:async(_request,_ids,signal)=>{events.push("fresh");signal?.throwIfAborted();if(revoked)throw new ServerPollProblem("PERMISSION_DENIED","inert revoke");return{global:true,serverIds:allowed,authorityProof:"a".repeat(64)}},maintenanceLease:async(work,options)=>{assert.deepEqual(options.serverIds,[1,2]);locks.push("writers-drained");return work()},journal:{intent:async()=>{},acknowledge:async()=>{},claim:async(_request,bindingProof)=>{if(claim)return claim.receipt?{state:"known",bindingProof:claim.proof,receipt:claim.receipt}:{state:"unresolved",bindingProof:claim.proof};claim={proof:bindingProof};return{state:"new"}},complete:async(receipt)=>{claim!.receipt=structuredClone(receipt)}}}) +let configured=input(),deps=createServerPollProductionDependencies(configured),checks=0 +const context=await deps.readContext(configured.request,async()=>{checks++},[1,2],{deadlineAt:Date.parse(time)+45000}) +assert.equal(context.settings.traffic!.retentionDays,14);assert.equal(context.servers[0].routingEndpointEpoch,epoch);assert.equal(context.snapshots[0].polledAt,"2026-10-24T12:00:00.000Z");assert.equal(context.partitions[0].from,"2000-01-03T00:00:00.000Z");assert.ok(checks>sql.length,"Every SQL await has before/after authorization") +assert.equal(sql.filter(isBundle).length,1,"One bounded bundle reads every private context source") +assert.deepEqual(sql.filter(text=>text.startsWith("SELECT id FROM servers")||isBundle(text)).map(text=>isBundle(text)?"bundle":"metadata"),["metadata","bundle"]) +for(const text of sql){const event=isBundle(text)?"bundle":text,index=events.indexOf(event);assert.deepEqual(events.slice(index-1,index+2),["fresh",event,"fresh"],"Every physical SQL keeps fresh authority immediately on both sides")} +assert.equal(context.servers[0].opaque,"NEVER_POLL_SERVER");assert.equal(context.settings.traffic!.opaque,"NEVER_SETTINGS");assert.equal(context.snapshots[0].opaque,"NEVER_HISTORY");assert.equal((context.partitions[0].catalog as Row).opaque,"NEVER_PARTITION") +reset();mode="shuffled";configured=input();deps=createServerPollProductionDependencies(configured) +assert.deepEqual(await deps.readContext(configured.request,async()=>{},[1,2],{deadlineAt:Date.parse(time)+45000}),context,"Tagged ordinals conserve the complete context despite shuffled transport rows") +reset();allowed=[1];configured=input();deps=createServerPollProductionDependencies(configured);await assert.rejects(deps.readContext(configured.request,async()=>{},[1],{deadlineAt:Date.parse(time)+45000}));assert.ok(!sql.some(isBundle),"Metadata denies before credentials/settings bundle") +reset();configured=input();deps=createServerPollProductionDependencies(configured);afterQuery=()=>{revoked=true};await assert.rejects(deps.readContext(configured.request,async()=>{},[1,2],{deadlineAt:Date.parse(time)+45000}));assert.ok(!sql.some(isBundle)) +for(const fault of ["oversize","bad-parent","snapshot-oversize","snapshot-overflow","unknown-source","duplicate-ordinal","ordinal-gap","numeric-ordinal","non-string-payload","partition-null-sentinel","parent-null-key","settings-overflow"]){reset();mode=fault;configured=input();deps=createServerPollProductionDependencies(configured);await assert.rejects(deps.readContext(configured.request,async()=>{},[1,2],{deadlineAt:Date.parse(time)+45000}),ServerPollProblem,fault);assert.equal(native.length,0);assert.ok(!sql.includes("COMMIT"),fault+" refuses the whole context")} +for(const fault of ["revoke-after-metadata","revoke-after-bundle"]){reset();mode=fault;configured=input();deps=createServerPollProductionDependencies(configured);await assert.rejects(deps.readContext(configured.request,async()=>{},[1,2],{deadlineAt:Date.parse(time)+45000}));assert.equal(sql.filter(isBundle).length,fault==="revoke-after-bundle"?1:0);assert.ok(!sql.includes("COMMIT"));assert.ok(unsafeReleases>0)} +reset();configured=input();const executor=createServerPollProductionExecution(configured),plan=await executor.prepare(configured.request);assert.equal(native.length,0) +const receipt=await executor.execute(plan) +assert.equal(receipt.complete,true);assert.equal(receipt.maintenanceComplete,true);assert.deepEqual(native.sort(),["addresses","identity","interfaces","resource"]);assert.deepEqual(locks,["global","servers","writers-drained"]);assert.equal(cache,1);assert.equal(snapshots[0].raw_interfaces,null);assert.equal(servers[1].name,"foreign");assert.ok(sql.some(text=>text.startsWith("DROP TABLE")));assert.ok(sql.some(text=>text.startsWith("CREATE TABLE")));assert.ok(sql.some(text=>text.startsWith("ALTER TABLE")));assert.ok(!JSON.stringify(receipt).includes("NEVER_")) +const writes=sql.filter(text=>/^(UPDATE|INSERT|CREATE|DROP|ALTER)/.test(text)).length;await executor.execute(plan);assert.equal(sql.filter(text=>/^(UPDATE|INSERT|CREATE|DROP|ALTER)/.test(text)).length,writes,"Known receipt never replays SQL/DDL") +reset();configured=input();const failed=createServerPollProductionExecution(configured),pending=await failed.prepare(configured.request);mode="lost-commit";await assert.rejects(failed.execute(pending),ServerPollExecutionError);assert.equal(servers[0].name,"after");assert.equal(cache,0);assert.ok(unsafeReleases>0);const count=sql.filter(text=>text.startsWith("UPDATE servers")).length;await assert.rejects(failed.execute(pending));assert.equal(sql.filter(text=>text.startsWith("UPDATE servers")).length,count) +reset();configured=input();deps=createServerPollProductionDependencies(configured);await assert.rejects(deps.effect({kind:"flow-cache.invalidate",serverId:1},{receiptId:configured.request.receiptId,stage:0,deadlineAt:Date.parse(time)+45000,beforeDispatch:async()=>{}}));assert.equal(cache,0,"No publication outside owned global/server/writer lease") +assert.throws(()=>createServerPollProductionDependencies({...input(),maintenanceLease:undefined as never})) +assert.throws(()=>createServerPollProductionDependencies({...input(),journal:{} as never})) +reset();configured=input();deps=createServerPollProductionDependencies(configured);await assert.rejects(deps.readContext({...configured.request,receiptId:randomUUID()},async()=>{},[1,2],{deadlineAt:Date.parse(time)+45000}));assert.equal(sql.length,0,"Bound adapter refuses another operation before SQL") +reset();configured=input();const late=createServerPollProductionExecution({...configured,journal:{...configured.journal,intent:async value=>{if(value.stage==="publish.0")revoked=true}}}),latePlan=await late.prepare(configured.request);await assert.rejects(late.execute(latePlan));assert.ok(!sql.some(text=>text.startsWith("UPDATE servers")),"Revocation after durable intent prevents SQL dispatch") +reset();configured=input();deps=createServerPollProductionDependencies({...configured,io:{...io,connect:async()=>{throw Error("NEVER_SQL_CREDENTIAL")}}});await assert.rejects(deps.readContext(configured.request,async()=>{},[1,2],{deadlineAt:Date.parse(time)+45000}),(error:unknown)=>error instanceof ServerPollProblem&&!error.message.includes("NEVER_")) +reset();configured=input();let delegated=0 +const sameBackendIo:ServerPollProductionIo={...io, + connect:async()=>{throw Error("Adapter must use supplied same-backend transaction")}, + transaction:async(readOnly,fence,options,work)=>{ + delegated++;assert.equal(readOnly,true);await fence() + const connection=await io.connect(options) + try{return await work(async(text,values=[])=>{await fence();const result=await connection.query(text,values,{signal:options.signal,timeoutMs:1000});await fence();return result})}finally{connection.release(false)} + }, +} +deps=createServerPollProductionDependencies({...configured,io:sameBackendIo}) +assert.equal((await deps.readContext(configured.request,async()=>{},[1,2],{deadlineAt:Date.parse(time)+45000})).servers.length,2) +assert.equal(delegated,1,"Context must share the maintenance transaction backend, including nested reads") +console.log("Server poll production adapter inert tests: actual schema mapping, metadata-before-secrets, full raw context, source fences, global/server/writer locks, same-backend transaction delegation, all publications/DDL, lost commit no replay passed; no SQL/network executed") diff --git a/backend/src/services/server-poll-production.ts b/backend/src/services/server-poll-production.ts new file mode 100644 index 00000000..6861e383 --- /dev/null +++ b/backend/src/services/server-poll-production.ts @@ -0,0 +1,283 @@ +import { carryPollError } from "./server-poll-error-provenance.js" +import { projectServerPollApprovalContext } from "./server-poll-approval-context.js" +import { assertServerPollSettingsEpochCatalog, SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL, SERVER_POLL_SETTINGS_TABLES } from "./server-poll-settings-epoch.js" +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +import { AsyncLocalStorage } from "node:async_hooks" +import type { QueryConfig } from "pg" +import type { ServerPartitionTransaction } from "./server-partition-maintenance-lease.js" +import type { ServerPollBoundaryCoordinator } from "./server-poll-boundary.js" +import { PARTITION_SPECS } from "../db/partitions.js" +import { createServerPollExecution,type ServerPollDependencies,type ServerPollGrant,type ServerPollRequest } from "./server-poll-execution.js" +import { ServerPollProblem,pollProblem,validateServerPollContext,planServerPollMaintenance,type ServerPollContext,type PollRow,type ServerPollAck } from "./server-poll-execution-model.js" + +export interface ServerPollSqlConnection { + query(text:string,values:unknown[],options:{signal?:AbortSignal;timeoutMs:number;statementName?:string}):Promise<{rows:Record[];rowCount?:number|null;command?:string}> + release(unsafe?:boolean):void + on?(event:"error",listener:(error:Error)=>void):unknown + removeListener?(event:"error",listener:(error:Error)=>void):unknown +} +export interface ServerPollProductionIo { + connect(options:{signal?:AbortSignal;deadlineAt:number}):Promise + /** A maintenance lease may bind every transaction (and nested context read) + * to its one SQL backend. No global pool replacement is permitted. */ + transaction?:ServerPartitionTransaction + applicationLock:(work:()=>Promise)=>Promise + serverLocks:(ids:number[],work:()=>Promise)=>Promise + native:ServerPollDependencies["native"] + invalidateCache:()=>Promise +} +export interface ServerPollProductionInput { + /** Fixed private composition switch. Public storage sessions always enable v3. */ + approvalProtocol?:"configuration-epoch-v3" + boundary?:ServerPollBoundaryCoordinator + request:ServerPollRequest + authorize:ServerPollDependencies["authorize"] + /** REQUIRED durable integration. No process-memory journal fallback exists. */ + journal:Pick + /** REQUIRED: quiesce/drain ALL affected partition writers across replicas and + * keep their exclusion held until work settles. Config locks alone do not + * fence scheduler/NetFlow writers. No default/no-op implementation exists. */ + maintenanceLease:(work:()=>Promise,options:{signal?:AbortSignal;deadlineAt:number;serverIds:number[]})=>Promise + io?:ServerPollProductionIo + proof?:ServerPollDependencies["proof"] + now?:()=>number +} +const MAX_BYTES=1048576 +const parents=PARTITION_SPECS.map(spec=>spec.parent) +const partitionKeys:Record=Object.fromEntries(parents.map(parent=>[parent,["flow_buckets","flow_minute_stats","flow_minute_dims","flow_hour_facts"].includes(parent)?"RANGE (bucket_at)":["flow_daily_dims","flow_daily_facts"].includes(parent)?"RANGE (day)":parent==="server_snapshots"?"RANGE (polled_at)":"RANGE (sampled_at)"])) +const factStorage={fillfactor:"70",autovacuum_vacuum_scale_factor:"0.05",autovacuum_vacuum_cost_limit:"2000"} +const settingsTables={traffic:"traffic_settings",uptime:"uptime_settings",internetPath:"internet_path_settings"}as const +const dates=new Set(["created_at","updated_at","polled_at","last_collected_at"]) +const bounded=(value:unknown,limit=MAX_BYTES)=>{try{return Buffer.byteLength(JSON.stringify(value))<=limit}catch{return false}} +function record(value:unknown):value is Record{return !!value&&typeof value==="object"&&!Array.isArray(value)&&Object.getPrototypeOf(value)===Object.prototype} +function rawRow(value:unknown):PollRow{ + if(typeof value!=="string"||Buffer.byteLength(value)>MAX_BYTES)pollProblem("STATE_UNAVAILABLE","Отсутствует bounded SQL row server poll") + let raw:unknown + try{raw=JSON.parse(value,(_key,item:unknown)=>{if(typeof item==="number"&&(!Number.isFinite(item)||Number.isInteger(item)&&!Number.isSafeInteger(item)))throw Error();return item})}catch{pollProblem("STATE_UNAVAILABLE","Непредставимая SQL row server poll")} + if(!record(raw))pollProblem("STATE_UNAVAILABLE","Повреждена SQL row server poll") + const result:PollRow={} + for(const [key,item]of Object.entries(raw)){ + const name=key.replace(/_([a-z])/g,(_match,letter:string)=>letter.toUpperCase()) + if(Object.hasOwn(result,name))pollProblem("STATE_UNAVAILABLE","Неоднозначное имя SQL поля server poll") + let normalized=item + if(dates.has(key)&&item!==null){if(typeof item!=="string"||!Number.isFinite(Date.parse(item)))pollProblem("STATE_UNAVAILABLE","Не подтверждено время SQL row server poll");normalized=new Date(item).toISOString()} + result[name]=normalized as PollRow[string] + } + return result +} +function requireRows(rows:Record[],limit:number):void{if(!Array.isArray(rows)||rows.length>limit)pollProblem("STATE_UNAVAILABLE","Превышен bounded SQL каталог server poll")} +function partitionRow(raw:Record):PollRow{ + if(typeof raw.parent!=="string"||!parents.includes(raw.parent)||typeof raw.name!=="string"||typeof raw.namespace!=="string"||raw.namespace!=="public"||typeof raw.bound!=="string"||!Array.isArray(raw.reloptions)||raw.reloptions.some(option=>typeof option!=="string")||typeof raw.catalog_json!=="string")pollProblem("STATE_UNAVAILABLE","Неполный pg partition каталог server poll") + const storage:Record={} + for(const option of raw.reloptions as string[]){const equal=option.indexOf("=");if(equal<=0||Object.hasOwn(storage,option.slice(0,equal)))pollProblem("STATE_UNAVAILABLE","Повреждены reloptions partition");storage[option.slice(0,equal)]=option.slice(equal+1)} + const match=raw.bound.match(/^FOR VALUES FROM \('([^']+)'\) TO \('([^']+)'\)$/) + const parsed=(text:string)=>{const value=Date.parse(text);if(!Number.isFinite(value))pollProblem("STATE_UNAVAILABLE","Неподдерживаемые bounds partition");return new Date(value).toISOString()} + return {namespace:raw.namespace,parent:raw.parent,name:raw.name,bound:raw.bound,from:match?parsed(match[1]):null,to:match?parsed(match[2]):null,storage,catalog:rawRow(raw.catalog_json)} +} +const partitionSql=`SELECT p.relname AS parent,c.relname AS name,n.nspname AS namespace,pg_get_expr(c.relpartbound,c.oid,true) AS bound, + COALESCE(c.reloptions,ARRAY[]::text[]) AS reloptions, + CASE WHEN octet_length(to_jsonb(c)::text)<=16384 AND SUM(octet_length(to_jsonb(c)::text)) OVER (ORDER BY c.oid)<=1048576 THEN (to_jsonb(c)-ARRAY['reloptions','relpages','reltuples','relallvisible','relallfrozen','relfrozenxid','relminmxid'])::text END AS catalog_json + FROM pg_inherits i JOIN pg_class c ON c.oid=i.inhrelid JOIN pg_class p ON p.oid=i.inhparent + JOIN pg_namespace pn ON pn.oid=p.relnamespace JOIN pg_namespace n ON n.oid=c.relnamespace + WHERE pn.nspname='public' AND p.relname=ANY($1::text[])` +const jsonSelect=(alias:string)=>`CASE WHEN octet_length(to_jsonb(${alias})::text)<=1048576 THEN to_jsonb(${alias})::text END AS raw_json` +/** Enforce the aggregate transfer budget IN SQL. A per-row CASE alone could + * otherwise transfer 2,001 individually valid one-MiB raw snapshots. The first + * over-budget row remains present as NULL and is rejected, never truncated. */ +const boundedRowsSql=(table:string,where:string,order:string,limit:number)=>`WITH poll_rows AS ( + SELECT ${jsonSelect("s")},octet_length(to_jsonb(s)::text) AS row_bytes,row_number() OVER (ORDER BY ${order}) AS row_order + FROM ${table} s ${where} ORDER BY ${order} LIMIT ${limit} +) SELECT row_order,CASE WHEN SUM(row_bytes) OVER (ORDER BY row_order)<=1048576 THEN raw_json END AS raw_json FROM poll_rows ORDER BY row_order` +const parentSql="SELECT p.relname AS name,n.nspname AS namespace,p.relkind,p.oid::text AS oid,p.relowner::text AS owner,CASE WHEN octet_length(pg_get_partkeydef(p.oid))<=256 THEN pg_get_partkeydef(p.oid) END AS partition_key FROM pg_class p JOIN pg_namespace n ON n.oid=p.relnamespace WHERE n.nspname='public' AND p.relname=ANY($2::text[]) ORDER BY p.oid LIMIT 13" +const contextSources=["servers","traffic","uptime","internetPath","parents","partitions","snapshots","protections","settingsEpoch"]as const +type ContextSource=typeof contextSources[number] +const tagged=(source:ContextSource,sql:string)=>`SELECT '${source}'::text AS source,q.row_order,q.raw_json FROM (${sql}) q` +/** One snapshot statement after metadata authorization. Each source keeps its + * original overflow sentinel and ordering; the outer budget bounds transfer + * across all sources, including catalog envelopes. The transport envelope may + * escape nested catalog JSON, so its cap is 8 MiB; each original source and the + * decoded complete context retain their original 1 MiB cap. NULL is never filtered. */ +const contextBundleSql=(epoch:boolean)=>`/* poll-context:bundle */ WITH bundle AS MATERIALIZED ( + ${[ + tagged("servers",boundedRowsSql("servers","WHERE id=ANY($1::int[])","s.id",1001)), + ...Object.entries(settingsTables).map(([key,table])=>tagged(key as ContextSource,boundedRowsSql(table,"WHERE id=1","s.id",2))), + tagged("parents",`SELECT row_number() OVER(ORDER BY p.oid::bigint) AS row_order,to_jsonb(p)::text AS raw_json FROM (${parentSql}) p`), + tagged("partitions",`SELECT row_number() OVER(ORDER BY (p.catalog_json::jsonb->>'oid')::bigint) AS row_order,to_jsonb(p)::text AS raw_json FROM (${partitionSql.replace("$1::text[]","$2::text[]")} ORDER BY c.oid LIMIT 1001) p`), + tagged("snapshots",boundedRowsSql("server_snapshots","WHERE server_id=$3","s.id,s.polled_at",2001)), + tagged("protections",boundedRowsSql("mcp_protections","","s.id",1001)), + ...(epoch?[tagged("settingsEpoch",`SELECT row_number() OVER(ORDER BY g.table_name,g.trigger_name) AS row_order,to_jsonb(g)::text AS raw_json FROM (${SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL.replace("$1::text[]","$4::text[]")}) g`)]:[]), + ].join("\nUNION ALL\n")} +), metered AS ( + SELECT source,row_order,raw_json,SUM(octet_length(raw_json)) OVER(ORDER BY source,row_order) AS transfer_bytes FROM bundle +) SELECT source,row_order::text AS row_order,CASE WHEN transfer_bytes<=8388608 THEN raw_json END AS raw_json FROM metered ORDER BY source,metered.row_order` +function contextBundle(rows:Record[]):Record[]> { + requireRows(rows,5028) + const groups=Object.fromEntries(contextSources.map(source=>[source,[]]))as unknown as Record[]> + const positions=new Set() + for(const row of rows){ + if(Object.keys(row).sort().join(",")!=="raw_json,row_order,source"||!contextSources.includes(row.source as ContextSource) + ||typeof row.row_order!=="string"||!/^[1-9]\d{0,4}$/.test(row.row_order)||typeof row.raw_json!=="string"||Buffer.byteLength(row.raw_json)>MAX_BYTES)pollProblem("STATE_UNAVAILABLE","Повреждён bounded context bundle poll") + const key=row.source+":"+row.row_order + if(positions.has(key))pollProblem("STATE_UNAVAILABLE","Неоднозначный порядок context bundle poll") + positions.add(key);groups[row.source as ContextSource].push(row) + } + for(const source of contextSources){groups[source].sort((a,b)=>Number(a.row_order)-Number(b.row_order));if(groups[source].some((row,index)=>Number(row.row_order)!==index+1))pollProblem("STATE_UNAVAILABLE","Неполный порядок context bundle poll")} + return groups +} +function catalogEnvelope(value:unknown):Record{ + if(typeof value!=="string"||Buffer.byteLength(value)>MAX_BYTES)pollProblem("STATE_UNAVAILABLE","Отсутствует bounded catalog envelope poll") + let parsed:unknown;try{parsed=JSON.parse(value)}catch{pollProblem("STATE_UNAVAILABLE","Повреждён catalog envelope poll")} + if(!record(parsed))pollProblem("STATE_UNAVAILABLE","Повреждён catalog envelope poll") + return parsed +} +const ident=(value:string)=>{if(!/^[a-z_][a-z\d_]{0,99}$/.test(value))pollProblem("INVALID_INPUT","Недопустимый SQL identifier poll");return '"'+value+'"'} + +/** Imports alone do not connect, query, acquire locks, invalidate a cache or + * create clients. Native GET uses the shared last-moment request hook. */ +export function createServerPollProductionIo():ServerPollProductionIo{ + return { + connect:async options=>{ + options.signal?.throwIfAborted();const {pool}=await import("../db/index.js") + let settled=false + const pending=pool.connect() + let timer:ReturnType|undefined,abort:(()=>void)|undefined + const client=await Promise.race([pending,new Promise((_resolve,reject)=>{timer=setTimeout(()=>reject(Error("SQL acquisition deadline")),Math.max(1,options.deadlineAt-Date.now()));abort=()=>reject(Error("SQL acquisition aborted"));options.signal?.addEventListener("abort",abort,{once:true})})]).then(client=>{settled=true;return client}).finally(async()=>{if(timer)clearTimeout(timer);if(abort)options.signal?.removeEventListener("abort",abort);if(!settled)await pending.then(client=>client.release(true),()=>{})}) + let released=false,unsafe=false + const release=(destroy=false)=>{if(released)return;released=true;client.release(destroy||unsafe)} + return {query:async(text,values,queryOptions)=>{ + queryOptions.signal?.throwIfAborted();if(released)pollProblem("STATE_UNAVAILABLE","SQL connection poll закрыт") + const cancel=()=>{unsafe=true;release(true)};queryOptions.signal?.addEventListener("abort",cancel,{once:true}) + if(queryOptions.signal?.aborted){cancel();pollProblem("ABORTED","SQL server poll отменён до dispatch")} + try{const request:QueryConfig&{query_timeout:number}={text,values,name:queryOptions.statementName,query_timeout:queryOptions.timeoutMs};return await client.query>(request)}catch(error){unsafe=true;throw error}finally{queryOptions.signal?.removeEventListener("abort",cancel)} + },release,on:(event,listener)=>client.on(event,listener),removeListener:(event,listener)=>client.removeListener(event,listener)} + }, + applicationLock:async work=>(await import("./config-mutation-lock.js")).withApplicationMutationLock(work), + serverLocks:async(ids,work)=>(await import("./config-mutation-lock.js")).withConfigMutationLock(ids,work), + native:async(read,server,options)=>{ + const {MikrotikClient}=await import("./mikrotik.js"),path={identity:"/system/identity",resource:"/system/resource",interfaces:"/interface",addresses:"/ip/address"}[read] + return MikrotikClient.fromServer(server as unknown as Parameters[0]).get(path,Math.max(1,Math.min(15000,options.deadlineAt-Date.now())),options.signal,MAX_BYTES,options.beforeDispatch) + }, + invalidateCache:async()=>{(await import("./traffic-flow-topology.js")).invalidateFlowCatalogCache()}, + } +} + +/** Bound to one private durable operation. The returned dependencies cannot be + * used for an unrelated request or outside their owned live lock/lease. */ +export function createServerPollProductionDependencies(input:ServerPollProductionInput):ServerPollDependencies{ + if(!input.request||!Number.isSafeInteger(input.request.serverId)||input.request.serverId<=0||!/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i.test(input.request.receiptId)||!input.journal||!["intent","acknowledge","claim","complete"].every(key=>typeof input.journal[key as keyof typeof input.journal]==="function")||typeof input.maintenanceLease!=="function"||typeof input.authorize!=="function")pollProblem("INVALID_INPUT","Нужны bound request, durable journal и global writer lease server poll") + const io=input.io??createServerPollProductionIo(),now=input.now??Date.now + const proof=input.proof??(async(value:unknown)=>{const security=await import("../modules/mcp/security.js");return security.privateSecretFingerprint(security.fingerprint(value),"server-poll-production-v1")}) + const held=new AsyncLocalStorage<{active:boolean;ids:number[];authorityProof:string;context?:ServerPollContext}>() + const bound=(request:ServerPollRequest)=>{if(request.serverId!==input.request.serverId||request.receiptId!==input.request.receiptId)pollProblem("STATE_CONFLICT","Request не принадлежит server poll adapter")} + const check=async(ids:number[],options:{signal?:AbortSignal;deadlineAt:number},expected?:string):Promise=>{ + options.signal?.throwIfAborted();if(now()>=options.deadlineAt)pollProblem("ABORTED","Истёк deadline server poll SQL") + const grant=await input.authorize(input.request,[...new Set([input.request.serverId,...ids])],options.signal) + if(!grant.global||!grant.serverIds.includes(input.request.serverId)||ids.some(id=>!grant.serverIds.includes(id))||!/^[a-f0-9]{64}$/.test(grant.authorityProof)||expected&&grant.authorityProof!==expected)pollProblem("PERMISSION_DENIED","Допуск global server poll изменился") + options.signal?.throwIfAborted();if(now()>=options.deadlineAt)pollProblem("ABORTED","Истёк deadline server poll SQL");return grant + } + const transaction=async(readOnly:boolean,fence:()=>Promise,options:{signal?:AbortSignal;deadlineAt:number},work:(query:(sql:string,values?:unknown[])=>Promise<{rows:Record[];rowCount?:number|null}>)=>Promise):Promise=>{ + if(io.transaction)return io.transaction(readOnly,fence,options,work) + await fence();let connection:ServerPollSqlConnection;try{connection=await io.connect(options)}catch(error){throw carryPollError(new ServerPollProblem("STATE_UNAVAILABLE","SQL connection server poll недоступен"),error,"poll.connect")};let unsafe=false,committed=false + const query=async(sql:string,values:unknown[]=[])=>{await fence();const timeoutMs=Math.max(1,Math.min(10000,options.deadlineAt-now()));let result:{rows:Record[];rowCount?:number|null};try{result=await connection.query(sql,values,{signal:options.signal,timeoutMs})}catch(error){unsafe=true;throw carryPollError(Error("SQL boundary unavailable"),error,"poll.sql")};await fence();return result} + try{await query(readOnly?"BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY":"BEGIN");await query("SET LOCAL TIME ZONE 'UTC'");await query(`SET LOCAL statement_timeout='${Math.max(1,Math.min(10000,options.deadlineAt-now()))}ms'`);const result=await work(query);await query("COMMIT");committed=true;return result} + finally{if(!committed)unsafe=true;connection.release(unsafe)} + } + const metadataIds=async(query:(sql:string,values?:unknown[])=>Promise<{rows:Record[]}>)=>{ + const {rows}=await query("SELECT id FROM servers ORDER BY id LIMIT 1001");requireRows(rows,1000) + const ids=rows.map(row=>Number(row.id));if(ids.some(id=>!Number.isSafeInteger(id)||id<=0)||new Set(ids).size!==ids.length)pollProblem("STATE_UNAVAILABLE","Повреждён metadata scope server poll");return ids + } + const approvalEnabled=input.approvalProtocol==="configuration-epoch-v3",approvalContexts=new WeakSet() + const readContext:ServerPollDependencies["readContext"]=async(request,beforeRead,permitted,options)=>{ + bound(request);const initial=await check([request.serverId],options) + const own=input.boundary?.fence(permitted,initial.authorityProof,options) + const fence=own?input.boundary!.combine([beforeRead,own],permitted,options)??(async()=>{await beforeRead();await check([request.serverId],options,initial.authorityProof)}):(async()=>{await beforeRead();await check([request.serverId],options,initial.authorityProof)}) + const context=await transaction(true,fence,options,async query=>{ + const ids=await metadataIds(query) + if(ids.some(id=>!permitted.includes(id)))pollProblem("PERMISSION_DENIED","Global metadata содержит сервер вне разрешённой области") + await check(ids,options,initial.authorityProof) + const bundle=contextBundle((await query(contextBundleSql(approvalEnabled),approvalEnabled?[ids,parents,request.serverId,[...SERVER_POLL_SETTINGS_TABLES]]:[ids,parents,request.serverId])).rows) + const serverRows=bundle.servers;requireRows(serverRows,1000) + const servers=serverRows.map(row=>rawRow(row.raw_json));if(servers.length!==ids.length||servers.some((row,index)=>row.id!==ids[index]))pollProblem("STATE_CONFLICT","Полный каталог серверов изменился") + if(servers.some(row=>typeof row.host!=="string"||!row.host.trim()||!Number.isSafeInteger(row.port)||Number(row.port)<1||Number(row.port)>65535||typeof row.username!=="string"||typeof row.password!=="string"||typeof row.useSsl!=="boolean"||typeof row.verifySsl!=="boolean"||typeof row.enabled!=="boolean"))pollProblem("STATE_UNAVAILABLE","Неполные endpoint credentials server poll") + const settings={}as ServerPollContext["settings"] + for(const key of Object.keys(settingsTables)){const rows=bundle[key as keyof typeof settingsTables];requireRows(rows,1);settings[key as keyof typeof settings]=rows.length?rawRow(rows[0].raw_json):null} + const parentRows=bundle.parents.map(row=>catalogEnvelope(row.raw_json)) + if(parentRows.length!==parents.length||new Set(parentRows.map(row=>row.name)).size!==parents.length||parentRows.some(row=>!parents.includes(String(row.name))||row.namespace!=="public"||row.relkind!=="p"||typeof row.partition_key!=="string"||row.partition_key.replaceAll('"',"").replace(/\s+/g," ").trim()!==partitionKeys[String(row.name)]))pollProblem("STATE_UNAVAILABLE","Неполный или несовместимый каталог partition parents") + const childRows=bundle.partitions.map(row=>catalogEnvelope(row.raw_json));requireRows(childRows,1000) + const snapshotsRaw=bundle.snapshots;requireRows(snapshotsRaw,2000) + const protectionsRaw=bundle.protections;requireRows(protectionsRaw,1000) + const captured:ServerPollContext&{partitionParents:PollRow[];settingsEpochGuard?:PollRow[]}={servers,settings,partitions:childRows.map(partitionRow),snapshots:snapshotsRaw.map(row=>rawRow(row.raw_json)),protections:protectionsRaw.map(row=>rawRow(row.raw_json)),partitionParents:parentRows as PollRow[]} + if(approvalEnabled){const guard=bundle.settingsEpoch.map(row=>catalogEnvelope(row.raw_json));assertServerPollSettingsEpochCatalog(guard);captured.settingsEpochGuard=guard as PollRow[]} + validateServerPollContext(captured,request.serverId) + if(!bounded(captured))pollProblem("STATE_UNAVAILABLE","Полный private context poll превышает 1 МиБ") + for(const effect of planServerPollMaintenance(captured,new Date(now()).toISOString()))if(effect.kind==="partition.ensure"){const old=captured.partitions.find(row=>row.name===effect.name);if(old&&(old.parent!==effect.parent||old.from!==effect.from||old.to!==effect.to))pollProblem("STATE_UNAVAILABLE","Существующая partition имеет другие границы")} + await check(ids,options,initial.authorityProof);if(approvalEnabled)approvalContexts.add(captured);return captured + }) + await fence();const lease=held.getStore();if(lease?.active){if(lease.authorityProof!==initial.authorityProof||JSON.stringify(lease.ids)!==JSON.stringify(context.servers.map(row=>Number(row.id))))pollProblem("STATE_CONFLICT","Global lock scope server poll изменился");lease.context=structuredClone(context)} + return context + } + const requireLease=(options:{signal?:AbortSignal;deadlineAt:number})=>{const lease=held.getStore();if(!lease?.active)pollProblem("PERMISSION_DENIED","Нет живой global maintenance lease poll");const live=()=>{if(!lease.active)pollProblem("PERMISSION_DENIED","Global lease poll закрыта")};return {lease,fence:input.boundary?.fence(lease.ids,lease.authorityProof,options,live)??(async()=>{live();await check(lease.ids,options,lease.authorityProof)})}} + const effect:ServerPollDependencies["effect"]=async(effect,options)=>{ + if(options.receiptId!==input.request.receiptId)pollProblem("STATE_CONFLICT","Publication poll принадлежит другому receipt") + const {lease,fence}=requireLease(options) + await fence() + if(effect.kind==="flow-cache.invalidate"){if(effect.serverId!==input.request.serverId)pollProblem("STATE_CONFLICT","Cache scope poll изменился");await options.beforeDispatch();await fence();await io.invalidateCache();await fence();return {}} + let dispatched=false + try{return await transaction(false,fence,options,async query=>{ + // query owns a fresh authority check immediately before the SQL send. + // beforeDispatch persists intent; no authority decision crosses that I/O. + const dispatch=async(sql:string,values:unknown[]=[])=>{if(dispatched)pollProblem("STATE_CONFLICT","Повторная SQL publication poll запрещена");await options.beforeDispatch();dispatched=true;return query(sql,values)} + let ack:ServerPollAck={} + if(effect.kind==="server.name"){ + if(effect.serverId!==input.request.serverId)pollProblem("STATE_CONFLICT","Изменён server name scope") + const result=await dispatch(`UPDATE servers s SET name=$2,updated_at=$3 WHERE id=$1 RETURNING ${jsonSelect("s")}`,[effect.serverId,effect.name,effect.updatedAt]) + if(result.rows.length!==1){pollProblem("VERIFICATION_FAILED","Не подтверждено обновление имени poll")} + const row=rawRow(result.rows[0].raw_json);if(row.id!==effect.serverId||row.name!==effect.name||row.updatedAt!==effect.updatedAt)pollProblem("VERIFICATION_FAILED","Не совпадает имя server poll") + }else if(effect.kind==="snapshot.insert"){ + if(effect.snapshot.serverId!==input.request.serverId||!lease.context||!bounded({...lease.context,snapshots:[...lease.context.snapshots,effect.snapshot]},MAX_BYTES-16384))pollProblem("STATE_UNAVAILABLE","Snapshot превысит проверяемый private budget poll") + const fields=["serverId","polledAt","status","latencyMs","rosVersion","boardName","uptime","cpuLoad","freeMemory","totalMemory","identityName","rawInterfaces","rawIpAddresses"] + if(Object.keys(effect.snapshot).some(key=>!fields.includes(key)))pollProblem("INVALID_INPUT","Неизвестное поле snapshot poll") + const columns=fields.map(key=>key.replace(/[A-Z]/g,letter=>"_"+letter.toLowerCase())),values=fields.map(key=>["rawInterfaces","rawIpAddresses"].includes(key)?JSON.stringify(effect.snapshot[key]??null):effect.snapshot[key]??null) + const result=await dispatch(`INSERT INTO server_snapshots AS s (${columns.join(",")}) VALUES (${fields.map((key,index)=>"$"+(index+1)+(["rawInterfaces","rawIpAddresses"].includes(key)?"::jsonb":"")).join(",")}) RETURNING ${jsonSelect("s")}`,values) + if(result.rows.length!==1)pollProblem("VERIFICATION_FAILED","Не подтверждён INSERT snapshot poll");ack={row:rawRow(result.rows[0].raw_json)} + }else if(effect.kind==="snapshot.raw.clear"){ + if(effect.serverId!==input.request.serverId||effect.ids.length>2000||effect.ids.some(id=>!Number.isSafeInteger(Number(id))||Number(id)<=0))pollProblem("INVALID_INPUT","Некорректный raw cleanup inventory poll") + const result=await dispatch("UPDATE server_snapshots SET raw_interfaces=NULL,raw_ip_addresses=NULL WHERE server_id=$1 AND polled_at<$2 AND id=ANY($3::bigint[]) AND (raw_interfaces IS NOT NULL OR raw_ip_addresses IS NOT NULL) RETURNING id::text AS id,server_id,polled_at::text,raw_interfaces,raw_ip_addresses",[effect.serverId,effect.beforeAt,effect.ids]) + if(result.rows.length!==effect.ids.length||result.rows.some(row=>!effect.ids.some(id=>String(id)===row.id)||row.raw_interfaces!==null||row.raw_ip_addresses!==null))pollProblem("VERIFICATION_FAILED","Не подтверждена точная очистка raw snapshots") + }else{ + if(!parents.includes(effect.parent)||!new RegExp("^"+effect.parent+"_[dwm]_\\d{6}(?:\\d{2})?$").test(effect.name))pollProblem("INVALID_INPUT","Недопустимая partition publication poll") + const table='"public".'+ident(effect.name) + if(effect.kind==="partition.drop")await dispatch("DROP TABLE IF EXISTS "+table) + else if(effect.kind==="partition.ensure"){ + if(![effect.from,effect.to].every(value=>Number.isFinite(Date.parse(value))&&new Date(value).toISOString()===value)||effect.from>=effect.to)pollProblem("INVALID_INPUT","Некорректные partition bounds poll") + await dispatch(`CREATE TABLE IF NOT EXISTS ${table} PARTITION OF "public".${ident(effect.parent)} FOR VALUES FROM ('${effect.from}') TO ('${effect.to}')`) + }else{ + if(!["flow_hour_facts","flow_daily_facts"].includes(effect.parent)||JSON.stringify(effect.settings)!==JSON.stringify(factStorage))pollProblem("INVALID_INPUT","Недопустимые fact storage settings poll") + await dispatch(`ALTER TABLE ${table} SET (fillfactor=70,autovacuum_vacuum_scale_factor=0.05,autovacuum_vacuum_cost_limit=2000)`) + } + const rows=(await query(partitionSql+" AND c.relname=$2 ORDER BY c.oid LIMIT 2",[parents,effect.name])).rows;requireRows(rows,1) + if(effect.kind==="partition.drop"){if(rows.length)pollProblem("VERIFICATION_FAILED","DROP partition poll не подтверждён")} + else{if(rows.length!==1)pollProblem("VERIFICATION_FAILED","Partition poll не подтверждена");const row=partitionRow(rows[0]);if(row.parent!==effect.parent||row.name!==effect.name||effect.kind==="partition.ensure"&&(row.from!==effect.from||row.to!==effect.to)||effect.kind==="partition.storage"&&Object.entries(factStorage).some(([key,value])=>(row.storage as Record)[key]!==value))pollProblem("VERIFICATION_FAILED","Partition read-back poll не совпадает");ack={row}} + } + // The transaction owner checks authority again before and after COMMIT. + return ack + })}catch(error){if(dispatched)pollProblem("MUTATION_OUTCOME_UNKNOWN","SQL/DDL poll могла примениться; повтор запрещён");throw error} + } + return { + boundary:input.boundary,now,proof,...(approvalEnabled?{approvalProof:async(context:ServerPollContext)=>{if(!approvalContexts.has(context))pollProblem("STATE_UNAVAILABLE","Отсутствует подлинный SQL снимок конфигурации poll");return proof(projectServerPollApprovalContext(context,input.request.serverId))}}:{}),authorize:async(request,ids,signal)=>{bound(request);return check(ids,{signal,deadlineAt:now()+SERVER_POLL_WALL_BUDGET_MS})},readContext, + withGlobalLock:async(work,options)=>{ + const initial=await check([input.request.serverId],options) + return io.applicationLock(async()=>{ + const fence=async()=>{await check([input.request.serverId],options,initial.authorityProof)} + const ids=await transaction(true,fence,options,metadataIds);await check(ids,options,initial.authorityProof) + return io.serverLocks(ids,()=>input.maintenanceLease(async()=>{ + await check(ids,options,initial.authorityProof);const lease={active:true,ids,authorityProof:initial.authorityProof} + try{return await held.run(lease,work)}finally{lease.active=false} + },{...options,serverIds:ids})) + }) + }, + native:async(read,server,options)=>{const {lease,fence}=requireLease(options);let dispatched=false;await fence();return io.native(read,server,{...options,beforeDispatch:async()=>{if(dispatched)pollProblem("STATE_CONFLICT","Повтор native dispatch poll запрещён");await fence();await options.beforeDispatch();await fence();const current=lease.context?.servers.find(row=>row.id===input.request.serverId);if(server.id!==input.request.serverId||!current||await proof(current)!==await proof(server)||dispatched)pollProblem("STATE_CONFLICT","Native endpoint/dispatch не совпадает с private source poll");dispatched=true}})}, + effect,intent:input.journal.intent,acknowledge:input.journal.acknowledge,claim:input.journal.claim,complete:input.journal.complete, + } +} +export function createServerPollProductionExecution(input:ServerPollProductionInput){return createServerPollExecution(createServerPollProductionDependencies(input))} diff --git a/backend/src/services/server-poll-rest-authority.sql-helper.ts b/backend/src/services/server-poll-rest-authority.sql-helper.ts new file mode 100644 index 00000000..7078ba6d --- /dev/null +++ b/backend/src/services/server-poll-rest-authority.sql-helper.ts @@ -0,0 +1,119 @@ +/** Owned fresh PostgreSQL acceptance using the fixed REST host and its actual + * authority factory. No source, signer, SQL, session or authority overrides. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, resolve } from "node:path" +import type { AuthUser } from "../lib/permissions.js" +import type { ServerPollRestContext } from "./server-poll-rest.js" + +export async function acceptServerPollRestAuthoritySql(options: { httpRoute?: true } = {}) { + // These assertions precede all imports capable of creating a configured pool. + assert.equal(process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH, "true") + const endpoint = new URL(process.env.DATABASE_URL ?? "") + assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.pathname, "/postgres") + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { ensurePartitionFor } = await import("../db/partitions.js") + const { executeDurableServerPollRest } = await import("./server-poll-rest.js") + const ownerId = "owned-rest-authority-" + randomUUID(), name = "owned-rest-authority" + let serverId: number | undefined, requests = 0, checkedAdmissions = 0, dispatchFailure = false + let app: Awaited> | undefined + let httpOrigin = "" + const paths: string[] = [] + const listener = createServer(async (request, response) => { + requests++; paths.push(request.url ?? "") + try { + assert.equal(request.method, "GET") + const rows = (await pool.query(`SELECT a.receipt_id,a.claim_token,a.claim_attempted_at,a.used_rows, + u.admissions,u.reserved_rows,u.reserved_bytes::text FROM server_poll_storage_admissions a + JOIN server_poll_storage_usage u ON u.scope='owner:'||a.owner_id + JOIN server_poll_executions e USING(receipt_id) + WHERE a.owner_id=$1 AND a.identity_kind='rest' AND e.context_proof=a.context_proof`, [ownerId])).rows + assert.equal(rows.length, 1); assert.equal(rows[0].claim_token, null) + assert(rows[0].claim_attempted_at); assert(Number(rows[0].used_rows) >= 1) + assert.equal(Number(rows[0].admissions), 1); assert.equal(Number(rows[0].reserved_rows), 4019) + assert.equal(rows[0].reserved_bytes, "8421376"); checkedAdmissions++ + const result = request.url === "/rest/system/identity" ? { name } + : request.url === "/rest/system/resource" ? { version: "7.synthetic", "board-name": "owned", uptime: "1h", "cpu-load": "2", "free-memory": "1024", "total-memory": "2048" } + : request.url === "/rest/interface" ? [{ type: "ether", name: "owned-interface" }] + : request.url === "/rest/ip/address" ? [{ interface: "owned-interface", address: "192.0.2.1/24" }] : null + assert.notEqual(result, null) + response.setHeader("content-type", "application/json"); response.statusCode = 200; response.end(JSON.stringify(result)) + } catch { + dispatchFailure = true; response.statusCode = 500; response.end("Owned dispatch refused") + } + }) + const actor: AuthUser = { id: ownerId, email: "owned@example.invalid", name: "Owned REST authority", apps: ["mm"], + permissions: ["mm:servers:write"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 600000 } + const context = (patch: Partial = {}): ServerPollRestContext => ({ actor, authRequired: true, + acceptedAt: Date.now(), requestId: randomUUID(), deadlineAt: Date.now() + 180000, ...patch }) + const call = async (id: string, accepted: ServerPollRestContext) => { + if (!options.httpRoute) return executeDurableServerPollRest(id, accepted) + assert(app && httpOrigin) + const { env } = await import("../config.js") + const token = accepted.actor ? app.jwt.sign({ iss: env.authIssuer, sub: accepted.actor.id, email: accepted.actor.email, name: accepted.actor.name, + apps: accepted.actor.apps, permissions: accepted.actor.permissions, iat: Math.floor(accepted.actor.issuedAt! / 1000), exp: Math.floor(accepted.actor.expiresAt! / 1000) }) : undefined + const response = await fetch(httpOrigin + "/api/servers/" + encodeURIComponent(id) + "/poll", { method: "POST", + headers: { "content-type": "application/json", "x-request-id": "untrusted-header", ...(token ? { authorization: "Bearer " + token } : {}) }, + body: JSON.stringify({ actor: { id: "untrusted-owner" }, authRequired: false, requestId: "untrusted-body", deadlineAt: Date.now() + 86400000 }), + signal: AbortSignal.timeout(185000) }) + return { statusCode: response.status, body: await response.json() as Record } + } + try { + await new Promise((yes, no) => { listener.once("error", no); listener.listen(0, "127.0.0.1", () => { listener.off("error", no); yes() }) }) + const port = (listener.address() as { port: number }).port + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES($1,'127.0.0.1',$2,true,'SYNTHETIC-USER','SYNTHETIC-PRIVATE-PASSWORD',false,true) RETURNING id", [name, port])).rows[0].id) + await ensurePartitionFor(pool, "server_snapshots", "week", new Date()) + if (options.httpRoute) { + const { buildApp } = await import("../index.js") + app = await buildApp({ logger: false, startScheduler: false }) + await app.listen({ host: "127.0.0.1", port: 0 }) + const address = app.server.address(); assert(address && typeof address !== "string") + httpOrigin = `http://127.0.0.1:${address.port}` + } + const before = (await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions")).rows[0].n + const negative = [ + await call("invalid-id", context()), + await call(String(serverId), context({ actor: undefined })), + await call(String(serverId), context({ actor: { ...actor, permissions: ["mm:servers:read"] } })), + await call("2147483647", context()), + ] + assert.deepEqual(negative.map(reply => reply.statusCode), [400, options.httpRoute ? 401 : 403, 403, 404]) + assert.equal(requests, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions")).rows[0].n, before) + const started = Date.now() + // Exactly one invocation. A failed/unknown attempt is never replayed here. + const reply = await call(String(serverId), context()) + const elapsedMs = Date.now() - started + if (reply.statusCode !== 200) { + const phase = (await pool.query(`SELECT a.used_rows,a.claim_attempted_at IS NOT NULL AS consumed, + EXISTS(SELECT 1 FROM server_poll_receipts r WHERE r.receipt_id=a.receipt_id) AS receipt, + EXISTS(SELECT 1 FROM server_poll_quarantines q WHERE q.receipt_id=a.receipt_id) AS quarantine, + (SELECT stage FROM server_poll_intents i WHERE i.receipt_id=a.receipt_id ORDER BY created_at DESC,stage DESC LIMIT 1) AS last_stage + FROM server_poll_storage_admissions a WHERE owner_id=$1`, [ownerId])).rows + console.log("Owned actual REST authority failure:", JSON.stringify({ elapsedMs, statusCode: reply.statusCode, + nativeGets: requests, phase, publicError: reply.body.error, automaticReplay: false })) + } + assert.equal(reply.statusCode, 200, "Actual REST authority full poll failed; no automatic replay") + assert.equal(dispatchFailure, false); assert.equal(requests, 4); assert.equal(checkedAdmissions, 4) + assert.deepEqual(paths.sort(), ["/rest/interface", "/rest/ip/address", "/rest/system/identity", "/rest/system/resource"]) + assert.deepEqual(Object.keys(reply.body).sort(), ["id", "serverId", "polledAt", "status", "latencyMs", "rosVersion", "boardName", "uptime", "cpuLoad", "freeMemory", "totalMemory", "identityName"].sort()) + assert.equal(reply.body.serverId, serverId); assert.equal(reply.body.status, "online"); assert.equal(reply.body.identityName, name) + assert.equal(reply.body.rosVersion, "7.synthetic"); assert.equal(reply.body.boardName, "owned"); assert.equal(reply.body.uptime, "1h") + assert.equal(reply.body.cpuLoad, 2); assert.equal(reply.body.freeMemory, 1024); assert.equal(reply.body.totalMemory, 2048) + assert(Number.isSafeInteger(reply.body.id) && Number(reply.body.id) > 0) + assert.equal(typeof reply.body.polledAt, "string"); assert(Number.isFinite(Date.parse(String(reply.body.polledAt)))) + assert.equal(typeof reply.body.latencyMs, "number"); assert(Number(reply.body.latencyMs) >= 0) + const serialized = JSON.stringify(reply.body) + for (const secret of ["SYNTHETIC-USER", "SYNTHETIC-PRIVATE-PASSWORD", "rawInterfaces", "rawIpAddresses", "192.0.2.1", "owned-interface"]) assert.equal(serialized.includes(secret), false) + const result = { statusCode: reply.statusCode, elapsedMs, nativeGets: requests, dispatchesAfterConsumedAdmission: checkedAdmissions, + actualRestAuthorityFactory: true, actualProductionSession: true, actualRegisteredHttpRoute: options.httpRoute === true, actualSignedJwt: options.httpRoute === true, httpAppSchedulerStarted: false, negativeStatuses: negative.map(value => value.statusCode), + exactLegacySnapshotShape: true, privateFieldsAbsent: true, automaticReplay: false } + console.log("Owned actual REST authority:", JSON.stringify(result)); return result + } finally { + await app?.close() + listener.closeAllConnections(); if (listener.listening) await new Promise(yes => listener.close(() => yes())) + if (serverId !== undefined) await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + } +} diff --git a/backend/src/services/server-poll-rest.test.ts b/backend/src/services/server-poll-rest.test.ts new file mode 100644 index 00000000..b25b0bff --- /dev/null +++ b/backend/src/services/server-poll-rest.test.ts @@ -0,0 +1,118 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { pool } from "../db/index.js" +import { McpError } from "../modules/mcp/security.js" +import type { ServerPollPlan, ServerPollReceipt } from "./server-poll-execution.js" +import { createDurableServerPollRestForTests, executeDurableServerPollRest, type ServerPollRestContext } from "./server-poll-rest.js" + +const context = (): ServerPollRestContext => ({ acceptedAt: Date.now(), requestId: randomUUID(), authRequired: true, + actor: { id: "operator", email: "", name: "", apps: ["mm"], permissions: ["mm:servers:write"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000 } }) +const gate = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const proof = "a".repeat(64) +function fixture() { + const events: string[] = [], inputs: Parameters[0]["session"]>[0][] = [] + let outcome: "online" | "offline" = "online", incomplete = false + const dependencies: Parameters[0] = { + authority: () => async () => { events.push("authority"); return { global: true, serverIds: [1, 2], authorityProof: proof } }, + session: input => { + inputs.push(input) + const plan: ServerPollPlan = { version: 1, requiresHuman: true, scope: "global", request: input.request, around: new Date().toISOString(), + beforeProof: proof, authorityProof: proof, bindingProof: proof, maintenance: [], affectedServerIds: [1, 2], mutationCost: 8 } + return { prepare: async () => { events.push("prepare"); return plan }, execute: async () => { + events.push("execute") + const receipt: ServerPollReceipt = { receiptId: input.request.receiptId, complete: !incomplete, maintenanceComplete: !incomplete, configurationVerified: true, + nativeReads: 4, confirmedPublications: 2, completedPublications: 2, afterProof: proof, observation: outcome, + snapshot: { id: 11, serverId: input.request.serverId, polledAt: "2026-10-25T12:00:00.000Z", status: outcome, + ...(outcome === "online" ? { latencyMs: 3, rosVersion: "7.20", boardName: "owned", uptime: "1h", cpuLoad: 2, freeMemory: 100, totalMemory: 200, identityName: "router" } : {}) } } + return receipt + } } + }, + } + return { dependencies, events, inputs, offline: () => { outcome = "offline" }, incomplete: () => { incomplete = true } } +} + +test("REST owns UUIDs and exact snapshot response after prepare/execute/post-authority", async () => { + const f = fixture(), execute = createDurableServerPollRestForTests(f.dependencies), accepted = context() + const result = await execute("1", accepted) + assert.equal(result.statusCode, 200) + assert.deepEqual(result.body, { id: 11, serverId: 1, polledAt: "2026-10-25T12:00:00.000Z", status: "online", latencyMs: 3, + rosVersion: "7.20", boardName: "owned", uptime: "1h", cpuLoad: 2, freeMemory: 100, totalMemory: 200, identityName: "router" }) + assert(!Object.hasOwn(result.body, "ok")); assert(!Object.hasOwn(result.body, "receiptId")) + assert.deepEqual(f.events, ["authority", "prepare", "execute", "authority"]) + const input = f.inputs[0]; assert.match(input.request.receiptId, /^[0-9a-f-]{36}$/); assert.notEqual(input.request.receiptId, input.identity.executionId) + assert.equal(input.identity.kind, "rest"); assert.equal(input.identity.ownerId, "operator"); assert.equal(input.identity.expiresAt, accepted.actor!.expiresAt) + assert.equal(input.identity.issuedAt, accepted.actor!.issuedAt); assert(Object.isFrozen(input.request)); assert(Object.isFrozen(input.identity)) +}) + +test("offline snapshot preserves all nullable legacy SnapshotRead fields", async () => { + const f = fixture(); f.offline() + const reply = await createDurableServerPollRestForTests(f.dependencies)("1", context()) + assert.equal(reply.statusCode, 200); assert.equal(reply.body.status, "offline") + for (const field of ["latencyMs", "rosVersion", "boardName", "uptime", "cpuLoad", "freeMemory", "totalMemory", "identityName"]) assert.equal(reply.body[field], null) +}) + +test("production unauthenticated/expired/app-less actors and malformed ID perform no SQL", async () => { + const prior = pool.connect, previousQuery = pool.query; let io = 0 + pool.connect = (() => { io++; throw Error("must not connect") }) as never + pool.query = (() => { io++; throw Error("must not query") }) as never + try { + const missing = await executeDurableServerPollRest("1", { ...context(), actor: undefined }); assert.equal(missing.statusCode, 403) + const expired = context(); expired.actor!.expiresAt = Date.now() - 1 + assert.equal((await executeDurableServerPollRest("1", expired)).statusCode, 403) + const wrongApp = context(); wrongApp.actor!.apps = [] + assert.equal((await executeDurableServerPollRest("1", wrongApp)).statusCode, 403) + assert.equal((await executeDurableServerPollRest("1;DROP TABLE servers", context())).statusCode, 400) + assert.equal(io, 0) + } finally { pool.connect = prior; pool.query = previousQuery } +}) + +test("authorized 404 happens before any private session read/publication", async () => { + const f = fixture(); f.dependencies.authority = () => async () => { throw new McpError("NOT_FOUND", "PRIVATE_PROVIDER", 404) } + assert.deepEqual(await createDurableServerPollRestForTests(f.dependencies)("1", context()), { statusCode: 404, body: { error: "Server not found" } }) + assert.equal(f.inputs.length, 0) +}) + +test("REST clones/freeze accepted actor and clips local operation to 180 seconds", async () => { + const f = fixture(), original = context(), pending = gate(), entered = gate() + let captured: ServerPollRestContext["actor"] + f.dependencies.authority = actor => { captured = actor; return async () => { entered.resolve(); await pending.promise; return { global: true, serverIds: [1, 2], authorityProof: proof } } } + const result = createDurableServerPollRestForTests(f.dependencies)("1", original) + await entered.promise; original.actor!.id = "mutated"; original.actor!.permissions.length = 0; pending.resolve() + assert.equal((await result).statusCode, 200); assert.equal(captured!.id, "operator"); assert(Object.isFrozen(captured)); assert(Object.isFrozen(captured!.permissions)) + const local = fixture(), accepted = { ...context(), actor: undefined, authRequired: false }, before = Date.now() + assert.equal((await createDurableServerPollRestForTests(local.dependencies)("1", accepted)).statusCode, 200) + assert.equal(local.inputs[0].identity.kind, "rest") + if (local.inputs[0].identity.kind === "rest") assert.equal(local.inputs[0].identity.authentication, "local") + assert(local.inputs[0].identity.expiresAt <= before + 180000 + 10) +}) + +test("whole host timeout/signal settles crypto gap; late continuation cannot prepare", async () => { + const f = fixture(), pending = gate(), entered = gate() + f.dependencies.authority = () => async () => { entered.resolve(); await pending.promise; return { global: true, serverIds: [1, 2], authorityProof: proof } } + const controller = new AbortController(), response = createDurableServerPollRestForTests(f.dependencies)("1", { ...context(), signal: controller.signal }) + await entered.promise; controller.abort(); const result = await response + assert.equal(result.statusCode, 409); pending.resolve(); await new Promise(done => setImmediate(done)); assert.equal(f.inputs.length, 0) + const hanging = fixture(); hanging.dependencies.authority = () => async () => new Promise(() => {}) + assert.equal((await createDurableServerPollRestForTests(hanging.dependencies)("1", { ...context(), deadlineAt: Date.now() + 30 })).statusCode, 409) +}) + +test("incomplete execution and post-publication revocation never return snapshot/private error", async () => { + const f = fixture(); f.incomplete() + const result = await createDurableServerPollRestForTests(f.dependencies)("1", context()); assert.equal(result.statusCode, 502); assert(!Object.hasOwn(result.body, "snapshot")) + const revoked = fixture(); let checks = 0 + revoked.dependencies.authority = () => async () => { if (++checks === 2) throw new McpError("PERMISSION_DENIED", "PRIVATE_PROVIDER", 403); return { global: true, serverIds: [1, 2], authorityProof: proof } } + const reply = await createDurableServerPollRestForTests(revoked.dependencies)("1", context()); assert.equal(reply.statusCode, 403); assert(!JSON.stringify(reply).includes("PRIVATE_PROVIDER")); assert(revoked.events.includes("execute")) +}) + +test("fixed request lifetime is checked again after execution before publication", async () => { + const f = fixture(), original = f.dependencies.session; let active = true + f.dependencies.session = input => { + const session = original(input) + return { ...session, execute: async (...args) => { const receipt = await session.execute(...args); active = false; return receipt } } + } + const reply = await createDurableServerPollRestForTests(f.dependencies)("1", { ...context(), assertRequestActive: () => { + if (!active) throw new McpError("READ_CANCELLED", "PRIVATE_REQUEST_STATE", 409) + } }) + assert.equal(reply.statusCode, 409); assert(!JSON.stringify(reply).includes("PRIVATE_REQUEST_STATE")); assert(!Object.hasOwn(reply.body,"serverId")) +}) diff --git a/backend/src/services/server-poll-rest.ts b/backend/src/services/server-poll-rest.ts new file mode 100644 index 00000000..1450e7dc --- /dev/null +++ b/backend/src/services/server-poll-rest.ts @@ -0,0 +1,83 @@ +import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js" +/** Fixed internal REST host. Route/auth middleware integration is separate. */ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import type { AuthUser } from "../lib/permissions.js" +import { McpError } from "../modules/mcp/security.js" +import { createAtomicStorageDurableServerPollSession as createDurableServerPollSession } from "./server-poll-durable-session.js" +import { createRestServerPollAuthority, type ServerPollRestAuthorityContext } from "./server-poll-authority.js" +import { ServerPollProblem } from "./server-poll-execution-model.js" + +export interface ServerPollRestContext extends ServerPollRestAuthorityContext { actor?: AuthUser } +type Dependencies = { session: typeof createDurableServerPollSession; authority: typeof createRestServerPollAuthority } +const snapshotSchema = z.object({ id: z.number().int().positive().safe(), serverId: z.number().int().positive().safe(), + polledAt: z.string().datetime(), status: z.enum(["online", "offline"]), latencyMs: z.number().nonnegative().nullable().optional(), + rosVersion: z.string().nullable().optional(), boardName: z.string().nullable().optional(), uptime: z.string().nullable().optional(), + cpuLoad: z.number().nullable().optional(), freeMemory: z.number().nonnegative().nullable().optional(), totalMemory: z.number().nonnegative().nullable().optional(), + identityName: z.string().nullable().optional() }).strict() +export type ServerPollRestReply = { statusCode: number; body: Record } +function failure(error: unknown): ServerPollRestReply { + const code = error instanceof McpError || error instanceof ServerPollProblem ? error.code : "STATE_UNAVAILABLE" + const errors: Record = { INVALID_INPUT: [400, "Некорректный запрос полного опроса"], NOT_FOUND: [404, "Server not found"], + PERMISSION_DENIED: [403, "Нет актуального права полного опроса"], PROTECTED_OBJECT: [409, "Полный опрос заблокирован защитой"], APPROVAL_REQUIRED: [409, "Полный опрос требует подтверждённого допуска"], + STATE_CONFLICT: [409, "Источники или допуск полного опроса изменились"], READ_CANCELLED: [409, "Полный опрос отменён или истёк"], ABORTED: [409, "Полный опрос отменён или истёк"], + MUTATION_OUTCOME_UNKNOWN: [502, "Полный опрос завершён не полностью; автоматический повтор запрещён"], VERIFICATION_FAILED: [502, "Результат полного опроса не подтверждён; автоматический повтор запрещён"] } + const [statusCode, message] = errors[code] ?? [503, "Состояние полного опроса недоступно; автоматический повтор запрещён"] + return { statusCode, body: { error: message } } +} +function host(deps: Dependencies) { + return async (rawServerId: string, rawContext: ServerPollRestContext): Promise => { + let timer: ReturnType | undefined, onAbort: (() => void) | undefined + const controller = new AbortController() + try { + const serverId = /^[1-9][0-9]*$/.test(rawServerId) ? Number(rawServerId) : NaN + if (!Number.isSafeInteger(serverId)) throw new McpError("INVALID_INPUT", "Некорректный server id", 400) + // Freeze the accepted request and JWT before the first async boundary. + const actor = rawContext.actor ? structuredClone(rawContext.actor) : undefined + if (actor) { Object.freeze(actor.apps); Object.freeze(actor.permissions); Object.freeze(actor) } + const context = { authRequired: rawContext.authRequired, acceptedAt: rawContext.acceptedAt, requestId: rawContext.requestId, + deadlineAt: rawContext.deadlineAt, signal: rawContext.signal, assertRequestActive: rawContext.assertRequestActive } + const signal = context.signal ? AbortSignal.any([context.signal, controller.signal]) : controller.signal + const originalAuthority = deps.authority(actor, { ...context, signal }) + const authorize: typeof originalAuthority = async (...args) => { + context.assertRequestActive?.() + const grant = await originalAuthority(...args) + context.assertRequestActive?.() + return grant + } + const deadlineAt = Math.min(Date.now() + SERVER_POLL_WALL_BUDGET_MS, context.acceptedAt + SERVER_POLL_WALL_BUDGET_MS, context.deadlineAt ?? Infinity, actor?.expiresAt ?? Infinity) + if (!Number.isSafeInteger(deadlineAt) || deadlineAt <= Date.now() || context.deadlineAt !== undefined && !Number.isSafeInteger(context.deadlineAt)) throw new McpError("READ_CANCELLED", "Полный опрос истёк", 409) + const request = Object.freeze({ serverId, receiptId: randomUUID() }) + const identity = Object.freeze({ kind: "rest" as const, authentication: actor ? "jwt" as const : "local" as const, + ownerId: actor?.id ?? "local-operator", executionId: randomUUID(), requestId: context.requestId, + issuedAt: actor?.issuedAt ?? context.acceptedAt, expiresAt: deadlineAt }) + const stopped = new Promise((_yes, reject) => { onAbort = () => { controller.abort(); reject(new McpError("READ_CANCELLED", "Полный опрос отменён или истёк", 409)) } }) + timer = setTimeout(onAbort!, Math.max(1, deadlineAt - Date.now())) + context.signal?.addEventListener("abort", onAbort!, { once: true }) + const work = Promise.resolve().then(async () => { + signal.throwIfAborted() + // Metadata-only authorization and 404 precede construction of a session + // capable of reading private endpoints or publishing any durable effect. + await authorize(request, [serverId], signal); signal.throwIfAborted() + const session = deps.session({ request, identity, authorize }) + const plan = await session.prepare(signal); signal.throwIfAborted() + const receipt = await session.execute(plan, signal); signal.throwIfAborted() + if (!receipt.complete || !receipt.maintenanceComplete || !receipt.configurationVerified) throw new McpError("VERIFICATION_FAILED", "Неполный poll receipt", 502) + await authorize(request, plan.affectedServerIds, signal); signal.throwIfAborted() + const snapshot = snapshotSchema.parse(receipt.snapshot) + if (snapshot.serverId !== serverId) throw new McpError("VERIFICATION_FAILED", "Poll snapshot identity drift", 502) + // Exact legacy SnapshotRead shape, including nullable offline fields. + return { statusCode: 200, body: { id: snapshot.id, serverId: snapshot.serverId, polledAt: snapshot.polledAt, status: snapshot.status, + latencyMs: snapshot.latencyMs ?? null, rosVersion: snapshot.rosVersion ?? null, boardName: snapshot.boardName ?? null, uptime: snapshot.uptime ?? null, + cpuLoad: snapshot.cpuLoad ?? null, freeMemory: snapshot.freeMemory ?? null, totalMemory: snapshot.totalMemory ?? null, identityName: snapshot.identityName ?? null } } + }) + if (context.signal?.aborted) onAbort!() + return await Promise.race([work, stopped]) + } catch (error) { return failure(error) } + finally { clearTimeout(timer); if (onAbort) rawContext.signal?.removeEventListener("abort", onAbort); controller.abort() } + } +} + +export const executeDurableServerPollRest = host({ session: createDurableServerPollSession, authority: createRestServerPollAuthority }) +/** Explicit unit-only trusted composition; never populated from request JSON. */ +export const createDurableServerPollRestForTests = (dependencies: Dependencies) => host(dependencies) diff --git a/backend/src/services/server-poll-retired-archive.test.ts b/backend/src/services/server-poll-retired-archive.test.ts new file mode 100644 index 00000000..b58124ff --- /dev/null +++ b/backend/src/services/server-poll-retired-archive.test.ts @@ -0,0 +1,159 @@ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHmac, randomBytes } from "node:crypto" +import { mkdtemp, rmdir, unlink, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { test } from "node:test" +import { fingerprint } from "../modules/mcp/security.js" +import { createServerPollRetiredArchiveChargeVerifier, createServerPollRetiredArchiveForTests } from "./server-poll-retired-archive.js" +import { createServerPollTerminalArchiveForTests, ServerPollArchiveRefused } from "./server-poll-terminal-archive.js" +import type { VerifiedServerPollHistory } from "./server-poll-history-verification.js" + +const receiptId = "11111111-1111-4111-8111-111111111111" +const executionId = "22222222-2222-4222-8222-222222222222" +const connectionId = "33333333-3333-4333-8333-333333333333" +const planId = "44444444-4444-4444-8444-444444444444" +const ownerId = "owned-retired-archive" +const secret = "ORIGINAL-CREDENTIAL-OR-PAYLOAD-MUST-DISAPPEAR" +const expiredAt = "2026-10-29T03:20:00.123456Z" +const terminalAt = "2026-10-29T03:21:00.123457Z" +const completedAt = "2026-10-29T03:18:00.123458Z" +const createdAt = "2026-10-29T03:17:00.123459Z" +const gcNotBefore = "2026-11-29T03:20:00.123460Z" +const now = Date.parse("2026-11-30T00:00:00Z") +const key = randomBytes(32), mac = randomBytes(32) +const proof = async (purpose: string, value: unknown) => createHmac("sha256", mac).update(purpose).update(fingerprint(value)).digest("hex") + +test("retired archive accounting derives the existing key anew for each scan and cannot bootstrap it", async () => { + const directory = await mkdtemp(join(tmpdir(), "mcp-retired-archive-key-")) + const filename = join(directory, "encryption-key"), previous = process.env.MCP_STATE_DIR + try { + process.env.MCP_STATE_DIR = directory + await assert.rejects(createServerPollRetiredArchiveChargeVerifier(), { code: "STATE_UNAVAILABLE" }) + await writeFile(filename, key, { flag: "wx" }) + const verifier = await createServerPollRetiredArchiveChargeVerifier() + const metadata = { + version: 2, receipt_id: receiptId, execution_id: executionId, connection_id: connectionId, owner_id: ownerId, + binding_proof: "a".repeat(64), context_proof: "b".repeat(64), terminal_digest: "c".repeat(64), + original_activity_at: terminalAt, original_expires_at: expiredAt, retention_days: 30, safety_ms: 60000, + source_digest: "d".repeat(64), source_sha256: "e".repeat(64), decoded_bytes: 10, compressed_bytes: 20, + ciphertext_bytes: 64, ciphertext_sha256: "f".repeat(64), + } + const purposeKey = createHmac("sha256", key).update("MCP private fingerprint:server-poll-retired-archive-record-v2").digest() + const archiveProof = createHmac("sha256", purposeKey).update(fingerprint({ + ...metadata, identity_kind: "mcp", storage_state: "compacted", admission_bytes: 32768, charged_rows: 2, + })).digest("hex") + const row = { ...metadata, archive_proof: archiveProof } + assert.equal((await verifier(row)).bytes, 32832) + await assert.rejects(verifier({ ...row, ciphertext_sha256: "0".repeat(64) }), ServerPollArchiveRefused) + await writeFile(filename, randomBytes(32)) + await assert.rejects((await createServerPollRetiredArchiveChargeVerifier())(row), ServerPollArchiveRefused) + } finally { + if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous + await unlink(filename).catch(error => { if (error.code !== "ENOENT") throw error }) + await rmdir(directory) + } +}) +const encrypt = async (plain: Buffer) => { + const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv) + const ciphertext = Buffer.concat([cipher.update(plain), cipher.final()]) + return Buffer.concat([iv, cipher.getAuthTag(), ciphertext]) +} +const decrypt = async (bytes: Buffer) => { + const cipher = createDecipheriv("aes-256-gcm", key, bytes.subarray(0, 12)) + cipher.setAuthTag(bytes.subarray(12, 28)) + return Buffer.concat([cipher.update(bytes.subarray(28)), cipher.final()]) +} +async function fixture(v2Encrypt = encrypt) { + const admission = { storage_state: "full", gc_not_before: gcNotBefore, opaque: secret } + const terminal = { operation: { finished_at: terminalAt, result: secret }, plan: { id: planId, expires_at: expiredAt, evidence: secret }, + step: { intended_state: secret, result: secret } } + const journal = { execution: { created_at: createdAt, context_ciphertext: secret }, receipt: { created_at: completedAt, receipt_ciphertext: secret }, + intents: [{ opaque: randomBytes(2048).toString("hex") }], acks: [{ opaque: secret }], quarantines: [] } + // Tests use genuine V1 codec authentication with a controlled source callback. + // Full production V1 source parity is tested by its existing verifier suite. + const v1 = createServerPollTerminalArchiveForTests({ encrypt, decrypt, proof, verifySource: async () => ({ + receiptId, executionId, connectionId, ownerId, bindingProof: "a".repeat(64), contextProof: "b".repeat(64), terminalDigest: "c".repeat(64), + originalActivityAt: terminalAt, originalExpiresAt: expiredAt, retentionDays: 30, safetyMs: 60000, + }) }) + const archive = await v1.create(JSON.stringify({ admission, terminal, journal }), now) + assert(archive) + const history = { + context: { identity: { kind: "mcp", executionId, connectionId, ownerId, credentialId: secret }, + plan: { request: { receiptId, serverId: 23 }, affectedServerIds: [12, 23], maintenance: [secret] } }, + receipt: { receiptId, complete: true, maintenanceComplete: true, configurationVerified: true, observation: "online", + nativeReads: 4, confirmedPublications: 8, completedPublications: 8, snapshot: { identityName: secret } }, + source: { createdAt, completedAt, stageCount: 12, publications: 8, + nativeOutcomes: Array.from({ length: 4 }, (_, index) => ({ stage: "native." + index, outcome: "confirmed", resultProof: "d".repeat(64) })) }, + } as unknown as VerifiedServerPollHistory + const calls = { decrypt: 0, history: 0 } + const codec = createServerPollRetiredArchiveForTests({ openV1: v1.open, verifyHistory: async () => { calls.history++; return history }, + encrypt: v2Encrypt, proof, decrypt: async bytes => { calls.decrypt++; return decrypt(bytes) } }) + return { codec, history, calls, input: { archive, terminal, admission: { ...admission, storage_state: "compacted", + archive_bytes: archive.ciphertext_bytes, original_activity_at: terminalAt, compact_at: "2026-10-29T03:23:00.123456Z" } } } +} +const refusal = (work: Promise) => assert.rejects(work, (error: unknown) => { + assert(error instanceof ServerPollArchiveRefused && error.automaticReplay === false) + assert(!String(error).includes(secret)) + return true +}) + +test("retirement authenticates V1 and projects only completed history, scope, and original lifetime", async () => { + const { codec, calls, input } = await fixture() + const retired = await codec.project(input, now) + assert(retired) + assert.equal(calls.history, 1) + assert(retired.ciphertext_bytes < input.archive.ciphertext_bytes) + const { history, metadata } = await codec.open(retired) + assert.deepEqual(history, { version: 2, operation: "servers.poll", operationVersion: 1, planId, serverId: 23, affectedServerIds: [12, 23], + gcNotBefore, terminalAt, previous: { archiveProof: input.archive.archive_proof, sourceDigest: input.archive.source_digest, sourceSha256: input.archive.source_sha256 }, + history: { createdAt, completedAt, stageCount: 12, publications: 8, nativeReads: 4, observation: "online", verification: "passed" } }) + assert.equal(metadata.terminal_digest, input.archive.terminal_digest) + assert.equal(metadata.original_activity_at, terminalAt) + assert.equal(metadata.original_expires_at, expiredAt) + assert.equal(metadata.retention_days, input.archive.retention_days) + assert(!JSON.stringify(history).includes(secret)) + assert.equal((await codec.verifyCharge(retired)).bytes, retired.ciphertext_bytes + 32768) +}) + +test("retirement refuses unauthenticated V1 and any current admission or original terminal drift", async () => { + const { codec, input, calls } = await fixture() + const changed = [ + { ...input, archive: { ...input.archive, archive_proof: "e".repeat(64) } }, + { ...input, admission: { ...input.admission, gc_not_before: terminalAt } }, + { ...input, admission: { ...input.admission, original_activity_at: "2026-10-29T03:21:00.123458Z" } }, + { ...input, admission: { ...input.admission, archive_bytes: 1 } }, + { ...input, terminal: { ...input.terminal, step: { result: "changed" } } }, + ] + for (const row of changed) await refusal(codec.project(row, now)) + assert.equal(calls.history, 0) +}) + +test("V2 metadata or ciphertext tamper never reaches V2 decrypt", async () => { + const { codec, input, calls } = await fixture() + const retired = await codec.project(input, now) + assert(retired) + for (const row of [ + { ...retired, version: 1 }, { ...retired, owner_id: "another-owner" }, + { ...retired, original_expires_at: "2026-10-29T03:20:00.123457Z" }, + { ...retired, terminal_digest: "e".repeat(64) }, + { ...retired, ciphertext: (retired.ciphertext[0] === "A" ? "B" : "A") + retired.ciphertext.slice(1) }, + ]) await refusal(codec.open(row)) + assert.equal(calls.decrypt, 0) + await refusal(codec.verifyCharge(input.archive)) + await refusal(codec.verifyCharge({ ...retired, ciphertext_bytes: retired.ciphertext_bytes - 1 })) +}) + +test("retirement refuses unknown history or a scope that omits its original target", async () => { + const { codec, history, input } = await fixture() + history.source.nativeOutcomes[0].outcome = "read-failed" + await refusal(codec.project(input, now)) + history.source.nativeOutcomes[0].outcome = "confirmed" + history.context.plan.affectedServerIds = [12] + await refusal(codec.project(input, now)) +}) + +test("a larger V2 ciphertext keeps the authenticated V1 instead of increasing the charge", async () => { + const { codec, input } = await fixture(async value => encrypt(Buffer.concat([Buffer.alloc(4096), value]))) + assert.equal(await codec.project(input, now), undefined) +}) diff --git a/backend/src/services/server-poll-retired-archive.ts b/backend/src/services/server-poll-retired-archive.ts new file mode 100644 index 00000000..9fe0dec6 --- /dev/null +++ b/backend/src/services/server-poll-retired-archive.ts @@ -0,0 +1,176 @@ +/** Private retirement projection. Publication still requires the SQL owner's + * locks, exact compare-and-swap, dependency checks and atomic quota adjustment. */ +import { createHash, timingSafeEqual } from "node:crypto" +import { gzipSync, gunzipSync } from "node:zlib" +import { z } from "zod" +import { canonicalPollArchiveTimestamp, openServerPollTerminalArchive, ServerPollArchiveRefused } from "./server-poll-terminal-archive.js" +import { verifyServerPollHistory } from "./server-poll-history-verification.js" +import { decryptBinary, encryptBinary, fingerprint, privateFingerprinterExisting, privateSecretFingerprint } from "../modules/mcp/security.js" + +const MAX_PAYLOAD_BYTES = 24576 +const MAX_COMPRESSED_BYTES = MAX_PAYLOAD_BYTES +const MAX_CIPHERTEXT_BYTES = 33000 +const RECORD_PURPOSE = "server-poll-retired-archive-record-v2" +const SOURCE_PURPOSE = "server-poll-retired-archive-source-v2" +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +const integer = z.number().int().positive().safe() +const owner = z.string().min(1).max(200).refine(value => value.trim() === value && !/[\x00-\x1f\x7f]/.test(value)) +const utc = z.string().transform(canonicalPollArchiveTimestamp) +const metadataSchema = z.object({ + version: z.literal(2), receipt_id: uuid, execution_id: uuid, connection_id: uuid, owner_id: owner, + binding_proof: hash, context_proof: hash, terminal_digest: hash, + original_activity_at: utc, original_expires_at: utc, retention_days: integer.max(365), safety_ms: z.literal(60000), + source_digest: hash, source_sha256: hash, decoded_bytes: integer.max(MAX_PAYLOAD_BYTES), + compressed_bytes: integer.max(MAX_COMPRESSED_BYTES), ciphertext_bytes: integer.max(MAX_CIPHERTEXT_BYTES), ciphertext_sha256: hash, +}) +const projectionSchema = z.object({ + version: z.literal(2), operation: z.literal("servers.poll"), operationVersion: z.literal(1), + planId: uuid, serverId: integer, affectedServerIds: z.array(integer).min(1).max(1000), + gcNotBefore: utc, terminalAt: utc, + previous: z.object({ archiveProof: hash, sourceDigest: hash, sourceSha256: hash }).strict(), + history: z.object({ + createdAt: utc, completedAt: utc, stageCount: integer.max(2008), + publications: integer.max(2008), nativeReads: z.literal(4), + observation: z.literal("online"), verification: z.literal("passed"), + }).strict(), +}).strict().refine(value => value.affectedServerIds.includes(value.serverId) + && value.affectedServerIds.length === new Set(value.affectedServerIds).size + && value.history.createdAt <= value.history.completedAt + && value.history.completedAt <= value.terminalAt) + +export type ServerPollRetiredArchiveMetadata = z.infer +export type ServerPollRetiredHistory = z.infer +export type ServerPollRetiredArchive = ServerPollRetiredArchiveMetadata & { ciphertext: string; archive_proof: string } + +export interface LockedServerPollRetirementSource { + /** Existing V1 reader projection, including retention and actual ciphertext digest. */ + archive: Record + /** Current compacted admission JSON and exact operation, plan and step JSON. */ + admission: Record + terminal: { operation: Record; plan: Record; step: Record } +} + +interface Dependencies { + openV1: typeof openServerPollTerminalArchive + verifyHistory: typeof verifyServerPollHistory + encrypt(value: Buffer): Promise + decrypt(value: Buffer): Promise + proof(purpose: string, value: unknown): Promise +} +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +const equal = (left: string, right: string) => timingSafeEqual(Buffer.from(hash.parse(left), "hex"), Buffer.from(hash.parse(right), "hex")) +const refuse = (): never => { throw new ServerPollArchiveRefused() } +const requireThat: (value: unknown) => asserts value = value => { if (!value) refuse() } + +function immutableAdmission(raw: Record) { + const result = { ...raw } + for (const name of ["storage_state", "compact_at", "archive_bytes", "original_activity_at"]) delete result[name] + return result +} + +function codec(deps: Dependencies) { + const proof = async (purpose: string, value: unknown) => hash.parse(await deps.proof(purpose, value)) + const recordProof = (metadata: ServerPollRetiredArchiveMetadata) => proof(RECORD_PURPOSE, { + ...metadata, identity_kind: "mcp", storage_state: "compacted", admission_bytes: 32768, charged_rows: 2, + }) + async function readMetadata(raw: Record) { + const metadata = metadataSchema.parse(raw) + requireThat(equal(hash.parse(raw.archive_proof), await recordProof(metadata))) + return metadata + } + + async function project(raw: LockedServerPollRetirementSource, now: number): Promise { + try { + requireThat(Number.isSafeInteger(now) && now > 0) + const input = structuredClone(raw) + const opened = await deps.openV1(input.archive, now) + const { source, metadata: previous } = opened + const current = input.admission + requireThat(source.admission.storage_state === "full" && current.storage_state === "compacted" + && current.archive_bytes === previous.ciphertext_bytes + && canonicalPollArchiveTimestamp(current.original_activity_at) === previous.original_activity_at + && fingerprint(immutableAdmission(source.admission)) === fingerprint(immutableAdmission(current)) + && fingerprint(source.terminal) === fingerprint(input.terminal)) + canonicalPollArchiveTimestamp(current.compact_at) + + // The existing codec authenticates the entire original. The shared + // verifier supplies its typed history rather than reading opaque fields + // from unverified context or preserving original authority in V2. + const { context, receipt, source: journal } = await deps.verifyHistory(source.journal) + requireThat(context.identity.kind === "mcp" && context.identity.executionId === previous.execution_id + && context.identity.connectionId === previous.connection_id && context.identity.ownerId === previous.owner_id + && context.plan.request.receiptId === previous.receipt_id && receipt.receiptId === previous.receipt_id + && receipt.complete && receipt.maintenanceComplete && receipt.configurationVerified && receipt.observation === "online" + && receipt.confirmedPublications === receipt.completedPublications && journal.publications === receipt.completedPublications + && journal.nativeOutcomes.length === 4 && journal.nativeOutcomes.every(item => item.outcome === "confirmed")) + const terminalAt = [source.terminal.operation.finished_at, source.terminal.plan.expires_at].map(canonicalPollArchiveTimestamp).sort()[1] + const projection = projectionSchema.parse({ + version: 2, operation: "servers.poll", operationVersion: 1, planId: source.terminal.plan.id, + serverId: context.plan.request.serverId, affectedServerIds: context.plan.affectedServerIds, + gcNotBefore: source.admission.gc_not_before, terminalAt, + previous: { archiveProof: input.archive.archive_proof, sourceDigest: previous.source_digest, sourceSha256: previous.source_sha256 }, + history: { createdAt: source.journal.execution.created_at, completedAt: source.journal.receipt.created_at, + stageCount: journal.stageCount, publications: journal.publications, nativeReads: receipt.nativeReads, + observation: receipt.observation, verification: "passed" }, + }) + requireThat(projection.terminalAt <= previous.original_activity_at && previous.original_expires_at <= projection.gcNotBefore) + const plaintext = Buffer.from(JSON.stringify(projection)) + requireThat(plaintext.length <= MAX_PAYLOAD_BYTES) + const compressed = gzipSync(plaintext, { level: 6, maxOutputLength: MAX_COMPRESSED_BYTES }) + const ciphertext = (await deps.encrypt(compressed)).toString("base64") + const bytes = Buffer.byteLength(ciphertext) + if (bytes > previous.ciphertext_bytes || bytes > MAX_CIPHERTEXT_BYTES) return undefined + const sourceSha = sha(plaintext) + const metadata = metadataSchema.parse({ ...previous, version: 2, source_sha256: sourceSha, source_digest: await proof(SOURCE_PURPOSE, sourceSha), + decoded_bytes: plaintext.length, compressed_bytes: compressed.length, ciphertext_bytes: bytes, ciphertext_sha256: sha(ciphertext) }) + return { ...metadata, ciphertext, archive_proof: await recordProof(metadata) } + } catch { return refuse() } + } + + async function verifyCharge(raw: Record) { + try { + const metadata = await readMetadata(structuredClone(raw)) + return { bytes: metadata.ciphertext_bytes + 32768, rows: 2 as const, metadata } + } catch { return refuse() } + } + + async function open(raw: Record) { + try { + const input = structuredClone(raw) + const metadata = await readMetadata(input), ciphertext = input.ciphertext + requireThat(typeof ciphertext === "string" && Buffer.byteLength(ciphertext) === metadata.ciphertext_bytes + && sha(ciphertext) === metadata.ciphertext_sha256) + const binary = Buffer.from(ciphertext, "base64") + requireThat(binary.toString("base64") === ciphertext) + const compressed = await deps.decrypt(binary) + requireThat(compressed.length === metadata.compressed_bytes) + const plaintext = gunzipSync(compressed, { maxOutputLength: MAX_PAYLOAD_BYTES }) + const text = plaintext.toString("utf8") + requireThat(plaintext.length === metadata.decoded_bytes && Buffer.from(text).equals(plaintext) + && sha(plaintext) === metadata.source_sha256 && equal(metadata.source_digest, await proof(SOURCE_PURPOSE, metadata.source_sha256))) + const history = projectionSchema.parse(JSON.parse(text)) + requireThat(history.terminalAt <= metadata.original_activity_at && metadata.original_expires_at <= history.gcNotBefore) + return { metadata, history } + } catch { return refuse() } + } + return Object.freeze({ project, verifyCharge, open }) +} + +const productionDependencies: Dependencies = { + openV1: openServerPollTerminalArchive, verifyHistory: verifyServerPollHistory, + encrypt: encryptBinary, decrypt: decryptBinary, + proof: (purpose, value) => privateSecretFingerprint(fingerprint(value), purpose), +} +const production = codec(productionDependencies) +export const projectServerPollRetiredArchive = production.project +export const openServerPollRetiredArchive = production.open +/** One accounting pass reuses the existing record-purpose signer. */ +export async function createServerPollRetiredArchiveChargeVerifier() { + const signer = await privateFingerprinterExisting(RECORD_PURPOSE) + return codec({ ...productionDependencies, proof: async (purpose, value) => { + if (purpose !== RECORD_PURPOSE) return refuse() + return signer(fingerprint(value)) + } }).verifyCharge +} +/** Isolated component crypto composition. No public/runtime caller selects it. */ +export const createServerPollRetiredArchiveForTests = (deps: Dependencies) => codec(Object.freeze({ ...deps })) diff --git a/backend/src/services/server-poll-settings-epoch.sql-helper.ts b/backend/src/services/server-poll-settings-epoch.sql-helper.ts new file mode 100644 index 00000000..95b6c6a3 --- /dev/null +++ b/backend/src/services/server-poll-settings-epoch.sql-helper.ts @@ -0,0 +1,146 @@ +/** Own-cluster-only acceptance of the epoch guard. Proposal mode applies DDL + * transactionally; numbered mode preserves its existing registered schema. + * All synthetic changes roll back; no service, router or scheduler starts. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import { basename, dirname, resolve } from "node:path" +import type { Pool } from "pg" +import { assertServerPollSettingsEpochCatalog, SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL } from "./server-poll-settings-epoch.js" + +export async function acceptServerPollSettingsEpochSql(pool: Pool, numbered = false) { + assert.equal(process.env.MMAPP_OWN_SERVER_POLL_SETTINGS_EPOCH, "true") + assert.equal(process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH, "true") + assert.equal(process.env.NODE_ENV, "test") + const endpoint = new URL(process.env.DATABASE_URL ?? "") + assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.pathname, "/postgres") + assert.equal(endpoint.username, "mcp_test") + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const tables = ["traffic_settings", "uptime_settings", "internet_path_settings"] as const + const snapshots = async () => Promise.all(tables.map(async table => + (await pool.query(`SELECT to_jsonb(t) AS row FROM public.${table} t ORDER BY id`)).rows)) + const before = await snapshots(), owner = await pool.connect(), groups: string[] = [] + try { + await owner.query("BEGIN") + for (const table of tables) await owner.query(`INSERT INTO public.${table}(id) VALUES(1) ON CONFLICT(id) DO NOTHING`) + const existing: Record[] = [] + for (const table of tables) existing.push((await owner.query(`SELECT to_jsonb(t) AS row FROM public.${table} t WHERE id=1`)).rows[0].row) + if (!numbered) await owner.query(await readFile(new URL("../db/proposals/server-poll-settings-epoch.sql", import.meta.url), "utf8")) + else assertServerPollSettingsEpochCatalog((await owner.query(SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL, [tables])).rows) + const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ + const row = async (table: typeof tables[number]) => + (await owner.query(`SELECT to_jsonb(t) AS row FROM public.${table} t WHERE id=1`)).rows[0].row as Record + for (const [index, table] of tables.entries()) { + let current = await row(table), epoch = String(current.poll_configuration_epoch) + assert.match(epoch, uuid) + const { poll_configuration_epoch: _epoch, ...preserved } = current + const expected = { ...existing[index] }; if (numbered) delete expected.poll_configuration_epoch + assert.deepEqual(preserved, expected) + assert.equal(_epoch, epoch) + groups.push(table + ":backfill-preserves-complete-row") + + await owner.query(`UPDATE public.${table} SET last_collected_at=clock_timestamp(),last_duration_ms=17, + last_error='synthetic runtime',updated_at=clock_timestamp(),poll_configuration_epoch=NULL WHERE id=1`) + current = await row(table); assert.equal(current.poll_configuration_epoch, epoch) + assert.equal(current.last_error, "synthetic runtime"); assert.equal(current.last_duration_ms, 17) + await owner.query(`UPDATE public.${table} SET last_error=NULL,last_duration_ms=NULL,poll_configuration_epoch=$1 WHERE id=1`, [randomUUID()]) + assert.equal((await row(table)).poll_configuration_epoch, epoch) + groups.push(table + ":runtime-and-caller-epoch-cannot-change-identity") + + const originalInterval = current.interval_sec + await owner.query(`UPDATE public.${table} SET interval_sec=interval_sec+1,poll_configuration_epoch=$1 WHERE id=1`, [epoch]) + current = await row(table); assert.notEqual(current.poll_configuration_epoch, epoch) + const middle = current.poll_configuration_epoch + await owner.query(`UPDATE public.${table} SET interval_sec=$1,poll_configuration_epoch=$2 WHERE id=1`, [originalInterval, epoch]) + current = await row(table) + assert.equal(current.interval_sec, originalInterval) + assert.notEqual(current.poll_configuration_epoch, epoch); assert.notEqual(current.poll_configuration_epoch, middle) + epoch = String(current.poll_configuration_epoch) + groups.push(table + ":configuration-ABA-even-with-forged-old-epoch") + + await owner.query(`ALTER TABLE public.${table} ADD COLUMN future_configuration jsonb`) + await owner.query(`UPDATE public.${table} SET future_configuration='{"nested":[1,true]}'::jsonb WHERE id=1`) + assert.notEqual((await row(table)).poll_configuration_epoch, epoch) + epoch = String((await row(table)).poll_configuration_epoch) + await owner.query(`UPDATE public.${table} SET created_at=created_at+interval '1 second' WHERE id=1`) + assert.notEqual((await row(table)).poll_configuration_epoch, epoch) + epoch = String((await row(table)).poll_configuration_epoch) + groups.push(table + ":unknown-fields-and-created-at-remain-configuration") + + await owner.query("SAVEPOINT epoch_rollback") + await owner.query(`UPDATE public.${table} SET enabled=NOT enabled WHERE id=1`) + assert.notEqual((await row(table)).poll_configuration_epoch, epoch) + await owner.query("ROLLBACK TO SAVEPOINT epoch_rollback") + assert.equal((await row(table)).poll_configuration_epoch, epoch) + await owner.query("RELEASE SAVEPOINT epoch_rollback") + groups.push(table + ":rolled-back-configuration-preserves-identity") + + await owner.query("SET LOCAL session_replication_role=replica") + await owner.query(`UPDATE public.${table} SET enabled=NOT enabled,poll_configuration_epoch=$1 WHERE id=1`, [epoch]) + assert.notEqual((await row(table)).poll_configuration_epoch, epoch) + await owner.query("SET LOCAL session_replication_role=origin") + epoch = String((await row(table)).poll_configuration_epoch) + groups.push(table + ":always-trigger-enforced-in-replica-mode") + + await owner.query(`DELETE FROM public.${table} WHERE id=1`) + await owner.query(`INSERT INTO public.${table}(id,poll_configuration_epoch) VALUES(1,$1)`, [epoch]) + assert.notEqual((await row(table)).poll_configuration_epoch, epoch) + epoch = String((await row(table)).poll_configuration_epoch) + await owner.query(`UPDATE public.${table} SET enabled=enabled,poll_configuration_epoch=$1 WHERE id=1`, [randomUUID()]) + assert.equal((await row(table)).poll_configuration_epoch, epoch) + groups.push(table + ":delete-reinsert-and-noop-forgery") + } + const inventory = async () => (await owner.query(SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL, [tables])).rows + assertServerPollSettingsEpochCatalog(await inventory()) + const refused = async (name: string, sql: string) => { + await owner.query("SAVEPOINT epoch_tamper") + try { + await owner.query(sql) + assert.throws(() => assertServerPollSettingsEpochCatalog([])) + const observed = await inventory() + assert.throws(() => assertServerPollSettingsEpochCatalog(observed), + (error: unknown) => error instanceof Error && "code" in error && error.code === "STATE_UNAVAILABLE") + } finally { await owner.query("ROLLBACK TO SAVEPOINT epoch_tamper"); await owner.query("RELEASE SAVEPOINT epoch_tamper") } + assertServerPollSettingsEpochCatalog(await inventory()) + groups.push(name) + } + await refused("missing-guard-refused", "DROP TRIGGER server_poll_settings_epoch ON public.traffic_settings") + await refused("disabled-guard-refused", "ALTER TABLE public.uptime_settings DISABLE TRIGGER server_poll_settings_epoch") + await refused("ordinary-replica-bypass-guard-refused", "ALTER TABLE public.internet_path_settings ENABLE TRIGGER server_poll_settings_epoch") + await refused("extra-row-trigger-refused", `CREATE TRIGGER zz_extra_epoch BEFORE UPDATE ON public.traffic_settings + FOR EACH ROW EXECUTE FUNCTION public.server_poll_settings_epoch_guard()`) + await refused("forged-function-source-refused", `CREATE OR REPLACE FUNCTION public.server_poll_settings_epoch_guard() + RETURNS trigger LANGUAGE plpgsql SET search_path=pg_catalog AS 'BEGIN RETURN NEW; END;'`) + await refused("changed-function-search-path-refused", "ALTER FUNCTION public.server_poll_settings_epoch_guard() SET search_path=public") + await refused("missing-epoch-column-refused", "ALTER TABLE public.traffic_settings RENAME COLUMN poll_configuration_epoch TO unavailable_epoch") + await refused("nullable-epoch-refused", "ALTER TABLE public.uptime_settings ALTER COLUMN poll_configuration_epoch DROP NOT NULL") + await refused("changed-epoch-default-refused", "ALTER TABLE public.internet_path_settings ALTER COLUMN poll_configuration_epoch DROP DEFAULT") + const triggers = (await owner.query(`SELECT c.relname,t.tgenabled,t.tgtype,t.tgnargs,t.tgqual, + pn.nspname AS function_schema,p.proname,p.prosecdef,p.proconfig + FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid=t.tgrelid + JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace JOIN pg_catalog.pg_proc p ON p.oid=t.tgfoid + JOIN pg_catalog.pg_namespace pn ON pn.oid=p.pronamespace + WHERE n.nspname='public' AND c.relname=ANY($1::text[]) AND t.tgname='server_poll_settings_epoch' + ORDER BY c.relname`, [tables])).rows + assert.equal(triggers.length, 3) + for (const trigger of triggers) { + assert.equal(trigger.tgenabled, "A"); assert.equal(trigger.tgtype, 23) + assert.equal(trigger.tgnargs, 0); assert.equal(trigger.tgqual, null) + assert.equal(trigger.function_schema, "public"); assert.equal(trigger.proname, "server_poll_settings_epoch_guard") + assert.equal(trigger.prosecdef, false); assert.deepEqual(trigger.proconfig, ["search_path=pg_catalog"]) + } + groups.push("exact-always-row-before-insert-update-metadata") + await owner.query("ROLLBACK") + } finally { + try { await owner.query("ROLLBACK") } finally { owner.release(true) } + } + assert.deepEqual(await snapshots(), before) + if (!numbered) { + assert.equal((await pool.query("SELECT to_regprocedure('public.server_poll_settings_epoch_guard()') IS NULL AS absent")).rows[0].absent, true) + assert.equal((await pool.query(`SELECT count(*)::int AS n FROM information_schema.columns + WHERE table_schema='public' AND table_name=ANY($1::text[]) AND column_name='poll_configuration_epoch'`, [tables])).rows[0].n, 0) + } else assertServerPollSettingsEpochCatalog((await pool.query(SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL, [tables])).rows) + groups.push(numbered ? "all-synthetic-data-and-tampering-rolled-back-numbered-guards-restored" : "all-proposal-ddl-and-synthetic-data-rolled-back") + console.log(numbered ? "Owned poll settings epoch numbered:" : "Owned poll settings epoch proposal:", JSON.stringify({ groups, groupCount: groups.length, + numberedMigration: numbered, productionActivated: false, approvalRuntimeWired: false, fullExecutionProofUnchanged: true })) +} diff --git a/backend/src/services/server-poll-settings-epoch.ts b/backend/src/services/server-poll-settings-epoch.ts new file mode 100644 index 00000000..d9ad29bb --- /dev/null +++ b/backend/src/services/server-poll-settings-epoch.ts @@ -0,0 +1,53 @@ +import { pollProblem } from "./server-poll-execution-model.js" + +export const SERVER_POLL_SETTINGS_TABLES = ["traffic_settings", "uptime_settings", "internet_path_settings"] as const +/** Exact UTF-8 function body, including whitespace. This is a reviewed source + * pin, not a digest learned from the live database. No public adapter yet uses + * this candidate: catalog validation alone is not an authority issuer. */ +export const SERVER_POLL_SETTINGS_EPOCH_BODY_SHA256 = "3d62b03c1753acdaf91d9de306527c2065b17c606d311aa8502271ea6654741f" +export const SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL = `/* poll-settings:epoch-inventory */ +SELECT c.relname AS table_name,c.relkind,a.attnotnull AS not_null, + a.atttypid='pg_catalog.uuid'::pg_catalog.regtype AS uuid_type, + a.attgenerated::text AS generated,a.attidentity::text AS identity, + pg_catalog.pg_get_expr(d.adbin,d.adrelid) AS default_expression, + t.tgname AS trigger_name,t.tgenabled AS enabled,t.tgtype::int AS trigger_type, + t.tgnargs::int AS argument_count,t.tgattr::text AS attribute_numbers, + t.tgqual IS NULL AS unconditional,t.tgconstraint::text AS constraint_oid, + t.tgoldtable IS NULL AND t.tgnewtable IS NULL AS no_transition_tables, + pn.nspname AS function_schema,p.proname AS function_name,l.lanname AS language, + p.prosecdef AS security_definer,p.proleakproof AS leakproof,p.provolatile AS volatility, + p.prokind AS function_kind,p.pronargs::int AS function_arguments, + p.prorettype='pg_catalog.trigger'::pg_catalog.regtype AS trigger_return,p.proconfig AS configuration, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(p.prosrc,'UTF8')),'hex') AS body_sha256 +FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace +LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attname='poll_configuration_epoch' AND NOT a.attisdropped +LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=c.oid AND d.adnum=a.attnum +-- All non-internal triggers, so an additional trigger cannot silently override +-- this epoch later in the same statement. +LEFT JOIN pg_catalog.pg_trigger t ON t.tgrelid=c.oid AND NOT t.tgisinternal +LEFT JOIN pg_catalog.pg_proc p ON p.oid=t.tgfoid +LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid=p.pronamespace +LEFT JOIN pg_catalog.pg_language l ON l.oid=p.prolang +WHERE n.nspname='public' AND c.relname=ANY($1::text[]) ORDER BY c.relname,t.tgname LIMIT 5` + +/** The fixed trusted SQL reader must supply this observation in the same + * protected context as the settings rows. A caller-supplied observation is + * never sufficient to issue an approval proof. Missing/extra guards refuse. */ +export function assertServerPollSettingsEpochCatalog(rows: Record[]): void { + const fail = (): never => pollProblem("STATE_UNAVAILABLE", "Не подтверждена защита версии настроек опроса") + if (!Array.isArray(rows) || rows.length !== 3 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row)) + || new Set(rows.map(row => row.table_name)).size !== 3) fail() + for (const row of rows) { + if (!SERVER_POLL_SETTINGS_TABLES.some(table => table === row.table_name) + || row.relkind !== "r" || row.not_null !== true || row.uuid_type !== true + || row.generated !== "" || row.identity !== "" || row.default_expression !== "gen_random_uuid()" + || row.trigger_name !== "server_poll_settings_epoch" || row.enabled !== "A" || row.trigger_type !== 23 + || row.argument_count !== 0 || row.attribute_numbers !== "" || row.unconditional !== true + || row.constraint_oid !== "0" || row.no_transition_tables !== true + || row.function_schema !== "public" || row.function_name !== "server_poll_settings_epoch_guard" + || row.language !== "plpgsql" || row.security_definer !== false || row.leakproof !== false + || row.volatility !== "v" || row.function_kind !== "f" || row.function_arguments !== 0 + || row.trigger_return !== true || JSON.stringify(row.configuration) !== '["search_path=pg_catalog"]' + || row.body_sha256 !== SERVER_POLL_SETTINGS_EPOCH_BODY_SHA256) fail() + } +} diff --git a/backend/src/services/server-poll-storage-execution.sql-helper.ts b/backend/src/services/server-poll-storage-execution.sql-helper.ts new file mode 100644 index 00000000..ba289725 --- /dev/null +++ b/backend/src/services/server-poll-storage-execution.sql-helper.ts @@ -0,0 +1,62 @@ +/** Owned opt-in SQL profile only: actual production poll, journal and quota. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { readFile } from "node:fs/promises" +import { resolve } from "node:path" + +export async function acceptServerPollStorageExecutionSql(){ + assert([process.env.MMAPP_OWN_SERVER_POLL_STORAGE,process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH].includes("true")) + const {pool}=await import("../db/index.js") + const {ensurePartitionFor}=await import("../db/partitions.js") + const {createStorageDurableServerPollSession}=await import("./server-poll-durable-session.js") + const security=await import("../modules/mcp/security.js") + let requests=0,serverId:number|undefined,admissionChecked=0,firstReservationBytes="" + const receiptId=randomUUID(),executionId=randomUUID(),ownerId="owned-full-storage-"+randomUUID() + const listener=createServer(async(req,res)=>{ + requests++ + try{ + const admission=(await pool.query("SELECT a.claim_token,a.claim_attempted_at,a.used_rows,u.reserved_bytes::text FROM server_poll_storage_admissions a JOIN server_poll_storage_usage u ON u.scope='owner:'||a.owner_id JOIN server_poll_executions e USING(receipt_id) WHERE a.receipt_id=$1 AND a.execution_id=$2 AND a.owner_id=$3",[receiptId,executionId,ownerId])).rows + assert.equal(admission.length,1);assert.equal(admission[0].claim_token,null);assert(admission[0].claim_attempted_at);assert(Number(admission[0].used_rows)>=1) + firstReservationBytes=admission[0].reserved_bytes;assert.equal(firstReservationBytes,"8421376");admissionChecked++ + const result=req.url==="/rest/system/identity"?{name:"owned-poll-storage-after"}:req.url==="/rest/system/resource"?{version:"7.synthetic","board-name":"owned",uptime:"1h","cpu-load":"2","free-memory":"1024","total-memory":"2048"}:req.url==="/rest/interface"?[{name:"owned-interface",type:"ether"}]:req.url==="/rest/ip/address"?[{address:"192.0.2.1/24",interface:"owned-interface"}]:null + res.setHeader("content-type","application/json");res.statusCode=result===null?404:200;res.end(JSON.stringify(result)) + }catch{res.statusCode=500;res.end("SYNTHETIC admission check refused")} + }) + try{ + await new Promise((yes,no)=>{listener.once("error",no);listener.listen(0,"127.0.0.1",()=>{listener.off("error",no);yes()})}) + const port=(listener.address()as{port:number}).port + serverId=Number((await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES('owned-poll-storage-before','127.0.0.1',$1,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id",[port])).rows[0].id) + await ensurePartitionFor(pool,"server_snapshots","week",new Date()) + const issuedAt=Date.now()-1000,expiresAt=Date.now()+600000,generation=await security.accessGeneration() + const epoch=(await pool.query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1",[serverId])).rows[0].routing_endpoint_epoch + const authorityProof=await security.privateSecretFingerprint(security.fingerprint({ownerId,issuedAt,expiresAt,generation,serverId,port,epoch,permissions:["mm:servers:write"],global:true}),"owned-full-storage-authority-v1") + let authorityReads=0 + const session=createStorageDurableServerPollSession({request:{serverId,receiptId},identity:{kind:"rest",authentication:"local",ownerId,executionId,requestId:randomUUID(),issuedAt,expiresAt},authorize:async(request,ids,signal)=>{ + signal?.throwIfAborted();authorityReads++ + assert(Date.now()>=issuedAt&&Date.now()id===serverId)) + return {global:true,serverIds:[serverId!],authorityProof} + }}) + const plan=await session.prepare(),started=Date.now() + assert.equal(requests,0) + console.log("Owned storage-bound poll prepared:",JSON.stringify({maintenanceEffects:plan.maintenance.length})) + const receipt=await session.execute(plan) + assert.equal(receipt.complete,true);assert.equal(receipt.maintenanceComplete,true);assert.equal(receipt.configurationVerified,true) + assert.equal(requests,4);assert.equal(admissionChecked,4) + const admission=(await pool.query("SELECT max_stages,used_rows,used_bytes::text FROM server_poll_storage_admissions WHERE receipt_id=$1",[receiptId])).rows[0] + assert.equal(admission.max_stages,plan.maintenance.length+8) + assert.equal(admission.used_rows,2*(receipt.completedPublications+4)+2) + assert(Number(admission.used_bytes)>0&&Number(admission.used_bytes)<8388608) + const repeated=await session.execute(plan) + assert.equal(repeated.complete,true);assert.equal(requests,4) + assert.equal((await pool.query("SELECT used_rows FROM server_poll_storage_admissions WHERE receipt_id=$1",[receiptId])).rows[0].used_rows,admission.used_rows) + const result={fourActualNativeGets:requests,allDispatchesAfterConfirmedConsumedAdmission:admissionChecked,maintenanceEffects:plan.maintenance.length,completedPublications:receipt.completedPublications,fullJournalRows:admission.used_rows,logicalReservationBytes:firstReservationBytes,knownReplayNoNewNativeOrCharge:true,freshAuthorityReads:authorityReads,elapsedWithReplayMs:Date.now()-started,productionQuotaBoundAssembly:true,actualNewRestAuthorityFactory:false,publicRegistration:false} + console.log("Owned storage-bound full poll:",JSON.stringify(result));return result + }finally{ + listener.closeAllConnections();if(listener.listening)await new Promise(yes=>listener.close(()=>yes())) + if(serverId!==undefined)await pool.query("UPDATE servers SET enabled=false WHERE id=$1",[serverId]) + } +} diff --git a/backend/src/services/server-poll-storage-fill.sql-helper.ts b/backend/src/services/server-poll-storage-fill.sql-helper.ts new file mode 100644 index 00000000..f1a893a4 --- /dev/null +++ b/backend/src/services/server-poll-storage-fill.sql-helper.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { resolve } from "node:path" +/** Owned normal/historical SQL profiles only; fixed global quota evidence. */ +export async function acceptPollStorageGlobalCapSql(){ + assert([process.env.MMAPP_OWN_SERVER_POLL_STORAGE,process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH].includes("true")) + const {pool}=await import("../db/index.js") + const {createServerPollStorage}=await import("./server-poll-storage.js") + const globalBefore=Number((await pool.query("SELECT admissions FROM server_poll_storage_usage WHERE scope='global'")).rows[0].admissions) + const fill=await Promise.allSettled(Array.from({length:130-globalBefore},async()=>{ + const scope={receiptId:randomUUID(),kind:"rest" as const,executionId:randomUUID(),ownerId:"owned-fill-"+randomUUID(),bindingProof:"a".repeat(64),contextProof:"b".repeat(64),issuedAt:Date.now()-1000,expiresAt:Date.now()+180000} + return createServerPollStorage({scope,connectionId:null,maxStages:8,authorize:async()=>{}}).claim({deadlineAt:Date.now()+60000},async sql=>{ + await sql.query("INSERT INTO server_poll_executions(receipt_id,identity_kind,execution_id,binding_proof,context_ciphertext,context_proof) VALUES($1,'rest',$2,$3,'SYNTHETIC',$4)",[scope.receiptId,scope.executionId,scope.bindingProof,scope.contextProof]) + }) + })) + assert.equal(fill.filter(row=>row.status==="fulfilled").length,128-globalBefore) + assert.equal(Number((await pool.query("SELECT admissions FROM server_poll_storage_usage WHERE scope='global'")).rows[0].admissions),128) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions")).rows[0].n,128) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_usage WHERE scope<>'global' AND admissions=0")).rows[0].n,0) + const evidence=await(await import("./server-poll-storage-restore-evidence.sql-helper.js")).pollStorageRestoreEvidence(pool) + await writeFile(resolve(process.env.MCP_STATE_DIR!,"poll-storage-owned-restore-evidence.json"),JSON.stringify(evidence),{flag:"wx",mode:0o600}) + return {globalAdmissions:128,actualAdmissions:128,concurrentIndependentOwners:true,attemptsRefused:2,zeroOrphanCounters:true} +} diff --git a/backend/src/services/server-poll-storage-fresh.sql.test.ts b/backend/src/services/server-poll-storage-fresh.sql.test.ts new file mode 100644 index 00000000..ac33a2e8 --- /dev/null +++ b/backend/src/services/server-poll-storage-fresh.sql.test.ts @@ -0,0 +1,94 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import { basename,dirname,resolve } from "node:path" +assert.equal(process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH,"true") +const endpoint=new URL(process.env.DATABASE_URL??"") +assert.equal(endpoint.hostname,"127.0.0.1");assert.equal(endpoint.pathname,"/postgres") +assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const {pool,closePool}=await import("../db/index.js") +const {Pool}=await import("pg") +const {applySqlMigrations}=await import("../db/migrate.js") +const {applyLegacyPollStorageFixtureMigrations}=await import("../db/legacy-poll-storage-fixture.js") +try{ + await applySqlMigrations(pool);await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n,MIGRATION_MANIFEST.length) + assert.deepEqual((await pool.query("SELECT * FROM server_poll_storage_usage")).rows,[{scope:"global",admissions:0,reserved_rows:0,reserved_bytes:0,compacted_admissions:0,archive_bytes:0}]) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions")).rows[0].n,0) + const migration=await readFile(new URL("../../drizzle/0039_server_poll_storage.sql",import.meta.url),"utf8") + const name="owned_poll_enroll_"+randomUUID().replaceAll("-","") + await pool.query(`CREATE DATABASE "${name}" TEMPLATE template0`) + const otherUrl=new URL(endpoint);otherUrl.pathname="/"+name + const other=new Pool({connectionString:otherUrl.toString(),max:2,connectionTimeoutMillis:3000}) + try{ + await applyLegacyPollStorageFixtureMigrations(other) + const receiptId=randomUUID(),executionId=randomUUID() + await other.query("INSERT INTO server_poll_quarantines(receipt_id,identity_kind,execution_id,context_proof) VALUES($1,'rest',$2,$3)",[receiptId,executionId,"a".repeat(64)]) + const before=(await other.query("SELECT to_jsonb(t) AS value FROM server_poll_quarantines t")).rows + const client=await other.connect() + try{await assert.rejects(client.query(migration))}finally{client.release(true)} + assert.deepEqual((await other.query("SELECT to_jsonb(t) AS value FROM server_poll_quarantines t")).rows,before) + assert.equal((await other.query("SELECT to_regclass('public.server_poll_storage_admissions') IS NULL AS absent")).rows[0].absent,true) + assert.equal((await other.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0039_server_poll_storage'")).rows[0].n,0) + }finally{await other.end();await pool.query(`DROP DATABASE "${name}"`)} + const {tickMcpWorkers}=await import("../modules/mcp/workers.js") + assert.equal((await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled,false) + const expiredRequestIds = Array.from({ length: 1001 }, () => randomUUID()).sort() + const [runningId, freshId] = [randomUUID(), randomUUID()].sort() + await pool.query(`INSERT INTO mcp_requests(id,tool,status,created_at) + SELECT id,'owned-retention-test','completed',now()-interval '31 days' FROM unnest($1::uuid[]) AS ids(id) + UNION ALL SELECT $2::uuid,'owned-retention-test','running',now()-interval '32 days' + UNION ALL SELECT $3::uuid,'owned-retention-test','completed',now()-interval '29 days'`, [expiredRequestIds, runningId, freshId]) + const expiredAudit = (await pool.query(`INSERT INTO mcp_audit(actor_id,action,created_at) + SELECT 'owned-retention-test','expired',now()-interval '181 days' FROM generate_series(1,1001) + RETURNING id`)).rows.map(row => String(row.id)) + const lastExpiredAuditId = expiredAudit.reduce((last, id) => BigInt(id) > BigInt(last) ? id : last) + const freshAuditId = (await pool.query(`INSERT INTO mcp_audit(actor_id,action,created_at) + VALUES ('owned-retention-test','fresh',now()-interval '179 days') RETURNING id`)).rows[0].id + const artifactConnectionId = randomUUID() + const expiredArtifactIds = Array.from({ length: 1001 }, () => randomUUID()).sort() + const freshArtifactId = randomUUID() + await pool.query(`INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation) + VALUES ($1,'owned-retention-test','owned-retention-test','[]','{}','owned-retention-test')`, [artifactConnectionId]) + await pool.query(`INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,expires_at) + SELECT id,$1::uuid,'test','application/octet-stream','test',now()-interval '1 day' FROM unnest($2::uuid[]) AS ids(id) + UNION ALL SELECT $3::uuid,$1::uuid,'test','application/octet-stream','test',now()+interval '1 day'`, [artifactConnectionId, expiredArtifactIds, freshArtifactId]) + const requestIds = async () => (await pool.query("SELECT id FROM mcp_requests WHERE tool='owned-retention-test' ORDER BY id")).rows.map(row => row.id) + const auditIds = async () => (await pool.query("SELECT id FROM mcp_audit WHERE actor_id='owned-retention-test' ORDER BY id")).rows.map(row => String(row.id)) + const artifactIds = async () => (await pool.query("SELECT id FROM mcp_artifacts WHERE connection_id=$1 ORDER BY id", [artifactConnectionId])).rows.map(row => row.id) + await tickMcpWorkers() + assert.deepEqual(await requestIds(), [...expiredRequestIds.slice(-1), runningId, freshId].sort()) + assert.deepEqual(await auditIds(), [lastExpiredAuditId, String(freshAuditId)]) + assert.deepEqual(await artifactIds(), [...expiredArtifactIds.slice(-1), freshArtifactId].sort()) + await tickMcpWorkers() + assert.deepEqual(await requestIds(), [runningId, freshId].sort()) + assert.deepEqual(await auditIds(), [String(freshAuditId)]) + assert.deepEqual(await artifactIds(), [freshArtifactId]) + await pool.query("DELETE FROM mcp_artifacts WHERE id=$1", [freshArtifactId]) + await pool.query("DELETE FROM mcp_connections WHERE id=$1", [artifactConnectionId]) + console.log("Owned fresh poll storage:",JSON.stringify({normalStartupTwice:true,migrations:MIGRATION_MANIFEST.length,globalZeroSeed:true,legacyPrefix:38,nonemptyJournalRefusedBeforeDdl:true,oldRowsPreserved:true,noForgedMarker:true,actualWorkerTickWithMcpDisabled:true,boundedRequestAuditAndArtifactRetention:true})) + if (process.env.MMAPP_OWN_SERVER_POLL_SETTINGS_EPOCH === "true") { + await (await import("../db/server-poll-settings-epoch.owned-helper.js")).acceptServerPollSettingsEpochMigrationSql(pool) + } else if (process.env.MMAPP_OWN_SERVER_POLL_ATOMIC_MAINTENANCE === "true") { + await (await import("./server-poll-atomic-owned-helper.js")).acceptAtomicServerPollOwned() + } else if (process.env.MMAPP_OWN_SERVER_POLL_DELAYED_APPROVAL === "true") { + assert.equal(process.env.NODE_ENV,"test");assert.equal(endpoint.username,"mcp_test") + await(await import("../db/bootstrap.js")).initDatabase() + const {acceptServerPollPublicRuntimeSql}=await import("../modules/mcp/server-poll-http.sql-helper.js") + for(const scenario of ["runtime","configuration-aba","guard"] as const)await acceptServerPollPublicRuntimeSql({delayedApproval:scenario}) + console.log("Owned delayed human approval: runtime waiting accepted; configuration ABA and missing guard refused before admission/native") + } else if (process.env.MMAPP_OWN_SERVER_POLL_HTTP_ROUTE === "true") { + assert.equal(process.env.NODE_ENV, "test"); assert.equal(endpoint.username, "mcp_test") + await (await import("../db/bootstrap.js")).initDatabase() + await (await import("./server-poll-rest-authority.sql-helper.js")).acceptServerPollRestAuthoritySql({ httpRoute: true }) + await (await import("../modules/mcp/server-poll-http.sql-helper.js")).acceptServerPollPublicRuntimeSql() + console.log("Owned full poll registered HTTP route and normal MCP parity accepted; cold/quota matrix not run") + } else { + const publicOnly=process.env.MMAPP_OWN_SERVER_POLL_PUBLIC_ONLY==="true" + if(!publicOnly&&process.env.MMAPP_OWN_SERVER_POLL_AUTHORITY_ONLY!=="true")await(await import("./server-poll-storage-execution.sql-helper.js")).acceptServerPollStorageExecutionSql() + if(!publicOnly)await(await import("./server-poll-rest-authority.sql-helper.js")).acceptServerPollRestAuthoritySql() + if(process.env.MMAPP_OWN_SERVER_POLL_AUTHORITY_ONLY!=="true")await(await import("../modules/mcp/server-poll-http.sql-helper.js")).acceptServerPollPublicRuntimeSql() + console.log("Owned fresh poll quota:",JSON.stringify(await(await import("./server-poll-storage-fill.sql-helper.js")).acceptPollStorageGlobalCapSql())) + } +}finally{await closePool()} diff --git a/backend/src/services/server-poll-storage-journal.test.ts b/backend/src/services/server-poll-storage-journal.test.ts new file mode 100644 index 00000000..651821e6 --- /dev/null +++ b/backend/src/services/server-poll-storage-journal.test.ts @@ -0,0 +1,242 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mkdtemp, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, join, resolve } from "node:path" +import { test } from "node:test" +import { fingerprint, privateSecretFingerprint } from "../modules/mcp/security.js" +import type { ProbeJournalSql } from "./server-probe-journal.js" +import type { ServerPollJournalContext } from "./server-poll-journal.js" +import type { ProbeStorageOptions } from "./server-probe-journal-maintenance.js" +import { createServerPollStorageForTests, PollStorageRefused, PollStorageUnknown, type PollStorageBinding } from "./server-poll-storage.js" +import { createServerPollStorageJournalForTests, PollStorageSpent } from "./server-poll-storage-journal.js" + +const gate = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function context(): ServerPollJournalContext { + const now = Date.now() + return { identity: { kind: "mcp", ownerId: "original-owner", executionId: randomUUID(), connectionId: randomUUID(), credentialId: randomUUID(), operationName: "servers.poll", issuedAt: now - 1000, expiresAt: now + 60000 }, + plan: { version: 1, requiresHuman: true, scope: "global", request: { serverId: 1, receiptId: randomUUID() }, around: new Date(now).toISOString(), + beforeProof: "a".repeat(64), authorityProof: "b".repeat(64), bindingProof: "c".repeat(64), maintenance: [], affectedServerIds: [1], mutationCost: 8 } } +} +function fixture() { + let present = false, anchored = false, poisoned = false, loss: "none" | "reserve" | "consume" = "none", quarantineFails = false, revoked = false, clock = Date.now() + const events: string[] = [], statements: string[] = [], windows: ProbeStorageOptions[] = [], seen: ServerPollJournalContext[] = [] + let captured!: PollStorageBinding + const sqlFor = () => { + let active = true + const sql: ProbeJournalSql = Object.freeze({ async query(text: string) { + if (!active) throw new PollStorageRefused() + statements.push(text) + if (text.includes("poll-storage-journal:existing-anchor")) return { rows: anchored ? [{ receipt_id: captured.scope.receiptId, identity_kind: captured.scope.kind, execution_id: captured.scope.executionId }] : [] } + if (text.startsWith("INSERT INTO server_poll_executions")) anchored = true + return { rows: [] } + } }) + return { sql, close: () => { active = false } } + } + const scoped = async (label: string, options: ProbeStorageOptions, work: (sql: ProbeJournalSql) => Promise) => { + if (poisoned) throw new PollStorageRefused() + windows.push(options); await captured.authorize(options); events.push(label + ":begin-lock") + const owner = sqlFor() + try { const result = await work(owner.sql); await captured.authorize(options); events.push(label + ":commit"); return result } + finally { owner.close(); events.push(label + ":release") } + } + const dependencies = { now: () => clock, contextProof: async () => { events.push("context-proof"); return "d".repeat(64) }, + createStorage(binding: PollStorageBinding): ReturnType { + captured = binding; events.push("construct-storage") + return Object.freeze({ + inspect: (options, work) => scoped("inspect", options, sql => work(sql, present ? {} : undefined)), + claim: async (options, work) => { + if (present || poisoned) throw new PollStorageRefused() + await captured.authorize(options); present = true; events.push("reservation:commit") + if (loss === "reserve") { poisoned = true; events.push("reservation:destroy"); throw new PollStorageUnknown() } + return scoped("consume", options, async sql => { events.push("consume:token"); const result = await work(sql) + if (loss === "consume") { poisoned = true; events.push("consume:destroy"); throw new PollStorageUnknown() } + return result }) + }, + write: (options, work) => scoped("write", options, work), + quarantine: async () => { events.push("quarantine"); if (quarantineFails) throw new PollStorageUnknown() }, + }) + } } + const options = { deadlineAt: clock + 2000, assertAuthority: async (value: Readonly) => { + events.push("authority"); if (revoked) throw Error("PRIVATE_PROVIDER revoked"); seen.push(value as ServerPollJournalContext) + } } + return { dependencies, options, events, statements, windows, seen, binding: () => captured, + existing: (anchor: boolean) => { present = true; anchored = anchor }, lose: (phase: typeof loss) => { loss = phase }, + failQuarantine: () => { quarantineFails = true }, revoke: () => { revoked = true }, advance: (amount: number) => { clock += amount } } +} + +test("context MAC follows current authority and uses exact original frozen context", async () => { + const f = fixture(), original = context(), expected = structuredClone(original) + const pending = createServerPollStorageJournalForTests(original, f.options, f.dependencies) + original.identity.ownerId = "mutated"; original.plan.affectedServerIds.length = 0 + const bridge = await pending + assert.deepEqual(f.seen[0], expected); assert(Object.isFrozen(f.seen[0])); assert(Object.isFrozen(f.seen[0].identity)); assert(Object.isFrozen(f.seen[0].plan.affectedServerIds)) + assert(f.events.indexOf("authority") < f.events.indexOf("context-proof")) + assert.equal(f.binding().scope.contextProof, "d".repeat(64)); assert.equal(f.binding().scope.ownerId, expected.identity.ownerId) + assert.equal(f.binding().maxStages, 8); assert.equal(f.binding().connectionId, expected.identity.kind === "mcp" ? expected.identity.connectionId : null) + await bridge.transaction("claim", sql => sql.query("INSERT INTO server_poll_executions VALUES($1)", [expected.plan.request.receiptId])) + assert(f.events.indexOf("reservation:commit") < f.events.indexOf("consume:token")) +}) + +test("read bridge missing admission or anchor never reserves, consumes, writes or quarantines", async () => { + for (const present of [false, true]) { + const f = fixture(); if (present) f.existing(false) + const bridge = await createServerPollStorageJournalForTests(context(), f.options, f.dependencies) + assert.equal(await bridge.read(async () => assert.fail("No existing anchor")), undefined) + assert(!f.events.some(event => /reservation|consume|write|quarantine/.test(event))) + } +}) + +test("read bridge uses existing same-lease SELECT facade and refuses writes and escaped late SQL", async () => { + const f = fixture(); f.existing(true) + const bridge = await createServerPollStorageJournalForTests(context(), f.options, f.dependencies) + let escaped!: ProbeJournalSql + assert.equal(await bridge.read(async sql => { escaped = sql; await sql.query("SELECT authenticated_receipt"); return "receipt" }), "receipt") + const commands = f.statements.length + await assert.rejects(escaped.query("SELECT late_receipt"), PollStorageRefused); assert.equal(f.statements.length, commands) + await assert.rejects(bridge.read(sql => sql.query("INSERT INTO server_poll_executions VALUES(1)")), PollStorageRefused) + assert(!f.statements.some(text => text.startsWith("INSERT"))); assert(!f.events.some(event => /reservation|consume|write|quarantine/.test(event))) +}) + +test("read COMMIT loss propagates without implicit quarantine or new claim", async () => { + const f = fixture(); f.existing(true) + const createStorage = (binding: PollStorageBinding) => { + const storage = f.dependencies.createStorage(binding) + return { ...storage, inspect: (async (options, work) => { await storage.inspect(options, work); throw new PollStorageUnknown() }) as typeof storage.inspect } + } + const bridge = await createServerPollStorageJournalForTests(context(), f.options, { ...f.dependencies, createStorage }) + await assert.rejects(bridge.read(sql => sql.query("SELECT authenticated_receipt")), PollStorageUnknown) + assert(!f.events.some(event => /reservation|consume|write|quarantine/.test(event))) +}) + +test("default context MAC equals the journal codec domain using real owned crypto", async () => { + const directory = await mkdtemp(join(tmpdir(), "poll-storage-bridge-")), prior = process.env.MCP_STATE_DIR + assert.equal(resolve(dirname(directory)).toLowerCase(), resolve(tmpdir()).toLowerCase()); assert(basename(directory).startsWith("poll-storage-bridge-")) + process.env.MCP_STATE_DIR = directory + try { + const f = fixture(), source = context() + await createServerPollStorageJournalForTests(source, f.options, { createStorage: f.dependencies.createStorage, now: f.dependencies.now }) + assert.equal(f.binding().scope.contextProof, await privateSecretFingerprint(fingerprint(source), "server-poll-journal-v1:context")) + } finally { if (prior === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = prior; await rm(directory, { recursive: true, force: true }) } +}) + +test("REST scope has no MCP connection and stage cap is derived from captured maintenance", async () => { + const f = fixture(), source = context(), now = Date.now() + source.identity = { kind: "rest", ownerId: "accepted-local-owner", executionId: randomUUID(), requestId: randomUUID(), authentication: "local", issuedAt: now - 1000, expiresAt: now + 60000 } + source.plan.maintenance = [{ kind: "partition.storage", parent: "traffic_samples", name: "traffic_samples_w_20261026", settings: { fillfactor: "70" } }] + source.plan.mutationCost = 9 + await createServerPollStorageJournalForTests(source, f.options, f.dependencies) + assert.equal(f.binding().connectionId, null); assert.equal(f.binding().scope.kind, "rest") + assert.equal(f.binding().maxStages, 9); assert.equal(f.binding().scope.expiresAt, source.identity.expiresAt) +}) + +test("explicit v4 stage grouping preserves full logical cost and rejects discounted admission", async () => { + const f = fixture(), source = context() + source.plan.version = 4; source.plan.approvalProof = "d".repeat(64); source.plan.maintenanceGroupSize = 8 + source.plan.maintenance = Array.from({ length: 9 }, (_, index) => ({ kind: "partition.drop", parent: "server_snapshots", + name: "server_snapshots_w_" + new Date(Date.UTC(2000, 0, 3 + index * 7)).toISOString().slice(0, 10).replaceAll("-", "") })) + source.plan.mutationCost = 17 + await createServerPollStorageJournalForTests(source, f.options, f.dependencies) + assert.equal(f.binding().maxStages, 10) + source.plan.mutationCost = 10 + await assert.rejects(createServerPollStorageJournalForTests(source, f.options, f.dependencies), PollStorageRefused) + assert.equal(f.events.filter(event => event === "construct-storage").length, 1) +}) + +test("spent admission without anchor returns typed refusal and never renews quota/token or invokes work", async () => { + const f = fixture(); f.existing(false) + const bridge = await createServerPollStorageJournalForTests(context(), f.options, f.dependencies); let called = false + await assert.rejects(bridge.transaction("claim", async () => { called = true }), PollStorageSpent) + assert.equal(called, false); assert(!f.events.includes("reservation:commit")); assert(!f.events.includes("consume:token")) +}) + +test("typed spent error survives the real storage transaction wrapper", async () => { + const value = context(), commands: string[] = [] + let row: Record + const bridge = await createServerPollStorageJournalForTests(value, { deadlineAt: Date.now() + 2000, assertAuthority: async () => {} }, { + contextProof: async () => "d".repeat(64), createStorage: binding => { + row = { receipt_id: binding.scope.receiptId, identity_kind: binding.scope.kind, execution_id: binding.scope.executionId, + owner_id: binding.scope.ownerId, connection_id: binding.connectionId, binding_proof: binding.scope.bindingProof, context_proof: binding.scope.contextProof, + authority_issued_at: new Date(binding.scope.issuedAt), authority_expires_at: new Date(binding.scope.expiresAt), max_stages: binding.maxStages } + return createServerPollStorageForTests(binding, { now: Date.now, connect: async () => ({ query: async text => { + commands.push(text); return { rows: text.startsWith("SELECT * FROM server_poll_storage_admissions") ? [row] : [] } + }, release: () => {} }) }) + }, + }) + await assert.rejects(bridge.transaction("claim", async () => assert.fail("Spent identity must not execute journal callback")), PollStorageSpent) + assert(commands.includes("ROLLBACK")); assert(!commands.some(sql => sql.startsWith("INSERT"))) +}) + +test("known receipt uses frozen SELECT-only facade inside inspect, with comments/FOR UPDATE", async () => { + const f = fixture(); f.existing(true) + const bridge = await createServerPollStorageJournalForTests(context(), f.options, f.dependencies); let facade!: ProbeJournalSql + const known = await bridge.transaction("claim", async sql => { + facade = sql; assert(Object.isFrozen(sql)) + await sql.query("/* poll-journal:anchor */ SELECT * FROM server_poll_executions WHERE receipt_id=$1 FOR UPDATE", [f.binding().scope.receiptId]) + await sql.query("/* poll-journal:lock */ SELECT pg_advisory_xact_lock(hashtextextended($1,0))", ["fixed-journal-identity-lock"]) + await sql.query("-- context read\nSELECT * FROM server_poll_receipts WHERE receipt_id=$1", [f.binding().scope.receiptId]) + return { state: "known", receipt: "validated by journal callback" } + }) + assert.equal(known.state, "known"); assert(!f.events.includes("reservation:commit")); assert(!f.events.includes("consume:token")) + await assert.rejects(facade.query("SELECT * FROM server_poll_receipts"), PollStorageRefused) +}) + +test("known identity cannot write, use CTE, SELECT INTO, lifecycle or multiple statements", async () => { + for (const text of ["INSERT INTO server_poll_executions VALUES(1)", "UPDATE server_poll_receipts SET receipt_proof='x'", "DELETE FROM server_poll_intents", "WITH gone AS (DELETE FROM server_poll_intents RETURNING *) SELECT * FROM gone", "SELECT 1 INTO poll_escape", "SELECT 1; DELETE FROM server_poll_receipts", "COMMIT", "/* safe? */ ROLLBACK"]) { + const f = fixture(); f.existing(true) + const bridge = await createServerPollStorageJournalForTests(context(), f.options, f.dependencies) + await assert.rejects(bridge.transaction("claim", sql => sql.query(text)), PollStorageRefused) + assert(!f.statements.includes(text)); assert(!f.events.includes("reservation:commit")) + } +}) + +test("write phase delegates only through charged storage and original accepted options", async () => { + const f = fixture(), value = context(), accepted = f.options.deadlineAt + const bridge = await createServerPollStorageJournalForTests(value, f.options, f.dependencies) + f.options.deadlineAt += 60000; value.identity.expiresAt += 60000 + await bridge.transaction("claim", sql => sql.query("INSERT INTO server_poll_executions VALUES($1)", [value.plan.request.receiptId])) + await bridge.transaction("write", sql => sql.query("INSERT INTO server_poll_intents VALUES($1)", [value.plan.request.receiptId])) + assert(f.events.includes("write:commit")); assert(f.windows.every(options => options.deadlineAt === accepted)) +}) + +test("unknown reservation never runs journal work and existing spent identity stays closed", async () => { + const f = fixture(); f.lose("reserve") + const bridge = await createServerPollStorageJournalForTests(context(), f.options, f.dependencies); let called = false + await assert.rejects(bridge.transaction("claim", async () => { called = true }), PollStorageUnknown) + assert.equal(called, false); assert(!f.events.includes("consume:token")); assert.equal(await bridge.quarantine(), true) + await assert.rejects(bridge.transaction("write", async () => {}), PollStorageRefused) +}) + +test("unknown consume releases owner before explicit independent quarantine", async () => { + const f = fixture(); f.lose("consume") + const bridge = await createServerPollStorageJournalForTests(context(), f.options, f.dependencies) + await assert.rejects(bridge.transaction("claim", sql => sql.query("INSERT INTO server_poll_executions VALUES(1)")), PollStorageUnknown) + assert.equal(await bridge.quarantine(), true); assert(f.events.indexOf("consume:release") < f.events.indexOf("quarantine")) +}) + +test("quarantine survives original signal/expiry/revocation without authority and returns false on unknown", async () => { + const f = fixture(), controller = new AbortController() + const bridge = await createServerPollStorageJournalForTests(context(), { ...f.options, signal: controller.signal }, f.dependencies) + const before = f.events.filter(event => event === "authority").length + controller.abort(); f.advance(300000); f.revoke() + assert.equal(await bridge.quarantine(), true); assert.equal(f.events.filter(event => event === "authority").length, before) + f.failQuarantine(); assert.equal(await bridge.quarantine(), false) + await assert.rejects(bridge.transaction("claim", async () => {}), PollStorageRefused) +}) + +test("original expiry bounds setup and leaked late crypto cannot construct a storage provider", async () => { + const f = fixture(), source = context(), entered = gate(), resume = gate() + source.identity.expiresAt = f.dependencies.now() + 30 + f.dependencies.contextProof = async () => { entered.resolve(); await resume.promise; return "d".repeat(64) } + const pending = createServerPollStorageJournalForTests(source, f.options, f.dependencies) + await entered.promise; await assert.rejects(pending, PollStorageRefused); resume.resolve(); await new Promise(done => setImmediate(done)) + assert(!f.events.includes("construct-storage")) +}) + +test("pre-aborted/expired creation refuses before authority, proof or storage I/O", async () => { + const f = fixture(), controller = new AbortController(); controller.abort() + await assert.rejects(createServerPollStorageJournalForTests(context(), { ...f.options, signal: controller.signal }, f.dependencies), PollStorageRefused) + const expired = context(); expired.identity.expiresAt = f.dependencies.now() - 1 + await assert.rejects(createServerPollStorageJournalForTests(expired, f.options, f.dependencies), PollStorageRefused) + assert.equal(f.events.length, 0) +}) diff --git a/backend/src/services/server-poll-storage-journal.ts b/backend/src/services/server-poll-storage-journal.ts new file mode 100644 index 00000000..744b8dcc --- /dev/null +++ b/backend/src/services/server-poll-storage-journal.ts @@ -0,0 +1,133 @@ +/** Private bridge between the authenticated poll journal and charged storage. + * No construction registers a route/service or enrolls a schema. */ +import { publicationLayout } from "./server-poll-maintenance-groups.js" +import { fingerprint, privateSecretFingerprint } from "../modules/mcp/security.js" +import type { ProbeJournalSql } from "./server-probe-journal.js" +import type { ProbeStorageOptions } from "./server-probe-journal-maintenance.js" +import type { ServerPollJournalContext } from "./server-poll-journal.js" +import { createServerPollStorage, createHolderBoundServerPollStorage, PollStorageRefused, type PollStorageBinding } from "./server-poll-storage.js" +import { isServerPollJournalOwner, type ServerPollJournalOwner } from "./server-partition-maintenance-lease.js" + +export class PollStorageSpent extends PollStorageRefused { + readonly automaticReplay = false + constructor() { super(); this.message = "Poll admission is already spent; automatic replay forbidden"; this.name = "PollStorageSpent" } +} +export interface ServerPollJournalStorage { + read(work: (sql: ProbeJournalSql) => Promise): Promise + transaction(phase: "claim" | "write", work: (sql: ProbeJournalSql) => Promise): Promise + quarantine(): Promise +} +export interface ServerPollStorageJournalOptions { + assertAuthority(context: Readonly): Promise + deadlineAt: number + signal?: AbortSignal +} +interface Dependencies { + createStorage: typeof createServerPollStorage + contextProof(context: ServerPollJournalContext): Promise + now(): number +} +const authorityOwners=new WeakMapvoid}>() +/** A fixed production bridge already checks this exact authority before/after + * every SQL. Only immutable composition is recognized, never a cached grant. + * Serialized, wrapped or test-injected adapters cannot manufacture this proof. */ +export function bindServerPollJournalAuthority(storage:ServerPollJournalStorage,context:ServerPollJournalContext,provider:ServerPollStorageJournalOptions["assertAuthority"]):(()=>void)|undefined{ + const owner=authorityOwners.get(storage) + return owner&&owner.provider===provider&&owner.context===fingerprint(context)?owner.local:undefined +} +const production: Dependencies = { createStorage: createServerPollStorage, + contextProof: context => privateSecretFingerprint(fingerprint(context), "server-poll-journal-v1:context"), now: Date.now } +const fail = (): never => { throw new PollStorageRefused() } +function freeze(value: T): T { + if (value && typeof value === "object") { for (const child of Object.values(value)) freeze(child); Object.freeze(value) } + return value +} +/** Callbacks are fixed journal source code, never caller-supplied SQL. Block + * writes/CTEs/SELECT INTO, lifecycle commands and multiple statements while + * retaining the journal's SELECT ... FOR UPDATE and leading comments. */ +function readOnly(sql: ProbeJournalSql): ProbeJournalSql { + return Object.freeze({ query: async (text: string, values?: unknown[]) => { + const statement = text.replace(/\/\*[\s\S]*?\*\/|--[^\r\n]*/g, " ").trim() + const plain = statement.replace(/\bFOR\s+(?:NO\s+KEY\s+)?UPDATE\b/gi, "FOR_LOCK") + if (!/^SELECT\b/i.test(statement) || /;\s*\S/.test(statement) || /\b(?:INSERT|UPDATE|DELETE|MERGE|CREATE|ALTER|DROP|TRUNCATE|COPY|CALL|DO|INTO|BEGIN|COMMIT|ROLLBACK|SET|RESET|GRANT|REVOKE)\b/i.test(plain)) fail() + return sql.query(text, values) + } }) +} + +async function construct(raw: ServerPollJournalContext, rawOptions: ServerPollStorageJournalOptions, rawDependencies: Dependencies,productionOwner=false): Promise { + const deps = Object.freeze({ ...rawDependencies }), context = freeze(structuredClone(raw)), authority = rawOptions.assertAuthority + const deadlineAt = Math.min(rawOptions.deadlineAt, context.identity.expiresAt, deps.now() + 180000) + const options: ProbeStorageOptions = Object.freeze({ deadlineAt, signal: rawOptions.signal }) + if (typeof authority !== "function" || !Number.isSafeInteger(rawOptions.deadlineAt) || !Number.isSafeInteger(deadlineAt) + || (context.plan.version !== 1 && context.plan.version !== 2 && context.plan.version !== 3 && context.plan.version !== 4) || context.plan.requiresHuman !== true || context.plan.scope !== "global" + || !Array.isArray(context.plan.maintenance) || context.plan.maintenance.length > 2000) fail() + let maxStages:number + try{maxStages=publicationLayout(context.plan).journalMaxStages}catch{fail()} + let active = true + const live = () => { if (!active || options.signal?.aborted || deps.now() >= deadlineAt) fail() } + const authorize = async () => { live(); await authority(context); live() } + let stop!: () => void + const stopped = new Promise((_yes, reject) => { stop = () => { active = false; reject(new PollStorageRefused()) } }) + const timer = setTimeout(stop, Math.max(1, deadlineAt - deps.now())) + options.signal?.addEventListener("abort", stop, { once: true }) + let storage: ReturnType + try { + const setup = Promise.resolve().then(async () => { + await authorize() + const contextProof = await deps.contextProof(context); live() + if (!/^[a-f0-9]{64}$/.test(contextProof)) fail() + await authorize() + const binding: PollStorageBinding = { scope: { receiptId: context.plan.request.receiptId, kind: context.identity.kind, + executionId: context.identity.executionId, ownerId: context.identity.ownerId, bindingProof: context.plan.bindingProof, contextProof, + issuedAt: context.identity.issuedAt, expiresAt: context.identity.expiresAt }, + connectionId: context.identity.kind === "mcp" ? context.identity.connectionId : null, + maxStages, authorize: async () => authorize() } + return deps.createStorage(binding) + }) + if (options.signal?.aborted || deps.now() >= deadlineAt) stop() + storage = await Promise.race([setup, stopped]); live() + } catch { active = false; throw new PollStorageRefused() } + finally { clearTimeout(timer); options.signal?.removeEventListener("abort", stop) } + const boundary:ServerPollJournalStorage=Object.freeze({ + async read(work: (sql: ProbeJournalSql) => Promise): Promise { + live() + return storage.inspect(options, async (sql, row) => { + if (!row) return undefined + const anchors = (await sql.query(`/* poll-storage-journal:existing-anchor */ SELECT receipt_id,identity_kind,execution_id + FROM server_poll_executions WHERE receipt_id=$1 OR(identity_kind=$2 AND execution_id=$3) LIMIT 2 FOR UPDATE`, + [context.plan.request.receiptId, context.identity.kind, context.identity.executionId])).rows + if (!anchors.length) return undefined + if (anchors.length !== 1 || anchors[0].receipt_id !== context.plan.request.receiptId + || anchors[0].identity_kind !== context.identity.kind || anchors[0].execution_id !== context.identity.executionId) return fail() + return work(readOnly(sql)) + }) + }, + async transaction(phase: "claim" | "write", work: (sql: ProbeJournalSql) => Promise): Promise { + live() + if (phase === "write") return storage.write(options, work) + if (phase !== "claim") return fail() + const found = await storage.inspect(options, async (sql, row) => { + if (!row) return { present: false as const } + const anchors = (await sql.query(`/* poll-storage-journal:existing-anchor */ SELECT receipt_id,identity_kind,execution_id + FROM server_poll_executions WHERE receipt_id=$1 OR(identity_kind=$2 AND execution_id=$3) LIMIT 2 FOR UPDATE`, + [context.plan.request.receiptId, context.identity.kind, context.identity.executionId])).rows + if (!anchors.length) throw new PollStorageSpent() + if (anchors.length !== 1 || anchors[0].receipt_id !== context.plan.request.receiptId + || anchors[0].identity_kind !== context.identity.kind || anchors[0].execution_id !== context.identity.executionId) return fail() + return { present: true as const, value: await work(readOnly(sql)) } + }) + return found.present ? found.value : storage.claim(options, work) + }, + async quarantine() { try { await storage.quarantine(); return true } catch { return false } }, + }) + if(productionOwner)authorityOwners.set(boundary,{context:fingerprint(context),provider:authority,local:live}) + return boundary +} + +export const createServerPollStorageJournal = (context: ServerPollJournalContext, options: ServerPollStorageJournalOptions) => construct(context, options, production,true) +export function createHolderBoundServerPollStorageJournal(context:ServerPollJournalContext,options:ServerPollStorageJournalOptions,owner:ServerPollJournalOwner) { + if(!isServerPollJournalOwner(owner))fail() + return construct(context,options,{...production,createStorage:binding=>createHolderBoundServerPollStorage(binding,owner)},true) +} +/** Explicit trusted unit composition only; no request can select these hooks. */ +export const createServerPollStorageJournalForTests = (context: ServerPollJournalContext, options: ServerPollStorageJournalOptions, dependencies: Pick & Partial>) => construct(context, options, { ...production, ...dependencies }) diff --git a/backend/src/services/server-poll-storage-model.test.ts b/backend/src/services/server-poll-storage-model.test.ts new file mode 100644 index 00000000..f180a3c7 --- /dev/null +++ b/backend/src/services/server-poll-storage-model.test.ts @@ -0,0 +1,139 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { SERVER_POLL_STORAGE_POLICY as policy, PollStorageRefused, pollStoragePolicy, pollStorageScope, + pollStorageQuotaFit, pollStorageUsageRemaining, pollStorageGcEligible, type PollStorageScope, type PollStorageGcCandidate } from "./server-poll-storage-model.js" + +const DAY = 86400000 +const scope: PollStorageScope = { receiptId: "5f4aaf7b-cc19-4b36-b693-5ce092a03852", executionId: "b332191d-bf58-4196-a629-f30367a48ef7", + kind: "rest", ownerId: "operator", bindingProof: "a".repeat(64), contextProof: "b".repeat(64), issuedAt: 1000, expiresAt: 2000 } +const empty = { attempts: 0, bytes: 0, rows: 0 } +const usage = (attempts: number) => ({ attempts, bytes: attempts * (policy.attemptBytes + policy.admissionBytes), rows: attempts * policy.attemptRows }) +const compactUsage = (attempts: number, compactedAttempts:number, encodedPerArchive=59075) => ({ attempts,compactedAttempts,archiveBytes:compactedAttempts*encodedPerArchive, + bytes:attempts*(policy.attemptBytes+policy.admissionBytes)+compactedAttempts*(policy.admissionBytes+encodedPerArchive),rows:attempts*policy.attemptRows+compactedAttempts*2 }) +const candidate = (patch: Partial = {}): PollStorageGcCandidate => ({ scope, lastActivityAt: 1500, + gcNotBefore: 0, retentionDays: 14, safetyWindowMs: 60000, mcpTerminal: false, ...patch }) +const boundary = 2000 + 14 * DAY + 60000 + +test("reservations cover all journal rows and metadata independently, with immutable cloned policy", () => { + assert.equal(policy.attemptRows, 1 + 2008 + 2008 + 1 + 1) + assert.equal(policy.attemptBytes, 8 * 1024 * 1024) + assert.equal(policy.admissionBytes, 32768) + assert.equal(policy.globalAttempts, 128); assert.equal(policy.ownerAttempts, 8) + assert.equal(policy.globalRows, 128 * 4019); assert.equal(policy.ownerRows, 8 * 4019) + assert.equal(policy.globalBytes, 128 * (8388608 + 32768)); assert.equal(policy.ownerBytes, 8 * (8388608 + 32768)) + const input = { ...policy, retentionDays: 30 }, cloned = pollStoragePolicy(input) + input.retentionDays = 1 + assert.equal(cloned.retentionDays, 30); assert.equal(Object.isFrozen(cloned), true) + for (const patch of [{ globalAttempts: 129 }, { attemptRows: 4018 }, { ownerBytes: policy.ownerBytes + 1 }, { retentionDays: 0 }, + { retentionDays: 366 }, { safetyWindowMs: 59999 }, { safetyWindowMs: DAY + 1 }, { retentionDays: 1.5 }]) { + assert.throws(() => pollStoragePolicy({ ...policy, ...patch }), PollStorageRefused) + } + assert.equal(pollStoragePolicy({ ...policy, retentionDays: 365, safetyWindowMs: DAY }).retentionDays, 365) +}) + +test("scope validates original identity, clones and freezes, and never invents a lifetime cap", () => { + const input = { ...scope }, parsed = pollStorageScope(input, { now: 1000 }) + input.ownerId = "changed" + assert.equal(parsed.ownerId, "operator"); assert.equal(Object.isFrozen(parsed), true) + assert.equal(pollStorageScope({ ...scope, ownerId: "x".repeat(200), expiresAt: 2000 + 1000 * DAY }, { now: 1500 }).expiresAt, 2000 + 1000 * DAY) + for (const patch of [{ receiptId: "invalid" }, { executionId: "invalid" }, { ownerId: " operator" }, { ownerId: "operator " }, + { ownerId: "x".repeat(201) }, { ownerId: "x\n" }, { ownerId: "x\u0085" }, { ownerId: "" }, { bindingProof: "A".repeat(64) }, + { contextProof: "b".repeat(63) }, { kind: "unknown" }, { issuedAt: -1 }, { issuedAt: 1.5 }, { issuedAt: NaN }, + { expiresAt: Infinity }, { expiresAt: 8640000000000001 }, { expiresAt: 1000 }, { extra: true }]) { + assert.throws(() => pollStorageScope({ ...scope, ...patch }, { now: 1500 }), PollStorageRefused) + } + assert.throws(() => pollStorageScope(scope, { now: 999 }), PollStorageRefused) + assert.throws(() => pollStorageScope(scope, { now: 2000 }), PollStorageRefused) + assert.equal(pollStorageScope(scope, { now: 2000, allowExpired: true }).expiresAt, 2000) +}) + +test("quota checks exact capacity and cannot refund admission allowance to fit an extra attempt", () => { + assert.equal(pollStorageQuotaFit(usage(127), usage(7)), true) + assert.equal(pollStorageQuotaFit(usage(128), empty), false) + assert.equal(pollStorageQuotaFit(usage(8), usage(8)), false) + for (const dimension of ["bytes", "rows", "attempts"] as const) { + assert.equal(pollStorageQuotaFit({ ...usage(127), [dimension]: usage(127)[dimension] + 1 }, empty), false) + assert.equal(pollStorageQuotaFit(usage(127), { ...usage(7), [dimension]: usage(7)[dimension] + 1 }), false) + for (const bad of [-1, 0.5, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1, "0", undefined]) { + assert.equal(pollStorageQuotaFit({ ...empty, [dimension]: bad } as typeof empty, empty), false) + } + } + assert.equal(pollStorageQuotaFit({ ...usage(127), bytes: policy.globalBytes - policy.attemptBytes }, empty), false) + assert.equal(pollStorageQuotaFit(empty, usage(1)), false) + assert.equal(pollStorageQuotaFit({ ...empty, bytes: Number.MAX_SAFE_INTEGER }, empty), false) +}) + +test("quota refuses partial, undercharged and mismatched reservation counters", () => { + const malformed = [{ attempts: 1 }, { bytes: policy.attemptBytes, rows: policy.attemptRows }, + { ...usage(1), bytes: policy.attemptBytes }, { ...usage(1), bytes: usage(1).bytes - 1 }, + { ...usage(1), rows: policy.attemptRows - 1 }, { ...usage(1), rows: policy.attemptRows + 1 }, + { ...usage(1), attempts: 2 }, { ...empty, bytes: 1 }, { ...empty, rows: 1 }, + { ...usage(1), bytes: usage(1).bytes + 1 }, { ...usage(1), extra: true }] + for (const value of malformed) { + assert.equal(pollStorageQuotaFit(value as typeof empty, empty), false) + assert.equal(pollStorageQuotaFit(usage(7), value as typeof empty), false) + } + assert.equal(pollStorageQuotaFit(usage(7), usage(7)), true) + assert.equal(pollStorageQuotaFit(empty, empty), true) +}) + +test("GC is strictly after expiry plus retention/safety and persisted gcNotBefore", () => { + assert.equal(pollStorageGcEligible(candidate(), policy, boundary), false) + assert.equal(pollStorageGcEligible(candidate(), policy, boundary + 1), true) + assert.equal(pollStorageGcEligible(candidate({ gcNotBefore: boundary + DAY }), policy, boundary + DAY), false) + assert.equal(pollStorageGcEligible(candidate({ gcNotBefore: boundary + DAY }), policy, boundary + DAY + 1), true) + assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, kind: "mcp" } }), policy, boundary + 1), false) + assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, kind: "mcp" }, mcpTerminal: true }), policy, boundary + 1), true) +}) + +test("336 paid hourly histories and one heavy poll fit unchanged owner bytes and rows",()=>{ + const retained=compactUsage(1,336) + assert(retained.bytes{ + const good=compactUsage(0,1,40) + assert.equal(pollStorageQuotaFit(good,good),true) + assert.equal(pollStorageQuotaFit(compactUsage(0,1,131072),compactUsage(0,1,131072)),true) + const {archiveBytes:_bytes,...missingArchive}=good,{compactedAttempts:_count,...missingCount}=good + for(const value of [missingArchive,missingCount,{...good,archiveBytes:undefined},{...good,compactedAttempts:undefined}, + compactUsage(0,1,39),compactUsage(0,1,131073),{...good,bytes:good.bytes-1},{...good,rows:good.rows+1}, + {...good,compactedAttempts:-0},{...good,archiveBytes:NaN},{...good,opaque:true}, + {...usage(0),compactedAttempts:0,archiveBytes:1},compactUsage(0,Number.MAX_SAFE_INTEGER,40)]){ + assert.equal(pollStorageQuotaFit(value as typeof good,value as typeof good),false) + assert.equal(pollStorageUsageRemaining(value as typeof good,"owner"),undefined) + } + assert.equal(pollStorageQuotaFit({...empty,compactedAttempts:0,archiveBytes:0},empty),true) + assert.equal(pollStorageQuotaFit(compactUsage(0,1,41),compactUsage(0,1,40)),false,"No other compact admission can own the unexplained byte") +}) + +test("free heavy slots do not imply another full byte/row reservation fits",()=>{ + const owner=compactUsage(0,2000,40),global=compactUsage(0,32600,40) + assert(owner.bytes<=policy.ownerBytes);assert(global.bytes<=policy.globalBytes) + assert.equal(pollStorageUsageRemaining(owner,"owner"),0) + assert.equal(pollStorageUsageRemaining(global,"global"),0) + assert.equal(pollStorageQuotaFit(global,owner),false) + assert.equal(pollStorageUsageRemaining(compactUsage(0,2056,40),"owner"),undefined) +}) + +test("retention increases, persisted retention/safety, recent stages and long original authority all delay GC", () => { + assert.equal(pollStorageGcEligible(candidate(), { ...policy, retentionDays: 30 }, boundary + 1), false) + assert.equal(pollStorageGcEligible(candidate({ retentionDays: 30 }), policy, boundary + 1), false) + assert.equal(pollStorageGcEligible(candidate({ safetyWindowMs: DAY }), policy, boundary + 1), false) + assert.equal(pollStorageGcEligible(candidate({ lastActivityAt: boundary }), policy, boundary + 1), false) + assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, expiresAt: 2000 + 1000 * DAY } }), policy, boundary + 1), false) + assert.equal(pollStorageGcEligible(candidate(), { ...policy, safetyWindowMs: DAY }, boundary + 1), false) +}) + +test("GC fails closed for malformed metadata, unsafe arithmetic and Date overflow", () => { + for (const patch of [{ retentionDays: 0 }, { retentionDays: 366 }, { lastActivityAt: -1 }, { lastActivityAt: 999 }, + { lastActivityAt: boundary + 2 }, { gcNotBefore: NaN }, { gcNotBefore: Infinity }, { safetyWindowMs: 59999 }, { safetyWindowMs: DAY + 1 }]) { + assert.equal(pollStorageGcEligible(candidate(patch), policy, boundary + 1), false) + } + assert.equal(pollStorageGcEligible(candidate(), policy, NaN), false) + assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, expiresAt: 8640000000000000 } }), policy, 8640000000000000), false) +}) diff --git a/backend/src/services/server-poll-storage-model.ts b/backend/src/services/server-poll-storage-model.ts new file mode 100644 index 00000000..9f676a8d --- /dev/null +++ b/backend/src/services/server-poll-storage-model.ts @@ -0,0 +1,141 @@ +/** Pure poll-journal accounting, not MAC/terminal authority. Full reservations + * include unresolved work; compact history continues paying bytes and rows. + * Bytes are logical charges, never a claim about PostgreSQL relation size. */ +const ATTEMPT_BYTES = 8 * 1024 * 1024 +const ADMISSION_BYTES = 32768 +const ATTEMPT_ROWS = 4019 // anchor + 2008 intents + 2008 acks + receipt + quarantine +const DAY_MS = 86400000 +const DATE_MAX = 8640000000000000 + +export interface PollStoragePolicy { + attemptBytes: number + admissionBytes: number + attemptRows: number + globalAttempts: number + ownerAttempts: number + globalBytes: number + ownerBytes: number + globalRows: number + ownerRows: number + retentionDays: number + safetyWindowMs: number +} +export const SERVER_POLL_STORAGE_POLICY: Readonly = Object.freeze({ + attemptBytes: ATTEMPT_BYTES, admissionBytes: ADMISSION_BYTES, attemptRows: ATTEMPT_ROWS, + globalAttempts: 128, ownerAttempts: 8, + globalBytes: 128 * (ATTEMPT_BYTES + ADMISSION_BYTES), ownerBytes: 8 * (ATTEMPT_BYTES + ADMISSION_BYTES), + globalRows: 128 * ATTEMPT_ROWS, ownerRows: 8 * ATTEMPT_ROWS, + retentionDays: 14, safetyWindowMs: 60000, +}) + +export class PollStorageRefused extends Error { + readonly automaticReplay=false + constructor() { super("Poll storage state unavailable"); this.name = "PollStorageRefused" } +} +function refuse(): never { throw new PollStorageRefused() } +const integer = (value: unknown): value is number => typeof value === "number" && Number.isSafeInteger(value) && value >= 0 && !Object.is(value,-0) +const timestamp = (value: unknown): value is number => integer(value) && value <= DATE_MAX +const record = (value: unknown): value is Record => typeof value === "object" && value !== null && !Array.isArray(value) +const days = (value: unknown): value is number => integer(value) && value >= 1 && value <= 365 +const uuid = (value: unknown): value is string => typeof value === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value) +const hash = (value: unknown): value is string => typeof value === "string" && /^[a-f0-9]{64}$/.test(value) + +/** Trusted composition only. Fixed reservations/caps cannot be weakened by a + * caller; only retention and its safety window are configurable. */ +export function pollStoragePolicy(raw: unknown = SERVER_POLL_STORAGE_POLICY): Readonly { + if (!record(raw) || Object.keys(raw).some(key => !Object.hasOwn(SERVER_POLL_STORAGE_POLICY, key))) refuse() + for (const key of Object.keys(SERVER_POLL_STORAGE_POLICY) as (keyof PollStoragePolicy)[]) { + if (key !== "retentionDays" && key !== "safetyWindowMs" && raw[key] !== SERVER_POLL_STORAGE_POLICY[key]) refuse() + } + if (!days(raw.retentionDays) || !integer(raw.safetyWindowMs) || raw.safetyWindowMs < 60000 || raw.safetyWindowMs > DAY_MS) refuse() + return Object.freeze({ ...SERVER_POLL_STORAGE_POLICY, retentionDays: raw.retentionDays, safetyWindowMs: raw.safetyWindowMs }) +} + +export interface PollStorageScope { + receiptId: string + kind: "rest" | "mcp" + executionId: string + ownerId: string + bindingProof: string + contextProof: string + issuedAt: number + expiresAt: number +} +/** Expired scope parsing is exclusively for retained metadata, never a claim. */ +export function pollStorageScope(raw: unknown, options: { now: number; allowExpired?: boolean }): Readonly { + if (!record(raw) || !timestamp(options.now) || options.allowExpired !== undefined && typeof options.allowExpired !== "boolean" + || Object.keys(raw).some(key => !["receiptId", "kind", "executionId", "ownerId", "bindingProof", "contextProof", "issuedAt", "expiresAt"].includes(key)) + || !uuid(raw.receiptId) || !uuid(raw.executionId) || raw.kind !== "rest" && raw.kind !== "mcp" + || typeof raw.ownerId !== "string" || !raw.ownerId.length || raw.ownerId.length > 200 || raw.ownerId.trim() !== raw.ownerId || /[\x00-\x1f\x7f-\x9f]/.test(raw.ownerId) + || !hash(raw.bindingProof) || !hash(raw.contextProof) || !timestamp(raw.issuedAt) || !timestamp(raw.expiresAt) + || raw.expiresAt <= raw.issuedAt || raw.issuedAt > options.now || !options.allowExpired && options.now >= raw.expiresAt) refuse() + return Object.freeze({ receiptId: raw.receiptId, kind: raw.kind, executionId: raw.executionId, ownerId: raw.ownerId, + bindingProof: raw.bindingProof, contextProof: raw.contextProof, issuedAt: raw.issuedAt, expiresAt: raw.expiresAt }) +} + +export interface PollStorageUsage { attempts: number; bytes: number; rows: number; compactedAttempts?: number; archiveBytes?: number } +function usage(raw: PollStorageUsage) { + if (!record(raw)) refuse() + const compact = Object.hasOwn(raw,"compactedAttempts"), archive = Object.hasOwn(raw,"archiveBytes") + const keys = Object.keys(raw).sort().join(",") + if (compact !== archive || keys !== (compact ? "archiveBytes,attempts,bytes,compactedAttempts,rows" : "attempts,bytes,rows")) refuse() + const count = compact ? raw.compactedAttempts : 0, encoded = archive ? raw.archiveBytes : 0 + if (!integer(raw.attempts) || !integer(raw.bytes) || !integer(raw.rows) || !integer(count) || !integer(encoded) + || encoded < count * 40 || encoded > count * 131072) refuse() + const expectedRows = raw.attempts * ATTEMPT_ROWS + count * 2 + const expectedBytes = raw.attempts * (ATTEMPT_BYTES + ADMISSION_BYTES) + count * ADMISSION_BYTES + encoded + if (!integer(expectedRows) || !integer(expectedBytes) || raw.rows !== expectedRows || raw.bytes !== expectedBytes) refuse() + return { ...raw, compactedAttempts:count, archiveBytes:encoded } +} +/** Available NEW full reservations under every unchanged cap. SQL aggregate + * equality and all retained charge MACs must be checked by the admission owner; + * these arithmetic checks cannot authenticate compaction or free UNKNOWN. */ +export function pollStorageUsageRemaining(raw: PollStorageUsage, scope: "global"|"owner", rawPolicy:PollStoragePolicy=SERVER_POLL_STORAGE_POLICY):number|undefined { + try { + const policy=pollStoragePolicy(rawPolicy), value=usage(raw) + if(scope!=="global"&&scope!=="owner")refuse() + const attempts=scope==="global"?policy.globalAttempts:policy.ownerAttempts + const bytes=scope==="global"?policy.globalBytes:policy.ownerBytes, rows=scope==="global"?policy.globalRows:policy.ownerRows + if(value.attempts>attempts||value.bytes>bytes||value.rows>rows)refuse() + return Math.min(attempts-value.attempts,Math.floor((bytes-value.bytes)/(policy.attemptBytes+policy.admissionBytes)),Math.floor((rows-value.rows)/policy.attemptRows)) + }catch{return undefined} +} +/** Exact counters, including full reservations and paid retained archives. */ +export function pollStorageQuotaFit(globalUsage: PollStorageUsage, ownerUsage: PollStorageUsage, + rawPolicy: PollStoragePolicy = SERVER_POLL_STORAGE_POLICY): boolean { + try { + const policy = pollStoragePolicy(rawPolicy) + const global=usage(globalUsage),owner=usage(ownerUsage) + return (pollStorageUsageRemaining(global,"global",policy)??0)>0 && (pollStorageUsageRemaining(owner,"owner",policy)??0)>0 + && owner.attempts<=global.attempts && owner.bytes<=global.bytes && owner.rows<=global.rows + && owner.compactedAttempts<=global.compactedAttempts && owner.archiveBytes<=global.archiveBytes + && global.archiveBytes-owner.archiveBytes >= (global.compactedAttempts-owner.compactedAttempts)*40 + && global.archiveBytes-owner.archiveBytes <= (global.compactedAttempts-owner.compactedAttempts)*131072 + } catch { return false } +} + +export interface PollStorageGcCandidate { + scope: PollStorageScope + lastActivityAt: number + gcNotBefore: number + retentionDays: number + safetyWindowMs: number + /** Caller must verify the actual operation, never infer terminality from a receipt. */ + mcpTerminal: boolean +} +/** No forced-delete branch: malformed/overflowed dates and live MCP operations + * remain reserved. Increasing retention applies to already persisted attempts. */ +export function pollStorageGcEligible(candidate: PollStorageGcCandidate, rawPolicy: PollStoragePolicy, now: number): boolean { + try { + const policy = pollStoragePolicy(rawPolicy) + if (!record(candidate) || !timestamp(now)) return false + const scope = pollStorageScope(candidate.scope, { now, allowExpired: true }) + if (!timestamp(candidate.lastActivityAt) || candidate.lastActivityAt < scope.issuedAt || candidate.lastActivityAt > now + || !timestamp(candidate.gcNotBefore) || !days(candidate.retentionDays) || !integer(candidate.safetyWindowMs) + || candidate.safetyWindowMs < 60000 || candidate.safetyWindowMs > DAY_MS || typeof candidate.mcpTerminal !== "boolean" + || scope.kind === "mcp" && !candidate.mcpTerminal) return false + const retainedUntil = Math.max(scope.expiresAt, candidate.lastActivityAt) + + Math.max(policy.retentionDays, candidate.retentionDays) * DAY_MS + Math.max(policy.safetyWindowMs, candidate.safetyWindowMs) + return timestamp(retainedUntil) && now > Math.max(candidate.gcNotBefore, retainedUntil) + } catch { return false } +} diff --git a/backend/src/services/server-poll-storage-restore-evidence.sql-helper.ts b/backend/src/services/server-poll-storage-restore-evidence.sql-helper.ts new file mode 100644 index 00000000..d65738c8 --- /dev/null +++ b/backend/src/services/server-poll-storage-restore-evidence.sql-helper.ts @@ -0,0 +1,33 @@ +/** Owned SQL evidence only. Hashes include opaque ciphertext/claim tokens; + * these values are never printed or written to the expectation artifact. */ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import type { Pool } from "pg" +export const POLL_STORAGE_TABLE_KEYS=Object.freeze({ + server_poll_executions:"receipt_id",server_poll_intents:"receipt_id,stage",server_poll_acks:"receipt_id,stage", + server_poll_receipts:"receipt_id",server_poll_quarantines:"receipt_id",server_poll_storage_admissions:"receipt_id",server_poll_storage_usage:"scope", + server_poll_archives:"receipt_id", +}) +function canonical(value:unknown,key=""):unknown{ + if(Array.isArray(value))return value.map(item=>canonical(item)) + if(value&&typeof value==="object")return Object.fromEntries(Object.entries(value).sort(([a],[b])=>a.localeCompare(b)).map(([name,item])=>[name,canonical(item,name)])) + if(typeof value==="string"&&(key.endsWith("_at")||key==="gc_not_before")){ + // JSONB preserves PostgreSQL microseconds; JS Date alone would silently + // discard the last three digits and hide restore corruption. + const stamp=/^(.*T\d{2}:\d{2}:\d{2})(?:\.(\d{1,6}))?(Z|[+-]\d{2}:\d{2})$/.exec(value) + if(stamp&&Number.isFinite(Date.parse(stamp[1]+stamp[3])))return new Date(stamp[1]+stamp[3]).toISOString().slice(0,19)+"."+(stamp[2]??"").padEnd(6,"0")+"Z" + } + return value +} +export async function pollStorageRestoreEvidence(pool:Pool){ + assert(process.env.MMAPP_OWN_SERVER_POLL_STORAGE==="true"||process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH==="true"||process.env.MMAPP_OWN_SERVER_POLL_STORAGE_RESTORE==="true") + const source=new URL(process.env.DATABASE_URL??"");assert.equal(source.hostname,"127.0.0.1");assert.equal(source.pathname,"/postgres") + const precision=(await pool.query("SELECT to_jsonb(t) AS value FROM (SELECT '2026-10-01T03:00:00.123456+03:00'::timestamptz AS first_at,'2026-10-01T00:00:00.123457Z'::timestamptz AS second_at) t")).rows[0].value + assert.deepEqual(canonical(precision),{first_at:"2026-10-01T00:00:00.123456Z",second_at:"2026-10-01T00:00:00.123457Z"},"Actual PG timestamp evidence must preserve one-microsecond differences") + const tables:Record={} + for(const [name,keys] of Object.entries(POLL_STORAGE_TABLE_KEYS)){ + const rows=(await pool.query(`SELECT to_jsonb(t) AS value FROM public.${name} t ORDER BY ${keys}`)).rows.map(row=>canonical(row.value)) + tables[name]={rows:rows.length,sha256:createHash("sha256").update(JSON.stringify(rows)).digest("hex")} + } + return {version:1 as const,tables} +} diff --git a/backend/src/services/server-poll-storage-restore.sql.test.ts b/backend/src/services/server-poll-storage-restore.sql.test.ts new file mode 100644 index 00000000..c6665ca3 --- /dev/null +++ b/backend/src/services/server-poll-storage-restore.sql.test.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import { basename,dirname,resolve } from "node:path" +assert.equal(process.env.MMAPP_OWN_SERVER_POLL_STORAGE_RESTORE,"true") +const endpoint=new URL(process.env.DATABASE_URL??"") +assert.equal(endpoint.hostname,"127.0.0.1");assert.equal(endpoint.pathname,"/postgres") +assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const {pool,closePool}=await import("../db/index.js") +const {pollStorageRestoreEvidence,POLL_STORAGE_TABLE_KEYS}=await import("./server-poll-storage-restore-evidence.sql-helper.js") +const {createServerPollStorage}=await import("./server-poll-storage.js") +const {PollStorageRefused}=await import("./server-poll-storage-model.js") +try{ + const expected=JSON.parse(await readFile(resolve(process.env.MCP_STATE_DIR!,"poll-storage-owned-restore-evidence.json"),"utf8")) + const actual=await pollStorageRestoreEvidence(pool) + assert.deepEqual(actual,expected,"Every poll admission/token/ciphertext/stage/quarantine/counter must survive actual terminal restore byte-for-byte in canonical data form") + const counts=(await pool.query("SELECT(SELECT count(*) FROM server_poll_storage_admissions)::int AS actual,admissions::int AS heavy,compacted_admissions::int AS compacted,(admissions+compacted_admissions)::int AS charged FROM server_poll_storage_usage WHERE scope='global'")).rows[0] + assert.equal(counts.actual,counts.charged);assert.equal(counts.heavy,128) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_usage u WHERE u.scope<>'global' AND (u.admissions<>(SELECT count(*) FROM server_poll_storage_admissions a WHERE u.scope='owner:'||a.owner_id AND a.storage_state='full') OR u.compacted_admissions<>(SELECT count(*) FROM server_poll_storage_admissions a WHERE u.scope='owner:'||a.owner_id AND a.storage_state='compacted'))")).rows[0].n,0) + const fresh={receiptId:randomUUID(),kind:"rest" as const,executionId:randomUUID(),ownerId:"owned-after-restore-"+randomUUID(),bindingProof:"a".repeat(64),contextProof:"b".repeat(64),issuedAt:Date.now()-1000,expiresAt:Date.now()+60000} + await assert.rejects(createServerPollStorage({scope:fresh,connectionId:null,maxStages:8,authorize:async()=>{}}).claim({deadlineAt:Date.now()+10000},async()=>assert.fail("Restored global quota must prevent any new anchor")),PollStorageRefused) + assert.deepEqual(await pollStorageRestoreEvidence(pool),expected,"Refused fresh admission must not alter restored counters or spend an extra token") + console.log("Owned poll storage AFTER terminal restore:",JSON.stringify({tablesCompared:Object.keys(POLL_STORAGE_TABLE_KEYS).length,exactClaimTokensAndCiphertextHashes:true,microsecondsPreserved:true,actualAdmissions:counts.actual,chargedAdmissions:counts.charged,heavyAdmissions:counts.heavy,compactedAdmissions:counts.compacted,allOwnerCountersReconciled:true,restoredGlobalQuotaStillEnforced:true,refusedAttemptChangesNoRows:true,newNativeDispatches:0,productionDatabaseTouched:false})) +}finally{await closePool()} diff --git a/backend/src/services/server-poll-storage-retention.test.ts b/backend/src/services/server-poll-storage-retention.test.ts new file mode 100644 index 00000000..56485c86 --- /dev/null +++ b/backend/src/services/server-poll-storage-retention.test.ts @@ -0,0 +1,209 @@ +import assert from "node:assert/strict" +import { createHash, createHmac, randomUUID } from "node:crypto" +import { test } from "node:test" +import { gzipSync } from "node:zlib" +import { fingerprint } from "../modules/mcp/security.js" +import { collectExpiredServerPollStorageForTests } from "./server-poll-storage-retention.js" +import { PollStorageRefused, PollStorageUnknown } from "./server-poll-storage.js" +import type { ProbeStorageOptions } from "./server-probe-journal-maintenance.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" + +const tables = ["server_poll_acks", "server_poll_intents", "server_poll_receipts", "server_poll_quarantines", "server_poll_executions", "server_poll_archives", "server_poll_storage_admissions"] +const deferred = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function fixture(openRetired?: Parameters[1]) { + const now = Date.now(), candidates: Record[] = [{ receipt_id: randomUUID(), owner_id: "owned-gc", archive_version: null }, + { receipt_id: randomUUID(), owner_id: "owned-gc", archive_version: 1 }] + const retiredRows: Record[] = [] + const commands: { text: string; values: unknown[]; timeout: number }[] = [], releases: boolean[] = [], options: ProbeStorageOptions[] = [] + let settings: Record[] = [{ requests_retention_days: 30 }], fail = "", residual = "", removed: Record[] | undefined, hold: ReturnType | undefined,writer=true + const entered = deferred() + const run = () => collectExpiredServerPollStorageForTests({ now: () => now, connect: async accepted => { + options.push(accepted) + return { async query(text, values = [], queryOptions) { + commands.push({ text, values, timeout: queryOptions!.timeoutMs }) + if (text === fail) throw Error("PRIVATE_DRIVER_SECRET") + if (text.includes("pg_try_advisory_xact_lock_shared")) return { rows: [{ acquired:writer }] } + if (text.includes("current_setting('lock_timeout')")) return { rows: [{ lock_timeout: "0" }] } + // GC transaction/ordering fixture only. Authentic compact MAC and complete + // counter conservation are covered by archive-accounting component tests. + if (text.startsWith("SELECT a.receipt_id AS admission_receipt_id") || text.startsWith("SELECT r.receipt_id FROM server_poll_archives")) return { rows: [] } + if (text.startsWith("SELECT scope,admissions,compacted_admissions,archive_bytes,reserved_rows,reserved_bytes")) + return { rows: [{ scope: "global", admissions: 0, compacted_admissions: 0, archive_bytes: 0, reserved_rows: 0, reserved_bytes: 0 }] } + if (text.includes("requests_retention_days")) { entered.resolve(); if (hold) await hold.promise; return { rows: settings } } + if (text.startsWith("SELECT receipt_id,owner_id")) return { rows: structuredClone(candidates) } + if (text.startsWith("SELECT r.version,r.receipt_id")) return { rows: structuredClone(retiredRows) } + if (text.startsWith("DELETE FROM server_poll_storage_admissions")) return { rows: removed ?? candidates.map(row => ({ receipt_id: row.receipt_id })) } + if (text.startsWith("SELECT receipt_id FROM ") && text.includes("FROM " + residual + " ")) return { rows: [{ receipt_id: candidates[0]?.receipt_id }] } + return { rows: [] } + }, release(unsafe = false) { releases.push(unsafe) } } + } }, openRetired) + return { run, now, candidates, retiredRows, commands, releases, options, entered: entered.promise, block: () => { hold = deferred(); return hold },setWriter:(value:boolean)=>{writer=value}, + setting: (rows: Record[]) => { settings = rows }, fail: (text: string) => { fail = text }, residual: (table: string) => { residual = table }, removed: (rows: Record[]) => { removed = rows } } +} + +test("only a definite shared xact lock miss defers GC before POLL storage or a retention read",async()=>{ + const fixtureBlocked=fixture();fixtureBlocked.setWriter(false) + assert.deepEqual(await fixtureBlocked.run(),{collected:0,automaticReplay:false}) + const lock="SELECT pg_catalog.pg_try_advisory_xact_lock_shared($1::bigint) AS acquired" + assert.deepEqual(fixtureBlocked.commands.map(command=>command.text),["BEGIN",lock,"COMMIT"]) + const unexpected=fixture();unexpected.fail(lock) + await assert.rejects(unexpected.run(),PollStorageRefused) + assert.deepEqual(unexpected.releases,[true]) +}) + +test("selected GC owns one bounded backend, locks current retention, deletes all children and verifies trigger refund outcome", async () => { + const f = fixture() + assert.deepEqual(await f.run(), { collected: 2, automaticReplay: false }) + assert.equal(f.options.length, 1); assert.equal(f.options[0].deadlineAt, f.now + 20000) + assert(f.commands.every(command => command.timeout >= 1 && command.timeout <= 5000)) + const texts = f.commands.map(command => command.text), setting = texts.findIndex(text => text.includes("requests_retention_days")) + const writer = texts.findIndex(text => text.includes("pg_advisory_xact_lock_shared(")), storage = texts.findIndex(text => text.includes("server-poll-storage-v1")) + assert(writer > 0 && writer < storage && storage < setting) + assert(texts[setting].endsWith("FOR SHARE")); assert(setting < texts.indexOf("SET LOCAL mm.poll_storage_gc='on'")) + const selection = texts.find(text => text.startsWith("SELECT receipt_id,owner_id"))! + assert(selection.includes("gc_not_before row.receipt_id) + for (const table of tables) { + const deletion = f.commands.find(command => command.text.startsWith(`DELETE FROM ${table} WHERE`))! + assert.deepEqual(deletion.values, [ids]) + assert(texts.some(text => text === `SELECT receipt_id FROM ${table} WHERE receipt_id=ANY($1::uuid[]) LIMIT 1`)) + } + const refund = f.commands.find(command => command.text.startsWith("DELETE FROM server_poll_storage_usage"))! + assert.deepEqual(refund.values, [["owner:owned-gc"]]); assert(refund.text.includes("admissions=0 AND compacted_admissions=0 AND archive_bytes=0 AND reserved_rows=0 AND reserved_bytes=0")) + const archiveDelete = texts.findIndex(text => text.startsWith("DELETE FROM server_poll_archives")) + assert(archiveDelete > texts.findIndex(text => text.startsWith("DELETE FROM server_poll_executions"))) + assert(archiveDelete < texts.findIndex(text => text.startsWith("DELETE FROM server_poll_storage_admissions"))) + const accounting = texts.findIndex(text => text.startsWith("SELECT scope,admissions,compacted_admissions")) + assert(accounting > texts.indexOf(selection)) + assert(accounting < texts.findIndex(text => text.startsWith("DELETE FROM"))) + assert(texts.indexOf("COMMIT") > texts.findIndex(text => text.startsWith("SELECT receipt_id FROM server_poll_storage_admissions"))) + assert.deepEqual(f.releases, [false]) +}) + +async function retiredFixture() { + const { createServerPollRetiredArchiveForTests } = await import("./server-poll-retired-archive.js") + const proof = async (purpose: string, value: unknown) => + createHmac("sha256", "gc-fixture-only").update(purpose).update(fingerprint(value)).digest("hex") + const sha = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") + let opened = 0 + const codec = createServerPollRetiredArchiveForTests({ + proof, + decrypt: async value => { opened++; return value }, + encrypt: async () => { throw new Error("Unexpected archive write") }, + openV1: async () => { throw new Error("Unexpected archive fallback") }, + verifyHistory: async () => { throw new Error("Unexpected historical fallback") }, + }) + const f = fixture(codec.open) + const selected = f.candidates[1] + selected.archive_version = 2 + const timestamp = "2026-10-01T00:00:00.123456Z", gcNotBefore = "2026-11-01T00:00:00.123456Z" + const history = { version: 2, operation: "servers.poll", operationVersion: 1, planId: randomUUID(), serverId: 3, + affectedServerIds: [3], gcNotBefore, terminalAt: timestamp, + previous: { archiveProof: "1".repeat(64), sourceDigest: "2".repeat(64), sourceSha256: "3".repeat(64) }, + history: { createdAt: timestamp, completedAt: timestamp, stageCount: 12, publications: 8, + nativeReads: 4, observation: "online", verification: "passed" } } + const plaintext = Buffer.from(JSON.stringify(history)), compressed = gzipSync(plaintext), ciphertext = compressed.toString("base64") + const metadata = { version: 2, receipt_id: selected.receipt_id, execution_id: randomUUID(), connection_id: randomUUID(), owner_id: selected.owner_id, + binding_proof: "a".repeat(64), context_proof: "b".repeat(64), terminal_digest: "c".repeat(64), + original_activity_at: timestamp, original_expires_at: timestamp, retention_days: 30, safety_ms: 60000, + source_digest: await proof("server-poll-retired-archive-source-v2", sha(plaintext)), source_sha256: sha(plaintext), + decoded_bytes: plaintext.length, compressed_bytes: compressed.length, ciphertext_bytes: ciphertext.length, ciphertext_sha256: sha(ciphertext) } + const archiveProof = await proof("server-poll-retired-archive-record-v2", { ...metadata, identity_kind: "mcp", + storage_state: "compacted", admission_bytes: 32768, charged_rows: 2 }) + f.retiredRows.push({ ...metadata, ciphertext, archive_proof: archiveProof, admission_gc_not_before: gcNotBefore }) + return { ...f, opened: () => opened } +} + +test("GC opens only selected V2 under a row lock and authenticates the original lifetime before any DELETE", async () => { + const f = await retiredFixture() + assert.deepEqual(await f.run(), { collected: 2, automaticReplay: false }) + assert.equal(f.opened(), 1) + const index = f.commands.findIndex(command => command.text.startsWith("SELECT r.version,r.receipt_id")) + assert.deepEqual(f.commands[index].values, [f.candidates[1].receipt_id]) + assert(f.commands[index].text.endsWith("FOR UPDATE OF r")) + assert(index < f.commands.findIndex(command => command.text.startsWith("DELETE"))) +}) + +test("missing V2, forged ciphertext or proof, and microsecond lifetime drift refuse before any DELETE", async () => { + for (const fault of ["missing", "ciphertext", "actual-hash", "proof", "lifetime", "expiry", "version"] as const) { + const f = await retiredFixture(), row = f.retiredRows[0] + if (fault === "missing") f.retiredRows.length = 0 + if (fault === "ciphertext") row.ciphertext = "A" + String(row.ciphertext).slice(1) + if (fault === "actual-hash") row.ciphertext_sha256 = "a".repeat(64) + if (fault === "proof") row.archive_proof = "a".repeat(64) + if (fault === "lifetime") row.admission_gc_not_before = "2026-11-01T00:00:00.123455Z" + if (fault === "expiry") row.original_expires_at = "2026-10-01T00:00:00.123455Z" + if (fault === "version") f.candidates[1].archive_version = 3 + await assert.rejects(f.run(), PollStorageRefused) + assert.equal(f.opened(), fault === "lifetime" ? 1 : 0) + assert(!f.commands.some(command => command.text.startsWith("DELETE") || command.text === "COMMIT")) + assert.equal(f.commands.filter(command => command.text === "ROLLBACK").length, 1) + } +}) + +test("empty candidate set returns zero without deleting or refunding", async () => { + const f = fixture(); f.candidates.length = 0 + assert.deepEqual(await f.run(), { collected: 0, automaticReplay: false }) + assert(!f.commands.some(command => command.text.includes("pg_catalog.sha256") || command.text.startsWith("SELECT scope,admissions,compacted_admissions")), + "An idle GC tick must not hash or hydrate the retained history") + assert(!f.commands.some(command => command.text.startsWith("DELETE"))); assert.deepEqual(f.releases, [false]) +}) + +test("missing, duplicate or invalid authoritative retention never enables GC or deletes", async () => { + for (const rows of [[], [{ requests_retention_days: 0 }], [{ requests_retention_days: 366 }], [{ requests_retention_days: "30" }], [{ requests_retention_days: 1.5 }], [{ requests_retention_days: 30 }, { requests_retention_days: 30 }]]) { + const f = fixture(); f.setting(rows) + await assert.rejects(f.run(), PollStorageRefused) + assert(!f.commands.some(command => /^(?:DELETE|SET LOCAL mm\.poll_storage_gc)/.test(command.text))) + assert.equal(f.commands.filter(command => command.text === "ROLLBACK").length, 1); assert.deepEqual(f.releases, [true]) + } +}) + +test("candidate overflow, duplicate or malformed identity fails before deletion", async () => { + for (const change of [(f: ReturnType) => { while (f.candidates.length < 9) f.candidates.push({ receipt_id: randomUUID(), owner_id: "owned-gc" }) }, + (f: ReturnType) => { f.candidates[1] = { ...f.candidates[0] } }, + (f: ReturnType) => { f.candidates[0].receipt_id = "not-uuid" }]) { + const f = fixture(); change(f); await assert.rejects(f.run(), PollStorageRefused) + assert(!f.commands.some(command => command.text.startsWith("DELETE"))); assert.deepEqual(f.releases, [true]) + } +}) + +test("missing admission refund or residual journal row prevents COMMIT", async () => { + for (const corrupt of [(f: ReturnType) => { f.removed([]) }, (f: ReturnType) => { f.residual("server_poll_receipts") }, + (f: ReturnType) => { f.residual("server_poll_archives") }]) { + const f = fixture(); corrupt(f); await assert.rejects(f.run(), PollStorageRefused) + assert(!f.commands.some(command => command.text === "COMMIT")); assert(f.commands.some(command => command.text === "ROLLBACK")); assert.deepEqual(f.releases, [true]) + } +}) + +test("lost GC COMMIT acknowledgement destroys once with no rollback, retry or provider leak", async () => { + const f = fixture(); f.fail("COMMIT") + await assert.rejects(f.run(), error => error instanceof PollStorageUnknown && !error.message.includes("PRIVATE_DRIVER_SECRET")) + assert.equal(f.options.length, 1); assert.equal(f.commands.filter(command => command.text === "COMMIT").length, 1) + assert(!f.commands.some(command => command.text === "ROLLBACK")); assert.deepEqual(f.releases, [true]) +}) + +test("SQL failure rolls back once and destroys the sole backend", async () => { + const f = fixture(); f.fail("DELETE FROM server_poll_intents WHERE receipt_id=ANY($1::uuid[])") + await assert.rejects(f.run(), error => error instanceof PollStorageRefused && !error.message.includes("PRIVATE_DRIVER_SECRET")) + assert.equal(f.commands.filter(command => command.text === "ROLLBACK").length, 1) + assert(!f.commands.some(command => command.text === "COMMIT")); assert.equal(f.options.length, 1); assert.deepEqual(f.releases, [true]) +}) + +test("shared database replacement waits for the full active GC lease and refuses a new GC while paused", async () => { + const f = fixture(), resume = f.block(); const pending = f.run(); await f.entered + let replaced = false + const replacement = sharedDatabaseGate.exclusive(async () => { replaced = true; assert.deepEqual(f.releases, [false]) }) + await Promise.resolve(); assert.equal(replaced, false) + const other = fixture(); await assert.rejects(other.run(), error => (error as { statusCode?: number }).statusCode === 409); assert.equal(other.options.length, 0) + resume.resolve(); await pending; await replacement; assert.equal(replaced, true) +}) + +test("simultaneous GC calls each retain their bounded owned transaction and drain before replacement", async () => { + const a = fixture(), b = fixture(), resumeA = a.block(), resumeB = b.block() + const first = a.run(), second = b.run(); await Promise.all([a.entered, b.entered]) + let replaced = false; const replacement = sharedDatabaseGate.exclusive(async () => { replaced = true }) + resumeA.resolve(); await first; assert.equal(replaced, false) + resumeB.resolve(); await second; await replacement + assert.equal(replaced, true) + for (const f of [a, b]) { assert.equal(f.options.length, 1); assert.equal(f.options[0].deadlineAt, f.now + 20000); assert.deepEqual(f.releases, [false]) } +}) diff --git a/backend/src/services/server-poll-storage-retention.ts b/backend/src/services/server-poll-storage-retention.ts new file mode 100644 index 00000000..712feb10 --- /dev/null +++ b/backend/src/services/server-poll-storage-retention.ts @@ -0,0 +1,74 @@ +/** Private fixed poll GC. Not scheduled or exposed by this opt-in module. */ +import { sharedDatabaseGate } from "./shared-database-gate.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import { admitPollStorageWriter, runPollStorageTransaction, tryAdmitPollStorageMaintenanceWriter, type PollStorageDependencies } from "./server-poll-storage.js" +import { PollStorageRefused } from "./server-poll-storage-model.js" +import { assertServerPollArchiveAccounting } from "./server-poll-archive-accounting.js" +import type { openServerPollRetiredArchive } from "./server-poll-retired-archive.js" + +const uuid=/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i +const fail=():never=>{throw new PollStorageRefused()} +const production:PollStorageDependencies={connect:connectProbeStorage,now:Date.now} +const openRetiredArchive:typeof openServerPollRetiredArchive=async row=> + (await import("./server-poll-retired-archive.js")).openServerPollRetiredArchive(row) +const retiredCandidate = `SELECT r.version,r.receipt_id,r.execution_id,r.connection_id,r.owner_id, + r.binding_proof,r.context_proof,r.terminal_digest,r.source_digest,r.source_sha256, + r.decoded_bytes,r.compressed_bytes,r.ciphertext_bytes,r.ciphertext,r.archive_proof,a.retention_days,a.safety_ms, + to_char(r.original_activity_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_activity_at, + to_char(r.original_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_expires_at, + to_char(a.gc_not_before AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS admission_gc_not_before, + pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8')),'hex') AS ciphertext_sha256 + FROM server_poll_archives r JOIN server_poll_storage_admissions a USING(receipt_id) + WHERE r.receipt_id=$1 AND r.version=2 AND a.storage_state='compacted' AND a.identity_kind='mcp' + AND r.execution_id=a.execution_id AND r.connection_id=a.connection_id AND r.owner_id=a.owner_id + AND r.binding_proof=a.binding_proof AND r.context_proof=a.context_proof + AND r.original_activity_at=a.original_activity_at AND r.original_expires_at=a.authority_expires_at + AND r.ciphertext_bytes=a.archive_bytes AND r.ciphertext_bytes=octet_length(r.ciphertext) + FOR UPDATE OF r` + +async function collect(deps:PollStorageDependencies, openRetired:typeof openServerPollRetiredArchive){ + const deadlineAt=deps.now()+20000 + return runPollStorageTransaction(deps,{deadlineAt},async()=>{},async sql=>{ + // Hold the authoritative setting against concurrent updates until COMMIT. + // Each DELETE guard additionally checks its current value independently. + const setting=(await sql.query("SELECT requests_retention_days FROM mcp_settings WHERE id=1 FOR SHARE")).rows + if(setting.length!==1||!Number.isSafeInteger(setting[0].requests_retention_days)||Number(setting[0].requests_retention_days)<1||Number(setting[0].requests_retention_days)>365)fail() + await sql.query("SET LOCAL mm.poll_storage_gc='on'") + const rows=(await sql.query(`SELECT receipt_id,owner_id, + (SELECT version FROM server_poll_archives WHERE receipt_id=a.receipt_id) AS archive_version + FROM server_poll_storage_admissions a WHERE gc_not_before8||rows.some(row=>typeof row.receipt_id!=="string"||!uuid.test(row.receipt_id)||typeof row.owner_id!=="string" + || (row.archive_version!==null&&row.archive_version!==1&&row.archive_version!==2))||new Set(rows.map(row=>row.receipt_id)).size!==rows.length)fail() + if(!rows.length)return {collected:0,automaticReplay:false as const} + await assertServerPollArchiveAccounting(sql) + for(const candidate of rows){ + if(candidate.archive_version!==2)continue + const locked=(await sql.query(retiredCandidate,[candidate.receipt_id])).rows + if(locked.length!==1||locked[0].version!==2||locked[0].receipt_id!==candidate.receipt_id||locked[0].owner_id!==candidate.owner_id)fail() + const {history}=await openRetired(locked[0]) + if(history.gcNotBefore!==locked[0].admission_gc_not_before)fail() + } + const ids=rows.map(row=>row.receipt_id),owners=[...new Set(rows.map(row=>row.owner_id))] + // No payload-only shortcut: all dependent rows, admission and its exact + // trigger-owned refund commit together. Any missing guard/refund aborts. + for(const table of ["server_poll_acks","server_poll_intents","server_poll_receipts","server_poll_quarantines","server_poll_executions"]){ + await sql.query(`DELETE FROM ${table} WHERE receipt_id=ANY($1::uuid[])`,[ids]) + } + await sql.query("DELETE FROM server_poll_archives WHERE receipt_id=ANY($1::uuid[])",[ids]) + const removed=(await sql.query("DELETE FROM server_poll_storage_admissions WHERE receipt_id=ANY($1::uuid[]) RETURNING receipt_id",[ids])).rows + if(removed.length!==ids.length||removed.some(row=>!ids.includes(row.receipt_id))||new Set(removed.map(row=>row.receipt_id)).size!==ids.length)fail() + await sql.query("DELETE FROM server_poll_storage_usage WHERE scope=ANY($1::text[]) AND admissions=0 AND compacted_admissions=0 AND archive_bytes=0 AND reserved_rows=0 AND reserved_bytes=0",[owners.map(owner=>"owner:"+owner)]) + for(const table of ["server_poll_acks","server_poll_intents","server_poll_receipts","server_poll_quarantines","server_poll_executions","server_poll_archives","server_poll_storage_admissions"]){ + if((await sql.query(`SELECT receipt_id FROM ${table} WHERE receipt_id=ANY($1::uuid[]) LIMIT 1`,[ids])).rows.length)fail() + } + return {collected:ids.length,automaticReplay:false as const} + },async sql=>{ + if(!await tryAdmitPollStorageMaintenanceWriter(sql,"transaction"))return {deferred:{collected:0,automaticReplay:false as const}} + await admitPollStorageWriter(sql,deadlineAt) + }) +} +export const collectExpiredServerPollStorage=()=>sharedDatabaseGate.request(()=>collect(production,openRetiredArchive)) +/** Explicit owned SQL composition; does not start a worker. */ +export const collectExpiredServerPollStorageForTests=(deps:PollStorageDependencies,openRetired=openRetiredArchive)=> + sharedDatabaseGate.request(()=>collect(deps,openRetired)) diff --git a/backend/src/services/server-poll-storage.sql.test.ts b/backend/src/services/server-poll-storage.sql.test.ts new file mode 100644 index 00000000..9403fab8 --- /dev/null +++ b/backend/src/services/server-poll-storage.sql.test.ts @@ -0,0 +1,222 @@ +import assert from "node:assert/strict" +import { createHash,randomUUID } from "node:crypto" +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import { readFile,writeFile } from "node:fs/promises" +import { basename,dirname,resolve } from "node:path" +import type { ProbeStorageSql,ProbeStorageOptions } from "./server-probe-journal-maintenance.js" +import type { PollStorageScope } from "./server-poll-storage-model.js" + +assert.equal(process.env.MMAPP_OWN_SERVER_POLL_STORAGE,"true") +const endpoint=new URL(process.env.DATABASE_URL??"") +assert.equal(endpoint.hostname,"127.0.0.1");assert.equal(endpoint.pathname,"/postgres") +assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const {pool,closePool}=await import("../db/index.js") +const {applyLegacyPollStorageFixtureMigrations:applySqlMigrations}=await import("../db/legacy-poll-storage-fixture.js") +const {connectProbeStorage}=await import("./server-probe-storage-connection.js") +const {createServerPollStorage,createServerPollStorageForTests,PollStorageUnknown}=await import("./server-poll-storage.js") +const {collectExpiredServerPollStorage,collectExpiredServerPollStorageForTests}=await import("./server-poll-storage-retention.js") +const {PollStorageRefused}=await import("./server-poll-storage-model.js") +const proof="a".repeat(64),contextProof="b".repeat(64),cases:string[]=[] +const options=():ProbeStorageOptions=>({deadlineAt:Date.now()+20000}) +function scope(ownerId="owned-"+randomUUID(),expiresAt=Date.now()+180000):PollStorageScope{return {receiptId:randomUUID(),kind:"rest",executionId:randomUUID(),ownerId,bindingProof:proof,contextProof,issuedAt:Date.now()-1000,expiresAt}} +function binding(value=scope()){return {scope:value,connectionId:null,maxStages:8,authorize:async()=>{assert(Date.now(){ + await sql.query("INSERT INTO server_poll_executions(receipt_id,identity_kind,execution_id,binding_proof,context_ciphertext,context_proof) VALUES($1,$2,$3,$4,'SYNTHETIC-CIPHERTEXT',$5)",[value.receiptId,value.kind,value.executionId,value.bindingProof,value.contextProof]) +} +const usage=async(ownerId:string)=>(await pool.query("SELECT admissions::int,reserved_rows::int,reserved_bytes::bigint::text FROM server_poll_storage_usage WHERE scope=$1",["owner:"+ownerId])).rows[0] +const historical:PollStorageScope[]=[],connectionId=randomUUID(),pendingOperation=randomUUID(),pendingPlan=randomUUID(),workerConnection=randomUUID(),workerOperation=randomUUID(),workerPlan=randomUUID() +try{ + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n,38) + // The actual migration runs on ONE backend/transaction. Synthetic aged + // records are seeded between table DDL and guard installation. Every file + // statement, including its real terminal marker, executes unchanged. + const proposal=await readFile(new URL("../../drizzle/0039_server_poll_storage.sql",import.meta.url),"utf8") + const boundary=proposal.indexOf("-- Every guard");assert(boundary>0) + const setup=await pool.connect() + try{ + await setup.query(proposal.slice(0,boundary)) + const old=new Date(Date.now()-40*86400000),issued=new Date(old.getTime()-3600000) + await setup.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,created_at) VALUES($1,'SYNTHETIC','historical-mcp','[]','{}','synthetic',$2)",[connectionId,issued]) + await setup.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,created_at) VALUES($1,$2,'servers.poll',1,1,'synthetic','{}','{}','approved',$3,$4)",[pendingPlan,connectionId,old,issued]) + await setup.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,created_at) VALUES($1,$2,$3,$5,'running',$4)",[pendingOperation,pendingPlan,connectionId,issued,pendingOperation]) + await setup.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,created_at) VALUES($1,'SYNTHETIC','historical-worker','[]','{}','synthetic',$2)",[workerConnection,issued]) + await setup.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,created_at) VALUES($1,$2,'servers.poll',1,1,'synthetic','{}','{}','approved',$3,$4)",[workerPlan,workerConnection,old,issued]) + await setup.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,created_at) VALUES($1,$2,$3,$5,'pending',$4)",[workerOperation,workerPlan,workerConnection,issued,workerOperation]) + for(const owner of ["historical-rest","historical-mcp","historical-missing-source","historical-recent","historical-worker"]){ + const value={...scope(owner),issuedAt:issued.getTime(),expiresAt:old.getTime()} + if(owner.includes("mcp")||owner.includes("missing")){value.kind="mcp";value.executionId=owner.includes("missing")?randomUUID():pendingOperation} + if(owner==="historical-worker"){value.kind="mcp";value.executionId=workerOperation} + historical.push(value) + await setup.query("INSERT INTO server_poll_storage_admissions(receipt_id,identity_kind,execution_id,connection_id,owner_id,binding_proof,context_proof,authority_issued_at,authority_expires_at,max_stages,claim_token,claim_attempted_at,created_at,retention_days,safety_ms,gc_not_before) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,8,NULL,$9,$8,30,60000,$9::timestamptz+interval '30 days 60 seconds')",[value.receiptId,value.kind,value.executionId,owner==="historical-worker"?workerConnection:value.kind==="mcp"?connectionId:null,owner,proof,contextProof,issued,old]) + await setup.query("INSERT INTO server_poll_storage_usage(scope,admissions,reserved_rows,reserved_bytes) VALUES($1,1,4019,8421376)",["owner:"+owner]) + } + await setup.query("UPDATE server_poll_storage_usage SET admissions=5,reserved_rows=5*4019,reserved_bytes=5*8421376 WHERE scope='global'") + const value=historical[0] + await setup.query("INSERT INTO server_poll_executions(receipt_id,identity_kind,execution_id,binding_proof,context_ciphertext,context_proof,created_at) VALUES($1,'rest',$2,$3,'SYNTHETIC',$4,$5)",[value.receiptId,value.executionId,proof,contextProof,old]) + await setup.query("INSERT INTO server_poll_intents(receipt_id,stage,before_proof,effect_proof,intent_proof,created_at) VALUES($1,'native.identity',$2,$2,$2,$3)",[value.receiptId,proof,old]) + await setup.query("INSERT INTO server_poll_acks(receipt_id,stage,result_proof,outcome,ack_proof,created_at) VALUES($1,'native.identity',$2,'confirmed',$2,$3)",[value.receiptId,proof,old]) + await setup.query("INSERT INTO server_poll_receipts(receipt_id,receipt_ciphertext,receipt_proof,created_at) VALUES($1,'SYNTHETIC',$2,$3)",[value.receiptId,proof,old]) + await setup.query("INSERT INTO server_poll_quarantines(receipt_id,identity_kind,execution_id,context_proof,created_at) VALUES($1,'rest',$2,$3,clock_timestamp())",[historical[3].receiptId,historical[3].executionId,contextProof]) + await setup.query(proposal.slice(boundary)) + }catch(error){await setup.query("ROLLBACK");throw error}finally{setup.release()} + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n,39) + cases.push("owned historical synthetic rows inserted BEFORE guards; no clock alteration, trigger disabling or normal migration marker fabrication") + + // Preserve the historical 0039 proof, then run current code only against the + // normal current schema. No synthetic marker or quota backfill is installed. + const addedAdmission=["storage_state","compact_at","archive_bytes","original_activity_at","compaction_claim","compaction_claimed_at","compaction_claim_xid"] + const {POLL_STORAGE_TABLE_KEYS}=await import("./server-poll-storage-restore-evidence.sql-helper.js") + const captureHistorical=async()=>{ + const client=await pool.connect(),tables:Record={} + try{ + await client.query("BEGIN READ ONLY");await client.query("SET LOCAL TimeZone='UTC'") + for(const [name,keys] of Object.entries(POLL_STORAGE_TABLE_KEYS)){ + if(name==="server_poll_archives")continue + const omitted=name==="server_poll_storage_admissions"?addedAdmission:name==="server_poll_storage_usage"?["compacted_admissions","archive_bytes"]:[] + const rows=(await client.query(`SELECT to_jsonb(t)-$1::text[] AS value FROM ${name} t ORDER BY ${keys}`,[omitted])).rows + tables[name]={rows,sha256:createHash("sha256").update(JSON.stringify(rows)).digest("hex")} + } + await client.query("COMMIT");return tables + }finally{client.release(true)} + } + const historicalBefore=await captureHistorical() + await(await import("../db/migrate.js")).applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n,MIGRATION_MANIFEST.length) + assert.deepEqual(await captureHistorical(),historicalBefore,"Current normal migrations preserve every historical full admission/journal/counter field and hash") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions WHERE storage_state<>'full' OR compact_at IS NOT NULL OR archive_bytes IS NOT NULL OR original_activity_at IS NOT NULL OR compaction_claim IS NOT NULL OR compaction_claimed_at IS NOT NULL OR compaction_claim_xid IS NOT NULL")).rows[0].n,0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_usage WHERE compacted_admissions<>0 OR archive_bytes<>0")).rows[0].n,0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_archives")).rows[0].n,0) + cases.push("genuine staged historicalPrefix39 upgraded by normal current migrations; all seven old tables and exact opaque fields/hashes preserved with only new default columns") + + const value=scope(),store=createServerPollStorage(binding(value)) + await store.claim(options(),sql=>anchor(sql,value)) + assert.equal((await usage(value.ownerId)).admissions,1) + assert.equal((await pool.query("SELECT used_rows FROM server_poll_storage_admissions WHERE receipt_id=$1",[value.receiptId])).rows[0].used_rows,1) + await assert.rejects(createServerPollStorage(binding(value)).claim(options(),async()=>assert.fail("spent identity cannot recreate anchor")),PollStorageRefused) + await assert.rejects(store.claim(options(),async()=>assert.fail("same factory never claims twice")),PollStorageRefused) + await store.write(options(),async sql=>{await sql.query("INSERT INTO server_poll_intents(receipt_id,stage,before_proof,effect_proof,intent_proof,created_at) VALUES($1,'native.identity',$2,$2,$2,'2000-01-01')",[value.receiptId,proof])}) + assert((await pool.query("SELECT created_at>clock_timestamp()-interval '1 minute' AS fresh FROM server_poll_intents WHERE receipt_id=$1",[value.receiptId])).rows[0].fresh) + await assert.rejects(store.write(options(),sql=>sql.query("INSERT INTO server_poll_intents(receipt_id,stage,before_proof,effect_proof,intent_proof) VALUES($1,'publish.4',$2,$2,$2)",[value.receiptId,proof])),PollStorageRefused) + const before=(await pool.query("SELECT used_rows,used_bytes FROM server_poll_storage_admissions WHERE receipt_id=$1",[value.receiptId])).rows[0] + await store.quarantine();const marked=(await pool.query("SELECT used_rows,used_bytes FROM server_poll_storage_admissions WHERE receipt_id=$1",[value.receiptId])).rows[0] + assert.equal(marked.used_rows,before.used_rows+1) + await pool.query("INSERT INTO server_poll_quarantines(receipt_id,identity_kind,execution_id,context_proof) VALUES($1,'rest',$2,$3) ON CONFLICT DO NOTHING",[value.receiptId,value.executionId,contextProof]) + assert.deepEqual((await pool.query("SELECT used_rows,used_bytes FROM server_poll_storage_admissions WHERE receipt_id=$1",[value.receiptId])).rows[0],marked) + await assert.rejects(store.write(options(),sql=>sql.query("INSERT INTO server_poll_intents(receipt_id,stage,before_proof,effect_proof,intent_proof) VALUES($1,'native.resource',$2,$2,$2)",[value.receiptId,proof])),PollStorageRefused) + cases.push("two-phase claim, exact reservation, durable spent identity, normalized activity, plan stage cap and idempotent quarantine actual-row metering") + + const owner="concurrent-"+randomUUID(),claims=await Promise.allSettled(Array.from({length:10},async()=>{const s=scope(owner);return createServerPollStorage(binding(s)).claim(options(),sql=>anchor(sql,s))})) + assert.equal(claims.filter(row=>row.status==="fulfilled").length,8) + assert.deepEqual(await usage(owner),{admissions:8,reserved_rows:8*4019,reserved_bytes:String(8*8421376)}) + cases.push("ten concurrent independent factories atomically admit eight owner attempts, refuse two, never partially charge counters") + + const uncertain=scope(),b=binding(uncertain);let loss=true,callbacks=0 + const lost=createServerPollStorageForTests(b,{now:Date.now,async connect(opts){const c=await connectProbeStorage(opts);return {release:v=>c.release(v),async query(text,values,queryOptions){const result=await c.query(text,values,queryOptions);if(text==="COMMIT"&&loss){loss=false;throw Error("SYNTHETIC lost admission commit acknowledgement")};return result}}}}) + await assert.rejects(lost.claim(options(),async()=>{callbacks++}),PollStorageUnknown) + assert.equal(callbacks,0);assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE receipt_id=$1",[uncertain.receiptId])).rows[0].n,0) + await assert.rejects(createServerPollStorage(b).claim(options(),async()=>assert.fail("unknown admission cannot restart")),PollStorageRefused) + await lost.quarantine() + assert.equal((await usage(uncertain.ownerId)).admissions,1) + cases.push("real acknowledged-on-server admission COMMIT with lost client ACK never dispatches anchor, remains spent on new factory and supports charged orphan quarantine") + + for(const phase of ["consume","write"] as const){ + const s=scope();let commit=0,revoked=false + const bound={...binding(s),authorize:async()=>{assert(!revoked)}} + const attempt=createServerPollStorageForTests(bound,{now:Date.now,async connect(opts){const c=await connectProbeStorage(opts);return {release:v=>c.release(v),async query(text,values,queryOptions){const result=await c.query(text,values,queryOptions);if(text==="COMMIT"&&++commit===(phase==="consume"?2:3))throw Error("SYNTHETIC lost "+phase+" commit ACK");return result}}}}) + if(phase==="consume")await assert.rejects(attempt.claim(options(),sql=>anchor(sql,s)),PollStorageUnknown) + else{ + await attempt.claim(options(),sql=>anchor(sql,s)) + await assert.rejects(attempt.write(options(),sql=>sql.query("INSERT INTO server_poll_intents(receipt_id,stage,before_proof,effect_proof,intent_proof) VALUES($1,'native.identity',$2,$2,$2)",[s.receiptId,proof])),PollStorageUnknown) + } + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE receipt_id=$1",[s.receiptId])).rows[0].n,1) + if(phase==="write")assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1",[s.receiptId])).rows[0].n,1) + revoked=true;await attempt.quarantine() + await assert.rejects(attempt.write(options(),async()=>assert.fail("poisoned writer cannot restart")),PollStorageRefused) + await assert.rejects(createServerPollStorage(binding(s)).claim(options(),async()=>assert.fail("spent attempt cannot renew")),PollStorageRefused) + assert.equal((await usage(s.ownerId)).admissions,1) + } + cases.push("real consume/anchor and intent COMMIT ACK losses preserve committed rows, destroy owner before fresh quarantine despite revocation and never reopen or recharge the attempt") + await assert.rejects(pool.query("INSERT INTO server_poll_quarantines(receipt_id,identity_kind,execution_id,context_proof) VALUES($1,'rest',$2,$3)",[randomUUID(),randomUUID(),contextProof])) + await assert.rejects(pool.query("UPDATE server_poll_executions SET context_ciphertext='REPLACED' WHERE receipt_id=$1",[historical[0].receiptId])) + + const gc=await pool.connect() + try{await gc.query("BEGIN");await gc.query("SET LOCAL mm.poll_storage_gc='on'");await assert.rejects(gc.query("DELETE FROM server_poll_executions WHERE receipt_id=$1",[value.receiptId]));await gc.query("ROLLBACK")}finally{gc.release()} + const predicate=await pool.connect(),settingWriter=await pool.connect() + try{ + await predicate.query("BEGIN");await predicate.query("SET LOCAL mm.poll_storage_gc='on'") + assert.equal((await predicate.query("SELECT server_poll_storage_gc_allowed($1) AS allowed",[historical[0].receiptId])).rows[0].allowed,true) + await settingWriter.query("BEGIN");await settingWriter.query("SET LOCAL lock_timeout='100ms'") + await assert.rejects(settingWriter.query("UPDATE mcp_settings SET requests_retention_days=365 WHERE id=1"),(error:unknown)=>(error as{code?:string}).code==="55P03") + }finally{await settingWriter.query("ROLLBACK");settingWriter.release();await predicate.query("ROLLBACK");predicate.release()} + cases.push("direct GC predicate itself locks current retention until transaction end; concurrent retention increase cannot race a GUC-enabled delete") + await pool.query("UPDATE mcp_settings SET requests_retention_days=365 WHERE id=1") + assert.equal((await collectExpiredServerPollStorage()).collected,0) + await pool.query("UPDATE mcp_settings SET requests_retention_days=30 WHERE id=1") + let failDelete=true + await assert.rejects(collectExpiredServerPollStorageForTests({now:Date.now,async connect(opts){const c=await connectProbeStorage(opts);return {release:v=>c.release(v),async query(text,values,queryOptions){const result=await c.query(text,values,queryOptions);if(failDelete&&text.startsWith("DELETE FROM server_poll_storage_admissions")){failDelete=false;throw Error("SYNTHETIC rollback after actual admission deletion and refund")};return result}}}}),PollStorageRefused) + assert.equal((await usage("historical-rest")).admissions,1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE receipt_id=$1",[historical[0].receiptId])).rows[0].n,1) + let loseGcAck=true + await assert.rejects(collectExpiredServerPollStorageForTests({now:Date.now,async connect(opts){const c=await connectProbeStorage(opts);return {release:v=>c.release(v),async query(text,values,queryOptions){const result=await c.query(text,values,queryOptions);if(text==="COMMIT"&&loseGcAck){loseGcAck=false;throw Error("SYNTHETIC lost actual GC commit ACK")};return result}}}}),PollStorageUnknown) + assert.equal(await usage("historical-rest"),undefined) + assert.throws(()=>createServerPollStorage(binding(historical[0])),PollStorageRefused,"Original expired identity cannot recreate a reservation even after complete GC") + for(const table of ["server_poll_executions","server_poll_intents","server_poll_acks","server_poll_receipts","server_poll_quarantines","server_poll_storage_admissions"]){assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table} WHERE receipt_id=$1`,[historical[0].receiptId])).rows[0].n,0)} + assert.equal((await usage("historical-mcp")).admissions,1) + assert.equal((await collectExpiredServerPollStorage()).collected,0,"Next maintenance pass must not refund the already committed unknown cleanup twice") + await pool.query("UPDATE mcp_operations SET state='unknown',finished_at=now()-interval '40 days' WHERE id=$1",[pendingOperation]) + assert.equal((await collectExpiredServerPollStorage()).collected,1) + assert.equal(await usage("historical-mcp"),undefined) + assert.equal((await usage("historical-missing-source")).admissions,1) + assert.equal((await usage("historical-recent")).admissions,1) + cases.push("GUC alone cannot erase live data; increased current retention, recent quarantine and missing/running original MCP operation prevent GC; real terminal owner-linked operation permits atomic whole-journal deletion and exact refund") + cases.push("injected failure after real DELETE/refund rolls back all child rows and both counters together") + cases.push("lost acknowledgement after real GC COMMIT is explicit UNKNOWN; a later fresh pass refunds nothing twice, never repeats the spent identity") + + await pool.query("UPDATE mcp_operations SET state='unknown',finished_at=now()-interval '40 days' WHERE id=$1",[workerOperation]) + assert.equal((await usage("historical-worker")).admissions,1) + assert.equal((await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled,false) + await(await import("../modules/mcp/workers.js")).tickMcpWorkers() + assert.equal(await usage("historical-worker"),undefined) + cases.push("actual existing worker tick collects an eligible original terminal poll admission and refunds quota while MCP is disabled") + await (await import("./server-poll-storage-execution.sql-helper.js")).acceptServerPollStorageExecutionSql() + cases.push("actual quota-bound full production poll: all four native GETs observe committed consumed admission, every maintenance publication/ACK metered, known replay no new native or quota") + const globalBefore=Number((await pool.query("SELECT admissions FROM server_poll_storage_usage WHERE scope='global'")).rows[0].admissions) + const fill=await Promise.allSettled(Array.from({length:130-globalBefore},async()=>{const s=scope();return createServerPollStorage(binding(s)).claim({deadlineAt:Date.now()+60000},sql=>anchor(sql,s))})) + assert.equal(fill.filter(row=>row.status==="fulfilled").length,128-globalBefore) + assert.equal(Number((await pool.query("SELECT admissions FROM server_poll_storage_usage WHERE scope='global'")).rows[0].admissions),128) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_usage WHERE scope<>'global' AND admissions=0")).rows[0].n,0,"Refused owner reservations leave no zero counter rows") + cases.push("global 128-attempt cap holds across independent owners and concurrent actual commits; failed admissions leave no orphan counters") + { + const {readServerPollCapacity}=await import("./server-poll-capacity.js") + const fullOwner=(await pool.query("SELECT scope FROM server_poll_storage_usage WHERE scope<>'global' AND admissions=8 LIMIT 1")).rows[0] + assert(fullOwner) + const ownerId=String(fullOwner.scope).slice("owner:".length) + const before=(await pool.query("SELECT * FROM server_poll_storage_usage ORDER BY scope")).rows + const readOnly=await pool.connect() + try{ + await readOnly.query("BEGIN READ ONLY") + const capacity=await readServerPollCapacity(readOnly,ownerId,true) + assert.equal(capacity.status,"observed") + if(capacity.status!=="observed")assert.fail("Expected actual full retained capacity") + assert.deepEqual(capacity.owner,{used:8,limit:8,remaining:0}) + assert.deepEqual(capacity.global,{exhausted:true,usage:{used:128,limit:128,remaining:0}}) + const privateCapacity=await readServerPollCapacity(readOnly,ownerId,false) + assert.equal(privateCapacity.status,"observed") + if(privateCapacity.status!=="observed")assert.fail("Expected scoped private capacity") + assert.deepEqual(privateCapacity.global,{exhausted:true,usage:null}) + assert.equal(JSON.stringify(privateCapacity).includes(ownerId),false) + const emptyOwner=await readServerPollCapacity(readOnly,"unallocated-"+randomUUID(),false) + assert.equal(emptyOwner.status,"observed") + if(emptyOwner.status!=="observed")assert.fail("Expected unallocated owner capacity") + assert.deepEqual(emptyOwner.owner,{used:0,limit:8,remaining:8}) + assert.equal(emptyOwner.global.exhausted,true) + await readOnly.query("COMMIT") + }finally{readOnly.release()} + assert.deepEqual((await pool.query("SELECT * FROM server_poll_storage_usage ORDER BY scope")).rows,before) + cases.push("portal retained capacity uses real READ ONLY snapshot of owner8/global128, hides non-admin global numbers, never claims unallocated owner bypasses full global cap, and changes no quota row") + } + const evidence=await(await import("./server-poll-storage-restore-evidence.sql-helper.js")).pollStorageRestoreEvidence(pool) + await writeFile(resolve(process.env.MCP_STATE_DIR!,"poll-storage-owned-restore-evidence.json"),JSON.stringify(evidence),{flag:"wx",mode:0o600}) + console.log("Owned poll storage SQL:",JSON.stringify({cases:cases.length,names:cases,historicalPrefix39:39,currentMigrations:MIGRATION_MANIFEST.length,actualMigrationFileStagedForOwnedHistoricalFixtures:true,allFileStatementsAndRealTerminalMarker:true,historicalRowsAndHashesPreserved:true,realPgTransactions:true,realQuotaTriggers:true,historicalFixtureBeforeGuards:true,providerOrClockOverrides:false,simulatedCommitAckLossAfterRealCommit:true,publicRoutesRegistered:false,journalAdapterIntegrated:true,productionDatabaseTouched:false,externalNetwork:false})) +}finally{await closePool()} diff --git a/backend/src/services/server-poll-storage.test.ts b/backend/src/services/server-poll-storage.test.ts new file mode 100644 index 00000000..b5282ed9 --- /dev/null +++ b/backend/src/services/server-poll-storage.test.ts @@ -0,0 +1,264 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import type { ProbeStorageConnection, ProbeStorageOptions, ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { createServerPollStorageForTests, runPollStorageTransaction, PollStorageRefused, PollStorageUnknown, + type PollStorageBinding, type PollStorageDependencies } from "./server-poll-storage.js" + +type State = { admission?: Record; anchors: string[]; quarantines: Record[] } +const gate = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const safe = (error: unknown) => (error instanceof PollStorageRefused || error instanceof PollStorageUnknown) && !error.message.includes("PRIVATE_PROVIDER") +function fixture() { + let now = Date.now(), state: State = { anchors: [], quarantines: [] }, lostCommit = 0, commits = 0, revoked = false, authorizations = 0 + let phaseAfterReservation: string | undefined + const leases: { commands: string[]; releases: boolean[]; options: ProbeStorageOptions; timeouts: number[] }[] = [], events: string[] = [] + const binding: PollStorageBinding = { scope: { receiptId: randomUUID(), kind: "mcp", executionId: randomUUID(), ownerId: "owned-operator", bindingProof: "a".repeat(64), contextProof: "b".repeat(64), issuedAt: now - 1000, expiresAt: now + 60000 }, + connectionId: randomUUID(), maxStages: 11, authorize: async () => { authorizations++; if (revoked) throw Error("PRIVATE_PROVIDER revoked") } } + const dependencies: PollStorageDependencies = { now: () => now, connect: async options => { + const number = leases.length + 1, lease = { commands: [] as string[], releases: [] as boolean[], options: { ...options }, timeouts: [] as number[] } + leases.push(lease); events.push("connect:" + number) + let transaction: State | undefined, closed = false + const connection: ProbeStorageConnection = { async query(text, values = [], queryOptions) { + assert(!closed, "A released physical backend must not receive SQL") + lease.commands.push(text); lease.timeouts.push(queryOptions!.timeoutMs); events.push(number + ":" + text) + if (text === "BEGIN") { transaction = structuredClone(state); return { rows: [] } } + if (text === "ROLLBACK") { transaction = undefined; return { rows: [] } } + if (text === "COMMIT") { + assert(transaction); state = transaction; transaction = undefined; commits++ + if (commits === 1 && phaseAfterReservation && state.admission) state.admission.storage_state = phaseAfterReservation + if (commits === lostCommit) throw Error("PRIVATE_PROVIDER lost actual COMMIT acknowledgement") + return { rows: [] } + } + assert(transaction) + if (text === "SELECT set_config('TimeZone','UTC',true),set_config('statement_timeout','5000ms',true)" || text.includes("pg_advisory_xact_lock")) return { rows: [] } + if (text.startsWith("SELECT a.receipt_id AS admission_receipt_id")) { + assert.equal(transaction.admission, undefined, "First reservation scans the actual empty fixture ledger") + return { rows: [] } + } + if (text.startsWith("SELECT r.receipt_id FROM server_poll_archives")) return { rows: [] } + if (text.startsWith("SELECT scope,admissions,compacted_admissions,archive_bytes,reserved_rows,reserved_bytes")) + return { rows: [{ scope: "global", admissions: 0, compacted_admissions: 0, archive_bytes: 0, reserved_rows: 0, reserved_bytes: 0 }] } + if (text.startsWith("SELECT * FROM server_poll_storage_admissions")) { + const row = transaction.admission + return { rows: row && (row.receipt_id === values[0] || row.identity_kind === values[1] && row.execution_id === values[2]) ? [structuredClone(row)] : [] } + } + if (text.startsWith("INSERT INTO server_poll_storage_admissions")) { + assert.equal(transaction.admission, undefined) + transaction.admission = { receipt_id: values[0], identity_kind: values[1], execution_id: values[2], connection_id: values[3], owner_id: values[4], + binding_proof: values[5], context_proof: values[6], authority_issued_at: new Date(String(values[7])), authority_expires_at: new Date(String(values[8])), + max_stages: values[9], claim_token: values[10], claim_attempted_at: null, storage_state: "full" } + return { rows: [{ receipt_id: values[0] }] } + } + if (text.startsWith("UPDATE server_poll_storage_admissions SET claim_token=NULL")) { + const row = transaction.admission + if (!row || row.receipt_id !== values[0] || row.claim_token !== values[1] || new Date(row.authority_expires_at as Date).getTime() <= now) return { rows: [] } + row.claim_token = null; row.claim_attempted_at = new Date(now) + return { rows: [{ receipt_id: values[0] }] } + } + if (text.startsWith("SELECT receipt_id,identity_kind,execution_id,context_proof FROM server_poll_quarantines")) return { rows: structuredClone(transaction.quarantines.filter(row => row.receipt_id === values[0])) } + if (text.startsWith("INSERT INTO server_poll_quarantines")) { + assert(transaction.admission); assert(!transaction.quarantines.some(row => row.receipt_id === values[0])) + transaction.quarantines.push({ receipt_id: values[0], identity_kind: values[1], execution_id: values[2], context_proof: values[3] }); return { rows: [] } + } + if (text.startsWith("INSERT INTO server_poll_executions")) { transaction.anchors.push(String(values[0])); return { rows: [] } } + if (text === "SELECT bounded_callback") return { rows: [] } + throw Error("Unapproved fake SQL; PRIVATE_PROVIDER") + }, release(unsafe = false) { assert(!closed, "Release exactly once"); closed = true; transaction = undefined; lease.releases.push(unsafe); events.push("release:" + number + ":" + unsafe) } } + return connection + } } + const options = () => ({ deadlineAt: now + 2000 }) + const anchor = (sql: ProbeStorageSql) => sql.query("INSERT INTO server_poll_executions(receipt_id) VALUES($1)", [binding.scope.receiptId]) + return { binding, dependencies, options, anchor, leases, events, state: () => structuredClone(state), now: () => now, + phaseAfterReserve: (phase: string) => { phaseAfterReservation = phase }, + phase: (phase: string) => { assert(state.admission); state.admission.storage_state = phase }, + advance: (ms: number) => { now += ms }, lose: (which: number) => { lostCommit = which }, revoke: () => { revoked = true }, authorizations: () => authorizations } +} + +test("two-phase reservation COMMIT precedes consume/anchor; every transaction owns storage lock first", async () => { + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies) + await storage.claim(f.options(), f.anchor) + assert.equal(f.leases.length, 2); assert.deepEqual(f.leases.map(lease => lease.releases), [[false], [false]]) + assert(f.events.indexOf("1:COMMIT") < f.events.findIndex(event => event.startsWith("2:UPDATE server_poll_storage_admissions"))) + assert(f.events.indexOf("1:COMMIT") < f.events.findIndex(event => event.startsWith("2:INSERT INTO server_poll_executions"))) + for (const lease of f.leases) { + assert(lease.commands.findIndex(sql => sql.includes("pg_advisory_xact_lock")) < lease.commands.findIndex(sql => sql.startsWith("SELECT * FROM server_poll_storage_admissions"))) + assert(lease.timeouts.every(value => value >= 1 && value <= 5000)) + } + const first = f.leases[0].commands + assert(first.findIndex(sql => sql.startsWith("SELECT a.receipt_id AS admission_receipt_id")) < first.findIndex(sql => sql.startsWith("INSERT INTO server_poll_storage_admissions"))) + assert(first.findIndex(sql => sql.startsWith("SELECT scope,admissions,compacted_admissions")) < first.findIndex(sql => sql.startsWith("INSERT INTO server_poll_storage_admissions"))) + assert.equal(f.state().admission!.claim_token, null); assert.equal(f.state().anchors.length, 1); assert(f.authorizations() > 2) +}) + +test("phase drift after reservation refuses consume/anchor and later writes require full phase", async () => { + const drift = fixture(); drift.phaseAfterReserve("compacted") + await assert.rejects(createServerPollStorageForTests(drift.binding, drift.dependencies).claim(drift.options(), drift.anchor), PollStorageRefused) + assert.equal(drift.state().anchors.length, 0) + assert.equal(drift.leases.flatMap(lease => lease.commands).some(sql => sql.startsWith("UPDATE server_poll_storage_admissions SET claim_token=NULL")), false) + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies) + await storage.claim(f.options(), f.anchor); f.phase("compacted") + let writes = 0 + await assert.rejects(storage.write(f.options(), async () => { writes++ }), PollStorageRefused) + assert.equal(writes, 0); assert.equal(f.state().anchors.length, 1) +}) + +test("lost reservation COMMIT never enters anchor; fresh instance sees spent reservation without renewal", async () => { + const f = fixture(); f.lose(1) + const storage = createServerPollStorageForTests(f.binding, f.dependencies); let callbacks = 0 + await assert.rejects(storage.claim(f.options(), async () => { callbacks++ }), PollStorageUnknown) + assert.equal(callbacks, 0); assert(f.state().admission!.claim_token); assert.equal(f.state().anchors.length, 0) + assert.deepEqual(f.leases[0].releases, [true]); assert(!f.leases[0].commands.includes("ROLLBACK")) + await assert.rejects(storage.claim(f.options(), async () => { callbacks++ }), PollStorageRefused) + const restarted = createServerPollStorageForTests(f.binding, f.dependencies) + await assert.rejects(restarted.claim(f.options(), async () => { callbacks++ }), PollStorageRefused) + assert.equal(callbacks, 0); assert.equal(f.leases.flatMap(lease => lease.commands).filter(sql => sql.startsWith("INSERT INTO server_poll_storage_admissions")).length, 1) +}) + +test("lost consume COMMIT destroys original backend before independent quarantine; no rollback/replay", async () => { + const f = fixture(); f.lose(2) + const storage = createServerPollStorageForTests(f.binding, f.dependencies) + await assert.rejects(storage.claim(f.options(), f.anchor), PollStorageUnknown) + assert.equal(f.state().anchors.length, 1); assert.equal(f.state().admission!.claim_token, null) + assert.deepEqual(f.leases[1].releases, [true]); assert(!f.leases[1].commands.includes("ROLLBACK")) + await storage.quarantine() + assert(f.events.indexOf("release:2:true") < f.events.indexOf("connect:3")); assert.equal(f.state().quarantines.length, 1) + const previous = f.leases.length + await assert.rejects(storage.write(f.options(), async () => {}), PollStorageRefused) + await assert.rejects(storage.claim(f.options(), f.anchor), PollStorageRefused); assert.equal(f.leases.length, previous) +}) + +test("unknown write COMMIT poisons future work, including fresh instance claim", async () => { + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies) + await storage.claim(f.options(), f.anchor); f.lose(3) + await assert.rejects(storage.write(f.options(), sql => sql.query("SELECT bounded_callback")), PollStorageUnknown) + assert(!f.leases[2].commands.includes("ROLLBACK")); assert.deepEqual(f.leases[2].releases, [true]) + await assert.rejects(storage.write(f.options(), async () => {}), PollStorageRefused) + await assert.rejects(createServerPollStorageForTests(f.binding, f.dependencies).claim(f.options(), f.anchor), PollStorageRefused) +}) + +test("callback crypto-gap timeout destroys once and refuses late SQL without fresh checkout", async () => { + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies), entered = gate(), resume = gate(), late = gate() + const pending = storage.claim({ deadlineAt: f.now() + 40 }, async sql => { + entered.resolve(); await resume.promise; await assert.rejects(sql.query("SELECT late_source"), safe); late.resolve() + }) + await entered.promise; await assert.rejects(pending, PollStorageRefused) + assert.deepEqual(f.leases[1].releases, [true]); assert(!f.leases[1].commands.includes("ROLLBACK")) + const commands = f.leases[1].commands.length; resume.resolve(); await late.promise + assert.equal(f.leases[1].commands.length, commands); assert.deepEqual(f.leases[1].releases, [true]); assert.equal(f.leases.length, 2) + assert.equal(f.state().anchors.length, 0); assert(f.state().admission!.claim_token) +}) + +test("escaped successful callback facade is inactive after its owner commits", async () => { + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies); let escaped!: ProbeStorageSql + await storage.claim(f.options(), async sql => { escaped = sql; await f.anchor(sql) }) + const commands = f.leases[1].commands.length + await assert.rejects(escaped.query("SELECT late_source"), PollStorageRefused) + assert.equal(f.leases[1].commands.length, commands); assert.deepEqual(f.leases[1].releases, [false]) +}) + +test("callback cannot dispatch transaction boundaries or multiple statements owned by the host", async () => { + for (const command of ["COMMIT", "/* callback */ ROLLBACK", "-- callback\nBEGIN", "END", "SAVEPOINT nested", "RELEASE nested", "START TRANSACTION", "PREPARE TRANSACTION 'nested'", "SET TRANSACTION READ ONLY", "SELECT bounded_callback; COMMIT"]) { + const f = fixture() + await assert.rejects(runPollStorageTransaction(f.dependencies, f.options(), async () => {}, sql => sql.query(command)), PollStorageRefused) + assert.equal(f.leases.length, 1) + const lease = f.leases[0] + assert.deepEqual(lease.commands.filter(sql => !sql.startsWith("SELECT set_config(") && !sql.includes("pg_advisory_xact_lock")), ["BEGIN", "ROLLBACK"]) + assert.deepEqual(lease.releases, [true]); assert.equal(f.state().anchors.length, 0) + } +}) + +test("ordinary callback SELECT still commits exactly once through its transaction owner", async () => { + const f = fixture() + const result = await runPollStorageTransaction(f.dependencies, f.options(), async () => {}, async sql => { + await sql.query("SELECT bounded_callback"); return "confirmed" + }) + assert.equal(result, "confirmed"); assert.equal(f.leases.length, 1) + const lease = f.leases[0] + assert.deepEqual(lease.commands.filter(sql => !sql.startsWith("SELECT set_config(") && !sql.includes("pg_advisory_xact_lock")), ["BEGIN", "SELECT bounded_callback", "COMMIT"]) + assert.deepEqual(lease.releases, [false]) +}) + +test("scope, authority provider, connector and clock are captured before any asynchronous work", async () => { + const f = fixture(), original = structuredClone(f.binding.scope), storage = createServerPollStorageForTests(f.binding, f.dependencies) + f.binding.scope.ownerId = "foreign"; f.binding.scope.receiptId = randomUUID(); f.binding.maxStages = 2008; f.binding.connectionId = randomUUID() + f.binding.authorize = async () => { throw Error("Replacement authority must never run") } + f.dependencies.connect = async () => { throw Error("Replacement connector must never run") }; f.dependencies.now = () => 0 + await storage.claim(f.options(), sql => sql.query("INSERT INTO server_poll_executions(receipt_id) VALUES($1)", [original.receiptId])) + assert.equal(f.state().admission!.owner_id, original.ownerId); assert.equal(f.state().admission!.receipt_id, original.receiptId) + assert.equal(f.state().admission!.max_stages, 11); assert.equal(f.state().anchors[0], original.receiptId) +}) + +test("both claim phases retain the accepted signal/deadline even when caller options change", async () => { + const f = fixture(), controller = new AbortController(), replacement = new AbortController() + const options = { deadlineAt: f.now() + 2000, signal: controller.signal }, acceptedDeadline = options.deadlineAt + const originalAuthorize = f.binding.authorize; let changed = false + f.binding.authorize = async value => { + await originalAuthorize(value); assert(Object.isFrozen(value)) + if (!changed) { changed = true; options.deadlineAt += 90000; options.signal = replacement.signal } + } + await createServerPollStorageForTests(f.binding, f.dependencies).claim(options, f.anchor) + assert.equal(f.leases.length, 2) + for (const lease of f.leases) { assert.equal(lease.options.deadlineAt, acceptedDeadline); assert.equal(lease.options.signal, controller.signal) } +}) + +test("transaction entry captures connector, clock and options before an authorization await", async () => { + const f = fixture(), entered = gate(), resume = gate(), controller = new AbortController() + const options = { deadlineAt: f.now() + 2000, signal: controller.signal }, acceptedDeadline = options.deadlineAt + const pending = runPollStorageTransaction(f.dependencies, options, async () => { entered.resolve(); await resume.promise }, async sql => { + await sql.query("SELECT bounded_callback"); return "confirmed" + }) + await entered.promise + f.dependencies.connect = async () => { throw Error("Replacement connector must not run") }; f.dependencies.now = () => 0 + options.deadlineAt = 0; options.signal = AbortSignal.abort() + resume.resolve(); assert.equal(await pending, "confirmed") + assert.equal(f.leases.length, 1); assert.equal(f.leases[0].options.deadlineAt, acceptedDeadline); assert.equal(f.leases[0].options.signal, controller.signal) + assert.deepEqual(f.leases[0].releases, [false]) +}) + +test("duplicate/fresh claim and foreign owner/receipt/context/connection cannot adopt a reservation", async () => { + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies) + await storage.claim(f.options(), f.anchor) + await assert.rejects(storage.claim(f.options(), f.anchor), PollStorageRefused) + for (const changed of [{ scope: { ...f.binding.scope, ownerId: "foreign" } }, { scope: { ...f.binding.scope, receiptId: randomUUID() } }, + { scope: { ...f.binding.scope, contextProof: "c".repeat(64) } }, { connectionId: randomUUID() }]) { + const foreign = createServerPollStorageForTests({ ...f.binding, ...changed }, f.dependencies) + await assert.rejects(foreign.claim(f.options(), async () => assert.fail("Must not write foreign anchor")), PollStorageRefused) + await assert.rejects(foreign.quarantine(), PollStorageRefused) + } + assert.equal(f.state().anchors.length, 1); assert.equal(f.state().quarantines.length, 0) +}) + +test("quarantine survives original expiry/revocation/abort with independent 10s budget and exact idempotence", async () => { + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies), controller = new AbortController() + await storage.claim({ ...f.options(), signal: controller.signal }, f.anchor) + controller.abort(); f.revoke(); f.advance(120000) + const authorizations = f.authorizations(), before = f.now() + await storage.quarantine(); await storage.quarantine() + assert.equal(f.authorizations(), authorizations); assert.equal(f.state().quarantines.length, 1) + for (const lease of f.leases.slice(2)) { assert.equal(lease.options.deadlineAt, before + 10000); assert.equal(lease.options.signal, undefined); assert.deepEqual(lease.releases, [false]) } + await assert.rejects(storage.write(f.options(), async () => {}), PollStorageRefused) +}) + +test("quarantine cannot create quota-free orphan metadata", async () => { + const f = fixture(), storage = createServerPollStorageForTests(f.binding, f.dependencies) + await assert.rejects(storage.quarantine(), PollStorageRefused) + assert.equal(f.state().admission, undefined); assert.equal(f.state().quarantines.length, 0) +}) + +test("whole authorization gap and abandoned checkout settle promptly and dispose late backend once", async () => { + const resume = gate(), entered = gate(); let checkouts = 0 + const pending = runPollStorageTransaction({ now: Date.now, connect: async () => { checkouts++; throw Error("No checkout expected") } }, { deadlineAt: Date.now() + 30 }, async () => { entered.resolve(); await resume.promise }, async () => {}) + await entered.promise; await assert.rejects(pending, PollStorageRefused); resume.resolve(); await new Promise(done => setImmediate(done)); assert.equal(checkouts, 0) + const checkout = gate(), started = gate(), releases: boolean[] = [], controller = new AbortController() + const late = runPollStorageTransaction({ now: Date.now, connect: async () => { started.resolve(); await checkout.promise; return { query: async () => assert.fail("Late backend cannot receive BEGIN"), release: unsafe => { releases.push(unsafe ?? false) } } } }, + { deadlineAt: Date.now() + 2000, signal: controller.signal }, async () => {}, async () => {}) + await started.promise; controller.abort(); await assert.rejects(late, PollStorageRefused); checkout.resolve(); await new Promise(done => setImmediate(done)); assert.deepEqual(releases, [true]) +}) + +test("provider errors never escape and invalid/expired scope refuses without any connection", async () => { + const f = fixture() + assert.throws(() => createServerPollStorageForTests({ ...f.binding, scope: { ...f.binding.scope, expiresAt: f.now() } }, f.dependencies), PollStorageRefused) + assert.equal(f.leases.length, 0) + f.dependencies.connect = async () => { throw Error("PRIVATE_PROVIDER database URL") } + await assert.rejects(createServerPollStorageForTests(f.binding, f.dependencies).claim(f.options(), f.anchor), safe) +}) diff --git a/backend/src/services/server-poll-storage.ts b/backend/src/services/server-poll-storage.ts new file mode 100644 index 00000000..3c9c4d4d --- /dev/null +++ b/backend/src/services/server-poll-storage.ts @@ -0,0 +1,229 @@ +import { carryPollError } from "./server-poll-error-provenance.js" +/** Private opt-in poll storage. No import/constructor performs I/O or enables + * a route. The separately reviewed storage schema must already be installed. */ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import type { ProbeStorageConnection, ProbeStorageOptions, ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { pollStorageScope, PollStorageRefused, type PollStorageScope } from "./server-poll-storage-model.js" +import { assertServerPollArchiveAccounting } from "./server-poll-archive-accounting.js" +import type { ServerPollJournalOwner } from "./server-partition-maintenance-lease.js" +import { admitPartitionWriter, PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" + +export { PollStorageRefused } +export class PollStorageUnknown extends Error { + readonly automaticReplay=false + constructor(){super("Poll storage outcome unknown; automatic replay forbidden")} +} +export interface PollStorageBinding { + scope:PollStorageScope + connectionId:string|null + maxStages:number + /** Fresh original actor/policy/global scope/ALL servers/protections. Never a + * cached admission or a boolean obtained from request JSON. */ + authorize(options:ProbeStorageOptions):Promise +} +export interface PollStorageDependencies { + connect(options:ProbeStorageOptions):Promise + now():number +} +const production:PollStorageDependencies={connect:connectProbeStorage,now:Date.now} +const fail=():never=>{throw new PollStorageRefused()} +const lock="SELECT pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0))" + +/** The writer fence precedes POLL's storage, operation and row locks. Restore + * the lock timeout before the runner sets its usual storage statement budget. */ +export async function admitPollStorageWriter(sql:ProbeStorageSql,deadlineAt:number):Promise{ + const settings=(await sql.query("SELECT pg_catalog.current_setting('lock_timeout') AS lock_timeout")).rows + if(settings.length!==1||typeof settings[0].lock_timeout!=="string")fail() + await admitPartitionWriter(sql,{deadlineAt:Math.min(deadlineAt,Date.now()+5000)}) + await sql.query("SELECT pg_catalog.set_config('lock_timeout',$1,true)",[settings[0].lock_timeout]) +} + +/** Only a definite false read from this exact lock query can defer maintenance. + * A SQL, provider, timeout, shape or acknowledgement failure stays an error. */ +export async function tryAdmitPollStorageMaintenanceWriter(sql:ProbeStorageSql,scope:"session"|"transaction"):Promise{ + const statement=scope==="session" + ?"SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired" + :"SELECT pg_catalog.pg_try_advisory_xact_lock_shared($1::bigint) AS acquired" + const {rows}=await sql.query(statement,[PARTITION_WRITER_ADVISORY_KEY]) + if(rows.length!==1||typeof rows[0].acquired!=="boolean")return fail() + if(rows[0].acquired&&scope==="transaction"&&databaseRestoreQuarantineEnabled())await assertDatabaseRestoreQuarantineOpen(sql) + return rows[0].acquired +} + +/** Keep the shared writer lock across separately acknowledged claim and publication + * transactions. An unsafe transaction release destroys the physical session. */ +export function createPollMaintenanceWriter(deps:PollStorageDependencies,leaseDeadline:number){ + let physical:ProbeStorageConnection|undefined,borrowed=false,acquired=false,closed=false + const release=(unsafe:boolean)=>{ + closed=true + const connection=physical;physical=undefined + connection?.release(unsafe) + } + const connect:PollStorageDependencies["connect"]=async options=>{ + if(closed||borrowed)fail() + borrowed=true + try{ + const connection=physical??await deps.connect({...options,deadlineAt:leaseDeadline}) + if(closed){connection.release(true);return fail()} + physical=connection + let released=false + return { + query:(...args)=>{if(released||closed)return Promise.reject(new PollStorageRefused());return connection.query(...args)}, + release(unsafe=false){if(released)return;released=true;borrowed=false;if(unsafe)release(true)}, + } + }catch(error){borrowed=false;throw error} + } + return { + deps:{now:deps.now,connect}, + async tryAcquire(sql:ProbeStorageSql){ + acquired=await tryAdmitPollStorageMaintenanceWriter(sql,"session") + if(acquired&&databaseRestoreQuarantineEnabled()){ + try{await assertDatabaseRestoreQuarantineOpen(sql)} + catch(error){release(true);throw error} + } + return acquired + }, + async finish(deadlineAt:number){ + if(!physical||borrowed)return fail() + if(acquired){ + const remaining=Math.min(5000,deadlineAt-deps.now()) + if(remaining<1)return fail() + const {rows}=await physical.query("SELECT pg_catalog.pg_advisory_unlock_shared($1::bigint) AS released",[PARTITION_WRITER_ADVISORY_KEY],{timeoutMs:remaining}) + if(rows.length!==1||rows[0].released!==true)return fail() + } + release(false) + }, + destroy(){if(!closed)release(true)}, + } +} + +type PollStoragePrelude=(sql:ProbeStorageSql)=>Promise + +/** Own one connector through callback and COMMIT, including non-SQL awaits. + * Leaked facades expire; no ROLLBACK or retry follows an uncertain COMMIT. + * A trusted maintenance caller can admit its writer on this same checkout before the storage lock. */ +export async function runPollStorageTransaction(deps:PollStorageDependencies,options:ProbeStorageOptions,authorize:()=>Promise,work:(sql:ProbeStorageSql)=>Promise,beforeStorageLock?:PollStoragePrelude):Promise{ + deps=Object.freeze({connect:deps.connect,now:deps.now}) + options=Object.freeze({deadlineAt:options.deadlineAt,signal:options.signal}) + if(!Number.isSafeInteger(options.deadlineAt)||options.deadlineAt<=deps.now()||options.deadlineAt-deps.now()>180000||options.signal?.aborted)fail() + let active=true,busy=false,released=false,commitSent=false,committed=false,begun=false,broken=false + let connection:ProbeStorageConnection|undefined + const close=(unsafe:boolean)=>{if(connection&&!released){released=true;try{connection.release(unsafe)}catch{broken=true}}} + const live=()=>{if(!active||released||broken||options.signal?.aborted||deps.now()>=options.deadlineAt)fail()} + const fence=async()=>{live();await authorize();live()} + let stop!:()=>void + const stopped=new Promise((_resolve,reject)=>{stop=()=>{active=false;broken=true;close(true);reject(commitSent?new PollStorageUnknown():new PollStorageRefused())}}) + const timer=setTimeout(stop,Math.max(1,options.deadlineAt-deps.now())) + options.signal?.addEventListener("abort",stop,{once:true}) + const sql:ProbeStorageSql=Object.freeze({async query(text:string,values?:unknown[]){ + await fence();if(busy||!connection)throw new PollStorageRefused();const physical=connection;busy=true + try{ + if(text==="COMMIT")commitSent=true + const result=await physical.query(text,values,{signal:options.signal,timeoutMs:Math.max(1,Math.min(5000,options.deadlineAt-deps.now()))}) + if(text==="COMMIT")committed=true + await fence();return result + }catch(error){broken=true;throw error}finally{busy=false} + }}) + const completion=Promise.resolve().then(async()=>{ + await fence() + connection=await deps.connect(options) + if(!active||options.signal?.aborted||deps.now()>=options.deadlineAt){close(true);fail()} + await sql.query("BEGIN");begun=true + const owned:ProbeStorageSql=Object.freeze({async query(text:string,values?:unknown[]){ + const command=text.replace(/\/\*[\s\S]*?\*\/|--[^\r\n]*/g," ").trim() + if(/^(?:BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|START|PREPARE)\b|^SET\s+TRANSACTION\b/i.test(command)||command.includes(";"))fail() + return sql.query(text,values) + }}) + if(beforeStorageLock){ + const admission=await beforeStorageLock(owned) + if(admission){await sql.query("COMMIT");return admission.deferred} + } + await sql.query("SELECT set_config('TimeZone','UTC',true),set_config('statement_timeout','5000ms',true)") + await sql.query(lock) + const result=await work(owned);await fence();await sql.query("COMMIT");return result + }) + try{return await Promise.race([completion,stopped])} + catch(error){ + if(commitSent)throw carryPollError(new PollStorageUnknown(), error, "storage") + if(begun&&active&&!busy&&connection&&!released){try{await Promise.race([connection.query("ROLLBACK",[],{timeoutMs:5000}),stopped])}catch{broken=true}} + throw carryPollError(error instanceof PollStorageRefused?error:new PollStorageRefused(), error, "storage") + }finally{active=false;clearTimeout(timer);options.signal?.removeEventListener("abort",stop);close(broken||busy||!committed)} +} + +function factory(raw:PollStorageBinding,rawDeps:PollStorageDependencies,journalOwner?:ServerPollJournalOwner){ + const deps=Object.freeze({connect:rawDeps.connect,now:rawDeps.now}) + const scope=pollStorageScope(structuredClone(raw.scope),{now:deps.now()}),authorize=raw.authorize + const connectionId=scope.kind==="mcp"?z.string().uuid().parse(raw.connectionId):raw.connectionId===null?null:fail() + const maxStages=raw.maxStages + if(!Number.isSafeInteger(maxStages)||maxStages<8||maxStages>2008||typeof authorize!=="function")fail() + let claimStarted=false,claimed=false,poisoned=false + const current=async(options:ProbeStorageOptions)=>{ + if(poisoned||deps.now()>=scope.expiresAt)fail() + await authorize(options) + if(poisoned||deps.now()>=scope.expiresAt)fail() + } + const matching=(row:Record)=>row.receipt_id===scope.receiptId&&row.identity_kind===scope.kind&&row.execution_id===scope.executionId + &&row.connection_id===connectionId&&row.owner_id===scope.ownerId&&row.binding_proof===scope.bindingProof&&row.context_proof===scope.contextProof + &&new Date(row.authority_issued_at as string|Date).getTime()===scope.issuedAt&&new Date(row.authority_expires_at as string|Date).getTime()===scope.expiresAt + &&row.max_stages===maxStages + const read=async(sql:ProbeStorageSql)=>{ + const rows=(await sql.query("SELECT * FROM server_poll_storage_admissions WHERE receipt_id=$1 OR(identity_kind=$2 AND execution_id=$3) FOR UPDATE",[scope.receiptId,scope.kind,scope.executionId])).rows + if(rows.length>1||rows.length&&!matching(rows[0]))fail() + return rows[0] + } + const run=async(options:ProbeStorageOptions,work:(sql:ProbeStorageSql)=>Promise)=>{ + const accepted=Object.freeze({signal:options.signal,deadlineAt:Math.min(options.deadlineAt,scope.expiresAt)}) + try{ + const authorize=()=>current(accepted) + return await (journalOwner?journalOwner.transaction(accepted,authorize,work):runPollStorageTransaction(deps,accepted,authorize,work)) + } + catch(error){if(error instanceof PollStorageUnknown)poisoned=true;throw error} + } + return Object.freeze({ + /** Existing spent reservations may be inspected, never renewed. */ + async inspect(options:ProbeStorageOptions,work:(sql:ProbeStorageSql,row:Record|undefined)=>Promise){return run(options,async sql=>work(sql,await read(sql)))}, + async claim(options:ProbeStorageOptions,writeAnchor:(sql:ProbeStorageSql)=>Promise):Promise{ + options=Object.freeze({deadlineAt:options.deadlineAt,signal:options.signal}) + if(claimStarted||claimed||poisoned)fail();claimStarted=true + const token=randomUUID() + // This COMMIT MUST be acknowledged before anchor SQL is even attempted. + await run(options,async sql=>{ + if(await read(sql))fail() + await assertServerPollArchiveAccounting(sql) + const rows=(await sql.query("INSERT INTO server_poll_storage_admissions(receipt_id,identity_kind,execution_id,connection_id,owner_id,binding_proof,context_proof,authority_issued_at,authority_expires_at,max_stages,claim_token,retention_days,safety_ms,gc_not_before) VALUES($1,$2,$3,$4,$5,$6,$7,$8::timestamptz,$9::timestamptz,$10,$11,1,60000,$9::timestamptz+interval '1 day 60 seconds') RETURNING receipt_id",[scope.receiptId,scope.kind,scope.executionId,connectionId,scope.ownerId,scope.bindingProof,scope.contextProof,new Date(scope.issuedAt).toISOString(),new Date(scope.expiresAt).toISOString(),maxStages,token])).rows + if(rows.length!==1||rows[0].receipt_id!==scope.receiptId)fail() + }) + const result=await run(options,async sql=>{ + const row=await read(sql);if(!row||row.storage_state!=="full"||row.claim_token!==token||row.claim_attempted_at!==null)fail() + const changed=(await sql.query("UPDATE server_poll_storage_admissions SET claim_token=NULL,claim_attempted_at=clock_timestamp() WHERE receipt_id=$1 AND claim_token=$2 AND authority_expires_at>clock_timestamp() RETURNING receipt_id",[scope.receiptId,token])).rows + if(changed.length!==1)fail() + return writeAnchor(sql) + }) + claimed=true;return result + }, + async write(options:ProbeStorageOptions,work:(sql:ProbeStorageSql)=>Promise):Promise{ + if(!claimed||poisoned)fail() + return run(options,async sql=>{const row=await read(sql);if(!row||row.storage_state!=="full"||row.claim_token!==null||row.claim_attempted_at===null)fail();return work(sql)}) + }, + /** Only a blocking marker against an already charged matching identity. + * Fresh 10s lease deliberately survives original revoke/abort/expiry. Old + * run() has released/destroyed its physical backend before this is called. */ + async quarantine(){ + const options={deadlineAt:deps.now()+10000} + journalOwner?.assertClosedForQuarantine() + return runPollStorageTransaction(deps,options,async()=>{},async sql=>{ + if(!await read(sql))fail() + const rows=(await sql.query("SELECT receipt_id,identity_kind,execution_id,context_proof FROM server_poll_quarantines WHERE receipt_id=$1",[scope.receiptId])).rows + if(rows.length){if(rows.length!==1||rows[0].identity_kind!==scope.kind||rows[0].execution_id!==scope.executionId||rows[0].context_proof!==scope.contextProof)fail();return} + await sql.query("INSERT INTO server_poll_quarantines(receipt_id,identity_kind,execution_id,context_proof) VALUES($1,$2,$3,$4)",[scope.receiptId,scope.kind,scope.executionId,scope.contextProof]) + },journalOwner?sql=>admitPollStorageWriter(sql,options.deadlineAt):undefined) + }, + }) +} +export const createServerPollStorage=(binding:PollStorageBinding)=>factory(binding,production) +export const createHolderBoundServerPollStorage=(binding:PollStorageBinding,owner:ServerPollJournalOwner)=>factory(binding,production,owner) +/** Explicit unit/isolated SQL composition, never populated from a request. */ +export const createServerPollStorageForTests=(binding:PollStorageBinding,deps:PollStorageDependencies)=>factory(binding,deps) diff --git a/backend/src/services/server-poll-terminal-archive.owned.test.ts b/backend/src/services/server-poll-terminal-archive.owned.test.ts new file mode 100644 index 00000000..f6e2bc86 --- /dev/null +++ b/backend/src/services/server-poll-terminal-archive.owned.test.ts @@ -0,0 +1,410 @@ +/** One genuine registry/runtime MCP poll, followed by natural terminal expiry. + * Only the disposable owned runner may execute this file. No historical source, + * terminal state, timestamp, deadline or clock is manufactured for eligibility. */ +import assert from "node:assert/strict" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { gzipSync } from "node:zlib" +import { randomUUID } from "node:crypto" +import type { AuthUser } from "../lib/permissions.js" + +async function main() { + assert.equal(process.env.MMAPP_OWN_POLL_TERMINAL_ARCHIVE, "true") + // The optional fault lane is meaningful only after this entry's outer owned + // guard; it never changes source state, clocks, SQL or native responses. + const lostAckVariant = process.env.MMAPP_OWN_POLL_ARCHIVE_LOST_ACK === "true" + const historyVariant = process.env.MMAPP_OWN_POLL_ARCHIVE_HISTORY === "true" + const coldVariant = process.env.MMAPP_OWN_POLL_ARCHIVE_COLD === "true" + assert(!coldVariant||historyVariant,"Cold archive lane is explicit and independent from normal startup history acceptance") + const claimLossVariant = process.env.MMAPP_OWN_POLL_ARCHIVE_CLAIM_LOSS === "true" + assert(Number(historyVariant) + Number(lostAckVariant) + Number(claimLossVariant) <= 1, "History, archive-ACK and claim-ACK variants require separate owned runs") + assert.equal(process.env.NODE_ENV, "test") + const endpoint = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(endpoint.protocol)) + assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.username, "mcp_test"); assert(endpoint.password) + assert.equal(endpoint.pathname, "/postgres"); assert.equal(endpoint.search, ""); assert.equal(endpoint.hash, "") + assert(Number.isInteger(Number(endpoint.port)) && Number(endpoint.port) > 0 && Number(endpoint.port) <= 65535) + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) + assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH = "true" + process.env.MMAPP_OWN_SQL_DB = "postgres" + 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 { buildApp } = await import("../index.js") + const { acceptServerPollPublicRuntimeSql } = await import("../modules/mcp/server-poll-http.sql-helper.js") + const { fingerprint, decryptSecret } = await import("../modules/mcp/security.js") + const { applyPlan, invokeMcpTool } = await import("../modules/mcp/runtime.js") + const { getConnection, updateMcpConnection, rotateConnectionToken, setConnectionStatus, authenticateMcpToken, createMcpConnection } = await import("../modules/mcp/store.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const { tickMcpWorkers } = await import("../modules/mcp/workers.js") + const { openServerPollTerminalArchive, SERVER_POLL_ARCHIVE_MAX_BYTES } = await import("./server-poll-terminal-archive.js") + const { compactCompletedServerPollStorage, compactCompletedServerPollStorageForTests, SERVER_POLL_ARCHIVE_READ_SQL } = await import("./server-poll-terminal-compaction.js") + const { connectProbeStorage } = await import("./server-probe-storage-connection.js") + const { PollStorageUnknown } = await import("./server-poll-storage.js") + let app: Awaited> | undefined + let priorEnabled: boolean | undefined + const currentConnections: { id: string; actor: AuthUser }[] = [] + const groups: string[] = [], started = Date.now() + const journalTables = ["server_poll_executions", "server_poll_intents", "server_poll_acks", "server_poll_receipts", "server_poll_quarantines"] + const ledger = async () => { + const result: Record = {} + for (const table of ["server_poll_storage_admissions", ...journalTables, "server_poll_archives", "server_poll_storage_usage", "mcp_operations", "mcp_operation_steps"]) + result[table] = (await pool.query(`SELECT to_jsonb(t) AS value FROM ${table} t ORDER BY to_jsonb(t)::text`)).rows.map(row => row.value) + if (historyVariant) result.mcpMutationBudget = (await pool.query("SELECT to_jsonb(t) AS value FROM mcp_budget_buckets t WHERE kind='mutations' ORDER BY to_jsonb(t)::text")).rows.map(row => row.value) + return fingerprint(result) + } + // Independent read-only capture for comparison only. This does not call the + // eligibility/source function, sign evidence, mint an archive or alter rows. + const captureOriginal = async (receiptId: string) => { + const owner = await pool.connect() + try { + await owner.query("BEGIN READ ONLY"); await owner.query("SET LOCAL TimeZone='UTC'") + const result = await owner.query(`SELECT jsonb_build_object('admission',to_jsonb(a),'journal',jsonb_build_object( + 'execution',(SELECT to_jsonb(e) FROM server_poll_executions e WHERE e.receipt_id=a.receipt_id), + 'intents',(SELECT COALESCE(jsonb_agg(to_jsonb(i) ORDER BY i.stage),'[]'::jsonb) FROM server_poll_intents i WHERE i.receipt_id=a.receipt_id), + 'acks',(SELECT COALESCE(jsonb_agg(to_jsonb(k) ORDER BY k.stage),'[]'::jsonb) FROM server_poll_acks k WHERE k.receipt_id=a.receipt_id), + 'receipt',(SELECT to_jsonb(r) FROM server_poll_receipts r WHERE r.receipt_id=a.receipt_id), + 'quarantines',(SELECT COALESCE(jsonb_agg(to_jsonb(q) ORDER BY q.created_at,q.receipt_id),'[]'::jsonb) FROM server_poll_quarantines q WHERE q.receipt_id=a.receipt_id)), + 'terminal',jsonb_build_object('operation',to_jsonb(o),'plan',to_jsonb(p),'step',to_jsonb(s)))::text AS source + FROM server_poll_storage_admissions a JOIN mcp_operations o ON o.id=a.execution_id AND o.connection_id=a.connection_id + JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + WHERE a.receipt_id=$1`, [receiptId]) + assert.equal(result.rows.length, 1); assert.equal(typeof result.rows[0].source, "string") + await owner.query("ROLLBACK"); return String(result.rows[0].source) + } finally { owner.release(true) } + } + try { + await applySqlMigrations(pool) + assert.equal(MIGRATION_MANIFEST.at(-1)?.file, "0057_mcp_routing_charge_audit_index.sql") + assert.equal(MIGRATION_MANIFEST.find(pin=>pin.file==="0041_server_poll_terminal_archives.sql")?.sha256,"fbb8935ce977cfc507801f01beb8f303d4817ed63c6bdd4c05799768dfaac9d4") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions")).rows[0].n, 0) + if(!coldVariant)await(await import("../db/bootstrap.js")).initDatabase() + app = await buildApp({ logger: false, startScheduler: false }); await app.ready() + 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") + if(historyVariant)process.env.MMAPP_OWN_SERVER_POLL_DELAYED_APPROVAL="true" + const runtime = await acceptServerPollPublicRuntimeSql(historyVariant ? { cleanupStatus: "paused",delayedApproval:"runtime" } : undefined) + if(historyVariant)assert.equal(runtime.delayedApprovalUsabilityAccepted,true) + assert.equal(runtime.actualDefaultPublicRegistry, true); assert.equal(runtime.actualNativeGets, 4) + const admissions = (await pool.query("SELECT a.*,o.plan_id,o.idempotency_key,p.evidence,c.policy->>'status' AS original_policy_status FROM server_poll_storage_admissions a JOIN mcp_operations o ON o.id=a.execution_id JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_connections c ON c.id=a.connection_id WHERE a.identity_kind='mcp'")).rows + assert.equal(admissions.length, 1) + const a = admissions[0], receiptId = String(a.receipt_id) + assert.equal(a.storage_state, "full"); assert.equal(a.original_policy_status, historyVariant ? "paused" : "revoked") + const evidence = JSON.parse(await decryptSecret(a.evidence.sealed)) + const binding = JSON.parse(await decryptSecret(evidence.steps[0].input.bindingCiphertext)) + assert.equal(binding.plan.request.receiptId, receiptId) + let currentOwner: AuthUser | undefined + if (historyVariant) { + const sourceConnection = await getConnection(String(a.connection_id)) + currentOwner = { id: String(a.owner_id), email: "history-owner@example.test", name: "Owned current history reader", apps: ["mm"], + permissions: [...sourceConnection.owner_permissions], issuedAt: Date.now(), expiresAt: Date.now() + 3600000 } + // Paused is intentionally reversible through official APIs. Revoked + // connections remain permanently revoked in normal/lost-ACK variants. + await rotateConnectionToken(sourceConnection.id, currentOwner) + assert((await pool.query("SELECT revoked_at FROM mcp_credentials WHERE id=$1", [binding.principal.credentialId])).rows[0].revoked_at) + await assert.rejects(applyPlan(binding.principal, String(a.plan_id), String(a.idempotency_key)), (error: unknown) => (error as { code: string }).code === "UNAUTHORIZED") + currentConnections.push({ id: sourceConnection.id, actor: currentOwner }) + } + const beforeSource = await captureOriginal(receiptId), beforeLedger = await ledger() + const originalGzipBytes = gzipSync(Buffer.from(beforeSource), { level: 6 }).length + console.log("Owned actual MCP source footprint:", JSON.stringify({ decodedBytes: Buffer.byteLength(beforeSource), gzipBytes: originalGzipBytes, originalJournalRows: Number(a.used_rows), sourceIsComparisonOnly: true })) + assert(originalGzipBytes <= 96 * 1024 - 256, "Actual MCP source exceeds bounded archive gzip envelope; retain full history, no synthetic positive accepted") + assert.equal((await pool.query("SELECT server_poll_storage_source_payload($1) AS value", [receiptId])).rows[0].value, null) + assert.equal((await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled, true) + assert.deepEqual(await compactCompletedServerPollStorage(), { scanned: 0, compacted: 0, skipped: 0, automaticReplay: false }) + assert.equal(await ledger(), beforeLedger, "Early compaction cannot claim, publish, remove history or change charges") + groups.push(historyVariant + ? "genuine normal registry prepare/human approval/apply completes one MCP poll/four localhost reads; source connection paused and original credential permanently revoked before natural wait" + : "genuine normal registry prepare/human approval/apply completes one MCP poll and four localhost native reads; original connection then revoked") + + // Compaction/reader wiring follows the root-reviewed production service + // interface. No test clock or historical timestamp mutation may replace it. + const waitForNaturalEligibility = async () => { + for (;;) { + const row = (await pool.query(`SELECT server_poll_storage_compact_allowed($1) AS eligible, + GREATEST(a.authority_expires_at,p.expires_at,o.finished_at,s.finished_at)+interval '60 seconds' AS horizon + FROM server_poll_storage_admissions a JOIN mcp_operations o ON o.id=a.execution_id + JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 WHERE a.receipt_id=$1`, [receiptId])).rows[0] + assert(row) + if (row.eligible === true) return + const horizon = new Date(row.horizon).getTime() + assert(Number.isFinite(horizon)) + assert(Date.now() - started < 25 * 60000, "Natural original source horizon did not become eligible; no clock/state mutation permitted") + console.log("Owned poll archive natural-expiry wait:", JSON.stringify({ elapsedMs: Date.now() - started, remainingMs: Math.max(0, horizon - Date.now()), originalPlanExpiryIncluded: true })) + await delay(Math.max(1, Math.min(30000, Math.max(1000, horizon - Date.now() + 10)))) + } + } + await waitForNaturalEligibility() + assert.equal(await captureOriginal(receiptId), beforeSource, "The natural wait changes no historical source timestamp or state") + if (claimLossVariant) { + let commits = 0, connections = 0 + const commands: string[] = [], releases: boolean[] = [] + await assert.rejects(compactCompletedServerPollStorageForTests({ now: Date.now, connect: async options => { + connections++ + const physical = await connectProbeStorage(options) + return { query: async (text, values, queryOptions) => { + commands.push(text) + const result = await physical.query(text, values, queryOptions) + // The physical claim is durable, but no acknowledged claim reaches + // the orchestrator. It must never open the destructive transaction. + if (text === "COMMIT" && ++commits === 1) throw Error("PRIVATE_OWNED_CLAIM_COMMIT_ACK") + return result + }, release: unsafe => { releases.push(Boolean(unsafe)); physical.release(unsafe) } } + } }), (error: unknown) => { + assert(error instanceof PollStorageUnknown) + assert.equal(error.message, "Poll storage outcome unknown; automatic replay forbidden") + assert.equal(error.automaticReplay, false) + return true + }) + assert.equal(commits, 1); assert.equal(connections, 1); assert.deepEqual(releases, [true]) + assert.equal(commands.includes("ROLLBACK"), false) + assert.equal(commands.some(text => /INSERT INTO server_poll_archives/i.test(text)), false) + const retainedSource = await captureOriginal(receiptId) + const original = JSON.parse(beforeSource), retained = JSON.parse(retainedSource) + assert.equal(retained.admission.storage_state, "full") + assert.match(String(retained.admission.compaction_claim), /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i) + assert(Number.isFinite(Date.parse(String(retained.admission.compaction_claimed_at)))) + assert.match(String(retained.admission.compaction_claim_xid), /^[1-9]\d*$/) + for (const field of ["compaction_claim", "compaction_claimed_at", "compaction_claim_xid"]) { + assert.equal(original.admission[field], null) + delete retained.admission[field]; delete original.admission[field] + } + assert.deepEqual(retained, original, "Only the three durable claim markers change; original full journal, operation, plan and step remain exact") + assert.equal(retained.terminal.operation.state, "applied") + assert.equal(retained.terminal.operation.verification, "passed") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_archives")).rows[0].n, 0) + const usage = (await pool.query("SELECT scope,admissions,compacted_admissions,archive_bytes,reserved_rows,reserved_bytes FROM server_poll_storage_usage ORDER BY scope")).rows + assert.deepEqual(usage.map(row => ({ ...row, archive_bytes: Number(row.archive_bytes), reserved_bytes: Number(row.reserved_bytes) })), + ["global", "owner:" + a.owner_id].sort().map(scope => ({ scope, admissions: 1, compacted_admissions: 0, archive_bytes: 0, reserved_rows: 4019, reserved_bytes: 8421376 }))) + groups.push("real first claim COMMIT succeeds but caller ACK is lost; one unsafe backend release, no destructive transaction/archive, original applied source retained with full heavy charge") + const afterClaim = await ledger() + for (const sql of [ + "UPDATE server_poll_storage_admissions SET compaction_claim=NULL,compaction_claimed_at=NULL,compaction_claim_xid=NULL WHERE receipt_id=$1", + "UPDATE server_poll_storage_admissions SET compaction_claim=$2::uuid WHERE receipt_id=$1", + "UPDATE server_poll_storage_admissions SET compaction_claimed_at=clock_timestamp() WHERE receipt_id=$1", + "UPDATE server_poll_storage_admissions SET compaction_claim_xid=pg_current_xact_id() WHERE receipt_id=$1", + ]) { + await assert.rejects(pool.query(sql, sql.includes("$2") ? [receiptId, randomUUID()] : [receiptId])) + assert.equal(await ledger(), afterClaim, "Claim reset/replacement/retimestamp/xid tampering refuses atomically") + } + await assert.rejects(applyPlan(binding.principal, String(a.plan_id), String(a.idempotency_key)), (error: unknown) => (error as { code: string }).code === "UNAUTHORIZED") + assert.deepEqual(await compactCompletedServerPollStorage(), { scanned: 0, compacted: 0, skipped: 0, automaticReplay: false }) + assert.equal(await ledger(), afterClaim, "A claimed full source cannot be replayed by a fresh default compactor or revoked principal") + assert.equal(await captureOriginal(receiptId), retainedSource, "All retained source text remains byte-exact after failed tampering, old apply and default compactor") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='poll.history.compacted' AND payload->>'receiptId'=$1", [receiptId])).rows[0].n, 0) + groups.push("all four durable claim mutations refuse; fresh compactor never replays the spent full claim, revoked principal refuses, no compaction audit or charge release") + console.log(JSON.stringify({ accepted: groups.length, groups, actualMcpSources: 1, actualNativeGets: runtime.actualNativeGets, + naturalWait: true, originalPlanExpiryIncluded: true, historicalTimestampUpdates: false, fakeClock: false, + claimCommitAckLossActualAccepted: true, positiveArchive: false, commitAckLossActualAccepted: false, + originalOperationApplied: true, retainedFullReservedBytes: 8421376, retainedFullReservedRows: 4019, + originalJournalRows: Number(a.used_rows), maintenanceEffects: runtime.maintenanceEffects, elapsedMs: Date.now() - started, + ownerHttpHistoryApiAccepted: false, ownerGlobalReconcileActualAccepted: false, defaultWorkerActualAccepted: false, + originalConnectionRevoked: true, capacity336ActualAccepted: false, actualUnknownSourceCoverage: false, + lostAckVariant, historyVariant, claimLossVariant, backendTestPeakRssKiB: process.resourceUsage().maxRSS, + wholeInstallation2GbAcceptance: false })) + return + } + let commitAckLossAccepted = false + if (historyVariant) { + const lockedPlan = randomUUID(), freePlan = randomUUID(), interruptedPlan = randomUUID() + const terminalPlan = randomUUID() + const fixturePlans = [lockedPlan, freePlan, interruptedPlan, terminalPlan] + await pool.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) + SELECT fixture.id,source.connection_id,source.operation_id,source.operation_version,source.policy_version, + source.generation,'{}'::jsonb,'{}'::jsonb,'draft',now()-interval '1 minute' + FROM mcp_plans source CROSS JOIN unnest($2::uuid[]) AS fixture(id) WHERE source.id=$1`, [a.plan_id, fixturePlans]) + await pool.query("UPDATE mcp_plans SET state='executing' WHERE id=$1", [interruptedPlan]) + await pool.query("UPDATE mcp_plans SET state='completed' WHERE id=$1", [terminalPlan]) + const operation = randomUUID(), terminalOperation = randomUUID(), readLease = randomUUID(), recoveryProfile = randomUUID() + await pool.query(`INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,created_at) + SELECT $1,id,connection_id,$1::uuid::text,'running',now()-interval '3 minutes' + FROM mcp_plans WHERE id=$2`, [operation, interruptedPlan]) + await pool.query(`INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,verification,finished_at) + SELECT $1,id,connection_id,$1::uuid::text,'applied','passed',now() + FROM mcp_plans WHERE id=$2`, [terminalOperation, terminalPlan]) + await pool.query(`INSERT INTO mcp_read_leases(id,connection_id,expires_at) + SELECT $1,connection_id,now()-interval '1 minute' FROM mcp_plans WHERE id=$2`, [readLease, interruptedPlan]) + await pool.query(`INSERT INTO mcp_routing_profiles(id,connection_id,config,enabled,grant_expires_at,credential_id,generation,policy_version) + SELECT $1,p.connection_id,'{}',false,now(),$2,p.generation,p.policy_version + FROM mcp_plans p WHERE id=$3`, [recoveryProfile, binding.principal.credentialId, interruptedPlan]) + await pool.query("INSERT INTO mcp_recovery_intents(operation_id,state,authority_profile_id) VALUES($1,'intent',$2)", [terminalOperation, recoveryProfile]) + const maintenanceState = async () => (await pool.query(`SELECT + (SELECT state FROM mcp_operations WHERE id=$1) AS operation, + EXISTS(SELECT 1 FROM mcp_read_leases WHERE id=$2) AS lease, + (SELECT state FROM mcp_recovery_intents WHERE operation_id=$3) AS intent, + (SELECT count(*)::int FROM mcp_audit WHERE action='operation.interrupted' AND payload->>'operationId'=$1::uuid::text) AS audit`, + [operation, readLease, terminalOperation])).rows[0] + const planStates = async () => Object.fromEntries((await pool.query( + "SELECT id,state FROM mcp_plans WHERE id=ANY($1::uuid[])", [fixturePlans])).rows.map(row => [row.id, row.state])) + const archiveCount = async () => (await pool.query( + "SELECT count(*)::int AS n FROM server_poll_archives WHERE receipt_id=$1", [receiptId])).rows[0].n + const blocker = await pool.connect() + try { + await blocker.query("BEGIN") + assert.equal((await blocker.query("SELECT id FROM mcp_plans WHERE id=$1 FOR UPDATE", [lockedPlan])).rowCount, 1) + assert.equal((await blocker.query("SELECT id FROM mcp_operations WHERE id=$1 FOR UPDATE", [operation])).rowCount, 1) + assert.equal((await blocker.query("SELECT id FROM mcp_read_leases WHERE id=$1 FOR UPDATE", [readLease])).rowCount, 1) + assert.equal((await blocker.query("SELECT operation_id FROM mcp_recovery_intents WHERE operation_id=$1 FOR UPDATE", [terminalOperation])).rowCount, 1) + await tickMcpWorkers() + assert.deepEqual(await planStates(), { [lockedPlan]: "draft", [freePlan]: "expired", [interruptedPlan]: "executing", [terminalPlan]: "completed" }) + assert.deepEqual(await maintenanceState(), { operation: "running", lease: true, intent: "intent", audit: 0 }) + assert.equal(await archiveCount(), 1) + } finally { + try { await blocker.query("ROLLBACK") } finally { blocker.release() } + } + await tickMcpWorkers() + assert.deepEqual(await planStates(), { [lockedPlan]: "expired", [freePlan]: "expired", [interruptedPlan]: "completed", [terminalPlan]: "completed" }) + assert.deepEqual(await maintenanceState(), { operation: "unknown", lease: false, intent: "unknown", audit: 1 }) + assert.equal(await archiveCount(), 1) + groups.push("default worker archives a naturally expired POLL source despite four locked maintenance rows and processes deferred work without duplicate archival on the next tick") + } else if (lostAckVariant) { + let commits = 0 + const commands: string[] = [], releases: boolean[] = [] + await assert.rejects(compactCompletedServerPollStorageForTests({ now: Date.now, connect: async options => { + const physical = await connectProbeStorage(options) + return { query: async (text, values, queryOptions) => { + commands.push(text) + const result = await physical.query(text, values, queryOptions) + // The real second/destructive COMMIT already returned successfully. + // Only its caller ACK is lost; the first claim ACK remains known. + if (text === "COMMIT" && ++commits === 2) throw Error("PRIVATE_OWNED_ARCHIVE_COMMIT_ACK") + return result + }, release: unsafe => { releases.push(Boolean(unsafe)); physical.release(unsafe) } } + } }), (error: unknown) => { + assert(error instanceof PollStorageUnknown) + assert.equal(error.message.includes("PRIVATE_OWNED_ARCHIVE_COMMIT_ACK"), false) + return true + }) + assert.equal(commits, 2); assert.deepEqual(releases, [false, true]) + assert.equal(commands.includes("ROLLBACK"), false, "Unknown COMMIT ACK cannot trigger rollback or replay") + commitAckLossAccepted = true + groups.push("after natural original expiry, real claim COMMIT ACK is known and real archive COMMIT succeeds but loses caller ACK; generic unknown destroys backend without rollback/retry") + } else { + const { PARTITION_WRITER_ADVISORY_KEY } = await import("./partition-write-transaction.js") + const blockedKeys = [ + { text: "SELECT pg_advisory_xact_lock($1::bigint)", value: PARTITION_WRITER_ADVISORY_KEY, scanned: 0 }, + ...["mcp-operation:" + a.execution_id, "server-poll-execution-v1:mcp:" + a.execution_id, + "server-poll-receipt-v1:" + receiptId].map(value => ({ + text: "SELECT pg_advisory_xact_lock(hashtextextended($1,0))", value, scanned: 1, + })), + ] + for (const { text, value, scanned } of blockedKeys) { + const blocker = await pool.connect() + try { + await blocker.query("BEGIN") + await blocker.query(text, [value]) + assert.deepEqual(await compactCompletedServerPollStorage(), + { scanned, compacted: 0, skipped: scanned, automaticReplay: false }) + } finally { + try { await blocker.query("ROLLBACK") } finally { blocker.release() } + } + assert.equal(await captureOriginal(receiptId), beforeSource, "Lock deferral cannot consume the claim") + } + groups.push("exclusive partition writer and each exact operation, execution and receipt lock defer before claim") + const compacted = await compactCompletedServerPollStorage() + assert.deepEqual(compacted, { scanned: 1, compacted: 1, skipped: 0, automaticReplay: false }) + groups.push("natural original plan/admission/activity expiry plus60s passes; production two-transaction one-shot compaction archives one actual MCP source") + } + const archivedRows = (await pool.query(SERVER_POLL_ARCHIVE_READ_SQL, [receiptId])).rows + assert.equal(archivedRows.length, 1) + const archive = archivedRows[0] + const { retireCompletedServerPollOperation } = await import("./server-poll-operation-retirement.js") + const beforeRetirement = await ledger() + await assert.rejects(retireCompletedServerPollOperation({ receiptId, oldArchiveProof: archive.archive_proof })) + assert.equal(await ledger(), beforeRetirement, "Authenticated V1 younger than 180 days retains exact charge") + assert.equal((await pool.query("SELECT version FROM server_poll_archives WHERE receipt_id=$1", [receiptId])).rows[0].version, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operation_replay_tombstones WHERE operation_id=$1", [a.execution_id])).rows[0].n, 0) + assert.equal(Number(archive.actual_ciphertext_bytes), Number(archive.ciphertext_bytes)) + assert(Number(archive.ciphertext_bytes) > 0 && Number(archive.ciphertext_bytes) <= SERVER_POLL_ARCHIVE_MAX_BYTES) + assert.equal(Number(archive.ciphertext_bytes), Buffer.byteLength(archive.ciphertext)) + const opened = await openServerPollTerminalArchive(archive, Date.now()) + const original = JSON.parse(beforeSource) + const archivedAdmission = { ...opened.source.admission }, originalAdmission = { ...original.admission } + assert.equal(typeof archivedAdmission.compaction_claim, "string") + assert.match(String(archivedAdmission.compaction_claim), /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i) + assert(Number.isFinite(Date.parse(String(archivedAdmission.compaction_claimed_at)))) + assert.match(String(archivedAdmission.compaction_claim_xid), /^[1-9]\d*$/) + for (const field of ["compaction_claim", "compaction_claimed_at", "compaction_claim_xid"]) { delete archivedAdmission[field]; delete originalAdmission[field] } + assert.deepEqual(archivedAdmission, originalAdmission, "Every original admission value is retained apart from the acknowledged one-shot claim marker") + assert.deepEqual(opened.source.journal, original.journal) + assert.deepEqual(opened.source.terminal, original.terminal) + assert.deepEqual(JSON.parse(opened.sourceText), opened.source) + assert.equal(Number(archive.decoded_bytes), Buffer.byteLength(opened.sourceText)) + for (const table of journalTables) + assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table} WHERE receipt_id=$1`, [receiptId])).rows[0].n, 0) + const tombstone = (await pool.query("SELECT * FROM server_poll_storage_admissions WHERE receipt_id=$1", [receiptId])).rows[0] + assert.equal(tombstone.storage_state, "compacted"); assert(tombstone.compact_at) + assert.equal(Number(tombstone.archive_bytes), Number(archive.ciphertext_bytes)) + const expectedBytes = 32768 + Number(archive.ciphertext_bytes) + const usage = (await pool.query("SELECT scope,admissions,compacted_admissions,archive_bytes,reserved_rows,reserved_bytes FROM server_poll_storage_usage ORDER BY scope")).rows + assert.deepEqual(usage.map(row => ({ ...row, archive_bytes: Number(row.archive_bytes), reserved_bytes: Number(row.reserved_bytes) })), + ["global", "owner:" + a.owner_id].sort().map(scope => ({ scope, admissions: 0, compacted_admissions: 1, archive_bytes: Number(archive.ciphertext_bytes), reserved_rows: 2, reserved_bytes: expectedBytes }))) + groups.push("production crypto reopens the exact original journal/terminal and all admission fields plus durable claim; children removed, tombstone and actual archive bytes remain charged") + const afterArchive = await ledger() + await assert.rejects(pool.query("UPDATE server_poll_archives SET ciphertext=ciphertext||'AAAA' WHERE receipt_id=$1", [receiptId])) + assert.equal(await ledger(), afterArchive, "SQL archive tampering must roll back without altering retained accounting") + await assert.rejects(applyPlan(binding.principal, String(a.plan_id), String(a.idempotency_key)), (error: unknown) => (error as { code: string }).code === "UNAUTHORIZED") + assert.equal(await ledger(), afterArchive, "Old revoked principal cannot repeat native work or alter compacted history") + assert.deepEqual(await compactCompletedServerPollStorage(), { scanned: 0, compacted: 0, skipped: 0, automaticReplay: false }) + assert.equal(await ledger(), afterArchive) + groups.push("archive UPDATE denied; original revoked principal/idempotency apply refuses, repeat compaction is read-only and produces no new poll or charge") + let ownerHistoryAccepted = false, currentNarrowDenied = false, foreignOwnerDenied = false + if (historyVariant) { + assert(currentOwner) + const serverIds = [...binding.plan.affectedServerIds] as number[] + const configuration = mcpConnectionCreateSchema.parse({ name: "Owned current poll-history grant", mode: "approval", dangerousPolicy: "approval", + expiresAt: new Date(Date.now() + 3600000).toISOString(), permissions: ["mm:servers:write"], + scope: { serverIds, operationIds: ["servers.poll", "operations.global.reconcile"], allowGlobal: true }, + limits: { callsPerMinute: 10000, mutationsPerHour: 10000, serversPerPlan: Math.max(1, serverIds.length), objectsPerPlan: 10000 } }) + await updateMcpConnection(String(a.connection_id), configuration, currentOwner) + const currentToken = await rotateConnectionToken(String(a.connection_id), currentOwner) + await setConnectionStatus(String(a.connection_id), "active", currentOwner) + const currentPrincipal = await authenticateMcpToken(currentToken.token) + assert.notEqual(currentPrincipal.credentialId, binding.principal.credentialId) + await assert.rejects(applyPlan(binding.principal, String(a.plan_id), String(a.idempotency_key)), (error: unknown) => (error as { code: string }).code === "UNAUTHORIZED") + const beforeRead = await ledger() + const result = await invokeMcpTool(currentPrincipal, "mm_global_operation_reconcile", { operationId: String(a.execution_id) }) as { + operationId: string; state: string; results: { matchesDesiredState: boolean; matchesOriginalState: boolean }[]; automaticRetry: boolean + } + assert.equal(result.operationId, String(a.execution_id)); assert.equal(result.state, "applied"); assert.equal(result.automaticRetry, false) + assert.equal(result.results.length, 1); assert.equal(result.results[0].matchesDesiredState, true); assert.equal(result.results[0].matchesOriginalState, false) + for (const secret of ["NEVER_PUBLIC_POLL_USER", "NEVER_PUBLIC_POLL_PASSWORD", String(evidence.steps[0].input.bindingCiphertext), "rawInterfaces", "rawIpAddresses", "context_ciphertext", "receipt_ciphertext", "sourceText"]) + assert.equal(JSON.stringify(result).includes(secret), false, "Archive reconciliation cannot expose private historical payload") + assert.equal(await ledger(), beforeRead, "Actual current-owner read cannot mutate source operations, archive, native journal or charges") + ownerHistoryAccepted = true + await updateMcpConnection(String(a.connection_id), { ...configuration, scope: { ...configuration.scope, serverIds: [] } }, currentOwner) + const narrowToken = await rotateConnectionToken(String(a.connection_id), currentOwner), narrow = await authenticateMcpToken(narrowToken.token) + await assert.rejects(invokeMcpTool(narrow, "mm_global_operation_reconcile", { operationId: String(a.execution_id) }), (error: unknown) => (error as { code: string }).code === "SERVER_DENIED") + currentNarrowDenied = true + const foreignActor: AuthUser = { ...currentOwner, id: "owned-foreign-history-" + randomUUID() } + const foreignAccess = await createMcpConnection(configuration, foreignActor) + currentConnections.push({ id: foreignAccess.connection.id, actor: foreignActor }) + const foreign = await authenticateMcpToken(foreignAccess.token) + await assert.rejects(invokeMcpTool(foreign, "mm_global_operation_reconcile", { operationId: String(a.execution_id) }), (error: unknown) => (error as { code: string }).code === "NOT_FOUND") + foreignOwnerDenied = true + assert.equal(await ledger(), beforeRead) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='poll.history.compacted' AND payload->>'receiptId'=$1", [receiptId])).rows[0].n, 1) + groups.push("actual global reconcile with a new current-owner credential reads original archived history; original credential, narrowed server grant and foreign owner all refuse without new native work or storage charge") + } + console.log(JSON.stringify({ accepted: groups.length, groups, actualMcpSources: 1, actualNativeGets: runtime.actualNativeGets, + naturalWait: true, originalPlanExpiryIncluded: true, historicalTimestampUpdates: false, fakeClock: false, + originalGrantRevoked: true, actualCiphertextBytes: Number(archive.ciphertext_bytes), chargedArchiveBytes: expectedBytes, + originalJournalRows: Number(a.used_rows), maintenanceEffects: runtime.maintenanceEffects, elapsedMs: Date.now() - started, + ownerHttpHistoryApiAccepted: false, ownerGlobalReconcileActualAccepted: ownerHistoryAccepted, defaultWorkerActualAccepted: historyVariant, + currentNarrowGrantActualDenied: currentNarrowDenied, foreignOwnerActualDenied: foreignOwnerDenied, + sourceConnectionPausedThenCurrentGrant: historyVariant, originalConnectionRevoked: !historyVariant, originalCredentialPermanentlyRevoked: historyVariant, + capacity336ActualAccepted: false, actualUnknownSourceCoverage: false, + commitAckLossActualAccepted: commitAckLossAccepted, claimCommitAckLossActualAccepted: false, positiveArchive: true, + lostAckVariant, historyVariant, claimLossVariant, backendTestPeakRssKiB: process.resourceUsage().maxRSS, wholeInstallation2GbAcceptance: false })) + } finally { + try { + for (const connection of currentConnections) await setConnectionStatus(connection.id, "revoked", connection.actor) + if (priorEnabled !== undefined) await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [priorEnabled]) + } + finally { try { if (app) await app.close() } finally { await closePool() } } + } +} +await main() diff --git a/backend/src/services/server-poll-terminal-archive.test.ts b/backend/src/services/server-poll-terminal-archive.test.ts new file mode 100644 index 00000000..6af0c97d --- /dev/null +++ b/backend/src/services/server-poll-terminal-archive.test.ts @@ -0,0 +1,176 @@ +/** Real AES-GCM/HMAC component composition. The trusted source callback here + * tests codec ordering, not actual SQL source/terminal acceptance or authority. */ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from "node:crypto" +import { test } from "node:test" +import { gzipSync } from "node:zlib" +import { canonicalPollArchiveTimestamp, createServerPollTerminalArchiveForTests, ServerPollArchiveRefused, + SERVER_POLL_ARCHIVE_MAX_BYTES, SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES, + type ServerPollArchiveSource, type ServerPollArchiveSourceMetadata, type ServerPollTerminalArchive } from "./server-poll-terminal-archive.js" + +const key = Buffer.from("4f".repeat(32), "hex"), macKey = Buffer.from("7c".repeat(32), "hex") +const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +const canonical = (value: unknown): string => value && typeof value === "object" ? Array.isArray(value) ? "[" + value.map(canonical).join(",") + "]" + : "{" + Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([name, item]) => JSON.stringify(name) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value) +const proof = async (purpose: string, value: unknown) => createHmac("sha256", macKey).update(purpose + ":" + canonical(value)).digest("hex") +const encrypt = (plaintext: Buffer) => { + const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv) + const encrypted = Buffer.concat([cipher.update(plaintext), cipher.final()]) + return Buffer.concat([iv, cipher.getAuthTag(), encrypted]) +} +const decrypt = (encrypted: Buffer) => { + const decipher = createDecipheriv("aes-256-gcm", key, encrypted.subarray(0, 12)) + decipher.setAuthTag(encrypted.subarray(12, 28)) + return Buffer.concat([decipher.update(encrypted.subarray(28)), decipher.final()]) +} +const receiptId = "11111111-1111-4111-8111-111111111111", executionId = "22222222-2222-4222-8222-222222222222", connectionId = "33333333-3333-4333-8333-333333333333" +const privateText = "PRIVATE-PAYLOAD-MUST-NOT-ESCAPE" +const sample = (): ServerPollArchiveSource => ({ + admission: { receipt_id: receiptId, opaque_future_column: { secret: privateText, unicode: "Настройки 🛰️ 東京\u0000", future_flag: false }, last_activity_at: "2026-10-29T03:24:15.123456+00:00" }, + journal: { execution: { receipt_id: receiptId, created_at: "2026-10-29T03:20:00.654321Z", future_native_hint: [null, "雪"] }, + intents: [{ stage: "native.identity", future_opaque_field: { payload: "unchanged" } }], acks: [{ stage: "native.identity", created_at: "2026-10-29T03:20:01.000009Z" }], + receipt: { receipt_ciphertext: "opaque-original-ciphertext", new_receipt_column: 17 }, quarantines: [] }, + terminal: { operation: { id: executionId, unknown_result: { unicode: "Опрос завершён ✅", secret: privateText } }, + plan: { id: "44444444-4444-4444-8444-444444444444", unknown_plan_config: [1, true, null] }, step: { step_index: 0, extra_column: "retained" } }, +}) +const metadata = (): ServerPollArchiveSourceMetadata => ({ receiptId, executionId, connectionId, ownerId: "owned-terminal-component", + bindingProof: "a".repeat(64), contextProof: "b".repeat(64), terminalDigest: "c".repeat(64), originalActivityAt: "2026-10-29T03:24:15.123456+00:00", + originalExpiresAt: "2026-10-29T03:25:00.000009Z", retentionDays: 30, safetyMs: 60000 }) +function fixture(padding = 0) { + const events: string[] = [], counts = { verify: 0, encrypt: 0, decrypt: 0 } + const state = { failSource: false, failEncrypt: false, failProof: false, verified: metadata() } + const codec = createServerPollTerminalArchiveForTests({ + encrypt: async value => { events.push("encrypt"); counts.encrypt++; if (state.failEncrypt) throw Error(privateText); return Buffer.concat([Buffer.alloc(padding), encrypt(value)]) }, + decrypt: async value => { events.push("decrypt"); counts.decrypt++; return decrypt(value.subarray(padding)) }, + proof: async (purpose, value) => { events.push("proof:" + purpose); if (state.failProof) throw Error(privateText); return proof(purpose, value) }, + verifySource: async (source, now) => { events.push("verifySource"); counts.verify++; assert(Number.isSafeInteger(now) && now >= 0); assert(source.admission); if (state.failSource) throw Error(privateText); return structuredClone(state.verified) }, + }) + return { codec, events, counts, state } +} +const refusal = async (work: Promise) => assert.rejects(work, (error: unknown) => { + assert(error instanceof ServerPollArchiveRefused) + assert.equal(error.automaticReplay, false) + assert.equal(String(error).includes(privateText), false) + assert.equal(JSON.stringify(error).includes(privateText), false) + return true +}) +async function resign(row: ServerPollTerminalArchive): Promise { + const { ciphertext: _ciphertext, archive_proof: _archive, ...fields } = row + row.archive_proof = await proof("server-poll-terminal-archive-record-v1", { ...fields, identity_kind: "mcp", storage_state: "compacted", admission_bytes: 32768, charged_rows: 2 }) +} +async function replaceBinary(row: ServerPollTerminalArchive, compressed: Buffer, decoded: Buffer) { + row.ciphertext = encrypt(compressed).toString("base64") + row.compressed_bytes = compressed.length; row.ciphertext_bytes = Buffer.byteLength(row.ciphertext); row.ciphertext_sha256 = sha(row.ciphertext) + row.decoded_bytes = Math.min(decoded.length, SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES) + row.source_sha256 = sha(decoded); row.source_digest = await proof("server-poll-terminal-archive-source-v1", row.source_sha256) + await resign(row) +} + +test("real AES-GCM/HMAC preserves exact sourceText, opaque new raw columns, Unicode and six-digit SQL timestamps", async () => { + const f = fixture(), source = sample(), text = "\n " + JSON.stringify(source, null, 2) + " \n" + const archive = await f.codec.create(text, 1000); assert(archive) + assert.equal(archive.decoded_bytes, Buffer.byteLength(text)); assert.equal(archive.source_sha256, sha(text)) + assert.equal(archive.ciphertext_bytes, Buffer.byteLength(archive.ciphertext)); assert.equal(archive.ciphertext_sha256, sha(archive.ciphertext)) + assert.equal(archive.compressed_bytes, gzipSync(Buffer.from(text), { level: 6 }).length) + assert.equal(Buffer.from(archive.ciphertext, "base64").length, archive.compressed_bytes + 28) + assert.equal(archive.original_activity_at, "2026-10-29T03:24:15.123456Z"); assert.equal(archive.original_expires_at, "2026-10-29T03:25:00.000009Z") + assert.equal(f.events[0], "verifySource"); assert(f.events.indexOf("encrypt") > f.events.indexOf("verifySource")) + const charge = await f.codec.verifyCharge(archive) + assert.equal(charge.bytes, archive.ciphertext_bytes + 32768); assert.equal(charge.rows, 2) + assert.equal(f.counts.decrypt, 0, "Bounded charge verification never decrypts") + f.events.length = 0 + const opened = await f.codec.open(archive, 2000) + assert.equal(opened.sourceText, text); assert.deepEqual(opened.source, source) + assert.deepEqual(Object.keys(opened).sort(), ["metadata", "source", "sourceText"]) + assert.equal(f.events[0], "proof:server-poll-terminal-archive-record-v1") + assert(f.events.indexOf("decrypt") > 0); assert(f.events.indexOf("verifySource") > f.events.indexOf("decrypt")) + assert.equal(f.counts.verify, 2) +}) + +test("every metadata field and its MAC is authenticated before decrypt", async () => { + const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive) + const alternatives: Record, unknown> = { + version: 2, receipt_id: executionId, execution_id: receiptId, connection_id: receiptId, owner_id: "different-owner", binding_proof: "d".repeat(64), context_proof: "d".repeat(64), + source_digest: "d".repeat(64), source_sha256: "d".repeat(64), terminal_digest: "d".repeat(64), original_activity_at: "2026-10-29T03:24:15.123457Z", original_expires_at: "2026-10-29T03:25:00.000010Z", + retention_days: 31, safety_ms: 60001, decoded_bytes: archive.decoded_bytes + 1, compressed_bytes: archive.compressed_bytes + 1, + ciphertext_bytes: archive.ciphertext_bytes + 1, ciphertext_sha256: "d".repeat(64), archive_proof: "d".repeat(64), + } + for (const [field, value] of Object.entries(alternatives)) { + const altered = { ...archive, [field]: value } + await refusal(f.codec.verifyCharge(altered)); await refusal(f.codec.open(altered, 1)) + } + assert.equal(f.counts.decrypt, 0); assert.equal(f.counts.verify, 1) +}) + +test("actual changed ciphertext digest/length or noncanonical base64 refuses before decryption", async () => { + const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive) + await refusal(f.codec.open({ ...archive, ciphertext: archive.ciphertext + "AAAA" }, 1)) + await refusal(f.codec.open({ ...archive, ciphertext: archive.ciphertext.replace(/^./, value => value === "A" ? "B" : "A") }, 1)) + for (const ciphertext of [archive.ciphertext + "\n", "%%%invalid-base64%%%", "AB=="]) { + const altered = { ...archive, ciphertext, ciphertext_bytes: Buffer.byteLength(ciphertext), ciphertext_sha256: sha(ciphertext) } + await resign(altered); await refusal(f.codec.open(altered, 1)) + } + assert.equal(f.counts.decrypt, 0) +}) + +test("real AES authentication tag, gzip corruption and invalid UTF-8 cannot escape generic refusal", async () => { + for (const kind of ["tag", "gzip", "utf8"] as const) { + const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive) + if (kind === "tag") { + const binary = Buffer.from(archive.ciphertext, "base64"); binary[12] ^= 1 + archive.ciphertext = binary.toString("base64"); archive.ciphertext_sha256 = sha(archive.ciphertext); await resign(archive) + } else if (kind === "gzip") await replaceBinary(archive, Buffer.from("not a gzip stream " + privateText), Buffer.from("unused")) + else { const invalid = Buffer.from([0xff, 0xfe, 0x80]); await replaceBinary(archive, gzipSync(invalid), invalid) } + await refusal(f.codec.open(archive, 1)); assert.equal(f.counts.decrypt, 1); assert.equal(f.counts.verify, 1) + } +}) + +test("incompressible or oversized encoded archives retain full history with undefined", async () => { + const incompressible = sample(); incompressible.admission.opaque = randomBytes(180000).toString("base64") + const f = fixture(); assert.equal(await f.codec.create(JSON.stringify(incompressible), 1), undefined) + assert.equal(f.counts.verify, 1) + const padded = fixture(12000), source = sample(); source.admission.opaque = randomBytes(90000).toString("base64") + const text = JSON.stringify(source), compressed = gzipSync(Buffer.from(text), { level: 6 }) + assert(compressed.length < 96 * 1024 - 256) + assert(Buffer.byteLength(Buffer.concat([Buffer.alloc(12000), encrypt(compressed)]).toString("base64")) > SERVER_POLL_ARCHIVE_MAX_BYTES) + assert.equal(await padded.codec.create(text, 1), undefined); assert.equal(padded.counts.encrypt, 1) +}) + +test("authenticated compressed bomb stops at the16MiB decode cap before source verification", async () => { + const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive) + const bomb = Buffer.alloc(SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES + 1, "x") + await replaceBinary(archive, gzipSync(bomb, { level: 6 }), bomb) + assert(archive.compressed_bytes < 96 * 1024 - 256) + await refusal(f.codec.open(archive, 1)); assert.equal(f.counts.decrypt, 1); assert.equal(f.counts.verify, 1) + await refusal(f.codec.create("x".repeat(SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES + 1), 1)) +}) + +test("UTC normalization retains microseconds and rejects invalid calendars/noncanonical timestamps", () => { + assert.equal(canonicalPollArchiveTimestamp("2024-02-29T12:34:56.123456+00:00"), "2024-02-29T12:34:56.123456Z") + assert.equal(canonicalPollArchiveTimestamp("2026-10-29T03:24:15.1Z"), "2026-10-29T03:24:15.100000Z") + for (const invalid of ["2026-02-29T00:00:00Z", "2026-02-30T00:00:00Z", "2026-04-31T00:00:00Z", "2026-10-29T24:00:00Z", "2026-10-29T00:00:60Z", + "2026-10-29T00:00:00.1234567Z", "2026-10-29 00:00:00Z", "2026-10-29T00:00:00+07:00", "2026-10-29", "2026-10-29T00:00:00Z ", "1969-12-31T23:59:59Z", 0, new Date()]) + assert.throws(() => canonicalPollArchiveTimestamp(invalid), ServerPollArchiveRefused) +}) + +test("source validator must accept both seal and open; changed authenticated source metadata cannot be promoted", async () => { + const denied = fixture(); denied.state.failSource = true + await refusal(denied.codec.create(JSON.stringify(sample()), 1)); assert.equal(denied.counts.encrypt, 0) + const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive) + f.state.failSource = true; await refusal(f.codec.open(archive, 1)) + f.state.failSource = false; f.state.verified.retentionDays++ + await refusal(f.codec.open(archive, 1)) + assert.equal(f.counts.verify, 3) +}) + +test("crypto/proof failures expose no private payload; invalid clock never reaches crypto/source", async () => { + for (const mode of ["failEncrypt", "failProof"] as const) { + const f = fixture(); f.state[mode] = true; await refusal(f.codec.create(JSON.stringify(sample()), 1)) + } + const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive) + f.events.length = 0 + for (const invalid of [NaN, Infinity, -1, 1.5, Number.MAX_SAFE_INTEGER + 1]) { + await refusal(f.codec.create(JSON.stringify(sample()), invalid)); await refusal(f.codec.open(archive, invalid)) + } + assert.deepEqual(f.events, []) +}) diff --git a/backend/src/services/server-poll-terminal-archive.ts b/backend/src/services/server-poll-terminal-archive.ts new file mode 100644 index 00000000..99345ed3 --- /dev/null +++ b/backend/src/services/server-poll-terminal-archive.ts @@ -0,0 +1,166 @@ +/** Bounded private terminal-history representation. This module neither claims + * work nor refunds a quota. Only the locked source owner may publish its result. */ +import { createHash } from "node:crypto" +import { gzipSync, gunzipSync } from "node:zlib" +import { z } from "zod" +import type { ServerPollHistoryRows } from "./server-poll-history-verification.js" + +export const SERVER_POLL_ARCHIVE_MAX_BYTES = 131072 +export const SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES = 16 * 1024 * 1024 +const MAX_COMPRESSED_BYTES = 96 * 1024 - 256 +const sha = (value: string | Buffer) => createHash("sha256").update(value).digest("hex") +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +const positive = z.number().int().positive().safe() +const owner = z.string().min(1).max(200).refine(value => value.trim() === value && !/[\x00-\x1f\x7f]/.test(value)) +export class ServerPollArchiveRefused extends Error { + readonly automaticReplay = false + constructor() { super("Terminal poll history cannot be authenticated"); this.name = "ServerPollArchiveRefused" } +} +const refuse = (): never => { throw new ServerPollArchiveRefused() } + +/** UTC, retaining all six PostgreSQL fractional digits. A JS Date alone would + * silently discard microseconds and weaken the original activity binding. */ +export function canonicalPollArchiveTimestamp(raw: unknown): string { + if (typeof raw !== "string") return refuse() + const match = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,6}))?(?:Z|\+00:00)$/.exec(raw) + if (!match) return refuse() + const millis = Date.parse(raw) + if (!Number.isFinite(millis) || millis < 0 || new Date(millis).toISOString().slice(0, 19) !== match[1]) return refuse() + return match[1] + "." + (match[2] ?? "").padEnd(6, "0") + "Z" +} +const timestamp = z.string().transform(canonicalPollArchiveTimestamp) +const metadataSchema = z.object({ + version: z.literal(1), receipt_id: uuid, execution_id: uuid, connection_id: uuid, owner_id: owner, + binding_proof: hash, context_proof: hash, source_digest: hash, source_sha256: hash, terminal_digest: hash, + original_activity_at: timestamp, original_expires_at: timestamp, + retention_days: z.number().int().min(1).max(365), safety_ms: z.literal(60000), + decoded_bytes: positive.max(SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES), compressed_bytes: positive.max(MAX_COMPRESSED_BYTES), + ciphertext_bytes: positive.max(SERVER_POLL_ARCHIVE_MAX_BYTES), ciphertext_sha256: hash, +}) +export type ServerPollArchiveMetadata = z.infer +export type ServerPollTerminalArchive = ServerPollArchiveMetadata & { ciphertext: string; archive_proof: string } +export interface ServerPollArchiveSource { + admission: Record + journal: ServerPollHistoryRows + terminal: { operation: Record; plan: Record; step: Record } +} +export interface ServerPollArchiveSourceMetadata { + receiptId: string; executionId: string; connectionId: string; ownerId: string + bindingProof: string; contextProof: string; originalActivityAt: string; originalExpiresAt: string + retentionDays: number; safetyMs: number; terminalDigest: string +} +export interface ServerPollArchiveDependencies { + encrypt(value: Buffer): Promise + decrypt(value: Buffer): Promise + proof(purpose: string, value: unknown): Promise + /** Test-only composition. Production always calls both fixed source verifiers. */ + verifySource(source: ServerPollArchiveSource, now: number): Promise +} +const sourceSchema = z.object({ admission: z.record(z.string(), z.unknown()), + journal: z.object({ execution: z.record(z.string(), z.unknown()), intents: z.array(z.record(z.string(), z.unknown())).max(2008), + acks: z.array(z.record(z.string(), z.unknown())).max(2008), receipt: z.record(z.string(), z.unknown()), + quarantines: z.array(z.record(z.string(), z.unknown())).max(1) }).strict(), + terminal: z.object({ operation: z.record(z.string(), z.unknown()), plan: z.record(z.string(), z.unknown()), + step: z.record(z.string(), z.unknown()) }).strict(), +}).strict() +const production: ServerPollArchiveDependencies = { + async encrypt(value) { return (await import("../modules/mcp/security.js")).encryptBinary(value) }, + async decrypt(value) { return (await import("../modules/mcp/security.js")).decryptBinary(value) }, + async proof(purpose, value) { + const security = await import("../modules/mcp/security.js") + return security.privateSecretFingerprint(security.fingerprint(value), purpose) + }, + async verifySource(source, now) { + const history = await (await import("./server-poll-history-verification.js")).verifyServerPollHistory(source.journal) + return (await import("./server-poll-terminal-source.js")).verifyServerPollTerminalSource(source, history, now) + }, +} +const RECORD_PURPOSE = "server-poll-terminal-archive-record-v1" +const SOURCE_PURPOSE = "server-poll-terminal-archive-source-v1" + +function codec(raw: ServerPollArchiveDependencies) { + const deps = Object.freeze({ ...raw }) + const source = (text: string): ServerPollArchiveSource => { + if (typeof text !== "string" || !text.length || Buffer.byteLength(text) > SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES) return refuse() + try { return sourceSchema.parse(JSON.parse(text)) } catch { return refuse() } + } + const metadata = (verified: ServerPollArchiveSourceMetadata) => ({ version: 1 as const, receipt_id: verified.receiptId, + execution_id: verified.executionId, connection_id: verified.connectionId, owner_id: verified.ownerId, + binding_proof: verified.bindingProof, context_proof: verified.contextProof, terminal_digest: verified.terminalDigest, + original_activity_at: canonicalPollArchiveTimestamp(verified.originalActivityAt), + original_expires_at: canonicalPollArchiveTimestamp(verified.originalExpiresAt), retention_days: verified.retentionDays, safety_ms: verified.safetyMs }) + const proof = async (purpose: string, value: unknown) => hash.parse(await deps.proof(purpose, value)) + const readMetadata = async (row: unknown) => { + try { + const parsed = metadataSchema.parse(row), archiveProof = hash.parse((row as Record).archive_proof) + if (archiveProof !== await proof(RECORD_PURPOSE, { ...parsed, identity_kind: "mcp", storage_state: "compacted", admission_bytes: 32768, charged_rows: 2 })) return refuse() + return parsed + } catch { return refuse() } + } + return Object.freeze({ + /** Undefined means too large; the caller MUST keep the complete full charge. + * Invalid source is an error, never a compactable partial history. */ + async create(sourceText: string, now: number): Promise { + try { + if (!Number.isSafeInteger(now) || now < 0) return refuse() + const parsed = source(sourceText), verified = await deps.verifySource(parsed, now) + let compressed: Buffer + try { compressed = gzipSync(Buffer.from(sourceText), { level: 6, maxOutputLength: MAX_COMPRESSED_BYTES }) } + catch (error) { + if ((error as { code?: string }).code === "ERR_BUFFER_TOO_LARGE") return undefined + return refuse() + } + const encrypted = await deps.encrypt(compressed), ciphertext = encrypted.toString("base64") + if (Buffer.byteLength(ciphertext) > SERVER_POLL_ARCHIVE_MAX_BYTES) return undefined + const sourceSha = sha(sourceText) + const unsigned = metadataSchema.parse({ ...metadata(verified), source_sha256: sourceSha, + source_digest: await proof(SOURCE_PURPOSE, sourceSha), decoded_bytes: Buffer.byteLength(sourceText), + compressed_bytes: compressed.length, ciphertext_bytes: Buffer.byteLength(ciphertext), ciphertext_sha256: sha(ciphertext) }) + const archive_proof = await proof(RECORD_PURPOSE, { ...unsigned, identity_kind: "mcp", storage_state: "compacted", admission_bytes: 32768, charged_rows: 2 }) + return { ...unsigned, ciphertext, archive_proof } + } catch { return refuse() } + }, + /** SQL must supply ACTUAL ciphertext digest/length, not stored assertions. + * This bounded metadata verifier does not decrypt or hydrate all archives. */ + async verifyCharge(row: unknown): Promise<{ bytes: number; rows: 2; metadata: ServerPollArchiveMetadata }> { + const parsed = await readMetadata(row) + return { bytes: parsed.ciphertext_bytes + 32768, rows: 2, metadata: parsed } + }, + async open(row: unknown, now: number): Promise<{ sourceText: string; source: ServerPollArchiveSource; metadata: ServerPollArchiveMetadata }> { + if (!Number.isSafeInteger(now) || now < 0) return refuse() + const parsed = await readMetadata(row), ciphertext = (row as Record).ciphertext + if (typeof ciphertext !== "string" || Buffer.byteLength(ciphertext) !== parsed.ciphertext_bytes || sha(ciphertext) !== parsed.ciphertext_sha256 || + !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(ciphertext)) return refuse() + try { + const binary = Buffer.from(ciphertext, "base64") + if (binary.toString("base64") !== ciphertext) return refuse() + const compressed = await deps.decrypt(binary) + if (compressed.length !== parsed.compressed_bytes) return refuse() + const decoded = gunzipSync(compressed, { maxOutputLength: SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES }) + if (decoded.length !== parsed.decoded_bytes) return refuse() + const text = decoded.toString("utf8") + if (!Buffer.from(text).equals(decoded) || sha(text) !== parsed.source_sha256 || await proof(SOURCE_PURPOSE, parsed.source_sha256) !== parsed.source_digest) return refuse() + const original = source(text), verified = await deps.verifySource(original, now) + const authenticated = metadataSchema.parse({ ...parsed, ...metadata(verified) }) + if (JSON.stringify(authenticated) !== JSON.stringify(parsed)) return refuse() + return { sourceText: text, source: original, metadata: parsed } + } catch { return refuse() } + }, + }) +} +const productionCodec = codec(production) +export const createServerPollTerminalArchive = productionCodec.create +export const openServerPollTerminalArchive = productionCodec.open +export const verifyServerPollArchiveCharge = productionCodec.verifyCharge +/** Derive the existing fixed verification purpose once for one bounded SQL + * pass. This caches no row, accounting total, permission or admission result. */ +export async function createServerPollArchiveChargeVerifier() { + const security = await import("../modules/mcp/security.js") + const signer = await security.privateFingerprinterExisting(RECORD_PURPOSE) + return codec({ ...production, proof: async (purpose, value) => { + if (purpose !== RECORD_PURPOSE) return refuse() + return signer(security.fingerprint(value)) + } }).verifyCharge +} +/** Explicit deterministic component composition, never accepted from an API. */ +export const createServerPollTerminalArchiveForTests = (dependencies: ServerPollArchiveDependencies) => codec(dependencies) diff --git a/backend/src/services/server-poll-terminal-compaction.test.ts b/backend/src/services/server-poll-terminal-compaction.test.ts new file mode 100644 index 00000000..d50b56d0 --- /dev/null +++ b/backend/src/services/server-poll-terminal-compaction.test.ts @@ -0,0 +1,276 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import { z } from "zod" +import * as model from "./server-poll-storage-model.js" +import * as errorProvenance from "./server-poll-error-provenance.js" +import { admitPartitionWriter, PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" +import type { PollStorageDependencies } from "./server-poll-storage.js" + +/** Actual compactor and transaction dispatcher, explicit SQL/codec/accounting + * doubles. These tests establish control flow, NOT SQL triggers, cryptographic + * provenance, native execution or physical PostgreSQL durability. */ +const compile = (name: string) => ts.transpileModule(readFileSync(new URL(name, import.meta.url), "utf8"), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, +}).outputText +const storageCode = compile("./server-poll-storage.ts"), compactorCode = compile("./server-poll-terminal-compaction.ts") +const ACCOUNTING = "SELECT component_accounting" +type Row = Record +type State = { claim: string | null; archive: Row | null; audits: Row[] } +type Command = { tx: number; text: string; values: unknown[] } +type Api = { compactCompletedServerPollStorageForTests(deps: PollStorageDependencies): Promise<{ scanned: number; compacted: number; skipped: number; automaticReplay: false }> } +function load(code: string, modules: Record) { + const exports: Record = {} + runInNewContext(code, { exports, require(name: string) { assert(Object.hasOwn(modules, name), "Unapproved production import: " + name); return modules[name] }, + Date, Error, Promise, Object, Math, Number, String, JSON, RegExp, setTimeout, clearTimeout, structuredClone }, { filename: "poll-compaction-component.js" }) + return exports +} +function fixture(quarantined = false) { + const now = Date.now(), receiptId = "ffffffff-ffff-4fff-8fff-ffffffffffff", operationId = randomUUID(), connectionId = randomUUID() + const earlierReceipts = Array.from({ length: 128 }, (_, index) => `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`) + const authorityExpiresAt = "2026-10-01 00:00:00.123456+00" + let clock = now, skippedPrefix: "slow" | "full-page" | undefined, removedReceiptId: string | undefined + let state: State = { claim: null, archive: null, audits: [] }, connects = 0, transactions=0, commits = 0, createCalls = 0, openCalls = 0, accountCalls = 0 + let eligible = true, sourceMissing = false, accountingFails = false, preflight: "valid" | "oversize" | "invalid" = "valid", secondOversize = false + let lostCommit = 0, commitPersists = true, badPublishedSize = false, badPublishedText = false, badPublishedId = false, auditFails = false, writer: boolean|Error=true + const commands: Command[] = [], releases: { tx: number; unsafe: boolean }[] = [], events: string[] = [] + class ArchiveRefused extends Error {} + const forbiddenConnector = async (): Promise => { throw Error("Production connector must never run") } + const accounting = async (sql: { query(text: string): Promise }) => { accountCalls++; await sql.query(ACCOUNTING); if (accountingFails) throw new model.PollStorageRefused() } + const storage = load(storageCode, { + "node:crypto": { randomUUID }, zod: { z }, "./server-probe-storage-connection.js": { connectProbeStorage: forbiddenConnector }, + "./server-poll-storage-model.js": model, "./server-poll-archive-accounting.js": { assertServerPollArchiveAccounting: accounting }, + "./server-poll-error-provenance.js": errorProvenance, + "./partition-write-transaction.js": { admitPartitionWriter, PARTITION_WRITER_ADVISORY_KEY }, + "../db/database-restore-quarantine.js": { + databaseRestoreQuarantineEnabled: () => quarantined, + assertDatabaseRestoreQuarantineOpen: async () => { throw new Error("Owned restore quarantine") }, + }, + }) + const archives = new Map() + const api = load(compactorCode, { + "node:crypto": { randomUUID }, "./shared-database-gate.js": { sharedDatabaseGate: { request: (work: () => unknown) => work() } }, + "./server-probe-storage-connection.js": { connectProbeStorage: forbiddenConnector }, "./server-poll-storage.js": storage, + "./server-poll-archive-accounting.js": { assertServerPollArchiveAccounting: accounting }, + "./server-poll-terminal-archive.js": { + ServerPollArchiveRefused: ArchiveRefused, + async createServerPollTerminalArchive(text: string, observedAt: number) { + createCalls++; events.push("create:" + createCalls); assert.equal(observedAt, now) + if (createCalls === 1 && preflight === "invalid") throw new ArchiveRefused() + if (createCalls === 1 && preflight === "oversize" || createCalls === 2 && secondOversize) return undefined + const ciphertext = "component-ciphertext-" + createCalls; archives.set(ciphertext, text) + return { version: 1, receipt_id: receiptId, execution_id: operationId, connection_id: connectionId, owner_id: "component-owner", + binding_proof: "binding-double", context_proof: "context-double", source_digest: "source-double", source_sha256: "sha-double", terminal_digest: "terminal-double", + original_activity_at: "2026-10-01T00:00:00.123456Z", original_expires_at: "2026-10-01T00:00:00.123456Z", decoded_bytes: text.length, + compressed_bytes: 30, ciphertext, ciphertext_bytes: ciphertext.length, archive_proof: "archive-double", retention_days: 30 } + }, + async openServerPollTerminalArchive(archive: Row) { + openCalls++; events.push("open:" + openCalls) + assert(archives.has(String(archive.ciphertext))) + return { sourceText: badPublishedText ? "altered" : archives.get(String(archive.ciphertext)), metadata: { receipt_id: badPublishedId ? operationId : receiptId }, + source: { journal: { intents: [{}, {}], acks: [{}, {}] } } } + }, + }, + }) as unknown as Api + const deps: PollStorageDependencies = { now: () => clock, connect: async options => { + const owner = ++connects; let tx=0, transaction: State | undefined, closed = false + assert.equal(options.deadlineAt, clock + 40000) + return { async query(text, values = [], queryOptions) { + values = Array.from(values) // Normalize the VM caller's array realm only. + assert(!closed); assert(queryOptions && queryOptions.timeoutMs >= 1 && queryOptions.timeoutMs <= 5000) + if(text==="BEGIN")tx=++transactions + commands.push({ tx, text, values }); events.push(tx + ":" + text) + if (text === "BEGIN") { transaction = structuredClone(state); return { rows: [] } } + if (text === "ROLLBACK") { transaction = undefined; return { rows: [] } } + if (text.includes("pg_advisory_unlock_shared")) {assert.equal(transaction,undefined);return {rows:[{released:true}]}} + assert(transaction) + if (text.includes("pg_try_advisory_lock_shared")) {if(writer instanceof Error)throw writer;return {rows:[{acquired:writer}]}} + if (text === "COMMIT") { + commits++; if (commits !== lostCommit || commitPersists) state = structuredClone(transaction) + transaction = undefined + if (commits === lostCommit) throw Error("PRIVATE_DRIVER_COMMIT_ACK_LOST") + return { rows: [] } + } + if (text.includes("current_setting('lock_timeout')")) return { rows: [{ lock_timeout: "0" }] } + if (text.startsWith("SELECT set_config(") || text.startsWith("SELECT pg_catalog.set_config(") || text.startsWith("SET LOCAL ") || text.includes("pg_advisory_xact_lock") || text === ACCOUNTING) return { rows: [] } + if (text.startsWith("SELECT a.receipt_id,") && text.includes("FROM server_poll_storage_admissions a")) { + assert(text.includes("a.compaction_claim IS NULL")); assert(text.includes("a.storage_state='full'")) + const ids = earlierReceipts.slice(0, skippedPrefix === "slow" ? 5 : skippedPrefix === "full-page" ? 128 : 0) + if (eligible && !transaction.claim && !transaction.archive) ids.push(receiptId) + if (values[0] != null) { + assert(text.includes("(a.authority_expires_at,a.receipt_id)>($1::timestamptz,$2::uuid)")) + assert.deepEqual(values, [authorityExpiresAt, values[1]]) + } + return { rows: ids.filter(id => id !== removedReceiptId && (values[0] == null || id > String(values[1]))).slice(0, 128) + .map(receipt_id => ({ receipt_id, authority_expires_at: authorityExpiresAt })) } + } + if (text.startsWith("SELECT server_poll_storage_source_payload(")) { + if (typeof values[0] === "string" && earlierReceipts.includes(values[0])) { + if (skippedPrefix === "slow") clock += 4500 + return { rows: [{ source_text: null }] } + } + assert.deepEqual(values, [receiptId]); return { rows: [{ source_text: sourceMissing ? null : JSON.stringify({ receiptId, claim: transaction.claim }), observed_at: new Date(now) }] } + } + if (text.startsWith("UPDATE server_poll_storage_admissions SET compaction_claim=")) { + assert.equal(transaction.claim, null); assert.equal(values[0], receiptId); assert.equal(typeof values[1], "string") + transaction.claim = String(values[1]); return { rows: [{ receipt_id: receiptId }] } + } + if (text.startsWith("SELECT receipt_id FROM server_poll_storage_admissions")) { + assert.deepEqual(values, [receiptId, transaction.claim]); assert(transaction.claim) + assert(text.includes("compaction_claim_xid IS NOT NULL FOR UPDATE")); return { rows: [{ receipt_id: receiptId }] } + } + if (text.startsWith("INSERT INTO server_poll_archives(")) { + const columns = text.slice(text.indexOf("(") + 1, text.indexOf(")")).split(",") + transaction.archive = Object.fromEntries(columns.map((column, index) => [column, values[index]])) + assert(transaction.claim); return { rows: [{ receipt_id: receiptId }] } + } + if (text.startsWith("SELECT r.*,")) { + assert(transaction.archive); assert(text.includes("pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8'))")); assert(text.includes("octet_length(r.ciphertext)")) + assert(text.includes('HH24:MI:SS.US"Z"')); assert.deepEqual(values, [receiptId]) + return { rows: [{ ...transaction.archive, actual_ciphertext_bytes: Number(transaction.archive.ciphertext_bytes) + (badPublishedSize ? 1 : 0) }] } + } + if (text.startsWith("INSERT INTO mcp_audit(")) { + if (auditFails) throw Error("PRIVATE_DRIVER_AUDIT_FAILURE") + assert.equal(values[1], "poll.history.compacted"); transaction.audits.push({ actor: values[0], connection: values[2], payload: JSON.parse(String(values[3])) }); return { rows: [] } + } + throw Error("Unapproved fixture SQL: " + text) + }, release(unsafe = false) { assert(!closed); closed = true; transaction = undefined; releases.push({ tx:owner, unsafe }) } } + } } + return { run: () => api.compactCompletedServerPollStorageForTests(deps), commands, releases, events, state: () => structuredClone(state), + counts: () => ({ connects, commits, createCalls, openCalls, accountCalls }), Unknown: storage.PollStorageUnknown as new () => Error,setWriter:(value:boolean|Error)=>{writer=value}, + configure(options: { eligible?: boolean; sourceMissing?: boolean; accountingFails?: boolean; preflight?: typeof preflight; secondOversize?: boolean; lostCommit?: number; commitPersists?: boolean; badPublishedSize?: boolean; badPublishedText?: boolean; badPublishedId?: boolean; auditFails?: boolean; skippedPrefix?: typeof skippedPrefix; removeLastSlowReceipt?: boolean }) { + eligible = options.eligible ?? eligible; sourceMissing = options.sourceMissing ?? sourceMissing; accountingFails = options.accountingFails ?? accountingFails + preflight = options.preflight ?? preflight; secondOversize = options.secondOversize ?? secondOversize; lostCommit = options.lostCommit ?? lostCommit + commitPersists = options.commitPersists ?? commitPersists; badPublishedSize = options.badPublishedSize ?? badPublishedSize; badPublishedText = options.badPublishedText ?? badPublishedText; auditFails = options.auditFails ?? auditFails + badPublishedId = options.badPublishedId ?? badPublishedId + skippedPrefix = options.skippedPrefix ?? skippedPrefix + if (options.removeLastSlowReceipt) removedReceiptId = earlierReceipts[3] + } } +} +const claimed = (f: ReturnType) => f.commands.filter(command => command.text.startsWith("UPDATE server_poll_storage_admissions")) +const published = (f: ReturnType) => f.commands.filter(command => command.text.startsWith("INSERT INTO server_poll_archives")) + +test("idle tick skips accounting and codec/key/native dependencies", async () => { + const f = fixture(); f.configure({ eligible: false }) + assert.equal((await f.run()).compacted, 0) + assert.deepEqual(f.counts(), { connects: 1, commits: 1, createCalls: 0, openCalls: 0, accountCalls: 0 }) + assert.equal(claimed(f).length, 0); assert.equal(published(f).length, 0) +}) + +test("only a definite shared session lock miss defers before POLL storage, and SQL failures remain errors",async()=>{ + const blocked=fixture();blocked.setWriter(false) + assert.deepEqual({...await blocked.run()},{scanned:0,compacted:0,skipped:0,automaticReplay:false}) + assert.deepEqual(blocked.commands.map(command=>command.text),["BEGIN","SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired","COMMIT"]) + assert.deepEqual(blocked.releases,[{tx:1,unsafe:false}]) + const unexpected=fixture();unexpected.setWriter(Error("owned unexpected provider failure")) + await assert.rejects(unexpected.run(),model.PollStorageRefused) + assert.deepEqual(unexpected.releases,[{tx:1,unsafe:true}]) + assert.equal(claimed(unexpected).length,0) +}) + +test("quarantine refusal discards the POLL session holding the partition lock", async () => { + const f = fixture(true) + await assert.rejects(f.run(), model.PollStorageRefused) + assert.equal(f.counts().commits, 0) + assert.deepEqual(f.releases, [{ tx: 1, unsafe: true }]) + assert.equal(claimed(f).length, 0) + assert.equal(published(f).length, 0) +}) + +test("ineligible SQL source, oversize and invalid preflight skip without consuming claim", async () => { + for (const options of [{ sourceMissing: true }, { preflight: "oversize" as const }, { preflight: "invalid" as const }]) { + const f = fixture(); f.configure(options); const result = await f.run() + assert.equal(result.compacted, 0); assert.equal(result.skipped, 1) + assert.equal(claimed(f).length, 0); assert.equal(published(f).length, 0); assert.equal(f.counts().connects, 1) + assert.equal(f.state().claim, null); assert.equal(f.counts().accountCalls, 1) + } +}) + +test("slow skipped candidates cannot consume successive scan windows ahead of an eligible row", async () => { + const f = fixture(); f.configure({ skippedPrefix: "slow" }) + assert.deepEqual({ ...await f.run() }, { scanned: 4, compacted: 0, skipped: 4, automaticReplay: false }) + assert.equal(claimed(f).length, 0) + f.configure({ removeLastSlowReceipt: true }) + assert.deepEqual({ ...await f.run() }, { scanned: 2, compacted: 1, skipped: 1, automaticReplay: false }) + assert.equal(claimed(f).length, 1) +}) + +test("a full unclaimable SQL page advances to the next timestamp/receipt tuple", async () => { + // The SQL paging double deliberately exceeds the current 128 FULL-row accounting cap. + const f = fixture(); f.configure({ skippedPrefix: "full-page" }) + assert.deepEqual({ ...await f.run() }, { scanned: 128, compacted: 0, skipped: 128, automaticReplay: false }) + assert.equal(claimed(f).length, 0) + assert.deepEqual({ ...await f.run() }, { scanned: 1, compacted: 1, skipped: 0, automaticReplay: false }) + assert.equal(claimed(f).length, 1) +}) + +test("failed accounting never consumes claim or runs codec", async () => { + const f = fixture(); f.configure({ accountingFails: true }); await assert.rejects(f.run(), model.PollStorageRefused) + assert.equal(claimed(f).length, 0); assert.equal(f.counts().createCalls, 0) + assert.equal(f.commands.filter(command => command.text === "ROLLBACK").length, 1) +}) + +test("phase1 COMMIT ACK loss never starts phase2 or inserts archive, for either database outcome", async () => { + for (const commitPersists of [false, true]) { + const f = fixture(); f.configure({ lostCommit: 1, commitPersists }) + await assert.rejects(f.run(), error => error instanceof f.Unknown && !error.message.includes("PRIVATE_DRIVER")) + assert.equal(f.counts().connects, 1); assert.equal(f.counts().commits, 1); assert.equal(published(f).length, 0) + assert(!f.commands.some(command => command.text === "ROLLBACK")); assert.equal(!!f.state().claim, commitPersists) + assert.equal(f.state().archive, null); assert.deepEqual(f.releases, [{ tx: 1, unsafe: true }]) + } +}) + +test("normal two acknowledged COMMITs publish exact readback and audit in the same transaction", async () => { + const f = fixture(), result = await f.run() + assert.equal(result.compacted, 1); assert.equal(result.automaticReplay, false) + assert.deepEqual(f.counts(), { connects: 1, commits: 2, createCalls: 2, openCalls: 1, accountCalls: 3 }) + const firstCommit = f.events.indexOf("1:COMMIT"), secondBegin = f.events.indexOf("2:BEGIN") + assert(firstCommit >= 0 && firstCommit < secondBegin) + for (const tx of [1, 2]) { + const commands = f.commands.filter(command => command.tx === tx), lock = commands.findIndex(command => command.text.includes("pg_advisory_xact_lock")) + assert(lock >= 0 && lock < commands.findIndex(command => command.text === ACCOUNTING)) + assert(commands.findIndex(command => command.text === ACCOUNTING) < commands.findIndex(command => /^(?:UPDATE server_poll_storage_admissions|INSERT INTO server_poll_archives)/.test(command.text))) + } + const second = f.commands.filter(command => command.tx === 2).map(command => command.text) + const insert = second.findIndex(text => text.startsWith("INSERT INTO server_poll_archives")), read = second.findIndex(text => text.startsWith("SELECT r.*,")), audit = second.findIndex(text => text.startsWith("INSERT INTO mcp_audit")) + assert(insert < read && read < audit && audit < second.lastIndexOf(ACCOUNTING) && second.lastIndexOf(ACCOUNTING) < second.indexOf("COMMIT")) + const opened = f.events.indexOf("open:1") + assert(f.events.findIndex(event => event.startsWith("2:SELECT r.*,")) < opened && + opened < f.events.findIndex(event => event.startsWith("2:INSERT INTO mcp_audit"))) + assert.equal(f.state().audits.length, 1); assert.deepEqual(f.state().audits[0].payload, { operationId: f.state().archive!.execution_id, receiptId: f.state().archive!.receipt_id, + archiveBytes: f.state().archive!.ciphertext_bytes, sourceJournalRows: 6, originalRetentionDays: 30, automaticReplay: false }) + assert.deepEqual(f.releases, [{ tx: 1, unsafe: false }]) +}) + +test("phase2 COMMIT ACK loss leaves spent full or complete archive, never rollback or automatic resume", async () => { + for (const commitPersists of [false, true]) { + const f = fixture(); f.configure({ lostCommit: 2, commitPersists }) + await assert.rejects(f.run(), f.Unknown) + assert(f.state().claim); assert.equal(!!f.state().archive, commitPersists); assert.equal(f.state().audits.length, commitPersists ? 1 : 0) + assert(!f.commands.some(command => command.text === "ROLLBACK")); assert.equal(published(f).length, 1) + assert.deepEqual(f.releases, [{ tx: 1, unsafe: true }]) + const counts = f.counts(); const next = await f.run() + assert.equal(next.scanned, 0); assert.equal(next.compacted, 0); assert.equal(claimed(f).length, 1); assert.equal(published(f).length, 1) + assert.equal(f.counts().createCalls, counts.createCalls); assert.equal(f.counts().accountCalls, counts.accountCalls) + } +}) + +test("phase2 size growth preserves spent full charge and subsequent candidate exclusion", async () => { + const f = fixture(); f.configure({ secondOversize: true }) + assert.equal((await f.run()).skipped, 1); assert(f.state().claim); assert.equal(f.state().archive, null) + assert.equal(published(f).length, 0); assert.equal((await f.run()).scanned, 0); assert.equal(claimed(f).length, 1) + assert(!f.commands.some(command => /^(?:DELETE|UPDATE server_poll_storage_usage)/.test(command.text))) +}) + +test("published size, text or identity mismatch or audit failure rolls back archive but keeps acknowledged claim", async () => { + for (const options of [{ badPublishedSize: true }, { badPublishedText: true }, { badPublishedId: true }, { auditFails: true }]) { + const f = fixture(); f.configure(options); await assert.rejects(f.run(), model.PollStorageRefused) + assert(f.state().claim); assert.equal(f.state().archive, null); assert.equal(f.state().audits.length, 0) + assert.equal(f.commands.filter(command => command.tx === 2 && command.text === "ROLLBACK").length, 1) + assert.equal(f.commands.filter(command => command.tx === 2 && command.text === "COMMIT").length, 0) + assert.equal((await f.run()).scanned, 0); assert.equal(published(f).length, 1) + } +}) diff --git a/backend/src/services/server-poll-terminal-compaction.ts b/backend/src/services/server-poll-terminal-compaction.ts new file mode 100644 index 00000000..0b406862 --- /dev/null +++ b/backend/src/services/server-poll-terminal-compaction.ts @@ -0,0 +1,131 @@ +/** Existing-worker maintenance only. Acknowledged one-shot claim precedes the + * sole destructive transaction. A restart never resumes a claimed full row. */ +import { randomUUID } from "node:crypto" +import { sharedDatabaseGate } from "./shared-database-gate.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import { admitPollStorageWriter, createPollMaintenanceWriter, runPollStorageTransaction, PollStorageRefused, type PollStorageDependencies } from "./server-poll-storage.js" +import { assertServerPollArchiveAccounting } from "./server-poll-archive-accounting.js" +import { createServerPollTerminalArchive, openServerPollTerminalArchive, ServerPollArchiveRefused, type ServerPollTerminalArchive } from "./server-poll-terminal-archive.js" + +const fail: () => never = () => { throw new PollStorageRefused() } +const uuid = /^[a-f\d]{8}-[a-f\d]{4}-[1-8][a-f\d]{3}-[89ab][a-f\d]{3}-[a-f\d]{12}$/i +const production: PollStorageDependencies = { connect: connectProbeStorage, now: Date.now } +// A slow skipped prefix must not consume every tick's bounded scan window. +const scanCursors = new WeakMap() +const CANDIDATE_LIMIT = 128 +const CANDIDATES = `SELECT a.receipt_id,a.authority_expires_at::text AS authority_expires_at FROM server_poll_storage_admissions a + JOIN mcp_operations o ON o.id=a.execution_id AND o.connection_id=a.connection_id + JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + WHERE a.storage_state='full' AND a.compaction_claim IS NULL AND a.identity_kind='mcp' + AND o.state='applied' AND o.verification='passed' AND o.error_code IS NULL AND o.finished_at IS NOT NULL + AND p.operation_id='servers.poll' AND p.operation_version=1 AND p.state='completed' AND p.approved_by IS NOT NULL + AND s.state='verified' AND s.finished_at IS NOT NULL + AND GREATEST(a.authority_expires_at,p.expires_at,o.finished_at,s.finished_at)+a.safety_ms*interval '1 millisecond'($1::timestamptz,$2::uuid)) + ORDER BY a.authority_expires_at,a.receipt_id LIMIT ${CANDIDATE_LIMIT}` + +/** Fixed column projection shared with the bounded history reader. Retention + * metadata belongs to the immutable admission; digest/length are ACTUAL SQL. */ +export const SERVER_POLL_ARCHIVE_READ_SQL = `SELECT r.*, + to_char(r.original_activity_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_activity_at, + to_char(r.original_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_expires_at, + a.retention_days,a.safety_ms,pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8')),'hex') AS ciphertext_sha256, + octet_length(r.ciphertext) AS actual_ciphertext_bytes FROM server_poll_archives r + JOIN server_poll_storage_admissions a USING(receipt_id) WHERE r.receipt_id=$1 AND a.storage_state='compacted' LIMIT 2` + +function insert(archive: ServerPollTerminalArchive) { + // No caller JSON selects columns, table, SQL or identity. SQL owns the entire + // archive→old-children deletion→tombstone→exact-charge transition. + const names = ["receipt_id", "version", "execution_id", "connection_id", "owner_id", "binding_proof", "context_proof", "source_digest", "source_sha256", "terminal_digest", + "original_activity_at", "original_expires_at", "decoded_bytes", "compressed_bytes", "ciphertext", "ciphertext_bytes", "archive_proof"] as const + return { text: `INSERT INTO server_poll_archives(${names.join(",")}) VALUES(${names.map((_name, index) => "$" + (index + 1)).join(",")}) RETURNING receipt_id`, + values: names.map(name => archive[name]) } +} +async function compact(deps:PollStorageDependencies){ + const firstDeadline=deps.now()+20000,writer=createPollMaintenanceWriter(deps,firstDeadline+20000) + try{return await compactOnWriter(writer,firstDeadline,deps)}finally{writer.destroy()} +} + +async function compactOnWriter(writer:ReturnType,firstDeadline:number,sourceDeps:PollStorageDependencies) { + const {deps}=writer + let scanned = 0, skipped = 0 + const claimed = await runPollStorageTransaction(deps, { deadlineAt: firstDeadline }, async () => {}, async sql => { + const previous = scanCursors.get(sourceDeps) + const candidates = (await sql.query(CANDIDATES, [previous?.expiresAt ?? null, previous?.receiptId ?? null])).rows + if (candidates.length > CANDIDATE_LIMIT) fail() + // Idle ticks do not hash the complete retained history. An allocation or + // destructive transition still authenticates it under this same lock. + if (!candidates.length) { scanCursors.delete(sourceDeps); return undefined } + await assertServerPollArchiveAccounting(sql) + for (const candidate of candidates) { + if (deps.now() >= firstDeadline - 2000) break + if (typeof candidate.receipt_id !== "string" || !uuid.test(candidate.receipt_id) || typeof candidate.authority_expires_at !== "string") fail() + scanCursors.set(sourceDeps, { expiresAt: candidate.authority_expires_at, receiptId: candidate.receipt_id }) + scanned++ + // The SQL function takes the exact operation/journal locks without + // waiting on a current connection owner. NULL is an ineligible/busy row. + const rows = (await sql.query("SELECT server_poll_storage_source_payload($1::uuid)::text AS source_text,clock_timestamp() AS observed_at", [candidate.receipt_id])).rows + if (rows.length !== 1) fail() + const text = rows[0].source_text + if (text === null) { skipped++; continue } + if (typeof text !== "string") fail() + const observedAt = new Date(rows[0].observed_at as string | Date).getTime() + try { + // Source proofs, full audit and encoded size pass BEFORE consuming the + // one-shot claim. This preflight publishes nothing and frees nothing. + if (!await createServerPollTerminalArchive(text, observedAt)) { skipped++; continue } + } catch (error) { + if (!(error instanceof ServerPollArchiveRefused)) throw error + skipped++; continue + } + const token = randomUUID() + const changed = (await sql.query("UPDATE server_poll_storage_admissions SET compaction_claim=$2::uuid,compaction_claimed_at=clock_timestamp() WHERE receipt_id=$1::uuid AND storage_state='full' AND compaction_claim IS NULL RETURNING receipt_id", [candidate.receipt_id, token])).rows + if (changed.length !== 1 || changed[0].receipt_id !== candidate.receipt_id) fail() + scanCursors.delete(sourceDeps) + return { receiptId: candidate.receipt_id, token } + } + if (scanned === candidates.length && candidates.length < CANDIDATE_LIMIT) scanCursors.delete(sourceDeps) + return undefined + }, async sql => { + if(!await writer.tryAcquire(sql))return {deferred:undefined} + await admitPollStorageWriter(sql,firstDeadline) + }) + if (!claimed){await writer.finish(firstDeadline);return { scanned, compacted: 0, skipped, automaticReplay: false as const }} + // This branch is reached ONLY after the separate claim COMMIT was ACKed. + // Any loss after here leaves either a complete archive or a charged spent + // full row. Fresh workers select compaction_claim IS NULL and never replay it. + const secondDeadline = deps.now() + 20000 + const compacted = await runPollStorageTransaction(deps, { deadlineAt: secondDeadline }, async () => {}, async sql => { + await assertServerPollArchiveAccounting(sql) + const admission = (await sql.query("SELECT receipt_id FROM server_poll_storage_admissions WHERE receipt_id=$1::uuid AND storage_state='full' AND compaction_claim=$2::uuid AND compaction_claimed_at IS NOT NULL AND compaction_claim_xid IS NOT NULL FOR UPDATE", [claimed.receiptId, claimed.token])).rows + if (admission.length !== 1) fail() + const rows = (await sql.query("SELECT server_poll_storage_source_payload($1::uuid)::text AS source_text,clock_timestamp() AS observed_at", [claimed.receiptId])).rows + if (rows.length !== 1 || typeof rows[0].source_text !== "string") fail() + const text = rows[0].source_text, now = new Date(rows[0].observed_at as string | Date).getTime() + const archive = await createServerPollTerminalArchive(text, now) + if (!archive) return 0 // Oversize retains the FULL charge and spent claim. + if (archive.receipt_id !== claimed.receiptId) fail() + const command = insert(archive), inserted = (await sql.query(command.text, command.values)).rows + if (inserted.length !== 1 || inserted[0].receipt_id !== claimed.receiptId) fail() + const published = (await sql.query(SERVER_POLL_ARCHIVE_READ_SQL, [claimed.receiptId])).rows + if (published.length !== 1 || published[0].actual_ciphertext_bytes !== archive.ciphertext_bytes) fail() + // Authenticate the persisted representation before commit. A refusal rolls + // back both the archive insert and its trigger's journal/charge transition. + const authenticated = await openServerPollTerminalArchive(published[0], now) + if (authenticated.sourceText !== text || authenticated.metadata.receipt_id !== claimed.receiptId) fail() + await sql.query("INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)", + ["mcp-worker", "poll.history.compacted", archive.connection_id, JSON.stringify({ operationId: archive.execution_id, + receiptId: archive.receipt_id, archiveBytes: archive.ciphertext_bytes, + sourceJournalRows: authenticated.source.journal.intents.length + authenticated.source.journal.acks.length + 2, + originalRetentionDays: archive.retention_days, automaticReplay: false })]) + await assertServerPollArchiveAccounting(sql) + return 1 + }, sql => admitPollStorageWriter(sql, secondDeadline)) + await writer.finish(secondDeadline) + return { scanned, compacted, skipped: skipped + (compacted ? 0 : 1), automaticReplay: false as const } +} +export const compactCompletedServerPollStorage = () => sharedDatabaseGate.request(() => compact(production)) +/** Actual owned-SQL fault injection only; not a registered maintenance API. */ +export const compactCompletedServerPollStorageForTests = (deps: PollStorageDependencies) => sharedDatabaseGate.request(() => compact(deps)) diff --git a/backend/src/services/server-poll-terminal-source.test.ts b/backend/src/services/server-poll-terminal-source.test.ts new file mode 100644 index 00000000..0fd0751a --- /dev/null +++ b/backend/src/services/server-poll-terminal-source.test.ts @@ -0,0 +1,220 @@ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHmac, randomBytes, randomUUID } from "node:crypto" +import { test } from "node:test" +import { encodeServerPollJournalAnchor } from "./server-poll-journal-anchor.js" +import { publicationLayout, serverPollMaintenancePublications } from "./server-poll-maintenance-groups.js" +import type { ServerPollEffect } from "./server-poll-execution-model.js" +import { fingerprint, redact } from "../modules/mcp/security.js" +import { verifyServerPollHistoryForTests } from "./server-poll-history-verification.js" +import { verifyServerPollTerminalSourceForTests, type ServerPollTerminalSourceRows, ServerPollTerminalSourceInvalid } from "./server-poll-terminal-source.js" + +async function fixture(version:1|2|3|4=1) { + const key = randomBytes(32), base = Date.now() - 600000, stamp = (offset: number) => new Date(base + offset).toISOString() + const proof = async (domain: string, value: unknown) => createHmac("sha256", key).update(domain).update(fingerprint(value)).digest("hex") + const encrypt = (text: string) => { + const nonce = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, nonce) + const bytes = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]) + return Buffer.concat([nonce, cipher.getAuthTag(), bytes]).toString("base64") + } + const decrypt = async (text: string) => { + const bytes = Buffer.from(text, "base64"), cipher = createDecipheriv("aes-256-gcm", key, bytes.subarray(0, 12)) + cipher.setAuthTag(bytes.subarray(12, 28)); return Buffer.concat([cipher.update(bytes.subarray(28)), cipher.final()]).toString("utf8") + } + const crypto = { decrypt, proof }, id = randomUUID(), receiptId = randomUUID(), connectionId = randomUUID(), planId = randomUUID(), credentialId = randomUUID() + const principal = { connectionId, credentialId, credentialExpiresAt: stamp(240000) } + const maintenance:ServerPollEffect[]=version===4?Array.from({length:17},(_,index)=>({kind:"partition.drop",parent:"server_snapshots",name:"server_snapshots_w_"+new Date(Date.UTC(2000,0,3+index*7)).toISOString().slice(0,10).replaceAll("-","")})):[] + const epochPlan=version===3||version===4 + const core = { version, ...(epochPlan?{approvalProof:"9".repeat(64)}:{}),...(version===4?{maintenanceGroupSize:8}:{}), requiresHuman: true as const, scope: "global" as const, request: { serverId: 1, receiptId }, around: stamp(0), + beforeProof: "a".repeat(64), authorityProof: "b".repeat(64), maintenance, affectedServerIds: [1], mutationCost: maintenance.length+8 } + const plan = { ...core, bindingProof: await proof("server-poll-production-v1", core) } + const context = { identity: { kind: "mcp" as const, ownerId: "original-owner", executionId: id, connectionId, credentialId, operationName: "servers.poll", issuedAt: base, expiresAt: base + 180000 }, plan } + const contextProof = await proof("server-poll-journal-v1:context", context) + const firstProof=epochPlan?"d".repeat(64):plan.beforeProof + const anchorText=await encodeServerPollJournalAnchor(context,epochPlan?{fullProof:firstProof,approvalProof:plan.approvalProof!}:undefined,{proof:(domain,value)=>proof("server-poll-journal-v1:"+domain,value)}) + const snapshot = { id: 42, serverId: 1, polledAt: stamp(1000), status: "online" as const, latencyMs: 0, + rosVersion: null, boardName: null, uptime: null, cpuLoad: null, freeMemory: null, totalMemory: null, identityName: null } + const effects=serverPollMaintenancePublications(plan),count=effects.length+2 + const receipt = { receiptId, configurationVerified: true as const, complete: true as const, maintenanceComplete: true as const, + nativeReads: 4 as const, confirmedPublications: count, completedPublications: count, afterProof: "f".repeat(64), observation: "online" as const, snapshot } + const receiptText = JSON.stringify(receipt), stages = ["native.identity", "native.resource", "native.interfaces", "native.addresses", ...Array.from({length:count},(_,index)=>"publish."+index)] + const intents = [], acks = [] + for (let index = 0; index < stages.length; index++) { + const stage = stages[index], beforeProof = index > 4 ? "e".repeat(64) : firstProof + const intent = { receiptId, stage, beforeProof, effectProof: index>=6?await proof("server-poll-production-v1",effects[index-6]):"c".repeat(64) } + const ack = { receiptId, stage, resultProof: index === stages.length-1 ? receipt.afterProof : "e".repeat(64), outcome: "confirmed" as const } + intents.push({ receipt_id: receiptId, stage, before_proof: beforeProof, effect_proof: intent.effectProof, + intent_proof: await proof("server-poll-journal-v1:intent", intent), created_at: stamp(500 + index * 100) }) + acks.push({ receipt_id: receiptId, stage, result_proof: ack.resultProof, outcome: ack.outcome, + ack_proof: await proof("server-poll-journal-v1:ack", ack), created_at: stamp(550 + index * 100) }) + } + const binding = { version: 1, operation: "servers.poll", principal, ownerId: context.identity.ownerId, issuedAt: base, expiresAt: base + 180000, plan } + const input = { serverId: 1, receiptId, inputDigest: await proof("server-poll-mcp-binding-v1", binding), bindingCiphertext: encrypt(JSON.stringify(binding)) } + const step = { kind: "service", service: "servers.poll", input } + const evidence = { target: { scope: "global" }, objects: [{ kind: "server", id: "1", serverId: 1 }], snapshots: [], steps: [step], before: { proof: plan.beforeProof }, + mutationCost: plan.mutationCost, requiresHuman: true, verificationScope: "configuration", validUntil: stamp(180000), + warnings: ["Полный опрос читает устройство и изменяет общие снимки и разделы истории; требуется подтверждение человеком. Автоматический повтор запрещён."] } + const raw: ServerPollTerminalSourceRows = { + admission: { receipt_id: receiptId, identity_kind: "mcp", execution_id: id, connection_id: connectionId, owner_id: context.identity.ownerId, + storage_state: "full", compact_at: null, archive_bytes: null, original_activity_at: null, + binding_proof: plan.bindingProof, context_proof: contextProof, authority_issued_at: stamp(0), authority_expires_at: stamp(180000), + max_stages: publicationLayout(plan).journalMaxStages, claim_token: null, claim_attempted_at: stamp(300), created_at: stamp(200), retention_days: 14, safety_ms: 60000, + gc_not_before: stamp(180000 + 14 * 86400000 + 60000), used_rows: 2*stages.length+2, used_bytes: 4000 }, + journal: { execution: { receipt_id: receiptId, identity_kind: "mcp", execution_id: id, binding_proof: plan.bindingProof, + context_ciphertext: encrypt(anchorText), context_proof: contextProof, created_at: stamp(400) }, intents, acks, + receipt: { receipt_id: receiptId, receipt_ciphertext: encrypt(receiptText), receipt_proof: await proof("server-poll-journal-v1:receipt", receiptText), created_at: stamp(1400) }, quarantines: [] }, + terminal: { + operation: { id, plan_id: planId, connection_id: connectionId, state: "applied", verification: "passed", error_code: null, + result: { confirmedWrites: 1, verificationScope: "configuration" }, created_at: stamp(100), finished_at: stamp(1600) }, + plan: { id: planId, connection_id: connectionId, operation_id: "servers.poll", operation_version: 1, state: "completed", created_at: stamp(25), approved_by: "human-owner", approved_at: stamp(50), + expires_at: stamp(190000), evidence: { sealed: encrypt(JSON.stringify(evidence)) }, payload: { sealed: encrypt(JSON.stringify({ serverId: 1 })) } }, + step: { operation_id: id, step_index: 0, server_id: null, state: "verified", intended_state: redact(step), finished_at: stamp(1500), + result: { operationId: id, receiptId, nativeReads: 4, confirmedPublications: count, completedPublications: count, complete: true, maintenanceComplete: true, + configurationVerified: true, automaticReplay: false, snapshot } }, + }, + } + const history = await verifyServerPollHistoryForTests(raw.journal, { crypto: { decrypt, proof: (domain, value) => proof("server-poll-journal-v1:" + domain, value) }, + executionProof: value => proof("server-poll-production-v1", value) }) + const now = base + 600000 + const run = () => verifyServerPollTerminalSourceForTests(raw, history, now, crypto) + return { raw, history, crypto, now, run, encrypt, decrypt, proof, binding, evidence, input, stamp } +} + +for(const version of [2,3,4] as const)test(`v${version}: expired original source and exact original plan retain terminal eligibility`,async()=>{ + const f=await fixture(version),result=await f.run() + assert.equal(result.receiptId,f.raw.admission.receipt_id) + assert.equal(f.history.context.plan.version,version) + if(version===3||version===4){ + const payload=JSON.parse(await f.decrypt(String(f.raw.journal.execution.context_ciphertext))) + assert.notEqual(payload.executionStart.fullProof,f.history.context.plan.beforeProof) + assert.equal(f.raw.admission.context_proof,await f.proof("server-poll-journal-v1:context",f.history.context)) + } +}) +for(const version of [3,4] as const)test(`v${version} terminal source refuses missing/replaced start independently of the supplied authenticated history`,async()=>{ + for(const change of ["missing","proof","full","extra"]){ + const f=await fixture(version),payload=JSON.parse(await f.decrypt(String(f.raw.journal.execution.context_ciphertext))) + if(change==="missing")delete payload.executionStart + if(change==="proof")payload.executionStart.proof="0".repeat(64) + if(change==="full")payload.executionStart.fullProof=f.history.context.plan.beforeProof + if(change==="extra")payload.executionStart.extra=true + f.raw.journal.execution.context_ciphertext=f.encrypt(JSON.stringify(payload)) + await assert.rejects(f.run(),ServerPollTerminalSourceInvalid) + } +}) + +test("v4 terminal retains full mutation cost separately from the grouped stage budget",async()=>{ + const f=await fixture(4) + assert.equal(f.history.context.plan.mutationCost,25);assert.equal(f.raw.admission.max_stages,11) + await f.run() + f.raw.admission.max_stages=25 + await assert.rejects(f.run(),ServerPollTerminalSourceInvalid) +}) + +test("expired original credentials need no live renewal: authenticated history and exact terminal return only fixed metadata", async () => { + const f = await fixture(), result = await f.run() + assert(f.now > Date.parse(f.binding.principal.credentialExpiresAt)) + assert.equal(result.originalActivityAt, f.raw.terminal.plan.expires_at) + assert.equal(result.originalExpiresAt, f.raw.admission.authority_expires_at) + assert.equal(result.receiptId, f.raw.admission.receipt_id); assert.equal(result.ownerId, "original-owner") + assert.equal(result.retentionDays, 14); assert.equal(result.safetyMs, 60000) + assert.match(result.terminalDigest, /^[a-f0-9]{64}$/); assert(Object.isFrozen(result)) + assert(!JSON.stringify(result).includes("bindingCiphertext")); assert(!("capability" in result)) + assert.deepEqual(await f.run(), result) +}) + +for (const fault of ["rest", "unknown", "partial", "unverified", "unapproved", "version", "connection", "owner", "claim", "intent", "result", "operation-result", "quarantine", "native-failure", "rows", "context", "receipt", "binding-mac", "payload", "evidence", "time-order"] as const) + test("terminal source refuses " + fault + " without granting credit", async () => { + const f = await fixture(), { raw } = f + if (fault === "rest") raw.admission.identity_kind = "rest" + if (fault === "unknown" || fault === "partial") raw.terminal.operation.state = fault + if (fault === "unverified") raw.terminal.step.state = "applied" + if (fault === "unapproved") raw.terminal.plan.approved_by = null + if (fault === "version") raw.terminal.plan.operation_version = 2 + if (fault === "connection") raw.terminal.operation.connection_id = randomUUID() + if (fault === "owner") raw.admission.owner_id = "other" + if (fault === "claim") raw.admission.claim_token = randomUUID() + if (fault === "intent") raw.terminal.step.intended_state = { service: "other" } + if (fault === "result") raw.terminal.step.result = { complete: true } + if (fault === "operation-result") raw.terminal.operation.result = { confirmedWrites: 1, verificationScope: "configuration", recovery: { state: "verified" } } + if (fault === "quarantine") raw.journal.quarantines.push({ receipt_id: raw.admission.receipt_id }) + if (fault === "native-failure") f.history.source.nativeOutcomes[0].outcome = "read-failed" + if (fault === "rows") raw.admission.used_rows = 13 + if (fault === "context") raw.journal.execution.context_proof = "0".repeat(64) + if (fault === "receipt") raw.journal.receipt.receipt_proof = "0".repeat(64) + if (fault === "binding-mac") { + f.input.inputDigest = "0".repeat(64) + raw.terminal.plan.evidence = { sealed: f.encrypt(JSON.stringify(f.evidence)) } + } + if (fault === "payload") raw.terminal.plan.payload = { sealed: f.encrypt(JSON.stringify({ serverId: 2 })) } + if (fault === "evidence") { + f.evidence.requiresHuman = false + raw.terminal.plan.evidence = { sealed: f.encrypt(JSON.stringify(f.evidence)) } + } + if (fault === "time-order") raw.terminal.operation.finished_at = f.stamp(1000) + await assert.rejects(f.run(), ServerPollTerminalSourceInvalid) + }) + +test("original deadline plus safety is strict; future activity cannot be compacted", async () => { + const f = await fixture(), deadline = Date.parse(String(f.raw.terminal.plan.expires_at)) + 60000 + await assert.rejects(verifyServerPollTerminalSourceForTests(f.raw, f.history, deadline, f.crypto), ServerPollTerminalSourceInvalid) + await verifyServerPollTerminalSourceForTests(f.raw, f.history, deadline + 1, f.crypto) + f.raw.terminal.operation.finished_at = new Date(f.now + 1).toISOString() + await assert.rejects(f.run(), ServerPollTerminalSourceInvalid) +}) + +for (const fault of ["storage-unknown", "storage-compacted", "storage-missing", "compact-time", "compact-bytes", "compact-activity", "compact-undefined", + "missing-plan-created", "future-plan-created", "created-after-approval", "expired-before-approval", "expired-at-operation", "operation-at-original-expiry", "native-step-server", "missing-step-server", "late-ack", "late-receipt"] as const) + test("0041 terminal eligibility rejects " + fault, async () => { + const f = await fixture(), { raw } = f + if (fault === "storage-unknown") raw.admission.storage_state = "unknown" + if (fault === "storage-compacted") raw.admission.storage_state = "compacted" + if (fault === "storage-missing") delete raw.admission.storage_state + if (fault === "compact-time") raw.admission.compact_at = f.stamp(200000) + if (fault === "compact-bytes") raw.admission.archive_bytes = 100 + if (fault === "compact-activity") raw.admission.original_activity_at = f.stamp(190000) + if (fault === "compact-undefined") raw.admission.compact_at = undefined + if (fault === "missing-plan-created") delete raw.terminal.plan.created_at + if (fault === "future-plan-created") raw.terminal.plan.created_at = new Date(f.now + 1).toISOString() + if (fault === "created-after-approval") raw.terminal.plan.created_at = f.stamp(51) + if (fault === "expired-before-approval") raw.terminal.plan.expires_at = f.stamp(49) + if (fault === "expired-at-operation") raw.terminal.plan.expires_at = raw.terminal.operation.created_at + if (fault === "operation-at-original-expiry") raw.terminal.operation.created_at = raw.admission.authority_expires_at + if (fault === "native-step-server") raw.terminal.step.server_id = 1 + if (fault === "missing-step-server") delete raw.terminal.step.server_id + if (fault === "late-ack") raw.journal.acks.at(-1)!.created_at = raw.admission.authority_expires_at + if (fault === "late-receipt") raw.journal.receipt.created_at = raw.admission.authority_expires_at + await assert.rejects(f.run(), ServerPollTerminalSourceInvalid) + }) + +test("confirmed service work before original expiry permits delayed generic terminal SQL publication", async () => { + const f = await fixture() + f.raw.terminal.step.finished_at = f.stamp(181000) + f.raw.terminal.operation.finished_at = f.stamp(182000) + f.raw.terminal.plan.expires_at = f.stamp(179000) + const result = await f.run() + assert.equal(result.originalActivityAt, f.raw.terminal.operation.finished_at) + assert.equal(result.originalExpiresAt, f.raw.admission.authority_expires_at) +}) + +test("activity selection preserves original PostgreSQL microseconds", async () => { + const f = await fixture(), sourceTime = String(f.raw.terminal.plan.expires_at).replace(/\.(\d{3})Z$/, ".$1123+00:00") + f.raw.terminal.plan.expires_at = sourceTime + assert.equal((await f.run()).originalActivityAt, sourceTime) +}) + +test("unknown terminal columns remain in fixed terminal MAC rather than being dropped", async () => { + const f = await fixture(), first = await f.run() + f.raw.terminal.operation.future_opaque = { preserve: "unchanged original evidence" } + const second = await f.run() + assert.notEqual(second.terminalDigest, first.terminalDigest) + assert.deepEqual(f.raw.terminal.operation.future_opaque, { preserve: "unchanged original evidence" }) +}) + +test("caller mutation after the first crypto await cannot replace the captured original source", async () => { + const f = await fixture(), expected = await f.run() + let first = true + const crypto = { ...f.crypto, decrypt: async (text: string) => { + if (first) { first = false; f.raw.terminal.operation.state = "unknown"; f.raw.admission.owner_id = "other" } + return f.decrypt(text) + } } + assert.deepEqual(await verifyServerPollTerminalSourceForTests(f.raw, f.history, f.now, crypto), expected) +}) diff --git a/backend/src/services/server-poll-terminal-source.ts b/backend/src/services/server-poll-terminal-source.ts new file mode 100644 index 00000000..e15aac3b --- /dev/null +++ b/backend/src/services/server-poll-terminal-source.ts @@ -0,0 +1,132 @@ +/** Historical terminal eligibility only. The caller owns the SQL transaction, + * global storage lock, exact source-row locks and nonblocking operation locks. + * This module never renews a grant or creates an execution capability. */ +import { publicationLayout } from "./server-poll-maintenance-groups.js" +import { z } from "zod" +import { openServerPollJournalAnchor } from "./server-poll-journal-anchor.js" +import { decryptSecret, fingerprint, privateSecretFingerprint, redact } from "../modules/mcp/security.js" +import type { ServerPollHistoryRows, VerifiedServerPollHistory } from "./server-poll-history-verification.js" + +export interface ServerPollTerminalSourceRows { + admission: Record; journal: ServerPollHistoryRows + terminal: { operation: Record; plan: Record; step: Record } +} +export interface ServerPollTerminalBinding { + receiptId: string; executionId: string; connectionId: string; ownerId: string; bindingProof: string; contextProof: string + originalActivityAt: string; originalExpiresAt: string; retentionDays: number; safetyMs: number; terminalDigest: string +} +interface Crypto { decrypt(value: string): Promise; proof(domain: string, value: unknown): Promise } +export class ServerPollTerminalSourceInvalid extends Error { + readonly code = "SERVER_POLL_TERMINAL_SOURCE_INVALID" + constructor() { super("Confirmed original server poll terminal source unavailable") } +} +const must = (value: unknown): void => { if (!value) throw new ServerPollTerminalSourceInvalid() } +const uuid = z.string().uuid(), hash = z.string().regex(/^[a-f0-9]{64}$/), sid = z.number().int().positive().safe() +const time = z.union([z.date(), z.string().min(1).max(100)]).refine(value => Number.isFinite(new Date(value).getTime())) +const actor = z.string().min(1).max(200).refine(value => value.trim() === value && !/[\u0000-\u001f\u007f]/.test(value)) +const sealed = z.object({ sealed: z.string().min(1).max(4194304) }).strict() +const admissionSchema = z.object({ receipt_id: uuid, identity_kind: z.literal("mcp"), execution_id: uuid, connection_id: uuid, + storage_state: z.literal("full"), + owner_id: actor, binding_proof: hash, context_proof: hash, authority_issued_at: time, authority_expires_at: time, + max_stages: z.number().int().min(8).max(2008), claim_token: z.null(), claim_attempted_at: time, created_at: time, + retention_days: z.number().int().min(1).max(365), safety_ms: z.literal(60000), gc_not_before: time, + used_rows: z.number().int().min(1).max(4019), used_bytes: z.union([z.number().int().nonnegative().safe(), z.string().regex(/^\d{1,16}$/)]) }).passthrough() +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() +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() +const principalSchema = z.object({ connectionId: uuid, credentialId: uuid, credentialExpiresAt: z.string().datetime() }).strict() +// The complete plan is authenticated by the shared history verifier. Compare +// the same original plan, rather than reconstructing live maintenance/settings. +const bindingSchema = z.object({ version: z.literal(1), operation: z.literal("servers.poll"), principal: principalSchema, + ownerId: actor, issuedAt: z.number().int().nonnegative().safe(), expiresAt: z.number().int().positive().safe(), + plan: z.record(z.string(), z.unknown()) }).strict() +const at = (value: Date | string) => new Date(value).getTime() +const rawTime = (value: Date | string): string => value instanceof Date ? value.toISOString() : value +// Preserve PostgreSQL microseconds when selecting the original activity value. +const micros = (value: Date | string): bigint => BigInt(at(value)) * 1000n + BigInt(typeof value === "string" ? Number((/\.(\d+)/.exec(value)?.[1] ?? "").padEnd(6, "0").slice(3, 6)) : 0) + +async function verify(raw: ServerPollTerminalSourceRows, supplied: VerifiedServerPollHistory, now: number, crypto: Crypto): Promise { + try { + must(Number.isSafeInteger(now) && now > 0 && raw && supplied) + // Capture before crypto awaits. Unknown columns remain in the original + // terminal tuple and in the caller's lossless archived source payload. + const source = structuredClone(raw), history = structuredClone(supplied) + must(Buffer.byteLength(JSON.stringify(source)) <= 16 * 1048576) + const a = admissionSchema.parse(source.admission), o = operationSchema.parse(source.terminal.operation) + const p = planSchema.parse(source.terminal.plan), s = stepSchema.parse(source.terminal.step) + for (const field of ["compact_at", "archive_bytes", "original_activity_at"]) + if (Object.hasOwn(source.admission, field)) must(source.admission[field] === null) + const { identity, plan } = history.context, receipt = history.receipt + must(identity.kind === "mcp") + if (identity.kind !== "mcp") throw new ServerPollTerminalSourceInvalid() + must(o.id === a.execution_id && o.connection_id === a.connection_id && p.id === o.plan_id && p.connection_id === a.connection_id && s.operation_id === o.id) + must(identity.executionId === o.id && identity.ownerId === a.owner_id && identity.connectionId === a.connection_id && identity.operationName === "servers.poll") + must(identity.issuedAt === at(a.authority_issued_at) && identity.expiresAt === at(a.authority_expires_at) && identity.expiresAt > identity.issuedAt) + must((plan.version === 1 || plan.version === 2 || plan.version === 3 || plan.version === 4) && plan.requiresHuman === true && plan.scope === "global" && plan.request.receiptId === a.receipt_id && plan.bindingProof === a.binding_proof) + const layout = publicationLayout(plan) + must(a.max_stages === layout.journalMaxStages && plan.mutationCost === layout.logicalMutationCost && plan.affectedServerIds.includes(plan.request.serverId)) + must(new Set(plan.affectedServerIds).size === plan.affectedServerIds.length && plan.affectedServerIds.length <= 1000) + must(receipt.observation === "online" && receipt.complete && receipt.configurationVerified && receipt.maintenanceComplete && receipt.nativeReads === 4) + must(history.source.nativeOutcomes.length === 4 && history.source.nativeOutcomes.every(row => row.outcome === "confirmed")) + must(source.journal.quarantines.length === 0 && source.journal.acks.every(row => row.outcome === "confirmed")) + must(a.used_rows === source.journal.intents.length + source.journal.acks.length + 2 && Number(a.used_bytes) > 0 && Number(a.used_bytes) <= 8388608) + must(history.source.stageCount === source.journal.intents.length && source.journal.intents.length === source.journal.acks.length) + const anchor = source.journal.execution, terminalReceipt = source.journal.receipt + must(anchor.receipt_id === a.receipt_id && anchor.execution_id === a.execution_id && anchor.identity_kind === "mcp" && anchor.binding_proof === a.binding_proof && anchor.context_proof === a.context_proof) + must(typeof anchor.context_ciphertext === "string" && typeof terminalReceipt.receipt_ciphertext === "string") + const decode = async (value: string, limit: number) => { must(Buffer.byteLength(value) <= limit * 2); const text = await crypto.decrypt(value); must(Buffer.byteLength(text) <= limit); return text } + const contextText = await decode(String(anchor.context_ciphertext), 1048576) + await openServerPollJournalAnchor(JSON.parse(contextText),history.context,{proof:(domain,value)=>crypto.proof("server-poll-journal-v1:"+domain,value)}) + must(a.context_proof === await crypto.proof("server-poll-journal-v1:context", history.context)) + const receiptText = await decode(String(terminalReceipt.receipt_ciphertext), 131072) + must(fingerprint(JSON.parse(receiptText)) === fingerprint(receipt) && terminalReceipt.receipt_proof === await crypto.proof("server-poll-journal-v1:receipt", receiptText)) + const evidence = JSON.parse(await decode(p.evidence.sealed, 2097152)) as Record + const payload = JSON.parse(await decode(p.payload.sealed, 1048576)) + must(fingerprint(payload) === fingerprint({ serverId: plan.request.serverId })) + if (!Array.isArray(evidence.steps) || evidence.steps.length !== 1) throw new ServerPollTerminalSourceInvalid() + const step = evidence.steps[0] as { kind?: unknown; service?: unknown; input?: unknown } + must(step.kind === "service" && step.service === "servers.poll") + const input = inputSchema.parse(step.input), binding = bindingSchema.parse(JSON.parse(await decode(input.bindingCiphertext, 1048576))) + must(input.serverId === plan.request.serverId && input.receiptId === a.receipt_id && input.inputDigest === await crypto.proof("server-poll-mcp-binding-v1", binding)) + must(binding.ownerId === a.owner_id && binding.principal.connectionId === a.connection_id && binding.principal.credentialId === identity.credentialId) + must(binding.issuedAt === identity.issuedAt && binding.expiresAt === identity.expiresAt && binding.expiresAt <= Date.parse(binding.principal.credentialExpiresAt) && fingerprint(binding.plan) === fingerprint(plan)) + const expectedEvidence = { target: { scope: "global" }, objects: plan.affectedServerIds.map(serverId => ({ kind: "server", id: String(serverId), serverId })), + snapshots: [], steps: [{ kind: "service", service: "servers.poll", input }], before: { proof: plan.beforeProof }, mutationCost: plan.mutationCost, + requiresHuman: true, verificationScope: "configuration", validUntil: new Date(binding.expiresAt).toISOString(), + warnings: ["Полный опрос читает устройство и изменяет общие снимки и разделы истории; требуется подтверждение человеком. Автоматический повтор запрещён."] } + must(fingerprint(evidence) === fingerprint(expectedEvidence) && fingerprint(s.intended_state) === fingerprint(redact(step))) + const snapshot = { ...receipt.snapshot } + for (const field of ["latencyMs", "rosVersion", "boardName", "uptime", "cpuLoad", "freeMemory", "totalMemory", "identityName"] as const) snapshot[field] ??= null + const publicResult = { operationId: o.id, receiptId: a.receipt_id, nativeReads: 4, confirmedPublications: receipt.confirmedPublications, + completedPublications: receipt.completedPublications, complete: true, maintenanceComplete: true, configurationVerified: true, automaticReplay: false, snapshot } + must(fingerprint(s.result) === fingerprint(publicResult) && fingerprint(o.result) === fingerprint({ confirmedWrites: 1, verificationScope: "configuration" })) + must(identity.issuedAt <= at(p.approved_at) && micros(p.created_at) <= micros(p.approved_at) && micros(p.approved_at) <= micros(o.created_at)) + must(micros(o.created_at) < micros(p.expires_at) && micros(o.created_at) < BigInt(identity.expiresAt) * 1000n && at(o.created_at) <= at(a.created_at)) + must(at(a.created_at) <= at(a.claim_attempted_at) && at(a.claim_attempted_at) <= at(anchor.created_at as Date | string)) + const originalExpiry = BigInt(identity.expiresAt) * 1000n + must(micros(time.parse(terminalReceipt.created_at)) < originalExpiry) + for (const ack of source.journal.acks) must(micros(time.parse(ack.created_at)) < originalExpiry) + must(at(terminalReceipt.created_at as Date | string) <= at(s.finished_at) && at(s.finished_at) <= at(o.finished_at)) + must(at(history.source.createdAt) === at(anchor.created_at as Date | string) && at(history.source.completedAt) === at(terminalReceipt.created_at as Date | string)) + const dates = [a.authority_expires_at, a.created_at, a.claim_attempted_at, p.expires_at, o.finished_at, s.finished_at, + time.parse(anchor.created_at), time.parse(terminalReceipt.created_at), ...source.journal.intents.map(row => time.parse(row.created_at)), ...source.journal.acks.map(row => time.parse(row.created_at))] + const activity = dates.reduce((latest, value) => micros(value) > micros(latest) ? value : latest) + must(micros(activity) + BigInt(a.safety_ms) * 1000n < BigInt(now) * 1000n) + must(at(a.gc_not_before) >= at(a.authority_expires_at) + a.retention_days * 86400000 + a.safety_ms) + const tuple = { format: "server-poll-terminal-source-v1", receiptId: a.receipt_id, executionId: a.execution_id, connectionId: a.connection_id, + ownerId: a.owner_id, bindingProof: a.binding_proof, contextProof: a.context_proof, terminal: source.terminal } + const terminalDigest = hash.parse(await crypto.proof("server-poll-terminal-source-v1", tuple)) + return Object.freeze({ receiptId: a.receipt_id, executionId: a.execution_id, connectionId: a.connection_id, ownerId: a.owner_id, + bindingProof: a.binding_proof, contextProof: a.context_proof, originalActivityAt: rawTime(activity), originalExpiresAt: rawTime(a.authority_expires_at), + retentionDays: a.retention_days, safetyMs: a.safety_ms, terminalDigest }) + } catch { throw new ServerPollTerminalSourceInvalid() } +} + +export const verifyServerPollTerminalSource = (source: ServerPollTerminalSourceRows, history: VerifiedServerPollHistory, now: number) => verify(source, history, now, + { decrypt: decryptSecret, proof: (domain, value) => privateSecretFingerprint(fingerprint(value), domain) }) +/** Explicit unit-only crypto. No SQL/public caller can select this composition. */ +export const verifyServerPollTerminalSourceForTests = (source: ServerPollTerminalSourceRows, history: VerifiedServerPollHistory, now: number, crypto: Crypto) => verify(source, history, now, crypto) diff --git a/backend/src/services/server-probe-authority.test.ts b/backend/src/services/server-probe-authority.test.ts new file mode 100644 index 00000000..00daa8eb --- /dev/null +++ b/backend/src/services/server-probe-authority.test.ts @@ -0,0 +1,85 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import type { ConnectionRow } from "../modules/mcp/store.js" +import { McpError } from "../modules/mcp/security.js" +import { createMcpServerProbeAuthority, createMcpServerProbeReadAuthority, createRestServerProbeAuthority, SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION, type ServerProbeAuthorityDependencies } from "./server-probe-authority.js" + +const originalQuery = pool.query, originalConnect = pool.connect +pool.query = (() => { throw Error("Actual SQL forbidden") }) as never +pool.connect = (() => { throw Error("Actual SQL forbidden") }) as never +let cases = 0 +function fixture() { + let time = 1000000, revoked = false, reads = 0, portalReads = 0, executionChecks = 0 + let protections: Record[] = [], afterRead: (() => void) | undefined + const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(2000000).toISOString() } + const connection = { id: principal.connectionId, owner_id: "owner", owner_permissions: ["mm:servers:write"], generation: "generation", owner_valid_until: new Date(2000000), policy: { status: "active", mode: "approval", expiresAt: principal.credentialExpiresAt, permissions: ["mm:servers:write"], scope: { serverIds: [2, 1], operationIds: [SERVER_PROBE_OPERATION], allowGlobal: false } } } as ConnectionRow + const actor: AuthUser = { id: "portal-owner", email: "", name: "", apps: ["mm"], permissions: ["mm:servers:write"], issuedAt: 900000, expiresAt: 1900000 } + const deps: ServerProbeAuthorityDependencies = { + now: () => time, + connection: async () => { if (revoked) throw new McpError("PERMISSION_DENIED", "revoked", 403); return structuredClone(connection) }, + portal: async () => { portalReads++; if (revoked) throw new McpError("OWNER_CLAIMS_REVOKED", "revoked", 401) }, + verified: async () => { executionChecks++; if (revoked) throw new McpError("PERMISSION_DENIED", "no execution", 403) }, + query: async sql => { reads++; const rows = sql.includes("FROM mcp_protections") ? structuredClone(protections) : [{ id: 1 }, { id: 2 }]; afterRead?.(); return rows }, + } + return { deps, principal, connection, actor, counts: () => ({ reads, portalReads, executionChecks }), revoke: () => { revoked = true }, advance: (value: number) => { time += value }, protections: (value: Record[]) => { protections = value }, afterRead: (hook: () => void) => { afterRead = hook } } +} +async function denied(work: Promise) { await assert.rejects(work, error => error instanceof McpError); cases++ } +try { + { + const f = fixture(), auth = createMcpServerProbeAuthority(f.principal, { executing: true }, f.deps) + const value = await auth.readAuthority() + assert.equal(value.transport, "mcp"); assert.deepEqual(value.scopeServerIds, [1, 2]); assert.equal(f.counts().executionChecks, 1) + await auth.assertProtections([1, 2], value); cases++ + f.connection.policy.scope.serverIds = [1] + await denied(auth.assertProtections([1, 2], value)) + } + for (const mutate of [(f: ReturnType) => { f.connection.policy.mode = "observer" }, (f: ReturnType) => { f.connection.policy.scope.operationIds = [] }, (f: ReturnType) => { f.connection.owner_permissions = [] }, (f: ReturnType) => { f.connection.owner_valid_until = new Date(999999) }, (f: ReturnType) => f.revoke()]) { + const f = fixture(); mutate(f) + await denied(createMcpServerProbeAuthority(f.principal, {}, f.deps).readAuthority()) + assert.equal(f.counts().reads, 0) + } + { + const f = fixture() + assert.throws(() => createRestServerProbeAuthority(undefined, { requestId: "http1", acceptedAt: 1000000, authRequired: true }, f.deps), McpError); cases++ + const auth = createRestServerProbeAuthority(undefined, { requestId: "http1", acceptedAt: 1000000, authRequired: false }, f.deps), value = await auth.readAuthority() + assert.equal(value.transport, "rest"); assert.equal(value.ownerId, "local-operator"); assert.equal(value.connectionId, undefined); assert.equal(value.credentialId, undefined) + assert.equal(f.counts().portalReads, 0); assert.equal(value.requestId, "http1"); cases++ + f.advance(300001); await denied(auth.readAuthority()) + } + { + const f = fixture(), auth = createRestServerProbeAuthority(f.actor, { requestId: "http2", acceptedAt: 1000000, authRequired: true }, f.deps) + const value = await auth.readAuthority() + assert.equal(value.ownerId, f.actor.id); assert.equal(value.issuedAt, f.actor.issuedAt); assert.equal(value.expiresAt, 1300000); assert.equal(f.counts().portalReads, 2); cases++ + f.afterRead(f.revoke) + await denied(auth.readAuthority()) + } + for (const protection of ["deny", "human-approval-required", "unknown"]) { + const f = fixture(), auth = createRestServerProbeAuthority(f.actor, { requestId: "http3", acceptedAt: 1000000, authRequired: true }, f.deps), value = await auth.readAuthority() + f.protections([{ object_ref: { kind: "server", id: "1", protection } }]) + await denied(auth.assertProtections([1], value)) + } + { + const f = fixture(), auth = createMcpServerProbeAuthority(f.principal, {}, f.deps), value = await auth.readAuthority() + f.protections([{ object_ref: { kind: "server", id: "1", protection: "human-approval-required" } }]) + await auth.assertProtections([1], value); cases++ + f.protections([{ object_ref: { kind: "server", id: "1", protection: "deny" } }]) + await denied(auth.assertProtections([1], value)) + } + { + const f = fixture(), controller = new AbortController(), auth = createMcpServerProbeAuthority(f.principal, { signal: controller.signal }, f.deps) + controller.abort(); await denied(auth.readAuthority()); assert.equal(f.counts().reads, 0) + } + { + const f = fixture() + f.connection.policy.mode = "observer"; f.connection.policy.scope.operationIds = [SERVER_PROBE_RECEIPT_READ_OPERATION] + f.connection.owner_permissions = ["mm:servers:read"]; f.connection.policy.permissions = ["mm:servers:read"] + const auth = createMcpServerProbeReadAuthority(f.principal, {}, f.deps), value = await auth.readAuthority() + await auth.assertProtections([1], value); cases++ + await denied(createMcpServerProbeAuthority(f.principal, {}, f.deps).readAuthority()) + f.connection.policy.scope.operationIds = [SERVER_PROBE_OPERATION] + await denied(auth.readAuthority()) + } + console.log(`Server probe authority: ${cases} inert cases passed; live MCP scope/owner/execution and REST JWT/local request identities, protection/expiry/revoke fences; no actual SQL`) +} finally { pool.query = originalQuery; pool.connect = originalConnect } diff --git a/backend/src/services/server-probe-authority.ts b/backend/src/services/server-probe-authority.ts new file mode 100644 index 00000000..cd486461 --- /dev/null +++ b/backend/src/services/server-probe-authority.ts @@ -0,0 +1,102 @@ +import type { AuthUser } from "../lib/permissions.js" +import { hasPermission } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { effectiveMcpConnection, type McpPrincipal } from "../modules/mcp/store.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { assertVerifiedConfigurationAccess } from "./config-mutation-policy.js" +import type { ServerProbeAuthority } from "./server-probe-execution.js" + +export const SERVER_PROBE_OPERATION = "servers.probes.run" +export const SERVER_PROBE_RECEIPT_READ_OPERATION = "servers.probes.receipt.read" +const fail = (message: string, code = "PERMISSION_DENIED", status = 403): never => { throw new McpError(code, message, status) } +const production = { + connection: effectiveMcpConnection, + portal: assertPortalClaimsCurrent, + verified: assertVerifiedConfigurationAccess, + now: Date.now, + query: async (sql: string, values: unknown[]) => (await pool.query(sql, values)).rows as Record[], +} +export type ServerProbeAuthorityDependencies = typeof production +function live(signal: AbortSignal | undefined, expiresAt: number, now: number) { + if (signal?.aborted) fail("Диагностика отменена", "READ_CANCELLED", 409) + if (!Number.isSafeInteger(expiresAt) || expiresAt <= now) fail("Срок допуска диагностики истёк") +} +function ids(value: unknown): number[] { + if (!Array.isArray(value) || value.length > 1000 || value.some(id => !Number.isSafeInteger(id) || id <= 0) || new Set(value).size !== value.length) return fail("Полная область диагностики недоступна", "STATE_UNAVAILABLE", 503) + return [...value].sort((a, b) => a - b) +} +/** Trusted constructors only. None of the transport/actor/admission options is + * accepted from a tool's or REST request's JSON. No I/O occurs at construction. */ +function createMcpProbeAuthority(principal: McpPrincipal, options: { signal?: AbortSignal; executing?: boolean }, overrides: Partial, permission: "mm:servers:read" | "mm:servers:write", operation: string) { + const deps = { ...production, ...overrides } + const readAuthority = async (): Promise => { + live(options.signal, Date.parse(principal.credentialExpiresAt), deps.now()) + if (options.executing) await deps.verified() + const current = await deps.connection(principal) + const expiresAt = Math.min(Date.parse(principal.credentialExpiresAt), Date.parse(current.policy.expiresAt), current.owner_valid_until.getTime()) + live(options.signal, expiresAt, deps.now()) + if (current.id !== principal.connectionId || current.policy.status !== "active" || permission === "mm:servers:write" && current.policy.mode === "observer" || !current.policy.scope.operationIds.includes(operation) || !hasPermission(current.owner_permissions, permission) || !hasPermission(current.policy.permissions, permission)) return fail("Нет актуального права серверной диагностики") + return { transport: "mcp", ownerId: current.owner_id, connectionId: current.id, credentialId: principal.credentialId, expiresAt, generation: current.generation, + policyProof: fingerprint(current.policy), ownerProof: fingerprint(current.owner_permissions), active: true, operationAllowed: true, + scopeServerIds: ids(current.policy.scope.serverIds), ownerPermissions: [...current.owner_permissions], policyPermissions: [...current.policy.permissions] } + } + return { readAuthority, assertProtections: protectionReader(readAuthority, deps, true) } +} +export function createMcpServerProbeAuthority(principal: McpPrincipal, options: { signal?: AbortSignal; executing?: boolean } = {}, overrides: Partial = {}) { + return createMcpProbeAuthority(principal, options, overrides, "mm:servers:write", SERVER_PROBE_OPERATION) +} +/** Historical public measurements use today's read grant, never an expired + * execution credential. This cannot prepare or run a diagnostic command. */ +export function createMcpServerProbeReadAuthority(principal: McpPrincipal, options: { signal?: AbortSignal } = {}, overrides: Partial = {}) { + return createMcpProbeAuthority(principal, options, overrides, "mm:servers:read", SERVER_PROBE_RECEIPT_READ_OPERATION) +} + +export function createRestServerProbeAuthority(actor: AuthUser | undefined, request: { requestId: string; acceptedAt: number; authRequired: boolean; signal?: AbortSignal }, overrides: Partial = {}, resolveServerIds?: () => Promise) { + const deps = { ...production, ...overrides } + if (!request.requestId || request.requestId.length > 256 || !Number.isSafeInteger(request.acceptedAt) || request.acceptedAt <= 0) return fail("Не подтверждён HTTP-контекст диагностики", "INVALID_INPUT", 400) + // Open standalone mode is a server configuration, never a caller flag. Its + // local actor is recorded as REST; it cannot manufacture an MCP connection. + const local = !request.authRequired && !actor + if (!local && (!actor || !actor.id || !hasPermission(actor.permissions, "mm:servers:write"))) return fail("Нет права серверной диагностики") + const issuedAt = actor?.issuedAt ?? request.acceptedAt, expiresAt = Math.min(actor?.expiresAt ?? Infinity, request.acceptedAt + 300000) + const portalAccess = async () => { + if (actor) { + if (!hasPermission(actor.permissions, "mm:servers:write")) return fail("Право серверной диагностики отозвано") + await deps.portal(actor) + if (!hasPermission(actor.permissions, "mm:servers:write")) return fail("Право серверной диагностики отозвано") + } + } + const readAuthority = async (): Promise => { + live(request.signal, expiresAt, deps.now()) + if (issuedAt > deps.now()) return fail("JWT диагностики выдан в будущем") + await portalAccess() + live(request.signal, expiresAt, deps.now()) + // JWT write permission applies to all servers. Read bounded IDs only, never + // credentials, before establishing the complete allowed source set. + const serverIds = resolveServerIds ? ids([...await resolveServerIds()]) : ids((await deps.query("SELECT id FROM servers ORDER BY id LIMIT 1001", [])).map(row => row.id)) + await portalAccess() + live(request.signal, expiresAt, deps.now()) + const permissions = local ? ["mm:servers:write"] : [...actor!.permissions] + return { transport: "rest", requestId: request.requestId, issuedAt, ownerId: actor?.id ?? "local-operator", expiresAt, generation: fingerprint({ kind: local ? "local-request" : "portal-jwt", requestId: request.requestId, issuedAt, expiresAt }), + policyProof: fingerprint({ transport: "rest", authentication: local ? "local" : "jwt", operation: SERVER_PROBE_OPERATION, serverIds, expiresAt }), ownerProof: fingerprint(permissions), + active: true, operationAllowed: true, scopeServerIds: serverIds, ownerPermissions: permissions, policyPermissions: [...permissions] } + } + return { readAuthority, assertProtections: protectionReader(readAuthority, deps, false) } +} + +function protectionReader(readAuthority: () => Promise, deps: ServerProbeAuthorityDependencies, humanApprovedPlan: boolean) { + return async (serverIds: number[], expected: ServerProbeAuthority): Promise => { + const requested = ids(serverIds), before = await readAuthority() + if (fingerprint(before) !== fingerprint(expected) || requested.some(id => !before.scopeServerIds.includes(id))) return fail("Допуск участников диагностики изменился", "STATE_CONFLICT", 409) + const rows = await deps.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[])) LIMIT 1001", [requested.map(String)]) + const after = await readAuthority() + if (fingerprint(before) !== fingerprint(after)) return fail("Допуск изменился во время проверки защит", "STATE_CONFLICT", 409) + if (rows.length > 1000) return fail("Каталог защит превышает предел", "STATE_UNAVAILABLE", 503) + for (const row of rows) { + const ref = row.object_ref as { protection?: unknown } | null + if (!ref || !["deny", "human-approval-required"].includes(String(ref.protection))) return fail("Неизвестная защита диагностики", "STATE_UNAVAILABLE", 503) + if (ref.protection === "deny" || !humanApprovedPlan) return fail("Диагностика заблокирована защитой; нужен разрешённый MCP-план", ref.protection === "deny" ? "PROTECTED_OBJECT" : "APPROVAL_REQUIRED", 409) + } + } +} diff --git a/backend/src/services/server-probe-execution-model.ts b/backend/src/services/server-probe-execution-model.ts new file mode 100644 index 00000000..b4c31b66 --- /dev/null +++ b/backend/src/services/server-probe-execution-model.ts @@ -0,0 +1,117 @@ +import { isIP, isIPv4 } from "node:net" +import { z } from "zod" + +const integer = z.number().int().positive().safe(), text = z.string().trim().min(1).max(1024) +const traceTime = z.string().trim().min(1).max(32).refine(value => { + const unit = /^(\d+(?:\.\d+)?)s$|^(\d+)ms$/.exec(value) + if (unit) return Number.isFinite(Number(unit[1] ?? unit[2])) && Number(unit[1] ?? unit[2]) > 0 + const clock = /^(\d{1,2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?$/.exec(value) + return !!clock && Number(clock[2]) < 60 && Number(clock[3]) < 60 && [clock[1], clock[2], clock[3], clock[4]].some(part => Number(part) > 0) +}) +/** Complete documented POST /servers/:id/probes/run inputs. Unknown fields are + * rejected in this typed foundation; existing REST is not changed by import. */ +export const serverProbeExecutionRequest = z.object({ + tool: z.string().trim().pipe(z.enum(["ping", "traceroute", "bandwidth", "dns", "route", "mtu"])), + target: text.optional(), pingCount: integer.optional(), pingSize: integer.optional(), pingTtl: integer.optional(), + traceProto: z.enum(["icmp", "udp", "tcp"]).optional(), traceMaxHops: integer.optional(), traceHopTimeout: traceTime.optional(), traceProbeCount: integer.optional(), traceUseDns: z.boolean().optional(), + dnsType: z.string().trim().toUpperCase().pipe(z.enum(["A", "AAAA", "MX", "NS", "TXT", "CNAME", "PTR"])).optional(), + bwRemoteAddress: text.optional(), dstServerId: integer.optional(), bwProto: z.enum(["tcp", "udp"]).optional(), bwDuration: integer.optional(), +}).strict().superRefine((value, context) => { + if (value.tool === "bandwidth" ? !value.bwRemoteAddress : !value.target) context.addIssue({ code: "custom", path: [value.tool === "bandwidth" ? "bwRemoteAddress" : "target"], message: "Обязательная цель диагностики" }) + if (value.tool === "route" && value.target && !isIPv4(value.target)) context.addIssue({ code: "custom", path: ["target"], message: "Поиск маршрута требует IPv4" }) +}) +export type ServerProbeRequest = z.infer +export type ServerProbeTool = ServerProbeRequest["tool"] +export interface ServerProbePlan { request: ServerProbeRequest; nativeBody?: Record; wireTimeoutMs: number; wallBudgetMs: number; trace?: { requestedHops: number; effectiveHops: number; count: number; timeout: string }; mtuSizes?: number[] } +export function planServerProbeExecution(raw: unknown): ServerProbePlan { + const request = serverProbeExecutionRequest.parse(raw) + const clamp = (value: number | undefined, fallback: number, min: number, max: number) => Math.min(max, Math.max(min, value ?? fallback)) + if (request.tool === "ping") return { request, nativeBody: { address: request.target!, count: String(clamp(request.pingCount, 5, 1, 100)), size: String(clamp(request.pingSize, 64, 28, 8192)), ttl: String(clamp(request.pingTtl, 64, 1, 255)), interval: "0.2s" }, wireTimeoutMs: 120000, wallBudgetMs: 130000 } + if (request.tool === "traceroute") { + const raw = request.traceHopTimeout ?? "1s", unit = /^(\d+(?:\.\d+)?)s$|^(\d+)ms$/.exec(raw), clock = /^(\d{1,2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?$/.exec(raw) + const milliseconds = Math.min(3000, Math.max(10, unit ? Number(unit[1] ?? unit[2]) * (unit[1] !== undefined ? 1000 : 1) : clock ? ((Number(clock[1]) * 3600 + Number(clock[2]) * 60 + Number(clock[3])) * 1000 + Number((clock[4] ?? "").padEnd(3, "0"))) : 1000)) + const rounded = Math.round(milliseconds), timeout = `00:00:${String(Math.floor(rounded / 1000)).padStart(2, "0")}${rounded % 1000 ? "." + String(rounded % 1000).padStart(3, "0") : ""}` + const count = clamp(request.traceProbeCount, 1, 1, 3), requestedHops = clamp(request.traceMaxHops, 30, 1, 64), effectiveHops = Math.min(requestedHops, Math.max(1, Math.floor(53000 / (count * milliseconds)))) + return { request, nativeBody: { address: request.target!, protocol: request.traceProto ?? "icmp", "max-hops": String(effectiveHops), timeout, count: String(count), "use-dns": request.traceUseDns ? "true" : "false" }, wireTimeoutMs: 58000, wallBudgetMs: 68000, trace: { requestedHops, effectiveHops, count, timeout } } + } + if (request.tool === "bandwidth") return { request, nativeBody: { address: request.bwRemoteAddress!, protocol: request.bwProto ?? "tcp", direction: "both", duration: `${clamp(request.bwDuration, 10, 3, 120)}s` }, wireTimeoutMs: 30000, wallBudgetMs: 30000 } + if (request.tool === "mtu") return { request, wireTimeoutMs: 25000, wallBudgetMs: 260000, mtuSizes: [1500, 1492, 1480, 1476, 1472, 1468, 1400, 1280, 1024, 576] } + return { request, wireTimeoutMs: request.tool === "dns" ? 30000 : 10000, wallBudgetMs: request.tool === "dns" ? 30000 : 10000 } +} +export class ServerProbeDataError extends Error { constructor() { super("Устройство не подтвердило структурированные измерения диагностики") } } +const bad = (): never => { throw new ServerProbeDataError() } +export function serverProbeRows(raw: unknown, ceiling: number): Record[] { + if (!Array.isArray(raw) || raw.length >= ceiling || Buffer.byteLength(JSON.stringify(raw)) > 1048576 || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.getPrototypeOf(row) !== Object.prototype)) return bad() + return raw as Record[] +} +function scalar(value: unknown, maximum = 1024): string | undefined { + if (value === undefined || value === null || value === "") return undefined + if (typeof value === "string" && value.length <= maximum && !/[\x00-\x08\x0b-\x1f\x7f]/.test(value)) return value + if (typeof value === "number" && Number.isFinite(value)) return String(value) + return bad() +} +const metric = (value: unknown) => scalar(value, 128)?.trim() || "—" +const number = (value: unknown): number | undefined => { if (value === undefined) return undefined; const text = scalar(value, 32); if (!text || !/^\d+$/.test(text) || !Number.isSafeInteger(Number(text))) return bad(); return Number(text) } +export interface ServerProbeView { output: string; data: Record } +export function projectServerProbePing(raw: unknown, target: string): ServerProbeView { + const rows = serverProbeRows(raw, 202).map(row => ({ ...(row.seq !== undefined ? { seq: number(row.seq) } : {}), ...(row.ttl !== undefined ? { ttl: number(row.ttl) } : {}), ...(row.time !== undefined ? { time: scalar(row.time, 64)?.trim() || undefined } : {}), ...(row.status !== undefined ? { status: scalar(row.status, 128) ?? "" } : {}), ...(row.sent !== undefined ? { sent: number(row.sent), received: number(row.received), packetLoss: scalar(row["packet-loss"], 64)?.trim() || undefined } : {}) })) + const lines = ["PING " + target] + for (const row of rows) if (row.seq !== undefined || row.status !== undefined) lines.push(row.status === "timeout" ? ` seq=${row.seq ?? "?"} timeout` : ` seq=${row.seq ?? "?"} ttl=${row.ttl ?? "?"} time=${row.time ?? "—"}${row.status ? ` status=${row.status}` : ""}`) + const summary = [...rows].reverse().find(row => row.sent !== undefined) + if (summary) lines.push(` sent=${summary.sent ?? "?"} received=${summary.received ?? "?"} packet-loss=${summary.packetLoss ?? "—"}`) + if (!rows.length) lines.push(" API не вернул измерений ping.") + return { output: lines.join("\n"), data: { kind: "ping", rows } } +} +export function projectServerProbeTrace(raw: unknown, plan: ServerProbePlan): ServerProbeView { + const rows = serverProbeRows(raw, 258).map(row => ({ address: metric(row.address ?? row.host ?? row["from-address"]), loss: metric(row.loss ?? row["packet-loss"]), last: metric(row.last ?? row.time), avg: metric(row.avg ?? row["avg-rtt"]) })) + const trace = plan.trace!, note = trace.effectiveHops < trace.requestedHops ? `;; REST API RouterOS: сессия ~60 с (параметры traceroute не продлевают лимит). max-hops снижен с ${trace.requestedHops} до ${trace.effectiveHops} (count=${trace.count}, timeout=${trace.timeout}).\n` : "" + return { output: note + [" # ADDRESS LOSS LAST AVG", ...rows.map((row, index) => ` ${String(index + 1).padStart(2)} ${row.address.padEnd(40)} ${row.loss.padEnd(6)} ${row.last.padStart(8)} ${row.avg.padStart(8)}`)].join("\n"), data: { kind: "traceroute", rows, ...trace } } +} +export function projectServerProbeBandwidth(raw: unknown): ServerProbeView { + const rows = serverProbeRows(raw, 5002).map(row => ({ tx: metric(row["tx-current"] ?? row["tx-total-average"]), rx: metric(row["rx-current"] ?? row["rx-total-average"]), duration: scalar(row["test-duration"], 128) ?? "" })) + return { output: rows.length ? ["bandwidth-test:", ...rows.map(row => ` tx=${row.tx} rx=${row.rx}${row.duration ? ` t=${row.duration}` : ""}`)].join("\n") : "(no bandwidth-test output)", data: { kind: "bandwidth", rows } } +} +export function classifyServerProbeMtu(raw: unknown): "reply" | "timeout" { + const rows = serverProbeRows(raw, 8) + if (rows.some(row => /^\d+$/.test(String(row.received ?? "")) && Number(row.received) === 1) || rows.some(row => /^[01]$/.test(String(row.seq ?? "")) && !row.status && /^(?:\d+(?:\.\d+)?(?:us|ms|s)|\d{1,2}:\d{2}:\d{2}(?:\.\d+)?)$/.test(String(row.time ?? "")))) return "reply" + if (rows.some(row => row.status === "timeout")) return "timeout" + return bad() +} +/** Only already classified, ordered measurements may be rendered. Unknown + * native results must fail classification rather than inventing an MTU. */ +export function projectServerProbeMtu(measurements: readonly { size: number; result: "reply" | "timeout" }[], target: string): ServerProbeView { + const sizes = [1500, 1492, 1480, 1476, 1472, 1468, 1400, 1280, 1024, 576] + if (!measurements.length || measurements.length > sizes.length || measurements.some((row, index) => row.size !== sizes[index] || !["reply", "timeout"].includes(row.result) || row.result === "reply" && index !== measurements.length - 1) || measurements.at(-1)!.result !== "reply" && measurements.length !== sizes.length) return bad() + const found = measurements.at(-1)!.result === "reply" ? measurements.at(-1)!.size : null + return { output: [`MTU discovery (do-not-fragment ping) → ${target}`, "", ...measurements.map(row => row.result === "reply" ? ` ${String(row.size).padStart(4)} ✓ ok` : `${String(row.size).padStart(4)} timeout; причина не определена`), "", found !== null ? `MTU (DF): ~${found} bytes` : "MTU: could not determine"].join("\n"), data: { kind: "mtu", mtu: found, measurements: measurements.map(row => ({ ...row })) } } +} +function cidr(raw: unknown): { net: number; bits: number } | undefined { + if (typeof raw !== "string") return undefined + const parts = raw.trim().split("/").map(part => part.trim()), bits = parts.length === 1 ? 32 : /^(?:[0-9]|[12][0-9]|3[0-2])$/.test(parts[1] ?? "") ? Number(parts[1]) : -1 + if (parts.length > 2 || !isIPv4(parts[0]) || bits < 0) return undefined + const value = parts[0].split(".").reduce((n, octet) => ((n << 8) | Number(octet)) >>> 0, 0), mask = bits ? (0xffffffff << (32 - bits)) >>> 0 : 0 + return { net: value & mask, bits } +} +export function projectServerProbeRoute(raw: unknown, target: string): ServerProbeView { + const rows = serverProbeRows(raw, 10002), destination = cidr(target)!, unknown = rows.filter(row => !["true", "yes", "false", "no"].includes(String(row.active ?? "").toLowerCase()) || !cidr(row["dst-address"])).length + const matches = rows.filter(row => { const parsed = cidr(row["dst-address"]), mask = parsed?.bits ? (0xffffffff << (32 - parsed.bits)) >>> 0 : 0; return parsed && ["true", "yes"].includes(String(row.active).trim().toLowerCase()) && ["", "main"].includes(String(row["routing-table"] ?? "").trim().toLowerCase()) && String(row.disabled ?? "false").toLowerCase() !== "true" && String(row.inactive ?? "false").toLowerCase() !== "true" && (destination.net & mask) === parsed.net }) + const safeDistance = (value: unknown) => { try { return number(value) ?? Infinity } catch { return Infinity } } + matches.sort((a, b) => cidr(b["dst-address"])!.bits - cidr(a["dst-address"])!.bits || safeDistance(a.distance) - safeDistance(b.distance) || Number(!String(a.gateway ?? "").trim()) - Number(!String(b.gateway ?? "").trim())) + if (!matches.length) return { output: unknown ? `Маршрут не подтверждён: неизвестных записей ${unknown}.` : `no route for ${target} (route records checked: ${rows.length})`, data: { kind: "route", confirmed: false, checked: rows.length, unknown } } + const display = (value: unknown) => value === "" ? "" : scalar(value) ?? "—" + const row = matches[0], best = { destination: display(row["dst-address"]), gateway: display(row.gateway ?? row.interface), distance: safeDistance(row.distance) === Infinity ? null : safeDistance(row.distance), routingMark: display(row["routing-mark"]) } + return { output: [`lookup ${target} → ${unknown ? `best confirmed match (неизвестных записей: ${unknown}):` : "best match:"}`, ` dst-address: ${best.destination}`, ` gateway: ${best.gateway}`, ` distance: ${best.distance ?? "—"}`, ` routing-mark: ${best.routingMark}`].join("\n"), data: { kind: "route", confirmed: true, checked: rows.length, unknown, best } } +} +export function projectServerProbeDns(raw: unknown, name: string, type: string): ServerProbeView { + if (!["A", "AAAA", "MX", "NS", "TXT", "CNAME", "PTR"].includes(type)) return bad() + const records: Array = [] + if (!Array.isArray(raw) || raw.length >= 1002 || Buffer.byteLength(JSON.stringify(raw)) > 262144) return bad() + for (const item of raw) { + if (type === "MX") { const value = item as { priority?: unknown; exchange?: unknown }; const priority = number(value?.priority), exchange = scalar(value?.exchange, 253); if (priority === undefined || !exchange) return bad(); records.push({ priority, exchange }) } + else if (type === "TXT") { if (!Array.isArray(item) || item.length > 100 || item.some(chunk => typeof chunk !== "string")) return bad(); records.push(item.map(chunk => scalar(chunk, 8192) ?? "").join("")) } + else { if (typeof item !== "string") return bad(); const value = scalar(item, 1024); if (!value || type === "A" && isIP(value) !== 4 || type === "AAAA" && isIP(value) !== 6) return bad(); records.push(value) } + } + if (type === "MX") records.sort((a, b) => (a as { priority: number }).priority - (b as { priority: number }).priority) + const lines = type === "CNAME" ? [`${name}. IN CNAME ${records.join(",")}`] : records.map(record => typeof record === "object" ? `${name}. IN MX ${record.priority} ${record.exchange}` : type === "TXT" ? `${name}. IN TXT "${record}"` : `${name}${type === "PTR" && /^[\d.]+$/.test(name) ? ".in-addr.arpa" : ""}. IN ${type} ${record}`) + return { output: [`;; QUESTION: ${name} ${type}`, "", ...lines].join("\n"), data: { kind: "dns", resolver: "manager", type, records } } +} diff --git a/backend/src/services/server-probe-execution.test.ts b/backend/src/services/server-probe-execution.test.ts new file mode 100644 index 00000000..3f5fc701 --- /dev/null +++ b/backend/src/services/server-probe-execution.test.ts @@ -0,0 +1,263 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { createServerProbeExecutor, ServerProbeExecutionError, type ServerProbeAuthority, type ServerProbeEndpoint, type ServerProbeExecutionDependencies, type ServerProbeReceipt } from "./server-probe-execution.js" +import { planServerProbeExecution, projectServerProbeRoute } from "./server-probe-execution-model.js" +import { McpError } from "../modules/mcp/security.js" + +function fixture() { + let time = 1000000, sends = 0, dnsCalls = 0, beforeGet: ((id: number) => void) | undefined, beforeSend: (() => void) | undefined, afterSend: (() => void) | undefined + let providerFailure = false, sourceDnsFailure = false, repeatedFence = false, parallelFence = false, waitDns = false + let claimLost = false, intentLost = false, receiptLost = false, receivedLost = false + let raw: unknown = [{ seq: "0", ttl: "64", time: "1ms" }], metadataRef = 2 + const source: ServerProbeEndpoint = { id: 1, enabled: true, host: "192.0.2.1", port: 80, username: "NEVER_SOURCE_USER", password: "NEVER_SOURCE_PASSWORD", useSsl: false, verifySsl: true, routingEndpointEpoch: randomUUID() } + const destination: ServerProbeEndpoint = { ...source, id: 2, host: "192.0.2.2", username: "NEVER_DEST_USER", password: "NEVER_DEST_PASSWORD", routingEndpointEpoch: randomUUID() } + const authority: ServerProbeAuthority = { ownerId: "owner", connectionId: randomUUID(), credentialId: randomUUID(), expiresAt: 2000000, generation: "generation", policyProof: "a".repeat(64), ownerProof: "b".repeat(64), active: true, operationAllowed: true, scopeServerIds: [1, 2], ownerPermissions: ["mm:servers:write"], policyPermissions: ["mm:servers:write"] } + const events: Array<{ kind: string; value?: unknown }> = [], bindings = new Map(), claims = new Map(), receipts = new Map(), getIds: number[] = [] + let expectedSignal: AbortSignal | undefined + const deps: ServerProbeExecutionDependencies = { + async getServer(id) { getIds.push(id); beforeGet?.(id); return id === 1 ? structuredClone(source) : id === 2 ? structuredClone(destination) : undefined }, + async findDestination(_remote, explicit, allowedIds) { events.push({ kind: "metadata", value: [...allowedIds] }); return explicit !== undefined && !allowedIds.includes(explicit) ? undefined : { id: metadataRef } }, + async readAuthority() { return structuredClone(authority) }, + async assertProtections(ids) { assert(ids.every(id => authority.scopeServerIds.includes(id))); events.push({ kind: "protection", value: ids }) }, + async privateProof(domain, value) { return createHmac("sha256", "server-private-test-key").update(domain).update(JSON.stringify(value)).digest("hex") }, + async seal(value) { const id = randomUUID(); bindings.set(id, value); return id }, async open(value) { return bindings.get(value)! }, now: () => time, + async post(_server, path, body, timeout, signal, fence) { + expectedSignal = signal; beforeSend?.(); if (parallelFence) await Promise.all([fence(), fence()]); else await fence(); sends++; events.push({ kind: "native", value: { path, body: { ...body }, timeout } }) + if (repeatedFence) await fence() + if (providerFailure) throw Error("NEVER_PROVIDER_PASSWORD") + afterSend?.(); return structuredClone(raw) + }, + async routes(_server, timeout, signal, fence) { expectedSignal = signal; beforeSend?.(); await fence(); sends++; events.push({ kind: "route", value: timeout }); if (providerFailure) throw Error("NEVER_ROUTE_PROVIDER"); afterSend?.(); return structuredClone(raw) }, + async dns(name, type, signal, fence) { + expectedSignal = signal; await fence(); dnsCalls++; events.push({ kind: "dns", value: { name, type } }) + if (waitDns) return new Promise(() => {}) + if (sourceDnsFailure && name === "router.example") throw Error("NEVER_DNS_PROVIDER") + if (providerFailure) throw Error("NEVER_DNS_PROVIDER") + afterSend?.(); return structuredClone(raw) + }, + journal: { + async claim(value) { if (claims.has(value.receiptId)) return "exists"; claims.set(value.receiptId, value.bindingProof); events.push({ kind: "claim", value }); if (claimLost) throw Error("lost claim ACK"); return "claimed" }, + async intent(value) { assert(claims.has(value.receiptId)); events.push({ kind: "intent", value }); if (intentLost) throw Error("lost intent ACK") }, + async received(value) { events.push({ kind: "received", value }); if (receivedLost) throw Error("lost received ACK") }, + async finish(value) { receipts.set(value.receiptId, structuredClone(value)); events.push({ kind: "finish", value }); if (receiptLost) throw Error("lost receipt ACK") }, + async quarantine(value) { claims.set(value.receiptId, value.bindingProof); receipts.set(value.receiptId, { receiptId: value.receiptId, tool: value.tool as ServerProbeReceipt["tool"], serverId: 1, state: "unknown", httpStatus: 503, errorCode: value.errorCode, attemptedStages: value.attemptedStages, completedStages: value.completedStages, automaticReplay: false, forwardingVerified: false }); events.push({ kind: "quarantine", value }) }, + async read(id, bindingProof) { assert.equal(claims.get(id), bindingProof); return receipts.get(id) }, + }, + } + return { executor: createServerProbeExecutor(deps), deps, source, destination, authority, events, receipts, getIds, + raw(value: unknown) { raw = value }, beforeGet(hook: (id: number) => void) { beforeGet = hook }, beforeSend(hook: () => void) { beforeSend = hook }, afterSend(hook: () => void) { afterSend = hook }, + failure() { providerFailure = true }, sourceFailure() { sourceDnsFailure = true }, duplicateFence() { repeatedFence = true }, concurrentFence() { parallelFence = true }, pendingDns() { waitDns = true }, metadata(id: number) { metadataRef = id }, + lost(kind: "claim" | "intent" | "receipt" | "received") { if (kind === "claim") claimLost = true; if (kind === "intent") intentLost = true; if (kind === "receipt") receiptLost = true; if (kind === "received") receivedLost = true }, + advance(ms: number) { time += ms }, counts() { return { sends, dnsCalls } }, signal() { return expectedSignal }, + } +} +let cases = 0 +// A completed network read must survive bounded journal/authority overhead; +// its wire timeout and original private expiry must not be extended. +for (const [metadataMs, authorityMs, success] of [[11000, 1000000, true], [41000, 1000000, false], [13000, 12000, false]] as const) { + const f = fixture() + f.authority.expiresAt = 1000000 + authorityMs + f.raw([{ active: "true", "dst-address": "0.0.0.0/0", gateway: "192.0.2.254", distance: "1" }]) + const acknowledge = f.deps.journal!.received.bind(f.deps.journal!) + f.deps.journal!.received = async value => { await acknowledge(value); f.advance(metadataMs) } + const prepared = await f.executor.prepare(1, { tool: "route", target: "192.0.2.2" }) + if (success) assert.equal((await f.executor.execute(prepared)).state, "completed") + else await assert.rejects(f.executor.execute(prepared), value => value instanceof ServerProbeExecutionError && value.code === "CANCELLED") + assert.equal(f.counts().sends, 1) + assert.equal(f.events.find(event => event.kind === "route")?.value, 10000) + cases++ +} +const request = (tool: string, extra: Record = {}) => ({ tool, target: "192.0.2.2", bwRemoteAddress: "192.0.2.2", ...extra }) +async function failure(work: Promise, code?: string) { + await assert.rejects(work, value => value instanceof ServerProbeExecutionError && value.automaticReplay === false && !value.message.includes("NEVER_") && (code === undefined || value.code === code)); cases++ +} +for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) { + const f = fixture() + if (tool === "traceroute") f.raw([{ address: "192.0.2.2", loss: "0%", last: "1ms", avg: "1ms", "opaque-password": "NEVER_RAW" }]) + if (tool === "bandwidth") f.raw([{ "tx-current": "0", "rx-current": "", "opaque-password": "NEVER_RAW" }]) + if (tool === "dns") f.raw(["192.0.2.2"]) + if (tool === "route") f.raw([{ ".id": "*1", "dst-address": "192.0.2.0/24", active: "true", gateway: "ether1", distance: "1", "opaque-password": "NEVER_RAW" }]) + const prepared = await f.executor.prepare(1, request(tool)), result = await f.executor.execute(prepared) + assert.equal(result.state, "completed"); assert.equal(result.httpStatus, 200); assert.equal(result.forwardingVerified, false) + assert.ok(result.output); assert.ok(result.data); assert(!JSON.stringify(result).includes("NEVER_")); assert.equal(f.receipts.get(prepared.receiptId)?.state, "completed") + assert.equal(f.events.filter(event => event.kind === "intent").length, tool === "dns" ? f.counts().dnsCalls : f.counts().sends) + assert(f.signal() instanceof AbortSignal) + const sends = f.counts(); assert.deepEqual(await f.executor.execute(prepared), result); assert.deepEqual(f.counts(), sends) + if (tool === "bandwidth") { const native = f.events.find(event => event.kind === "native")!.value as { body: Record }; assert.equal(native.body.user, f.destination.username); assert.equal(native.body.password, f.destination.password); assert.equal(native.body.direction, "both") } + cases++ +} +assert.deepEqual(planServerProbeExecution(request("ping", { pingCount: 999, pingSize: 1, pingTtl: 999 })).nativeBody, { address: "192.0.2.2", count: "100", size: "28", ttl: "255", interval: "0.2s" }); cases++ +const trace = planServerProbeExecution(request("traceroute", { traceHopTimeout: "3s", traceProbeCount: 3, traceMaxHops: 64 })) +assert.equal(trace.nativeBody!["max-hops"], "5"); assert.equal(trace.nativeBody!.timeout, "00:00:03"); assert.equal(planServerProbeExecution(request("traceroute", { traceHopTimeout: "800ms" })).nativeBody!.timeout, "00:00:00.800"); cases++ +assert.equal(planServerProbeExecution(request("bandwidth", { bwDuration: 999 })).nativeBody!.duration, "120s"); assert.equal(planServerProbeExecution(request("bandwidth")).wireTimeoutMs, 30000); cases++ +for (const malformed of [{ tool: "unknown" }, request("ping", { pingCount: "4" }), request("route", { target: "example.com" }), request("traceroute", { traceHopTimeout: "00:99:00" }), request("dns", { dnsType: "SOA" }), request("ping", { password: "caller-secret" })]) { assert.throws(() => planServerProbeExecution(malformed)); cases++ } +for (const type of ["A", "AAAA", "MX", "NS", "TXT", "CNAME", "PTR"]) { + const f = fixture(), rows: Record = { A: ["192.0.2.2"], AAAA: ["2001:db8::1"], MX: [{ priority: 10, exchange: "mail.example" }], NS: ["ns.example"], TXT: [["part1", "part2"]], CNAME: ["canonical.example"], PTR: ["ptr.example"] } + f.raw(rows[type]); const result = await f.executor.execute(await f.executor.prepare(1, request("dns", { target: "example.com", dnsType: type }))) + assert.equal(result.data!.resolver, "manager"); assert.match(result.output!, new RegExp(type)); assert.equal(f.counts().sends, 0); cases++ +} +{ + const f = fixture(); f.raw([{ seq: "0", status: "timeout" }]); const result = await f.executor.execute(await f.executor.prepare(1, request("mtu"))) + assert.equal(f.counts().sends, 10); assert.match(result.output!, /could not determine/); assert.doesNotMatch(result.output!, /fragment needed|✓ ok/); cases++ +} +{ + const f = fixture(); f.raw([]); await failure(f.executor.execute(await f.executor.prepare(1, request("mtu"))), "MEASUREMENT_UNKNOWN"); assert.equal(f.counts().sends, 1) +} +{ + const f = fixture(); f.source.host = "router.example"; f.sourceFailure() + const prepared = await f.executor.prepare(1, request("ping")), result = await f.executor.execute(prepared) + assert.equal(result.state, "completed"); assert.equal(f.counts().dnsCalls, 1); assert.equal(f.counts().sends, 1) + assert.equal((f.events.find(event => event.kind === "native")!.value as { body: Record }).body["src-address"], undefined) + assert.equal(f.events.filter(event => event.kind === "received").length, 2); cases++ +} +{ + const f = fixture(); f.authority.scopeServerIds = [1]; f.metadata(99) + await failure(f.executor.prepare(1, request("bandwidth")), "PERMISSION_DENIED") + assert(!f.getIds.includes(99)); assert(!f.getIds.includes(2)); assert.deepEqual(f.events.find(event => event.kind === "metadata")?.value, [1]) +} +{ + const f = fixture(); f.beforeGet(() => { f.authority.active = false }) + await failure(f.executor.prepare(1, request("bandwidth")), "PERMISSION_DENIED") + assert.deepEqual(f.getIds, [1]); assert(!f.events.some(event => event.kind === "metadata")) +} +for (const mutate of [ + (f: ReturnType) => { f.authority.policyProof = "c".repeat(64) }, + (f: ReturnType) => { f.authority.ownerProof = "c".repeat(64) }, + (f: ReturnType) => { f.authority.credentialId = randomUUID() }, + (f: ReturnType) => { f.authority.generation = "changed" }, + (f: ReturnType) => { f.source.routingEndpointEpoch = randomUUID() }, + (f: ReturnType) => { f.destination.password = "rotated" }, + (f: ReturnType) => { f.destination.enabled = false }, +]) { const f = fixture(), prepared = await f.executor.prepare(1, request("bandwidth")); mutate(f); await failure(f.executor.execute(prepared)); assert.equal(f.counts().sends, 0); assert(!f.events.some(event => event.kind === "claim")) } +for (const kind of ["claim", "intent", "receipt", "received"] as const) { + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")); f.lost(kind) + await failure(f.executor.execute(prepared), "JOURNAL_UNKNOWN") + assert.equal(f.receipts.get(prepared.receiptId)?.state, "unknown"); assert.equal(f.counts().sends, ["claim", "intent"].includes(kind) ? 0 : 1) + const sends = f.counts(); await failure(f.executor.execute(prepared), "REPLAY_FORBIDDEN"); assert.deepEqual(f.counts(), sends) +} +{ + const f = fixture(); f.source.host = "router.example"; f.raw(["192.0.2.1"]) + const prepared = await f.executor.prepare(1, request("ping")); f.lost("received") + await failure(f.executor.execute(prepared), "JOURNAL_UNKNOWN"); assert.equal(f.counts().dnsCalls, 1); assert.equal(f.counts().sends, 0); assert.equal(f.events.filter(event => event.kind === "received").length, 1) +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")); f.duplicateFence() + await failure(f.executor.execute(prepared), "REPLAY_FORBIDDEN"); assert.equal(f.counts().sends, 1); assert.equal(f.receipts.get(prepared.receiptId)?.state, "unknown") +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")); f.concurrentFence() + await failure(f.executor.execute(prepared), "REPLAY_FORBIDDEN"); assert.equal(f.counts().sends, 0) +} +for (const mutate of [ + (value: ServerProbeReceipt) => { value.receiptId = randomUUID() }, + (value: ServerProbeReceipt) => { value.tool = "dns" }, + (value: ServerProbeReceipt) => { value.serverId = 99 }, + (value: ServerProbeReceipt) => { value.destinationServerId = 99 }, + (value: ServerProbeReceipt) => { value.completedStages = -1 }, + (value: ServerProbeReceipt) => { value.attemptedStages = Number.NaN }, + (value: ServerProbeReceipt) => { value.attemptedStages = 1.5 }, + (value: ServerProbeReceipt) => { value.completedStages = 2 }, + (value: ServerProbeReceipt) => { value.attemptedStages = value.completedStages = 3 }, + (value: ServerProbeReceipt) => { value.data = { kind: "foreign" } }, +]) { + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")); await f.executor.execute(prepared) + mutate(f.receipts.get(prepared.receiptId)!) + await failure(f.executor.execute(prepared), "RECEIPT_INVALID"); assert.equal(f.counts().sends, 1) +} +{ + const f = fixture(); f.raw([{ "tx-current": `native user=${f.destination.username} secret=${f.destination.password}`, "rx-current": "0" }]) + const prepared = await f.executor.prepare(1, request("bandwidth")), result = await f.executor.execute(prepared) + assert(!JSON.stringify(result).includes("NEVER_")); assert(!JSON.stringify(f.receipts.get(prepared.receiptId)).includes("NEVER_")); assert.equal(result.receiptId, prepared.receiptId); assert.equal(result.state, "completed"); cases++ +} +{ + const f = fixture(); f.source.username = "a"; const prepared = await f.executor.prepare(1, request("ping")), result = await f.executor.execute(prepared) + assert.equal(result.receiptId, prepared.receiptId); assert.equal(result.state, "completed"); assert.equal(result.data!.kind, "ping"); assert.equal(result.attemptedStages, 1) + assert.equal((await f.executor.execute(prepared)).receiptId, prepared.receiptId); cases++ +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")); f.beforeSend(() => { f.authority.active = false }) + await failure(f.executor.execute(prepared), "PERMISSION_DENIED"); assert.equal(f.counts().sends, 0) +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("mtu")); f.raw([{ status: "timeout" }]); f.afterSend(() => { f.source.password = "rotated" }) + await failure(f.executor.execute(prepared), "STATE_CONFLICT"); assert.equal(f.counts().sends, 1); assert.equal(f.receipts.get(prepared.receiptId)?.state, "unknown") +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")); f.beforeSend(() => f.advance(300001)) + await failure(f.executor.execute(prepared), "CANCELLED"); assert.equal(f.counts().sends, 0) +} +{ + const f = fixture(), controller = new AbortController(); f.pendingDns(); const prepared = await f.executor.prepare(1, request("dns")) + const pending = f.executor.execute(prepared, controller.signal) + while (!f.counts().dnsCalls) await new Promise(resolve => setTimeout(resolve, 1)) + controller.abort(); await failure(pending, "CANCELLED"); assert.equal(f.signal()?.aborted, true); assert.equal(f.receipts.get(prepared.receiptId)?.state, "unknown") +} +{ + const f = fixture(); f.failure(); const prepared = await f.executor.prepare(1, request("ping")) + await failure(f.executor.execute(prepared), "UPSTREAM_UNAVAILABLE"); const sends = f.counts(); await failure(f.executor.execute(prepared), "REPLAY_FORBIDDEN"); assert.deepEqual(f.counts(), sends) +} +{ + const view = projectServerProbeRoute([{ "dst-address": "0.0.0.0/0", active: "true", gateway: "default", distance: "1" }, { "dst-address": "192.0.2.0/24", active: "true", gateway: "scoped", distance: "10" }, { "dst-address": "192.0.2.2/32", active: "true", "routing-table": "foreign", gateway: "private" }, { "dst-address": "192.0.2.2/32", active: "unknown", gateway: "unconfirmed" }], "192.0.2.2") + assert.match(view.output, /gateway: scoped/); assert.equal(view.data.unknown, 1); cases++ +} +{ + const f = fixture() + Object.assign(f.authority, { transport: "rest", requestId: "real-http-request-id", issuedAt: 999000 }) + delete f.authority.connectionId; delete f.authority.credentialId + const prepared = await f.executor.prepare(1, request("ping")) + await f.executor.execute(prepared) + const claim = f.events.find(event => event.kind === "claim")!.value as Record + assert.equal(claim.transport, "rest"); assert.equal(claim.requestId, "real-http-request-id") + assert.equal(Object.hasOwn(claim, "connectionId"), false, "REST must not fabricate an MCP connection") + assert.equal(claim.issuedAt, 999000); cases++ +} +for (const change of [{ connectionId: "fabricated" }, { issuedAt: 1000001 }, { requestId: "" }, { credentialId: "fabricated" }]) { + const f = fixture() + Object.assign(f.authority, { transport: "rest", requestId: "real-http-request-id", issuedAt: 999000 }) + delete f.authority.connectionId; delete f.authority.credentialId + Object.assign(f.authority, change) + await failure(f.executor.prepare(1, request("ping")), "PERMISSION_DENIED") + assert.equal(f.getIds.length, 0, "Malformed REST identity refuses before privileged reads") +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("bandwidth")) + const before = await f.executor.preflight(prepared) + assert.deepEqual(before.serverIds, [1, 2]); assert.equal(before.receiptId, prepared.receiptId) + assert.equal(f.events.some(event => ["claim", "native", "dns", "finish"].includes(event.kind)), false) + assert.deepEqual(await f.executor.preflight(prepared), before); cases++ + f.destination.password = "changed" + await failure(f.executor.preflight(prepared), "STATE_CONFLICT") +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")), cancel = new AbortController() + cancel.abort(); await failure(f.executor.preflight(prepared, cancel.signal), "CANCELLED") + await failure(f.executor.execute({ ...prepared, expiresAt: new Date(2000001).toISOString() }), "INVALID_BINDING") + assert.deepEqual(f.counts(), { sends: 0, dnsCalls: 0 }) +} +for (const code of ["OWNER_CLAIMS_REVOKED", "PROTECTED_OBJECT", "GRANT_INACTIVE", "UNAUTHORIZED", "MCP_DISABLED", "CONNECTION_REVOKED"]) { + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")) + f.beforeSend(() => { throw new McpError(code, "NEVER_PRIVATE_AUTHORITY_DIAGNOSTIC", 403) }) + await failure(f.executor.execute(prepared), "PERMISSION_DENIED") + assert.deepEqual(f.counts(), { sends: 0, dnsCalls: 0 }); assert.equal(f.receipts.get(prepared.receiptId)?.errorCode, "PERMISSION_DENIED") +} +{ + const f = fixture(), prepared = await f.executor.prepare(1, request("ping")) + f.afterSend(() => { throw new McpError("OWNER_CLAIMS_REVOKED", "NEVER_PRIVATE_AUTHORITY_DIAGNOSTIC", 403) }) + await failure(f.executor.execute(prepared), "PERMISSION_DENIED") + assert.equal(f.counts().sends, 1); assert.equal(f.receipts.get(prepared.receiptId)?.state, "unknown") +} +{ + const f = fixture(), executor = createServerProbeExecutor({ ...f.deps, journal: undefined }), prepared = await executor.prepare(1, request("ping")) + await executor.preflight(prepared) + const reads = f.getIds.length + await failure(executor.execute(prepared), "JOURNAL_UNAVAILABLE") + assert.equal(f.getIds.length, reads); assert.deepEqual(f.counts(), { sends: 0, dnsCalls: 0 }) + assert.equal(f.events.some(event => event.kind === "claim"), false) +} +{ + const f = fixture(), executor = createServerProbeExecutor({ ...f.deps, findDestination: async (_remote, _explicit, allowed) => { assert.deepEqual(allowed, [1, 2]); return { id: 2 } } }), prepared = await executor.prepare(1, request("bandwidth", { dstServerId: 999 })) + assert.equal(prepared.destinationServerId, 2, "Scoped metadata resolver may return the documented host fallback") + await executor.execute(prepared) + assert.equal(f.receipts.get(prepared.receiptId)?.destinationServerId, 2); cases++ +} +console.log(`Server probe execution: ${cases} cases passed; all six tools, manager DNS7, private scoped BW credentials, source/authority fences, durable injected journal, loss/cancel/no replay; zero real I/O`) diff --git a/backend/src/services/server-probe-execution.ts b/backend/src/services/server-probe-execution.ts new file mode 100644 index 00000000..656a719f --- /dev/null +++ b/backend/src/services/server-probe-execution.ts @@ -0,0 +1,335 @@ +import { randomUUID } from "node:crypto" +import { isIPv4 } from "node:net" +import { z } from "zod" +import { hasPermission } from "../lib/permissions.js" +import { McpError } from "../modules/mcp/security.js" +import { sanitizeServerProbeReceipt } from "./server-probe-public-result.js" +import { classifyServerProbeMtu, planServerProbeExecution, projectServerProbeBandwidth, projectServerProbeDns, projectServerProbePing, projectServerProbeRoute, projectServerProbeTrace, projectServerProbeMtu, ServerProbeDataError, type ServerProbeRequest, type ServerProbeView } from "./server-probe-execution-model.js" + +export interface ServerProbeEndpoint { id: number; enabled: boolean; host: string; port: number; useSsl: boolean; verifySsl: boolean; username: string; password: string; routingEndpointEpoch: string } +export interface ServerProbeAuthority { + transport?: "mcp" | "rest"; ownerId: string; connectionId?: string; credentialId?: string; requestId?: string; issuedAt?: number + expiresAt: number; generation: string; policyProof: string; ownerProof: string + active: boolean; operationAllowed: boolean; scopeServerIds: number[]; ownerPermissions: string[]; policyPermissions: string[] +} +export interface ServerProbeReceipt { + receiptId: string; tool: ServerProbeRequest["tool"]; serverId: number; destinationServerId?: number + state: "completed" | "failed" | "cancelled" | "unknown"; httpStatus: number; output?: string; data?: Record; errorCode?: string + attemptedStages: number; completedStages: number; automaticReplay: false; forwardingVerified: false +} +/** All methods must acknowledge a committed durable transaction. claim is a + * unique CAS by receiptId+bindingProof, never an upsert/reset. Terminal rows + * never return to executable state. Lost ACK is quarantined, never retried. + * Production integration must bind this to the real operation's journal. */ +export interface ServerProbeJournalBinding { + receiptId: string; bindingProof: string; ownerId: string; transport?: "mcp" | "rest"; connectionId?: string; requestId?: string; issuedAt?: number; serverIds: number[]; tool: string +} +export interface ServerProbeJournal { + claim(value: ServerProbeJournalBinding): Promise<"claimed" | "exists"> + intent(value: { receiptId: string; stageIndex: number; kind: string; timeoutMs: number }): Promise + received(value: { receiptId: string; stageIndex: number; outcome: "received" | "source-unavailable" }): Promise + finish(value: ServerProbeReceipt): Promise + quarantine(value: ServerProbeJournalBinding & { errorCode: string; attemptedStages: number; completedStages: number }): Promise + read(receiptId: string, bindingProof: string): Promise +} +export interface ServerProbeExecutionDependencies { + getServer(id: number): Promise + /** Metadata-only lookup restricted BEFORE querying to this trusted scope. + * Never read or return destination credentials here. */ + findDestination(remote: string, explicitId: number | undefined, allowedServerIds: readonly number[]): Promise<{ id: number } | undefined> + readAuthority(): Promise + /** Fresh deny/human-approval policy for BOTH source and bandwidth destination. */ + assertProtections(serverIds: number[], authority: ServerProbeAuthority): Promise + privateProof(domain: string, value: unknown): Promise + seal(value: string): Promise; open(value: string): Promise + post(server: ServerProbeEndpoint, path: string, body: Record, timeoutMs: number, signal: AbortSignal, beforeDispatch: () => Promise): Promise + /** Admission immediately before HTTP request construction, not after the + * subsequent DNS/TLS/socket queue. Authority is checked again after response. */ + routes(server: ServerProbeEndpoint, timeoutMs: number, signal: AbortSignal, beforeDispatch: () => Promise): Promise + dns(name: string, type: string, signal: AbortSignal, beforeDispatch: () => Promise): Promise + /** Preparation/preflight do not own an operation yet. Execution requires a + * real journal and refuses before any privileged read if it is absent. */ + journal?: ServerProbeJournal + now?(): number +} +export class ServerProbeExecutionError extends Error { + readonly automaticReplay = false + constructor(readonly code: string, message: string, readonly statusCode: number, readonly state: ServerProbeReceipt["state"] = "failed", readonly attemptedStages = 0, readonly completedStages = 0) { super(message) } +} +const error = (code: string, message: string, status = 409): never => { throw new ServerProbeExecutionError(code, message, status) } +const proof = z.string().regex(/^[a-f0-9]{64}$/) +const bindingSchema = z.object({ version: z.literal(1), receiptId: z.string().uuid(), serverId: z.number().int().positive().safe(), destinationId: z.number().int().positive().safe().optional(), sourceProof: proof, destinationProof: proof.optional(), authorityProof: proof, request: z.unknown(), preparedAt: z.number().int().nonnegative().safe(), expiresAt: z.number().int().positive().safe() }).strict() +const completedReceiptSchema = z.object({ receiptId: z.string().uuid(), tool: z.enum(["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]), serverId: z.number().int().positive().safe(), destinationServerId: z.number().int().positive().safe().optional(), state: z.literal("completed"), httpStatus: z.literal(200), output: z.string().max(131072), data: z.record(z.string(), z.unknown()), attemptedStages: z.number().int().min(1).max(11), completedStages: z.number().int().min(1).max(11), automaticReplay: z.literal(false), forwardingVerified: z.literal(false) }).strict() +type Binding = z.infer +/** Server-private plan material only; never put bindingCiphertext into a public + * tool response. The existing encrypted-plan layer may persist it unchanged. */ +export interface PreparedServerProbe { receiptId: string; tool: ServerProbeRequest["tool"]; serverId: number; destinationServerId?: number; expiresAt: string; bindingCiphertext: string } +function endpointValue(server: ServerProbeEndpoint) { + if (!Number.isSafeInteger(server.id) || server.id <= 0 || !server.enabled || !z.string().uuid().safeParse(server.routingEndpointEpoch).success || !server.host || !Number.isSafeInteger(server.port) || server.port <= 0 || server.port > 65535) return error("SOURCE_UNAVAILABLE", "Источник диагностики выключен или не подтверждён") + return { id: server.id, enabled: server.enabled, host: server.host, port: server.port, useSsl: server.useSsl, verifySsl: server.verifySsl, username: server.username, password: server.password, epoch: server.routingEndpointEpoch } +} +function cleanReceipt(receipt: ServerProbeReceipt, endpoints: ServerProbeEndpoint[]): ServerProbeReceipt { + const secrets = [...new Set(endpoints.flatMap(server => [server.password, server.username]).filter(Boolean))] + const clean = (value: unknown, field?: string): unknown => { + if (typeof value === "string") { + if (field === "kind" && ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"].includes(value) || field === "resolver" && value === "manager" || field === "type" && ["A", "AAAA", "MX", "NS", "TXT", "CNAME", "PTR"].includes(value) || field === "result" && ["reply", "timeout"].includes(value)) return value + let result = value; for (const secret of secrets) result = result.split(secret).join("[redacted]"); return result.replace(/\bBearer\s+\S+/gi, "Bearer [redacted]").replace(/\bmmcp_[a-z]+_[A-Za-z0-9_-]+/g, "[redacted]") + } + if (Array.isArray(value)) return value.map(child => clean(child)) + if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, clean(child, key)])) + return value + } + // Public IDs/enums/counters are trusted structural metadata. Redact only + // provider-derived measurements/text, so a short username cannot corrupt a + // UUID or turn the fixed state enum into an invalid receipt. + const result: ServerProbeReceipt = { ...receipt, ...(receipt.output !== undefined ? { output: clean(receipt.output) as string } : {}), ...(receipt.data !== undefined ? { data: clean(receipt.data) as Record } : {}) } + if (Buffer.byteLength(JSON.stringify(result)) > 131072) throw new ServerProbeDataError() + try { return sanitizeServerProbeReceipt(result) } catch { throw new ServerProbeDataError() } +} +function validateAuthority(authority: ServerProbeAuthority, serverIds: number[], now: number): void { + const identityValid = authority.transport === "rest" + ? typeof authority.requestId === "string" && authority.requestId.length > 0 && authority.requestId.length <= 256 && authority.connectionId === undefined && authority.credentialId === undefined && Number.isSafeInteger(authority.issuedAt) && authority.issuedAt! > 0 && authority.issuedAt! <= now + : (authority.transport === undefined || authority.transport === "mcp") && !!authority.connectionId && !!authority.credentialId && authority.requestId === undefined + if (!identityValid || !authority.active || !authority.operationAllowed || !authority.ownerId || !authority.generation || !Number.isSafeInteger(authority.expiresAt) || authority.expiresAt <= now || !proof.safeParse(authority.policyProof).success || !proof.safeParse(authority.ownerProof).success || !hasPermission(authority.ownerPermissions, "mm:servers:write") || !hasPermission(authority.policyPermissions, "mm:servers:write") || serverIds.some(id => !authority.scopeServerIds.includes(id))) error("PERMISSION_DENIED", "Нет актуального допуска диагностики обоих участников", 403) +} +async function untilAbort(work: Promise, signal: AbortSignal): Promise { + if (signal.aborted) throw new ServerProbeExecutionError("CANCELLED", "Диагностика отменена или истёк лимит времени", 502) + let abort: (() => void) | undefined + try { return await Promise.race([work, new Promise((_resolve, reject) => { abort = () => reject(new ServerProbeExecutionError("CANCELLED", "Диагностика отменена или истёк лимит времени", 502)); signal.addEventListener("abort", abort, { once: true }) })]) } + finally { if (abort) signal.removeEventListener("abort", abort) } +} + +/** Inert factory. Injected tests need no DB, DNS, RouterOS or collector. No + * caller JSON can supply clients, journal handles, guard callbacks or secrets. */ +export function createServerProbeExecutor(deps: ServerProbeExecutionDependencies) { + const now = deps.now ?? Date.now + const privateEndpoint = (server: ServerProbeEndpoint) => deps.privateProof("endpoint-v1", endpointValue(server)) + const prepare = async (serverId: number, raw: unknown, signal?: AbortSignal): Promise => { + const plan = planServerProbeExecution(raw) + if (!Number.isSafeInteger(serverId) || serverId <= 0) return error("INVALID_INPUT", "Некорректный сервер диагностики", 400) + const abort = () => { if (signal?.aborted) error("CANCELLED", "Подготовка диагностики отменена", 502) } + abort() + const authority = await deps.readAuthority(); validateAuthority(authority, [serverId], now()); await deps.assertProtections([serverId], authority); abort() + const authorityProof = await deps.privateProof("authority-v1", authority) + const authorityFence = async (ids: number[]) => { abort(); const fresh = await deps.readAuthority(); validateAuthority(fresh, ids, now()); if (await deps.privateProof("authority-v1", fresh) !== authorityProof) return error("STATE_CONFLICT", "Допуск подготовки диагностики изменился"); await deps.assertProtections(ids, fresh); abort() } + const source = await deps.getServer(serverId) + if (!source || source.id !== serverId) return error("NOT_FOUND", "Сервер диагностики не найден", 404) + await authorityFence([serverId]) + const sourceProof = await privateEndpoint(source) + await authorityFence([serverId]) + const destinationRef = plan.request.tool === "bandwidth" ? await deps.findDestination(plan.request.bwRemoteAddress!, plan.request.dstServerId, [...authority.scopeServerIds]) : undefined + if (plan.request.tool === "bandwidth" && !destinationRef) return error("DESTINATION_UNAVAILABLE", "Сервер назначения BW-теста не найден", 400) + const serverIds = [...new Set([serverId, ...(destinationRef ? [destinationRef.id] : [])])] + await authorityFence(serverIds) + const destination = destinationRef ? await deps.getServer(destinationRef.id) : undefined + await authorityFence(serverIds) + if (destinationRef && (!destination || destination.id !== destinationRef.id)) return error("DESTINATION_UNAVAILABLE", "Назначение BW-теста исчезло", 409) + const destinationProof = destination ? await privateEndpoint(destination) : undefined + const freshSource = await deps.getServer(serverId) + await authorityFence(serverIds) + if (!freshSource || await privateEndpoint(freshSource) !== sourceProof) return error("STATE_CONFLICT", "Источник или допуск диагностики изменились") + if (destination) { const freshDestination = await deps.getServer(destination.id); await authorityFence(serverIds); if (!freshDestination || await privateEndpoint(freshDestination) !== destinationProof) return error("STATE_CONFLICT", "Назначение BW-теста изменилось") } + await authorityFence(serverIds) + const preparedAt = now(), expiresAt = Math.min(authority.expiresAt, preparedAt + 300000), receiptId = randomUUID() + const binding: Binding = { version: 1, receiptId, serverId, ...(destination ? { destinationId: destination.id, destinationProof } : {}), sourceProof, authorityProof, request: plan.request, preparedAt, expiresAt } + const bindingCiphertext = await deps.seal(JSON.stringify(binding)) + await authorityFence(serverIds) + const publicationSource = await deps.getServer(serverId); await authorityFence(serverIds) + if (!publicationSource || await privateEndpoint(publicationSource) !== sourceProof) return error("STATE_CONFLICT", "Источник изменился перед публикацией плана") + if (destination) { const publicationDestination = await deps.getServer(destination.id); await authorityFence(serverIds); if (!publicationDestination || await privateEndpoint(publicationDestination) !== destinationProof) return error("STATE_CONFLICT", "Назначение изменилось перед публикацией плана") } + await authorityFence(serverIds) + return { receiptId, tool: plan.request.tool, serverId, ...(destination ? { destinationServerId: destination.id } : {}), expiresAt: new Date(expiresAt).toISOString(), bindingCiphertext } + } + const load = async (prepared: PreparedServerProbe) => { + let binding: Binding + try { if (prepared.bindingCiphertext.length > 262144) throw Error(); binding = bindingSchema.parse(JSON.parse(await deps.open(prepared.bindingCiphertext))) } catch { return error("INVALID_BINDING", "Частный план диагностики не подтверждён", 409) } + const plan = planServerProbeExecution(binding.request) + if (prepared.receiptId !== binding.receiptId || prepared.serverId !== binding.serverId || prepared.destinationServerId !== binding.destinationId || prepared.tool !== plan.request.tool || prepared.expiresAt !== new Date(binding.expiresAt).toISOString()) return error("INVALID_BINDING", "Привязка диагностики изменена") + return { binding, plan } + } + /** Source/authority preconditions only: no probe, DNS, claim or journal write. + * Used by the real runner's current() and private preparation evidence. */ + const preflight = async (prepared: PreparedServerProbe, signal?: AbortSignal) => { + const { binding, plan } = await load(prepared), serverIds = [...new Set([binding.serverId, ...(binding.destinationId ? [binding.destinationId] : [])])] + const access = async () => { + if (signal?.aborted || now() >= binding.expiresAt || now() < binding.preparedAt) return error("CANCELLED", "Частный план диагностики истёк или отменён", 502) + const authority = await deps.readAuthority(); validateAuthority(authority, serverIds, now()) + if (await deps.privateProof("authority-v1", authority) !== binding.authorityProof) return error("STATE_CONFLICT", "Допуск диагностики изменился") + await deps.assertProtections(serverIds, authority) + if (signal?.aborted || now() >= binding.expiresAt) return error("CANCELLED", "Частный план диагностики истёк или отменён", 502) + } + await access() + const source = await deps.getServer(binding.serverId); await access() + if (!source || source.id !== binding.serverId || await privateEndpoint(source) !== binding.sourceProof) return error("STATE_CONFLICT", "Источник диагностики изменился") + if (binding.destinationId) { + const destination = await deps.getServer(binding.destinationId); await access() + if (!destination || destination.id !== binding.destinationId || await privateEndpoint(destination) !== binding.destinationProof) return error("STATE_CONFLICT", "Назначение диагностики изменилось") + } + await access() + return { receiptId: binding.receiptId, bindingProof: await deps.privateProof("binding-v1", binding), tool: plan.request.tool, serverIds, expiresAt: new Date(binding.expiresAt).toISOString() } + } + const execute = async (prepared: PreparedServerProbe, signal?: AbortSignal): Promise => { + const journal = deps.journal + if (!journal) return error("JOURNAL_UNAVAILABLE", "Для запуска диагностики нужен постоянный журнал реальной операции", 503) + const { binding, plan } = await load(prepared) + // Network stage limits remain unchanged. Current grants, source fences and + // durable claim/ACK/final receipt need a bounded control-plane allowance. + // The original private plan/authority expiry always wins. + const serverIds = [...new Set([binding.serverId, ...(binding.destinationId ? [binding.destinationId] : [])])], controller = new AbortController(), deadline = Math.min(binding.expiresAt, now() + Math.min(300000, plan.wallBudgetMs + 30000)) + const abort = () => controller.abort(); signal?.addEventListener("abort", abort, { once: true }); if (signal?.aborted) abort() + const timer = setTimeout(abort, Math.max(1, deadline - now())) + let attempted = 0, completed = 0, claimed = false, currentSource: ServerProbeEndpoint | undefined, currentDestination: ServerProbeEndpoint | undefined + let journalBinding: ServerProbeJournalBinding | undefined + const check = async (): Promise => { + if (controller.signal.aborted || now() >= deadline || now() < binding.preparedAt) error("CANCELLED", "Диагностика отменена или истёк лимит времени", 502) + const authorityFence = async () => { const authority = await deps.readAuthority(); validateAuthority(authority, serverIds, now()); if (await deps.privateProof("authority-v1", authority) !== binding.authorityProof) error("STATE_CONFLICT", "Owner, policy или credential диагностики изменились"); await deps.assertProtections(serverIds, authority); return authority } + await authorityFence() + const source = await deps.getServer(binding.serverId) + await authorityFence() + if (!source || source.id !== binding.serverId || await privateEndpoint(source) !== binding.sourceProof) return error("STATE_CONFLICT", "Источник диагностики изменился") + currentSource = source + if (binding.destinationId) { const destination = await deps.getServer(binding.destinationId); await authorityFence(); if (!destination || destination.id !== binding.destinationId || await privateEndpoint(destination) !== binding.destinationProof) return error("STATE_CONFLICT", "Назначение или credentials BW-теста изменились"); currentDestination = destination } + const authority = await authorityFence() + if (controller.signal.aborted || now() >= deadline) error("CANCELLED", "Диагностика отменена или истёк лимит времени", 502) + return authority + } + const stage = async (kind: string, timeoutMs: number, work: (beforeDispatch: () => Promise, timeout: number, signal: AbortSignal) => Promise, sourceDns = false): Promise => { + await check() + const index = attempted + 1, timeout = Math.min(timeoutMs, deadline - now()) + if (timeout <= 0) return error("CANCELLED", "Лимит диагностики исчерпан", 502) + try { await journal.intent({ receiptId: binding.receiptId, stageIndex: index, kind, timeoutMs: timeout }) } catch { return error("JOURNAL_UNKNOWN", "Подтверждение intent недоступно; диагностика не повторяется", 503) } + let dispatched = false, dispatchClaimed = false + const stageAbort = new AbortController(), cancelStage = () => stageAbort.abort(), stageTimer = setTimeout(cancelStage, timeout) + controller.signal.addEventListener("abort", cancelStage, { once: true }); if (controller.signal.aborted) cancelStage() + const beforeDispatch = async () => { if (dispatchClaimed) return error("REPLAY_FORBIDDEN", "Повтор native/DNS stage запрещён"); dispatchClaimed = true; if (stageAbort.signal.aborted) return error("CANCELLED", "Лимит stage исчерпан", 502); await check(); if (stageAbort.signal.aborted) return error("CANCELLED", "Лимит stage исчерпан", 502); dispatched = true; attempted++ } + try { + let result: unknown + try { result = await untilAbort(work(beforeDispatch, timeout, stageAbort.signal), stageAbort.signal) } + catch (failure) { + if (sourceDns && dispatched && !(failure instanceof ServerProbeExecutionError)) { + await check(); try { await journal.received({ receiptId: binding.receiptId, stageIndex: index, outcome: "source-unavailable" }) } catch { return error("JOURNAL_UNKNOWN", "ACK stage недоступен; повтор запрещён", 503) }; completed++; return [] + } + throw failure + } + if (!dispatched) return error("INVALID_ADAPTER", "Адаптер не подтвердил границу отправки", 503) + await check(); try { await journal.received({ receiptId: binding.receiptId, stageIndex: index, outcome: "received" }) } catch { return error("JOURNAL_UNKNOWN", "ACK stage недоступен; повтор запрещён", 503) }; completed++ + return result + } finally { stageAbort.abort(); clearTimeout(stageTimer); controller.signal.removeEventListener("abort", cancelStage) } + } + try { + const authority = await check(), bindingProof = await deps.privateProof("binding-v1", binding) + journalBinding = { receiptId: binding.receiptId, bindingProof, ownerId: authority.ownerId, ...(authority.transport === "rest" ? { transport: "rest", requestId: authority.requestId, issuedAt: authority.issuedAt } : { transport: "mcp", connectionId: authority.connectionId }), serverIds, tool: plan.request.tool } + let claim: "claimed" | "exists" + try { claim = await journal.claim(journalBinding) } catch { return error("JOURNAL_UNKNOWN", "Исход durable claim неизвестен; диагностика не повторяется", 503) } + if (claim === "exists") { + let prior: ServerProbeReceipt | undefined + try { prior = await journal.read(binding.receiptId, bindingProof) } catch { return error("JOURNAL_UNKNOWN", "Ранее использованная попытка недоступна; повтор запрещён", 503) }; await check() + if (!prior || prior.state !== "completed") throw new ServerProbeExecutionError("REPLAY_FORBIDDEN", "Попытка уже использована; автоматический повтор запрещён", 409, "unknown", prior?.attemptedStages ?? 0, prior?.completedStages ?? 0) + const parsed = completedReceiptSchema.safeParse(prior) + const maximum = plan.request.tool === "mtu" ? 11 : ["ping", "traceroute"].includes(plan.request.tool) ? 2 : 1 + if (!parsed.success || parsed.data.receiptId !== binding.receiptId || parsed.data.tool !== plan.request.tool || parsed.data.serverId !== binding.serverId || parsed.data.destinationServerId !== binding.destinationId || parsed.data.data.kind !== plan.request.tool || parsed.data.completedStages !== parsed.data.attemptedStages || parsed.data.attemptedStages > maximum) return error("RECEIPT_INVALID", "Сохранённая квитанция не соответствует частному плану; повтор запрещён", 409) + return cleanReceipt(parsed.data, [currentSource!, ...(currentDestination ? [currentDestination] : [])]) + } + claimed = true + let sourceIpv4: string | null = null + if (["ping", "traceroute", "mtu"].includes(plan.request.tool)) { + const host = currentSource!.host.trim() + if (isIPv4(host)) sourceIpv4 = host + else { + const addresses = await stage("source-dns-A", 10000, (fence, _timeout, signal) => deps.dns(host, "A", signal, fence), true) + if (!Array.isArray(addresses) || addresses.length > 1000 || addresses.some(value => typeof value !== "string" || !isIPv4(value))) throw new ServerProbeDataError() + sourceIpv4 = addresses[0] as string | undefined ?? null + } + } + const nativeBody = { ...plan.nativeBody, ...(sourceIpv4 ? { "src-address": sourceIpv4 } : {}) } + let view: ServerProbeView + if (plan.request.tool === "ping") view = projectServerProbePing(await stage("native-ping", plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/ping", nativeBody, timeout, signal, fence)), plan.request.target!) + else if (plan.request.tool === "traceroute") view = projectServerProbeTrace(await stage("native-traceroute", plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/traceroute", nativeBody, timeout, signal, fence)), plan) + else if (plan.request.tool === "bandwidth") view = projectServerProbeBandwidth(await stage("native-bandwidth", plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/bandwidth-test", { ...nativeBody, user: currentDestination!.username, password: currentDestination!.password }, timeout, signal, fence))) + else if (plan.request.tool === "route") view = projectServerProbeRoute(await stage("native-route-read", plan.wireTimeoutMs, (fence, timeout, signal) => deps.routes(currentSource!, timeout, signal, fence)), plan.request.target!) + else if (plan.request.tool === "dns") view = projectServerProbeDns(await stage("manager-dns-" + (plan.request.dnsType ?? "A"), plan.wireTimeoutMs, (fence, _timeout, signal) => deps.dns(plan.request.target!, plan.request.dnsType ?? "A", signal, fence)), plan.request.target!, plan.request.dnsType ?? "A") + else { + const measurements: Array<{ size: number; result: "reply" | "timeout" }> = [] + for (const size of plan.mtuSizes!) { + const result = classifyServerProbeMtu(await stage("native-mtu-" + size, plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/ping", { address: plan.request.target!, count: "1", size: String(size), interval: "0.2s", "do-not-fragment": "true", ...(sourceIpv4 ? { "src-address": sourceIpv4 } : {}) }, timeout, signal, fence))) + measurements.push({ size, result }) + if (result === "reply") break + } + view = projectServerProbeMtu(measurements, plan.request.target!) + } + await check() + const receipt = cleanReceipt({ receiptId: binding.receiptId, tool: plan.request.tool, serverId: binding.serverId, ...(binding.destinationId ? { destinationServerId: binding.destinationId } : {}), state: "completed", httpStatus: 200, ...view, attemptedStages: attempted, completedStages: completed, automaticReplay: false, forwardingVerified: false }, [currentSource!, ...(currentDestination ? [currentDestination] : [])]) + try { await journal.finish(receipt) } catch { return error("JOURNAL_UNKNOWN", "Подтверждение receipt недоступно; повтор запрещён", 503) }; await check() + return receipt + } catch (failure) { + const accessCode = failure instanceof McpError + ? ["READ_CANCELLED", "PLAN_EXPIRED"].includes(failure.code) ? "CANCELLED" + : ["STATE_CONFLICT", "RESET_REQUIRED"].includes(failure.code) ? "STATE_CONFLICT" + : ["PERMISSION_DENIED", "UNAUTHORIZED", "MCP_DISABLED", "CONNECTION_REVOKED", "OWNER_CLAIMS_REVOKED", "GRANT_INACTIVE", "PROTECTED_OBJECT", "APPROVAL_REQUIRED", "SERVER_DENIED", "CREDENTIAL_EXPIRED"].includes(failure.code) ? "PERMISSION_DENIED" : undefined + : undefined + const cause = failure instanceof ServerProbeExecutionError ? failure : accessCode + ? new ServerProbeExecutionError(accessCode, "Допуск диагностики изменился или операция отменена", accessCode === "PERMISSION_DENIED" ? 403 : accessCode === "STATE_CONFLICT" ? 409 : 502) + : new ServerProbeExecutionError(failure instanceof ServerProbeDataError ? "MEASUREMENT_UNKNOWN" : "UPSTREAM_UNAVAILABLE", failure instanceof ServerProbeDataError ? failure.message : "Диагностика недоступна; исход отправленной пробы не подтверждён", 502) + attempted = Math.max(attempted, cause.attemptedStages); completed = Math.max(completed, cause.completedStages) + const state = attempted || cause.state === "unknown" || cause.code === "JOURNAL_UNKNOWN" ? "unknown" : cause.code === "CANCELLED" ? "cancelled" : "failed" + if (claimed || cause.code === "JOURNAL_UNKNOWN" && journalBinding) { + try { + if (state === "unknown") await journal.quarantine({ ...journalBinding!, errorCode: cause.code, attemptedStages: attempted, completedStages: completed }) + else await journal.finish({ receiptId: binding.receiptId, tool: plan.request.tool, serverId: binding.serverId, ...(binding.destinationId ? { destinationServerId: binding.destinationId } : {}), state, httpStatus: cause.statusCode, errorCode: cause.code, attemptedStages: attempted, completedStages: completed, automaticReplay: false, forwardingVerified: false }) + } catch { throw new ServerProbeExecutionError("JOURNAL_UNKNOWN", "Durable исход диагностики недоступен; повтор запрещён", 503, "unknown", attempted, completed) } + } + throw new ServerProbeExecutionError(cause.code, cause.message, cause.statusCode, state, attempted, completed) + } finally { clearTimeout(timer); signal?.removeEventListener("abort", abort) } + } + return { prepare, preflight, execute } +} + +/** Concrete production I/O mapping, deliberately unregistered. Callers must + * supply the real durable operation journal and fresh authority/protections; + * no in-memory fallback is available. Importing this module opens nothing. */ +export type ServerProbeSourceReaders = Pick + +export function createProductionServerProbeExecutor(input: Pick, readers?: ServerProbeSourceReaders) { + // Trusted source composition only. Supplying incomplete readers must never + // fall back to a second pool checkout while the host holds storage locks. + if (readers !== undefined && (!readers || typeof readers !== "object" || typeof readers.getServer !== "function" || typeof readers.findDestination !== "function")) return error("STATE_UNAVAILABLE", "Источник диагностики не подключён", 503) + const source = readers === undefined ? undefined : { getServer: readers.getServer, findDestination: readers.findDestination } + return createServerProbeExecutor({ ...input, + getServer: source ? source.getServer : async id => (await import("../modules/servers/repository/servers-repository.js")).getServerRowById(id), + findDestination: source ? source.findDestination : async (remote, explicit, allowedServerIds) => { + const { pool } = await import("../db/index.js") + const { resolveServerProbeRestBandwidthPeer, ServerProbeRestContractError } = await import("./server-probe-rest-contract.js") + try { + const selected = await resolveServerProbeRestBandwidthPeer(remote, explicit, allowedServerIds, { + byId: async (id, ids) => ids.includes(id) ? (await pool.query<{ id: number; host: string; enabled: boolean }>("SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND id=$2 LIMIT 1", [[...ids], id])).rows[0] : undefined, + enabledByHost: async (host, ids) => (await pool.query<{ id: number; host: string; enabled: boolean }>("SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND enabled=true AND lower(btrim(host))=lower(btrim($2)) LIMIT 1001", [[...ids], host])).rows, + }) + return { id: selected.id } + } catch (failure) { + if (failure instanceof ServerProbeRestContractError) throw new ServerProbeExecutionError(failure.code, failure.message, failure.statusCode) + throw failure + } + }, + privateProof: async (domain, value) => { const security = await import("../modules/mcp/security.js"); return security.privateSecretFingerprint(security.fingerprint(value), "server-probe-execution-" + domain) }, + seal: async value => (await import("../modules/mcp/security.js")).encryptSecret(value), open: async value => (await import("../modules/mcp/security.js")).decryptSecret(value), + post: async (server, path, body, timeout, signal, fence) => { const { MikrotikClient } = await import("./mikrotik.js"); return MikrotikClient.fromServer(server as Parameters[0]).post(path, body, timeout, signal, fence) }, + routes: async (server, timeout, signal, fence) => { const { MikrotikClient } = await import("./mikrotik.js"); return MikrotikClient.fromServer(server as Parameters[0]).get("/ip/route", timeout, signal, 1048576, fence) }, + dns: async (name, type, signal, fence) => { + const { Resolver } = await import("node:dns/promises"), resolver = new Resolver(), abort = () => resolver.cancel() + signal.addEventListener("abort", abort, { once: true }) + try { + await fence(); signal.throwIfAborted() + switch (type) { + case "A": return await resolver.resolve4(name) + case "AAAA": return await resolver.resolve6(name) + case "MX": return await resolver.resolveMx(name) + case "NS": return await resolver.resolveNs(name) + case "TXT": return await resolver.resolveTxt(name) + case "CNAME": return await resolver.resolveCname(name) + case "PTR": return /^[\d.]+$/.test(name) ? await resolver.reverse(name) : await resolver.resolvePtr(name.includes(".arpa") ? name : `${name}.in-addr.arpa`) + default: return error("INVALID_INPUT", "Неизвестный тип DNS", 400) + } + } finally { signal.removeEventListener("abort", abort) } + }, + }) +} diff --git a/backend/src/services/server-probe-journal-history.test.ts b/backend/src/services/server-probe-journal-history.test.ts new file mode 100644 index 00000000..f181caaa --- /dev/null +++ b/backend/src/services/server-probe-journal-history.test.ts @@ -0,0 +1,53 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { createServerProbeJournalHistoryReader, type ServerProbeHistoryBinding } from "./server-probe-journal-history.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" +import { ProbePayloadExpired, ProbePayloadUnavailable } from "./server-probe-storage-journal-adapter.js" + +const now = 1800000000000, receiptId = randomUUID(), operationId = randomUUID(), connectionId = randomUUID(), oldCredentialId = randomUUID(), newCredentialId = randomUUID() +const binding: ServerProbeHistoryBinding = { receiptId, bindingProof: "a".repeat(64), context: { identity: { kind: "mcp", executionId: operationId, ownerId: "owner", connectionId, credentialId: oldCredentialId, issuedAt: now - 7200000, expiresAt: now - 3600000, operationName: "servers.probes.run" }, sourceServerId: 9, destinationServerId: 2, tool: "bandwidth" } } +const receipt: ServerProbeReceipt = { receiptId, serverId: 9, destinationServerId: 2, tool: "bandwidth", state: "completed", httpStatus: 200, output: "old public result", data: { kind: "bandwidth" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } +const proof = async (domain: string, value: unknown) => createHash("sha256").update(domain + JSON.stringify(value)).digest("hex") +function fixture() { + let plaintext = JSON.stringify({ version: 1, context: binding.context, bindingProof: binding.bindingProof, receipt }), row: Record, reads = 0, fences = 0, decrypts = 0, revokeAt = Infinity, alterOnSecondRead = false + const initialize = async () => { row = { receipt_id: receiptId, binding_proof: binding.bindingProof, identity_kind: "mcp", execution_id: operationId, actor_id: "owner", authentication: "mcp", mcp_connection_id: connectionId, mcp_credential_id: oldCredentialId, request_id: null, authority_issued_at: now - 7200000, authority_expires_at: new Date(now - 3600000), source_server_id: 9, destination_server_id: 2, tool: "bandwidth", state: "completed", attempted_stages: 1, completed_stages: 1, receipt_ciphertext: "privatecipher", receipt_proof: await proof("receipt", plaintext), quarantine_count: "0" } } + const currentCredential = newCredentialId + let active = true, currentConnection = connectionId, currentOwner = "owner", scope = [2, 9], denyProtection = false, absent = false + const reader = createServerProbeJournalHistoryReader({ + assertReadAuthority: async value => { fences++; assert.deepEqual(value, binding); assert.equal(value.context.identity.kind === "mcp" && value.context.identity.credentialId, oldCredentialId); assert.ok(active && fences < revokeAt && currentConnection === connectionId && currentOwner === "owner" && currentCredential === newCredentialId && scope.includes(9) && scope.includes(2) && !denyProtection) }, + loadArchive: async () => { reads++; if (alterOnSecondRead && reads === 2) row.quarantine_count = "1"; return absent ? [] : [structuredClone(row)] }, + crypto: { decrypt: async () => { decrypts++; return plaintext }, proof }, + }) + return { initialize, reader, setRow: (key: string, value: unknown) => { row[key] = value }, envelope: async (value: unknown, validProof = true) => { plaintext = JSON.stringify(value); if (validProof) row.receipt_proof = await proof("receipt", plaintext) }, plaintext: () => JSON.parse(plaintext), reads: () => reads, fences: () => fences, decrypts: () => decrypts, revoke: (n = 1) => { revokeAt = n }, foreignConnection: () => { currentConnection = randomUUID() }, foreignOwner: () => { currentOwner = "other" }, missingDestination: () => { scope = [9] }, denyProtection: () => { denyProtection = true }, noAnchor: () => { absent = true }, quarantineAfterDecrypt: () => { alterOnSecondRead = true }, expiredCurrent: () => { active = false } } +} +let cases = 0 +{ + const f = fixture(); await f.initialize(); assert.deepEqual(await f.reader.read(binding), receipt); assert.equal(f.reads(), 2); assert.equal(f.decrypts(), 1); assert.ok(f.fences() >= 6); cases++ +} +for (const [key, value] of Object.entries({ binding_proof: "0".repeat(64), receipt_id: randomUUID(), execution_id: randomUUID(), actor_id: "foreign", mcp_connection_id: randomUUID(), mcp_credential_id: newCredentialId, authority_issued_at: now, authority_expires_at: new Date(now + 1000), source_server_id: 2, destination_server_id: null, tool: "ping", state: "claimed", attempted_stages: 2, completed_stages: 0, receipt_proof: "0".repeat(64), quarantine_count: "1", receipt_ciphertext: "x".repeat(262145) })) { + const f = fixture(); await f.initialize(); f.setRow(key, value); await assert.rejects(f.reader.read(binding)); cases++ +} +for (const mutate of [(f: ReturnType) => f.foreignConnection(), (f: ReturnType) => f.foreignOwner(), (f: ReturnType) => f.missingDestination(), (f: ReturnType) => f.denyProtection(), (f: ReturnType) => f.expiredCurrent(), (f: ReturnType) => f.noAnchor()]) { + const f = fixture(); await f.initialize(); mutate(f); await assert.rejects(f.reader.read(binding)); assert.equal(f.decrypts(), 0); cases++ +} +for (const n of [2, 3, 4, 5, 6]) { const f = fixture(); await f.initialize(); f.revoke(n); await assert.rejects(f.reader.read(binding)); cases++ } +for (const mutate of [(e: Record) => ({ ...e, version: 2 }), (e: Record) => ({ ...e, extra: true }), (e: Record) => ({ ...e, context: { ...binding.context, identity: { ...binding.context.identity, credentialId: newCredentialId } } }), (e: Record) => ({ ...e, receipt: { ...receipt, data: { kind: "ping" } } }), (e: Record) => ({ ...e, receipt: { ...receipt, output: "x".repeat(131073) } })]) { + const f = fixture(); await f.initialize(); await f.envelope(mutate(f.plaintext())); await assert.rejects(f.reader.read(binding)); cases++ +} +{ + const f = fixture(); await f.initialize(); await f.envelope({ ...f.plaintext(), receipt: { ...receipt, output: "tampered" } }, false); await assert.rejects(f.reader.read(binding)); cases++ +} +{ + const f = fixture(); await f.initialize(); f.quarantineAfterDecrypt(); await assert.rejects(f.reader.read(binding)); assert.equal(f.reads(), 2); cases++ +} +for (const state of ["expired", "unavailable"] as const) { + const f = fixture(); await f.initialize(); f.setRow("stored_payload_state", state); f.setRow("receipt_ciphertext", null) + await assert.rejects(f.reader.read(binding), state === "expired" ? ProbePayloadExpired : ProbePayloadUnavailable) + assert.equal(f.reads(), 2); assert.equal(f.decrypts(), 0); cases++ + const changed = fixture(); await changed.initialize(); changed.setRow("stored_payload_state", state); changed.setRow("receipt_ciphertext", null); changed.quarantineAfterDecrypt() + await assert.rejects(changed.reader.read(binding), error => !(error instanceof ProbePayloadExpired || error instanceof ProbePayloadUnavailable)) + assert.equal(changed.reads(), 2); cases++ + const revoked = fixture(); await revoked.initialize(); revoked.setRow("stored_payload_state", state); revoked.setRow("receipt_ciphertext", null); revoked.revoke(3) + await assert.rejects(revoked.reader.read(binding), error => !(error instanceof ProbePayloadExpired || error instanceof ProbePayloadUnavailable)); cases++ +} +console.log(`server-probe historical journal: ${cases} inert cases passed`) diff --git a/backend/src/services/server-probe-journal-history.ts b/backend/src/services/server-probe-journal-history.ts new file mode 100644 index 00000000..7282bfc5 --- /dev/null +++ b/backend/src/services/server-probe-journal-history.ts @@ -0,0 +1,99 @@ +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { sanitizeServerProbeReceipt } from "./server-probe-public-result.js" +import type { ServerProbeJournalContext, ProbeJournalCrypto } from "./server-probe-journal.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" +import { ProbePayloadExpired, ProbePayloadUnavailable } from "./server-probe-storage-journal-adapter.js" + +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +export interface ServerProbeHistoryBinding { + /** ORIGINAL execution identity, never reconstructed from the reader's new credential. */ + context: ServerProbeJournalContext + receiptId: string + bindingProof: string +} +export interface ServerProbeJournalHistoryDependencies { + /** Verifies current read authority, both participant scopes/protections and + * the exact actual operation/step/input. Must never call native probes. */ + assertReadAuthority(binding: Readonly): Promise + /** Read-only snapshot. Storage adapters must count quarantine overlays by + * receipt OR original execution identity, including overlays without anchors. + * Replacing this loader permits future separated payload storage. */ + loadArchive?(binding: Readonly): Promise[]> + crypto?: Pick +} +const codec: Pick = { + async decrypt(value) { return (await import("../modules/mcp/security.js")).decryptSecret(value) }, + async proof(domain, value) { const security = await import("../modules/mcp/security.js"); return security.privateSecretFingerprint(security.fingerprint(value), "server-probe-journal-v1:" + domain) }, +} +export class ServerProbeHistoryUnavailable extends Error { + readonly automaticReplay = false + constructor() { super("Owned diagnostic history is unavailable or not authentic; no replay") } +} +function must(value: unknown): asserts value { if (!value) throw new ServerProbeHistoryUnavailable() } +async function loadArchive(binding: Readonly) { + const { pool } = await import("../db/index.js") + // No row locks, transactions, claim, state changes, expiry substitution or + // credential lookups. Current authority is the caller's independent fence. + return (await pool.query(`/* probe-journal:history */ SELECT j.*, + (SELECT count(*) FROM server_probe_execution_quarantines q WHERE q.receipt_id=$1 OR (q.identity_kind=$2 AND q.execution_id=$3)) AS quarantine_count + FROM server_probe_executions j WHERE j.receipt_id=$1 OR (j.identity_kind=$2 AND j.execution_id=$3) LIMIT 2`, + [binding.receiptId, binding.context.identity.kind, binding.context.identity.executionId])).rows as Record[] +} +/** Intentionally separate from the execution journal. This reader authenticates + * expired ORIGINAL envelopes and authorizes only CURRENT read credentials. */ +export function createServerProbeJournalHistoryReader(deps: ServerProbeJournalHistoryDependencies) { + const crypto = deps.crypto ?? codec, load = deps.loadArchive ?? loadArchive + return { + async read(raw: ServerProbeHistoryBinding): Promise { + const binding = structuredClone(raw), { context, receiptId, bindingProof } = binding, identity = context.identity + uuid.parse(receiptId); hash.parse(bindingProof); uuid.parse(identity.executionId) + // This history API is MCP-specific; REST uses its own durable identity adapter. + must(identity.kind === "mcp" && identity.operationName === "servers.probes.run") + must(Number.isSafeInteger(identity.issuedAt) && identity.issuedAt >= 0 && Number.isSafeInteger(identity.expiresAt) && identity.expiresAt > identity.issuedAt) + must(Number.isSafeInteger(context.sourceServerId) && context.sourceServerId > 0 && (context.tool === "bandwidth") === (context.destinationServerId !== undefined)) + if (context.destinationServerId !== undefined) must(Number.isSafeInteger(context.destinationServerId) && context.destinationServerId > 0) + await deps.assertReadAuthority(structuredClone(binding)) + const rows = await load(structuredClone(binding)) + await deps.assertReadAuthority(structuredClone(binding)) + must(rows.length === 1) + const row = structuredClone(rows[0]) + must(row.quarantine_count === "0" || row.quarantine_count === 0) + must(row.receipt_id === receiptId && row.binding_proof === bindingProof && row.identity_kind === "mcp" && row.execution_id === identity.executionId && row.actor_id === identity.ownerId && row.authentication === "mcp" && row.mcp_connection_id === identity.connectionId && row.mcp_credential_id === identity.credentialId && row.request_id === null && String(row.authority_issued_at) === String(identity.issuedAt) && new Date(row.authority_expires_at as string | Date).getTime() === identity.expiresAt && row.source_server_id === context.sourceServerId && row.destination_server_id === (context.destinationServerId ?? null) && row.tool === context.tool) + must(["completed", "failed", "cancelled"].includes(String(row.state))) + must(hash.safeParse(row.receipt_proof).success) + // Only the opt-in separated-storage loader supplies this projection. + // Original anchor identity and quarantine checks above remain mandatory. + const payloadState = row.stored_payload_state + must(payloadState === undefined || ["present", "expired", "unavailable"].includes(String(payloadState))) + if (payloadState === "expired" || payloadState === "unavailable") { + must(row.receipt_ciphertext === null) + await deps.assertReadAuthority(structuredClone(binding)) + const final = await load(structuredClone(binding)) + await deps.assertReadAuthority(structuredClone(binding)) + must(final.length === 1 && isDeepStrictEqual(final[0], row)) + if (payloadState === "expired") throw new ProbePayloadExpired() + throw new ProbePayloadUnavailable() + } + must(typeof row.receipt_ciphertext === "string" && Buffer.byteLength(row.receipt_ciphertext) > 0 && Buffer.byteLength(row.receipt_ciphertext) <= 262144 && hash.safeParse(row.receipt_proof).success) + await deps.assertReadAuthority(structuredClone(binding)) + const plaintext = await crypto.decrypt(row.receipt_ciphertext) + await deps.assertReadAuthority(structuredClone(binding)) + must(Buffer.byteLength(plaintext) <= 150000 && await crypto.proof("receipt", plaintext) === row.receipt_proof) + const envelope = JSON.parse(plaintext) as Record + must(isDeepStrictEqual(Object.keys(envelope).sort(), ["bindingProof", "context", "receipt", "version"]) && envelope.version === 1 && isDeepStrictEqual(envelope.context, context) && envelope.bindingProof === bindingProof) + const original = envelope.receipt as ServerProbeReceipt, receipt = sanitizeServerProbeReceipt(original) + must(isDeepStrictEqual(original, receipt) && Buffer.byteLength(JSON.stringify(receipt)) <= 131072) + must(receipt.receiptId === receiptId && receipt.tool === context.tool && receipt.serverId === context.sourceServerId && receipt.destinationServerId === context.destinationServerId && receipt.state === row.state && receipt.attemptedStages === row.attempted_stages && receipt.completedStages === row.completed_stages) + const budget = context.tool === "mtu" ? 11 : ["ping", "traceroute"].includes(context.tool) ? 2 : 1 + must(receipt.attemptedStages <= budget && (receipt.state === "completed" ? receipt.data?.kind === context.tool && receipt.errorCode === undefined : receipt.httpStatus >= 400 && receipt.output === undefined && receipt.data === undefined && typeof receipt.errorCode === "string")) + await deps.assertReadAuthority(structuredClone(binding)) + // Re-read the immutable anchor and overlay after decryption, so a newly + // quarantined/lost-ACK result is never adopted as completed history. + const final = await load(structuredClone(binding)) + await deps.assertReadAuthority(structuredClone(binding)) + must(final.length === 1 && isDeepStrictEqual(final[0], row)) + return receipt + }, + } +} diff --git a/backend/src/services/server-probe-journal-maintenance-model.ts b/backend/src/services/server-probe-journal-maintenance-model.ts new file mode 100644 index 00000000..0a14462a --- /dev/null +++ b/backend/src/services/server-probe-journal-maintenance-model.ts @@ -0,0 +1,9 @@ +/** Pure expiry proof, no deletion capability. Original signed authority must be + * checked before claim even when no database row remains after approved GC. */ +export interface ProbeMetadataExpiry { authorityExpiresAt:string;lastActivityAt:string } +export function probeMetadataGcEligible(value:ProbeMetadataExpiry,now:number,retentionDays:number,safetyWindowMs:number):boolean{ + const authority=Date.parse(value.authorityExpiresAt),activity=Date.parse(value.lastActivityAt) + if(!Number.isFinite(authority)||!Number.isFinite(activity)||!Number.isFinite(now)||!Number.isSafeInteger(retentionDays)||retentionDays<1||retentionDays>3650||!Number.isSafeInteger(safetyWindowMs)||safetyWindowMs<60000||safetyWindowMs>86400000)return false + const deadline=Math.max(authority,activity)+retentionDays*86400000+safetyWindowMs + return Number.isSafeInteger(deadline)&&now>deadline +} diff --git a/backend/src/services/server-probe-journal-maintenance.test.ts b/backend/src/services/server-probe-journal-maintenance.test.ts new file mode 100644 index 00000000..f688f9a7 --- /dev/null +++ b/backend/src/services/server-probe-journal-maintenance.test.ts @@ -0,0 +1,199 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createProbeJournalStorage,probeStorageFits,ProbeStorageRefused,ProbeStorageUnknown,PROBE_PAYLOAD_MAX_BYTES,PROBE_PERMANENT_RESERVATION_BYTES,type ProbeStorageDependencies,type ProbeStoragePolicy,type ProbeStorageScope } from "./server-probe-journal-maintenance.js" +import { probeMetadataGcEligible } from "./server-probe-journal-maintenance-model.js" + +const digest="a".repeat(64),at=Date.parse("2026-10-25T00:00:00Z") +const policy:ProbeStoragePolicy={retentionDays:7,globalBytes:2000000,ownerBytes:1000000,globalFiles:4,ownerFiles:2,globalAdmissions:5,ownerAdmissions:3} +const request=():ProbeStorageScope=>({ownerId:"owner-a",receiptId:randomUUID(),identityKind:"rest",executionId:randomUUID(),bindingProof:digest,authorityExpiresAt:new Date(at+300000).toISOString()}) +{ + const original={authorityExpiresAt:new Date(at).toISOString(),lastActivityAt:new Date(at-1).toISOString()} + assert.equal(probeMetadataGcEligible(original,at+7*86400000+60000,7,60000),false) + assert.equal(probeMetadataGcEligible(original,at+7*86400000+60001,7,60000),true) + assert.equal(probeMetadataGcEligible({...original,lastActivityAt:new Date(at+86400000).toISOString()},at+7*86400000+60001,7,60000),false) + assert.equal(probeMetadataGcEligible({...original,authorityExpiresAt:"unavailable"},at+100*86400000,7,60000),false) + assert.equal(probeMetadataGcEligible(original,at+100*86400000,0,60000),false) +} +type Row=Record +function fixture(){ + const cancellations=new Map();let currentTime=at + const admissions=new Map(),payloads=new Map(),usage=new Map([["global",{scope:"global",admissions:0,reserved_bytes:0,reserved_files:0}]]) + const effects:string[]=[],anchors=new Set();let queries=0,allow=true,lostCommit=0,commits=0,closedUnsafe=false,phaseHook:((tag:string)=>void)|undefined + let tail=Promise.resolve() + const deps:ProbeStorageDependencies={policy,safetyWindowMs:60000,now:()=>currentTime,async authorize(){assert.equal(allow,true);return digest},async connect(){ + let unlock:(()=>void)|undefined,saved:{admissions:Map;payloads:Map;usage:Map;anchors:Set}|undefined + const restore=()=>{if(saved){for(const [target,source]of [[admissions,saved.admissions],[payloads,saved.payloads],[usage,saved.usage]]as const){target.clear();for(const [key,value]of source)target.set(key,value)}anchors.clear();for(const key of saved.anchors)anchors.add(key)}} + return {release(unsafe){closedUnsafe ||= !!unsafe;unlock?.();unlock=undefined},async query(text,values=[],options){ + queries++;assert.ok(!options||options.timeoutMs<=10000);let rows:Row[]=[] + const tag=text.match(/probe-storage:([\w-]+)/)?.[1]??text + if(tag==="lock"){const before=tail;tail=new Promise(resolve=>{unlock=resolve});await before;saved=structuredClone({admissions,payloads,usage,anchors})} + else if(tag==="gc-candidates")rows=[...admissions.values()].filter(row=>row.owner_id===values[0]&&probeMetadataGcEligible({authorityExpiresAt:String(row.authority_expires_at),lastActivityAt:String(row.authority_expires_at)},currentTime,Number(values[1]),Number(values[3]))).slice(0,Number(values[2])).map(row=>({...row,last_activity:row.authority_expires_at})) + else if(tag==="gc-payload"){for(const id of values[0]as string[])payloads.delete(id)} + else if(tag==="gc-stages"||tag==="gc-quarantines"){} + else if(tag==="gc-anchors"){for(const id of values[0]as string[])anchors.delete(id)} + else if(tag==="gc-admissions"){for(const id of values[1]as string[]){assert.equal(admissions.get(id)!.owner_id,values[0]);admissions.delete(id);rows.push({receipt_id:id})}} + else if(tag==="gc-release")rows=(values[0]as string[]).map(key=>{const row=usage.get(key)!;row.admissions=Number(row.admissions)-Number(values[1]);row.reserved_files=Number(row.reserved_files)-Number(values[2]);row.reserved_bytes=Number(row.reserved_bytes)-Number(values[3]);return {scope:key}}) + else if(tag==="gc-owner-counter"){const key=String(values[0]),row=usage.get(key);if(row&&row.admissions===0&&row.reserved_files===0&&row.reserved_bytes===0)usage.delete(key)} + else if(tag==="gc-readback")rows=(values[0]as string[]).filter(id=>admissions.has(id)||payloads.has(id)||anchors.has(id)).map(receipt_id=>({receipt_id})) + else if(tag==="cancel-gc-candidates")rows=[...cancellations.values()].filter(row=>row.actor_id===values[0]&&row.owner_id===values[0]&&row.operation_name==="servers.probes.run"&&["applied","failed","unknown"].includes(String(row.state))&&row.finished_at!==null&&row.real_plan===true&&probeMetadataGcEligible({authorityExpiresAt:String(row.authority_expires_at),lastActivityAt:String(row.last_activity)},currentTime,Number(values[1]),Number(values[3]))).slice(0,Number(values[2])) + else if(tag==="cancel-gc-delete"){for(const id of values[0]as string[]){assert.equal(cancellations.get(id)!.actor_id,values[1]);cancellations.delete(id);rows.push({operation_id:id})}} + else if(tag==="cancel-gc-readback")rows=(values[0]as string[]).filter(id=>cancellations.has(id)).map(operation_id=>({operation_id})) + else if(tag==="owner-counter"){const key=String(values[0]);if(!usage.has(key))usage.set(key,{scope:key,admissions:0,reserved_bytes:0,reserved_files:0})} + else if(tag==="counters")rows=(values[0]as string[]).map(key=>usage.get(key)!) + else if(tag==="reserve"||tag==="cancel-reserve"){ + assert.match(text,/authority_expires_at/);assert.match(text,/clock_timestamp\(\)/) + const [id,ownerId,kind,executionId,bindingProof,expires,token]=values + if(!admissions.has(String(id))&&![...admissions.values()].some(row=>row.identity_kind===kind&&row.execution_id===executionId)){ + admissions.set(String(id),{receipt_id:id,owner_id:ownerId,identity_kind:kind,execution_id:executionId,binding_proof:bindingProof,authority_expires_at:expires,claim_token:tag==="cancel-reserve"?null:token,purged_at:null,claim_attempted_at:tag==="cancel-reserve"?new Date(at).toISOString():null,cancelled_at:tag==="cancel-reserve"?new Date(at).toISOString():null});rows=[{receipt_id:id}] + } + }else if(tag==="charge"||tag==="release"){ + rows=(values[0]as string[]).map(key=>{const row=usage.get(key)!;if(tag==="charge"){row.admissions=Number(row.admissions)+1;row.reserved_files=Number(row.reserved_files)+1;row.reserved_bytes=Number(row.reserved_bytes)+Number(values[1])}else{row.reserved_files=Number(row.reserved_files)-Number(values[1]);row.reserved_bytes=Number(row.reserved_bytes)-Number(values[2])}return {scope:key}}) + }else if(tag==="admission")rows=[...admissions.values()].filter(row=>row.receipt_id===values[0]||row.identity_kind===values[1]&&row.execution_id===values[2]) + else if(tag==="cancel-existing"){const row=admissions.get(String(values[0]));if(row&&row.cancelled_at===null){row.cancelled_at=new Date(at).toISOString();row.claim_token=null;row.claim_attempted_at??=new Date(at).toISOString();rows=[{receipt_id:values[0]}]}} + else if(tag==="consume"){const row=admissions.get(String(values[0]));assert.match(text,/authority_expires_at>clock_timestamp/);assert.match(text,/cancelled_at IS NULL/);if(row?.claim_token===values[1]&&row.purged_at===null&&row.cancelled_at===null){row.claim_token=null;row.claim_attempted_at=new Date(at).toISOString();rows=[{receipt_id:values[0]}]}} + else if(tag==="publish"){const id=String(values[0]);assert.ok(anchors.has(id));assert.ok(!payloads.has(id));payloads.set(id,{receipt_ciphertext:values[1],receipt_proof:values[2]});rows=[{receipt_id:id}]} + else if(tag==="read"){const row=admissions.get(String(values[0]));if(row)rows=[{...row,...(payloads.get(String(values[0]))??{receipt_ciphertext:null,receipt_proof:null})}]} + else if(tag==="purge-candidates"){assert.match(text,/a.owner_id=\$1/);assert.match(text,/authority_expires_at/);rows=[...admissions.values()].filter(row=>row.owner_id===values[0]&&row.purged_at===null&&row.expired===true).slice(0,Number(values[2])).map(row=>({receipt_id:row.receipt_id}))} + else if(tag==="mark-purged"){for(const id of values[1]as string[]){const row=admissions.get(id)!;assert.equal(row.owner_id,values[0]);assert.equal(row.purged_at,null);row.purged_at=new Date(at).toISOString();row.claim_token=null;rows.push({receipt_id:id})}} + else if(tag==="delete-payload"){for(const id of values[0]as string[]){assert.notEqual(admissions.get(id)!.purged_at,null);if(payloads.delete(id))rows.push({receipt_id:id})}} + else if(tag==="purge-readback")rows=(values[0]as string[]).filter(id=>payloads.has(id)).map(id=>({receipt_id:id})) + else if(text==="test:anchor"){anchors.add(String(values[0]));effects.push(text)} + else if(text==="test:terminal")effects.push(text) + else if(text==="COMMIT"){commits++;unlock?.();unlock=undefined;if(commits===lostCommit)throw Error("private-driver-detail")} + else if(text==="ROLLBACK"){restore();unlock?.();unlock=undefined} + phaseHook?.(tag);return {rows:structuredClone(rows)} + }} + }} + return {cancellations,setNow(value:number){currentTime=value},deps,storage:createProbeJournalStorage(deps),admissions,payloads,usage,effects,anchors,options:{deadlineAt:at+120000},setAllow(value:boolean){allow=value},setHook(value:(tag:string)=>void){phaseHook=value},loseCommit(value:number){lostCommit=value},get queries(){return queries},get unsafe(){return closedUnsafe}} +} +async function claimed(f:ReturnType,scope=request()){await f.storage.claim(scope,f.options,sql=>sql.query("test:anchor",[scope.receiptId]));return scope} + +{ + const f=fixture(),scope=await claimed(f) + assert.equal(f.usage.get("global")!.reserved_bytes,PROBE_PAYLOAD_MAX_BYTES+PROBE_PERMANENT_RESERVATION_BYTES) + await f.storage.publish(scope,"sealed-payload",digest,f.options,sql=>sql.query("test:terminal")) + assert.equal((await f.storage.read(scope,f.options)).kind,"payload") + const foreign=await claimed(f,{...request(),ownerId:"owner-b"});f.admissions.get(scope.receiptId)!.expired=true;f.admissions.get(foreign.receiptId)!.expired=true + assert.deepEqual(await f.storage.purge(scope.ownerId,100,f.options),{purged:1,automaticReplay:false}) + assert.ok(f.anchors.has(scope.receiptId));assert.ok(f.admissions.has(scope.receiptId));assert.equal(f.payloads.has(scope.receiptId),false) + assert.equal(f.admissions.get(foreign.receiptId)!.purged_at,null) + assert.equal((await f.storage.read(scope,f.options)).kind,"expired") + assert.equal(f.usage.get("owner:owner-a")!.admissions,1);assert.equal(f.usage.get("owner:owner-a")!.reserved_bytes,PROBE_PERMANENT_RESERVATION_BYTES) + assert.deepEqual(await f.storage.purge(scope.ownerId,100,f.options),{purged:0,automaticReplay:false}) + await assert.rejects(f.storage.claim(scope,f.options,async()=>{throw Error("replayed")}),ProbeStorageRefused) + await assert.rejects(f.storage.publish(scope,"again",digest,f.options,async()=>{throw Error("republished")}),ProbeStorageRefused) + assert.equal(f.effects.filter(value=>value==="test:anchor").length,2) +} +{ + const f=fixture();f.loseCommit(1) + await assert.rejects(claimed(f),ProbeStorageUnknown);assert.equal(f.effects.length,0);assert.equal(f.admissions.size,1);assert.equal(f.unsafe,true) +} +{ + const f=fixture(),scope=request();f.loseCommit(2) + await assert.rejects(claimed(f,scope),ProbeStorageUnknown);assert.equal(f.effects.length,1) + await assert.rejects(claimed(f,scope),ProbeStorageRefused);assert.equal(f.effects.length,1) +} +{ + const f=fixture(),scope=request();f.setHook(tag=>{if(tag==="consume")f.setAllow(false)}) + await assert.rejects(claimed(f,scope),ProbeStorageRefused);assert.equal(f.effects.length,0);assert.equal(f.admissions.size,1) +} +{ + const f=fixture(),scope=await claimed(f);f.setHook(tag=>{if(tag==="publish")f.setAllow(false)}) + await assert.rejects(f.storage.publish(scope,"payload",digest,f.options,sql=>sql.query("test:terminal")),ProbeStorageRefused) + assert.equal(f.payloads.size,0) +} +{ + const f=fixture(),scopes=[request(),request(),request()] + const outcomes=await Promise.allSettled(scopes.map(scope=>claimed(f,scope))) + assert.equal(outcomes.filter(value=>value.status==="fulfilled").length,2);assert.equal(f.admissions.size,2);assert.equal(f.usage.get("owner:owner-a")!.reserved_files,2) + assert.equal(probeStorageFits(policy,{admissions:0,reserved_bytes:Number.MAX_SAFE_INTEGER,reserved_files:0},{admissions:0,reserved_bytes:0,reserved_files:0}),false) + const before=f.queries;await assert.rejects(claimed(f,{...request(),authorityExpiresAt:new Date(at).toISOString()}),ProbeStorageRefused);assert.equal(f.queries,before) + await assert.rejects(f.storage.purge("owner-a",101,f.options),ProbeStorageRefused) +} +{ + const f=fixture(),old=await claimed(f) + await f.storage.publish(old,"old-payload",digest,f.options,sql=>sql.query("test:terminal")) + const foreign=await claimed(f,{...request(),ownerId:"owner-b"}) + const after=at+9*86400000,gcOptions={deadlineAt:after+120000};f.setNow(after) + assert.deepEqual(await f.storage.collectExpired("owner-a",100,gcOptions),{collected:1,automaticReplay:false}) + assert.equal(f.admissions.has(old.receiptId),false);assert.equal(f.anchors.has(old.receiptId),false);assert.equal(f.payloads.has(old.receiptId),false) + assert.ok(f.admissions.has(foreign.receiptId));assert.equal(f.usage.has("owner:owner-a"),false) + assert.deepEqual(await f.storage.collectExpired("owner-a",100,gcOptions),{collected:0,automaticReplay:false}) + const calls=f.queries;await assert.rejects(f.storage.claim(old,gcOptions,sql=>sql.query("test:anchor",[old.receiptId])),ProbeStorageRefused);assert.equal(f.queries,calls) + const fresh={...request(),authorityExpiresAt:new Date(after+300000).toISOString()} + await f.storage.claim(fresh,gcOptions,sql=>sql.query("test:anchor",[fresh.receiptId]));assert.ok(f.admissions.has(fresh.receiptId)) +} +{ + const f=fixture(),old=await claimed(f);f.admissions.get(old.receiptId)!.expired=true + await f.storage.purge("owner-a",100,f.options) + const after=at+9*86400000;f.setNow(after) + await f.storage.collectExpired("owner-a",100,{deadlineAt:after+120000}) + assert.deepEqual(f.usage.get("global"),{scope:"global",admissions:0,reserved_bytes:0,reserved_files:0}) +} +{ + const f=fixture(),old=await claimed(f),after=at+9*86400000;f.setNow(after);f.setHook(tag=>{if(tag==="gc-admissions")f.setAllow(false)}) + await assert.rejects(f.storage.collectExpired("owner-a",100,{deadlineAt:after+120000}),ProbeStorageRefused) + assert.ok(f.admissions.has(old.receiptId));assert.ok(f.anchors.has(old.receiptId)) +} +{ + const f=fixture(),old=await claimed(f),after=at+9*86400000;f.setNow(after);f.loseCommit(3) + await assert.rejects(f.storage.collectExpired("owner-a",100,{deadlineAt:after+120000}),ProbeStorageUnknown) + assert.equal(f.admissions.has(old.receiptId),false);assert.equal(f.unsafe,true) +} +{ + const f=fixture(),after=at+9*86400000;f.setNow(after) + const base={connection_id:randomUUID(),actor_id:"owner-a",owner_id:"owner-a",operation_name:"servers.probes.run",real_plan:true,state:"applied",finished_at:new Date(at).toISOString(),authority_expires_at:new Date(at).toISOString(),last_activity:new Date(at).toISOString()} + for(const row of [base,{...base,state:"pending",finished_at:null},{...base,state:"running",finished_at:null},{...base,actor_id:"owner-b",owner_id:"owner-b"},{...base,real_plan:false}]){const id=randomUUID();f.cancellations.set(id,{...row,operation_id:id})} + f.usage.set("owner:owner-a",{scope:"owner:owner-a",admissions:4,reserved_files:4,reserved_bytes:4*294912});f.usage.set("global",{scope:"global",admissions:5,reserved_files:5,reserved_bytes:5*294912}) + assert.deepEqual(await f.storage.collectExpiredCancellations("owner-a",100,{deadlineAt:after+120000}),{collected:1,automaticReplay:false}) + assert.equal(f.cancellations.size,4);assert.equal(f.usage.get("owner:owner-a")!.reserved_files,4);assert.equal(f.usage.get("owner:owner-a")!.reserved_bytes,4*294912) + assert.deepEqual(await f.storage.collectExpiredCancellations("owner-a",100,{deadlineAt:after+120000}),{collected:0,automaticReplay:false}) +} + +console.log("Probe journal storage inert tests passed: preclaim budgets, concurrent admission, lost COMMIT, owner/late guards, payload retention and approved expiry GC; no SQL/network") + +async function cancelled(f:ReturnType,bound=request()){ + return f.storage.cancel(bound.ownerId,f.options,async sql=>{await sql.query("test:source-lock");return bound},async sql=>{await sql.query("test:cancel");return bound.receiptId}) +} +{ + const f=fixture(),bound={...request(),authorityExpiresAt:new Date(at-1).toISOString()},order:string[]=[] + f.setHook(tag=>order.push(tag));assert.equal(await cancelled(f,bound),bound.receiptId) + assert.ok(order.indexOf("lock")sql.query("test:terminal")),ProbeStorageRefused) +} +{ + const f=fixture(),bound=await claimed(f),row=f.admissions.get(bound.receiptId)!,attempted=row.claim_attempted_at + await cancelled(f,bound);assert.equal(row.claim_attempted_at,attempted);assert.equal(f.usage.get("global")!.admissions,1) + await assert.rejects(cancelled(f,{...bound,ownerId:"other-owner"}),ProbeStorageRefused) + await assert.rejects(cancelled(f,{...bound,receiptId:randomUUID()}),ProbeStorageRefused) + await assert.rejects(cancelled(f,{...bound,executionId:randomUUID()}),ProbeStorageRefused) +} +{ + const f=fixture(),bounds=[request(),request(),request()] + const result=await Promise.allSettled([cancelled(f,bounds[0]),claimed(f,bounds[1]),cancelled(f,bounds[2])]) + assert.equal(result.filter(value=>value.status==="fulfilled").length,2);assert.equal(f.usage.get("global")!.admissions,2) + assert.equal(f.usage.get("global")!.reserved_bytes,2*294912) +} +{ + const f=fixture(),bound=request();f.loseCommit(1) + await assert.rejects(cancelled(f,bound),ProbeStorageUnknown);assert.equal(f.unsafe,true) + assert.notEqual(f.admissions.get(bound.receiptId)!.cancelled_at,null) + await cancelled(f,bound);assert.equal(f.usage.get("global")!.admissions,1) +} +{ + const f=fixture();const result=await f.storage.cancel("owner-a",f.options,async()=>null,async()=>{throw Error("terminal source must not write")}) + assert.equal(result,undefined);assert.equal(f.admissions.size,0);assert.equal(f.usage.size,1) +} +{ + const f=fixture(),bound=request();let boundChecks=0,wrote=false + f.deps.authorize=async(ownerId,operation,scope)=>{assert.equal(ownerId,bound.ownerId);assert.equal(operation,"cancel");if(scope){boundChecks++;throw Error("revoked source")};return digest} + await assert.rejects(f.storage.cancel(bound.ownerId,f.options,async()=>bound,async()=>{wrote=true}),ProbeStorageRefused) + assert.equal(boundChecks,1);assert.equal(wrote,false);assert.equal(f.admissions.size,0) +} +console.log("Probe cancellation shared quota tests passed: original expired authority, lock order, full charge, duplicate under exhaustion, collisions, execution race, bound fence, terminal no-op and unknown COMMIT") diff --git a/backend/src/services/server-probe-journal-maintenance.ts b/backend/src/services/server-probe-journal-maintenance.ts new file mode 100644 index 00000000..29207a82 --- /dev/null +++ b/backend/src/services/server-probe-journal-maintenance.ts @@ -0,0 +1,287 @@ +/** Opt-in v2 storage boundary. No imports initialize a database or scheduler. + * Admission is committed BEFORE an anchor claim is attempted. Its permanent + * unique identity survives payload retention. Metadata expiry GC requires + * trusted original-expiry entry gates and the guarded v2 schema proposal. */ +import { randomUUID } from "node:crypto" +import { probeMetadataGcEligible } from "./server-probe-journal-maintenance-model.js" +import { admitPartitionWriter } from "./partition-write-transaction.js" + +export interface ProbeStorageSql { query(text:string,values?:unknown[],options?:{signal?:AbortSignal;timeoutMs:number}):Promise<{rows:Record[]}> } +export interface ProbeStorageConnection extends ProbeStorageSql { + /** Revocation only: ends when this physical borrow closes, never authority. */ + readonly lifetimeSignal?:AbortSignal + release(unsafe?:boolean):void +} +export interface ProbeStorageScope { + ownerId:string; receiptId:string; identityKind:"rest"|"mcp"; executionId:string; bindingProof:string + authorityExpiresAt:string +} +export interface ProbeStoragePolicy { + retentionDays:number; globalBytes:number; ownerBytes:number; globalFiles:number; ownerFiles:number + globalAdmissions:number; ownerAdmissions:number +} +export interface ProbeStorageOptions { signal?:AbortSignal; deadlineAt:number } +export interface ProbeStorageDependencies { + connect(options:ProbeStorageOptions):Promise + /** Fresh real owner/policy/source/protection checks BEFORE/AFTER each SQL. + * Purge requires global storage maintenance plus this exact owner scope. + * Return a private authority/epoch proof stable for the whole operation. */ + authorize(ownerId:string,operation:"claim"|"publish"|"read"|"purge"|"cancel",scope:Readonly|undefined,options:ProbeStorageOptions):Promise + /** Explicit root mapping, e.g. requestsRetentionDays. No new UI/settings. + * Include this policy revision in authorize's proof when mutable. */ + policy:ProbeStoragePolicy + safetyWindowMs:number + now?:()=>number +} +export class ProbeStorageRefused extends Error { readonly automaticReplay=false;constructor(){super("Probe storage refused: scope, budget, authority or permanent admission mismatch; no replay")} } +export class ProbeStorageUnknown extends Error { readonly automaticReplay=false;constructor(){super("Probe storage outcome unknown; no retry, claim or payload publication permitted")} } +export const PROBE_PAYLOAD_MAX_BYTES=262144 +/** Conservative logical metadata allowance per attempt (anchor, admission, + * <=11 stages, <=1 quarantine and <=1 cancellation). PostgreSQL physical/WAL/index + * bloat requires independent disk monitoring/vacuum, not this logical quota. */ +export const PROBE_PERMANENT_RESERVATION_BYTES=32768 +const reservation=PROBE_PAYLOAD_MAX_BYTES +const hash=/^[a-f0-9]{64}$/,uuid=/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i +function must(value:unknown):asserts value{if(!value)throw new ProbeStorageRefused()} +function owner(value:string){must(typeof value==="string"&&value.length>0&&value.length<=200&&value.trim()===value&&!/[\x00-\x1f\x7f]/.test(value))} +function scope(value:ProbeStorageScope):ProbeStorageScope{ + owner(value.ownerId);must(uuid.test(value.receiptId)&&uuid.test(value.executionId)&&hash.test(value.bindingProof)&&["mcp","rest"].includes(value.identityKind)&&Number.isFinite(Date.parse(value.authorityExpiresAt))) + return {...value,authorityExpiresAt:new Date(value.authorityExpiresAt).toISOString()} +} +export function validateProbeStoragePolicy(raw:Record):ProbeStoragePolicy{ + const keys={retentionDays:"retention_days",globalBytes:"global_bytes",ownerBytes:"owner_bytes",globalFiles:"global_files",ownerFiles:"owner_files",globalAdmissions:"global_admissions",ownerAdmissions:"owner_admissions"} as const + const result={} as ProbeStoragePolicy + for(const [key,column]of Object.entries(keys)){const value=Number(raw[column]);must(Number.isSafeInteger(value)&&value>0);result[key as keyof ProbeStoragePolicy]=value} + must(result.retentionDays<=3650&&result.ownerBytes<=result.globalBytes&&result.ownerFiles<=result.globalFiles&&result.ownerAdmissions<=result.globalAdmissions) + return result +} +/** Pure overflow-safe admission check, used after locking both SQL counters. + * Retained admissions bound metadata. Payload purge never refunds metadata. + * Expired metadata GC refunds these retained caps, never a callable attempt. */ +export function probeStorageFits(policy:ProbeStoragePolicy,global:Record,perOwner:Record):boolean{ + const check=(row:Record,bytes:number,files:number,admissions:number)=>{ + const count=Number(row.admissions),reserved=Number(row.reserved_bytes),live=Number(row.reserved_files) + return [count,reserved,live].every(value=>Number.isSafeInteger(value)&&value>=0)&&count=60000&&deps.safetyWindowMs<=86400000) + const policy=validateProbeStoragePolicy({retention_days:configured.retentionDays,global_bytes:configured.globalBytes,owner_bytes:configured.ownerBytes,global_files:configured.globalFiles,owner_files:configured.ownerFiles,global_admissions:configured.globalAdmissions,owner_admissions:configured.ownerAdmissions}) + const run=async(ownerId:string,operation:"claim"|"publish"|"read"|"purge"|"cancel",bound:ProbeStorageScope|undefined,options:ProbeStorageOptions,work:(sql:ProbeStorageSql,policy:ProbeStoragePolicy,bind:(value:ProbeStorageScope)=>Promise)=>Promise,mode:"default"|"expiry-write"="default"):Promise=>{ + owner(ownerId);must(Number.isFinite(options.deadlineAt)&&options.deadlineAt>now()) + let authority:string|undefined + const fence=async()=>{options.signal?.throwIfAborted();must(now()connection.query(text,values,{signal:options.signal,timeoutMs:Math.max(1,Math.min(10000,options.deadlineAt-now()))}) + const sql:ProbeStorageSql={query:async(text,values)=>{await fence();const result=await query(text,values);await fence();return result}} + try{ + if(mode==="expiry-write"||operation==="claim"||operation==="publish"||operation==="cancel"){ + // Acquire the common key before authority checks can retain a relation + // lock on this backend and before the secondary probe-storage lock. + await fence() + await query("BEGIN") + try{await admitPartitionWriter({query},{signal:options.signal,deadlineAt:Date.now()+Math.min(options.deadlineAt-now(),1000)})} + catch{began=true;throw new ProbeStorageRefused()} + if(mode!=="expiry-write"){ + began=true + await query("SET LOCAL lock_timeout TO DEFAULT") + } + await fence() + }else await sql.query("BEGIN") + began=true + await sql.query("SET LOCAL TimeZone='UTC'") + await sql.query(`SET LOCAL statement_timeout='${Math.max(1,Math.min(10000,options.deadlineAt-now()))}ms'`) + // Shared ordering across EVERY replica, admission, finish and purge. + await sql.query("/* probe-storage:lock */ SELECT pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0))") + const result=await work(sql,policy,async value=>{must(value.ownerId===ownerId);bound=value;await fence()});await fence();commitSent=true;await sql.query("COMMIT");committed=true;return result + }catch(error){ + if(commitSent&&!committed||!began){destroy=true;throw new ProbeStorageUnknown()} + try{await connection.query("ROLLBACK")}catch{destroy=true} + throw error instanceof ProbeStorageRefused?error:new ProbeStorageRefused() + }finally{connection.release(destroy)} + } + const admission=async(sql:ProbeStorageSql,bound:ProbeStorageScope)=>{ + const rows=(await sql.query("/* probe-storage:admission */ SELECT * FROM server_probe_storage_admissions WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) FOR UPDATE",[bound.receiptId,bound.identityKind,bound.executionId])).rows + must(rows.length===1);const row=rows[0] + must(row.receipt_id===bound.receiptId&&row.owner_id===bound.ownerId&&row.identity_kind===bound.identityKind&&row.execution_id===bound.executionId&&row.binding_proof===bound.bindingProof&&new Date(row.authority_expires_at as string|Date).toISOString()===bound.authorityExpiresAt&&row.purged_at===null) + return row + } + const counters=async(sql:ProbeStorageSql,ownerId:string)=>{ + await sql.query("/* probe-storage:owner-counter */ INSERT INTO server_probe_storage_usage(scope) VALUES($1) ON CONFLICT DO NOTHING",["owner:"+ownerId]) + const rows=(await sql.query("/* probe-storage:counters */ SELECT * FROM server_probe_storage_usage WHERE scope=ANY($1::text[]) ORDER BY scope FOR UPDATE",[["global","owner:"+ownerId]])).rows + must(rows.length===2);const global=rows.find(row=>row.scope==="global"),local=rows.find(row=>row.scope==="owner:"+ownerId);must(global&&local);return {global,local} + } + return { + /** Resolve authenticated ORIGINAL authority under its operation lock only + * AFTER the shared storage lock. Both callbacks use this same transaction. + * null is a terminal-source no-op. Expired original authority may cancel. + * An exact existing identity spends no further quota, even after purge. */ + async cancel(ownerId:string,options:ProbeStorageOptions,resolveScope:(sql:ProbeStorageSql)=>Promise,writeCancellation:(sql:ProbeStorageSql,bound:ProbeStorageScope)=>Promise):Promise{ + return run(ownerId,"cancel",undefined,options,async(sql,policy,bind)=>{ + const resolved=await resolveScope(sql);if(resolved===null)return undefined + const bound=scope(resolved);await bind(bound) + const existing=(await sql.query("/* probe-storage:admission */ SELECT * FROM server_probe_storage_admissions WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) FOR UPDATE",[bound.receiptId,bound.identityKind,bound.executionId])).rows + if(existing.length){ + must(existing.length===1);const row=existing[0] + must(row.receipt_id===bound.receiptId&&row.owner_id===bound.ownerId&&row.identity_kind===bound.identityKind&&row.execution_id===bound.executionId&&row.binding_proof===bound.bindingProof&&new Date(row.authority_expires_at as string|Date).toISOString()===bound.authorityExpiresAt) + if(row.cancelled_at==null){ + const marked=(await sql.query("/* probe-storage:cancel-existing */ UPDATE server_probe_storage_admissions SET cancelled_at=clock_timestamp(),claim_token=NULL,claim_attempted_at=COALESCE(claim_attempted_at,clock_timestamp()) WHERE receipt_id=$1 AND cancelled_at IS NULL RETURNING receipt_id",[bound.receiptId])).rows + must(marked.length===1&&marked[0].receipt_id===bound.receiptId) + } + }else{ + const usage=await counters(sql,ownerId);must(probeStorageFits(policy,usage.global,usage.local)) + const rows=(await sql.query("/* probe-storage:cancel-reserve */ INSERT INTO server_probe_storage_admissions(receipt_id,owner_id,identity_kind,execution_id,binding_proof,authority_expires_at,claim_token,claim_attempted_at,cancelled_at,retention_days,safety_ms,gc_not_before) VALUES($1,$2,$3,$4,$5,$6::timestamptz,NULL,clock_timestamp(),clock_timestamp(),$7::integer,$8::integer,GREATEST($6::timestamptz,clock_timestamp())+make_interval(days=>$7::integer)+$8::integer*interval '1 millisecond') ON CONFLICT DO NOTHING RETURNING receipt_id",[bound.receiptId,bound.ownerId,bound.identityKind,bound.executionId,bound.bindingProof,bound.authorityExpiresAt,policy.retentionDays,deps.safetyWindowMs])).rows + must(rows.length===1&&rows[0].receipt_id===bound.receiptId) + const charged=(await sql.query("/* probe-storage:charge */ UPDATE server_probe_storage_usage SET admissions=admissions+1,reserved_files=reserved_files+1,reserved_bytes=reserved_bytes+$2 WHERE scope=ANY($1::text[]) RETURNING scope",[["global","owner:"+ownerId],reservation+PROBE_PERMANENT_RESERVATION_BYTES])).rows;must(charged.length===2) + } + return writeCancellation(sql,bound) + }) + }, + /** One invocation only. Admission COMMIT precedes callback/anchor SQL. + * A crash or lost COMMIT ACK permanently spends this identity. No API to + * recreate a claim token exists. Callback uses only this guarded SQL. */ + async claim(raw:ProbeStorageScope,options:ProbeStorageOptions,writeAnchor:(sql:ProbeStorageSql)=>Promise):Promise{ + const bound=scope(raw);must(now(){ + const usage=await counters(sql,bound.ownerId);must(probeStorageFits(policy,usage.global,usage.local)) + const rows=(await sql.query("/* probe-storage:reserve */ INSERT INTO server_probe_storage_admissions(receipt_id,owner_id,identity_kind,execution_id,binding_proof,authority_expires_at,claim_token,retention_days,safety_ms,gc_not_before) SELECT $1,$2,$3,$4,$5,$6::timestamptz,$7,$8::integer,$9::integer,$6::timestamptz+make_interval(days=>$8::integer)+($9::integer*interval '1 millisecond') WHERE $6::timestamptz>clock_timestamp() ON CONFLICT DO NOTHING RETURNING receipt_id",[bound.receiptId,bound.ownerId,bound.identityKind,bound.executionId,bound.bindingProof,bound.authorityExpiresAt,token,policy.retentionDays,deps.safetyWindowMs])).rows + must(rows.length===1&&rows[0].receipt_id===bound.receiptId) + const changed=(await sql.query("/* probe-storage:charge */ UPDATE server_probe_storage_usage SET admissions=admissions+1,reserved_files=reserved_files+1,reserved_bytes=reserved_bytes+$2 WHERE scope=ANY($1::text[]) RETURNING scope",[["global","owner:"+bound.ownerId],reservation+PROBE_PERMANENT_RESERVATION_BYTES])).rows;must(changed.length===2) + }) + return run(bound.ownerId,"claim",bound,options,async sql=>{ + const row=await admission(sql,bound);must(row.cancelled_at==null&&now()clock_timestamp() RETURNING receipt_id",[bound.receiptId,token])).rows + must(consumed.length===1);return writeAnchor(sql) + }) + }, + /** Call inside the finish boundary instead of storing ciphertext in the + * immutable anchor. The supplied terminal update and payload INSERT commit + * atomically. Full maximum payload capacity was reserved before claim. */ + async publish(raw:ProbeStorageScope,ciphertext:string,proof:string,options:ProbeStorageOptions,writeTerminal:(sql:ProbeStorageSql)=>Promise):Promise{ + const bound=scope(raw);must(typeof ciphertext==="string"&&Buffer.byteLength(ciphertext)>0&&Buffer.byteLength(ciphertext)<=reservation&&hash.test(proof)) + return run(bound.ownerId,"publish",bound,options,async sql=>{ + const row=await admission(sql,bound);must(row.cancelled_at==null&&row.claim_token===null&&row.claim_attempted_at!==null&&now(){ + const rows=(await sql.query("/* probe-storage:read */ SELECT a.owner_id,a.binding_proof,a.identity_kind,a.execution_id,a.purged_at,p.receipt_ciphertext,p.receipt_proof FROM server_probe_storage_admissions a LEFT JOIN server_probe_execution_payloads p USING(receipt_id) WHERE a.receipt_id=$1",[bound.receiptId])).rows + must(rows.length===1);const row=rows[0];must(row.owner_id===bound.ownerId&&row.binding_proof===bound.bindingProof&&row.identity_kind===bound.identityKind&&row.execution_id===bound.executionId) + if(row.purged_at!==null)return {kind:"expired" as const,automaticReplay:false as const} + if(row.receipt_ciphertext===null)return {kind:"unavailable" as const,automaticReplay:false as const} + must(typeof row.receipt_ciphertext==="string"&&Buffer.byteLength(row.receipt_ciphertext)<=reservation&&typeof row.receipt_proof==="string"&&hash.test(row.receipt_proof)) + return {kind:"payload" as const,ciphertext:row.receipt_ciphertext,proof:row.receipt_proof,automaticReplay:false as const} + }) + }, + /** Small owner-scoped batch. Stale claimed/orphan admissions are safe only + * after authority expiration AND retention: they remain permanent unknown + * attempts. Existing stages, anchors and quarantines are never deleted. */ + async purge(ownerId:string,batchSize:number,options:ProbeStorageOptions){ + must(Number.isSafeInteger(batchSize)&&batchSize>=1&&batchSize<=100) + return run(ownerId,"purge",undefined,options,async(sql,policy)=>{ + await counters(sql,ownerId) + const rows=(await sql.query(`/* probe-storage:purge-candidates */ SELECT a.receipt_id FROM server_probe_storage_admissions a + LEFT JOIN server_probe_executions e USING(receipt_id) LEFT JOIN server_probe_execution_quarantines q USING(receipt_id) + WHERE a.owner_id=$1 AND a.purged_at IS NULL AND a.authority_expires_at$2) + AND COALESCE(e.finished_at,q.created_at,a.authority_expires_at)$2) + ORDER BY a.created_at,a.receipt_id LIMIT $3 FOR UPDATE OF a`,[ownerId,policy.retentionDays,batchSize])).rows + must(rows.length<=batchSize&&rows.every(row=>typeof row.receipt_id==="string"&&uuid.test(row.receipt_id))&&new Set(rows.map(row=>row.receipt_id)).size===rows.length) + const ids=rows.map(row=>row.receipt_id);if(!ids.length)return {purged:0,automaticReplay:false as const} + const changed=(await sql.query("/* probe-storage:mark-purged */ UPDATE server_probe_storage_admissions SET purged_at=clock_timestamp(),claim_token=NULL WHERE owner_id=$1 AND receipt_id=ANY($2::uuid[]) AND purged_at IS NULL RETURNING receipt_id",[ownerId,ids])).rows + must(changed.length===ids.length&&changed.every(row=>ids.includes(row.receipt_id))) + await sql.query("/* probe-storage:delete-payload */ DELETE FROM server_probe_execution_payloads WHERE receipt_id=ANY($1::uuid[]) RETURNING receipt_id",[ids]) + const absent=(await sql.query("/* probe-storage:purge-readback */ SELECT receipt_id FROM server_probe_execution_payloads WHERE receipt_id=ANY($1::uuid[])",[ids])).rows;must(absent.length===0) + const released=(await sql.query("/* probe-storage:release */ UPDATE server_probe_storage_usage SET reserved_files=reserved_files-$2,reserved_bytes=reserved_bytes-$3 WHERE scope=ANY($1::text[]) AND reserved_files>=$2 AND reserved_bytes>=$3 RETURNING scope",[["global","owner:"+ownerId],ids.length,ids.length*reservation])).rows;must(released.length===2) + return {purged:ids.length,automaticReplay:false as const} + }) + }, + /** Approved bounded metadata retention, never native execution. + * Original authority expiry must be validated before all claim attempts, + * even after this collector removes the old identity tombstone. */ + async collectExpired(ownerId:string,batchSize:number,options:ProbeStorageOptions){ + must(Number.isSafeInteger(batchSize)&&batchSize>=1&&batchSize<=100) + return run(ownerId,"purge",undefined,options,async(sql,policy)=>{ + await counters(sql,ownerId) + await sql.query("SET LOCAL mm.probe_storage_gc='on'") + const rows=(await sql.query(`/* probe-storage:gc-candidates */ SELECT a.receipt_id,a.purged_at,a.authority_expires_at, + GREATEST(a.authority_expires_at,a.created_at,COALESCE(a.cancelled_at,a.created_at),COALESCE(e.finished_at,a.created_at),COALESCE(q.created_at,a.created_at), + COALESCE((SELECT max(GREATEST(s.created_at,COALESCE(s.received_at,s.created_at))) FROM server_probe_execution_stages s WHERE s.receipt_id=a.receipt_id),a.created_at)) AS last_activity + FROM server_probe_storage_admissions a LEFT JOIN server_probe_executions e USING(receipt_id) + LEFT JOIN server_probe_execution_quarantines q USING(receipt_id) + WHERE a.owner_id=$1 AND a.gc_not_beforeGREATEST(a.retention_days,$2))- (GREATEST(a.safety_ms,$4)::bigint*interval '1 millisecond') + ORDER BY a.gc_not_before,a.receipt_id LIMIT $3 FOR UPDATE OF a`,[ownerId,policy.retentionDays,batchSize,deps.safetyWindowMs])).rows + must(rows.length<=batchSize&&rows.every(row=>typeof row.receipt_id==="string"&&uuid.test(row.receipt_id)&&typeof row.purged_at!=="undefined")&&new Set(rows.map(row=>row.receipt_id)).size===rows.length) + must(rows.every(row=>probeMetadataGcEligible({authorityExpiresAt:new Date(row.authority_expires_at as string|Date).toISOString(),lastActivityAt:new Date(row.last_activity as string|Date).toISOString()},now(),policy.retentionDays,deps.safetyWindowMs))) + const ids=rows.map(row=>row.receipt_id);if(!ids.length)return {collected:0,automaticReplay:false as const} + // The trigger validates the persisted deadline and original authority; + // this flag ALONE cannot authorize any deletion. + await sql.query("SET LOCAL mm.probe_storage_gc='on'") + await sql.query("/* probe-storage:gc-cancellations */ DELETE FROM mcp_diagnostic_cancellations c USING server_probe_storage_admissions a WHERE a.receipt_id=ANY($1::uuid[]) AND a.identity_kind='mcp' AND c.operation_id=a.execution_id AND c.actor_id=a.owner_id",[ids]) + await sql.query("/* probe-storage:gc-payload */ DELETE FROM server_probe_execution_payloads WHERE receipt_id=ANY($1::uuid[])",[ids]) + await sql.query("/* probe-storage:gc-stages */ DELETE FROM server_probe_execution_stages WHERE receipt_id=ANY($1::uuid[])",[ids]) + await sql.query("/* probe-storage:gc-quarantines */ DELETE FROM server_probe_execution_quarantines WHERE receipt_id=ANY($1::uuid[])",[ids]) + await sql.query("/* probe-storage:gc-anchors */ DELETE FROM server_probe_executions WHERE receipt_id=ANY($1::uuid[])",[ids]) + const removed=(await sql.query("/* probe-storage:gc-admissions */ DELETE FROM server_probe_storage_admissions WHERE owner_id=$1 AND receipt_id=ANY($2::uuid[]) AND gc_not_beforeids.includes(row.receipt_id))&&new Set(removed.map(row=>row.receipt_id)).size===ids.length) + const files=rows.filter(row=>row.purged_at===null).length,bytes=files*reservation+ids.length*PROBE_PERMANENT_RESERVATION_BYTES + const released=(await sql.query("/* probe-storage:gc-release */ UPDATE server_probe_storage_usage SET admissions=admissions-$2,reserved_files=reserved_files-$3,reserved_bytes=reserved_bytes-$4 WHERE scope=ANY($1::text[]) AND admissions>=$2 AND reserved_files>=$3 AND reserved_bytes>=$4 RETURNING scope",[["global","owner:"+ownerId],ids.length,files,bytes])).rows;must(released.length===2) + await sql.query("/* probe-storage:gc-owner-counter */ DELETE FROM server_probe_storage_usage WHERE scope=$1 AND admissions=0 AND reserved_files=0 AND reserved_bytes=0",["owner:"+ownerId]) + const absent=(await sql.query("/* probe-storage:gc-readback */ SELECT a.receipt_id FROM server_probe_storage_admissions a WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT e.receipt_id FROM server_probe_executions e WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT s.receipt_id FROM server_probe_execution_stages s WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT q.receipt_id FROM server_probe_execution_quarantines q WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT p.receipt_id FROM server_probe_execution_payloads p WHERE receipt_id=ANY($1::uuid[])",[ids])).rows;must(absent.length===0) + return {collected:ids.length,automaticReplay:false as const} + },"expiry-write") + }, + /** DRAFT: only expired cancellation records of terminal real own diagnostic + * operations. Does not delete operations, plans, connections or audit. */ + async collectExpiredCancellations(ownerId:string,batchSize:number,options:ProbeStorageOptions){ + must(Number.isSafeInteger(batchSize)&&batchSize>=1&&batchSize<=100) + return run(ownerId,"purge",undefined,options,async(sql,policy)=>{ + await counters(sql,ownerId) + await sql.query("SET LOCAL mm.probe_storage_gc='on'") + const rows=(await sql.query(`/* probe-storage:cancel-gc-candidates */ SELECT c.operation_id,c.connection_id,c.actor_id, + p.expires_at AS authority_expires_at,GREATEST(p.expires_at,o.finished_at,c.created_at) AS last_activity + FROM mcp_diagnostic_cancellations c JOIN mcp_operations o ON o.id=c.operation_id AND o.connection_id=c.connection_id + JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_connections owner_connection ON owner_connection.id=o.connection_id + WHERE owner_connection.owner_id=$1 AND c.actor_id=$1 AND p.operation_id='servers.probes.run' + AND EXISTS(SELECT 1 FROM server_probe_storage_admissions a WHERE a.identity_kind='mcp' AND a.execution_id=c.operation_id AND a.owner_id=$1 AND server_probe_storage_gc_allowed(a.receipt_id)) + AND o.state IN ('applied','failed','unknown') AND o.finished_at IS NOT NULL + AND GREATEST(p.expires_at,o.finished_at,c.created_at)$2)-($4::bigint*interval '1 millisecond') + ORDER BY c.created_at,c.operation_id LIMIT $3 FOR UPDATE OF c,o,p,owner_connection`,[ownerId,policy.retentionDays,batchSize,deps.safetyWindowMs])).rows + must(rows.length<=batchSize&&rows.every(row=>typeof row.operation_id==="string"&&uuid.test(row.operation_id)&&row.actor_id===ownerId&&typeof row.connection_id==="string"&&uuid.test(row.connection_id))&&new Set(rows.map(row=>row.operation_id)).size===rows.length) + must(rows.every(row=>probeMetadataGcEligible({authorityExpiresAt:new Date(row.authority_expires_at as string|Date).toISOString(),lastActivityAt:new Date(row.last_activity as string|Date).toISOString()},now(),policy.retentionDays,deps.safetyWindowMs))) + const ids=rows.map(row=>row.operation_id);if(!ids.length)return {collected:0,automaticReplay:false as const} + await sql.query("SET LOCAL mm.probe_storage_gc='on'") + // Trigger independently rechecks terminal real operation and persisted + // cancellation gc_not_before; app SQL rechecks the current owner join. + const removed=(await sql.query(`/* probe-storage:cancel-gc-delete */ DELETE FROM mcp_diagnostic_cancellations c USING mcp_operations o,mcp_plans p,mcp_connections owner_connection + WHERE c.operation_id=ANY($1::uuid[]) AND c.actor_id=$2 AND o.id=c.operation_id AND o.connection_id=c.connection_id + AND p.id=o.plan_id AND p.connection_id=o.connection_id AND owner_connection.id=o.connection_id AND owner_connection.owner_id=$2 + AND p.operation_id='servers.probes.run' AND o.state IN ('applied','failed','unknown') AND o.finished_at IS NOT NULL + AND GREATEST(p.expires_at,o.finished_at,c.created_at)$3)-($4::bigint*interval '1 millisecond') RETURNING c.operation_id`,[ids,ownerId,policy.retentionDays,deps.safetyWindowMs])).rows + must(removed.length===ids.length&&removed.every(row=>ids.includes(row.operation_id))&&new Set(removed.map(row=>row.operation_id)).size===ids.length) + // Cancellation metadata shares the original admission reservation. + // Only collectExpired refunds that reservation after deleting admission. + await sql.query("/* probe-storage:gc-owner-counter */ DELETE FROM server_probe_storage_usage WHERE scope=$1 AND admissions=0 AND reserved_files=0 AND reserved_bytes=0",["owner:"+ownerId]) + const absent=(await sql.query("/* probe-storage:cancel-gc-readback */ SELECT operation_id FROM mcp_diagnostic_cancellations WHERE operation_id=ANY($1::uuid[])",[ids])).rows;must(absent.length===0) + return {collected:ids.length,automaticReplay:false as const} + }) + }, + } +} diff --git a/backend/src/services/server-probe-journal-schema.ts b/backend/src/services/server-probe-journal-schema.ts new file mode 100644 index 00000000..57e89e67 --- /dev/null +++ b/backend/src/services/server-probe-journal-schema.ts @@ -0,0 +1,68 @@ +/** Canonical DDL shared with migration 0035_server_probe_journal.sql; the SQL + * fixture checks exact parity. Importing this module never initializes SQL. + * Independent tables support real REST identities without fabricated MCP rows. + * Quarantine is an append-only overlay: a lost finish ACK cannot rewrite an + * immutable completed receipt, but must permanently prevent its publication. */ +export const SERVER_PROBE_JOURNAL_DDL = ` +CREATE TABLE server_probe_executions ( + receipt_id uuid PRIMARY KEY, binding_proof text NOT NULL CHECK(binding_proof ~ '^[a-f0-9]{64}$'), + identity_kind text NOT NULL CHECK(identity_kind IN ('mcp','rest')), + execution_id uuid NOT NULL, actor_id text NOT NULL CHECK(length(actor_id) BETWEEN 1 AND 200), + authentication text NOT NULL CHECK(authentication IN ('mcp','jwt','local')), + mcp_connection_id uuid REFERENCES mcp_connections(id), mcp_credential_id uuid REFERENCES mcp_credentials(id), request_id text, + authority_issued_at bigint NOT NULL CHECK(authority_issued_at>=0), authority_expires_at timestamptz NOT NULL, + source_server_id integer NOT NULL CHECK(source_server_id>0), destination_server_id integer CHECK(destination_server_id>0), + tool text NOT NULL CHECK(tool IN ('ping','traceroute','bandwidth','dns','route','mtu')), + state text NOT NULL DEFAULT 'claimed' CHECK(state IN ('claimed','completed','failed','cancelled')), + attempted_stages integer CHECK(attempted_stages BETWEEN 0 AND 11), completed_stages integer CHECK(completed_stages BETWEEN 0 AND attempted_stages), + receipt_ciphertext text CHECK(octet_length(receipt_ciphertext)<=262144), receipt_proof text CHECK(receipt_proof ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), finished_at timestamptz, + UNIQUE(identity_kind,execution_id), + CHECK((identity_kind='mcp' AND authentication='mcp' AND mcp_connection_id IS NOT NULL AND mcp_credential_id IS NOT NULL AND request_id IS NULL) + OR (identity_kind='rest' AND authentication IN ('jwt','local') AND mcp_connection_id IS NULL AND mcp_credential_id IS NULL AND request_id IS NOT NULL AND length(request_id) BETWEEN 1 AND 200)), + CHECK((tool='bandwidth')=(destination_server_id IS NOT NULL)), + CHECK((state='claimed' AND receipt_ciphertext IS NULL AND receipt_proof IS NULL AND finished_at IS NULL AND attempted_stages IS NULL AND completed_stages IS NULL) + OR (state<>'claimed' AND receipt_ciphertext IS NOT NULL AND receipt_proof IS NOT NULL AND finished_at IS NOT NULL AND attempted_stages IS NOT NULL AND completed_stages IS NOT NULL)) +); +CREATE TABLE server_probe_execution_stages ( + receipt_id uuid NOT NULL REFERENCES server_probe_executions(receipt_id), stage_index integer NOT NULL CHECK(stage_index BETWEEN 1 AND 11), + kind text NOT NULL CHECK(kind IN ('source-dns-A','native-ping','native-traceroute','native-bandwidth','native-route-read', + 'manager-dns-A','manager-dns-AAAA','manager-dns-MX','manager-dns-NS','manager-dns-TXT','manager-dns-CNAME','manager-dns-PTR', + 'native-mtu-1500','native-mtu-1492','native-mtu-1480','native-mtu-1476','native-mtu-1472','native-mtu-1468','native-mtu-1400','native-mtu-1280','native-mtu-1024','native-mtu-576')), + timeout_ms integer NOT NULL CHECK(timeout_ms BETWEEN 1 AND 120000), + state text NOT NULL DEFAULT 'intent' CHECK(state IN ('intent','received')), + outcome text CHECK(outcome IN ('received','source-unavailable')), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), received_at timestamptz, + PRIMARY KEY(receipt_id,stage_index), + CHECK((state='intent' AND outcome IS NULL AND received_at IS NULL) OR (state='received' AND outcome IS NOT NULL AND received_at IS NOT NULL)) +); +CREATE TABLE server_probe_execution_quarantines ( + receipt_id uuid PRIMARY KEY, binding_proof text NOT NULL CHECK(binding_proof ~ '^[a-f0-9]{64}$'), + identity_kind text NOT NULL CHECK(identity_kind IN ('mcp','rest')), execution_id uuid NOT NULL, + identity_proof text NOT NULL CHECK(identity_proof ~ '^[a-f0-9]{64}$'), + error_code text NOT NULL CHECK(error_code ~ '^[A-Z_]{1,64}$'), + attempted_stages integer NOT NULL CHECK(attempted_stages BETWEEN 0 AND 11), completed_stages integer NOT NULL CHECK(completed_stages BETWEEN 0 AND attempted_stages), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), UNIQUE(identity_kind,execution_id) +); +CREATE FUNCTION server_probe_execution_immutable() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN + IF TG_OP='DELETE' THEN RAISE EXCEPTION 'probe execution journal is append only'; END IF; + IF OLD.state<>'claimed' OR (to_jsonb(OLD)-ARRAY['state','attempted_stages','completed_stages','receipt_ciphertext','receipt_proof','finished_at']) IS DISTINCT FROM + (to_jsonb(NEW)-ARRAY['state','attempted_stages','completed_stages','receipt_ciphertext','receipt_proof','finished_at']) OR NEW.state='claimed' + THEN RAISE EXCEPTION 'probe execution identity or terminal receipt is immutable'; END IF; + IF EXISTS(SELECT 1 FROM server_probe_execution_quarantines WHERE receipt_id=OLD.receipt_id) THEN RAISE EXCEPTION 'probe execution quarantined'; END IF; + RETURN NEW; END $$; +CREATE TRIGGER server_probe_execution_immutable BEFORE UPDATE OR DELETE ON server_probe_executions FOR EACH ROW EXECUTE FUNCTION server_probe_execution_immutable(); +CREATE FUNCTION server_probe_stage_immutable() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN + IF TG_OP='DELETE' THEN RAISE EXCEPTION 'probe stage journal is append only'; END IF; + PERFORM receipt_id FROM server_probe_executions WHERE receipt_id=NEW.receipt_id AND state='claimed' FOR UPDATE; + IF NOT FOUND OR EXISTS(SELECT 1 FROM server_probe_execution_quarantines WHERE receipt_id=NEW.receipt_id) THEN RAISE EXCEPTION 'probe attempt is not executable'; END IF; + IF TG_OP='INSERT' AND (NEW.stage_index<>(SELECT COALESCE(max(stage_index),0)+1 FROM server_probe_execution_stages WHERE receipt_id=NEW.receipt_id) + OR EXISTS(SELECT 1 FROM server_probe_execution_stages WHERE receipt_id=NEW.receipt_id AND state<>'received')) + THEN RAISE EXCEPTION 'probe stages must be serial and contiguous'; END IF; + IF TG_OP='UPDATE' AND (OLD.state<>'intent' OR NEW.state<>'received' OR (to_jsonb(OLD)-ARRAY['state','outcome','received_at']) IS DISTINCT FROM (to_jsonb(NEW)-ARRAY['state','outcome','received_at'])) + THEN RAISE EXCEPTION 'probe stage terminal state is immutable'; END IF; + RETURN NEW; END $$; +CREATE TRIGGER server_probe_stage_immutable BEFORE INSERT OR UPDATE OR DELETE ON server_probe_execution_stages FOR EACH ROW EXECUTE FUNCTION server_probe_stage_immutable(); +CREATE FUNCTION server_probe_quarantine_immutable() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'probe quarantine is immutable'; END $$; +CREATE TRIGGER server_probe_quarantine_immutable BEFORE UPDATE OR DELETE ON server_probe_execution_quarantines FOR EACH ROW EXECUTE FUNCTION server_probe_quarantine_immutable(); +` diff --git a/backend/src/services/server-probe-journal.sql.test.ts b/backend/src/services/server-probe-journal.sql.test.ts new file mode 100644 index 00000000..2b4be38c --- /dev/null +++ b/backend/src/services/server-probe-journal.sql.test.ts @@ -0,0 +1,328 @@ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHmac, randomBytes, randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import { readFile } from "node:fs/promises" +import { createSqlServerProbeJournal, ServerProbeJournalConflict, ServerProbeJournalOutcomeUnknown, type ProbeJournalConnections, type ProbeJournalCrypto, type ServerProbeJournalContext } from "./server-probe-journal.js" +import { SERVER_PROBE_JOURNAL_DDL } from "./server-probe-journal-schema.js" +import type { ServerProbeJournalBinding, ServerProbeReceipt } from "./server-probe-execution.js" + +// Explicit disposable-runner admission BEFORE importing the configured pool. +// This file is not in the ordinary test profile and must never open a real DB. +assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true", "Owned isolated probe-journal runner required") +const databaseUrl = new URL(process.env.DATABASE_URL ?? "") +assert.equal(databaseUrl.hostname, "127.0.0.1") +assert.equal(databaseUrl.pathname, "/postgres") +assert(["postgres:", "postgresql:"].includes(databaseUrl.protocol)) +const stateDirectory = resolve(process.env.MCP_STATE_DIR ?? "") +assert(process.env.MCP_STATE_DIR && basename(dirname(stateDirectory)).startsWith("mm-mcp-test-"), "Disposable mm-mcp-test- state directory required") +const { pool, closePool } = await import("../db/index.js") +const { applyLegacyProbeFixtureMigrations: applySqlMigrations } = await import("../db/legacy-probe-fixture.js") +let cases = 0 +const key = randomBytes(32) +const crypto: ProbeJournalCrypto = { + async encrypt(value) { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv), body = Buffer.concat([cipher.update(value), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), body]).toString("base64") }, + async decrypt(value) { const bytes = Buffer.from(value, "base64"), cipher = createDecipheriv("aes-256-gcm", key, bytes.subarray(0, 12)); cipher.setAuthTag(bytes.subarray(12, 28)); return Buffer.concat([cipher.update(bytes.subarray(28)), cipher.final()]).toString() }, + async proof(domain, value) { return createHmac("sha256", key).update(domain).update(JSON.stringify(value)).digest("hex") }, +} +const connections: ProbeJournalConnections = { async connect() { const client = await pool.connect(); return { async query(text, values) { return client.query(text, values) }, release(destroy) { client.release(destroy) } } } } +const conflict = async (work: Promise) => { await assert.rejects(work); cases++ } + +try { + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + await applySqlMigrations(pool) + // The terminal restore test restarts normal application scheduling. Keep + // this journal-only fixture inert both before and after its dump/restore. + await pool.query("UPDATE traffic_settings SET enabled=false WHERE id=1") + await pool.query("UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false WHERE id=1") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Fresh empty disposable database required") + const migration = await readFile(new URL("../../drizzle/0035_server_probe_journal.sql", import.meta.url), "utf8") + assert.equal(migration, "-- Durable diagnostic attempts; this migration registers no MCP action.\nBEGIN;\n" + SERVER_PROBE_JOURNAL_DDL + "\nINSERT INTO schema_migrations(id) VALUES ('0035_server_probe_journal') ON CONFLICT (id) DO NOTHING;\nCOMMIT;\n") + assert.equal((await pool.query("SELECT to_regclass('server_probe_executions') AS name")).rows[0].name, "server_probe_executions", "The real migration must install the journal") + await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0035_server_probe_journal'")).rows[0].n, 1, "Repeated migration discovery preserves the completed atomic marker") + const serverIds = (await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('probe-journal-source','127.0.0.1',9,true),('probe-journal-destination','127.0.0.1',9,true),('probe-journal-foreign','127.0.0.1',9,true) RETURNING id")).rows.map(row => Number(row.id)) + const [source, destination, foreign] = serverIds + const fixture = async (kind: "jwt" | "local" | "mcp" = "jwt", tool: ServerProbeJournalContext["tool"] = "ping", customCrypto = crypto) => { + const issuedAt = Date.now(), expiresAt = issuedAt + 3600000, ownerId = `probe-owner-${randomUUID()}`, executionId = randomUUID() + const identity: ServerProbeJournalContext["identity"] = kind === "mcp" ? { kind, ownerId, executionId, connectionId: randomUUID(), credentialId: randomUUID(), operationName: "servers.probes.run", issuedAt, expiresAt } : { kind: "rest", authentication: kind, ownerId, executionId, requestId: `request-${randomUUID()}`, issuedAt, expiresAt } + const scope = tool === "bandwidth" ? [source, destination] : [source] + if (identity.kind === "mcp") { + const policy = { id: identity.connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: new Date(expiresAt).toISOString(), scope: { serverIds: scope, operationIds: [identity.operationName], allowGlobal: false } } + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until) VALUES($1,'probe SQL fixture',$2,$3::jsonb,$4::jsonb,'probe-generation',$5)", [identity.connectionId, ownerId, JSON.stringify(["mm:servers:write"]), JSON.stringify(policy), new Date(expiresAt)]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [identity.credentialId, identity.connectionId, randomUUID(), new Date(expiresAt)]) + const planId = randomUUID() + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) VALUES($1,$2,$3,1,1,'probe-generation','{}','{}','executing',$4)", [planId, identity.connectionId, identity.operationName, new Date(expiresAt)]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'running')", [executionId, planId, identity.connectionId, randomUUID()]) + } + const context: ServerProbeJournalContext = { identity, sourceServerId: source, ...(tool === "bandwidth" ? { destinationServerId: destination } : {}), tool } + const binding: ServerProbeJournalBinding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), ownerId, tool, serverIds: scope, transport: identity.kind, ...(identity.kind === "mcp" ? { connectionId: identity.connectionId } : { requestId: identity.requestId, issuedAt }) } + let active = true, authorityCalls = 0 + const deps = { connections, crypto: customCrypto, async assertAuthority(actual: Readonly, proof: string) { authorityCalls++; assert(active, "Synthetic trusted authority revoked"); assert.deepEqual(actual, context); assert.equal(proof, binding.bindingProof) } } + const journal = createSqlServerProbeJournal(context, deps) + const stage = async (instance = journal) => { const native = { ping: "native-ping", traceroute: "native-traceroute", bandwidth: "native-bandwidth", route: "native-route-read", dns: "manager-dns-TXT", mtu: "native-mtu-1500" }[tool]; await instance.intent({ receiptId: binding.receiptId, stageIndex: 1, kind: native, timeoutMs: 1000 }); await instance.received({ receiptId: binding.receiptId, stageIndex: 1, outcome: "received" }) } + const receipt = (count = 1): ServerProbeReceipt => ({ receiptId: binding.receiptId, tool, serverId: source, ...(tool === "bandwidth" ? { destinationServerId: destination } : {}), state: "completed", httpStatus: 200, output: "SYNTHETIC_PRIVATE_MEASUREMENT", data: { kind: tool, value: "SYNTHETIC_PRIVATE_MEASUREMENT" }, attemptedStages: count, completedStages: count, automaticReplay: false, forwardingVerified: false }) + const quarantine = (instance = journal, attemptedStages = 1, completedStages = 0) => instance.quarantine({ ...binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages, completedStages }) + const anchor = async () => (await pool.query("SELECT * FROM server_probe_executions WHERE receipt_id=$1", [binding.receiptId])).rows[0] + return { context, binding, deps, journal, stage, receipt, quarantine, anchor, revoke() { active = false }, restore() { active = true }, calls: () => authorityCalls } + } + // Actual PostgreSQL transactions + synthetic crypto; no DNS/RouterOS callback. + for (const kind of ["jwt", "local", "mcp"] as const) for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"] as const) { + const f = await fixture(kind, tool) + assert.equal(await f.journal.claim(f.binding), "claimed"); await f.stage(); await f.journal.finish(f.receipt()) + const restarted = createSqlServerProbeJournal(f.context, f.deps) + assert.deepEqual(await restarted.read(f.binding.receiptId, f.binding.bindingProof), f.receipt()) + assert.equal(await restarted.claim(f.binding), "exists") + const row = await f.anchor(); assert.equal(row.authentication, kind); assert.equal(row.actor_id, f.context.identity.ownerId) + assert.equal(row.mcp_connection_id === null, kind !== "mcp"); assert(!JSON.stringify(row).includes("SYNTHETIC_PRIVATE_MEASUREMENT")); assert(f.calls() > 0) + await conflict(f.journal.finish(f.receipt())); await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 2, kind: "native-ping", timeoutMs: 1000 })); cases++ + } + { + const f = await fixture("mcp", "bandwidth"), other = createSqlServerProbeJournal(f.context, f.deps) + assert.deepEqual((await Promise.all([f.journal.claim(f.binding), other.claim(f.binding)])).sort(), ["claimed", "exists"]) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE execution_id=$1", [f.context.identity.executionId])).rows[0].n, 1) + await conflict(createSqlServerProbeJournal(f.context, f.deps).claim({ ...f.binding, receiptId: randomUUID() })) + for (const binding of [{ ...f.binding, ownerId: "foreign" }, { ...f.binding, connectionId: randomUUID() }, { ...f.binding, serverIds: [source, foreign] }, { ...f.binding, serverIds: [source] }, { ...f.binding, bindingProof: "b".repeat(64) }]) await conflict(createSqlServerProbeJournal(f.context, f.deps).claim(binding)) + cases++ + } + // SQL authority must reject actual out-of-scope, disabled and revoked rows, + // even when the external callback is permissive and remains live. + for (const mutation of ["scope", "operation", "owner", "credential", "credential-expired", "owner-expired", "observer", "policy-expired", "operation-state"] as const) { + const f = await fixture("mcp", "bandwidth"), id = f.context.identity; assert.equal(id.kind, "mcp"); if (id.kind !== "mcp") throw Error("fixture") + if (mutation === "scope") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}',$2::jsonb) WHERE id=$1", [id.connectionId, JSON.stringify([source])]) + if (mutation === "operation") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,operationIds}','[]') WHERE id=$1", [id.connectionId]) + if (mutation === "owner") await pool.query("UPDATE mcp_connections SET owner_id='foreign' WHERE id=$1", [id.connectionId]) + if (mutation === "credential") await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [id.credentialId]) + if (mutation === "credential-expired") await pool.query("UPDATE mcp_credentials SET expires_at=clock_timestamp()-interval '1 second' WHERE id=$1", [id.credentialId]) + if (mutation === "owner-expired") await pool.query("UPDATE mcp_connections SET owner_valid_until=clock_timestamp()-interval '1 second' WHERE id=$1", [id.connectionId]) + if (mutation === "observer") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{mode}','\"observer\"') WHERE id=$1", [id.connectionId]) + if (mutation === "policy-expired") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{expiresAt}',to_jsonb((clock_timestamp()-interval '1 second')::text)) WHERE id=$1", [id.connectionId]) + if (mutation === "operation-state") await pool.query("UPDATE mcp_operations SET state='applied' WHERE id=$1", [id.executionId]) + await conflict(f.journal.claim(f.binding)); assert.equal(await f.anchor(), undefined) + } + for (const disabled of [source, destination]) { + const f = await fixture("mcp", "bandwidth"); await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [disabled]) + try { await conflict(f.journal.claim(f.binding)) } finally { await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [disabled]) } + } + { + const f = await fixture("mcp", "bandwidth"), peer = await fixture("mcp", "bandwidth") + assert(f.context.identity.kind === "mcp" && peer.context.identity.kind === "mcp") + const id = f.context.identity + for (const context of [ + { ...f.context, sourceServerId: foreign }, + { ...f.context, destinationServerId: foreign }, + { ...f.context, identity: { ...id, executionId: randomUUID() } }, + { ...f.context, identity: { ...id, credentialId: peer.context.identity.credentialId } }, + { ...f.context, identity: { ...id, connectionId: peer.context.identity.connectionId } }, + { ...f.context, identity: { ...id, ownerId: peer.context.identity.ownerId } }, + ] satisfies ServerProbeJournalContext[]) { + const identity = context.identity; assert(identity.kind === "mcp") + const binding = { ...f.binding, receiptId: randomUUID(), ownerId: identity.ownerId, connectionId: identity.connectionId, serverIds: [context.sourceServerId, context.destinationServerId!] } + // Deliberately permissive callback isolates the actual SQL owner/scope join. + await conflict(createSqlServerProbeJournal(context, { ...f.deps, async assertAuthority() {} }).claim(binding)) + } + assert.equal(await f.anchor(), undefined) + } + { + const f = await fixture("mcp"), id = f.context.identity; assert(id.kind === "mcp") + await f.journal.claim(f.binding) + await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [id.credentialId]) + await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 })) + await f.quarantine(f.journal, 0, 0) + await pool.query("UPDATE mcp_credentials SET revoked_at=NULL WHERE id=$1", [id.credentialId]) + assert.equal((await createSqlServerProbeJournal(f.context, f.deps).read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown"); cases++ + } + { + const f = await fixture("local", "mtu"); await f.journal.claim(f.binding) + await conflict(pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms) VALUES($1,2,'native-mtu-1500',1000)", [f.binding.receiptId])) + await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 2, kind: "native-mtu-1492", timeoutMs: 1000 })) + await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-mtu-1492", timeoutMs: 1000 })) + await f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "source-dns-A", timeoutMs: 1000 }) + await conflict(pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms) VALUES($1,2,'native-mtu-1500',1000)", [f.binding.receiptId])) + await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 2, kind: "native-mtu-1500", timeoutMs: 1000 })) + await f.journal.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "source-unavailable" }) + await conflict(f.journal.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "received" })) + for (const [offset, size] of [1500, 1492].entries()) { const stageIndex = offset + 2; await f.journal.intent({ receiptId: f.binding.receiptId, stageIndex, kind: `native-mtu-${size}`, timeoutMs: 25000 }); await f.journal.received({ receiptId: f.binding.receiptId, stageIndex, outcome: "received" }) } + await conflict(f.journal.finish(f.receipt(2))); await f.journal.finish(f.receipt(3)); cases++ + } + { + const f = await fixture(); await f.journal.claim(f.binding) + await conflict(pool.query("UPDATE server_probe_executions SET actor_id='foreign' WHERE receipt_id=$1", [f.binding.receiptId])) + await conflict(pool.query("DELETE FROM server_probe_executions WHERE receipt_id=$1", [f.binding.receiptId])) + await f.stage(); await conflict(pool.query("UPDATE server_probe_execution_stages SET timeout_ms=2 WHERE receipt_id=$1", [f.binding.receiptId])) + await conflict(pool.query("DELETE FROM server_probe_execution_stages WHERE receipt_id=$1", [f.binding.receiptId])) + await f.journal.finish(f.receipt()) + await conflict(pool.query("UPDATE server_probe_executions SET state='claimed' WHERE receipt_id=$1", [f.binding.receiptId])) + await conflict(pool.query("UPDATE server_probe_executions SET receipt_ciphertext='tampered' WHERE receipt_id=$1", [f.binding.receiptId])) + await f.quarantine(f.journal, 1, 1) + await conflict(pool.query("UPDATE server_probe_execution_quarantines SET error_code='CANCELLED' WHERE receipt_id=$1", [f.binding.receiptId])) + await conflict(pool.query("DELETE FROM server_probe_execution_quarantines WHERE receipt_id=$1", [f.binding.receiptId])) + assert.equal((await f.journal.read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown"); cases++ + } + // Corrupt encrypted material through the injected codec, never disable SQL + // immutability triggers merely to manufacture a tampered completed row. + for (const tamper of ["ciphertext", "proof", "envelope"] as const) { + const badCodec: ProbeJournalCrypto = { ...crypto, + async encrypt(value) { if (tamper === "ciphertext") { const bytes = Buffer.from(await crypto.encrypt(value), "base64"); bytes[bytes.length - 1] ^= 1; return bytes.toString("base64") } return crypto.encrypt(value) }, + async decrypt(value) { const plaintext = await crypto.decrypt(value); return tamper === "envelope" ? plaintext.replace("SYNTHETIC_PRIVATE_MEASUREMENT", "forged") : plaintext }, + async proof(domain, value) { return tamper === "proof" && domain === "receipt" ? "b".repeat(64) : crypto.proof(domain, value) }, + } + const f = await fixture("jwt", "ping", badCodec); await f.journal.claim(f.binding); await f.stage(); await f.journal.finish(f.receipt()) + // HMAC mismatch test changes verifier only, retaining the real stored HMAC. + const reader = createSqlServerProbeJournal(f.context, { ...f.deps, crypto: tamper === "proof" ? crypto : badCodec }) + await assert.rejects(reader.read(f.binding.receiptId, f.binding.bindingProof), ServerProbeJournalConflict); cases++ + } + { + const f = await fixture("jwt"); await f.journal.claim(f.binding); f.revoke() + await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 })) + await f.quarantine(f.journal, 0, 0); await conflict(f.journal.read(f.binding.receiptId, f.binding.bindingProof)); f.restore() + assert.equal((await f.journal.read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown") + await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 })); cases++ + } + // COMMIT really reaches PostgreSQL first. The wrapper then drops its ACK. + // Instrument exact commands: no rollback or retry is allowed after COMMIT. + for (const lost of ["claim", "intent", "received", "finish", "quarantine"] as const) { + const f = await fixture("mcp"), statements: string[] = [], destroyed: boolean[] = [] + let armed = false + const dropping: ProbeJournalConnections = { async connect() { const client = await pool.connect(); return { async query(text, values) { statements.push(text); const result = await client.query(text, values); if (text === "COMMIT" && armed) throw new Error("Synthetic lost committed ACK"); return result }, release(destroy) { destroyed.push(!!destroy); client.release(destroy) } } } } + const uncertain = createSqlServerProbeJournal(f.context, { ...f.deps, connections: dropping }) + if (lost !== "claim") { await f.journal.claim(f.binding); assert.equal(await uncertain.claim(f.binding), "exists"); statements.length = 0; destroyed.length = 0 } + if (["received", "finish"].includes(lost)) await f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }) + if (lost === "finish") await f.journal.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "received" }) + armed = true + const work = lost === "claim" ? uncertain.claim(f.binding) : lost === "intent" ? uncertain.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }) : lost === "received" ? uncertain.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "received" }) : lost === "finish" ? uncertain.finish(f.receipt()) : f.quarantine(uncertain, 0, 0) + await assert.rejects(work, ServerProbeJournalOutcomeUnknown) + assert.equal(statements.filter(text => text === "COMMIT").length, 1); assert.equal(statements.filter(text => text === "ROLLBACK").length, 0); assert.deepEqual(destroyed, [true]) + const row = await f.anchor(); assert.equal(row.state, lost === "finish" ? "completed" : "claimed") + const stages = (await pool.query("SELECT * FROM server_probe_execution_stages WHERE receipt_id=$1", [f.binding.receiptId])).rows + assert.equal(stages.length, ["intent", "received", "finish"].includes(lost) ? 1 : 0) + if (lost === "received" || lost === "finish") assert.equal(stages[0].state, "received") + await f.quarantine(f.journal, lost === "finish" || lost === "received" ? 1 : 0, lost === "finish" ? 1 : 0) + const restarted = createSqlServerProbeJournal(f.context, f.deps) + assert.equal(await restarted.claim(f.binding), "exists") + assert.equal((await restarted.read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown") + assert.equal((await f.anchor()).state, lost === "finish" ? "completed" : "claimed", "Quarantine overlay must not rewrite completed receipt") + await conflict(restarted.intent({ receiptId: f.binding.receiptId, stageIndex: stages.length + 1, kind: "native-ping", timeoutMs: 1000 })); cases++ + } + { + const { default: Fastify } = await import("fastify") + const { createServerProbeExecutor } = await import("./server-probe-execution.js") + const { createRestServerProbeAuthority } = await import("./server-probe-authority.js") + const { createServerProbeRestExecution } = await import("./server-probe-rest-execution.js") + let raw: unknown, dispatches = 0, authenticated = false + const actor = { id: "probe-http-owner", email: "synthetic@example.invalid", name: "Synthetic", apps: [], permissions: ["mm:servers:write"], issuedAt: Date.now(), expiresAt: Date.now() + 300000 } + const execution = createServerProbeRestExecution({ + authority: (user, request) => createRestServerProbeAuthority(user, request, { portal: async actual => { assert.equal(actual.id, actor.id) }, query: async (sql, values) => (await pool.query(sql, values)).rows }), + executor: access => createServerProbeExecutor({ ...access, + async getServer(id) { return [source, destination].includes(id) ? { id, enabled: true, host: "127.0.0.1", port: 9, username: "SYNTHETIC_USER", password: "SYNTHETIC_PASSWORD", useSsl: false, verifySsl: true, routingEndpointEpoch: "11111111-1111-4111-8111-111111111111" } : undefined }, + async findDestination() { return { id: destination } }, + seal: crypto.encrypt, open: crypto.decrypt, privateProof: crypto.proof, + async post(_server, _path, _body, _timeout, signal, fence) { signal.throwIfAborted(); await fence(); dispatches++; return structuredClone(raw) }, + async routes(_server, _timeout, signal, fence) { signal.throwIfAborted(); await fence(); dispatches++; return structuredClone(raw) }, + async dns(_name, _type, signal, fence) { signal.throwIfAborted(); await fence(); dispatches++; return structuredClone(raw) }, + }), + journal: (context, deps) => createSqlServerProbeJournal(context, { ...deps, connections, crypto }), + }) + const app = Fastify() + app.post<{ Params: { id: string } }>("/test/probes/:id", async (request, reply) => { + const result = await execution(request.params.id, request.body, { actor: authenticated ? actor : undefined, requestId: request.id, acceptedAt: Date.now(), authRequired: authenticated }) + return reply.code(result.statusCode).send(result.body) + }) + try { + const address = await app.listen({ host: "127.0.0.1", port: 0 }) + for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) { + raw = tool === "traceroute" ? [{ address: "127.0.0.1", loss: "0%", last: "1ms", avg: "1ms" }] + : tool === "bandwidth" ? [{ "tx-current": "1", "rx-current": "2" }] + : tool === "dns" ? ["127.0.0.1"] + : tool === "route" ? [{ ".id": "*1", "dst-address": "127.0.0.0/8", active: "true", gateway: "ether1", distance: "1" }] + : tool === "mtu" ? [{ seq: "0", status: "timeout" }] + : [{ seq: "0", ttl: "64", time: "1ms" }] + const response = await fetch(`${address}/test/probes/${source}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tool, target: "127.0.0.1", bwRemoteAddress: "127.0.0.1", dstServerId: destination, callerIdentity: "ignored" }) }) + const body = await response.json() as { output?: string } + assert.equal(response.status, 200, JSON.stringify(body)); assert.equal(typeof body.output, "string"); assert(!JSON.stringify(body).includes("SYNTHETIC_PASSWORD")); cases++ + } + authenticated = true; raw = [{ seq: "0", ttl: "64", time: "1ms" }] + const response = await fetch(`${address}/test/probes/${source}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tool: "ping", target: "127.0.0.1" }) }) + assert.equal(response.status, 200, await response.text()); cases++ + assert.equal(dispatches, 16, "Five simple tools, ten MTU requests, one JWT ping") + const rows = (await pool.query("SELECT authentication,mcp_connection_id,mcp_credential_id FROM server_probe_executions WHERE actor_id=$1", [actor.id])).rows + assert.equal(rows.length, 1); assert.equal(rows[0].authentication, "jwt"); assert.equal(rows[0].mcp_connection_id, null); assert.equal(rows[0].mcp_credential_id, null) + const locals = (await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE actor_id='local-operator' AND authentication='local'")).rows[0].n + assert.equal(locals, 6) + } finally { await app.close() } + } + { + const { createServerProbeJournalHistoryReader } = await import("./server-probe-journal-history.js") + const f = await fixture("mcp", "bandwidth"), id = f.context.identity; assert(id.kind === "mcp") + await f.journal.claim(f.binding); await f.stage(); await f.journal.finish(f.receipt()) + const original = { context: f.context, receiptId: f.binding.receiptId, bindingProof: f.binding.bindingProof } + // Keep SQL/envelope identity original while execution-time authority is + // expired in its own clock. Historical admission is an independent fence. + await assert.rejects(createSqlServerProbeJournal(f.context, { ...f.deps, now: () => id.expiresAt + 1 }).read(f.binding.receiptId, f.binding.bindingProof)) + let current = true, fences = 0 + const history = createServerProbeJournalHistoryReader({ crypto, async assertReadAuthority(binding) { fences++; assert(current); assert.deepEqual(binding, original) } }) + assert.deepEqual(await history.read(original), f.receipt()); assert(fences >= 6); cases++ + current = false; await conflict(history.read(original)); current = true + await f.quarantine(f.journal, 1, 1); await conflict(history.read(original)) + } + // The actual 0036 migration and request/watch implementation share this + // disposable database. Stop requests never delete or rewrite probe evidence. + { + const { createDiagnosticCancellationRequest, withDiagnosticCancellation } = await import("../modules/mcp/diagnostic-cancellation.js") + const { McpError } = await import("../modules/mcp/security.js") + const f = await fixture("mcp", "bandwidth"), id = f.context.identity + assert(id.kind === "mcp") + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=(SELECT plan_id FROM mcp_operations WHERE id=$1)", [id.executionId, JSON.stringify({ sealed: await crypto.encrypt(JSON.stringify({ target: { scope: "servers", serverIds: [source, destination] } })) })]) + const principal = { connectionId: id.connectionId, credentialId: id.credentialId, credentialExpiresAt: new Date(id.expiresAt).toISOString() } + const request = createDiagnosticCancellationRequest({ + connection: async (actual, client) => { + const row = (await (client ?? pool).query("SELECT * FROM mcp_connections WHERE id=$1", [actual.connectionId])).rows[0] + assert(row); return row + }, + open: crypto.decrypt, + }) + const context = { operationId: id.executionId, phase: "forward" as const, stepIndex: 0 } + // Production SQL read runs both before and after the work. + assert.equal(await withDiagnosticCancellation(context, async signal => { assert.equal(signal.aborted, false); return "running" }, { authorize: async () => {} }), "running"); cases++ + assert.equal((await request(principal, id.executionId)).cancelRequested, true) + assert.equal((await request(principal, id.executionId)).cancelRequested, true) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='diagnostic.cancel-requested' AND connection_id=$1 AND payload->>'operationId'=$2", [id.connectionId, id.executionId])).rows[0].n, 1, "Repeated cancellation creates exactly one audit event") + const row = (await pool.query("SELECT * FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [id.executionId])).rows[0] + assert.equal(row.connection_id, id.connectionId); assert.equal(row.actor_id, id.ownerId) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [id.executionId])).rows[0].n, 1); cases++ + let starts = 0 + await assert.rejects(withDiagnosticCancellation(context, async () => { starts++ }, { authorize: async () => {} }), error => error instanceof McpError && error.code === "READ_CANCELLED") + assert.equal(starts, 0); cases++ + await conflict(pool.query("UPDATE mcp_diagnostic_cancellations SET actor_id='foreign' WHERE operation_id=$1", [id.executionId])) + await conflict(pool.query("DELETE FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [id.executionId])) + await conflict(pool.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3)", [randomUUID(), id.connectionId, id.ownerId])) + const other = await fixture("mcp"), foreignIdentity = other.context.identity; assert(foreignIdentity.kind === "mcp") + await conflict(request({ connectionId: foreignIdentity.connectionId, credentialId: foreignIdentity.credentialId, credentialExpiresAt: new Date(foreignIdentity.expiresAt).toISOString() }, id.executionId)) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{mode}','\"observer\"') WHERE id=$1", [id.connectionId]) + await conflict(request(principal, id.executionId)) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{mode}','\"approval\"') WHERE id=$1", [id.connectionId]) + await pool.query("UPDATE mcp_operations SET state='applied' WHERE id=$1", [id.executionId]) + assert.equal((await request(principal, id.executionId)).terminal, true); cases++ + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0036_diagnostic_cancellations'")).rows[0].n, 1) + } + // Full six-tool runner, paged DNS and both cancellation lanes now execute + // against the real public v2 registry in --probe-storage-fresh. This legacy + // schema profile must not replace that registered service with a fake one. + const authority = await (await import("../modules/mcp/routing-profile-authority.sql-helper.js")).acceptRoutingProfileAuthoritySql() + console.log("routing profile persistent authority actual SQL acceptance:", JSON.stringify(authority)) + const partitionLease = await (await import("./server-partition-maintenance-lease.sql-helper.js")).acceptServerPartitionMaintenanceLeaseSql() + console.log("partition writer lease actual SQL boundary acceptance:", JSON.stringify(partitionLease)) + const pollJournal = await (await import("./server-poll-journal.sql-helper.js")).acceptServerPollJournalSql() + console.log("server poll journal actual SQL proposal acceptance:", JSON.stringify({ cases: pollJournal, productionRegistration: false })) + const writerAdmission = await (await import("./partition-write-transaction.sql-helper.js")).acceptPartitionWriteTransactionSql() + console.log("actual same-backend partition writer admission:", JSON.stringify(writerAdmission)) + const legacyRebuild = await (await import("./traffic-flow-facts-rebuild-cli.sql-helper.js")).acceptLegacyFlowFactsRebuildSql() + console.log("actual legacy CLI safe rebuild integration:", JSON.stringify(legacyRebuild)) + const persistentIntegration = await (await import("../modules/mcp/routing-profile-authority-integration.sql-helper.js")).acceptRoutingProfileAuthorityIntegrationSql() + console.log("persistent routing authority actual dispatcher/HTTP integration:", JSON.stringify(persistentIntegration)) + await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [serverIds]) + console.log(`server probe journal actual SQL acceptance: ${cases} cases passed; synthetic crypto, no native/DNS I/O`) +} finally { + // Rows intentionally survive until runner destroys its entire ephemeral DB: + // deleting append-only evidence here would invalidate the tested invariant. + await closePool() +} diff --git a/backend/src/services/server-probe-journal.test.ts b/backend/src/services/server-probe-journal.test.ts new file mode 100644 index 00000000..37ce5a43 --- /dev/null +++ b/backend/src/services/server-probe-journal.test.ts @@ -0,0 +1,171 @@ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, createHmac, randomBytes, randomUUID } from "node:crypto" +import { createSqlServerProbeJournal, ServerProbeJournalConflict, ServerProbeJournalOutcomeUnknown, type ProbeJournalConnections, type ProbeJournalCrypto, type ServerProbeJournalContext } from "./server-probe-journal.js" +import { SERVER_PROBE_JOURNAL_DDL } from "./server-probe-journal-schema.js" +import type { ServerProbeJournalBinding, ServerProbeReceipt } from "./server-probe-execution.js" + +type Row = Record +interface State { anchors: Row[]; stages: Row[]; quarantines: Row[] } +function fixture(kind: "mcp" | "jwt" | "local" = "jwt", bandwidth = false) { + let state: State = { anchors: [], stages: [], quarantines: [] }, live = true, sqlOwner = true, enabled = true, pending: string | undefined, queued = Promise.resolve(), guardCalls = 0, connections = 0 + const released: boolean[] = [], queries: string[] = [], key = randomBytes(32) + const context: ServerProbeJournalContext = { identity: kind === "mcp" ? { kind: "mcp", executionId: randomUUID(), ownerId: "real-owner", connectionId: randomUUID(), credentialId: randomUUID(), operationName: "servers.probes.run", issuedAt: 1000, expiresAt: 2000000 } : { kind: "rest", executionId: randomUUID(), ownerId: kind === "local" ? "local-request-actor" : "real-jwt-sub", authentication: kind, requestId: "actual-http-request-17", issuedAt: 1000, expiresAt: 2000000 }, sourceServerId: 1, ...(bandwidth ? { destinationServerId: 2 } : {}), tool: bandwidth ? "bandwidth" : "ping" } + const metadata: ServerProbeJournalBinding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), ownerId: context.identity.ownerId, transport: context.identity.kind, ...(context.identity.kind === "mcp" ? { connectionId: context.identity.connectionId } : { requestId: context.identity.requestId, issuedAt: context.identity.issuedAt }), serverIds: bandwidth ? [1, 2] : [1], tool: context.tool } + const crypto: ProbeJournalCrypto = { + async encrypt(value) { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv), body = Buffer.concat([cipher.update(value), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), body]).toString("base64") }, + async decrypt(value) { const data = Buffer.from(value, "base64"), decipher = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)); decipher.setAuthTag(data.subarray(12, 28)); return Buffer.concat([decipher.update(data.subarray(28)), decipher.final()]).toString() }, + async proof(domain, value) { return createHmac("sha256", key).update(domain).update(JSON.stringify(value)).digest("hex") }, + } + const pool: ProbeJournalConnections = { async connect() { + connections++; let current: State | undefined, unlock: (() => void) | undefined, lastMutation: string | undefined + return { async query(text, values = []) { + queries.push(text) + if (text === "BEGIN") { if (pending === "BEGIN") { pending = undefined; throw Error("NEVER_SQL_SECRET") } return { rows: [] } } + if (text.includes("probe-journal:lock")) { + if (!unlock) { const previous = queued; queued = new Promise(resolve => { unlock = resolve }); await previous; current = structuredClone(state) } + return { rows: [] } + } + if (text === "COMMIT") { if (current) state = structuredClone(current); if (pending !== undefined && pending === lastMutation) { pending = undefined; throw Error("NEVER_LOST_COMMIT_ACK") } return { rows: [] } } + if (text === "ROLLBACK") { current = undefined; return { rows: [] } } + assert.ok(current, "every query uses owned transaction + advisory locks") + const result = (rows: Row[]) => ({ rows: structuredClone(rows) }) + if (text.includes("probe-journal:server_probe_executions")) return result(current.anchors.filter(row => row.receipt_id === values[0] || row.identity_kind === values[1] && row.execution_id === values[2])) + if (text.includes("probe-journal:server_probe_execution_quarantines")) return result(current.quarantines.filter(row => row.receipt_id === values[0] || row.identity_kind === values[1] && row.execution_id === values[2])) + if (text.includes("probe-journal:servers")) return result(enabled ? (values[0] as number[]).map(id => ({ id })) : []) + if (text.includes("probe-journal:owner")) { + assert.equal(context.identity.kind, "mcp"); const identity = context.identity as Extract + assert.deepEqual(values, [identity.executionId, identity.connectionId, identity.ownerId, identity.credentialId, JSON.stringify(metadata.serverIds), identity.operationName]) + assert.match(text, /scope,operationIds/); return result(sqlOwner ? [{ id: identity.executionId }] : []) + } + if (text.includes("probe-journal:claim")) { + if (current.anchors.some(row => row.receipt_id === values[0] || row.identity_kind === values[2] && row.execution_id === values[3])) return result([]) + const fields = ["receipt_id", "binding_proof", "identity_kind", "execution_id", "actor_id", "authentication", "mcp_connection_id", "mcp_credential_id", "request_id", "authority_issued_at", "authority_expires_at", "source_server_id", "destination_server_id", "tool"] + const row: Row = Object.fromEntries(fields.map((field, index) => [field, values[index]])); row.state = "claimed"; current.anchors.push(row); lastMutation = "claim"; return result([{ receipt_id: row.receipt_id }]) + } + if (text.includes("probe-journal:stages")) return result(current.stages.filter(row => row.receipt_id === values[0]).sort((a, b) => Number(a.stage_index) - Number(b.stage_index))) + if (text.includes("probe-journal:intent")) { + if (current.stages.some(row => row.receipt_id === values[0] && row.stage_index === values[1])) throw Error("duplicate native intent") + current.stages.push({ receipt_id: values[0], stage_index: values[1], kind: values[2], timeout_ms: values[3], state: "intent" }); lastMutation = "intent"; return result([{ stage_index: values[1] }]) + } + if (text.includes("probe-journal:received")) { + const row = current.stages.find(row => row.receipt_id === values[0] && row.stage_index === values[1] && row.state === "intent"); if (!row) return result([]) + row.state = "received"; row.outcome = values[2]; lastMutation = "received"; return result([{ stage_index: row.stage_index }]) + } + if (text.includes("probe-journal:finish")) { + const row = current.anchors.find(row => row.receipt_id === values[0] && row.state === "claimed"); if (!row) return result([]) + Object.assign(row, { state: values[1], attempted_stages: values[2], completed_stages: values[3], receipt_ciphertext: values[4], receipt_proof: values[5] }); lastMutation = "finish"; return result([{ receipt_id: values[0] }]) + } + if (text.includes("probe-journal:quarantine")) { + if (current.quarantines.some(row => row.receipt_id === values[0] || row.identity_kind === values[2] && row.execution_id === values[3])) return result([]) + const fields = ["receipt_id", "binding_proof", "identity_kind", "execution_id", "identity_proof", "error_code", "attempted_stages", "completed_stages"] + current.quarantines.push(Object.fromEntries(fields.map((field, index) => [field, values[index]]))); lastMutation = "quarantine"; return result([{ receipt_id: values[0] }]) + } + throw Error("unexpected fake SQL") + }, release(destroy) { released.push(!!destroy); unlock?.() } } + } } + const deps = { connections: pool, crypto, now: () => 1000000, async assertAuthority() { guardCalls++; if (!live) throw Error("NEVER_REVOKED_SECRET") } } + const journal = createSqlServerProbeJournal(context, deps) + const receipt = (): ServerProbeReceipt => ({ receiptId: metadata.receiptId, tool: context.tool, serverId: 1, ...(bandwidth ? { destinationServerId: 2 } : {}), state: "completed", httpStatus: 200, output: "NEVER_PRIVATE_MEASUREMENT", data: { kind: context.tool, known: "NEVER_PRIVATE_MEASUREMENT" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false }) + const stage = async () => { await journal.intent({ receiptId: metadata.receiptId, stageIndex: 1, kind: bandwidth ? "native-bandwidth" : "native-ping", timeoutMs: 1000 }); await journal.received({ receiptId: metadata.receiptId, stageIndex: 1, outcome: "received" }) } + const quarantine = () => journal.quarantine({ ...metadata, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 1, completedStages: 0 }) + return { journal, context, metadata, receipt, stage, quarantine, queries, crypto, deps, released, + state: () => state, connections: () => connections, guardCalls: () => guardCalls, revoke() { live = false }, restore() { live = true }, denySqlOwner() { sqlOwner = false }, disableServer() { enabled = false }, lose(which: string) { pending = which }, + } +} +let cases = 0 +async function refused(work: Promise) { await assert.rejects(work); cases++ } +for (const authentication of ["mcp", "jwt", "local"] as const) { + const f = fixture(authentication, true) + assert.equal(await f.journal.claim(f.metadata), "claimed"); await f.stage(); await f.journal.finish(f.receipt()) + assert.deepEqual(await f.journal.read(f.metadata.receiptId, f.metadata.bindingProof), f.receipt()) + assert.equal(await f.journal.claim(f.metadata), "exists") + assert.equal(f.state().anchors.length, 1); const anchor = f.state().anchors[0] + assert.equal(anchor.authentication, authentication); assert.equal(anchor.actor_id, f.context.identity.ownerId) + assert.equal(anchor.mcp_connection_id === null, authentication !== "mcp") + assert.equal(f.queries.some(query => query.includes("probe-journal:owner")), authentication === "mcp") + assert(!JSON.stringify(f.state()).includes("NEVER_PRIVATE_MEASUREMENT")) + const snapshot = structuredClone(f.state()); await refused(f.journal.finish(f.receipt())); assert.deepEqual(f.state(), snapshot) + await refused(f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 2, kind: "native-bandwidth", timeoutMs: 1000 })); assert.deepEqual(f.state(), snapshot) + assert(f.connections() >= 7); cases++ +} +{ + const f = fixture(), second = createSqlServerProbeJournal(f.context, f.deps) + const results = await Promise.all([f.journal.claim(f.metadata), second.claim(f.metadata)]) + assert.deepEqual(results.sort(), ["claimed", "exists"]); assert.equal(f.state().anchors.length, 1); cases++ + await refused(createSqlServerProbeJournal(f.context, f.deps).claim({ ...f.metadata, receiptId: randomUUID(), bindingProof: "b".repeat(64) })) +} +for (const mutate of [ + (value: ServerProbeJournalBinding) => { value.ownerId = "foreign" }, + (value: ServerProbeJournalBinding) => { value.transport = "mcp"; value.connectionId = randomUUID() }, + (value: ServerProbeJournalBinding) => { value.requestId = "foreign-http-request" }, + (value: ServerProbeJournalBinding) => { value.issuedAt = 1 }, + (value: ServerProbeJournalBinding) => { value.serverIds = [1] }, + (value: ServerProbeJournalBinding) => { value.serverIds = [2, 1] }, + (value: ServerProbeJournalBinding) => { value.tool = "dns" }, +]) { const f = fixture("jwt", true), value = structuredClone(f.metadata); mutate(value); await refused(f.journal.claim(value)); assert.equal(f.state().anchors.length, 0) } +{ + const f = fixture("mcp"); f.denySqlOwner(); await refused(f.journal.claim(f.metadata)); assert.equal(f.state().anchors.length, 0) +} +{ + const f = fixture(); f.disableServer(); await refused(f.journal.claim(f.metadata)); assert.equal(f.state().anchors.length, 0) +} +{ + const f = fixture(); f.revoke(); await refused(f.journal.claim(f.metadata)); assert.equal(f.connections(), 0) +} +for (const lost of ["BEGIN", "claim", "intent", "received", "finish"]) { + const f = fixture() + if (["BEGIN", "claim"].includes(lost)) { f.lose(lost); await assert.rejects(f.journal.claim(f.metadata), ServerProbeJournalOutcomeUnknown) } + else { + await f.journal.claim(f.metadata) + if (lost === "intent") { f.lose(lost); await assert.rejects(f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }), ServerProbeJournalOutcomeUnknown) } + else if (lost === "received") { await f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }); f.lose(lost); await assert.rejects(f.journal.received({ receiptId: f.metadata.receiptId, stageIndex: 1, outcome: "received" }), ServerProbeJournalOutcomeUnknown) } + else { await f.stage(); f.lose(lost); await assert.rejects(f.journal.finish(f.receipt()), ServerProbeJournalOutcomeUnknown) } + } + assert(f.released.includes(true)); const oldAnchor = structuredClone(f.state().anchors) + f.revoke(); await f.quarantine(); f.restore() + assert.deepEqual(f.state().anchors, oldAnchor, "quarantine never rewrites immutable terminal receipts") + assert.equal((await f.journal.read(f.metadata.receiptId, f.metadata.bindingProof))?.state, "unknown") + assert.equal(await createSqlServerProbeJournal(f.context, f.deps).claim(f.metadata), "exists") + await refused(f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 2, kind: "native-ping", timeoutMs: 1000 })) + assert.equal(f.state().quarantines.length, 1); await f.quarantine(); assert.equal(f.state().quarantines.length, 1); cases++ +} +{ + const f = fixture(); await f.journal.claim(f.metadata) + await refused(f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 2, kind: "native-ping", timeoutMs: 1000 })) + await refused(f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 1, kind: "native-bandwidth", timeoutMs: 1000 })) + await refused(f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 120001 })) + await f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 1, kind: "source-dns-A", timeoutMs: 1000 }) + await refused(f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 2, kind: "native-ping", timeoutMs: 1000 })) + await f.journal.received({ receiptId: f.metadata.receiptId, stageIndex: 1, outcome: "source-unavailable" }) + await refused(f.journal.received({ receiptId: f.metadata.receiptId, stageIndex: 1, outcome: "received" })) + await f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 2, kind: "native-ping", timeoutMs: 1000 }) + await refused(f.journal.received({ receiptId: f.metadata.receiptId, stageIndex: 2, outcome: "source-unavailable" })) +} +for (const mutate of [ + (receipt: ServerProbeReceipt) => { receipt.receiptId = randomUUID() }, + (receipt: ServerProbeReceipt) => { receipt.serverId = 99 }, + (receipt: ServerProbeReceipt) => { receipt.destinationServerId = 99 }, + (receipt: ServerProbeReceipt) => { receipt.completedStages = 2 }, + (receipt: ServerProbeReceipt) => { receipt.attemptedStages = 0.5 }, + (receipt: ServerProbeReceipt) => { receipt.output = "x".repeat(131073) }, + (receipt: ServerProbeReceipt) => { receipt.data = { kind: "ping", value: Number.NaN } }, +]) { const f = fixture(); await f.journal.claim(f.metadata); await f.stage(); const receipt = f.receipt(); mutate(receipt); await refused(f.journal.finish(receipt)); assert.equal(f.state().anchors[0].state, "claimed") } +{ + const f = fixture(); await f.journal.claim(f.metadata); await f.stage(); await f.journal.finish(f.receipt()) + f.state().anchors[0].receipt_proof = "f".repeat(64); await refused(f.journal.read(f.metadata.receiptId, f.metadata.bindingProof)) +} +{ + const f = fixture(); await f.journal.claim(f.metadata); await f.stage(); await f.journal.finish(f.receipt()) + f.state().anchors[0].actor_id = "foreign"; await refused(f.journal.read(f.metadata.receiptId, f.metadata.bindingProof)) +} +{ + const f = fixture(); await f.journal.claim(f.metadata); await f.journal.intent({ receiptId: f.metadata.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }) + const receipt = { ...f.receipt(), state: "cancelled" as const, httpStatus: 502, output: undefined, data: undefined, errorCode: "CANCELLED", attemptedStages: 0, completedStages: 0 } + await f.journal.finish(receipt); assert.equal((await f.journal.read(f.metadata.receiptId, f.metadata.bindingProof))?.state, "cancelled"); cases++ +} +assert.match(SERVER_PROBE_JOURNAL_DDL, /UNIQUE\(identity_kind,execution_id\)/) +assert.match(SERVER_PROBE_JOURNAL_DDL, /terminal receipt is immutable/) +assert.match(SERVER_PROBE_JOURNAL_DDL, /quarantine is immutable/) +assert.doesNotMatch(SERVER_PROBE_JOURNAL_DDL, /ON DELETE CASCADE/) +assert.ok(ServerProbeJournalConflict) +console.log(`Server probe SQL journal: ${cases} injected transactional cases passed; MCP/JWT/local identities, single CAS, ordered stages, encrypted immutable receipts, append-only lost-ACK quarantine; no SQL/network executed`) diff --git a/backend/src/services/server-probe-journal.ts b/backend/src/services/server-probe-journal.ts new file mode 100644 index 00000000..85153231 --- /dev/null +++ b/backend/src/services/server-probe-journal.ts @@ -0,0 +1,227 @@ +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import type { ServerProbeJournal, ServerProbeJournalBinding, ServerProbeReceipt } from "./server-probe-execution.js" +import { ProbePayloadExpired, ProbePayloadUnavailable, type ServerProbeStorageJournalBoundary } from "./server-probe-storage-journal-adapter.js" + +const uuid = z.string().uuid(), hash = z.string().regex(/^[a-f0-9]{64}$/), toolSchema = z.enum(["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) +const actor = z.string().min(1).max(200).refine(value => value.trim() === value && !/[\x00-\x1f\x7f]/.test(value)) +const baseIdentity = { ownerId: actor, executionId: uuid, issuedAt: z.number().int().nonnegative().safe(), expiresAt: z.number().int().positive().safe() } +const identitySchema = z.discriminatedUnion("kind", [ + z.object({ ...baseIdentity, kind: z.literal("mcp"), connectionId: uuid, credentialId: uuid, operationName: z.string().regex(/^[a-z][a-z0-9_.]{0,199}$/) }).strict(), + z.object({ ...baseIdentity, kind: z.literal("rest"), requestId: actor, authentication: z.enum(["jwt", "local"]) }).strict(), +]) +/** MCP executionId is the ACTUAL mcp_operations.id, never a made-up owner or + * plan. REST executionId belongs to a trusted request execution; requestId and + * issuedAt come from the real JWT/local accepted request, never request JSON. + * Local identity may be selected only by the AUTH_REQUIRED=false host adapter. */ +export type ServerProbeJournalIdentity = z.infer +export interface ServerProbeJournalContext { identity: ServerProbeJournalIdentity; sourceServerId: number; destinationServerId?: number; tool: z.infer } +export interface ProbeJournalSql { query(text: string, values?: unknown[]): Promise<{ rows: Record[] }> } +export interface ProbeJournalConnection extends ProbeJournalSql { release(destroy?: boolean): void } +export interface ProbeJournalConnections { connect(): Promise } +export interface ProbeJournalCrypto { encrypt(value: string): Promise; decrypt(value: string): Promise; proof(domain: string, value: unknown): Promise } +export interface ServerProbeJournalDependencies { + /** Fresh trusted owner/credential/policy/protections + full source/destination + * epoch/credential checks for this real execution. Quarantine intentionally + * bypasses revocable access so loss after revocation remains durable. */ + assertAuthority(context: Readonly, bindingProof: string): Promise + connections?: ProbeJournalConnections; crypto?: ProbeJournalCrypto; now?: () => number + /** Explicit opt-in v2 schema only. Default journal/0035 remain unchanged. */ + storageBoundary?: ServerProbeStorageJournalBoundary +} +const ownedConnections: ProbeJournalConnections = { async connect() { return (await import("../db/index.js")).pool.connect() } } +const privateCrypto: ProbeJournalCrypto = { + async encrypt(value) { return (await import("../modules/mcp/security.js")).encryptSecret(value) }, + async decrypt(value) { return (await import("../modules/mcp/security.js")).decryptSecret(value) }, + async proof(domain, value) { const security = await import("../modules/mcp/security.js"); return security.privateSecretFingerprint(security.fingerprint(value), "server-probe-journal-v1:" + domain) }, +} +export class ServerProbeJournalConflict extends Error { readonly automaticReplay = false; constructor() { super("Probe journal refused: execution, binding, authority or immutable state mismatch; no replay") } } +export class ServerProbeJournalOutcomeUnknown extends Error { readonly automaticReplay = false; readonly requiresDurableQuarantine = true; constructor() { super("Probe journal commit outcome unknown; durable quarantine required, no retry or dispatch") } } +function must(condition: unknown): asserts condition { if (!condition) throw new ServerProbeJournalConflict() } +const counts = z.object({ attemptedStages: z.number().int().min(0).max(11), completedStages: z.number().int().min(0).max(11) }).refine(value => value.completedStages <= value.attemptedStages) +const receiptSchema = z.object({ receiptId: uuid, tool: toolSchema, serverId: z.number().int().positive().safe(), destinationServerId: z.number().int().positive().safe().optional(), state: z.enum(["completed", "failed", "cancelled", "unknown"]), httpStatus: z.number().int().min(200).max(599), output: z.string().max(131072).optional(), data: z.record(z.string(), z.unknown()).optional(), errorCode: z.string().regex(/^[A-Z_]{1,64}$/).optional(), attemptedStages: z.number().int().min(0).max(11), completedStages: z.number().int().min(0).max(11), automaticReplay: z.literal(false), forwardingVerified: z.literal(false) }).strict() +const failureCodes = new Set(["INVALID_INPUT", "NOT_FOUND", "DESTINATION_UNAVAILABLE", "SOURCE_UNAVAILABLE", "PERMISSION_DENIED", "STATE_CONFLICT", "CANCELLED", "JOURNAL_UNKNOWN", "REPLAY_FORBIDDEN", "INVALID_ADAPTER", "INVALID_BINDING", "UPSTREAM_UNAVAILABLE", "MEASUREMENT_UNKNOWN", "RECEIPT_INVALID", "UNCLASSIFIED_FAILURE"]) +const sizes = [1500, 1492, 1480, 1476, 1472, 1468, 1400, 1280, 1024, 576] +interface Stage extends Record { stage_index: number; kind: string; timeout_ms: number; state: "intent" | "received"; outcome?: string } + +/** Protocol parsers shared with internal legacy inspection. Parsing is not an + * execution grant, current authorization or database-source attestation. */ +export function parseServerProbeJournalContext(rawContext: ServerProbeJournalContext): ServerProbeJournalContext { + const identity = identitySchema.parse(structuredClone(rawContext.identity)), context: ServerProbeJournalContext = { identity, sourceServerId: z.number().int().positive().max(2147483647).parse(rawContext.sourceServerId), ...(rawContext.destinationServerId !== undefined ? { destinationServerId: z.number().int().positive().max(2147483647).parse(rawContext.destinationServerId) } : {}), tool: toolSchema.parse(rawContext.tool) } + must((context.tool === "bandwidth") === (context.destinationServerId !== undefined)) + return context +} +export function validateServerProbeJournalReceipt(context: ServerProbeJournalContext, receiptId: string, value: ServerProbeReceipt) { + const receipt = receiptSchema.parse(structuredClone(value)); counts.parse(receipt) + const json = (value: unknown, depth = 0): void => { must(depth <= 16); if (value === undefined || value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return; must(value && typeof value === "object" && (Array.isArray(value) || Object.getPrototypeOf(value) === Object.prototype)); for (const child of Object.values(value)) json(child, depth + 1) } + json(receipt) + must(receipt.receiptId === receiptId && receipt.tool === context.tool && receipt.serverId === context.sourceServerId && receipt.destinationServerId === context.destinationServerId && receipt.state !== "unknown" && Buffer.byteLength(JSON.stringify(receipt)) <= 131072) + if (receipt.state === "completed") must(receipt.httpStatus === 200 && receipt.output !== undefined && receipt.data?.kind === context.tool && receipt.errorCode === undefined && receipt.attemptedStages >= 1 && receipt.attemptedStages === receipt.completedStages) + else must(receipt.httpStatus >= 400 && receipt.output === undefined && receipt.data === undefined && !!receipt.errorCode && failureCodes.has(receipt.errorCode)) + return receipt +} + +/** Own a fresh connection and transaction for EVERY durable method. Never use + * a caller's transaction: its rollback must not resurrect a spent attempt. */ +export function createSqlServerProbeJournal(rawContext: ServerProbeJournalContext, deps: ServerProbeJournalDependencies): ServerProbeJournal { + const context = parseServerProbeJournalContext(rawContext), identity = context.identity + must(typeof deps.assertAuthority === "function") + deps.storageBoundary?.assertContext(context) + const scope = [...new Set([context.sourceServerId, ...(context.destinationServerId !== undefined ? [context.destinationServerId] : [])])], codec = deps.crypto ?? privateCrypto, now = deps.now ?? Date.now + let pinned: { receiptId: string; bindingProof: string } | undefined + const pin = (receiptId: string, bindingProof: string) => { uuid.parse(receiptId); hash.parse(bindingProof); if (pinned) must(pinned.receiptId === receiptId && pinned.bindingProof === bindingProof); else pinned = { receiptId, bindingProof }; return pinned } + const metadata = (value: ServerProbeJournalBinding) => { + must(value.ownerId === identity.ownerId && value.tool === context.tool && isDeepStrictEqual(value.serverIds, scope) && (value.transport ?? "mcp") === identity.kind) + if (identity.kind === "mcp") must(value.connectionId === identity.connectionId && value.requestId === undefined && (value.issuedAt === undefined || value.issuedAt === identity.issuedAt)) + else must(value.connectionId === undefined && value.requestId === identity.requestId && value.issuedAt === identity.issuedAt) + return pin(value.receiptId, value.bindingProof) + } + const guard = async (bindingProof: string) => { must(now() < identity.expiresAt); await deps.assertAuthority(structuredClone(context), bindingProof); must(now() < identity.expiresAt) } + const transaction = async (work: (sql: ProbeJournalSql) => Promise, operation: "claim" | "publish" | "intent" | "received" | "read" | "quarantine" = "read", payload?: { ciphertext: string; proof: string }): Promise => { + if (deps.storageBoundary) { + const locked = async (sql: ProbeJournalSql) => { + await sql.query("/* probe-journal:lock */ SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [`server-probe-journal-v1:${identity.kind}:${identity.executionId}`]) + await sql.query("/* probe-journal:lock */ SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [`server-probe-journal-receipt-v1:${pinned!.receiptId}`]) + return work(sql) + } + if (operation === "claim") return deps.storageBoundary.claim(pinned!, locked) + if (operation === "publish") { must(payload); return deps.storageBoundary.publish(pinned!, payload.ciphertext, payload.proof, locked) } + return deps.storageBoundary.transaction(pinned!, operation, locked) + } + let connection: ProbeJournalConnection + try { connection = await (deps.connections ?? ownedConnections).connect() } catch { throw new ServerProbeJournalOutcomeUnknown() } + let beginSent = false, begun = false, commitSent = false, committed = false, destroy = false + try { + beginSent = true; await connection.query("BEGIN"); begun = true + await connection.query("/* probe-journal:lock */ SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [`server-probe-journal-v1:${identity.kind}:${identity.executionId}`]) + await connection.query("/* probe-journal:lock */ SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [`server-probe-journal-receipt-v1:${pinned!.receiptId}`]) + const result = await work(connection) + commitSent = true; await connection.query("COMMIT"); committed = true + return result + } catch (error) { + if (beginSent && !begun || commitSent && !committed) { destroy = true; throw new ServerProbeJournalOutcomeUnknown() } + if (begun && !committed) { try { await connection.query("ROLLBACK") } catch { destroy = true } } + throw error instanceof ServerProbeJournalConflict ? error : new ServerProbeJournalConflict() + } finally { connection.release(destroy) } + } + const rows = async (sql: ProbeJournalSql, table: "server_probe_executions" | "server_probe_execution_quarantines") => (await sql.query(`/* probe-journal:${table} */ SELECT * FROM ${table} WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) FOR UPDATE`, [pinned!.receiptId, identity.kind, identity.executionId])).rows + const identityProof = () => codec.proof("identity", context) + const checkAnchor = (row: Record) => { + must(row.receipt_id === pinned!.receiptId && row.binding_proof === pinned!.bindingProof && row.identity_kind === identity.kind && row.execution_id === identity.executionId && row.actor_id === identity.ownerId && row.authentication === (identity.kind === "mcp" ? "mcp" : identity.authentication) && row.mcp_connection_id === (identity.kind === "mcp" ? identity.connectionId : null) && row.mcp_credential_id === (identity.kind === "mcp" ? identity.credentialId : null) && row.request_id === (identity.kind === "rest" ? identity.requestId : null) && String(row.authority_issued_at) === String(identity.issuedAt) && new Date(row.authority_expires_at as string | Date).getTime() === identity.expiresAt && row.source_server_id === context.sourceServerId && row.destination_server_id === (context.destinationServerId ?? null) && row.tool === context.tool) + } + const checkQuarantine = async (row: Record) => { must(row.receipt_id === pinned!.receiptId && row.binding_proof === pinned!.bindingProof && row.identity_kind === identity.kind && row.execution_id === identity.executionId && row.identity_proof === await identityProof()) } + const requireAnchor = async (sql: ProbeJournalSql, executable: boolean) => { + const quarantines = await rows(sql, "server_probe_execution_quarantines"); must(quarantines.length === 0) + const anchors = await rows(sql, "server_probe_executions"); must(anchors.length === 1); checkAnchor(anchors[0]); if (executable) must(anchors[0].state === "claimed") + return anchors[0] + } + const sqlAuthority = async (sql: ProbeJournalSql, freshClaim: boolean) => { + const servers = await sql.query("/* probe-journal:servers */ SELECT id FROM servers WHERE id=ANY($1::int[]) AND enabled=true", [scope]); must(servers.rows.length === scope.length) + if (identity.kind === "mcp") { + const owner = await sql.query(`/* probe-journal:owner */ SELECT o.id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN mcp_connections c ON c.id=o.connection_id JOIN mcp_credentials k ON k.connection_id=c.id + WHERE o.id=$1 AND c.id=$2 AND c.owner_id=$3 AND k.id=$4 AND k.revoked_at IS NULL AND k.expires_at>clock_timestamp() + AND c.owner_valid_until>clock_timestamp() AND c.policy->>'status'='active' AND c.policy->>'mode'<>'observer' + AND (c.policy->>'expiresAt')::timestamptz>clock_timestamp() AND c.policy#>'{scope,serverIds}' @> $5::jsonb + AND c.policy#>'{scope,operationIds}' @> to_jsonb(ARRAY[$6::text]) AND p.operation_id=$6 + ${freshClaim ? "AND o.state IN ('pending','running') AND p.state='executing'" : ""}`, [identity.executionId, identity.connectionId, identity.ownerId, identity.credentialId, JSON.stringify(scope), identity.operationName]) + must(owner.rows.length === 1) + } + } + const stageRows = async (sql: ProbeJournalSql): Promise => (await sql.query("/* probe-journal:stages */ SELECT * FROM server_probe_execution_stages WHERE receipt_id=$1 ORDER BY stage_index FOR UPDATE", [pinned!.receiptId])).rows as Stage[] + const assertStage = (prior: Stage[], kind: string, index: number, timeoutMs: number) => { + must(index === prior.length + 1 && index <= 11 && prior.every((stage, offset) => stage.stage_index === offset + 1 && stage.state === "received") && Number.isInteger(timeoutMs) && timeoutMs >= 1 && timeoutMs <= (context.tool === "ping" ? 120000 : context.tool === "traceroute" ? 58000 : context.tool === "mtu" ? 25000 : context.tool === "route" ? 10000 : 30000)) + if (kind === "source-dns-A") { must(["ping", "traceroute", "mtu"].includes(context.tool) && index === 1 && timeoutMs <= 10000); return } + const native = prior.filter(stage => stage.kind !== "source-dns-A") + if (context.tool === "mtu") must(kind === `native-mtu-${sizes[native.length]}` && native.length < sizes.length) + else { must(native.length === 0); const expected = { ping: "native-ping", traceroute: "native-traceroute", bandwidth: "native-bandwidth", route: "native-route-read" }; must(context.tool === "dns" ? /^manager-dns-(A|AAAA|MX|NS|TXT|CNAME|PTR)$/.test(kind) : kind === expected[context.tool]) } + } + const validateReceipt = (value: ServerProbeReceipt) => validateServerProbeJournalReceipt(context, pinned!.receiptId, value) + return { + async claim(value) { + const binding = metadata(structuredClone(value)); await guard(binding.bindingProof) + return transaction(async sql => { + const quarantines = await rows(sql, "server_probe_execution_quarantines") + if (quarantines.length) { must(quarantines.length === 1); await checkQuarantine(quarantines[0]); return "exists" } + const existing = await rows(sql, "server_probe_executions") + if (existing.length) { must(existing.length === 1); checkAnchor(existing[0]); await sqlAuthority(sql, false); await guard(binding.bindingProof); return "exists" } + await sqlAuthority(sql, true); await guard(binding.bindingProof) + const result = await sql.query(`/* probe-journal:claim */ INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,request_id,authority_issued_at,authority_expires_at,source_server_id,destination_server_id,tool) + VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) ON CONFLICT DO NOTHING RETURNING receipt_id`, [binding.receiptId, binding.bindingProof, identity.kind, identity.executionId, identity.ownerId, identity.kind === "mcp" ? "mcp" : identity.authentication, identity.kind === "mcp" ? identity.connectionId : null, identity.kind === "mcp" ? identity.credentialId : null, identity.kind === "rest" ? identity.requestId : null, identity.issuedAt, new Date(identity.expiresAt), context.sourceServerId, context.destinationServerId ?? null, context.tool]) + must(result.rows.length === 1); await guard(binding.bindingProof); return "claimed" + }, "claim") + }, + async intent(value) { + must(pinned && value.receiptId === pinned.receiptId); await guard(pinned.bindingProof) + await transaction(async sql => { + await requireAnchor(sql, true); await sqlAuthority(sql, false) + assertStage(await stageRows(sql), value.kind, value.stageIndex, value.timeoutMs); await guard(pinned!.bindingProof) + const inserted = await sql.query("/* probe-journal:intent */ INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms) VALUES($1,$2,$3,$4) RETURNING stage_index", [value.receiptId, value.stageIndex, value.kind, value.timeoutMs]); must(inserted.rows.length === 1) + }, "intent") + }, + async received(value) { + must(pinned && value.receiptId === pinned.receiptId && ["received", "source-unavailable"].includes(value.outcome)); await guard(pinned.bindingProof) + await transaction(async sql => { + await requireAnchor(sql, true); await sqlAuthority(sql, false) + const stages = await stageRows(sql), stage = stages[stages.length - 1] + must(stage && stage.stage_index === value.stageIndex && stage.state === "intent" && (value.outcome !== "source-unavailable" || stage.kind === "source-dns-A")); await guard(pinned!.bindingProof) + const changed = await sql.query("/* probe-journal:received */ UPDATE server_probe_execution_stages SET state='received',outcome=$3,received_at=clock_timestamp() WHERE receipt_id=$1 AND stage_index=$2 AND state='intent' RETURNING stage_index", [value.receiptId, value.stageIndex, value.outcome]); must(changed.rows.length === 1) + }, "received") + }, + async finish(value) { + must(pinned); const receipt = validateReceipt(value); await guard(pinned.bindingProof) + const envelope = JSON.stringify({ version: 1, context, bindingProof: pinned.bindingProof, receipt }), ciphertext = await codec.encrypt(envelope), digest = await codec.proof("receipt", envelope) + must(Buffer.byteLength(ciphertext) <= 262144 && hash.safeParse(digest).success) + await transaction(async sql => { + await requireAnchor(sql, true); await sqlAuthority(sql, false) + const stages = await stageRows(sql), received = stages.filter(stage => stage.state === "received").length + must(received === receipt.completedStages && receipt.attemptedStages <= stages.length && stages.length - receipt.attemptedStages <= 1) + if (receipt.state === "completed") must(received === stages.length && stages.length === receipt.attemptedStages) + await guard(pinned!.bindingProof) + const changed = deps.storageBoundary + ? await sql.query("/* probe-journal:finish-stored */ UPDATE server_probe_executions SET state=$2,attempted_stages=$3,completed_stages=$4,receipt_proof=$5,finished_at=clock_timestamp() WHERE receipt_id=$1 AND state='claimed' RETURNING receipt_id", [pinned!.receiptId, receipt.state, receipt.attemptedStages, receipt.completedStages, digest]) + : await sql.query("/* probe-journal:finish */ UPDATE server_probe_executions SET state=$2,attempted_stages=$3,completed_stages=$4,receipt_ciphertext=$5,receipt_proof=$6,finished_at=clock_timestamp() WHERE receipt_id=$1 AND state='claimed' RETURNING receipt_id", [pinned!.receiptId, receipt.state, receipt.attemptedStages, receipt.completedStages, ciphertext, digest]); must(changed.rows.length === 1) + }, "publish", { ciphertext, proof: digest }) + }, + async quarantine(value) { + const binding = metadata(structuredClone(value)); counts.parse(value) + const digest = await identityProof(), code = failureCodes.has(value.errorCode) ? value.errorCode : "UNCLASSIFIED_FAILURE" + await transaction(async sql => { + const anchors = await rows(sql, "server_probe_executions"); must(anchors.length <= 1); if (anchors.length) checkAnchor(anchors[0]) + const existing = await rows(sql, "server_probe_execution_quarantines") + if (existing.length) { must(existing.length === 1); await checkQuarantine(existing[0]); return } + const result = await sql.query("/* probe-journal:quarantine */ INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,$2,$3,$4,$5,$6,$7,$8) ON CONFLICT DO NOTHING RETURNING receipt_id", [binding.receiptId, binding.bindingProof, identity.kind, identity.executionId, digest, code, value.attemptedStages, value.completedStages]); must(result.rows.length === 1) + }, "quarantine") + }, + async read(receiptId, bindingProof) { + pin(receiptId, bindingProof); await guard(bindingProof) + const result = await transaction(async sql => { + const quarantines = await rows(sql, "server_probe_execution_quarantines") + if (quarantines.length) { + must(quarantines.length === 1); const row = quarantines[0]; await checkQuarantine(row) + const progress = counts.parse({ attemptedStages: row.attempted_stages, completedStages: row.completed_stages }) + await guard(bindingProof) + return { receiptId, tool: context.tool, serverId: context.sourceServerId, ...(context.destinationServerId !== undefined ? { destinationServerId: context.destinationServerId } : {}), state: "unknown" as const, httpStatus: 503, errorCode: "JOURNAL_UNKNOWN", ...progress, automaticReplay: false as const, forwardingVerified: false as const } + } + const anchors = await rows(sql, "server_probe_executions"); if (!anchors.length) return undefined + must(anchors.length === 1); const row = anchors[0]; checkAnchor(row); await sqlAuthority(sql, false) + if (row.state === "claimed") return undefined + let ciphertext = row.receipt_ciphertext + if (deps.storageBoundary) { + const stored = await deps.storageBoundary.readPayload(pinned!, sql) + if (stored.kind === "expired") throw new ProbePayloadExpired() + if (stored.kind === "unavailable") throw new ProbePayloadUnavailable() + ciphertext = stored.ciphertext; must(stored.proof === row.receipt_proof) + } + must(typeof ciphertext === "string" && Buffer.byteLength(ciphertext) <= 262144 && typeof row.receipt_proof === "string") + const plaintext = await codec.decrypt(ciphertext); must(Buffer.byteLength(plaintext) <= 150000 && await codec.proof("receipt", plaintext) === row.receipt_proof) + const envelope = JSON.parse(plaintext) as { version?: unknown; context?: unknown; bindingProof?: unknown; receipt?: unknown } + must(envelope.version === 1 && isDeepStrictEqual(envelope.context, context) && envelope.bindingProof === bindingProof) + const receipt = validateReceipt(envelope.receipt as ServerProbeReceipt) + must(row.state === receipt.state && row.attempted_stages === receipt.attemptedStages && row.completed_stages === receipt.completedStages) + await guard(bindingProof); return receipt + }) + await guard(bindingProof); return result + }, + } +} diff --git a/backend/src/services/server-probe-legacy-archive-approval.test.ts b/backend/src/services/server-probe-legacy-archive-approval.test.ts new file mode 100644 index 00000000..00ff5881 --- /dev/null +++ b/backend/src/services/server-probe-legacy-archive-approval.test.ts @@ -0,0 +1,53 @@ +import assert from "node:assert/strict" +import { generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { mkdtemp, realpath, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import path from "node:path" +import { test } from "node:test" +import { provisionDatabaseSourceBindingOffline } from "../../scripts/database-source-binding-operator.js" +import { readDatabaseSourceBindingRegistry, sourceBindingBytes, sourceBindingCanonical, sourceBindingSha256 } from "../db/database-source-binding.js" +import { LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN, verifyLegacyProbeArchiveApproval } from "./server-probe-legacy-archive-approval.js" + +test("only the pinned original operator can bind the exact archive and legacy storage to the signed source head", async () => { + const temporary = await realpath(tmpdir()) + const root = await realpath(await mkdtemp(path.join(temporary, "mm-legacy-archive-approval-"))) + try { + const key = generateKeyPairSync("ed25519"), attacker = generateKeyPairSync("ed25519") + const privateKeyFile = path.join(root, "operator.pem") + await writeFile(privateKeyFile, key.privateKey.export({ type: "pkcs8", format: "pem" }), { mode: 0o600, flag: "wx" }) + const request = { + installationId: randomUUID(), sourceIdentityId: randomUUID(), generation: randomUUID(), + source: { systemIdentifier: "123456789", databaseOid: 16384, databaseName: "synthetic", + sessionUser: "publisher", currentUser: "publisher", inRecovery: false as const, serverVersionNum: 180004 }, + } + const { deploymentPin } = await provisionDatabaseSourceBindingOffline({ request, privateKeyFile, outputDirectory: path.join(root, "registry") }) + const registry = await readDatabaseSourceBindingRegistry(deploymentPin) + const archive = Buffer.from("synthetic archive bytes") + const operatorPublicKeyPem = Buffer.from(key.publicKey.export({ type: "spki", format: "pem" })) + const body = { format: "legacy-probe-archive-approval-v1", installationId: request.installationId, + sourceIdentityId: request.sourceIdentityId, generation: request.generation, ordinal: 1, + headSha256: registry.headSha256, bindingSha256: registry.head.bindingSha256, + archiveSha256: sourceBindingSha256(archive), storageSha256: "a".repeat(64) } + const signed = (statement: typeof body, signingKey = key.privateKey) => sourceBindingBytes({ body: statement, + signature: sign(null, Buffer.from(LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN + sourceBindingCanonical(statement)), signingKey).toString("base64url") }) + const evidence = { archive, operatorPublicKeyPem, signedApproval: signed(body) } + const result = await verifyLegacyProbeArchiveApproval(deploymentPin, evidence) + assert.deepEqual(result, { ...body, approvalSha256: sourceBindingSha256(evidence.signedApproval), signerAssertionVerified: true, archiveBytesMatched: true }) + + const replacement = JSON.parse(evidence.signedApproval.toString("utf8")) + replacement.body.storageSha256 = "b".repeat(64) + for (const signedApproval of [sourceBindingBytes(replacement), sourceBindingBytes(body), signed(body, attacker.privateKey), + signed({ ...body, generation: randomUUID() }), signed({ ...body, headSha256: "b".repeat(64) }), + signed({ ...body, bindingSha256: "b".repeat(64) }), Buffer.concat([evidence.signedApproval, Buffer.from("\n")]), Buffer.alloc(4097)]) { + await assert.rejects(verifyLegacyProbeArchiveApproval(deploymentPin, { ...evidence, signedApproval }), /approval refused/) + } + await assert.rejects(verifyLegacyProbeArchiveApproval(deploymentPin, { ...evidence, archive: Buffer.from("different bytes") }), /approval refused/) + await assert.rejects(verifyLegacyProbeArchiveApproval(deploymentPin, { ...evidence, + operatorPublicKeyPem: Buffer.from(attacker.publicKey.export({ type: "spki", format: "pem" })), signedApproval: signed(body, attacker.privateKey) }), /approval refused/) + await assert.rejects(verifyLegacyProbeArchiveApproval({ ...deploymentPin, installationId: randomUUID() }, evidence), /approval refused/) + } finally { + assert.equal(path.dirname(root), temporary) + assert(path.basename(root).startsWith("mm-legacy-archive-approval-")) + await rm(root, { recursive: true, force: true }) + } +}) diff --git a/backend/src/services/server-probe-legacy-archive-approval.ts b/backend/src/services/server-probe-legacy-archive-approval.ts new file mode 100644 index 00000000..2eebbfa3 --- /dev/null +++ b/backend/src/services/server-probe-legacy-archive-approval.ts @@ -0,0 +1,52 @@ +import { createPublicKey, verify } from "node:crypto" +import { z } from "zod" +import { + readDatabaseSourceBindingRegistry, sourceBindingBodySchema, sourceBindingBytes, + sourceBindingCanonical, sourceBindingSha256, type DatabaseSourceDeploymentPin, +} from "../db/database-source-binding.js" +import { MAX_DATABASE_DUMP_BYTES } from "./bounded-database-dump.js" + +export const LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN = "mmapp:legacy-probe-archive-approval:ed25519:v1\n" +export const LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN = "mmapp:legacy-probe-custody-approval:ed25519:v1\n" +const digest = z.string().regex(/^[0-9a-f]{64}$/) +const bodySchema = sourceBindingBodySchema.pick({ installationId: true, sourceIdentityId: true, generation: true, ordinal: true }).extend({ + format: z.literal("legacy-probe-archive-approval-v1"), + headSha256: digest, bindingSha256: digest, archiveSha256: digest, storageSha256: digest, +}).strict() +const custodySchema = bodySchema.extend({ format: z.literal("legacy-probe-custody-approval-v1"), lineageSha256: digest }) +const envelopeSchema = z.object({ body: z.discriminatedUnion("format", [bodySchema, custodySchema]), signature: z.string().regex(/^[A-Za-z0-9_-]{86}$/) }).strict() + +export interface LegacyProbeArchiveApprovalInput { + archive: Buffer + signedApproval: Buffer + operatorPublicKeyPem: Buffer +} + +/** The retained original head and key are independently pinned by deployment. + * Evidence bytes name their subject. Only those deployment pins select trust. */ +export async function verifyLegacyProbeArchiveApproval(rawPin: DatabaseSourceDeploymentPin, input: LegacyProbeArchiveApprovalInput) { + const pin = { ...rawPin } + const { signedApproval, archive, operatorPublicKeyPem } = input + const refused = () => new Error("Legacy diagnostic archive approval refused") + try { + if (!Buffer.isBuffer(signedApproval) || !signedApproval.length || signedApproval.length > 4096 || + !Buffer.isBuffer(operatorPublicKeyPem) || !operatorPublicKeyPem.length || operatorPublicKeyPem.length > 4096 || + operatorPublicKeyPem.includes("PRIVATE KEY") || !Buffer.isBuffer(archive) || !archive.length || archive.length > MAX_DATABASE_DUMP_BYTES) throw refused() + const decoded: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(signedApproval)) + if (!sourceBindingBytes(decoded).equals(signedApproval)) throw refused() + const { body, signature } = envelopeSchema.parse(decoded) + const domain = body.format === "legacy-probe-custody-approval-v1" ? LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN : LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN + const publicKey = createPublicKey(operatorPublicKeyPem), signatureBytes = Buffer.from(signature, "base64url") + if (publicKey.asymmetricKeyType !== "ed25519" || + sourceBindingSha256(publicKey.export({ type: "spki", format: "der" })) !== pin.publicKeySpkiSha256 || + body.installationId !== pin.installationId || body.headSha256 !== pin.currentHeadSha256 || + signatureBytes.toString("base64url") !== signature || + !verify(null, Buffer.from(domain + sourceBindingCanonical(body)), publicKey, signatureBytes) || + sourceBindingSha256(archive) !== body.archiveSha256) throw refused() + const approvalSha256 = sourceBindingSha256(signedApproval) + const registry = await readDatabaseSourceBindingRegistry(pin) + if (registry.body.sourceIdentityId !== body.sourceIdentityId || registry.body.generation !== body.generation || + registry.body.ordinal !== body.ordinal || registry.head.bindingSha256 !== body.bindingSha256) throw refused() + return Object.freeze({ ...body, approvalSha256, signerAssertionVerified: true as const, archiveBytesMatched: true as const }) + } catch { throw refused() } +} diff --git a/backend/src/services/server-probe-legacy-cold-proof.ts b/backend/src/services/server-probe-legacy-cold-proof.ts new file mode 100644 index 00000000..65429fb3 --- /dev/null +++ b/backend/src/services/server-probe-legacy-cold-proof.ts @@ -0,0 +1,54 @@ +import { createPublicKey, verify } from "node:crypto" +import { z } from "zod" +import { sourceBindingBodySchema, sourceBindingBytes, sourceBindingCanonical, sourceBindingSha256 } from "../db/database-source-binding.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" + +export const LEGACY_PROBE_COLD_PROOF_DOMAIN = "mmapp:legacy-probe-cold-proof:ed25519:v1\n" +export const LEGACY_COLD_DIGEST = z.string().regex(/^[0-9a-f]{64}$/) +export const legacyColdProofBody = z.object({ + format: z.literal("legacy-probe-cold-proof-v1"), approvalSha256: LEGACY_COLD_DIGEST, custodySha256: LEGACY_COLD_DIGEST, + coldArchiveSha256: LEGACY_COLD_DIGEST, storageSha256: LEGACY_COLD_DIGEST, lineageSha256: LEGACY_COLD_DIGEST, + credentialSha256: LEGACY_COLD_DIGEST, keySha256: LEGACY_COLD_DIGEST, tlsSpkiSha256: LEGACY_COLD_DIGEST, + coldSource: sourceBindingBodySchema.shape.source, roleOid: z.string().regex(/^[1-9][0-9]{0,9}$/), + observedAt: z.number().int().positive(), expiresAt: z.number().int().positive(), +}).strict() +const envelope = z.object({ body: legacyColdProofBody, signature: z.string().regex(/^[A-Za-z0-9_-]{86}$/) }).strict() + +export function verifyLegacyColdProof(proof: Buffer, pinned: { + publicKeyPem: Buffer; signerSpkiSha256: string; sourceSignerSpkiSha256: string; approvalSha256: string; + custodySha256: string; coldArchiveSha256: string; storageSha256: string; lineageSha256: string +}, at = Date.now()) { + if (proof.length > 8192 || pinned.publicKeyPem.length > 4096 || pinned.publicKeyPem.includes("PRIVATE KEY")) throw new Error("Legacy cold proof refused") + const raw: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(proof)) + const { body, signature } = envelope.parse(raw), signatureBytes = Buffer.from(signature, "base64url"), key = createPublicKey(pinned.publicKeyPem) + if (!sourceBindingBytes(raw).equals(proof) || signatureBytes.toString("base64url") !== signature || key.asymmetricKeyType !== "ed25519" || + pinned.signerSpkiSha256 === pinned.sourceSignerSpkiSha256 || sourceBindingSha256(key.export({ type: "spki", format: "der" })) !== pinned.signerSpkiSha256 || + body.approvalSha256 !== pinned.approvalSha256 || body.custodySha256 !== pinned.custodySha256 || body.coldArchiveSha256 !== pinned.coldArchiveSha256 || + body.storageSha256 !== pinned.storageSha256 || body.lineageSha256 !== pinned.lineageSha256 || !Number.isSafeInteger(at) || body.observedAt > at || + body.expiresAt <= at || body.expiresAt - body.observedAt > 900000 || + !verify(null, Buffer.from(LEGACY_PROBE_COLD_PROOF_DOMAIN + sourceBindingCanonical(body)), key, signatureBytes)) throw new Error("Legacy cold proof refused") + return Object.freeze(body) +} + +/** Includes credentials named directly by anchors and all credentials of + * connections named by related operations or cancellations. Sealed operation + * bindings can refer to credentials which no longer have an anchor. */ +export async function captureLegacyProbeCredentialDigest(sql: ProbeStorageSql) { + const { rows: [shape] } = await sql.query(`SELECT c.relkind='r' AND c.relpersistence='p' + AND NOT EXISTS(SELECT FROM pg_catalog.pg_inherits i WHERE i.inhparent=c.oid) AS supported + FROM pg_catalog.pg_class c WHERE c.oid='public.mcp_credentials'::pg_catalog.regclass`) + if (shape?.supported !== true) throw new Error("Exact legacy credentials table required") + const { rows } = await sql.query(`WITH identities AS ( + SELECT execution_id AS id FROM ONLY public.server_probe_executions WHERE identity_kind='mcp' + UNION SELECT execution_id FROM ONLY public.server_probe_execution_quarantines WHERE identity_kind='mcp' + UNION SELECT operation_id FROM ONLY public.mcp_diagnostic_cancellations + ), connections AS ( + SELECT o.connection_id AS id FROM ONLY public.mcp_operations o JOIN identities i ON i.id=o.id + UNION SELECT mcp_connection_id FROM ONLY public.server_probe_executions WHERE identity_kind='mcp' + UNION SELECT connection_id FROM ONLY public.mcp_diagnostic_cancellations + ) SELECT pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(pg_catalog.record_out(c)::text,'UTF8')),'hex') AS sha256 + FROM ONLY public.mcp_credentials c WHERE c.connection_id IN (SELECT id FROM connections) + OR c.id IN (SELECT mcp_credential_id FROM ONLY public.server_probe_executions WHERE identity_kind='mcp') LIMIT 10001`) + if (rows.length > 10000 || rows.some(row => !LEGACY_COLD_DIGEST.safeParse(row.sha256).success)) throw new Error("Legacy credential digest bounds exceeded") + return sourceBindingSha256("pg18-legacy-probe-credentials-v1\n" + rows.map(row => row.sha256).sort().join("")) +} diff --git a/backend/src/services/server-probe-legacy-handoff-inspection.ts b/backend/src/services/server-probe-legacy-handoff-inspection.ts new file mode 100644 index 00000000..b0d840c7 --- /dev/null +++ b/backend/src/services/server-probe-legacy-handoff-inspection.ts @@ -0,0 +1,204 @@ +import { createPublicKey, verify } from "node:crypto" +import { z } from "zod" +import { exactHistory } from "../db/controlled-migration-history.js" +import { checkDatabaseSchemaControlState } from "../db/database-schema-control-model.js" +import { readDatabaseSourceBindingRegistry, sourceBindingBytes, sourceBindingCanonical, sourceBindingSha256, type DatabaseSourceDeploymentPin } from "../db/database-source-binding.js" +import { MIGRATION_MANIFEST, migrationManifestDigest } from "../db/migration-manifest.js" +import { captureMigrationSchema } from "../db/migration-schema-inventory.js" +import { MAX_DATABASE_DUMP_BYTES } from "./bounded-database-dump.js" +import { verifyLegacyProbeArchiveApproval, type LegacyProbeArchiveApprovalInput } from "./server-probe-legacy-archive-approval.js" +import { captureLegacyProbeLineage, captureLegacyProbeStorage, LEGACY_PROBE_CUSTODY_RELATIONS, LEGACY_PROBE_RELATIONS } from "./server-probe-legacy-source-inventory.js" +import { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK } from "../db/operation-lock.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" + +export const LEGACY_PROBE_HANDOFF_DOMAIN = "mmapp:legacy-probe-0037-handoff:ed25519:v1\n" +const heldDrainKeys = [SYSTEM_DATABASE_REQUEST_LOCK, SYSTEM_DATABASE_OPERATION_LOCK, 8723110] +const heldRelations = [...LEGACY_PROBE_CUSTODY_RELATIONS, "mcp_settings"] +const verifiedIntents = new WeakSet() +const digest = z.string().regex(/^[0-9a-f]{64}$/) +const handoffEnvelope = z.object({ + body: z.object({ format: z.literal("legacy-probe-0037-handoff-intent-v1"), operationId: z.uuidv4(), + applicationXid: z.string().regex(/^[1-9][0-9]{0,19}$/).refine(value => BigInt(value) <= 18446744073709551615n && BigInt.asUintN(32, BigInt(value)) >= 3n), + approvalSha256: digest, coldArchiveSha256: digest, preSchemaSha256: digest, postSchemaSha256: digest, + manifestSha256: digest, migrationSha256: digest, executionApprovalSha256: digest.optional(), coldProofSha256: digest.optional() }).strict(), + signature: z.string().regex(/^[A-Za-z0-9_-]{86}$/), +}).strict() + +export async function verifyLegacyProbeHandoffIntent(pin: DatabaseSourceDeploymentPin, input: LegacyProbeArchiveApprovalInput & { intent: Buffer; coldArchive: Buffer }) { + const approved = await verifyLegacyProbeArchiveApproval(pin, input) + if (approved.format !== "legacy-probe-custody-approval-v1" || !Buffer.isBuffer(input.intent) || input.intent.length > 4096 || + !Buffer.isBuffer(input.coldArchive) || input.coldArchive.length > MAX_DATABASE_DUMP_BYTES || input.coldArchive.subarray(0,5).toString() !== "PGDMP") { + throw new Error("Legacy probe handoff evidence refused") + } + const decoded: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(input.intent)) + const { body, signature } = handoffEnvelope.parse(decoded), signatureBytes = Buffer.from(signature, "base64url") + if (!sourceBindingBytes(decoded).equals(input.intent) || signatureBytes.toString("base64url") !== signature || + (body.executionApprovalSha256 === undefined) !== (body.coldProofSha256 === undefined) || + body.approvalSha256 !== approved.approvalSha256 || body.coldArchiveSha256 !== sourceBindingSha256(input.coldArchive) || + body.migrationSha256 !== MIGRATION_MANIFEST[36].sha256 || body.manifestSha256 !== migrationManifestDigest() || + !verify(null, Buffer.from(LEGACY_PROBE_HANDOFF_DOMAIN + sourceBindingCanonical(body)), createPublicKey(input.operatorPublicKeyPem), signatureBytes)) { + throw new Error("Legacy probe handoff intent refused") + } + const registry = await readDatabaseSourceBindingRegistry(pin) + const intent = Object.freeze({ ...body, intentSha256: sourceBindingSha256(input.intent), storageSha256: approved.storageSha256, + lineageSha256: approved.lineageSha256, source: Object.freeze({ ...registry.body.source }) }) + verifiedIntents.add(intent) + return intent +} + +type ApprovedHandoff = Awaited> +const heldOwners = new WeakSet() +const lockProof = `SELECT + (SELECT count(DISTINCT l.relation)::int FROM pg_catalog.pg_locks l WHERE l.pid=$1 AND l.database=$2::oid + AND l.locktype='relation' AND l.granted AND l.mode=ANY($5::text[]) + AND l.relation IN (SELECT pg_catalog.to_regclass('public.' || name) FROM pg_catalog.unnest($3::text[]) AS t(name))) AS relations, + (SELECT count(DISTINCT l.objid)::int FROM pg_catalog.pg_locks l WHERE l.pid=$1 AND l.database=$2::oid + AND l.locktype='advisory' AND l.granted AND l.mode='ExclusiveLock' AND l.classid=0 AND l.objsubid=1 AND l.objid=ANY($4::oid[])) AS drains` + +export async function verifyHeldLegacyProbeOwner(sql: ProbeStorageSql, intent: ApprovedHandoff) { + if (!verifiedIntents.has(intent)) throw new Error("Verified legacy custody intent required") + const { rows: [context] } = await sql.query(`SELECT pg_catalog.pg_backend_pid() AS pid, pg_catalog.transaction_timestamp()::text AS started, + pg_catalog.pg_current_xact_id_if_assigned()::text AS xid, pg_catalog.current_database() AS database_name, + d.oid::text AS database_oid, c.system_identifier::text AS system_identifier, pg_catalog.pg_is_in_recovery() AS recovery, + pg_catalog.current_setting('server_version_num')::int AS version, pg_catalog.current_setting('transaction_isolation') AS isolation, + pg_catalog.current_setting('transaction_read_only') AS read_only, + CURRENT_USER=SESSION_USER AND pg_catalog.pg_has_role(CURRENT_USER,n.nspowner,'USAGE') AS owns_public + FROM pg_catalog.pg_database d CROSS JOIN pg_catalog.pg_control_system() c CROSS JOIN pg_catalog.pg_namespace n + WHERE d.datname=pg_catalog.current_database() AND n.nspname='public'`) + if (!context || context.xid !== intent.applicationXid || context.database_oid !== String(intent.source.databaseOid) || context.database_name !== intent.source.databaseName || + context.system_identifier !== intent.source.systemIdentifier || context.version !== intent.source.serverVersionNum || context.recovery !== false || + context.isolation !== "repeatable read" || context.read_only !== "off" || context.owns_public !== true || + typeof context.pid !== "number" || !Number.isSafeInteger(context.pid) || typeof context.started !== "string" || !Number.isFinite(Date.parse(context.started))) { + throw new Error("Exact held legacy owner transaction required") + } + const proof = Object.freeze({ intent, pid: context.pid, started: context.started }) + heldOwners.add(proof) + await assertHeldLegacyProbeOwner(sql, proof) + const storage = await captureLegacyProbeStorage(sql), lineage = await captureLegacyProbeLineage(sql) + if (storage.rowCount === 0 || storage.sha256 !== intent.storageSha256 || lineage.sha256 !== intent.lineageSha256) throw new Error("Held legacy custody rows changed") + return proof +} + +export type HeldLegacyProbeOwner = Awaited> + +export async function assertHeldLegacyProbeOwner(sql: ProbeStorageSql, proof: HeldLegacyProbeOwner) { + if (!heldOwners.has(proof)) throw new Error("Verified held legacy owner required") + const { intent, pid, started } = proof + await sql.query("SELECT pg_catalog.pg_stat_clear_snapshot()") + const { rows: [context] } = await sql.query(`SELECT pg_catalog.pg_xact_status($1::pg_catalog.xid8) AS status, + EXISTS(SELECT FROM pg_catalog.pg_stat_activity a WHERE a.pid=$2 AND a.datid=$3::oid + AND a.xact_start=$4::timestamptz AND a.backend_xid::text=$5) AS held, + (pg_catalog.pg_control_system()).system_identifier::text AS system_identifier, + (SELECT oid::text FROM pg_catalog.pg_database WHERE datname=pg_catalog.current_database()) AS database_oid`, + [intent.applicationXid, pid, intent.source.databaseOid, started, BigInt.asUintN(32, BigInt(intent.applicationXid)).toString()]) + const { rows: [locks] } = await sql.query(lockProof, [pid, intent.source.databaseOid, heldRelations, heldDrainKeys, ["AccessExclusiveLock"]]) + if (context?.status !== "in progress" || context.held !== true || context.system_identifier !== intent.source.systemIdentifier || + context.database_oid !== String(intent.source.databaseOid) || locks?.relations !== heldRelations.length || locks.drains !== heldDrainKeys.length) { + throw new Error("Legacy owner transaction and exclusive drain no longer held") + } +} + +export const LEGACY_HANDOFF_ADOPTION_RELATIONS = Object.freeze([...heldRelations, "mcp_credentials", "server_probe_storage_admissions", + "server_probe_storage_usage", "server_probe_execution_payloads"].sort()) +const adoptionProofs = new WeakSet() + +export async function verifyLegacyHandoffAdoption(holder: ProbeStorageSql, observer: ProbeStorageSql, intent: ApprovedHandoff, deadlineAt: number, + release?: { approvalSha256: string; adoptionSha256: string }) { + if (!verifiedIntents.has(intent)) throw new Error("Verified legacy custody intent required") + const { rows: [identity] } = await holder.query(`SELECT pg_catalog.pg_backend_pid() AS pid, pg_catalog.transaction_timestamp()::text AS started`) + if (typeof identity?.pid !== "number" || !Number.isSafeInteger(identity.pid) || typeof identity.started !== "string" || !Number.isFinite(Date.parse(identity.started))) { + throw new Error("Held legacy adoption owner required") + } + const { rows: [marker] } = await observer.query(`SELECT pg_catalog.floor(EXTRACT(EPOCH FROM applied_at)*1000)::text AS applied_at + FROM public.schema_migrations WHERE id=$1`, [MIGRATION_MANIFEST[36].file.slice(0,-4)]) + const appliedAt = Number(marker?.applied_at) + if (!Number.isSafeInteger(appliedAt) || appliedAt <= 0) throw new Error("Exact committed 0037 marker timestamp required") + const proof = Object.freeze({ intent, pid: identity.pid, started: identity.started, appliedAt, release: release && Object.freeze({ ...release }) }) + adoptionProofs.add(proof) + await assertLegacyHandoffAdoptionHeld(holder, proof) + const observed = await observeLegacyProbeHandoff(observer, intent, deadlineAt) + if (observed.outcome !== "committed" || !observed.quarantineActive || + (release ? !observed.adoptionRecorded || observed.adoptionSha256 !== release.adoptionSha256 : observed.adoptionRecorded)) { + throw new Error("Exact committed legacy handoff phase required") + } + await assertLegacyHandoffAdoptionHeld(holder, proof) + return proof +} + +export type LegacyHandoffAdoption = Awaited> + +export async function assertLegacyHandoffAdoptionHeld(sql: ProbeStorageSql, proof: LegacyHandoffAdoption) { + if (!adoptionProofs.has(proof)) throw new Error("Verified committed legacy handoff required") + const { intent, pid, started } = proof + await sql.query("SELECT pg_catalog.pg_stat_clear_snapshot()") + const { rows: [context] } = await sql.query(`SELECT EXISTS(SELECT FROM pg_catalog.pg_stat_activity a WHERE a.pid=$1 AND a.datid=$2::oid + AND a.xact_start=$3::timestamptz) AS held, + (pg_catalog.pg_control_system()).system_identifier::text AS system_identifier, + (SELECT oid::text FROM pg_catalog.pg_database WHERE datname=pg_catalog.current_database()) AS database_oid`, [pid, intent.source.databaseOid, started]) + const { rows: [locks] } = await sql.query(lockProof, [pid, intent.source.databaseOid, LEGACY_HANDOFF_ADOPTION_RELATIONS, heldDrainKeys, ["ShareLock"]]) + if (context?.held !== true || context.system_identifier !== intent.source.systemIdentifier || context.database_oid !== String(intent.source.databaseOid) || + locks?.relations !== LEGACY_HANDOFF_ADOPTION_RELATIONS.length || locks.drains !== heldDrainKeys.length) throw new Error("Legacy adoption drain no longer held") +} + +export async function captureLegacyHandoffSchema(sql: ProbeStorageSql, deadlineAt: number) { + await sql.query("SET LOCAL search_path=pg_catalog; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'; SET LOCAL standard_conforming_strings=on") + return (await captureMigrationSchema(sql, { scope: { applicationNamespaces: ["public"], excludedControlNamespaces: ["mmapp_control"] }, deadlineAt })).sha256 +} + +export async function checkLegacyHandoffPostState(sql: ProbeStorageSql, expectedSchema: string, xid: string, deadlineAt: number) { + const { rows: markers } = await sql.query('SELECT id,xmin::text AS xid FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT 38') + if (markers.length !== 37 || !markers.every((row, index) => row.id === MIGRATION_MANIFEST[index].file.slice(0, -4)) || + markers[36].xid !== BigInt.asUintN(32, BigInt(xid)).toString() || await captureLegacyHandoffSchema(sql, deadlineAt) !== expectedSchema) return false + const { rows: [facts] } = await sql.query(`SELECT (${[...LEGACY_PROBE_RELATIONS, "server_probe_storage_admissions", "server_probe_execution_payloads"] + .map(relation => `NOT EXISTS(SELECT FROM public.${relation})`).join(" AND ")}) AS empty, + (SELECT count(*)=1 AND bool_and(scope='global' AND admissions=0 AND reserved_files=0 AND reserved_bytes=0) FROM public.server_probe_storage_usage) AS seed`) + return facts?.empty === true && facts.seed === true +} + +export async function observeLegacyProbeHandoff(sql: ProbeStorageSql, intent: Awaited>, deadlineAt: number) { + await sql.query("SET LOCAL search_path=pg_catalog; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'; SET LOCAL standard_conforming_strings=on") + const { rows: gate } = await sql.query(`SELECT q.operation_id::text AS active, s.state + FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1`) + const state = checkDatabaseSchemaControlState(gate[0]?.state) + if (gate.length !== 1) throw new Error("Exact legacy control singleton required") + const { rows: history } = await sql.query(`SELECT id::text AS id,intent,terminal FROM mmapp_control.database_schema_intents ORDER BY id LIMIT ${MIGRATION_MANIFEST.length + 1}`) + const linked = history.find(row => row.id === intent.operationId) + if (gate[0].active === null) { + const snapshot = { ...state, intent: null, phase: "TERMINAL" as const } + const completed = history.filter(row => row.id !== state.intent?.id) + if (!linked || await exactHistory(completed, snapshot, intent.manifestSha256, true) < 37) throw new Error("Exact released legacy history required") + const terminal = linked.terminal as NonNullable + if (!terminal.legacyRelease || terminal.intent.artifactSha256 !== intent.intentSha256) throw new Error("Exact authenticated legacy release required") + return Object.freeze({ operationId: intent.operationId, intentSha256: intent.intentSha256, outcome: "committed" as const, adoptionRecorded: true, + adoptionSha256: terminal.observationSha256, quarantineActive: false as const, releaseRecorded: true, + releaseApprovalSha256: terminal.legacyRelease.approvalSha256, automaticReplay: false as const, readyAuthorized: false as const }) + } + const original = state.intent ?? state.terminal?.intent + if (gate[0].active !== intent.operationId || original?.id !== intent.operationId || original.kind !== "migration" || + original.manifestSha256 !== intent.manifestSha256 || original.artifactSha256 !== intent.intentSha256) throw new Error("Exact quarantined legacy operation required") + const adoptionRecorded = state.intent === null + if (adoptionRecorded) { + if (await exactHistory(history, state, intent.manifestSha256, true) !== 37 || state.terminal?.legacyRelease) throw new Error("Exact authenticated adopted 0037 required") + } else { + const pending = history.filter(row => row.id === intent.operationId) + if (pending.length !== 1 || pending[0].terminal !== null || sourceBindingCanonical(pending[0].intent) !== sourceBindingCanonical(state.intent) || + await exactHistory(history.filter(row => row.id !== intent.operationId), { ...state, intent: null, phase: "TERMINAL" }, intent.manifestSha256, true) !== 36) { + throw new Error("Exact authenticated legacy migration prefix required") + } + } + const { rows: [xact] } = await sql.query(`SELECT pg_catalog.pg_xact_status($1::pg_catalog.xid8) AS status, + pg_catalog.pg_visible_in_snapshot($1::pg_catalog.xid8,pg_catalog.pg_current_snapshot()) AS visible`, [intent.applicationXid]) + const { rows: markers } = await sql.query('SELECT id,xmin::text AS xid FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT 38') + let outcome: "committed" | "rolled-back" | "unknown" = "unknown" + const exactMarkers = (length: number) => markers.length === length && markers.every((row, index) => row.id === MIGRATION_MANIFEST[index].file.slice(0, -4)) + if (!adoptionRecorded && xact?.status === "aborted" && xact.visible === true && exactMarkers(36)) { + const storage = await captureLegacyProbeStorage(sql), lineage = await captureLegacyProbeLineage(sql) + if (storage.sha256 === intent.storageSha256 && lineage.sha256 === intent.lineageSha256 && await captureLegacyHandoffSchema(sql, deadlineAt) === intent.preSchemaSha256) outcome = "rolled-back" + } else if (xact?.status === "committed" && xact.visible === true && + await checkLegacyHandoffPostState(sql, intent.postSchemaSha256, intent.applicationXid, deadlineAt)) { + outcome = "committed" + } + return Object.freeze({ operationId: intent.operationId, intentSha256: intent.intentSha256, outcome, adoptionRecorded, + adoptionSha256: adoptionRecorded ? state.terminal?.observationSha256 : undefined, quarantineActive: true as const, + releaseRecorded: false, automaticReplay: false as const, readyAuthorized: false as const }) +} diff --git a/backend/src/services/server-probe-legacy-inspection.sql-helper.ts b/backend/src/services/server-probe-legacy-inspection.sql-helper.ts new file mode 100644 index 00000000..7e41d230 --- /dev/null +++ b/backend/src/services/server-probe-legacy-inspection.sql-helper.ts @@ -0,0 +1,97 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import type { DatabaseSourceDeploymentPin } from "../db/database-source-binding.js" +import type { DatabaseSourceObservation } from "../db/database-source-observation.js" + +/** Actual private host acceptance against this runner's disposable legacy DB. */ +export async function acceptLegacyProbeInspectionSql(issue: (source: DatabaseSourceObservation["source"]) => Promise) { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { inspectLegacyServerProbeSnapshot, inspectLegacyServerProbeSource } = await import("./server-probe-legacy-inspection.js") + const { McpError } = await import("../modules/mcp/security.js") + const actor = { id: "owned-inspection-" + randomUUID(), email: "", name: "", apps: ["mm"], permissions: ["mm:settings:admin"], + issuedAt: Date.now() - 1000, expiresAt: Date.now() + 120000 } + const before = (await pool.query("SELECT count(*)::int AS n FROM server_probe_executions")).rows[0].n + const summary = await inspectLegacyServerProbeSnapshot(actor, { deadlineAt: Date.now() + 30000 }) + assert.equal(summary.complete, true); assert.equal(summary.counts.verified, 2) + assert.equal(summary.migrationReady, false); assert.equal(summary.conversionAllowed, false) + const paired = await inspectLegacyServerProbeSource(actor, { deadlineAt: Date.now() + 30000 }) + assert.equal(paired.summary.counts.verified, 2) + assert.equal(paired.sourceObservation.source.databaseName, "postgres") + assert.equal(paired.sourceObservation.generationAuthenticated, false) + assert.equal(paired.sourceObservation.ready, false) + assert(Object.isFrozen(paired) && Object.isFrozen(paired.sourceObservation)) + assert.equal(paired.sourceBinding, undefined) + const pin = await issue(paired.sourceObservation.source) + const bound = await inspectLegacyServerProbeSource(actor, { deadlineAt: Date.now() + 30000 }, pin) + assert.equal(bound.summary.counts.verified, 2) + assert.equal(bound.sourceBinding?.headSha256, pin.currentHeadSha256) + assert.equal(bound.sourceBinding?.genuineSourceFactsMatched, true) + assert.equal(bound.sourceBinding?.generationAuthenticated, false) + assert.equal(bound.sourceBinding?.allWriterDrainAuthenticated, false) + assert.equal(bound.conversionAllowed, false) + const wrongSource = await issue({ ...paired.sourceObservation.source, databaseName: "different-fixture" }) + await assert.rejects(inspectLegacyServerProbeSource(actor, { deadlineAt: Date.now() + 30000 }, wrongSource), McpError) + await assert.rejects(inspectLegacyServerProbeSource(actor, { deadlineAt: Date.now() + 30000 }, + { ...pin, currentHeadSha256: "0".repeat(64) }), McpError) + await pool.query("INSERT INTO mcp_owner_revocations(owner_id,invalid_before) VALUES($1,$2)", [actor.id, new Date()]) + await assert.rejects(inspectLegacyServerProbeSnapshot(actor, { deadlineAt: Date.now() + 30000 }), McpError) + const postActor = { ...actor, id: "owned-inspection-post-" + randomUUID() } + const { createLegacyServerProbeInspectionForTests } = await import("./server-probe-legacy-inspection.js") + const { connectProbeStorage } = await import("./server-probe-storage-connection.js") + const { assertPortalClaimsCurrent } = await import("../modules/mcp/portal-revocation.js") + const { collectLegacyServerProbeSnapshot } = await import("./server-probe-legacy-snapshot.js") + let collected = false + const postRace = createLegacyServerProbeInspectionForTests({ connect: connectProbeStorage, portal: assertPortalClaimsCurrent, + async collect(sql, options) { + const result = await collectLegacyServerProbeSnapshot(sql, options) + assert.equal(result.counts.verified, 2); collected = true + await pool.query("INSERT INTO mcp_owner_revocations(owner_id,invalid_before) VALUES($1,$2)", [postActor.id, new Date()]) + return result + } }) + await assert.rejects(postRace(postActor, { deadlineAt: Date.now() + 30000 }), McpError) + assert.equal(collected, true) + const freshActor = { ...actor, id: "owned-inspection-" + randomUUID() } + const blocker = await pool.connect(), controller = new AbortController() + let blockedPid: number | undefined + try { + await blocker.query("BEGIN") + await blocker.query("LOCK TABLE server_probe_executions IN ACCESS EXCLUSIVE MODE") + const completed = inspectLegacyServerProbeSnapshot(freshActor, { deadlineAt: Date.now() + 10000, signal: controller.signal }) + .then(value => ({ value, error: undefined }), error => ({ value: undefined, error })) + const until = Date.now() + 4000 + while (!blockedPid && Date.now() < until) { + const rows = (await pool.query(`SELECT pid FROM pg_stat_activity WHERE datname=current_database() + AND query LIKE '%probe-legacy-snapshot:orphan-stages%' AND wait_event_type='Lock' AND pid<>pg_backend_pid()`)).rows + if (rows.length) blockedPid = Number(rows[0].pid) + else await new Promise(resolve => setTimeout(resolve, 10)) + } + assert(blockedPid, "Expected actual collector SELECT blocked on the legacy anchor relation") + const abortedAt = Date.now(); controller.abort() + let timer: ReturnType | undefined + const result = await Promise.race([completed, new Promise((_, reject) => { + timer = setTimeout(() => reject(Error("Private inspection did not settle while relation lock remained held")), 2000) + })]).finally(() => clearTimeout(timer)) + assert(result.error instanceof McpError); assert.equal(result.value, undefined) + assert(Date.now() - abortedAt < 2000) + const granted = (await blocker.query("SELECT EXISTS(SELECT 1 FROM pg_locks WHERE pid=pg_backend_pid() AND mode='AccessExclusiveLock' AND granted) AS granted")).rows[0].granted + assert.equal(granted, true) + } finally { controller.abort(); await blocker.query("ROLLBACK"); blocker.release(true) } + const cleanupUntil = Date.now() + 3000 + let remaining = true + while (remaining && Date.now() < cleanupUntil) { + remaining = !!(await pool.query("SELECT 1 FROM pg_stat_activity WHERE pid=$1", [blockedPid])).rows.length + if (remaining) await new Promise(resolve => setTimeout(resolve, 10)) + } + assert.equal(remaining, false) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions")).rows[0].n, before) + return { actualPrivateProductionHost: true, actualPortalRevocation: true, productionCrypto: true, + twoLegacyReceiptsAuthenticated: true, actualSourceAndLegacyDataPairedInOwnedSnapshot: true, signedBindingMatchedInOwnedSnapshot: true, + wrongSourceAndHeadRefused: true, sourceGenerationStillUnauthenticated: true, realPostCollectionRevocationRefused: true, postRaceUsesTrustedCollectorObserver: true, actualRelationLock: true, abortedBeforeUnlockWithin2Seconds: true, + cancelledBackendGoneAfterUnlock: true, unchangedAnchorCount: true, nativeCalls: 0, publicRegistration: false, + migrationReady: false, conversionAllowed: false } +} diff --git a/backend/src/services/server-probe-legacy-inspection.test.ts b/backend/src/services/server-probe-legacy-inspection.test.ts new file mode 100644 index 00000000..951e3c65 --- /dev/null +++ b/backend/src/services/server-probe-legacy-inspection.test.ts @@ -0,0 +1,147 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import type { AuthUser } from "../lib/permissions.js" +import { McpError } from "../modules/mcp/security.js" +import type { ProbeStorageConnection, ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import type { LegacyServerProbeSnapshotSummary } from "./server-probe-legacy-snapshot.js" +import { createLegacyServerProbeInspectionForTests } from "./server-probe-legacy-inspection.js" + +const actor = (): AuthUser => ({ id: "operator", email: "operator@example.test", name: "Operator", apps: ["mm"], + permissions: ["mm:settings:admin"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000 }) +const options = () => ({ deadlineAt: Date.now() + 2000 }) +const deferred = () => { let resolve!: (value: T) => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +const summary = Object.freeze({ version: 1, complete: true, aggregateDigest: "a".repeat(64), migrationReady: false, + conversionAllowed: false, automaticReplay: false }) as unknown as Readonly +function harness() { + const leases: { commands: string[]; releases: boolean[]; timeouts: number[] }[] = [] + let active = 0 + const connect = async () => { + assert.equal(active, 0, "one-backend pool must never require parallel checkout") + active++ + const entry = { commands: [] as string[], releases: [] as boolean[], timeouts: [] as number[] }; leases.push(entry) + const connection: ProbeStorageConnection = { + async query(text, _values, queryOptions) { + entry.commands.push(text); entry.timeouts.push(queryOptions!.timeoutMs) + if (text.includes("probe-legacy-snapshot:transaction")) return { rows: [{ isolation: "repeatable read", read_only: "on", explicit_transaction: true }] } + if (text.includes("probe-legacy-snapshot:orphan-stages")) return { rows: [{ count: "0" }] } + return { rows: [] } + }, + release(unsafe = false) { entry.releases.push(unsafe); active-- }, + } + return connection + } + return { connect, leases } +} +const safeFailure = (error: unknown) => error instanceof McpError && !error.message.includes("private-provider") + +test("unauthorized, expired, missing/future JWT claims perform no I/O", async () => { + const h = harness(); let portal = 0 + const inspect = createLegacyServerProbeInspectionForTests({ connect: h.connect, portal: async () => { portal++ } }) + const bad = [undefined, { ...actor(), permissions: [], isAdmin: true }, { ...actor(), expiresAt: Date.now() - 1 }, + { ...actor(), issuedAt: undefined }, { ...actor(), expiresAt: undefined }, { ...actor(), issuedAt: Date.now() + 5000 }, + { ...actor(), permissions: ["mm:mcp:admin"] }, { ...actor(), id: "x".repeat(201) }, { ...actor(), apps: [] }] + for (const value of bad) await assert.rejects(inspect(value as AuthUser, options()), safeFailure) + assert.equal(h.leases.length, 0); assert.equal(portal, 0) +}) + +test("real collector owns RR read-only snapshot; current claims use fresh sequential leases", async () => { + const h = harness(); const checked: string[] = [] + const inspect = createLegacyServerProbeInspectionForTests({ connect: h.connect, portal: async (value, client) => { + assert(client) + checked.push(value.id); await client.query("SELECT invalid_before FROM mcp_owner_revocations WHERE owner_id=$1", [value.id]) + } }) + const result = await inspect(actor(), options()) + assert.equal(result.complete, true); assert.equal(result.conversionAllowed, false); assert.equal(result.migrationReady, false) + assert.equal(h.leases.length, 3); assert.deepEqual(checked, ["operator", "operator"]) + assert.deepEqual(h.leases.map(lease => lease.releases), [[false], [false], [false]]) + assert.deepEqual(h.leases[1].commands.slice(0, 2), ["BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY", "SET LOCAL statement_timeout = '10s'"]) + assert.equal(h.leases[1].commands.at(-1), "ROLLBACK") + assert(h.leases.every(lease => lease.timeouts.every(value => value > 0 && value <= 10000))) + assert(!h.leases.flatMap(value => value.commands).some(text => /\b(COMMIT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP)\b/.test(text))) +}) + +test("precheck revocation prevents secret SQL and uses fixed safe errors", async () => { + const h = harness(); let collected = false + const inspect = createLegacyServerProbeInspectionForTests({ connect: h.connect, + portal: async () => { throw new Error("private-provider revocation details") }, collect: async () => { collected = true; return summary } }) + await assert.rejects(inspect(actor(), options()), safeFailure) + assert.equal(collected, false); assert.equal(h.leases.length, 1); assert.deepEqual(h.leases[0].releases, [false]) +}) + +test("postcheck revocation never returns a partial/complete unauthorized summary", async () => { + const h = harness(); let checks = 0, collected = false + const inspect = createLegacyServerProbeInspectionForTests({ connect: h.connect, + portal: async () => { if (++checks === 2) throw new Error("private-provider") }, + collect: async () => { collected = true; return summary } }) + await assert.rejects(inspect(actor(), options()), safeFailure) + assert.equal(collected, true); assert.equal(checks, 2); assert.equal(h.leases.length, 3) + assert(h.leases.every(value => value.releases.length === 1)) +}) + +test("original actor is cloned/frozen; JWT expiry and 300s cap bound checkout", async () => { + const h = harness(); const original = actor(); const expiration = original.expiresAt!; const deadlines: number[] = [] + const inspect = createLegacyServerProbeInspectionForTests({ connect: async value => { deadlines.push(value.deadlineAt); return h.connect() }, + portal: async value => { assert(Object.isFrozen(value)); assert(Object.isFrozen((value as AuthUser).permissions)); assert.equal(value.id, "operator") }, + collect: async (_sql, value) => { original.id = "changed"; original.expiresAt = Date.now() + 9999999; original.permissions.length = 0; + assert.equal(value.deadlineAt, expiration); return summary } }) + await inspect(original, { deadlineAt: Date.now() + 9999999 }) + assert(deadlines.every(value => value <= expiration)) + const longActor = { ...actor(), expiresAt: Date.now() + 9999999 }; const start = Date.now() + const capped = createLegacyServerProbeInspectionForTests({ connect: h.connect, portal: async () => {}, + collect: async (_sql, value) => { assert(value.deadlineAt <= start + 300000 + 10); return summary } }) + await capped(longActor, { deadlineAt: Date.now() + 9999999 }) +}) + +test("abort during crypto gap returns promptly, destroys once, and refuses late SQL", async () => { + const h = harness(), reached = deferred(), gap = deferred(), late = deferred() + const controller = new AbortController(); let facade!: ProbeStorageSql + const inspect = createLegacyServerProbeInspectionForTests({ connect: h.connect, portal: async () => {}, collect: async sql => { + facade = sql; reached.resolve(); await gap.promise + await assert.rejects(sql.query("SELECT private_source"), safeFailure); late.resolve(); return summary + } }) + const pending = inspect(actor(), { ...options(), signal: controller.signal }) + await reached.promise; controller.abort(); await assert.rejects(pending, safeFailure) + assert.deepEqual(h.leases[1].releases, [true]); assert(!h.leases[1].commands.includes("ROLLBACK")) + const count = h.leases[1].commands.length + gap.resolve(); await late.promise; await assert.rejects(facade.query("SELECT private_source"), safeFailure) + assert.equal(h.leases[1].commands.length, count); assert.deepEqual(h.leases[1].releases, [true]); assert.equal(h.leases.length, 2) +}) + +test("deadline bounds portal filesystem gaps and prevents late revocation SQL", async () => { + const h = harness(), reached = deferred(), gap = deferred(), late = deferred() + const inspect = createLegacyServerProbeInspectionForTests({ connect: h.connect, portal: async (_actor, client) => { + assert(client) + reached.resolve(); await gap.promise; await assert.rejects(client.query("SELECT revocation"), safeFailure); late.resolve() + } }) + const pending = inspect(actor(), { deadlineAt: Date.now() + 40 }) + await reached.promise; await assert.rejects(pending, safeFailure) + assert.deepEqual(h.leases[0].releases, [true]); gap.resolve(); await late.promise + assert.deepEqual(h.leases[0].commands, []); assert.deepEqual(h.leases[0].releases, [true]) +}) + +test("failed SQL destroys once without rollback, healthy collector error rolls back", async () => { + const h = harness() + const inspect = createLegacyServerProbeInspectionForTests({ connect: async _value => { + const connection = await h.connect() + if (h.leases.length === 2) { const query = connection.query.bind(connection); connection.query = async (text, values, opts) => { + if (text === "SELECT fail") throw new Error("private-provider") + return query(text, values, opts) + } } + return connection + }, portal: async () => {}, collect: async sql => { await sql.query("SELECT fail"); return summary } }) + await assert.rejects(inspect(actor(), options()), safeFailure) + assert.deepEqual(h.leases[1].releases, [true]); assert(!h.leases[1].commands.includes("ROLLBACK")) + const healthy = harness() + const failCollector = createLegacyServerProbeInspectionForTests({ connect: healthy.connect, portal: async () => {}, collect: async () => { throw new Error("private-provider") } }) + await assert.rejects(failCollector(actor(), options()), safeFailure) + assert.equal(healthy.leases[1].commands.at(-1), "ROLLBACK"); assert.deepEqual(healthy.leases[1].releases, [false]) +}) + +test("abandoned checkout is destroyed exactly once when it eventually resolves", async () => { + const acquired = deferred(), releases: boolean[] = [], controller = new AbortController() + const inspect = createLegacyServerProbeInspectionForTests({ connect: () => acquired.promise, portal: async () => {} }) + const pending = inspect(actor(), { ...options(), signal: controller.signal }) + await Promise.resolve(); controller.abort(); await assert.rejects(pending, safeFailure) + acquired.resolve({ query: async () => { throw new Error("must not query") }, release: value => { releases.push(value ?? false) } }) + await new Promise(resolve => setImmediate(resolve)); assert.deepEqual(releases, [true]) +}) diff --git a/backend/src/services/server-probe-legacy-inspection.ts b/backend/src/services/server-probe-legacy-inspection.ts new file mode 100644 index 00000000..6fb22286 --- /dev/null +++ b/backend/src/services/server-probe-legacy-inspection.ts @@ -0,0 +1,189 @@ +/** Internal authenticated operator observation. No transport registration, borrowed + * transaction, conversion capability, or caller-selected storage is provided. */ +import type { PoolClient } from "pg" +import { createDatabaseSourceBindingVerifier, type DatabaseSourceDeploymentPin } from "../db/database-source-binding.js" +import { captureDatabaseSourceObservation, assertDatabaseSourceObservationCurrent } from "../db/database-source-observation.js" +import { hasPermission, type AuthUser } from "../lib/permissions.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { McpError } from "../modules/mcp/security.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import type { ProbeStorageConnection, ProbeStorageOptions, ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { collectLegacyServerProbeSnapshot, type LegacyServerProbeSnapshotOptions } from "./server-probe-legacy-snapshot.js" +import { captureLegacyProbeLineage, captureLegacyProbeStorage, LEGACY_STORAGE_TIME_CONTEXT } from "./server-probe-legacy-source-inventory.js" +import { verifyLegacyProbeArchiveApproval, type LegacyProbeArchiveApprovalInput } from "./server-probe-legacy-archive-approval.js" + +type Dependencies = { + connect: (options: ProbeStorageOptions) => Promise + portal: typeof assertPortalClaimsCurrent + collect: typeof collectLegacyServerProbeSnapshot +} +const unavailable = () => new McpError("STATE_UNAVAILABLE", "Наблюдение legacy-диагностики недоступно", 503) +const denied = () => new McpError("PERMISSION_DENIED", "Требуется актуальный JWT администратора настроек", 403) + +/** Owns checkout and the entire callback, including crypto/filesystem gaps. + * Every continuation holds an inactive facade after timeout or completion. */ +async function owned(deps: Dependencies, options: ProbeStorageOptions, snapshot: boolean, work: (sql: ProbeStorageSql) => Promise): Promise { + let active = true, broken = false, busy = false, transaction = false, released = false + let physical: ProbeStorageConnection | undefined + const release = (unsafe: boolean) => { + if (!physical || released) return + released = true + try { physical.release(unsafe) } catch { /* Never disclose driver details. */ } + } + const alive = () => active && !broken && !options.signal?.aborted && Date.now() < options.deadlineAt + const fence = () => { if (!alive()) throw unavailable() } + let stop!: () => void + const stopped = new Promise((_resolve, reject) => { + stop = () => { active = false; broken = true; release(true); reject(unavailable()) } + }) + const timer = setTimeout(stop, Math.max(1, options.deadlineAt - Date.now())) + options.signal?.addEventListener("abort", stop, { once: true }) + const sql: ProbeStorageSql = Object.freeze({ + async query(text: string, values?: unknown[], queryOptions?: { signal?: AbortSignal; timeoutMs: number }) { + fence() + if (busy || !physical) { stop(); throw unavailable() } + busy = true + try { + const result = await physical.query(text, values, { + signal: options.signal, + timeoutMs: Math.max(1, Math.min(10000, options.deadlineAt - Date.now(), queryOptions?.timeoutMs ?? 10000)), + }) + fence() + return result + } catch { broken = true; release(true); throw unavailable() } + finally { busy = false } + }, + }) + const completion = Promise.resolve().then(async () => { + fence() + const acquired = await deps.connect(options) + physical = acquired + if (!alive()) { release(true); throw unavailable() } + try { + if (snapshot) { + await sql.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + transaction = true + await sql.query("SET LOCAL statement_timeout = '10s'") + } + const result = await work(sql) + fence() + return result + } finally { + // Failed/in-flight backends are destroyed, never sent cleanup SQL. A + // healthy callback failure may still return its read-only lease cleanly. + if (transaction && alive() && !busy) { + await sql.query("ROLLBACK") + transaction = false + } + } + }) + try { + if (!alive()) stop() + const result = await Promise.race([completion, stopped]) + fence() + return result + } finally { + active = false + clearTimeout(timer) + options.signal?.removeEventListener("abort", stop) + release(broken || busy || transaction) + } +} + +function createInspection(deps: Dependencies) { + return async (rawActor: AuthUser, rawOptions: LegacyServerProbeSnapshotOptions) => { + // Authenticated middleware supplies this actor; no local/open-mode bypass. + const now = Date.now() + if (!rawActor || typeof rawActor.id !== "string" || !rawActor.id.trim() || rawActor.id.length > 200 + || !Array.isArray(rawActor.apps) || !rawActor.apps.includes("mm") + || !Array.isArray(rawActor.permissions) || !hasPermission(rawActor.permissions, "mm:settings:admin") + || !Number.isSafeInteger(rawActor.issuedAt) || rawActor.issuedAt! <= 0 || rawActor.issuedAt! > now + || !Number.isSafeInteger(rawActor.expiresAt) || rawActor.expiresAt! <= now || rawActor.expiresAt! <= rawActor.issuedAt!) throw denied() + // Copy before the first await; mutation of a request object cannot extend + // the original grant or change the principal checked after observation. + const actor: AuthUser = Object.freeze({ ...rawActor, apps: Object.freeze([...rawActor.apps]) as unknown as string[], + permissions: Object.freeze([...rawActor.permissions]) as unknown as string[] }) + const options = { ...rawOptions } + if (!Number.isSafeInteger(options.deadlineAt) || options.deadlineAt <= now + || options.batchSize !== undefined && (!Number.isSafeInteger(options.batchSize) || options.batchSize < 1 || options.batchSize > 100) + || options.totalLimit !== undefined && (!Number.isSafeInteger(options.totalLimit) || options.totalLimit < 1 || options.totalLimit > 10000)) throw unavailable() + const deadlineAt = Math.min(options.deadlineAt, now + 300000, actor.expiresAt!) + const fence = () => { if (options.signal?.aborted || Date.now() >= deadlineAt) throw unavailable() } + const authorize = async () => { + fence() + await owned(deps, { signal: options.signal, deadlineAt: Math.min(deadlineAt, Date.now() + 10000) }, false, async sql => { + await deps.portal(actor, { query: sql.query as PoolClient["query"] }) + fence() + }) + fence() + } + try { + await authorize() + const summary = await owned(deps, { deadlineAt, signal: options.signal }, true, + sql => deps.collect(sql, { ...options, deadlineAt })) + // Fresh lease after releasing RR snapshot: revocations committed during + // collection must be visible even with a pool maximum of one backend. + await authorize() + fence() + return summary + } catch { throw unavailable() } + } +} + +/** Production entry point: fixed implementations, inert until explicitly used. */ +export const inspectLegacyServerProbeSnapshot = createInspection({ connect: connectProbeStorage, + portal: assertPortalClaimsCurrent, collect: collectLegacyServerProbeSnapshot }) + +/** Only trusted operator assembly supplies deployment pins. Archive evidence + * can authenticate a signer assertion. Current SQL and historical origin are + * distinct facts, even when their raw storage digests match. */ +export async function inspectLegacyServerProbeSource(actor: AuthUser, options: LegacyServerProbeSnapshotOptions, + deploymentPin?: DatabaseSourceDeploymentPin, storageExpectation?: string | LegacyProbeArchiveApprovalInput, + originalSourcePin?: DatabaseSourceDeploymentPin) { + const expectedDigest = typeof storageExpectation === "string" ? storageExpectation : undefined + if (expectedDigest !== undefined && !/^[a-f0-9]{64}$/.test(expectedDigest)) throw unavailable() + const archive = storageExpectation !== undefined && typeof storageExpectation !== "string" + ? { evidence: { ...storageExpectation }, pin: originalSourcePin && { ...originalSourcePin } } : undefined + if (archive && !archive.pin) throw unavailable() + const pin = deploymentPin === undefined ? undefined : { ...deploymentPin } + let source: Awaited> | undefined + let sourceBinding: Awaited["verify"]>> | undefined + let storageSnapshot: Awaited> | undefined + let lineageSnapshot: Awaited> | undefined + let approvedArchive: Awaited> | undefined + const summary = await createInspection({ connect: connectProbeStorage, portal: assertPortalClaimsCurrent, + collect: async (sql, boundedOptions) => { + if (archive?.pin) approvedArchive = await verifyLegacyProbeArchiveApproval(archive.pin, archive.evidence) + const expectedStorageSha256 = approvedArchive?.storageSha256 ?? expectedDigest + if (expectedStorageSha256 !== undefined) await sql.query(`SELECT ${LEGACY_STORAGE_TIME_CONTEXT}`) + const original = await captureDatabaseSourceObservation(sql, boundedOptions) + const verifier = pin === undefined ? undefined : createDatabaseSourceBindingVerifier(pin) + await verifier?.verify(sql, original, boundedOptions) + const result = await collectLegacyServerProbeSnapshot(sql, boundedOptions) + if (expectedStorageSha256 !== undefined) { + if (!result.complete) throw unavailable() + storageSnapshot = await captureLegacyProbeStorage(sql) + if (storageSnapshot.sha256 !== expectedStorageSha256) throw unavailable() + if (approvedArchive?.format === "legacy-probe-custody-approval-v1") { + lineageSnapshot = await captureLegacyProbeLineage(sql) + if (lineageSnapshot.sha256 !== approvedArchive.lineageSha256) throw unavailable() + } + } + sourceBinding = await verifier?.verify(sql, original, boundedOptions) + await assertDatabaseSourceObservationCurrent(sql, original, boundedOptions) + source = original + return result + } })(actor, options) + if (!source) throw unavailable() + return Object.freeze({ sourceObservation: source, summary, ...(sourceBinding ? { sourceBinding } : {}), + ...(storageSnapshot ? { storageSnapshot } : {}), + ...(lineageSnapshot ? { lineageSnapshot } : {}), + ...(approvedArchive ? { approvedArchive } : {}), + migrationReady: false as const, conversionAllowed: false as const, + requiresExternalSourceBinding: true as const, generationAuthenticated: false as const }) +} + +/** Explicit trusted-code test seam. Never compose this from request JSON. */ +export function createLegacyServerProbeInspectionForTests(dependencies: Pick & Partial>) { + return createInspection({ collect: collectLegacyServerProbeSnapshot, ...dependencies }) +} diff --git a/backend/src/services/server-probe-legacy-orphans.sql.test.ts b/backend/src/services/server-probe-legacy-orphans.sql.test.ts new file mode 100644 index 00000000..c1dca3b3 --- /dev/null +++ b/backend/src/services/server-probe-legacy-orphans.sql.test.ts @@ -0,0 +1,238 @@ +import assert from "node:assert/strict" +import { generateKeyPairSync, randomUUID, sign } from "node:crypto" +import { realpath, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { basename, dirname, isAbsolute, relative, resolve } from "node:path" +import type { ServerProbeJournalContext } from "./server-probe-journal.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" + +// A separate, disposable legacy-only profile. Guard before any application +// import can create the fixed production pool or access private state. +assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_ORPHANS, "true") +const url = new URL(process.env.DATABASE_URL ?? "") +assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") +assert(process.env.MCP_STATE_DIR) +const state = resolve(process.env.MCP_STATE_DIR), temporary = relative(resolve(tmpdir()), state) +assert(temporary && !isAbsolute(temporary) && temporary !== ".." && !temporary.startsWith("..\\") && !temporary.startsWith("../")) +assert(basename(dirname(state)).startsWith("mm-mcp-test-")) + +const { pool, closePool } = await import("../db/index.js") +const { applyLegacyProbeFixtureMigrations: applySqlMigrations } = await import("../db/legacy-probe-fixture.js") +const security = await import("../modules/mcp/security.js") +const { readServerProbeExecutionSource } = await import("../modules/mcp/diagnostic-cancellation-source.js") +const { effectiveMcpConnection } = await import("../modules/mcp/store.js") +const { connectProbeStorage } = await import("./server-probe-storage-connection.js") +const { inspectLegacyServerProbeSource } = await import("./server-probe-legacy-inspection.js") +const { captureLegacyProbeStorage } = await import("./server-probe-legacy-source-inventory.js") +const { sourceBindingBytes, sourceBindingCanonical, sourceBindingSha256, readDatabaseSourceBindingRegistry } = await import("../db/database-source-binding.js") +const { provisionDatabaseSourceBindingOffline } = await import("../../scripts/database-source-binding-operator.js") +const { LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN } = await import("./server-probe-legacy-archive-approval.js") +const cases: string[] = [] +const privateValue = "owned orphan profile synthetic receipt plaintext" +const ownerId = "owned-orphan-original-" + randomUUID(), connectionId = randomUUID(), credentialId = randomUUID() +const stamp = Date.now(), issuedAt = stamp - 7200000, expiresAt = stamp - 3600000 +const oldExpiry = new Date(expiresAt).toISOString(), until = new Date(stamp + 3600000).toISOString() +let serverId: number, generation: string +const bindingProof = "a".repeat(64) +const actor = () => ({ id: "owned-orphan-operator-" + randomUUID(), email: "", name: "", apps: ["mm"], permissions: ["mm:settings:admin"], + issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000 }) + +async function legacySchema() { + const migrations = (await pool.query("SELECT id FROM schema_migrations ORDER BY id")).rows.map(value => String(value.id)) + assert(migrations.includes("0035_server_probe_journal")); assert(migrations.includes("0036_diagnostic_cancellations")) + assert.equal(migrations.at(-1), "0036_diagnostic_cancellations") + const relations = (await pool.query(`SELECT n.nspname,c.relname FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace + WHERE c.relname IN ('server_probe_execution_payloads','server_probe_storage_admissions','server_probe_storage_usage') + ORDER BY n.nspname,c.relname`)).rows + assert.deepEqual(relations, [], "Legacy-only profile must never install the v2 proposal") + return migrations +} +async function tableFingerprint() { + const tables = ["server_probe_executions", "server_probe_execution_stages", "server_probe_execution_quarantines", "mcp_diagnostic_cancellations", + "mcp_connections", "mcp_credentials", "mcp_plans", "mcp_operations", "mcp_operation_steps", "mcp_owner_revocations", "mcp_settings", "schema_migrations"] + const result: Record = {} + for (const table of tables) { + // Names are a source-owned constant list; no caller identifier is accepted. + const rows = (await pool.query(`SELECT COALESCE(jsonb_agg(to_jsonb(t) ORDER BY to_jsonb(t)::text),'[]'::jsonb) AS rows FROM ${table} t`)).rows[0].rows + result[table] = security.fingerprint(rows) + } + return result +} +async function mcpSource(forgedDigest = false) { + const operationId = randomUUID(), receiptId = randomUUID(), planId = randomUUID() + const context: ServerProbeJournalContext = { identity: { kind: "mcp", ownerId, executionId: operationId, issuedAt, expiresAt, + connectionId, credentialId, operationName: "servers.probes.run" }, sourceServerId: serverId, tool: "ping" } + const binding = { version: 1, operation: "servers.probes.run", principal: { connectionId, credentialId, credentialExpiresAt: oldExpiry }, + ownerId, issuedAt, authorityExpiresAt: expiresAt, prepared: { receiptId, serverId, tool: "ping", expiresAt: oldExpiry, + bindingCiphertext: await security.encryptSecret("owned synthetic original provider request") }, bindingProof, stageBudget: 2 } + const input = { receiptId, serverId, inputDigest: forgedDigest ? "b".repeat(64) + : await security.privateSecretFingerprint(security.fingerprint(binding), "server-probe-mcp-binding-v1"), + bindingCiphertext: await security.encryptSecret(JSON.stringify(binding)) } + const step = { kind: "service", service: "servers.probes.run", input } + const evidence = { steps: [step], target: { scope: "servers", serverIds: [serverId] }, requiresHuman: true, + verificationScope: "acknowledgement", mutationCost: 2, validUntil: oldExpiry } + const sealed = await security.encryptSecret(JSON.stringify(evidence)) + await pool.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by) + VALUES($1,$2,'servers.probes.run',1,1,$3,'{}',$4::jsonb,'completed',$5,$6)`, [planId, connectionId, generation, JSON.stringify({ sealed }), oldExpiry, ownerId]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,finished_at) VALUES($1,$2,$3,$4,'applied',clock_timestamp())", [operationId, planId, connectionId, randomUUID()]) + await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", [operationId, JSON.stringify(security.redact(step))]) + return { operationId, receiptId, context, sealed, input } +} +type Source = Awaited> +async function readActualSource(value: Source, reject = false) { + const before = await tableFingerprint() + const options = { deadlineAt: Date.now() + 10000 }, sql = await connectProbeStorage(options) + try { + await sql.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + await sql.query("SET LOCAL statement_timeout='10s'") + const load = readServerProbeExecutionSource(sql, value.operationId, { connectionId, ownerId }) + if (reject) await assert.rejects(load) + else { + const actual = await load + assert.deepEqual(actual.originalContext, value.context) + assert.equal(actual.originalCredentialId, credentialId); assert.equal(actual.originalContext.identity.expiresAt, expiresAt) + assert(actual.originalContext.identity.expiresAt < Date.now()); assert.equal(actual.scope.receiptId, value.receiptId) + } + await sql.query("ROLLBACK") + } finally { sql.release(true) } + assert.deepEqual(await tableFingerprint(), before) +} +async function quarantine(value: Source, attempted = 0, invalidMac = false) { + const mac = invalidMac ? "c".repeat(64) : await security.privateSecretFingerprint(security.fingerprint(value.context), "server-probe-journal-v1:identity") + await pool.query(`INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) + VALUES($1,$2,'mcp',$3,$4,'JOURNAL_UNKNOWN',$5,0)`, [value.receiptId, bindingProof, value.operationId, mac, attempted]) +} +async function cancel(value: Source) { + await pool.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3)", [value.operationId, connectionId, ownerId]) +} +async function observe(identities: number, unresolved: number, reasons: Record = {}) { + const before = await tableFingerprint(), schema = await legacySchema() + const result = await inspectLegacyServerProbeSource(actor(), { deadlineAt: Date.now() + 30000, batchSize: 1 }) + assert.equal(result.summary.complete, true) + assert.equal(result.summary.counts.identities, identities); assert.equal(result.summary.counts.verified, 3); assert.equal(result.summary.counts.unresolved, unresolved) + assert.deepEqual(result.summary.authenticationCounts, { receipt: 1, "approved-source": 2, "identity-context-only": 0 }) + assert.deepEqual(result.summary.reasons, reasons) + assert.equal(result.summary.migrationReady, false); assert.equal(result.summary.conversionAllowed, false); assert.equal(result.summary.automaticReplay, false) + assert.equal(result.sourceObservation.ready, false); assert.equal(result.sourceObservation.generationAuthenticated, false) + assert.equal(result.sourceObservation.requiresExternalSourceBinding, true); assert.equal(result.migrationReady, false) + assert.equal(result.sourceObservation.source.databaseName, "postgres") + for (const privateData of [privateValue, ownerId, credentialId, connectionId]) assert.equal(JSON.stringify(result.summary).includes(privateData), false) + assert.deepEqual(await tableFingerprint(), before, "Fixed host must not alter anchors, ledgers, cancellations, source evidence or schema") + assert.deepEqual(await legacySchema(), schema) + return result.summary +} + +async function captureLegacyRows() { + const client = await pool.connect() + try { + await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + return await captureLegacyProbeStorage({ query: (text, values) => client.query(text, values) }) + } finally { + try { await client.query("ROLLBACK") } finally { client.release(true) } + } +} + +try { + await applySqlMigrations(pool) + await pool.query("UPDATE traffic_settings SET enabled=false; UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false") + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + const initialSchema = await legacySchema() + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('owned-legacy-orphans','127.0.0.1',9,true) RETURNING id")).rows[0].id) + generation = await security.accessGeneration() + const policy = { id: connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: until, + scope: { serverIds: [serverId], operationIds: ["servers.probes.run"], allowGlobal: false } } + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned orphan source',$2,$3::jsonb,$4::jsonb,$5,$6,clock_timestamp())", [connectionId, ownerId, JSON.stringify(policy.permissions), JSON.stringify(policy), generation, until]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), oldExpiry]) + const completed = await mcpSource(), orphanQuarantine = await mcpSource(), orphanCancellation = await mcpSource() + for (const source of [completed, orphanQuarantine, orphanCancellation]) await readActualSource(source) + const currentCredentialId = randomUUID() + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [currentCredentialId, connectionId, randomUUID(), until]) + assert.equal((await effectiveMcpConnection({ connectionId, credentialId: currentCredentialId, credentialExpiresAt: until })).owner_id, ownerId) + await assert.rejects(effectiveMcpConnection({ connectionId, credentialId, credentialExpiresAt: oldExpiry })) + const receipt: ServerProbeReceipt = { receiptId: completed.receiptId, tool: "ping", serverId, state: "completed", httpStatus: 200, + output: privateValue, data: { kind: "ping" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } + const plaintext = JSON.stringify({ version: 1, context: completed.context, bindingProof, receipt }) + await pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,authority_issued_at,authority_expires_at,source_server_id,tool) + VALUES($1,$2,'mcp',$3,$4,'mcp',$5,$6,$7,$8,$9,'ping')`, [completed.receiptId, bindingProof, completed.operationId, ownerId, connectionId, credentialId, issuedAt, oldExpiry, serverId]) + await pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,received_at) VALUES($1,1,'native-ping',1000,'received','received',clock_timestamp())", [completed.receiptId]) + await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_ciphertext=$2,receipt_proof=$3,finished_at=clock_timestamp() WHERE receipt_id=$1", [completed.receiptId, await security.encryptSecret(plaintext), await security.privateSecretFingerprint(security.fingerprint(plaintext), "server-probe-journal-v1:receipt")]) + await quarantine(orphanQuarantine); await cancel(orphanCancellation) + const positive = await observe(3, 0) + assert.deepEqual(positive.counts, { identities: 3, verified: 3, unresolved: 0, mcp: 3, rest: 0, anchors: 1, quarantines: 1, cancellations: 1, orphanStages: 0 }) + const rawSnapshot = await captureLegacyRows() + assert.equal(rawSnapshot.rowCount, 4) + assert.deepEqual(await captureLegacyRows(), rawSnapshot) + for (const datum of [privateValue, completed.receiptId, ownerId]) assert.equal(JSON.stringify(rawSnapshot).includes(datum), false) + const compareSnapshot = (expectedStorageSha256: string) => inspectLegacyServerProbeSource(actor(), + { deadlineAt: Date.now() + 30000 }, undefined, expectedStorageSha256) + const correlated = await compareSnapshot(rawSnapshot.sha256) + assert.deepEqual(correlated.storageSnapshot, rawSnapshot) + assert.equal(correlated.summary.counts.verified, 3) + assert.equal(correlated.generationAuthenticated, false) + assert.equal(correlated.conversionAllowed, false) + await assert.rejects(inspectLegacyServerProbeSource(actor(), { deadlineAt: Date.now() + 30000, totalLimit: 1 }, + undefined, rawSnapshot.sha256), { code: "STATE_UNAVAILABLE" }) + const key = generateKeyPairSync("ed25519") + const runnerDirectory = await realpath(dirname(state)) + const privateKeyFile = resolve(runnerDirectory, "owned-archive-approval.pem") + await writeFile(privateKeyFile, key.privateKey.export({ type: "pkcs8", format: "pem" }), { flag: "wx", mode: 0o600 }) + const request = { installationId: randomUUID(), sourceIdentityId: randomUUID(), generation: randomUUID(), + source: { ...correlated.sourceObservation.source, databaseName: "original-legacy", inRecovery: false as const } } + const { deploymentPin: originalPin } = await provisionDatabaseSourceBindingOffline({ request, privateKeyFile, + outputDirectory: resolve(runnerDirectory, "owned-original-archive-source") }) + const original = await readDatabaseSourceBindingRegistry(originalPin) + const archive = Buffer.from("owned synthetic original archive bytes") + const statement = { format: "legacy-probe-archive-approval-v1", installationId: request.installationId, + sourceIdentityId: request.sourceIdentityId, generation: request.generation, ordinal: 1, + headSha256: original.headSha256, bindingSha256: original.head.bindingSha256, + archiveSha256: sourceBindingSha256(archive), storageSha256: rawSnapshot.sha256 } + const signedApproval = sourceBindingBytes({ body: statement, signature: sign(null, + Buffer.from(LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN + sourceBindingCanonical(statement)), key.privateKey).toString("base64url") }) + const archiveApproval = { archive, signedApproval, operatorPublicKeyPem: Buffer.from(key.publicKey.export({ type: "spki", format: "pem" })) } + const compareArchive = (evidence = archiveApproval, trustedPin = originalPin) => inspectLegacyServerProbeSource(actor(), + { deadlineAt: Date.now() + 30000 }, undefined, evidence, trustedPin) + const approved = await compareArchive() + assert.equal(approved.approvedArchive?.signerAssertionVerified, true) + assert.equal(approved.approvedArchive?.archiveBytesMatched, true) + assert.equal(approved.approvedArchive?.generation, request.generation) + assert.equal(approved.generationAuthenticated, false) + assert.equal(approved.conversionAllowed, false) + await assert.rejects(compareArchive({ ...archiveApproval, archive: Buffer.from("different bytes") }), { code: "STATE_UNAVAILABLE" }) + await assert.rejects(inspectLegacyServerProbeSource(actor(), { deadlineAt: Date.now() + 30000 }, undefined, archiveApproval), + { code: "STATE_UNAVAILABLE" }) + cases.push("actual completed MCP encrypted receipt and received ledger authenticated with expired original credential") + cases.push("actual orphan MCP quarantine zero/zero and context MAC authenticated by sealed approved source without anchor") + cases.push("actual orphan MCP cancellation authenticated by sealed approved source without anchor") + + await pool.query("INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,$2,'rest',$3,$4,'JOURNAL_UNKNOWN',0,0)", [randomUUID(), bindingProof, randomUUID(), "d".repeat(64)]) + const changedSnapshot = await captureLegacyRows() + assert.equal(changedSnapshot.rowCount, 5) + assert.notEqual(changedSnapshot.sha256, rawSnapshot.sha256) + assert.notEqual(changedSnapshot.tables[1].sha256, rawSnapshot.tables[1].sha256) + assert.equal(changedSnapshot.tables[3].sha256, rawSnapshot.tables[3].sha256) + await assert.rejects(compareSnapshot(rawSnapshot.sha256), { code: "STATE_UNAVAILABLE" }) + await assert.rejects(compareArchive(), { code: "STATE_UNAVAILABLE" }) + await assert.rejects(compareSnapshot("malformed"), { code: "STATE_UNAVAILABLE" }) + assert.equal((await compareSnapshot(changedSnapshot.sha256)).summary.counts.unresolved, 1) + cases.push("raw legacy operator snapshot is stable across leases and detects an appended quarantine without exposing data") + cases.push("legacy operator correlates raw digest and verified classifications on one owned snapshot and refuses drift") + cases.push("legacy operator verifies independently pinned source signer and exact archive bytes before correlating snapshot") + await observe(4, 1, { CONTEXT_UNAVAILABLE: 1 }); cases.push("orphan REST quarantine without original context remains unresolved") + await pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool) + VALUES($1,$2,'rest',$3,$4,'jwt',$5,$6,$7,$8,'ping')`, [randomUUID(), bindingProof, randomUUID(), "unproved-rest-owner", randomUUID(), issuedAt, oldExpiry, serverId]) + await observe(5, 2, { CONTEXT_UNAVAILABLE: 2 }); cases.push("claimed REST anchor without receipt or context MAC remains unresolved") + const invalidMac = await mcpSource(); await readActualSource(invalidMac); await quarantine(invalidMac, 0, true) + await observe(6, 3, { CONTEXT_UNAVAILABLE: 2, QUARANTINE_INVALID: 1 }); cases.push("actual sealed MCP source does not authenticate invalid orphan quarantine MAC") + const forged = await mcpSource(true); await readActualSource(forged, true); await cancel(forged) + await observe(7, 4, { CONTEXT_UNAVAILABLE: 2, QUARANTINE_INVALID: 1, SOURCE_UNAVAILABLE: 1 }); cases.push("actual encrypted MCP evidence with forged keyed input digest remains unresolved") + const noLedger = await mcpSource(); await readActualSource(noLedger); await quarantine(noLedger, 1) + const final = await observe(8, 5, { CONTEXT_UNAVAILABLE: 2, QUARANTINE_INVALID: 2, SOURCE_UNAVAILABLE: 1 }) + cases.push("actual orphan MCP quarantine nonzero progress without ledger remains unresolved") + assert.deepEqual(await legacySchema(), initialSchema) + console.log("Owned legacy-only orphan/source acceptance:", JSON.stringify({ cases, actualSql: true, actualProductionCrypto: true, + actualFixedProductionSourcePairedHost: true, originalExpiredCredentialNotRevived: true, appendOnlyFixtures: true, + immutableRowsEdited: 0, triggersDisabled: 0, v2ProposalApplied: false, v2RelationsBefore: 0, v2RelationsAfter: 0, schemaVersionBefore: initialSchema.at(-1), schemaVersionAfter: initialSchema.at(-1), + finalCounts: final.counts, authenticationCounts: final.authenticationCounts, reasons: final.reasons, + sourceGenerationAuthenticated: false, migrationReady: false, conversionAllowed: false, nativeCalls: 0, realNetworkCalls: 0 })) +} finally { await closePool() } diff --git a/backend/src/services/server-probe-legacy-snapshot.sql-helper.ts b/backend/src/services/server-probe-legacy-snapshot.sql-helper.ts new file mode 100644 index 00000000..c55622e9 --- /dev/null +++ b/backend/src/services/server-probe-legacy-snapshot.sql-helper.ts @@ -0,0 +1,64 @@ +import assert from "node:assert/strict" +import { basename, dirname, resolve } from "node:path" + +/** Owned legacy-schema fixture only. No migration or production enrollment. */ +export async function acceptLegacyProbeSnapshotSql(writeConcurrent: () => Promise) { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { connectProbeStorage } = await import("./server-probe-storage-connection.js") + const { collectLegacyServerProbeSnapshot } = await import("./server-probe-legacy-snapshot.js") + const inspect = async (concurrent = false) => { + const options = { deadlineAt: Date.now() + 30000, batchSize: 1 } + const client = await connectProbeStorage(options) + let observed = false + try { + await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + await client.query("SET LOCAL statement_timeout='10000ms'") + return await collectLegacyServerProbeSnapshot({ async query(text, values, queryOptions) { + const result = await client.query(text, values, queryOptions) + if (concurrent && !observed && text.includes("probe-legacy-snapshot:identities")) { + observed = true + await writeConcurrent() + } + return result + } }, options) + } finally { try { await client.query("ROLLBACK") } finally { client.release(true) } } + } + const raw = await connectProbeStorage({ deadlineAt: Date.now() + 10000 }) + try { + const refused = await collectLegacyServerProbeSnapshot(raw, { deadlineAt: Date.now() + 10000 }) + assert.equal(refused.complete, false); assert.equal(refused.reasons.SNAPSHOT_REQUIRED, 1) + } finally { raw.release(true) } + const session = await connectProbeStorage({ deadlineAt: Date.now() + 10000 }) + try { + await session.query("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY") + const autocommit = await collectLegacyServerProbeSnapshot(session, { deadlineAt: Date.now() + 10000 }) + assert.equal(autocommit.complete, false); assert.equal(autocommit.reasons.SNAPSHOT_REQUIRED, 1) + } finally { session.release(true) } + const original = await inspect() + assert.equal(original.complete, true); assert.equal(original.counts.identities, 1) + assert.equal(original.counts.verified, 1); assert.equal(original.authenticationCounts.receipt, 1) + assert.equal(original.counts.unresolved, 0); assert.equal(original.migrationReady, false) + assert.equal(original.conversionAllowed, false) + assert.equal((await inspect()).aggregateDigest, original.aggregateDigest) + const concurrent = await inspect(true) + assert.equal(concurrent.counts.identities, 1) + assert.equal(concurrent.aggregateDigest, original.aggregateDigest) + const fresh = await inspect() + assert.equal(fresh.complete, true); assert.equal(fresh.counts.identities, 2) + assert.equal(fresh.counts.verified, 2); assert.notEqual(fresh.aggregateDigest, original.aggregateDigest) + const before = (await pool.query("SELECT count(*)::int AS n FROM server_probe_executions")).rows[0].n + const repeated = await inspect() + assert.equal(repeated.aggregateDigest, fresh.aggregateDigest) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions")).rows[0].n, before) + const encoded = JSON.stringify(fresh) + for (const privateField of ["receipt_ciphertext", "receipt_id", "binding_proof", "execution_id", "ownerId", "originalContext", "owned synthetic measurement"]) + assert(!encoded.includes(privateField)) + return { actualSql: true, productionCrypto: true, actualLegacyWriter: true, + repeatableReadReadOnly: true, concurrentCommittedWriterExcluded: true, nextSnapshotSeesWriter: true, + unchangedSnapshotDigestStable: true, autocommitRefused: true, readOnlyRepeatableDefaultsStillRefusedWithoutBegin: true, summaryContainsNoIndividualIdentityOrPayload: true, + migrationReady: false, conversionAllowed: false, nativeCalls: 0, productionEnrollment: false } +} diff --git a/backend/src/services/server-probe-legacy-snapshot.test.ts b/backend/src/services/server-probe-legacy-snapshot.test.ts new file mode 100644 index 00000000..a0ba2a1b --- /dev/null +++ b/backend/src/services/server-probe-legacy-snapshot.test.ts @@ -0,0 +1,171 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mkdtemp, rmdir, unlink } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { encryptSecret, fingerprint, privateSecretFingerprint, redact } from "../modules/mcp/security.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { collectLegacyServerProbeSnapshot, type LegacyServerProbeSnapshotOptions } from "./server-probe-legacy-snapshot.js" + +type Row = Record +const privateValue = "NEVER_SNAPSHOT_PLAINTEXT_OR_PROVIDER_ERROR" +const timestamp = new Date("2020-01-01T00:00:00Z") +function database() { + const anchors: Row[] = [], quarantines: Row[] = [], cancellations: Row[] = [], stages: Row[] = [] + const sources = new Map(), calls: { text: string; values?: unknown[]; timeout?: number }[] = [] + let isolation = "repeatable read", readOnly = "on", explicit = true, orphanStages = 0 + let observe: ((text: string) => void | Promise) | undefined + const sql: ProbeStorageSql = { async query(text, values, options) { + calls.push({ text, values, timeout: options?.timeoutMs }) + assert(!/\bBEGIN\b|\bCOMMIT\b|\bROLLBACK\b|\bINSERT\b|\bDELETE\b|\bUPDATE\b|\bALTER\b|\bCREATE\b|FOR SHARE/.test(text)) + assert(options && options.timeoutMs >= 1 && options.timeoutMs <= 10000) + await observe?.(text) + let rows: Row[] + const [kind, execution, receipt] = values ?? [] + if (text.includes(":transaction")) rows = [{ isolation, read_only: readOnly, explicit_transaction: explicit }] + else if (text.includes(":orphan-stages")) rows = [{ count: String(orphanStages) }] + else if (text.includes(":identities")) { + const all = [...anchors, ...quarantines].map(row => ({ identity_kind: row.identity_kind, execution_id: row.execution_id })) + all.push(...cancellations.map(row => ({ identity_kind: "mcp", execution_id: row.operation_id }))) + rows = [...new Map(all.map(row => [String(row.identity_kind) + ":" + String(row.execution_id), row])).values()] + .sort((a,b) => String(a.identity_kind).localeCompare(String(b.identity_kind)) || String(a.execution_id).localeCompare(String(b.execution_id))) + .filter(row => kind === null || String(row.identity_kind) > String(kind) || row.identity_kind === kind && String(row.execution_id) > String(execution)) + .slice(0, Number(receipt)) + } else if (text.includes(":anchor-receipt")) rows = anchors.filter(row => row.identity_kind === kind && row.execution_id === execution || row.receipt_id === receipt).slice(0, 2) + else if (text.includes(":anchor")) rows = anchors.filter(row => row.identity_kind === kind && row.execution_id === execution).slice(0, 2) + else if (text.includes(":quarantine")) rows = quarantines.filter(row => row.identity_kind === kind && row.execution_id === execution || receipt !== null && row.receipt_id === receipt).slice(0, 2) + else if (text.includes(":cancellation")) rows = cancellations.filter(row => kind === "mcp" && row.operation_id === execution).slice(0, 2) + else if (text.includes(":owner")) { const source = sources.get(String(kind)); rows = source ? [{ connection_id: source.connection_id, owner_id: source.owner_id }] : [] } + else if (text.includes("diagnostic-cancellation:source")) { + assert(!text.includes("FOR UPDATE")); const source = sources.get(String(kind)); rows = source ? [source] : [] + } else if (text.includes(":stages")) rows = stages.filter(row => row.receipt_id === kind).sort((a,b) => Number(a.stage_index) - Number(b.stage_index)).slice(0, 12) + else throw Error(privateValue) + return { rows: structuredClone(rows) } + } } + return { sql, anchors, quarantines, cancellations, stages, sources, calls, + settings: (iso: string, ro: string, transaction = true) => { isolation = iso; readOnly = ro; explicit = transaction }, + orphans: (count: number) => { orphanStages = count }, onQuery: (callback: typeof observe) => { observe = callback } } +} +type Database = ReturnType +async function addRest(db: Database) { + const receiptId = randomUUID(), executionId = randomUUID(), requestId = randomUUID(), owner = "private-owner-" + randomUUID() + const context = { identity: { kind: "rest", ownerId: owner, executionId, issuedAt: 1, expiresAt: timestamp.getTime(), requestId, authentication: "jwt" }, sourceServerId: 9, tool: "ping" } + const receipt = { receiptId, tool: "ping", serverId: 9, state: "completed", httpStatus: 200, output: privateValue, + data: { kind: "ping" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } + const plaintext = JSON.stringify({ version: 1, context, bindingProof: "a".repeat(64), receipt }) + db.anchors.push({ receipt_id: receiptId, binding_proof: "a".repeat(64), identity_kind: "rest", execution_id: executionId, actor_id: owner, + authentication: "jwt", mcp_connection_id: null, mcp_credential_id: null, request_id: requestId, authority_issued_at: "1", authority_expires_at: timestamp, + source_server_id: 9, destination_server_id: null, tool: "ping", state: "completed", attempted_stages: 1, completed_stages: 1, + receipt_ciphertext: await encryptSecret(plaintext), receipt_proof: await privateSecretFingerprint(fingerprint(plaintext), "server-probe-journal-v1:receipt"), created_at: timestamp, finished_at: timestamp }) + db.stages.push({ receipt_id: receiptId, stage_index: 1, kind: "native-ping", timeout_ms: 1000, state: "received", outcome: "received", created_at: timestamp, received_at: timestamp }) + return { executionId, receiptId, owner, requestId } +} +async function addMcpCancellation(db: Database) { + const operationId = randomUUID(), connectionId = randomUUID(), credentialId = randomUUID(), receiptId = randomUUID(), ownerId = "private-mcp-owner" + const binding = { version: 1, operation: "servers.probes.run", principal: { connectionId, credentialId, credentialExpiresAt: timestamp.toISOString() }, + ownerId, issuedAt: 1, authorityExpiresAt: timestamp.getTime(), prepared: { receiptId, serverId: 9, tool: "ping", expiresAt: timestamp.toISOString(), bindingCiphertext: "private-inner-ciphertext" }, bindingProof: "b".repeat(64), stageBudget: 2 } + const input = { receiptId, serverId: 9, bindingCiphertext: await encryptSecret(JSON.stringify(binding)), inputDigest: await privateSecretFingerprint(fingerprint(binding), "server-probe-mcp-binding-v1") } + const step = { kind: "service", service: "servers.probes.run", input } + const evidence = { steps: [step], target: { scope: "servers", serverIds: [9] }, requiresHuman: true, verificationScope: "acknowledgement", validUntil: timestamp.toISOString(), mutationCost: 2 } + const source: Row = { operation_id: operationId, connection_id: connectionId, owner_id: ownerId, operation_state: "pending", operation_name: "servers.probes.run", + operation_version: 1, plan_state: "executing", approved_by: "real-human", evidence: { sealed: await encryptSecret(JSON.stringify(evidence)) }, + step_index: null, step_state: null, intended_state: null } + db.sources.set(operationId, source) + db.cancellations.push({ operation_id: operationId, connection_id: connectionId, actor_id: ownerId }) + return { source, operationId, step, input } +} +const collect = (db: Database, options: Partial = {}) => collectLegacyServerProbeSnapshot(db.sql, { deadlineAt: Date.now() + 60000, ...options }) +const directory = await mkdtemp(join(tmpdir(), "mcp-legacy-snapshot-")), previous = process.env.MCP_STATE_DIR +process.env.MCP_STATE_DIR = directory +let checks = 0 +try { + { + const db = database(); const result = await collect(db) + assert.equal(result.complete, true); assert.equal(result.counts.identities, 0); assert.equal(result.migrationReady, false) + assert.equal(result.conversionAllowed, false); assert.equal(result.requiresDatabaseSourceAttestation, true) + assert.equal(result.requiresWriterDrain, true); assert.equal(result.requiresLockedReverification, true) + assert.deepEqual(db.calls.map(row => row.text.match(/probe-legacy-snapshot:([a-z-]+)/)?.[1]), ["transaction", "orphan-stages", "identities"]); checks++ + } + { + const db = database(), rest = await addRest(db); await addMcpCancellation(db) + const result = await collect(db, { batchSize: 1 }) + assert.equal(result.complete, true); assert.deepEqual(result.counts, { identities: 2, verified: 2, unresolved: 0, mcp: 1, rest: 1, anchors: 1, quarantines: 0, cancellations: 1, orphanStages: 0 }) + assert.deepEqual(result.authenticationCounts, { receipt: 1, "approved-source": 1, "identity-context-only": 0 }) + assert.deepEqual(result.reasons, {}); assert.match(result.aggregateDigest, /^[a-f0-9]{64}$/) + for (const value of [rest.executionId, rest.receiptId, rest.owner, rest.requestId, privateValue, "private-inner-ciphertext", "private-mcp-owner", String(db.anchors[0].receipt_ciphertext), String(db.anchors[0].receipt_proof)]) assert.equal(JSON.stringify(result).includes(value), false) + assert.equal(db.calls.filter(row => row.text.includes("diagnostic-cancellation:source")).length, 1) + assert(Object.isFrozen(result) && Object.isFrozen(result.counts) && Object.isFrozen(result.reasons)) + const stable = await collect(db, { batchSize: 1 }); assert.equal(stable.aggregateDigest, result.aggregateDigest) + db.sources.values().next().value!.approved_by = "another-human" + const changed = await collect(db, { batchSize: 1 }); assert.notEqual(changed.aggregateDigest, result.aggregateDigest) + checks++ + } + for (const [isolation, readOnly] of [["read committed", "on"], ["repeatable read", "off"]]) { + const db = database(); db.settings(isolation, readOnly) + const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.reasons.SNAPSHOT_REQUIRED, 1); assert.equal(db.calls.length, 1); checks++ + } + { + const db = database(); db.settings("repeatable read", "on", false) + const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.reasons.SNAPSHOT_REQUIRED, 1) + assert.equal(db.calls.length, 1); assert(db.calls[0].text.includes("transaction_timestamp() { throw Error(privateValue) }) + const result = await collect(db); assert.equal(result.reasons.SQL_UNAVAILABLE, 1); assert.equal(JSON.stringify(result).includes(privateValue), false); checks++ + } + { + const db = database(), controller = new AbortController(); controller.abort() + const result = await collect(db, { signal: controller.signal }); assert.equal(result.reasons.CANCELLED, 1); assert.equal(db.calls.length, 0); checks++ + } + { + const db = database(), controller = new AbortController(); db.onQuery(() => controller.abort()) + const result = await collect(db, { signal: controller.signal }); assert.equal(result.reasons.CANCELLED, 1); assert.equal(db.calls.length, 1); checks++ + } + { + const db = database(), result = await collect(db, { deadlineAt: Date.now() - 1 }) + assert.equal(result.reasons.DEADLINE_EXCEEDED, 1); assert.equal(db.calls.length, 0); checks++ + } + for (const bad of [{ batchSize: 101 }, { totalLimit: 10001 }, { totalLimit: 0 }, { deadlineAt: NaN }]) { + const db = database(), result = await collect(db, bad); assert.equal(result.reasons.INVALID_OPTIONS, 1); assert.equal(db.calls.length, 0); checks++ + } + console.log(`server-probe-legacy-snapshot: ${checks} fixed-SQL read-only checks passed; no database/native calls; conversion always false`) +} finally { + if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous + await unlink(join(directory, "encryption-key")); await rmdir(directory) +} diff --git a/backend/src/services/server-probe-legacy-snapshot.ts b/backend/src/services/server-probe-legacy-snapshot.ts new file mode 100644 index 00000000..8886f4e5 --- /dev/null +++ b/backend/src/services/server-probe-legacy-snapshot.ts @@ -0,0 +1,128 @@ +/** Internal SQL-only observation. Caller owns the bounded READ ONLY REPEATABLE + * READ transaction, connection, rollback and writer drain. No pool or lifecycle + * operations are available here; an observation never authorizes conversion. */ +import { createHash } from "node:crypto" +import { z } from "zod" +import { fingerprint } from "../modules/mcp/security.js" +import { readServerProbeExecutionSource, type ServerProbeExecutionSource } from "../modules/mcp/diagnostic-cancellation-source.js" +import { createLegacyServerProbeVerifier, type LegacyServerProbeReason } from "./server-probe-legacy-verifier.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" + +export interface LegacyServerProbeSnapshotOptions { deadlineAt: number; signal?: AbortSignal; batchSize?: number; totalLimit?: number } +export type LegacyServerProbeSnapshotReason = LegacyServerProbeReason | "SNAPSHOT_REQUIRED" | "INVALID_OPTIONS" | "CANCELLED" | "DEADLINE_EXCEEDED" | "SQL_UNAVAILABLE" | "ROW_LIMIT" | "TOTAL_LIMIT" | "IDENTITY_DRIFT" | "ORPHAN_STAGES" +export interface LegacyServerProbeSnapshotSummary { + version: 1; complete: boolean + counts: { identities: number; verified: number; unresolved: number; mcp: number; rest: number; anchors: number; quarantines: number; cancellations: number; orphanStages: number } + authenticationCounts: { receipt: number; "approved-source": number; "identity-context-only": number } + reasons: Readonly>> + aggregateDigest: string; migrationReady: false; conversionAllowed: false; automaticReplay: false + requiresDatabaseSourceAttestation: true; requiresWriterDrain: true; requiresLockedReverification: true +} +class Stopped extends Error { constructor(readonly reason: LegacyServerProbeSnapshotReason) { super(reason) } } +const identitySchema = z.object({ identity_kind: z.enum(["mcp", "rest"]), execution_id: z.string().uuid() }).strict() + +export async function collectLegacyServerProbeSnapshot(sql: ProbeStorageSql, options: LegacyServerProbeSnapshotOptions): Promise> { + const counts = { identities: 0, verified: 0, unresolved: 0, mcp: 0, rest: 0, anchors: 0, quarantines: 0, cancellations: 0, orphanStages: 0 } + const authenticationCounts = { receipt: 0, "approved-source": 0, "identity-context-only": 0 } + const reasons: Partial> = {}, digest = createHash("sha256") + digest.update("server-probe-legacy-snapshot-observation-v1\n") + const addReason = (reason: LegacyServerProbeSnapshotReason) => { reasons[reason] = (reasons[reason] ?? 0) + 1 } + let complete = false + const deadlineAt = options.deadlineAt, signal = options.signal, batchSize = options.batchSize ?? 100, totalLimit = options.totalLimit ?? 10000 + const fence = () => { + if (signal?.aborted) throw new Stopped("CANCELLED") + if (Date.now() >= deadlineAt) throw new Stopped("DEADLINE_EXCEEDED") + } + const query: ProbeStorageSql["query"] = async (text, values) => { + fence() + let result: Awaited> + try { result = await sql.query(text, values, { signal, timeoutMs: Math.max(1, Math.min(10000, deadlineAt - Date.now())) }) } + catch { fence(); throw new Stopped("SQL_UNAVAILABLE") } + fence() + if (!Array.isArray(result.rows)) throw new Stopped("SQL_UNAVAILABLE") + // Digest all actually returned rows, including sealed source evidence. This + // is an observation checksum, not a source generation/readiness capability. + digest.update(fingerprint({ text, values, rows: result.rows }) + "\n") + return result + } + const unique = (rows: Record[]) => { if (rows.length > 1) throw new Stopped("ROW_LIMIT"); return rows[0] } + try { + if (!Number.isSafeInteger(deadlineAt) || !Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 100 + || !Number.isSafeInteger(totalLimit) || totalLimit < 1 || totalLimit > 10000) throw new Stopped("INVALID_OPTIONS") + const state = unique((await query(`/* probe-legacy-snapshot:transaction */ SELECT + current_setting('transaction_isolation') AS isolation,current_setting('transaction_read_only') AS read_only, + transaction_timestamp()($1::text,$2::uuid) + ORDER BY identity_kind,execution_id LIMIT $3`, [lastKind, lastExecution, take + 1])).rows + if (page.length > take + 1) throw new Stopped("ROW_LIMIT") + for (const rawIdentity of page.slice(0, take)) { + let identity: z.infer + try { identity = identitySchema.parse(rawIdentity) } catch { throw new Stopped("IDENTITY_DRIFT") } + const { identity_kind: kind, execution_id: executionId } = identity + if (executionId !== executionId.toLowerCase() || lastKind !== null && (kind < lastKind || kind === lastKind && executionId <= lastExecution!)) throw new Stopped("IDENTITY_DRIFT") + let anchor = unique((await query(`/* probe-legacy-snapshot:anchor */ SELECT * FROM server_probe_executions + WHERE identity_kind=$1 AND execution_id=$2 LIMIT 2`, [kind, executionId])).rows) + const quarantine = unique((await query(`/* probe-legacy-snapshot:quarantine */ SELECT * FROM server_probe_execution_quarantines + WHERE (identity_kind=$1 AND execution_id=$2) OR receipt_id=$3::uuid LIMIT 2`, [kind, executionId, anchor?.receipt_id ?? null])).rows) + if (!anchor && quarantine) anchor = unique((await query(`/* probe-legacy-snapshot:anchor-receipt */ SELECT * FROM server_probe_executions + WHERE (identity_kind=$1 AND execution_id=$2) OR receipt_id=$3::uuid LIMIT 2`, [kind, executionId, quarantine.receipt_id])).rows) + const cancellation = unique((await query(`/* probe-legacy-snapshot:cancellation */ SELECT * FROM mcp_diagnostic_cancellations + WHERE $1::text='mcp' AND operation_id=$2 LIMIT 2`, [kind, executionId])).rows) + let source: ServerProbeExecutionSource | undefined, sourceFailure: unknown + const loadMcpSource = async (operationId: string) => { + if (operationId !== executionId) throw new Stopped("IDENTITY_DRIFT") + if (source) return source + if (sourceFailure) throw sourceFailure + try { + const owner = unique((await query(`/* probe-legacy-snapshot:owner */ SELECT o.connection_id,c.owner_id + FROM mcp_operations o JOIN mcp_connections c ON c.id=o.connection_id WHERE o.id=$1 LIMIT 2`, [operationId])).rows) + if (!owner || typeof owner.connection_id !== "string" || typeof owner.owner_id !== "string") throw new Stopped("SOURCE_UNAVAILABLE") + source = await readServerProbeExecutionSource({ query }, operationId, { connectionId: owner.connection_id, ownerId: owner.owner_id }) + fence(); digest.update(fingerprint({ sourceDigest: source.sourceDigest }) + "\n") + return source + } catch (failure) { sourceFailure = failure; throw failure } + } + if (kind === "mcp") { + try { await loadMcpSource(executionId) } + catch (failure) { if (failure instanceof Stopped) throw failure; fence() } + } + const receiptId = anchor?.receipt_id ?? quarantine?.receipt_id ?? source?.scope.receiptId + const stageRows = receiptId === undefined ? [] : (await query(`/* probe-legacy-snapshot:stages */ SELECT * FROM server_probe_execution_stages + WHERE receipt_id=$1 ORDER BY stage_index LIMIT 12`, [receiptId])).rows + if (stageRows.length > 11) throw new Stopped("ROW_LIMIT") + fence() + const result = await createLegacyServerProbeVerifier({ loadMcpSource })({ identityKind: kind, executionId, + ...(anchor ? { anchor } : {}), ...(quarantine ? { quarantine } : {}), ...(cancellation ? { cancellation } : {}), stages: stageRows }) + fence() + counts.identities++; counts[kind]++; counts.anchors += Number(!!anchor); counts.quarantines += Number(!!quarantine); counts.cancellations += Number(!!cancellation) + if (result.status === "verified") { counts.verified++; authenticationCounts[result.authentication]++ } + else { counts.unresolved++; addReason(result.reasonCode) } + digest.update(fingerprint({ kind, executionId, result }) + "\n") + lastKind = kind; lastExecution = executionId + } + if (page.length <= take) { complete = true; break } + if (counts.identities === totalLimit) throw new Stopped("TOTAL_LIMIT") + } + } catch (failure) { addReason(failure instanceof Stopped ? failure.reason : "SQL_UNAVAILABLE") } + digest.update(fingerprint({ complete, counts, reasons }) + "\n") + return Object.freeze({ version: 1, complete, counts: Object.freeze(counts), authenticationCounts: Object.freeze(authenticationCounts), reasons: Object.freeze(reasons), + aggregateDigest: digest.digest("hex"), migrationReady: false, conversionAllowed: false, automaticReplay: false, + requiresDatabaseSourceAttestation: true, requiresWriterDrain: true, requiresLockedReverification: true }) +} diff --git a/backend/src/services/server-probe-legacy-source-inventory.ts b/backend/src/services/server-probe-legacy-source-inventory.ts new file mode 100644 index 00000000..cae965ec --- /dev/null +++ b/backend/src/services/server-probe-legacy-source-inventory.ts @@ -0,0 +1,77 @@ +import { createHash } from "node:crypto" +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" + +export const LEGACY_PROBE_RELATIONS = Object.freeze([ + "mcp_diagnostic_cancellations", "server_probe_execution_quarantines", "server_probe_execution_stages", "server_probe_executions", +] as const) +const sourceRelations = ["mcp_connections", "mcp_operation_steps", "mcp_operations", "mcp_plans"] as const +export const LEGACY_PROBE_CUSTODY_RELATIONS = Object.freeze(["schema_migrations", ...LEGACY_PROBE_RELATIONS, ...sourceRelations].sort()) +const custodyRelations = LEGACY_PROBE_CUSTODY_RELATIONS +export const LEGACY_PROBE_CUSTODY_LOCK = `LOCK TABLE ${custodyRelations + .map(relation => `public.${relation}`).join(", ")} IN SHARE MODE NOWAIT` +const markers = MIGRATION_MANIFEST.filter(entry => entry.file < "0037_").map(entry => entry.file.slice(0, -4)) +const rowLimit = 10_000 +const sha256 = (text: string) => createHash("sha256").update(text).digest("hex") +export const LEGACY_STORAGE_TIME_CONTEXT = "pg_catalog.set_config('TimeZone','UTC',true), pg_catalog.set_config('DateStyle','ISO, YMD',true)" + +/** Raw storage evidence on the operator's owned read-only snapshot. */ +export async function captureLegacyProbeStorage(sql: ProbeStorageSql) { + await sql.query(`SELECT ${LEGACY_STORAGE_TIME_CONTEXT}, pg_catalog.set_config('search_path','pg_catalog',true), + pg_catalog.set_config('row_security','off',true), pg_catalog.set_config('extra_float_digits','3',true), + pg_catalog.set_config('bytea_output','hex',true), pg_catalog.set_config('IntervalStyle','postgres',true)`) + const { rows: applied } = await sql.query( + 'SELECT id FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1', [markers.length + 1]) + if (applied.length !== markers.length || applied.some((row, index) => row.id !== markers[index])) { + throw new Error("Exact pre-0037 diagnostic migration markers required") + } + + const tables = [] + let rowCount = 0 + for (const relation of LEGACY_PROBE_RELATIONS) { + const { rows } = await sql.query(`SELECT pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to( + pg_catalog.record_out(t)::text, 'UTF8')), 'hex') AS sha256 FROM ONLY public.${relation} AS t LIMIT $1`, [rowLimit - rowCount + 1]) + rowCount += rows.length + if (rowCount > rowLimit || rows.some(row => typeof row.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(row.sha256))) { + throw new Error("Legacy diagnostic snapshot exceeds capture bounds or is malformed") + } + tables.push(Object.freeze({ relation, rowCount: rows.length, sha256: sha256(rows.map(row => row.sha256).sort().join("")) })) + } + const format = "pg18-legacy-probe-storage-v1" + return Object.freeze({ format, rowCount, tables: Object.freeze(tables), sha256: sha256(JSON.stringify({ format, tables })) }) +} + +/** Hash the exact stored MCP rows for the journal's original execution IDs. + * Call after captureLegacyProbeStorage on the same transaction for its fixed + * record formatting. An absent source stays absent; no new source is inferred. */ +export async function captureLegacyProbeLineage(sql: ProbeStorageSql) { + // The archive hashes ONLY each table. The existing receipt reader's SQL + // would also traverse inheritance, so reject any relation with descendants. + const { rows: relations } = await sql.query(`SELECT c.relname FROM pg_catalog.pg_class c + WHERE c.relnamespace='public'::pg_catalog.regnamespace AND c.relname::text=ANY($1::text[]) + AND c.relkind='r' AND c.relpersistence='p' + AND NOT EXISTS (SELECT FROM pg_catalog.pg_inherits i WHERE i.inhparent=c.oid)`, [custodyRelations]) + if (relations.length !== custodyRelations.length) throw new Error("Exact ordinary legacy custody tables required") + const limit = rowLimit * sourceRelations.length + const { rows } = await sql.query(`WITH identities AS ( + SELECT execution_id AS id FROM ONLY public.server_probe_executions WHERE identity_kind='mcp' + UNION SELECT execution_id FROM ONLY public.server_probe_execution_quarantines WHERE identity_kind='mcp' + UNION SELECT operation_id FROM ONLY public.mcp_diagnostic_cancellations + ), operations AS MATERIALIZED ( + SELECT o.* FROM ONLY public.mcp_operations o JOIN identities i ON i.id=o.id + ), evidence AS ( + SELECT 'operation:' || pg_catalog.record_out(o)::text AS content FROM operations o + UNION ALL SELECT 'connection:' || pg_catalog.record_out(c)::text FROM ONLY public.mcp_connections c + WHERE EXISTS (SELECT FROM operations o WHERE o.connection_id=c.id) + UNION ALL SELECT 'plan:' || pg_catalog.record_out(p)::text FROM ONLY public.mcp_plans p + WHERE EXISTS (SELECT FROM operations o WHERE o.plan_id=p.id) + UNION ALL SELECT 'step:' || pg_catalog.record_out(s)::text FROM ONLY public.mcp_operation_steps s + WHERE EXISTS (SELECT FROM operations o WHERE o.id=s.operation_id) + ) SELECT pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(content, 'UTF8')), 'hex') AS sha256 + FROM evidence LIMIT $1`, [limit + 1]) + if (rows.length > limit || rows.some(row => typeof row.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(row.sha256))) { + throw new Error("Legacy operation lineage exceeds capture bounds or is malformed") + } + const format = "pg18-legacy-probe-lineage-v1" + return Object.freeze({ format, rowCount: rows.length, sha256: sha256(format + "\n" + rows.map(row => row.sha256).sort().join("")) }) +} diff --git a/backend/src/services/server-probe-legacy-verifier.test.ts b/backend/src/services/server-probe-legacy-verifier.test.ts new file mode 100644 index 00000000..76c5f676 --- /dev/null +++ b/backend/src/services/server-probe-legacy-verifier.test.ts @@ -0,0 +1,234 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { mkdtemp, readdir, rm, rmdir } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import type { ServerProbeExecutionSource } from "../modules/mcp/diagnostic-cancellation-source.js" +import { fingerprint } from "../modules/mcp/security.js" +import type { ServerProbeJournalContext } from "./server-probe-journal.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" +import { createLegacyServerProbeVerifier, createLegacyServerProbeVerifierForTests, type LegacyServerProbeBundle, type LegacyServerProbeVerifierCrypto } from "./server-probe-legacy-verifier.js" + +const secret = "PRIVATE_LEGACY_PROVIDER_VALUE" +const stamp = new Date("2020-01-01T00:00:00Z") +async function fixture(kind: "rest" | "mcp" = "rest", terminal = true) { + const executionId = randomUUID(), receiptId = randomUUID(), ciphertexts = new Map() + const context: ServerProbeJournalContext = { identity: kind === "rest" + ? { kind, ownerId: "original-owner", executionId, issuedAt: 1, expiresAt: stamp.getTime(), requestId: randomUUID(), authentication: "jwt" } + : { kind, ownerId: "original-owner", executionId, issuedAt: 1, expiresAt: stamp.getTime(), connectionId: randomUUID(), credentialId: randomUUID(), operationName: "servers.probes.run" }, sourceServerId: 9, tool: "ping" } + const scope = { ownerId: context.identity.ownerId, receiptId, identityKind: kind, executionId, bindingProof: "a".repeat(64), authorityExpiresAt: stamp.toISOString() } + const source: ServerProbeExecutionSource = { scope, sourceDigest: "d".repeat(64), sourceState: "unknown", terminal: true, + serverIds: [9], stageBudget: 2, originalCredentialId: context.identity.kind === "mcp" ? context.identity.credentialId : randomUUID(), + originalContext: context as ServerProbeExecutionSource["originalContext"] } + const seenProofs: { domain: string; isString: boolean }[] = [] + const crypto: LegacyServerProbeVerifierCrypto = { + open: async value => { const opened = ciphertexts.get(value); if (!opened) throw Error(secret); return opened }, + proof: async (domain, value) => { seenProofs.push({ domain, isString: typeof value === "string" }); return createHash("sha256").update(domain + fingerprint(value)).digest("hex") }, + } + const receipt: ServerProbeReceipt = { receiptId, tool: "ping", serverId: 9, state: "completed", httpStatus: 200, + output: secret, data: { kind: "ping" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } + const id = context.identity + const anchor: Record = { receipt_id: receiptId, binding_proof: scope.bindingProof, identity_kind: kind, execution_id: executionId, + actor_id: id.ownerId, authentication: id.kind === "mcp" ? "mcp" : id.authentication, + mcp_connection_id: id.kind === "mcp" ? id.connectionId : null, mcp_credential_id: id.kind === "mcp" ? id.credentialId : null, + request_id: id.kind === "rest" ? id.requestId : null, authority_issued_at: String(id.issuedAt), authority_expires_at: stamp, + source_server_id: 9, destination_server_id: null, tool: "ping", state: terminal ? "completed" : "claimed", + attempted_stages: terminal ? 1 : null, completed_stages: terminal ? 1 : null, receipt_ciphertext: null, receipt_proof: null, + created_at: stamp, finished_at: terminal ? stamp : null } + const stage: Record = { receipt_id: receiptId, stage_index: 1, kind: "native-ping", timeout_ms: 1000, + state: "received", outcome: "received", created_at: stamp, received_at: stamp } + const bundle: LegacyServerProbeBundle = { identityKind: kind, executionId, anchor, stages: terminal ? [stage] : [] } + let loads = 0 + const deps = { loadMcpSource: async (operationId: string) => { loads++; assert.equal(operationId, executionId); return source } } + const verify = createLegacyServerProbeVerifierForTests(deps, crypto) + const refreshReceipt = async (rawContext: unknown = context) => { + const plaintext = JSON.stringify({ version: 1, context: rawContext, bindingProof: scope.bindingProof, receipt }) + const cipher = "fixture-cipher-" + randomUUID(); ciphertexts.set(cipher, plaintext) + anchor.receipt_ciphertext = cipher; anchor.receipt_proof = await crypto.proof("receipt", plaintext) + } + const quarantine = async () => { + bundle.quarantine = { receipt_id: receiptId, binding_proof: scope.bindingProof, identity_kind: kind, execution_id: executionId, + identity_proof: await crypto.proof("identity", context), error_code: "JOURNAL_UNKNOWN", attempted_stages: terminal ? 1 : 0, + completed_stages: terminal ? 1 : 0, created_at: stamp } + } + if (terminal) await refreshReceipt() + return { verify, deps, crypto, seenProofs, source, context, scope, bundle, receipt, anchor, stage, ciphertexts, refreshReceipt, quarantine, loads: () => loads } +} +let checks = 0 +for (const kind of ["rest", "mcp"] as const) { + const f = await fixture(kind), result = await f.verify(f.bundle) + assert.equal(result.status, "verified") + if (result.status !== "verified") throw Error("Expected authenticated receipt") + assert.equal(result.authentication, "receipt"); assert.equal(result.bindingAuthenticated, true); assert.equal(result.progressAuthenticated, true) + assert.equal(result.migrationReady, false); assert.equal(result.stageLedgerAuthenticated, false); assert.equal(result.timestampsAuthenticated, false) + assert.equal(result.originalContext.identity.expiresAt, stamp.getTime()); assert.equal(result.stageCount, 1) + assert.equal(result.requiresDatabaseSourceAttestation, true); assert.equal(result.automaticReplay, false) + assert.equal(JSON.stringify(result).includes(secret), false) + assert.equal(JSON.stringify(result).includes("fixture-cipher-"), false) + assert(Object.isFrozen(result) && Object.isFrozen(result.originalContext) && Object.isFrozen(result.originalContext.identity) && Object.isFrozen(result.scope)) + assert(f.seenProofs.filter(value => value.domain === "receipt").every(value => value.isString)) + assert.equal(f.loads(), kind === "mcp" ? 1 : 0); checks++ +} +{ + const f = await fixture("mcp", false), result = await f.verify(f.bundle) + assert.equal(result.status, "verified") + if (result.status === "verified") { assert.equal(result.authentication, "approved-source"); assert.equal(result.progressAuthenticated, false) } + checks++ +} +{ + const f = await fixture("rest", false) + assert.equal((await f.verify(f.bundle)).status, "unresolved") + await f.quarantine() + const result = await f.verify(f.bundle) + assert.equal(result.status, "verified") + if (result.status === "verified") { + assert.equal(result.authentication, "identity-context-only"); assert.equal(result.bindingAuthenticated, false) + assert.equal(result.progressAuthenticated, false); assert.equal(result.quarantined, true) + } + assert(f.seenProofs.filter(value => value.domain === "identity").every(value => !value.isString)) + // Identity MAC deliberately does not authenticate the binding or timestamp. + f.anchor.binding_proof = "b".repeat(64); f.bundle.quarantine!.binding_proof = "b".repeat(64) + f.bundle.quarantine!.created_at = new Date("2021-01-01T00:00:00Z") + const changed = await f.verify(f.bundle) + assert.equal(changed.status, "verified") + if (changed.status === "verified") { assert.equal(changed.bindingAuthenticated, false); assert.equal(changed.timestampsAuthenticated, false); assert.equal(changed.migrationReady, false) } + delete f.bundle.anchor; assert.equal((await f.verify(f.bundle)).status, "unresolved"); checks++ +} +for (const orphan of ["quarantine", "cancellation"] as const) { + const f = await fixture("mcp", false) + if (orphan === "quarantine") await f.quarantine() + else { + const id = f.context.identity; assert(id.kind === "mcp") + f.bundle.cancellation = { operation_id: id.executionId, connection_id: id.connectionId, actor_id: id.ownerId } + } + delete f.bundle.anchor + const result = await f.verify(f.bundle); assert.equal(result.status, "verified") + if (result.status === "verified") { assert.equal(result.hasAnchor, false); assert.equal(result.authentication, "approved-source"); assert.equal(result.migrationReady, false) } + checks++ +} +{ + const f = await fixture("mcp", false); await f.quarantine(); delete f.bundle.anchor + f.bundle.quarantine!.attempted_stages = 1 + // The sealed source authenticates context, but an absent anchor/stage ledger + // cannot substantiate nonzero orphan progress. Unresolved is insufficient + // evidence for transfer, not a claim that historical data was corrupted. + const result = await f.verify(f.bundle) + assert.deepEqual(result, { status: "unresolved", reasonCode: "QUARANTINE_INVALID", migrationReady: false, automaticReplay: false }) + checks++ +} +{ + const f = await fixture("rest") + f.receipt.state = "failed"; f.receipt.httpStatus = 502; f.receipt.errorCode = "CANCELLED" + delete f.receipt.output; delete f.receipt.data; f.receipt.attemptedStages = 0; f.receipt.completedStages = 0 + Object.assign(f.anchor, { state: "failed", attempted_stages: 0, completed_stages: 0 }); f.bundle.stages = [] + await f.refreshReceipt(); assert.equal((await f.verify(f.bundle)).status, "verified"); checks++ +} +for (const tool of ["dns", "route", "bandwidth", "mtu"] as const) { + const f = await fixture() + f.context.tool = tool; f.receipt.tool = tool; f.receipt.data = { kind: tool }; f.anchor.tool = tool + if (tool === "bandwidth") { + f.context.destinationServerId = 12; f.receipt.destinationServerId = 12; f.anchor.destination_server_id = 12 + } + const kinds = tool === "mtu" ? ["source-dns-A", ...[1500,1492,1480,1476,1472,1468,1400,1280,1024,576].map(size => `native-mtu-${size}`)] + : [tool === "dns" ? "manager-dns-AAAA" : tool === "route" ? "native-route-read" : "native-bandwidth"] + f.bundle.stages = kinds.map((kind, index) => ({ ...f.stage, stage_index: index + 1, kind })) + f.receipt.attemptedStages = kinds.length; f.receipt.completedStages = kinds.length + f.anchor.attempted_stages = kinds.length; f.anchor.completed_stages = kinds.length + await f.refreshReceipt(); assert.equal((await f.verify(f.bundle)).status, "verified") + if (tool === "mtu") { + f.bundle.stages[1].kind = "native-mtu-1492" + assert.equal((await f.verify(f.bundle)).status, "unresolved") + } + checks++ +} +{ + const f = await fixture() + f.bundle.stages = [{ ...f.stage, kind: "source-dns-A", outcome: "source-unavailable" }, { ...f.stage, stage_index: 2 }] + f.receipt.attemptedStages = 2; f.receipt.completedStages = 2; f.anchor.attempted_stages = 2; f.anchor.completed_stages = 2 + await f.refreshReceipt(); assert.equal((await f.verify(f.bundle)).status, "verified"); checks++ +} +type Fixture = Awaited> +const mutations: Array<(f: Fixture) => void | Promise> = [ + f => { f.bundle.executionId = randomUUID() }, f => { f.bundle.identityKind = "mcp" }, + f => { f.anchor.actor_id = "foreign" }, f => { f.anchor.authentication = "local" }, + f => { f.anchor.mcp_connection_id = randomUUID() }, f => { f.anchor.mcp_credential_id = randomUUID() }, + f => { f.anchor.request_id = randomUUID() }, f => { f.anchor.authority_issued_at = "2" }, + f => { f.anchor.authority_expires_at = new Date("2030-01-01T00:00:00Z") }, + f => { f.anchor.source_server_id = 10 }, f => { f.anchor.destination_server_id = 11 }, f => { f.anchor.tool = "dns" }, + f => { f.anchor.receipt_id = randomUUID() }, f => { f.anchor.binding_proof = "b".repeat(64) }, + f => { f.anchor.receipt_proof = "b".repeat(64) }, f => { f.anchor.receipt_ciphertext = "missing-private" }, + async f => { f.anchor.receipt_proof = await f.crypto.proof("receipt", JSON.parse(f.ciphertexts.get(String(f.anchor.receipt_ciphertext))!)) }, + f => { f.anchor.receipt_ciphertext = "x".repeat(262145) }, f => { f.anchor.finished_at = null }, + f => { f.anchor.created_at = "invalid" }, f => { f.anchor.state = "cancelled" }, + f => { f.anchor.attempted_stages = 2 }, f => { f.anchor.completed_stages = 0 }, + async f => { f.receipt.serverId = 10; await f.refreshReceipt() }, + async f => { f.receipt.automaticReplay = true as false; await f.refreshReceipt() }, + async f => { f.receipt.state = "unknown"; await f.refreshReceipt() }, + async f => { f.receipt.errorCode = "PRIVATE_UNKNOWN"; await f.refreshReceipt() }, + async f => { await f.refreshReceipt({ ...f.context, extra: true }) }, + f => { f.bundle.stages = [] }, f => { f.stage.stage_index = 2 }, f => { f.stage.receipt_id = randomUUID() }, + f => { f.stage.timeout_ms = 120001 }, f => { f.stage.timeout_ms = 0 }, f => { f.stage.kind = "native-route-read" }, + f => { f.stage.outcome = "source-unavailable" }, f => { f.stage.received_at = null }, + f => { f.stage.state = "intent" }, f => { f.bundle.stages.push({ ...f.stage }) }, +] +for (const mutate of mutations) { + const f = await fixture(); await mutate(f) + const result = await f.verify(f.bundle) + assert.equal(result.status, "unresolved"); assert.equal(JSON.stringify(result).includes(secret), false); checks++ +} +const sourceMutations: Array<(f: Fixture) => void> = [ + f => { Object.assign(f.source.scope, { ownerId: "foreign" }) }, f => { Object.assign(f.source.scope, { executionId: randomUUID() }) }, + f => { Object.assign(f.source.scope, { bindingProof: "b".repeat(64) }) }, f => { Object.assign(f.source, { originalCredentialId: randomUUID() }) }, + f => { Object.assign(f.source, { serverIds: [10] }) }, f => { Object.assign(f.source, { stageBudget: 1 }) }, + f => { Object.assign(f.source.scope, { authorityExpiresAt: "2030-01-01T00:00:00Z" }) }, +] +for (const mutate of sourceMutations) { + const f = await fixture("mcp"); mutate(f) + assert.equal((await f.verify(f.bundle)).status, "unresolved"); checks++ +} +for (const mutate of [ + (row: Record) => { row.identity_proof = "b".repeat(64) }, + (row: Record) => { row.binding_proof = "b".repeat(64) }, + (row: Record) => { row.execution_id = randomUUID() }, + (row: Record) => { row.identity_kind = "rest" }, + (row: Record) => { row.attempted_stages = 12 }, + (row: Record) => { row.completed_stages = 1 }, +]) { + const f = await fixture("mcp", false); await f.quarantine(); mutate(f.bundle.quarantine!) + assert.equal((await f.verify(f.bundle)).status, "unresolved"); checks++ +} +for (const field of ["operation_id", "connection_id", "actor_id"]) { + const f = await fixture("mcp", false), id = f.context.identity; assert(id.kind === "mcp") + f.bundle.cancellation = { operation_id: id.executionId, connection_id: id.connectionId, actor_id: id.ownerId, [field]: randomUUID() } + assert.equal((await f.verify(f.bundle)).status, "unresolved"); checks++ +} +{ + const f = await fixture("mcp"), verify = createLegacyServerProbeVerifier({ loadMcpSource: async () => { throw Error(secret) } }) + const result = await verify(f.bundle); assert.equal(result.status, "unresolved"); assert.equal(JSON.stringify(result).includes(secret), false); checks++ +} +{ + const f = await fixture(), verify = createLegacyServerProbeVerifierForTests(f.deps, { ...f.crypto, proof: async () => { throw Error(secret) } }) + const result = await verify(f.bundle); assert.equal(result.status, "unresolved"); assert.equal(JSON.stringify(result).includes(secret), false); checks++ +} +{ + const state = await mkdtemp(join(tmpdir(), "mm-legacy-verifier-test-")) + const previousState = process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR = state + try { + const receipt = await fixture() + const quarantine = await fixture("rest", false) + await quarantine.quarantine() + const verify = createLegacyServerProbeVerifier(receipt.deps) + for (const bundle of [receipt.bundle, quarantine.bundle]) { + assert.equal((await verify(bundle)).status, "unresolved") + assert.deepEqual(await readdir(state), [], "Historical reads must leave missing encryption state untouched") + checks++ + } + } finally { + if (previousState === undefined) delete process.env.MCP_STATE_DIR + else process.env.MCP_STATE_DIR = previousState + await rm(join(state, "encryption-key"), { force: true }) + await rmdir(state) + } +} +console.log(`server-probe-legacy-verifier: ${checks} historical authentication checks passed; migration readiness always false`) diff --git a/backend/src/services/server-probe-legacy-verifier.ts b/backend/src/services/server-probe-legacy-verifier.ts new file mode 100644 index 00000000..c52b8eeb --- /dev/null +++ b/backend/src/services/server-probe-legacy-verifier.ts @@ -0,0 +1,169 @@ +/** Internal historical authentication only. No SQL, current grant, native work, + * migration readiness or public payload is produced by this verifier. */ +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import type { ServerProbeExecutionSource } from "../modules/mcp/diagnostic-cancellation-source.js" +import { decryptSecretExisting, fingerprint, privateFingerprinterExisting } from "../modules/mcp/security.js" +import { parseServerProbeJournalContext, validateServerProbeJournalReceipt, type ServerProbeJournalContext } from "./server-probe-journal.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" +import type { ProbeStorageScope } from "./server-probe-journal-maintenance.js" + +export interface LegacyServerProbeBundle { + identityKind: "mcp" | "rest"; executionId: string + anchor?: Record; quarantine?: Record; cancellation?: Record + stages: Record[] +} +export interface LegacyServerProbeVerifierDependencies { loadMcpSource(operationId: string): Promise } +export interface LegacyServerProbeVerifierCrypto { open(value: string): Promise; proof(domain: "receipt" | "identity", value: unknown): Promise } +export type LegacyServerProbeReason = "INVALID_BUNDLE" | "SOURCE_UNAVAILABLE" | "SOURCE_MISMATCH" | "CONTEXT_UNAVAILABLE" | "ANCHOR_MISMATCH" | "RECEIPT_INVALID" | "QUARANTINE_INVALID" | "CANCELLATION_INVALID" | "STAGES_INVALID" +export type LegacyServerProbeVerification = { + status: "verified"; originalContext: Readonly; scope: Readonly + authentication: "receipt" | "approved-source" | "identity-context-only" + quarantined: boolean; cancelled: boolean; hasAnchor: boolean; terminal: boolean; stageCount: number + bindingAuthenticated: boolean; progressAuthenticated: boolean; stageLedgerAuthenticated: false; timestampsAuthenticated: false + migrationReady: false; requiresDatabaseSourceAttestation: true; automaticReplay: false +} | { status: "unresolved"; reasonCode: LegacyServerProbeReason; migrationReady: false; automaticReplay: false } + +const uuid = z.string().uuid(), hash = z.string().regex(/^[a-f0-9]{64}$/) +const record = z.record(z.string(), z.unknown()) +const bundleSchema = z.object({ identityKind: z.enum(["mcp", "rest"]), executionId: uuid, + anchor: record.optional(), quarantine: record.optional(), cancellation: record.optional(), stages: z.array(record).max(11) }).strict() +const envelopeSchema = z.object({ version: z.literal(1), context: z.unknown(), bindingProof: hash, receipt: z.unknown() }).strict() +const countSchema = z.object({ attempted: z.number().int().min(0).max(11), completed: z.number().int().min(0).max(11) }) + .refine(value => value.completed <= value.attempted) +const sizes = [1500, 1492, 1480, 1476, 1472, 1468, 1400, 1280, 1024, 576] +class Unresolved extends Error { constructor(readonly code: LegacyServerProbeReason) { super(code) } } +function requireFact(value: unknown, code: LegacyServerProbeReason): asserts value { if (!value) throw new Unresolved(code) } +const time = (value: unknown) => value instanceof Date ? value.getTime() : typeof value === "string" ? Date.parse(value) : NaN +function validTime(value: unknown, code: LegacyServerProbeReason) { requireFact(Number.isFinite(time(value)), code) } +function context(raw: unknown) { + const parsed = parseServerProbeJournalContext(raw as ServerProbeJournalContext) + requireFact(isDeepStrictEqual(raw, parsed) && parsed.identity.expiresAt > parsed.identity.issuedAt, "CONTEXT_UNAVAILABLE") + return parsed +} +function anchorContext(row: Record, executionId: string) { + requireFact(row.identity_kind === "rest" && row.execution_id === executionId && ["jwt", "local"].includes(String(row.authentication)) + && row.mcp_connection_id === null && row.mcp_credential_id === null, "ANCHOR_MISMATCH") + const issuedAt = Number(row.authority_issued_at) + requireFact(String(issuedAt) === String(row.authority_issued_at), "ANCHOR_MISMATCH") + return context({ identity: { kind: "rest", ownerId: row.actor_id, executionId, + requestId: row.request_id, authentication: row.authentication, issuedAt, expiresAt: time(row.authority_expires_at) }, + sourceServerId: row.source_server_id, ...(row.destination_server_id !== null ? { destinationServerId: row.destination_server_id } : {}), tool: row.tool }) +} +function checkAnchor(row: Record, ctx: ServerProbeJournalContext, scope: ProbeStorageScope) { + const id = ctx.identity + requireFact(row.receipt_id === scope.receiptId && row.binding_proof === scope.bindingProof && row.identity_kind === id.kind + && row.execution_id === id.executionId && row.actor_id === id.ownerId && row.authentication === (id.kind === "mcp" ? "mcp" : id.authentication) + && row.mcp_connection_id === (id.kind === "mcp" ? id.connectionId : null) && row.mcp_credential_id === (id.kind === "mcp" ? id.credentialId : null) + && row.request_id === (id.kind === "rest" ? id.requestId : null) && String(row.authority_issued_at) === String(id.issuedAt) + && time(row.authority_expires_at) === id.expiresAt && row.source_server_id === ctx.sourceServerId + && row.destination_server_id === (ctx.destinationServerId ?? null) && row.tool === ctx.tool, "ANCHOR_MISMATCH") + validTime(row.created_at, "ANCHOR_MISMATCH") + if (row.state === "claimed") { + requireFact(row.receipt_ciphertext === null && row.receipt_proof === null && row.finished_at === null + && row.attempted_stages === null && row.completed_stages === null, "ANCHOR_MISMATCH") + } else { + requireFact(["completed", "failed", "cancelled"].includes(String(row.state)), "ANCHOR_MISMATCH") + validTime(row.finished_at, "ANCHOR_MISMATCH") + } +} +function stages(rows: Record[], ctx: ServerProbeJournalContext, receiptId: string, hasAnchor: boolean) { + requireFact(hasAnchor || rows.length === 0, "STAGES_INVALID") + let received = 0, native = 0 + for (const [index, row] of rows.entries()) { + requireFact(row.receipt_id === receiptId && row.stage_index === index + 1 && received === index + && Number.isInteger(row.timeout_ms) && Number(row.timeout_ms) >= 1 + && Number(row.timeout_ms) <= ({ ping: 120000, traceroute: 58000, mtu: 25000, route: 10000, bandwidth: 30000, dns: 30000 }[ctx.tool]), "STAGES_INVALID") + if (row.kind === "source-dns-A") requireFact(["ping", "traceroute", "mtu"].includes(ctx.tool) && index === 0 && Number(row.timeout_ms) <= 10000, "STAGES_INVALID") + else { + const expected = { ping: "native-ping", traceroute: "native-traceroute", bandwidth: "native-bandwidth", route: "native-route-read" } + requireFact(ctx.tool === "mtu" ? native < sizes.length && row.kind === `native-mtu-${sizes[native]}` + : native === 0 && (ctx.tool === "dns" ? typeof row.kind === "string" && /^manager-dns-(A|AAAA|MX|NS|TXT|CNAME|PTR)$/.test(row.kind) : row.kind === expected[ctx.tool]), "STAGES_INVALID") + native++ + } + validTime(row.created_at, "STAGES_INVALID") + if (row.state === "received") { + requireFact(row.outcome === "received" || row.outcome === "source-unavailable" && row.kind === "source-dns-A", "STAGES_INVALID") + validTime(row.received_at, "STAGES_INVALID"); received++ + } else requireFact(row.state === "intent" && row.outcome === null && row.received_at === null, "STAGES_INVALID") + } + return received +} +// Inspection must never recreate missing application key state. +const productionCrypto: LegacyServerProbeVerifierCrypto = { open: decryptSecretExisting, + proof: async (domain, value) => (await privateFingerprinterExisting("server-probe-journal-v1:" + domain))(fingerprint(value)) } + +function verifier(dependencies: LegacyServerProbeVerifierDependencies, crypto: LegacyServerProbeVerifierCrypto) { + return async (raw: LegacyServerProbeBundle): Promise => { + let phase: LegacyServerProbeReason = "INVALID_BUNDLE" + try { + const bundle = bundleSchema.parse(structuredClone(raw)), { anchor, quarantine, cancellation } = bundle + requireFact(anchor || quarantine || cancellation, "INVALID_BUNDLE") + let original: ServerProbeJournalContext | undefined, source: ServerProbeExecutionSource | undefined + let receipt: ServerProbeReceipt | undefined, receiptId: string | undefined, bindingProof: string | undefined + if (bundle.identityKind === "mcp") { + phase = "SOURCE_UNAVAILABLE"; source = await dependencies.loadMcpSource(bundle.executionId) + phase = "SOURCE_MISMATCH"; original = context(source.originalContext) + const id = original.identity + requireFact(id.kind === "mcp" && id.executionId === bundle.executionId && id.operationName === "servers.probes.run" + && source.originalCredentialId === id.credentialId && source.scope.identityKind === "mcp" && source.scope.executionId === id.executionId + && source.scope.ownerId === id.ownerId && time(source.scope.authorityExpiresAt) === id.expiresAt + && isDeepStrictEqual(source.serverIds, [...new Set([original.sourceServerId, ...(original.destinationServerId !== undefined ? [original.destinationServerId] : [])])].sort((a,b) => a-b)) + && source.stageBudget === (original.tool === "mtu" ? 11 : ["ping", "traceroute"].includes(original.tool) ? 2 : 1), "SOURCE_MISMATCH") + receiptId = uuid.parse(source.scope.receiptId); bindingProof = hash.parse(source.scope.bindingProof) + } + if (anchor && anchor.state !== "claimed") { + phase = "RECEIPT_INVALID" + requireFact(typeof anchor.receipt_ciphertext === "string" && Buffer.byteLength(anchor.receipt_ciphertext) > 0 + && Buffer.byteLength(anchor.receipt_ciphertext) <= 262144 && hash.safeParse(anchor.receipt_proof).success, phase) + const plaintext = await crypto.open(anchor.receipt_ciphertext) + requireFact(Buffer.byteLength(plaintext) <= 150000 && await crypto.proof("receipt", plaintext) === anchor.receipt_proof, phase) + const envelope = envelopeSchema.parse(JSON.parse(plaintext)), fromReceipt = context(envelope.context) + requireFact(fromReceipt.identity.kind === bundle.identityKind && fromReceipt.identity.executionId === bundle.executionId + && (!original || isDeepStrictEqual(original, fromReceipt)), phase) + original = fromReceipt + requireFact((receiptId === undefined || receiptId === anchor.receipt_id) && (bindingProof === undefined || bindingProof === envelope.bindingProof), phase) + receiptId = uuid.parse(anchor.receipt_id); bindingProof = envelope.bindingProof + receipt = validateServerProbeJournalReceipt(original, receiptId, envelope.receipt as ServerProbeReceipt) + requireFact(anchor.state === receipt.state && anchor.attempted_stages === receipt.attemptedStages && anchor.completed_stages === receipt.completedStages, phase) + } + if (!original && anchor && quarantine) { phase = "CONTEXT_UNAVAILABLE"; original = anchorContext(anchor, bundle.executionId) } + requireFact(original, "CONTEXT_UNAVAILABLE") + if (receiptId === undefined) { receiptId = uuid.parse(anchor?.receipt_id); bindingProof = hash.parse(anchor?.binding_proof) } + requireFact(bindingProof, "CONTEXT_UNAVAILABLE") + const scope: ProbeStorageScope = { ownerId: original.identity.ownerId, receiptId, bindingProof, identityKind: original.identity.kind, + executionId: original.identity.executionId, authorityExpiresAt: new Date(original.identity.expiresAt).toISOString() } + phase = "ANCHOR_MISMATCH"; if (anchor) checkAnchor(anchor, original, scope) + phase = "STAGES_INVALID"; const received = stages(bundle.stages, original, receiptId, !!anchor) + if (receipt) requireFact(received === receipt.completedStages && receipt.attemptedStages <= bundle.stages.length + && bundle.stages.length - receipt.attemptedStages <= 1 + && (receipt.state !== "completed" || receipt.attemptedStages === bundle.stages.length), phase) + if (quarantine) { + phase = "QUARANTINE_INVALID" + requireFact(quarantine.receipt_id === receiptId && quarantine.binding_proof === bindingProof && quarantine.identity_kind === original.identity.kind + && quarantine.execution_id === original.identity.executionId && hash.safeParse(quarantine.identity_proof).success + && quarantine.identity_proof === await crypto.proof("identity", original) + && typeof quarantine.error_code === "string" && /^[A-Z_]{1,64}$/.test(quarantine.error_code), phase) + const counts = countSchema.parse({ attempted: quarantine.attempted_stages, completed: quarantine.completed_stages }) + requireFact(counts.attempted <= bundle.stages.length && counts.completed <= received && bundle.stages.length - counts.attempted <= 1, phase) + validTime(quarantine.created_at, phase) + } + if (cancellation) { + phase = "CANCELLATION_INVALID"; const id = original.identity + requireFact(id.kind === "mcp" && cancellation.operation_id === id.executionId && cancellation.connection_id === id.connectionId && cancellation.actor_id === id.ownerId, phase) + } + const authentication = receipt ? "receipt" : source ? "approved-source" : "identity-context-only" + requireFact(authentication !== "identity-context-only" || quarantine, "CONTEXT_UNAVAILABLE") + Object.freeze(original.identity); Object.freeze(original) + return Object.freeze({ status: "verified", originalContext: original, scope: Object.freeze(scope), authentication, + quarantined: !!quarantine, cancelled: !!cancellation, hasAnchor: !!anchor, terminal: !!receipt || !!source?.terminal, + stageCount: bundle.stages.length, bindingAuthenticated: !!receipt || !!source, progressAuthenticated: !!receipt, + stageLedgerAuthenticated: false, timestampsAuthenticated: false, migrationReady: false, requiresDatabaseSourceAttestation: true, automaticReplay: false }) + } catch (failure) { + return Object.freeze({ status: "unresolved", reasonCode: failure instanceof Unresolved ? failure.code : phase, migrationReady: false, automaticReplay: false }) + } + } +} +export const createLegacyServerProbeVerifier = (dependencies: LegacyServerProbeVerifierDependencies) => verifier(dependencies, productionCrypto) +/** Test-only codec composition; never populated from HTTP/MCP request JSON. */ +export const createLegacyServerProbeVerifierForTests = verifier diff --git a/backend/src/services/server-probe-operation-retirement.owned.test.ts b/backend/src/services/server-probe-operation-retirement.owned.test.ts new file mode 100644 index 00000000..5b1e9fde --- /dev/null +++ b/backend/src/services/server-probe-operation-retirement.owned.test.ts @@ -0,0 +1,323 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import type { PoolClient } from "pg" +import { assertOwnedBandwidthSql, prepareOwnedBandwidthState } from "./bandwidth-admission-process.sql-helper.js" + +assertOwnedBandwidthSql() +assert.equal(process.env.MMAPP_OWN_PROBE_OPERATION_RETIREMENT, "true") +await prepareOwnedBandwidthState() +const { pool, closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") +const { authenticateMcpToken, createMcpConnection } = await import("../modules/mcp/store.js") +const { applyPlan, invokeMcpTool } = await import("../modules/mcp/runtime.js") +const { encryptSecret, McpError } = await import("../modules/mcp/security.js") +const { serverProbeRetiredFixture } = await import("./server-probe-retired-source.fixture.js") +const { capturePublishedServerProbeTerminalProof, captureServerProbeTerminalProof, collectExpiredRetiredServerProbe, retireCompletedServerProbeOperation, + retireExpiredServerProbeOperation } = await import("./server-probe-operation-retirement.js") +const { createProbeJournalStorage, ProbeStorageRefused } = await import("./server-probe-journal-maintenance.js") +const { PARTITION_WRITER_ADVISORY_KEY } = await import("./partition-write-transaction.js") +const { connectProbeStorage } = await import("./server-probe-storage-connection.js") +const { PROBE_STORAGE_LIMITS } = await import("../modules/mcp/diagnostic-cancellation-storage.js") +const { createServerProbeRetentionForTests, retainServerProbeStorage } = await import("./server-probe-retention.js") +const { tickMcpWorkers } = await import("../modules/mcp/workers.js") +const { Client } = await import("pg") + +async function assertWaitsBeforeSecondary(start:()=>Promise, checkLocks:(sql:PoolClient)=>Promise, checkUnchanged:(sql:PoolClient)=>Promise) { + const exclusive = await pool.connect() + let blocked:Promise|undefined + try { + await exclusive.query("SELECT pg_advisory_lock($1::bigint)",[PARTITION_WRITER_ADVISORY_KEY]) + await exclusive.query("BEGIN") + blocked = start() + void blocked.catch(() => {}) + let waiting = false + for (const deadline=Date.now()+3000; !waiting&&Date.now() {}) } finally { + try { + await exclusive.query("ROLLBACK") + assert.equal((await exclusive.query("SELECT pg_advisory_unlock($1::bigint) AS unlocked",[PARTITION_WRITER_ADVISORY_KEY])).rows[0].unlocked,true) + } finally { exclusive.release(true) } + } + } +} + +try { + await applySqlMigrations(pool) + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + await pool.query("INSERT INTO servers(id,name,host,enabled) VALUES(9,'owned retirement source','127.0.0.1',true)") + async function access() { + const actor = { id: randomUUID(), name: "Owned diagnostic retention", email: "owned-retention@example.test", apps: ["mm"], + permissions: ["mm:servers:read", "mm:servers:write", "mm:mcp:approve"], issuedAt: Date.now()-1000, expiresAt: Date.now()+300000 } + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: actor.name, mode: "approval", permissions: actor.permissions, + expiresAt: new Date(actor.expiresAt).toISOString(), scope: { serverIds: [9], + operationIds: ["servers.probes.run", "servers.probes.receipt.read", "operations.read", "changes.apply"], allowGlobal: false }, + limits: { callsPerMinute: 100, mutationsPerHour: 10 } }), actor) + return { actor, principal: await authenticateMcpToken(issued.token) } + } + async function verifyLifecycle(mode: "retained" | "purged" | "default-old" | "default-young" | "default-unproven" | "default-spent" | "scheduled-retained") { + const { actor, principal } = await access(), foreign = (await access()).principal + const historical = mode === "retained" ? undefined : { + base: Date.now()-(mode === "default-young" ? 60 : 200)*86400000, retentionDays:mode === "scheduled-retained" ? 365 : 30, + } + const { raw } = await serverProbeRetiredFixture(false, { connectionId: principal.connectionId, credentialId: principal.credentialId, ownerId: actor.id }, historical) + const { original: o, admission: a } = raw, key = randomUUID(), empty = await encryptSecret("[]") + const target = { receiptId: a.receipt_id, operationId: o.operation_id, connectionId: o.connection_id } + const request = { operationId: o.operation_id, receiptId: a.receipt_id, page: 0 } + const own = await pool.connect() + try { + await own.query("BEGIN") + // Only this disposable database gets synthetic historical SQL rows. The + // actual codec signs the historical originals with its owned state key. + await own.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by,approved_at,created_at) + VALUES($1,$2,$3,1,1,'owned historical test','{}',$4,'completed',$5,$6,$7,$8)`, + [o.plan_id,o.connection_id,o.operation_name,o.evidence,o.plan_expires_at,o.owner_id,o.plan_approved_at,o.plan_created_at]) + await own.query(`INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,verification,result,created_at,finished_at) + VALUES($1,$2,$3,$4,'applied','inconclusive',$5,$6,$7)`, [o.operation_id,o.plan_id,o.connection_id,key,o.operation_result,o.operation_created_at,o.operation_finished_at]) + await own.query(`INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state,result,created_at,finished_at) + VALUES($1,0,'acknowledged',$2,$3,$4,$5)`, [o.operation_id,o.intended_state,o.step_result,o.step_created_at,o.step_finished_at]) + await own.query("INSERT INTO mcp_operation_checkpoints(operation_id,step_index,before_sealed,after_sealed) VALUES($1,0,$2,$2)", [o.operation_id,empty]) + await own.query("SET LOCAL session_replication_role='replica'") + await own.query("INSERT INTO server_probe_storage_admissions SELECT * FROM jsonb_populate_record(NULL::server_probe_storage_admissions,$1)", [a]) + await own.query("INSERT INTO server_probe_executions SELECT * FROM jsonb_populate_record(NULL::server_probe_executions,$1)", [raw.anchor]) + await own.query("INSERT INTO server_probe_execution_payloads SELECT * FROM jsonb_populate_record(NULL::server_probe_execution_payloads,$1)", [raw.payload]) + await own.query("INSERT INTO server_probe_execution_stages SELECT * FROM jsonb_populate_record(NULL::server_probe_execution_stages,$1)", [raw.stages[0]]) + await own.query("UPDATE server_probe_storage_usage SET admissions=1,reserved_files=1,reserved_bytes=294912 WHERE scope='global'") + await own.query("INSERT INTO server_probe_storage_usage VALUES($1,1,1,294912)", ["owner:"+actor.id]) + await own.query("SET LOCAL session_replication_role='origin'") + await own.query("COMMIT") + } finally { own.release(true) } + const live = await invokeMcpTool(principal, "mm_server_probe_receipt_get", request) + const storage = createProbeJournalStorage({ connect:connectProbeStorage, policy:{ ...PROBE_STORAGE_LIMITS,retentionDays:30 }, safetyWindowMs:60000, + async authorize(ownerId,operation,scope) { + assert.equal(ownerId,actor.id); assert.equal(operation,"purge"); assert.equal(scope,undefined) + return "a".repeat(64) + } }) + if (mode === "default-young" || mode === "default-old" || mode === "default-unproven" || mode === "default-spent") { + await assert.rejects(retireExpiredServerProbeOperation(target)) + if (mode === "default-old") { + const storageState = async (sql: Pick) => (await sql.query(`SELECT + (SELECT to_jsonb(a) FROM public.server_probe_storage_admissions a WHERE receipt_id=$1) AS admission, + (SELECT receipt_proof FROM public.server_probe_execution_payloads WHERE receipt_id=$1) AS payload_proof, + (SELECT jsonb_agg(to_jsonb(u) ORDER BY u.scope) FROM public.server_probe_storage_usage u + WHERE u.scope IN ('global',$2)) AS counters`,[target.receiptId,"owner:"+actor.id])).rows[0] + const beforeExpiry = await storageState(pool) + assert(beforeExpiry.admission && beforeExpiry.payload_proof) + // Isolate the real retention transaction from its independent + // best-effort pre-capture, which takes its own common writer lock. + const collect = createServerProbeRetentionForTests(connectProbeStorage) + await assertWaitsBeforeSecondary(collect, async exclusive => { + await exclusive.query("LOCK TABLE public.mcp_settings IN ACCESS EXCLUSIVE MODE NOWAIT") + await exclusive.query("LOCK TABLE public.server_probe_storage_usage IN ACCESS EXCLUSIVE MODE NOWAIT") + }, async exclusive => assert.deepEqual(await storageState(exclusive),beforeExpiry)) + } + if (mode === "default-unproven") { + // This original still qualifies for normal payload expiry, but cannot + // be authenticated as a dependency-free retirement source. + await pool.query("UPDATE mcp_operation_checkpoints SET after_sealed=$2 WHERE operation_id=$1", + [o.operation_id,await encryptSecret('["held"]')]) + } + assert.deepEqual(await retainServerProbeStorage(), { skipped:false,owners:1,collected:1 }) + assert.equal((await pool.query("SELECT reserved_bytes::int AS bytes FROM server_probe_storage_usage WHERE scope='global'")).rows[0].bytes,0) + assert.equal((await pool.query("SELECT 1 FROM server_probe_terminal_proofs WHERE operation_id=$1",[o.operation_id])).rows.length, + mode === "default-unproven" ? 0 : 1) + await assert.rejects(invokeMcpTool(principal,"mm_server_probe_receipt_get",request)) + await assert.rejects(captureServerProbeTerminalProof(target)) + await assert.rejects(retireCompletedServerProbeOperation(target)) + if (mode === "default-spent") { + const spent = (await pool.query(`INSERT INTO server_probe_retirement_claims + (receipt_id,operation_id,plan_id,connection_id,source_proof,claim_token) + SELECT receipt_id,operation_id,$2,$3,source_proof,$4 FROM server_probe_terminal_proofs WHERE operation_id=$1 + RETURNING claim_token`,[o.operation_id,o.plan_id,o.connection_id,randomUUID()])).rows[0] + assert.ok(spent) + await tickMcpWorkers() + await tickMcpWorkers() + assert.deepEqual((await pool.query("SELECT claim_token FROM server_probe_retirement_claims WHERE receipt_id=$1",[a.receipt_id])).rows,[spent]) + assert.equal((await pool.query("SELECT 1 FROM mcp_operations WHERE id=$1",[o.operation_id])).rows.length,1) + assert.equal((await pool.query("SELECT 1 FROM mcp_operation_replay_tombstones WHERE operation_id=$1",[o.operation_id])).rows.length,0) + return + } + if (mode !== "default-old") { + await tickMcpWorkers() + await assert.rejects(retireExpiredServerProbeOperation(target)) + assert.equal((await pool.query("SELECT 1 FROM server_probe_retirement_claims WHERE receipt_id=$1",[a.receipt_id])).rows.length,0) + assert.equal((await pool.query("SELECT 1 FROM mcp_operations WHERE id=$1",[o.operation_id])).rows.length,1) + return + } + await pool.query("UPDATE mcp_operations SET result=jsonb_set(result,'{confirmedWrites}','1') WHERE id=$1",[o.operation_id]) + await tickMcpWorkers() + assert.equal((await pool.query("SELECT 1 FROM server_probe_retirement_claims WHERE receipt_id=$1",[a.receipt_id])).rows.length,0) + await pool.query("UPDATE mcp_operations SET result=$2 WHERE id=$1",[o.operation_id,o.operation_result]) + await tickMcpWorkers() + const absence = (await pool.query(`SELECT + NOT EXISTS(SELECT 1 FROM mcp_operations WHERE id=$1) AS op, + NOT EXISTS(SELECT 1 FROM mcp_plans WHERE id=$2) AS plan, + NOT EXISTS(SELECT 1 FROM mcp_operation_steps WHERE operation_id=$1) AS step, + NOT EXISTS(SELECT 1 FROM mcp_operation_checkpoints WHERE operation_id=$1) AS checkpoint, + NOT EXISTS(SELECT 1 FROM server_probe_terminal_proofs WHERE operation_id=$1) AS proof, + NOT EXISTS(SELECT 1 FROM server_probe_retired_sources WHERE operation_id=$1) AS archive`,[o.operation_id,o.plan_id])).rows[0] + assert.deepEqual(absence,{op:true,plan:true,step:true,checkpoint:true,proof:true,archive:true}) + await assert.rejects(applyPlan(principal,o.plan_id,key),error=>error instanceof McpError&&error.code==="OPERATION_HISTORY_EXPIRED") + await assert.rejects(invokeMcpTool(foreign,"mm_operation_get",{operationId:o.operation_id}),error=>error instanceof McpError&&error.code==="NOT_FOUND") + return + } + await pool.query("UPDATE mcp_operation_checkpoints SET after_sealed=$2 WHERE operation_id=$1", [o.operation_id,await encryptSecret('["held"]')]) + await assert.rejects(retireCompletedServerProbeOperation(target)) + assert.equal((await pool.query("SELECT 1 FROM server_probe_retirement_claims WHERE receipt_id=$1", [a.receipt_id])).rows.length, 0) + await pool.query("UPDATE mcp_operation_checkpoints SET after_sealed=$2 WHERE operation_id=$1", [o.operation_id,empty]) + if (mode === "scheduled-retained") { + await tickMcpWorkers() + await tickMcpWorkers() + assert.equal((await pool.query("SELECT 1 FROM mcp_operations WHERE id=$1",[o.operation_id])).rows.length,0) + assert.equal((await pool.query("SELECT 1 FROM server_probe_retired_sources WHERE receipt_id=$1",[a.receipt_id])).rows.length,1) + assert.deepEqual(await invokeMcpTool(principal,"mm_server_probe_receipt_get",request),live) + await assert.rejects(applyPlan(principal,o.plan_id,key),error=>error instanceof McpError&&error.code==="OPERATION_HISTORY_EXPIRED") + return + } + if (mode === "retained") { + const assertOriginalUnlocked = async (exclusive: PoolClient) => { + assert.equal((await exclusive.query("SELECT pg_try_advisory_xact_lock(hashtextextended($1,0)) AS locked",["mcp-operation:"+target.operationId])).rows[0].locked,true) + assert.equal((await exclusive.query("SELECT id FROM public.mcp_connections WHERE id=$1 FOR UPDATE NOWAIT",[target.connectionId])).rows.length,1) + assert.equal((await exclusive.query("SELECT id FROM public.mcp_operations WHERE id=$1 FOR UPDATE NOWAIT",[target.operationId])).rows.length,1) + } + const captureState = async (sql: Pick) => (await sql.query(`SELECT to_jsonb(o) AS operation, + to_char(o.finished_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS finished_at, + (SELECT receipt_id FROM public.server_probe_terminal_proofs WHERE operation_id=o.id) AS proof_receipt + FROM public.mcp_operations o WHERE id=$1`,[target.operationId])).rows[0] + const beforeCapture = await captureState(pool) + assert.equal(beforeCapture.proof_receipt,null) + const published = { ...target, finishedAt:beforeCapture.finished_at } + await assertWaitsBeforeSecondary(() => capturePublishedServerProbeTerminalProof(published),assertOriginalUnlocked, + async exclusive => assert.deepEqual(await captureState(exclusive),beforeCapture)) + assert.deepEqual(await capturePublishedServerProbeTerminalProof(published),{captured:true,automaticReplay:false}) + assert.deepEqual(await captureState(pool),{...beforeCapture,proof_receipt:target.receiptId}) + assert.deepEqual(await invokeMcpTool(principal,"mm_server_probe_receipt_get",request),live) + await assertWaitsBeforeSecondary(() => retireCompletedServerProbeOperation(target), assertOriginalUnlocked, async exclusive => { + const {rows:[state]} = await exclusive.query(`SELECT + EXISTS(SELECT 1 FROM mcp_operations WHERE id=$1) AS live, + EXISTS(SELECT 1 FROM server_probe_retirement_claims WHERE receipt_id=$2) AS claim, + EXISTS(SELECT 1 FROM server_probe_retired_sources WHERE receipt_id=$2) AS archive, + EXISTS(SELECT 1 FROM mcp_operation_replay_tombstones WHERE operation_id=$1) AS tombstone`,[target.operationId,target.receiptId]) + assert.deepEqual(state,{live:true,claim:false,archive:false,tombstone:false}) + }) + const originalQuery = Client.prototype.query, admissionOrder:string[] = [] + try { + Client.prototype.query = function(this:InstanceType,...args:unknown[]) { + const text = (args[0] as {text?:string})?.text + if (text === "SELECT pg_advisory_xact_lock_shared($1::bigint)") admissionOrder.push("common") + if (text === "SELECT pg_try_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0)) AS locked") admissionOrder.push("storage") + return Reflect.apply(originalQuery,this,args) + } as typeof Client.prototype.query + assert.deepEqual(await retireCompletedServerProbeOperation(target), { retired:true,automaticReplay:false }) + } finally { Client.prototype.query = originalQuery } + assert.deepEqual(admissionOrder,["common","storage","common","storage"]) + } else assert.deepEqual(await retireCompletedServerProbeOperation(target), { retired:true,automaticReplay:false }) + const proof = (await pool.query(`SELECT + NOT EXISTS(SELECT 1 FROM mcp_operations WHERE id=$1) AS op, + NOT EXISTS(SELECT 1 FROM mcp_plans WHERE id=$2) AS plan, + NOT EXISTS(SELECT 1 FROM mcp_operation_steps WHERE operation_id=$1) AS step, + NOT EXISTS(SELECT 1 FROM mcp_operation_checkpoints WHERE operation_id=$1) AS checkpoint, + (SELECT receipt_ciphertext=$4 FROM server_probe_execution_payloads WHERE receipt_id=$3) AS payload, + (SELECT reserved_bytes=294912 FROM server_probe_storage_usage WHERE scope='global') AS quota`, + [o.operation_id,o.plan_id,a.receipt_id,raw.payload.receipt_ciphertext])).rows[0] + assert.deepEqual(proof, { op:true,plan:true,step:true,checkpoint:true,payload:true,quota:true }) + assert.deepEqual(await invokeMcpTool(principal, "mm_server_probe_receipt_get", request), live) + await assert.rejects(invokeMcpTool(foreign, "mm_server_probe_receipt_get", request)) + await assert.rejects(applyPlan(principal,o.plan_id,key), error => error instanceof McpError && error.code==="OPERATION_HISTORY_EXPIRED" && error.statusCode===410) + await assert.rejects(applyPlan(principal,randomUUID(),key), error => error instanceof McpError && error.code==="STATE_CONFLICT" && error.statusCode===409) + await assert.rejects(retireCompletedServerProbeOperation(target)) + const gc = await pool.connect() + try { + await gc.query("BEGIN") + await gc.query("SET LOCAL mm.probe_storage_gc='on'") + assert.equal((await gc.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [a.receipt_id])).rows[0].allowed, false) + await gc.query("ROLLBACK") + } finally { gc.release(true) } + const protectedWrite = await pool.connect() + try { + await protectedWrite.query("BEGIN") + const {rows:[archive]} = await protectedWrite.query("SELECT proof FROM server_probe_retired_sources WHERE receipt_id=$1",[a.receipt_id]) + await protectedWrite.query(`SELECT set_config('mm.probe_storage_gc','on',true),set_config('mm.probe_retired_gc_receipt',$1,true), + set_config('mm.probe_retired_gc_proof',$2,true)`, [a.receipt_id,archive.proof]) + await assert.rejects(protectedWrite.query("DELETE FROM server_probe_retired_sources WHERE receipt_id=$1",[a.receipt_id]),/immutable/) + await protectedWrite.query("ROLLBACK") + } finally { protectedWrite.release(true) } + const raced = await pool.connect() + try { + await raced.query("BEGIN") + await raced.query("SELECT receipt_id FROM server_probe_retired_sources WHERE receipt_id=$1 FOR UPDATE",[a.receipt_id]) + await assert.rejects(collectExpiredRetiredServerProbe(target)) + await raced.query("ROLLBACK") + } finally { raced.release(true) } + assert.deepEqual(await invokeMcpTool(principal, "mm_server_probe_receipt_get", request),live) + if (mode === "purged") { + await pool.query("UPDATE mcp_settings SET requests_retention_days=365 WHERE id=1") + await assert.rejects(collectExpiredRetiredServerProbe(target)) + assert.deepEqual(await invokeMcpTool(principal,"mm_server_probe_receipt_get",request),live) + await pool.query("UPDATE mcp_settings SET requests_retention_days=30 WHERE id=1") + assert.deepEqual(await storage.purge(actor.id,1,{ deadlineAt:Date.now()+10000 }),{ purged:1,automaticReplay:false }) + await assert.rejects(invokeMcpTool(principal,"mm_server_probe_receipt_get",request)) + assert.equal((await pool.query("SELECT reserved_bytes::int AS bytes FROM server_probe_storage_usage WHERE scope='global'")).rows[0].bytes,32768) + } + const gcState = async (sql:Pick) => (await sql.query(`SELECT + (SELECT count(*)::int FROM server_probe_retired_sources WHERE receipt_id=$1) AS archive, + (SELECT count(*)::int FROM server_probe_storage_admissions WHERE receipt_id=$1) AS admission, + (SELECT count(*)::int FROM server_probe_executions WHERE receipt_id=$1) AS anchor, + (SELECT count(*)::int FROM server_probe_execution_stages WHERE receipt_id=$1) AS stages, + (SELECT count(*)::int FROM server_probe_execution_payloads WHERE receipt_id=$1) AS payload, + (SELECT reserved_bytes::int FROM server_probe_storage_usage WHERE scope='global') AS quota, + (SELECT count(*)::int FROM server_probe_storage_usage WHERE scope=$2) AS owner_counter, + (SELECT to_jsonb(t) FROM mcp_operation_replay_tombstones t WHERE t.operation_id=$3 AND t.connection_id=$4) AS tombstone, + (SELECT to_jsonb(c) FROM server_probe_retirement_claims c WHERE c.receipt_id=$1 AND c.operation_id=$3 AND c.connection_id=$4) AS claim`, + [a.receipt_id,"owner:"+actor.id,target.operationId,target.connectionId])).rows[0] + const beforeGc = await gcState(pool) + assert(beforeGc.tombstone && beforeGc.claim) + if (mode === "purged") { + await assertWaitsBeforeSecondary(() => collectExpiredRetiredServerProbe(target),async exclusive => { + assert.equal((await exclusive.query("SELECT id FROM public.mcp_settings WHERE id=1 FOR UPDATE NOWAIT")).rows.length,1) + const locked = (await exclusive.query(`SELECT r.proof FROM public.server_probe_retired_sources r + JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=r.operation_id + JOIN public.mcp_connections c ON c.id=t.connection_id + JOIN public.server_probe_storage_admissions a ON a.receipt_id=r.receipt_id AND a.owner_id=c.owner_id + JOIN public.server_probe_executions j ON j.receipt_id=r.receipt_id + WHERE r.receipt_id=$1 AND r.operation_id=$2 AND c.id=$3 FOR UPDATE OF c,r,t,a,j NOWAIT`, + [target.receiptId,target.operationId,target.connectionId])).rows + assert.equal(locked.length,1) + await exclusive.query(`SELECT set_config('mm.probe_storage_gc','on',true),set_config('mm.probe_retired_gc_receipt',$1,true), + set_config('mm.probe_retired_gc_proof',$2,true)`,[target.receiptId,locked[0].proof]) + assert.equal((await exclusive.query("SELECT public.server_probe_storage_gc_allowed($1) AS allowed",[target.receiptId])).rows[0].allowed,true) + },async exclusive => assert.deepEqual(await gcState(exclusive),beforeGc)) + } + if (mode === "retained") await tickMcpWorkers() + else assert.deepEqual(await collectExpiredRetiredServerProbe(target), {collected:true,automaticReplay:false}) + assert.deepEqual(await gcState(pool),{archive:0,admission:0,anchor:0,stages:0,payload:0,quota:0,owner_counter:0, + tombstone:beforeGc.tombstone,claim:beforeGc.claim}) + await assert.rejects(collectExpiredRetiredServerProbe(target)) + await assert.rejects(invokeMcpTool(principal,"mm_server_probe_receipt_get",request)) + await assert.rejects(applyPlan(principal,o.plan_id,key),error=>error instanceof McpError&&error.code==="OPERATION_HISTORY_EXPIRED") + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$1", [principal.connectionId]) + await assert.rejects(invokeMcpTool(principal, "mm_server_probe_receipt_get", request)) + await assert.rejects(applyPlan(principal,o.plan_id,key), error => error instanceof McpError && error.code==="NOT_FOUND" && error.statusCode===404) + } + await verifyLifecycle("retained") + await verifyLifecycle("purged") + await verifyLifecycle("default-young") + await verifyLifecycle("default-old") + await verifyLifecycle("default-unproven") + await verifyLifecycle("default-spent") + await verifyLifecycle("scheduled-retained") + console.log("Owned diagnostic retirement: real worker, retained/expired paths, spent claims and holds, public replay/read and exact GC refunds") +} finally { await closePool() } diff --git a/backend/src/services/server-probe-operation-retirement.ts b/backend/src/services/server-probe-operation-retirement.ts new file mode 100644 index 00000000..19953f7c --- /dev/null +++ b/backend/src/services/server-probe-operation-retirement.ts @@ -0,0 +1,376 @@ +/** Exact internal maintenance entry. No scheduler, public registration, or native work. */ +import { randomUUID } from "node:crypto" +import { z } from "zod" +import { operationIdempotencyDigest } from "../modules/mcp/operation-replay-tombstones.js" +import { constantEqual, decryptSecretExisting, fingerprint, privateFingerprinterExisting } from "../modules/mcp/security.js" +import { PROBE_PAYLOAD_MAX_BYTES, PROBE_PERMANENT_RESERVATION_BYTES, ProbeStorageRefused, ProbeStorageUnknown, type ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { findRetiredServerProbeHistory } from "./server-probe-retired-history.js" +import { matchesServerProbeRetiredTerminal, openServerProbeRetiredSource, projectServerProbeRetiredSource, ServerProbeRetirementSourceRefused } from "./server-probe-retired-source.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" +import { admitPartitionWriter } from "./partition-write-transaction.js" + +const targetSchema = z.object({ receiptId: z.string().uuid(), operationId: z.string().uuid(), connectionId: z.string().uuid() }).strict() +type Target = z.infer +function requireThat(value: unknown): asserts value { if (!value) throw new ProbeStorageRefused() } + +const ORIGINAL = `jsonb_build_object( + 'operation_id',o.id,'connection_id',o.connection_id,'owner_id',c.owner_id,'plan_id',p.id, + 'operation_name',p.operation_id,'operation_version',p.operation_version,'operation_state',o.state, + 'operation_verification',o.verification,'operation_error_code',o.error_code,'operation_result',o.result, + 'plan_state',p.state,'approved_by',p.approved_by,'evidence',p.evidence,'step_index',s.step_index, + 'step_state',s.state,'intended_state',s.intended_state,'step_result',s.result, + 'operation_created_at',o.created_at,'operation_finished_at',o.finished_at,'plan_created_at',p.created_at, + 'plan_approved_at',p.approved_at,'plan_expires_at',p.expires_at,'step_created_at',s.created_at,'step_finished_at',s.finished_at + ) AS original,o.idempotency_key,k.before_sealed,k.after_sealed` +const ORIGINAL_JOINS = `JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_connections c ON c.id=o.connection_id + JOIN public.mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0 + JOIN public.mcp_operation_checkpoints k ON k.operation_id=o.id AND k.step_index=s.step_index` +const ORIGINAL_ELIGIBLE = ` AND o.state='applied' AND o.verification='inconclusive' AND o.error_code IS NULL + AND isfinite(o.finished_at) AND o.finished_at0) + AND NOT EXISTS(SELECT 1 FROM public.mcp_operation_checkpoints WHERE operation_id=o.id AND step_index<>0) + AND NOT EXISTS(SELECT 1 FROM public.mcp_drift WHERE operation_id=o.id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_recovery_intents WHERE operation_id=o.id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_scoped_recovery_intents WHERE source_operation_id=o.id OR recovery_operation_id=o.id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_routing_reservations WHERE source_operation_id=o.id OR source_plan_id=p.id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_routing_forward_charges WHERE source_operation_id=o.id OR source_plan_id=p.id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_routing_manual_inverse_charges WHERE source_operation_id=o.id OR recovery_operation_id=o.id OR source_plan_id=p.id OR recovery_plan_id=p.id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_diagnostic_cancellations WHERE operation_id=o.id) + AND NOT EXISTS(SELECT 1 FROM public.server_probe_execution_quarantines WHERE receipt_id=$1 OR(identity_kind='mcp' AND execution_id=o.id)) + AND NOT EXISTS(SELECT 1 FROM public.server_poll_storage_admissions WHERE identity_kind='mcp' AND execution_id=o.id) + AND NOT EXISTS(SELECT 1 FROM public.vrf_production_tickets WHERE plan_id=p.id) +` +const SOURCE = `SELECT ${ORIGINAL},to_jsonb(a) AS admission,to_jsonb(j) AS anchor,to_jsonb(b) AS payload + FROM public.server_probe_storage_admissions a + JOIN public.server_probe_executions j ON j.receipt_id=a.receipt_id + JOIN public.server_probe_execution_payloads b ON b.receipt_id=a.receipt_id + JOIN public.mcp_operations o ON o.id=a.execution_id + ${ORIGINAL_JOINS} + WHERE a.receipt_id=$1 AND o.id=$2 AND c.id=$3 AND c.owner_id=a.owner_id AND a.identity_kind='mcp' + AND a.claim_token IS NULL AND a.purged_at IS NULL AND a.cancelled_at IS NULL + AND (($5::timestamptz IS NULL AND a.authority_expires_at(deadlineAt: number, work: (sql: ProbeStorageSql) => Promise, mode: "default" | "fenced-write" = "default"): Promise { + const sql = await connectProbeStorage({ deadlineAt }) + let committing = false, committed = false + let timer: ReturnType | undefined + try { + await sql.query("BEGIN") + if (mode === "fenced-write") { + // There is no mutation to reconcile if the common writer fence refuses. + try { await admitPartitionWriter(sql, { deadlineAt: Math.min(deadlineAt, Date.now()+1000) }) } + catch { throw new ProbeStorageRefused() } + } + await sql.query("SELECT set_config('TimeZone','UTC',true),set_config('statement_timeout','5000ms',true),set_config('lock_timeout','1000ms',true)") + // The same order as admission, publication, cancellation and payload GC. + requireThat((await sql.query("SELECT pg_try_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0)) AS locked")).rows[0]?.locked === true) + // Bound non-SQL proof work as well. Any late continuation retains only + // this permanently released checkout and cannot issue further SQL. + const expired = new Promise((_resolve,reject) => { + timer = setTimeout(() => { + sql.release(true) + reject(new ProbeStorageRefused()) + }, Math.max(1,deadlineAt-Date.now())) + }) + const result = await Promise.race([work(sql),expired]) + clearTimeout(timer) + requireThat(Date.now()) { + for (const sealed of [row.before_sealed, row.after_sealed]) { + requireThat(typeof sealed === "string") + let decoded: unknown + try { decoded = JSON.parse(await decryptSecretExisting(sealed)) } + catch { throw new ProbeStorageRefused() } + requireThat(Array.isArray(decoded) && decoded.length === 0) + } +} +async function prepare(sql: ProbeStorageSql, target: Target, requireOld = true, expectedFinishedAt?: string) { + const { receiptId, operationId, connectionId } = target + await lockOriginal(sql, target) + const rows = (await sql.query(SOURCE, [receiptId, operationId, connectionId, requireOld, expectedFinishedAt ?? null])).rows + requireThat(rows.length === 1) + const { original, admission, anchor, payload, idempotency_key: key } = rows[0] + requireThat(typeof key === "string") + await assertEmptyCheckpoints(rows[0]) + const stages = (await sql.query(`SELECT to_jsonb(s) AS stage FROM public.server_probe_execution_stages s + WHERE s.receipt_id=$1 ORDER BY s.stage_index LIMIT 12 FOR UPDATE NOWAIT`, [receiptId])).rows.map(row => row.stage) + const signed = await projectServerProbeRetiredSource({ original, admission, anchor, payload, stages, quarantineCount: 0, cancellationCount: 0 }) + requireThat(signed.source.receiptId === receiptId && signed.source.executionId === operationId && signed.source.connectionId === connectionId) + return { signed, key } +} + +const captureHash = z.string().regex(/^[a-f0-9]{64}$/) +async function terminalCaptureProof(sourceProof: string, idempotencyDigest: string) { + const sign = await privateFingerprinterExisting("server-probe-terminal-capture-v1") + return sign(fingerprint({ sourceProof, idempotencyDigest })) +} + +function captureTerminalProof(target: Target, deadlineAt: number, expectedFinishedAt?: string) { + return sharedDatabaseGate.request(() => transaction(deadlineAt, async sql => { + const { signed, key } = await prepare(sql, target, false, expectedFinishedAt) + const digest = operationIdempotencyDigest(target.connectionId, key) + const proof = await terminalCaptureProof(signed.proof, digest) + const saved = (await sql.query(`INSERT INTO public.server_probe_terminal_proofs + (operation_id,receipt_id,source,source_proof,idempotency_digest,capture_proof) + VALUES($1,$2,$3::jsonb,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING receipt_id`, + [target.operationId,target.receiptId,JSON.stringify(signed.source),signed.proof,digest,proof])).rows + requireThat(saved.length === 1 && saved[0].receipt_id === target.receiptId) + return { captured:true as const, automaticReplay:false as const } + }, "fenced-write")) +} +/** Captures evidence only while the normal retention owner can still read the original. */ +export function captureServerProbeTerminalProof(raw: Target, deadlineAt = Date.now()+20000) { + return captureTerminalProof(targetSchema.parse(raw), deadlineAt) +} + +/** Only the operation runner supplies the positively acknowledged terminal SQL timestamp. */ +export function capturePublishedServerProbeTerminalProof(raw: Target & { finishedAt: string }) { + const { finishedAt, ...identity } = raw + const stamp = z.string().regex(/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}Z$/).parse(finishedAt) + return captureTerminalProof(targetSchema.parse(identity), Date.now()+1200, stamp) +} + +async function prepareClosed(sql: ProbeStorageSql, target: Target) { + const { receiptId, operationId, connectionId } = target + await lockOriginal(sql, target) + const rows = (await sql.query(CLOSED_SOURCE, [receiptId, operationId, connectionId])).rows + requireThat(rows.length === 1) + const row = rows[0], key = z.string().parse(row.idempotency_key) + await assertEmptyCheckpoints(row) + const proof = captureHash.parse(row.source_proof), digest = operationIdempotencyDigest(connectionId, key) + requireThat(digest === row.idempotency_digest + && constantEqual(captureHash.parse(row.capture_proof), await terminalCaptureProof(proof, digest))) + const source = await openServerProbeRetiredSource({ source:row.source, proof }) + requireThat(source.receiptId === receiptId && source.executionId === operationId && source.connectionId === connectionId + && matchesServerProbeRetiredTerminal(source, row.original)) + return { signed:{source,proof},key } +} + +function retireServerProbeOperation(raw: Target, kind: "retained" | "expired", deadlineAt = Date.now()+20000) { + const target = targetSchema.parse(raw) + const { receiptId, operationId, connectionId } = target + return sharedDatabaseGate.request(async () => { + const token = randomUUID() + const read = kind === "retained" ? prepare : prepareClosed + const claim = await transaction(deadlineAt, async sql => { + const { signed, key } = await read(sql, target) + const digest = operationIdempotencyDigest(connectionId, key) + const result = await sql.query(`INSERT INTO public.server_probe_retirement_claims + (receipt_id,operation_id,plan_id,connection_id,source_proof,claim_token) + VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING receipt_id`, + [receiptId, operationId, signed.source.planId, connectionId, signed.proof, token]) + requireThat(result.rows.length === 1 && result.rows[0].receipt_id === receiptId) + return { proof: signed.proof, planId: signed.source.planId, digest } + }, "fenced-write") + return transaction(deadlineAt, async sql => { + const { signed, key } = await read(sql, target), { planId, proof, digest } = claim + requireThat(signed.proof === proof && signed.source.planId === planId && operationIdempotencyDigest(connectionId, key) === digest) + const consumed = await sql.query(`SELECT receipt_id FROM public.server_probe_retirement_claims + WHERE receipt_id=$1 AND operation_id=$2 AND plan_id=$3 AND connection_id=$4 AND source_proof=$5 + AND claim_token=$6 AND claim_xid<>pg_current_xact_id()`, [receiptId, operationId, planId, connectionId, proof, token]) + requireThat(consumed.rows.length === 1 && consumed.rows[0].receipt_id === receiptId) + const tombstone = await sql.query(`INSERT INTO public.mcp_operation_replay_tombstones + (operation_id,plan_id,connection_id,idempotency_digest,action_id,target_server_ids,target_global) + VALUES($1,$2,$3,$4,'servers.probes.run',$5::bigint[],false) RETURNING operation_id`, + [operationId, planId, connectionId, digest, signed.source.serverIds]) + requireThat(tombstone.rows.length === 1 && tombstone.rows[0].operation_id === operationId) + await sql.query("SELECT set_config('mm.probe_retirement_claim',$1,true)", [token]) + if (kind === "retained") { + const archive = await sql.query(`INSERT INTO public.server_probe_retired_sources(receipt_id,operation_id,source,proof) + VALUES($1,$2,$3::jsonb,$4) RETURNING receipt_id`, [receiptId, operationId, JSON.stringify(signed.source), proof]) + requireThat(archive.rows.length === 1 && archive.rows[0].receipt_id === receiptId) + } + const released = await sql.query("DELETE FROM public.server_probe_terminal_proofs WHERE operation_id=$1 RETURNING receipt_id", [operationId]) + requireThat(released.rows.length <= 1 && released.rows.every(row => row.receipt_id === receiptId) + && (kind !== "expired" || released.rows.length === 1)) + const checkpoint = await sql.query("DELETE FROM public.mcp_operation_checkpoints WHERE operation_id=$1 AND step_index=0 RETURNING operation_id", [operationId]) + requireThat(checkpoint.rows.length === 1 && checkpoint.rows[0].operation_id === operationId) + const step = await sql.query("DELETE FROM public.mcp_operation_steps WHERE operation_id=$1 AND step_index=0 RETURNING operation_id", [operationId]) + requireThat(step.rows.length === 1 && step.rows[0].operation_id === operationId) + const operation = await sql.query("DELETE FROM public.mcp_operations WHERE id=$1 AND connection_id=$2 AND plan_id=$3 RETURNING id", [operationId, connectionId, planId]) + requireThat(operation.rows.length === 1 && operation.rows[0].id === operationId) + const plan = await sql.query("DELETE FROM public.mcp_plans WHERE id=$1 AND connection_id=$2 RETURNING id", [planId, connectionId]) + requireThat(plan.rows.length === 1 && plan.rows[0].id === planId) + if (kind === "retained") { + const observed = await findRetiredServerProbeHistory(async (text, values) => (await sql.query(text, values)).rows, + { ...target, ownerId: signed.source.ownerId, scopeServerIds: signed.source.serverIds }) + requireThat(observed && !observed.payloadPurged && fingerprint(observed.source) === fingerprint(signed.source)) + } + return { retired: true as const, automaticReplay: false as const } + }, "fenced-write") + }) +} +export function retireCompletedServerProbeOperation(raw: Target) { + return retireServerProbeOperation(raw, "retained") +} +export function retireExpiredServerProbeOperation(raw: Target) { + return retireServerProbeOperation(raw, "expired") +} + +/** Exact internal GC. This entry has no worker or public registration. */ +export function collectExpiredRetiredServerProbe(raw: Target, deadlineAt = Date.now()+20000) { + const target = targetSchema.parse(raw) + const { receiptId, operationId, connectionId } = target + return sharedDatabaseGate.request(() => transaction(deadlineAt, async sql => { + const settings = (await sql.query("SELECT requests_retention_days FROM public.mcp_settings WHERE id=1 FOR SHARE NOWAIT")).rows + const retentionDays = settings[0]?.requests_retention_days + requireThat(settings.length === 1 && typeof retentionDays === "number" && Number.isInteger(retentionDays) + && retentionDays >= 1 && retentionDays <= 365) + const pinned = (await sql.query(`SELECT c.owner_id,r.proof,t.target_server_ids::text[] AS server_ids FROM public.server_probe_retired_sources r + JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=r.operation_id + JOIN public.mcp_connections c ON c.id=t.connection_id + JOIN public.server_probe_storage_admissions a ON a.receipt_id=r.receipt_id AND a.owner_id=c.owner_id + JOIN public.server_probe_executions j ON j.receipt_id=r.receipt_id + WHERE r.receipt_id=$1 AND r.operation_id=$2 AND c.id=$3 FOR UPDATE OF c,r,a,j NOWAIT`, + [receiptId, operationId, connectionId])).rows + const identity = z.object({ owner_id: z.string().min(1).max(200), proof: z.string().regex(/^[a-f0-9]{64}$/), + server_ids: z.array(z.string().regex(/^[1-9]\d{0,15}$/).transform(Number).pipe(z.number().int().positive().safe())).min(1).max(2) }).strict().parse(pinned[0]) + requireThat(pinned.length === 1) + const observed = await findRetiredServerProbeHistory(async (text, values) => (await sql.query(text, values)).rows, + { ...target, ownerId: identity.owner_id, scopeServerIds: identity.server_ids }) + requireThat(observed) + const stageRows = (await sql.query(`SELECT to_jsonb(s) AS stage FROM public.server_probe_execution_stages s + WHERE s.receipt_id=$1 ORDER BY s.stage_index LIMIT 12 FOR UPDATE NOWAIT`, [receiptId])).rows + requireThat(stageRows.length >= 1 && stageRows.length <= 11 + && fingerprint(stageRows.map(row => row.stage)) === observed.source.stageSnapshotDigest) + await sql.query(`SELECT set_config('mm.probe_storage_gc','on',true),set_config('mm.probe_retired_gc_receipt',$1,true), + set_config('mm.probe_retired_gc_proof',$2,true)`, [receiptId, identity.proof]) + requireThat((await sql.query("SELECT public.server_probe_storage_gc_allowed($1) AS allowed", [receiptId])).rows[0]?.allowed === true) + const deletedPayload = (await sql.query("DELETE FROM public.server_probe_execution_payloads WHERE receipt_id=$1 RETURNING receipt_id", [receiptId])).rows + const files = observed.payloadPurged ? 0 : 1 + requireThat(deletedPayload.length === files && deletedPayload.every(row => row.receipt_id === receiptId)) + const deletedStages = (await sql.query("DELETE FROM public.server_probe_execution_stages WHERE receipt_id=$1 RETURNING receipt_id", [receiptId])).rows + requireThat(deletedStages.length === stageRows.length && deletedStages.every(row => row.receipt_id === receiptId)) + const deletedAnchor = (await sql.query("DELETE FROM public.server_probe_executions WHERE receipt_id=$1 RETURNING receipt_id", [receiptId])).rows + requireThat(deletedAnchor.length === 1 && deletedAnchor[0].receipt_id === receiptId) + // Admission deletion cascades to the exact authenticated archive only. + const deletedAdmission = (await sql.query("DELETE FROM public.server_probe_storage_admissions WHERE receipt_id=$1 AND owner_id=$2 RETURNING receipt_id", + [receiptId, identity.owner_id])).rows + requireThat(deletedAdmission.length === 1 && deletedAdmission[0].receipt_id === receiptId) + const scopes = ["global", "owner:" + identity.owner_id], bytes = PROBE_PERMANENT_RESERVATION_BYTES + files * PROBE_PAYLOAD_MAX_BYTES + const released = (await sql.query(`UPDATE public.server_probe_storage_usage SET admissions=admissions-1, + reserved_files=reserved_files-$2,reserved_bytes=reserved_bytes-$3 WHERE scope=ANY($1::text[]) + AND admissions>=1 AND reserved_files>=$2 AND reserved_bytes>=$3 RETURNING scope`, [scopes, files, bytes])).rows + requireThat(released.length === 2 && scopes.every(scope => released.some(row => row.scope === scope))) + await sql.query("DELETE FROM public.server_probe_storage_usage WHERE scope=$1 AND admissions=0 AND reserved_files=0 AND reserved_bytes=0", [scopes[1]]) + const state = (await sql.query(`SELECT + NOT EXISTS(SELECT 1 FROM public.server_probe_retired_sources WHERE receipt_id=$1) AS archive_gone, + EXISTS(SELECT 1 FROM public.mcp_operation_replay_tombstones WHERE operation_id=$2 AND connection_id=$3) AS replay_retained, + EXISTS(SELECT 1 FROM public.server_probe_retirement_claims WHERE receipt_id=$1 AND operation_id=$2 AND connection_id=$3) AS claim_retained`, + [receiptId, operationId, connectionId])).rows[0] + requireThat(state?.archive_gone === true && state.replay_retained === true && state.claim_retained === true) + return { collected: true as const, automaticReplay: false as const } + }, "fenced-write")) +} + +const RETIRE_CANDIDATES = `SELECT COALESCE(a.receipt_id,f.receipt_id) AS receipt_id,o.id AS operation_id,o.connection_id, + o.finished_at::text AS ordered_at,CASE WHEN a.receipt_id IS NULL THEN 'expired' ELSE 'retained' END AS kind + FROM public.mcp_operations o + JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_connections c ON c.id=o.connection_id + LEFT JOIN public.server_probe_storage_admissions a ON a.identity_kind='mcp' AND a.execution_id=o.id AND a.owner_id=c.owner_id + LEFT JOIN public.server_probe_terminal_proofs f ON f.operation_id=o.id + WHERE o.state='applied' AND o.verification='inconclusive' AND o.error_code IS NULL + AND isfinite(o.finished_at) AND o.finished_at($1::timestamptz,$2::uuid)) + ORDER BY o.finished_at,o.id LIMIT 8` +const GC_CANDIDATES = `SELECT r.receipt_id,r.operation_id,t.connection_id,a.gc_not_before::text AS ordered_at,'gc' AS kind + FROM public.server_probe_retired_sources r + JOIN public.server_probe_storage_admissions a ON a.receipt_id=r.receipt_id AND a.execution_id=r.operation_id + JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=r.operation_id + JOIN public.mcp_connections c ON c.id=t.connection_id AND c.owner_id=a.owner_id + WHERE isfinite(a.gc_not_before) AND a.gc_not_before($1::timestamptz,$2::uuid)) + ORDER BY a.gc_not_before,r.operation_id LIMIT 8` + +type Cursor = { orderedAt: string; operationId: string } +let retireCursor: Cursor | undefined, gcCursor: Cursor | undefined +const workerRow = z.object({ receipt_id:z.string().uuid(),operation_id:z.string().uuid(),connection_id:z.string().uuid(), + ordered_at:z.string().refine(value=>Number.isFinite(Date.parse(value))),kind:z.enum(["retained","expired","gc"]) }).strict() + +/** Invoked only under the existing scheduler owner. Keep expiry and one-shot + * retirement independent; a claimed operation is never selected again. */ +export function maintainServerProbeOperationRetirement() { + return sharedDatabaseGate.request(async () => { + const deadlineAt = Date.now()+20000 + const scan = async (lane: "retire" | "gc") => { + const cursor = lane === "retire" ? retireCursor : gcCursor + const setCursor = (value: Cursor | undefined) => { if (lane === "retire") retireCursor=value; else gcCursor=value } + const rows = await transaction(deadlineAt,async sql => (await sql.query(lane === "retire" ? RETIRE_CANDIDATES : GC_CANDIDATES, + [cursor?.orderedAt ?? null,cursor?.operationId ?? null])).rows) + requireThat(rows.length<=8) + let scanned=0, completed=0 + for (const raw of rows) { + if (Date.now()>=deadlineAt-2000 || completed>=2) break + const row=workerRow.parse(raw) + requireThat((row.kind === "gc") === (lane === "gc")) + const target={receiptId:row.receipt_id,operationId:row.operation_id,connectionId:row.connection_id} + setCursor({orderedAt:row.ordered_at,operationId:row.operation_id}); scanned++ + try { + if (row.kind === "gc") await collectExpiredRetiredServerProbe(target,deadlineAt) + else await retireServerProbeOperation(target,row.kind,deadlineAt) + completed++ + } catch (error) { + if (!(error instanceof ProbeStorageRefused || error instanceof ServerProbeRetirementSourceRefused || error instanceof z.ZodError)) throw error + } + } + if (scanned === rows.length && rows.length < 8) setCursor(undefined) + return completed + } + const collected=await scan("gc") + const retired=Date.now() ({ index, gateway: "192.0.2.1" })), status: 'password="DO_NOT_RETURN" Bearer DO_NOT_RETURN mmcp_access_DO_NOT_RETURN', token: "DO_NOT_RETURN", nested: { secret: "DO_NOT_RETURN" } }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } +const safe = sanitizeServerProbeReceipt(receipt) +assert.ok(!JSON.stringify(safe).includes("DO_NOT_RETURN")) +assert.equal(safe.output, receipt.output) +assert.equal((safe.data!.rows as unknown[]).length, 600) +const first = serverProbeReceiptPage(safe, 0), chunks: Buffer[] = [] +assert.ok(first.totalPages > 1) +for (let page = 0; page < first.totalPages; page++) { + const result = serverProbeReceiptPage(safe, page), projected = redact(result) as typeof result + assert.deepEqual(projected, result, "Generic MCP redaction must preserve each complete public page") + assert.ok(result.contentB64.length <= 12000) + assert.equal(result.nextPage, page + 1 < first.totalPages ? page + 1 : null) + chunks.push(Buffer.from(result.contentB64, "base64")) +} +const restored = Buffer.concat(chunks) +assert.equal(restored.length, first.bytes) +assert.equal(createHash("sha256").update(restored).digest("hex"), first.sha256) +assert.deepEqual(JSON.parse(restored.toString("utf8")), safe) +for (const page of [-1, 0.5, NaN, first.totalPages, Number.MAX_SAFE_INTEGER]) assert.throws(() => serverProbeReceiptPage(safe, page)) +assert.throws(() => sanitizeServerProbeReceipt({ ...receipt, output: "x".repeat(131073) })) +assert.throws(() => sanitizeServerProbeReceipt({ ...receipt, completedStages: 2 })) +assert.throws(() => sanitizeServerProbeReceipt({ ...receipt, data: { bad: Infinity } })) +assert.throws(() => sanitizeServerProbeReceipt({ ...receipt, data: { bad: new Date() } })) +let nested: unknown = "x" +for (let index = 0; index < 15; index++) nested = { child: nested } +assert.throws(() => sanitizeServerProbeReceipt({ ...receipt, data: { nested } })) +console.log("Probe public receipts: full Unicode/output/600 rows retained across bounded MCP pages, exact SHA256, credential-pattern redaction, malformed/depth/size/count refusal passed") diff --git a/backend/src/services/server-probe-public-result.ts b/backend/src/services/server-probe-public-result.ts new file mode 100644 index 00000000..78f5984b --- /dev/null +++ b/backend/src/services/server-probe-public-result.ts @@ -0,0 +1,41 @@ +import { createHash } from "node:crypto" +import { z } from "zod" +import type { ServerProbeReceipt } from "./server-probe-execution.js" + +const LIMIT = 131072, PAGE_BYTES = 9000 +const sensitive = /password|passwd|secret|private.?key|preshared|pre.shared|authorization|auth.?key|authentication.?key|tcp.?md5.?key|api.?key|certificate.?contents|token|ciphertext/i +const schema = z.object({ receiptId: z.string().uuid(), tool: z.enum(["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]), serverId: z.number().int().positive().safe(), destinationServerId: z.number().int().positive().safe().optional(), state: z.enum(["completed", "failed", "cancelled", "unknown"]), httpStatus: z.number().int().min(100).max(599), output: z.string().max(LIMIT).optional(), data: z.record(z.string(), z.unknown()).optional(), errorCode: z.string().regex(/^[A-Z_]+$/).max(100).optional(), attemptedStages: z.number().int().min(0).max(11), completedStages: z.number().int().min(0).max(11), automaticReplay: z.literal(false), forwardingVerified: z.literal(false) }).strict() + +function text(value: string) { + return value.replace(/\bBearer\s+[^\s,;]+/gi, "Bearer [redacted]") + .replace(/\bmmcp_[a-z]+_[A-Za-z0-9_-]+/g, "[redacted]") + .replace(/((?:password|secret|private-key|preshared-key|auth-key|authentication-key|tcp-md5-key|api-key|token)\s*[=:]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)/gi, "$1[redacted]") +} +/** Same sensitive fields/text as generic MCP redaction, with explicit whole + * receipt byte/depth limits instead of silent string/array truncation. Known + * endpoint usernames/passwords must also be redacted by the shared executor. */ +export function sanitizeServerProbeReceipt(receipt: ServerProbeReceipt): ServerProbeReceipt { + schema.parse(receipt) + if (receipt.completedStages > receipt.attemptedStages || receipt.state === "completed" && (receipt.httpStatus !== 200 || receipt.output === undefined || receipt.data === undefined || receipt.attemptedStages < 1 || receipt.completedStages !== receipt.attemptedStages)) throw new Error("Invalid diagnostic receipt") + const clean = (value: unknown, depth = 0): unknown => { + if (depth > 12) throw new Error("Diagnostic receipt depth limit") + if (value === undefined || value === null || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return value + if (typeof value === "string") return text(value) + if (Array.isArray(value)) { if (value.length > 10000) throw new Error("Diagnostic receipt array limit"); return value.map(item => clean(item, depth + 1)) } + if (!value || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype || Object.keys(value).length > 2000) throw new Error("Invalid diagnostic receipt data") + return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, sensitive.test(key) && !/Ref$|Configured$/i.test(key) ? "[redacted]" : clean(item, depth + 1)])) + } + const safe = schema.parse(clean(receipt)) + if (Buffer.byteLength(JSON.stringify(safe)) > LIMIT) throw new Error("Diagnostic receipt byte limit") + return safe +} + +/** Base64 is transport encoding of already sanitized PUBLIC measurements. It + * is not encryption. Fixed-size pages survive generic MCP output truncation; + * callers must authorize the owned receipt before requesting each page. */ +export function serverProbeReceiptPage(receipt: ServerProbeReceipt, page: number) { + if (!Number.isSafeInteger(page) || page < 0) throw new Error("Invalid diagnostic page") + const safe = sanitizeServerProbeReceipt(receipt), content = Buffer.from(JSON.stringify(safe)), totalPages = Math.ceil(content.length / PAGE_BYTES) + if (page >= totalPages) throw new Error("Diagnostic page out of range") + return { receiptId: safe.receiptId, page, totalPages, bytes: content.length, sha256: createHash("sha256").update(content).digest("hex"), encoding: "base64-json-utf8" as const, contentB64: content.subarray(page * PAGE_BYTES, (page + 1) * PAGE_BYTES).toString("base64"), nextPage: page + 1 < totalPages ? page + 1 : null } +} diff --git a/backend/src/services/server-probe-rest-contract.test.ts b/backend/src/services/server-probe-rest-contract.test.ts new file mode 100644 index 00000000..a60d0125 --- /dev/null +++ b/backend/src/services/server-probe-rest-contract.test.ts @@ -0,0 +1,117 @@ +import assert from "node:assert/strict" +import { classifyServerProbeMtu, planServerProbeExecution, projectServerProbeBandwidth, projectServerProbeDns, projectServerProbeMtu, projectServerProbePing, projectServerProbeRoute, projectServerProbeTrace, ServerProbeDataError, serverProbeExecutionRequest } from "./server-probe-execution-model.js" +import { normalizeServerProbeRestRequest, parseServerProbeRestServerId, resolveServerProbeRestBandwidthPeer, serverProbeRestErrorReply, serverProbeRestReceiptReply, ServerProbeRestContractError, type ServerProbeRestPeerMetadata, type ServerProbeRestPeerReader } from "./server-probe-rest-contract.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" + +// Frozen expected strings/bodies below are copied from the existing REST +// contract, not calculated with the projector under test. No DB/DNS/native I/O. +let cases = 0 +const target = "192.0.2.2" +const plan = (raw: unknown) => planServerProbeExecution(normalizeServerProbeRestRequest(raw)) +const equal = (actual: unknown, expected: unknown) => { assert.deepEqual(actual, expected); cases++ } +const refused = (work: () => unknown) => { assert.throws(work); cases++ } +equal(plan({ tool: "ping", target }).nativeBody, { address: target, count: "5", size: "64", ttl: "64", interval: "0.2s" }) +equal(plan({ tool: "traceroute", target }).nativeBody, { address: target, protocol: "icmp", "max-hops": "30", timeout: "00:00:01", count: "1", "use-dns": "false" }) +equal(plan({ tool: "bandwidth", bwRemoteAddress: target }).nativeBody, { address: target, protocol: "tcp", direction: "both", duration: "10s" }) +equal(plan({ tool: "mtu", target }).mtuSizes, [1500, 1492, 1480, 1476, 1472, 1468, 1400, 1280, 1024, 576]) +for (const [tool, wire, wall] of [["ping", 120000, 130000], ["traceroute", 58000, 68000], ["bandwidth", 30000, 30000], ["dns", 30000, 30000], ["route", 10000, 10000], ["mtu", 25000, 260000]] as const) { + const planned = plan({ tool, target, bwRemoteAddress: target }); equal([planned.wireTimeoutMs, planned.wallBudgetMs], [wire, wall]) +} +equal(plan({ tool: "ping", target, pingCount: 999, pingSize: 99999, pingTtl: 999 }).nativeBody, { address: target, count: "100", size: "8192", ttl: "255", interval: "0.2s" }) +equal(plan({ tool: "ping", target, pingCount: 1, pingSize: 1, pingTtl: 1 }).nativeBody, { address: target, count: "1", size: "28", ttl: "1", interval: "0.2s" }) +for (const [duration, expected] of [[1, "3s"], [999, "120s"]] as const) equal(plan({ tool: "bandwidth", bwRemoteAddress: target, bwProto: "udp", bwDuration: duration }).nativeBody, { address: target, protocol: "udp", direction: "both", duration: expected }) +for (const [input, timeout] of [["800ms", "00:00:00.800"], ["0.001s", "00:00:00.010"], ["99s", "00:00:03"], ["00:00:00.125", "00:00:00.125"], ["01:00:00", "00:00:03"]]) equal(plan({ tool: "traceroute", target, traceHopTimeout: input }).nativeBody?.timeout, timeout) +const capped = plan({ tool: "traceroute", target, traceProto: "tcp", traceMaxHops: 999, traceProbeCount: 999, traceHopTimeout: "3s", traceUseDns: true }) +equal(capped.nativeBody, { address: target, protocol: "tcp", "max-hops": "5", timeout: "00:00:03", count: "3", "use-dns": "true" }) +equal(plan({ tool: "traceroute", target, traceProto: "udp", traceMaxHops: 1, traceProbeCount: 1, traceUseDns: false }).nativeBody?.protocol, "udp") + +const allDocumented = { tool: " ping ", target: " 192.0.2.2 ", pingCount: 2, pingSize: 128, pingTtl: 20, traceProto: "udp", traceMaxHops: 12, traceHopTimeout: "800ms", traceProbeCount: 2, traceUseDns: false, dnsType: " txt ", bwRemoteAddress: " peer.test ", dstServerId: 9, bwProto: "udp", bwDuration: 19 } +equal(normalizeServerProbeRestRequest({ ...allDocumented, ignoredLegacyField: { arbitrary: true }, clients: "untrusted", authority: "untrusted", transport: "rest" }), { ...allDocumented, tool: "ping", target, dnsType: "TXT", bwRemoteAddress: "peer.test" }) +assert(!serverProbeExecutionRequest.safeParse({ tool: "ping", target, ignoredLegacyField: true }).success); cases++ +for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) equal(normalizeServerProbeRestRequest({ tool, target, bwRemoteAddress: target, ignored: true }).tool, tool) +for (const invalid of [[], null, {}, { tool: "ssh" }, { tool: "ping" }, { tool: "bandwidth", target }, { tool: "route", target: "::1" }, { tool: "route", target: "example.test" }]) refused(() => normalizeServerProbeRestRequest(invalid)) +for (const field of [{ pingCount: "5" }, { pingCount: 0 }, { pingSize: -1 }, { pingTtl: 1.2 }, { traceUseDns: "false" }, { traceProto: "junk" }, { traceMaxHops: 0 }, { traceHopTimeout: "00:99:01" }, { traceHopTimeout: "0s" }, { traceProbeCount: 0 }, { dnsType: "ANY" }, { target: {} }, { dstServerId: -1 }, { bwProto: "junk" }, { bwDuration: 0 }]) refused(() => normalizeServerProbeRestRequest({ tool: "ping", target, ...field })) +for (const raw of ["0", "-1", "01", "1oops", "1.2", "9007199254740993", " 1"]) refused(() => parseServerProbeRestServerId(raw)) +equal(parseServerProbeRestServerId("12"), 12) +for (const [raw, error] of [[{ tool: "ping" }, "Некорректные параметры диагностики: target"], [{ tool: "bandwidth" }, "Некорректные параметры диагностики: bwRemoteAddress"], [{ tool: "route", target: "x" }, "Некорректные параметры диагностики: target"]] as const) { + try { normalizeServerProbeRestRequest(raw); assert.fail("expected validation error") } catch (failure) { assert(failure instanceof ServerProbeRestContractError); equal(serverProbeRestErrorReply(failure), { statusCode: 400, body: { error } }) } +} + +equal(projectServerProbePing([{ seq: "0", ttl: "64", time: " 0ms " }, { seq: "1", status: "timeout" }, { seq: "2", ttl: 63, time: "1ms", status: "reply" }, { sent: "3", received: "2", "packet-loss": " 33% " }], target).output, + "PING 192.0.2.2\n seq=0 ttl=64 time=0ms\n seq=1 timeout\n seq=2 ttl=63 time=1ms status=reply\n sent=3 received=2 packet-loss=33%") +equal(projectServerProbePing([], target).output, "PING 192.0.2.2\n API не вернул измерений ping.") +equal(projectServerProbePing([{ status: "" }, { sent: "0", received: "0" }], target).output, "PING 192.0.2.2\n seq=? ttl=? time=—\n sent=0 received=0 packet-loss=—") +equal(projectServerProbeTrace([{ host: " hop.test ", "packet-loss": " 0% ", time: " 1ms ", "avg-rtt": " 2ms " }], plan({ tool: "traceroute", target })).output, + " # ADDRESS LOSS LAST AVG\n 1 hop.test 0% 1ms 2ms") +equal(projectServerProbeTrace([], capped).output, ";; REST API RouterOS: сессия ~60 с (параметры traceroute не продлевают лимит). max-hops снижен с 64 до 5 (count=3, timeout=00:00:03).\n # ADDRESS LOSS LAST AVG") +equal(projectServerProbeBandwidth([]).output, "(no bandwidth-test output)") +equal(projectServerProbeBandwidth([{ "tx-current": " 0 ", "rx-current": "", "test-duration": "1s" }, { "tx-total-average": "10Mbps", "rx-total-average": "20Mbps" }]).output, "bandwidth-test:\n tx=0 rx=— t=1s\n tx=10Mbps rx=20Mbps") +equal(projectServerProbeBandwidth([{ "tx-current": "", "tx-total-average": "99Mbps", "rx-current": 0 }]).output, "bandwidth-test:\n tx=— rx=0") + +const dnsFixtures: Array<{ type: string; raw: unknown; name?: string; expected: string }> = [ + { type: "A", raw: ["192.0.2.10", "192.0.2.11"], expected: "example.test. IN A 192.0.2.10\nexample.test. IN A 192.0.2.11" }, + { type: "AAAA", raw: ["2001:db8::1"], expected: "example.test. IN AAAA 2001:db8::1" }, + { type: "MX", raw: [{ priority: 20, exchange: "mx2.test" }, { priority: 10, exchange: "mx1.test" }], expected: "example.test. IN MX 10 mx1.test\nexample.test. IN MX 20 mx2.test" }, + { type: "NS", raw: ["ns1.test", "ns2.test"], expected: "example.test. IN NS ns1.test\nexample.test. IN NS ns2.test" }, + { type: "TXT", raw: [["hello", " world"], [""]], expected: "example.test. IN TXT \"hello world\"\nexample.test. IN TXT \"\"" }, + { type: "CNAME", raw: ["a.test", "b.test"], expected: "example.test. IN CNAME a.test,b.test" }, + { type: "CNAME", raw: [], expected: "example.test. IN CNAME " }, + { type: "PTR", raw: ["router.test"], name: "192.0.2.2", expected: "192.0.2.2.in-addr.arpa. IN PTR router.test" }, + { type: "PTR", raw: ["router.test"], name: "2.2.0.192.in-addr.arpa", expected: "2.2.0.192.in-addr.arpa. IN PTR router.test" }, + { type: "PTR", raw: ["router.test"], name: "reverse.test", expected: "reverse.test. IN PTR router.test" }, +] +for (const fixture of dnsFixtures) { + const name = fixture.name ?? "example.test" + equal(projectServerProbeDns(fixture.raw, name, fixture.type).output, `;; QUESTION: ${name} ${fixture.type}\n\n${fixture.expected}`) + equal(normalizeServerProbeRestRequest({ tool: "dns", target: name, dnsType: fixture.type.toLowerCase() }).dnsType, fixture.type) +} +equal(projectServerProbeDns([], "example.test", "A").output, ";; QUESTION: example.test A\n") +equal(projectServerProbeRoute([], target).output, "no route for 192.0.2.2 (route records checked: 0)") +equal(projectServerProbeRoute([{}], target).output, "Маршрут не подтверждён: неизвестных записей 1.") +const routeRows = [ + { active: "true", "dst-address": "0.0.0.0/0", gateway: "default", distance: "1" }, + { active: "true", "dst-address": "192.0.2.0/24", gateway: "wrong-vrf", "routing-table": "vrf", distance: "1" }, + { active: "false", "dst-address": "192.0.2.2/32", gateway: "inactive" }, + { active: "true", "dst-address": "192.0.2.0/24", gateway: "", distance: "5", "routing-mark": "" }, + { active: "yes", "dst-address": "192.0.2.0/24", gateway: "best", distance: "5", "routing-mark": "mark" }, + { active: "true", disabled: "true", "dst-address": "192.0.2.2/32", gateway: "disabled" }, + { active: "true", inactive: "true", "dst-address": "192.0.2.2/32", gateway: "inactive2" }, +] +equal(projectServerProbeRoute(routeRows, target).output, "lookup 192.0.2.2 → best match:\n dst-address: 192.0.2.0/24\n gateway: best\n distance: 5\n routing-mark: mark") +equal(projectServerProbeRoute([{ active: " true ", "dst-address": " 192.0.2.0 / 24 ", gateway: "", interface: "ether1", "routing-mark": "", distance: "junk" }], target).output, "lookup 192.0.2.2 → best confirmed match (неизвестных записей: 1):\n dst-address: 192.0.2.0 / 24 \n gateway: \n distance: —\n routing-mark: ") +equal(projectServerProbeRoute([{ active: "true", "dst-address": "192.0.2.2", interface: "ether1", distance: 0 }], target).output, "lookup 192.0.2.2 → best match:\n dst-address: 192.0.2.2\n gateway: ether1\n distance: 0\n routing-mark: —") +equal(classifyServerProbeMtu([{ received: "1" }]), "reply") +equal(classifyServerProbeMtu([{ seq: "0", time: "00:00:00.001" }]), "reply") +equal(classifyServerProbeMtu([{ status: "timeout" }]), "timeout") +equal(projectServerProbeMtu([{ size: 1500, result: "timeout" }, { size: 1492, result: "reply" }], target).output, "MTU discovery (do-not-fragment ping) → 192.0.2.2\n\n1500 timeout; причина не определена\n 1492 ✓ ok\n\nMTU (DF): ~1492 bytes") +const timeouts = [1500, 1492, 1480, 1476, 1472, 1468, 1400, 1280, 1024, 576].map(size => ({ size, result: "timeout" as const })) +equal(projectServerProbeMtu(timeouts, target).data.mtu, null) +assert(projectServerProbeMtu(timeouts, target).output.endsWith("\n\nMTU: could not determine")); cases++ +for (const raw of [[], [{}], [{ seq: "0", time: "junk" }], [{ received: "99999999999999999999999999" }]]) refused(() => classifyServerProbeMtu(raw)) +for (const measurements of [[], [{ size: 1492, result: "reply" as const }], [{ size: 1500, result: "timeout" as const }], [{ size: 1500, result: "reply" as const }, { size: 1492, result: "reply" as const }]]) refused(() => projectServerProbeMtu(measurements, target)) +for (const work of [() => projectServerProbePing([{ seq: "junk" }], target), () => projectServerProbeTrace("raw provider diagnostic", capped), () => projectServerProbeBandwidth([{ "tx-current": {} }]), () => projectServerProbeDns(["not IPv4"], "test", "A"), () => projectServerProbeDns([[123]], "test", "TXT"), () => projectServerProbeDns([123], "test", "NS"), () => projectServerProbeDns([], "test", "ANY")]) { assert.throws(work, ServerProbeDataError); cases++ } + +const peers: ServerProbeRestPeerMetadata[] = [{ id: 2, host: " PEER.test ", enabled: true }, { id: 3, host: "peer.test", enabled: true }, { id: 4, host: "disabled.test", enabled: false }] +const lookups: Array<{ kind: string; value: string | number; scope: readonly number[] }> = [] +const reader: ServerProbeRestPeerReader = { + async byId(id, scope) { lookups.push({ kind: "id", value: id, scope }); return peers.find(row => row.id === id && scope.includes(row.id)) }, + async enabledByHost(host, scope) { lookups.push({ kind: "host", value: host, scope }); return peers.filter(row => scope.includes(row.id) && row.enabled && row.host.trim().toLowerCase() === host) }, +} +equal(await resolveServerProbeRestBandwidthPeer("different.test", 3, [2, 3, 4], reader), peers[1]) +equal(lookups.at(-1), { kind: "id", value: 3, scope: [2, 3, 4] }) +equal(await resolveServerProbeRestBandwidthPeer(" PEER.TEST ", 999, [2, 3, 4], reader), peers[0]) +equal(lookups.slice(-2), [{ kind: "id", value: 999, scope: [2, 3, 4] }, { kind: "host", value: "peer.test", scope: [2, 3, 4] }]) +equal(await resolveServerProbeRestBandwidthPeer("peer.test", undefined, [3], reader), peers[1]) +for (const [remote, id, status] of [["peer.test", 4, 409], ["missing.test", undefined, 400]] as const) { + const before = lookups.length + await assert.rejects(resolveServerProbeRestBandwidthPeer(remote, id, [2, 3, 4], reader), (failure: unknown) => failure instanceof ServerProbeRestContractError && failure.statusCode === status); cases++ + if (id === 4) equal(lookups.length, before + 1) +} +await assert.rejects(resolveServerProbeRestBandwidthPeer("peer.test", undefined, [2], { ...reader, async enabledByHost() { return [peers[1]] } }), (failure: unknown) => failure instanceof ServerProbeRestContractError && failure.statusCode === 403); cases++ +const completed: ServerProbeReceipt = { receiptId: "00000000-0000-4000-8000-000000000001", tool: "ping", serverId: 1, state: "completed", httpStatus: 200, output: "PING fixture", data: { kind: "ping" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } +equal(serverProbeRestReceiptReply(completed), { statusCode: 200, body: { output: "PING fixture" } }) +for (const output of [";; QUESTION: test A\n;; ERROR: safe failure", " ERROR: safe failure"]) equal(serverProbeRestReceiptReply({ ...completed, output }), { statusCode: 502, body: { error: output } }) +for (const [code, status] of [["NOT_FOUND", 404], ["SOURCE_UNAVAILABLE", 409], ["DESTINATION_DISABLED", 409], ["DESTINATION_UNAVAILABLE", 400], ["CANCELLED", 502], ["MEASUREMENT_UNKNOWN", 502], ["JOURNAL_UNKNOWN", 503], ["PERMISSION_DENIED", 403]] as const) equal(serverProbeRestErrorReply({ code }).statusCode, status) +equal(serverProbeRestReceiptReply({ ...completed, state: "unknown", httpStatus: 503, errorCode: "JOURNAL_UNKNOWN", output: "must not publish" }).statusCode, 503) +assert(!JSON.stringify(serverProbeRestErrorReply(Object.assign(new Error("NEVER_RAW_PROVIDER_PASSWORD"), { code: "ECONNRESET" }))).includes("NEVER_RAW_PROVIDER_PASSWORD")); cases++ +console.log(`server probe REST contract: ${cases} inert parity cases passed`) diff --git a/backend/src/services/server-probe-rest-contract.ts b/backend/src/services/server-probe-rest-contract.ts new file mode 100644 index 00000000..df753906 --- /dev/null +++ b/backend/src/services/server-probe-rest-contract.ts @@ -0,0 +1,79 @@ +import { serverProbeExecutionRequest, type ServerProbeRequest } from "./server-probe-execution-model.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" + +/** REST compatibility boundary only. MCP continues using the strict schema. + * Inert: imports do not open the database, DNS, RouterOS or a journal. */ +export class ServerProbeRestContractError extends Error { + constructor(readonly statusCode: number, readonly code: string, message: string) { super(message) } +} +const knownKeys = new Set(Object.keys(serverProbeExecutionRequest.shape)) +export function normalizeServerProbeRestRequest(raw: unknown): ServerProbeRequest { + const candidate = raw && typeof raw === "object" && !Array.isArray(raw) + ? Object.fromEntries(Object.entries(raw).filter(([key]) => knownKeys.has(key))) : raw + const parsed = serverProbeExecutionRequest.safeParse(candidate) + if (!parsed.success) throw new ServerProbeRestContractError(400, "INVALID_INPUT", "Некорректные параметры диагностики: " + [...new Set(parsed.error.issues.map(issue => issue.path.join(".") || "body"))].join(", ")) + return parsed.data +} +export function parseServerProbeRestServerId(raw: string): number { + const value = /^[1-9][0-9]*$/.test(raw) ? Number(raw) : NaN + if (!Number.isSafeInteger(value)) throw new ServerProbeRestContractError(400, "INVALID_INPUT", "Invalid server id") + return value +} +export interface ServerProbeRestPeerMetadata { id: number; host: string; enabled: boolean } +export interface ServerProbeRestPeerReader { + /** SQL must restrict to allowed IDs BEFORE reading metadata. Never return + * credentials. An existing disabled explicit peer must still be returned. */ + byId(id: number, allowedServerIds: readonly number[]): Promise + /** Return enabled matches in the same repository order as legacy .find(). + * SQL must restrict scope BEFORE lookup; do not fetch all server secrets. */ + enabledByHost(host: string, allowedServerIds: readonly number[]): Promise +} +/** Exact legacy selection: existing explicit ID wins (even disabled); a + * missing ID falls back to first enabled case-insensitive trimmed host match. + * Scope is supplied by trusted authority, never by request JSON. */ +export async function resolveServerProbeRestBandwidthPeer(remoteHost: string, explicitId: number | undefined, allowedServerIds: readonly number[], reader: ServerProbeRestPeerReader): Promise { + if (!allowedServerIds.every(id => Number.isSafeInteger(id) && id > 0) || explicitId !== undefined && (!Number.isSafeInteger(explicitId) || explicitId <= 0)) throw new ServerProbeRestContractError(400, "INVALID_INPUT", "Некорректные параметры диагностики: dstServerId") + const safe = (row: ServerProbeRestPeerMetadata) => { + if (!Number.isSafeInteger(row.id) || !allowedServerIds.includes(row.id) || typeof row.host !== "string" || typeof row.enabled !== "boolean") throw new ServerProbeRestContractError(403, "PERMISSION_DENIED", "Нет актуального допуска диагностики обоих серверов") + return { id: row.id, host: row.host, enabled: row.enabled } + } + let selected: ServerProbeRestPeerMetadata | undefined + if (explicitId !== undefined) { + const row = await reader.byId(explicitId, allowedServerIds) + if (row) { selected = safe(row); if (selected.id !== explicitId) throw new ServerProbeRestContractError(409, "STATE_CONFLICT", "Назначение BW-теста изменилось") } + } + if (!selected) { + const normalized = remoteHost.trim().toLowerCase(), rows = await reader.enabledByHost(normalized, allowedServerIds) + for (const row of rows) { const checked = safe(row); if (!checked.enabled || checked.host.trim().toLowerCase() !== normalized) throw new ServerProbeRestContractError(409, "STATE_CONFLICT", "Назначение BW-теста изменилось"); if (!selected) selected = checked } + } + if (!selected) throw new ServerProbeRestContractError(400, "DESTINATION_UNAVAILABLE", "Не найден сервер назначения для bandwidth-test: добавьте узел с host = GRE remote или укажите dstServerId") + if (!selected.enabled) throw new ServerProbeRestContractError(409, "DESTINATION_DISABLED", "Устройство назначения выключено. BW-тест недоступен.") + return selected +} +export type ServerProbeRestReply = { statusCode: number; body: { output: string } | { error: string } } +/** Receipt must already have passed the executor's private binding, durable + * journal and redaction checks. Never accept raw provider output here. */ +export function serverProbeRestReceiptReply(receipt: ServerProbeReceipt): ServerProbeRestReply { + if (receipt.state !== "completed" || receipt.httpStatus !== 200 || typeof receipt.output !== "string") return serverProbeRestErrorReply({ code: receipt.errorCode, statusCode: receipt.httpStatus }) + if (/^;;\s*ERROR:/im.test(receipt.output) || /^error:/i.test(receipt.output.trimStart())) return { statusCode: 502, body: { error: receipt.output } } + return { statusCode: 200, body: { output: receipt.output } } +} +/** Safe public messages only; provider error.message must never be forwarded. */ +export function serverProbeRestErrorReply(failure: { code?: string; statusCode?: number }): ServerProbeRestReply { + if (failure instanceof ServerProbeRestContractError) return { statusCode: failure.statusCode, body: { error: failure.message } } + const known: Record = { + NOT_FOUND: [404, "Server not found"], + SOURCE_UNAVAILABLE: [409, "Устройство выключено. Диагностика недоступна."], + DESTINATION_DISABLED: [409, "Устройство назначения выключено. BW-тест недоступен."], + DESTINATION_UNAVAILABLE: [400, "Не найден сервер назначения для bandwidth-test: добавьте узел с host = GRE remote или укажите dstServerId"], + INVALID_INPUT: [400, "Некорректные параметры диагностики"], + CANCELLED: [502, "Запрос отменён (клиент закрыл соединение или истёк лимит времени для traceroute)."], + MEASUREMENT_UNKNOWN: [502, "Устройство не подтвердило структурированные измерения диагностики"], + PERMISSION_DENIED: [403, "Нет актуального допуска диагностики обоих серверов"], + STATE_CONFLICT: [409, "Источник или назначение диагностики изменились"], + JOURNAL_UNKNOWN: [503, "Исход диагностики неизвестен; автоматический повтор запрещён"], + REPLAY_FORBIDDEN: [409, "Попытка диагностики уже использована; автоматический повтор запрещён"], + } + const [statusCode, error] = known[failure.code ?? ""] ?? [502, "Не удалось получить подтверждённый результат диагностики"] + return { statusCode, body: { error } } +} diff --git a/backend/src/services/server-probe-rest-execution.test.ts b/backend/src/services/server-probe-rest-execution.test.ts new file mode 100644 index 00000000..249e87aa --- /dev/null +++ b/backend/src/services/server-probe-rest-execution.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict" +import { createHmac, randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import { McpError } from "../modules/mcp/security.js" +import { createServerProbeExecutor, type ServerProbeAuthority, type ServerProbeEndpoint, type ServerProbeExecutionDependencies, type ServerProbeJournal, type ServerProbeReceipt } from "./server-probe-execution.js" +import { createServerProbeRestExecution } from "./server-probe-rest-execution.js" +import type { ServerProbeJournalContext } from "./server-probe-journal.js" + +// The real common executor is exercised; only transport/storage/authority I/O +// is synthetic. Importing/running this profile must never contact an endpoint. +const oldQuery = pool.query, oldConnect = pool.connect +pool.query = (() => { throw Error("Actual SQL forbidden") }) as never +pool.connect = (() => { throw Error("Actual SQL forbidden") }) as never +let cases = 0 +function fixture() { + const now = Date.now(), controller = new AbortController() + const authority: ServerProbeAuthority = { transport: "rest", ownerId: "local-operator", requestId: randomUUID(), issuedAt: now, expiresAt: now + 300000, generation: "epoch", policyProof: "a".repeat(64), ownerProof: "b".repeat(64), active: true, operationAllowed: true, scopeServerIds: [1, 2], ownerPermissions: ["mm:servers:write"], policyPermissions: ["mm:servers:write"] } + const endpoint: ServerProbeEndpoint = { id: 1, enabled: true, host: "192.0.2.1", port: 80, username: "PRIVATE_USER", password: "PRIVATE_PASSWORD", useSsl: false, verifySsl: true, routingEndpointEpoch: randomUUID() } + const sealed = new Map(), claims = new Map(), receipts = new Map() + let raw: unknown = [{ seq: "0", ttl: "64", time: "1ms" }], journalContext: ServerProbeJournalContext | undefined, guards: (() => Promise) | undefined + let sends = 0, claimsAttempted = 0, loseClaim = false, corruptRead = false, sourceReads = 0, actorCalls = 0, claimProof = "", afterSend: (() => void) | undefined + const access = { async readAuthority() { if (!authority.active) throw new McpError("OWNER_CLAIMS_REVOKED", "PRIVATE_REVOKE", 401); return structuredClone(authority) }, async assertProtections() {} } + const journal: ServerProbeJournal = { + async claim(binding) { claimsAttempted++; claimProof = binding.bindingProof; await guards!(); if (loseClaim) throw Error("PRIVATE_COMMIT_ACK"); if (claims.has(binding.receiptId)) return "exists"; claims.set(binding.receiptId, binding.bindingProof); return "claimed" }, + async intent() { await guards!() }, async received() { await guards!() }, + async finish(receipt) { await guards!(); receipts.set(receipt.receiptId, structuredClone(receipt)) }, + async quarantine(binding) { receipts.set(binding.receiptId, { receiptId: binding.receiptId, tool: binding.tool as ServerProbeReceipt["tool"], serverId: 1, state: "unknown", httpStatus: 503, errorCode: binding.errorCode, attemptedStages: binding.attemptedStages, completedStages: binding.completedStages, automaticReplay: false, forwardingVerified: false }) }, + async read(id) { await guards!(); const row = receipts.get(id); return row && corruptRead ? { ...row, output: "FORGED_PUBLIC_RESULT" } : row }, + } + const dependencies: Omit = { + ...access, async getServer(id) { sourceReads++; return [1, 2].includes(id) ? { ...endpoint, id, host: `192.0.2.${id}` } : undefined }, + async findDestination() { return { id: 2 } }, + async privateProof(domain, value) { return createHmac("sha256", "test-key").update(domain).update(JSON.stringify(value)).digest("hex") }, + async seal(value) { const id = randomUUID(); sealed.set(id, value); return id }, async open(id) { return sealed.get(id)! }, + async post(_source, _path, _body, _timeout, signal, fence) { signal.throwIfAborted(); await fence(); sends++; afterSend?.(); return structuredClone(raw) }, + async routes(_source, _timeout, signal, fence) { signal.throwIfAborted(); await fence(); sends++; afterSend?.(); return structuredClone(raw) }, + async dns(_name, _type, signal, fence) { signal.throwIfAborted(); await fence(); sends++; afterSend?.(); return structuredClone(raw) }, + } + const run = createServerProbeRestExecution({ authority: () => access, + executor: injected => createServerProbeExecutor({ ...dependencies, ...injected }), + journal: (context, deps) => { journalContext = context; guards = () => deps.assertAuthority!(context, claimProof); return journal }, + actor: async (id, work) => { assert.equal(id, authority.ownerId); actorCalls++; return work() }, + }) + return { authority, controller, receipts, + async run(tool = "ping", serverId = "1", extra: Record = {}) { return run(serverId, { tool, target: "192.0.2.2", bwRemoteAddress: "192.0.2.2", ...extra }, { requestId: authority.requestId!, acceptedAt: now, authRequired: false, signal: controller.signal }) }, + raw(value: unknown) { raw = value }, loseClaim() { loseClaim = true }, corruptRead() { corruptRead = true }, afterSend(hook: () => void) { afterSend = hook }, + counts() { return { sends, claimsAttempted, sourceReads, actorCalls } }, context() { return journalContext }, + } +} +try { + for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) { + const f = fixture() + if (tool === "traceroute") f.raw([{ address: "192.0.2.2", loss: "0%", last: "1ms", avg: "1ms" }]) + if (tool === "bandwidth") f.raw([{ "tx-current": "1", "rx-current": "2" }]) + if (tool === "dns") f.raw(["192.0.2.2"]) + if (tool === "route") f.raw([{ ".id": "*1", "dst-address": "192.0.2.0/24", active: "true", gateway: "ether1", distance: "1" }]) + const reply = await f.run(tool, "1", { callerSuppliedIdentity: "ignored", executionId: "ignored" }) + assert.equal(reply.statusCode, 200, JSON.stringify(reply)); assert.equal(typeof (reply.body as { output: string }).output, "string") + assert(!JSON.stringify(reply).includes("PRIVATE_")); assert.equal(f.counts().claimsAttempted, 1); assert.equal(f.counts().actorCalls, 1) + const context = f.context()!; assert.equal(context.identity.kind, "rest"); assert.equal("connectionId" in context.identity, false); assert.equal(context.sourceServerId, 1) + assert.notEqual(context.identity.executionId, "ignored"); assert.equal(context.destinationServerId, tool === "bandwidth" ? 2 : undefined); cases++ + } + { + const f = fixture(); assert.equal((await f.run("ping", "01")).statusCode, 400); assert.equal(f.counts().sourceReads, 0); assert.equal(f.context(), undefined); cases++ + } + { + const f = fixture(); f.loseClaim(); const reply = await f.run(); assert.equal(reply.statusCode, 503); assert.equal(f.counts().sends, 0); assert(!JSON.stringify(reply).includes("PRIVATE_")); cases++ + } + { + const f = fixture(); f.corruptRead(); const reply = await f.run(); assert.notEqual(reply.statusCode, 200); assert(!JSON.stringify(reply).includes("FORGED_PUBLIC_RESULT")); assert.equal(f.counts().sends, 1); cases++ + } + { + const f = fixture(); f.afterSend(() => { f.authority.active = false }); const reply = await f.run(); assert.notEqual(reply.statusCode, 200); assert(!JSON.stringify(reply).includes("PRIVATE_")); assert.equal(f.counts().sends, 1); cases++ + } + { + const f = fixture(); f.controller.abort(); const reply = await f.run(); assert.notEqual(reply.statusCode, 200); assert.equal(f.counts().sends, 0); assert.equal(f.counts().claimsAttempted, 0); cases++ + } + console.log(`REST common diagnostic execution: ${cases} inert cases passed; all six real executor paths, owned REST identity, admission failure, exact persisted receipt and late revoke/abort; no actual SQL/DNS/RouterOS`) +} finally { pool.query = oldQuery; pool.connect = oldConnect } diff --git a/backend/src/services/server-probe-rest-execution.ts b/backend/src/services/server-probe-rest-execution.ts new file mode 100644 index 00000000..88535888 --- /dev/null +++ b/backend/src/services/server-probe-rest-execution.ts @@ -0,0 +1,59 @@ +import { randomUUID } from "node:crypto" +import type { AuthUser } from "../lib/permissions.js" +import { McpError, fingerprint } from "../modules/mcp/security.js" +import { createProductionServerProbeExecutor, ServerProbeExecutionError } from "./server-probe-execution.js" +import { createRestServerProbeAuthority } from "./server-probe-authority.js" +import { createSqlServerProbeJournal } from "./server-probe-journal.js" +import { normalizeServerProbeRestRequest, parseServerProbeRestServerId, serverProbeRestErrorReply, serverProbeRestReceiptReply } from "./server-probe-rest-contract.js" +import { withConfigurationActor } from "./config-mutation-policy.js" +import type { ServerProbeRequest } from "./server-probe-execution-model.js" +import type { ProbeStorageHostJournal } from "./server-probe-storage-host-io.js" + +export interface ServerProbeRestContext { actor?: AuthUser; requestId: string; acceptedAt: number; authRequired: boolean; signal?: AbortSignal } +export interface ServerProbeRestTarget { serverId: number; request: ServerProbeRequest } +const authority = (actor: AuthUser | undefined, context: ServerProbeRestContext, _target?: ServerProbeRestTarget) => createRestServerProbeAuthority(actor, context) +const journal: ProbeStorageHostJournal = createSqlServerProbeJournal +const production = { authority, executor: createProductionServerProbeExecutor, journal, executionId: randomUUID, actor: withConfigurationActor } +export type ServerProbeRestExecutionDependencies = typeof production + +/** One common executor for every REST tool. A fresh server-owned execution UUID + * is never accepted from JSON; REST has no fabricated MCP connection/operation. + * Importing this module neither registers a route nor starts any collector. */ +export function createServerProbeRestExecution(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + return async (rawServerId: string, body: unknown, context: ServerProbeRestContext) => { + try { + const serverId = parseServerProbeRestServerId(rawServerId), request = normalizeServerProbeRestRequest(body) + const access = deps.authority(context.actor, context, { serverId, request }), planner = deps.executor(access) + const initial = await access.readAuthority() + if (initial.transport !== "rest" || initial.connectionId !== undefined || initial.credentialId !== undefined) throw new McpError("STATE_CONFLICT", "HTTP-контекст диагностики изменился", 409) + // REST write permission covers the fleet. Existence is checked only after + // current authentication and before reading source credentials. + if (!initial.scopeServerIds.includes(serverId)) throw new ServerProbeExecutionError("NOT_FOUND", "Server not found", 404) + const prepared = await planner.prepare(serverId, request, context.signal), before = await planner.preflight(prepared, context.signal) + const principal = await access.readAuthority() + if (principal.transport !== "rest" || principal.connectionId !== undefined || principal.credentialId !== undefined || !principal.requestId || principal.issuedAt === undefined) throw new McpError("STATE_CONFLICT", "HTTP-контекст диагностики изменился", 409) + const journal = deps.journal({ identity: { kind: "rest", authentication: context.actor ? "jwt" : "local", ownerId: principal.ownerId, executionId: deps.executionId(), requestId: principal.requestId, issuedAt: principal.issuedAt, expiresAt: principal.expiresAt }, sourceServerId: prepared.serverId, ...(prepared.destinationServerId !== undefined ? { destinationServerId: prepared.destinationServerId } : {}), tool: prepared.tool }, { + assertAuthority: async (_scope, bindingProof) => { + if (bindingProof !== before.bindingProof) throw new McpError("STATE_CONFLICT", "Привязка журнала диагностики изменилась", 409) + const fresh = await planner.preflight(prepared, context.signal) + if (fingerprint(fresh) !== fingerprint(before)) throw new McpError("STATE_CONFLICT", "Допуск или источник диагностики изменился", 409) + }, + }, { signal: context.signal, deadlineAt: Date.parse(prepared.expiresAt) }) + const executor = deps.executor({ ...access, journal }) + return await deps.actor(principal.ownerId, async () => { + const receipt = await executor.execute(prepared, context.signal) + await planner.preflight(prepared, context.signal) + const recorded = await journal.read(prepared.receiptId, before.bindingProof) + if (!recorded || fingerprint(recorded) !== fingerprint(receipt)) throw new McpError("STATE_CONFLICT", "Нет точной постоянной квитанции диагностики", 409) + await planner.preflight(prepared, context.signal) + return serverProbeRestReceiptReply(recorded) + }) + } catch (failure) { + if (failure instanceof McpError && ["UNAUTHORIZED", "OWNER_CLAIMS_REVOKED"].includes(failure.code)) return { statusCode: 401, body: { error: "Доступ отозван; требуется новый вход" } } + if (failure instanceof McpError && ["PROTECTED_OBJECT", "APPROVAL_REQUIRED"].includes(failure.code)) return { statusCode: 409, body: { error: "Диагностика требует разрешённого плана MCP" } } + return serverProbeRestErrorReply(failure && typeof failure === "object" ? failure as { code?: string; statusCode?: number } : {}) + } + } +} +export const executeServerProbeRest = createServerProbeRestExecution() diff --git a/backend/src/services/server-probe-retention.sql-helper.ts b/backend/src/services/server-probe-retention.sql-helper.ts new file mode 100644 index 00000000..614aea77 --- /dev/null +++ b/backend/src/services/server-probe-retention.sql-helper.ts @@ -0,0 +1,32 @@ +import assert from "node:assert/strict" +import { basename, dirname, resolve } from "node:path" + +/** Uses the owned historical storage fixture's already elapsed admissions. + * Fresh production admission guards are never disabled or time-shifted. */ +export async function acceptServerProbeRetentionSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { retainServerProbeStorage } = await import("./server-probe-retention.js") + const before = (await pool.query("SELECT receipt_id,owner_id,purged_at FROM server_probe_storage_admissions ORDER BY receipt_id")).rows + const expected = before.find(row => row.owner_id === "expired-rollback-owner") + assert(expected, "Historical rollback fixture must still contain an elapsed admission") + const usage = (await pool.query("SELECT * FROM server_probe_storage_usage WHERE scope='global'")).rows[0] + let collected = 0 + for (let round = 0; round < 8; round++) { + collected += (await retainServerProbeStorage()).collected + if (!(await pool.query("SELECT 1 FROM server_probe_storage_admissions WHERE receipt_id=$1", [expected.receipt_id])).rows.length) break + } + const after = (await pool.query("SELECT receipt_id FROM server_probe_storage_admissions ORDER BY receipt_id")).rows + const removed = before.filter(row => !after.some(current => current.receipt_id === row.receipt_id)) + assert(removed.some(row => row.receipt_id === expected.receipt_id)); assert.equal(removed.length, collected) + const files = removed.filter(row => row.purged_at === null).length + const current = (await pool.query("SELECT * FROM server_probe_storage_usage WHERE scope='global'")).rows[0] + assert.equal(Number(current.admissions), Number(usage.admissions) - removed.length) + assert.equal(Number(current.reserved_files), Number(usage.reserved_files) - files) + assert.equal(Number(current.reserved_bytes), Number(usage.reserved_bytes) - removed.length * 32768 - files * 262144) + return { actualProductionRetention: true, expiredAdmissionCollected: true, exactMetadataAndPayloadRefund: true, + freshGuardsNeverDisabled: true, ownerBatchesBounded: true, nativeCalls: 0, removed: removed.length } +} diff --git a/backend/src/services/server-probe-retention.test.ts b/backend/src/services/server-probe-retention.test.ts new file mode 100644 index 00000000..050b408c --- /dev/null +++ b/backend/src/services/server-probe-retention.test.ts @@ -0,0 +1,81 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createServerProbeRetentionForTests } from "./server-probe-retention.js" +import { ProbeStorageUnknown, type ProbeStorageConnection } from "./server-probe-journal-maintenance.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" + +function fixture(owner = false) { + const commands: string[] = [], releases: boolean[] = [] + let settingsReads = 0, driftAfter = Infinity, failCommit = false, checkouts = 0 + const connection: ProbeStorageConnection = { + async query(text) { + commands.push(text) + if (text.includes("probe-retention:settings")) return { rows: [{ requests_retention_days: ++settingsReads > driftAfter ? 31 : 30 }] } + if (text.includes("probe-retention:owners")) return { rows: owner ? [{ owner_id: "owned-retention" }] : [] } + if (text.includes("probe-storage:counters")) return { rows: [{ scope: "global" }, { scope: "owner:owned-retention" }] } + if (text.includes("probe-storage:gc-candidates")) return { rows: [] } + if (text === "COMMIT" && failCommit) throw Error("PRIVATE_DRIVER_ERROR") + return { rows: [] } + }, release(unsafe) { releases.push(!!unsafe) }, + } + const run = createServerProbeRetentionForTests(async options => { + checkouts++; assert(options.deadlineAt <= Date.now() + 20000); return connection + }) + return { run, commands, releases, checkouts: () => checkouts, drift: (after = 5) => { driftAfter = after }, failCommit: () => { failCommit = true } } +} +test("empty retention uses one bounded backend and no mutation", async () => { + const f = fixture(); assert.deepEqual(await f.run(), { skipped: false, owners: 0, collected: 0 }) + assert.equal(f.checkouts(), 1); assert.deepEqual(f.releases, [false]) + assert.equal(f.commands.length, 2); assert(f.commands.every(text => text.includes("SELECT"))) +}) +test("selected owner GC reuses one physical backend for policy fences and transaction", async () => { + const f = fixture(true) + assert.deepEqual(await f.run(), { skipped: false, owners: 1, collected: 0 }) + assert.equal(f.checkouts(), 1); assert.deepEqual(f.releases, [false]) + assert.equal(f.commands.filter(text => text === "BEGIN").length, 1) + assert.equal(f.commands.filter(text => text === "COMMIT").length, 1) + const begin = f.commands.indexOf("BEGIN") + const writer = f.commands.indexOf("SELECT pg_advisory_xact_lock_shared($1::bigint)") + const storage = f.commands.findIndex(text => text.includes("probe-storage:lock")) + const recheck = f.commands.findIndex((text, index) => index > begin && text.includes("probe-retention:settings")) + assert(begin < writer && writer < recheck && recheck < storage) + assert(!f.commands.some(text => /DELETE FROM|\/tool\//.test(text))) +}) +test("retention drift rolls back without committing cleanup", async () => { + const f = fixture(true); f.drift(); await assert.rejects(f.run()) + assert(!f.commands.includes("COMMIT")); assert(f.commands.includes("ROLLBACK")) + assert.equal(f.releases.length, 1) +}) +test("policy drift on the first in-transaction recheck still destroys the checkout before storage admission", async () => { + const f = fixture(true); f.drift(3) + await assert.rejects(f.run(), ProbeStorageUnknown) + assert(f.commands.includes("SELECT pg_advisory_xact_lock_shared($1::bigint)")) + assert(!f.commands.some(text => /probe-storage:lock|probe-storage:owner-counter|COMMIT|ROLLBACK/.test(text))) + assert.deepEqual(f.releases, [true]) +}) +test("unknown commit destroys once and does not repeat the maintenance command", async () => { + const f = fixture(true); f.failCommit(); await assert.rejects(f.run()) + assert.equal(f.commands.filter(text => text === "COMMIT").length, 1) + assert.deepEqual(f.releases, [true]) +}) + +test("maintenance does not overlap and database replacement drains its real lifetime", async () => { + let entered!: () => void, resume!: () => void, checkouts = 0, replacementStarted = false + const started = new Promise(resolve => { entered = resolve }) + const pending = new Promise(resolve => { resume = resolve }) + const run = createServerProbeRetentionForTests(async () => { + checkouts++ + return { async query(text) { + if (text.includes("probe-retention:settings")) { + entered(); await pending; return { rows: [{ requests_retention_days: 30 }] } + } + return { rows: [] } + }, release() {} } + }) + const first = run(); await started + assert.deepEqual(await run(), { skipped: true, owners: 0, collected: 0 }); assert.equal(checkouts, 1) + const replacement = sharedDatabaseGate.exclusive(async () => { replacementStarted = true }) + await Promise.resolve(); assert.equal(replacementStarted, false) + resume(); await first; await replacement + assert.equal(replacementStarted, true) +}) diff --git a/backend/src/services/server-probe-retention.ts b/backend/src/services/server-probe-retention.ts new file mode 100644 index 00000000..da1d564e --- /dev/null +++ b/backend/src/services/server-probe-retention.ts @@ -0,0 +1,115 @@ +/** Fixed background maintenance. No caller-selected owner, policy, SQL or native + * executor; expiry predicates and quota refunds remain in the approved store. */ +import { fingerprint } from "../modules/mcp/security.js" +import { PROBE_STORAGE_LIMITS } from "../modules/mcp/diagnostic-cancellation-storage.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import { createProbeJournalStorage, ProbeStorageRefused, type ProbeStorageConnection, type ProbeStorageOptions } from "./server-probe-journal-maintenance.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" +import { captureServerProbeTerminalProof } from "./server-probe-operation-retirement.js" + +/** Independent from the expiry transaction. A busy, malformed, or uncertain + * source cannot extend retention of its ciphertext or reuse a checkout. */ +async function captureBeforeExpiry() { + const deadlineAt = Date.now() + 2200 + let candidates: Record[] + try { + const sql = await connectProbeStorage({ deadlineAt: Math.min(deadlineAt, Date.now()+500) }) + let read = false + try { + candidates = (await sql.query(`/* probe-retention:capture-candidates */ + SELECT a.receipt_id,o.id AS operation_id,o.connection_id + FROM public.server_probe_storage_admissions a + JOIN public.server_probe_execution_payloads b USING(receipt_id) + JOIN public.mcp_operations o ON o.id=a.execution_id + JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id + JOIN public.mcp_connections c ON c.id=o.connection_id AND c.owner_id=a.owner_id + WHERE a.identity_kind='mcp' AND a.purged_at IS NULL AND a.cancelled_at IS NULL AND a.claim_token IS NULL + AND a.claim_attempted_at IS NOT NULL AND a.authority_expires_at= deadlineAt || candidates.length > 8) break + if (typeof row.receipt_id !== "string" || typeof row.operation_id !== "string" || typeof row.connection_id !== "string") continue + try { + await captureServerProbeTerminalProof({ receiptId:row.receipt_id, operationId:row.operation_id, connectionId:row.connection_id }, + Math.min(deadlineAt, Date.now()+550)) + } catch { /* Never hold the payload or retry a capture in this window. */ } + } +} + +function createRetention(connect: typeof connectProbeStorage, preCapture?: () => Promise) { + let running = false, cursor = "" + return async () => { + if (running) return { skipped: true, owners: 0, collected: 0 } + running = true + try { + return await sharedDatabaseGate.request(async () => { + await preCapture?.() + const options: ProbeStorageOptions = { deadlineAt: Date.now() + 20000 } + const physical = await connect(options) + let active = true, broken = false, released = false, currentOwner = "" + const destroy = () => { broken = true; if (!released) { released = true; physical.release(true) } } + const fence = () => { if (!active || broken || Date.now() >= options.deadlineAt) throw new ProbeStorageRefused() } + const query: ProbeStorageConnection["query"] = async (text, values, queryOptions) => { + fence() + try { + const result = await physical.query(text, values, { timeoutMs: Math.max(1, Math.min(10000, options.deadlineAt - Date.now(), queryOptions?.timeoutMs ?? 10000)) }) + fence(); return result + } catch { destroy(); throw new ProbeStorageRefused() } + } + const logical: ProbeStorageConnection = { query, release(unsafe) { if (unsafe) destroy() } } + const retention = async () => { + const rows = (await query("/* probe-retention:settings */ SELECT requests_retention_days FROM mcp_settings WHERE id=1")).rows + const days = rows[0]?.requests_retention_days + if (rows.length !== 1 || typeof days !== "number" || !Number.isInteger(days) || days < 1 || days > 365) throw new ProbeStorageRefused() + return days + } + try { + const days = await retention() + const storage = createProbeJournalStorage({ connect: async () => logical, + policy: { ...PROBE_STORAGE_LIMITS, retentionDays: days }, safetyWindowMs: 60000, + authorize: async (ownerId, operation, scope) => { + fence() + if (!currentOwner || ownerId !== currentOwner || operation !== "purge" || scope) throw new ProbeStorageRefused() + const currentDays = await retention() + if (currentDays !== days) throw new ProbeStorageRefused() + return fingerprint({ format: "server-probe-retention-v1", ownerId, retentionDays: days }) + } }) + let owners = (await query(`/* probe-retention:owners */ SELECT DISTINCT owner_id FROM server_probe_storage_admissions + WHERE owner_id>$1 ORDER BY owner_id LIMIT 16`, [cursor])).rows + if (!owners.length && cursor) { + cursor = "" + owners = (await query(`/* probe-retention:owners */ SELECT DISTINCT owner_id FROM server_probe_storage_admissions + WHERE owner_id>$1 ORDER BY owner_id LIMIT 16`, [cursor])).rows + } + if (owners.length > 16 || owners.some(row => typeof row.owner_id !== "string" || !row.owner_id || row.owner_id.length > 200) + || new Set(owners.map(row => row.owner_id)).size !== owners.length) throw new ProbeStorageRefused() + let collected = 0, completedOwners = 0 + for (const row of owners) { + fence(); currentOwner = row.owner_id as string + const result = await storage.collectExpired(currentOwner, 25, options) + collected += result.collected; completedOwners++; cursor = currentOwner + } + return { skipped: false, owners: completedOwners, collected } + } finally { + active = false + // A failed/uncertain maintenance round never reuses its backend. + if (!released) { released = true; physical.release(broken) } + } + }) + } finally { running = false } + } +} + +export const retainServerProbeStorage = createRetention(connectProbeStorage, captureBeforeExpiry) +/** Trusted owned-test composition only; never an HTTP/MCP parameter. */ +export const createServerProbeRetentionForTests = createRetention diff --git a/backend/src/services/server-probe-retired-history.ts b/backend/src/services/server-probe-retired-history.ts new file mode 100644 index 00000000..374d99a3 --- /dev/null +++ b/backend/src/services/server-probe-retired-history.ts @@ -0,0 +1,67 @@ +/** Fixed private source lookup. The action owns the current authority and + * protection fence, and the existing history reader authenticates payload. */ +import { z } from "zod" +import { fingerprint } from "../modules/mcp/security.js" +import { openServerProbeRetiredSource, ServerProbeRetirementSourceRefused } from "./server-probe-retired-source.js" + +type Query=(sql:string,values:unknown[])=>Promise[]> +export interface ServerProbeRetiredLookup { + receiptId:string + operationId:string + connectionId:string + ownerId:string + scopeServerIds:readonly number[] +} + +const SQL=`/* probe-history:retired-source */ SELECT r.source,r.proof,t.plan_id,t.target_server_ids::text[] AS tombstone_server_ids, + j.source_server_id,j.destination_server_id,j.tool,j.mcp_credential_id,j.authority_issued_at, + j.receipt_proof,a.binding_proof,a.retention_days,a.safety_ms, + to_char(a.authority_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS authority_expires_at, + to_char(a.gc_not_before AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS gc_not_before, + a.purged_at IS NOT NULL AS payload_purged,p.receipt_proof AS payload_proof, + CASE WHEN p.receipt_ciphertext IS NOT NULL THEN pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(p.receipt_ciphertext,'UTF8')),'hex') END AS payload_sha256 + FROM public.server_probe_retired_sources r + JOIN public.mcp_operation_replay_tombstones t ON t.operation_id=r.operation_id AND t.connection_id=$3 + JOIN public.mcp_connections c ON c.id=t.connection_id AND c.owner_id=$4 + JOIN public.server_probe_storage_admissions a ON a.receipt_id=r.receipt_id + JOIN public.server_probe_executions j ON j.receipt_id=a.receipt_id AND j.execution_id=a.execution_id AND j.identity_kind=a.identity_kind + LEFT JOIN public.server_probe_execution_payloads p ON p.receipt_id=a.receipt_id + WHERE r.receipt_id=$1 AND r.operation_id=$2 AND a.execution_id=r.operation_id AND a.identity_kind='mcp' + AND a.owner_id=c.owner_id AND a.claim_token IS NULL AND isfinite(a.claim_attempted_at) AND a.cancelled_at IS NULL + AND (a.purged_at IS NULL OR isfinite(a.purged_at)) + AND j.actor_id=c.owner_id AND j.mcp_connection_id=c.id AND j.authentication='mcp' AND j.request_id IS NULL + AND j.binding_proof=a.binding_proof AND j.authority_expires_at=a.authority_expires_at AND j.state='completed' + AND t.action_id='servers.probes.run' AND NOT t.target_global AND cardinality(t.target_server_ids) BETWEEN 1 AND 2 + AND j.source_server_id=ANY($5::int[]) AND (j.destination_server_id IS NULL OR j.destination_server_id=ANY($5::int[])) + AND NOT EXISTS(SELECT 1 FROM public.mcp_operations WHERE id=r.operation_id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_plans WHERE id=t.plan_id) + AND NOT EXISTS(SELECT 1 FROM public.mcp_operation_steps WHERE operation_id=r.operation_id) + AND NOT EXISTS(SELECT 1 FROM public.server_probe_execution_quarantines q + WHERE q.receipt_id=r.receipt_id OR(q.identity_kind='mcp' AND q.execution_id=r.operation_id)) + LIMIT 2` +const sid=z.number().int().positive().safe() +const ids=z.array(z.string().regex(/^[1-9]\d{0,15}$/).transform(Number).pipe(sid)).min(1).max(2) + +export async function findRetiredServerProbeHistory(query:Query,input:ServerProbeRetiredLookup) { + const rows=await query(SQL,[input.receiptId,input.operationId,input.connectionId,input.ownerId,input.scopeServerIds]) + if(rows.length!==1)return + const row=rows[0] + const source=await openServerProbeRetiredSource({source:row.source,proof:row.proof}).catch(error=>{ + if(error instanceof ServerProbeRetirementSourceRefused)return + throw error + }) + const parsedScope=ids.safeParse(row.tombstone_server_ids) + if(!source||!parsedScope.success)return + const scope=parsedScope.data.sort((a,b)=>a-b) + if(source.receiptId!==input.receiptId||source.executionId!==input.operationId||source.connectionId!==input.connectionId|| + source.ownerId!==input.ownerId||source.planId!==row.plan_id||fingerprint(source.serverIds)!==fingerprint(scope)|| + source.serverIds.some(id=>!input.scopeServerIds.includes(id))||source.sourceServerId!==row.source_server_id|| + (source.destinationServerId??null)!==row.destination_server_id||source.tool!==row.tool|| + source.originalCredentialId!==row.mcp_credential_id||String(source.issuedAt)!==String(row.authority_issued_at)|| + source.bindingProof!==row.binding_proof||source.receiptProof!==row.receipt_proof||source.retentionDays!==row.retention_days|| + source.safetyMs!==row.safety_ms||source.authorityExpiresAt!==row.authority_expires_at||source.gcNotBefore!==row.gc_not_before)return + if(row.payload_purged===true){ + if(row.payload_proof!==null||row.payload_sha256!==null)return + }else if(row.payload_purged!==false||row.payload_proof!==source.receiptProof||row.payload_sha256!==source.ciphertextSha256)return + return {source,payloadPurged:row.payload_purged} +} diff --git a/backend/src/services/server-probe-retired-source.fixture.ts b/backend/src/services/server-probe-retired-source.fixture.ts new file mode 100644 index 00000000..bf63028f --- /dev/null +++ b/backend/src/services/server-probe-retired-source.fixture.ts @@ -0,0 +1,50 @@ +import { randomUUID } from "node:crypto" +import { encryptSecret,fingerprint,privateSecretFingerprint,redact } from "../modules/mcp/security.js" +import { serverProbeReceiptMetadata } from "../modules/mcp/server-probe-actions.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" +import type { ServerProbeHistoryBinding } from "./server-probe-journal-history.js" +export async function serverProbeRetiredFixture(twoSides=false, identity?:{connectionId:string;credentialId:string;ownerId:string}, + historical={base:Date.parse("2025-01-02T00:00:00Z"),retentionDays:365}){ + const operationId=randomUUID(),receiptId=randomUUID(),connectionId=identity?.connectionId??randomUUID(),credentialId=identity?.credentialId??randomUUID(),ownerId=identity?.ownerId??randomUUID() + const {base,retentionDays}=historical,stamp=(ms:number)=>new Date(base+ms).toISOString() + const originalTimes={issuedAt:base,authorityExpiresAt:base+15000} + const tool=twoSides?"bandwidth":"ping",sourceServerId=9,destination=twoSides?{destinationServerId:11}:{} + const prepared={receiptId,tool,serverId:sourceServerId,...destination,expiresAt:stamp(15000),bindingCiphertext:"inert-private-inner-binding"} + const binding={version:1,operation:"servers.probes.run",principal:{connectionId,credentialId,credentialExpiresAt:stamp(30000)}, + ownerId,...originalTimes,prepared,bindingProof:"a".repeat(64),stageBudget:twoSides?1:2} + const proof=(domain:string,value:unknown)=>privateSecretFingerprint(fingerprint(value),domain) + const input={serverId:sourceServerId,receiptId,inputDigest:await proof("server-probe-mcp-binding-v1",binding), + bindingCiphertext:await encryptSecret(JSON.stringify(binding))} + const step={kind:"service",service:binding.operation,input} + const serverIds=twoSides?[9,11]:[9] + const evidence={steps:[step],requiresHuman:true,verificationScope:"acknowledgement",mutationCost:binding.stageBudget, + target:{scope:"servers",serverIds},validUntil:prepared.expiresAt} + const context:ServerProbeHistoryBinding["context"]={identity:{kind:"mcp",operationName:"servers.probes.run",ownerId, + connectionId,credentialId,executionId:operationId,issuedAt:base,expiresAt:base+15000},sourceServerId,...destination,tool} + const receipt:ServerProbeReceipt={receiptId,serverId:sourceServerId,...destination,tool,state:"completed",httpStatus:200, + attemptedStages:1,completedStages:1,output:"INERT_RESULT_ONLY_IN_ORIGINAL_PAYLOAD",data:{kind:tool},automaticReplay:false,forwardingVerified:false} + const metadata=serverProbeReceiptMetadata(receipt,operationId,binding.stageBudget) + const plaintext=JSON.stringify({version:1,context,bindingProof:binding.bindingProof,receipt}) + const payload={receipt_id:receiptId,receipt_proof:await proof("server-probe-journal-v1:receipt",plaintext),receipt_ciphertext:await encryptSecret(plaintext)} + const anchor={receipt_id:receiptId,binding_proof:binding.bindingProof,identity_kind:"mcp",execution_id:operationId, + actor_id:ownerId,authentication:"mcp",mcp_connection_id:connectionId,mcp_credential_id:credentialId,request_id:null, + authority_issued_at:String(base),authority_expires_at:stamp(15000),source_server_id:sourceServerId, + destination_server_id:destination.destinationServerId??null,tool,state:"completed",receipt_proof:payload.receipt_proof, + attempted_stages:1,completed_stages:1,created_at:stamp(500),finished_at:stamp(1000)} + const raw={ + original:{operation_id:operationId,connection_id:connectionId,owner_id:ownerId,plan_id:randomUUID(), + operation_name:binding.operation,operation_version:1,operation_state:"applied",operation_verification:"inconclusive",operation_error_code:null, + operation_result:{confirmedWrites:0,verificationScope:"acknowledgement",completedServiceSteps:1,diagnosticReceipt:metadata}, + plan_state:"completed",approved_by:ownerId,evidence:{sealed:await encryptSecret(JSON.stringify(evidence))}, + step_index:0,step_state:"acknowledged",intended_state:redact(step),step_result:metadata, + operation_created_at:stamp(100),operation_finished_at:stamp(1500),plan_created_at:stamp(10),plan_approved_at:stamp(50), + plan_expires_at:stamp(16000),step_created_at:stamp(200),step_finished_at:stamp(1400)}, + admission:{receipt_id:receiptId,owner_id:ownerId,identity_kind:"mcp",execution_id:operationId,binding_proof:binding.bindingProof, + authority_expires_at:stamp(15000),claim_token:null,claim_attempted_at:stamp(400),purged_at:null,cancelled_at:null,created_at:stamp(300), + retention_days:retentionDays,safety_ms:60000,gc_not_before:stamp(15000+retentionDays*86400000+60000)}, + anchor,payload,stages:[{receipt_id:receiptId,stage_index:1,kind:twoSides?"native-bandwidth":"native-ping",timeout_ms:1000, + state:"received",outcome:"received",created_at:stamp(600),received_at:stamp(900)}],quarantineCount:0,cancellationCount:0, + } + return {raw,context,receipt,serverIds,stamp} +} + diff --git a/backend/src/services/server-probe-retired-source.test.ts b/backend/src/services/server-probe-retired-source.test.ts new file mode 100644 index 00000000..89e00c3c --- /dev/null +++ b/backend/src/services/server-probe-retired-source.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { findRetiredServerProbeHistory } from "./server-probe-retired-history.js" +import { randomUUID } from "node:crypto" +import { mkdtemp,rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import path from "node:path" +import { test } from "node:test" +import { serverProbeRetiredFixture as fixture } from "./server-probe-retired-source.fixture.js" +import { createServerProbeJournalHistoryReader } from "./server-probe-journal-history.js" +import { openServerProbeRetiredSource,projectServerProbeRetiredSource,ServerProbeRetirementSourceRefused } from "./server-probe-retired-source.js" + +test("production proofs authenticate the minimal projection and keep the original payload readable only with the current fence",async()=>{ + const directory=await mkdtemp(path.join(tmpdir(),"owned-probe-retirement-")) + const previous=process.env.MCP_STATE_DIR + process.env.MCP_STATE_DIR=directory + try{ + for(const twoSides of [false,true]){ + const {raw,context,receipt,serverIds}=await fixture(twoSides) + const exact=structuredClone(raw),signed=await projectServerProbeRetiredSource(raw) + assert.deepEqual(raw,exact) + const opened=await openServerProbeRetiredSource(JSON.parse(JSON.stringify(signed))) + assert.deepEqual(opened,signed.source) + assert.deepEqual(opened.serverIds,serverIds) + assert.equal(opened.authorityExpiresAt,"2025-01-02T00:00:15.000000Z") + assert.equal(opened.terminalAt,"2025-01-02T00:00:16.000000Z") + assert.equal(opened.activityAt,opened.terminalAt) + assert.equal(opened.retentionDays,365) + assert.equal(opened.gcNotBefore,"2026-01-02T00:01:15.000000Z") + const lookup={operationId:opened.executionId,receiptId:opened.receiptId,connectionId:opened.connectionId,ownerId:opened.ownerId,scopeServerIds:serverIds} + const stored={...signed,...raw.anchor,plan_id:raw.original.plan_id,tombstone_server_ids:serverIds.map(String), + retention_days:raw.admission.retention_days,safety_ms:raw.admission.safety_ms, + gc_not_before:raw.admission.gc_not_before.replace("Z","000Z"),authority_expires_at:raw.anchor.authority_expires_at.replace("Z","000Z"), + payload_purged:false,payload_proof:raw.payload.receipt_proof,payload_sha256:opened.ciphertextSha256} + const read=(row:Record)=>findRetiredServerProbeHistory(async (_sql,values)=>{ + assert.deepEqual(values,[lookup.receiptId,lookup.operationId,lookup.connectionId,lookup.ownerId,lookup.scopeServerIds]) + return [row] + },lookup) + assert.deepEqual(await read(stored),{source:opened,payloadPurged:false}) + assert.deepEqual(await read({...stored,payload_purged:true,payload_proof:null,payload_sha256:null}),{source:opened,payloadPurged:true}) + for(const alteration of [{tombstone_server_ids:["800"]},{plan_id:randomUUID()},{gc_not_before:raw.admission.authority_expires_at}, + {mcp_credential_id:randomUUID()},{payload_sha256:"d".repeat(64)},{payload_purged:true}]) + assert.equal(await read({...stored,...alteration}),undefined) + assert.equal(await read({...stored,proof:"d".repeat(64)}),undefined) + const envelope=JSON.stringify(signed) + for(const secret of [raw.payload.receipt_ciphertext,raw.original.evidence.sealed,receipt.output!,"inert-private-inner-binding"]) + assert(!envelope.includes(secret)) + let permitted=true + const reader=createServerProbeJournalHistoryReader({ + assertReadAuthority:async()=>{if(!permitted)throw Error("current scope or protection denied")}, + loadArchive:async()=>[{...raw.anchor,receipt_ciphertext:raw.payload.receipt_ciphertext,quarantine_count:0}], + }) + const binding={context,receiptId:opened.receiptId,bindingProof:opened.bindingProof} + assert.deepEqual(await reader.read(binding),receipt) + permitted=false + await assert.rejects(reader.read(binding),/current scope or protection denied/) + for(const alteration of [{retentionDays:364},{activityAt:"2025-01-02T00:00:17.000000Z"},{ownerId:randomUUID()}]) + await assert.rejects(openServerProbeRetiredSource({...signed,source:{...signed.source,...alteration}}),ServerProbeRetirementSourceRefused) + } + const {raw}=await fixture() + for(const alter of [ + (copy:typeof raw)=>{copy.original.evidence.sealed="broken"}, + (copy:typeof raw)=>{copy.payload.receipt_proof="b".repeat(64)}, + (copy:typeof raw)=>{copy.original.step_result.sha256="b".repeat(64)}, + (copy:typeof raw)=>{copy.stages[0].received_at="2025-01-02T00:00:00.000001Z"}, + (copy:typeof raw)=>{copy.admission.authority_expires_at="2025-01-02T00:00:15.000001Z"}, + (copy:typeof raw)=>{copy.quarantineCount=1}, + (copy:typeof raw)=>{copy.cancellationCount=1}, + ]){ + const copy=structuredClone(raw);alter(copy) + await assert.rejects(projectServerProbeRetiredSource(copy),ServerProbeRetirementSourceRefused) + } + }finally{ + if(previous===undefined)delete process.env.MCP_STATE_DIR + else process.env.MCP_STATE_DIR=previous + await rm(directory,{recursive:true,force:true}) + } +}) diff --git a/backend/src/services/server-probe-retired-source.ts b/backend/src/services/server-probe-retired-source.ts new file mode 100644 index 00000000..e2213e8f --- /dev/null +++ b/backend/src/services/server-probe-retired-source.ts @@ -0,0 +1,147 @@ +/** Private codec only. A future caller must own a bounded, locked SQL + * snapshot, and readers must still prove current scope and protections. */ +import { createHash } from "node:crypto" +import { z } from "zod" +import { authenticateServerProbeExecutionSourceRow } from "../modules/mcp/diagnostic-cancellation-source.js" +import { serverProbeReceiptMetadata } from "../modules/mcp/server-probe-actions.js" +import { serviceDiagnosticReceipt } from "../modules/mcp/service-diagnostic-progress.js" +import { constantEqual, decryptSecretExisting, fingerprint, privateFingerprinterExisting } from "../modules/mcp/security.js" +import { createLegacyServerProbeVerifier } from "./server-probe-legacy-verifier.js" +import { validateServerProbeJournalReceipt } from "./server-probe-journal.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" +import { canonicalPollArchiveTimestamp } from "./server-poll-terminal-archive.js" + +const purpose="server-probe-retired-source-v1" +const uuid=z.string().uuid(),hash=z.string().regex(/^[0-9a-f]{64}$/),sid=z.number().int().positive().safe() +const time=z.string().transform(canonicalPollArchiveTimestamp) +const tool=z.enum(["ping","traceroute","bandwidth","dns","route","mtu"]) +const record=z.record(z.string(),z.unknown()) +const originalSchema=z.object({ + operation_id:uuid,connection_id:uuid,owner_id:z.string().min(1).max(200),plan_id:uuid, + operation_name:z.literal("servers.probes.run"),operation_version:z.literal(1),operation_state:z.literal("applied"), + operation_verification:z.literal("inconclusive"),operation_error_code:z.null(),operation_result:z.unknown(), + plan_state:z.literal("completed"),approved_by:z.string().min(1).max(200),evidence:record, + step_index:z.literal(0),step_state:z.literal("acknowledged"),intended_state:record,step_result:record, + operation_created_at:time,operation_finished_at:time,plan_created_at:time,plan_approved_at:time, + plan_expires_at:time,step_created_at:time,step_finished_at:time, +}).strict() +const admissionSchema=z.object({ + receipt_id:uuid,owner_id:z.string().min(1).max(200),identity_kind:z.literal("mcp"),execution_id:uuid,binding_proof:hash, + authority_expires_at:time,claim_token:z.null(),claim_attempted_at:time,purged_at:z.null(),cancelled_at:z.null(),created_at:time, + retention_days:z.number().int().min(1).max(365),safety_ms:z.number().int().min(60000).max(86400000),gc_not_before:time, +}).strict() +const sourceSchema=z.object({ + original:originalSchema,admission:admissionSchema,anchor:record, + payload:z.object({receipt_id:uuid,receipt_proof:hash,receipt_ciphertext:z.string().min(1).max(262144)}).strict(), + stages:z.array(record).min(1).max(11),quarantineCount:z.literal(0),cancellationCount:z.literal(0), +}).strict() +const signedSchema=z.object({ + version:z.literal(1),receiptId:uuid,executionId:uuid,connectionId:uuid,ownerId:z.string().min(1).max(200),planId:uuid, + operation:z.literal("servers.probes.run"),operationVersion:z.literal(1),originalCredentialId:uuid, + sourceServerId:sid,destinationServerId:sid.optional(),tool,serverIds:z.array(sid).min(1).max(2), + issuedAt:z.number().int().nonnegative().safe(),authorityExpiresAt:time,terminalAt:time,activityAt:time,gcNotBefore:time, + retentionDays:z.number().int().min(1).max(365),safetyMs:z.number().int().min(60000).max(86400000), + bindingProof:hash,sourceDigest:hash,terminalDigest:hash,stageSnapshotDigest:hash,receiptProof:hash,ciphertextSha256:hash, +}).strict().refine(value=>{ + const expected=[...new Set([value.sourceServerId,...(value.destinationServerId===undefined?[]:[value.destinationServerId])])].sort((a,b)=>a-b) + return fingerprint(expected)===fingerprint(value.serverIds) + && (value.tool==="bandwidth" ? value.destinationServerId!==undefined&&value.destinationServerId!==value.sourceServerId : value.destinationServerId===undefined) + && value.issuedAt=micros(value.authorityExpiresAt)+BigInt(value.retentionDays*86400000+value.safetyMs)*1000n +}) + +export type ServerProbeRetiredSource = z.output +/** Match the exact canonical live terminal projection after payload expiry. */ +export function matchesServerProbeRetiredTerminal(source: ServerProbeRetiredSource, raw: unknown): boolean { + const parsed = originalSchema.safeParse(raw) + return parsed.success && parsed.data.operation_id === source.executionId && parsed.data.plan_id === source.planId + && parsed.data.connection_id === source.connectionId && parsed.data.owner_id === source.ownerId + && fingerprint(parsed.data) === source.terminalDigest +} +export interface SignedServerProbeRetiredSource { source: ServerProbeRetiredSource; proof: string } +export class ServerProbeRetirementSourceRefused extends Error { + readonly automaticReplay=false + constructor(){super("Diagnostic retirement source cannot be authenticated");this.name="ServerProbeRetirementSourceRefused"} +} +function requireThat(value:unknown):asserts value {if(!value)throw new ServerProbeRetirementSourceRefused()} +function micros(value:string){return BigInt(Date.parse(value))*1000n+BigInt(value.slice(23,26))} +const maximum=(...values:string[])=>values.sort().at(-1)! + +/** Produces no operation/payload/cancellation mutation or public authorization. + * Only settled successful sources with the original retained payload qualify. */ +export async function projectServerProbeRetiredSource(raw:unknown):Promise{ + try{ + requireThat(Buffer.byteLength(JSON.stringify(raw))<=3*1048576) + const source=sourceSchema.parse(structuredClone(raw)),{original,admission,anchor,payload,stages}=source + const authenticated=await authenticateServerProbeExecutionSourceRow(original,original.operation_id, + {connectionId:original.connection_id,ownerId:original.owner_id}) + requireThat(authenticated.terminal&&authenticated.sourceState==="applied") + const context=authenticated.originalContext,id=context.identity + requireThat(admission.receipt_id===authenticated.scope.receiptId&&admission.execution_id===id.executionId&& + admission.owner_id===id.ownerId&&admission.binding_proof===authenticated.scope.bindingProof&& + micros(admission.authority_expires_at)===BigInt(id.expiresAt)*1000n&&time.parse(anchor.authority_expires_at)===admission.authority_expires_at&& + payload.receipt_id===admission.receipt_id&& + payload.receipt_proof===anchor.receipt_proof&&anchor.state==="completed") + const verify=createLegacyServerProbeVerifier({loadMcpSource:async operationId=>{ + requireThat(operationId===id.executionId) + return authenticated + }}) + const verified=await verify({identityKind:"mcp",executionId:id.executionId, + anchor:{...anchor,receipt_ciphertext:payload.receipt_ciphertext},stages}) + requireThat(verified.status==="verified"&&verified.authentication==="receipt"&&verified.bindingAuthenticated&& + verified.progressAuthenticated&&verified.terminal&&!verified.quarantined&&!verified.cancelled) + // The same ciphertext has already passed the existing receipt MAC and + // original-context checks above. Match its actual public effect as well. + const envelope=JSON.parse(await decryptSecretExisting(payload.receipt_ciphertext)) as {receipt:ServerProbeReceipt} + const receipt=validateServerProbeJournalReceipt(context,admission.receipt_id,envelope.receipt) + requireThat(receipt.state==="completed") + const metadata=serverProbeReceiptMetadata(receipt,id.executionId,authenticated.stageBudget) + const confirmed=serviceDiagnosticReceipt(metadata,true,id.executionId,authenticated.stageBudget) + requireThat(confirmed&&fingerprint(original.step_result)===fingerprint(metadata)&&fingerprint(original.operation_result)===fingerprint({ + confirmedWrites:0,verificationScope:"acknowledgement",completedServiceSteps:1,diagnosticReceipt:confirmed, + })) + // The original receipt authenticates context and progress, not SQL row + // timestamps. Sign the exact observed locked snapshot and its chronology, + // without upgrading the historical journal's stage or timestamp proofs. + const anchorCreated=time.parse(anchor.created_at),anchorFinished=time.parse(anchor.finished_at) + requireThat(original.plan_created_at<=original.plan_approved_at&&original.plan_approved_at<=original.operation_created_at&& + original.operation_created_at{ + try{ + requireThat(Buffer.byteLength(JSON.stringify(raw))<=8192) + const {source,proof}=z.object({source:signedSchema,proof:hash}).strict().parse(structuredClone(raw)) + const sign=await privateFingerprinterExisting(purpose) + requireThat(constantEqual(proof,sign(fingerprint(source)))) + return source + }catch{throw new ServerProbeRetirementSourceRefused()} +} diff --git a/backend/src/services/server-probe-runner.sql-helper.ts b/backend/src/services/server-probe-runner.sql-helper.ts new file mode 100644 index 00000000..df681ea8 --- /dev/null +++ b/backend/src/services/server-probe-runner.sql-helper.ts @@ -0,0 +1,177 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +import type { ServerProbeEndpoint } from "./server-probe-execution.js" +import type { McpPrincipal, StoredOperation } from "../modules/mcp/store.js" + +/** Historical pre-registration fixture; active acceptance is now + * modules/mcp/server-probe-all-tools.sql-helper.ts on the public v2 registry. + * Not invoked by current profiles. Originally used after owned migrations. + * Uses the actual public tool dispatcher, approval/admission, leased async + * runner, cancellation watcher, encrypted plans, SQL journal and history. + * Only provider I/O/endpoints are synthetic. Registers nothing on import. + * No cleanup/retention, production routes, external DNS or RouterOS access. + */ +export async function acceptServerProbeSqlRunner() { + assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { add, listActions } = await import("../modules/mcp/actions.js") + const { registerMcpService } = await import("../modules/mcp/service-registry.js") + const { createServerProbeMcpAdapter } = await import("../modules/mcp/server-probe-actions.js") + const { createServerProbeExecutor } = await import("./server-probe-execution.js") + const { SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION } = await import("./server-probe-authority.js") + const { invokeMcpTool, approvePlan, applyPlan } = await import("../modules/mcp/runtime.js") + const { requestDiagnosticCancellation } = await import("../modules/mcp/diagnostic-cancellation.js") + const security = await import("../modules/mcp/security.js") + const { mcpConnectionPolicySchema } = await import("@mmapp/contracts/mcp") + assert(!listActions().some(action => action.operation.id === SERVER_PROBE_OPERATION), "Production diagnostic must remain unregistered before this test-only setup") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + const previousMcpEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean + const ids = (await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('runner-synthetic-source','127.0.0.1',9,true),('runner-synthetic-destination','127.0.0.1',9,true) RETURNING id")).rows.map(row => Number(row.id)) + const endpoints = ids.map((id): ServerProbeEndpoint => ({ id, enabled: true, host: "127.0.0.1", port: 9, useSsl: false, verifySsl: true, username: "SYNTHETIC_RUNNER_USERNAME", password: "SYNTHETIC_RUNNER_PASSWORD", routingEndpointEpoch: randomUUID() })) + try { + await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") + let dispatches = 0, mode: "normal" | "before" | "during" = "normal" + let enterCurrent: (() => void) | undefined, releaseCurrent: (() => void) | undefined, enteredProvider: (() => void) | undefined + const provider = async (signal: AbortSignal, fence: () => Promise) => { + await fence(); signal.throwIfAborted(); dispatches++ + if (mode === "during") { + enteredProvider?.() + await new Promise((_resolve, reject) => { + const abort = () => reject(signal.reason) + if (signal.aborted) abort(); else signal.addEventListener("abort", abort, { once: true }) + }) + } + } + const adapter = createServerProbeMcpAdapter({ + executor: input => createServerProbeExecutor({ ...input, + getServer: async id => structuredClone(endpoints.find(endpoint => endpoint.id === id)), + findDestination: async (_remote, explicit, scope) => scope.includes(explicit ?? ids[1]) ? { id: explicit ?? ids[1] } : undefined, + seal: security.encryptSecret, open: security.decryptSecret, + privateProof: (domain, value) => security.privateSecretFingerprint(security.fingerprint(value), "synthetic-runner:" + domain), + post: async (_server, path, _body, _timeout, signal, fence) => { + await provider(signal, fence) + if (path === "/tool/bandwidth-test") return [{ "tx-current": "1000", "rx-current": "2000" }] + if (path === "/tool/traceroute") return [{ address: "192.0.2.1", loss: "0%", last: "1ms", avg: "1ms" }] + return [{ seq: "0", ttl: "64", time: "1ms", received: "1" }] + }, + routes: async (_server, _timeout, signal, fence) => { await provider(signal, fence); return [{ active: "true", "dst-address": "0.0.0.0/0", gateway: "192.0.2.254", distance: "1" }] }, + dns: async (_name, type, signal, fence) => { await provider(signal, fence); return type === "TXT" ? [["PUBLIC_SYNTHETIC_MEASUREMENT_".repeat(180)], ["PUBLIC_SECOND_RECORD_".repeat(180)]] : ["192.0.2.1"] }, + }), + }) + const runTool = "mm_test_owned_probe_plan", readTool = "mm_test_owned_probe_receipt_get" + add(SERVER_PROBE_OPERATION, runTool, "Изолированная диагностика", "network", "mm:servers:write", "diagnostic", "servers", adapter.schema, { target: adapter.target, prepare: adapter.prepare }) + add(SERVER_PROBE_RECEIPT_READ_OPERATION, readTool, "Изолированная квитанция", "servers", "mm:servers:read", "read", "servers", adapter.receiptSchema, { target: adapter.receiptTarget, read: adapter.readPage }) + registerMcpService(SERVER_PROBE_OPERATION, { ...adapter.service, current: async input => { + const result = await adapter.service.current(input) + if (mode === "before") { enterCurrent?.(); await new Promise(resolve => { releaseCurrent = resolve }) } + return result + } }) + let cases = 0 + const fixture = async () => { + const connectionId = randomUUID(), credentialId = randomUUID(), ownerId = `runner-owner-${randomUUID()}`, now = Date.now(), expires = now + 3600000 + const permissions = ["mm:servers:write", "mm:servers:read", "mm:mcp:approve"] + const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt: new Date(expires).toISOString(), scope: { serverIds: ids, operationIds: [SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION], allowGlobal: false }, limits: { callsPerMinute: 100, mutationsPerHour: 100, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } }) + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'synthetic runner',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), await security.accessGeneration(), new Date(expires), new Date(now - 1000)]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), new Date(expires)]) + const principal: McpPrincipal = { connectionId, credentialId, credentialExpiresAt: new Date(expires).toISOString() } + return { principal, actor: { id: ownerId, email: "synthetic@example.invalid", name: "Synthetic", apps: ["mm"], permissions, issuedAt: now - 1000, expiresAt: expires } } + } + const terminal = async (id: string): Promise => { + const until = Date.now() + 20000 + while (Date.now() < until) { + const row = (await pool.query("SELECT * FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (row && !["pending", "running"].includes(row.state)) return row + await new Promise(resolve => setTimeout(resolve, 20)) + } + assert.fail("Actual asynchronous leased runner did not reach terminal state") + } + const prepare = async (f: Awaited>, tool: "ping" | "traceroute" | "bandwidth" | "dns" | "route" | "mtu") => { + const request = tool === "bandwidth" ? { tool, bwRemoteAddress: endpoints[1].host, dstServerId: ids[1] } : tool === "dns" ? { tool, target: "synthetic.invalid", dnsType: "TXT" } : { tool, target: "192.0.2.1" } + const before = dispatches + const result = await invokeMcpTool(f.principal, runTool, { serverId: ids[0], request }) as { planId: string } + assert.equal(dispatches, before, "Prepare cannot dispatch") + const plan = (await pool.query("SELECT * FROM mcp_plans WHERE id=$1", [result.planId])).rows[0] + assert.equal(typeof plan.payload.sealed, "string"); assert.equal(typeof plan.evidence.sealed, "string") + assert(!JSON.stringify(plan).includes("SYNTHETIC_RUNNER_PASSWORD")) + await approvePlan(result.planId, f.actor, true) + return result.planId + } + for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"] as const) { + const f = await fixture(), planId = await prepare(f, tool), operation = await applyPlan(f.principal, planId, randomUUID()), done = await terminal(operation.id) + assert.equal(done.state, "applied", `${tool}: ${JSON.stringify(done)}`) + const result = done.result as { confirmedWrites: number; completedServiceSteps: number; diagnosticReceipt: { receiptId: string; attemptedStages: number; completedStages: number; stageBudget: number; sha256: string; totalPages: number } } + assert.equal(result.confirmedWrites, 0); assert.equal(result.completedServiceSteps, 1) + const receipt = result.diagnosticReceipt + assert.equal(receipt.attemptedStages, receipt.completedStages); assert(receipt.attemptedStages > 0 && receipt.attemptedStages <= receipt.stageBudget) + const steps = (await pool.query("SELECT * FROM mcp_operation_steps WHERE operation_id=$1", [done.id])).rows + assert.equal(steps.length, 1); assert.equal(steps[0].state, "acknowledged"); assert.equal(steps[0].result.receiptId, receipt.receiptId) + const anchor = (await pool.query("SELECT * FROM server_probe_executions WHERE execution_id=$1", [done.id])).rows[0] + assert.equal(anchor.receipt_id, receipt.receiptId); assert.equal(anchor.mcp_credential_id, f.principal.credentialId) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_stages WHERE receipt_id=$1", [receipt.receiptId])).rows[0].n, receipt.attemptedStages) + const beforeRead = dispatches, chunks: Buffer[] = [] + for (let page = 0; page < receipt.totalPages; page++) { + const value = await invokeMcpTool(f.principal, readTool, { operationId: done.id, receiptId: receipt.receiptId, page }) as { contentB64: string; sha256: string } + assert.equal(value.sha256, receipt.sha256); chunks.push(Buffer.from(value.contentB64, "base64")) + } + const complete = Buffer.concat(chunks) + assert.equal(createHash("sha256").update(complete).digest("hex"), receipt.sha256); assert.equal(JSON.parse(complete.toString()).tool, tool) + if (tool === "dns") { + assert(receipt.totalPages > 1, "Real historical pagination required") + const rotatedId = randomUUID() + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [rotatedId, f.principal.connectionId, randomUUID(), new Date(f.principal.credentialExpiresAt)]) + await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [f.principal.credentialId]) + const rotated = { ...f.principal, credentialId: rotatedId } + // Current access is a real read-only observer grant; historical reads + // cannot rely on the original execution's write policy or credential. + await pool.query("UPDATE mcp_connections SET policy=policy || $2::jsonb WHERE id=$1", [rotated.connectionId, JSON.stringify({ mode: "observer", permissions: ["mm:servers:read"], scope: { serverIds: ids, operationIds: [SERVER_PROBE_RECEIPT_READ_OPERATION], allowGlobal: false } })]) + for (let page = 0; page < receipt.totalPages; page++) await invokeMcpTool(rotated, readTool, { operationId: done.id, receiptId: receipt.receiptId, page }) + await assert.rejects(invokeMcpTool(f.principal, readTool, { operationId: done.id, receiptId: receipt.receiptId, page: 0 })) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$1", [rotated.connectionId]) + await assert.rejects(invokeMcpTool(rotated, readTool, { operationId: done.id, receiptId: receipt.receiptId, page: 1 })) + } + assert.equal(dispatches, beforeRead, "Historical pages and credential rotation cannot redispatch") + const audit = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1 AND action='operation.complete'", [f.principal.connectionId])).rows + assert.equal(audit.length, 1); assert.equal(audit[0].payload.confirmedWrites, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action LIKE 'configuration.%'", [f.principal.connectionId])).rows[0].n, 0) + cases++ + } + for (const cancellation of ["before", "during"] as const) { + const f = await fixture(), planId = await prepare(f, "ping"), before = dispatches + const entered = new Promise(resolve => { if (cancellation === "before") enterCurrent = resolve; else enteredProvider = resolve }) + mode = cancellation + try { + const operation = await applyPlan(f.principal, planId, randomUUID()) + await Promise.race([entered, new Promise((_resolve, reject) => { const timer = setTimeout(() => reject(Error("Runner did not reach cancellation barrier")), 10000); timer.unref() })]) + const cancelled = await requestDiagnosticCancellation(f.principal, operation.id) + assert.equal(cancelled.cancelRequested, true) + releaseCurrent?.() + const done = await terminal(operation.id), result = done.result as { confirmedWrites: number; completedServiceSteps: number; diagnosticProgress?: { attemptedStages: number; completedStages: number; state: string } } + assert.equal(done.state, cancellation === "before" ? "rejected" : "unknown"); assert.equal(result.confirmedWrites, 0); assert.equal(result.completedServiceSteps, 0) + assert.equal(dispatches - before, cancellation === "before" ? 0 : 1) + if (cancellation === "during") { + assert.equal(result.diagnosticProgress?.attemptedStages, 1); assert.equal(result.diagnosticProgress.completedStages, 0); assert.equal(result.diagnosticProgress.state, "unknown") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE execution_id=$1", [done.id])).rows[0].n, 1) + } else { + assert.equal(result.diagnosticProgress?.state, "cancelled") + assert.equal(result.diagnosticProgress.attemptedStages, 0); assert.equal(result.diagnosticProgress.completedStages, 0) + assert.equal((done.result as { needsReview: boolean }).needsReview, false) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE execution_id=$1", [done.id])).rows[0].n, 0) + } + const duplicate = await applyPlan(f.principal, planId, operation.idempotency_key as string) + assert.equal(duplicate.id, done.id); assert.equal(dispatches - before, cancellation === "before" ? 0 : 1) + cases++ + } finally { mode = "normal"; releaseCurrent?.(); releaseCurrent = undefined; enterCurrent = undefined; enteredProvider = undefined } + } + return { cases, providerDispatches: dispatches, realRunner: true, productionRegistration: false, externalNetwork: false } + } finally { + // The later restore profile can restart scheduling: leave only our own + // fixture endpoints disabled, even after a failed acceptance assertion. + await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [ids]) + await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousMcpEnabled]) + } +} diff --git a/backend/src/services/server-probe-storage-connection.sql-helper.ts b/backend/src/services/server-probe-storage-connection.sql-helper.ts new file mode 100644 index 00000000..535adb5c --- /dev/null +++ b/backend/src/services/server-probe-storage-connection.sql-helper.ts @@ -0,0 +1,81 @@ +/** Real wire-level checks against the runner's disposable loopback PostgreSQL. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { Pool } from "pg" +import { createProbeStorageConnectionConnector } from "./server-probe-storage-connection.js" +import { ProbeStorageRefused, ProbeStorageUnknown } from "./server-probe-journal-maintenance.js" + +export async function acceptProbeStorageConnectionSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + assert.equal(new URL(process.env.DATABASE_URL!).hostname, "127.0.0.1") + const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 1, connectionTimeoutMillis: 3000 }) + const releases: boolean[] = [], budgets: number[] = [], cases: string[] = [] + let loseCommit = false + const connect = createProbeStorageConnectionConnector(async () => { + const client = await pool.connect() + return { + release(unsafe?: boolean) { releases.push(!!unsafe); client.release(unsafe) }, + on: client.on.bind(client), removeListener: client.removeListener.bind(client), + async query(config) { + budgets.push(config.query_timeout) + const result = await client.query(config) + if (config.text === "COMMIT" && loseCommit) { loseCommit = false; throw Error("Owned actual COMMIT ACK loss") } + return result + }, + } + }) + const until = async (predicate: () => boolean | Promise) => { + const deadline = Date.now() + 3000 + while (!await predicate()) { assert(Date.now() < deadline, "Owned PG fixture cleanup timed out"); await new Promise(resolve => setTimeout(resolve, 10)) } + } + try { + for (const kind of ["abort", "deadline"]) { + const held = await pool.connect(), controller = new AbortController(), before = releases.length + const pending = connect({ deadlineAt: Date.now() + (kind === "deadline" ? 50 : 3000), signal: controller.signal }) + const rejected = assert.rejects(pending, ProbeStorageRefused) + try { + await until(() => pool.waitingCount === 1) + if (kind === "abort") controller.abort() + await rejected + } finally { held.release() } + await until(() => releases.length === before + 1) + assert.equal(releases.at(-1), true) + await until(() => pool.totalCount === 0) + cases.push("actual saturated max=1 checkout " + kind + " refuses and late backend is destroyed once") + } + for (const kind of ["abort", "query-timeout"]) { + const controller = new AbortController(), before = releases.length, name = "owned-connector-rollback-" + randomUUID() + const lease = await connect({ deadlineAt: Date.now() + 10000 }) + const pid = Number((await lease.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + await lease.query("BEGIN"); await lease.query("SET LOCAL statement_timeout='200ms'") + await lease.query("INSERT INTO servers(name,host,port,enabled) VALUES($1,'192.0.2.250',9,false)", [name]) + const pending = lease.query("SELECT pg_sleep(10)", [], { signal: controller.signal, timeoutMs: kind === "query-timeout" ? 30 : 1000 }) + const rejected = assert.rejects(pending, ProbeStorageUnknown) + const abort = kind === "abort" ? setTimeout(() => controller.abort(), 20) : undefined + try { await rejected } finally { if (abort) clearTimeout(abort); lease.release() } + assert.equal(releases.length, before + 1); assert.equal(releases.at(-1), true) + await until(async () => Number((await pool.query("SELECT count(*) AS n FROM pg_stat_activity WHERE pid=$1", [pid])).rows[0].n) === 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers WHERE name=$1", [name])).rows[0].n), 0) + await assert.rejects(lease.query("SELECT 1"), ProbeStorageRefused) + cases.push("actual in-flight " + kind + " destroys backend once; server timeout bounds cleanup, uncommitted insert rolls back") + } + const name = "owned-connector-commit-" + randomUUID(), before = releases.length + const committed = await connect({ deadlineAt: Date.now() + 10000 }) + await committed.query("BEGIN") + await committed.query("INSERT INTO servers(name,host,port,enabled) VALUES($1,'192.0.2.251',9,false)", [name]) + loseCommit = true; await assert.rejects(committed.query("COMMIT"), ProbeStorageUnknown); committed.release() + assert.equal(releases.length, before + 1); assert.equal(releases.at(-1), true) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers WHERE name=$1", [name])).rows[0].n), 1) + await pool.query("DELETE FROM servers WHERE name=$1", [name]) + cases.push("actual COMMIT then lost ACK destroys connection and remains unknown while independent read proves persisted insert") + const original = new AbortController(), aborted = await connect({ deadlineAt: Date.now() + 10000, signal: original.signal }) + original.abort(); aborted.release() + const cleanup = await connect({ deadlineAt: Date.now() + 10000, signal: AbortSignal.timeout(10000) }) + assert.equal(Number((await cleanup.query("SELECT 1 AS value")).rows[0].value), 1); cleanup.release(); cleanup.release() + assert.equal(releases.at(-1), false) + assert(budgets.every(value => value >= 1 && value <= 10000)) + cases.push("fresh cleanup options use a healthy independent lease after original abort; healthy release is once") + return { cases, actualSql: true, dedicatedPoolMax: 1, driverWallTimersAndSignals: true, serverStatementTimeoutBackup: true, + committedUnknownNotRetried: true, nativeOrDnsCalls: 0, productionDdlApplied: false } + } finally { await pool.end() } +} diff --git a/backend/src/services/server-probe-storage-connection.test.ts b/backend/src/services/server-probe-storage-connection.test.ts new file mode 100644 index 00000000..5939a96a --- /dev/null +++ b/backend/src/services/server-probe-storage-connection.test.ts @@ -0,0 +1,384 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { capturePollError, carryPollError, pollErrorLog } from "./server-poll-error-provenance.js" +import { EventEmitter } from "node:events" +import { DatabaseError, type QueryConfig } from "pg" +import { createProbeStorageConnectionConnector, connectProbeStorage, type ProbeStoragePgClient } from "./server-probe-storage-connection.js" +import { ProbeStorageRefused, ProbeStorageUnknown } from "./server-probe-journal-maintenance.js" +import { principalSourceSql, principalSourceStatementName } from "../modules/mcp/principal-security-sql.js" + +type Result = Awaited> +const result = (command = "SELECT"): Result => ({ rows: [], command }) +function deferred() { let resolve!: (value: T) => void, reject!: (error: unknown) => void; const promise = new Promise((yes, no) => { resolve = yes; reject = no }); return { promise, resolve, reject } } +const tick = () => new Promise(resolve => setImmediate(resolve)) +function fixture(handler: (config: QueryConfig & { query_timeout: number }) => Promise = async config => result(config.text.trim() === "COMMIT" ? "COMMIT" : "SELECT")) { + const queries: Array = [], releases: boolean[] = [], listeners = new Set<(error: Error) => void>() + let active = 0, maximumActive = 0, checkouts = 0 + const client: ProbeStoragePgClient = { + async query(config) { queries.push(config); active++; maximumActive = Math.max(maximumActive, active); try { return await handler(config) } finally { active-- } }, + release(unsafe = false) { releases.push(unsafe) }, + on(_event, listener) { listeners.add(listener) }, removeListener(_event, listener) { listeners.delete(listener) }, + } + const connect = createProbeStorageConnectionConnector(async () => { checkouts++; return client }) + return { client, connect, queries, releases, checkouts: () => checkouts, maximumActive: () => maximumActive, emitError: () => { for (const listener of listeners) listener(Error("NEVER_PRIVATE_DRIVER_DIAGNOSTIC")) } } +} +const refused = (error: unknown) => error instanceof ProbeStorageRefused && !error.message.includes("NEVER_") +const unknown = (error: unknown) => error instanceof ProbeStorageUnknown && !error.message.includes("NEVER_") + +test("only exact fixed source is named; fresh arguments/timeouts survive and near matches stay unnamed", async () => { + const f = fixture(), lease = await f.connect({ deadlineAt: Date.now() + 30000 }) + try { + for (const values of [["credential-a", "connection-a", true], ["credential-b", "connection-b", false]]) { + await lease.query(principalSourceSql, values, { timeoutMs: 543 }) + const sent = f.queries.at(-1)! + assert.equal(sent.name, principalSourceStatementName); assert.equal(sent.text, principalSourceSql) + assert.equal(sent.values, values); assert(sent.query_timeout > 0 && sent.query_timeout <= 543) + } + assert(principalSourceStatementName.length <= 63) + for (const text of [principalSourceSql + " ", "/* mcp-principal:coherent-source-v1 */ SELECT 1", "SELECT 1"]) { + await lease.query(text); assert.equal(Object.hasOwn(f.queries.at(-1)!, "name"), false) + } + } finally { lease.release() } +}) + +test("fixed-source schema failure destroys once without unnamed fallback or retry", async () => { + const f = fixture(async () => { throw Object.assign(Error("NEVER_PRIVATE_DRIVER_DIAGNOSTIC"), { code: "0A000" }) }) + const lease = await f.connect({ deadlineAt: Date.now() + 30000 }) + await assert.rejects(lease.query(principalSourceSql, ["a", "b", true]), unknown) + assert.equal(f.queries.length, 1); assert.deepEqual(f.releases, [true]); assert.equal(lease.lifetimeSignal?.aborted, true) + await assert.rejects(lease.query(principalSourceSql, ["a", "b", true]), refused) + assert.equal(f.queries.length, 1); lease.release(); assert.deepEqual(f.releases, [true]) +}) + +test("connector import/construction is inert; invalid/pre-aborted checkout performs no work", async () => { + const f = fixture(), aborted = new AbortController(); aborted.abort(Error("NEVER_PRIVATE_ABORT_REASON")) + assert.equal(typeof connectProbeStorage, "function"); assert.equal(f.checkouts(), 0) + await assert.rejects(f.connect({ deadlineAt: Date.now() + 1000, signal: aborted.signal }), refused) + for (const deadlineAt of [0, NaN, Infinity, Date.now() - 1, Date.now() + 0.5]) await assert.rejects(f.connect({ deadlineAt }), refused) + assert.equal(f.checkouts(), 0); assert.equal(f.queries.length, 0); assert.deepEqual(f.releases, []) +}) + +for (const mode of ["abort", "deadline"] as const) test("bounded checkout " + mode + " destroys an eventual client once", async () => { + const f = fixture(), pendingClient = deferred(), controller = new AbortController() + let checkouts = 0 + const connect = createProbeStorageConnectionConnector(() => { checkouts++; return pendingClient.promise }) + const started = Date.now(), waiting = connect({ deadlineAt: Date.now() + (mode === "deadline" ? 25 : 1000), signal: controller.signal }) + if (mode === "abort") controller.abort(Error("NEVER_PRIVATE_ABORT_REASON")) + await assert.rejects(waiting, refused) + assert.equal(checkouts, 1); assert.equal(Date.now() - started < 1000, true); assert.deepEqual(f.releases, []) + pendingClient.resolve(f.client); await tick() + assert.deepEqual(f.releases, [true]); assert.equal(f.queries.length, 0) +}) + +test("late checkout failure is consumed after cancellation and never retried", async () => { + const pendingClient = deferred(), controller = new AbortController() + let checkouts = 0, unhandled = 0 + const listener = () => { unhandled++ } + process.on("unhandledRejection", listener) + try { + const connect = createProbeStorageConnectionConnector(() => { checkouts++; return pendingClient.promise }) + const waiting = connect({ deadlineAt: Date.now() + 1000, signal: controller.signal }) + controller.abort(); await assert.rejects(waiting, refused) + pendingClient.reject(Error("NEVER_PRIVATE_DRIVER_DIAGNOSTIC")); await tick(); await tick() + assert.equal(checkouts, 1); assert.equal(unhandled, 0) + } finally { process.removeListener("unhandledRejection", listener) } +}) + +test("checkout driver rejection and synchronous throw are sanitized", async () => { + for (const checkout of [async () => { throw Error("NEVER_PRIVATE_DRIVER_DIAGNOSTIC") }, () => { throw Error("NEVER_PRIVATE_DRIVER_DIAGNOSTIC") }]) { + await assert.rejects(createProbeStorageConnectionConnector(checkout)({ deadlineAt: Date.now() + 1000 }), refused) + } +}) + +test("healthy lease carries values and pg query_timeout; release is once and closed is unusable", async () => { + const f = fixture(), lease = await f.connect({ deadlineAt: Date.now() + 30000 }) + await lease.query("SELECT $1", [7], { timeoutMs: 321 }) + assert.equal(f.queries.length, 1); assert.deepEqual(f.queries[0].values, [7]) + assert.equal(f.queries[0].query_timeout > 0 && f.queries[0].query_timeout <= 321, true) + await lease.query("SELECT 1") + assert.equal(f.queries[1].query_timeout > 0 && f.queries[1].query_timeout <= 10000, true) + lease.release(); lease.release(true) + assert.deepEqual(f.releases, [false]) + await assert.rejects(lease.query("SELECT 2"), refused); assert.equal(f.queries.length, 2) +}) + +test("actual connector publishes one immutable native lifetime signal; every close revokes BEFORE exact-once physical release",async()=>{ + for(const mode of ["healthy","destroy","error","abort","deadline"]){ + const f=fixture(),input=new AbortController(),lease=await f.connect({deadlineAt:Date.now()+(mode==="deadline"?30:1000),signal:input.signal}) + const signal=lease.lifetimeSignal;assert(signal instanceof AbortSignal);assert.equal(signal.aborted,false) + const descriptor=Object.getOwnPropertyDescriptor(lease,"lifetimeSignal")! + assert.equal(descriptor.value,signal);assert.equal(descriptor.get,undefined);assert.equal(descriptor.writable,false);assert.equal(descriptor.configurable,false) + assert.throws(()=>Object.defineProperty(lease,"lifetimeSignal",{value:new AbortController().signal})) + const order:string[]=[],rawRelease=f.client.release.bind(f.client) + signal.addEventListener("abort",()=>order.push("revoked"),{once:true}) + f.client.release=(unsafe)=>{assert.equal(signal.aborted,true);order.push("release");rawRelease(unsafe)} + if(mode==="healthy")lease.release() + if(mode==="destroy")lease.release(true) + if(mode==="error")f.emitError() + if(mode==="abort")input.abort() + if(mode==="deadline")await new Promise(done=>setTimeout(done,45)) + assert.equal(signal.aborted,true);assert.deepEqual(order,["revoked","release"]) + lease.release(true);f.emitError();assert.deepEqual(f.releases,[mode!=="healthy"]) + await assert.rejects(lease.query("SELECT forbidden_after_close"),refused);assert.equal(f.queries.length,0) + } +}) + +test("idle owner loss while external async work is pending synchronously revokes the retained lease",async()=>{ + const f=fixture(),lease=await f.connect({deadlineAt:Date.now()+1000}),pending=deferred() + const signal=lease.lifetimeSignal!;let consumed=false + const afterExternal=(async()=>{await pending.promise;if(signal.aborted)return;consumed=true})() + f.emitError();assert.equal(signal.aborted,true);pending.resolve();await afterExternal + assert.equal(consumed,false);assert.deepEqual(f.releases,[true]);assert.equal(f.checkouts(),1);assert.equal(f.queries.length,0) +}) + +test("late abandoned checkout retains socket error absorber, never publishes facade or replays checkout",async()=>{ + const waiting=deferred(),controller=new AbortController(),socket=new EventEmitter() + let checkouts=0,releases=0,published=false + const client:ProbeStoragePgClient={on:socket.on.bind(socket),removeListener:socket.removeListener.bind(socket),async query(){assert.fail("late client cannot query")},release(unsafe){assert.equal(unsafe,true);releases++;queueMicrotask(()=>socket.emit("error",Error("NEVER_PRIVATE_LATE_SOCKET")))}} + const connect=createProbeStorageConnectionConnector(()=>{checkouts++;return waiting.promise}) + const result=connect({deadlineAt:Date.now()+1000,signal:controller.signal}).then(()=>{published=true}) + controller.abort();await assert.rejects(result,refused);waiting.resolve(client);await tick();await tick() + assert.equal(published,false);assert.equal(checkouts,1);assert.equal(releases,1);assert(socket.listenerCount("error")>0) + socket.emit("error",Error("NEVER_PRIVATE_REPEATED_SOCKET"));assert.equal(releases,1) +}) + +test("genuine signed core/connectRegistry/node strong facades preserve physical lifetime revocation (synthetic PG only)",async()=>{ + const fs=await import("node:fs/promises"),path=await import("node:path"),os=await import("node:os"),crypto=await import("node:crypto"),{Pool}=await import("pg") + const core=await import("./bandwidth-control-source.js"),node=await import("./bandwidth-node-source.js"),bootstrap=await import("./bandwidth-node-bootstrap.js") + const controlFixture=await import("../test/fixtures/bandwidth-control-identity.js"),registry=(await import("../test/fixtures/bandwidth-peer-identity.js")).fixture() + const directory=await fs.mkdtemp(path.join(await fs.realpath(os.tmpdir()),"mm-probe-lifetime-")),keys=crypto.generateKeyPairSync("ed25519"),eventKey=crypto.randomBytes(32),issuerKey=crypto.randomBytes(32) + const sha=(value:string|Buffer)=>crypto.createHash("sha256").update(value).digest("hex"),signed=(format:string,text:string)=>{const bytes=Buffer.from(text);return {format,payload:bytes.toString("base64url"),signature:crypto.sign(null,bytes,keys.privateKey).toString("base64url")}} + const config={databaseUrl:"postgresql://bandwidth_runtime:synthetic@127.0.0.1:1/owned_database",applicationDatabaseUrl:"postgresql://restricted_application:synthetic@127.0.0.1:1/application_database",trustKeyFile:path.join(directory,"trust.pem"),eventKeyFile:path.join(directory,"event.key"),attestationFile:path.join(directory,"source.json"),trustKeySha256:sha(keys.publicKey.export({type:"spki",format:"der"}))} + const record={format:"mmapp-bandwidth-control-source-v1" as const,installationId:registry.expected.installationId,controlGeneration:crypto.randomUUID(),databaseName:"owned_database",databaseOid:123,runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",endpointSha256:core.bandwidthControlEndpointSha256(config.databaseUrl),schemaSourceSha256:core.bandwidthControlSchemaSourceSha256(),catalogSha256:controlFixture.expected.catalogSha256,eventKeySha256:sha(eventKey),attestationRef:"Synthetic signed lifecycle propagation, not actual PostgreSQL acceptance"} + // Registry fixture pins its own issuer fingerprint; use that signed public + // value and a matching controlled key digest only for core paths below. + const nodeRecord={format:"mmapp-bandwidth-node-bootstrap-v2" as const,installationId:record.installationId,issuerKeySha256:sha(issuerKey),quiescenceProof:registry.expected.quiescenceProof,registrySchemaSourceSha256:bootstrap.bandwidthPeerRegistrySchemaSourceSha256(),registryCatalogSha256:registry.expected.registryCatalogSha256,attestationRef:"Synthetic signed lifecycle bootstrap, not operator enrollment authority"} + // The original pure validator consumes these genuine fixture facts. Replace + // only the synthetic authority's issuer value to match our actual key bytes. + registry.expected.issuerKeySha256=nodeRecord.issuerKeySha256 + for(const row of registry.rows)if(row.kind==="authority"){const value=JSON.parse(row.raw_json!);value.issuer_key_sha256=nodeRecord.issuerKeySha256;row.raw_json=JSON.stringify(value)} + Object.assign(controlFixture.expected,{installationId:record.installationId,controlGeneration:record.controlGeneration,sourceRecordSha256:sha(core.canonicalBandwidthControlRecord(record)),eventKeySha256:record.eventKeySha256}) + const issuerKeyFile=path.join(directory,"issuer.key"),bootstrapFile=path.join(directory,"bootstrap.json") + await Promise.all([fs.writeFile(config.trustKeyFile,keys.publicKey.export({type:"spki",format:"pem"})),fs.writeFile(config.eventKeyFile,eventKey),fs.writeFile(config.attestationFile,JSON.stringify(signed("mmapp-bandwidth-control-envelope-v1",core.canonicalBandwidthControlRecord(record)))),fs.writeFile(issuerKeyFile,issuerKey),fs.writeFile(bootstrapFile,JSON.stringify(signed("mmapp-bandwidth-node-bootstrap-envelope-v1",bootstrap.canonicalBandwidthNodeBootstrap(nodeRecord))))]) + const originalConnect=Pool.prototype.connect,sockets:EventEmitter[]=[],releases:boolean[]=[] + Pool.prototype.connect=(async()=>{const socket=new EventEmitter();sockets.push(socket);return Object.assign(socket,{async query(input:{text:string}){ + if(input.text.startsWith("/* bandwidth-control:registry-identity */"))return {command:"SELECT",rows:[...controlFixture.rows().map(row=>({family:"core",...row})),...registry.rows.map(row=>({family:"registry",...row}))]} + if(input.text.startsWith("/* bandwidth-control:identity */"))return {command:"SELECT",rows:controlFixture.rows()} + assert.equal(input.text,"SELECT 1");return {command:"SELECT",rows:[]} + },release(unsafe:boolean){releases.push(unsafe)}})}) as never + const source=core.createConfiguredBandwidthControlSource(config) + let coreSignal:AbortSignal|undefined + const nodes=node.createConfiguredBandwidthNodeSource({...source,async connectRegistry(...args:Parameters){const value=await source.connectRegistry(...args);coreSignal=value.lifetimeSignal;return value}},{issuerKeyFile,issuerKeySha256:nodeRecord.issuerKeySha256,bootstrapFile}) + try{ + for(const mode of ["core","registry","node"]){ + const input={deadlineAt:Date.now()+5000},session=mode==="node"?await nodes.open(input):undefined + const connection=session?.connection??(mode==="core"?await source.connect(input):await source.connectRegistry(input,async()=>{})) + const signal=connection.lifetimeSignal;assert(signal instanceof AbortSignal);assert.equal(signal.aborted,false) + if(mode==="node")assert.equal(signal,coreSignal) + const property=Object.getOwnPropertyDescriptor(connection,"lifetimeSignal")!;assert.equal(property.value,signal);assert.equal(property.writable,false);assert.equal(property.configurable,false) + sockets.at(-1)!.emit("error",Error("NEVER_PRIVATE_PHYSICAL_LOSS"));assert.equal(signal.aborted,true) + session?.close();connection.release(true);assert.equal(releases.length,sockets.length);assert(releases.every(Boolean)) + sockets.at(-1)!.emit("error",Error("NEVER_PRIVATE_LATE_LOSS")) + } + }finally{ + nodes.close();await source.close();Pool.prototype.connect=originalConnect;eventKey.fill(0);issuerKey.fill(0) + const actual=await fs.realpath(directory),parent=await fs.realpath(os.tmpdir()),stat=await fs.lstat(directory) + assert(stat.isDirectory()&&!stat.isSymbolicLink());assert.equal(path.dirname(actual),parent);assert.match(path.basename(actual),/^mm-probe-lifetime-[A-Za-z0-9]+$/) + await fs.rm(actual,{recursive:true,force:true}) + } +}) + +test("query budget cannot extend initial deadline, including mutation of caller options", async () => { + const f = fixture(), options = { deadlineAt: Date.now() + 1000 }, original = options.deadlineAt + const lease = await f.connect(options); options.deadlineAt += 100000 + await lease.query("SELECT 1", [], { timeoutMs: 10000 }) + assert.equal(f.queries[0].query_timeout <= original - Date.now() + 100, true) + assert.equal(f.queries[0].query_timeout <= 1000, true) + lease.release() +}) + +test("invalid query budgets reject before dispatch without poisoning a healthy idle lease", async () => { + const f = fixture(), lease = await f.connect({ deadlineAt: Date.now() + 1000 }) + for (const timeoutMs of [0, -1, 10001, NaN, Infinity, 1.5]) await assert.rejects(lease.query("SELECT 1", [], { timeoutMs }), refused) + await assert.rejects(lease.query(""), refused) + assert.equal(lease.lifetimeSignal?.aborted,false) + assert.equal(f.queries.length, 0); await lease.query("SELECT 1"); lease.release() + assert.deepEqual(f.releases, [false]) +}) + +for (const source of ["initial", "query"] as const) test("in-flight " + source + " abort destroys once; late success cannot revive the lease", async () => { + const driver = deferred(), initial = new AbortController(), query = new AbortController(), f = fixture(() => driver.promise) + const lease = await f.connect({ deadlineAt: Date.now() + 1000, signal: initial.signal }) + const pending = lease.query("SELECT 1", [], { timeoutMs: 1000, signal: query.signal }) + ;(source === "initial" ? initial : query).abort(Error("NEVER_PRIVATE_ABORT_REASON")) + await assert.rejects(pending, unknown); assert.deepEqual(f.releases, [true]) + assert.equal(lease.lifetimeSignal?.aborted,true) + lease.release(); driver.resolve(result()); await tick() + await assert.rejects(lease.query("SELECT 2"), refused) + assert.equal(f.queries.length, 1); assert.deepEqual(f.releases, [true]) +}) + +for (const source of ["initial", "query"] as const) test("wall timer bounds hung driver by " + source + " deadline", async () => { + const driver = deferred(), f = fixture(() => driver.promise) + const options = { deadlineAt: Date.now() + (source === "initial" ? 25 : 1000) }, lease = await f.connect(options) + options.deadlineAt += 100000 + const started = Date.now(), pending = lease.query("SELECT 1", [], { timeoutMs: source === "query" ? 25 : 1000 }) + await assert.rejects(pending, unknown) + assert.equal(lease.lifetimeSignal?.aborted,true) + assert.equal(Date.now() - started < 1000, true); assert.deepEqual(f.releases, [true]) + driver.reject(Error("NEVER_PRIVATE_LATE_FAILURE")); await tick(); lease.release(); assert.deepEqual(f.releases, [true]) +}) + +test("driver failure and idle socket error permanently destroy the lease", async () => { + const f = fixture(async () => { throw Error("NEVER_PRIVATE_DRIVER_DIAGNOSTIC") }), lease = await f.connect({ deadlineAt: Date.now() + 1000 }) + await assert.rejects(lease.query("SELECT 1"), unknown); lease.release(); f.emitError() + assert.equal(lease.lifetimeSignal?.aborted,true) + assert.deepEqual(f.releases, [true]); assert.equal(f.queries.length, 1) + const idle = fixture(), idleLease = await idle.connect({ deadlineAt: Date.now() + 1000 }) + idle.emitError(); await assert.rejects(idleLease.query("SELECT 1"), refused); idleLease.release() + assert.deepEqual(idle.releases, [true]); assert.equal(idle.queries.length, 0) +}) + +test("overlap never dispatches a second query and invalidates the first attempt", async () => { + const driver = deferred(), f = fixture(() => driver.promise), lease = await f.connect({ deadlineAt: Date.now() + 1000 }) + const first = lease.query("SELECT 1"), second = lease.query("SELECT 2") + await assert.rejects(second, refused); await assert.rejects(first, unknown) + driver.resolve(result()); await tick(); lease.release() + assert.equal(f.queries.length, 1); assert.equal(f.maximumActive(), 1); assert.deepEqual(f.releases, [true]) +}) + +test("release during in-flight work destroys instead of healthy pooling", async () => { + const driver = deferred(), f = fixture(() => driver.promise), lease = await f.connect({ deadlineAt: Date.now() + 1000 }) + const pending = lease.query("SELECT 1"); lease.release(); lease.release() + await assert.rejects(pending, unknown); driver.resolve(result()); await tick() + assert.deepEqual(f.releases, [true]) +}) + +test("COMMIT accepts only acknowledged COMMIT and refuses ROLLBACK or lost ACK", async () => { + const good = fixture(), goodLease = await good.connect({ deadlineAt: Date.now() + 1000 }) + await goodLease.query("COMMIT"); goodLease.release(); assert.deepEqual(good.releases, [false]) + for (const mode of ["rollback", "lost"] as const) { + for (const statement of ["COMMIT;", "/* owner */ COMMIT TRANSACTION; -- durable", "END WORK;"]) { + const f = fixture(async () => { if (mode === "lost") throw Error("NEVER_PRIVATE_COMMIT_ACK_FAILURE"); return result("ROLLBACK") }), lease = await f.connect({ deadlineAt: Date.now() + 1000 }) + await assert.rejects(lease.query(statement), unknown); lease.release() + assert.equal(f.queries.length, 1); assert.deepEqual(f.releases, [true]) + } + } +}) + +test("COMMIT abort after driver-side completion is unknown and never pooled healthy", async () => { + const controller = new AbortController(), f = fixture(async () => { controller.abort(); return result("COMMIT") }) + const lease = await f.connect({ deadlineAt: Date.now() + 1000, signal: controller.signal }) + await assert.rejects(lease.query("COMMIT"), unknown); lease.release() + assert.equal(f.queries.length, 1); assert.deepEqual(f.releases, [true]) +}) + +test("idle abort expires only its lease; a fresh quarantine checkout uses fresh options", async () => { + const f = fixture(), fresh = fixture(), clients = [f.client, fresh.client], aborted = new AbortController() + let checkouts = 0 + const connect = createProbeStorageConnectionConnector(async () => { checkouts++; return clients.shift()! }) + const lease = await connect({ deadlineAt: Date.now() + 1000, signal: aborted.signal }) + aborted.abort(); lease.release(); assert.deepEqual(f.releases, [true]) + const freshLease = await connect({ deadlineAt: Date.now() + 1000, signal: new AbortController().signal }) + await freshLease.query("SELECT 1"); freshLease.release() + assert.deepEqual(fresh.releases, [false]); assert.equal(checkouts, 2); assert.equal(f.queries.length, 0) +}) + +test("idle initial deadline destroys without waiting for a query or explicit release", async () => { + const f = fixture(), lease = await f.connect({ deadlineAt: Date.now() + 25 }) + await new Promise(resolve => setTimeout(resolve, 40)) + assert.deepEqual(f.releases, [true]); await assert.rejects(lease.query("SELECT 1"), refused) + lease.release(); assert.deepEqual(f.releases, [true]); assert.equal(f.queries.length, 0) +}) + +test("an abandoned late client's throwing release is absorbed and attempted once", async () => { + const f = fixture(), late = deferred(), controller = new AbortController() + let releases = 0 + const client: ProbeStoragePgClient = { ...f.client, release() { releases++; throw Error("NEVER_PRIVATE_RELEASE_DIAGNOSTIC") } } + const connect = createProbeStorageConnectionConnector(() => late.promise), pending = connect({ deadlineAt: Date.now() + 1000, signal: controller.signal }) + controller.abort(); await assert.rejects(pending, refused) + late.resolve(client); await tick(); assert.equal(releases, 1); assert.equal(f.queries.length, 0) +}) + +const diagnosticId = "11111111-1111-4111-8111-111111111111" +async function guardFailure(work: Promise, branch: string, kind = "other", phase = "query") { + let caught: unknown + try { await work; assert.fail("Expected guard failure") } catch (error) { caught = error } + assert(caught instanceof ProbeStorageUnknown || caught instanceof ProbeStorageRefused) + assert.deepEqual(capturePollError(caught).probe, { phase, queryKind: kind, branch }) + assert(!JSON.stringify(capturePollError(caught)).includes("NEVER_PRIVATE")) + return caught +} +test("guard provenance: plain driver rejection differs from synthesized Unknown and exact kinds are diagnostic only", async () => { + const meta = "SELECT o.id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id WHERE o.connection_id=$1 AND p.operation_id=$2 AND s.step_index=0 AND s.intended_state=$3::jsonb LIMIT 2" + for (const [text, kind] of [[principalSourceSql,"principal"],[meta,"meta"],["/* private */ END WORK;","commit"],[meta+" ","other"],["SELECT 'NEVER_PRIVATE SQL/password'","other"]]) { + const f=fixture(async()=>{throw Error("NEVER_PRIVATE driver timeout syntax error 57014")}),lease=await f.connect({deadlineAt:Date.now()+1000}) + const error=await guardFailure(lease.query(text,["NEVER_PRIVATE credential"]),"driver-reject",kind) + assert.equal(capturePollError(error).origin.kind,"other");assert.equal(capturePollError(error).origin.sqlState,null) + let final=error as Error;for(const site of ["storage","journal.tx","poll.safe"] as const)final=carryPollError(new Error("public"),final,site) + const log=pollErrorLog(final,diagnosticId,"servers.poll")! + assert.deepEqual(log.provenance.sites,["probe.query","storage","journal.tx","poll.safe"]) + assert.equal(log.provenance.probe?.branch,"driver-reject");assert(Buffer.byteLength(JSON.stringify(log))<=1024) + assert(!JSON.stringify(log).includes("NEVER_PRIVATE"));assert.equal(pollErrorLog(final,diagnosticId,"other.operation"),null) + assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]);lease.release() + } + const pg=new DatabaseError("NEVER_PRIVATE real pg class",1,"error");pg.code="57014" + const driver=fixture(async()=>{throw pg}),lease=await driver.connect({deadlineAt:Date.now()+1000}) + const error=await guardFailure(lease.query(principalSourceSql),"driver-reject","principal") + assert.equal(capturePollError(error).origin.kind,"pg");assert.equal(capturePollError(error).origin.sqlState,"57014") +}) +test("guard provenance: synchronous physical query throw has its own branch", async () => { + let calls=0,releases=0 + const lease=await createProbeStorageConnectionConnector(async()=>({query(){calls++;throw Error("NEVER_PRIVATE sync")},release(unsafe){assert.equal(unsafe,true);releases++}}))({deadlineAt:Date.now()+1000}) + await guardFailure(lease.query("COMMIT"),"driver-throw","commit");assert.equal(calls,1);assert.equal(releases,1) +}) +test("guard provenance: query wall timeout, caller abort and lease socket close remain distinct and late success cannot clear them", async () => { + for(const mode of ["deadline","abort","lease-error","lease-closed"] as const){ + const driver=deferred(),controller=new AbortController(),f=fixture(()=>driver.promise),lease=await f.connect({deadlineAt:Date.now()+1000}) + const pending=lease.query("SELECT 1",[],{timeoutMs:mode==="deadline"?20:1000,signal:controller.signal}) + if(mode==="abort")controller.abort(Error("NEVER_PRIVATE abort"));if(mode==="lease-error")f.emitError();if(mode==="lease-closed")lease.release() + const error=await guardFailure(pending,mode);driver.resolve(result());await tick() + assert.equal(capturePollError(error).probe?.branch,mode);assert.equal(f.queries.length,1);assert.deepEqual(f.releases,[true]);lease.release() + } +}) +test("guard provenance: initial lease deadline, result shape, result accessor and COMMIT ACK guard are synthetic, not driver rejection", async () => { + const hung=deferred(),expired=fixture(()=>hung.promise),old=await expired.connect({deadlineAt:Date.now()+20}) + await guardFailure(old.query("SELECT 1",[],{timeoutMs:1000}),"lease-timeout");hung.resolve(result());await tick() + for(const [value,text,branch] of [[{rows:"NEVER_PRIVATE rows",command:"SELECT"},"SELECT 1","result-shape"],[null,"SELECT 1","result-access"],[result("ROLLBACK"),"COMMIT","commit-ack"]] as const){ + const f=fixture(async()=>value as unknown as Result),lease=await f.connect({deadlineAt:Date.now()+1000}) + await guardFailure(lease.query(text),branch,text==="COMMIT"?"commit":"other");assert.deepEqual(f.releases,[true]);assert.equal(f.queries.length,1) + } +}) +test("guard provenance: post-resolution closed and inactive checks retain their precise branch", async () => { + for(const mode of ["result-closed","result-inactive"] as const){ + const realNow=Date.now,start=realNow(),socket=new EventEmitter();let clock=start,releases=0 + if(mode==="result-inactive")Date.now=()=>clock + try{ + const lease=await createProbeStorageConnectionConnector(async()=>({on:socket.on.bind(socket),removeListener:socket.removeListener.bind(socket),query(){return Promise.resolve(result()).then(value=>{queueMicrotask(()=>queueMicrotask(()=>{if(mode==="result-closed")socket.emit("error",Error("NEVER_PRIVATE socket"));else clock=start+2000}));return value})},release(unsafe){assert.equal(unsafe,true);releases++}}))({deadlineAt:start+1000}) + await guardFailure(lease.query("SELECT 1"),mode);assert.equal(releases,1) + }finally{Date.now=realNow} + } +}) +test("guard provenance: checkout rejected/thrown/aborted/timed out never acquires query authority", async () => { + for(const mode of ["driver-reject","driver-throw","abort","deadline"] as const){ + const controller=new AbortController(),later=deferred(),f=fixture();let calls=0 + const checkout:()=>Promise=mode==="driver-throw"?()=>{calls++;throw Error("NEVER_PRIVATE checkout")}:mode==="driver-reject"?async()=>{calls++;throw Error("NEVER_PRIVATE checkout")}:()=>{calls++;return later.promise} + const pending=createProbeStorageConnectionConnector(checkout)({deadlineAt:Date.now()+(mode==="deadline"?20:1000),signal:controller.signal}) + if(mode==="abort")controller.abort();await guardFailure(pending,mode,"other","checkout") + later.resolve(f.client);await tick();assert.equal(calls,1);assert.equal(f.queries.length,0) + } +}) +test("guard provenance: pre-dispatch guards preserve Refused and query counts", async () => { + const f=fixture(),lease=await f.connect({deadlineAt:Date.now()+1000}) + await guardFailure(lease.query("",[],{timeoutMs:1}),"input");assert.equal(f.queries.length,0) + const aborted=new AbortController();aborted.abort();await guardFailure(lease.query("SELECT 1",[],{timeoutMs:1,signal:aborted.signal}),"inactive") + await guardFailure(lease.query("SELECT 1"),"closed");assert.equal(f.queries.length,0);assert.deepEqual(f.releases,[true]) + const waiting=deferred(),busy=fixture(()=>waiting.promise),borrow=await busy.connect({deadlineAt:Date.now()+1000}),first=borrow.query("SELECT 1") + const firstCheck=guardFailure(first,"lease-closed");await guardFailure(borrow.query("SELECT 2"),"busy");await firstCheck;waiting.resolve(result());await tick();assert.equal(busy.queries.length,1) +}) +test("guard provenance: successful result/reference, named principal and public error fields stay unchanged", async () => { + const value=result("COMMIT"),f=fixture(async()=>value),lease=await f.connect({deadlineAt:Date.now()+1000}) + assert.equal(await lease.query("COMMIT"),value);lease.release();assert.deepEqual(f.releases,[false]) + const plain=new ProbeStorageUnknown(),before=Object.getOwnPropertyDescriptors(plain) + carryPollError(plain,new Error("NEVER_PRIVATE"),"probe.query");assert.deepEqual(Object.getOwnPropertyDescriptors(plain),before) +}) diff --git a/backend/src/services/server-probe-storage-connection.ts b/backend/src/services/server-probe-storage-connection.ts new file mode 100644 index 00000000..bced0d2f --- /dev/null +++ b/backend/src/services/server-probe-storage-connection.ts @@ -0,0 +1,129 @@ +import { carryPollError, tagPollProbeGuard, type PollProbeGuard } from "./server-poll-error-provenance.js" +/** Bounded PG lease for opt-in probe storage. Construction/import performs no + * checkout, schema change, execution, or registration. */ +import type { QueryConfig } from "pg" +import { principalSourceSql, principalSourceStatementName } from "../modules/mcp/principal-security-sql.js" +import { ProbeStorageRefused, ProbeStorageUnknown, type ProbeStorageConnection, type ProbeStorageOptions } from "./server-probe-journal-maintenance.js" + +export interface ProbeStoragePgClient { + query(config: QueryConfig & { query_timeout: number }): Promise<{ rows: Record[]; command: string }> + release(unsafe?: boolean): void + on?(event: "error", listener: (error: Error) => void): unknown + removeListener?(event: "error", listener: (error: Error) => void): unknown +} +/** Trusted server source/test seam only; never accepted from request JSON. */ +export type ProbeStorageCheckout = () => Promise +const productionCheckout: ProbeStorageCheckout = async () => (await import("../db/index.js")).pool.connect() +const MAX_TIMER = 2147483647 +const alive = (deadlineAt: number, signals: readonly AbortSignal[]) => Date.now() < deadlineAt && signals.every(signal => !signal.aborted) +const commits = (text: string) => /^(?:COMMIT|END)(?:\s+(?:WORK|TRANSACTION))?\s*;?$/i.test(text.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/--[^\r\n]*/g, " ").trim()) +const queryKind = (text: unknown): PollProbeGuard["queryKind"] => text === "SELECT pg_advisory_xact_lock(hashtextextended('server-poll-storage-v1',0))" ? "storage-lock" : text === principalSourceSql ? "principal" : text === "SELECT o.id FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_operation_steps s ON s.operation_id=o.id WHERE o.connection_id=$1 AND p.operation_id=$2 AND s.step_index=0 AND s.intended_state=$3::jsonb LIMIT 2" ? "meta" : typeof text === "string" && commits(text) ? "commit" : "other" + + +/** A real wall timer bounds drivers/pool waits even when their timeout or signal + * support is absent. Late values/failures are always consumed. */ +function bounded(work: () => Promise, deadlineAt: number, signals: readonly AbortSignal[], failure: (branch: "deadline" | "abort" | "driver-throw" | "driver-reject", original?: unknown) => Error, stop: () => void, late: (value: T) => void = () => {}): Promise { + return new Promise((resolve, reject) => { + let settled = false, timer: ReturnType | undefined + const cleanup = () => { if (timer) clearTimeout(timer); for (const signal of signals) signal.removeEventListener("abort", onAbort) } + const abort = (branch: "deadline" | "abort" | "driver-throw" | "driver-reject" = signals.some(signal => signal.aborted) ? "abort" : "deadline", original?: unknown) => { if (settled) return; settled = true; cleanup(); stop(); reject(failure(branch, original)) } + const onAbort = () => abort("abort") + const schedule = () => { if (!alive(deadlineAt, signals)) { abort(); return }; timer = setTimeout(schedule, Math.max(1, Math.min(MAX_TIMER, deadlineAt - Date.now()))) } + for (const signal of signals) signal.addEventListener("abort", onAbort, { once: true }) + if (!alive(deadlineAt, signals)) { abort(); return } + schedule() + let pending: Promise + try { pending = work() } catch (error) { abort("driver-throw", error); return } + Promise.resolve(pending).then(value => { + if (settled) { late(value); return } + if (!alive(deadlineAt, signals)) { abort(); late(value); return } + settled = true; cleanup(); resolve(value) + }, error => abort("driver-reject", error)) + }) +} + +export function createProbeStorageConnectionConnector(checkout: ProbeStorageCheckout = productionCheckout): (options: ProbeStorageOptions) => Promise { + return async rawOptions => { + const deadlineAt = rawOptions.deadlineAt, initialSignal = rawOptions.signal + const signals = initialSignal ? [initialSignal] : [] + if (!Number.isSafeInteger(deadlineAt) || !alive(deadlineAt, signals)) throw tagPollProbeGuard(new ProbeStorageRefused(), undefined, { phase: "checkout", queryKind: "other", branch: Number.isSafeInteger(deadlineAt) ? "inactive" : "input" }) + const lifetime = new AbortController() + const disposeLate = (client: ProbeStoragePgClient) => { + lifetime.abort() + // No facade was published. Absorb a delayed physical socket error even + // when this abandoned checkout is destroyed after caller cancellation. + try { client.on?.("error", () => {}) } catch { /* Still attempt destroy. */ } + try { client.release(true) } catch { /* Never re-enter the pool healthy. */ } + } + const client = await bounded(checkout, deadlineAt, signals, (branch, original) => tagPollProbeGuard(new ProbeStorageRefused(), original, { phase: "checkout", queryKind: "other", branch }), () => lifetime.abort(), disposeLate) + let closed = false, busy = false, interrupt: (() => void) | undefined, leaseTimer: ReturnType | undefined + let leaseEnd: PollProbeGuard["branch"] = "lease-closed" + const onError = () => { if (!closed) leaseEnd = "lease-error"; close(true) } + const onInitialAbort = () => { if (!closed) leaseEnd = "lease-abort"; close(true) } + const close = (unsafe: boolean) => { + if (closed) return + closed = true + // Synchronous revocation precedes physical release and any external + // asynchronous authority/Q continuation that still retains this facade. + lifetime.abort() + if (leaseTimer) clearTimeout(leaseTimer) + initialSignal?.removeEventListener("abort", onInitialAbort) + // Destroyed clients retain the error absorber for late socket errors. + if (!unsafe) client.removeListener?.("error", onError) + try { client.release(unsafe) } catch { /* Release is attempted exactly once, without private driver errors. */ } + interrupt?.() + } + const scheduleLease = () => { + if (closed) return + if (!alive(deadlineAt, signals)) { leaseEnd = initialSignal?.aborted ? "lease-abort" : "lease-timeout"; close(true); return } + leaseTimer = setTimeout(scheduleLease, Math.max(1, Math.min(MAX_TIMER, deadlineAt - Date.now()))) + } + client.on?.("error", onError) + initialSignal?.addEventListener("abort", onInitialAbort, { once: true }) + scheduleLease() + if (closed) throw tagPollProbeGuard(new ProbeStorageRefused(), undefined, { phase: "checkout", queryKind: "other", branch: "closed" }) + const facade: ProbeStorageConnection = { + async query(text, values, options) { + const refused = (branch: PollProbeGuard["branch"]) => tagPollProbeGuard(new ProbeStorageRefused(), undefined, { phase: "query", queryKind: queryKind(text), branch }) + if (closed) throw refused("closed") + if (busy) { close(true); throw refused("busy") } + const requested = options?.timeoutMs ?? 10000, querySignal = options?.signal + if (typeof text !== "string" || !text.trim() || !Number.isSafeInteger(requested) || requested < 1 || requested > 10000) throw refused("input") + const allSignals = [...new Set([...signals, ...(querySignal ? [querySignal] : [])])] + if (!alive(deadlineAt, allSignals)) { close(true); throw refused("inactive") } + const queryDeadline = Math.min(deadlineAt, Date.now() + requested) + busy = true + const interrupted = new AbortController() + interrupt = () => interrupted.abort() + let inspectingResult = false, taggedResult = false + try { + // Prepare only this immutable statement, once per physical backend. + // Every execution binds fresh values. Any schema/driver failure below + // destroys the lease; there is no reparse, fallback or query replay. + const result = await bounded(() => client.query({ text, values, ...(text === principalSourceSql ? { name: principalSourceStatementName } : {}), query_timeout: Math.max(1, Math.min(requested, queryDeadline - Date.now())) }), queryDeadline, [...allSignals, interrupted.signal], (branch, original) => tagPollProbeGuard(new ProbeStorageUnknown(), original, { phase: "query", queryKind: queryKind(text), branch: branch === "abort" && interrupted.signal.aborted && !allSignals.some(signal => signal.aborted) ? leaseEnd : branch }), () => close(true)) + inspectingResult = true + let rejected: PollProbeGuard["branch"] | undefined + if (closed) rejected = "result-closed" + else if (!alive(queryDeadline, allSignals)) rejected = "result-inactive" + else if (!Array.isArray(result.rows)) rejected = "result-shape" + else if (commits(text) && result.command !== "COMMIT") rejected = "commit-ack" + if (rejected) { + close(true); taggedResult = true; throw tagPollProbeGuard(new ProbeStorageUnknown(), undefined, { phase: "query", queryKind: queryKind(text), branch: rejected }) + } + return result + } catch (error) { + close(true) + // A null/throwing result accessor is a result guard failure, not driver rejection. + const source = inspectingResult && !taggedResult ? tagPollProbeGuard(new ProbeStorageUnknown(), error, { phase: "query", queryKind: queryKind(text), branch: "result-access" }) : error + throw carryPollError(new ProbeStorageUnknown(), source, "probe.query") + } finally { interrupt = undefined; busy = false } + }, + release(unsafe = false) { close(unsafe || busy || !alive(deadlineAt, signals)) }, + } + Object.defineProperty(facade, "lifetimeSignal", { value: lifetime.signal, enumerable: true, writable: false, configurable: false }) + return facade + } +} + +/** Inert default connector. Fresh quarantine options create an independent lease. */ +export const connectProbeStorage = createProbeStorageConnectionConnector() diff --git a/backend/src/services/server-probe-storage-fresh.sql.test.ts b/backend/src/services/server-probe-storage-fresh.sql.test.ts new file mode 100644 index 00000000..667e5752 --- /dev/null +++ b/backend/src/services/server-probe-storage-fresh.sql.test.ts @@ -0,0 +1,41 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" +assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_FRESH, "true") +const url = new URL(process.env.DATABASE_URL ?? "") +assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") +assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const { pool, closePool } = await import("../db/index.js") +const { Pool } = await import("pg") +const { applySqlMigrations } = await import("../db/migrate.js") +const { applyLegacyProbeFixtureMigrations } = await import("../db/legacy-probe-fixture.js") +const { readFile } = await import("node:fs/promises") +const migration = await readFile(new URL("../../drizzle/0037_server_probe_storage_v2.sql", import.meta.url), "utf8") +try { + await applySqlMigrations(pool); await applySqlMigrations(pool) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length) + assert.deepEqual((await pool.query("SELECT * FROM server_probe_storage_usage")).rows, [{ scope: "global", admissions: 0, reserved_files: 0, reserved_bytes: 0 }]) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM information_schema.columns WHERE table_name='server_probe_executions' AND column_name='receipt_ciphertext'")).rows[0].n, 0) + const name = "owned_probe_fresh_" + randomUUID().replaceAll("-", "") + await pool.query(`CREATE DATABASE "${name}" TEMPLATE template0`) + const otherUrl = new URL(url); otherUrl.pathname = "/" + name + const other = new Pool({ connectionString: otherUrl.toString(), max: 2, connectionTimeoutMillis: 3000 }) + try { + await applyLegacyProbeFixtureMigrations(other) + const receiptId = randomUUID() + await other.query("INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,$2,'rest',$3,$2,'JOURNAL_UNKNOWN',0,0)", [receiptId, "a".repeat(64), randomUUID()]) + const before = (await other.query("SELECT row_to_json(q) AS value FROM server_probe_execution_quarantines q")).rows + const client = await other.connect() + try { await assert.rejects(client.query(migration)) } finally { client.release(true) } + assert.deepEqual((await other.query("SELECT row_to_json(q) AS value FROM server_probe_execution_quarantines q")).rows, before) + assert.equal((await other.query("SELECT to_regclass('public.server_probe_storage_usage') IS NULL AS absent")).rows[0].absent, true) + assert.equal((await other.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0037_server_probe_storage_v2'")).rows[0].n, 0) + assert.equal((await other.query("SELECT count(*)::int AS n FROM information_schema.columns WHERE table_name='server_probe_executions' AND column_name='receipt_ciphertext'")).rows[0].n, 1) + } finally { await other.end(); await pool.query(`DROP DATABASE "${name}"`) } + console.log("Owned fresh probe migration:", JSON.stringify({ normalStartupTwice: true, migrationCount: MIGRATION_MANIFEST.length, emptyGlobalSeed: true, nonemptyLegacyRefusedBeforeDdl: true, legacyRowsAndColumnPreserved: true, noForgedMarker: true })) + await (await import("../modules/mcp/security.js")).accessGeneration() + console.log("Owned fresh MCP host:", JSON.stringify(await (await import("../modules/mcp/server-probe-storage-host.sql-helper.js")).acceptServerProbeStorageHostSql())) + console.log("Owned fresh public all tools:", JSON.stringify(await (await import("../modules/mcp/server-probe-all-tools.sql-helper.js")).acceptServerProbeAllToolsSql())) + console.log("Owned fresh REST host:", JSON.stringify(await (await import("./server-probe-storage-rest.sql-helper.js")).acceptServerProbeStorageRestSql())) +} finally { await closePool() } diff --git a/backend/src/services/server-probe-storage-history.sql.test.ts b/backend/src/services/server-probe-storage-history.sql.test.ts new file mode 100644 index 00000000..2c4f8069 --- /dev/null +++ b/backend/src/services/server-probe-storage-history.sql.test.ts @@ -0,0 +1,105 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import { basename, dirname, resolve } from "node:path" +import type { ServerProbeHistoryBinding } from "./server-probe-journal-history.js" +import type { ServerProbeReceipt } from "./server-probe-execution.js" + +assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_HISTORY, "true") +const url = new URL(process.env.DATABASE_URL ?? "") +assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") +assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const { pool, closePool } = await import("../db/index.js") +const { applyLegacyProbeFixtureMigrations: applySqlMigrations } = await import("../db/legacy-probe-fixture.js") +const security = await import("../modules/mcp/security.js") +const { createMcpServerProbeReadAuthority } = await import("./server-probe-authority.js") +const { createServerProbeStorageHistoryReader } = await import("./server-probe-storage-history.js") +const { ProbePayloadExpired, ProbePayloadUnavailable } = await import("./server-probe-storage-journal-adapter.js") +const { createProbeJournalStorage } = await import("./server-probe-journal-maintenance.js") +const cases: string[] = [] +let source: number, destination: number +const crypto = { decrypt: security.decryptSecret, proof: async (domain: string, value: unknown) => security.privateSecretFingerprint(security.fingerprint(value), "server-probe-journal-v1:" + domain) } + +async function seed(tamper = "", separated = false) { + const at = separated ? Date.now() : Date.now() - 32 * 86400000, ownerId = "history-" + randomUUID(), connectionId = randomUUID(), oldCredential = randomUUID(), credentialId = randomUUID() + const binding: ServerProbeHistoryBinding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), context: { + identity: { kind: "mcp", ownerId, connectionId, credentialId: oldCredential, executionId: randomUUID(), issuedAt: at, expiresAt: at + 3600000, operationName: "servers.probes.run" }, + sourceServerId: source, destinationServerId: destination, tool: "bandwidth" } } + const identity = binding.context.identity + const until = new Date(Date.now() + 3600000), principal = { connectionId, credentialId, credentialExpiresAt: until.toISOString() } + const policy = { id: connectionId, version: 1, status: "active", mode: "observer", permissions: ["mm:servers:read"], expiresAt: until.toISOString(), scope: { serverIds: [source, destination], operationIds: ["servers.probes.receipt.read"], allowGlobal: false } } + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned historical reader',$2,$3::jsonb,$4::jsonb,$5,$6,clock_timestamp())", [connectionId, ownerId, JSON.stringify(policy.permissions), JSON.stringify(policy), await security.accessGeneration(), until]) + for (const [id, expires] of [[oldCredential, new Date(identity.expiresAt)], [credentialId, until]]) await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [id, connectionId, randomUUID(), expires]) + const receipt: ServerProbeReceipt = { receiptId: binding.receiptId, serverId: source, destinationServerId: destination, tool: "bandwidth", state: "completed", httpStatus: 200, output: "owned historical measurement", data: { kind: "bandwidth" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } + const plaintext = JSON.stringify({ version: 1, context: binding.context, bindingProof: binding.bindingProof, receipt }) + const cipher = tamper === "cipher" ? "untrusted-cipher" : await security.encryptSecret(plaintext) + const proof = tamper === "proof" ? "b".repeat(64) : await crypto.proof("receipt", plaintext) + if (separated) { + // Model an incomplete writer using only valid v2 guard transitions. No + // payload DELETE or trigger bypass is used to manufacture this gap. + await pool.query("INSERT INTO server_probe_storage_admissions(receipt_id,owner_id,identity_kind,execution_id,binding_proof,authority_expires_at,claim_token,retention_days) VALUES($1,$2,'mcp',$3,$4,$5,$6,30)", [binding.receiptId, ownerId, identity.executionId, binding.bindingProof, new Date(identity.expiresAt), randomUUID()]) + await pool.query("UPDATE server_probe_storage_admissions SET claim_token=NULL,claim_attempted_at=clock_timestamp() WHERE receipt_id=$1", [binding.receiptId]) + } + await pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,authority_issued_at,authority_expires_at,source_server_id,destination_server_id,tool,created_at) + VALUES($1,$2,'mcp',$3,$4,'mcp',$5,$6,$7,$8,$9,$10,'bandwidth',$11)`, [binding.receiptId, binding.bindingProof, identity.executionId, ownerId, connectionId, oldCredential, at, new Date(identity.expiresAt), source, destination, new Date(at)]) + await pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,created_at,received_at) VALUES($1,1,'native-bandwidth',1000,'received','received',$2,$2)", [binding.receiptId, new Date(at)]) + if (separated) await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_proof=$2,finished_at=$3 WHERE receipt_id=$1", [binding.receiptId, proof, new Date(at)]) + else await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_ciphertext=$2,receipt_proof=$3,finished_at=$4 WHERE receipt_id=$1", [binding.receiptId, cipher, proof, new Date(at)]) + let decrypts = 0, hook: (() => Promise) | undefined + const access = createMcpServerProbeReadAuthority(principal) + const reader = () => createServerProbeStorageHistoryReader({ options: { deadlineAt: Date.now() + 120000 }, crypto: { ...crypto, async decrypt(value) { decrypts++; await hook?.(); return crypto.decrypt(value) } }, + async assertReadAuthority(actual) { + assert.deepEqual(actual, binding) + const current = await access.readAuthority() + assert.equal(current.ownerId, ownerId); assert.equal(current.connectionId, connectionId) + assert.equal(current.credentialId, credentialId); assert.notEqual(credentialId, oldCredential) + assert([source, destination].every(id => current.scopeServerIds.includes(id))) + await access.assertProtections([source, destination], current) + } }) + return { binding, receipt, reader, ownerId, connectionId, credentialId, oldCredential, decrypts: () => decrypts, onDecrypt(value: () => Promise) { hook = value } } +} +type Fixture = Awaited> +const quarantine = async (f: Fixture) => { await pool.query("INSERT INTO server_probe_execution_quarantines(receipt_id,identity_kind,execution_id,binding_proof,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,'mcp',$2,$3,$4,'JOURNAL_UNKNOWN',1,1)", [f.binding.receiptId, f.binding.context.identity.executionId, f.binding.bindingProof, "c".repeat(64)]) } +const connect = async () => { const client = await pool.connect(); return { query: (text: string, values?: unknown[]) => client.query(text, values), release: (destroy?: boolean) => client.release(destroy) } } +const storage = createProbeJournalStorage({ connect, safetyWindowMs: 60000, policy: { retentionDays: 30, globalBytes: 64000000, ownerBytes: 1000000, globalFiles: 100, ownerFiles: 3, globalAdmissions: 100, ownerAdmissions: 3 }, authorize: async () => "c".repeat(64) }) +const purge = (f: Fixture) => storage.purge(f.ownerId, 10, { deadlineAt: Date.now() + 120000 }) + +try { + await applySqlMigrations(pool) + await pool.query("UPDATE traffic_settings SET enabled=false; UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false; UPDATE mcp_settings SET enabled=true") + const ids = (await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('history-source','127.0.0.1',9,true),('history-destination','127.0.0.1',9,true) RETURNING id")).rows.map(row => Number(row.id)) + ;[source, destination] = ids + const normal = await seed(), expired = await seed(), duringPurge = await seed(), duringQuarantine = await seed(), duringRevoke = await seed(), badProof = await seed("proof"), badCipher = await seed("cipher"), scope = await seed(), protection = await seed(), foreign = await seed() + const client = await pool.connect() + try { await client.query(await readFile(new URL("../db/proposals/server-probe-storage-v2.sql", import.meta.url), "utf8")) } finally { client.release(true) } + const orphan = randomUUID() + await assert.rejects(pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,authority_issued_at,authority_expires_at,source_server_id,destination_server_id,tool) + VALUES($1,$2,'mcp',$3,$4,'mcp',$5,$6,$7,$8,$9,$10,'bandwidth')`, [orphan, normal.binding.bindingProof, randomUUID(), normal.ownerId, normal.connectionId, normal.credentialId, Date.now(), new Date(Date.now() + 3600000), source, destination]), /no live consumed admission/) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE receipt_id=$1", [orphan])).rows[0].n, 0) + cases.push("actual v2 guard rejects orphan anchor without consumed matching admission") + const incomplete = await seed("", true) + await assert.rejects(incomplete.reader().read(incomplete.binding), ProbePayloadUnavailable); assert.equal(incomplete.decrypts(), 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions j JOIN server_probe_storage_admissions a USING(receipt_id) WHERE j.receipt_id=$1 AND a.purged_at IS NULL", [incomplete.binding.receiptId])).rows[0].n, 1) + cases.push("valid spent terminal anchor without payload yields explicit unavailable and preserves attempt metadata") + assert.deepEqual(await normal.reader().read(normal.binding), normal.receipt) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_credentials WHERE id=$1 AND expires_at { assert.equal((await purge(duringPurge)).purged, 1) }) + await assert.rejects(duringPurge.reader().read(duringPurge.binding)); cases.push("separate final SQL snapshot observes payload purge committed during decrypt and refuses stale result") + duringQuarantine.onDecrypt(() => quarantine(duringQuarantine)) + await assert.rejects(duringQuarantine.reader().read(duringQuarantine.binding)); cases.push("quarantine committed on another backend during decrypt blocks adoption") + duringRevoke.onDecrypt(async () => { await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [duringRevoke.credentialId]) }) + await assert.rejects(duringRevoke.reader().read(duringRevoke.binding)); cases.push("actual current credential revocation during decrypt blocks adoption despite unchanged original anchor") + for (const f of [badProof, badCipher]) { await assert.rejects(f.reader().read(f.binding)); cases.push("converted " + (f === badProof ? "forged proof" : "malformed ciphertext") + " refuses without rewriting original envelope") } + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}',$2::jsonb) WHERE id=$1", [scope.connectionId, JSON.stringify([source])]) + await assert.rejects(scope.reader().read(scope.binding)); assert.equal(scope.decrypts(), 0); cases.push("missing current destination scope refuses before decrypt") + await pool.query("UPDATE mcp_connections SET owner_id='foreign-owner' WHERE id=$1", [foreign.connectionId]) + await assert.rejects(foreign.reader().read(foreign.binding)); assert.equal(foreign.decrypts(), 0); cases.push("current foreign owner refuses before decrypt") + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'owned-history-test')", [randomUUID(), JSON.stringify({ kind: "server", id: String(destination), serverId: destination, protection: "deny" })]) + await assert.rejects(protection.reader().read(protection.binding)); assert.equal(protection.decrypts(), 0); cases.push("current participant protection denies before decrypt") + console.log("Owned separated probe history SQL:", JSON.stringify({ cases: cases.length, names: cases, actualSql: true, productionCrypto: true, productionCurrentReadAuthority: true, syntheticOriginalExecution: true, nativeCalls: 0, metadataGcRun: false, productionDdlApplied: false, routesRegistered: false })) +} finally { await closePool() } diff --git a/backend/src/services/server-probe-storage-history.test.ts b/backend/src/services/server-probe-storage-history.test.ts new file mode 100644 index 00000000..7368c60e --- /dev/null +++ b/backend/src/services/server-probe-storage-history.test.ts @@ -0,0 +1,33 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { createServerProbeStorageHistoryReader } from "./server-probe-storage-history.js" +import { ServerProbeHistoryUnavailable, type ServerProbeHistoryBinding } from "./server-probe-journal-history.js" + +const at = 1800000000000 +const binding: ServerProbeHistoryBinding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), context: { + identity: { kind: "mcp", ownerId: "owner", executionId: randomUUID(), connectionId: randomUUID(), credentialId: randomUUID(), issuedAt: at - 2000, expiresAt: at - 1000, operationName: "servers.probes.run" }, sourceServerId: 1, tool: "ping" } } + +test("fixed history reader refuses invalid or extended absolute budgets before I/O", () => { + for (const deadlineAt of [NaN, Infinity, at, at - 1, at + 120001]) assert.throws(() => createServerProbeStorageHistoryReader({ options: { deadlineAt }, now: () => at, assertReadAuthority: async () => { assert.fail("No authority or SQL") } }), ServerProbeHistoryUnavailable) +}) +test("aborted read never reaches current authority or SQL", async () => { + const controller = new AbortController(); controller.abort() + const reader = createServerProbeStorageHistoryReader({ options: { deadlineAt: at + 120000, signal: controller.signal }, now: () => at, assertReadAuthority: async () => { assert.fail("No authority or SQL") } }) + await assert.rejects(reader.read(binding), { name: "AbortError" }) +}) +test("copied deadline cannot be extended after construction", async () => { + let time = at + const options = { deadlineAt: at + 1000 } + const reader = createServerProbeStorageHistoryReader({ options, now: () => time, assertReadAuthority: async () => { assert.fail("No authority or SQL") } }) + options.deadlineAt = at + 120000; time = at + 1000 + await assert.rejects(reader.read(binding), ServerProbeHistoryUnavailable) +}) +test("authority refusal and time spent in authority both stop before SQL", async () => { + const denial = Error("current authority revoked") + const refused = createServerProbeStorageHistoryReader({ options: { deadlineAt: at + 1000 }, now: () => at, assertReadAuthority: async () => { throw denial } }) + await assert.rejects(refused.read(binding), error => error === denial) + let time = at, calls = 0 + const expired = createServerProbeStorageHistoryReader({ options: { deadlineAt: at + 1000 }, now: () => time, assertReadAuthority: async original => { assert.deepEqual(original, binding); calls++; time = at + 1000 } }) + await assert.rejects(expired.read(binding), ServerProbeHistoryUnavailable); assert.equal(calls, 1) +}) diff --git a/backend/src/services/server-probe-storage-history.ts b/backend/src/services/server-probe-storage-history.ts new file mode 100644 index 00000000..7a57b62a --- /dev/null +++ b/backend/src/services/server-probe-storage-history.ts @@ -0,0 +1,70 @@ +/** Opt-in reader for the separate v2 payload schema. No DDL, execution-time + * credential revival, mutations, claim/replay, retention or registration. */ +import { createServerProbeJournalHistoryReader, ServerProbeHistoryUnavailable, + type ServerProbeHistoryBinding, type ServerProbeJournalHistoryDependencies } from "./server-probe-journal-history.js" +import type { ProbeStorageOptions } from "./server-probe-journal-maintenance.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" + +export interface ServerProbeStorageHistoryDependencies { + assertReadAuthority: ServerProbeJournalHistoryDependencies["assertReadAuthority"] + crypto?: ServerProbeJournalHistoryDependencies["crypto"] + options: ProbeStorageOptions + now?: () => number +} +const SQL = `/* probe-storage:history */ SELECT j.*,p.receipt_ciphertext, + p.receipt_proof AS payload_proof,a.receipt_id AS admission_receipt_id, + a.owner_id AS admission_owner_id,a.identity_kind AS admission_identity_kind, + a.execution_id AS admission_execution_id,a.binding_proof AS admission_binding_proof, + a.authority_expires_at AS admission_authority_expires_at,a.claim_token, + a.claim_attempted_at,a.purged_at, + (SELECT count(*) FROM server_probe_storage_admissions s + WHERE s.receipt_id=$1 OR(s.identity_kind=$2 AND s.execution_id=$3)) AS admission_count, + (SELECT count(*) FROM server_probe_execution_quarantines q + WHERE q.receipt_id=$1 OR(q.identity_kind=$2 AND q.execution_id=$3)) AS quarantine_count + FROM server_probe_executions j + LEFT JOIN server_probe_storage_admissions a ON a.receipt_id=j.receipt_id + LEFT JOIN server_probe_execution_payloads p ON p.receipt_id=j.receipt_id + WHERE j.receipt_id=$1 OR(j.identity_kind=$2 AND j.execution_id=$3) LIMIT 2` +function must(value: unknown): asserts value { if (!value) throw new ServerProbeHistoryUnavailable() } + +export function createServerProbeStorageHistoryReader(deps: ServerProbeStorageHistoryDependencies) { + const options = { ...deps.options }, now = deps.now ?? Date.now, authorize = deps.assertReadAuthority + must(typeof authorize === "function" && Number.isSafeInteger(options.deadlineAt) && options.deadlineAt > now() && options.deadlineAt <= now() + 120000) + const live = () => { options.signal?.throwIfAborted(); must(now() < options.deadlineAt) } + const fence = async (binding: Readonly) => { live(); await authorize(structuredClone(binding)); live() } + return createServerProbeJournalHistoryReader({ crypto: deps.crypto, assertReadAuthority: fence, + async loadArchive(binding) { + await fence(binding) + // Separate autocommit snapshots deliberately observe quarantine/purge + // committed during decryption. No repeatable-read snapshot across reads. + const client = await connectProbeStorage(options) + let unsafe = false + const result = await (async () => { + try { + live() + return await client.query(SQL, [binding.receiptId, binding.context.identity.kind, binding.context.identity.executionId], { + signal: options.signal, timeoutMs: Math.max(1, Math.min(10000, options.deadlineAt - now())) }) + } + catch (error) { unsafe = true; throw error } + finally { client.release(unsafe) } + })() + await fence(binding) + return result.rows.map((row: Record) => { + const identity = binding.context.identity + must((row.admission_count === "1" || row.admission_count === 1) && row.admission_receipt_id === binding.receiptId + && row.admission_owner_id === identity.ownerId && row.admission_identity_kind === identity.kind + && row.admission_execution_id === identity.executionId && row.admission_binding_proof === binding.bindingProof + && new Date(row.admission_authority_expires_at as Date | string).getTime() === identity.expiresAt + && row.claim_token === null && row.claim_attempted_at !== null + && Number.isFinite(new Date(row.claim_attempted_at as Date | string).getTime())) + let state: "present" | "expired" | "unavailable" + if (row.purged_at !== null) { + must(Number.isFinite(new Date(row.purged_at as Date | string).getTime()) && row.receipt_ciphertext === null && row.payload_proof === null) + state = "expired" + } else if (row.receipt_ciphertext === null) { must(row.payload_proof === null); state = "unavailable" } + else { must(row.payload_proof === row.receipt_proof); state = "present" } + return { ...row, stored_payload_state: state } + }) + }, + }) +} diff --git a/backend/src/services/server-probe-storage-host-io.ts b/backend/src/services/server-probe-storage-host-io.ts new file mode 100644 index 00000000..92b50712 --- /dev/null +++ b/backend/src/services/server-probe-storage-host-io.ts @@ -0,0 +1,167 @@ +/** Shared opt-in v2 I/O. Request budgets own no backend; SQL leases do. + * Transport and callbacks are trusted source composition, never request JSON. */ +import { AsyncLocalStorage } from "node:async_hooks" +import type { PoolClient } from "pg" +import { McpError } from "../modules/mcp/security.js" +import type { ServerProbeJournal } from "./server-probe-execution.js" +import { createSqlServerProbeJournal, type ServerProbeJournalContext, type ServerProbeJournalDependencies } from "./server-probe-journal.js" +import { createServerProbeStorageJournalAdapter } from "./server-probe-storage-journal-adapter.js" +import { connectProbeStorage } from "./server-probe-storage-connection.js" +import { ProbeStorageUnknown, validateProbeStoragePolicy, type ProbeStorageConnection, type ProbeStoragePolicy, type ProbeStorageOptions } from "./server-probe-journal-maintenance.js" + +interface Lease { sql: ProbeStorageConnection; client: Pick; active: boolean } +interface RequestWindow extends ProbeStorageOptions { active: boolean; parent?: RequestWindow } +export type ProbeStorageHostLimits = Omit +export interface ProbeStorageJournalExecution { signal?: AbortSignal; deadlineAt?: number } +export type ProbeStorageHostJournal = (context: ServerProbeJournalContext, dependencies: Pick, execution?: ProbeStorageJournalExecution) => ServerProbeJournal +const denied = (): never => { throw new McpError("STATE_CONFLICT", "Контекст хранилища диагностики завершён или не подтверждён", 409) } +const combined = (...values: (AbortSignal | undefined)[]) => { + const signals = [...new Set(values.filter((value): value is AbortSignal => !!value))] + return signals.length > 1 ? AbortSignal.any(signals) : signals[0] +} + +/** Covers MTU's 260s budget without extending private plan or original authority. */ +export function serverProbeStorageJournalDeadline(authorityExpiresAt: number, preparedExpiresAt: number, now = Date.now()) { + const deadlineAt = Math.min(now + 300000, authorityExpiresAt, preparedExpiresAt) + if (![now, authorityExpiresAt, preparedExpiresAt, deadlineAt].every(Number.isSafeInteger) || deadlineAt <= now) return denied() + return deadlineAt +} + +export function createProbeStorageHostIo(limits: ProbeStorageHostLimits, transport: "mcp" | "rest") { + if (transport !== "mcp" && transport !== "rest") return denied() + const configured = Object.freeze({ ...limits }), leases = new AsyncLocalStorage(), requests = new AsyncLocalStorage() + const storagePolicy = (retention: unknown): ProbeStoragePolicy => validateProbeStoragePolicy({ retention_days: retention, + global_bytes: configured.globalBytes, owner_bytes: configured.ownerBytes, global_files: configured.globalFiles, + owner_files: configured.ownerFiles, global_admissions: configured.globalAdmissions, owner_admissions: configured.ownerAdmissions }) + storagePolicy(30) + const checkWindow = (value: RequestWindow | undefined) => { + for (let ancestor = value; ancestor; ancestor = ancestor.parent) if (!ancestor.active) return denied() + return value + } + const request = () => checkWindow(requests.getStore()) + const current = () => { + request() + const lease = leases.getStore() + if (lease && !lease.active) return denied() + return lease + } + // This layer owns every backend it acquires. Borrowed planner transactions + // remain caller-owned and are never silently released or independently raced. + const owned = async (options: ProbeStorageOptions, work: () => Promise): Promise => { + if (current()) return denied() + const ownerWindow = request() + const physical = await connectProbeStorage(options) + let inTransaction = false, broken = false + const sql: ProbeStorageConnection = { + async query(text, values, queryOptions) { + checkWindow(ownerWindow) + if (!lease?.active || broken) return denied() + if (/^BEGIN\b/i.test(text)) inTransaction = true + try { + const result = await physical.query(text, values, queryOptions) + if (text === "COMMIT" || text === "ROLLBACK") inTransaction = false + return result + } catch (error) { broken = true; physical.release(true); throw error } + }, + release(unsafe) { if (unsafe) { broken = true; physical.release(true) } }, + } + const lease: Lease = { active: true, sql, client: { query: sql.query as PoolClient["query"] } } + // Bound the whole fixed callback too: owner checks may await local crypto + // or revocation files between SQL statements. Late continuations retain an + // inactive facade and cannot resume SQL after this method has returned. + let abort!: () => void + const stopped = new Promise((_resolve, reject) => { + abort = () => { broken = true; lease.active = false; physical.release(true); reject(new ProbeStorageUnknown()) } + }) + const timer = setTimeout(abort, Math.max(1, options.deadlineAt - Date.now())) + options.signal?.addEventListener("abort", abort, { once: true }) + try { + const completion = Promise.race([Promise.resolve().then(() => { + checkWindow(ownerWindow) + if (!lease.active || options.signal?.aborted || Date.now() >= options.deadlineAt) throw new ProbeStorageUnknown() + return leases.run(lease, work) + }), stopped]) + if (options.signal?.aborted || Date.now() >= options.deadlineAt) abort() + const result = await completion + if (!lease.active || options.signal?.aborted || Date.now() >= options.deadlineAt) { broken = true; throw new ProbeStorageUnknown() } + return result + } finally { + clearTimeout(timer); options.signal?.removeEventListener("abort", abort) + lease.active = false; physical.release(broken || inTransaction) + } + } + const metadataOptions = (extra?: ProbeStorageOptions): ProbeStorageOptions => { + const window = request() + return { deadlineAt: Math.min(Date.now() + 10000, window?.deadlineAt ?? Infinity, extra?.deadlineAt ?? Infinity), + signal: combined(window?.signal, extra?.signal) } + } + const query = async (text: string, values?: unknown[]) => { + const lease = current() + return lease ? lease.sql.query(text, values) : owned(metadataOptions(), () => current()!.sql.query(text, values)) + } + const journal: ProbeStorageHostJournal = (rawContext, dependencies, execution) => { + if (rawContext.identity.kind !== transport) return denied() + const context = structuredClone(rawContext), options = { deadlineAt: serverProbeStorageJournalDeadline(context.identity.expiresAt, execution?.deadlineAt ?? NaN), signal: execution?.signal } + let durable: ServerProbeJournal | undefined, busy = false + const run = async (quarantine: boolean, work: (value: ServerProbeJournal) => Promise): Promise => { + if (busy || current()) return denied() + busy = true + try { + // Cleanup deliberately excludes the aborted/expired request window. + return await owned(quarantine ? { deadlineAt: Date.now() + 10000, signal: AbortSignal.timeout(10000) } : options, async () => { + if (!durable) { + const readPolicy = async () => { + const rows = (await query("SELECT enabled,requests_retention_days FROM mcp_settings WHERE id=1")).rows + if (rows.length !== 1 || transport === "mcp" && rows[0].enabled !== true) return denied() + return storagePolicy(rows[0].requests_retention_days) + } + const policy = await readPolicy() + const boundary = createServerProbeStorageJournalAdapter(context, { policy, readPolicy, options, + assertAuthority: dependencies.assertAuthority, + connect: async () => { const lease = current(); if (!lease) return denied(); return lease.sql } }) + durable = createSqlServerProbeJournal(context, { assertAuthority: dependencies.assertAuthority, storageBoundary: boundary }) + } + return work(durable) + }) + } finally { busy = false } + } + return Object.freeze({ + claim: value => run(false, actual => actual.claim(value)), + intent: value => run(false, actual => actual.intent(value)), + received: value => run(false, actual => actual.received(value)), + finish: value => run(false, actual => actual.finish(value)), + quarantine: value => run(true, actual => actual.quarantine(value)), + read: (receiptId, proof) => run(false, actual => actual.read(receiptId, proof)), + }) + } + return { + query, rows: async (text: string, values: unknown[]) => (await query(text, values)).rows, + currentClient: () => current()?.client, + assertOutside() { if (current()) return denied() }, + journal, + async runRequest(options: ProbeStorageOptions, work: () => Promise): Promise { + if (current()) return denied() + const parent = request() + if (!Number.isSafeInteger(options.deadlineAt)) return denied() + const window: RequestWindow = { active: true, parent, + deadlineAt: Math.min(options.deadlineAt, Date.now() + 300000, parent?.deadlineAt ?? Infinity), + signal: combined(options.signal, parent?.signal) } + try { return await requests.run(window, work) } finally { window.active = false } + }, + async withClient(work: (client: Pick) => Promise, options?: ProbeStorageOptions): Promise { + const lease = current() + return lease ? work(lease.client) : owned(metadataOptions(options), () => work(current()!.client)) + }, + async withBorrowed(client: Pick, work: () => Promise): Promise { + if (current()) return denied() + const ownerWindow = request() + const sql: ProbeStorageConnection = { query: (text, values) => { + checkWindow(ownerWindow) + if (!lease.active) return denied() + return client.query(text, values) + }, release: () => denied() } + const lease: Lease = { active: true, client: { query: sql.query as PoolClient["query"] }, sql } + try { return await leases.run(lease, work) } finally { lease.active = false } + }, + } +} diff --git a/backend/src/services/server-probe-storage-journal-adapter.test.ts b/backend/src/services/server-probe-storage-journal-adapter.test.ts new file mode 100644 index 00000000..7bd36bf8 --- /dev/null +++ b/backend/src/services/server-probe-storage-journal-adapter.test.ts @@ -0,0 +1,145 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { test } from "node:test" +import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" +import { createSqlServerProbeJournal, type ServerProbeJournalContext } from "./server-probe-journal.js" +import { createServerProbeStorageJournalAdapter, ProbePayloadExpired, ProbePayloadUnavailable } from "./server-probe-storage-journal-adapter.js" +import { ProbeStorageUnknown, type ProbeStorageConnection, type ProbeStoragePolicy, type ProbeStorageOptions } from "./server-probe-journal-maintenance.js" +import type { ServerProbeJournalBinding, ServerProbeReceipt } from "./server-probe-execution.js" + +type Row = Record +function fixture() { + let state = { admissions: [] as Row[], anchors: [] as Row[], stages: [] as Row[], quarantines: [] as Row[], payloads: [] as Row[], + usage: [{ scope: "global", admissions: 0, reserved_files: 0, reserved_bytes: 0 }] as Row[] } + let allowed = true, nextConnection = 0, queued = Promise.resolve(), lose = "", policyDrift = false, revokeAt = "", expireAt = "" + const budgets: ProbeStorageOptions[] = [] + const events: { connection: number; tag: string }[] = [], unsafe: boolean[] = [] + let now = 10000 + const controller = new AbortController() + const policy: ProbeStoragePolicy = { retentionDays: 7, globalBytes: 4000000, ownerBytes: 2000000, globalFiles: 4, ownerFiles: 2, globalAdmissions: 4, ownerAdmissions: 2 } + const context: ServerProbeJournalContext = { identity: { kind: "rest", authentication: "jwt", ownerId: "actual-host-fixture-owner", executionId: randomUUID(), requestId: "accepted-host-request", issuedAt: 1000, expiresAt: 100000 }, sourceServerId: 1, tool: "ping" } + const binding: ServerProbeJournalBinding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), ownerId: context.identity.ownerId, transport: "rest", requestId: "accepted-host-request", issuedAt: 1000, serverIds: [1], tool: "ping" } + const connect = async (options: ProbeStorageOptions): Promise => { + budgets.push(options) + const connection = ++nextConnection; let local: typeof state | undefined, unlock: (() => void) | undefined, mutation = "", writerAdmitted = false + return { release(destroy) { unsafe.push(!!destroy); unlock?.() }, async query(text, values = []) { + const tag = text.match(/\/\* (probe-[\w-]+:[\w-]+) \*\//)?.[1] ?? text; events.push({ connection, tag }) + const result = (rows: Row[] = []) => { if (revokeAt === tag) allowed = false; if (expireAt === tag) now += 10001; return { rows: structuredClone(rows) } } + if (text === "BEGIN" || text.startsWith("SET LOCAL")) return result() + if (text === "SELECT pg_advisory_xact_lock_shared($1::bigint)") { + assert(!local && !writerAdmitted) + assert.deepEqual(values, [PARTITION_WRITER_ADVISORY_KEY]) + writerAdmitted = true + return result() + } + if (/^\s*(?:\/\*.*?\*\/\s*)?(?:INSERT|UPDATE|DELETE)\b/.test(text)) assert(writerAdmitted) + if (tag === "probe-storage:lock") { const prior = queued; queued = new Promise(resolve => { unlock = resolve }); await prior; local = structuredClone(state); return result() } + assert(local, "all journal SQL uses storage-owned transaction and global storage lock") + if (tag === "probe-journal:lock") return result() + if (text === "ROLLBACK") return result() + if (text === "COMMIT") { state = structuredClone(local); if (lose && mutation === lose) { lose = ""; throw Error("private-driver-error") } return result() } + if (tag === "probe-storage:owner-counter") { if (!local.usage.some(row => row.scope === values[0])) local.usage.push({ scope: values[0], admissions: 0, reserved_files: 0, reserved_bytes: 0 }); return result() } + if (tag === "probe-storage:counters") return result(local.usage.filter(row => (values[0] as string[]).includes(row.scope as string))) + if (tag === "probe-storage:reserve") { + if (local.admissions.some(row => row.receipt_id === values[0] || row.identity_kind === values[2] && row.execution_id === values[3])) return result() + const keys = ["receipt_id", "owner_id", "identity_kind", "execution_id", "binding_proof", "authority_expires_at", "claim_token"] + local.admissions.push({ ...Object.fromEntries(keys.map((key, i) => [key, values[i]])), claim_attempted_at: null, purged_at: null }); mutation = "reserve"; return result([{ receipt_id: values[0] }]) + } + if (tag === "probe-storage:charge") { const rows = local.usage.filter(row => (values[0] as string[]).includes(row.scope as string)); rows.forEach(row => { row.admissions = Number(row.admissions) + 1; row.reserved_files = Number(row.reserved_files) + 1; row.reserved_bytes = Number(row.reserved_bytes) + Number(values[1]) }); return result(rows) } + if (["probe-storage:admission", "probe-storage-adapter:admission"].includes(tag)) return result(local.admissions.filter(row => row.receipt_id === values[0] || row.identity_kind === values[1] && row.execution_id === values[2])) + if (tag === "probe-storage:consume") { const row = local.admissions.find(row => row.receipt_id === values[0] && row.claim_token === values[1]); if (!row) return result(); row.claim_token = null; row.claim_attempted_at = new Date(now); return result([{ receipt_id: row.receipt_id }]) } + if (tag === "probe-journal:server_probe_executions") return result(local.anchors.filter(row => row.receipt_id === values[0] || row.identity_kind === values[1] && row.execution_id === values[2])) + if (tag === "probe-journal:server_probe_execution_quarantines") return result(local.quarantines.filter(row => row.receipt_id === values[0] || row.identity_kind === values[1] && row.execution_id === values[2])) + if (tag === "probe-journal:servers") return result((values[0] as number[]).map(id => ({ id }))) + if (tag === "probe-journal:claim") { const keys = ["receipt_id", "binding_proof", "identity_kind", "execution_id", "actor_id", "authentication", "mcp_connection_id", "mcp_credential_id", "request_id", "authority_issued_at", "authority_expires_at", "source_server_id", "destination_server_id", "tool"]; local.anchors.push({ ...Object.fromEntries(keys.map((key, i) => [key, values[i]])), state: "claimed" }); mutation = "claim"; return result([{ receipt_id: values[0] }]) } + if (tag === "probe-journal:stages") return result(local.stages.filter(row => row.receipt_id === values[0])) + if (tag === "probe-journal:intent") { local.stages.push({ receipt_id: values[0], stage_index: values[1], kind: values[2], timeout_ms: values[3], state: "intent" }); return result([{ stage_index: values[1] }]) } + if (tag === "probe-journal:received") { const row = local.stages.find(row => row.receipt_id === values[0] && row.stage_index === values[1])!; row.state = "received"; return result([{ stage_index: values[1] }]) } + if (tag === "probe-journal:finish-stored") { const row = local.anchors.find(row => row.receipt_id === values[0])!; assert.equal(row.state, "claimed"); Object.assign(row, { state: values[1], attempted_stages: values[2], completed_stages: values[3], receipt_proof: values[4] }); return result([{ receipt_id: values[0] }]) } + if (tag === "probe-storage:publish") { assert.equal(local.anchors.find(row => row.receipt_id === values[0])?.receipt_proof, values[2]); local.payloads.push({ receipt_id: values[0], receipt_ciphertext: values[1], receipt_proof: values[2] }); mutation = "publish"; return result([{ receipt_id: values[0] }]) } + if (tag === "probe-storage-adapter:payload") return result(local.payloads.filter(row => row.receipt_id === values[0])) + if (tag === "probe-journal:quarantine") { const keys = ["receipt_id", "binding_proof", "identity_kind", "execution_id", "identity_proof", "error_code", "attempted_stages", "completed_stages"]; local.quarantines.push(Object.fromEntries(keys.map((key, i) => [key, values[i]]))); mutation = "quarantine"; return result([{ receipt_id: values[0] }]) } + throw Error("unexpected fixture query tag") + } } + } + const assertAuthority = async (actual: Readonly, proof: string) => { assert(allowed); assert.deepEqual(actual, context); assert.equal(proof, binding.bindingProof) } + // Codec fixture only; encryption/authentication already have independent journal tests. + const crypto = { async encrypt(value: string) { return Buffer.from(value).toString("base64") }, async decrypt(value: string) { return Buffer.from(value, "base64").toString() }, async proof(domain: string, value: unknown) { return createHash("sha256").update(domain + JSON.stringify(value)).digest("hex") } } + const boundary = createServerProbeStorageJournalAdapter(context, { connect, assertAuthority, policy, readPolicy: async () => ({ ...policy, retentionDays: policyDrift ? 8 : 7 }), now: () => now, options: { deadlineAt: 120000, signal: controller.signal } }) + const journal = createSqlServerProbeJournal(context, { assertAuthority, crypto, now: () => now, storageBoundary: boundary }) + const receipt: ServerProbeReceipt = { receiptId: binding.receiptId, tool: "ping", serverId: 1, state: "completed", httpStatus: 200, output: "fixture-output", data: { kind: "ping" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } + const stage = async () => { await journal.intent({ receiptId: binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }); await journal.received({ receiptId: binding.receiptId, stageIndex: 1, outcome: "received" }) } + return { journal, boundary, context, binding, receipt, stage, events, unsafe, budgets, state: () => state, revoke() { allowed = false }, restore() { allowed = true }, drift() { policyDrift = true }, abort() { controller.abort() }, expire() { now = 130000 }, lose(tag: string) { lose = tag }, revokeOn(tag: string) { revokeAt = tag }, expireOn(tag: string) { expireAt = tag } } +} + +for (const interruption of ["abort", "expire", "drift"] as const) test("lost terminal COMMIT is quarantined despite execution " + interruption, async () => { + const f = fixture(); await f.journal.claim(f.binding); await f.stage() + f.lose("publish"); await assert.rejects(f.journal.finish(f.receipt), ProbeStorageUnknown) + assert.equal(f.state().anchors[0].state, "completed"); assert.equal(f.state().payloads.length, 1) + f[interruption](); f.revoke() + await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 1, completedStages: 1 }) + assert.equal(f.state().quarantines.length, 1) + assert.equal(f.state().admissions.length, 1); assert.equal(f.state().payloads.length, 1) + assert.notEqual(f.budgets.at(-1)?.signal, f.budgets[0].signal) + assert.equal(f.budgets.at(-1)?.signal?.aborted, false) + assert.equal(f.budgets.at(-1)?.deadlineAt, interruption === "expire" ? 140000 : 20000) + await assert.rejects(f.journal.claim(f.binding)); await assert.rejects(f.journal.finish(f.receipt)) + await assert.rejects(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 2, kind: "native-ping", timeoutMs: 1000 })) + await assert.rejects(f.journal.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "received" })) + await assert.rejects(f.journal.read(f.binding.receiptId, f.binding.bindingProof)) +}) + +test("quarantine cleanup budget expiry rolls back metadata; cleanup COMMIT ACK loss remains unknown", async () => { + const expired = fixture(); await expired.journal.claim(expired.binding) + expired.abort(); expired.expireOn("probe-journal:quarantine") + await assert.rejects(expired.journal.quarantine({ ...expired.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 0, completedStages: 0 })) + assert.equal(expired.state().quarantines.length, 0) + const lost = fixture(); await lost.journal.claim(lost.binding); lost.abort(); lost.lose("quarantine") + await assert.rejects(lost.journal.quarantine({ ...lost.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 0, completedStages: 0 }), ProbeStorageUnknown) + assert.equal(lost.state().quarantines.length, 1); assert(lost.unsafe.includes(true)) + assert.equal(lost.events.filter(event => event.tag === "probe-journal:quarantine").length, 1) +}) + +test("real journal callbacks share admission/anchor and terminal/payload owner transactions; no anchor ciphertext", async () => { + const f = fixture(); assert.equal(await f.journal.claim(f.binding), "claimed"); await f.stage(); await f.journal.finish(f.receipt) + assert.deepEqual(await f.journal.read(f.binding.receiptId, f.binding.bindingProof), f.receipt) + const get = (tag: string) => f.events.find(value => value.tag === tag)! + assert(get("probe-storage:reserve").connection < get("probe-journal:claim").connection) + assert.equal(get("probe-storage:consume").connection, get("probe-journal:claim").connection) + assert.equal(get("probe-journal:finish-stored").connection, get("probe-storage:publish").connection) + assert(!Object.hasOwn(f.state().anchors[0], "receipt_ciphertext")); assert.equal(f.state().payloads.length, 1) + await assert.rejects(f.journal.claim(f.binding)); assert.equal(f.state().anchors.length, 1) + assert(!Object.hasOwn(f.boundary, "purge")); assert(!Object.hasOwn(f.boundary, "collectExpired")) +}) +test("purged and absent payloads are explicit no-replay failures; quarantine dominates", async () => { + const f = fixture(); await f.journal.claim(f.binding); await f.stage(); await f.journal.finish(f.receipt) + f.state().payloads = []; await assert.rejects(f.journal.read(f.binding.receiptId, f.binding.bindingProof), ProbePayloadUnavailable) + f.state().admissions[0].purged_at = new Date(); await assert.rejects(f.journal.read(f.binding.receiptId, f.binding.bindingProof), ProbePayloadExpired) + f.revoke(); await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 1, completedStages: 0 }); f.restore() + assert.equal((await f.journal.read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown") +}) +for (const tag of ["reserve", "claim", "publish"]) test("lost " + tag + " COMMIT never reclaims or republishes", async () => { + const f = fixture() + if (tag === "publish") { await f.journal.claim(f.binding); await f.stage(); f.lose(tag); await assert.rejects(f.journal.finish(f.receipt), ProbeStorageUnknown) } + else { f.lose(tag); await assert.rejects(f.journal.claim(f.binding), ProbeStorageUnknown) } + await assert.rejects(f.journal.claim(f.binding)); assert(f.unsafe.includes(true)); assert.equal(f.state().admissions.length, 1) + if (tag === "reserve") assert.equal(f.state().anchors.length, 0) + if (tag === "publish") { assert.equal(f.state().anchors[0].state, "completed"); assert.equal(f.state().payloads.length, 1) } +}) +test("foreign context, fresh policy drift, revoked authority and uncharged quarantine refuse", async () => { + const f = fixture(); assert.throws(() => f.boundary.assertContext({ ...f.context, sourceServerId: 2 })) + f.revoke(); await assert.rejects(f.journal.claim(f.binding)); assert.equal(f.events.length, 0) + await assert.rejects(f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 0, completedStages: 0 })); assert.equal(f.state().quarantines.length, 0) + f.restore(); f.drift(); await assert.rejects(f.journal.claim(f.binding)); assert.equal(f.state().admissions.length, 0) +}) +test("quota refusal prevents real journal anchor callback, and late authority loss rolls terminal and payload back", async () => { + const full = fixture() + Object.assign(full.state().usage[0], { admissions: 4, reserved_files: 4, reserved_bytes: 4 * 294912 }) + await assert.rejects(full.journal.claim(full.binding)) + assert.equal(full.state().admissions.length, 0); assert.equal(full.state().anchors.length, 0) + assert(!full.events.some(event => event.tag === "probe-journal:claim")) + const late = fixture(); await late.journal.claim(late.binding); await late.stage() + late.revokeOn("probe-storage:publish"); await assert.rejects(late.journal.finish(late.receipt)) + assert.equal(late.state().anchors[0].state, "claimed"); assert.equal(late.state().payloads.length, 0) + assert.equal(late.state().admissions.length, 1) +}) diff --git a/backend/src/services/server-probe-storage-journal-adapter.ts b/backend/src/services/server-probe-storage-journal-adapter.ts new file mode 100644 index 00000000..3cd4d155 --- /dev/null +++ b/backend/src/services/server-probe-storage-journal-adapter.ts @@ -0,0 +1,141 @@ +/** Opt-in bridge to the real journal's SQL callbacks. No DDL, native calls, + * registration, purge or metadata GC. Requires the separate v2 storage schema. */ +import { createHash } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { admitPartitionWriter } from "./partition-write-transaction.js" +import { z } from "zod" +import { createProbeJournalStorage, ProbeStorageRefused, ProbeStorageUnknown, validateProbeStoragePolicy, PROBE_PAYLOAD_MAX_BYTES, + type ProbeStorageConnection, type ProbeStorageOptions, type ProbeStoragePolicy, type ProbeStorageScope, type ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import type { ProbeJournalSql, ServerProbeJournalContext } from "./server-probe-journal.js" + +export interface ProbeStorageJournalBinding { receiptId: string; bindingProof: string } +export type ProbeStoredPayload = { kind: "payload"; ciphertext: string; proof: string; automaticReplay: false } | { kind: "expired"; automaticReplay: false } | { kind: "unavailable"; automaticReplay: false } +export class ProbePayloadExpired extends Error { readonly automaticReplay = false; constructor() { super("Diagnostic payload expired; attempt remains spent; no replay") } } +export class ProbePayloadUnavailable extends Error { readonly automaticReplay = false; constructor() { super("Diagnostic payload unavailable; attempt remains spent; no replay") } } +export interface ServerProbeStorageJournalBoundary { + assertContext(context: ServerProbeJournalContext): void + claim(binding: ProbeStorageJournalBinding, work: (sql: ProbeJournalSql) => Promise): Promise + publish(binding: ProbeStorageJournalBinding, ciphertext: string, proof: string, work: (sql: ProbeJournalSql) => Promise): Promise + transaction(binding: ProbeStorageJournalBinding, operation: "intent" | "received" | "read" | "quarantine", work: (sql: ProbeJournalSql) => Promise): Promise + readPayload(binding: ProbeStorageJournalBinding, sql: ProbeJournalSql): Promise +} +export interface ServerProbeStorageJournalDependencies { + connect(options: ProbeStorageOptions): Promise + /** The SAME trusted planner/operation guard used by the journal host adapter; + * verifies current owner, protections, exact input and source credential epoch. + * No caller-selected native-auth booleans or prepared request JSON allowed. */ + assertAuthority(context: Readonly, bindingProof: string): Promise + /** Fresh root mapping from actual settings, including requests retention and + * deployment quota configuration. Must agree across replicas. */ + readPolicy(): Promise + policy: ProbeStoragePolicy + options: ProbeStorageOptions + now?: () => number +} +const hash = z.string().regex(/^[a-f0-9]{64}$/), uuid = z.string().uuid() +const must = (value: unknown): void => { if (!value) throw new ProbeStorageRefused() } +function policy(value: ProbeStoragePolicy) { + return validateProbeStoragePolicy({ retention_days: value.retentionDays, global_bytes: value.globalBytes, owner_bytes: value.ownerBytes, + global_files: value.globalFiles, owner_files: value.ownerFiles, global_admissions: value.globalAdmissions, owner_admissions: value.ownerAdmissions }) +} + +export function createServerProbeStorageJournalAdapter(rawContext: ServerProbeJournalContext, deps: ServerProbeStorageJournalDependencies): ServerProbeStorageJournalBoundary { + must(typeof deps.connect === "function" && typeof deps.assertAuthority === "function" && typeof deps.readPolicy === "function") + const context = structuredClone(rawContext), configured = policy(deps.policy), now = deps.now ?? Date.now + const connect = deps.connect, assertAuthority = deps.assertAuthority, readPolicy = deps.readPolicy + const options = { ...deps.options }, identity = context.identity + uuid.parse(identity.executionId); must(Number.isSafeInteger(identity.expiresAt) && identity.expiresAt > identity.issuedAt) + must(typeof identity.ownerId === "string" && identity.ownerId.length > 0 && identity.ownerId.length <= 200) + const scope = (binding: ProbeStorageJournalBinding): ProbeStorageScope => { + uuid.parse(binding.receiptId); hash.parse(binding.bindingProof) + return { ownerId: identity.ownerId, identityKind: identity.kind, executionId: identity.executionId, + authorityExpiresAt: new Date(identity.expiresAt).toISOString(), receiptId: binding.receiptId, bindingProof: binding.bindingProof } + } + const live = (current = options) => { current.signal?.throwIfAborted(); must(Number.isFinite(current.deadlineAt) && now() < current.deadlineAt) } + const authorize = async (bound: ProbeStorageScope) => { + live(); must(isDeepStrictEqual(policy(await readPolicy()), configured)); live() + must(now() < identity.expiresAt); await assertAuthority(structuredClone(context), bound.bindingProof); must(now() < identity.expiresAt); live() + // Stability digest, not a capability or native-source authentication proof. + return createHash("sha256").update(JSON.stringify({ context, configured, bound })).digest("hex") + } + const storage = createProbeJournalStorage({ policy: configured, safetyWindowMs: 60000, now, connect, + authorize: async (ownerId, operation, bound) => { + must(bound && ownerId === identity.ownerId && operation !== "purge") + return authorize(bound!) + } }) + const admission = async (sql: ProbeJournalSql, bound: ProbeStorageScope) => { + const rows = (await sql.query(`/* probe-storage-adapter:admission */ SELECT * FROM server_probe_storage_admissions + WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) FOR UPDATE`, [bound.receiptId, bound.identityKind, bound.executionId])).rows + must(rows.length === 1); const row = rows[0] + must(row.receipt_id === bound.receiptId && row.owner_id === bound.ownerId && row.identity_kind === bound.identityKind && row.execution_id === bound.executionId + && row.binding_proof === bound.bindingProof && new Date(row.authority_expires_at as string | Date).toISOString() === bound.authorityExpiresAt) + must(row.purged_at === null || Number.isFinite(new Date(row.purged_at as string | Date).getTime())) + must(row.claim_token === null || uuid.safeParse(row.claim_token).success) + must(row.claim_attempted_at === null || Number.isFinite(new Date(row.claim_attempted_at as string | Date).getTime())) + return row + } + return Object.freeze({ + assertContext(value: ServerProbeJournalContext) { must(isDeepStrictEqual(value, context)) }, + async claim(binding: ProbeStorageJournalBinding, work: (sql: ProbeJournalSql) => Promise) { + return storage.claim(scope(binding), options, work) + }, + async publish(binding: ProbeStorageJournalBinding, ciphertext: string, proof: string, work: (sql: ProbeJournalSql) => Promise) { + return storage.publish(scope(binding), ciphertext, proof, options, work) + }, + async transaction(binding: ProbeStorageJournalBinding, operation: "intent" | "received" | "read" | "quarantine", work: (sql: ProbeJournalSql) => Promise) { + const bound = scope(binding), overlay = operation === "quarantine" + // Recording an uncertain outcome only tightens an already charged attempt. + // It must survive the execution's abort, expiry, revocation and policy drift. + // Its independent budget permits no new admission, native work or payload. + const current = overlay ? { deadlineAt: now() + 10000, signal: AbortSignal.timeout(10000) } : options + const fence = async () => { if (overlay) live(current); else await authorize(bound) } + await fence() + let connection: ProbeStorageConnection + try { connection = await connect(current) } catch { throw new ProbeStorageUnknown() } + let began = false, commitSent = false, committed = false, destroy = false + const query: ProbeStorageSql["query"] = (text, values) => + connection.query(text, values, { signal: current.signal, timeoutMs: Math.max(1, Math.min(10000, current.deadlineAt - now())) }) + const sql: ProbeStorageSql = { query: async (text, values) => { + await fence() + const result = await query(text, values) + await fence(); return result + } } + try { + if (operation === "read") { + await sql.query("BEGIN"); began = true + } else { + await fence() + await query("BEGIN"); began = true + try { + await admitPartitionWriter({ query }, { signal: current.signal, deadlineAt: Date.now() + Math.min(current.deadlineAt - now(), 1000) }) + } catch { throw new ProbeStorageRefused() } + await query("SET LOCAL lock_timeout TO DEFAULT") + await fence() + } + await sql.query(`SET LOCAL statement_timeout='${Math.max(1, Math.min(10000, current.deadlineAt - now()))}ms'`) + await sql.query("/* probe-storage:lock */ SELECT pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0))") + // Existing committed admission is mandatory even after revocation. This + // branch cannot create an admission, anchor, stage or terminal payload. + const row = await admission(sql, bound) + if (!overlay && operation !== "read") must(row.purged_at === null && row.claim_token === null && row.claim_attempted_at !== null) + const result = await work(sql) + await fence(); commitSent = true; await sql.query("COMMIT"); committed = true + return result + } catch (error) { + if (commitSent && !committed || !began) { destroy = true; throw new ProbeStorageUnknown() } + try { await connection.query("ROLLBACK", undefined, { signal: current.signal, timeoutMs: Math.max(1, Math.min(10000, current.deadlineAt - now())) }) } catch { destroy = true } + throw error + } finally { connection.release(destroy) } + }, + async readPayload(binding: ProbeStorageJournalBinding, sql: ProbeJournalSql): Promise { + const bound = scope(binding), row = await admission(sql, bound) + if (row.purged_at !== null) return { kind: "expired", automaticReplay: false } + const rows = (await sql.query("/* probe-storage-adapter:payload */ SELECT receipt_ciphertext,receipt_proof FROM server_probe_execution_payloads WHERE receipt_id=$1", [bound.receiptId])).rows + must(rows.length <= 1) + if (!rows.length) return { kind: "unavailable", automaticReplay: false } + const value = rows[0] + must(typeof value.receipt_ciphertext === "string" && Buffer.byteLength(value.receipt_ciphertext) > 0 && Buffer.byteLength(value.receipt_ciphertext) <= PROBE_PAYLOAD_MAX_BYTES && hash.safeParse(value.receipt_proof).success) + return { kind: "payload", ciphertext: value.receipt_ciphertext as string, proof: value.receipt_proof as string, automaticReplay: false } + }, + }) +} diff --git a/backend/src/services/server-probe-storage-journal.sql.test.ts b/backend/src/services/server-probe-storage-journal.sql.test.ts new file mode 100644 index 00000000..9d887c29 --- /dev/null +++ b/backend/src/services/server-probe-storage-journal.sql.test.ts @@ -0,0 +1,268 @@ +import assert from "node:assert/strict" +import { generateKeyPairSync, randomUUID } from "node:crypto" +import { basename, dirname, join, resolve } from "node:path" +import { mkdtemp, readFile, realpath, writeFile } from "node:fs/promises" +assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") +const databaseUrl = new URL(process.env.DATABASE_URL ?? "") +assert.equal(databaseUrl.hostname, "127.0.0.1"); assert.equal(databaseUrl.pathname, "/postgres") +assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +const { pool, closePool } = await import("../db/index.js") +const { applyLegacyProbeFixtureMigrations: applySqlMigrations } = await import("../db/legacy-probe-fixture.js") +const { createSqlServerProbeJournal } = await import("./server-probe-journal.js") +const { createServerProbeStorageJournalAdapter } = await import("./server-probe-storage-journal-adapter.js") +const { connectProbeStorage } = await import("./server-probe-storage-connection.js") +const { createProbeJournalStorage, ProbeStorageUnknown } = await import("./server-probe-journal-maintenance.js") +const security = await import("../modules/mcp/security.js") +const cases: string[] = [] +type JournalContext = import("./server-probe-journal.js").ServerProbeJournalContext +type Receipt = import("./server-probe-execution.js").ServerProbeReceipt +let serverId: number, lose = "", revoked = false, failGc = false +const policy = { retentionDays: 30, globalBytes: 64 * 1048576, ownerBytes: 1048576, globalFiles: 100, ownerFiles: 2, globalAdmissions: 100, ownerAdmissions: 2 } +const readPolicy = async () => ({ ...policy, retentionDays: Number((await pool.query("SELECT requests_retention_days FROM mcp_settings WHERE id=1")).rows[0].requests_retention_days) }) +const connect = async (options: import("./server-probe-journal-maintenance.js").ProbeStorageOptions = { deadlineAt: Date.now() + 120000 }) => { + const client = await connectProbeStorage(options); let mutation = "" + return { release(destroy?: boolean) { client.release(destroy) }, async query(text: string, values?: unknown[], queryOptions?: { signal?: AbortSignal; timeoutMs: number }) { + if (text.includes("probe-storage:reserve")) mutation = "reserve" + if (text.includes("probe-journal:claim")) mutation = "claim" + if (text.includes("probe-storage:publish")) mutation = "publish" + if (text.includes("probe-storage:gc-admissions")) mutation = "gc" + const result = await client.query(text, values, queryOptions).catch((error: unknown) => { + const failure = error as { code?: string; constraint?: string } + console.error("Owned fixture SQL failure", { tag: text.match(/probe-storage:([a-z-]+)/)?.[1], code: failure.code, constraint: failure.constraint }) + throw error + }) + if (mutation === "gc" && text.includes("probe-storage:gc-admissions") && failGc) { failGc = false; throw Error("Owned GC fixture failure after actual DELETE before COMMIT") } + if (text === "COMMIT" && mutation && mutation === lose) { lose = ""; throw Error("Owned fixture lost actual storage COMMIT acknowledgement") } + return result + } } +} +async function fixture(ownerId = "owner-" + randomUUID(), at = Date.now(), legacy = false) { + const context: JournalContext = { identity: { kind: "rest", authentication: "jwt", ownerId, executionId: randomUUID(), requestId: "request-" + randomUUID(), issuedAt: at - 1000, expiresAt: at + 3600000 }, sourceServerId: serverId, tool: "ping" } + const binding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), ownerId, transport: "rest" as const, requestId: context.identity.kind === "rest" ? context.identity.requestId : "", issuedAt: context.identity.issuedAt, serverIds: [serverId], tool: "ping" as const } + const assertAuthority = async () => { assert(!revoked); assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=$1 AND enabled=true", [serverId])).rows[0].n, 1) } + const controller = new AbortController() + let clockOffset = 0, interruption = "" + const readCurrentPolicy = async () => { + if (interruption === "policy-unavailable") throw Error("Owned fixture settings unavailable") + const current = await readPolicy() + return interruption === "quota-drift" ? { ...current, ownerBytes: current.ownerBytes + 1 } : current + } + const options = { deadlineAt: Date.now() + 120000, signal: controller.signal } + const storageBoundary = legacy ? undefined : createServerProbeStorageJournalAdapter(context, { connect, assertAuthority, policy, readPolicy: readCurrentPolicy, options, now: () => Date.now() + clockOffset }) + const journal = createSqlServerProbeJournal(context, { assertAuthority, now: legacy ? () => at : undefined, connections: { connect }, storageBoundary }) + const receipt: Receipt = { receiptId: binding.receiptId, tool: "ping", serverId, state: "completed", httpStatus: 200, output: "owned synthetic measurement", data: { kind: "ping", value: "owned fixture" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false } + const stage = async () => { await journal.intent({ receiptId: binding.receiptId, kind: "native-ping", stageIndex: 1, timeoutMs: 1000 }); await journal.received({ receiptId: binding.receiptId, stageIndex: 1, outcome: "received" }) } + const complete = async () => { assert.equal(await journal.claim(binding), "claimed"); await stage(); await journal.finish(receipt) } + const fresh = () => createSqlServerProbeJournal(context, { assertAuthority, connections: { connect }, storageBoundary: createServerProbeStorageJournalAdapter(context, { connect, assertAuthority, policy, readPolicy, options: { deadlineAt: Date.now() + 120000 } }) }) + const interrupt = (kind: string) => { interruption = kind; if (kind === "abort") controller.abort(); if (kind === "deadline") clockOffset = 121000 } + return { context, binding, journal, stage, receipt, complete, fresh, interrupt } +} +try { + await applySqlMigrations(pool) + await pool.query("UPDATE traffic_settings SET enabled=false; UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false") + serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('owned-probe-storage','127.0.0.1',9,true) RETURNING id")).rows[0].id) + const legacy = await fixture("legacy-owner", Date.now(), true), expired = await fixture("expired-owner", Date.now() - 32 * 86400000, true) + const expiredDirect = await fixture("expired-direct-owner", Date.now() - 32 * 86400000, true) + const expiredLate = await fixture("expired-late-owner", Date.now() - 32 * 86400000, true) + const expiredLost = await fixture("expired-lost-owner", Date.now() - 32 * 86400000, true) + const expiredRollback = await fixture("expired-rollback-owner", Date.now() - 32 * 86400000, true) + await legacy.complete() + const legacySnapshot = await (await import("./server-probe-legacy-snapshot.sql-helper.js")).acceptLegacyProbeSnapshotSql(async () => { + await (await fixture("snapshot-concurrent-owner", Date.now(), true)).complete() + }) + console.log("Owned authenticated legacy snapshot:", JSON.stringify(legacySnapshot)) + const { provisionDatabaseSourceBindingOffline } = await import("../../scripts/database-source-binding-operator.js") + const fixtureRoot = await mkdtemp(join(await realpath(dirname(resolve(process.env.MCP_STATE_DIR!))), "source-binding-")) + const privateKeyFile = join(fixtureRoot, "issuer-private.pem") + const { privateKey } = generateKeyPairSync("ed25519") + await writeFile(privateKeyFile, privateKey.export({ format: "pem", type: "pkcs8" }), { mode: 0o600, flag: "wx" }) + const installationId = randomUUID(), sourceIdentityId = randomUUID() + const inspectionHost = await (await import("./server-probe-legacy-inspection.sql-helper.js")).acceptLegacyProbeInspectionSql(async source => { + const generation = randomUUID() + const issued = await provisionDatabaseSourceBindingOffline({ privateKeyFile, outputDirectory: join(fixtureRoot, generation), + request: { installationId, sourceIdentityId, generation, source } }) + return issued.deploymentPin + }) + console.log("Owned operator legacy inspection:", JSON.stringify(inspectionHost)) + const sourceObservation = await (await import("../db/database-source-observation.sql-helper.js")).acceptDatabaseSourceObservationSql() + console.log("Owned source database observation:", JSON.stringify(sourceObservation)) + // Seed an already elapsed synthetic legacy history, before conversion. SQL + // clocks remain real; no trigger is disabled and no immutable row is edited. + for (const prior of [expired, expiredDirect, expiredLate, expiredLost, expiredRollback]) { + const oldAt = new Date(prior.context.identity.issuedAt), oldExpiry = new Date(prior.context.identity.expiresAt) + const envelope = JSON.stringify({ version: 1, context: prior.context, bindingProof: prior.binding.bindingProof, receipt: prior.receipt }) + const ciphertext = await security.encryptSecret(envelope), proof = await security.privateSecretFingerprint(security.fingerprint(envelope), "server-probe-journal-v1:receipt") + await pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool,created_at) + VALUES($1,$2,'rest',$3,$4,'jwt',$5,$6,$7,$8,'ping',$9)`, [prior.binding.receiptId, prior.binding.bindingProof, prior.context.identity.executionId, prior.context.identity.ownerId, prior.binding.requestId, prior.context.identity.issuedAt, oldExpiry, serverId, oldAt]) + await pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,created_at,received_at) VALUES($1,1,'native-ping',1000,'received','received',$2,$2)", [prior.binding.receiptId, oldAt]) + await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_ciphertext=$2,receipt_proof=$3,finished_at=$4 WHERE receipt_id=$1", [prior.binding.receiptId, ciphertext, proof, oldAt]) + if (prior === expiredDirect) await pool.query("INSERT INTO server_probe_execution_quarantines(receipt_id,identity_kind,execution_id,binding_proof,identity_proof,error_code,attempted_stages,completed_stages,created_at) VALUES($1,'rest',$2,$3,$4,'JOURNAL_UNKNOWN',1,1,$5)", [prior.binding.receiptId, prior.context.identity.executionId, prior.binding.bindingProof, "c".repeat(64), oldAt]) + } + const cancelledFixtures: { receiptId: string; operationId: string; planId: string; ownerId: string; state: string }[] = [] + for (const state of ["failed", "running"]) { + const receiptId = randomUUID(), operationId = randomUUID(), planId = randomUUID(), connectionId = randomUUID(), credentialId = randomUUID(), ownerId = "gc-cancellation-" + state + const identity = { kind: "mcp" as const, ownerId, executionId: operationId, connectionId, credentialId, operationName: "servers.probes.run", issuedAt: expired.context.identity.issuedAt, expiresAt: expired.context.identity.expiresAt } + const until = new Date(Date.now() + 3600000), oldAt = new Date(identity.issuedAt), expiry = new Date(identity.expiresAt) + const currentPolicy = { id: connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: until.toISOString(), scope: { serverIds: [serverId], operationIds: ["servers.probes.run"], allowGlobal: false } } + await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until) VALUES($1,'owned GC fixture',$2,$3::jsonb,$4::jsonb,$5,$6)", [connectionId, ownerId, JSON.stringify(currentPolicy.permissions), JSON.stringify(currentPolicy), await security.accessGeneration(), until]) + await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), expiry]) + await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by) VALUES($1,$2,'servers.probes.run',1,1,$3,'{}','{}',$4,$5,$6)", [planId, connectionId, await security.accessGeneration(), state === "running" ? "executing" : "completed", expiry, ownerId]) + await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,created_at,finished_at) VALUES($1,$2,$3,$4,$5,$6,$7)", [operationId, planId, connectionId, randomUUID(), state, oldAt, state === "running" ? null : oldAt]) + const context: JournalContext = { identity, sourceServerId: serverId, tool: "ping" }, receipt: Receipt = { ...expired.receipt, receiptId } + const body = JSON.stringify({ version: 1, context, bindingProof: expired.binding.bindingProof, receipt }) + const cipher = await security.encryptSecret(body), proof = await security.privateSecretFingerprint(security.fingerprint(body), "server-probe-journal-v1:receipt") + await pool.query("INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,authority_issued_at,authority_expires_at,source_server_id,tool,created_at) VALUES($1,$2,'mcp',$3,$4,'mcp',$5,$6,$7,$8,$9,'ping',$10)", [receiptId, expired.binding.bindingProof, operationId, ownerId, connectionId, credentialId, identity.issuedAt, expiry, serverId, oldAt]) + await pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,created_at,received_at) VALUES($1,1,'native-ping',1000,'received','received',$2,$2)", [receiptId, oldAt]) + await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_ciphertext=$2,receipt_proof=$3,finished_at=$4 WHERE receipt_id=$1", [receiptId, cipher, proof, oldAt]) + await pool.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id,created_at) VALUES($1,$2,$3,$4)", [operationId, connectionId, ownerId, oldAt]) + await pool.query("INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,'synthetic.gc-preserve',$2,$3::jsonb)", [ownerId, connectionId, JSON.stringify({ operationId })]) + cancelledFixtures.push({ receiptId, operationId, planId, ownerId, state }) + } + const oldCiphertext = (await pool.query("SELECT receipt_ciphertext FROM server_probe_executions WHERE receipt_id=$1", [legacy.binding.receiptId])).rows[0].receipt_ciphertext + const client = await pool.connect() + try { await client.query(await readFile(new URL("../db/proposals/server-probe-storage-v2.sql", import.meta.url), "utf8")) } finally { client.release(true) } + assert.equal((await pool.query("SELECT receipt_ciphertext FROM server_probe_execution_payloads WHERE receipt_id=$1", [legacy.binding.receiptId])).rows[0].receipt_ciphertext, oldCiphertext) + await pool.query("INSERT INTO server_probe_execution_quarantines(receipt_id,identity_kind,execution_id,binding_proof,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,'rest',$2,$3,$4,'JOURNAL_UNKNOWN',1,1)", [expiredLate.binding.receiptId, expiredLate.context.identity.executionId, expiredLate.binding.bindingProof, "c".repeat(64)]) + assert.deepEqual(await legacy.fresh().read(legacy.binding.receiptId, legacy.binding.bindingProof), legacy.receipt) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM information_schema.columns WHERE table_name='server_probe_executions' AND column_name='receipt_ciphertext'")).rows[0].n, 0) + cases.push("actual proposal copies legacy ciphertext exactly before removing anchor payload and opt-in journal reads the converted receipt") + const normal = await fixture(); await normal.complete() + assert.deepEqual(await normal.fresh().read(normal.binding.receiptId, normal.binding.bindingProof), normal.receipt) + const anchor = (await pool.query("SELECT * FROM server_probe_executions WHERE receipt_id=$1", [normal.binding.receiptId])).rows[0] + assert.equal(anchor.state, "completed"); assert(anchor.receipt_proof); assert.equal(anchor.receipt_ciphertext, undefined) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_payloads WHERE receipt_id=$1", [normal.binding.receiptId])).rows[0].n, 1) + cases.push("real claim/stage/finish/read use quota admission and separate authenticated payload with proof-only immutable anchor") + const owner = "quota-owner-" + randomUUID(), first = await fixture(owner), second = await fixture(owner), third = await fixture(owner) + await first.journal.claim(first.binding); await second.journal.claim(second.binding) + await assert.rejects(third.journal.claim(third.binding)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE receipt_id=$1", [third.binding.receiptId])).rows[0].n, 0) + assert.equal(Number((await pool.query("SELECT admissions FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + owner])).rows[0].admissions), 2) + cases.push("actual owner quota refuses before anchor insertion with no extra charge") + for (const phase of ["reserve", "claim", "publish"] as const) { + const f = await fixture() + if (phase === "publish") { await f.journal.claim(f.binding); await f.stage() } + lose = phase + await assert.rejects(phase === "publish" ? f.journal.finish(f.receipt) : f.journal.claim(f.binding)) + const fresh = f.fresh(); await assert.rejects(fresh.claim(f.binding)) + revoked = true + await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: phase === "reserve" ? 0 : 1, completedStages: phase === "publish" ? 1 : 0 }) + revoked = false + const observed = await f.fresh().read(f.binding.receiptId, f.binding.bindingProof) + assert.equal(observed?.state, "unknown") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_storage_admissions WHERE receipt_id=$1", [f.binding.receiptId])).rows[0].n, 1) + cases.push("actual " + phase + " COMMIT ACK loss retains spent admission and quarantine dominates even after authority revocation") + } + for (const interruption of ["abort", "deadline", "retention-drift", "quota-drift", "policy-unavailable"]) { + const f = await fixture(); await f.journal.claim(f.binding); await f.stage() + lose = "publish"; await assert.rejects(f.journal.finish(f.receipt), ProbeStorageUnknown) + const snapshot = async () => (await pool.query(`SELECT row_to_json(e) AS anchor, + (SELECT row_to_json(p) FROM server_probe_execution_payloads p WHERE p.receipt_id=e.receipt_id) AS payload, + (SELECT json_agg(s ORDER BY stage_index) FROM server_probe_execution_stages s WHERE s.receipt_id=e.receipt_id) AS stages + FROM server_probe_executions e WHERE receipt_id=$1`, [f.binding.receiptId])).rows[0] + const before = await snapshot(); assert.equal(before.anchor.state, "completed"); assert(before.payload) + f.interrupt(interruption); revoked = true + if (interruption === "retention-drift") await pool.query("UPDATE mcp_settings SET requests_retention_days=31 WHERE id=1") + try { + await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 1, completedStages: 1 }) + await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 1, completedStages: 1 }) + } finally { + revoked = false + if (interruption === "retention-drift") await pool.query("UPDATE mcp_settings SET requests_retention_days=30 WHERE id=1") + } + assert.deepEqual(await snapshot(), before) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE receipt_id=$1", [f.binding.receiptId])).rows[0].n, 1) + assert.equal((await f.fresh().read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown") + await assert.rejects(f.fresh().claim(f.binding)) + cases.push("actual publish COMMIT ACK loss followed by " + interruption + " preserves exact terminal/payload/stages and fresh read returns only quarantined unknown") + } + const storage = createProbeJournalStorage({ connect, policy, safetyWindowMs: 60000, authorize: async () => "c".repeat(64) }) + const purged = await storage.purge(expired.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 }) + assert.equal(purged.purged, 1) + const stored = await storage.read({ ownerId: expired.context.identity.ownerId, receiptId: expired.binding.receiptId, identityKind: "rest", executionId: expired.context.identity.executionId, bindingProof: expired.binding.bindingProof, authorityExpiresAt: new Date(expired.context.identity.expiresAt).toISOString() }, { deadlineAt: Date.now() + 120000 }) + assert.equal(stored.kind, "expired") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE receipt_id=$1", [expired.binding.receiptId])).rows[0].n, 1) + assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + expired.context.identity.ownerId])).rows[0].reserved_bytes), 32768) + cases.push("actual expired payload purge releases only payload capacity; immutable attempt metadata and permanent expired tombstone survive") + const gcCheck = await pool.connect() + try { + await gcCheck.query("BEGIN") + assert((await gcCheck.query("SELECT proconfig FROM pg_proc WHERE proname='server_probe_storage_gc_allowed'")).rows[0].proconfig.includes("TimeZone=UTC")) + assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [expired.binding.receiptId])).rows[0].allowed, false) + await gcCheck.query("SET LOCAL mm.probe_storage_gc='on'") + assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [expired.binding.receiptId])).rows[0].allowed, true) + assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [normal.binding.receiptId])).rows[0].allowed, false) + await gcCheck.query("UPDATE mcp_settings SET requests_retention_days=365 WHERE id=1") + assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [expired.binding.receiptId])).rows[0].allowed, false) + } finally { await gcCheck.query("ROLLBACK"); gcCheck.release(true) } + cases.push("actual GC predicate requires explicit mode, persisted expiry and current retention; fresh admission remains protected") + for (const table of ["server_probe_executions", "server_probe_execution_stages", "server_probe_storage_admissions"]) { + await assert.rejects(pool.query(`DELETE FROM ${table} WHERE receipt_id=$1`, [expired.binding.receiptId])) + } + cases.push("actual SQL rejects metadata DELETE without validated GC mode even for expired rows") + assert.equal((await storage.collectExpired(expired.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1) + for (const table of ["server_probe_executions", "server_probe_execution_stages", "server_probe_storage_admissions"]) assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table} WHERE receipt_id=$1`, [expired.binding.receiptId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + expired.context.identity.ownerId])).rows[0].n, 0) + await assert.rejects(storage.claim({ ownerId: expired.context.identity.ownerId, receiptId: expired.binding.receiptId, identityKind: "rest", executionId: expired.context.identity.executionId, bindingProof: expired.binding.bindingProof, authorityExpiresAt: new Date(expired.context.identity.expiresAt).toISOString() }, { deadlineAt: Date.now() + 120000 }, async () => assert.fail("Expired original identity cannot execute after GC"))) + cases.push("actual GC removes expired spent metadata and refunds remaining reservation once; original expired identity cannot replay") + assert.equal((await storage.collectExpired(expiredDirect.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_payloads WHERE receipt_id=$1", [expiredDirect.binding.receiptId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE receipt_id=$1", [expiredDirect.binding.receiptId])).rows[0].n, 0) + assert.equal((await storage.collectExpired(expiredDirect.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0) + assert.equal((await storage.collectExpired(normal.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0) + cases.push("actual GC also removes unpurged expired ciphertext atomically; repeat and fresh owner batches change nothing") + assert.equal((await storage.collectExpired(expiredLate.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE receipt_id=$1", [expiredLate.binding.receiptId])).rows[0].n, 1) + cases.push("actual recent quarantine protects old original identity from GC until last-activity retention elapses") + failGc = true + await assert.rejects(storage.collectExpired(expiredRollback.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })) + for (const table of ["server_probe_executions", "server_probe_execution_stages", "server_probe_execution_payloads", "server_probe_storage_admissions"]) assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table} WHERE receipt_id=$1`, [expiredRollback.binding.receiptId])).rows[0].n, 1) + assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + expiredRollback.context.identity.ownerId])).rows[0].reserved_bytes), 294912) + cases.push("actual error after GC DELETE rolls back all metadata and capacity changes atomically") + lose = "gc" + await assert.rejects(storage.collectExpired(expiredLost.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 }), ProbeStorageUnknown) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_storage_admissions WHERE receipt_id=$1", [expiredLost.binding.receiptId])).rows[0].n, 0) + assert.equal((await storage.collectExpired(expiredLost.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0) + cases.push("actual lost GC COMMIT ACK reports unknown without replay; independent reconciliation sees the completed deletion") + const terminal = cancelledFixtures.find(row => row.state === "failed")!, running = cancelledFixtures.find(row => row.state === "running")! + assert.equal((await storage.collectExpiredCancellations(running.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0) + assert.equal((await storage.collectExpired(running.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0) + cases.push("actual expired original identity cannot permit GC while its MCP operation is still running") + assert.equal((await storage.collectExpiredCancellations(terminal.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1) + assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + terminal.ownerId])).rows[0].reserved_bytes), 294912) + assert.equal((await storage.collectExpired(terminal.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [terminal.operationId])).rows[0].n, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE id=$1", [terminal.operationId])).rows[0].n, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE id=$1", [terminal.planId])).rows[0].n, 1) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE payload->>'operationId'=$1", [terminal.operationId])).rows[0].n, 1) + cases.push("actual terminal cancellation GC refunds no duplicate quota; admission GC preserves plans, operations and general audit") + const cancelledScope = { ownerId: "owned-cancel-quota", receiptId: randomUUID(), identityKind: "rest" as const, executionId: randomUUID(), bindingProof: "d".repeat(64), authorityExpiresAt: new Date(Date.now() + 3600000).toISOString() } + const cancel = (bound = cancelledScope) => storage.cancel(bound.ownerId, { deadlineAt: Date.now() + 120000 }, async sql => { await sql.query("SELECT 1 /* owned synthetic cancellation source */"); return bound }, async sql => { await sql.query("SELECT 1 /* owned cancellation acknowledgement */"); return bound.receiptId }) + assert.equal(await cancel(), cancelledScope.receiptId) + const cancelled = (await pool.query("SELECT * FROM server_probe_storage_admissions WHERE receipt_id=$1", [cancelledScope.receiptId])).rows[0] + assert.equal(cancelled.claim_token, null); assert(cancelled.cancelled_at); assert(Number.isFinite(new Date(cancelled.gc_not_before).getTime())) + await cancel({ ...cancelledScope, receiptId: randomUUID(), executionId: randomUUID() }) + assert.equal(await cancel(), cancelledScope.receiptId) + assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + cancelledScope.ownerId])).rows[0].reserved_bytes), 2 * 294912) + await assert.rejects(cancel({ ...cancelledScope, receiptId: randomUUID(), executionId: randomUUID() })) + await assert.rejects(storage.claim(cancelledScope, { deadlineAt: Date.now() + 120000 }, async () => assert.fail("Cancelled identity cannot acquire anchor"))) + await assert.rejects(storage.publish(cancelledScope, "owned synthetic ciphertext", "a".repeat(64), { deadlineAt: Date.now() + 120000 }, async () => assert.fail("Cancelled identity cannot publish"))) + cases.push("actual preclaim cancellation reserves shared quota once with finite GC lifetime; duplicate at quota limit succeeds while claim and publish remain refused") + const sourcePrerequisites = await (await import("../modules/mcp/diagnostic-cancellation-source.sql-helper.js")).acceptDiagnosticCancellationSourceSql(serverId) + console.log("Owned cancellation source prerequisites:", JSON.stringify(sourcePrerequisites)) + console.log("Owned storage source mapping:", JSON.stringify(await (await import("./server-probe-storage-source.sql-helper.js")).acceptProbeStorageSourceSql(serverId))) + console.log("Owned bounded storage connection:", JSON.stringify(await (await import("./server-probe-storage-connection.sql-helper.js")).acceptProbeStorageConnectionSql())) + // Only the current MCP runtime subcase needs the replay fence. Keep the + // earlier legacy inspection and conversion fixtures on their historical schema. + const { analyzeMigration, MIGRATION_MANIFEST } = await import("../db/migration-manifest.js") + const replayPin = MIGRATION_MANIFEST.find(pin => pin.file === "0045_mcp_operation_replay_tombstones.sql") + assert(replayPin) + const replayMigration = await readFile(new URL("../../drizzle/" + replayPin.file, import.meta.url)) + analyzeMigration(replayPin.file, replayMigration, replayPin) + const runtimeFixtureClient = await pool.connect() + try { await runtimeFixtureClient.query(replayMigration.toString("utf8")) } finally { runtimeFixtureClient.release(true) } + console.log("Owned storage probe runtime host:", JSON.stringify(await (await import("../modules/mcp/server-probe-storage-host.sql-helper.js")).acceptServerProbeStorageHostSql())) + console.log("Owned storage REST host:", JSON.stringify(await (await import("./server-probe-storage-rest.sql-helper.js")).acceptServerProbeStorageRestSql())) + console.log("Owned bounded metadata cancellation:", JSON.stringify(await (await import("./server-probe-storage-metadata.sql-helper.js")).acceptProbeStorageMetadataSql())) + console.log("Owned retention worker:", JSON.stringify(await (await import("./server-probe-retention.sql-helper.js")).acceptServerProbeRetentionSql())) + console.log("Owned probe storage journal SQL:", JSON.stringify({ cases: cases.length, names: cases, actualSql: true, actualCommitAckLosses: 8, productionCrypto: true, syntheticRestIdentity: true, nativeOrDnsCalls: 0, metadataGcRun: "owned synthetic database only", productionDdlApplied: false, routesRegistered: false, historicalNewCredentialReaderIntegrated: false })) +} finally { revoked = false; await closePool() } diff --git a/backend/src/services/server-probe-storage-metadata.sql-helper.ts b/backend/src/services/server-probe-storage-metadata.sql-helper.ts new file mode 100644 index 00000000..a6ffd8bf --- /dev/null +++ b/backend/src/services/server-probe-storage-metadata.sql-helper.ts @@ -0,0 +1,168 @@ +/** Disposable owned PostgreSQL only: cancellation while real source metadata is + * blocked, before any native dispatch or durable admission. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { realpath } from "node:fs/promises" +import { basename, dirname, resolve } from "node:path" + +export async function acceptProbeStorageMetadataSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") + assert(["postgres:", "postgresql:"].includes(url.protocol)); assert.equal(url.hostname, "127.0.0.1") + assert.equal(decodeURIComponent(url.username), "mcp_test"); assert.equal(url.pathname, "/postgres") + assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres"); assert.equal(url.search, ""); assert.equal(url.hash, "") + assert(process.env.MCP_STATE_DIR) + const state = resolve(process.env.MCP_STATE_DIR) + assert.equal(basename(state), "state"); assert.match(basename(dirname(state)), /^mm-mcp-test-[A-Za-z0-9-]+$/) + assert.equal(resolve(await realpath(state)).toLowerCase(), state.toLowerCase()) + // All application/driver imports follow the owned database and state guards. + const { pool } = await import("../db/index.js") + const { createStorageServerProbeRestExecution } = await import("./server-probe-storage-rest.js") + const { default: Fastify } = await import("fastify") + const { validatorCompiler, serializerCompiler } = await import("@fastify/type-provider-zod") + const { SignJWT } = await import("jose") + const { default: authPlugin, requireAuth } = await import("../plugins/auth.js") + const { createStorageProbeRoutes } = await import("../routes/probe-storage-handler.js") + const { env } = await import("../config.js") + assert.equal(env.authRequired, true) + const wait = async (predicate: () => Promise, message: string, budget = 15000) => { + const deadline = Date.now() + budget + while (!await predicate()) { + assert(Date.now() < deadline, message) + await new Promise(yes => setTimeout(yes, 20)) + } + } + let nativeCalls = 0, sourceId: number | undefined + const router = createServer((_request, response) => { + nativeCalls++; response.statusCode = 500; response.end('{"error":"Unexpected owned metadata native dispatch"}') + }) + await new Promise((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) }) + const controller = new AbortController(), requestId = randomUUID() + const httpController = new AbortController() + let httpApp: import("fastify").FastifyInstance | undefined + const locker = await pool.connect() + let locked = false, backendPid: number | undefined + try { + assert.equal((await pool.query("SELECT to_regclass('server_probe_storage_admissions') IS NOT NULL AS ready")).rows[0].ready, true) + sourceId = Number((await pool.query(`INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) + VALUES($1,'127.0.0.1',$2,false,false,true,'owned-metadata-user','owned-metadata-password') RETURNING id`, + ["owned-metadata-" + randomUUID(), (router.address() as { port: number }).port])).rows[0].id) + const before = Number((await pool.query("SELECT count(*) AS n FROM server_probe_storage_admissions")).rows[0].n) + const lockerPid = Number((await locker.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + await locker.query("BEGIN"); locked = true + await locker.query("SET LOCAL idle_in_transaction_session_timeout='20000ms'") + await locker.query("LOCK TABLE servers IN ACCESS EXCLUSIVE MODE") + const run = createStorageServerProbeRestExecution() + const pending = run(String(sourceId), { tool: "ping", target: "127.0.0.1", pingCount: 1 }, { + actor: undefined, authRequired: false, requestId, acceptedAt: Date.now(), signal: controller.signal, + }) + // Observe the actual production SELECT waiting on our relation lock. The + // observer never reads query parameters, credentials or provider output. + await wait(async () => { + const rows = (await pool.query(`SELECT pid FROM pg_stat_activity + WHERE datname=current_database() AND usename=current_user AND wait_event_type='Lock' + AND wait_event='relation' AND $1::int=ANY(pg_blocking_pids(pid)) + AND query LIKE 'SELECT id FROM servers WHERE id=$1 LIMIT 2%'`, [lockerPid])).rows + assert(rows.length <= 1, "More than one owned metadata SELECT is blocked") + if (rows.length) backendPid = Number(rows[0].pid) + return rows.length === 1 + }, "Production source metadata did not reach the owned relation lock") + const abortedAt = Date.now(); controller.abort() + let timer: ReturnType | undefined + try { + const result = await Promise.race([pending, new Promise((_yes, no) => { + timer = setTimeout(() => no(Error("Metadata cancellation did not settle while table lock remained held")), 2000) + })]) + assert(result.statusCode >= 400); assert.deepEqual(Object.keys(result.body), ["error"]) + assert.equal(JSON.stringify(result).includes("owned-metadata-password"), false) + } finally { if (timer) clearTimeout(timer) } + const cancellationElapsedMs = Date.now() - abortedAt + assert(cancellationElapsedMs < 2000) + // Prove the caller settled before we release the conflicting table lock. + assert.equal(Number((await locker.query(`SELECT count(*) AS n FROM pg_locks + WHERE pid=pg_backend_pid() AND relation='servers'::regclass AND mode='AccessExclusiveLock' AND granted`)).rows[0].n), 1) + assert.equal(nativeCalls, 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_storage_admissions")).rows[0].n), before) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_executions WHERE request_id=$1", [requestId])).rows[0].n), 0) + await locker.query("ROLLBACK"); locked = false + // Closing the driver connection bounds the caller immediately; PostgreSQL + // may observe that disconnect only after the relation lock is released. + await wait(async () => Number((await pool.query("SELECT count(*) AS n FROM pg_stat_activity WHERE pid=$1", [backendPid])).rows[0].n) === 0, + "Cancelled metadata backend survived relation lock release") + assert.equal(nativeCalls, 0) + + httpApp = Fastify({ logger: false }) + httpApp.setValidatorCompiler(validatorCompiler); httpApp.setSerializerCompiler(serializerCompiler) + await httpApp.register(authPlugin); httpApp.addHook("onRequest", requireAuth) + let handlerFinished!: () => void, handlerCalls = 0 + const handlerSettled = new Promise(yes => { handlerFinished = yes }) + // Fixture observation only: delegate the real handler with its original + // receiver/arguments; no executor, authority, connector or result override. + httpApp.addHook("onRoute", options => { + const original = options.handler + options.handler = async function (request, reply) { + handlerCalls++ + try { return await original.call(this, request, reply) } + finally { handlerFinished() } + } + }) + await httpApp.register(createStorageProbeRoutes(), { prefix: "/api" }) + const base = await httpApp.listen({ host: "127.0.0.1", port: 0 }) + const httpOwner = "owned-metadata-http-" + randomUUID() + const jwt = await new SignJWT({ sub: httpOwner, apps: ["mm"], permissions: ["mm:servers:write"] }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setIssuedAt().setExpirationTime("1h") + .sign(new TextEncoder().encode(env.jwtSecret)) + await locker.query("BEGIN"); locked = true + await locker.query("SET LOCAL idle_in_transaction_session_timeout='20000ms'") + await locker.query("LOCK TABLE servers IN ACCESS EXCLUSIVE MODE") + const httpPending = fetch(`${base}/api/servers/${sourceId}/probes/run`, { method: "POST", signal: httpController.signal, + headers: { "content-type": "application/json", authorization: "Bearer " + jwt }, + body: JSON.stringify({ tool: "ping", target: "127.0.0.1", pingCount: 1 }) }).then( + async response => { await response.body?.cancel(); return false }, () => true) + backendPid = undefined + await wait(async () => { + const rows = (await pool.query(`SELECT pid FROM pg_stat_activity + WHERE datname=current_database() AND usename=current_user AND wait_event_type='Lock' + AND wait_event='relation' AND $1::int=ANY(pg_blocking_pids(pid)) + AND query LIKE 'SELECT id FROM servers WHERE id=$1 LIMIT 2%'`, [lockerPid])).rows + assert(rows.length <= 1, "More than one owned HTTP metadata SELECT is blocked") + if (rows.length) backendPid = Number(rows[0].pid) + return rows.length === 1 + }, "HTTP metadata did not reach the owned relation lock") + const disconnectedAt = Date.now(); httpController.abort() + let httpTimer: ReturnType | undefined + try { + await Promise.race([handlerSettled, new Promise((_yes, no) => { + httpTimer = setTimeout(() => no(Error("HTTP handler did not settle while metadata table lock remained held")), 2000) + })]) + } finally { if (httpTimer) clearTimeout(httpTimer) } + const httpCancellationElapsedMs = Date.now() - disconnectedAt + assert(httpCancellationElapsedMs < 2000); assert.equal(await httpPending, true); assert.equal(handlerCalls, 1) + assert.equal(Number((await locker.query(`SELECT count(*) AS n FROM pg_locks + WHERE pid=pg_backend_pid() AND relation='servers'::regclass AND mode='AccessExclusiveLock' AND granted`)).rows[0].n), 1) + assert.equal(nativeCalls, 0) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_storage_admissions")).rows[0].n), before) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_executions WHERE actor_id=$1", [httpOwner])).rows[0].n), 0) + await locker.query("ROLLBACK"); locked = false + await wait(async () => Number((await pool.query("SELECT count(*) AS n FROM pg_stat_activity WHERE pid=$1", [backendPid])).rows[0].n) === 0, + "HTTP cancelled metadata backend survived relation lock release") + assert.equal(nativeCalls, 0) + return { actualProductionRestMetadata: true, observedRelationLock: true, callerCancelledWhileLockHeld: true, + cancellationElapsedMs, cancelledBackendGoneAfterUnlock: true, noAdmissionBeforeClaim: true, + localServerOwnedAuthority: true, nativeOrDnsCalls: nativeCalls, httpDisconnectMetadataClaimed: true, + actualHttpSignedJwt: true, handlerSettledWhileLockHeld: true, httpCancellationElapsedMs, routesRegisteredInFixtureOnly: true, + productionDdlApplied: false, routesRegistered: false } + } finally { + controller.abort(); httpController.abort() + try { if (locked) await locker.query("ROLLBACK") } + finally { + locker.release() + try { if (sourceId !== undefined) await pool.query("DELETE FROM servers WHERE id=$1", [sourceId]) } + finally { + try { await httpApp?.close() } + finally { router.closeAllConnections(); await new Promise(yes => router.close(() => yes())) } + } + } + } +} diff --git a/backend/src/services/server-probe-storage-metadata.test.ts b/backend/src/services/server-probe-storage-metadata.test.ts new file mode 100644 index 00000000..7dc240bb --- /dev/null +++ b/backend/src/services/server-probe-storage-metadata.test.ts @@ -0,0 +1,91 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { pool } from "../db/index.js" +import { createProbeStorageHostIo } from "./server-probe-storage-host-io.js" +import { PROBE_STORAGE_LIMITS } from "../modules/mcp/diagnostic-cancellation-storage.js" +import { ProbeStorageUnknown } from "./server-probe-journal-maintenance.js" + +type Result = { rows: Record[]; command: string } +function deferred() { + let resolve!: (value: T) => void + const promise = new Promise(yes => { resolve = yes }) + return { promise, resolve } +} +async function fixture(work: (value: { io: ReturnType; queries: { query_timeout: number }[]; releases: boolean[]; checkouts: () => number }) => Promise, driver?: () => Promise) { + const originalConnect = pool.connect, originalQuery = pool.query + const queries: { query_timeout: number }[] = [], releases: boolean[] = []; let checkouts = 0 + pool.query = (() => assert.fail("No unbounded pool.query")) as typeof pool.query + pool.connect = (async () => { + checkouts++ + return { async query(config: { query_timeout: number }) { queries.push(config); return driver ? driver() : { rows: [{ value: 1 }], command: "SELECT" } }, release(unsafe?: boolean) { releases.push(!!unsafe) } } + }) as unknown as typeof pool.connect + try { await work({ io: createProbeStorageHostIo(PROBE_STORAGE_LIMITS, "rest"), queries, releases, checkouts: () => checkouts }) } + finally { pool.connect = originalConnect; pool.query = originalQuery } +} + +test("metadata groups share one bounded backend and copy the request budget", async () => { + await fixture(async ({ io, queries, releases, checkouts }) => { + const options = { deadlineAt: Date.now() + 1000 } + await io.runRequest(options, async () => { + options.deadlineAt += 300000 + await io.withClient(async client => { + await client.query("SELECT owner"); await io.query("SELECT source"); await client.query("SELECT settings") + }) + }) + assert.equal(checkouts(), 1); assert.equal(queries.length, 3); assert.deepEqual(releases, [false]) + assert(queries.every(value => value.query_timeout >= 1 && value.query_timeout <= 1000)) + }) +}) +test("request abort destroys an in-flight metadata backend and absorbs the late driver value", async () => { + const response = deferred(), started = deferred() + await fixture(async ({ io, releases, queries }) => { + const controller = new AbortController() + const pending = io.runRequest({ deadlineAt: Date.now() + 10000, signal: controller.signal }, () => io.query("SELECT blocked")) + const rejected = assert.rejects(pending, ProbeStorageUnknown) + await started.promise; controller.abort(); await rejected + assert.deepEqual(releases, [true]); assert.equal(queries.length, 1) + response.resolve({ rows: [{ value: 1 }], command: "SELECT" }); await new Promise(resolve => setImmediate(resolve)) + assert.deepEqual(releases, [true]); assert.equal(queries.length, 1) + }, () => { started.resolve(); return response.promise }) +}) +test("abort also bounds a callback waiting between SQL statements and closes its late facade", async () => { + const entered = deferred(), gate = deferred(), lateDone = deferred() + await fixture(async ({ io, queries, releases }) => { + const controller = new AbortController() + const pending = io.runRequest({ deadlineAt: Date.now() + 10000, signal: controller.signal }, () => io.withClient(async client => { + entered.resolve(); await gate.promise + await assert.rejects(client.query("SELECT after-callback-abort"), { code: "STATE_CONFLICT" }); lateDone.resolve() + })) + const rejected = assert.rejects(pending, ProbeStorageUnknown) + await entered.promise; controller.abort(); await rejected + assert.deepEqual(releases, [true]); assert.equal(queries.length, 0) + gate.resolve(); await lateDone.promise + assert.deepEqual(releases, [true]); assert.equal(queries.length, 0) + }) +}) +for (const kind of ["owned", "borrowed"] as const) test("closed ancestor refuses a detached " + kind + " raw client before SQL", async () => { + await fixture(async ({ io, queries, releases, checkouts }) => { + const gate = deferred(), entered = deferred(), options = { deadlineAt: Date.now() + 10000 } + let rejected: Promise | undefined, borrowedQueries = 0 + await io.runRequest(options, async () => { + const work = async () => { + const client = io.currentClient()!; entered.resolve(); await gate.promise + return client.query("SELECT forbidden-late") + } + const child = io.runRequest(options, () => kind === "owned" ? io.withClient(work) : io.withBorrowed({ query: (async () => { borrowedQueries++; return { rows: [] } }) as never }, work)) + rejected = assert.rejects(child, { code: "STATE_CONFLICT" }) + await entered.promise + }) + gate.resolve(); await rejected + assert.equal(queries.length, 0); assert.equal(borrowedQueries, 0) + assert.equal(checkouts(), kind === "owned" ? 1 : 0); assert.deepEqual(releases, kind === "owned" ? [false] : []) + }) +}) +test("a request cannot open a nested operation while a physical lease is held", async () => { + await fixture(async ({ io, releases, checkouts }) => { + await io.withClient(async () => { + await assert.rejects(io.runRequest({ deadlineAt: Date.now() + 1 }, async () => 1), { code: "STATE_CONFLICT" }) + }) + assert.equal(checkouts(), 1); assert.deepEqual(releases, [false]) + }) +}) diff --git a/backend/src/services/server-probe-storage-rest.sql-helper.ts b/backend/src/services/server-probe-storage-rest.sql-helper.ts new file mode 100644 index 00000000..3c7b8efe --- /dev/null +++ b/backend/src/services/server-probe-storage-rest.sql-helper.ts @@ -0,0 +1,227 @@ +/** Owned disposable PG + loopback HTTP acceptance of the real REST assembly. + * Existing routes, production schema and MCP registration are untouched. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { basename, dirname, resolve } from "node:path" +import { realpath } from "node:fs/promises" +import type { AuthUser } from "../lib/permissions.js" + +export async function acceptServerProbeStorageRestSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") + assert(["postgres:", "postgresql:"].includes(url.protocol)); assert.equal(url.hostname, "127.0.0.1") + assert.equal(decodeURIComponent(url.username), "mcp_test"); assert.equal(url.pathname, "/postgres") + assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres"); assert.equal(url.search, ""); assert.equal(url.hash, "") + assert(process.env.MCP_STATE_DIR) + const state = resolve(process.env.MCP_STATE_DIR) + assert.equal(basename(state), "state"); assert.match(basename(dirname(state)), /^mm-mcp-test-[A-Za-z0-9-]+$/) + assert.equal(resolve(await realpath(state)).toLowerCase(), state.toLowerCase()) + const { pool } = await import("../db/index.js") + const { createStorageServerProbeRestExecution } = await import("./server-probe-storage-rest.js") + const { PROBE_STORAGE_LIMITS } = await import("../modules/mcp/diagnostic-cancellation-storage.js") + const { decryptSecret } = await import("../modules/mcp/security.js") + const { buildApp } = await import("../index.js") + const { SignJWT } = await import("jose") + const { env } = await import("../config.js") + assert.equal(env.authRequired, true) + assert.equal((await pool.query("SELECT to_regclass('server_probe_storage_admissions') IS NOT NULL AS ready")).rows[0].ready, true) + const priorSettings = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0]; assert(priorSettings) + const actor: AuthUser = { id: "owned-rest-storage-" + randomUUID(), email: "fixture@example.test", name: "Owned REST", apps: ["mm"], + permissions: ["mm:servers:write"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 3600000 } + const sourceUser = "NEVER_REST_SOURCE_USER", sourcePassword = "NEVER_REST_SOURCE_PASSWORD" + const peerUser = "NEVER_REST_PEER_USER", peerPassword = "NEVER_REST_PEER_PASSWORD" + const privateValues = [sourceUser, sourcePassword, peerUser, peerPassword] + const hidden = (value: unknown) => assert.equal(privateValues.some(secret => JSON.stringify(value).includes(secret)), false, "Private REST diagnostic material leaked") + const serverIds: number[] = [], peerHost = "owned-peer-" + randomUUID() + ".invalid" + let nativeCalls = 0, protocolErrors = 0, mode: "normal" | "error" | "hold" = "normal", activeAbort: AbortController | undefined + let heldStarted!: () => void, heldClosed!: () => void, releaseHeld!: () => void + let started!: Promise, closed!: Promise, heldGate!: Promise + const resetHeld = () => { + started = new Promise(yes => { heldStarted = yes }); closed = new Promise(yes => { heldClosed = yes }) + heldGate = new Promise(yes => { releaseHeld = yes }) + } + resetHeld() + let httpApp: import("fastify").FastifyInstance | undefined + const barrier = async (pending: Promise, message: string) => { + let timer: ReturnType | undefined + try { await Promise.race([pending, new Promise((_yes, no) => { timer = setTimeout(() => no(Error(message)), 15000) })]) } + finally { if (timer) clearTimeout(timer) } + } + const router = createServer(async (request, response) => { + response.setHeader("content-type", "application/json") + try { + nativeCalls++ + const selectedMode = mode + assert.equal(request.socket.remoteAddress, "127.0.0.1"); assert.equal(request.method, "POST") + assert.equal(request.headers.authorization === "Basic " + Buffer.from(`${sourceUser}:${sourcePassword}`).toString("base64"), true) + let raw = ""; for await (const chunk of request) raw += chunk + const body = JSON.parse(raw) as Record + if (request.url === "/rest/tool/bandwidth-test") { + assert.equal(body.address, peerHost); assert.equal(body.user === peerUser && body.password === peerPassword, true) + assert.equal(body.duration, "3s"); assert.equal(body.protocol, "tcp"); assert.equal(body.direction, "both") + response.end(JSON.stringify([{ "tx-current": "0", "rx-current": "42", "test-duration": "3s" }])); return + } + assert.equal(request.url, "/rest/tool/ping"); assert.equal(body.address, "127.0.0.1"); assert.equal(body.count, "1") + if (selectedMode === "error") { + response.statusCode = 500; response.end(JSON.stringify({ error: `${sourceUser}:${sourcePassword}:${peerUser}:${peerPassword}` })); return + } + if (selectedMode === "hold") { response.once("close", heldClosed); heldStarted(); await heldGate; if (response.destroyed) return } + response.end(JSON.stringify([{ seq: "0", ttl: "64", time: "1ms", status: `${sourceUser}:${sourcePassword}` }, { sent: "1", received: "1", "packet-loss": "0%" }])) + } catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned REST loopback protocol mismatch"}') } + }) + await new Promise((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) }) + const execution = createStorageServerProbeRestExecution() + const context = (kind: string, selectedActor: AuthUser | undefined = actor, authRequired = true, signal?: AbortSignal) => ({ actor: selectedActor, authRequired, signal, requestId: "owned-rest-" + kind + "-" + randomUUID(), acceptedAt: Date.now() }) + const ping = { tool: "ping", target: "127.0.0.1", pingCount: 1 } + const usage = async (owner: string) => { + const row = (await pool.query("SELECT admissions,reserved_files,reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + owner])).rows[0] + return { admissions: Number(row?.admissions ?? 0), files: Number(row?.reserved_files ?? 0), bytes: Number(row?.reserved_bytes ?? 0) } + } + const archive = async (requestId: string) => (await pool.query(`SELECT j.*,to_jsonb(j) ? 'receipt_ciphertext' AS anchor_has_ciphertext, + a.owner_id,a.claim_token,a.claim_attempted_at,p.receipt_ciphertext,p.receipt_proof AS payload_proof, + (SELECT count(*)::int FROM server_probe_execution_quarantines q WHERE q.receipt_id=j.receipt_id AND q.execution_id=j.execution_id) AS quarantines + FROM server_probe_executions j JOIN server_probe_storage_admissions a USING(receipt_id) + LEFT JOIN server_probe_execution_payloads p USING(receipt_id) WHERE j.request_id=$1`, [requestId])).rows + const completed = async (requestId: string, ownerId: string, authentication: "jwt" | "local") => { + const rows = await archive(requestId); assert.equal(rows.length, 1) + const row = rows[0]; assert.equal(row.identity_kind, "rest"); assert.equal(row.authentication, authentication) + assert.equal(row.actor_id, ownerId); assert.equal(row.owner_id, ownerId); assert.equal(row.mcp_connection_id, null); assert.equal(row.mcp_credential_id, null) + assert.equal(row.state, "completed"); assert.equal(row.anchor_has_ciphertext, false); assert.equal(row.claim_token, null); assert(row.claim_attempted_at) + assert.equal(row.quarantines, 0); assert.equal(typeof row.receipt_ciphertext, "string"); assert.equal(row.payload_proof === row.receipt_proof, true) + hidden(JSON.parse(await decryptSecret(row.receipt_ciphertext))) + return row + } + const success = (reply: Awaited>) => { hidden(reply); assert.equal(reply.statusCode, 200); assert.deepEqual(Object.keys(reply.body), ["output"]) } + try { + await pool.query("UPDATE mcp_settings SET enabled=false WHERE id=1") + const sourceId = Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES('owned-rest-source','127.0.0.1',$1,false,false,true,$2,$3) RETURNING id", [(router.address() as { port: number }).port, sourceUser, sourcePassword])).rows[0].id) + serverIds.push(sourceId) + const peerId = Number((await pool.query("INSERT INTO servers(name,host,enabled,username,password) VALUES('owned-rest-peer',$1,false,$2,$3) RETURNING id", [peerHost, peerUser, peerPassword])).rows[0].id) + serverIds.push(peerId) + const inactive = (await pool.query("INSERT INTO servers(name,host,enabled) SELECT $1||n::text,'198.51.100.254',false FROM generate_series(1,1001) n RETURNING id", ["owned-rest-inactive-" + randomUUID() + "-"])).rows + serverIds.push(...inactive.map(row => Number(row.id))); assert.equal(inactive.length, 1001) + const missingId = Number((await pool.query("SELECT max(id)+10000 AS id FROM servers")).rows[0].id) + const firstContext = context("write"), first = await execution(String(sourceId), ping, firstContext) + success(first); assert.equal((first.body as { output: string }).output, "PING 127.0.0.1\n seq=0 ttl=64 time=1ms status=[redacted]:[redacted]\n sent=1 received=1 packet-loss=0%") + await completed(firstContext.requestId, actor.id, "jwt"); assert.equal(nativeCalls, 1) + assert.equal((await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled, false) + assert.deepEqual(await usage(actor.id), { admissions: 1, files: 1, bytes: 294912 }) + const missing = await execution(String(missingId), ping, context("missing")); assert.equal(missing.statusCode, 404); assert.deepEqual(missing.body, { error: "Server not found" }) + const readonly = await execution(String(missingId), ping, context("readonly", { ...actor, permissions: ["mm:servers:read"] })) + assert.equal(readonly.statusCode, 403) + const deniedContext = { ...context("unauth"), actor: undefined } + const denied = await execution(String(sourceId), { ...ping, authRequired: false, ownerId: actor.id, kind: "local" }, deniedContext) + assert.equal(denied.statusCode, 403); hidden(denied); assert.equal(nativeCalls, 1) + + const localBefore = await usage("local-operator"), forgedExecution = randomUUID() + const localContext = { ...context("local", undefined, false), actor: undefined } + const local = await execution(String(sourceId), { ...ping, ownerId: "forged-owner", kind: "mcp", authRequired: true, + executionId: forgedExecution, requestId: "forged-request", connectionId: randomUUID(), credentialId: randomUUID() }, localContext) + success(local) + const localRow = await completed(localContext.requestId, "local-operator", "local") + assert.equal(localRow.execution_id === forgedExecution, false) + const localAfter = await usage("local-operator") + assert.deepEqual(localAfter, { admissions: localBefore.admissions + 1, files: localBefore.files + 1, bytes: localBefore.bytes + 294912 }) + assert.equal(nativeCalls, 2) + + const bandwidth = { tool: "bandwidth", bwRemoteAddress: peerHost, dstServerId: peerId, bwDuration: 3 } + const disabled = await execution(String(sourceId), bandwidth, context("disabled-peer")) + assert.equal(disabled.statusCode, 409); hidden(disabled); assert.equal(nativeCalls, 2) + await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [peerId]) + const bwContext = context("bw-fallback"), bw = await execution(String(sourceId), { ...bandwidth, dstServerId: missingId }, bwContext) + success(bw); assert.equal((bw.body as { output: string }).output, "bandwidth-test:\n tx=0 rx=42 t=3s") + const bwRow = await completed(bwContext.requestId, actor.id, "jwt"); assert.equal(bwRow.destination_server_id, peerId); assert.equal(nativeCalls, 3) + + mode = "error" + const failureContext = context("provider-error"), failure = await execution(String(sourceId), ping, failureContext) + hidden(failure); assert.equal(failure.statusCode, 502); assert.deepEqual(Object.keys(failure.body), ["error"]) + const failed = await archive(failureContext.requestId); assert.equal(failed.length, 1); assert.equal(failed[0].quarantines, 1); assert.equal(failed[0].receipt_ciphertext, null) + assert.equal(nativeCalls, 4) + + mode = "hold"; activeAbort = new AbortController() + const abortContext = context("abort", actor, true, activeAbort.signal), pending = execution(String(sourceId), ping, abortContext) + await barrier(started, "Owned REST native did not reach held response barrier") + activeAbort.abort() + const aborted = await pending; hidden(aborted); assert.equal(aborted.statusCode, 502); assert.deepEqual(Object.keys(aborted.body), ["error"]) + await barrier(closed, "REST abort did not close the owned native socket") + const stopped = await archive(abortContext.requestId); assert.equal(stopped.length, 1) + assert.equal(stopped[0].state, "claimed"); assert.equal(stopped[0].quarantines, 1); assert.equal(stopped[0].receipt_ciphertext, null) + assert.equal(stopped[0].claim_token, null); assert(stopped[0].claim_attempted_at) + assert.equal(nativeCalls, 5); releaseHeld(); mode = "normal" + assert.deepEqual(await usage(actor.id), { admissions: 4, files: 4, bytes: 4 * 294912 }) + + const quotaActor = { ...actor, id: "owned-rest-quota-" + randomUUID() }, quotaLimits = { ...PROBE_STORAGE_LIMITS, ownerBytes: 294912, ownerFiles: 1, ownerAdmissions: 1 } + const quotaRun = createStorageServerProbeRestExecution(quotaLimits), quotaContext = context("quota-first", quotaActor) + success(await quotaRun(String(sourceId), ping, quotaContext)); await completed(quotaContext.requestId, quotaActor.id, "jwt") + const beforeQuota = nativeCalls, anotherInstance = createStorageServerProbeRestExecution(quotaLimits) + const exhausted = await anotherInstance(String(sourceId), ping, context("quota-exhausted", quotaActor)) + hidden(exhausted); assert.equal(exhausted.statusCode, 503); assert.equal(nativeCalls, beforeQuota) + assert.deepEqual(await usage(quotaActor.id), { admissions: 1, files: 1, bytes: 294912 }); assert.equal(protocolErrors, 0) + + // Real HTTP bridge: the actual auth plugin and route create trusted context. + // No identity, auth mode or request ID comes from body/forwarded headers. + httpApp = await buildApp({ logger: false, startScheduler: false }) + const base = await httpApp.listen({ host: "127.0.0.1", port: 0 }), endpoint = `${base}/api/servers/${sourceId}/probes/run` + const token = (owner: string, permission: string) => new SignJWT({ sub: owner, apps: ["mm"], permissions: [permission] }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setIssuedAt().setExpirationTime("1h") + .sign(new TextEncoder().encode(env.jwtSecret)) + const httpOwner = "owned-rest-http-" + randomUUID(), abortOwner = "owned-rest-http-abort-" + randomUUID() + const forgedRequestId = randomUUID(), forgedBody = { ...ping, kind: "mcp", authRequired: false, + ownerId: "forged-http-owner", connectionId: randomUUID(), credentialId: randomUUID(), requestId: forgedRequestId } + const postHttp = (jwt?: string, signal?: AbortSignal) => fetch(endpoint, { method: "POST", signal, + headers: { "content-type": "application/json", "x-request-id": forgedRequestId, ...(jwt ? { authorization: "Bearer " + jwt } : {}) }, + body: JSON.stringify(forgedBody) }) + const unauthHttp = await postHttp(); assert.equal(unauthHttp.status, 401); hidden(await unauthHttp.json()) + const readonlyHttp = await postHttp(await token(httpOwner, "mm:servers:read")); assert.equal(readonlyHttp.status, 403); hidden(await readonlyHttp.json()) + assert.equal(nativeCalls, 6) + const writeHttp = await postHttp(await token(httpOwner, "mm:servers:write")), writeBody: unknown = await writeHttp.json() + assert.equal(writeHttp.status, 200); hidden(writeBody) + assert.deepEqual(writeBody, first.body); assert.equal(nativeCalls, 7) + const httpRows = (await pool.query("SELECT request_id FROM server_probe_executions WHERE actor_id=$1 AND source_server_id=$2", [httpOwner, sourceId])).rows + assert.equal(httpRows.length, 1); assert.match(httpRows[0].request_id, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i) + assert.notEqual(httpRows[0].request_id, forgedRequestId); await completed(httpRows[0].request_id, httpOwner, "jwt") + + resetHeld(); mode = "hold"; activeAbort = new AbortController() + const disconnect = postHttp(await token(abortOwner, "mm:servers:write"), activeAbort.signal).then( + async response => { await response.body?.cancel(); return false }, () => true) + await barrier(started, "HTTP bridge native did not reach held response barrier") + activeAbort.abort(); assert.equal(await disconnect, true) + await barrier(closed, "HTTP client disconnect did not close the owned native socket") + const until = Date.now() + 15000 + let disconnectedRows: Awaited> = [] + while (Date.now() < until) { + const identities = (await pool.query("SELECT request_id FROM server_probe_executions WHERE actor_id=$1 AND source_server_id=$2", [abortOwner, sourceId])).rows + assert(identities.length <= 1) + disconnectedRows = identities.length ? await archive(identities[0].request_id) : [] + if (disconnectedRows.length === 1 && disconnectedRows[0].quarantines === 1) break + await new Promise(yes => setTimeout(yes, 25)) + } + assert.equal(disconnectedRows.length, 1) + const disconnected = disconnectedRows[0] + assert.equal(disconnected.identity_kind, "rest"); assert.equal(disconnected.authentication, "jwt") + assert.equal(disconnected.actor_id, abortOwner); assert.equal(disconnected.owner_id, abortOwner) + assert.equal(disconnected.mcp_connection_id, null); assert.equal(disconnected.mcp_credential_id, null) + assert.notEqual(disconnected.request_id, forgedRequestId); assert.equal(disconnected.quarantines, 1) + assert.equal(disconnected.receipt_ciphertext, null); assert.equal(disconnected.claim_token, null); assert(disconnected.claim_attempted_at) + assert.deepEqual(await usage(abortOwner), { admissions: 1, files: 1, bytes: 294912 }) + assert.equal(nativeCalls, 8); assert.equal(protocolErrors, 0); releaseHeld(); mode = "normal" + return { actualProductionRestAssembly: true, productionAuthorityAndCrypto: true, mcpDisabledRestWorks: true, + unrelatedInactiveServers: inactive.length, boundedParticipantsWithoutFleetEnumeration: true, authenticatedMissingServer404: true, + readOnlyAndUnauthenticatedDenied: true, fixedLocalRestIdentityIgnoresBodyCapabilities: true, bandwidthDisabledAndFallback: true, + safeProviderFailure: true, abortStatus: aborted.statusCode, abortSocketClosedAndQuarantined: true, + sharedQuotaAcrossFactoryInstances: true, separateEncryptedPayload: true, nativeLoopbackHttpCalls: nativeCalls, + actualHttpRouteSignedJwt: true, clientDisconnect: true, actualApplicationBuildRegistration: true, routesRegisteredInFixtureOnly: false, + nativeOrManagerDnsCalls: 0, allSixToolsClaimed: false, routesRegistered: true, productionDdlApplied: false } + } finally { + activeAbort?.abort(); releaseHeld() + try { if (serverIds.length) await pool.query("DELETE FROM servers WHERE id=ANY($1::int[])", [serverIds]) } + finally { + try { await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [priorSettings.enabled]) } + finally { + try { await httpApp?.close() } + finally { router.closeAllConnections(); await new Promise(yes => router.close(() => yes())) } + } + } + } +} diff --git a/backend/src/services/server-probe-storage-rest.test.ts b/backend/src/services/server-probe-storage-rest.test.ts new file mode 100644 index 00000000..4ab76a5b --- /dev/null +++ b/backend/src/services/server-probe-storage-rest.test.ts @@ -0,0 +1,58 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { createStorageServerProbeRestExecution } from "./server-probe-storage-rest.js" +import { createRestServerProbeAuthority } from "./server-probe-authority.js" +import { createProbeStorageHostIo } from "./server-probe-storage-host-io.js" +import { PROBE_STORAGE_LIMITS } from "../modules/mcp/diagnostic-cancellation-storage.js" + +const context = () => ({ requestId: randomUUID(), acceptedAt: Date.now(), authRequired: true }) +const request = { tool: "ping", target: "127.0.0.1" } +const actor = (permissions: string[]): AuthUser => ({ id: "owned-rest-user", email: "fixture@example.test", name: "Fixture", apps: ["mm"], permissions, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 300000 }) + +test("REST opt-in construction and unauthenticated/read-only inputs cannot query a source or acquire a slot", async () => { + const originalQuery = pool.query, originalConnect = pool.connect + pool.query = (() => assert.fail("No SQL before REST authentication")) as typeof pool.query + pool.connect = (() => assert.fail("No checkout before REST authentication")) as typeof pool.connect + try { + const execute = createStorageServerProbeRestExecution() + assert.equal((await execute("1", { ...request, authRequired: false, ownerId: "admin", kind: "mcp" }, context())).statusCode, 403) + assert.equal((await execute("1", request, { ...context(), actor: actor(["mm:servers:read"]) })).statusCode, 403) + assert.equal((await execute("1", request, { ...context(), authRequired: false, actor: actor(["mm:servers:read"]) })).statusCode, 403) + const expired = { ...actor(["mm:servers:write"]), expiresAt: Date.now() - 1 } + assert.equal((await execute("1", request, { ...context(), actor: expired })).statusCode, 403) + } finally { pool.query = originalQuery; pool.connect = originalConnect } +}) +test("authenticated local missing source retains 404 without fleet enumeration or credential reads", async () => { + const originalQuery = pool.query, originalConnect = pool.connect, queries: string[] = [], releases: boolean[] = [] + pool.query = (() => assert.fail("Metadata must use bounded checkout")) as typeof pool.query + pool.connect = (async () => ({ + async query(config: { text: string; values?: unknown[]; query_timeout: number }) { + queries.push(config.text); assert.equal(config.text, "SELECT id FROM servers WHERE id=$1 LIMIT 2"); assert.deepEqual(config.values, [37]) + assert(config.query_timeout > 0 && config.query_timeout <= 10000) + return { rows: [], command: "SELECT" } + }, release(unsafe?: boolean) { releases.push(!!unsafe) }, + })) as unknown as typeof pool.connect + try { + const response = await createStorageServerProbeRestExecution()("37", request, { ...context(), authRequired: false }) + assert.deepEqual(response, { statusCode: 404, body: { error: "Server not found" } }); assert.equal(queries.length, 1); assert.deepEqual(releases, [false]) + } finally { pool.query = originalQuery; pool.connect = originalConnect } +}) +test("scoped REST authority uses trusted participant metadata and fences permission before resolving", async () => { + let reads = 0, selected = [9, 2] + const resolver = async () => { reads++; return selected } + const local = createRestServerProbeAuthority(undefined, { ...context(), authRequired: false }, { query: async () => assert.fail("No fleet enumeration") }, resolver) + const first = await local.readAuthority(); assert.deepEqual(first.scopeServerIds, [2, 9]); assert.equal(reads, 1) + selected = [9, 3] + const changed = await local.readAuthority(); assert.notEqual(changed.policyProof, first.policyProof) + assert.throws(() => createRestServerProbeAuthority(actor(["mm:servers:read"]), context(), {}, resolver)) + const revoked = createRestServerProbeAuthority(actor(["mm:servers:write"]), context(), { portal: async () => { throw Error("revoked") } }, resolver) + const before = reads; await assert.rejects(revoked.readAuthority()); assert.equal(reads, before) +}) +test("shared kernel fixes identity kind at construction and refuses cross-transport journals before I/O", () => { + assert.throws(() => createProbeStorageHostIo(PROBE_STORAGE_LIMITS, "caller-selected" as never)) + const io = createProbeStorageHostIo(PROBE_STORAGE_LIMITS, "rest") + assert.throws(() => io.journal({ identity: { kind: "mcp" } } as never, { assertAuthority: async () => assert.fail("No authority for wrong transport") })) +}) diff --git a/backend/src/services/server-probe-storage-rest.ts b/backend/src/services/server-probe-storage-rest.ts new file mode 100644 index 00000000..451a76e2 --- /dev/null +++ b/backend/src/services/server-probe-storage-rest.ts @@ -0,0 +1,54 @@ +/** Opt-in REST v2 assembly; importing it does not replace the existing route. + * Authentication/local context is supplied by the server, never request JSON. */ +import { McpError } from "../modules/mcp/security.js" +import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js" +import { PROBE_STORAGE_LIMITS, type ProbeStorageLimits } from "../modules/mcp/diagnostic-cancellation-storage.js" +import { createProbeStorageHostIo } from "./server-probe-storage-host-io.js" +import { createRestServerProbeAuthority } from "./server-probe-authority.js" +import { createProductionServerProbeExecutor } from "./server-probe-execution.js" +import { createSqlServerProbeSourceReaders } from "./server-probe-storage-source.js" +import { createServerProbeRestExecution, type ServerProbeRestTarget, type ServerProbeRestContext } from "./server-probe-rest-execution.js" +import { resolveServerProbeRestBandwidthPeer, serverProbeRestErrorReply, ServerProbeRestContractError, type ServerProbeRestPeerMetadata } from "./server-probe-rest-contract.js" + +const unavailable = (): never => { throw new McpError("STATE_CONFLICT", "Область HTTP-диагностики изменилась", 409) } + +export function createStorageServerProbeRestExecution(limits: ProbeStorageLimits = PROBE_STORAGE_LIMITS) { + const io = createProbeStorageHostIo(limits, "rest") + // Called only inside createRestServerProbeAuthority's before/after portal + // fences: that authority grants fleet-wide server-write. Metadata is bounded + // to actual participants; it never enumerates all IDs or reads credentials. + const participants = async (target: ServerProbeRestTarget): Promise => { + const rows = (await io.query("SELECT id FROM servers WHERE id=$1 LIMIT 2", [target.serverId])).rows + if (!rows.length) return [] + if (rows.length !== 1 || rows[0].id !== target.serverId) return unavailable() + if (target.request.tool !== "bandwidth") return [target.serverId] + const explicit = target.request.dstServerId, remote = target.request.bwRemoteAddress! + if (explicit !== undefined && (!Number.isSafeInteger(explicit) || explicit <= 0)) throw new ServerProbeRestContractError(400, "INVALID_INPUT", "Некорректный dstServerId") + const explicitRows = explicit === undefined ? [] : (await io.query("SELECT id,host,enabled FROM servers WHERE id=$1 LIMIT 2", [explicit])).rows + if (explicitRows.length > 1) return unavailable() + const candidates = explicitRows.length ? explicitRows : (await io.query(`SELECT id,host,enabled FROM servers + WHERE enabled=true AND lower(btrim(host))=lower(btrim($1)) LIMIT 1`, [remote])).rows + const metadata = candidates as unknown as ServerProbeRestPeerMetadata[] + const peer = await resolveServerProbeRestBandwidthPeer(remote, explicit, metadata.map(row => row.id), { + byId: async () => explicitRows.length ? metadata[0] : undefined, + enabledByHost: async () => metadata, + }) + return [...new Set([target.serverId, peer.id])] + } + const execute = createServerProbeRestExecution({ journal: io.journal, + authority: (actor, context, target) => { + if (!target) return unavailable() + return createRestServerProbeAuthority(actor, context, { query: io.rows, + portal: claims => io.withClient(client => assertPortalClaimsCurrent(claims, client)) }, () => participants(target)) + }, + executor: input => createProductionServerProbeExecutor(input, { + getServer: async id => createSqlServerProbeSourceReaders({ query: io.query }, (await input.readAuthority()).scopeServerIds).getServer(id), + findDestination: async (remote, explicit, scope) => createSqlServerProbeSourceReaders({ query: io.query }, scope).findDestination(remote, explicit, scope), + }), + }) + return async (serverId: string, body: unknown, context: ServerProbeRestContext) => { + try { return await io.runRequest({ deadlineAt: Math.min(Date.now() + 300000, context.acceptedAt + 300000, context.actor?.expiresAt ?? Infinity), signal: context.signal }, () => execute(serverId, body, context)) } + catch (failure) { return serverProbeRestErrorReply(failure && typeof failure === "object" ? failure as { code?: string; statusCode?: number } : {}) } + } +} +export const executeStorageServerProbeRest = createStorageServerProbeRestExecution() diff --git a/backend/src/services/server-probe-storage-source.sql-helper.ts b/backend/src/services/server-probe-storage-source.sql-helper.ts new file mode 100644 index 00000000..465a290c --- /dev/null +++ b/backend/src/services/server-probe-storage-source.sql-helper.ts @@ -0,0 +1,42 @@ +/** Owned synthetic SQL fixture only. No native dispatch or production enrolment. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import { createSqlServerProbeSourceReaders } from "./server-probe-storage-source.js" +import { createProductionServerProbeExecutor, type ServerProbeAuthority } from "./server-probe-execution.js" + +export async function acceptProbeStorageSourceSql(serverId: number) { + assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") + const peer = Number((await pool.query(`INSERT INTO servers(name,host,port,enabled,username,password) + VALUES('owned-storage-source-peer','192.0.2.2',443,true,'synthetic-peer-user','synthetic-peer-secret') RETURNING id`)).rows[0].id) + const client = await pool.connect(), original = pool.query + const pids = new Set(), queries: string[] = [] + const raw = { async query(text: string, values?: unknown[]) { + pids.add(Number((await client.query("SELECT pg_backend_pid() AS pid")).rows[0].pid)) + queries.push(text); return client.query(text, values) + } } + try { + pool.query = (() => { throw Error("Owned source fixture forbids pool.query under lease") }) as typeof pool.query + const readers = createSqlServerProbeSourceReaders(raw, [serverId, peer]) + const authority: ServerProbeAuthority = { transport: "mcp", ownerId: "owned-source-owner", connectionId: randomUUID(), credentialId: randomUUID(), expiresAt: Date.now() + 300000, + generation: "owned-synthetic-generation", policyProof: "a".repeat(64), ownerProof: "b".repeat(64), active: true, operationAllowed: true, + scopeServerIds: [serverId, peer], ownerPermissions: ["mm:servers:write"], policyPermissions: ["mm:servers:write"] } + const executor = createProductionServerProbeExecutor({ async readAuthority() { return structuredClone(authority) }, async assertProtections(ids) { assert(ids.every(id => authority.scopeServerIds.includes(id))) } }, readers) + const ping = await executor.prepare(serverId, { tool: "ping", target: "192.0.2.2" }) + assert.equal((await executor.preflight(ping)).receiptId, ping.receiptId) + const bandwidth = await executor.prepare(serverId, { tool: "bandwidth", bwRemoteAddress: "192.0.2.2", dstServerId: peer }) + assert.equal(bandwidth.destinationServerId, peer) + await executor.preflight(bandwidth) + assert(!JSON.stringify(bandwidth).includes("synthetic-peer-secret")) + await raw.query("UPDATE servers SET password='synthetic-changed' WHERE id=$1", [peer]) + await assert.rejects(executor.preflight(bandwidth), { code: "STATE_CONFLICT" }) + await raw.query("UPDATE servers SET enabled=false WHERE id=$1", [peer]) + await assert.rejects(readers.findDestination("192.0.2.2", peer, authority.scopeServerIds), { code: "DESTINATION_DISABLED" }) + assert.equal(await readers.getServer(peer + 100000), undefined) + assert.equal(pids.size, 1) + assert(queries.some(sql => sql.includes("peer-id"))); assert(queries.some(sql => sql.includes("endpoint"))) + return { productionExecutorMapping: true, productionCrypto: true, exactEndpointProofDrift: true, disabledPeerRefused: true, + explicitSqlProjection: true, poolQueryForbiddenUnderLease: true, physicalBackends: pids.size, sourceSqlQueries: queries.length, + syntheticAuthority: true, fullRuntimeAuthorityIntegrated: false, nativeOrDnsCalls: 0, routesRegistered: false } + } finally { pool.query = original; client.release(true) } +} diff --git a/backend/src/services/server-probe-storage-source.test.ts b/backend/src/services/server-probe-storage-source.test.ts new file mode 100644 index 00000000..11952b02 --- /dev/null +++ b/backend/src/services/server-probe-storage-source.test.ts @@ -0,0 +1,54 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { test } from "node:test" +import { createSqlServerProbeSourceReaders } from "./server-probe-storage-source.js" +import { createProductionServerProbeExecutor } from "./server-probe-execution.js" + +const source = { id: 1, enabled: true, host: "192.0.2.1", port: 443, useSsl: true, verifySsl: true, + username: "synthetic-user", password: "synthetic-secret", routingEndpointEpoch: randomUUID() } +function fixture() { + let rows: Record[] = [source] + const queries: { sql: string; values?: unknown[] }[] = [] + const scope = [1, 2] + const readers = createSqlServerProbeSourceReaders({ async query(sql, values) { queries.push({ sql, values }); return { rows } } }, scope) + return { readers, queries, scope, rows(value: Record[]) { rows = value } } +} +test("endpoint reads use exact bounded projection and host-owned scope; credentials only for the exact source", async () => { + const f = fixture(); f.scope.push(3) + assert.equal(await f.readers.getServer(3), undefined); assert.equal(f.queries.length, 0) + assert.deepEqual(await f.readers.getServer(1), source) + assert.deepEqual(f.queries[0].values, [[1, 2], 1]) + assert.match(f.queries[0].sql, /routing_endpoint_epoch AS "routingEndpointEpoch"/) + assert(!f.queries[0].sql.includes("SELECT *")) + f.rows([{ ...source, id: 2 }]); await assert.rejects(f.readers.getServer(1)) + f.rows([source, source]); await assert.rejects(f.readers.getServer(1)) + f.rows([{ ...source, routingEndpointEpoch: "not-an-epoch" }]); await assert.rejects(f.readers.getServer(1)) +}) +test("bandwidth metadata is restricted before query and cannot return credentials or a foreign endpoint", async () => { + const f = fixture(); f.rows([{ id: 2, host: "192.0.2.2", enabled: true }]) + assert.deepEqual(await f.readers.findDestination("192.0.2.2", 2, [2, 9]), { id: 2 }) + assert.deepEqual(f.queries[0].values, [[2], 2]) + assert(!/username|password/.test(f.queries[0].sql)) + f.rows([{ id: 2, host: "192.0.2.2", enabled: true, password: "must-not-appear" }]) + await assert.rejects(f.readers.findDestination("192.0.2.2", 2, [2])) + f.rows([{ id: 9, host: "192.0.2.2", enabled: true }]); await assert.rejects(f.readers.findDestination("192.0.2.2", undefined, [2, 9])) +}) +test("peer resolution preserves disabled explicit refusal, absent-ID host fallback and result bounds", async () => { + const f = fixture(); f.rows([{ id: 2, host: "192.0.2.2", enabled: false }]) + await assert.rejects(f.readers.findDestination("192.0.2.2", 2, [2]), { code: "DESTINATION_DISABLED" }) + f.rows([{ id: 2, host: "192.0.2.2", enabled: true }]) + assert.deepEqual(await f.readers.findDestination(" 192.0.2.2 ", 9, [2]), { id: 2 }) + assert.match(f.queries.at(-1)!.sql, /probe-storage-source:peer-host/) + f.rows(Array.from({ length: 1001 }, () => ({ id: 2, host: "192.0.2.2", enabled: true }))) + await assert.rejects(f.readers.findDestination("192.0.2.2", undefined, [2])) +}) +test("source construction rejects ambiguous scope and incomplete production hooks never fall back to pool", () => { + const sql = { async query() { return { rows: [] } } } + assert.throws(() => createSqlServerProbeSourceReaders(sql, [1, 1])) + assert.throws(() => createSqlServerProbeSourceReaders(sql, [-1])) + const access = { async readAuthority() { throw Error("must not run") }, async assertProtections() {} } + assert.throws(() => createProductionServerProbeExecutor(access, { getServer: async () => undefined } as never), { code: "STATE_UNAVAILABLE" }) + for (const malformed of [null, false, 0, "", {}, () => {}]) { + assert.throws(() => createProductionServerProbeExecutor(access, malformed as never), { code: "STATE_UNAVAILABLE" }) + } +}) diff --git a/backend/src/services/server-probe-storage-source.ts b/backend/src/services/server-probe-storage-source.ts new file mode 100644 index 00000000..a5c35c6a --- /dev/null +++ b/backend/src/services/server-probe-storage-source.ts @@ -0,0 +1,54 @@ +/** Trusted source readers for an opt-in storage host's already owned backend. + * No pool, Drizzle, native calls or registration. Scope is host authority, never + * caller request JSON. Native dispatch and crypto stay in the production executor. */ +import { z } from "zod" +import { ServerProbeExecutionError, type ServerProbeSourceReaders } from "./server-probe-execution.js" +import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js" +import { resolveServerProbeRestBandwidthPeer, ServerProbeRestContractError } from "./server-probe-rest-contract.js" + +const ids = z.array(z.number().int().positive().safe()).max(1000).refine(value => new Set(value).size === value.length) +const endpoint = z.object({ id: z.number().int().positive().safe(), enabled: z.boolean(), host: z.string(), + port: z.number().int().min(1).max(65535), useSsl: z.boolean(), verifySsl: z.boolean(), username: z.string(), password: z.string(), routingEndpointEpoch: z.string().uuid() }).strict() +const metadata = endpoint.pick({ id: true, enabled: true, host: true }) +const deny = (): never => { throw new ServerProbeExecutionError("SOURCE_UNAVAILABLE", "Источник диагностики не подтверждён", 503) } + +export function createSqlServerProbeSourceReaders(sql: ProbeStorageSql, allowedServerIds: readonly number[]): ServerProbeSourceReaders { + const scope = ids.parse([...allowedServerIds]) + const checkRows = (schema: z.ZodType, rows: Record[], maximum: number): T[] => { + if (rows.length > maximum) return deny() + const parsed = z.array(schema).safeParse(rows) + if (!parsed.success) return deny() + return parsed.data + } + return Object.freeze({ + async getServer(id: number) { + if (!Number.isSafeInteger(id) || !scope.includes(id)) return undefined + const rows = checkRows(endpoint, (await sql.query(`/* probe-storage-source:endpoint */ + SELECT id,enabled,host,port,use_ssl AS "useSsl",verify_ssl AS "verifySsl",username,password, + routing_endpoint_epoch AS "routingEndpointEpoch" FROM servers WHERE id=ANY($1::int[]) AND id=$2 LIMIT 2`, [scope, id])).rows, 1) + if (rows.some(row => row.id !== id)) return deny() + return rows[0] + }, + async findDestination(remote: string, explicit: number | undefined, currentScope: readonly number[]) { + const current = ids.parse([...currentScope]), allowed = scope.filter(id => current.includes(id)) + try { + const selected = await resolveServerProbeRestBandwidthPeer(remote, explicit, allowed, { + byId: async id => { + if (!allowed.includes(id)) return undefined + const rows = checkRows(metadata, (await sql.query(`/* probe-storage-source:peer-id */ + SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND id=$2 LIMIT 2`, [allowed, id])).rows, 1) + if (rows.some(row => row.id !== id)) return deny() + return rows[0] + }, + enabledByHost: async host => checkRows(metadata, (await sql.query(`/* probe-storage-source:peer-host */ + SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND enabled=true + AND lower(btrim(host))=lower(btrim($2)) LIMIT 1001`, [allowed, host])).rows, 1000), + }) + return { id: selected.id } + } catch (error) { + if (error instanceof ServerProbeRestContractError) throw new ServerProbeExecutionError(error.code, error.message, error.statusCode) + throw error + } + }, + }) +} diff --git a/backend/src/services/servers-rest-ping-collector.ts b/backend/src/services/servers-rest-ping-collector.ts index 689e11cf..a8f5d22b 100644 --- a/backend/src/services/servers-rest-ping-collector.ts +++ b/backend/src/services/servers-rest-ping-collector.ts @@ -1,6 +1,8 @@ +import { withPartitionWrite, isPartitionWriteOutcomeUnknown, type PartitionWriteOptions } from "./partition-write-transaction.js" import { eq } from "drizzle-orm" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" import { listEnabledMonitoringServers } from "./monitoring-server-catalog.js" -import { db, dbAll, dbQuery } from "../db/index.js" +import { db, dbAll } from "../db/index.js" import { serversApiPingSettings, serversRestPingSamples } from "../db/schema.js" import type { ServersRestPingRunSnapshot } from "../types/scheduler-run-snapshot.js" import { SCHEDULER_RUN_SNAPSHOT_VERSION } from "../types/scheduler-run-snapshot.js" @@ -31,7 +33,10 @@ export async function getServersApiPingSettings() { return await getSettingsRow() } -export async function updateServersApiPingSettings(patch: { enabled?: boolean; intervalSec?: number }) { +export function updateServersApiPingSettings(patch: { enabled?: boolean; intervalSec?: number }) { + return executeGuardedApplicationWrite({ kind: "servers-api-ping-settings", id: "1" }, "all", () => updateServersApiPingSettingsImpl(patch)) +} +async function updateServersApiPingSettingsImpl(patch: { enabled?: boolean; intervalSec?: number }) { const prev = await getSettingsRow() const next = { enabled: patch.enabled ?? prev.enabled, @@ -73,7 +78,7 @@ async function loadLatestResourceDownServerIds(): Promise> { * Лёгкая проверка: GET `/system/identity` (REST) на узел, замер RTT. * Пишет строки в `servers_rest_ping_samples` для алерт-движка (см. `buildSignalSnapshot`). */ -export async function collectServersRestPingOnce(): Promise { +export async function collectServersRestPingOnce(partitionWriteOptions:PartitionWriteOptions={}): Promise { const sampledAt = new Date().toISOString() if (collecting) { return { @@ -127,7 +132,7 @@ export async function collectServersRestPingOnce(): Promise { + await withPartitionWrite(async ({ db: tx }) => { for (const row of snapshot.servers) { const sid = row.serverId const last = lastByServer.get(sid) @@ -158,7 +163,7 @@ export async function collectServersRestPingOnce(): Promise core, certificates: async () => undefined, wireguard: async () => 0, ipsec: async () => { throw Error("NEVER_PROVIDER_BODY") }, bgp: async () => undefined, vxlan: async () => 2, containers: async () => NaN } +const result = await readSidebarCounts(deps), view = sidebarCountsMcpView(result) +assert.equal(view.counts.wireguard, 0); assert.equal(view.counts.vxlan, 2); assert.equal(view.counts.ipsec, null); assert.equal(view.counts.certificates, null) +assert.deepEqual(view.unknownCounters.sort(), ["bgpSessions", "certificates", "containers", "ipsec"]) +assert.equal(view.incomplete, true); assert.ok(!JSON.stringify(view).includes("NEVER_")) +for (const value of [{}, { ...core, monitoringItems: 4 }, { ...core, users: -1 }, { ...core, servers: Number.MAX_SAFE_INTEGER + 1 }]) await assert.rejects(readSidebarCounts({ ...deps, core: async () => value }), /Основные счётчики/) +let read: McpAction["read"], collections = 0, release!: () => void +const gate = new Promise(resolve => { release = resolve }) +registerSidebarCountsActions(((...args: Parameters) => { read = args[8].read }) as typeof add, async () => { collections++; await gate; return result }, async () => "1") +const first = read!({}, {} as ActionContext), second = read!({}, {} as ActionContext) +await yieldTurn(); assert.equal(collections, 1, "Overlapping calls must start only one actual collection") +release() +const shared = await Promise.all([first, second]) as { startedAt: string; observedAt: string }[] +assert.equal(shared[0].startedAt, shared[1].startedAt); assert.equal(shared[0].observedAt, shared[1].observedAt) +await read!({}, {} as ActionContext); assert.equal(collections, 2, "A completed snapshot is not cached as a current reading") +console.log("Shared sidebar counters: complete SQL core required, valid zero retained, unknown/invalid/failed optional sources explicit in MCP and no provider payloads") diff --git a/backend/src/services/sidebar-counts.ts b/backend/src/services/sidebar-counts.ts new file mode 100644 index 00000000..435fdba3 --- /dev/null +++ b/backend/src/services/sidebar-counts.ts @@ -0,0 +1,42 @@ +import { z } from "zod" +import { count } from "drizzle-orm" +import { db } from "../db/index.js" +import { appUsers, filterRules, recursiveRoutes, servers, uptimeProbes, uptimeSpeedProbes } from "../db/schema.js" +import { boundedCompleteCount, completeCollectionCount } from "./complete-count.js" +import { listCertificatesFromServers } from "./certificates-service.js" +import { countWireGuardInterfaces } from "./wireguard-live.js" +import { countIpsecClients } from "./ipsec-live.js" +import { countVxlanTunnels } from "./vxlan-live.js" +import { countContainers } from "./containers-live.js" +import { countBgpSessions } from "./bgp-peers-live.js" + +async function tableCount(table: typeof servers | typeof filterRules | typeof uptimeProbes | typeof uptimeSpeedProbes | typeof recursiveRoutes | typeof appUsers) { + const [row] = await db.select({ n: count() }).from(table) + if (typeof row?.n !== "number" || !Number.isSafeInteger(row.n) || row.n < 0) throw Error("Счётчик БД не подтверждён") + return row.n +} +const counter = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER) +const coreSchema = z.object({ servers: counter, filterRules: counter, uptimeProbes: counter, uptimeSpeedProbes: counter, monitoringItems: counter, recursiveRoutes: counter, users: counter }).strict().refine(value => value.monitoringItems === value.uptimeProbes + value.uptimeSpeedProbes) +export interface SidebarCountsDependencies { + core: () => Promise + certificates: () => Promise + wireguard: () => Promise + ipsec: () => Promise + bgp: () => Promise + vxlan: () => Promise + containers: () => Promise +} +export const defaultSidebarCountsDependencies: SidebarCountsDependencies = { + core: async () => { + const [serversTotal, filterRulesTotal, uptimeProbesTotal, uptimeSpeedProbesTotal, recursiveRoutesTotal, users] = await Promise.all([tableCount(servers), tableCount(filterRules), tableCount(uptimeProbes), tableCount(uptimeSpeedProbes), tableCount(recursiveRoutes), tableCount(appUsers)]) + return { servers: serversTotal, filterRules: filterRulesTotal, uptimeProbes: uptimeProbesTotal, uptimeSpeedProbes: uptimeSpeedProbesTotal, monitoringItems: uptimeProbesTotal + uptimeSpeedProbesTotal, recursiveRoutes: recursiveRoutesTotal, users } + }, + certificates: () => boundedCompleteCount(listCertificatesFromServers, value => completeCollectionCount(value, "certificates")), + wireguard: countWireGuardInterfaces, ipsec: countIpsecClients, bgp: countBgpSessions, vxlan: countVxlanTunnels, containers: countContainers, +} +export async function readSidebarCounts(deps = defaultSidebarCountsDependencies) { + const [core, certificates, wireguard, ipsec, bgpSessions, vxlan, containers] = await Promise.all([deps.core(), boundedCompleteCount(deps.certificates, x => x), boundedCompleteCount(deps.wireguard, x => x), boundedCompleteCount(deps.ipsec, x => x), boundedCompleteCount(deps.bgp, x => x), boundedCompleteCount(deps.vxlan, x => x), boundedCompleteCount(deps.containers, x => x)]) + const parsed = coreSchema.safeParse(core) + if (!parsed.success) throw Error("Основные счётчики не подтверждены") + return { ...parsed.data, certificates, wireguard, ipsec, bgpSessions, vxlan, containers } +} diff --git a/backend/src/services/speed-history-read.ts b/backend/src/services/speed-history-read.ts new file mode 100644 index 00000000..eae9d13e --- /dev/null +++ b/backend/src/services/speed-history-read.ts @@ -0,0 +1,30 @@ +import { and, desc, eq, inArray, or } from "drizzle-orm" +import { db } from "../db/index.js" +import { uptimeSpeedTestRuns } from "../db/schema.js" +import { mapSpeedHistoryRun } from "./speed-history-dto.js" + +type SpeedHistoryOptions = { serverIds?: number[]; serverId?: number; limit?: number; offset?: number } +const pageLimit = (limit = 200) => Math.min(200, Math.max(1, limit)) + +async function readSpeedHistoryRows(options: SpeedHistoryOptions, limit: number, offset: number) { + if (options.serverIds && options.serverIds.length === 0) return [] + const scope = options.serverIds ? and(inArray(uptimeSpeedTestRuns.srcServerId, options.serverIds), inArray(uptimeSpeedTestRuns.dstServerId, options.serverIds)) : undefined + const selected = options.serverId ? or(eq(uptimeSpeedTestRuns.srcServerId, options.serverId), eq(uptimeSpeedTestRuns.dstServerId, options.serverId)) : undefined + return db.select().from(uptimeSpeedTestRuns).where(and(scope, selected)) + .orderBy(desc(uptimeSpeedTestRuns.createdAt), desc(uptimeSpeedTestRuns.id)).limit(limit).offset(offset) +} + +export async function readSpeedHistoryRuns(options: SpeedHistoryOptions = {}) { + return (await readSpeedHistoryRows(options, pageLimit(options.limit), Math.max(0, options.offset ?? 0))).map(mapSpeedHistoryRun) +} + +export async function readSpeedHistoryPage(options: SpeedHistoryOptions) { + const limit = pageLimit(options.limit), offset = Math.max(0, options.offset ?? 0) + const rows = await readSpeedHistoryRows(options, limit + 1, offset) + const incomplete = rows.length > limit + return { + runs: rows.slice(0, limit).map(mapSpeedHistoryRun), + incomplete, + nextOffset: incomplete ? offset + limit : null, + } +} diff --git a/backend/src/services/speed-probe-catalog.ts b/backend/src/services/speed-probe-catalog.ts new file mode 100644 index 00000000..be78d438 --- /dev/null +++ b/backend/src/services/speed-probe-catalog.ts @@ -0,0 +1,24 @@ +import { asc, eq, sql } from "drizzle-orm" +import { emptySpeedProbeObservations, type SpeedProbeWrite } from "@mmapp/contracts" +import { db } from "../db/index.js" +import { uptimeSpeedProbes } from "../db/schema.js" +import { replaceSpeedProbes } from "./speed-probe-replacement.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" + +export async function readSpeedProbeCatalog() { + return db.select().from(uptimeSpeedProbes).orderBy(asc(uptimeSpeedProbes.sortOrder)) +} +export function replaceSpeedProbeCatalog(probes: SpeedProbeWrite[]) { + return executeGuardedApplicationWrite({ kind: "speed-probe-catalog", id: "all" }, "all", () => replaceSpeedProbes({ transaction: run => db.transaction(async tx => { + await tx.execute(sql`select pg_advisory_xact_lock(hashtext('uptime-speed-probes'))`) + await run({ + listProbes: async () => tx.select().from(uptimeSpeedProbes).for("update"), + remove: async id => { await tx.delete(uptimeSpeedProbes).where(eq(uptimeSpeedProbes.id, id)) }, + upsert: async (row, createdAt, resetObservations) => { + const { id, ...patch } = row + await tx.insert(uptimeSpeedProbes).values({ id, ...patch, createdAt }) + .onConflictDoUpdate({ target: uptimeSpeedProbes.id, set: { ...patch, ...(resetObservations ? emptySpeedProbeObservations : {}) } }) + }, + }) + }) }, probes), async () => [...new Set([...(await readSpeedProbeCatalog()).map(row => row.id), ...probes.map(row => row.id)])].map(id => ({ kind: "speed-probe", id }))) +} diff --git a/backend/src/services/speed-run-persistence.ts b/backend/src/services/speed-run-persistence.ts index b5bef851..c5752406 100644 --- a/backend/src/services/speed-run-persistence.ts +++ b/backend/src/services/speed-run-persistence.ts @@ -1,3 +1,7 @@ +export class SpeedTestServerNotFound extends Error { + constructor() { super("Server not found") } +} + export interface SpeedProbeEndpoints { id: string; srcServerId: number; dstServerId: number } export class SpeedRunPersistenceUnknown extends Error { constructor(public readonly runId: string, cause?: unknown) { diff --git a/backend/src/services/statistics-aggregate.ts b/backend/src/services/statistics-aggregate.ts index 34b22b3a..7314e468 100644 --- a/backend/src/services/statistics-aggregate.ts +++ b/backend/src/services/statistics-aggregate.ts @@ -1,4 +1,4 @@ -import { eq } from "drizzle-orm" +import { and, eq } from "drizzle-orm" import { db, dbAll } from "../db/index.js" import { appUsers, flowAsnMeta, servers, userInterfaceBindings } from "../db/schema.js" import { @@ -135,8 +135,8 @@ async function warmIfaceCache(ids: Iterable): Promise { await Promise.all(uniq.map((id) => refreshServerIfaces(id))) } -async function warmBindingIfaceCache(): Promise { - const rows = await db.select({ serverId: userInterfaceBindings.serverId }).from(userInterfaceBindings) +async function warmBindingIfaceCache(serverId?: number): Promise { + const rows = await db.select({ serverId: userInterfaceBindings.serverId }).from(userInterfaceBindings).where(serverId === undefined ? undefined : eq(userInterfaceBindings.serverId, serverId)) await warmIfaceCache(rows.map((r) => r.serverId)) } @@ -285,8 +285,8 @@ interface UserBindTuple { iface: string } -async function loadBindUserTuples(): Promise { - const binds = await db.select().from(userInterfaceBindings) +async function loadBindUserTuples(serverId?: number): Promise { + const binds = await db.select().from(userInterfaceBindings).where(serverId === undefined ? undefined : eq(userInterfaceBindings.serverId, serverId)) const seen = new Set() const out: UserBindTuple[] = [] for (const b of binds) { @@ -312,9 +312,9 @@ function uniqueBoundIfaces(tuples: UserBindTuple[]): Array<{ serverId: number; i return out } -async function resolveUserIfaces(userId?: string): Promise | null> { +async function resolveUserIfaces(userId?: string, serverId?: number): Promise | null> { if (!userId || userId === STATISTICS_UNBOUND_USER_ID) return null - const binds = await db.select().from(userInterfaceBindings).where(eq(userInterfaceBindings.userId, userId)) + const binds = await db.select().from(userInterfaceBindings).where(and(eq(userInterfaceBindings.userId, userId), serverId === undefined ? undefined : eq(userInterfaceBindings.serverId, serverId))) return expandBindingIfaces(binds.map((b) => ({ serverId: b.serverId, iface: b.interfaceName }))) } @@ -351,7 +351,7 @@ async function loadPayloadScope(serverId?: number): Promise<{ }> { const topo = await loadFlowTopology() const catalog = await getServerCatalog() - await warmIfaceCache(catalog.list.map((s) => s.id)) + await warmIfaceCache(catalog.list.filter(s => serverId === undefined || s.id === serverId).map((s) => s.id)) const overlayRaw: Array<{ serverId: number; iface: string }> = [] const wanRaw: Array<{ serverId: number; iface: string }> = [] for (const s of catalog.list) { @@ -378,10 +378,10 @@ async function loadPayloadScope(serverId?: number): Promise<{ } async function buildFilterCtx(query: StatisticsQuery, period: ParsedPeriod): Promise { - const bindTuples = await loadBindUserTuples() + const bindTuples = await loadBindUserTuples(query.serverId) const boundIfaces = uniqueBoundIfaces(bindTuples) const unboundOnly = query.userId === STATISTICS_UNBOUND_USER_ID - const userIfaces = unboundOnly ? null : await resolveUserIfaces(query.userId) + const userIfaces = unboundOnly ? null : await resolveUserIfaces(query.userId, query.serverId) if (userIfaces && userIfaces.length === 0) return null if (unboundOnly) return null const scope = await loadPayloadScope(query.serverId) @@ -414,9 +414,9 @@ export async function getStatistics(query: StatisticsQuery): Promise() +const restoreAdmission = new AsyncLocalStorage<{ active: boolean }>() +// The shared readiness latch also refuses new request/worker admission after +// unconfirmed replacement; startup recovery supplies the cross-process guard. +/** Acquire before worker/config locks so admitted requests can drain without a lock inversion. */ +export function withSystemDatabaseBackupAdmission(work: () => Promise) { + return sharedDatabaseGate.exclusive(async () => { + const admission = { active: true } + try { return await backupAdmission.run(admission, work) } finally { admission.active = false } + }) +} + +/** Terminal MCP claim and restore share one exclusive admission from first SQL. */ +export function withSystemDatabaseRestoreAdmission(work: () => Promise) { + return sharedDatabaseGate.exclusive(async () => { + const admission = { active: true } + try { return await restoreAdmission.run(admission, work) } finally { admission.active = false } + }) +} function fmtTimestamp(date = new Date()): string { const pad = (n: number) => String(n).padStart(2, "0") @@ -36,7 +74,7 @@ function spawnCollected(command: string, args: string[], opts?: { stdin?: Buffer child.stderr.on("data", (chunk: Buffer) => { err += chunk.toString("utf8") }) - child.on("error", reject) + child.on("error", () => reject(new Error(`Утилита ${command} недоступна; проверьте установку PostgreSQL client tools и PATH`))) child.on("close", (code) => { if (code !== 0) { reject(new Error(err.trim().replaceAll(env.DATABASE_URL, "[DATABASE_URL]") || `${command} завершился с кодом ${code}`)) @@ -49,48 +87,293 @@ function spawnCollected(command: string, args: string[], opts?: { stdin?: Buffer }) } -async function withDatabaseOperation(fn: () => Promise): Promise { - return withOperationLock(() => pool.connect(), BACKUP_LOCK_KEY, () => executeDatabaseOperation(fn, { - stopListener: stopTrafficFlowListenerForDatabaseOperation, - stopScheduler: async () => { - await stopSchedulerForDatabaseOperation() - await pauseRipeBackgroundWork() - }, - startListener: restartTrafficFlowListenerAfterDatabaseOperation, - startScheduler: async () => { - resumeRipeBackgroundWork() - await restartSchedulerAfterDatabaseOperation() - }, - }), "Операция с базой приложения уже выполняется") +async function runRestoreTool(command: string, args: string[], env: NodeJS.ProcessEnv): Promise { + try { + return await boundedDatabaseCommand(command, args, { + env, timeoutMs: command === "psql" ? 600_000 : 120_000, + }) + } catch { + throw new Error(`Команда восстановления PostgreSQL ${command} не выполнена`) + } } -export async function exportSystemDatabaseBackup(): Promise<{ filename: string; buffer: Buffer } & DatabaseOperationWarnings> { - return await withDatabaseOperation(async () => { - const buffer = await spawnCollected("pg_dump", ["-Fc", env.DATABASE_URL]) +type PendingRestoreCheckpoint = Extract + +async function withDatabaseOperation(fn: (client: DatabaseOperationLockClient, discard: () => void, targetCheckSql?: string, journalHead?: RestoreJournalHead | null, checkpoint?: PendingRestoreCheckpoint) => Promise, admittedKind?: "backup" | "restore"): Promise { + const execute = () => withSystemDatabaseOperationLock(() => pool.connect(), async (client, discard) => { + const run = (targetCheckSql?: string, journalHead?: RestoreJournalHead | null, checkpoint?: PendingRestoreCheckpoint) => executeDatabaseOperation(() => fn(client, discard, targetCheckSql, journalHead, checkpoint), { + stopListener: stopTrafficFlowListenerForDatabaseOperation, + beforeRestart: schemaReadiness.assertReady, + stopScheduler: async () => { + await stopSchedulerForDatabaseOperation() + await pauseRipeBackgroundWork() + }, + startListener: restartTrafficFlowListenerAfterDatabaseOperation, + startScheduler: async () => { + resumeRipeBackgroundWork() + await restartSchedulerAfterDatabaseOperation() + }, + }) + let journalHead: RestoreJournalHead | null | undefined + let pending: PendingRestoreCheckpoint | undefined + if (databaseRestoreQuarantineEnabled()) { + await assertDatabaseRestoreQuarantineOpen(client) + const { assertDatabaseRestoreJournalCurrent } = await import("./database-maintenance-startup.js") + journalHead = await assertDatabaseRestoreJournalCurrent(client, mcpStateDirectory()) + } + if (process.env.DATABASE_RESTORE_CHECKPOINT_FILE !== undefined) { + const { DatabaseRestoreCheckpointRefused, readDatabaseRestoreCheckpoint, restoreJournalHeadsEqual, restoreWriterHeadIsCurrent } = await import("./database-restore-checkpoint.js") + const checkpoint = await readDatabaseRestoreCheckpoint(mcpStateDirectory(), env.DATABASE_URL) + if (!checkpoint || !restoreJournalHeadsEqual(checkpoint.head, journalHead) || + checkpoint.status !== (admittedKind === "restore" ? "pending" : "sealed") || + !restoreWriterHeadIsCurrent(checkpoint.writerHead, await readRuntimeDatabaseWriterHead(client))) throw new DatabaseRestoreCheckpointRefused() + if (checkpoint.status === "pending") pending = checkpoint + } + if (process.env.DATABASE_RUNTIME_ACTIVATION_DIRECTORY !== undefined) assertRuntimeDatabaseActivationCurrent() + return admittedKind === "restore" ? withDatabaseRestoreTarget(client, discard, targetCheckSql => run(targetCheckSql, journalHead, pending)) : run() + }) + const admission = admittedKind === "backup" ? backupAdmission.getStore() : admittedKind === "restore" ? restoreAdmission.getStore() : undefined + return admission?.active ? sharedDatabaseGate.withinAdmission(execute) : sharedDatabaseGate.exclusive(execute) +} + +export async function exportSystemDatabaseBackup(options?: { signal?: AbortSignal; authorize?: () => Promise }): Promise<{ filename: string; buffer: Buffer } & DatabaseOperationWarnings> { + return await withDatabaseOperation(async (client, discard) => { + await options?.authorize?.() + let buffer: Buffer + const archivePasswordFile = process.env.DATABASE_ARCHIVE_PASSWORD_FILE + if (process.env.DATABASE_RUNTIME_REQUEST_FILE !== undefined || archivePasswordFile !== undefined) { + const { exportProtectedDatabaseSnapshot } = await import("./database-protected-archive.js") + buffer = await exportProtectedDatabaseSnapshot(client, discard, archivePasswordFile, options) + } else if (options?.authorize) { + buffer = await runPrivateDatabaseDump(env.DATABASE_URL, options) + } else { + buffer = await spawnCollected("pg_dump", ["-Fc", DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG, env.DATABASE_URL]) + } + await options?.authorize?.() return { filename: `mikrotik-manager_${fmtTimestamp()}.dump`, buffer, } - }) + }, "backup") } -export async function restoreSystemDatabaseBackup(buffer: Buffer): Promise { +/** REST and MCP share the same export event; event failure never erases a completed dump. */ +export async function exportSystemDatabaseBackupWithEvent(options?: Parameters[0], dependencies = { exportBackup: exportSystemDatabaseBackup, event: appendEvent }) { + const result = await dependencies.exportBackup(options) + let eventError: string | undefined + try { + await dependencies.event({ level: "info", eventType: "system.database.backup", sourceModule: "system", title: "Создан бэкап базы приложения", message: result.filename, + entityType: "system_database", entityId: result.filename, payload: { filename: result.filename, sizeBytes: result.buffer.length } }) + } catch { eventError = "Не удалось записать событие резервного копирования" } + return { ...result, ...(eventError ? { eventError } : {}) } +} + +export async function restoreSystemDatabaseBackup(buffer: Buffer, options?: { beforeRestore?: () => Promise }): Promise { validateSystemDatabaseRestoreArchive(buffer) - return await withDatabaseOperation(() => executeSystemDatabaseRestore(buffer, env.DATABASE_URL, { - allocate: async () => { - const tempDir = path.join(os.tmpdir(), "mmapp-db-restore") - await mkdir(tempDir, { recursive: true, mode: 0o700 }) - await chmod(tempDir, 0o700) - return path.join(tempDir, `restore-${randomUUID()}.dump`) - }, - write: (file, data) => writeFile(file, data, { mode: 0o600 }), - run: async args => { - await restorePartitionedDatabase(args[args.length - 1], env.DATABASE_URL, { - command: spawnCollected, - prepareSql: file => writeFile(file, "", { mode: 0o600 }), - }) - }, - remove: async file => { await Promise.all([rm(file, { force: true }), rm(`${file}.sql`, { force: true })]) }, - invalidate: invalidateSystemDatabaseCaches, - })) + const quarantine = databaseRestoreQuarantineEnabled() + const dataVerification = process.env.DATABASE_RESTORE_ARCHIVE_SHA256 !== undefined || process.env.DATABASE_RESTORE_DATA_SHA256 !== undefined + ? await import("../db/database-restore-data.js") : undefined + const expectedData = dataVerification?.approvedRestoreDataDigest(buffer, quarantine) + if (quarantine && !env.DATABASE_RESTORE_URL_FILE) throw new Error("Для карантина восстановления требуется подключение владельца БД") + if (!env.DATABASE_RESTORE_URL_FILE) validatePrivateRestoreConnection(env.DATABASE_URL) + await assertSystemDatabaseRestoreTools() + return await withDatabaseOperation(async (_client, _discard, targetCheckSql, previousHead, checkpoint) => { + if (!targetCheckSql) throw new Error("Не подтверждено подключение к базе для восстановления") + if ((expectedData && previousHead === undefined) || (!expectedData && previousHead)) { + throw new Error("Для восстановления требуется согласованная настройка защищённого журнала PostgreSQL") + } + if (checkpoint && (expectedData?.archiveSha256 !== checkpoint.archiveSha256 || expectedData?.dataSha256 !== checkpoint.dataSha256)) { + const { DatabaseRestoreCheckpointRefused } = await import("./database-restore-checkpoint.js") + throw new DatabaseRestoreCheckpointRefused() + } + let consumedCheckpoint: Extract | undefined + const assertCurrentCheckpoint = async () => { + if (!checkpoint) return + const { DatabaseRestoreCheckpointRefused, readDatabaseRestoreCheckpoint } = await import("./database-restore-checkpoint.js") + const current = await readDatabaseRestoreCheckpoint(mcpStateDirectory(), env.DATABASE_URL) + if (JSON.stringify(current) !== JSON.stringify(consumedCheckpoint ?? checkpoint)) throw new DatabaseRestoreCheckpointRefused() + } + await assertDatabaseRestoreAllowed() + const owner = env.DATABASE_RESTORE_URL_FILE ? await readRestoreOwnerConnection(env.DATABASE_RESTORE_URL_FILE) : undefined + const connection = owner?.connection ?? env.DATABASE_URL + const restoreCheckSql = owner ? `${targetCheckSql}\n${databaseRestoreOwnerCheckSql(owner.owner)}` : targetCheckSql + const journalProof = expectedData ? await (async () => { + const { RESTORE_DATA_JOURNAL_DOMAIN, runtimeDatabaseTargetSha256 } = await import("./database-maintenance-resolution.js") + const { databaseRestoreJournalHead, readMaintenanceRecoveryFacts } = await import("./database-maintenance-startup.js") + const { constants } = await import("node:fs") + const activationSha256 = process.env.DATABASE_RUNTIME_ACTIVATION_SHA256 ?? null + if (activationSha256 !== null && !/^[a-f0-9]{64}$/.test(activationSha256)) { + throw new Error("Поколение PostgreSQL для журнала восстановления не подтверждено") + } + return { + format: RESTORE_DATA_JOURNAL_DOMAIN, + openFlags: constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | (constants.O_NOFOLLOW ?? 0), + binding: { ...expectedData, runtimeTargetSha256: runtimeDatabaseTargetSha256(env.DATABASE_URL), activationSha256 }, + sign: await privateFingerprinter(RESTORE_DATA_JOURNAL_DOMAIN), + readMaintenanceRecoveryFacts, + databaseRestoreJournalHead, + } as const + })() : undefined + await options?.beforeRestore?.() + await assertCurrentCheckpoint() + if (checkpoint) { + const { consumeRestoreCheckpoint } = await import("./database-restore-checkpoint-claim.js") + consumedCheckpoint = await consumeRestoreCheckpoint(checkpoint) + await assertCurrentCheckpoint() + } + await accessGeneration() + const directory = mcpStateDirectory() + const journalPath = path.join(directory, "database-maintenance.jsonl") + const journal = await open(journalPath, journalProof?.openFlags ?? "a", 0o600) + const assertJournalFile = async () => { + const handle = await journal.stat({ bigint: true }) + const current = await lstat(journalPath, { bigint: true }) + if (!handle.isFile() || !current.isFile() || handle.nlink !== 1n || current.nlink !== 1n || + handle.dev !== current.dev || handle.ino !== current.ino) { + throw new Error("Файл журнала восстановления PostgreSQL изменился") + } + } + const operationId = checkpoint?.operationId ?? randomUUID() + let verified = false + let intentHead: RestoreJournalHead | undefined + let appliedHead: RestoreJournalHead | undefined + const syncJournalDirectory = async () => { + if (process.platform === "win32") return + const parent = await open(directory, "r") + try { await parent.sync() } finally { await parent.close() } + } + const record = async (state: RestoreDataJournalRecord["state"]) => { + const at = new Date().toISOString() + let signedLine: Buffer | undefined + if (journalProof) { + if (state !== "intent" && !intentHead) throw new Error("Начало восстановления PostgreSQL не подтверждено") + if (state === "applied" && !verified) throw new Error("Проверка данных PostgreSQL не подтверждена") + await assertJournalFile() + const binding = consumedCheckpoint ? { ...journalProof.binding, claimId: consumedCheckpoint.claimId } : journalProof.binding + const record: RestoreDataJournalRecord = { format: journalProof.format, operationId, state, at, binding } + signedLine = Buffer.from(JSON.stringify({ ...record, mac: journalProof.sign(JSON.stringify(record)) }) + "\n") + await journal.writeFile(signedLine) + } else { + await journal.writeFile(JSON.stringify({ operationId, state, at }) + "\n") + } + await journal.sync() + if (journalProof && state === "intent") await syncJournalDirectory() + if (journalProof && signedLine) { + const { journal: persisted } = await journalProof.readMaintenanceRecoveryFacts(directory) + if (!persisted.subarray(-signedLine.length).equals(signedLine)) { + throw new Error("Запись журнала восстановления PostgreSQL не подтверждена") + } + const expectedPrefix = state === "intent" ? previousHead : intentHead + if (expectedPrefix && (persisted.length !== expectedPrefix.bytes + signedLine.length || + journalProof.databaseRestoreJournalHead(persisted.subarray(0, expectedPrefix.bytes)).sha256 !== expectedPrefix.sha256)) { + throw new Error("История журнала восстановления PostgreSQL изменилась") + } + await assertJournalFile() + if (state === "intent") intentHead = journalProof.databaseRestoreJournalHead(persisted) + if (state === "applied") appliedHead = journalProof.databaseRestoreJournalHead(persisted) + } + } + const claimedRestoreSql = quarantine ? `${restoreCheckSql} +DO $mm_restore_quarantine$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM mmapp_control.database_restore_quarantine WHERE id=1 AND operation_id='${operationId}'::pg_catalog.uuid) THEN + RAISE EXCEPTION 'Restore quarantine is unconfirmed' USING ERRCODE='55000'; + END IF; +END $mm_restore_quarantine$;` : restoreCheckSql + const updateQuarantine = async (update: typeof beginDatabaseRestoreQuarantine, expected?: RestoreJournalHead | null, next?: RestoreJournalHead) => { + if (journalProof && (expected === undefined || !next)) throw new Error("Не подтверждена запись журнала восстановления") + const client = new pg.Client({ connectionString: connection, connectionTimeoutMillis: 8_000 }) + try { + await client.connect() + await client.query("BEGIN") + await client.query("SET LOCAL lock_timeout='5s'; SET LOCAL statement_timeout='10s'; SET LOCAL synchronous_commit=on") + await client.query(restoreCheckSql) + await update(client, operationId, expected !== undefined && next ? { expected, next } : undefined) + await client.query(restoreCheckSql) + if ((await client.query("COMMIT")).command !== "COMMIT") throw new Error("Commit not acknowledged") + } catch { + throw new Error("Не подтверждена запись карантина восстановления") + } finally { await client.end() } + } + const outcome = await executeMcpDatabaseMaintenance(async () => { + let result: SystemDatabaseRestoreOutcome | undefined + try { + await withPrivateRestoreService(connection, async ({ dsn, env: connectionEnvironment }) => { + result = await executeSystemDatabaseRestore(buffer, dsn, { + allocate: async () => { + const tempDir = path.join(os.tmpdir(), "mmapp-db-restore") + await mkdir(tempDir, { recursive: true, mode: 0o700 }) + await chmod(tempDir, 0o700) + return path.join(tempDir, `restore-${randomUUID()}.dump`) + }, + write: (file, data) => { + if (dataVerification && expectedData) { + const approved = dataVerification.approvedRestoreDataDigest(data, quarantine) + if (approved.archiveSha256 !== expectedData.archiveSha256 || approved.dataSha256 !== expectedData.dataSha256) { + throw new dataVerification.DatabaseRestoreDataRefused() + } + } + return writeFile(file, data, { mode: 0o600 }) + }, + run: async args => { + await restorePartitionedDatabase(args[args.length - 1], dsn, { + command: (command, commandArgs) => runRestoreTool(command, commandArgs, + command === "psql" ? connectionEnvironment : restoreDatabaseToolEnvironment()), + beforeTargetReplacement: async () => { + await assertCurrentCheckpoint() + schemaReadiness.beginReplacement() + if (quarantine) await updateQuarantine(beginDatabaseRestoreQuarantine, previousHead, intentHead) + }, + targetCheckSql: claimedRestoreSql, + exclusivePartitionWriters: quarantine, + writePublicSchemaPreservingList: process.env.DATABASE_RUNTIME_WRITER_APPROVAL_FILE === undefined ? undefined + : (file, list) => writeFile(file, list, { flag: "wx", mode: 0o600 }), + prepareSql: file => writeFile(file, "", { mode: 0o600 }), + }) + }, + remove: async file => { await Promise.all([rm(file, { force: true }), rm(`${file}.sql`, { force: true }), rm(`${file}.list`, { force: true })]) }, + invalidate: invalidateSystemDatabaseCaches, + }) + }) + } catch (error) { + if (!result) throw error + const warning = "Не удалось удалить временные параметры подключения к базе" + result.cleanupError = result.cleanupError ? `${result.cleanupError}; ${warning}` : warning + } + if (!result) throw new Error("Восстановление PostgreSQL не подтверждено") + const ownerPool = owner ? new pg.Pool({ connectionString: connection, max: 1, connectionTimeoutMillis: 8_000 }) : undefined + const migrationUnconfirmed = "База восстановлена, но миграции приложения не завершены. Требуется проверка схемы; не повторяйте restore." + try { + try { await applySqlMigrations(ownerPool ?? pool, claimedRestoreSql) } + catch { result.migrationError = migrationUnconfirmed } + if (dataVerification && expectedData && !result.migrationError) { + if (!ownerPool) throw new dataVerification.DatabaseRestoreDataRefused() + await dataVerification.assertRuntimeRestoreData(ownerPool, expectedData.dataSha256, claimedRestoreSql) + assertRuntimeDatabaseActivationCurrent() + verified = true + result.dataVerified = true + } + } finally { + if (ownerPool) try { await ownerPool.end() } catch { result.migrationError ??= migrationUnconfirmed } + } + return result + }, { record, invalidateAccess: async () => { + await invalidateMcpAccess() + if (journalProof) await syncJournalDirectory() + }, close: () => journal.close() }) + // Neither uncertain replacement nor unconfirmed external completion may + // release request admission or restart consumers. A persisted claim requires operator inspection. + if (!outcome.migrationError && !outcome.maintenanceError) { + if (checkpoint) return { ...outcome, maintenanceError: "Восстановление подтверждено. Карантин сохранён до публикации внешней контрольной записи PostgreSQL." } + if (quarantine) try { await updateQuarantine(completeDatabaseRestoreQuarantine, intentHead, appliedHead) } + catch { return { ...outcome, maintenanceError: "Не подтверждено снятие карантина восстановления. Требуется сверка базы." } } + schemaReadiness.confirmSchema() + } + return outcome + }, "restore") +} + +export async function assertSystemDatabaseRestoreTools() { + await runRestoreTool("pg_restore", ["--version"], restoreDatabaseToolEnvironment()) + await runRestoreTool("psql", ["--version"], restoreDatabaseToolEnvironment()) } diff --git a/backend/src/services/system-database-cache.test.ts b/backend/src/services/system-database-cache.test.ts index c05b1d19..98826ec6 100644 --- a/backend/src/services/system-database-cache.test.ts +++ b/backend/src/services/system-database-cache.test.ts @@ -1,6 +1,7 @@ import assert from "node:assert/strict" import { test } from "node:test" import { invalidateSystemDatabaseCaches } from "./system-database-cache.js" +import { createBackupJobStore, readBackupJob } from "./backup-job-store.js" import { applyRingSnapshot, snapshotRings } from "./traffic-flow-engine.js" import { rememberServerIfaces, ifaceCacheHas } from "./traffic-flow-ifindex.js" import { DatabaseBackgroundWork } from "./database-background-work.js" @@ -16,14 +17,19 @@ test("restore discards old RIPE, interface and live data without reopening RIPE seedRipeCacheForTests({ prefix: "8.8.8.0/24", asn: 15169, country: "US", lat: null, lng: null, holder: "old database", ok: true, fetchedAt: Date.now() }) rememberServerIfaces(1, [{ ".id": "*1", name: "ether1" }]) - applyRingSnapshot([{ key: "old database", inBps: [1_000_000], outBps: [1_000_000] }]) + applyRingSnapshot([{ key: "old database", inBps: Array(60).fill(1_000_000), outBps: Array(60).fill(1_000_000) }]) assert.equal(lookupRipeCached("8.8.8.8")?.holder, "old database") assert.equal(ifaceCacheHas(1), true) assert.equal(snapshotRings().length, 1) await pauseRipeBackgroundWork() try { const count = ripeFetchCountForTests() + const backupStore = createBackupJobStore() + backupStore.jobs.set("old-job", { id: "old-job", serverIds: ["1"], status: "queued", total: 1, completed: 0, requestedAt: "2026-10-01T00:00:00Z", created: [], failures: [] }) + assert.ok(readBackupJob("old-job")) invalidateSystemDatabaseCaches() + assert.equal(readBackupJob("old-job"), undefined) + backupStore.close() assert.equal(lookupRipeCached("8.8.8.8"), null) assert.equal(ifaceCacheHas(1), false) assert.equal(snapshotRings().length, 0) diff --git a/backend/src/services/system-database-cache.ts b/backend/src/services/system-database-cache.ts index 0f8de7c3..445eb726 100644 --- a/backend/src/services/system-database-cache.ts +++ b/backend/src/services/system-database-cache.ts @@ -6,9 +6,11 @@ import { invalidateFlowClassificationCache } from "./traffic-flow-classify.js" import { invalidateFlowAnalyticsCache } from "./traffic-flow-analytics.js" import { invalidateFlowMapHopsCache } from "./traffic-flow-map-hops.js" import { resetEngineAfterDatabaseRestore } from "./traffic-flow-engine.js" +import { invalidateBackupJobs } from "./backup-job-store.js" /** Synchronous invalidation after confirmed restore and before consumers restart. */ export function invalidateSystemDatabaseCaches(): void { + invalidateBackupJobs() invalidateTrafficFlowSettingsCache() invalidateFlowCatalogCache() invalidateRipeCache() diff --git a/backend/src/services/system-database-operation.test.ts b/backend/src/services/system-database-operation.test.ts index 3b28202d..93d85682 100644 --- a/backend/src/services/system-database-operation.test.ts +++ b/backend/src/services/system-database-operation.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict" import {test} from "node:test" -import {executeDatabaseOperation} from "./system-database-operation.js" +import {createDatabaseSchemaReadiness,executeDatabaseOperation} from "./system-database-operation.js" function fixture(fail: string[] = []) { const calls:string[]=[] const step=(name:string)=>async()=>{calls.push(name);if(fail.includes(name))throw Error(name+" failed")} @@ -21,3 +21,20 @@ test("core failure survives restart failures without command retry",async()=>{ test("stop failure prevents command and still attempts both restarts",async()=>{ for(const step of ["stop listener","stop scheduler"]){const f=fixture([step,"start listener"]);await assert.rejects(executeDatabaseOperation(f.run,f.deps),new RegExp(step+" failed"));assert.equal(f.calls.includes("command"),false);assert.deepEqual(f.calls.slice(-2),["start listener","start scheduler"])} }) + +test("unconfirmed schema blocks both restarts across subsequent operations",async()=>{ + const readiness=createDatabaseSchemaReadiness();readiness.beginReplacement() + for(let i=0;i<2;i++){ + const f=fixture();const result=await executeDatabaseOperation(f.run,{...f.deps,beforeRestart:readiness.assertReady}) + assert.match(result.listenerError!,/не подтверждена/);assert.match(result.schedulerError!,/не подтверждена/) + assert.deepEqual(f.calls,["stop listener","stop scheduler","command"]) + } + readiness.confirmSchema();const f=fixture();await executeDatabaseOperation(f.run,{...f.deps,beforeRestart:readiness.assertReady}) + assert.deepEqual(f.calls.slice(-2),["start listener","start scheduler"]) +}) + +test("readiness refusal preserves primary failure and is checked once before either restart",async()=>{ + const f=fixture(["command"]);let checks=0 + await assert.rejects(executeDatabaseOperation(f.run,{...f.deps,beforeRestart:()=>{checks++;throw Error("schema unknown")}}),/command failed/) + assert.equal(checks,1);assert.deepEqual(f.calls,["stop listener","stop scheduler","command"]) +}) diff --git a/backend/src/services/system-database-operation.ts b/backend/src/services/system-database-operation.ts index 44f9220a..5295a4f3 100644 --- a/backend/src/services/system-database-operation.ts +++ b/backend/src/services/system-database-operation.ts @@ -4,8 +4,18 @@ interface Dependencies { stopScheduler: () => Promise | void startListener: () => Promise | void startScheduler: () => Promise | void + beforeRestart?: () => Promise | void } -/** Restart both consumers independently; secondary errors cannot erase a completed command. */ +/** Process-local latch; durable restart recovery needs a separate control journal. */ +export function createDatabaseSchemaReadiness() { + let ready = true + return { + beginReplacement() { ready = false }, + confirmSchema() { ready = true }, + assertReady() { if (!ready) throw new Error("Схема базы после восстановления не подтверждена") }, + } +} +/** Restart both consumers independently only after the common readiness guard. */ export async function executeDatabaseOperation(run: () => Promise, deps: Dependencies): Promise { let result: T | undefined, failed = false, failure: unknown const warnings: DatabaseOperationWarnings = {} @@ -15,8 +25,16 @@ export async function executeDatabaseOperation(run: () => Prom result = await run() } catch (error) { failed = true; failure = error } finally { - try { await deps.startListener() } catch { warnings.listenerError = "Не удалось перезапустить приём NetFlow" } - try { await deps.startScheduler() } catch { warnings.schedulerError = "Не удалось перезапустить планировщик" } + let restartAllowed = true + try { await deps.beforeRestart?.() } catch { + restartAllowed = false + warnings.listenerError = "Приём NetFlow оставлен остановленным: схема базы не подтверждена" + warnings.schedulerError = "Планировщик оставлен остановленным: схема базы не подтверждена" + } + if (restartAllowed) { + try { await deps.startListener() } catch { warnings.listenerError = "Не удалось перезапустить приём NetFlow" } + try { await deps.startScheduler() } catch { warnings.schedulerError = "Не удалось перезапустить планировщик" } + } } if (failed) throw failure return { ...result!, ...warnings } diff --git a/backend/src/services/system-database-partition-restore.test.ts b/backend/src/services/system-database-partition-restore.test.ts index 975ee329..3d4db38b 100644 --- a/backend/src/services/system-database-partition-restore.test.ts +++ b/backend/src/services/system-database-partition-restore.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict" import { test } from "node:test" -import { partitionRestorePrelude, restorePartitionedDatabase } from "./system-database-partition-restore.js" +import { partitionRestoreAdmissionSql, partitionRestorePrelude, restorePartitionedDatabase } from "./system-database-partition-restore.js" const toc = "1; 1259 1 TABLE public servers mmapp\n2; 1259 2 TABLE public schema_migrations mmapp\n3; 1259 3 TABLE public server_snapshots mmapp\n" test("preparation covers only known application parents present in the archive", () => { @@ -21,9 +21,12 @@ test("archive decoding precedes one transactional psql execution with fail-fast calls.push(command) if (command === "pg_restore" && args[0] === "--list") return Buffer.from(toc) if (command === "psql") { - assert.deepEqual(args.slice(0, 6), ["-X", "--single-transaction", "--set", "ON_ERROR_STOP=on", "-d", "fixture-db"]) - assert.equal(args[6], "-c"); assert.ok(args[7].includes("DROP CONSTRAINT")) - assert.deepEqual(args.slice(8), ["-f", "fixture.dump.sql"]) + assert.deepEqual(args.slice(0, 7), ["-X", "--no-password", "--single-transaction", "--set", "ON_ERROR_STOP=on", "-d", "fixture-db"]) + assert.equal(args[7], "-c"); assert.equal(args[8], partitionRestoreAdmissionSql) + assert.match(args[8], /pg_advisory_xact_lock_shared\(8723110::bigint\)/) + assert.match(args[8], /SET LOCAL lock_timeout='10s'/) + assert.equal(args[9], "-c"); assert.ok(args[10].includes("DROP CONSTRAINT")) + assert.deepEqual(args.slice(11), ["-f", "fixture.dump.sql"]) } return Buffer.alloc(0) }, @@ -31,6 +34,111 @@ test("archive decoding precedes one transactional psql execution with fail-fast assert.deepEqual(calls, ["pg_restore", "prepare", "pg_restore", "psql"]) }) +test("psql admission failure is not automatically retried", async () => { + let attempts = 0 + await assert.rejects(restorePartitionedDatabase("fixture with spaces.dump", "fixture-db", { + prepareSql: async () => {}, + command: async (command, args) => { + if (command === "psql") { attempts++; assert.equal(args[8], partitionRestoreAdmissionSql); throw Error("owned admission refused") } + return Buffer.from(toc) + }, + }), /owned admission refused/) + assert.equal(attempts, 1) +}) + +test("approved restore preserves the existing public schema and restores its other entries", async () => { + const entries = `8; 2615 2200 SCHEMA - public installation_writer\r\n9; 0 0 ACL - SCHEMA public installation_writer\r\n${toc}` + let selected = false + await restorePartitionedDatabase("fixture.dump", "fixture-db", { + prepareSql: async () => {}, + writePublicSchemaPreservingList: async (file, list) => { + assert.equal(file, "fixture.dump.list") + assert.equal(list, `;${entries}`) + selected = true + }, + command: async (command, args) => { + if (command === "pg_restore" && args[0] === "--list") return Buffer.from(entries) + assert.ok(selected) + if (command === "pg_restore") assert.deepEqual(args, ["--clean", "--if-exists", "--use-list", "fixture.dump.list", "--file", "fixture.dump.sql", "fixture.dump"]) + else assert.equal(command, "psql") + return Buffer.alloc(0) + }, + }) + assert.ok(selected) +}) + +test("decoded archive cannot reach psql after an asynchronous quarantine failure", async () => { + const calls: string[] = [] + await assert.rejects(restorePartitionedDatabase("fixture.dump", "fixture-db", { + prepareSql: async () => {}, + beforeTargetReplacement: async () => { + await Promise.resolve() + calls.push("quarantine") + throw Error("quarantine denied") + }, + command: async command => { + calls.push(command) + return Buffer.from(toc) + }, + }), /quarantine denied/) + assert.deepEqual(calls, ["pg_restore", "pg_restore", "quarantine"]) +}) + +test("the target witness brackets the decode on the same psql transaction", async () => { + const proof = "SELECT 'owned target witness'" + await restorePartitionedDatabase("fixture.dump", "fixture-db", { + prepareSql: async () => {}, + targetCheckSql: proof, + command: async (command, args) => { + if (command === "psql") { + assert.deepEqual(args.slice(7, 11), ["-c", proof, "-c", partitionRestoreAdmissionSql]) + assert.deepEqual(args.slice(-4), ["-f", "fixture.dump.sql", "-c", proof]) + } + return Buffer.from(toc) + }, + }) +}) + +test("quarantined restore takes the exclusive writer lock and rechecks the witness before DDL", async () => { + const proof = "SELECT 'owned maintenance witness'" + await restorePartitionedDatabase("fixture.dump", "fixture-db", { + prepareSql: async () => {}, + exclusivePartitionWriters: true, + targetCheckSql: proof, + command: async (command, args) => { + if (command === "psql") { + assert.deepEqual(args.slice(7, 10), ["-c", proof, "-c"]) + assert.match(args[10], /pg_catalog\.pg_advisory_xact_lock\(8723110::bigint\)/) + assert.deepEqual(args.slice(11, 14), ["-c", proof, "-c"]) + assert.match(args[14], /DROP CONSTRAINT/) + assert.deepEqual(args.slice(-4), ["-f", "fixture.dump.sql", "-c", proof]) + } + return Buffer.from(toc) + }, + }) +}) + +test("protected restore repairs native inherited primary keys before the final target check", async () => { + const proof = "SELECT 'target witness'" + await restorePartitionedDatabase("fixture.dump", "fixture-db", { + prepareSql: async () => {}, + writePublicSchemaPreservingList: async () => {}, + exclusivePartitionWriters: true, + targetCheckSql: proof, + command: async (command, args) => { + if (command === "psql") { + const script = args.indexOf("-f"), repair = args[script + 3] + assert.deepEqual(args.slice(script, script + 3), ["-f", "fixture.dump.sql", "-c"]) + assert.match(repair, /p\.relname IN \('server_snapshots'\)/) + assert.match(repair, /DROP CONSTRAINT %I RESTRICT/) + assert.match(repair, /NOT \(c\.connoinherit OR c\.conislocal\)/) + assert.deepEqual(args.slice(-2), ["-c", proof]) + } + return Buffer.from(toc) + }, + }) +}) + test("invalid or undecodable archive never starts target SQL", async () => { for (const phase of ["list", "decode"]) { let executions = 0 @@ -45,3 +153,15 @@ test("invalid or undecodable archive never starts target SQL", async () => { assert.equal(executions, 0) } }) + +test("control namespace archive refuses immediately after TOC before decoding or target replacement", async () => { + for (const entry of ["SCHEMA - mmapp_control owner", "TABLE mmapp_control database_schema_state owner", "COMMENT - SCHEMA mmapp_control owner", 'ACL - SCHEMA "MMAPP_CONTROL" owner']) { + const calls: string[] = [] + await assert.rejects(restorePartitionedDatabase("fixture.dump", "fixture-db", { + prepareSql: async () => { calls.push("prepare") }, + beforeTargetReplacement: () => { calls.push("replacement") }, + command: async (command, args) => { calls.push(command + " " + args[0]); return Buffer.from(toc + "4; 2615 4 " + entry + "\n") }, + }), /служебное пространство/) + assert.deepEqual(calls, ["pg_restore --list"]) + } +}) diff --git a/backend/src/services/system-database-partition-restore.ts b/backend/src/services/system-database-partition-restore.ts index ac04b475..3590877d 100644 --- a/backend/src/services/system-database-partition-restore.ts +++ b/backend/src/services/system-database-partition-restore.ts @@ -1,7 +1,14 @@ import { PARTITION_SPECS } from "../db/partitions.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" +import { assertDatabaseControlArchiveBoundary } from "./database-control-archive-boundary.js" +import { restoreManagedPartitionPrimaryKeysSql } from "./database-partition-restore-keys.js" -/** Only application parents actually present in the archive may be prepared. */ -export function partitionRestorePrelude(toc: string): string { +// --single-transaction begins on the actual psql backend BEFORE this first +// command. Admission precedes prelude/archive DDL and COPY on that same session. +export const partitionRestoreAdmissionSql = `SET LOCAL lock_timeout='10s'; SET LOCAL statement_timeout='600s'; SELECT pg_advisory_xact_lock_shared(${PARTITION_WRITER_ADVISORY_KEY}::bigint);` +const partitionRestoreExclusiveAdmissionSql = `SET LOCAL lock_timeout='10s'; SET LOCAL statement_timeout='600s'; SELECT pg_catalog.pg_advisory_xact_lock(${PARTITION_WRITER_ADVISORY_KEY}::bigint);` + +function managedPartitionParents(toc: string) { const tables = new Set() for (const line of toc.split(/\r?\n/)) { const match = /^\d+;\s+\d+\s+\d+\s+TABLE public ([a-z_][a-z_0-9]*)\s/.exec(line) @@ -10,7 +17,12 @@ export function partitionRestorePrelude(toc: string): string { if (!tables.has("servers") || !tables.has("schema_migrations")) { throw new Error("Архив не содержит обязательные таблицы базы приложения") } - const parents = PARTITION_SPECS.map(s => s.parent).filter(name => tables.has(name)) + return PARTITION_SPECS.map(s => s.parent).filter(name => tables.has(name)) +} + +/** Only application parents actually present in the archive may be prepared. */ +export function partitionRestorePrelude(toc: string): string { + const parents = managedPartitionParents(toc) if (!parents.length) return "SELECT 1;" return `DO $mm_restore$ DECLARE r record; @@ -32,16 +44,42 @@ END $mm_restore$;` interface Dependencies { command: (command: string, args: string[]) => Promise prepareSql: (file: string) => Promise + beforeTargetReplacement?: () => void | Promise + targetCheckSql?: string + exclusivePartitionWriters?: boolean + writePublicSchemaPreservingList?: (file: string, list: string) => Promise } export async function restorePartitionedDatabase(file: string, databaseUrl: string, deps: Dependencies): Promise { const toc = await deps.command("pg_restore", ["--list", file]) - const prelude = partitionRestorePrelude(toc.toString("utf8")) + assertDatabaseControlArchiveBoundary(toc) + const parsedToc = toc.toString("utf8") + const prelude = partitionRestorePrelude(parsedToc) const sqlFile = `${file}.sql` await deps.prepareSql(sqlFile) - // Fully decode before connecting to the target database; never rewrite archive SQL/data. - await deps.command("pg_restore", ["--clean", "--if-exists", "--file", sqlFile, file]) + const selectEntries: string[] = [] + if (deps.writePublicSchemaPreservingList) { + let schemaEntries = 0 + const list = parsedToc.replace(/^\d+;\s+\d+\s+\d+\s+SCHEMA - public(?: [^\r\n]+)?$/gm, entry => { + schemaEntries++ + return `;${entry}` + }) + if (schemaEntries > 1) throw new Error("Архив содержит несколько определений схемы public") + if (schemaEntries === 1) { + const listFile = `${file}.list` + await deps.writePublicSchemaPreservingList(listFile, list) + selectEntries.push("--use-list", listFile) + } + } + // Keep the protected schema object itself and decode every other archive entry unchanged. + await deps.command("pg_restore", ["--clean", "--if-exists", ...selectEntries, "--file", sqlFile, file]) // psql wraps both -c and -f in ONE transaction and rolls back on a SQL error. - await deps.command("psql", ["-X", "--single-transaction", "--set", "ON_ERROR_STOP=on", - "-d", databaseUrl, "-c", prelude, "-f", sqlFile]) + await deps.beforeTargetReplacement?.() + const targetCheck = deps.targetCheckSql ? ["-c", deps.targetCheckSql] : [] + const admission = deps.exclusivePartitionWriters ? partitionRestoreExclusiveAdmissionSql : partitionRestoreAdmissionSql + // Recheck the owning session after a possibly blocking wait for writers. + const afterDrain = deps.exclusivePartitionWriters ? targetCheck : [] + const postlude = deps.writePublicSchemaPreservingList ? ["-c", restoreManagedPartitionPrimaryKeysSql(managedPartitionParents(parsedToc))] : [] + await deps.command("psql", ["-X", "--no-password", "--single-transaction", "--set", "ON_ERROR_STOP=on", + "-d", databaseUrl, ...targetCheck, "-c", admission, ...afterDrain, "-c", prelude, "-f", sqlFile, ...postlude, ...targetCheck]) } diff --git a/backend/src/services/system-database-restore-execution.ts b/backend/src/services/system-database-restore-execution.ts index f65927ed..dc73626c 100644 --- a/backend/src/services/system-database-restore-execution.ts +++ b/backend/src/services/system-database-restore-execution.ts @@ -1,4 +1,4 @@ -export type SystemDatabaseRestoreOutcome={cleanupError?:string} +export type SystemDatabaseRestoreOutcome={cleanupError?:string;maintenanceError?:string;migrationError?:string;dataVerified?:true} interface Dependencies { allocate:()=>Promise write:(path:string,buffer:Buffer)=>Promise diff --git a/backend/src/services/system-database-restore-inspection.ts b/backend/src/services/system-database-restore-inspection.ts new file mode 100644 index 00000000..db42b148 --- /dev/null +++ b/backend/src/services/system-database-restore-inspection.ts @@ -0,0 +1,43 @@ +import { env } from "../config.js" +import { databaseRestoreQuarantineEnabled, readDatabaseRestoreQuarantineStatus } from "../db/database-restore-quarantine.js" +import { assertRuntimeDatabaseActivationCurrent, pool } from "../db/index.js" +import { withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { mcpStateDirectory } from "../modules/mcp/security.js" +import { databaseRestoreJournalHead, readMaintenanceRecoveryFacts, readPendingRestoreCompletion } from "./database-maintenance-startup.js" +import { DatabaseRestoreCheckpointRefused, readDatabaseRestoreCheckpointSnapshot, restoreJournalHeadsEqual } from "./database-restore-checkpoint.js" + +export async function inspectSystemDatabaseRestoreCheckpoint() { + if (!process.env.DATABASE_URL || !databaseRestoreQuarantineEnabled()) throw new DatabaseRestoreCheckpointRefused() + return withSystemDatabaseOperationLock(() => pool.connect(), async client => { + const directory = mcpStateDirectory() + const snapshot = await readDatabaseRestoreCheckpointSnapshot(directory, env.DATABASE_URL) + if (!snapshot) throw new DatabaseRestoreCheckpointRefused() + const { checkpoint } = snapshot + const quarantine = await readDatabaseRestoreQuarantineStatus(client) + const facts = await readMaintenanceRecoveryFacts(directory) + const localHead = facts.kind === "journal" && facts.authenticated ? databaseRestoreJournalHead(facts.journal) : null + const matches = restoreJournalHeadsEqual(quarantine.head, localHead) && restoreJournalHeadsEqual(checkpoint.head, localHead) + let state = "inspection-required" + let operationId: string | null = quarantine.activeOperationId ?? (checkpoint.status === "sealed" ? null : checkpoint.operationId) + if (quarantine.activeOperationId === null && matches && !facts.vault.unresolved && + !facts.operations.some(operation => operation.state !== "applied")) { + if (checkpoint.status === "sealed") state = "matches-sealed" + if (checkpoint.status === "pending") state = "matches-pending" + } + if (quarantine.activeOperationId && quarantine.head) { + const proof = await readPendingRestoreCompletion(directory, quarantine.activeOperationId, quarantine.head).catch(() => undefined) + if (proof && restoreJournalHeadsEqual(proof.appliedHead, localHead) && + proof.binding.runtimeTargetSha256 === checkpoint.runtimeTargetSha256 && proof.binding.activationSha256 === checkpoint.activationSha256) { + if (checkpoint.status === "sealed" && restoreJournalHeadsEqual(checkpoint.head, proof.appliedHead)) state = "applied-sealed" + if (checkpoint.status === "consumed" && checkpoint.operationId === quarantine.activeOperationId && + checkpoint.claimId === proof.binding.claimId && checkpoint.archiveSha256 === proof.binding.archiveSha256 && + checkpoint.dataSha256 === proof.binding.dataSha256 && restoreJournalHeadsEqual(checkpoint.head, proof.previousHead)) state = "applied-consumed" + } + } + if (state === "matches-sealed") operationId = facts.operations.at(-1)?.operationId ?? null + const current = await readDatabaseRestoreCheckpointSnapshot(directory, env.DATABASE_URL) + if (current?.sha256 !== snapshot.sha256) throw new DatabaseRestoreCheckpointRefused() + assertRuntimeDatabaseActivationCurrent() + return { state, operationId, checkpointStatus: checkpoint.status, checkpointSha256: snapshot.sha256, journalHead: localHead, quarantine } + }) +} diff --git a/backend/src/services/system-database-restore-recovery.ts b/backend/src/services/system-database-restore-recovery.ts new file mode 100644 index 00000000..b3bb7393 --- /dev/null +++ b/backend/src/services/system-database-restore-recovery.ts @@ -0,0 +1,100 @@ +import pg from "pg" +import { env } from "../config.js" +import { captureRuntimeRestoreData } from "../db/database-restore-data.js" +import { + completeDatabaseRestoreQuarantine, databaseRestoreQuarantineEnabled, readDatabaseRestoreJournalHead, +} from "../db/database-restore-quarantine.js" +import { assertRuntimeDatabaseActivationCurrent, pool, readRuntimeDatabaseWriterHead } from "../db/index.js" +import { withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { mcpStateDirectory } from "../modules/mcp/security.js" +import { readRestoreOwnerConnection } from "./database-dump-connection.js" +import { runtimeDatabaseTargetSha256 } from "./database-maintenance-resolution.js" +import { readPendingRestoreCompletion } from "./database-maintenance-startup.js" +import { databaseRestoreOwnerCheckSql, withDatabaseRestoreTarget } from "./database-restore-target.js" +import { readDatabaseRestoreCheckpoint, restoreJournalHeadsEqual, restoreWriterHeadIsCurrent, type RestoreCheckpoint } from "./database-restore-checkpoint.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" + +const sha256 = /^[a-f0-9]{64}$/ +const uuid = /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/ + +/** The operator runs these steps with the backend stopped. */ +export function completePendingSystemDatabaseRestore(operationId: string, appliedSha256: string): Promise { + return confirmPendingSystemDatabaseRestore("complete", operationId, appliedSha256) +} + +export function sealPendingSystemDatabaseRestore(operationId: string, appliedSha256: string): Promise { + return confirmPendingSystemDatabaseRestore("seal", operationId, appliedSha256) +} + +async function confirmPendingSystemDatabaseRestore(action: "complete" | "seal", operationId: string, appliedSha256: string): Promise { + const refused = () => new Error(action === "seal" + ? "Публикация контрольной точки PostgreSQL не подтверждена. Сверьте состояние и журнал перед повтором." + : "Снятие карантина PostgreSQL не подтверждено. Сверьте состояние и журнал перед повтором.") + const archiveSha256 = process.env.DATABASE_RESTORE_ARCHIVE_SHA256 + const dataSha256 = process.env.DATABASE_RESTORE_DATA_SHA256 + if (!process.env.DATABASE_URL || !databaseRestoreQuarantineEnabled() || !env.DATABASE_RESTORE_URL_FILE || + !uuid.test(operationId) || !sha256.test(appliedSha256) || !archiveSha256 || !sha256.test(archiveSha256) || + !dataSha256 || !sha256.test(dataSha256)) throw refused() + + try { + const owner = await readRestoreOwnerConnection(env.DATABASE_RESTORE_URL_FILE) + const stateDirectory = mcpStateDirectory() + const runtimeTarget = runtimeDatabaseTargetSha256(env.DATABASE_URL) + await withSystemDatabaseOperationLock(() => pool.connect(), (runtime, discard) => + withDatabaseRestoreTarget(runtime, discard, async targetSql => { + const client = new pg.Client({ connectionString: owner.connection, connectionTimeoutMillis: 8_000, statement_timeout: 10_000 }) + try { + await client.connect() + const checkSql = `${targetSql}\n${databaseRestoreOwnerCheckSql(owner.owner)}` + await client.query(checkSql) + const intentHead = await readDatabaseRestoreJournalHead(client, operationId) + if (!intentHead) throw refused() + const proof = await readPendingRestoreCompletion(stateDirectory, operationId, intentHead) + const { binding, appliedHead } = proof + if (appliedHead.sha256 !== appliedSha256 || binding.archiveSha256 !== archiveSha256 || binding.dataSha256 !== dataSha256 || + binding.runtimeTargetSha256 !== runtimeTarget || binding.activationSha256 !== (process.env.DATABASE_RUNTIME_ACTIVATION_SHA256 ?? null)) throw refused() + const checkpointFile = process.env.DATABASE_RESTORE_CHECKPOINT_FILE + const checkpoint = await readDatabaseRestoreCheckpoint(stateDirectory, env.DATABASE_URL) + let consumed: Extract | undefined + const assertSealedCheckpoint = (current: RestoreCheckpoint | undefined) => { + if ((binding.claimId || current) && (current?.status !== "sealed" || !restoreJournalHeadsEqual(current.head, appliedHead))) throw refused() + } + if (action === "seal") { + if (!checkpointFile || checkpoint?.status !== "consumed" || checkpoint.operationId !== operationId || + checkpoint.archiveSha256 !== archiveSha256 || checkpoint.dataSha256 !== dataSha256 || + checkpoint.claimId !== binding.claimId || !restoreJournalHeadsEqual(checkpoint.head, proof.previousHead)) throw refused() + consumed = checkpoint + } else assertSealedCheckpoint(checkpoint) + + // Hold this session's outer lock through the CAS, including after the snapshot releases its own acquisition. + await client.query("SET lock_timeout='10s'") + await client.query("SELECT pg_catalog.pg_advisory_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + const snapshot = await captureRuntimeRestoreData(client, () => { /* This connection is always closed. */ }, checkSql) + if (snapshot.sha256 !== dataSha256) throw refused() + if (action === "complete") { + if ((await client.query("BEGIN")).command !== "BEGIN") throw refused() + await client.query("SET LOCAL lock_timeout='5s'; SET LOCAL synchronous_commit=on") + } + await client.query(checkSql) + // A stopped writer may have appended applied but died before flushing it. + const current = await readPendingRestoreCompletion(stateDirectory, operationId, intentHead, true) + if (current.appliedHead.bytes !== appliedHead.bytes || current.appliedHead.sha256 !== appliedSha256) throw refused() + assertRuntimeDatabaseActivationCurrent() + const writerHead = await readRuntimeDatabaseWriterHead(runtime) + if (consumed && checkpointFile) { + if (!restoreJournalHeadsEqual(await readDatabaseRestoreJournalHead(client, operationId), intentHead)) throw refused() + if (consumed.writerHead && (!writerHead || !(writerHead.issuedAt > current.appliedAt))) throw refused() + const { publishSealedRestoreCheckpointFile } = await import("./database-restore-checkpoint-operator.js") + await publishSealedRestoreCheckpointFile(checkpointFile, consumed, appliedHead, writerHead) + return + } + const currentCheckpoint = await readDatabaseRestoreCheckpoint(stateDirectory, env.DATABASE_URL) + assertSealedCheckpoint(currentCheckpoint) + if (!restoreWriterHeadIsCurrent(currentCheckpoint?.writerHead, writerHead)) throw refused() + await completeDatabaseRestoreQuarantine(client, operationId, { expected: intentHead, next: appliedHead }) + await client.query(checkSql) + if ((await client.query("COMMIT")).command !== "COMMIT") throw refused() + } finally { await client.end() } + })) + } catch { throw refused() } +} diff --git a/backend/src/services/system-database-restore-staging.ts b/backend/src/services/system-database-restore-staging.ts new file mode 100644 index 00000000..2bf42c57 --- /dev/null +++ b/backend/src/services/system-database-restore-staging.ts @@ -0,0 +1,42 @@ +import { randomUUID } from "node:crypto" +import { env } from "../config.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" +import { assertRuntimeDatabaseActivationCurrent, pool, readRuntimeDatabaseWriterHead } from "../db/index.js" +import { withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { mcpStateDirectory } from "../modules/mcp/security.js" +import { assertDatabaseMaintenanceStartup, assertDatabaseRestoreJournalCurrent } from "./database-maintenance-startup.js" +import { + DatabaseRestoreCheckpointRefused, readDatabaseRestoreCheckpointSnapshot, restoreJournalHeadsEqual, restoreWriterHeadIsCurrent, +} from "./database-restore-checkpoint.js" +import { stagePendingRestoreCheckpointFile } from "./database-restore-checkpoint-operator.js" + +const sha256 = /^[a-f0-9]{64}$/ + +/** Publishes a fresh permit for the exact approved, currently sealed checkpoint. */ +export async function stageSystemDatabaseRestoreCheckpoint(expectedCurrentFileSha256: string): Promise { + const archiveSha256 = process.env.DATABASE_RESTORE_ARCHIVE_SHA256 + const dataSha256 = process.env.DATABASE_RESTORE_DATA_SHA256 + if (!process.env.DATABASE_URL || !databaseRestoreQuarantineEnabled() || !env.DATABASE_RESTORE_URL_FILE || + !sha256.test(expectedCurrentFileSha256) || !archiveSha256 || !sha256.test(archiveSha256) || !dataSha256 || !sha256.test(dataSha256)) { + throw new DatabaseRestoreCheckpointRefused() + } + const directory = mcpStateDirectory() + try { + return await withSystemDatabaseOperationLock(() => pool.connect(), async client => { + const snapshot = await readDatabaseRestoreCheckpointSnapshot(directory, env.DATABASE_URL) + if (!snapshot || snapshot.sha256 !== expectedCurrentFileSha256 || snapshot.checkpoint.status !== "sealed") { + throw new DatabaseRestoreCheckpointRefused() + } + await assertDatabaseRestoreQuarantineOpen(client) + await assertDatabaseMaintenanceStartup(directory, env.DATABASE_URL) + const head = await assertDatabaseRestoreJournalCurrent(client, directory) + if (!restoreJournalHeadsEqual(head, snapshot.checkpoint.head)) throw new DatabaseRestoreCheckpointRefused() + assertRuntimeDatabaseActivationCurrent() + const writerHead = await readRuntimeDatabaseWriterHead(client) + if (!restoreWriterHeadIsCurrent(snapshot.checkpoint.writerHead, writerHead)) throw new DatabaseRestoreCheckpointRefused() + const operationId = randomUUID() + await stagePendingRestoreCheckpointFile(snapshot.file, snapshot.checkpoint, { operationId, archiveSha256, dataSha256, writerHead }, snapshot.sha256) + return operationId + }) + } catch { throw new DatabaseRestoreCheckpointRefused() } +} diff --git a/backend/src/services/system-database-schema-readiness.test.ts b/backend/src/services/system-database-schema-readiness.test.ts new file mode 100644 index 00000000..6e679c90 --- /dev/null +++ b/backend/src/services/system-database-schema-readiness.test.ts @@ -0,0 +1,201 @@ +import assert from "node:assert/strict" +import Fastify from "fastify" +import { test } from "node:test" +import { readFile } from "node:fs/promises" +import { EventEmitter } from "node:events" +import { AsyncLocalStorage } from "node:async_hooks" +import os from "node:os" +import path from "node:path" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import { DatabaseRequestGate } from "./database-request-gate.js" +import * as operation from "./system-database-operation.js" +import * as restore from "./system-database-restore-execution.js" +import * as partitionRestore from "./system-database-partition-restore.js" +import * as maintenance from "../modules/mcp/database-maintenance.js" +import * as archiveBoundary from "./database-control-archive-boundary.js" + +// Execute the actual restore/export entry points with inert I/O providers. No +// configured pool, native subprocess, filesystem journal or network is opened. +async function fixture() { + const calls: string[] = [] + // Execute the actual shared singleton wiring in a fresh inert module. + const shared = {} as { sharedDatabaseGate: DatabaseRequestGate; databaseSchemaReadiness: ReturnType } + const sharedSource = await readFile(new URL("./shared-database-gate.ts", import.meta.url), "utf8") + const sharedCompiled = ts.transpileModule(sharedSource, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + runInNewContext(sharedCompiled, { exports: shared, require: (name: string) => { + if (name === "./database-request-gate.js") return { DatabaseRequestGate } + if (name === "./system-database-operation.js") return operation + throw Error(`Unapproved shared import ${name}`) + } }) + const { sharedDatabaseGate: gate, databaseSchemaReadiness } = shared + let failure: "decode" | "psql" | "migration" | "cleanup" | "journal-applied" | "journal-close" | "revoke" | undefined + const step = (name: string) => async () => { calls.push(name) } + const spawn = (command: string, args: string[]) => { + const child = new EventEmitter() as EventEmitter & { stdout: EventEmitter; stderr: EventEmitter; stdin: { end(): void } } + child.stdout = new EventEmitter(); child.stderr = new EventEmitter() + child.stdin = { end() { queueMicrotask(() => { + const phase = args.includes("--version") ? "version" : args.includes("--list") ? "list" : command === "psql" ? "psql" : args.includes("--clean") ? "decode" : "dump" + calls.push(phase) + if (phase === "dump") assert.deepEqual(Array.from(args), ["-Fc", archiveBoundary.DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG, "inert-owned-database"]) + if (failure === phase) { child.stderr.emit("data", Buffer.from(`synthetic ${phase} failure`)); child.emit("close", 1); return } + if (phase === "list") child.stdout.emit("data", Buffer.from("1; 1259 1 TABLE public servers owner\n2; 1259 2 TABLE public schema_migrations owner\n")) + child.emit("close", 0) + }) } } + return child + } + const files = { + open: async () => { let state = ""; return { writeFile: async (raw: string) => { state = JSON.parse(raw).state }, sync: async () => { if (failure === "journal-applied" && state === "applied") throw Error("synthetic applied fsync failure") }, close: async () => { if (failure === "journal-close") throw Error("synthetic close failure") } } }, + chmod: async () => {}, mkdir: async () => {}, writeFile: async () => {}, + rm: async () => { if (failure === "cleanup") throw Error("synthetic cleanup failure") }, + } + const modules: Record = { + "node:crypto": { randomUUID: () => "owned-inert-fixture" }, "node:async_hooks": { AsyncLocalStorage }, + pg: { Pool: class { constructor() { throw Error("Real SQL forbidden") } } }, + "node:fs/promises": files, "node:os": os, "node:path": path, "node:child_process": { spawn }, + "./bounded-database-dump.js": { + boundedDatabaseCommand: (command: string, args: string[]) => new Promise((resolve, reject) => { + const child = spawn(command, args), chunks: Buffer[] = [] + child.stdout.on("data", chunk => chunks.push(chunk)) + child.once("close", code => code === 0 ? resolve(Buffer.concat(chunks)) : reject(Error("Synthetic command failure"))) + child.stdin.end() + }), + }, + "../modules/events/service/events-service.js": { appendEvent: async () => {} }, + "./database-dump-connection.js": { + runPrivateDatabaseDump: async () => { throw Error("Unused private dump") }, + restoreDatabaseToolEnvironment: () => ({}), + validatePrivateRestoreConnection: () => {}, + withPrivateRestoreService: (_connection: string, work: (service: { dsn: string; env: object }) => Promise) => work({ dsn: "inert-owned-database", env: {} }), + }, + "./database-control-archive-boundary.js": archiveBoundary, + "./shared-database-gate.js": { sharedDatabaseGate: gate, databaseSchemaReadiness }, + "./config-mutation-policy.js": { assertDatabaseRestoreAllowed: async () => {} }, + "../modules/mcp/security.js": { accessGeneration: async () => "fixture", invalidateMcpAccess: async () => { if (failure === "revoke") throw Error("synthetic revoke failure") }, mcpStateDirectory: () => "inert-state" }, + "../modules/mcp/database-maintenance.js": maintenance, + "../db/migrate.js": { applySqlMigrations: async () => { calls.push("migration"); if (failure === "migration") throw Error("synthetic migration failure") } }, + "./system-database-restore-execution.js": restore, + "../config.js": { env: { DATABASE_URL: "inert-owned-database" } }, + "../db/index.js": { pool: { connect: () => { throw Error("Real SQL forbidden") } } }, + "../db/operation-lock.js": { withSystemDatabaseOperationLock: (_connect: unknown, work: (owner: object, discard: () => void) => unknown) => work({}, () => {}) }, + "../db/database-restore-quarantine.js": { databaseRestoreQuarantineEnabled: () => false }, + "./database-restore-target.js": { withDatabaseRestoreTarget: (_owner: unknown, _discard: unknown, work: (sql: string) => unknown) => work("SELECT 1") }, + "./system-database-operation.js": operation, + "./scheduler.js": { stopSchedulerForDatabaseOperation: step("stop scheduler"), restartSchedulerAfterDatabaseOperation: step("start scheduler") }, + "./traffic-flow-ripe.js": { pauseRipeBackgroundWork: step("pause ripe"), resumeRipeBackgroundWork: () => { calls.push("resume ripe") } }, + "./system-database-cache.js": { invalidateSystemDatabaseCaches: () => { calls.push("invalidate") } }, + "./system-database-partition-restore.js": partitionRestore, + "./traffic-flow-ingest.js": { stopTrafficFlowListenerForDatabaseOperation: step("stop listener"), restartTrafficFlowListenerAfterDatabaseOperation: step("start listener") }, + } + const exports = {} as { restoreSystemDatabaseBackup(buffer: Buffer): Promise>; exportSystemDatabaseBackup(): Promise } + const source = await readFile(new URL("./system-database-backup.ts", import.meta.url), "utf8") + const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, esModuleInterop: true } }).outputText + runInNewContext(compiled, { exports, require: (name: string) => { assert(name in modules, `Unapproved import ${name}`); return modules[name] }, Buffer, process: { env: {} }, Error, Date, Promise }, { filename: "system-database-backup.ts" }) + return { calls, exports, gate, fail: (phase?: typeof failure) => { failure = phase } } +} +const archive = Buffer.from("PGDMPinert") +const restartCalls = (calls: string[]) => calls.filter(c => ["start listener", "start scheduler", "resume ripe"].includes(c)) + +test("actual restore leaves consumers stopped after migration failure, including subsequent export", async () => { + const f = await fixture(); f.fail("migration") + const result = await f.exports.restoreSystemDatabaseBackup(archive) + assert.match(String(result.migrationError), /миграции/); assert.match(String(result.schedulerError), /не подтверждена/) + assert.equal(f.calls.filter(c => c === "psql").length, 1); assert.deepEqual(restartCalls(f.calls), []) + let dispatched = 0 + await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/) + assert.equal(dispatched, 0); assert.equal(f.gate.isPaused(), true) + let resumed = false + const waiter = f.gate.whenOpen().then(() => { resumed = true }) + f.fail(); await f.exports.exportSystemDatabaseBackup(); assert.deepEqual(restartCalls(f.calls), []) + await Promise.resolve(); assert.equal(resumed, false) + void waiter + await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/) + assert.equal(dispatched, 0) +}) +test("actual restore preserves unknown replacement failure without restarting or retrying", async () => { + const f = await fixture(); f.fail("psql") + await assert.rejects(f.exports.restoreSystemDatabaseBackup(archive), /Команда восстановления PostgreSQL psql не выполнена/) + assert.equal(f.calls.filter(c => c === "psql").length, 1); assert(!f.calls.includes("migration")); assert.deepEqual(restartCalls(f.calls), []) + assert.equal(f.gate.isPaused(), true) + let dispatched = 0 + await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/) + assert.equal(dispatched, 0) + // Repair uses the existing exclusive service boundary, outside request admission. + let resumed = false + const waiter = f.gate.whenOpen().then(() => { resumed = true }) + await Promise.resolve(); assert.equal(resumed, false) + f.fail("revoke"); await assert.rejects(f.exports.restoreSystemDatabaseBackup(archive), /synthetic revoke failure/) + await Promise.resolve(); assert.equal(resumed, false); assert.equal(f.calls.filter(c => c === "psql").length, 1) + await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/) + f.fail(); await f.exports.restoreSystemDatabaseBackup(archive) + await waiter; assert.equal(resumed, true); assert.equal(f.gate.isPaused(), false) + await f.gate.request(async () => { dispatched++ }); assert.equal(dispatched, 1) +}) +test("archive decoding failure before target replacement permits normal restart", async () => { + const f = await fixture(); f.fail("decode") + await assert.rejects(f.exports.restoreSystemDatabaseBackup(archive), /Команда восстановления PostgreSQL pg_restore не выполнена/) + assert(!f.calls.includes("psql")); assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"]) + await f.gate.request(async () => {}) +}) +test("acknowledged schema restarts consumers even when temporary cleanup fails", async () => { + const f = await fixture(); f.fail("cleanup") + const result = await f.exports.restoreSystemDatabaseBackup(archive) + assert.match(String(result.cleanupError), /временный файл/); assert.equal(result.migrationError, undefined) + assert(f.calls.indexOf("migration") < f.calls.indexOf("start listener")) + await f.gate.request(async () => {}) + assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"]) +}) +test("ordinary backup retains its restart behavior", async () => { + const f = await fixture(); await f.exports.exportSystemDatabaseBackup() + assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"]) +}) +test("a separate explicit restore clears the latch only after acknowledged migrations", async () => { + const f = await fixture(); f.fail("migration") + await f.exports.restoreSystemDatabaseBackup(archive); assert.deepEqual(restartCalls(f.calls), []) + f.calls.length = 0; f.fail() + const result = await f.exports.restoreSystemDatabaseBackup(archive) + assert.equal(result.migrationError, undefined); assert.equal(result.schedulerError, undefined) + assert(f.calls.indexOf("migration") < f.calls.indexOf("start listener")) + await f.gate.request(async () => {}) + assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"]) +}) + +for (const phase of ["journal-applied", "journal-close"] as const) test(`acknowledged replacement with ${phase} stays closed until explicit repair`, async () => { + const f = await fixture(); f.fail(phase) + const result = await f.exports.restoreSystemDatabaseBackup(archive) + assert.equal(typeof result.maintenanceError, "string"); assert.deepEqual(restartCalls(f.calls), []) + let dispatched = 0, resumed = false + await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/) + const waiting = f.gate.whenOpen().then(() => { resumed = true }) + await Promise.resolve(); assert.equal(resumed, false); assert.equal(dispatched, 0) + f.fail(); await f.exports.restoreSystemDatabaseBackup(archive); await waiting + await f.gate.request(async () => { dispatched++ }); assert.equal(dispatched, 1) +}) + +test("actual HTTP gate preserves only the existing backup/restore repair exclusions", async () => { + const f = await fixture(), app = Fastify() + const plugin = {} as { installDatabaseRequestGate(app: ReturnType): void } + const source = await readFile(new URL("../plugins/database-request-gate.ts", import.meta.url), "utf8") + const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + runInNewContext(compiled, { exports: plugin, require: (name: string) => { + if (name === "../services/shared-database-gate.js") return { sharedDatabaseGate: f.gate } + if (name === "../services/config-mutation-policy.js") return { withConfigurationActor: (_actor: string, work: () => unknown) => work() } + if (name === "../modules/mcp/database-request-lease.js") return { withDatabaseApiRequest: (work: () => unknown) => work() } + throw Error(`Unapproved HTTP adapter import ${name}`) + } }) + plugin.installDatabaseRequestGate(app) + let ordinarySql = 0 + app.get("/api/system/database/status", async () => { ordinarySql++; return { ok: true } }) + app.post("/api/system/database/restore", async () => f.exports.restoreSystemDatabaseBackup(archive)) + app.get("/api/system/database/backup", async () => f.exports.exportSystemDatabaseBackup()) + try { + f.fail("psql") + assert.equal((await app.inject({ method: "POST", url: "/api/system/database/restore" })).statusCode, 500) + assert.equal((await app.inject("/api/system/database/status")).statusCode, 503); assert.equal(ordinarySql, 0) + f.fail() + assert.equal((await app.inject("/api/system/database/backup")).statusCode, 200) + assert.equal((await app.inject("/api/system/database/status")).statusCode, 503); assert.equal(ordinarySql, 0) + assert.equal((await app.inject({ method: "POST", url: "/api/system/database/restore" })).statusCode, 200) + assert.equal((await app.inject("/api/system/database/status")).statusCode, 200); assert.equal(ordinarySql, 1) + } finally { await app.close() } +}) diff --git a/backend/src/services/terminal-diagnostic-client.ts b/backend/src/services/terminal-diagnostic-client.ts new file mode 100644 index 00000000..898b778c --- /dev/null +++ b/backend/src/services/terminal-diagnostic-client.ts @@ -0,0 +1,98 @@ +import http from "node:http" +import https from "node:https" +import { MikrotikClient } from "./mikrotik.js" +import type { MikrotikConnectParams } from "./mikrotik.js" +import type { TerminalDiagnosticClient } from "./terminal-diagnostics.js" +import { McpError } from "../modules/mcp/security.js" + +export const TERMINAL_NATIVE_MAX_BYTES = 1048576 +export const TERMINAL_NATIVE_MAX_TOTAL_BYTES = 8 * 1048576 +export const TERMINAL_NATIVE_MAX_REQUESTS = 256 +const reads = new Set(["/system/identity", "/system/resource", "/ip/address", "/interface", "/ip/route", "/ip/firewall/filter", "/routing/bgp/session", "/routing/ospf/neighbor", "/routing/ospf/area", "/routing/ospf/instance", "/routing/ospf/interface-template", "/routing/bfd/session", "/log?buffer=memory&.proplist=.id,time,topics,message"]) +const flagGets = new Set(["/routing/ospf/area/get", "/routing/ospf/instance/get", "/routing/ospf/interface-template/get"]) +const failure = (code: string, message: string) => new McpError(code, message, 409) +export interface TerminalDiagnosticReadOptions { signal?: AbortSignal; authorize: () => Promise; timeoutMs?: number } + +/** Retain shared MikrotikClient semantic readers, replacing only their transport. */ +class TerminalClient extends MikrotikClient { + readonly pending = new Set>() + readonly cancellation = new AbortController() + private readonly httpAgent = new http.Agent({ keepAlive: false, maxSockets: 2 }) + private readonly httpsAgent = new https.Agent({ keepAlive: false, maxSockets: 2 }) + private totalBytes = 0 + private requests = 0 + constructor(private readonly endpoint: MikrotikConnectParams, private readonly options: TerminalDiagnosticReadOptions) { super(endpoint) } + private check = async () => { this.cancellation.signal.throwIfAborted(); await this.options.authorize(); this.cancellation.signal.throwIfAborted() } + private dispatch(path: string, body?: Record): Promise { + const task = (async () => { + await this.check() + if (++this.requests > TERMINAL_NATIVE_MAX_REQUESTS) throw failure("READ_LIMIT", "Превышен бюджет запросов терминальной диагностики") + const value = await this.request(path, body) + await this.check() + return value + })() + this.pending.add(task) + void task.catch(() => this.cancellation.abort()).finally(() => this.pending.delete(task)) + return task + } + private request(path: string, body?: Record): Promise { + return new Promise((resolve, reject) => { + const signal = this.cancellation.signal + const payload = body ? Buffer.from(JSON.stringify(body)) : undefined + let problem: Error | undefined, result: unknown, ended = false, bytes = 0 + const chunks: Buffer[] = [] + const transport = this.endpoint.useSsl ? https : http + const request = transport.request({ hostname: this.endpoint.host, port: this.endpoint.port, path: `/rest${path}`, method: body ? "POST" : "GET", + auth: `${this.endpoint.username}:${this.endpoint.password}`, rejectUnauthorized: this.endpoint.verifySsl, + agent: this.endpoint.useSsl ? this.httpsAgent : this.httpAgent, signal, + headers: { accept: "application/json", ...(payload ? { "content-type": "application/json", "content-length": payload.length } : {}) } }, response => { + response.once("aborted", () => { problem ??= failure("DEVICE_UNAVAILABLE", "Ответ RouterOS прерван") }) + response.once("error", () => { problem ??= failure("DEVICE_UNAVAILABLE", "Ответ RouterOS прерван") }) + if (response.statusCode !== 200) { problem = failure("DEVICE_UNAVAILABLE", "RouterOS отказал в терминальной диагностике"); response.resume(); request.destroy(); return } + response.on("data", (chunk: Buffer) => { + bytes += chunk.length; this.totalBytes += chunk.length + if (bytes > TERMINAL_NATIVE_MAX_BYTES || this.totalBytes > TERMINAL_NATIVE_MAX_TOTAL_BYTES) { problem = failure("READ_LIMIT", "Ответ терминальной диагностики превышает предел"); chunks.length = 0; request.destroy(); return } + chunks.push(chunk) + }) + response.once("end", () => { + ended = true + try { + result = JSON.parse(Buffer.concat(chunks, bytes).toString("utf8")) + if (Array.isArray(result) && (result.length > 1000 || result.some(row => !row || typeof row !== "object" || Array.isArray(row)))) throw Error() + if (result === null || typeof result !== "object") throw Error() + } catch { problem ??= failure("STATE_UNAVAILABLE", "RouterOS вернул недостоверные данные диагностики") } + }) + }) + const timer = setTimeout(() => { problem ??= failure("READ_TIMEOUT", "Истёк срок терминальной диагностики"); request.destroy() }, path === "/tool/ping" ? 20000 : 10000) + request.once("error", error => { + const code = "code" in error && /^E[A-Z_]{1,50}$/.test(String(error.code)) ? String(error.code) : "unavailable" + problem ??= signal.aborted ? failure("READ_CANCELLED", "Терминальная диагностика отменена") : failure("DEVICE_UNAVAILABLE", `RouterOS недоступен для терминальной диагностики (${code})`) + }) + request.once("close", () => { clearTimeout(timer); if (problem || !ended) reject(problem ?? failure("DEVICE_UNAVAILABLE", "Ответ RouterOS не завершён")); else resolve(result) }) + request.end(payload) + }) + } + override async get(path: string): Promise { + if (!reads.has(path)) throw failure("UNSUPPORTED_CAPABILITY", "Ресурс отсутствует в allowlist терминальной диагностики") + return await this.dispatch(path) as T + } + override async post(path: string, body: Record): Promise { + const keys = Object.keys(body) + const getter = flagGets.has(path) && keys.length === 2 && keys.includes(".id") && keys.includes("value-name") && typeof body[".id"] === "string" && /^\*[a-z0-9]+$/i.test(body[".id"]) && ["disabled", ...(path.includes("interface-template") ? ["passive"] : [])].includes(String(body["value-name"])) + const membership = path === "/routing/ospf/interface-template/print" && JSON.stringify(body[".proplist"]) === '[".id"]' && [JSON.stringify(["passive"]), JSON.stringify(["passive", "#!"])].includes(JSON.stringify(body[".query"])) && keys.length === 2 + const ping = path === "/tool/ping" && keys.length === 3 && keys.every(key => ["address", "count", "interval"].includes(key)) && typeof body.address === "string" && /^\S{1,1019}$/.test(body.address) && /^(?:[1-9]|1[0-9]|20)$/.test(String(body.count)) && body.interval === "0.2s" + if (!getter && !membership && !ping) throw failure("UNSUPPORTED_CAPABILITY", "Команда отсутствует в allowlist терминальной диагностики") + return await this.dispatch(path, body) as T + } + async close() { this.cancellation.abort(); await Promise.allSettled([...this.pending]); this.httpAgent.destroy(); this.httpsAgent.destroy() } +} + +export async function withTerminalDiagnosticClient(endpoint: MikrotikConnectParams, options: TerminalDiagnosticReadOptions, work: (client: TerminalDiagnosticClient) => Promise): Promise { + const client = new TerminalClient(endpoint, options) + const abort = () => client.cancellation.abort() + const timer = setTimeout(abort, options.timeoutMs ?? 45000) + options.signal?.addEventListener("abort", abort, { once: true }) + if (options.signal?.aborted) abort() + try { await options.authorize(); return await work(client) } + finally { clearTimeout(timer); options.signal?.removeEventListener("abort", abort); await client.close() } +} diff --git a/backend/src/services/terminal-diagnostics.test.ts b/backend/src/services/terminal-diagnostics.test.ts new file mode 100644 index 00000000..9c22f360 --- /dev/null +++ b/backend/src/services/terminal-diagnostics.test.ts @@ -0,0 +1,48 @@ +import assert from "node:assert/strict" +import { setTimeout as delay } from "node:timers/promises" +import { MikrotikClient } from "./mikrotik.js" +import { dispatchTerminalCommand, executeTerminalDiagnostic, parseTerminalCommand, terminalDiagnosticSchema } from "./terminal-diagnostics.js" +import { withTerminalDiagnosticClient } from "./terminal-diagnostic-client.js" +import { createTerminalDiagnosticFixture, terminalDiagnosticCases } from "../test/terminal-diagnostic-fixture.js" + +const fixture = await createTerminalDiagnosticFixture(), endpoint = { host: "127.0.0.1", port: fixture.port, useSsl: false, verifySsl: false, username: "fixture", password: "NEVER_TERMINAL_CREDENTIAL" } +let checks = 0, allowed = true +const authorize = async () => { checks++; if (!allowed) throw Error("NEVER_TERMINAL_AUTHORITY_DETAIL") } +const client = new MikrotikClient(endpoint) +try { + for (const [raw, expected] of terminalDiagnosticCases) { + const parsed = parseTerminalCommand(raw); assert.equal(parsed.diagnostic, expected) + const before = fixture.requests.length + const bounded = await withTerminalDiagnosticClient(endpoint, { authorize }, native => executeTerminalDiagnostic(native, parsed)).catch(error => { throw new Error(`Terminal parity branch ${expected}`, { cause: error }) }) + assert.equal(bounded, await dispatchTerminalCommand(client, raw), raw) + if (expected === "help") assert.equal(fixture.requests.length, before) + } + assert.ok(checks > fixture.requests.length) + assert.deepEqual(fixture.requests.filter(row => row.path === "/tool/ping").at(-1)?.body, { address: "192.0.2.1", count: "20", interval: "0.2s" }) + for (const bad of [{ diagnostic: "execute", command: "/system reboot" }, { diagnostic: "interfaces", target: "host" }, { diagnostic: "logs", count: 2 }, { diagnostic: "ping" }, { diagnostic: "ping", target: "192.0.2.1", count: 21 }, { diagnostic: "logs", limit: 201 }]) assert.equal(terminalDiagnosticSchema.safeParse(bad).success, false) + for (const bad of ["/system reboot", ":execute /system resource print", "ip address print;system reboot", "log print limit=0", "ping host count=1junk"]) assert.throws(() => parseTerminalCommand(bad)) + const oldInterfaces = fixture.payloads["/interface"] + fixture.payloads["/interface"] = Array.from({ length: 1001 }, () => ({ name: "x" })) + await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize }, native => native.getInterfaces())) + fixture.payloads["/interface"] = [{ name: "x".repeat(1048577) }] + await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize }, native => native.getInterfaces()), error => error instanceof Error && !error.message.includes(endpoint.password)) + fixture.payloads["/interface"] = oldInterfaces + fixture.setBeforeResponse(async (_request, _response, path) => { if (path === "/routing/ospf/interface-template") allowed = false }) + const beforeRevoke = fixture.requests.length + await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize }, native => native.getOspfInterfaceTemplates())) + assert.equal(fixture.requests.length, beforeRevoke + 1, "Revocation after native response prevents flag getter/print dispatch") + allowed = true + let disconnected!: () => void + const closed = new Promise(resolve => { disconnected = resolve }) + fixture.setBeforeResponse(async (request, response) => { response.writeHead(200); response.write("["); request.socket.once("close", disconnected); return true }) + const cancellation = new AbortController() + const cancelled = withTerminalDiagnosticClient(endpoint, { authorize, signal: cancellation.signal }, native => native.getInterfaces()) + const rejected = assert.rejects(cancelled) + await delay(20); cancellation.abort(); await rejected; await closed + const timed = withTerminalDiagnosticClient(endpoint, { authorize, timeoutMs: 25 }, native => native.getInterfaces()) + await assert.rejects(timed) + const beforeAlreadyCancelled = fixture.requests.length, stopped = new AbortController(); stopped.abort() + await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize, signal: stopped.signal }, native => native.getInterfaces())) + assert.equal(fixture.requests.length, beforeAlreadyCancelled) + console.log("Typed terminal: all REST branches/aliases exact formatted parity; inherited OSPF/resource semantics, clamps, no CLI, bounded rows/bytes, cancellation/deadline and late authority fences") +} finally { await fixture.close() } diff --git a/backend/src/services/terminal-diagnostics.ts b/backend/src/services/terminal-diagnostics.ts new file mode 100644 index 00000000..317e82b4 --- /dev/null +++ b/backend/src/services/terminal-diagnostics.ts @@ -0,0 +1,290 @@ +import { z } from "zod" +import type { MikrotikClient } from "./mikrotik.js" +import { observedDurationMs, observedInteger } from "./ospf-observed-values.js" +import type { RosIpAddress, RosInterface, RosResource, RosIdentity, RosIpRoute, RosFirewallFilter, RosLogEntry, RosBgpSession, RosPingResult, RosOspfNeighbor, RosOspfArea, RosOspfInstance, RosOspfInterfaceTemplate, RosBfdSession } from "../types/server.js" +// ── string helpers ───────────────────────────────────────────────────────────── + +function p(s: string | undefined, len: number): string { + return (s?.trim() || "?").padEnd(len) +} +function flag(val: string | undefined, char: string): string { + return val === "true" ? char : val === "false" ? " " : "?" +} + +// ── formatters ───────────────────────────────────────────────────────────────── + +function fmtIpAddresses(addrs: RosIpAddress[]): string { + const hdr = [ + "Flags: X - disabled, I - invalid, D - dynamic", + " # ADDRESS NETWORK INTERFACE", + ] + const rows = addrs.map((a, i) => { + const f = `${flag(a.disabled,"X")}${flag(a.invalid,"I")}${flag(a.dynamic,"D")}` + return ` ${String(i).padStart(2)} ${f} ${p(a.address,18)} ${p(a.network,15)} ${a.interface}${a.comment ? ` ; ${a.comment}` : ""}` + }) + return [...hdr, ...rows].join("\n") +} + +function fmtInterfaces(ifaces: RosInterface[]): string { + const hdr = [ + "Flags: X - disabled, D - dynamic, R - running", + " # NAME TYPE MTU MAC-ADDRESS", + ] + const rows = ifaces.map((f, i) => { + const flags = `${flag(f.disabled,"X")} ${flag(f.running,"R")}` + const name = p(f.name, 20) + const type = p(f.type, 12) + const mtu = p(f["actual-mtu"] ?? f.mtu, 6) + const mac = f["mac-address"] ?? "" + return ` ${String(i).padStart(2)} ${flags} ${name} ${type} ${mtu} ${mac}${f.comment ? ` ; ${f.comment}` : ""}` + }) + return [...hdr, ...rows].join("\n") +} + +function fmtResource(r: RosResource, id: RosIdentity): string { + const kv = (k: string, v: string) => `${k.padStart(30)}: ${v}` + const mib = (b: string | undefined) => b && /^\d+$/.test(b) && Number.isSafeInteger(Number(b)) ? `${(parseInt(b) / 1024 / 1024).toFixed(1)} MiB` : "?" + return [ + kv("uptime", r["uptime"] ?? "?"), + kv("version", r["version"] ?? "?"), + kv("build-time", r["build-time"] ?? "?"), + kv("free-memory", mib(r["free-memory"])), + kv("total-memory", mib(r["total-memory"])), + kv("cpu", r["cpu"] ?? "?"), + kv("cpu-count", r["cpu-count"] ?? "?"), + kv("cpu-frequency", r["cpu-frequency"] ? `${r["cpu-frequency"]} MHz` : "?"), + kv("cpu-load", r["cpu-load"] ? `${r["cpu-load"]}%` : "?"), + kv("free-hdd-space", mib(r["free-hdd-space"])), + kv("total-hdd-space", mib(r["total-hdd-space"])), + kv("architecture-name",r["architecture-name"] ?? "?"), + kv("board-name", r["board-name"] ?? "?"), + kv("platform", r["platform"] ?? "?"), + kv("identity", id.name ?? "?"), + ].join("\n") +} + +function fmtIpRoutes(routes: RosIpRoute[]): string { + const hdr = [ + "Flags: X - disabled, A - active, D - dynamic, C - connect, S - static,", + " r - rip, b - bgp, o - ospf, B - blackhole, U - unreachable, P - prohibit", + " # DST-ADDRESS PREF-SRC GATEWAY DIST", + ] + const rows = routes.map((r, i) => { + const f = [ + flag(r.active, "A"), + flag(r.dynamic, "D"), + flag(r.connect, "C"), + flag(r.static, "S"), + flag(r.bgp, "b"), + flag(r.ospf, "o"), + flag(r.blackhole, "B"), + flag(r.unreachable, "U"), + flag(r.prohibit, "P"), + ].join("") + const dst = p(r["dst-address"] ?? "", 18) + const pref = p(r["pref-src"] ?? "", 15) + const gw = p(r["gateway"] ?? r["interface"] ?? "", 16) + const dist = r["distance"] ?? "?" + return ` ${String(i).padStart(2)} ${f} ${dst} ${pref} ${gw} ${dist}` + }) + return [...hdr, ...rows].join("\n") +} + +function fmtBgpSessions(sessions: RosBgpSession[]): string { + const hdr = [ + "Flags: E - established", + " # NAME REMOTE-AS REMOTE-ADDRESS STATE UPTIME", + ] + const rows = sessions.map((s, i) => { + const est = s["established"] === "true" || s["state"] === "established" + const fl = est ? "E" : " " + const name = p(s["name"] ?? s[".id"], 23) + const remAs = p(s["remote.as"], 10) + const remIp = p((s["remote.address"] ?? "").split("/")[0], 22) + const state = est ? "established" : (s["state"] ?? "?") + const up = s["uptime"] ?? "—" + return ` ${String(i).padStart(2)} ${fl} ${name} ${remAs} ${remIp} ${p(state, 12)} ${up}` + }) + return [...hdr, ...rows].join("\n") +} + +function fmtOspfNeighbors(neighbors: RosOspfNeighbor[]): string { + const hdr = [ + "Flags: V - virtual", + " # ROUTER-ID STATE CHANGES ADJACENCY INTERFACE", + ] + const rows = neighbors.map((n, i) => { + return ` ${String(i).padStart(2)} ${p(n["router-id"],16)} ${p(n["state"],8)} ${p(n["state-changes"] ?? "?",8)} ${p(n["adjacency"] ?? "—",15)} ${n["interface"] ?? ""}` + }) + return [...hdr, ...rows].join("\n") +} + +function fmtOspfAreas(areas: RosOspfArea[]): string { + const hdr = [" # NAME AREA-ID TYPE INSTANCE"] + const rows = areas.map((a, i) => + ` ${String(i).padStart(2)} ${p(a.name,20)} ${p(a["area-id"] ?? "?",15)} ${p(a.type ?? "?",12)} ${a.instance}` + ) + return [...hdr, ...rows].join("\n") +} + +function fmtOspfInstances(insts: RosOspfInstance[]): string { + const hdr = [" # NAME ROUTER-ID VER DISTRIBUTE"] + const rows = insts.map((inst, i) => + ` ${String(i).padStart(2)} ${p(inst.name,15)} ${p(inst["router-id"],18)} ${p(inst.version ?? "?",4)} ${inst["redistribute"] ?? "—"}` + ) + return [...hdr, ...rows].join("\n") +} + +function fmtOspfIfaceTemplates(templates: RosOspfInterfaceTemplate[]): string { + const hdr = [" # INTERFACES AREA COST TYPE BFD"] + const rows = templates.map((t, i) => + ` ${String(i).padStart(2)} ${p(t.interfaces ?? "—",21)} ${p(t.area,19)} ${p(t.cost ?? "?",5)} ${p(t.type ?? "?",10)} ${t["use-bfd"] === "true" ? "yes" : t["use-bfd"] === "false" ? "no" : "?"}` + ) + return [...hdr, ...rows].join("\n") +} + +function fmtBfdSessions(sessions: RosBfdSession[]): string { + const hdr = [" # LOCAL-ADDRESS REMOTE-ADDRESS STATE UPTIME"] + const rows = sessions.map((s, i) => + ` ${String(i).padStart(2)} ${p(s["local-address"],27)} ${p(s["remote-address"],27)} ${p(s["state"] ?? "?",8)} ${s["uptime"] ?? "—"}` + ) + return [...hdr, ...rows].join("\n") +} + +function fmtFirewallFilter(rules: RosFirewallFilter[]): string { + const lines: string[] = ["Flags: X - disabled, I - invalid, D - dynamic"] + rules.forEach((r, i) => { + const f = `${flag(r.disabled,"X")}${flag(r.invalid,"I")}${flag(r.dynamic,"D")}` + lines.push(``) + lines.push(` ${String(i).padStart(2)} ${f}`) + if (r.comment) lines.push(` ;;; ${r.comment}`) + const parts = [`chain=${r.chain}`, `action=${r.action}`] + if (r.protocol) parts.push(`protocol=${r.protocol}`) + if (r["connection-state"])parts.push(`connection-state=${r["connection-state"]}`) + if (r["src-address"]) parts.push(`src-address=${r["src-address"]}`) + if (r["dst-address"]) parts.push(`dst-address=${r["dst-address"]}`) + if (r["src-address-list"])parts.push(`src-address-list=${r["src-address-list"]}`) + if (r["dst-address-list"])parts.push(`dst-address-list=${r["dst-address-list"]}`) + if (r["src-port"]) parts.push(`src-port=${r["src-port"]}`) + if (r["dst-port"]) parts.push(`dst-port=${r["dst-port"]}`) + if (r["in-interface"]) parts.push(`in-interface=${r["in-interface"]}`) + if (r["out-interface"]) parts.push(`out-interface=${r["out-interface"]}`) + if (r["tls-host"]) parts.push(`tls-host=${r["tls-host"]}`) + lines.push(` ${parts.join(" ")}`) + if (r.packets != null || r.bytes != null) lines.push(` packets=${r.packets ?? "?"} bytes=${r.bytes ?? "?"}`) + }) + return lines.join("\n") +} + +function fmtLogs(entries: RosLogEntry[]): string { + if (entries.length === 0) return "(no log entries)" + return entries.map(e => + `${(e.time ?? "").padEnd(20)} ${(e.topics ?? "").padEnd(28)} ${e.message ?? ""}` + ).join("\n") +} + +function fmtPing(results: RosPingResult[], host: string): string { + const lines = [`PING ${host}`] + // individual replies + for (const r of results) { + if (r.status === "timeout") { + lines.push(` seq=${r.seq??"?"} timeout`) + } else if(r.time!=null||r.ttl!=null) { + lines.push(` seq=${r.seq??"?"} ttl=${r.ttl ?? "?"} time=${r.time ?? "?"}`) + } + } + // summary — last item with sent/received fields + const sum = [...results].reverse().find(r => r.sent != null) + if (sum) { + const loss = sum["packet-loss"] ?? "?" + lines.push(` sent=${sum.sent} received=${sum.received??"?"} packet-loss=${loss}`) + const average = sum["avg-rtt"] + const received = typeof sum.received === "string" ? observedInteger(sum.received, 1) : null + lines.push(` avg-rtt=${received !== null && typeof average === "string" && observedDurationMs(average) !== null ? average : "?"}`) + } + return lines.join("\n") +} + +// ── help text ────────────────────────────────────────────────────────────────── + +const HELP_TEXT = [ + "Доступные команды (RouterOS REST API):", + " ip address print — IP-адреса", + " ip route print — таблица маршрутов", + " interface print — интерфейсы", + " system resource print — ресурсы системы", + " system identity print — имя роутера", + " ip firewall filter print — правила firewall", + " routing bgp session print — BGP-сессии", + " routing ospf neighbor print — OSPF-соседи", + " routing ospf area print — OSPF-зоны", + " routing ospf instance print — OSPF-инстансы", + " routing ospf interface print — OSPF-интерфейсы", + " routing bfd session print — BFD-сессии", + " log print [limit=N] — системный лог", + " ping [count=N] — пинг", +].join("\n") + +export const terminalDiagnosticNames = ["help", "addresses", "interfaces", "resources", "identity", "routes", "firewall_filters", "bgp_sessions", "ospf_neighbors", "ospf_areas", "ospf_instances", "ospf_interfaces", "bfd_sessions", "logs", "ping"] as const +export const terminalDiagnosticSchema = z.object({ + diagnostic: z.enum(terminalDiagnosticNames), + target: z.string().min(1).max(1019).regex(/^\S+$/).optional(), + count: z.number().int().positive().safe().max(20).optional(), + limit: z.number().int().positive().safe().max(200).optional(), +}).strict().superRefine((value, context) => { + if (value.diagnostic === "ping" ? !value.target || value.limit !== undefined : value.target !== undefined || value.count !== undefined || value.diagnostic !== "logs" && value.limit !== undefined) context.addIssue({ code: "custom", message: "Параметры не соответствуют выбранной диагностике" }) +}) +export type TerminalDiagnostic = z.infer +export type TerminalDiagnosticClient = Pick +export class ExecValidationError extends Error { readonly statusCode = 400 } + +/** REST grammar is intentionally an allowlist, never a RouterOS script. */ +export function parseTerminalCommand(raw: string): TerminalDiagnostic { + const cmd = raw.trim().replace(/^\/+/, ""), lower = cmd.toLowerCase().replace(/\s+/g, " ") + const commands: Record = { + "?": "help", help: "help", "ip address print": "addresses", "interface print": "interfaces", "system resource print": "resources", "system identity print": "identity", "ip route print": "routes", "ip firewall filter print": "firewall_filters", + "routing bgp session print": "bgp_sessions", "bgp session print": "bgp_sessions", "bgp peer print": "bgp_sessions", "routing ospf neighbor print": "ospf_neighbors", "ospf neighbor print": "ospf_neighbors", + "routing ospf area print": "ospf_areas", "ospf area print": "ospf_areas", "routing ospf instance print": "ospf_instances", "ospf instance print": "ospf_instances", "routing ospf interface print": "ospf_interfaces", "routing ospf interface-template print": "ospf_interfaces", "routing bfd session print": "bfd_sessions", "bfd session print": "bfd_sessions", + } + if (Object.hasOwn(commands, lower)) return { diagnostic: commands[lower] } + if (/^log print(?:\s|$)/.test(lower)) { + const match = lower.match(/^log print(?: limit=(\d+))?$/) + if (!match) throw new ExecValidationError("Используйте log print [limit=N].") + const limit = match[1] ? Number(match[1]) : 50 + if (!Number.isSafeInteger(limit) || limit < 1) throw new ExecValidationError("limit должен быть положительным целым числом.") + return { diagnostic: "logs", limit: Math.min(limit, 200) } + } + if (lower.split(" ")[0] === "ping") { + const parts = cmd.replace(/\s+/g, " ").split(" ") + if (parts.length < 2 || parts.length > 3) throw new ExecValidationError("Используйте ping [count=N].") + const match = parts[2]?.match(/^count=(\d+)$/i) + if (parts[2] && !match) throw new ExecValidationError("count должен быть положительным целым числом.") + const count = match ? Number(match[1]) : 4 + if (!Number.isSafeInteger(count) || count < 1) throw new ExecValidationError("count должен быть положительным целым числом.") + return { diagnostic: "ping", target: parts[1], count: Math.min(count, 20) } + } + throw new ExecValidationError("Команда не поддерживается REST терминалом. Введите help для списка.") +} + +/** Same presentation and semantic client methods for REST aliases and typed MCP. */ +export async function executeTerminalDiagnostic(client: TerminalDiagnosticClient, input: TerminalDiagnostic): Promise { + switch (input.diagnostic) { + case "help": return HELP_TEXT + case "addresses": return fmtIpAddresses(await client.getIpAddresses()) + case "interfaces": return fmtInterfaces(await client.getInterfaces()) + case "resources": { const [resource, identity] = await Promise.all([client.getResource(), client.getIdentity()]); return fmtResource(resource, identity) } + case "identity": return ` name: ${(await client.getIdentity()).name}` + case "routes": return fmtIpRoutes(await client.getIpRoutes()) + case "firewall_filters": return fmtFirewallFilter(await client.getFirewallFilters()) + case "bgp_sessions": return fmtBgpSessions(await client.getBgpSessions()) + case "ospf_neighbors": return fmtOspfNeighbors(await client.getOspfNeighbors()) + case "ospf_areas": return fmtOspfAreas(await client.getOspfAreas()) + case "ospf_instances": return fmtOspfInstances(await client.getOspfInstances()) + case "ospf_interfaces": return fmtOspfIfaceTemplates(await client.getOspfInterfaceTemplates()) + case "bfd_sessions": return fmtBfdSessions(await client.getBfdSessions()) + case "logs": return fmtLogs(await client.getLogs(input.limit ?? 50)) + case "ping": return fmtPing(await client.ping(input.target!, input.count ?? 4), input.target!) + } +} +export function dispatchTerminalCommand(client: TerminalDiagnosticClient, raw: string) { return executeTerminalDiagnostic(client, parseTerminalCommand(raw)) } + diff --git a/backend/src/services/traffic-collection-state.ts b/backend/src/services/traffic-collection-state.ts index 59758a6a..a8ef12ec 100644 --- a/backend/src/services/traffic-collection-state.ts +++ b/backend/src/services/traffic-collection-state.ts @@ -1,4 +1,4 @@ -import { and, eq, lte } from "drizzle-orm" +import { and, eq, inArray, lte } from "drizzle-orm" import { db } from "../db/index.js" import { trafficCollectionState, trafficSettings, type Server } from "../db/schema.js" @@ -20,8 +20,8 @@ export async function recordTrafficCollectionAttempt(server: Pick [s.serverId, s])), settings: settings[0] } } diff --git a/backend/src/services/traffic-collector.ts b/backend/src/services/traffic-collector.ts index 7109c78c..13912625 100644 --- a/backend/src/services/traffic-collector.ts +++ b/backend/src/services/traffic-collector.ts @@ -1,4 +1,7 @@ -import { and, asc, desc, eq, gte } from "drizzle-orm" +import { withPartitionWrite, isPartitionWriteOutcomeUnknown, PartitionWriteError, type PartitionWriteOptions } from "./partition-write-transaction.js" +import { and, asc, desc, eq, gte, lte } from "drizzle-orm" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { withApplicationCollectorLock } from "./config-mutation-lock.js" import { db, pool } from "../db/index.js" import { withTrafficCollectionLock } from "./traffic-collection-lock.js" import { servers, trafficSamples, trafficSettings } from "../db/schema.js" @@ -62,19 +65,23 @@ function toNum(raw: unknown): number { return parseTrafficCounter(raw) } +const trafficSettingsDefaults = { enabled: true, intervalSec: 30, retentionDays: 14 } + +async function readSettingsRow() { + return (await db.select().from(trafficSettings).where(eq(trafficSettings.id, 1)).limit(1))[0] +} + async function getSettingsRow() { - const row = (await db.select().from(trafficSettings).where(eq(trafficSettings.id, 1)).limit(1))[0] + const row = await readSettingsRow() if (row) return row const now = new Date().toISOString() await db.insert(trafficSettings).values({ id: 1, - enabled: true, - intervalSec: 30, - retentionDays: 14, + ...trafficSettingsDefaults, createdAt: now, updatedAt: now, }) - return (await db.select().from(trafficSettings).where(eq(trafficSettings.id, 1)).limit(1))[0] + return readSettingsRow() } async function readPreviousWave(serverId: number): Promise> { @@ -104,7 +111,11 @@ async function readPreviousWave(serverId: number): Promise [`${r.interfaceName}\0${r.peerPublicKey ?? ""}`, r])) } -export async function collectTrafficOnce(): Promise { +export async function collectTrafficOnce(partitionWriteOptions:PartitionWriteOptions={}): Promise { + return withApplicationCollectorLock(() => collectTrafficUnderApplicationLock(partitionWriteOptions)) +} + +async function collectTrafficUnderApplicationLock(partitionWriteOptions:PartitionWriteOptions={}): Promise { const sampledAt = new Date().toISOString() if (collecting) { return { @@ -117,11 +128,12 @@ export async function collectTrafficOnce(): Promise { } collecting = true try { - const result = await withTrafficCollectionLock(() => pool.connect(), assertActive => collectTrafficRun(sampledAt,assertActive)) + const result = await withTrafficCollectionLock(() => pool.connect(), assertActive => collectTrafficRun(sampledAt,assertActive,partitionWriteOptions)) return result.acquired ? result.value : { v:SCHEDULER_RUN_SNAPSHOT_VERSION,job:"traffic",sampledAt,skipped:true,servers:[], } } catch (error) { + if (isPartitionWriteOutcomeUnknown(error)) throw error return {v:SCHEDULER_RUN_SNAPSHOT_VERSION,job:"traffic",sampledAt,servers:[], fatalError:error instanceof Error ? error.message : String(error)} } finally { @@ -129,7 +141,7 @@ export async function collectTrafficOnce(): Promise { } } -async function collectTrafficRun(sampledAt:string,assertActive:()=>void=()=>{}):Promise { +async function collectTrafficRun(sampledAt:string,assertActive:()=>void=()=>{},partitionWriteOptions:PartitionWriteOptions={}):Promise { const startedAt = Date.now() const now = sampledAt const snapshot: TrafficRunSnapshot = { @@ -234,7 +246,7 @@ async function collectTrafficRun(sampledAt:string,assertActive:()=>void=()=>{}): } await ensureSource() if (rows.length > 0) { - await db.insert(trafficSamples).values(rows) + await withPartitionWrite(async ({ db: writerDb }) => writerDb.insert(trafficSamples).values(rows), { ...partitionWriteOptions, authorize:async()=>{await partitionWriteOptions.authorize?.();await ensureSource()} }) } await ensureSource() await recordTrafficCollectionAttempt(srv, "ok", now, peerWarning) @@ -251,6 +263,7 @@ async function collectTrafficRun(sampledAt:string,assertActive:()=>void=()=>{}): } : {}), }) } catch (err) { + if (err instanceof PartitionWriteError) throw err assertActive() await recordTrafficCollectionAttempt(srv, "error", now, err instanceof Error ? err.message : String(err)) snapshot.servers.push({ @@ -271,6 +284,7 @@ async function collectTrafficRun(sampledAt:string,assertActive:()=>void=()=>{}): updatedAt: now, }).where(eq(trafficSettings.id, 1)) } catch (e) { + if (isPartitionWriteOutcomeUnknown(e)) throw e assertActive() const msg = e instanceof Error ? e.message : String(e) snapshot.fatalError = msg @@ -307,7 +321,24 @@ export async function getTrafficSettings() { return await getSettingsRow() } -export async function updateTrafficSettings(patch: { +export function toTrafficSettingsDto(settings?: typeof trafficSettings.$inferSelect) { + return { + enabled: settings?.enabled ?? trafficSettingsDefaults.enabled, + intervalSec: settings?.intervalSec ?? trafficSettingsDefaults.intervalSec, + retentionDays: settings?.retentionDays ?? trafficSettingsDefaults.retentionDays, + lastCollectedAt: settings?.lastCollectedAt ?? null, + lastDurationMs: settings?.lastDurationMs ?? null, + lastError: settings?.lastError || null, + } +} + +export async function readPublicTrafficSettings() { + const settings = toTrafficSettingsDto(await readSettingsRow()) + return { ...settings, collectorRunning: settings.enabled } +} + +export function updateTrafficSettings(patch: Parameters[0]) { return executeGuardedApplicationWrite({ kind: "traffic-settings", id: "1" }, "all", () => updateTrafficSettingsImpl(patch)) } +async function updateTrafficSettingsImpl(patch: { enabled?: boolean intervalSec?: number retentionDays?: number @@ -323,13 +354,19 @@ export async function updateTrafficSettings(patch: { return await getSettingsRow() } -export async function readServerSamplesInRange(serverId: number, sinceIso: string) { - const rows = await db.select() +export interface TrafficHistoryReadOptions { untilIso?: string; maxRows?: 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( eq(trafficSamples.serverId, serverId), gte(trafficSamples.sampledAt, sinceIso), + options.untilIso === undefined ? undefined : lte(trafficSamples.sampledAt, options.untilIso), )) - .orderBy(asc(trafficSamples.sampledAt)) + .orderBy(asc(trafficSamples.sampledAt)).$dynamic() + 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("История трафика превышает предел; выберите меньший диапазон") return rows.map((r) => ({ ...r, ...decodeTrafficSampleFlags(r.flags), continuityKnown: trafficCounterContinuity(r.flags) })) } diff --git a/backend/src/services/traffic-flow-active-rebuild-preload.mjs b/backend/src/services/traffic-flow-active-rebuild-preload.mjs new file mode 100644 index 00000000..c06f9c58 --- /dev/null +++ b/backend/src/services/traffic-flow-active-rebuild-preload.mjs @@ -0,0 +1,31 @@ +import assert from 'node:assert/strict' +import { existsSync } from 'node:fs' +import { join } from 'node:path' +import { isMainThread } from 'node:worker_threads' + +// The collector inherits this preload before it registers its own TS loader. +assert.equal(process.env.NODE_ENV, 'test') +assert.equal(process.env.MMAPP_OWN_FLOW_ACTIVE_REBUILD, 'true') +const database = new URL(process.env.DATABASE_URL ?? '') +assert.equal(database.hostname, '127.0.0.1') +assert.equal(database.username, 'mcp_test') +assert.equal(database.pathname, '/postgres') + +if (!isMainThread) { + const pauseFile = join(process.env.MCP_STATE_DIR, 'pause-flow-flush-timer') + const startInterval = globalThis.setInterval + globalThis.setInterval = (callback, ms, ...args) => { + // Only the collector installs a 2-second interval in this fixture. + if (ms !== 2000) return startInterval(callback, ms, ...args) + return startInterval((...values) => { + if (!existsSync(pauseFile)) callback(...values) + }, ms, ...args) + } +} + +const fetchLocal = globalThis.fetch.bind(globalThis) +globalThis.fetch = async (input, options) => { + const url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url) + if (url.hostname !== '127.0.0.1') throw new Error('Owned NetFlow fixture blocks external fetch') + return fetchLocal(input, { ...options, redirect: 'error' }) +} diff --git a/backend/src/services/traffic-flow-active-rebuild.owned.test.ts b/backend/src/services/traffic-flow-active-rebuild.owned.test.ts new file mode 100644 index 00000000..30d57b51 --- /dev/null +++ b/backend/src/services/traffic-flow-active-rebuild.owned.test.ts @@ -0,0 +1,396 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +import { flowAnalyticsDtoSchema, flowIngestQualitySchema } from "@mmapp/contracts/traffic-flow" +/** Real active main/Worker/UDP plus the production facts rebuild service. + * Only the owned disposable PostgreSQL runner may execute this entry. UDP + * transport is loopback. Public destination addresses exist in packet payloads + * only, and the worker preload blocks external HTTP. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createSocket } from "node:dgram" +import { mkdir, unlink, writeFile } from "node:fs/promises" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, join, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" + +async function main() { + assert.equal(process.env.MMAPP_OWN_FLOW_ACTIVE_REBUILD, "true") + assert.equal(process.env.NODE_ENV, "test") + const target = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(target.protocol)) + assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") + assert(target.password); assert.equal(target.pathname, "/postgres") + assert(Number.isInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) + assert.equal(target.search, ""); assert.equal(target.hash, "") + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) + assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const pauseFile = join(process.env.MCP_STATE_DIR, "pause-flow-flush-timer") + process.env.FLOW_LISTEN_HOST = "127.0.0.1" + process.env.FLOW_AGENT_SOCKET = "" + + const { pool, closePool } = await import("../db/index.js") + const { applySqlMigrations } = await import("../db/migrate.js") + const { ensurePartitionsAround, ensurePartitionFor } = await import("../db/partitions.js") + const ingest = await import("./traffic-flow-ingest.js") + const settings = await import("./traffic-flow-settings.js") + const { rebuildFlowFactsSafely, reconcileFlowFactsRebuild } = await import("./traffic-flow-facts-rebuild-service.js") + const { pauseRipeBackgroundWork, ripeFetchCountForTests } = await import("./traffic-flow-ripe.js") + const { buildApp } = await import("../index.js") + const { env } = await import("../config.js") + let app: Awaited> | undefined + const sender = createSocket("udp4") + const groups: string[] = [] + let fixtureError: unknown, ifaceGets = 0, mainStarted = false, serverId: number | undefined + const native = createServer((request, response) => { + try { + assert.equal(request.method, "GET"); assert.equal(request.url, "/rest/interface") + assert.equal(request.headers.authorization, "Basic " + Buffer.from("owned-rebuild:owned-password").toString("base64")) + ifaceGets++; response.setHeader("content-type", "application/json") + response.end(JSON.stringify([{ ".id": "*A", name: "gre-client", type: "gre", ifindex: "10" }, { ".id": "*1", name: "ether1", type: "ether", ifindex: "1" }])) + } catch (error) { fixtureError = error; response.destroy() } + }) + const authorize = async () => { if (fixtureError) throw fixtureError } + const until = async (predicate: () => boolean | Promise, description: string) => { + const deadline = performance.now() + 15000 + while (!await predicate()) { await authorize(); assert(performance.now() < deadline, description); await delay(20) } + } + const reservePort = async () => { + const socket = createSocket("udp4") + try { + await new Promise((done, reject) => { socket.once("error", reject); socket.bind(0, "127.0.0.1", done) }) + return socket.address().port + } finally { await new Promise(done => socket.close(done)) } + } + const assertPortFree = async (port: number) => { + const socket = createSocket("udp4") + try { await new Promise((done, reject) => { socket.once("error", reject); socket.bind(port, "127.0.0.1", done) }) } + finally { try { await new Promise(done => socket.close(done)) } catch { /* failed bind has no handle */ } } + } + const send = (port: number, bytes: number, sequence: number) => { + const buffer = Buffer.alloc(72) + buffer.writeUInt16BE(5, 0); buffer.writeUInt16BE(1, 2) + buffer.writeUInt32BE(Math.floor(Date.now() / 1000), 8); buffer.writeUInt32BE(sequence, 16) + buffer.set([10, 144, 0, 10], 24); buffer.set([8, 8, 8, 8], 28) + buffer.writeUInt16BE(10, 36); buffer.writeUInt16BE(1, 38) + buffer.writeUInt32BE(3, 40); buffer.writeUInt32BE(bytes, 44) + buffer.writeUInt16BE(49152, 56); buffer.writeUInt16BE(443, 58); buffer.writeUInt8(6, 62) + return new Promise((done, reject) => sender.send(buffer, port, "127.0.0.1", error => error ? reject(error) : done())) + } + const configuration = async () => (await pool.query(`SELECT to_jsonb(s) - ARRAY[ + 'last_datagram_at','last_exporter_ip','last_error','packets_received','updated_at'] AS value + FROM traffic_flow_settings s WHERE id=1`)).rows[0].value + const publication = async () => { + const result: Record = {} + for (const table of ["flow_hour_facts", "flow_daily_facts", "events", "flow_write_receipts"]) + result[table] = (await pool.query(`SELECT to_jsonb(t) AS value FROM ${table} t ${table === "flow_write_receipts" ? "WHERE kind='rebuild'" : ""} ORDER BY to_jsonb(t)::text`)).rows.map(row => row.value) + return result + } + const receiptSnapshot = async () => (await pool.query<{ + batch_id: string; kind: string; source_proof: string; result: Record; committed_at: string + }>("SELECT batch_id,kind,source_proof,result,committed_at::text FROM flow_write_receipts ORDER BY batch_id")).rows + const assertReceiptConservation = async (before: Awaited>, forbiddenJob?: string) => { + const after = await receiptSnapshot(), previous = new Map(before.map(row => [row.batch_id, row])) + const current = new Map(after.map(row => [row.batch_id, row])) + for (const row of before) assert.deepEqual(current.get(row.batch_id), row, "Every pre-existing receipt remains unchanged") + for (const row of after.filter(row => !previous.has(row.batch_id))) { + assert.equal(row.kind, "ingest", "Only independent active-writer ingest receipts may appear") + assert.notEqual(row.batch_id, forbiddenJob) + assert.match(row.batch_id, /^[a-f0-9-]{36}$/); assert.match(row.source_proof, /^[a-f0-9]{64}$/) + assert.deepEqual(Object.keys(row.result).sort(), ["buckets", "facts", "ingestQuality", "skippedInet"]) + assert.equal(flowIngestQualitySchema.parse(row.result.ingestQuality).localAdmission, "no-local-loss-observed") + for (const key of ["buckets", "facts", "skippedInet"]) { + assert(Number.isSafeInteger(row.result[key])); assert(Number(row.result[key]) >= 0) + } + assert(Number(row.result.facts) <= Number(row.result.buckets)) + } + if (forbiddenJob) assert.equal(current.has(forbiddenJob), false, "Failed rebuild must have no durable receipt") + } + const untouched = async () => { + const result: Record = {} + for (const table of ["app_users", "user_interface_bindings", "server_snapshots", "alert_outbox", "scheduler_runs", "flow_asn_meta"]) + result[table] = (await pool.query(`SELECT to_jsonb(t) AS value FROM ${table} t ORDER BY to_jsonb(t)::text`)).rows.map(row => row.value) + return result + } + try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + await ensurePartitionsAround(pool) + await new Promise(done => native.listen(0, "127.0.0.1", done)) + const nativePort = (native.address() as { port: number }).port + serverId = Number((await pool.query(`INSERT INTO servers(name,host,port,type,use_ssl,verify_ssl,username,password,enabled,wan_uplinks) + VALUES('owned-active-rebuild','127.0.0.1',$1,'jump-host',false,false,'owned-rebuild','owned-password',false,$2::jsonb) RETURNING id`, + [nativePort, JSON.stringify([{ iface: "ether1", ip: "127.0.0.1" }])])).rows[0].id) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('owned-rebuild-user','owned-rebuild-user','Owned rebuild user')") + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type) VALUES($1,'owned-rebuild-user',$2,'gre-client','gre')", [randomUUID(), serverId]) + const at = new Date(); at.setUTCMinutes(0, 0, 0) + const day = at.toISOString().slice(0, 10) + const unrelated = new Date(at.getTime() - 86400000) + await ensurePartitionFor(pool, "server_snapshots", "week", at) + await ensurePartitionFor(pool, "flow_hour_facts", "day", unrelated) + await ensurePartitionFor(pool, "flow_daily_facts", "month", unrelated) + await pool.query("INSERT INTO server_snapshots(server_id,polled_at,status,raw_interfaces) VALUES($1,$2,'online',$3::jsonb)", + [serverId, at, JSON.stringify([{ name: "gre-client", type: "gre", ifindex: "10" }, { name: "gre-overlay", type: "gre", ifindex: "11" }, { name: "ether1", type: "ether", ifindex: "1" }])]) + // The rebuild must exclude this GRE overlay row. Classified client traffic + // can only come from the real UDP worker. + 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,$2,'10.144.0.11','8.8.8.8',47,0,0,2000,9,'gre-overlay','ether1')`, [serverId, at]) + await pool.query(`INSERT INTO flow_hour_facts(server_id,bucket_at,iface,country,service,asn,bytes,packets) + VALUES($1,$2,'gre-client','DE','stale-target',64512,999,9),($1,$3,'gre-unrelated','DE','unrelated',64512,55,2)`, [serverId, at, unrelated]) + await pool.query(`INSERT INTO flow_daily_facts(server_id,day,iface,country,service,asn,bytes,packets) + VALUES($1,($2::timestamptz AT TIME ZONE 'UTC')::date,'gre-client','DE','stale-target',64512,999,9), + ($1,($3::timestamptz AT TIME ZONE 'UTC')::date,'gre-unrelated','DE','unrelated',64512,55,2)`, [serverId, at, unrelated]) + assert.deepEqual((await pool.query("SELECT day::text AS day FROM flow_daily_facts WHERE server_id=$1 AND service='unrelated'", [serverId])).rows, + [{ day: unrelated.toISOString().slice(0, 10) }], "Daily source fixture must use the same UTC day semantics as rebuild replacement") + await settings.getTrafficFlowSettingsRow() + const port = await reservePort() + // Keep the unrelated previous-day fixture comfortably inside raw retention. + // Capture this legitimate configuration below and require it stays intact. + await pool.query(`UPDATE traffic_flow_settings SET enabled=true,retention_hours=72,collector_ip='127.0.0.1',flow_listen_port=$1, + host_private_key='owned-rebuild-private-sentinel',host_public_key='owned-rebuild-public-sentinel',peers_json=$2::jsonb WHERE id=1`, + [port, JSON.stringify([{ serverId, name: "Owned rebuild peer", publicKey: "owned-rebuild-peer-sentinel", address: "127.0.0.1", allowedIps: ["127.0.0.1/32"] }])]) + settings.invalidateTrafficFlowSettingsCache() + const original = await configuration(), baseline = await untouched() + const observedDrains: Awaited>[] = [] + // Observe the actual production call and result; never substitute an IPC + // reply, worker, writer or drain outcome. + const authority = { serverIds: [serverId], authorize, drainExisting: async (check: () => Promise, ids: readonly number[]) => { + const result = await ingest.drainExistingTrafficFlowWriters(check, ids) + observedDrains.push(result) + return result + } } + const assertActive = () => { + assert.equal(ingest.getFlowListenerState().bound, true) + assert.equal(ingest.getFlowListenerState().address, `127.0.0.1:${port}`) + assert.equal(ingest.getFlowWorkerHealth().alive, true) + } + const assertCubes = async (bytes: number, packets: number) => { + const expected = [{ iface: "gre-client", country: "XX", service: "Google", asn: 0, bytes, packets }] + assert.deepEqual((await pool.query("SELECT iface,country::text,service,asn,bytes,packets FROM flow_hour_facts WHERE server_id=$1 AND (bucket_at AT TIME ZONE 'UTC')::date=$2", [serverId, day])).rows, expected) + assert.deepEqual((await pool.query("SELECT iface,country::text,service,asn,bytes,packets FROM flow_daily_facts WHERE server_id=$1 AND day=$2", [serverId, day])).rows, expected) + for (const table of ["flow_hour_facts", "flow_daily_facts"]) + assert.deepEqual((await pool.query(`SELECT bytes,packets FROM ${table} WHERE service='unrelated'`)).rows, [{ bytes: 55, packets: 2 }], `${table}: unrelated previous-day fixture must be conserved`) + } + const assertUdp = async (bytes: number, packets: number) => { + assert.deepEqual((await pool.query("SELECT coalesce(sum(bytes),0)::bigint AS bytes,coalesce(sum(packets),0)::bigint AS packets FROM flow_buckets WHERE server_id=$1 AND dst='8.8.8.8' AND proto=6", [serverId])).rows, [{ bytes, packets }]) + } + for (const table of ["flow_hour_facts", "flow_daily_facts"]) + assert.deepEqual((await pool.query(`SELECT bytes,packets FROM ${table} WHERE service='unrelated'`)).rows, [{ bytes: 55, packets: 2 }], `${table}: unrelated fixture exists before worker startup`) + await mkdir(dirname(pauseFile), { recursive: true }) + await writeFile(pauseFile, "", { flag: "wx" }) + mainStarted = true + await ingest.startTrafficFlowListener(authorize) + await until(() => ingest.getFlowListenerState().bound, "Production main-owned worker did not bind") + const { rememberServerIfaces } = await import("./traffic-flow-ifindex.js") + rememberServerIfaces(serverId, [{ name: "gre-client", ifindex: "10" }, { name: "ether1", ifindex: "1" }]) + await send(port, 1500, 1) + // The worker's first ingress resolves interfaces asynchronously, after + // the same packet has already been queued synchronously. + await until(() => ifaceGets === 1, "First UDP packet did not trigger a worker interface read") + await assertUdp(0, 0) + if (process.env.MMAPP_OWN_FLOW_ACTIVE_READ === "true") { + const drain = await ingest.drainExistingTrafficFlowWriters(authorize, [serverId]) + assert.equal(drain.existingWorker.receipt?.buckets, 1) + await assertUdp(1500, 3) + const { Client, StreamableHTTPClientTransport } = await import("@modelcontextprotocol/client") + const actor = { id: "owned-active-flow-read", email: "", name: "", apps: ["mm"], + permissions: ["mm:traffic:read", "mm:mcp:admin"], issuedAt: Date.now() - 1000, expiresAt: Date.now() + 300000 } + await (await import("../modules/mcp/store.js")).updateMcpSettings({ enabled: true }, actor) + const api = app = await buildApp({ startScheduler: false, logger: false }) + const origin = await api.listen({ host: "127.0.0.1", port: 0 }) + const ownerToken = api.jwt.sign({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }, { iss: env.authIssuer, expiresIn: "4m" }) + const created = await api.inject({ method: "POST", url: "/api/mcp/connections", headers: { authorization: "Bearer " + ownerToken }, + payload: { name: "Owned active flow read", mode: "observer", permissions: ["mm:traffic:read"], + expiresAt: new Date(Date.now() + 180000).toISOString(), + scope: { allowGlobal: false, serverIds: [serverId], operationIds: ["traffic.flow.analytics.read"] } } }) + assert.equal(created.statusCode, 200, created.body) + const sdk = new Client({ name: "owned-active-flow-read", version: "1.0.0" }) + try { + await sdk.connect(new StreamableHTTPClientTransport(new URL("/mcp", origin), + { requestInit: { headers: { authorization: "Bearer " + created.json().token } } })) + const receipts = await receiptSnapshot() + const rest = await api.inject({ method: "GET", url: `/api/traffic/flow/analytics?range=15m&serverId=${serverId}&iface=gre-client&dedup=false&excludeMesh=false&excludeOverlay=false`, + headers: { authorization: "Bearer " + ownerToken } }) + assert.equal(rest.statusCode, 200, rest.body) + const restAnalytics = flowAnalyticsDtoSchema.parse(rest.json()) + const mcp = await sdk.callTool({ name: "mm_netflow_analytics_report_get", + arguments: { serverId, minutes: 15, interface: "gre-client", dedup: false, excludeMesh: false, excludeOverlay: false } }) + assert(!mcp.isError, JSON.stringify(mcp.content)) + const { result } = mcp.structuredContent as { result: { serverId: number; analytics: Pick } } + assert.equal(result.serverId, serverId) + for (const analytics of [restAnalytics, result.analytics]) { + assert.equal(analytics.bytes, 1500) + assert.equal(analytics.packets, 3) + assert.equal(analytics.conversationsList.length, 1) + assert.equal(analytics.conversationsList[0].dst, "8.8.8.8") + assert.equal(analytics.conversationsList[0].inIface, "gre-client") + } + assertActive() + assert.deepEqual(await receiptSnapshot(), receipts) + assert.deepEqual(await configuration(), original) + assert.deepEqual(await untouched(), baseline) + console.log(JSON.stringify({ publicRestAnalytics: true, publicMcpAnalytics: true, udpBytes: 1500, exportedPackets: 3, + workerAliveAndBound: true, writeReceiptsUnchanged: true })) + } finally { await sdk.close() } + return + } + const first = await rebuildFlowFactsSafely(authority) + assert.equal(observedDrains.length, 1) + assert.equal(observedDrains[0].existingWorker.workerPresent, true) + assert.equal(first.ok, true); assert.equal(first.facts, 1); assert.equal(first.buckets, 2) + assert.deepEqual(first.days, [day]); assertActive(); await assertUdp(1500, 3); await assertCubes(1500, 3) + const ingestReceipts = await pool.query("SELECT result FROM flow_write_receipts WHERE kind='ingest'") + const firstIpcReceipt = observedDrains[0].existingWorker.receipt + assert(firstIpcReceipt) + assert.equal(firstIpcReceipt.buckets, 1) + const durable = (await pool.query("SELECT result FROM flow_write_receipts WHERE batch_id=$1 AND kind='ingest'", [firstIpcReceipt.batchId])).rows[0] + assert(durable); assert.equal(durable.result.buckets, 1) + assert.equal(ingestReceipts.rows.filter(row => row.result.buckets > 0).length, 1) + await unlink(pauseFile) + const known = await reconcileFlowFactsRebuild(first.jobId, first.configurationProof, authority) + assert.equal(known.committed, true); assert.deepEqual(known.result, first) + for (const proof of [first.configurationProof, first.classifierProof, first.resultProof]) assert.match(proof, /^[a-f0-9]{64}$/) + assert.equal(JSON.stringify(known).includes("8.8.8.8"), false) + assert.deepEqual(await reconcileFlowFactsRebuild(randomUUID(), first.configurationProof, authority), { committed: false, result: null }) + groups.push("active main/production Worker/loopback UDP persisted by real writer; genuine drain -> UDP raw row/frozen classifier -> hour/day replacement and durable rebuild receipt/reconcile") + + const once = await publication(), receiptsOnce = await receiptSnapshot() + assert.deepEqual(await rebuildFlowFactsSafely(authority, { jobId: first.jobId, expectedConfigurationProof: first.configurationProof }), first) + assert.equal(observedDrains.length, 1, "Matching rebuild receipt skips even the genuine drain call") + assert.deepEqual(await publication(), once); await assertReceiptConservation(receiptsOnce); assertActive() + await assertUdp(1500, 3) + groups.push("same durable rebuild job skips drain/republication while listener remains active; absent job reconciliation is read-only") + + app = await buildApp({ startScheduler: false, logger: false }) + const rebuildThroughRest = (permission: string) => app!.inject({ + method: "POST", + url: "/api/traffic/flow/rebuild-facts", + headers: { authorization: "Bearer " + app!.jwt.sign({ + sub: "owned-flow-rebuild-rest", apps: ["mm"], permissions: [permission], + }, { iss: env.authIssuer, expiresIn: "5m" }) }, + }) + const denied = await rebuildThroughRest("mm:traffic:read") + assert.equal(denied.statusCode, 403, denied.body) + assert.deepEqual(await publication(), once) + await send(port, 2500, 2) + await until(() => ingest.getFlowWorkerHealth().packetsReceived >= 2, "Active listener did not receive subsequent actual UDP") + const response = await rebuildThroughRest("mm:traffic:write") + assert.equal(response.statusCode, 200, response.body) + assert.deepEqual(response.json(), { ok: true, buckets: 2, facts: 1, days: [day] }) + const { rows: newRebuilds } = await pool.query( + "SELECT batch_id,source_proof FROM flow_write_receipts WHERE kind='rebuild' AND batch_id<>$1", [first.jobId]) + assert.equal(newRebuilds.length, 1) + const restRebuild = newRebuilds[0] + assert.equal((await reconcileFlowFactsRebuild(restRebuild.batch_id, restRebuild.source_proof, authority)).committed, true) + await assertUdp(4000, 6); await assertCubes(4000, 6); assertActive() + groups.push("production REST rebuild refuses read-only JWT, then publishes exactly one receipt with write JWT and conserves active UDP counters and unrelated day") + + // One-shot disposable SQL fault, scoped to this fixture's actual server/day. + // It verifies real hour INSERT happened before the daily INSERT failure. + await pool.query("UPDATE flow_hour_facts SET bytes=777 WHERE server_id=$1 AND (bucket_at AT TIME ZONE 'UTC')::date=$2", [serverId, day]) + await pool.query("UPDATE flow_daily_facts SET bytes=888 WHERE server_id=$1 AND day=$2", [serverId, day]) + const beforeFailure = await publication(), receiptsBeforeFailure = await receiptSnapshot(), failedJob = randomUUID() + const { rows: [daily] } = await pool.query("SELECT tableoid::regclass::text AS name FROM flow_daily_facts WHERE server_id=$1 AND day=$2 LIMIT 1", [serverId, day]) + const dailyPartition = daily?.name + assert.match(dailyPartition, /^flow_daily_facts_m_\d{6}$/) + await pool.query(`CREATE FUNCTION owned_active_rebuild_fail_daily() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN IF NEW.server_id=${serverId} AND NEW.day=DATE '${day}' AND NEW.iface='gre-client' AND NEW.service='Google' THEN + IF (SELECT sum(bytes) FROM flow_hour_facts WHERE server_id=NEW.server_id AND (bucket_at AT TIME ZONE 'UTC')::date=NEW.day) IS DISTINCT FROM 4000::bigint THEN RAISE EXCEPTION 'owned unexpected hour replacement'; END IF; + RAISE EXCEPTION 'owned active rebuild SQL fault after hour INSERT'; + END IF; RETURN NEW; END $$`) + await pool.query(`CREATE TRIGGER owned_active_rebuild_daily BEFORE INSERT ON ${dailyPartition} FOR EACH ROW EXECUTE FUNCTION owned_active_rebuild_fail_daily()`) + await assert.rejects(rebuildFlowFactsSafely(authority, { jobId: failedJob }), /owned active rebuild SQL fault after hour INSERT/) + assert.deepEqual(await publication(), beforeFailure, "Both cubes/events/rebuild receipts roll back despite real earlier hour INSERT") + await assertReceiptConservation(receiptsBeforeFailure, failedJob) + assertActive(); await assertUdp(4000, 6) + assert.deepEqual(await reconcileFlowFactsRebuild(failedJob, restRebuild.source_proof, authority), { committed: false, result: null }) + await pool.query(`DROP TRIGGER owned_active_rebuild_daily ON ${dailyPartition}`) + await pool.query("DROP FUNCTION owned_active_rebuild_fail_daily()") + const recovered = await rebuildFlowFactsSafely(authority, { jobId: failedJob }) + assert.equal((await reconcileFlowFactsRebuild(recovered.jobId, recovered.configurationProof, authority)).committed, true) + await assertCubes(4000, 6); assertActive() + groups.push("real SQL failure after hour publication rolls back cubes/event/receipt; read-only absence then retry succeeds with same active worker") + + const actor = { id: "owned-flow-rebuild-mcp", apps: ["mm"], permissions: ["mm:traffic:write", "mm:mcp:admin", "mm:mcp:approve"], + expiresAt: Date.now() + 300000, issuedAt: Date.now() - 1000 } + const { updateMcpSettings } = await import("../modules/mcp/store.js") + await updateMcpSettings({ enabled: true }, { ...actor, email: "", name: "" }) + const { Client, StreamableHTTPClientTransport } = await import("@modelcontextprotocol/client") + const sdk = new Client({ name: "owned-active-rebuild", version: "1.0.0" }) + try { + const origin = await app.listen({ host: "127.0.0.1", port: 0 }) + const jwt = app.jwt.sign({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }, { iss: env.authIssuer, expiresIn: "4m" }) + const owner = async (url: string, payload: object) => { + const reply = await app!.inject({ method: "POST", url, headers: { authorization: "Bearer " + jwt }, payload }) + assert.equal(reply.statusCode, 200, reply.body) + return reply.json() + } + const { connection, token } = await owner("/api/mcp/connections", { + name: "Active NetFlow rebuild", mode: "approval", permissions: ["mm:traffic:write"], + expiresAt: new Date(Date.now() + 180000).toISOString(), + scope: { allowGlobal: true, serverIds: [serverId], operationIds: ["traffic.flow.facts.rebuild", "changes.apply", "operations.read"] }, + }) + await sdk.connect(new StreamableHTTPClientTransport(new URL("/mcp", origin), { + requestInit: { headers: { authorization: "Bearer " + token } }, + }), { timeout: 5000 }) + const call = async (name: string, args: Record) => { + const reply = await sdk.callTool({ name, arguments: args }, undefined, { timeout: 10000 }) + assert(!reply.isError, JSON.stringify(reply.content)) + return (reply.structuredContent as { result: T }).result + } + const plan = await call<{ planId: string; state: string }>("mm_netflow_facts_rebuild_plan", {}) + assert.equal(plan.state, "awaiting-approval") + assert.deepEqual(await owner("/api/mcp/plans/" + plan.planId + "/decision", { approve: true }), { ok: true }) + await send(port, 1000, 3) + await until(() => ingest.getFlowWorkerHealth().packetsReceived >= 3, "MCP sample did not reach the worker") + const operation = await call<{ id: string }>("mm_change_apply", { planId: plan.planId, idempotencyKey: randomUUID() }) + await until(async () => !["pending", "running"].includes((await pool.query( + "SELECT state FROM mcp_operations WHERE id=$1 AND connection_id=$2", [operation.id, connection.id])).rows[0]?.state), + "MCP rebuild did not finish") + const terminal = await call<{ state: string; verification: string; plan_id: string; steps: { result: { jobId: string } }[] }>( + "mm_operation_get", { operationId: operation.id }) + assert.equal(terminal.state, "applied") + assert.equal(terminal.verification, "passed") + assert.equal(terminal.plan_id, plan.planId) + assert.equal((await pool.query("SELECT 1 FROM flow_write_receipts WHERE batch_id=$1 AND kind='rebuild'", [terminal.steps[0].result.jobId])).rowCount, 1) + await assertUdp(5000, 9); await assertCubes(5000, 9); assertActive() + groups.push("public MCP rebuild over SDK HTTP with owner approval, durable receipt and active UDP conservation") + } finally { await sdk.close() } + + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline) + assert.equal(ripeFetchCountForTests(), 0); assert(ifaceGets > 0); assert.equal(fixtureError, undefined) + await ingest.stopTrafficFlowListenerForDatabaseOperation(); mainStarted = false + assert.equal(ingest.getFlowWorkerHealth().alive, false); await assertPortFree(port) + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline); await assertUdp(5000, 9) + // A blocked external lookup can produce only the expected negative cache entry. + const { rows: ipMetadata } = await pool.query("SELECT prefix,asn,ok FROM flow_ip_meta") + assert(ipMetadata.length <= 1) + for (const row of ipMetadata) assert.deepEqual(row, { prefix: "8.8.8.8/32", asn: 0, ok: 0 }) + groups.push("tracked shutdown releases localhost UDP; settings/users/bindings/snapshots/notifier/scheduler rows preserved; only the fixture destination may have a negative cache entry") + console.log(JSON.stringify({ accepted: groups.length, groups, ifaceGets, syntheticUdpDatagrams: 3, classifiedUdpBytes: 5000, + genuineProductionServiceDrain: true, loopbackUdpOnly: true, + observedGenuineDrains: observedDrains.length, + firstIpcDrainPublishedNonemptyReceipt: true, + deterministicPendingIpcPublicationAccepted: true, + classifiedSource: "actual loopback UDP persisted by Worker", udpClassifiedFactsAccepted: true, + externalHttpBlocked: true, realRouterInteroperabilityAccepted: false, wholeInstallation2GbAcceptance: false })) + } finally { + try { + if (mainStarted) await ingest.stopTrafficFlowListenerForDatabaseOperation() + await pauseRipeBackgroundWork() + await pool.query("DROP FUNCTION IF EXISTS owned_active_rebuild_fail_daily() CASCADE") + } finally { + try { + await app?.close() + } finally { + try { + try { sender.close() } catch { /* no packet sent before fixture setup failure */ } + await new Promise((done, reject) => native.close(error => error && (error as NodeJS.ErrnoException).code !== "ERR_SERVER_NOT_RUNNING" ? reject(error) : done())) + } finally { await closePool() } + } + } + } +} + +await main() diff --git a/backend/src/services/traffic-flow-active-services.owned.test.ts b/backend/src/services/traffic-flow-active-services.owned.test.ts new file mode 100644 index 00000000..521a433d --- /dev/null +++ b/backend/src/services/traffic-flow-active-services.owned.test.ts @@ -0,0 +1,362 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +/** Production settings/purge with an active worker on owned PostgreSQL and + * localhost UDP/HTTP. The public profile exercises REST and MCP authorization. + * No service, Worker, IPC or SQL adapters are replaced. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createSocket } from "node:dgram" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client" + +async function main() { + assert.equal(process.env.MMAPP_OWN_FLOW_ACTIVE_SERVICES, "true") + assert.equal(process.env.NODE_ENV, "test") + const target = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(target.protocol)) + assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") + assert(target.password); assert.equal(target.pathname, "/postgres") + assert(Number.isInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) + assert.equal(target.search, ""); assert.equal(target.hash, "") + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) + assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.FLOW_LISTEN_HOST = "127.0.0.1" + // Inherit neither a production host-agent socket nor an alternative bind. + process.env.FLOW_AGENT_SOCKET = "" + + const { pool, closePool } = await import("../db/index.js") + const { applySqlMigrations } = await import("../db/migrate.js") + const { ensurePartitionsAround } = await import("../db/partitions.js") + const ingest = await import("./traffic-flow-ingest.js") + const settings = await import("./traffic-flow-settings.js") + const { updateTrafficFlowSettingsWithListener } = await import("./traffic-flow-settings-service.js") + const { purgeTrafficFlowWithEvent } = await import("./flow-purge-service.js") + const { flowPurgeTables } = await import("./traffic-flow-purge-execution.js") + const { pauseRipeBackgroundWork } = await import("./traffic-flow-ripe.js") + let app: Awaited> | undefined + const sender = createSocket("udp4") + let fixtureError: unknown, ifaceGets = 0, serverId: number | undefined, mainStarted = false + const groups: string[] = [] + const native = createServer((request, response) => { + try { + assert.equal(request.method, "GET"); assert.equal(request.url, "/rest/interface") + assert.equal(request.headers.authorization, "Basic " + Buffer.from("owned-active:owned-password").toString("base64")) + ifaceGets++; response.setHeader("content-type", "application/json"); response.end("[]") + } catch (error) { fixtureError = error; response.destroy() } + }) + const until = async (predicate: () => boolean | Promise, description: string, timeoutMs = 15000) => { + const deadline = performance.now() + timeoutMs + while (!await predicate()) { + if (fixtureError) throw fixtureError + assert(performance.now() < deadline, description) + await delay(20) + } + } + const reservePort = async () => { + const socket = createSocket("udp4") + try { + await new Promise((done, reject) => { socket.once("error", reject); socket.bind(0, "127.0.0.1", done) }) + return socket.address().port + } finally { await new Promise(done => socket.close(done)) } + } + const portFree = async (port: number) => { + const socket = createSocket("udp4") + try { await new Promise((done, reject) => { socket.once("error", reject); socket.bind(port, "127.0.0.1", done) }) } + finally { try { await new Promise(done => socket.close(done)) } catch { /* failed bind */ } } + } + // Private endpoints keep the genuine worker's RIPE enqueue path inactive. + const send = async (port: number, bytes: number, sequence: number) => { + const buffer = Buffer.alloc(72) + buffer.writeUInt16BE(5, 0); buffer.writeUInt16BE(1, 2) + buffer.writeUInt32BE(Math.floor(Date.now() / 1000), 8); buffer.writeUInt32BE(sequence, 16) + buffer.set([10, 145, 0, 10], 24); buffer.set([10, 145, 0, 20], 28) + buffer.writeUInt16BE(1, 36); buffer.writeUInt32BE(3, 40); buffer.writeUInt32BE(bytes, 44) + buffer.writeUInt16BE(49152, 56); buffer.writeUInt16BE(443, 58); buffer.writeUInt8(6, 62) + await new Promise((done, reject) => sender.send(buffer, port, "127.0.0.1", error => error ? reject(error) : done())) + } + const authorize = async () => { if (fixtureError) throw fixtureError } + const configuration = async () => (await pool.query(`SELECT to_jsonb(s) - ARRAY[ + 'last_datagram_at','last_exporter_ip','last_error','packets_received','updated_at'] AS value + FROM traffic_flow_settings s WHERE id=1`)).rows[0].value + const totals = async () => (await pool.query("SELECT coalesce(sum(bytes),0)::bigint AS bytes,coalesce(sum(packets),0)::bigint AS packets FROM flow_buckets WHERE server_id=$1", [serverId])).rows[0] + let inFlightSqlFlushes = 0 + const duringBlockedFlush = async (port: number, bytes: number, sequence: number, + start: () => { task: Promise; admitted: () => boolean | Promise }) => { + const blocker = await pool.connect() + let task: Promise<{ ok: true; value: T } | { ok: false; error: unknown }> | undefined + let outcome: Awaited, settled = false + try { + await blocker.query("BEGIN") + await blocker.query("SET LOCAL lock_timeout='3s'") + await blocker.query("LOCK TABLE flow_buckets IN ACCESS EXCLUSIVE MODE") + const blockerPid = Number((await blocker.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + await send(port, bytes, sequence) + // A real production flush must reach SQL and wait on our table lock. + // No heartbeat/pending-size assumption: heartbeats follow awaited flush. + await until(async () => Number((await pool.query(`SELECT count(*) AS n FROM pg_stat_activity + WHERE datname=current_database() AND usename='mcp_test' AND pid<>pg_backend_pid() + AND state='active' AND wait_event_type='Lock' + AND $1::int=ANY(pg_blocking_pids(pid)) + AND (query ~* 'INSERT INTO flow_buckets' OR query ~* 'PARTITION OF flow_buckets')`, [blockerPid])).rows[0].n) > 0, + "Genuine production worker SQL did not block at owned flow_buckets lock") + inFlightSqlFlushes++ + const active = start() + // Attach both outcomes before awaiting admission; no unhandled service + // rejection can strand the blocker or the production worker. + task = active.task.then(value => { settled = true; return { ok: true as const, value } }, error => { settled = true; return { ok: false as const, error } }) + await until(active.admitted, "Shared service did not enter while genuine worker SQL was in flight", 3000) + assert.equal(settled, false, "Shared service must not finish ahead of blocked actual worker SQL") + } finally { + // Release the genuine SQL blocker BEFORE joining a service that owns or + // waits for this worker. Terminal entry cleanup runs only after this join. + try { + try { await blocker.query("ROLLBACK") } finally { blocker.release() } + } finally { + if (task) outcome = await task + } + } + assert(outcome) + if (!outcome.ok) throw outcome.error + return outcome.value + } + const settingsDuringFlush = (port: number, bytes: number, sequence: number, + patch: Parameters[0]) => { + const receiptId = randomUUID() + return duringBlockedFlush(port, bytes, sequence, () => ({ + task: updateTrafficFlowSettingsWithListener(patch, { authorize, receiptId }), + admitted: async () => Number((await pool.query("SELECT count(*) AS n FROM flow_settings_intents WHERE receipt_id=$1 AND phase='configuration' AND state='claimed'", [receiptId])).rows[0].n) === 1, + })) + } + const intentReceipt = async (receipt: Awaited>) => { + assert.equal(receipt.status, "applied"); assert.equal(receipt.configurationConfirmed, true) + assert.equal(receipt.listener, "request-acknowledged"); assert.equal(receipt.listenerReadinessConfirmed, false) + assert.equal(receipt.receiptConfirmed, true); assert.equal(receipt.automaticReplay, false) + assert.equal(receipt.privateMaterialExported, false); assert.equal(receipt.attempts, 2) + assert.equal(receipt.retries, 0); assert.equal(receipt.listenerError, undefined) + const rows = (await pool.query("SELECT phase,state,result FROM flow_settings_intents WHERE receipt_id=$1 ORDER BY phase", [receipt.receiptId])).rows + assert.equal(rows.length, 2) + assert.deepEqual(rows.map(row => row.phase), ["configuration", "listener"]) + for (const row of rows) { + assert.equal(row.state, "completed"); assert.equal(row.result.receiptId, receipt.receiptId) + assert.equal(row.result.receiptConfirmed, true) + assert(!JSON.stringify(row.result).includes("owned-private-sentinel")) + } + } + try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + await ensurePartitionsAround(pool) + await new Promise(done => native.listen(0, "127.0.0.1", done)) + const nativePort = (native.address() as { port: number }).port + serverId = Number((await pool.query(`INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) + VALUES('owned-active-services','127.0.0.1',$1,false,false,'owned-active','owned-password',false) RETURNING id`, [nativePort])).rows[0].id) + await settings.getTrafficFlowSettingsRow() + // An unknown configuration column exercises the complete production CAS. + await pool.query("ALTER TABLE traffic_flow_settings ADD COLUMN owned_active_opaque jsonb NOT NULL DEFAULT '{\"preserve\":true}'::jsonb") + const port = await reservePort() + await pool.query(`UPDATE traffic_flow_settings SET enabled=true,collector_ip='127.0.0.1',flow_listen_port=$1, + host_private_key='owned-private-sentinel',host_public_key='owned-public-sentinel', + peers_json=$2::jsonb WHERE id=1`, [port, JSON.stringify([{ serverId, name: "Owned active peer", publicKey: "owned-peer-sentinel", address: "127.0.0.1", allowedIps: ["127.0.0.1/32"] }])]) + settings.invalidateTrafficFlowSettingsCache() + const original = await configuration() + const untouched = async () => (await pool.query(`SELECT + (SELECT count(*) FROM alert_outbox)::int AS alerts, + (SELECT count(*) FROM scheduler_runs)::int AS scheduler, + (SELECT count(*) FROM flow_ip_meta)::int AS ripe`)).rows[0] + const baseline = await untouched() + + mainStarted = true + await ingest.startTrafficFlowListener(authorize) + await until(() => ingest.getFlowListenerState().bound, "Initial production worker did not bind") + assert.deepEqual(await totals(), { bytes: 0, packets: 0 }) + if (process.env.MMAPP_OWN_FLOW_ACTIVE_PUBLIC === "true") { + const { buildApp } = await import("../index.js") + const { env } = await import("../config.js") + const actor = { id: "owned-active-public", email: "", name: "", apps: ["mm"], + permissions: ["mm:traffic:write", "mm:mcp:admin", "mm:mcp:approve"], expiresAt: Date.now() + 300000, issuedAt: Date.now() - 1000 } + await (await import("../modules/mcp/store.js")).updateMcpSettings({ enabled: true }, actor) + const api = app = await buildApp({ startScheduler: false, logger: false }) + const origin = await api.listen({ host: "127.0.0.1", port: 0 }) + const request = (method: "PUT" | "POST", url: string, permission: string[], payload?: object) => api.inject({ + method, url, payload, headers: { authorization: "Bearer " + api.jwt.sign({ + sub: actor.id, apps: actor.apps, permissions: permission, + }, { iss: env.authIssuer, expiresIn: "4m" }) }, + }) + const owner = async (url: string, payload: object) => { + const response = await request("POST", url, actor.permissions, payload) + assert.equal(response.statusCode, 200, response.body) + return response.json() + } + const { connection, token } = await owner("/api/mcp/connections", { + name: "Owned active NetFlow", mode: "approval", permissions: ["mm:traffic:write"], + expiresAt: new Date(Date.now() + 180000).toISOString(), + scope: { allowGlobal: true, serverIds: [serverId], + operationIds: ["netflow.settings.update", "traffic.flow.purge", "changes.apply", "operations.read"] }, + limits: { mutationsPerHour: 10000, objectsPerPlan: 10000 }, + }) + const sdk = new Client({ name: "owned-active-netflow", version: "1.0.0" }) + try { + await sdk.connect(new StreamableHTTPClientTransport(new URL("/mcp", origin), { + requestInit: { headers: { authorization: "Bearer " + token } }, + }), { timeout: 5000 }) + const call = async (name: string, args: Record) => { + const reply = await sdk.callTool({ name, arguments: args }, undefined, { timeout: 15000 }) + assert(!reply.isError, JSON.stringify(reply.content)) + return (reply.structuredContent as { result: T }).result + } + const execute = async (tool: string, input: Record, expectedVerification: "passed" | "inconclusive") => { + const plan = await call<{ planId: string; state: string }>(tool, input) + assert.equal(plan.state, "awaiting-approval") + await owner("/api/mcp/plans/" + plan.planId + "/decision", { approve: true }) + const operation = await call<{ id: string }>("mm_change_apply", { planId: plan.planId, idempotencyKey: randomUUID() }) + await until(async () => { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1 AND connection_id=$2", [operation.id, connection.id])).rows[0]?.state + return state && !["pending", "running"].includes(state) + }, tool + " did not finish", 30000) + const terminal = await call<{ id: string; plan_id: string; state: string; verification: string; error_code?: string; steps: { state: string; result: T }[] }>( + "mm_operation_get", { operationId: operation.id }) + assert.equal(terminal.id, operation.id) + assert.equal(terminal.plan_id, plan.planId) + assert.equal(terminal.state, "applied", terminal.error_code) + assert.equal(terminal.verification, expectedVerification, terminal.error_code) + assert.equal(terminal.steps.length, 1) + assert.equal(terminal.steps[0].state, expectedVerification === "passed" ? "verified" : "acknowledged") + return terminal.steps[0].result + } + const receive = async (bytes: number, seq: number) => { + const packets = ingest.getFlowWorkerHealth().packetsReceived + await send(port, bytes, seq) + await until(() => ingest.getFlowWorkerHealth().packetsReceived > packets, "Collector did not receive UDP") + await ingest.drainExistingTrafficFlowWriters(authorize, [serverId]) + } + const bound = () => until(() => ingest.getFlowListenerState().bound && ingest.getFlowWorkerHealth().alive, "Worker did not resume") + const settingsPath = "/api/traffic/flow/settings", purgePath = "/api/traffic/flow/purge" + const patch = { mapServiceMinSharePct: Number(original.map_service_min_share_pct) } + const eventCount = async () => Number((await pool.query("SELECT count(*) AS n FROM events WHERE event_type='traffic.flow.purge'")).rows[0].n) + await receive(1500, 1) + assert.equal((await request("PUT", settingsPath, ["mm:traffic:read"], patch)).statusCode, 403) + const restSettings = await request("PUT", settingsPath, actor.permissions, patch) + assert.equal(restSettings.statusCode, 200, restSettings.body) + assert.equal(restSettings.json().ok, true) + assert.equal(restSettings.json().listenerError, undefined) + await bound() + assert.deepEqual(await configuration(), original) + assert.deepEqual(await totals(), { bytes: 1500, packets: 3 }) + await intentReceipt(await execute>>( + "mm_netflow_settings_update_plan", { settings: patch }, "passed")) + await bound() + assert.deepEqual(await configuration(), original) + assert.deepEqual(await totals(), { bytes: 1500, packets: 3 }) + const assertPurged = async (events: number) => { + for (const table of Object.values(flowPurgeTables)) + assert.equal(Number((await pool.query(`SELECT count(*) AS n FROM ${table}`)).rows[0].n), 0, table) + assert.equal(await eventCount(), events) + await bound() + assert.deepEqual(await configuration(), original) + } + assert.equal((await request("POST", purgePath, ["mm:traffic:read"])).statusCode, 403) + assert.deepEqual(await totals(), { bytes: 1500, packets: 3 }) + const before = await eventCount() + const restPurge = await request("POST", purgePath, actor.permissions) + assert.equal(restPurge.statusCode, 200, restPurge.body) + const restResult = restPurge.json() + assert.equal(restResult.ok, true) + for (const field of ["listenerError", "partitionError", "eventError"]) assert.equal(restResult[field], undefined, field) + await assertPurged(before + 1) + await receive(2500, 2) + assert.deepEqual(await totals(), { bytes: 2500, packets: 3 }) + const purged = await execute>("mm_netflow_purge_plan", {}, "inconclusive") + assert.equal(purged.ok, true) + assert.equal(purged.configurationPreserved, true) + for (const field of ["listenerError", "partitionError", "eventError"]) assert.equal(purged[field], undefined, field) + await assertPurged(before + 2) + await receive(3500, 3) + assert.deepEqual(await totals(), { bytes: 3500, packets: 3 }) + assert.deepEqual(await untouched(), baseline) + console.log(JSON.stringify({ publicRestSettings: true, publicMcpSettings: true, publicRestPurge: true, + publicMcpPurge: true, purgeEvents: 2, postPurgeUdpBytes: 3500, localPostgres: true })) + } finally { await sdk.close() } + return + } + await intentReceipt(await settingsDuringFlush(port, 1500, 1, { enabled: false })) + assert.equal(ingest.getFlowListenerState().bound, false) + assert.equal(ingest.getFlowWorkerHealth().alive, false) + await portFree(port) + assert.deepEqual(await totals(), { bytes: 1500, packets: 3 }) + assert.deepEqual(await configuration(), { ...original, enabled: false }) + groups.push("production shared settings disable admitted during actual worker SQL flush, awaits retirement, conserves UDP and completes both durable phase receipts") + + await intentReceipt(await updateTrafficFlowSettingsWithListener({ enabled: true }, { authorize })) + await until(() => ingest.getFlowListenerState().bound, "Shared settings re-enable did not bind a real worker") + assert.deepEqual(await configuration(), original) + assert.deepEqual(await totals(), { bytes: 1500, packets: 3 }) + // Even an equal display setting must execute the complete production + // retire/flush/prune/start path. Readiness is proved separately via UDP. + const displayValue = Number(original.map_service_min_share_pct) + await intentReceipt(await settingsDuringFlush(port, 2500, 2, { mapServiceMinSharePct: displayValue })) + await until(() => ingest.getFlowListenerState().bound, "No-op display patch did not restore a real listener") + assert.deepEqual(await totals(), { bytes: 4000, packets: 6 }) + assert.deepEqual(await configuration(), original) + groups.push("production settings re-enable and display no-op admitted during real SQL flush; UDP conserved and complete opaque/key/peer configuration unchanged") + + assert.deepEqual(await totals(), { bytes: 4000, packets: 6 }) + const purgeEventsBefore = Number((await pool.query("SELECT count(*) AS n FROM events WHERE event_type='traffic.flow.purge'")).rows[0].n) + // No sender runs concurrently with deletion. Zero rows is a fixture + // checkpoint, not a promise that a live collector remains empty afterwards. + let drainedBeforeDelete = false, purgeAdmitted = false + const purged = await duringBlockedFlush(port, 3500, 3, () => ({ task: purgeTrafficFlowWithEvent({ + beforeDrain: async () => { purgeAdmitted = true }, + beforeDelete: async client => { + // Read-only observation inside the genuine delete transaction, after its + // real stop/drain. This hook replaces no SQL, writer, fence or receipt. + const row = (await client.query("SELECT coalesce(sum(bytes),0)::bigint AS bytes,coalesce(sum(packets),0)::bigint AS packets FROM flow_buckets WHERE server_id=$1", [serverId])).rows[0] + assert.deepEqual(row, { bytes: 7500, packets: 9 }); drainedBeforeDelete = true + }, + }), admitted: () => purgeAdmitted })) + assert.equal(drainedBeforeDelete, true) + assert.equal(purged.ok, true); assert.equal(purged.listenerError, undefined) + assert.equal(purged.partitionError, undefined); assert.equal(purged.eventError, undefined) + assert(purged.deleted.buckets > 0); assert(purged.deleted.minuteStats > 0) + for (const table of Object.values(flowPurgeTables)) + assert.equal(Number((await pool.query(`SELECT count(*) AS n FROM ${table}`)).rows[0].n), 0, table) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM events WHERE event_type='traffic.flow.purge'")).rows[0].n), purgeEventsBefore + 1) + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline) + await until(() => ingest.getFlowListenerState().bound, "Full shared purge did not restart a real listener") + assert.equal(ingest.getFlowWorkerHealth().alive, true) + groups.push("full production purge admitted during real SQL flush waits for worker drain, deletes all six flow tables, maintains partitions, restarts and journals one purge event") + + await send(port, 4500, 4) + await until(() => ingest.getFlowWorkerHealth().packetsReceived >= 1, "Post-purge worker did not publish actual UDP observation") + const drained = await ingest.drainExistingTrafficFlowWriters(authorize, [serverId]) + assert.equal(drained.existingWorker.workerPresent, true) + assert.deepEqual(await totals(), { bytes: 4500, packets: 3 }) + await ingest.stopTrafficFlowListenerForDatabaseOperation(); mainStarted = false + await portFree(port) + assert.equal(ingest.getFlowWorkerHealth().alive, false) + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline) + assert(ifaceGets > 0); assert.equal(fixtureError, undefined) + assert.equal(inFlightSqlFlushes, 3) + groups.push("post-purge production worker accepts new actual UDP/SQL; terminal tracked stop releases port and preserves configuration without scheduler/outbox/RIPE activity") + console.log(JSON.stringify({ accepted: groups.length, groups, ifaceGets, syntheticUdpDatagrams: 4, + beforePurgeBytes: 7500, afterPurgeBytes: 4500, settingsOperations: 3, inFlightSqlFlushes, + sharedProductionServices: true, publicRestMcpAuthorizationAccepted: false, realRouterInteroperabilityAccepted: false })) + } finally { + try { + if (mainStarted) await ingest.stopTrafficFlowListenerForDatabaseOperation() + await pauseRipeBackgroundWork() + if (serverId !== undefined) await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + } finally { + try { await app?.close() } finally { + try { sender.close() } catch { /* setup failed before first send */ } + try { await new Promise((done, reject) => native.close(error => error && (error as NodeJS.ErrnoException).code !== "ERR_SERVER_NOT_RUNNING" ? reject(error) : done())) } + finally { await closePool() } + } + } + } +} + +await main() diff --git a/backend/src/services/traffic-flow-analytics.ts b/backend/src/services/traffic-flow-analytics.ts index b18907d4..62848d06 100644 --- a/backend/src/services/traffic-flow-analytics.ts +++ b/backend/src/services/traffic-flow-analytics.ts @@ -1,6 +1,7 @@ -import { eq } from "drizzle-orm" -import { db, dbAll } from "../db/index.js" -import { appUsers, userInterfaceBindings } from "../db/schema.js" +import { flowReadDbAll } from "./traffic-flow-read-policy.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" import type { FlowAnalyticsDto, FlowBreakdownRow, @@ -22,7 +23,7 @@ import { } from "./traffic-flow-ingest.js" import { flowDataEpoch, MAX_PENDING, RING_OVERLAY } from "./traffic-flow-engine.js" import { resolveIfaceName } from "./traffic-flow-ifaces.js" -import { getTrafficFlowSettingsRow, listHostPeers } from "./traffic-flow-settings.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 { enqueueRipeMisses } from "./traffic-flow-ripe.js" @@ -94,7 +95,7 @@ function bump( async function userIfaceAllow(userId: string): Promise> | null> { if (!userId) return null - const binds = await db.select().from(userInterfaceBindings).where(eq(userInterfaceBindings.userId, userId)) + const binds = await flowReadDbAll<{serverId:number;interfaceName:string}>('SELECT server_id AS "serverId",interface_name AS "interfaceName" FROM user_interface_bindings WHERE user_id=?',[userId]) const allow = new Map>() for (const b of binds) { const set = allow.get(b.serverId) ?? new Set() @@ -140,7 +141,15 @@ function topLabel(map: Map { +export interface ScopedFlowAnalyticsRead { serverIds: readonly number[]; maxRows: number; authorize: () => Promise } +export async function buildFlowAnalytics(q: FlowAnalyticsQuery, scope?: ScopedFlowAnalyticsRead): Promise { + if (scope) { + if (!Number.isSafeInteger(q.serverId) || !scope.serverIds.includes(q.serverId!) || !Number.isSafeInteger(scope.maxRows) || scope.maxRows < 1 || scope.maxRows > 50000 || !Number.isSafeInteger(q.minutes) || q.minutes < 1 || q.minutes > 1440) throw new Error("Некорректная область аналитики NetFlow") + await scope.authorize() + const dto = await buildFlowAnalyticsUncached(q, scope) + await scope.authorize() + return dto + } const key = analyticsQueryKey(q) const now = Date.now() if (analyticsCache && analyticsCache.key === key && now - analyticsCache.at < ANALYTICS_CACHE_TTL_MS) { @@ -174,22 +183,29 @@ function analyticsQueryKey(q: FlowAnalyticsQuery): string { const ANALYTICS_CACHE_TTL_MS = 2000 let analyticsCache: { key: string; at: number; dto: FlowAnalyticsDto } | null = null -async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery): Promise { - const settings = await getTrafficFlowSettingsRow() +async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedFlowAnalyticsRead): Promise { + const authorize = scope?.authorize ?? (async () => {}) + await authorize() + const settings = scope ? await readFlowSourceLimits() : await getTrafficFlowSettingsRow() const top = Math.min(50, Math.max(10, settings.topN)) const windowSec = Math.max(60, q.minutes * 60) - const raw = await listFlowRowsForWindow(q.minutes) - const allow = q.userId ? await userIfaceAllow(q.userId) : null - const catalog = await getServerCatalog() + await authorize() + const raw = await listFlowRowsForWindow(q.minutes, q.serverId, { ...(scope ? { maxRows: scope.maxRows } : {}), untilIso: new Date().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) const nameById = new Map([...catalog.byId].map(([id, s]) => [id, s.name])) const countryById = new Map([...catalog.byId].map(([id, s]) => [id, s.country])) const ifaceFilter = q.iface && q.iface !== "__all__" ? q.iface : "" const wantDedup = q.dedup !== false && !ifaceFilter const excludeMesh = q.excludeMesh !== false const excludeOverlay = q.excludeOverlay !== false - const topo = await loadFlowTopology() + await authorize() + const topo = await loadFlowTopology(scope?.serverIds, authorize) + const allow = q.userId ? scope ? new Map([...topo.clientByIface.values()].filter(b => b.userId === q.userId).map(b => [b.serverId, new Set([...topo.clientByIface.values()].filter(other => other.userId === q.userId && other.serverId === b.serverId).map(other => other.interfaceName))])) : await userIfaceAllow(q.userId) : null + if (scope && q.userId && !allow?.size) throw new Error("Пользователь не найден в разрешённой топологии") - refreshFlowCatalogInBackground() + if (!scope) refreshFlowCatalogInBackground() const applications = new Map() const protocols = new Map() @@ -400,7 +416,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery): Promise { @@ -466,8 +482,13 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery): Promise { const runtime = await getFlowRuntimeCounters() - const rows = await listFlowRowsForWindow(minutes) + const inputs = await readFlowExporterInputs(minutes) + const rows = inputs.window.rows const { bytes, sessions } = summarizeByServer(rows) const ids = new Set([...bytes.keys()]) - for (const p of await listHostPeers()) ids.add(p.serverId) - const catalog = await getServerCatalog() + for (const id of inputs.configuredIds) ids.add(id) + const catalog = inputs.catalog const emptySeries = Array(60).fill(0) as number[] const exporters = catalog.list .filter((s) => ids.has(s.id)) @@ -566,15 +588,16 @@ export async function listFlowExporters(minutes: number): Promise { - const users = await db.select().from(appUsers) - const binds = await db.select().from(userInterfaceBindings) + const inputs = await readFlowClientInputs(minutes) + const users = inputs.users + const binds = users.flatMap(user => user.bindings.map(binding => ({ ...binding, userId: user.id }))) const byUser = new Map() for (const b of binds) { const list = byUser.get(b.userId) ?? [] list.push(b) byUser.set(b.userId, list) } - const rows = await listFlowRowsForWindow(minutes) + const rows = inputs.window.rows const emptySeries = Array(60).fill(0) as number[] const windowSec = Math.max(60, minutes * 60) const clients: FlowEntityCard[] = [] @@ -635,13 +658,13 @@ export function isFlowAnalyticsDegraded(): boolean { return health.pendingSize >= LIVE_DEGRADED_PENDING } -export async function safeBuildLiveFlowSample(q: Omit): Promise<{ +export async function safeBuildLiveFlowSample(q: Omit, scope?: ScopedFlowAnalyticsRead): Promise<{ event: "sample" | "error" data: unknown }> { return formatLiveSseFromBuilder(async () => { const skipHeavy = isFlowAnalyticsDegraded() - return await buildFlowAnalytics({ ...q, minutes: LIVE_ANALYTICS_MINUTES, skipHeavy }) + return await buildFlowAnalytics({ ...q, minutes: LIVE_ANALYTICS_MINUTES, skipHeavy }, scope) }) } @@ -665,7 +688,7 @@ function toBreakdown( ): FlowBreakdownRow[] { const denom = totalBytes || 1 return rows - .sort((a, b) => b.bytes - a.bytes) + .sort((a, b) => b.bytes - a.bytes || a.key.localeCompare(b.key)) .map((r) => ({ id: r.key, label: r.key, @@ -676,33 +699,41 @@ function toBreakdown( })) } -export async function getFlowMonthly(month: string, serverId?: number): Promise { +export async function getFlowMonthly(month: string, serverId?: number | readonly number[], options?: { maxKeys: number }): Promise { const bounds = flowMonthBounds(month) if (!bounds) { throw Object.assign(new Error("Некорректный месяц отчёта"), { statusCode: 400 }) } const params: Array = [bounds.start, bounds.end] let where = "day >= ? AND day < ? AND dim IN ('country', 'service', 'asn')" - if (serverId != null) { + if (Array.isArray(serverId)) { + if (serverId.some(id => !Number.isSafeInteger(id) || id <= 0)) throw new Error("Некорректная область месячного NetFlow") + if (serverId.length === 0) where += " AND false" + else { where += " AND server_id IN (" + serverId.map(() => "?").join(",") + ")"; params.push(...serverId) } + } else if (serverId != null) { where += " AND server_id = ?" - params.push(serverId) + params.push(serverId as number) } - const rows = await dbAll<{ dim: string; key: string; bytes: number; packets: 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) + 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 + ${options ? "LIMIT ?" : ""} `, params) + if (options && rows.length > options.maxKeys) 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 for (const row of rows) { - const rec = { key: row.key, bytes: Number(row.bytes) || 0, packets: Number(row.packets) || 0 } + const rec = { key: row.key, bytes: flowCounter(row.bytes), packets: flowCounter(row.packets) } if (row.dim === "country") { countries.push(rec) - bytes += rec.bytes + bytes = flowCounter(bytes + rec.bytes) } else if (row.dim === "service") services.push(rec) else if (row.dim === "asn") asns.push(rec) } @@ -710,12 +741,12 @@ export async function getFlowMonthly(month: string, serverId?: number): Promise< 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 = countries.reduce((a, r) => a + r.bytes, 0) || bytes || 1 + const countryTotal = flowCounterSum(countries.map(row => row.bytes)) || bytes || 1 return { month, bytes, countries: toBreakdown(countries, countryTotal, windowSec), - services: toBreakdown(services, services.reduce((a, r) => a + r.bytes, 0) || 1, windowSec), - asns: toBreakdown(asns, asns.reduce((a, r) => a + r.bytes, 0) || 1, windowSec), + services: toBreakdown(services, flowCounterSum(services.map(row => row.bytes)) || 1, windowSec), + asns: toBreakdown(asns, flowCounterSum(asns.map(row => row.bytes)) || 1, windowSec), } } diff --git a/backend/src/services/traffic-flow-batch-proof.test.ts b/backend/src/services/traffic-flow-batch-proof.test.ts new file mode 100644 index 00000000..a216b2fc --- /dev/null +++ b/backend/src/services/traffic-flow-batch-proof.test.ts @@ -0,0 +1,15 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { flowBatchProof } from "./traffic-flow-batch-proof.js" + +const payload=[{serverId:1,bytes:10}] +const before=JSON.stringify(payload) +const proof=flowBatchProof(payload,"facts") +assert.match(proof,/^[a-f0-9]{64}$/) +assert.equal(flowBatchProof(payload,"facts"),proof) +assert.notEqual(flowBatchProof(payload,"ingest"),proof) +assert.notEqual(flowBatchProof([{serverId:1,bytes:11}],"facts"),proof) +assert.equal(JSON.stringify(payload),before) +const source=await readFile(new URL("./traffic-flow-batch-proof.ts",import.meta.url),"utf8") +assert.deepEqual([...source.matchAll(/from\s+["']([^"']+)["']/g)].map(match=>match[1]),["node:crypto"]) +console.log("traffic-flow-batch-proof.test.ts: deterministic owned identity, domain/payload separation, nonmutation; sole dependency node:crypto, no MCP lifecycle/filesystem/classifier/SQL/network") diff --git a/backend/src/services/traffic-flow-batch-proof.ts b/backend/src/services/traffic-flow-batch-proof.ts new file mode 100644 index 00000000..eb79d6ae --- /dev/null +++ b/backend/src/services/traffic-flow-batch-proof.ts @@ -0,0 +1,9 @@ +import { createHmac, randomBytes } from "node:crypto" + +// An owned ingest batch never crosses a process boundary. Its proof stays with +// that batch through unknown-COMMIT reconciliation. No MCP state, filesystem, +// database, classifier initialization or network is needed to identify it. +const processKey = randomBytes(32) +export function flowBatchProof(payload: unknown, domain: "ingest" | "facts"): string { + return createHmac("sha256", processKey).update("flow-owned-batch-v1:" + domain + ":").update(JSON.stringify(payload)).digest("hex") +} diff --git a/backend/src/services/traffic-flow-bounded-sql.ts b/backend/src/services/traffic-flow-bounded-sql.ts new file mode 100644 index 00000000..d0623f4d --- /dev/null +++ b/backend/src/services/traffic-flow-bounded-sql.ts @@ -0,0 +1,46 @@ +import { getFlowReadResourceProfile } from "./traffic-flow-read-resource-profile.js" +import type { QueryResultRow } from "pg" +import { trackFlowReadSource } from "./traffic-flow-read-budget.js" + +export interface FlowBoundedQueryClient { + query(sql: string, values: unknown[]): Promise<{ rows: Array>; fields?:Array<{name:string}> }> +} +export interface FlowDecodedSourceLimits { maxRows: number; maxEncodedBytes: number } + +/** Scalar preflight reserves cumulative request capacity before any data decode. + * The following gated data statement enforces that same reservation on its own + * snapshot, so concurrent source growth cannot bypass preflight admission. + * Source is evaluated only on + * PostgreSQL. An oversized source yields only scalar metadata, no data payload. + * Query must be trusted SELECT text using PostgreSQL $n placeholders. + * maxEncodedBytes bounds source fields using their JSON representation; V8 amplification still needs a + * separately reserved/measured working set. No silent LIMIT or result truncation. + */ +export async function readBoundedFlowSql(client: FlowBoundedQueryClient, query: string, values: unknown[], limits: FlowDecodedSourceLimits): Promise { + if (!Number.isSafeInteger(limits.maxRows) || limits.maxRows < 1 || !Number.isSafeInteger(limits.maxEncodedBytes) || limits.maxEncodedBytes < 1) throw Error("Invalid NetFlow SQL source limit") + const profile=getFlowReadResourceProfile() + const sourceByteLimit=profile.name==="standard"?limits.maxEncodedBytes:Math.min(limits.maxEncodedBytes,profile.querySourceBytes) + return trackFlowReadSource(async admit=>{ + 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"}) + 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}), + flow_read_size AS MATERIALIZED (SELECT count(*) AS n, COALESCE(sum(octet_length(row_to_json(s)::text)),0) AS bytes FROM flow_read_source s) + SELECT size.n::text AS "__flowRowCount", size.bytes::text AS "__flowEncodedBytes", admitted.* + FROM flow_read_size size LEFT JOIN LATERAL + (SELECT TRUE AS "__flowPresent", s.* FROM flow_read_source s + WHERE size.n <= $${countIndex} AND size.bytes + 2 + GREATEST(size.n-1,0) <= $${bytesIndex}) admitted ON TRUE`, [...values, reserved.maxRows, reserved.maxEncodedBytes]) + 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 (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). + for (const item of result.rows) { delete item.__flowRowCount; delete item.__flowEncodedBytes; delete item.__flowPresent } + return result.rows as T[] + }) +} diff --git a/backend/src/services/traffic-flow-catalog-read.test.ts b/backend/src/services/traffic-flow-catalog-read.test.ts new file mode 100644 index 00000000..8ee99d3c --- /dev/null +++ b/backend/src/services/traffic-flow-catalog-read.test.ts @@ -0,0 +1,39 @@ +import { observeFlowRing, applyRingSnapshot, snapshotRings } from "./traffic-flow-engine.js" +import { createFlowIngestQuality } from "./traffic-flow-ingest-quality.js" +import assert from "node:assert/strict" +import { flowCounter, flowCounterSum, observeFlowClient, observeFlowExporter, type FlowClientInput } from "./traffic-flow-catalog-read.js" +import type { PendingFlowRow } from "./traffic-flow-ingest.js" + +for (const value of [-1, NaN, Infinity, "bad", "1.5", "9007199254740992", null, undefined, true]) assert.throws(() => flowCounter(value)) +assert.equal(flowCounter("0"), 0); assert.equal(flowCounterSum([0, 5, 10]), 15) +assert.throws(() => flowCounterSum([Number.MAX_SAFE_INTEGER, 1])) +const row = { serverId: 1, bytes: 1000, packets: 10, inIface: "client-vlan", bucketAt: "2026-10-19T00:00:00Z" } as PendingFlowRow +const user: FlowClientInput = { id: "u", login: "user", name: "Name", active: true, bindingCount: 2, selectedBindingCount: 1, bindings: [{ serverId: 1, interfaceName: "client-vlan", peerSpecific: false }] } +const observed = observeFlowClient(user, [row, { ...row, inIface: "2", bytes: 9999 }, { ...row, serverId: 2, bytes: 8888 }], 5) +assert.equal(observed.ingressNamedRowsBytes, 1000); assert.equal(observed.ingressNamedRowCount, 1) +assert.equal(observed.ifIndexRowsNotAttributed, 1); assert.equal(observed.completeSourceSet, false); assert.equal(observed.completeAttribution, false) +assert.equal(observed.availability, "unknown"); assert.equal(observed.rxMbps, null); assert.equal(observed.txMbps, null); assert.equal(observed.historicalOwnershipVerified, false) +assert.equal(observeFlowClient(user, [row], 5).completeAttribution, false) +const peer = observeFlowClient({ ...user, bindings: [{ ...user.bindings[0], peerSpecific: true }] }, [row], 5) +assert.equal(peer.ingressNamedRowsBytes, null); assert.equal(peer.peerBindingsNotAttributed, 1) +assert.equal(observeFlowClient({ ...user, active: false }, [], 5).availability, "unknown") +const source = { id: 1, name: "R", site: "site", country: "RU", host: "192.0.2.1", type: "home-router", enabled: false } +const exporter = observeFlowExporter({ catalog: { list: [source], byId: new Map([[1, source]]) }, configuredIds: new Set([1]), configurationMembershipVerified: true, window: { rows: [row], since: "", until: "", topRowsPerBucketLimit: 20 } }, 1, 5) +assert.equal(exporter.retainedRowsBytes, 1000); assert.equal(exporter.retainedRowsPackets, 10); assert.equal(exporter.collectionEnabled, false) +assert.equal(exporter.availability, "unknown"); assert.equal(exporter.wireRateMbps, null) +assert.equal(exporter.retainedRowsMeanMbps, 1000 * 8 / 300 / 1000000) +assert.equal(observeFlowExporter({ catalog: { list: [source], byId: new Map([[1, source]]) }, configuredIds: new Set([1]), configurationMembershipVerified: false, window: { rows: [], since: "", until: "", topRowsPerBucketLimit: 20 } }, 1, 5).storedPeerRecord, true) +applyRingSnapshot([]) +assert.equal(observeFlowRing(1).available, false); assert.equal(observeFlowRing(1).lastSlotIngressMbps, null) +const ringQuality=createFlowIngestQuality().snapshot() +const validRing={key:"1\0__all__",inBps:[...Array(59).fill(0),1000000],outBps:Array(60).fill(0)} +applyRingSnapshot([validRing],ringQuality) +assert.equal(observeFlowRing(1).lastSlotIngressMbps, 1); assert.equal(observeFlowRing(1).slotTimestampsVerified, false) +const originalRings=snapshotRings() +assert.throws(()=>applyRingSnapshot([validRing,{key:"2\0__all__",inBps:[NaN],outBps:[]}],ringQuality),/Invalid|oversized/) +assert.deepEqual(snapshotRings(),originalRings,"A rejected frame cannot partially replace a valid ring catalogue") +assert.equal(observeFlowRing(2).available,false) +applyRingSnapshot([validRing]) +assert.equal(observeFlowRing(1).available,false,"Legacy missing quality is unobserved, not a healthy zero-loss interval") +applyRingSnapshot([]) +console.log("NetFlow catalog models: safe counters/overflow, named ingress subset, peer/ifIndex uncertainty, disabled is not offline and observed row means are not wire rates") diff --git a/backend/src/services/traffic-flow-catalog-read.ts b/backend/src/services/traffic-flow-catalog-read.ts new file mode 100644 index 00000000..29084147 --- /dev/null +++ b/backend/src/services/traffic-flow-catalog-read.ts @@ -0,0 +1,103 @@ +import { flowReadDbAll } from "./traffic-flow-read-policy.js" +import { flowCounter, flowCounterSum } from "./traffic-flow-observed-values.js" +export { flowCounter, flowCounterSum } from "./traffic-flow-observed-values.js" +import { listStoredFlowRows, type PendingFlowRow } from "./traffic-flow-ingest.js" +import { getTrafficFlowSettingsRow } from "./traffic-flow-settings.js" +import { getServerCatalog } from "./traffic-flow-topology.js" +import { observeFlowRing } from "./traffic-flow-engine.js" + +const fail = (message: string) => Object.assign(new Error(message), { statusCode: 502 }) +function ids(values: readonly number[]) { + if (values.some(value => !Number.isSafeInteger(value) || value <= 0)) throw fail("Некорректная серверная область NetFlow") + return [...new Set(values)] +} +export async function readFlowCatalogWindow(minutes: number, serverIds?: readonly number[]) { + if (!Number.isSafeInteger(minutes) || minutes < 1 || minutes > 1440) throw fail("Окно NetFlow должно быть от 1 до 1440 минут") + const selected = serverIds === undefined ? undefined : ids(serverIds), until = new Date().toISOString() + const since = new Date(Date.parse(until) - minutes * 60000).toISOString() + 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})) + for (const row of rows) { flowCounter(row.bytes); flowCounter(row.packets) } + return { rows, since, until, topRowsPerBucketLimit: Math.max(20, settings.topN), topConversationsLimit: settings.topN } +} +export async function readFlowExporterInputs(minutes: number, serverIds?: readonly number[]) { + const selected = serverIds === undefined ? undefined : ids(serverIds) + const [window, catalog] = await Promise.all([readFlowCatalogWindow(minutes, selected), getServerCatalog(selected)]) + if (selected?.length === 0) return { window, catalog, configuredIds: new Set(), configurationMembershipVerified: null } + let peers: unknown = (await getTrafficFlowSettingsRow()).peersJson + if (typeof peers === "string") try { peers = JSON.parse(peers) } catch { peers = null } + const configuredIds = new Set(), selectedSet = selected === undefined ? undefined : new Set(selected) + let verified = Array.isArray(peers) + if (Array.isArray(peers)) for (const peer of peers) { + if (!peer || typeof peer !== "object" || !Number.isSafeInteger(peer.serverId) || peer.serverId <= 0 || selectedSet && !selectedSet.has(peer.serverId)) continue + if (typeof peer.publicKey !== "string") { verified = false; continue } + configuredIds.add(peer.serverId) + } + return { window, catalog, configuredIds, configurationMembershipVerified: verified } +} +export interface FlowClientInput { + id: string; login: string; name: string; active: boolean; bindingCount: number; selectedBindingCount: number; + bindings: { serverId: number; interfaceName: string; peerSpecific: boolean }[]; +} +/** Eligibility, hidden-binding exclusion, paging and selected bindings use one + * SQL statement/snapshot. No hidden user or binding is deserialized in Node. */ +export async function readFlowClientInputs(minutes: number, options?: { serverIds: readonly number[]; allowedServerIds: readonly number[]; offset: number; limit: number }) { + const selected = options ? ids(options.serverIds) : undefined, allowed = options ? ids(options.allowedServerIds) : undefined + if (selected?.some(id => !allowed!.includes(id))) throw fail("Выборка NetFlow вышла за серверную область") + if (options && (!Number.isSafeInteger(options.offset) || options.offset < 0 || !Number.isSafeInteger(options.limit) || options.limit < 1 || options.limit > 50)) throw fail("Некорректная страница клиентов NetFlow") + if (selected?.length === 0) return { users: [] as FlowClientInput[], total: 0, window: await readFlowCatalogWindow(minutes, []) } + const result = await flowReadDbAll<{ total: number | string; users: unknown }>(` + WITH eligible AS ( + SELECT u.id,u.login,u.name,u.active FROM app_users u + WHERE EXISTS(SELECT 1 FROM user_interface_bindings b WHERE b.user_id=u.id AND ($1::bigint[] IS NULL OR b.server_id=ANY($1))) + AND ($2::bigint[] IS NULL OR NOT EXISTS(SELECT 1 FROM user_interface_bindings hidden WHERE hidden.user_id=u.id AND NOT(hidden.server_id=ANY($2)))) + ), page AS (SELECT * FROM eligible ORDER BY id OFFSET $3 LIMIT $4), projected AS ( + SELECT p.*, + (SELECT count(*) FROM user_interface_bindings b WHERE b.user_id=p.id) AS "bindingCount", + (SELECT count(*) FROM user_interface_bindings b WHERE b.user_id=p.id AND ($1::bigint[] IS NULL OR b.server_id=ANY($1))) AS "selectedBindingCount", + COALESCE((SELECT jsonb_agg(jsonb_build_object('serverId',b.server_id,'interfaceName',b.interface_name,'peerSpecific',b.peer_public_key<>'')) + FROM (SELECT b.* FROM user_interface_bindings b WHERE b.user_id=p.id AND ($1::bigint[] IS NULL OR b.server_id=ANY($1)) ORDER BY b.id LIMIT $5) b),'[]'::jsonb) AS bindings + FROM page p + ) SELECT (SELECT count(*) FROM eligible) AS total,COALESCE(jsonb_agg(to_jsonb(projected)),'[]'::jsonb) AS users FROM projected + `, [selected ?? null, allowed ?? null, options?.offset ?? 0, options?.limit ?? null, options ? 1001 : null]) + const total = flowCounter(result[0]?.total), raw = result[0]?.users + if (!Array.isArray(raw)) throw fail("Не подтверждён каталог клиентов NetFlow") + const users: FlowClientInput[] = raw.map(user => { + if (!user || typeof user.id !== "string" || typeof user.login !== "string" || typeof user.name !== "string" || typeof user.active !== "boolean" || !Array.isArray(user.bindings)) throw fail("Некорректный клиент NetFlow") + const bindingCount = flowCounter(user.bindingCount), selectedBindingCount = flowCounter(user.selectedBindingCount) + if (options && user.bindings.length > 1000 || user.bindings.length !== selectedBindingCount) throw fail("Превышен предел привязок клиента NetFlow") + if (user.bindings.some((binding: FlowClientInput["bindings"][number]) => !Number.isSafeInteger(binding.serverId) || typeof binding.interfaceName !== "string" || typeof binding.peerSpecific !== "boolean" || selected && !selected.includes(binding.serverId))) throw fail("Некорректная привязка клиента NetFlow") + return { id: user.id, login: user.login, name: user.name, active: user.active, bindingCount, selectedBindingCount, bindings: user.bindings } + }) + return { users, total, window: await readFlowCatalogWindow(minutes, selected) } +} +export function observeFlowExporter(input: Awaited>, serverId: number, minutes: number) { + const source = input.catalog.byId.get(serverId)!, rows = input.window.rows.filter(row => row.serverId === serverId) + const retainedRowsBytes = flowCounterSum(rows.map(row => row.bytes)), retainedRowsPackets = flowCounterSum(rows.map(row => row.packets)) + return { id: String(serverId), name: source.name, site: source.site, country: source.country, sourceHost: source.host, + collectionEnabled: source.enabled ?? null, storedPeerRecord: input.configuredIds.has(serverId) ? true : input.configurationMembershipVerified === true ? false : null, + retainedRowsBytes, retainedRowsPackets, retainedRowCount: rows.length, retainedRowsMeanMbps: retainedRowsBytes * 8 / (minutes * 60) / 1000000, + lastRetainedBucketAt: rows.length ? rows.map(row => row.bucketAt).sort().at(-1)! : null, + flowRing: observeFlowRing(serverId), wireRateMbps: null, availability: "unknown", forwardingVerified: false } +} +export function observeFlowClient(user: FlowClientInput, rows: PendingFlowRow[], minutes: number) { + const interfaces = new Map>() + for (const binding of user.bindings) if (!binding.peerSpecific) { + const names = interfaces.get(binding.serverId) ?? new Set(); names.add(binding.interfaceName); interfaces.set(binding.serverId, names) + } + const ifIndexRowsNotAttributed = rows.filter(row => interfaces.has(row.serverId) && /^#?\d+$/.test(row.inIface)).length + const matched = rows.filter(row => !/^#?\d+$/.test(row.inIface) && interfaces.get(row.serverId)?.has(row.inIface)) + const attributable = user.bindings.some(binding => !binding.peerSpecific), bytes = attributable ? flowCounterSum(matched.map(row => row.bytes)) : null + return { id: user.id, name: user.login, displayName: user.name, accountEnabled: user.active, availability: "unknown", + scopedBindingCount: user.bindingCount, selectedBindingCount: user.selectedBindingCount, peerBindingsNotAttributed: user.bindings.filter(binding => binding.peerSpecific).length, + completeSourceSet: user.bindingCount === user.selectedBindingCount, completeAttribution: user.bindingCount === user.selectedBindingCount && user.bindings.every(binding => !binding.peerSpecific) && ifIndexRowsNotAttributed === 0, + ifIndexRowsNotAttributed, historicalOwnershipVerified: false, attribution: "current-named-whole-interface-bindings", + ingressNamedRowsBytes: bytes, ingressNamedRowCount: attributable ? matched.length : null, + ingressNamedRowsMeanMbps: bytes === null ? null : bytes * 8 / (minutes * 60) / 1000000, + serverContextFlowRing: user.bindings.length ? { serverId: user.bindings[0].serverId, scope: "server-payload; not per-client", ...observeFlowRing(user.bindings[0].serverId) } : null, + rxMbps: null, txMbps: null, wireRateVerified: false } +} diff --git a/backend/src/services/traffic-flow-classify.ts b/backend/src/services/traffic-flow-classify.ts index 0a06b37d..d389e2d1 100644 --- a/backend/src/services/traffic-flow-classify.ts +++ b/backend/src/services/traffic-flow-classify.ts @@ -2,6 +2,7 @@ import { databaseBackgroundWork } from "./database-background-work.js" import { OTHER_SERVICE, resolveFlowBrand } from "./traffic-flow-brands.js" import { db } from "../db/index.js" import { evobgpSettings } from "../db/schema.js" +import { withActiveDatabaseRequest } from "../modules/mcp/database-request-lease.js" import { ipInCidrV4, parseCidrV4 } from "./traffic-flow-ip.js" import type { FlowIpMeta } from "./traffic-flow-ripe.js" import { applicationName } from "./traffic-flow-apps.js" @@ -63,8 +64,8 @@ export function categoryFromPurpose(purpose: string, proto: number, dstPort: num return OTHER_SERVICE } -function matchCidr(ip: string): CatalogCidr | null { - for (const row of cidrs) { +function matchCidr(ip: string,rows=cidrs): CatalogCidr | null { + for (const row of rows) { if (ipInCidrV4(ip, row.cidr)) return row } return null @@ -76,7 +77,7 @@ export function classifyFlowDst( dstPort: number, srcPort: number, ripe: FlowIpMeta | null, - opts?: { ignoreTunnelProto?: boolean }, + opts?: { ignoreTunnelProto?: boolean;catalog?:{cidrs:CatalogCidr[];asnPurpose:Map} }, ): FlowClassification { if (!opts?.ignoreTunnelProto) { if (proto === 47) return { service: "GRE", category: "Туннель" } @@ -84,10 +85,10 @@ export function classifyFlowDst( } const app = applicationName(proto, dstPort, srcPort) if (app === "WireGuard") return { service: "WireGuard", category: "Туннель" } - const hit = matchCidr(dst) + const hit = matchCidr(dst,opts?.catalog?.cidrs) const holder = ripe?.holder ?? "" const brand = resolveFlowBrand(dst, ripe?.asn ?? 0, holder, proto, dstPort, srcPort) - const asnName = ripe?.asn ? asnPurpose.get(ripe.asn) : undefined + const asnName = ripe?.asn ? (opts?.catalog?.asnPurpose??asnPurpose).get(ripe.asn) : undefined const service = (hit?.purpose || brand?.service || asnName || OTHER_SERVICE).trim() || OTHER_SERVICE const category = hit ? categoryFromPurpose(hit.purpose, proto, dstPort, srcPort) @@ -95,13 +96,20 @@ export function classifyFlowDst( return { service, category } } +/** Copies already-observed data only; never fetches the EvoBGP catalog. */ +export function captureFlowClassificationCatalog() { + if(cidrs.length>100000||asnPurpose.size>100000)throw Error("Flow classification snapshot exceeds bound") + return {cidrs:cidrs.map(row=>({...row})),asnPurpose:new Map(asnPurpose)} +} + async function fetchCatalog(): Promise { if (!catalogFetchEnabled) return if (Date.now() - fetchedAt < CATALOG_TTL_MS) return if (inflight) return inflight inflight = (async () => { try { - const row = (await db.select().from(evobgpSettings).limit(1))[0] + const readSettings = async () => db.select().from(evobgpSettings).limit(1) + const [row] = await (withActiveDatabaseRequest(readSettings) ?? readSettings()) if (!row?.enabled) return const root = String(row.baseUrl ?? "").replace(/\/+$/, "") const token = String(row.apiKey ?? "").replace(/^Bearer\s+/i, "").trim() diff --git a/backend/src/services/traffic-flow-collector-ipc.ts b/backend/src/services/traffic-flow-collector-ipc.ts index d605c577..4cff8f9a 100644 --- a/backend/src/services/traffic-flow-collector-ipc.ts +++ b/backend/src/services/traffic-flow-collector-ipc.ts @@ -1,4 +1,6 @@ +import type {FlowIngestQuality} from "./traffic-flow-ingest-quality.js" import type { OverlayPeerRef } from "./traffic-flow-map-exporter.js" +import type { FlowDrainToMain, FlowDrainToWorker } from "./traffic-flow-drain-protocol.js" export interface ExporterMapPayload { overlayPrefix: string @@ -16,6 +18,7 @@ export interface CollectorStartPayload { } export interface CollectorHeartbeat { + dataQuality:FlowIngestQuality|null bound: boolean address: string | null packetsReceived: number @@ -30,6 +33,8 @@ export interface CollectorHeartbeat { } export type MainToWorker = + | FlowDrainToWorker + | { type: "heartbeat-ack"; heartbeatId: string } | { type: "start"; payload: CollectorStartPayload } | { type: "stop" } | { type: "shutdown" } @@ -37,6 +42,7 @@ export type MainToWorker = | { type: "updateSettings"; payload: { topN: number; retentionHours: number } } export type WorkerToMain = - | { type: "heartbeat"; payload: CollectorHeartbeat } + | FlowDrainToMain + | { type: "heartbeat"; heartbeatId: string; payload: CollectorHeartbeat } | { type: "error"; payload: { message: string } } | { type: "shutdown-complete" } diff --git a/backend/src/services/traffic-flow-collector-worker.ts b/backend/src/services/traffic-flow-collector-worker.ts index 4219802d..d80ec8ff 100644 --- a/backend/src/services/traffic-flow-collector-worker.ts +++ b/backend/src/services/traffic-flow-collector-worker.ts @@ -1,8 +1,13 @@ +import { createFlowHeartbeatSender } from "./traffic-flow-heartbeat.js" +import { randomUUID } from "node:crypto" +import { clearFlowTemplateCache } from "./traffic-flow-parse.js" import { createSocket, type Socket } from "node:dgram" import { parentPort } from "node:worker_threads" +import { runExistingWorkerDrain } from "./traffic-flow-drain-protocol.js" import { pool } from "../db/index.js" import { pauseRipeBackgroundWork } from "./traffic-flow-ripe.js" import type { + CollectorHeartbeat, CollectorStartPayload, ExporterMapPayload, MainToWorker, @@ -12,6 +17,8 @@ import { TICK_MS, configureEngine, flushPending, + strictDrainEngine, + setFlowEngineLifecycleAuthorization, getEngineStats, ingestDatagram, setEngineError, @@ -24,8 +31,15 @@ let flushTimer: ReturnType | null = null let bound = false let address: string | null = null let shuttingDown = false +let startingAuthorized = false +// Failed startup is terminal for collection. A distinct, freshly authorized +// retirement may still close its retained pool/port; never replay startup. +let authorizedStartFailed = false +let failedStartRetirementPending = false -async function shutdown(): Promise { +async function shutdown(authorize?: () => Promise, close = true): Promise { + if (authorize) setFlowEngineLifecycleAuthorization(authorize) + await authorize?.() shuttingDown = true if (flushTimer) clearInterval(flushTimer) flushTimer = null @@ -36,9 +50,16 @@ async function shutdown(): Promise { if (current) await new Promise((resolve, reject) => { try { current.close(() => resolve()) } catch (error) { reject(error) } }) - await flushPending({ force: true, prune: false }) + await authorize?.() + await flushPending({ force: true, prune: false }, authorize) + await authorize?.() await pauseRipeBackgroundWork() + await authorize?.() await pool.end() + if (close) completeShutdown() +} + +function completeShutdown(): void { send({ type: "shutdown-complete" }) parentPort?.close() } @@ -47,11 +68,9 @@ function send(msg: WorkerToMain): void { parentPort?.postMessage(msg) } -function heartbeat(): void { +const heartbeatSender = createFlowHeartbeatSender(() => { const stats = getEngineStats() - send({ - type: "heartbeat", - payload: { + return { bound, address, packetsReceived: stats.packetsReceived, @@ -60,12 +79,14 @@ function heartbeat(): void { lastDatagramAt: stats.lastDatagramAt, pendingSize: stats.pendingSize, dropped: stats.dropped, + dataQuality:stats.dataQuality, rowsStored: stats.rowsStored, workerAlive: true, rings: snapshotRings(), - }, - }) -} + } +}, (heartbeatId, payload) => send({ type: "heartbeat", heartbeatId, payload })) + +function heartbeat(): void { heartbeatSender.publish() } function applyExporterMap(payload: ExporterMapPayload): void { setExporterResolveCtx({ @@ -76,6 +97,15 @@ function applyExporterMap(payload: ExporterMapPayload): void { }) } +function quiesceListener(): void { + if (flushTimer) clearInterval(flushTimer) + flushTimer = null + if (socket) { try { socket.close() } catch {} } + socket = null + bound = false + address = null +} + function stopListener(): void { if (flushTimer) { clearInterval(flushTimer) @@ -92,13 +122,24 @@ function stopListener(): void { address = null } -function startListener(payload: CollectorStartPayload): void { - stopListener() +async function startListener(payload: CollectorStartPayload, authorize?: () => Promise): Promise { + if (authorize) { + setFlowEngineLifecycleAuthorization(authorize) + await authorize() + quiesceListener() + await authorize() + await flushPending({ force: true }, authorize) + await authorize() + } else stopListener() configureEngine({ topN: payload.topN, retentionHours: payload.retentionHours }) applyExporterMap(payload.exporterMap) + // The prior socket is already quiesced; do not retain retired generations. + clearFlowTemplateCache() const sock = createSocket("udp4") + const collectorSocketId = randomUUID() sock.on("error", (err) => { + if (socket !== sock || shuttingDown) return setEngineError(err.message) bound = false address = null @@ -106,34 +147,70 @@ function startListener(payload: CollectorStartPayload): void { heartbeat() }) sock.on("message", (msg, rinfo) => { - if (shuttingDown) return + if (socket !== sock || shuttingDown || startingAuthorized) return try { - ingestDatagram(msg, rinfo.address) + ingestDatagram(msg, rinfo.address, { sourcePort: rinfo.port, collectorSocketId }) } catch (e) { setEngineError(e instanceof Error ? e.message : String(e)) } }) - try { - sock.setRecvBufferSize(8 * 1024 * 1024) - } catch { - /* platform may ignore */ - } + socket = sock sock.bind(payload.listenPort, payload.listenHost, () => { + if (socket !== sock || shuttingDown) return + try { + // A native socket handle exists only after bind. The OS may clamp or + // reject this request; it is not a measured receive-buffer guarantee. + sock.setRecvBufferSize(8 * 1024 * 1024) + } catch { + /* retain the platform default */ + } bound = true address = `${payload.listenHost}:${payload.listenPort}` setEngineError("") heartbeat() }) - socket = sock + if (authorize) await authorize() + if (!authorize) startFlowFlushTimer() +} + +function startFlowFlushTimer(): void { flushTimer = setInterval(() => { void flushPending().catch((e) => { setEngineError(e instanceof Error ? e.message : String(e)) }).finally(() => heartbeat()) }, TICK_MS) } +function completeAuthorizedStart(): void { + setFlowEngineLifecycleAuthorization(undefined) + startingAuthorized = false + startFlowFlushTimer() +} parentPort?.on("message", (msg: MainToWorker) => { - if (shuttingDown) return + if (shuttingDown && !(authorizedStartFailed && !failedStartRetirementPending && msg.type === "authorized-shutdown")) return + if (msg.type === "heartbeat-ack") { heartbeatSender.acknowledge(msg.heartbeatId); return } + if(msg.type==="authorized-start") { + startingAuthorized = true + const peer=parentPort + if(peer)void runExistingWorkerDrain(peer,msg.requestId,async authorize=>{await startListener(msg.payload,authorize);return null}, + completeAuthorizedStart, + ()=>{authorizedStartFailed=true;shuttingDown=true;quiesceListener()}) + return + } + if(msg.type==="authorized-shutdown") { + const peer=parentPort + if(peer) { + if(authorizedStartFailed)failedStartRetirementPending=true + void runExistingWorkerDrain(peer,msg.requestId,async authorize=>{await shutdown(authorize,false);return null},completeShutdown, + ()=>{failedStartRetirementPending=false}) + } + return + } + if(msg.type==="strict-drain") { + const peer=parentPort + if(peer)void runExistingWorkerDrain(peer,msg.requestId,authorize=>strictDrainEngine(authorize,msg.serverIds)) + return + } if (msg.type === "shutdown") { void shutdown().catch(() => { send({ type: "error", payload: { message: "Не удалось завершить остановку NetFlow worker" } }) @@ -142,7 +219,7 @@ parentPort?.on("message", (msg: MainToWorker) => { return } try { - if (msg.type === "start") startListener(msg.payload) + if (msg.type === "start") void startListener(msg.payload).catch(error => {setEngineError(String(error));heartbeat()}) else if (msg.type === "stop") { stopListener() heartbeat() diff --git a/backend/src/services/traffic-flow-dest.ts b/backend/src/services/traffic-flow-dest.ts index 39d73753..2e0476e7 100644 --- a/backend/src/services/traffic-flow-dest.ts +++ b/backend/src/services/traffic-flow-dest.ts @@ -29,8 +29,9 @@ export function destCtxForIface( serverId: number, inIface: string, nat?: Pick, + resolveIface=canonicalFactIface, ): InternetDestCtx { - const name = canonicalFactIface(serverId, inIface) || String(inIface ?? "").trim() + const name = resolveIface(serverId, inIface) || String(inIface ?? "").trim() return { ours: flowOursHosts(topo), boundClient: Boolean( @@ -96,13 +97,16 @@ export function resolveInternetDest(opts: { natDst?: string natSrcPort?: number natDstPort?: number + resolveIface?:typeof canonicalFactIface + resolveIp?:(ip:string)=>FlowIpMeta|null + classify?:typeof classifyFlowDst }): InternetDestMeta { const ctx = destCtxForIface(opts.topo, opts.serverId, opts.inIface, { natSrc: opts.natSrc, natDst: opts.natDst, natSrcPort: opts.natSrcPort, natDstPort: opts.natDstPort, - }) + },opts.resolveIface) const endpoints = resolveFlowEndpoints({ src: opts.src, dst: opts.dst, @@ -114,8 +118,8 @@ export function resolveInternetDest(opts: { if (!dest) { return { dest: "", ripe: null, classified: OTHER_BRAND, country: "", asn: 0, endpoints } } - const ripe = resolveFlowIp(dest) - const classified = classifyFlowDst( + const ripe = (opts.resolveIp??resolveFlowIp)(dest) + const classified = (opts.classify??classifyFlowDst)( dest, opts.proto, endpoints.peerPort, diff --git a/backend/src/services/traffic-flow-drain-protocol.test.ts b/backend/src/services/traffic-flow-drain-protocol.test.ts new file mode 100644 index 00000000..bbce606c --- /dev/null +++ b/backend/src/services/traffic-flow-drain-protocol.test.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { EventEmitter } from "node:events" +import { AsyncLocalStorage, AsyncResource } from "node:async_hooks" +import { requestExistingWorkerDrain,runExistingWorkerDrain,type FlowDrainPeer } from "./traffic-flow-drain-protocol.js" + +const pair=()=>{ + const main=new EventEmitter(),worker=new EventEmitter(),sent:unknown[]=[] + const left:FlowDrainPeer={on:(event,fn)=>main.on(event,fn),off:(event,fn)=>main.off(event,fn),postMessage:message=>{sent.push(message);queueMicrotask(()=>worker.emit("message",message))}} + const right:FlowDrainPeer={on:(event,fn)=>worker.on(event,fn),off:(event,fn)=>worker.off(event,fn),postMessage:message=>{sent.push(message);queueMicrotask(()=>main.emit("message",message))}} + return {main,worker,left,right,sent} +} +const receipt={batchId:"11111111-1111-4111-8111-111111111111",buckets:2,facts:1,skippedInet:0} +assert.deepEqual(await requestExistingWorkerDrain(null,async()=>{},[1]),{workerPresent:false,receipt:null}) +const happy=pair();let running:Promise|undefined,checks=0,dispatches=0 +happy.worker.on("message",message=>{if(message.type==="strict-drain")running=runExistingWorkerDrain(happy.right,message.requestId,async authorize=>{await authorize();dispatches++;await authorize();return receipt})}) +assert.deepEqual(await requestExistingWorkerDrain(happy.left,async()=>{checks++},[1]),{workerPresent:true,receipt}) +await running;assert.equal(dispatches,1);assert.ok(checks>=5);assert.equal(happy.main.listenerCount("message"),0) + +const revoked=pair();let allowed=true,deniedRun:Promise|undefined,writes=0 +revoked.worker.on("message",message=>{if(message.type==="strict-drain")deniedRun=runExistingWorkerDrain(revoked.right,message.requestId,async authorize=>{allowed=false;await authorize();writes++;return receipt})}) +await assert.rejects(requestExistingWorkerDrain(revoked.left,async()=>{if(!allowed)throw Error("revoked")},[1]),/revoked/) +await deniedRun;assert.equal(writes,0) + +const late=pair();let wake:()=>void=()=>{},lateChecks=0,lateRun:Promise|undefined +const gate=new Promise(resolve=>{wake=resolve}) +late.worker.on("message",message=>{if(message.type==="strict-drain")lateRun=runExistingWorkerDrain(late.right,message.requestId,async()=>{writes++;return receipt})}) +let returned=false +const timed=requestExistingWorkerDrain(late.left,async()=>{lateChecks++;if(lateChecks>1)await gate},[1],20).finally(()=>{returned=true}) +const rejected=assert.rejects(timed,/deadline/) +await new Promise(resolve=>setTimeout(resolve,40)) +assert.equal(returned,false,"drain retains outstanding authority work before returning") +wake();await rejected;await lateRun;await new Promise(resolve=>setImmediate(resolve)) +assert.equal(writes,0);assert.ok(!late.sent.some(message=>typeof message==="object"&&message!==null&&"type" in message&&message.type==="drain-authorized")) +const exited=pair();const waiting=requestExistingWorkerDrain(exited.left,async()=>{},[1],1000);queueMicrotask(()=>exited.main.emit("exit",1));await assert.rejects(waiting,/exited/) +assert.ok(!happy.sent.some(message=>typeof message==="object"&&message!==null&&"type" in message&&["start","stop","shutdown"].includes(String(message.type)))) +// Model the real EventEmitter callback boundary of an already-existing Worker: +// replies arrive in its creation context, while each new request owns a live +// distinct caller scope. No SQL or permission outcome is replaced here. +const callers=new AsyncLocalStorage<{name:string;active:boolean}>() +const oldWorker=callers.run({name:"old-worker-owner",active:false},()=>new AsyncResource("existing-worker-reply")) +const scoped=pair(),caller={name:"current-caller",active:true};let scopedRun:Promise|undefined,scopedChecks=0 +scoped.right.postMessage=message=>oldWorker.runInAsyncScope(()=>queueMicrotask(()=>scoped.main.emit("message",message))) +scoped.worker.on("message",message=>{if(message.type==="strict-drain")scopedRun=runExistingWorkerDrain(scoped.right,message.requestId,async authorize=>{await authorize();return receipt})}) +await callers.run(caller,async()=>{ + assert.deepEqual(await requestExistingWorkerDrain(scoped.left,async()=>{assert.equal(callers.getStore(),caller);assert.equal(caller.active,true);scopedChecks++},[1]),{workerPresent:true,receipt}) + await scopedRun +}) +assert.ok(scopedChecks>=5);assert.equal(scoped.main.listenerCount("message"),0);oldWorker.emitDestroy() +console.log("traffic-flow-drain-protocol.test.ts: existing-peer checkpoints, revoke before dispatch, timeout/late approval suppression, worker exit, cleanup and zero start/stop/restart messages verified; no worker/network/SQL executed") diff --git a/backend/src/services/traffic-flow-drain-protocol.ts b/backend/src/services/traffic-flow-drain-protocol.ts new file mode 100644 index 00000000..4c15e014 --- /dev/null +++ b/backend/src/services/traffic-flow-drain-protocol.ts @@ -0,0 +1,89 @@ +import type { CollectorStartPayload } from "./traffic-flow-collector-ipc.js" +import { randomUUID } from "node:crypto" +import { AsyncLocalStorage } from "node:async_hooks" + +export interface CollectorDrainReceipt {batchId:string;buckets:number;facts:number;skippedInet:number} +export type FlowDrainToWorker = {type:"authorized-start";requestId:string;payload:CollectorStartPayload}|{type:"authorized-shutdown";requestId:string}|{type:"strict-drain";requestId:string;serverIds:readonly number[]}|{type:"drain-authorized";requestId:string;checkpointId:string;approved:boolean}|{type:"drain-cancel";requestId:string} +export type FlowDrainToMain = {type:"drain-authorize";requestId:string;checkpointId:string}|{type:"drain-result";requestId:string;ok:boolean;receipt:CollectorDrainReceipt|null} +export interface FlowDrainPeer { + postMessage:(message:unknown)=>void + on:(event:"message"|"exit"|"error",listener:(value:unknown)=>void)=>unknown + off:(event:"message"|"exit"|"error",listener:(value:unknown)=>void)=>unknown +} +const record=(value:unknown):value is Record=>Boolean(value)&&typeof value==="object" +const uuid=(value:unknown):value is string=>typeof value==="string"&&/^[a-f0-9-]{36}$/i.test(value) +const validReceipt=(value:unknown):value is CollectorDrainReceipt|null=>value===null||record(value)&&uuid(value.batchId)&&[value.buckets,value.facts,value.skippedInet].every(count=>Number.isSafeInteger(count)&&Number(count)>=0) + +/** Addresses only the supplied peer: strict-drain preserves its listener, while + * explicit authorized-start/shutdown modes fence initialization or retirement. + * This transport never constructs workers or retries/restarts an operation. */ +export async function requestExistingWorkerDrain(peer:FlowDrainPeer|null,authorize:()=>Promise,serverIds:readonly number[],timeoutMs=30000,operation:"strict-drain"|"authorized-shutdown"|"authorized-start"="strict-drain",payload?:CollectorStartPayload):Promise<{workerPresent:boolean;receipt:CollectorDrainReceipt|null}> { + if(!Array.isArray(serverIds)||serverIds.length>1000||new Set(serverIds).size!==serverIds.length||serverIds.some(id=>!Number.isSafeInteger(id)||id<=0))throw Error("Invalid existing-worker drain scope") + const scope=[...serverIds] + await authorize() + if(!peer)return {workerPresent:false,receipt:null} + if(!Number.isInteger(timeoutMs)||timeoutMs<1||timeoutMs>30000)throw Error("Invalid worker drain deadline") + const requestId=randomUUID();let active=true,poll:Promise|undefined + const checkpoints=new Set>() + let timer:ReturnType|undefined,interval:ReturnType|undefined + let onMessage:(value:unknown)=>void=()=>{},onExit:(value:unknown)=>void=()=>{} + try { + return await new Promise((resolve,reject)=>{ + const fail=(error:unknown)=>{if(!active)return;active=false;try{peer.postMessage({type:"drain-cancel",requestId})}catch{};reject(error)} + const check=async()=>{await authorize();if(!active)throw Error("Worker drain request is closed")} + const track=(task:Promise)=>{checkpoints.add(task);void task.finally(()=>checkpoints.delete(task))} + // Existing Worker events carry the Worker's creation context. Execute + // this request's checks in its genuine caller context; the original live + // scope/authority and this request's closed/deadline guards still apply. + onMessage=AsyncLocalStorage.bind((value:unknown)=>{ + if(!active||!record(value)||value.requestId!==requestId)return + if(value.type==="drain-authorize"&&uuid(value.checkpointId)){ + track(check().then(()=>{if(active)peer.postMessage({type:"drain-authorized",requestId,checkpointId:value.checkpointId,approved:true})}).catch(fail)) + }else if(value.type==="drain-result"){ + if(value.ok!==true||!validReceipt(value.receipt)){fail(Error("Worker drain outcome is unconfirmed"));return} + const receipt=value.receipt + // A shutdown result already follows its final checkpoint. Resolve + // before exit; the separate shutdown waiter still requires ack + exit. + if(operation!=="strict-drain"){active=false;resolve({workerPresent:true,receipt});return} + track(check().then(()=>{if(active){active=false;resolve({workerPresent:true,receipt})}}).catch(fail)) + } + }) + onExit=()=>fail(Error("Existing worker exited before drain acknowledgement")) + peer.on("message",onMessage);peer.on("exit",onExit);peer.on("error",onExit) + timer=setTimeout(()=>fail(Error("Worker drain deadline exceeded; no collector restart")),timeoutMs) + interval=setInterval(()=>{if(!active||poll)return;poll=check().catch(fail).finally(()=>{poll=undefined})},100) + try{peer.postMessage(operation==="authorized-start"?{type:operation,requestId,payload}:operation==="authorized-shutdown"?{type:operation,requestId}:{type:operation,requestId,serverIds:scope})}catch(error){fail(error)} + }) + }finally{ + active=false;if(timer)clearTimeout(timer);if(interval)clearInterval(interval) + peer.off("message",onMessage);peer.off("exit",onExit);peer.off("error",onExit) + await poll + await Promise.all(checkpoints) + } +} + +async function requestDrainCheckpoint(peer:FlowDrainPeer,requestId:string,signal:AbortSignal):Promise { + signal.throwIfAborted();const checkpointId=randomUUID() + await new Promise((resolve,reject)=>{ + const cleanup=()=>{clearTimeout(timer);peer.off("message",message);signal.removeEventListener("abort",abort)} + const abort=()=>{cleanup();reject(Error("Worker drain authority closed"))} + const message=(value:unknown)=>{if(record(value)&&value.type==="drain-authorized"&&value.requestId===requestId&&value.checkpointId===checkpointId){cleanup();if(value.approved===true)resolve();else reject(Error("Worker drain authority refused"))}} + const timer=setTimeout(()=>{cleanup();reject(Error("Worker drain authority checkpoint timed out"))},5000) + peer.on("message",message);signal.addEventListener("abort",abort,{once:true}) + try{peer.postMessage({type:"drain-authorize",requestId,checkpointId})}catch(error){cleanup();reject(error)} + }) + signal.throwIfAborted() +} + +/** Worker execution uses the same checkpoint closure for a strict owned batch + * or explicit startup/shutdown. Revocation never triggers an anonymous flush. */ +export async function runExistingWorkerDrain(peer:FlowDrainPeer,requestId:string,run:(authorize:()=>Promise,signal:AbortSignal)=>Promise,completed?:()=>void,failed?:()=>void):Promise { + if(!uuid(requestId))throw Error("Invalid worker drain request") + const cancellation=new AbortController();let active=true + const onMessage=(value:unknown)=>{if(record(value)&&value.type==="drain-cancel"&&value.requestId===requestId)cancellation.abort()} + peer.on("message",onMessage) + const authorize=async()=>{if(!active)throw Error("Worker drain execution is closed");await requestDrainCheckpoint(peer,requestId,cancellation.signal);if(!active)throw Error("Worker drain execution is closed")} + try{await authorize();const receipt=await run(authorize,cancellation.signal);await authorize();peer.postMessage({type:"drain-result",requestId,ok:true,receipt});completed?.()} + catch{failed?.();peer.postMessage({type:"drain-result",requestId,ok:false,receipt:null})} + finally{active=false;cancellation.abort();peer.off("message",onMessage)} +} diff --git a/backend/src/services/traffic-flow-engine-owned.test.ts b/backend/src/services/traffic-flow-engine-owned.test.ts new file mode 100644 index 00000000..9c7f5a68 --- /dev/null +++ b/backend/src/services/traffic-flow-engine-owned.test.ts @@ -0,0 +1,324 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import * as crypto from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { createFlowIngestQuality, type FlowIngestQuality } from "./traffic-flow-ingest-quality.js" +import ts from "typescript" +import { DatabaseBackgroundWork } from "./database-background-work.js" +import * as owned from "./traffic-flow-owned-batch.js" +import * as proof from "./traffic-flow-batch-proof.js" +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 { pruneRipeSqlite } from "./traffic-flow-ripe.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" + +// Exercise the actual connected engine with explicit fake I/O boundaries. +const source=await readFile(new URL("./traffic-flow-engine.ts",import.meta.url),"utf8") +assert.ok(source.includes("ownedEngineBatch?.assertResolved()")) +assert.ok(!source.includes("privateSecretFingerprint")) +const live=accumulator.createFlowFactAccumulator() +let mode="normal",commits=0,dispatches=0,inTransaction=false,receiptPresent=false,factsResetAllowed=true,mutatePayload=false,ripePruneFails=false +let identified:{rows:{bytes:number}[];ingestQuality:FlowIngestQuality}|undefined +let heldStarted:()=>void=()=>{},releaseHeld:()=>void=()=>{},heldGate:Promise=Promise.resolve() +const capQueries:Array<{sql:string;params:unknown}>=[] +class Unknown extends Error {} +const dependencies:Record={ + "node:crypto":crypto, + "node:util":{isDeepStrictEqual},"./traffic-flow-ingest-quality.js":{createFlowIngestQuality}, + "./traffic-flow-owned-batch.js":owned,"./traffic-flow-batch-proof.js":{flowBatchProof:(payload:unknown,domain:"ingest"|"facts")=>{identified=payload as typeof identified;return proof.flowBatchProof(payload,domain)}}, + "./traffic-flow-fact-accumulator.js":accumulator,"./serial-operation.js":serial, + "./traffic-flow-parse.js":parse,"./traffic-flow-ip.js":ip, + "./database-background-work.js":{databaseBackgroundWork:{track:()=>{throw Error("unexpected background load")}}}, + "./traffic-flow-write-transaction.js":{ + FlowCommitUnknown:Unknown,flowWriteContext:()=>inTransaction?{partitions:new Set()}:undefined, + flowWritePool:{query:async()=>{assert.ok(inTransaction);dispatches++;return {rows:[]}}}, + flowWriteAll:async(sql:string,params:unknown)=>{assert.ok(inTransaction);dispatches++;if(sql.includes("FROM flow_daily_dims")&&sql.includes("dim = 'asn'"))capQueries.push({sql,params});return []}, + flowWriteQuery:async(sql:string)=>{assert.ok(inTransaction);dispatches++;if(ripePruneFails&&sql.includes("DELETE FROM flow_ip_meta"))throw Error("RIPE prune SQL failure");return {rows:[]}}, + commitFlowWrite:async(_input:unknown,work:()=>Promise,authorize:()=>Promise)=>{ + commits++;await authorize();inTransaction=true + if(mutatePayload&&identified?.rows[0])identified.rows[0].bytes++ + try{const result=await work();await authorize();if(mode==="held"){heldStarted();await heldGate}if(mode==="unknown")throw new Unknown("lost COMMIT");if(mode==="rollback")throw Error("known rollback");return result}finally{inTransaction=false} + }, + reconcileFlowWrite:async()=>({committed:receiptPresent,result:receiptPresent?{buckets:1,ingestQuality:identified?.ingestQuality}:null}), + }, + "../db/partitions.js":{specForParent:()=>null,ensurePartitionFor:()=>{throw Error("unexpected DDL")}}, + "./traffic-flow-planes.js":{classifyFlowPlaneLite:()=>"internet"}, + "./traffic-flow-map-exporter.js":{pickServerIdForExporter:()=>1}, + "./traffic-flow-apps.js":{applicationName:()=>"https"}, + "./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-facts-filter.js":{shouldWriteFlowFact:()=>true}, + "./traffic-flow-dest.js":{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, + takePendingFlowFacts:()=>{const rows=live.snapshot();live.clear();return rows}, + persistFlowFactRows:async(rows:unknown[])=>{assert.ok(inTransaction);return rows.length}, + hourBucketIso:accumulator.hourBucketIso,resetFactsAfterDatabaseRestore:()=>live.clear(),assertFactsResetAllowed:()=>{if(!factsResetAllowed)throw Error("unresolved standalone facts")}, + }, +} +const output=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText +type ReadOptions={serverId?:number;sinceIso?:string;untilIso?:string} +type ReadRow={serverId:number;bucketAt:string;src:string;bytes:number;packets:number} +type Engine={peekPendingFlows:(options?:ReadOptions)=>ReadRow[];listLiveFlowRows:(sinceIso:string,options?:Omit)=>ReadRow[];queueParsedFlows:(id:number,flows:unknown[])=>void;strictDrainEngine:(authorize?:()=>Promise,serverIds?:readonly number[])=>Promise;getEngineStats:()=>{pendingSize:number;rowsStored:number};resetEngineAfterDatabaseRestore:()=>void;reconcileEnginePendingWrite:()=>Promise<{state:string}>} +const exports:Record={} +runInNewContext(output,{exports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout}) +const engine=exports as unknown as Engine +assert.equal(await engine.strictDrainEngine(async()=>{},[]),null,"unobserved maintenance drain must not overwrite restored stats or run a global cap") +assert.equal(commits,0);assert.equal(dispatches,0) +engine.resetEngineAfterDatabaseRestore() +assert.equal(await engine.strictDrainEngine(async()=>{},[]),null) +assert.equal(commits,0);assert.equal(dispatches,0) +const strict=engine.strictDrainEngine +engine.strictDrainEngine=(authorize=async()=>{},serverIds=[1])=>strict(authorize,serverIds) +const flow={src:"192.0.2.1",dst:"198.51.100.1",proto:6,srcPort:12345,dstPort:443,bytes:100,packets:1,inIface:"ether1",outIface:"ether2"} +engine.queueParsedFlows(1,[flow]);mode="rollback" +await assert.rejects(engine.strictDrainEngine(async()=>{},[2]),/outside authorized drain scope/) +assert.equal(commits,0);assert.equal(dispatches,0) +await assert.rejects(engine.strictDrainEngine(),/known rollback/) +assert.equal(engine.getEngineStats().pendingSize,1) +assert.throws(()=>engine.resetEngineAfterDatabaseRestore(),/resolved|unresolved/i) +engine.queueParsedFlows(1,[{...flow,src:"192.0.2.2"}]);mode="unknown" +await assert.rejects(engine.strictDrainEngine(),/lost COMMIT/) +const attempted=commits,sql=dispatches +await assert.rejects(engine.strictDrainEngine(),/unknown/i) +assert.equal(commits,attempted);assert.equal(dispatches,sql) +assert.equal(engine.getEngineStats().pendingSize,2) +assert.throws(()=>engine.resetEngineAfterDatabaseRestore(),/resolved|unresolved/i) +receiptPresent=true +assert.equal((await engine.reconcileEnginePendingWrite()).state,"committed") +assert.equal(engine.getEngineStats().pendingSize,1) +assert.equal(engine.getEngineStats().rowsStored,1) +mode="normal";await engine.strictDrainEngine() +assert.ok(capQueries.length>0&&capQueries.every(query=>query.sql.includes("server_id=ANY(?::integer[])"))) +assert.equal(engine.getEngineStats().pendingSize,0) +engine.resetEngineAfterDatabaseRestore() +engine.queueParsedFlows(1,[flow]);const before=commits +await assert.rejects(engine.strictDrainEngine(async()=>{throw Error("revoked")}),/revoked/) +assert.equal(commits,before);assert.equal(engine.getEngineStats().pendingSize,1) +factsResetAllowed=false +assert.throws(()=>engine.resetEngineAfterDatabaseRestore(),/standalone facts/) +assert.equal(engine.getEngineStats().pendingSize,1,"fact-owner reset refusal precedes every engine clear") +factsResetAllowed=true;mutatePayload=true +await assert.rejects(engine.strictDrainEngine(),/payload changed/) +assert.equal(engine.getEngineStats().pendingSize,1) +mutatePayload=false +const retryExports:Record={} +runInNewContext(output,{exports:retryExports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout}) +const normalEngine=retryExports as unknown as Engine&{flushPending:(opts:{force:boolean;prune?:boolean})=>Promise} +normalEngine.queueParsedFlows(1,[flow]);mode="rollback" +await assert.rejects(normalEngine.flushPending({force:true}),/known rollback/) +const priorNormalAttempts=commits,priorNormalSql=dispatches +await assert.rejects(normalEngine.strictDrainEngine(async()=>{},[1]),/global prune/) +assert.equal(commits,priorNormalAttempts);assert.equal(dispatches,priorNormalSql) +assert.equal(normalEngine.getEngineStats().pendingSize,1) +assert.throws(()=>normalEngine.resetEngineAfterDatabaseRestore(),/unresolved/) +mode="normal";ripePruneFails=true +await assert.rejects(normalEngine.flushPending({force:true}),/RIPE prune SQL failure/) +assert.equal(normalEngine.getEngineStats().pendingSize,1) +ripePruneFails=false +const scopedExports:Record={} +runInNewContext(output,{exports:scopedExports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout}) +const scopedEngine=scopedExports as unknown as Engine&{configureEngine:(input:{topN:number})=>void;peekPendingFlows:()=>unknown[]} +scopedEngine.configureEngine({topN:1}) +scopedEngine.queueParsedFlows(1,[{...flow,bytes:1000}]) +scopedEngine.queueParsedFlows(999,Array.from({length:21},(_,index)=>({...flow,src:`203.0.113.${index+1}`,bytes:index===20?1:1000}))) +const scopedAttempts=commits +await assert.rejects(scopedEngine.strictDrainEngine(async()=>{},[1]),/Pending flow source/) +assert.equal(commits,scopedAttempts);assert.equal(scopedEngine.getEngineStats().pendingSize,22,"foreign below per-server minimum topN=20 input stays pending without detachment or discard") +assert.ok(source.indexOf("serverIds&&[...pending.values()]")={} +runInNewContext(ts.transpileModule(purgeSource,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,{exports:purgeExports,Error,process:{env:{}},require:(name:string)=>{if(name==="./partition-write-transaction.js")return {PARTITION_WRITER_ADVISORY_KEY};if(name==="../db/database-restore-quarantine.js")return {databaseRestoreQuarantineEnabled:()=>false};throw Error("Unexpected purge dependency: "+name)}}) +type PurgeDeps={stop:()=>Promise;deleteData:()=>Promise>;invalidate:()=>void;maintain:()=>Promise;restart:()=>Promise;withDataFence:(work:()=>Promise)=>Promise} +const executePurge=purgeExports.executeTrafficFlowPurge as (deps:PurgeDeps)=>Promise<{partitionError?:string}> +const order:string[]=[];let held=false,failDelete=false +const purgeDeps:PurgeDeps={ + stop:async()=>{order.push("drain");assert.equal(held,false)}, + withDataFence:async work=>{held=true;order.push("lock");try{return await work()}finally{held=false;order.push("unlock")}}, + deleteData:async()=>{assert.equal(held,true);order.push("delete");if(failDelete)throw Error("delete failed");return {buckets:1,minuteStats:1,minuteDims:1,dailyDims:1}}, + invalidate:()=>{assert.equal(held,true);order.push("invalidate")}, + maintain:async()=>{assert.equal(held,true);order.push("maintain");throw Error("partition partial")}, + restart:async()=>{assert.equal(held,false);order.push("restart")}, +} +assert.equal((await executePurge(purgeDeps)).partitionError,"partition partial") +assert.deepEqual(order,["drain","lock","delete","invalidate","maintain","unlock","restart"]) +order.length=0;failDelete=true +await assert.rejects(executePurge(purgeDeps),/delete failed/) +assert.deepEqual(order,["drain","lock","delete","unlock","restart"]) +console.log("traffic-flow-engine-owned.test.ts: actual connected engine source; known rollback retains batch, unknown prevents replay/reset, new arrivals isolated, explicit receipt reconcile acknowledges once, revocation blocks capture/dispatch; fake dependencies only") + + +// Read the exact detached candidate batch through the real engine exports. +// SQL-backed callers can merge only known-uncommitted rows; all uncertain +// intersections fail explicitly and unrelated server/windows remain readable. +const readNow=Date.now() +class ReadModelDate extends Date { + constructor(value?:string|number){super(value??readNow)} + static now(){return readNow} +} +function freshReadEngine():Engine { + const exports:Record={} + runInNewContext(output,{exports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout,Date:ReadModelDate}) + const next=exports as unknown as Engine + factsResetAllowed=true;mutatePayload=false;ripePruneFails=false + next.resetEngineAfterDatabaseRestore() + return next +} +const since=new Date(Date.now()-60000).toISOString() +const knownRead=freshReadEngine() +knownRead.queueParsedFlows(1,[flow]);mode="rollback" +await assert.rejects(knownRead.strictDrainEngine(async()=>{},[1,2]),/known rollback/) +assert.equal(knownRead.getEngineStats().pendingSize,1) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,100) +knownRead.queueParsedFlows(1,[{...flow,bytes:40,packets:2}]) +knownRead.queueParsedFlows(2,[{...flow,src:"192.0.2.2",bytes:25}]) +assert.equal(knownRead.peekPendingFlows().length,2) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,140) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].packets,3) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:1})[0].bytes,140) +const detachedView=knownRead.peekPendingFlows({serverId:1});detachedView[0].bytes=99999 +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,140,"view mutation cannot alter either detached proof or new arrivals") +mode="normal";await knownRead.strictDrainEngine(async()=>{},[1,2]) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,40) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:1})[0].bytes,140,"ACK moves detached rows to recent exactly once") +await knownRead.strictDrainEngine(async()=>{},[1,2]) +assert.equal(knownRead.peekPendingFlows().length,0) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:1})[0].bytes,140) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:2})[0].bytes,25) + +const unknownRead=freshReadEngine() +unknownRead.queueParsedFlows(1,[flow]);mode="unknown" +await assert.rejects(unknownRead.strictDrainEngine(async()=>{},[1,2]),/lost COMMIT/) +unknownRead.queueParsedFlows(1,[{...flow,bytes:40}]);unknownRead.queueParsedFlows(2,[{...flow,src:"192.0.2.2",bytes:25}]) +const readAttempts=commits,readDispatches=dispatches +const uncertain=(state:"running"|"unknown")=>(error:unknown)=>Boolean(error&&typeof error==="object"&&(error as {code?:string}).code==="FLOW_PENDING_UNCONFIRMED"&&(error as {batchState?:string}).batchState===state) +assert.throws(()=>unknownRead.peekPendingFlows({serverId:1}),uncertain("unknown")) +assert.throws(()=>unknownRead.listLiveFlowRows(since,{serverId:1}),uncertain("unknown")) +assert.throws(()=>unknownRead.peekPendingFlows(),uncertain("unknown")) +assert.equal(unknownRead.peekPendingFlows({serverId:2})[0].bytes,25) +assert.equal(unknownRead.listLiveFlowRows(since,{serverId:2})[0].bytes,25) +assert.equal(unknownRead.peekPendingFlows({serverId:1,sinceIso:new Date(Date.now()+3600000).toISOString()}).length,0) +assert.equal(unknownRead.peekPendingFlows({serverId:1,untilIso:new Date(Date.now()-3600000).toISOString()}).length,0) +assert.equal(commits,readAttempts);assert.equal(dispatches,readDispatches,"reads never replay/reconcile or dispatch SQL") +receiptPresent=false +assert.equal((await unknownRead.reconcileEnginePendingWrite()).state,"pending") +assert.equal(unknownRead.peekPendingFlows({serverId:1})[0].bytes,140,"barrier-proven absence restores known pending visibility") + +const committedUnknown=freshReadEngine() +committedUnknown.queueParsedFlows(1,[flow]);mode="unknown" +await assert.rejects(committedUnknown.strictDrainEngine(async()=>{},[1]),/lost COMMIT/) +committedUnknown.queueParsedFlows(1,[{...flow,bytes:40}]);receiptPresent=true +assert.equal((await committedUnknown.reconcileEnginePendingWrite()).state,"committed") +assert.equal(committedUnknown.peekPendingFlows({serverId:1})[0].bytes,40,"proven committed batch is not added to SQL pending merge") +assert.equal(committedUnknown.listLiveFlowRows(since,{serverId:1})[0].bytes,140) + +const runningRead=freshReadEngine() +runningRead.queueParsedFlows(1,[flow]);mode="held" +const started=new Promise(resolve=>{heldStarted=resolve}) +heldGate=new Promise(resolve=>{releaseHeld=resolve}) +const inFlight=runningRead.strictDrainEngine(async()=>{},[1,2]) +await started +runningRead.queueParsedFlows(2,[{...flow,src:"192.0.2.2",bytes:25}]) +assert.throws(()=>runningRead.peekPendingFlows({serverId:1}),uncertain("running")) +assert.throws(()=>runningRead.listLiveFlowRows(since,{serverId:1}),uncertain("running")) +assert.equal(runningRead.peekPendingFlows({serverId:2})[0].bytes,25) +mode="normal";releaseHeld();await inFlight +assert.equal(runningRead.peekPendingFlows({serverId:1}).length,0) +assert.equal(runningRead.listLiveFlowRows(since,{serverId:1})[0].bytes,100) +for(const options of [{serverId:0},{serverId:-1},{sinceIso:"invalid"},{untilIso:"invalid"},{sinceIso:"2026-02-02T00:00:00Z",untilIso:"2026-01-01T00:00:00Z"}])assert.throws(()=>runningRead.peekPendingFlows(options),/Invalid pending flow read scope/) +console.log("actual engine read boundary: known rollback+same-key arrivals merge once, detached view immutable, scoped running/unknown refusal, unrelated scope readable, explicit reconciliation resolves ambiguity without double count") + +const cappedRead=freshReadEngine() as Engine&{configureEngine:(options:{topN:number})=>void} +cappedRead.configureEngine({topN:1}) +cappedRead.queueParsedFlows(1,Array.from({length:21},(_,index)=>({...flow,src:`203.0.113.${index+1}`,bytes:index===20?1:1000}))) +mode="rollback" +await assert.rejects(cappedRead.strictDrainEngine(async()=>{},[1]),/known rollback/) +assert.equal(cappedRead.peekPendingFlows({serverId:1}).length,21,"known rollback retains all source rows, including the row outside persisted topN") +assert.equal(cappedRead.peekPendingFlows({serverId:1}).find(row=>row.src==="203.0.113.21")!.bytes,1) +assert.equal(cappedRead.listLiveFlowRows(since,{serverId:1}).length,21) +console.log("actual read topN model: all detached source rows remain visible until ACK; fixed model clock avoids minute-boundary races") + +// A maintenance no-op must never hide error-only evidence or an uncertain +// owned batch. Explicit normal retention work must still run on a fresh engine. +const errorOnly=freshReadEngine() as Engine&{setEngineError:(message:string)=>void} +errorOnly.setEngineError("synthetic collector error") +mode="unknown";receiptPresent=false +const errorAttempts=commits +await assert.rejects(errorOnly.strictDrainEngine(async()=>{},[]),/lost COMMIT/) +assert.equal(commits,errorAttempts+1) +await assert.rejects(errorOnly.strictDrainEngine(async()=>{},[]),/unknown/i) +assert.equal(commits,errorAttempts+1,"empty unknown batch cannot be skipped or replayed") +receiptPresent=true +assert.equal((await errorOnly.reconcileEnginePendingWrite()).state,"committed") +mode="normal" +const explicitPrune=freshReadEngine() as Engine&{flushPending:(opts:{force:boolean;prune:boolean})=>Promise} +const pruneAttempts=commits,pruneDispatches=dispatches +await explicitPrune.flushPending({force:true,prune:true}) +assert.equal(commits,pruneAttempts+1) +assert.ok(dispatches>pruneDispatches,"explicit retention/prune still dispatches on a fresh engine") +console.log("maintenance no-op: pristine/reset skip only; error-only unknown batch remains owned without replay; explicit prune preserved") + + +// A paused cold cache must not enqueue one pair of SQL loaders per datagram. +const coldWork = new DatabaseBackgroundWork() +let coldClock = 0 +let topologyLoads = 0 +let catalogLoads = 0 +let catalogReady = false +let topologyReady = false +let rejectTopology!: (error: Error) => void +let releaseCatalog!: () => void +const firstTopology = new Promise((_resolve, reject) => { rejectTopology = reject }) +const firstCatalog = new Promise(resolve => { releaseCatalog = resolve }) +const coldDependencies = { + ...dependencies, + "./database-background-work.js": { databaseBackgroundWork: coldWork }, + "./traffic-flow-topology.js": { + peekFlowTopology: () => topologyReady ? {} : null, + peekServerCatalog: () => catalogReady ? { byId: new Map() } : null, + loadFlowTopology: async () => { + topologyLoads++ + if (topologyLoads === 1) await firstTopology + topologyReady = true + }, + getServerCatalog: async () => { + catalogLoads++ + await firstCatalog + catalogReady = true + }, + }, +} +const coldExports: Record = {} +runInNewContext(output, { + exports: coldExports, + require: (name: keyof typeof coldDependencies) => coldDependencies[name], + performance: { now: () => coldClock }, + Buffer, console, setTimeout, clearTimeout, +}) +const coldEngine = coldExports as unknown as Engine +for (let i = 0; i < 128; i++) coldEngine.queueParsedFlows(1, []) +assert.deepEqual([topologyLoads, catalogLoads], [1, 1]) +let drained = false +const drain = coldWork.drain().then(() => { drained = true }) +await Promise.resolve() +assert.equal(drained, false) +rejectTopology(new Error("Owned topology load refused")) +releaseCatalog() +await drain +assert.equal(drained, true) +coldClock = 999 +for (let i = 0; i < 128; i++) coldEngine.queueParsedFlows(1, []) +assert.deepEqual([topologyLoads, catalogLoads], [1, 1]) +coldClock = 1000 +coldEngine.queueParsedFlows(1, []) +await coldWork.drain() +assert.deepEqual([topologyLoads, catalogLoads], [2, 1]) +assert.equal(topologyReady, true) diff --git a/backend/src/services/traffic-flow-engine-pending.test.ts b/backend/src/services/traffic-flow-engine-pending.test.ts new file mode 100644 index 00000000..ce026c6f --- /dev/null +++ b/backend/src/services/traffic-flow-engine-pending.test.ts @@ -0,0 +1,249 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import * as crypto from "node:crypto" +import ts from "typescript" +import * as owned from "./traffic-flow-owned-batch.js" +import * as proof from "./traffic-flow-batch-proof.js" +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 { pruneRipeSqlite } from "./traffic-flow-ripe.js" + +// Evaluate the exact review artifact with explicit fake boundaries. Production +// engine source stays untouched; unexpected runtime dependencies fail closed. +const patch=await readFile(new URL("../../../tmp-flow-engine-strict-pending.patch",import.meta.url),"utf8") +const source=patch.split(/\r?\n/).filter(line=>line.startsWith("+")&&!line.startsWith("+++" )).map(line=>line.slice(1)).join("\n") +assert.ok(source.includes("ownedEngineBatch?.assertResolved()")) +assert.ok(!source.includes("privateSecretFingerprint")) +const live=accumulator.createFlowFactAccumulator() +let mode="normal",commits=0,dispatches=0,inTransaction=false,receiptPresent=false,factsResetAllowed=true,mutatePayload=false,ripePruneFails=false +let identified:{rows:{bytes:number}[]}|undefined +let heldStarted:()=>void=()=>{},releaseHeld:()=>void=()=>{},heldGate:Promise=Promise.resolve() +const capQueries:Array<{sql:string;params:unknown}>=[] +class Unknown extends Error {} +const dependencies:Record={ + "node:crypto":crypto, + "./traffic-flow-owned-batch.js":owned,"./traffic-flow-batch-proof.js":{flowBatchProof:(payload:unknown,domain:"ingest"|"facts")=>{identified=payload as typeof identified;return proof.flowBatchProof(payload,domain)}}, + "./traffic-flow-fact-accumulator.js":accumulator,"./serial-operation.js":serial, + "./traffic-flow-parse.js":parse,"./traffic-flow-ip.js":ip, + "./database-background-work.js":{databaseBackgroundWork:{track:()=>{throw Error("unexpected background load")}}}, + "./traffic-flow-write-transaction.js":{ + FlowCommitUnknown:Unknown,flowWriteContext:()=>inTransaction?{partitions:new Set()}:undefined, + flowWritePool:{query:async()=>{assert.ok(inTransaction);dispatches++;return {rows:[]}}}, + flowWriteAll:async(sql:string,params:unknown)=>{assert.ok(inTransaction);dispatches++;if(sql.includes("FROM flow_daily_dims")&&sql.includes("dim = 'asn'"))capQueries.push({sql,params});return []}, + flowWriteQuery:async(sql:string)=>{assert.ok(inTransaction);dispatches++;if(ripePruneFails&&sql.includes("DELETE FROM flow_ip_meta"))throw Error("RIPE prune SQL failure");return {rows:[]}}, + commitFlowWrite:async(_input:unknown,work:()=>Promise,authorize:()=>Promise)=>{ + commits++;await authorize();inTransaction=true + if(mutatePayload&&identified?.rows[0])identified.rows[0].bytes++ + try{const result=await work();await authorize();if(mode==="held"){heldStarted();await heldGate}if(mode==="unknown")throw new Unknown("lost COMMIT");if(mode==="rollback")throw Error("known rollback");return result}finally{inTransaction=false} + }, + reconcileFlowWrite:async()=>({committed:receiptPresent,result:receiptPresent?{buckets:1}:null}), + }, + "../db/partitions.js":{specForParent:()=>null,ensurePartitionFor:()=>{throw Error("unexpected DDL")}}, + "./traffic-flow-planes.js":{classifyFlowPlaneLite:()=>"internet"}, + "./traffic-flow-map-exporter.js":{pickServerIdForExporter:()=>1}, + "./traffic-flow-apps.js":{applicationName:()=>"https"}, + "./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-facts-filter.js":{shouldWriteFlowFact:()=>true}, + "./traffic-flow-dest.js":{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,factsPendingSize:()=>live.size, + takePendingFlowFacts:()=>{const rows=live.snapshot();live.clear();return rows}, + persistFlowFactRows:async(rows:unknown[])=>{assert.ok(inTransaction);return rows.length}, + hourBucketIso:accumulator.hourBucketIso,resetFactsAfterDatabaseRestore:()=>live.clear(),assertFactsResetAllowed:()=>{if(!factsResetAllowed)throw Error("unresolved standalone facts")}, + }, +} +const output=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText +type ReadOptions={serverId?:number;sinceIso?:string;untilIso?:string} +type ReadRow={serverId:number;bucketAt:string;src:string;bytes:number;packets:number} +type Engine={peekPendingFlows:(options?:ReadOptions)=>ReadRow[];listLiveFlowRows:(sinceIso:string,options?:Omit)=>ReadRow[];queueParsedFlows:(id:number,flows:unknown[])=>void;strictDrainEngine:(authorize?:()=>Promise,serverIds?:readonly number[])=>Promise;getEngineStats:()=>{pendingSize:number;rowsStored:number};resetEngineAfterDatabaseRestore:()=>void;reconcileEnginePendingWrite:()=>Promise<{state:string}>} +const exports:Record={} +runInNewContext(output,{exports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout}) +const engine=exports as unknown as Engine +const strict=engine.strictDrainEngine +engine.strictDrainEngine=(authorize=async()=>{},serverIds=[1])=>strict(authorize,serverIds) +const flow={src:"192.0.2.1",dst:"198.51.100.1",proto:6,srcPort:12345,dstPort:443,bytes:100,packets:1,inIface:"ether1",outIface:"ether2"} +engine.queueParsedFlows(1,[flow]);mode="rollback" +await assert.rejects(engine.strictDrainEngine(async()=>{},[2]),/outside authorized drain scope/) +assert.equal(commits,0);assert.equal(dispatches,0) +await assert.rejects(engine.strictDrainEngine(),/known rollback/) +assert.equal(engine.getEngineStats().pendingSize,1) +assert.throws(()=>engine.resetEngineAfterDatabaseRestore(),/resolved|unresolved/i) +engine.queueParsedFlows(1,[{...flow,src:"192.0.2.2"}]);mode="unknown" +await assert.rejects(engine.strictDrainEngine(),/lost COMMIT/) +const attempted=commits,sql=dispatches +await assert.rejects(engine.strictDrainEngine(),/unknown/i) +assert.equal(commits,attempted);assert.equal(dispatches,sql) +assert.equal(engine.getEngineStats().pendingSize,2) +assert.throws(()=>engine.resetEngineAfterDatabaseRestore(),/resolved|unresolved/i) +receiptPresent=true +assert.equal((await engine.reconcileEnginePendingWrite()).state,"committed") +assert.equal(engine.getEngineStats().pendingSize,1) +assert.equal(engine.getEngineStats().rowsStored,1) +mode="normal";await engine.strictDrainEngine() +assert.ok(capQueries.length>0&&capQueries.every(query=>query.sql.includes("server_id=ANY(?::integer[])"))) +assert.equal(engine.getEngineStats().pendingSize,0) +engine.resetEngineAfterDatabaseRestore() +engine.queueParsedFlows(1,[flow]);const before=commits +await assert.rejects(engine.strictDrainEngine(async()=>{throw Error("revoked")}),/revoked/) +assert.equal(commits,before);assert.equal(engine.getEngineStats().pendingSize,1) +factsResetAllowed=false +assert.throws(()=>engine.resetEngineAfterDatabaseRestore(),/standalone facts/) +assert.equal(engine.getEngineStats().pendingSize,1,"fact-owner reset refusal precedes every engine clear") +factsResetAllowed=true;mutatePayload=true +await assert.rejects(engine.strictDrainEngine(),/payload changed/) +assert.equal(engine.getEngineStats().pendingSize,1) +mutatePayload=false +const retryExports:Record={} +runInNewContext(output,{exports:retryExports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout}) +const normalEngine=retryExports as unknown as Engine&{flushPending:(opts:{force:boolean;prune?:boolean})=>Promise} +normalEngine.queueParsedFlows(1,[flow]);mode="rollback" +await assert.rejects(normalEngine.flushPending({force:true}),/known rollback/) +const priorNormalAttempts=commits,priorNormalSql=dispatches +await assert.rejects(normalEngine.strictDrainEngine(async()=>{},[1]),/global prune/) +assert.equal(commits,priorNormalAttempts);assert.equal(dispatches,priorNormalSql) +assert.equal(normalEngine.getEngineStats().pendingSize,1) +assert.throws(()=>normalEngine.resetEngineAfterDatabaseRestore(),/unresolved/) +mode="normal";ripePruneFails=true +await assert.rejects(normalEngine.flushPending({force:true}),/RIPE prune SQL failure/) +assert.equal(normalEngine.getEngineStats().pendingSize,1) +ripePruneFails=false +const scopedExports:Record={} +runInNewContext(output,{exports:scopedExports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout}) +const scopedEngine=scopedExports as unknown as Engine&{configureEngine:(input:{topN:number})=>void;peekPendingFlows:()=>unknown[]} +scopedEngine.configureEngine({topN:1}) +scopedEngine.queueParsedFlows(1,[{...flow,bytes:1000}]) +scopedEngine.queueParsedFlows(999,Array.from({length:21},(_,index)=>({...flow,src:`203.0.113.${index+1}`,bytes:index===20?1:1000}))) +const scopedAttempts=commits +await assert.rejects(scopedEngine.strictDrainEngine(async()=>{},[1]),/Pending flow source/) +assert.equal(commits,scopedAttempts);assert.equal(scopedEngine.getEngineStats().pendingSize,22,"foreign below per-server minimum topN=20 input stays pending without detachment or discard") +assert.ok(source.indexOf("serverIds&&[...pending.values()]")!line.startsWith("+")).map(line=>line.slice(1)).join("\n") + const after=lines.filter(line=>!line.startsWith("-")).map(line=>line.slice(1)).join("\n") + assert.ok(purgeSource.includes(before),"pending purge hunk matches current source") + purgeSource=purgeSource.replace(before,after) +} +const purgeExports:Record={} +runInNewContext(ts.transpileModule(purgeSource,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,{exports:purgeExports,Error}) +type PurgeDeps={stop:()=>Promise;deleteData:()=>Promise>;invalidate:()=>void;maintain:()=>Promise;restart:()=>Promise;withDataFence:(work:()=>Promise)=>Promise} +const executePurge=purgeExports.executeTrafficFlowPurge as (deps:PurgeDeps)=>Promise<{partitionError?:string}> +const order:string[]=[];let held=false,failDelete=false +const purgeDeps:PurgeDeps={ + stop:async()=>{order.push("drain");assert.equal(held,false)}, + withDataFence:async work=>{held=true;order.push("lock");try{return await work()}finally{held=false;order.push("unlock")}}, + deleteData:async()=>{assert.equal(held,true);order.push("delete");if(failDelete)throw Error("delete failed");return {buckets:1,minuteStats:1,minuteDims:1,dailyDims:1}}, + invalidate:()=>{assert.equal(held,true);order.push("invalidate")}, + maintain:async()=>{assert.equal(held,true);order.push("maintain");throw Error("partition partial")}, + restart:async()=>{assert.equal(held,false);order.push("restart")}, +} +assert.equal((await executePurge(purgeDeps)).partitionError,"partition partial") +assert.deepEqual(order,["drain","lock","delete","invalidate","maintain","unlock","restart"]) +order.length=0;failDelete=true +await assert.rejects(executePurge(purgeDeps),/delete failed/) +assert.deepEqual(order,["drain","lock","delete","unlock","restart"]) +console.log("traffic-flow-engine-pending.test.ts: exact unapplied artifact; known rollback retains batch, unknown prevents replay/reset, new arrivals isolated, explicit receipt reconcile acknowledges once, revocation blocks capture/dispatch; fake dependencies only") + + +// Read the exact detached candidate batch through the real engine exports. +// SQL-backed callers can merge only known-uncommitted rows; all uncertain +// intersections fail explicitly and unrelated server/windows remain readable. +const readNow=Date.now() +class ReadModelDate extends Date { + constructor(value?:string|number){super(value??readNow)} + static now(){return readNow} +} +function freshReadEngine():Engine { + const exports:Record={} + runInNewContext(output,{exports,require:(name:string)=>{if(!(name in dependencies))throw Error("Unexpected engine dependency: "+name);return dependencies[name]},Buffer,console,setTimeout,clearTimeout,Date:ReadModelDate}) + const next=exports as unknown as Engine + factsResetAllowed=true;mutatePayload=false;ripePruneFails=false + next.resetEngineAfterDatabaseRestore() + return next +} +const since=new Date(Date.now()-60000).toISOString() +const knownRead=freshReadEngine() +knownRead.queueParsedFlows(1,[flow]);mode="rollback" +await assert.rejects(knownRead.strictDrainEngine(async()=>{},[1,2]),/known rollback/) +assert.equal(knownRead.getEngineStats().pendingSize,1) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,100) +knownRead.queueParsedFlows(1,[{...flow,bytes:40,packets:2}]) +knownRead.queueParsedFlows(2,[{...flow,src:"192.0.2.2",bytes:25}]) +assert.equal(knownRead.peekPendingFlows().length,2) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,140) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].packets,3) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:1})[0].bytes,140) +const detachedView=knownRead.peekPendingFlows({serverId:1});detachedView[0].bytes=99999 +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,140,"view mutation cannot alter either detached proof or new arrivals") +mode="normal";await knownRead.strictDrainEngine(async()=>{},[1,2]) +assert.equal(knownRead.peekPendingFlows({serverId:1})[0].bytes,40) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:1})[0].bytes,140,"ACK moves detached rows to recent exactly once") +await knownRead.strictDrainEngine(async()=>{},[1,2]) +assert.equal(knownRead.peekPendingFlows().length,0) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:1})[0].bytes,140) +assert.equal(knownRead.listLiveFlowRows(since,{serverId:2})[0].bytes,25) + +const unknownRead=freshReadEngine() +unknownRead.queueParsedFlows(1,[flow]);mode="unknown" +await assert.rejects(unknownRead.strictDrainEngine(async()=>{},[1,2]),/lost COMMIT/) +unknownRead.queueParsedFlows(1,[{...flow,bytes:40}]);unknownRead.queueParsedFlows(2,[{...flow,src:"192.0.2.2",bytes:25}]) +const readAttempts=commits,readDispatches=dispatches +const uncertain=(state:"running"|"unknown")=>(error:unknown)=>Boolean(error&&typeof error==="object"&&(error as {code?:string}).code==="FLOW_PENDING_UNCONFIRMED"&&(error as {batchState?:string}).batchState===state) +assert.throws(()=>unknownRead.peekPendingFlows({serverId:1}),uncertain("unknown")) +assert.throws(()=>unknownRead.listLiveFlowRows(since,{serverId:1}),uncertain("unknown")) +assert.throws(()=>unknownRead.peekPendingFlows(),uncertain("unknown")) +assert.equal(unknownRead.peekPendingFlows({serverId:2})[0].bytes,25) +assert.equal(unknownRead.listLiveFlowRows(since,{serverId:2})[0].bytes,25) +assert.equal(unknownRead.peekPendingFlows({serverId:1,sinceIso:new Date(Date.now()+3600000).toISOString()}).length,0) +assert.equal(unknownRead.peekPendingFlows({serverId:1,untilIso:new Date(Date.now()-3600000).toISOString()}).length,0) +assert.equal(commits,readAttempts);assert.equal(dispatches,readDispatches,"reads never replay/reconcile or dispatch SQL") +receiptPresent=false +assert.equal((await unknownRead.reconcileEnginePendingWrite()).state,"pending") +assert.equal(unknownRead.peekPendingFlows({serverId:1})[0].bytes,140,"barrier-proven absence restores known pending visibility") + +const committedUnknown=freshReadEngine() +committedUnknown.queueParsedFlows(1,[flow]);mode="unknown" +await assert.rejects(committedUnknown.strictDrainEngine(async()=>{},[1]),/lost COMMIT/) +committedUnknown.queueParsedFlows(1,[{...flow,bytes:40}]);receiptPresent=true +assert.equal((await committedUnknown.reconcileEnginePendingWrite()).state,"committed") +assert.equal(committedUnknown.peekPendingFlows({serverId:1})[0].bytes,40,"proven committed batch is not added to SQL pending merge") +assert.equal(committedUnknown.listLiveFlowRows(since,{serverId:1})[0].bytes,140) + +const runningRead=freshReadEngine() +runningRead.queueParsedFlows(1,[flow]);mode="held" +const started=new Promise(resolve=>{heldStarted=resolve}) +heldGate=new Promise(resolve=>{releaseHeld=resolve}) +const inFlight=runningRead.strictDrainEngine(async()=>{},[1,2]) +await started +runningRead.queueParsedFlows(2,[{...flow,src:"192.0.2.2",bytes:25}]) +assert.throws(()=>runningRead.peekPendingFlows({serverId:1}),uncertain("running")) +assert.throws(()=>runningRead.listLiveFlowRows(since,{serverId:1}),uncertain("running")) +assert.equal(runningRead.peekPendingFlows({serverId:2})[0].bytes,25) +mode="normal";releaseHeld();await inFlight +assert.equal(runningRead.peekPendingFlows({serverId:1}).length,0) +assert.equal(runningRead.listLiveFlowRows(since,{serverId:1})[0].bytes,100) +for(const options of [{serverId:0},{serverId:-1},{sinceIso:"invalid"},{untilIso:"invalid"},{sinceIso:"2026-02-02T00:00:00Z",untilIso:"2026-01-01T00:00:00Z"}])assert.throws(()=>runningRead.peekPendingFlows(options),/Invalid pending flow read scope/) +console.log("exact pending engine read boundary: known rollback+same-key arrivals merge once, detached view immutable, scoped running/unknown refusal, unrelated scope readable, explicit reconciliation resolves ambiguity without double count") + +const cappedRead=freshReadEngine() as Engine&{configureEngine:(options:{topN:number})=>void} +cappedRead.configureEngine({topN:1}) +cappedRead.queueParsedFlows(1,Array.from({length:21},(_,index)=>({...flow,src:`203.0.113.${index+1}`,bytes:index===20?1:1000}))) +mode="rollback" +await assert.rejects(cappedRead.strictDrainEngine(async()=>{},[1]),/known rollback/) +assert.equal(cappedRead.peekPendingFlows({serverId:1}).length,21,"known rollback retains all source rows, including the row outside persisted topN") +assert.equal(cappedRead.peekPendingFlows({serverId:1}).find(row=>row.src==="203.0.113.21")!.bytes,1) +assert.equal(cappedRead.listLiveFlowRows(since,{serverId:1}).length,21) +console.log("exact pending read topN model: all detached source rows remain visible until ACK; fixed model clock avoids minute-boundary races") diff --git a/backend/src/services/traffic-flow-engine.ts b/backend/src/services/traffic-flow-engine.ts index ecce51f2..35e6b786 100644 --- a/backend/src/services/traffic-flow-engine.ts +++ b/backend/src/services/traffic-flow-engine.ts @@ -1,8 +1,14 @@ +import {isDeepStrictEqual} from "node:util" +import {createFlowIngestQuality,type FlowIngestQuality} from "./traffic-flow-ingest-quality.js" import { databaseBackgroundWork } from "./database-background-work.js" -import { dbAll, dbQuery, pool } from "../db/index.js" +import { randomUUID } from "node:crypto" +import { flowWriteAll as dbAll, flowWriteQuery as dbQuery, flowWritePool as pool, flowWriteContext, commitFlowWrite, reconcileFlowWrite, FlowCommitUnknown } from "./traffic-flow-write-transaction.js" +import { OwnedFlowBatch } from "./traffic-flow-owned-batch.js" +import { flowBatchProof } from "./traffic-flow-batch-proof.js" +import { createFlowFactAccumulator, type FactRow } from "./traffic-flow-fact-accumulator.js" import { createSerialOperation } from "./serial-operation.js" import { ensurePartitionFor, specForParent } from "../db/partitions.js" -import { normalizeParsedFlow, parseFlowPacket, protoName, type ParsedFlow, type ParsedFlowInput } from "./traffic-flow-parse.js" +import { normalizeParsedFlow, parseFlowPacket, FlowTemplateRefused, protoName, type ParsedFlow, type ParsedFlowInput, type FlowDatagramTransport } from "./traffic-flow-parse.js" import { classifyFlowPlaneLite } from "./traffic-flow-planes.js" import { pickServerIdForExporter, type OverlayPeerRef } from "./traffic-flow-map-exporter.js" import { applicationName } from "./traffic-flow-apps.js" @@ -21,10 +27,13 @@ import { } from "./traffic-flow-topology.js" import { bumpFlowFact, + hasPendingFlowFact, factsPendingSize, - flushFlowFacts, + takePendingFlowFacts, + persistFlowFactRows, hourBucketIso, resetFactsAfterDatabaseRestore, + assertFactsResetAllowed, } from "./traffic-flow-facts.js" export const TICK_MS = 2_000 @@ -44,9 +53,10 @@ async function ensureParentPartition(parent: string, ts: string): Promise if (!spec) return const iso = ts.length === 10 ? `${ts}T00:00:00Z` : ts const key = `${parent}:${iso.slice(0, 10)}` - if (ensuredParts.has(key)) return + const parts=flowWriteContext()?.partitions??ensuredParts + if (parts.has(key)) return await ensurePartitionFor(pool, parent, spec.kind, new Date(iso)) - ensuredParts.add(key) + parts.add(key) } export interface PendingFlowRow { @@ -126,29 +136,6 @@ function sanitizeFlowRow(r: PendingFlowRow): PendingFlowRow | null { } } -function flowUpsertParams(r: PendingFlowRow) { - return { - serverId: r.serverId, - bucketAt: r.bucketAt, - src: r.src, - dst: r.dst, - proto: r.proto, - srcPort: r.srcPort, - dstPort: r.dstPort, - bytes: r.bytes, - packets: r.packets, - inIface: r.inIface, - outIface: r.outIface, - nextHop: inetOrNull(r.nextHop), - flowStartMs: r.flowStartMs, - flowEndMs: r.flowEndMs, - natSrc: inetOrNull(r.natSrc), - natDst: inetOrNull(r.natDst), - natSrcPort: r.natSrcPort, - natDstPort: r.natDstPort, - } -} - export interface EngineStats { packetsReceived: number lastExporterIp: string | null @@ -156,6 +143,7 @@ export interface EngineStats { lastDatagramAt: string | null dropped: number rowsStored: number + dataQuality:FlowIngestQuality pendingSize: number } @@ -187,15 +175,68 @@ export interface ExporterResolveCtx { hostIps: Map } +// Intended engine-local slice, not a generic owner/token map. Counts bound +// retained containers across the live maps AND the one detached owned batch. +export const FLOW_RETAINED_LIMITS = Object.freeze({rollups:4096,uniqueSources:100000,uniqueDestinations:100000,dimensions:50000,facts:16000,rings:2048,keyUnits:1024,fieldUnits:256}) +const MAX_BUFFERED_BYTES = Math.floor(Number.MAX_SAFE_INTEGER / 24) // at most three tick contributions ×8bits +interface RetainedAggregateUsage {uniqueSources:number;uniqueDestinations:number;bytes:number;packets:number;records:number} +let liveAggregateUsage:RetainedAggregateUsage={uniqueSources:0,uniqueDestinations:0,bytes:0,packets:0,records:0} +let retainedRingKeys=0 +let recentBytes=0,recentPackets=0 +const ingestQuality=createFlowIngestQuality() +let receivedRingSnapshot=false,lastRingQuality:FlowIngestQuality|null=null +function ringDataQuality():FlowIngestQuality|null {return receivedRingSnapshot?lastRingQuality:ingestQuality.snapshot()} + +function rejectAdmission(flow:ParsedFlow,reason:"aggregate-capacity"|"ring-capacity"|"key-width"|"unsafe-numeric",observedAt:string):false { + ingestQuality.reject(flow.bytes,flow.packets,reason,observedAt) + return false +} +function safeBufferedSum(a:number,b:number,c:number,maximum=Number.MAX_SAFE_INTEGER):boolean { + return Number.isSafeInteger(c)&&c>=0&&a<=maximum-b&&c<=maximum-a-b +} +function admitAggregateRecord(rollupCacheKey:string,flow:ParsedFlow,dims:Array<{dim:string;key:string;cacheKey:string}>,fact:FactRow|null,ticks:Array<{key:string;inBytes:number;outBytes:number}>,observedAt:string):boolean { + const held=ownedEngineBatch?.payload,old=held?.retainedUsage + if(!safeBufferedSum(liveAggregateUsage.bytes+recentBytes,old?.bytes??0,flow.bytes,MAX_BUFFERED_BYTES) + ||!safeBufferedSum(liveAggregateUsage.packets+recentPackets,old?.packets??0,flow.packets) + ||!safeBufferedSum(liveAggregateUsage.records,old?.records??0,1))return rejectAdmission(flow,"unsafe-numeric",observedAt) + // Preserve values; over-width or ambiguous keys are rejected explicitly. + const fields=[flow.src,flow.dst,flow.inIface,flow.outIface,flow.nextHop,flow.natSrc,flow.natDst,...dims.map(row=>row.key),...(fact?[fact.iface,fact.country,fact.service]:[])] + if(fields.some(value=>value.length>FLOW_RETAINED_LIMITS.fieldUnits||value.includes("\0")))return rejectAdmission(flow,"key-width",observedAt) + const rollup=minuteRollup.get(rollupCacheKey),newSource=flow.src&&!rollup?.srcs.has(flow.src)?1:0,newDestination=flow.dst&&!rollup?.dsts.has(flow.dst)?1:0 + const newDims=new Set(dims.filter(row=>row.key).map(row=>row.cacheKey).filter(key=>!minuteDims.has(key))) + if([...newDims].some(key=>key.length>FLOW_RETAINED_LIMITS.keyUnits))return rejectAdmission(flow,"key-width",observedAt) + if(minuteRollup.size+(held?.rollup.size??0)+(rollup?0:1)>FLOW_RETAINED_LIMITS.rollups + ||liveAggregateUsage.uniqueSources+(old?.uniqueSources??0)+newSource>FLOW_RETAINED_LIMITS.uniqueSources + ||liveAggregateUsage.uniqueDestinations+(old?.uniqueDestinations??0)+newDestination>FLOW_RETAINED_LIMITS.uniqueDestinations + ||minuteDims.size+(held?.dims.size??0)+newDims.size>FLOW_RETAINED_LIMITS.dimensions + ||factsPendingSize()+(held?.facts.length??0)+(fact&&!hasPendingFlowFact(fact)?1:0)>FLOW_RETAINED_LIMITS.facts)return rejectAdmission(flow,"aggregate-capacity",observedAt) + const addedRingKeys=new Set(ticks.map(row=>row.key).filter(key=>!tickAccum.has(key)&&!rings.has(key))) + if(retainedRingKeys+addedRingKeys.size>FLOW_RETAINED_LIMITS.rings)return rejectAdmission(flow,"ring-capacity",observedAt) + // This is the single synchronous commit point. All side-effect functions + // below MUST use the same prepared dim/fact data; no await splits admission. + liveAggregateUsage.uniqueSources+=newSource;liveAggregateUsage.uniqueDestinations+=newDestination + liveAggregateUsage.bytes+=flow.bytes;liveAggregateUsage.packets+=flow.packets;liveAggregateUsage.records++ + retainedRingKeys+=addedRingKeys.size + return true +} + let topN = 200 let retentionHours = 24 -const pending = new Map() +let pending = new Map() const recent = new Map() const tickAccum = new Map() -const rings = new Map() -const minuteRollup = new Map() -const minuteDims = new Map() +let rings = new Map() +let minuteRollup = new Map() +let minuteDims = new Map() +interface EngineBatch { + retainedUsage:RetainedAggregateUsage;ingestQuality:FlowIngestQuality; + rows:PendingFlowRow[]; pendingRows:PendingFlowRow[]; pendingCount:number; skippedInet:number; + rollup:Map; dims:Map; facts:FactRow[]; + force:boolean; prune:boolean; capturedTopN:number; capturedRetentionHours:number; stats:EngineStats; +} +let ownedEngineBatch:OwnedFlowBatch|null=null +export interface FlowEngineWriteReceipt { batchId:string; buckets:number; facts:number; skippedInet:number } let packetsReceived = 0 let lastExporterIp: string | null = null @@ -274,22 +315,21 @@ function bumpTick(key: string, inBytes: number, outBytes: number): void { tickAccum.set(key, prev) } -function addToTick(serverId: number, flow: ParsedFlow, bytes: number): void { - const plane = classifyFlowPlaneLite(flow) - if (plane === "mgmt") return - const bucket = plane === "overlay" ? RING_OVERLAY : plane === "client_mesh" ? RING_MESH : RING_PAYLOAD - bumpTick(ringKey(serverId, bucket), bytes, 0) - if (flow.inIface) bumpTick(ringKey(serverId, flow.inIface), bytes, 0) - if (flow.outIface && flow.outIface !== flow.inIface) bumpTick(ringKey(serverId, flow.outIface), 0, bytes) +function prepareFlowTicks(serverId:number,flow:ParsedFlow):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}, + ...(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}]:[])] } function emptyRing(): { inBps: number[]; outBps: number[] } { return { inBps: Array(RING_LEN).fill(0), outBps: Array(RING_LEN).fill(0) } } -function bumpDim(serverId: number, bucketAt: string, dim: string, key: string, bytes: number, packets: number): void { +function bumpDim(serverId: number, bucketAt: string, dim: string, key: string, bytes: number, packets: number,preparedKey?:string): void { if (!key) return - const k = dimKey(serverId, bucketAt, dim, key) + const k = preparedKey??dimKey(serverId, bucketAt, dim, key) const prev = minuteDims.get(k) if (prev) { prev.bytes += bytes @@ -299,8 +339,8 @@ function bumpDim(serverId: number, bucketAt: string, dim: string, key: string, b minuteDims.set(k, { bytes, packets }) } -function bumpRollup(serverId: number, bucketAt: string, flow: ParsedFlow, bytes: number, packets: number): void { - const k = rollupKey(serverId, bucketAt) +function bumpRollup(serverId: number, bucketAt: string, flow: ParsedFlow, bytes: number, packets: number,preparedKey?:string): void { + const k = preparedKey??rollupKey(serverId, bucketAt) let acc = minuteRollup.get(k) if (!acc) { acc = { bytes: 0, packets: 0, srcs: new Set(), dsts: new Set(), conversations: 0 } @@ -355,84 +395,59 @@ export function getEngineStats(): EngineStats { return { packetsReceived, lastExporterIp, - lastError, + lastError:ingestQuality.snapshot().localAdmission==="no-local-loss-observed"?lastError:`${lastError.slice(0,192)} NetFlow: часть записей потеряна при переполнении или отказе буфера; статистика неполная.`, + dataQuality:ingestQuality.snapshot(), lastDatagramAt, dropped, rowsStored, - pendingSize: pending.size, + pendingSize: pending.size+(ownedEngineBatch?.payload.pendingCount??0), } } +function coldSourceRefresh(load: () => Promise): () => void { + let loading = false + let retryAfter = 0 + return () => { + if (loading || performance.now() < retryAfter) return + loading = true + void databaseBackgroundWork.track(load()).then( + () => { loading = false }, + () => { retryAfter = performance.now() + 1000; loading = false }, + ) + } +} + +const refreshColdTopology = coldSourceRefresh(loadFlowTopology) +const refreshColdCatalog = coldSourceRefresh(getServerCatalog) + export function queueParsedFlows(serverId: number, flows: ParsedFlowInput[]): void { if (flows.length) bumpDataEpoch() - const bucketAt = minuteBucketIso() - const hourAt = hourBucketIso() - const ripeMisses: string[] = [] - const topo = peekFlowTopology() - const catalog = peekServerCatalog() - if (!topo) void databaseBackgroundWork.track(loadFlowTopology()).catch(() => {}) - if (!catalog) void databaseBackgroundWork.track(getServerCatalog()).catch(() => {}) + const at=Date.now(),bucketAt=minuteBucketIso(at),hourAt=hourBucketIso(at),observedAt=new Date(at).toISOString(),ripeMisses:string[]=[] + const topo = peekFlowTopology(), catalog = peekServerCatalog() + if (!topo) refreshColdTopology() + if (!catalog) refreshColdCatalog() const serverType = catalog?.byId.get(serverId)?.type for (const raw of flows) { const flow = normalizeParsedFlow(raw) - addToTick(serverId, flow, flow.bytes) - bumpRollup(serverId, bucketAt, flow, flow.bytes, flow.packets) - 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 ripe = destMeta.ripe - if (destMeta.dest && !ripe) ripeMisses.push(destMeta.dest) - const classified = destMeta.classified - const app = applicationName(flow.proto, flow.dstPort, flow.srcPort) - const country = destMeta.country - const asnKey = destMeta.asn ? String(destMeta.asn) : "unknown" - bumpDim(serverId, bucketAt, "proto", protoName(flow.proto), flow.bytes, flow.packets) - bumpDim(serverId, bucketAt, "app", app, flow.bytes, flow.packets) - bumpDim(serverId, bucketAt, "iface", flow.inIface || "__unknown__", flow.bytes, flow.packets) - bumpDim(serverId, bucketAt, "category", classified.category, flow.bytes, flow.packets) - bumpDim(serverId, bucketAt, "service", classified.service, flow.bytes, flow.packets) - if (country) bumpDim(serverId, bucketAt, "country", country, flow.bytes, flow.packets) - bumpDim(serverId, bucketAt, "asn", asnKey, flow.bytes, flow.packets) - if (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, - })) { - bumpFlowFact({ - serverId, - bucketAt: hourAt, - iface: canonicalFactIface(serverId, flow.inIface), - country: country || "XX", - service: classified.service, - asn: destMeta.asn, - bytes: flow.bytes, - packets: flow.packets, - }) - } - + 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 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)})) + 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) + 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) + for(const dim of dims)bumpDim(serverId,bucketAt,dim.dim,dim.key,flow.bytes,flow.packets,dim.cacheKey) + if(fact)bumpFlowFact(fact) + if(destMeta.dest&&!destMeta.ripe)ripeMisses.push(destMeta.dest) + // ORIGINAL RAW KEY / MERGE / CAP TAIL IS INSERTED HERE UNCHANGED. const key = pendingKey(serverId, bucketAt, flow) const prev = pending.get(key) if (prev) { @@ -465,10 +480,8 @@ export function queueParsedFlows(serverId: number, flows: ParsedFlowInput[]): vo if (ripeMisses.length) enqueueRipeMisses(ripeMisses) } -export function ingestDatagram(msg: Buffer, exporterIp: string): boolean { +export function ingestDatagram(msg: Buffer, exporterIp: string, transport?: FlowDatagramTransport): boolean { bumpPacketMeta(exporterIp) - const flows = parseFlowPacket(msg, exporterIp) - if (!flows.length) return true const serverId = resolveServerId(exporterIp) if (serverId == null) { setEngineError( @@ -476,6 +489,12 @@ export function ingestDatagram(msg: Buffer, exporterIp: string): boolean { ) return false } + // Stateful templates must never be retained for an unmapped exporter, + // including a template-only packet with no decoded flow records. + let flows: ParsedFlow[] + try { flows = parseFlowPacket(msg, exporterIp, transport) } + catch (error) { if (!(error instanceof FlowTemplateRefused)) throw error; setEngineError(error.message); return false } + if (!flows.length) return true setEngineError("") maybeRefreshIfaces(serverId) queueParsedFlows(serverId, flows) @@ -528,29 +547,54 @@ function mergeInto(map: Map, row: PendingFlowRow): void function pruneRecent(sinceMs = Date.now() - LIVE_WINDOW_MS): void { const cutoff = new Date(sinceMs).toISOString() for (const [key, row] of recent) { - if (row.bucketAt < cutoff) recent.delete(key) + if (row.bucketAt < cutoff) {recentBytes-=row.bytes;recentPackets-=row.packets;recent.delete(key)} } while (recent.size > MAX_PENDING) { const first = recent.keys().next().value if (first == null) break + const row=recent.get(first)!;recentBytes-=row.bytes;recentPackets-=row.packets recent.delete(first) } } -export function peekPendingFlows(): PendingFlowRow[] { - return [...pending.values()].map(toPendingRow) +export interface PendingFlowReadOptions { serverId?: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`) } +} +function pendingReadMatches(options:PendingFlowReadOptions):(row:Pick)=>boolean { + 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") + return row=>{ + if(options.serverId!==undefined&&row.serverId!==options.serverId)return false + const time=Date.parse(row.bucketAt) + if(!Number.isFinite(time))throw Error("Invalid pending flow timestamp") + return time>=since&&time<=until + } +} +/** Known rolled-back rows remain visible. Running and unknown batches may + * already be visible in SQL, so an intersecting read refuses to double count. + * This function performs no write, retry, reconciliation or live-state mutation. */ +export function peekPendingFlows(options:PendingFlowReadOptions={}): PendingFlowRow[] { + const matches=pendingReadMatches(options),batch=ownedEngineBatch + if(batch&&(batch.state==="running"||batch.state==="unknown")&&batch.payload.pendingRows.some(matches))throw new FlowPendingReadUnconfirmed(batch.state) + let sourceRows=0 + for(const entry of pending.values())if(matches(entry)&&++sourceRows>MAX_PENDING)throw Error("NetFlow pending snapshot too large; narrow the read scope") + if(batch?.state==="pending")for(const row of batch.payload.pendingRows)if(matches(row)&&++sourceRows>MAX_PENDING)throw Error("NetFlow pending snapshot too large; narrow the read scope") + const merged=new Map() + if(batch?.state==="pending")for(const row of batch.payload.pendingRows)if(matches(row))mergeInto(merged,row) + for(const entry of pending.values()){const row=toPendingRow(entry);if(matches(row))mergeInto(merged,row)} + return [...merged.values()] } -export function listLiveFlowRows(sinceIso: string): PendingFlowRow[] { - const merged = new Map() - for (const row of recent.values()) { - if (row.bucketAt < sinceIso) continue - mergeInto(merged, row) - } - for (const row of peekPendingFlows()) { - if (row.bucketAt < sinceIso) continue - mergeInto(merged, row) - } +export function listLiveFlowRows(sinceIso:string,options:Omit={}):PendingFlowRow[] { + const scope={...options,sinceIso},matches=pendingReadMatches(scope),merged=new Map() + // Resolve uncertainty before exposing a partial recent/pending result. + const pendingRows=peekPendingFlows(scope) + let sourceRows=pendingRows.length;for(const row of recent.values())if(matches(row)&&++sourceRows>MAX_PENDING)throw Error("NetFlow live snapshot too large; narrow the read scope") + for(const row of recent.values())if(matches(row))mergeInto(merged,row) + for(const row of pendingRows)mergeInto(merged,row) return [...merged.values()] } @@ -572,7 +616,7 @@ export function rollFlowRings(): void { ring.outBps.push(outBps) ring.outBps.shift() const silent = ring.inBps.every((v) => v === 0) && ring.outBps.every((v) => v === 0) - if (silent && !tickAccum.has(key)) rings.delete(key) + if (silent && !tickAccum.has(key)) { rings.delete(key);retainedRingKeys-- } } } @@ -582,6 +626,7 @@ export function getRingMbps(serverId: number, iface = RING_PAYLOAD): { rxNow: number txNow: number } { + if(ringDataQuality()?.localAdmission!=="no-local-loss-observed")throw Object.assign(Error("NetFlow local aggregation is incomplete or unobserved"),{code:"FLOW_AGGREGATES_UNCONFIRMED",statusCode:503}) const ring = rings.get(ringKey(serverId, iface)) const scale = 1_000_000 if (!ring) { @@ -603,14 +648,40 @@ export function snapshotRings(): Array<{ key: string; inBps: number[]; outBps: n })) } -export function applyRingSnapshot(rows: Array<{ key: string; inBps: number[]; outBps: number[] }>): void { - rings.clear() - for (const row of rows) { - rings.set(row.key, { inBps: row.inBps, outBps: row.outBps }) - } +/** Expose the existing flow-derived ring without manufacturing a zero-filled + * observation when absent. Slot times/initial padding cannot be proved by this + * cache, and these rates are not RouterOS wire counters. */ +export function observeFlowRing(serverId: number, iface = RING_PAYLOAD) { + const dataQuality=ringDataQuality() + const ring = dataQuality?.localAdmission==="no-local-loss-observed"?rings.get(ringKey(serverId, iface)):undefined + if (ring && ([ring.inBps, ring.outBps].some(series => !Array.isArray(series) || series.length !== RING_LEN || series.some(value => typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > Number.MAX_SAFE_INTEGER)))) throw new Error("Недостоверный flow ring выбранного сервера") + const ingress = ring ? ring.inBps.map(value => value / 1000000) : null, egress = ring ? ring.outBps.map(value => value / 1000000) : null + return { dataQuality, available: !!ring, ingressSeriesMbps: ingress, egressSeriesMbps: egress, + lastSlotIngressMbps: ingress?.at(-1) ?? null, lastSlotEgressMbps: egress?.at(-1) ?? null, + nominalIntervalMs: TICK_MS, maximumSlots: RING_LEN, paddingMayBePresent: true, slotTimestampsVerified: false, + source: "ingested flow bytes per nominal tick", wireRateVerified: false } } -async function persistListenerStats(force = false): Promise { +export function applyRingSnapshot(rows: Array<{ key: string; inBps: number[]; outBps: number[] }>,quality:FlowIngestQuality|null=null): void { + if(!Array.isArray(rows)||rows.length>FLOW_RETAINED_LIMITS.rings||rows.some(row=>!row||typeof row!=="object"||typeof row.key!=="string"||row.key.length>FLOW_RETAINED_LIMITS.keyUnits + ||!Array.isArray(row.inBps)||!Array.isArray(row.outBps)||row.inBps.length!==RING_LEN||row.outBps.length!==RING_LEN + ||[...row.inBps,...row.outBps].some(value=>!Number.isFinite(value)||value<0||value>Number.MAX_SAFE_INTEGER)))throw Error("Invalid or oversized NetFlow ring snapshot") + const incoming=new Set(rows.map(row=>row.key)),all=new Set([...incoming,...tickAccum.keys()]) + if(incoming.size!==rows.length||all.size>FLOW_RETAINED_LIMITS.rings)throw Error("NetFlow ring snapshot exceeds retained key bound") + const next=new Map(rows.map(row=>[row.key,{inBps:[...row.inBps],outBps:[...row.outBps]}])) + retainedRingKeys=all.size;receivedRingSnapshot=true;lastRingQuality=quality + rings=next +} + +/** A rejected IPC frame cannot leave a previous ring looking current. Pending + * tick contributions still belong to the engine and are never discarded. */ +export function markFlowRingSnapshotUnavailable():void { + rings=new Map();retainedRingKeys=tickAccum.size + receivedRingSnapshot=true;lastRingQuality=null +} + +async function persistListenerStats(force = false, observed:EngineStats=getEngineStats()): Promise { + const {packetsReceived,lastDatagramAt,lastExporterIp,lastError}=observed if ( lastPersistedStats && lastPersistedStats.packetsReceived === packetsReceived @@ -652,8 +723,8 @@ async function persistListenerStats(force = false): Promise { return true } -async function upsertMinuteAndDaily(): Promise { - for (const [k, acc] of minuteRollup) { +async function upsertMinuteAndDaily(rollup:Map,dims:Map): Promise { + for (const [k, acc] of rollup) { const [serverIdRaw, bucketAt] = k.split("\0") await ensureParentPartition("flow_minute_stats", bucketAt ?? "") await dbQuery(` @@ -678,7 +749,7 @@ async function upsertMinuteAndDaily(): Promise { conversations: acc.conversations, }) } - for (const [k, acc] of minuteDims) { + for (const [k, acc] of dims) { const [serverIdRaw, bucketAt, dim, key] = k.split("\0") await ensureParentPartition("flow_minute_dims", bucketAt ?? "") await ensureParentPartition("flow_daily_dims", dayKey(bucketAt ?? "")) @@ -713,18 +784,17 @@ async function upsertMinuteAndDaily(): Promise { }) } } - minuteRollup.clear() - minuteDims.clear() } -async function capDailyAsn(): Promise { +async function capDailyAsn(serverIds?:readonly number[]): Promise { const today = nowIso().slice(0, 10) const rows = await dbAll<{ serverId: number; key: string; bytes: number; packets: number }>(` SELECT server_id AS "serverId", key, bytes, packets FROM flow_daily_dims WHERE day = ? AND dim = 'asn' + ${serverIds?"AND server_id=ANY(?::integer[])":""} ORDER BY server_id, bytes DESC - `, [today]) + `, serverIds?[today,[...serverIds]]:[today]) const byServer = new Map() for (const row of rows) { const list = byServer.get(row.serverId) ?? [] @@ -757,19 +827,24 @@ async function capDailyAsn(): Promise { } } -async function pruneStored(): Promise { +async function pruneStored(retentionHoursForBatch=retentionHours,topNForBatch=topN,authorize:()=>Promise=async()=>{}): Promise { const now = Date.now() if (now - lastPruneAt < PRUNE_MS) return lastPruneAt = now - const flowCutoff = new Date(now - retentionHours * 3600_000).toISOString() + const flowCutoff = new Date(now - retentionHoursForBatch * 3600_000).toISOString() const minuteCutoff = new Date(now - MINUTE_RETENTION_HOURS * 3600_000).toISOString() const dailyCutoff = new Date(now - DAILY_RETENTION_DAYS * 86400_000).toISOString().slice(0, 10) + await authorize() await dbQuery(`DELETE FROM flow_buckets WHERE bucket_at < ?`, [flowCutoff]) + await authorize() await dbQuery(`DELETE FROM flow_minute_stats WHERE bucket_at < ?`, [minuteCutoff]) + await authorize() await dbQuery(`DELETE FROM flow_minute_dims WHERE bucket_at < ?`, [minuteCutoff]) + await authorize() await dbQuery(`DELETE FROM flow_daily_dims WHERE day < ?`, [dailyCutoff]) - const keep = Math.max(20, topN) + const keep = Math.max(20, topNForBatch) + await authorize() await dbQuery(` DELETE FROM flow_buckets fb USING ( @@ -792,7 +867,9 @@ async function pruneStored(): Promise { AND fb.dst_port = drop_rows.dst_port AND fb.in_iface = drop_rows.in_iface `, [keep]) - await pruneRipeSqlite(now) + await authorize() + await pruneRipeSqlite(now,{query:async(sql,params)=>{await authorize();return dbQuery(sql,params)},strict:true}) + await authorize() } function topNPending(rows: PendingFlowRow[]): PendingFlowRow[] { @@ -887,134 +964,101 @@ async function upsertFlowBucketsBatch(rows: PendingFlowRow[]): Promise { }) } -const FLOW_UPSERT_SQL = ` - INSERT INTO flow_buckets ( - server_id, bucket_at, src, dst, proto, src_port, dst_port, bytes, packets, in_iface, out_iface, next_hop, flow_start_ms, flow_end_ms, nat_src, nat_dst, nat_src_port, nat_dst_port - ) VALUES ( - @serverId, @bucketAt, @src, @dst, @proto, @srcPort, @dstPort, @bytes, @packets, @inIface, @outIface, @nextHop, @flowStartMs, @flowEndMs, @natSrc, @natDst, @natSrcPort, @natDstPort - ) - ON CONFLICT(server_id, bucket_at, src, dst, proto, src_port, dst_port, in_iface) - DO UPDATE SET - bytes = flow_buckets.bytes + excluded.bytes, - packets = flow_buckets.packets + excluded.packets, - out_iface = CASE WHEN excluded.out_iface != '' THEN excluded.out_iface ELSE flow_buckets.out_iface END, - next_hop = COALESCE(excluded.next_hop, flow_buckets.next_hop), - flow_start_ms = CASE - WHEN excluded.flow_start_ms > 0 AND (flow_buckets.flow_start_ms = 0 OR excluded.flow_start_ms < flow_buckets.flow_start_ms) - THEN excluded.flow_start_ms ELSE flow_buckets.flow_start_ms END, - flow_end_ms = GREATEST(flow_buckets.flow_end_ms, excluded.flow_end_ms), - nat_src = COALESCE(excluded.nat_src, flow_buckets.nat_src), - nat_dst = COALESCE(excluded.nat_dst, flow_buckets.nat_dst), - nat_src_port = CASE WHEN excluded.nat_src_port > 0 THEN excluded.nat_src_port ELSE flow_buckets.nat_src_port END, - nat_dst_port = CASE WHEN excluded.nat_dst_port > 0 THEN excluded.nat_dst_port ELSE flow_buckets.nat_dst_port END - ` - -async function upsertFlowBuckets(rows: PendingFlowRow[]): Promise { - if (rows.length === 0) return 0 - try { - await upsertFlowBucketsBatch(rows) - return rows.length - } catch (err) { - const message = err instanceof Error ? err.message : String(err) - setEngineError(`flow_buckets: ${message}`) - let stored = 0 - for (const r of rows) { - try { - await ensureParentPartition("flow_buckets", r.bucketAt) - await dbQuery(FLOW_UPSERT_SQL, flowUpsertParams(r)) - stored += 1 - } catch (rowErr) { - if (stored === 0 && lastError.startsWith("flow_buckets:")) { - const rowMsg = rowErr instanceof Error ? rowErr.message : String(rowErr) - setEngineError(`flow_buckets: ${rowMsg}`) - } - } - } - return stored - } -} - const serializeFlush = createSerialOperation() -export function flushPending(opts?: { force?: boolean; prune?: boolean }): Promise { - return serializeFlush(() => flushPendingInternal(opts)) +// Installed only while an existing worker is retiring. The dynamic closure also +// fences a timer flush that was already awaiting SQL when shutdown arrived. +let lifecycleAuthorization: (() => Promise) | undefined +export function setFlowEngineLifecycleAuthorization(authorize: (() => Promise) | undefined): void { + lifecycleAuthorization = authorize +} +const authorizeEngineLifecycle = async () => { await lifecycleAuthorization?.() } + +export function flushPending(opts?: { force?: boolean; prune?: boolean }, authorize?: () => Promise): Promise { + return serializeFlush(async () => { await flushPendingInternal(opts, authorize) }) } -async function flushPendingInternal(opts?: { force?: boolean; prune?: boolean }): Promise { +function acknowledgeEngineBatch(batch:OwnedFlowBatch,receipt:{buckets:number}):void { + for(const row of batch.payload.rows){mergeInto(recent,row);recentBytes+=row.bytes;recentPackets+=row.packets} + rowsStored+=receipt.buckets + if(receipt.buckets>0)bumpDataEpoch() + lastPersistAt=Date.now();lastFlushedMinute=minuteBucketIso() + lastFlushUsedTransaction=batch.payload.pendingCount>0||batch.payload.rollup.size>0||batch.payload.dims.size>0||batch.payload.facts.length>0 pruneRecent() - rollFlowRings() - const force = Boolean(opts?.force) - const doPrune = opts?.prune !== false - const hasWork = pending.size > 0 || minuteRollup.size > 0 || minuteDims.size > 0 || factsPendingSize() > 0 - const due = persistDue(force, hasWork) - try { - await persistListenerStats(force) - } catch { - /* settings row may be absent in unit tests */ - } + ownedEngineBatch=null +} - if (!hasWork) { - if (force && doPrune) { - try { - await pruneStored() - } catch { - /* prune best-effort */ - } - } - lastFlushUsedTransaction = false - return +async function flushPendingInternal(opts?:{force?:boolean;prune?:boolean},authorize:()=>Promise=authorizeEngineLifecycle,serverIds?:readonly number[]):Promise { + await authorize();pruneRecent();rollFlowRings() + if(!ownedEngineBatch) { + if(serverIds&&[...pending.values()].some(entry=>!serverIds.includes(entry.serverId)))throw Error("Pending flow source contains records outside authorized drain scope") + const force=Boolean(opts?.force),hasWork=pending.size>0||minuteRollup.size>0||minuteDims.size>0||factsPendingSize()>0||ingestQuality.hasDelta() + const observedStats=getEngineStats() + const statsDirty=!lastPersistedStats||lastPersistedStats.packetsReceived!==observedStats.packetsReceived||lastPersistedStats.lastDatagramAt!==observedStats.lastDatagramAt||lastPersistedStats.lastExporterIp!==observedStats.lastExporterIp||lastPersistedStats.lastError!==observedStats.lastError + // A fresh/reset isolate has no observation to persist over restored stats. + // Maintenance drains must still commit actual pending data, errors and all + // existing owned batches; this only skips an entirely unobserved no-op. + if(force&&opts?.prune===false&&!hasWork&&!lastPersistedStats&&packetsReceived===0&&lastDatagramAt===null&&lastExporterIp===null&&!lastError&&dropped===0&&rowsStored===0){lastFlushUsedTransaction=false;return null} + if(!force&&!persistDue(false,hasWork)&&!(statsDirty&&Date.now()-lastStatsPersistAt>=STATS_PERSIST_MS)){lastFlushUsedTransaction=false;return null} + const rows:PendingFlowRow[]=[];let skippedInet=0 + for(const row of topNPending([...pending.values()].map(toPendingRow))){const clean=sanitizeFlowRow(row);if(clean)rows.push(clean);else skippedInet++} + // Detach synchronously before any await. New arrivals always target new maps. + const facts=takePendingFlowFacts(),payload:EngineBatch={retainedUsage:{...liveAggregateUsage},ingestQuality:ingestQuality.detachDelta(),rows,pendingRows:[...pending.values()].map(toPendingRow),pendingCount:pending.size,skippedInet,rollup:minuteRollup,dims:minuteDims,facts,force,prune:opts?.prune!==false,capturedTopN:topN,capturedRetentionHours:retentionHours,stats:getEngineStats()} + ownedEngineBatch=new OwnedFlowBatch(randomUUID(),"",payload) + pending=new Map();minuteRollup=new Map();minuteDims=new Map();liveAggregateUsage={uniqueSources:0,uniqueDestinations:0,bytes:0,packets:0,records:0} } - if (!due) { - lastFlushUsedTransaction = false - return + const batch=ownedEngineBatch + if(serverIds){ + if(batch.payload.prune)throw Error("Strict drain refuses a previously captured batch with global prune; resolve it through normal owned writer first") + const scoped=new Set(serverIds) + const ids=[...batch.payload.pendingRows.map(row=>row.serverId),...batch.payload.rows.map(row=>row.serverId),...batch.payload.facts.map(row=>row.serverId),...[...batch.payload.rollup.keys(),...batch.payload.dims.keys()].map(key=>Number(key.split("\0",1)[0]))] + if(ids.some(id=>!scoped.has(id)))throw Error("Existing flow batch contains records outside authorized drain scope") } + if(!batch.sourceProof)batch.prove(flowBatchProof({...batch.payload,rollup:[...batch.payload.rollup].map(([key,value])=>[key,{...value,srcs:[...value.srcs],dsts:[...value.dsts]}]),dims:[...batch.payload.dims]},"ingest")) + const assertPayload=()=>batch.assertProof(flowBatchProof({...batch.payload,rollup:[...batch.payload.rollup].map(([key,value])=>[key,{...value,srcs:[...value.srcs],dsts:[...value.dsts]}]),dims:[...batch.payload.dims]},"ingest")) + assertPayload() + batch.begin() + const previous={lastPruneAt,lastPersistedStats,lastStatsPersistAt} + try { + const result=await commitFlowWrite({id:batch.id,kind:"ingest",sourceProof:batch.sourceProof},async()=>{ + await authorize();await persistListenerStats(batch.payload.force,batch.payload.stats) + await upsertFlowBucketsBatch(batch.payload.rows) + await upsertMinuteAndDaily(batch.payload.rollup,batch.payload.dims);await capDailyAsn(serverIds) + const accumulator=createFlowFactAccumulator();for(const row of batch.payload.facts)accumulator.bump(row) + const facts=await persistFlowFactRows(accumulator.collectCapped()) + if(batch.payload.prune){await authorize();await pruneStored(batch.payload.capturedRetentionHours,batch.payload.capturedTopN,authorize)} + await authorize() + assertPayload() + return {buckets:batch.payload.rows.length,facts,skippedInet:batch.payload.skippedInet,ingestQuality:batch.payload.ingestQuality} + },authorize) + if(!isDeepStrictEqual(result.ingestQuality,batch.payload.ingestQuality))throw new FlowCommitUnknown("Flow receipt quality does not match owned source") + batch.committed();acknowledgeEngineBatch(batch,result) + if(result.skippedInet)setEngineError(`flow_buckets: пропуск ${result.skippedInet} строк с невалидным IP`) + return {batchId:batch.id,...result} + }catch(error){ + lastPruneAt=previous.lastPruneAt;lastPersistedStats=previous.lastPersistedStats;lastStatsPersistAt=previous.lastStatsPersistAt + if(batch.state==="running"){if(error instanceof FlowCommitUnknown)batch.unknown();else batch.rolledBack()} + lastFlushUsedTransaction=false + throw error + } +} - const sanitized: PendingFlowRow[] = [] - let skippedInet = 0 - for (const row of topNPending([...pending.values()].map(toPendingRow))) { - const clean = sanitizeFlowRow(row) - if (!clean) { - skippedInet += 1 - continue - } - sanitized.push(clean) - } - pending.clear() - for (const row of sanitized) mergeInto(recent, row) - if (skippedInet > 0) { - setEngineError(`flow_buckets: пропуск ${skippedInet} строк с невалидным IP`) - } - - lastFlushUsedTransaction = false - lastPersistAt = Date.now() - lastFlushedMinute = minuteBucketIso() - try { - const stored = await upsertFlowBuckets(sanitized) - lastFlushUsedTransaction = stored === sanitized.length - rowsStored += stored - if (stored > 0) bumpDataEpoch() - } catch (err) { - const message = err instanceof Error ? err.message : String(err) - setEngineError(`flow_buckets: ${message}`) - } - try { - await upsertMinuteAndDaily() - await capDailyAsn() - } catch { - /* rollup best-effort */ - } - try { - await flushFlowFacts() - } catch { - /* statistics cube best-effort */ - } - if (doPrune) { - try { - await pruneStored() - } catch { - /* prune best-effort */ - } - } +/** Flushes the existing isolate only; callers separately request a drain from an + * already-running worker. No collector or timer is started by this API. */ +export function strictDrainEngine(authorize:()=>Promise,serverIds:readonly number[]):Promise { + if(!Array.isArray(serverIds)||serverIds.length>1000||new Set(serverIds).size!==serverIds.length||serverIds.some(id=>!Number.isSafeInteger(id)||id<=0))return Promise.reject(Error("Invalid strict drain server scope")) + const scope=[...serverIds] + return serializeFlush(()=>flushPendingInternal({force:true,prune:false},authorize,scope)) +} +export function reconcileEnginePendingWrite(authorize:()=>Promise=async()=>{}) { + return serializeFlush(async()=>{ + if(!ownedEngineBatch||ownedEngineBatch.state!=="unknown")return {state:ownedEngineBatch?.state??"idle"} + const batch=ownedEngineBatch,result=await reconcileFlowWrite({id:batch.id,kind:"ingest",sourceProof:batch.sourceProof},authorize) + if(result.committed&&!isDeepStrictEqual(result.result?.ingestQuality,batch.payload.ingestQuality))throw Error("Unconfirmed NetFlow receipt quality; batch remains unknown") + batch.reconciled(result.committed?"committed":"absent-after-barrier") + if(result.committed)acknowledgeEngineBatch(batch,{buckets:Number(result.result?.buckets??0)}) + return {state:batch.state,batchId:batch.id} + }) } export function lastFlushUsedTransactionForTests(): boolean { @@ -1030,7 +1074,7 @@ export async function flushPendingForTests(): Promise { } export function onEngineTick(): void { - void flushPending() + void flushPending().catch(()=>setEngineError("Не подтверждено сохранение flow batch; данные остаются owned")) } export function ingestParsedFlowsForServerForTests(serverId: number, flows: ParsedFlowInput[]): void { @@ -1045,8 +1089,12 @@ export function resetEngineForTests(): void { /** Writers are drained before replacing the database; discard data from the old snapshot. */ export function resetEngineAfterDatabaseRestore(): void { + ownedEngineBatch?.assertResolved() + assertFactsResetAllowed() + liveAggregateUsage={uniqueSources:0,uniqueDestinations:0,bytes:0,packets:0,records:0};retainedRingKeys=0;ingestQuality.reset() + ownedEngineBatch=null pending.clear() - recent.clear() + recent.clear();recentBytes=0;recentPackets=0;receivedRingSnapshot=false;lastRingQuality=null tickAccum.clear() rings.clear() minuteRollup.clear() diff --git a/backend/src/services/traffic-flow-fact-accumulator.test.ts b/backend/src/services/traffic-flow-fact-accumulator.test.ts new file mode 100644 index 00000000..e34f8218 --- /dev/null +++ b/backend/src/services/traffic-flow-fact-accumulator.test.ts @@ -0,0 +1,89 @@ +import assert from "node:assert/strict" +import { + createFlowFactAccumulator, FACT_ASN_TOP, FACT_TUPLE_CAP, FACT_SERVICE_MAX_LEN, + normalizeFactAsn, normalizeFactCountry, normalizeFactIface, normalizeFactService, + type FactRow, +} from "./traffic-flow-fact-accumulator.js" + +const hour="2026-10-24T10:00:00.000Z" +const row=(patch:Partial={}):FactRow=>({serverId:1,bucketAt:hour,iface:"ether1",country:"US",service:"https",asn:15169,bytes:10,packets:1,...patch}) +const totals=(rows:FactRow[])=>rows.reduce((sum,row)=>({bytes:sum.bytes+row.bytes,packets:sum.packets+row.packets}),{bytes:0,packets:0}) + +// Ownership includes input rows, snapshot rows and collected rows, not just separate maps. +const live=createFlowFactAccumulator(),job=createFlowFactAccumulator() +const frozen=Object.freeze(row()) +live.bump(frozen);job.bump(row({bytes:99})) +const snapshot=live.snapshot();snapshot[0].bytes=999;snapshot[0].iface="mutated";snapshot.push(row()) +assert.deepEqual(live.snapshot(),[row()]);assert.deepEqual(job.snapshot(),[row({bytes:99})]) +assert.equal(live.size,1);assert.equal(job.size,1) +const collected=job.collectCapped();assert.equal(job.size,0);assert.deepEqual(live.snapshot(),[row()]) +collected[0].bytes=1234;job.bump(row({bytes:2}));assert.equal(job.snapshot()[0].bytes,2) +job.clear();assert.equal(job.size,0);assert.deepEqual(live.collectCapped(),[row()]);assert.deepEqual(live.collectCapped(),[]) + +// Normalization collisions must merge before any cap, without modifying inputs. +const normalized=createFlowFactAccumulator(),longService="x".repeat(FACT_SERVICE_MAX_LEN) +normalized.bump(Object.freeze(row({iface:" ether1 ",country:" us ",service:` ${longService}first `,asn:15169.9,bytes:7,packets:2}))) +normalized.bump(Object.freeze(row({iface:"ether1",country:"US",service:`${longService}second`,asn:15169,bytes:3,packets:4}))) +normalized.bump(row({iface:" ",country:"invalid",service:" ",asn:Infinity,bytes:5,packets:2})) +normalized.bump(row({iface:"",country:"",service:"",asn:-1,bytes:2,packets:3})) +assert.equal(normalized.size,2) +assert.deepEqual(normalized.collectCapped(),[ + row({service:longService,bytes:10,packets:6}), + row({iface:"__unknown__",country:"XX",service:"other",asn:0,bytes:7,packets:5}), +]) +assert.deepEqual([NaN,Infinity,-1,0,0.5,2.9].map(normalizeFactAsn),[0,0,0,0,0,2]) +assert.equal(normalizeFactCountry(" é "),"XX");assert.equal(normalizeFactCountry(" zz "),"ZZ") +assert.equal(normalizeFactIface(" 0 "),"0");assert.equal(normalizeFactService("\t"),"other") + +// Equal-weight ASNs retain first-seen order, including zero's participation in ranking. +const asnTies=createFlowFactAccumulator() +for(let asn=FACT_ASN_TOP+1;asn>=1;asn--)asnTies.bump(row({asn,bytes:1})) +const tied=asnTies.collectCapped() +assert.deepEqual(tied.map(row=>row.asn),[...Array.from({length:FACT_ASN_TOP},(_,i)=>FACT_ASN_TOP+1-i),0]) +assert.deepEqual(totals(tied),{bytes:201,packets:201}) +const zeroRank=createFlowFactAccumulator();zeroRank.bump(row({asn:0,bytes:1000})) +for(let asn=1;asn<=FACT_ASN_TOP;asn++)zeroRank.bump(row({asn,bytes:1})) +const zeroResult=zeroRank.collectCapped() +assert.equal(zeroResult.find(row=>row.asn===0)?.bytes,1001) +assert.ok(!zeroResult.some(row=>row.asn===FACT_ASN_TOP));assert.equal(zeroResult.length,FACT_ASN_TOP) + +// Tuple fallback is per iface. It can exceed 8000 output rows and can merge a kept fallback tuple. +const tuples=createFlowFactAccumulator() +for(let i=0;irow.service),Array.from({length:FACT_TUPLE_CAP},(_,i)=>`svc-${i}`)) +assert.deepEqual(tupleResult.slice(FACT_TUPLE_CAP),[ + row({iface:"a",country:"XX",service:"other",asn:0,bytes:2,packets:2}), + row({iface:"b",country:"XX",service:"other",asn:0,bytes:1,packets:1}), +]) +assert.deepEqual(totals(tupleResult),{bytes:8003,packets:8003}) +const collision=createFlowFactAccumulator();collision.bump(row({iface:"a",country:"XX",service:"other",asn:0,bytes:50,packets:5})) +for(let i=0;irow.asn),[...Array.from({length:200},(_,i)=>i+1),0]) + +// Keep legacy fallback behavior: delimiter-corrupted/unparseable keys drop at collection, +// finite negative server IDs and non-ISO buckets are not newly rejected by this extraction. +const legacy=createFlowFactAccumulator() +legacy.bump(row({iface:"bad\0iface"}));legacy.bump(row({serverId:NaN})) +legacy.bump(row({serverId:-1,bucketAt:"unvalidated",bytes:-2,packets:-1})) +assert.equal(legacy.size,3) +assert.deepEqual(legacy.collectCapped(),[row({serverId:-1,bucketAt:"unvalidated",bytes:-2,packets:-1})]) +assert.equal(legacy.size,0) +console.log("traffic-flow-fact-accumulator.test.ts: instance/input/output isolation, normalization collisions, stable ASN/tuple ties, per-group caps/fallback conservation and legacy parsing verified") diff --git a/backend/src/services/traffic-flow-fact-accumulator.ts b/backend/src/services/traffic-flow-fact-accumulator.ts new file mode 100644 index 00000000..2c981fcc --- /dev/null +++ b/backend/src/services/traffic-flow-fact-accumulator.ts @@ -0,0 +1,202 @@ +export const FACT_ASN_TOP = 200 +export const FACT_TUPLE_CAP = 8000 +export const FACT_SERVICE_MAX_LEN = 64 +export const UNKNOWN_COUNTRY = "XX" +export const OTHER_SERVICE = "other" +export const UNKNOWN_IFACE = "__unknown__" + +export interface FactAcc { + bytes: number + packets: number +} + +export interface FactRow { + serverId: number + bucketAt: string + iface: string + country: string + service: string + asn: number + bytes: number + packets: number +} + +export function hourBucketIso(at = Date.now()): string { + const d = new Date(at) + d.setMinutes(0, 0, 0) + return d.toISOString() +} + +export function normalizeFactCountry(raw: string): string { + const iso = raw.trim().toUpperCase() + if (/^[A-Z]{2}$/.test(iso)) return iso + return UNKNOWN_COUNTRY +} + +export function normalizeFactService(raw: string): string { + const s = raw.trim().slice(0, FACT_SERVICE_MAX_LEN) + return s || OTHER_SERVICE +} + +export function normalizeFactIface(raw: string): string { + return raw.trim() || UNKNOWN_IFACE +} + +export function normalizeFactAsn(raw: number): number { + if (!Number.isFinite(raw) || raw <= 0) return 0 + return Math.trunc(raw) +} + +function factKey( + serverId: number, + bucketAt: string, + iface: string, + country: string, + service: string, + asn: number, +): string { + return `${serverId}\0${bucketAt}\0${iface}\0${country}\0${service}\0${asn}` +} + +function parseFactKey(k: string, acc: FactAcc): FactRow | null { + const parts = k.split("\0") + if (parts.length !== 6) return null + const serverId = Number(parts[0]) + const asn = Number(parts[5]) + if (!Number.isFinite(serverId) || !Number.isFinite(asn)) return null + return { + serverId, + bucketAt: parts[1] ?? "", + iface: parts[2] ?? UNKNOWN_IFACE, + country: parts[3] ?? UNKNOWN_COUNTRY, + service: parts[4] ?? OTHER_SERVICE, + asn, + bytes: acc.bytes, + packets: acc.packets, + } +} + +export interface FlowFactAccumulator { + has(row:FactRow):boolean + bump(row: FactRow): void + collectCapped(): FactRow[] + snapshot(): FactRow[] + clear(): void + readonly size: number +} + +/** Private state per owner. Collection drains only this instance; no SQL or runtime imports. */ +export function createFlowFactAccumulator(): FlowFactAccumulator { + const hourFacts = new Map() + + function bumpFlowFact(row: { + serverId: number + bucketAt: string + iface: string + country: string + service: string + asn: number + bytes: number + packets: number + }): void { + const iface = normalizeFactIface(row.iface) + const country = normalizeFactCountry(row.country) + const service = normalizeFactService(row.service) + const asn = normalizeFactAsn(row.asn) + const k = factKey(row.serverId, row.bucketAt, iface, country, service, asn) + const prev = hourFacts.get(k) + if (prev) { + prev.bytes += row.bytes + prev.packets += row.packets + return + } + hourFacts.set(k, { bytes: row.bytes, packets: row.packets }) + } + + function groupKey(row: FactRow): string { + return `${row.serverId}\0${row.bucketAt}` + } + + function mergeRow(map: Map, row: FactRow): void { + const k = factKey(row.serverId, row.bucketAt, row.iface, row.country, row.service, row.asn) + const prev = map.get(k) + if (prev) { + prev.bytes += row.bytes + prev.packets += row.packets + return + } + map.set(k, { ...row }) + } + + /** Cap ASN tail and tuple count per server×hour before persist. */ + function collectCappedFacts(): FactRow[] { + const parsed: FactRow[] = [] + for (const [k, acc] of hourFacts) { + const row = parseFactKey(k, acc) + if (row) parsed.push(row) + } + hourFacts.clear() + + const groups = new Map() + for (const row of parsed) { + const g = groupKey(row) + const list = groups.get(g) ?? [] + list.push(row) + groups.set(g, list) + } + + const out = new Map() + for (const list of groups.values()) { + const byAsn = new Map() + for (const row of list) { + byAsn.set(row.asn, (byAsn.get(row.asn) ?? 0) + row.bytes) + } + const asnKeep = new Set( + [...byAsn.entries()] + .sort((a, b) => b[1] - a[1]) + .slice(0, FACT_ASN_TOP) + .map(([asn]) => asn), + ) + const afterAsn: FactRow[] = [] + for (const row of list) { + if (asnKeep.has(row.asn) || row.asn === 0) { + afterAsn.push(row) + continue + } + afterAsn.push({ ...row, asn: 0 }) + } + const collapsed = new Map() + for (const row of afterAsn) mergeRow(collapsed, row) + const tuples = [...collapsed.values()].sort((a, b) => b.bytes - a.bytes) + const keep = tuples.slice(0, FACT_TUPLE_CAP) + const tail = tuples.slice(FACT_TUPLE_CAP) + for (const row of keep) mergeRow(out, row) + for (const row of tail) { + mergeRow(out, { + ...row, + country: UNKNOWN_COUNTRY, + service: OTHER_SERVICE, + asn: 0, + }) + } + } + return [...out.values()] + } + + + return { + has:row=>hourFacts.has(factKey(row.serverId,row.bucketAt,normalizeFactIface(row.iface),normalizeFactCountry(row.country),normalizeFactService(row.service),normalizeFactAsn(row.asn))), + bump: bumpFlowFact, + collectCapped: collectCappedFacts, + snapshot: () => { + const parsed: FactRow[] = [] + for (const [key, acc] of hourFacts) { + const row = parseFactKey(key, acc) + if (row) parsed.push(row) + } + return parsed + }, + clear: () => { hourFacts.clear() }, + get size() { return hourFacts.size }, + } +} diff --git a/backend/src/services/traffic-flow-facts-filter.ts b/backend/src/services/traffic-flow-facts-filter.ts index 174e96d8..02c08e78 100644 --- a/backend/src/services/traffic-flow-facts-filter.ts +++ b/backend/src/services/traffic-flow-facts-filter.ts @@ -89,11 +89,13 @@ export function shouldWriteFlowFact(opts: { natDst?: string natSrcPort?: number natDstPort?: number + resolveIface?:typeof canonicalFactIface }): boolean { - const inName = canonicalFactIface(opts.serverId, opts.inIface) || String(opts.inIface ?? "").trim() + const resolveIface=opts.resolveIface??canonicalFactIface + const inName = resolveIface(opts.serverId, opts.inIface) || String(opts.inIface ?? "").trim() if (isJunkFactIface(inName) || isJunkFactIface(opts.inIface)) return false const outRaw = String(opts.outIface ?? "").trim() - const outName = outRaw ? (canonicalFactIface(opts.serverId, outRaw) || outRaw) : "" + const outName = outRaw ? (resolveIface(opts.serverId, outRaw) || outRaw) : "" const plane = classifyFlowPlane({ src: opts.src, dst: opts.dst, @@ -117,7 +119,7 @@ export function shouldWriteFlowFact(opts: { natDst: opts.natDst, natSrcPort: opts.natSrcPort, natDstPort: opts.natDstPort, - }), + },resolveIface), ) if (!dest) return false const ours = flowOursHosts(opts.topo) diff --git a/backend/src/services/traffic-flow-facts-rebuild-classifier.test.ts b/backend/src/services/traffic-flow-facts-rebuild-classifier.test.ts new file mode 100644 index 00000000..2dc5f316 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-classifier.test.ts @@ -0,0 +1,39 @@ +import assert from "node:assert/strict" +import { pool } from "../db/index.js" +import { captureFlowFactRebuildClassifier } from "./traffic-flow-facts-rebuild-classifier.js" +import { rememberServerIfaces,resetIfaceCacheForTests } from "./traffic-flow-ifindex.js" +import { seedFlowCatalogForTests,resetFlowCatalogForTests } from "./traffic-flow-classify.js" +import { seedRipeCacheForTests,resetRipeCacheForTests } from "./traffic-flow-ripe.js" +import { resetGeoipForTests } from "./traffic-flow-geoip.js" +import { resolveInternetDest } from "./traffic-flow-dest.js" +import { shouldWriteFlowFact } from "./traffic-flow-facts-filter.js" +import type { FlowTopology } from "./traffic-flow-topology.js" +import type { RebuildBucketRow } from "./traffic-flow-facts-rebuild-execution.js" + +const originalQuery=pool.query,originalFetch=globalThis.fetch +let sql=0,http=0 +pool.query=(()=>{sql++;throw Error("Classifier must not run SQL")}) as typeof pool.query +globalThis.fetch=(async()=>{http++;throw Error("Classifier must not run network")}) as typeof fetch +resetRipeCacheForTests();resetGeoipForTests();resetIfaceCacheForTests();resetFlowCatalogForTests() +const topo:FlowTopology={clientIfaces:new Map([[1,new Set(["gre-client"])]]),clientByIface:new Map([["1|gre-client",{userId:"u",login:"u",name:"u",serverId:1,interfaceName:"gre-client"}]]),enNodes:[],enHosts:new Set(),jhHosts:new Set(["203.0.113.10"]),wanIfaces:new Map([[1,new Set(["ether1"])]]),plane:{clientIfaceNames:new Set(["gre-client"]),enHosts:new Set(),jhHosts:new Set(["203.0.113.10"])}} +const row:RebuildBucketRow={serverId:1,bucketAt:"2026-10-24T00:00:00Z",src:"95.167.1.10",dst:"8.8.8.8",proto:6,srcPort:1234,dstPort:443,bytes:50,packets:2,inIface:"10",outIface:"ether1",natSrc:"",natDst:"",natSrcPort:0,natDstPort:0} +try{ + rememberServerIfaces(1,[{name:"gre-client",ifindex:"10"}]) + const cold=captureFlowFactRebuildClassifier(topo,new Map([[1,"jump-host"]]),[1]) + assert.equal(cold.ripeLoaded,false);assert.equal(cold.classify(row)?.asn,0) + assert.equal(sql,0);assert.equal(http,0) + seedRipeCacheForTests({prefix:"8.8.8.0/24",asn:15169,country:"US",lat:null,lng:null,holder:"GOOGLE",ok:true,fetchedAt:Date.now()}) + seedFlowCatalogForTests({cidrs:[{cidr:"8.8.8.0/24",purpose:"fixture-service"}]}) + const types=new Map([[1,"jump-host"]]),ids=[1],captured=captureFlowFactRebuildClassifier(topo,types,ids) + const old=resolveInternetDest({...row,topo}) + assert.equal(shouldWriteFlowFact({...row,serverType:"jump-host",topo,dest:old.dest}),true) + assert.deepEqual(captured.classify(row),{serverId:1,iface:"gre-client",country:old.country||"XX",service:old.classified.service,asn:old.asn,bytes:50,packets:2}) + const stable=captured.classify(row) + rememberServerIfaces(1,[{name:"wg-flow",ifindex:"10"}]);resetRipeCacheForTests();seedFlowCatalogForTests({cidrs:[{cidr:"8.8.8.0/24",purpose:"changed"}]}) + topo.clientIfaces.clear();topo.clientByIface.clear();types.clear();ids.length=0 + assert.deepEqual(captured.classify(row),stable) + assert.throws(()=>captured.classify({...row,serverId:999}),/Foreign/) + await new Promise(resolve=>setImmediate(resolve)) + assert.equal(sql,0);assert.equal(http,0) + console.log("traffic-flow-facts-rebuild-classifier.test.ts: cold cache has no SQL/network, seeded REST classifier parity, cache/topology/ifIndex/source-input snapshot isolation and foreign scope verified") +}finally{pool.query=originalQuery;globalThis.fetch=originalFetch;resetRipeCacheForTests();resetGeoipForTests();resetIfaceCacheForTests();resetFlowCatalogForTests()} diff --git a/backend/src/services/traffic-flow-facts-rebuild-classifier.ts b/backend/src/services/traffic-flow-facts-rebuild-classifier.ts new file mode 100644 index 00000000..4c0d123d --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-classifier.ts @@ -0,0 +1,26 @@ +import { captureFactIfaceResolver } from "./traffic-flow-ifindex.js" +import { captureRipeCacheLookup } from "./traffic-flow-ripe.js" +import { captureGeoipLookup } from "./traffic-flow-geoip.js" +import { captureFlowClassificationCatalog, classifyFlowDst } from "./traffic-flow-classify.js" +import { resolveInternetDest } from "./traffic-flow-dest.js" +import { shouldWriteFlowFact } from "./traffic-flow-facts-filter.js" +import type { FlowTopology } from "./traffic-flow-topology.js" +import type { RebuildSnapshot } from "./traffic-flow-facts-rebuild-execution.js" + +/** Captures observed caches without refresh/load/enqueue. No classifier call + * reads SQL/network or live process-global accumulators after this function. */ +export function captureFlowFactRebuildClassifier(topology:FlowTopology,serverTypes:ReadonlyMap,serverIds:readonly number[]) { + const topo=structuredClone(topology),types=new Map(serverTypes) + const allowed=new Set(serverIds) + const ifaces=captureFactIfaceResolver(serverIds),ripe=captureRipeCacheLookup(),geo=captureGeoipLookup(),catalog=captureFlowClassificationCatalog() + const proofInput={topology:topo,serverTypes:[...types],ifaces:ifaces.entries,ripe:{loaded:ripe.loaded,entries:ripe.entries},geo:geo.status,catalog:{cidrs:catalog.cidrs,asnPurpose:[...catalog.asnPurpose]}} + const serialized=JSON.stringify(proofInput,(_key,value:unknown)=>value instanceof Map?[...value]:value instanceof Set?[...value]:value) + if(Buffer.byteLength(serialized)>4194304)throw Error("Fact classifier snapshot exceeds 4 MiB") + const classify:RebuildSnapshot["classify"]=row=>{ + if(!allowed.has(row.serverId))throw Error("Foreign server in fact classifier") + const dest=resolveInternetDest({...row,topo,resolveIface:ifaces.resolve,resolveIp:ip=>geo.lookup(ip)??ripe.lookup(ip),classify:(dst,proto,dstPort,srcPort,meta)=>classifyFlowDst(dst,proto,dstPort,srcPort,meta,{ignoreTunnelProto:true,catalog})}) + if(!shouldWriteFlowFact({...row,topo,serverType:types.get(row.serverId),dest:dest.dest,resolveIface:ifaces.resolve}))return null + return {serverId:row.serverId,iface:ifaces.resolve(row.serverId,row.inIface),country:dest.country||"XX",service:dest.classified.service,asn:dest.asn,bytes:row.bytes,packets:row.packets} + } + return {classify,serializedProofInput:serialized,ripeLoaded:ripe.loaded} +} diff --git a/backend/src/services/traffic-flow-facts-rebuild-cli.sql-helper.ts b/backend/src/services/traffic-flow-facts-rebuild-cli.sql-helper.ts new file mode 100644 index 00000000..e4614e69 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-cli.sql-helper.ts @@ -0,0 +1,68 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { basename, dirname, resolve } from "node:path" + +/** Real legacy entrypoint/service acceptance, called only by an owned private + * PostgreSQL profile. Requires EMPTY source/cubes; never clears foreign rows to + * satisfy this precondition. Uses actual existing-writer drain, never a stub. */ +export async function acceptLegacyFlowFactsRebuildSql() { + assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") + const url = new URL(process.env.DATABASE_URL ?? "") + assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") + assert(["postgres:", "postgresql:"].includes(url.protocol)) + assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + const { pool } = await import("../db/index.js") + const { ensurePartitionFor } = await import("../db/partitions.js") + const { rebuildFlowFactsFromBuckets } = await import("./traffic-flow-facts-rebuild.js") + const { withPartitionWrite } = await import("./partition-write-transaction.js") + assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") + for (const table of ["flow_buckets", "flow_hour_facts", "flow_daily_facts"]) assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table}`)).rows[0].n, 0, "Dedicated empty source/cube profile required; foreign fixture rows are never deleted") + const protectionId = randomUUID(), userId = `legacy-rebuild-${randomUUID()}`, bindingId = randomUUID() + const day = "2102-02-04", unrelatedDay = "2102-02-03", target = `${day}T10:00:00Z` + const serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled,type,wan_uplinks) VALUES('legacy-rebuild-owned','127.0.0.1',9,false,'jump-host',$1::jsonb) RETURNING id", [JSON.stringify([{ iface: "ether1", ip: "127.0.0.1" }])])).rows[0].id) + let protectionInserted = false + const savedFetch = globalThis.fetch + let networkAttempts = 0 + globalThis.fetch = (async () => { networkAttempts++; throw Error("Owned legacy rebuild acceptance forbids network I/O") }) as typeof fetch + try { + for (const [parent, kind, at] of [["flow_buckets", "day", target], ["flow_hour_facts", "day", target], ["flow_daily_facts", "month", target], ["flow_hour_facts", "day", `${unrelatedDay}T10:00:00Z`], ["server_snapshots", "week", target]] as const) await ensurePartitionFor(pool, parent, kind, new Date(at)) + await withPartitionWrite(async ({ client }) => { + await client.query("INSERT INTO app_users(id,login,name) VALUES($1,$1,'Owned legacy rebuild fixture')", [userId]) + await client.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type) VALUES($1,$2,$3,'legacy-client','gre')", [bindingId, userId, serverId]) + await client.query("INSERT INTO server_snapshots(server_id,polled_at,status,raw_interfaces) VALUES($1,$2,'online',$3::jsonb)", [serverId, target, JSON.stringify([{ name: "legacy-client", type: "gre" }, { name: "ether1", type: "ether" }])]) + for (const [minute, bytes, packets] of [["10:01", 10, 1], ["10:02", 5, 2]] as const) await client.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets,in_iface,out_iface) VALUES($1,$2,'95.167.1.10','8.8.8.8',6,1234,443,$3,$4,'legacy-client','ether1')", [serverId, `${day}T${minute}:00Z`, bytes, packets]) + for (const [at, service, bytes] of [[target, "legacy-target-sentinel", 999], [`${unrelatedDay}T10:00:00Z`, "legacy-unrelated-sentinel", 55]] as const) { + await client.query("INSERT INTO flow_hour_facts(server_id,bucket_at,iface,country,service,asn,bytes,packets) VALUES($1,$2,'legacy-client','XX',$3,0,$4,9)", [serverId, at, service, bytes]) + await client.query("INSERT INTO flow_daily_facts(server_id,day,iface,country,service,asn,bytes,packets) VALUES($1,$2::timestamptz::date,'legacy-client','XX',$3,0,$4,9)", [serverId, at, service, bytes]) + } + }) + const beforeReceipts = (await pool.query("SELECT count(*)::int AS n FROM flow_write_receipts WHERE kind='rebuild'")).rows[0].n + const result = await rebuildFlowFactsFromBuckets() + assert.deepEqual(result, { ok: true, buckets: 2, facts: 1, days: [day] }) + assert.equal(Object.keys(result).length, 4, "Public legacy result contract remains unchanged") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM flow_write_receipts WHERE kind='rebuild'")).rows[0].n, beforeReceipts + 1) + const receipt = (await pool.query("SELECT batch_id,result FROM flow_write_receipts WHERE kind='rebuild' ORDER BY committed_at DESC LIMIT 1")).rows[0] + assert(receipt && receipt.result.jobId === receipt.batch_id) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=$1 AND event_type='traffic.flow.rebuild_facts'", [receipt.batch_id])).rows[0].n, 1) + for (const table of ["flow_hour_facts", "flow_daily_facts"]) { + const total = (await pool.query(`SELECT sum(bytes)::int AS bytes,sum(packets)::int AS packets FROM ${table} WHERE server_id=$1 AND service<>'legacy-unrelated-sentinel'`, [serverId])).rows[0] + assert.deepEqual(total, { bytes: 15, packets: 3 }) + assert.equal((await pool.query(`SELECT sum(bytes)::int AS n FROM ${table} WHERE server_id=$1 AND service='legacy-unrelated-sentinel'`, [serverId])).rows[0].n, 55) + } + // A future leaf protection is part of the actual service's full closure; + // it is rejected without any fake local approval/MCP principal. + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'legacy-rebuild-owned-acceptance')", [protectionId, JSON.stringify({ kind: "database-partition", id: "flow_hour_facts/flow_hour_facts_d_21990101", protection: "deny" })]) + protectionInserted = true + const beforeDenied = (await pool.query("SELECT count(*)::int AS n FROM flow_write_receipts WHERE kind='rebuild'")).rows[0].n + await assert.rejects(rebuildFlowFactsFromBuckets(), /защищённый объект|защит|protec/i) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM flow_write_receipts WHERE kind='rebuild'")).rows[0].n, beforeDenied) + assert.equal(networkAttempts, 0) + return { cases: 3, actualLegacyEntrypoint: true, actualSafeService: true, actualExistingWriterDrain: true, noWorkerStartedByHelper: true, futureLeafProtectionRefused: true, unknownCommitPassthrough: "inert wrapper only", nativeNetworkIo: false } + } finally { + globalThis.fetch = savedFetch + // Exact-ID fixture cleanup is explicitly authorized by the parent task. + // Append-only audit, history, events and durable receipts remain untouched. + if (protectionInserted) await pool.query("DELETE FROM mcp_protections WHERE id=$1 AND created_by='legacy-rebuild-owned-acceptance'", [protectionId]) + await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [serverId]) + } +} diff --git a/backend/src/services/traffic-flow-facts-rebuild-cli.test.ts b/backend/src/services/traffic-flow-facts-rebuild-cli.test.ts new file mode 100644 index 00000000..5ce018d2 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-cli.test.ts @@ -0,0 +1,42 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import ts from "typescript" + +const source = await readFile(new URL("./traffic-flow-facts-rebuild.ts", import.meta.url), "utf8") +const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText +type Authority = { serverIds: number[]; authorize: (client?: object) => Promise; drainExisting: unknown } +async function fixture() { + let rows: Array<{ id: number; revision: string }> = [{ id: 2, revision: "11" }, { id: 8, revision: "12" }], fail: unknown, invoke = 0, poolReads = 0, clientReads = 0, authority!: Authority + const inspect = (text: string) => { assert.equal(text, "SELECT id,xmin::text AS revision FROM servers ORDER BY id LIMIT 1001"); return { rows: structuredClone(rows) } } + const pool = { query: async (text: string) => { poolReads++; return inspect(text) } }, owned = { query: async (text: string) => { clientReads++; return inspect(text) } } + const drain = async () => {} + const modules: Record = { + "../db/index.js": { pool }, + "./traffic-flow-ingest.js": { drainExistingTrafficFlowWriters: drain }, + "./traffic-flow-facts-rebuild-service.js": { rebuildFlowFactsSafely: async (input: Authority) => { authority = input; invoke++; await input.authorize(); await input.authorize(owned); if (fail) throw fail; return { ok: true, buckets: 4, facts: 2, days: ["2026-10-25"], jobId: "private-job", configurationProof: "private-config", classifierProof: "private-classifier", resultProof: "private-result" } } }, + } + const exports = {} as { FACT_REBUILD_LOCK_KEY: number; rebuildFlowFactsFromBuckets: () => Promise } + runInNewContext(compiled, { exports, require: (name: string) => { assert(name in modules, "Unapproved legacy dependency: " + name); return modules[name] }, Error, Number, JSON }, { filename: "traffic-flow-facts-rebuild.ts" }) + return { run: exports.rebuildFlowFactsFromBuckets, key: exports.FACT_REBUILD_LOCK_KEY, drain, owned, authority: () => authority, setRows: (value: typeof rows) => { rows = value }, fail: (value: unknown) => { fail = value }, counts: () => ({ invoke, poolReads, clientReads }) } +} +const f = await fixture() +assert.equal(f.key, 8723104) +assert.deepEqual(JSON.parse(JSON.stringify(await f.run())), { ok: true, buckets: 4, facts: 2, days: ["2026-10-25"] }) +assert.deepEqual(Array.from(f.authority().serverIds), [2, 8]); assert.equal(f.authority().drainExisting, f.drain) +assert.deepEqual(f.counts(), { invoke: 1, poolReads: 2, clientReads: 1 }) +const before = f.counts(); await f.authority().authorize(f.owned) +assert.equal(f.counts().poolReads, before.poolReads); assert.equal(f.counts().clientReads, before.clientReads + 1) +for (const changed of [[{ id: 2, revision: "99" }, { id: 8, revision: "12" }], [{ id: 2, revision: "11" }], [{ id: 2, revision: "11" }, { id: 8, revision: "12" }, { id: 9, revision: "1" }]]) { + f.setRows(changed); await assert.rejects(f.authority().authorize(f.owned), /catalog changed/) +} +for (const invalid of [Array.from({ length: 1001 }, (_, index) => ({ id: index + 1, revision: "1" })), [{ id: 1, revision: "x" }], [{ id: 0, revision: "1" }], [{ id: 2, revision: "1" }, { id: 2, revision: "1" }], [{ id: 8, revision: "1" }, { id: 2, revision: "1" }]]) { + const f = await fixture(); f.setRows(invalid); await assert.rejects(f.run(), /exact bounded/); assert.equal(f.counts().invoke, 0) +} +const unknown = Object.assign(new Error("Unconfirmed publication; reconcile by jobId"), { code: "FLOW_COMMIT_UNKNOWN", jobId: "owned-job", configurationProof: "owned-config" }) +const uncertain = await fixture(); uncertain.fail(unknown) +await assert.rejects(uncertain.run(), error => error === unknown) +assert.equal(uncertain.counts().invoke, 1) +const denied = await fixture(), protectedObject = Error("Protected future partition refused") +denied.fail(protectedObject); await assert.rejects(denied.run(), error => error === protectedObject); assert.equal(denied.counts().invoke, 1) +console.log("Legacy fact rebuild CLI: 13 inert exact-catalog/owned-client/service/drain/no-replay assertions passed") diff --git a/backend/src/services/traffic-flow-facts-rebuild-execution.test.ts b/backend/src/services/traffic-flow-facts-rebuild-execution.test.ts new file mode 100644 index 00000000..66c5c681 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-execution.test.ts @@ -0,0 +1,43 @@ +import assert from "node:assert/strict" +import { createFlowFactAccumulator, type FactRow } from "./traffic-flow-fact-accumulator.js" +import { executeFlowFactsRebuild, type RebuildBucketRow, type RebuildExecutionDependencies, type RebuildResult } from "./traffic-flow-facts-rebuild-execution.js" +const row=(patch:Partial={}):RebuildBucketRow=>({serverId:1,bucketAt:"2026-10-24T10:02:00.000Z",src:"192.0.2.1",dst:"198.51.100.1",proto:6,srcPort:1234,dstPort:443,bytes:10,packets:1,inIface:"ether1",outIface:"",natSrc:"",natDst:"",natSrcPort:0,natDstPort:0,...patch}) +let source=[row(),row({bytes:5,packets:2})],daysSeen:string[]=[],published:FactRow[]=[],events:RebuildResult[]=[],replaceCalls=0,drains=0,active=false,mode="normal",validations=0 +const old=row({serverId:2,bytes:123}),live=createFlowFactAccumulator();live.bump({serverId:9,bucketAt:"live",iface:"live",country:"DE",service:"live",asn:9,bytes:99,packets:3}) +const hourBefore=[old],dayBefore=[old];let hours:unknown[]=structuredClone(hourBefore),daily:unknown[]=structuredClone(dayBefore) +const dependencies:RebuildExecutionDependencies={ + authorize:async()=>{if(mode==="revoked")throw Error("revoked")}, + admit:async work=>{assert.equal(active,false);active=true;try{return await work()}finally{active=false}}, + drain:async()=>{assert.equal(active,true);drains++;if(mode==="drain-failure")throw Error("unconfirmed pending batch")}, + atomic:async work=>{ + const saved={hours:structuredClone(hours),daily:structuredClone(daily),events:structuredClone(events)} + try{return await work()}catch(error){hours=saved.hours;daily=saved.daily;events=saved.events;throw error} + }, + snapshot:async()=>({serverIds:[1,2],rows:(async function*(){for(const entry of source)yield [entry]})(), + classify:bucket=>({serverId:bucket.serverId,iface:bucket.inIface,country:"US",service:"https",asn:15169,bytes:bucket.bytes,packets:bucket.packets}), + validate:async()=>{validations++;if(mode==="source-drift"&&validations===2)throw Error("source drift")}, + }), + replace:async(days,facts)=>{replaceCalls++;daysSeen=[...days];published=structuredClone([...facts]);hours=structuredClone([...facts]);if(mode==="hour-write-failure")throw Error("hour write failed");daily=structuredClone([...facts]);if(mode==="revoke-after-write")mode="revoked"}, + event:async result=>{if(mode==="event-failure")throw Error("event failed");events.push(structuredClone(result))}, +} +const result=await executeFlowFactsRebuild(dependencies) +assert.deepEqual(result,{ok:true,buckets:2,facts:1,days:["2026-10-24"]}) +assert.deepEqual(daysSeen,["2026-10-24"]);assert.equal(published[0].bytes,15);assert.equal(published[0].packets,3);assert.equal(published[0].bucketAt,"2026-10-24T10:00:00.000Z") +assert.deepEqual(hours,daily);assert.equal(events.length,1);assert.equal(drains,1) +assert.deepEqual(live.snapshot(),[{serverId:9,bucketAt:"live",iface:"live",country:"DE",service:"live",asn:9,bytes:99,packets:3}]) +source=[];const beforeEmpty=structuredClone(hours),beforeReplace=replaceCalls +assert.deepEqual(await executeFlowFactsRebuild(dependencies),{ok:true,buckets:0,facts:0,days:[]}) +assert.deepEqual(hours,beforeEmpty);assert.equal(replaceCalls,beforeReplace);assert.equal(events.length,2) +for(const fault of ["drain-failure","source-drift","hour-write-failure","event-failure","revoke-after-write"]){ + source=[row()];hours=structuredClone(hourBefore);daily=structuredClone(dayBefore);events=[];mode=fault;validations=0 + await assert.rejects(executeFlowFactsRebuild(dependencies)) + assert.deepEqual(hours,hourBefore);assert.deepEqual(daily,dayBefore);assert.deepEqual(events,[]) +} +mode="normal" +for(const invalid of [row({serverId:999}),row({bucketAt:"bad"}),row({bytes:Number.MAX_SAFE_INTEGER+1}),row({packets:-1})]){ + source=[invalid];const before=replaceCalls;await assert.rejects(executeFlowFactsRebuild(dependencies));assert.equal(replaceCalls,before) +} +source=[row(),row()];await assert.rejects(executeFlowFactsRebuild(dependencies,{maxRows:1,maxDays:1}),/row limit/) +source=[row(),row({bucketAt:"2026-10-25T00:00:00Z"})];await assert.rejects(executeFlowFactsRebuild(dependencies,{maxRows:5,maxDays:1}),/day limit/) +source=[row({bytes:Number.MAX_SAFE_INTEGER}),row({bytes:1})];await assert.rejects(executeFlowFactsRebuild(dependencies),/overflow/) +console.log("traffic-flow-facts-rebuild-execution.test.ts: private accumulation, shared drain/atomic ordering, empty preservation, two-cube/event rollback, revoke/source/row/day/overflow fences; no SQL/network/controllers executed") diff --git a/backend/src/services/traffic-flow-facts-rebuild-execution.ts b/backend/src/services/traffic-flow-facts-rebuild-execution.ts new file mode 100644 index 00000000..f0fcf2c3 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-execution.ts @@ -0,0 +1,66 @@ +import { createFlowFactAccumulator, hourBucketIso, type FactRow } from "./traffic-flow-fact-accumulator.js" + +export interface RebuildBucketRow { + serverId:number;bucketAt:Date|string;src:string;dst:string;proto:number;srcPort:number;dstPort:number; + bytes:number;packets:number;inIface:string;outIface:string;natSrc:string;natDst:string;natSrcPort:number;natDstPort:number +} +export interface RebuildResult { ok:true;buckets:number;facts:number;days:string[] } +export interface RebuildSnapshot { + serverIds:readonly number[] + rows:AsyncIterable + /** Capture/freeze dependencies before returning this synchronous classifier. + * It must perform neither SQL nor network nor mutate global accumulators. */ + classify:(row:RebuildBucketRow)=>Omit|null + validate:()=>Promise +} +export interface RebuildExecutionDependencies { + authorize:()=>Promise + admit:(work:()=>Promise)=>Promise + drain:()=>Promise + /** One fenced transaction owns snapshot, both cubes, event and receipt. */ + atomic:(work:()=>Promise)=>Promise + snapshot:()=>Promise + replace:(days:readonly string[],facts:readonly FactRow[])=>Promise + event:(result:RebuildResult)=>Promise +} + +/** Complete job algorithm, independent of SQL drivers and controllers. Publication + * dependencies must remain in the same transaction; no cleanup starts a worker. */ +export async function executeFlowFactsRebuild(deps:RebuildExecutionDependencies,limits:{maxRows:number;maxDays:number;maxSourceBytes?:number}={maxRows:200000,maxDays:1000}):Promise { + if(!Number.isSafeInteger(limits.maxRows)||limits.maxRows<1||limits.maxRows>1000000||!Number.isSafeInteger(limits.maxDays)||limits.maxDays<1||limits.maxDays>1000)throw Error("Invalid fact rebuild bounds") + const maxBytes=limits.maxSourceBytes??16777216 + if(!Number.isSafeInteger(maxBytes)||maxBytes<1||maxBytes>67108864)throw Error("Invalid fact rebuild byte bound") + await deps.authorize() + return deps.admit(async()=>{ + await deps.authorize();await deps.drain();await deps.authorize() + return deps.atomic(async()=>{ + const source=await deps.snapshot(),allowed=new Set(source.serverIds),rows:RebuildBucketRow[]=[],days=new Set() + let sourceBytes=0 + await deps.authorize();await source.validate() + for await(const batch of source.rows){ + await deps.authorize() + if(rows.length+batch.length>limits.maxRows)throw Error("Fact rebuild source row limit exceeded; no partial publication") + for(const row of batch){ + const time=row.bucketAt instanceof Date?row.bucketAt.getTime():Date.parse(row.bucketAt) + if(!allowed.has(row.serverId)||!Number.isFinite(time)||typeof row.inIface!=="string"||typeof row.outIface!=="string"||row.inIface.length>512||row.outIface.length>512||!Number.isSafeInteger(row.bytes)||row.bytes<0||!Number.isSafeInteger(row.packets)||row.packets<0)throw Error("Invalid or foreign fact rebuild row") + sourceBytes+=Buffer.byteLength(JSON.stringify(row)) + if(sourceBytes>maxBytes)throw Error("Fact rebuild source byte limit exceeded") + rows.push({...row,bucketAt:new Date(time).toISOString()});days.add(new Date(time).toISOString().slice(0,10)) + if(days.size>limits.maxDays)throw Error("Fact rebuild day limit exceeded") + } + } + await deps.authorize();await source.validate() + const accumulator=createFlowFactAccumulator() + // No await while classifying the bounded snapshot. No process-global facts map. + for(const row of rows){const fact=source.classify(row);if(fact){if(fact.serverId!==row.serverId||fact.bytes!==row.bytes||fact.packets!==row.packets)throw Error("Classifier changed fact ownership or counters");accumulator.bump({...fact,bucketAt:hourBucketIso(Date.parse(String(row.bucketAt)))})}} + const facts=accumulator.collectCapped() + if(facts.some(row=>!Number.isSafeInteger(row.bytes)||!Number.isSafeInteger(row.packets)))throw Error("Fact aggregation overflow") + const result:RebuildResult={ok:true,buckets:rows.length,facts:facts.length,days:[...days].sort()} + await deps.authorize();await source.validate() + // Empty source deliberately preserves old cubes, matching intended REST behavior. + if(result.days.length)await deps.replace(result.days,facts) + await deps.authorize();await deps.event(result);await deps.authorize() + return result + }) + }) +} diff --git a/backend/src/services/traffic-flow-facts-rebuild-service.test.ts b/backend/src/services/traffic-flow-facts-rebuild-service.test.ts new file mode 100644 index 00000000..ade99468 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-service.test.ts @@ -0,0 +1,105 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { runInNewContext } from "node:vm" +import * as crypto from "node:crypto" +import ts from "typescript" +import * as execution from "./traffic-flow-facts-rebuild-execution.js" + +const source=await readFile(new URL("./traffic-flow-facts-rebuild-service.ts",import.meta.url),"utf8") +let current:unknown,mode="normal",drains=0,atomic=0,policyMasked=false +let footprint:Array<{kind:string;id:string}>=[] +let partitionProtections:Array<{id:string;protection:string}>=[] +let relatedProtections:Array<{kind:string;id:string;protection:string;serverId?:string;revision:string}>=[] +let pendingWrites=0,committedWrites=0,rollbacks=0 +const receipts=new Map}>() +let admissionHeld=false,receiptGrant=true +class Unknown extends Error {} +const client={query:async(sql:string)=>{ + if(sql.includes("FROM servers"))return {rows:[{id:1,revision:"1"}]} + if(sql.includes("FROM user_interface_bindings")||sql.includes("FROM app_users")||sql.includes("FROM server_snapshots"))return {rows:[]} + if(sql.includes("FROM traffic_flow_settings"))return {rows:[{top_n:20,retention_hours:24}]} + if(sql.includes("FROM mcp_protections"))return {rows:[...partitionProtections.map(row=>({kind:"database-partition",...row,revision:"1"})),...relatedProtections]} + if(sql.startsWith("INSERT INTO events")){pendingWrites++;if(mode==="late-protection-body")relatedProtections=[{kind:"server",id:"1",protection:"deny",revision:"2"}]} + return {rows:[]} +}} +const modules:Record={ + "node:crypto":crypto,"../db/index.js":{pool:client}, + "../db/operation-lock.js":{withOperationLock:async(_connect:unknown,key:unknown,work:()=>Promise)=>{assert.equal(key,8723104);assert.equal(admissionHeld,false);admissionHeld=true;try{return await work()}finally{admissionHeld=false}}}, + "./config-mutation-lock.js":{withApplicationMutationLock:async(work:()=>Promise)=>work()}, + "./config-mutation-policy.js":{executeGuardedApplicationWrite:async(_object:unknown,_ids:unknown,work:()=>Promise,affected:()=>Promise)=>{ + footprint=await affected() + if(partitionProtections.some(protection=>protection.protection==="deny"&&footprint.some(object=>object.kind==="database-partition"&&object.id===protection.id)))throw Error("Denied database partition protection") + if(mode==="late-partition")partitionProtections=[{id:"flow_hour_facts/flow_hour_facts_d_20990101",protection:"deny"}] + try{const result=await work();if(mode==="audit-after-commit")throw Error("policy ACK audit failed");return result}catch(error){if(mode==="lost-commit-audit-mask"){policyMasked=true;throw Error("policy unconfirmed audit failed")}throw error} + }}, + "../modules/mcp/security.js":{privateSecretFingerprint:async(value:string,domain:string)=>crypto.createHash("sha256").update(domain+value).digest("hex")}, + "./traffic-flow-write-transaction.js":{ + FlowCommitUnknown:Unknown,flowWriteContext:()=>current, + commitFlowWrite:async(input:{id:string;sourceProof:string},work:()=>Promise>,authorize:()=>Promise)=>{ + atomic++;current={client,mode:"exclusive"} + try{await authorize();if(mode==="known-failure")throw Error("known BEGIN failure");const result=await work();await authorize();committedWrites+=pendingWrites;pendingWrites=0;receipts.set(input.id,{sourceProof:input.sourceProof,result});if(mode==="lost-commit-audit-mask")throw new Unknown("lost COMMIT");return result}catch(error){pendingWrites=0;if(!(error instanceof Unknown))rollbacks++;throw error}finally{current=undefined} + },reconcileFlowWrite:async(input:{id:string;sourceProof:string},authorize:()=>Promise)=>{assert.equal(admissionHeld,true,"early receipt observation is inside global admission");await authorize();const known=receipts.get(input.id);if(known&&known.sourceProof!==input.sourceProof)throw Error("receipt source mismatch");if(mode==="receipt-revoked")receiptGrant=false;await authorize();return {committed:Boolean(known),result:known?.result??null}}, + }, + "./traffic-flow-facts.js":{persistFlowFactRows:async()=>0}, + "./traffic-flow-facts-rebuild-execution.js":execution, + "./traffic-flow-facts-rebuild-source.js":{readFlowRebuildSnapshot:async(_client:unknown,_ids:unknown,dependencies:{captureClassifier:()=>Promise})=>{await dependencies.captureClassifier();return {serverIds:[1],rows:(async function*(){})(),classify:()=>null,validate:async()=>{}}}}, + "./traffic-flow-facts-rebuild-classifier.js":{captureFlowFactRebuildClassifier:()=>({serializedProofInput:"fixture",classify:()=>null})}, + "./traffic-flow-facts-rebuild-topology.js":{readFactRebuildTopology:async()=>({topology:{},serverTypes:new Map()})}, +} +const exports:Record={} +runInNewContext(ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,{exports,Buffer,Error,structuredClone,require:(name:string)=>{if(!(name in modules))throw Error("Unexpected service dependency: "+name);return modules[name]}}) +type Authority={serverIds:number[];authorize:()=>Promise;drainExisting:(authorize:()=>Promise,ids:readonly number[])=>Promise} +const rebuild=exports.rebuildFlowFactsSafely as (authority:Authority,request?:{jobId?:string;expectedConfigurationProof?:string})=>Promise<{jobId:string;buckets:number;configurationProof:string}> +let drainDispatches=0 +const authority:Authority={serverIds:[1],authorize:async()=>{if(!receiptGrant)throw Error("receipt authority revoked")},drainExisting:async(authorize,ids)=>{assert.equal(admissionHeld,true);await authorize();assert.deepEqual([...ids],[1]);drains++;if(mode==="late-protection-drain")relatedProtections=[{kind:"netflow-settings",id:"1",protection:"deny",revision:"2"}];await authorize();drainDispatches++}} +const jobId="11111111-1111-4111-8111-111111111111" +const result=await rebuild(authority,{jobId}) +assert.equal(result.jobId,jobId);assert.equal(result.buckets,0);assert.equal(drains,1) +for(const kind of ["netflow-purge","netflow-store","netflow-rollups","netflow-partitions","netflow-facts","events-journal","netflow-settings"])assert.ok(footprint.some(object=>object.kind===kind)) +const before={drains,atomic} +assert.deepEqual(await rebuild(authority,{jobId}),result) +assert.equal(drains,before.drains,"known receipt short-circuits before any existing-writer drain") +assert.equal(atomic,before.atomic,"known receipt avoids publication body entirely") +const known=receipts.get(jobId)! +receipts.set(jobId,{...known,result:{...known.result,rawBuckets:[]}}) +await assert.rejects(rebuild(authority,{jobId}),(error:unknown)=>(error as {code:string}).code==="FLOW_COMMIT_UNKNOWN") +assert.equal(drains,before.drains);assert.equal(atomic,before.atomic) +receipts.set(jobId,known) +mode="receipt-revoked" +await assert.rejects(rebuild(authority,{jobId}),/receipt authority revoked/) +assert.equal(drains,before.drains);assert.equal(atomic,before.atomic) +mode="normal";receiptGrant=true +await assert.rejects(rebuild(authority,{jobId:"invalid"}),/job ID/) +await assert.rejects(rebuild(authority,{expectedConfigurationProof:"stale"}),/source changed/) +authority.serverIds=[1,999] +await assert.rejects(rebuild(authority,{jobId}),/exact current server catalog/) +authority.serverIds=[1] +assert.equal(drains,before.drains);assert.equal(atomic,before.atomic) +partitionProtections=[{id:"flow_hour_facts/flow_hour_facts_d_20990101",protection:"deny"}] +await assert.rejects(rebuild(authority,{jobId}),/Denied database partition/) +assert.ok(footprint.some(object=>object.kind==="database-partition"&&object.id==="flow_hour_facts/flow_hour_facts_d_20990101"),"protected future leaf is included without requiring catalog existence") +assert.equal(drains,before.drains);assert.equal(atomic,before.atomic) +partitionProtections=[];mode="late-partition" +await assert.rejects(rebuild(authority),/configuration changed/) +assert.equal(drains,before.drains);assert.equal(atomic,before.atomic) +partitionProtections=[] +mode="late-protection-drain" +const beforeLate={drainDispatches,committedWrites,rollbacks} +await assert.rejects(rebuild(authority),/configuration changed/) +assert.equal(drainDispatches,beforeLate.drainDispatches,"late global deny blocks next drain SQL") +assert.equal(committedWrites,beforeLate.committedWrites) +relatedProtections=[];mode="late-protection-body" +await assert.rejects(rebuild(authority),/configuration changed/) +assert.equal(committedWrites,beforeLate.committedWrites);assert.equal(pendingWrites,0);assert.ok(rollbacks>beforeLate.rollbacks,"late server deny rolls back event before next SQL/COMMIT") +relatedProtections=[] +for(const failure of ["lost-commit-audit-mask","audit-after-commit"]){ + mode=failure + const faultJobId=crypto.randomUUID() + await assert.rejects(rebuild(authority,{jobId:faultJobId}),(error:unknown)=>{ + const value=error as {code:string;jobId:string;configurationProof:string} + assert.equal(value.code,"FLOW_COMMIT_UNKNOWN");assert.equal(value.jobId,faultJobId);assert.equal(value.configurationProof,result.configurationProof);return true + }) +} +assert.equal(policyMasked,true) +mode="known-failure";await assert.rejects(rebuild(authority),/known BEGIN failure/) +console.log("traffic-flow-facts-rebuild-service.test.ts: exact shared service footprint, empty semantics, invalid/stale admission before drain, retained reconciliation metadata after lost COMMIT masked by audit and after ACK-audit failure; no SQL/network executed") diff --git a/backend/src/services/traffic-flow-facts-rebuild-service.ts b/backend/src/services/traffic-flow-facts-rebuild-service.ts new file mode 100644 index 00000000..6dbd7fec --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-service.ts @@ -0,0 +1,155 @@ +import { randomUUID } from "node:crypto" +import type { PoolClient } from "pg" +import { pool } from "../db/index.js" +import { withOperationLock } from "../db/operation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { withApplicationMutationLock } from "./config-mutation-lock.js" +import { privateSecretFingerprint } from "../modules/mcp/security.js" +import { commitFlowWrite,flowWriteContext,reconcileFlowWrite,FlowCommitUnknown } from "./traffic-flow-write-transaction.js" +import { persistFlowFactRows } from "./traffic-flow-facts.js" +import { executeFlowFactsRebuild,type RebuildResult } from "./traffic-flow-facts-rebuild-execution.js" +import { readFlowRebuildSnapshot } from "./traffic-flow-facts-rebuild-source.js" +import { captureFlowFactRebuildClassifier } from "./traffic-flow-facts-rebuild-classifier.js" +import { readFactRebuildTopology } from "./traffic-flow-facts-rebuild-topology.js" + +export interface FactRebuildAuthority { + serverIds:readonly number[] + authorize:(client?:PoolClient)=>Promise + /** Existing main/worker owned-batch drain only. Mandatory; no start/restart fallback. */ + drainExisting:(authorize:()=>Promise,serverIds:readonly number[])=>Promise +} +export class FlowFactsRebuildUnknown extends Error { + readonly code="FLOW_COMMIT_UNKNOWN" + constructor(readonly jobId:string,readonly configurationProof:string){super("Fact rebuild COMMIT is unconfirmed; use read-only reconciliation and do not repeat the rebuild")} +} +type CompleteRebuildResult=RebuildResult&{jobId:string;configurationProof:string;classifierProof:string;resultProof:string} +function completeRebuildReceipt(value:Record|null,jobId:string,configurationProof:string):CompleteRebuildResult { + const keys=["ok","buckets","facts","days","jobId","configurationProof","classifierProof","resultProof"] + if(!value||Buffer.byteLength(JSON.stringify(value))>60000||Object.keys(value).length!==keys.length||Object.keys(value).some(key=>!keys.includes(key))||value.ok!==true||value.jobId!==jobId||value.configurationProof!==configurationProof||!Number.isSafeInteger(value.buckets)||Number(value.buckets)<0||Number(value.buckets)>1000000||!Number.isSafeInteger(value.facts)||Number(value.facts)<0||Number(value.facts)>Number(value.buckets)||!Array.isArray(value.days)||value.days.length>1000||value.days.some(day=>typeof day!=="string"||!/^\d{4}-\d{2}-\d{2}$/.test(day)||!Number.isFinite(Date.parse(day+"T00:00:00Z"))||new Date(day+"T00:00:00Z").toISOString().slice(0,10)!==day)||new Set(value.days).size!==value.days.length||JSON.stringify([...value.days].sort())!==JSON.stringify(value.days)||[value.classifierProof,value.resultProof].some(proof=>typeof proof!=="string"||!/^[a-f0-9]{64}$/.test(proof))||(value.buckets===0&&(value.facts!==0||value.days.length!==0))||(Number(value.buckets)>0&&value.days.length===0))throw Error("Fact rebuild durable receipt result is invalid or incomplete") + return {ok:true,buckets:Number(value.buckets),facts:Number(value.facts),days:value.days.map(day=>String(day)),jobId,configurationProof,classifierProof:String(value.classifierProof),resultProof:String(value.resultProof)} +} +/** Includes indirect strict-drain effects. Cap is restricted to these approved + * servers; global pruning is forbidden in strict drain, including old retries. */ +export const FLOW_FACT_REBUILD_WRITE_FOOTPRINT={ + tables:["flow_buckets","flow_minute_stats","flow_minute_dims","flow_daily_dims","flow_hour_facts","flow_daily_facts","traffic_flow_settings","events","flow_write_receipts"], + partitionDdl:["flow_buckets","flow_minute_stats","flow_minute_dims","flow_daily_dims","flow_hour_facts","flow_daily_facts"], + globalPrune:false,asnCap:"authorized-servers-only", + objects:[{kind:"netflow-purge",id:"global"},{kind:"netflow-store",id:"global"},{kind:"netflow-rollups",id:"global"},{kind:"netflow-partitions",id:"global"},{kind:"netflow-facts",id:"global"},{kind:"events-journal",id:"global"},{kind:"netflow-settings",id:"1"}], +} as const +/** Include protected future leaves as well as existing ones. Conservatively + * include every partition protection; no catalog-existence join can hide one. */ +export async function readFactRebuildProtectionSource(serverIds:readonly number[],authorize:()=>Promise,client:Pick=pool) { + await authorize() + const rows=(await client.query(`SELECT + CASE WHEN octet_length(object_ref->>'kind')<=64 THEN object_ref->>'kind' ELSE NULL END AS kind, + CASE WHEN octet_length(object_ref->>'id')<=512 THEN object_ref->>'id' ELSE NULL END AS id, + CASE WHEN object_ref->>'serverId' IS NULL THEN NULL WHEN octet_length(object_ref->>'serverId')<=16 THEN object_ref->>'serverId' ELSE '__invalid__' END AS "serverId", + CASE WHEN octet_length(object_ref->>'protection')<=64 THEN object_ref->>'protection' ELSE NULL END AS protection, + xmin::text AS revision + FROM mcp_protections WHERE object_ref->>'kind'='database-partition' + OR object_ref->>'serverId'=ANY($1::text[]) + OR (object_ref->>'kind'='server' AND object_ref->>'id'=ANY($1::text[])) + OR EXISTS(SELECT 1 FROM jsonb_array_elements($2::jsonb) target WHERE target->>'kind'=object_ref->>'kind' AND target->>'id'=object_ref->>'id') + ORDER BY object_ref->>'kind',object_ref->>'id',object_ref->>'serverId',object_ref->>'protection' LIMIT 1001`,[serverIds.map(String),JSON.stringify(FLOW_FACT_REBUILD_WRITE_FOOTPRINT.objects)])).rows + if(rows.length>1000||rows.some(row=>typeof row.kind!=="string"||!row.kind||typeof row.id!=="string"||!row.id||typeof row.protection!=="string"||!row.protection||typeof row.revision!=="string"||!/^\d{1,16}$/.test(row.revision)||(row.serverId!=null&&(!Number.isSafeInteger(Number(row.serverId))||Number(row.serverId)<=0))))throw Error("Fact rebuild protections exceed bound or are invalid") + await authorize() + return {objects:rows.map(row=>({kind:String(row.kind),id:String(row.id),...(row.serverId==null?{}:{serverId:Number(row.serverId)})})),proofInput:rows.map(row=>({kind:row.kind,id:row.id,serverId:row.serverId??null,protection:row.protection,revision:row.revision}))} +} +export async function readFactRebuildConfiguration(serverIds:readonly number[],authorize:()=>Promise,client:Pick=pool) { + await authorize() + const servers=(await client.query("SELECT id,xmin::text AS revision FROM servers ORDER BY id LIMIT 1001")).rows + if(servers.length>1000||servers.length!==serverIds.length||servers.some(row=>!serverIds.includes(Number(row.id))))throw Error("Rebuild configuration requires the exact current server catalog") + await authorize() + const bindings=(await client.query("SELECT id,xmin::text AS revision FROM user_interface_bindings ORDER BY id LIMIT 1001")).rows + await authorize() + const users=(await client.query("SELECT id,xmin::text AS revision FROM app_users ORDER BY id LIMIT 1001")).rows + await authorize() + const snapshots=(await client.query("SELECT DISTINCT ON(server_id) server_id,xmin::text AS revision FROM server_snapshots ORDER BY server_id,polled_at DESC LIMIT 1001")).rows + if(bindings.length>1000||users.length>1000||snapshots.length>1000)throw Error("Rebuild topology metadata exceeds bound") + await authorize() + const settings=(await client.query("SELECT top_n,retention_hours FROM traffic_flow_settings WHERE id=1")).rows[0] + if(!settings)throw Error("Rebuild NetFlow settings unavailable") + const protections=await readFactRebuildProtectionSource(serverIds,authorize,client) + const proof=await privateSecretFingerprint(JSON.stringify({servers,bindings,users,snapshots,settings,protections:protections.proofInput}),"flow-fact-rebuild-config-v3") + await authorize();return {proof,serverIds:servers.map(row=>Number(row.id)),objects:[...FLOW_FACT_REBUILD_WRITE_FOOTPRINT.objects.map(object=>({...object})),...protections.objects]} +} + +/** Production transaction service. REST/MCP wiring must supply its own live + * authority and the verified strict-drain API; absence is never a no-op drain. */ +export async function rebuildFlowFactsSafely(authority:FactRebuildAuthority,request:{jobId?:string;expectedConfigurationProof?:string}={}):Promise { + const jobId=request.jobId??randomUUID() + const serverIds=[...authority.serverIds] + if(!/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(jobId))throw Error("Invalid fact rebuild job ID") + if(serverIds.length>1000||new Set(serverIds).size!==serverIds.length||serverIds.some(id=>!Number.isSafeInteger(id)||id<=0))throw Error("Invalid fact rebuild server scope") + await authority.authorize() + const initial=await readFactRebuildConfiguration(serverIds,()=>authority.authorize()) + if(request.expectedConfigurationProof&&initial.proof!==request.expectedConfigurationProof)throw Error("Fact rebuild source changed since approval") + const authorize=()=>assertSource(flowWriteContext()?.client) + let classifierProof="",resultProof="",publicationMayHaveCommitted=false + const assertSource=async(client?:PoolClient)=>{ + client=client??flowWriteContext()?.client + await authority.authorize(client) + const current=await readFactRebuildConfiguration(serverIds,()=>authority.authorize(client),client??pool) + if(current.proof!==initial.proof)throw Error("Fact rebuild configuration changed") + } + const execute=()=>executeFlowFactsRebuild({ + authorize, + // The complete executor is called only while the outer admission owns8723104. + admit:work=>work(), + drain:async()=>{await assertSource();await authority.drainExisting(authorize,serverIds);await assertSource()}, + atomic:async work=>{ + try{ + const result=await commitFlowWrite({id:jobId,kind:"rebuild",sourceProof:initial.proof,exclusive:true},async()=>({...(await work()),jobId,configurationProof:initial.proof,classifierProof,resultProof}),authorize) + publicationMayHaveCommitted=true;return result + }catch(error){if(error instanceof FlowCommitUnknown)publicationMayHaveCommitted=true;throw error} + }, + snapshot:async()=>{ + const client=flowWriteContext()?.client;if(!client)throw Error("Rebuild lost its owned transaction") + return readFlowRebuildSnapshot(client,serverIds,{ + authorize:authority.authorize, + validateSource:assertSource, + captureClassifier:async()=>{ + await authorize();const capturedSource=await readFactRebuildTopology(client,serverIds,authorize) + await assertSource(client) + const captured=captureFlowFactRebuildClassifier(capturedSource.topology,capturedSource.serverTypes,serverIds) + classifierProof=await privateSecretFingerprint(captured.serializedProofInput,"flow-fact-rebuild-classifier-v1") + return captured.classify + }, + }) + }, + replace:async(days,rows)=>{ + const client=flowWriteContext()?.client;if(!client)throw Error("Rebuild publication lost its transaction") + await assertSource(client) + await client.query("DELETE FROM flow_hour_facts WHERE (bucket_at AT TIME ZONE 'UTC')::date=ANY($1::date[])",[days]) + await authorize() + await client.query("DELETE FROM flow_daily_facts WHERE day=ANY($1::date[])",[days]) + await authorize() + await persistFlowFactRows([...rows]) + resultProof=await privateSecretFingerprint(JSON.stringify(rows),"flow-fact-rebuild-result-v1") + }, + event:async value=>{ + const client=flowWriteContext()?.client;if(!client)throw Error("Rebuild event lost its transaction") + if(!resultProof)resultProof=await privateSecretFingerprint("[]","flow-fact-rebuild-result-v1") + await assertSource(client) + await client.query("INSERT INTO events(id,created_at,level,event_type,source_module,title,message,entity_type,entity_id,payload_json) VALUES($1,now(),'info','traffic.flow.rebuild_facts','traffic','Пересчитан куб NetFlow',$2,'traffic_flow','rebuild-facts',$3::jsonb)",[jobId,`Факты ${value.facts} из ${value.buckets} сессий, дней ${value.days.length}`,JSON.stringify(value)]) + }, + }) + const result=await withApplicationMutationLock(()=>withOperationLock(()=>pool.connect(),8723104,()=>executeGuardedApplicationWrite({kind:"netflow-facts",id:"global"},serverIds,async()=>{ + await assertSource() + // Same-job barrier plus final live authority check. Receipt absence here is + // only an observation under global admission, never a replay token. + const known=await reconcileFlowWrite({id:jobId,kind:"rebuild",sourceProof:initial.proof},authorize) + await assertSource() + if(known.committed){publicationMayHaveCommitted=true;return completeRebuildReceipt(known.result,jobId,initial.proof)} + return execute() + },async()=>initial.objects.map(object=>({...object}))),"Пересчёт или сброс NetFlow уже выполняется")) + .catch(error=>{if(publicationMayHaveCommitted)throw new FlowFactsRebuildUnknown(jobId,initial.proof);throw error}) + // commitFlowWrite returns an existing durable result without running its body. + return {...result,jobId,configurationProof:initial.proof,classifierProof:(result as {classifierProof?:string}).classifierProof??classifierProof,resultProof:(result as {resultProof?:string}).resultProof??resultProof} +} + +/** Never reruns the rebuild. The transaction helper waits out an in-flight old + * COMMIT using the same per-job advisory barrier before interpreting absence. */ +export async function reconcileFlowFactsRebuild(jobId:string,configurationProof:string,authority:Pick) { + return reconcileFlowWrite({id:jobId,kind:"rebuild",sourceProof:configurationProof},()=>authority.authorize()) +} diff --git a/backend/src/services/traffic-flow-facts-rebuild-source.test.ts b/backend/src/services/traffic-flow-facts-rebuild-source.test.ts new file mode 100644 index 00000000..1856ac21 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-source.test.ts @@ -0,0 +1,46 @@ +import assert from "node:assert/strict" +import type { PoolClient } from "pg" +import { withFlowWriteClient } from "./traffic-flow-write-transaction.js" +import { readFlowRebuildSnapshot } from "./traffic-flow-facts-rebuild-source.js" +import type { RebuildBucketRow } from "./traffic-flow-facts-rebuild-execution.js" +import { buildFactRebuildTopology,type RebuildTopologyInputs } from "./traffic-flow-facts-rebuild-topology.js" + +const calls:string[]=[],row:RebuildBucketRow={serverId:1,bucketAt:"2026-10-24T00:00:00Z",src:"192.0.2.1",dst:"198.51.100.1",proto:6,srcPort:1,dstPort:443,bytes:1,packets:1,inIface:"ether1",outIface:"",natSrc:"",natDst:"",natSrcPort:0,natDstPort:0} +let affected=[1,2],catalog=[1,2],fetches=0,captures=0,checks=0 +const client={query:async(sql:string)=>{ + calls.push(sql) + if(sql.startsWith("SELECT id FROM servers"))return {rows:catalog.map(id=>({id}))} + if(sql.startsWith("WITH source_days"))return {rows:affected.map(server_id=>({server_id}))} + if(sql.startsWith("FETCH")){fetches++;return {rows:fetches===1?[row]:[]}} + return {rows:[]} +}} as unknown as PoolClient +const dependencies={authorize:async(current:PoolClient)=>{checks++;assert.strictEqual(current,client)},validateSource:async(current:PoolClient)=>{assert.strictEqual(current,client)},captureClassifier:async()=>{captures++;return (bucket:RebuildBucketRow)=>({serverId:bucket.serverId,iface:bucket.inIface,country:"XX",service:"other",asn:0,bytes:bucket.bytes,packets:bucket.packets})}} +await assert.rejects(readFlowRebuildSnapshot(client,[1,2],dependencies),/exclusive/) +assert.equal(calls.length,0) +await withFlowWriteClient(client,async()=>{ + await assert.rejects(readFlowRebuildSnapshot(client,[1],dependencies),/complete server/) + assert.equal(captures,0) + catalog=[1];affected=[1,999] + const beforeOrphanReads=calls.filter(sql=>sql.startsWith("WITH source_days")).length + await assert.rejects(readFlowRebuildSnapshot(client,[1,999],dependencies),/exact current catalog/) + assert.equal(calls.filter(sql=>sql.startsWith("WITH source_days")).length,beforeOrphanReads,"superset scope cannot authorize orphan history even before raw affected IDs") + assert.equal(captures,0);catalog=[1,2] + affected=[999];await assert.rejects(readFlowRebuildSnapshot(client,[1,2],dependencies),/orphan/) + assert.equal(captures,0);assert.ok(!calls.some(sql=>sql.startsWith("DECLARE"))) + affected=[1,2];const source=await readFlowRebuildSnapshot(client,[1,2],dependencies) + for await(const batch of source.rows){assert.deepEqual(batch,[row]);break} + assert.ok(calls.some(sql=>sql.startsWith("CLOSE flow_rebuild_"))) + assert.equal(fetches,1);assert.equal(captures,1);assert.ok(checks>=5) +},"exclusive") +assert.ok(calls.some(sql=>sql.includes("ORDER BY bucket_at,server_id,src,dst,proto,src_port,dst_port,in_iface"))) +assert.ok(!calls.some(sql=>sql.includes("OFFSET"))) + +const inputs:RebuildTopologyInputs={servers:[{id:1,name:"jh",host:"203.0.113.1",type:"jump-host",wanUplinks:[{iface:"ether1",ip:"203.0.113.2"}]},{id:2,name:"en",host:"198.51.100.1",type:"exit-node",wanUplinks:"[]"}],users:[{id:"u",login:"login",name:"Name"}],bindings:[{userId:"u",serverId:1,interfaceName:"wg-peer-binding"}],snapshots:[{serverId:1,rawInterfaces:[{name:"gre-client",type:"gre"},{name:"wg-flow",type:"wg"},{name:"wg-peer-binding",type:"wg"},{name:"ether1",type:"ether"}]}]} +const original=structuredClone(inputs),topology=buildFactRebuildTopology(inputs) +assert.deepEqual(inputs,original) +assert.deepEqual([...topology.jhHosts],["203.0.113.1","203.0.113.2"]);assert.deepEqual([...topology.enHosts],["198.51.100.1"]) +assert.deepEqual([...topology.clientIfaces.get(1)!],["wg-peer-binding"]) +assert.deepEqual(topology.clientByIface.get("1|wg-peer-binding"),{userId:"u",serverId:1,interfaceName:"wg-peer-binding",login:"login",name:"Name"}) +assert.deepEqual([...topology.tunnelIfaces!.get(1)!],["gre-client","wg-peer-binding"]) +assert.deepEqual([...topology.wanIfaces.get(1)!],["ether1"]) +console.log("traffic-flow-facts-rebuild-source.test.ts: exclusive owned-client cursor, full/global and same-day foreign history scope, early CLOSE, no OFFSET and full REST topology rules verified with fake client; no SQL/network executed") diff --git a/backend/src/services/traffic-flow-facts-rebuild-source.ts b/backend/src/services/traffic-flow-facts-rebuild-source.ts new file mode 100644 index 00000000..2a314a2f --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-source.ts @@ -0,0 +1,48 @@ +import { randomUUID } from "node:crypto" +import type { PoolClient } from "pg" +import { flowWriteContext } from "./traffic-flow-write-transaction.js" +import type { RebuildBucketRow, RebuildSnapshot } from "./traffic-flow-facts-rebuild-execution.js" + +export interface RebuildSourceDependencies { + authorize:(client:PoolClient)=>Promise + /** Factory returns a frozen, synchronous, no-I/O classifier, never global live accumulators. */ + captureClassifier:(client:PoolClient)=>Promise + /** Checks sealed topology/configuration/ownership proof with this same client. */ + validateSource:(client:PoolClient)=>Promise +} +/** Called only inside the owner's exclusive fenced transaction. Every cursor + * request uses that client, so normal writer SQL cannot move OFFSET pages. */ +export async function readFlowRebuildSnapshot(client:PoolClient,serverIds:readonly number[],deps:RebuildSourceDependencies):Promise { + const context=flowWriteContext() + if(context?.client!==client||context.mode!=="exclusive")throw Error("Fact rebuild source requires its exclusive owned transaction") + if(serverIds.length>1000||new Set(serverIds).size!==serverIds.length||serverIds.some(id=>!Number.isSafeInteger(id)||id<=0))throw Error("Invalid complete rebuild server scope") + await deps.authorize(client) + const catalog=(await client.query<{id:number}>("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>row.id) + if(catalog.length>1000||catalog.length!==serverIds.length||catalog.some(id=>!serverIds.includes(id)))throw Error("Fact rebuild requires complete server scope matching the exact current catalog") + await deps.authorize(client) + // Replacement is by day, not server: facts from a server without any buckets + // on that day are still affected. Detect orphan/ungranted history before raw rows. + const affected=(await client.query<{server_id:number}>(`WITH source_days AS (SELECT DISTINCT (bucket_at AT TIME ZONE 'UTC')::date AS day FROM flow_buckets) + SELECT server_id FROM flow_buckets + UNION SELECT server_id FROM flow_hour_facts WHERE (bucket_at AT TIME ZONE 'UTC')::date IN (SELECT day FROM source_days) + UNION SELECT server_id FROM flow_daily_facts WHERE day IN (SELECT day FROM source_days) + LIMIT 1001`)).rows.map(row=>row.server_id) + if(affected.length>1000||affected.some(id=>!Number.isSafeInteger(id)||!serverIds.includes(id)))throw Error("Fact rebuild affects ungranted or orphan historical servers") + await deps.authorize(client);await deps.validateSource(client) + const classify=await deps.captureClassifier(client) + await deps.authorize(client) + const cursor="flow_rebuild_"+randomUUID().replaceAll("-","") + await client.query(`DECLARE ${cursor} NO SCROLL CURSOR FOR + SELECT server_id AS "serverId",bucket_at AS "bucketAt",host(src) AS src,host(dst) AS dst,proto, + src_port AS "srcPort",dst_port AS "dstPort",bytes,packets, + CASE WHEN octet_length(in_iface)<=512 THEN in_iface ELSE NULL END AS "inIface", + CASE WHEN octet_length(COALESCE(out_iface,''))<=512 THEN COALESCE(out_iface,'') ELSE NULL END AS "outIface", + COALESCE(host(nat_src),'') AS "natSrc",COALESCE(host(nat_dst),'') AS "natDst",COALESCE(nat_src_port,0) AS "natSrcPort",COALESCE(nat_dst_port,0) AS "natDstPort" + FROM flow_buckets ORDER BY bucket_at,server_id,src,dst,proto,src_port,dst_port,in_iface`) + const rows=(async function*(){ + try { + for(;;){await deps.authorize(client);const batch=(await client.query(`FETCH FORWARD 4000 FROM ${cursor}`)).rows;if(!batch.length)break;yield batch} + }finally{await client.query(`CLOSE ${cursor}`)} + })() + return {serverIds,rows,classify,validate:async()=>{await deps.authorize(client);await deps.validateSource(client)}} +} diff --git a/backend/src/services/traffic-flow-facts-rebuild-topology.ts b/backend/src/services/traffic-flow-facts-rebuild-topology.ts new file mode 100644 index 00000000..a10d0fb9 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild-topology.ts @@ -0,0 +1,50 @@ +import type { PoolClient } from "pg" +import { parseJsonArray } from "../db/json.js" +import { parseRawInterfaces } from "../modules/users/iface-type.js" +import type { FlowTopology } from "./traffic-flow-topology.js" + +export interface RebuildTopologyInputs { + servers:Array<{id:number;name:string;host:string;type:string;wanUplinks:unknown}> + users:Array<{id:string;login:string;name:string}> + bindings:Array<{userId:string;serverId:number;interfaceName:string}> + snapshots:Array<{serverId:number;rawInterfaces:unknown}> +} +/** Same full-catalog rules as legacy REST topology, including all bindings. + * Unlike scoped analytics, rebuild is global and must not silently change + * peer-binding filtering while replacing the global cube. */ +export function buildFactRebuildTopology(input:RebuildTopologyInputs):FlowTopology { + const loginById=new Map(input.users.map(user=>[user.id,user])),clientIfaces=new Map>(),clientByIface:FlowTopology["clientByIface"]=new Map(),allClientNames=new Set() + for(const binding of input.bindings){ + const set=clientIfaces.get(binding.serverId)??new Set();set.add(binding.interfaceName);clientIfaces.set(binding.serverId,set);allClientNames.add(binding.interfaceName) + const user=loginById.get(binding.userId) + clientByIface.set(`${binding.serverId}|${binding.interfaceName}`,{...binding,login:user?.login||binding.userId,name:user?.name||user?.login||binding.userId}) + } + const enHosts=new Set(),jhHosts=new Set(),enNodes:FlowTopology["enNodes"]=[],wanIfaces=new Map>(),tunnelIfaces=new Map>() + for(const server of input.servers){ + const wans=(Array.isArray(server.wanUplinks)?server.wanUplinks:parseJsonArray(server.wanUplinks)) as Array<{iface?:string;ip?:string}> + const hosts=[server.host,...wans.map(wan=>String(wan.ip??"").trim())].filter(Boolean),wanSet=new Set(wans.map(wan=>String(wan.iface??"").trim()).filter(Boolean)) + if(wanSet.size)wanIfaces.set(server.id,wanSet) + if(server.type==="exit-node"){for(const host of hosts)enHosts.add(host);enNodes.push({id:server.id,name:server.name||server.host,hosts})} + if(server.type==="jump-host")for(const host of hosts)jhHosts.add(host) + } + for(const snapshot of input.snapshots){const set=new Set();for(const iface of parseRawInterfaces(snapshot.rawInterfaces))if((iface.type==="gre"||iface.type==="wg")&&iface.name.toLowerCase()!=="wg-flow")set.add(iface.name);if(set.size)tunnelIfaces.set(snapshot.serverId,set)} + return {clientIfaces,clientByIface,enNodes,enHosts,jhHosts,wanIfaces,tunnelIfaces,plane:{clientIfaceNames:allClientNames,enHosts,jhHosts}} +} +export async function readFactRebuildTopology(client:PoolClient,serverIds:readonly number[],authorize:()=>Promise) { + await authorize() + const servers=(await client.query(`SELECT id,name,host,type,CASE WHEN octet_length(wan_uplinks::text)<=16384 THEN wan_uplinks ELSE NULL END AS "wanUplinks" FROM servers ORDER BY id LIMIT 1001`)).rows + if(servers.length>1000||servers.some(row=>row.wanUplinks===null||!serverIds.includes(row.id)))throw Error("Fact topology server source exceeds scope or bound") + await authorize() + const users=(await client.query("SELECT id,login,name FROM app_users ORDER BY id LIMIT 1001")).rows + await authorize() + const bindings=(await client.query(`SELECT user_id AS "userId",server_id AS "serverId",interface_name AS "interfaceName" FROM user_interface_bindings ORDER BY id LIMIT 1001`)).rows + if(users.length>1000||bindings.length>1000||bindings.some(row=>!serverIds.includes(row.serverId)))throw Error("Fact topology user/binding source exceeds scope or bound") + await authorize() + const size=(await client.query("SELECT COALESCE(sum(octet_length(raw_interfaces::text)),0)::text AS n FROM (SELECT DISTINCT ON(server_id) raw_interfaces FROM server_snapshots ORDER BY server_id,polled_at DESC) latest")).rows[0] + if(!size||!/^\d+$/.test(String(size.n))||Number(size.n)>4194304)throw Error("Fact topology snapshots exceed 4 MiB") + await authorize() + const snapshots=(await client.query(`SELECT DISTINCT ON(server_id) server_id AS "serverId",CASE WHEN octet_length(raw_interfaces::text)<=1048576 THEN raw_interfaces ELSE NULL END AS "rawInterfaces" FROM server_snapshots ORDER BY server_id,polled_at DESC LIMIT 1001`)).rows + if(snapshots.length>1000||snapshots.some(row=>row.rawInterfaces===null||!serverIds.includes(row.serverId)))throw Error("Fact topology snapshot exceeds scope or bound") + await authorize() + return {topology:buildFactRebuildTopology({servers,users,bindings,snapshots}),serverTypes:new Map(servers.map(row=>[row.id,row.type]))} +} diff --git a/backend/src/services/traffic-flow-facts-rebuild.pg.test.ts b/backend/src/services/traffic-flow-facts-rebuild.pg.test.ts new file mode 100644 index 00000000..24576336 --- /dev/null +++ b/backend/src/services/traffic-flow-facts-rebuild.pg.test.ts @@ -0,0 +1,162 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { PoolClient } from "pg" + +// This executable requires the root-owned disposable runner with ALL migrations. +// It intentionally does not test engine/worker draining and cannot start either. +const endpoint=new URL(process.env.DATABASE_URL??"postgresql://invalid") +assert.equal(process.env.MMAPP_OWN_FLOW_REBUILD,"true") +assert.equal(endpoint.protocol,"postgresql:") +assert.equal(endpoint.hostname,"127.0.0.1") +assert.equal(endpoint.pathname,"/postgres") +assert.match(process.env.MCP_STATE_DIR??"",/mm-mcp-test-/) +const savedFetch=globalThis.fetch +let networkAttempts=0,coldMetadataReads=0 +globalThis.fetch=(async()=>{networkAttempts++;throw Error("Rebuild acceptance forbids external HTTP")}) as typeof fetch +const {pool,closePool}=await import("../db/index.js") +const {ensurePartitionFor}=await import("../db/partitions.js") +const {rebuildFlowFactsSafely,reconcileFlowFactsRebuild,readFactRebuildConfiguration}=await import("./traffic-flow-facts-rebuild-service.js") +const {rememberServerIfaces,resetIfaceCacheForTests}=await import("./traffic-flow-ifindex.js") +const {seedRipeCacheForTests,resetRipeCacheForTests}=await import("./traffic-flow-ripe.js") +const {seedFlowCatalogForTests,resetFlowCatalogForTests}=await import("./traffic-flow-classify.js") +const {resetGeoipForTests}=await import("./traffic-flow-geoip.js") +const savedQuery=pool.query +pool.query=(function(...args:unknown[]){ + const input=args[0],sql=typeof input==="string"?input:(input as {text?:string})?.text??"" + if(/\b(?:FROM|JOIN)\s+flow_(?:ip|asn)_meta\b/i.test(sql)){coldMetadataReads++;throw Error("Frozen classifier must not load metadata SQL")} + return (savedQuery as (...values:unknown[])=>unknown).apply(pool,args) +}) as typeof pool.query + +const day="2026-10-24",unrelatedDay="2026-10-23",target=`${day}T10:00:00Z` +const protectionId=randomUUID() +let mode="normal",faultInjected=false,drains=0,drainDispatches=0 +const protection=async(kind="database-partition",id="flow_hour_facts/flow_hour_facts_d_20990101")=>{ + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'flow-rebuild-pg')",[protectionId,JSON.stringify({kind,id,protection:"deny"})]) +} +const authorize=async(client?:PoolClient)=>{ + // Fault injection observes a REAL uncommitted first-cube DELETE on its own + // transaction connection. It cannot be reached by a VM or shadow-table model. + if(!client||faultInjected||!["late-protection","source-change","revoked"].includes(mode))return + const remaining=Number((await client.query("SELECT count(*) AS n FROM flow_hour_facts WHERE (bucket_at AT TIME ZONE 'UTC')::date=$1",[day])).rows[0].n) + if(remaining!==0)return + faultInjected=true + if(mode==="revoked")throw Error("fixture authority revoked after hour DELETE") + if(mode==="source-change")await pool.query("UPDATE servers SET name='changed-during-rebuild' WHERE id=1") + else await protection("server","1") +} +const authority={serverIds:[1],authorize,drainExisting:async(check:()=>Promise,ids:readonly number[])=>{ + assert.deepEqual([...ids],[1]);drains++;await check() + // Explicit no-op existing-drain fixture. No engine/IPC/collector import/call. + if(mode==="drain-protection"){await protection();await check()} + drainDispatches++ +}} +const state=async()=>{ + const result:Record={} + for(const table of ["flow_hour_facts","flow_daily_facts","events","flow_write_receipts"]) + result[table]=(await pool.query(`SELECT to_jsonb(t) AS row FROM ${table} t ORDER BY to_jsonb(t)::text`)).rows.map(row=>row.row) + return result +} +const assertNoNextDailyDelete=async()=>{ + assert.equal((await pool.query("SELECT is_called FROM flow_rebuild_daily_delete_calls")).rows[0].is_called,false,"next cube DELETE must not dispatch after a failed live source/grant fence") +} + +try{ + // Fresh full-schema database only. Do not truncate or take over a populated DB. + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n),0) + const actual=(await pool.query("SELECT c.relname,c.relkind FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relname=ANY($1::text[])",[["flow_buckets","flow_hour_facts","flow_daily_facts","flow_write_receipts"]])).rows + assert.equal(actual.length,4,"runner must apply the complete production migration chain including0032") + for(const table of ["flow_buckets","flow_hour_facts","flow_daily_facts"])assert.equal(actual.find(row=>row.relname===table)?.relkind,"p","real partitioned cube/source parent required") + await pool.query("INSERT INTO servers(id,name,host,type,wan_uplinks) VALUES(1,'rebuild-fixture','203.0.113.10','jump-host',$1::jsonb)",[JSON.stringify([{iface:"ether1",ip:"203.0.113.10"}])]) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('flow-rebuild-user','flow-rebuild-user','Flow rebuild fixture')") + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type) VALUES($1,'flow-rebuild-user',1,'gre-client','gre')",[randomUUID()]) + for(const [parent,kind,at] of [["flow_buckets","day",target],["flow_hour_facts","day",target],["flow_daily_facts","month",target],["flow_hour_facts","day",`${unrelatedDay}T10:00:00Z`],["server_snapshots","week",target]] as const) + await ensurePartitionFor(pool,parent,kind,new Date(at)) + await pool.query("INSERT INTO server_snapshots(server_id,polled_at,status,raw_interfaces) VALUES(1,$1,'online',$2::jsonb)",[target,JSON.stringify([{name:"gre-client",type:"gre",ifindex:"10"},{name:"ether1",type:"ether"}])]) + await pool.query("INSERT INTO traffic_flow_settings(id) VALUES(1) ON CONFLICT(id) DO NOTHING") + for(const [minute,bytes,packets] of [["10:01",10,1],["10:02",5,2],["11:01",20,4]] as const) + 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,'95.167.1.10','8.8.8.8',6,1234,443,$2,$3,'10','ether1')",[`${day}T${minute}:00Z`,bytes,packets]) + for(const [at,service,bytes] of [[target,"old-target",999],[`${unrelatedDay}T10:00:00Z`,"unrelated",55]] as const){ + await pool.query("INSERT INTO flow_hour_facts(server_id,bucket_at,iface,country,service,asn,bytes,packets) VALUES(1,$1,'gre-client','DE',$2,64512,$3,9)",[at,service,bytes]) + await pool.query("INSERT INTO flow_daily_facts(server_id,day,iface,country,service,asn,bytes,packets) VALUES(1,$1::timestamptz::date,'gre-client','DE',$2,64512,$3,9)",[at,service,bytes]) + } + resetGeoipForTests();resetRipeCacheForTests();resetFlowCatalogForTests();resetIfaceCacheForTests() + rememberServerIfaces(1,[{name:"gre-client",ifindex:"10"}]) + seedRipeCacheForTests({prefix:"8.8.8.0/24",asn:15169,country:"US",lat:null,lng:null,holder:"GOOGLE",ok:true,fetchedAt:Date.now()}) + seedFlowCatalogForTests({cidrs:[{cidr:"8.8.8.0/24",purpose:"fixture-service"}]}) + + const jobId=randomUUID(),success=await rebuildFlowFactsSafely(authority,{jobId}) + assert.deepEqual({buckets:success.buckets,facts:success.facts,days:success.days},{buckets:3,facts:2,days:[day]}) + const hours=(await pool.query("SELECT to_char(bucket_at AT TIME ZONE 'UTC','HH24:MI') AS hour,iface,country::text,service,asn,bytes,packets FROM flow_hour_facts WHERE (bucket_at AT TIME ZONE 'UTC')::date=$1 ORDER BY bucket_at",[day])).rows + assert.deepEqual(hours,[{hour:"10:00",iface:"gre-client",country:"US",service:"fixture-service",asn:15169,bytes:15,packets:3},{hour:"11:00",iface:"gre-client",country:"US",service:"fixture-service",asn:15169,bytes:20,packets:4}]) + assert.deepEqual((await pool.query("SELECT iface,country::text,service,asn,bytes,packets FROM flow_daily_facts WHERE day=$1",[day])).rows,[{iface:"gre-client",country:"US",service:"fixture-service",asn:15169,bytes:35,packets:7}]) + for(const table of ["flow_hour_facts","flow_daily_facts"])assert.equal(Number((await pool.query(`SELECT sum(bytes) AS n FROM ${table} WHERE service='unrelated'`)).rows[0].n),55) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM events WHERE id=$1",[jobId])).rows[0].n),1) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM flow_write_receipts WHERE batch_id=$1",[jobId])).rows[0].n),1) + const baseline=await state() + const beforeKnownRepeat={drains,drainDispatches} + await rebuildFlowFactsSafely(authority,{jobId,expectedConfigurationProof:success.configurationProof}) + assert.equal(drains,beforeKnownRepeat.drains,"matching durable receipt must skip even the existing-drain fixture") + assert.equal(drainDispatches,beforeKnownRepeat.drainDispatches,"matching job must not dispatch new engine effects") + assert.deepEqual(await state(),baseline,"durable receipt prevents second cube/event publication") + const reconciled=await reconcileFlowFactsRebuild(jobId,success.configurationProof,authority) + assert.equal(reconciled.committed,true);assert.equal(reconciled.result?.jobId,jobId) + assert.ok(!JSON.stringify(reconciled).includes("8.8.8.8"),"receipt must not contain raw traffic IPs") + assert.deepEqual(await reconcileFlowFactsRebuild(randomUUID(),success.configurationProof,authority),{committed:false,result:null}) + + const beforeScope=drains + await assert.rejects(rebuildFlowFactsSafely({...authority,serverIds:[1,999]}),/exact current server catalog/) + assert.equal(drains,beforeScope);assert.deepEqual(await state(),baseline) + await protection() + const protectedSource=await readFactRebuildConfiguration([1],async()=>{}) + assert.ok(protectedSource.objects.some(object=>object.kind==="database-partition"&&object.id==="flow_hour_facts/flow_hour_facts_d_20990101")) + await assert.rejects(rebuildFlowFactsSafely(authority),/защищённый объект/) + assert.equal(drains,beforeScope);assert.deepEqual(await state(),baseline) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protectionId]) + mode="drain-protection" + const beforeDispatch=drainDispatches + await assert.rejects(rebuildFlowFactsSafely(authority),/configuration changed/) + assert.equal(drainDispatches,beforeDispatch);assert.deepEqual(await state(),baseline) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protectionId]) + + // A PostgreSQL sequence is deliberately nontransactional: it proves the next + // daily DELETE was never dispatched, even though the failed rebuild rolls back. + await pool.query("CREATE SEQUENCE flow_rebuild_daily_delete_calls") + await pool.query("CREATE FUNCTION flow_rebuild_observe_daily_delete() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN PERFORM nextval('flow_rebuild_daily_delete_calls'); RETURN NULL; END $$") + await pool.query("CREATE TRIGGER flow_rebuild_daily_delete BEFORE DELETE ON flow_daily_facts FOR EACH STATEMENT EXECUTE FUNCTION flow_rebuild_observe_daily_delete()") + for(const fault of ["late-protection","source-change","revoked"]){ + mode=fault;faultInjected=false + await pool.query("ALTER SEQUENCE flow_rebuild_daily_delete_calls RESTART WITH 1") + await assert.rejects(rebuildFlowFactsSafely(authority),/configuration changed|authority revoked/) + assert.equal(faultInjected,true,`${fault} reached first real cube deletion`) + await assertNoNextDailyDelete();assert.deepEqual(await state(),baseline,`${fault} rolls back both cubes/event/receipt`) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protectionId]) + await pool.query("UPDATE servers SET name='rebuild-fixture' WHERE id=1") + } + mode="normal" + await pool.query("UPDATE flow_hour_facts SET bytes=777 WHERE bucket_at=$1",[target]) + await pool.query("UPDATE flow_daily_facts SET bytes=888 WHERE day=$1",[day]) + const beforeRealSqlFailure=await state() + await pool.query("CREATE FUNCTION flow_rebuild_fail_daily_insert() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE n bigint; BEGIN SELECT sum(bytes) INTO n FROM flow_hour_facts WHERE (bucket_at AT TIME ZONE 'UTC')::date=DATE '2026-10-24'; IF n IS DISTINCT FROM 35 THEN RAISE EXCEPTION 'fixture unexpected hour replacement'; END IF; RAISE EXCEPTION 'fixture real SQL failure after hour replacement'; END $$") + await pool.query("CREATE TRIGGER flow_rebuild_daily_insert BEFORE INSERT ON flow_daily_facts FOR EACH STATEMENT EXECUTE FUNCTION flow_rebuild_fail_daily_insert()") + await assert.rejects(rebuildFlowFactsSafely(authority),/fixture real SQL failure after hour replacement/) + assert.deepEqual(await state(),beforeRealSqlFailure,"real SQL exception after hour INSERT restores old sentinel hour/day/event/receipt atomically") + await pool.query("DROP TRIGGER flow_rebuild_daily_insert ON flow_daily_facts") + await pool.query("DROP FUNCTION flow_rebuild_fail_daily_insert()") + + await pool.query("DELETE FROM flow_buckets") + const beforeEmpty=await state(),empty=await rebuildFlowFactsSafely(authority) + assert.deepEqual({buckets:empty.buckets,facts:empty.facts,days:empty.days},{buckets:0,facts:0,days:[]}) + const afterEmpty=await state() + assert.deepEqual(afterEmpty.flow_hour_facts,beforeEmpty.flow_hour_facts);assert.deepEqual(afterEmpty.flow_daily_facts,beforeEmpty.flow_daily_facts) + assert.equal(afterEmpty.events.length,beforeEmpty.events.length+1);assert.equal(afterEmpty.flow_write_receipts.length,beforeEmpty.flow_write_receipts.length+1) + assert.equal((await reconcileFlowFactsRebuild(empty.jobId,empty.configurationProof,authority)).committed,true) + assert.equal(networkAttempts,0);assert.equal(coldMetadataReads,0) + console.log("Real shared flow-facts rebuild: production partitioned schemas, canonical classifier/cube parity, unrelated/empty preservation, durable receipt/event once, exact scope, real partition/late server protection, source/revoke fences before next SQL, real post-hour SQL rollback and read-only reconcile; explicit no-op drain fixture, no engine/collector/notification starts") +}finally{ + // Only test-created observer objects; the dedicated runner removes the DB. + try{await pool.query("DROP TRIGGER IF EXISTS flow_rebuild_daily_insert ON flow_daily_facts");await pool.query("DROP TRIGGER IF EXISTS flow_rebuild_daily_delete ON flow_daily_facts");await pool.query("DROP FUNCTION IF EXISTS flow_rebuild_fail_daily_insert()");await pool.query("DROP FUNCTION IF EXISTS flow_rebuild_observe_daily_delete()");await pool.query("DROP SEQUENCE IF EXISTS flow_rebuild_daily_delete_calls")}finally{ + pool.query=savedQuery;globalThis.fetch=savedFetch + resetGeoipForTests();resetRipeCacheForTests();resetFlowCatalogForTests();resetIfaceCacheForTests() + await closePool() + } +} diff --git a/backend/src/services/traffic-flow-facts-rebuild.ts b/backend/src/services/traffic-flow-facts-rebuild.ts index 7451ab83..be920583 100644 --- a/backend/src/services/traffic-flow-facts-rebuild.ts +++ b/backend/src/services/traffic-flow-facts-rebuild.ts @@ -1,145 +1,29 @@ -import { dbAll, dbGet, dbQuery, withAdvisoryLock } from "../db/index.js" -import { flushEngineNow } from "./traffic-flow-engine.js" -import { resolveInternetDest } from "./traffic-flow-dest.js" -import { - bumpFlowFact, - discardPendingFacts, - flushFlowFacts, - hourBucketIso, -} from "./traffic-flow-facts.js" -import { shouldWriteFlowFact } from "./traffic-flow-facts-filter.js" -import { canonicalFactIface } from "./traffic-flow-ifindex.js" -import { getServerCatalog, loadFlowTopology } from "./traffic-flow-topology.js" +import type { PoolClient } from "pg" +import { pool } from "../db/index.js" +import { rebuildFlowFactsSafely } from "./traffic-flow-facts-rebuild-service.js" +import { drainExistingTrafficFlowWriters } from "./traffic-flow-ingest.js" export const FACT_REBUILD_LOCK_KEY = 8_723_104 -const BATCH = 4_000 - -export interface FlowFactsRebuildResult { - ok: true - buckets: number - facts: number - days: string[] -} - -function hourFromBucket(raw: Date | string): string { - const iso = raw instanceof Date ? raw.toISOString() : String(raw) - const ms = new Date(iso).getTime() - return hourBucketIso(Number.isFinite(ms) ? ms : Date.now()) +export interface FlowFactsRebuildResult { ok: true; buckets: number; facts: number; days: string[] } +const catalogSql = "SELECT id,xmin::text AS revision FROM servers ORDER BY id LIMIT 1001" +async function catalog(client: Pick) { + const rows = (await client.query<{ id: number; revision: string }>(catalogSql)).rows + if (rows.length > 1000 || rows.some((row, index) => !Number.isSafeInteger(row.id) || row.id <= 0 || index > 0 && rows[index - 1].id >= row.id || typeof row.revision !== "string" || !/^\d{1,16}$/.test(row.revision))) throw Error("Legacy fact rebuild requires an exact bounded current server catalog") + return rows.map(row => ({ id: row.id, revision: row.revision })) } +/** Trusted local CLI retains the complete service's application protections, + * frozen classifier, strict existing-writer drain and atomic durable receipt. + * Its authority is the exact bounded catalog, freshly re-read on the service's + * owned backend whenever supplied. It never fabricates MCP/JWT approval. + */ export async function rebuildFlowFactsFromBuckets(): Promise { - return await withAdvisoryLock(FACT_REBUILD_LOCK_KEY, async () => { - await flushEngineNow({ prune: false }) - discardPendingFacts() - - const days = await dbAll<{ day: string }>(` - SELECT DISTINCT (bucket_at AT TIME ZONE 'UTC')::date::text AS day - FROM flow_buckets - ORDER BY 1 - `) - const dayList = days.map((r) => r.day).filter(Boolean) - if (dayList.length === 0) { - return { ok: true as const, buckets: 0, facts: 0, days: [] } - } - - await dbQuery( - `DELETE FROM flow_hour_facts WHERE (bucket_at AT TIME ZONE 'UTC')::date = ANY(?::date[])`, - [dayList], - ) - await dbQuery( - `DELETE FROM flow_daily_facts WHERE day = ANY(?::date[])`, - [dayList], - ) - - const topo = await loadFlowTopology() - const catalog = await getServerCatalog() - let offset = 0 - let buckets = 0 - - for (;;) { - const rows = await dbAll<{ - serverId: number - bucketAt: Date | string - src: string - dst: string - proto: number - srcPort: number - dstPort: number - bytes: number - packets: number - inIface: string - outIface: string - natSrc: string - natDst: string - natSrcPort: number - natDstPort: number - }>(` - SELECT server_id AS "serverId", bucket_at AS "bucketAt", - host(src) AS src, host(dst) AS dst, proto, src_port AS "srcPort", dst_port AS "dstPort", - bytes, packets, in_iface AS "inIface", COALESCE(out_iface, '') AS "outIface", - COALESCE(host(nat_src), '') AS "natSrc", COALESCE(host(nat_dst), '') AS "natDst", - COALESCE(nat_src_port, 0) AS "natSrcPort", COALESCE(nat_dst_port, 0) AS "natDstPort" - FROM flow_buckets - ORDER BY bucket_at, server_id - LIMIT ? OFFSET ? - `, [BATCH, offset]) - if (rows.length === 0) break - for (const row of rows) { - buckets += 1 - const serverType = catalog.byId.get(row.serverId)?.type - const destMeta = resolveInternetDest({ - src: row.src, - dst: row.dst, - proto: Number(row.proto) || 0, - srcPort: Number(row.srcPort) || 0, - dstPort: Number(row.dstPort) || 0, - serverId: row.serverId, - inIface: row.inIface, - topo, - natSrc: row.natSrc, - natDst: row.natDst, - natSrcPort: Number(row.natSrcPort) || 0, - natDstPort: Number(row.natDstPort) || 0, - }) - if (!shouldWriteFlowFact({ - serverId: row.serverId, - serverType, - inIface: row.inIface, - outIface: row.outIface, - proto: Number(row.proto) || 0, - srcPort: Number(row.srcPort) || 0, - dstPort: Number(row.dstPort) || 0, - src: row.src, - dst: row.dst, - topo, - dest: destMeta.dest, - natSrc: row.natSrc, - natDst: row.natDst, - natSrcPort: Number(row.natSrcPort) || 0, - natDstPort: Number(row.natDstPort) || 0, - })) continue - bumpFlowFact({ - serverId: row.serverId, - bucketAt: hourFromBucket(row.bucketAt), - iface: canonicalFactIface(row.serverId, row.inIface), - country: destMeta.country || "XX", - service: destMeta.classified.service, - asn: destMeta.asn, - bytes: Number(row.bytes) || 0, - packets: Number(row.packets) || 0, - }) - } - offset += rows.length - if (rows.length < BATCH) break - } - - const facts = await flushFlowFacts() - const range = await dbGet<{ n: number }>(`SELECT COUNT(*)::int AS n FROM flow_buckets`) - return { - ok: true as const, - buckets: Number(range?.n) || buckets, - facts, - days: dayList, - } - }) + const initial = await catalog(pool), proof = JSON.stringify(initial) + const authorize = async (client?: PoolClient) => { + if (JSON.stringify(await catalog(client ?? pool)) !== proof) throw Error("Legacy fact rebuild server catalog changed; no further dispatch is authorized") + } + const result = await rebuildFlowFactsSafely({ serverIds: initial.map(row => row.id), authorize, drainExisting: drainExistingTrafficFlowWriters }) + // Unknown service errors carry jobId/configurationProof for read-only + // reconciliation. Deliberately no catch, fallback, retry or global accumulator. + return { ok: result.ok, buckets: result.buckets, facts: result.facts, days: result.days } } diff --git a/backend/src/services/traffic-flow-facts.ts b/backend/src/services/traffic-flow-facts.ts index a719983d..51fffe65 100644 --- a/backend/src/services/traffic-flow-facts.ts +++ b/backend/src/services/traffic-flow-facts.ts @@ -1,197 +1,47 @@ -import { pool } from "../db/index.js" +import { randomUUID } from "node:crypto" +import { flowWritePool as pool, flowWriteContext, commitFlowWrite, reconcileFlowWrite, FlowCommitUnknown } from "./traffic-flow-write-transaction.js" +import { flowBatchProof } from "./traffic-flow-batch-proof.js" +import { OwnedFlowBatch } from "./traffic-flow-owned-batch.js" +import { createSerialOperation } from "./serial-operation.js" import { ensurePartitionFor, specForParent } from "../db/partitions.js" +import { createFlowFactAccumulator, type FactRow } from "./traffic-flow-fact-accumulator.js" +export { + FACT_ASN_TOP, FACT_TUPLE_CAP, FACT_SERVICE_MAX_LEN, UNKNOWN_COUNTRY, OTHER_SERVICE, UNKNOWN_IFACE, + hourBucketIso, normalizeFactCountry, normalizeFactService, normalizeFactIface, normalizeFactAsn, +} from "./traffic-flow-fact-accumulator.js" +export type { FactAcc, FactRow } from "./traffic-flow-fact-accumulator.js" -export const FACT_ASN_TOP = 200 -export const FACT_TUPLE_CAP = 8000 -export const FACT_SERVICE_MAX_LEN = 64 -export const UNKNOWN_COUNTRY = "XX" -export const OTHER_SERVICE = "other" -export const UNKNOWN_IFACE = "__unknown__" - -export interface FactAcc { - bytes: number - packets: number -} - -export interface FactRow { - serverId: number - bucketAt: string - iface: string - country: string - service: string - asn: number - bytes: number - packets: number -} - -const hourFacts = new Map() +const liveFacts = createFlowFactAccumulator() const ensuredParts = new Set() +let ownedFacts: OwnedFlowBatch | null = null -export function hourBucketIso(at = Date.now()): string { - const d = new Date(at) - d.setMinutes(0, 0, 0) - return d.toISOString() -} - -export function normalizeFactCountry(raw: string): string { - const iso = raw.trim().toUpperCase() - if (/^[A-Z]{2}$/.test(iso)) return iso - return UNKNOWN_COUNTRY -} - -export function normalizeFactService(raw: string): string { - const s = raw.trim().slice(0, FACT_SERVICE_MAX_LEN) - return s || OTHER_SERVICE -} - -export function normalizeFactIface(raw: string): string { - return raw.trim() || UNKNOWN_IFACE -} - -export function normalizeFactAsn(raw: number): number { - if (!Number.isFinite(raw) || raw <= 0) return 0 - return Math.trunc(raw) -} - -function factKey( - serverId: number, - bucketAt: string, - iface: string, - country: string, - service: string, - asn: number, -): string { - return `${serverId}\0${bucketAt}\0${iface}\0${country}\0${service}\0${asn}` -} - -function parseFactKey(k: string, acc: FactAcc): FactRow | null { - const parts = k.split("\0") - if (parts.length !== 6) return null - const serverId = Number(parts[0]) - const asn = Number(parts[5]) - if (!Number.isFinite(serverId) || !Number.isFinite(asn)) return null - return { - serverId, - bucketAt: parts[1] ?? "", - iface: parts[2] ?? UNKNOWN_IFACE, - country: parts[3] ?? UNKNOWN_COUNTRY, - service: parts[4] ?? OTHER_SERVICE, - asn, - bytes: acc.bytes, - packets: acc.packets, - } -} - -export function bumpFlowFact(row: { - serverId: number - bucketAt: string - iface: string - country: string - service: string - asn: number - bytes: number - packets: number -}): void { - const iface = normalizeFactIface(row.iface) - const country = normalizeFactCountry(row.country) - const service = normalizeFactService(row.service) - const asn = normalizeFactAsn(row.asn) - const k = factKey(row.serverId, row.bucketAt, iface, country, service, asn) - const prev = hourFacts.get(k) - if (prev) { - prev.bytes += row.bytes - prev.packets += row.packets - return - } - hourFacts.set(k, { bytes: row.bytes, packets: row.packets }) -} - -function groupKey(row: FactRow): string { - return `${row.serverId}\0${row.bucketAt}` -} - -function mergeRow(map: Map, row: FactRow): void { - const k = factKey(row.serverId, row.bucketAt, row.iface, row.country, row.service, row.asn) - const prev = map.get(k) - if (prev) { - prev.bytes += row.bytes - prev.packets += row.packets - return - } - map.set(k, { ...row }) -} - -/** Cap ASN tail and tuple count per server×hour before persist. */ -export function collectCappedFacts(): FactRow[] { - const parsed: FactRow[] = [] - for (const [k, acc] of hourFacts) { - const row = parseFactKey(k, acc) - if (row) parsed.push(row) - } - hourFacts.clear() - - const groups = new Map() - for (const row of parsed) { - const g = groupKey(row) - const list = groups.get(g) ?? [] - list.push(row) - groups.set(g, list) - } - - const out = new Map() - for (const list of groups.values()) { - const byAsn = new Map() - for (const row of list) { - byAsn.set(row.asn, (byAsn.get(row.asn) ?? 0) + row.bytes) - } - const asnKeep = new Set( - [...byAsn.entries()] - .sort((a, b) => b[1] - a[1]) - .slice(0, FACT_ASN_TOP) - .map(([asn]) => asn), - ) - const afterAsn: FactRow[] = [] - for (const row of list) { - if (asnKeep.has(row.asn) || row.asn === 0) { - afterAsn.push(row) - continue - } - afterAsn.push({ ...row, asn: 0 }) - } - const collapsed = new Map() - for (const row of afterAsn) mergeRow(collapsed, row) - const tuples = [...collapsed.values()].sort((a, b) => b.bytes - a.bytes) - const keep = tuples.slice(0, FACT_TUPLE_CAP) - const tail = tuples.slice(FACT_TUPLE_CAP) - for (const row of keep) mergeRow(out, row) - for (const row of tail) { - mergeRow(out, { - ...row, - country: UNKNOWN_COUNTRY, - service: OTHER_SERVICE, - asn: 0, - }) - } - } - return [...out.values()] -} +export function hasPendingFlowFact(row:FactRow):boolean {return liveFacts.has(row)} +export function bumpFlowFact(row: FactRow): void { liveFacts.bump(row) } +export function collectCappedFacts(): FactRow[] { return liveFacts.collectCapped() } async function ensureParentPartition(parent: string, ts: string): Promise { const spec = specForParent(parent) if (!spec) return const iso = ts.length === 10 ? `${ts}T00:00:00Z` : ts const key = `${parent}:${iso.slice(0, 10)}` - if (ensuredParts.has(key)) return + const parts=flowWriteContext()?.partitions??ensuredParts + if (parts.has(key)) return await ensurePartitionFor(pool, parent, spec.kind, new Date(iso)) - ensuredParts.add(key) + parts.add(key) } function dayKey(bucketAt: string): string { return bucketAt.slice(0, 10) } -export async function flushFlowFacts(): Promise { - const rows = collectCappedFacts() +export function takePendingFlowFacts(): FactRow[] { + if(ownedFacts)throw new Error("A standalone fact batch still owns pending facts") + const rows=liveFacts.snapshot();liveFacts.clear();return rows +} + +/** Caller owns the transaction and private accumulator; this never drains live state. */ +export async function persistFlowFactRows(rows:FactRow[]): Promise { + if(!flowWriteContext())throw new Error("Fact persistence requires a fenced transaction") if (rows.length === 0) return 0 const hours = new Set(rows.map((r) => r.bucketAt)) const days = new Set(rows.map((r) => dayKey(r.bucketAt))) @@ -236,7 +86,8 @@ export async function flushFlowFacts(): Promise { INSERT INTO flow_daily_facts ( server_id, day, iface, country, service, asn, bytes, packets ) - SELECT * + SELECT server_id, day, iface, country, service, asn, + sum(bytes)::bigint, sum(packets)::bigint FROM UNNEST( $1::bigint[], $2::date[], @@ -247,6 +98,7 @@ export async function flushFlowFacts(): Promise { $7::bigint[], $8::bigint[] ) AS t(server_id, day, iface, country, service, asn, bytes, packets) + GROUP BY server_id, day, iface, country, service, asn ON CONFLICT (server_id, day, iface, country, service, asn) DO UPDATE SET bytes = flow_daily_facts.bytes + excluded.bytes, @@ -266,26 +118,56 @@ export async function flushFlowFacts(): Promise { return rows.length } +const serializeFacts=createSerialOperation() +export function flushFlowFacts():Promise{return serializeFacts(flushOwnedFacts)} +async function flushOwnedFacts(): Promise { + if(!ownedFacts){const raw=takePendingFlowFacts();if(!raw.length)return 0;ownedFacts=new OwnedFlowBatch(randomUUID(),"",raw)} + const batch=ownedFacts + if(!batch.sourceProof)batch.prove(flowBatchProof(batch.payload,"facts")) + batch.assertProof(flowBatchProof(batch.payload,"facts")) + batch.begin() + try { + const result=await commitFlowWrite({id:batch.id,kind:"facts",sourceProof:batch.sourceProof},async()=>{ + const accumulator=createFlowFactAccumulator();for(const row of batch.payload)accumulator.bump(row) + const facts=await persistFlowFactRows(accumulator.collectCapped()) + batch.assertProof(flowBatchProof(batch.payload,"facts")) + return {facts} + }) + batch.committed();ownedFacts=null;return result.facts + }catch(error){if(error instanceof FlowCommitUnknown)batch.unknown();else batch.rolledBack();throw error} +} + +export async function reconcilePendingFactWrite(authorize:()=>Promise=async()=>{}) { + return serializeFacts(async()=>{ + if(!ownedFacts||ownedFacts.state!=="unknown")return {state:ownedFacts?.state??"idle"} + const batch=ownedFacts,result=await reconcileFlowWrite({id:batch.id,kind:"facts",sourceProof:batch.sourceProof},authorize) + batch.reconciled(result.committed?"committed":"absent-after-barrier") + if(result.committed)ownedFacts=null + return {state:batch.state,batchId:batch.id} + }) +} + export function factsPendingSize(): number { - return hourFacts.size + return liveFacts.size+(ownedFacts?.payload.length??0) } export const resetFactsForTests = resetFactsAfterDatabaseRestore +export function assertFactsResetAllowed():void { + ownedFacts?.assertResolved() +} export function resetFactsAfterDatabaseRestore(): void { - hourFacts.clear() + assertFactsResetAllowed() + liveFacts.clear() + ownedFacts=null ensuredParts.clear() } export function discardPendingFacts(): void { - hourFacts.clear() + if(ownedFacts)throw new Error("Cannot discard an owned fact write batch") + liveFacts.clear() } export function factsSnapshotForTests(): FactRow[] { - const parsed: FactRow[] = [] - for (const [k, acc] of hourFacts) { - const row = parseFactKey(k, acc) - if (row) parsed.push(row) - } - return parsed + return liveFacts.snapshot() } diff --git a/backend/src/services/traffic-flow-geoip.ts b/backend/src/services/traffic-flow-geoip.ts index 085d5a68..2c7e46ce 100644 --- a/backend/src/services/traffic-flow-geoip.ts +++ b/backend/src/services/traffic-flow-geoip.ts @@ -116,11 +116,11 @@ function safeAsn(reader: Reader, ip: string): { asn: number; holder * Синхронный lookup по локальным GeoLite2. Возвращает FlowIpMeta в семантике RIPE-кэша * (ok=true когда есть страна или ASN; null — данных нет, пусть пробует RIPE). */ -export function lookupGeoip(ip: string): FlowIpMeta | null { +export function lookupGeoip(ip: string,fromReaders:GeoipReaders=readers): FlowIpMeta | null { const trimmed = canonicalIp(ip) if (!trimmed) return null if (isNonPublicIp(trimmed)) return negativeMeta(trimmed) - const { country: countryReader, asn: asnReader } = readers + const { country: countryReader, asn: asnReader } = fromReaders if (!countryReader && !asnReader) return null const iso = countryReader ? safeCountryIso(countryReader, trimmed) : "" const country = iso && isIsoCountry(iso) ? iso : "" @@ -138,6 +138,15 @@ export function lookupGeoip(ip: string): FlowIpMeta | null { } } +/** Reader references are immutable; reload replaces globals but not this view. */ +export function captureGeoipLookup() { + const captured={...readers},observedAt=Date.now() + return {status:{countryBuildAt:captured.country?.metadata.buildEpoch.toISOString()??null,asnBuildAt:captured.asn?.metadata.buildEpoch.toISOString()??null},lookup:(ip:string)=>{ + const result=lookupGeoip(ip,captured) + return result?{...result,fetchedAt:observedAt}:null + }} +} + /** Главный вход для потребителей пайплайна: локальные базы первыми, RIPE-кэш fallback. */ export function resolveFlowIp(ip: string): FlowIpMeta | null { return lookupGeoip(ip) ?? lookupRipeCached(ip) diff --git a/backend/src/services/traffic-flow-heartbeat-receiver.test.ts b/backend/src/services/traffic-flow-heartbeat-receiver.test.ts new file mode 100644 index 00000000..630b9fd5 --- /dev/null +++ b/backend/src/services/traffic-flow-heartbeat-receiver.test.ts @@ -0,0 +1,64 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { test } from "node:test" +import ts from "typescript" +import { createFlowIngestQuality } from "./traffic-flow-ingest-quality.js" +import { flowIngestQualitySchema } from "@mmapp/contracts/traffic-flow" + +function source(names:string[]):string { + const text=readFileSync(new URL("./traffic-flow-ingest.ts",import.meta.url),"utf8") + const ast=ts.createSourceFile("candidate.ts",text,ts.ScriptTarget.ES2022,true) + return ts.transpileModule(names.map(name=>{const node=ast.statements.find(node=>ts.isFunctionDeclaration(node)&&node.name?.text===name);assert(node,name);return node.getText(ast)}).join("\n"),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText +} +function fixture() { + const original=Error("original ring validation"),ackError=Error("original transport failure"),events:string[]=[] + let badRing=false,badAck=false,quality:unknown=null + 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 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"])} + 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}}) + 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} +} +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)})) + assert.deepEqual(f.events,["consume","invalidate","ack"]);assert.equal(f.run.inspect().heartbeatFailure,f.original) + assert.equal(f.run.inspect().lastHeartbeat.packetsReceived,11);assert.equal(f.run.health().alive,false);assert.equal(f.run.health().bound,false) + assert.equal(f.quality(),null);assert.equal((await f.run.counters()).dataQuality,null);assert.equal((await f.run.counters()).lastError,"unavailable") + f.ring(false);f.run.receive({type:"heartbeat",heartbeatId:"good",payload:f.payload(12)}) + assert.equal(f.run.inspect().heartbeatFailure,null);assert.equal(f.run.health().alive,true);assert.equal((await f.run.counters()).packetsReceived,12) +}) +test("inconsistent quality never makes a healthy observation or replaces prior counters",()=>{ + const f=fixture();f.run.receive({type:"heartbeat",heartbeatId:"first",payload:f.payload()});f.events.length=0 + const payload=f.payload(999);payload.dataQuality={...payload.dataQuality,records:"1"} + assert.doesNotThrow(()=>f.run.receive({type:"heartbeat",heartbeatId:"bad",payload}));assert.deepEqual(f.events,["invalidate","ack"]) + assert.equal(f.run.health().alive,false);assert.equal(f.run.inspect().lastHeartbeat.packetsReceived,11) +}) +test("initial malformed metadata uses persisted counters marked unknown instead of fabricated healthy zero",async()=>{ + for(const change of [(value:Record)=>{value.packetsReceived=NaN},(value:Record)=>{value.lastDatagramAt="bad"},(value:Record)=>{value.lastError="x".repeat(4097)}]){ + const f=fixture(),payload=f.payload();change(payload);assert.doesNotThrow(()=>f.run.receive({type:"heartbeat",heartbeatId:"bad",payload})) + assert.equal(f.run.inspect().lastHeartbeat,null);assert.equal(f.run.health().alive,false) + const counters=await f.run.counters();assert.equal(counters.packetsReceived,77);assert.equal(counters.dataQuality,null);assert.equal(counters.lastError,"unavailable") + } +}) +test("ACK failure remains unavailable without escaping the event callback; first error survives until valid observation",()=>{ + const f=fixture();f.ack(true);assert.doesNotThrow(()=>f.run.receive({type:"heartbeat",heartbeatId:"first",payload:f.payload()})) + assert.equal(f.run.inspect().heartbeatFailure,f.ackError);assert.equal(f.run.health().alive,false) + f.ring(true);assert.doesNotThrow(()=>f.run.receive({type:"heartbeat",heartbeatId:"next",payload:f.payload(99)}));assert.equal(f.run.inspect().heartbeatFailure,f.ackError) +}) +test("legacy missing quality is explicitly unknown; it cannot claim complete aggregates",async()=>{ + const f=fixture(),payload:Record=f.payload();delete payload.dataQuality + f.run.receive({type:"heartbeat",heartbeatId:"legacy",payload}) + assert.equal(f.quality(),null);assert.equal((await f.run.counters()).dataQuality,null) +}) +test("local test helper never emits a fake IPC ACK and keeps validation failures visible to its caller",()=>{ + const f=fixture();f.run.local(f.payload());assert.deepEqual(f.events,["consume"]) + f.ring(true);assert.throws(()=>f.run.local(f.payload()),error=>error===f.original) + assert.deepEqual(f.events,["consume","consume"]) +}) diff --git a/backend/src/services/traffic-flow-heartbeat-wiring.test.ts b/backend/src/services/traffic-flow-heartbeat-wiring.test.ts new file mode 100644 index 00000000..9611f0a0 --- /dev/null +++ b/backend/src/services/traffic-flow-heartbeat-wiring.test.ts @@ -0,0 +1,39 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { test } from "node:test" +import ts from "typescript" +import { createFlowHeartbeatSender } from "./traffic-flow-heartbeat.js" + +const base = new URL("./", import.meta.url) +function selected(file: string, names: string[], listener = false): string { + const text = readFileSync(new URL(file, base), "utf8") + const ast = ts.createSourceFile(file, text, ts.ScriptTarget.ES2022, true) + const parts = names.map(name => { + const found = ast.statements.find(node => ts.isFunctionDeclaration(node) ? node.name?.text === name : ts.isVariableStatement(node) && node.declarationList.declarations.some(d => ts.isIdentifier(d.name) && d.name.text === name)) + assert(found, name); return found.getText(ast) + }) + if (listener) { + const found = ast.statements.find(node => ts.isExpressionStatement(node) && node.getText(ast).startsWith('parentPort?.on("message"')) + assert(found); parts.push(found.getText(ast)) + } + return ts.transpileModule(parts.join("\n"), { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText +} + +test("exact worker sender and ACK listener construct only one frame until matching ACK", () => { + const frames: Array<{ type: string; heartbeatId: string; payload: { packetsReceived: number } }> = [] + let builds = 0, value = 1, receive: (message: unknown) => void = () => { throw Error("Missing listener") } + const run = new Function("deps", ` + const {createFlowHeartbeatSender,getEngineStats,snapshotRings,send,parentPort}=deps; + let bound=true,address='owned',shuttingDown=false; + ${selected("traffic-flow-collector-worker.ts", ["authorizedStartFailed", "failedStartRetirementPending", "heartbeatSender", "heartbeat"], true)} + return {publish:heartbeat,shutdown:()=>{shuttingDown=true}}; + `)({ createFlowHeartbeatSender, getEngineStats: () => ({ packetsReceived: value }), snapshotRings: () => { builds++; return [] }, send: (message: typeof frames[number]) => frames.push(message), parentPort: { on: (_event: string, handler: typeof receive) => { receive = handler } } }) + run.publish(); for (value = 2; value <= 1000; value++) run.publish() + assert.equal(builds, 1); assert.equal(frames.length, 1) + receive({ type: "heartbeat-ack", heartbeatId: "foreign" }); assert.equal(builds, 1) + receive({ type: "heartbeat-ack", heartbeatId: frames[0].heartbeatId }) + assert.equal(builds, 2); assert.equal(frames[1].payload.packetsReceived, 1001) + run.publish(); run.shutdown() + receive({ type: "heartbeat-ack", heartbeatId: frames[1].heartbeatId }); assert.equal(builds, 2) +}) + diff --git a/backend/src/services/traffic-flow-heartbeat.test.ts b/backend/src/services/traffic-flow-heartbeat.test.ts new file mode 100644 index 00000000..e1313e12 --- /dev/null +++ b/backend/src/services/traffic-flow-heartbeat.test.ts @@ -0,0 +1,62 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { MessageChannel } from "node:worker_threads" +import { createFlowHeartbeatSender } from "./traffic-flow-heartbeat.js" + +test("blocked consumer retains one frame; 100000 updates build no additional snapshots", () => { + let value = 0, builds = 0 + const sent: Array<{ id: string; value: number }> = [] + const sender = createFlowHeartbeatSender(() => { builds++; return value }, (id, data) => sent.push({ id, value: data })) + sender.publish() + for (value = 1; value <= 100000; value++) sender.publish() + assert.equal(builds, 1); assert.equal(sent.length, 1) + assert.equal(sender.acknowledge("foreign"), false) + assert.equal(sender.acknowledge(sent[0].id), true) + assert.equal(sent.length, 2); assert.equal(sent[1].value, 100001) + assert.equal(sender.acknowledge(sent[0].id), false) + assert.equal(sender.acknowledge(sent[1].id), true) + assert.equal(sent.length, 2) +}) + +test("snapshot and transport errors release only their own unconfirmed frame", () => { + for (const failure of ["snapshot", "send"]) { + let fail = true, id = "" + const sender = createFlowHeartbeatSender(() => { if (fail && failure === "snapshot") throw Error("snapshot"); return 1 }, (token) => { if (fail && failure === "send") throw Error("send"); id = token }) + assert.throws(() => sender.publish(), new RegExp(failure)) + assert.equal(sender.acknowledge("foreign"), false) + fail = false; sender.publish(); assert(id); assert.equal(sender.acknowledge(id), true) + } +}) + +test("an ACK from a retired sender cannot unblock a new worker incarnation", () => { + let oldId = "", currentId = "", frames = 0 + const old = createFlowHeartbeatSender(() => 1, id => { oldId = id }) + const current = createFlowHeartbeatSender(() => 2, id => { currentId = id; frames++ }) + old.publish(); current.publish(); current.publish() + assert.notEqual(oldId, currentId) + assert.equal(current.acknowledge(oldId), false); assert.equal(frames, 1) + assert.equal(current.acknowledge(currentId), true); assert.equal(frames, 2) +}) + +test("actual MessagePort queues one snapshot and sends latest state after processing ACK", async () => { + const { port1, port2 } = new MessageChannel() + const frames: Array<{ id: string; value: number }> = [] + let builds = 0, value = 1 + const sender = createFlowHeartbeatSender(() => { builds++; return value }, (id, data) => port1.postMessage({ id, value: data })) + const turn = () => new Promise(resolve => setImmediate(resolve)) + const waitFor = async (n: number) => { + const deadline = performance.now() + 3000 + while (frames.length < n) { assert(performance.now() < deadline); await turn() } + } + port2.on("message", frame => frames.push(frame)) + port1.on("message", ({ id }) => sender.acknowledge(id)) + try { + sender.publish() + for (value = 2; value <= 10000; value++) sender.publish() + await waitFor(1); assert.equal(builds, 1); assert.equal(frames.length, 1) + port2.postMessage({ id: frames[0].id }); await waitFor(2) + assert.equal(builds, 2); assert.equal(frames[1].value, 10001) + port2.postMessage({ id: frames[0].id }); await turn(); assert.equal(builds, 2) + port2.postMessage({ id: frames[1].id }); await turn(); assert.equal(builds, 2) + } finally { port1.close(); port2.close() } +}) diff --git a/backend/src/services/traffic-flow-heartbeat.ts b/backend/src/services/traffic-flow-heartbeat.ts new file mode 100644 index 00000000..693e2d4b --- /dev/null +++ b/backend/src/services/traffic-flow-heartbeat.ts @@ -0,0 +1,27 @@ +/** One in-flight snapshot. Coalesce updates without retaining pending payloads. */ +import { randomUUID } from "node:crypto" + +export function createFlowHeartbeatSender(snapshot: () => T, send: (id: string, payload: T) => void) { + let outstanding: string | null = null + let dirty = false + function publish(): void { + if (outstanding !== null) { dirty = true; return } + const id = randomUUID() + outstanding = id + dirty = false + try { send(id, snapshot()) } + catch (error) { + if (outstanding === id) { outstanding = null; dirty = true } + throw error + } + } + return { + publish, + acknowledge(id: string): boolean { + if (outstanding === null || id !== outstanding) return false + outstanding = null + if (dirty) publish() + return true + }, + } +} diff --git a/backend/src/services/traffic-flow-host-agent.ts b/backend/src/services/traffic-flow-host-agent.ts index 4770bd86..4b57350f 100644 --- a/backend/src/services/traffic-flow-host-agent.ts +++ b/backend/src/services/traffic-flow-host-agent.ts @@ -3,13 +3,21 @@ import { parseJsonArray } from "../db/json.js" import { getTrafficFlowSettingsRow, invalidateTrafficFlowSettingsCache } from "./traffic-flow-settings.js" import { agentSocket, callFlowAgent, emptyHostStatus, type FlowAgentSnapshot } from "./traffic-flow-agent-client.js" import { FlowConfigurationQueue } from "./traffic-flow-operation-queue.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { AsyncLocalStorage } from "node:async_hooks" // Session lock: RouterOS/network operations use other pooled connections. Never lock the settings row here. const queue = new FlowConfigurationQueue() -export const withFlowConfigurationLock = (fn: () => Promise) => queue.run(() => withAdvisoryLock(7340291, async () => { - invalidateTrafficFlowSettingsCache() - return fn() -})) +const configurationLease = new AsyncLocalStorage<{ active: boolean }>() +export const withFlowConfigurationLock = (fn: () => Promise): Promise => { + if (configurationLease.getStore()?.active) return fn() + return queue.run(() => withAdvisoryLock(7340291, async () => { + invalidateTrafficFlowSettingsCache() + const lease = { active: true } + try { return await configurationLease.run(lease, fn) } + finally { lease.active = false } + })) +} async function snapshot(): Promise { const row = await getTrafficFlowSettingsRow() @@ -42,5 +50,5 @@ export async function preflightFlowHost() { // Caller holds withFlowConfigurationLock. This is also used after saving a newly discovered router key. export async function reconcileFlowHost() { if (!agentSocket()) return emptyHostStatus(false) - return callFlowAgent("/apply", await snapshot()) + return executeGuardedApplicationWrite({ kind: "netflow-host", id: "1" }, "all", async () => callFlowAgent("/apply", await snapshot())) } diff --git a/backend/src/services/traffic-flow-host-file-builders.ts b/backend/src/services/traffic-flow-host-file-builders.ts index f7600000..f6d76a5a 100644 --- a/backend/src/services/traffic-flow-host-file-builders.ts +++ b/backend/src/services/traffic-flow-host-file-builders.ts @@ -30,7 +30,7 @@ function buildHostWgQuickConf(row: HostExportSnapshot): string { endpoint: p.endpoint, persistentKeepalive: p.endpoint ? 25 : undefined, })), - }) + }, { includePrivateKey: true }) } function buildHostComposeOverride(row: HostExportSnapshot): string { diff --git a/backend/src/services/traffic-flow-host-files.ts b/backend/src/services/traffic-flow-host-files.ts index 3a0909d9..3e8cc5e1 100644 --- a/backend/src/services/traffic-flow-host-files.ts +++ b/backend/src/services/traffic-flow-host-files.ts @@ -6,10 +6,14 @@ import { buildFlowAgentInstaller } from "./traffic-flow-agent-install.js" export async function listTrafficFlowHostFiles() { const row = await getTrafficFlowSettingsRow() - const files = buildTrafficFlowHostFiles({ ...row, peers: parseJsonArray(row.peersJson) }) + return buildTrafficFlowHostFilesBundle({ ...row, peers: parseJsonArray(row.peersJson) }) +} + +export async function buildTrafficFlowHostFilesBundle(snapshot: { flowListenPort: number; [key: string]: unknown }) { + const files = buildTrafficFlowHostFiles(snapshot) // The same source is used by a repository install and by the downloadable self-contained installer. const installer = await readFile(new URL("../../../deploy/netflow-agent/install.sh", import.meta.url), "utf8") const conf = files.find(file => file.id === "wg-quick")!.code - const code = buildFlowAgentInstaller(conf, row.flowListenPort, installer) + const code = buildFlowAgentInstaller(conf, snapshot.flowListenPort, installer) return [{ id: "agent", label: "Агент Docker", filename: "install-netflow-agent.sh", code }, ...files] } diff --git a/backend/src/services/traffic-flow-host-keys-service.ts b/backend/src/services/traffic-flow-host-keys-service.ts new file mode 100644 index 00000000..9d01dc5a --- /dev/null +++ b/backend/src/services/traffic-flow-host-keys-service.ts @@ -0,0 +1,55 @@ +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import { privateSecretFingerprint, fingerprint } from "../modules/mcp/security.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { withApplicationMutationLock } from "./config-mutation-lock.js" +import { withFlowConfigurationLock } from "./traffic-flow-host-agent.js" +import { invalidateTrafficFlowSettingsCache } from "./traffic-flow-settings.js" +import { generateWireGuardKeyPair } from "./wg-keys.js" +import { createFlowSettingsStore } from "./traffic-flow-settings-store.js" +import { createFlowSettingsExecution, type FlowSettingsPlan } from "./traffic-flow-settings-execution.js" + +export type FlowHostKeysReceipt = { + ok: true; receiptId: string; created: boolean; publicKey: string; + afterProof: string; privateMaterialExported: false; listenerRestarted: false; automaticReplay: false; +} +export const flowSettingsPrivateProof = (value: unknown) => privateSecretFingerprint(fingerprint(value), "flow-settings-private-v1") +export async function readFlowHostKeysConfiguration(authorize: () => Promise = async () => {}) { + return createFlowSettingsStore(pool, authorize, invalidateTrafficFlowSettingsCache).read() +} +/** Shared by REST, overlay setup and MCP. Includes only database key installation; + * never starts a collector, applies a host agent, exports a private key or rotates + * an already complete pair. All participating settings writers use the same lock. */ +export async function generateFlowHostKeys(control: { + authorize?: () => Promise; expected?: FlowSettingsPlan; receiptId?: string; +} = {}): Promise { + const authorize = control.authorize ?? (async () => {}) + await authorize() + return withApplicationMutationLock(() => withFlowConfigurationLock(() => executeGuardedApplicationWrite({ kind: "netflow-settings", id: "1" }, "all", async () => { + const store = createFlowSettingsStore(pool, authorize, invalidateTrafficFlowSettingsCache) + const receiptId = control.receiptId ?? randomUUID() + const engine = createFlowSettingsExecution({ + authorize: async () => { await authorize() }, exclusive: async work => work(), readConfiguration: () => store.read(), + intent: async phase => store.claim(receiptId, phase, proof), + compareAndSet: (before, after) => store.compareAndSet(receiptId, proof, before, after), + generateKeys: generateWireGuardKeyPair, + startListener: async () => { throw Error("Host key generation never starts a collector") }, + }) + const plan = control.expected ?? await engine.prepare({ kind: "ensure-keys" }) + if (plan.command.kind !== "ensure-keys") throw Error("Expected NetFlow key generation command") + const proof = await flowSettingsPrivateProof(plan) + const result = await engine.execute(plan) + if (result.status !== "applied" || !result.configurationConfirmed || typeof result.publicKey !== "string" || typeof result.created !== "boolean") { + throw Object.assign(new Error("Создание ключей NetFlow не подтверждено; повтор запрещён"), { code: "MUTATION_OUTCOME_UNKNOWN", statusCode: 503 }) + } + // Even a no-op has a unique durable completion; the same reviewed receipt + // cannot become a later key rotation if the row is externally cleared. + if (result.attempts === 0) await store.claim(receiptId, "configuration", proof) + const afterProof = await flowSettingsPrivateProof(await store.read()) + const receipt: FlowHostKeysReceipt = { ok: true, receiptId, created: result.created, publicKey: result.publicKey, afterProof, + privateMaterialExported: false, listenerRestarted: false, automaticReplay: false } + try { await store.complete(receiptId, "configuration", proof, receipt); await authorize() } + catch { throw Object.assign(new Error("Ключи сохранены, но квитанция не подтверждена; повтор запрещён"), { code: "MUTATION_OUTCOME_UNKNOWN", statusCode: 503 }) } + return receipt + }))) +} diff --git a/backend/src/services/traffic-flow-ifaces.test.ts b/backend/src/services/traffic-flow-ifaces.test.ts index 34786912..c0cedb6d 100644 --- a/backend/src/services/traffic-flow-ifaces.test.ts +++ b/backend/src/services/traffic-flow-ifaces.test.ts @@ -1,5 +1,10 @@ import assert from "node:assert/strict" +import { test } from "node:test" +import { db } from "../db/index.js" +import { MikrotikClient } from "./mikrotik.js" +import { refreshServerIfaces } from "./traffic-flow-ifaces.js" import { + bindingIfaceAliases, rememberServerIfaces, resetIfaceCacheForTests, resolveIfaceName, @@ -38,6 +43,17 @@ assert.equal(rosIdToIfIndex("*12"), 18) assert.equal(resolveIfaceName(8, "10").name, "gre1") assert.equal(resolveIfaceName(8, "18").name, "gre1") +const collidingIndexes = [ + { ifindex: "10", ".id": "*12", name: "gre1" }, + { ".id": "*A", name: "ether1" }, +] +for (const rows of [collidingIndexes, [...collidingIndexes].reverse()]) { + rememberServerIfaces(8, rows) + assert.equal(resolveIfaceName(8, "10").name, "gre1") + assert.equal(resolveIfaceName(8, "18").name, "gre1") + assert.ok(!bindingIfaceAliases(8, "ether1").includes("10")) +} + resetIfaceCacheForTests() assert.equal(shouldRefreshIfaces(9), true) rememberServerIfaces(9, [{ ".id": "*2", name: "ether1" }]) @@ -60,3 +76,26 @@ assert.equal(flowRowMatchesFilter({ serverId: 7, inIface: "2" }, "ether1", { ifa assert.equal(flowRowMatchesFilter({ serverId: 8, inIface: "2" }, "ether1", { serverId: 7 }, null), false) console.log("traffic-flow-ifaces.test.ts: ok") + +test("NetFlow interface discovery only opens native connections for enabled servers", async (t) => { + let rows: Array<{ enabled: boolean }> = [] + t.mock.method(db, "select", () => ({ from: () => ({ where: () => ({ limit: async () => rows }) }) })) + const nativeClient = t.mock.method(MikrotikClient, "fromServer", () => ({ + get: async (path: string) => { + assert.equal(path, "/interface") + return [{ ifindex: "10", name: "wan" }] + }, + })) + + await refreshServerIfaces(40) + rows = [{ enabled: false }] + rememberServerIfaces(41, [{ ifindex: "10", name: "historical" }]) + await refreshServerIfaces(41, true) + assert.equal(nativeClient.mock.callCount(), 0) + assert.equal(resolveIfaceName(41, "10").name, "historical") + + rows = [{ enabled: true }] + await refreshServerIfaces(42) + assert.equal(nativeClient.mock.callCount(), 1) + assert.equal(resolveIfaceName(42, "10").name, "wan") +}) diff --git a/backend/src/services/traffic-flow-ifaces.ts b/backend/src/services/traffic-flow-ifaces.ts index 823afda9..3ed4a56e 100644 --- a/backend/src/services/traffic-flow-ifaces.ts +++ b/backend/src/services/traffic-flow-ifaces.ts @@ -30,7 +30,7 @@ async function refreshServerIfacesInner(serverId: number, force = false): Promis inflight.add(serverId) try { const row = (await db.select().from(servers).where(eq(servers.id, serverId)).limit(1))[0] - if (!row) return + if (!row?.enabled) return const client = MikrotikClient.fromServer(row) const ifaces = await client.get("/interface") rememberServerIfaces(serverId, Array.isArray(ifaces) ? ifaces : []) diff --git a/backend/src/services/traffic-flow-ifindex.ts b/backend/src/services/traffic-flow-ifindex.ts index 1a3d1561..9c1f6d31 100644 --- a/backend/src/services/traffic-flow-ifindex.ts +++ b/backend/src/services/traffic-flow-ifindex.ts @@ -10,7 +10,7 @@ const lastAttempt = new Map() export const IFACE_CACHE_TTL_MS = 60_000 -/** RouterOS `.id` (`*A`) → SNMP ifIndex (10). */ +/** Decode a RouterOS `.id` (`*A`) as a legacy ifIndex alias (10). */ export function rosIdToIfIndex(id: string | undefined | null): number | null { if (!id) return null const raw = String(id).trim() @@ -22,14 +22,16 @@ export function rosIdToIfIndex(id: string | undefined | null): number | null { export function rememberServerIfaces(serverId: number, rows: RosIfaceIndexRow[]): void { const map = new Map() + const explicitIndexes = new Map() for (const row of rows) { const name = String(row.name ?? "").trim() if (!name) continue const fromProp = Number.parseInt(String(row.ifindex ?? ""), 10) const fromId = rosIdToIfIndex(row[".id"]) - if (Number.isFinite(fromProp) && fromProp > 0) map.set(fromProp, name) + if (Number.isFinite(fromProp) && fromProp > 0) explicitIndexes.set(fromProp, name) if (fromId != null && fromId > 0) map.set(fromId, name) } + for (const [index, name] of explicitIndexes) map.set(index, name) cache.set(serverId, map) fetchedAt.set(serverId, Date.now()) } @@ -53,6 +55,16 @@ export function canonicalFactIface(serverId: number, inIface: string): string { return name || trimmed } +/** Frozen read-only resolver; does not request RouterOS or refresh any cache. */ +export function captureFactIfaceResolver(serverIds:readonly number[]) { + const captured=new Map(serverIds.map(id=>[id,new Map(cache.get(id)??[])])) + return {entries:[...captured].map(([id,entries])=>[id,[...entries]]),resolve:(serverId:number,inIface:string)=>{ + const trimmed=String(inIface??"").trim() + if(!trimmed||!/^\d+$/.test(trimmed))return trimmed + return captured.get(serverId)?.get(Number(trimmed))||trimmed + }} +} + function numericIfaceIndex(iface: string): string | null { const raw = String(iface ?? "").trim() if (/^\d+$/.test(raw)) return raw diff --git a/backend/src/services/traffic-flow-ingest-quality.test.ts b/backend/src/services/traffic-flow-ingest-quality.test.ts new file mode 100644 index 00000000..2b8ee562 --- /dev/null +++ b/backend/src/services/traffic-flow-ingest-quality.test.ts @@ -0,0 +1,36 @@ +import assert from "node:assert/strict" +import {test} from "node:test" +import {createFlowIngestQuality} from "./traffic-flow-ingest-quality.js" +test("quantified loss delta is immutable across new arrivals and detached batch retry",()=>{ + const quality=createFlowIngestQuality() + quality.reject(100,3,"aggregate-capacity","2026-10-29T10:00:00.000Z") + const captured=quality.detachDelta() + quality.reject(50,2,"ring-capacity","2026-10-29T10:01:00.000Z") + assert.equal(captured.bytes,"100");assert.equal(captured.packets,"3") + assert.equal(quality.snapshot().bytes,"150");assert.equal(quality.detachDelta().bytes,"50") + assert.equal(captured.localAdmission,"partial");assert.equal(captured.exportCoverage,"unverified") + assert(Object.isFrozen(captured));assert(Object.isFrozen(captured.reasons)) +}) +test("unsafe record counters are unquantified; rounded values never become exact BigInt loss",()=>{ + const quality=createFlowIngestQuality() + quality.reject(Number.MAX_SAFE_INTEGER+1,Number.NaN,"unsafe-numeric") + const value=quality.snapshot() + assert.equal(value.records,"1");assert.equal(value.bytes,null);assert.equal(value.packets,null) + assert.equal(value.localAdmission,"loss-quantity-unknown") + assert.equal(value.knownLowerBounds.bytes,"0","zero is only an explicit lower bound, never the reported lost quantity") +}) +test("fixed U64 counters saturate explicitly without unbounded BigInt/string growth",()=>{ + const quality=createFlowIngestQuality() + for(let i=0;i<2049;i++)quality.reject(Number.MAX_SAFE_INTEGER,1,"unsafe-numeric") + const value=quality.snapshot() + assert.equal(value.bytes,null);assert.equal(value.counterOverflow,true) + assert.equal(value.knownLowerBounds.bytes,"18446744073709551615") + assert.equal(value.packets,"2049");assert(value.knownLowerBounds.bytes.length<=20) +}) +test("invalid loss interval becomes unknown and reset declares a new collector epoch",()=>{ + const quality=createFlowIngestQuality(),epoch=quality.snapshot().collectorEpoch + quality.reject(1,1,"key-width","x".repeat(100000)) + assert.equal(quality.snapshot().firstAt,null);assert.equal(quality.snapshot().intervalUnknown,true) + quality.reset();assert.notEqual(quality.snapshot().collectorEpoch,epoch) + assert.equal(quality.snapshot().localAdmission,"no-local-loss-observed") +}) diff --git a/backend/src/services/traffic-flow-ingest-quality.ts b/backend/src/services/traffic-flow-ingest-quality.ts new file mode 100644 index 00000000..20ada9dd --- /dev/null +++ b/backend/src/services/traffic-flow-ingest-quality.ts @@ -0,0 +1,51 @@ +/** Fixed-width local decoded-record admission accounting. This does not prove + * exporter/UDP coverage, and unknown quantities are never reported as zero. */ +import {randomUUID} from "node:crypto" +const U64_MAX = (1n << 64n) - 1n +type LossReason = "aggregate-capacity" | "ring-capacity" | "key-width" | "unsafe-numeric" +interface Counters { + records: bigint; bytes: bigint; packets: bigint + recordsOverflow: boolean; bytesUnknown: boolean; packetsUnknown: boolean; bytesOverflow:boolean;packetsOverflow:boolean;intervalUnknown:boolean + firstAt: string | null; lastAt: string | null + reasons: Record +} +const fresh = (): Counters => ({records:0n,bytes:0n,packets:0n,recordsOverflow:false,bytesUnknown:false,packetsUnknown:false,bytesOverflow:false,packetsOverflow:false,intervalUnknown:false, + firstAt:null,lastAt:null,reasons:{"aggregate-capacity":false,"ring-capacity":false,"key-width":false,"unsafe-numeric":false}}) +const valid = (value:number) => Number.isSafeInteger(value) && value >= 0 +function increment(value:bigint, amount:bigint): {value:bigint;overflow:boolean} { + const next=value+amount + return {value:next>U64_MAX?U64_MAX:next,overflow:next>U64_MAX} +} +function record(target:Counters,bytes:number,packets:number,reason:LossReason,at:string):void { + const records=increment(target.records,1n);target.records=records.value;target.recordsOverflow ||= records.overflow + if(valid(bytes)){const next=increment(target.bytes,BigInt(bytes));target.bytes=next.value;target.bytesOverflow ||= next.overflow}else target.bytesUnknown=true + if(valid(packets)){const next=increment(target.packets,BigInt(packets));target.packets=next.value;target.packetsOverflow ||= next.overflow}else target.packetsUnknown=true + if(at.length!==24||!Number.isFinite(Date.parse(at))||new Date(at).toISOString()!==at)target.intervalUnknown=true + else {if(target.firstAt===null||attarget.lastAt)target.lastAt=at} + target.reasons[reason]=true +} +function view(target:Counters,collectorEpoch:string) { + const lost=target.records>0n,unknown=target.recordsOverflow||target.bytesUnknown||target.packetsUnknown||target.bytesOverflow||target.packetsOverflow + return Object.freeze({version:1 as const,collectorEpoch,scope:"collector-global" as const,exportCoverage:"unverified" as const, + localAdmission:unknown?"loss-quantity-unknown" as const:lost?"partial" as const:"no-local-loss-observed" as const, + records:target.recordsOverflow?null:target.records.toString(),bytes:target.bytesUnknown||target.bytesOverflow?null:target.bytes.toString(),packets:target.packetsUnknown||target.packetsOverflow?null:target.packets.toString(), + knownLowerBounds:Object.freeze({records:target.records.toString(),bytes:target.bytes.toString(),packets:target.packets.toString()}), + firstAt:target.intervalUnknown?null:target.firstAt,lastAt:target.intervalUnknown?null:target.lastAt,intervalUnknown:target.intervalUnknown,reasons:Object.freeze({...target.reasons}),counterOverflow:target.recordsOverflow||target.bytesOverflow||target.packetsOverflow, + rawRowsPolicy:"topN-and-pending-cap-not-complete" as const,lifetimeScope:"current-collector-isolate" as const}) +} +export type FlowIngestQuality = ReturnType +export function createFlowIngestQuality() { + let lifetime=fresh(),delta=fresh(),collectorEpoch=randomUUID() + return { + reject(bytes:number,packets:number,reason:LossReason,at=new Date().toISOString()):void { + if(!Object.hasOwn(lifetime.reasons,reason))throw Error("Invalid flow loss reason") + record(lifetime,bytes,packets,reason,at);record(delta,bytes,packets,reason,at) + }, + snapshot:():FlowIngestQuality=>view(lifetime,collectorEpoch), + hasDelta:():boolean=>delta.records>0n, + // No retained references point at the new live delta. The returned frozen + // object belongs to the existing source-proven SQL-owned engine batch. + detachDelta:():FlowIngestQuality=>{const result=view(delta,collectorEpoch);delta=fresh();return result}, + reset():void {lifetime=fresh();delta=fresh();collectorEpoch=randomUUID()}, + } +} diff --git a/backend/src/services/traffic-flow-ingest.test.ts b/backend/src/services/traffic-flow-ingest.test.ts index c620384d..6de51007 100644 --- a/backend/src/services/traffic-flow-ingest.test.ts +++ b/backend/src/services/traffic-flow-ingest.test.ts @@ -26,6 +26,13 @@ import { configureEngine, droppedForTests, getEngineStats, isValidFlowInet, pend import { dbQuery } from "../db/index.js" import { withPgOrSkip } from "../test/pg.js" +// Even an all-invalid batch now owns a receipt/settings transaction. Initialize +// its real schema before the first flush, rather than relying on swallowed SQL. +if (!(await withPgOrSkip())) { + console.log("traffic-flow-ingest.test.ts: skip (PostgreSQL unavailable)") + process.exit(0) +} + resetIfaceCacheForTests() resetFlowRingsForTests() @@ -78,7 +85,8 @@ assert.equal(isValidFlowInet("not-an-ip"), false) assert.equal(isValidFlowInet("999.1.1.1"), false) resetFlowRingsForTests() -ingestParsedFlowsForServerForTests(1, [{ +await dbQuery("INSERT INTO servers(id,name,host) VALUES(91008,'invalid-flow-fixture','127.0.0.1')") +ingestParsedFlowsForServerForTests(91008, [{ src: "not-an-ip", dst: "8.8.8.8", proto: 6, @@ -91,6 +99,7 @@ ingestParsedFlowsForServerForTests(1, [{ }]) await flushPendingForTests() assert.match(getEngineStats().lastError, /невалидн/) +await dbQuery("DELETE FROM servers WHERE id=91008") resetFlowRingsForTests() configureEngine({ topN: 20 }) @@ -134,6 +143,7 @@ applyHeartbeatForTests({ dropped: 0, rowsStored: 1, workerAlive: true, + dataQuality: null, rings: [], }) assert.equal(getFlowListenerState().bound, true) diff --git a/backend/src/services/traffic-flow-ingest.ts b/backend/src/services/traffic-flow-ingest.ts index 8919d44d..e6fe1a97 100644 --- a/backend/src/services/traffic-flow-ingest.ts +++ b/backend/src/services/traffic-flow-ingest.ts @@ -1,17 +1,27 @@ -import { deleteTrafficFlowData, executeTrafficFlowPurge } from "./traffic-flow-purge-execution.js" +import { flowReadBucketSnapshot } from "./traffic-flow-read-policy.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" +import { readFlowSourceLimits } from "./traffic-flow-source-limits.js" +import { deleteTrafficFlowData, executeTrafficFlowPurge, type FlowPurgeOptions } from "./traffic-flow-purge-execution.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" import { withTrafficFlowPurgeLock } from "../db/operation-lock.js" import { Worker } from "node:worker_threads" -import { waitForCollectorShutdown } from "./traffic-flow-worker-shutdown.js" -import { db, dbAll, pool } from "../db/index.js" +import { FlowWorkerLifecycle } from "./traffic-flow-worker-lifecycle.js" +import { requestExistingWorkerDrain } from "./traffic-flow-drain-protocol.js" +import { withFlowWriterExclusion } from "./traffic-flow-writer-exclusion.js" +import { db, pool } from "../db/index.js" import { dropExpiredPartitions } from "../db/partitions.js" import { servers } from "../db/schema.js" -import type { FlowPurgeDto, FlowStatsDto, FlowTalkerDto } from "@mmapp/contracts/traffic-flow" -import { protoName, type ParsedFlowInput } from "./traffic-flow-parse.js" +import type { FlowPurgeDto, FlowStatsDto } from "@mmapp/contracts/traffic-flow" +import type { ParsedFlowInput } from "./traffic-flow-parse.js" import type { CollectorHeartbeat, ExporterMapPayload, MainToWorker, WorkerToMain } from "./traffic-flow-collector-ipc.js" import { applyRingSnapshot, + markFlowRingSnapshotUnavailable, configureEngine, flushPending, + strictDrainEngine, getEngineStats, getRingMbps as engineGetRingMbps, ingestParsedFlowsForServerForTests as engineIngestForServer, @@ -30,7 +40,7 @@ import { listHostPeers, invalidateTrafficFlowSettingsCache, } from "./traffic-flow-settings.js" -import { applicationName } from "./traffic-flow-apps.js" +import { projectFlowTalkers } from "./traffic-flow-talkers-model.js" import { resolveIfaceName } from "./traffic-flow-ifaces.js" import { canonicalIp } from "./traffic-flow-ip.js" import { getServerCatalog } from "./traffic-flow-topology.js" @@ -51,9 +61,13 @@ export interface FlowWorkerHealth { } let worker: Worker | null = null +const workerLifecycle=new FlowWorkerLifecycle() +let listenerGeneration=0 let restartTimer: ReturnType | null = null let restartAttempts = 0 let lastHeartbeat: CollectorHeartbeat | null = null +let heartbeatFailure:Error|null=null +const HEARTBEAT_UNAVAILABLE="NetFlow: недостоверный снимок коллектора; наблюдения недоступны" let state: FlowListenerState = { bound: false, address: null } let wantListen = false let databaseStop: Promise | null = null @@ -101,12 +115,37 @@ function postToWorker(msg: MainToWorker): void { worker?.postMessage(msg) } +function applyWorkerHeartbeat(payload: CollectorHeartbeat): void { + const boundedString=(value:unknown,max:number)=>value===null||typeof value==="string"&&value.length<=max + const counter=(value:unknown)=>typeof value==="number"&&Number.isSafeInteger(value)&&value>=0 + if(!payload||typeof payload!=="object"||typeof payload.bound!=="boolean"||typeof payload.workerAlive!=="boolean" + ||!boundedString(payload.address,1024)||!boundedString(payload.lastExporterIp,64) + ||typeof payload.lastError!=="string"||payload.lastError.length>4096 + ||![payload.packetsReceived,payload.pendingSize,payload.dropped,payload.rowsStored].every(counter) + ||!(payload.lastDatagramAt===null||typeof payload.lastDatagramAt==="string"&&payload.lastDatagramAt.length===24&&Number.isFinite(Date.parse(payload.lastDatagramAt))&&new Date(payload.lastDatagramAt).toISOString()===payload.lastDatagramAt))throw Error("Invalid NetFlow heartbeat metadata") + const dataQuality=payload.dataQuality==null?null:flowIngestQualitySchema.parse(payload.dataQuality) + applyRingSnapshot(payload.rings,dataQuality) + lastHeartbeat = {...payload,dataQuality,rings:[]} + heartbeatFailure=null + state = { bound: payload.bound, address: payload.address } + restartAttempts = 0 +} + +function rejectWorkerHeartbeat(error:unknown):void { + heartbeatFailure??=error instanceof Error?error:new Error("Invalid NetFlow heartbeat") + markFlowRingSnapshotUnavailable() + state={bound:false,address:null} + if(lastHeartbeat)lastHeartbeat={...lastHeartbeat,bound:false,address:null,workerAlive:false,dataQuality:null,rings:[],lastError:HEARTBEAT_UNAVAILABLE} +} + function handleWorkerMessage(msg: WorkerToMain): void { if (msg.type === "heartbeat") { - lastHeartbeat = msg.payload - state = { bound: msg.payload.bound, address: msg.payload.address } - applyRingSnapshot(msg.payload.rings) - restartAttempts = 0 + try { applyWorkerHeartbeat(msg.payload) } + catch(error){rejectWorkerHeartbeat(error)} + finally { + try{postToWorker({ type:"heartbeat-ack",heartbeatId:msg.heartbeatId })} + catch(error){rejectWorkerHeartbeat(error)} + } return } if (msg.type === "error") { @@ -116,18 +155,43 @@ function handleWorkerMessage(msg: WorkerToMain): void { } } -async function spawnWorker(): Promise { - if (databaseStop) return - stopWorkerProcess() +async function spawnWorker(authorize?: FlowLifecycleAuthorization): Promise { + if (databaseStop || !wantListen) return + const generation=listenerGeneration + const current = () => !databaseStop && generation === listenerGeneration && wantListen + const fence = () => flowLifecycleBoundary(authorize, current) + if (!await fence()) return + if (authorize && worker) throw Error("Concurrent NetFlow worker prevents a bounded startup") + stopWorkerProcess(authorize) + if (!await fence()) return + await workerLifecycle.drain() + if (!await fence()) return const settings = await getTrafficFlowSettingsRow() - if (databaseStop) return + if (!await fence()) return configureEngine({ topN: settings.topN, retentionHours: settings.retentionHours }) - await applyExporterCtxFromDb() const exporterMap = await buildExporterMapPayload() - if (databaseStop) return - const w = new Worker(trafficFlowWorkerFileUrl(), { execArgv: process.execArgv }) - w.on("message", (msg: WorkerToMain) => handleWorkerMessage(msg)) + if (!await fence()) return + setExporterResolveCtx({ overlayPrefix: exporterMap.overlayPrefix, byTunnelIp: new Map(exporterMap.byTunnelIp), peers: exporterMap.peers, hostIps: new Map(exporterMap.hostIps) }) + // An ordinary start may retire a worker installed during an earlier await. + // A gated start refuses that concurrent pointer to preserve its write budget. + while(true){ + if (!await fence()) return + // Check the pointer after the asynchronous fence, then install without an + // intervening await so concurrent starts cannot orphan another SQL writer. + if (!worker) break + if (authorize) throw Error("Concurrent NetFlow worker prevents a bounded startup") + stopWorkerProcess(authorize) + if (!await fence()) return + await workerLifecycle.drain() + if (!await fence()) return + } + let startupPending = Boolean(authorize) + // Let Node inherit supported parent options. Explicit execArgv rejects V8 + // heap flags that are valid on the parent CLI; the bootstrap owns TS loading. + const w = new Worker(trafficFlowWorkerFileUrl()) + w.on("message", (msg: WorkerToMain) => {if(worker===w)handleWorkerMessage(msg)}) w.on("error", (err) => { + if(worker!==w)return state = { bound: false, address: null } lastHeartbeat = lastHeartbeat ? { ...lastHeartbeat, workerAlive: false, lastError: err.message, bound: false } @@ -140,6 +204,7 @@ async function spawnWorker(): Promise { lastDatagramAt: null, pendingSize: 0, dropped: 0, + dataQuality:null, rowsStored: 0, workerAlive: false, rings: [], @@ -149,7 +214,7 @@ async function spawnWorker(): Promise { if (worker !== w) return worker = null state = { bound: false, address: null } - if (!wantListen) return + if (!wantListen || startupPending) return const delay = Math.min(30_000, 1000 * 2 ** restartAttempts) restartAttempts += 1 restartTimer = setTimeout(() => { @@ -159,56 +224,40 @@ async function spawnWorker(): Promise { }) worker = w const host = process.env.FLOW_LISTEN_HOST?.trim() || settings.collectorIp || "127.0.0.1" - postToWorker({ - type: "start", - payload: { - listenHost: host, - listenPort: settings.flowListenPort, - topN: settings.topN, - retentionHours: settings.retentionHours, - exporterMap, - }, - }) + const payload = { + listenHost: host, + listenPort: settings.flowListenPort, + topN: settings.topN, + retentionHours: settings.retentionHours, + exporterMap, + } + if (authorize) { + await requestExistingWorkerDrain(w, async () => { if (!await fence()) throw Error("NetFlow startup superseded") }, [], 30000, "authorized-start", payload) + if (!await fence() || worker !== w) throw Error("NetFlow startup superseded") + startupPending = false + } else postToWorker({ type: "start", payload }) } -function stopWorkerProcess(): void { +function stopWorkerProcess(authorize?: FlowLifecycleAuthorization): void { if (restartTimer) { clearTimeout(restartTimer) restartTimer = null } if (worker) { - try { - postToWorker({ type: "stop" }) - void worker.terminate() - } catch { - /* ignore */ - } + const current=worker + // Register the acknowledged retirement before detaching the active pointer. + // The sync public stop returns immediately; starts and DB operations drain it. + void workerLifecycle.retire(current,authorize) worker = null } } -async function stopWorkerProcessAsync(): Promise { - if (restartTimer) { - clearTimeout(restartTimer) - restartTimer = null - } - if (!worker) return - const current = worker - worker = null - try { - current.postMessage({ type: "stop" }) - await current.terminate() - } catch { - /* ignore */ - } -} - export function reattachFlowSqlite(): void { /* no-op: engine uses PostgreSQL pool */ } export function applyHeartbeatForTests(payload: CollectorHeartbeat): void { - handleWorkerMessage({ type: "heartbeat", payload }) + applyWorkerHeartbeat(payload) } export function simulateWorkerExitForTests(): number { @@ -232,7 +281,7 @@ export function getFlowWorkerHealth(): FlowWorkerHealth { const hb = lastHeartbeat const mem = getEngineStats() return { - alive: Boolean(worker) && (hb?.workerAlive ?? false), + alive: heartbeatFailure===null&&Boolean(worker) && (hb?.workerAlive ?? false), bound: state.bound, pendingSize: hb?.pendingSize ?? mem.pendingSize, dropped: hb?.dropped ?? mem.dropped, @@ -246,36 +295,69 @@ export async function getFlowRuntimeCounters() { return { packetsReceived: hb.packetsReceived, lastExporterIp: hb.lastExporterIp ?? null, - lastError: hb.lastError || null, + lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:hb.lastError || null, lastDatagramAt: hb.lastDatagramAt ?? null, dropped: hb.dropped ?? 0, + dataQuality:hb.dataQuality??null, } } const settings = await getTrafficFlowSettingsRow() return { packetsReceived: settings.packetsReceived, lastExporterIp: settings.lastExporterIp ?? null, - lastError: settings.lastError || null, + lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:settings.lastError || null, lastDatagramAt: settings.lastDatagramAt ?? null, dropped: 0, + dataQuality:null, } } -export async function startTrafficFlowListener() { - if (databaseStop) throw new Error("Приём NetFlow приостановлен для операции с базой") - stopTrafficFlowListener() - const settings = await getTrafficFlowSettingsRow() - if (databaseStop) throw new Error("Приём NetFlow приостановлен для операции с базой") - if (!settings.enabled) { +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 + // keeps its existing fire-and-forget behavior for ungated callers. + const initialGeneration = listenerGeneration + if (authorize && !await flowLifecycleBoundary(authorize, () => listenerGeneration === initialGeneration && !databaseStop)) return + if (authorize) { + listenerGeneration++ wantListen = false + stopWorkerProcess(async () => { + await flowLifecycleBoundary(authorize, () => listenerGeneration === initialGeneration + 1 && !databaseStop) + }) state = { bound: false, address: null } - return + } else stopTrafficFlowListener() + const generation = listenerGeneration + const current = () => generation === listenerGeneration && !databaseStop + const fence = () => flowLifecycleBoundary(authorize, current) + const authorizeCurrent = authorize ? async () => { + if (!await fence()) throw Error("NetFlow listener lifecycle changed during authorization") + } : undefined + try { + if (!await fence()) return + await workerLifecycle.drain() + if (!await fence()) return + if (authorize) { + await flushPending({ force: true }, authorizeCurrent) + if (!await fence()) return + } + const settings = await getTrafficFlowSettingsRow() + if (!await fence()) return + if (!settings.enabled) { + wantListen = false + state = { bound: false, address: null } + return + } + wantListen = true + await spawnWorker(authorizeCurrent) + } catch (error) { + // A rejected/unknown transition never schedules an automatic replay. + if (generation === listenerGeneration) wantListen = false + throw error } - wantListen = true - await spawnWorker() } export function stopTrafficFlowListener() { + listenerGeneration++ wantListen = false stopWorkerProcess() void flushPending({ force: true }).catch(() => { /* ignore */ }) @@ -285,14 +367,15 @@ export function stopTrafficFlowListener() { export async function stopTrafficFlowListenerForDatabaseOperation(): Promise { if (databaseStop) return databaseStop wantListen = false + listenerGeneration++ if (restartTimer) clearTimeout(restartTimer) restartTimer = null - const current = worker + stopWorkerProcess() databaseStop = (async () => { - if (current) await waitForCollectorShutdown(current) + await workerLifecycle.drain() await flushPending({ force: true, prune: false }) state = { bound: false, address: null } - lastHeartbeat = null + lastHeartbeat = null;heartbeatFailure=null })() // Keep a failed stop latched: restart must not kill a worker whose writes are unconfirmed. await databaseStop @@ -300,7 +383,13 @@ export async function stopTrafficFlowListenerForDatabaseOperation(): Promise { if (databaseStop) await databaseStop + const settings = await getTrafficFlowSettingsRow() databaseStop = null + if (!settings.enabled) { + wantListen = false + state = { bound: false, address: null } + return + } await startTrafficFlowListener() } @@ -386,10 +475,15 @@ interface StoredBucketRow { nat_dst_port: number | null } -export async function listStoredFlowRows(sinceIso: string): Promise { - const settings = await getTrafficFlowSettingsRow() +export async function listStoredFlowRows(sinceIso: string, serverId?: number, options?: { maxRows?: number; untilIso?: string }): Promise { + 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 stored = await dbAll(` + const parameters: Array = serverId === undefined ? [sinceIso, keep] : [sinceIso, keep, serverId] + const untilParameter = options?.untilIso ? parameters.push(options.untilIso) : undefined + if (options?.maxRows !== undefined) parameters.push(options.maxRows + 1) + const stored = await flowReadBucketSnapshot(` SELECT server_id, bucket_at, COALESCE(host(src), '') AS src, COALESCE(host(dst), '') AS dst, proto, src_port, dst_port, bytes, packets, in_iface, out_iface, @@ -401,10 +495,12 @@ export async function listStoredFlowRows(sinceIso: string): Promise= $1 + WHERE bucket_at >= $1 ${serverId === undefined ? "" : "AND server_id=$3"} ${untilParameter ? `AND bucket_at <= $${untilParameter}` : ""} ) ranked WHERE rn <= $2 - `, [sinceIso, keep]) + ${options?.maxRows !== undefined ? `LIMIT $${parameters.length}` : ""} + `, parameters) + if (options?.maxRows !== undefined && stored.length > options.maxRows) throw new Error("Превышен предел flow rows; сократите окно или число серверов") const merged = new Map() for (const r of stored) { mergeInto(merged, { @@ -415,8 +511,8 @@ export async function listStoredFlowRows(sinceIso: string): Promise options.untilIso || serverId !== undefined && p.serverId !== serverId) continue + flowCounter(p.bytes); flowCounter(p.packets) mergeInto(merged, p) } } - return capFlowRowsPerServerBucket([...merged.values()], keep) + const result = capFlowRowsPerServerBucket([...merged.values()], keep) + if (options?.maxRows !== undefined && result.length > options.maxRows) throw new Error("Превышен предел flow rows с pending; сократите окно или число серверов") + return result } -export async function listFlowRowsForWindow(minutes: number): Promise { +export async function listFlowRowsForWindow(minutes: number, serverId?: number, options?: { maxRows?: number; untilIso?: string }): Promise { const sinceIso = new Date(Date.now() - minutes * 60_000).toISOString() // History remains available while the collector starts or is stopped; pending rows are merged below. - return await listStoredFlowRows(sinceIso) + return await listStoredFlowRows(sinceIso, serverId, options) } export async function listFlowTalkers(minutes = 5): Promise { const settings = await getTrafficFlowSettingsRow() const runtime = await getFlowRuntimeCounters() - const rows = await listFlowRowsForWindow(minutes) + const rows = await listFlowRowsForWindow(minutes, undefined, { untilIso: new Date().toISOString() }) const catalog = await getServerCatalog() - const nameById = new Map([...catalog.byId].map(([id, s]) => [id, s.name])) - const agg = new Map() - const protoBytes = new Map() - const srcs = new Set() - const dsts = new Set() - const exporters = new Set() - let totalBytes = 0 - for (const r of rows) { - const resolved = resolveIfaceName(r.serverId, r.inIface) - const key = `${r.serverId}|${r.src}|${r.dst}|${r.proto}|${r.srcPort}|${r.dstPort}|${r.inIface}` - const prev = agg.get(key) - const bytes = r.bytes - totalBytes += bytes - srcs.add(r.src) - dsts.add(r.dst) - exporters.add(r.serverId) - protoBytes.set(r.proto, (protoBytes.get(r.proto) ?? 0) + bytes) - if (prev) { - prev.rawBytes += bytes - prev.bytes += bytes - prev.packets += r.packets - } else { - agg.set(key, { - serverId: String(r.serverId), - serverName: nameById.get(r.serverId) ?? String(r.serverId), - src: r.src, - dst: r.dst, - proto: r.proto, - protoName: protoName(r.proto), - srcPort: r.srcPort, - dstPort: r.dstPort, - bytes, - packets: r.packets, - bps: 0, - inIface: resolved.name, - inIfaceIndex: resolved.index, - application: applicationName(r.proto, r.dstPort, r.srcPort), - rawBytes: bytes, - }) - } - } - const windowSec = Math.max(60, minutes * 60) - const talkers = [...agg.values()] - .map((t) => ({ ...t, bps: (t.rawBytes * 8) / windowSec })) - .sort((a, b) => b.bytes - a.bytes) - .slice(0, settings.topN) - .map(({ rawBytes: _raw, ...rest }) => rest) - let topProto = "—" - let topProtoBytes = 0 - for (const [p, b] of protoBytes) { - if (b > topProtoBytes) { - topProtoBytes = b - topProto = protoName(p) - } - } + const nameById = new Map([...catalog.byId].map(([id, server]) => [id, server.name])) + const projection = projectFlowTalkers(rows, minutes, settings.topN, nameById, resolveIfaceName) return { - exportersOnline: exporters.size, - bytesPerMin: minutes > 0 ? totalBytes / minutes : totalBytes, - uniqueSrc: srcs.size, - uniqueDst: dsts.size, - topProto, - talkers, + exportersOnline: projection.observedExporters, + bytesPerMin: projection.bytesPerMin, + uniqueSrc: projection.uniqueSrc, + uniqueDst: projection.uniqueDst, + topProto: projection.topProto, + talkers: projection.talkers, + dataQuality:runtime.dataQuality, lastExporterIp: runtime.lastExporterIp, lastError: runtime.lastError, packetsReceived: runtime.packetsReceived, @@ -534,7 +581,7 @@ export function ingestParsedFlowsForServerForTests(serverId: number, flows: Pars export function resetFlowRingsForTests() { resetEngineForTests() - lastHeartbeat = null + lastHeartbeat = null;heartbeatFailure=null wantListen = false restartAttempts = 0 } @@ -547,23 +594,45 @@ export async function flushPendingForTests(): Promise { await flushPending({ force: true }) } +/** Existing isolates only: no listener start, stop or restart is hidden here. */ +export async function drainExistingTrafficFlowWriters(authorize:()=>Promise,serverIds:readonly number[]) { + await authorize() + if(databaseStop)throw Error("NetFlow database lifecycle operation is active") + const current=worker,generation=listenerGeneration + const validate=async()=>{ + await authorize() + if(databaseStop||worker!==current||listenerGeneration!==generation)throw Error("Existing collector lifecycle changed during strict drain") + } + const scope=[...serverIds] + const main=await strictDrainEngine(validate,scope) + await validate() + const existingWorker=await requestExistingWorkerDrain(current,validate,scope) + await validate() + return {main,existingWorker} +} + const PURGE_LOCK_KEY = 8_723_103 /** Удаляет данные одним transaction; ключи и пиры не меняются. */ -export async function purgeTrafficFlowStore(): Promise { - return withTrafficFlowPurgeLock(() => pool.connect(), PURGE_LOCK_KEY, () => executeTrafficFlowPurge({ +export async function purgeTrafficFlowStore(options: FlowPurgeOptions = {}): Promise { + return executeGuardedApplicationWrite({kind:"netflow-purge",id:"global"},"all", () => + withTrafficFlowPurgeLock(() => pool.connect(), PURGE_LOCK_KEY, () => + // Existing rebuild holds 8723104. Share its exclusion without changing its + // public behavior; a running rebuild refuses purge before worker shutdown. + withTrafficFlowPurgeLock(() => pool.connect(), 8_723_104, () => executeTrafficFlowPurge({ stop: async () => { - wantListen = false - await stopWorkerProcessAsync() + await options.beforeDrain?.() + await stopTrafficFlowListenerForDatabaseOperation() resetEngineForTests() - lastHeartbeat = null + lastHeartbeat = null;heartbeatFailure=null state = { bound: false, address: null } }, - deleteData: () => deleteTrafficFlowData(() => pool.connect()), + deleteData: () => deleteTrafficFlowData(() => pool.connect(), options.beforeDelete), + withDataFence: work => withFlowWriterExclusion(work), invalidate: invalidateTrafficFlowSettingsCache, - maintain: () => dropExpiredPartitions(pool), - restart: startTrafficFlowListener, - })) + maintain: async () => { await options.beforeMaintenance?.(); await dropExpiredPartitions(pool,undefined,options.beforePartitionDrop) }, + restart: async () => { await options.beforeRestart?.(); await restartTrafficFlowListenerAfterDatabaseOperation() }, + })))) } export { peekPendingFlows } diff --git a/backend/src/services/traffic-flow-ipv6-pipeline.test.ts b/backend/src/services/traffic-flow-ipv6-pipeline.test.ts index 139a49d9..a22b0f38 100644 --- a/backend/src/services/traffic-flow-ipv6-pipeline.test.ts +++ b/backend/src/services/traffic-flow-ipv6-pipeline.test.ts @@ -107,7 +107,7 @@ if (!ownDb) { worker = new Worker(workerFile) worker.on("error", error => { workerError = error }) worker.on("message", (message: WorkerToMain) => { - if (message.type === "heartbeat") heartbeat = message.payload + if (message.type === "heartbeat") { heartbeat = message.payload; worker!.postMessage({ type: "heartbeat-ack", heartbeatId: message.heartbeatId }) } else if (message.type === "shutdown-complete") stopped = true else if (message.type === "error") workerError = new Error(message.payload.message) }) diff --git a/backend/src/services/traffic-flow-lifecycle-fence.test.ts b/backend/src/services/traffic-flow-lifecycle-fence.test.ts new file mode 100644 index 00000000..4f54e4c8 --- /dev/null +++ b/backend/src/services/traffic-flow-lifecycle-fence.test.ts @@ -0,0 +1,301 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { EventEmitter } from "node:events" +import test from "node:test" +import ts from "typescript" +import { flowLifecycleBoundary } from "./traffic-flow-lifecycle-fence.js" + +// Run the exact production orchestration with inert Worker/SQL adapters. Importing +// ingest itself would initialize the database and defeat this isolated proof. +const source = readFileSync(new URL("./traffic-flow-ingest.ts", import.meta.url), "utf8") +test("database restart preserves disabled collectors and failed pause latches", async () => { + const begin = source.indexOf("export async function restartTrafficFlowListenerAfterDatabaseOperation(") + const restart = ts.transpileModule(source.slice(begin, source.indexOf("\nexport async function refreshFlowExporterMap", begin)).replace("export ", ""), { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText + function restartFixture(enabled: boolean, failSettings = false, failStop = false) { + let starts = 0 + const run = new Function("deps", ` + let databaseStop=deps.stop,wantListen=true,state={bound:true,address:'old'}; + const getTrafficFlowSettingsRow=deps.settings,startTrafficFlowListener=deps.start; + ${restart} + return {restart:restartTrafficFlowListenerAfterDatabaseOperation,inspect:()=>({latched:databaseStop!==null,wantListen,state})}; + `)({ stop: failStop ? Promise.reject(Error("stop unconfirmed")) : Promise.resolve(), settings: async () => { if (failSettings) throw Error("settings unavailable"); return { enabled } }, start: async () => { starts++ } }) + return { ...run, starts: () => starts } + } + const disabled = restartFixture(false) + await disabled.restart() + assert.deepEqual(disabled.inspect(), { latched: false, wantListen: false, state: { bound: false, address: null } }) + assert.equal(disabled.starts(), 0) + const enabled = restartFixture(true) + await enabled.restart() + assert.equal(enabled.starts(), 1) + for (const failed of [restartFixture(false, true), restartFixture(false, false, true)]) { + await assert.rejects(failed.restart(), /settings unavailable|stop unconfirmed/) + assert.equal(failed.inspect().latched, true) + assert.equal(failed.starts(), 0) + } +}) +const start = source.slice(source.indexOf("export async function startTrafficFlowListener("), source.indexOf("\nexport function stopTrafficFlowListener()")) +const spawn = source.slice(source.indexOf("async function spawnWorker("), source.indexOf("\nfunction stopWorkerProcess(")) +const script = ts.transpileModule(`${spawn}\n${start.replace("export ", "")}`, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None } }).outputText +function fixture(revokeAt?: string, failAt?: string) { + const effects: string[] = [] + let allowed = true + const workers: EventEmitter[] = [] + let installConcurrent: () => void = () => {} + const occurrences = new Map() + const effect = async (name: string) => { + effects.push(name) + const occurrence = (occurrences.get(name) ?? 0) + 1 + occurrences.set(name, occurrence) + if (name === revokeAt || `${name}:${occurrence}` === revokeAt) allowed = false + if (name === "exporters" && failAt === "concurrent-worker") installConcurrent() + if (name === "settings" && occurrence === 1 && failAt === "concurrent-before-spawn") installConcurrent() + if (name === "startup" && failAt === "exit-before-ack") { workers.at(-1)?.emit("exit",1); throw Error("unknown") } + if (name === failAt) throw Error("unknown") + } + class FakeWorker extends EventEmitter { + constructor(..._args: unknown[]) { super(); workers.push(this); effects.push("spawn") } + } + const adapter = new Function("deps", ` + const { flowLifecycleBoundary, Worker, effect } = deps; + let worker = null, databaseStop = null, listenerGeneration = 0, wantListen = false; + let state = {bound:false,address:null}, restartTimer = null, restartAttempts = 0, lastHeartbeat = null; + const process = {execArgv:[],env:{}}; + const setTimeout = ()=>{deps.effects.push("restart-timer");return 1}; + const clearTimeout = ()=>{}; + const workerLifecycle = {drain:()=>effect("drain")}; + const stopWorkerProcess = ()=>{deps.effects.push("stop");worker=null}; + const stopTrafficFlowListener = ()=>{listenerGeneration++;wantListen=false;stopWorkerProcess();void effect("ungated-flush")}; + const flushPending = async (_opts, authorize)=>{await authorize?.();await effect("flush");await authorize?.()}; + const getTrafficFlowSettingsRow = async ()=>{await effect("settings");return {enabled:true,topN:20,retentionHours:24}}; + const configureEngine = ()=>deps.effects.push("configure"); + const buildExporterMapPayload = async ()=>{await effect("exporters");return {byTunnelIp:[],hostIps:[],peers:[]}}; + const setExporterResolveCtx = ()=>deps.effects.push("context"); + const trafficFlowWorkerFileUrl = ()=>"inert"; + const handleWorkerMessage = ()=>{}; + const postToWorker = ()=>deps.effects.push("dispatch"); + const requestExistingWorkerDrain = async (_worker,authorize)=>{await authorize();await effect("startup");await authorize()}; + ${script} + return {start:startTrafficFlowListener,advance:()=>{listenerGeneration++;wantListen=false},wanted:()=>wantListen,installConcurrent:()=>{worker={}}}; + `)({ flowLifecycleBoundary, Worker: FakeWorker, effect, effects }) + installConcurrent=adapter.installConcurrent + return { ...adapter, effects, exit:()=>workers.at(-1)?.emit("exit",1), revoke: () => { allowed = false }, authorize: async () => { if (!allowed) throw Error("revoked") } } +} + +test("production gated lifecycle completes stop, flush, drain and a single spawn", async () => { + const f = fixture() + await f.start(f.authorize) + assert.deepEqual(f.effects, ["stop", "drain", "flush", "settings", "stop", "drain", "settings", "configure", "exporters", "context", "spawn", "startup"]) +}) +test("revocation during every internal asynchronous boundary prevents later effects", async () => { + for (const boundary of ["drain", "drain:2", "flush", "settings", "settings:2", "exporters"]) { + const f = fixture(boundary) + await assert.rejects(f.start(f.authorize), /revoked/) + assert.equal(f.effects.at(-1), boundary.split(":")[0]) + assert.equal(f.effects.includes("spawn"), false) + assert.equal(f.wanted(), false) + } +}) +test("denial before entry leaves collector untouched", async () => { + const f = fixture(); f.revoke() + await assert.rejects(f.start(f.authorize), /revoked/) + assert.deepEqual(f.effects, []) +}) +test("unknown flush/drain/read outcomes stay stopped without replay", async () => { + for (const boundary of ["drain", "flush", "settings", "exporters"]) { + const f = fixture(undefined, boundary) + await assert.rejects(f.start(f.authorize), /unknown/) + assert.equal(f.effects.at(-1), boundary.split(":")[0]) + assert.equal(f.effects.includes("spawn"), false) + assert.equal(f.wanted(), false) + } +}) +test("generation invalidation while authorization awaits blocks a late worker spawn", async () => { + const f = fixture() + await assert.rejects(f.start(async () => { if (f.effects.at(-1) === "exporters") f.advance() }), /lifecycle changed/) + assert.equal(f.effects.includes("spawn"), false) + assert.equal(f.wanted(), false) +}) +test("ordinary caller retains the existing ungated stop flush", async () => { + const f = fixture(); await f.start() + assert.equal(f.effects.includes("ungated-flush"), true) + assert.equal(f.effects.includes("flush"), false) + assert.equal(f.effects.filter(value => value === "spawn").length, 1) +}) + +test("production prune fences each SQL dispatch after previous awaits", async () => { + const engine = readFileSync(new URL("./traffic-flow-engine.ts", import.meta.url), "utf8") + const prune = engine.slice(engine.indexOf("async function pruneStored("), engine.indexOf("\nfunction topNPending")) + const code = ts.transpileModule(prune, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText + for (let revokedAfter = 1; revokedAfter <= 7; revokedAfter++) { + let calls = 0, allowed = true + const run = new Function("deps", ` + let lastPruneAt=0; + const PRUNE_MS=1,retentionHours=24,topN=20,MINUTE_RETENTION_HOURS=24,DAILY_RETENTION_DAYS=30; + const dbQuery=deps.query; + const pruneRipeSqlite=async (_now, deps)=>{await deps.query("ripe-one");await deps.query("ripe-two")}; + ${code} + return pruneStored; + `)({ query: async () => { if (++calls === revokedAfter) allowed = false } }) + await assert.rejects(run(24, 20, async () => { if (!allowed) throw Error("revoked") }), /revoked/) + assert.equal(calls, revokedAfter) + } + assert.match(engine, /flushPendingInternal\(opts, authorize\)/) + assert.match(engine, /pruneStored\(batch.payload.capturedRetentionHours,batch.payload.capturedTopN,authorize\)/) +}) + +import type { Worker } from "node:worker_threads" +import { runExistingWorkerDrain, type FlowDrainPeer } from "./traffic-flow-drain-protocol.js" +import { waitForCollectorShutdown } from "./traffic-flow-worker-shutdown.js" + +test("authorized worker shutdown fences full flush and awaits clean exit without replay", async () => { + const workerSource = readFileSync(new URL("./traffic-flow-collector-worker.ts", import.meta.url), "utf8") + const shutdown = workerSource.slice(workerSource.indexOf("async function shutdown("), workerSource.indexOf("\nfunction completeShutdown(")) + const shutdownCode = ts.transpileModule(shutdown, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText + for (const revokeAt of [undefined, "socket", "flush-one", "flush-two", "pause", "pool"]) { + const main = new EventEmitter(), worker = new EventEmitter(), effects: string[] = [] + let allowed = true, execution: Promise | undefined, dynamicFence: (() => Promise) | undefined + const effect = async (name: string) => { effects.push(name); if (name === revokeAt) allowed = false } + const left: FlowDrainPeer = { on: (e, f) => main.on(e, f), off: (e, f) => main.off(e, f), postMessage: msg => { queueMicrotask(() => worker.emit("message", msg)) } } + const right: FlowDrainPeer = { on: (e, f) => worker.on(e, f), off: (e, f) => worker.off(e, f), postMessage: msg => { queueMicrotask(() => main.emit("message", msg)) } } + const run = new Function("deps", ` + let shuttingDown=false,flushTimer=null,socket={close:fn=>{deps.effect("socket").then(fn)}},bound=true,address="bound"; + const setFlowEngineLifecycleAuthorization=deps.setFence; + const flushPending=async (_opts, authorize)=>{await authorize();await deps.effect("flush-one");await authorize();await deps.effect("flush-two");await authorize()}; + const pauseRipeBackgroundWork=()=>deps.effect("pause"); + const pool={end:()=>deps.effect("pool")}; + const completeShutdown=()=>{}; + ${shutdownCode} + return shutdown; + `)({ effect, setFence: (fence: () => Promise) => { dynamicFence = fence } }) + Object.assign(main, { postMessage: left.postMessage }) + worker.on("message", msg => { + if (msg.type !== "authorized-shutdown") return + execution = runExistingWorkerDrain(right, msg.requestId, async authorize => { await run(authorize, false); return null }, () => { + effects.push("complete") + right.postMessage({ type: "shutdown-complete" }) + setImmediate(() => main.emit("exit", 0)) + }) + }) + const stopped = waitForCollectorShutdown(main as unknown as Worker, 1000, async () => { if (!allowed) throw Error("revoked") }) + if (revokeAt) { + await assert.rejects(stopped, /операция с базой отменена/) + assert.equal(effects.at(-1), revokeAt) + assert.equal(effects.includes("complete"), false) + } else { + await stopped + assert.deepEqual(effects, ["socket", "flush-one", "flush-two", "pause", "pool", "complete"]) + } + await execution + assert.ok(dynamicFence, "retirement installs a dynamic fence for pre-existing timer flushes") + // A timer writer retaining this closure cannot continue anonymously later. + await assert.rejects(dynamicFence!(), /closed/) + } + const engine = readFileSync(new URL("./traffic-flow-engine.ts", import.meta.url), "utf8") + assert.match(engine, /authorize:.*=authorizeEngineLifecycle,serverIds/) + assert.match(workerSource, /flushPending\(\{ force: true, prune: false \}, authorize\)/) +}) + +test("authorized startup uses production worker code and remains stopped on late denial", async () => { + const workerSource = readFileSync(new URL("./traffic-flow-collector-worker.ts", import.meta.url), "utf8") + const begin = workerSource.indexOf("async function startListener(") + const code = ts.transpileModule(workerSource.slice(begin, workerSource.indexOf('\nparentPort?.on(', begin)), { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText + for (const revokeAt of [undefined, "flush-one", "flush-two", "bind", "bind-failed"]) { + const main = new EventEmitter(), worker = new EventEmitter(), effects: string[] = [] + const handlers=new Mapvoid>() + let lateBind: (() => void) | undefined + let allowed = true, finished = false, execution: Promise | undefined, dynamicFence: (() => Promise) | undefined + const effect = (name: string) => { effects.push(name); if (name === revokeAt) allowed = false } + const left: FlowDrainPeer = { on: (e, f) => main.on(e, f), off: (e, f) => main.off(e, f), postMessage: msg => queueMicrotask(() => worker.emit("message", msg)) } + const right: FlowDrainPeer = { on: (e, f) => worker.on(e, f), off: (e, f) => worker.off(e, f), postMessage: msg => queueMicrotask(() => main.emit("message", msg)) } + const run = new Function("deps", ` + let socket=null,flushTimer=null,bound=false,address=null,shuttingDown=false,startingAuthorized=true; + const TICK_MS=2000;const randomUUID=()=>"owned-lifecycle-socket";const clearFlowTemplateCache=()=>deps.effect("templates"); + const stopListener=()=>{throw Error("anonymous stop forbidden")}; + const quiesceListener=()=>{socket=null;flushTimer=null;deps.effect("quiesce")}; + const setFlowEngineLifecycleAuthorization=deps.setFence; + const flushPending=async (_opts,authorize)=>{await authorize();deps.effect("flush-one");await authorize();deps.effect("flush-two");await authorize()}; + const configureEngine=()=>deps.effect("configure"),applyExporterMap=()=>deps.effect("exporters"); + const setEngineError=()=>{},send=()=>{},heartbeat=()=>deps.effect("heartbeat"),ingestDatagram=()=>deps.effect("packet"); + const createSocket=()=>({on:(event,fn)=>deps.setHandler(event,fn),setRecvBufferSize:()=>{},bind:(_port,_host,fn)=>{deps.setBind(fn);deps.effect("bind");if(deps.failBind)throw Error("bind failed")}}); + const setInterval=()=>{deps.effect("timer");return 1}; + ${code} + return {start:startListener,complete:completeAuthorizedStart,quiesce:quiesceListener,active:()=>Boolean(socket||flushTimer),state:()=>({bound,address})}; + `)({ effect, failBind:revokeAt==="bind-failed", setHandler:(event:string,fn:(...args:unknown[])=>void)=>handlers.set(event,fn), setBind:(fn:()=>void)=>{lateBind=fn}, setFence: (fence: (() => Promise) | undefined) => { dynamicFence = fence } }) + worker.on("message", msg => { + if (msg.type !== "authorized-start") return + execution = runExistingWorkerDrain(right, msg.requestId, async authorize => { await run.start(msg.payload, authorize); assert.equal(effects.includes("timer"),false,"no periodic writes before final startup checkpoint"); handlers.get("message")!(Buffer.alloc(0),{address:"inert"}); assert.equal(effects.includes("packet"),false,"no packet/enrichment before final startup checkpoint"); return null }, () => { finished = true; run.complete() }, run.quiesce) + }) + const { requestExistingWorkerDrain } = await import("./traffic-flow-drain-protocol.js") + const requested = requestExistingWorkerDrain(left, async () => { if (!allowed) throw Error("revoked") }, [], 1000, "authorized-start", { listenHost: "inert", listenPort: 1, topN: 20, retentionHours: 24, exporterMap: { overlayPrefix: "", byTunnelIp: [], hostIps: [], peers: [] } }) + if (revokeAt) { + await assert.rejects(requested, revokeAt === "bind-failed" ? /Worker drain outcome is unconfirmed/ : /revoked/) + await execution + assert.equal(finished, false) + assert.equal(run.active(), false) + assert.equal(effects.at(-1), "quiesce") + if (revokeAt === "bind" || revokeAt === "bind-failed") { + assert.equal(effects.includes("timer"), false) + assert.ok(effects.indexOf("quiesce") < effects.indexOf("templates")) + assert.ok(effects.indexOf("flush-two") < effects.indexOf("templates")) + assert.ok(effects.indexOf("templates") < effects.indexOf("bind")) + } else assert.equal(effects.includes("templates"), false,"revoked flush must not clear the prior template cache") + if (lateBind) { + const before = [...effects] + lateBind() + assert.deepEqual(run.state(), {bound:false,address:null}) + assert.deepEqual(effects, before, "late bind cannot revive a denied startup or publish heartbeat") + } + } else { + await requested; await execution + assert.equal(finished, true) + assert.ok(effects.indexOf("flush-two") < effects.indexOf("templates")) + assert.ok(effects.indexOf("templates") < effects.indexOf("bind")) + assert.equal(run.active(), true) + assert.equal(dynamicFence, undefined, "normal runtime begins after the confirmed startup checkpoint") + lateBind!() + assert.deepEqual(run.state(), {bound:true,address:"inert:1"}) + assert.equal(effects.at(-1), "heartbeat") + handlers.get("message")!(Buffer.alloc(0),{address:"inert"}) + assert.equal(effects.at(-1),"packet") + } + } +}) + +test("an unconfirmed authorized-start never acknowledges or schedules a replay", async () => { + for (const f of [fixture("startup"), fixture(undefined, "startup")]) { + await assert.rejects(f.start(f.authorize), /revoked|unknown/) + assert.equal(f.effects.filter(value => value === "spawn").length, 1) + assert.equal(f.effects.at(-1), "startup") + assert.equal(f.wanted(), false) + assert.equal(f.effects.includes("dispatch"), false) + } +}) + + +test("bounded gated startup refuses a concurrent worker before any extra retirement", async () => { + const f=fixture(undefined,"concurrent-worker") + await assert.rejects(f.start(f.authorize),/bounded startup/) + assert.equal(f.effects.filter(value=>value==="stop").length,2,"one initial stop and one empty spawn stop, no additional worker retirement") + assert.equal(f.effects.includes("spawn"),false) + assert.equal(f.effects.includes("restart-timer"),false) +}) +test("pre-ACK worker exit cannot schedule a replay; normal post-ACK exit can restart", async () => { + const failed=fixture(undefined,"exit-before-ack") + await assert.rejects(failed.start(failed.authorize),/unknown/) + assert.equal(failed.effects.filter(value=>value==="spawn").length,1) + assert.equal(failed.effects.includes("restart-timer"),false) + assert.equal(failed.wanted(),false) + const successful=fixture() + await successful.start(successful.authorize) + successful.exit() + assert.equal(successful.effects.at(-1),"restart-timer","ordinary runtime restart begins only after confirmed startup") +}) + +test("initial gated spawn fence refuses a concurrent pointer without a second retirement", async () => { + const f=fixture(undefined,"concurrent-before-spawn") + await assert.rejects(f.start(f.authorize),/bounded startup/) + assert.equal(f.effects.filter(value=>value==="stop").length,1,"only the original start retirement; spawn performs no extra stop") + assert.deepEqual(f.effects,["stop","drain","flush","settings"]) + assert.equal(f.effects.includes("spawn"),false) +}) diff --git a/backend/src/services/traffic-flow-lifecycle-fence.ts b/backend/src/services/traffic-flow-lifecycle-fence.ts new file mode 100644 index 00000000..86235dad --- /dev/null +++ b/backend/src/services/traffic-flow-lifecycle-fence.ts @@ -0,0 +1,13 @@ +/** Lifecycle authorization is re-read before dispatch and after asynchronous work. + * A generation check after authorization prevents an older start from resuming + * while that authorization request was in flight. */ +export type FlowLifecycleAuthorization = () => Promise +export async function flowLifecycleBoundary( + authorize: FlowLifecycleAuthorization | undefined, + isCurrent: () => boolean, +): Promise { + await authorize?.() + const current = isCurrent() + if (authorize && !current) throw Error("NetFlow listener lifecycle changed during authorization") + return current +} diff --git a/backend/src/services/traffic-flow-live-slots.ts b/backend/src/services/traffic-flow-live-slots.ts new file mode 100644 index 00000000..e6cffddb --- /dev/null +++ b/backend/src/services/traffic-flow-live-slots.ts @@ -0,0 +1,11 @@ +// One process-wide admission limit shared by REST SSE and bounded MCP observations. +export const MAX_FLOW_LIVE_SUBSCRIBERS = 4 +export const FLOW_LIVE_TICK_MS = 2000 +let liveSubscribers = 0 +export function tryAcquireFlowLiveSlot(): boolean { + if (liveSubscribers >= MAX_FLOW_LIVE_SUBSCRIBERS) return false + liveSubscribers += 1 + return true +} +export function releaseFlowLiveSlot(): void { liveSubscribers = Math.max(0, liveSubscribers - 1) } +export function resetFlowLiveSlotsForTests(): void { liveSubscribers = 0 } diff --git a/backend/src/services/traffic-flow-map-hops.ts b/backend/src/services/traffic-flow-map-hops.ts index dfd27db6..03f0f674 100644 --- a/backend/src/services/traffic-flow-map-hops.ts +++ b/backend/src/services/traffic-flow-map-hops.ts @@ -1,7 +1,9 @@ -import { eq } from "drizzle-orm" +import { flowReadDbAll } from "./traffic-flow-read-policy.js" +import { isDeepStrictEqual } from "node:util" +import { readFlowMapSourceLimits } from "./traffic-flow-source-limits.js" +import { flowCounterSum } from "./traffic-flow-observed-values.js" +import type { ScopedFlowAnalyticsRead } from "./traffic-flow-analytics.js" import type { FlowMapCountryServiceGroup, FlowMapHop, FlowMapHopsDto, FlowMapService, FlowMapServiceEdge, FlowMapServicePath } from "@mmapp/contracts/traffic-flow" -import { db } from "../db/index.js" -import { userInterfaceBindings } from "../db/schema.js" import { flowRowMatchesFilter } from "./traffic-flow-apps.js" import { OTHER_SERVICE, isNamedInternetService, mapCountryNodeId, mapCountryServiceNodeId, mapServiceNodeId, resolveRipeCountry } from "./traffic-flow-brands.js" import type { FlowIpMeta } from "./traffic-flow-ripe.js" @@ -340,7 +342,7 @@ function hopsQueryKey(q: FlowMapHopsQuery, minSharePct: number): string { async function userIfaceAllow(userId: string): Promise> | null> { if (!userId) return null - const binds = await db.select().from(userInterfaceBindings).where(eq(userInterfaceBindings.userId, userId)) + const binds = await flowReadDbAll<{serverId:number;interfaceName:string}>('SELECT server_id AS "serverId",interface_name AS "interfaceName" FROM user_interface_bindings WHERE user_id=?',[userId]) const allow = new Map>() for (const b of binds) { const set = allow.get(b.serverId) ?? new Set() @@ -408,18 +410,35 @@ async function resolveMinSharePct(q: FlowMapHopsQuery): Promise { } } -async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number): Promise { - refreshFlowCatalogInBackground() +async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number, scope?: ScopedFlowAnalyticsRead): Promise { + const authorize = scope?.authorize ?? (async () => {}) + if (!scope) refreshFlowCatalogInBackground() + await authorize() + const limits = scope ? await readFlowMapSourceLimits() : undefined const windowSec = Math.max(60, q.minutes * 60) - const raw = await listFlowRowsForWindow(q.minutes) - const allow = q.userId ? await userIfaceAllow(q.userId) : null - const catalog = await getServerCatalog() + await authorize() + const raw = await listFlowRowsForWindow(q.minutes, q.serverId, { ...(scope ? { maxRows: scope.maxRows } : {}), untilIso: new Date().toISOString() }) + if (scope && raw.some(row => row.serverId !== q.serverId)) throw new Error("Строка карты вне выбранного сервера") + await authorize() + const catalog = await getServerCatalog(scope?.serverIds) const nameById = new Map([...catalog.byId].map(([id, s]) => [id, s.name])) const ifaceFilter = q.iface && q.iface !== "__all__" ? q.iface : "" const wantDedup = q.dedup !== false && !ifaceFilter const excludeMesh = q.excludeMesh !== false const excludeOverlay = q.excludeOverlay !== false - const topo = await loadFlowTopology() + await authorize() + const topo = await loadFlowTopology(scope?.serverIds, authorize) + let allow: Map> | null = null + if (q.userId) { + if (!scope) allow = await userIfaceAllow(q.userId) + else { + allow = new Map() + for (const binding of topo.clientByIface.values()) if (binding.userId === q.userId) { + const names = allow.get(binding.serverId) ?? new Set(); names.add(binding.interfaceName); allow.set(binding.serverId, names) + } + if (!allow.size) throw new Error("Пользователь не найден в разрешённой топологии") + } + } const matched = [] for (const r of raw) { @@ -440,6 +459,7 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number } const working = wantDedup ? dedupFlowRowsMaxBytes(matched) : matched + if (scope) flowCounterSum(working.map(row => row.bytes)) const hops = new Map() const dstAcc = new Map() const jhToEn = new Map() @@ -664,6 +684,9 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number const listener = getFlowListenerState() const geo = geoipReadersStatus() + await authorize() + if (scope && (!isDeepStrictEqual(topo, await loadFlowTopology(scope.serverIds, authorize)) || !isDeepStrictEqual(limits, await readFlowMapSourceLimits()))) throw new Error("Топология или пределы карты NetFlow изменились во время чтения") + await authorize() return { hops: [...hops.values()] .map((a) => toHop(a, windowSec)) @@ -691,7 +714,17 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number } /** Hop-rates для карты сети: те же фильтры, что у общего NetFlow (dedup / mesh / overlay). */ -export async function buildFlowMapHops(q: FlowMapHopsQuery): Promise { +export async function buildFlowMapHops(q: FlowMapHopsQuery, scope?: ScopedFlowAnalyticsRead): Promise { + if (scope) { + if (!Number.isSafeInteger(q.serverId) || !scope.serverIds.includes(q.serverId!) || !Number.isSafeInteger(scope.maxRows) || scope.maxRows < 1 || scope.maxRows > 50000 || !Number.isSafeInteger(q.minutes) || q.minutes < 1 || q.minutes > 1440 || q.minSharePct !== undefined) throw new Error("Некорректная область карты NetFlow") + await scope.authorize() + const limits = await readFlowMapSourceLimits() + const dto = await buildFlowMapHopsUncached(q, limits.mapServiceMinSharePct, scope) + await scope.authorize() + if (!isDeepStrictEqual(limits, await readFlowMapSourceLimits())) throw new Error("Предел карты NetFlow изменился во время чтения") + await scope.authorize() + return dto + } const minSharePct = await resolveMinSharePct(q) const key = hopsQueryKey(q, minSharePct) const now = Date.now() diff --git a/backend/src/services/traffic-flow-observed-values.ts b/backend/src/services/traffic-flow-observed-values.ts new file mode 100644 index 00000000..7f50f8f7 --- /dev/null +++ b/backend/src/services/traffic-flow-observed-values.ts @@ -0,0 +1,10 @@ +/** PostgreSQL SUM(bigint) may return decimal text beyond JS's exact integer + * range. Never turn a malformed/overflowed observation into zero or round it. */ +export function flowCounter(value: unknown): number { + const number = typeof value === "number" || typeof value === "string" && /^\d+$/.test(value) ? Number(value) : NaN + if (!Number.isSafeInteger(number) || number < 0) throw Object.assign(new Error("Недостоверный или слишком большой счётчик NetFlow"), { statusCode: 502 }) + return number +} +export function flowCounterSum(values: readonly number[]) { + return values.reduce((sum, value) => flowCounter(sum + flowCounter(value)), 0) +} diff --git a/backend/src/services/traffic-flow-overlay-execution.ts b/backend/src/services/traffic-flow-overlay-execution.ts new file mode 100644 index 00000000..218047ab --- /dev/null +++ b/backend/src/services/traffic-flow-overlay-execution.ts @@ -0,0 +1,132 @@ +import { eq } from "drizzle-orm" +import { setTimeout as delay } from "node:timers/promises" +import { db } from "../db/index.js" +import { servers } from "../db/schema.js" +import { MikrotikClient, encodeRosId } from "./mikrotik.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { withFlowConfigurationLock, preflightFlowHost, reconcileFlowHost, getFlowHostStatus } from "./traffic-flow-host-agent.js" +import { ensureHostKeys, getTrafficFlowSettingsRow, invalidateTrafficFlowSettingsCache, upsertHostPeer, enableTrafficFlowIngest } from "./traffic-flow-settings.js" +import { startTrafficFlowListener, refreshFlowExporterMap, getFlowListenerState } from "./traffic-flow-ingest.js" +import { invalidateFlowCatalogCache } from "./traffic-flow-topology.js" +import { listTrafficFlowHostFiles } from "./traffic-flow-host-files.js" +import { allocateOverlayAddress, FLOW_CACHE_ENTRIES, FLOW_TARGET_SRC_AUTO } from "./traffic-flow-overlay.js" +import { overlayConfigRow, overlayPeerEndpoint, readOverlayState, validateOverlayConfiguration, type OverlayOptions, type OverlayRow, type OverlayState } from "./traffic-flow-overlay-state.js" +import type { TrafficFlowOverlayResult } from "@mmapp/contracts/traffic-flow" + +const wg="/interface/wireguard",peerPath=wg+"/peers",addressPath="/ip/address",routePath="/ip/route",filterPath="/ip/firewall/filter",flowPath="/ip/traffic-flow",ipfixPath=flowPath+"/ipfix",targetPath=flowPath+"/target" +const id=(row:OverlayRow)=>row[".id"]??row.id +const matches=(row:OverlayRow,fields:OverlayRow)=>Object.entries(fields).every(([key,value])=>row[key]===value) +const same=(left:unknown,right:unknown)=>fingerprint(left)===fingerprint(right) +const conflict=()=>new McpError("STATE_CONFLICT","Источник overlay изменился; нужен новый план",409) +const unknown=()=>new McpError("MUTATION_OUTCOME_UNKNOWN","Эффект шага overlay не подтверждён; запись не повторяется, нужна сверка",502) +export const ipfixFields=Object.fromEntries(["bytes","packets","src-address","dst-address","protocol","src-port","dst-port","in-interface","out-interface","gateway","first-forwarded","last-forwarded","nat-src-address","nat-dst-address","nat-src-port","nat-dst-port"].map(key=>[key,"yes"])) +export type OverlayExecutionReceipt={configurationVerified:boolean;afterProof:string;collectorListening:boolean;hostInstallationRequired:boolean;linuxHostApplied:boolean;completedStages:string[]} + +/** One staged executor is used by REST and MCP. Each acknowledged native write is + * read back before the next stage; no command is retried or silently skipped. */ +export async function executeFlowOverlay(serverId:number,opts:OverlayOptions,control:{authorize?:()=>Promise;signal?:AbortSignal;expectedProof?:string}={}) :Promise { + const authorize=async()=>{if(control.signal?.aborted)throw new McpError("READ_CANCELLED","Overlay отменён",409);await control.authorize?.()} + await authorize() + return withApplicationMutationLock(()=>withConfigMutationLock([serverId],()=>withFlowConfigurationLock(async()=>{ + let state=await readOverlayState(serverId,opts,authorize,control.signal) + if(control.expectedProof&&state.proof!==control.expectedProof)throw conflict() + const endpoint=validateOverlayConfiguration(state,opts),ids=state.catalog.map(row=>row.id) + const address=state.server.mgmtTunnelIp||allocateOverlayAddress(state.settings.prefix,state.settings.collectorIp,serverId,new Set(state.catalog.map(row=>row.mgmtTunnelIp).filter(Boolean))) + const cidr=address+"/"+state.settings.prefix.split("/")[1],routeDst=state.settings.collectorIp+"/32",steps:string[]=[] + const existingAddresses=(state.router[addressPath]??[]).filter(row=>row.interface==="wg-flow") + const existingRoutes=(state.router[routePath]??[]).filter(row=>row["dst-address"]===routeDst) + const existingFirewall=(state.router[filterPath]??[]).filter(row=>row.comment==="mm-wg-flow") + if(existingAddresses.length>1||existingAddresses.some(row=>row.address!==cidr||row.disabled==="true")||existingRoutes.length>1||existingRoutes.some(row=>row.gateway!=="wg-flow"||row.disabled==="true")||existingFirewall.length>1||existingFirewall.some(row=>!matches(row,{chain:"input",protocol:"udp","dst-port":"13232",action:"accept"})||row.disabled==="true"))throw new McpError("STATE_CONFLICT","Существующий адрес, маршрут или firewall wg-flow несовместим с overlay",409) + await authorize() + if(state.agentConfigured&&!state.settings.hasHostPublicKey){if(!(await getFlowHostStatus()).reachable)throw new McpError("STATE_UNAVAILABLE","Агент хоста не готов к созданию overlay",409)} + else await preflightFlowHost() + await authorize() + const source=()=>readOverlayState(serverId,opts,authorize,control.signal) + const fence=async()=>{await authorize();if((await source()).proof!==state.proof)throw conflict()} + const client=async()=>{await fence();const row=await getServerRowById(serverId);await authorize();if(!row)throw conflict();return MikrotikClient.fromServer(row)} + const dbStage=async(name:string,write:()=>Promise,allowed:(before:OverlayState,after:OverlayState)=>boolean)=>{ + await fence();const before=state;await authorize();await write();await authorize();const after=await source() + if(!allowed(before,after))throw unknown();state=after;steps.push(name) + } + const routerStage=async(name:string,path:string,method:"put"|"patch"|"delete"|"post",body:Record={},rowId?:string)=>{ + const native=await client(),before=state,rows=before.router[path] + if(!rows)throw conflict() + const fields=await overlayConfigRow(path,body),dispatchPath=method==="post"?path+"/set":rowId?path+"/"+encodeRosId(rowId):path + await authorize() + if(method==="put")await native.put(dispatchPath,body,15000,fence) + else if(method==="patch")await native.patch(dispatchPath,body,15000,fence) + else if(method==="delete")await native.delete(dispatchPath,10000,fence) + else {await fence();await native.post(dispatchPath,body,15000,control.signal)} + await authorize();const after=await source(),next=after.router[path] + if(!next)throw unknown() + let valid=false + if(method==="put") { + const added=next.filter(row=>!rows.some(old=>id(old)===id(row))) + valid=added.length===1&&Boolean(id(added[0]))&&matches(added[0],fields)&&same(next.filter(row=>id(row)!==id(added[0])),rows) + }else if(method==="delete")valid=same(next,rows.filter(row=>id(row)!==rowId)) + else { + const target=rowId?rows.find(row=>id(row)===rowId):rows[0] + if(target)valid=same(next,rows.map(row=>row===target?{...row,...fields}:row)) + else valid=method==="post"&&next.length===1&&matches(next[0],fields) + } + const oldOther={...before.configuration,router:{...before.router,[path]:null}},newOther={...after.configuration,router:{...after.router,[path]:null}} + // Creating WG also creates its generic interface row. No other implicit config is accepted. + if(path===wg&&method==="put") { + const oldInterfaces=before.router["/interface"]??[],newInterfaces=after.router["/interface"]??[],added=newInterfaces.filter(row=>!oldInterfaces.some(old=>id(old)===id(row))) + valid=valid&&added.length===1&&added[0].name==="wg-flow"&&same(newInterfaces.filter(row=>id(row)!==id(added[0])),oldInterfaces) + oldOther.router["/interface"]=null;newOther.router["/interface"]=null + } + if(!valid||!same(oldOther,newOther))throw unknown() + state=after;steps.push(name) + } + const unchangedExceptSettings=(before:OverlayState,after:OverlayState,keys:string[])=>same(before.catalog,after.catalog)&&same(before.router,after.router)&&same(Object.fromEntries(Object.entries(before.settings).filter(([key])=>!keys.includes(key))),Object.fromEntries(Object.entries(after.settings).filter(([key])=>!keys.includes(key)))) + return executeGuardedApplicationWrite({kind:"netflow-overlay",id:"1"},ids,async()=>{ + if(!state.settings.hasHostPublicKey)await dbStage("host-keys",ensureHostKeys,(before,after)=>after.settings.hasHostPrivateKey&&after.settings.hasHostPublicKey&&unchangedExceptSettings(before,after,["hasHostPrivateKey","hasHostPublicKey","hostPrivateKeyProof","hostPublicKeyProof"])) + await fence();await preflightFlowHost();await authorize() + if(!(state.router[wg]??[]).some(row=>row.name==="wg-flow"))await routerStage("wireguard-interface",wg,"put",{name:"wg-flow","listen-port":"13232",mtu:"1420",comment:"MikrotikManager traffic-flow overlay"}) + if(!existingAddresses.length)await routerStage("interface-address",addressPath,"put",{address:cidr,interface:"wg-flow"}) + await fence();invalidateTrafficFlowSettingsCache();const hostKey=(await getTrafficFlowSettingsRow()).hostPublicKey;await authorize() + const keyFields=await overlayConfigRow(peerPath,{"public-key":hostKey}) + const peers=(state.router[peerPath]??[]).filter(row=>row.interface==="wg-flow"&&row["public-key"]===keyFields["public-key"]) + if(peers.length>1)throw conflict() + const peerFields={interface:"wg-flow","public-key":hostKey,"allowed-address":routeDst,comment:"MM traffic-flow collector",name:"mm-collector"} + if(peers[0]?.["endpoint-address"])await routerStage("peer-endpoint-remove",peerPath,"delete",{},id(peers[0])) + if(!peers.length||peers[0]["endpoint-address"])await routerStage("collector-peer",peerPath,"put",peerFields) + else await routerStage("collector-peer",peerPath,"patch",peerFields,id(peers[0])) + if(!existingRoutes.length)await routerStage("collector-route",routePath,"put",{"dst-address":routeDst,gateway:"wg-flow",comment:"MM traffic-flow collector"}) + if(!existingFirewall.length)await routerStage("firewall-input",filterPath,"put",{chain:"input",protocol:"udp","dst-port":"13232",action:"accept",comment:"mm-wg-flow"}) + const singleton=state.router[flowPath]?.[0] + await routerStage("traffic-flow",flowPath,singleton&&id(singleton)?"patch":"post",{enabled:"yes",interfaces:"all","cache-entries":FLOW_CACHE_ENTRIES,"active-flow-timeout":"1m","inactive-flow-timeout":"15s"},singleton?id(singleton):undefined) + if(state.router[ipfixPath]!==null){const fields=state.router[ipfixPath]?.[0];await routerStage("ipfix-fields",ipfixPath,fields&&id(fields)?"patch":"post",ipfixFields,fields?id(fields):undefined)} + const targets=(state.router[targetPath]??[]).filter(row=>row["dst-address"]===state.settings.collectorIp) + if(targets.length>1)throw conflict() + await routerStage("ipfix-target",targetPath,targets.length?"patch":"put",{"dst-address":state.settings.collectorIp,"src-address":FLOW_TARGET_SRC_AUTO,port:String(state.settings.flowListenPort),version:"ipfix"},targets[0]?id(targets[0]):undefined) + if(opts.disableGreFastPath)for(const row of state.router["/interface/gre"]??[])if(row["allow-fast-path"]!=="false") { + if(!id(row)||row.dynamic==="true")throw new McpError("PROTECTED_OBJECT","Динамический или неопознанный GRE нельзя менять",409) + await routerStage("gre-slow-path","/interface/gre","patch",{"allow-fast-path":"no"},id(row)) + } + await fence();const native=await client(),raw=await native.get(wg,10000,control.signal);await authorize() + const iface=(Array.isArray(raw)?raw:[]).find(row=>row&&typeof row==="object"&&row.name==="wg-flow") as Record|undefined + const publicKey=String(iface?.["public-key"]??"") + if(!publicKey)throw unknown() + await dbStage("server-overlay-address",async()=>{await db.update(servers).set({mgmtTunnelIp:address,updatedAt:new Date().toISOString()}).where(eq(servers.id,serverId));invalidateFlowCatalogCache()},(before,after)=>same(before.settings,after.settings)&&same(before.router,after.router)&&same(before.catalog.map(row=>row.id===serverId?{...row,mgmtTunnelIp:address,updatedAt:"changed"}:row),after.catalog.map(row=>row.id===serverId?{...row,updatedAt:"changed"}:row))) + await dbStage("collector-host-peer",()=>upsertHostPeer({serverId,name:state.server.name||state.server.host,publicKey,allowedIps:[address+"/32"],address,endpoint:overlayPeerEndpoint(endpoint)}),(before,after)=>unchangedExceptSettings(before,after,["peersJson"])&&same(before.settings.peersJson.filter(peer=>peer.serverId!==serverId),after.settings.peersJson.filter(peer=>peer.serverId!==serverId))&&after.settings.peersJson.filter(peer=>peer.serverId===serverId).length===1) + await dbStage("enable-ingest",enableTrafficFlowIngest,(before,after)=>after.settings.enabled&&unchangedExceptSettings(before,after,["enabled"])) + let hostStatus:Awaited>|undefined,hostError:string|undefined + if(state.agentConfigured){await fence();try{hostStatus=await reconcileFlowHost();await authorize();if(hostStatus.configMatches!==true)hostError="Хост не подтвердил конфигурацию"}catch{await authorize();hostError="Применение хоста не подтверждено; нужна сверка без повтора"}steps.push(hostError?"host-pending":"host-applied")} + if(!hostError){ + await fence();await startTrafficFlowListener();await authorize();await refreshFlowExporterMap();await authorize() + // Worker start acknowledges scheduling, not bind. Manual installs can legitimately lack the host interface. + if(state.agentConfigured)for(let n=0;n<100&&!getFlowListenerState().bound;n++){await delay(100,undefined,{signal:control.signal});await authorize()} + if(state.agentConfigured&&!getFlowListenerState().bound)throw unknown() + steps.push(getFlowListenerState().bound?"collector-listening":"collector-start-requested") + } + await fence() + const receipt:OverlayExecutionReceipt={configurationVerified:!hostError,afterProof:state.proof,collectorListening:getFlowListenerState().bound,hostInstallationRequired:!state.agentConfigured,linuxHostApplied:state.agentConfigured&&!hostError,completedStages:steps} + return {ok:!hostError,serverId,interfaceName:"wg-flow",address,publicKey,linuxPeerBlock:`[Peer]\nPublicKey = ${publicKey}\nAllowedIPs = ${address}/32\nEndpoint = ${overlayPeerEndpoint(endpoint)}\nPersistentKeepalive = 25`,trafficFlow:true,steps,hostFiles:state.agentConfigured?[]:await listTrafficFlowHostFiles(),stage:hostError?"host-pending":state.agentConfigured?"applied":"manual",...(hostStatus?{hostStatus}:{}),...(hostError?{hostError}:{}),receipt} + },async()=>[{kind:"netflow-settings",id:"1"},{kind:"netflow-host",id:"1"},{kind:"netflow-host-files",id:"1"}]) + }))) +} diff --git a/backend/src/services/traffic-flow-overlay-model.test.ts b/backend/src/services/traffic-flow-overlay-model.test.ts new file mode 100644 index 00000000..e568de33 --- /dev/null +++ b/backend/src/services/traffic-flow-overlay-model.test.ts @@ -0,0 +1,38 @@ +import assert from "node:assert/strict" +import { overlayMatchesDesired } from "./traffic-flow-overlay-reconcile.js" +import { ipfixFields } from "./traffic-flow-overlay-execution.js" +import { overlayPeerEndpoint, type OverlayState } from "./traffic-flow-overlay-state.js" + +const server={id:126,name:"router",host:"192.0.2.126",mgmtTunnelIp:"",updatedAt:"before"} +const router={"/interface":[{".id":"*E",name:"ether1",type:"ether"}],"/interface/wireguard":[],"/interface/wireguard/peers":[],"/ip/address":[],"/ip/route":[],"/ip/firewall/filter":[],"/ip/traffic-flow":[{".id":"*F",enabled:"false"}],"/ip/traffic-flow/ipfix":[{".id":"*I",bytes:"false"}],"/ip/traffic-flow/target":[],"/interface/gre":[{".id":"*G","allow-fast-path":"true"}]} +const settings={prefix:"10.255.254.0/24",collectorIp:"10.255.254.1",flowListenPort:4739,hasHostPrivateKey:true,hasHostPublicKey:true,hostPrivateKeyProof:"hostprivate",hostPublicKeyProof:"hostpublic",peersJson:[],enabled:false} +const before={server,catalog:[server],settings,router,configuration:{agentSocketProof:"agent",listenerHostProof:"host"},proof:"before"} as unknown as OverlayState +const after=structuredClone(before) +after.catalog[0].mgmtTunnelIp="10.255.254.127";after.catalog[0].updatedAt="after" +after.settings.enabled=true +after.settings.peersJson=[{serverId:126,name:"router",publicKeyProof:"routerpublic",allowedIps:["10.255.254.127/32"],address:"10.255.254.127",endpoint:"router.example:13232"}] +after.router["/interface"]!.push({".id":"*W",name:"wg-flow",type:"wg"}) +after.router["/interface/wireguard"]=[{".id":"*W",name:"wg-flow","listen-port":"13232",mtu:"1420",comment:"MikrotikManager traffic-flow overlay","public-key":"proof:routerpublic"}] +after.router["/interface/wireguard/peers"]=[{".id":"*P",interface:"wg-flow","public-key":"proof:hostpublic","allowed-address":"10.255.254.1/32",comment:"MM traffic-flow collector",name:"mm-collector"}] +after.router["/ip/address"]=[{".id":"*A",interface:"wg-flow",address:"10.255.254.127/24"}] +after.router["/ip/route"]=[{".id":"*R","dst-address":"10.255.254.1/32",gateway:"wg-flow"}] +after.router["/ip/firewall/filter"]=[{".id":"*B",chain:"input",protocol:"udp","dst-port":"13232",action:"accept",comment:"mm-wg-flow"}] +after.router["/ip/traffic-flow"]=[{".id":"*F",enabled:"true",interfaces:"all","cache-entries":"256k","active-flow-timeout":"1m","inactive-flow-timeout":"15s"}] +after.router["/ip/traffic-flow/ipfix"]=[{".id":"*I",...Object.fromEntries(Object.keys(ipfixFields).map(key=>[key,"true"]))}] +after.router["/ip/traffic-flow/target"]=[{".id":"*T","dst-address":"10.255.254.1","src-address":"0.0.0.0",port:"4739",version:"ipfix"}] +after.router["/interface/gre"]=[{".id":"*G","allow-fast-path":"false"}] +const opts={publicEndpoint:"router.example",disableGreFastPath:true} +assert.equal(overlayMatchesDesired(before,after,opts),true) +for(const change of [ + (state:OverlayState)=>{state.catalog[0].host="192.0.2.200"}, + (state:OverlayState)=>{state.settings.hostPrivateKeyProof="rotated"}, + (state:OverlayState)=>{state.router["/interface"]![0].comment="foreign drift"}, + (state:OverlayState)=>{state.router["/interface/wireguard/peers"]![0]["endpoint-address"]="unexpected.example"}, + (state:OverlayState)=>{state.router["/ip/traffic-flow/target"]![0].port="9999"}, + (state:OverlayState)=>{state.router["/ip/firewall/filter"]![0].disabled="true"}, + (state:OverlayState)=>{state.router["/interface/gre"]![0]["allow-fast-path"]="true"}, + (state:OverlayState)=>{state.settings.peersJson.push({serverId:999,publicKeyProof:"foreign"})}, +]){const changed=structuredClone(after);change(changed);assert.equal(overlayMatchesDesired(before,changed,opts),false)} +assert.equal(overlayPeerEndpoint("2001:db8::1"),"[2001:db8::1]:13232") +assert.equal(overlayPeerEndpoint("router.example"),"router.example:13232") +console.log("traffic-flow-overlay-model.test.ts: desired native/DB graph, unrelated/key/endpoint/firewall/GRE drift and IPv6 endpoints verified") diff --git a/backend/src/services/traffic-flow-overlay-reconcile.ts b/backend/src/services/traffic-flow-overlay-reconcile.ts new file mode 100644 index 00000000..a4340158 --- /dev/null +++ b/backend/src/services/traffic-flow-overlay-reconcile.ts @@ -0,0 +1,45 @@ +import { fingerprint } from "../modules/mcp/security.js" +import { allocateOverlayAddress, FLOW_CACHE_ENTRIES, FLOW_TARGET_SRC_AUTO } from "./traffic-flow-overlay.js" +import { ipfixFields } from "./traffic-flow-overlay-execution.js" +import { overlayPeerEndpoint, type OverlayOptions, type OverlayState, type OverlayRow } from "./traffic-flow-overlay-state.js" + +const same=(a:unknown,b:unknown)=>fingerprint(a)===fingerprint(b) +const omit=(row:object,keys:string[])=>Object.fromEntries(Object.entries(row).filter(([key])=>!keys.includes(key))) +const match=(row:OverlayRow,fields:OverlayRow)=>Object.entries(fields).every(([key,value])=>row[key]===value) +/** Read-only semantic comparison. Generated IDs/keys are accepted only for a + * newly provisioned object; every pre-existing unrelated configuration is preserved. */ +export function overlayMatchesDesired(before:OverlayState,after:OverlayState,opts:OverlayOptions):boolean { + const serverId=before.server.id,endpoint=opts.publicEndpoint?.trim()||before.server.host.trim() + const address=before.server.mgmtTunnelIp||allocateOverlayAddress(before.settings.prefix,before.settings.collectorIp,serverId,new Set(before.catalog.map(row=>row.mgmtTunnelIp).filter(Boolean))) + if(before.configuration.agentSocketProof!==after.configuration.agentSocketProof||before.configuration.listenerHostProof!==after.configuration.listenerHostProof||!after.settings.enabled||!after.settings.hasHostPrivateKey||!after.settings.hasHostPublicKey)return false + if(!same(before.catalog.map(row=>row.id===serverId?{...omit(row,["updatedAt"]),mgmtTunnelIp:address}:row),after.catalog.map(row=>row.id===serverId?omit(row,["updatedAt"]):row)))return false + const keyProps=["hasHostPrivateKey","hasHostPublicKey","hostPrivateKeyProof","hostPublicKeyProof"] + if(!same(omit(before.settings,["enabled","peersJson",...(!before.settings.hasHostPublicKey?keyProps:[])]),omit(after.settings,["enabled","peersJson",...(!before.settings.hasHostPublicKey?keyProps:[])])))return false + const expectedPeer={serverId,name:before.server.name||before.server.host,publicKeyProof:(after.router["/interface/wireguard"]??[]).find(row=>row.name==="wg-flow")?.["public-key"]?.replace(/^proof:/,""),allowedIps:[address+"/32"],address,endpoint:overlayPeerEndpoint(endpoint)} + if(!same(before.settings.peersJson.filter(peer=>peer.serverId!==serverId),after.settings.peersJson.filter(peer=>peer.serverId!==serverId))||!same(after.settings.peersJson.filter(peer=>peer.serverId===serverId),[expectedPeer]))return false + const resource=(path:string,select:(row:OverlayRow)=>boolean,fields:OverlayRow,preserve=false)=>{ + const originals=(before.router[path]??[]).filter(select),current=(after.router[path]??[]).filter(select) + if(current.length!==1||!match(current[0],fields)||current[0].disabled==="true")return false + if(!same((before.router[path]??[]).filter(row=>!select(row)),(after.router[path]??[]).filter(row=>!select(row))))return false + if(originals.length&&preserve&&!same(originals,current))return false + if(originals.length&&!preserve&&!same(omit(originals[0],Object.keys(fields)),omit(current[0],Object.keys(fields))))return false + return true + } + const wgBefore=(before.router["/interface/wireguard"]??[]).filter(row=>row.name==="wg-flow") + if(!resource("/interface/wireguard",row=>row.name==="wg-flow",wgBefore.length?{"listen-port":"13232"}:{"listen-port":"13232",mtu:"1420",comment:"MikrotikManager traffic-flow overlay"},Boolean(wgBefore.length)))return false + if(!resource("/interface",row=>row.name==="wg-flow",{name:"wg-flow"},Boolean(wgBefore.length)))return false + if(!resource("/ip/address",row=>row.interface==="wg-flow",{address:address+"/"+before.settings.prefix.split("/")[1],interface:"wg-flow"},true))return false + if(!resource("/ip/route",row=>row["dst-address"]===before.settings.collectorIp+"/32",{"dst-address":before.settings.collectorIp+"/32",gateway:"wg-flow"},true))return false + if(!resource("/ip/firewall/filter",row=>row.comment==="mm-wg-flow",{chain:"input",protocol:"udp","dst-port":"13232",action:"accept",comment:"mm-wg-flow"},true))return false + const peerKey="proof:"+after.settings.hostPublicKeyProof,selected=(row:OverlayRow)=>row.interface==="wg-flow"&&row["public-key"]===peerKey + const currentPeers=(after.router["/interface/wireguard/peers"]??[]).filter(selected),oldPeers=(before.router["/interface/wireguard/peers"]??[]).filter(selected) + if(currentPeers.length!==1||currentPeers[0]["endpoint-address"]||!match(currentPeers[0],{interface:"wg-flow","public-key":peerKey,"allowed-address":before.settings.collectorIp+"/32",comment:"MM traffic-flow collector",name:"mm-collector"}))return false + if(!same((before.router["/interface/wireguard/peers"]??[]).filter(row=>!selected(row)),(after.router["/interface/wireguard/peers"]??[]).filter(row=>!selected(row))))return false + if(oldPeers.length&&!oldPeers[0]["endpoint-address"]&&!same(omit(oldPeers[0],["allowed-address","comment","name"]),omit(currentPeers[0],["allowed-address","comment","name"])))return false + if(!resource("/ip/traffic-flow",()=>true,{enabled:"true",interfaces:"all","cache-entries":FLOW_CACHE_ENTRIES,"active-flow-timeout":"1m","inactive-flow-timeout":"15s"}))return false + if(before.router["/ip/traffic-flow/ipfix"]!==null&&!resource("/ip/traffic-flow/ipfix",()=>true,Object.fromEntries(Object.keys(ipfixFields).map(key=>[key,"true"]))))return false + if(before.router["/ip/traffic-flow/ipfix"]===null&&after.router["/ip/traffic-flow/ipfix"]!==null)return false + if(!resource("/ip/traffic-flow/target",row=>row["dst-address"]===before.settings.collectorIp,{"dst-address":before.settings.collectorIp,"src-address":FLOW_TARGET_SRC_AUTO,port:String(before.settings.flowListenPort),version:"ipfix"}))return false + if(opts.disableGreFastPath&&!same((before.router["/interface/gre"]??[]).map(row=>({...row,"allow-fast-path":"false"})),after.router["/interface/gre"]))return false + return true +} diff --git a/backend/src/services/traffic-flow-overlay-state.ts b/backend/src/services/traffic-flow-overlay-state.ts new file mode 100644 index 00000000..3dd27477 --- /dev/null +++ b/backend/src/services/traffic-flow-overlay-state.ts @@ -0,0 +1,76 @@ +import { isIP } from "node:net" +import { trafficFlowPublicEndpointSchema, trafficFlowOverlayPrefixSchema } from "@mmapp/contracts/traffic-flow" +import { db } from "../db/index.js" +import { servers, trafficFlowSettings } from "../db/schema.js" +import { MikrotikClient, MikrotikError } from "./mikrotik.js" +import { agentSocket } from "./traffic-flow-agent-client.js" +import { allocateOverlayAddress } from "./traffic-flow-overlay.js" +import { fingerprint, privateFingerprinter, McpError } from "../modules/mcp/security.js" + +export type OverlayOptions = { publicEndpoint?: string; requestHost?: string; disableGreFastPath?: boolean } +export type OverlayRow = Record +export const overlayPeerEndpoint=(host:string)=>`${isIP(host)===6?`[${host}]`:host}:13232` +const inPrefix=(ip:string,prefix:string)=>{const [base,bits]=prefix.split("/"),number=(value:string)=>value.split(".").reduce((sum,part)=>(sum*256+Number(part))>>>0,0),mask=Number(bits)===0?0:(0xffffffff<<(32-Number(bits)))>>>0;return isIP(ip)===4&&((number(ip)&mask)>>>0)===((number(base)&mask)>>>0)} +export const overlayPaths = ["/interface", "/interface/wireguard", "/interface/wireguard/peers", "/ip/address", "/ip/route", "/ip/firewall/filter", "/ip/traffic-flow", "/ip/traffic-flow/ipfix", "/ip/traffic-flow/target"] as const +const runtime = new Set(["running","active","inactive","invalid","actual-mtu","current-endpoint-address","current-endpoint-port","last-handshake","rx","tx","rx-byte","tx-byte","rx-packet","tx-packet","bytes","packets","hw-offload","immediate-gw","active-flows","total-flows","cache-used","cache-free","received-bytes","transmitted-bytes","last-link-up-time","last-link-down-time","link-downs","fp-rx-byte","fp-tx-byte","fp-rx-packet","fp-tx-packet"]) +export async function overlayConfigRow(path:string,row:Record,privateSign?:(value:string)=>string):Promise { + const sign=privateSign??await privateFingerprinter("netflow-overlay-field-v1") + return Object.fromEntries(Object.entries(row).filter(([key])=>!runtime.has(key)||path.endsWith("/ipfix")&&["bytes","packets"].includes(key)).map(([key,value])=>[key,/password|(?:^|-)key$|secret/i.test(key)?`proof:${sign(String(value??""))}`:value===true||value==="yes"?"true":value===false||value==="no"?"false":String(value??"")])) +} +export type OverlayState = Awaited> +export async function readOverlayState(serverId:number,opts:OverlayOptions,authorize:()=>Promise=async()=>{},signal?:AbortSignal) { + const check=async()=>{if(signal?.aborted)throw new McpError("READ_CANCELLED","Чтение overlay отменено",409);await authorize()} + await check() + const rawCatalog=await db.select().from(servers).orderBy(servers.id).limit(1001) + if(rawCatalog.length>1000)throw new McpError("STATE_UNAVAILABLE","Превышен предел каталога overlay",409) + await check() + const rawServer=rawCatalog.find(row=>row.id===serverId) + if(!rawServer?.enabled)throw new McpError("NOT_FOUND","Сервер отсутствует или выключен",404) + const rawSettings=(await db.select().from(trafficFlowSettings).limit(2))[0] + if(!rawSettings||rawSettings.id!==1)throw new McpError("STATE_UNAVAILABLE","Настройки NetFlow не инициализированы",409) + if(!Array.isArray(rawSettings.peersJson)||rawSettings.peersJson.length>1000||rawSettings.peersJson.some((peer:unknown)=>!peer||typeof peer!=="object"||!rawCatalog.some(server=>server.id===(peer as {serverId?:unknown}).serverId)))throw new McpError("STATE_UNAVAILABLE","Не подтверждён полный набор host peers",409) + const sign=await privateFingerprinter("netflow-overlay-field-v1") + const catalog=rawCatalog.map(({password,...row})=>({...row,credentialProof:sign(password)})),server=catalog.find(row=>row.id===serverId)! + // This state can be sealed into plans: it contains proofs, never router credentials or WG keys. + const {hostPrivateKey,hostPublicKey,peersJson,lastDatagramAt:_a,lastExporterIp:_b,packetsReceived:_c,lastError:_d,updatedAt:_e,...configurationSettings}=rawSettings + const settings={...configurationSettings,hasHostPrivateKey:Boolean(hostPrivateKey),hasHostPublicKey:Boolean(hostPublicKey),hostPrivateKeyProof:sign(hostPrivateKey),hostPublicKeyProof:sign(hostPublicKey), + peersJson:peersJson.map((peer:unknown)=>{const {publicKey,...rest}=peer as Record;return {...rest,serverId:Number(rest.serverId),publicKeyProof:sign(String(publicKey??""))}})} + const client=MikrotikClient.fromServer(rawServer),router:Record={} + for(const path of [...overlayPaths,...(opts.disableGreFastPath?["/interface/gre"]:[])]) { + await check() + try { + const raw=await client.get(path,10000,signal),rows=Array.isArray(raw)?raw:raw&&typeof raw==="object"?[raw]:null + if(!rows||rows.length>2000||rows.some(row=>!row||typeof row!=="object"||Array.isArray(row)))throw new Error("shape") + router[path]=await Promise.all(rows.filter(row=>path!=="/ip/route"||![true,"true","yes"].includes((row as Record).dynamic as string|boolean)).map(row=>overlayConfigRow(path,row as Record,sign))) + }catch(error){await check();if(path==="/ip/traffic-flow/ipfix"&&error instanceof MikrotikError&&[400,404].includes(error.statusCode))router[path]=null;else throw new McpError("STATE_UNAVAILABLE","Не подтверждён ограниченный native источник overlay",409)} + await check() + } + const configuration={catalog,settings,router,agentSocketProof:sign(agentSocket()),listenerHostProof:sign(process.env.FLOW_LISTEN_HOST?.trim()||rawSettings.collectorIp||"127.0.0.1")} + if(Buffer.byteLength(JSON.stringify(configuration))>2097152)throw new McpError("STATE_UNAVAILABLE","Источник overlay превышает 2 МиБ",409) + await check() + return {server,settings,catalog,router,agentConfigured:Boolean(agentSocket()),configuration,proof:sign(fingerprint(configuration))} +} +export function validateOverlayConfiguration(state:OverlayState,opts:OverlayOptions) { + if(!trafficFlowOverlayPrefixSchema.safeParse(state.settings.prefix).success||!inPrefix(state.settings.collectorIp,state.settings.prefix))throw new McpError("UNSUPPORTED_CAPABILITY","Overlay требует IPv4-префикс и collector внутри него",409) + const endpoint=opts.publicEndpoint?.trim()||state.server.host.trim() + if(!endpoint||!trafficFlowPublicEndpointSchema.safeParse(endpoint).success)throw new McpError("INVALID_INPUT","Нужен IP или DNS endpoint без порта, URL и управляющих символов",400) + if(state.settings.hasHostPrivateKey!==state.settings.hasHostPublicKey)throw new McpError("STATE_CONFLICT","Неполная пара ключей хоста; исправьте отдельно",409) + const names=(state.router["/interface"]??[]).filter(row=>row.name==="wg-flow"),ifaces=(state.router["/interface/wireguard"]??[]).filter(row=>row.name==="wg-flow") + if(names.length>1||ifaces.length>1||names.length&&!ifaces.length)throw new McpError("STATE_CONFLICT","Имя wg-flow занято или неоднозначно",409) + if(ifaces.length&&(ifaces[0]["listen-port"]!=="13232"||ifaces[0].disabled==="true"))throw new McpError("STATE_CONFLICT","Существующий wg-flow выключен или слушает другой порт",409) + for(const path of ["/ip/traffic-flow","/ip/traffic-flow/ipfix"])if((state.router[path]?.length??0)>1)throw new McpError("STATE_UNAVAILABLE","Не подтверждён singleton Traffic Flow",409) + if(opts.disableGreFastPath&&(state.router["/interface/gre"]?.length??0)>100)throw new McpError("STATE_UNAVAILABLE","Более 100 GRE: разделите изменение fast-path",409) + if(opts.disableGreFastPath&&(state.router["/interface/gre"]??[]).some(row=>row.dynamic==="true"||!row[".id"]))throw new McpError("PROTECTED_OBJECT","Динамический или неопознанный GRE нельзя менять",409) + const address=state.server.mgmtTunnelIp||allocateOverlayAddress(state.settings.prefix,state.settings.collectorIp,state.server.id,new Set(state.catalog.map(row=>row.mgmtTunnelIp).filter(Boolean))) + if(!inPrefix(address,state.settings.prefix)||address===state.settings.collectorIp||state.catalog.some(row=>row.id!==state.server.id&&row.mgmtTunnelIp===address))throw new McpError("STATE_CONFLICT","Адрес overlay конфликтует или находится вне префикса",409) + const unique=(path:string,predicate:(row:OverlayRow)=>boolean,valid:(row:OverlayRow)=>boolean)=>{ + const rows=(state.router[path]??[]).filter(predicate) + if(rows.length>1||rows.some(row=>row.disabled==="true"||!row[".id"]||!valid(row)))throw new McpError("STATE_CONFLICT","Существующий объект overlay неоднозначен или несовместим: "+path,409) + } + unique("/ip/address",row=>row.interface==="wg-flow",row=>row.address===address+"/"+state.settings.prefix.split("/")[1]) + unique("/ip/route",row=>row["dst-address"]===state.settings.collectorIp+"/32",row=>row.gateway==="wg-flow") + unique("/ip/firewall/filter",row=>row.comment==="mm-wg-flow",row=>row.chain==="input"&&row.protocol==="udp"&&row["dst-port"]==="13232"&&row.action==="accept") + unique("/ip/traffic-flow/target",row=>row["dst-address"]===state.settings.collectorIp,()=>true) + unique("/interface/wireguard/peers",row=>row.interface==="wg-flow"&&row["public-key"]===`proof:${state.settings.hostPublicKeyProof}`,()=>true) + return endpoint +} diff --git a/backend/src/services/traffic-flow-overlay.ts b/backend/src/services/traffic-flow-overlay.ts index a6df6afa..2ef0402a 100644 --- a/backend/src/services/traffic-flow-overlay.ts +++ b/backend/src/services/traffic-flow-overlay.ts @@ -1,33 +1,4 @@ -import { eq } from "drizzle-orm" -import { db } from "../db/index.js" -import { servers } from "../db/schema.js" import type { TrafficFlowOverlayResult } from "@mmapp/contracts/traffic-flow" -import { encodeRosId, MikrotikClient, MikrotikError } from "./mikrotik.js" -import { getEnabledServerById, listWireGuardInterfaces } from "./wireguard-live.js" -import { - asRosArray, - patchRosPath, - putIpAddress, - putWireguardInterface, - putWireguardPeer, - rosRowId, - toRosBody, -} from "./wireguard-ros.js" -import { - enableTrafficFlowIngest, - ensureHostKeys, - getTrafficFlowSettingsRow, - upsertHostPeer, -} from "./traffic-flow-settings.js" -import { refreshFlowExporterMap, startTrafficFlowListener } from "./traffic-flow-ingest.js" -import { listTrafficFlowHostFiles } from "./traffic-flow-host-files.js" -import { invalidateFlowCatalogCache } from "./traffic-flow-topology.js" -import { agentSocket } from "./traffic-flow-agent-client.js" -import { getFlowHostStatus, preflightFlowHost, reconcileFlowHost, withFlowConfigurationLock } from "./traffic-flow-host-agent.js" - -const IFACE_NAME = "wg-flow" -const JH_LISTEN_PORT = 13232 -const WG_INPUT_COMMENT = "mm-wg-flow" export function allocateOverlayAddress(prefix: string, collectorIp: string, serverId: number, taken: Set): string { const [base] = prefix.split("/") @@ -46,150 +17,10 @@ export function allocateOverlayAddress(prefix: string, collectorIp: string, serv throw new Error("Нет свободных адресов в префиксе wg-flow") } -function linuxPeerBlock(publicKey: string, address: string, comment: string, endpoint: string): string { - return [ - `[Peer]`, - `PublicKey = ${publicKey}`, - `AllowedIPs = ${address}/32`, - `Endpoint = ${endpoint}:${JH_LISTEN_PORT}`, - `PersistentKeepalive = 25`, - comment ? `# ${comment}` : "", - ].filter(Boolean).join("\n") -} - -async function findIface(client: MikrotikClient, name: string): Promise | undefined> { - const list = asRosArray>(await client.get("/interface/wireguard")) - return list.find((i) => String(i.name ?? "") === name) -} - -async function findPeer( - client: MikrotikClient, - iface: string, - publicKey: string, -): Promise | undefined> { - const list = asRosArray>(await client.get("/interface/wireguard/peers")) - return list.find((p) => - String(p.interface ?? "") === iface && String(p["public-key"] ?? "") === publicKey, - ) -} - -async function findAddress(client: MikrotikClient, iface: string): Promise | undefined> { - const list = asRosArray>(await client.get("/ip/address")) - return list.find((a) => String(a.interface ?? "") === iface) -} - -async function findRoute(client: MikrotikClient, dst: string): Promise | undefined> { - const list = asRosArray>(await client.get("/ip/route")) - return list.find((r) => String(r["dst-address"] ?? "") === dst) -} - -async function ensureWgInputAccept(client: MikrotikClient, listenPort: number): Promise { - const rules = asRosArray>(await client.get("/ip/firewall/filter")) - const existing = rules.find((r) => String(r.comment ?? "") === WG_INPUT_COMMENT) - if (existing) return false - await client.put("/ip/firewall/filter", toRosBody({ - chain: "input", - protocol: "udp", - "dst-port": String(listenPort), - action: "accept", - comment: WG_INPUT_COMMENT, - })) - return true -} - -/** Официальный авто-source UDP IPFIX, не фильтр 0.0.0.0/0. */ export const FLOW_TARGET_SRC_AUTO = "0.0.0.0" -async function ensureIpfixFields(client: MikrotikClient): Promise { - const body = toRosBody({ - bytes: "yes", - packets: "yes", - "src-address": "yes", - "dst-address": "yes", - protocol: "yes", - "src-port": "yes", - "dst-port": "yes", - "in-interface": "yes", - "out-interface": "yes", - gateway: "yes", - "first-forwarded": "yes", - "last-forwarded": "yes", - "nat-src-address": "yes", - "nat-dst-address": "yes", - "nat-src-port": "yes", - "nat-dst-port": "yes", - }) - const rows = asRosArray>(await client.get("/ip/traffic-flow/ipfix")) - const id = rows[0] ? rosRowId(rows[0]) : "" - if (id) { - await patchRosPath(client, `/ip/traffic-flow/ipfix/${encodeRosId(id)}`, body) - return - } - await client.post("/ip/traffic-flow/ipfix/set", body) -} - -/** Максимум flows в RAM (docs: overflow обрезает новые 5-tuple). */ export const FLOW_CACHE_ENTRIES = "256k" -async function ensureTrafficFlow( - client: MikrotikClient, - collectorIp: string, - port: number, -): Promise { - const body = toRosBody({ - enabled: "yes", - interfaces: "all", - "cache-entries": FLOW_CACHE_ENTRIES, - "active-flow-timeout": "1m", - "inactive-flow-timeout": "15s", - }) - const rows = asRosArray>(await client.get("/ip/traffic-flow")) - const id = rows[0] ? rosRowId(rows[0]) : "" - if (id) { - await patchRosPath(client, `/ip/traffic-flow/${encodeRosId(id)}`, body) - } else { - await client.post("/ip/traffic-flow/set", body) - } - - try { - await ensureIpfixFields(client) - } catch { - /* поля IPFIX опциональны на старых ROS */ - } - - const targets = asRosArray>(await client.get("/ip/traffic-flow/target")) - const existing = targets.find((t) => String(t["dst-address"] ?? "") === collectorIp) - const targetBody = toRosBody({ - "dst-address": collectorIp, - "src-address": FLOW_TARGET_SRC_AUTO, - port: String(port), - version: "ipfix", - }) - if (existing) { - const targetId = rosRowId(existing) - if (targetId) await patchRosPath(client, `/ip/traffic-flow/target/${encodeRosId(targetId)}`, targetBody) - return - } - await client.put("/ip/traffic-flow/target", targetBody) -} - -/** GRE allow-fast-path=no: inner пакеты идут через CPU и попадают в Traffic Flow. Нагрузка на CPU. */ -export async function ensureGreSlowPath(client: MikrotikClient): Promise { - const rows = asRosArray>(await client.get("/interface/gre")) - let patched = 0 - for (const row of rows) { - const id = rosRowId(row) - if (!id) continue - const current = String(row["allow-fast-path"] ?? "true").toLowerCase() - if (current === "false" || current === "no") continue - await patchRosPath(client, `/interface/gre/${encodeRosId(id)}`, toRosBody({ - "allow-fast-path": "no", - })) - patched += 1 - } - return patched -} - export function usablePublicHost(raw: string | undefined): string { if (!raw) return "" const host = raw.split(",")[0]?.trim().replace(/^\[/, "").replace(/\]:\d+$/, "").split(":")[0]?.trim() ?? "" @@ -205,179 +36,9 @@ export async function applyFlowOverlay( serverIdRaw: string | number, opts?: { publicEndpoint?: string; requestHost?: string; disableGreFastPath?: boolean }, ): Promise { - return withFlowConfigurationLock(() => applyFlowOverlayLocked(serverIdRaw, opts)) -} - -async function applyFlowOverlayLocked( - serverIdRaw: string | number, - opts?: { publicEndpoint?: string; requestHost?: string; disableGreFastPath?: boolean }, -): Promise { - const steps: string[] = [] - await preflightFlowHost() - const keys = await ensureHostKeys() - const settings = await getTrafficFlowSettingsRow() - const hostPublicKey = settings.hostPublicKey || keys.publicKey - if (!hostPublicKey) { - throw Object.assign(new Error("Не удалось создать ключи хоста MM"), { statusCode: 500 }) - } - - const server = await getEnabledServerById(String(serverIdRaw)) - if (!server || !server.enabled) { - throw Object.assign(new Error("Сервер не найден или выключен"), { statusCode: 404 }) - } - - const endpointHost = (opts?.publicEndpoint?.trim() || server.host.trim()).trim() - if (!endpointHost) { - throw Object.assign(new Error("Укажите публичный IP или DNS jump-host"), { statusCode: 400 }) - } - const peerEndpoint = `${endpointHost}:${JH_LISTEN_PORT}` - - const takenRows = await db.select({ ip: servers.mgmtTunnelIp }).from(servers) - const taken = new Set(takenRows.map((r) => r.ip).filter((ip): ip is string => Boolean(ip))) - const address = server.mgmtTunnelIp || allocateOverlayAddress(settings.prefix, settings.collectorIp, server.id, taken) - const client = MikrotikClient.fromServer(server) - - try { - let iface = await findIface(client, IFACE_NAME) - if (!iface) { - await putWireguardInterface(client, { - name: IFACE_NAME, - "listen-port": String(JH_LISTEN_PORT), - mtu: "1420", - comment: "MikrotikManager traffic-flow overlay", - }) - steps.push(`Создан интерфейс ${IFACE_NAME}`) - iface = await findIface(client, IFACE_NAME) - } else { - steps.push(`Интерфейс ${IFACE_NAME} уже есть`) - } - - const addrRow = await findAddress(client, IFACE_NAME) - const mask = (settings.prefix.split("/")[1] || "24").replace(/\D/g, "") || "24" - const cidr = `${address}/${mask}` - if (!addrRow) { - await putIpAddress(client, cidr, IFACE_NAME) - steps.push(`Адрес ${cidr}`) - } else { - steps.push(`Адрес на ${IFACE_NAME} уже назначен`) - } - - const peer = await findPeer(client, IFACE_NAME, hostPublicKey) - const peerBody = { - interface: IFACE_NAME, - "public-key": hostPublicKey, - "allowed-address": `${settings.collectorIp}/32`, - comment: "MM traffic-flow collector", - name: "mm-collector", - } - if (!peer) { - await putWireguardPeer(client, peerBody) - steps.push("Добавлен пир на pubkey хоста MM (сервер, без endpoint)") - } else { - const id = rosRowId(peer) - const hadEndpoint = Boolean(String(peer["endpoint-address"] ?? "").trim()) - if (hadEndpoint && id) { - await client.delete(`/interface/wireguard/peers/${encodeURIComponent(id)}`) - await putWireguardPeer(client, peerBody) - steps.push("Пир пересоздан как сервер (endpoint снят)") - } else if (id) { - await patchRosPath(client, `/interface/wireguard/peers/${encodeURIComponent(id)}`, peerBody) - steps.push("Пир хоста MM обновлён") - } - } - - const routeDst = `${settings.collectorIp}/32` - const route = await findRoute(client, routeDst) - if (!route) { - await client.put("/ip/route", toRosBody({ - "dst-address": routeDst, - gateway: IFACE_NAME, - comment: "MM traffic-flow collector", - })) - steps.push(`Маршрут ${routeDst} через ${IFACE_NAME}`) - } else { - steps.push("Маршрут до collector уже есть") - } - - if (await ensureWgInputAccept(client, JH_LISTEN_PORT)) { - steps.push(`Firewall input accept UDP ${JH_LISTEN_PORT}`) - } else { - steps.push("Firewall input WG уже есть") - } - - await ensureTrafficFlow(client, settings.collectorIp, settings.flowListenPort) - steps.push(`Traffic Flow → ${settings.collectorIp}:${settings.flowListenPort} ipfix (src auto, cache ${FLOW_CACHE_ENTRIES})`) - - if (opts?.disableGreFastPath) { - try { - const n = await ensureGreSlowPath(client) - steps.push( - n > 0 - ? `GRE allow-fast-path=no (${n}) — inner IPFIX через CPU` - : "GRE already allow-fast-path=no", - ) - } catch { - steps.push("GRE allow-fast-path не изменён (нет /interface/gre)") - } - } - - const listed = await listWireGuardInterfaces({ serverId: String(server.id), includePrivateKey: false }) - const created = listed.interfaces.find((i) => i.name === IFACE_NAME) - const publicKey = created?.publicKey ?? "" - if (!publicKey) { - throw new Error("Не удалось прочитать public-key интерфейса wg-flow") - } - - await db.update(servers).set({ - mgmtTunnelIp: address, - updatedAt: new Date().toISOString(), - }).where(eq(servers.id, server.id)) - invalidateFlowCatalogCache() - - await upsertHostPeer({ - serverId: server.id, - name: server.name || server.host, - publicKey, - allowedIps: [`${address}/32`], - address, - endpoint: peerEndpoint, - }) - - await enableTrafficFlowIngest() - await startTrafficFlowListener() - await refreshFlowExporterMap() - steps.push("Коллектор IPFIX на MM включён") - - let hostStatus = undefined - let hostError: string | undefined - if (agentSocket()) { - try { - hostStatus = await reconcileFlowHost() - steps.push("Пир применён на Linux-хосте без перезапуска WireGuard") - } catch (error) { - hostError = error instanceof Error ? error.message : "Настройки хоста не применены" - hostStatus = await getFlowHostStatus() - steps.push("MikroTik настроен; требуется повторное применение настроек хоста") - } - } - - return { - ok: !hostError, - serverId: server.id, - interfaceName: IFACE_NAME, - address, - publicKey, - linuxPeerBlock: linuxPeerBlock(publicKey, address, server.name || server.host, endpointHost), - trafficFlow: true, - steps, - hostFiles: agentSocket() ? [] : await listTrafficFlowHostFiles(), - stage: hostError ? "host-pending" : agentSocket() ? "applied" : "manual", - ...(hostStatus ? { hostStatus } : {}), - ...(hostError ? { hostError } : {}), - } - } catch (e) { - const msg = e instanceof MikrotikError ? e.message : e instanceof Error ? e.message : String(e) - const err = Object.assign(new Error(`Настройка MikroTik могла быть выполнена частично: ${msg}. Проверьте состояние и повторите подключение.`), { statusCode: 502 }) - throw err - } + const id = Number(serverIdRaw) + if (!Number.isSafeInteger(id) || id <= 0) throw Object.assign(new Error("Некорректный сервер"), { statusCode: 400 }) + const { executeFlowOverlay } = await import("./traffic-flow-overlay-execution.js") + const { receipt: _receipt, ...result } = await executeFlowOverlay(id, opts ?? {}) + return result } diff --git a/backend/src/services/traffic-flow-owned-batch.test.ts b/backend/src/services/traffic-flow-owned-batch.test.ts new file mode 100644 index 00000000..3289271e --- /dev/null +++ b/backend/src/services/traffic-flow-owned-batch.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict" +import { OwnedFlowBatch } from "./traffic-flow-owned-batch.js" +const proof="a".repeat(64),payload={rows:[{bytes:10}]} +const batch=new OwnedFlowBatch("fixture", "",payload) +assert.throws(()=>batch.assertResolved(),/unresolved/) +assert.throws(()=>batch.begin(),/proof/);assert.equal(batch.state,"pending") +batch.prove(proof);assert.throws(()=>batch.prove(proof),/cannot change/) +assert.doesNotThrow(()=>batch.assertProof(proof));assert.throws(()=>batch.assertProof("b".repeat(64)),/payload changed/) +batch.begin();assert.throws(()=>batch.begin(),/running/) +assert.throws(()=>batch.assertResolved(),/running/) +batch.rolledBack();assert.equal(batch.state,"pending");assert.strictEqual(batch.payload,payload) +batch.begin();batch.unknown();assert.equal(batch.state,"unknown") +assert.throws(()=>batch.assertResolved(),/unknown/);assert.strictEqual(batch.payload,payload) +assert.throws(()=>batch.begin(),/unknown/);assert.throws(()=>batch.rolledBack(),/without/) +assert.strictEqual(batch.payload,payload) +batch.reconciled("absent-after-barrier");assert.equal(batch.state,"pending") +batch.begin();batch.committed();assert.equal(batch.state,"committed");assert.throws(()=>batch.begin(),/committed/) +assert.doesNotThrow(()=>batch.assertResolved()) +const committed=new OwnedFlowBatch("second",proof,{bytes:20});committed.begin();committed.unknown();committed.reconciled("committed") +assert.equal(committed.state,"committed");assert.throws(()=>committed.reconciled("absent-after-barrier"),/Only an unknown/) +console.log("traffic-flow-owned-batch.test.ts: proof ownership, known rollback, unknown replay refusal and explicit reconciliation transitions verified") diff --git a/backend/src/services/traffic-flow-owned-batch.ts b/backend/src/services/traffic-flow-owned-batch.ts new file mode 100644 index 00000000..e76b8f4d --- /dev/null +++ b/backend/src/services/traffic-flow-owned-batch.ts @@ -0,0 +1,40 @@ +export type OwnedBatchState = "pending" | "running" | "unknown" | "committed" +/** A detached batch is never merged with new arrivals or replayed after an + * uncertain commit. Only a barrier-backed receipt lookup can resolve unknown. */ +export class OwnedFlowBatch { + private stateValue: OwnedBatchState = "pending" + constructor(readonly id: string, private proof: string, readonly payload: T) {} + get sourceProof():string { return this.proof } + assertProof(current:string):void { + if(!this.proof||current!==this.proof)throw new Error("Owned flow batch payload changed after identification") + } + prove(proof:string):void { + if(this.stateValue!=="pending"||this.proof||!/^[a-f0-9]{64}$/.test(proof))throw new Error("Owned batch proof cannot change") + this.proof=proof + } + get state(): OwnedBatchState { return this.stateValue } + assertResolved():void { + if(this.stateValue!=="committed")throw new Error("Cannot reset or discard unresolved owned flow batch: "+this.stateValue) + } + begin(): void { + if (this.stateValue !== "pending") throw new Error("Owned flow batch cannot dispatch: " + this.stateValue) + if(!/^[a-f0-9]{64}$/.test(this.proof))throw new Error("Owned batch has no source proof") + this.stateValue = "running" + } + committed(): void { + if (this.stateValue !== "running") throw new Error("Flow commit without a running batch") + this.stateValue = "committed" + } + rolledBack(): void { + if (this.stateValue !== "running") throw new Error("Flow rollback without a running batch") + this.stateValue = "pending" + } + unknown(): void { + if (this.stateValue !== "running") throw new Error("Unknown flow outcome without a running batch") + this.stateValue = "unknown" + } + reconciled(receipt: "committed" | "absent-after-barrier"): void { + if (this.stateValue !== "unknown") throw new Error("Only an unknown batch can be reconciled") + this.stateValue = receipt === "committed" ? "committed" : "pending" + } +} diff --git a/backend/src/services/traffic-flow-owned-writer.test.ts b/backend/src/services/traffic-flow-owned-writer.test.ts new file mode 100644 index 00000000..c6a63ab9 --- /dev/null +++ b/backend/src/services/traffic-flow-owned-writer.test.ts @@ -0,0 +1,27 @@ +import assert from "node:assert/strict" +import { createFlowOwnedWriter } from "./traffic-flow-owned-writer.js" + +let arrivals=[{bytes:10}],commits=0,acks=0,identityFails=true,mode="rollback",receipt=false +const retained:unknown[]=[],writer=createFlowOwnedWriter({ + capture:()=>{if(!arrivals.length)return null;const owned=arrivals;arrivals=[];retained.push(owned);return owned}, + identify:async()=>{if(identityFails)throw Error("proof unavailable");return {id:"fixture",sourceProof:"a".repeat(64)}}, + commit:async(batch,authorize)=>{await authorize();commits++;if(mode!=="normal")throw Error(mode);return {bytes:batch.payload.reduce((sum,row)=>sum+row.bytes,0)}}, + isUnknown:error=>error instanceof Error&&error.message==="unknown", + acknowledge:(_payload,result)=>{acks+=result.bytes}, + reconcile:async()=>({committed:receipt,result:receipt?{bytes:10}:null}), +}) +await assert.rejects(writer.flush(),/proof unavailable/);assert.equal(writer.state,"captured");assert.equal(commits,0) +assert.throws(()=>writer.assertResetAllowed(),/identity/) +arrivals.push({bytes:99});identityFails=false +await assert.rejects(writer.flush(),/rollback/);assert.equal(writer.state,"pending");assert.equal(commits,1);assert.throws(()=>writer.assertResetAllowed(),/unresolved/) +mode="unknown";await assert.rejects(writer.flush(),/unknown/);assert.equal(commits,2);assert.equal(writer.state,"unknown") +await assert.rejects(writer.flush(),/unknown/);assert.equal(commits,2);assert.equal(arrivals[0].bytes,99) +assert.throws(()=>writer.assertResetAllowed(),/unknown/) +assert.deepEqual(await writer.reconcile(),{state:"pending",batchId:"fixture"});assert.equal(commits,2) +mode="normal";assert.deepEqual(await writer.flush(),{bytes:10});assert.equal(acks,10);assert.equal(writer.state,"idle") +assert.deepEqual(await writer.flush(),{bytes:99});assert.equal(acks,109);assert.equal(retained.length,2) +arrivals=[{bytes:10}];mode="unknown";await assert.rejects(writer.flush());receipt=true +assert.deepEqual(await writer.reconcile(),{state:"committed",batchId:"fixture"});assert.equal(acks,119) +const before=commits;assert.equal(await writer.flush(),null);assert.equal(commits,before) +assert.doesNotThrow(()=>writer.assertResetAllowed()) +console.log("traffic-flow-owned-writer.test.ts: proof-failure retention, arrival isolation, rollback vs unknown, reset refusal, no replay before explicit barrier-backed reconciliation, and acknowledgement once verified") diff --git a/backend/src/services/traffic-flow-owned-writer.ts b/backend/src/services/traffic-flow-owned-writer.ts new file mode 100644 index 00000000..4533fab6 --- /dev/null +++ b/backend/src/services/traffic-flow-owned-writer.ts @@ -0,0 +1,50 @@ +import { OwnedFlowBatch } from "./traffic-flow-owned-batch.js" +import { createSerialOperation } from "./serial-operation.js" + +export interface FlowOwnedWriterDependencies { + capture:()=>T|null + identify:(payload:T)=>{id:string;sourceProof:string}|Promise<{id:string;sourceProof:string}> + commit:(batch:OwnedFlowBatch,authorize:()=>Promise)=>Promise + isUnknown:(error:unknown)=>boolean + acknowledge:(payload:T,result:R)=>void + reconcile:(batch:OwnedFlowBatch,authorize:()=>Promise)=>Promise<{committed:boolean;result:R|null}> +} +/** Reusable writer state machine. It owns detached payload before the first + * asynchronous operation, preserves it on failure, and never retries unknown. */ +export function createFlowOwnedWriter(deps:FlowOwnedWriterDependencies) { + const serial=createSerialOperation() + let captured:T|null=null,batch:OwnedFlowBatch|null=null + return { + flush(authorize:()=>Promise=async()=>{}):Promise{return serial(async()=>{ + await authorize() + if(!batch){ + if(captured===null)captured=deps.capture() + if(captured===null)return null + const identity=await deps.identify(captured) + batch=new OwnedFlowBatch(identity.id,identity.sourceProof,captured) + } + const current=batch;current.begin() + try{ + const result=await deps.commit(current,authorize) + current.committed();deps.acknowledge(current.payload,result) + captured=null;batch=null;return result + }catch(error){ + // Acknowledgement callback errors must never downgrade a committed batch. + if(current.state==="running"){if(deps.isUnknown(error))current.unknown();else current.rolledBack()} + throw error + } + })}, + reconcile(authorize:()=>Promise=async()=>{}){return serial(async()=>{ + if(!batch||batch.state!=="unknown")return {state:batch?.state??(captured===null?"idle":"captured")} + const current=batch,result=await deps.reconcile(current,authorize) + current.reconciled(result.committed?"committed":"absent-after-barrier") + if(result.committed){if(result.result===null)throw new Error("Committed owned batch has no receipt result");deps.acknowledge(current.payload,result.result);captured=null;batch=null} + return {state:current.state,batchId:current.id} + })}, + assertResetAllowed(){ + if(captured!==null&&!batch)throw new Error("Cannot reset a detached batch awaiting identity") + batch?.assertResolved() + }, + get state(){return batch?.state??(captured===null?"idle":"captured")}, + } +} diff --git a/backend/src/services/traffic-flow-parse.ts b/backend/src/services/traffic-flow-parse.ts index 50c9ddfb..d0533845 100644 --- a/backend/src/services/traffic-flow-parse.ts +++ b/backend/src/services/traffic-flow-parse.ts @@ -68,30 +68,209 @@ interface FieldSpec { length: number } +/** Trusted local receiver context, never read from the exported packet. + * collectorSocketId identifies a receiver socket incarnation. A wildcard-bound + * socket does not establish the packet's precise local destination IP. */ +export interface FlowDatagramTransport { sourcePort: number; collectorSocketId: string } +function templateScope(version: number, domain: number, transport?: FlowDatagramTransport): string { + if (transport === undefined) return JSON.stringify([version, domain, "unknown-transport"]) + if (!transport || !Number.isInteger(transport.sourcePort) || transport.sourcePort < 1 || transport.sourcePort > 65535 + || typeof transport.collectorSocketId !== "string" || transport.collectorSocketId.length < 1 || transport.collectorSocketId.length > 128 + || !/^[A-Za-z0-9_-]+$/.test(transport.collectorSocketId)) throw new Error("Invalid flow datagram transport context") + return JSON.stringify([version, domain, "udp-socket", transport.sourcePort, transport.collectorSocketId]) +} +function scopedTemplateId(scope: string, id: number): string { return JSON.stringify([scope, id]) } + interface Template { fields: FieldSpec[] + refreshedAtMs: number + v9Clock?: V9Clock + v9ValidFrom?: V9Clock + v9Conflicted?: true + ipfixExportTime?: number + ipfixValidFrom?: number + ipfixConflicted?: true + ipfixScope?: string + ipfixResetMessage?: object } -const MAX_TEMPLATE_EXPORTERS = 256 -const templatesByExporter = new Map>() +type V9Clock = { exportedAt: number; uptime: number; sequence: number } +type IpfixClock = { exportTime: number; sequence: number } +type IpfixScopeState = { clock?: IpfixClock; resetAt?: number } -function templatesForExporter(exporter: string): Map { +// RFC 3954 uses uint32 milliseconds for uptime and whole seconds for export time. +function differentV9Boot(first: V9Clock, second: V9Clock): boolean { + const elapsedExportMs = (second.exportedAt - first.exportedAt) * 1000 + // Signed modular distance handles wrap and reordered datagrams. + return Math.abs((second.uptime - first.uptime - elapsedExportMs) | 0) > 2000 +} + +function compareV9Clock(first: V9Clock, second: V9Clock): number { + return first.exportedAt - second.exportedAt || + ((first.uptime - second.uptime) | 0) || ((first.sequence - second.sequence) | 0) +} + +// RouterOS refreshes reuse IDs; its configurable record fields number in tens. +// These generous retention budgets bound objects, not an asserted V8 byte size. +export const FLOW_TEMPLATE_LIMITS = Object.freeze({ exporters: 256, perExporterTemplates: 256, templates: 4096, + perTemplateFields: 256, perExporterFields: 8192, fields: 65536 }) +// RFC7011 8.4: UDP lifetime is deployment-specific; use three times the +// 30-minute refresh interval in the MikroTik MUM configuration example. +// Longer exporter refresh intervals require an explicit collector override. +// This is not active-flow-timeout, and data traffic never refreshes a template. +export const FLOW_TEMPLATE_LIFETIME_MS = Number(process.env.FLOW_TEMPLATE_LIFETIME_MS ?? 90 * 60 * 1000) +if (!Number.isSafeInteger(FLOW_TEMPLATE_LIFETIME_MS) || FLOW_TEMPLATE_LIFETIME_MS <= 0) { + throw new Error("FLOW_TEMPLATE_LIFETIME_MS must be a positive safe integer in milliseconds") +} +const MAX_TEMPLATE_EXPORTERS = FLOW_TEMPLATE_LIMITS.exporters +const templatesByExporter = new Map>() +// Each scope exists only while its exporter retains at least one template. +const ipfixScopes = new WeakMap, Map>() +const fieldsByExporter = new Map() +let templateCount = 0, fieldCount = 0, refusedTemplates = 0 +export class FlowTemplateRefused extends Error { + readonly code = "FLOW_TEMPLATE_REFUSED" + constructor(readonly reason: "fields" | "exporter-templates" | "templates" | "exporter-fields" | "total-fields" | "truncated" | "missing-template") { + super("NetFlow/IPFIX template refused: " + reason) + } +} +function refuseTemplate(reason: FlowTemplateRefused["reason"]): never { refusedTemplates++; throw new FlowTemplateRefused(reason) } +function forgetExporter(exporter: string): void { + const previous = templatesByExporter.get(exporter) + if (!previous) return + templateCount -= previous.size; fieldCount -= fieldsByExporter.get(exporter) ?? 0 + fieldsByExporter.delete(exporter); templatesByExporter.delete(exporter) +} + +/** Lazy expiry uses local monotonic time, never exporter timestamps. Retention + * remains bounded by existing caps; no sweeper timer, pending data or tombstones. + * Empty exporter maps remain in the existing bounded LRU until normal eviction. */ +function expireTemplate(exporter: string, map: Map, id: string, now: number): void { + const retained = map.get(id) + if (!retained || now - retained.refreshedAtMs < FLOW_TEMPLATE_LIFETIME_MS) return + map.delete(id); templateCount--; fieldCount -= retained.fields.length + const scope = retained.ipfixScope + if (scope !== undefined && ![...map.values()].some(template => template.ipfixScope === scope)) { + ipfixScopes.get(map)?.delete(scope) + } + fieldsByExporter.set(exporter, (fieldsByExporter.get(exporter) ?? 0) - retained.fields.length) +} +function dataTemplate(exporter: string, id: string, clock?: V9Clock, ipfixExportTime?: number, resetMessage?: object): Template { + const map = templatesByExporter.get(exporter) + if (map) expireTemplate(exporter, map, id, performance.now()) + const retained = map?.get(id) + // The existing ingress catches FlowTemplateRefused, exposes an error and + // refuses publication. An undecodable datagram is not an observed zero. + if (!retained || retained.v9Conflicted || clock && retained.v9Clock && differentV9Boot(retained.v9Clock, clock)) + refuseTemplate("missing-template") + if (clock && retained.v9ValidFrom && compareV9Clock(clock, retained.v9ValidFrom) < 0) + refuseTemplate("missing-template") + if (ipfixExportTime !== undefined && retained.ipfixValidFrom !== undefined && ipfixExportTime < retained.ipfixValidFrom) + refuseTemplate("missing-template") + const resetAt = retained.ipfixScope === undefined ? undefined : ipfixScopes.get(map!)?.get(retained.ipfixScope)?.resetAt + if (retained.ipfixConflicted) refuseTemplate("missing-template") + if (resetAt !== undefined) { + const fresh = retained.ipfixExportTime! > resetAt || + retained.ipfixExportTime === resetAt && retained.ipfixResetMessage !== undefined + const inResetMessage = resetMessage !== undefined && retained.ipfixResetMessage === resetMessage + if (!fresh || ipfixExportTime! <= resetAt && !inResetMessage) refuseTemplate("missing-template") + } + return retained +} + +function templatesForExporter(exporter: string): Map { const existing = templatesByExporter.get(exporter) if (existing) { templatesByExporter.delete(exporter) templatesByExporter.set(exporter, existing) return existing } - const created = new Map() + const created = new Map() templatesByExporter.set(exporter, created) + fieldsByExporter.set(exporter, 0) while (templatesByExporter.size > MAX_TEMPLATE_EXPORTERS) { const oldest = templatesByExporter.keys().next().value if (oldest == null || oldest === exporter) break - templatesByExporter.delete(oldest) + forgetExporter(oldest) } return created } +/** Check retention budgets after bounded field staging and body validation. + * The per-template count is checked before staging. A rejected replacement leaves + * the complete old template and all accounting intact. No await splits commit. */ +function admitTemplate(exporter: string, map: Map, id: string, count: number): void { + if (count > FLOW_TEMPLATE_LIMITS.perTemplateFields) refuseTemplate("fields") + const now = performance.now() + for (const retainedId of map.keys()) expireTemplate(exporter, map, retainedId, now) + const previous = map.get(id), added = previous ? 0 : 1, delta = count - (previous?.fields.length ?? 0) + // Preview eviction without changing any retained state until validation succeeds. + const victim = !templatesByExporter.has(exporter) && templatesByExporter.size >= MAX_TEMPLATE_EXPORTERS + ? templatesByExporter.keys().next().value : undefined + const releasedTemplates = victim === undefined ? 0 : templatesByExporter.get(victim)?.size ?? 0 + const releasedFields = victim === undefined ? 0 : fieldsByExporter.get(victim) ?? 0 + if (map.size + added > FLOW_TEMPLATE_LIMITS.perExporterTemplates) refuseTemplate("exporter-templates") + if (templateCount - releasedTemplates + added > FLOW_TEMPLATE_LIMITS.templates) refuseTemplate("templates") + if ((fieldsByExporter.get(exporter) ?? 0) + delta > FLOW_TEMPLATE_LIMITS.perExporterFields) refuseTemplate("exporter-fields") + if (fieldCount - releasedFields + delta > FLOW_TEMPLATE_LIMITS.fields) refuseTemplate("total-fields") +} +function storeTemplate(exporter: string, map: Map, id: string, fields: FieldSpec[], v9Clock?: V9Clock, ipfix?: { scope: string; exportTime: number; resetMessage?: object }): void { + const previous = map.get(id), delta = fields.length - (previous?.fields.length ?? 0) + const ipfixExportTime = ipfix?.exportTime + const resetAt = ipfix && ipfixScopes.get(map)?.get(ipfix.scope)?.resetAt + if (v9Clock && previous?.v9Clock && previous.v9Clock.exportedAt > v9Clock.exportedAt) return + if (ipfixExportTime !== undefined && previous?.ipfixExportTime !== undefined && previous.ipfixExportTime > ipfixExportTime) return + if (ipfixExportTime !== undefined && resetAt !== undefined && ipfixExportTime <= resetAt && !ipfix?.resetMessage) return + const sameLayout = previous && delta === 0 && fields.every((field, index) => + field.type === previous.fields[index].type && field.length === previous.fields[index].length) + if (v9Clock && previous?.v9Clock?.exportedAt === v9Clock.exportedAt) { + if (previous.v9Conflicted || previous.fields.length > 0 && fields.length > 0 && + differentV9Boot(previous.v9Clock, v9Clock)) { + previous.v9Conflicted = true + return + } + if (compareV9Clock(v9Clock, previous.v9Clock) < 0) return + } + // Export Time has one-second resolution. Conflicting definitions within + // that second cannot be ordered using the message timestamp. + if (previous && ipfixExportTime !== undefined && previous.ipfixExportTime === ipfixExportTime && + (!sameLayout || previous.ipfixConflicted)) { + previous.ipfixConflicted = true + return + } + if (!previous) templateCount++ + fieldCount += delta; fieldsByExporter.set(exporter, (fieldsByExporter.get(exporter) ?? 0) + delta) + if (ipfix) { + let scopes = ipfixScopes.get(map) + if (!scopes) ipfixScopes.set(map, scopes = new Map()) + if (!scopes.has(ipfix.scope)) scopes.set(ipfix.scope, {}) + } + // A refresh of the same layout must not move the first compatible packet. + const v9ValidFrom = v9Clock && previous?.v9Clock + ? sameLayout && !previous.v9Conflicted && !differentV9Boot(previous.v9Clock, v9Clock) + ? previous.v9ValidFrom : v9Clock + : undefined + map.set(id, { fields, refreshedAtMs: performance.now(), v9Clock, v9ValidFrom, ipfixExportTime, + ipfixScope: ipfix?.scope, + ipfixResetMessage: ipfix?.resetMessage, + ipfixValidFrom: sameLayout && !previous.ipfixConflicted && + (resetAt === undefined || previous.ipfixExportTime! > resetAt) ? previous.ipfixValidFrom : ipfixExportTime }) +} +function templateFields(buf: Buffer, offset: number, end: number, count: number, enterprise: boolean) { + const fields: FieldSpec[] = [] + for (let index = 0; index < count; index++) { + if (offset + 4 > end) refuseTemplate("truncated") + const type = buf.readUInt16BE(offset), length = buf.readUInt16BE(offset + 2) + offset += 4 + if (enterprise && type & 0x8000) { + if (offset + 4 > end) refuseTemplate("truncated") + offset += 4 + } + fields.push({ type: enterprise ? type & 0x7fff : type, length }) + } + return { fields, next: offset } +} + function ipv4(buf: Buffer, offset: number): string { return `${buf[offset]}.${buf[offset + 1]}.${buf[offset + 2]}.${buf[offset + 3]}` } @@ -180,32 +359,30 @@ function parseNetflowV5(buf: Buffer): ParsedFlow[] { return out } -function parseIpfixTemplates(exporter: string, buf: Buffer, setStart: number, setEnd: number, setId: number) { +function parseIpfixTemplates(exporter: string, buf: Buffer, setStart: number, setEnd: number, setId: number, scope: string, exportTime: number, resetMessage?: object) { let off = setStart + 4 - const map = templatesForExporter(exporter) + let map: Map | undefined while (off + 4 <= setEnd) { const templateId = buf.readUInt16BE(off) const fieldCount = buf.readUInt16BE(off + 2) off += 4 + // This collector is UDP-only: RFC7011 section8.4 requires ignoring + // withdrawals, including the four-byte Options withdrawal record. + if (fieldCount === 0) continue + if (fieldCount > FLOW_TEMPLATE_LIMITS.perTemplateFields) refuseTemplate("fields") if (setId === 3) { // options template: skip scope count - if (off + 2 > setEnd) break + if (off + 2 > setEnd) refuseTemplate("truncated") off += 2 } - const fields: FieldSpec[] = [] - for (let i = 0; i < fieldCount && off + 4 <= setEnd; i++) { - const type = buf.readUInt16BE(off) - const length = buf.readUInt16BE(off + 2) - off += 4 - if (type & 0x8000) { - if (off + 4 > setEnd) break - off += 4 - } - fields.push({ type: type & 0x7fff, length }) + const parsed = templateFields(buf, off, setEnd, fieldCount, true) + off = parsed.next + if (templateId >= 256) { + admitTemplate(exporter, map ?? templatesByExporter.get(exporter) ?? new Map(), scopedTemplateId(scope, templateId), fieldCount) + map ??= templatesForExporter(exporter) + storeTemplate(exporter, map, scopedTemplateId(scope, templateId), parsed.fields, undefined, { scope, exportTime, resetMessage }) } - if (templateId >= 256) map.set(templateId, { fields }) } - templatesByExporter.set(exporter, map) } function recordFromFields( @@ -266,14 +443,26 @@ function recordFromFields( break case 225: if (data.length === 4) { - natSrc = ipv4(data, 0) - if (!usableIpfixIp(src) && usableIpfixIp(natSrc)) src = natSrc + const value = ipv4(data, 0) + if (usableIpfixIp(value) || !usableIpfixIp(natSrc)) natSrc = value } break case 226: if (data.length === 4) { - natDst = ipv4(data, 0) - if (!usableIpfixIp(dst) && usableIpfixIp(natDst)) dst = natDst + const value = ipv4(data, 0) + if (usableIpfixIp(value) || !usableIpfixIp(natDst)) natDst = value + } + break + case 281: + if (data.length === 16 && !usableIpfixIp(natSrc)) { + const value = ipv6(data, 0) + if (usableIpfixIp(value)) natSrc = value + } + break + case 282: + if (data.length === 16 && !usableIpfixIp(natDst)) { + const value = ipv6(data, 0) + if (usableIpfixIp(value)) natDst = value } break case 227: @@ -336,6 +525,8 @@ function recordFromFields( off = field.next } if (ifaceName && !inIface) inIface = ifaceName + if (natSrc && !usableIpfixIp(src) && usableIpfixIp(natSrc)) src = natSrc + if (natDst && !usableIpfixIp(dst) && usableIpfixIp(natDst)) dst = natDst return { flow: normalizeParsedFlow({ src, dst, proto, srcPort, dstPort, bytes, packets, inIface, outIface, nextHop, flowStartMs, flowEndMs, natSrc, natDst, natSrcPort, natDstPort, @@ -362,34 +553,67 @@ function parseDataRecords( } } -function parseIpfix(buf: Buffer, exporter: string): ParsedFlow[] { +function parseIpfix(buf: Buffer, exporter: string, scope: string): ParsedFlow[] { if (buf.length < 16) return [] const total = buf.readUInt16BE(2) + const exportTime = buf.readUInt32BE(4) + const sequence = buf.readUInt32BE(8) + const map = templatesByExporter.get(exporter) + const state = map && ipfixScopes.get(map)?.get(scope) + const previous = state?.clock + const delta = previous ? (sequence - previous.sequence) >>> 0 : 0 + const regressed = state && previous && exportTime > previous.exportTime && delta > 0x80000000 const end = Math.min(buf.length, total) + let resetMessage: object | undefined + if (regressed) { + for (let start = 16; start + 4 <= end;) { + const length = buf.readUInt16BE(start + 2) + if (length < 4 || start + length > end) break + if (buf.readUInt16BE(start) >= 256) { + resetMessage = {} + break + } + start += length + } + } + if (regressed && resetMessage) { + state.resetAt = exportTime + state.clock = undefined + } let off = 16 const out: ParsedFlow[] = [] + let hasData = false while (off + 4 <= end) { const setId = buf.readUInt16BE(off) const setLen = buf.readUInt16BE(off + 2) if (setLen < 4 || off + setLen > end) break const setEnd = off + setLen if (setId === 2 || setId === 3) { - parseIpfixTemplates(exporter, buf, off, setEnd, setId) + parseIpfixTemplates(exporter, buf, off, setEnd, setId, scope, exportTime, resetMessage) } else if (setId >= 256) { - const tpl = templatesByExporter.get(exporter)?.get(setId) - if (tpl) parseDataRecords(tpl, buf, off + 4, setEnd, out) + const tpl = dataTemplate(exporter, scopedTemplateId(scope, setId), undefined, exportTime, resetMessage) + parseDataRecords(tpl, buf, off + 4, setEnd, out) + hasData = true } off = setEnd } + const currentMap = templatesByExporter.get(exporter) + const current = currentMap && ipfixScopes.get(currentMap)?.get(scope) + if (hasData && current && (!current.clock || + exportTime >= current.clock.exportTime && ((sequence - current.clock.sequence) >>> 0) < 0x80000000)) { + current.clock = { exportTime, sequence } + } return out } -function parseNetflowV9(buf: Buffer, exporter: string): ParsedFlow[] { +function parseNetflowV9(buf: Buffer, exporter: string, scope: string): ParsedFlow[] { if (buf.length < 20) return [] const count = buf.readUInt16BE(2) + const clock = { exportedAt: buf.readUInt32BE(8), uptime: buf.readUInt32BE(4), sequence: buf.readUInt32BE(12) } let off = 20 const out: ParsedFlow[] = [] - const map = templatesForExporter(exporter) + let map = templatesByExporter.get(exporter) + if (map) templatesForExporter(exporter) for (let s = 0; s < count && off + 4 <= buf.length; s++) { const setId = buf.readUInt16BE(off) const setLen = buf.readUInt16BE(off + 2) @@ -401,29 +625,40 @@ function parseNetflowV9(buf: Buffer, exporter: string): ParsedFlow[] { const templateId = buf.readUInt16BE(tOff) const fieldCount = buf.readUInt16BE(tOff + 2) tOff += 4 - const fields: FieldSpec[] = [] - for (let i = 0; i < fieldCount && tOff + 4 <= setEnd; i++) { - fields.push({ type: buf.readUInt16BE(tOff), length: buf.readUInt16BE(tOff + 2) }) - tOff += 4 + // Preserve v9's existing zero-field replacement semantics. RFC7011's + // UDP withdrawal-ignore rule applies to IPFIX, not the v9 format. + if (fieldCount === 0) { + if (templateId >= 256) { + admitTemplate(exporter, map ?? new Map(), scopedTemplateId(scope, templateId), 0) + map ??= templatesForExporter(exporter); storeTemplate(exporter, map, scopedTemplateId(scope, templateId), [], clock) + } + continue + } + if (fieldCount > FLOW_TEMPLATE_LIMITS.perTemplateFields) refuseTemplate("fields") + const parsed = templateFields(buf, tOff, setEnd, fieldCount, false) + tOff = parsed.next + if (templateId >= 256) { + admitTemplate(exporter, map ?? new Map(), scopedTemplateId(scope, templateId), fieldCount) + map ??= templatesForExporter(exporter); storeTemplate(exporter, map, scopedTemplateId(scope, templateId), parsed.fields, clock) } - if (templateId >= 256) map.set(templateId, { fields }) } - templatesByExporter.set(exporter, map) + if (map) templatesForExporter(exporter) } else if (setId >= 256) { - const tpl = map.get(setId) - if (tpl) parseDataRecords(tpl, buf, off + 4, setEnd, out) + const tpl = dataTemplate(exporter, scopedTemplateId(scope, setId), clock) + parseDataRecords(tpl, buf, off + 4, setEnd, out) } off = setEnd } return out } -export function parseFlowPacket(buf: Buffer, exporterIp: string): ParsedFlow[] { +export function parseFlowPacket(buf: Buffer, exporterIp: string, transport?: FlowDatagramTransport): ParsedFlow[] { if (buf.length < 2) return [] const version = buf.readUInt16BE(0) if (version === 5) return parseNetflowV5(buf) - if (version === 9) return parseNetflowV9(buf, exporterIp) - if (version === 10) return parseIpfix(buf, exporterIp) + // Validate receiver context before ANY template access/LRU touch. + if (version === 9 && buf.length >= 20) return parseNetflowV9(buf, exporterIp, templateScope(version, buf.readUInt32BE(16), transport)) + if (version === 10 && buf.length >= 16) return parseIpfix(buf, exporterIp, templateScope(version, buf.readUInt32BE(12), transport)) return [] } @@ -440,8 +675,20 @@ export function protoName(proto: number): string { } } -export function resetFlowTemplatesForTests() { +/** One collector socket per isolate. Call only after invalidating its predecessor's + * callbacks; parsed flow rows and owned SQL batches do not retain templates. */ +export function clearFlowTemplateCache(): void { templatesByExporter.clear() + fieldsByExporter.clear(); templateCount = 0; fieldCount = 0 +} +export function resetFlowTemplatesForTests(): void { + clearFlowTemplateCache(); refusedTemplates = 0 +} + +export function templateCacheUsageForTests(exporter?: string) { + return { exporters: templatesByExporter.size, templates: templateCount, fields: fieldCount, refusedTemplates, + exporterTemplates: exporter === undefined ? null : templatesByExporter.get(exporter)?.size ?? 0, + exporterFields: exporter === undefined ? null : fieldsByExporter.get(exporter) ?? 0 } } export function templateExporterCountForTests(): number { diff --git a/backend/src/services/traffic-flow-protocol-scope-fixture.ts b/backend/src/services/traffic-flow-protocol-scope-fixture.ts new file mode 100644 index 00000000..5e31d564 --- /dev/null +++ b/backend/src/services/traffic-flow-protocol-scope-fixture.ts @@ -0,0 +1,74 @@ +/** Extract the real ingress and listener bodies; only SQL, metadata and timers + * are inert. Native mode uses actual localhost UDP sockets. */ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createSocket, type Socket } from "node:dgram" +import { readFileSync } from "node:fs" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import { FlowTemplateRefused, parseFlowPacket, clearFlowTemplateCache, type ParsedFlow, type FlowDatagramTransport } from "./traffic-flow-parse.js" + +type Ingress = { ingest: (packet: Buffer, exporter: string, transport?: FlowDatagramTransport) => boolean; queued: ParsedFlow[] } +type FixtureSocket = Socket & { emitPacket: (packet: Buffer, sourcePort: number) => void } +type WorkerAdapter = { start: (payload: unknown) => Promise } +type WorkerDependencies = { + create: () => Socket | object + id: () => string + clear: () => void + error: (message: string) => void + ingest: (packet: Buffer, exporter: string, transport: FlowDatagramTransport) => boolean +} + +function functionSource(file: string, name: string): string { + const source=readFileSync(new URL(file,import.meta.url),"utf8") + const ast=ts.createSourceFile(file,source,ts.ScriptTarget.ES2022,true) + const node=ast.statements.find(value=>ts.isFunctionDeclaration(value)&&value.name?.text===name) + assert(node,"Production function exists: "+name) + return ts.transpileModule(node.getText(ast),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText +} + +export function makeIngress(): Ingress { + const exports: { ingestDatagram?: Ingress["ingest"] } = {}, queued: ParsedFlow[]=[] + runInNewContext(functionSource("./traffic-flow-engine.ts","ingestDatagram"),{ + exports,Buffer,FlowTemplateRefused,parseFlowPacket,bumpPacketMeta:()=>{}, + resolveServerId:(exporter:string)=>exporter==="known"||exporter==="127.0.0.1"?1:null, + setEngineError:()=>{},maybeRefreshIfaces:()=>{},queueParsedFlows:(_server:number,flows:ParsedFlow[])=>queued.push(...flows), + }) + assert(exports.ingestDatagram) + return {ingest:exports.ingestDatagram,queued} +} + +export function makeWorker(ingress: Ingress, real=false) { + const sockets: FixtureSocket[]=[],contexts: FlowDatagramTransport[]=[],errors: string[]=[] + const waiters: Array<{count:number;done:()=>void}>=[] + function create(): Socket | object { + if(real){const socket=createSocket("udp4");sockets.push(socket as FixtureSocket);return socket} + const handlers=new Mapvoid>() + const socket={ + on:(type:string,callback:(...args:unknown[])=>void):unknown=>{handlers.set(type,callback);return socket}, + bind:(_port:number,_host:string,callback:()=>void)=>callback(),setRecvBufferSize:()=>{},close:()=>{}, + emitPacket:(packet:Buffer,port:number)=>handlers.get("message")?.(packet,{address:"known",port}), + } + sockets.push(socket as unknown as FixtureSocket);return socket + } + const factory=new Function("deps",` + let socket=null,bound=false,address=null,shuttingDown=false,startingAuthorized=false; + const createSocket=deps.create,randomUUID=deps.id,ingestDatagram=deps.ingest,clearFlowTemplateCache=deps.clear; + const setEngineError=deps.error,send=()=>{},configureEngine=()=>{},applyExporterMap=()=>{},heartbeat=()=>{}; + const stopListener=()=>{if(socket)socket.close();socket=null},quiesceListener=stopListener; + const setFlowEngineLifecycleAuthorization=()=>{},flushPending=async()=>{},startFlowFlushTimer=()=>{}; + ${functionSource("./traffic-flow-collector-worker.ts","startListener")} + return {start:startListener}; + `) as (deps:WorkerDependencies)=>WorkerAdapter + const adapter=factory({create,id:randomUUID,clear:clearFlowTemplateCache,error:value=>errors.push(value),ingest:(packet,exporter,context)=>{ + contexts.push(context) + const result=ingress.ingest(packet,exporter,context) + for(const waiter of waiters)if(contexts.length>=waiter.count)waiter.done() + return result + }}) + const waitForContexts=(count:number)=>contexts.length>=count?Promise.resolve():new Promise((resolve,reject)=>{ + const timer=setTimeout(()=>reject(Error("Owned localhost UDP receive timeout "+JSON.stringify({expected:count,actual:contexts.length,errors}))),2000) + waiters.push({count,done:()=>{clearTimeout(timer);resolve()}}) + }) + return {...adapter,sockets,contexts,waitForContexts} +} diff --git a/backend/src/services/traffic-flow-protocol-scope.test.ts b/backend/src/services/traffic-flow-protocol-scope.test.ts new file mode 100644 index 00000000..29145ff6 --- /dev/null +++ b/backend/src/services/traffic-flow-protocol-scope.test.ts @@ -0,0 +1,175 @@ +/** Exercise protocol scopes with the actual parser and production receiver bodies. + * The fixture keeps SQL/metadata inert and can use native localhost UDP sockets. */ +import assert from "node:assert/strict" +import { test } from "node:test" +import { parseFlowPacket, clearFlowTemplateCache, resetFlowTemplatesForTests, templateCacheUsageForTests, FLOW_TEMPLATE_LIMITS as limits, FlowTemplateRefused } from "./traffic-flow-parse.js" +import { makeIngress, makeWorker } from "./traffic-flow-protocol-scope-fixture.js" +import { createSocket } from "node:dgram" + +type Transport = { sourcePort: number; collectorSocketId: string } +const known: Transport = { sourcePort: 2055, collectorSocketId: "owned_socket_a" } +function packet(version: 9 | 10, domain: number, setId: number, body: Buffer) { + const size=version===9?20:16,value=Buffer.alloc(size+4+body.length) + value.writeUInt16BE(version);value.writeUInt16BE(version===9?1:value.length,2) + value.writeUInt32BE(domain,version===9?16:12);value.writeUInt16BE(setId,size);value.writeUInt16BE(body.length+4,size+2);body.copy(value,size+4) + return value +} +function template(version:9|10,domain:number,reverse=false,count=2,id=256){ + const body=Buffer.alloc(4+count*4);body.writeUInt16BE(id);body.writeUInt16BE(count,2) + for(let i=0;ipacket(version,domain,256,Buffer.from([192,0,2,1,198,51,100,1])) +const decode=(version:9|10,domain:number,transport?:Transport)=>parseFlowPacket(data(version,domain),"known",transport)[0]?.src + +test("IPFIX observation domains sharing a numeric template ID retain independent field definitions",()=>{ + resetFlowTemplatesForTests() + parseFlowPacket(template(10,1),"known",known);parseFlowPacket(template(10,2,true),"known",known) + assert.equal(decode(10,1,known),"192.0.2.1");assert.equal(decode(10,2,known),"198.51.100.1") + assert.equal(templateCacheUsageForTests("known").exporterTemplates,2) +}) +test("actual UDP source port scopes templates independently of exporter IP and observation domain",()=>{ + resetFlowTemplatesForTests() + const other={...known,sourcePort:2056} + parseFlowPacket(template(10,1),"known",known);parseFlowPacket(template(10,1,true),"known",other) + assert.equal(decode(10,1,known),"192.0.2.1");assert.equal(decode(10,1,other),"198.51.100.1") +}) +test("v9 source IDs and protocol versions never borrow each other's templates",()=>{ + resetFlowTemplatesForTests() + parseFlowPacket(template(9,7),"known",known);parseFlowPacket(template(9,8,true),"known",known) + parseFlowPacket(template(10,7,true),"known",known) + assert.equal(decode(9,7,known),"192.0.2.1");assert.equal(decode(9,8,known),"198.51.100.1");assert.equal(decode(10,7,known),"198.51.100.1") +}) + +test("IPFIX IPv6 NAT fields survive ingress without replacing the original addresses", () => { + resetFlowTemplatesForTests() + const ingress = makeIngress() + const fields: [number, Buffer][] = [ + [281, Buffer.from("20010db8000200000000000000000001", "hex")], + [282, Buffer.from("20010db8000200000000000000000002", "hex")], + [225, Buffer.alloc(4)], [226, Buffer.alloc(4)], + [27, Buffer.from("20010db8000100000000000000000001", "hex")], + [28, Buffer.from("20010db8000100000000000000000002", "hex")], + [10, Buffer.from([0, 0, 0, 13])], [14, Buffer.from([0, 0, 0, 42])], + [227, Buffer.from([0x14, 0xe9])], [228, Buffer.from([0x01, 0xbb])], + [1, Buffer.from([0, 0, 0x05, 0xdc])], [2, Buffer.from([0, 0, 0, 10])], + ] + for (const [index, layout] of [fields, [...fields].reverse()].entries()) { + const body = Buffer.alloc(4 + layout.length * 4) + body.writeUInt16BE(256) + body.writeUInt16BE(layout.length, 2) + layout.forEach(([id, value], offset) => { + body.writeUInt16BE(id, 4 + offset * 4) + body.writeUInt16BE(value.length, 6 + offset * 4) + }) + const domain = index + 1 + ingress.ingest(packet(10, domain, 2, body), "known", known) + ingress.ingest(packet(10, domain, 256, Buffer.concat(layout.map(([, value]) => value))), "known", known) + } + assert.equal(ingress.queued.length, 2) + for (const flow of ingress.queued) assert.deepEqual( + [flow.src, flow.dst, flow.natSrc, flow.natDst, flow.inIface, flow.outIface, flow.natSrcPort, flow.natDstPort, flow.bytes, flow.packets], + ["2001:db8:1:0:0:0:0:1", "2001:db8:1:0:0:0:0:2", "2001:db8:2:0:0:0:0:1", "2001:db8:2:0:0:0:0:2", "13", "42", 5353, 443, 1500, 10], + ) +}) +test("unknown legacy transport still works in its explicit partition and never borrows known UDP state",()=>{ + resetFlowTemplatesForTests() + parseFlowPacket(template(10,0),"known") + assert.equal(decode(10,0),"192.0.2.1");assert.throws(()=>decode(10,0,known),error=>error instanceof FlowTemplateRefused&&error.reason==="missing-template") + parseFlowPacket(template(10,0,true),"known",known) + assert.equal(decode(10,0),"192.0.2.1");assert.equal(decode(10,0,known),"198.51.100.1") + assert.throws(()=>decode(10,0,{...known,collectorSocketId:"replacement_socket"}),error=>error instanceof FlowTemplateRefused&&error.reason==="missing-template") +}) +test("invalid context refuses before cache creation, mutation or LRU touch; socket scope width is bounded",()=>{ + resetFlowTemplatesForTests() + for(let i=0;iparseFlowPacket(template(10,0),"valid-0",invalid),/transport context/) + assert.throws(()=>parseFlowPacket(template(10,0),"invalid-new",invalid),/transport context/) + } + assert.deepEqual(templateCacheUsageForTests(),before) + parseFlowPacket(template(10,0),"replacement",known) + assert.equal(templateCacheUsageForTests("valid-0").exporterTemplates,0,"invalid access must not refresh valid-0's LRU position") +}) +test("template cardinality and fields remain bounded per exporter across all domains and sessions",()=>{ + resetFlowTemplatesForTests() + for(let i=0;iparseFlowPacket(template(9,999),"known",known),error=>error instanceof FlowTemplateRefused&&error.reason==="exporter-templates") + resetFlowTemplatesForTests() + for(let i=0;iparseFlowPacket(template(10,999),"known",known),error=>error instanceof FlowTemplateRefused&&error.reason==="exporter-fields") +}) +test("receiver-owned socket identity and actual rinfo.port reach parser through real Worker/engine bodies",async()=>{ + resetFlowTemplatesForTests() + const ingress=makeIngress(),worker=makeWorker(ingress) + const payload={listenHost:"127.0.0.1",listenPort:2055,topN:20,retentionHours:24,exporterMap:{}} + await worker.start(payload) + const first=worker.sockets[0] + first.emitPacket(template(10,1),10001);first.emitPacket(template(10,1,true),10002) + first.emitPacket(data(10,1),10001);first.emitPacket(data(10,1),10002) + assert.equal(ingress.queued[0].src,"192.0.2.1");assert.equal(ingress.queued[1].src,"198.51.100.1") + assert.equal(worker.contexts[0].sourcePort,10001);assert.equal(worker.contexts[1].sourcePort,10002) + const previousSocketId=worker.contexts[0].collectorSocketId + await worker.start(payload) + const second=worker.sockets[1] + first.emitPacket(template(10,1,true),10001) + second.emitPacket(data(10,1),10001) + assert.equal(ingress.queued.length,2,"replacement socket cannot reuse old templates and retired socket cannot populate new scope") + const replacementContext=worker.contexts.at(-1) + assert(replacementContext) + assert.notEqual(replacementContext.collectorSocketId,previousSocketId) + second.emitPacket(template(10,1),10001);second.emitPacket(data(10,1),10001) + assert.equal(ingress.queued.length,3);assert.equal(ingress.queued[2].src,"192.0.2.1") + const before=templateCacheUsageForTests() + assert.equal(ingress.ingest(template(10,99),"unmapped",known),false) + assert.deepEqual(templateCacheUsageForTests(),before,"wire domain must not bypass mapped-exporter authorization") +}) + +test("real localhost UDP source ports reach the production Worker callback and stay isolated through engine parsing",async()=>{ + resetFlowTemplatesForTests() + const ingress=makeIngress(),worker=makeWorker(ingress,true),senders=[createSocket("udp4"),createSocket("udp4")] + try{ + await Promise.all(senders.map(socket=>new Promise((resolve,reject)=>{socket.once("error",reject);socket.bind(0,"127.0.0.1",resolve)}))) + await worker.start({listenHost:"127.0.0.1",listenPort:0,topN:20,retentionHours:24,exporterMap:{}}) + const receiver=worker.sockets[0] + await new Promise((resolve,reject)=>{receiver.once("error",reject);try{receiver.address();resolve()}catch{receiver.once("listening",resolve)}}) + const destination=receiver.address().port + const send=(index:number,bytes:Buffer)=>new Promise((resolve,reject)=>senders[index].send(bytes,destination,"127.0.0.1",error=>error?reject(error):resolve())) + await send(0,template(10,1));await worker.waitForContexts(1) + await send(1,template(10,1,true));await worker.waitForContexts(2) + await send(0,data(10,1));await worker.waitForContexts(3) + await send(1,data(10,1));await worker.waitForContexts(4) + assert.equal(worker.contexts[0].sourcePort,senders[0].address().port) + assert.equal(worker.contexts[1].sourcePort,senders[1].address().port) + assert.notEqual(worker.contexts[0].sourcePort,worker.contexts[1].sourcePort) + assert.equal(ingress.queued[0].src,"192.0.2.1");assert.equal(ingress.queued[1].src,"198.51.100.1") + }finally{ + for(const socket of [...senders,...worker.sockets])await new Promise(resolve=>{try{socket.close(resolve)}catch{resolve()}}) + } +}) + +test("production cache clear preserves refusal counters and repeated receiver incarnations cannot exhaust exporter capacity",async()=>{ + resetFlowTemplatesForTests() + assert.throws(()=>parseFlowPacket(template(10,1,false,limits.perTemplateFields+1),"known"),FlowTemplateRefused) + const refused=templateCacheUsageForTests().refusedTemplates + clearFlowTemplateCache();assert.equal(templateCacheUsageForTests().refusedTemplates,refused) + const ingress=makeIngress(),worker=makeWorker(ingress) + const payload={listenHost:"127.0.0.1",listenPort:2055,topN:20,retentionHours:24,exporterMap:{}} + for(let i=0;i({ query:async(sql:string)=>{calls.push(sql);if(sql===failAt)throw Error("fixture failure");if(sql.startsWith("SELECT COUNT"))return {rows:[{n:count.value}]};if(sql.startsWith("DELETE"))pendingDeletes++;if(sql.startsWith("UPDATE"))pendingCounters=0;if(sql==="COMMIT"){committedDeletes=pendingDeletes;counters=pendingCounters}if(sql==="ROLLBACK"){pendingDeletes=0;pendingCounters=counters}return {rows:[]}},release:()=>{released++}}), - state:()=>({calls,released,committedDeletes,counters}), + connect:async()=>({ query:async(sql:string)=>{calls.push(sql);if(sql===failAt)throw Error("fixture failure");if(sql.startsWith("SELECT pg_try_advisory_xact_lock_shared"))return {rows:[{acquired:admitted}]};if(sql.startsWith("SELECT COUNT"))return {rows:[{n:count.value}]};if(sql.startsWith("DELETE"))pendingDeletes++;if(sql.startsWith("UPDATE"))pendingCounters=0;if(sql==="COMMIT"){committedDeletes=pendingDeletes;counters=pendingCounters;if(failAt==="COMMIT_ACK_LOST")throw Error("fixture failure after commit")}if(sql==="ROLLBACK"){pendingDeletes=0;pendingCounters=counters}return {rows:[]}},release:(destroy=false)=>{released++;unsafe=destroy}}), + state:()=>({calls,released,unsafe,committedDeletes,counters}), } } let db=database();const deleted=await deleteTrafficFlowData(db.connect) assert.deepEqual(deleted,{buckets:2,minuteStats:2,minuteDims:2,dailyDims:2,hourFacts:2,dailyFacts:2});assert.equal(db.state().committedDeletes,6);assert.equal(db.state().counters,0);assert.equal(db.state().released,1) -assert.equal(db.state().calls[0],"BEGIN");assert.match(db.state().calls[1],/^LOCK TABLE .* IN ACCESS EXCLUSIVE MODE$/);assert.equal(db.state().calls.at(-1),"COMMIT") +assert.equal(db.state().calls[0],"BEGIN");assert.match(db.state().calls[3],/^SELECT pg_try_advisory_xact_lock_shared/);assert.match(db.state().calls[4],/^LOCK TABLE .* IN ACCESS EXCLUSIVE MODE$/);assert.equal(db.state().calls.at(-1),"COMMIT") for(const failAt of ["DELETE FROM flow_minute_dims", "UPDATE traffic_flow_settings SET packets_received = 0, last_datagram_at = NULL, last_exporter_ip = NULL, last_error = '', updated_at = now() WHERE id = 1", "COMMIT"]){ - db=database(failAt);await assert.rejects(deleteTrafficFlowData(db.connect),/fixture failure/);assert.equal(db.state().committedDeletes,0);assert.equal(db.state().counters,42);assert.equal(db.state().released,1);assert.equal(db.state().calls.at(-1),"ROLLBACK");assert.equal(db.state().calls.filter(c=>c==="BEGIN").length,1) + db=database(failAt);await assert.rejects(deleteTrafficFlowData(db.connect),/fixture failure/);assert.equal(db.state().committedDeletes,0);assert.equal(db.state().counters,42);assert.equal(db.state().released,1);assert.equal(db.state().calls.at(-1),failAt==="COMMIT"?"COMMIT":"ROLLBACK");assert.equal(db.state().unsafe,failAt==="COMMIT");assert.equal(db.state().calls.filter(c=>c==="BEGIN").length,1) } +db=database("",{value:"2"},false);await assert.rejects(deleteTrafficFlowData(db.connect),/очистка не начата/);assert(!db.state().calls.some(sql=>sql.startsWith("LOCK TABLE")||sql.startsWith("DELETE"))) +db=database("COMMIT_ACK_LOST");await assert.rejects(deleteTrafficFlowData(db.connect),/after commit/);assert.equal(db.state().committedDeletes,6);assert.equal(db.state().unsafe,true);assert.equal(db.state().calls.filter(sql=>sql==="COMMIT").length,1);assert(!db.state().calls.includes("ROLLBACK")) for(const bad of [null,undefined,"NaN","9007199254740992",-1,1.5]){db=database("",{value:bad});await assert.rejects(deleteTrafficFlowData(db.connect));assert.equal(db.state().committedDeletes,0);assert.equal(db.state().released,1);assert.equal(db.state().calls.filter(c=>c.startsWith("DELETE")).length,0)} let stops=0,deletes=0,restarts=0,invalidates=0,maintains=0,failDelete=false const deps={stop:async()=>{stops++},deleteData:async()=>{deletes++;if(failDelete)throw Error("delete unknown");return deleted},invalidate:()=>{invalidates++},maintain:async()=>{maintains++;throw Error("partition unavailable")},restart:async()=>{restarts++;throw Error("listener unavailable")}} const result=await executeTrafficFlowPurge(deps);assert.equal(result.ok,true);assert.deepEqual(result.deleted,deleted);assert.equal(result.partitionError,"partition unavailable");assert.equal(result.listenerError,"listener unavailable");assert.equal(result.fileBytesBefore,null);assert.equal(result.fileBytesAfter,null) assert.equal(invalidates,1);assert.equal(deletes,1);assert.equal(restarts,1) failDelete=true;await assert.rejects(executeTrafficFlowPurge(deps),/delete unknown.*не восстановлен/);assert.equal(stops,2);assert.equal(deletes,2);assert.equal(maintains,1);assert.equal(invalidates,1);assert.equal(restarts,2) +const before={deletes,restarts};await assert.rejects(executeTrafficFlowPurge({...deps,stop:async()=>{throw Error("shutdown not acknowledged")}}),/not acknowledged/);assert.equal(deletes,before.deletes);assert.equal(restarts,before.restarts) +db=database();let fenced=false;await deleteTrafficFlowData(db.connect,async()=>{fenced=true;assert.ok(db.state().calls.some(call=>call.startsWith("LOCK TABLE")));assert.equal(db.state().calls.filter(call=>call.startsWith("DELETE")).length,0)});assert.equal(fenced,true) +db=database();await assert.rejects(deleteTrafficFlowData(db.connect,async()=>{throw Error("late source drift")}));assert.equal(db.state().committedDeletes,0);assert.equal(db.state().calls.filter(call=>call.startsWith("DELETE")).length,0) console.log("NetFlow purge transaction: all tables/counters commit together, rollback/release, safe counts, maintenance/restart partial result without repeated delete") diff --git a/backend/src/services/traffic-flow-purge-execution.ts b/backend/src/services/traffic-flow-purge-execution.ts index f3edb401..0877dc55 100644 --- a/backend/src/services/traffic-flow-purge-execution.ts +++ b/backend/src/services/traffic-flow-purge-execution.ts @@ -1,16 +1,35 @@ import type { FlowPurgeDto } from "@mmapp/contracts/traffic-flow" -const tables = { +import type { ExpiredPartition } from "../db/partitions.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" +export const flowPurgeTables = { buckets: "flow_buckets", minuteStats: "flow_minute_stats", minuteDims: "flow_minute_dims", dailyDims: "flow_daily_dims", hourFacts: "flow_hour_facts", dailyFacts: "flow_daily_facts", } as const -type Client = { query: (sql: string) => Promise<{ rows: Record[] }>; release: () => void } -export async function deleteTrafficFlowData(connect: () => Promise): Promise { +export type FlowPurgeClient = { query: (sql: string, params?: unknown[]) => Promise<{ rows: Record[] }> } +type Client = FlowPurgeClient & { release: (unsafe?: boolean) => void } +export interface FlowPurgeOptions { beforeDrain?: () => Promise; beforeDelete?: (client: FlowPurgeClient) => Promise; beforeMaintenance?: () => Promise; beforePartitionDrop?: (partition: ExpiredPartition) => Promise; beforeRestart?: () => Promise; beforeRecord?: () => Promise } +export async function deleteTrafficFlowData(connect: () => Promise, beforeDelete?: FlowPurgeOptions["beforeDelete"]): Promise { const client = await connect() + let commitSent = false, unsafe = false try { await client.query("BEGIN") - await client.query("LOCK TABLE " + Object.values(tables).join(", ") + " IN ACCESS EXCLUSIVE MODE") + await client.query("SET LOCAL lock_timeout = '5s'") + await client.query("SET LOCAL statement_timeout = '60s'") + // The flow-exclusive fence can be held by another backend. Never wait for + // partition admission while retaining that fence: refuse before table SQL + // so a queued maintenance owner and flow writer cannot form a lock cycle. + const admission = await client.query("SELECT pg_try_advisory_xact_lock_shared($1::bigint) AS acquired", [PARTITION_WRITER_ADVISORY_KEY]) + if (admission.rows[0]?.acquired !== true) throw Object.assign(new Error("Обслуживание разделов занято; очистка не начата"), { code: "PARTITION_WRITER_BUSY", statusCode: 409 }) + if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client) + if (process.env.DATABASE_RUNTIME_ACTIVATION_DIRECTORY !== undefined) { + const { assertRuntimeDatabaseActivationCurrent } = await import("../db/index.js") + assertRuntimeDatabaseActivationCurrent() + } + await client.query("LOCK TABLE " + Object.values(flowPurgeTables).join(", ") + " IN ACCESS EXCLUSIVE MODE") + await client.query("SELECT id FROM traffic_flow_settings WHERE id=1 FOR UPDATE") const deleted: FlowPurgeDto["deleted"] = { buckets: 0, minuteStats: 0, minuteDims: 0, dailyDims: 0 } - for (const [field, table] of Object.entries(tables)) { + for (const [field, table] of Object.entries(flowPurgeTables)) { const result = await client.query("SELECT COUNT(*)::bigint AS n FROM " + table) const raw = result.rows[0]?.n if ((typeof raw !== "number" && typeof raw !== "string") || !/^\d+$/.test(String(raw))) throw new Error("Не подтверждён счётчик " + table) @@ -18,15 +37,18 @@ export async function deleteTrafficFlowData(connect: () => Promise): Pro if (!Number.isSafeInteger(count) || count < 0) throw new Error("Счётчик вне допустимого диапазона: " + table) deleted[field as keyof typeof deleted] = count } - for (const table of Object.values(tables)) await client.query("DELETE FROM " + table) + await beforeDelete?.(client) + for (const table of Object.values(flowPurgeTables)) await client.query("DELETE FROM " + table) await client.query("UPDATE traffic_flow_settings SET packets_received = 0, last_datagram_at = NULL, last_exporter_ip = NULL, last_error = '', updated_at = now() WHERE id = 1") + commitSent = true await client.query("COMMIT") return deleted } catch (error) { // A lost COMMIT response remains unknown; never retry deletion here. - try { await client.query("ROLLBACK") } catch { /* preserve the original failure */ } + if (commitSent) unsafe = true + else try { await client.query("ROLLBACK") } catch { unsafe = true } throw error - } finally { client.release() } + } finally { client.release(unsafe) } } const message = (error: unknown) => error instanceof Error ? error.message : String(error) export async function executeTrafficFlowPurge(deps: { @@ -35,20 +57,34 @@ export async function executeTrafficFlowPurge(deps: { invalidate: () => void maintain: () => Promise restart: () => Promise + withDataFence?: (work:()=>Promise)=>Promise }): Promise { let deleted: FlowPurgeDto["deleted"] - try { - await deps.stop() - deleted = await deps.deleteData() - } catch (error) { - let restartError: string | undefined - try { await deps.restart() } catch (restart) { restartError = message(restart) } - throw new Error(message(error) + (restartError === undefined ? "" : "; приём NetFlow не восстановлен: " + restartError)) - } - deps.invalidate() + // Stop failure leaves the worker's SQL outcome unconfirmed. Do not kill or + // restart that worker, and never begin deletion; the production stop is latched. + await deps.stop() let partitionError: string | undefined, listenerError: string | undefined - try { await deps.maintain() } catch (error) { partitionError = message(error) } + try { + const publish=async()=>{ + const next=await deps.deleteData() + deps.invalidate() + try { await deps.maintain() } catch(error) { partitionError=message(error) } + return next + } + deleted=deps.withDataFence?await deps.withDataFence(publish):await publish() + }catch(error){ + let restartError:string|undefined + try{await deps.restart()}catch(restart){restartError=message(restart)} + throw new Error(message(error)+(restartError===undefined?"":"; приём NetFlow не восстановлен: "+restartError)) + } try { await deps.restart() } catch (error) { listenerError = message(error) } return { ok: true, deleted, fileBytesBefore: null, fileBytesAfter: null, vacuumed: false, ...(partitionError !== undefined ? { partitionError } : {}), ...(listenerError !== undefined ? { listenerError } : {}) } } + +/** Shared full REST/MCP scenario: a journal error cannot repeat a committed purge. */ +export async function completeTrafficFlowPurge(deps: { purge: () => Promise; record: (result: FlowPurgeDto) => Promise }): Promise { + const result = await deps.purge() + try { await deps.record(result) } catch (error) { return { ...result, eventError: message(error) } } + return result +} diff --git a/backend/src/services/traffic-flow-quality-contract.test.ts b/backend/src/services/traffic-flow-quality-contract.test.ts new file mode 100644 index 00000000..06edcaf3 --- /dev/null +++ b/backend/src/services/traffic-flow-quality-contract.test.ts @@ -0,0 +1,42 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createFlowIngestQuality } from "./traffic-flow-ingest-quality.js" +import { flowIngestQualitySchema } from "@mmapp/contracts/traffic-flow" + +test("real producer fresh, quantified, unsafe, bad-interval and saturating views satisfy the contract", () => { + const quality = createFlowIngestQuality() + const check = () => assert.equal(flowIngestQualitySchema.safeParse(quality.snapshot()).success, true) + check(); quality.reject(128, 1, "aggregate-capacity"); check() + quality.reject(Number.NaN, 2, "unsafe-numeric"); check() + quality.reject(1, 1, "key-width", "unknown"); check() + quality.reset() + for (let i = 0; i < 2049; i++) quality.reject(Number.MAX_SAFE_INTEGER, 1, "unsafe-numeric") + check() +}) +test("inconsistent counters/status/reasons/intervals and unbounded decimal input are refused", () => { + const quality = createFlowIngestQuality(); quality.reject(128, 1, "aggregate-capacity", "2026-10-29T10:00:00.000Z") + const original = quality.snapshot() + const mutations: Array<(value: ReturnType extends infer T ? {-readonly [K in keyof T]: K extends "knownLowerBounds" ? {records:string;bytes:string;packets:string} : K extends "reasons" ? {"aggregate-capacity":boolean;"ring-capacity":boolean;"key-width":boolean;"unsafe-numeric":boolean} : T[K]} : never) => void> = [ + value => { value.records = "01"; value.knownLowerBounds.records = "01" }, + value => { value.bytes = "18446744073709551616"; value.knownLowerBounds.bytes = value.bytes }, + value => { value.records = "-1"; value.knownLowerBounds.records = "-1" }, + value => { value.bytes = "1.0"; value.knownLowerBounds.bytes = "1.0" }, + value => { value.bytes = "9".repeat(100000); value.knownLowerBounds.bytes = value.bytes }, + value => { value.knownLowerBounds.bytes = "127" }, + value => { value.localAdmission = "no-local-loss-observed" }, + value => { value.bytes = null }, + value => { value.counterOverflow = true }, + value => { value.lastAt = "2026-10-29T09:59:59.000Z" }, + value => { value.firstAt = null }, + value => { value.intervalUnknown = true }, + value => { value.records = "0"; value.knownLowerBounds.records = "0" }, + value => { value.records = null; value.localAdmission = "loss-quantity-unknown" }, + value => { value.reasons["aggregate-capacity"] = false }, + value => { value.firstAt = "2026-10-29T10:00:00Z" }, + ] + for (const mutate of mutations) { + const value = structuredClone(original); mutate(value) + assert.doesNotThrow(() => flowIngestQualitySchema.safeParse(value)) + assert.equal(flowIngestQualitySchema.safeParse(value).success, false) + } +}) diff --git a/backend/src/services/traffic-flow-quality.owned.test.ts b/backend/src/services/traffic-flow-quality.owned.test.ts new file mode 100644 index 00000000..3b54be66 --- /dev/null +++ b/backend/src/services/traffic-flow-quality.owned.test.ts @@ -0,0 +1,101 @@ +/** Production engine/SQL acceptance on one private disposable database only. */ +import assert from "node:assert/strict" +import { basename, dirname, isAbsolute } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import type { PoolClient } from "pg" + +async function main() { + assert.equal(process.env.MMAPP_OWN_FLOW_QUALITY,"true");assert.equal(process.env.MMAPP_OWN_SQL_DB,"postgres");assert.equal(process.env.NODE_ENV,"test") + const endpoint=new URL(process.env.DATABASE_URL??"postgresql://invalid") + assert(["postgres:","postgresql:"].includes(endpoint.protocol));assert.equal(endpoint.hostname,"127.0.0.1");assert.equal(endpoint.username,"mcp_test");assert(endpoint.password) + assert.equal(endpoint.pathname,"/postgres");assert.equal(endpoint.search,"");assert.equal(endpoint.hash,"");assert(Number(endpoint.port)>0) + assert(process.env.MCP_STATE_DIR&&isAbsolute(process.env.MCP_STATE_DIR));assert.equal(basename(process.env.MCP_STATE_DIR),"state");assert(basename(dirname(process.env.MCP_STATE_DIR)).startsWith("mm-mcp-test-")) + const {pool,closePool}=await import("../db/index.js") + const {applySqlMigrations}=await import("../db/migrate.js") + const {ensurePartitionsAround}=await import("../db/partitions.js") + const engine=await import("./traffic-flow-engine.js") + const {FlowCommitUnknown}=await import("./traffic-flow-write-transaction.js") + const {getServerCatalog,loadFlowTopology}=await import("./traffic-flow-topology.js") + const {pauseRipeBackgroundWork}=await import("./traffic-flow-ripe.js") + const originalConnect=pool.connect + let triggerCreated=false + const groups:string[]=[] + try { + await applySqlMigrations(pool);await ensurePartitionsAround(pool) + const id=Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,password,type) VALUES('own-flow-quality','127.0.0.1',9,false,'synthetic-unused','home-router') RETURNING id")).rows[0].id) + await getServerCatalog();await loadFlowTopology() + const base={src:"10.144.0.1",dst:"10.144.0.2",proto:6,srcPort:12345,dstPort:443,bytes:128,packets:1,inIface:"ether1",outIface:""} + const feed=(flows:Parameters[1])=>engine.queueParsedFlows(id,flows) + const drain=()=>engine.strictDrainEngine(async()=>{},[id]) + const receipts=async()=>(await pool.query("SELECT batch_id,source_proof,result FROM flow_write_receipts WHERE kind='ingest' ORDER BY committed_at,batch_id")).rows + const mass=async()=>(await pool.query("SELECT coalesce(sum(bytes),0)::bigint AS bytes,coalesce(sum(packets),0)::bigint AS packets FROM flow_minute_stats WHERE server_id=$1",[id])).rows[0] + const reset=()=>engine.resetEngineAfterDatabaseRestore() + const installFailure=async()=>{ + await pool.query("CREATE FUNCTION public.mcp_flow_quality_fixture_fail() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'owned quality receipt failure'; END $$") + await pool.query("CREATE TRIGGER mcp_flow_quality_fixture_fail BEFORE INSERT ON flow_write_receipts FOR EACH ROW EXECUTE FUNCTION public.mcp_flow_quality_fixture_fail()") + triggerCreated=true + } + const removeFailure=async()=>{await pool.query("DROP TRIGGER mcp_flow_quality_fixture_fail ON flow_write_receipts");await pool.query("DROP FUNCTION public.mcp_flow_quality_fixture_fail()");triggerCreated=false} + + reset();for(let i=0;i<2049;i++)feed([{...base,inIface:"ether"+i}]) + const pressure=engine.getEngineStats().dataQuality + assert.equal(pressure.records,"2");assert.equal(pressure.bytes,"256");assert.equal(pressure.packets,"2");assert.equal(pressure.reasons["ring-capacity"],true) + assert.equal(engine.peekPendingFlows().reduce((n,row)=>n+row.bytes,0),2047*128) + await drain();assert.deepEqual(await mass(),{bytes:2047*128,packets:2047}) + assert.deepEqual((await receipts())[0].result.ingestQuality,pressure) + assert.equal(engine.observeFlowRing(id).available,false);assert.throws(()=>engine.getRingMbps(id),/incomplete|unobserved/) + groups.push("ring pressure admits2047/rejects2 before aggregates; genuine SQL minute mass and quantified receipt conserve all input") + + reset();feed([{...base,bytes:Number.MAX_SAFE_INTEGER+1}]);const beforeLossMass=await mass(),beforeLossReceipts=(await receipts()).length + assert.equal(engine.peekPendingFlows().length,0);await drain() + const lossOnly=(await receipts()).at(-1)!.result + assert.equal((await receipts()).length,beforeLossReceipts+1);assert.equal(lossOnly.buckets,0);assert.equal(lossOnly.ingestQuality.bytes,null);assert.equal(lossOnly.ingestQuality.localAdmission,"loss-quantity-unknown") + assert.deepEqual(await mass(),beforeLossMass) + const countBeforeIdle=(await receipts()).length + await delay(15100);await engine.flushPending({prune:false}) + assert.equal((await receipts()).length,countBeforeIdle,"Persisted quality warning is not perpetual dirty stats creating empty receipts") + groups.push("unsafe quantity is null; loss-only receipt persists atomically without flow mass and idle warning creates no empty receipt") + + reset();feed([base,{...base,inIface:"x".repeat(257)}]);const rollbackMass=await mass(),rollbackReceipts=(await receipts()).length + await installFailure();await assert.rejects(drain(),/owned quality receipt failure/) + assert.deepEqual(await mass(),rollbackMass);assert.equal((await receipts()).length,rollbackReceipts) + feed([{...base,srcPort:12346,bytes:64},{...base,bytes:64,inIface:"x".repeat(257)}]) + await removeFailure();await drain() + let written=await receipts();assert.equal(written.at(-1)!.result.ingestQuality.bytes,"128") + assert.equal(engine.peekPendingFlows().reduce((n,row)=>n+row.bytes,0),64) + await drain();written=await receipts();assert.equal(written.at(-1)!.result.ingestQuality.bytes,"64") + assert.equal(written.length,rollbackReceipts+2) + groups.push("real receipt failure rolls back every SQL cube; captured/live loss deltas remain separate through explicit retry") + + reset();feed([base,{...base,inIface:"x".repeat(257)}]);let lost=false,quarantined=false + pool.connect=(function(this:typeof pool,...args:unknown[]){ + if(args.length)return (originalConnect as (...args:unknown[])=>unknown).apply(pool,args) + return (originalConnect as()=>Promise).call(pool).then(client=>new Proxy(client,{get(target,key){ + if(key==="query")return async(...queryArgs:unknown[])=>{const result=await(target.query as(...args:unknown[])=>Promise).apply(target,queryArgs);if(queryArgs[0]==="COMMIT"&&!lost){lost=true;throw Error("owned lost flow COMMIT ACK")}return result} + if(key==="release")return(destroy=false)=>{quarantined ||= destroy;target.release(destroy)} + const value=Reflect.get(target,key);return typeof value==="function"?value.bind(target):value + }})as PoolClient) + })as typeof pool.connect + try{await assert.rejects(drain(),FlowCommitUnknown)}finally{pool.connect=originalConnect} + assert(lost&&quarantined);const committed=(await receipts()).at(-1)!,genuineResult=structuredClone(committed.result),unknownMass=await mass() + feed([{...base,srcPort:12347,bytes:64},{...base,bytes:NaN}]) + await assert.rejects(drain(),/unknown/i);assert.deepEqual(await mass(),unknownMass) + await pool.query("UPDATE flow_write_receipts SET result=result-'ingestQuality' WHERE batch_id=$1",[committed.batch_id]) + await assert.rejects(engine.reconcileEnginePendingWrite(),/receipt quality/) + await pool.query("UPDATE flow_write_receipts SET result=$2::jsonb WHERE batch_id=$1",[committed.batch_id,JSON.stringify({...genuineResult,ingestQuality:{...genuineResult.ingestQuality,bytes:"999"}})]) + await assert.rejects(engine.reconcileEnginePendingWrite(),/receipt quality/) + await pool.query("UPDATE flow_write_receipts SET result=$2::jsonb WHERE batch_id=$1",[committed.batch_id,JSON.stringify(genuineResult)]) + assert.equal((await engine.reconcileEnginePendingWrite()).state,"committed") + assert.deepEqual(await mass(),unknownMass);assert.equal(engine.peekPendingFlows().reduce((n,row)=>n+row.bytes,0),64) + await drain();const latest=(await receipts()).at(-1)!.result.ingestQuality + assert.equal(latest.records,"1");assert.equal(latest.bytes,null);assert.equal(latest.knownLowerBounds.bytes,"0") + assert.deepEqual((await pool.query("SELECT result FROM flow_write_receipts WHERE batch_id=$1",[committed.batch_id])).rows[0].result,genuineResult) + groups.push("actual COMMIT ACK loss quarantines old backend; missing/tampered receipt quality cannot ACK; genuine receipt reconciles without replay and new arrivals keep separate delta") + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM flow_ip_meta")).rows[0].n),0,"Private synthetic flows never enqueue RIPE metadata") + console.log(JSON.stringify({accepted:groups.length,groups,actualSql:true,actualCommitAckLost:lost,realRouters:false,wrappedClientOnlyForExplicitCommitAckFault:true,wholeInstallation2GbAccepted:false})) + }finally{ + pool.connect=originalConnect + try{if(triggerCreated){await pool.query("DROP TRIGGER IF EXISTS mcp_flow_quality_fixture_fail ON flow_write_receipts");await pool.query("DROP FUNCTION IF EXISTS public.mcp_flow_quality_fixture_fail()")};await pauseRipeBackgroundWork()}finally{await closePool()} + } +} +await main() diff --git a/backend/src/services/traffic-flow-read-budget.test.ts b/backend/src/services/traffic-flow-read-budget.test.ts new file mode 100644 index 00000000..0223349e --- /dev/null +++ b/backend/src/services/traffic-flow-read-budget.test.ts @@ -0,0 +1,114 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { EventEmitter, once } from 'node:events' +import { createServer, type ServerResponse } from 'node:http' +import { connect } from 'node:net' +import { setTimeout as delay } from 'node:timers/promises' +import { createFlowReadBudget, createFlowReadResponseOwner, writeBoundedFlowSse, trackFlowReadSource, withFlowReadSourceWindow } from './traffic-flow-read-budget.js' +const limits={maxOwners:1,maxReservedBytes:100} +function fake(){return new EventEmitter() as ServerResponse} + +test('both independent fences are mandatory; repeated notifications never over-release',async()=>{ + const budget=createFlowReadBudget(limits),response=fake(),owner=createFlowReadResponseOwner(response,budget,limits) + assert.equal(await owner.run(100,async()=>42),42) + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:100}) + assert.throws(()=>budget.acquire(1),/занят/) + response.emit('finish');response.emit('close') + assert.deepEqual(budget.stats(),{owners:0,reservedBytes:0}) + const response2=fake(),owner2=createFlowReadResponseOwner(response2,budget,limits) + let finish!:()=>void + const pending=owner2.run(100,()=>new Promise(resolve=>{finish=resolve})) + response2.emit('close') + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:100}) + assert.throws(()=>budget.acquire(1),/занят/) + finish();await pending + assert.deepEqual(budget.stats(),{owners:0,reservedBytes:0}) +}) + +test('nested and sequential callbacks share one response owner with cumulative output admission',async()=>{ + const budget=createFlowReadBudget({maxOwners:10,maxReservedBytes:1000}),response=fake(),owner=createFlowReadResponseOwner(response,budget,limits) + await owner.run(40,async()=>({payload:'retained'})) + await owner.run(40,async()=>await owner.run(40,async()=>{owner.retainOutput(4*1024*1024)})) + await owner.run(40,async()=>{owner.retainOutput(4*1024*1024)}) + await assert.rejects(owner.run(40,async()=>{owner.retainOutput(1)}),/суммарный/) + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:40}) + response.emit('close') + await assert.rejects(owner.run(1,async()=>{}),/закрыт/) + assert.deepEqual(budget.stats(),{owners:0,reservedBytes:0}) + assert.throws(()=>createFlowReadBudget({maxOwners:NaN,maxReservedBytes:1}),/Invalid/) +}) + +test('early handler rejection and response abort do not release still-running sibling SQL work',async()=>{ + const budget=createFlowReadBudget(limits),response=fake(),owner=createFlowReadResponseOwner(response,budget,limits) + let finish!:()=>void,pending:Promise|undefined + await assert.rejects(owner.run(100,async()=>{ + pending=trackFlowReadSource(async admit=>{admit(1,100);await new Promise(resolve=>{finish=resolve})}) + throw Error('another Promise.all member failed') + }),/member failed/) + response.emit('close');await delay(20) + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:100}) + finish();await pending + assert.deepEqual(budget.stats(),{owners:0,reservedBytes:0}) +}) + +test('decoded source reservations accumulate across completed SQL reads before another decode',async()=>{ + const budget=createFlowReadBudget(limits),response=fake(),owner=createFlowReadResponseOwner(response,budget,limits) + let decodes=0 + await assert.rejects(owner.run(100,async()=>{ + await trackFlowReadSource(async admit=>{admit(1,20*1024*1024);decodes++}) + await trackFlowReadSource(async admit=>{admit(1,20*1024*1024);decodes++}) + }),/суммарный/) + assert.equal(decodes,1) + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:100}) + response.emit('finish');assert.deepEqual(budget.stats(),{owners:0,reservedBytes:0}) +}) + +test('sequential SSE source windows do not exhaust a stream over time; response still owns the lease',async()=>{ + const budget=createFlowReadBudget(limits),response=fake(),owner=createFlowReadResponseOwner(response,budget,limits) + let frames=0 + await owner.run(100,async()=>{ + for(let i=0;i<20;i++)await withFlowReadSourceWindow(async()=>{await trackFlowReadSource(async admit=>{admit(50000,20*1024*1024);frames++})}) + }) + assert.equal(frames,20) + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:100}) + response.emit('close');assert.deepEqual(budget.stats(),{owners:0,reservedBytes:0}) +}) + +test('actual localhost slow response holds one encoded frame and global ownership through abort and pending computation',{timeout:10000},async()=>{ + const budget=createFlowReadBudget(limits) + let writes=0,completedWrites=0,computing=false,finishCompute!:()=>void,finishHandler!:()=>void + const handlerDone=new Promise(resolve=>{finishHandler=resolve}) + const server=createServer((_,response)=>{ + const owner=createFlowReadResponseOwner(response,budget,limits) + void owner.run(100,async()=>{ + try {response.writeHead(200,{'Content-Type':'text/event-stream'});for(let i=0;i<128;i++){writes++;await writeBoundedFlowSse(response,'sample',{payload:'x'.repeat(2*1024*1024)},3*1024*1024);completedWrites++}} + catch{/* Expected client abort. */} + computing=true;await new Promise(resolve=>{finishCompute=resolve}) + }).finally(finishHandler) + }) + await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve)) + const address=server.address();assert.ok(address&&typeof address!=='string') + const client=connect({host:'127.0.0.1',port:address.port});client.pause() + try { + await once(client,'connect');client.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n') + await delay(300) + assert.ok(writes>0&&writes<128,'Unread localhost client must block the bounded producer') + assert.equal(writes-completedWrites,1,'Exactly one encoded frame is outstanding') + const held=writes;await delay(100);assert.equal(writes,held,'No new snapshot is encoded while write callback is blocked') + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:100}) + assert.throws(()=>budget.acquire(1),/занят/) + client.destroy() + for(let i=0;i<100&&!computing;i++)await delay(10) + assert.equal(computing,true) + assert.deepEqual(budget.stats(),{owners:1,reservedBytes:100},'Abort alone cannot release pending computation') + finishCompute();await handlerDone + assert.deepEqual(budget.stats(),{owners:0,reservedBytes:0}) + }finally{client.destroy();finishCompute?.();server.closeAllConnections();await new Promise(resolve=>server.close(()=>resolve()))} +}) + +test('oversize SSE is refused before enqueue and cannot partially publish',{timeout:5000},async()=>{ + let writes=0 + const response=fake();response.write=(()=>{writes++;return true}) as ServerResponse['write'] + await assert.rejects(writeBoundedFlowSse(response,'sample',{text:'я'.repeat(100)},20),/exceeds/) + assert.equal(writes,0) +}) diff --git a/backend/src/services/traffic-flow-read-budget.ts b/backend/src/services/traffic-flow-read-budget.ts new file mode 100644 index 00000000..03a41b5d --- /dev/null +++ b/backend/src/services/traffic-flow-read-budget.ts @@ -0,0 +1,147 @@ +import { getFlowReadResourceProfile } from "./traffic-flow-read-resource-profile.js" +import type { ServerResponse } from "node:http" +import { AsyncLocalStorage } from "node:async_hooks" +import { withActiveDatabaseRequest } from "../modules/mcp/database-request-lease.js" +import { sharedDatabaseGate } from "./shared-database-gate.js" + +export interface FlowReadBudgetLimits { maxOwners: number; maxReservedBytes: number } +export interface FlowReadLease { computationDone(): void; responseDone(): void } +function positive(value: number): boolean { return Number.isSafeInteger(value) && value > 0 } +function unavailable(message: string) { return Object.assign(new Error(message), { statusCode: 429, code: "RATE_LIMITED" }) } + +/** Reservation accounting, not a measurement of V8 or native memory. No wait queue. */ +export function createFlowReadBudget(limits: FlowReadBudgetLimits) { + if (!positive(limits.maxOwners) || !positive(limits.maxReservedBytes)) throw Error("Invalid NetFlow read budget") + let owners = 0, reservedBytes = 0 + return { + acquire(bytes: number): FlowReadLease { + if (!positive(bytes)) throw Error("Invalid NetFlow read reservation") + if (owners >= limits.maxOwners || bytes > limits.maxReservedBytes - reservedBytes) throw unavailable("Ресурс чтения NetFlow занят; повторите запрос позже") + owners++; reservedBytes += bytes + let computationFinished = false, responseFinished = false, released = false + const release = () => { + if (!released && computationFinished && responseFinished) { released = true; owners--; reservedBytes -= bytes } + } + return { + computationDone() { computationFinished = true; release() }, + responseDone() { responseFinished = true; release() }, + } + }, + stats: () => ({ owners, reservedBytes }), + } +} +export type FlowReadBudget = ReturnType +interface SourceLimits { maxRows: number; maxEncodedBytes: number } +interface ReadWork { + owner:symbol + pending: number; finished: boolean; rows: number; bytes: number; activeSql: number + release(): void; closed(): boolean +} +const readWork = new AsyncLocalStorage() +/** A child service operation owns a handler-lifetime database lease. This is + * independent of response ownership and keeps active SQL/default initialization + * fenced after an early parent-handler rejection. No socket wait is introduced. + * Without a NetFlow response context, existing internal restore/restart callers + * retain their existing admission path. + */ +export async function trackFlowReadComputation(run:()=>Promise):Promise{ + const work=readWork.getStore() + if(!work)return run() + if(work.closed())throw unavailable("Ответ NetFlow уже закрыт") + work.pending++ + try{return await sharedDatabaseGate.request(() => withActiveDatabaseRequest(run) ?? run())} + finally{work.pending--;if(work.finished&&!work.pending)work.release()} +} +/** Sequential SSE frames have separate temporary source windows. Their outer + * response reservation remains held; no earlier frame is reused until its write + * callback and all child computation complete. MCP retained samples do not reset. + */ +export async function withFlowReadSourceWindow(run:()=>Promise):Promise{ + const parent=readWork.getStore() + if(!parent)return run() + if(parent.closed())throw unavailable("Ответ NetFlow уже закрыт") + parent.pending++ + let released=false + const work:ReadWork={owner:parent.owner,pending:0,finished:false,rows:0,bytes:0,activeSql:0,closed:parent.closed,release:()=>{ + if(released)return;released=true;parent.pending--;if(parent.finished&&!parent.pending)parent.release() + }} + try{return await readWork.run(work,run)}finally{work.finished=true;if(!work.pending)work.release()} +} +/** Track child SQL past early Promise.all failure; admit cumulatively before decode. */ +export async function trackFlowReadSource(run: (admit: (rows: number, bytes: number) => SourceLimits) => Promise): Promise { + const work=readWork.getStore() + if(!work)return run((maxRows,maxEncodedBytes)=>({maxRows,maxEncodedBytes})) + if(work.closed() || work.activeSql>=getFlowReadResourceProfile().parallelSql)throw unavailable("Превышен ресурс параллельных источников NetFlow") + work.pending++;work.activeSql++ + const read = () => run((rows,bytes)=>{ + if(work.closed() || !Number.isSafeInteger(rows)||rows<0||!positive(bytes)||rows>100000-work.rows||bytes>getFlowReadResourceProfile().responseSourceBytes-work.bytes)throw unavailable("Превышен суммарный объём источников ответа NetFlow") + work.rows+=rows;work.bytes+=bytes + return {maxRows:Math.max(1,rows),maxEncodedBytes:bytes} + }) + try{return await sharedDatabaseGate.request(() => withActiveDatabaseRequest(read) ?? read())} + finally{work.pending--;work.activeSql--;if(work.finished&&!work.pending)work.release()} +} + +/** Owns reservations until computation AND actual response finish/abort. + * Completed invocations remain charged while their response retains results. + * A disconnect never cancels or releases still-running SQL/computation. + */ +export function createFlowReadResponseOwner(response: ServerResponse, budget: FlowReadBudget, limits: FlowReadBudgetLimits) { + if (!positive(limits.maxOwners) || !positive(limits.maxReservedBytes)) throw Error("Invalid NetFlow response budget") + const ownerId=Symbol() + let lease:FlowReadLease|undefined,work:ReadWork|undefined,reservedBytes=0,outputBytes=0,responseFinished=false + const finished = () => { + if (responseFinished) return + responseFinished = true + response.off("finish", finished); response.off("close", finished) + if(work){work.finished=true;if(!work.pending)work.release()} + lease?.responseDone() + } + response.once("finish", finished); response.once("close", finished) + if (response.writableFinished || response.destroyed) finished() + return { + async run(bytes: number, compute: () => Promise): Promise { + if (!positive(bytes)) throw Error("Invalid NetFlow read reservation") + if (responseFinished) throw unavailable("Ответ NetFlow уже закрыт") + if(bytes>limits.maxReservedBytes || lease&&bytes>reservedBytes)throw unavailable("Превышен ресурс одного ответа NetFlow") + if(!lease){ + lease=budget.acquire(bytes);reservedBytes=bytes + work={owner:ownerId,pending:0,finished:false,rows:0,bytes:0,activeSql:0,release:()=>lease!.computationDone(),closed:()=>responseFinished} + } + // Every callback shares the response lease/source counters. Nested calls + // inherit their active temporary SSE source window when applicable. + const current=readWork.getStore(),scope=current?.owner===ownerId?current:work! + scope.pending++ + try { return await readWork.run(scope,compute) } + finally { scope.pending--;if(scope.finished&&!scope.pending)scope.release() } + }, + retainOutput(bytes:number){ + if(!positive(bytes)||responseFinished||bytes>getFlowReadResourceProfile().responseOutputBytes-outputBytes)throw unavailable("Превышен суммарный объём ответа NetFlow") + outputBytes+=bytes + }, + } +} +export type FlowReadResponseOwner = ReturnType + +/** At most one queued SSE frame per producer; no timer/drain race frees ownership. + * The caller must reserve stringify/UTF-8 transient copies before invoking this. + * Size rejection happens before enqueue, never truncates or partially emits data. + */ +export async function writeBoundedFlowSse(response: ServerResponse, event: string, data: unknown, maxFrameBytes: number): Promise { + if (!positive(maxFrameBytes) || !/^[a-zA-Z0-9_-]+$/.test(event)) throw Error("Invalid NetFlow SSE frame policy") + if (response.destroyed || response.writableEnded) throw Error("NetFlow response closed") + const frame = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n` + if (Buffer.byteLength(frame) > maxFrameBytes) throw Error("NetFlow SSE frame exceeds reserved capacity") + await new Promise((resolve, reject) => { + let settled = false + const finish = (error?: Error | null) => { + if (settled) return + settled = true; response.off("close", closed); response.off("error", failed) + if (error) reject(error); else resolve() + } + const closed = () => finish(Error("NetFlow response closed")) + const failed = (error: Error) => finish(error) + response.once("close", closed); response.once("error", failed) + try { response.write(frame, finish) } catch (error) { finish(error instanceof Error ? error : Error(String(error))) } + }) +} diff --git a/backend/src/services/traffic-flow-read-http.owned.test.ts b/backend/src/services/traffic-flow-read-http.owned.test.ts new file mode 100644 index 00000000..5545af27 --- /dev/null +++ b/backend/src/services/traffic-flow-read-http.owned.test.ts @@ -0,0 +1,199 @@ +import assert from 'node:assert/strict' +import Fastify from 'fastify' +import { serializerCompiler,validatorCompiler } from '@fastify/type-provider-zod' +import { Client,StreamableHTTPClientTransport } from '@modelcontextprotocol/client' +import { setTimeout as delay } from 'node:timers/promises' +import { pool,closePool } from '../db/index.js' +import { withSystemDatabaseOperationLock } from '../db/operation-lock.js' +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 mcpTransport from '../routes/mcp-transport.js' +import trafficFlowRoutes from '../routes/traffic-flow.js' +import logsRoutes from '../routes/logs.js' +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 { disableRipeEnqueueForTests } from './traffic-flow-ripe.js' +import { readFlowCatalogWindow } from './traffic-flow-catalog-read.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' +import { readBoundedFlowSql } from './traffic-flow-bounded-sql.js' +import { EventEmitter } from 'node:events' +import type { ServerResponse } from 'node:http' + +assert.equal(new URL(process.env.DATABASE_URL!).hostname,'127.0.0.1') +assert.match(process.env.MCP_STATE_DIR!,/mm-flow-http-owned-/) +const actor={id:'flow-http-owned',apps:['mm'],permissions:['mm:settings:admin','mm:mcp:admin','mm:traffic:read'],expiresAt:Date.now()+3600000} +const app=Fastify();app.setSerializerCompiler(serializerCompiler);app.setValidatorCompiler(validatorCompiler) +let client:Client|undefined +try{ + await applySqlMigrations(pool);await updateMcpSettings({enabled:true},actor) + 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) + 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()}) + await readFlowCatalogWindow(5,[1]) + { + const budget=createFlowReadBudget({maxOwners:2,maxReservedBytes:200}),response=new EventEmitter() as ServerResponse,owner=createFlowReadResponseOwner(response,budget,{maxOwners:1,maxReservedBytes:100}) + let began!:()=>void,release!:()=>void,entered=false,lateSqlCalls=0,dangling:Promise|undefined + const started=new Promise(resolve=>{began=resolve}),hold=new Promise(resolve=>{release=resolve}) + const watched={query:async(sql:string,values:unknown[])=>{const result=await pool.query(sql,values);began();await hold;return result}} + await assert.rejects(owner.run(100,async()=>{dangling=readBoundedFlowSql(watched,'SELECT 1 AS value',[],{maxRows:2,maxEncodedBytes:1024});void dangling.catch(()=>{});await started;throw Error('early handler failure')}),/early handler failure/) + response.emit('close');assert.equal(budget.stats().owners,1) + const exclusive=sharedDatabaseGate.exclusive(async()=>{ + entered=true + // Actual internal consumer restart read while the owned exclusive callback is + // paused must stay valid; no NetFlow response ALS context is inherited here. + invalidateTrafficFlowSettingsCache();assert.equal((await getTrafficFlowSettingsRow()).id,1) + }) + await delay(20);assert.equal(entered,false,'Dangling child computation must hold restore admission') + const outsiderResponse=new EventEmitter() as ServerResponse,outsider=createFlowReadResponseOwner(outsiderResponse,budget,{maxOwners:1,maxReservedBytes:100}) + await assert.rejects(outsider.run(100,()=>readBoundedFlowSql({query:async(sql,values)=>{lateSqlCalls++;return pool.query(sql,values)}},'SELECT 2 AS value',[],{maxRows:2,maxEncodedBytes:1024})),/базой/) + outsiderResponse.emit('close');assert.equal(lateSqlCalls,0,'Outsider source SQL must refuse before execution while paused') + release();await assert.rejects(dangling!,/объём|закрыт/);await exclusive + assert.equal(entered,true);assert.equal(budget.stats().owners,0) + await assert.rejects(owner.run(100,async()=>{lateSqlCalls++;return 3}),/закрыт/) + assert.equal(lateSqlCalls,0,'Closed retired response cannot publish or start new work after restore') + console.log('PASS actual PostgreSQL/shared gate: dangling child holds restore, outsider SQL refused while paused, exclusive internal settings reload succeeds, closed late work refused') + } + await app.register(trafficFlowRoutes,{prefix:'/api'});await app.register(mcpTransport) + await app.register(async child=>{ + child.addHook('preHandler',async request=>{request.user={sub:actor.id}}) + await child.register(logsRoutes);await child.register(trafficRoutes) + },{prefix:'/api'}) + const endpoint=await app.listen({host:'127.0.0.1',port:0}) + const flowUrl=endpoint+'/api/traffic/flow?range=15m' + const before=await fetch(flowUrl) + assert.equal(before.status,200) + const originalTalkers=(await before.json() as {talkers:unknown}).talkers + const withBucketMeasurement=async(action:()=>Promise,read:()=>Promise)=>{ + const connect=pool.connect + let observed=false + pool.connect=(function(this:typeof pool,...args:unknown[]){ + if(args.length)return Reflect.apply(connect,this,args) + return (Reflect.apply(connect,this,[]) as ReturnType).then(connection=>{ + const query=connection.query,release=connection.release + connection.query=(async (...queryArgs:unknown[])=>{ + const result=await Reflect.apply(query,connection,queryArgs) + const statement=queryArgs[0] + if(!observed&&typeof statement==='string'&&statement.startsWith('WITH flow_read_source AS MATERIALIZED')&&statement.includes('FROM flow_buckets fb')){ + observed=true + await action() + } + return result + }) as typeof query + connection.release=(...releaseArgs)=>{ + connection.query=query;connection.release=release;release.apply(connection,releaseArgs) + } + return connection + }) + }) as typeof connect + try{await read();assert(observed,'The request must reach the source measurement')}finally{pool.connect=connect} + } + await withBucketMeasurement(async()=>{ + await pool.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets) VALUES(1,$1,'198.18.1.2','8.8.8.8',6,2,443,200,1)",[bucket]) + },async()=>{ + const overlapped=await fetch(flowUrl) + assert.equal(overlapped.status,200) + assert.deepEqual((await overlapped.json() as {talkers:unknown}).talkers,originalTalkers) + }) + const after=await fetch(flowUrl) + assert.equal(after.status,200) + assert.notDeepEqual((await after.json() as {talkers:unknown}).talkers,originalTalkers) + await pool.query("DELETE FROM flow_buckets WHERE server_id=1 AND bucket_at=$1 AND src='198.18.1.2'::inet",[bucket]) + console.log('PASS REST NetFlow retains one bounded bucket snapshot while the collector commits concurrently') + client=new Client({name:'owned-netflow-read',version:'1'}) + await client.connect(new StreamableHTTPClientTransport(new URL(endpoint+'/mcp'),{requestInit:{headers:{authorization:'Bearer '+access.token}}})) + const call=()=>client!.callTool({name:'mm_netflow_talkers_get',arguments:{serverId:1}}) + let result=await call();assert.ok(!result.isError,JSON.stringify(result));assert.ok(!JSON.stringify(result).includes('FORBIDDEN_')) + assert.equal((result.structuredContent as {result:{retainedRowsBytes:number}}).result.retainedRowsBytes,100) + const scopedQuality=(result.structuredContent as {result:Record}).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 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{ + await globalClient.connect(new StreamableHTTPClientTransport(new URL(endpoint+'/mcp'),{requestInit:{headers:{authorization:'Bearer '+globalAccess.token}}})) + const globalResult=await globalClient.callTool({name:'mm_netflow_talkers_get',arguments:{serverId:1}}) + assert.ok(!globalResult.isError,JSON.stringify(globalResult)) + assert.equal((globalResult.structuredContent as {result:{dataQuality:{bytes:string}}}).result.dataQuality.bytes,'987654') + }finally{await globalClient.close()} + console.log('PASS actual MCP scoped collector counters withheld; existing explicit global telemetry grant receives genuine synthetic receiver quality') + // The production admission guard rejects multi-call batches before SDK dispatch. + const batch=await fetch(endpoint+'/mcp',{method:'POST',headers:{authorization:'Bearer '+access.token,'content-type':'application/json',accept:'application/json, text/event-stream','mcp-protocol-version':'2025-06-18'},body:JSON.stringify([1,2].map(id=>({jsonrpc:'2.0',id,method:'tools/call',params:{name:'mm_netflow_talkers_get',arguments:{serverId:1}}})))}) + assert.equal(batch.status,400) + const rejectedBatch=await batch.json() as {id:unknown;error:{code:number}} + assert.equal(rejectedBatch.id,null) + assert.equal(rejectedBatch.error.code,-32600) + result=await client.callTool({name:'mm_netflow_talkers_get',arguments:{serverId:2}});assert.equal(result.isError,true) + // Revocation during a real bounded source statement must prevent publication. + await withBucketMeasurement(async()=>{await setConnectionStatus(access.connection.id,'paused',actor)},async()=>{ + result=await call() + assert.equal(result.isError,true) + assert.ok(!JSON.stringify(result).includes('198.18.1.1')) + }) + await setConnectionStatus(access.connection.id,'active',actor) + // Four actual REST SSE streams retain their shared global owners. MCP and REST + // share the same admission pool; a rejected fifth read allocates no flow source. + const streams:AbortController[]=[] + try{ + for(let i=0;i<4;i++){ + const controller=new AbortController();streams.push(controller) + const response=await fetch(endpoint+'/api/traffic/flow/live?serverId=1',{signal:controller.signal}) + assert.equal(response.status,200) + assert.equal(response.headers.get('x-mm-netflow-stream'),null) + const reader=response.body!.getReader();await reader.read() + } + assert.equal(flowReadBudget.stats().owners,4) + result=await call();assert.equal(result.isError,true);assert.match(JSON.stringify(result),/RATE_LIMITED/) + const rest=await fetch(endpoint+'/api/traffic/flow?range=5m');assert.equal(rest.status,429);await rest.text() + }finally{for(const stream of streams)stream.abort()} + for(let i=0;i<100&&flowReadBudget.stats().owners;i++)await delay(20) + assert.equal(flowReadBudget.stats().owners,0) + result=await call();assert.ok(!result.isError) + console.log('PASS actual production HTTP SDK: scoped source/no hidden rows, multi-call batch refusal, source-time revocation, four REST SSE owners versus MCP/REST rejection, abort cleanup and successful readmission') + + const logsUrl=endpoint+'/api/logs/live?serverId=1&cursor=0' + const trafficUrl=endpoint+'/api/traffic/servers/1/live' + const liveFlowUrl=endpoint+'/api/traffic/flow/live?serverId=1' + const signal=AbortSignal.timeout(8000) + async function firstEvent(url:string,event:string) { + const response=await fetch(url,{signal});assert.equal(response.status,200) + const reader=response.body!.pipeThrough(new TextDecoderStream()).getReader() + let body='' + while (!/event: \w+\n/.test(body) || !body.endsWith('\n\n')) { + const chunk=await reader.read().catch(cause=>{throw Error(`Waiting for first ${event} at ${url}: ${body}`,{cause})}) + assert.equal(chunk.done,false,body);body+=chunk.value + } + assert.match(body,new RegExp(`event: ${event}\\n`)) + return async ()=>{ + let rest='' + for (;;) { + const chunk=await reader.read().catch(cause=>{throw Error(`Waiting for ${event} stream to finish at ${url}: ${rest}`,{cause})}) + if(chunk.done)return rest;rest+=chunk.value + } + } + } + const [finishLogs,finishFlow]=await Promise.all([firstEvent(logsUrl,'batch'),firstEvent(liveFlowUrl,'error')]) + await withSystemDatabaseOperationLock(()=>pool.connect(),async()=>{ + assert.match(await finishLogs(),/^event: reset\ndata: \{"reason":"database-maintenance"\}\n\n$/) + assert.equal(await finishFlow(),'') + for (const url of [logsUrl,trafficUrl]) { + const denied=await fetch(url,{signal});assert.equal(denied.status,503);await denied.text() + } + const deniedFlow=await fetch(liveFlowUrl,{signal});assert.equal(deniedFlow.status,200);assert.equal(await deniedFlow.text(),':\n\n') + }) + console.log('PASS PostgreSQL database drain: REST logs reset, active NetFlow ends without data, all three live routes refuse new database reads') +}finally{await client?.close();console.log('Owned HTTP SDK client closed');app.server.closeAllConnections();await app.close();console.log('Owned HTTP Fastify server closed');await closePool();console.log('Owned HTTP application pool closed')} +// Imported application maintenance timers are outside this isolated read test. +// Required transport/server/pool cleanup above must complete before terminal exit. +process.exit(0) diff --git a/backend/src/services/traffic-flow-read-policy.ts b/backend/src/services/traffic-flow-read-policy.ts new file mode 100644 index 00000000..96d82b4b --- /dev/null +++ b/backend/src/services/traffic-flow-read-policy.ts @@ -0,0 +1,47 @@ +import { getFlowReadResourceProfile } from "./traffic-flow-read-resource-profile.js" +import { bindSql } from "../db/sql-bind.js" +import { pool } from "../db/index.js" +import { createFlowReadBudget, trackFlowReadComputation } from "./traffic-flow-read-budget.js" +import { readBoundedFlowSql } from "./traffic-flow-bounded-sql.js" +import type { QueryResultRow } from "pg" +const flowReadResourceProfile=getFlowReadResourceProfile() +// Provisional reservation: replace only after measured maximum source/DTO/clone peaks. +// These are admission envelopes, not a claim of a 2 GiB installation proof. +export const FLOW_READ_RESERVATION_BYTES=flowReadResourceProfile.reservationBytes +export const FLOW_SSE_MAX_FRAME_BYTES=flowReadResourceProfile.responseOutputBytes +export const flowReadBudget=createFlowReadBudget({maxOwners:flowReadResourceProfile.maxOwners,maxReservedBytes:flowReadResourceProfile.maxOwners*FLOW_READ_RESERVATION_BYTES}) +export const flowResponseReadLimits={maxOwners:1,maxReservedBytes:FLOW_READ_RESERVATION_BYTES} +const sqlSourceLimits = { maxRows: 50000, maxEncodedBytes: 16 * 1024 * 1024 } + +export async function flowReadDbAll( + sql: string, params?: unknown[] | Record, +): Promise { + const bound = bindSql(sql, params) + return readBoundedFlowSql(pool, bound.text, bound.values ?? [], sqlSourceLimits) +} + +/** A collector commit must not change the bucket rows between budget measurement and decode. */ +export async function flowReadBucketSnapshot(sql: string, params: unknown[]): Promise { + const bound = bindSql(sql, params) + return trackFlowReadComputation(async () => { + const client = await pool.connect() + let discard = true + try { + await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + const rows = await readBoundedFlowSql(client, bound.text, bound.values ?? [], sqlSourceLimits) + await client.query("COMMIT") + discard = false + return rows + } catch (error) { + try { + await client.query("ROLLBACK") + discard = false + } catch { + // A connection with an uncertain transaction state must not be reused. + } + throw error + } finally { + client.release(discard) + } + }) +} diff --git a/backend/src/services/traffic-flow-read-resource-profile.test.ts b/backend/src/services/traffic-flow-read-resource-profile.test.ts new file mode 100644 index 00000000..9575827d --- /dev/null +++ b/backend/src/services/traffic-flow-read-resource-profile.test.ts @@ -0,0 +1,18 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { parseFlowReadResourceProfile } from "./traffic-flow-read-resource-profile.js" + +test("standard profile preserves existing capacities without an opt-in",()=>{ + const profile=parseFlowReadResourceProfile(undefined) + assert.deepEqual(profile,{name:"standard",maxOwners:4,reservationBytes:134217728,querySourceBytes:16777216,responseSourceBytes:33554432,responseOutputBytes:8388608,parallelSql:8}) + assert.deepEqual(parseFlowReadResourceProfile("standard"),profile) +}) +test("constrained profile preserves eight child slots and permits UI plus AI owners",()=>{ + const profile=parseFlowReadResourceProfile("constrained") + assert.deepEqual(profile,{name:"constrained",maxOwners:2,reservationBytes:33554432,querySourceBytes:2097152,responseSourceBytes:4194304,responseOutputBytes:2097152,parallelSql:8}) + assert.equal(Reflect.set(profile,"maxOwners",999),false) + assert.equal(profile.maxOwners,2) +}) +test("invalid explicit profile never falls back to standard",()=>{ + for(const invalid of ["","unknown","CONSTRAINED"," constrained","standard "])assert.throws(()=>parseFlowReadResourceProfile(invalid),/MM_FLOW_READ_RESOURCE_PROFILE/) +}) diff --git a/backend/src/services/traffic-flow-read-resource-profile.ts b/backend/src/services/traffic-flow-read-resource-profile.ts new file mode 100644 index 00000000..76c0386d --- /dev/null +++ b/backend/src/services/traffic-flow-read-resource-profile.ts @@ -0,0 +1,24 @@ +export interface FlowReadResourceProfile { + name:"standard"|"constrained" + maxOwners:number + reservationBytes:number + querySourceBytes:number + responseSourceBytes:number + responseOutputBytes:number + parallelSql:number +} +const MiB=1024*1024 +/** Explicit opt-in capacity policy. These reservations are accounting units, + * not a universal V8/native-memory bound or whole-installation sizing proof. */ +export function parseFlowReadResourceProfile(value:string|undefined):Readonly{ + if(value===undefined||value==="standard")return Object.freeze({name:"standard",maxOwners:4,reservationBytes:128*MiB,querySourceBytes:16*MiB,responseSourceBytes:32*MiB,responseOutputBytes:8*MiB,parallelSql:8}) + if(value==="constrained")return Object.freeze({name:"constrained",maxOwners:2,reservationBytes:32*MiB,querySourceBytes:2*MiB,responseSourceBytes:4*MiB,responseOutputBytes:2*MiB,parallelSql:8}) + throw Error("MM_FLOW_READ_RESOURCE_PROFILE must be standard or constrained") +} +let selected:Readonly|undefined +/** Module import is pure: read-policy first calls this in its module body, + * after pool/config dependencies have initialized dotenv. Runtime limits then + * stay fixed for that process and cannot relax halfway through a response. */ +export function getFlowReadResourceProfile():Readonly{ + return selected??=parseFlowReadResourceProfile(process.env.MM_FLOW_READ_RESOURCE_PROFILE) +} diff --git a/backend/src/services/traffic-flow-ripe-prune-owned.test.ts b/backend/src/services/traffic-flow-ripe-prune-owned.test.ts new file mode 100644 index 00000000..517e2cf5 --- /dev/null +++ b/backend/src/services/traffic-flow-ripe-prune-owned.test.ts @@ -0,0 +1,69 @@ +import assert from "node:assert/strict" +import type { PoolClient } from "pg" +import { pool } from "../db/index.js" +import { enqueueRipeMisses, flushRipeQueueForTests, lookupRipeCached, pauseRipeBackgroundWork, pruneRipeSqlite, resumeRipeBackgroundWork, seedRipeCacheForTests, setRipeFetchForTests } from "./traffic-flow-ripe.js" +import { commitFlowWrite,flowWriteQuery } from "./traffic-flow-write-transaction.js" + +const now=Date.parse("2026-10-24T12:00:00Z"),calls:Array<{sql:string;values:unknown[]}>=[] +let pending=0,committed=0,failDelete=0,globalCalls=0,released:boolean|undefined, writerBusy=false +const globalQuery=pool.query, globalConnect=pool.connect +pool.query=(async()=>{globalCalls++;throw Error("global query must not be used by cache writes")}) as typeof pool.query +const connect=async()=>({query:async(input:string|{text:string;values:unknown[]},params?:unknown[])=>{ + const sql=typeof input==="string"?input:input.text,values=(typeof input==="string"?params:input.values)??[] + calls.push({sql,values}) + if(sql.includes("pg_try_advisory_xact_lock_shared"))return {rows:[{acquired:!writerBusy}]} + if(sql.startsWith("DELETE")){pending++;if(pending===failDelete)throw Error("prune SQL fault")} + if(sql==="COMMIT")committed=pending + if(sql==="ROLLBACK")pending=committed + return {rows:[],command:sql} +},release:(destroy:boolean)=>{released=destroy}} as unknown as PoolClient) +try{ + const run=()=>commitFlowWrite({id:"11111111-1111-4111-8111-111111111111",kind:"ingest",sourceProof:"a".repeat(64)},async()=>{ + await pruneRipeSqlite(now,{query:flowWriteQuery,strict:true});return {maintenance:true} + },async()=>{},connect) + await run() + assert.equal(globalCalls,0);assert.equal(committed,3);assert.equal(released,false) + const deletions=calls.filter(call=>call.sql.startsWith("DELETE")) + assert.equal(deletions.length,3) + assert.deepEqual(deletions.map(call=>call.values),[["2026-10-23T12:00:00.000Z"],["2026-10-24T06:00:00.000Z"],["2026-10-23T12:00:00.000Z"]]) + assert.ok(calls.findIndex(call=>call.sql.includes("INSERT INTO flow_write_receipts"))>calls.findIndex(call=>call.sql.includes("DELETE FROM flow_asn_meta"))) + pending=0;committed=0;failDelete=2;calls.length=0 + await assert.rejects(run(),/prune SQL fault/) + assert.equal(pending,0);assert.equal(committed,0);assert.equal(globalCalls,0) + assert.ok(calls.some(call=>call.sql==="ROLLBACK"));assert.ok(!calls.some(call=>call.sql.includes("INSERT INTO flow_write_receipts")||call.sql==="COMMIT")) + pool.connect=connect as typeof pool.connect + calls.length=0;failDelete=0;writerBusy=true + await pruneRipeSqlite(now) + assert.ok(calls.some(call=>call.sql.includes("pg_try_advisory_xact_lock_shared"))) + assert.equal(calls.at(-1)?.sql,"ROLLBACK") + assert.ok(!calls.some(call=>call.sql.startsWith("DELETE"))) + + calls.length=0;writerBusy=false + await pruneRipeSqlite(now) + assert.equal(calls.filter(call=>call.sql.startsWith("DELETE")).length,3) + assert.equal(calls.at(-1)?.sql,"COMMIT") + + seedRipeCacheForTests({prefix:"9.9.9.9/32",asn:0,country:"",lat:null,lng:null,holder:"",ok:false,fetchedAt:Date.now()}) + for (const busy of [false, true]) { + calls.length=0 + writerBusy=busy + const ip=busy?"1.1.1.1":"8.8.8.8", asn=busy?13335:15169 + setRipeFetchForTests(async input=>Response.json(String(input).includes("network-info") + ? {data:{prefix:`${ip}/32`,asns:[asn]}} + : {data:{holder:"Example"}})) + enqueueRipeMisses([ip]) + await flushRipeQueueForTests() + await pauseRipeBackgroundWork() + assert.equal(lookupRipeCached(ip)?.asn,asn) + assert.equal(calls.filter(call=>call.sql.includes("pg_try_advisory_xact_lock_shared")).length,2) + const inserts=calls.filter(call=>call.sql.includes("INSERT INTO flow_")) + assert.equal(inserts.length,busy?0:2) + if (!busy) { + assert.equal(inserts.find(call=>call.sql.includes("flow_asn_meta"))?.values[0],asn) + assert.equal(inserts.find(call=>call.sql.includes("flow_ip_meta"))?.values[0],`${ip}/32`) + } + resumeRipeBackgroundWork() + } + assert.equal(globalCalls,0) +}finally{pool.query=globalQuery;pool.connect=globalConnect} +console.log("traffic-flow-ripe-prune-owned.test.ts: owned pruning and background admission for pruning and RIPE cache writes") diff --git a/backend/src/services/traffic-flow-ripe.ts b/backend/src/services/traffic-flow-ripe.ts index fbb7941b..702b271a 100644 --- a/backend/src/services/traffic-flow-ripe.ts +++ b/backend/src/services/traffic-flow-ripe.ts @@ -1,5 +1,7 @@ import { databaseBackgroundWork } from "./database-background-work.js" -import { dbAll, dbQuery } from "../db/index.js" +import { dbAll } from "../db/index.js" +import { bindSql } from "../db/sql-bind.js" +import { withBackgroundPartitionWrite } from "./partition-write-transaction.js" import { canonicalIp, ipv4ToInt, ipv6ToInt, isNonPublicIp, parseCidrV4, parseCidrV6 } from "./traffic-flow-ip.js" import { resolveRipeCountry } from "./traffic-flow-brands.js" @@ -247,7 +249,7 @@ async function loadPg(): Promise { function persist(entry: FlowIpMeta): void { if (!persistEnabled) return - void databaseBackgroundWork.track(dbQuery(` + void databaseBackgroundWork.track(withBackgroundPartitionWrite(({ client }) => client.query(bindSql(` INSERT INTO flow_ip_meta (prefix, asn, country, lat, lng, holder, ok, fetched_at) VALUES (@prefix, @asn, @country, @lat, @lng, @holder, @ok, @fetchedAt) ON CONFLICT(prefix) DO UPDATE SET @@ -262,12 +264,12 @@ function persist(entry: FlowIpMeta): void { holder: entry.holder, ok: entry.ok ? 1 : 0, fetchedAt: new Date(entry.fetchedAt).toISOString(), - })).catch(() => { /* ignore persist errors */ }) + })))).catch(() => { /* ignore persist errors */ }) } function persistAsn(asn: number, holder: string): void { if (!persistEnabled || !asn) return - void databaseBackgroundWork.track(dbQuery(` + void databaseBackgroundWork.track(withBackgroundPartitionWrite(({ client }) => client.query(bindSql(` INSERT INTO flow_asn_meta (asn, holder, fetched_at) VALUES (@asn, @holder, @fetchedAt) ON CONFLICT(asn) DO UPDATE SET holder=excluded.holder, fetched_at=excluded.fetched_at @@ -275,21 +277,23 @@ function persistAsn(asn: number, holder: string): void { asn, holder, fetchedAt: new Date().toISOString(), - })).catch(() => { /* ignore */ }) + })))).catch(() => { /* ignore */ }) } /** Удаляет просроченный RIPE-кэш с диска (hit 24h / negative 6h). */ -export async function pruneRipeSqlite(nowMs = Date.now()): Promise { +export async function pruneRipeSqlite(nowMs = Date.now(), owned?: {query:(sql:string,params:unknown[])=>Promise;strict:true}): Promise { if (!persistEnabled) return - try { + const prune = async (query: (sql: string, params: unknown[]) => Promise) => { const hitCutoff = new Date(nowMs - HIT_TTL_MS).toISOString() const negCutoff = new Date(nowMs - NEG_TTL_MS).toISOString() - await dbQuery(`DELETE FROM flow_ip_meta WHERE ok != 0 AND fetched_at < ?`, [hitCutoff]) - await dbQuery(`DELETE FROM flow_ip_meta WHERE ok = 0 AND fetched_at < ?`, [negCutoff]) - await dbQuery(`DELETE FROM flow_asn_meta WHERE fetched_at < ?`, [hitCutoff]) - } catch { - /* table may not exist in isolated tests */ + await query(`DELETE FROM flow_ip_meta WHERE ok != 0 AND fetched_at < ?`, [hitCutoff]) + await query(`DELETE FROM flow_ip_meta WHERE ok = 0 AND fetched_at < ?`, [negCutoff]) + await query(`DELETE FROM flow_asn_meta WHERE fetched_at < ?`, [hitCutoff]) } + if (owned) return prune(owned.query) + await databaseBackgroundWork.track(withBackgroundPartitionWrite(({ client }) => + prune((sql, params) => client.query(bindSql(sql, params))), + )).catch(() => { /* best-effort cache maintenance */ }) } function negative(prefix: string): FlowIpMeta { @@ -349,6 +353,32 @@ export function lookupRipeCached(ip: string): FlowIpMeta | null { : null } +/** Stable local-cache view. Unlike lookupRipeCached, this never triggers loadPg, + * enqueues HTTP, advances runtime counters or changes the global cache. */ +export function captureRipeCacheLookup() { + if(mem.size>50000)throw Error("RIPE cache snapshot exceeds bound") + const observedAt=Date.now(),copy=(entry:FlowIpMeta)=>({...entry}) + const narrow4=new Map([...v24Index].map(([key,rows])=>[key,rows.map(row=>({...row,entry:copy(row.entry)}))])) + const wide4=wideIndex.map(row=>({...row,entry:copy(row.entry)})) + const narrow6=new Map([...v48Index].map(([key,rows])=>[key,rows.map(row=>({...row,entry:copy(row.entry)}))])) + const wide6=wideV6Index.map(row=>({...row,entry:copy(row.entry)})) + const fresh=(entry:FlowIpMeta)=>observedAt-entry.fetchedAt{ + const trimmed=canonicalIp(ip) + if(!trimmed)return null + if(isNonPublicIp(trimmed))return {...negative(hostPrefix(trimmed)),fetchedAt:observedAt} + let best:FlowIpMeta|null=null,bestLength=-1 + if(trimmed.includes(":")){ + const addr=ipv6ToInt(trimmed);if(addr===null)return null + for(const row of [...(narrow6.get(addr>>80n)??[]),...wide6])if(fresh(row.entry)&&addr>>row.shift===row.net&&row.prefixLen>bestLength){best=row.entry;bestLength=row.prefixLen} + }else{ + const addr=ipv4ToInt(trimmed);if(addr===null)return null + for(const row of [...(narrow4.get(addr>>>8)??[]),...wide4])if(fresh(row.entry)&&((addr&row.mask)>>>0)===row.net&&row.prefixLen>bestLength){best=row.entry;bestLength=row.prefixLen} + } + return best?{...best,country:resolveRipeCountry(best.country,best.asn,best.holder)||"—"}:null + }} +} + async function ripeJson(path: string, resource: string): Promise { fetchCount += 1 const url = `${RIPE_BASE}/${path}/data.json?resource=${encodeURIComponent(resource)}` diff --git a/backend/src/services/traffic-flow-server-source.ts b/backend/src/services/traffic-flow-server-source.ts new file mode 100644 index 00000000..9ffd0254 --- /dev/null +++ b/backend/src/services/traffic-flow-server-source.ts @@ -0,0 +1,16 @@ +import { getTableColumns } from "drizzle-orm" +import { servers } from "../db/schema.js" +import { pool } from "../db/index.js" +import { readBoundedFlowSql } from "./traffic-flow-bounded-sql.js" +type ServerRow=typeof servers.$inferSelect +const columns=Object.entries(getTableColumns(servers)) +const quote=(value:string)=>'"'+value.replaceAll('"','""')+'"' +export async function readBoundedFlowServerRows(ids:readonly number[]):Promise { + if(ids.some(id=>!Number.isSafeInteger(id)||id<=0)||ids.length>1000)throw Error("Некорректная область серверов NetFlow") + if(!ids.length)return [] + // Timestamp strings retain the normal Drizzle text parser behavior. + const projection=columns.map(([key,column])=>quote(column.name)+(column.columnType==="PgTimestampString"?"::text":"")+" AS "+quote(key)).join(",") + const rows=await readBoundedFlowSql(pool,"SELECT "+projection+" FROM servers WHERE id=ANY($1::bigint[]) ORDER BY id",[[...ids]],{maxRows:1000,maxEncodedBytes:4194304}) + return rows.map(row=>Object.fromEntries(columns.map(([key,column])=>[key,row[key]===null?null:column.mapFromDriverValue(row[key])])) as ServerRow) +} +export async function getBoundedFlowServerRowById(id:number){return(await readBoundedFlowServerRows([id]))[0]} diff --git a/backend/src/services/traffic-flow-settings-execution.test.ts b/backend/src/services/traffic-flow-settings-execution.test.ts new file mode 100644 index 00000000..fc3d2891 --- /dev/null +++ b/backend/src/services/traffic-flow-settings-execution.test.ts @@ -0,0 +1,99 @@ +import assert from "node:assert/strict" +import { createFlowSettingsExecution, type FlowSettingsConfiguration, type FlowSettingsDependencies, type FlowSettingsPhase } from "./traffic-flow-settings-execution.js" +import { reviewFlowSettingsPlan } from "../modules/mcp/flow-settings-review.js" + +const baseline = (): FlowSettingsConfiguration => ({ id: 1, enabled: false, collectorIp: "10.255.254.1", flowListenPort: 4739, + wgListenPort: 51821, prefix: "10.255.254.0/24", publicEndpoint: "", hubServerId: null, + retentionHours: 24, topN: 200, mapServiceMinSharePct: 5, hostPublicKey: "", hostPrivateKey: "", + peersJson: [{ serverId: 1, publicKey: "peer-public", allowedIps: ["10.255.254.2/32"] }], createdAt: "initial", futureConfig: { keep: true } }) +function fixture() { + let state = baseline(), writes = 0, starts = 0, generations = 0, reads = 0 + let denied = false + let onRead = () => {}, onWrite = () => {}, onStart = () => {}, onIntent = (_phase: FlowSettingsPhase) => {} + const phases: string[] = [] + const deps: FlowSettingsDependencies = { + authorize: async () => { if (denied) throw new Error("revoked") }, + exclusive: async work => work(), + readConfiguration: async () => { reads++; onRead(); return structuredClone(state) }, + intent: async phase => { phases.push(phase); onIntent(phase) }, + compareAndSet: async (_expected, desired) => { writes++; state = structuredClone(desired); onWrite(); return true }, + generateKeys: () => { generations++; return { publicKey: "synthetic-public", privateKey: "synthetic-private" } }, + startListener: async fence => { await fence(); starts++; onStart() }, + } + return { engine: createFlowSettingsExecution(deps), deps, phases, get state() { return state }, + get writes() { return writes }, get starts() { return starts }, get reads() { return reads }, get generations() { return generations }, + deny: () => { denied = true }, readHook: (fn: typeof onRead) => { onRead = fn }, + writeHook: (fn: typeof onWrite) => { onWrite = fn }, startHook: (fn: typeof onStart) => { onStart = fn }, + intentHook: (fn: typeof onIntent) => { onIntent = fn } } +} +let cases = 0 +const patches = [ { enabled: true }, { enabled: false }, { collectorIp: "10.255.254.3" }, { flowListenPort: 4740 }, + { wgListenPort: 51822 }, { prefix: "10.200.0.0/24" }, { publicEndpoint: "flow.example" }, { hubServerId: 3 }, + { hubServerId: null }, { retentionHours: 2147483647 }, { topN: 1000 }, { mapServiceMinSharePct: 0 }, { mapServiceMinSharePct: 100 } ] +for (const patch of [...patches, Object.assign({}, ...patches)]) { + const f = fixture(), before = structuredClone(f.state) + const plan = await f.engine.prepare({ kind: "patch", patch }) + const receipt = await f.engine.execute(plan) + assert.equal(receipt.status, "applied"); assert.equal(receipt.configurationConfirmed, true) + assert.equal(receipt.listenerReadinessConfirmed, false); assert.equal(f.starts, 1); assert.equal(f.writes, 1) + assert.deepEqual(f.state, { ...before, ...patch }); assert.deepEqual(f.phases, ["configuration", "listener"]) + assert.equal(reviewFlowSettingsPlan(plan).collectorRestartRequired, true); cases++ +} +for (const patch of [{}, { bad: true }, { hostPrivateKey: "forbidden" }, { flowListenPort: 0 }, { flowListenPort: 65536 }, + { topN: 1001 }, { retentionHours: 0 }, { mapServiceMinSharePct: -1 }, { enabled: "false" }, { hubServerId: 0 }, { prefix: "::/64" }]) { + const f = fixture() + await assert.rejects(f.engine.prepare({ kind: "patch", patch: patch as never })) + assert.equal(f.reads, 0); assert.equal(f.writes, 0); assert.equal(f.starts, 0); cases++ +} +for (const mutate of [ + (state: FlowSettingsConfiguration) => { state.hostPrivateKey = "changed" }, + (state: FlowSettingsConfiguration) => { state.peersJson = [] }, + (state: FlowSettingsConfiguration) => { state.futureConfig = { keep: false } }, +]) { + const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 20 } }) + mutate(f.state); await assert.rejects(f.engine.execute(plan)); assert.equal(f.writes, 0); assert.equal(f.starts, 0); cases++ +} +for (const phase of ["configuration", "listener"] as const) { + const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 20 } }) + f.intentHook(current => { if (current === phase) f.deny() }) + if (phase === "configuration") { await assert.rejects(f.engine.execute(plan)); assert.equal(f.writes, 0) } + else { assert.equal((await f.engine.execute(plan)).status, "partial"); assert.equal(f.writes, 1) } + assert.equal(f.starts, 0); cases++ +} +for (const phase of ["write", "listener", "read-after-write", "read-after-listener"] as const) { + const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 20 } }) + if (phase === "write") f.writeHook(() => { throw new Error("synthetic-private") }) + if (phase === "listener") f.startHook(() => { throw new Error("synthetic-private") }) + if (phase === "read-after-write") f.writeHook(() => f.readHook(() => { throw new Error("synthetic-private") })) + if (phase === "read-after-listener") f.startHook(() => f.readHook(() => { throw new Error("synthetic-private") })) + const receipt = await f.engine.execute(plan) + assert.equal(receipt.status, ["write", "read-after-write"].includes(phase) ? "unknown" : "partial") + assert.equal(f.writes, 1); assert.ok(f.starts <= 1); assert.equal(receipt.retries, 0) + assert.ok(!JSON.stringify(receipt).includes("synthetic-private")); cases++ +} +for (const pair of [["", ""], ["old-public", ""], ["", "old-private"], ["old-public", "old-private"]]) { + const f = fixture(); [f.state.hostPublicKey, f.state.hostPrivateKey] = pair + const original = structuredClone(f.state), present = Boolean(pair[0] && pair[1]) + const plan = await f.engine.prepare({ kind: "ensure-keys" }), receipt = await f.engine.execute(plan) + assert.equal(receipt.status, "applied"); assert.equal(receipt.created, !present) + assert.equal(f.generations, present ? 0 : 1); assert.equal(f.writes, present ? 0 : 1); assert.equal(f.starts, 0) + assert.deepEqual(f.state, present ? original : { ...original, hostPublicKey: "synthetic-public", hostPrivateKey: "synthetic-private" }) + assert.ok(!JSON.stringify({ receipt, review: reviewFlowSettingsPlan(plan) }).includes("private\"")); cases++ +} +{ + const f = fixture(), plan = await f.engine.prepare({ kind: "ensure-keys" }) + f.deps.compareAndSet = async () => false + assert.equal((await f.engine.execute(plan)).status, "unknown"); assert.equal(f.starts, 0); cases++ +} +for (const key of ["hostPrivateKey", "peersJson", "futureConfig"]) { + const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 30 } }) + f.writeHook(() => { f.state[key] = "collateral" }) + assert.equal((await f.engine.execute(plan)).status, "unknown"); assert.equal(f.starts, 0); cases++ +} +{ + const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 30 } }) + f.deps.startListener = async fence => { f.deny(); await fence(); throw new Error("must not reach side effect") } + const receipt = await f.engine.execute(plan) + assert.equal(receipt.status, "partial"); assert.equal(receipt.attempts, 2); assert.equal(f.starts, 0); cases++ +} +console.log(`NetFlow settings candidate: ${cases} full-field, private conservation, intent revocation, lost ACK, listener and key lifecycle cases; injected dependencies only, no SQL/network/collector starts`) diff --git a/backend/src/services/traffic-flow-settings-execution.ts b/backend/src/services/traffic-flow-settings-execution.ts new file mode 100644 index 00000000..488df7fa --- /dev/null +++ b/backend/src/services/traffic-flow-settings-execution.ts @@ -0,0 +1,114 @@ +import { isDeepStrictEqual } from "node:util" +import { trafficFlowSettingsPatchSchema, type TrafficFlowSettingsPatch } from "@mmapp/contracts/traffic-flow" + +export const flowSettingsPatchSchema = trafficFlowSettingsPatchSchema.strict().refine(value => Object.values(value).some(item => item !== undefined)) +/** Private, complete configuration, including keys and peer JSON. Exclude only ingestion + * telemetry and updatedAt. Keep unknown future configuration fields in this object. */ +export type FlowSettingsConfiguration = Record & { hostPublicKey: string; hostPrivateKey: string } +export type FlowSettingsCommand = { kind: "patch"; patch: TrafficFlowSettingsPatch } | { kind: "ensure-keys" } +export type FlowSettingsPlan = { command: FlowSettingsCommand; before: FlowSettingsConfiguration } +export type FlowSettingsPhase = "configuration" | "listener" +export type FlowSettingsReceipt = { + status: "applied" | "partial" | "unknown" + configurationConfirmed: boolean + listener: "not-requested" | "request-acknowledged" | "unknown" + listenerReadinessConfirmed: false + created?: boolean + publicKey?: string + attempts: number + retries: 0 +} +export type FlowSettingsDependencies = { + /** Must cover the full server catalog, global permission, protections and live grant. */ + authorize: () => Promise + /** Serialize with other settings/key/overlay writers, not only this factory. */ + exclusive: (work: () => Promise) => Promise + readConfiguration: () => Promise + /** Persist one owned intent before dispatch; must refuse an existing/unknown intent. */ + intent: (phase: FlowSettingsPhase) => Promise + /** Atomic compare-and-set of the complete configuration. False means no write. + * Timestamp/cache invalidation belong to this adapter. Never retry an exception. */ + compareAndSet: (expected: FlowSettingsConfiguration, desired: FlowSettingsConfiguration) => Promise + generateKeys: () => { publicKey: string; privateKey: string } + /** The full REST stop/drain/flush/start operation, including enabled=false. + * The adapter must await fence before each stop/flush/prune/spawn dispatch, + * including after its internal awaits. An outer fence alone is insufficient. */ + startListener: (fence: () => Promise) => Promise +} +const conflict = () => Object.assign(new Error("Настройки NetFlow изменились или не подтверждены"), { statusCode: 409 }) +function configuration(value: FlowSettingsConfiguration): FlowSettingsConfiguration { + if (!value || typeof value !== "object" || typeof value.hostPublicKey !== "string" || typeof value.hostPrivateKey !== "string") throw conflict() + return structuredClone(value) +} +function command(value: FlowSettingsCommand): FlowSettingsCommand { + if (value.kind === "ensure-keys") { + if (Object.keys(value).some(key => key !== "kind")) throw conflict() + return { kind: "ensure-keys" } + } + if (value.kind !== "patch" || Object.keys(value).some(key => !["kind", "patch"].includes(key))) throw conflict() + const parsed = flowSettingsPatchSchema.parse(value.patch) + return { kind: "patch", patch: Object.fromEntries(Object.entries(parsed).filter(([, value]) => value !== undefined)) } +} + +/** Local candidate only: no runtime imports, registration, default dependencies or + * secret export. An acknowledged listener request never proves UDP readiness. */ +export function createFlowSettingsExecution(deps: FlowSettingsDependencies) { + async function prepare(input: FlowSettingsCommand): Promise { + const parsed = command(input) + await deps.authorize() + const before = configuration(await deps.readConfiguration()) + await deps.authorize() + return { command: parsed, before } + } + async function execute(prepared: FlowSettingsPlan): Promise { + const plan = { command: command(prepared.command), before: configuration(prepared.before) } + return deps.exclusive(async () => { + const fence = async (expected: FlowSettingsConfiguration) => { + await deps.authorize() + if (!isDeepStrictEqual(configuration(await deps.readConfiguration()), expected)) throw conflict() + await deps.authorize() + } + await fence(plan.before) + const keysAlreadyPresent = Boolean(plan.before.hostPublicKey && plan.before.hostPrivateKey) + if (plan.command.kind === "ensure-keys" && keysAlreadyPresent) return { + status: "applied", configurationConfirmed: true, listener: "not-requested", listenerReadinessConfirmed: false, + publicKey: plan.before.hostPublicKey, created: false, attempts: 0, retries: 0, + } + const desired = configuration(plan.before) + if (plan.command.kind === "patch") Object.assign(desired, plan.command.patch) + else { + const pair = deps.generateKeys() + if (typeof pair.publicKey !== "string" || typeof pair.privateKey !== "string" || !pair.publicKey || !pair.privateKey) throw conflict() + desired.hostPublicKey = pair.publicKey + desired.hostPrivateKey = pair.privateKey + } + await deps.intent("configuration") + await fence(plan.before) + const receipt: FlowSettingsReceipt = { status: "unknown", configurationConfirmed: false, + listener: "not-requested", listenerReadinessConfirmed: false, attempts: 1, retries: 0 } + try { + if (!await deps.compareAndSet(plan.before, desired)) throw conflict() + await fence(desired) + } catch { return receipt } + receipt.configurationConfirmed = true + if (plan.command.kind === "ensure-keys") return { ...receipt, status: "applied", publicKey: desired.hostPublicKey, created: true } + // Every accepted patch, including display-only and no-op updates, has this + // effect in REST. A denied listener fence stops here without starting it. + receipt.status = "partial" + try { + await deps.intent("listener") + await fence(desired) + } catch { return receipt } + receipt.listener = "unknown" + receipt.attempts++ + try { + await deps.startListener(() => fence(desired)) + await fence(desired) + receipt.listener = "request-acknowledged" + receipt.status = "applied" + } catch { /* No replay or arbitrary exception text in model-facing output. */ } + return receipt + }) + } + return { prepare, execute } +} diff --git a/backend/src/services/traffic-flow-settings-read-profile.owned.test.ts b/backend/src/services/traffic-flow-settings-read-profile.owned.test.ts new file mode 100644 index 00000000..67cc66e1 --- /dev/null +++ b/backend/src/services/traffic-flow-settings-read-profile.owned.test.ts @@ -0,0 +1,77 @@ +import assert from 'node:assert/strict' +import Fastify from 'fastify' +import {serializerCompiler,validatorCompiler}from'@fastify/type-provider-zod' +import{pool,closePool}from'../db/index.js' +import{applySqlMigrations}from'../db/migrate.js' +import{ensurePartitionFor}from'../db/partitions.js' +import routes from'../routes/traffic-flow.js' +import mcpTransport from'../routes/mcp-transport.js' +import{createMcpConnection,updateMcpSettings}from'../modules/mcp/store.js' +import{mcpConnectionCreateSchema}from'@mmapp/contracts/mcp' +import{getTrafficFlowSettingsRow,invalidateTrafficFlowSettingsCache}from'./traffic-flow-settings.js' +import{sharedDatabaseGate}from'./shared-database-gate.js' +import{flowReadBudget}from'./traffic-flow-read-policy.js' +import{getFlowReadResourceProfile}from'./traffic-flow-read-resource-profile.js' +import{resetFlowRingsForTests}from'./traffic-flow-ingest.js' +import{disableRipeEnqueueForTests}from'./traffic-flow-ripe.js' +import{disableCatalogFetchForTests}from'./traffic-flow-classify.js' +import{invalidateFlowAnalyticsCache}from'./traffic-flow-analytics.js' +assert.equal(process.env.MMAPP_OWN_FLOW_PROFILE_TEST,'true') +assert.equal(new URL(process.env.DATABASE_URL!).hostname,'127.0.0.1') +assert.equal(new URL(process.env.DATABASE_URL!).pathname,'/postgres') +assert.match(process.env.MCP_STATE_DIR??'',/mm-flow-profile-cold-owned-/) +const profile=getFlowReadResourceProfile(),app=Fastify();app.setSerializerCompiler(serializerCompiler);app.setValidatorCompiler(validatorCompiler) +let serial=0,grow=false,duplicateDuringPreflight=false,dataQueries=0,decodedSettingsRows=0 +const sizes:number[]=[],metadataCounts:number[]=[],pending=new Mapvoid;reject:(x:unknown)=>void}>(),original=pool.query.bind(pool) +process.on('message',(raw:unknown)=>{if(!raw||typeof raw!=='object')return;const message=raw as{type?:string;id?:unknown;ok?:boolean;result?:unknown;error?:unknown};if(message.type==='client-result'&&typeof message.id==='number'){const call=pending.get(message.id);pending.delete(message.id);if(message.ok)call?.resolve(message.result);else call?.reject(Error(String(message.error)))}}) +const clientCall=(operation:string,payload:Record)=>new Promise((resolve,reject)=>{const id=++serial;pending.set(id,{resolve:value=>resolve(value as T),reject});process.send?.({type:'client-command',id,operation,payload})}) +const http=(url:string)=>clientCall<{status:number;summary:{bytes:number}}>('http',{url}) +pool.query=((...args:unknown[])=>{ + const text=typeof args[0]==='string'?args[0]:(args[0]as{text?:string})?.text + const result=(original as(...args:unknown[])=>Promise<{rows:Record[]} >)(...args) + const settingsData=text?.includes('from "traffic_flow_settings"')&&text.includes('"enabled"') + if(settingsData)dataQueries++ + return result.then(async rows=>{ + if(settingsData)decodedSettingsRows+=rows.rows.length + if(text?.startsWith('SELECT octet_length(row_to_json(traffic_flow_settings)')){sizes.push(Number(rows.rows[0]?.bytes));metadataCounts.push(rows.rows.length);if(grow){grow=false;await original("UPDATE traffic_flow_settings SET public_endpoint=repeat('G',3000000) WHERE id=1")}if(duplicateDuringPreflight){duplicateDuringPreflight=false;await original('ALTER TABLE traffic_flow_settings DROP CONSTRAINT traffic_flow_settings_pkey');await original('INSERT INTO traffic_flow_settings SELECT * FROM traffic_flow_settings')}} + return rows + }) +})as typeof pool.query +try{ + await applySqlMigrations(pool);await pool.query("INSERT INTO servers(id,name,host)VALUES(1,'cold settings','192.0.2.1')") + const at=new Date();await ensurePartitionFor(pool,'flow_buckets','day',at) + 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-(g/20)*interval '1 millisecond',('198.19.'||(g/254)::text||'.'||(g%254+1)::text)::inet,'8.8.8.8',6,g,443,100,1,'ether1'FROM generate_series(1,4000)g",[at]) + const actor={id:'cold-profile-owner',apps:['mm'],permissions:['mm:mcp:admin','mm:settings:admin','mm:traffic:read'],expiresAt:Date.now()+3600000} + await updateMcpSettings({enabled:true},actor) + const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:'cold batch',mode:'observer',permissions:['mm:traffic:read'],expiresAt:new Date(Date.now()+600000).toISOString(),scope:{serverIds:[1],operationIds:['traffic.flow.analytics.read'],allowGlobal:false},limits:{callsPerMinute:120}}),actor) + resetFlowRingsForTests();disableRipeEnqueueForTests();disableCatalogFetchForTests();await app.register(routes,{prefix:'/api'});await app.register(mcpTransport);const endpoint=await app.listen({host:'127.0.0.1',port:0}) + const read=()=>http(endpoint+'/api/traffic/flow/analytics?serverId=1&range=5m') + for(let i=0;i<2;i++){invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();const response=await read();assert.equal(response.status,200,JSON.stringify(response));assert.equal(response.summary.bytes,400000)} + assert.ok(sizes.every(bytes=>bytes>0&&bytes<65536),'Ordinary cold row must charge real small bytes, not fixed4MiB') + const batch=await clientCall('batch',{endpoint,token:access.token});assert.deepEqual(batch,{successes:2,refusals:0},'Permitted bounded SDK batch must retain both complete results') + invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();const beforeGrowth=decodedSettingsRows;grow=true + const changed=await read();assert.equal(changed.status,409);assert.equal(decodedSettingsRows,beforeGrowth,'Growing settings row must not be decoded after scalar reservation') + await pool.query("UPDATE traffic_flow_settings SET public_endpoint='' WHERE id=1") + invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();assert.equal((await read()).status,200) + const restorePrimaryKey=async()=>{await pool.query('DELETE FROM traffic_flow_settings WHERE ctid NOT IN(SELECT ctid FROM traffic_flow_settings WHERE id=1 LIMIT 1)');await pool.query('ALTER TABLE traffic_flow_settings ADD PRIMARY KEY(id)')} + invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();const beforeDuplicateRace=decodedSettingsRows;duplicateDuringPreflight=true + assert.equal((await read()).status,409);assert.equal(decodedSettingsRows,beforeDuplicateRace,'Concurrent duplicates after preflight must not decode a chosen row') + await restorePrimaryKey() + await pool.query('ALTER TABLE traffic_flow_settings DROP CONSTRAINT traffic_flow_settings_pkey');await pool.query('INSERT INTO traffic_flow_settings SELECT * FROM traffic_flow_settings');await pool.query('INSERT INTO traffic_flow_settings SELECT * FROM traffic_flow_settings') + invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();const beforeDuplicates=dataQueries + assert.equal((await read()).status,409);assert.equal(dataQueries,beforeDuplicates);assert.equal(metadataCounts.at(-1),2,'Malformed four-row source must hydrate only two scalar metadata rows') + await restorePrimaryKey();invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();assert.equal((await read()).status,200) + await pool.query("UPDATE traffic_flow_settings SET public_endpoint=repeat('L',$1)WHERE id=1",[profile.name==='standard'?5*1024*1024:3*1024*1024]) + invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();const beforeOversize=dataQueries + assert.equal((await read()).status,409);assert.equal(dataQueries,beforeOversize,'Oversize settings must refuse before the Drizzle data query') + await pool.query("UPDATE traffic_flow_settings SET public_endpoint='' WHERE id=1") + await sharedDatabaseGate.exclusive(async()=>{ + invalidateTrafficFlowSettingsCache();const row=await getTrafficFlowSettingsRow();assert.equal(row.id,1);assert.equal(sharedDatabaseGate.isPaused(),true) + assert.equal((await read()).status,409,'Paused outside public source must refuse') + }) + invalidateTrafficFlowSettingsCache();invalidateFlowAnalyticsCache();assert.equal((await read()).status,200) + assert.equal(flowReadBudget.stats().owners,0) + process.send?.({type:'report',report:{test:'actual public cold/invalidate/settings-growth/exclusive',profile:profile.name,permittedSdkBatch:batch,realOrdinaryRowBytes:sizes.filter(x=>x<65536),growthBlockedBeforeDecode:true,duplicatesAndConcurrentDuplicatesRefusedBeforeDecode:true,maxScalarMetadataRows:Math.max(...metadataCounts),oversizeBlockedBeforeDataQuery:true,internalExclusiveRestartSucceeded:true,owners:0}}) + console.log('PASS actual cold settings/public/exclusive '+profile.name) +}finally{pool.query=original;app.server.closeAllConnections();await app.close();await closePool()} +process.exit(0) diff --git a/backend/src/services/traffic-flow-settings-service.test.ts b/backend/src/services/traffic-flow-settings-service.test.ts new file mode 100644 index 00000000..66392a1b --- /dev/null +++ b/backend/src/services/traffic-flow-settings-service.test.ts @@ -0,0 +1,59 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import { createTrafficFlowSettingsService } from "./traffic-flow-settings-service.js" +import type { FlowSettingsConfiguration } from "./traffic-flow-settings-execution.js" + +export function settingsFixture() { + let configuration: FlowSettingsConfiguration = { id: 1, enabled: false, collectorIp: "10.255.254.1", flowListenPort: 4739, wgListenPort: 51821, prefix: "10.255.254.0/24", publicEndpoint: "", hubServerId: null, retentionHours: 24, topN: 200, mapServiceMinSharePct: 5, hostPublicKey: "public-existing", hostPrivateKey: "PRIVATE-EXISTING", peersJson: [{ opaque: "PEER-SECRET" }], future: { opaque: "FUTURE-SECRET" } } + const intents = new Map(), effects: string[] = [] + let pinned = false, allowed = true, listenerFailure = false, writeUnknown = false, receiptFailure = false, listenerHook = () => {} + const proof = async (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex") + const store = { + read: async () => structuredClone(configuration), + claim: async (id: string, phase: string, _proof: string) => { const key = `${id}:${phase}`; if (intents.has(key)) throw Error("Permanent claim exists"); effects.push(`intent:${phase}`); intents.set(key, null) }, + compareAndSet: async (_id: string, _proof: string, before: FlowSettingsConfiguration, desired: FlowSettingsConfiguration) => { assert.deepEqual(configuration, before); effects.push("write"); configuration = structuredClone(desired); if (writeUnknown) throw Error("lost commit ACK PRIVATE"); return true }, + complete: async (id: string, phase: string, _proof: string, result: unknown) => { effects.push(`complete:${phase}`); if (receiptFailure) throw Error("receipt unavailable PRIVATE"); intents.set(`${id}:${phase}`, result) }, + } + const update = createTrafficFlowSettingsService({ + store: () => store, + exclusive: async work => { effects.push("app->flow-lock"); return work() }, + guard: async work => { effects.push("guard-global"); return work() }, + agentPinned: () => pinned, proof, + startListener: async authorize => { await authorize?.(); effects.push("listener:stop"); listenerHook(); await authorize?.(); effects.push("listener:flush"); if (listenerFailure) throw Error("listener PRIVATE unavailable"); await authorize?.(); effects.push("listener:start-request") }, + }) + return { update, store, proof, effects, intents, get configuration() { return configuration }, + authorize: async () => { if (!allowed) throw Error("revoked PRIVATE") }, + pin: () => { pinned = true }, revoke: () => { allowed = false }, + listenerHook: (hook: () => void) => { listenerHook = hook }, + failListener: () => { listenerFailure = true }, unknownWrite: () => { writeUnknown = true }, failReceipt: () => { receiptFailure = true } } +} +const all = { enabled: false, collectorIp: "10.255.253.1", flowListenPort: 4740, wgListenPort: 51822, prefix: "10.255.253.0/24", publicEndpoint: "flow.test", hubServerId: 2, retentionHours: 48, topN: 100, mapServiceMinSharePct: 0 } +for (const patch of [all, { mapServiceMinSharePct: 0 }, { enabled: false }, { topN: 200 }]) { + const f = settingsFixture(), before = structuredClone(f.configuration), receipt = await f.update(patch, { authorize: f.authorize }) + assert.equal(receipt.status, "applied"); assert.equal(receipt.receiptConfirmed, true); assert.equal(receipt.listenerReadinessConfirmed, false) + assert.deepEqual(f.configuration, { ...before, ...patch }) + assert.deepEqual(f.effects, ["app->flow-lock", "guard-global", "intent:configuration", "write", "intent:listener", "listener:stop", "listener:flush", "listener:start-request", "complete:configuration", "complete:listener"]) + assert.ok(!JSON.stringify(receipt).includes("PRIVATE")); assert.ok(!JSON.stringify(receipt).includes("SECRET")) + await assert.rejects(f.update(patch, { receiptId: receipt.receiptId })); assert.equal(f.effects.filter(x => x === "write").length, 1) +} +for (const patch of [{ collectorIp: "10.255.253.1" }, { prefix: "10.255.253.0/24" }, { flowListenPort: 4740 }, { wgListenPort: 51822 }]) { + const f = settingsFixture(); f.pin(); await assert.rejects(f.update(patch), /закреплены/); assert.equal(f.effects.includes("write"), false) +} +{ + const f = settingsFixture(); f.pin(); assert.equal((await f.update({ collectorIp: "10.255.254.1", mapServiceMinSharePct: 0 })).status, "applied") +} +for (const fail of ["listener", "receipt", "revoke"] as const) { + const f = settingsFixture() + if (fail === "listener") f.failListener() + if (fail === "receipt") f.failReceipt() + if (fail === "revoke") f.listenerHook(f.revoke) + const receipt = await f.update({ topN: 100 }, { authorize: f.authorize }) + assert.equal(receipt.configurationConfirmed, true); assert.equal(receipt.status, "partial"); assert.ok(receipt.listenerError) + assert.ok(!JSON.stringify(receipt).includes("PRIVATE")); assert.equal(f.effects.filter(x => x === "write").length, 1) + if (fail === "revoke") assert.equal(f.effects.includes("listener:flush"), false) +} +{ + const f = settingsFixture(); f.unknownWrite(); await assert.rejects(f.update({ topN: 100 }), /не подтверждено/) + assert.equal(f.configuration.topN, 100); assert.equal(f.effects.includes("listener:stop"), false) +} +console.log("NetFlow full settings service: all ten fields, no-op/display restart, full configuration preservation, permanent two-phase intents, agent pin, partial/unknown sanitized no replay; inert dependencies only") diff --git a/backend/src/services/traffic-flow-settings-service.ts b/backend/src/services/traffic-flow-settings-service.ts new file mode 100644 index 00000000..4fafd721 --- /dev/null +++ b/backend/src/services/traffic-flow-settings-service.ts @@ -0,0 +1,82 @@ +import { randomUUID } from "node:crypto" +import type { TrafficFlowSettingsPatch } from "@mmapp/contracts/traffic-flow" +import { pool } from "../db/index.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { withApplicationMutationLock } from "./config-mutation-lock.js" +import { withFlowConfigurationLock } from "./traffic-flow-host-agent.js" +import { agentSocket } from "./traffic-flow-agent-client.js" +import { invalidateTrafficFlowSettingsCache } from "./traffic-flow-settings.js" +import { startTrafficFlowListener } from "./traffic-flow-ingest.js" +import { flowSettingsPrivateProof } from "./traffic-flow-host-keys-service.js" +import { createFlowSettingsStore } from "./traffic-flow-settings-store.js" +import { createFlowSettingsExecution, flowSettingsPatchSchema, type FlowSettingsPlan, type FlowSettingsReceipt } from "./traffic-flow-settings-execution.js" + +/** Every accepted REST patch restarts the full collector, even a no-op/display + * change. Its flush may prune the complete store and the shared RIPE cache. */ +export const flowSettingsAffectedObjects = [ + { kind: "netflow-settings", id: "1" }, { kind: "netflow-host", id: "1" }, + { kind: "netflow-collector", id: "global" }, { kind: "netflow-store", id: "global" }, { kind: "netflow-ripe-cache", id: "global" }, + ...["traffic_flow_settings", "flow_settings_intents", "flow_buckets", "flow_minute_stats", "flow_minute_dims", "flow_daily_dims", "flow_hour_facts", "flow_daily_facts", "flow_write_receipts", "flow_ip_meta", "flow_asn_meta"].map(id => ({ kind: "database-table", id })), +] +export type FlowSettingsUpdateReceipt = FlowSettingsReceipt & { + ok: true; receiptId: string; afterProof: string; receiptConfirmed: boolean; + privateMaterialExported: false; automaticReplay: false; listenerError?: string; +} +const unknown = () => Object.assign(new Error("Сохранение настроек NetFlow не подтверждено; повтор запрещён"), { code: "MUTATION_OUTCOME_UNKNOWN", statusCode: 503 }) +const production = { + store: (authorize: () => Promise) => createFlowSettingsStore(pool, authorize, invalidateTrafficFlowSettingsCache), + exclusive: (work: () => Promise) => withApplicationMutationLock(() => withFlowConfigurationLock(work)), + guard: (work: () => Promise) => executeGuardedApplicationWrite({ kind: "netflow-settings", id: "1" }, "all", work, async () => { + const rows = (await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'kind'='database-partition' AND object_ref->>'id' ~ '^flow_(buckets|minute_stats|minute_dims|daily_dims|hour_facts|daily_facts)/' LIMIT 1001")).rows + if (rows.length > 1000) throw Error("NetFlow partition protection bound exceeded") + return [...flowSettingsAffectedObjects, ...rows.map(row => row.object_ref as {kind:string;id:string})] + }), + agentPinned: () => Boolean(agentSocket()), startListener: startTrafficFlowListener, proof: flowSettingsPrivateProof, +} +export function createTrafficFlowSettingsService(overrides: Partial = {}) { + const deps = { ...production, ...overrides } + return async (input: TrafficFlowSettingsPatch, control: { authorize?: () => Promise; expected?: FlowSettingsPlan; receiptId?: string } = {}): Promise => { + const patch = flowSettingsPatchSchema.parse(input) + const external = control.authorize ?? (async () => {}) + await external() + return deps.exclusive(() => deps.guard(async () => { + let before = control.expected?.before + const authorize = async () => { + await external() + if (before && deps.agentPinned() && (["collectorIp", "prefix", "flowListenPort", "wgListenPort"] as const).some(key => patch[key] !== undefined && patch[key] !== before![key])) { + throw Object.assign(new Error("Адрес, префикс и порты закреплены установкой агента; повторите установщик хоста"), { statusCode: 409 }) + } + } + const store = deps.store(authorize), receiptId = control.receiptId ?? randomUUID() + let proof = "" + const engine = createFlowSettingsExecution({ + authorize, exclusive: async work => work(), readConfiguration: () => store.read(), + intent: phase => store.claim(receiptId, phase, proof), + compareAndSet: (expected, desired) => store.compareAndSet(receiptId, proof, expected, desired), + generateKeys: () => { throw Error("Settings patch never creates keys") }, startListener: deps.startListener, + }) + const plan = control.expected ?? await engine.prepare({ kind: "patch", patch }) + if (plan.command.kind !== "patch" || JSON.stringify(flowSettingsPatchSchema.parse(plan.command.patch)) !== JSON.stringify(patch)) throw unknown() + before = plan.before + await authorize() + proof = await deps.proof(plan) + const result = await engine.execute(plan) + if (!result.configurationConfirmed) throw unknown() + const desired = { ...plan.before, ...patch } + const receipt: FlowSettingsUpdateReceipt = { ...result, ok: true, receiptId, afterProof: await deps.proof(desired), receiptConfirmed: false, privateMaterialExported: false, automaticReplay: false } + if (result.status !== "applied" || result.listener !== "request-acknowledged") receipt.listenerError = "Настройки сохранены; выполнение полного перезапуска NetFlow не подтверждено. Повтор запрещён" + try { + const completed = { ...receipt, receiptConfirmed: true } + await store.complete(receiptId, "configuration", proof, completed) + if (result.listener === "request-acknowledged") await store.complete(receiptId, "listener", proof, completed) + await authorize() + receipt.receiptConfirmed = true + } catch { + receipt.status = "partial" + receipt.listenerError = "Настройки сохранены; квитанция выполнения NetFlow не подтверждена. Повтор запрещён" + } + return receipt + })) + } +} +export const updateTrafficFlowSettingsWithListener = createTrafficFlowSettingsService() diff --git a/backend/src/services/traffic-flow-settings-store.test.ts b/backend/src/services/traffic-flow-settings-store.test.ts new file mode 100644 index 00000000..78a75b68 --- /dev/null +++ b/backend/src/services/traffic-flow-settings-store.test.ts @@ -0,0 +1,18 @@ +import assert from "node:assert/strict" +import { flowSettingsConfiguration } from "./traffic-flow-settings-store.js" + +const raw = JSON.parse('{"id":1,"host_public_key":"public","host_private_key":"PRIVATE","peers_json":[{"private-key":"opaque","updated_at":"nested-config"}],"updated_at":"telemetry","packets_received":1,"last_error":"telemetry","last_datagram_at":null,"last_exporter_ip":null,"future_config":{"packets_received":99},"constructor":"future","__proto__":{"retained":true},"toString":"future"}') +const value = flowSettingsConfiguration(raw) +assert.equal(value.hostPublicKey, "public"); assert.equal(value.hostPrivateKey, "PRIVATE") +for (const key of ["packets_received", "updated_at", "last_error", "last_datagram_at", "last_exporter_ip"]) assert.equal(Object.hasOwn(value, key), false) +assert.deepEqual(value.peersJson, [{ "private-key": "opaque", updated_at: "nested-config" }]) +assert.deepEqual(value.future_config, { packets_received: 99 }) +assert.equal(Object.hasOwn(value, "constructor"), true); assert.equal(value.constructor, "future") +assert.equal(Object.hasOwn(value, "__proto__"), true); assert.deepEqual(value["__proto__"], { retained: true }) +assert.equal(Object.getPrototypeOf(value), Object.prototype) +assert.equal(value.toString, "future") +assert.equal(raw.host_public_key, "public"); assert.equal(raw.updated_at, "telemetry") +assert.throws(() => flowSettingsConfiguration({ ...raw, hostPublicKey: "ambiguous future column" })) +assert.throws(() => flowSettingsConfiguration({ ...raw, id: 2 })) +assert.throws(() => flowSettingsConfiguration({ ...raw, host_private_key: null })) +console.log("Flow settings raw projection: only five exact telemetry columns excluded; unknown/nested/prototype-named configuration preserved; alias ambiguity refused; no SQL/network") diff --git a/backend/src/services/traffic-flow-settings-store.ts b/backend/src/services/traffic-flow-settings-store.ts new file mode 100644 index 00000000..4ffaaaf8 --- /dev/null +++ b/backend/src/services/traffic-flow-settings-store.ts @@ -0,0 +1,86 @@ +import { isDeepStrictEqual } from "node:util" +import type { Pool, PoolClient } from "pg" +import type { FlowSettingsConfiguration, FlowSettingsPhase } from "./traffic-flow-settings-execution.js" + +const aliases = { + collector_ip: "collectorIp", flow_listen_port: "flowListenPort", wg_listen_port: "wgListenPort", + public_endpoint: "publicEndpoint", host_public_key: "hostPublicKey", host_private_key: "hostPrivateKey", + hub_server_id: "hubServerId", retention_hours: "retentionHours", top_n: "topN", + map_service_min_share_pct: "mapServiceMinSharePct", peers_json: "peersJson", created_at: "createdAt", +} as const +const telemetry = ["last_datagram_at", "last_exporter_ip", "last_error", "packets_received", "updated_at"] +const mutable = new Map(Object.entries({ enabled: "enabled", prefix: "prefix", ...aliases }).filter(([key]) => key !== "created_at").map(([column, field]) => [field, column])) +export function flowSettingsConfiguration(raw: Record): FlowSettingsConfiguration { + const value: Record = {} + for (const [key, item] of Object.entries(raw)) { + if (!telemetry.includes(key)) { + const target = Object.hasOwn(aliases, key) ? aliases[key as keyof typeof aliases] : key + if (Object.hasOwn(value, target)) throw new Error("Ambiguous NetFlow configuration columns") + Object.defineProperty(value, target, { value: structuredClone(item), writable: true, enumerable: true, configurable: true }) + } + } + if (value.id !== 1 || typeof value.hostPublicKey !== "string" || typeof value.hostPrivateKey !== "string") throw new Error("NetFlow configuration is missing or invalid") + return value as FlowSettingsConfiguration +} +function rawConfiguration(value: FlowSettingsConfiguration) { + const reverse = new Map(Object.entries(aliases).map(([column, field]) => [field as string, column])) + return Object.fromEntries(Object.entries(value).map(([field, item]) => [reverse.get(field) ?? field, item])) +} +const projection = `to_jsonb(s) - ARRAY['last_datagram_at','last_exporter_ip','last_error','packets_received','updated_at']::text[]` +const conflict = () => Object.assign(new Error("Настройки NetFlow или квитанция изменились; повтор запрещён"), { statusCode: 409 }) +type Database = Pick +/** This store never creates settings on a read, never retries a write and keeps + * unknown future columns in the complete CAS comparison. A receipt claim is a + * permanent tombstone even if COMMIT or listener acknowledgement is lost. */ +export function createFlowSettingsStore(database: Database, authorize: () => Promise, invalidate: () => void) { + const read = async (client: Pick = database): Promise => { + await authorize() + const rows = (await client.query(`SELECT CASE WHEN octet_length((${projection})::text)<=262144 THEN ${projection} ELSE NULL END AS configuration FROM traffic_flow_settings s WHERE id=1`)).rows + await authorize() + if (rows.length !== 1 || !rows[0].configuration) throw conflict() + return flowSettingsConfiguration(rows[0].configuration) + } + const claim = async (receiptId: string, phase: FlowSettingsPhase, proof: string) => { + await authorize() + const result = await database.query("INSERT INTO flow_settings_intents(receipt_id,phase,request_proof) VALUES($1,$2,$3) ON CONFLICT DO NOTHING RETURNING phase", [receiptId, phase, proof]) + if (result.rows.length !== 1) throw conflict() + await authorize() + } + const compareAndSet = async (receiptId: string, proof: string, expected: FlowSettingsConfiguration, desired: FlowSettingsConfiguration) => { + const changes = Object.keys(desired).filter(key => !isDeepStrictEqual(expected[key], desired[key])) + if (Object.keys(expected).some(key => !(key in desired)) || changes.some(key => !mutable.has(key))) throw conflict() + // jsonb_populate_record decodes each known column using its actual SQL type. + const desiredRaw = rawConfiguration(desired) + const values = [JSON.stringify(desiredRaw), JSON.stringify(rawConfiguration(expected))] + const assignments = changes.map(key => `${mutable.get(key)}=d.${mutable.get(key)}`) + assignments.push("updated_at=now()") + const client = await database.connect() + let begun = false, committing = false, unsafe = false + try { + await authorize(); await client.query("BEGIN"); begun = true + const intent = (await client.query("SELECT state FROM flow_settings_intents WHERE receipt_id=$1 AND phase='configuration' AND request_proof=$2 FOR UPDATE", [receiptId, proof])).rows + if (intent.length !== 1 || intent[0].state !== "claimed") throw conflict() + await authorize() + const result = await client.query(`UPDATE traffic_flow_settings s SET ${assignments.join(",")} FROM jsonb_populate_record(NULL::traffic_flow_settings,$1::jsonb) d WHERE s.id=1 AND ${projection}=$2::jsonb RETURNING s.id`, values) + if (result.rows.length !== 1) { await client.query("ROLLBACK"); begun = false; return false } + if (!isDeepStrictEqual(await read(client), desired)) throw conflict() + await authorize() + await client.query("UPDATE flow_settings_intents SET state='completed' WHERE receipt_id=$1 AND phase='configuration' AND request_proof=$2 AND state='claimed'", [receiptId, proof]) + await authorize(); committing = true; await client.query("COMMIT"); begun = false + invalidate(); await authorize(); return true + } catch (error) { + // A rejected COMMIT acknowledgement is ambiguous. Destroy the connection; + // the permanent claim blocks a second execution in either outcome. + if (committing) unsafe = true + else if (begun) try { await client.query("ROLLBACK") } catch { unsafe = true } + invalidate(); throw error + } finally { client.release(unsafe) } + } + const complete = async (receiptId: string, phase: FlowSettingsPhase, proof: string, result: unknown) => { + await authorize() + const saved = await database.query("UPDATE flow_settings_intents SET state='completed',result=$4::jsonb WHERE receipt_id=$1 AND phase=$2 AND request_proof=$3 AND (state='claimed' OR (state='completed' AND result IS NULL)) RETURNING phase", [receiptId, phase, proof, JSON.stringify(result)]) + if (saved.rows.length !== 1) throw conflict() + await authorize() + } + return { read, claim, compareAndSet, complete } +} diff --git a/backend/src/services/traffic-flow-settings.ts b/backend/src/services/traffic-flow-settings.ts index f2817504..beb11fa5 100644 --- a/backend/src/services/traffic-flow-settings.ts +++ b/backend/src/services/traffic-flow-settings.ts @@ -1,10 +1,11 @@ -import { and, eq, or } from "drizzle-orm" -import { db } from "../db/index.js" +import { getFlowReadResourceProfile } from "./traffic-flow-read-resource-profile.js" +import { trackFlowReadSource, trackFlowReadComputation } from "./traffic-flow-read-budget.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { eq, and, sql } from "drizzle-orm" +import { db, pool } from "../db/index.js" import { parseJsonArray } from "../db/json.js" import { trafficFlowSettings } from "../db/schema.js" import type { FlowHostPeer, TrafficFlowSettingsDto, TrafficFlowSettingsPatch } from "@mmapp/contracts/traffic-flow" -import { ensureTrafficFlowHostKeys } from "./traffic-flow-key-execution.js" -import { generateWireGuardKeyPair } from "./wg-keys.js" type SettingsRow = NonNullable>> @@ -19,10 +20,29 @@ export function invalidateTrafficFlowSettingsCache(): void { } async function readSettingsRow() { - return (await db.select().from(trafficFlowSettings).where(eq(trafficFlowSettings.id, 1)).limit(1))[0] + return trackFlowReadSource(async admit=>{ + // Scalar metadata only. No settings JSON/credentials enter Node before the + // measured source fits both the query profile and this response's budget. + const metadata=(await pool.query<{bytes:string}>("SELECT octet_length(row_to_json(traffic_flow_settings)::text)::text AS bytes FROM traffic_flow_settings WHERE id=1 LIMIT 2")).rows + if(metadata.length>1)throw Object.assign(Error("Источник настроек NetFlow содержит несколько строк"),{statusCode:409}) + const raw=metadata[0] + if(!raw)return undefined + const bytes=Number(raw.bytes),profile=getFlowReadResourceProfile() + // Standard keeps its existing 4MiB RAW-row cap; constrained query limits + // include the two array-framing bytes charged below, like bounded SQL. + const oversized=profile.name==="standard"?bytes>4194304:bytes+2>Math.min(4194306,profile.querySourceBytes) + if(!/^[0-9]{1,10}$/.test(raw.bytes)||!Number.isSafeInteger(bytes)||bytes<1||oversized)throw Object.assign(Error("Настройки NetFlow превышают допустимый объём"),{statusCode:409}) + admit(1,bytes+2) + // Preserve Drizzle's normal per-column mapping. The data query enforces + // the measured size on its own snapshot: growth cannot bypass preflight. + const row=(await db.select().from(trafficFlowSettings).where(and(eq(trafficFlowSettings.id,1),sql`octet_length(row_to_json(traffic_flow_settings)::text)<=${bytes}`,sql`(SELECT count(*) FROM (SELECT 1 FROM traffic_flow_settings WHERE id=1 LIMIT 2) flow_settings_cardinality)=1`)).limit(1))[0] + if(!row&&(await db.select({id:trafficFlowSettings.id}).from(trafficFlowSettings).where(eq(trafficFlowSettings.id,1)).limit(1)).length)throw Object.assign(Error("Настройки NetFlow изменились во время чтения; повторите запрос"),{statusCode:409}) + return row + }) } export async function getTrafficFlowSettingsRow(): Promise { + return trackFlowReadComputation(async()=>{ if (settingsRowCache) return settingsRowCache let row = await readSettingsRow() if (row) { @@ -43,6 +63,7 @@ export async function getTrafficFlowSettingsRow(): Promise { row = await readSettingsRow() settingsRowCache = row! return settingsRowCache + }) } function parsePeers(raw: unknown): FlowHostPeer[] { @@ -79,7 +100,8 @@ export async function toTrafficFlowSettingsDto( } } -export async function updateTrafficFlowSettings(patch: TrafficFlowSettingsPatch) { +export function updateTrafficFlowSettings(patch: TrafficFlowSettingsPatch) { return executeGuardedApplicationWrite({ kind: "netflow-settings", id: "1" }, "all", () => updateTrafficFlowSettingsImpl(patch)) } +async function updateTrafficFlowSettingsImpl(patch: TrafficFlowSettingsPatch) { await getTrafficFlowSettingsRow() // Do not copy untouched fields from a potentially stale cache into a concurrent update. await db.update(trafficFlowSettings).set({ ...patch, updatedAt: nowIso() }).where(eq(trafficFlowSettings.id, 1)) @@ -88,23 +110,8 @@ export async function updateTrafficFlowSettings(patch: TrafficFlowSettingsPatch) } export async function ensureHostKeys(): Promise<{ publicKey: string; created: boolean }> { - await getTrafficFlowSettingsRow() - return ensureTrafficFlowHostKeys({ - read: async () => { - invalidateTrafficFlowSettingsCache() - const row = await getTrafficFlowSettingsRow() - return { publicKey: row.hostPublicKey, privateKey: row.hostPrivateKey } - }, - generate: generateWireGuardKeyPair, - installIfMissing: async keys => { - const rows = await db.update(trafficFlowSettings).set({ - hostPublicKey: keys.publicKey, hostPrivateKey: keys.privateKey, updatedAt: nowIso(), - }).where(and(eq(trafficFlowSettings.id, 1), or(eq(trafficFlowSettings.hostPublicKey, ""), eq(trafficFlowSettings.hostPrivateKey, "")))) - .returning({ id: trafficFlowSettings.id }) - invalidateTrafficFlowSettingsCache() - return rows.length === 1 - }, - }) + const { publicKey, created } = await (await import("./traffic-flow-host-keys-service.js")).generateFlowHostKeys() + return { publicKey, created } } export async function upsertHostPeer(peer: FlowHostPeer) { diff --git a/backend/src/services/traffic-flow-source-limits.ts b/backend/src/services/traffic-flow-source-limits.ts new file mode 100644 index 00000000..37d6816f --- /dev/null +++ b/backend/src/services/traffic-flow-source-limits.ts @@ -0,0 +1,14 @@ +import { pool } from "../db/index.js" + +/** Scoped readers need retention limits, never collector credentials or implicit initialization. */ +export async function readFlowSourceLimits() { + const row = (await pool.query<{ topN: number }>('SELECT top_n AS "topN" FROM traffic_flow_settings WHERE id=1')).rows[0] + if (!row || !Number.isSafeInteger(row.topN) || row.topN < 1 || row.topN > 1000) throw new Error("Не подтверждён предел источника NetFlow") + return row +} + +export async function readFlowMapSourceLimits() { + const row = (await pool.query<{ topN: number; mapServiceMinSharePct: number }>('SELECT top_n AS "topN",map_service_min_share_pct AS "mapServiceMinSharePct" FROM traffic_flow_settings WHERE id=1')).rows[0] + if (!row || !Number.isSafeInteger(row.topN) || row.topN < 1 || row.topN > 1000 || !Number.isFinite(row.mapServiceMinSharePct) || row.mapServiceMinSharePct < 0 || row.mapServiceMinSharePct > 100) throw new Error("Не подтверждены пределы карты NetFlow") + return row +} diff --git a/backend/src/services/traffic-flow-talkers-model.test.ts b/backend/src/services/traffic-flow-talkers-model.test.ts new file mode 100644 index 00000000..05292b41 --- /dev/null +++ b/backend/src/services/traffic-flow-talkers-model.test.ts @@ -0,0 +1,24 @@ +import assert from "node:assert/strict" +import { projectFlowTalkers } from "./traffic-flow-talkers-model.js" +import type { PendingFlowRow } from "./traffic-flow-engine.js" + +const row: PendingFlowRow = { serverId: 1, bucketAt: "2026-10-21T00:00:00Z", src: "192.0.2.1", dst: "198.51.100.1", proto: 6, + srcPort: 1234, dstPort: 443, bytes: 300, packets: 3, inIface: "2", outIface: "", nextHop: "", flowStartMs: 0, flowEndMs: 0, + natSrc: "", natDst: "", natSrcPort: 0, natDstPort: 0 } +const names = new Map([[1, "Router"]]), resolve = (_id: number, value: string) => ({ name: value === "2" ? "ether2" : value, index: value }) +const read = (rows: PendingFlowRow[], topN = 20) => projectFlowTalkers(rows, 5, topN, names, resolve) +const result = read([row, { ...row, bucketAt: "2026-10-21T00:01:00Z", bytes: 600, packets: 5 }, { ...row, inIface: "3", bytes: 100 }]) +assert.equal(result.totalConversations, 2) +assert.equal(result.retainedRowsBytes, 1000) +assert.equal(result.talkers[0].bytes, 900); assert.equal(result.talkers[0].packets, 8) +assert.equal(result.talkers[0].bps, 24); assert.equal(result.talkers[0].inIface, "ether2") +assert.equal(result.talkers[0].serverName, "Router") +assert.equal(read([row, { ...row, src: "192.0.2.2" }], 1).talkers.length, 1) +assert.equal(read([row, { ...row, src: "192.0.2.2" }], 1).totalConversations, 2) +assert.deepEqual(read([row, { ...row, src: "192.0.2.2" }]).talkers, read([{ ...row, src: "192.0.2.2" }, row]).talkers) +assert.equal(read([]).retainedRowsBytes, 0); assert.equal(read([]).topProto, "—") +for (const bytes of [-1, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1]) assert.throws(() => read([{ ...row, bytes }]), /счётчик/) +assert.throws(() => read([{ ...row, bytes: Number.MAX_SAFE_INTEGER }, row]), /счётчик/) +assert.throws(() => read([{ ...row, packets: Number.MAX_SAFE_INTEGER }, row]), /счётчик/) +assert.throws(() => read([row], 0), /предел/) +console.log("✓ NetFlow talkers: shared projection, conversation/interface identity, exact sums, stable pages and overflow rejection") diff --git a/backend/src/services/traffic-flow-talkers-model.ts b/backend/src/services/traffic-flow-talkers-model.ts new file mode 100644 index 00000000..879a2b46 --- /dev/null +++ b/backend/src/services/traffic-flow-talkers-model.ts @@ -0,0 +1,36 @@ +import type { FlowTalkerDto } from "@mmapp/contracts/traffic-flow" +import type { PendingFlowRow } from "./traffic-flow-engine.js" +import { protoName } from "./traffic-flow-parse.js" +import { applicationName } from "./traffic-flow-apps.js" +import { flowCounter, flowCounterSum } from "./traffic-flow-observed-values.js" + +/** Shared REST/MCP projection. The caller must scope and bound rows before this + * function; retained exporter rows are not a deduplicated network total. */ +export function projectFlowTalkers(rows: readonly PendingFlowRow[], minutes: number, topN: number, + names: ReadonlyMap, resolve: (serverId: number, value: string) => { name: string; index: string }) { + if (!Number.isSafeInteger(minutes) || minutes < 1 || minutes > 1440 || !Number.isSafeInteger(topN) || topN < 1 || topN > 1000) throw new Error("Некорректное окно или предел NetFlow") + const agg = new Map(), protocols = new Map() + const sources = new Set(), destinations = new Set(), exporters = new Set() + let totalBytes = 0 + for (const row of rows) { + const bytes = flowCounter(row.bytes), packets = flowCounter(row.packets) + totalBytes = flowCounterSum([totalBytes, bytes]) + sources.add(row.src); destinations.add(row.dst); exporters.add(row.serverId) + protocols.set(row.proto, flowCounterSum([protocols.get(row.proto) ?? 0, bytes])) + const key = JSON.stringify([row.serverId, row.src, row.dst, row.proto, row.srcPort, row.dstPort, row.inIface]) + const previous = agg.get(key) + if (previous) { + previous.bytes = flowCounterSum([previous.bytes, bytes]); previous.packets = flowCounterSum([previous.packets, packets]) + } else { + const iface = resolve(row.serverId, row.inIface) + agg.set(key, { serverId: String(row.serverId), serverName: names.get(row.serverId) ?? String(row.serverId), + src: row.src, dst: row.dst, proto: row.proto, protoName: protoName(row.proto), srcPort: row.srcPort, dstPort: row.dstPort, + bytes, packets, bps: 0, inIface: iface.name, inIfaceIndex: iface.index, application: applicationName(row.proto, row.dstPort, row.srcPort) }) + } + } + const ordered = [...agg.entries()].sort((a, b) => b[1].bytes - a[1].bytes || a[0].localeCompare(b[0])) + const talkers = ordered.slice(0, topN).map(([, row]) => ({ ...row, bps: row.bytes * 8 / (minutes * 60) })) + const topProtocol = [...protocols].sort((a, b) => b[1] - a[1] || a[0] - b[0])[0] + return { talkers, totalConversations: agg.size, retainedRowsBytes: totalBytes, observedExporters: exporters.size, + bytesPerMin: totalBytes / minutes, uniqueSrc: sources.size, uniqueDst: destinations.size, topProto: topProtocol ? protoName(topProtocol[0]) : "—" } +} diff --git a/backend/src/services/traffic-flow-template-cache.test.ts b/backend/src/services/traffic-flow-template-cache.test.ts new file mode 100644 index 00000000..8ee9ecae --- /dev/null +++ b/backend/src/services/traffic-flow-template-cache.test.ts @@ -0,0 +1,208 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { test } from "node:test" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import { FLOW_TEMPLATE_LIMITS as limits, FlowTemplateRefused, parseFlowPacket, resetFlowTemplatesForTests, templateCacheUsageForTests } from "./traffic-flow-parse.js" + +type Field = { type: number; length: number; enterprise?: number } +function packet(version: 9 | 10, set: number, body: Buffer, exportTime = 0) { + const header = version === 10 ? 16 : 20, value = Buffer.alloc(header + 4 + body.length) + value.writeUInt16BE(version, 0); value.writeUInt16BE(version === 10 ? value.length : 1, 2) + if (version === 10) value.writeUInt32BE(exportTime, 4) + value.writeUInt16BE(set, header); value.writeUInt16BE(4 + body.length, header + 2); body.copy(value, header + 4) + return value +} +function template(version: 9 | 10, id: number, fields: Field[], options = false, exportTime = 0) { + const body = Buffer.alloc(4 + (options ? 2 : 0) + fields.reduce((n, field) => n + 4 + (field.enterprise === undefined ? 0 : 4), 0)) + body.writeUInt16BE(id); body.writeUInt16BE(fields.length, 2) + let offset = 4 + if (options) { body.writeUInt16BE(1, offset); offset += 2 } + for (const field of fields) { + body.writeUInt16BE(field.type | (field.enterprise === undefined ? 0 : 0x8000), offset); body.writeUInt16BE(field.length, offset + 2); offset += 4 + if (field.enterprise !== undefined) { body.writeUInt32BE(field.enterprise, offset); offset += 4 } + } + return packet(version, version === 9 ? 0 : options ? 3 : 2, body, exportTime) +} +const v4: Field[] = [{ type: 8, length: 4 }, { type: 12, length: 4 }] +const ipv4Data = (version: 9 | 10, id = 256, exportTime = 0) => packet(version, id, Buffer.from([192, 0, 2, 1, 198, 51, 100, 1]), exportTime) +const fields = (count: number): Field[] => Array.from({ length: count }, () => ({ type: 1, length: 1 })) +const usage = (exporter?: string) => { const { refusedTemplates: _refused, ...value } = templateCacheUsageForTests(exporter); return value } +const refused = (work: () => unknown, reason: FlowTemplateRefused["reason"]) => assert.throws(work, error => error instanceof FlowTemplateRefused && error.reason === reason) + +test("normal v9/IPFIX IPv4, IPv6, enterprise fields and refresh remain usable", () => { + resetFlowTemplatesForTests() + for (const version of [9, 10] as const) { + const exporter = "192.0.2." + version + parseFlowPacket(template(version, 256, v4), exporter) + assert.equal(parseFlowPacket(ipv4Data(version), exporter)[0].src, "192.0.2.1") + for (let index = 0; index < 20; index++) parseFlowPacket(template(version, 256, v4), exporter) + assert.equal(templateCacheUsageForTests(exporter).exporterTemplates, 1) + parseFlowPacket(template(version, 256, [{ type: 27, length: 16 }, { type: 28, length: 16 }], false, 1), exporter) + const bytes = Buffer.alloc(32); bytes.writeUInt16BE(0x2001); bytes.writeUInt16BE(0x0db8, 2); bytes[15] = 1 + bytes.writeUInt16BE(0x2001, 16); bytes.writeUInt16BE(0x0db8, 18); bytes[31] = 2 + assert.equal(parseFlowPacket(packet(version, 256, bytes, 1), exporter)[0].src, "2001:db8:0:0:0:0:0:1") + } + parseFlowPacket(template(10, 300, [{ ...v4[0], enterprise: 14988 }, v4[1]]), "enterprise") + assert.equal(parseFlowPacket(ipv4Data(10, 300), "enterprise")[0].dst, "198.51.100.1") +}) + +test("UDP IPFIX withdrawals are ignored including options without a scope count; v9 zero-field behavior stays unchanged", () => { + resetFlowTemplatesForTests() + parseFlowPacket(template(10, 256, v4), "udp"); parseFlowPacket(template(10, 257, v4, true), "udp") + const before = usage("udp") + for (const [set, id] of [[2, 256], [3, 257], [2, 2], [3, 3]]) { + const body = Buffer.alloc(4); body.writeUInt16BE(id) + parseFlowPacket(packet(10, set, body), "udp") + } + assert.deepEqual(usage("udp"), before) + const emptyWithdrawal = Buffer.alloc(4); emptyWithdrawal.writeUInt16BE(256) + parseFlowPacket(packet(10, 2, emptyWithdrawal), "withdrawal-only-new-source") + assert.deepEqual(usage("udp"), before, "Ignoring a withdrawal must not create an exporter or evict another source") + assert.equal(parseFlowPacket(ipv4Data(10), "udp").length, 1); assert.equal(parseFlowPacket(ipv4Data(10, 257), "udp").length, 1) + parseFlowPacket(template(9, 256, v4), "v9"); parseFlowPacket(template(9, 256, []), "v9") + assert.equal(parseFlowPacket(ipv4Data(9), "v9").length, 0) + assert.equal(templateCacheUsageForTests("v9").exporterTemplates, 1); assert.equal(templateCacheUsageForTests("v9").exporterFields, 0) +}) + +test("field-count refusal precedes allocation/body parsing and replacement remains intact", () => { + resetFlowTemplatesForTests() + for (const version of [9, 10] as const) { + const exporter = "replace-" + version + parseFlowPacket(template(version, 256, v4), exporter) + const before = usage(exporter), header = Buffer.alloc(4); header.writeUInt16BE(256); header.writeUInt16BE(65535, 2) + refused(() => parseFlowPacket(packet(version, version === 9 ? 0 : 2, header), exporter), "fields") + assert.deepEqual(usage(exporter), before) + assert.equal(parseFlowPacket(ipv4Data(version), exporter)[0].src, "192.0.2.1") + const short = Buffer.alloc(8); short.writeUInt16BE(256); short.writeUInt16BE(2, 2); short.writeUInt16BE(8, 4); short.writeUInt16BE(4, 6) + refused(() => parseFlowPacket(packet(version, version === 9 ? 0 : 2, short), exporter), "truncated") + assert.deepEqual(usage(exporter), before) + } + const before = usage("replace-10"), missingEnterprise = Buffer.alloc(8) + missingEnterprise.writeUInt16BE(256); missingEnterprise.writeUInt16BE(1, 2); missingEnterprise.writeUInt16BE(0x8008, 4); missingEnterprise.writeUInt16BE(4, 6) + refused(() => parseFlowPacket(packet(10, 2, missingEnterprise), "replace-10"), "truncated") + assert.deepEqual(usage("replace-10"), before); assert.equal(parseFlowPacket(ipv4Data(10), "replace-10").length, 1) +}) + +test("per-exporter template and field caps use replacement deltas without truncation", () => { + resetFlowTemplatesForTests() + for (let id = 256; id < 256 + limits.perExporterTemplates; id++) parseFlowPacket(template(10, id, fields(1)), "ids") + const before = usage("ids") + refused(() => parseFlowPacket(template(10, 1024, fields(1)), "ids"), "exporter-templates") + assert.deepEqual(usage("ids"), before) + parseFlowPacket(template(10, 256, v4, false, 1), "ids") + assert.equal(templateCacheUsageForTests("ids").exporterFields, limits.perExporterTemplates + 1) + assert.equal(parseFlowPacket(ipv4Data(10, 256, 1), "ids").length, 1) + resetFlowTemplatesForTests() + const full = fields(limits.perTemplateFields) + for (let index = 0; index < limits.perExporterFields / limits.perTemplateFields; index++) parseFlowPacket(template(9, 256 + index, full), "fields") + const fieldBefore = usage("fields") + refused(() => parseFlowPacket(template(9, 512, fields(1)), "fields"), "exporter-fields") + assert.deepEqual(usage("fields"), fieldBefore) + parseFlowPacket(template(9, 256, fields(1)), "fields") + assert.equal(templateCacheUsageForTests("fields").exporterFields, limits.perExporterFields - limits.perTemplateFields + 1) +}) + +test("global template and field budgets refuse independently of exporter budgets", () => { + resetFlowTemplatesForTests() + for (let exporter = 0; exporter < limits.templates / limits.perExporterTemplates; exporter++) for (let id = 0; id < limits.perExporterTemplates; id++) parseFlowPacket(template(10, 256 + id, fields(1)), "global-id-" + exporter) + refused(() => parseFlowPacket(template(10, 256, fields(1)), "global-id-extra"), "templates") + assert.equal(templateCacheUsageForTests().templates, limits.templates) + resetFlowTemplatesForTests() + const full = fields(limits.perTemplateFields) + for (let exporter = 0; exporter < limits.fields / limits.perExporterFields; exporter++) for (let id = 0; id < limits.perExporterFields / limits.perTemplateFields; id++) parseFlowPacket(template(10, 256 + id, full), "global-field-" + exporter) + refused(() => parseFlowPacket(template(10, 256, fields(1)), "global-field-extra"), "total-fields") + assert.equal(templateCacheUsageForTests().fields, limits.fields) + parseFlowPacket(template(10, 256, fields(128), false, 1), "global-field-0") + parseFlowPacket(template(10, 256, fields(128)), "global-field-extra") + assert.equal(templateCacheUsageForTests().fields, limits.fields) +}) + +test("existing 256-exporter LRU eviction releases exact template/field accounting", () => { + resetFlowTemplatesForTests() + for (let exporter = 0; exporter < limits.exporters; exporter++) parseFlowPacket(template(10, 256, v4), "evict-" + exporter) + parseFlowPacket(template(10, 256, v4), "evict-0") + parseFlowPacket(template(10, 256, v4), "evict-new") + assert.equal(templateCacheUsageForTests("evict-1").exporterTemplates, 0) + assert.equal(templateCacheUsageForTests("evict-0").exporterTemplates, 1) + assert.equal(templateCacheUsageForTests().exporters, limits.exporters) + assert.equal(templateCacheUsageForTests().templates, limits.exporters); assert.equal(templateCacheUsageForTests().fields, limits.exporters * 2) +}) + +test("actual engine ingress rejects unknown template-only sources before parser state and exposes bounded refusal", () => { + resetFlowTemplatesForTests() + const source = readFileSync(new URL("./traffic-flow-engine.ts", import.meta.url), "utf8") + const parsed = ts.createSourceFile("engine.ts", source, ts.ScriptTarget.ES2022, true) + const ingress = parsed.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "ingestDatagram") + assert(ingress) + const code = ts.transpileModule(ingress.getText(parsed), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText + const exports: { ingestDatagram?: (value: Buffer, exporter: string) => boolean } = {}, queued: unknown[][] = [] + let error = "", packets = 0 + runInNewContext(code, { exports, Buffer, FlowTemplateRefused, parseFlowPacket, + bumpPacketMeta: () => { packets++ }, resolveServerId: (exporter: string) => exporter === "known" ? 1 : null, + setEngineError: (value: string) => { error = value }, maybeRefreshIfaces: () => {}, queueParsedFlows: (_id: number, flows: unknown[]) => queued.push(flows) }) + const ingest = exports.ingestDatagram! + assert.equal(ingest(template(10, 256, v4), "unknown"), false) + assert.equal(templateCacheUsageForTests().exporters, 0); assert(error.includes("нет jump-host")) + assert.equal(ingest(template(10, 256, v4), "known"), true) + assert.equal(ingest(ipv4Data(10), "known"), true); assert.equal(queued.length, 1); assert.equal(queued[0].length, 1) + assert.equal(ingest(template(10, 256, fields(limits.perTemplateFields + 1)), "known"), false) + assert.equal(error, "NetFlow/IPFIX template refused: fields"); assert.equal(queued.length, 1) + assert.equal(ingest(ipv4Data(10), "known"), true); assert.equal(queued.length, 2); assert.equal(packets, 5) + resetFlowTemplatesForTests() +}) + +test("new v9 header-only and unknown-template data sources cannot evict usable templates", () => { + resetFlowTemplatesForTests() + for (let exporter = 0; exporter < limits.exporters; exporter++) parseFlowPacket(template(9, 256, v4), "valid-" + exporter) + const before = usage() + const header = Buffer.alloc(20); header.writeUInt16BE(9) + assert.deepEqual(parseFlowPacket(header, "empty-new"), []) + refused(() => parseFlowPacket(ipv4Data(9), "data-only-new"), "missing-template") + assert.deepEqual(usage(), before) + assert.equal(templateCacheUsageForTests("empty-new").exporterTemplates, 0) + assert.equal(templateCacheUsageForTests("data-only-new").exporterTemplates, 0) + assert.equal(parseFlowPacket(ipv4Data(9), "valid-0")[0].src, "192.0.2.1") +}) + +test("new truncated IPFIX template leaves the full exporter cache and LRU victim usable", () => { + resetFlowTemplatesForTests() + for (let exporter = 0; exporter < limits.exporters; exporter++) parseFlowPacket(template(10, 256, v4), "valid-" + exporter) + const before = usage(), body = Buffer.alloc(4); body.writeUInt16BE(256); body.writeUInt16BE(1, 2) + refused(() => parseFlowPacket(packet(10, 2, body), "truncated-new"), "truncated") + assert.deepEqual(usage(), before) + assert.equal(templateCacheUsageForTests("truncated-new").exporterTemplates, 0) + assert.equal(parseFlowPacket(ipv4Data(10), "valid-0")[0].src, "192.0.2.1") + parseFlowPacket(template(10, 256, v4), "valid-new") + assert.equal(templateCacheUsageForTests("valid-0").exporterTemplates, 0, "successful replacement still evicts the actual LRU victim") + assert.equal(parseFlowPacket(ipv4Data(10), "valid-new")[0].dst, "198.51.100.1") +}) + +test("new valid exporter can replace its LRU victim at the global template budget", () => { + resetFlowTemplatesForTests() + const perExporter = limits.templates / limits.exporters + assert(Number.isInteger(perExporter)) + for (let exporter = 0; exporter < limits.exporters; exporter++) for (let id = 256; id < 256 + perExporter; id++) parseFlowPacket(template(10, id, v4), "full-" + exporter) + assert.equal(templateCacheUsageForTests().templates, limits.templates) + parseFlowPacket(template(10, 256, v4), "replacement") + assert.equal(templateCacheUsageForTests().templates, limits.templates - perExporter + 1) + assert.equal(templateCacheUsageForTests().fields, (limits.templates - perExporter + 1) * v4.length) + assert.equal(templateCacheUsageForTests("full-0").exporterTemplates, 0) + assert.equal(parseFlowPacket(ipv4Data(10), "full-1").length, 1) + assert.equal(parseFlowPacket(ipv4Data(10), "replacement").length, 1) +}) + +test("new valid exporter can replace its LRU victim at the global field budget", () => { + resetFlowTemplatesForTests() + const perExporter = limits.fields / limits.exporters + assert(perExporter <= limits.perTemplateFields) + for (let exporter = 0; exporter < limits.exporters; exporter++) parseFlowPacket(template(10, 256, fields(perExporter)), "full-" + exporter) + assert.equal(templateCacheUsageForTests().fields, limits.fields) + parseFlowPacket(template(10, 256, v4), "replacement") + assert.equal(templateCacheUsageForTests().fields, limits.fields - perExporter + v4.length) + assert.equal(templateCacheUsageForTests().templates, limits.exporters) + assert.equal(templateCacheUsageForTests("full-0").exporterTemplates, 0) + assert.equal(templateCacheUsageForTests("full-1").exporterFields, perExporter) + assert.equal(parseFlowPacket(ipv4Data(10), "replacement")[0].src, "192.0.2.1") + resetFlowTemplatesForTests() +}) diff --git a/backend/src/services/traffic-flow-template-lifetime.test.ts b/backend/src/services/traffic-flow-template-lifetime.test.ts new file mode 100644 index 00000000..f2f359f6 --- /dev/null +++ b/backend/src/services/traffic-flow-template-lifetime.test.ts @@ -0,0 +1,269 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { test } from "node:test" +import { runInNewContext } from "node:vm" +import ts from "typescript" +import { FLOW_TEMPLATE_LIFETIME_MS as lifetime, FlowTemplateRefused, parseFlowPacket, resetFlowTemplatesForTests, templateCacheUsageForTests } from "./traffic-flow-parse.js" + +type Version = 9 | 10 +const transport = { sourcePort: 2055, collectorSocketId: "owned_clock" } +function packet(version: Version, set: number, body: Buffer, domain = 0) { + const header = version === 9 ? 20 : 16, value = Buffer.alloc(header + 4 + body.length) + value.writeUInt16BE(version); value.writeUInt16BE(version === 9 ? 1 : value.length, 2) + value.writeUInt32BE(domain, version === 9 ? 16 : 12) + value.writeUInt16BE(set, header); value.writeUInt16BE(body.length + 4, header + 2); body.copy(value, header + 4) + return value +} +function template(version: Version, id = 256, domain = 0) { + const body = Buffer.from([1, 0, 0, 2, 0, 8, 0, 4, 0, 12, 0, 4]); body.writeUInt16BE(id) + return packet(version, version === 9 ? 0 : 2, body, domain) +} +const data = (version: Version, id = 256, domain = 0) => packet(version, id, Buffer.from([192, 0, 2, 1, 198, 51, 100, 1]), domain) +const parse = (value: Buffer) => parseFlowPacket(value, "known", transport) +const unavailable = (work: () => unknown) => assert.throws(work, error => error instanceof FlowTemplateRefused && error.reason === "missing-template") + +test("v9 refuses old layout after same-port exporter reboot until a new template arrives", t => { + resetFlowTemplatesForTests() + t.after(resetFlowTemplatesForTests) + const at = (value: Buffer, seconds: number, uptime: number) => { + value.writeUInt32BE(uptime, 4) + value.writeUInt32BE(seconds, 8) + return value + } + const old = at(template(9), 10000, 1200000) + parse(old) + parse(at(template(9, 256, 1), 10000, 1200000)) + assert.equal(parse(at(data(9), 10001, 1201000))[0].src, "192.0.2.1") + unavailable(() => parse(at(data(9), 10002, 500))) + const replacement = packet(9, 0, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4])) + parse(at(replacement, 10003, 1500)) + parse(old) // A delayed template from the previous boot must not replace the new layout. + assert.equal(parse(at(data(9), 10004, 2500))[0].src, "198.51.100.1") + unavailable(() => parse(at(data(9), 10001, 1201000))) + assert.equal(parse(at(data(9, 256, 1), 10001, 1201000))[0].src, "192.0.2.1") + parse(at(template(9, 257), 20000, 0xfffffe0c)) + assert.equal(parse(at(data(9, 257), 20001, 500))[0].src, "192.0.2.1") +}) + +test("v9 rejects incompatible clocks even within one second or after new uptime passes the old template", t => { + resetFlowTemplatesForTests() + t.after(resetFlowTemplatesForTests) + const at = (value: Buffer, seconds: number, uptime: number) => { + value.writeUInt32BE(uptime, 4) + value.writeUInt32BE(seconds, 8) + return value + } + const newLayout = () => packet(9, 0, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4])) + parse(at(template(9), 10000, 120000)) + assert.equal(parse(at(data(9), 10000, 120500))[0].src, "192.0.2.1") + unavailable(() => parse(at(data(9), 10000, 500))) + parse(at(newLayout(), 10000, 500)) + parse(at(template(9), 10000, 120000)) + unavailable(() => parse(at(data(9), 10001, 1500))) + parse(at(newLayout(), 10001, 1500)) + assert.equal(parse(at(data(9), 10002, 2500))[0].src, "198.51.100.1") + + parse(at(template(9, 257), 20000, 10000)) + unavailable(() => parse(at(data(9, 257), 20100, 20000))) + parse(at(template(9, 257), 20100, 20000)) + assert.equal(parse(at(data(9, 257), 20101, 21000))[0].src, "192.0.2.1") + + parse(at(template(9, 258), 30000, 0xffffff00)) + assert.equal(parse(at(data(9, 258), 30000, 200))[0].src, "192.0.2.1") + assert.equal(parse(at(data(9, 258), 29999, 0xfffffb18))[0].src, "192.0.2.1") +}) + +test("v9 retains the clock of a layout change across refresh and refuses older data and templates", t => { + resetFlowTemplatesForTests() + t.after(resetFlowTemplatesForTests) + const at = (value: Buffer, seconds: number, uptime: number, sequence: number) => { + value.writeUInt32BE(uptime, 4) + value.writeUInt32BE(seconds, 8) + value.writeUInt32BE(sequence, 12) + return value + } + const newLayout = () => packet(9, 0, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4])) + parse(at(template(9), 10000, 100000, 0xfffffffd)) + parse(at(template(9, 256, 1), 10000, 100000, 1)) + parse(at(newLayout(), 10000, 100500, 0xffffffff)) + + unavailable(() => parse(at(data(9), 10000, 100250, 0xfffffffe))) + unavailable(() => parse(at(data(9), 10000, 100500, 0xfffffffe))) + parse(at(template(9), 10000, 100500, 0xfffffffe)) + assert.equal(parse(at(data(9), 10000, 100500, 0))[0].src, "198.51.100.1") + parse(at(newLayout(), 10001, 101000, 1)) + assert.equal(parse(at(data(9), 10000, 100500, 0))[0].src, "198.51.100.1") + unavailable(() => parse(at(data(9), 9999, 99500, 0xfffffffc))) + assert.equal(parse(at(data(9, 256, 1), 10001, 101000, 2))[0].src, "192.0.2.1") +}) + +test("IPFIX rejects delayed data from before a layout change and ignores delayed templates", t => { + resetFlowTemplatesForTests() + t.after(resetFlowTemplatesForTests) + const at = (value: Buffer, seconds: number) => { + value.writeUInt32BE(seconds, 4) + return value + } + parse(at(template(10), 100)) + const replacement = packet(10, 2, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4])) + parse(at(replacement, 200)) + parse(at(template(10), 150)) + unavailable(() => parse(at(data(10), 160))) + parse(at(replacement, 210)) + assert.equal(parse(at(data(10), 205))[0].src, "198.51.100.1") +}) + +test("IPFIX sequence regression with newer export time waits for a fresh template", t => { + resetFlowTemplatesForTests() + t.after(resetFlowTemplatesForTests) + const at = (value: Buffer, seconds: number, sequence = 0) => { + value.writeUInt32BE(seconds, 4) + value.writeUInt32BE(sequence, 8) + return value + } + parse(at(template(10), 100)) + parse(at(template(10, 257), 100)) + parse(at(template(10, 256, 1), 100)) + assert.equal(parse(at(data(10), 110, 1000)).length, 1) + assert.equal(parse(at(data(10), 109, 999)).length, 1, "Delayed older data does not reset the clock") + unavailable(() => parse(at(data(10, 257), 111, 0))) + unavailable(() => parse(at(data(10), 112, 1))) + assert.equal(parse(at(data(10, 256, 1), 111, 0)).length, 1, "The sibling domain stays usable") + parse(at(template(10), 110)) + parse(at(template(10), 111)) + unavailable(() => parse(at(data(10), 112, 2))) + parse(at(template(10), 112)) + assert.equal(parse(at(data(10), 112, 2)).length, 1) + unavailable(() => parse(at(data(10, 257), 113, 3))) + unavailable(() => parse(at(data(10), 110, 1000))) + parse(at(template(10, 257), 114)) + assert.equal(parse(at(data(10, 257), 114, 3)).length, 1) + parse(at(template(10, 257, 2), 120)) + assert.equal(parse(at(data(10, 257, 2), 121, 0xffffffff)).length, 1) + assert.equal(parse(at(data(10, 257, 2), 122, 0)).length, 1) +}) + +test("IPFIX accepts a fresh definition followed by data in the regression datagram", t => { + resetFlowTemplatesForTests() + t.after(resetFlowTemplatesForTests) + const at = (value: Buffer, seconds: number, sequence = 0) => { + value.writeUInt32BE(seconds, 4) + value.writeUInt32BE(sequence, 8) + return value + } + parse(at(template(10), 100)) + parse(at(template(10, 257), 100)) + parse(at(data(10), 110, 1000)) + const replacement = packet(10, 2, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4])) + const mixed = Buffer.concat([replacement, data(10).subarray(16)]) + mixed.writeUInt16BE(mixed.length, 2) + assert.equal(parse(at(mixed, 111))[0].src, "198.51.100.1") + unavailable(() => parse(at(data(10), 111, 1))) + assert.equal(parse(at(data(10), 112, 1))[0].src, "198.51.100.1") + unavailable(() => parse(at(data(10, 257), 112, 2))) + + const reversed = Buffer.concat([data(10), replacement.subarray(16)]) + reversed.writeUInt16BE(reversed.length, 2) + parse(at(data(10), 113, 2000)) + unavailable(() => parse(at(reversed, 114))) + unavailable(() => parse(at(data(10), 115, 1))) +}) + +test("IPFIX with conflicting definitions in one second needs a strictly newer template", t => { + resetFlowTemplatesForTests() + t.after(resetFlowTemplatesForTests) + const at = (value: Buffer, seconds: number) => { + value.writeUInt32BE(seconds, 4) + return value + } + const replacement = packet(10, 2, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4])) + parse(at(template(10), 100)) + parse(at(replacement, 100)) + unavailable(() => parse(at(data(10), 100))) + unavailable(() => parse(at(data(10), 101))) + parse(at(template(10), 100)) + unavailable(() => parse(at(data(10), 101))) + parse(at(template(10), 102)) + unavailable(() => parse(at(data(10), 101))) + assert.equal(parse(at(data(10), 102))[0].src, "192.0.2.1") + parse(at(replacement, 103)) + assert.equal(parse(at(data(10), 103))[0].src, "198.51.100.1") +}) + +for (const version of [9, 10] as const) { + test(`v${version}: exact monotonic expiry refuses repeated data until a real template refresh`, t => { + let now = 100; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests() + parse(template(version)); now += lifetime - 1 + assert.equal(parse(data(version))[0].src, "192.0.2.1") + now++; unavailable(() => parse(data(version))); unavailable(() => parse(data(version))) + assert.equal(templateCacheUsageForTests("known").exporterTemplates, 0) + assert.equal(templateCacheUsageForTests().fields, 0) + parse(template(version)); assert.equal(parse(data(version))[0].dst, "198.51.100.1") + resetFlowTemplatesForTests() + }) + test(`v${version}: data never extends lifetime; accepted refresh does; malformed refresh does not`, t => { + let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests() + parse(template(version)); now = lifetime - 1; parse(data(version)); parse(template(version)) + now = lifetime; assert.equal(parse(data(version)).length, 1) + now = 2 * lifetime - 2 + const malformed = packet(version, version === 9 ? 0 : 2, Buffer.from([1, 0, 0, 1])) + assert.throws(() => parse(malformed), error => error instanceof FlowTemplateRefused && error.reason === "truncated") + assert.equal(parse(data(version)).length, 1) + now++; unavailable(() => parse(data(version))); resetFlowTemplatesForTests() + }) + test(`v${version}: expiry releases exact field budgets and preserves freshly renewed neighbour`, t => { + let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests() + parse(template(version)); parse(template(version, 257)); now = lifetime / 2; parse(template(version, 257)) + now = lifetime; unavailable(() => parse(data(version))) + assert.equal(parse(data(version, 257)).length, 1) + assert.equal(templateCacheUsageForTests().templates, 1); assert.equal(templateCacheUsageForTests().fields, 2) + parse(template(version, 258)); assert.equal(templateCacheUsageForTests().templates, 2) + assert.equal(templateCacheUsageForTests().fields, 4) + now = 3 * lifetime; parse(template(version, 259)) + assert.equal(templateCacheUsageForTests().templates, 1); assert.equal(templateCacheUsageForTests().fields, 2) + resetFlowTemplatesForTests() + }) +} + +test("IPFIX UDP withdrawals neither delete nor refresh retained template lifetime", t => { + let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests() + parse(template(10)); now = lifetime - 1 + for (const set of [2, 3]) parse(packet(10, set, Buffer.from([1, 0, 0, 0]))) + assert.equal(parse(data(10)).length, 1) + now++; unavailable(() => parse(data(10))); resetFlowTemplatesForTests() +}) + +test("template expiry remains isolated across protocol/domain/actual port/receiver/legacy partitions", t => { + let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests() + parse(template(10)); now = lifetime / 2 + const others = [ + { packet: template(9), data: data(9), context: transport }, + { packet: template(10, 256, 1), data: data(10, 256, 1), context: transport }, + { packet: template(10), data: data(10), context: { ...transport, sourcePort: 2056 } }, + { packet: template(10), data: data(10), context: { ...transport, collectorSocketId: "replacement" } }, + { packet: template(10), data: data(10), context: undefined }, + ] + for (const other of others) parseFlowPacket(other.packet, "known", other.context) + now = lifetime; unavailable(() => parse(data(10))) + for (const other of others) assert.equal(parseFlowPacket(other.data, "known", other.context).length, 1) + assert.equal(templateCacheUsageForTests("known").exporterTemplates, 5); resetFlowTemplatesForTests() +}) + +test("real ingress body exposes missing/expired template as refusal and never queues a fabricated zero", t => { + let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests() + const source = readFileSync(new URL("./traffic-flow-engine.ts", import.meta.url), "utf8") + const ast = ts.createSourceFile("engine.ts", source, ts.ScriptTarget.ES2022, true) + const ingress = ast.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "ingestDatagram")! + const code = ts.transpileModule(ingress.getText(ast), { compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText + let error = ""; const queued: unknown[] = [], exports: { ingestDatagram?: (value: Buffer, exporter: string, context: typeof transport) => boolean } = {} + runInNewContext(code, { exports, Buffer, FlowTemplateRefused, parseFlowPacket, bumpPacketMeta() {}, resolveServerId: () => 1, + setEngineError: (value: string) => { error = value }, maybeRefreshIfaces() {}, queueParsedFlows: (_id: number, value: unknown) => queued.push(value) }) + const ingest = (value: Buffer) => exports.ingestDatagram!(value, "known", transport) + assert.equal(ingest(template(10)), true); assert.equal(ingest(data(10)), true); assert.equal(queued.length, 1) + now = lifetime + for (let attempt = 0; attempt < 2; attempt++) { + assert.equal(ingest(data(10)), false); assert.match(error, /missing-template/); assert.equal(queued.length, 1) + } + assert.equal(ingest(template(10)), true); assert.match(error, /missing-template/, "Template alone does not claim data coverage") + assert.equal(ingest(data(10)), true); assert.equal(error, ""); assert.equal(queued.length, 2); resetFlowTemplatesForTests() +}) diff --git a/backend/src/services/traffic-flow-topology.ts b/backend/src/services/traffic-flow-topology.ts index 9de88cb2..3ccf0f6f 100644 --- a/backend/src/services/traffic-flow-topology.ts +++ b/backend/src/services/traffic-flow-topology.ts @@ -1,6 +1,5 @@ -import { db, dbAll } from "../db/index.js" +import { flowReadDbAll } from "./traffic-flow-read-policy.js" import { parseJsonArray } from "../db/json.js" -import { appUsers, servers, userInterfaceBindings } from "../db/schema.js" import { mapRosInterfaceType, parseRawInterfaces } from "../modules/users/iface-type.js" import { canonicalIp } from "./traffic-flow-ip.js" import type { PlaneTopology } from "./traffic-flow-planes.js" @@ -32,6 +31,7 @@ export interface FlowTopology { } export interface ServerCatalogEntry { + enabled?: boolean id: number name: string country: string @@ -60,14 +60,16 @@ export function peekServerCatalog(): { list: ServerCatalogEntry[]; byId: Map }> { +export async function getServerCatalog(serverIds?: readonly number[]): Promise<{ list: ServerCatalogEntry[]; byId: Map }> { const now = Date.now() - if (serverCatalogCache && now - serverCatalogCache.at < CATALOG_TTL_MS) { + if (serverIds === undefined && serverCatalogCache && now - serverCatalogCache.at < CATALOG_TTL_MS) { return serverCatalogCache } - const rows = await db.select().from(servers) + if (serverIds?.length === 0) return { list: [], byId: new Map() } + const rows = await flowReadDbAll('SELECT id,name,country,host,type,site,enabled FROM servers WHERE (?::bigint[] IS NULL OR id=ANY(?::bigint[]))',[serverIds??null,serverIds??null]) const list: ServerCatalogEntry[] = rows.map((s) => ({ id: s.id, + enabled: s.enabled, name: s.name || s.host, country: (s.country || "").toUpperCase() || "UN", host: s.host, @@ -75,8 +77,8 @@ export async function getServerCatalog(): Promise<{ list: ServerCatalogEntry[]; site: s.site || "—", })) const byId = new Map(list.map((s) => [s.id, s])) - serverCatalogCache = { at: now, list, byId } - return serverCatalogCache + if (serverIds === undefined) serverCatalogCache = { at: now, list, byId } + return { list, byId } } function parseWanUplinks(raw: unknown): Array<{ iface?: string; ip?: string }> { @@ -88,14 +90,16 @@ function ifaceKey(serverId: number, name: string): string { return `${serverId}|${name}` } -async function loadTunnelIfacesFromSnapshots(): Promise>> { - const rows = await dbAll<{ serverId: number; rawInterfaces: unknown }>(` - SELECT DISTINCT ON (server_id) server_id AS "serverId", raw_interfaces AS "rawInterfaces" +async function loadTunnelIfacesFromSnapshots(serverIds?: readonly number[]): Promise>> { + const rows = await flowReadDbAll<{ serverId: number; rawInterfaces: unknown }>(` + SELECT DISTINCT ON (server_id) server_id AS "serverId", CASE WHEN ?::bigint[] IS NULL OR octet_length(raw_interfaces::text)<=1048576 THEN raw_interfaces ELSE NULL END AS "rawInterfaces" FROM server_snapshots + WHERE (?::bigint[] IS NULL OR server_id=ANY(?::bigint[])) ORDER BY server_id, polled_at DESC - `) + `, [serverIds ?? null, serverIds ?? null, serverIds ?? null]) const map = new Map>() for (const r of rows) { + if (serverIds !== undefined && r.rawInterfaces === null) throw new Error("Превышен предел снимка топологии NetFlow") const set = new Set() for (const iface of parseRawInterfaces(r.rawInterfaces)) { if (iface.type !== "gre" && iface.type !== "wg") continue @@ -107,13 +111,25 @@ async function loadTunnelIfacesFromSnapshots(): Promise> return map } -export async function loadFlowTopology(): Promise { - if (seeded) return seeded +export async function loadFlowTopology(serverIds?: readonly number[], authorize: () => Promise = async () => {}): Promise { + if (serverIds && (serverIds.length > 1000 || serverIds.some(id => !Number.isSafeInteger(id) || id <= 0))) throw new Error("Некорректная область топологии NetFlow") + if (serverIds === undefined && seeded) return seeded const now = Date.now() - if (topologyCache && now - topologyCache.at < CATALOG_TTL_MS) return topologyCache.topo - const serverRows = await db.select().from(servers) - const users = await db.select().from(appUsers) - const binds = await db.select().from(userInterfaceBindings) + if (serverIds === undefined && topologyCache && now - topologyCache.at < CATALOG_TTL_MS) return topologyCache.topo + await authorize() + const serverRows = await flowReadDbAll<{id:number;host:string;name:string;type:string;wanUplinks:unknown}>('SELECT id,host,name,type,wan_uplinks AS "wanUplinks" FROM servers WHERE (?::bigint[] IS NULL OR id=ANY(?::bigint[]))',[serverIds??null,serverIds??null]) + await authorize() + const users = serverIds === undefined ? await flowReadDbAll<{id:string;login:string;name:string}>('SELECT id,login,name FROM app_users') : await flowReadDbAll<{ id: string; login: string; name: string }>(` + SELECT u.id,u.login,u.name FROM app_users u WHERE EXISTS(SELECT 1 FROM user_interface_bindings b WHERE b.user_id=u.id AND b.server_id=ANY(?::bigint[])) + AND NOT EXISTS(SELECT 1 FROM user_interface_bindings hidden WHERE hidden.user_id=u.id AND NOT(hidden.server_id=ANY(?::bigint[]))) LIMIT 1001 + `, [serverIds, serverIds]) + await authorize() + const binds = serverIds === undefined ? await flowReadDbAll<{userId:string;serverId:number;interfaceName:string}>('SELECT user_id AS "userId",server_id AS "serverId",interface_name AS "interfaceName" FROM user_interface_bindings') : await flowReadDbAll<{ userId: string; serverId: number; interfaceName: string }>(` + SELECT b.user_id AS "userId",b.server_id AS "serverId",b.interface_name AS "interfaceName" FROM user_interface_bindings b + WHERE b.server_id=ANY(?::bigint[]) AND b.peer_public_key='' AND NOT EXISTS(SELECT 1 FROM user_interface_bindings hidden WHERE hidden.user_id=b.user_id AND NOT(hidden.server_id=ANY(?::bigint[]))) ORDER BY b.id + LIMIT 1001 + `, [serverIds, serverIds]) + if (serverIds !== undefined && (binds.length > 1000 || users.length > 1000)) throw new Error("Превышен предел топологии NetFlow") const loginById = new Map(users.map((u) => [u.id, u])) const clientIfaces = new Map>() const clientByIface = new Map() @@ -149,7 +165,9 @@ export async function loadFlowTopology(): Promise { for (const h of hosts) jhHosts.add(h) } } - const tunnelIfaces = await loadTunnelIfacesFromSnapshots() + await authorize() + const tunnelIfaces = await loadTunnelIfacesFromSnapshots(serverIds) + await authorize() const topo: FlowTopology = { clientIfaces, clientByIface, @@ -164,7 +182,7 @@ export async function loadFlowTopology(): Promise { jhHosts, }, } - topologyCache = { at: Date.now(), topo } + if (serverIds === undefined) topologyCache = { at: Date.now(), topo } return topo } @@ -226,29 +244,32 @@ export function enGreIfaceNames(topo: FlowTopology, serverId: number, ifaceNames }) } -export async function latestWireBps(serverId: number, ifaceNames: string[]): Promise<{ bps: number; bytes: number }> { +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 dbAll<{ + const rows = await flowReadDbAll<{ name: string rxBps: number txBps: number rxBytes: number txBytes: number }>(` - SELECT interface_name AS name, rx_bps AS "rxBps", tx_bps AS "txBps", rx_bytes AS "rxBytes", tx_bytes AS "txBytes" + 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 sampled_at DESC + ORDER BY ${bounded ? "interface_name," : ""} sampled_at DESC `, [serverId, ...ifaceNames]) const seen = new Set() 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) } + if (bounded && (!Number.isSafeInteger(bytes) || !Number.isFinite(bps) || bps > Number.MAX_SAFE_INTEGER)) throw new Error("Переполнение физических счётчиков NetFlow") return { bps, bytes } } diff --git a/backend/src/services/traffic-flow-worker-ipc.owned.test.ts b/backend/src/services/traffic-flow-worker-ipc.owned.test.ts new file mode 100644 index 00000000..f364ae6b --- /dev/null +++ b/backend/src/services/traffic-flow-worker-ipc.owned.test.ts @@ -0,0 +1,547 @@ +import { MIGRATION_MANIFEST } from "../db/migration-manifest.js" +/** Production main/worker orchestration with real IPC, UDP and disposable SQL. + * No application bootstrap, scheduler, external exporter, RIPE or real router. + * The owned-PG runner must opt in; importing this entry without guards fails + * before importing the application database configuration. */ +import assert from "node:assert/strict" +import { createSocket } from "node:dgram" +import { createServer } from "node:http" +import { basename, dirname, isAbsolute, resolve } from "node:path" +import { performance } from "node:perf_hooks" +import { setTimeout as delay } from "node:timers/promises" +import { Worker } from "node:worker_threads" + +async function main() { + assert.equal(process.env.MMAPP_OWN_FLOW_WORKER_IPC, "true") + assert.equal(process.env.NODE_ENV, "test") + const target = new URL(process.env.DATABASE_URL ?? "") + assert(["postgres:", "postgresql:"].includes(target.protocol)) + assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") + assert(target.password); assert.equal(target.pathname, "/postgres") + assert(Number.isInteger(Number(target.port)) && Number(target.port) > 0 && Number(target.port) <= 65535) + assert.equal(target.search, ""); assert.equal(target.hash, "") + assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) + assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") + assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) + process.env.FLOW_LISTEN_HOST = "127.0.0.1" + + const { pool, closePool } = await import("../db/index.js") + const { applySqlMigrations } = await import("../db/migrate.js") + const { ensurePartitionsAround } = await import("../db/partitions.js") + const ingest = await import("./traffic-flow-ingest.js") + const settings = await import("./traffic-flow-settings.js") + const { requestExistingWorkerDrain } = await import("./traffic-flow-drain-protocol.js") + const { FlowWorkerLifecycle } = await import("./traffic-flow-worker-lifecycle.js") + const { waitForCollectorShutdown } = await import("./traffic-flow-worker-shutdown.js") + const { pauseRipeBackgroundWork } = await import("./traffic-flow-ripe.js") + const sender = createSocket("udp4") + const standalone = new Set() + let fixtureError: unknown, ifaceGets = 0, serverId: number | undefined, mainStarted = false + const groups: string[] = [] + const reportError = (stage: string, error: unknown) => console.log(JSON.stringify({ stage, + type: error instanceof Error ? error.name : typeof error, + code: typeof (error as { code?: unknown } | null)?.code === "string" ? (error as { code: string }).code : null })) + let firstWorkError: unknown + const native = createServer((request, response) => { + try { + assert.equal(request.method, "GET"); assert.equal(request.url, "/rest/interface") + assert.equal(request.headers.authorization, "Basic " + Buffer.from("owned-worker:owned-password").toString("base64")) + ifaceGets++; response.setHeader("content-type", "application/json"); response.end("[]") + } catch (error) { fixtureError = error; response.destroy() } + }) + const until = async (predicate: () => boolean | Promise, description: string) => { + const deadline = performance.now() + 15000 + while (!await predicate()) { + if (fixtureError) throw fixtureError + assert(performance.now() < deadline, description) + await delay(20) + } + } + const reservePort = async () => { + const socket = createSocket("udp4") + try { + await new Promise((done, reject) => { socket.once("error", reject); socket.bind(0, "127.0.0.1", done) }) + return socket.address().port + } finally { await new Promise(done => socket.close(done)) } + } + const assertPortFree = async (port: number) => { + const socket = createSocket("udp4") + try { await new Promise((done, reject) => { socket.once("error", reject); socket.bind(port, "127.0.0.1", done) }) } + finally { try { await new Promise(done => socket.close(done)) } catch { /* failed bind has no handle */ } } + } + // Both flow endpoints are private: the genuine worker's production resolver + // cannot enqueue an external RIPE lookup for these packets. + const packet = (bytes: number, sequence: number) => { + const buffer = Buffer.alloc(72) + buffer.writeUInt16BE(5, 0); buffer.writeUInt16BE(1, 2) + buffer.writeUInt32BE(Math.floor(Date.now() / 1000), 8); buffer.writeUInt32BE(sequence, 16) + buffer.set([10, 144, 0, 10], 24); buffer.set([10, 144, 0, 20], 28) + buffer.writeUInt16BE(1, 36); buffer.writeUInt32BE(3, 40); buffer.writeUInt32BE(bytes, 44) + buffer.writeUInt16BE(49152, 56); buffer.writeUInt16BE(443, 58); buffer.writeUInt8(6, 62) + return buffer + } + const send = (port: number, bytes: number, sequence: number) => new Promise((done, reject) => + sender.send(packet(bytes, sequence), port, "127.0.0.1", error => error ? reject(error) : done())) + const authorize = async () => { if (fixtureError) throw fixtureError } + const configuration = async () => (await pool.query(`SELECT to_jsonb(s) - ARRAY[ + 'last_datagram_at','last_exporter_ip','last_error','packets_received','updated_at'] AS value + FROM traffic_flow_settings s WHERE id=1`)).rows[0].value + const totals = async () => (await pool.query("SELECT coalesce(sum(bytes),0)::bigint AS bytes, coalesce(sum(packets),0)::bigint AS packets FROM flow_buckets WHERE server_id=$1", [serverId])).rows[0] + const createWorker = () => { + const worker = new Worker(ingest.trafficFlowWorkerFileUrl()) + const threadId = worker.threadId + console.log(JSON.stringify({ stage: "standalone-worker-created", threadId })) + standalone.add(worker) + worker.on("error", error => { fixtureError = error }) + worker.once("exit", code => { standalone.delete(worker); console.log(JSON.stringify({ stage: "standalone-worker-exit", threadId, code })) }) + return worker + } + try { + await applySqlMigrations(pool) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM schema_migrations")).rows[0].n), MIGRATION_MANIFEST.length) + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers")).rows[0].n), 0) + await ensurePartitionsAround(pool) + await new Promise(done => native.listen(0, "127.0.0.1", done)) + const nativePort = (native.address() as { port: number }).port + serverId = Number((await pool.query(`INSERT INTO servers(name,host,port,use_ssl,verify_ssl,username,password,enabled) + VALUES('owned-worker-ipc','127.0.0.1',$1,false,false,'owned-worker','owned-password',false) RETURNING id`, [nativePort])).rows[0].id) + await settings.getTrafficFlowSettingsRow() + const port = await reservePort() + await pool.query(`UPDATE traffic_flow_settings SET enabled=true,collector_ip='127.0.0.1',flow_listen_port=$1, + host_private_key='owned-private-sentinel',host_public_key='owned-public-sentinel', + peers_json=$2::jsonb WHERE id=1`, [port, JSON.stringify([{ serverId, name: "Owned IPC peer", publicKey: "owned-peer-sentinel", address: "127.0.0.1", allowedIps: ["127.0.0.1/32"] }])]) + settings.invalidateTrafficFlowSettingsCache() + const original = await configuration() + const untouched = async () => (await pool.query(`SELECT + (SELECT count(*) FROM alert_outbox)::int AS alerts, + (SELECT count(*) FROM scheduler_runs)::int AS scheduler, + (SELECT count(*) FROM flow_ip_meta)::int AS ripe`)).rows[0] + const baseline = await untouched() + + // Calls production main startup and its real production Worker bootstrap. + mainStarted = true + await ingest.startTrafficFlowListener(authorize) + await until(() => ingest.getFlowListenerState().bound, "Main-owned production worker did not bind") + await send(port, 1500, 1) + await until(() => ingest.getFlowWorkerHealth().packetsReceived >= 1, "Worker did not receive actual UDP") + const drained = await ingest.drainExistingTrafficFlowWriters(authorize, [serverId]) + assert.equal(drained.existingWorker.workerPresent, true) + assert(drained.existingWorker.receipt) + const receipt = (await pool.query("SELECT result FROM flow_write_receipts WHERE batch_id=$1", [drained.existingWorker.receipt.batchId])).rows[0] + assert(receipt); assert.equal(receipt.result.buckets, drained.existingWorker.receipt.buckets) + assert.deepEqual(await totals(), { bytes: 1500, packets: 3 }) + assert.equal(ingest.getFlowListenerState().bound, true) + assert.equal(ingest.getFlowWorkerHealth().alive, true) + groups.push("main startup -> production Worker/localhost UDP -> scoped real IPC drain -> durable SQL receipt; listener remains active") + + await send(port, 2500, 2) + await until(() => ingest.getFlowWorkerHealth().packetsReceived >= 2, "Same listener did not receive second packet") + // Same port can only be rebound after the previous production worker's + // authorized retirement and clean exit, as required by main orchestration. + await ingest.startTrafficFlowListener(authorize) + await until(() => ingest.getFlowListenerState().bound, "Replacement worker did not bind after retirement") + assert.deepEqual(await totals(), { bytes: 4000, packets: 6 }) + await send(port, 3500, 3) + await until(() => ingest.getFlowWorkerHealth().packetsReceived >= 1, "Replacement worker did not receive UDP") + await ingest.drainExistingTrafficFlowWriters(authorize, [serverId]) + assert.deepEqual(await totals(), { bytes: 7500, packets: 9 }) + groups.push("real main restart retires previous worker before same-port bind; pending UDP data conserved across authorized shutdown") + + // Hold the first real authorization AFTER start A captured its generation. + // Start B retires/replaces the genuine current worker while A is suspended. + // No Worker, socket, IPC response or production main state is substituted. + let entered = false + let releaseStart!: () => void + const startBarrier = new Promise(done => { releaseStart = done }) + const displacedStart = ingest.startTrafficFlowListener(async () => { + entered = true + await startBarrier + }).then(() => ({ ok: true as const }), error => ({ ok: false as const, error })) + try { + await until(() => entered, "Displaced start did not enter its authorization barrier") + await ingest.startTrafficFlowListener(authorize) + await until(() => ingest.getFlowListenerState().bound, "Winning concurrent start did not bind") + assert.equal(ingest.getFlowListenerState().address, `127.0.0.1:${port}`) + assert.equal(ingest.getFlowWorkerHealth().alive, true) + } finally { + // Always release and join the suspended caller before terminal cleanup, + // even when the winning start fails or the real UDP bind is refused. + releaseStart() + await displacedStart + } + const displacedOutcome = await displacedStart + assert.equal(displacedOutcome.ok, false, "A superseded start must refuse its late authorization") + if (!displacedOutcome.ok) assert.match(String(displacedOutcome.error), /lifecycle changed/) + assert.equal(ingest.getFlowListenerState().bound, true) + assert.equal(ingest.getFlowWorkerHealth().alive, true) + assert.deepEqual(await totals(), { bytes: 7500, packets: 9 }) + await send(port, 4500, 4) + await until(() => ingest.getFlowWorkerHealth().packetsReceived >= 1, "Winning start did not receive actual UDP") + await ingest.drainExistingTrafficFlowWriters(authorize, [serverId]) + assert.deepEqual(await totals(), { bytes: 12000, packets: 12 }) + assert.deepEqual(await configuration(), original) + groups.push("overlapping main starts: late initial authorization refuses displaced caller; winner retains same-port listener and persists subsequent actual UDP") + + // Change the actual main generation while its authorization awaits. This + // verifies the production pre-dispatch generation check, not a fake peer. + let checks = 0 + await assert.rejects(ingest.drainExistingTrafficFlowWriters(async () => { + if (++checks === 2) ingest.stopTrafficFlowListener() + }, [serverId]), /lifecycle changed/) + await ingest.stopTrafficFlowListenerForDatabaseOperation() + mainStarted = false + assert.equal(ingest.getFlowWorkerHealth().alive, false) + await assertPortFree(port) + assert.deepEqual(await totals(), { bytes: 12000, packets: 12 }) + assert.deepEqual(await configuration(), original) + assert.deepEqual(await untouched(), baseline) + assert(ifaceGets > 0); assert.equal(fixtureError, undefined) + groups.push("actual main generation change refuses stale drain; tracked stop waits for exit, releases UDP port and preserves keys/peers/settings") + + // Faults use the same production bootstrap, with no listener start or data. + const revoked = createWorker() + let checkpoints = 0 + await assert.rejects(requestExistingWorkerDrain(revoked, async () => { + if (++checkpoints >= 2) throw Error("owned IPC authority revoked") + }, [serverId], 10000), /owned IPC authority revoked/) + await waitForCollectorShutdown(revoked, 10000) + assert(checkpoints >= 2) + groups.push("real parentPort authorization exchange revokes an existing worker drain without publication") + + const lost = createWorker() + let deathChecks = 0 + await assert.rejects(requestExistingWorkerDrain(lost, async () => { + if (++deathChecks === 2) await lost.terminate() + }, [serverId], 10000), /exited before drain acknowledgement/) + const lifecycle = new FlowWorkerLifecycle(worker => waitForCollectorShutdown(worker, 100)) + await assert.rejects(lifecycle.retire(lost), /не подтверждена/) + await assert.rejects(lifecycle.drain(), /не подтверждена/) + assert.deepEqual(await totals(), { bytes: 12000, packets: 12 }) + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline) + groups.push("real production worker exit before IPC ACK fails closed; retirement failure stays latched") + console.log(JSON.stringify({ stage: "original-worker-faults-complete", groups: groups.length })) + // Real production Worker + two actual UDP source ports + SQL. All three + // streams reuse template ID256, and only the trusted receiver scopes differ. + const scopeSender = createSocket("udp4") + let scopeWorkError: unknown + const scopeWorker = createWorker() + const scopePort = await reservePort() + let scopeBound = false, scopePacketsReceived = 0, scopeHeartbeatCount = 0, scopeAckEnabled = true, scopeHeartbeatId = "" + scopeWorker.on("message", (message: import("./traffic-flow-collector-ipc.js").WorkerToMain) => { + if (message.type === "heartbeat") { + scopeBound = message.payload.bound + scopePacketsReceived = message.payload.packetsReceived + scopeHeartbeatCount++; scopeHeartbeatId = message.heartbeatId + if (scopeAckEnabled) scopeWorker.postMessage({ type: "heartbeat-ack", heartbeatId: message.heartbeatId }) + } + }) + try { + await new Promise((done, reject) => { scopeSender.once("error", reject); scopeSender.bind(0, "127.0.0.1", done) }) + assert.notEqual(sender.address().port, scopeSender.address().port, "The two native sender sockets must have distinct actual source ports") + await requestExistingWorkerDrain(scopeWorker, authorize, [], 10000, "authorized-start", { + listenHost: "127.0.0.1", listenPort: scopePort, topN: 200, retentionHours: 24, + exporterMap: { overlayPrefix: "10.255.254.0/24", byTunnelIp: [], peers: [], hostIps: [["127.0.0.1", serverId]] }, + }) + await until(() => scopeBound, "Scoped-template production Worker did not bind") + const ipfix = (domain: number, setId: number, body: Buffer) => { + const value = Buffer.alloc(20 + body.length) + value.writeUInt16BE(10); value.writeUInt16BE(value.length, 2) + value.writeUInt32BE(Math.floor(Date.now() / 1000), 4); value.writeUInt32BE(domain, 12) + value.writeUInt16BE(setId, 16); value.writeUInt16BE(body.length + 4, 18); body.copy(value, 20) + return value + } + const ipfixTemplate = (domain: number, reverse: boolean) => { + const ids = reverse ? [12, 8, 2, 1, 4, 7, 11, 10] : [8, 12, 1, 2, 4, 7, 11, 10] + const widths = [4, 4, 4, 4, 1, 2, 2, 4], body = Buffer.alloc(4 + ids.length * 4) + body.writeUInt16BE(256); body.writeUInt16BE(ids.length, 2) + for (let index = 0; index < ids.length; index++) { body.writeUInt16BE(ids[index], 4 + index * 4); body.writeUInt16BE(widths[index], 6 + index * 4) } + return ipfix(domain, 2, body) + } + const streams = [ + { domain: 17, reverse: false, src: "10.144.1.11", dst: "10.144.1.21", bytes: 1100, packets: 11, sourcePort: 49151, socket: sender }, + { domain: 17, reverse: true, src: "10.144.1.12", dst: "10.144.1.22", bytes: 2200, packets: 22, sourcePort: 49152, socket: scopeSender }, + { domain: 18, reverse: true, src: "10.144.1.13", dst: "10.144.1.23", bytes: 3300, packets: 33, sourcePort: 49153, socket: sender }, + ] + const sendScope = (socket: typeof sender, value: Buffer) => new Promise((done, reject) => socket.send(value, scopePort, "127.0.0.1", error => error ? reject(error) : done())) + // Install all conflicting layouts before data. Without either domain or + // real source-port separation, stream17/A is decoded with the wrong layout. + for (const stream of streams) await sendScope(stream.socket, ipfixTemplate(stream.domain, stream.reverse)) + await until(() => scopePacketsReceived >= 3, "Scoped Worker did not receive all three template datagrams") + for (const stream of streams) { + const body = Buffer.alloc(25) + const addresses = stream.reverse ? [stream.dst, stream.src] : [stream.src, stream.dst] + body.set(addresses[0].split(".").map(Number), 0); body.set(addresses[1].split(".").map(Number), 4) + body.writeUInt32BE(stream.reverse ? stream.packets : stream.bytes, 8) + body.writeUInt32BE(stream.reverse ? stream.bytes : stream.packets, 12) + body.writeUInt8(6, 16); body.writeUInt16BE(stream.sourcePort, 17); body.writeUInt16BE(443, 19); body.writeUInt32BE(1, 21) + await sendScope(stream.socket, ipfix(stream.domain, 256, body)) + } + await until(() => scopePacketsReceived >= 6, "Scoped Worker did not receive all six IPFIX datagrams") + assert.equal(scopePacketsReceived, 6) + // Hold the actual main-side ACK across two real worker timer ticks. + scopeAckEnabled = false + const beforeHeldHeartbeat = scopeHeartbeatCount + await until(() => scopeHeartbeatCount > beforeHeldHeartbeat, "Worker must publish its first held heartbeat") + const heldCount = scopeHeartbeatCount, heldId = scopeHeartbeatId + await delay(4500) + assert.equal(scopeHeartbeatCount, heldCount, "Slow consumer must receive only one outstanding snapshot across real timer ticks") + scopeWorker.postMessage({ type: "heartbeat-ack", heartbeatId: "not-the-owned-frame" }) + await delay(50) + assert.equal(scopeHeartbeatCount, heldCount, "Foreign ACK must not release the actual worker sender") + scopeAckEnabled = true + scopeWorker.postMessage({ type: "heartbeat-ack", heartbeatId: heldId }) + await until(() => scopeHeartbeatCount > heldCount, "Matching ACK must publish coalesced current state") + assert.equal(scopePacketsReceived, 6) + groups.push("real production Worker heartbeat backpressure: one outstanding frame across two timer ticks, foreign ACK refused, matching ACK resumes latest state") + const scopedDrain = await requestExistingWorkerDrain(scopeWorker, authorize, [serverId], 10000) + assert.equal(scopedDrain.workerPresent, true) + const rows = (await pool.query(`SELECT host(src) AS src,host(dst) AS dst,src_port,coalesce(sum(bytes),0)::bigint AS bytes,coalesce(sum(packets),0)::bigint AS packets + FROM flow_buckets WHERE server_id=$1 AND src <<= '10.144.1.0/24'::inet GROUP BY src,dst,src_port ORDER BY src`, [serverId])).rows + assert.deepEqual(rows, streams.map(stream => ({ src: stream.src, dst: stream.dst, src_port: stream.sourcePort, bytes: stream.bytes, packets: stream.packets }))) + assert.deepEqual(await totals(), { bytes: 18600, packets: 78 }, "All previous v5 totals plus three scoped IPFIX records must be conserved once") + assert.deepEqual((await pool.query("SELECT coalesce(sum(bytes),0)::bigint AS bytes,coalesce(sum(packets),0)::bigint AS packets FROM flow_minute_stats WHERE server_id=$1", [serverId])).rows[0], { bytes: 18600, packets: 78 }, "Minute totals independently conserve all admitted v5 and IPFIX bytes/packets") + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline) + assert.equal(fixtureError, undefined) + groups.push("real production Worker/UDP/SQL: template ID256 isolated by two actual source ports and two observation domains; exact decoded endpoints and6600bytes/66packets conserved") + } catch (error) { scopeWorkError = error; reportError("scope-work-error", error); throw error } finally { + // Close this group's second sender on every path. The production Worker + // remains in the existing standalone set until its actual clean exit. + try { await new Promise(done => scopeSender.close(done)) } catch { /* setup may fail before bind */ } + if (scopeWorker.threadId !== -1) { + try { console.log(JSON.stringify({ stage: "scope-worker-cleanup-begin", threadId: scopeWorker.threadId })); await waitForCollectorShutdown(scopeWorker, 10000, authorize); console.log(JSON.stringify({ stage: "scope-worker-cleanup-confirmed" })) } + catch (error) { reportError("scope-cleanup-error", error); await scopeWorker.terminate(); if (scopeWorkError === undefined) throw error } + } + try { await assertPortFree(scopePort) } catch (error) { reportError("scope-port-cleanup-error", error); if (scopeWorkError === undefined) throw error } + } + assert.deepEqual(await totals(), { bytes: 18600, packets: 78 }) + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline) + console.log(JSON.stringify({ stage: "original-eight-complete", groups: groups.length })) + + // Four remaining local protocol boundaries. Authority comes from actual + // encrypted/approved MCP settings plans and the unchanged production + // adapter.current guard, never a fixture "authorized" boolean. + const { createMcpConnection, authenticateMcpToken, updateMcpSettings, setConnectionStatus } = await import("../modules/mcp/store.js") + const { invokeMcpTool, approvePlan } = await import("../modules/mcp/runtime.js") + const { decryptSecret } = await import("../modules/mcp/security.js") + const { createFlowSettingsAdapter } = await import("../modules/mcp/flow-settings-actions.js") + const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") + const owner: import("../lib/permissions.js").AuthUser = { id: "owned-flow-worker-faults", email: "worker-faults@example.invalid", name: "Owned Worker faults", apps: ["mm"], permissions: ["mm:settings:admin", "mm:traffic:admin", "mm:servers:admin"], expiresAt: Date.now() + 900000 } + await updateMcpSettings({ enabled: true }, owner) + const adapter = createFlowSettingsAdapter() + const makeAuthority = async (label: string, listenPort: number) => { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: label, mode: "approval", permissions: ["mm:traffic:write"], expiresAt: new Date(Date.now() + 600000).toISOString(), scope: { allowGlobal: true, serverIds: [serverId!], operationIds: ["netflow.settings.update"] }, limits: { callsPerMinute: 240, mutationsPerHour: 1000, objectsPerPlan: 1000, serversPerPlan: 1000 } }), owner) + const principal = await authenticateMcpToken(access.token) + const plan = await invokeMcpTool(principal, "mm_netflow_settings_update_plan", { settings: { flowListenPort: listenPort, mapServiceMinSharePct: 0 } }) as { planId: string } + await approvePlan(plan.planId, owner, true) + const sealed = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0].evidence.sealed + const evidence = JSON.parse(await decryptSecret(sealed)) as { before: { proof: string }; steps: Array<{ kind: string; service: string; input: Record }> } + const step = evidence.steps[0]; assert.equal(step.kind, "service"); assert.equal(step.service, "netflow.settings.update") + const check = async () => { + const current = (await pool.query("SELECT p.id FROM mcp_plans p JOIN mcp_connections c ON c.id=p.connection_id WHERE p.id=$1 AND p.connection_id=$2 AND c.owner_id=$3 AND p.operation_id='netflow.settings.update' AND p.operation_version=3 AND p.state='approved' AND p.approved_by IS NOT NULL AND p.expires_at>clock_timestamp()", [plan.planId, access.connection.id, owner.id])).rows + assert.equal(current.length, 1, "Original approved operation window is required") + const result = await adapter.service.current!(step.input) as { proof: string } + assert.equal(result.proof, evidence.before.proof, "Actual full configuration must remain original") + } + await check() + return { connectionId: access.connection.id, planId: plan.planId, check } + } + type Observed = { w: Worker; threadId: number; checkpoints: Array<{ requestId: string; checkpointId: string }>; results: Array<{ requestId: string; ok: boolean }>; sends: Array<{ type: string; requestId?: string; approved?: boolean; at: number }>; exited: Promise; bound(): boolean; error(): string; ack(): boolean; code(): number | null } + const observedWorkers: Observed[] = [] + function observeWorker(): Observed { + const w = createWorker(), threadId = w.threadId + const checkpoints: Array<{ requestId: string; checkpointId: string }> = [] + const results: Array<{ requestId: string; ok: boolean }> = [] + const sends: Array<{ type: string; requestId?: string; approved?: boolean; at: number }> = [] + let bound = false, lastError = "", ack = false, code: number | null = null + // Transparent send observer: forwards the exact original message to the + // actual Worker. No fake receipts/events, payload edits or delayed send. + const originalPost = w.postMessage.bind(w) + w.postMessage = (...args: Parameters) => { + originalPost(...args) + const value = args[0] as { type?: string; requestId?: string; approved?: boolean } + sends.push({ type: String(value.type), ...(value.requestId ? { requestId: value.requestId } : {}), ...(value.approved !== undefined ? { approved: value.approved } : {}), at: performance.now() }) + } + const exited = new Promise(done => w.once("exit", value => { code = value; done() })) + // Registered before the production protocol's listener, so its authority + // callback can distinguish real checkpoints from the100ms periodic poll. + w.on("message", (message: import("./traffic-flow-collector-ipc.js").WorkerToMain) => { + if (message.type === "heartbeat") { bound = message.payload.bound; lastError = message.payload.lastError; w.postMessage({ type: "heartbeat-ack", heartbeatId: message.heartbeatId }) } + if (message.type === "drain-authorize") checkpoints.push({ requestId: message.requestId, checkpointId: message.checkpointId }) + if (message.type === "drain-result") results.push({ requestId: message.requestId, ok: message.ok }) + if (message.type === "shutdown-complete") ack = true + }) + const observed = { w, threadId, checkpoints, results, sends, exited, bound: () => bound, error: () => lastError, ack: () => ack, code: () => code } + observedWorkers.push(observed) + return observed + } + const payload = async (listenPort: number) => { + const current = await settings.getTrafficFlowSettingsRow() + return { listenHost: "127.0.0.1", listenPort, topN: current.topN, retentionHours: current.retentionHours, exporterMap: await ingest.buildExporterMapPayload() } + } + const receiptCount = async () => Number((await pool.query("SELECT count(*)::int AS count FROM flow_write_receipts")).rows[0].count) + const originalFaultReceipts = (await pool.query("SELECT to_jsonb(r) AS value FROM flow_write_receipts r ORDER BY batch_id")).rows.map(row => row.value) + const originalFaultReceiptIds = originalFaultReceipts.map(row => String(row.batch_id)) + const checkPreserved = async (expectedTotals: { bytes: number; packets: number }, receipts: number | null) => { + assert.deepEqual(await totals(), expectedTotals) + if (receipts !== null) assert.equal(await receiptCount(), receipts, "A refused operation must not publish any new receipt") + assert.deepEqual((await pool.query("SELECT to_jsonb(r) AS value FROM flow_write_receipts r WHERE batch_id=ANY($1::uuid[]) ORDER BY batch_id", [originalFaultReceiptIds])).rows.map(row => row.value), originalFaultReceipts) + // Successful explicit startup/teardown may checkpoint runtime stats in an + // empty ingest transaction. This is not traffic data publication. + const empty = (await pool.query("SELECT kind,result FROM flow_write_receipts WHERE NOT(batch_id=ANY($1::uuid[]))", [originalFaultReceiptIds])).rows + for (const row of empty) { assert.equal(row.kind, "ingest"); assert.equal(row.result.buckets, 0); assert.equal(row.result.facts, 0); assert.equal(row.result.skippedInet, 0) } + assert.deepEqual(await configuration(), original); assert.deepEqual(await untouched(), baseline) + } + const freshCleanup = async (observed: Observed, port: number, label: string, portCheck = true) => { + console.log(JSON.stringify({ stage: "fresh-approved-cleanup-begin", label, threadId: observed.threadId })) + const cleanup = await makeAuthority(label, port) + await waitForCollectorShutdown(observed.w, 1500, cleanup.check) + await observed.exited; assert.equal(observed.code(), 0); assert.equal(observed.ack(), true) + console.log(JSON.stringify({ stage: "fresh-approved-cleanup-confirmed", label, threadId: observed.threadId, code: observed.code(), acknowledged: observed.ack() })) + if (portCheck) await assertPortFree(port) + } + const faultBefore = { bytes: 18600, packets: 78 }, oldReceipts = await receiptCount() + const fixtureSignal = async (signal: Promise, label: string) => { + let deadline: ReturnType | undefined + try { await Promise.race([signal, new Promise((_done, reject) => { deadline = setTimeout(() => reject(Error(label)), 5000) })]) } + finally { clearTimeout(deadline) } + } + let newWorkError: unknown + try { + //1. Physical production Worker exists; revoke only after its first actual + // authorized-start checkpoint, then require fresh authorized teardown. + console.log(JSON.stringify({ stage: "new-group-1-post-spawn-revoke" })) + const start = observeWorker(), startPort = await reservePort(), startAuthority = await makeAuthority("post-spawn startup authority", startPort) + let startRevoke: Promise | undefined + await assert.rejects(requestExistingWorkerDrain(start.w, async () => { + if (start.checkpoints.length) await (startRevoke ??= setConnectionStatus(startAuthority.connectionId, "paused", owner)) + await startAuthority.check() + }, [], 10000, "authorized-start", await payload(startPort))) + assert(start.checkpoints.length > 0); assert.equal(start.bound(), false) + console.log(JSON.stringify({ stage: "startup-revoke-confirmed", threadId: start.threadId, actualCheckpoints: start.checkpoints.length })) + await assertPortFree(startPort); await checkPreserved(faultBefore, oldReceipts) + assert.equal(start.sends.filter(value => value.type === "authorized-start").length, 1) + const revokedStartupCheckpoints = start.checkpoints.length + await freshCleanup(start, startPort, "fresh cleanup after failed startup") + assert(start.checkpoints.length > revokedStartupCheckpoints, "Fresh retirement must actually reauthorize in the same production Worker") + assert.equal(start.sends.filter(value => value.type === "authorized-shutdown").length, 1) + assert.equal(start.sends.filter(value => value.type === "authorized-start").length, 1, "Cleanup must never replay failed startup") + groups.push("post-spawn authorized-start real MCP revoke: no bind/SQL/publication/replay; fresh approved teardown exits") + + //2. Real native UDP bind error. Readiness must remain explicitly false; + // protocol request acknowledgement is not claimed as listener readiness. + console.log(JSON.stringify({ stage: "new-group-2-native-bind-error" })) + const blocker = createSocket("udp4"), bindPort = await reservePort(), bind = observeWorker(), bindAuthority = await makeAuthority("native bind error authority", bindPort) + try { + await new Promise((done, reject) => { blocker.once("error", reject); blocker.bind(bindPort, "127.0.0.1", done) }) + await requestExistingWorkerDrain(bind.w, bindAuthority.check, [], 10000, "authorized-start", await payload(bindPort)) + await until(() => /EADDRINUSE/.test(bind.error()), "Native bind error was not observed") + assert.equal(bind.bound(), false); await checkPreserved(faultBefore, null) + assert.equal(bind.sends.filter(value => value.type === "authorized-start").length, 1) + await freshCleanup(bind, bindPort, "fresh bind-error teardown", false) + } finally { await new Promise(done => blocker.close(() => done())) } + await assertPortFree(bindPort) + const recovered = observeWorker(), recoveryAuthority = await makeAuthority("explicit fresh startup after bind error", bindPort) + await requestExistingWorkerDrain(recovered.w, recoveryAuthority.check, [], 10000, "authorized-start", await payload(bindPort)) + await until(recovered.bound, "Explicit fresh startup did not bind the released port") + await freshCleanup(recovered, bindPort, "fresh successful-start teardown") + groups.push("actual UDP EADDRINUSE startup remains unbound/no publication/no automatic retry; explicit fresh approved startup rebinds after tracked teardown") + + //3. Actual authorized-shutdown checkpoint revoke, not strict-drain. The + // failed retirement remains sticky even after explicit fresh cleanup. + console.log(JSON.stringify({ stage: "new-group-3-retirement-revoke" })) + const retirement = observeWorker(), retirementPort = await reservePort(), retireAuthority = await makeAuthority("retirement revoke authority", retirementPort) + await requestExistingWorkerDrain(retirement.w, retireAuthority.check, [], 10000, "authorized-start", await payload(retirementPort)) + const beforeRetirementReceipts = await receiptCount() + await until(retirement.bound, "Retirement source did not bind") + const firstRetirementCheckpoint = retirement.checkpoints.length + let retirementRevoke: Promise | undefined + const life = new FlowWorkerLifecycle((worker, authorize) => waitForCollectorShutdown(worker, 1500, authorize)) + const retirementAuthorization = async () => { + if (retirement.checkpoints.length > firstRetirementCheckpoint) await (retirementRevoke ??= setConnectionStatus(retireAuthority.connectionId, "paused", owner)) + await retireAuthority.check() + } + const retired = life.retire(retirement.w, retirementAuthorization) + await assert.rejects(retired); assert(retirementRevoke) + assert.equal(life.retire(retirement.w, retirementAuthorization), retired) + let databaseDispatches = 0 + await assert.rejects(life.drain().then(() => { databaseDispatches++ })) + assert.equal(databaseDispatches, 0); await checkPreserved(faultBefore, beforeRetirementReceipts) + await freshCleanup(retirement, retirementPort, "fresh retirement-revoke teardown") + await assert.rejects(life.drain()) + groups.push("actual authorized-shutdown MCP revoke latches retirement; no replay/database dispatch; fresh teardown does not clear failure") + + //4. Existing public timeout API, NOT auth expiry and NOT rewritten plan + // timestamps. Original token/approved binding remains valid throughout. + console.log(JSON.stringify({ stage: "new-group-4-protocol-deadline" })) + const timed = observeWorker(), timedPort = await reservePort(), timedAuthority = await makeAuthority("bounded protocol deadline authority", timedPort) + await requestExistingWorkerDrain(timed.w, timedAuthority.check, [], 10000, "authorized-start", await payload(timedPort)) + const beforeTimedReceipts = await receiptCount() + await until(timed.bound, "Timeout source did not bind") + const firstTimedCheckpoint = timed.checkpoints.length + let releaseLate!: () => void, enteredLate!: () => void, returnedLate!: () => void, initialLatency = true + const lateGate = new Promise(done => { releaseLate = done }), lateEntered = new Promise(done => { enteredLate = done }), lateReturned = new Promise(done => { returnedLate = done }) + const timedAuthorization = async () => { + await timedAuthority.check() + if (initialLatency) { initialLatency = false; await delay(300); await timedAuthority.check() } + if (timed.checkpoints.length > firstTimedCheckpoint) { enteredLate(); await lateGate; await timedAuthority.check(); returnedLate() } + } + const timedLife = new FlowWorkerLifecycle((worker, authorize) => waitForCollectorShutdown(worker, 1000, authorize)) + let callerRejectedAt = Number.POSITIVE_INFINITY + const timedRetirement = timedLife.retire(timed.w, timedAuthorization) + const timedRejected = assert.rejects(timedRetirement.catch(error => { callerRejectedAt = performance.now(); throw error })) + void timedRejected.catch(() => {}) // observed until the phase-specific join + try { await fixtureSignal(lateEntered, "Real shutdown checkpoint did not arrive"); await timedRejected } finally { releaseLate() } + assert(Number.isFinite(callerRejectedAt)) + const timedRequestId = timed.checkpoints[firstTimedCheckpoint].requestId + await fixtureSignal(lateReturned, "Released real authorization did not return"); await delay(0) + assert.equal(timed.sends.filter(value => value.type === "drain-authorized" && value.requestId === timedRequestId && value.approved && value.at >= callerRejectedAt).length, 0, "A late checkpoint cannot authorize after the caller's protocol timeout") + await assert.rejects(timedLife.drain()); assert.equal(timedLife.retire(timed.w, timedAuthorization), timedRetirement) + await checkPreserved(faultBefore, beforeTimedReceipts) + await freshCleanup(timed, timedPort, "fresh protocol-timeout teardown") + await assert.rejects(timedLife.drain()) + groups.push("real Worker shutdown protocol timeout suppresses late checkpoint effects; authority remains current and retirement failure stays sticky") + } catch (error) { newWorkError = error; reportError("new-group-work-error", error); throw error } finally { + // Forced termination is fixture cleanup only, never a successful ACK or + // replacement for the failed protocol outcome. All handles are joined. + let handleCleanupError: unknown + for (const observed of observedWorkers) if (observed.w.threadId !== -1) { + try { await observed.w.terminate() } catch (error) { handleCleanupError ??= error; reportError("new-worker-terminate-error", error) } + } + try { await Promise.all(observedWorkers.map(observed => observed.exited)) } catch (error) { handleCleanupError ??= error; reportError("new-worker-join-error", error) } + console.log(JSON.stringify({ stage: "new-worker-handles-joined", workers: observedWorkers.map(observed => ({ threadId: observed.threadId, code: observed.code(), acknowledged: observed.ack() })) })) + if (newWorkError === undefined && handleCleanupError !== undefined) throw handleCleanupError + } + assert(observedWorkers.every(observed => observed.w.threadId === -1)) + await checkPreserved(faultBefore, null) + + console.log(JSON.stringify({ accepted: groups.length, groups, ifaceGets, syntheticUdpDatagrams: 10, + totalBytes: 18600, totalPackets: 78, scopedIpfixDatagrams: 6, scopedIpfixRecords: 3, actualSenderPortCount: 2, observationDomainCount: 2, mainGenerationCoverage: "pre-dispatch invalidation, sequential replacement, overlapping starts with late initial authorization", + overlappingStartLateInitialAuthorizationAccepted: true, + allConcurrentSpawnInterleavingsAccepted: false, realRouterInteroperabilityAccepted: false })) + } catch (error) { firstWorkError = error; reportError("first-work-error", error); throw error } finally { + // Main owns its hidden current/retiring workers. Its tracked database stop + // must finish before fixture rows disappear or the SQL pool is closed. + let cleanupError: unknown + let mainStopped = !mainStarted + const cleanup = async (stage: string, action: () => Promise) => { + try { await action() } catch (error) { cleanupError ??= error; reportError(stage, error) } + } + try { + if (mainStarted) await cleanup("main-stop-error", async () => { await ingest.stopTrafficFlowListenerForDatabaseOperation(); mainStopped = true }) + for (const worker of standalone) { + console.log(JSON.stringify({ stage: "outer-standalone-cleanup-begin", threadId: worker.threadId })) + await cleanup("outer-standalone-cleanup-error", async () => { try { await waitForCollectorShutdown(worker, 2000) } finally { if (worker.threadId !== -1) await worker.terminate() } }) + } + await cleanup("ripe-stop-error", pauseRipeBackgroundWork) + if (mainStopped && standalone.size === 0 && serverId !== undefined) await cleanup("fixture-row-cleanup-error", () => pool.query("DELETE FROM servers WHERE id=$1", [serverId])) + } finally { + for (const worker of standalone) await cleanup("forced-standalone-cleanup-error", () => worker.terminate()) + try { sender.close() } catch { /* no UDP sent before setup failure */ } + await cleanup("native-close-error", () => new Promise((done, reject) => native.close(error => error && (error as NodeJS.ErrnoException).code !== "ERR_SERVER_NOT_RUNNING" ? reject(error) : done()))) + await cleanup("pool-close-error", closePool) + } + if (firstWorkError === undefined && cleanupError !== undefined) throw cleanupError + } +} + +await main() diff --git a/backend/src/services/traffic-flow-worker-lifecycle.test.ts b/backend/src/services/traffic-flow-worker-lifecycle.test.ts new file mode 100644 index 00000000..c8a91b09 --- /dev/null +++ b/backend/src/services/traffic-flow-worker-lifecycle.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict" +import { EventEmitter } from "node:events" +import type { Worker } from "node:worker_threads" +import test from "node:test" +import { FlowWorkerLifecycle } from "./traffic-flow-worker-lifecycle.js" +import { waitForCollectorShutdown } from "./traffic-flow-worker-shutdown.js" + +function fixture(){ + const events=new EventEmitter();let commands=0 + Object.assign(events,{postMessage:(message:{type:string})=>{assert.equal(message.type,"shutdown");commands++}}) + return{worker:events as unknown as Worker,events,commands:()=>commands,finish:()=>{events.emit("message",{type:"shutdown-complete"});events.emit("exit",0)}} +} +test("normal sync stop remains tracked before purge/start, including an already running stop",async()=>{ + for(const next of["purge","start"]){ + const life=new FlowWorkerLifecycle(),f=fixture();let dispatched=0 + const first=life.retire(f.worker) + assert.equal(life.retire(f.worker),first);assert.equal(f.commands(),1) + const operation=life.drain().then(()=>{dispatched++}) + await Promise.resolve();assert.equal(dispatched,0,next) + f.events.emit("message",{type:"shutdown-complete"}) + await Promise.resolve();assert.equal(dispatched,0,"ack without clean exit cannot dispatch "+next) + f.events.emit("exit",0);await operation + assert.equal(dispatched,1);assert.equal(f.commands(),1) + await life.drain() + } +}) +test("drain includes another retirement registered while an earlier worker is exiting",async()=>{ + const life=new FlowWorkerLifecycle(),one=fixture(),two=fixture();let complete=false + void life.retire(one.worker) + const drained=life.drain().then(()=>{complete=true}) + void life.retire(two.worker);one.finish() + await Promise.resolve();await Promise.resolve();assert.equal(complete,false) + two.finish();await drained;assert.equal(complete,true) + assert.equal(one.commands(),1);assert.equal(two.commands(),1) +}) +test("timeout/nonzero/no acknowledgement latch the failure and forbid every later drain without retry",async()=>{ + for(const mode of["timeout","nonzero","no-ack","worker-error"]){ + const life=new FlowWorkerLifecycle(worker=>waitForCollectorShutdown(worker,10)),f=fixture() + const retirement=life.retire(f.worker) + const rejected=assert.rejects(life.drain(),/операция с базой отменена/) + if(mode==="nonzero"){f.events.emit("message",{type:"shutdown-complete"});f.events.emit("exit",1)} + if(mode==="no-ack")f.events.emit("exit",0) + if(mode==="worker-error")f.events.emit("error",Error("worker failed")) + await rejected + let deletionOrRestart=0 + await assert.rejects(life.drain().then(()=>{deletionOrRestart++}),/операция с базой отменена/) + assert.equal(life.retire(f.worker),retirement) + await assert.rejects(retirement);assert.equal(f.commands(),1);assert.equal(deletionOrRestart,0) + assert.equal(f.events.listenerCount("message"),0) + } +}) diff --git a/backend/src/services/traffic-flow-worker-lifecycle.ts b/backend/src/services/traffic-flow-worker-lifecycle.ts new file mode 100644 index 00000000..541457c1 --- /dev/null +++ b/backend/src/services/traffic-flow-worker-lifecycle.ts @@ -0,0 +1,28 @@ +import type { Worker } from "node:worker_threads" +import { waitForCollectorShutdown } from "./traffic-flow-worker-shutdown.js" + +/** A sync stop may detach the active worker, but its SQL lifetime remains owned + * until shutdown acknowledgement AND clean exit. Failures are never forgotten. */ +export class FlowWorkerLifecycle { + private readonly retiring=new Map>() + private failure:unknown + constructor(private readonly shutdown:(worker:Worker,authorize?:()=>Promise)=>Promise=(worker,authorize)=>waitForCollectorShutdown(worker,30000,authorize)){} + retire(worker:Worker,authorize?:()=>Promise):Promise{ + const existing=this.retiring.get(worker) + if(existing)return existing + let stopped:Promise + try{stopped=this.shutdown(worker,authorize)}catch(error){stopped=Promise.reject(error)} + this.retiring.set(worker,stopped) + // Observe fire-and-forget sync stops without converting their stored promise + // to success. Keep failed entries and a permanent failure latch. + void stopped.then(()=>{this.retiring.delete(worker)},error=>{this.failure??=error}) + return stopped + } + async drain():Promise{ + if(this.failure!==undefined)throw this.failure + while(this.retiring.size){ + await Promise.all([...this.retiring.values()]) + if(this.failure!==undefined)throw this.failure + } + } +} diff --git a/backend/src/services/traffic-flow-worker-platform.test.ts b/backend/src/services/traffic-flow-worker-platform.test.ts new file mode 100644 index 00000000..35620ab8 --- /dev/null +++ b/backend/src/services/traffic-flow-worker-platform.test.ts @@ -0,0 +1,118 @@ +/** Production constructor/bind callback component tests. Real local Worker and + * UDP handle; engine/SQL dependencies are inert. No PG or remote device. */ +import assert from "node:assert/strict" +import { spawn } from "node:child_process" +import { createSocket, type Socket } from "node:dgram" +import { readFileSync } from "node:fs" +import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises" +import { basename, dirname, isAbsolute, join, relative } from "node:path" +import { fileURLToPath, pathToFileURL } from "node:url" +import { test } from "node:test" +import ts from "typescript" + +test("actual production Worker constructor inherits parent CLI heap flags and TSX bootstrap imports", async () => { + const text = readFileSync(new URL("./traffic-flow-ingest.ts", import.meta.url), "utf8") + const source = ts.createSourceFile("traffic-flow-ingest.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS) + const constructors: ts.NewExpression[] = [] + const visit = (node: ts.Node) => { if (ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "Worker") constructors.push(node); ts.forEachChild(node, visit) } + visit(source); assert.equal(constructors.length, 1) + const constructor = constructors[0].getText(source) + const workspace = await realpath(fileURLToPath(new URL("../../../", import.meta.url))) + const parentDirectory = join(workspace, "tmp") + await mkdir(parentDirectory, { recursive: true }) + const parent = await realpath(parentDirectory), parentRelative = relative(workspace, parent) + assert(parentRelative && !parentRelative.startsWith("..") && !isAbsolute(parentRelative)) + const directory = await mkdtemp(join(parent, "mm-flow-worker-platform-")) + try { + const typed = join(directory, "typed.mts"), worker = join(directory, "worker.mjs"), parent = join(directory, "parent.mjs") + await writeFile(typed, "export const typedValue: number = 7;\n") + await writeFile(worker, `import {parentPort} from 'node:worker_threads'; + import {getHeapStatistics} from 'node:v8'; import {tsImport} from 'tsx/esm/api'; + const typed=await tsImport(${JSON.stringify(pathToFileURL(typed).href)},import.meta.url); + parentPort.postMessage({typed:typed.typedValue,heap:getHeapStatistics().heap_size_limit,argv:process.execArgv});parentPort.close();`) + await writeFile(parent, `import assert from 'node:assert/strict';import {Worker} from 'node:worker_threads';import {getHeapStatistics} from 'node:v8'; + const trafficFlowWorkerFileUrl=()=>new URL(${JSON.stringify(pathToFileURL(worker).href)}); + const worker=${constructor}; + const timer=setTimeout(()=>{worker.terminate();process.exitCode=1},10000); + worker.once('error',error=>{clearTimeout(timer);console.error(error);process.exitCode=1}); + worker.once('message',value=>{assert.equal(value.typed,7);assert.equal(value.heap,getHeapStatistics().heap_size_limit); + assert(value.argv.includes('--import'));console.log(JSON.stringify({node:process.version,...value}));}); + worker.once('exit',code=>{clearTimeout(timer);if(code!==0)process.exitCode=1});`) + const result = await new Promise<{ code: number | null; stdout: string; stderr: string }>((resolve, reject) => { + const child = spawn(process.execPath, ["--max-old-space-size=192", "--max-semi-space-size=16", "--import", "tsx", parent], { cwd: fileURLToPath(new URL("../../../", import.meta.url)), stdio: ["ignore", "pipe", "pipe"], windowsHide: true }) + let stdout = "", stderr = "" + let timedOut = false + const timer = setTimeout(() => { timedOut = true; child.kill() }, 15000) + child.stdout.on("data", value => { stdout += value }); child.stderr.on("data", value => { stderr += value }) + child.once("error", error => { clearTimeout(timer); reject(error) }) + child.once("exit", code => { clearTimeout(timer); if (timedOut) reject(Error("Owned Worker probe timeout")); else resolve({ code, stdout, stderr }) }) + }) + assert.equal(result.code, 0, result.stderr) + const report = JSON.parse(result.stdout.trim()); assert.equal(report.typed, 7) + assert(report.argv.includes("--max-old-space-size=192")); assert(report.argv.includes("--max-semi-space-size=16")) + console.log("Owned platform inheritance:", JSON.stringify({ node: report.node, heapLimitBytes: report.heap, actualWorker: true, tsxTypedImport: true, targetNode22Accepted: false, wholeInstallationRamAccepted: false })) + } finally { + const target = await realpath(directory) + assert.equal(dirname(target), parent) + assert(basename(target).startsWith("mm-flow-worker-platform-")) + await rm(target, { recursive: true, force: true }) + } +}) + +function bindingFixture(real: boolean, bufferFails = false) { + const text = readFileSync(new URL("./traffic-flow-collector-worker.ts", import.meta.url), "utf8") + const start = text.slice(text.indexOf("async function startListener("), text.indexOf("\nfunction startFlowFlushTimer(")) + const script = ts.transpileModule(start, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText + let current: Socket | undefined, callback: (() => void) | undefined, receiveCalls = 0, heartbeats = 0, requested = 0, actual: number | undefined + const fake = { on() { return this }, bind(_port: number, _host: string, done: () => void) { callback = done }, setRecvBufferSize(bytes: number) { receiveCalls++; requested = bytes; if (bufferFails) throw Error("platform clamp/refusal") } } + const create = () => { + if (!real) return fake + const socket = createSocket("udp4"), set = socket.setRecvBufferSize.bind(socket) + current = socket + socket.setRecvBufferSize = bytes => { + socket.address(); receiveCalls++; requested = bytes + try { if (bufferFails) throw Error("platform fallback"); set(bytes) } + finally { actual = socket.getRecvBufferSize() } + } + return socket + } + const adapter = new Function("deps", ` + let socket=null,bound=false,address=null,shuttingDown=false,startingAuthorized=false; + const createSocket=deps.create;const randomUUID=()=>"owned-platform-socket";const clearFlowTemplateCache=()=>{};const setEngineError=()=>{};const send=()=>{}; + const configureEngine=()=>{};const applyExporterMap=()=>{};const stopListener=()=>{}; + const setFlowEngineLifecycleAuthorization=()=>{};const quiesceListener=()=>{};const flushPending=async()=>{}; + const startFlowFlushTimer=()=>{};const heartbeat=deps.heartbeat;const ingestDatagram=()=>{}; + ${script} + return {start:startListener,retire:()=>{socket=null},shutdown:()=>{shuttingDown=true},bound:()=>bound}; + `)({ create, heartbeat: () => { heartbeats++ } }) as { start: (payload: unknown) => Promise; retire: () => void; shutdown: () => void; bound: () => boolean } + return { ...adapter, finishBind: () => { assert(callback); callback() }, socket: () => current, counts: () => ({ receiveCalls, heartbeats, requested, actual }) } +} +const payload = { listenHost: "127.0.0.1", listenPort: 0, topN: 20, retentionHours: 24, exporterMap: {} } + +test("real localhost UDP handle requests receive buffer after bind and reads actual OS value", async () => { + for (const bufferFails of [false, true]) { + const f = bindingFixture(true, bufferFails) + await f.start(payload) + const socket = f.socket(); assert(socket) + try { + if (!f.bound()) await new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(Error("Owned localhost bind timeout")), 3000) + socket.once("listening", () => { clearTimeout(timer); resolve() }); socket.once("error", error => { clearTimeout(timer); reject(error) }) + }) + assert.equal(f.bound(), true); assert.equal(f.counts().receiveCalls, 1); assert.equal(f.counts().requested, 8 * 1024 * 1024) + assert.equal(typeof f.counts().actual, "number"); assert(f.counts().actual! > 0) + console.log("Owned UDP receive buffer:", JSON.stringify({ requestedBytes: f.counts().requested, actualBytes: f.counts().actual, injectedPlatformRefusal: bufferFails, platform: process.platform, targetLinuxAccepted: false })) + } finally { await new Promise(resolve => socket.close(resolve)) } + } +}) + +test("retired/shutdown bind callbacks never configure a stale socket, platform refusal retains bound listener", async () => { + for (const mode of ["retired", "shutdown"] as const) { + const f = bindingFixture(false); await f.start(payload) + assert.equal(f.counts().receiveCalls, 0) + if (mode === "retired") f.retire(); else f.shutdown() + f.finishBind(); assert.equal(f.counts().receiveCalls, 0); assert.equal(f.bound(), false); assert.equal(f.counts().heartbeats, 0) + } + const fallback = bindingFixture(false, true); await fallback.start(payload); fallback.finishBind() + assert.equal(fallback.bound(), true); assert.equal(fallback.counts().receiveCalls, 1); assert.equal(fallback.counts().heartbeats, 1) +}) diff --git a/backend/src/services/traffic-flow-worker-shutdown.ts b/backend/src/services/traffic-flow-worker-shutdown.ts index 6680fc2a..78e034ad 100644 --- a/backend/src/services/traffic-flow-worker-shutdown.ts +++ b/backend/src/services/traffic-flow-worker-shutdown.ts @@ -1,11 +1,14 @@ +import { requestExistingWorkerDrain } from "./traffic-flow-drain-protocol.js" import type { Worker } from "node:worker_threads" import type { WorkerToMain } from "./traffic-flow-collector-ipc.js" /** Both an acknowledgement after pool.end() and a clean exit are required. */ -export function waitForCollectorShutdown(worker: Worker, timeoutMs = 30_000): Promise { +export function waitForCollectorShutdown(worker: Worker, timeoutMs = 30_000, authorize?: () => Promise): Promise { return new Promise((resolve, reject) => { let acknowledged = false + let active = true const cleanup = () => { + active = false clearTimeout(timer) worker.off("message", message); worker.off("exit", exit); worker.off("error", error) } @@ -17,6 +20,18 @@ export function waitForCollectorShutdown(worker: Worker, timeoutMs = 30_000): Pr } const timer = setTimeout(error, timeoutMs) worker.on("message", message); worker.on("exit", exit); worker.on("error", error) - try { worker.postMessage({ type: "shutdown" }) } catch { error() } + if (authorize) { + // The caller's deadline also closes authority, including checks that + // began before timeout. The inner protocol starts after initial auth and + // can otherwise outlive this waiter's deadline; never authorize it late. + const authorizeWhilePending = async () => { + if (!active) throw new Error("Worker shutdown authority is closed") + await authorize() + if (!active) throw new Error("Worker shutdown authority is closed") + } + void requestExistingWorkerDrain(worker,authorizeWhilePending,[],timeoutMs,"authorized-shutdown").catch(error) + } else { + try { worker.postMessage({ type: "shutdown" }) } catch { error() } + } }) } diff --git a/backend/src/services/traffic-flow-write-transaction.pg.test.ts b/backend/src/services/traffic-flow-write-transaction.pg.test.ts new file mode 100644 index 00000000..8dda9880 --- /dev/null +++ b/backend/src/services/traffic-flow-write-transaction.pg.test.ts @@ -0,0 +1,184 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import type { PoolClient } from "pg" +import { pool, closePool } from "../db/index.js" +import { commitFlowWrite, reconcileFlowWrite, flowWriteQuery, FlowCommitUnknown, FLOW_WRITE_LOCK_KEY } from "./traffic-flow-write-transaction.js" +import { withFlowWriterExclusion } from "./traffic-flow-writer-exclusion.js" + +// Only the disposable embedded-PG runner may invoke this SQL acceptance. +const url = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") +assert.equal(process.env.MMAPP_OWN_FLOW_WRITES, "true") +assert.equal(url.hostname, "127.0.0.1") +assert.equal(url.pathname, "/postgres") +assert.match(process.env.MCP_STATE_DIR ?? "", /mm-mcp-test-/) +const schema = "flow_write_" + randomUUID().replaceAll("-", "") +const connect = async () => { + const client = await pool.connect() + try { await client.query(`SET search_path TO ${schema}`); return client } + catch (error) { client.release(true); throw error } +} +const identity = () => ({ id: randomUUID(), kind: "rebuild" as const, sourceProof: "a".repeat(64) }) +const write = async (value: number) => { + for (const table of ["flow_hour_facts", "flow_daily_facts", "events"]) + await flowWriteQuery(`INSERT INTO ${table}(value) VALUES($1)`, [value]) + return { value } +} +const counts = async () => { + const client = await connect() + try { + const values: number[] = [] + for (const table of ["flow_hour_facts", "flow_daily_facts", "events", "flow_write_receipts"]) + values.push(Number((await client.query(`SELECT count(*) AS n FROM ${table}`)).rows[0].n)) + return values + } finally { client.release() } +} +const waitForLockWaiter = async (pid: number) => { + const deadline = Date.now() + 3000 + while (Date.now() < deadline) { + if ((await pool.query("SELECT 1 FROM pg_locks WHERE pid=$1 AND NOT granted AND locktype='advisory'", [pid])).rowCount) return + await new Promise(resolve => setTimeout(resolve, 10)) + } + throw Error("Expected PostgreSQL advisory-lock waiter was not observed") +} + +try { + await pool.query(`CREATE SCHEMA ${schema}`) + const setup = await connect() + try { + await setup.query(await readFile(new URL("../../drizzle/0032_flow_write_receipts.sql", import.meta.url), "utf8")) + // Shadow publication tables isolate the transaction contract from collector lifecycle. + for (const table of ["flow_hour_facts", "flow_daily_facts", "events"]) + await setup.query(`CREATE TABLE ${table}(value integer NOT NULL)`) + } finally { setup.release() } + + const success = identity() + assert.deepEqual(await commitFlowWrite(success, () => write(1), undefined, connect), { value: 1 }) + assert.deepEqual(await counts(), [1, 1, 1, 1]) + let replayed = 0 + assert.deepEqual(await commitFlowWrite(success, async () => { replayed++; return write(99) }, undefined, connect), { value: 1 }) + assert.equal(replayed, 0) + await assert.rejects(commitFlowWrite({ ...success, sourceProof: "b".repeat(64) }, () => write(2), undefined, connect), /source does not match/) + + const rolledBack = identity() + await assert.rejects(commitFlowWrite(rolledBack, async () => { await write(2); throw Error("publication fault") }, undefined, connect), /publication fault/) + assert.deepEqual(await counts(), [1, 1, 1, 1]) + assert.deepEqual(await reconcileFlowWrite(rolledBack, undefined, connect), { committed: false, result: null }) + + // An aborted transaction can answer COMMIT with a successful ROLLBACK tag. + // This is not durable publication acknowledgement and must retain ownership. + const rollbackAcknowledged=identity() + let actualCommitCommand:string|undefined,rollbackBackendDestroyed=false + const abortedCommit=async()=>{ + const client=await connect() + return new Proxy(client,{get(target,key){ + if(key==="query")return async(...args:unknown[])=>{ + if(args[0]==="COMMIT"){ + await assert.rejects(target.query("SELECT 1/0")) + const answer=await target.query("COMMIT");actualCommitCommand=answer.command;return answer + } + return (target.query as (...args:unknown[])=>Promise).apply(target,args) + } + if(key==="release")return(destroy=false)=>{rollbackBackendDestroyed=destroy;target.release(destroy)} + const value=Reflect.get(target,key);return typeof value==="function"?value.bind(target):value + }})as PoolClient + } + await assert.rejects(commitFlowWrite(rollbackAcknowledged,()=>write(8),undefined,abortedCommit),FlowCommitUnknown) + assert.equal(actualCommitCommand,"ROLLBACK");assert.equal(rollbackBackendDestroyed,true) + assert.deepEqual(await counts(),[1,1,1,1]) + assert.deepEqual(await reconcileFlowWrite(rollbackAcknowledged,undefined,connect),{committed:false,result:null}) + + // Actual COMMIT succeeds, but its response is lost to the caller. + const lost = identity() + const loseAck = async () => { + const client = await connect() + return new Proxy(client, { get(target, key) { + if (key === "query") return async (...args: unknown[]) => { + const result = await (target.query as (...args: unknown[]) => Promise).apply(target, args) + if (args[0] === "COMMIT") throw Error("simulated lost COMMIT response") + return result + } + const value = Reflect.get(target, key) + return typeof value === "function" ? value.bind(target) : value + } }) as PoolClient + } + await assert.rejects(commitFlowWrite(lost, () => write(3), undefined, loseAck), FlowCommitUnknown) + assert.deepEqual(await reconcileFlowWrite(lost, undefined, connect), { committed: true, result: { value: 3 } }) + assert.deepEqual(await counts(), [2, 2, 2, 2]) + + // A predecessor still holds its actual PostgreSQL transaction/batch lock. + // Receipt absence must remain inconclusive until that backend is finished. + const pending = identity(), predecessor = await connect() + await predecessor.query("BEGIN") + await predecessor.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", ["flow-write-batch:" + pending.id]) + await predecessor.query("INSERT INTO flow_write_receipts(batch_id,kind,source_proof,result) VALUES($1,$2,$3,$4)", [pending.id, pending.kind, pending.sourceProof, { value: 4 }]) + const observer = await connect(), observerPid = Number((await observer.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + let finished = false + const waiting = reconcileFlowWrite(pending, undefined, async () => observer).then(value => { finished = true; return value }) + try { + await waitForLockWaiter(observerPid) + assert.equal(finished, false) + await predecessor.query("COMMIT") + assert.deepEqual(await waiting, { committed: true, result: { value: 4 } }) + } finally { await predecessor.query("ROLLBACK"); predecessor.release(); await waiting } + + const absent = identity(), rollingBack = await connect() + await rollingBack.query("BEGIN") + await rollingBack.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", ["flow-write-batch:" + absent.id]) + const absentObserver = await connect(), absentPid = Number((await absentObserver.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + let absenceConfirmed = false + const absentWaiting = reconcileFlowWrite(absent, undefined, async () => absentObserver).then(value => { absenceConfirmed = true; return value }) + try { + await waitForLockWaiter(absentPid) + assert.equal(absenceConfirmed, false) + await rollingBack.query("ROLLBACK") + assert.deepEqual(await absentWaiting, { committed: false, result: null }) + } finally { await rollingBack.query("ROLLBACK"); rollingBack.release(); await absentWaiting } + + // The production purge lease must fence an ordinary shared ingest writer too. + let releaseFence: () => void = () => {}, fenceEntered: () => void = () => {} + const fenceReady = new Promise(resolve => { fenceEntered = resolve }) + const release = new Promise(resolve => { releaseFence = resolve }) + const exclusion = withFlowWriterExclusion(async () => { fenceEntered(); await release }, undefined, connect) + await fenceReady + const blockedClient = await connect(), blockedPid = Number((await blockedClient.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + let bodyStarted = false + const blockedWrite = commitFlowWrite(identity(), async () => { bodyStarted = true; return write(5) }, undefined, async () => blockedClient) + try { + await waitForLockWaiter(blockedPid) + assert.equal(bodyStarted, false) + } finally { releaseFence(); await exclusion } + assert.deepEqual(await blockedWrite, { value: 5 }) + + const writer = await connect() + try { + await writer.query("BEGIN") + await writer.query("SELECT pg_advisory_xact_lock_shared($1)", [FLOW_WRITE_LOCK_KEY]) + let ran = false + await assert.rejects(commitFlowWrite({ ...identity(), exclusive: true }, async () => { ran = true; return write(5) }, undefined, connect), /writers are busy/) + assert.equal(ran, false) + } finally { await writer.query("ROLLBACK"); writer.release() } + + const receiptBlocker = await connect(), cachedObserver = await connect() + await receiptBlocker.query("BEGIN") + await receiptBlocker.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", ["flow-write-batch:" + success.id]) + const cachedPid = Number((await cachedObserver.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) + let cachedRevoked = false, cachedBody = false + const cachedWaiting = commitFlowWrite(success, async () => { cachedBody = true; return write(98) }, async () => { if (cachedRevoked) throw Error("cached authority revoked during lock wait") }, async () => cachedObserver) + try { + await waitForLockWaiter(cachedPid) + cachedRevoked = true + await receiptBlocker.query("ROLLBACK") + await assert.rejects(cachedWaiting, /cached authority revoked during lock wait/) + assert.equal(cachedBody, false) + } finally { await receiptBlocker.query("ROLLBACK"); receiptBlocker.release() } + + const beforeRevocation = await counts() + let revoked = false + await assert.rejects(commitFlowWrite(identity(), async () => { await write(6); revoked = true; return { value: 6 } }, async () => { if (revoked) throw Error("revoked before commit") }, connect), /revoked before commit/) + assert.deepEqual(await counts(), beforeRevocation) + console.log("Flow writer real PostgreSQL: atomic cubes/event/receipt rollback, idempotence, source mismatch, lost COMMIT ACK, observed in-flight COMMIT/rollback advisory barriers, both shared/exclusive lease directions, cached-receipt revocation after real lock wait and pre-COMMIT revocation passed") +} finally { + await pool.query(`DROP SCHEMA IF EXISTS ${schema} CASCADE`) + await closePool() +} diff --git a/backend/src/services/traffic-flow-write-transaction.test.ts b/backend/src/services/traffic-flow-write-transaction.test.ts new file mode 100644 index 00000000..a300f64d --- /dev/null +++ b/backend/src/services/traffic-flow-write-transaction.test.ts @@ -0,0 +1,97 @@ +import assert from "node:assert/strict" +import type { PoolClient } from "pg" +import { commitFlowWrite, reconcileFlowWrite, flowWriteQuery, FlowCommitUnknown, FLOW_WRITE_LOCK_KEY } from "./traffic-flow-write-transaction.js" + +const id="11111111-1111-4111-8111-111111111111",sourceProof="a".repeat(64),input={id,sourceProof,kind:"ingest" as const} +const calls:string[]=[],released:boolean[]=[] +let stored:{kind:string;source_proof:string;result:unknown}|null=null,staged:typeof stored=null,mode="normal",exclusive=true +let finishOldTransaction:(()=>void)|undefined,oldTransaction:Promise|undefined +let revokeAt:string|undefined,revoked=false +const connect=async()=>({ + query:async(sql:string|{text:string;values?:unknown[]},parameters?:unknown[])=>{ + const text=typeof sql==="string"?sql:sql.text,params=typeof sql==="string"?parameters:sql.values;calls.push(text) + if(revokeAt&&text.startsWith(revokeAt))revoked=true + if(text.startsWith("SELECT pg_try_advisory_xact_lock"))return {rows:[{acquired:exclusive}]} + if(text.startsWith("SELECT pg_advisory_lock("))await oldTransaction + if(text.startsWith("SELECT pg_advisory_unlock("))return {rows:[{released:true}]} + if(text.startsWith("SELECT kind,source_proof,result"))return {rows:stored?[stored]:[]} + if(text.startsWith("INSERT INTO flow_write_receipts"))staged={kind:String(params?.[1]),source_proof:String(params?.[2]),result:JSON.parse(String(params?.[3]))} + if(text==="ROLLBACK")staged=null + if(text==="COMMIT"){ + if(mode==="rollback-command"||mode==="missing-command"||mode==="wrong-command"){ + staged=null + return {rows:[],...(mode==="rollback-command"?{command:"ROLLBACK"}:mode==="wrong-command"?{command:"SELECT"}:{})} + } + if(mode==="delayed-lost"){ + const pending=staged;staged=null + oldTransaction=new Promise(resolve=>{finishOldTransaction=()=>{stored=pending;resolve()}}) + throw Error("lost response while COMMIT still running") + } + stored=staged;staged=null + return {rows:[],command:"COMMIT"} + } + return {rows:[]} + }, + release:(destroy=false)=>{released.push(destroy)}, +}) as unknown as PoolClient + +let checks=0 +const result=await commitFlowWrite(input,async()=>{await flowWriteQuery("UPDATE fixture SET n=?",[1]);return {buckets:1}},async()=>{checks++},connect) +assert.deepEqual(result,{buckets:1});assert.equal(checks,6) +assert.ok(calls.indexOf("SELECT pg_advisory_xact_lock_shared($1)")sql.startsWith("INSERT INTO flow_write_receipts")){executed++;return {buckets:99}},async()=>{},connect),{buckets:1});assert.equal(executed,0) +// Lock waits and even a receipt SELECT may outlive the caller's authority. +for(const boundary of ["SELECT pg_advisory_xact_lock(hashtextextended", "SELECT kind,source_proof,result"]){ + revokeAt=boundary;revoked=false;calls.length=0 + await assert.rejects(commitFlowWrite(input,async()=>{executed++;return {}},async()=>{if(revoked)throw Error("cached receipt authority revoked")},connect),/cached receipt authority revoked/) + assert.equal(executed,0) + assert.ok(!calls.some(sql=>sql.startsWith("INSERT")||sql==="COMMIT")) +} +revokeAt=undefined;revoked=false +stored=null;calls.length=0 +await assert.rejects(commitFlowWrite(input,async()=>{throw Error("known body failure")},async()=>{},connect),/known body failure/) +assert.ok(calls.includes("ROLLBACK"));assert.ok(!calls.includes("COMMIT"));assert.equal(stored,null) +exclusive=false;calls.length=0 +await assert.rejects(commitFlowWrite({...input,exclusive:true},async()=>{executed++;return {}},async()=>{},connect),/busy/) +assert.equal(executed,0);assert.ok(!calls.some(sql=>sql.startsWith("INSERT")));exclusive=true +calls.length=0 +await assert.rejects(commitFlowWrite(input,async()=>({}),async()=>{throw Error("revoked")},connect),/revoked/) +assert.ok(!calls.includes("BEGIN")) + +for(const commandMode of ["rollback-command","missing-command","wrong-command"]){ + mode=commandMode;stored=null;calls.length=0 + await assert.rejects(commitFlowWrite(input,async()=>({buckets:7}),async()=>{},connect),FlowCommitUnknown) + assert.equal(released.at(-1),true);assert.equal(stored,null) + assert.equal(calls.filter(sql=>sql==="COMMIT").length,1) + assert.ok(!calls.includes("ROLLBACK"),"An unconfirmed COMMIT quarantines the backend instead of attempting a second transition") +} +mode="normal" + +// Unknown COMMIT must destroy the connection. Even receipt absence remains +// inconclusive while the previous backend transaction still owns its batch lock. +mode="delayed-lost";calls.length=0 +await assert.rejects(commitFlowWrite(input,async()=>({buckets:7}),async()=>{},connect),FlowCommitUnknown) +assert.equal(released.at(-1),true);assert.equal(stored,null) +let reconciled=false +const waiting=reconcileFlowWrite(input,async()=>{},connect).then(value=>{reconciled=true;return value}) +await new Promise(resolve=>setImmediate(resolve));assert.equal(reconciled,false) +finishOldTransaction!() +assert.deepEqual(await waiting,{committed:true,result:{buckets:7}}) +assert.ok(calls.some(sql=>sql.startsWith("SELECT pg_advisory_lock(hashtextextended"))) +mode="normal";stored=null;oldTransaction=undefined +assert.deepEqual(await reconcileFlowWrite(input,async()=>{},connect),{committed:false,result:null}) + +// An asynchronous child must not use its former transaction after commit. +let wake:()=>void=()=>{},detached:Promise|undefined +await commitFlowWrite({...input,id:"22222222-2222-4222-8222-222222222222"},async()=>{ + const later=new Promise(resolve=>{wake=resolve}) + detached=later.then(()=>flowWriteQuery("UPDATE forbidden_after_commit SET n=1")) + return {buckets:0} +},async()=>{},connect) +wake();await assert.rejects(detached!,/context is closed/) +assert.ok(!calls.includes("UPDATE forbidden_after_commit SET n=1")) +console.log("traffic-flow-write-transaction.test.ts: fake-client lease ordering, receipts, rollback, revoke, connection quarantine, in-flight COMMIT reconciliation barrier and closed-context fencing verified; no SQL/network executed") diff --git a/backend/src/services/traffic-flow-write-transaction.ts b/backend/src/services/traffic-flow-write-transaction.ts new file mode 100644 index 00000000..2b363105 --- /dev/null +++ b/backend/src/services/traffic-flow-write-transaction.ts @@ -0,0 +1,93 @@ +import { AsyncLocalStorage } from "node:async_hooks" +import type { Pool, PoolClient, QueryResultRow } from "pg" +import { assertRuntimeDatabaseActivationCurrent, pool } from "../db/index.js" +import { bindSql } from "../db/sql-bind.js" +import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" + +// Separate from admission 8723104: final-flush drains precede exclusive access. +export const FLOW_WRITE_LOCK_KEY = 8_723_105 +export type FlowWriteKind = "ingest" | "facts" | "rebuild" +type Context = { client: PoolClient; active: boolean; partitions: Set;mode:"shared"|"exclusive";authorize:()=>Promise } +const context = new AsyncLocalStorage() +export class FlowCommitUnknown extends Error { readonly code = "FLOW_COMMIT_UNKNOWN" } +export function flowWriteContext(): Context | undefined { + const current = context.getStore() + if (current && !current.active) throw new Error("Flow write transaction context is closed") + return current +} +// Only this module resolves the owned query client; the global pool is never swapped. +export const flowWritePool = new Proxy(pool, { get(target, property, receiver) { + if (property === "query") return (async(...args:unknown[])=>{ + const current=flowWriteContext() + await current?.authorize() + if(current&&!current.active)throw Error("Flow write transaction context is closed") + const query=(current?.client??target).query.bind(current?.client??target) as (...args:unknown[])=>Promise + return query(...args) + }) as Pool["query"] + return Reflect.get(target,property,receiver) +} }) +export async function flowWriteQuery(sql:string,params?:unknown[]|Record) { + return flowWritePool.query(bindSql(sql,params)) +} +export async function flowWriteAll(sql:string,params?:unknown[]|Record):Promise { + return (await flowWriteQuery(sql,params)).rows +} +export async function withFlowWriteClient(client:PoolClient,work:()=>Promise,mode:"shared"|"exclusive"="shared",authorize:()=>Promise=async()=>{}):Promise { + const current:Context={client,active:true,partitions:new Set(),mode,authorize} + try { return await context.run(current,work) } finally { current.active=false } +} +/** Shared xact lease covers every SQL request, including partition DDL and final + * flush. Rebuild/purge take the exclusive session lease on the same key. */ +export async function commitFlowWrite>(input:{id:string;kind:FlowWriteKind;sourceProof:string;exclusive?:boolean},work:()=>Promise,authorize:()=>Promise=async()=>{},connect:()=>Promise=()=>pool.connect()):Promise { + if(flowWriteContext())throw new Error("A flow write batch must own its transaction") + const client=await connect();let commitSent=false,destroy=false + try { + await authorize();await client.query("BEGIN") + await client.query("SET LOCAL lock_timeout='10s'");await client.query("SET LOCAL statement_timeout='120s'") + // Enter partition admission on THIS transaction/backend before taking the + // flow key or issuing any partition DML/DDL. A row trigger is too late. + await client.query("SELECT pg_advisory_xact_lock_shared($1::bigint)",[PARTITION_WRITER_ADVISORY_KEY]) + if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client) + if(input.exclusive){if((await client.query("SELECT pg_try_advisory_xact_lock($1) AS acquired",[FLOW_WRITE_LOCK_KEY])).rows[0]?.acquired!==true)throw new Error("Flow writers are busy; rebuild was not started")} + else await client.query("SELECT pg_advisory_xact_lock_shared($1)",[FLOW_WRITE_LOCK_KEY]) + await client.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))",["flow-write-batch:"+input.id]) + await authorize() + assertRuntimeDatabaseActivationCurrent() + const existing=(await client.query("SELECT kind,source_proof,result FROM flow_write_receipts WHERE batch_id=$1",[input.id])).rows[0] + if(existing) { + if(existing.kind!==input.kind||existing.source_proof!==input.sourceProof)throw new Error("Flow receipt source does not match owned batch") + await client.query("ROLLBACK");await authorize();return existing.result as T + } + const result=await withFlowWriteClient(client,async()=>{await authorize();return work()},input.exclusive?"exclusive":"shared",authorize) + if(Buffer.byteLength(JSON.stringify(result))>60000)throw new Error("Flow write receipt exceeds bound") + await authorize() + await client.query("INSERT INTO flow_write_receipts(batch_id,kind,source_proof,result) VALUES($1,$2,$3,$4::jsonb)",[input.id,input.kind,input.sourceProof,JSON.stringify(result)]) + await authorize() + commitSent=true + const committed=await client.query("COMMIT") + if(committed.command!=="COMMIT")throw new FlowCommitUnknown("PostgreSQL did not confirm flow COMMIT") + return result + }catch(error) { + if(commitSent){destroy=true;throw new FlowCommitUnknown("Flow COMMIT was dispatched without a confirmed response; do not replay")} + try{await client.query("ROLLBACK")}catch{destroy=true} + throw error + }finally{client.release(destroy)} +} +/** Acquiring the SAME batch lock waits for an old COMMIT/rollback, even after + * quarantining its connection. Absence before this barrier is never evidence. */ +export async function reconcileFlowWrite(input:{id:string;kind:FlowWriteKind;sourceProof:string},authorize:()=>Promise=async()=>{},connect:()=>Promise=()=>pool.connect()):Promise<{committed:boolean;result:Record|null}> { + const client=await connect();let locked=false,destroy=false + try { + await authorize();await client.query("SET lock_timeout='10s'") + destroy=true;await client.query("SELECT pg_advisory_lock(hashtextextended($1,0))",["flow-write-batch:"+input.id]);locked=true;destroy=false + await authorize() + const row=(await client.query("SELECT kind,source_proof,result FROM flow_write_receipts WHERE batch_id=$1",[input.id])).rows[0] + if(row&&(row.kind!==input.kind||row.source_proof!==input.sourceProof))throw new Error("Flow receipt proof mismatch") + await authorize();return {committed:Boolean(row),result:row?.result??null} + }finally { + if(locked)try{const result=await client.query("SELECT pg_advisory_unlock(hashtextextended($1,0)) AS released",["flow-write-batch:"+input.id]);destroy=result.rows[0]?.released!==true}catch{destroy=true} + try{await client.query("RESET lock_timeout")}catch{destroy=true} + client.release(destroy) + } +} diff --git a/backend/src/services/traffic-flow-writer-exclusion.test.ts b/backend/src/services/traffic-flow-writer-exclusion.test.ts new file mode 100644 index 00000000..3881f610 --- /dev/null +++ b/backend/src/services/traffic-flow-writer-exclusion.test.ts @@ -0,0 +1,20 @@ +import assert from "node:assert/strict" +import type { PoolClient } from "pg" +import { withFlowWriterExclusion } from "./traffic-flow-writer-exclusion.js" + +let released:boolean|undefined,unlock=true,acquireFails=false,held=false +const log:string[]=[] +const connect=async()=>({query:async(sql:string)=>{log.push(sql);if(sql.includes("pg_advisory_lock(")){if(acquireFails)throw Error("connection lost during lock");held=true}if(sql.includes("pg_advisory_unlock(")){held=false;return {rows:[{released:unlock}]}}return {rows:[]}},release:(destroy:boolean)=>{released=destroy}} as unknown as PoolClient) +assert.equal(await withFlowWriterExclusion(async()=>{assert.equal(held,true);return 7},async()=>{},connect),7) +assert.equal(held,false);assert.equal(released,false) +assert.ok(log.findIndex(sql=>sql.includes("pg_advisory_lock("))sql.includes("pg_advisory_unlock("))) +await assert.rejects(withFlowWriterExclusion(async()=>{throw Error("maintenance failed")},async()=>{},connect),/maintenance/) +assert.equal(held,false);assert.equal(released,false) +unlock=false;await withFlowWriterExclusion(async()=>{},async()=>{},connect);assert.equal(released,true) +acquireFails=true;let work=false +await assert.rejects(withFlowWriterExclusion(async()=>{work=true},async()=>{},connect),/connection lost/) +assert.equal(work,false);assert.equal(released,true) +acquireFails=false;let checks=0 +await assert.rejects(withFlowWriterExclusion(async()=>{work=true},async()=>{if(++checks===2)throw Error("revoked")},connect),/revoked/) +assert.equal(work,false);assert.equal(held,false) +console.log("traffic-flow-writer-exclusion.test.ts: exclusive lease spans maintenance; revocation blocks body; error releases, uncertain lock/unlock quarantines; fake SQL only") diff --git a/backend/src/services/traffic-flow-writer-exclusion.ts b/backend/src/services/traffic-flow-writer-exclusion.ts new file mode 100644 index 00000000..d239591a --- /dev/null +++ b/backend/src/services/traffic-flow-writer-exclusion.ts @@ -0,0 +1,23 @@ +import type { PoolClient } from "pg" +import { pool } from "../db/index.js" +import { FLOW_WRITE_LOCK_KEY } from "./traffic-flow-write-transaction.js" + +/** Caller must finish existing-worker/main drains BEFORE entering. Admission + * 8723104 is separate, so a shutdown final flush cannot wait on its own lock. + * Hold this lease through deletion and partition maintenance; release BEFORE + * any collector restart. Uncertain lock/unlock destroys the connection. */ +export async function withFlowWriterExclusion(work:()=>Promise,authorize:()=>Promise=async()=>{},connect:()=>Promise=()=>pool.connect()):Promise { + const client=await connect();let locked=false,destroy=false + try { + await authorize();await client.query("SET lock_timeout='10s'") + destroy=true + await client.query("SELECT pg_advisory_lock($1)",[FLOW_WRITE_LOCK_KEY]) + locked=true;destroy=false + await authorize() + return await work() + }finally { + if(locked)try{const result=await client.query("SELECT pg_advisory_unlock($1) AS released",[FLOW_WRITE_LOCK_KEY]);destroy=result.rows[0]?.released!==true}catch{destroy=true} + try{await client.query("RESET lock_timeout")}catch{destroy=true} + client.release(destroy) + } +} diff --git a/backend/src/services/traffic-history-model.test.ts b/backend/src/services/traffic-history-model.test.ts new file mode 100644 index 00000000..0c621b2b --- /dev/null +++ b/backend/src/services/traffic-history-model.test.ts @@ -0,0 +1,16 @@ +import assert from "node:assert/strict" +import { projectTrafficHistory } from "./traffic-history-model.js" + +const history = { volumeUnit: "GiB" as const, id: "52", name: "NEVER_NAME", site: "NEVER_SITE", country: "UN", status: "degraded" as const, + rxNow: 0, txNow: 99, rxPeak: 0, txPeak: 99, rxTotal: 0, txTotal: 2, sessions: 0, rxSeries: [0, 0], txSeries: [99, 0], + observations: { rxNow: true, txNow: false, rxPeak: true, txPeak: false, rxSeries: [true, false], txSeries: [false, true], sampledAt: null, error: "NEVER_ERROR" } } +const result = projectTrafficHistory(history) +assert.equal(result.rxNowMbps, 0); assert.equal(result.txNowMbps, null) +assert.equal(result.rxPeakMbps, 0); assert.equal(result.txPeakMbps, null) +assert.deepEqual(result.rxSeriesMbps, [0, null]); assert.deepEqual(result.txSeriesMbps, [null, 0]) +assert.equal(result.accountedRxGiB, 0); assert.equal(result.historyCoverageVerified, false); assert.equal(result.live, false) +assert.equal(JSON.stringify(result).includes("NEVER_"), false) +assert.equal(projectTrafficHistory({ ...history, observations: undefined }).rxNowMbps, null) +assert.equal(projectTrafficHistory({ ...history, rxNow: NaN, rxTotal: -1 }).rxNowMbps, null) +assert.equal(projectTrafficHistory({ ...history, rxTotal: Infinity }).accountedRxGiB, null) +console.log("Traffic history projection: confirmed zero, unknown masks, bounded safe values, accounted volume semantics and no freeform errors/labels") diff --git a/backend/src/services/traffic-history-model.ts b/backend/src/services/traffic-history-model.ts new file mode 100644 index 00000000..eb6c439a --- /dev/null +++ b/backend/src/services/traffic-history-model.ts @@ -0,0 +1,25 @@ +import type { readServerTrafficHistory } from "./traffic-history-read.js" + +type History = Awaited> +type Metrics = Pick & { observations?: History["observations"] } +const nonnegative = (value: unknown) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : null + +/** Application zero placeholders remain null unless their observation mask confirms them. */ +export function projectTrafficHistory(history: History) { + return { serverId: Number(history.id), lastSnapshotStatus: history.status, lastSnapshotCurrentStateVerified: false, ...projectStoredTrafficMetrics(history) } +} +export function projectStoredTrafficMetrics(history: Metrics) { + const observed = history.observations + return { + currentState: observed?.currentState ?? "unknown", sampledAt: observed?.sampledAt ?? null, + expiresAt: observed?.expiresAt ?? null, lastAttemptAt: observed?.lastAttemptAt ?? null, lastSucceededAt: observed?.lastSucceededAt ?? null, + collectionError: observed?.currentState === "error", measurementIntervalMs: observed?.measurementIntervalMs ?? null, + rxNowMbps: observed?.rxNow ? nonnegative(history.rxNow) : null, txNowMbps: observed?.txNow ? nonnegative(history.txNow) : null, + rxPeakMbps: observed?.rxPeak ? nonnegative(history.rxPeak) : null, txPeakMbps: observed?.txPeak ? nonnegative(history.txPeak) : null, + rxSeriesMbps: history.rxSeries.map((value, index) => observed?.rxSeries[index] ? nonnegative(value) : null), + txSeriesMbps: history.txSeries.map((value, index) => observed?.txSeries[index] ? nonnegative(value) : null), + accountedRxGiB: nonnegative(history.rxTotal), accountedTxGiB: nonnegative(history.txTotal), + volumeMeaning: "retained-adjacent-counter-deltas", direction: "router-interface-rx-tx", deduplicatedAcrossInterfaces: false, + networkTotalVerified: false, historyCoverageVerified: false, live: false, forwardingVerified: false, + } +} diff --git a/backend/src/services/traffic-history-read.ts b/backend/src/services/traffic-history-read.ts new file mode 100644 index 00000000..018bdb77 --- /dev/null +++ b/backend/src/services/traffic-history-read.ts @@ -0,0 +1,49 @@ +import { desc, eq } from "drizzle-orm" +import { db } from "../db/index.js" +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" + +export type TrafficHistoryServer = Pick +type CollectionContext = Awaited> + +/** 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() }), + 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 status = snapshots[0]?.status === "offline" ? "offline" as const : snapshots[0]?.status === "online" ? "online" as const : "degraded" as const + return { + 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, + } +} + +/** 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 byIface = new Map() + for (const row of rows) { + if (isLoopbackName(row.interfaceName) || row.peerPublicKey) continue + const group = byIface.get(row.interfaceName) ?? [] + group.push(row); byIface.set(row.interfaceName, group) + } + const evidence = new Map() + const interfaces = [...byIface.entries()].map(([name, group]) => { + const last = group[group.length - 1]! + const built = buildTrafficFromSamples(group, rangeStartMs, rangeEndMs, name, undefined, true) + const observed = applyTrafficFreshness(built.observations!, server, context.settings, context.states.get(server.id), rangeEndMs) + evidence.set(name, { stateKnown: last.statusKnown, sampledAt: last.sampledAt, currentState: observed.currentState }) + return { name, running: Boolean(last.running), disabled: Boolean(last.disabled), rxNow: observed.rxNow ? built.rxNow : null, txNow: observed.txNow ? built.txNow : null } + }).sort((a, b) => ((b.rxNow ?? -1) + (b.txNow ?? -1)) - ((a.rxNow ?? -1) + (a.txNow ?? -1))) + return { interfaces, evidence } +} diff --git a/backend/src/services/traffic-membership.test.ts b/backend/src/services/traffic-membership.test.ts index 869ac146..8a6f3da8 100644 --- a/backend/src/services/traffic-membership.test.ts +++ b/backend/src/services/traffic-membership.test.ts @@ -16,3 +16,10 @@ assert.equal(partialPeak.observations!.rxNow,true);assert.equal(partialPeak.rxPe const future=calc([sample('a',0,0,false),sample('a',30_000,0),sample('a',120_000,1024**2)]) assert.equal(future.rxPeak,0);assert.equal(future.txPeak,0) console.log('peak excludes incomplete waves and out-of-range points; volumes retained') +const partialRows=[sample('a',0,0,false),sample('a',30_000,0),sample('a',60_000,1024**2),sample('new',60_000,0,false),sample('a',90_000,1024**2),sample('new',90_000,0)] +const end=at+95_000 +const coarse=buildTrafficFromSamples(partialRows,end-3600_000,end,undefined,undefined,true) +const fine=buildTrafficFromSamples(partialRows,end-300_000,end,undefined,undefined,true) +assert.equal(coarse.observations!.rxNow,true);assert(coarse.observations!.rxSeries.every(value=>!value)) +assert(fine.observations!.rxSeries.some(Boolean));assert.equal(fine.rxTotalGiB,coarse.rxTotalGiB) +console.log('coarse buckets retain unknown membership intervals; measured zero history is visible at finer resolution') diff --git a/backend/src/services/traffic-observation.test.ts b/backend/src/services/traffic-observation.test.ts index 4c7f6f74..dd1eeecf 100644 --- a/backend/src/services/traffic-observation.test.ts +++ b/backend/src/services/traffic-observation.test.ts @@ -37,11 +37,16 @@ console.log("traffic observation counters/rates/source confidence tests ok") const collector = fs.readFileSync(new URL("./traffic-collector.ts", import.meta.url), "utf8") const ast = ts.createSourceFile("traffic-collector.ts", collector, ts.ScriptTarget.Latest, true) const functions = ast.statements.filter(n => ts.isFunctionDeclaration(n) && n.name && - ["waveKey", "sampleRate", "toNum", "collectTrafficOnce", "collectTrafficRun"].includes(n.name.text)).map(n => n.getText(ast)).join("\n") + ["waveKey", "sampleRate", "toNum", "collectTrafficOnce", "collectTrafficUnderApplicationLock", "collectTrafficRun"].includes(n.name.text)).map(n => n.getText(ast)).join("\n") assert.equal(ast.statements.filter(n => ts.isFunctionDeclaration(n) && n.name && n.name.text === "collectTrafficOnce").length, 1) let inserted: unknown[] = [], wave = new Map(), counters: unknown = "1000" let included = true const context = vm.createContext({ + // This fixture covers rate aggregation; the real PostgreSQL collector lock is accepted separately. + withApplicationCollectorLock:async(work:()=>Promise)=>work(), + withPartitionWrite:async(run:(owner:{db:{insert:()=>{values:(rows:unknown[])=>Promise}}})=>Promise,options?:{authorize?:()=>Promise})=>{await options?.authorize?.();return run({db:{insert:()=>({values:async rows=>{inserted.push(...rows)}})}})}, + isPartitionWriteOutcomeUnknown:()=>false, + PartitionWriteError:class extends Error {}, pool:{connect:()=>{}},withTrafficCollectionLock:async(_connect:unknown,run:()=>Promise)=>({acquired:true,value:await run()}), Date, nativeTrafficCounterIdentity,parseTrafficPeers, observedTrafficRate, parseTrafficCounter, parseTrafficFlag, bpsToMbps, shouldIncludeIface, SCHEDULER_RUN_SNAPSHOT_VERSION: 1, servers: {}, trafficSamples: {}, trafficSettings: {}, eq: () => ({}), @@ -52,7 +57,7 @@ const context = vm.createContext({ [{ ".id":"*1",name: "ether1", running: String(included), disabled: "false", "rx-byte": counters, "tx-byte": counters }] : [] }) }, db: { select: () => ({ from: () => ({ where: () => {const rows=[{updatedAt:iso,id:1,name:"audit",host:"audit.example",enabled:true}];return Object.assign(Promise.resolve(rows),{limit:async()=>rows})} }) }), - insert: () => ({ values: async (rows: unknown[]) => { inserted.push(...rows) } }), + insert: () => { throw Error("Partition INSERT must use the admitted owner") }, update: () => ({ set: () => ({ where: async () => {} }) }), }, }) diff --git a/backend/src/services/traffic-status-sql.test.ts b/backend/src/services/traffic-status-sql.test.ts index 65a6f97a..be4d04f6 100644 --- a/backend/src/services/traffic-status-sql.test.ts +++ b/backend/src/services/traffic-status-sql.test.ts @@ -5,6 +5,7 @@ import {pool,closePool} from "../db/index.js" import {initDatabase} from "../db/bootstrap.js" import {collectTrafficOnce} from "./traffic-collector.js" import trafficRoutes from "../routes/traffic.js" +import {stopSchedulerForDatabaseOperation} from "./scheduler.js" const database=process.env.MMAPP_OWN_SQL_DB if(!database)console.log("traffic status SQL: skipped; explicit owned database required") else{ @@ -29,7 +30,7 @@ else{ assert.equal(rows.find((r:{interfaceName:string})=>r.interfaceName==="wg1").status,"unknown");cases++ peerDisabled=true;await collectTrafficOnce();rows=await bindings();assert.equal(rows.find((r:{interfaceName:string})=>r.interfaceName==="wg1").status,"offline");cases++ flag=false;await collectTrafficOnce();rows=await bindings();assert.equal(rows.find((r:{interfaceName:string})=>r.interfaceName==="ether1").status,"offline");cases++ - flag="bad";let snap=await collectTrafficOnce();assert.equal(snap.servers[0].ok,false);rows=await bindings();assert(rows.every((r:{status:string})=>r.status==="unknown"));cases++ + flag="bad";const snap=await collectTrafficOnce();assert.equal(snap.servers[0].ok,false);rows=await bindings();assert(rows.every((r:{status:string})=>r.status==="unknown"));cases++ flag=true;deny=true;await collectTrafficOnce();rows=await bindings();assert(rows.every((r:{status:string})=>r.status==="unknown"));cases++ deny=false;await collectTrafficOnce();await pool.query("UPDATE traffic_samples SET flags=flags & 7 WHERE server_id=$1",[serverId]);rows=await bindings();assert(rows.every((r:{status:string})=>r.status==="unknown"));cases++ await collectTrafficOnce();await pool.query("UPDATE traffic_collection_state SET last_attempt_at=$1 WHERE server_id=$2",[new Date(Date.now()-100_000).toISOString(),serverId]);rows=await bindings();assert(rows.every((r:{status:string})=>r.status==="unknown"));cases++ @@ -58,5 +59,5 @@ else{ } await pool.query("UPDATE servers SET enabled=false WHERE id=$1",[serverId]);rows=await bindings();assert(rows.every((r:{status:string})=>r.status==="unknown"));cases++ console.log(`traffic status owned SQL: ${cases} cases; first-wave native flags/peer config/invalid/error/legacy/stale/disabled passed`) - }finally{await app.close();await new Promise(resolve=>http.close(()=>resolve()));await pool.query("DELETE FROM app_users WHERE id=$1",[userId]);if(serverId!==undefined){await pool.query("DELETE FROM traffic_samples WHERE server_id=$1",[serverId]);await pool.query("DELETE FROM servers WHERE id=$1",[serverId])}await closePool()} + }finally{await app.close();await stopSchedulerForDatabaseOperation();await new Promise(resolve=>http.close(()=>resolve()));await pool.query("DELETE FROM app_users WHERE id=$1",[userId]);if(serverId!==undefined){await pool.query("DELETE FROM traffic_samples WHERE server_id=$1",[serverId]);await pool.query("DELETE FROM servers WHERE id=$1",[serverId])}await closePool()} } diff --git a/backend/src/services/traffic-user-history-read.ts b/backend/src/services/traffic-user-history-read.ts new file mode 100644 index 00000000..172f8a42 --- /dev/null +++ b/backend/src/services/traffic-user-history-read.ts @@ -0,0 +1,41 @@ +import { pool } from "../db/index.js" +import type { TrafficUserInput } from "./traffic-users.js" + +export interface ScopedTrafficUserQuery { + allowedServerIds: readonly number[]; selectedServerIds: readonly number[]; + userId?: string; offset: number; limit: number; includeUnbound?: boolean; +} +export interface ScopedTrafficUser extends TrafficUserInput { bindingCount: number; selectedBindingCount: number } + +/** One SQL snapshot: scope/eligibility/paging precede user JSON and binding hydration. + * Mixed users with a binding outside the grant are excluded as a whole. */ +export async function readScopedTrafficUsers(input: ScopedTrafficUserQuery): Promise<{ users: ScopedTrafficUser[]; total: number }> { + if (input.allowedServerIds.some(id => !Number.isSafeInteger(id) || id < 1) || input.selectedServerIds.some(id => !input.allowedServerIds.includes(id)) || + !Number.isSafeInteger(input.offset) || input.offset < 0 || input.offset > 100000 || !Number.isSafeInteger(input.limit) || input.limit < 1 || input.limit > 50) throw new Error("Некорректная область истории пользователей") + const { rows: [result] } = await pool.query(` + WITH eligible AS ( + SELECT u.id,u.login,u.name,u.role,u.active, + (SELECT count(*)::int FROM user_interface_bindings b WHERE b.user_id=u.id) AS binding_count, + (SELECT count(*)::int FROM user_interface_bindings b WHERE b.user_id=u.id AND b.server_id=ANY($2::bigint[])) AS selected_binding_count + FROM app_users u + WHERE ($3::text IS NULL OR u.id=$3) + AND NOT EXISTS(SELECT 1 FROM user_interface_bindings b WHERE b.user_id=u.id AND NOT(b.server_id=ANY($1::bigint[]))) + AND (EXISTS(SELECT 1 FROM user_interface_bindings b WHERE b.user_id=u.id AND b.server_id=ANY($2::bigint[])) + OR ($6::boolean AND NOT EXISTS(SELECT 1 FROM user_interface_bindings b WHERE b.user_id=u.id))) + ), page AS (SELECT * FROM eligible ORDER BY id OFFSET $4 LIMIT $5) + SELECT (SELECT count(*)::int FROM eligible) AS total, + COALESCE((SELECT jsonb_agg(jsonb_build_object( + 'id',p.id,'login',p.login,'name',p.name,'role',p.role,'active',p.active, + 'bindingCount',p.binding_count,'selectedBindingCount',p.selected_binding_count, + 'bindings',COALESCE((SELECT jsonb_agg(jsonb_build_object( + 'id',b.id,'userId',b.user_id,'serverId',b.server_id,'interfaceName',b.interface_name,'interfaceType',b.interface_type, + 'peerPublicKey',b.peer_public_key,'peerName','','comment','','serverName','','serverSite','','serverCountry','', + 'createdAt',b.created_at,'updatedAt',b.updated_at) ORDER BY b.id) + FROM (SELECT * FROM user_interface_bindings b WHERE b.user_id=p.id AND b.server_id=ANY($2::bigint[]) ORDER BY b.id LIMIT 201) b),'[]'::jsonb) + ) ORDER BY p.id) FROM page p),'[]'::jsonb) AS users + `, [input.allowedServerIds, input.selectedServerIds, input.userId ?? null, input.offset, input.limit, input.includeUnbound ?? false]) + if (!result || !Number.isSafeInteger(result.total) || result.total < 0 || !Array.isArray(result.users)) throw new Error("Не подтверждён каталог истории пользователей") + const users = result.users as ScopedTrafficUser[] + if (users.some(user => !Number.isSafeInteger(user.bindingCount) || !Number.isSafeInteger(user.selectedBindingCount) || user.selectedBindingCount > 200 || user.selectedBindingCount < 0 || user.bindingCount < user.selectedBindingCount || !Array.isArray(user.bindings) || user.bindings.length !== user.selectedBindingCount)) throw new Error("История пользователя превышает 200 выбранных привязок") + return { users, total: result.total } +} diff --git a/backend/src/services/traffic-users-cache.test.ts b/backend/src/services/traffic-users-cache.test.ts index cc327f4e..ee38fc8c 100644 --- a/backend/src/services/traffic-users-cache.test.ts +++ b/backend/src/services/traffic-users-cache.test.ts @@ -21,7 +21,7 @@ const context=vm.createContext({Date,Map,Promise,buildTrafficFromSamples,mergeBu return [0,30_000].map((offset,i)=>({interfaceName:'ether1',sampledAt:new Date(at+offset).toISOString(),rxBytes:i*1024,txBytes:0,rxBps:0,txBps:0,running:true,disabled:false}))}, }) vm.runInContext(ts.transpileModule(functions,{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.CommonJS}}).outputText.replace(/^.*exports.*$/gm,''),context) -const build=context.buildUserTrafficList as (start:number,end:number)=>Promise> +const build=context.buildUserTrafficList as (start:number,end:number,options?:{users:unknown[]})=>Promise> const rows=await build(at,at+30_000) assert.equal(calls,1,'100 concurrent users share one server history read') assert.equal(rows.length,100);assert(rows.every(r=>r.rxTotal===1024/1024**3&&r.interfaces.length===1)) @@ -30,4 +30,6 @@ fail=true;await assert.rejects(build(at,at+60_000),/fixture read failed/);assert fail=false;await build(at,at+60_000);assert.equal(calls,4,'a failed request cannot poison later reads') users[99].bindings[0].serverId=2 await build(at,at+60_000);assert.equal(calls,6,'different server IDs never share a cached history') +assert.equal((await build(at,at+60_000,{users:[]})).length,0);assert.equal(calls,6,'empty injected catalog never falls back to all users') +assert.equal((await build(at,at+60_000,{users:[users[0]]})).length,1);assert.equal(calls,7,'injected user page still uses the common calculation and one request-local history read') console.log('user traffic: 100 concurrent consumers/one server read/request scope/failure/recovery passed') diff --git a/backend/src/services/traffic-users.ts b/backend/src/services/traffic-users.ts index a9f9bc34..a2ed1791 100644 --- a/backend/src/services/traffic-users.ts +++ b/backend/src/services/traffic-users.ts @@ -1,7 +1,8 @@ import { db } from "../db/index.js" +import { inArray } from "drizzle-orm" import { servers } from "../db/schema.js" import { listUsers } from "../modules/users/service/users-service.js" -import { readServerSamplesInRange } from "./traffic-collector.js" +import { readServerSamplesInRange, type TrafficHistoryReadOptions } from "./traffic-collector.js" import { buildTrafficFromSamples, mergeBuiltTraffic, @@ -75,10 +76,12 @@ function seriesFromBuilt(built: BuiltTrafficSeries) { } } -export async function buildUserTrafficList(rangeStartMs: number, rangeEndMs: number): Promise { +export type TrafficUserInput = Pick>[number], "id" | "login" | "name" | "role" | "active" | "bindings"> +export interface UserTrafficReadOptions extends TrafficHistoryReadOptions { users?: TrafficUserInput[]; serverIds?: readonly number[] } +export async function buildUserTrafficList(rangeStartMs: number, rangeEndMs: number, options: UserTrafficReadOptions = {}): Promise { const sinceIso = new Date(rangeStartMs).toISOString() - const users = await listUsers() - const [context, allServers] = await Promise.all([readTrafficCollectionContext(), db.select().from(servers)]) + 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 catalog = new Map(allServers.map(s => [s.id, s])) const sampleCache = new Map>() @@ -89,7 +92,8 @@ export async function buildUserTrafficList(rangeStartMs: number, rangeEndMs: num for (const b of user.bindings) { let pending = sampleCache.get(b.serverId) if (!pending) { - pending = readServerSamplesInRange(b.serverId, sinceIso) + 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() }) sampleCache.set(b.serverId, pending) } const rows = await pending diff --git a/backend/src/services/traffic-volume-sql.test.ts b/backend/src/services/traffic-volume-sql.test.ts index ab986f8f..25c44928 100644 --- a/backend/src/services/traffic-volume-sql.test.ts +++ b/backend/src/services/traffic-volume-sql.test.ts @@ -58,7 +58,12 @@ else { await pool.query("DELETE FROM traffic_samples WHERE server_id=$1",[serverId]); for(const[name,offset,bytes]of[["ether1",-90_000,0],["ether1",-60_000,0],["ether1",-30_000,1024**2],["new",-30_000,0],["ether1",0,1024**2],["new",0,0]] as const)await put(name,offset,bytes); await pool.query("UPDATE traffic_samples SET flags=61,native_counter_identity=interface_name WHERE server_id=$1",[serverId]);await pool.query("UPDATE traffic_samples SET flags=45 WHERE server_id=$1 AND interface_name='new' AND sampled_at=$2",[serverId,new Date(Date.parse(at)-30_000).toISOString()]); - s=(await get(`/traffic/servers/${serverId}?range=1h`)).server;assert.equal(s.observations.rxNow,true);assert.equal(s.rxPeak,0);assert.equal(s.txPeak,0);assert.equal(s.rxTotal,1/1024);assert(s.observations.rxSeries.some(Boolean));cases++; + s=(await get(`/traffic/servers/${serverId}?range=1h`)).server;assert.equal(s.observations.rxNow,true);assert.equal(s.rxPeak,0);assert.equal(s.txPeak,0);assert.equal(s.rxTotal,1/1024); + // A one-minute bucket may overlap the incomplete membership wave even when + // the latest 30-second zero interval is measured. Check its history on a + // five-minute range, where that complete interval occupies distinct buckets. + const shortMembership=(await get(`/traffic/servers/${serverId}?range=5m`)).server; + assert.equal(shortMembership.rxTotal,s.rxTotal);assert.equal(shortMembership.observations.rxNow,true);assert(shortMembership.observations.rxSeries.some(Boolean));cases++; await pool.query("DELETE FROM user_interface_bindings WHERE user_id=$1",[userId]) for(const name of ['peakA','peakB'])await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type) VALUES($1,$2,$3,$4,'ether')",[prefix+name,userId,serverId,name]) for(const mode of ['separate','coincident','jitter']){ diff --git a/backend/src/services/uptime-collector.ts b/backend/src/services/uptime-collector.ts index 03119225..98b5df92 100644 --- a/backend/src/services/uptime-collector.ts +++ b/backend/src/services/uptime-collector.ts @@ -1,6 +1,9 @@ +import { withPartitionWrite, isPartitionWriteOutcomeUnknown, PartitionWriteError, type PartitionWriteOptions } from "./partition-write-transaction.js" import { replacePingProbes } from "./ping-probe-replacement.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { withApplicationCollectorLock } from "./config-mutation-lock.js" import type { PingProbeWrite } from "@mmapp/contracts" -import { and, asc, eq, gte, inArray, max, sql } from "drizzle-orm" +import { and, asc, eq, gte, lte, inArray, max, sql } from "drizzle-orm" import { listEnabledMonitoringServers } from "./monitoring-server-catalog.js" import { db, dbAll } from "../db/index.js" import { @@ -125,7 +128,11 @@ async function lastProbeSampleTimes(): Promise> { } /** Сбор метрик ресурсов (CPU/RAM/…) по всем включённым серверам. */ -export async function collectResourceSamplesOnce(): Promise { +export async function collectResourceSamplesOnce(partitionWriteOptions:PartitionWriteOptions={}): Promise { + return withApplicationCollectorLock(() => collectResourceSamplesUnderApplicationLock(partitionWriteOptions)) +} + +async function collectResourceSamplesUnderApplicationLock(partitionWriteOptions:PartitionWriteOptions={}): Promise { const sampledAt = new Date().toISOString() if (collectingResources) { return { @@ -139,7 +146,6 @@ export async function collectResourceSamplesOnce(): Promise writerDb.insert(uptimeResourceSamples).values({ serverId: s.id, sampledAt: now, status: "online", @@ -171,7 +178,7 @@ export async function collectResourceSamplesOnce(): Promise 0 ? Math.round((totalMem - freeMem) / (1024 * 1024)) : 0 const memTotalMb = totalMem > 0 ? Math.round(totalMem / (1024 * 1024)) : 0 snapshot.servers.push({ @@ -190,8 +197,9 @@ export async function collectResourceSamplesOnce(): Promise writerDb.insert(uptimeResourceSamples).values({ serverId: s.id, sampledAt: now, status: "offline", @@ -201,7 +209,7 @@ export async function collectResourceSamplesOnce(): Promise { +export async function collectPingProbesOnce(partitionWriteOptions:PartitionWriteOptions={}): Promise { + return withApplicationCollectorLock(() => collectPingProbesUnderApplicationLock(partitionWriteOptions)) +} + +async function collectPingProbesUnderApplicationLock(partitionWriteOptions:PartitionWriteOptions={}): Promise { const sampledAt = new Date().toISOString() if (collectingPing) { return { @@ -247,10 +260,6 @@ export async function collectPingProbesOnce(): Promise { collectingPing = true const started = Date.now() const now = sampledAt - const settings = await getSettings() - const defaultProbeSec = Math.max(5, settings.probeIntervalSec ?? settings.intervalSec) - const lastByProbe = await lastProbeSampleTimes() - const nowMs = Date.now() const snapshot: PingRunSnapshot = { v: SCHEDULER_RUN_SNAPSHOT_VERSION, job: "uptime_ping", @@ -260,6 +269,10 @@ export async function collectPingProbesOnce(): Promise { } try { + const settings = await getSettings() + const defaultProbeSec = Math.max(5, settings.probeIntervalSec ?? settings.intervalSec) + const lastByProbe = await lastProbeSampleTimes() + const nowMs = Date.now() const rosPollServers = await serversForUptimeRosPoll() const probes = await db.select().from(uptimeProbes).where(eq(uptimeProbes.enabled, true)).orderBy(asc(uptimeProbes.sortOrder)) @@ -282,13 +295,13 @@ export async function collectPingProbesOnce(): Promise { const results = await client.ping(p.target, 4, p.srcInterface || undefined) const parsed = parsePing(results) if (!parsed.measured) throw new Error("RouterOS не вернул подтверждённые результаты Ping") - await db.insert(uptimeProbeSamples).values({ + await withPartitionWrite(async ({ db: writerDb }) => writerDb.insert(uptimeProbeSamples).values({ probeId: p.id, sampledAt: now, rttMs: parsed.avgRtt, lossPct: parsed.loss, status: parsed.status, - }) + }),partitionWriteOptions) snapshot.probes.push({ probeId: p.id, name: p.name, @@ -302,7 +315,8 @@ export async function collectPingProbesOnce(): Promise { status: parsed.status, }) } catch (err) { - await db.insert(uptimeProbeSamples).values({ + if (err instanceof PartitionWriteError) throw err + await withPartitionWrite(async ({ db: writerDb }) => writerDb.insert(uptimeProbeSamples).values({ probeId: p.id, sampledAt: now, rttMs: null, @@ -310,7 +324,7 @@ export async function collectPingProbesOnce(): Promise { status: "down", measurementKnown: false, observationError: err instanceof Error ? err.message : String(err), - }) + }),partitionWriteOptions) snapshot.probes.push({ probeId: p.id, name: p.name, @@ -334,6 +348,7 @@ export async function collectPingProbesOnce(): Promise { updatedAt: now, }).where(eq(uptimeSettings.id, 1)) } catch (e) { + if (isPartitionWriteOutcomeUnknown(e)) throw e snapshot.fatalError = e instanceof Error ? e.message : String(e) await db.update(uptimeSettings).set({ lastCollectedAt: now, @@ -353,7 +368,7 @@ const MAX_PROBE_IDS_PER_GROUP_COLLECT = 64 * Немедленный ping по указанным пробам (без учёта interval_sec) — запись в uptime_probe_samples. * Используется кнопкой «Обновить» у группы «сервер + назначение» на вкладке проб. */ -export async function collectPingForProbeIds(probeIds: string[]): Promise<{ polled: number }> { +export async function collectPingForProbeIds(probeIds: string[],partitionWriteOptions:PartitionWriteOptions={}): Promise<{ polled: number }> { const uniqueIds = [...new Set(probeIds.filter((id) => typeof id === "string" && id.trim() !== ""))].slice( 0, MAX_PROBE_IDS_PER_GROUP_COLLECT, @@ -368,10 +383,10 @@ export async function collectPingForProbeIds(probeIds: string[]): Promise<{ poll collectingPing = true const now = new Date().toISOString() - const settings = await getSettings() let polled = 0 try { + await getSettings() const rosPollServers = await serversForUptimeRosPoll() const rows = await db.select().from(uptimeProbes).where(inArray(uptimeProbes.id, uniqueIds)) @@ -383,15 +398,16 @@ export async function collectPingForProbeIds(probeIds: string[]): Promise<{ poll const results = await client.ping(p.target, 4, p.srcInterface || undefined) const parsed = parsePing(results) if (!parsed.measured) throw new Error("RouterOS не вернул подтверждённые результаты Ping") - await db.insert(uptimeProbeSamples).values({ + await withPartitionWrite(async ({ db: writerDb }) => writerDb.insert(uptimeProbeSamples).values({ probeId: p.id, sampledAt: now, rttMs: parsed.avgRtt, lossPct: parsed.loss, status: parsed.status, - }) + }),partitionWriteOptions) } catch (err) { - await db.insert(uptimeProbeSamples).values({ + if (err instanceof PartitionWriteError) throw err + await withPartitionWrite(async ({ db: writerDb }) => writerDb.insert(uptimeProbeSamples).values({ probeId: p.id, sampledAt: now, rttMs: null, @@ -399,7 +415,7 @@ export async function collectPingForProbeIds(probeIds: string[]): Promise<{ poll status: "down", measurementKnown: false, observationError: err instanceof Error ? err.message : String(err), - }) + }),partitionWriteOptions) } polled += 1 } @@ -411,9 +427,10 @@ export async function collectPingForProbeIds(probeIds: string[]): Promise<{ poll } /** Совместимость: один проход ресурсы + ping (для ручного вызова без планировщика). */ -export async function collectUptimeOnce(): Promise { - await collectResourceSamplesOnce() - await collectPingProbesOnce() +export async function collectUptimeOnce(): Promise<{resources:ResourcesRunSnapshot;ping:PingRunSnapshot}> { + const resources=await collectResourceSamplesOnce() + const ping=await collectPingProbesOnce() + return {resources,ping} } /** no-op: таймеры управляются планировщиком */ @@ -425,7 +442,8 @@ export async function readUptimeSettings() { return await getSettings() } -export async function updateUptimeSettings(patch: { +export function updateUptimeSettings(patch: Parameters[0]) { return executeGuardedApplicationWrite({ kind: "uptime-settings", id: "1" }, "all", () => updateUptimeSettingsImpl(patch)) } +async function updateUptimeSettingsImpl(patch: { /** Устаревший: если передан без per-job флагов — выставляет все три job-флага. */ enabled?: boolean resourcesEnabled?: boolean @@ -471,11 +489,19 @@ export async function updateUptimeSettings(patch: { return await readUptimeSettings() } -export async function readProbeRows() { - return await db.select().from(uptimeProbes).orderBy(asc(uptimeProbes.sortOrder)) +export async function readProbeRows(serverIds?: readonly number[], maxRows?: number) { + if (serverIds?.length === 0) return [] + let query = db.select().from(uptimeProbes).where(serverIds ? inArray(uptimeProbes.srcServerId, [...serverIds]) : undefined).orderBy(asc(uptimeProbes.sortOrder), asc(uptimeProbes.id)).$dynamic() + if (maxRows !== undefined) query = query.limit(maxRows + 1) + const rows = await query + if (maxRows !== undefined && rows.length > maxRows) throw new Error("Uptime probe catalog exceeds read limit") + return rows } -export async function updateProbeShowOnDashboard(probeId: string, showOnDashboard: boolean): Promise { +export function updateProbeShowOnDashboard(probeId: string, showOnDashboard: boolean): Promise { + return executeGuardedApplicationWrite({ kind: "ping-probe", id: probeId }, async () => (await db.select({ id: uptimeProbes.srcServerId }).from(uptimeProbes).where(eq(uptimeProbes.id, probeId))).map(row => row.id), () => updateProbeShowOnDashboardImpl(probeId, showOnDashboard)) +} +async function updateProbeShowOnDashboardImpl(probeId: string, showOnDashboard: boolean): Promise { const now = new Date().toISOString() const r = await db.update(uptimeProbes) .set({ showOnDashboard, updatedAt: now }) @@ -484,8 +510,11 @@ export async function updateProbeShowOnDashboard(probeId: string, showOnDashboar return r.length > 0 } -export async function replaceProbes(rows: PingProbeWrite[]) { - await replacePingProbes({ transaction: run => db.transaction(async tx => { +export function replaceProbes(rows: PingProbeWrite[],partitionWriteOptions:PartitionWriteOptions={}) { + return executeGuardedApplicationWrite({ kind: "ping-probe-catalog", id: "all" }, "all", () => replaceProbesImpl(rows,partitionWriteOptions), async () => [...new Set([...(await readProbeRows()).map(row => row.id), ...rows.map(row => row.id)])].map(id => ({ kind: "ping-probe", id }))) +} +async function replaceProbesImpl(rows: PingProbeWrite[],partitionWriteOptions:PartitionWriteOptions={}) { + await replacePingProbes({ transaction: run => withPartitionWrite(async ({ db: tx }) => { await tx.execute(sql`select pg_advisory_xact_lock(hashtext('uptime-ping-probes'))`) await run({ listIds: async () => (await tx.select({ id: uptimeProbes.id }).from(uptimeProbes)).map(row => row.id), @@ -496,13 +525,17 @@ export async function replaceProbes(rows: PingProbeWrite[]) { .onConflictDoUpdate({ target: uptimeProbes.id, set: patch }) }, }) - }) }, rows) + },partitionWriteOptions) }, rows) } -export async function readProbeSamplesSince(sinceIso: string) { - const rows = await db.select().from(uptimeProbeSamples) - .where(gte(uptimeProbeSamples.sampledAt, sinceIso)) - .orderBy(asc(uptimeProbeSamples.sampledAt)) +export async function readProbeSamplesSince(sinceIso: string, options: { probeIds?: readonly string[]; untilIso?: string; maxRows?: number } = {}) { + if (options.probeIds?.length === 0) return [] + let query = db.select().from(uptimeProbeSamples) + .where(and(gte(uptimeProbeSamples.sampledAt, sinceIso), options.untilIso ? lte(uptimeProbeSamples.sampledAt, options.untilIso) : undefined, options.probeIds ? inArray(uptimeProbeSamples.probeId, [...options.probeIds]) : undefined)) + .orderBy(asc(uptimeProbeSamples.sampledAt), asc(uptimeProbeSamples.probeId)).$dynamic() + 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("Uptime probe samples exceed read limit") if (!rows.length) return rows // Old scheduled failures can be classified only when their exact snapshot provenance still exists. const failures = await dbAll<{probeId: string | null; sampledAt: string | null; error: string | null}>(` @@ -510,7 +543,11 @@ export async function readProbeSamplesSince(sinceIso: string) { FROM scheduler_runs CROSS JOIN LATERAL jsonb_array_elements( CASE WHEN jsonb_typeof(result_json->'probes')='array' THEN result_json->'probes' ELSE '[]'::jsonb END) probe WHERE job_key='uptime_ping' AND status IN ('ok','error') AND finished_at >= ? - AND result_json->'v'='1'::jsonb AND result_json->>'job'='uptime_ping' AND probe->'ok'='false'::jsonb`, [sinceIso]) + AND result_json->'v'='1'::jsonb AND result_json->>'job'='uptime_ping' AND probe->'ok'='false'::jsonb + ${options.probeIds ? `AND probe->>'probeId' IN (${options.probeIds.map(() => "?").join(",")})` : ""} + ${options.untilIso ? "AND finished_at <= ?" : ""} + ${options.maxRows !== undefined ? "LIMIT ?" : ""}`, [sinceIso, ...(options.probeIds ?? []), ...(options.untilIso ? [options.untilIso] : []), ...(options.maxRows !== undefined ? [options.maxRows + 1] : [])]) + if (options.maxRows !== undefined && failures.length > options.maxRows) throw new Error("Uptime probe provenance exceeds read limit") const bySample = new Map() for (const failure of failures) { if (!failure.probeId || !failure.sampledAt || !Number.isFinite(Date.parse(failure.sampledAt))) continue @@ -522,11 +559,16 @@ export async function readProbeSamplesSince(sinceIso: string) { }) } -export async function readResourceSamplesSince(sinceIso: string, serverId: number) { - return await db.select().from(uptimeResourceSamples) +export async function readResourceSamplesSince(sinceIso: string, serverId: number, options: { untilIso?: string; maxRows?: number } = {}) { + let query = db.select().from(uptimeResourceSamples) .where(and( eq(uptimeResourceSamples.serverId, serverId), gte(uptimeResourceSamples.sampledAt, sinceIso), + options.untilIso ? lte(uptimeResourceSamples.sampledAt, options.untilIso) : undefined, )) - .orderBy(asc(uptimeResourceSamples.sampledAt)) + .orderBy(asc(uptimeResourceSamples.sampledAt)).$dynamic() + 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("Uptime resource samples exceed read limit") + return rows } diff --git a/backend/src/services/uptime-overview-read.ts b/backend/src/services/uptime-overview-read.ts new file mode 100644 index 00000000..e4ea6d5f --- /dev/null +++ b/backend/src/services/uptime-overview-read.ts @@ -0,0 +1,69 @@ +import { and, asc, desc, eq, lte } from "drizzle-orm" +import { db } from "../db/index.js" +import { servers, serverSnapshots, type UptimeResourceSampleRow } from "../db/schema.js" +import { readProbeRows, readProbeSamplesSince, readResourceSamplesSince, readUptimeSettings } from "./uptime-collector.js" +import { buildUptimeProbeObservations } from "./uptime-probe-observations.js" + +export function pickResourceDisplayRow(rows: readonly UptimeResourceSampleRow[], maxGapMs: number) { + const last = rows.at(-1) + if (!last) return { row: undefined, hasData: false } + const valid = (row: UptimeResourceSampleRow) => row.status === "online" && row.totalMemory > 0 + if (valid(last)) return { row: last, hasData: true } + const fallback = [...rows].reverse().find(valid) + return fallback && Date.parse(last.sampledAt) - Date.parse(fallback.sampledAt) <= maxGapMs ? { row: fallback, hasData: true } : { row: undefined, hasData: false } +} +function toSeries(values: number[], target = 40) { + if (!values.length) return Array(target).fill(0) + return values.length >= target ? values.slice(-target) : [...Array(target - values.length).fill(values[0]), ...values] +} +export type UptimeOverviewOptions = { startAt: string; endAt: string; maxRows?: number; maxProbes?: number; probeOffset?: number; probeLimit?: number } + +/** Shared REST/MCP stored view. SQL scope is applied before samples and provenance. + * Resource/probe evidence is independent of the legacy UI's padded/zero defaults. */ +export async function readUptimeOverview(serverIds: readonly number[] | undefined, options: UptimeOverviewOptions) { + const selected = serverIds ?? (await db.select({ id: servers.id }).from(servers).orderBy(asc(servers.id))).map(row => row.id) + const probesBefore = await readProbeRows(selected, options.maxProbes) + const page = probesBefore.slice(options.probeOffset ?? 0, options.probeLimit === undefined ? undefined : (options.probeOffset ?? 0) + options.probeLimit) + const samples = await readProbeSamplesSince(options.startAt, { probeIds: page.map(row => row.id), untilIso: options.endAt, maxRows: options.maxRows }) + const settings = await readUptimeSettings(), intervalSec = Math.max(15, settings.intervalSec ?? 300) + const maxGapMs = Math.min(7200000, Math.max(600000, intervalSec * 6000)) + const probes = page.map(probe => { + const rows = samples.filter(row => row.probeId === probe.id) + const observations = buildUptimeProbeObservations(rows), last = observations.samples.at(-1) + return { id: probe.id, srcServerId: String(probe.srcServerId), srcInterface: probe.srcInterface || "", name: probe.name, target: probe.target, filter: probe.probeFilter || "—", + rtt: last?.rtt ?? null, loss: last?.loss ?? null, status: last?.status ?? "unknown", series: observations.samples.map(row => row.rtt), enabled: probe.enabled, showOnDashboard: probe.showOnDashboard, observations } + }) + const resourceResults = await Promise.all(selected.map(async serverId => { + const [rows, snapshots] = await Promise.all([readResourceSamplesSince(options.startAt, serverId, { untilIso: options.endAt, maxRows: options.maxRows }), + db.select({ boardName: serverSnapshots.boardName }).from(serverSnapshots).where(and(eq(serverSnapshots.serverId, serverId), lte(serverSnapshots.polledAt, options.endAt))).orderBy(desc(serverSnapshots.polledAt)).limit(1)]) + const { row: pick, hasData } = pickResourceDisplayRow(rows, maxGapMs) + return { presentation: { serverId: String(serverId), hasData, cpu: pick?.cpuLoad ?? 0, cpuHistory: toSeries(rows.map(row => row.cpuLoad)), + ramUsed: Math.max(0, ((pick?.totalMemory ?? 0) - (pick?.freeMemory ?? 0)) / 1048576), ramTotal: Math.max(0, (pick?.totalMemory ?? 0) / 1048576), + hddUsed: Math.max(0, ((pick?.totalHddSpace ?? 0) - (pick?.freeHddSpace ?? 0)) / 1048576), hddTotal: Math.max(0, (pick?.totalHddSpace ?? 0) / 1048576), + uptimeSeconds: pick?.uptimeSeconds ?? 0, boardName: snapshots[0]?.boardName || "RouterBOARD", temp: undefined as number | undefined }, + evidence: { serverId, pick: pick ?? null, last: rows.at(-1) ?? null, sampleCount: rows.length, cpuHistory: rows.slice(-40).map(row => ({ sampledAt: row.sampledAt, cpuLoad: row.cpuLoad, status: row.status })) } } + })) + const probesAfter = await readProbeRows(selected, options.maxProbes) + if (JSON.stringify(probesBefore) !== JSON.stringify(probesAfter)) throw new Error("Uptime probe configuration changed during read") + return { probes, resources: resourceResults.map(row => row.presentation), resourceEvidence: resourceResults.map(row => row.evidence), probeEvidence: samples, + totalProbes: probesBefore.length, probeConfiguration: probesBefore, rangeStartAt: options.startAt, rangeEndAt: options.endAt } +} + +const finite = (value: number | undefined | null, minimum = 0, maximum = Number.MAX_SAFE_INTEGER) => typeof value === "number" && Number.isFinite(value) && value >= minimum && value <= maximum ? value : null +export function projectUptimeOverview(view: Awaited>, sampleLimit: number) { + return { probes: view.probes.map(probe => { + const rows = view.probeEvidence.filter(row => row.probeId === probe.id), last = rows.at(-1) + const samples = rows.slice(-sampleLimit).map(row => ({ sampledAt: row.sampledAt, measurementKnown: row.measurementKnown === true, + rttMs: row.measurementKnown === true ? finite(row.rttMs) : null, lossPct: row.measurementKnown === true ? finite(row.lossPct, 0, 100) : null, + status: row.measurementKnown === true ? row.status : "unknown" })) + const known = last?.measurementKnown === true + return { id: probe.id, serverId: Number(probe.srcServerId), srcInterface: probe.srcInterface, name: probe.name, target: probe.target, enabled: probe.enabled, showOnDashboard: probe.showOnDashboard, + latestSampledAt: last?.sampledAt ?? null, ageSec: last ? (Date.parse(view.rangeEndAt) - Date.parse(last.sampledAt)) / 1000 : null, measurementKnown: known, rttMs: known ? finite(last.rttMs) : null, lossPct: known ? finite(last.lossPct, 0, 100) : null, + status: known ? last.status : "unknown", samples, totalSamples: rows.length, samplesTruncated: rows.length > samples.length } + }), resources: view.resourceEvidence.map(({ serverId, pick, last, sampleCount, cpuHistory }) => ({ serverId, sampledAt: pick?.sampledAt ?? null, latestAttemptAt: last?.sampledAt ?? null, + latestAttemptStatus: last?.status ?? "unknown", ageSec: pick ? (Date.parse(view.rangeEndAt) - Date.parse(pick.sampledAt)) / 1000 : null, usingHistoricalFallback: !!pick && pick.sampledAt !== last?.sampledAt, sampleCount, + cpuLoadPct: pick ? finite(pick.cpuLoad, 0, 100) : null, totalMemoryBytes: finite(pick?.totalMemory), freeMemoryBytes: pick && pick.freeMemory <= pick.totalMemory ? finite(pick.freeMemory) : null, + totalHddBytes: finite(pick?.totalHddSpace), freeHddBytes: pick && pick.freeHddSpace <= pick.totalHddSpace ? finite(pick.freeHddSpace) : null, uptimeSeconds: finite(pick?.uptimeSeconds), + cpuHistory: cpuHistory.map(row => ({ sampledAt: row.sampledAt, cpuLoadPct: row.status === "online" ? finite(row.cpuLoad, 0, 100) : null })) })), + totalProbes: view.totalProbes, from: view.rangeStartAt, to: view.rangeEndAt, source: "stored-uptime-samples", measurementProvenance: "stored-known-flag-with-retained-scheduler-failure-overrides", nativeMeasurementVerified: false, live: false, currentStateVerified: false, forwardingVerified: false } +} diff --git a/backend/src/services/uptime-speed-service.test.ts b/backend/src/services/uptime-speed-service.test.ts new file mode 100644 index 00000000..bc900e9f --- /dev/null +++ b/backend/src/services/uptime-speed-service.test.ts @@ -0,0 +1,58 @@ +import assert from "node:assert/strict" +import { mock, test } from "node:test" +import { servers, uptimeSpeedProbes } from "../db/schema.js" +import { SpeedRunPersistenceUnknown, SpeedTestServerNotFound } from "./speed-run-persistence.js" + +// Replace only storage and RouterOS; exercise the real service and node locks. +const endpoints = [ + { id: 2, enabled: true, host: "192.0.2.2", username: "destination", password: "fixture", name: "Destination" }, + { id: 1, enabled: true, host: "192.0.2.1", username: "source", password: "fixture", name: "Source" }, +] +const probe = { id: "probe", srcServerId: 1, dstServerId: 2, srcInterface: "", dstInterface: "", protocol: "tcp", direction: "both", durationSec: 10, enabled: true } +const reads: unknown[] = [], writes: Record[] = [], native: Record[] = [] +let missing = false, commitFails = false, transactions = 0, updates = 0 +const tx = { + update: () => ({ set: () => ({ where: () => ({ returning: async () => { updates++; return [{ id: probe.id }] } }) }) }), + insert: () => ({ values: async (row: Record) => { writes.push(row) } }), +} +mock.module("../db/index.js", { namedExports: { db: { + select: () => ({ from: (table: unknown) => { + reads.push(table) + const rows = table === servers ? missing ? endpoints.slice(0, 1) : endpoints : [probe] + const selected = Object.assign(Promise.resolve(rows), { limit: async () => rows }) + return { where: () => selected, orderBy: async () => rows } + } }), + transaction: async (work: (value: typeof tx) => Promise) => { transactions++; await work(tx); if (commitFails) throw Error("commit unavailable") }, +} } }) +mock.module("./mikrotik.js", { namedExports: { MikrotikClient: { fromServer: (server: { id: number }) => ({ + bandwidthTest: async (body: Record) => { native.push({ serverId: server.id, ...body }); return [{ status: "done testing", "tx-total-average": "1000000", "rx-total-average": "0" }] }, + ping: async () => [], +}) } } }) +mock.module("./uptime-collector.js", { namedExports: { parsePing: () => ({ measured: true, avgRtt: 2, loss: 0 }) } }) +const { runBandwidthSpeedTest, runScheduledSpeedProbesOnce } = await import("./uptime-speed-service.js") +const command = { srcServerId: 1, dstServerId: 2 } + +test("one endpoint read, optional probe read, native defaults and existing history transaction", async () => { + await assert.rejects(runBandwidthSpeedTest({...command,maxMbps:0})) + assert.equal(reads.length,0);assert.equal(native.length,0) + const result = await runBandwidthSpeedTest({ ...command, runId: "ad-hoc" }) + assert.deepEqual(reads, [servers]); assert.equal(transactions, 1); assert.equal(updates, 0) + assert.deepEqual(native[0], { serverId: 1, address: "192.0.2.2", user: "destination", password: "fixture", protocol: "tcp", direction: "both", durationSec: 10 }) + assert.equal(result.txAvgMbps, 1); assert.equal(result.rxAvgMbps, 0); assert.equal(writes[0].probeId, null) + reads.length = 0 + await runBandwidthSpeedTest({ ...command, probeId: "probe", runId: "associated",maxMbps:10 }) + assert.deepEqual(reads, [servers, uptimeSpeedProbes]); assert.equal(updates, 1); assert.equal(writes.at(-1)?.probeId, "probe") + assert.equal(native.at(-1)?.maxMbps,10) + missing = true + const before = native.length + await assert.rejects(runBandwidthSpeedTest(command), SpeedTestServerNotFound) + assert.equal(native.length, before); missing = false + reads.length = 0 + const scheduled = await runScheduledSpeedProbesOnce() + assert.deepEqual(reads, [uptimeSpeedProbes, servers, servers, uptimeSpeedProbes]) + assert.equal(scheduled.runs[0].srcServerName, "Source"); assert.equal(scheduled.runs[0].dstServerName, "Destination") + commitFails = true + const beforeUnknown = native.length + await assert.rejects(runBandwidthSpeedTest({ ...command, runId: "unknown" }), (error: unknown) => error instanceof SpeedRunPersistenceUnknown && error.runId === "unknown") + assert.equal(native.length, beforeUnknown + 1) +}) diff --git a/backend/src/services/uptime-speed-service.ts b/backend/src/services/uptime-speed-service.ts index 7725ea11..fa25627a 100644 --- a/backend/src/services/uptime-speed-service.ts +++ b/backend/src/services/uptime-speed-service.ts @@ -1,4 +1,4 @@ -import { and, asc, eq } from "drizzle-orm" +import { and, asc, eq, inArray } from "drizzle-orm" import { db } from "../db/index.js" import { servers, uptimeSpeedProbes, uptimeSpeedTestRuns } from "../db/schema.js" import type { SpeedRunSnapshot, SpeedScheduledRunSnapshot } from "../types/scheduler-run-snapshot.js" @@ -8,7 +8,7 @@ import { withBtestNodeLocks } from "./btest-node-locks.js" import { MikrotikClient } from "./mikrotik.js" import { parsePing } from "./uptime-collector.js" import { assertBandwidthTestCompleted } from "./bandwidth-test-status.js" -import { assertSpeedProbeContext, persistSpeedRun, SpeedRunPersistenceUnknown, type SpeedProbeContext } from "./speed-run-persistence.js" +import { assertSpeedProbeContext, persistSpeedRun, SpeedRunPersistenceUnknown, SpeedTestServerNotFound, type SpeedProbeContext } from "./speed-run-persistence.js" import { readBandwidthTestRates } from "./bandwidth-test-rates.js" import { speedRunErrorMessage } from "./speed-run-error-message.js" @@ -47,6 +47,8 @@ export interface SpeedTestRunParams { protocol?: "tcp" | "udp" direction?: "transmit" | "receive" | "both" durationSec?: number + /** Optional RouterOS per-direction ceiling, in decimal Mbit/s. */ + maxMbps?: number } export interface SpeedTestRunResult { @@ -76,12 +78,14 @@ export interface SpeedTestRunResult { /** Полный цикл bandwidth-test + after-ping + запись в БД. Вызывать внутри withBtestNodeLocks при необходимости — здесь lock уже снаружи. */ export async function executeBandwidthSpeedTestInner(params: SpeedTestRunParams): Promise { + if(params.maxMbps!==undefined&&(!Number.isSafeInteger(params.maxMbps)||params.maxMbps<1||params.maxMbps>1000))throw new Error("Лимит скорости должен быть целым числом от 1 до 1000 Мбит/с") const probeId = String(params.probeId ?? "").trim() const srcId = params.srcServerId const dstId = params.dstServerId - const src = (await db.select().from(servers).where(eq(servers.id, srcId)).limit(1))[0] - const dst = (await db.select().from(servers).where(eq(servers.id, dstId)).limit(1))[0] - if (!src || !dst) throw new Error("Server not found") + const endpoints = await db.select().from(servers).where(inArray(servers.id, [srcId, dstId])) + const src = endpoints.find(server => server.id === srcId) + const dst = endpoints.find(server => server.id === dstId) + if (!src || !dst) throw new SpeedTestServerNotFound() assertMonitoringServersEnabled(src, dst) const protocol = params.protocol === "udp" ? "udp" : "tcp" @@ -117,6 +121,7 @@ export async function executeBandwidthSpeedTestInner(params: SpeedTestRunParams) protocol, direction, durationSec, + ...(params.maxMbps===undefined?{}:{maxMbps:params.maxMbps}), }) assertBandwidthTestCompleted(rows) const { txAvgMbps: txFinal, rxAvgMbps: rxFinal, txSeries, rxSeries } = readBandwidthTestRates(rows) @@ -274,17 +279,15 @@ export async function recordSpeedTestFailure(params: { }) } -async function serverDisplayName(id: number): Promise { - const s = (await db.select().from(servers).where(eq(servers.id, id)).limit(1))[0] - return s ? (s.name || s.host) : `#${id}` -} - /** Фоновый прогон всех включённых speed-проб (по очереди). */ export async function runScheduledSpeedProbesOnce(): Promise { const sampledAt = new Date().toISOString() const runs: SpeedRunSnapshot[] = [] const rows = await db.select().from(uptimeSpeedProbes).orderBy(asc(uptimeSpeedProbes.sortOrder)) - const enabledIds = new Set((await listEnabledMonitoringServers()).map(server => server.id)) + const enabledServers = await listEnabledMonitoringServers() + const enabledIds = new Set(enabledServers.map(server => server.id)) + const serverNames = new Map(enabledServers.map(server => [server.id, server.name || server.host])) + const serverDisplayName = (id: number) => serverNames.get(id) ?? `#${id}` const enabled = rows.filter((r) => r.enabled !== false && r.srcServerId !== r.dstServerId && enabledIds.has(r.srcServerId) && enabledIds.has(r.dstServerId)) const protocol = (p: (typeof enabled)[number]) => (p.protocol === "udp" ? "udp" : "tcp") @@ -307,9 +310,9 @@ export async function runScheduledSpeedProbesOnce(): Promisets.transpileModule(await readFile(new URL(name,import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ES2022}}).outputText +const uri=source=>'data:text/javascript;base64,'+Buffer.from(source).toString('base64') +// Preserve offline I/O substitution while linking pure relative leaf modules. +const pureModules=new Map() +async function pureModuleUri(name){ + if(pureModules.has(name))return pureModules.get(name) + const loading=(async()=>{ + let code=await compile(name) + const ast=ts.createSourceFile(name,code,ts.ScriptTarget.ES2022,true,ts.ScriptKind.JS) + const imports=ast.statements.filter(ts.isImportDeclaration).filter(node=>node.moduleSpecifier.text.startsWith('./')) + const edits=await Promise.all(imports.map(async node=>({start:node.moduleSpecifier.getStart(ast),end:node.moduleSpecifier.end,value:await pureModuleUri(node.moduleSpecifier.text.replace(/\.js$/,'.ts'))}))) + for(const edit of edits.reverse())code=code.slice(0,edit.start)+JSON.stringify(edit.value)+code.slice(edit.end) + return uri(code) + })() + pureModules.set(name,loading);return loading +} +const diagnosticUri=await pureModuleUri('./vrf-execution-diagnostic.ts') +const diagnostic=await import(diagnosticUri) +const id='12345678-1234-4123-8123-123456789abc' +let cases=0 +for(const [error,expected]of [[Error('SECRET'),'Error'],[new TypeError('SECRET'),'TypeError'],[new RangeError('SECRET'),'RangeError'],[new SyntaxError('SECRET'),'SyntaxError'],[new ReferenceError('SECRET'),'ReferenceError'],[new URIError('SECRET'),'URIError'],[new EvalError('SECRET'),'EvalError'],[{message:'SECRET'},'NonError'],[new Proxy(Error('SECRET'),{get(){throw Error('getter')}}),'NonError']]){ + const value=diagnostic.vrfExecutionDiagnostic(id,'create','raw-before',error,false,{serverId:1,catalog:'vrf',boundary:'native-get'}) + assert.equal(value.errorClass,expected);assert.equal(JSON.stringify(value).includes('SECRET'),false);cases++ +} +const trap=Error('SECRET') +for(const key of ['message','stack','code','cause'])Object.defineProperty(trap,key,{get(){throw Error('SECRET getter')}}) +assert.equal(diagnostic.vrfExecutionDiagnostic(id,'create','raw-before',trap,false).category,'other');cases++ +for(const code of ['ECONNRESET','ETIMEDOUT','40P01','SECRET']){ + const value=diagnostic.vrfExecutionDiagnostic(id,'delete','ack-journal',Object.assign(Error('SECRET'),{code}),true) + assert.equal(value.category,({ECONNRESET:'reset',ETIMEDOUT:'timeout','40P01':'pg-deadlock'})[code]??'other') + assert.equal(JSON.stringify(value).includes('SECRET'),false);cases++ +} +assert.equal(diagnostic.vrfExecutionDiagnostic('SECRET','create','raw-before',Error(),false),undefined) +assert.equal(diagnostic.vrfExecutionDiagnostic(id,'SECRET','raw-before',Error(),false),undefined) +assert.equal(diagnostic.vrfExecutionDiagnostic(id,'create','SECRET',Error(),false),undefined) +assert.equal(diagnostic.vrfExecutionDiagnostic(id,'create','raw-before',Error(),false,{serverId:1,catalog:'SECRET',boundary:'native-get'}).read,undefined);cases++ +diagnostic.notifyVrfDiagnostic(()=>{throw Error('observer')},{}) +diagnostic.notifyVrfDiagnostic(async()=>{throw Error('observer')},{}) +await new Promise(resolve=>setImmediate(resolve));cases++ + +const key='__vrfDiagnosticOfflineFixture' +class McpError extends Error{constructor(code,message){super(message);this.code=code}} +globalThis[key]={McpError} +const functions=['readVrfProductionSnapshot','claimStoredVrfProductionTicket','inspectVrfProductionAdmission','assertVrfProductionHistoryProvenance','assertVrfProductionTransitionCheckpoint','classifyVrfProductionTransition','openVrfProductionTransitionCheckpoint','sealVrfProductionTransitionCheckpoint','vrfProductionCheckpointCatalogs','assertVerifiedConfigurationAccess','withVrfClaimQuarantine','assertVrfActionContext','vrfNativeActionFor','vrfActionContractsCurrent','findMcpOperation','decryptSecretExisting','encryptSecret','fingerprint','withVrfProductionSealingKey','configurationRows','resourceById','routerClient','rosSnapshotSourceProof'] +const stubUri=uri(`export const McpError=globalThis.${key}.McpError;export const pool={query:(...args)=>globalThis.${key}.query(...args)};`+functions.map(name=>`export const ${name}=(...args)=>globalThis.${key}.${name}(...args);`).join('\n')) +let emitted=await compile('./vrf-production-execution.ts') +const ast=ts.createSourceFile('executor.js',emitted,ts.ScriptTarget.ES2022,true,ts.ScriptKind.JS) +const edits=ast.statements.filter(ts.isImportDeclaration).map(node=>({start:node.moduleSpecifier.getStart(ast),end:node.moduleSpecifier.end,value:node.moduleSpecifier.text.endsWith('/vrf-execution-diagnostic.js')?diagnosticUri:stubUri})) +for(const edit of edits.reverse())emitted=emitted.slice(0,edit.start)+JSON.stringify(edit.value)+emitted.slice(edit.end) +const api=await import(uri(emitted)) +function fixture(options={}){ + const failure=Object.assign(Error('PRIVATE_PASSWORD native-body SQL /private/path'),{code:options.errno??'ETIMEDOUT'}) + const events=[],calls=[];let reads=0,deps=0,access=0,inspect=0,routers=0,guard,clientCalls=0 + const proof='a'.repeat(64),snapshot={projection:'raw-native-v1',serverId:1,sourceProof:proof,catalogs:{vrf:[{'.id':'*0',name:'main',interfaces:'all',disabled:'false'}],interface_inventory:[]}} + const fail=name=>{calls.push(name);if(options.fail===name)throw failure} + const f={McpError, + async query(sql){ + if(sql.includes('SELECT cp.after_sealed')){fail('history-load');return{rows:[]}} + if(sql.includes('SELECT ticket_id')){fail('ticket-lookup');return{rows:[{ticket_id:'ticket'}]}} + if(sql.includes('SET before_sealed'))fail('checkpoint-before') + if(sql.includes("SET state='applied'"))fail('ack-journal') + if(sql.includes('SET after_sealed'))fail('checkpoint-after') + return{rows:[{id,operation_id:id}]} + }, + async assertVerifiedConfigurationAccess(){fail('access-'+(++access))}, + async routerClient(){fail('router-'+(++routers));return{server:{},client:{ + async put(){clientCalls++;fail('client-before-guard');await guard();fail('client-after-guard');return options.badAck?{}:{'.id':'*1'}}, + async patch(){clientCalls++;await guard();return{'.id':'*1'}}, + async delete(_path,_timeout,_before,onBody){clientCalls++;await guard();onBody(options.badDeleteBody?'PRIVATE_INVALID_JSON':'{}')}, + }}}, + async rosSnapshotSourceProof(){return proof}, + vrfNativeActionFor(){return'ros.vrf.create'},assertVrfActionContext(){},vrfActionContractsCurrent(){return true},findMcpOperation(){return{version:1}}, + async withVrfProductionSealingKey(work){fail('sealing-key');return work(Buffer.alloc(32))}, + async readVrfProductionSnapshot(input){reads++;input.observeRead?.({serverId:1,catalog:'gre',boundary:'native-get'});fail('read-'+reads);return structuredClone(snapshot)}, + configurationRows(_spec,rows){return rows},resourceById(){return{}},fingerprint(){return'current-fingerprint'}, + async withVrfClaimQuarantine(input){return input.claim()},async claimStoredVrfProductionTicket(){fail('ticket-claim');return{privateSealed:'PRIVATE_SEALED'}}, + async encryptSecret(){return'PRIVATE_CIPHERTEXT'},async decryptSecretExisting(){throw Error('not used')}, + inspectVrfProductionAdmission(){fail('inspect-'+(++inspect));return{admission:{}}}, + assertVrfProductionHistoryProvenance(){},assertVrfProductionTransitionCheckpoint(){},openVrfProductionTransitionCheckpoint(){},vrfProductionCheckpointCatalogs(){return[]}, + classifyVrfProductionTransition(){fail('classification');return{checkpoint:{}}},sealVrfProductionTransitionCheckpoint(){return'PRIVATE_HISTORY'}, + } + globalThis[key]=f + const input={operationId:id,planId:'plan',connectionId:'connection',ownerId:'owner',operationName:options.action==='delete'?'ros.vrf.delete':'ros.vrf.create',operationVersion:1,stepIndex:0, + step:{serverId:1,resource:'vrf',action:options.action??'create',...(options.action==='delete'?{id:'*1'}:{}),fields:options.action==='delete'?{}:{name:'customers',interfaces:'none'},placeBeforeId:options.action==='delete'?undefined:'*0'},sourceProof:proof, + originalSnapshots:options.originalDrift?[{serverId:1,resource:'vrf',sourceProof:proof,fingerprint:'old-fingerprint'}]:[], + async authorize(){},async assertDependencies(){fail('dependencies-'+(++deps))},installBeforeDispatch(value){fail('install');guard=value}, + onDiagnostic(value){events.push(value);if(options.observerThrows)throw Error('logging failed');if(options.observerRejects)return Promise.reject(Error('async logging failed'))}, + } + return{failure,events,calls,input,clientCalls:()=>clientCalls,run:()=>api.executeVrfProductionStep(input)} +} +for(const [fail,stage,ack]of [ + ['access-1','initial-authorization',false],['sealing-key','sealing-key',false],['read-1','raw-before',false], + ['ticket-lookup','ticket-lookup',false],['ticket-claim','ticket-claim',false],['checkpoint-before','checkpoint-before',false],['install','install-predispatch',false], + ['access-2','explicit-predispatch-authorization',false],['dependencies-1','explicit-predispatch-dependencies',false],['read-2','explicit-predispatch-read',false],['inspect-1','explicit-predispatch-admission',false],['access-3','explicit-predispatch-final-authorization',false], + ['router-4','dispatch-source',false],['client-before-guard','client-call',false],['access-4','installed-predispatch-authorization',false],['dependencies-2','installed-predispatch-dependencies',false],['read-3','installed-predispatch-read',false],['inspect-2','installed-predispatch-admission',false],['access-5','installed-predispatch-final-authorization',false],['client-after-guard','client-call',false], + ['ack-journal','ack-journal',true],['access-6','post-ack-authorization',true],['read-4','raw-after',true],['classification','transition-classification',true],['dependencies-3','post-ack-dependencies',true],['access-7','post-ack-final-authorization',true],['checkpoint-after','checkpoint-after',true], +]){ + const f=fixture({fail});let thrown;try{await f.run()}catch(error){thrown=error} + assert.ok(thrown,fail);assert.equal(f.events.length,1,fail);assert.equal(f.events[0].stage,stage,fail);assert.equal(f.events[0].executorAckObserved,ack,fail) + assert.equal(f.events[0].category,'timeout',fail) + if(fail.startsWith('read-'))assert.deepEqual(f.events[0].read,{serverId:1,catalog:'gre',boundary:'native-get'},fail) + else assert.equal(f.events[0].read,undefined,fail) + if(!ack)assert.equal(thrown,f.failure,fail) + else assert.ok(thrown instanceof api.VrfProductionAcknowledgedFailure,fail) + if(fail==='ack-journal')assert.equal(thrown.code,'VRF_ACK_JOURNAL_UNCONFIRMED') + if(fail==='checkpoint-after')assert.equal(thrown.code,'VRF_VERIFIED_CHECKPOINT_UNCONFIRMED') + assert.equal(JSON.stringify(f.events).includes('PRIVATE'),false);cases++ +} +for(const observer of ['observerThrows','observerRejects']){ + const f=fixture({fail:'read-1',[observer]:true});await assert.rejects(f.run(),error=>error===f.failure) + await new Promise(resolve=>setImmediate(resolve));assert.equal(f.events.length,1);cases++ +} +const original=fixture({originalDrift:true});await assert.rejects(original.run(),error=>error.code==='STATE_CONFLICT');assert.equal(original.events[0].stage,'original-dependencies');cases++ +const history=fixture({action:'delete',fail:'history-load'});await assert.rejects(history.run(),error=>error===history.failure);assert.equal(history.events[0].stage,'history-load');cases++ +const malformed=fixture({badAck:true});await assert.rejects(malformed.run(),error=>error.code==='STATE_CONFLICT'&&!(error instanceof api.VrfProductionAcknowledgedFailure));assert.equal(malformed.events[0].stage,'ack-identity');assert.equal(malformed.events[0].executorAckObserved,true);cases++ +const malformedDelete=fixture({action:'delete',badDeleteBody:true});await assert.rejects(malformedDelete.run(),error=>error instanceof api.VrfProductionAcknowledgedFailure&&error.code==='VERIFICATION_FAILED');assert.equal(malformedDelete.events[0].executorAckObserved,true);assert.equal(malformedDelete.events[0].stage,'ack-identity');cases++ +const happy=fixture();const receipt=await happy.run();assert.equal(receipt.configurationVerified,true);assert.equal(happy.events.length,0);assert.equal(happy.clientCalls(),1);cases++ +const silent=fixture({fail:'read-1'});delete silent.input.onDiagnostic;await assert.rejects(silent.run(),error=>error===silent.failure);cases++ +// Exercise the exact emitted runtime policy call/callback/fallback/clear wiring. +// Only its surrounding policy and executor I/O are synthetic. Extracting the +// live AST statements means this test fails if the runtime stops wiring them. +const runtimeSource=await compile('../modules/mcp/runtime.ts') +const runtimeAst=ts.createSourceFile('runtime.js',runtimeSource,ts.ScriptTarget.ES2022,true,ts.ScriptKind.JS) +const setup=[],clear=[],policyCalls=[],fallback=[] +function visit(node){ + if(ts.isExpressionStatement(node)){ + const text=node.getText(runtimeAst) + if(text.startsWith('vrfDiagnosticFallback = productionVrf'))setup.push(text) + if(text==='vrfDiagnosticFallback = undefined;')clear.push(text) + if(text==='vrfDiagnosticFallback?.failure(error);')fallback.push(text) + } + if(ts.isVariableStatement(node)&&node.declarationList.declarations.length===1){ + const value=node.declarationList.declarations[0] + if(value.name.getText(runtimeAst)==='result'&&value.initializer&&ts.isAwaitExpression(value.initializer)&&ts.isCallExpression(value.initializer.expression)&&value.initializer.expression.expression.getText(runtimeAst)==='withVerifiedConfigurationChange'&&node.getText(runtimeAst).includes('executeVrfProductionStep'))policyCalls.push(node.getText(runtimeAst)) + } + ts.forEachChild(node,visit) +} +visit(runtimeAst) +for(const statements of [setup,clear,policyCalls,fallback])assert.equal(statements.length,1) +const runtimeBoundary=new Function(`return async function(env){ + const {productionVrf,step,plan,operationId,connection,serverIds,evidence,serviceExecution,reservedStep,authorizeCatalog,authorize,prefix,McpError,executeVrfProductionStep,executeRosStep,withVerifiedConfigurationChange,createVrfRuntimeDiagnosticBoundary,console}=env; + const index=0;let vrfDiagnosticFallback,vrfBeforeDispatch; + const beforeDispatch=env.beforeDispatch; + try{${setup[0]}\n${policyCalls[0]}\n${clear[0]}\nif(env.failAfterWrapper)throw env.failure;return result} + catch(error){${fallback[0]}\nthrow error} +}`)() +for(const mode of ['runtime-policy-entry','policy-authorize','executor-entry','executor-clone','executor-binding','executor-reported','runtime-policy-exit','after-wrapper','non-production','success']){ + for(const loggerThrows of [false,true]){ + 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'}}}, + 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}, + async executeVrfProductionStep(input){ + if(mode==='executor-entry')throw error + if(mode==='executor-clone'){input.step.fields.uncloneable=()=>{};return api.executeVrfProductionStep(input)} + if(mode==='executor-binding'){fixture();globalThis[key].assertVrfActionContext=()=>{throw error};return api.executeVrfProductionStep(input)} + if(mode==='executor-reported'){input.onDiagnostic(diagnostic.vrfExecutionDiagnostic(id,'create','raw-before',error,false,{serverId:1,catalog:'vrf',boundary:'native-get'}));throw error} + return{kind:'vrf-production-execution-v1',configurationVerified:true,nativeId:'*1'} + }, + async withVerifiedConfigurationChange(context,work){ + if(mode==='runtime-policy-entry')throw error + if(mode==='policy-authorize')await context.authorize() + const receipt=await work();if(mode==='runtime-policy-exit')throw error;return receipt + }, + } + if(mode==='success'){assert.equal((await runtimeBoundary(env)).configurationVerified,true);assert.equal(events.length,0)} + else{ + await assert.rejects(runtimeBoundary(env),value=>mode==='executor-clone'?value.name==='DataCloneError':value===error) + if(['after-wrapper','non-production'].includes(mode))assert.equal(events.length,0) + else{assert.equal(events.length,1);assert.equal(events[0].stage,mode==='executor-reported'?'raw-before':['executor-clone','executor-binding'].includes(mode)?'executor-entry':mode==='policy-authorize'?'runtime-policy-entry':mode);assert.equal(events[0].executorAckObserved,mode==='runtime-policy-exit');assert.equal(events[0].category,mode==='executor-clone'?'other':'reset')} + } + assert.equal(JSON.stringify(events).includes('PRIVATE'),false);cases++ + } +} +delete globalThis[key] +console.log('VRF private execution diagnostics, actual executor body with offline imported I/O, closed projection cases:',cases) diff --git a/backend/src/services/vrf-execution-diagnostic.ts b/backend/src/services/vrf-execution-diagnostic.ts new file mode 100644 index 00000000..4f4dfa7e --- /dev/null +++ b/backend/src/services/vrf-execution-diagnostic.ts @@ -0,0 +1,74 @@ +import { types } from "node:util" +import { readVrfTransitionRefusal, type VrfTransitionRefusalTag } from "./vrf-transition-refusal.js" + +export const VRF_EXECUTION_STAGES = [ + "runtime-policy-entry", "executor-entry", "runtime-policy-exit", + "initial-authorization", "sealing-key", "history-load", "raw-before", "original-dependencies", "history-provenance", "supported-shape", "ticket-lookup", "ticket-claim", "checkpoint-before", "install-predispatch", + "explicit-predispatch-authorization", "explicit-predispatch-dependencies", "explicit-predispatch-read", "explicit-predispatch-admission", "explicit-predispatch-final-authorization", + "dispatch-source", "client-call", "installed-predispatch-authorization", "installed-predispatch-dependencies", "installed-predispatch-read", "installed-predispatch-admission", "installed-predispatch-final-authorization", + "ack-identity", "ack-journal", "post-ack-authorization", "raw-after", "transition-classification", "post-ack-dependencies", "post-ack-final-authorization", "checkpoint-after", +] as const +export type VrfExecutionStage = typeof VRF_EXECUTION_STAGES[number] +export const VRF_READ_BOUNDARIES = ["authority-before", "native-get", "authority-after", "shape", "snapshot-validation", "final-authority"] as const +export interface VrfRawReadObservation { serverId: number; catalog?: string; boundary: typeof VRF_READ_BOUNDARIES[number] } +const catalogs = new Set(["bridge", "bridge_port", "bridge_vlan", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table", "ethernet", "ip_filter", "ipv6_filter", "routing_rule", "ip_settings", "ipv6_settings", "interface_inventory", "interface_list_members", "vlan", "bonding", "wireguard", "wireguard_peer", "gre", "ipip", "eoip", "vxlan", "vxlan_vtep", "pppoe_client"]) +const classes = [[Error.prototype, "Error"], [TypeError.prototype, "TypeError"], [RangeError.prototype, "RangeError"], [SyntaxError.prototype, "SyntaxError"], [ReferenceError.prototype, "ReferenceError"], [URIError.prototype, "URIError"], [EvalError.prototype, "EvalError"]] as const +const categories = Object.freeze({ ECONNRESET: "reset", ECONNREFUSED: "refused", ETIMEDOUT: "timeout", ENOTFOUND: "dns", EAI_AGAIN: "dns", EPIPE: "pipe", ABORT_ERR: "aborted", ERR_STREAM_DESTROYED: "stream", "40001": "pg-serialization", "40P01": "pg-deadlock", "57014": "pg-cancelled", "08003": "pg-connection", "08006": "pg-connection", "57P01": "pg-connection" } as const) +export interface VrfExecutionDiagnostic { + event: "mcp.vrf.execution-failed" + operationId: string + action: "create" | "patch" | "delete" + stage: VrfExecutionStage + errorClass: "Error" | "TypeError" | "RangeError" | "SyntaxError" | "ReferenceError" | "URIError" | "EvalError" | "OtherNativeError" | "NonError" + category: "other" | typeof categories[keyof typeof categories] + /** Executor response observation only, never write-fence/dispatch proof. */ + executorAckObserved: boolean + read?: VrfRawReadObservation + transition?: VrfTransitionRefusalTag +} +/** Closed private diagnostic only. Never read error messages, stacks, getters, + * causes, request bodies, SQL or native values. No outcome/authority is minted. */ +export function vrfExecutionDiagnostic(operationId: string, action: string, stage: VrfExecutionStage, error: unknown, executorAckObserved: boolean, read?: VrfRawReadObservation): VrfExecutionDiagnostic | undefined { + if (typeof operationId !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(operationId) || !["create", "patch", "delete"].includes(action) || !VRF_EXECUTION_STAGES.includes(stage) || typeof executorAckObserved !== "boolean") return undefined + const result: VrfExecutionDiagnostic = { event: "mcp.vrf.execution-failed", operationId, action: action as VrfExecutionDiagnostic["action"], stage, errorClass: "NonError", category: "other", executorAckObserved } + if (types.isNativeError(error)) { + result.errorClass = classes.find(([prototype]) => Object.getPrototypeOf(error) === prototype)?.[1] ?? "OtherNativeError" + const descriptor = Object.getOwnPropertyDescriptor(error, "code") + if (descriptor && Object.hasOwn(descriptor, "value") && typeof descriptor.value === "string" && Object.hasOwn(categories, descriptor.value)) result.category = categories[descriptor.value as keyof typeof categories] + } + if (read && Number.isSafeInteger(read.serverId) && read.serverId > 0 && VRF_READ_BOUNDARIES.includes(read.boundary) && (read.catalog === undefined || typeof read.catalog === "string" && catalogs.has(read.catalog))) result.read = { serverId: read.serverId, boundary: read.boundary, ...(read.catalog === undefined ? {} : { catalog: read.catalog }) } + const transition = readVrfTransitionRefusal(error) + if (transition) result.transition = transition + return result +} +/** Observers are in-process only and cannot fail, await, or replace the work. */ +export function notifyVrfDiagnostic(observer: ((value: T) => unknown) | undefined, value: T): void { + try { + if (!observer) return + const result = observer(value) + if (types.isPromise(result)) void Promise.prototype.then.call(result, undefined, () => {}) + } catch { /* Private observation never changes the original control flow. */ } +} +/** Private per-step fallback for failures before the executor's own try or in + * its surrounding policy context. Executor diagnostics always take precedence. + * Runtime discards this object immediately when the policy wrapper succeeds. */ +export function createVrfRuntimeDiagnosticBoundary(operationId: string, action: string, observer: (value: VrfExecutionDiagnostic) => unknown) { + let stage: VrfExecutionStage = "runtime-policy-entry", emitted = false, executorAckObserved = false + return { + executorEntered() { stage = "executor-entry" }, + executorReturned() { stage = "runtime-policy-exit"; executorAckObserved = true }, + observed(value: VrfExecutionDiagnostic) { + if (value.operationId !== operationId || value.action !== action || emitted) return + emitted = true + notifyVrfDiagnostic(observer, value) + }, + failure(error: unknown) { + if (emitted) return + emitted = true + try { + const value = vrfExecutionDiagnostic(operationId, action, stage, error, executorAckObserved) + if (value) notifyVrfDiagnostic(observer, value) + } catch { /* Fallback logging cannot replace the outer runtime error. */ } + }, + } +} diff --git a/backend/src/services/vrf-members.test.ts b/backend/src/services/vrf-members.test.ts new file mode 100644 index 00000000..aaaa8de4 --- /dev/null +++ b/backend/src/services/vrf-members.test.ts @@ -0,0 +1,11 @@ +import assert from "node:assert/strict" +import { canonicalVrfMembers } from "./vrf-members.js" +assert.equal(canonicalVrfMembers("gre,bridge"), "bridge,gre") +assert.equal(canonicalVrfMembers(["gre", "bridge"]), "bridge,gre") +assert.equal(canonicalVrfMembers(" bridge, gre "), "bridge,gre") +assert.equal(canonicalVrfMembers("all"), "all") +assert.equal(canonicalVrfMembers("none"), "none") +assert.equal(canonicalVrfMembers([]), "none") +assert.equal(canonicalVrfMembers(""), "none") +for (const value of [null, undefined, {}, ["bridge", {}], "gre,,bridge", ["gre", "gre"], "none,gre"]) assert.throws(() => canonicalVrfMembers(value)) +console.log("VRF membership: native array/string equivalence, unordered members, explicit empty/none and malformed/duplicate rejection; no row reordering") diff --git a/backend/src/services/vrf-members.ts b/backend/src/services/vrf-members.ts new file mode 100644 index 00000000..4aacec77 --- /dev/null +++ b/backend/src/services/vrf-members.ts @@ -0,0 +1,8 @@ +/** Membership order within one VRF is immaterial; VRF row priority remains ordered. */ +export function canonicalVrfMembers(value: unknown): string { + const tokens = Array.isArray(value) ? value : typeof value === "string" ? value === "" ? [] : value.split(",") : null + if (!tokens || tokens.length > 1000 || tokens.some(token => typeof token !== "string" || !token.trim())) throw new Error("Не подтверждён список интерфейсов VRF") + const members = tokens.map(token => (token as string).trim()) + if (new Set(members).size !== members.length || members.includes("none") && members.length > 1) throw new Error("Повторяющиеся или противоречивые интерфейсы VRF") + return members.length ? members.sort().join(",") : "none" +} diff --git a/backend/src/services/vrf-production-action-binding.test.ts b/backend/src/services/vrf-production-action-binding.test.ts new file mode 100644 index 00000000..e954493b --- /dev/null +++ b/backend/src/services/vrf-production-action-binding.test.ts @@ -0,0 +1,26 @@ +import assert from "node:assert/strict" +import { assertVrfActionContext, dedupeVrfAliasSnapshots, vrfNativeActionFor, vrfPublicAuthority } from "./vrf-production-action-binding.js" +import type { VrfTicketContext } from "./vrf-production-ticket-store.js" +import type { VrfProductionRequest } from "./vrf-production-admission.js" +import type { RosSnapshot } from "../modules/mcp/resources.js" +const context = { actionId: "ros.vrf.update", actionVersion: 4, authorityAction: { id: "routing.vrf.update", version: 4 } } as VrfTicketContext +const request: VrfProductionRequest = { action: "patch", id: "*a", fields: { interfaces: "owned-a,owned-b" } } +let cases = 0 +assertVrfActionContext(context, request); cases++ +assert.deepEqual(vrfPublicAuthority(context), { id: "routing.vrf.update", version: 4 }); cases++ +assert.equal(vrfNativeActionFor("routing.vrf.update", request), "ros.vrf.update"); cases++ +const invalidRequests: VrfProductionRequest[] = [ + { ...request, action: "create" as const }, { ...request, action: "delete" as const }, { ...request, fields: { name: "other" } }, + { ...request, fields: { interfaces: "owned-a", comment: "extra" } }, { ...request, placeBeforeId: "*b" }, { ...request, id: undefined }, +] +for (const changed of invalidRequests) { assert.throws(() => assertVrfActionContext(context, changed)); cases++ } +assert.throws(() => assertVrfActionContext({ ...context, authorityAction: { id: "routing.vrf.update", version: 2 } } as unknown as VrfTicketContext, request)); cases++ +assert.throws(() => assertVrfActionContext({ ...context, authorityAction: { id: "routing.vrf.update", version: 3 } } as unknown as VrfTicketContext, request)); cases++ +assert.throws(() => assertVrfActionContext({ ...context, actionId: "ros.vrf.delete" }, request)); cases++ +const snapshot: RosSnapshot = { serverId: 1, resource: "vrf", rows: [{ ".id": "*a", interfaces: "owned-a" }], fingerprint: "a".repeat(64), sourceProof: "b".repeat(64), projectionVersion: 3 } +assert.equal(dedupeVrfAliasSnapshots([snapshot, structuredClone(snapshot)]).length, 1); cases++ +for (const mutate of [ + (value: RosSnapshot) => { value.sourceProof = "c".repeat(64) }, (value: RosSnapshot) => { value.projectionVersion = 2 }, + (value: RosSnapshot) => { value.fingerprint = "d".repeat(64) }, (value: RosSnapshot) => { value.rows[0].interfaces = "changed" }, +]) { const changed = structuredClone(snapshot); mutate(changed); assert.throws(() => dedupeVrfAliasSnapshots([snapshot, changed])); cases++ } +console.log(JSON.stringify({ component: "VRF closed business alias mapping/deduplication", cases, actualRouter: false })) diff --git a/backend/src/services/vrf-production-action-binding.ts b/backend/src/services/vrf-production-action-binding.ts new file mode 100644 index 00000000..f942fd79 --- /dev/null +++ b/backend/src/services/vrf-production-action-binding.ts @@ -0,0 +1,53 @@ +import { isDeepStrictEqual } from "node:util" +import { hasManagedCommentPrefix } from "../managed-markers.js" +import { findMcpOperation } from "../modules/mcp/registry.js" +import type { ChangeEvidence } from "../modules/mcp/action-types.js" +import type { RosSnapshot } from "../modules/mcp/resources.js" +import type { VrfProductionRequest } from "./vrf-production-admission.js" + +export type VrfNativeActionId = "ros.vrf.create" | "ros.vrf.update" | "ros.vrf.delete" +export type VrfAuthorityActionId = VrfNativeActionId | "routing.vrf.update" +export interface VrfActionContext { + actionId: VrfNativeActionId + actionVersion: number + /** Actual public authority, never a canonical permission substitution. */ + authorityAction?: { id: "routing.vrf.update"; version: 4 } +} +const refuse = (reason: string): never => { throw Error("VRF action binding refused: " + reason) } +export function vrfPublicAuthority(context: VrfActionContext): { id: VrfAuthorityActionId; version: number } { + return context.authorityAction ?? { id: context.actionId, version: context.actionVersion } +} +export function vrfNativeActionFor(publicId: string, request: VrfProductionRequest): VrfNativeActionId { + const native = `ros.vrf.${request.action === "patch" ? "update" : request.action}` as VrfNativeActionId + if (!["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete"].includes(native)) refuse("unknown native action") + if (publicId !== native && !(publicId === "routing.vrf.update" && native === "ros.vrf.update")) refuse("unmapped public/native action") + return native +} +export function assertVrfActionContext(context: VrfActionContext, request: VrfProductionRequest): void { + const authority = vrfPublicAuthority(context) + if (!Number.isSafeInteger(context.actionVersion) || context.actionVersion < 1 || vrfNativeActionFor(authority.id, request) !== context.actionId) refuse("native action/version") + if (context.authorityAction && (context.authorityAction.id !== "routing.vrf.update" || context.authorityAction.version !== 4 || request.action !== "patch" || !request.id || !/^\*[a-f0-9]+$/i.test(request.id) || request.placeBeforeId !== undefined || Object.keys(request.fields).length !== 1 || typeof request.fields.interfaces !== "string")) refuse("alias must be one exact interfaces PATCH") +} +/** Registry versions are protocol fences, not permission grants. */ +export function vrfActionContractsCurrent(context: VrfActionContext): boolean { + const authority = vrfPublicAuthority(context) + return findMcpOperation(context.actionId)?.version === context.actionVersion && findMcpOperation(authority.id)?.version === authority.version +} +export function assertVrfAliasEvidence(context: VrfActionContext & { binding: { serverId: number } }, request: VrfProductionRequest, evidence: Pick): void { + if (!context.authorityAction) return + if (evidence.requiresHuman !== true || !Array.isArray(evidence.snapshots)) refuse("alias requires human approval and original snapshots") + const snapshots = evidence.snapshots.filter(snapshot => snapshot.serverId === context.binding.serverId) + const vrfs = snapshots.filter(snapshot => snapshot.resource === "vrf") + const row = vrfs.length === 1 ? vrfs[0].rows.find(row => row[".id"] === request.id) : undefined + if (!row || row.name === "main" || !hasManagedCommentPrefix(String(row.comment ?? ""))) refuse("alias managed target provenance") + for (const resource of ["ipv4_address", "ipv6_address", "routing_rule", "routing_table", "ospf_template", "gre", "vlan", "bridge_port"]) if (!snapshots.some(snapshot => snapshot.resource === resource)) refuse("alias original dependency missing: " + resource) +} +export function dedupeVrfAliasSnapshots(snapshots: RosSnapshot[]): RosSnapshot[] { + const unique = new Map() + for (const snapshot of snapshots) { + const key = `${snapshot.serverId}/${snapshot.resource}`, previous = unique.get(key) + if (previous && (previous.sourceProof !== snapshot.sourceProof || previous.projectionVersion !== snapshot.projectionVersion || previous.fingerprint !== snapshot.fingerprint || !isDeepStrictEqual(previous.rows, snapshot.rows))) refuse("original snapshot mismatch: " + key) + if (!previous) unique.set(key, snapshot) + } + return [...unique.values()] +} diff --git a/backend/src/services/vrf-production-admission.test.ts b/backend/src/services/vrf-production-admission.test.ts new file mode 100644 index 00000000..88652ed2 --- /dev/null +++ b/backend/src/services/vrf-production-admission.test.ts @@ -0,0 +1,203 @@ +import assert from "node:assert/strict" +import { createHash, createHmac } from "node:crypto" +import { claimVrfProductionAdmission, inspectVrfProductionAdmission, sealVrfProductionAdmission, VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionBinding, type VrfProductionRequest, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { readVrfProductionSnapshot, VRF_PRODUCTION_RAW_PATHS, type VrfProductionRawClient } from "./vrf-production-read.js" + +const binding: VrfProductionBinding = { serverId: 7, sourceProof: "a".repeat(64), actorId: "human-42", planId: "plan-17" } +const secret = Buffer.alloc(32, 73), now = 100000 +function snapshot(): VrfProductionSnapshot { + const catalogs: VrfProductionSnapshot["catalogs"] = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, []])) + catalogs.ip_settings = [{}]; catalogs.ipv6_settings = [{ disabled: "false" }] + catalogs.vrf = [{ ".id": "*1", name: "production", interfaces: "customer-vlan", disabled: "false", comment: "human configured" }, { ".id": "*2", name: "main", interfaces: "all", builtin: "true" }] + catalogs.interface_inventory = [{ ".id": "*10", name: "ether1", type: "ether", "rx-byte": "1234", "rx-bytes": "opaque-config" }, { ".id": "*11", name: "customer-vlan", type: "vlan" }] + catalogs.ethernet = [{ ".id": "*20", name: "ether1", mtu: "1500", "auto-negotiation": "yes" }] + catalogs.vlan = [{ ".id": "*21", name: "customer-vlan", interface: "ether1", "vlan-id": "101" }] + catalogs.ipv4_address = [{ ".id": "*30", interface: "customer-vlan", address: "192.0.2.1/24", network: "192.0.2.0", vrf: "production" }] + catalogs.ipv6_address = [{ ".id": "*31", interface: "customer-vlan", address: "2001:db8:1234::1/48", advertise: "yes" }, { ".id": "*32", interface: "customer-vlan", address: "fe80::1/64", dynamic: "true" }] + catalogs.ipv4_route = [{ ".id": "*40", "dst-address": "192.0.2.0/24", gateway: "customer-vlan", "routing-table": "production", dynamic: "true", connect: "true" }] + catalogs.routing_table = [{ ".id": "*1", name: "production", dynamic: "true", fib: "" }] + return { projection: "raw-native-v1", serverId: binding.serverId, sourceProof: binding.sourceProof, catalogs } +} +const request: VrfProductionRequest = { action: "patch", id: "*1", fields: { name: "customers" } } +const issued = sealVrfProductionAdmission({ snapshot: snapshot(), binding, request, secret, now }) +const input = () => ({ sealed: issued.sealed, snapshot: snapshot(), binding: structuredClone(binding), request: structuredClone(request), secret, now: now + 1 }) +const read = inspectVrfProductionAdmission(input()) +assert.equal(read.admission.version, 3) +assert.equal(read.admission.companionExecution, "requires-native-transition-proof") +assert.equal(read.admission.members[0].id, "*11") +assert.deepEqual(read.admission.members[0].parents.map(row => [row.id, row.configurationId]), [["*11", "*21"], ["*10", "*20"]]) +assert.equal(read.admission.members[0].addresses.length, 3) +assert.equal(read.admission.members[0].addresses[0].parentId, "*11") +assert.equal(read.digest, issued.digest) +const canonicalForPublicHash = (value: unknown): unknown => Array.isArray(value) ? value.map(canonicalForPublicHash) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, child]) => [key, canonicalForPublicHash(child)])) : value +const publicHash = (value: unknown): string => createHash("sha256").update(JSON.stringify(canonicalForPublicHash(value))).digest("hex") +const nativeForHash = snapshot() +delete nativeForHash.catalogs.interface_inventory[0]["rx-byte"] +assert.notEqual(read.admission.snapshotHash, publicHash(nativeForHash.catalogs)) +assert.notEqual(read.admission.members[0].configurationHash, publicHash(nativeForHash.catalogs.interface_inventory[1])) +assert.notEqual(read.admission.members[0].parents[0].configurationHash, publicHash(nativeForHash.catalogs.vlan[0])) +assert.notEqual(read.admission.members[0].addresses[0].configurationHash, publicHash(nativeForHash.catalogs.ipv4_address[0])) +assert.notEqual(read.digest, publicHash(read.admission)) +const otherKey = sealVrfProductionAdmission({ snapshot: snapshot(), binding, request, secret: Buffer.alloc(32, 74), now }) +assert.notEqual(otherKey.admission.snapshotHash, read.admission.snapshotHash) +assert.notEqual(otherKey.admission.members[0].configurationHash, read.admission.members[0].configurationHash) +assert.notEqual(otherKey.admission.members[0].parents[0].configurationHash, read.admission.members[0].parents[0].configurationHash) +assert.notEqual(otherKey.admission.members[0].addresses[0].configurationHash, read.admission.members[0].addresses[0].configurationHash) +assert.equal(inspectVrfProductionAdmission({ ...input(), sealed: otherKey.sealed, secret: Buffer.alloc(32, 74) }).digest, otherKey.digest) +// Identical row data under different roles must produce different fingerprints. +const sameRowSnapshot = snapshot() +sameRowSnapshot.catalogs.interface_inventory[1].interface = "ether1" +sameRowSnapshot.catalogs.vlan[0] = structuredClone(sameRowSnapshot.catalogs.interface_inventory[1]) +const sameRow = sealVrfProductionAdmission({ snapshot: sameRowSnapshot, binding, request, secret, now }).admission.members[0] +assert.notEqual(sameRow.configurationHash, sameRow.parents[0].configurationHash) +const privateConfig = snapshot() +privateConfig.catalogs.ethernet[0]["opaque-secret"] = "sensitive-test-value" +const privateTicket = sealVrfProductionAdmission({ snapshot: privateConfig, binding, request, secret, now }) +assert.equal(Buffer.from(privateTicket.sealed.split(".")[0], "base64url").toString("utf8").includes("sensitive-test-value"), false) +let refusals = 0 +function refuse(change: (value: ReturnType) => void): void { + const value = input(); change(value) + assert.throws(() => inspectVrfProductionAdmission(value)); refusals++ +} +for (const [resource, field] of [["ip_settings", "rx-byte"], ["ipv6_settings", "running"], ["routing_table", "packets"], ["vrf", "active"], ["ethernet", "bytes"], ["vlan", "running"]]) { + refuse(value => { value.snapshot.catalogs[resource][0][field] = "unreviewed-field-on-wrong-path" }) +} +refuse(value => { value.snapshot.catalogs.ip_settings[0].opaque = { "rx-byte": "nested configuration" } }) +refuse(value => { value.snapshot.catalogs.interface_inventory[0]["rx-byte"] = { opaque: "unreviewed telemetry object" } }) +for (const [resource, field] of [["interface_inventory", "rx-byte"], ["ipv4_route", "active"], ["routing_table", "invalid"], ["ip_settings", "ipv4-fast-path-bytes"]]) { + const fresh = input(); fresh.snapshot.catalogs[resource][0][field] = "changed-live-telemetry" + assert.equal(inspectVrfProductionAdmission(fresh).digest, issued.digest) +} +// Old admissions cannot be replayed under v3 even with the correct key. +// Private schema/domains and the four VRF operation versions changed together. +const oldPayload = Buffer.from(JSON.stringify({ ...issued.admission, version: 1 })).toString("base64url") +for (const domain of ["mmapp-vrf-production-admission-v1\0", "mmapp-vrf-production-admission-v2\0", "mmapp-vrf-production-admission-v3\0"]) { + refuse(value => { value.sealed = oldPayload + "." + createHmac("sha256", secret).update(domain).update(oldPayload).digest("hex") }) +} +for (const field of ["actorId", "planId", "sourceProof"] as const) refuse(value => { value.binding[field] = "different" }) +refuse(value => { value.binding.serverId++ }) +refuse(value => { value.snapshot.sourceProof = "b".repeat(64) }) +refuse(value => { value.snapshot.projection = "configuration-v1" as "raw-native-v1" }) +refuse(value => { value.request.fields.name = "another" }) +refuse(value => { value.request.id = "*2" }) +refuse(value => { value.now = now - 1 }) +refuse(value => { value.now = issued.admission.expiresAt }) +refuse(value => { value.secret = Buffer.alloc(32, 74) }) +refuse(value => { value.sealed = value.sealed.slice(0, -1) + (value.sealed.endsWith("a") ? "b" : "a") }) +refuse(value => { value.sealed += ".extra" }) +for (const resource of VRF_PRODUCTION_REQUIRED_CATALOGS) refuse(value => { delete value.snapshot.catalogs[resource] }) +for (const [resource, field, value] of [["interface_inventory", ".id", "*99"], ["ethernet", "mtu", "1400"], ["vlan", "interface", "unknown"], ["ipv4_address", "address", "192.0.2.2/24"], ["ipv4_address", "vrf", "main"], ["ipv6_address", "advertise", "no"], ["ipv4_route", "dynamic", "false"], ["routing_table", ".id", "*98"], ["vrf", "comment", "new owner"], ["interface_inventory", "rx-bytes", "new unknown field"]]) { + refuse(input => { input.snapshot.catalogs[resource][0][field] = value }) +} +refuse(value => { value.snapshot.catalogs.ipv6_address.pop() }) +refuse(value => { value.snapshot.catalogs.vrf.reverse() }) +refuse(value => { value.snapshot.catalogs.interface_inventory.push({ ".id": "*90", name: "ether1", type: "ether" }) }) +refuse(value => { value.snapshot.catalogs.vlan.push(structuredClone(value.snapshot.catalogs.vlan[0])) }) +refuse(value => { value.snapshot.catalogs.vrf[0]["unknown-config"] = "new" }) +const telemetryChange = input() +telemetryChange.snapshot.catalogs.interface_inventory[0]["rx-byte"] = "9999" +telemetryChange.snapshot.catalogs.ipv4_route[0].active = "false" +assert.equal(inspectVrfProductionAdmission(telemetryChange).digest, issued.digest) +const seal = (request: VrfProductionRequest, native = snapshot()) => sealVrfProductionAdmission({ request, snapshot: native, binding, secret, now }) +assert.equal(seal({ action: "create", fields: { name: "new-vrf", interfaces: "ether1" }, placeBeforeId: "*1" }).admission.members[0].name, "ether1") +assert.equal(seal({ action: "delete", id: "*1", fields: {} }).admission.members[0].addresses.length, 3) +assert.equal(seal({ action: "patch", id: "*1", fields: { interfaces: "ether1" } }).admission.members.length, 2) +assert.equal(seal({ action: "create", fields: { name: "new-vrf", interfaces: "none" }, placeBeforeId: "*1" }).admission.members.length, 0) +for (const bad of [ + { action: "delete", id: "*2", fields: {} }, { action: "patch", id: "*99", fields: { name: "x" } }, + { action: "patch", id: "*1", fields: {} }, { action: "delete", id: "*1", fields: { name: "x" } }, + { action: "create", id: "*90", fields: { name: "x", interfaces: "ether1" }, placeBeforeId: "*1" }, + { action: "create", fields: { name: "x", interfaces: "ether1" } }, + { action: "create", fields: { name: "production", interfaces: "ether1" }, placeBeforeId: "*1" }, + ...["all", "none,ether1", "ether1,ether1", "unknown", " ether1"].map(interfaces => ({ action: "patch", id: "*1", fields: { interfaces } })), + { action: "patch", id: "*1", fields: { disabled: "maybe" } }, { action: "patch", id: "*1", fields: { ".id": "*2" } }, +] as VrfProductionRequest[]) { assert.throws(() => seal(bad)); refusals++ } +const missingParent = snapshot(); delete missingParent.catalogs.vlan +assert.throws(() => seal(request, missingParent)); refusals++ +const cycle = snapshot(); cycle.catalogs.vlan[0].interface = "customer-vlan" +assert.throws(() => seal(request, cycle)); refusals++ +const unknownType = snapshot(); unknownType.catalogs.interface_inventory[1].type = "unreviewed" +assert.throws(() => seal(request, unknownType)); refusals++ +for (const type of ["__proto__", "constructor", "toString"]) { + const native = snapshot(); native.catalogs.interface_inventory[1].type = type + assert.throws(() => seal(request, native), /missing type-specific interface catalog/); refusals++ +} +// RouterOS hex IDs denote numeric identity: case and leading zero spelling +// cannot turn a duplicate into a separate configurable object. +for (const alias of ["*A", "*0a", "*000A"]) { + const native = snapshot() + native.catalogs.bridge = [{ ".id": "*a", name: "first" }, { ".id": alias, name: "second" }] + assert.throws(() => seal(request, native), /duplicate native ID/); refusals++ +} +for (const [type, resource] of [["ether", "ethernet"], ["bridge", "bridge"], ["vlan", "vlan"], ["bond", "bonding"], ["wireguard", "wireguard"], ["gre-tunnel", "gre"], ["ipip-tunnel", "ipip"], ["eoip-tunnel", "eoip"], ["vxlan", "vxlan"], ["pppoe-out", "pppoe_client"]]) { + const native = snapshot() + const selectedName = "human-selected" + native.catalogs.interface_inventory.push({ ".id": "*50", name: selectedName, type }) + native.catalogs[resource] ??= [] + const config: Record = { ".id": "*60", name: selectedName, comment: "existing human configuration" } + if (["vlan", "pppoe_client"].includes(resource)) config.interface = "ether1" + if (resource === "bonding") config.slaves = "ether1" + if (["gre", "ipip", "eoip", "vxlan"].includes(resource)) { + config["local-address"] = "192.0.2.1"; config["remote-address"] = "198.51.100.1" + native.catalogs.ipv4_address[0].dynamic = "false" + } + if (resource === "wireguard") native.catalogs.wireguard_peer = [{ ".id": "*70", interface: selectedName, "endpoint-address": "198.51.100.1", "allowed-address": "10.0.0.0/24" }] + if (resource === "vxlan") native.catalogs.vxlan_vtep = [{ ".id": "*70", interface: selectedName, "remote-ip": "198.51.100.1" }] + if (resource === "bridge") { + native.catalogs.bridge_port = [{ ".id": "*70", bridge: selectedName, interface: "ether1" }] + native.catalogs.bridge_vlan = [{ ".id": "*71", bridge: selectedName, tagged: `${selectedName},customer-vlan`, untagged: "ether1" }] + } + native.catalogs[resource].push(config) + native.catalogs.ipv4_address.push({ ".id": "*80", interface: selectedName, address: "203.0.113.1/24", dynamic: "false" }) + const selectedRequest: VrfProductionRequest = { action: "create", fields: { name: "new-vrf", interfaces: selectedName }, placeBeforeId: "*1" } + const client: VrfProductionRawClient = { async get(path: string): Promise { + const key = Object.keys(VRF_PRODUCTION_RAW_PATHS).find(key => VRF_PRODUCTION_RAW_PATHS[key] === path)! + return structuredClone(native.catalogs[key]) as T + } } + const raw = await readVrfProductionSnapshot({ serverId: binding.serverId, expectedSourceProof: binding.sourceProof, request: selectedRequest, async assertAuthority() {}, adapter: { async resolve(serverId) { return { serverId, sourceProof: binding.sourceProof, enabled: true, client } } } }) + const ticket = sealVrfProductionAdmission({ snapshot: raw, binding, request: selectedRequest, secret, now }) + const roundTrip = inspectVrfProductionAdmission({ sealed: ticket.sealed, snapshot: raw, binding, request: selectedRequest, secret, now: now + 1 }) + const member = roundTrip.admission.members[0] + assert.equal(member.id, "*50"); assert.equal(member.parents[0].configurationId, "*60") + assert.equal(member.parents[0].resource, resource) + assert.ok(member.addresses.some(address => address.id === "*80" && address.parentId === "*50")) + if (["gre", "ipip", "eoip", "vxlan", "bridge"].includes(resource)) { + assert.ok(member.parents.some(parent => parent.name === "customer-vlan" && parent.id === "*11" && parent.configurationId === "*21")) + assert.ok(member.parents.some(parent => parent.name === "ether1" && parent.id === "*10" && parent.configurationId === "*20")) + assert.ok(member.addresses.some(address => address.id === "*30" && address.parentId === "*11")) + } + if (resource === "wireguard" || resource === "vxlan") { + const extra = resource === "wireguard" ? "wireguard_peer" : "vxlan_vtep" + const missing = structuredClone(raw); delete missing.catalogs[extra] + assert.throws(() => seal(selectedRequest, missing), /missing .* catalog/); refusals++ + const changed = structuredClone(raw); changed.catalogs[extra][0].comment = "changed" + assert.throws(() => inspectVrfProductionAdmission({ sealed: ticket.sealed, snapshot: changed, binding, request: selectedRequest, secret, now: now + 1 })); refusals++ + } +} +const deep = snapshot() +deep.catalogs.interface_inventory = [{ ".id": "*10", name: "ether1", type: "ether" }] +deep.catalogs.vlan = [] +for (let i = 0; i < 130; i++) { + deep.catalogs.interface_inventory.push({ ".id": `*${(1000 + i).toString(16)}`, name: `vlan-${i}`, type: "vlan" }) + deep.catalogs.vlan.push({ ".id": `*${(2000 + i).toString(16)}`, name: `vlan-${i}`, interface: i === 129 ? "ether1" : `vlan-${i + 1}` }) +} +deep.catalogs.vrf[0].interfaces = "vlan-0" +assert.throws(() => seal(request, deep), /closure budget/); refusals++ +// Model the durable transaction contract: issued digest only; all subsequent +// concurrent claims and unknown ticket IDs refuse. No in-memory store is shipped +// as a production default and no native command is called by this service. +const durable = new Map([[issued.admission.ticketId, issued.digest]]) +const store = { async claimIssued(id: string, digest: string): Promise { + if (durable.get(id) !== digest) return false + durable.delete(id); return true +} } +const claims = await Promise.allSettled([claimVrfProductionAdmission(input(), store), claimVrfProductionAdmission(input(), store)]) +assert.equal(claims.filter(result => result.status === "fulfilled").length, 1) +await assert.rejects(() => claimVrfProductionAdmission(input(), store)); refusals++ +const another = seal(request) +await assert.rejects(() => claimVrfProductionAdmission({ ...input(), sealed: another.sealed }, store)); refusals++ +const changedBeforeClaim = input(); changedBeforeClaim.snapshot.catalogs.ethernet[0].mtu = "1300" +let called = false +await assert.rejects(() => claimVrfProductionAdmission(changedBeforeClaim, { async claimIssued() { called = true; return true } })); refusals++ +assert.equal(called, false) +console.log(`VRF production admission: existing physical/VLAN parent closure, IPv4 /24 and IPv6 /48 + LL preserved; signed source/human/plan/config/order binding; purpose-separated keyed configuration/ticket fingerprints; atomic single-use claim; ${refusals} refusal models; native transition/forwarding remain unsupported`) diff --git a/backend/src/services/vrf-production-admission.ts b/backend/src/services/vrf-production-admission.ts new file mode 100644 index 00000000..55190d15 --- /dev/null +++ b/backend/src/services/vrf-production-admission.ts @@ -0,0 +1,243 @@ +import { createHmac, randomUUID, timingSafeEqual } from "node:crypto" +import { isIP } from "node:net" +import { vrfProductionConfiguration } from "./vrf-production-projection.js" + +type Json = null | boolean | number | string | Json[] | { [key: string]: Json } +export type VrfProductionRow = Record +export interface VrfProductionSnapshot { + /** Only a trusted raw reader may set this; configurationRows is insufficient. */ + projection: "raw-native-v1" + serverId: number + sourceProof: string + catalogs: Record +} +export interface VrfProductionBinding { serverId: number; sourceProof: string; actorId: string; planId: string } +export interface VrfProductionRequest { + action: "create" | "patch" | "delete" + id?: string + fields: Record + /** Exact existing row before which a new VRF is inserted. */ + placeBeforeId?: string +} +export interface VrfProductionMember { + id: string + name: string + configurationHash: string + parents: Array<{ id: string; name: string; resource: string; configurationId: string; configurationHash: string }> + addresses: Array<{ resource: "ipv4_address" | "ipv6_address"; id: string; parentId: string; configurationHash: string }> +} +export interface VrfProductionAdmission { + version: 3 + ticketId: string + issuedAt: number + expiresAt: number + binding: VrfProductionBinding + request: VrfProductionRequest + snapshotHash: string + members: VrfProductionMember[] + /** This ticket proves a sealed precondition, never native side-effect support. */ + companionExecution: "requires-native-transition-proof" +} + +// The reviewed v5 fixture reads all these catalogs. Missing routes or raw LL +// addresses must never become an empty, apparently safe dependency closure. +export const VRF_PRODUCTION_REQUIRED_CATALOGS = ["bridge", "bridge_port", "bridge_vlan", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table", "ethernet", "ip_filter", "ipv6_filter", "routing_rule", "ip_settings", "ipv6_settings", "interface_inventory", "interface_list_members"] as const + +const idPattern = /^\*[a-f0-9]+$/i +const proofPattern = /^[a-f0-9]{64}$/i +const singleton = new Set(["ip_settings", "ipv6_settings"]) +const interfaceResources: Record = { ether: "ethernet", ethernet: "ethernet", bridge: "bridge", vlan: "vlan", wg: "wireguard", wireguard: "wireguard", "gre-tunnel": "gre", "ipip-tunnel": "ipip", "eoip-tunnel": "eoip", vxlan: "vxlan", bond: "bonding", bonding: "bonding", "pppoe-out": "pppoe_client" } +const nativeIdentity = (id: string): string => id.slice(1).replace(/^0+/, "").toLowerCase() || "0" +function requireAdmission(condition: unknown, reason: string): asserts condition { + if (!condition) throw new Error(`VRF production admission refused: ${reason}`) +} +function canonical(value: unknown, depth = 0): string { + requireAdmission(depth <= 32, "nested JSON evidence") + if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return JSON.stringify(value) + if (Array.isArray(value)) return `[${value.map(child => canonical(child, depth + 1)).join(",")}]` + requireAdmission(value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype, "non-JSON evidence") + return `{${Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, child]) => `${JSON.stringify(key)}:${canonical(child, depth + 1)}`).join(",")}}` +} +// Private, purpose-separated fingerprints avoid a public dictionary oracle for +// low-entropy configuration values in a persisted ticket. Tickets remain +// server-private: their authenticated envelope does not encrypt its payload. +type DigestPurpose = "snapshot" | "member" | "parent" | "address" | "ticket" +const hash = (value: unknown, secret: Uint8Array, purpose: DigestPurpose): string => createHmac("sha256", secret).update(`mmapp-vrf-production-${purpose}-digest-v3\0`).update(canonical(value)).digest("hex") +function validateBinding(binding: VrfProductionBinding): void { + requireAdmission(Number.isSafeInteger(binding.serverId) && binding.serverId > 0 && proofPattern.test(binding.sourceProof), "server/source binding") + requireAdmission(typeof binding.actorId === "string" && binding.actorId.length > 0 && binding.actorId.length <= 200 && typeof binding.planId === "string" && binding.planId.length > 0 && binding.planId.length <= 200, "human/plan binding") +} +function validateSnapshot(snapshot: VrfProductionSnapshot, binding: VrfProductionBinding): Record { + validateBinding(binding) + requireAdmission(snapshot.projection === "raw-native-v1" && snapshot.serverId === binding.serverId && snapshot.sourceProof === binding.sourceProof, "raw source changed") + requireAdmission(snapshot.catalogs && typeof snapshot.catalogs === "object" && !Array.isArray(snapshot.catalogs), "missing raw catalogs") + for (const resource of VRF_PRODUCTION_REQUIRED_CATALOGS) requireAdmission(Object.hasOwn(snapshot.catalogs, resource), `missing ${resource} catalog`) + const result: Record = {} + for (const [resource, rows] of Object.entries(snapshot.catalogs)) { + requireAdmission(Array.isArray(rows) && rows.length <= 50000, "catalog cardinality") + const ids = new Set() + result[resource] = rows.map(row => { + requireAdmission(row && typeof row === "object" && !Array.isArray(row), "invalid native row") + if (!singleton.has(resource)) { + requireAdmission(typeof row[".id"] === "string" && idPattern.test(row[".id"]) && !ids.has(nativeIdentity(row[".id"])), "missing/duplicate native ID") + ids.add(nativeIdentity(row[".id"])) + } + canonical(row) + return vrfProductionConfiguration(resource, row) + }) + if (singleton.has(resource)) requireAdmission(rows.length === 1, "missing/ambiguous native settings") + } + return result +} +function members(value: Json | undefined): string[] { + requireAdmission(typeof value === "string", "unknown member selection") + if (value === "none" || value === "") return [] + const names = value.split(",") + requireAdmission(names.length <= 1000 && names.every(name => !["all", "none", "dynamic"].includes(name) && name.length > 0 && name.trim() === name) && new Set(names).size === names.length, "ambiguous member selection") + return names +} +function buildMembers(catalogs: Record, request: VrfProductionRequest, secret: Uint8Array): VrfProductionMember[] { + requireAdmission(["create", "patch", "delete"].includes(request.action) && request.fields && typeof request.fields === "object" && !Array.isArray(request.fields), "invalid request") + requireAdmission(Object.entries(request.fields).every(([key, value]) => ["name", "interfaces", "disabled", "comment"].includes(key) && typeof value === "string"), "unknown requested field") + if (request.fields.name !== undefined) requireAdmission(request.fields.name.length > 0 && request.fields.name.trim() === request.fields.name && !["main", "all", "none"].includes(request.fields.name), "reserved/unknown VRF name") + if (request.fields.disabled !== undefined) requireAdmission(["true", "false", "yes", "no"].includes(request.fields.disabled), "unknown disabled value") + const matches = catalogs.vrf.filter(row => row[".id"] === request.id) + let before: VrfProductionRow | undefined + if (request.action === "create") { + requireAdmission(request.id === undefined && typeof request.fields.name === "string" && request.fields.interfaces !== undefined, "CREATE identity/fields") + requireAdmission(typeof request.placeBeforeId === "string" && catalogs.vrf.some(row => row[".id"] === request.placeBeforeId), "CREATE exact order parent") + requireAdmission(!catalogs.vrf.some(row => row.name === request.fields.name) && !catalogs.routing_table.some(row => row.name === request.fields.name), "existing VRF/table name") + } else { + requireAdmission(typeof request.id === "string" && idPattern.test(request.id) && matches.length === 1 && request.placeBeforeId === undefined, "existing exact target") + before = matches[0] + requireAdmission(before.name !== "main" && ![true, "true", "yes"].includes(before.builtin as string | boolean), "builtin/main target") + requireAdmission(request.action === "delete" ? Object.keys(request.fields).length === 0 : Object.keys(request.fields).length > 0, "empty PATCH/nonempty DELETE") + if (request.fields.name !== undefined) requireAdmission(!catalogs.vrf.some(row => row[".id"] !== request.id && row.name === request.fields.name) && !catalogs.routing_table.some(row => row.name === request.fields.name && row.name !== before!.name), "existing VRF/table name") + } + const selected = new Set([...(before ? members(before.interfaces) : []), ...(request.fields.interfaces !== undefined ? members(request.fields.interfaces) : [])]) + const inventoryNames = new Set() + for (const row of catalogs.interface_inventory) { + requireAdmission(typeof row.name === "string" && row.name.length > 0 && !inventoryNames.has(row.name) && typeof row.type === "string" && row.type.length > 0, "ambiguous interface inventory") + inventoryNames.add(row.name) + } + const closure = (name: string, visiting: Set, result: VrfProductionMember["parents"]): void => { + requireAdmission(visiting.size < 128 && result.length < 1000, "interface closure budget") + requireAdmission(!visiting.has(name), "interface parent cycle") + const inventory = catalogs.interface_inventory.filter(row => row.name === name) + requireAdmission(inventory.length === 1, "missing interface parent") + const row = inventory[0], resource = Object.hasOwn(interfaceResources, String(row.type)) ? interfaceResources[String(row.type)] : undefined + requireAdmission(resource && Object.hasOwn(catalogs, resource), "missing type-specific interface catalog") + const configurations = catalogs[resource].filter(item => item.name === name) + requireAdmission(configurations.length === 1, "ambiguous type-specific interface parent") + const config = configurations[0] + if (result.some(item => item.id === row[".id"])) return + visiting.add(name) + result.push({ id: String(row[".id"]), name, resource, configurationId: String(config[".id"]), configurationHash: hash(config, secret, "parent") }) + if (resource === "wireguard") { + requireAdmission(Object.hasOwn(catalogs, "wireguard_peer"), "missing WireGuard peer catalog") + for (const peer of catalogs.wireguard_peer.filter(item => item.interface === name)) { + const endpoint = peer["endpoint-address"] + requireAdmission(endpoint === undefined || endpoint === "" || typeof endpoint === "string" && isIP(endpoint) > 0, "unresolved WireGuard endpoint") + } + } + if (resource === "vxlan") { + requireAdmission(Object.hasOwn(catalogs, "vxlan_vtep"), "missing VXLAN VTEP catalog") + for (const vtep of catalogs.vxlan_vtep.filter(item => item.interface === name)) requireAdmission(typeof vtep["remote-ip"] === "string" && isIP(vtep["remote-ip"]) > 0, "unresolved VXLAN VTEP") + } + if (resource === "vlan") requireAdmission(members(config.interface).length === 1, "missing VLAN parent") + if (resource === "bonding") requireAdmission(members(config.slaves).length > 0, "missing bonding slaves") + if (resource === "pppoe_client") requireAdmission(members(config.interface).length === 1, "missing PPPoE parent") + if (["gre", "ipip", "eoip", "vxlan"].includes(resource)) { + const local = config["local-address"] + requireAdmission(typeof local === "string" && isIP(local) > 0 && !["0.0.0.0", "::"].includes(local), "unbound tunnel local source") + if (resource !== "vxlan") requireAdmission(typeof config["remote-address"] === "string" && isIP(config["remote-address"]) > 0, "unresolved tunnel endpoint") + const bound = [...catalogs.ipv4_address, ...catalogs.ipv6_address].filter(item => typeof item.address === "string" && item.address.split("/")[0] === local) + requireAdmission(bound.length === 1 && typeof bound[0].interface === "string" && [false, "false", "no"].includes(bound[0].dynamic as string | boolean), "ambiguous/dynamic tunnel source parent") + closure(bound[0].interface, visiting, result) + } + for (const field of ["interface", "interfaces", "slaves", "bridge", "master-interface"]) { + const value = config[field] + if (value === undefined || value === "" || value === "none") continue + for (const parent of members(value)) closure(parent, visiting, result) + } + // A bridge's port/slave configuration is sealed as well as its row. + if (resource === "bridge") { + for (const port of catalogs.bridge_port.filter(item => item.bridge === name)) { + const parents = members(port.interface) + requireAdmission(parents.length === 1, "unknown bridge port parent") + closure(parents[0], visiting, result) + } + for (const vlan of catalogs.bridge_vlan.filter(item => item.bridge === name)) for (const field of ["tagged", "untagged", "current-tagged", "current-untagged"]) { + if (vlan[field] !== undefined) for (const parent of members(vlan[field])) if (parent !== name) closure(parent, visiting, result) + } + } + visiting.delete(name) + } + // Human-selected existing production interfaces are references, not ACK-owned + // fixture objects. Physical interfaces and arbitrary static prefixes are valid + // preconditions; no bridge marker, /32 or /128 restriction is imposed here. + return [...selected].sort().map(name => { + const rows = catalogs.interface_inventory.filter(row => row.name === name) + requireAdmission(rows.length === 1, "missing selected interface") + const row = rows[0], id = String(row[".id"]) + const parents: VrfProductionMember["parents"] = [] + closure(name, new Set(), parents) + const addresses: VrfProductionMember["addresses"] = [] + const parentIds = new Map(parents.map(parent => [parent.name, parent.id])) + for (const resource of ["ipv4_address", "ipv6_address"] as const) for (const address of catalogs[resource].filter(item => typeof item.interface === "string" && parentIds.has(item.interface))) { + requireAdmission(typeof address.address === "string", "unknown address") + const [host, length, extra] = address.address.split("/"), family = resource === "ipv4_address" ? 4 : 6 + requireAdmission(extra === undefined && isIP(host) === family && /^\d+$/.test(length ?? "") && Number(length) <= (family === 4 ? 32 : 128), "invalid address prefix") + addresses.push({ resource, id: String(address[".id"]), parentId: parentIds.get(String(address.interface))!, configurationHash: hash(address, secret, "address") }) + } + return { id, name, configurationHash: hash(row, secret, "member"), parents, addresses } + }) +} +function key(secret: Uint8Array): void { requireAdmission(secret instanceof Uint8Array && secret.byteLength >= 32, "weak sealing key") } +function signature(payload: string, secret: Uint8Array): string { return createHmac("sha256", secret).update("mmapp-vrf-production-admission-v3\0").update(payload).digest("hex") } + +/** Trusted prepare path only: authenticate the human and validate permission + * before calling. Persist the returned ticket ID/digest with the plan in the + * same durable transaction. A caller-provided sourceProof is never authority. */ +export function sealVrfProductionAdmission(input: { snapshot: VrfProductionSnapshot; binding: VrfProductionBinding; request: VrfProductionRequest; secret: Uint8Array; now: number; ttlMs?: number }): { sealed: string; admission: VrfProductionAdmission; digest: string } { + key(input.secret) + const ttl = input.ttlMs ?? 300000 + requireAdmission(Number.isSafeInteger(input.now) && input.now >= 0 && Number.isSafeInteger(ttl) && ttl > 0 && ttl <= 300000 && Number.isSafeInteger(input.now + ttl), "invalid ticket lifetime") + const catalogs = validateSnapshot(input.snapshot, input.binding) + const selected = buildMembers(catalogs, input.request, input.secret) + const admission: VrfProductionAdmission = { version: 3, ticketId: randomUUID(), issuedAt: input.now, expiresAt: input.now + ttl, binding: structuredClone(input.binding), request: structuredClone(input.request), snapshotHash: hash(catalogs, input.secret, "snapshot"), members: selected, companionExecution: "requires-native-transition-proof" } + const payload = Buffer.from(canonical(admission)).toString("base64url") + return { sealed: `${payload}.${signature(payload, input.secret)}`, admission, digest: hash(admission, input.secret, "ticket") } +} + +/** Read-only validation. Re-reading cannot refresh or adopt new IDs/config. */ +export function inspectVrfProductionAdmission(input: { sealed: string; secret: Uint8Array; snapshot: VrfProductionSnapshot; binding: VrfProductionBinding; request: VrfProductionRequest; now: number }): { admission: VrfProductionAdmission; digest: string } { + key(input.secret) + requireAdmission(typeof input.sealed === "string" && input.sealed.length <= 2000000, "invalid sealed ticket") + const [payload, mac, extra] = input.sealed.split(".") + requireAdmission(extra === undefined && /^[A-Za-z0-9_-]+$/.test(payload ?? "") && proofPattern.test(mac ?? ""), "invalid sealed envelope") + requireAdmission(timingSafeEqual(Buffer.from(mac, "hex"), Buffer.from(signature(payload, input.secret), "hex")), "ticket signature changed") + const admission = JSON.parse(Buffer.from(payload, "base64url").toString("utf8")) as VrfProductionAdmission + requireAdmission(admission.version === 3 && admission.companionExecution === "requires-native-transition-proof" && typeof admission.ticketId === "string" && /^[a-f0-9-]{36}$/.test(admission.ticketId), "unknown ticket schema") + requireAdmission(Number.isSafeInteger(input.now) && Number.isSafeInteger(admission.issuedAt) && Number.isSafeInteger(admission.expiresAt) && input.now >= admission.issuedAt && input.now < admission.expiresAt && admission.expiresAt - admission.issuedAt <= 300000, "expired/future ticket") + requireAdmission(canonical(input.binding) === canonical(admission.binding) && canonical(input.request) === canonical(admission.request), "human/plan/source/request changed") + const catalogs = validateSnapshot(input.snapshot, input.binding) + requireAdmission(hash(catalogs, input.secret, "snapshot") === admission.snapshotHash && canonical(buildMembers(catalogs, input.request, input.secret)) === canonical(admission.members), "raw configuration/parent closure changed") + return { admission, digest: hash(admission, input.secret, "ticket") } +} + +export interface VrfProductionTicketStore { + /** Atomic durable CAS: issued -> claimed, bound to this exact digest. Must + * return false for unknown IDs, consumed IDs, another digest, or ambiguity. + * A claimed ticket stays spent on timeout/failure; never reissue a native call. */ + claimIssued(ticketId: string, digest: string): Promise +} + +/** Burns the durable ticket for a review/proof attempt. This is NOT permission + * to mutate: the caller still needs a supported native transition classifier, + * ACK identity proof, full post-read conservation and journaled executor. */ +export async function claimVrfProductionAdmission(input: Parameters[0], store: VrfProductionTicketStore): Promise> { + const result = inspectVrfProductionAdmission(input) + requireAdmission(await store.claimIssued(result.admission.ticketId, result.digest) === true, "unknown/replayed ticket") + return result +} diff --git a/backend/src/services/vrf-production-bridge-delete.test.ts b/backend/src/services/vrf-production-bridge-delete.test.ts new file mode 100644 index 00000000..f6d3ec60 --- /dev/null +++ b/backend/src/services/vrf-production-bridge-delete.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { createHmac } from "node:crypto" +import { sealVrfProductionAdmission, VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionSnapshot, type VrfProductionRow as Row } from "./vrf-production-admission.js" +import { classifyVrfProductionTransition, sealVrfProductionTransitionCheckpoint, openVrfProductionTransitionCheckpoint, assertVrfProductionHistoryProvenance } from "./vrf-production-transition.js" + +const fixture = JSON.parse(await readFile(new URL("./fixtures/vrf-bridge-delete-ll-v33.json", import.meta.url), "utf8")) +const secret = Buffer.alloc(32, 9), now = 1000, sourceProof = "a".repeat(64) +const binding = { serverId: 2, sourceProof, actorId: "human", planId: "bridge-delete-synthetic-graph" } +const name = fixture.before.bridge[0].name, request = { action: "delete" as const, id: "*1", fields: {} } +let cases = 0 +function snapshot(which: "before" | "after"): VrfProductionSnapshot { + const catalogs: Record = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(key => [key, []])) + Object.assign(catalogs, structuredClone(fixture[which])) + // These surroundings and single selected member are reconstructed. Captured + // bridge/address/route/virtual shapes above are not a full native raw17 pair. + for (const vrf of catalogs.vrf) vrf.interfaces = name + catalogs.vrf.push({ ".id": "*0", name: "main", interfaces: "all", disabled: "false", builtin: "true" }) + catalogs.routing_table.unshift({ ".id": "*0", name: "main", fib: "true", dynamic: "true" }) + catalogs.ip_settings = [{}]; catalogs.ipv6_settings = [{}] + return { projection: "raw-native-v1", serverId: 2, sourceProof, catalogs } +} +const before = snapshot("before"), after = snapshot("after") +function classify(b = before, a = after, at = now) { + const issued = sealVrfProductionAdmission({ snapshot: b, binding, request, secret, now }) + return classifyVrfProductionTransition({ before: b, after: a, binding, request, secret, now: at, sealed: issued.sealed }) +} +const result = classify() +assert.equal(result.status, "explained-native-transition"); assert.equal(result.forwardingVerified, false); cases++ +const scope = { serverId: 2, sourceProof, actorId: binding.actorId, nativeId: "*1" } +const sealed = sealVrfProductionTransitionCheckpoint(result.checkpoint, scope, secret) +const durable = JSON.parse(Buffer.from(sealed.split(".")[0], "base64url").toString()) +assert.deepEqual(durable.history.absentLl, []); assert.deepEqual(durable.history.provenance.missingMembers, []) +assertVrfProductionHistoryProvenance(openVrfProductionTransitionCheckpoint(sealed, scope, secret), after); cases++ +// Compatible v1 EUI64 history also has no invented revival debt. It retains the +// original proof kind/domain and is not silently upgraded when opened. +durable.version = 1; delete durable.history.provenance.greMembers +const payload = Buffer.from(JSON.stringify(durable)).toString("base64url") +const signature = createHmac("sha256", secret).update("mmapp-vrf-production-transition-history-v1\0").update(payload).digest("hex") +const legacy = openVrfProductionTransitionCheckpoint(payload + "." + signature, scope, secret) +assertVrfProductionHistoryProvenance(legacy, after); cases++ +const inv = (s: VrfProductionSnapshot) => s.catalogs.interface_inventory.find(row => row.name === name)! +const ll = (s: VrfProductionSnapshot) => s.catalogs.ipv6_address[0] +const refuse = (mutate: (b: VrfProductionSnapshot, a: VrfProductionSnapshot) => void) => { + const b = structuredClone(before), a = structuredClone(after); mutate(b, a) + assert.throws(() => classify(b, a), /VRF production/); cases++ +} +for (const mutate of [ + (_b, a) => { inv(a)["mac-address"] = "82:97:C3:74:4C:A6" }, + (_b, a) => { inv(a)[".id"] = "*ffff" }, + (_b, a) => { inv(a).disabled = "true" }, + (_b, a) => { inv(a).running = "false" }, + (_b, a) => { inv(a)["link-downs"] = "1" }, + (_b, a) => { a.catalogs.bridge[0].opaque = "configuration drift" }, + (b, a) => { for (const s of [b, a]) s.catalogs.bridge_port.push({ ".id": "*fe1", bridge: name, interface: "ether-root" }) }, + (b, a) => { for (const s of [b, a]) s.catalogs.bridge_vlan.push({ ".id": "*fe1", bridge: name, "vlan-ids": "1" }) }, + (b, _a) => { ll(b).dynamic = "false" }, + (b, _a) => { ll(b)["auto-link-local"] = "false" }, + (b, _a) => { ll(b)["link-local"] = "false" }, + (b, _a) => { ll(b).invalid = "true" }, + (b, _a) => { ll(b).disabled = "true" }, + (b, _a) => { ll(b)["actual-interface"] = "foreign-parent" }, + (b, _a) => { ll(b).address = "fe80::1/64" }, + (b, _a) => { ll(b).address = "fe80::8097:c3ff:fe74:4ca5/128" }, + (b, _a) => { b.catalogs.ipv6_address.push({ ...ll(b), ".id": "*feed" }) }, + (_b, a) => { a.catalogs.ipv6_route = structuredClone(before.catalogs.ipv6_route) }, + (_b, a) => { a.catalogs.ipv6_route.push({ ...before.catalogs.ipv6_route[0], ".id": "*ffff", protocol: "foreign" }) }, + (_b, a) => { a.catalogs.ipv4_address = [] }, + (_b, a) => { a.catalogs.ipv4_address[0][".id"] = "*ffff" }, + (_b, a) => { a.catalogs.ip_filter.push({ ".id": "*ffff", action: "drop" }) }, + (_b, a) => { a.sourceProof = "b".repeat(64) }, +] as Array<(b: VrfProductionSnapshot, a: VrfProductionSnapshot) => void>) refuse(mutate) +// A real MAC/EUI64 on another concrete type is insufficient for this DELETE +// exception. Existing physical/VLAN/GRE behavior must keep refusing absence. +for (const [type, resource] of [["ether", "ethernet"], ["vlan", "vlan"], ["gre-tunnel", "gre"]]) refuse((b, a) => { + for (const s of [b, a]) { inv(s).type = type; s.catalogs[resource] = [{ ...s.catalogs.bridge[0] }]; s.catalogs.bridge = [] } +}) +assert.throws(() => classify(before, after, now + 600001), /VRF production/); cases++ +console.log("Bridge automatic LL DELETE: captured subset, synthetic admission/classifier, checkpoint and strict negatives:", cases) diff --git a/backend/src/services/vrf-production-execution.ts b/backend/src/services/vrf-production-execution.ts new file mode 100644 index 00000000..b7c8f580 --- /dev/null +++ b/backend/src/services/vrf-production-execution.ts @@ -0,0 +1,238 @@ +import { pool } from "../db/index.js" +import { readVrfProductionSnapshot } from "./vrf-production-read.js" +import { claimStoredVrfProductionTicket, type VrfTicketContext, type VrfTicketSql } from "./vrf-production-ticket-store.js" +import { inspectVrfProductionAdmission, type VrfProductionRequest, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { assertVrfProductionHistoryProvenance, assertVrfProductionTransitionCheckpoint, classifyVrfProductionTransition, openVrfProductionTransitionCheckpoint, sealVrfProductionTransitionCheckpoint, vrfProductionCheckpointCatalogs, type VrfProductionTransitionCheckpoint } from "./vrf-production-transition.js" +import { assertVerifiedConfigurationAccess } from "./config-mutation-policy.js" +import { withVrfClaimQuarantine } from "../modules/mcp/vrf-ticket-runtime.js" +import { assertVrfActionContext, vrfNativeActionFor, vrfActionContractsCurrent, type VrfAuthorityActionId } from "./vrf-production-action-binding.js" +import { findMcpOperation } from "../modules/mcp/registry.js" +import { decryptSecretExisting, encryptSecret, fingerprint, McpError, withVrfProductionSealingKey } from "../modules/mcp/security.js" +import { configurationRows, resourceById, routerClient, rosSnapshotSourceProof, type RosSnapshot, type RosStep } from "../modules/mcp/resources.js" +import { notifyVrfDiagnostic, vrfExecutionDiagnostic, type VrfExecutionDiagnostic, type VrfExecutionStage, type VrfRawReadObservation } from "./vrf-execution-diagnostic.js" + +export interface VrfProductionExecutionInput { + operationId: string + planId: string + connectionId: string + ownerId: string + operationName: VrfAuthorityActionId + operationVersion: number + stepIndex: number + step: RosStep + sourceProof: string + originalSnapshots: RosSnapshot[] + /** Existing runtime authority/original grant/policy boundary. */ + authorize(): Promise + /** All original sealed dependency snapshots not explained as companions. */ + assertDependencies(): Promise + /** The existing native policy invokes this AFTER configuration.intent and + * before actual HTTP. The runtime owns the closure; never a JSON field. */ + installBeforeDispatch(guard: () => Promise): void + /** Private in-process observation only; never authority or public evidence. */ + onDiagnostic?(value: VrfExecutionDiagnostic): unknown +} +export interface VrfProductionExecutionReceipt { + kind: "vrf-production-execution-v1" + action: VrfProductionRequest["action"] + nativeId: string + configurationVerified: true + forwardingVerified: false +} +export class VrfProductionAcknowledgedFailure extends McpError { + readonly acknowledgedWrites = 1 + constructor(error: unknown) { super(error instanceof McpError ? error.code : "VERIFICATION_FAILED", "VRF native ACK получен, но финальная проверка не завершена; повтор запрещён", 409) } +} +const companionResources = new Set(["vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table"]) +export const isVrfCompanionResource = (resource: string) => companionResources.has(resource) +function insist(value: unknown, message: string): asserts value { if (!value) throw new McpError("STATE_CONFLICT", message, 409) } +const yes = (value: unknown) => [true, "true", "yes"].includes(value as string | boolean) +export async function loadVerifiedVrfProductionHistory(input: { sql: VrfTicketSql; ownerId: string; serverId: number; sourceProof: string; nativeId?: string; secret: Uint8Array }): Promise { + if (!input.nativeId) return undefined + const history = await input.sql.query(`SELECT cp.after_sealed FROM mcp_operation_checkpoints cp + JOIN mcp_operation_steps st ON st.operation_id=cp.operation_id AND st.step_index=cp.step_index + JOIN mcp_operations o ON o.id=st.operation_id JOIN mcp_connections c ON c.id=o.connection_id + WHERE o.state='applied' AND st.state='verified' AND c.owner_id=$1 + AND st.server_id=$2 AND st.result->>'kind'='vrf-production-execution-v1' + AND st.result->>'nativeId'=$3 AND cp.after_sealed IS NOT NULL + ORDER BY o.finished_at DESC,o.id DESC LIMIT 1`, [input.ownerId, input.serverId, input.nativeId]) + if (!history.rows[0]) return undefined + const value = JSON.parse(await decryptSecretExisting(history.rows[0].after_sealed as string)) as { kind: string; sealed: string } + insist(value.kind === "vrf-production-private-checkpoint-v1", "Неверная VRF private checkpoint provenance") + return openVrfProductionTransitionCheckpoint(value.sealed, { serverId: input.serverId, sourceProof: input.sourceProof, actorId: input.ownerId, nativeId: input.nativeId }, input.secret) +} +/** Read both old and new concrete closures, including after transfer/delete. + * This synthetic CREATE selection is read-only and never an execution request. */ +export function vrfProductionReadSelection(step: RosStep, oldMembers: unknown): VrfProductionRequest { + const names = new Set() + for (const value of [oldMembers, step.fields.interfaces]) { + if (value === undefined || value === "none" || value === "") continue + insist(typeof value === "string", "Неизвестный исходный состав VRF") + for (const name of value.split(",")) { insist(name.length > 0 && name.trim() === name && !["all", "none", "dynamic"].includes(name), "Неоднозначный состав VRF"); names.add(name) } + } + return { action: "create", fields: { name: "__read_only_vrf_closure__", interfaces: [...names].join(",") || "none" } } +} + +/** No mutation/retry authority. Only an authenticated, verified private + * checkpoint can produce a boolean whole-companion comparison. */ +export async function reconcileVrfProductionStep(input: { step: RosStep; ownerId: string; sourceProof: string; oldMembers: unknown; verifiedNativeId?: string; privateCheckpoint?: string; authorize(): Promise }): Promise<{ matchesVerifiedCheckpoint: boolean | null; checkpointAvailable: boolean }> { + insist(input.step.resource === "vrf", "Неверный typed VRF reconcile") + return withVrfProductionSealingKey(async secret => { + const checkpoint = input.privateCheckpoint && input.verifiedNativeId ? openVrfProductionTransitionCheckpoint(input.privateCheckpoint, { serverId: input.step.serverId, sourceProof: input.sourceProof, actorId: input.ownerId, nativeId: input.verifiedNativeId }, secret) : undefined + const current = await readVrfProductionSnapshot({ serverId: input.step.serverId, expectedSourceProof: input.sourceProof, request: vrfProductionReadSelection(input.step, input.oldMembers), requiredCatalogs: checkpoint ? vrfProductionCheckpointCatalogs(checkpoint) : undefined, assertAuthority: input.authorize }) + if (!checkpoint) return { matchesVerifiedCheckpoint: null, checkpointAvailable: false } + let matches = true + try { assertVrfProductionTransitionCheckpoint(checkpoint, current) } catch { matches = false } + await input.authorize() + return { matchesVerifiedCheckpoint: matches, checkpointAvailable: true } + }) +} + +/** Known transition capability, checked before dispatch. Raw post-read remains + * authoritative. This does not turn unsupported interface/default shapes into + * permission merely because the human selected productionAdmission. */ +function supported(before: VrfProductionSnapshot, request: VrfProductionRequest): void { + const target = request.id ? before.catalogs.vrf.find(row => row[".id"] === request.id) : undefined + if (target && request.fields.disabled !== undefined && !yes(request.fields.disabled)) insist(!before.catalogs.routing_table.some(row => row.name === target.name && row[".id"] !== target[".id"]), "Имя включаемой VRF занято посторонней таблицей") + const names = new Set([String(target?.interfaces ?? "none"), request.fields.interfaces ?? "none"].flatMap(value => value === "none" ? [] : value.split(","))) + const changesContext = request.action !== "patch" || request.fields.name !== undefined || request.fields.interfaces !== undefined || request.fields.disabled !== undefined + if (changesContext) for (const name of names) { + const rows = before.catalogs.interface_inventory.filter(row => row.name === name) + insist(rows.length === 1 && typeof rows[0].vrf === "string", "Нет однозначной native context identity выбранного интерфейса") + } +} + +/** Existing operation/step intent is required independently of the caller's + * callback. A replay, unknown step, sibling operation or raw native dispatcher + * cannot enter this executor as a fresh command. */ +async function assertIntent(input: VrfProductionExecutionInput, acknowledged = false): Promise { + const { rows } = await pool.query(`SELECT o.id FROM mcp_operations o + JOIN mcp_plans p ON p.id=o.plan_id JOIN mcp_connections c ON c.id=o.connection_id + JOIN mcp_operation_steps st ON st.operation_id=o.id + WHERE o.id=$1 AND p.id=$2 AND c.id=$3 AND c.owner_id=$4 AND o.state='running' + AND p.state='executing' AND p.approved_by IS NOT NULL AND p.operation_id=$5 AND p.operation_version=$6 + AND p.expires_at>clock_timestamp() AND st.step_index=$7 AND st.server_id=$8 AND st.state=$9`, + [input.operationId, input.planId, input.connectionId, input.ownerId, input.operationName, input.operationVersion, input.stepIndex, input.step.serverId, acknowledged ? "applied" : "intent"]) + insist(rows.length === 1, "Exact running VRF operation intent не подтверждён; повтор запрещён") +} + +export async function executeVrfProductionStep(original: VrfProductionExecutionInput): Promise { + const input = { ...original, step: structuredClone(original.step), originalSnapshots: structuredClone(original.originalSnapshots) } + insist(input.step.resource === "vrf" && input.stepIndex === 0, "Неоднозначный typed VRF executor") + const request: VrfProductionRequest = { action: input.step.action === "update" ? "patch" : input.step.action, ...(input.step.id ? { id: input.step.id } : {}), fields: input.step.fields, ...(input.step.placeBeforeId ? { placeBeforeId: input.step.placeBeforeId } : {}) } + const binding = { serverId: input.step.serverId, sourceProof: input.sourceProof, actorId: input.ownerId, planId: input.planId } + const actionId = vrfNativeActionFor(input.operationName, request) + const context: VrfTicketContext = { binding, connectionId: input.connectionId, actionId, + actionVersion: input.operationName === "routing.vrf.update" ? Number(findMcpOperation(actionId)?.version) : input.operationVersion, + ...(input.operationName === "routing.vrf.update" ? { authorityAction: { id: "routing.vrf.update" as const, version: input.operationVersion as 4 } } : {}) } + assertVrfActionContext(context, request) + const assertSource = async () => { + await input.authorize() + const current = await routerClient(binding.serverId) + insist(await rosSnapshotSourceProof(current.server) === binding.sourceProof, "VRF source/endpoint epoch изменился") + await input.authorize() + } + let acknowledged = false + let executorAckObserved = false, diagnosticReported = false + let stage: VrfExecutionStage = "initial-authorization", read: VrfRawReadObservation | undefined + const mark = (next: VrfExecutionStage) => { stage = next; read = undefined } + const observeRead = (value: VrfRawReadObservation) => { read = value } + const report = (error: unknown) => { + if (diagnosticReported) return + diagnosticReported = true + try { + const diagnostic = vrfExecutionDiagnostic(input.operationId, request.action, stage, error, executorAckObserved, read) + if (diagnostic) notifyVrfDiagnostic(input.onDiagnostic, diagnostic) + } catch { /* Original exception and acknowledgement handling stay authoritative. */ } + } + const authorize = async () => { await assertVerifiedConfigurationAccess(); await input.authorize(); if (!vrfActionContractsCurrent(context)) throw new McpError("PLAN_EXPIRED", "Native/public VRF contract изменился; повтор запрещён", 409); await assertIntent(input, acknowledged); await assertSource() } + try { + await authorize() + mark("sealing-key") + return await withVrfProductionSealingKey(async secret => { + mark("history-load") + const previous = await loadVerifiedVrfProductionHistory({ sql: pool, ownerId: input.ownerId, serverId: binding.serverId, sourceProof: binding.sourceProof, nativeId: request.id, secret }) + mark("raw-before") + const before = await readVrfProductionSnapshot({ serverId: binding.serverId, expectedSourceProof: binding.sourceProof, request, assertAuthority: authorize, observeRead }) + mark("original-dependencies") + for (const original of input.originalSnapshots) { + if (before.catalogs[original.resource] === undefined) continue + insist(original.serverId === binding.serverId && original.sourceProof === binding.sourceProof && fingerprint(configurationRows(resourceById(original.resource), before.catalogs[original.resource])) === original.fingerprint, "Original VRF dependency projection изменилась; новый raw ticket не разрешает adoption") + } + mark("history-provenance") + if (previous) assertVrfProductionHistoryProvenance(previous, before) + mark("supported-shape") + supported(before, request) + mark("ticket-lookup") + const tickets = await pool.query<{ ticket_id: string }>("SELECT ticket_id FROM vrf_production_tickets WHERE plan_id=$1 AND connection_id=$2", [input.planId, input.connectionId]) + insist(tickets.rows.length === 1, "Нет единственного authentic VRF preparation ticket") + mark("ticket-claim") + const claimed = await withVrfClaimQuarantine({ operationId: input.operationId, planId: input.planId, connectionId: input.connectionId, ownerId: input.ownerId, journal: pool, + claim: () => claimStoredVrfProductionTicket({ context, request, secret, execution: { operationId: input.operationId, stepIndex: input.stepIndex }, sql: pool, ticketId: tickets.rows[0].ticket_id, snapshot: before, assertAuthority: authorize, assertCurrentSource: assertSource }) }) + insist(typeof claimed.privateSealed === "string", "Нет authentic private VRF admission") + const sealed = claimed.privateSealed + mark("checkpoint-before") + const beforeJournal = await pool.query("UPDATE mcp_operation_checkpoints SET before_sealed=$3 WHERE operation_id=$1 AND step_index=$2 RETURNING operation_id", [input.operationId, input.stepIndex, await encryptSecret(JSON.stringify({ kind: "vrf-production-before-v1", before }))]) + insist(beforeJournal.rows.length === 1, "Исходный private VRF checkpoint не подтверждён; dispatch запрещён") + const preDispatch = async (installed = false) => { + const prefix = installed ? "installed" : "explicit" + mark(`${prefix}-predispatch-authorization`) + await authorize() + mark(`${prefix}-predispatch-dependencies`) + await input.assertDependencies() + mark(`${prefix}-predispatch-read`) + const current = await readVrfProductionSnapshot({ serverId: binding.serverId, expectedSourceProof: binding.sourceProof, request, requiredCatalogs: Object.keys(before.catalogs), assertAuthority: authorize, observeRead }) + mark(`${prefix}-predispatch-admission`) + inspectVrfProductionAdmission({ snapshot: current, binding, request, sealed, secret, now: Date.now() }) + mark(`${prefix}-predispatch-final-authorization`) + await authorize() + if (installed) mark("client-call") + } + mark("install-predispatch") + input.installBeforeDispatch(() => preDispatch(true)) + await preDispatch() + mark("dispatch-source") + const { client, server } = await routerClient(binding.serverId) + insist(await rosSnapshotSourceProof(server) === binding.sourceProof, "VRF dispatch source изменился") + const path = "/ip/vrf" + (request.id ? "/" + encodeURIComponent(request.id) : "") + let deleteAck: unknown + mark("client-call") + const ack = request.action === "create" ? await client.put(path, { ...request.fields, ...(request.placeBeforeId ? { "place-before": request.placeBeforeId } : {}) }) + : request.action === "delete" ? (await client.delete(path, 10000, undefined, body => { + acknowledged = true + executorAckObserved = true + mark("ack-identity") + try { deleteAck = body.trim() ? JSON.parse(body) : undefined } catch { throw new McpError("VERIFICATION_FAILED", "Native VRF DELETE ACK имеет неизвестный body; повтор запрещён", 409) } + }), deleteAck) : await client.patch(path, request.fields) + executorAckObserved = true + mark("ack-identity") + const acknowledgedId = request.action === "create" && ack && typeof ack === "object" ? (ack as Record)[".id"] : request.id + insist(typeof acknowledgedId === "string", "Нет точного native ACK ID; повтор запрещён") + // Receipt is already observed even if the following durable journal fails. + acknowledged = true + if (request.action !== "create" && ack && typeof ack === "object" && Object.hasOwn(ack, ".id") && (ack as Record)[".id"] !== request.id) throw new McpError("VERIFICATION_FAILED", "Native VRF ACK содержит противоречащий target ID; повтор запрещён", 409) + mark("ack-journal") + try { + const ackJournal = await pool.query("UPDATE mcp_operation_steps SET state='applied',result=$3::jsonb,finished_at=clock_timestamp() WHERE operation_id=$1 AND step_index=$2 AND state='intent' RETURNING operation_id", [input.operationId, input.stepIndex, JSON.stringify({ kind: "vrf-production-ack-v1", nativeId: acknowledgedId, action: request.action })]) + insist(ackJournal.rows.length === 1, "VRF ACK journal не подтверждён") + } catch (error) { report(error); throw new McpError("VRF_ACK_JOURNAL_UNCONFIRMED", "Native VRF ACK получен, durable journal не подтверждён; сверка без повтора", 409) } + mark("post-ack-authorization") + await authorize() + mark("raw-after") + const after = await readVrfProductionSnapshot({ serverId: binding.serverId, expectedSourceProof: binding.sourceProof, request: vrfProductionReadSelection(input.step, before.catalogs.vrf.find(row => row[".id"] === request.id)?.interfaces), requiredCatalogs: Object.keys(before.catalogs), assertAuthority: authorize, observeRead }) + mark("transition-classification") + const classified = classifyVrfProductionTransition({ before, after, binding, request, sealed, secret, now: Date.now(), acknowledgedId, previous }) + mark("post-ack-dependencies") + await input.assertDependencies() + mark("post-ack-final-authorization") + await authorize() + mark("checkpoint-after") + try { + const checkpoint = sealVrfProductionTransitionCheckpoint(classified.checkpoint, { serverId: binding.serverId, sourceProof: binding.sourceProof, actorId: binding.actorId, nativeId: acknowledgedId }, secret) + const persisted = await pool.query("UPDATE mcp_operation_checkpoints SET after_sealed=$3 WHERE operation_id=$1 AND step_index=$2 RETURNING operation_id", [input.operationId, input.stepIndex, await encryptSecret(JSON.stringify({ kind: "vrf-production-private-checkpoint-v1", sealed: checkpoint }))]) + insist(persisted.rows.length === 1, "Verified private VRF checkpoint не подтверждён") + } catch (error) { report(error); throw new McpError("VRF_VERIFIED_CHECKPOINT_UNCONFIRMED", "Native VRF ACK получен, private checkpoint не подтверждён; сверка без повтора", 409) } + const receipt: VrfProductionExecutionReceipt = { kind: "vrf-production-execution-v1", action: request.action, nativeId: acknowledgedId, configurationVerified: true, forwardingVerified: false } + return receipt + }) } catch (error) { report(error); if (acknowledged) throw new VrfProductionAcknowledgedFailure(error); throw error } +} diff --git a/backend/src/services/vrf-production-gre-cycle.test.mjs b/backend/src/services/vrf-production-gre-cycle.test.mjs new file mode 100644 index 00000000..f38ccd99 --- /dev/null +++ b/backend/src/services/vrf-production-gre-cycle.test.mjs @@ -0,0 +1,64 @@ +import assert from 'node:assert/strict' +import {readFile} from 'node:fs/promises' +import {createHmac} from 'node:crypto' +import ts from 'typescript' + +// Pure production modules only: no SQL/HTTP authority adapters are imported. +const cache=new Map() +async function moduleUri(name){ + if(cache.has(name))return cache.get(name) + const loading=(async()=>{ + let code=ts.transpileModule(await readFile(new URL(name,import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ES2022}}).outputText + const ast=ts.createSourceFile(name,code,ts.ScriptTarget.ES2022,true) + const imports=ast.statements.filter(ts.isImportDeclaration).filter(node=>node.moduleSpecifier.text.startsWith('./')) + const edits=await Promise.all(imports.map(async node=>({start:node.moduleSpecifier.getStart(ast),end:node.moduleSpecifier.end,value:await moduleUri(node.moduleSpecifier.text.replace(/\.js$/,'.ts'))}))) + for(const edit of edits.reverse())code=code.slice(0,edit.start)+JSON.stringify(edit.value)+code.slice(edit.end) + if(name==='./vrf-production-transition.ts')code+='\nexport { memberInventoryOmissions, same };\n' + return'data:text/javascript;base64,'+Buffer.from(code).toString('base64') + })();cache.set(name,loading);return loading +} +const a=await import(await moduleUri('./vrf-production-admission.ts')),t=await import(await moduleUri('./vrf-production-transition.ts')),p=await import(await moduleUri('./vrf-production-projection.ts')) + +const pair=JSON.parse(await readFile(new URL('./fixtures/vrf-gre-inverse-cycle-v32.json',import.meta.url),'utf8')) +let cases=0 +const gre=pair.before.catalogs.interface_inventory.filter(row=>row.type==='gre-tunnel') +const rows=(before,after,name)=>[before.catalogs.interface_inventory.find(row=>row.name===name),after.catalogs.interface_inventory.find(row=>row.name===name)] +const selectedBoundary=(before,after,name)=>{const [old,current]=rows(before,after,name);const omit=t.memberInventoryOmissions(before,after,name,old,current);t.same('interface_inventory',old,current,omit);return omit} +for(const old of gre){ + const [a,b]=rows(pair.before,pair.after,old.name) + assert.deepEqual(selectedBoundary(pair.before,pair.after,old.name),['vrf','status','link-downs','last-link-up-time','last-link-down-time']);cases++ +} +// Model forward zero→one separately. This is NOT an actual captured forward pair. +const forwardBefore=structuredClone(pair.after),forwardAfter=structuredClone(pair.before),name=gre[0].name +const [fa,fb]=rows(forwardBefore,forwardAfter,name);fa['link-downs']='0';delete fa['last-link-down-time'];fa['last-link-up-time']='2026-11-05 19:00:00';fb['link-downs']='1';fb['last-link-down-time']='2026-11-05 19:42:15';fb['last-link-up-time']='2026-11-05 19:42:15' +assert.equal(selectedBoundary(forwardBefore,forwardAfter,name).length,5);cases++ +for(const mutate of [ + (a,b)=>{delete b.catalogs.interface_inventory.find(r=>r.name===name)['link-downs']}, + (a,b)=>{delete a.catalogs.interface_inventory.find(r=>r.name===name)['last-link-up-time']}, + (a,b)=>{delete b.catalogs.interface_inventory.find(r=>r.name===name)['last-link-down-time']}, + (a,b)=>{delete b.catalogs.interface_inventory.find(r=>r.name===name)['last-link-up-time']}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['link-downs']='0'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name).ifindex='999'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name).opaque='unreviewed'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['link-downs']='3'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['link-downs']='02'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['last-link-down-time']='2026-11-05 19:00:00'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['last-link-up-time']='2026-11-05 20:30:40'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['last-link-down-time']='not-a-clock'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name).running='false'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name).disabled='true'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name).type='bridge'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['.id']='*ffff'}, + (a,b)=>{b.catalogs.interface_inventory.find(r=>r.name===name)['mac-address']='02:00:00:00:00:01'}, + (a,b)=>{b.catalogs.vrf=structuredClone(a.catalogs.vrf);b.catalogs.interface_inventory.find(r=>r.name===name).vrf=a.catalogs.interface_inventory.find(r=>r.name===name).vrf}, + (a,b)=>{b.catalogs.gre[0]['remote-address']='198.18.245.11'}, + (a,b)=>{b.catalogs.gre[0]['local-address']='198.18.245.99'}, + (a,b)=>{b.catalogs.gre[0].keepalive='20s,20'}, + (a,b)=>{b.catalogs.wireguard[0].opaque='changed-root-config'}, + (a,b)=>{b.catalogs.ipv4_address.find(r=>r.interface.includes('-wg')).comment='changed-source-config'}, +]){ + const a=structuredClone(pair.before),b=structuredClone(pair.after);mutate(a,b);const [old,current]=rows(a,b,name) + assert.throws(()=>selectedBoundary(a,b,name),/VRF production transition refused/);cases++ +} +const unchanged=structuredClone(pair.before);for(const current of unchanged.catalogs.interface_inventory.filter(r=>r.type==='gre-tunnel'))assert.deepEqual(t.memberInventoryOmissions(pair.before,unchanged,current.name,rows(pair.before,unchanged,current.name)[0],current),['vrf','status']);cases++ +console.log('Observed inverse GRE cycle and synthetic bounded-rule negatives:',cases) diff --git a/backend/src/services/vrf-production-gre-link-local.test.ts b/backend/src/services/vrf-production-gre-link-local.test.ts new file mode 100644 index 00000000..f6ed1161 --- /dev/null +++ b/backend/src/services/vrf-production-gre-link-local.test.ts @@ -0,0 +1,210 @@ +import assert from "node:assert/strict" +import { createHmac } from "node:crypto" +import { readFile } from "node:fs/promises" +import { sealVrfProductionAdmission, VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionRequest, type VrfProductionSnapshot, type VrfProductionRow as Row } from "./vrf-production-admission.js" +import { vrfProductionDependencyObjects, classifyVrfProductionTransition, sealVrfProductionTransitionCheckpoint, openVrfProductionTransitionCheckpoint, assertVrfProductionHistoryProvenance, type VrfProductionTransitionCheckpoint } from "./vrf-production-transition.js" + +const secret = Buffer.alloc(32, 7), now = 1000, sourceProof = "a".repeat(64) +const binding = { serverId: 7, sourceProof, actorId: "human", planId: "gre-local-proof" } +let cases = 0 +const report = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v5.json", import.meta.url), "utf8")) +// Lifecycle shapes come from the existing native bridge fixture. Converting +// the concrete parents and auto-LL below is a pure synthetic GRE lifecycle +// model, not evidence that those GRE lifecycle reads ran on native routers. +function snapshot(phase: string, gre = true, macBackedGre = false): VrfProductionSnapshot { + const raw = report.phases.find((item: { phase: string }) => item.phase === phase) + const catalogs: Record = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(key => [key, structuredClone(raw.owned[key].raw)])) + catalogs.vrf.push({ ".id": "*0", name: "main", interfaces: "all", disabled: "false", builtin: "true" }) + catalogs.routing_table.unshift({ ".id": "*0", name: "main", dynamic: "true", fib: "true" }) + catalogs.ip_settings = [{}]; catalogs.ipv6_settings = [{}] + if (!gre) return { projection: "raw-native-v1", serverId: 7, sourceProof, catalogs } + catalogs.gre = catalogs.bridge.map((row, index) => ({ ".id": row[".id"], name: row.name, "local-address": `198.18.245.${index ? 13 : 9}`, "remote-address": `198.18.245.${index ? 14 : 10}` })) + const greNames = new Set(catalogs.gre.map(row => row.name)) + for (const row of catalogs.interface_inventory) if (greNames.has(row.name)) { row.type = "gre-tunnel"; if (!macBackedGre) delete row["mac-address"] } + if (!macBackedGre) for (const row of catalogs.ipv6_address) if (row.dynamic === "true" && greNames.has(row.interface)) { + row.address = row.interface === catalogs.gre[0].name ? "fe80::2:c612:f50a/64" : "fe80::2:c612:f50e/64" + // Synthetic correction from observed GRE context; not native lifecycle proof. + row.vrf = catalogs.interface_inventory.find(item => item.name === row.interface)!.vrf + } + if (!macBackedGre) for (const row of catalogs.interface_inventory.filter(item => greNames.has(item.name))) { + // Synthetic ordered-context cycle model, not native forward-cycle proof. + const contextPhases = ["addresses-before-vrf", "vrf-create", "vrf-rename", "vrf-disable", "vrf-reenable", "vrf-membership-transfer-a-to-b", "vrf-delete-addresses-remain"] + let previous = "main", cycles = 0 + for (const step of contextPhases.slice(0, contextPhases.indexOf(phase) + 1)) { + const vector = report.phases.find((item: { phase: string }) => item.phase === step) + const current = String(vector.owned.interface_inventory.raw.find((item: Row) => item.name === row.name).vrf) + if (current !== previous) cycles++ + previous = current + } + const clock = (seconds: number) => new Date(Date.UTC(2026, 0, 1, 0, 0, seconds)).toISOString().slice(0, 19).replace("T", " ") + row["link-downs"] = String(cycles); row["last-link-up-time"] = clock(cycles * 2) + if (cycles) row["last-link-down-time"] = clock(cycles * 2 - 1) + else delete row["last-link-down-time"] + } + catalogs.bridge = [] + catalogs.wireguard = [{ ".id": "*ff11", name: "gre-source-wg" }]; catalogs.wireguard_peer = [] + catalogs.interface_inventory.push({ ".id": "*ff11", name: "gre-source-wg", type: "wg", vrf: "main" }) + for (const [index, host] of [9, 13].entries()) catalogs.ipv4_address.push({ ".id": `*ff2${index}`, interface: "gre-source-wg", address: `198.18.245.${host}/30`, dynamic: "false", disabled: "false", vrf: "main" }) + return { projection: "raw-native-v1", serverId: 7, sourceProof, catalogs } +} +function input(before: VrfProductionSnapshot, after: VrfProductionSnapshot, request: VrfProductionRequest, previous?: VrfProductionTransitionCheckpoint, acknowledgedId?: string) { + const issued = sealVrfProductionAdmission({ snapshot: before, binding, request, secret, now }) + return { before, after, request, previous, acknowledgedId, binding, secret, now, sealed: issued.sealed } +} +const stages = ["vrf-create", "vrf-rename", "vrf-disable", "vrf-reenable", "vrf-membership-transfer-a-to-b", "vrf-delete-addresses-remain"] +const inputs: ReturnType[] = [] +let before = snapshot("addresses-before-vrf"), previous: VrfProductionTransitionCheckpoint | undefined +for (const phase of stages) { + const command = report.journal.find((item: { phase: string }) => item.phase === phase), after = snapshot(phase) + const request: VrfProductionRequest = { action: command.method === "PUT" ? "create" : command.method === "DELETE" ? "delete" : "patch", ...(command.id ? { id: command.id } : {}), fields: command.fields, ...(command.method === "PUT" ? { placeBeforeId: "*0" } : {}) } + const current = input(before, after, request, previous, command.assignedId) + const result = classifyVrfProductionTransition(current) + const scope = { serverId: 7, sourceProof, actorId: binding.actorId, nativeId: command.assignedId ?? command.id } + const sealed = sealVrfProductionTransitionCheckpoint(result.checkpoint, scope, secret) + previous = openVrfProductionTransitionCheckpoint(sealed, scope, secret) + assertVrfProductionHistoryProvenance(previous, after) + inputs.push(current); before = after; cases++ +} +// These four LL/endpoints/no-MAC inventory observations are from the exact V29 +// native capture. Unrelated raw17 and concrete parent defaults are synthetic. +for (const [id, name, local, remote, ll] of [ + ["*2A", "mcp-route-8b47e64eec5a-gre-a", "198.18.245.9", "198.18.245.10", "fe80::2:c612:f50a/64"], + ["*2B", "mcp-route-8b47e64eec5a-gre-b", "198.18.245.13", "198.18.245.14", "fe80::2:c612:f50e/64"], + ["*1E", "mcp-route-8b47e64eec5a-gre-a", "198.18.245.10", "198.18.245.9", "fe80::2:c612:f509/64"], + ["*1F", "mcp-route-8b47e64eec5a-gre-b", "198.18.245.14", "198.18.245.13", "fe80::2:c612:f50d/64"], +]) { + const s = snapshot("addresses-before-vrf") + s.catalogs.gre = [{ ".id": id, name, "local-address": local, "remote-address": remote }] + s.catalogs.interface_inventory = [{ ".id": id, name, type: "gre-tunnel", vrf: "main", disabled: "false", running: "true" }, { ".id": "*ff11", name: "gre-source-wg", type: "wg", vrf: "main" }] + s.catalogs.ipv4_address = [{ ".id": "*ff20", address: `${local}/30`, interface: "gre-source-wg", disabled: "false", dynamic: "false", vrf: "main" }] + s.catalogs.ipv6_address = [{ ".id": "*33", address: ll, interface: name, "actual-interface": name, dynamic: "true", disabled: "false", invalid: "false", vrf: "main", "auto-link-local": "true", "link-local": "true" }] + s.catalogs.ipv6_route = [{ ".id": "*ff30", "dst-address": "fe80::/64", dynamic: "true", connect: "true", "routing-table": "main", gateway: name, "immediate-gw": name }] + const request: VrfProductionRequest = { action: "create", placeBeforeId: "*0", fields: { name: "capture-vrf", interfaces: name } } + const { admission } = sealVrfProductionAdmission({ snapshot: s, binding, request, secret, now }) + assert.ok(vrfProductionDependencyObjects(s, admission).some(row => row.kind === "ipv6_route")); cases++ +} +const base = inputs[0] +const selected = String(base.request.fields.interfaces) +const ll = (s: VrfProductionSnapshot) => s.catalogs.ipv6_address.find(row => row.interface === selected && row.dynamic === "true")! +const inventory = (s: VrfProductionSnapshot) => s.catalogs.interface_inventory.find(row => row.name === selected)! +for (const mutate of [ + (s: VrfProductionSnapshot) => { s.catalogs.gre[0]["remote-address"] = "198.18.245.99" }, + (s: VrfProductionSnapshot) => { s.catalogs.gre[0]["local-address"] = "198.18.245.99" }, + (s: VrfProductionSnapshot) => { s.catalogs.gre = [] }, + (s: VrfProductionSnapshot) => { s.catalogs.gre.push({ ...s.catalogs.gre[0], ".id": "*ff88" }) }, + (s: VrfProductionSnapshot) => { inventory(s).type = "ether" }, + (s: VrfProductionSnapshot) => { inventory(s)["mac-address"] = "02:00:00:00:00:01" }, + ...["auto-link-local", "link-local", "disabled"].map(field => (s: VrfProductionSnapshot) => { ll(s)[field] = field === "disabled" ? "true" : "false" }), + (s: VrfProductionSnapshot) => { ll(s).address = "fe80::2:c612:f50a/128" }, + (s: VrfProductionSnapshot) => { ll(s).address = "fe80::cafe/64" }, + (s: VrfProductionSnapshot) => { ll(s)["actual-interface"] = "foreign" }, + (s: VrfProductionSnapshot) => { ll(s).invalid = "true" }, + (s: VrfProductionSnapshot) => { s.catalogs.ipv6_route[0].gateway = "foreign" }, +]) { + const changed = structuredClone(base.before); mutate(changed) + assert.throws(() => { const issued = sealVrfProductionAdmission({ snapshot: changed, binding, request: base.request, secret, now }); vrfProductionDependencyObjects(changed, issued.admission) }, /VRF production/); cases++ +} +// Parent/config/endpoint changes cannot hide behind a newly approved before, +// nor behind serialized checkpoint histories while an acquired LL is absent. +const absentInput = inputs[5], checkpoint = absentInput.previous! +for (const mutate of [ + (s: VrfProductionSnapshot) => { s.catalogs.gre[1][".id"] = "*ff99" }, + (s: VrfProductionSnapshot) => { s.catalogs.gre[1]["remote-address"] = "198.18.245.15" }, + (s: VrfProductionSnapshot) => { s.catalogs.gre[1]["local-address"] = "198.18.245.9" }, + (s: VrfProductionSnapshot) => { s.catalogs.wireguard[0][".id"] = "*ff99" }, + (s: VrfProductionSnapshot) => { s.catalogs.interface_inventory.find(row => row.type === "wg")![".id"] = "*ff98" }, + (s: VrfProductionSnapshot) => { s.catalogs.interface_inventory.find(row => row.type === "wg")!.type = "ether" }, + (s: VrfProductionSnapshot) => { s.catalogs.ipv4_address.find(row => row.interface === "gre-source-wg")![".id"] = "*ff98" }, + (s: VrfProductionSnapshot) => { const row = s.catalogs.ipv4_address.find(row => row.interface === "gre-source-wg")!; row.address = String(row.address).replace("/30", "/29") }, + (s: VrfProductionSnapshot) => { s.catalogs.interface_inventory.find(row => row.name === s.catalogs.gre[1].name)!["mac-address"] = "02:00:00:00:00:01" }, +]) { const changed = structuredClone(absentInput.before); mutate(changed); assert.throws(() => assertVrfProductionHistoryProvenance(checkpoint, changed)); cases++ } +const foreign = structuredClone(base.after); foreign.catalogs.ipv6_route.push({ ".id": "*ffaa", opaque: "foreign" }) +assert.throws(() => classifyVrfProductionTransition({ ...base, after: foreign })); cases++ +const staticAdoption = structuredClone(base.after); ll(staticAdoption).dynamic = "false" +assert.throws(() => classifyVrfProductionTransition({ ...base, after: staticAdoption })); cases++ +for (const mutate of [ + (s: VrfProductionSnapshot) => { s.catalogs.gre[0][".id"] = "*ff99" }, + (s: VrfProductionSnapshot) => { s.catalogs.gre[0]["remote-address"] = "198.18.245.11"; ll(s).address = "fe80::2:c612:f50b/64" }, + (s: VrfProductionSnapshot) => { s.catalogs.gre[0]["local-address"] = "198.18.245.13" }, + (s: VrfProductionSnapshot) => { inventory(s).type = "bridge" }, + (s: VrfProductionSnapshot) => { inventory(s)["mac-address"] = "02:00:00:00:00:01" }, +]) { const changed = structuredClone(base.after); mutate(changed); assert.throws(() => classifyVrfProductionTransition({ ...base, after: changed })); cases++ } +assert.throws(() => classifyVrfProductionTransition({ ...base, now: now + 300000 })); cases++ +assert.throws(() => classifyVrfProductionTransition({ ...base, binding: { ...binding, actorId: "foreign" } })); cases++ +assert.throws(() => classifyVrfProductionTransition({ ...base, after: { ...base.after, sourceProof: "b".repeat(64) } })); cases++ +// Authenticated legacy v1 EUI64 histories remain supported. A deliberately +// incomplete v1 GRE absence checkpoint must fail closed after authentication. +const scope = { serverId: 7, sourceProof, actorId: binding.actorId, nativeId: absentInput.request.id! } +const encoded = sealVrfProductionTransitionCheckpoint(checkpoint, scope, secret) +const legacy = JSON.parse(Buffer.from(encoded.split(".")[0], "base64url").toString()) +legacy.version = 1; delete legacy.history.provenance.greMembers +for (const row of legacy.history.provenance.missingMembers) delete row.greParent +const payload = Buffer.from(JSON.stringify(legacy)).toString("base64url") +const signature = createHmac("sha256", secret).update("mmapp-vrf-production-transition-history-v1\0").update(payload).digest("hex") +assert.throws(() => openVrfProductionTransitionCheckpoint(`${payload}.${signature}`, scope, secret), /incomplete legacy GRE provenance/); cases++ +function asLegacy(sealed: string): string { + const value = JSON.parse(Buffer.from(sealed.split(".")[0], "base64url").toString()) + value.version = 1; delete value.history.provenance.greMembers + for (const row of value.history.provenance.missingMembers) delete row.greParent + const payload = Buffer.from(JSON.stringify(value)).toString("base64url") + return payload + "." + createHmac("sha256", secret).update("mmapp-vrf-production-transition-history-v1\0").update(payload).digest("hex") +} +const createdScope = { ...scope, nativeId: base.acknowledgedId! } +const oldGre = openVrfProductionTransitionCheckpoint(asLegacy(sealVrfProductionTransitionCheckpoint(inputs[1].previous!, createdScope, secret)), createdScope, secret) +assert.throws(() => assertVrfProductionHistoryProvenance(oldGre, base.after), /incomplete legacy GRE provenance/); cases++ +const oldGreResealed = sealVrfProductionTransitionCheckpoint(oldGre, createdScope, secret) +assert.equal(JSON.parse(Buffer.from(oldGreResealed.split(".")[0], "base64url").toString()).version, 1); cases++ +const nativeBefore = snapshot("addresses-before-vrf", false), nativeAfter = snapshot("vrf-create", false) +const nativeResult = classifyVrfProductionTransition(input(nativeBefore, nativeAfter, base.request, undefined, base.acknowledgedId)) +const oldEui64 = openVrfProductionTransitionCheckpoint(asLegacy(sealVrfProductionTransitionCheckpoint(nativeResult.checkpoint, createdScope, secret)), createdScope, secret) +assertVrfProductionHistoryProvenance(oldEui64, nativeAfter) +classifyVrfProductionTransition(input(nativeAfter, snapshot("vrf-rename", false), inputs[1].request, oldEui64)); cases++ +let legacyBefore = nativeAfter, legacyPrevious = oldEui64 +for (let index = 1; index < stages.length; index++) { + const after = snapshot(stages[index], false) + const result = classifyVrfProductionTransition(input(legacyBefore, after, inputs[index].request, legacyPrevious)) + legacyPrevious = openVrfProductionTransitionCheckpoint(asLegacy(sealVrfProductionTransitionCheckpoint(result.checkpoint, createdScope, secret)), createdScope, secret) + assertVrfProductionHistoryProvenance(legacyPrevious, after) + legacyBefore = after; cases++ +} +// GREs with a real MAC retain the original strict EUI64 dispatch and legacy +// member witness, including old v1 absence/revival history compatibility. +let macBefore = snapshot("addresses-before-vrf", true, true), macPrevious: VrfProductionTransitionCheckpoint | undefined +const macAdmission = sealVrfProductionAdmission({ snapshot: macBefore, binding, request: base.request, secret, now }).admission +assert.ok(vrfProductionDependencyObjects(macBefore, macAdmission).some(row => row.kind === "ipv6_route")); cases++ +for (let index = 0; index < stages.length; index++) { + const after = snapshot(stages[index], true, true) + const result = classifyVrfProductionTransition(input(macBefore, after, inputs[index].request, macPrevious, inputs[index].acknowledgedId)) + macPrevious = openVrfProductionTransitionCheckpoint(asLegacy(sealVrfProductionTransitionCheckpoint(result.checkpoint, createdScope, secret)), createdScope, secret) + assertVrfProductionHistoryProvenance(macPrevious, after) + macBefore = after; cases++ +} +// Even a valid replacement MAC/EUI64 pair cannot replace a historical no-MAC +// GRE witness under a fresh approval or while its acquired LL remains absent. +const macReplacement = structuredClone(absentInput.before) +const acquired = macReplacement.catalogs.interface_inventory.find(row => row.name === macReplacement.catalogs.gre[1].name)! +acquired["mac-address"] = "02:00:00:00:00:01" +assert.throws(() => assertVrfProductionHistoryProvenance(checkpoint, macReplacement)); cases++ +const presentReplacement = structuredClone(base.after) +inventory(presentReplacement)["mac-address"] = "02:00:00:00:00:01" +ll(presentReplacement).address = "fe80::ff:fe00:1/64" +assert.throws(() => assertVrfProductionHistoryProvenance(inputs[1].previous!, presentReplacement)); cases++ +// Optional secrets and noncausal source annotations never enter the durable +// witness. Their complete values remain fenced within a current transition. +const secretBefore = structuredClone(base.before), secretAfter = structuredClone(base.after) +for (const s of [secretBefore, secretAfter]) { + s.catalogs.gre[0]["ipsec-secret"] = "GRE-secret-must-never-be-serialized" + s.catalogs.ipv4_address.find(row => row.interface === "gre-source-wg")!.comment = "source-annotation-must-not-be-serialized" +} +const secretResult = classifyVrfProductionTransition(input(secretBefore, secretAfter, base.request, undefined, base.acknowledgedId)) +const secretSealed = sealVrfProductionTransitionCheckpoint(secretResult.checkpoint, createdScope, secret) +const secretPayload = Buffer.from(secretSealed.split(".")[0], "base64url").toString() +assert.equal(secretPayload.includes("ipsec-secret"), false) +assert.equal(secretPayload.includes("GRE-secret-must-never-be-serialized"), false) +assert.equal(secretPayload.includes("source-annotation-must-not-be-serialized"), false) +const secretOpened = openVrfProductionTransitionCheckpoint(secretSealed, createdScope, secret) +assertVrfProductionHistoryProvenance(secretOpened, secretAfter); cases++ +const currentSecretDrift = structuredClone(secretAfter) +currentSecretDrift.catalogs.gre[0]["ipsec-secret"] = "changed-during-current-transition" +assert.throws(() => classifyVrfProductionTransition(input(secretBefore, currentSecretDrift, base.request, undefined, base.acknowledgedId))); cases++ +console.log("GRE captured auto-LL, synthetic lifecycle, durable endpoints, negative conservation cases:", cases) diff --git a/backend/src/services/vrf-production-peer-telemetry.test.mjs b/backend/src/services/vrf-production-peer-telemetry.test.mjs new file mode 100644 index 00000000..e6c7fbe4 --- /dev/null +++ b/backend/src/services/vrf-production-peer-telemetry.test.mjs @@ -0,0 +1,125 @@ +import assert from 'node:assert/strict' +import {readFile} from 'node:fs/promises' +import {createHmac} from 'node:crypto' +import ts from 'typescript' + +// Pure production modules only: no SQL/HTTP authority adapters are imported. +const cache=new Map() +async function moduleUri(name){ + if(cache.has(name))return cache.get(name) + const loading=(async()=>{ + let code=ts.transpileModule(await readFile(new URL(name,import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ES2022}}).outputText + const ast=ts.createSourceFile(name,code,ts.ScriptTarget.ES2022,true) + const imports=ast.statements.filter(ts.isImportDeclaration).filter(node=>node.moduleSpecifier.text.startsWith('./')) + const edits=await Promise.all(imports.map(async node=>({start:node.moduleSpecifier.getStart(ast),end:node.moduleSpecifier.end,value:await moduleUri(node.moduleSpecifier.text.replace(/\.js$/,'.ts'))}))) + for(const edit of edits.reverse())code=code.slice(0,edit.start)+JSON.stringify(edit.value)+code.slice(edit.end) + return'data:text/javascript;base64,'+Buffer.from(code).toString('base64') + })();cache.set(name,loading);return loading +} +const a=await import(await moduleUri('./vrf-production-admission.ts')),t=await import(await moduleUri('./vrf-production-transition.ts')),p=await import(await moduleUri('./vrf-production-projection.ts')) +const fixture=JSON.parse(await readFile(new URL('./fixtures/vrf-projection-v2.json',import.meta.url),'utf8')) +const {before,after:legacyAfter,binding,request,scope,now,acknowledgedId}=fixture,secret=Buffer.alloc(32,fixture.secretFill) +// Preserve the genuine old proof input verbatim; correct only the current +// synthetic no-MAC GRE after-context used by new-transition assertions. +const after=structuredClone(legacyAfter) +for(const row of after.catalogs.ipv6_address) { + const member=after.catalogs.interface_inventory.find(item=>item.name===row.interface) + if(row.dynamic==='true'&&member?.type==='gre-tunnel'&&!Object.hasOwn(member,'mac-address'))row.vrf=member.vrf +} +let cases=0 +const seal=snapshot=>a.sealVrfProductionAdmission({snapshot,binding,request,secret,now}) +const issued=seal(before) +assert.equal(issued.admission.version,3) +const inspect=snapshot=>a.inspectVrfProductionAdmission({snapshot,binding,request,secret,now,sealed:issued.sealed}) +const input={before,after,binding,request,secret,now,sealed:issued.sealed,acknowledgedId} +const classify=next=>t.classifyVrfProductionTransition({...input,after:next}) +const peer=snapshot=>snapshot.catalogs.wireguard_peer[0] +const decode=sealed=>JSON.parse(Buffer.from(sealed.split('.')[0],'base64url').toString()) +inspect(before);const baseline=classify(after);cases++ +for(const field of ['rx','tx','last-handshake'])for(const value of ['tick',301,false]){ + const b=structuredClone(before),next=structuredClone(after);peer(b)[field]=value;peer(next)[field]=value + inspect(b);const result=classify(next) + const cp=t.openVrfProductionTransitionCheckpoint(t.sealVrfProductionTransitionCheckpoint(result.checkpoint,scope,secret),scope,secret) + const later=structuredClone(next);peer(later)[field]='later';t.assertVrfProductionTransitionCheckpoint(cp,later);cases++ +} +const combinedBefore=structuredClone(before),combinedAfter=structuredClone(after) +for(const s of [combinedBefore,combinedAfter])Object.assign(peer(s),{rx:'501',tx:'601','last-handshake':'2s'}) +inspect(combinedBefore);classify(combinedAfter);cases++ +// Exact actual pre-change ticket: valid v2 MAC, now refused before any claim. +const [oldPayload,oldMac]=fixture.admissionV2.split('.') +assert.equal(decode(fixture.admissionV2).version,2) +assert.equal(createHmac('sha256',secret).update('mmapp-vrf-production-admission-v2\0').update(oldPayload).digest('hex'),oldMac) +assert.throws(()=>a.inspectVrfProductionAdmission({snapshot:before,binding,request,secret,now,sealed:fixture.admissionV2}),/ticket signature changed/);cases++ +const falselyResigned=oldPayload+'.'+createHmac('sha256',secret).update('mmapp-vrf-production-admission-v3\0').update(oldPayload).digest('hex') +assert.throws(()=>a.inspectVrfProductionAdmission({snapshot:before,binding,request,secret,now,sealed:falselyResigned}),/unknown ticket schema/);cases++ +const oldCheckpoint=t.openVrfProductionTransitionCheckpoint(fixture.checkpointProofV1,scope,secret) +t.assertVrfProductionTransitionCheckpoint(oldCheckpoint,legacyAfter) +const tickedAfter=structuredClone(after);peer(tickedAfter).rx='101' +const legacyTicked=structuredClone(legacyAfter);peer(legacyTicked).rx='101' +assert.throws(()=>t.assertVrfProductionTransitionCheckpoint(oldCheckpoint,legacyTicked),/checkpoint parent\/configuration\/order drift/) +const oldResealed=t.sealVrfProductionTransitionCheckpoint(oldCheckpoint,scope,secret) +assert.deepEqual(decode(oldResealed),decode(fixture.checkpointProofV1)) +assert.equal(decode(oldResealed).history.snapshot.kind,'configuration-sha256-v1');cases++ +const currentSealed=t.sealVrfProductionTransitionCheckpoint(baseline.checkpoint,scope,secret) +assert.equal(decode(currentSealed).history.snapshot.kind,'configuration-sha256-v2') +const current=t.openVrfProductionTransitionCheckpoint(currentSealed,scope,secret) +t.assertVrfProductionTransitionCheckpoint(current,tickedAfter);cases++ +const unknownProof=decode(currentSealed);unknownProof.history.snapshot.kind='configuration-sha256-v999' +const unknownPayload=Buffer.from(JSON.stringify(unknownProof)).toString('base64url') +const unknownSealed=unknownPayload+'.'+createHmac('sha256',secret).update('mmapp-vrf-production-transition-history-v1\0').update(unknownPayload).digest('hex') +assert.throws(()=>t.openVrfProductionTransitionCheckpoint(unknownSealed,scope,secret),/private checkpoint source\/history/);cases++ +// Old causal provenance may continue only with fresh current approval/ticket; +// the legacy whole-config digest itself remains strict and never upgraded. +const patch={action:'patch',id:scope.nativeId,fields:{comment:'fresh-v3-approved'}} +const patchAfter=structuredClone(tickedAfter);patchAfter.catalogs.vrf.find(row=>row['.id']===scope.nativeId).comment=patch.fields.comment +const fresh=a.sealVrfProductionAdmission({snapshot:tickedAfter,binding,request:patch,secret,now}) +const continuation=t.classifyVrfProductionTransition({before:tickedAfter,after:patchAfter,binding,request:patch,secret,now,sealed:fresh.sealed,previous:oldCheckpoint}) +assert.equal(decode(t.sealVrfProductionTransitionCheckpoint(continuation.checkpoint,scope,secret)).history.snapshot.kind,'configuration-sha256-v2');cases++ + +function refused(mutate){ + const b=structuredClone(before),next=structuredClone(after);mutate(b);mutate(next) + assert.throws(()=>inspect(b),/VRF production/) + assert.throws(()=>classify(next),/VRF production/);cases++ +} +for(const field of ['rx','tx','last-handshake'])for(const value of [null,{value:302},[302]])refused(s=>{peer(s)[field]=value}) +for(const [field,value]of Object.entries({'public-key':'OTHER_PUBLIC_KEY','private-key':'OTHER_PRIVATE_KEY','preshared-key':'OTHER_PSK','endpoint-address':'192.0.2.3','endpoint-port':'55102','current-endpoint-address':'192.0.2.3','current-endpoint-port':'55102','allowed-address':'198.51.100.0/24','persistent-keepalive':'20s',disabled:'true',running:'false',opaque:'changed',unreviewed:'unknown','rx-byte':'999','.id':'*ff32',interface:'other-wg'}))refused(s=>{peer(s)[field]=value}) +refused(s=>{s.catalogs.wireguard[0]['private-key']='OTHER_PRIVATE_KEY'}) +for(const resource of ['wireguard','gre','interface_inventory','bridge'])for(const field of ['rx','tx','last-handshake']){ + refused(s=>{const row=s.catalogs[resource][0]??{'.id':'*ff90'};row[field]='unreviewed';if(!s.catalogs[resource].length)s.catalogs[resource].push(row)}) +} +for(const field of ['rx','tx','last-handshake']){ + assert.equal(p.isVrfProductionTelemetryValue('wireguard_peer',field,NaN),false) + assert.equal(p.isVrfProductionTelemetryValue('wireguard_peer',field,Infinity),false) + assert.equal(p.isVrfProductionTelemetryValue('wireguard_peer',field,null),false) + assert.equal(p.vrfProductionConfigurationV2('wireguard_peer',{[field]:'old'})[field],'old');cases++ +} +const twoBefore=structuredClone(before),twoAfter=structuredClone(after) +for(const s of [twoBefore,twoAfter])s.catalogs.wireguard_peer.push({...peer(s),'.id':'*ff32','public-key':'SECOND_PUBLIC_KEY'}) +const twoIssued=seal(twoBefore);twoAfter.catalogs.wireguard_peer.reverse() +assert.throws(()=>t.classifyVrfProductionTransition({...input,before:twoBefore,after:twoAfter,sealed:twoIssued.sealed}),/foreign wireguard_peer/);cases++ + +// Inspect actual emitted operation-version expressions and the real runtime +// fence, without loading the application or granting a dispatch capability. +async function ast(name){const source=await readFile(new URL(name,import.meta.url),'utf8');return ts.createSourceFile(name,ts.transpileModule(source,{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ES2022}}).outputText,ts.ScriptTarget.ES2022,true)} +function nodes(root,predicate){const found=[];function visit(node){if(predicate(node))found.push(node);ts.forEachChild(node,visit)}visit(root);return found} +const actionAst=await ast('../modules/mcp/actions.ts') +const actionCall=nodes(actionAst,node=>ts.isCallExpression(node)&&node.expression.getText(actionAst)==='add'&&node.arguments[0]?.getText(actionAst)==='`ros.${resource.id}.${action}`')[0] +const impl=actionCall.arguments.at(-1),version=impl.properties.find(node=>node.name?.getText(actionAst)==='version').initializer.getText(actionAst) +const dependencyAst=await ast('../modules/mcp/routing-dependencies.ts') +function declaration(source,name){return 'const '+nodes(source,node=>ts.isVariableDeclaration(node)&&node.name.getText(source)===name)[0].getText(source)+';'} +const declarations=[declaration(dependencyAst,'ospfInterfaceResources'),...['delegated','guardsOspfInterfaceUse','previousVersion'].map(name=>declaration(actionAst,name))].join('\n') +const nativeVersion=new Function('resource','action',declarations+'return '+version) +assert.deepEqual(['create','update','delete'].map(action=>nativeVersion({id:'vrf'},action)),[5,4,5]) +const aliasAst=await ast('../modules/mcp/routing-controls.ts'),alias=nodes(aliasAst,node=>ts.isCallExpression(node)&&node.expression.getText(aliasAst)==='register'&&node.arguments[0]?.text==='routing.vrf.update')[0] +assert.equal(alias.arguments.at(-1).properties.find(node=>node.name?.getText(aliasAst)==='version').initializer.text,'4') +assert.equal(alias.arguments[4].text,'mm:network:write');cases++ +const runtimeAst=await ast('../modules/mcp/runtime.ts'),execute=runtimeAst.statements.find(node=>ts.isFunctionDeclaration(node)&&node.name?.text==='executeOperationWithLease'),outer=execute.body.statements.find(ts.isTryStatement) +const fence=outer.tryBlock.statements.find(node=>ts.isIfStatement(node)&&node.expression.getText(runtimeAst).startsWith('plan.operation_version !== findActionByOperation')) +assert.ok(fence);assert.ok(fence.poscheck({operation_id:operation,operation_version:old},()=>({operation:{version:current}}),McpError),error=>error.code==='PLAN_EXPIRED') + check({operation_id:operation,operation_version:current},()=>({operation:{version:current}}),McpError);cases++ +} +console.log('VRF peer three-field telemetry, actual legacy tickets/proofs, conservative drift and pre-intent version fences:',cases) diff --git a/backend/src/services/vrf-production-projection.ts b/backend/src/services/vrf-production-projection.ts new file mode 100644 index 00000000..692d9d68 --- /dev/null +++ b/backend/src/services/vrf-production-projection.ts @@ -0,0 +1,24 @@ +/** Reviewed top-level telemetry spellings are scoped to native REST catalogs. + * Unknown catalogs/fields, nested values and familiar names on other paths + * remain configuration. New admissions use v3; the immutable v2 rules below + * remain available only to compare authenticated legacy snapshot proofs. */ +const interfaceTelemetry = ["running", "actual-mtu", "current-mac-address", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "rx-drop", "tx-drop", "rx-error", "tx-error", "tx-queue-drop", "fp-rx-byte", "fp-tx-byte", "fp-rx-packet", "fp-tx-packet"] +const resourceTelemetry: Readonly>> = { + bridge: new Set(interfaceTelemetry), ethernet: new Set(interfaceTelemetry), interface_inventory: new Set(interfaceTelemetry), + ipv4_route: new Set(["active", "inactive", "invalid"]), ipv6_route: new Set(["active", "inactive", "invalid"]), + ipv4_address: new Set(["invalid"]), ipv6_address: new Set(["invalid"]), routing_table: new Set(["invalid"]), + ip_filter: new Set(["packets", "bytes", "invalid"]), ipv6_filter: new Set(["packets", "bytes", "invalid"]), + ip_settings: new Set(["ipv4-fast-path-active", "ipv4-fasttrack-active", "ipv4-fast-path-packets", "ipv4-fast-path-bytes", "ipv4-fasttrack-packets", "ipv4-fasttrack-bytes"]), + ipv6_settings: new Set(["ipv6-fast-path-active", "ipv6-fast-path-packets", "ipv6-fast-path-bytes"]), +} +const wireguardPeerTelemetry = new Set(["rx", "tx", "last-handshake"]) +const scalar = (value: unknown): boolean => typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value) +export const isVrfProductionTelemetry = (resource: string, field: string): boolean => resourceTelemetry[resource]?.has(field) === true || resource === "wireguard_peer" && wireguardPeerTelemetry.has(field) +export const isVrfProductionTelemetryValue = (resource: string, field: string, value: unknown): boolean => isVrfProductionTelemetry(resource, field) && scalar(value) +export function vrfProductionConfiguration(resource: string, row: Record, omit: readonly string[] = []): Record { + return Object.fromEntries(Object.entries(row).filter(([field, value]) => !isVrfProductionTelemetryValue(resource, field, value) && !omit.includes(field))) +} +/** Exact pre-v3 projection. Never use to prepare/claim a new admission. */ +export function vrfProductionConfigurationV2(resource: string, row: Record, omit: readonly string[] = []): Record { + return Object.fromEntries(Object.entries(row).filter(([field, value]) => !(resourceTelemetry[resource]?.has(field) === true && scalar(value)) && !omit.includes(field))) +} diff --git a/backend/src/services/vrf-production-read.test.ts b/backend/src/services/vrf-production-read.test.ts new file mode 100644 index 00000000..331819f5 --- /dev/null +++ b/backend/src/services/vrf-production-read.test.ts @@ -0,0 +1,212 @@ +import assert from "node:assert/strict" +import { readVrfProductionSnapshot, VRF_PRODUCTION_RAW_PATHS, type VrfProductionReadAdapter, type VrfProductionRawClient } from "./vrf-production-read.js" +import { VRF_PRODUCTION_REQUIRED_CATALOGS, sealVrfProductionAdmission, type VrfProductionRequest, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import type { VrfRawReadObservation } from "./vrf-execution-diagnostic.js" + +const sourceProof = "a".repeat(64) +const request: VrfProductionRequest = { action: "patch", id: "*1", fields: { name: "customers" } } +function model() { + const catalogs: VrfProductionSnapshot["catalogs"] = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, []])) + catalogs.ip_settings = [{ "rp-filter": "loose" }]; catalogs.ipv6_settings = [{ "disable-ipv6": "false" }] + catalogs.vrf = [{ ".id": "*1", name: "production", interfaces: "customer-vlan", disabled: "false" }, { ".id": "*2", name: "main", interfaces: "all", builtin: "true" }] + catalogs.interface_inventory = [{ ".id": "*10", name: "ether1", type: "ether", "rx-byte": "123", "unknown-field": "preserve" }, { ".id": "*11", name: "customer-vlan", type: "vlan" }] + catalogs.ethernet = [{ ".id": "*20", name: "ether1", mtu: "1500" }] + catalogs.vlan = [{ ".id": "*21", name: "customer-vlan", interface: "ether1", "vlan-id": "101" }] + catalogs.ipv4_address = [{ ".id": "*30", interface: "customer-vlan", address: "192.0.2.1/24", vrf: "production", dynamic: "false" }] + catalogs.ipv6_address = [{ ".id": "*31", interface: "customer-vlan", address: "2001:db8:1234::1/48", advertise: "yes", dynamic: "false" }, { ".id": "*32", interface: "customer-vlan", address: "fe80::1/64", dynamic: "true" }] + catalogs.ipv4_route = [{ ".id": "*40", gateway: "customer-vlan", dynamic: "true", connect: "true", "routing-table": "production" }] + const calls: string[] = [] + let proof = sourceProof, enabled = true, authority = true, authorityCalls = 0, resolveCalls = 0 + let afterGet: ((path: string) => void) | undefined + let afterResolve: (() => void) | undefined + const payloads: Record = {} + const client: VrfProductionRawClient = { async get(path: string, timeoutMs?: number, _signal?: AbortSignal, maxBytes?: number): Promise { + assert.equal(timeoutMs, 10000); assert.equal(maxBytes, 8 * 1024 * 1024) + calls.push(path) + const resource = Object.keys(VRF_PRODUCTION_RAW_PATHS).find(key => VRF_PRODUCTION_RAW_PATHS[key] === path)! + const payload = Object.hasOwn(payloads, resource) ? payloads[resource] : catalogs[resource] + afterGet?.(path) + return structuredClone(payload) as T + } } + const adapter: VrfProductionReadAdapter = { async resolve(serverId) { + resolveCalls++ + const source = { serverId, enabled, sourceProof: proof, client } + afterResolve?.() + return source + } } + const input = { serverId: 7, expectedSourceProof: sourceProof, request: structuredClone(request), adapter, async assertAuthority() { authorityCalls++; assert.ok(authority, "authority revoked") } } + return { catalogs, calls, payloads, input, revoke() { authority = false }, disable() { enabled = false }, changeSource() { proof = "b".repeat(64) }, afterGet(callback: typeof afterGet) { afterGet = callback }, afterResolve(callback: typeof afterResolve) { afterResolve = callback }, counters() { return { authorityCalls, resolveCalls } } } +} +const happy = model(), snapshot = await readVrfProductionSnapshot(happy.input) +assert.equal(snapshot.projection, "raw-native-v1") +assert.equal(happy.calls.length, 18) +assert.deepEqual(snapshot.catalogs, happy.catalogs) +assert.equal(snapshot.catalogs.interface_inventory[0]["unknown-field"], "preserve") +assert.equal(snapshot.catalogs.ipv6_address[1].dynamic, "true") +assert.equal(happy.counters().authorityCalls, happy.counters().resolveCalls * 2) +const sealed = sealVrfProductionAdmission({ snapshot, binding: { serverId: 7, sourceProof, actorId: "human", planId: "plan" }, request, secret: Buffer.alloc(32, 7), now: 1 }) +assert.equal(sealed.admission.companionExecution, "requires-native-transition-proof") +assert.deepEqual(sealed.admission.members[0].parents.map(row => [row.id, row.configurationId]), [["*11", "*21"], ["*10", "*20"]]) +assert.equal(sealed.admission.members[0].addresses.length, 3) +happy.catalogs.vlan[0].interface = "changed-after-read" +assert.equal(snapshot.catalogs.vlan[0].interface, "ether1") + +for (const change of ["revoke", "disable", "source"] as const) { + const m = model() + if (change === "revoke") m.revoke() + if (change === "disable") m.disable() + if (change === "source") m.changeSource() + await assert.rejects(readVrfProductionSnapshot(m.input)) + assert.equal(m.calls.length, 0) +} +for (const change of ["revoke", "disable", "source"] as const) { + const m = model() + m.afterGet(() => { if (change === "revoke") m.revoke(); if (change === "disable") m.disable(); if (change === "source") m.changeSource() }) + await assert.rejects(readVrfProductionSnapshot(m.input)) + assert.equal(m.calls.length, 1) +} +const resolving = model(); resolving.afterResolve(() => resolving.revoke()) +await assert.rejects(readVrfProductionSnapshot(resolving.input)); assert.equal(resolving.calls.length, 0) +const finalFence = model(); finalFence.afterGet(path => { if (path === "/interface/vlan") finalFence.changeSource() }) +await assert.rejects(readVrfProductionSnapshot(finalFence.input), /source changed/) + +for (const payload of [undefined, { rows: [], truncated: true }, [{ name: "without-id" }], [{ ".id": "*1" }, { ".id": "*1" }], new Array(50000).fill({ ".id": "*1" }), [{ ".id": "*1", comment: "x".repeat(8 * 1024 * 1024) }]]) { + const m = model(); m.payloads.bridge = payload + await assert.rejects(readVrfProductionSnapshot(m.input), /VRF production read refused/) +} +for (const alias of ["*A", "*0a", "*000A"]) { + const m = model(); m.payloads.bridge = [{ ".id": "*a", name: "first" }, { ".id": alias, name: "second" }] + await assert.rejects(readVrfProductionSnapshot(m.input), /duplicate native ID/) +} +for (const type of ["__proto__", "constructor", "toString"]) { + const m = model(); m.catalogs.interface_inventory[1].type = type + await assert.rejects(readVrfProductionSnapshot(m.input), /unsupported\/missing concrete/) + assert.ok(m.calls.every(path => typeof path === "string" && path.startsWith("/"))) +} +for (const payload of [[], [{}, {}], { foo: undefined }]) { + const m = model(); m.payloads.ip_settings = payload + await assert.rejects(readVrfProductionSnapshot(m.input), /VRF production read refused/) +} +for (const mutate of [ + (m: ReturnType) => { m.catalogs.interface_inventory.push({ ".id": "*99", name: "ether1", type: "ether" }) }, + (m: ReturnType) => { m.catalogs.interface_inventory[1].type = "unreviewed-type" }, + (m: ReturnType) => { m.catalogs.vlan[0].interface = "missing" }, + (m: ReturnType) => { m.catalogs.vlan[0].interface = "all" }, + (m: ReturnType) => { m.catalogs.vlan[0].interface = "customer-vlan" }, + (m: ReturnType) => { delete m.catalogs.vlan[0].interface }, + (m: ReturnType) => { m.catalogs.vlan.push({ ...m.catalogs.vlan[0], ".id": "*99" }) }, + (m: ReturnType) => { m.catalogs.ipv4_address[0].address = "192.0.2.1/33" }, + (m: ReturnType) => { m.input.request.fields.interfaces = "customer-vlan,customer-vlan" }, +]) { + const m = model(); mutate(m) + await assert.rejects(readVrfProductionSnapshot(m.input), /VRF production read refused/) +} +const physical = model(); physical.input.request.fields.interfaces = "ether1" +assert.equal((await readVrfProductionSnapshot(physical.input)).catalogs.ethernet[0][".id"], "*20") +const bridge = model() +bridge.catalogs.interface_inventory.push({ ".id": "*50", name: "br-users", type: "bridge" }) +bridge.catalogs.bridge = [{ ".id": "*60", name: "br-users" }] +bridge.catalogs.bridge_port = [{ ".id": "*61", bridge: "br-users", interface: "ether1" }] +bridge.catalogs.bridge_vlan = [{ ".id": "*62", bridge: "br-users", tagged: "br-users,customer-vlan", untagged: "ether1", "current-tagged": "br-users,customer-vlan" }] +bridge.input.request.fields.interfaces = "br-users" +assert.equal((await readVrfProductionSnapshot(bridge.input)).catalogs.bridge_port[0][".id"], "*61") +const bond = model() +bond.catalogs.interface_inventory.push({ ".id": "*50", name: "bond-users", type: "bond" }) +bond.catalogs.bonding = [{ ".id": "*60", name: "bond-users", slaves: "ether1", mode: "802.3ad" }] +bond.input.request.fields.interfaces = "bond-users" +assert.equal((await readVrfProductionSnapshot(bond.input)).catalogs.bonding[0].slaves, "ether1") +for (const [type, resource] of [["gre-tunnel", "gre"], ["ipip-tunnel", "ipip"], ["eoip-tunnel", "eoip"], ["vxlan", "vxlan"]]) { + const m = model() + m.catalogs.interface_inventory.push({ ".id": "*50", name: "customer-tunnel", type }) + m.catalogs[resource] = [{ ".id": "*60", name: "customer-tunnel", "local-address": "192.0.2.1", "remote-address": "198.51.100.1", interface: "ether1" }] + if (resource === "vxlan") m.catalogs.vxlan_vtep = [{ ".id": "*70", interface: "customer-tunnel", "remote-ip": "198.51.100.1" }] + m.input.request.fields.interfaces = "customer-tunnel" + assert.equal((await readVrfProductionSnapshot(m.input)).catalogs[resource][0][".id"], "*60") + m.catalogs[resource][0]["local-address"] = "0.0.0.0" + await assert.rejects(readVrfProductionSnapshot(m.input), /unbound tunnel local source/) +} +const wg = model() +wg.catalogs.interface_inventory.push({ ".id": "*50", name: "wg-users", type: "wireguard" }) +wg.catalogs.wireguard = [{ ".id": "*60", name: "wg-users", "listen-port": "51820" }] +wg.catalogs.wireguard_peer = [{ ".id": "*70", interface: "wg-users", "public-key": "opaque-public", "endpoint-address": "198.51.100.1", "allowed-address": "10.0.0.0/24" }] +wg.input.request.fields.interfaces = "wg-users" +assert.equal((await readVrfProductionSnapshot(wg.input)).catalogs.wireguard_peer[0][".id"], "*70") +wg.catalogs.wireguard_peer[0]["endpoint-address"] = "peer.example.test" +await assert.rejects(readVrfProductionSnapshot(wg.input), /unresolved WireGuard endpoint/) +const ppp = model() +ppp.catalogs.interface_inventory.push({ ".id": "*50", name: "ppp-users", type: "pppoe-out" }) +ppp.catalogs.pppoe_client = [{ ".id": "*60", name: "ppp-users", interface: "ether1" }] +ppp.input.request.fields.interfaces = "ppp-users" +assert.equal((await readVrfProductionSnapshot(ppp.input)).catalogs.pppoe_client[0].interface, "ether1") +const deep = model() +deep.catalogs.interface_inventory = [{ ".id": "*10", name: "ether1", type: "ether" }] +deep.catalogs.vlan = [] +for (let i = 0; i < 130; i++) { + deep.catalogs.interface_inventory.push({ ".id": `*${(1000 + i).toString(16)}`, name: `vlan-${i}`, type: "vlan" }) + deep.catalogs.vlan.push({ ".id": `*${(2000 + i).toString(16)}`, name: `vlan-${i}`, interface: i === 129 ? "ether1" : `vlan-${i + 1}` }) +} +deep.catalogs.vrf[0].interfaces = "vlan-0" +await assert.rejects(readVrfProductionSnapshot(deep.input), /closure budget/) +console.log("VRF production raw reader model tests passed") + +// Optional observation adds no GET, authentication, source resolution or timer. +const observed = model(), observations: VrfRawReadObservation[] = [] +assert.deepEqual(await readVrfProductionSnapshot({ ...observed.input, observeRead: value => observations.push(value) }), snapshot) +assert.deepEqual(observed.calls, happy.calls) +assert.deepEqual(observed.counters(), happy.counters()) +assert.equal(observations.length, observed.calls.length * 5 + 2) +assert.deepEqual(observations.slice(0, 4).map(value => value.boundary), ["authority-before", "native-get", "authority-after", "shape"]) +assert.ok(observations.every(value => value.serverId === 7)) +assert.deepEqual(observations.at(-2), { serverId: 7, boundary: "final-authority" }) +assert.deepEqual(observations.at(-1), { serverId: 7, boundary: "snapshot-validation" }) +for (const rejectAsync of [false, true]) { + const unchanged = model() + await readVrfProductionSnapshot({ ...unchanged.input, observeRead: () => { + if (rejectAsync) return Promise.reject(Error("private observer failure")) + throw Error("private observer failure") + } }) + await new Promise(resolve => setImmediate(resolve)) + assert.deepEqual(unchanged.calls, happy.calls) + assert.deepEqual(unchanged.counters(), happy.counters()) +} +const failedGet = model(), readError = Object.assign(Error("PRIVATE_NATIVE_JSON_OR_TRANSPORT"), { code: "ECONNRESET" }), readEvents: VrfRawReadObservation[] = [] +const resolveSource = failedGet.input.adapter.resolve +failedGet.input.adapter.resolve = async serverId => { + const source = await resolveSource(serverId) + return { ...source, client: { async get(): Promise { throw readError } } } +} +await assert.rejects(readVrfProductionSnapshot({ ...failedGet.input, observeRead: value => readEvents.push(value) }), error => error === readError) +assert.deepEqual(readEvents.at(-1), { serverId: 7, catalog: "bridge", boundary: "native-get" }) +const failedShape = model(), shapeEvents: VrfRawReadObservation[] = [] +failedShape.payloads.bridge = { PRIVATE_NATIVE_ROWS: "not a catalog" } +await assert.rejects(readVrfProductionSnapshot({ ...failedShape.input, observeRead: value => shapeEvents.push(value) }), /incomplete\/oversized raw catalog/) +assert.deepEqual(shapeEvents.at(-1), { serverId: 7, catalog: "bridge", boundary: "shape" }) +const failedAuthority = model(), authorityEvents: VrfRawReadObservation[] = [] +failedAuthority.afterGet(() => failedAuthority.revoke()) +await assert.rejects(readVrfProductionSnapshot({ ...failedAuthority.input, observeRead: value => authorityEvents.push(value) })) +assert.deepEqual(authorityEvents.at(-1), { serverId: 7, catalog: "bridge", boundary: "authority-after" }) +assert.equal(JSON.stringify([...readEvents, ...shapeEvents, ...authorityEvents]).includes("PRIVATE"), false) +for (const kind of ["inventory", "closure", "source"] as const) { + const local = model(), localEvents: VrfRawReadObservation[] = [] + if (kind === "inventory") local.catalogs.interface_inventory.push({ ".id": "*ff88", name: "ether1", type: "ether" }) + if (kind === "closure") local.input.request.fields.interfaces = "missing-selected-member" + if (kind === "source") { + local.catalogs.interface_inventory.push({ ".id": "*50", name: "customer-gre", type: "gre-tunnel" }) + local.catalogs.gre = [{ ".id": "*60", name: "customer-gre", "local-address": "0.0.0.0", "remote-address": "198.51.100.1" }] + local.input.request.fields.interfaces = "customer-gre" + } + await assert.rejects(readVrfProductionSnapshot({ ...local.input, observeRead: value => localEvents.push(value) })) + assert.deepEqual(localEvents.at(-1), { serverId: 7, boundary: "snapshot-validation" }, kind) + assert.ok(local.calls.length >= VRF_PRODUCTION_REQUIRED_CATALOGS.length) +} +const budget = model(), budgetEvents: VrfRawReadObservation[] = [] +for (const resource of ["bridge", "bridge_port", "bridge_vlan", "ip_filter", "ipv6_filter"]) budget.catalogs[resource] = [{ ".id": "*ff88", opaque: "x".repeat(7 * 1024 * 1024) }] +await assert.rejects(readVrfProductionSnapshot({ ...budget.input, observeRead: value => budgetEvents.push(value) }), /raw evidence byte budget/) +assert.deepEqual(budgetEvents.at(-1), { serverId: 7, boundary: "snapshot-validation" }) +const finalAuthority = model(), finalEvents: VrfRawReadObservation[] = [] +finalAuthority.afterResolve(() => { + if (finalAuthority.counters().resolveCalls === happy.counters().resolveCalls) finalAuthority.revoke() +}) +await assert.rejects(readVrfProductionSnapshot({ ...finalAuthority.input, observeRead: value => finalEvents.push(value) })) +assert.deepEqual(finalEvents.at(-1), { serverId: 7, boundary: "final-authority" }) +console.log("VRF raw-reader optional diagnostics: same IO/order/timers and original read/shape/authority failures passed") diff --git a/backend/src/services/vrf-production-read.ts b/backend/src/services/vrf-production-read.ts new file mode 100644 index 00000000..4a33762b --- /dev/null +++ b/backend/src/services/vrf-production-read.ts @@ -0,0 +1,216 @@ +import { isIP } from "node:net" +import { VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionRequest, type VrfProductionRow, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { notifyVrfDiagnostic, type VrfRawReadObservation } from "./vrf-execution-diagnostic.js" + +/** Raw, complete REST catalogs. Never replace with configurationRows(): that + * projection drops native companions, unknown fields and dynamic link-local. */ +export const VRF_PRODUCTION_RAW_PATHS: Readonly> = Object.freeze({ + bridge: "/interface/bridge", bridge_port: "/interface/bridge/port", bridge_vlan: "/interface/bridge/vlan", + vrf: "/ip/vrf", ipv4_address: "/ip/address", ipv6_address: "/ipv6/address", ipv4_route: "/ip/route", ipv6_route: "/ipv6/route", + routing_table: "/routing/table", ethernet: "/interface/ethernet", ip_filter: "/ip/firewall/filter", ipv6_filter: "/ipv6/firewall/filter", + routing_rule: "/routing/rule", ip_settings: "/ip/settings", ipv6_settings: "/ipv6/settings", interface_inventory: "/interface", interface_list_members: "/interface/list/member", + vlan: "/interface/vlan", bonding: "/interface/bonding", wireguard: "/interface/wireguard", wireguard_peer: "/interface/wireguard/peers", + gre: "/interface/gre", ipip: "/interface/ipip", eoip: "/interface/eoip", vxlan: "/interface/vxlan", vxlan_vtep: "/interface/vxlan/vteps", pppoe_client: "/interface/pppoe-client", +}) +const types: Readonly> = Object.freeze({ ether: "ethernet", ethernet: "ethernet", bridge: "bridge", vlan: "vlan", bond: "bonding", bonding: "bonding", wg: "wireguard", wireguard: "wireguard", "gre-tunnel": "gre", "ipip-tunnel": "ipip", "eoip-tunnel": "eoip", vxlan: "vxlan", "pppoe-out": "pppoe_client" }) +const idPattern = /^\*[a-f0-9]+$/i +const nativeIdentity = (id: string): string => id.slice(1).replace(/^0+/, "").toLowerCase() || "0" +const singleton = new Set(["ip_settings", "ipv6_settings"]) +const MAX_ROWS = 50000, MAX_BYTES = 8 * 1024 * 1024, MAX_TOTAL_BYTES = 32 * 1024 * 1024 + +export interface VrfProductionRawClient { + /** Must return a complete GET response or throw; never slice or paginate + * silently. The real client rejects oversized/incomplete HTTP bodies. */ + get(path: string, timeoutMs?: number, signal?: AbortSignal, maxResponseBytes?: number): Promise +} +export interface VrfProductionReadSource { + serverId: number + enabled: boolean + sourceProof: string + client: VrfProductionRawClient +} +export interface VrfProductionReadAdapter { + resolve(serverId: number): Promise +} +export interface ReadVrfProductionInput { + serverId: number + expectedSourceProof: string + request: VrfProductionRequest + /** Server-owned authenticated prior closure, fixed during preparation. */ + requiredCatalogs?: readonly string[] + /** Trusted fresh authentication/permission/plan guard, required even on the + * first GET. Caller JSON must never supply this callback or the adapter. */ + assertAuthority(): Promise + adapter?: VrfProductionReadAdapter + /** Trusted in-process observer only; no reads, timers or authority are added. */ + observeRead?(value: VrfRawReadObservation): unknown +} +function insist(value: unknown, reason: string): asserts value { + if (!value) throw new Error(`VRF production read refused: ${reason}`) +} + +/** Lazy imports keep the injected model tests independent of SQL/config. This + * is the existing production routerClient/MikrotikClient factory, not a new + * endpoint or credential path. Use the identical private source-v3 proof. */ +const productionAdapter: VrfProductionReadAdapter = { + async resolve(serverId) { + const [{ routerClient }, { fingerprint, privateSecretFingerprint }] = await Promise.all([ + import("../modules/mcp/resources.js"), import("../modules/mcp/security.js"), + ]) + const { server, client } = await routerClient(serverId) + const sourceProof = await privateSecretFingerprint(fingerprint({ id: server.id, enabled: server.enabled, host: server.host, port: server.port, useSsl: server.useSsl, verifySsl: server.verifySsl, username: server.username, password: server.password, epoch: server.routingEndpointEpoch }), "mcp-ros-snapshot-source-v3") + return { serverId: server.id, enabled: server.enabled, sourceProof, client } + }, +} +function names(value: unknown): string[] { + insist(typeof value === "string", "unknown member/parent selection") + if (value === "" || value === "none") return [] + const result = value.split(",") + insist(result.length <= 1000 && result.every(name => name.trim() === name && name.length > 0 && !["all", "none", "dynamic"].includes(name)) && new Set(result).size === result.length, "ambiguous member/parent selection") + return result +} +function json(value: unknown, depth = 0): void { + insist(depth <= 16, "nested raw evidence") + if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return + insist(value && typeof value === "object" && (Array.isArray(value) || Object.getPrototypeOf(value) === Object.prototype), "non-JSON raw evidence") + for (const child of Object.values(value)) json(child, depth + 1) +} +function rows(resource: string, payload: unknown): VrfProductionRow[] { + const result = singleton.has(resource) && payload && typeof payload === "object" && !Array.isArray(payload) ? [payload] : payload + // Refuse even the exact ceiling: it could be a provider's silent row cap. + insist(Array.isArray(result) && result.length < MAX_ROWS, "incomplete/oversized raw catalog") + insist(!singleton.has(resource) || result.length === 1, "missing/ambiguous native settings") + const ids = new Set() + for (const row of result) { + insist(row && typeof row === "object" && !Array.isArray(row) && Object.getPrototypeOf(row) === Object.prototype, "invalid raw row") + json(row) + if (!singleton.has(resource)) { + insist(typeof row[".id"] === "string" && idPattern.test(row[".id"]) && !ids.has(nativeIdentity(row[".id"])), "missing/duplicate native ID") + ids.add(nativeIdentity(row[".id"])) + } + } + return structuredClone(result) as VrfProductionRow[] +} + +/** Read-only precondition evidence. This does not attest ACK ownership, native + * transition support, rollback safety or grant any executor permission. */ +export async function readVrfProductionSnapshot(input: ReadVrfProductionInput): Promise { + insist(Number.isSafeInteger(input.serverId) && input.serverId > 0 && /^[a-f0-9]{64}$/i.test(input.expectedSourceProof) && typeof input.assertAuthority === "function", "missing server/source/authority binding") + const request = structuredClone(input.request) + insist(request && ["create", "patch", "delete"].includes(request.action) && request.fields && typeof request.fields === "object" && !Array.isArray(request.fields), "invalid request") + const adapter = input.adapter ?? productionAdapter + const fence = async (): Promise => { + await input.assertAuthority() + const source = await adapter.resolve(input.serverId) + await input.assertAuthority() + insist(source.serverId === input.serverId && source.enabled === true && source.sourceProof === input.expectedSourceProof, "server/source changed") + return source + } + const catalogs: VrfProductionSnapshot["catalogs"] = {} + let totalBytes = 0 + const read = async (resource: string): Promise => { + insist(Object.hasOwn(VRF_PRODUCTION_RAW_PATHS, resource), "unknown raw resource") + notifyVrfDiagnostic(input.observeRead, { serverId: input.serverId, catalog: resource, boundary: "authority-before" }) + const source = await fence() + notifyVrfDiagnostic(input.observeRead, { serverId: input.serverId, catalog: resource, boundary: "native-get" }) + const payload = await source.client.get(VRF_PRODUCTION_RAW_PATHS[resource], 10000, undefined, MAX_BYTES) + notifyVrfDiagnostic(input.observeRead, { serverId: input.serverId, catalog: resource, boundary: "authority-after" }) + await fence() + notifyVrfDiagnostic(input.observeRead, { serverId: input.serverId, catalog: resource, boundary: "shape" }) + const catalog = rows(resource, payload) + // Catalog shape succeeded. Budgets and subsequent closure validation are + // snapshot work, not a failure of this completed native GET/catalog shape. + notifyVrfDiagnostic(input.observeRead, { serverId: input.serverId, boundary: "snapshot-validation" }) + const size = Buffer.byteLength(JSON.stringify(catalog)) + totalBytes += size + insist(size < MAX_BYTES && totalBytes < MAX_TOTAL_BYTES, "raw evidence byte budget") + catalogs[resource] = catalog + } + for (const resource of VRF_PRODUCTION_REQUIRED_CATALOGS) await read(resource) + for (const resource of [...(input.requiredCatalogs ?? [])]) if (!Object.hasOwn(catalogs, resource)) await read(resource) + const selected = new Set() + if (request.action === "create") insist(request.id === undefined && request.fields.interfaces !== undefined, "CREATE selection") + else { + const target = catalogs.vrf.filter(row => row[".id"] === request.id) + insist(target.length === 1 && target[0].name !== "main" && ![true, "true", "yes"].includes(target[0].builtin as string | boolean), "unknown/builtin target") + names(target[0].interfaces).forEach(name => selected.add(name)) + } + if (request.fields.interfaces !== undefined) names(request.fields.interfaces).forEach(name => selected.add(name)) + const inventory = new Map() + for (const row of catalogs.interface_inventory) { + insist(typeof row.name === "string" && row.name.length > 0 && typeof row.type === "string" && !inventory.has(row.name), "ambiguous interface inventory") + inventory.set(row.name, row) + } + const visiting = new Set(), visited = new Set() + const closure = async (name: string): Promise => { + insist(visiting.size < 128 && visited.size + visiting.size < 1000, "interface closure budget") + insist(!visiting.has(name), "interface parent cycle") + if (visited.has(name)) return + const native = inventory.get(name), resource = native && Object.hasOwn(types, String(native.type)) ? types[String(native!.type)] : undefined + insist(native && resource, "unsupported/missing concrete member or parent") + visiting.add(name) + if (!Object.hasOwn(catalogs, resource)) await read(resource) + const configs = catalogs[resource].filter(row => row.name === name) + insist(configs.length === 1, "missing/ambiguous concrete configuration") + const config = configs[0] + if (resource === "wireguard") { + if (!Object.hasOwn(catalogs, "wireguard_peer")) await read("wireguard_peer") + for (const peer of catalogs.wireguard_peer.filter(row => row.interface === name)) { + const endpoint = peer["endpoint-address"] + insist(endpoint === undefined || endpoint === "" || typeof endpoint === "string" && isIP(endpoint) > 0, "unresolved WireGuard endpoint") + } + } + if (resource === "vxlan") { + if (!Object.hasOwn(catalogs, "vxlan_vtep")) await read("vxlan_vtep") + for (const vtep of catalogs.vxlan_vtep.filter(row => row.interface === name)) { + insist(typeof vtep["remote-ip"] === "string" && isIP(vtep["remote-ip"]) > 0, "unresolved VXLAN VTEP") + } + } + // A raw interface inventory row and type-specific configuration can have + // different IDs. Preserve both rather than inventing a shared identity. + if (resource === "vlan") insist(names(config.interface).length === 1, "missing VLAN parent") + if (resource === "bonding") insist(names(config.slaves).length > 0, "missing bonding slaves") + if (resource === "pppoe_client") insist(names(config.interface).length === 1, "missing PPPoE parent") + // Tunnel endpoints are not interface names. An explicit local address + // binds their concrete parent; wildcard/autoselected source has no such + // identity proof and remains refused. The full route catalogs are sealed. + if (["gre", "ipip", "eoip", "vxlan"].includes(resource)) { + const local = config["local-address"] + insist(typeof local === "string" && isIP(local) > 0 && !["0.0.0.0", "::"].includes(local), "unbound tunnel local source") + if (resource !== "vxlan") insist(typeof config["remote-address"] === "string" && isIP(config["remote-address"]) > 0, "unresolved tunnel endpoint") + const bound = [...catalogs.ipv4_address, ...catalogs.ipv6_address].filter(row => typeof row.address === "string" && row.address.split("/")[0] === local) + insist(bound.length === 1 && typeof bound[0].interface === "string" && [false, "false", "no"].includes(bound[0].dynamic as string | boolean), "ambiguous/dynamic tunnel source parent") + await closure(bound[0].interface) + } + for (const field of ["interface", "interfaces", "slaves", "bridge", "master-interface"]) { + if (config[field] !== undefined) for (const parent of names(config[field])) await closure(parent) + } + if (resource === "bridge") { + for (const port of catalogs.bridge_port.filter(row => row.bridge === name)) { + const parents = names(port.interface) + insist(parents.length === 1, "ambiguous bridge port parent") + await closure(parents[0]) + } + for (const vlan of catalogs.bridge_vlan.filter(row => row.bridge === name)) { + for (const field of ["tagged", "untagged", "current-tagged", "current-untagged"]) { + if (vlan[field] !== undefined) for (const parent of names(vlan[field])) if (parent !== name) await closure(parent) + } + } + } + visiting.delete(name); visited.add(name) + } + for (const name of selected) await closure(name) + // Keep every address, including dynamic LL. Validate all closure addresses; + // no /32 or /128 fixture assumption and no claimed ownership of live rows. + for (const resource of ["ipv4_address", "ipv6_address"] as const) for (const address of catalogs[resource]) { + if (typeof address.interface !== "string" || !visited.has(address.interface)) continue + insist(typeof address.address === "string", "unknown native address") + const [host, prefix, extra] = address.address.split("/") + const family = resource === "ipv4_address" ? 4 : 6 + insist(extra === undefined && isIP(host) === family && /^\d+$/.test(prefix ?? "") && Number(prefix) <= (family === 4 ? 32 : 128), "invalid native address prefix") + } + notifyVrfDiagnostic(input.observeRead, { serverId: input.serverId, boundary: "final-authority" }) + await fence() + notifyVrfDiagnostic(input.observeRead, { serverId: input.serverId, boundary: "snapshot-validation" }) + return { projection: "raw-native-v1", serverId: input.serverId, sourceProof: input.expectedSourceProof, catalogs } +} diff --git a/backend/src/services/vrf-production-ticket-store-sql.test.ts b/backend/src/services/vrf-production-ticket-store-sql.test.ts new file mode 100644 index 00000000..d9a1d53f --- /dev/null +++ b/backend/src/services/vrf-production-ticket-store-sql.test.ts @@ -0,0 +1,90 @@ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, randomBytes, randomUUID } from "node:crypto" +import { VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionRequest, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { claimStoredVrfProductionTicket, inspectStoredVrfProductionTicket, prepareVrfProductionTicket, type StoredVrfProductionTicketInput, type VrfTicketCrypto, type VrfTicketSql, type VrfTicketClaimConnections } from "./vrf-production-ticket-store.js" +import { VRF_PRODUCTION_RAW_PATHS, type VrfProductionRawClient } from "./vrf-production-read.js" + +/** Actual SQL acceptance helper: caller supplies an isolated migrated PostgreSQL + * database and real transaction implementation. Import is inert; no production + * database, RouterOS client, environment or app configuration is opened here. */ +export async function runVrfProductionTicketSqlAcceptance(input: { sql: VrfTicketSql; claimConnections: VrfTicketClaimConnections; transaction(work: (sql: VrfTicketSql) => Promise): Promise }): Promise<{ passed: true; cases: number }> { + const key = randomBytes(32), secret = randomBytes(32), sourceProof = "a".repeat(64) + const codec: VrfTicketCrypto = { + async encrypt(value) { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv); const data = Buffer.concat([cipher.update(value), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), data]).toString("base64") }, + async decrypt(value) { const data = Buffer.from(value, "base64"), cipher = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)); cipher.setAuthTag(data.subarray(12, 28)); return Buffer.concat([cipher.update(data.subarray(28)), cipher.final()]).toString() }, + } + const connectionId = randomUUID(), owner = `vrf-ticket-${randomUUID()}` + const server = (await input.sql.query("INSERT INTO servers(name,host,enabled) VALUES($1,'192.0.2.254',true) RETURNING id", [owner])).rows[0] + const serverId = Number(server.id) + await input.sql.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation) VALUES($1,$2,$2,'{}','{}','test')", [connectionId, owner]) + const plans: string[] = [] + let cases = 0 + try { + const fixture = async () => { + const planId = randomUUID(), request: VrfProductionRequest = { action: "patch", id: "*1", fields: { name: "customers" } } + plans.push(planId) + const evidence = { target: { scope: "servers", serverIds: [serverId] }, steps: [{ kind: "ros", step: { serverId, resource: "vrf", action: "update", id: "*1", fields: request.fields } }] } + await input.sql.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) + VALUES($1,$2,'ros.vrf.update',2,1,'test','{}',$3::jsonb,'draft',clock_timestamp()+interval '5 minutes')`, [planId, connectionId, JSON.stringify({ sealed: await codec.encrypt(JSON.stringify(evidence)) })]) + const catalogs: VrfProductionSnapshot["catalogs"] = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, []])) + catalogs.ip_settings = [{}]; catalogs.ipv6_settings = [{}] + catalogs.vrf = [{ ".id": "*1", name: "production", interfaces: "ether1", disabled: "false" }, { ".id": "*2", name: "main", interfaces: "all", builtin: "true" }] + catalogs.interface_inventory = [{ ".id": "*10", name: "ether1", type: "ether" }] + catalogs.ethernet = [{ ".id": "*20", name: "ether1", comment: "raw-private-fixture" }] + const context = { binding: { serverId, sourceProof, actorId: owner, planId }, connectionId, actionId: "ros.vrf.update" as const, actionVersion: 2 } + let authorized = true, sourceCurrent = true + const guards = { async assertAuthority() { assert.ok(authorized, "revoked") }, async assertCurrentSource() { assert.ok(sourceCurrent, "source changed") } } + const client: VrfProductionRawClient = { async get(path: string): Promise { const resource = Object.keys(VRF_PRODUCTION_RAW_PATHS).find(key => VRF_PRODUCTION_RAW_PATHS[key] === path)!; return structuredClone(catalogs[resource]) as T } } + const prepared = await prepareVrfProductionTicket({ context, request, secret, crypto: codec, ...guards, transaction: input.transaction, adapter: { async resolve() { return { serverId, sourceProof, enabled: true, client } } } }) + assert.equal(prepared.executionAvailable, false) + assert.equal(JSON.stringify(prepared).includes("raw-private-fixture"), false) + const stored = (await input.sql.query("SELECT * FROM vrf_production_tickets WHERE ticket_id=$1", [prepared.ticketId])).rows[0] + assert.equal(stored.state, "issued"); assert.equal(stored.ciphertext.includes("raw-private-fixture"), false) + const claim: StoredVrfProductionTicketInput = { context, request, secret, crypto: codec, ...guards, sql: input.sql, claimConnections: input.claimConnections, ticketId: prepared.ticketId, snapshot: { projection: "raw-native-v1", serverId, sourceProof, catalogs } } + return { claim, stored, prepared, revoke() { authorized = false }, changeSource() { sourceCurrent = false } } + } + const concurrent = await fixture() + const results = await Promise.allSettled([claimStoredVrfProductionTicket(concurrent.claim), claimStoredVrfProductionTicket(concurrent.claim)]) + assert.equal(results.filter(result => result.status === "fulfilled").length, 1) + await assert.rejects(claimStoredVrfProductionTicket(concurrent.claim)); cases++ + const badDigest = await fixture() + await input.sql.query("UPDATE vrf_production_tickets SET digest=$2 WHERE ticket_id=$1", [badDigest.prepared.ticketId, "b".repeat(64)]) + await assert.rejects(claimStoredVrfProductionTicket(badDigest.claim), /HMAC\/digest/); cases++ + const expired = await fixture() + await input.sql.query("UPDATE vrf_production_tickets SET issued_at=clock_timestamp()-interval '10 minutes',expires_at=clock_timestamp()-interval '9 minutes' WHERE ticket_id=$1", [expired.prepared.ticketId]) + await assert.rejects(claimStoredVrfProductionTicket(expired.claim)); cases++ + const revoked = await fixture(); revoked.revoke() + await assert.rejects(claimStoredVrfProductionTicket(revoked.claim)); cases++ + const changed = await fixture(); changed.changeSource() + await assert.rejects(claimStoredVrfProductionTicket(changed.claim)); cases++ + const ownerChanged = await fixture() + await assert.rejects(claimStoredVrfProductionTicket({ ...ownerChanged.claim, context: { ...ownerChanged.claim.context, binding: { ...ownerChanged.claim.context.binding, actorId: "other-owner" } } })); cases++ + const actionChanged = await fixture() + await input.sql.query("UPDATE mcp_plans SET operation_version=3 WHERE id=$1", [actionChanged.claim.context.binding.planId]) + await assert.rejects(claimStoredVrfProductionTicket(actionChanged.claim)); cases++ + const lostAck = await fixture() + const uncertainConnections: VrfTicketClaimConnections = { async connect() { const connection = await input.claimConnections.connect(); return { async query(text, values) { const result = await connection.query(text, values); if (text === "COMMIT") throw new Error("lost COMMIT ACK"); return result }, release(destroy) { connection.release(destroy) } } } } + await assert.rejects(claimStoredVrfProductionTicket({ ...lostAck.claim, claimConnections: uncertainConnections }), /outcome unknown/) + assert.equal((await input.sql.query("SELECT state FROM vrf_production_tickets WHERE ticket_id=$1", [lostAck.prepared.ticketId])).rows[0].state, "claimed") + await assert.rejects(claimStoredVrfProductionTicket(lostAck.claim)); cases++ + const revokeAfter = await fixture() + const revokingConnections: VrfTicketClaimConnections = { async connect() { const connection = await input.claimConnections.connect(); return { async query(text, values) { const result = await connection.query(text, values); if (text === "COMMIT") revokeAfter.revoke(); return result }, release(destroy) { connection.release(destroy) } } } } + await assert.rejects(claimStoredVrfProductionTicket({ ...revokeAfter.claim, claimConnections: revokingConnections })) + assert.equal((await input.sql.query("SELECT state FROM vrf_production_tickets WHERE ticket_id=$1", [revokeAfter.prepared.ticketId])).rows[0].state, "claimed"); cases++ + const hmac = await fixture() + await assert.rejects(inspectStoredVrfProductionTicket({ ...hmac.claim, secret: randomBytes(32) }), /signature changed/); cases++ + const callerRollback = await fixture() + await assert.rejects(input.transaction(async callerTransaction => { + await claimStoredVrfProductionTicket({ ...callerRollback.claim, sql: callerTransaction }) + throw new Error("caller downstream failure") + }), /caller downstream failure/) + assert.equal((await input.sql.query("SELECT state FROM vrf_production_tickets WHERE ticket_id=$1", [callerRollback.prepared.ticketId])).rows[0].state, "claimed") + await assert.rejects(claimStoredVrfProductionTicket(callerRollback.claim)); cases++ + return { passed: true, cases } + } finally { + await input.sql.query("DELETE FROM vrf_production_tickets WHERE connection_id=$1", [connectionId]) + await input.sql.query("DELETE FROM mcp_plans WHERE id=ANY($1::uuid[])", [plans]) + await input.sql.query("DELETE FROM mcp_connections WHERE id=$1", [connectionId]) + await input.sql.query("DELETE FROM servers WHERE id=$1", [serverId]) + } +} diff --git a/backend/src/services/vrf-production-ticket-store.test.ts b/backend/src/services/vrf-production-ticket-store.test.ts new file mode 100644 index 00000000..520b9e01 --- /dev/null +++ b/backend/src/services/vrf-production-ticket-store.test.ts @@ -0,0 +1,102 @@ +import assert from "node:assert/strict" +import { createCipheriv, createDecipheriv, randomBytes, randomUUID } from "node:crypto" +import { VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { VRF_PRODUCTION_RAW_PATHS, type VrfProductionRawClient } from "./vrf-production-read.js" +import { claimStoredVrfProductionTicket, inspectStoredVrfProductionTicket, prepareVrfProductionTicket, type StoredVrfProductionTicketInput, type VrfTicketCrypto, type VrfTicketSql, type VrfTicketClaimConnections } from "./vrf-production-ticket-store.js" + +interface ModelTicket extends Record { + ticket_id: string; plan_id: string; connection_id: string; server_id: number; owner_id: string + action_id: string; action_version: number; source_proof: string; digest: string; ciphertext: string + issued_at: Date; expires_at: Date; state: string +} + +async function fixture() { + const key = randomBytes(32), secret = randomBytes(32), sourceProof = "a".repeat(64) + const codec: VrfTicketCrypto = { + async encrypt(value) { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv); const data = Buffer.concat([cipher.update(value), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), data]).toString("base64") }, + async decrypt(value) { const data = Buffer.from(value, "base64"), cipher = createDecipheriv("aes-256-gcm", key, data.subarray(0, 12)); cipher.setAuthTag(data.subarray(12, 28)); return Buffer.concat([cipher.update(data.subarray(28)), cipher.final()]).toString() }, + } + const context = { binding: { serverId: 7, sourceProof, actorId: "human-owner", planId: randomUUID() }, connectionId: randomUUID(), actionId: "ros.vrf.update" as const, actionVersion: 2 } + const request = { action: "patch" as const, id: "*1", fields: { name: "customers" } } + const evidence = { target: { scope: "servers", serverIds: [7] }, steps: [{ kind: "ros", step: { serverId: 7, resource: "vrf", action: "update", id: "*1", fields: { ...request.fields } } }] } + const plan = { id: context.binding.planId, connection_id: context.connectionId, operation_id: context.actionId, operation_version: 2, owner_id: context.binding.actorId, state: "draft", expires_at: new Date(Date.now() + 300000), evidence: { sealed: await codec.encrypt(JSON.stringify(evidence)) } } + const catalogs: VrfProductionSnapshot["catalogs"] = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, []])) + catalogs.ip_settings = [{}]; catalogs.ipv6_settings = [{}] + catalogs.vrf = [{ ".id": "*1", name: "production", interfaces: "ether1", disabled: "false" }, { ".id": "*2", name: "main", interfaces: "all", builtin: "true" }] + catalogs.interface_inventory = [{ ".id": "*10", name: "ether1", type: "ether" }] + catalogs.ethernet = [{ ".id": "*20", name: "ether1", comment: "never-public-configuration" }] + const tickets = new Map() + let authority = true, source = true, gets = 0, claimUpdates = 0, afterClaim: (() => void) | undefined + const sql: VrfTicketSql = { async query(text, values = []) { + if (text.startsWith("SELECT p.id")) return { rows: values[0] === plan.id && values[1] === plan.connection_id && values[3] === plan.owner_id && values[4] === plan.operation_id && values[5] === plan.operation_version && ["draft", "approved", "awaiting-approval"].includes(plan.state) ? [structuredClone(plan)] : [] } + if (text.startsWith("INSERT INTO vrf_production_tickets")) { + assert.ok(![...tickets.values()].some(row => row.plan_id === values[1]), "unique plan receipt") + const stringAt = (index: number): string => { const value = values[index]; assert.equal(typeof value, "string"); return value as string } + const numberAt = (index: number): number => { const value = values[index]; assert.equal(typeof value, "number"); return value as number } + const dateAt = (index: number): Date => { const value = values[index]; assert.ok(value instanceof Date); return value } + const row: ModelTicket = { ticket_id: stringAt(0), plan_id: stringAt(1), connection_id: stringAt(2), server_id: numberAt(3), owner_id: stringAt(4), action_id: stringAt(5), action_version: numberAt(6), source_proof: stringAt(7), digest: stringAt(8), ciphertext: stringAt(9), issued_at: dateAt(10), expires_at: dateAt(11), state: "issued" } + tickets.set(String(values[0]), row); return { rows: [{ ticket_id: values[0] }] } + } + if (text.startsWith("SELECT ciphertext")) { + const row = tickets.get(String(values[0])) + return { rows: row && row.state === "issued" && row.plan_id === values[1] && row.connection_id === values[2] && row.server_id === values[3] && row.owner_id === values[4] && row.action_id === values[5] && row.action_version === values[6] && row.source_proof === values[7] && row.expires_at.getTime() > Date.now() ? [structuredClone(row)] : [] } + } + if (text.startsWith("UPDATE vrf_production_tickets t")) { + claimUpdates++ + const row = tickets.get(String(values[0])) + if (!row || row.state !== "issued" || row.digest !== values[1] || row.plan_id !== values[2] || row.connection_id !== values[3] || row.server_id !== values[4] || row.owner_id !== values[5] || row.action_id !== values[6] || row.action_version !== values[7] || row.source_proof !== values[8] || plan.operation_version !== values[7] || !["draft", "approved", "awaiting-approval"].includes(plan.state)) return { rows: [] } + row.state = "claimed"; afterClaim?.(); return { rows: [{ ticket_id: values[0] }] } + } + throw new Error("Unexpected model SQL") + } } + const guards = { async assertAuthority() { assert.ok(authority, "authority revoked") }, async assertCurrentSource() { assert.ok(source, "source changed") } } + const claimConnections: VrfTicketClaimConnections = { async connect() { return { async query(text, values) { if (["BEGIN", "COMMIT", "ROLLBACK"].includes(text)) return { rows: [] }; return sql.query(text, values) }, release() {} } } } + const client: VrfProductionRawClient = { async get(path: string): Promise { gets++; const resource = Object.keys(VRF_PRODUCTION_RAW_PATHS).find(key => VRF_PRODUCTION_RAW_PATHS[key] === path)!; return structuredClone(catalogs[resource]) as T } } + const prepare = { context, request, secret, crypto: codec, ...guards, async transaction(work: (sql: VrfTicketSql) => Promise): Promise { const old = structuredClone(tickets); try { return await work(sql) } catch (error) { tickets.clear(); for (const [id, row] of old) tickets.set(id, row); throw error } }, adapter: { async resolve() { return { serverId: 7, sourceProof, enabled: true, client } } } } + const issue = async () => { + const prepared = await prepareVrfProductionTicket(prepare) + const claim: StoredVrfProductionTicketInput = { context, request, secret, crypto: codec, ...guards, sql, claimConnections, ticketId: prepared.ticketId, snapshot: { projection: "raw-native-v1", serverId: 7, sourceProof, catalogs } } + return { prepared, claim, row: tickets.get(prepared.ticketId)! } + } + return { prepare, issue, tickets, plan, sql, revoke() { authority = false }, changeSource() { source = false }, afterClaim(callback: () => void) { afterClaim = callback }, counters() { return { gets, claimUpdates } } } +} +const good = await fixture(), issued = await good.issue() +assert.equal(issued.prepared.executionAvailable, false) +assert.equal(Object.keys(issued.prepared).sort().join(","), "executionAvailable,expiresAt,ticketId") +assert.equal(JSON.stringify(issued.prepared).includes("never-public-configuration"), false) +assert.equal(issued.row.ciphertext.includes("never-public-configuration"), false) +assert.equal(good.counters().gets, 17) +assert.equal((await inspectStoredVrfProductionTicket(issued.claim)).admission.companionExecution, "requires-native-transition-proof") +const race = await Promise.allSettled([claimStoredVrfProductionTicket(issued.claim), claimStoredVrfProductionTicket(issued.claim)]) +assert.equal(race.filter(result => result.status === "fulfilled").length, 1) +assert.equal(issued.row.state, "claimed") +await assert.rejects(claimStoredVrfProductionTicket(issued.claim)) +await assert.rejects(good.issue(), /unique plan receipt/) +for (const revoke of ["authority", "source"]) { + const m = await fixture(); if (revoke === "authority") m.revoke(); else m.changeSource() + await assert.rejects(m.issue()); assert.equal(m.tickets.size, 0); assert.equal(m.counters().gets, 0) +} +const owner = await fixture(); owner.prepare.context.binding.actorId = "wrong-owner" +await assert.rejects(owner.issue(), /authoritative plan/); assert.equal(owner.counters().gets, 0) +const forged = await fixture(); forged.prepare.request.fields.name = "forged-request" +await assert.rejects(forged.issue(), /authoritative plan/); assert.equal(forged.counters().gets, 0) +const wrongHmac = await fixture(), hmacIssued = await wrongHmac.issue() +await assert.rejects(claimStoredVrfProductionTicket({ ...hmacIssued.claim, secret: randomBytes(32) }), /signature changed/) +assert.equal(wrongHmac.counters().claimUpdates, 0) +const wrongDigest = await fixture(), digestIssued = await wrongDigest.issue(); digestIssued.row.digest = "b".repeat(64) +await assert.rejects(claimStoredVrfProductionTicket(digestIssued.claim), /HMAC\/digest/) +assert.equal(wrongDigest.counters().claimUpdates, 0) +const expired = await fixture(), expiredIssued = await expired.issue(); expiredIssued.row.expires_at = new Date(0) +await assert.rejects(claimStoredVrfProductionTicket(expiredIssued.claim), /expired ticket/) +const version = await fixture(), versionIssued = await version.issue(); version.plan.operation_version++ +await assert.rejects(claimStoredVrfProductionTicket(versionIssued.claim), /authoritative plan/) +const lost = await fixture(), lostIssued = await lost.issue() +let destroyedLostConnection = false, rolledBackLostCommit = false +const lostConnections: VrfTicketClaimConnections = { async connect() { const connection = await lostIssued.claim.claimConnections!.connect(); return { async query(text, values) { if (text === "ROLLBACK") rolledBackLostCommit = true; const result = await connection.query(text, values); if (text === "COMMIT") throw new Error("lost ACK"); return result }, release(destroy) { destroyedLostConnection = destroy === true; connection.release(destroy) } } } } +await assert.rejects(claimStoredVrfProductionTicket({ ...lostIssued.claim, claimConnections: lostConnections }), /outcome unknown/) +assert.equal(destroyedLostConnection, true); assert.equal(rolledBackLostCommit, false) +assert.equal(lostIssued.row.state, "claimed"); await assert.rejects(claimStoredVrfProductionTicket(lostIssued.claim)) +const after = await fixture(), afterIssued = await after.issue(); after.afterClaim(() => after.revoke()) +await assert.rejects(claimStoredVrfProductionTicket(afterIssued.claim), /authority revoked/) +assert.equal(afterIssued.row.state, "claimed") +console.log("VRF production ticket store injected model tests passed; no executor grant") diff --git a/backend/src/services/vrf-production-ticket-store.ts b/backend/src/services/vrf-production-ticket-store.ts new file mode 100644 index 00000000..551b2e7a --- /dev/null +++ b/backend/src/services/vrf-production-ticket-store.ts @@ -0,0 +1,256 @@ +import { inspectVrfProductionAdmission, sealVrfProductionAdmission, type VrfProductionAdmission, type VrfProductionBinding, type VrfProductionRequest, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { readVrfProductionSnapshot, type VrfProductionReadAdapter } from "./vrf-production-read.js" +import { assertVrfActionContext, assertVrfAliasEvidence, vrfPublicAuthority, type VrfActionContext } from "./vrf-production-action-binding.js" +import type { ChangeEvidence } from "../modules/mcp/action-types.js" + +export interface VrfTicketSql { + query(text: string, values?: unknown[]): Promise<{ rows: Record[]; command?: string }> +} +export interface VrfTicketClaimConnection extends VrfTicketSql { release(destroy?: boolean): void } +/** Test injection must return a fresh unshared, nontransactional connection. + * Production always owns a pool checkout; a caller transaction is never used. */ +export interface VrfTicketClaimConnections { connect(): Promise } +const ownedProductionConnections: VrfTicketClaimConnections = { + async connect() { return (await import("../db/index.js")).pool.connect() }, +} +export class VrfProductionClaimOutcomeUnknown extends Error { + readonly requiresDurableOperationQuarantine = true + constructor(readonly ticketId: string) { super("VRF production ticket refused: claim outcome unknown; quarantine the operation, no retry or execution permitted") } +} +export interface VrfTicketCrypto { encrypt(value: string): Promise; decrypt(value: string): Promise } +const privateCrypto: VrfTicketCrypto = { + async encrypt(value) { return (await import("../modules/mcp/security.js")).encryptSecret(value) }, + async decrypt(value) { return (await import("../modules/mcp/security.js")).decryptSecret(value) }, +} +export interface VrfTicketContext extends VrfActionContext { + binding: VrfProductionBinding + connectionId: string +} +interface Guards { + /** Trusted fresh credential/permission/policy/plan check, never caller JSON. */ + assertAuthority(): Promise + /** Fresh trusted source-v3 endpoint/epoch/enabled check against context. */ + assertCurrentSource(): Promise +} +interface Shared extends Guards { + context: VrfTicketContext + request: VrfProductionRequest + secret: Uint8Array + crypto?: VrfTicketCrypto + now?: () => number + /** Server-owned currently running operation, never a tool/plan input. */ + execution?: { operationId: string; stepIndex: number } +} +const uuid = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i +const proof = /^[a-f0-9]{64}$/ +function must(value: unknown, reason: string): asserts value { + if (!value) throw new Error(`VRF production ticket refused: ${reason}`) +} +function validate(context: VrfTicketContext, request: VrfProductionRequest): void { + must(context && uuid.test(context.connectionId) && uuid.test(context.binding.planId) && Number.isSafeInteger(context.binding.serverId) && context.binding.serverId > 0 && proof.test(context.binding.sourceProof) && context.binding.actorId.length > 0, "invalid binding") + assertVrfActionContext(context, request) +} +function pinned(input: T): T { + must(input.secret instanceof Uint8Array && input.secret.byteLength >= 32 && input.secret.byteLength <= 4096 && typeof input.assertAuthority === "function" && typeof input.assertCurrentSource === "function", "missing trusted guards/sealing key") + if (input.execution) must(uuid.test(input.execution.operationId) && Number.isSafeInteger(input.execution.stepIndex) && input.execution.stepIndex >= 0, "invalid execution identity") + return { ...input, context: structuredClone(input.context), request: structuredClone(input.request), secret: Uint8Array.from(input.secret), ...(input.execution ? { execution: structuredClone(input.execution) } : {}) } +} +async function fence(input: Guards): Promise { + await input.assertAuthority(); await input.assertCurrentSource(); await input.assertAuthority() +} +function canonical(value: unknown, depth = 0): string { + must(depth <= 24, "nested plan evidence") + if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return JSON.stringify(value) + if (Array.isArray(value)) return `[${value.map(child => canonical(child, depth + 1)).join(",")}]` + must(value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype, "invalid plan evidence") + return `{${Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, child]) => `${JSON.stringify(key)}:${canonical(child, depth + 1)}`).join(",")}}` +} +function envelopeValue(value: unknown): string { + must(value && typeof value === "object" && !Array.isArray(value) && typeof (value as { sealed?: unknown }).sealed === "string", "missing encrypted plan evidence") + return (value as { sealed: string }).sealed +} +function record(value: unknown, reason: string): Record { + must(value && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype, reason) + return value as Record +} +function timestampMs(value: unknown): number { + must(value instanceof Date || typeof value === "string" && value.length > 0 && value.length <= 64, "invalid stored timestamp") + const result = value instanceof Date ? value.getTime() : Date.parse(value) + must(Number.isSafeInteger(result) && result >= 0, "invalid stored timestamp") + return result +} + +/** Verify the real plan's owner/action/version and its encrypted single VRF + * step. An arbitrary caller-supplied plan UUID or ownership assertion is not + * sufficient. Preparation invokes this inside the caller's plan transaction. */ +async function authoritativePlan(sql: VrfTicketSql, input: Shared, lock: boolean): Promise<{ expiresAtMs: number }> { + const c = input.context + const authority = vrfPublicAuthority(c) + const { rows } = await sql.query(`SELECT p.id,p.connection_id,p.operation_id,p.operation_version,p.state,p.expires_at,p.evidence,c.owner_id + FROM mcp_plans p JOIN mcp_connections c ON c.id=p.connection_id JOIN servers s ON s.id=$3 + WHERE p.id=$1 AND p.connection_id=$2 AND c.owner_id=$4 AND p.operation_id=$5 AND p.operation_version=$6 + AND (($7::uuid IS NULL AND p.state IN ('draft','awaiting-approval','approved')) OR + ($7::uuid IS NOT NULL AND p.state='executing' AND p.approved_by IS NOT NULL AND EXISTS ( + SELECT 1 FROM mcp_operations o JOIN mcp_operation_steps st ON st.operation_id=o.id + WHERE o.id=$7 AND o.plan_id=p.id AND o.connection_id=p.connection_id AND o.state='running' + AND st.step_index=$8 AND st.state='intent' AND st.server_id=s.id))) + AND p.expires_at>clock_timestamp() AND s.enabled=true + ${lock ? "FOR UPDATE OF p,c" : ""}`, [c.binding.planId, c.connectionId, c.binding.serverId, c.binding.actorId, authority.id, authority.version, input.execution?.operationId ?? null, input.execution?.stepIndex ?? null]) + await fence(input) + must(rows.length === 1, "unknown/revoked/expired authoritative plan") + const plan = rows[0], codec = input.crypto ?? privateCrypto + const plaintext = await codec.decrypt(envelopeValue(plan.evidence)) + await fence(input) + must(plaintext.length <= 2 * 1024 * 1024, "oversized plan evidence") + const evidence = record(JSON.parse(plaintext) as unknown, "invalid encrypted plan evidence") + if (input.execution) must(evidence.vrfProductionAdmission === true, "executing plan lacks VRF production admission") + const target = record(evidence.target, "plan scope/step ambiguity") + must(target.scope === "servers" && Array.isArray(target.serverIds) && target.serverIds.length === 1 && target.serverIds[0] === c.binding.serverId && Array.isArray(evidence.steps) && evidence.steps.length === 1, "plan scope/step ambiguity") + const step = record(evidence.steps[0], "plan is not exact VRF step") + const native = record(step.step, "plan is not exact VRF step") + must(step.kind === "ros" && native.resource === "vrf" && native.serverId === c.binding.serverId, "plan is not exact VRF step") + const request = { action: native.action === "update" ? "patch" : native.action, ...(native.id !== undefined ? { id: native.id } : {}), fields: native.fields, ...(native.placeBeforeId !== undefined ? { placeBeforeId: native.placeBeforeId } : {}) } + must(canonical(request) === canonical(input.request), "request differs from authoritative plan") + assertVrfAliasEvidence(c, input.request, evidence as unknown as Pick) + return { expiresAtMs: timestampMs(plan.expires_at) } +} + +export interface PrepareVrfProductionTicketInput extends Shared { + requiredCatalogs?: readonly string[] + /** Existing trusted plan transaction (including the actual plan INSERT if + * new). The caller owns BEGIN/COMMIT. Never create a synthetic plan here. */ + transaction(work: (sql: VrfTicketSql) => Promise): Promise + adapter?: VrfProductionReadAdapter + /** Production runtime matches its already encrypted original projections; + * raw preparation must not silently adopt a later dependency snapshot. */ + assertPreparedSnapshot?(snapshot: VrfProductionSnapshot): Promise + onAdmission?(admission: VrfProductionAdmission, snapshot: VrfProductionSnapshot): Promise +} +/** Server-internal preparation primitive. No public tool is registered. The + * returned reference contains no sealed payload, raw configuration or grant. */ +export async function prepareVrfProductionTicket(input: PrepareVrfProductionTicketInput): Promise<{ ticketId: string; expiresAt: string; executionAvailable: false }> { + input = pinned(input) + must(!input.execution, "execution cannot mint a preparation ticket") + validate(input.context, input.request) + await fence(input) + return input.transaction(async sql => { + const plan = await authoritativePlan(sql, input, true) + const snapshot = await readVrfProductionSnapshot({ serverId: input.context.binding.serverId, expectedSourceProof: input.context.binding.sourceProof, request: input.request, requiredCatalogs: input.requiredCatalogs, assertAuthority: () => fence(input), adapter: input.adapter }) + await input.assertPreparedSnapshot?.(snapshot) + await fence(input) + const now = (input.now ?? Date.now)(), planExpiry = plan.expiresAtMs + const ttlMs = Math.min(300000, planExpiry - now) + const issued = sealVrfProductionAdmission({ snapshot, binding: input.context.binding, request: input.request, secret: input.secret, now, ttlMs }) + await input.onAdmission?.(structuredClone(issued.admission), snapshot) + const envelope = JSON.stringify({ context: input.context, sealed: issued.sealed }) + must(envelope.length <= 2 * 1024 * 1024, "oversized private envelope") + const ciphertext = await (input.crypto ?? privateCrypto).encrypt(envelope) + await fence(input) + const c = input.context + const authority = vrfPublicAuthority(c) + const { rows } = await sql.query(`INSERT INTO vrf_production_tickets(ticket_id,plan_id,connection_id,server_id,owner_id,action_id,action_version,source_proof,digest,ciphertext,issued_at,expires_at) + SELECT $1,p.id,p.connection_id,$4,$5,$6,$7,$8,$9,$10,$11,$12 + FROM mcp_plans p JOIN mcp_connections c ON c.id=p.connection_id JOIN servers s ON s.id=$4 + WHERE p.id=$2 AND p.connection_id=$3 AND c.owner_id=$5 AND p.operation_id=$13 AND p.operation_version=$14 AND s.enabled=true + AND p.state IN ('draft','awaiting-approval','approved') AND p.expires_at>=$12 AND $12>clock_timestamp() + RETURNING ticket_id`, [issued.admission.ticketId, c.binding.planId, c.connectionId, c.binding.serverId, c.binding.actorId, c.actionId, c.actionVersion, c.binding.sourceProof, issued.digest, ciphertext, new Date(issued.admission.issuedAt), new Date(issued.admission.expiresAt), authority.id, authority.version]) + must(rows.length === 1, "plan changed before ticket issue") + await fence(input) + return { ticketId: issued.admission.ticketId, expiresAt: new Date(issued.admission.expiresAt).toISOString(), executionAvailable: false as const } + }) +} + +export interface StoredVrfProductionTicketInput extends Shared { + /** Read-only inspection handle. Never used for claim writes. */ + sql: VrfTicketSql + /** Absent in production: use a fresh owned connection from the real pool. */ + claimConnections?: VrfTicketClaimConnections + ticketId: string + /** Trusted fresh readVrfProductionSnapshot result, never caller JSON. */ + snapshot: VrfProductionSnapshot +} +export async function inspectStoredVrfProductionTicket(input: StoredVrfProductionTicketInput): Promise & { privateSealed?: string }> { + input = pinned(input) + validate(input.context, input.request); must(uuid.test(input.ticketId), "invalid ticket ID") + await fence(input) + await authoritativePlan(input.sql, input, false) + const c = input.context + const { rows } = await input.sql.query(`SELECT ciphertext,digest,issued_at,expires_at FROM vrf_production_tickets + WHERE ticket_id=$1 AND plan_id=$2 AND connection_id=$3 AND server_id=$4 AND owner_id=$5 AND action_id=$6 AND action_version=$7 + AND source_proof=$8 AND state='issued' AND expires_at>clock_timestamp()`, [input.ticketId, c.binding.planId, c.connectionId, c.binding.serverId, c.binding.actorId, c.actionId, c.actionVersion, c.binding.sourceProof]) + await fence(input) + must(rows.length === 1, "unknown/claimed/expired ticket") + const row = rows[0] + must(typeof row.ciphertext === "string" && row.ciphertext.length <= 4 * 1024 * 1024, "oversized/missing ciphertext") + const plaintext = await (input.crypto ?? privateCrypto).decrypt(row.ciphertext) + await fence(input) + must(plaintext.length <= 2 * 1024 * 1024, "oversized private envelope") + const envelope = record(JSON.parse(plaintext) as unknown, "invalid private envelope") + must(canonical(envelope.context) === canonical(c), "encrypted action/owner binding changed") + must(typeof envelope.sealed === "string", "missing signed private envelope") + const inspected = inspectVrfProductionAdmission({ sealed: envelope.sealed, snapshot: input.snapshot, binding: c.binding, request: input.request, secret: input.secret, now: (input.now ?? Date.now)() }) + must(inspected.admission.ticketId === input.ticketId && inspected.digest === row.digest && inspected.admission.issuedAt === timestampMs(row.issued_at) && inspected.admission.expiresAt === timestampMs(row.expires_at), "stored ticket HMAC/digest/lifetime mismatch") + return { ...inspected, ...(input.execution ? { privateSealed: envelope.sealed } : {}) } +} + +/** A successful claim is a spent review/proof attempt, never a mutation grant. + * There is no reset, delete, upsert or retry path. On lost ACK after committed + * CAS, or revocation detected afterwards, the durable row remains claimed. */ +export async function claimStoredVrfProductionTicket(input: StoredVrfProductionTicketInput): Promise & { executionAvailable: false; privateSealed?: string }> { + input = pinned(input) + const inspected = await inspectStoredVrfProductionTicket(input) + await fence(input) + const c = input.context + const authority = vrfPublicAuthority(c) + let connection: VrfTicketClaimConnection + try { connection = await (input.claimConnections ?? ownedProductionConnections).connect() } + catch { + try { await fence(input) } catch { /* no claim/result */ } + throw new VrfProductionClaimOutcomeUnknown(input.ticketId) + } + let beginDispatched = false, transactionStarted = false, updateDispatched = false, updateCompleted = false, commitDispatched = false, committed = false, destroy = false + try { + await fence(input) + beginDispatched = true + await connection.query("BEGIN") + transactionStarted = true + updateDispatched = true + const result = await connection.query(`UPDATE vrf_production_tickets t SET state='claimed',claimed_at=clock_timestamp() + FROM mcp_plans p,mcp_connections c,servers s + WHERE t.ticket_id=$1 AND t.digest=$2 AND t.state='issued' AND t.expires_at>clock_timestamp() + AND t.plan_id=$3 AND t.connection_id=$4 AND t.server_id=$5 AND t.owner_id=$6 AND t.action_id=$7 AND t.action_version=$8 AND t.source_proof=$9 + AND p.id=t.plan_id AND p.connection_id=t.connection_id AND p.operation_id=$12 AND p.operation_version=$13 + AND (($10::uuid IS NULL AND p.state IN ('draft','awaiting-approval','approved')) OR + ($10::uuid IS NOT NULL AND p.state='executing' AND p.approved_by IS NOT NULL AND EXISTS ( + SELECT 1 FROM mcp_operations o JOIN mcp_operation_steps st ON st.operation_id=o.id + WHERE o.id=$10 AND o.plan_id=p.id AND o.connection_id=p.connection_id AND o.state='running' + AND st.step_index=$11 AND st.state='intent' AND st.server_id=s.id))) + AND p.expires_at>clock_timestamp() + AND c.id=t.connection_id AND c.owner_id=t.owner_id AND s.id=t.server_id AND s.enabled=true RETURNING t.ticket_id`, [input.ticketId, inspected.digest, c.binding.planId, c.connectionId, c.binding.serverId, c.binding.actorId, c.actionId, c.actionVersion, c.binding.sourceProof, input.execution?.operationId ?? null, input.execution?.stepIndex ?? null, authority.id, authority.version]) + updateCompleted = true + must(result.rows.length === 1, "unknown/replayed/revoked claim") + commitDispatched = true + const commit = await connection.query("COMMIT") + if (input.execution) must(commit.command === "COMMIT", "execution claim COMMIT not acknowledged") + committed = true + // Post-claim failure intentionally happens after COMMIT; revocation must + // never roll a successfully claimed ticket back into the issued state. + await fence(input) + return { ...inspected, executionAvailable: false } + } catch (error) { + if ((beginDispatched && !transactionStarted || commitDispatched || updateDispatched && !updateCompleted) && !committed) { + destroy = true + // Never send ROLLBACK or replay UPDATE after an uncertain COMMIT. The + // future executor must quarantine its durable operation intent here. + // If the DB never committed, this service cannot assert a spent bit. + try { await fence(input) } catch { /* no result or execution */ } + throw new VrfProductionClaimOutcomeUnknown(input.ticketId) + } + if (transactionStarted && !committed) { + try { await connection.query("ROLLBACK") } catch { destroy = true } + } + throw error + } finally { + connection.release(destroy) + } +} diff --git a/backend/src/services/vrf-production-transition.test.ts b/backend/src/services/vrf-production-transition.test.ts new file mode 100644 index 00000000..110b5495 --- /dev/null +++ b/backend/src/services/vrf-production-transition.test.ts @@ -0,0 +1,226 @@ +import assert from "node:assert/strict" +import { randomBytes, randomUUID } from "node:crypto" +import { readFile } from "node:fs/promises" +import { sealVrfProductionAdmission, VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionRequest, type VrfProductionSnapshot, type VrfProductionRow } from "./vrf-production-admission.js" +import { classifyVrfProductionTransition, sealVrfProductionTransitionCheckpoint, openVrfProductionTransitionCheckpoint, assertVrfProductionTransitionCheckpoint, assertVrfProductionHistoryProvenance, type VrfProductionTransitionCheckpoint, type VrfProductionTransitionInput } from "./vrf-production-transition.js" + +type Row = VrfProductionRow +const report = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v5.json", import.meta.url), "utf8")) +const secret = randomBytes(32), sourceProof = "a".repeat(64), binding = { serverId: 7, sourceProof, actorId: "human", planId: randomUUID() }, now = Date.now() +const catalog = (name: string): VrfProductionSnapshot => { + const phase = report.phases.find((item: { phase: string }) => item.phase === name) + assert.ok(phase) + const catalogs: Record = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, structuredClone(phase.owned[resource].raw)])) + catalogs.vrf.push({ ".id": "*0", name: "main", interfaces: "all", disabled: "false", builtin: "true" }) + catalogs.routing_table.unshift({ ".id": "*0", name: "main", fib: "true", dynamic: "true" }) + catalogs.ip_settings = [{}]; catalogs.ipv6_settings = [{}] + return { projection: "raw-native-v1", serverId: 7, sourceProof, catalogs } +} +let cases = 0 +function inputFor(before: VrfProductionSnapshot, after: VrfProductionSnapshot, request: VrfProductionRequest, previous?: VrfProductionTransitionCheckpoint, acknowledgedId?: string): VrfProductionTransitionInput { + const issued = sealVrfProductionAdmission({ snapshot: before, binding, request, secret, now, ttlMs: 300000 }) + return { before, after, request, binding, secret, now, sealed: issued.sealed, previous, acknowledgedId } +} +const phases = ["vrf-create", "vrf-rename", "vrf-disable", "vrf-reenable", "vrf-membership-transfer-a-to-b", "vrf-delete-addresses-remain"] +function replay(transform?: (snapshot: VrfProductionSnapshot) => void, mapPrimaryId = (id: string) => id) { + let before = catalog("addresses-before-vrf"), previous: VrfProductionTransitionCheckpoint | undefined + transform?.(before) + const inputs: VrfProductionTransitionInput[] = [] + for (const phase of phases) { + const command = report.journal.find((item: { phase: string }) => item.phase === phase), after = catalog(phase) + transform?.(after) + const request: VrfProductionRequest = { action: command.method === "PUT" ? "create" : command.method === "DELETE" ? "delete" : "patch", ...(command.id ? { id: mapPrimaryId(command.id) } : {}), fields: command.fields, ...(command.method === "PUT" ? { placeBeforeId: "*0" } : {}) } + const input = inputFor(before, after, request, previous, command.assignedId ? mapPrimaryId(command.assignedId) : undefined) + let result + try { result = classifyVrfProductionTransition(input) } catch (error) { throw new Error(`phase ${phase}`, { cause: error }) } + assert.equal(result.executionAvailable, false); assert.equal(result.forwardingVerified, false) + assert.deepEqual(Object.keys(result.checkpoint), ["kind"]) + inputs.push(input); previous = result.checkpoint; before = after; cases++ + } + return inputs +} +const inputs = replay() +// Same native transformations with ordinary production subnets. Two selected +// bridges use disjoint arbitrary prefixes; connected route network differs +// from host address, proving no fixture /32-/128 shortcut is used. +replay(snapshot => { + for (const resource of ["ipv4_address", "ipv6_address"]) for (const [index, row] of snapshot.catalogs[resource].filter(row => row.dynamic === "false").entries()) { + const old = String(row.address), replacement = resource === "ipv4_address" ? `10.${60 + index}.7.93/23` : `2001:db8:${50 + index}::cafe/56` + row.address = replacement + for (const route of snapshot.catalogs[resource === "ipv4_address" ? "ipv4_route" : "ipv6_route"]) { + if (route["dst-address"] !== old) continue + route["dst-address"] = resource === "ipv4_address" ? `10.${60 + index}.6.0/23` : `2001:db8:${50 + index}::/56` + if (typeof route["local-address"] === "string") route["local-address"] = route["local-address"].replace(old.split("/")[0], replacement.split("/")[0]) + } + } +}) +const refused = (input: VrfProductionTransitionInput) => { assert.throws(() => classifyVrfProductionTransition(input), /VRF production/); cases++ } +// Preserve the captured native lifecycle while rebinding only the primary +// VRF, its table and the generated Linux ifname. The inventory row keeps +// its separate native ID and matching decimal id field. +function replayWithGeneratedIfname(nativeVrfId: string, ifname: string) { + const originalId = inputs[0].acknowledgedId + assert.ok(originalId) + return replay(snapshot => { + for (const resource of ["vrf", "routing_table"]) { + const row = snapshot.catalogs[resource].find(item => item[".id"] === originalId) + if (row) row[".id"] = nativeVrfId + } + const virtual = snapshot.catalogs.interface_inventory.find(row => row.type === "vrf") + if (virtual?.ifname !== undefined) virtual.ifname = ifname + }, id => id === originalId ? nativeVrfId : id) +} +const hexVrf = replayWithGeneratedIfname("*C", "vrfc") +replayWithGeneratedIfname("*A", "vrfa") +replayWithGeneratedIfname("*C", "vrf12") +const withoutHistory = { ...hexVrf[3], previous: undefined } +assert.equal(classifyVrfProductionTransition(withoutHistory).status, "explained-native-transition"); cases++ +const withIfname = (input: VrfProductionTransitionInput, ifname: string) => { + const after = structuredClone(input.after) + after.catalogs.interface_inventory.find(row => row.type === "vrf")!.ifname = ifname + return { ...input, after } +} +for (const ifname of ["vrfa", "vrfC", "vrf0c", "vrf13", "otherc", "vrff"]) { + refused(withIfname(hexVrf[0], ifname)) + refused(withIfname(withoutHistory, ifname)) +} +// Even the valid decimal spelling cannot replace a different authenticated +// name preserved by the preceding disable operation's private checkpoint. +refused(withIfname(hexVrf[3], "vrf12")) +assert.equal(classifyVrfProductionTransition(withIfname(withoutHistory, "vrf12")).status, "explained-native-transition"); cases++ +// Every foreign catalog, opaque key and noncompanion is conserved. +for (const resource of VRF_PRODUCTION_REQUIRED_CATALOGS) { + const input = { ...inputs[1], after: structuredClone(inputs[1].after) } + if (["ip_settings", "ipv6_settings"].includes(resource)) input.after.catalogs[resource][0].opaque = "drift" + else input.after.catalogs[resource].push({ ".id": "*feed", opaque: "foreign addition" }) + refused(input) +} +for (const mutate of [ + (input: VrfProductionTransitionInput) => { input.after.sourceProof = "b".repeat(64) }, + (input: VrfProductionTransitionInput) => { delete input.after.catalogs.ipv6_filter }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.vrf.reverse() }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.bridge[0].opaque = "parent drift" }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.ipv4_route[0].opaque = "new-counter-looking-field" }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.ipv4_address[0][".id"] = "*feed" }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.interface_inventory.find(row => row.type === "vrf")!.caps = "foreign" }, + (input: VrfProductionTransitionInput) => { const row = input.after.catalogs.bridge[0]; input.after.catalogs.bridge.push({ ...row, ".id": "*0" + String(row[".id"]).slice(1) }) }, +]) { const input = { ...inputs[1], after: structuredClone(inputs[1].after) }; mutate(input); refused(input) } +refused({ ...inputs[0], acknowledgedId: "*0" }) +refused({ ...inputs[0], acknowledgedId: undefined }) +assert.equal(classifyVrfProductionTransition({ ...inputs[3], previous: undefined }).status, "explained-native-transition"); cases++ +const unknownEnabledTable = { ...inputs[3], previous: undefined, after: structuredClone(inputs[3].after) } +unknownEnabledTable.after.catalogs.routing_table.find(row => row[".id"] === inputs[3].request.id)!.opaque = "unreviewed generated field" +refused(unknownEnabledTable) +refused({ ...inputs[3], previous: { kind: "vrf-production-transition-checkpoint-v1" } }) +refused({ ...inputs[5], previous: undefined }) +const telemetry = { ...inputs[1], after: structuredClone(inputs[1].after) } +telemetry.after.catalogs.bridge[0]["rx-byte"] = "9999" +assert.equal(classifyVrfProductionTransition(telemetry).executionAvailable, false); cases++ +for (const [resource, field] of [["ip_settings", "rx-byte"], ["ipv6_settings", "running"], ["routing_table", "packets"], ["vrf", "active"], ["bridge", "packets"], ["bridge_port", "invalid"]]) { + const input = { ...inputs[1], after: structuredClone(inputs[1].after) } + if (input.after.catalogs[resource].length) input.after.catalogs[resource][0][field] = "999" + else input.after.catalogs[resource].push({ ".id": "*feed", [field]: "999" }) + refused(input) +} +const nestedBefore = structuredClone(inputs[1].before), nestedAfter = structuredClone(inputs[1].after) +nestedBefore.catalogs.ip_settings[0].opaque = { "rx-byte": "1", nested: { running: "false" } } +nestedAfter.catalogs.ip_settings[0].opaque = { "rx-byte": "2", nested: { running: "false" } } +refused(inputFor(nestedBefore, nestedAfter, inputs[1].request)) +const nestedCounter = { ...inputs[1], after: structuredClone(inputs[1].after) } +nestedCounter.after.catalogs.bridge[0]["rx-byte"] = { configuration: "not a native scalar counter" } +refused(nestedCounter) +for (const mutate of [ + (input: VrfProductionTransitionInput) => { delete input.after.catalogs.ipv4_route[0]["local-address"] }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.ipv4_route[0].dynamic = "false" }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.ipv4_route[0].connected = "false" }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.ipv4_route[0].gateway = "foreign-uplink" }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.ipv4_route[0]["dst-address"] = "10.0.0.0/8" }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.ipv4_route[0][".id"] = "*feed"; input.after.catalogs.ipv4_route.push({ ".id": "*feed", dynamic: "false" }) }, + (input: VrfProductionTransitionInput) => { input.after.catalogs.interface_inventory.find(row => row.type === "vrf")!.ifname = "vrf999" }, +]) { const input = { ...inputs[0], after: structuredClone(inputs[0].after) }; mutate(input); refused(input) } +const physicalBefore = structuredClone(inputs[0].before), physicalAfter = structuredClone(inputs[0].after) +const selected = inputs[0].request.fields.interfaces +for (const snapshot of [physicalBefore, physicalAfter]) { + snapshot.catalogs.interface_inventory.find(row => row.name === selected)!.type = "ether" + snapshot.catalogs.bridge = snapshot.catalogs.bridge.filter(row => row.name !== selected) + snapshot.catalogs.ethernet.push({ ".id": "*ff10", name: selected, mtu: "1500", "auto-negotiation": "yes" }) +} +assert.equal(classifyVrfProductionTransition(inputFor(physicalBefore, physicalAfter, inputs[0].request, undefined, inputs[0].acknowledgedId)).status, "explained-native-transition"); cases++ +const combinedAfter = structuredClone(inputs[2].after) +combinedAfter.catalogs.vrf.find(row => row[".id"] === inputs[2].request.id)!.comment = "combined change" +classifyVrfProductionTransition(inputFor(inputs[2].before, combinedAfter, { ...inputs[2].request, fields: { disabled: "true", comment: "combined change" } }, inputs[2].previous)); cases++ +const combinedCollateral = structuredClone(combinedAfter); combinedCollateral.catalogs.bridge[0].opaque = "unrequested combined side effect" +refused(inputFor(inputs[2].before, combinedCollateral, { ...inputs[2].request, fields: { disabled: "true", comment: "combined change" } }, inputs[2].previous)) +const foreignRouteBefore = structuredClone(inputs[0].before), foreignRouteAfter = structuredClone(inputs[0].after) +foreignRouteBefore.catalogs.ipv4_route.push({ ...foreignRouteBefore.catalogs.ipv4_route[0], ".id": "*feed", dynamic: "false", connect: "false", comment: "static foreign route" }) +foreignRouteAfter.catalogs.ipv4_route[0][".id"] = "*feed" +refused(inputFor(foreignRouteBefore, foreignRouteAfter, inputs[0].request, undefined, inputs[0].acknowledgedId)) +refused({ ...inputs[0], now: now + 300000 }) +console.log(`VRF production transition: ${cases} cases passed; pure classifier, no executor grant`) + +// A disabled CREATE has no active table and must preserve all prior member +// configuration. This model is not a claim of actual RouterOS default support. +const disabledBefore = structuredClone(inputs[0].before), disabledAfter = structuredClone(disabledBefore) +const created = inputs[0].after.catalogs.vrf.find(row => row[".id"] === inputs[0].acknowledgedId)! +disabledAfter.catalogs.vrf.unshift({ ...created, disabled: "true" }) +const inactive = structuredClone(inputs[0].after.catalogs.interface_inventory.find(row => row.type === "vrf")!) +Object.assign(inactive, { disabled: "true", running: "false", status: "------------------------", "mac-address": "00:00:00:00:00:00", "link-downs": "0" }) +for (const key of ["ifname", "ifindex", "flags", "actual-mtu", "last-link-up-time"]) delete inactive[key] +disabledAfter.catalogs.interface_inventory.push(inactive) +const disabledRequest: VrfProductionRequest = { ...inputs[0].request, fields: { ...inputs[0].request.fields, disabled: "true" } } +const disabledResult = classifyVrfProductionTransition(inputFor(disabledBefore, disabledAfter, disabledRequest, undefined, inputs[0].acknowledgedId)); cases++ +const scope = { serverId: binding.serverId, sourceProof: binding.sourceProof, actorId: binding.actorId, nativeId: inputs[0].acknowledgedId! } +const persisted = sealVrfProductionTransitionCheckpoint(disabledResult.checkpoint, scope, secret) +const reopened = openVrfProductionTransitionCheckpoint(persisted, scope, secret) +assertVrfProductionTransitionCheckpoint(reopened, disabledAfter); cases++ +for (const changed of [{ ...scope, actorId: "foreign" }, { ...scope, sourceProof: "b".repeat(64) }]) { assert.throws(() => openVrfProductionTransitionCheckpoint(persisted, changed, secret)); cases++ } +assert.throws(() => openVrfProductionTransitionCheckpoint(persisted.slice(0, -1) + (persisted.endsWith("0") ? "1" : "0"), scope, secret)); cases++ +const changedBefore = structuredClone(disabledAfter); changedBefore.catalogs.bridge[0].opaque = "late drift" +assert.throws(() => assertVrfProductionTransitionCheckpoint(reopened, changedBefore)); cases++ +// Wide unrelated configuration must not hit a smaller durable checkpoint cap +// after an otherwise accepted write. Its opaque content remains conserved. +const wideBefore = structuredClone(disabledBefore), wideAfter = structuredClone(disabledAfter) +for (const snapshot of [wideBefore, wideAfter]) snapshot.catalogs.bridge[0].opaque = "x".repeat(3 * 1024 * 1024) +const wideResult = classifyVrfProductionTransition(inputFor(wideBefore, wideAfter, disabledRequest, undefined, scope.nativeId)) +const wideSealed = sealVrfProductionTransitionCheckpoint(wideResult.checkpoint, scope, secret) +assert.ok(wideSealed.length < 20000) +const wideOpened = openVrfProductionTransitionCheckpoint(wideSealed, scope, secret) +assertVrfProductionTransitionCheckpoint(wideOpened, wideAfter); cases++ +const wideChanged = structuredClone(wideAfter); wideChanged.catalogs.bridge[0].opaque = "y" + String(wideChanged.catalogs.bridge[0].opaque).slice(1) +assert.throws(() => assertVrfProductionTransitionCheckpoint(wideOpened, wideChanged)); cases++ +const keyOrder = structuredClone(wideAfter); keyOrder.catalogs.bridge[0] = Object.fromEntries(Object.entries(keyOrder.catalogs.bridge[0]).reverse()) +assertVrfProductionTransitionCheckpoint(wideOpened, keyOrder); cases++ +classifyVrfProductionTransition(inputFor(disabledAfter, inputs[0].after, { action: "patch", id: scope.nativeId, fields: { disabled: "false" } }, reopened)); cases++ +classifyVrfProductionTransition(inputFor(disabledAfter, disabledBefore, { action: "delete", id: scope.nativeId, fields: {} }, reopened)); cases++ + +// The existing physical/VLAN admission closure must remain exact. VLAN's +// physical parent stays in main; only the selected inventory context moves. +const vlanBefore = structuredClone(inputs[0].before), vlanAfter = structuredClone(inputs[0].after) +for (const snapshot of [vlanBefore, vlanAfter]) { + const row = snapshot.catalogs.interface_inventory.find(row => row.name === selected)! + row.type = "vlan" + snapshot.catalogs.bridge = snapshot.catalogs.bridge.filter(row => row.name !== selected) + snapshot.catalogs.vlan = [{ ".id": "*ff03", name: selected, interface: "owned-parent", "vlan-id": "101" }] + snapshot.catalogs.interface_inventory.push({ ".id": "*ff01", name: "owned-parent", type: "ether", vrf: "main" }) + snapshot.catalogs.ethernet.push({ ".id": "*ff02", name: "owned-parent", mtu: "1500", "auto-negotiation": "yes" }) +} +classifyVrfProductionTransition(inputFor(vlanBefore, vlanAfter, inputs[0].request, undefined, inputs[0].acknowledgedId)); cases++ +const vlanDrift = structuredClone(vlanAfter); vlanDrift.catalogs.ethernet.at(-1)!.mtu = "1400" +refused(inputFor(vlanBefore, vlanDrift, inputs[0].request, undefined, inputs[0].acknowledgedId)) +console.log("staged VRF disabled CREATE/enable/delete, durable authenticated checkpoint and physical/VLAN exact closure cases:", cases) +// Fresh human approval can accept legitimate foreign changes; past history +// only authenticates generated companions that the transition actually uses. +const foreignBefore = structuredClone(inputs[3].before), foreignAfter = structuredClone(inputs[3].after) +for (const snapshot of [foreignBefore, foreignAfter]) { + snapshot.catalogs.ip_filter.push({ ".id": "*ffed", chain: "forward", action: "drop", comment: "legitimate foreign rule" }) + const target = snapshot.catalogs.vrf.find(row => row[".id"] === inputs[3].request.id)!, oldName = String(target.name) + for (const rows of Object.values(snapshot.catalogs)) for (const row of rows) for (const [key, value] of Object.entries(row)) if (typeof value === "string") row[key] = value.replaceAll(oldName, "human-renamed-vrf") + target.comment = "human approved harmless metadata" +} +classifyVrfProductionTransition(inputFor(foreignBefore, foreignAfter, inputs[3].request, inputs[3].previous)); cases++ +assert.throws(() => assertVrfProductionTransitionCheckpoint(inputs[3].previous!, foreignBefore)); cases++ +const lateForeign = structuredClone(foreignAfter); lateForeign.catalogs.ip_filter.push({ ".id": "*ffee", action: "accept" }) +refused(inputFor(foreignBefore, lateForeign, inputs[3].request, inputs[3].previous)) +const cycleDrift = structuredClone(foreignBefore); cycleDrift.catalogs.interface_inventory.find(row => row.type === "vrf")!["link-downs"] = "99" +assert.throws(() => assertVrfProductionHistoryProvenance(inputs[3].previous!, cycleDrift)); cases++ +console.log("VRF scoped causal history, strict full reconcile/current transition:", cases) diff --git a/backend/src/services/vrf-production-transition.ts b/backend/src/services/vrf-production-transition.ts new file mode 100644 index 00000000..73a2eb1b --- /dev/null +++ b/backend/src/services/vrf-production-transition.ts @@ -0,0 +1,489 @@ +import { noteVrfTransitionRefusal, withVrfTransitionSection, type VrfTransitionGuard } from "./vrf-transition-refusal.js" +import { isIP } from "node:net" +import { isDeepStrictEqual } from "node:util" +import { createHash, createHmac, timingSafeEqual } from "node:crypto" +import { inspectVrfProductionAdmission, VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionAdmission, type VrfProductionBinding, type VrfProductionRequest, type VrfProductionRow, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { isVrfProductionTelemetryValue, vrfProductionConfiguration, vrfProductionConfigurationV2 } from "./vrf-production-projection.js" + +type Row = VrfProductionRow +export interface VrfProductionTransitionCheckpoint { readonly kind: "vrf-production-transition-checkpoint-v1" } +interface SnapshotProof { kind: "configuration-sha256-v1" | "configuration-sha256-v2"; serverId: number; sourceProof: string; catalogDigests: Record } +interface CausalProvenance { inactiveCycle?: Row; missingMembers: Row[]; greMembers?: Row[] } +interface History { snapshot: VrfProductionSnapshot | SnapshotProof; vrfId: string; activeVirtual?: Row; dormantTable?: Row; absentLl: Array<{ address: Row; route: Row; member: string }>; provenance: CausalProvenance } +const histories = new WeakMap() +export interface VrfProductionCheckpointScope { serverId: number; sourceProof: string; actorId: string; nativeId: string } +const checkpointPurpose = "mmapp-vrf-production-transition-history-v1\0" +// Full raw reads already have a 32MiB bound. The durable history contains only +// provenance subsets plus a constant-size whole-configuration digest; allow the +// JSON envelope/escaping without imposing a smaller post-ACK catalog limit. +const maxCheckpointBytes = 64 * 1024 * 1024 + +/** Private durable provenance, stored only inside the existing encrypted + * operation checkpoint after its exact generic VERIFIED receipt. No write + * authority is minted, and the next transition still matches the full before. */ +export function sealVrfProductionTransitionCheckpoint(checkpoint: VrfProductionTransitionCheckpoint, scope: VrfProductionCheckpointScope, secret: Uint8Array): string { + const history = histories.get(checkpoint) + must(history && history.vrfId === scope.nativeId && history.snapshot.serverId === scope.serverId && history.snapshot.sourceProof === scope.sourceProof && scope.actorId.length > 0 && secret.byteLength >= 32, "checkpoint scope") + const durable = { ...history, snapshot: "projection" in history.snapshot ? snapshotProof(history.snapshot) : history.snapshot } + // Re-sealing legacy history cannot mint the missing GRE witnesses. + const text = JSON.stringify({ version: history.provenance.greMembers === undefined ? 1 : 2, scope, history: durable }) + must(Buffer.byteLength(text) <= maxCheckpointBytes, "private checkpoint budget") + const payload = Buffer.from(text).toString("base64url") + return payload + "." + createHmac("sha256", secret).update(checkpointPurpose).update(payload).digest("hex") +} +export function openVrfProductionTransitionCheckpoint(sealed: string, scope: VrfProductionCheckpointScope, secret: Uint8Array): VrfProductionTransitionCheckpoint { + must(typeof sealed === "string" && sealed.length <= maxCheckpointBytes * 2 && secret.byteLength >= 32, "private checkpoint budget/key") + const parts = sealed.split(".") + must(parts.length === 2 && /^[A-Za-z0-9_-]+$/.test(parts[0]) && /^[a-f0-9]{64}$/.test(parts[1]), "private checkpoint encoding") + const expected = createHmac("sha256", secret).update(checkpointPurpose).update(parts[0]).digest() + must(timingSafeEqual(expected, Buffer.from(parts[1], "hex")), "private checkpoint signature") + const bytes = Buffer.from(parts[0], "base64url"); must(bytes.byteLength <= maxCheckpointBytes, "private checkpoint budget") + const value = JSON.parse(bytes.toString("utf8")) as { version: number; scope: VrfProductionCheckpointScope; history: History } + must([1, 2].includes(value.version) && isDeepStrictEqual(value.scope, scope) && value.history?.vrfId === scope.nativeId, "private checkpoint scope/version") + const proof = value.history.snapshot as SnapshotProof + must(["configuration-sha256-v1", "configuration-sha256-v2"].includes(proof?.kind) && proof.catalogDigests && VRF_PRODUCTION_REQUIRED_CATALOGS.every(key => Object.hasOwn(proof.catalogDigests, key)) && Object.values(proof.catalogDigests).every(digest => /^[a-f0-9]{64}$/.test(digest)) && proof.serverId === scope.serverId && proof.sourceProof === scope.sourceProof && Array.isArray(value.history.absentLl) && Array.isArray(value.history.provenance?.missingMembers), "private checkpoint source/history") + must(value.version === 2 ? Array.isArray(value.history.provenance.greMembers) : !value.history.provenance.missingMembers.some(row => row.type === "gre-tunnel" && !Object.hasOwn(row, "mac-address")), "incomplete legacy GRE provenance") + const checkpoint: VrfProductionTransitionCheckpoint = Object.freeze({ kind: "vrf-production-transition-checkpoint-v1" }) + histories.set(checkpoint, structuredClone(value.history)) + return checkpoint +} +export function assertVrfProductionTransitionCheckpoint(checkpoint: VrfProductionTransitionCheckpoint, before: VrfProductionSnapshot): void { + const history = histories.get(checkpoint) + must(history, "unknown private checkpoint") + snapshotSame(history.snapshot, before) +} +export function vrfProductionCheckpointCatalogs(checkpoint: VrfProductionTransitionCheckpoint): string[] { + const history = histories.get(checkpoint); must(history, "unknown private checkpoint") + return Object.keys("projection" in history.snapshot ? history.snapshot.catalogs : history.snapshot.catalogDigests) +} +const cycleFields = [".id", "id", "type", "vrf", "disabled", "link-downs", "last-link-up-time", "last-link-down-time"] +function witness(row: Row, fields: readonly string[]): Row { return Object.fromEntries(Object.entries(row).filter(([field]) => fields.includes(field))) } +function memberWitness(row: Row, snapshot: VrfProductionSnapshot): Row { + const result = witness(row, [".id", "name", "type", "mac-address"]) + if (typeof result["mac-address"] === "string") result["mac-address"] = result["mac-address"].toLowerCase() + if (row.type === "gre-tunnel" && !Object.hasOwn(row, "mac-address")) result.greParent = greParent(snapshot, row) + return result +} +/** Prior history is causal provenance, not a lease on the router's complete + * configuration. The fresh approved ticket fences ALL current dependencies. */ +export function assertVrfProductionHistoryProvenance(checkpoint: VrfProductionTransitionCheckpoint, before: VrfProductionSnapshot): void { + const history = histories.get(checkpoint); must(history, "unknown private checkpoint") + must(history.snapshot.serverId === before.serverId && history.snapshot.sourceProof === before.sourceProof, "history source changed") + const target = before.catalogs.vrf.find(row => row[".id"] === history.vrfId) + must(history.provenance.greMembers !== undefined || !before.catalogs.interface_inventory.some(row => row.type === "gre-tunnel" && !Object.hasOwn(row, "mac-address") && target && members(target.interfaces).includes(String(row.name))), "incomplete legacy GRE provenance") + for (const expected of history.provenance.greMembers ?? []) { + const member = before.catalogs.interface_inventory.find(row => row[".id"] === expected[".id"]) + must(member && isDeepStrictEqual(memberWitness(member, before), expected), "GRE causal parent/endpoint changed") + } + if (history.provenance.inactiveCycle) { + const cycle = history.provenance.inactiveCycle, virtual = before.catalogs.interface_inventory.find(row => row[".id"] === cycle[".id"]) + must(target && yes(target.disabled) && virtual && virtual.name === target.name, "inactive causal identity/context changed") + inactiveVirtual(virtual) + must(isDeepStrictEqual(witness(virtual, cycleFields), cycle) && !before.catalogs.routing_table.some(row => row[".id"] === history.vrfId), "inactive causal cycle/table absence changed") + } + must(history.provenance.missingMembers.length === new Set(history.absentLl.map(item => item.member)).size, "missing LL causal witnesses") + for (const expected of history.provenance.missingMembers) { + const member = before.catalogs.interface_inventory.find(row => row[".id"] === expected[".id"]) + must(target && no(target.disabled) && member && isDeepStrictEqual(memberWitness(member, before), expected) && member.vrf === target.name && context(before.catalogs.vrf, String(member.name)) === target.name, "missing LL member causal identity/MAC/context changed") + must(!addressSources(before, [String(member.name)]).some(source => source.ll), "historical LL absence changed") + } +} +const yes = (value: unknown) => [true, "true", "yes"].includes(value as string | boolean) +const no = (value: unknown) => [false, "false", "no"].includes(value as string | boolean) +function must(value: unknown, reason: string, guard?: VrfTransitionGuard): asserts value { + if (!value) { + const error = Error(`VRF production transition refused: ${reason}`) + noteVrfTransitionRefusal(error, reason, guard) + throw error + } +} +function nativeId(value: unknown): string { must(typeof value === "string" && /^\*[a-f0-9]+$/i.test(value), "native ID"); return value.slice(1).replace(/^0+/, "").toLowerCase() || "0" } +const projected = vrfProductionConfiguration +function same(resource: string, old: Row, current: Row, omit: readonly string[] = []): void { must(isDeepStrictEqual(projected(resource, old, omit), projected(resource, current, omit)), "opaque configuration drift") } +function valid(snapshot: VrfProductionSnapshot): void { + must(snapshot.projection === "raw-native-v1" && snapshot.catalogs && VRF_PRODUCTION_REQUIRED_CATALOGS.every(key => Object.hasOwn(snapshot.catalogs, key)), "complete raw17 snapshot") + const json = (value: unknown, depth = 0): void => { + must(depth <= 24, "JSON nesting budget") + if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) return + must(value && typeof value === "object" && (Array.isArray(value) || Object.getPrototypeOf(value) === Object.prototype), "non-JSON raw value") + for (const child of Object.values(value)) json(child, depth + 1) + } + let bytes = 0 + for (const [resource, rows] of Object.entries(snapshot.catalogs)) { + must(Array.isArray(rows) && rows.length < 50000, "catalog budget") + const ids = new Set() + if (["ip_settings", "ipv6_settings"].includes(resource)) must(rows.length === 1, "singleton cardinality") + for (const row of rows) { + must(row && typeof row === "object" && !Array.isArray(row), "native row") + json(row) + if (["ip_settings", "ipv6_settings"].includes(resource)) continue + const id = nativeId(row[".id"]); must(!ids.has(id), "ambiguous ID aliases"); ids.add(id) + } + bytes += Buffer.byteLength(JSON.stringify(rows)); must(bytes < 32 * 1024 * 1024, "raw byte budget") + } +} +function snapshotProof(snapshot: VrfProductionSnapshot, kind: SnapshotProof["kind"] = "configuration-sha256-v2"): SnapshotProof { + let hash = createHash("sha256") + const feed = (value: unknown): void => { + if (Array.isArray(value)) { hash.update("["); for (const child of value) { feed(child); hash.update(",") } hash.update("]"); return } + if (value && typeof value === "object") { hash.update("{"); for (const [key, child] of Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) { hash.update(JSON.stringify(key)).update(":"); feed(child); hash.update(",") } hash.update("}"); return } + hash.update(JSON.stringify(value)) + } + const catalogDigests: Record = {} + const projection = kind === "configuration-sha256-v1" ? vrfProductionConfigurationV2 : projected + const domain = kind === "configuration-sha256-v1" ? "mmapp-vrf-production-full-catalog-v1\0" : "mmapp-vrf-production-full-catalog-v2\0" + for (const resource of Object.keys(snapshot.catalogs).sort()) { hash = createHash("sha256").update(domain); feed(resource); feed(snapshot.catalogs[resource].map(row => projection(resource, row))); catalogDigests[resource] = hash.digest("hex") } + return { kind, serverId: snapshot.serverId, sourceProof: snapshot.sourceProof, catalogDigests } +} +function snapshotSame(old: VrfProductionSnapshot | SnapshotProof, current: VrfProductionSnapshot): void { + if (!("projection" in old)) { const next = snapshotProof(current, old.kind); must(old.serverId === next.serverId && old.sourceProof === next.sourceProof && Object.entries(old.catalogDigests).every(([resource, digest]) => next.catalogDigests[resource] === digest), "checkpoint parent/configuration/order drift"); return } + snapshotSame(snapshotProof(old), current) +} +function members(value: unknown): string[] { + must(typeof value === "string", "unknown member selector") + if (value === "none" || value === "") return [] + const names = value.split(",") + must(names.every(name => name.trim() === name && name.length > 0 && !["all", "none", "dynamic"].includes(name)) && new Set(names).size === names.length, "ambiguous members") + return names +} +/** Strict full-prefix parser; arbitrary static prefixes, no fixture address space. */ +function prefix(raw: unknown, family: 4 | 6): { network: string; host: string; length: number } { + must(typeof raw === "string" && !/[\s%@.]/.test(family === 6 ? raw : raw.replaceAll(".", "")), "address context") + const parts = raw.split("/"); must(parts.length === 2 && /^(0|[1-9]\d{0,2})$/.test(parts[1]) && isIP(parts[0]) === family, "CIDR") + const bits = family === 4 ? 32 : 128, length = Number(parts[1]); must(length <= bits, "prefix length") + let host = 0n + if (family === 4) for (const octet of parts[0].split(".")) host = host << 8n | BigInt(octet) + else { + const halves = parts[0].split("::"), left = halves[0] ? halves[0].split(":") : [], right = halves[1] ? halves[1].split(":") : [] + const words = halves.length === 1 ? left : [...left, ...Array(8 - left.length - right.length).fill("0"), ...right] + must(words.length === 8, "IPv6 words"); for (const word of words) host = host << 16n | BigInt(parseInt(word, 16)) + must(host >> 32n !== 0xffffn, "mapped IPv6 alias") + } + const network = host >> BigInt(bits - length) << BigInt(bits - length) + return { network: `${family}:${network.toString(16)}/${length}`, host: parts[0], length } +} +function context(vrfs: Row[], name: string): string { + for (const vrf of vrfs) { + must(typeof vrf.name === "string" && (yes(vrf.disabled) || no(vrf.disabled)), "VRF identity/enabled state") + if (vrf.name === "main") { must(vrf.interfaces === "all" && no(vrf.disabled), "main VRF shape"); return "main" } + if (no(vrf.disabled) && members(vrf.interfaces).includes(name)) return vrf.name + } + const error = Error("VRF production transition refused: missing ordered main VRF") + noteVrfTransitionRefusal(error, "missing ordered main VRF") + throw error +} +function eui64(row: Row, inventory: Row): void { + must(yes(row.dynamic) && yes(row["auto-link-local"]) && yes(row["link-local"]) && row.vrf === "main" && typeof inventory["mac-address"] === "string" && /^(?:[a-f0-9]{2}:){5}[a-f0-9]{2}$/i.test(inventory["mac-address"]), "link-local parent flags/MAC") + const mac = inventory["mac-address"].split(":").map(word => parseInt(word, 16)), bytes = [mac[0] ^ 2, mac[1], mac[2], 255, 254, mac[3], mac[4], mac[5]], words = ["fe80", "0", "0", "0", ...[0, 2, 4, 6].map(index => (bytes[index] * 256 + bytes[index + 1]).toString(16))] + must(prefix(row.address, 6).length === 64 && prefix(row.address, 6).host && prefix(`${words.join(":")}/128`, 6).network === prefix(`${String(row.address).split("/")[0]}/128`, 6).network, "EUI64 link-local address") +} +/** Bounded observed RouterOS IPv4 GRE auto-LL form, verified on four native + * GREs in the V29 capture. This is not a general IPv6 or tunnel derivation. + * The current concrete GRE and its explicit source parent remain mandatory. */ +function greParent(snapshot: VrfProductionSnapshot, inventory: Row): Row { + must(inventory.type === "gre-tunnel" && !Object.hasOwn(inventory, "mac-address"), "GRE native no-MAC parent") + const rows = snapshot.catalogs.gre?.filter(row => row.name === inventory.name) ?? [] + must(rows.length === 1, "GRE exact concrete parent") + const config = rows[0]; nativeId(config[".id"]); nativeId(inventory[".id"]) + const unicast4 = (value: unknown): value is string => typeof value === "string" && isIP(value) === 4 && Number(value.split(".")[0]) > 0 && Number(value.split(".")[0]) < 224 && value !== "255.255.255.255" + must(unicast4(config["local-address"]) && unicast4(config["remote-address"]), "GRE explicit IPv4 endpoints") + const sources = snapshot.catalogs.ipv4_address.filter(row => typeof row.address === "string" && row.address.split("/")[0] === config["local-address"]) + must(sources.length === 1 && no(sources[0].dynamic) && no(sources[0].disabled), "GRE exact static source") + const roots = snapshot.catalogs.interface_inventory.filter(row => row.name === sources[0].interface) + must(roots.length === 1 && roots[0][".id"] !== inventory[".id"], "GRE exact source parent") + const resources: Record = { ether: "ethernet", ethernet: "ethernet", bridge: "bridge", vlan: "vlan", wg: "wireguard", wireguard: "wireguard", "gre-tunnel": "gre", "ipip-tunnel": "ipip", "eoip-tunnel": "eoip", vxlan: "vxlan", bond: "bonding", bonding: "bonding", "pppoe-out": "pppoe_client" } + const resource = resources[String(roots[0].type)], configs = resource ? snapshot.catalogs[resource]?.filter(row => row.name === roots[0].name) ?? [] : [] + must(configs.length === 1, "GRE concrete source parent") + nativeId(roots[0][".id"]); nativeId(configs[0][".id"]) + // Durable causal history needs identity/endpoints, not opaque configuration + // or optional GRE secrets. Fresh admission/current reconciliation still fence + // the complete configuration separately. + return { configuration: witness(config, [".id", "name", "local-address", "remote-address"]), sourceAddress: witness(sources[0], [".id", "address", "interface", "actual-interface", "dynamic", "disabled", "vrf"]), sourceInventory: witness(roots[0], [".id", "name", "type", "mac-address"]), sourceConfiguration: witness(configs[0], [".id", "name"]), sourceResource: resource } +} +function nativeLinkLocal(snapshot: VrfProductionSnapshot, row: Row, inventory: Row): void { + if (inventory.type !== "gre-tunnel" || Object.hasOwn(inventory, "mac-address")) { eui64(row, inventory); return } + const parent = greParent(snapshot, inventory), config = parent.configuration as Row + must(yes(row.dynamic) && yes(row["auto-link-local"]) && yes(row["link-local"]) && no(row.disabled) && row.interface === inventory.name && row["actual-interface"] === inventory.name && no(row.invalid), "GRE auto-link-local native flags/identity") + const effectiveContext = context(snapshot.catalogs.vrf, String(inventory.name)) + must(inventory.vrf === effectiveContext, "GRE inventory effective context") + must(row.vrf === effectiveContext, "GRE link-local effective context") + const octets = String(config["remote-address"]).split(".").map(Number) + const expected = `fe80::2:${(octets[0] * 256 + octets[1]).toString(16)}:${(octets[2] * 256 + octets[3]).toString(16)}/64` + const parsed = prefix(row.address, 6) + must(parsed.length === 64 && prefix(`${parsed.host}/128`, 6).network === prefix(`${expected.split("/")[0]}/128`, 6).network, "GRE observed remote-bound auto-link-local") +} +interface Source { resource: "ipv4_address" | "ipv6_address"; row: Row; member: string; network: string; family: 4 | 6; ll: boolean } +function addressSources(snapshot: VrfProductionSnapshot, names: string[]): Source[] { + const result: Source[] = [] + for (const [resource, family] of [["ipv4_address", 4], ["ipv6_address", 6]] as const) for (const row of snapshot.catalogs[resource]) { + if (!names.includes(String(row.interface))) continue + const member = String(row.interface), ll = yes(row.dynamic) + must(no(row.disabled) || row.disabled === undefined && ll, "disabled/unknown selected address unsupported") + const parsed = prefix(row.address, family) + if (ll) { + must(family === 6, "dynamic non-link-local address") + const inventory = snapshot.catalogs.interface_inventory.filter(item => item.name === member); must(inventory.length === 1, "LL inventory"); nativeLinkLocal(snapshot, row, inventory[0]) + } else must(no(row.dynamic) && row.vrf === context(snapshot.catalogs.vrf, member), "static address provenance/context") + result.push({ resource, row, member, network: parsed.network, family, ll }) + } + must(new Set(result.map(source => `${source.member}/${source.network}`)).size === result.length, "ambiguous equal-prefix address sources") + return result +} +function routeFor(row: Row, source: Source, vrfs: Row[]): boolean { + if (!yes(row.dynamic) || ![row.connect, row.connected].some(yes) && row.protocol !== "connect") return false + if (["connect", "connected"].some(field => row[field] !== undefined && !yes(row[field])) || row.protocol !== undefined && row.protocol !== "connect") return false + let network: string; try { network = prefix(row["dst-address"], source.family).network } catch { return false } + if (network !== source.network) return false + const table = context(vrfs, source.member), gateway = source.member + (table === "main" ? "" : `@${table}`) + if (row["routing-table"] !== table || row.gateway !== gateway || row["immediate-gw"] !== source.member || row.interface !== undefined && row.interface !== "" && row.interface !== source.member) return false + return row["local-address"] === undefined || row["local-address"] === `${String(source.row.address).split("/")[0]}%${gateway}` +} +/** A terminal DELETE may remove one proven automatic LL from an unchanged, + * empty concrete bridge. It creates no revival debt for the deleted target. */ +function deletedBridgeLinkLocal(before: VrfProductionSnapshot, after: VrfProductionSnapshot, old: Row, source: Source, oldRoute: Row, beforeSources: Source[], afterSources: Source[]): void { + const name = source.member + must(source.ll && source.family === 6 && no(old.disabled) && members(old.interfaces).includes(name) && context(before.catalogs.vrf, name) === old.name && context(after.catalogs.vrf, name) === "main" && !after.catalogs.vrf.some(row => row[".id"] === old[".id"]), "DELETE bridge LL owned context") + const parents = [before, after].map(snapshot => snapshot.catalogs.interface_inventory.filter(row => row.name === name)) + must(parents.every(rows => rows.length === 1) && parents.every(rows => rows[0].type === "bridge" && rows[0].disabled === "false" && rows[0].running === "true" && rows[0].dynamic === "false") && parents[0][0][".id"] === parents[1][0][".id"] && parents[0][0]["mac-address"] === parents[1][0]["mac-address"], "DELETE bridge LL concrete parent") + const parent = parents[0][0], id = parent[".id"] + for (const snapshot of [before, after]) { + const bridges = snapshot.catalogs.bridge.filter(row => row.name === name) + const reference = (value: unknown) => typeof value === "string" && value.split(",").some(item => [name, id].includes(item.trim())) + must(bridges.length === 1 && bridges[0][".id"] === id && bridges[0].disabled === "false" && bridges[0].dynamic === "false" && bridges[0].running === "true" && bridges[0]["mac-address"] === parent["mac-address"] && !snapshot.catalogs.bridge_port.some(row => reference(row.bridge) || reference(row.interface)) && !snapshot.catalogs.bridge_vlan.some(row => reference(row.bridge) || reference(row.tagged) || reference(row.untagged) || reference(row["current-tagged"]) || reference(row["current-untagged"])) && !(snapshot.catalogs.vlan ?? []).some(row => reference(row.interface)), "DELETE bridge LL empty configuration") + } + same("bridge", before.catalogs.bridge.find(row => row.name === name)!, after.catalogs.bridge.find(row => row.name === name)!) + must(source.row.dynamic === "true" && source.row["auto-link-local"] === "true" && source.row["link-local"] === "true" && source.row.disabled === "false" && source.row.invalid === "false" && source.row.interface === name && source.row["actual-interface"] === name, "DELETE bridge LL automatic provenance") + eui64(source.row, parent) + const statics = beforeSources.filter(item => item.member === name && !item.ll) + must(statics.some(item => item.family === 4) && statics.every(item => afterSources.some(next => next.member === name && next.resource === item.resource && !next.ll && next.row[".id"] === item.row[".id"] && next.network === item.network)) && !afterSources.some(item => item.member === name && item.ll), "DELETE bridge LL static address closure") + const routeReference = (row: Row) => [row.gateway, row["immediate-gw"], row.interface].some(value => typeof value === "string" && (value === name || value === id || value.startsWith(name + "@") || value.startsWith(String(id) + "@"))) + const linkLocalRoute = (row: Row) => { try { return prefix(row["dst-address"], 6).network === source.network && routeReference(row) } catch { return false } } + must(before.catalogs.ipv6_route.filter(linkLocalRoute).length === 1 && before.catalogs.ipv6_route.filter(linkLocalRoute)[0] === oldRoute && !after.catalogs.ipv6_route.some(linkLocalRoute), "DELETE bridge LL connected route closure") +} +/** Existing object guards must see the exact concrete parent/dependent + * identities already proved by admission, not a guessed numeric namespace. */ +export function vrfProductionDependencyObjects(snapshot: VrfProductionSnapshot, admission: VrfProductionAdmission): Array<{ kind: string; id: string; serverId: number }> { + valid(snapshot) + must(snapshot.serverId === admission.binding.serverId && snapshot.sourceProof === admission.binding.sourceProof, "dependency object source") + const objects = new Map() + const add = (kind: string, id: unknown) => { nativeId(id); const object = { kind, id: String(id), serverId: snapshot.serverId }; objects.set(`${kind}/${String(id)}`, object) } + for (const member of admission.members) { + for (const parent of member.parents) { add(parent.resource, parent.configurationId); add("interface_inventory", parent.id) } + for (const address of member.addresses) add(address.resource, address.id) + } + for (const source of addressSources(snapshot, admission.members.map(member => member.name))) { + const resource = source.family === 4 ? "ipv4_route" : "ipv6_route" + const routes = snapshot.catalogs[resource].filter(row => routeFor(row, source, snapshot.catalogs.vrf)) + must(routes.length === 1, "dependency connected route identity") + add(resource, routes[0][".id"]) + } + const target = snapshot.catalogs.vrf.find(row => row[".id"] === admission.request.id) + if (target) { + for (const row of snapshot.catalogs.routing_table.filter(row => row[".id"] === target[".id"] && row.name === target.name)) add("routing_table", row[".id"]) + for (const row of snapshot.catalogs.interface_inventory.filter(row => row.type === "vrf" && row.name === target.name && row.vrf === "main")) add("interface_inventory", row[".id"]) + } + return [...objects.values()] +} +const timestamp = (value: unknown): value is string => typeof value === "string" && /^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/.test(value) && !Number.isNaN(Date.parse(value.replace(" ", "T") + "Z")) && new Date(value.replace(" ", "T") + "Z").toISOString().slice(0, 19).replace("T", " ") === value +const greMemberChronology = ["link-downs", "last-link-up-time", "last-link-down-time"] as const +/** One observed no-MAC GRE context-transfer cycle only. No chronology becomes + * telemetry: unchanged context and every other concrete type remain strict. */ +function memberInventoryOmissions(before: VrfProductionSnapshot, after: VrfProductionSnapshot, name: string, old: Row, current: Row): string[] { + const omit = ["vrf", "status"] + if (isDeepStrictEqual(witness(old, greMemberChronology), witness(current, greMemberChronology))) return omit + const previousContext = context(before.catalogs.vrf, name), nextContext = context(after.catalogs.vrf, name) + must(old.type === "gre-tunnel" && current.type === "gre-tunnel" && old.name === name && current.name === name && old[".id"] === current[".id"] && !Object.hasOwn(old, "mac-address") && !Object.hasOwn(current, "mac-address") && previousContext !== nextContext && old.vrf === previousContext && current.vrf === nextContext, "bounded GRE cycle member identity/context") + must(no(old.disabled) && no(current.disabled) && old.running === "true" && current.running === "true", "bounded GRE cycle active state") + const a = greParent(before, old), b = greParent(after, current) + const parent = (snapshot: VrfProductionSnapshot, resource: string, id: unknown) => snapshot.catalogs[resource]?.find(row => row[".id"] === id) + const oldGre = before.catalogs.gre.find(row => row.name === name), newGre = after.catalogs.gre.find(row => row.name === name) + const resource = String(a.sourceResource) + const oldRoot = parent(before, resource, (a.sourceConfiguration as Row)[".id"]), newRoot = parent(after, resource, (b.sourceConfiguration as Row)[".id"]) + const oldAddress = parent(before, "ipv4_address", (a.sourceAddress as Row)[".id"]), newAddress = parent(after, "ipv4_address", (b.sourceAddress as Row)[".id"]) + must(isDeepStrictEqual(a, b) && oldGre && newGre && oldRoot && newRoot && oldAddress && newAddress && isDeepStrictEqual(projected("gre", oldGre), projected("gre", newGre)) && isDeepStrictEqual(projected(resource, oldRoot), projected(resource, newRoot)) && isDeepStrictEqual(projected("ipv4_address", oldAddress), projected("ipv4_address", newAddress)), "bounded GRE cycle parent/configuration") + const oldCount = old["link-downs"], newCount = current["link-downs"] + // Twenty canonical digits are a local parsing budget, not a native bit-width claim. + must(typeof oldCount === "string" && /^(0|[1-9]\d{0,19})$/.test(oldCount) && typeof newCount === "string" && /^(0|[1-9]\d{0,19})$/.test(newCount) && BigInt(newCount) === BigInt(oldCount) + 1n, "bounded GRE cycle count") + const oldUp = old["last-link-up-time"], oldDown = old["last-link-down-time"], newDown = current["last-link-down-time"], newUp = current["last-link-up-time"] + must(timestamp(oldUp) && (oldDown === undefined || timestamp(oldDown)) && timestamp(newDown) && timestamp(newUp) && (oldDown === undefined || oldDown <= oldUp) && newDown >= oldUp && (oldDown === undefined || newDown >= oldDown) && newDown <= newUp, "bounded GRE cycle chronology") + return [...omit, ...greMemberChronology] +} +function matchesGeneratedVrfIfname(ifname: unknown, vrfId: string): boolean { + const id = nativeId(vrfId) + // RouterOS can render the primary VRF ID in lowercase hexadecimal. + return ifname === `vrf${id}` || ifname === `vrf${BigInt("0x" + id)}` +} +function activeVirtual(row: Row, vrfId: string): void { + const ifname = row.ifname + const knownIfnameShape = typeof ifname === "string" && (/^vrf\d+$/.test(ifname) || matchesGeneratedVrfIfname(ifname, vrfId)) + must(row.type === "vrf" && row.vrf === "main" && no(row.disabled) && row.running === "true" && row.status === "--LERU--O---------------" && row.flags === " UP RUNNING NOARP MASTER LOWER_UP (0x104c1)" && row["actual-mtu"] === "65536" && row.mtu === "65536" && knownIfnameShape && typeof row.ifindex === "string" && /^[1-9]\d*$/.test(row.ifindex) && typeof row["mac-address"] === "string" && /^(?:[a-f0-9]{2}:){5}[a-f0-9]{2}$/i.test(row["mac-address"]) && (parseInt(row["mac-address"].slice(0, 2), 16) & 3) === 2 && timestamp(row["last-link-up-time"]), "bounded native active virtual shape") +} +function inactiveVirtual(row: Row): void { + must(row.type === "vrf" && row.vrf === "main" && yes(row.disabled) && row.running === "false" && row.status === "------------------------" && row.mtu === "65536" && row["mac-address"] === "00:00:00:00:00:00" && ["ifname", "ifindex", "flags", "actual-mtu"].every(field => !Object.hasOwn(row, field)), "bounded native inactive virtual shape") + must(row["last-link-down-time"] === undefined || timestamp(row["last-link-down-time"]), "inactive virtual chronology") +} + +export interface VrfProductionTransitionInput { + before: VrfProductionSnapshot; after: VrfProductionSnapshot; binding: VrfProductionBinding; request: VrfProductionRequest; sealed: string; secret: Uint8Array; now: number + /** Trusted primary ACK identity for CREATE only; classification grants no write. */ + acknowledgedId?: string + previous?: VrfProductionTransitionCheckpoint +} +/** Pure post-ACK classification only. No executor, ownership adoption, network, + * durable claim, recovery permission, or forwarding attestation is provided. */ +export function classifyVrfProductionTransition(input: VrfProductionTransitionInput): { status: "explained-native-transition"; executionAvailable: false; forwardingVerified: false; checkpoint: VrfProductionTransitionCheckpoint; explainedRows: number } { + valid(input.before); valid(input.after) + const before = structuredClone(input.before), after = structuredClone(input.after), request = structuredClone(input.request) + const admission = inspectVrfProductionAdmission({ snapshot: before, binding: input.binding, request, sealed: input.sealed, secret: input.secret, now: input.now }).admission + valid(before); valid(after) + must(before.serverId === after.serverId && before.sourceProof === after.sourceProof && isDeepStrictEqual(Object.keys(before.catalogs).sort(), Object.keys(after.catalogs).sort()), "after source/catalog set changed") + const history = input.previous ? histories.get(input.previous) : undefined + if (input.previous) { must(history, "unrecognized serialized checkpoint"); assertVrfProductionHistoryProvenance(input.previous, before) } + const vrfId = request.action === "create" ? input.acknowledgedId : request.id + must(typeof vrfId === "string", "missing primary ACK target ID"); nativeId(vrfId) + if (history) must(history.vrfId === vrfId, "checkpoint target changed") + const old = before.catalogs.vrf.find(row => row[".id"] === vrfId), current = after.catalogs.vrf.find(row => row[".id"] === vrfId) + const oldForeign = before.catalogs.vrf.filter(row => row !== old), newForeign = after.catalogs.vrf.filter(row => row !== current) + must(isDeepStrictEqual(oldForeign.map(row => projected("vrf", row)), newForeign.map(row => projected("vrf", row))), "foreign VRF configuration/order changed") + if (request.action === "create") { + must(!old && current && !before.catalogs.vrf.some(row => nativeId(row[".id"]) === nativeId(vrfId)), "CREATE exact new ACK identity") + must(Object.keys(current).every(field => [".id", "disabled", ...Object.keys(request.fields)].includes(field)) && (yes(request.fields.disabled) ? yes(current.disabled) : no(current.disabled)), "unreviewed CREATE defaults") + must(after.catalogs.vrf.indexOf(current) + 1 === after.catalogs.vrf.findIndex(row => row[".id"] === request.placeBeforeId), "CREATE exact ordered insertion") + } else if (request.action === "delete") must(old && !current, "DELETE exact target") + else { must(old && current && before.catalogs.vrf.indexOf(old) === after.catalogs.vrf.indexOf(current), "PATCH exact target/order"); same("vrf", old, current, Object.keys(request.fields)) } + if (current) for (const [field, value] of Object.entries(request.fields)) must(current[field] === value, "requested primary field mismatch") + const names = admission.members.map(member => member.name) + const oldContext = new Map(names.map(name => [name, context(before.catalogs.vrf, name)])), newContext = new Map(names.map(name => [name, context(after.catalogs.vrf, name)])) + const changes = names.filter(name => oldContext.get(name) !== newContext.get(name)) + const transfer = request.action === "patch" && request.fields.interfaces !== undefined && old && current && no(old.disabled) && no(current.disabled) && members(old.interfaces).length === 1 && members(current.interfaces).length === 1 && old.interfaces !== current.interfaces && oldContext.get(String(old.interfaces)) === old.name && newContext.get(String(current.interfaces)) === current.name + const disable = old && current && no(old.disabled) && yes(current.disabled), reenable = old && current && yes(old.disabled) && no(current.disabled) + if (disable || reenable) must(Object.hasOwn(request.fields, "disabled"), "unrequested enabled transition") + const exemptBefore = new Map>(), exemptAfter = new Map>() + const explain = (resource: string, previous: Row | undefined, next: Row | undefined) => { if (previous) { const rows = exemptBefore.get(resource) ?? new Set(); rows.add(previous); exemptBefore.set(resource, rows) } if (next) { const rows = exemptAfter.get(resource) ?? new Set(); rows.add(next); exemptAfter.set(resource, rows) } } + explain("vrf", old, current) + withVrfTransitionSection("member-inventory", () => { + for (const name of changes) { + const a = before.catalogs.interface_inventory.filter(row => row.name === name), b = after.catalogs.interface_inventory.filter(row => row.name === name) + // The signed admission already resolves exact concrete physical/VLAN/etc + // configuration IDs and their full parent closure. Moving context may alter + // only this same inventory identity's VRF marker, never its concrete type. + must(a.length === 1 && b.length === 1 && a[0].type === b[0].type && a[0][".id"] === b[0][".id"], "member concrete type/parent identity changed") + must(a[0].vrf === oldContext.get(name) && b[0].vrf === newContext.get(name), "member native VRF context") + if (a[0].status !== b[0].status) { + const x = String(a[0].status), y = String(b[0].status) + must(x.length === 24 && y.length === 24 && x.slice(0, 20) === y.slice(0, 20) && x.slice(21) === y.slice(21) && x[20] === (oldContext.get(name) === "main" ? "-" : "V") && y[20] === (newContext.get(name) === "main" ? "-" : "V"), "member status native shape") + } + same("interface_inventory", a[0], b[0], memberInventoryOmissions(before, after, name, a[0], b[0])); explain("interface_inventory", a[0], b[0]) + } + }) + const beforeSources = withVrfTransitionSection("before-address", () => addressSources(before, names)) + const afterSources = withVrfTransitionSection("after-address", () => addressSources(after, names)) + const absences: History["absentLl"] = [] + const routeResource = (source: Source) => source.family === 4 ? "ipv4_route" : "ipv6_route" + const route = (snapshot: VrfProductionSnapshot, source: Source): Row => withVrfTransitionSection("connected-route", () => { const matches = snapshot.catalogs[routeResource(source)].filter(row => routeFor(row, source, snapshot.catalogs.vrf)); must(matches.length === 1, "missing/ambiguous proven connected route"); return matches[0] }) + const usedAfter = new Set() + for (const source of beforeSources) { + const successors = afterSources.filter(candidate => candidate.member === source.member && candidate.network === source.network && candidate.ll === source.ll) + must(successors.length <= 1, "ambiguous address successor") + const successor = successors[0], oldRoute = route(before, source) + if (!successor) { + if (request.action === "delete" && source.ll) { + withVrfTransitionSection("LL-comparison", () => deletedBridgeLinkLocal(before, after, old!, source, oldRoute, beforeSources, afterSources)) + explain(source.resource, source.row, undefined); explain(routeResource(source), oldRoute, undefined); continue + } + must(source.ll && transfer && current!.interfaces === source.member && oldContext.get(source.member) === "main", "address/route absence outside exact acquisition") + absences.push({ address: structuredClone(source.row), route: structuredClone(oldRoute), member: source.member }); explain(source.resource, source.row, undefined); explain(routeResource(source), oldRoute, undefined); continue + } + usedAfter.add(successor) + const moved = changes.includes(source.member) + if (source.ll) { + const concreteNoMacGre = [before, after].every(snapshot => snapshot.catalogs.interface_inventory.some(row => row.name === source.member && row.type === "gre-tunnel" && !Object.hasOwn(row, "mac-address"))) + withVrfTransitionSection("LL-comparison", () => same(source.resource, source.row, successor.row, moved && concreteNoMacGre ? [".id", "vrf"] : [".id"])) + } + else { must(source.row[".id"] === successor.row[".id"], "static address identity changed"); same(source.resource, source.row, successor.row, ["vrf"]) } + const newRoute = route(after, successor) + must(Object.hasOwn(oldRoute, "local-address") === Object.hasOwn(newRoute, "local-address"), "connected route local-address shape changed") + const ownedDelete = request.action === "delete" && oldContext.get(source.member) === old!.name + withVrfTransitionSection("connected-route", () => same(routeResource(source), oldRoute, newRoute, moved ? [".id", "gateway", "immediate-gw", "routing-table", "local-address"] : transfer || ownedDelete ? [".id"] : [])) + if (source.ll && source.row[".id"] !== successor.row[".id"]) must(moved || transfer || ownedDelete, "LL ID changed outside native move") + explain(source.resource, source.row, successor.row); explain(routeResource(source), oldRoute, newRoute) + } + for (const successor of afterSources.filter(source => !usedAfter.has(source))) { + const missing = history?.absentLl.filter(item => item.member === successor.member) ?? [] + must(request.action === "delete" && successor.ll && missing.length === 1, "new address/route adoption forbidden") + withVrfTransitionSection("LL-comparison", () => same(successor.resource, missing[0].address, successor.row, [".id"])); const nextRoute = route(after, successor) + must(Object.hasOwn(missing[0].route, "local-address") === Object.hasOwn(nextRoute, "local-address"), "revived route local-address shape") + same(routeResource(successor), missing[0].route, nextRoute, [".id", "gateway", "immediate-gw", "routing-table", "local-address"]) + explain(successor.resource, undefined, successor.row); explain(routeResource(successor), undefined, nextRoute) + } + for (const missing of history?.absentLl ?? []) if (!afterSources.some(source => source.member === missing.member && source.ll)) { must(request.action !== "delete", "DELETE missing expected LL revival"); absences.push(structuredClone(missing)) } + let active: Row | undefined, dormant: Row | undefined + withVrfTransitionSection("virtual", () => { + for (const resource of ["routing_table", "interface_inventory"]) { + const find = (snapshot: VrfProductionSnapshot, vrf: Row | undefined): Row | undefined => { + if (!vrf) return undefined + const matches = snapshot.catalogs[resource].filter(row => resource === "routing_table" ? row[".id"] === vrfId : row.type === "vrf" && row.name === vrf.name && row.vrf === "main") + must(matches.length <= 1, "ambiguous target companion"); return matches[0] + } + const a = find(before, old), b = find(after, current) + if (resource === "routing_table") { + const check = (row: Row | undefined, vrf: Row | undefined) => { if (vrf && no(vrf.disabled)) must(row && row[".id"] === vrfId && row.name === vrf.name && yes(row.dynamic) && row.fib === "" && row.vrf === undefined, "VRF table exact native parent"); else must(!row, "disabled/deleted table unexpectedly present") } + check(a, old); check(b, current) + if (a && b) same(resource, a, b, ["name"]) + else if (!old && b) must(Object.keys(b).every(field => isVrfProductionTelemetryValue(resource, field, b[field]) || [".id", "name", "dynamic", "fib"].includes(field)), "new table unknown fields") + else if (reenable && b) { + if (history?.dormantTable) same(resource, history.dormantTable, b, ["name"]) + else must(Object.keys(b).every(field => isVrfProductionTelemetryValue(resource, field, b[field]) || [".id", "name", "dynamic", "fib"].includes(field)), "newly enabled table unknown fields") + } + else if (disable && a) dormant = structuredClone(a) + else if (old && current && yes(old.disabled)) dormant = history?.dormantTable + } else { + if (old) must(a, "missing prior virtual interface") + if (current) must(b, "missing resulting virtual interface") + if (!old && b) { + const enabled = current && no(current.disabled) + if (enabled) activeVirtual(b, vrfId); else inactiveVirtual(b) + must(Object.keys(b).every(field => isVrfProductionTelemetryValue(resource, field, b[field]) || [".id", "caps", "disabled", "flags", "fp-rps-drop", "id", "ifindex", "ifname", "last-link-up-time", "link-downs", "mac-address", "mtu", "name", "status", "type", "vrf"].includes(field)) && b.caps === "" && b["fp-rps-drop"] === "0" && b["link-downs"] === "0" && typeof b.id === "string" && /^\d+$/.test(b.id) && BigInt(b.id) === BigInt("0x" + nativeId(b[".id"])) && (!enabled || matchesGeneratedVrfIfname(b.ifname, vrfId) && after.catalogs.interface_inventory.filter(row => row.ifindex === b.ifindex).length === 1 && after.catalogs.interface_inventory.filter(row => String(row["mac-address"]).toLowerCase() === String(b["mac-address"]).toLowerCase()).length === 1), "new virtual unknown fields/defaults/generated identity") + if (enabled) active = structuredClone(b) + } else if (a && b) { + must(a[".id"] === b[".id"], "virtual interface ID changed") + if (disable) { + activeVirtual(a, vrfId) + must(yes(b.disabled) && b.status === "------------------------" && b["mac-address"] === "00:00:00:00:00:00" && a["link-downs"] === "0" && b["link-downs"] === "1" && a["last-link-down-time"] === undefined && timestamp(b["last-link-down-time"]) && String(b["last-link-down-time"]) >= String(a["last-link-up-time"]) && b.running === "false" && ["ifname", "ifindex", "flags", "actual-mtu"].every(field => !Object.hasOwn(b, field)), "bounded virtual disable shape") + same(resource, a, b, ["name", "disabled", "status", "mac-address", "link-downs", "last-link-down-time", "ifname", "ifindex", "flags"]); active = structuredClone(a) + } else if (reenable) { + inactiveVirtual(a); activeVirtual(b, vrfId) + const previousIfname = history?.activeVirtual?.ifname + const sameIfname = previousIfname === undefined ? matchesGeneratedVrfIfname(b.ifname, vrfId) : b.ifname === previousIfname + must(sameIfname && (a["last-link-down-time"] === undefined || timestamp(a["last-link-down-time"]) && String(b["last-link-up-time"]) >= a["last-link-down-time"]) && after.catalogs.interface_inventory.filter(row => row.ifindex === b.ifindex).length === 1 && after.catalogs.interface_inventory.filter(row => typeof row["mac-address"] === "string" && String(row["mac-address"]).toLowerCase() === String(b["mac-address"]).toLowerCase()).length === 1, "reenable generated identity/chronology") + same(resource, a, b, ["name", "disabled", "status", "mac-address", "ifname", "ifindex", "flags", "last-link-up-time"]); active = structuredClone(b) + } else { same(resource, a, b, ["name"]); active = history?.activeVirtual } + } + } + explain(resource, a, b) + } + }) + // No explained successor may acquire an ID belonging to a foreign row. + withVrfTransitionSection("foreign", () => { + for (const resource of Object.keys(before.catalogs)) { + const a = exemptBefore.get(resource) ?? new Set(), b = exemptAfter.get(resource) ?? new Set() + const foreignBefore = before.catalogs[resource].filter(row => !a.has(row)), foreignAfter = after.catalogs[resource].filter(row => !b.has(row)) + must(isDeepStrictEqual(foreignBefore.map(row => projected(resource, row)), foreignAfter.map(row => projected(resource, row))), `foreign ${resource} configuration/order changed`, "FOREIGN_CATALOG_CONFIGURATION_ORDER_CHANGED") + for (const row of b) must(!foreignBefore.some(previous => nativeId(previous[".id"]) === nativeId(row[".id"])), "companion acquired foreign ID") + } + }) + const checkpoint: VrfProductionTransitionCheckpoint = Object.freeze({ kind: "vrf-production-transition-checkpoint-v1" }) + const provenance: CausalProvenance = { missingMembers: [...new Set(absences.map(item => item.member))].map(name => { + const inventory = after.catalogs.interface_inventory.find(row => row.name === name); must(inventory, "missing LL causal member") + return memberWitness(inventory, after) + }), greMembers: [...new Set([...names, ...(history?.provenance.greMembers ?? []).map(row => String(row.name))])].flatMap(name => { + const inventory = after.catalogs.interface_inventory.find(row => row.name === name) + return inventory?.type === "gre-tunnel" && !Object.hasOwn(inventory, "mac-address") ? [memberWitness(inventory, after)] : [] + }) } + if (current && yes(current.disabled) && (active || dormant)) { + const virtual = after.catalogs.interface_inventory.find(row => row.type === "vrf" && row.name === current.name && row.vrf === "main") + must(virtual, "missing inactive causal virtual"); provenance.inactiveCycle = witness(virtual, cycleFields) + } + histories.set(checkpoint, { snapshot: structuredClone(after), vrfId, activeVirtual: active, dormantTable: dormant, absentLl: absences, provenance }) + return { status: "explained-native-transition", executionAvailable: false, forwardingVerified: false, checkpoint, explainedRows: [...exemptAfter.values()].reduce((sum, rows) => sum + rows.size, 0) } +} diff --git a/backend/src/services/vrf-production-wireguard-alias.test.ts b/backend/src/services/vrf-production-wireguard-alias.test.ts new file mode 100644 index 00000000..b43c1dcd --- /dev/null +++ b/backend/src/services/vrf-production-wireguard-alias.test.ts @@ -0,0 +1,119 @@ +import assert from "node:assert/strict" +import test from "node:test" +import { readVrfProductionSnapshot, VRF_PRODUCTION_RAW_PATHS, type ReadVrfProductionInput } from "./vrf-production-read.js" +import { sealVrfProductionAdmission, inspectVrfProductionAdmission, VRF_PRODUCTION_REQUIRED_CATALOGS, type VrfProductionSnapshot } from "./vrf-production-admission.js" +import { vrfProductionDependencyObjects } from "./vrf-production-transition.js" + +const marker = "mcp-route-fea781dcfaab", wg = marker + "-wg", gre = marker + "-gre-a" +const proof = "a".repeat(64), secret = Buffer.alloc(32, 73), now = 100000 +const binding = { serverId: 1, sourceProof: proof, actorId: "fixture-owner", planId: "fixture-vrf-plan" } + +// Reduced configuration from the V23 safe snapshot: GRE -> static outer IPv4 -> +// RouterOS /interface type "wg". Original native IDs and raw type stay intact. +function model(type = "wg") { + const catalogs: VrfProductionSnapshot["catalogs"] = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, []])) + catalogs.ip_settings = [{ "rp-filter": "loose" }] + catalogs.ipv6_settings = [{ "disable-ipv6": "false" }] + catalogs.vrf = [{ ".id": "*0", name: "main", interfaces: "all", builtin: "true", disabled: "false" }] + catalogs.routing_table = [{ ".id": "*0", name: "main", fib: "true" }] + catalogs.interface_inventory = [{ ".id": "*1B", name: wg, type, disabled: "false" }, + { ".id": "*1C", name: gre, type: "gre-tunnel", disabled: "false" }] + catalogs.wireguard = [{ ".id": "*1B", name: wg, "listen-port": "55100", mtu: "1420", disabled: "false", comment: marker }] + catalogs.wireguard_peer = [{ ".id": "*3", interface: wg, "endpoint-address": "192.0.2.7", "endpoint-port": "55101", + "allowed-address": "198.18.245.10/32,198.18.245.14/32", "persistent-keepalive": "10s", disabled: "false", comment: marker }] + catalogs.gre = [{ ".id": "*1C", name: gre, "local-address": "198.18.245.9", "remote-address": "198.18.245.10", + "allow-fast-path": "false", "clamp-tcp-mss": "true", disabled: "false", comment: marker }] + catalogs.ipv4_address = [{ ".id": "*1F", interface: wg, address: "198.18.245.9/30", dynamic: "false", disabled: "false", vrf: "main", comment: marker }, + { ".id": "*21", interface: gre, address: "198.18.245.1/30", dynamic: "false", disabled: "false", vrf: "main", comment: marker }] + catalogs.ipv4_route = [{ ".id": "*30", "dst-address": "198.18.245.0/30", dynamic: "true", connect: "true", "routing-table": "main", gateway: gre, "immediate-gw": gre }] + let enabled = true, sourceProof = proof, revoked = false + let afterGet = (_path: string) => {} + const calls: string[] = [] + const input: ReadVrfProductionInput = { serverId: 1, expectedSourceProof: proof, + request: { action: "create", placeBeforeId: "*0", fields: { name: marker + "-vrf", interfaces: gre, comment: marker } }, + async assertAuthority() { assert(!revoked, "Owner revoked") }, + adapter: { async resolve() { return { serverId: 1, enabled, sourceProof, client: { async get(path: string) { + calls.push(path) + const resource = Object.keys(VRF_PRODUCTION_RAW_PATHS).find(key => VRF_PRODUCTION_RAW_PATHS[key] === path) + assert(resource && Object.hasOwn(catalogs, resource), "Unexpected raw read") + const result = structuredClone(catalogs[resource]) + afterGet(path) + return result as T + } } } } }, + } + return { catalogs, input, calls, revoke() { revoked = true }, disable() { enabled = false }, + changeSource() { sourceProof = "b".repeat(64) }, afterGet(callback: (path: string) => void) { afterGet = callback } } +} + +for (const type of ["wg", "wireguard"]) test(`reads and seals GRE through static outer IPv4 and ${type} parent without changing native identity`, async () => { + const m = model(type) + const snapshot = await readVrfProductionSnapshot(m.input) + assert.equal(snapshot.catalogs.interface_inventory[0].type, type) + assert(m.calls.includes("/interface/wireguard/peers")) + const issued = sealVrfProductionAdmission({ snapshot, request: m.input.request, binding, secret, now }) + const checked = inspectVrfProductionAdmission({ sealed: issued.sealed, snapshot, request: m.input.request, binding, secret, now: now + 1 }) + assert.deepEqual(checked.admission.members[0].parents.map(parent => [parent.id, parent.resource, parent.configurationId]), + [["*1C", "gre", "*1C"], ["*1B", "wireguard", "*1B"]]) + assert.equal(checked.admission.members[0].addresses.length, 2) + assert.deepEqual(vrfProductionDependencyObjects(snapshot, checked.admission).map(object => [object.kind, object.id]), [ + ["gre", "*1C"], ["interface_inventory", "*1C"], ["wireguard", "*1B"], ["interface_inventory", "*1B"], + ["ipv4_address", "*1F"], ["ipv4_address", "*21"], ["ipv4_route", "*30"], + ]) +}) + +// Synthetic provenance and route evidence close the layer that alias-only +// reader/admission tests previously skipped; they do not establish a live cause. +test("alias dependency closure refuses missing static provenance and exact connected route evidence", async () => { + for (const mutation of ["absent-vrf", "absent-dynamic", "missing-route", "immediate-gw-mismatch"]) { + const m = model() + const address = m.catalogs.ipv4_address[1] + if (mutation === "absent-vrf") delete address.vrf + if (mutation === "absent-dynamic") delete address.dynamic + if (mutation === "missing-route") m.catalogs.ipv4_route = [] + if (mutation === "immediate-gw-mismatch") m.catalogs.ipv4_route[0]["immediate-gw"] = wg + const snapshot = await readVrfProductionSnapshot(m.input) + const issued = sealVrfProductionAdmission({ snapshot, request: m.input.request, binding, secret, now }) + assert.throws(() => vrfProductionDependencyObjects(snapshot, issued.admission), + mutation.startsWith("absent-") ? /static address provenance\/context/ : /dependency connected route identity/) + } +}) + +test("unsupported parent types and ambiguous or dynamic tunnel source parents still refuse", async () => { + for (const type of ["wg-unknown", "__proto__", "constructor"]) { + const m = model(type) + await assert.rejects(readVrfProductionSnapshot(m.input), /unsupported\/missing concrete/) + } + for (const mutation of ["duplicate-address", "dynamic-address", "duplicate-inventory", "unresolved-peer"]) { + const m = model() + if (mutation === "duplicate-address") m.catalogs.ipv4_address.push({ ...m.catalogs.ipv4_address[0], ".id": "*99" }) + if (mutation === "dynamic-address") m.catalogs.ipv4_address[0].dynamic = "true" + if (mutation === "duplicate-inventory") m.catalogs.interface_inventory.push({ ...m.catalogs.interface_inventory[0], ".id": "*99" }) + if (mutation === "unresolved-peer") m.catalogs.wireguard_peer[0]["endpoint-address"] = "peer.example.test" + await assert.rejects(readVrfProductionSnapshot(m.input), /VRF production read refused/) + } +}) + +test("authority revocation, disabled source and source drift still stop the aliased parent read", async () => { + for (const change of ["revoke", "disable", "changeSource"] as const) { + const m = model() + m.afterGet(path => { if (path === "/interface/wireguard") m[change]() }) + await assert.rejects(readVrfProductionSnapshot(m.input)) + assert.equal(m.calls.at(-1), "/interface/wireguard") + } +}) + +test("sealed alias admission still refuses stale tickets and peer or root configuration drift", async () => { + const m = model(), snapshot = await readVrfProductionSnapshot(m.input) + const issued = sealVrfProductionAdmission({ snapshot, request: m.input.request, binding, secret, now, ttlMs: 1000 }) + const input = { sealed: issued.sealed, snapshot, request: m.input.request, binding, secret, now: now + 1 } + assert.throws(() => inspectVrfProductionAdmission({ ...input, now: now + 1000 }), /expired\/future ticket/) + for (const change of ["peer-disabled", "peer-allowed", "wg-disabled", "inventory-id", "gre-source"]) { + const changed = structuredClone(snapshot) + if (change === "peer-disabled") changed.catalogs.wireguard_peer[0].disabled = "true" + if (change === "peer-allowed") changed.catalogs.wireguard_peer[0]["allowed-address"] = "0.0.0.0/0" + if (change === "wg-disabled") changed.catalogs.wireguard[0].disabled = "true" + if (change === "inventory-id") changed.catalogs.interface_inventory[0][".id"] = "*99" + if (change === "gre-source") changed.catalogs.gre[0]["local-address"] = "198.18.245.13" + assert.throws(() => inspectVrfProductionAdmission({ ...input, snapshot: changed }), /VRF production admission refused/) + } +}) diff --git a/backend/src/services/vrf-transition-refusal.test.mjs b/backend/src/services/vrf-transition-refusal.test.mjs new file mode 100644 index 00000000..de0a3d3c --- /dev/null +++ b/backend/src/services/vrf-transition-refusal.test.mjs @@ -0,0 +1,77 @@ +import assert from 'node:assert/strict' +import {readFile} from 'node:fs/promises' +import {createHmac} from 'node:crypto' +import ts from 'typescript' + +// Pure production modules only: no SQL/HTTP authority adapters are imported. +const cache=new Map() +async function moduleUri(name){ + if(cache.has(name))return cache.get(name) + const loading=(async()=>{ + let code=ts.transpileModule(await readFile(new URL(name,import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ES2022}}).outputText + const ast=ts.createSourceFile(name,code,ts.ScriptTarget.ES2022,true) + const imports=ast.statements.filter(ts.isImportDeclaration).filter(node=>node.moduleSpecifier.text.startsWith('./')) + const edits=await Promise.all(imports.map(async node=>({start:node.moduleSpecifier.getStart(ast),end:node.moduleSpecifier.end,value:await moduleUri(node.moduleSpecifier.text.replace(/\.js$/,'.ts'))}))) + for(const edit of edits.reverse())code=code.slice(0,edit.start)+JSON.stringify(edit.value)+code.slice(edit.end) + return'data:text/javascript;base64,'+Buffer.from(code).toString('base64') + })();cache.set(name,loading);return loading +} +const a=await import(await moduleUri('./vrf-production-admission.ts')),t=await import(await moduleUri('./vrf-production-transition.ts')),p=await import(await moduleUri('./vrf-production-projection.ts')) + +const r=await import(await moduleUri('./vrf-transition-refusal.ts')) +const d=await import(await moduleUri('./vrf-execution-diagnostic.ts')) +const fixture=JSON.parse(await readFile(new URL('./fixtures/vrf-projection-v2.json',import.meta.url),'utf8')) +const {before,binding,request,scope,now,acknowledgedId}=fixture,after=structuredClone(fixture.after),secret=Buffer.alloc(32,fixture.secretFill) +// Correct this current synthetic vector only; preserve genuine legacy fixture. +for(const row of after.catalogs.ipv6_address){const parent=after.catalogs.interface_inventory.find(item=>item.name===row.interface);if(row.dynamic==='true'&&parent?.type==='gre-tunnel'&&!Object.hasOwn(parent,'mac-address'))row.vrf=parent.vrf} +const issued=a.sealVrfProductionAdmission({snapshot:before,binding,request,secret,now}) +const input={before,after,binding,request,secret,now,sealed:issued.sealed,acknowledgedId} +const native=after.catalogs.interface_inventory.find(row=>row.type==='gre-tunnel') +const ll=snapshot=>snapshot.catalogs.ipv6_address.find(row=>row.interface===native.name&&row.dynamic==='true') +const inventory=snapshot=>snapshot.catalogs.interface_inventory.find(row=>row.name===native.name) +const caught=work=>{let result;try{work()}catch(error){result=error}assert.ok(result,'expected refusal');return result} +let cases=0 +assert.equal(t.classifyVrfProductionTransition(input).status,'explained-native-transition');cases++ +for(const [mutate,guard,section]of [ + [s=>{ll(s).vrf='main'},'GRE_LINK_LOCAL_EFFECTIVE_CONTEXT','after-address'], + [s=>{inventory(s).vrf='main'},'MEMBER_NATIVE_VRF_CONTEXT','member-inventory'], + [s=>{inventory(s)['link-downs']='999'},'BOUNDED_GRE_CYCLE_COUNT','member-inventory'], + [s=>{inventory(s)['last-link-down-time']='2026-11-05 19:42:15'},'BOUNDED_GRE_CYCLE_COUNT','member-inventory'], + [s=>{inventory(s).opaque='SECRET_NATIVE_VALUE'},'OPAQUE_CONFIGURATION_DRIFT','member-inventory'], + [s=>{s.catalogs.ip_filter.push({'.id':'*fffa',opaque:'SECRET_NATIVE_VALUE'})},'FOREIGN_CATALOG_CONFIGURATION_ORDER_CHANGED','foreign'], +]){ + const changed=structuredClone(after);mutate(changed) + const error=caught(()=>t.classifyVrfProductionTransition({...input,after:changed})) + assert.equal(Object.getPrototypeOf(error),Error.prototype) + const tag=r.readVrfTransitionRefusal(error);assert.deepEqual(tag,{guard,section}) + const event=d.vrfExecutionDiagnostic('12345678-1234-4123-8123-123456789abc','create','transition-classification',error,true) + assert.deepEqual(event.transition,tag);assert.equal(JSON.stringify(event).includes('SECRET_NATIVE_VALUE'),false) + assert.equal(caught(()=>r.withVrfTransitionSection('virtual',()=>{throw error})),error) + assert.deepEqual(r.readVrfTransitionRefusal(error),tag) + tag.section='virtual';assert.equal(r.readVrfTransitionRefusal(error).section,section);cases++ +} +const fake=Error('VRF production transition refused: GRE link-local effective context') +Object.defineProperty(fake,'message',{get(){throw Error('forbidden message read')}}) +Object.defineProperty(fake,'stack',{get(){throw Error('forbidden stack read')}}) +assert.equal(r.readVrfTransitionRefusal(fake),undefined) +assert.equal(d.vrfExecutionDiagnostic('12345678-1234-4123-8123-123456789abc','create','transition-classification',fake,false).transition,undefined) +assert.equal(caught(()=>r.withVrfTransitionSection('foreign',()=>{throw fake})),fake) +assert.equal(r.readVrfTransitionRefusal(fake),undefined);cases++ +assert.equal(r.readVrfTransitionRefusal(new Proxy(fake,{get(){throw Error('proxy')}})),undefined);cases++ +let coerced=0 +const unowned=Error('unowned') +r.noteVrfTransitionRefusal(unowned,{[Symbol.toPrimitive](){coerced++;return 'opaque configuration drift'}}) +assert.equal(coerced,0);assert.equal(r.readVrfTransitionRefusal(unowned),undefined) +assert.equal(Object.isFrozen(r.VRF_TRANSITION_REASON_CODES),true) +assert.throws(()=>Object.defineProperty(r.VRF_TRANSITION_REASON_CODES,'opaque configuration drift',{get(){throw Error('mapping getter')}})) +cases++ +assert.equal(new Set(Object.values(r.VRF_TRANSITION_REASON_CODES)).size,Object.values(r.VRF_TRANSITION_REASON_CODES).length) +const source=await readFile(new URL('./vrf-production-transition.ts',import.meta.url),'utf8'),ast=ts.createSourceFile('transition.ts',source,ts.ScriptTarget.ES2022,true) +let staticReasons=0,dynamicReasons=0 +function visit(node){if(ts.isCallExpression(node)&&node.expression.getText(ast)==='must'){ + const reason=node.arguments[1] + if(ts.isStringLiteral(reason)){assert.ok(Object.hasOwn(r.VRF_TRANSITION_REASON_CODES,reason.text));staticReasons++} + else{assert.equal(node.arguments[2]?.text,'FOREIGN_CATALOG_CONFIGURATION_ORDER_CHANGED');dynamicReasons++} +}ts.forEachChild(node,visit)}visit(ast) +assert.ok(staticReasons>50);assert.equal(dynamicReasons,1);cases++ +console.log('GRE effective-context, strict chronology, owned original-error tags and section identity cases:',cases) diff --git a/backend/src/services/vrf-transition-refusal.ts b/backend/src/services/vrf-transition-refusal.ts new file mode 100644 index 00000000..9bd95b99 --- /dev/null +++ b/backend/src/services/vrf-transition-refusal.ts @@ -0,0 +1,148 @@ +import { types } from "node:util" + +// Explicit immutable code table, never derive a tag from +// a message, stack, native value, dynamic resource name or caller JSON. +export const VRF_TRANSITION_REASON_CODES = Object.freeze({ + "DELETE bridge LL owned context": "DELETE_BRIDGE_LL_OWNED_CONTEXT", + "DELETE bridge LL concrete parent": "DELETE_BRIDGE_LL_CONCRETE_PARENT", + "DELETE bridge LL empty configuration": "DELETE_BRIDGE_LL_EMPTY_CONFIGURATION", + "DELETE bridge LL automatic provenance": "DELETE_BRIDGE_LL_AUTOMATIC_PROVENANCE", + "DELETE bridge LL static address closure": "DELETE_BRIDGE_LL_STATIC_ADDRESS_CLOSURE", + "DELETE bridge LL connected route closure": "DELETE_BRIDGE_LL_CONNECTED_ROUTE_CLOSURE", + "bounded GRE cycle member identity/context": "BOUNDED_GRE_CYCLE_MEMBER_IDENTITY_CONTEXT", + "bounded GRE cycle active state": "BOUNDED_GRE_CYCLE_ACTIVE_STATE", + "bounded GRE cycle parent/configuration": "BOUNDED_GRE_CYCLE_PARENT_CONFIGURATION", + "bounded GRE cycle count": "BOUNDED_GRE_CYCLE_COUNT", + "bounded GRE cycle chronology": "BOUNDED_GRE_CYCLE_CHRONOLOGY", + "CIDR": "CIDR", + "CREATE exact new ACK identity": "CREATE_EXACT_NEW_ACK_IDENTITY", + "CREATE exact ordered insertion": "CREATE_EXACT_ORDERED_INSERTION", + "DELETE exact target": "DELETE_EXACT_TARGET", + "DELETE missing expected LL revival": "DELETE_MISSING_EXPECTED_LL_REVIVAL", + "EUI64 link-local address": "EUI64_LINK_LOCAL_ADDRESS", + "GRE auto-link-local native flags/identity": "GRE_AUTO_LINK_LOCAL_NATIVE_FLAGS_IDENTITY", + "GRE causal parent/endpoint changed": "GRE_CAUSAL_PARENT_ENDPOINT_CHANGED", + "GRE concrete source parent": "GRE_CONCRETE_SOURCE_PARENT", + "GRE exact concrete parent": "GRE_EXACT_CONCRETE_PARENT", + "GRE exact source parent": "GRE_EXACT_SOURCE_PARENT", + "GRE exact static source": "GRE_EXACT_STATIC_SOURCE", + "GRE explicit IPv4 endpoints": "GRE_EXPLICIT_IPV4_ENDPOINTS", + "GRE inventory effective context": "GRE_INVENTORY_EFFECTIVE_CONTEXT", + "GRE link-local effective context": "GRE_LINK_LOCAL_EFFECTIVE_CONTEXT", + "GRE native no-MAC parent": "GRE_NATIVE_NO_MAC_PARENT", + "GRE observed remote-bound auto-link-local": "GRE_OBSERVED_REMOTE_BOUND_AUTO_LINK_LOCAL", + "IPv6 words": "IPV6_WORDS", + "JSON nesting budget": "JSON_NESTING_BUDGET", + "LL ID changed outside native move": "LL_ID_CHANGED_OUTSIDE_NATIVE_MOVE", + "LL inventory": "LL_INVENTORY", + "PATCH exact target/order": "PATCH_EXACT_TARGET_ORDER", + "VRF identity/enabled state": "VRF_IDENTITY_ENABLED_STATE", + "VRF table exact native parent": "VRF_TABLE_EXACT_NATIVE_PARENT", + "address context": "ADDRESS_CONTEXT", + "address/route absence outside exact acquisition": "ADDRESS_ROUTE_ABSENCE_OUTSIDE_EXACT_ACQUISITION", + "after source/catalog set changed": "AFTER_SOURCE_CATALOG_SET_CHANGED", + "ambiguous ID aliases": "AMBIGUOUS_ID_ALIASES", + "ambiguous address successor": "AMBIGUOUS_ADDRESS_SUCCESSOR", + "ambiguous equal-prefix address sources": "AMBIGUOUS_EQUAL_PREFIX_ADDRESS_SOURCES", + "ambiguous members": "AMBIGUOUS_MEMBERS", + "ambiguous target companion": "AMBIGUOUS_TARGET_COMPANION", + "bounded native active virtual shape": "BOUNDED_NATIVE_ACTIVE_VIRTUAL_SHAPE", + "bounded native inactive virtual shape": "BOUNDED_NATIVE_INACTIVE_VIRTUAL_SHAPE", + "bounded virtual disable shape": "BOUNDED_VIRTUAL_DISABLE_SHAPE", + "catalog budget": "CATALOG_BUDGET", + "checkpoint parent/configuration/order drift": "CHECKPOINT_PARENT_CONFIGURATION_ORDER_DRIFT", + "checkpoint scope": "CHECKPOINT_SCOPE", + "checkpoint target changed": "CHECKPOINT_TARGET_CHANGED", + "companion acquired foreign ID": "COMPANION_ACQUIRED_FOREIGN_ID", + "complete raw17 snapshot": "COMPLETE_RAW17_SNAPSHOT", + "connected route local-address shape changed": "CONNECTED_ROUTE_LOCAL_ADDRESS_SHAPE_CHANGED", + "dependency connected route identity": "DEPENDENCY_CONNECTED_ROUTE_IDENTITY", + "dependency object source": "DEPENDENCY_OBJECT_SOURCE", + "disabled/deleted table unexpectedly present": "DISABLED_DELETED_TABLE_UNEXPECTEDLY_PRESENT", + "disabled/unknown selected address unsupported": "DISABLED_UNKNOWN_SELECTED_ADDRESS_UNSUPPORTED", + "dynamic non-link-local address": "DYNAMIC_NON_LINK_LOCAL_ADDRESS", + "foreign VRF configuration/order changed": "FOREIGN_VRF_CONFIGURATION_ORDER_CHANGED", + "historical LL absence changed": "HISTORICAL_LL_ABSENCE_CHANGED", + "history source changed": "HISTORY_SOURCE_CHANGED", + "inactive causal cycle/table absence changed": "INACTIVE_CAUSAL_CYCLE_TABLE_ABSENCE_CHANGED", + "inactive causal identity/context changed": "INACTIVE_CAUSAL_IDENTITY_CONTEXT_CHANGED", + "inactive virtual chronology": "INACTIVE_VIRTUAL_CHRONOLOGY", + "incomplete legacy GRE provenance": "INCOMPLETE_LEGACY_GRE_PROVENANCE", + "link-local parent flags/MAC": "LINK_LOCAL_PARENT_FLAGS_MAC", + "main VRF shape": "MAIN_VRF_SHAPE", + "mapped IPv6 alias": "MAPPED_IPV6_ALIAS", + "member concrete type/parent identity changed": "MEMBER_CONCRETE_TYPE_PARENT_IDENTITY_CHANGED", + "member native VRF context": "MEMBER_NATIVE_VRF_CONTEXT", + "member status native shape": "MEMBER_STATUS_NATIVE_SHAPE", + "missing LL causal member": "MISSING_LL_CAUSAL_MEMBER", + "missing LL causal witnesses": "MISSING_LL_CAUSAL_WITNESSES", + "missing LL member causal identity/MAC/context changed": "MISSING_LL_MEMBER_CAUSAL_IDENTITY_MAC_CONTEXT_CHANGED", + "missing inactive causal virtual": "MISSING_INACTIVE_CAUSAL_VIRTUAL", + "missing ordered main VRF": "MISSING_ORDERED_MAIN_VRF", + "missing primary ACK target ID": "MISSING_PRIMARY_ACK_TARGET_ID", + "missing prior virtual interface": "MISSING_PRIOR_VIRTUAL_INTERFACE", + "missing resulting virtual interface": "MISSING_RESULTING_VIRTUAL_INTERFACE", + "missing/ambiguous proven connected route": "MISSING_AMBIGUOUS_PROVEN_CONNECTED_ROUTE", + "native ID": "NATIVE_ID", + "native row": "NATIVE_ROW", + "new address/route adoption forbidden": "NEW_ADDRESS_ROUTE_ADOPTION_FORBIDDEN", + "new table unknown fields": "NEW_TABLE_UNKNOWN_FIELDS", + "new virtual unknown fields/defaults/generated identity": "NEW_VIRTUAL_UNKNOWN_FIELDS_DEFAULTS_GENERATED_IDENTITY", + "newly enabled table unknown fields": "NEWLY_ENABLED_TABLE_UNKNOWN_FIELDS", + "non-JSON raw value": "NON_JSON_RAW_VALUE", + "opaque configuration drift": "OPAQUE_CONFIGURATION_DRIFT", + "prefix length": "PREFIX_LENGTH", + "private checkpoint budget": "PRIVATE_CHECKPOINT_BUDGET", + "private checkpoint budget/key": "PRIVATE_CHECKPOINT_BUDGET_KEY", + "private checkpoint encoding": "PRIVATE_CHECKPOINT_ENCODING", + "private checkpoint scope/version": "PRIVATE_CHECKPOINT_SCOPE_VERSION", + "private checkpoint signature": "PRIVATE_CHECKPOINT_SIGNATURE", + "private checkpoint source/history": "PRIVATE_CHECKPOINT_SOURCE_HISTORY", + "raw byte budget": "RAW_BYTE_BUDGET", + "reenable generated identity/chronology": "REENABLE_GENERATED_IDENTITY_CHRONOLOGY", + "requested primary field mismatch": "REQUESTED_PRIMARY_FIELD_MISMATCH", + "revived route local-address shape": "REVIVED_ROUTE_LOCAL_ADDRESS_SHAPE", + "singleton cardinality": "SINGLETON_CARDINALITY", + "static address identity changed": "STATIC_ADDRESS_IDENTITY_CHANGED", + "static address provenance/context": "STATIC_ADDRESS_PROVENANCE_CONTEXT", + "unknown member selector": "UNKNOWN_MEMBER_SELECTOR", + "unknown private checkpoint": "UNKNOWN_PRIVATE_CHECKPOINT", + "unrecognized serialized checkpoint": "UNRECOGNIZED_SERIALIZED_CHECKPOINT", + "unrequested enabled transition": "UNREQUESTED_ENABLED_TRANSITION", + "unreviewed CREATE defaults": "UNREVIEWED_CREATE_DEFAULTS", + "virtual interface ID changed": "VIRTUAL_INTERFACE_ID_CHANGED" +} as const) +export type VrfTransitionGuard = typeof VRF_TRANSITION_REASON_CODES[keyof typeof VRF_TRANSITION_REASON_CODES] | "FOREIGN_CATALOG_CONFIGURATION_ORDER_CHANGED" +export const VRF_TRANSITION_GUARDS = [...Object.values(VRF_TRANSITION_REASON_CODES), "FOREIGN_CATALOG_CONFIGURATION_ORDER_CHANGED"] as const +export const VRF_TRANSITION_SECTIONS = ["member-inventory", "before-address", "after-address", "LL-comparison", "connected-route", "virtual", "foreign"] as const +export type VrfTransitionSection = typeof VRF_TRANSITION_SECTIONS[number] +export interface VrfTransitionRefusalTag { guard: VrfTransitionGuard; section?: VrfTransitionSection } +const guards = new Set(VRF_TRANSITION_GUARDS), sections = new Set(VRF_TRANSITION_SECTIONS) +const owned = new WeakMap>() +/** Internal transition must() calls this immediately after creating its Error. + * Preserve that exact Error and its properties; no replacement/wrapping. */ +export function noteVrfTransitionRefusal(error: Error, reason: string, explicit?: VrfTransitionGuard): void { + try { + if (typeof reason !== "string") return + const guard = explicit ?? (Object.hasOwn(VRF_TRANSITION_REASON_CODES, reason) ? VRF_TRANSITION_REASON_CODES[reason as keyof typeof VRF_TRANSITION_REASON_CODES] : undefined) + if (types.isNativeError(error) && guard && guards.has(guard)) owned.set(error, Object.freeze({ guard })) + } catch { /* Diagnostic recording cannot replace the original refusal. */ } +} +export function readVrfTransitionRefusal(error: unknown): VrfTransitionRefusalTag | undefined { + if (!error || typeof error !== "object") return undefined + const value = owned.get(error) + return value ? { ...value } : undefined +} +/** Synchronous pure classifier sections only. Never tag an unowned error; keep + * the innermost established section and rethrow the original object unchanged. */ +export function withVrfTransitionSection(section: VrfTransitionSection, work: () => T): T { + try { return work() } catch (error) { + try { + if (error && typeof error === "object") { + const value = owned.get(error) + if (value && !value.section && sections.has(section)) owned.set(error, Object.freeze({ ...value, section })) + } + } catch { /* Never replace the original error. */ } + throw error + } +} diff --git a/backend/src/services/vxlan-live.test.ts b/backend/src/services/vxlan-live.test.ts index 43716f13..c154adfc 100644 --- a/backend/src/services/vxlan-live.test.ts +++ b/backend/src/services/vxlan-live.test.ts @@ -67,11 +67,13 @@ console.log("vxlan: exact ARP modes and RouterOS no-learning passed") assert.equal(mapVxlanRow(server, { ...complete, arp: "enabled", "arp-proxy": "true" }, [], 0).arpProxy, false) -for (const field of [".id", "name", "vni", "port", "l2mtu", "mtu", "disabled", "running", "arp", "learning"]) { +for (const field of [".id", "name", "vni", "port", "mtu", "disabled", "running", "arp", "learning"]) { const incomplete = { ...complete } as Record delete incomplete[field] assert.throws(() => mapVxlanSnapshot(server, [incomplete], []), /Некорректный снимок/) } +const { l2mtu: _l2mtu, ...withoutL2 } = complete +assert.equal(mapVxlanSnapshot(server, [withoutL2], [])[0].l2mtu, null, "RouterOS omits unsupported/unavailable l2mtu; never invent a positive MTU") for (const bad of [{ vni: "12junk" }, { vni: "0" }, { port: "0" }, { port: "65536" }, { l2mtu: "unknown" }, { running: "unknown" }]) { assert.throws(() => mapVxlanSnapshot(server, [{ ...complete, ...bad }], []), /Некорректный снимок/) } diff --git a/backend/src/services/vxlan-live.ts b/backend/src/services/vxlan-live.ts index ce74e4b1..6f4dbc22 100644 --- a/backend/src/services/vxlan-live.ts +++ b/backend/src/services/vxlan-live.ts @@ -41,7 +41,7 @@ export type VxlanTunnelLive = { serverId: string vtepIp: string remoteVteps: string[] - l2mtu: number + l2mtu: number | null mtu: number arpProxy: boolean arpMode: "disabled" | "enabled" | "local-proxy-arp" | "proxy-arp" | "reply-only" @@ -95,7 +95,7 @@ export function mapVxlanRow( serverId: String(server.id), vtepIp: (vx["local-address"] ?? vx["vtep-address"] ?? "").trim(), remoteVteps, - l2mtu: readInteger(vx.l2mtu, "l2mtu", 1, 65535), + l2mtu: vx.l2mtu === undefined ? null : readInteger(vx.l2mtu, "l2mtu", 0, 65535), mtu: readInteger(vx.mtu, "mtu", 1, 65535), arpProxy: arpMode === "proxy-arp" || arpMode === "local-proxy-arp", arpMode, diff --git a/backend/src/services/wan-runtime-model.test.ts b/backend/src/services/wan-runtime-model.test.ts new file mode 100644 index 00000000..d2019c29 --- /dev/null +++ b/backend/src/services/wan-runtime-model.test.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { buildWanRuntime } from "./wan-runtime-model.js" + +const uplinks = [{ id: "wan", iface: "ether1", name: "WAN", isp: "Fixture", ip: "192.0.2.10" }] +const route = { ".id": "*1", "dst-address": "0.0.0.0/0", "routing-table": "main", active: "true", distance: "1", gateway: "192.0.2.1", "immediate-gw": "192.0.2.1%ether1" } +const source = { dhcp: [{ interface: "ether1", address: "192.0.2.10/24", gateway: "192.0.2.1", status: "bound" }], addresses: [{ interface: "ether1", address: "192.0.2.10/24" }], routes: [route] } +test("confirmed native IPv4/main default and bound DHCP, without a forwarding claim", () => { + const result = buildWanRuntime(uplinks, source) + assert.equal(result.defaultRouteStatus, "confirmed"); assert.equal(result.defaultInterface, "ether1") + assert.equal(result.uplinks[0].leasedIp, "192.0.2.10"); assert.equal(result.uplinks[0].addressSource, "dhcp-bound") + assert.equal(result.uplinks[0].isDefault, true); assert.equal(result.uplinks[0].defaultKnown, true) + assert.equal(result.forwardingVerified, false) +}) +test("ECMP entries and a multi-gateway native row do not select the first WAN", () => { + for (const routes of [[route, { ...route, ".id": "*2", gateway: "198.51.100.1", "immediate-gw": "198.51.100.1%ether2" }], [{ ...route, gateway: "192.0.2.1,198.51.100.1" }]]) { + const result = buildWanRuntime(uplinks, { ...source, routes }) + assert.equal(result.defaultRouteStatus, "ambiguous"); assert.equal(result.defaultGateway, null) + assert.equal(result.uplinks[0].isDefault, false); assert.equal(result.uplinks[0].defaultKnown, false) + } +}) +test("missing table, activity or distance and contradictory active flags remain unknown", () => { + for (const fields of [{ active: undefined }, { "routing-table": undefined }, { distance: "1garbage" }, { disabled: "true" }, { inactive: "true" }]) { + const value = Object.fromEntries(Object.entries({ ...route, ...fields }).filter(([, value]) => value !== undefined)) + const result = buildWanRuntime(uplinks, { ...source, routes: [value] }) + assert.equal(result.defaultRouteStatus, "unknown"); assert.equal(result.defaultInterface, null) + } + const missingAlternative = { ".id": "*2", "dst-address": "0.0.0.0/0", "routing-table": "main" } + assert.equal(buildWanRuntime(uplinks, { ...source, routes: [route, missingAlternative] }).defaultRouteStatus, "unknown") +}) +test("known inactive/foreign table routes and empty route arrays are not a main default", () => { + for (const routes of [[], [{ ...route, active: "false" }], [{ ...route, "routing-table": "isolated" }]]) { + const result = buildWanRuntime(uplinks, { ...source, routes }) + assert.equal(result.defaultRouteStatus, "none"); assert.equal(result.uplinks[0].defaultKnown, true) + } +}) +test("an unbound lease is not used; multiple native addresses and conflicting interfaces remain ambiguous", () => { + const result = buildWanRuntime(uplinks, { ...source, dhcp: [{ ...source.dhcp[0], status: "searching", address: "203.0.113.77/24" }], addresses: [...source.addresses, { interface: "ether1", address: "192.0.2.11/24" }] }) + assert.equal(result.uplinks[0].leasedIp, null); assert.equal(result.uplinks[0].addressSource, "ambiguous") + const conflict = buildWanRuntime(uplinks, { ...source, routes: [{ ...route, interface: "ether2" }] }) + assert.equal(conflict.defaultInterface, null); assert.equal(conflict.uplinks[0].defaultKnown, false) +}) +test("malformed source shapes fail; invalid IPv4 and prefix suffixes are not addresses", () => { + for (const key of ["dhcp", "addresses", "routes"]) assert.throws(() => buildWanRuntime(uplinks, { ...source, [key]: {} })) + for (const address of ["192.0.2.1/24garbage", "192.0.2.1/33", "192.0.2.1/24/1", "999.0.0.1", "2001:db8::1/64"]) { + const result = buildWanRuntime(uplinks, { ...source, dhcp: [], addresses: [{ interface: "ether1", address }] }) + assert.equal(result.uplinks[0].leasedIp, null) + } +}) diff --git a/backend/src/services/wan-runtime-model.ts b/backend/src/services/wan-runtime-model.ts new file mode 100644 index 00000000..b18ec73a --- /dev/null +++ b/backend/src/services/wan-runtime-model.ts @@ -0,0 +1,70 @@ +import { isIP } from "node:net" +import { observedBoolean, observedInteger } from "./ospf-observed-values.js" +import { readInternetPathRows } from "./internet-path-snapshot.js" + +type Uplink = { id: string; iface: string; name: string; isp: string; ip: string } +type Row = Record +function ipv4(raw: string | undefined): string | null { + if (!raw) return null + const [address, prefix, extra] = raw.trim().split("/") + return isIP(address) === 4 && extra === undefined && (prefix === undefined || /^\d+$/.test(prefix) && Number(prefix) <= 32) ? address : null +} +function gatewayInterface(raw: string | undefined): string | null { + const parts = raw?.trim().split("%") + return parts?.length === 2 && parts[1] ? parts[1] : null +} +function gatewayAddress(raw: string | undefined): string | null { return ipv4(raw?.split("%")[0]) } +function unique(values: Array) { return [...new Set(values.filter((value): value is string => !!value))] } + +/** A local IPv4/main observation, not packet forwarding or a policy-route proof. */ +export function buildWanRuntime(uplinks: Uplink[], sources: { dhcp: unknown; addresses: unknown; routes: unknown }) { + const dhcp = readInternetPathRows(sources.dhcp, "WAN DHCP", ["interface"]) + const addresses = readInternetPathRows(sources.addresses, "WAN addresses", ["address", "interface"]) + const routes = readInternetPathRows(sources.routes, "WAN routes", ["dst-address"]) + const findings = new Set() + const defaults = routes.filter(row => row["dst-address"] === "0.0.0.0/0") + const possibleMain = defaults.filter(row => row["routing-table"] === "main" || !row["routing-table"]) + const unknown = possibleMain.some(row => !row["routing-table"] || observedBoolean(row.active) === null || observedBoolean(row.active) === true && (observedBoolean(row.disabled) === true || observedBoolean(row.inactive) === true || observedInteger(row.distance, 0, 255) === null)) + if (unknown) findings.add("default-route-state-unknown") + const active = possibleMain.filter(row => row["routing-table"] === "main" && observedBoolean(row.active) === true && observedBoolean(row.disabled) !== true && observedBoolean(row.inactive) !== true && observedInteger(row.distance, 0, 255) !== null) + const bestDistance = active.length ? Math.min(...active.map(row => Number(row.distance))) : null + const best = active.filter(row => Number(row.distance) === bestDistance) + const multipath = best.some(row => row.gateway?.includes(",") || row["immediate-gw"]?.includes(",")) + const defaultRouteStatus = unknown ? "unknown" : best.length > 1 || multipath ? "ambiguous" : best.length === 1 ? "confirmed" : "none" + if (multipath) findings.add("multipath-default-gateway") + if (best.length > 1) findings.add("multiple-active-default-routes") + const selected = defaultRouteStatus === "confirmed" ? best[0] : undefined + const gateway = selected?.gateway?.trim() || null, immediateGateway = selected?.["immediate-gw"]?.trim() || null + + const inferInterface = (route: Row): string | null => { + const explicit = unique([gatewayInterface(route["immediate-gw"]), gatewayInterface(route.gateway), route.interface?.trim()]) + if (explicit.length === 1) return explicit[0] + if (explicit.length > 1) { findings.add("default-interface-conflict"); return null } + const target = gatewayAddress(route["immediate-gw"]) ?? gatewayAddress(route.gateway) + if (!target) return null + const candidates = unique(dhcp.filter(row => row.status?.toLowerCase() === "bound" && observedBoolean(row.disabled) !== true && gatewayAddress(row.gateway) === target).map(row => row.interface)) + if (candidates.length === 1) return candidates[0] + if (candidates.length > 1) findings.add("default-interface-ambiguous") + return null + } + const defaultInterface = selected ? inferInterface(selected) : null + if (selected && !defaultInterface) findings.add("default-interface-unknown") + const resultUplinks = uplinks.map(uplink => { + const iface = uplink.iface.trim() + const clients = dhcp.filter(row => row.interface.trim() === iface && observedBoolean(row.disabled) !== true) + const leases = unique(clients.filter(row => row.status?.toLowerCase() === "bound").map(row => ipv4(row.address))) + const interfaceAddresses = unique(addresses.filter(row => row.interface.trim() === iface && observedBoolean(row.disabled) !== true && observedBoolean(row.invalid) !== true).map(row => ipv4(row.address))) + const statuses = unique(clients.map(row => row.status?.trim())) + const addressSource = leases.length === 1 ? "dhcp-bound" : leases.length > 1 || interfaceAddresses.length > 1 ? "ambiguous" : interfaceAddresses.length === 1 ? "interface" : "unknown" + const leasedIp = leases.length === 1 ? leases[0] : leases.length === 0 && interfaceAddresses.length === 1 ? interfaceAddresses[0] : null + if (addressSource === "ambiguous") findings.add("uplink-address-ambiguous") + return { id: uplink.id, iface, name: uplink.name, isp: uplink.isp, configuredIp: uplink.ip, leasedIp, + dhcpStatus: statuses.length === 1 ? statuses[0] : null, isDefault: defaultInterface !== null && defaultInterface === iface, + addressSource, addresses: interfaceAddresses, defaultKnown: defaultRouteStatus === "none" || defaultRouteStatus === "confirmed" && defaultInterface !== null, + } + }) + return { defaultGateway: gateway, immediateGateway, defaultInterface, uplinks: resultUplinks, defaultRouteStatus, + activeDefaults: active.slice(0, 200).map(row => ({ id: row[".id"] ?? null, gateway: row.gateway ?? null, immediateGateway: row["immediate-gw"] ?? null, distance: Number(row.distance) })), + activeDefaultsTruncated: active.length > 200, findings: [...findings], family: "ipv4", table: "main", forwardingVerified: false, + } +} diff --git a/backend/src/services/wan-runtime.ts b/backend/src/services/wan-runtime.ts new file mode 100644 index 00000000..c850fe00 --- /dev/null +++ b/backend/src/services/wan-runtime.ts @@ -0,0 +1,11 @@ +import { MikrotikClient } from "./mikrotik.js" +import { buildWanRuntime } from "./wan-runtime-model.js" +import { toServerRead } from "../modules/servers/mapper/servers-mapper.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" + +export async function readWanRuntimeForServer(server: ServerRow) { + if (!server.enabled) throw Object.assign(new Error("Сервер выключен. Опрос WAN не выполнялся"), { statusCode: 409 }) + const client = MikrotikClient.fromServer(server) + const [dhcp, addresses, routes] = await Promise.all([client.get("/ip/dhcp-client"), client.get("/ip/address"), client.get("/ip/route")]) + return buildWanRuntime(toServerRead(server, undefined).wanUplinks ?? [], { dhcp, addresses, routes }) +} diff --git a/backend/src/services/wireguard-config.test.ts b/backend/src/services/wireguard-config.test.ts index 4370ad14..8ed8cca1 100644 --- a/backend/src/services/wireguard-config.test.ts +++ b/backend/src/services/wireguard-config.test.ts @@ -6,7 +6,12 @@ import { parseMikrotikRsc, parseNativeConf, parseWgConfig, + generatePeerClientConf, + WG_CONFIG_LIMITS, + splitWgEndpoint, + formatWgEndpoint, } from "./wireguard-config.js" +import { parseWireguardImport } from "./wireguard-import.js" const sampleConf = `[Interface] PrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa= @@ -44,7 +49,7 @@ const roundConf = generateNativeConf({ : undefined, persistentKeepalive: p.persistentKeepalive, })), -}) +}, { includePrivateKey: true }) const reparsed = parseNativeConf(roundConf) assert.equal(reparsed.interface.privateKey, parsedConf.interface.privateKey) assert.equal(reparsed.peers[0]?.publicKey, parsedConf.peers[0]?.publicKey) @@ -98,3 +103,61 @@ assert.equal(rscAgain.interface.name, "wg-msk-spb") assert.equal(rscAgain.peers[0]?.publicKey, parsedRsc.peers[0]?.publicKey) console.log("wireguard-config tests ok") + +const privateKey = Buffer.alloc(32, 81).toString("base64"), peerKey = Buffer.alloc(32, 82).toString("base64"), otherKey = Buffer.alloc(32, 83).toString("base64"), peerPrivate = Buffer.alloc(32, 84).toString("base64") +const literal = 'Юникод 😀 "quotes" \\ $[ /system reboot ]; ${variable} [x] #tag' +const full = { name: 'wg "edge" \\ $() [1]; тест', enabled: false, mtu: 1380, listenPort: 24567, privateKey, address: "2001:db8::1/64", comment: literal, + peers: [{ publicKey: peerKey, allowedIps: ["192.0.2.2/32", "2001:db8::2/128"], endpoint: "[2001:db8::10]:33445", persistentKeepalive: 0, disabled: true, privateKey: peerPrivate, name: literal, comment: literal, clientAddress: "192.0.2.2", clientDns: "1.1.1.1", clientEndpoint: literal }, + { publicKey: otherKey, allowedIps: ["198.51.100.2/32"], disabled: false, privateKey: "none", endpointAddress: "vpn.example.test", endpointPort: 12345 }] } +const fullRsc = generateMikrotikRsc(full, { includePrivateKey: true }), decoded = parseWireguardImport(fullRsc, "rsc") +assert.equal(decoded.interface.name, full.name); assert.equal(decoded.interface.comment, literal); assert.equal(decoded.interface.privateKey, privateKey); assert.equal(decoded.interface.disabled, true) +assert.equal(decoded.peers.length, 2); assert.equal(decoded.peers[0].name, literal); assert.equal(decoded.peers[0].comment, literal); assert.equal(decoded.peers[0].clientEndpoint, literal) +assert.equal(decoded.peers[0].privateKey, peerPrivate); assert.equal(decoded.peers[0].disabled, true); assert.equal(decoded.peers[1].disabled, false); assert.equal(decoded.peers[1].privateKey, "none") +assert.equal(decoded.peers[0].endpointAddress, "2001:db8::10"); assert.equal(decoded.peers[0].endpointPort, 33445); assert.equal(decoded.peers[0].persistentKeepalive, 0) +assert.deepEqual(splitWgEndpoint(formatWgEndpoint("2001:db8::10", 33445)), { address: "2001:db8::10", port: 33445 }) +assert.equal(fullRsc.includes("\\$["), true); assert.equal(fullRsc.includes('comment="'), true) +for (const comment of ['\\$name', '\\"quoted"', '$()', '$[]', 'slash\\end', '#;[]{}()', '😀\\$"; [ :put x ]']) assert.equal(parseMikrotikRsc(generateMikrotikRsc({ name: "literal", mtu: 1420, comment, peers: [] })).interface.comment, comment) +for (const flags of [undefined, { includePrivateKey: false }]) { + const redacted = generateMikrotikRsc(full, flags) + assert.equal(redacted.includes(privateKey), false); assert.equal(redacted.includes(peerPrivate), false); assert.equal(parseMikrotikRsc(redacted).interface.privateKey, undefined) + assert.equal(parseMikrotikRsc(redacted).peers[1].privateKey, "none") + const confWithoutKey = generateNativeConf({ ...full, enabled: true, peers: [{ ...full.peers[0], disabled: false }] }, flags) + assert.equal(confWithoutKey.includes(privateKey), false); assert.equal(confWithoutKey.includes(peerPrivate), false) + assert.equal(generatePeerClientConf({ peerPrivateKey: privateKey, serverPublicKey: peerKey }, flags).includes(privateKey), false) +} +const interop = generateNativeConf({ name: "wg0", mtu: 1420, privateKey, address: "192.0.2.1/24", peers: [{ publicKey: peerKey, allowedIps: ["0.0.0.0/0"], endpoint: "[2001:db8::10]:33445", persistentKeepalive: 0 }] }, { includePrivateKey: true }) +assert.equal(parseWireguardImport(interop, "conf").interface.privateKey, privateKey); assert.equal(parseWireguardImport(interop, "conf").peers[0].persistentKeepalive, 0) +assert.equal(generatePeerClientConf({ peerPrivateKey: privateKey, serverPublicKey: peerKey }, { includePrivateKey: true }).includes(privateKey), true) +assert.throws(() => generateMikrotikRsc({ ...full, comment: privateKey }), /Приватный ключ/) +assert.throws(() => generateNativeConf({ ...full, enabled: true, peers: [{ ...full.peers[0], disabled: false, comment: peerPrivate }] }), /Приватный ключ/) +assert.throws(() => generatePeerClientConf({ peerPrivateKey: privateKey, serverPublicKey: privateKey }), /Приватный ключ/) +assert.throws(() => generateNativeConf(full, { includePrivateKey: true }), /Отключённые/) +assert.throws(() => generateNativeConf({ ...full, enabled: true }, { includePrivateKey: true }), /Отключённые/) +for (const unsafe of ["[2001:db8::10]", "2001:db8::10:33445", "host:0", "host:65536", "host;ignored:22", "host\nPostUp=x:22", "[fe80::1%eth0]:22"]) assert.throws(() => splitWgEndpoint(unsafe)) +for (const source of [ + '/interface wireguard add name="$[ /system reboot ]"', + '/interface wireguard add name=[/system identity get name]', + '/interface wireguard add name="wg"; /system reboot', + '/interface wireguard add name="wg" /system reboot', + '/interface wireguard add name="wg" name="duplicate"', + '/interface wireguard add name="wg" on-up="NEVER_SCRIPT"', + '/interface wireguard add name="unterminated', + '/interface wireguard add name="wg"\n/system reboot', + '/interface wireguard add name="wg"\n/interface wireguard add name="other"', + '/interface wireguard add name="wg"\\', + '/interface wireguard add name="\\nNEVER_SCRIPT"', + '/interface wireguard add name="wg" comment="\\ff"', +]) assert.throws(() => parseMikrotikRsc(source), (error: Error) => !error.message.includes("NEVER_SCRIPT")) +assert.equal(parseMikrotikRsc('/interface wireguard add name="\\D0\\9C\\D0\\9A"').interface.name, "МК") +const multiline = 'line one\n/system reboot\r\n[Peer]\rPrivateKey = literal\t$[x] "quoted"' +const multilineSource = generateMikrotikRsc({ ...full, comment: multiline, peers: [{ ...full.peers[0], comment: multiline }] }, { includePrivateKey: true }) +assert.equal(multilineSource.split("\n").filter(line => line.startsWith("/system")).length, 0) +const multilineParsed = parseWireguardImport(multilineSource, "rsc") +assert.equal(multilineParsed.interface.comment, multiline); assert.equal(multilineParsed.peers[0].comment, multiline) +const multilineConf = generateNativeConf({ ...full, enabled: true, comment: multiline, peers: [{ ...full.peers[0], disabled: false, comment: multiline }] }, { includePrivateKey: true }) +assert.equal((multilineConf.match(/^\[Peer\]$/gm) ?? []).length, 1) +assert.ok(multilineConf.includes("# /system reboot")); assert.ok(multilineConf.includes("# PrivateKey = literal")) +assert.equal(parseNativeConf(multilineConf).peers.length, 1) +for (const bad of [{ ...full, name: "bad\nname" }, { ...full, comment: "bad\0comment" }, { ...full, peers: [{ ...full.peers[0], name: "bad\npeer" }] }]) assert.throws(() => generateMikrotikRsc(bad)) +for (const source of ["#".repeat(WG_CONFIG_LIMITS.bytes + 1), "\n".repeat(WG_CONFIG_LIMITS.lines), '#'.repeat(WG_CONFIG_LIMITS.lineBytes + 1), '/interface wireguard add name="' + "я".repeat(WG_CONFIG_LIMITS.tokenBytes / 2 + 1) + '"']) assert.throws(() => parseWgConfig(source, "rsc")) +console.log("WireGuard codecs: explicit secret flags, literal RSC escaping, all peer fields and disabled state, bracket IPv6, zero keepalive, unknown/script rejection and UTF-8 work bounds passed") diff --git a/backend/src/services/wireguard-config.ts b/backend/src/services/wireguard-config.ts index 7b35912b..a45229b0 100644 --- a/backend/src/services/wireguard-config.ts +++ b/backend/src/services/wireguard-config.ts @@ -1,3 +1,4 @@ +import { isIP } from "node:net" /** * WireGuard config codecs: native .conf ↔ MikroTik .rsc */ @@ -47,6 +48,10 @@ export type WgExportIface = { publicKey: string allowedIps: string[] endpoint?: string + endpointAddress?: string + endpointPort?: number + disabled?: boolean + privateKey?: string persistentKeepalive?: number persistent?: boolean comment?: string @@ -57,310 +62,269 @@ export type WgExportIface = { }> } -function stripQuotes(v: string): string { - const t = v.trim() - if ((t.startsWith('"') && t.endsWith('"')) || (t.startsWith("'") && t.endsWith("'"))) { - return t.slice(1, -1) - } - return t -} -function parseKvLine(line: string): Record { - const out: Record = {} - // Match key=value pairs; values may be quoted - const re = /([a-zA-Z0-9_-]+)=("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|[^\s\\]+)/g - let m: RegExpExecArray | null - while ((m = re.exec(line)) !== null) { - out[m[1]] = stripQuotes(m[2]) - } - return out +/** Limits bound decoded input and parser work, including content held in secret references. */ +export const WG_CONFIG_LIMITS = Object.freeze({ bytes: 262144, lines: 4096, lineBytes: 16384, tokenBytes: 4096, peers: 100, addressesPerPeer: 100, fieldsPerCommand: 24 }) +const invalid = (message = "Неподдерживаемая или неоднозначная конфигурация WireGuard") => new Error(message) +function text(value: string, multilineComment = false) { + if (typeof value !== "string" || Buffer.byteLength(value) > WG_CONFIG_LIMITS.tokenBytes || (multilineComment ? /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/ : /[\u0000-\u001f\u007f]/).test(value) || Buffer.from(value).toString("utf8") !== value) throw invalid() + return value } - -function joinContinuedLines(text: string): string[] { - const raw = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n") - const lines: string[] = [] - let buf = "" - for (const line of raw) { - const trimmedEnd = line.replace(/\s+$/, "") - if (trimmedEnd.endsWith("\\")) { - buf += trimmedEnd.slice(0, -1).trimEnd() + " " - continue - } - buf += trimmedEnd - if (buf.trim()) lines.push(buf.trim()) - buf = "" - } - if (buf.trim()) lines.push(buf.trim()) +function inputLines(content: string) { + if (typeof content !== "string" || Buffer.byteLength(content) > WG_CONFIG_LIMITS.bytes || /\0/.test(content) || Buffer.from(content).toString("utf8") !== content) throw invalid("Превышен предел или некорректный текст конфигурации WireGuard") + const lines = content.replace(/\r\n|\r/g, "\n").split("\n") + if (lines.length > WG_CONFIG_LIMITS.lines || lines.some(line => Buffer.byteLength(line) > WG_CONFIG_LIMITS.lineBytes)) throw invalid("Превышен предел строк конфигурации WireGuard") return lines } - -export function detectWgConfigFormat(content: string): "rsc" | "conf" { - const t = content.trim() - if (/\[Interface\]/i.test(t) || /\[Peer\]/i.test(t)) return "conf" - if (/\/interface\s+wireguard/i.test(t) || /\/interface\/wireguard/i.test(t)) return "rsc" - if (/PrivateKey\s*=/i.test(t) || /PublicKey\s*=/i.test(t)) return "conf" - return "rsc" +function integer(value: string, field: string) { + if (!/^\d+$/.test(value) || !Number.isSafeInteger(Number(value))) throw invalid("Некорректный " + field) + return Number(value) } - -export function parseNativeConf(content: string): WgParsedConfig { - const lines = content.replace(/\r\n/g, "\n").split("\n") - let section: "interface" | "peer" | null = null - const iface: WgParsedInterface = { name: "wg0" } - const peers: WgParsedPeer[] = [] - let currentPeer: WgParsedPeer | null = null - - const flushPeer = () => { - if (currentPeer?.publicKey) peers.push(currentPeer) - currentPeer = null +function boolean(value: string) { + if (value === "yes" || value === "true") return true + if (value === "no" || value === "false") return false + throw invalid("Некорректный disabled или флаг адреса WireGuard") +} +function list(value: string, field: string) { + const values = value.split(",").map(item => item.trim()) + if (!values.length || values.length > WG_CONFIG_LIMITS.addressesPerPeer || values.some(item => !item)) throw invalid("Некорректный " + field) + return values +} +export function splitWgEndpoint(value: string): { address: string; port: number } { + text(value) + const match = value.match(/^(?:\[([^\]]+)\]|([^:\s\[\]]+)):(\d+)$/) + if (!match) throw invalid("Некорректный Endpoint: нужен host:port или [IPv6]:port") + const address = match[1] ?? match[2], port = integer(match[3], "Endpoint") + if (!address || (match[1] !== undefined && isIP(address) !== 6) || port < 1 || port > 65535 || !endpointHost(address)) throw invalid("Некорректный Endpoint") + return { address, port } +} +export function formatWgEndpoint(address: string, port: number) { + text(address) + if (!endpointHost(address) || !Number.isSafeInteger(port) || port < 1 || port > 65535) throw invalid("Некорректный Endpoint") + return `${isIP(address) === 6 ? `[${address}]` : address}:${port}` +} +function endpointHost(address: string) { return address.length <= 253 && !address.includes("%") && (isIP(address) !== 0 || /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.?$/i.test(address)) } +function peerEndpoint(peer: WgExportIface["peers"][number]) { + if (peer.endpointAddress !== undefined || peer.endpointPort !== undefined) { + if (peer.endpointAddress === undefined || peer.endpointPort === undefined) throw invalid("Неполный Endpoint") + const endpoint = formatWgEndpoint(peer.endpointAddress, peer.endpointPort) + if (peer.endpoint && formatWgEndpoint(splitWgEndpoint(peer.endpoint).address, splitWgEndpoint(peer.endpoint).port) !== endpoint) throw invalid("Противоречивый Endpoint") + return endpoint } - - for (const raw of lines) { + if (!peer.endpoint) return undefined + const endpoint = splitWgEndpoint(peer.endpoint) + return formatWgEndpoint(endpoint.address, endpoint.port) +} +export function detectWgConfigFormat(content: string): "rsc" | "conf" { + const lines = inputLines(content).map(line => line.trim()).filter(line => line && !/^[#;]/.test(line)) + return lines[0]?.startsWith("[") ? "conf" : "rsc" +} +export function parseNativeConf(content: string): WgParsedConfig { + const iface: WgParsedInterface = { name: "wg0" }, peers: WgParsedPeer[] = [] + let section: "interface" | "peer" | undefined, current: WgParsedPeer | undefined, found = false, seen = new Set() + for (const raw of inputLines(content)) { const line = raw.trim() - if (!line || line.startsWith("#") || line.startsWith(";")) continue - if (/^\[Interface\]$/i.test(line)) { - flushPeer() - section = "interface" - continue + if (!line || /^[#;]/.test(line)) continue + if (line.startsWith("[")) { + if (/^\[Interface\]$/i.test(line)) { + if (found || peers.length) throw invalid("Конфигурация должна содержать одну секцию [Interface] перед [Peer]") + found = true; section = "interface" + } else if (/^\[Peer\]$/i.test(line)) { + if (!found) throw invalid("Сначала нужна секция [Interface]") + if (peers.length >= WG_CONFIG_LIMITS.peers) throw invalid("Превышен предел peers WireGuard") + section = "peer"; current = { publicKey: "", allowedAddresses: [] }; peers.push(current) + } else throw invalid("Неподдерживаемая секция WireGuard") + seen = new Set(); continue } - if (/^\[Peer\]$/i.test(line)) { - flushPeer() - section = "peer" - currentPeer = { publicKey: "", allowedAddresses: [] } - continue - } - const eq = line.indexOf("=") - if (eq < 0) continue - const key = line.slice(0, eq).trim().toLowerCase() - const value = line.slice(eq + 1).trim() - + const match = line.match(/^([A-Za-z][A-Za-z0-9]*)\s*=\s*(.*)$/) + if (!match || !section) throw invalid() + const key = match[1].toLowerCase(), value = text(match[2]) + if (seen.has(key)) throw invalid("Повторяющееся поле конфигурации WireGuard") + seen.add(key) + if (key === "presharedkey") throw invalid("Импорт PresharedKey пока не поддерживается") if (section === "interface") { if (key === "privatekey") iface.privateKey = value - else if (key === "address") iface.address = value.split(",")[0]?.trim() - else if (key === "listenport") iface.listenPort = Number(value) - else if (key === "mtu") iface.mtu = Number(value) - else if (key === "name") iface.name = value || iface.name - } else if (section === "peer" && currentPeer) { - if (key === "publickey") currentPeer.publicKey = value - else if (key === "allowedips") { - currentPeer.allowedAddresses = value - .split(",") - .map((s) => s.trim()) - .filter(Boolean) - } else if (key === "endpoint") { - const lastColon = value.lastIndexOf(":") - if (lastColon > 0 && !value.includes("]:")) { - currentPeer.endpointAddress = value.slice(0, lastColon) - currentPeer.endpointPort = Number(value.slice(lastColon + 1)) - } else if (value.startsWith("[") && value.includes("]:")) { - const idx = value.indexOf("]:") - currentPeer.endpointAddress = value.slice(1, idx) - currentPeer.endpointPort = Number(value.slice(idx + 2)) - } else { - currentPeer.endpointAddress = value - } - } else if (key === "persistentkeepalive") { - currentPeer.persistentKeepalive = Number(value) - } else if (key === "presharedkey") { - // ignore PSK for ROS import for now + else if (key === "address") { if (value.includes(",")) throw invalid("Импорт нескольких адресов Address пока не поддерживается"); iface.address = value } + else if (key === "listenport") iface.listenPort = integer(value, "ListenPort") + else if (key === "mtu") iface.mtu = integer(value, "MTU") + else if (key === "name") iface.name = value + else throw invalid("Неподдерживаемое поле [Interface] WireGuard") + } else { + if (key === "publickey") current!.publicKey = value + else if (key === "allowedips") current!.allowedAddresses = list(value, "AllowedIPs") + else if (key === "endpoint") { const endpoint = splitWgEndpoint(value); current!.endpointAddress = endpoint.address; current!.endpointPort = endpoint.port } + else if (key === "persistentkeepalive") current!.persistentKeepalive = integer(value, "PersistentKeepalive") + else throw invalid("Неподдерживаемое поле [Peer] WireGuard") + } + } + if (!found) throw invalid("Нужна секция [Interface]") + if (peers.some(peer => !peer.publicKey)) throw invalid("У каждого пира должен быть PublicKey") + return { format: "conf", interface: iface, peers } +} +function logicalRscLines(content: string) { + const result: string[] = []; let pending = "" + for (const raw of inputLines(content)) { + const line = raw.trim() + if (!line || line.startsWith("#")) { if (pending) throw invalid(); continue } + let quoted = false + for (let index = 0; index < line.length; index++) { + if (line[index] === String.fromCharCode(92) && quoted) { index++; continue } + if (line[index] === '"') quoted = !quoted + } + if (quoted) throw invalid("Незакрытая строка конфигурации WireGuard") + const continued = line.endsWith(String.fromCharCode(92)) + pending += (continued ? line.slice(0, -1).trimEnd() : line) + (continued ? " " : "") + if (Buffer.byteLength(pending) > WG_CONFIG_LIMITS.lineBytes) throw invalid("Превышен предел команды WireGuard") + if (!continued) { result.push(pending); pending = "" } + } + if (pending) throw invalid("Незавершённая команда WireGuard") + return result +} +/** Constant strings only: no variables, concatenation, command substitution or executable suffix. */ +function rscFields(raw: string) { + const fields: Record = Object.create(null) + let cursor = 0 + while (cursor < raw.length) { + while (/\s/.test(raw[cursor] ?? "") && cursor < raw.length) cursor++ + if (cursor === raw.length || raw[cursor] === "#") break + const match = raw.slice(cursor).match(/^([A-Za-z][A-Za-z0-9-]*)=/) + if (!match) throw invalid() + const key = match[1].toLowerCase(); cursor += match[0].length + if (Object.hasOwn(fields, key) || Object.keys(fields).length >= WG_CONFIG_LIMITS.fieldsPerCommand) throw invalid("Повторяющиеся поля или превышен предел полей WireGuard") + let value = "" + if (raw[cursor] === '"') { + cursor++; const bytes: Buffer[] = []; let closed = false + while (cursor < raw.length) { + const character = raw[cursor++] + if (character === '"') { closed = true; break } + if (character === "$") throw invalid("Переменные и выражения RouterOS не поддерживаются") + if (character !== String.fromCharCode(92)) { const point = raw.codePointAt(cursor - 1)!; const literal = String.fromCodePoint(point); cursor += literal.length - 1; bytes.push(Buffer.from(literal)); continue } + const escaped = raw[cursor++] + if ([String.fromCharCode(92), '"', "$"].includes(escaped)) bytes.push(Buffer.from(escaped)) + else if (["n", "r", "t"].includes(escaped)) bytes.push(Buffer.from(escaped === "n" ? "\n" : escaped === "r" ? "\r" : "\t")) + else if (escaped === "_") bytes.push(Buffer.from(" ")) + else if (/^[0-9A-F]$/.test(escaped ?? "") && /^[0-9A-F]$/.test(raw[cursor] ?? "")) bytes.push(Buffer.from([parseInt(escaped + raw[cursor++], 16)])) + else throw invalid("Неподдерживаемая escape-последовательность RouterOS") + } + if (!closed || cursor < raw.length && !/\s/.test(raw[cursor])) throw invalid() + const buffer = Buffer.concat(bytes); value = buffer.toString("utf8") + if (!Buffer.from(value).equals(buffer)) throw invalid() + } else { + const start = cursor + while (cursor < raw.length && !/\s/.test(raw[cursor])) cursor++ + value = raw.slice(start, cursor) + if (!value || value.includes(String.fromCharCode(92)) || /[$;{}()\[\]"'#]/.test(value)) throw invalid("Исполняемые выражения RouterOS не поддерживаются") + } + fields[key] = text(value, key === "comment") + } + return fields +} +function allowed(fields: Record, keys: string[]) { + if (Object.hasOwn(fields, "preshared-key")) throw invalid("Импорт PresharedKey пока не поддерживается") + if (Object.keys(fields).some(key => !keys.includes(key))) throw invalid("Неподдерживаемое поле RouterOS WireGuard") +} +export function parseMikrotikRsc(content: string): WgParsedConfig { + const iface: WgParsedInterface = { name: "wg0" }, peers: WgParsedPeer[] = [] + let found = false, addressSeen = false + for (const line of logicalRscLines(content)) { + const command = line.match(/^(\/interface(?:\s+|\/)wireguard(?:\s+|\/)peers\s+add|\/interface(?:\s+|\/)wireguard\s+add|\/(?:ip|ipv6)(?:\s+|\/)address\s+add)(?:\s+(.*))?$/i) + if (!command) throw invalid("Неподдерживаемая команда RouterOS WireGuard") + const path = command[1].replace(/\//g, " ").replace(/\s+/g, " ").trim().toLowerCase(), fields = rscFields(command[2] ?? "") + if (path === "interface wireguard add") { + if (found) throw invalid("Конфигурация должна содержать один интерфейс WireGuard") + allowed(fields, ["name", "listen-port", "mtu", "private-key", "comment", "disabled"]) + if (fields.name !== undefined) iface.name = fields.name + if (fields["listen-port"] !== undefined) iface.listenPort = integer(fields["listen-port"], "ListenPort") + if (fields.mtu !== undefined) iface.mtu = integer(fields.mtu, "MTU") + if (fields["private-key"] !== undefined) iface.privateKey = fields["private-key"] + if (fields.comment !== undefined) iface.comment = fields.comment + if (fields.disabled !== undefined) iface.disabled = boolean(fields.disabled) + found = true + } else { + if (!found) throw invalid("Не удалось распознать интерфейс RouterOS WireGuard перед зависимыми объектами") + if (fields.interface !== undefined && fields.interface !== iface.name) throw invalid("Объект относится к другому интерфейсу") + if (path === "interface wireguard peers add") { + allowed(fields, ["interface", "public-key", "allowed-address", "endpoint-address", "endpoint-port", "persistent-keepalive", "comment", "name", "private-key", "client-address", "client-dns", "client-endpoint", "disabled"]) + if (peers.length >= WG_CONFIG_LIMITS.peers) throw invalid("Превышен предел peers WireGuard") + peers.push({ publicKey: fields["public-key"] ?? "", allowedAddresses: fields["allowed-address"] === undefined ? [] : list(fields["allowed-address"], "allowed-address"), + ...(fields["endpoint-address"] === undefined ? {} : { endpointAddress: fields["endpoint-address"] }), ...(fields["endpoint-port"] === undefined ? {} : { endpointPort: integer(fields["endpoint-port"], "Endpoint") }), + ...(fields["persistent-keepalive"] === undefined ? {} : { persistentKeepalive: integer(fields["persistent-keepalive"], "PersistentKeepalive") }), + ...(fields.comment === undefined ? {} : { comment: fields.comment }), ...(fields.name === undefined ? {} : { name: fields.name }), ...(fields["private-key"] === undefined ? {} : { privateKey: fields["private-key"] }), + ...(fields["client-address"] === undefined ? {} : { clientAddress: fields["client-address"] }), ...(fields["client-dns"] === undefined ? {} : { clientDns: fields["client-dns"] }), ...(fields["client-endpoint"] === undefined ? {} : { clientEndpoint: fields["client-endpoint"] }), ...(fields.disabled === undefined ? {} : { disabled: boolean(fields.disabled) }) }) + } else { + allowed(fields, ["interface", "address", "advertise", "disabled", "no-dad", "eui-64", "from-pool"]) + if (addressSeen) throw invalid("Импорт нескольких адресов пока не поддерживается") + if (!fields.address) throw invalid("Не указан Address") + if (fields["from-pool"] || ["advertise", "disabled", "no-dad", "eui-64"].some(key => fields[key] !== undefined && boolean(fields[key]))) throw invalid("Импорт специальных параметров адреса не поддерживается") + if ((path.startsWith("ipv6 ") ? 6 : 4) !== isIP(fields.address.split("/")[0])) throw invalid("Не совпадает семейство Address") + iface.address = fields.address; addressSeen = true } } } - flushPeer() - - if (!iface.name) iface.name = "wg0" - return { format: "conf", interface: iface, peers } -} - -export function parseMikrotikRsc(content: string): WgParsedConfig { - const lines = joinContinuedLines(content) - const iface: WgParsedInterface = { name: "wg0" } - const peers: WgParsedPeer[] = [] - let foundIface = false - - for (const line of lines) { - if (line.startsWith("#")) continue - const lower = line.toLowerCase() - - if ( - lower.startsWith("/interface wireguard add") || - lower.startsWith("/interface/wireguard add") - ) { - const kv = parseKvLine(line) - if (kv.name) iface.name = kv.name - if (kv["listen-port"]) iface.listenPort = Number(kv["listen-port"]) - if (kv.mtu) iface.mtu = Number(kv.mtu) - if (kv["private-key"]) iface.privateKey = kv["private-key"] - if (kv.comment) iface.comment = kv.comment - if (kv.disabled === "yes") iface.disabled = true - foundIface = true - continue - } - - if ( - lower.startsWith("/interface wireguard peers add") || - lower.startsWith("/interface/wireguard/peers add") - ) { - const kv = parseKvLine(line) - const allowed = (kv["allowed-address"] ?? "") - .split(",") - .map((s) => s.trim()) - .filter(Boolean) - peers.push({ - publicKey: kv["public-key"] ?? "", - allowedAddresses: allowed, - endpointAddress: kv["endpoint-address"], - endpointPort: kv["endpoint-port"] - ? Number(kv["endpoint-port"]) - : undefined, - persistentKeepalive: kv["persistent-keepalive"] - ? Number(kv["persistent-keepalive"]) - : undefined, - comment: kv.comment, - name: kv.name, - clientAddress: kv["client-address"], - clientDns: kv["client-dns"], - clientEndpoint: kv["client-endpoint"], - disabled: kv.disabled === "yes", - }) - continue - } - - if (lower.startsWith("/ip address add") || lower.startsWith("/ip/address add") || lower.startsWith("/ipv6 address add") || lower.startsWith("/ipv6/address add")) { - const kv = parseKvLine(line) - if (iface.address) throw new Error("Импорт нескольких адресов пока не поддерживается") - if (kv.interface && kv.interface !== iface.name) throw new Error("Адрес относится к другому интерфейсу") - if (kv["from-pool"] || kv["eui-64"] === "yes" || kv["no-dad"] === "yes" || kv.disabled === "yes" || kv.advertise === "yes") throw new Error("Импорт специальных параметров адреса не поддерживается") - if (kv.address) iface.address = kv.address - continue - } - } - - if (!foundIface) { - throw new Error("Не удалось распознать RouterOS WireGuard .rsc") - } + if (!found) throw invalid("Не удалось распознать RouterOS WireGuard .rsc") return { format: "rsc", interface: iface, peers } } - -export function parseWgConfig( - content: string, - format: "auto" | "rsc" | "conf" = "auto", -): WgParsedConfig { - const detected = format === "auto" ? detectWgConfigFormat(content) : format - if (detected === "conf") return parseNativeConf(content) - return parseMikrotikRsc(content) +export function parseWgConfig(content: string, format: "auto" | "rsc" | "conf" = "auto"): WgParsedConfig { + if (!["auto", "rsc", "conf"].includes(format)) throw invalid() + return (format === "auto" ? detectWgConfigFormat(content) : format) === "conf" ? parseNativeConf(content) : parseMikrotikRsc(content) } - +function checkedExport(iface: WgExportIface) { + text(iface.name); if (!iface.name.trim() || iface.peers.length > WG_CONFIG_LIMITS.peers) throw invalid() + if (iface.comment !== undefined) text(iface.comment, true) + for (const value of [iface.serverName, iface.privateKey, iface.publicKey, iface.address]) if (value !== undefined) text(value) + for (const value of [iface.mtu, iface.listenPort]) if (value !== undefined && (!Number.isSafeInteger(value) || value < 1 || value > 65535)) throw invalid() + for (const peer of iface.peers) { + if (peer.comment !== undefined) text(peer.comment, true) + for (const value of [peer.publicKey, peer.name, peer.privateKey, peer.clientAddress, peer.clientDns, peer.clientEndpoint]) if (value !== undefined) text(value) + if (peer.allowedIps.length > WG_CONFIG_LIMITS.addressesPerPeer) throw invalid() + peer.allowedIps.forEach(value => text(value)); peerEndpoint(peer) + if (peer.persistentKeepalive !== undefined && (!Number.isSafeInteger(peer.persistentKeepalive) || peer.persistentKeepalive < 0 || peer.persistentKeepalive > 65535)) throw invalid() + } +} +function output(value: string) { inputLines(value); return value } +function secretOutput(value: string, keys: Array, includePrivateKey?: boolean) { + if (includePrivateKey !== true && keys.some(key => key && key !== "auto" && key !== "none" && value.includes(key))) throw invalid("Приватный ключ найден в публичных полях экспорта WireGuard") + return output(value) +} +function confAtom(value: string) { text(value); if (!value || /[\s#;]/.test(value)) throw invalid("Значение нельзя представить в WireGuard .conf"); return value } +function keepalive(peer: WgExportIface["peers"][number]) { return peer.persistentKeepalive ?? (peer.persistent ? 25 : undefined) } export function generateNativeConf(iface: WgExportIface, opts?: { includePrivateKey?: boolean }): string { - const lines: string[] = [] - lines.push(`[Interface]`) - if (opts?.includePrivateKey && iface.privateKey) { - lines.push(`PrivateKey = ${iface.privateKey}`) - } else if (iface.privateKey) { - lines.push(`PrivateKey = ${iface.privateKey}`) - } else { - lines.push(`# PrivateKey = <заполните приватный ключ с роутера>`) + checkedExport(iface) + if (iface.enabled === false || iface.peers.some(peer => peer.disabled === true)) throw invalid("Отключённые объекты нельзя воспроизвести в WireGuard .conf") + const lines = ["[Interface]", opts?.includePrivateKey === true && iface.privateKey ? `PrivateKey = ${confAtom(iface.privateKey)}` : "# PrivateKey = <заполните приватный ключ с роутера>"] + if (iface.comment) lines.push(...iface.comment.split(/\r\n|\r|\n/).map(line => `# ${line}`)) + if (iface.address) lines.push(`Address = ${confAtom(iface.address)}`) + if (iface.listenPort !== undefined) lines.push(`ListenPort = ${iface.listenPort}`) + lines.push(`MTU = ${iface.mtu}`, "") + for (const peer of iface.peers) { + lines.push("[Peer]", `PublicKey = ${confAtom(peer.publicKey)}`, `AllowedIPs = ${peer.allowedIps.map(confAtom).join(", ")}`) + const endpoint = peerEndpoint(peer); if (endpoint) lines.push(`Endpoint = ${endpoint}`) + if (keepalive(peer) !== undefined) lines.push(`PersistentKeepalive = ${keepalive(peer)}`) + if (peer.comment) lines.push(...peer.comment.split(/\r\n|\r|\n/).map(line => `# ${line}`)) + lines.push("") } - if (iface.address) lines.push(`Address = ${iface.address}`) - if (iface.listenPort) lines.push(`ListenPort = ${iface.listenPort}`) - if (iface.mtu) lines.push(`MTU = ${iface.mtu}`) - lines.push(``) - - for (const p of iface.peers) { - lines.push(`[Peer]`) - lines.push(`PublicKey = ${p.publicKey}`) - lines.push(`AllowedIPs = ${p.allowedIps.join(", ")}`) - if (p.endpoint) lines.push(`Endpoint = ${p.endpoint}`) - const ka = p.persistentKeepalive ?? (p.persistent ? 25 : undefined) - if (ka != null && ka > 0) lines.push(`PersistentKeepalive = ${ka}`) - if (p.comment) lines.push(`# ${p.comment}`) - lines.push(``) - } - return lines.join("\n").trimEnd() + "\n" + return secretOutput(lines.join("\n").trimEnd() + "\n", [iface.privateKey, ...iface.peers.map(peer => peer.privateKey)], opts?.includePrivateKey) } - -export function generatePeerClientConf(args: { - peerPrivateKey?: string - peerAddress?: string - peerDns?: string - serverPublicKey: string - allowedIps?: string[] - endpoint?: string - persistentKeepalive?: number -}): string { - const lines: string[] = [] - lines.push(`[Interface]`) - lines.push( - args.peerPrivateKey - ? `PrivateKey = ${args.peerPrivateKey}` - : `# PrivateKey = <ключ клиента>`, - ) - if (args.peerAddress?.trim()) lines.push(`Address = ${args.peerAddress.trim()}`) - else lines.push(`# Address = <укажите адрес клиента>`) - if (args.peerDns) lines.push(`DNS = ${args.peerDns}`) - lines.push(``) - lines.push(`[Peer]`) - lines.push(`PublicKey = ${args.serverPublicKey}`) - if (args.allowedIps?.length) lines.push(`AllowedIPs = ${args.allowedIps.join(", ")}`) - else lines.push(`# AllowedIPs = <укажите сети через туннель>`) - if (args.endpoint) lines.push(`Endpoint = ${args.endpoint}`) - else lines.push(`# Endpoint = <укажите адрес сервера и порт>`) - if (args.persistentKeepalive != null && args.persistentKeepalive > 0) { - lines.push(`PersistentKeepalive = ${args.persistentKeepalive}`) - } - lines.push(``) - return lines.join("\n") +export function generatePeerClientConf(args: { peerPrivateKey?: string; peerAddress?: string; peerDns?: string; serverPublicKey: string; allowedIps?: string[]; endpoint?: string; persistentKeepalive?: number }, opts?: { includePrivateKey?: boolean }): string { + const lines = ["[Interface]", opts?.includePrivateKey === true && args.peerPrivateKey ? `PrivateKey = ${confAtom(args.peerPrivateKey)}` : "# PrivateKey = <ключ клиента>"] + if (args.peerAddress?.trim()) lines.push(`Address = ${confAtom(args.peerAddress.trim())}`); else lines.push("# Address = <укажите адрес клиента>") + if (args.peerDns) lines.push(`DNS = ${args.peerDns.split(",").map(value => confAtom(value.trim())).join(", ")}`) + lines.push("", "[Peer]", `PublicKey = ${confAtom(args.serverPublicKey)}`) + if (args.allowedIps?.length) { if (args.allowedIps.length > WG_CONFIG_LIMITS.addressesPerPeer) throw invalid(); lines.push(`AllowedIPs = ${args.allowedIps.map(confAtom).join(", ")}`) } else lines.push("# AllowedIPs = <укажите сети через туннель>") + if (args.endpoint) { const endpoint = splitWgEndpoint(args.endpoint); lines.push(`Endpoint = ${formatWgEndpoint(endpoint.address, endpoint.port)}`) } else lines.push("# Endpoint = <укажите адрес сервера и порт>") + if (args.persistentKeepalive !== undefined) { if (!Number.isSafeInteger(args.persistentKeepalive) || args.persistentKeepalive < 0 || args.persistentKeepalive > 65535) throw invalid(); lines.push(`PersistentKeepalive = ${args.persistentKeepalive}`) } + return secretOutput(lines.join("\n") + "\n", [args.peerPrivateKey], opts?.includePrivateKey) } - -export function generateMikrotikRsc(iface: WgExportIface): string { - const lines: string[] = [] - lines.push(`# WireGuard — ${iface.name}${iface.serverName ? ` · ${iface.serverName}` : ""}`) - lines.push(`# RouterOS 7.x · MikrotikManager`) - lines.push(``) - lines.push(`/interface wireguard add \\`) - lines.push(` name=${iface.name} \\`) - lines.push(` listen-port=${iface.listenPort ?? 13231} \\`) - lines.push(` mtu=${iface.mtu} \\`) - if (iface.privateKey) lines.push(` private-key="${iface.privateKey}" \\`) - if (iface.comment) lines.push(` comment="${iface.comment.replace(/"/g, '\\"')}" \\`) - if (iface.enabled === false) lines.push(` disabled=yes \\`) - // remove trailing backslash on last iface param by rewriting last line - if (lines[lines.length - 1]?.endsWith(" \\")) { - lines[lines.length - 1] = lines[lines.length - 1]!.slice(0, -2) +/** RouterOS double-quoted constant encoding; escape interpolation as well as quotes and backslashes. */ +export function quoteWgRsc(value: string, multilineComment = false) { const slash = String.fromCharCode(92); return '"' + text(value, multilineComment).split(slash).join(slash + slash).replace(/"/g, slash + '"').replace(/[$]/g, slash + "$" ).replace(/\r/g, slash + "r").replace(/\n/g, slash + "n").replace(/\t/g, slash + "t") + '"' } +export function generateMikrotikRsc(iface: WgExportIface, opts?: { includePrivateKey?: boolean }): string { + checkedExport(iface) + const command = (path: string, values: Record) => path + " " + Object.entries(values).filter(([, value]) => value !== undefined).map(([key, value]) => `${key}=${typeof value === "string" ? quoteWgRsc(value, key === "comment") : typeof value === "boolean" ? value ? "yes" : "no" : value}`).join(" ") + const lines = ["# WireGuard - MikrotikManager", command("/interface wireguard add", { name: iface.name, "listen-port": iface.listenPort ?? 13231, mtu: iface.mtu, comment: iface.comment, disabled: iface.enabled === undefined ? undefined : !iface.enabled, "private-key": opts?.includePrivateKey === true ? iface.privateKey : undefined })] + if (iface.address) lines.push(command(`${iface.address.includes(":") ? "/ipv6" : "/ip"} address add`, { address: iface.address, interface: iface.name, ...(iface.address.includes(":") ? { advertise: false } : {}) })) + for (const peer of iface.peers) { + const endpoint = peerEndpoint(peer), parts = endpoint ? splitWgEndpoint(endpoint) : undefined + lines.push(command("/interface wireguard peers add", { interface: iface.name, "public-key": peer.publicKey, "allowed-address": peer.allowedIps.join(","), "endpoint-address": parts?.address, "endpoint-port": parts?.port, "persistent-keepalive": keepalive(peer), comment: peer.comment, name: peer.name, disabled: peer.disabled, "private-key": peer.privateKey === "auto" || peer.privateKey === "none" || opts?.includePrivateKey === true ? peer.privateKey : undefined, "client-address": peer.clientAddress, "client-dns": peer.clientDns, "client-endpoint": peer.clientEndpoint })) } - lines.push(``) - - if (iface.address) { - lines.push(`${iface.address.includes(":") ? "/ipv6" : "/ip"} address add \\`) - lines.push(` address=${iface.address} \\`) - if (iface.address.includes(":")) lines.push(` advertise=no \\`) - lines.push(` interface=${iface.name}`) - lines.push(``) - } - - for (const p of iface.peers) { - lines.push(`/interface wireguard peers add \\`) - lines.push(` interface=${iface.name} \\`) - lines.push(` public-key="${p.publicKey}" \\`) - lines.push(` allowed-address=${p.allowedIps.join(",")} \\`) - if (p.endpoint) { - const host = p.endpoint.includes(":") ? p.endpoint.slice(0, p.endpoint.lastIndexOf(":")) : p.endpoint - const port = p.endpoint.includes(":") - ? p.endpoint.slice(p.endpoint.lastIndexOf(":") + 1) - : "13231" - lines.push(` endpoint-address=${host} \\`) - lines.push(` endpoint-port=${port} \\`) - } - const ka = p.persistentKeepalive ?? (p.persistent ? 25 : undefined) - if (ka != null && ka > 0) lines.push(` persistent-keepalive=${ka} \\`) - if (p.name) lines.push(` name=${p.name} \\`) - if (p.clientAddress) lines.push(` client-address=${p.clientAddress} \\`) - if (p.clientDns) lines.push(` client-dns=${p.clientDns} \\`) - if (p.clientEndpoint) lines.push(` client-endpoint=${p.clientEndpoint} \\`) - if (p.comment) lines.push(` comment="${p.comment.replace(/"/g, '\\"')}" \\`) - if (lines[lines.length - 1]?.endsWith(" \\")) { - lines[lines.length - 1] = lines[lines.length - 1]!.slice(0, -2) - } - lines.push(``) - } - return lines.join("\n") + return secretOutput(lines.join("\n") + "\n", [iface.privateKey, ...iface.peers.map(peer => peer.privateKey)], opts?.includePrivateKey) } diff --git a/backend/src/services/wireguard-dependency-read.test.ts b/backend/src/services/wireguard-dependency-read.test.ts new file mode 100644 index 00000000..524d54ad --- /dev/null +++ b/backend/src/services/wireguard-dependency-read.test.ts @@ -0,0 +1,124 @@ +import assert from "node:assert/strict" +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 { 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" }) + +function fixture(t: TestContext, outcomes: Outcome[]) { + const paths: string[] = [], timeouts: number[] = [] + t.mock.method(http, "request", (options: http.RequestOptions, receive: (res: unknown) => void) => { + assert.equal(options.method, "GET") + paths.push(options.path!) + const request = new EventEmitter() + const outcome = outcomes.shift() + assert(outcome) + return Object.assign(request, { + end() { + if (outcome === "reset") return request.emit("error", reset()) + if (outcome === "informational-reset") { + request.emit("information", { statusCode: 103 }) + return request.emit("error", reset()) + } + const response = Object.assign(new EventEmitter(), { headers: {}, statusCode: outcome === "refused" ? 429 : 200 }) + receive(response) + if (outcome === "late-reset") return request.emit("error", reset()) + if (outcome === "body-reset") return response.emit("error", reset()) + response.emit("data", Buffer.from(outcome === "invalid-json" ? "!" : "[]")) + response.emit("end") + }, + }) + }) + const client = new MikrotikClient({ host: "fixture.invalid", port: 80, useSsl: false, verifySsl: false, username: "fixture", password: "fixture" }) + return { + paths, timeouts, + client: { get(path: string, timeout?: number, signal?: AbortSignal, maxBytes?: number) { + assert.equal(maxBytes, 1_048_576) + timeouts.push(timeout!) + return client.get(path, timeout, signal, maxBytes) + } }, + } +} + +test("dependency diagnostics retain only the fixed catalogue and retry HTTP status", async t => { + const { client, paths } = fixture(t, ["reset", "refused"]) + const read = createWireguardDependencyReader(client, async () => {}) + await assert.rejects(read("/interface"), (error: unknown) => { + assert.deepEqual(wireguardReadDiagnostic(error), { + catalogue: "/interface", attempt: 2, category: "http-refusal", httpStatus: 429, + }) + assert.equal((error as Error).message, "Не прочитан обязательный источник зависимостей WireGuard") + return true + }) + assert.deepEqual(paths, ["/rest/interface", "/rest/interface"]) +}) + +test("dependency diagnostics do not retain arbitrary paths or native error text", async () => { + const read = createWireguardDependencyReader({ + async get() { throw new Error("fixture-opaque-native-text") }, + }, async () => {}) + await assert.rejects(read("/fixture-private-path"), (error: unknown) => { + assert.equal(wireguardReadDiagnostic(error), undefined) + assert.doesNotMatch(String(error), /fixture/) + return true + }) +}) + +test("two distinct dependency resets can retry with fresh authority and the original read deadlines", async t => { + const { client, paths, timeouts } = fixture(t, ["reset", "ok", "reset", "ok", "reset"]) + let authorized = 0, clock = 0 + const read = createWireguardDependencyReader(client, async () => { if (++authorized === 2) clock += 120 }, () => clock) + assert.deepEqual(await read("/interface"), []) + assert.deepEqual(await read("/routing/ospf/neighbor"), []) + await assert.rejects(read("/ip/route"), { code: "STATE_UNAVAILABLE" }) + assert.equal(authorized, 5) + assert.deepEqual(paths, ["/rest/interface", "/rest/interface", "/rest/routing/ospf/neighbor", "/rest/routing/ospf/neighbor", "/rest/ip/route"]) + assert.deepEqual(timeouts, [10_000, 9_880, 10_000, 10_000, 10_000]) +}) + +test("the same catalogue cannot use the second retry slot", async t => { + const { client, paths } = fixture(t, ["reset", "reset", "reset"]) + const read = createWireguardDependencyReader(client, async () => {}) + await assert.rejects(read("/interface"), { code: "STATE_UNAVAILABLE" }) + await assert.rejects(read("/interface"), { code: "STATE_UNAVAILABLE" }) + assert.equal(paths.length, 3) +}) + +test("post-header reset, refusal, malformed body and unproven reset cannot replay", async t => { + const { client, paths } = fixture(t, ["late-reset", "informational-reset", "body-reset", "refused", "invalid-json"]) + const read = createWireguardDependencyReader(client, async () => {}) + for (let index = 0; index < 5; index++) await assert.rejects(read("/interface"), { code: "STATE_UNAVAILABLE" }) + assert.equal(paths.length, 5) + const synthetic = reset() + assert.equal(isRouterOsPreHeaderGetReset(synthetic), false) + let calls = 0 + const unknown = createWireguardDependencyReader({ async get() { calls++; throw synthetic } }, async () => {}) + await assert.rejects(unknown("/interface"), { code: "STATE_UNAVAILABLE" }) + assert.equal(calls, 1) +}) + +test("IPv4 and IPv6 reads share the dependency retry budget", async t => { + const { client, paths } = fixture(t, ["reset", "ok", "reset", "ok", "reset"]) + const read = createWireguardDependencyReader(client, async () => {}) + assert.deepEqual(await read("/ip/address"), []) + assert.deepEqual(await read("/ipv6/address"), []) + await assert.rejects(read("/interface"), { code: "STATE_UNAVAILABLE" }) + assert.deepEqual(paths, ["/rest/ip/address", "/rest/ip/address", "/rest/ipv6/address", "/rest/ipv6/address", "/rest/interface"]) +}) + +test("retry authority denial and deadline expiry stop before transport", async t => { + const { client, paths } = fixture(t, ["reset", "reset"]) + const denied = new Error("authority denied") + let authorized = 0 + const read = createWireguardDependencyReader(client, async () => { if (++authorized === 2) throw denied }) + await assert.rejects(read("/interface"), error => error === denied) + let clock = 0 + const expired = createWireguardDependencyReader(client, async () => { if (++authorized === 4) clock = 10_000 }, () => clock) + await assert.rejects(expired("/interface"), { code: "STATE_UNAVAILABLE" }) + assert.equal(authorized, 4) + assert.equal(paths.length, 2) +}) diff --git a/backend/src/services/wireguard-export.test.ts b/backend/src/services/wireguard-export.test.ts new file mode 100644 index 00000000..32d0f399 --- /dev/null +++ b/backend/src/services/wireguard-export.test.ts @@ -0,0 +1,46 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { renderWireguardExport, wireguardExportRequestSchema, type WireguardExportRows } from "./wireguard-export.js" + +const serverKey=Buffer.alloc(32,11).toString("base64"), publicKey=Buffer.alloc(32,12).toString("base64"), peerKey=Buffer.alloc(32,13).toString("base64"), peerPrivate=Buffer.alloc(32,14).toString("base64") +const fixture=():WireguardExportRows=>({interfaces:[{".id":"*wg9",name:"wg-test","listen-port":"13231",mtu:"1420",disabled:"false","private-key":serverKey,"public-key":publicKey}],bindings:[{".id":"*if3",name:"wg-test",type:"wg"}],peers:[{".id":"*p",interface:"*if3","public-key":peerKey,"private-key":peerPrivate,"allowed-address":"192.0.2.2/32","endpoint-address":"2001:db8::2","endpoint-port":"13231","persistent-keepalive":"0",disabled:"false","client-address":"192.0.2.2/32","client-dns":"1.1.1.1"}],addresses:[{".id":"*a",interface:"*if3",address:"192.0.2.1/32",disabled:"false",dynamic:"false"}]}) +const request=(format:"rsc"|"conf"|"peer-conf",includePrivateKey=false)=>wireguardExportRequestSchema.parse({serverId:"143",interfaceName:"wg-test",format,includePrivateKey}) +const render=(format:"rsc"|"conf"|"peer-conf",rows=fixture(),include=false)=>renderWireguardExport(request(format,include),rows,{name:"fixture",host:"192.0.2.9"}).content.toString() + +test("export preserves independently joined interface identities and respects private-key opt-in for both formats",()=>{ + for(const format of ["rsc","conf"] as const){const plain=render(format);assert.ok(!plain.includes(serverKey));assert.ok(!plain.includes(peerPrivate));assert.ok(plain.includes(peerKey));assert.ok(plain.includes("192.0.2.1/32"));const secret=render(format,fixture(),true);assert.ok(secret.includes(serverKey));assert.ok(!secret.includes(peerPrivate))} + assert.equal(request("rsc").serverId,143);assert.equal(wireguardExportRequestSchema.parse({serverId:143,interfaceName:"wg-test",format:"conf"}).includePrivateKey,false) + const missing=fixture();missing.bindings=[];assert.throws(()=>render("rsc",missing)) + const wrong=fixture();wrong.peers[0].interface="*wg9";assert.throws(()=>render("rsc",wrong),"WG catalog ID cannot substitute for the separately observed generic ID") +}) +test("peer-conf has client routing placeholders and never exposes server or generated client keys",()=>{ + const value=render("peer-conf",fixture(),true) + assert.ok(value.includes(publicKey));assert.ok(value.includes("# AllowedIPs ="));assert.ok(!value.includes(serverKey));assert.ok(!value.includes(peerPrivate));assert.ok(value.includes("Address = 192.0.2.2/32"));assert.ok(value.includes("Endpoint = 192.0.2.9:13231")) + assert.throws(()=>renderWireguardExport({...request("peer-conf"),peerId:"*missing"},fixture(),{name:"fixture",host:"192.0.2.9"})) + assert.deepEqual(renderWireguardExport({...request("peer-conf"),peerId:"143:*p"},fixture(),{name:"fixture",host:"192.0.2.9"}),renderWireguardExport({...request("peer-conf"),peerId:"*p"},fixture(),{name:"fixture",host:"192.0.2.9"})) +}) +test("disabled peer flags survive RSC and nonrepresentable conf/address/PSK state is rejected",()=>{ + const disabled=fixture();disabled.peers[0].disabled="true";assert.ok(render("rsc",disabled).includes("disabled=yes"));assert.throws(()=>render("conf",disabled)) + for(const mutate of [(rows:WireguardExportRows)=>{rows.addresses.push({...rows.addresses[0],".id":"*a2",address:"192.0.2.3/32"})},(rows:WireguardExportRows)=>{rows.addresses[0].dynamic="true"},(rows:WireguardExportRows)=>{rows.addresses[0].advertise="true"},(rows:WireguardExportRows)=>{rows.peers[0]["preshared-key"]=peerPrivate},(rows:WireguardExportRows)=>{delete rows.peers[0].disabled},(rows:WireguardExportRows)=>{rows.interfaces[0].mtu="unknown"}]){const rows=fixture();mutate(rows);assert.throws(()=>render("rsc",rows))} +}) +test("unexpected secret reflection and invalid unbounded request values fail without embedding keys",()=>{ + const rows=fixture();rows.peers[0].comment=peerPrivate + assert.throws(()=>render("conf",rows),(error:Error)=>!error.message.includes(peerPrivate)) + assert.equal(wireguardExportRequestSchema.safeParse({serverId:143,interfaceName:"wg\nunsafe",format:"rsc"}).success,false) + assert.equal(wireguardExportRequestSchema.safeParse({serverId:143,interfaceName:"wg",format:"rsc",arbitrary:"field"}).success,false) + assert.equal(wireguardExportRequestSchema.safeParse({serverId:true,interfaceName:"wg",format:"rsc"}).success,false) +}) +test("multiline comments remain representable and safe private-key modes are preserved",()=>{ + const rows=fixture();rows.interfaces[0].comment="line one\nline two";rows.peers[0].comment="peer line\nsecond line";rows.peers[0]["private-key"]="none" + const rsc=render("rsc",rows);assert.ok(rsc.includes("private-key=\"none\"")||rsc.includes("private-key=none"));assert.ok(!rsc.includes(serverKey));assert.ok(!rsc.includes(peerPrivate)) + assert.doesNotThrow(()=>render("conf",rows)) +}) + +test("native duration keepalives use the same observed codec as peer mutations",()=>{ + for(const value of ["25s","00:00:25","0s","1m5s"]){ + const rows=fixture();rows.peers[0]["persistent-keepalive"]=value + const seconds=value==="0s"?0:value==="1m5s"?65:25 + for(const format of ["rsc","conf","peer-conf"] as const){const result=render(format,rows);assert.ok(result.includes(format==="rsc"?`persistent-keepalive=${seconds}`:`PersistentKeepalive = ${seconds}`))} + } + for(const value of ["65536s","1.5s","unknown"]){const rows=fixture();rows.peers[0]["persistent-keepalive"]=value;assert.throws(()=>render("rsc",rows))} +}) diff --git a/backend/src/services/wireguard-export.ts b/backend/src/services/wireguard-export.ts new file mode 100644 index 00000000..6b575301 --- /dev/null +++ b/backend/src/services/wireguard-export.ts @@ -0,0 +1,118 @@ +import { z } from "zod" +import { isIP } from "node:net" +import { resolveWgClientEndpoint } from "@mmapp/contracts/wireguard" +import { pool } from "../db/index.js" +import { readRoutingEndpoints, createRoutingSourceContext } from "../modules/mcp/routing-observation-source.js" +import { routeContextClients } from "../modules/mcp/routing-community-context-native.js" +import { fingerprint, McpError, privateSecretFingerprint } from "../modules/mcp/security.js" +import { nativeNetworkRows, networkSecretValues, type NativeNetworkRow } from "./network-observed-read.js" +import { generateMikrotikRsc, generateNativeConf, generatePeerClientConf, type WgExportIface } from "./wireguard-config.js" +import { observedWireguardPeerKeepalive } from "./wireguard-peer-update.js" + +const name = z.string().min(1).max(200).refine(value => !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(value)) +export const wireguardExportRequestSchema = z.object({ serverId: z.union([z.number(),z.string().regex(/^[1-9]\d*$/).transform(Number)]).pipe(z.number().int().positive().safe()), interfaceName: name, format: z.enum(["rsc", "conf", "peer-conf"]), peerId: name.optional(), includePrivateKey: z.boolean().default(false) }).strict() +export type WireguardExportRequest = z.infer +export type WireguardExportRows = { interfaces: NativeNetworkRow[]; bindings: NativeNetworkRow[]; peers: NativeNetworkRow[]; addresses: NativeNetworkRow[] } +const fail = (message: string, code = "STATE_UNAVAILABLE") => new McpError(code, message, 409) +const text = (value: unknown, empty = true): string => { + if (typeof value !== "string" || value.length > 16384 || /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(value) || !empty && !value) throw fail("Свойство WireGuard не подтверждено") + return value +} +const comment = (value: unknown) => { if(typeof value!=="string" || value.length>16384 || /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/.test(value))throw fail("Комментарий WireGuard не подтверждён");return value } +const number = (value: unknown, min: number, max: number) => { if (typeof value !== "string" || !/^\d+$/.test(value) || Number(value) < min || Number(value) > max) throw fail("Числовое свойство WireGuard не подтверждено"); return Number(value) } +const flag = (value: unknown) => { if (["true", "yes", true].includes(value as string | boolean)) return true; if (["false", "no", false].includes(value as string | boolean)) return false; throw fail("Состояние WireGuard не подтверждено") } +const key = (value: unknown) => { const result = text(value, false); if (!/^[A-Za-z0-9+/]{43}=$/.test(result)) throw fail("Ключ WireGuard не подтверждён"); return result } + +/** Renders only explicit bounded native observations. Interface catalog IDs are + * independent: this never compares /interface IDs with /interface/wireguard. */ +export function renderWireguardExport(request: WireguardExportRequest, rows: WireguardExportRows, server: { name: string; host: string }) { + const selected = rows.interfaces.filter(row => row.name === request.interfaceName) + if (selected.length !== 1) throw fail("Интерфейс WireGuard отсутствует или неоднозначен", "NOT_FOUND") + const raw = selected[0], name = text(raw.name, false), port = number(raw["listen-port"], 1, 65535), mtu = number(raw.mtu, 68, 65535) + const bindings = rows.bindings.filter(row => row.name === name) + if (bindings.length !== 1 || !["wg", "wireguard"].includes(bindings[0].type ?? "")) throw fail("Идентичность интерфейса WireGuard не подтверждена") + const interfaceName = (ref: unknown) => { + const matching=rows.bindings.filter(row=>row.name===ref || row[".id"]===ref) + if(matching.length!==1)throw fail("Привязка peer/address к интерфейсу не подтверждена") + return matching[0].name + } + const peers = rows.peers.filter(peer => interfaceName(peer.interface) === name) + const addresses = rows.addresses.filter(row => interfaceName(row.interface) === name && !flag(row.dynamic)) + const mapped = peers.map(peer => { + const endpointAddress = text(peer["endpoint-address"] ?? ""), endpointPort = peer["endpoint-port"] === undefined || peer["endpoint-port"] === "" ? undefined : number(peer["endpoint-port"], 0, 65535) + if (endpointAddress && endpointPort === undefined) throw fail("Порт endpoint WireGuard не подтверждён") + const keepalive = peer["persistent-keepalive"] === undefined ? undefined : observedWireguardPeerKeepalive(peer["persistent-keepalive"]) + if (keepalive === null) throw fail("Интервал keepalive WireGuard не подтверждён") + return { id: peer[".id"]!, publicKey: key(peer["public-key"]), allowedIps: text(peer["allowed-address"] ?? "").split(",").filter(Boolean), + endpoint: endpointAddress ? (isIP(endpointAddress) === 6 ? "[" + endpointAddress + "]" : endpointAddress) + ":" + endpointPort : undefined, + persistentKeepalive: keepalive, disabled: flag(peer.disabled), privateKey: ["auto","none"].includes(peer["private-key"]??"")?peer["private-key"]:undefined, + comment: comment(peer.comment ?? ""), name: text(peer.name ?? ""), clientAddress: text(peer["client-address"] ?? ""), clientDns: text(peer["client-dns"] ?? ""), clientEndpoint: text(peer["client-endpoint"] ?? "") } + }) + let content: string + if (request.format === "peer-conf") { + const matching = request.peerId ? mapped.filter(peer => peer.id === request.peerId || `${request.serverId}:${peer.id}` === request.peerId) : mapped.slice(0, 1) + if (matching.length !== 1) throw fail("Пир WireGuard отсутствует или неоднозначен", "NOT_FOUND") + const peer = matching[0] + const sourcePeer=peers.find(row=>row[".id"]===peer.id)! + if(sourcePeer["preshared-key"] && sourcePeer["preshared-key"]!=="none")throw fail("Экспорт PSK требует отдельного явного контракта", "UNSUPPORTED_CAPABILITY") + content = generatePeerClientConf({ peerAddress: peer.clientAddress, peerDns: peer.clientDns, serverPublicKey: key(raw["public-key"]), endpoint: resolveWgClientEndpoint(peer.clientEndpoint, server.host, port), persistentKeepalive: peer.persistentKeepalive ?? 25 }) + } else { + if (addresses.length > 1 || addresses.some(row => flag(row.disabled) || [row.advertise, row["no-dad"], row["eui-64"]].some(value => value === "yes" || value === "true") || !!row["from-pool"])) throw fail("Экспорт не воспроизводит несколько адресов или специальные параметры", "STATE_CONFLICT") + const address = addresses[0]?.address + if (address) { const [ip, prefix, extra] = address.split("/"); if (!isIP(ip) || !/^\d+$/.test(prefix ?? "") || extra || Number(prefix) > (isIP(ip) === 4 ? 32 : 128)) throw fail("Адрес WireGuard не подтверждён") } + if (rows.addresses.some(row => interfaceName(row.interface) === name && flag(row.dynamic))) throw fail("Динамический адрес не воспроизводится экспортом", "STATE_CONFLICT") + if (request.format === "conf" && (flag(raw.disabled) || mapped.some(peer => peer.disabled))) throw fail("Формат conf не воспроизводит отключённое состояние", "STATE_CONFLICT") + if (peers.some(peer => peer["preshared-key"] && peer["preshared-key"] !== "none")) throw fail("Экспорт PSK требует отдельного явного контракта", "UNSUPPORTED_CAPABILITY") + const iface: WgExportIface = { name, listenPort: port, mtu, enabled: !flag(raw.disabled), comment: comment(raw.comment ?? ""), address, + privateKey: request.includePrivateKey ? key(raw["private-key"]) : undefined, publicKey: raw["public-key"], serverName: text(server.name), peers: mapped } + content = request.format === "rsc" ? generateMikrotikRsc(iface, { includePrivateKey: request.includePrivateKey }) : generateNativeConf(iface, { includePrivateKey: request.includePrivateKey }) + } + const permittedSecret = request.includePrivateKey && request.format !== "peer-conf" ? raw["private-key"] : undefined + if (networkSecretValues([...rows.interfaces,...rows.peers]).filter(secret=>!["auto","none"].includes(secret)).some(secret => secret !== permittedSecret && content.includes(secret))) throw fail("Экспорт содержит отражение закрытого ключа в другом поле", "STATE_UNAVAILABLE") + if (Buffer.byteLength(content) > 1048576) throw fail("Экспорт WireGuard превышает 1 МиБ", "EXPORT_LIMIT") + return { content: Buffer.from(content, "utf8"), filename: (name.replace(/[^a-zA-Z0-9._-]/g, "_").slice(0, 180) || "wireguard") + (request.format === "rsc" ? ".rsc" : request.format === "conf" ? ".conf" : "-peer.conf"), format: request.format } +} + +export async function readWireguardExport(raw: unknown, options: { authorize?: () => Promise; signal?: AbortSignal } = {}) { + const request = wireguardExportRequestSchema.parse(raw), bounded = options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(45000)]) : AbortSignal.timeout(45000) + const check = async () => { bounded.throwIfAborted(); await options.authorize?.(); bounded.throwIfAborted() } + await check() + const endpoints = await readRoutingEndpoints([request.serverId]) + if (endpoints.length !== 1 || !endpoints[0].enabled) throw fail("Источник WireGuard отсутствует или отключён", "DEVICE_UNAVAILABLE") + const sources = await createRoutingSourceContext(endpoints), client = routeContextClients(endpoints, bounded).get(request.serverId)! + const server = (await pool.query("SELECT CASE WHEN octet_length(name)<=1000 THEN name ELSE NULL END AS name FROM servers WHERE id=$1", [request.serverId])).rows[0] + if (!server || typeof server.name !== "string") throw fail("Источник WireGuard не подтверждён") + const fence = async () => { await check(); await sources.assertCurrent(); const fresh = (await pool.query("SELECT name FROM servers WHERE id=$1 AND octet_length(name)<=1000", [request.serverId])).rows[0]; if (fresh?.name !== server.name) throw fail("Источник WireGuard изменился", "STATE_CONFLICT"); await check() } + const snapshot = async () => { + const catalogs: NativeNetworkRow[][] = [] + let total = 0 + for (const path of ["/interface/wireguard", "/interface/wireguard/peers", "/ip/address", "/ipv6/address", "/interface"]) { + await fence() + let rows: NativeNetworkRow[] + try { rows = nativeNetworkRows(await client.get(path)) } catch (error) { if (error instanceof McpError) throw error; throw fail("Недостоверный каталог WireGuard", "INVALID_NATIVE_RESPONSE") } + if (rows.length > 1000 || (total += rows.length) > 4000) throw fail("Каталог WireGuard превышает лимит", "LIMIT_EXCEEDED") + for (const row of rows) if ((path === "/interface/wireguard" && row.name === request.interfaceName || row.interface === request.interfaceName)) { + const properties = path.includes("/address") ? ["disabled", "dynamic"] : path === "/interface/wireguard" ? ["disabled", "listen-port", "mtu"] : ["disabled"] + for (const property of properties) if (row[property] === undefined) { + await fence(); const value = await client.post(path + "/get", { ".id": row[".id"]!, "value-name": property }) + if (!value || typeof value !== "object" || !("ret" in value) || typeof value.ret !== "string") throw fail("Effective свойство WireGuard не подтверждено") + row[property] = value.ret + } + } + catalogs.push(rows.map(row => Object.fromEntries(Object.entries(row).filter(([property]) => !["rx", "tx", "last-handshake", "current-endpoint-address", "current-endpoint-port", "running", "invalid"].includes(property) && !(path==="/interface" && /^(?:rx-|tx-|fp-|last-link-|link-downs$|driver-)/.test(property)))))) + } + await fence() + const bindings=catalogs[4], selectedBinding=bindings.filter(row=>row.name===request.interfaceName) + if(selectedBinding.length!==1)throw fail("Привязка интерфейса WireGuard не подтверждена") + // Resolve ID references through the independent generic catalog. Do not + // compare a WireGuard row ID with an /interface row ID. + for(const [index,path] of [[1,"/interface/wireguard/peers"],[2,"/ip/address"],[3,"/ipv6/address"]] as const) for(const row of catalogs[index]) if(row.interface===selectedBinding[0][".id"]) { + for(const property of index===1?["disabled"]:["disabled","dynamic"]) if(row[property]===undefined){await fence();const value=await client.post(path+"/get",{".id":row[".id"]!,"value-name":property});if(!value||typeof value!=="object"||!("ret" in value)||typeof value.ret!=="string")throw fail("Effective свойство WireGuard не подтверждено");row[property]=value.ret} + } + return { interfaces: catalogs[0], bindings, peers: catalogs[1], addresses: [...catalogs[2], ...catalogs[3]] } + } + const before = await snapshot(), rendered = renderWireguardExport(request, before, { name: server.name, host: endpoints[0].host }), after = await snapshot() + if (fingerprint(before) !== fingerprint(after)) throw fail("WireGuard изменился во время экспорта", "STATE_CONFLICT") + await fence() + return { ...rendered, serverIds: [request.serverId], proof: await privateSecretFingerprint(fingerprint({ request, sources: sources.expected, server: { name: server.name, host: endpoints[0].host }, rows: after, content: rendered.content.toString("base64") }), "wireguard-export-source-v1") } +} diff --git a/backend/src/services/wireguard-import-execution.test.ts b/backend/src/services/wireguard-import-execution.test.ts new file mode 100644 index 00000000..278b23d1 --- /dev/null +++ b/backend/src/services/wireguard-import-execution.test.ts @@ -0,0 +1,99 @@ +import assert from "node:assert/strict" +import { fileURLToPath } from "node:url" +import { MikrotikError } from "./mikrotik.js" +import { wireguardInterfaceCreateSchema, wireguardPublicKey } from "./wireguard-interface-create.js" +import { wireguardInterfaceDeleteProof, wireguardInterfaceDependencyPaths, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { wireguardImportStages, type WireguardImportPlan } from "./wireguard-import-plan.js" +import { executeWireguardImportStages, WireguardImportExecutionError, type WireguardImportCheckpoint, type WireguardImportCommand } from "./wireguard-import-execution.js" + +export async function acceptWireguardImportExecutionModel() { + const key = (byte: number) => Buffer.alloc(32, byte).toString("base64") + const plan: WireguardImportPlan = { format: "rsc", interface: wireguardInterfaceCreateSchema.parse({ serverId: 141, name: "wg-import", privateKey: key(21), address: "2001:db8::1/64" }), peers: [ + { publicKey: key(22), allowedAddresses: ["192.0.2.2/32"], name: "first", privateKey: "auto", clientAddress: "10.0.0.2", clientDns: "1.1.1.1", clientEndpoint: "vpn.example.org", endpointAddress: "vpn.example.org", endpointPort: 54321, persistentKeepalive: 60, comment: "first peer" }, + { publicKey: key(23), allowedAddresses: ["2001:db8:1::/64"], name: "second", privateKey: "none", disabled: true }, + ] } + async function fixture(options: { loseAt?: number; rejectAt?: number; tamperAt?: number; driftAfterCheckpoint?: number; intentDriftAt?: number; clampOnEnable?: boolean; rotateOnEnable?: boolean; foreignClampOnEnable?: boolean; ifaceOpaqueOnEnable?: boolean } = {}) { + const state: WireguardInterfaceDeleteState = { base: { interfaces: [], peers: [], bindings: [], flow: { hostPublicKey: "", registered: false }, serverProof: "source", proof: "base" }, ipv4: [], ipv6: [], dependencies: Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])), filters: [], proof: "" } + state.dependencies["/ip/firewall/filter"].push({ ".id": "*f", comment: "foreign opaque", chain: "forward", action: "accept" }) + if (options.foreignClampOnEnable) { + state.base.interfaces.push({ ".id": "*ff", name: "wg-foreign", "private-key": key(25), "public-key": wireguardPublicKey(key(25)), disabled: "false" }) + state.dependencies["/interface"].push({ ".id": "*ff", name: "wg-foreign", type: "wg" }) + } + state.proof = await wireguardInterfaceDeleteProof(state) + const baseline = state.proof, checkpoints: WireguardImportCheckpoint[] = [], commands: WireguardImportCommand[] = [] + let number = 0, nativeDispatches = 0 + const hash = async () => { state.proof = await wireguardInterfaceDeleteProof(state) } + const read = async () => { await hash(); return structuredClone(state) } + const apply = async (command: WireguardImportCommand) => { + if (command.method === "PUT") { + const row = { ".id": "*" + (++number).toString(16), ...command.body, dynamic: "false", "opaque-native-default": "retained" } + if (command.path === "/interface/wireguard") { Object.assign(row, { "public-key": wireguardPublicKey(plan.interface.privateKey!) }); state.base.interfaces.push(row); state.dependencies["/interface"].push({ ".id": row[".id"], name: row.name, type: "wg" }) } + else if (command.path === "/interface/wireguard/peers") { if (row["private-key"] === "auto") row["private-key"] = key(24); state.base.peers.push(row) } + else (command.path === "/ipv6/address" ? state.ipv6 : state.ipv4).push(row) + } else { + const id = decodeURIComponent(command.path.split("/").at(-1)!) + const row = [...state.base.interfaces, ...state.base.peers, ...state.ipv4, ...state.ipv6].find(row => row[".id"] === id)! + Object.assign(row, command.body) + if (command.path.startsWith("/interface/wireguard/") && !command.path.startsWith("/interface/wireguard/peers/") && command.body.disabled === "false") { + if (options.clampOnEnable || options.rotateOnEnable) { + const scalar = Buffer.from(row["private-key"], "base64") + scalar[0] &= 248; scalar[31] = (scalar[31] & 127) | 64 + if (options.rotateOnEnable) scalar[1] ^= 8 + row["private-key"] = scalar.toString("base64"); row["public-key"] = wireguardPublicKey(row["private-key"]) + } + if (options.foreignClampOnEnable) { + const foreign = state.base.interfaces.find(row => row.name === "wg-foreign")!, scalar = Buffer.from(foreign["private-key"], "base64") + scalar[0] &= 248; scalar[31] = (scalar[31] & 127) | 64 + foreign["private-key"] = scalar.toString("base64") + } + if (options.ifaceOpaqueOnEnable) row["opaque-native-default"] = "changed during own activation" + } + } + await hash() + } + const run = () => executeWireguardImportStages(plan, baseline, { authorize: async () => {}, read, + dispatch: async (command, fence) => { + const index = commands.length + 1 + if (options.intentDriftAt === index) { state.dependencies["/ip/firewall/filter"][0].comment = "changed after intent"; await hash() } + await fence(); commands.push(structuredClone(command)); nativeDispatches++ + if (options.rejectAt === index) throw new MikrotikError(422, command.path, "fixture rejection") + await apply(command) + if (options.loseAt === index) throw Error("fixture response lost") + if (options.tamperAt === index) { state.base.peers[0]["opaque-native-default"] = "changed"; await hash() } + }, + checkpoint: async checkpoint => { + checkpoints.push(structuredClone(checkpoint)) + if (options.driftAfterCheckpoint === checkpoint.completedCommands) { state.dependencies["/ip/firewall/filter"][0].comment = "foreign changed"; await hash() } + }, + }) + return { state, run, checkpoints, commands, nativeDispatches: () => nativeDispatches } + } + const successful = await fixture(), receipt = await successful.run() + assert.equal(receipt.verified, true); assert.equal(receipt.peersCreated, 2) + assert.equal(receipt.commandCount, wireguardImportStages(plan).length) + assert.equal(receipt.completedCommands, 7); assert.equal(successful.checkpoints.length, 7) + assert.equal(successful.state.base.peers[0].disabled, "false"); assert.equal(successful.state.base.peers[1].disabled, "true") + assert.deepEqual(successful.commands.slice(0, 4).map(command => command.body.disabled), ["true", "true", "true", "true"]) + assert.equal(successful.state.base.interfaces[0].disabled, "false") + assert.equal(successful.state.ipv6[0].disabled, "false") + const clamped = await fixture({ clampOnEnable: true }), clampedReceipt = await clamped.run() + assert.equal(clampedReceipt.verified, true); assert.equal(clampedReceipt.completedCommands, 7) + assert.notEqual(clamped.state.base.interfaces[0]["private-key"], plan.interface.privateKey) + assert.equal(clamped.state.base.interfaces[0]["public-key"], wireguardPublicKey(plan.interface.privateKey!)) + for (const [options, prefix, expectedDispatches, outcome] of [ + [{ loseAt: 4 }, 3, 4, "needs-review"], [{ rejectAt: 4 }, 3, 4, "partial"], + [{ tamperAt: 5 }, 4, 5, "needs-review"], [{ driftAfterCheckpoint: 3 }, 3, 3, "needs-review"], + [{ intentDriftAt: 4 }, 3, 3, "needs-review"], + [{ rotateOnEnable: true }, 6, 7, "needs-review"], [{ foreignClampOnEnable: true }, 6, 7, "needs-review"], + [{ clampOnEnable: true, ifaceOpaqueOnEnable: true }, 6, 7, "needs-review"], + ] as const) { + const test = await fixture(options) + await assert.rejects(test.run(), error => error instanceof WireguardImportExecutionError && error.state === outcome && error.verifiedPrefixCommands === prefix && error.commandCount === 7) + assert.equal(test.nativeDispatches(), expectedDispatches) + assert.equal(test.checkpoints.length, prefix) + assert.equal(test.commands.every(command => command.method !== ("DELETE" as string)), true, "No automatic compensation or replay") + } + console.log("WireGuard multi-peer import: disabled preparation, selective activation, full graph/opaque preservation, per-command checkpoints, durable-intent drift, explicit rejection prefix and lost response no replay/cleanup") + console.log("WireGuard import activation: corroborated X25519 scalar clamp succeeds only for selected own interface; meaningful rotation, foreign clamp and own opaque drift remain unknown") +} +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) await acceptWireguardImportExecutionModel() diff --git a/backend/src/services/wireguard-import-execution.ts b/backend/src/services/wireguard-import-execution.ts new file mode 100644 index 00000000..4f3ccc92 --- /dev/null +++ b/backend/src/services/wireguard-import-execution.ts @@ -0,0 +1,184 @@ +import { isIP } from "node:net" +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { readWireguardInterfaceCreateState, wireguardCreatePrefix, wireguardCreateKey } from "./wireguard-interface-create.js" +import { wireguardConfigurationRows } from "./wireguard-peer-delete.js" +import type { WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { observedWireguardPeerKeepalive } from "./wireguard-peer-update.js" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { greStableRows } from "./gre-mutation-state.js" +import { interfaceAddressPath } from "./entity-snapshots.js" +import { toRosBody } from "./wireguard-ros.js" +import { MikrotikClient, MikrotikError } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { wireguardCorroboratedPrivateScalar, wireguardPrivateScalarsEqual } from "./wireguard-key-scalar.js" +import { assertWireguardImportAllowed, wireguardImportPeerBody, wireguardImportStages, wireguardImportObjects, type WireguardImportPlan, type WireguardImportStage } from "./wireguard-import-plan.js" + +type State = WireguardInterfaceDeleteState +type Row = Record +export interface WireguardImportCheckpoint { completedCommands: number; commandCount: number; afterProof: string; interfaceId: string; peerIds: string[] } +export interface WireguardImportCommand { method: "PUT" | "PATCH"; path: string; body: Row } +export async function wireguardImportProtectionProof(serverId: number, objects: { kind: string; id: string }[]) { + const rows = (await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) OR EXISTS(SELECT 1 FROM jsonb_array_elements($2::jsonb) target WHERE target->>'kind'=object_ref->>'kind' AND target->>'id'=object_ref->>'id') ORDER BY id LIMIT 1001", [String(serverId), JSON.stringify(objects)])).rows + if (rows.length > 1000) throw new McpError("STATE_UNAVAILABLE", "Превышен предел защит импорта WireGuard", 409) + return fingerprint(rows) +} +export class WireguardImportExecutionError extends McpError { + constructor(public readonly state: "conflict" | "partial" | "needs-review", public readonly verifiedPrefixCommands: number, public readonly commandCount: number, public readonly lastVerifiedProof: string) { + super(state === "conflict" ? "STATE_CONFLICT" : state === "partial" ? "VERIFICATION_FAILED" : "MUTATION_OUTCOME_UNKNOWN", state === "conflict" ? "Исходное состояние WireGuard изменилось; импорт не начат" : state === "partial" ? "RouterOS отклонил этап импорта; подтверждённый префикс сохранён. Очистка требует отдельного решения" : "Исход этапа импорта неизвестен; повтор и автоматическая очистка запрещены до сверки", state === "conflict" ? 409 : 502) + } +} +function matches(row: Row, body: Row) { + return Object.entries(body).every(([key, value]) => { + if (key === "disabled") return observedBoolean(row[key]) === observedBoolean(value) + if (key === "endpoint-port") return /^\d+$/.test(row[key] ?? "") && Number(row[key]) === Number(value) + if (key === "persistent-keepalive") return observedWireguardPeerKeepalive(row[key] ?? "") === Number(value) + if (key === "allowed-address") { try { return fingerprint(row[key].split(",").map(canonicalBgpPolicyPrefix).sort()) === fingerprint(value.split(",").map(canonicalBgpPolicyPrefix).sort()) } catch { return false } } + if (key === "private-key" && value === "auto") return wireguardCreateKey.safeParse(row[key]).success + if (key === "private-key" && value === "none") return row[key] === "none" || row[key] === "" + return (row[key] ?? "") === value + }) +} +function selected(state: State, plan: WireguardImportPlan) { + const target = state.base.interfaces.find(row => row.name === plan.interface.name) + const belongs = (row: Row) => !!target && [target.name, target[".id"]].includes(row.interface) + return { target, belongs } +} +/** A complete prefix removes precisely this import's additions, then reuses the + * existing interface graph proof for every original/foreign/opaque property. */ +export async function wireguardImportPrefix(state: State, plan: WireguardImportPlan, baseline: string, count: number): Promise { + const stages = wireguardImportStages(plan) + if (!Number.isInteger(count) || count < 0 || count > stages.length) return false + if (!count) return state.proof === baseline + const done = stages.slice(0, count), { target, belongs } = selected(state, plan) + if (!target) return false + const created = done.filter(stage => stage.kind === "peer").map(stage => stage.peerIndex!) + const peers = state.base.peers.filter(belongs) + if (peers.length !== created.length) return false + for (const index of created) { + const rows = peers.filter(row => row["public-key"] === plan.peers[index].publicKey) + if (rows.length !== 1 || ["dynamic", "builtin", "default"].some(key => rows[0][key] !== undefined && observedBoolean(rows[0][key]) !== false)) return false + const body = wireguardImportPeerBody(plan, index) + body.interface = rows[0].interface + body.disabled = String(!done.some(stage => stage.kind === "peer-enable" && stage.peerIndex === index)) + if (!matches(rows[0], body)) return false + } + const withoutPeers = structuredClone(state) + withoutPeers.base.peers = withoutPeers.base.peers.filter(row => !belongs(row)) + const interfaceCount = 1 + Number(done.some(stage => stage.kind === "address")) + Number(done.some(stage => stage.kind === "address-enable")) + Number(done.some(stage => stage.kind === "interface-enable")) + return wireguardCreatePrefix(withoutPeers, plan.interface, baseline, interfaceCount) +} +/** Native defaults and generated secrets become immutable after first readback. */ +export function wireguardImportPreserved(before: State, after: State, plan: WireguardImportPlan, stage: WireguardImportStage): boolean { + const { target, belongs } = selected(before, plan) + if (!target) return stage.kind === "interface" + const enablingPeer = stage.kind === "peer-enable" ? plan.peers[stage.peerIndex!].publicKey : undefined + for (const [oldRows, newRows, change, ownedInterfaceActivation] of [ + [before.base.interfaces.filter(row => row[".id"] === target[".id"]), after.base.interfaces, (_row: Row) => stage.kind === "interface-enable", stage.kind === "interface-enable"], + [before.base.peers.filter(belongs), after.base.peers, (row: Row) => stage.kind === "peer-enable" && row["public-key"] === enablingPeer, false], + [before.ipv4.filter(row => belongs(row) && observedBoolean(row.dynamic) === false), after.ipv4, (_row: Row) => stage.kind === "address-enable", false], + [before.ipv6.filter(row => belongs(row) && observedBoolean(row.dynamic) === false), after.ipv6, (_row: Row) => stage.kind === "address-enable", false], + ] as const) for (const old of oldRows) { + const row = newRows.find(value => value[".id"] === old[".id"]) + if (!row) return false + const stable = (value: Row) => fingerprint(greStableRows(wireguardConfigurationRows([{ ...value, disabled: String(observedBoolean(value.disabled)) }]))) + const expected: Row = { ...old, ...(change(old) ? { disabled: "false" } : {}) }, observed: Row = { ...row } + if (ownedInterfaceActivation && old["private-key"] !== row["private-key"]) { + // RouterOS clamps ignored X25519 scalar bits on activation. Accept only + // two valid secrets corroborated by the same observed public key, and + // only for this import's selected interface during its activation stage. + if (!wireguardPrivateScalarsEqual(old["private-key"], row["private-key"], old["public-key"])) return false + const canonical = wireguardCorroboratedPrivateScalar(old["private-key"], old["public-key"])! + expected["private-key"] = canonical; observed["private-key"] = canonical + } + if (stable(expected) !== stable(observed)) return false + } + return true +} +export function wireguardImportCommand(plan: WireguardImportPlan, stage: WireguardImportStage, state: State): WireguardImportCommand { + const input = plan.interface, { target, belongs } = selected(state, plan) + if (stage.kind === "interface") return { method: "PUT", path: "/interface/wireguard", body: toRosBody({ name: input.name, "listen-port": String(input.listenPort), mtu: String(input.mtu), comment: input.comment, "private-key": input.privateKey, disabled: "true" }) } + if (stage.kind === "address") return { method: "PUT", path: interfaceAddressPath(input.address!), body: { address: input.address!, interface: input.name, disabled: "true", ...(isIP(input.address!.split("/")[0]) === 6 ? { advertise: "false" } : {}) } } + if (stage.kind === "peer") return { method: "PUT", path: "/interface/wireguard/peers", body: wireguardImportPeerBody(plan, stage.peerIndex!) } + const row = stage.kind === "interface-enable" ? target : stage.kind === "peer-enable" ? state.base.peers.find(row => belongs(row) && row["public-key"] === plan.peers[stage.peerIndex!].publicKey) : [...state.ipv4, ...state.ipv6].find(row => belongs(row) && observedBoolean(row.dynamic) === false) + if (!row) throw new McpError("STATE_CONFLICT", "Не найден подготовленный объект WireGuard", 409) + const path = stage.kind === "interface-enable" ? "/interface/wireguard" : stage.kind === "peer-enable" ? "/interface/wireguard/peers" : interfaceAddressPath(input.address!) + return { method: "PATCH", path: path + "/" + encodeURIComponent(row[".id"]), body: { disabled: "false" } } +} +export async function executeWireguardImportStages(plan: WireguardImportPlan, baseline: string, deps: { + authorize: () => Promise; read: () => Promise + dispatch: (command: WireguardImportCommand, fence: () => Promise) => Promise + checkpoint: (checkpoint: WireguardImportCheckpoint) => Promise +}) { + const stages = wireguardImportStages(plan) + await deps.authorize() + let state = await deps.read(), completed = 0 + if (state.proof !== baseline) throw new WireguardImportExecutionError("conflict", 0, stages.length, baseline) + assertWireguardImportAllowed(state, plan) + for (const stage of stages) { + let dispatched = false + try { + await deps.authorize() + const fresh = await deps.read() + if (fresh.proof !== state.proof) throw new WireguardImportExecutionError(completed ? "needs-review" : "conflict", completed, stages.length, state.proof) + const fence = async () => { + await deps.authorize() + if ((await deps.read()).proof !== state.proof) throw new McpError("STATE_CONFLICT", "WireGuard изменился после durable intent; команда не отправлена", 409) + await deps.authorize() + dispatched = true + } + await deps.dispatch(wireguardImportCommand(plan, stage, fresh), fence) + const after = await deps.read() + if (!await wireguardImportPrefix(after, plan, baseline, completed + 1) || !wireguardImportPreserved(state, after, plan, stage)) throw Error("unverified prefix") + await deps.authorize() + const { target, belongs } = selected(after, plan) + await deps.checkpoint({ completedCommands: completed + 1, commandCount: stages.length, afterProof: after.proof, interfaceId: target![".id"], peerIds: after.base.peers.filter(belongs).map(row => row[".id"]) }) + completed++; state = after + } catch (error) { + if (!dispatched && error instanceof McpError && !completed) throw error + if (dispatched && error instanceof MikrotikError && [400, 401, 403, 404, 405, 409, 422].includes(error.statusCode)) { + try { await deps.authorize(); if ((await deps.read()).proof === state.proof) throw new WireguardImportExecutionError("partial", completed, stages.length, state.proof) } + catch (confirmed) { if (confirmed instanceof WireguardImportExecutionError) throw confirmed } + } + throw new WireguardImportExecutionError("needs-review", completed, stages.length, state.proof) + } + } + const { target, belongs } = selected(state, plan) + return { ok: true as const, verified: true as const, interfaceId: target![".id"], peerIds: state.base.peers.filter(belongs).map(row => row[".id"]), peersCreated: plan.peers.length, commandCount: stages.length, completedCommands: completed, afterProof: state.proof, forwardingVerified: false as const, automaticReplay: false as const } +} +export async function executeWireguardImport(server: ServerRow, plan: WireguardImportPlan, expectedProof: string, authorize: () => Promise = async () => {}, options: { + recordRevision?: boolean; checkpoint?: (checkpoint: WireguardImportCheckpoint) => Promise; receiptId?: string +} = {}) { + if (plan.interface.serverId !== server.id) throw new McpError("INVALID_INPUT", "Сервер импорта не совпал", 400) + const objects = wireguardImportObjects(plan, options.receiptId ?? randomUUID()) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], async () => { + await authorize() + // Capture before the shared durable application intent, including REST's + // default context. A newly protected logical child cannot become baseline. + const protectionProof = await wireguardImportProtectionProof(server.id, objects) + return executeGuardedApplicationWrite({ kind: "wireguard-import", id: "1" }, [server.id], async () => { + const source = async () => { + await authorize() + const fresh = await getServerRowById(server.id) + if (!fresh || !sameWireguardEndpoint(fresh, server) || await wireguardImportProtectionProof(server.id, objects) !== protectionProof) throw new McpError("STATE_CONFLICT", "Источник или защиты WireGuard изменились", 409) + } + const client = MikrotikClient.fromServer(server) + const receipt = await executeWireguardImportStages(plan, expectedProof, { authorize: source, read: () => readWireguardInterfaceCreateState(server, source), + dispatch: (command, fence) => command.method === "PUT" ? client.put(command.path, command.body, 15000, fence) : client.patch(command.path, command.body, 15000, fence), + checkpoint: options.checkpoint ?? (async () => {}), + }) + let revisionRecorded = false + try { if (options.recordRevision !== false) revisionRecorded = await captureAndAppendRevision({ serverId: server.id, section: "wireguard", source: "copy", capture: () => captureWireguardSnapshot(server, source) }) !== null } catch { /* Never replay native import to repair revision storage. */ } + await source() + return { ...receipt, revisionRecorded } + }, async () => objects) + })) +} diff --git a/backend/src/services/wireguard-import-plan.test.ts b/backend/src/services/wireguard-import-plan.test.ts new file mode 100644 index 00000000..f25fda72 --- /dev/null +++ b/backend/src/services/wireguard-import-plan.test.ts @@ -0,0 +1,44 @@ +import assert from "node:assert/strict" +import { parseWireguardImportPlan, wireguardImportPreview, wireguardImportStages, wireguardImportObjects, WIREGUARD_IMPORT_MAX_BYTES } from "./wireguard-import-plan.js" +import { mcpPolicyPreviewRequestSchema } from "@mmapp/contracts/mcp" +import { evaluateMcpPolicy } from "../modules/mcp/policy.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" + +const key = (byte: number) => Buffer.alloc(32, byte).toString("base64") +const content = `[Interface]\nName = wg-import\nPrivateKey = ${key(11)}\nAddress = 2001:db8::1/64\nListenPort = 54321\nMTU = 1300\n[Peer]\nPublicKey = ${key(12)}\nAllowedIPs = 192.0.2.0/24,2001:db8:1::/64\nEndpoint = [2001:db8::2]:13231\nPersistentKeepalive = 25\n[Peer]\nPublicKey = ${key(13)}\nAllowedIPs = 198.51.100.3/32\n` +const plan = parseWireguardImportPlan(content, "auto", 141) +assert.equal(plan.peers.length, 2) +assert.equal(plan.interface.address, "2001:db8::1/64") +assert.equal(plan.peers[0].endpointAddress, "2001:db8::2") +assert.equal(plan.peers[0].endpointPort, 13231) +assert.equal(plan.interface.privateKey, key(11)) +assert.equal(wireguardImportStages(plan).length, 8) +const preview = JSON.stringify(wireguardImportPreview(plan)) +for (const secret of [key(11), key(12), key(13)]) assert.equal(preview.includes(secret), false) +plan.interface.comment = key(11) +plan.peers[0].comment = `${key(12)} ${key(13)}` +for (const secret of [key(11), key(12), key(13)]) assert.equal(JSON.stringify(wireguardImportPreview(plan)).includes(secret), false, "Even imported names/comments cannot echo keys") +for (const bad of [content.replace(key(13), key(12)), content + "\n[Peer]\nPublicKey=bad\nAllowedIPs=0.0.0.0/0", "x".repeat(WIREGUARD_IMPORT_MAX_BYTES + 1)]) assert.throws(() => parseWireguardImportPlan(bad, "auto", 141)) +const rsc = `/interface wireguard add name=wg-rsc private-key="${key(11)}" disabled=yes\n/ipv6 address add interface=wg-rsc address=2001:db8::1/64 advertise=no\n/interface wireguard peers add interface=wg-rsc public-key="${key(12)}" allowed-address=192.0.2.0/24 private-key=none name=first client-address=10.0.0.2 client-dns=1.1.1.1 client-endpoint=vpn.example.org disabled=yes\n/interface wireguard peers add interface=wg-rsc public-key="${key(13)}" allowed-address=2001:db8:1::/64 private-key=auto persistent-keepalive=30 comment=second` +const native = parseWireguardImportPlan(rsc, "rsc", 142) +assert.equal(native.peers.length, 2) +assert.equal(native.peers[0].disabled, true) +assert.equal(native.peers[0].clientDns, "1.1.1.1") +assert.equal(native.peers[0].privateKey, "none") +assert.equal(native.peers[1].privateKey, "auto") +assert.deepEqual(wireguardImportStages(native).map(stage => stage.kind), ["interface", "address", "peer", "peer", "peer-enable", "address-enable"]) +const objects = wireguardImportObjects(native, "d31deace-6412-4b5f-bd87-2d3af9ea51f9") +assert.equal(objects.length, 6) +assert.equal(new Set(objects.map(object => object.kind + ":" + object.id)).size, 6) +assert.equal(objects.filter(object => object.kind === "wireguard_peer").length, 2) +for (const secret of [key(11), key(12), key(13)]) assert.equal(JSON.stringify(objects).includes(secret), false) +const now = new Date(), lowBudget = mcpPolicyPreviewRequestSchema.parse({ operationId: "routing.ospf.optimize", policy: { id: "import-budget", version: 1, status: "active", expiresAt: new Date(now.getTime() + 60000).toISOString(), mode: "approval", permissions: ["mm:network:write", "mm:servers:read"], scope: { serverIds: [142], operationIds: ["routing.ospf.optimize"] }, limits: { objectsPerPlan: 2 } }, ownerPermissions: ["mm:network:write", "mm:servers:read"], target: { scope: "servers", serverIds: [142] }, objects, objectCount: 1 }) +assert.ok(evaluateMcpPolicy(lowBudget, now).reasons.some(reason => reason.code === "PLAN_LIMIT_EXCEEDED"), "Logical object count independently enforces the object budget") +const endpoint = { id: 142, enabled: true, host: "127.0.0.1", port: 8000, username: "fixture", password: "fixture", useSsl: false, verifySsl: false, routingEndpointEpoch: "d31deace-6412-4b5f-bd87-2d3af9ea51f9" } as ServerRow +assert.equal(sameWireguardEndpoint({ ...endpoint, name: "metadata-only", updatedAt: "later" }, endpoint), true) +assert.equal(sameWireguardEndpoint({ ...endpoint, routingEndpointEpoch: "fa217219-bc44-4194-b776-dda1ff032ffd" }, endpoint), false) +const multiline = parseWireguardImportPlan(rsc.replace("disabled=yes\n", 'disabled=yes comment="first\\nsecond\\tthird"\n').replace("comment=second", 'comment="peer\\r\\ncomment"'), "rsc", 142) +assert.equal(multiline.interface.comment, "first\nsecond\tthird") +assert.equal(multiline.peers[1].comment, "peer\r\ncomment") +console.log("WireGuard import plan: CONF/IPv6 and RSC multiple peers, all peer fields, mixed disabled activation, bounded validation and key-free preview") diff --git a/backend/src/services/wireguard-import-plan.ts b/backend/src/services/wireguard-import-plan.ts new file mode 100644 index 00000000..edf55a4e --- /dev/null +++ b/backend/src/services/wireguard-import-plan.ts @@ -0,0 +1,74 @@ +import { parseWireguardImport } from "./wireguard-import.js" +import { WG_CONFIG_LIMITS } from "./wireguard-config.js" +import { z } from "zod" +import { wireguardInterfaceCreateSchema, wireguardCreatePeerBody, assertWireguardCreateAllowed, wireguardCreateKey, type WireguardInterfaceCreate } from "./wireguard-interface-create.js" +import { wireguardPeerCreateSchema } from "./wireguard-peer-create.js" +import type { WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { McpError, redact } from "../modules/mcp/security.js" + +export const WIREGUARD_IMPORT_MAX_BYTES = WG_CONFIG_LIMITS.bytes +export const WIREGUARD_IMPORT_MAX_PEERS = WG_CONFIG_LIMITS.peers +const comment = z.string().max(1000).refine(value => !/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(value)).optional() +const importInterfaceSchema = wireguardInterfaceCreateSchema.extend({ comment }) +const importPeerSchema = wireguardPeerCreateSchema.extend({ comment }) +export interface WireguardImportPlan { + format: "rsc" | "conf" + interface: WireguardInterfaceCreate + peers: NonNullable[] +} +export type WireguardImportStage = { kind: "interface" | "address" | "peer" | "peer-enable" | "address-enable" | "interface-enable"; peerIndex?: number } +const invalid = (message: string) => new McpError("INVALID_INPUT", message, 400) + +/** Raw content is accepted only by the shared human REST boundary. MCP resolves + * its owner/purpose/server content reference before calling this function. */ +export function parseWireguardImportPlan(content: string, format: "auto" | "rsc" | "conf", serverId: number): WireguardImportPlan { + if (typeof content !== "string" || Buffer.byteLength(content) > WIREGUARD_IMPORT_MAX_BYTES) throw invalid("Конфигурация WireGuard превышает 256 КиБ") + let parsed + try { parsed = parseWireguardImport(content, format) } catch { throw invalid("Конфигурация WireGuard не поддерживается или содержит некорректные параметры") } + if (parsed.peers.length > WIREGUARD_IMPORT_MAX_PEERS) throw invalid("Импорт WireGuard превышает 100 пиров") + const iface = importInterfaceSchema.safeParse({ serverId, ...parsed.interface }) + if (!iface.success) throw invalid("Некорректные параметры интерфейса WireGuard") + const peers = parsed.peers.map(peer => { + const result = importPeerSchema.safeParse({ serverId, interfaceName: iface.data.name, ...peer }) + if (!result.success) throw invalid("Некорректные параметры пира WireGuard") + const { serverId: _server, interfaceName: _name, ...fields } = result.data + return fields + }) + if (new Set(peers.map(peer => peer.publicKey)).size !== peers.length || new Set(peers.filter(peer => peer.name).map(peer => peer.name)).size !== peers.filter(peer => peer.name).length) throw invalid("Импорт содержит повторяющиеся ключи или имена пиров") + return { format: parsed.format, interface: iface.data, peers } +} +export function wireguardImportStages(plan: WireguardImportPlan): WireguardImportStage[] { + return [{ kind: "interface" }, ...(plan.interface.address ? [{ kind: "address" as const }] : []), + ...plan.peers.map((_, peerIndex) => ({ kind: "peer" as const, peerIndex })), + ...plan.peers.flatMap((peer, peerIndex) => peer.disabled ? [] : [{ kind: "peer-enable" as const, peerIndex }]), + ...(plan.interface.address ? [{ kind: "address-enable" as const }] : []), ...(plan.interface.disabled ? [] : [{ kind: "interface-enable" as const }])] +} +/** Every logical object consumes the plan's object budget. Opaque IDs bind + * protections to the sealed receipt without exposing imported names or keys. */ +export function wireguardImportObjects(plan: WireguardImportPlan, receiptId: string) { + if (!z.string().uuid().safeParse(receiptId).success) throw invalid("Нет идентификатора плана импорта") + const serverId = plan.interface.serverId + return [{ kind: "server", id: String(serverId), serverId }, { kind: "wireguard-import", id: "1", serverId }, + { kind: "wireguard", id: `${receiptId}:interface`, serverId }, + ...(plan.interface.address ? [{ kind: "interface-address", id: `${receiptId}:address`, serverId }] : []), + ...plan.peers.map((_, index) => ({ kind: "wireguard_peer", id: `${receiptId}:peer:${index}`, serverId }))] +} +export function assertWireguardImportAllowed(state: WireguardInterfaceDeleteState, plan: WireguardImportPlan): void { + assertWireguardCreateAllowed(state, plan.interface) + for (const peer of plan.peers) assertWireguardCreateAllowed(state, { ...plan.interface, peer }) +} +export function wireguardImportPeerBody(plan: WireguardImportPlan, index: number) { + const peer = plan.peers[index] + if (!peer) throw invalid("Не найден пир этапа WireGuard") + return wireguardCreatePeerBody({ ...plan.interface, peer }) +} +/** Preview never contains key material, including public keys or literal keys + * echoed into names/comments by the imported document. */ +export function wireguardImportPreview(plan: WireguardImportPlan) { + const keys = [plan.interface.privateKey, ...plan.peers.flatMap(peer => [peer.publicKey, peer.privateKey])].filter((value): value is string => typeof value === "string" && wireguardCreateKey.safeParse(value).success) + const scrub = (value: unknown): unknown => typeof value === "string" ? keys.reduce((text, key) => text.split(key).join("[KEY]"), value) : Array.isArray(value) ? value.map(scrub) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).map(([key, field]) => [key, scrub(field)])) : value + const { privateKey, peer: _peer, serverId: _serverId, ...iface } = plan.interface + return scrub(redact({ format: plan.format, interface: { ...iface, privateKeyConfigured: Boolean(privateKey) }, + peers: plan.peers.map(({ publicKey: _public, privateKey: key, ...peer }, index) => ({ ...peer, index, publicKeyConfigured: true, privateKeyConfigured: Boolean(key), keyMode: key === "auto" || key === "none" ? key : key ? "stored" : "omitted" })), + peerCount: plan.peers.length, commandCount: wireguardImportStages(plan).length, stagedDisabled: true, automaticCompensation: false, automaticReplay: false })) +} diff --git a/backend/src/services/wireguard-import.test.ts b/backend/src/services/wireguard-import.test.ts index 5a6c24bf..5ad55660 100644 --- a/backend/src/services/wireguard-import.test.ts +++ b/backend/src/services/wireguard-import.test.ts @@ -1,7 +1,7 @@ import assert from "node:assert/strict" import { test } from "node:test" import { parseWireguardImport } from "./wireguard-import.js" -import { generatePeerClientConf } from "./wireguard-config.js" +import { generatePeerClientConf, parseWgConfig, WG_CONFIG_LIMITS } from "./wireguard-config.js" const key = Buffer.alloc(32, 7).toString("base64") const conf = `[Interface]\nPrivateKey = ${key}\nAddress = 198.18.255.241/30\nListenPort = 51879\n[Peer]\nPublicKey = ${key}\nAllowedIPs = 198.18.255.242/32\nEndpoint = 192.0.2.1:51879\n` @@ -49,3 +49,48 @@ test("RSC peers without routes or interface are rejected without default routes" assert.throws(() => parseWireguardImport("/interface wireguard add name=own-wg mtu=0", "rsc"), /MTU/) assert.deepEqual(parseWireguardImport(`/interface wireguard add name=own-wg\n${peer} allowed-address=192.0.2.0/24`, "rsc").peers[0]?.allowedAddresses, ["192.0.2.0/24"]) }) + +test("unknown conf semantics, duplicate fields and sections are rejected without content disclosure", () => { + for (const source of [ + conf + "[Unknown]\nNEVER_SECRET = x", + conf.replace("[Peer]", "Table = off\n[Peer]"), + conf.replace("[Peer]", "DNS = 1.1.1.1\n[Peer]"), + conf.replace("[Peer]", "PostUp = NEVER_SECRET\n[Peer]"), + conf.replace("[Peer]", "PrivateKey = NEVER_SECRET\n[Peer]"), + conf + "PublicKey = NEVER_SECRET", + conf + "Unknown = NEVER_SECRET", + conf + "[Peer]\nAllowedIPs = 192.0.2.2/32", + conf.replace("Address = 198.18.255.241/30", "Address = 198.18.255.241/30\nAddress = 192.0.2.1/24"), + ]) assert.throws(() => parseWireguardImport(source, "auto"), (error: Error) => !error.message.includes("NEVER_SECRET")) +}) + +test("RSC binds peers to the single interface and preserves supported private-key modes and booleans", () => { + const base = `/interface wireguard add name=own-wg disabled=false\n/interface wireguard peers add interface=own-wg public-key="${key}" allowed-address=192.0.2.2/32` + for (const [native, expected] of [["yes", true], ["true", true], ["no", false], ["false", false]] as const) { + const parsed = parseWireguardImport(base + ` disabled=${native} private-key=none`, "rsc") + assert.equal(parsed.interface.disabled, false); assert.equal(parsed.peers[0].disabled, expected); assert.equal(parsed.peers[0].privateKey, "none") + } + assert.equal(parseWireguardImport(base + " private-key=auto", "rsc").peers[0].privateKey, "auto") + assert.equal(parseWireguardImport(base + ` private-key="${key}"`, "rsc").peers[0].privateKey, key) + for (const suffix of [" private-key=NEVER_SECRET", " disabled=maybe", " preshared-key=NEVER_SECRET", " allowed-address=192.0.2.3/32", " endpoint-address=bad;script", ' endpoint-address="bad;script"', " persistent-keepalive=65536"]) assert.throws(() => parseWireguardImport(base + suffix, "rsc"), (error: Error) => !error.message.includes("NEVER_SECRET")) + assert.throws(() => parseWireguardImport(base.replace("interface=own-wg", "interface=foreign"), "rsc"), /другому интерфейсу/) + assert.throws(() => parseWireguardImport(base + "\n/interface wireguard add name=second", "rsc"), /один интерфейс/) +}) + +test("bounded multi-peer and allowed-address import remains supported in both formats", () => { + const peers = Array.from({ length: WG_CONFIG_LIMITS.peers }, (_, index) => ({ key: Buffer.alloc(32, index + 1).toString("base64"), address: `192.0.2.${index + 1}/32` })) + const confSource = `[Interface]\nPrivateKey = ${key}\n` + peers.map(peer => `[Peer]\nPublicKey = ${peer.key}\nAllowedIPs = ${peer.address}`).join("\n") + const rscSource = "/interface wireguard add name=wg-bound\n" + peers.map(peer => `/interface wireguard peers add interface=wg-bound public-key="${peer.key}" allowed-address=${peer.address}`).join("\n") + assert.equal(parseWireguardImport(confSource, "conf").peers.length, WG_CONFIG_LIMITS.peers) + assert.equal(parseWireguardImport(rscSource, "rsc").peers.length, WG_CONFIG_LIMITS.peers) + for (const [source, format] of [[confSource + `\n[Peer]\nPublicKey = ${key}\nAllowedIPs = 192.0.2.254/32`, "conf"], [rscSource + `\n/interface wireguard peers add public-key="${key}" allowed-address=192.0.2.254/32`, "rsc"]] as const) assert.throws(() => parseWireguardImport(source, format), /предел/) + const routes = Array.from({ length: WG_CONFIG_LIMITS.addressesPerPeer }, (_, index) => `198.51.100.${index + 1}/32`).join(",") + assert.equal(parseWireguardImport(conf.replace("198.18.255.242/32", routes), "conf").peers[0].allowedAddresses.length, WG_CONFIG_LIMITS.addressesPerPeer) + assert.throws(() => parseWireguardImport(conf.replace("198.18.255.242/32", routes + ",198.51.100.254/32"), "conf"), /AllowedIPs/) + assert.throws(() => parseWireguardImport(conf + conf.slice(conf.indexOf("[Peer]")), "conf"), /повторяется PublicKey/) + assert.throws(() => parseWireguardImport(conf.replace("198.18.255.242/32", "2001:db8::1/64,2001:0db8::2/64"), "conf"), /Повторяющиеся/) +}) + +test("UTF-8, token, line and total bounds apply before semantic parsing", () => { + for (const source of ["я".repeat(WG_CONFIG_LIMITS.bytes / 2 + 1), "#\n".repeat(WG_CONFIG_LIMITS.lines), "#".repeat(WG_CONFIG_LIMITS.lineBytes + 1), "[Interface]\nName = " + "я".repeat(WG_CONFIG_LIMITS.tokenBytes / 2 + 1)]) assert.throws(() => parseWgConfig(source, "conf")) +}) diff --git a/backend/src/services/wireguard-import.ts b/backend/src/services/wireguard-import.ts index 7e488186..e9b386a7 100644 --- a/backend/src/services/wireguard-import.ts +++ b/backend/src/services/wireguard-import.ts @@ -1,38 +1,30 @@ import { isIP } from "node:net" -import { parseWgConfig, type WgParsedConfig } from "./wireguard-config.js" +import { parseWgConfig, WG_CONFIG_LIMITS, type WgParsedConfig } from "./wireguard-config.js" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" function validKey(value: string): boolean { - return /^[A-Za-z0-9+/]{43}=$/.test(value) && Buffer.from(value, "base64").length === 32 + return /^[A-Za-z0-9+/]{43}=$/.test(value) && Buffer.from(value, "base64").length === 32 && Buffer.from(value, "base64").toString("base64") === value && Buffer.from(value, "base64").some(byte => byte !== 0) } function validNetwork(value: string): boolean { const parts = value.split("/") const [address, prefix] = parts const version = isIP(address ?? "") - return parts.length === 2 && version !== 0 && /^\d+$/.test(prefix ?? "") && Number(prefix) <= (version === 4 ? 32 : 128) + return parts.length === 2 && version !== 0 && !value.includes("%") && /^\d+$/.test(prefix ?? "") && Number(prefix) <= (version === 4 ? 32 : 128) } +function safeText(value: string | undefined, maximum: number, multilineComment = false) { return value === undefined || value.length <= maximum && !(multilineComment ? /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/ : /[\u0000-\u001f\u007f]/).test(value) } +function validHost(value: string) { return value === "" || value.length <= 253 && !value.includes("%") && (isIP(value) !== 0 || /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.?$/i.test(value)) } /** Validate the complete import before the first RouterOS mutation. Never echo keys. */ export function parseWireguardImport(content: string, format: "auto" | "rsc" | "conf"): WgParsedConfig { const parsed = parseWgConfig(content, format) if (parsed.format === "conf") { - if ((content.match(/^\s*\[Interface\]\s*$/gim) ?? []).length !== 1) { - throw new Error("Конфигурация .conf должна содержать одну секцию [Interface]") - } if (!parsed.interface.privateKey) { throw new Error("Заполните PrivateKey: клиентский шаблон нельзя применить без приватного ключа") } - if ((content.match(/^\s*\[Peer\]\s*$/gim) ?? []).length !== parsed.peers.length) { - throw new Error("У каждого пира должен быть PublicKey") - } - if (/^\s*Address\s*=.*,/im.test(content)) { - throw new Error("Импорт нескольких адресов Address пока не поддерживается; укажите один адрес") - } } - if (/^\s*PresharedKey\s*=\s*\S/im.test(content) || /\bpreshared-key\s*=/i.test(content)) { - throw new Error("Импорт PresharedKey пока не поддерживается; конфигурация не применена") - } - if (!parsed.interface.name.trim()) throw new Error("Укажите имя интерфейса") + if (!parsed.interface.name.trim() || !safeText(parsed.interface.name, 64)) throw new Error("Укажите корректное имя интерфейса") + if (!safeText(parsed.interface.comment, 1000, true)) throw new Error("Некорректный комментарий интерфейса WireGuard") if (parsed.interface.privateKey && !validKey(parsed.interface.privateKey)) { throw new Error("Некорректный PrivateKey: нужен ключ WireGuard длиной 32 байта в base64") } @@ -42,22 +34,27 @@ export function parseWireguardImport(content: string, format: "auto" | "rsc" | " if (parsed.interface.listenPort !== undefined && (!Number.isInteger(parsed.interface.listenPort) || parsed.interface.listenPort < 1 || parsed.interface.listenPort > 65535)) { throw new Error("ListenPort должен быть от 1 до 65535") } - if (parsed.interface.mtu !== undefined && (!Number.isInteger(parsed.interface.mtu) || parsed.interface.mtu <= 0)) { - throw new Error("MTU должен быть положительным целым числом") + if (parsed.interface.mtu !== undefined && (!Number.isInteger(parsed.interface.mtu) || parsed.interface.mtu <= 0 || parsed.interface.mtu > 65535)) { + throw new Error("MTU должен быть целым числом от 1 до 65535") } + if (parsed.peers.length > WG_CONFIG_LIMITS.peers || new Set(parsed.peers.map(peer => peer.publicKey)).size !== parsed.peers.length) throw new Error("Превышен предел peers или повторяется PublicKey") for (const peer of parsed.peers) { if (!validKey(peer.publicKey)) throw new Error("Некорректный PublicKey пира") - if (!peer.allowedAddresses.length || peer.allowedAddresses.some(value => !validNetwork(value))) { + if (!peer.allowedAddresses.length || peer.allowedAddresses.length > WG_CONFIG_LIMITS.addressesPerPeer || peer.allowedAddresses.some(value => !validNetwork(value))) { throw new Error("Заполните AllowedIPs / allowed-address каждого пира корректными сетями с префиксом") } + if (new Set(peer.allowedAddresses.map(canonicalBgpPolicyPrefix)).size !== peer.allowedAddresses.length) throw new Error("Повторяющиеся AllowedIPs / allowed-address") + if (peer.privateKey !== undefined && !["auto", "none"].includes(peer.privateKey) && !validKey(peer.privateKey)) throw new Error("Некорректный приватный ключ пира WireGuard") + if (peer.endpointAddress !== undefined && !validHost(peer.endpointAddress)) throw new Error("Некорректный адрес Endpoint") + if (!safeText(peer.name, 128) || !safeText(peer.comment, 1000, true) || [peer.clientAddress, peer.clientDns, peer.clientEndpoint].some(value => !safeText(value, 1000))) throw new Error("Некорректные текстовые поля пира WireGuard") if (peer.endpointPort !== undefined && (!Number.isInteger(peer.endpointPort) || peer.endpointPort < 1 || peer.endpointPort > 65535)) { throw new Error("Порт Endpoint должен быть от 1 до 65535") } if (parsed.format === "conf" && peer.endpointAddress && peer.endpointPort === undefined) { throw new Error("Укажите порт Endpoint после адреса") } - if (peer.persistentKeepalive !== undefined && (!Number.isInteger(peer.persistentKeepalive) || peer.persistentKeepalive < 0)) { - throw new Error("PersistentKeepalive должен быть неотрицательным целым числом") + if (peer.persistentKeepalive !== undefined && (!Number.isInteger(peer.persistentKeepalive) || peer.persistentKeepalive < 0 || peer.persistentKeepalive > 65535)) { + throw new Error("PersistentKeepalive должен быть целым числом от 0 до 65535") } } return parsed diff --git a/backend/src/services/wireguard-interface-create.test.ts b/backend/src/services/wireguard-interface-create.test.ts new file mode 100644 index 00000000..b246d4f5 --- /dev/null +++ b/backend/src/services/wireguard-interface-create.test.ts @@ -0,0 +1,82 @@ +import assert from "node:assert/strict" +import { fileURLToPath } from "node:url" +import { assertWireguardCreateAllowed, wireguardCreatePrefix, wireguardCreateStages, wireguardInterfaceCreateSchema, wireguardPublicKey, wireguardCreatePreserved } from "./wireguard-interface-create.js" +import { wireguardInterfaceDeleteProof, wireguardInterfaceDependencyPaths, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { wireguardCorroboratedPrivateScalar } from "./wireguard-key-scalar.js" + +export async function acceptWireguardInterfaceCreateModel() { + const key = Buffer.alloc(32, 41).toString("base64"), publicKey = wireguardPublicKey(key) + const input = wireguardInterfaceCreateSchema.parse({ serverId: 111, name: "wg-owned", privateKey: key, address: "2001:db8::1/64", peer: { publicKey, allowedAddresses: ["192.0.2.2/32"], endpointAddress: "example.org", endpointPort: 54321, persistentKeepalive: 60, name: "owned", comment: "owned peer", privateKey: "none", clientAddress: "10.0.0.2", clientDns: "1.1.1.1", clientEndpoint: "vpn.example.org" } }) + const state: WireguardInterfaceDeleteState = { base: { interfaces: [], peers: [], bindings: [], flow: { hostPublicKey: "", registered: false }, serverProof: "source", proof: "base" }, ipv4: [], ipv6: [], dependencies: Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])), filters: [], proof: "" } + state.proof = await wireguardInterfaceDeleteProof(state) + assertWireguardCreateAllowed(state, input) + assert.deepEqual(wireguardCreateStages(input), ["interface", "address", "peer", "peer-enable", "address-enable", "interface-enable"]) + assert.equal(await wireguardCreatePrefix(state, input, state.proof, 0), true) + const after = structuredClone(state) + after.base.interfaces.push({ ".id": "*a", name: input.name, disabled: "true", "listen-port": "13231", mtu: "1420", "public-key": publicKey, "private-key": key }) + after.dependencies["/interface"].push({ ".id": "*a", name: input.name, type: "wg" }) + assert.equal(await wireguardCreatePrefix(after, input, state.proof, 1), true) + after.ipv6.push({ ".id": "*b", interface: "*a", address: "2001:0db8::1/64", dynamic: "false", disabled: "true", advertise: "no" }) + assert.equal(await wireguardCreatePrefix(after, input, state.proof, 2), true) + after.base.peers.push({ ".id": "*c", interface: "*a", "public-key": publicKey, "allowed-address": "192.0.2.2/32", "endpoint-address": "example.org", "endpoint-port": "54321", "persistent-keepalive": "1m", name: "owned", comment: "owned peer", "private-key": "none", "client-address": "10.0.0.2", "client-dns": "1.1.1.1", "client-endpoint": "vpn.example.org", disabled: "true" }) + assert.equal(await wireguardCreatePrefix(after, input, state.proof, 3), true) + after.base.peers[0].disabled = "no" + assert.equal(await wireguardCreatePrefix(after, input, state.proof, 4), true) + after.ipv6[0].disabled = "no" + assert.equal(await wireguardCreatePrefix(after, input, state.proof, 5), true) + after.base.interfaces[0].disabled = "no" + assert.equal(await wireguardCreatePrefix(after, input, state.proof, 6), true) + const beforeActivation = structuredClone(after); beforeActivation.base.interfaces[0].disabled = "true" + const clamped = structuredClone(after); clamped.base.interfaces[0]["private-key"] = wireguardCorroboratedPrivateScalar(key, publicKey)! + const untouched = structuredClone(clamped) + assert.equal(wireguardCreatePreserved(beforeActivation, clamped, input, "interface-enable"), true) + assert.equal(await wireguardCreatePrefix(clamped, input, state.proof, 6), true) + assert.deepEqual(clamped, untouched); assert.equal(input.privateKey, key); assert.equal(beforeActivation.base.interfaces[0]["private-key"], key) + const noActivation = structuredClone(beforeActivation); noActivation.base.interfaces[0]["private-key"] = clamped.base.interfaces[0]["private-key"] + assert.equal(wireguardCreatePreserved(beforeActivation, noActivation, input, "peer-enable"), false, "Only the owned interface-enable stage authorizes scalar reserialization") + for (let ignored = 0; ignored < 32; ignored++) { + const bytes = Buffer.from(key, "base64"); bytes[0] = (bytes[0] & 248) | (ignored & 7); bytes[31] = (bytes[31] & 63) | ((ignored >> 3) << 6) + const represented = structuredClone(after); represented.base.interfaces[0]["private-key"] = bytes.toString("base64") + assert.equal(await wireguardCreatePrefix(represented, input, state.proof, 6), true) + assert.equal(wireguardCreatePreserved(beforeActivation, represented, input, "interface-enable"), true) + } + const rotated = structuredClone(clamped), changedBytes = Buffer.from(key, "base64"); changedBytes[1] ^= 1 + rotated.base.interfaces[0]["private-key"] = changedBytes.toString("base64"); rotated.base.interfaces[0]["public-key"] = wireguardPublicKey(rotated.base.interfaces[0]["private-key"]) + assert.equal(wireguardCreatePreserved(beforeActivation, rotated, input, "interface-enable"), false) + assert.equal(await wireguardCreatePrefix(rotated, input, state.proof, 6), false) + const wrongPublic = structuredClone(clamped); wrongPublic.base.interfaces[0]["public-key"] = rotated.base.interfaces[0]["public-key"] + assert.equal(wireguardCreatePreserved(beforeActivation, wrongPublic, input, "interface-enable"), false) + for (const mutate of [(value: typeof clamped) => { value.base.interfaces[0].opaque = "NEVER_CHANGED" }, (value: typeof clamped) => { value.base.peers[0]["private-key"] = key }, (value: typeof clamped) => { value.base.interfaces[0]["private-key"] = "(hidden)" }]) { const changed = structuredClone(clamped); mutate(changed); assert.equal(wireguardCreatePreserved(beforeActivation, changed, input, "interface-enable"), false) } + const foreignBaseline = structuredClone(state) + foreignBaseline.base.interfaces.push({ ".id": "*f", name: "wg-foreign", "private-key": key, "public-key": publicKey, disabled: "true" }); foreignBaseline.dependencies["/interface"].push({ ".id": "*f", name: "wg-foreign", type: "wg" }) + foreignBaseline.proof = await wireguardInterfaceDeleteProof(foreignBaseline) + const withForeign = structuredClone(clamped); withForeign.base.interfaces.push(structuredClone(foreignBaseline.base.interfaces[0])); withForeign.dependencies["/interface"].push(structuredClone(foreignBaseline.dependencies["/interface"][0])) + assert.equal(await wireguardCreatePrefix(withForeign, input, foreignBaseline.proof, 6), true) + withForeign.base.interfaces[1]["private-key"] = wireguardCorroboratedPrivateScalar(key, publicKey)! + assert.equal(await wireguardCreatePrefix(withForeign, input, foreignBaseline.proof, 6), false, "Create prefix must not normalize foreign baseline private bytes") + for (const change of [ + (value: typeof after) => { value.base.interfaces[0]["public-key"] = Buffer.alloc(32, 45).toString("base64") }, + (value: typeof after) => { value.base.interfaces[0]["private-key"] = Buffer.alloc(32, 44).toString("base64") }, + (value: typeof after) => { delete value.base.interfaces[0].disabled }, + (value: typeof after) => { value.dependencies["/ip/firewall/filter"].push({ ".id": "*d", comment: "NEVER_OPAQUE" }) }, + (value: typeof after) => { value.base.peers[0]["persistent-keepalive"] = "garbage" }, + (value: typeof after) => { value.ipv6[0].dynamic = "unknown" }, + (value: typeof after) => { value.dependencies["/interface"][0][".id"] = "*f" }, + (value: typeof after) => { value.ipv4.push({ ".id": "*g", interface: input.name, address: "192.0.2.3/32", disabled: "true", dynamic: "false" }) }, + ]) { const changed = structuredClone(after); change(changed); assert.equal(await wireguardCreatePrefix(changed, input, state.proof, 6), false) } + for (const change of [ + (value: typeof state) => { value.dependencies["/interface"].push({ ".id": "*a", name: input.name }) }, + (value: typeof state) => { value.dependencies["/interface/vlan"].push({ ".id": "*a", interface: input.name }) }, + (value: typeof state) => { value.dependencies["/ip/vrf"].push({ ".id": "*a", name: "other", interfaces: "all" }) }, + (value: typeof state) => { value.dependencies["/routing/ospf/interface-template"].push({ ".id": "*a", networks: "2001:db8::/64" }) }, + (value: typeof state) => { value.ipv6.push({ ".id": "*a", interface: "ether1", address: input.address! }) }, + ]) { const changed = structuredClone(state); change(changed); assert.throws(() => assertWireguardCreateAllowed(changed, input)) } + const quoted = wireguardInterfaceCreateSchema.parse({ serverId: 111, name: 'WG тест "edge" [x]', privateKey: key }) + assertWireguardCreateAllowed(state, quoted) + const named = structuredClone(state) + named.dependencies["/interface/vlan"].push({ ".id": "*named", interface: quoted.name }) + assert.throws(() => assertWireguardCreateAllowed(named, quoted)) + for (const bad of [{ name: "\u0000bad" }, { name: " " }, { listenPort: 65536 }, { mtu: 65536 }, { privateKey: "bad" }, { peer: { publicKey, allowedAddresses: ["bad"] } }]) assert.equal(wireguardInterfaceCreateSchema.safeParse({ serverId: 111, name: "wg", ...bad }).success, false) + console.log("WireGuard create model: all six disabled/activation prefixes, optional peer fields and literal keys, IPv6 normalization, unknown/static/native identity, whole dependency preservation, reserved ownership and selector conflicts checked") +} +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) await acceptWireguardInterfaceCreateModel() diff --git a/backend/src/services/wireguard-interface-create.ts b/backend/src/services/wireguard-interface-create.ts new file mode 100644 index 00000000..eeb41d96 --- /dev/null +++ b/backend/src/services/wireguard-interface-create.ts @@ -0,0 +1,176 @@ +import { createPrivateKey, createPublicKey } from "node:crypto" +import { isIP } from "node:net" +import { z } from "zod" +import { pool } from "../db/index.js" +import { wgCreateInterfaceSchema, wgCreatePeerSchema } from "@mmapp/contracts/wireguard" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { readWireguardInterfaceDeleteState, wireguardInterfaceDeleteProof, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { assertGreDeleteDependencies, greStableRows } from "./gre-mutation-state.js" +import { wireguardConfigurationRows } from "./wireguard-peer-delete.js" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { observedWireguardPeerKeepalive } from "./wireguard-peer-update.js" +import { interfaceAddressPath, interfaceAddressKey } from "./entity-snapshots.js" +import { toRosBody } from "./wireguard-ros.js" +import { MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { wireguardCorroboratedPrivateScalar, wireguardPrivateScalarsEqual } from "./wireguard-key-scalar.js" + +const text = z.string().max(1000).refine(value => !/[\u0000-\u001f\u007f]/.test(value)) +export const wireguardCreateKey = z.string().regex(/^[A-Za-z0-9+/]{43}=$/).refine(value => Buffer.from(value, "base64").toString("base64") === value && Buffer.from(value, "base64").some(byte => byte !== 0)) +const peerSchema = wgCreatePeerSchema.extend({ publicKey: wireguardCreateKey, allowedAddresses: z.array(z.string().max(100).refine(value => { try { canonicalBgpPolicyPrefix(value); return true } catch { return false } })).min(1).max(100), endpointAddress: text.max(253).optional(), endpointPort: z.number().int().min(1).max(65535).optional(), persistentKeepalive: z.number().int().min(0).max(65535).optional(), comment: text.optional(), name: text.max(128).optional(), privateKey: z.union([wireguardCreateKey, z.enum(["auto", "none"])]).optional(), clientAddress: text.optional(), clientDns: text.optional(), clientEndpoint: text.optional() }).strict() +export const wireguardInterfaceCreateSchema = wgCreateInterfaceSchema.extend({ serverId: z.number().int().positive().safe(), name: text.min(1).max(64).refine(value => value.trim().length > 0), listenPort: z.number().int().min(1).max(65535).default(13231), mtu: z.number().int().min(1).max(65535).default(1420), comment: text.optional(), privateKey: wireguardCreateKey.optional(), peer: peerSchema.optional() }).strict() +export type WireguardInterfaceCreate = z.infer +type State = WireguardInterfaceDeleteState +type Row = Record +const fail = (code: string, message: string) => new McpError(code, message, code === "INVALID_INPUT" ? 400 : ["STATE_UNAVAILABLE", "MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(code) ? 502 : 409) +export const readWireguardInterfaceCreateState = (server: ServerRow, authorize: () => Promise = async () => {}) => readWireguardInterfaceDeleteState(server, "*0", authorize) +export async function wireguardCreateProtectionProof(serverId: number) { + const { rows } = await pool.query("SELECT object_ref FROM mcp_protections WHERE object_ref->>'serverId'=$1 OR (object_ref->>'kind'='server' AND object_ref->>'id'=$1) ORDER BY id LIMIT 1001", [String(serverId)]) + if (rows.length > 1000) throw fail("STATE_UNAVAILABLE", "Превышен предел защит WireGuard") + return fingerprint(rows) +} +export function wireguardPublicKey(privateKey: string) { + const key = createPrivateKey({ key: Buffer.concat([Buffer.from("302e020100300506032b656e04220420", "hex"), Buffer.from(privateKey, "base64")]), format: "der", type: "pkcs8" }) + return createPublicKey(key).export({ format: "der", type: "spki" }).subarray(-32).toString("base64") +} +export function wireguardCreatePeerBody(input: WireguardInterfaceCreate) { + const peer = input.peer! + return toRosBody({ interface: input.name, "public-key": peer.publicKey, "allowed-address": peer.allowedAddresses.join(","), "endpoint-address": peer.endpointAddress, "endpoint-port": peer.endpointPort === undefined ? undefined : String(peer.endpointPort), "persistent-keepalive": peer.persistentKeepalive === undefined ? undefined : String(peer.persistentKeepalive), comment: peer.comment, name: peer.name, "private-key": peer.privateKey, "client-address": peer.clientAddress, "client-dns": peer.clientDns, "client-endpoint": peer.clientEndpoint, disabled: "true" }) +} +export function wireguardCreateStages(input: WireguardInterfaceCreate) { + return ["interface", ...(input.address ? ["address"] : []), ...(input.peer ? ["peer", ...(input.peer.disabled ? [] : ["peer-enable"])] : []), ...(input.address ? ["address-enable"] : []), ...(input.disabled ? [] : ["interface-enable"])] +} +export function assertWireguardCreateAllowed(state: State, input: WireguardInterfaceCreate) { + if (input.name === "wg-flow" || input.comment === "MikrotikManager traffic-flow overlay" || input.peer?.comment === "MM traffic-flow collector" || input.peer?.name === "mm-collector" || input.peer?.publicKey === state.base.flow.hostPublicKey) throw fail("PROTECTED_OBJECT", "Имя или peer зарезервированы сервисом NetFlow") + if (state.base.interfaces.some(row => row.name === input.name) || state.dependencies["/interface"].some(row => row.name === input.name)) throw fail("STATE_CONFLICT", "Имя интерфейса уже занято") + try { assertGreDeleteDependencies({ dependencies: state.dependencies, bindings: state.base.bindings, filters: state.filters }, { ".id": "*0", name: input.name }, "Новый WireGuard") } + catch { throw fail("DEPENDENCY_CONFLICT", "Новый WireGuard используется существующей конфигурацией или привязкой; сначала измените зависимости") } + if (state.dependencies["/ip/vrf"].some(row => row.name !== "main" && row.interfaces?.split(",").includes("all"))) throw fail("DEPENDENCY_CONFLICT", "Новый интерфейс попадёт в существующий VRF selector; требуется отдельный план VRF") + if (input.address) { + const ip = input.address.split("/")[0], family = isIP(ip) + const exact = (value: string) => { try { return canonicalBgpPolicyPrefix(value + (family === 4 ? "/32" : "/128")) } catch { return null } } + if ([...state.ipv4, ...state.ipv6].some(row => isIP(row.address?.split("/")[0]) === family && exact(row.address.split("/")[0]) === exact(ip))) throw fail("DEPENDENCY_CONFLICT", "Локальный адрес уже используется") + for (const row of state.dependencies["/routing/ospf/interface-template"]) for (const network of (row.networks ?? "").split(",").filter(Boolean)) { + try { if (isIP(network.split("/")[0]) === family && canonicalBgpPolicyPrefix(ip + "/" + network.split("/")[1]) === canonicalBgpPolicyPrefix(network)) throw fail("DEPENDENCY_CONFLICT", "Адрес активирует существующий сетевой selector OSPF") } + catch (error) { if (error instanceof McpError) throw error; throw fail("STATE_UNAVAILABLE", "Не подтверждён selector OSPF") } + } + } +} +function matches(row: Row, body: Record) { + return Object.entries(body).every(([key, value]) => { + if (key === "disabled" || key === "advertise") return observedBoolean(row[key]) === observedBoolean(value) + if (["mtu", "listen-port", "endpoint-port"].includes(key)) return /^\d+$/.test(row[key] ?? "") && Number(row[key]) === Number(value) + if (key === "persistent-keepalive") return observedWireguardPeerKeepalive(row[key] ?? "") === Number(value) + if (key === "address") { try { return interfaceAddressKey(row[key]) === interfaceAddressKey(value) } catch { return false } } + if (key === "allowed-address") { try { return fingerprint(row[key].split(",").map(canonicalBgpPolicyPrefix).sort()) === fingerprint(value.split(",").map(canonicalBgpPolicyPrefix).sort()) } catch { return false } } + if (key === "private-key" && value === "auto") return wireguardCreateKey.safeParse(row[key]).success + if (key === "private-key" && value === "none") return row[key] === "none" || row[key] === "" + return (row[key] ?? "") === value + }) +} +/** Strip exactly the verified additions; seal every other object and opaque property. */ +export async function wireguardCreatePrefix(state: State, input: WireguardInterfaceCreate, baseline: string, stageCount: number) { + const stages = wireguardCreateStages(input), done = new Set(stages.slice(0, stageCount)), next = structuredClone(state) + if (!Number.isInteger(stageCount) || stageCount < 0 || stageCount > stages.length) return false + const selected = next.base.interfaces.filter(row => row.name === input.name) + if (!done.has("interface")) return selected.length === 0 && state.proof === baseline + if (selected.length !== 1) return false + const target = selected[0], id = target[".id"], generic = next.dependencies["/interface"].filter(row => row.name === input.name || row[".id"] === id) + if (generic.length !== 1 || generic[0][".id"] !== id || generic[0].name !== input.name) return false + if (["dynamic", "builtin", "default"].some(key => target[key] !== undefined && observedBoolean(target[key]) !== false)) return false + if (!matches(target, { name: input.name, "listen-port": String(input.listenPort), mtu: String(input.mtu), comment: input.comment ?? "", disabled: String(!done.has("interface-enable")) })) return false + if (input.privateKey ? target["public-key"] !== wireguardPublicKey(input.privateKey) : !wireguardCreateKey.safeParse(target["public-key"]).success) return false + if (input.privateKey && target["private-key"] !== undefined && !wireguardPrivateScalarsEqual(target["private-key"], input.privateKey, target["public-key"])) return false + const belongs = (row: Row) => [id, input.name].includes(row.interface) + const peers = next.base.peers.filter(belongs) + if (peers.length !== (done.has("peer") ? 1 : 0)) return false + if (peers.length) { + const body = wireguardCreatePeerBody(input); body.interface = peers[0].interface; body.disabled = String(!done.has("peer-enable")) + if (!matches(peers[0], body) || ["dynamic", "builtin", "default"].some(key => peers[0][key] !== undefined && observedBoolean(peers[0][key]) !== false)) return false + } + const addressRows = [...next.ipv4, ...next.ipv6].filter(row => belongs(row) && !(observedBoolean(row.dynamic) === true && /^fe80:/i.test(row.address ?? ""))) + if (addressRows.length !== (done.has("address") ? 1 : 0)) return false + if (addressRows.length) { + const address = addressRows[0] + if (observedBoolean(address.dynamic) !== false || !matches(address, { address: input.address!, disabled: String(!done.has("address-enable")), ...(isIP(input.address!.split("/")[0]) === 6 ? { advertise: "false" } : {}) })) return false + if (!(isIP(input.address!.split("/")[0]) === 4 ? next.ipv4 : next.ipv6).includes(address)) return false + } + next.base.interfaces = next.base.interfaces.filter(row => row[".id"] !== id) + next.base.peers = next.base.peers.filter(row => !belongs(row)) + next.ipv4 = next.ipv4.filter(row => !belongs(row)); next.ipv6 = next.ipv6.filter(row => !belongs(row)) + next.dependencies["/interface"] = next.dependencies["/interface"].filter(row => row[".id"] !== id) + return await wireguardInterfaceDeleteProof(next) === baseline +} +/** Native defaults become sealed after the first read-back of each new object. */ +export function wireguardCreatePreserved(before: State, after: State, input: WireguardInterfaceCreate, stage: string) { + const target = before.base.interfaces.find(row => row.name === input.name) + if (!target) return stage === "interface" + const belongs = (row: Row) => [input.name, target[".id"]].includes(row.interface) + for (const [oldRows, newRows, allowed, interfaceActivation] of [ + [before.base.interfaces.filter(row => row[".id"] === target[".id"]), after.base.interfaces, stage === "interface-enable", stage === "interface-enable"], + [before.base.peers.filter(belongs), after.base.peers, stage === "peer-enable", false], + [before.ipv4.filter(row => belongs(row) && observedBoolean(row.dynamic) === false), after.ipv4, stage === "address-enable", false], + [before.ipv6.filter(row => belongs(row) && observedBoolean(row.dynamic) === false), after.ipv6, stage === "address-enable", false], + ] as const) for (const old of oldRows) { + const current = newRows.find(row => row[".id"] === old[".id"]) + if (!current) return false + const expected: Row = { ...old, ...(allowed ? { disabled: "false" } : {}) } + const observed: Row = { ...current } + if (interfaceActivation) { + const first = wireguardCorroboratedPrivateScalar(expected["private-key"], expected["public-key"]), second = wireguardCorroboratedPrivateScalar(observed["private-key"], observed["public-key"]) + if (first !== undefined && second !== undefined && first === second) { expected["private-key"] = first; observed["private-key"] = second } + } + if (fingerprint(greStableRows(wireguardConfigurationRows([expected]))) !== fingerprint(greStableRows(wireguardConfigurationRows([observed])))) return false + } + return true +} +export async function createWireguardInterface(server: ServerRow, raw: unknown, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: boolean } = {}) { + const parsed = wireguardInterfaceCreateSchema.safeParse(raw) + if (!parsed.success || parsed.data.serverId !== server.id) throw fail("INVALID_INPUT", "Некорректные параметры создания WireGuard") + const input = parsed.data + let protectionProof: string | undefined + const source = async () => { + await authorize() + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || fingerprint(fresh) !== fingerprint(server)) throw fail("STATE_CONFLICT", "Источник WireGuard изменился") + if (protectionProof !== undefined && await wireguardCreateProtectionProof(server.id) !== protectionProof) throw fail("STATE_CONFLICT", "Защиты WireGuard изменились; требуется новый план") + } + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "wireguard", id: input.name }, [server.id], async () => { + protectionProof = await wireguardCreateProtectionProof(server.id) + let state = await readWireguardInterfaceCreateState(server, authorize) + if (expectedProof && state.proof !== expectedProof) throw fail("STATE_CONFLICT", "WireGuard или зависимости изменились после подготовки") + assertWireguardCreateAllowed(state, input) + const baseline = state.proof, stages = wireguardCreateStages(input), client = MikrotikClient.fromServer(server) + for (let index = 0; index < stages.length; index++) { + const fresh = await readWireguardInterfaceCreateState(server, authorize) + if (fresh.proof !== state.proof) throw fail(index ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "Конфигурация изменилась между командами WireGuard") + const stage = stages[index], target = fresh.base.interfaces.find(row => row.name === input.name) + const dispatchFence = async () => { + const current = await readWireguardInterfaceCreateState(server, authorize) + if (current.proof !== fresh.proof) throw fail(index ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "WireGuard изменился после записи native intent; команда не отправлена") + await source() + } + await source() + try { + if (stage === "interface") await client.put("/interface/wireguard", toRosBody({ name: input.name, "listen-port": String(input.listenPort), mtu: String(input.mtu), comment: input.comment, "private-key": input.privateKey, disabled: "true" }), 15000, dispatchFence) + else if (stage === "address") await client.put(interfaceAddressPath(input.address!), { address: input.address!, interface: input.name, disabled: "true", ...(isIP(input.address!.split("/")[0]) === 6 ? { advertise: "false" } : {}) }, 15000, dispatchFence) + else if (stage === "peer") await client.put("/interface/wireguard/peers", wireguardCreatePeerBody(input), 15000, dispatchFence) + else if (stage === "interface-enable") await client.patch("/interface/wireguard/" + encodeURIComponent(target![".id"]), { disabled: "false" }, 15000, dispatchFence) + else if (stage === "peer-enable") await client.patch("/interface/wireguard/peers/" + encodeURIComponent(fresh.base.peers.find(row => [input.name, target![".id"]].includes(row.interface))![".id"]), { disabled: "false" }, 15000, dispatchFence) + else { const rows = isIP(input.address!.split("/")[0]) === 4 ? fresh.ipv4 : fresh.ipv6; await client.patch(interfaceAddressPath(input.address!) + "/" + encodeURIComponent(rows.find(row => [input.name, target![".id"]].includes(row.interface) && observedBoolean(row.dynamic) === false)![".id"]), { disabled: "false" }, 15000, dispatchFence) } + } catch (error) { if (error instanceof McpError) throw error; throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход команды создания WireGuard неизвестен; выполнение остановлено без повтора") } + const prior = state + try { state = await readWireguardInterfaceCreateState(server, authorize) } catch { throw fail("MUTATION_OUTCOME_UNKNOWN", "Команда WireGuard отправлена, read-back недоступен; повтор запрещён") } + if (!await wireguardCreatePrefix(state, input, baseline, index + 1)) throw fail("VERIFICATION_FAILED", "Создание WireGuard или сохранность зависимостей не подтверждены; повтор запрещён") + if (!wireguardCreatePreserved(prior, state, input, stage)) throw fail("VERIFICATION_FAILED", "Сохранность ранее подготовленных объектов WireGuard не подтверждена; повтор запрещён") + } + let revisionRecorded = false + try { if (options.recordRevision !== false) revisionRecorded = await captureAndAppendRevision({ serverId: server.id, section: "wireguard", source: "apply", capture: () => captureWireguardSnapshot(server, source) }) !== null } catch { /* Never repeat native creation to repair revision storage. */ } + return { ok: true, verified: true, interfaceId: state.base.interfaces.find(row => row.name === input.name)![".id"], afterProof: state.proof, commandCount: stages.length, revisionRecorded, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/wireguard-interface-delete.ts b/backend/src/services/wireguard-interface-delete.ts new file mode 100644 index 00000000..34bf9fab --- /dev/null +++ b/backend/src/services/wireguard-interface-delete.ts @@ -0,0 +1,199 @@ +import { isIP } from "node:net" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { pool } from "../db/index.js" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError, privateFingerprinter } from "../modules/mcp/security.js" +import { readWireguardPeerDeletionState, wireguardPeerDeletionTarget, wireguardConfigurationRows, type WireguardPeerDeletionState } from "./wireguard-peer-delete.js" +import { greStableRows, assertGreDeleteDependencies } from "./gre-mutation-state.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { isRouterOsPreHeaderGetReset, MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { wireguardInterfaceDependencyPaths, wireguardInterfaceRuntimeDependencyPaths } from "./routeros-native-paths.js" +import { markWireguardGetFailure, markWireguardReadFailure } from "./wireguard-read-diagnostic.js" +export { wireguardInterfaceDependencyPaths } from "./routeros-native-paths.js" + +type Row = Record +export interface WireguardInterfaceDeleteState { + base: WireguardPeerDeletionState; ipv4: Row[]; ipv6: Row[]; dependencies: Record; + filters: { id: number; gatewayTunnelId: string; gateway: string }[]; proof: string; +} +export interface WireguardInterfaceDeleteCommand { method: "PATCH" | "DELETE"; path: string; resource: "interfaces" | "peers" | "ipv4" | "ipv6"; id: string; body?: Record } +const runtimePaths = new Set(wireguardInterfaceRuntimeDependencyPaths) +const fail = (code: string, message: string) => new McpError(code, message, ["STATE_UNAVAILABLE", "MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(code) ? 502 : 409) +const automaticLinkLocal = (row: Row) => observedBoolean(row.dynamic) === true && /^fe80:/i.test(row.address ?? "") +const ordered = (path: string) => ["/ip/vrf", "/routing/rule", "/routing/filter/rule", "/routing/bfd/configuration"].includes(path) || /^\/(?:ip|ipv6)\/firewall\//.test(path) +const interfaceRows = (rows: Row[]) => greStableRows(wireguardConfigurationRows(rows)) +export function wireguardInterfaceDeleteValue(state: Omit) { + const wgNames = new Set(state.base.interfaces.map(row => row.name)) + return { source: state.base.serverProof, interfaces: interfaceRows(state.base.interfaces), peers: wireguardConfigurationRows(state.base.peers), bindings: state.base.bindings, flow: state.base.flow, + ipv4: greStableRows(state.ipv4.filter(row => observedBoolean(row.dynamic) !== true)), ipv6: greStableRows(state.ipv6.filter(row => observedBoolean(row.dynamic) !== true)), filters: state.filters, + dependencies: Object.fromEntries(Object.entries(state.dependencies).map(([path, rows]) => [path, runtimePaths.has(path) ? "runtime-inspected-not-sealed" : greStableRows(rows.filter(row => observedBoolean(row.dynamic) !== true && !(path === "/interface" && wgNames.has(row.name))).map(row => path === "/interface" ? { ".id": row[".id"], name: row.name, type: row.type ?? "" } : /\/(?:ip|ipv6)\/dhcp-client$/.test(path) ? Object.fromEntries(Object.entries(row).filter(([key]) => !["status", "expires-after", "address", "prefix", "gateway", "dhcp-server", "dhcp-server-address", "dhcp-server-duid", "primary-dns", "secondary-dns"].includes(key))) : row), ordered(path))])) } +} +export async function wireguardInterfaceDeleteProof(state: Omit) { + return (await privateFingerprinter("wireguard-interface-delete-v1"))(fingerprint(wireguardInterfaceDeleteValue(state))) +} +async function source(server: ServerRow, authorize: () => Promise) { + await authorize() + 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") + 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, authorizeSource: () => Promise, now = () => performance.now()) { + const timeoutMs = 10_000, maxBytes = 1_048_576 + const unavailable = () => fail("STATE_UNAVAILABLE", "Не прочитан обязательный источник зависимостей WireGuard") + const retriedPaths = new Set() + return async (path: string): Promise => { + await authorizeSource() + const deadline = now() + timeoutMs + try { + return await client.get(path, timeoutMs, undefined, maxBytes) + } catch (error) { + const retryableCatalogue = path === "/ip/address" || path === "/ipv6/address" || wireguardInterfaceDependencyPaths.includes(path) + if (retriedPaths.size >= 2 || retriedPaths.has(path) || !retryableCatalogue + || !isRouterOsPreHeaderGetReset(error) || now() >= deadline) throw markWireguardGetFailure(unavailable(), path, 1, error) + } + retriedPaths.add(path) + await authorizeSource() + 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) + } catch (error) { + throw markWireguardGetFailure(unavailable(), path, 2, error) + } + } +} + +export async function readWireguardInterfaceDeleteState(server: ServerRow, id: string, authorize: () => Promise = async () => {}): Promise { + 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])) + const read = async (path: string, required: string[] = []) => { + const raw = await readDependency(path) + bytes += Buffer.byteLength(JSON.stringify(raw)) + let rows: Row[] + try { rows = nativeRows(raw, required) } + catch (error) { + if (!(error instanceof McpError)) throw error + throw markWireguardReadFailure(error, path, undefined, Array.isArray(raw) && raw.length > 2000 ? "catalogue-limit" : "invalid-catalogue") + } + totalRows += rows.length + if (bytes > 1048576 || totalRows > 10000) throw markWireguardReadFailure(fail("STATE_UNAVAILABLE", "Превышен предел зависимостей WireGuard: 10000 строк / 1 МиБ"), path, undefined, "catalogue-limit") + return rows + } + const ipv4 = await read("/ip/address", ["address", "interface"]), ipv6 = await read("/ipv6/address", ["address", "interface"]) + const dependencies: Record = {} + for (const path of wireguardInterfaceDependencyPaths) dependencies[path] = await read(path, path === "/interface" ? ["name"] : []) + const parent = base.interfaces.find(row => row[".id"] === id) + 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 { + canonicalBgpPolicyPrefix(row.address) + if (isIP(row.address.split("/")[0]) !== (path === "/ip/address" ? 4 : 6)) throw new Error("family") + } catch { throw fail("STATE_UNAVAILABLE", "Не подтверждён адрес выбранного WireGuard") } + if (row.dynamic === undefined) { + await source(server, authorize) + let ret: unknown + try { ret = await client.post(path + "/get", { ".id": row[".id"], "value-name": "dynamic" }) } catch { throw fail("STATE_UNAVAILABLE", "Не подтверждено происхождение адреса WireGuard") } + if (!ret || typeof ret !== "object" || !("ret" in ret) || typeof ret.ret !== "string" || observedBoolean(ret.ret) === null) throw fail("STATE_UNAVAILABLE", "Не подтверждено происхождение адреса WireGuard") + row.dynamic = ret.ret + } + if (observedBoolean(row.dynamic) === null) throw fail("STATE_UNAVAILABLE", "Неизвестное происхождение адреса WireGuard") + } + await source(server, authorize) + const filters = (await pool.query('SELECT id,gateway_tunnel_id AS "gatewayTunnelId",gateway FROM filter_rules WHERE server_id=$1 ORDER BY id LIMIT 1001', [server.id])).rows + if (filters.length > 1000) throw fail("STATE_UNAVAILABLE", "Превышен предел DB-зависимостей WireGuard") + if (bytes + Buffer.byteLength(JSON.stringify([filters, base.bindings, base.flow])) > 1048576) throw fail("STATE_UNAVAILABLE", "Превышен суммарный предел зависимостей WireGuard: 1 МиБ") + await source(server, authorize) + const state = { base, ipv4, ipv6, dependencies, filters } + return { ...state, proof: await wireguardInterfaceDeleteProof(state) } +} +export function planWireguardInterfaceDelete(state: WireguardInterfaceDeleteState, id: string, cascade = true): WireguardInterfaceDeleteCommand[] { + if (!/^\*[a-z0-9]+$/i.test(id) || id.length > 100) throw new McpError("INVALID_INPUT", "Требуется стабильный ID WireGuard", 400) + const target = state.base.interfaces.find(row => row[".id"] === id) + if (!target?.name || state.base.interfaces.filter(row => row.name === target.name).length !== 1) throw fail("STATE_CONFLICT", "Интерфейс WireGuard не найден однозначно") + for (const flag of ["dynamic", "builtin", "default"]) if (target[flag] !== undefined && observedBoolean(target[flag]) !== false) throw fail("PROTECTED_OBJECT", "Системный интерфейс WireGuard нельзя удалить") + if (observedBoolean(target.disabled) === null) throw fail("STATE_UNAVAILABLE", "Не подтверждено состояние интерфейса WireGuard") + if (target.comment === "MikrotikManager traffic-flow overlay" || target.name === "wg-flow" && state.base.flow.registered === true) throw fail("PROTECTED_OBJECT", "Интерфейс управляется NetFlow; используйте отдельное изменение NetFlow") + if (target.name === "wg-flow" && state.base.flow.registered === null) throw fail("STATE_UNAVAILABLE", "Не подтверждено владение интерфейсом NetFlow") + const generic = state.dependencies["/interface"].filter(row => row.name === target.name || row[".id"] === id) + if (generic.length !== 1 || generic[0].name !== target.name || generic[0][".id"] !== id) throw fail("STATE_CONFLICT", "Расходится общий и WireGuard native ID") + const belongs = (row: Row) => [target.name, id].includes(row.interface) + try { assertGreDeleteDependencies({ dependencies: state.dependencies, bindings: state.base.bindings, filters: state.filters }, target, "WireGuard") } + catch { throw fail("DEPENDENCY_CONFLICT", "WireGuard используется интерфейсом, маршрутизацией, firewall или объектом приложения; сначала измените зависимости") } + const peers = state.base.peers.filter(belongs) + for (const peer of peers) wireguardPeerDeletionTarget(state.base, peer[".id"]) + const addresses = [...state.ipv4, ...state.ipv6].filter(belongs) + if (addresses.some(row => observedBoolean(row.dynamic) !== false && !automaticLinkLocal(row))) throw fail("DEPENDENCY_CONFLICT", "WireGuard имеет динамический адрес; сначала измените создающий его сервис") + // Detect literal local-address references even when a protocol does not name its interface. + const ipKey = (ip: string) => isIP(ip) ? canonicalBgpPolicyPrefix(ip + (isIP(ip) === 4 ? "/32" : "/128")) : ip + const local = new Set(addresses.map(row => row.address.split("/")[0]).filter(ip => isIP(ip) !== 0).map(ipKey)) + const addressFields = new Set(["local-address", "local.address", "pref-src", "src-address", "gateway"]) + for (const [path, rows] of Object.entries(state.dependencies)) if (rows.some(row => !(observedBoolean(row.dynamic) === true && ["/ip/route", "/ipv6/route"].includes(path) && (row.connect === "true" || row["belongs-to"]?.startsWith("connected"))) && Object.entries(row).some(([key, value]) => addressFields.has(key) && value.split(/[,\s%/]+/).some(part => local.has(ipKey(part)))))) throw fail("DEPENDENCY_CONFLICT", "Локальный адрес WireGuard используется конфигурацией протокола") + for (const template of state.dependencies["/routing/ospf/interface-template"]) { + const interfaces = (template.interfaces ?? "").split(",").map(value => value.trim()).filter(Boolean) + if (interfaces.length && !interfaces.some(value => ["all", target.name, id].includes(value))) continue + for (const network of (template.networks ?? "").split(",").map(value => value.trim()).filter(Boolean)) { + try { + const expected = canonicalBgpPolicyPrefix(network), mask = expected.split("/")[1] + if (addresses.some(row => isIP(row.address.split("/")[0]) === isIP(expected.split("/")[0]) && canonicalBgpPolicyPrefix(row.address.split("/")[0] + "/" + mask) === expected)) throw fail("DEPENDENCY_CONFLICT", "Сеть адреса WireGuard используется шаблоном OSPF") + } catch (error) { if (error instanceof McpError) throw error; throw fail("STATE_UNAVAILABLE", "Не подтверждён сетевой selector OSPF") } + } + } + if (!cascade && (peers.length || addresses.some(row => !automaticLinkLocal(row)))) throw fail("DEPENDENCY_CONFLICT", "Native DELETE не удаляет peers/адреса каскадно; используйте отдельный явный сервисный план") + const commands: WireguardInterfaceDeleteCommand[] = [] + if (cascade && observedBoolean(target.disabled) === false) commands.push({ method: "PATCH", path: "/interface/wireguard", resource: "interfaces", id, body: { disabled: "true" } }) + for (const peer of peers) commands.push({ method: "DELETE", path: "/interface/wireguard/peers", resource: "peers", id: peer[".id"] }) + for (const [path, resource, rows] of [["/ip/address", "ipv4", state.ipv4], ["/ipv6/address", "ipv6", state.ipv6]] as const) for (const row of rows.filter(row => belongs(row) && observedBoolean(row.dynamic) === false)) commands.push({ method: "DELETE", path, resource, id: row[".id"] }) + commands.push({ method: "DELETE", path: "/interface/wireguard", resource: "interfaces", id }) + if (commands.length > 50) throw fail("STATE_UNAVAILABLE", "Каскад WireGuard превышает 50 команд; сначала уменьшите число зависимых объектов") + return commands +} +export function projectWireguardInterfaceDeleteCommand(state: WireguardInterfaceDeleteState, command: WireguardInterfaceDeleteCommand): WireguardInterfaceDeleteState { + const next = structuredClone(state) + const rows = command.resource === "interfaces" ? next.base.interfaces : command.resource === "peers" ? next.base.peers : next[command.resource] + const target = rows.find(row => row[".id"] === command.id) + if (!target) throw fail("STATE_CONFLICT", "Не найден объект запланированной команды WireGuard") + if (command.method === "PATCH") Object.assign(target, command.body) + else rows.splice(rows.indexOf(target), 1) + if (command.resource === "interfaces" && command.method === "DELETE") next.dependencies["/interface"] = next.dependencies["/interface"].filter(row => row[".id"] !== command.id && row.name !== target.name) + return next +} +export async function deleteWireguardInterfaceGuarded(server: ServerRow, id: string, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: boolean; cascade?: boolean } = {}) { + if (!/^\*[a-z0-9]+$/i.test(id) || id.length > 100) throw new McpError("INVALID_INPUT", "Требуется стабильный ID WireGuard", 400) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "wireguard", id }, [server.id], async () => { + let state = await readWireguardInterfaceDeleteState(server, id, authorize) + if (expectedProof && state.proof !== expectedProof) throw fail("STATE_CONFLICT", "WireGuard или зависимости изменились после подготовки") + const commands = planWireguardInterfaceDelete(state, id, options.cascade !== false), client = MikrotikClient.fromServer(server) + let completedCommands = 0 + for (const command of commands) { + const fresh = await readWireguardInterfaceDeleteState(server, id, authorize) + if (fresh.proof !== state.proof) throw fail(completedCommands ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "WireGuard изменился между командами; выполнение остановлено") + planWireguardInterfaceDelete(fresh, id, options.cascade !== false) + const projected = projectWireguardInterfaceDeleteCommand(fresh, command), desiredProof = await wireguardInterfaceDeleteProof(projected) + await source(server, authorize) + try { + const path = command.path + "/" + encodeURIComponent(command.id) + if (command.method === "PATCH") await client.patch(path, command.body!) + else await client.delete(path) + } catch (error) { if (error instanceof McpError) throw error; throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход команды WireGuard не подтверждён; каскад остановлен без повтора") } + try { state = await readWireguardInterfaceDeleteState(server, id, authorize) } + catch { throw fail("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена, но сверка WireGuard недоступна; каскад остановлен без повтора") } + if (state.proof !== desiredProof) throw fail("VERIFICATION_FAILED", "Команда WireGuard или сохранность остальной конфигурации не подтверждена; каскад остановлен") + completedCommands++ + } + let revisionRecorded = false + try { if (options.recordRevision !== false) revisionRecorded = await captureAndAppendRevision({ serverId: server.id, section: "wireguard", source: "apply", capture: () => captureWireguardSnapshot(server, () => source(server, authorize)) }) !== null } catch { /* Never replay native commands for revision storage. */ } + return { ok: true, verified: true, interfaceId: id, commandCount: commands.length, completedCommands, afterProof: state.proof, revisionRecorded, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/wireguard-interface-update.test.ts b/backend/src/services/wireguard-interface-update.test.ts new file mode 100644 index 00000000..e715c641 --- /dev/null +++ b/backend/src/services/wireguard-interface-update.test.ts @@ -0,0 +1,126 @@ +import assert from "node:assert/strict" +import { fileURLToPath } from "node:url" +import { wireguardInterfaceDependencyPaths, wireguardInterfaceDeleteProof, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { wireguardPublicKey } from "./wireguard-interface-create.js" +import { planWireguardInterfaceUpdate, projectWireguardInterfaceUpdate, wireguardInterfaceUpdateSchema, wireguardInterfaceUpdateProof } from "./wireguard-interface-update.js" +import { wireguardInterfaceUpdatePlanSchema } from "../modules/mcp/wireguard-interface-update-actions.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" + +export async function acceptWireguardInterfaceUpdateModel() { + const endpoint = { id: 136, enabled: true, host: "127.0.0.1", port: 443, username: "admin", password: "NEVER_ENDPOINT_PASSWORD", useSsl: true, verifySsl: true, routingEndpointEpoch: "35adfb60-9da1-44fa-b699-9ce1a32371bf", name: "before", updatedAt: new Date(0) } as ServerRow + assert.equal(sameWireguardEndpoint({ ...endpoint, name: "after", comment: "metadata", updatedAt: new Date() }, endpoint), true) + assert.equal(sameWireguardEndpoint({ ...endpoint, routingEndpointEpoch: "ae5e2528-fe50-483b-a128-dc8ad90de36f" }, endpoint), false) + assert.equal(sameWireguardEndpoint({ ...endpoint, password: "changed" }, endpoint), false) + const privateKey = Buffer.alloc(32, 61).toString("base64"), nextKey = Buffer.alloc(32, 62).toString("base64") + const state: WireguardInterfaceDeleteState = { base: { interfaces: [{ ".id": "*a", name: "wg-owned", disabled: "false", mtu: "1420", "listen-port": "13231", "private-key": privateKey, "public-key": wireguardPublicKey(privateKey), opaque: "NEVER_OPAQUE" }], peers: [{ ".id": "*b", interface: "wg-owned", "public-key": wireguardPublicKey(nextKey), disabled: "false", "allowed-address": "192.0.2.2/32" }], bindings: [], flow: { hostPublicKey: "", registered: false }, serverProof: "source", proof: "base" }, ipv4: [{ ".id": "*c", interface: "wg-owned", address: "192.0.2.1/24", disabled: "false", dynamic: "false" }, { ".id": "*d", interface: "wg-owned", address: "198.51.100.1/24", disabled: "false", dynamic: "false", comment: "secondary" }], ipv6: [], dependencies: Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])), filters: [], proof: "" } + state.dependencies["/interface"].push({ ".id": "*a", name: "wg-owned", type: "wg" }) + state.proof = await wireguardInterfaceDeleteProof(state) + const beforeProofNormalization = structuredClone(state), scalarProof = await wireguardInterfaceUpdateProof(state) + const originalBytes = Buffer.from(privateKey, "base64"), equivalentKeys: string[] = [] + for (let ignored = 0; ignored < 32; ignored++) { + const bytes = Buffer.from(originalBytes) + bytes[0] = (bytes[0] & 248) | (ignored & 7) + bytes[31] = (bytes[31] & 63) | ((ignored >> 3) << 6) + const equivalent = bytes.toString("base64"); equivalentKeys.push(equivalent) + assert.equal(wireguardPublicKey(equivalent), state.base.interfaces[0]["public-key"]) + const changed = structuredClone(state); changed.base.interfaces[0]["private-key"] = equivalent + assert.equal(await wireguardInterfaceUpdateProof(changed), scalarProof, "All 32 RFC 7748 ignored-bit combinations prove one observed identity") + changed.dependencies["/interface"][0]["private-key"] = privateKey + assert.equal(await wireguardInterfaceUpdateProof(changed), scalarProof, "Corroborated generic key mirrors may differ only in ignored scalar bits") + changed.dependencies["/interface"][0]["private-key"] = equivalent + changed.base.interfaces[0]["private-key"] = privateKey + assert.equal(await wireguardInterfaceUpdateProof(changed), scalarProof) + } + assert.deepEqual(state, beforeProofNormalization, "Proof normalization must not rewrite captured or native secret material") + const meaningfulBytes = Buffer.from(originalBytes); meaningfulBytes[1] ^= 1 + const meaningfulKey = meaningfulBytes.toString("base64"), inconsistent = structuredClone(state) + inconsistent.base.interfaces[0]["private-key"] = meaningfulKey + assert.notEqual(await wireguardInterfaceUpdateProof(inconsistent), scalarProof) + const rotated = structuredClone(inconsistent); rotated.base.interfaces[0]["public-key"] = wireguardPublicKey(meaningfulKey) + assert.notEqual(await wireguardInterfaceUpdateProof(rotated), scalarProof, "An actual key rotation remains visible") + for (const publicKey of [undefined, "invalid", wireguardPublicKey(nextKey)]) { + const first = structuredClone(state), second = structuredClone(state) + if (publicKey === undefined) { delete first.base.interfaces[0]["public-key"]; delete second.base.interfaces[0]["public-key"] } + else { first.base.interfaces[0]["public-key"] = publicKey; second.base.interfaces[0]["public-key"] = publicKey } + first.base.interfaces[0]["private-key"] = equivalentKeys[0]; second.base.interfaces[0]["private-key"] = equivalentKeys[1] + assert.notEqual(await wireguardInterfaceUpdateProof(first), await wireguardInterfaceUpdateProof(second), "A missing or inconsistent public identity cannot authorize key normalization") + second.dependencies["/interface"][0]["private-key"] = equivalentKeys[0] + await assert.rejects(wireguardInterfaceUpdateProof(second), "An inconsistent generic private mirror cannot be normalized") + } + const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", noncanonical = privateKey.slice(0, -2) + alphabet[alphabet.indexOf(privateKey.at(-2)!) ^ 1] + "=" + assert.deepEqual(Buffer.from(noncanonical, "base64"), originalBytes) + const opaqueKeys: Array = [undefined, "", "********", "(hidden)", "invalid-key", noncanonical, Buffer.alloc(32).toString("base64")] + const opaqueProofs: string[] = [] + for (const opaque of opaqueKeys) { + const changed = structuredClone(state) + if (opaque === undefined) delete changed.base.interfaces[0]["private-key"]; else changed.base.interfaces[0]["private-key"] = opaque + const unchanged = structuredClone(changed), proof = await wireguardInterfaceUpdateProof(changed) + assert.equal(await wireguardInterfaceUpdateProof(changed), proof); assert.deepEqual(changed, unchanged) + opaqueProofs.push(proof) + } + assert.equal(new Set(opaqueProofs).size, opaqueKeys.length, "Missing, invalid and unknown key representations remain exact opaque state") + assert.ok(opaqueProofs.every(proof => proof !== scalarProof)) + const wrongGeneric = structuredClone(state); wrongGeneric.dependencies["/interface"][0]["private-key"] = meaningfulKey + await assert.rejects(wireguardInterfaceUpdateProof(wrongGeneric)) + const opaqueGeneric = structuredClone(state); opaqueGeneric.base.interfaces[0]["private-key"] = "(hidden)"; opaqueGeneric.dependencies["/interface"][0]["private-key"] = "(hidden)" + await wireguardInterfaceUpdateProof(opaqueGeneric) + opaqueGeneric.dependencies["/interface"][0]["private-key"] = "********" + await assert.rejects(wireguardInterfaceUpdateProof(opaqueGeneric)) + const changedOpaque = structuredClone(state); changedOpaque.base.interfaces[0].opaque = equivalentKeys[0] + assert.notEqual(await wireguardInterfaceUpdateProof(changedOpaque), scalarProof) + const all = { name: "renamed", mtu: 1380, listenPort: 23456, comment: "updated", disabled: true, privateKey: nextKey } + const commands = planWireguardInterfaceUpdate(state, "*a", all) + assert.equal(commands.length, 1) + const next = projectWireguardInterfaceUpdate(state, commands[0]) + const beforeProjectedProof = structuredClone(next) + await wireguardInterfaceUpdateProof(next) + assert.deepEqual(next, beforeProjectedProof); assert.equal(commands[0].body?.["private-key"], nextKey); assert.equal(next.base.interfaces[0]["private-key"], nextKey) + assert.equal(next.base.interfaces[0]["public-key"], wireguardPublicKey(nextKey)) + assert.equal(next.base.interfaces[0].opaque, "NEVER_OPAQUE") + assert.equal(next.base.peers[0].interface, "renamed") + assert.equal(next.ipv4[0].interface, "renamed") + assert.equal(next.dependencies["/interface"][0].name, "renamed") + assert.notEqual(await wireguardInterfaceDeleteProof(next), state.proof) + const generic = structuredClone(state) + generic.base.interfaces.push({ ".id": "*f", name: "wg-foreign", disabled: "true", mtu: "1420" }) + generic.dependencies["/interface"].push({ ".id": "*f", name: "wg-foreign", type: "wg", mtu: "1420", disabled: "yes", opaque: "NEVER_FOREIGN_GENERIC" }) + const genericProof = await wireguardInterfaceUpdateProof(generic) + const foreignKey = structuredClone(generic) + foreignKey.base.interfaces[1]["private-key"] = privateKey; foreignKey.base.interfaces[1]["public-key"] = wireguardPublicKey(privateKey) + const foreignProof = await wireguardInterfaceUpdateProof(foreignKey) + foreignKey.base.interfaces[1]["private-key"] = meaningfulKey + assert.notEqual(await wireguardInterfaceUpdateProof(foreignKey), foreignProof, "Foreign private material cannot drift under an unchanged public identity") + foreignKey.base.interfaces[1]["public-key"] = wireguardPublicKey(meaningfulKey) + assert.notEqual(await wireguardInterfaceUpdateProof(foreignKey), foreignProof, "Foreign public identity and real key rotation remain sealed") + generic.dependencies["/interface"][1]["rx-byte"] = "1234"; generic.dependencies["/interface"][1]["fp-rx-byte"] = "5678" + assert.equal(await wireguardInterfaceUpdateProof(generic), genericProof) + generic.dependencies["/interface"][1].opaque = "NEVER_CHANGED_FOREIGN" + assert.notEqual(await wireguardInterfaceUpdateProof(generic), genericProof) + generic.dependencies["/interface"][1].mtu = "1380" + await assert.rejects(wireguardInterfaceUpdateProof(generic)) + const mirrors = structuredClone(state); Object.assign(mirrors.dependencies["/interface"][0], { mtu: "1420", disabled: "no" }) + const projectedMirrors = projectWireguardInterfaceUpdate(mirrors, commands[0]) + assert.equal(projectedMirrors.dependencies["/interface"][0].mtu, "1380"); assert.equal(projectedMirrors.dependencies["/interface"][0].disabled, "true") + await wireguardInterfaceUpdateProof(projectedMirrors) + const address = planWireguardInterfaceUpdate(state, "*a", { address: "2001:db8::1/64" }) + assert.deepEqual(address.map(command => command.method), ["DELETE", "PUT"]) + assert.equal(address[0].id, "*c"); assert.equal(address[1].body?.advertise, "false") + const removed = projectWireguardInterfaceUpdate(state, address[0]), added = projectWireguardInterfaceUpdate(removed, address[1], "*e") + assert.equal(added.ipv4.length, 1); assert.equal(added.ipv4[0].comment, "secondary"); assert.equal(added.ipv6[0].address, "2001:db8::1/64") + assert.deepEqual(planWireguardInterfaceUpdate(state, "*a", { address: "" }).map(command => command.id), ["*c"]) + assert.deepEqual(planWireguardInterfaceUpdate(state, "*a", { address: "198.51.100.1/24" }).map(command => command.id), ["*c"]) + assert.equal(planWireguardInterfaceUpdate(state, "*a", { address: "192.0.2.1/24" }).length, 0) + for (const bad of [{}, { name: "\u0000bad" }, { listenPort: 65536 }, { mtu: 0 }, { privateKey: "bad" }, { unknown: true }]) assert.equal(wireguardInterfaceUpdateSchema.safeParse(bad).success, false) + assert.equal(wireguardInterfaceUpdatePlanSchema.safeParse({ serverId: 136, interfaceId: "*a", patch: { privateKey: nextKey } }).success, false) + assert.equal(wireguardInterfaceUpdatePlanSchema.safeParse({ serverId: 136, interfaceId: "*a", patch: { privateKeyRef: "b9ae94ed-5d23-4edb-b71b-60207a110c47" } }).success, true) + assert.throws(() => planWireguardInterfaceUpdate(state, "*a", { name: "renamed", address: "192.0.2.3/24" })) + for (const mutate of [ + (value: typeof state) => { value.dependencies["/interface/vlan"].push({ ".id": "*f", interface: "wg-owned" }) }, + (value: typeof state) => { value.base.interfaces[0].comment = "MikrotikManager traffic-flow overlay" }, + (value: typeof state) => { value.dependencies["/interface"][0][".id"] = "*f" }, + (value: typeof state) => { value.ipv4[0].dynamic = "true" }, + ]) { const changed = structuredClone(state); mutate(changed); assert.throws(() => planWireguardInterfaceUpdate(changed, "*a", { disabled: true })) } + console.log("WireGuard interface update model: all 32 corroborated RFC7748 scalar encodings, exact opaque/invalid keys, generic mirrors, real/foreign rotation, immutable native material; full fields, rename children, addresses and dependency protections checked") +} +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) await acceptWireguardInterfaceUpdateModel() diff --git a/backend/src/services/wireguard-interface-update.ts b/backend/src/services/wireguard-interface-update.ts new file mode 100644 index 00000000..ff50a4ee --- /dev/null +++ b/backend/src/services/wireguard-interface-update.ts @@ -0,0 +1,162 @@ +import { z } from "zod" +import { wgPatchInterfaceSchema } from "@mmapp/contracts/wireguard" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError, privateFingerprinter } from "../modules/mcp/security.js" +import { readWireguardInterfaceDeleteState, planWireguardInterfaceDelete, wireguardInterfaceDeleteProof, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { wireguardCreateKey, wireguardPublicKey, wireguardCreateProtectionProof, assertWireguardCreateAllowed } from "./wireguard-interface-create.js" +import { observedBoolean, observedInteger } from "./ospf-observed-values.js" +import { interfaceAddressKey, interfaceAddressPath } from "./entity-snapshots.js" +import { MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { greStableRows } from "./gre-mutation-state.js" +import { wireguardConfigurationRows } from "./wireguard-peer-delete.js" +import { wireguardCorroboratedPrivateScalar, wireguardPrivateScalarsEqual } from "./wireguard-key-scalar.js" + +const text = z.string().max(1000).refine(value => !/[\u0000-\u001f\u007f]/.test(value)) +export const wireguardInterfaceUpdateSchema = wgPatchInterfaceSchema.extend({ name: text.min(1).max(64).refine(value => value.trim().length > 0).optional(), listenPort: z.number().int().min(1).max(65535).optional(), mtu: z.number().int().min(1).max(65535).optional(), comment: text.optional(), privateKey: wireguardCreateKey.optional() }).strict().refine(value => Object.values(value).some(item => item !== undefined), "Нет полей для изменения") +export type WireguardInterfaceUpdate = z.infer +export function wireguardInterfaceNativeUpdate(fields: Record) { + const mapping: Record = { name: "name", "listen-port": "listenPort", mtu: "mtu", comment: "comment", disabled: "disabled" }, patch: Record = {} + for (const [key, value] of Object.entries(fields)) { + if (!mapping[key]) throw new McpError("UNSUPPORTED_CAPABILITY", "Поле native WireGuard не поддерживается общим планом", 409) + patch[mapping[key]] = key === "disabled" && typeof value === "string" ? observedBoolean(value) ?? value : ["mtu", "listen-port"].includes(key) && typeof value === "string" && /^\d+$/.test(value) ? Number(value) : value + } + return wireguardInterfaceUpdateSchema.parse(patch) +} +type State = WireguardInterfaceDeleteState +export interface WireguardInterfaceUpdateCommand { method: "PATCH" | "DELETE" | "PUT"; path: string; resource: "interfaces" | "ipv4" | "ipv6"; id?: string; body?: Record } +const fail = (code: string, message: string) => new McpError(code, message, code === "INVALID_INPUT" ? 400 : ["STATE_UNAVAILABLE", "MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(code) ? 502 : 409) +const genericMirrors = new Set(["mtu", "listen-port", "disabled", "comment", "public-key", "private-key"]) +function privateMirrorMatches(observed: string, expected: string | undefined, publicKey: string | undefined) { + if (observed === expected) return true + return wireguardPrivateScalarsEqual(observed, expected, publicKey) +} +function genericWireguardConfiguration(state: State) { + const result: Record[] = [] + for (const parent of state.base.interfaces) { + const rows = state.dependencies["/interface"].filter(row => row[".id"] === parent[".id"] || row.name === parent.name) + if (rows.length !== 1 || rows[0][".id"] !== parent[".id"] || rows[0].name !== parent.name) throw fail("STATE_CONFLICT", "Расходятся общий и WireGuard каталоги интерфейсов") + const generic = rows[0] + for (const key of genericMirrors) if (generic[key] !== undefined) { + const expected = parent[key] ?? (key === "comment" ? "" : undefined) + const equal = key === "private-key" ? privateMirrorMatches(generic[key], expected, parent["public-key"]) : key === "disabled" ? observedBoolean(generic[key]) !== null && observedBoolean(generic[key]) === observedBoolean(expected ?? "") : ["mtu", "listen-port"].includes(key) ? observedInteger(generic[key], 1, 65535) !== null && observedInteger(generic[key], 1, 65535) === observedInteger(expected ?? "", 1, 65535) : expected !== undefined && generic[key] === expected + if (!equal) throw fail("STATE_UNAVAILABLE", "Противоречат конфигурационные свойства общего и WireGuard каталогов") + } + // Mirrors are independently validated against the complete WG rows. Seal every other stable generic property. + result.push(Object.fromEntries(Object.entries(generic).filter(([key]) => !genericMirrors.has(key) && !["fp-rx-byte", "fp-tx-byte", "fp-rx-packet", "fp-tx-packet", "rx-drop", "tx-drop", "rx-error", "tx-error", "tx-queue-drop"].includes(key)))) + } + return greStableRows(wireguardConfigurationRows(result)) +} +export async function wireguardInterfaceUpdateProof(state: State) { + // Normalize a private proof-only clone. Never alter captured source rows, secret references or native command bodies. + const normalized = structuredClone(state) + for (const row of normalized.base.interfaces) { + const scalar = wireguardCorroboratedPrivateScalar(row["private-key"], row["public-key"]) + if (scalar !== undefined) row["private-key"] = scalar + } + return (await privateFingerprinter("wireguard-interface-update-v2"))(fingerprint({ configuration: await wireguardInterfaceDeleteProof(normalized), genericInterfaces: genericWireguardConfiguration(state) })) +} +export async function readWireguardInterfaceUpdateState(server: ServerRow, id: string, authorize: () => Promise = async () => {}) { + const state = await readWireguardInterfaceDeleteState(server, id, authorize) + state.proof = await wireguardInterfaceUpdateProof(state) + return state +} +export function planWireguardInterfaceUpdate(state: State, id: string, raw: unknown): WireguardInterfaceUpdateCommand[] { + const parsed = wireguardInterfaceUpdateSchema.safeParse(raw) + if (!parsed.success) throw fail("INVALID_INPUT", "Некорректные поля изменения WireGuard") + const input = parsed.data + // Also validates stable identity, ownership, all parent dependencies and child protections. + planWireguardInterfaceDelete(state, id) + const target = state.base.interfaces.find(row => row[".id"] === id)! + if (input.name === "wg-flow" || input.comment === "MikrotikManager traffic-flow overlay") throw fail("PROTECTED_OBJECT", "Имя и комментарий зарезервированы NetFlow") + if (input.name && input.name !== target.name) { + if (input.address !== undefined) throw fail("STATE_CONFLICT", "Изменяйте имя и адрес WireGuard отдельными операциями") + assertWireguardCreateAllowed(state, { serverId: 1, name: input.name, listenPort: 13231, mtu: 1420 }) + } + const fields: Record = {} + for (const [name, key] of [["name", "name"], ["listenPort", "listen-port"], ["mtu", "mtu"], ["comment", "comment"], ["privateKey", "private-key"], ["disabled", "disabled"]] as const) if (input[name] !== undefined) fields[key] = String(input[name]) + const commands: WireguardInterfaceUpdateCommand[] = Object.keys(fields).length ? [{ method: "PATCH", path: "/interface/wireguard", resource: "interfaces", id, body: fields }] : [] + if (input.address !== undefined) { + const belongs = (row: Record) => [target.name, id].includes(row.interface) + const all = [...state.ipv4, ...state.ipv6].filter(belongs).sort((a, b) => Number(observedBoolean(a.disabled) === true) - Number(observedBoolean(b.disabled) === true) || a.address.localeCompare(b.address)) + const statics = all.filter(row => observedBoolean(row.dynamic) === false) + if (statics.some(row => Boolean(row["from-pool"]) || observedBoolean(row["eui-64"]) === true)) throw fail("DEPENDENCY_CONFLICT", "Адрес из IPv6-пула или EUI-64 требует отдельной настройки") + const primary = statics[0] + const secondary = statics.filter(row => row.address !== primary?.address) + const desired = input.address ? interfaceAddressKey(input.address) : null + for (const row of statics) if (!secondary.includes(row) && (desired === null || interfaceAddressKey(row.address) !== desired)) commands.push({ method: "DELETE", path: interfaceAddressPath(row.address), resource: interfaceAddressPath(row.address) === "/ip/address" ? "ipv4" : "ipv6", id: row[".id"] }) + const retainedPrimary = statics.find(row => !secondary.includes(row) && desired !== null && interfaceAddressKey(row.address) === desired) + if (retainedPrimary && (observedBoolean(retainedPrimary.disabled) !== false || interfaceAddressPath(retainedPrimary.address) === "/ipv6/address" && observedBoolean(retainedPrimary.advertise) !== false)) commands.push({ method: "PATCH", path: interfaceAddressPath(retainedPrimary.address), resource: interfaceAddressPath(retainedPrimary.address) === "/ip/address" ? "ipv4" : "ipv6", id: retainedPrimary[".id"], body: { disabled: "false", ...(interfaceAddressPath(retainedPrimary.address) === "/ipv6/address" ? { advertise: "false" } : {}) } }) + if (input.address && !statics.some(row => interfaceAddressKey(row.address) === desired)) { + // Check literal address collisions and selectors without treating the existing interface as a new object. + const check = structuredClone(state); check.base.interfaces = check.base.interfaces.filter(row => row[".id"] !== id); check.dependencies["/interface"] = check.dependencies["/interface"].filter(row => row[".id"] !== id) + assertWireguardCreateAllowed(check, { serverId: 1, name: target.name, listenPort: 13231, mtu: 1420, address: input.address }) + commands.push({ method: "PUT", path: interfaceAddressPath(input.address), resource: interfaceAddressPath(input.address) === "/ip/address" ? "ipv4" : "ipv6", body: { address: input.address, interface: target.name, disabled: "false", ...(interfaceAddressPath(input.address) === "/ipv6/address" ? { advertise: "false" } : {}) } }) + } + } + return commands +} +export function projectWireguardInterfaceUpdate(state: State, command: WireguardInterfaceUpdateCommand, createdId?: string) { + const next = structuredClone(state), rows = command.resource === "interfaces" ? next.base.interfaces : next[command.resource] + if (command.method === "PUT") { if (!createdId) throw fail("STATE_UNAVAILABLE", "Не подтверждён ID нового адреса"); rows.push({ ".id": createdId, dynamic: "false", ...command.body! }); return next } + const target = rows.find(row => row[".id"] === command.id) + if (!target) throw fail("STATE_CONFLICT", "Объект команды WireGuard исчез") + if (command.method === "DELETE") rows.splice(rows.indexOf(target), 1) + else { + const oldName = target.name + const privateKeyObserved = target["private-key"] !== undefined + Object.assign(target, command.body) + if (command.body?.["private-key"]) { + target["public-key"] = wireguardPublicKey(command.body["private-key"]) + // Public-key derivation proves the replacement even on sources that omit private material. + if (!privateKeyObserved) delete target["private-key"] + } + if (command.body?.name && command.body.name !== oldName) { + for (const row of [...next.base.peers, ...next.ipv4, ...next.ipv6]) if (row.interface === oldName) row.interface = command.body.name + const generic = next.dependencies["/interface"].find(row => row[".id"] === command.id); if (generic) generic.name = command.body.name + } + if (command.resource === "interfaces") { + const generic = next.dependencies["/interface"].find(row => row[".id"] === command.id) + if (generic) for (const key of genericMirrors) if (generic[key] !== undefined && target[key] !== undefined) generic[key] = target[key] + } + } + return next +} +export async function updateWireguardInterface(server: ServerRow, id: string, raw: unknown, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: boolean } = {}) { + const parsed = wireguardInterfaceUpdateSchema.safeParse(raw) + if (!parsed.success) throw fail("INVALID_INPUT", "Некорректные поля изменения WireGuard") + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "wireguard", id }, [server.id], async () => { + const protections = await wireguardCreateProtectionProof(server.id) + const source = async () => { await authorize(); const current = await getServerRowById(server.id); if (!current || !sameWireguardEndpoint(current, server) || await wireguardCreateProtectionProof(server.id) !== protections) throw fail("STATE_CONFLICT", "Источник или защиты WireGuard изменились") } + let state = await readWireguardInterfaceUpdateState(server, id, source) + if (expectedProof && expectedProof !== state.proof) throw fail("STATE_CONFLICT", "WireGuard изменился после подготовки") + const commands = planWireguardInterfaceUpdate(state, id, parsed.data), client = MikrotikClient.fromServer(server) + let completedCommands = 0 + for (const command of commands) { + const fresh = await readWireguardInterfaceUpdateState(server, id, source) + if (fresh.proof !== state.proof) throw fail(completedCommands ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "WireGuard изменился между командами") + const fence = async () => { const current = await readWireguardInterfaceUpdateState(server, id, source); if (current.proof !== fresh.proof) throw fail("STATE_CONFLICT", "WireGuard изменился после native intent"); await source() } + await source() + try { const path = command.path + (command.id ? "/" + encodeURIComponent(command.id) : ""); if (command.method === "PATCH") await client.patch(path, command.body!, 15000, fence); else if (command.method === "PUT") await client.put(path, command.body!, 15000, fence); else await client.delete(path, 15000, fence) } + catch (error) { if (error instanceof McpError) throw error; throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход команды WireGuard неизвестен; повтор запрещён") } + let after: State + try { after = await readWireguardInterfaceUpdateState(server, id, source) } catch { throw fail("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена, read-back недоступен; повтор запрещён") } + try { planWireguardInterfaceDelete(after, id) } catch { throw fail("VERIFICATION_FAILED", "Идентичность или зависимости WireGuard изменились после команды; повтор запрещён") } + let projected: State + if (command.method === "PUT") { + const added = after[command.resource as "ipv4" | "ipv6"].filter(row => !fresh[command.resource as "ipv4" | "ipv6"].some(old => old[".id"] === row[".id"])) + if (added.length !== 1 || observedBoolean(added[0].dynamic) !== false || Object.entries(command.body!).some(([key, value]) => key === "address" ? interfaceAddressKey(added[0][key]) !== interfaceAddressKey(value) : ["disabled", "advertise"].includes(key) ? observedBoolean(added[0][key]) !== observedBoolean(value) : added[0][key] !== value && !(key === "interface" && added[0][key] === id))) throw fail("VERIFICATION_FAILED", "Новый адрес WireGuard не подтверждён; повтор запрещён") + projected = structuredClone(fresh); projected[command.resource as "ipv4" | "ipv6"].push(added[0]) + } else projected = projectWireguardInterfaceUpdate(fresh, command) + if (await wireguardInterfaceUpdateProof(projected) !== after.proof) throw fail("VERIFICATION_FAILED", "Изменение WireGuard или сохранность зависимостей не подтверждены; повтор запрещён") + state = after; completedCommands++ + } + let revisionRecorded = false + try { if (options.recordRevision !== false) revisionRecorded = await captureAndAppendRevision({ serverId: server.id, section: "wireguard", source: "apply", capture: () => captureWireguardSnapshot(server, source) }) !== null } catch { /* A verified mutation is never replayed for history storage. */ } + return { ok: true, verified: true, interfaceId: id, afterProof: state.proof, commandCount: commands.length, completedCommands, revisionRecorded, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/wireguard-ipv6.test.ts b/backend/src/services/wireguard-ipv6.test.ts index 8aeb6f11..d305ac73 100644 --- a/backend/src/services/wireguard-ipv6.test.ts +++ b/backend/src/services/wireguard-ipv6.test.ts @@ -31,6 +31,19 @@ try { assert(ops.some(op => op.op === "delete" && op.path === "/ipv6/address/*2")) assert(!ops.some(op => op.path.endsWith("/*3"))) assert(ops.some(op => op.op === "put" && op.path === "/ipv6/address" && op.body.advertise === "no")) + raw["/interface"] = [{ ".id": "*generic", name: "wg-own", type: "wg" }, { ".id": "*w", name: "ether1", type: "ether" }] + raw["/interface/wireguard/peers"] = [{ ".id": "*p", interface: "*generic", "public-key": "public", "preshared-key": "NEVER_IN_REVISION" }] + raw["/ip/address"] = [{ ".id": "*1", interface: "*generic", address: "10.1.0.1/30" }, { ".id": "*foreign", interface: "*w", address: "192.0.2.1/24" }] + const byId = await fetchWireguardRestoreState({ id: 1 } as Parameters[0]) + assert(calls.includes("/interface")) + assert.equal(byId.snapshot.interfaces[0].peers[0]?.presharedKeyConfigured, true) + assert.equal(JSON.stringify(byId.snapshot).includes("NEVER_IN_REVISION"), false) + assert.equal(byId.snapshot.interfaces[0].innerAddresses?.some(row => row.address === "10.1.0.1/30"), true) + assert.equal(byId.snapshot.interfaces[0].innerAddresses?.some(row => row.address === "192.0.2.1/24"), false) + raw["/interface"] = [{ ".id": "*generic", name: "wg-own", type: "ether" }] + await assert.rejects(fetchWireguardRestoreState({ id: 1 } as Parameters[0]), /не подтверждена ссылка/) + raw["/interface"] = [] + await assert.rejects(fetchWireguardRestoreState({ id: 1 } as Parameters[0]), /не подтверждена ссылка/) await putIpAddress({ put: async (path: string, body: Record) => { assert.equal(path, "/ipv6/address"); assert.equal(body.advertise, "no") } } as unknown as MikrotikClient, "fd00::1/64", "wg-own") MikrotikClient.fromServer = () => ({ get: async (path: string) => { if (path === "/ipv6/address") throw new Error("Unauthorized"); return [] } }) as unknown as MikrotikClient await assert.rejects(fetchWireguardRestoreState({ id: 1 } as Parameters[0]), /Unauthorized/) @@ -40,7 +53,7 @@ const rsc = generateMikrotikRsc({ name: "wg-own", mtu: 1400, address: "fd00:152: assert(rsc.includes("/ipv6 address add")) assert(rsc.includes("advertise=no")) assert.equal(parseWireguardImport(rsc, "rsc").interface.address, "fd00:152::1/64") -assert.throws(() => parseWireguardImport(rsc.replace("interface=wg-own", "interface=foreign"), "rsc"), /другому интерфейсу/) +assert.throws(() => parseWireguardImport(rsc.replace('interface="wg-own"', 'interface="foreign"'), "rsc"), /другому интерфейсу/) assert.throws(() => parseWireguardImport(rsc + "\n/ipv6 address add address=fd00::2/64 interface=wg-own", "rsc"), /нескольких/) assert.throws(() => parseWireguardImport(rsc.replace("advertise=no", "advertise=yes"), "rsc"), /специальных/) for (const address of ["fd00::1", "fd00::1/129", "bad/64", "10.0.0.1/33"]) { diff --git a/backend/src/services/wireguard-key-scalar.ts b/backend/src/services/wireguard-key-scalar.ts new file mode 100644 index 00000000..3cda67cf --- /dev/null +++ b/backend/src/services/wireguard-key-scalar.ts @@ -0,0 +1,21 @@ +import { createPrivateKey, createPublicKey } from "node:crypto" + +/** Proof-only RFC 7748 decodeScalar25519. Never use the result to rewrite captured/native key material. */ +export function wireguardCorroboratedPrivateScalar(privateKey: string | undefined, publicKey: string | undefined): string | undefined { + if (typeof privateKey !== "string" || typeof publicKey !== "string" || !/^[A-Za-z0-9+/]{43}=$/.test(privateKey)) return undefined + const scalar = Buffer.from(privateKey, "base64") + if (scalar.length !== 32 || scalar.toString("base64") !== privateKey || !scalar.some(byte => byte !== 0)) return undefined + try { + const key = createPrivateKey({ key: Buffer.concat([Buffer.from("302e020100300506032b656e04220420", "hex"), scalar]), format: "der", type: "pkcs8" }) + if (createPublicKey(key).export({ format: "der", type: "spki" }).subarray(-32).toString("base64") !== publicKey) return undefined + } catch { return undefined } + scalar[0] &= 248 + scalar[31] = (scalar[31] & 127) | 64 + return scalar.toString("base64") +} + +/** Equality requires corroboration of both complete keys; invalid/unknown representations are never normalized. */ +export function wireguardPrivateScalarsEqual(first: string | undefined, second: string | undefined, observedPublic: string | undefined) { + const left = wireguardCorroboratedPrivateScalar(first, observedPublic), right = wireguardCorroboratedPrivateScalar(second, observedPublic) + return left !== undefined && right !== undefined && left === right +} diff --git a/backend/src/services/wireguard-live.ts b/backend/src/services/wireguard-live.ts index ec981a2f..c23e129a 100644 --- a/backend/src/services/wireguard-live.ts +++ b/backend/src/services/wireguard-live.ts @@ -5,6 +5,7 @@ import { db } from "../db/index.js" import { servers } from "../db/schema.js" import { parseDbServerId } from "../utils/server-id.js" import { MikrotikClient } from "./mikrotik.js" +import { nativeNetworkRows, networkSecretValues, observeWireGuardInterface, maskNetworkSecrets, type ObservedWireGuardInterface } from "./network-observed-read.js" import type { WgIfaceDto, WgPeerDto } from "@mmapp/contracts/wireguard" import { canonicalWireguardSnapshot, @@ -46,6 +47,7 @@ interface RosWireGuardPeer { "client-dns"?: string "client-endpoint"?: string "private-key"?: string + "preshared-key"?: string } interface RosIpAddress { @@ -126,7 +128,7 @@ function mapIface( async function fetchForServer( server: ServerRow, includePrivateKey: boolean, -): Promise { +): Promise<{ interfaces: WgIfaceDto[]; observed: ObservedWireGuardInterface[] }> { const client = MikrotikClient.fromServer(server) const [ifacesRaw, peersRaw, v4Raw, v6Raw] = await Promise.all([ client.get("/interface/wireguard"), @@ -135,12 +137,20 @@ async function fetchForServer( client.get("/ipv6/address"), ]) if (![ifacesRaw, peersRaw, v4Raw, v6Raw].every(Array.isArray)) throw new Error("RouterOS: неполный каталог WireGuard/адресов") - const addrsRaw = [...v4Raw, ...v6Raw].sort((a, b) => (a.address ?? "").localeCompare(b.address ?? "")) + nativeNetworkRows(ifacesRaw, [".id", "name"]); nativeNetworkRows(peersRaw, [".id", "interface"]) + nativeNetworkRows(v4Raw, [".id", "interface", "address"]); nativeNetworkRows(v6Raw, [".id", "interface", "address"]) + if (new Set(ifacesRaw.map(row => row.name)).size !== ifacesRaw.length) throw new Error("Повторяющиеся имена WireGuard") + const addrsRaw = [...v4Raw, ...v6Raw].map(row => { + const owners = ifacesRaw.filter(iface => iface.name === row.interface || iface[".id"] === row.interface) + if (owners.length > 1) throw new Error("Неоднозначный интерфейс адреса WireGuard") + return owners.length ? { ...row, interface: owners[0].name } : row + }).sort((a, b) => (a.address ?? "").localeCompare(b.address ?? "")) const peersByIface = new Map() peersRaw.forEach((p, idx) => { - const ifaceName = (p.interface ?? "").trim() - if (!ifaceName) return + const matches = ifacesRaw.filter(row => row.name === p.interface || row[".id"] === p.interface) + if (matches.length !== 1) throw new Error("Не подтверждён родитель WireGuard peer") + const ifaceName = matches[0].name! const list = peersByIface.get(ifaceName) ?? [] list.push(mapPeer(p, idx)) peersByIface.set(ifaceName, list) @@ -154,7 +164,7 @@ async function fetchForServer( if (iface && addr && !addrByIface.has(iface)) addrByIface.set(iface, addr) } - return ifacesRaw.map((w) => { + const interfaces = ifacesRaw.map((w) => { const name = (w.name ?? "").trim() return mapIface( server, @@ -164,9 +174,11 @@ async function fetchForServer( includePrivateKey, ) }) + const secrets = networkSecretValues([...ifacesRaw, ...peersRaw] as Record[]) + return { interfaces, observed: maskNetworkSecrets(interfaces.map((dto, index) => observeWireGuardInterface(dto, ifacesRaw[index] as Record, peersRaw as Record[], addrsRaw as Record[])), secrets) } } -export async function fetchWireguardRestoreState(server: ServerRow): Promise<{ +export async function fetchWireguardRestoreState(server: ServerRow, beforeRead: () => Promise = async () => {}): Promise<{ client: MikrotikClient ifaces: WgLiveIface[] peers: WgLivePeer[] @@ -174,11 +186,12 @@ export async function fetchWireguardRestoreState(server: ServerRow): Promise<{ snapshot: WgSnapshot }> { const client = MikrotikClient.fromServer(server) + const read = async (path: string) => { await beforeRead(); return client.get(path) } const [ifacesRaw, peersRaw, v4Raw, v6Raw] = await Promise.all([ - client.get("/interface/wireguard"), - client.get("/interface/wireguard/peers"), - client.get("/ip/address"), - client.get("/ipv6/address"), + read("/interface/wireguard"), + read("/interface/wireguard/peers"), + read("/ip/address"), + read("/ipv6/address"), ]) if (![ifacesRaw, peersRaw, v4Raw, v6Raw].every(Array.isArray)) throw new Error("RouterOS: неполный каталог WireGuard/адресов") const addrsRaw = [...v4Raw, ...v6Raw].sort((a, b) => Number(a.disabled === "true" || a.disabled === "yes") - Number(b.disabled === "true" || b.disabled === "yes") || (a.address ?? "").localeCompare(b.address ?? "")) @@ -192,15 +205,30 @@ export async function fetchWireguardRestoreState(server: ServerRow): Promise<{ comment: w.comment ?? "", disabled: w.disabled === "true" || w.disabled === "yes", })) + const generic = [...peersRaw, ...addrsRaw].some(row => row.interface?.trim().startsWith("*")) + ? nativeNetworkRows(await read("/interface")) + : [] + const resolveInterface = (ref: string | undefined) => { + const value = ref?.trim() ?? "" + if (!value.startsWith("*")) return value + const matches = generic.filter(row => row[".id"] === value || row.name === value) + const parent = matches[0] + if (matches.length !== 1 || !parent?.name || ifaces.some(iface => iface.name === parent.name) && !["wg", "wireguard"].includes(parent.type ?? "")) { + throw new Error("RouterOS: не подтверждена ссылка на интерфейс WireGuard/адреса") + } + return parent.name + } const peers: WgLivePeer[] = (Array.isArray(peersRaw) ? peersRaw : []).map((p, idx) => { const mapped = mapPeer(p, idx) const ep = (p["endpoint-address"] ?? "").trim() const port = (p["endpoint-port"] ?? "").trim() const ka = p["persistent-keepalive"] ? Number.parseInt(p["persistent-keepalive"], 10) : NaN + const interfaceName = resolveInterface(p.interface) + if (ifaces.filter(iface => iface.name === interfaceName).length !== 1) throw new Error("RouterOS: не подтверждён родитель WireGuard peer") return { rosId: mapped.rosId, - interfaceName: (p.interface ?? "").trim(), + interfaceName, publicKey: mapped.publicKey, allowedAddresses: mapped.allowedIps, endpointAddress: ep, @@ -210,6 +238,7 @@ export async function fetchWireguardRestoreState(server: ServerRow): Promise<{ name: mapped.name ?? "", disabled: mapped.disabled === true, privateKey: p["private-key"] ?? "", + ...(p["preshared-key"] && p["preshared-key"] !== "none" ? { presharedKeyConfigured: true as const } : {}), clientAddress: mapped.clientAddress ?? "", clientDns: mapped.clientDns ?? "", clientEndpoint: mapped.clientEndpoint ?? "", @@ -219,7 +248,7 @@ export async function fetchWireguardRestoreState(server: ServerRow): Promise<{ const addrs: WgLiveAddr[] = [] for (const a of Array.isArray(addrsRaw) ? addrsRaw : []) { if (a.dynamic === "true" || a.dynamic === "yes") continue - const iface = (a.interface ?? "").trim() + const iface = resolveInterface(a.interface) const address = (a.address ?? "").trim() const rosId = String(a[".id"] ?? "") if (!iface || !address || !rosId) continue @@ -246,8 +275,8 @@ export async function fetchWireguardRestoreState(server: ServerRow): Promise<{ return { client, ifaces, peers, addrs, snapshot } } -export async function captureWireguardSnapshot(server: ServerRow): Promise { - const state = await fetchWireguardRestoreState(server) +export async function captureWireguardSnapshot(server: ServerRow, beforeRead: () => Promise = async () => {}): Promise { + const state = await fetchWireguardRestoreState(server, beforeRead) return state.snapshot } @@ -256,6 +285,14 @@ export type WgListResult = { failures: Array<{ serverId: string; serverName?: string; error: string }> } +export async function pollWireGuardServers(serverRows: readonly ServerRow[], includePrivateKey = false) { + const results = await Promise.all(serverRows.filter(server => server.enabled).map(async server => { + try { return { ...(await fetchForServer(server, includePrivateKey)), failures: [] } } + catch (error) { return { interfaces: [] as WgIfaceDto[], observed: [] as ObservedWireGuardInterface[], failures: [{ serverId: String(server.id), serverName: server.name, error: error instanceof Error ? error.message : "Ошибка WireGuard" }] } } + })) + return { interfaces: results.flatMap(result => result.interfaces), observed: results.flatMap(result => result.observed), failures: results.flatMap(result => result.failures) } +} + export async function listWireGuardInterfaces(opts?: { serverId?: string includePrivateKey?: boolean @@ -273,22 +310,8 @@ export async function listWireGuardInterfaces(opts?: { serverRows = await db.select().from(servers).where(eq(servers.enabled, true)) } - const failures: WgListResult["failures"] = [] - const results = await Promise.all( - serverRows.map(async (server) => { - try { - return await fetchForServer(server, includePrivateKey) - } catch (e) { - failures.push({ - serverId: String(server.id), - serverName: server.name ?? undefined, - error: e instanceof Error ? e.message : String(e), - }) - return [] as WgIfaceDto[] - } - }), - ) - return { interfaces: results.flat(), failures } + const result = await pollWireGuardServers(serverRows, includePrivateKey) + return { interfaces: result.interfaces, failures: result.failures } } export async function countWireGuardInterfaces():Promise{return boundedCompleteCount(()=>listWireGuardInterfaces({ includePrivateKey: false }),value=>completeCollectionCount(value,"interfaces"))} @@ -341,8 +364,8 @@ export async function listWireGuardPeersForCatalog(serverId: number): Promise<{ }] }) return { peers } - } catch (e) { - return { peers: [], error: e instanceof Error ? e.message : String(e) } + } catch { + return { peers: [], error: "Не удалось получить список пиров WireGuard." } } } diff --git a/backend/src/services/wireguard-peer-create.test.ts b/backend/src/services/wireguard-peer-create.test.ts new file mode 100644 index 00000000..96fb2613 --- /dev/null +++ b/backend/src/services/wireguard-peer-create.test.ts @@ -0,0 +1,55 @@ +import assert from "node:assert/strict" +import { fileURLToPath } from "node:url" +import { wireguardPeerCreateSchema, wireguardPeerCreateBody, wireguardPeerCreateStages, assertWireguardPeerCreateAllowed, wireguardPeerCreatePrefix, wireguardPeerCreatePreserved } from "./wireguard-peer-create.js" +import { wireguardPublicKey } from "./wireguard-interface-create.js" +import { wireguardInterfaceDeleteProof, wireguardInterfaceDependencyPaths, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" + +export async function acceptWireguardPeerCreateModel() { + const key = Buffer.alloc(32, 61).toString("base64"), publicKey = wireguardPublicKey(key) + const input = wireguardPeerCreateSchema.parse({ serverId: 112, interfaceName: "wg-parent", publicKey, allowedAddresses: ["192.0.2.2/32", "2001:db8::2/128"], endpointAddress: "vpn.example.org", endpointPort: 65535, persistentKeepalive: 60, name: "owned", comment: "peer", privateKey: key, clientAddress: "192.0.2.2,2001:db8::2", clientDns: "1.1.1.1", clientEndpoint: "client.example.org" }) + const state: WireguardInterfaceDeleteState = { base: { interfaces: [{ ".id": "*a", name: input.interfaceName, disabled: "false", opaque: "NEVER_PARENT" }], peers: [{ ".id": "*f", interface: input.interfaceName, "public-key": "foreign", disabled: "false", opaque: "NEVER_FOREIGN" }], bindings: [], flow: { hostPublicKey: "", registered: false }, serverProof: "source", proof: "base" }, ipv4: [], ipv6: [], dependencies: Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])), filters: [], proof: "" } + state.dependencies["/interface"] = [{ ".id": "*a", name: input.interfaceName, type: "wg" }] + state.base.bindings.push({ id: "user", interfaceName: input.interfaceName, peerPublicKey: "foreign" }) + state.dependencies["/routing/ospf/interface-template"].push({ ".id": "*o", interfaces: input.interfaceName, networks: "2001:db8::/64" }) + state.dependencies["/ip/vrf"].push({ ".id": "*v", name: "owned", interfaces: input.interfaceName }) + state.ipv6.push({ ".id": "*ip", interface: input.interfaceName, address: "2001:db8::1/64", dynamic: "false", disabled: "false" }) + state.proof = await wireguardInterfaceDeleteProof(state) + assertWireguardPeerCreateAllowed(state, input) + assert.deepEqual(wireguardPeerCreateStages(input), ["peer", "peer-enable"]) + const after = structuredClone(state) + const row = { ".id": "*c", ...wireguardPeerCreateBody(input), interface: "*a", "persistent-keepalive": "1m", opaque: "native-default" } + after.base.peers.push(row) + assert.equal(await wireguardPeerCreatePrefix(after, input, state.proof, 1), true) + const enabled = structuredClone(after); enabled.base.peers[1].disabled = "no" + assert.equal(await wireguardPeerCreatePrefix(enabled, input, state.proof, 2, "*c"), true) + assert.equal(wireguardPeerCreatePreserved(after, enabled, "*c"), true) + for (const change of [ + (s: typeof enabled) => { s.base.interfaces[0].opaque = "changed" }, + (s: typeof enabled) => { s.base.peers[0].opaque = "changed" }, + (s: typeof enabled) => { s.base.peers[1]["client-address"] = "lost" }, + (s: typeof enabled) => { s.base.peers[1]["private-key"] = "lost" }, + (s: typeof enabled) => { s.base.peers[1][".id"] = "*new" }, + (s: typeof enabled) => { s.base.peers[1].interface = "foreign" }, + (s: typeof enabled) => { s.base.peers[1].dynamic = "unknown" }, + (s: typeof enabled) => { s.dependencies["/ip/firewall/filter"].push({ ".id": "*d", opaque: "new" }) }, + (s: typeof enabled) => { s.base.peers.push({ ...s.base.peers[1], ".id": "*e" }) }, + ]) { const changed = structuredClone(enabled); change(changed); assert.equal(await wireguardPeerCreatePrefix(changed, input, state.proof, 2, "*c"), false) } + enabled.base.peers[1].opaque = "changed native default" + assert.equal(wireguardPeerCreatePreserved(after, enabled, "*c"), false) + for (const change of [ + (s: typeof state) => { s.dependencies["/interface"][0][".id"] = "*wrong" }, + (s: typeof state) => { s.base.interfaces[0].dynamic = "unknown" }, + (s: typeof state) => { s.base.interfaces[0].name = "wg-flow" }, + (s: typeof state) => { s.base.interfaces[0].comment = "MikrotikManager traffic-flow overlay" }, + (s: typeof state) => { s.base.peers[0]["public-key"] = publicKey }, + ]) { const changed = structuredClone(state); change(changed); assert.throws(() => assertWireguardPeerCreateAllowed(changed, input)) } + for (const privateKey of ["none", "auto", undefined]) { + const request = wireguardPeerCreateSchema.parse({ ...input, privateKey, disabled: true }), next = structuredClone(state) + next.base.peers.push({ ".id": "*c", ...wireguardPeerCreateBody(request), ...(privateKey === "auto" ? { "private-key": key } : {}) }) + assert.equal(await wireguardPeerCreatePrefix(next, request, state.proof, 1), true) + assert.deepEqual(wireguardPeerCreateStages(request), ["peer"]) + } + for (const invalid of [{ publicKey: "bad" }, { allowedAddresses: ["bad"] }, { endpointPort: 65536 }, { persistentKeepalive: -1 }, { privateKey: "secret" }, { interfaceName: "\0" }]) assert.equal(wireguardPeerCreateSchema.safeParse({ ...input, ...invalid }).success, false) + console.log("WireGuard peer create model: full fields, IPv4/IPv6, key modes, parent/foreign/opaque preservation, stable identity, supported existing users/OSPF/VRF closure and NetFlow guards") +} +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) await acceptWireguardPeerCreateModel() diff --git a/backend/src/services/wireguard-peer-create.ts b/backend/src/services/wireguard-peer-create.ts new file mode 100644 index 00000000..7763c55d --- /dev/null +++ b/backend/src/services/wireguard-peer-create.ts @@ -0,0 +1,116 @@ +import { z } from "zod" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError } from "../modules/mcp/security.js" +import { wireguardInterfaceCreateSchema, wireguardCreatePeerBody, wireguardCreateKey, wireguardCreateProtectionProof, readWireguardInterfaceCreateState } from "./wireguard-interface-create.js" +import { wireguardInterfaceDeleteProof, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { wireguardConfigurationRows } from "./wireguard-peer-delete.js" +import { observedWireguardPeerKeepalive } from "./wireguard-peer-update.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { markWireguardReadBackFailure } from "./wireguard-read-diagnostic.js" + +export const wireguardPeerCreateSchema = wireguardInterfaceCreateSchema.shape.peer.unwrap().extend({ serverId: z.number().int().positive().safe(), interfaceName: wireguardInterfaceCreateSchema.shape.name }).strict() +export type WireguardPeerCreate = z.infer +type State = WireguardInterfaceDeleteState +type Row = Record +const fail = (code: string, message: string) => new McpError(code, message, code === "INVALID_INPUT" ? 400 : ["STATE_UNAVAILABLE", "MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(code) ? 502 : 409) +export const wireguardPeerCreateStages = (input: WireguardPeerCreate) => input.disabled ? ["peer"] : ["peer", "peer-enable"] +export function wireguardPeerCreateBody(input: WireguardPeerCreate) { + return wireguardCreatePeerBody({ serverId: input.serverId, name: input.interfaceName, listenPort: 13231, mtu: 1420, peer: input }) +} +export function assertWireguardPeerCreateAllowed(state: State, input: WireguardPeerCreate) { + const parents = state.base.interfaces.filter(row => row.name === input.interfaceName) + if (parents.length !== 1) throw fail("STATE_CONFLICT", "Родительский WireGuard не найден однозначно") + const parent = parents[0] + for (const flag of ["dynamic", "builtin", "default"]) if (parent[flag] !== undefined && observedBoolean(parent[flag]) !== false) throw fail("PROTECTED_OBJECT", "Системный или неподтверждённый родитель WireGuard") + if (observedBoolean(parent.disabled) === null) throw fail("STATE_UNAVAILABLE", "Не подтверждено состояние родителя WireGuard") + const generic = state.dependencies["/interface"].filter(row => row.name === parent.name || row[".id"] === parent[".id"]) + if (generic.length !== 1 || generic[0].name !== parent.name || generic[0][".id"] !== parent[".id"]) throw fail("STATE_CONFLICT", "Расходятся native ID родителя WireGuard") + if (parent.name === "wg-flow" || parent.comment === "MikrotikManager traffic-flow overlay" || input.name === "mm-collector" || input.comment === "MM traffic-flow collector" || input.publicKey === state.base.flow.hostPublicKey) throw fail("PROTECTED_OBJECT", "WireGuard или peer зарезервирован NetFlow") + if (state.base.peers.some(row => [parent.name, parent[".id"]].includes(row.interface) && (row["public-key"] === input.publicKey || input.name && row.name === input.name))) throw fail("STATE_CONFLICT", "Peer с таким ключом или именем уже существует") + // Existing parent references are supported: creating a child neither removes + // nor renames the parent. Their complete proof must survive every stage. + return parent +} +function matches(row: Row, body: Row) { + return Object.entries(body).every(([key, value]) => { + if (key === "disabled") return observedBoolean(row[key]) === observedBoolean(value) + if (key === "endpoint-port") return /^\d+$/.test(row[key] ?? "") && Number(row[key]) === Number(value) + if (key === "persistent-keepalive") return observedWireguardPeerKeepalive(row[key] ?? "") === Number(value) + if (key === "allowed-address") { try { return fingerprint(row[key].split(",").map(canonicalBgpPolicyPrefix).sort()) === fingerprint(value.split(",").map(canonicalBgpPolicyPrefix).sort()) } catch { return false } } + if (key === "private-key" && value === "auto") return wireguardCreateKey.safeParse(row[key]).success + if (key === "private-key" && value === "none") return row[key] === "none" || row[key] === "" + return (row[key] ?? "") === value + }) +} +/** Remove only the new native identity; every parent/foreign/opaque field stays sealed. */ +export async function wireguardPeerCreatePrefix(state: State, input: WireguardPeerCreate, baseline: string, count: number, peerId?: string) { + if (!Number.isInteger(count) || count < 0 || count > wireguardPeerCreateStages(input).length) return false + if (count === 0) return state.proof === baseline + const parents = state.base.interfaces.filter(row => row.name === input.interfaceName) + if (parents.length !== 1) return false + const selected = state.base.peers.filter(row => [parents[0].name, parents[0][".id"]].includes(row.interface) && row["public-key"] === input.publicKey) + if (selected.length !== 1 || peerId !== undefined && selected[0][".id"] !== peerId) return false + const target = selected[0], body = wireguardPeerCreateBody(input) + body.interface = target.interface; body.disabled = String(count === 1) + if (!matches(target, body) || ["dynamic", "builtin", "default"].some(key => target[key] !== undefined && observedBoolean(target[key]) !== false)) return false + const next = structuredClone(state) + next.base.peers = next.base.peers.filter(row => row[".id"] !== target[".id"]) + return await wireguardInterfaceDeleteProof(next) === baseline +} +export function wireguardPeerCreatePreserved(before: State, after: State, peerId: string) { + const old = before.base.peers.find(row => row[".id"] === peerId), current = after.base.peers.find(row => row[".id"] === peerId) + if (!old || !current) return false + const canonical = (row: Row) => wireguardConfigurationRows([{ ...row, disabled: String(observedBoolean(row.disabled)) }]) + return fingerprint(canonical({ ...old, disabled: "false" })) === fingerprint(canonical(current)) +} +export async function createWireguardPeer(server: ServerRow, raw: unknown, expectedProof?: string, authorize: () => Promise = async () => {}, options: { recordRevision?: boolean } = {}) { + const parsed = wireguardPeerCreateSchema.safeParse(raw) + if (!parsed.success || parsed.data.serverId !== server.id) throw fail("INVALID_INPUT", "Некорректные параметры создания peer WireGuard") + const input = parsed.data + let protectionProof: string | undefined + const source = async () => { + await authorize() + const fresh = await getServerRowById(server.id) + if (!fresh?.enabled || fingerprint(fresh) !== fingerprint(server)) throw fail("STATE_CONFLICT", "Источник WireGuard изменился") + if (protectionProof !== undefined && await wireguardCreateProtectionProof(server.id) !== protectionProof) throw fail("STATE_CONFLICT", "Защиты WireGuard изменились") + } + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "wireguard_peer", id: input.publicKey }, [server.id], async () => { + protectionProof = await wireguardCreateProtectionProof(server.id) + let state = await readWireguardInterfaceCreateState(server, source) + if (expectedProof && state.proof !== expectedProof) throw fail("STATE_CONFLICT", "WireGuard или зависимости изменились после подготовки") + assertWireguardPeerCreateAllowed(state, input) + const baseline = state.proof, stages = wireguardPeerCreateStages(input), client = MikrotikClient.fromServer(server) + let peerId: string | undefined + for (let index = 0; index < stages.length; index++) { + const fresh = await readWireguardInterfaceCreateState(server, source) + if (fresh.proof !== state.proof) throw fail(index ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "WireGuard изменился между командами") + const dispatchFence = async () => { + const current = await readWireguardInterfaceCreateState(server, source) + if (current.proof !== fresh.proof) throw fail(index ? "VERIFICATION_FAILED" : "STATE_CONFLICT", "WireGuard изменился после native intent; команда не отправлена") + await source() + } + await source() + try { + if (index === 0) await client.put("/interface/wireguard/peers", wireguardPeerCreateBody(input), 15000, dispatchFence) + else await client.patch("/interface/wireguard/peers/" + encodeURIComponent(peerId!), { disabled: "false" }, 15000, dispatchFence) + } catch (error) { if (error instanceof McpError) throw error; throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход создания peer неизвестен; выполнение остановлено без повтора") } + const prior = state + try { state = await readWireguardInterfaceCreateState(server, source) } + catch (error) { + throw markWireguardReadBackFailure(fail("MUTATION_OUTCOME_UNKNOWN", "Команда peer отправлена, read-back недоступен; повтор запрещён"), error) + } + if (!await wireguardPeerCreatePrefix(state, input, baseline, index + 1, peerId)) throw fail("VERIFICATION_FAILED", "Создание peer или сохранность конфигурации не подтверждены; повтор запрещён") + if (index && !wireguardPeerCreatePreserved(prior, state, peerId!)) throw fail("VERIFICATION_FAILED", "Подготовленный peer изменился при включении; повтор запрещён") + peerId ??= state.base.peers.find(row => row["public-key"] === input.publicKey && [input.interfaceName, state.base.interfaces.find(row => row.name === input.interfaceName)![".id"]].includes(row.interface))![".id"] + } + let revisionRecorded = false + try { if (options.recordRevision !== false) revisionRecorded = await captureAndAppendRevision({ serverId: server.id, section: "wireguard", source: "apply", capture: () => captureWireguardSnapshot(server, source) }) !== null } catch { /* A history failure never justifies replaying native creation. */ } + return { ok: true, verified: true, peerId: peerId!, afterProof: state.proof, commandCount: stages.length, revisionRecorded, forwardingVerified: false } + }))) +} diff --git a/backend/src/services/wireguard-peer-delete.ts b/backend/src/services/wireguard-peer-delete.ts new file mode 100644 index 00000000..7a194849 --- /dev/null +++ b/backend/src/services/wireguard-peer-delete.ts @@ -0,0 +1,128 @@ +import { pool } from "../db/index.js" +import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError, privateFingerprinter } from "../modules/mcp/security.js" +import { isRouterOsPreHeaderGetReset, MikrotikClient } from "./mikrotik.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { sameWireguardEndpoint, wireguardEndpointSource } from "./wireguard-source.js" +import { markWireguardGetFailure, markWireguardReadFailure } from "./wireguard-read-diagnostic.js" + +type Row = Record +type Binding = { id: string; interfaceName: string; peerPublicKey: string } +export interface WireguardPeerDeletionState { interfaces: Row[]; peers: Row[]; bindings: Binding[]; flow: { hostPublicKey: string; registered: boolean | null }; serverProof: string; proof: string } +const runtime = new Set(["running", "actual-mtu", "l2mtu", "rx", "tx", "last-handshake", "current-endpoint-address", "current-endpoint-port", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "last-link-down-time", "last-link-up-time", "link-downs"]) +const problem = (message: string, code = "STATE_CONFLICT") => new McpError(code, message, code === "INVALID_INPUT" ? 400 : ["STATE_UNAVAILABLE", "MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(code) ? 502 : 409) +async function source(server: ServerRow, beforeRead: () => Promise) { + await beforeRead() + const fresh = await getServerRowById(server.id) + if (!fresh || !sameWireguardEndpoint(fresh, server)) throw problem("Источник WireGuard изменился") +} + +function rows(value: unknown, maximum: number): Row[] { + if (!Array.isArray(value) || value.length > maximum || value.some(row => !row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]) || Object.values(row).some(item => typeof item !== "string"))) throw problem("Неполный native-каталог WireGuard") + if (new Set(value.map(row => row[".id"])).size !== value.length) throw problem("Повторяющиеся native ID WireGuard") + return value as Row[] +} +export function wireguardConfigurationRows(values: Row[]) { + return values.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !runtime.has(key)).sort(([a], [b]) => a.localeCompare(b)))).sort((a, b) => a[".id"].localeCompare(b[".id"])) +} +const stable = wireguardConfigurationRows +function proofValue(state: Omit) { + return { serverProof: state.serverProof, interfaces: stable(state.interfaces), peers: stable(state.peers), bindings: state.bindings, flow: state.flow } +} +export async function expectedWireguardPeerDeletionProof(state: WireguardPeerDeletionState, id: string) { + wireguardPeerDeletionTarget(state, id) + const sign = await privateFingerprinter("wireguard-peer-delete-v1") + return sign(fingerprint(proofValue({ ...state, peers: state.peers.filter(row => row[".id"] !== id) }))) +} +export function wireguardPeerDeletionTarget(state: WireguardPeerDeletionState, id: string, allowBoundPeer = false) { + if (!/^\*[a-z0-9]+$/i.test(id) || id.length > 100) throw problem("Требуется стабильный native ID peer", "INVALID_INPUT") + const peer = state.peers.find(row => row[".id"] === id) + if (!peer?.interface || !peer["public-key"]?.trim()) throw problem("Peer WireGuard не найден или не подтверждена его принадлежность") + for (const flag of ["dynamic", "builtin", "default"]) if (peer[flag] !== undefined && observedBoolean(peer[flag]) !== false) throw problem("Системный или неподтверждённый peer нельзя изменять", "PROTECTED_OBJECT") + const parents = state.interfaces.filter(row => row.name === peer.interface || row[".id"] === peer.interface) + if (parents.length !== 1 || !parents[0].name) throw problem("Не подтверждён родительский интерфейс WireGuard") + const parent = parents[0] + const managed = peer.comment === "MM traffic-flow collector" || peer.name === "mm-collector" && parent.comment === "MikrotikManager traffic-flow overlay" + const hostPeer = peer["public-key"] === state.flow.hostPublicKey + if (managed || hostPeer && state.flow.registered === true) throw problem("Peer управляется NetFlow overlay; используйте отдельное изменение сервиса NetFlow", "PROTECTED_OBJECT") + if (hostPeer && state.flow.registered === null) throw problem("Не подтверждена принадлежность peer сервису NetFlow", "STATE_UNAVAILABLE") + if (!allowBoundPeer && state.bindings.some(binding => [parent.name, parent[".id"]].includes(binding.interfaceName) && (!binding.peerPublicKey.trim() || binding.peerPublicKey.trim() === peer["public-key"]))) throw problem("Peer используется привязкой пользователя; сначала измените привязку отдельным планом") + return { peer, parent } +} + +export async function readWireguardPeerDeletionState(server: ServerRow, beforeRead: () => Promise = async () => {}): Promise { + const sign = await privateFingerprinter("wireguard-peer-delete-v1"), serverProof = sign(fingerprint(wireguardEndpointSource(server))) + const client = MikrotikClient.fromServer(server) + let catalogueRetryUsed = false + const read = async (path: string) => { + await source(server, beforeRead) + const deadline = performance.now() + 10_000 + try { return await client.get(path, 10000, undefined, 1048576) } + catch (error) { + if (catalogueRetryUsed || !isRouterOsPreHeaderGetReset(error) || performance.now() >= deadline) { + throw markWireguardGetFailure(problem("Native-каталог WireGuard недоступен", "STATE_UNAVAILABLE"), path, 1, error) + } + } + catalogueRetryUsed = true + await source(server, beforeRead) + const remaining = Math.floor(deadline - performance.now()) + if (remaining < 1) throw markWireguardReadFailure(problem("Native-каталог WireGuard недоступен", "STATE_UNAVAILABLE"), path, 2, "read-deadline") + try { return await client.get(path, remaining, undefined, 1048576) } + catch (error) { throw markWireguardGetFailure(problem("Native-каталог WireGuard недоступен", "STATE_UNAVAILABLE"), path, 2, error) } + } + const rawInterfaces = await read("/interface/wireguard"), rawPeers = await read("/interface/wireguard/peers") + if (Buffer.byteLength(JSON.stringify([rawInterfaces, rawPeers])) > 1048576) throw problem("Каталог WireGuard превышает 1 МиБ") + const catalogue = (raw: unknown, maximum: number, path: string) => { + try { return rows(raw, maximum) } + catch (error) { + if (!(error instanceof McpError)) throw error + throw markWireguardReadFailure(error, path, undefined, Array.isArray(raw) && raw.length > maximum ? "catalogue-limit" : "invalid-catalogue") + } + } + const interfaces = catalogue(rawInterfaces, 100, "/interface/wireguard") + const peers = catalogue(rawPeers, 2000, "/interface/wireguard/peers") + await source(server, beforeRead) + const bindings = (await pool.query('SELECT id,interface_name AS "interfaceName",peer_public_key AS "peerPublicKey" FROM user_interface_bindings WHERE server_id=$1 ORDER BY id LIMIT 1001', [server.id])).rows + if (bindings.length > 1000) throw problem("Превышен предел привязок WireGuard") + // Project only this server's membership; never transfer the global peer/key payload. + const flow = (await pool.query<{ hostPublicKey: string | null; registered: boolean | null }>(`SELECT + CASE WHEN octet_length(host_public_key)<=128 THEN host_public_key ELSE NULL END AS "hostPublicKey", + CASE WHEN jsonb_typeof(peers_json)='array' AND octet_length(peers_json::text)<=1048576 + THEN jsonb_path_exists(peers_json,'$[*] ? (@.serverId == $sid || @.serverId == $sidText)',jsonb_build_object('sid',$1::int,'sidText',$1::text)) ELSE NULL END AS registered + FROM traffic_flow_settings WHERE id=1`, [server.id])).rows[0] ?? { hostPublicKey: "", registered: false } + if (flow.hostPublicKey === null) throw problem("Ключ принадлежности NetFlow не подтверждён", "STATE_UNAVAILABLE") + await source(server, beforeRead) + const flowContext = { hostPublicKey: flow.hostPublicKey, registered: flow.registered } + const proof = sign(fingerprint(proofValue({ serverProof, interfaces, peers, bindings, flow: flowContext }))) + return { interfaces, peers, bindings, flow: flowContext, serverProof, proof } +} + +/** One DELETE, no cascade or retry. All other opaque configuration must survive. */ +export async function deleteWireguardPeer(server: ServerRow, id: string, expectedProof?: string, beforeRead: () => Promise = async () => {}, options: { recordRevision?: boolean } = {}) { + if (!/^\*[a-z0-9]+$/i.test(id) || id.length > 100) throw problem("Требуется стабильный native ID peer", "INVALID_INPUT") + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "wireguard_peer", id }, [server.id], async () => { + const before = await readWireguardPeerDeletionState(server, beforeRead) + if (expectedProof && before.proof !== expectedProof) throw problem("Peer, его источник или зависимости изменились после подготовки") + wireguardPeerDeletionTarget(before, id) + await source(server, beforeRead) + const client = MikrotikClient.fromServer(server) + try { await client.delete("/interface/wireguard/peers/" + encodeURIComponent(id)) } + catch (error) { + if (error instanceof McpError) throw error + throw problem("Исход удаления peer не подтверждён; не повторяйте запись, требуется сверка", "MUTATION_OUTCOME_UNKNOWN") + } + let after: WireguardPeerDeletionState + try { after = await readWireguardPeerDeletionState(server, beforeRead) } + catch { throw problem("Удаление отправлено, но проверка WireGuard недоступна; не повторяйте запись", "MUTATION_OUTCOME_UNKNOWN") } + const verified = after.serverProof === before.serverProof && fingerprint(stable(after.interfaces)) === fingerprint(stable(before.interfaces)) && fingerprint(stable(after.peers)) === fingerprint(stable(before.peers.filter(row => row[".id"] !== id))) && fingerprint(after.bindings) === fingerprint(before.bindings) && fingerprint(after.flow) === fingerprint(before.flow) + if (!verified) throw problem("Удаление peer или сохранение остальной конфигурации WireGuard не подтверждены; повтор запрещён", "VERIFICATION_FAILED") + let revisionRecorded = false + try { if (options.recordRevision !== false) revisionRecorded = await captureAndAppendRevision({ serverId: server.id, section: "wireguard", source: "apply", capture: () => captureWireguardSnapshot(server, () => source(server, beforeRead)) }) !== null } + catch { /* Native deletion remains verified even if history storage fails. */ } + return { ok: true, verified: true, peerId: id, afterProof: after.proof, revisionRecorded, revisionError: revisionRecorded || options.recordRevision === false ? null : "Удаление проверено; запись ревизии приложения не подтверждена", forwardingVerified: false } + }))) +} diff --git a/backend/src/services/wireguard-peer-update.test.ts b/backend/src/services/wireguard-peer-update.test.ts new file mode 100644 index 00000000..71c732d9 --- /dev/null +++ b/backend/src/services/wireguard-peer-update.test.ts @@ -0,0 +1,53 @@ +import assert from "node:assert/strict" +import { fileURLToPath } from "node:url" +import { projectWireguardPeerUpdate, wireguardPeerNativeUpdate, wireguardPeerPatchBody, wireguardPeerUpdateDifferences, wireguardPeerUpdateSchema } from "./wireguard-peer-update.js" +import type { WireguardPeerDeletionState } from "./wireguard-peer-delete.js" +import type { WireguardPeerUpdateState } from "./wireguard-peer-update.js" + +export function acceptWireguardPeerUpdateModel() { + const key = Buffer.alloc(32, 21).toString("base64"), otherKey = Buffer.alloc(32, 22).toString("base64") + const base: WireguardPeerDeletionState = { serverProof: "source", proof: "private", interfaces: [{ ".id": "*a", name: "wg" }], peers: [{ ".id": "*b", interface: "wg", "public-key": key, disabled: "false", "allowed-address": "192.0.2.2/32", "preshared-key": "NEVER_VALUE", comment: "before" }], bindings: [], flow: { hostPublicKey: "", registered: false } } + const patch = wireguardPeerUpdateSchema.parse({ allowedAddresses: ["2001:db8::abcd/64", "192.0.2.123/24"], persistentKeepalive: 60, disabled: true, endpointPort: 0, endpointAddress: "", comment: "" }) + assert.deepEqual(patch.allowedAddresses, ["192.0.2.0/24", "2001:db8::/64"]) + assert.equal(wireguardPeerPatchBody(patch).comment, "") + assert.equal(wireguardPeerPatchBody(patch)["endpoint-port"], "0") + const desired = projectWireguardPeerUpdate(base, "*b", patch), actual = structuredClone(desired) + actual.peers[0].disabled = "yes"; actual.peers[0]["persistent-keepalive"] = "1m" + actual.peers[0]["allowed-address"] = "2001:0db8:0:0:0:0:0:0/64,192.0.2.0/24" + delete actual.peers[0].comment; delete actual.peers[0]["endpoint-address"] + assert.deepEqual(wireguardPeerUpdateDifferences(desired, actual, "*b"), []) + for (const [property, unknown] of [["disabled", ""], ["persistent-keepalive", "garbage"], ["endpoint-port", undefined]] as const) { + const changed = structuredClone(actual) + if (unknown === undefined) delete changed.peers[0][property]; else changed.peers[0][property] = unknown + assert.ok(wireguardPeerUpdateDifferences(desired, changed, "*b").includes("peer." + property)) + } + const opaque = structuredClone(actual) + opaque.peers[0]["preshared-key"] = "NEVER_CHANGED_SECRET" + opaque.peers[0]["NEVER_PROPERTY_SECRET"] = "NEVER_VALUE" + assert.deepEqual(wireguardPeerUpdateDifferences(desired, opaque, "*b"), ["peer.opaque-configuration"]) + assert.ok(!JSON.stringify(wireguardPeerUpdateDifferences(desired, opaque, "*b")).includes("NEVER_")) + const offDesired = projectWireguardPeerUpdate(base, "*b", { persistentKeepalive: 0 }) + assert.ok(wireguardPeerUpdateDifferences(offDesired, base, "*b").includes("peer.persistent-keepalive"), "Missing alone remains unknown") + const confirmedOff: WireguardPeerUpdateState = { ...base, confirmedUnsetKeepaliveIds: ["*b"] } + assert.deepEqual(wireguardPeerUpdateDifferences(offDesired, confirmedOff, "*b"), []) + const bound = structuredClone(base) + bound.bindings = [{ id: "binding", interfaceName: "*a", peerPublicKey: key }] + assert.equal(projectWireguardPeerUpdate(bound, "*b", { comment: "valid" }).peers[0].comment, "valid") + assert.throws(() => projectWireguardPeerUpdate(bound, "*b", { publicKey: otherKey }), /привязкой/) + bound.bindings[0].peerPublicKey = otherKey + assert.throws(() => projectWireguardPeerUpdate(bound, "*b", { publicKey: otherKey }), /привязкой/) + const duplicate = structuredClone(base) + duplicate.peers.push({ ".id": "*c", interface: "*a", "public-key": otherKey }) + assert.throws(() => projectWireguardPeerUpdate(duplicate, "*b", { publicKey: otherKey }), /уже используется/) + const flow = structuredClone(base) + flow.flow = { hostPublicKey: otherKey, registered: true } + assert.throws(() => projectWireguardPeerUpdate(flow, "*b", { publicKey: otherKey }), /NetFlow/) + flow.flow.registered = null + assert.throws(() => projectWireguardPeerUpdate(flow, "*b", { publicKey: otherKey }), /NetFlow/) + assert.throws(() => wireguardPeerNativeUpdate({ interface: "another" })) + assert.throws(() => wireguardPeerUpdateSchema.parse({ privateKey: key })) + assert.throws(() => wireguardPeerUpdateSchema.parse({ allowedAddresses: ["192.0.2.1/24", "192.0.2.0/24"] })) + assert.deepEqual(wireguardPeerNativeUpdate({ "persistent-keepalive": "1m", disabled: "no", "endpoint-port": "0" }), { persistentKeepalive: 60, disabled: false, endpointPort: 0 }) + console.log("WireGuard peer update model: canonical explicit values, unknown refusal, opaque diagnostics, binding identity, native alias and NetFlow ownership checked; no SQL/network") +} +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) acceptWireguardPeerUpdateModel() diff --git a/backend/src/services/wireguard-peer-update.ts b/backend/src/services/wireguard-peer-update.ts new file mode 100644 index 00000000..6195f268 --- /dev/null +++ b/backend/src/services/wireguard-peer-update.ts @@ -0,0 +1,161 @@ +import { isIP } from "node:net" +import { z } from "zod" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, McpError, privateFingerprinter } from "../modules/mcp/security.js" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { observedBoolean, observedDurationMs, observedInteger } from "./ospf-observed-values.js" +import { readWireguardPeerDeletionState, wireguardPeerDeletionTarget, wireguardConfigurationRows, type WireguardPeerDeletionState } from "./wireguard-peer-delete.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" +import { captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { MikrotikClient } from "./mikrotik.js" + +const text = z.string().max(1000).refine(value => !value.includes("\0"), "Недопустимый нулевой символ") +function prefix(value: string) { + const normalized = value.trim(), family = isIP(normalized) + return canonicalBgpPolicyPrefix(family ? normalized + (family === 4 ? "/32" : "/128") : normalized) +} +const addresses = z.array(z.string().max(100).transform((value, context) => { + try { return prefix(value) } catch { context.addIssue({ code: "custom", message: "Нужен literal IPv4/IPv6 prefix" }); return z.NEVER } +})).min(1).max(100).refine(values => new Set(values).size === values.length, "Повторяющиеся allowed addresses").transform(values => values.sort()) +const endpoint = z.string().trim().max(253).refine(value => value === "" || isIP(value) !== 0 && !value.includes("%") || /^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.?$/i.test(value), "Нужен IP или DNS host") +export const wireguardPeerUpdateSchema = z.object({ + publicKey: z.string().regex(/^[A-Za-z0-9+/]{43}=$/).refine(value => Buffer.from(value, "base64").length === 32 && Buffer.from(value, "base64").toString("base64") === value && Buffer.from(value, "base64").some(byte => byte !== 0), "Некорректный публичный ключ").optional(), + allowedAddresses: addresses.optional(), endpointAddress: endpoint.optional(), endpointPort: z.number().int().min(0).max(65535).optional(), + persistentKeepalive: z.number().int().min(0).max(65535).optional(), comment: text.optional(), name: text.max(128).optional(), disabled: z.boolean().optional(), + clientAddress: text.optional(), clientDns: text.optional(), clientEndpoint: text.optional(), +}).strict().refine(value => Object.values(value).some(item => item !== undefined), "Нет полей для изменения") +export type WireguardPeerUpdate = z.infer +const fieldNames: Record = { publicKey: "public-key", allowedAddresses: "allowed-address", endpointAddress: "endpoint-address", endpointPort: "endpoint-port", persistentKeepalive: "persistent-keepalive", comment: "comment", name: "name", disabled: "disabled", clientAddress: "client-address", clientDns: "client-dns", clientEndpoint: "client-endpoint" } +const problem = (code: string, message: string) => new McpError(code, message, code === "INVALID_INPUT" ? 400 : ["STATE_UNAVAILABLE", "VERIFICATION_FAILED", "MUTATION_OUTCOME_UNKNOWN"].includes(code) ? 502 : 409) +export function wireguardPeerNativeUpdate(fields: Record) { + const converted: Record = {} + for (const [key, value] of Object.entries(fields)) { + const name = Object.entries(fieldNames).find(([, native]) => native === key)?.[0] + if (!name) throw problem("UNSUPPORTED_CAPABILITY", "Поле native peer не поддерживается этим общим планом; перенос интерфейса требует отдельной миграции") + converted[name] = name === "allowedAddresses" && typeof value === "string" ? value.split(",") : name === "endpointPort" && typeof value === "string" && /^\d+$/.test(value) ? Number(value) : name === "persistentKeepalive" && typeof value === "string" ? keepalive(value) ?? value : name === "disabled" && typeof value === "string" ? observedBoolean(value) ?? value : value + } + return wireguardPeerUpdateSchema.parse(converted) +} +function keepalive(value: string) { + const integer = observedInteger(value, 0, 65535) + if (integer !== null) return integer + const ms = observedDurationMs(value), seconds = ms === null ? null : ms / 1000 + return seconds !== null && Number.isInteger(seconds) && seconds >= 0 && seconds <= 65535 ? seconds : null +} +export { keepalive as observedWireguardPeerKeepalive } +export interface WireguardPeerUpdateState extends WireguardPeerDeletionState { confirmedUnsetKeepaliveIds: string[] } +async function assertSource(server: ServerRow, beforeRead: () => Promise) { + await beforeRead() + const current = await getServerRowById(server.id) + if (!current?.enabled || fingerprint(current) !== fingerprint(server)) throw problem("STATE_CONFLICT", "Источник WireGuard изменился") +} +/** An absent property alone is unknown. RouterOS nil must be corroborated by a complete presence partition. */ +export async function readWireguardPeerUpdateState(server: ServerRow, id: string, beforeRead: () => Promise = async () => {}): Promise { + const state = await readWireguardPeerDeletionState(server, beforeRead), selected = state.peers.find(row => row[".id"] === id) + const result: WireguardPeerUpdateState = { ...state, peers: state.peers.map(row => ({ ...row })), confirmedUnsetKeepaliveIds: [] } + if (!selected || selected["persistent-keepalive"] !== undefined) return result + const client = MikrotikClient.fromServer(server) + const read = async (path: string, body: Record) => { + await assertSource(server, beforeRead) + try { return await client.post(path, body) } + catch { throw problem("STATE_UNAVAILABLE", "Не подтверждён effective keepalive WireGuard") } + } + const getter = await read("/interface/wireguard/peers/get", { ".id": id, "value-name": "persistent-keepalive" }) + const ret = getter && typeof getter === "object" && !Array.isArray(getter) && "ret" in getter ? getter.ret : undefined + if (typeof ret === "string" && keepalive(ret) !== null) result.peers.find(row => row[".id"] === id)!["persistent-keepalive"] = ret + else { + if (!Array.isArray(getter) || getter.length !== 0) throw problem("STATE_UNAVAILABLE", "Не подтверждён unset keepalive WireGuard") + const present = await read("/interface/wireguard/peers/print", { ".proplist": [".id"], ".query": ["persistent-keepalive"] }) + const absent = await read("/interface/wireguard/peers/print", { ".proplist": [".id"], ".query": ["persistent-keepalive", "#!"] }) + const ids = (value: unknown) => { + if (!Array.isArray(value) || value.length > 2000 || Buffer.byteLength(JSON.stringify(value)) > 1048576 || value.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.keys(row).length !== 1 || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]))) throw problem("STATE_UNAVAILABLE", "Неполная presence-проверка keepalive WireGuard") + const values = value.map(row => row[".id"] as string) + if (new Set(values).size !== values.length) throw problem("STATE_UNAVAILABLE", "Повторяющиеся ID в presence-проверке keepalive") + return values + } + const positive = new Set(ids(present)), negative = new Set(ids(absent)), all = new Set(state.peers.map(row => row[".id"])) + if (positive.size + negative.size !== all.size || [...positive].some(value => negative.has(value) || !all.has(value)) || [...negative].some(value => !all.has(value)) || !negative.has(id) || state.peers.some(row => row["persistent-keepalive"] !== undefined && !positive.has(row[".id"]))) throw problem("STATE_UNAVAILABLE", "Противоречивый native-контекст unset keepalive") + result.confirmedUnsetKeepaliveIds.push(id) + } + // Native lists and dependencies must survive the auxiliary reads unchanged. + if ((await readWireguardPeerDeletionState(server, beforeRead)).proof !== state.proof) throw problem("STATE_CONFLICT", "WireGuard изменился во время проверки keepalive") + await assertSource(server, beforeRead) + return result +} +export function wireguardPeerPatchBody(patch: WireguardPeerUpdate) { + return Object.fromEntries(Object.entries(patch).filter(([, value]) => value !== undefined).map(([key, value]) => [fieldNames[key as keyof WireguardPeerUpdate], Array.isArray(value) ? value.join(",") : String(value)])) +} +/** Normalize only documented representations of writable public fields. Unknown flags remain opaque. */ +function canonicalPeers(state: WireguardPeerDeletionState) { + return wireguardConfigurationRows(state.peers).map(row => { + const result: Record = { ...row } + for (const key of ["comment", "name", "endpoint-address", "client-address", "client-dns", "client-endpoint"]) result[key] = row[key] ?? "" + if (observedBoolean(row.disabled) !== null) result.disabled = String(observedBoolean(row.disabled)) + if (row["endpoint-port"] !== undefined && observedInteger(row["endpoint-port"], 0, 65535) !== null) result["endpoint-port"] = String(Number(row["endpoint-port"])) + if (row["persistent-keepalive"] !== undefined && keepalive(row["persistent-keepalive"]) !== null) result["persistent-keepalive"] = keepalive(row["persistent-keepalive"]) === 0 ? "confirmed-off" : String(keepalive(row["persistent-keepalive"])) + else if (row["persistent-keepalive"] === undefined && (state as Partial).confirmedUnsetKeepaliveIds?.includes(row[".id"])) result["persistent-keepalive"] = "confirmed-off" + if (row["allowed-address"] !== undefined) { + try { result["allowed-address"] = row["allowed-address"].split(",").map(prefix).sort().join(",") } catch { /* Preserve unknown source text rather than invent a prefix. */ } + } + return result + }) +} +/** Safe diagnostic categories only; never return native values or unknown property names. */ +export function wireguardPeerUpdateDifferences(desired: WireguardPeerDeletionState, actual: WireguardPeerDeletionState, id: string) { + const differences: string[] = [] + if (desired.serverProof !== actual.serverProof) differences.push("source") + if (fingerprint(wireguardConfigurationRows(desired.interfaces)) !== fingerprint(wireguardConfigurationRows(actual.interfaces))) differences.push("interfaces") + const wanted = canonicalPeers(desired), observed = canonicalPeers(actual) + if (fingerprint(wanted.map(row => row[".id"])) !== fingerprint(observed.map(row => row[".id"]))) differences.push("peer-identities") + if (fingerprint(wanted.filter(row => row[".id"] !== id)) !== fingerprint(observed.filter(row => row[".id"] !== id))) differences.push("other-peers") + const before = wanted.find(row => row[".id"] === id), after = observed.find(row => row[".id"] === id) + if (!before || !after) differences.push("selected-peer") + else { + const known = new Set([".id", "interface", ...Object.values(fieldNames)]) + for (const key of known) if (fingerprint(before[key] ?? null) !== fingerprint(after[key] ?? null)) differences.push("peer." + key) + const opaque = (row: Record) => Object.fromEntries(Object.entries(row).filter(([key]) => !known.has(key))) + if (fingerprint(opaque(before)) !== fingerprint(opaque(after))) differences.push("peer.opaque-configuration") + } + if (fingerprint(desired.bindings) !== fingerprint(actual.bindings)) differences.push("bindings") + if (fingerprint(desired.flow) !== fingerprint(actual.flow)) differences.push("netflow-ownership") + return differences +} +export async function wireguardPeerUpdateProof(state: WireguardPeerDeletionState) { + const sign = await privateFingerprinter("wireguard-peer-update-v1") + return sign(fingerprint({ serverProof: state.serverProof, interfaces: wireguardConfigurationRows(state.interfaces), peers: canonicalPeers(state), bindings: state.bindings, flow: state.flow })) +} +export function projectWireguardPeerUpdate(state: WireguardPeerDeletionState, id: string, patch: WireguardPeerUpdate) { + const { peer } = wireguardPeerDeletionTarget(state, id, true) + if (patch.publicKey !== undefined && patch.publicKey !== peer["public-key"]) wireguardPeerDeletionTarget(state, id) + const next = { ...state, peers: state.peers.map(row => row[".id"] === id ? { ...row, ...wireguardPeerPatchBody(patch) } : row) } + const target = wireguardPeerDeletionTarget(next, id, true) + if (patch.publicKey !== undefined && patch.publicKey !== peer["public-key"]) wireguardPeerDeletionTarget(next, id) + if (next.peers.some(row => row[".id"] !== id && [target.parent.name, target.parent[".id"]].includes(row.interface) && row["public-key"] === target.peer["public-key"])) throw problem("STATE_CONFLICT", "Такой публичный ключ уже используется другим peer интерфейса") + return next +} +export async function updateWireguardPeer(server: ServerRow, id: string, raw: unknown, expectedProof?: string, beforeRead: () => Promise = async () => {}, options: { recordRevision?: boolean; expectedOriginalProof?: string } = {}) { + if (!/^\*[a-z0-9]+$/i.test(id) || id.length > 100) throw problem("INVALID_INPUT", "Требуется стабильный native ID peer") + const parsed = wireguardPeerUpdateSchema.safeParse(raw) + if (!parsed.success) throw problem("INVALID_INPUT", "Некорректные поля изменения peer WireGuard") + const patch = parsed.data + const source = () => assertSource(server, beforeRead) + return withApplicationMutationLock(() => withConfigMutationLock([server.id], () => executeGuardedApplicationWrite({ kind: "wireguard_peer", id }, [server.id], async () => { + const before = await readWireguardPeerUpdateState(server, id, beforeRead) + if (expectedProof && before.proof !== expectedProof) throw problem("STATE_CONFLICT", "Peer, источник или зависимости изменились после подготовки") + if (options.expectedOriginalProof && await wireguardPeerUpdateProof(before) !== options.expectedOriginalProof) throw problem("STATE_CONFLICT", "Effective keepalive изменился после подготовки") + const desired = projectWireguardPeerUpdate(before, id, patch), desiredProof = await wireguardPeerUpdateProof(desired) + await source() + try { await MikrotikClient.fromServer(server).patch("/interface/wireguard/peers/" + encodeURIComponent(id), wireguardPeerPatchBody(patch)) } + catch (error) { if (error instanceof McpError) throw error; throw problem("MUTATION_OUTCOME_UNKNOWN", "Исход PATCH peer не подтверждён; повтор запрещён до сверки") } + let after: WireguardPeerDeletionState + try { after = await readWireguardPeerUpdateState(server, id, beforeRead) } + catch { throw problem("MUTATION_OUTCOME_UNKNOWN", "PATCH отправлен, но проверка WireGuard недоступна; повтор запрещён") } + if (await wireguardPeerUpdateProof(after) !== desiredProof) throw problem("VERIFICATION_FAILED", "Изменение peer не подтверждено: " + wireguardPeerUpdateDifferences(desired, after, id).join(", ") + "; повтор запрещён") + let revisionRecorded = false + try { if (options.recordRevision !== false) revisionRecorded = await captureAndAppendRevision({ serverId: server.id, section: "wireguard", source: "apply", capture: () => captureWireguardSnapshot(server, source) }) !== null } catch { /* Verified effect is never replayed to repair history storage. */ } + return { ok: true, verified: true, peerId: id, afterProof: after.proof, afterSemanticProof: await wireguardPeerUpdateProof(after), revisionRecorded, revisionError: revisionRecorded || options.recordRevision === false ? null : "Изменение проверено; запись ревизии приложения не подтверждена", forwardingVerified: false } + }))) +} diff --git a/backend/src/services/wireguard-read-diagnostic.ts b/backend/src/services/wireguard-read-diagnostic.ts new file mode 100644 index 00000000..83ff47d0 --- /dev/null +++ b/backend/src/services/wireguard-read-diagnostic.ts @@ -0,0 +1,36 @@ +import { isRouterOsPreHeaderGetReset, MikrotikError } from "./mikrotik.js" +import { wireguardInterfaceDependencyPaths } from "./routeros-native-paths.js" + +const cataloguePaths = new Set([ + "/interface/wireguard", "/interface/wireguard/peers", "/ip/address", "/ipv6/address", + ...wireguardInterfaceDependencyPaths, +]) + +type Category = "pre-header-reset" | "http-refusal" | "other-read-failure" | "invalid-catalogue" | "catalogue-limit" | "read-deadline" +type ReadDiagnostic = { catalogue: string; attempt?: 1 | 2; category: Category; httpStatus?: number; stage?: "read-back" } +const diagnostics = new WeakMap() + +export function markWireguardReadFailure(error: T, path: string, attempt: 1 | 2 | undefined, category: Category): T { + if (cataloguePaths.has(path)) diagnostics.set(error, { catalogue: path, attempt, category }) + return error +} + +export function markWireguardGetFailure(error: T, path: string, attempt: 1 | 2, cause: unknown): T { + const category = isRouterOsPreHeaderGetReset(cause) ? "pre-header-reset" : cause instanceof MikrotikError ? "http-refusal" : "other-read-failure" + markWireguardReadFailure(error, path, attempt, category) + const diagnostic = diagnostics.get(error) + if (diagnostic && cause instanceof MikrotikError && Number.isInteger(cause.statusCode) && cause.statusCode >= 400 && cause.statusCode <= 599) { + diagnostic.httpStatus = cause.statusCode + } + return error +} + +export function wireguardReadDiagnostic(error: unknown): ReadDiagnostic | undefined { + return typeof error === "object" && error !== null ? diagnostics.get(error) : undefined +} + +export function markWireguardReadBackFailure(error: T, cause: unknown): T { + const diagnostic = wireguardReadDiagnostic(cause) + if (diagnostic) diagnostics.set(error, { ...diagnostic, stage: "read-back" }) + return error +} diff --git a/backend/src/services/wireguard-revision-restore.test.ts b/backend/src/services/wireguard-revision-restore.test.ts new file mode 100644 index 00000000..05438e51 --- /dev/null +++ b/backend/src/services/wireguard-revision-restore.test.ts @@ -0,0 +1,52 @@ +import assert from "node:assert/strict" +import { fileURLToPath } from "node:url" +import { canonicalWireguardSnapshot } from "./entity-snapshots.js" +import { wireguardInterfaceDependencyPaths } from "./wireguard-interface-delete.js" +import { wireguardPublicKey } from "./wireguard-interface-create.js" +import { validateWireguardRestoreSnapshot,materializeWireguardRestore,planWireguardRevisionRestore,verifyWireguardRestoreCommand,wireguardRestoreStateProof,type WireguardRestoreState } from "./wireguard-revision-restore.js" + +export async function acceptWireguardRevisionRestoreModel(){ + const key=Buffer.alloc(32,91).toString("base64"),publicKey=wireguardPublicKey(key) + const state:WireguardRestoreState={base:{interfaces:[{".id":"*a",name:"wg-old",disabled:"false",mtu:"1420","listen-port":"13231","private-key":key,"public-key":publicKey,opaque:"private-opaque"}],peers:[{".id":"*b",interface:"wg-old","public-key":publicKey,"allowed-address":"192.0.2.2/32",disabled:"false","endpoint-address":"old.example","endpoint-port":"54321","persistent-keepalive":"25s","client-address":"old","client-dns":"old","client-endpoint":"old",name:"old",comment:"old","preshared-key":"NEVER_TOUCH_PSK",opaque:"keep-peer"}],bindings:[],flow:{hostPublicKey:"",registered:false},serverProof:"source",proof:"base"},ipv4:[{".id":"*c",interface:"wg-old",address:"192.0.2.1/24",disabled:"false",dynamic:"false",comment:"old"}],ipv6:[],dependencies:Object.fromEntries(wireguardInterfaceDependencyPaths.map(path=>[path,[]])),filters:[],proof:""} + state.dependencies["/interface"].push({".id":"*a",name:"wg-old",type:"wg"}) + const raw=canonicalWireguardSnapshot({interfaces:[{name:"wg-old",privateKey:"*****",listenPort:13232,mtu:1380,comment:"",disabled:false,address:"2001:db8::1/64",innerAddresses:[{address:"2001:db8::1/64",disabled:false,advertise:false,noDad:true,comment:""}],peers:[{publicKey,allowedAddresses:["2001:db8::2/128"],endpointAddress:"",endpointPort:"",persistentKeepalive:null,comment:"",name:"",disabled:false,privateKey:"*****",clientAddress:"",clientDns:"",clientEndpoint:""}]}]}) + for(const broken of [null,{},[],{interfaces:null},{interfaces:[{}]},{...raw,foreign:true},{interfaces:[...raw.interfaces,...raw.interfaces]}])assert.throws(()=>validateWireguardRestoreSnapshot(broken)) + const desired=materializeWireguardRestore(raw,state),commands=planWireguardRevisionRestore(desired,state) + assert.equal(commands[0].body?.disabled,"true");assert.equal(commands.at(-1)?.body?.disabled,"false") + assert.ok(commands.some(c=>c.method==="DELETE"&&c.id==="*c"));assert.ok(commands.some(c=>c.path==="/ipv6/address"&&c.body?.["no-dad"]==="true")) + const peerCommand=commands.find(c=>c.resource==="peers")! + for(const field of ["endpoint-address","comment","name","client-address","client-dns","client-endpoint"])assert.equal(peerCommand.body?.[field],"") + assert.equal(peerCommand.body?.["endpoint-port"],"0");assert.equal(peerCommand.body?.["persistent-keepalive"],"0") + assert.equal(peerCommand.body?.["private-key"],undefined);assert.equal(commands[0].body?.["private-key"],undefined) + const after=structuredClone(state);Object.assign(after.base.peers[0],peerCommand.body) + assert.equal(await verifyWireguardRestoreCommand(state,after,peerCommand),true) + after.base.peers[0]["preshared-key"]="changed";assert.equal(await verifyWireguardRestoreCommand(state,after,peerCommand),false) + const collateral=structuredClone(state);collateral.base.interfaces[0].opaque="changed";assert.notEqual(await wireguardRestoreStateProof(collateral),await wireguardRestoreStateProof(state)) + const ipv6=structuredClone(state);ipv6.ipv6.push({".id":"*e",interface:"wg-old",address:"2001:db8::1/64",disabled:"false",dynamic:"false",advertise:"false","no-dad":"true",comment:""}) + assert.match(await wireguardRestoreStateProof(ipv6),/^[a-f0-9]{64}$/,"IPv6 source is canonicalized once, never recanonicalized from a bracketed comparison key") + const ipv6Command=commands.find(c=>c.resource==="ipv6")!;assert.equal(await verifyWireguardRestoreCommand(state,ipv6,ipv6Command),true) + for(const mutate of [(s:WireguardRestoreState)=>{s.base.bindings.push({id:"user",interfaceName:"wg-old",peerPublicKey:publicKey})},(s:WireguardRestoreState)=>{s.base.interfaces[0].comment="MikrotikManager traffic-flow overlay"},(s:WireguardRestoreState)=>{s.dependencies["/interface/vlan"].push({".id":"*d",interface:"wg-old"})},(s:WireguardRestoreState)=>{s.dependencies["/routing/bgp/connection"].push({".id":"*d","local-address":"192.0.2.1"})}]){const protectedState=structuredClone(state);mutate(protectedState);assert.throws(()=>planWireguardRevisionRestore(desired,protectedState))} + const legacy=structuredClone(raw);delete legacy.interfaces[0].innerAddresses;const multiple=structuredClone(state);multiple.ipv6.push({".id":"*d",interface:"wg-old",address:"2001:db8::1/64",dynamic:"false",disabled:"false"});assert.throws(()=>materializeWireguardRestore(legacy,multiple)) + assert.throws(()=>materializeWireguardRestore(raw,{...state,base:{...state.base,interfaces:[]}})) + const withoutPeer=structuredClone(desired);withoutPeer.interfaces[0].peers=[] + assert.throws(()=>planWireguardRevisionRestore(withoutPeer,state),/удалить peer WireGuard с настроенным PSK/) + assert.throws(()=>planWireguardRevisionRestore({interfaces:[]},state),/удалить peer WireGuard с настроенным PSK/) + const withPsk=structuredClone(desired);withPsk.interfaces[0].peers[0].presharedKeyConfigured=true + assert.deepEqual(validateWireguardRestoreSnapshot(withPsk),withPsk) + assert.deepEqual(planWireguardRevisionRestore(withPsk,state),commands) + const withoutPsk=structuredClone(state);delete withoutPsk.base.peers[0]["preshared-key"] + assert.throws(()=>planWireguardRevisionRestore(withPsk,withoutPsk),/существующий peer WireGuard с настроенным PSK/) + const missingPeer=structuredClone(state);missingPeer.base.peers=[] + assert.throws(()=>planWireguardRevisionRestore(withPsk,missingPeer),/существующий peer WireGuard с настроенным PSK/) + const newPeer=planWireguardRevisionRestore(desired,missingPeer).find(c=>c.resource==="peers")! + const withUnexpectedPsk=structuredClone(missingPeer) + withUnexpectedPsk.base.peers.push({".id":"*f",...newPeer.body,"preshared-key":"UNEXPECTED"}) + assert.equal(await verifyWireguardRestoreCommand(missingPeer,withUnexpectedPsk,newPeer),false) + delete withUnexpectedPsk.base.peers[0]["preshared-key"] + assert.equal(await verifyWireguardRestoreCommand(missingPeer,withUnexpectedPsk,newPeer),true) + const deletions=planWireguardRevisionRestore({interfaces:[]},withoutPsk) + assert.deepEqual(deletions.map(c=>c.resource),["interfaces","peers","ipv4","interfaces"]) + assert.equal(state.base.interfaces[0]["private-key"],key);assert.equal(state.base.peers[0]["preshared-key"],"NEVER_TOUCH_PSK") + console.log("WireGuard revision restore model: schema, clearing, keys, IPv6, deletion closure, dependencies and private conservation passed") +} +if(process.argv[1]===fileURLToPath(import.meta.url))await acceptWireguardRevisionRestoreModel() diff --git a/backend/src/services/wireguard-revision-restore.ts b/backend/src/services/wireguard-revision-restore.ts new file mode 100644 index 00000000..29e36324 --- /dev/null +++ b/backend/src/services/wireguard-revision-restore.ts @@ -0,0 +1,207 @@ +import { z } from "zod" +import { pool } from "../db/index.js" +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { getServerRowById } from "../modules/servers/repository/servers-repository.js" +import { fingerprint, privateSecretFingerprint, McpError } from "../modules/mcp/security.js" +import { canonicalWireguardSnapshot, interfaceAddressKey, interfaceAddressPath, isHiddenSecret, type WgSnapshot } from "./entity-snapshots.js" +import { fingerprintPayload, captureAndAppendRevision } from "./config-revisions.js" +import { captureWireguardSnapshot } from "./wireguard-live.js" +import { readWireguardInterfaceDeleteState, planWireguardInterfaceDelete, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" +import { wireguardInterfaceUpdateProof, projectWireguardInterfaceUpdate } from "./wireguard-interface-update.js" +import { wireguardPeerDeletionTarget } from "./wireguard-peer-delete.js" +import { readWireguardPeerUpdateState, observedWireguardPeerKeepalive } from "./wireguard-peer-update.js" +import { wireguardCreateKey, wireguardPublicKey, wireguardCreateProtectionProof, assertWireguardCreateAllowed } from "./wireguard-interface-create.js" +import { wireguardPrivateScalarsEqual } from "./wireguard-key-scalar.js" +import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js" +import { observedBoolean } from "./ospf-observed-values.js" +import { sameWireguardEndpoint } from "./wireguard-source.js" +import { MikrotikClient } from "./mikrotik.js" +import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js" +import { executeGuardedApplicationWrite } from "./config-mutation-policy.js" + +type Row = Record +export type WireguardRestoreState = WireguardInterfaceDeleteState +export interface WireguardRestoreCommand { method:"PUT"|"PATCH"|"DELETE"; path:string; resource:"interfaces"|"peers"|"ipv4"|"ipv6"; id?:string; body?:Row } +export class WireguardRevisionRestoreExecutionError extends McpError { + constructor(code:"MUTATION_OUTCOME_UNKNOWN"|"VERIFICATION_FAILED",message:string,readonly confirmedWrites:number,readonly completedCommands:number,readonly commandCount:number){super(code,message,502)} +} +const fail = (message:string,code="STATE_CONFLICT") => new McpError(code,message,code==="INVALID_INPUT"?400:["STATE_UNAVAILABLE","MUTATION_OUTCOME_UNKNOWN","VERIFICATION_FAILED"].includes(code)?502:409) +const text=z.string().max(1000), cidr=z.string().max(100).refine(v=>{try{interfaceAddressKey(v);return true}catch{return false}}) +const secret=text.refine(v=>v===""||isHiddenSecret(v)||v==="none"||wireguardCreateKey.safeParse(v).success) +const address=z.object({address:cidr,disabled:z.boolean(),comment:text.optional(),advertise:z.boolean().optional(),noDad:z.boolean().optional(),fromPool:text.optional(),eui64:z.boolean().optional()}).strict() +const peer=z.object({publicKey:wireguardCreateKey,allowedAddresses:z.array(cidr).max(100),endpointAddress:text.max(253),endpointPort:z.string().refine(v=>v===""||/^\d+$/.test(v)&&Number(v)<=65535),persistentKeepalive:z.number().int().min(0).max(65535).nullable(),comment:text,name:text,disabled:z.boolean(),privateKey:secret,presharedKeyConfigured:z.literal(true).optional(),clientAddress:text,clientDns:text,clientEndpoint:text}).strict() +const iface=z.object({name:text.min(1).max(64).refine(v=>v.trim()===v),listenPort:z.number().int().min(1).max(65535),mtu:z.number().int().min(1).max(65535),privateKey:secret,address:z.union([z.literal(""),cidr]),innerAddresses:z.array(address).max(50).optional(),comment:text,disabled:z.boolean(),peers:z.array(peer).max(50)}).strict() +/** Never send a forgiving legacy parser's empty result to a destructive planner. */ +export function validateWireguardRestoreSnapshot(payload:unknown):WgSnapshot { + let bytes:number;try{bytes=Buffer.byteLength(JSON.stringify(payload))}catch{throw fail("Повреждена ревизия WireGuard","INVALID_INPUT")} + const parsed=z.object({interfaces:z.array(iface).max(50)}).strict().safeParse(payload) + if(bytes>1048576||!parsed.success)throw fail("Повреждена схема ревизии WireGuard","INVALID_INPUT") + const desired=canonicalWireguardSnapshot(parsed.data) + if(fingerprint(desired)!==fingerprint(payload)||new Set(desired.interfaces.map(i=>i.name)).size!==desired.interfaces.length)throw fail("Неполная или неоднозначная ревизия WireGuard","INVALID_INPUT") + for(const i of desired.interfaces){ + if(new Set(i.peers.map(p=>p.publicKey)).size!==i.peers.length||i.innerAddresses&&new Set(i.innerAddresses.map(a=>interfaceAddressKey(a.address))).size!==i.innerAddresses.length)throw fail("Повторяющиеся объекты ревизии WireGuard","INVALID_INPUT") + if(i.innerAddresses?.some(a=>a.fromPool||a.eui64))throw fail("IPv6 pool/EUI-64 требует отдельного плана","INVALID_INPUT") + } + return desired +} +export async function readWireguardRestoreRevision(serverId:number,id:string,authorize:()=>Promise=async()=>{}) { + await authorize() + const row=(await pool.query("SELECT id,server_id,section,fingerprint,created_at::text AS created_at,CASE WHEN octet_length(payload::text)<=1048576 THEN payload ELSE NULL END AS payload FROM config_revisions WHERE id=$1 AND server_id=$2 AND section='wireguard'",[id,serverId])).rows[0] + await authorize() + if(!row?.payload||row.fingerprint!==fingerprintPayload(row.payload))throw fail("Ревизия WireGuard отсутствует или повреждена") + return {desired:validateWireguardRestoreSnapshot(row.payload),revisionProof:await privateSecretFingerprint(fingerprint(row),"wireguard-restore-revision-v1")} +} +function values(state:WireguardRestoreState,resource:WireguardRestoreCommand["resource"]):Row[]{return resource==="interfaces"?state.base.interfaces:resource==="peers"?state.base.peers:state[resource]} +function normalize(state:WireguardRestoreState) { + const next=structuredClone(state) + for(const rows of [next.base.interfaces,next.base.peers,next.ipv4,next.ipv6])for(const row of rows){ + for(const key of ["disabled","advertise","no-dad","eui-64","dynamic"])if(observedBoolean(row[key]??"")!==null)row[key]=String(observedBoolean(row[key])) + for(const key of ["listen-port","mtu","endpoint-port"])if(/^\d+$/.test(row[key]??""))row[key]=String(Number(row[key])) + for(const key of ["comment",...(rows===next.base.peers?["name","endpoint-address","client-address","client-dns","client-endpoint"]:[])])row[key]??="" + if(rows===next.base.peers){if(row["persistent-keepalive"]!==undefined){const ka=observedWireguardPeerKeepalive(row["persistent-keepalive"]);if(ka!==null)row["persistent-keepalive"]=String(ka)}if(row["allowed-address"]!==undefined)row["allowed-address"]=row["allowed-address"].split(",").filter(Boolean).map(canonicalBgpPolicyPrefix).sort().join(",")} + // The shared full-state proof canonicalizes interface addresses exactly once. + // interfaceAddressKey's IPv6 comparison key contains brackets and is not a + // RouterOS address that may be fed into that canonicalizer a second time. + } + return next +} +export const wireguardRestoreStateProof=(state:WireguardRestoreState)=>wireguardInterfaceUpdateProof(normalize(state)) +export async function readWireguardRestoreState(server:ServerRow,beforeRead:()=>Promise=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) + 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) + 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} + } + for(const p of state.base.peers)if(p["persistent-keepalive"]===undefined){const effective=await readWireguardPeerUpdateState(server,p[".id"],authorize);if(effective.proof!==state.base.proof)throw fail("Peers изменились во время проверки keepalive");const value=effective.peers.find(row=>row[".id"]===p[".id"])?.["persistent-keepalive"];if(value===undefined&&!effective.confirmedUnsetKeepaliveIds.includes(p[".id"]))throw fail("Не подтверждён keepalive");p["persistent-keepalive"]=value??"0"} + state.proof=await wireguardRestoreStateProof(state);return state +} +const unknownKey=(v:string)=>v===""||isHiddenSecret(v) +export function materializeWireguardRestore(raw:WgSnapshot,before:WireguardRestoreState):WgSnapshot { + const desired=validateWireguardRestoreSnapshot(raw) + return canonicalWireguardSnapshot({interfaces:desired.interfaces.map(want=>{ + const live=before.base.interfaces.find(row=>row.name===want.name),belongs=(r:Row)=>r.interface===want.name||r.interface===live?.[".id"] + const statics=[...before.ipv4,...before.ipv6].filter(r=>belongs(r)&&observedBoolean(r.dynamic)===false) + if(want.innerAddresses===undefined&&statics.length>1)throw fail("Старая ревизия не описывает все адреса WireGuard") + if(!live&&unknownKey(want.privateKey))throw fail("Нельзя создать WireGuard с неизвестным приватным ключом") + const addresses=want.innerAddresses??(want.address?[{address:want.address,disabled:false}]:[]) + return {...want,innerAddresses:addresses.map(a=>{const old=statics.find(r=>interfaceAddressKey(r.address)===interfaceAddressKey(a.address));return {...a,comment:a.comment??old?.comment??"",...(interfaceAddressPath(a.address)==="/ipv6/address"?{advertise:a.advertise===true,noDad:a.noDad??(old?observedBoolean(old["no-dad"])===true:false)}:{})}}),peers:want.peers.map(p=>{const old=before.base.peers.find(row=>belongs(row)&&row["public-key"]===p.publicKey);if(!old&&p.privateKey!==""&&isHiddenSecret(p.privateKey))throw fail("Нельзя создать peer с неизвестным ключом");return p})} + })}) +} +function matches(row:Row,key:string,value:string){ + if(["disabled","advertise","no-dad"].includes(key))return observedBoolean(row[key]??"")===observedBoolean(value) + if(["mtu","listen-port","endpoint-port"].includes(key))return /^\d+$/.test(row[key]??"")&&Number(row[key])===Number(value) + if(key==="persistent-keepalive")return observedWireguardPeerKeepalive(row[key]??"")===Number(value) + if(key==="private-key")return wireguardPrivateScalarsEqual(row[key],value,row["public-key"])||row[key]===value||value==="none"&&(row[key]??"")==="" + if(key==="address")return interfaceAddressKey(row[key])===interfaceAddressKey(value) + if(key==="allowed-address")return fingerprint((row[key]??"").split(",").filter(Boolean).map(canonicalBgpPolicyPrefix).sort())===fingerprint(value.split(",").filter(Boolean).map(canonicalBgpPolicyPrefix).sort()) + return (row[key]??"")===value +} +/** Fixed inventory, exact field clears, and explicit deletion closure. No config is inferred from DTOs. */ +export function planWireguardRevisionRestore(desired:WgSnapshot,state:WireguardRestoreState):WireguardRestoreCommand[]{ + const commands:WireguardRestoreCommand[]=[],enable:WireguardRestoreCommand[]=[],wanted=new Set(desired.interfaces.map(i=>i.name)) + for(const parent of state.base.interfaces){ + const belongs=(row:Row)=>[parent.name,parent[".id"]].includes(row.interface),peers=state.base.peers.filter(belongs),addresses=[...state.ipv4,...state.ipv6].filter(row=>belongs(row)&&observedBoolean(row.dynamic)===false) + if(peers.some(row=>!row["public-key"])||new Set(peers.map(row=>row["public-key"])).size!==peers.length||new Set(addresses.map(row=>interfaceAddressKey(row.address))).size!==addresses.length)throw fail("Неоднозначный каталог peers/адресов WireGuard") + const wantedPeers=desired.interfaces.find(row=>row.name===parent.name)?.peers??[] + if(peers.some(row=>hasPresharedKey(row)&&!wantedPeers.some(p=>p.publicKey===row["public-key"])))throw fail("Ревизия не может удалить peer WireGuard с настроенным PSK") + } + const add=(resource:WireguardRestoreCommand["resource"],path:string,body:Row,live?:Row)=>{const patch=live?Object.fromEntries(Object.entries(body).filter(([k,v])=>!matches(live,k,v))):body;if(Object.keys(patch).length)commands.push({method:live?"PATCH":"PUT",path,resource,...(live?{id:live[".id"]}:{}),body:patch})} + for(const live of state.base.interfaces)if(!wanted.has(live.name))commands.push(...planWireguardInterfaceDelete(state,live[".id"])) + for(const want of desired.interfaces){ + const live=state.base.interfaces.find(row=>row.name===want.name),belongs=(row:Row)=>row.interface===want.name||row.interface===live?.[".id"],start=commands.length + if(!live)assertWireguardCreateAllowed(state,{serverId:1,name:want.name,listenPort:want.listenPort,mtu:want.mtu,comment:want.comment}) + const body:Row={name:want.name,"listen-port":String(want.listenPort),mtu:String(want.mtu),comment:want.comment,disabled:String(want.disabled)} + if(!unknownKey(want.privateKey)){if(!wireguardCreateKey.safeParse(want.privateKey).success)throw fail("Некорректный приватный ключ интерфейса");body["private-key"]=want.privateKey} + add("interfaces","/interface/wireguard",body,live) + for(const old of state.base.peers.filter(belongs))if(!want.peers.some(p=>p.publicKey===old["public-key"])){wireguardPeerDeletionTarget(state.base,old[".id"]);commands.push({method:"DELETE",path:"/interface/wireguard/peers",resource:"peers",id:old[".id"]})} + for(const p of want.peers){ + const old=state.base.peers.find(row=>belongs(row)&&row["public-key"]===p.publicKey) + if(p.presharedKeyConfigured&&!hasPresharedKey(old))throw fail("Ревизия требует существующий peer WireGuard с настроенным PSK") + const pb:Row={interface:old?.interface??want.name,"public-key":p.publicKey,"allowed-address":p.allowedAddresses.join(","),"endpoint-address":p.endpointAddress,"endpoint-port":p.endpointPort||"0","persistent-keepalive":String(p.persistentKeepalive??0),comment:p.comment,name:p.name,"client-address":p.clientAddress,"client-dns":p.clientDns,"client-endpoint":p.clientEndpoint,disabled:String(p.disabled)} + if(!unknownKey(p.privateKey))pb["private-key"]=p.privateKey;else if(!old)pb["private-key"]="none" + if(old&&Object.entries(pb).some(([k,v])=>!matches(old,k,v)))wireguardPeerDeletionTarget(state.base,old[".id"]) + add("peers","/interface/wireguard/peers",pb,old) + } + const addresses=want.innerAddresses??(want.address?[{address:want.address,disabled:false}]:[]) + for(const [resource,path,rows]of [["ipv4","/ip/address",state.ipv4],["ipv6","/ipv6/address",state.ipv6]]as const){ + const old=rows.filter(belongs) + for(const a of old)if(observedBoolean(a.dynamic)===false&&!addresses.some(w=>interfaceAddressKey(w.address)===interfaceAddressKey(a.address)))commands.push({method:"DELETE",path,resource,id:a[".id"]}) + for(const a of addresses.filter(a=>interfaceAddressPath(a.address)===path)){ + const found=old.find(row=>interfaceAddressKey(row.address)===interfaceAddressKey(a.address)) + if(found&&observedBoolean(found.dynamic)!==false)throw fail("Адрес ревизии совпадает с динамическим") + if(!found&&[...state.ipv4,...state.ipv6].some(row=>interfaceAddressKey(row.address).split("/")[0]===interfaceAddressKey(a.address).split("/")[0]))throw fail("Адрес ревизии уже занят") + add(resource,path,{interface:found?.interface??want.name,address:a.address,disabled:String(a.disabled),comment:a.comment??"",...(resource==="ipv6"?{advertise:String(a.advertise===true),"no-dad":String(a.noDad===true)}:{})},found) + } + } + if(commands.length>start){ + if(live)planWireguardInterfaceDelete(state,live[".id"]) + // Stage every changed parent disabled; resolve newly assigned IDs only after read-back. + const parent=commands.slice(start).find(c=>c.resource==="interfaces") + if(parent)parent.body={...parent.body,disabled:"true"} + else commands.splice(start,0,{method:"PATCH",path:"/interface/wireguard",resource:"interfaces",id:live![".id"],body:{disabled:"true"}}) + if(!want.disabled)enable.push({method:"PATCH",path:"/interface/wireguard",resource:"interfaces",body:{name:want.name,disabled:"false"}}) + } + } + if(commands.length+enable.length>50)throw fail("WireGuard restore превышает 50 native-команд") + return [...commands,...enable] +} + +function hasPresharedKey(row: Row | undefined) { + const value = row?.["preshared-key"] + return value !== undefined && value !== "" && value !== "none" +} + +export async function verifyWireguardRestoreCommand(before:WireguardRestoreState,after:WireguardRestoreState,command:WireguardRestoreCommand){ + const next=structuredClone(before),previous=values(before,command.resource),observed=values(after,command.resource) + if(command.method==="PUT"){ + const additions=observed.filter(row=>!previous.some(old=>old[".id"]===row[".id"])) + if(additions.length!==1||["dynamic","builtin","default"].some(k=>additions[0][k]!==undefined&&observedBoolean(additions[0][k])!==false)||Object.entries(command.body!).some(([k,v])=>k==="interface"? !after.base.interfaces.some(parent=>parent.name===v&&[parent.name,parent[".id"]].includes(additions[0][k])):!matches(additions[0],k,v)))return false + if(command.resource==="peers"&&hasPresharedKey(additions[0]))return false + if(command.resource==="interfaces"){ + const row=additions[0];if(row["public-key"]!==wireguardPublicKey(command.body!["private-key"]))return false + const generic=after.dependencies["/interface"].filter(a=>a[".id"]===row[".id"]||a.name===row.name) + if(generic.length!==1||generic[0][".id"]!==row[".id"]||generic[0].name!==row.name)return false + next.dependencies["/interface"].push(generic[0]) + } + values(next,command.resource).push(additions[0]) + }else{ + const old=values(next,command.resource).find(r=>r[".id"]===command.id);if(!old)return false + if(command.method==="DELETE"){values(next,command.resource).splice(values(next,command.resource).indexOf(old),1);if(command.resource==="interfaces")next.dependencies["/interface"]=next.dependencies["/interface"].filter(r=>r[".id"]!==command.id)} + else if(command.resource==="interfaces")return await wireguardRestoreStateProof(projectWireguardInterfaceUpdate(before,{...command,method:"PATCH",resource:"interfaces"}))===await wireguardRestoreStateProof(after) + else Object.assign(old,command.body) + } + return await wireguardRestoreStateProof(next)===await wireguardRestoreStateProof(after) +} +export async function restoreWireguardRevision(server:ServerRow,id:string,options:{beforeProof?:string;revisionProof?:string;desired?:WgSnapshot;authorize?:()=>Promise}={}){ + return withApplicationMutationLock(()=>withConfigMutationLock([server.id],()=>executeGuardedApplicationWrite({kind:"wireguard_revision",id},[server.id],async()=>{ + await options.authorize?.() + const deadline=Date.now()+120000,protection=await wireguardCreateProtectionProof(server.id) + const access=async()=>{if(Date.now()>=deadline)throw fail("Истёк срок WireGuard restore");await options.authorize?.();const fresh=await getServerRowById(server.id);if(!fresh||!sameWireguardEndpoint(fresh,server)||await wireguardCreateProtectionProof(server.id)!==protection)throw fail("Источник или защиты WireGuard изменились")} + await access();const revision=await readWireguardRestoreRevision(server.id,id,access) + let current=await readWireguardRestoreState(server,access) + if(options.beforeProof&&options.beforeProof!==current.proof||options.revisionProof&&options.revisionProof!==revision.revisionProof)throw fail("WireGuard или ревизия изменились после подтверждения") + const desired=options.desired?validateWireguardRestoreSnapshot(options.desired):materializeWireguardRestore(revision.desired,current),commands=planWireguardRevisionRestore(desired,current),client=MikrotikClient.fromServer(server) + let completedCommands=0,confirmedWrites=0,dispatched=false + const stopped=(code:"MUTATION_OUTCOME_UNKNOWN"|"VERIFICATION_FAILED",message:string)=>new WireguardRevisionRestoreExecutionError(code,message,confirmedWrites,completedCommands,commands.length) + try { for(const planned of commands){ + const command=structuredClone(planned) + if(command.method==="PATCH"&&!command.id){const parent=current.base.interfaces.find(r=>r.name===command.body?.name);if(!parent)throw fail("Не подтверждён созданный WireGuard");command.id=parent[".id"];delete command.body!.name} + const gate=async()=>{await access();if((await readWireguardRestoreRevision(server.id,id,access)).revisionProof!==revision.revisionProof)throw fail("Ревизия изменилась");const fresh=await readWireguardRestoreState(server,access);if(fresh.proof!==current.proof)throw fail("WireGuard изменился между командами");planWireguardRevisionRestore(desired,fresh)} + await gate() + const dispatch=async()=>{await gate();dispatched=true} + try{const path=command.path+(command.id?"/"+encodeURIComponent(command.id):""),timeout=Math.max(1,Math.min(15000,deadline-Date.now()));if(command.method==="PUT")await client.put(path,command.body!,timeout,dispatch);else if(command.method==="PATCH")await client.patch(path,command.body!,timeout,dispatch);else await client.delete(path,timeout,dispatch);confirmedWrites++}catch(error){if(!dispatched&&error instanceof McpError)throw error;throw stopped("MUTATION_OUTCOME_UNKNOWN","Исход WireGuard restore неизвестен; повтор запрещён")} + let after:WireguardRestoreState;try{after=await readWireguardRestoreState(server,access)}catch{throw stopped("MUTATION_OUTCOME_UNKNOWN","Команда отправлена, сверка недоступна; повтор запрещён")} + if(!await verifyWireguardRestoreCommand(current,after,command))throw stopped("VERIFICATION_FAILED","Команда или сохранность конфигурации WireGuard не подтверждены") + current=after;completedCommands++ + }}catch(error){if(error instanceof WireguardRevisionRestoreExecutionError)throw error;if(dispatched)throw stopped("MUTATION_OUTCOME_UNKNOWN","Восстановление WireGuard остановлено после отправки команды; нужна сверка без повторной записи");throw error} + if(planWireguardRevisionRestore(desired,current).length)throw fail("Ревизия WireGuard не восстановлена полностью","VERIFICATION_FAILED") + let revisionRecorded=false;try{revisionRecorded=await captureAndAppendRevision({serverId:server.id,section:"wireguard",source:"rollback",capture:async()=>{const snapshot=await captureWireguardSnapshot(server,access);if((await readWireguardRestoreState(server,access)).proof!==current.proof)throw fail("WireGuard изменился во время записи истории");return snapshot}})!==null}catch{/* Never replay a verified effect for history. */} + return {ok:revisionRecorded,verified:revisionRecorded,configurationVerified:true,afterFingerprint:current.proof,commandCount:commands.length,completedCommands,confirmedWrites,revisionRecorded,forwardingVerified:false,addressReadinessVerified:false} + }))) +} diff --git a/backend/src/services/wireguard-source.ts b/backend/src/services/wireguard-source.ts new file mode 100644 index 00000000..062d153b --- /dev/null +++ b/backend/src/services/wireguard-source.ts @@ -0,0 +1,10 @@ +import type { ServerRow } from "../modules/servers/repository/servers-repository.js" +import { fingerprint } from "../modules/mcp/security.js" + +/** Private proof input only. Display metadata does not change an endpoint incarnation. */ +export function wireguardEndpointSource(server: ServerRow) { + return { id: server.id, host: server.host, port: server.port, username: server.username, password: server.password, useSsl: server.useSsl, verifySsl: server.verifySsl, enabled: server.enabled, routingEndpointEpoch: server.routingEndpointEpoch } +} +export function sameWireguardEndpoint(current: ServerRow, expected: ServerRow) { + return current.enabled && /^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$/i.test(current.routingEndpointEpoch) && fingerprint(wireguardEndpointSource(current)) === fingerprint(wireguardEndpointSource(expected)) +} diff --git a/backend/src/test/chr-vrf-companion-address-space.test.ts b/backend/src/test/chr-vrf-companion-address-space.test.ts new file mode 100644 index 00000000..26133286 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-address-space.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict" +import { assertChrVrfFixtureAddressSpaceUnused, ChrAddressSpaceAdmissionError, parseChrVrfPrefix, type ChrAddressSpaceCatalog } from "./chr-vrf-companion-address-space.js" + +const candidates = ["198.18.9.1/32", "198.18.9.2/32", "fd6d:6370:1234:5678::1/128", "fd6d:6370:1234:5678::2/128"] +const baseline = (): ChrAddressSpaceCatalog => ({ + ipv4_address: [{ ".id": "*A", address: "192.0.2.1/24" }], + ipv6_address: [{ ".id": "*B", address: "fe80::1/64" }], + ipv4_route: [{ ".id": "*C", "dst-address": "0.0.0.0/0" }, { ".id": "*D", "dst-address": "192.0.2.0/24" }], + ipv6_route: [{ ".id": "*E", "dst-address": "::/0" }, { ".id": "*F", "dst-address": "fe80::/64" }], +}) +let refusals = 0 +function refuse(work: () => unknown, reason?: ChrAddressSpaceAdmissionError["reason"]) { + assert.throws(work, error => error instanceof ChrAddressSpaceAdmissionError && (!reason || error.reason === reason)) + refusals++ +} +assert.deepEqual(assertChrVrfFixtureAddressSpaceUnused(baseline(), candidates), { status: "proven-unused-fixture-address-space", candidates: 4, addressesChecked: 2, nonDefaultRoutesChecked: 2, defaultRoutesExcluded: 2 }) +const expanded = "FD6D:6370:1234:5678:0000:0000:0000:0001/128" +assert.equal(parseChrVrfPrefix(expanded, 6)?.hostKey, parseChrVrfPrefix(candidates[2], 6)?.hostKey) +assert.equal(assertChrVrfFixtureAddressSpaceUnused(baseline(), [candidates[3], candidates[1], expanded, candidates[0]]).status, "proven-unused-fixture-address-space") + +for (const [family, values] of [ + [4, ["192.0.2.1/24junk", "192.0.2.1/24/1", "192.0.2.1/033", "192.0.2.1/33", "192.0.2.1/-1", "192.0.2.1/+1", "192.0.2.1/1.0", "192.0.2.1", "192.0.2.256/32", "192.0.02.1/24", "192.0.2.1%ether1/32", "192.0.2.1@vrf/32", " 192.0.2.1/32", "192.0.2.1/32\n"]], + [6, ["fd6d::1/129", "fd6d::1/-1", "fd6d::1/128junk", "fd6d::1/0128", "fd6d::1/128/1", "fd6d::1%bridge/128", "fd6d::1@main/128", "[fd6d::1]/128", "::ffff:198.18.9.1/128", "::ffff:c612:901/128", "::198.18.9.1/128", "fd6d:::1/128", "fd6d::1", "fd6d::1/999"]], +] as const) for (const value of values) assert.equal(parseChrVrfPrefix(value, family), null, value) +assert.equal(parseChrVrfPrefix("255.255.255.255/32", 4)?.last, 4294967295n) +assert.equal(parseChrVrfPrefix("0.0.0.0/0", 4)?.last, (1n << 32n) - 1n) +assert.equal(parseChrVrfPrefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128", 6)?.last, (1n << 128n) - 1n) +assert.equal(parseChrVrfPrefix("::/0", 6)?.last, (1n << 128n) - 1n) +assert.equal(parseChrVrfPrefix("192.0.2.1/32", 6), null) +assert.equal(parseChrVrfPrefix("fd6d::1/128", 4), null) + +for (const [resource, field, values, reason] of [ + ["ipv4_address", "address", ["198.18.9.1/24", "198.18.9.1/32"], "address-host-collision"], + ["ipv6_address", "address", [expanded, "fd6d:6370:1234:5678::1/64"], "address-host-collision"], + ["ipv4_address", "address", ["198.18.9.99/24", "198.18.1.99/16", "0.0.0.0/0"], "address-subnet-overlap"], + ["ipv6_address", "address", ["fd6d:6370:1234:5678::99/64", "fd6d:6370::99/32", "::/0"], "address-subnet-overlap"], + ["ipv4_route", "dst-address", ["198.18.9.1/32", "198.18.9.0/24", "198.18.0.0/15", "128.0.0.0/1"], "nondefault-route-overlap"], + ["ipv6_route", "dst-address", [expanded, "fd6d:6370:1234:5678::/64", "fd6d:6370::/32", "8000::/1"], "nondefault-route-overlap"], +] as const) for (const value of values) { + const catalog = baseline() + catalog[resource] = [...catalog[resource], { ".id": "*10", [field]: value, disabled: "true", dynamic: "false", "routing-table": "other-vrf" }] + refuse(() => assertChrVrfFixtureAddressSpaceUnused(catalog, candidates), reason) +} +for (const resource of ["ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route"] as const) { + const field = resource.endsWith("address") ? "address" : "dst-address" + for (const row of [{ ".id": "*10", [field]: "unconfirmed" }, { ".id": "*10" }, { [field]: resource.startsWith("ipv4") ? "192.0.2.1/32" : "fd00::1/128" }]) { + const catalog = baseline(); catalog[resource] = [...catalog[resource], row] + refuse(() => assertChrVrfFixtureAddressSpaceUnused(catalog, candidates), "invalid-baseline") + } + const duplicate = baseline(); duplicate[resource] = [...duplicate[resource], duplicate[resource][0]] + refuse(() => assertChrVrfFixtureAddressSpaceUnused(duplicate, candidates), "invalid-baseline") + const missing = baseline(); missing[resource] = undefined as unknown as [] + refuse(() => assertChrVrfFixtureAddressSpaceUnused(missing, candidates), "invalid-baseline") +} +// Equal unrelated link-local ranges on different native IDs are legitimate; +// they are scanned, not collapsed or treated as a malformed duplicate catalog. +const repeatedRange = baseline(); repeatedRange.ipv6_address = [...repeatedRange.ipv6_address, { ".id": "*20", address: "fe80::2/64" }] +assert.equal(assertChrVrfFixtureAddressSpaceUnused(repeatedRange, candidates).addressesChecked, 3) +const oversized = baseline(); oversized.ipv4_address = Array(50001).fill({ ".id": "*A", address: "192.0.2.1/24" }) +refuse(() => assertChrVrfFixtureAddressSpaceUnused(oversized, candidates), "invalid-baseline") +for (const invalid of [ + candidates.slice(1), [...candidates, candidates[0]], [candidates[0], candidates[0], candidates[2], candidates[3]], + [candidates[0], candidates[1], expanded, candidates[2]], + ["198.19.9.1/32", "198.19.9.2/32", ...candidates.slice(2)], + ["198.18.0.1/32", "198.18.0.2/32", ...candidates.slice(2)], + ["198.18.254.1/32", "198.18.254.2/32", ...candidates.slice(2)], + ["198.18.9.3/32", "198.18.9.4/32", ...candidates.slice(2)], + [candidates[0], candidates[1], "fd6d:6370:0:1::1/128", "fd6d:6370:0:1::2/128"], + [candidates[0], candidates[1], "fd6d:6370:ffff:1::1/128", "fd6d:6370:ffff:1::2/128"], + [candidates[0], candidates[1], "fd00:6370:1:1::1/128", "fd00:6370:1:1::2/128"], + ["198.18.9.1/24", candidates[1], candidates[2], candidates[3]], +]) refuse(() => assertChrVrfFixtureAddressSpaceUnused(baseline(), invalid), "invalid-candidates") +const frozen = baseline() +for (const resource of Object.keys(frozen) as Array) { frozen[resource].forEach(Object.freeze); Object.freeze(frozen[resource]) } +Object.freeze(frozen) +assert.equal(assertChrVrfFixtureAddressSpaceUnused(frozen, Object.freeze([...candidates])).status, "proven-unused-fixture-address-space") +console.log(`VRF address-space admission: ${refusals} refusal models; strict canonical IP/CIDR, host/subnet/nondefault-route overlap, fixed approved candidates, malformed/zone/mapped/duplicate/bounds checks; zero SQL/network`) diff --git a/backend/src/test/chr-vrf-companion-address-space.ts b/backend/src/test/chr-vrf-companion-address-space.ts new file mode 100644 index 00000000..d3293cb6 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-address-space.ts @@ -0,0 +1,92 @@ +import { isIP } from "node:net" +import { ipv6ToInt } from "../services/traffic-flow-ip.js" + +export interface CanonicalChrPrefix { + family: 4 | 6; bits: 32 | 128; prefixLength: number; + host: bigint; first: bigint; last: bigint; hostKey: string; +} +type Resource = "ipv4_address" | "ipv6_address" | "ipv4_route" | "ipv6_route" +export type ChrAddressSpaceCatalog = Record[]> +export interface AddressSpaceAdmission { + status: "proven-unused-fixture-address-space"; + candidates: 4; addressesChecked: number; nonDefaultRoutesChecked: number; defaultRoutesExcluded: number; +} + +export class ChrAddressSpaceAdmissionError extends Error { + constructor(public readonly reason: "invalid-candidates" | "invalid-baseline" | "address-host-collision" | "address-subnet-overlap" | "nondefault-route-overlap") { + super(`VRF fixture address-space admission refused: ${reason}`) + } +} + +/** Strict native CIDR parser. Reject zones, suffixes, mapped aliases and partial + * decimal parses; do not silently strip context from an unfamiliar address. */ +export function parseChrVrfPrefix(raw: unknown, family: 4 | 6): CanonicalChrPrefix | null { + if (typeof raw !== "string" || raw.length > 200 || /[\s%@\[\]]/.test(raw)) return null + const parts = raw.split("/") + if (parts.length !== 2 || !/^(?:0|[1-9][0-9]{0,2})$/.test(parts[1]) || isIP(parts[0]) !== family) return null + const bits = family === 4 ? 32 : 128, prefixLength = Number(parts[1]) + if (prefixLength > bits) return null + let host: bigint + if (family === 4) host = parts[0].split(".").reduce((value, octet) => (value << 8n) | BigInt(octet), 0n) + else { + const parsed = ipv6ToInt(parts[0]) + if (parsed === null || parsed >> 32n === 0xffffn || parts[0].includes(".")) return null + host = parsed + } + const shift = BigInt(bits - prefixLength), first = (host >> shift) << shift + return { family, bits, prefixLength, host, first, last: first + (1n << shift) - 1n, hostKey: `${family}:${host.toString(16)}` } +} + +function overlap(left: CanonicalChrPrefix, right: CanonicalChrPrefix): boolean { + return left.family === right.family && left.first <= right.last && right.first <= left.last +} + +function candidatesForFixture(raw: readonly string[]): CanonicalChrPrefix[] { + const fail = () => { throw new ChrAddressSpaceAdmissionError("invalid-candidates") } + if (!Array.isArray(raw) || raw.length !== 4) return fail() + const parsed = raw.map(value => parseChrVrfPrefix(value, 4) ?? parseChrVrfPrefix(value, 6)) + if (parsed.some(value => value === null)) return fail() + const values = parsed as CanonicalChrPrefix[] + if (new Set(values.map(value => value.hostKey)).size !== 4 || values.some(value => value.prefixLength !== value.bits)) return fail() + const v4 = values.filter(value => value.family === 4).sort((a, b) => a.host < b.host ? -1 : 1) + const v6 = values.filter(value => value.family === 6).sort((a, b) => a.host < b.host ? -1 : 1) + if (v4.length !== 2 || v6.length !== 2) return fail() + // Exact shape used by the explicitly reviewed runner; this is not a general + // allocator for arbitrary benchmark, ULA, public or physical-link addresses. + const octets = [24n, 16n, 8n, 0n].map(shift => Number((v4[0].host >> shift) & 255n)) + if (octets[0] !== 198 || octets[1] !== 18 || octets[2] < 1 || octets[2] > 253 || octets[3] !== 1 || v4[1].host !== v4[0].host + 1n) return fail() + const words = [112n, 96n, 80n, 64n].map(shift => Number((v6[0].host >> shift) & 65535n)) + if (words[0] !== 0xfd6d || words[1] !== 0x6370 || words[2] < 1 || words[2] > 65534 || words[3] < 1 || words[3] > 65534 || (v6[0].host & ((1n << 64n) - 1n)) !== 1n || v6[1].host !== v6[0].host + 1n) return fail() + return values +} + +/** Scan the complete readable baseline before the FIRST PUT. No selection or + * filtering by active/dynamic/VRF is allowed: all existing configured space is + * conserved. Default route coverage alone is not evidence of a host conflict. */ +export function assertChrVrfFixtureAddressSpaceUnused(catalog: ChrAddressSpaceCatalog, rawCandidates: readonly string[]): AddressSpaceAdmission { + const candidates = candidatesForFixture(rawCandidates) + const result: AddressSpaceAdmission = { status: "proven-unused-fixture-address-space", candidates: 4, addressesChecked: 0, nonDefaultRoutesChecked: 0, defaultRoutesExcluded: 0 } + for (const [resource, family, field, address] of [ + ["ipv4_address", 4, "address", true], ["ipv6_address", 6, "address", true], + ["ipv4_route", 4, "dst-address", false], ["ipv6_route", 6, "dst-address", false], + ] as const) { + const rows = catalog[resource], ids = new Set() + if (!Array.isArray(rows) || rows.length > 50000) throw new ChrAddressSpaceAdmissionError("invalid-baseline") + for (const row of rows) { + if (!row || typeof row !== "object" || Array.isArray(row) || typeof row[".id"] !== "string" || !/^\*[0-9a-f]+$/i.test(row[".id"]) || ids.has(row[".id"])) throw new ChrAddressSpaceAdmissionError("invalid-baseline") + ids.add(row[".id"]) + const current = parseChrVrfPrefix(row[field], family) + if (!current) throw new ChrAddressSpaceAdmissionError("invalid-baseline") + if (address) { + result.addressesChecked++ + if (candidates.some(candidate => candidate.hostKey === current.hostKey)) throw new ChrAddressSpaceAdmissionError("address-host-collision") + if (candidates.some(candidate => overlap(candidate, current))) throw new ChrAddressSpaceAdmissionError("address-subnet-overlap") + } else if (current.prefixLength === 0) result.defaultRoutesExcluded++ + else { + result.nonDefaultRoutesChecked++ + if (candidates.some(candidate => overlap(candidate, current))) throw new ChrAddressSpaceAdmissionError("nondefault-route-overlap") + } + } + } + return result +} diff --git a/backend/src/test/chr-vrf-companion-cleanup-proof.test.ts b/backend/src/test/chr-vrf-companion-cleanup-proof.test.ts new file mode 100644 index 00000000..3e0ac50a --- /dev/null +++ b/backend/src/test/chr-vrf-companion-cleanup-proof.test.ts @@ -0,0 +1,128 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { prepareVrfCompanionCleanupEvidence, assertVrfCompanionCleanupCheckpoint, VRF_CLEANUP_RESOURCES, vrfCleanupForeignHashes, type VrfCompanionCleanupInput } from "./chr-vrf-companion-cleanup-proof.js" +import type { NativeCatalog, NativeRow } from "./chr-vrf-companion-proof.js" + +const original = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v2.json", import.meta.url), "utf8")) +const diagnostic = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-readonly-diagnostic-v1.json", import.meta.url), "utf8")) +const baseline = Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, []])) as NativeCatalog +baseline.bridge = [{ ".id": "*F0", name: "foreign-bridge", comment: "preserve", "tx-opaque-config": "unchanged" }] +baseline.vrf = [{ ".id": "*0", name: "main", interfaces: "all", disabled: "false" }] +baseline.routing_table = [{ ".id": "*0", name: "main", dynamic: "false", fib: "true" }] +baseline.ethernet = [{ ".id": "*1", name: "ether1", "rx-opaque-config": "unchanged" }] +baseline.ipv4_route = [{ ".id": "*F1", "dst-address": "203.0.113.0/24", gateway: "ether1", "routing-table": "main", dynamic: "false" }] +baseline.ipv6_route = [{ ".id": "*F2", "dst-address": "2001:db8::/64", gateway: "ether1", "routing-table": "main", dynamic: "false" }] +baseline.interface_inventory = [{ ".id": "*1", name: "ether1", type: "ether", "default-name": "ether1", "rx-opaque-config": "unchanged" }] +baseline.ip_settings = [{ "rp-filter": "no", "opaque-bytes": "preserve" }] +baseline.ipv6_settings = [{ "multipath-hash-policy": "l3" }] +// Use the REAL diagnostic's seven ACK objects/kernel forms against an explicit +// synthetic foreign baseline. The diagnostic is candidate-only, so it cannot +// reconstruct the real foreign catalogs or prove their current hashes alone. +original.phases.find((phase: { phase: string }) => phase.phase === "baseline").foreignHashes = vrfCleanupForeignHashes(baseline) +const current = Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, [...structuredClone(baseline[resource]), ...structuredClone(diagnostic.candidates[resource])]])) as NativeCatalog +const input: VrfCompanionCleanupInput = { original, diagnostic, currentCatalog: current, sourceReportSha256: diagnostic.sourceReportSha256, cleaned: [] } +const initial = prepareVrfCompanionCleanupEvidence(input) +assert.equal(initial.deletions.length, 7) +assert.deepEqual(initial.deletions.map(item => item.resource), ["vrf", "ipv6_address", "ipv6_address", "ipv4_address", "ipv4_address", "bridge", "bridge"]) +assert.equal(initial.next?.id, "*1") +assert.equal(initial.deletions.some(item => item.resource === "routing_table" || item.resource === "interface_inventory" || item.resource.endsWith("route")), false) +assert.deepEqual(initial.foreignHashes, vrfCleanupForeignHashes(baseline)) +let rejected = 0 +function refuse(change: (copy: VrfCompanionCleanupInput) => void) { + const copy = structuredClone(input); change(copy) + assert.throws(() => prepareVrfCompanionCleanupEvidence(copy)); rejected++ +} +refuse(copy => { copy.sourceReportSha256 = "f".repeat(64) }) +refuse(copy => { (copy.diagnostic as { sourceReportSha256: string }).sourceReportSha256 = "f".repeat(64) }) +refuse(copy => { copy.currentCatalog.bridge.find(row => row.comment === original.marker)!["unknown-property"] = "changed" }) +refuse(copy => { copy.currentCatalog.ethernet[0]["rx-opaque-config"] = "changed" }) +refuse(copy => { copy.currentCatalog.ip_settings[0]["opaque-bytes"] = "changed" }) +refuse(copy => { copy.currentCatalog.ipv4_route[0].gateway = "other-physical" }) +refuse(copy => { copy.currentCatalog.bridge_port.push({ ".id": "*F3", bridge: initial.deletions[5].expected.name, interface: "ether1" }) }) +refuse(copy => { copy.currentCatalog.bridge_vlan.push({ ".id": "*F3", bridge: "foreign-bridge", tagged: initial.deletions[5].expected.name }) }) +refuse(copy => { copy.currentCatalog.interface_list_members.push({ ".id": "*F3", list: "WAN", interface: initial.deletions[5].expected.name }) }) +refuse(copy => { copy.currentCatalog.vrf.push({ ".id": "*F3", name: "foreign", interfaces: initial.deletions[5].expected.name }) }) +for (const change of [{ type: "ether" }, { name: "reused" }, { "unknown-config": "changed" }, { comment: "foreign" }, { ".id": "*F3" }]) { + refuse(copy => { Object.assign(copy.currentCatalog.interface_inventory.find(row => row.type === "vrf")!, change) }) +} +refuse(copy => { copy.currentCatalog.interface_inventory.push(structuredClone(copy.currentCatalog.interface_inventory.find(row => row.type === "vrf")!)) }) +refuse(copy => { copy.currentCatalog.routing_table.find(row => row[".id"] === "*1")!.fib = "true" }) +refuse(copy => { copy.currentCatalog.ipv4_route.find(row => row.dynamic === "true" && String(row.gateway).includes("@"))!.gateway += "-foreign" }) +refuse(copy => { copy.currentCatalog.ipv4_route.find(row => row.dynamic === "true")!["local-address"] = "192.0.2.1%ether1" }) +refuse(copy => { copy.currentCatalog.ipv6_address.push({ ".id": "*F3", interface: initial.deletions[5].expected.name, address: "fd00::1/128", dynamic: "false" }) }) +refuse(copy => { copy.cleaned = [{ resource: "bridge", id: initial.deletions[5].id }] }) +refuse(copy => { copy.cleaned = [{ resource: "vrf", id: "*1" }] }) + +const statusReceipt = { + cleanup: JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-cleanup-v1.json", import.meta.url), "utf8")), + diagnostic: JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-cleanup-readonly-v1.json", import.meta.url), "utf8")), +} +for (const observedStatus of [false, true]) { +const evolving = structuredClone(input) +const preVrf = original.phases.find((phase: { phase: string }) => phase.phase === "addresses-before-vrf") +const vrfName = String(initial.deletions[0].expected.name) +for (const deletion of initial.deletions) { + evolving.currentCatalog[deletion.resource] = evolving.currentCatalog[deletion.resource].filter(row => row[".id"] !== deletion.id) + if (deletion.resource === "vrf") { + evolving.currentCatalog.routing_table = evolving.currentCatalog.routing_table.filter(row => row[".id"] !== deletion.id) + evolving.currentCatalog.interface_inventory = evolving.currentCatalog.interface_inventory.filter(row => row.type !== "vrf") + for (const resource of ["ipv4_address", "ipv6_address"]) for (const row of evolving.currentCatalog[resource]) if (row.vrf === vrfName) row.vrf = "main" + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of evolving.currentCatalog[resource]) { + if (row["routing-table"] === vrfName) row["routing-table"] = "main" + for (const field of ["gateway", "local-address"]) if (typeof row[field] === "string" && row[field].endsWith("@" + vrfName)) row[field] = row[field].slice(0, -(vrfName.length + 1)) + } + evolving.currentCatalog.interface_inventory = evolving.currentCatalog.interface_inventory.map(row => (preVrf.owned.interface_inventory.raw as NativeRow[]).find(old => old[".id"] === row[".id"]) ?? row) + // The observed *4 -> *8 LL regeneration may recur after VRF deletion. + evolving.currentCatalog.ipv6_address.find(row => row[".id"] === "*8")![".id"] = "*9" + } else if (deletion.resource.endsWith("address")) { + const routeResource = deletion.resource === "ipv4_address" ? "ipv4_route" : "ipv6_route" + evolving.currentCatalog[routeResource] = evolving.currentCatalog[routeResource].filter(row => row["dst-address"] !== deletion.expected.address) + } else if (deletion.resource === "bridge") { + evolving.currentCatalog.interface_inventory = evolving.currentCatalog.interface_inventory.filter(row => row[".id"] !== deletion.id) + evolving.currentCatalog.ipv6_address = evolving.currentCatalog.ipv6_address.filter(row => row.interface !== deletion.expected.name) + for (const resource of ["ipv4_route", "ipv6_route"]) evolving.currentCatalog[resource] = evolving.currentCatalog[resource].filter(row => row.gateway !== deletion.expected.name) + } + evolving.cleaned = [...evolving.cleaned, { resource: deletion.resource, id: deletion.id }] + if (observedStatus && evolving.cleaned.length >= 4) { + evolving.ipv4RemovalStatusReceipt = structuredClone(statusReceipt) + for (const bridge of initial.deletions.filter(item => item.resource === "bridge")) { + const row = evolving.currentCatalog.interface_inventory.find(item => item[".id"] === bridge.id) + if (row && !evolving.currentCatalog.ipv4_address.some(address => address.interface === bridge.expected.name)) row.status = "--LERU--O---------------" + } + for (const patch of [{ status: "unknown" }, { status: "--LERUI-O---------------" }, { "opaque-status-config": "changed" }]) { + const changed = structuredClone(evolving) + const row = changed.currentCatalog.interface_inventory.find(item => item[".id"] === "*4") + if (row) { + Object.assign(row, patch) + assert.throws(() => assertVrfCompanionCleanupCheckpoint(changed)); rejected++ + } + } + const unbound = structuredClone(evolving) + ;(unbound.ipv4RemovalStatusReceipt!.cleanup as { sourceReportSha256: string }).sourceReportSha256 = "f".repeat(64) + assert.throws(() => assertVrfCompanionCleanupCheckpoint(unbound)); rejected++ + } + const checkpoint = assertVrfCompanionCleanupCheckpoint(evolving) + assert.equal(checkpoint.remaining.length, 7 - evolving.cleaned.length) + assert.deepEqual(checkpoint.foreignHashes, vrfCleanupForeignHashes(baseline)) + if (deletion.resource === "vrf") { + const reuse = structuredClone(evolving) + reuse.currentCatalog.interface_inventory.push({ ".id": "*6", name: "physical-reuse", type: "ether" }) + assert.throws(() => assertVrfCompanionCleanupCheckpoint(reuse)); rejected++ + const changed = structuredClone(evolving) + changed.currentCatalog.interface_inventory.find(row => row[".id"] === "*4")!.status = "UNKNOWN" + assert.throws(() => assertVrfCompanionCleanupCheckpoint(changed)); rejected++ + for (const patch of [{ address: "fe80::1/64" }, { "auto-link-local": "false" }, { "link-local": "false" }, { dynamic: "false" }, { vrf: vrfName }, { "unknown-config": "changed" }]) { + const refused = structuredClone(evolving) + Object.assign(refused.currentCatalog.ipv6_address.find(row => row[".id"] === "*9")!, patch) + assert.throws(() => assertVrfCompanionCleanupCheckpoint(refused)); rejected++ + } + const duplicate = structuredClone(evolving) + duplicate.currentCatalog.ipv6_address.push({ ...duplicate.currentCatalog.ipv6_address.find(row => row[".id"] === "*9")!, ".id": "*A0" }) + assert.throws(() => assertVrfCompanionCleanupCheckpoint(duplicate)); rejected++ + } +} +assert.equal(assertVrfCompanionCleanupCheckpoint(evolving).next, null) +} +assert.equal(initial.deletions.length, 7, "Evidence and original reports remain immutable across cleanup checkpoints") +assert.equal(original.unknownWrite, true, "Original latch must never be cleared") +console.log(`VRF cleanup pure proof: actual diagnostic kernel forms + synthetic foreign baseline, seven ACK-only deletes, seven phase-aware checkpoints, ${rejected} refusal models; no executor/SQL/network`) diff --git a/backend/src/test/chr-vrf-companion-cleanup-proof.ts b/backend/src/test/chr-vrf-companion-cleanup-proof.ts new file mode 100644 index 00000000..03f49e60 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-cleanup-proof.ts @@ -0,0 +1,190 @@ +import { createHash } from "node:crypto" +import { isDeepStrictEqual } from "node:util" +import { configurationRows, resourceById } from "../modules/mcp/resources.js" +import { assertAcceptedOwnership, assertOwnedAddressClosure, assertVrfMembersOwned, deriveOwnedConnectedRoute, type NativeCatalog, type NativeOwnedObject, type NativeRow } from "./chr-vrf-companion-proof.js" +import { stripNativeTelemetry } from "./chr-vrf-companion-telemetry.js" +import { parseChrVrfPrefix } from "./chr-vrf-companion-address-space.js" + +export const VRF_CLEANUP_RESOURCES = ["bridge", "bridge_port", "bridge_vlan", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table", "ethernet", "ip_filter", "ipv6_filter", "routing_rule", "ip_settings", "ipv6_settings", "interface_inventory", "interface_list_members"] as const +export interface CleanupIdentity { resource: string; id: string } +export interface VrfCompanionCleanupInput { + original: unknown; diagnostic: unknown; currentCatalog: NativeCatalog; + /** Caller computes this from the immutable original FILE bytes. */ + sourceReportSha256: string; cleaned: readonly CleanupIdentity[]; + /** Optional, separately pinned ACK + GET evidence for the lab's exact + * inventory status transition after removing the last owned IPv4 address. */ + ipv4RemovalStatusReceipt?: { cleanup: unknown; diagnostic: unknown }; +} +export interface VrfCompanionCleanupEvidence { + schema: "chr-vrf-companion-cleanup-proof-v1"; marker: string; sourceReportSha256: string; + deletions: NativeOwnedObject[]; remaining: NativeOwnedObject[]; next: NativeOwnedObject | null; + cleaned: CleanupIdentity[]; foreignHashes: Record; kernelExclusions: CleanupIdentity[]; +} +function requireProof(condition: unknown): asserts condition { if (!condition) throw Error("VRF cleanup proof refused; no DELETE authorized") } +function object(value: unknown): NativeRow { requireProof(value && typeof value === "object" && !Array.isArray(value)); return value as NativeRow } +function array(value: unknown): unknown[] { requireProof(Array.isArray(value) && value.length <= 50000); return value } +const id = (value: unknown): value is string => typeof value === "string" && /^\*[0-9a-f]+$/i.test(value) +const key = (item: CleanupIdentity) => `${item.resource}/${item.id}` +const truth = (value: unknown) => [true, "true", "yes"].includes(value as string | boolean) +const canonical = (value: unknown): unknown => Array.isArray(value) ? value.map(canonical) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([name, child]) => [name, canonical(child)])) : value +export const vrfCleanupHash = (value: unknown): string => createHash("sha256").update(JSON.stringify(canonical(value))).digest("hex") +function project(resource: string, rows: NativeRow[]) { + if (resource === "interface_inventory") return rows.map(row => Object.fromEntries([".id", "name", "type", "default-name", "disabled"].filter(field => Object.hasOwn(row, field)).map(field => [field, row[field]]))) + if (resource === "interface_list_members") return rows.map(row => Object.fromEntries([".id", "list", "interface", "dynamic", "disabled", "comment"].filter(field => Object.hasOwn(row, field)).map(field => [field, row[field]]))) + return configurationRows(resourceById(resource), rows) +} +export function vrfCleanupForeignHashes(catalog: NativeCatalog, exclusions: readonly CleanupIdentity[] = []): Record { + const excluded = new Set(exclusions.map(key)) + return Object.fromEntries(VRF_CLEANUP_RESOURCES.flatMap(resource => { + const rows = catalog[resource].filter(row => !excluded.has(`${resource}/${String(row[".id"])}`)) + return [[resource, vrfCleanupHash(project(resource, rows))], [`${resource}:native-configuration`, vrfCleanupHash(rows.map(stripNativeTelemetry))]] + })) +} +function one(rows: unknown, nativeId: string): NativeRow { + const matches = array(rows).map(object).filter(row => row[".id"] === nativeId) + requireProof(matches.length === 1); return matches[0] +} +function phaseRows(phase: NativeRow, resource: string): unknown { return object(object(phase.owned)[resource]).raw } +function conserved(expected: NativeRow, current: NativeRow, omit: readonly string[] = []) { + const strip = (row: NativeRow) => Object.fromEntries(Object.entries(stripNativeTelemetry(row)).filter(([field]) => !omit.includes(field))) + requireProof(isDeepStrictEqual(strip(expected), strip(current))) +} +function linkLocalSuccessor(old: NativeRow, current: NativeRow, bridge: NativeOwnedObject) { + const previous = parseChrVrfPrefix(old.address, 6), next = parseChrVrfPrefix(current.address, 6) + requireProof(previous && next && previous.prefixLength === 64 && next.prefixLength === 64 && previous.host === next.host && next.host >> 64n === 0xfe80000000000000n) + requireProof([old, current].every(row => truth(row.dynamic) && truth(row["auto-link-local"]) && truth(row["link-local"]) && row.interface === bridge.expected.name)) + requireProof(current.vrf === "main" && typeof bridge.expected["mac-address"] === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(bridge.expected["mac-address"])) + const mac = bridge.expected["mac-address"].split(":").map(value => Number.parseInt(value, 16)) + const eui = [mac[0] ^ 2, mac[1], mac[2], 255, 254, mac[3], mac[4], mac[5]].reduce((value, byte) => (value << 8n) | BigInt(byte), 0n) + requireProof((next.host & ((1n << 64n) - 1n)) === eui) + conserved({ ...old, address: previous.hostKey }, { ...current, address: next.hostKey }, [".id", "vrf"]) +} + +/** New independent cleanup admission; never mutates/restarts the old runner or + * adopts objects by planned names. The only deletable identities are seven + * acknowledged original PUT IDs; all kernel classifications remain read-only. */ +export function prepareVrfCompanionCleanupEvidence(input: VrfCompanionCleanupInput): VrfCompanionCleanupEvidence { + const original = object(input.original), diagnostic = object(input.diagnostic) + requireProof(/^[a-f0-9]{64}$/.test(input.sourceReportSha256) && diagnostic.sourceReportSha256 === input.sourceReportSha256) + requireProof(original.schema === "chr-vrf-companion-native-v2" && original.proofVersion === 2 && original.nativeOnly === true && original.complete === false && original.cleanupComplete === false && original.unknownWrite === true) + requireProof(diagnostic.readOnly === true && diagnostic.nativeMutations === 0 && diagnostic.candidateAttributionIsNotOwnership === true) + requireProof(typeof original.marker === "string" && /^mcp-test-vrf-companion-[0-9a-f-]{36}$/.test(original.marker)) + const marker = original.marker, phases = array(original.phases).map(object), journal = array(original.journal).map(object) + requireProof(journal.length === 7) + const baseline = phases.filter(phase => phase.phase === "baseline"), preVrf = phases.filter(phase => phase.phase === "addresses-before-vrf"), postVrf = phases.filter(phase => phase.phase === "vrf-create") + requireProof(baseline.length === 1 && preVrf.length === 1 && postVrf.length === 1 && baseline[0].proofStatus === "passed" && baseline[0].foreignStable === true && preVrf[0].proofStatus === "passed" && preVrf[0].foreignStable === true) + const baselineHashes = object(baseline[0].foreignHashes) + requireProof(Object.keys(baselineHashes).length === 34 && VRF_CLEANUP_RESOURCES.every(resource => [resource, `${resource}:native-configuration`].every(field => typeof baselineHashes[field] === "string" && /^[a-f0-9]{64}$/.test(baselineHashes[field] as string)))) + const owned: NativeOwnedObject[] = [], identities = new Set() + for (const [index, command] of journal.entries()) { + const resource = command.resource + requireProof(typeof resource === "string" && ["bridge", "ipv4_address", "ipv6_address", "vrf"].includes(resource) && command.method === "PUT" && command.outcome === "acknowledged" && command.ownedIdentityConfirmed === true && id(command.assignedId)) + requireProof(index === 6 ? resource === "vrf" : resource !== "vrf" && command.readbackVerified === true) + const expected = one(phaseRows(resource === "vrf" ? postVrf[0] : preVrf[0], resource), command.assignedId) + requireProof(expected.comment === marker) + const fields = object(command.fields) + requireProof(fields.comment === marker && Object.entries(fields).every(([field, value]) => field !== ".id" && (isDeepStrictEqual(expected[field], value) || ["disabled", "advertise"].includes(field) && [true, "true", "yes", false, "false", "no"].includes(value as string | boolean) && [true, "true", "yes", false, "false", "no"].includes(expected[field] as string | boolean) && truth(expected[field]) === truth(value)))) + const item = { resource, marker, id: command.assignedId, expected: structuredClone(expected) } + requireProof(!identities.has(key(item))); identities.add(key(item)); owned.push(item) + } + requireProof(["bridge", "ipv4_address", "ipv6_address"].every(resource => owned.filter(item => item.resource === resource).length === 2)) + const recordedRemaining = array(original.knownRemaining).map(object) + requireProof(recordedRemaining.length === 7 && new Set(recordedRemaining.map(item => `${String(item.resource)}/${String(item.id)}`)).size === 7 && recordedRemaining.every(item => identities.has(`${String(item.resource)}/${String(item.id)}`))) + const bridges = owned.filter(item => item.resource === "bridge"), vrf = owned.find(item => item.resource === "vrf")! + assertVrfMembersOwned(vrf.expected, bridges) + const deletions = [vrf, ...owned.filter(item => item.resource === "ipv6_address"), ...owned.filter(item => item.resource === "ipv4_address"), ...bridges] + requireProof(Array.isArray(input.cleaned) && input.cleaned.length <= 7 && input.cleaned.every((item, index) => item && typeof item.resource === "string" && id(item.id) && key(item) === key(deletions[index]))) + const cleaned = input.cleaned.map(item => ({ resource: item.resource, id: item.id })), cleanedKeys = new Set(cleaned.map(key)), remaining = deletions.filter(item => !cleanedKeys.has(key(item))) + const current = input.currentCatalog + for (const resource of VRF_CLEANUP_RESOURCES) { + const rows = array(current[resource]).map(object), seen = new Set() + for (const row of rows) { + if (["ip_settings", "ipv6_settings"].includes(resource) && row[".id"] === undefined) continue + requireProof(id(row[".id"]) && !seen.has(row[".id"])); seen.add(row[".id"]) + } + } + for (const item of deletions) if (cleanedKeys.has(key(item))) requireProof(!current[item.resource].some(row => row[".id"] === item.id)) + const vrfPresent = !cleanedKeys.has(key(vrf)), remainingBridges = remaining.filter(item => item.resource === "bridge") + let ipv4RemovedStatus: string | undefined + if (input.ipv4RemovalStatusReceipt) { + const receipt = object(input.ipv4RemovalStatusReceipt.cleanup), observation = object(input.ipv4RemovalStatusReceipt.diagnostic) + const commands = array(receipt.journal).map(object) + requireProof(receipt.sourceReportSha256 === input.sourceReportSha256 && receipt.apply === true && receipt.complete === false && receipt.stopped === true && commands.length === 4) + requireProof(commands.every((command, index) => command.resource === deletions[index].resource && command.id === deletions[index].id && command.outcome === "acknowledged" && (index === 3 || command.readbackVerified === true))) + requireProof(observation.sourceReportSha256 === input.sourceReportSha256 && observation.readOnly === true && observation.nativeMutations === 0 && observation.candidateAttributionIsNotOwnership === true) + requireProof(cleaned.length >= 4 && !vrfPresent) + const removedAddress = deletions[3], parent = bridges.find(item => item.expected.name === removedAddress.expected.interface) + requireProof(parent) + const before = one(phaseRows(preVrf[0], "interface_inventory"), parent.id) + const after = one(object(observation.candidates).interface_inventory, parent.id) + // This exact observed pair is deliberately not a telemetry allowlist or + // a general interpretation of RouterOS status characters. + requireProof(before.status === "--LERUI-O---------------" && after.status === "--LERU--O---------------") + conserved(before, after, ["status"]) + requireProof(!array(object(observation.candidates).ipv4_address).map(object).some(row => row.interface === parent.expected.name)) + ipv4RemovedStatus = after.status + } + const allowedVrfs = ["main", vrf.id, String(vrf.expected.name)] + assertAcceptedOwnership(current, remaining, { allowedAddressVrfs: allowedVrfs }) + if (!vrfPresent) for (const item of remaining.filter(item => item.resource.endsWith("address"))) conserved(item.expected, one(current[item.resource], item.id)) + const closure = assertOwnedAddressClosure(current, remaining, remainingBridges, marker, { allowedAddressVrfs: vrfPresent ? allowedVrfs : ["main"] }) + const names = bridges.map(item => String(item.expected.name)) + requireProof([...current.bridge_port, ...current.bridge_vlan, ...current.interface_list_members].every(row => !names.includes(String(row.bridge)) && !names.includes(String(row.interface)) && ![row.tagged, row.untagged].some(value => typeof value === "string" && value.split(",").some(name => names.includes(name))))) + requireProof(current.vrf.every(row => row[".id"] === vrf.id && vrfPresent || typeof row.interfaces === "string" && !row.interfaces.split(",").some(name => names.includes(name)) && (row.name === "main" || row.interfaces !== "all"))) + const diagnosticCatalog = object(diagnostic.candidates), exclusions: CleanupIdentity[] = remaining.map(({ resource, id }) => ({ resource, id })) + for (const bridge of bridges) { + const expected = { ...one(vrfPresent ? diagnosticCatalog.interface_inventory : phaseRows(preVrf[0], "interface_inventory"), bridge.id) } + if (ipv4RemovedStatus && !remaining.some(item => item.resource === "ipv4_address" && item.expected.interface === bridge.expected.name)) { + requireProof(expected.status === "--LERUI-O---------------") + expected.status = ipv4RemovedStatus + } + requireProof(expected.type === "bridge" && expected.name === bridge.expected.name && expected.comment === marker) + if (cleanedKeys.has(key(bridge))) requireProof(!current.interface_inventory.some(row => row[".id"] === bridge.id)) + else { conserved(expected, one(current.interface_inventory, bridge.id)); exclusions.push({ resource: "interface_inventory", id: bridge.id }) } + } + const virtuals = array(diagnosticCatalog.interface_inventory).map(object).filter(row => row.type === "vrf" && row.name === vrf.expected.name) + requireProof(virtuals.length === 1 && id(virtuals[0][".id"]) && !bridges.some(bridge => bridge.id === virtuals[0][".id"]) && virtuals[0].vrf === "main" && virtuals[0].comment === undefined) + const virtual = virtuals[0] + if (vrfPresent) { conserved(virtual, one(current.interface_inventory, virtual[".id"] as string)); exclusions.push({ resource: "interface_inventory", id: virtual[".id"] as string }) } + else requireProof(!current.interface_inventory.some(row => row[".id"] === virtual[".id"])) + const table = one(diagnosticCatalog.routing_table, vrf.id) + requireProof(truth(table.dynamic) && table.name === vrf.expected.name && table.fib === "" && table.vrf === undefined) + if (vrfPresent) { conserved(table, one(current.routing_table, vrf.id)); exclusions.push({ resource: "routing_table", id: vrf.id }) } + else requireProof(!current.routing_table.some(row => row[".id"] === vrf.id)) + const diagnosticClosure = assertOwnedAddressClosure(diagnosticCatalog as NativeCatalog, owned, bridges, marker, { allowedAddressVrfs: allowedVrfs }) + for (const old of diagnosticClosure.linkLocalCompanions) { + const bridge = bridges.find(item => item.expected.name === old.interface)! + if (cleanedKeys.has(key(bridge))) requireProof(!current.ipv6_address.some(row => row[".id"] === old.id)) + else { + const now = closure.linkLocalCompanions.find(item => item.interface === old.interface) + requireProof(now) + if (vrfPresent) { requireProof(now.id === old.id); conserved(old.row, now.row, ["vrf"]) } + else linkLocalSuccessor(old.row, now.row, bridge) + exclusions.push({ resource: "ipv6_address", id: now.id }) + } + } + requireProof(closure.linkLocalCompanions.every(item => diagnosticClosure.linkLocalCompanions.some(old => old.interface === item.interface))) + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of current[resource]) { + const normalized = { ...row }, gateway = row.gateway, tableName = row["routing-table"] + if (typeof gateway === "string" && vrfPresent && tableName === vrf.expected.name) { + const bridge = remainingBridges.find(item => gateway === `${String(item.expected.name)}@${String(vrf.expected.name)}`) + if (bridge) normalized.gateway = bridge.expected.name + } + const route = deriveOwnedConnectedRoute(normalized, { addresses: closure, bridges: remainingBridges, allowedTables: vrfPresent ? ["main", String(vrf.expected.name)] : ["main"] }) + if (route) { + requireProof(row["immediate-gw"] === route.interface && (gateway === route.interface || vrfPresent && tableName === vrf.expected.name && gateway === `${route.interface}@${String(vrf.expected.name)}`)) + if (row["local-address"] !== undefined) { + const address = route.addressKind === "kernel-link-local" ? closure.linkLocalCompanions.find(item => item.id === route.addressId)?.address : closure.staticAddresses.find(item => item.id === route.addressId && item.resource === (resource === "ipv4_route" ? "ipv4_address" : "ipv6_address"))?.expected.address + requireProof(typeof address === "string" && row["local-address"] === `${address.split("/")[0]}%${route.interface}${tableName === "main" ? "" : `@${String(vrf.expected.name)}`}`) + } + exclusions.push({ resource, id: route.id }) + } + } + const hashes = vrfCleanupForeignHashes(current, exclusions) + requireProof(isDeepStrictEqual(hashes, baselineHashes)) + return { schema: "chr-vrf-companion-cleanup-proof-v1", marker, sourceReportSha256: input.sourceReportSha256, deletions: structuredClone(deletions), remaining: structuredClone(remaining), next: structuredClone(remaining[0] ?? null), cleaned, foreignHashes: hashes, kernelExclusions: exclusions.filter(item => !identities.has(key(item))) } +} + +export function assertVrfCompanionCleanupCheckpoint(input: VrfCompanionCleanupInput): VrfCompanionCleanupEvidence { + return prepareVrfCompanionCleanupEvidence(input) +} diff --git a/backend/src/test/chr-vrf-companion-disabled-cleanup-proof.test.ts b/backend/src/test/chr-vrf-companion-disabled-cleanup-proof.test.ts new file mode 100644 index 00000000..c83e0f1f --- /dev/null +++ b/backend/src/test/chr-vrf-companion-disabled-cleanup-proof.test.ts @@ -0,0 +1,105 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { prepareDisabledVrfCleanupEvidence, assertDisabledVrfCleanupCheckpoint, type DisabledVrfCleanupInput } from "./chr-vrf-companion-disabled-cleanup-proof.js" +import { VRF_CLEANUP_RESOURCES, vrfCleanupForeignHashes } from "./chr-vrf-companion-cleanup-proof.js" +import type { NativeCatalog, NativeRow } from "./chr-vrf-companion-proof.js" + +const original = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v3.json", import.meta.url), "utf8")) +const diagnostic = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-readonly-diagnostic-v3.json", import.meta.url), "utf8")) +const baseline = Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, []])) as NativeCatalog +baseline.bridge = [{ ".id": "*F0", name: "foreign-bridge", comment: "preserve", "tx-opaque-config": "unchanged" }] +baseline.vrf = [{ ".id": "*0", name: "main", interfaces: "all", disabled: "false" }] +baseline.routing_table = [{ ".id": "*0", name: "main", dynamic: "false", fib: "true" }] +baseline.ethernet = [{ ".id": "*1", name: "ether1", "rx-opaque-config": "unchanged" }] +baseline.ipv4_route = [{ ".id": "*F1", "dst-address": "203.0.113.0/24", gateway: "ether1", "routing-table": "main", dynamic: "false" }] +baseline.ipv6_route = [{ ".id": "*F2", "dst-address": "2001:db8::/64", gateway: "ether1", "routing-table": "main", dynamic: "false" }] +baseline.interface_inventory = [{ ".id": "*1", name: "ether1", type: "ether", "default-name": "ether1", "rx-opaque-config": "unchanged" }] +baseline.ip_settings = [{ "rp-filter": "no", "opaque-bytes": "preserve" }] +baseline.ipv6_settings = [{ "multipath-hash-policy": "l3" }] +// Actual immutable v3 owned raw shapes with an explicit synthetic foreign +// baseline. Candidate-only diagnostics cannot reconstruct foreign raw rows. +for (const phase of original.phases) if (phase.proofStatus === "passed") phase.foreignHashes = vrfCleanupForeignHashes(baseline) +const current = Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, [...structuredClone(baseline[resource]), ...structuredClone(diagnostic.candidates[resource])]])) as NativeCatalog +const input: DisabledVrfCleanupInput = { original, diagnostic, currentCatalog: current, sourceReportSha256: diagnostic.sourceReportSha256, cleaned: [] } +const initial = prepareDisabledVrfCleanupEvidence(input) +assert.equal(initial.deletions.length, 7) +assert.deepEqual(initial.deletions.map(item => item.resource), ["vrf", "ipv6_address", "ipv6_address", "ipv4_address", "ipv4_address", "bridge", "bridge"]) +assert.equal(initial.next?.id, "*2") +assert.equal(initial.next?.expected.disabled, "true") +assert.equal(initial.next?.expected.name, `${original.marker}-r`) +assert.deepEqual(initial.foreignHashes, vrfCleanupForeignHashes(baseline)) +assert.equal(initial.deletions.some(item => ["routing_table", "interface_inventory", "ipv4_route", "ipv6_route"].includes(item.resource)), false) +let refused = 0 +function reject(change: (copy: DisabledVrfCleanupInput) => void) { + const copy = structuredClone(input); change(copy) + assert.throws(() => prepareDisabledVrfCleanupEvidence(copy)); refused++ +} +reject(copy => { copy.sourceReportSha256 = "f".repeat(64) }) +reject(copy => { (copy.diagnostic as NativeRow).sourceReportSha256 = "f".repeat(64) }) +reject(copy => { (copy.original as typeof original).journal[8].outcome = "unknown" }) +reject(copy => { (copy.original as typeof original).journal[8].requestedChange = undefined }) +reject(copy => { (copy.original as typeof original).journal[7].readbackVerified = false }) +reject(copy => { (copy.original as typeof original).journal[7].fields.name = "foreign" }) +reject(copy => { (copy.original as typeof original).journal[0].assignedId = "*FF" }) +reject(copy => { (copy.original as typeof original).journal.push(structuredClone((copy.original as typeof original).journal[8])) }) +reject(copy => { (copy.original as typeof original).phases.find((phase: NativeRow) => phase.phase === "vrf-rename").foreignHashes.ethernet = "f".repeat(64) }) +reject(copy => { copy.cleaned = [{ resource: "bridge", id: "*7" }] }) +reject(copy => { copy.currentCatalog.vrf.find(row => row[".id"] === "*2")!.disabled = "false" }) +reject(copy => { copy.currentCatalog.vrf.find(row => row[".id"] === "*2")!.name = `${original.marker}-v` }) +reject(copy => { copy.currentCatalog.routing_table.push({ ".id": "*2", name: `${original.marker}-r`, dynamic: "true", fib: "" }) }) +reject(copy => { copy.currentCatalog.ethernet[0]["rx-opaque-config"] = "changed" }) +reject(copy => { copy.currentCatalog.ip_settings[0]["opaque-bytes"] = "changed" }) +for (const resource of ["ipv4_route", "ipv6_route"]) reject(copy => { copy.currentCatalog[resource] = copy.currentCatalog[resource].filter(row => row.gateway !== `${original.marker}-a`) }) +reject(copy => { copy.currentCatalog.bridge_port.push({ ".id": "*FF", bridge: `${original.marker}-a`, interface: "ether1" }) }) +reject(copy => { copy.currentCatalog.bridge_vlan.push({ ".id": "*FF", bridge: "foreign", tagged: `${original.marker}-a` }) }) +reject(copy => { copy.currentCatalog.interface_list_members.push({ ".id": "*FF", interface: `${original.marker}-a`, list: "WAN" }) }) +reject(copy => { copy.currentCatalog.vrf.push({ ".id": "*FF", name: "external", interfaces: `${original.marker}-a` }) }) +for (const change of [{ type: "bridge" }, { name: "foreign" }, { vrf: "foreign" }, { ".id": "*FF" }, { disabled: "false" }, { status: "--LERU--O---------------" }, { "mac-address": "86:34:5A:BA:8F:6E" }, { ifname: "vrf2" }, { "opaque-field": "new" }, { "tx-opaque-config": "changed" }]) { + reject(copy => { Object.assign(copy.currentCatalog.interface_inventory.find(row => row.type === "vrf")!, change) }) +} +for (const resource of ["bridge", "ipv4_address", "ipv6_address"]) reject(copy => { copy.currentCatalog[resource].find(row => row.comment === original.marker)!["unknown-field"] = "changed" }) +for (const resource of ["ipv4_address", "ipv6_address"]) reject(copy => { copy.currentCatalog[resource].find(row => row.comment === original.marker)!.vrf = `${original.marker}-r` }) +for (const change of [{ dynamic: "false" }, { "auto-link-local": "false" }, { "link-local": "false" }, { address: "fe80::1234/64" }, { ".id": "*FF" }, { "opaque-field": "new" }]) reject(copy => { Object.assign(copy.currentCatalog.ipv6_address.find(row => row[".id"] === "*F")!, change) }) +for (const change of [{ "local-address": "198.18.155.1%ether1" }, { "routing-table": `${original.marker}-r` }, { gateway: `${original.marker}-a@${original.marker}-r` }, { "unknown-field": "changed" }, { ".id": "*FF" }]) reject(copy => { Object.assign(copy.currentCatalog.ipv4_route.find(row => row.gateway === `${original.marker}-a`)!, change) }) +// Full unknown fields are conserved across the source→disabled diagnostic +// boundary too, not just against the subsequently pinned current snapshot. +for (const kind of ["bridge", "virtual", "link-local", "route"]) reject(copy => { + const diag = copy.diagnostic as typeof diagnostic + const resource = kind === "bridge" || kind === "virtual" ? "interface_inventory" : kind === "link-local" ? "ipv6_address" : "ipv4_route" + const row = kind === "virtual" ? diag.candidates[resource].find((row: NativeRow) => row.type === "vrf") : kind === "link-local" ? diag.candidates[resource].find((row: NativeRow) => row[".id"] === "*F") : diag.candidates[resource][0] + row["tx-unreviewed-config"] = "changed" + const currentRow = copy.currentCatalog[resource].find(current => current[".id"] === row[".id"])! + currentRow["tx-unreviewed-config"] = "changed" +}) +const evolving = structuredClone(input) +for (const deletion of initial.deletions) { + evolving.currentCatalog[deletion.resource] = evolving.currentCatalog[deletion.resource].filter(row => row[".id"] !== deletion.id) + if (deletion.resource === "vrf") { + evolving.currentCatalog.interface_inventory = evolving.currentCatalog.interface_inventory.filter(row => row.type !== "vrf") + // A successor is permitted only after this acknowledged VRF deletion. + evolving.currentCatalog.ipv6_address.find(row => row[".id"] === "*F")![".id"] = "*10" + } else if (deletion.resource.endsWith("address")) { + const routes = deletion.resource === "ipv4_address" ? "ipv4_route" : "ipv6_route" + evolving.currentCatalog[routes] = evolving.currentCatalog[routes].filter(row => row["dst-address"] !== deletion.expected.address) + if (deletion.resource === "ipv4_address") evolving.currentCatalog.interface_inventory.find(row => row.name === deletion.expected.interface)!.status = "--LERU--O---------------" + } else { + evolving.currentCatalog.interface_inventory = evolving.currentCatalog.interface_inventory.filter(row => row[".id"] !== deletion.id) + evolving.currentCatalog.ipv6_address = evolving.currentCatalog.ipv6_address.filter(row => row.interface !== deletion.expected.name) + evolving.currentCatalog.ipv6_route = evolving.currentCatalog.ipv6_route.filter(row => row.gateway !== deletion.expected.name) + } + evolving.cleaned = [...evolving.cleaned, { resource: deletion.resource, id: deletion.id }] + const checkpoint = assertDisabledVrfCleanupCheckpoint(evolving) + assert.deepEqual(checkpoint.foreignHashes, vrfCleanupForeignHashes(baseline)) + assert.equal(checkpoint.remaining.length, 7 - evolving.cleaned.length) + if (evolving.cleaned.length === 1) { + const reused = structuredClone(evolving) + reused.currentCatalog.interface_inventory.push({ ".id": "*9", name: "reused", type: "ether" }) + assert.throws(() => assertDisabledVrfCleanupCheckpoint(reused)); refused++ + const collateral = structuredClone(evolving) + collateral.currentCatalog.ipv6_address.find(row => row[".id"] === "*10")!["unknown-field"] = "changed" + assert.throws(() => assertDisabledVrfCleanupCheckpoint(collateral)); refused++ + } +} +assert.equal(assertDisabledVrfCleanupCheckpoint(evolving).next, null) +assert.deepEqual(vrfCleanupForeignHashes(evolving.currentCatalog), vrfCleanupForeignHashes(baseline)) +console.log(`Disabled VRF cleanup pure proof: actual v3 source/diagnostic + synthetic foreign baseline, seven ACK-only checkpoints, ${refused} refusal models; no executor/SQL/network`) diff --git a/backend/src/test/chr-vrf-companion-disabled-cleanup-proof.ts b/backend/src/test/chr-vrf-companion-disabled-cleanup-proof.ts new file mode 100644 index 00000000..372acbd2 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-disabled-cleanup-proof.ts @@ -0,0 +1,180 @@ +import { isDeepStrictEqual } from "node:util" +import { assertAcceptedOwnership, assertNativeKernelEvidence, assertOwnedAddressClosure, assertVrfMembersOwned, deriveOwnedConnectedRoute, type NativeCatalog, type NativeKernelEvidence, type NativeOwnedObject, type NativeRow } from "./chr-vrf-companion-proof.js" +import { VRF_CLEANUP_RESOURCES, vrfCleanupForeignHashes, type CleanupIdentity } from "./chr-vrf-companion-cleanup-proof.js" +import { stripNativeTelemetry } from "./chr-vrf-companion-telemetry.js" +import { parseChrVrfPrefix } from "./chr-vrf-companion-address-space.js" + +export interface DisabledVrfCleanupInput { + original: unknown; diagnostic: unknown; currentCatalog: NativeCatalog; + /** Computed by the executor from immutable original file bytes. */ + sourceReportSha256: string; cleaned: readonly CleanupIdentity[]; +} +export interface DisabledVrfCleanupEvidence { + schema: "chr-vrf-companion-disabled-cleanup-proof-v1"; marker: string; sourceReportSha256: string; + deletions: NativeOwnedObject[]; remaining: NativeOwnedObject[]; next: NativeOwnedObject | null; + cleaned: CleanupIdentity[]; foreignHashes: Record; kernelExclusions: CleanupIdentity[]; +} +function proof(value: unknown): asserts value { if (!value) throw Error("Disabled VRF cleanup proof refused; no DELETE authorized") } +const nativeId = (value: unknown): value is string => typeof value === "string" && /^\*[0-9a-f]+$/i.test(value) +const key = (item: CleanupIdentity) => `${item.resource}/${item.id}` +function obj(value: unknown): NativeRow { proof(value && typeof value === "object" && !Array.isArray(value)); return value as NativeRow } +function arr(value: unknown): unknown[] { proof(Array.isArray(value) && value.length <= 50000); return value } +function rows(value: unknown): NativeRow[] { return arr(value).map(obj) } +function one(value: unknown, id: string): NativeRow { const matches = rows(value).filter(row => row[".id"] === id); proof(matches.length === 1); return matches[0] } +function phaseRows(phase: NativeRow, resource: string): NativeRow[] { return rows(obj(obj(phase.owned)[resource]).raw) } +function rawCatalog(value: NativeRow): NativeCatalog { return Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, rows(value[resource])])) } +function phaseCatalog(phase: NativeRow): NativeCatalog { return Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, phaseRows(phase, resource)])) } +function same(old: NativeRow, current: NativeRow, omit: readonly string[] = []) { + const config = (row: NativeRow) => Object.fromEntries(Object.entries(stripNativeTelemetry(row)).filter(([field]) => !omit.includes(field))) + proof(isDeepStrictEqual(config(old), config(current))) +} +function validateCatalog(catalog: NativeCatalog) { + for (const resource of VRF_CLEANUP_RESOURCES) { + const entries = rows(catalog[resource]), seen = new Set() + for (const row of entries) { + if (["ip_settings", "ipv6_settings"].includes(resource) && row[".id"] === undefined) continue + proof(nativeId(row[".id"]) && !seen.has(row[".id"])); seen.add(row[".id"]) + } + } +} +function linkLocalSuccessor(old: NativeRow, current: NativeRow, bridge: NativeOwnedObject) { + const before = parseChrVrfPrefix(old.address, 6), after = parseChrVrfPrefix(current.address, 6) + proof(before && after && before.prefixLength === 64 && after.prefixLength === 64 && before.host === after.host && after.host >> 64n === 0xfe80000000000000n) + proof([old, current].every(row => row.dynamic === "true" && row["auto-link-local"] === "true" && row["link-local"] === "true" && row.vrf === "main" && row.interface === bridge.expected.name)) + const mac = bridge.expected["mac-address"] + proof(typeof mac === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(mac)) + const bytes = mac.split(":").map(value => Number.parseInt(value, 16)) + const eui = [bytes[0] ^ 2, bytes[1], bytes[2], 255, 254, bytes[3], bytes[4], bytes[5]].reduce((value, byte) => value << 8n | BigInt(byte), 0n) + proof((after.host & ((1n << 64n) - 1n)) === eui) + same({ ...old, address: before.hostKey }, { ...current, address: after.hostKey }, [".id"]) +} +function disabledVirtual(old: NativeRow, current: NativeRow) { + proof(old.type === "vrf" && current.type === "vrf" && old[".id"] === current[".id"] && old.name === current.name && old.vrf === "main" && current.vrf === "main" && old.comment === undefined && current.comment === undefined) + proof(old.disabled === "false" && current.disabled === "true" && old.status === "--LERU--O---------------" && current.status === "------------------------") + proof(typeof old["mac-address"] === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(old["mac-address"]) && old["mac-address"] !== "00:00:00:00:00:00" && current["mac-address"] === "00:00:00:00:00:00") + proof(old["link-downs"] === "0" && current["link-downs"] === "1" && old["last-link-down-time"] === undefined && typeof current["last-link-down-time"] === "string" && /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(current["last-link-down-time"])) + proof(typeof old["last-link-up-time"] === "string" && current["last-link-down-time"] >= old["last-link-up-time"]) + proof(typeof old.ifname === "string" && /^vrf\d+$/.test(old.ifname) && typeof old.ifindex === "string" && /^[1-9]\d*$/.test(old.ifindex) && old.flags === " UP RUNNING NOARP MASTER LOWER_UP (0x104c1)") + proof(["ifname", "ifindex", "flags", "actual-mtu"].every(field => !Object.hasOwn(current, field))) + proof(old["actual-mtu"] === "65536" && old.running === "true" && current.running === "false") + same(old, current, ["disabled", "status", "mac-address", "link-downs", "last-link-down-time", "ifname", "ifindex", "flags"]) +} + +/** Independent admission for the immutable v3 ACK-disable stop. No runner + * resume, no adoption by names, and no kernel IDs are ever deletable. */ +export function prepareDisabledVrfCleanupEvidence(input: DisabledVrfCleanupInput): DisabledVrfCleanupEvidence { + const original = obj(input.original), diagnostic = obj(input.diagnostic) + proof(/^[a-f0-9]{64}$/.test(input.sourceReportSha256) && diagnostic.sourceReportSha256 === input.sourceReportSha256) + proof(original.schema === "chr-vrf-companion-native-v3" && original.proofVersion === 3 && original.nativeOnly === true && original.complete === false && original.cleanupComplete === false && original.unknownWrite === true && typeof original.finishedAt === "string") + proof(diagnostic.readOnly === true && diagnostic.nativeMutations === 0 && diagnostic.candidateAttributionIsNotOwnership === true) + proof(typeof original.marker === "string" && /^mcp-test-vrf-companion-[0-9a-f-]{36}$/.test(original.marker)) + const marker = original.marker, phases = rows(original.phases), journal = rows(original.journal) + const phase = (name: string) => { const found = phases.filter(item => item.phase === name); proof(found.length === 1); return found[0] } + const baseline = phase("baseline"), preVrf = phase("addresses-before-vrf"), renamed = phase("vrf-rename"), final = phase("finally-readonly-classification") + const baselineHashes = obj(baseline.foreignHashes) + proof(Object.keys(baselineHashes).length === 34 && VRF_CLEANUP_RESOURCES.every(resource => [resource, `${resource}:native-configuration`].every(field => typeof baselineHashes[field] === "string" && /^[a-f0-9]{64}$/.test(baselineHashes[field] as string)))) + for (const recorded of [baseline, preVrf, renamed]) proof(recorded.proofStatus === "passed" && recorded.foreignStable === true && isDeepStrictEqual(recorded.foreignHashes, baselineHashes)) + proof(final.proofStatus === "refused-unproven-native-state" && journal.length === 9) + const owned: NativeOwnedObject[] = [], ids = new Set() + for (const [index, command] of journal.slice(0, 7).entries()) { + proof(command.method === "PUT" && command.outcome === "acknowledged" && command.ownedIdentityConfirmed === true && command.readbackVerified === true && command.requestedChange === true && nativeId(command.assignedId)) + proof(command.resource === (index < 2 ? "bridge" : index === 6 ? "vrf" : index % 2 === 0 ? "ipv4_address" : "ipv6_address")) + const resource = String(command.resource), expected = one(phaseRows(renamed, resource), command.assignedId), created = one(phaseRows(index === 6 ? phase("vrf-create") : preVrf, resource), command.assignedId) + proof(created.comment === marker && expected.comment === marker) + const fields = obj(command.fields) + proof(fields.comment === marker && Object.entries(fields).every(([field, value]) => field !== ".id" && isDeepStrictEqual(created[field], value))) + if (resource === "vrf") same(created, expected, ["name"]); else same(created, expected, resource.endsWith("address") ? ["vrf"] : []) + const item = { resource, id: command.assignedId, marker, expected: structuredClone(expected) } + proof(!ids.has(key(item))); ids.add(key(item)); owned.push(item) + } + const vrf = owned[6], bridges = owned.filter(item => item.resource === "bridge"), rename = journal[7], disable = journal[8] + proof(rename.phase === "vrf-rename" && rename.resource === "vrf" && rename.method === "PATCH" && rename.id === vrf.id && rename.outcome === "acknowledged" && rename.ownedIdentityConfirmed === true && rename.readbackVerified === true && rename.requestedChange === true && isDeepStrictEqual(rename.fields, { name: vrf.expected.name })) + proof(disable.phase === "vrf-disable" && disable.resource === "vrf" && disable.method === "PATCH" && disable.id === vrf.id && disable.outcome === "acknowledged" && disable.requestedChange === true && disable.readbackVerified === undefined && disable.ownedIdentityConfirmed === undefined && isDeepStrictEqual(disable.fields, { disabled: "true" })) + const remainingRecorded = rows(original.knownRemaining) + proof(remainingRecorded.length === 7 && new Set(remainingRecorded.map(item => `${String(item.resource)}/${String(item.id)}`)).size === 7 && remainingRecorded.every(item => ids.has(`${String(item.resource)}/${String(item.id)}`))) + assertVrfMembersOwned(vrf.expected, bridges) + const sourceCatalog = phaseCatalog(renamed), diagnosticCatalog = rawCatalog(obj(diagnostic.candidates)) + validateCatalog(sourceCatalog); validateCatalog(diagnosticCatalog); validateCatalog(input.currentCatalog) + const kernel = rows(renamed.attributions).map(item => obj(item.evidence)).filter(item => item.resource !== undefined) as unknown as NativeKernelEvidence[] + assertNativeKernelEvidence(sourceCatalog, owned, marker, { allowedAddressVrfs: ["main", vrf.id, String(vrf.expected.name)] }, kernel) + // Bind all source-owned rows to final post-ACK primary proof, then to the + // read-only diagnostic. Only disabled and member-bound address.vrf change. + for (const item of owned) { + const current = one(diagnosticCatalog[item.resource], item.id) + same(one(phaseRows(final, item.resource), item.id), current) + if (item.resource === "vrf") { proof(current.disabled === "true"); same(item.expected, current, ["disabled"]) } + else if (item.resource.endsWith("address")) { proof(current.vrf === "main"); same(item.expected, current, ["vrf"]) } + else same(item.expected, current) + item.expected = structuredClone(current) + } + const oldVirtual = kernel.filter(item => item.kind === "virtual" && item.parentId === vrf.id), oldTables = kernel.filter(item => item.kind === "table" && item.parentId === vrf.id) + proof(oldVirtual.length === 1 && oldTables.length === 1 && oldTables[0].id === vrf.id && !diagnosticCatalog.routing_table.some(row => row[".id"] === vrf.id)) + const virtual = one(diagnosticCatalog.interface_inventory, oldVirtual[0].id) + disabledVirtual(oldVirtual[0].expected, virtual) + for (const bridge of bridges) same(one(phaseRows(preVrf, "interface_inventory"), bridge.id), one(diagnosticCatalog.interface_inventory, bridge.id)) + const sourceClosure = assertOwnedAddressClosure(sourceCatalog, sourceCatalog.ipv4_address.concat(sourceCatalog.ipv6_address).filter(row => row.comment === marker).map(row => ({ resource: String(row.address).includes(":") ? "ipv6_address" : "ipv4_address", id: String(row[".id"]), marker, expected: row })), bridges, marker, { allowedAddressVrfs: ["main", String(vrf.expected.name)] }) + const diagnosticClosure = assertOwnedAddressClosure(diagnosticCatalog, owned, bridges, marker, { allowedAddressVrfs: ["main"] }) + proof(sourceClosure.linkLocalCompanions.length === 2 && diagnosticClosure.linkLocalCompanions.length === 2) + for (const old of sourceClosure.linkLocalCompanions) { + const current = diagnosticClosure.linkLocalCompanions.filter(item => item.interface === old.interface) + proof(current.length === 1); linkLocalSuccessor(old.row, current[0].row, bridges.find(item => item.expected.name === old.interface)!) + if (current[0].id !== old.id) proof(!diagnosticCatalog.ipv6_address.some(row => row[".id"] === old.id)) + } + const diagRouteKeys = new Set() + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of diagnosticCatalog[resource]) { + const route = deriveOwnedConnectedRoute(row, { addresses: diagnosticClosure, bridges, vrfs: [], allowedTables: ["main"] }) + if (!route) continue + proof(row["immediate-gw"] === route.interface && parseChrVrfPrefix(row["dst-address"], resource === "ipv4_route" ? 4 : 6)) + const semantic = `${resource}/${route.interface}/${parseChrVrfPrefix(route.prefix, resource === "ipv4_route" ? 4 : 6)?.hostKey}` + proof(!diagRouteKeys.has(semantic)); diagRouteKeys.add(semantic) + const old = kernel.filter(item => item.kind === "route" && item.resource === resource && item.expected["dst-address"] === row["dst-address"] && item.expected["immediate-gw"] === route.interface) + proof(old.length === 1); same(old[0].expected, row, [".id", "gateway", "routing-table", "local-address"]) + if (old[0].id !== row[".id"]) proof(!diagnosticCatalog[resource].some(item => item[".id"] === old[0].id)) + } + proof(diagRouteKeys.size === kernel.filter(item => item.kind === "route").length && diagRouteKeys.size === 6) + const deletions = [vrf, ...owned.filter(item => item.resource === "ipv6_address"), ...owned.filter(item => item.resource === "ipv4_address"), ...bridges] + proof(Array.isArray(input.cleaned) && input.cleaned.length <= 7 && input.cleaned.every((item, index) => item && key(item) === key(deletions[index]))) + const cleaned = input.cleaned.map(item => ({ resource: item.resource, id: item.id })), removed = new Set(cleaned.map(key)), remaining = deletions.filter(item => !removed.has(key(item))), current = input.currentCatalog + for (const item of deletions) if (removed.has(key(item))) proof(!current[item.resource].some(row => row[".id"] === item.id)) + assertAcceptedOwnership(current, remaining) + const liveBridges = remaining.filter(item => item.resource === "bridge"), vrfPresent = !removed.has(key(vrf)) + const closure = assertOwnedAddressClosure(current, remaining, liveBridges, marker, { allowedAddressVrfs: ["main"] }) + const names = bridges.map(item => String(item.expected.name)) + proof([...current.bridge_port, ...current.bridge_vlan, ...current.interface_list_members].every(row => !names.includes(String(row.bridge)) && !names.includes(String(row.interface)) && ![row.tagged, row.untagged].some(value => typeof value === "string" && value.split(",").some(name => names.includes(name))))) + proof(current.vrf.every(row => row[".id"] === vrf.id && vrfPresent || typeof row.interfaces === "string" && !row.interfaces.split(",").some(name => names.includes(name)) && (row.name === "main" || row.interfaces !== "all"))) + proof(!current.routing_table.some(row => row[".id"] === vrf.id)) + const exclusions: CleanupIdentity[] = remaining.map(({ resource, id }) => ({ resource, id })) + for (const bridge of bridges) { + if (removed.has(key(bridge))) { proof(!current.interface_inventory.some(row => row[".id"] === bridge.id)); continue } + const expected = { ...one(diagnosticCatalog.interface_inventory, bridge.id) } + if (!remaining.some(item => item.resource === "ipv4_address" && item.expected.interface === bridge.expected.name)) { + proof(expected.status === "--LERUI-O---------------"); expected.status = "--LERU--O---------------" + } + same(expected, one(current.interface_inventory, bridge.id)); exclusions.push({ resource: "interface_inventory", id: bridge.id }) + } + if (vrfPresent) { same(virtual, one(current.interface_inventory, oldVirtual[0].id)); exclusions.push({ resource: "interface_inventory", id: oldVirtual[0].id }) } + else proof(!current.interface_inventory.some(row => row[".id"] === oldVirtual[0].id)) + for (const old of diagnosticClosure.linkLocalCompanions) { + const bridge = bridges.find(item => item.expected.name === old.interface)! + if (removed.has(key(bridge))) { proof(!current.ipv6_address.some(row => row[".id"] === old.id)); continue } + const now = closure.linkLocalCompanions.filter(item => item.interface === old.interface) + proof(now.length === 1) + if (vrfPresent) { proof(now[0].id === old.id); same(old.row, now[0].row) } + else { linkLocalSuccessor(old.row, now[0].row, bridge); if (now[0].id !== old.id) proof(!current.ipv6_address.some(row => row[".id"] === old.id)) } + exclusions.push({ resource: "ipv6_address", id: now[0].id }) + } + proof(closure.linkLocalCompanions.length === liveBridges.length) + for (const resource of ["ipv4_route", "ipv6_route"]) for (const expected of diagnosticCatalog[resource]) { + const route = deriveOwnedConnectedRoute(expected, { addresses: diagnosticClosure, bridges, vrfs: [], allowedTables: ["main"] }) + if (!route) continue + const needed = route.addressKind === "kernel-link-local" ? liveBridges.some(bridge => bridge.expected.name === route.interface) : remaining.some(item => item.resource === (resource === "ipv4_route" ? "ipv4_address" : "ipv6_address") && item.id === route.addressId) + if (needed) { + same(expected, one(current[resource], route.id)) + exclusions.push({ resource, id: route.id }) + } else proof(!current[resource].some(row => row[".id"] === route.id)) + } + const hashes = vrfCleanupForeignHashes(current, exclusions) + proof(isDeepStrictEqual(hashes, baselineHashes)) + return { schema: "chr-vrf-companion-disabled-cleanup-proof-v1", marker, sourceReportSha256: input.sourceReportSha256, deletions: structuredClone(deletions), remaining: structuredClone(remaining), next: structuredClone(remaining[0] ?? null), cleaned, foreignHashes: hashes, kernelExclusions: exclusions.filter(item => !ids.has(key(item))) } +} +export function assertDisabledVrfCleanupCheckpoint(input: DisabledVrfCleanupInput): DisabledVrfCleanupEvidence { return prepareDisabledVrfCleanupEvidence(input) } diff --git a/backend/src/test/chr-vrf-companion-native.ts b/backend/src/test/chr-vrf-companion-native.ts new file mode 100644 index 00000000..060228e0 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-native.ts @@ -0,0 +1,277 @@ +import { createHash, randomUUID } from "node:crypto" +import { writeFile } from "node:fs/promises" +import { isAbsolute, resolve } from "node:path" +import { pathToFileURL } from "node:url" +import { setTimeout as delay } from "node:timers/promises" +import { MikrotikClient, MikrotikError } from "../services/mikrotik.js" +import { configurationRows, resourceById } from "../modules/mcp/resources.js" +import { closePool } from "../db/index.js" +import { stripNativeTelemetry } from "./chr-vrf-companion-telemetry.js" +import { assertChrVrfFixtureAddressSpaceUnused, ChrAddressSpaceAdmissionError } from "./chr-vrf-companion-address-space.js" +import { acceptNativeCreate, acceptNativePatch, advanceNativeKernelEvidence, assessNativeConnectedCapabilities, assertAcceptedOwnership, assertNativeDelete, assertNativeKernelEvidence, assertVrfMembersOwned, summarizeNativeVrfLifecycle, type NativeKernelEvidence, type NativeOwnedObject } from "./chr-vrf-companion-proof.js" + +type Row = Record +type Catalog = Record +type Owned = NativeOwnedObject & { present: boolean } +const specs = [ + ["bridge", "/interface/bridge"], ["bridge_port", "/interface/bridge/port"], ["bridge_vlan", "/interface/bridge/vlan"], + ["vrf", "/ip/vrf"], ["ipv4_address", "/ip/address"], ["ipv6_address", "/ipv6/address"], + ["ipv4_route", "/ip/route"], ["ipv6_route", "/ipv6/route"], ["routing_table", "/routing/table"], + ["ethernet", "/interface/ethernet"], ["ip_filter", "/ip/firewall/filter"], ["ipv6_filter", "/ipv6/firewall/filter"], + ["routing_rule", "/routing/rule"], ["ip_settings", "/ip/settings"], ["ipv6_settings", "/ipv6/settings"], + ["interface_inventory", "/interface"], ["interface_list_members", "/interface/list/member"], +] as const +const pathFor = (resource: string) => { + const path = specs.find(([id]) => id === resource)?.[1] + if (!path) throw Error("Unknown fixture resource") + return path +} +const canonical = (value: unknown): unknown => Array.isArray(value) ? value.map(canonical) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, child]) => [key, canonical(child)])) : value +const hash = (value: unknown) => createHash("sha256").update(JSON.stringify(canonical(value))).digest("hex") +const nativeId = (value: unknown): value is string => typeof value === "string" && /^\*[A-Fa-f0-9]+$/.test(value) +const truth = (value: unknown) => [true, "true", "yes"].includes(value as string | boolean) +function project(resource: string, rows: Row[]): Row[] { + if (resource === "interface_inventory") return rows.map(row => Object.fromEntries([".id", "name", "type", "default-name", "disabled"].filter(key => Object.hasOwn(row, key)).map(key => [key, row[key]]))) + if (resource === "interface_list_members") return rows.map(row => Object.fromEntries([".id", "list", "interface", "dynamic", "disabled", "comment"].filter(key => Object.hasOwn(row, key)).map(key => [key, row[key]]))) + return configurationRows(resourceById(resource), rows) +} +// Preserve unknown configuration fields too. Only explicit live telemetry is +// excluded; IDs, membership, comments and all other native fields remain. +function nativeConfiguration(rows: Row[]): Row[] { + return rows.map(stripNativeTelemetry) +} + +/** Direct user approval: temporary isolated fixture on the one fixed lab CHR. + * No DB server ID, DB query, physical port, uplink, default path, or MCP call. + * Import is inert; root must explicitly invoke this runner after code review. */ +export async function runChrVrfCompanion(): Promise { + const hosts = (process.env.MMAPP_TEST_CHR_HOSTS ?? "").split(",").map(value => value.trim()).filter(Boolean) + const ports = process.env.MMAPP_TEST_CHR_TUNNEL_PORTS?.split(",").map(Number) + const username = process.env.MMAPP_TEST_CHR_USER, password = process.env.MMAPP_TEST_CHR_PASSWORD + const reportPath = process.env.MMAPP_TEST_CHR_VRF_COMPANION_REPORT + if (process.env.MMAPP_TEST_CHR_VRF_COMPANION !== "true" || !reportPath || !isAbsolute(reportPath)) throw Error("Explicit VRF fixture flag and absolute report path required") + if (hosts.length !== 1 || hosts[0] !== "104.165.244.7") throw Error("Only the explicitly approved lab CHR is allowed") + if (!username || !password) throw Error("Explicit lab credentials required") + if (ports && (ports.length !== 1 || !Number.isInteger(ports[0]) || ports[0] < 1024 || ports[0] > 65535)) throw Error("Invalid explicit lab tunnel") + const marker = `mcp-test-vrf-companion-${randomUUID()}` + const bridges = [`${marker}-a`, `${marker}-b`], vrfs = [`${marker}-v`, `${marker}-r`] + const suffix = randomUUID().replaceAll("-", ""), third = 1 + Number.parseInt(suffix.slice(0, 2), 16) % 253 + const hextet = (1 + Number.parseInt(suffix.slice(2, 6), 16) % 65534).toString(16) + const secondHextet = (1 + Number.parseInt(suffix.slice(6, 10), 16) % 65534).toString(16) + const prefixes = [`198.18.${third}.1/32`, `198.18.${third}.2/32`, `fd6d:6370:${hextet}:${secondHextet}::1/128`, `fd6d:6370:${hextet}:${secondHextet}::2/128`] + const phases: Record[] = [], journal: Record[] = [] + const report: Record = { schema: "chr-vrf-companion-native-v5", proofVersion: 5, admissionContracts: { disable: "observed-routeros-7.24.5-v3", reenable: "observed-routeros-7.24.5-v4-bounded-on-shape-v1", transfer: "observed-routeros-7.24.5-v4-missing-ll" }, proofStatus: "not-run", marker, startedAt: new Date().toISOString(), complete: false, cleanupComplete: false, nativeOnly: true, mcpAdmissionTested: false, forwardingTested: false, foreignProofScope: "listed native catalogs excluding explicit telemetry plus configurationRows; exact ACK IDs and strictly typed pinned kernel companions only; static address vrf exact current active member; dormant table identity and on/off virtual provenance retained", phases, journal } + // Scalar masking avoids treating the harmless key admin-mac-address as a + // username reflection. Raw provider errors and foreign rows are never saved. + const mask = (value: unknown): unknown => { + if (Array.isArray(value)) return value.map(mask) + if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, /password|secret|token|private-key|username/i.test(key) ? "[redacted]" : mask(child)])) + if (typeof value !== "string") return value + if (value === username || value.includes(password) || hosts.some(host => value.includes(host))) return "[redacted-reflection]" + return value + } + const output = resolve(reportPath) + await writeFile(output, JSON.stringify(mask(report), null, 2) + "\n", { flag: "wx", mode: 0o600 }) + const save = () => writeFile(output, JSON.stringify(mask(report), null, 2) + "\n", { mode: 0o600 }) + const client = new MikrotikClient({ host: ports ? "127.0.0.1" : hosts[0], port: ports?.[0] ?? 80, useSsl: false, verifySsl: true, username, password }) + const owned = new Map() + let kernelEvidence: NativeKernelEvidence[] = [] + const addressVrfReferences = new Set(["main"]) + let unknownWrite = false, stopped = false, baselineHashes: Record | undefined + const read = async (): Promise => { + const value: Catalog = {} + for (const [resource, path] of specs) { + await delay(100) + const payload = await client.get(path) + const rows = Array.isArray(payload) ? payload : payload && typeof payload === "object" ? [payload] : undefined + if (!rows || rows.length > 50000 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row))) throw Error("Invalid native catalog") + const ids = rows.map(row => (row as Row)[".id"]).filter(id => id !== undefined) + if (ids.some(id => !nativeId(id)) || new Set(ids).size !== ids.length) throw Error("Invalid native catalog IDs") + if (!["ip_settings", "ipv6_settings"].includes(resource) && rows.some(row => !nativeId((row as Row)[".id"]))) throw Error("Missing native catalog ID") + value[resource] = rows as Row[] + } + return value + } + const present = () => [...owned.values()].filter(entry => entry.present) + const kernelPolicy = () => ({ allowedAddressVrfs: [...addressVrfReferences] }) + const prove = (catalog: Catalog) => { + const accepted = present(), acceptedBridges = accepted.filter(entry => entry.resource === "bridge"), liveVrfs = accepted.filter(entry => entry.resource === "vrf") + assertAcceptedOwnership(catalog, accepted, kernelPolicy()) + assertNativeKernelEvidence(catalog, accepted, marker, kernelPolicy(), kernelEvidence) + for (const entry of liveVrfs) assertVrfMembersOwned(catalog.vrf.find(row => row[".id"] === entry.id)!, acceptedBridges) + if ([...catalog.bridge_port, ...catalog.bridge_vlan, ...catalog.interface_list_members].some(row => bridges.includes(String(row.bridge)) || bridges.includes(String(row.interface)))) throw Error("Owned bridge acquired external dependencies") + if (catalog.vrf.some(row => !liveVrfs.some(entry => entry.id === row[".id"]) && (String(row.interfaces ?? "").split(",").some(name => bridges.includes(name)) || row.name !== "main" && row.interfaces === "all"))) throw Error("Foreign VRF acquired fixture member") + const refs = new Map() + const add = (resource: string, id: string, evidence: unknown) => refs.set(`${resource}/${id}`, evidence) + for (const entry of accepted) add(entry.resource, entry.id, { kind: "acknowledged-owned", id: entry.id }) + for (const companion of kernelEvidence) if (companion.present !== false) add(companion.resource, companion.id, companion) + return refs + } + const foreign = (catalog: Catalog, refs = prove(catalog)) => Object.fromEntries(specs.flatMap(([resource]) => { + const rows = catalog[resource].filter(row => !refs.has(`${resource}/${String(row[".id"])}`)) + return [[resource, hash(project(resource, rows))], [`${resource}:native-configuration`, hash(nativeConfiguration(rows))]] + })) + const assertForeign = (catalog: Catalog) => { if (!baselineHashes || hash(foreign(catalog)) !== hash(baselineHashes)) throw Error("Foreign configuration changed or native companion unproven") } + const assertOwned = (catalog: Catalog) => { prove(catalog) } + const knownRow = (catalog: Catalog, entry: Owned) => { assertAcceptedOwnership(catalog, [entry], kernelPolicy()); return catalog[entry.resource].find(row => row[".id"] === entry.id)! } + const capture = async (phase: string, catalog?: Catalog, persist = true) => { + const value = catalog ?? await read() + let refs = new Map(), proofStatus = "passed" + try { refs = prove(value) } catch { proofStatus = "refused-unproven-native-state" } + const actual = foreign(value, refs) + const foreignStable = proofStatus === "passed" && (baselineHashes ? hash(actual) === hash(baselineHashes) : true) + if (!foreignStable && proofStatus === "passed") proofStatus = "refused-foreign-or-unproven-native-change" + // On refused proof, expose only exact previously ACK-owned IDs as diagnostic + // rows. No planned-name candidate becomes owned or disappears from hashes. + const diagnostic = (resource: string, row: Row) => refs.has(`${resource}/${String(row[".id"])}`) || present().some(entry => entry.resource === resource && entry.id === row[".id"] && row.comment === marker) + phases.push({ phase, proofVersion: 5, proofStatus, foreignStable, foreignHashes: actual, attributions: [...refs.entries()].map(([object, evidence]) => ({ object, evidence })), owned: Object.fromEntries(specs.map(([resource]) => { + const raw = value[resource].filter(row => diagnostic(resource, row)) + return [resource, { raw, projected: project(resource, raw), nativeIndices: value[resource].flatMap((row, position) => diagnostic(resource, row) ? [position] : []) }] + })) }) + if (persist) await save() + return value + } + const mutate = async (phase: string, resource: string, method: "PUT" | "PATCH" | "DELETE", fields: Record, entry?: Owned): Promise => { + if (unknownWrite || stopped) throw Error("Native writes latched off") + const before = await read(); assertForeign(before); assertOwned(before) + const beforeAccepted = structuredClone(present()) + if (entry) knownRow(before, entry) + const item: Record = { phase, resource, method, ...(entry ? { id: entry.id } : {}), fields, outcome: "intent", httpStatus: null } + item.requestedChange = method !== "PATCH" || Object.entries(fields).some(([field, value]) => { + const old = entry?.expected[field] + return field === "disabled" ? truth(old) !== truth(value) : old !== value + }) + journal.push(item); await save() // Durable local intent BEFORE dispatch. + let result: unknown + try { + const path = pathFor(resource) + (entry ? `/${encodeURIComponent(entry.id)}` : "") + const beforeDispatch = async () => { + // The local intent checkpoint is asynchronous. Recheck the complete + // closure again AFTER it and immediately before the native command. + const fresh = await read(); assertForeign(fresh); assertOwned(fresh) + if (entry) knownRow(fresh, entry) + } + result = method === "PUT" ? await client.put(path, fields, 15000, beforeDispatch) : method === "PATCH" ? await client.patch(path, fields, 15000, beforeDispatch) : await client.delete(path, 10000, beforeDispatch) + item.outcome = "acknowledged" // Client hides the exact successful 2xx status. + } catch (error) { + item.httpStatus = error instanceof MikrotikError ? error.statusCode : null + item.outcome = error instanceof MikrotikError ? "http-error" : "unknown" + item.unsupportedCandidate = error instanceof MikrotikError && [400, 404, 405, 422, 501].includes(error.statusCode) + // No assumption that HTTP errors are atomic. A 4xx and complete exact + // unchanged readback are required to continue independent diagnostics. + const after = await read().catch(() => undefined) + const fullConfiguration = (catalog: Catalog) => specs.map(([id]) => ({ resource: id, projected: project(id, catalog[id]), native: nativeConfiguration(catalog[id]) })) + let unchanged = Boolean(after && hash(fullConfiguration(after)) === hash(fullConfiguration(before))) + if (after && unchanged) { try { assertOwned(after); assertForeign(after) } catch { unchanged = false } } + if (!(error instanceof MikrotikError && error.statusCode >= 400 && error.statusCode < 500 && unchanged)) unknownWrite = true + item.capabilityOutcome = unchanged && error instanceof MikrotikError && error.statusCode >= 400 && error.statusCode < 500 ? item.unsupportedCandidate ? "unsupported-command-unchanged" : "rejected-command-unchanged" : "unknown-outcome" + item.unchangedReadback = unchanged; item.unchangedReadbackScope = "full-native-configuration-and-projection"; report.unknownWrite = unknownWrite + if (after) await capture(`${phase}-error-readback`, after, false) + await save() + if (unknownWrite) throw Error("Unconfirmed native write; no retry or cleanup dispatch") + return false + } + // Any failure between ACK and complete readback/ownership registration + // conservatively latches dispatch off. No attempted command is replayed. + try { + const after = await read() + if (method === "PUT") { + const accepted = acceptNativeCreate(resource, marker, fields, result, before[resource], after[resource]) + owned.set(`${resource}/${accepted.id}`, { ...accepted, present: true }); item.assignedId = accepted.id + if (resource === "vrf") { addressVrfReferences.add(accepted.id); addressVrfReferences.add(String(accepted.expected.name)) } + } else if (entry && method === "PATCH") { + const accepted = acceptNativePatch(entry, fields, before[resource], after[resource], present().filter(value => value.resource === "bridge"), kernelPolicy()) + entry.expected = accepted.expected + if (resource === "vrf") { addressVrfReferences.add(accepted.id); addressVrfReferences.add(String(accepted.expected.name)) } + } else if (entry) { + assertNativeDelete(entry, before[resource], after[resource], kernelPolicy()) + entry.present = false + } + const primaryId = String(item.assignedId ?? entry?.id) + kernelEvidence = advanceNativeKernelEvidence({ before, after, beforeAccepted, afterAccepted: present(), marker, policy: kernelPolicy(), previous: kernelEvidence, transition: { resource, method, id: primaryId, fields } }) + // Ownership and full kernel configuration are pinned BEFORE reporting. + item.ownedIdentityConfirmed = true + await capture(phase, after, false) + assertForeign(after); assertOwned(after) + item.readbackVerified = true + item.capabilityOutcome = "ack-and-complete-proof-passed" + } catch { + item.capabilityOutcome = "unproven-native-shape" + unknownWrite = true; report.unknownWrite = true + throw Error("Post-dispatch proof incomplete; no replay") + } + await save() + return true + } + try { + const baseline = await read(); baselineHashes = foreign(baseline) + if (Object.values(baseline).flat().some(row => row.comment === marker || [...bridges, ...vrfs].includes(String(row.name)) || prefixes.includes(String(row.address)) || prefixes.includes(String(row["dst-address"])))) throw Error("Fixture collision") + try { report.addressSpaceAdmission = assertChrVrfFixtureAddressSpaceUnused({ ipv4_address: baseline.ipv4_address, ipv6_address: baseline.ipv6_address, ipv4_route: baseline.ipv4_route, ipv6_route: baseline.ipv6_route }, prefixes) } + catch (error) { + report.addressSpaceAdmission = { status: "refused-zero-mutations", reason: error instanceof ChrAddressSpaceAdmissionError ? error.reason : "unproven-address-space" } + await save() + throw Error("Fixture address space isolation not proven; no mutation") + } + if (baseline.ipv6_settings.some(row => truth(row["disable-ipv6"]))) throw Error("IPv6 disabled; no global settings change allowed") + await capture("baseline", baseline) + const capabilities = assessNativeConnectedCapabilities(baseline) + report.connectedCapabilityAdmission = capabilities + await save() + if (capabilities.status !== "proven-baseline-schema") throw Error("Connected native schema unproven; zero mutations") + for (const name of bridges) if (!await mutate("bridge-create", "bridge", "PUT", { name, comment: marker, "protocol-mode": "none", disabled: "false" })) throw Error("Bridge fixture refused") + for (const [member, name] of bridges.entries()) { + if (!await mutate("ipv4-address-create", "ipv4_address", "PUT", { address: prefixes[member], interface: name, comment: marker, disabled: "false" })) throw Error("IPv4 fixture refused") + if (!await mutate("ipv6-address-create", "ipv6_address", "PUT", { address: prefixes[member + 2], interface: name, comment: marker, advertise: "false", disabled: "false" })) throw Error("IPv6 fixture refused") + } + await capture("addresses-before-vrf") + // Do not make the CREATE observation depend on native support for the + // separately tested optional disabled property. + if (!await mutate("vrf-create", "vrf", "PUT", { name: vrfs[0], interfaces: bridges[0], comment: marker })) throw Error("VRF fixture refused") + const vrf = [...owned.values()].find(entry => entry.resource === "vrf")! + await mutate("vrf-rename", "vrf", "PATCH", { name: vrfs[1] }, vrf) + await mutate("vrf-disable", "vrf", "PATCH", { disabled: "true" }, vrf) + await mutate("vrf-reenable", "vrf", "PATCH", { disabled: "false" }, vrf) + await mutate("vrf-membership-transfer-a-to-b", "vrf", "PATCH", { interfaces: bridges[1] }, vrf) + // Addresses deliberately remain on BOTH bridges during native VRF delete. + if (!await mutate("vrf-delete-addresses-remain", "vrf", "DELETE", {}, vrf)) throw Error("VRF delete refused") + const final = await capture("vrf-deleted-addresses-remain") + if (final.ipv4_address.filter(row => row.comment === marker).length !== 2 || final.ipv6_address.filter(row => row.comment === marker).length !== 2) throw Error("Owned addresses changed during VRF lifecycle") + const lifecycle = summarizeNativeVrfLifecycle(journal) + Object.assign(report, lifecycle) + report.nativeLifecycleFullyAcknowledged = lifecycle.fullLifecycleSupported + report.nativeLifecycleAccepted = lifecycle.fullLifecycleSupported + // Unsupported unchanged responses may continue independent diagnostics, + // but never count as accepted lifecycle evidence. + report.complete = lifecycle.fullLifecycleSupported + } catch { + report.stopReason = unknownWrite ? "unconfirmed-native-write-no-replay" : "fixture-precondition-or-observation-failed" + } finally { + try { + const current = await capture("finally-readonly-classification") + assertForeign(current); assertOwned(current) + if (!unknownWrite) { + // Only ACKed/readback-confirmed IDs from THIS invocation. No discovery + // by broad prefix, no DELETE retry after uncertain outcome. + for (const resource of ["vrf", "ipv6_address", "ipv4_address", "bridge"]) for (const entry of [...owned.values()].filter(value => value.resource === resource && value.present)) { + if (!await mutate("cleanup-owned", resource, "DELETE", {}, entry)) throw Error("Cleanup refused") + } + const cleaned = await capture("cleanup-final"); assertForeign(cleaned) + report.cleanupComplete = present().length === 0 && prove(cleaned).size === 0 + } else report.cleanupSkipped = "unknown-write-latch-readonly-only" + } catch { report.cleanupStopped = true } + stopped = true + const lifecycle = summarizeNativeVrfLifecycle(journal) + Object.assign(report, lifecycle) + report.nativeLifecycleFullyAcknowledged = lifecycle.fullLifecycleSupported + report.nativeLifecycleAccepted = lifecycle.fullLifecycleSupported + report.knownRemaining = [...owned.values()].filter(value => value.present).map(({ resource, id }) => ({ resource, id })) + report.proofStatus = report.complete && report.cleanupComplete ? "observations-complete-guards-passed" : "refused-or-incomplete" + report.finishedAt = new Date().toISOString() + try { await save() } finally { await closePool() } + } + if (!report.complete || !report.cleanupComplete) throw Error("VRF companion evidence incomplete; inspect local report without replay") +} + +if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { + runChrVrfCompanion().catch(() => { console.error("VRF companion fixture incomplete; inspect designated local report. No write replay performed."); process.exitCode = 1 }) +} diff --git a/backend/src/test/chr-vrf-companion-proof.test.ts b/backend/src/test/chr-vrf-companion-proof.test.ts new file mode 100644 index 00000000..ad431c9c --- /dev/null +++ b/backend/src/test/chr-vrf-companion-proof.test.ts @@ -0,0 +1,520 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { vrfCleanupForeignHashes } from "./chr-vrf-companion-cleanup-proof.js" +import { acceptNativeCreate, acceptNativePatch, advanceNativeKernelEvidence, assertNativeKernelEvidence, summarizeNativeVrfLifecycle, VRF_NATIVE_LIFECYCLE_PHASES, assessNativeConnectedCapabilities, assertAcceptedOwnership, assertNativeDelete, assertOwnedAddressClosure, assertVrfMembersOwned, deriveOwnedConnectedRoute, deriveOwnedBridgeInventory, deriveVrfTableCompanion, type NativeCatalog, type NativeOwnedObject, type NativeRow } from "./chr-vrf-companion-proof.js" + +const marker = "mcp-test-vrf-companion-proof" +const bridgeRow = (id: string, name: string): NativeRow => ({ ".id": id, name, comment: marker, disabled: "false", "protocol-mode": "none", opaque: "preserve" }) +const bridge = (id: string, name: string): NativeOwnedObject => ({ resource: "bridge", id, marker, expected: bridgeRow(id, name) }) +const bridges = [bridge("*A", `${marker}-a`), bridge("*B", `${marker}-b`)] +const names = bridges.map(value => String(value.expected.name)) +const address = (resource: string, id: string, ip: string, member = 0): NativeOwnedObject => ({ resource, id, marker, expected: { ".id": id, address: ip, interface: names[member], comment: marker, dynamic: "false", disabled: "false", vrf: "main", opaque: "preserve" } }) +const addresses = [address("ipv4_address", "*11", "198.18.7.1/32"), address("ipv6_address", "*12", "fd6d:6370:1:2::1/128")] +const catalog = (): NativeCatalog => ({ bridge: bridges.map(value => structuredClone(value.expected)), ipv4_address: [structuredClone(addresses[0].expected)], ipv6_address: [structuredClone(addresses[1].expected)] }) +const refused = (fn: () => unknown) => assert.throws(fn, /VRF fixture proof refused/) +let checks = 0 + +// ACK identity, one-added-row and existing-row conservation are independent. +const previous = bridgeRow("*C", "foreign"), created = bridges[0].expected +assert.deepEqual(acceptNativeCreate("bridge", marker, { name: names[0], comment: marker }, { ".id": "*A" }, [previous], [previous, created]), bridges[0]) +for (const [ack, after] of [ + [{ ".id": "*C" }, [previous, created]], + [{ ".id": "*A" }, [previous, created, bridgeRow("*D", "extra")]], + [{ ".id": "*A" }, [{ ...previous, opaque: "changed" }, created]], + [{ ".id": "*A" }, [created]], + [{}, [previous, created]], + [{ ".id": "*A" }, [previous, { ...created, comment: "foreign" }]], +] as const) { refused(() => acceptNativeCreate("bridge", marker, { name: names[0], comment: marker }, ack, [previous], after)); checks++ } + +assertAcceptedOwnership(catalog(), [...bridges, ...addresses]) +const policy = { allowedAddressVrfs: ["main", `${marker}-v`, `${marker}-r`, "*30"] } +const moved = catalog(); moved.ipv4_address[0].vrf = `${marker}-v` +assertAcceptedOwnership(moved, addresses, policy) +for (const field of ["ether1", "foreign-vrf", "", undefined]) { + const changed = catalog(); changed.ipv4_address[0].vrf = field + refused(() => assertAcceptedOwnership(changed, addresses, policy)); checks++ +} +const opaque = catalog(); opaque.ipv4_address[0].opaque = "changed" +refused(() => assertAcceptedOwnership(opaque, addresses, policy)) + +const closure = assertOwnedAddressClosure(catalog(), addresses, bridges, marker, policy) +assert.equal(closure.staticAddresses.length, 2); assert.equal(closure.linkLocalCompanions.length, 0) +for (const change of ["extra-v4", "extra-v6", "extra-dynamic-global", "missing", "swapped-id", "duplicate-id", "changed-address", "foreign-marker-address", "extra-linklocal-static"] as const) { + const value = catalog() + if (change === "extra-v4") value.ipv4_address.push({ ...value.ipv4_address[0], ".id": "*99", comment: "external", address: "198.18.8.1/32" }) + if (change === "extra-v6") value.ipv6_address.push({ ...value.ipv6_address[0], ".id": "*99", comment: "external", address: "fd6d:6370:9::1/128" }) + if (change === "extra-dynamic-global") value.ipv6_address.push({ ".id": "*99", interface: names[0], dynamic: "true", address: "fd6d:6370:9::1/64" }) + if (change === "missing") value.ipv4_address = [] + if (change === "swapped-id") value.ipv4_address[0][".id"] = "*99" + if (change === "duplicate-id") value.ipv4_address.push(structuredClone(value.ipv4_address[0])) + if (change === "changed-address") value.ipv4_address[0].address = "198.18.8.1/32" + if (change === "foreign-marker-address") value.ipv6_address.push({ ".id": "*99", interface: "ether1", comment: marker, address: "fd6d:6370:9::1/64", dynamic: "true" }) + if (change === "extra-linklocal-static") value.ipv6_address.push({ ".id": "*99", interface: names[0], dynamic: "false", address: "fe80::1/64" }) + refused(() => assertOwnedAddressClosure(value, addresses, bridges, marker, policy)); checks++ +} + +// Auto link-local is typed evidence, not an ACK-owned/deletable address. +const withLinkLocal = catalog() +withLinkLocal.ipv6_address.push({ ".id": "*21", interface: names[0], dynamic: "true", "link-local": "true", address: "fe80::1234/64", vrf: "main" }) +withLinkLocal.ipv6_address.push({ ".id": "*22", interface: names[1], dynamic: true, address: "fe80::5678/64" }) +const linked = assertOwnedAddressClosure(withLinkLocal, addresses, bridges, marker, policy) +assert.equal(linked.staticAddresses.length, 2); assert.equal(linked.linkLocalCompanions.length, 2) +assert.ok(linked.linkLocalCompanions.every(value => value.kind === "kernel-link-local-address" && !("resource" in value))) +for (const alter of ["duplicate-context", "bad-flag", "bad-length", "foreign-vrf"] as const) { + const value = structuredClone(withLinkLocal) + if (alter === "duplicate-context") value.ipv6_address.push({ ...value.ipv6_address[1], ".id": "*23" }) + if (alter === "bad-flag") value.ipv6_address[1]["link-local"] = "false" + if (alter === "bad-length") value.ipv6_address[1].address = "fe80::1234/128" + if (alter === "foreign-vrf") value.ipv6_address[1].vrf = "foreign" + refused(() => assertOwnedAddressClosure(value, addresses, bridges, marker, policy)); checks++ +} + +const route: NativeRow = { ".id": "*41", dynamic: "true", connect: "true", "dst-address": "198.18.7.1/32", gateway: names[0], "routing-table": "main" } +const context = { addresses: linked, bridges, allowedTables: policy.allowedAddressVrfs } +assert.equal(deriveOwnedConnectedRoute(route, context)?.addressId, "*11") +assert.equal(deriveOwnedConnectedRoute({ ...route, ".id": "*42", "dst-address": "fd6d:6370:1:2::1/128" }, context)?.addressId, "*12") +assert.equal(deriveOwnedConnectedRoute({ ...route, ".id": "*43", "dst-address": "fe80::/64" }, context)?.addressId, "*21") +assert.equal(deriveOwnedConnectedRoute({ ...route, ".id": "*44", gateway: names[1], "dst-address": "fe80::/64" }, context)?.addressId, "*22") +assert.equal(deriveOwnedConnectedRoute({ ...route, "dst-address": "fd6d:6370:0001:0002:0:0:0:1/128" }, context)?.addressId, "*12") +const alternateLinkLocal = catalog() +alternateLinkLocal.ipv6_address.push({ ".id": "*25", interface: names[0], dynamic: "true", address: "fea0:1:2:3::1/64" }) +const alternateContext = { ...context, addresses: assertOwnedAddressClosure(alternateLinkLocal, addresses, bridges, marker, policy) } +assert.equal(deriveOwnedConnectedRoute({ ...route, "dst-address": "fe80::/64" }, alternateContext), undefined) +assert.equal(deriveOwnedConnectedRoute({ ...route, "dst-address": "fea0:1:2:3::/64" }, alternateContext)?.addressId, "*25") +for (const patch of [ + { connect: undefined, distance: "0", scope: "10" }, { connect: "false", protocol: "connect" }, { dynamic: "false" }, + { "dst-address": "198.18.9.1/32" }, { gateway: "ether1" }, { gateway: `prefix${names[0]}` }, + { gateway: `fe80::1%${names[0]}-foreign` }, { gateway: `${names[0]},ether1` }, { "routing-table": "foreign" }, + { "immediate-gw": "ether1" }, { interface: "ether1" }, { ".id": undefined }, { protocol: "static" }, +] as NativeRow[]) { assert.equal(deriveOwnedConnectedRoute({ ...route, ...patch }, context), undefined); checks++ } + +// Rename may change ONLY name. A response that silently adopts physical +// membership, opaque fields, neighbor rows or ID changes is never accepted. +const oldVrf: NativeRow = { ".id": "*30", name: `${marker}-v`, comment: marker, interfaces: names[0], disabled: "false", opaque: "preserve" } +const vrf: NativeOwnedObject = { resource: "vrf", id: "*30", marker, expected: oldVrf } +const renamed = { ...oldVrf, name: `${marker}-r` } +assert.deepEqual(acceptNativePatch(vrf, { name: `${marker}-r` }, [oldVrf], [renamed], bridges).expected, renamed) +assertVrfMembersOwned({ interfaces: names.join(",") }, bridges) +assertVrfMembersOwned({ interfaces: "" }, bridges) +for (const interfaces of ["ether1", "all", `${names[0]},ether1`, `${names[0]},${names[0]}`, ` ${names[0]}`, undefined]) { refused(() => assertVrfMembersOwned({ interfaces }, bridges)); checks++ } +for (const row of [{ ...renamed, interfaces: "ether1" }, { ...renamed, interfaces: names[1] }, { ...renamed, opaque: "changed" }, { ...renamed, ".id": "*31" }, { ...renamed, disabled: "bad" }, oldVrf]) { + refused(() => acceptNativePatch(vrf, { name: `${marker}-r` }, [oldVrf], [row], bridges)); checks++ +} +refused(() => acceptNativePatch(vrf, { interfaces: "ether1" }, [oldVrf], [{ ...oldVrf, interfaces: "ether1" }], bridges)) +refused(() => acceptNativePatch(vrf, { running: "false" }, [oldVrf], [{ ...oldVrf, running: "true" }], bridges)) +refused(() => acceptNativePatch(vrf, { disabled: "unknown" }, [oldVrf], [{ ...oldVrf, disabled: "unknown" }], bridges)) +refused(() => acceptNativePatch(vrf, { name: `${marker}-r` }, [oldVrf, previous], [renamed, { ...previous, opaque: "changed" }], bridges)) +const oldOpaqueBoolean = { ...oldVrf, disabled: "bad1" } +refused(() => acceptNativePatch({ ...vrf, expected: oldOpaqueBoolean }, { name: `${marker}-r` }, [oldOpaqueBoolean], [{ ...renamed, disabled: "bad2" }], bridges)) +const telemetryOnly = { ...renamed, running: "true", "rx-byte": "100" } +assert.equal(acceptNativePatch(vrf, { name: `${marker}-r` }, [oldVrf], [telemetryOnly], bridges).id, "*30") +for (const key of ["rx-bytes", "tx-unreviewed-setting", "max-bytes", "custom-errors"]) { + refused(() => acceptNativePatch(vrf, { name: `${marker}-r` }, [oldVrf], [{ ...renamed, [key]: "unexpected" }], bridges)); checks++ +} +assert.equal(deriveOwnedBridgeInventory({ ...bridges[0].expected, type: "bridge" }, bridges)?.id, "*A") +for (const patch of [{ ".id": "*99" }, { comment: "foreign" }, { type: "ether" }, { name: `${names[0]}-foreign` }]) { + assert.equal(deriveOwnedBridgeInventory({ ...bridges[0].expected, type: "bridge", ...patch }, bridges), undefined); checks++ +} +const currentVrf = acceptNativePatch(vrf, { name: `${marker}-r` }, [oldVrf], [renamed], bridges) +const table: NativeRow = { ".id": "*30", name: renamed.name, dynamic: "true", vrf: "true", fib: "true" } +assert.equal(deriveVrfTableCompanion(table, currentVrf)?.vrfId, "*30") +assert.equal(deriveVrfTableCompanion({ ...table, ".id": "*50", vrf: "*30" }, currentVrf)?.id, "*50") +for (const patch of [{ name: oldVrf.name }, { name: "foreign" }, { dynamic: "false" }, { dynamic: "unknown" }, { vrf: "false" }, { vrf: "foreign" }, { vrf: undefined }, { ".id": undefined }]) { + assert.equal(deriveVrfTableCompanion({ ...table, ...patch }, currentVrf), undefined); checks++ +} +const badDynamic = catalog(); badDynamic.ipv4_address[0].dynamic = "unknown" +refused(() => assertOwnedAddressClosure(badDynamic, [{ ...addresses[0], expected: badDynamic.ipv4_address[0] }, addresses[1]], bridges, marker, policy)) +assertNativeDelete(vrf, [oldVrf, previous], [previous]) +for (const after of [[oldVrf, previous], [], [{ ...previous, opaque: "changed" }], [previous, bridgeRow("*99", "extra")]]) { + refused(() => assertNativeDelete(vrf, [oldVrf, previous], after)); checks++ +} +const baselineCapabilities: NativeCatalog = { + interface_inventory: [{ ".id": "*90", name: "ether1" }], + ipv4_route: [{ ".id": "*91", dynamic: "true", connect: "true", gateway: "ether1", "routing-table": "main", "dst-address": "192.0.2.0/24" }], + ipv6_route: [{ ".id": "*92", dynamic: "true", connected: "true", gateway: "ether1", "routing-table": "main", "dst-address": "fe80::/64" }], +} +assert.equal(assessNativeConnectedCapabilities(baselineCapabilities).status, "proven-baseline-schema") +for (const change of ["missing-v6", "scope-only", "substring", "contradictory-context", "no-inventory"] as const) { + const value = structuredClone(baselineCapabilities) + if (change === "missing-v6") value.ipv6_route = [] + if (change === "scope-only") { delete value.ipv6_route[0].connected; value.ipv6_route[0].scope = "10"; value.ipv6_route[0].distance = "0" } + if (change === "substring") value.ipv6_route[0].gateway = "ether1-foreign" + if (change === "contradictory-context") value.ipv6_route[0].interface = "ether2" + if (change === "no-inventory") value.interface_inventory = [] + assert.equal(assessNativeConnectedCapabilities(value).status, "unproven-zero-mutations"); checks++ +} +console.log(`VRF companion pure proof: ${checks} rejection models plus exact ACK IDs, address closure, typed link-local evidence, strict connected routes and full PATCH conservation; zero SQL/network`) + + +// Replay only immutable recorded raw ACK phases, then the separately recorded +// RouterOS 7.24.5 candidates. Foreign baseline conservation remains the runner's +// independent 34-hash guard; this model tests the companion pinning contract. +const recorded = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v2.json", import.meta.url), "utf8")) +const observed = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-readonly-diagnostic-v1.json", import.meta.url), "utf8")) +const actualMarker: string = recorded.marker +const actualPolicy = { allowedAddressVrfs: ["main", "*1", `${actualMarker}-v`] } +const catalogOf = (phase: { owned: Record }): NativeCatalog => Object.fromEntries(Object.entries(phase.owned).map(([resource, value]) => [resource, structuredClone(value.raw)])) +const creations = recorded.journal as Array<{ resource: string; assignedId: string; fields: NativeRow }> +let before = catalogOf(recorded.phases[0]) +let owners: NativeOwnedObject[] = [] +let pinned: ReturnType = [] +let preVrfCatalog = before, preVrfOwners = owners, preVrfEvidence = pinned +for (const [index, creation] of creations.entries()) { + const after: NativeCatalog = index === 6 ? structuredClone(observed.candidates) : catalogOf(recorded.phases[index + 1]) + const row = after[creation.resource].find(item => item[".id"] === creation.assignedId)! + const nextOwners = [...owners, { resource: creation.resource, id: creation.assignedId, marker: actualMarker, expected: structuredClone(row) }] + if (index === 6) { preVrfCatalog = before; preVrfOwners = owners; preVrfEvidence = pinned } + pinned = advanceNativeKernelEvidence({ before, after, beforeAccepted: owners, afterAccepted: nextOwners, marker: actualMarker, policy: actualPolicy, previous: pinned, transition: { resource: creation.resource, method: "PUT", id: creation.assignedId, fields: creation.fields } }) + assertNativeKernelEvidence(after, nextOwners, actualMarker, actualPolicy, pinned) + before = after; owners = nextOwners +} +assert.equal(pinned.filter(item => item.kind === "virtual").length, 1) +assert.equal(pinned.find(item => item.kind === "virtual")!.id, "*6") +assert.equal(pinned.find(item => item.kind === "table")!.id, "*1") +assert.ok(pinned.filter(item => item.kind === "route").some(item => String(item.expected.gateway).includes("@"))) +const liveCatalog = before, liveOwners = owners, liveEvidence = pinned +for (const kind of ["bridge", "virtual", "table", "link-local", "route"] as const) { + const evidence = liveEvidence.find(item => item.kind === kind)! + for (const field of ["opaque-new-field", "rx-opaque-config", "tx-opaque-config", "opaque-bytes", "opaque-packets"]) { + const altered = structuredClone(liveCatalog) + altered[evidence.resource].find(row => row[".id"] === evidence.id)![field] = "unrequested" + refused(() => assertNativeKernelEvidence(altered, liveOwners, actualMarker, actualPolicy, liveEvidence)); checks++ + } + const altered = structuredClone(liveCatalog) + altered[evidence.resource].find(row => row[".id"] === evidence.id)![".id"] = "*FE" + refused(() => assertNativeKernelEvidence(altered, liveOwners, actualMarker, actualPolicy, liveEvidence)); checks++ +} +const vrfTransition = { resource: "vrf", method: "PUT" as const, id: "*1", fields: creations[6].fields } +const advanceVrf = (after: NativeCatalog, before = preVrfCatalog) => advanceNativeKernelEvidence({ before, after, beforeAccepted: preVrfOwners, afterAccepted: liveOwners, marker: actualMarker, policy: actualPolicy, previous: preVrfEvidence, transition: vrfTransition }) +for (const kind of ["bridge", "virtual", "table", "link-local", "route"] as const) { + const target = liveEvidence.find(item => item.kind === kind)! + // Unknown native configuration remains conserved even at an ACK boundary. + // Newly created virtual/table fields are pinned at that exact boundary; + // existing bridge/LL/route fields must also agree with their previous proof. + if (["virtual", "table"].includes(kind)) continue + const altered = structuredClone(liveCatalog) + altered[target.resource].find(row => row[".id"] === target.id)!["tx-opaque"] = "changed" + refused(() => advanceVrf(altered)); checks++ +} +for (const field of [{ type: "bridge" }, { name: "foreign" }, { comment: "foreign" }, { vrf: "foreign" }]) { + const altered = structuredClone(liveCatalog) + Object.assign(altered.interface_inventory.find(row => row.type === "vrf")!, field) + refused(() => advanceVrf(altered)); checks++ +} +const occupied = structuredClone(preVrfCatalog) +occupied.interface_inventory.push({ ".id": "*6", name: "foreign", type: "ether" }) +refused(() => advanceVrf(liveCatalog, occupied)); checks++ +const duplicateVirtual = structuredClone(liveCatalog) +duplicateVirtual.interface_inventory.push({ ...duplicateVirtual.interface_inventory.find(row => row.type === "vrf")!, ".id": "*FE" }) +refused(() => advanceVrf(duplicateVirtual)); checks++ +for (const change of [{ gateway: "other@foreign" }, { "immediate-gw": "other" }, { "local-address": "198.18.101.1%other" }, { "routing-table": "main" }]) { + const altered = structuredClone(liveCatalog) + Object.assign(altered.ipv4_route.find(row => String(row.gateway).includes("@"))!, change) + refused(() => advanceVrf(altered)); checks++ +} +console.log(`VRF lifecycle pinned companion models passed (${checks} refusal cases)`) + + +// Explicit VRF DELETE restores recorded pre-VRF context, while kernel LL IDs +// may regenerate once at that acknowledged boundary (never at read-only check). +const reverted = structuredClone(preVrfCatalog) +reverted.ipv6_address.find(row => row[".id"] === "*4")![".id"] = "*9" +const withoutVrf = liveOwners.filter(item => item.resource !== "vrf") +const deleteVrfInput = { before: liveCatalog, after: reverted, beforeAccepted: liveOwners, afterAccepted: withoutVrf, marker: actualMarker, policy: actualPolicy, previous: liveEvidence, transition: { resource: "vrf", method: "DELETE" as const, id: "*1", fields: {} } } +let cleanupEvidence = advanceNativeKernelEvidence(deleteVrfInput) +assertNativeKernelEvidence(reverted, withoutVrf, actualMarker, actualPolicy, cleanupEvidence) +for (const kind of ["virtual", "table"] as const) { + const old = liveEvidence.find(item => item.kind === kind)! + const altered = structuredClone(reverted) + altered[old.resource].push({ ...old.expected, name: "foreign-reused" }) + refused(() => advanceNativeKernelEvidence({ ...deleteVrfInput, after: altered })); checks++ +} +for (const change of [{ "opaque-new-field": "changed" }, { "auto-link-local": "false" }, { "link-local": "false" }, { address: "fe80::1234/64" }, { vrf: "foreign" }]) { + const altered = structuredClone(reverted) + Object.assign(altered.ipv6_address.find(row => row[".id"] === "*9")!, change) + refused(() => advanceNativeKernelEvidence({ ...deleteVrfInput, after: altered })); checks++ +} +const arbitraryStatus = structuredClone(reverted) +arbitraryStatus.interface_inventory.find(row => row[".id"] === "*4")!.status = "--LERU--O---------------" +refused(() => advanceNativeKernelEvidence({ ...deleteVrfInput, after: arbitraryStatus })); checks++ +let cleanupCatalog = reverted, cleanupOwners = withoutVrf +for (const resource of ["ipv6_address", "ipv4_address", "bridge"]) for (const owner of cleanupOwners.filter(item => item.resource === resource)) { + const after = structuredClone(cleanupCatalog), nextOwners = cleanupOwners.filter(item => item !== owner) + after[resource] = after[resource].filter(row => row[".id"] !== owner.id) + if (resource === "bridge") { + after.interface_inventory = after.interface_inventory.filter(row => row[".id"] !== owner.id) + after.ipv6_address = after.ipv6_address.filter(row => row.interface !== owner.expected.name) + for (const routes of ["ipv4_route", "ipv6_route"]) after[routes] = after[routes].filter(row => row.gateway !== owner.expected.name) + } else { + const routes = resource === "ipv4_address" ? "ipv4_route" : "ipv6_route" + after[routes] = after[routes].filter(row => row["dst-address"] !== owner.expected.address) + if (resource === "ipv4_address") after.interface_inventory.find(row => row.name === owner.expected.interface)!.status = "--LERU--O---------------" + } + cleanupEvidence = advanceNativeKernelEvidence({ before: cleanupCatalog, after, beforeAccepted: cleanupOwners, afterAccepted: nextOwners, marker: actualMarker, policy: actualPolicy, previous: cleanupEvidence, transition: { resource, method: "DELETE", id: owner.id, fields: {} } }) + assertNativeKernelEvidence(after, nextOwners, actualMarker, actualPolicy, cleanupEvidence) + cleanupCatalog = after; cleanupOwners = nextOwners +} +assert.equal(cleanupEvidence.length, 0) +assert.equal(cleanupOwners.length, 0) +const acceptedJournal = VRF_NATIVE_LIFECYCLE_PHASES.map(phase => ({ phase, outcome: "acknowledged", ownedIdentityConfirmed: true, readbackVerified: true, requestedChange: true })) +assert.equal(summarizeNativeVrfLifecycle(acceptedJournal).fullLifecycleSupported, true) +for (const replacement of [{ outcome: "http-error", unchangedReadback: true }, { outcome: "unknown" }, { ownedIdentityConfirmed: false }, { readbackVerified: false }, { requestedChange: false }, { requestedChange: undefined }]) { + const journal: NativeRow[] = structuredClone(acceptedJournal) + Object.assign(journal[2], replacement) + const result = summarizeNativeVrfLifecycle(journal) + assert.equal(result.fullLifecycleSupported, false) + assert.equal(result.testedPhases[2].accepted, false) +} +assert.equal(summarizeNativeVrfLifecycle(acceptedJournal.slice(0, -1)).fullLifecycleSupported, false) +assert.equal(summarizeNativeVrfLifecycle([...acceptedJournal, acceptedJournal[0]]).fullLifecycleSupported, false) +console.log(`VRF lifecycle DELETE/checkpoint and complete-report models passed (${checks} refusal cases total)`) + +for (const resource of ["ipv4_address", "ipv6_address"]) { + const staticA = liveOwners.find(item => item.resource === resource && String(item.expected.interface).endsWith("-a"))! + for (const vrf of ["main", "*1", `${actualMarker}-r`, "", undefined]) { + const altered = structuredClone(liveCatalog) + altered[resource].find(row => row[".id"] === staticA.id)!.vrf = vrf + refused(() => assertNativeKernelEvidence(altered, liveOwners, actualMarker, { allowedAddressVrfs: [...actualPolicy.allowedAddressVrfs, `${actualMarker}-r`] }, liveEvidence)); checks++ + } +} +console.log(`VRF lifecycle static current-member binding passed (${checks} refusal cases total)`) + +const renamePolicy = { allowedAddressVrfs: [...actualPolicy.allowedAddressVrfs, `${actualMarker}-r`] } +const renameOwners = structuredClone(liveOwners) +renameOwners.find(item => item.resource === "vrf")!.expected.name = `${actualMarker}-r` +const renameCatalog = structuredClone(liveCatalog) +renameCatalog.vrf.find(row => row[".id"] === "*1")!.name = `${actualMarker}-r` +const renameInput = { before: liveCatalog, after: renameCatalog, beforeAccepted: liveOwners, afterAccepted: renameOwners, marker: actualMarker, policy: renamePolicy, previous: liveEvidence, transition: { resource: "vrf", method: "PATCH" as const, id: "*1", fields: { name: `${actualMarker}-r` } } } +// The broad legacy allowlist includes both names; strict current-member binding +// must independently refuse historical name or main after an acknowledged rename. +for (const old of ["main", `${actualMarker}-v`, "*1"]) { + const altered = structuredClone(renameCatalog) + for (const resource of ["ipv4_address", "ipv6_address"]) for (const row of altered[resource]) if (row.interface === `${actualMarker}-a` && row.dynamic === "false") row.vrf = old + assert.throws(() => advanceNativeKernelEvidence({ ...renameInput, after: altered }), /static address current member VRF binding/) + checks++ +} +for (const resource of ["ipv4_address", "ipv6_address"]) for (const row of renameCatalog[resource]) if (row.interface === `${actualMarker}-a` && row.dynamic === "false") row.vrf = `${actualMarker}-r` +for (const row of renameCatalog.interface_inventory) { + if (row.type === "vrf") row.name = `${actualMarker}-r` + if (row.type === "bridge" && row.name === `${actualMarker}-a`) row.vrf = `${actualMarker}-r` +} +renameCatalog.routing_table[0].name = `${actualMarker}-r` +for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of renameCatalog[resource]) if (row["routing-table"] === `${actualMarker}-v`) { + row["routing-table"] = `${actualMarker}-r` + row.gateway = `${actualMarker}-a@${actualMarker}-r` + if (typeof row["local-address"] === "string") row["local-address"] = row["local-address"].replace(`@${actualMarker}-v`, `@${actualMarker}-r`) +} +const renamedEvidence = advanceNativeKernelEvidence(renameInput) +assertNativeKernelEvidence(renameCatalog, renameOwners, actualMarker, renamePolicy, renamedEvidence) +const renameOpaque = structuredClone(renameCatalog) +renameOpaque.interface_inventory.find(row => row.type === "vrf")!.ifname = "unobserved-companion-change" +refused(() => advanceNativeKernelEvidence({ ...renameInput, after: renameOpaque })); checks++ +console.log(`VRF lifecycle exact rename/unknown-field models passed (${checks} refusal cases total)`) + + +// v3 proves disable; this re-enable shape is a synthetic bounded model. +// The later v4 native source is checked independently below. +const v3 = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v3.json", import.meta.url), "utf8")) +const offDiagnostic = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-readonly-diagnostic-v3.json", import.meta.url), "utf8")) +const v3Rename = v3.phases.find((phase: { phase: string }) => phase.phase === "vrf-rename") +const v3Marker: string = v3.marker +const onCatalog = catalogOf(v3Rename) +const onOwners: NativeOwnedObject[] = ["bridge", "ipv4_address", "ipv6_address", "vrf"].flatMap(resource => onCatalog[resource].filter(row => row.comment === v3Marker).map(row => ({ resource, id: String(row[".id"]), marker: v3Marker, expected: structuredClone(row) }))) +const onEvidence = v3Rename.attributions.map((item: { evidence: NativeRow }) => item.evidence).filter((item: NativeRow) => item.resource !== undefined) as ReturnType +const offCatalog: NativeCatalog = structuredClone(offDiagnostic.candidates) +const offOwners = structuredClone(onOwners) +offOwners.find(item => item.resource === "vrf")!.expected.disabled = "true" +const v3Policy = { allowedAddressVrfs: ["main", "*2", `${v3Marker}-r`] } +const disableInput = { before: onCatalog, after: offCatalog, beforeAccepted: onOwners, afterAccepted: offOwners, marker: v3Marker, policy: v3Policy, previous: onEvidence, transition: { resource: "vrf", method: "PATCH" as const, id: "*2", fields: { disabled: "true" } } } +const offEvidence = advanceNativeKernelEvidence(disableInput) +assertNativeKernelEvidence(offCatalog, offOwners, v3Marker, v3Policy, offEvidence) +assert.equal(offEvidence.find(item => item.kind === "table")!.present, false) +assert.equal(offEvidence.find(item => item.kind === "table")!.id, "*2") +assert.equal(offEvidence.find(item => item.kind === "virtual")!.id, "*9") +assert.deepEqual(offEvidence.find(item => item.kind === "virtual")!.activeExpected, onEvidence.find(item => item.kind === "virtual")!.expected) +assert.deepEqual(offEvidence.find(item => item.kind === "virtual")!.disabledExpected, offCatalog.interface_inventory.find(row => row.type === "vrf")) +for (const kind of ["bridge", "virtual", "link-local", "route"] as const) { + const target = offEvidence.find(item => item.kind === kind)! + for (const field of ["opaque-field", "tx-opaque-config", "rx-opaque-config"]) { + const changed = structuredClone(offCatalog) + changed[target.resource].find(row => row[".id"] === target.id)![field] = "changed" + refused(() => advanceNativeKernelEvidence({ ...disableInput, after: changed })); checks++ + } +} +for (const field of ["disabled", "status", "mac-address", "link-downs", "last-link-down-time", "ifname", "ifindex", "flags", "actual-mtu"]) { + const changed = structuredClone(offCatalog) + changed.interface_inventory.find(row => row.type === "vrf")![field] = "unobserved" + refused(() => advanceNativeKernelEvidence({ ...disableInput, after: changed })); checks++ +} +const offWithTable = structuredClone(offCatalog) +offWithTable.routing_table.push(structuredClone(onCatalog.routing_table[0])) +refused(() => advanceNativeKernelEvidence({ ...disableInput, after: offWithTable })); checks++ +refused(() => assertNativeKernelEvidence(offCatalog, offOwners, v3Marker, v3Policy, offEvidence.filter(item => item.kind !== "table"))); checks++ +for (const resource of ["ipv4_route", "ipv6_route"]) { + const changed = structuredClone(offCatalog) + changed[resource] = changed[resource].slice(1) + refused(() => advanceNativeKernelEvidence({ ...disableInput, after: changed })); checks++ +} +const proposedOn = structuredClone(onCatalog) +proposedOn.ipv6_address.find(row => row[".id"] === "*E")![".id"] = "*11" +const proposedVirtual = proposedOn.interface_inventory.find(row => row.type === "vrf")! +Object.assign(proposedVirtual, { "mac-address": "02:11:22:33:44:55", ifindex: "101", "last-link-up-time": "2026-10-24 14:16:23", "last-link-down-time": "2026-10-24 14:16:22", "link-downs": "1" }) +const reenableInput = { before: offCatalog, after: proposedOn, beforeAccepted: offOwners, afterAccepted: onOwners, marker: v3Marker, policy: v3Policy, previous: offEvidence, transition: { resource: "vrf", method: "PATCH" as const, id: "*2", fields: { disabled: "false" } } } +const proposedEvidence = advanceNativeKernelEvidence(reenableInput) +assertNativeKernelEvidence(proposedOn, onOwners, v3Marker, v3Policy, proposedEvidence) +assert.equal(proposedEvidence.find(item => item.kind === "table")!.present, undefined) +assert.deepEqual(proposedEvidence.find(item => item.kind === "virtual")!.activeExpected, proposedVirtual) +assert.deepEqual(proposedEvidence.find(item => item.kind === "virtual")!.disabledExpected, offCatalog.interface_inventory.find(row => row.type === "vrf")) +for (const change of [ + { ".id": "*12" }, { ifname: "vrf999" }, { ifindex: "0" }, { ifindex: "-1" }, { ifindex: "01" }, + { "mac-address": "00:00:00:00:00:00" }, { "mac-address": "03:11:22:33:44:55" }, { "mac-address": "00:11:22:33:44:55" }, + { "last-link-up-time": "2026-10-24 14:16:21" }, { "last-link-up-time": "not-a-time" }, { "last-link-up-time": "2026-99-24 14:16:23" }, { "last-link-up-time": "2026-02-30 14:16:23" }, { "last-link-up-time": "2026-10-24 25:16:23" }, { "link-downs": "0" }, { "last-link-down-time": "2026-10-24 14:16:23" }, + { flags: "unobserved" }, { status: "unobserved" }, { mtu: "1500" }, { "actual-mtu": "1500" }, { caps: "unobserved" }, { "opaque-field": "changed" }, +]) { + const changed = structuredClone(proposedOn) + Object.assign(changed.interface_inventory.find(row => row.type === "vrf")!, change) + refused(() => advanceNativeKernelEvidence({ ...reenableInput, after: changed })); checks++ +} +for (const change of [{ ifindex: "101" }, { "mac-address": "02:11:22:33:44:55" }]) { + const changed = structuredClone(proposedOn) + changed.interface_inventory.push({ ".id": "*F0", name: "foreign", type: "ether", ...change }) + refused(() => advanceNativeKernelEvidence({ ...reenableInput, after: changed })); checks++ +} +for (const field of ["mac-address", "ifindex", "last-link-up-time"]) { + const changed = structuredClone(proposedOn) + changed.interface_inventory.find(row => row.type === "vrf")![field] = field === "mac-address" ? "02:11:22:33:44:66" : field === "ifindex" ? "102" : "2026-10-24 14:16:24" + refused(() => assertNativeKernelEvidence(changed, onOwners, v3Marker, v3Policy, proposedEvidence)); checks++ +} +for (const resource of ["ipv4_route", "ipv6_route", "routing_table"]) { + const changed = structuredClone(proposedOn) + changed[resource] = changed[resource].slice(1) + refused(() => advanceNativeKernelEvidence({ ...reenableInput, after: changed })); checks++ +} +const extraFields = { ...reenableInput, transition: { ...reenableInput.transition, fields: { disabled: "false", interfaces: `${v3Marker}-a` } } } +refused(() => advanceNativeKernelEvidence(extraFields)); checks++ +const noOnProvenance = offEvidence.map(item => item.kind === "virtual" ? { ...item, activeExpected: undefined } : item) +refused(() => advanceNativeKernelEvidence({ ...reenableInput, previous: noOnProvenance })); checks++ +const unknownEnabled = structuredClone(offOwners) +unknownEnabled.find(item => item.resource === "vrf")!.expected.disabled = "unknown" +refused(() => assertNativeKernelEvidence(offCatalog, unknownEnabled, v3Marker, v3Policy, offEvidence)); checks++ +console.log(`VRF observed disable + bounded re-enable models passed (${checks} refusal cases total); v3 re-enable model synthetic, v4 native source checked below`) + +const duplicateTombstone = [...offEvidence, { ...offEvidence.find(item => item.kind === "table")!, key: "forged-distinct-key" }] +refused(() => assertNativeKernelEvidence(offCatalog, offOwners, v3Marker, v3Policy, duplicateTombstone)); checks++ +const staleTableParent = offEvidence.map(item => item.kind === "table" ? { ...item, expected: { ...item.expected, name: "historical-name" } } : item) +refused(() => assertNativeKernelEvidence(offCatalog, offOwners, v3Marker, v3Policy, staleTableParent)); checks++ +const offVrfDeleted = structuredClone(offCatalog) +offVrfDeleted.vrf = [] +offVrfDeleted.interface_inventory = offVrfDeleted.interface_inventory.filter(row => row.type !== "vrf") +const offVrfDeletedOwners = offOwners.filter(item => item.resource !== "vrf") +const afterDisabledDelete = advanceNativeKernelEvidence({ before: offCatalog, after: offVrfDeleted, beforeAccepted: offOwners, afterAccepted: offVrfDeletedOwners, marker: v3Marker, policy: v3Policy, previous: offEvidence, transition: { resource: "vrf", method: "DELETE", id: "*2", fields: {} } }) +assert.equal(afterDisabledDelete.some(item => item.kind === "table" || item.kind === "virtual"), false) +assertNativeKernelEvidence(offVrfDeleted, offVrfDeletedOwners, v3Marker, v3Policy, afterDisabledDelete) +console.log(`VRF dormant provenance and disabled-parent DELETE passed (${checks} refusal cases total)`) + + +// Actual v4 transfer proves a specific missing LL + route pair and native +// permutation of IDs between fully conserved owned semantic routes. +const v4 = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v4.json", import.meta.url), "utf8")) +const transferDiagnostic = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-readonly-diagnostic-v4.json", import.meta.url), "utf8")) +const verifiedOn = v4.phases.find((phase: { phase: string }) => phase.phase === "vrf-reenable") +const transferMarker: string = v4.marker +const transferBefore = catalogOf(verifiedOn) +const transferOwners: NativeOwnedObject[] = ["bridge", "ipv4_address", "ipv6_address", "vrf"].flatMap(resource => transferBefore[resource].filter(row => row.comment === transferMarker).map(row => ({ resource, id: String(row[".id"]), marker: transferMarker, expected: structuredClone(row) }))) +const transferPrevious = verifiedOn.attributions.map((item: { evidence: NativeRow }) => item.evidence).filter((item: NativeRow) => item.resource !== undefined) as ReturnType +const transferredOwners = structuredClone(transferOwners) +transferredOwners.find(item => item.resource === "vrf")!.expected.interfaces = `${transferMarker}-b` +const transferAfter: NativeCatalog = structuredClone(transferDiagnostic.candidates) +const transferPolicy = { allowedAddressVrfs: ["main", "*3", `${transferMarker}-r`] } +const transferInput = { before: transferBefore, after: transferAfter, beforeAccepted: transferOwners, afterAccepted: transferredOwners, marker: transferMarker, policy: transferPolicy, previous: transferPrevious, transition: { resource: "vrf", method: "PATCH" as const, id: "*3", fields: { interfaces: `${transferMarker}-b` } } } +const transferredEvidence = advanceNativeKernelEvidence(transferInput) +assertNativeKernelEvidence(transferAfter, transferredOwners, transferMarker, transferPolicy, transferredEvidence) +assert.equal(transferredEvidence.filter(item => item.present === false && item.absenceReason === "transfer-ack").length, 2) +assert.equal(transferredEvidence.find(item => item.kind === "link-local" && item.parentId === "*B")!.present, false) +assert.equal(transferredEvidence.find(item => item.kind === "route" && item.parentId === "*B" && item.present === false)!.id, "*20145050") +assert.equal(transferredEvidence.find(item => item.kind === "route" && item.id === "*201450A0" && item.present !== false)!.parentId, "*B") +for (const resource of ["ipv4_route", "ipv6_route"]) { + const before = structuredClone(transferBefore) + const target = transferAfter[resource].find(row => row.gateway === `${transferMarker}-a`)! + before[resource].push({ ".id": target[".id"], gateway: "ether1", "routing-table": "main", "dst-address": resource === "ipv4_route" ? "203.0.113.0/24" : "2001:db8::/64", dynamic: "false" }) + refused(() => advanceNativeKernelEvidence({ ...transferInput, before })); checks++ + for (const field of ["scope", "tx-opaque-config", "rx-opaque-config", "unknown-config"]) { + const after = structuredClone(transferAfter) + after[resource].find(row => row[".id"] === "*201450A0" || row[".id"] === "*201450B0")![field] = "changed" + refused(() => advanceNativeKernelEvidence({ ...transferInput, after })); checks++ + } +} +const missingA = structuredClone(transferAfter) +missingA.ipv6_address = missingA.ipv6_address.filter(row => row[".id"] !== "*17") +refused(() => advanceNativeKernelEvidence({ ...transferInput, after: missingA })); checks++ +const missingBStaticRoute = structuredClone(transferAfter) +missingBStaticRoute.ipv6_route = missingBStaticRoute.ipv6_route.filter(row => row[".id"] !== "*201450B0") +refused(() => advanceNativeKernelEvidence({ ...transferInput, after: missingBStaticRoute })); checks++ +const oldBll = transferPrevious.find(item => item.kind === "link-local" && item.parentId === "*B")! +const oldBllRoute = transferPrevious.find(item => item.kind === "route" && item.parentId === "*B" && item.expected["dst-address"] === "fe80::/64")! +const prematureReturn = structuredClone(transferAfter) +prematureReturn.ipv6_address.push({ ...oldBll.expected, ".id": "*18" }) +prematureReturn.ipv6_route.push({ ...oldBllRoute.expected, ".id": "*E3", gateway: `${transferMarker}-b@${transferMarker}-r`, "routing-table": `${transferMarker}-r` }) +refused(() => assertNativeKernelEvidence(prematureReturn, transferredOwners, transferMarker, transferPolicy, transferredEvidence)); checks++ +const lostReason = transferredEvidence.map(item => item.present === false ? { ...item, absenceReason: undefined } : item) +refused(() => assertNativeKernelEvidence(transferAfter, transferredOwners, transferMarker, transferPolicy, lostReason)); checks++ +const idReassignment = structuredClone(transferAfter) +const first = idReassignment.ipv4_route[0][".id"] +idReassignment.ipv4_route[0][".id"] = idReassignment.ipv4_route[1][".id"] +idReassignment.ipv4_route[1][".id"] = first +refused(() => assertNativeKernelEvidence(idReassignment, transferredOwners, transferMarker, transferPolicy, transferredEvidence)); checks++ +const transferDeletedOwners = transferredOwners.filter(item => item.resource !== "vrf") +for (const regenerate of [false, true]) { + const deleted = structuredClone(transferAfter) + deleted.vrf = []; deleted.routing_table = [] + deleted.interface_inventory = deleted.interface_inventory.filter(row => row.type !== "vrf") + const b = deleted.interface_inventory.find(row => row[".id"] === "*B")! + b.vrf = "main"; b.status = "--LERUI-O---------------" + for (const resource of ["ipv4_address", "ipv6_address"]) for (const row of deleted[resource]) if (row.comment === transferMarker) row.vrf = "main" + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of deleted[resource]) if (row["routing-table"] === `${transferMarker}-r`) { + row["routing-table"] = "main"; row.gateway = `${transferMarker}-b` + if (typeof row["local-address"] === "string") row["local-address"] = row["local-address"].replace(`@${transferMarker}-r`, "") + } + if (regenerate) { + deleted.ipv6_address.push({ ...oldBll.expected, ".id": "*18" }) + deleted.ipv6_route.push({ ...oldBllRoute.expected, ".id": "*E3" }) + } + // RouterOS may permute owned route IDs even for the bridge whose context + // already was main. Its entire row except native ID must remain conserved. + const aRoute = deleted.ipv4_route.find(row => row.gateway === `${transferMarker}-a`)! + const bRoute = deleted.ipv4_route.find(row => row.gateway === `${transferMarker}-b`)! + const aRouteId = aRoute[".id"]; aRoute[".id"] = bRoute[".id"]; bRoute[".id"] = aRouteId + const deleteInput = { before: transferAfter, after: deleted, beforeAccepted: transferredOwners, afterAccepted: transferDeletedOwners, marker: transferMarker, policy: transferPolicy, previous: transferredEvidence, transition: { resource: "vrf", method: "DELETE" as const, id: "*3", fields: {} } } + const deletedEvidence = advanceNativeKernelEvidence(deleteInput) + assertNativeKernelEvidence(deleted, transferDeletedOwners, transferMarker, transferPolicy, deletedEvidence) + assert.equal(deletedEvidence.find(item => item.kind === "link-local" && item.parentId === "*B")!.present === false, !regenerate) + const changed = structuredClone(deleted) + if (regenerate) changed.ipv6_address.find(row => row[".id"] === "*18")![".id"] = "*19" + else changed.ipv6_address.push({ ...oldBll.expected, ".id": "*18" }) + refused(() => assertNativeKernelEvidence(changed, transferDeletedOwners, transferMarker, transferPolicy, deletedEvidence)); checks++ + if (regenerate) for (const patch of [{ address: "fe80::1234/64" }, { "auto-link-local": "false" }, { "unknown-config": "changed" }]) { + const bad = structuredClone(deleted) + Object.assign(bad.ipv6_address.find(row => row[".id"] === "*18")!, patch) + refused(() => advanceNativeKernelEvidence({ ...deleteInput, after: bad })); checks++ + } +} +console.log(`VRF actual transfer/permuted owned route IDs + pinned LL absence/deletion branches passed (${checks} refusal cases total)`) + +const missingPair = transferredEvidence.filter(item => !(item.present === false && item.kind === "route")) +refused(() => assertNativeKernelEvidence(transferAfter, transferredOwners, transferMarker, transferPolicy, missingPair)); checks++ +const ownPoolReuse = structuredClone(transferAfter) +// Route resources have independent ID namespaces; use the IPv6 LL route's ID +// inside IPv6 only, then prove the tombstone cannot mask its live own successor. +ownPoolReuse.ipv6_route.find(row => row.gateway === `${transferMarker}-a` && row["dst-address"] !== "fe80::/64")![".id"] = "*20145050" +const ownPoolEvidence = advanceNativeKernelEvidence({ ...transferInput, after: ownPoolReuse }) +assertNativeKernelEvidence(ownPoolReuse, transferredOwners, transferMarker, transferPolicy, ownPoolEvidence) +const emptyForeign = Object.fromEntries(Object.keys(transferAfter).map(resource => [resource, []])) as NativeCatalog +const actualExclusions = [...transferredOwners.map(({ resource, id }) => ({ resource, id })), ...transferredEvidence.filter(item => item.present !== false).map(({ resource, id }) => ({ resource, id }))] +assert.deepEqual(vrfCleanupForeignHashes(transferAfter, actualExclusions), vrfCleanupForeignHashes(emptyForeign)) +const untrackedRoute = structuredClone(transferAfter) +untrackedRoute.ipv6_route.push({ ...oldBllRoute.expected, ".id": "*F4", gateway: `${transferMarker}-b@${transferMarker}-r`, "routing-table": `${transferMarker}-r`, "opaque-collateral": "new" }) +// Scope proof classifies only pinned owned rows; the mandatory independent +// whole-catalog hash layer must reject every unclassified addition. +assertNativeKernelEvidence(untrackedRoute, transferredOwners, transferMarker, transferPolicy, transferredEvidence) +assert.notDeepEqual(vrfCleanupForeignHashes(untrackedRoute, actualExclusions), vrfCleanupForeignHashes(emptyForeign)) +console.log(`VRF transfer paired absence and all34 foreign-hash layer models passed (${checks} refusal cases total)`) diff --git a/backend/src/test/chr-vrf-companion-proof.ts b/backend/src/test/chr-vrf-companion-proof.ts new file mode 100644 index 00000000..5a787256 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-proof.ts @@ -0,0 +1,517 @@ +import { isIP } from "node:net" +import { isDeepStrictEqual } from "node:util" +import { isNativeTelemetryField, stripNativeTelemetry } from "./chr-vrf-companion-telemetry.js" + +export type NativeRow = Record +export type NativeCatalog = Record +export interface NativeOwnedObject { resource: string; id: string; marker: string; expected: NativeRow } +export interface NativeKernelPolicy { allowedAddressVrfs?: readonly string[] } +export interface LinkLocalCompanion { kind: "kernel-link-local-address"; id: string; interface: string; address: string; prefix: string; row: NativeRow } +export interface AddressClosure { staticAddresses: NativeOwnedObject[]; linkLocalCompanions: LinkLocalCompanion[] } +export interface ConnectedRouteAttribution { kind: "kernel-connected-route"; id: string; addressId: string; interface: string; prefix: string; table: string; addressKind: "acknowledged-static" | "kernel-link-local" } +export interface VrfTableCompanion { kind: "kernel-vrf-table"; id: string; vrfId: string; name: string } +export interface BridgeInventoryIdentity { kind: "accepted-bridge-inventory"; id: string; name: string } +export interface ConnectedCapabilityAdmission { status: "proven-baseline-schema" | "unproven-zero-mutations"; ipv4: string[]; ipv6: string[] } +export interface NativeKernelEvidence { + resource: string; id: string; key: string; kind: "bridge" | "virtual" | "table" | "link-local" | "route"; parentId: string; expected: NativeRow; + /** An absent table retains its fixed ID and last proven raw configuration. */ + present?: false; + /** Sole known LL absence: the newly acquired bridge at a verified transfer + * ACK. The originating VRF identity survives its later acknowledged delete. */ + absenceReason?: "transfer-ack"; absenceVrfId?: string; + /** Proven on/off raw snapshots; never refreshed by read-only checkpoints. */ + activeExpected?: NativeRow; disabledExpected?: NativeRow; +} +export interface NativeKernelTransition { resource: string; method: "PUT" | "PATCH" | "DELETE"; id: string; fields: NativeRow } +export const VRF_NATIVE_LIFECYCLE_PHASES = ["vrf-create", "vrf-rename", "vrf-disable", "vrf-reenable", "vrf-membership-transfer-a-to-b", "vrf-delete-addresses-remain"] as const +export function summarizeNativeVrfLifecycle(journal: readonly NativeRow[]): { fullLifecycleSupported: boolean; testedPhases: Array<{ phase: string; outcome: string; accepted: boolean }> } { + const testedPhases = VRF_NATIVE_LIFECYCLE_PHASES.map(phase => { + const entries = journal.filter(item => item.phase === phase), item = entries[0] + return { phase, outcome: entries.length === 1 ? String(item.outcome) : entries.length ? "duplicate-attempt" : "not-tested", accepted: entries.length === 1 && item.outcome === "acknowledged" && item.ownedIdentityConfirmed === true && item.readbackVerified === true && item.requestedChange === true } + }) + return { fullLifecycleSupported: testedPhases.every(item => item.accepted), testedPhases } +} + +const booleanFields = new Set(["disabled", "advertise", "dynamic", "connect", "connected", "fib", "auto-mac", "vlan-filtering"]) +const idValue = (value: unknown): value is string => typeof value === "string" && /^\*[0-9a-f]+$/i.test(value) +const trueValue = (value: unknown) => [true, "true", "yes"].includes(value as string | boolean) +const falseValue = (value: unknown) => [false, "false", "no"].includes(value as string | boolean) +function comparable(row: NativeRow, omit: ReadonlySet = new Set()): NativeRow { + return Object.fromEntries(Object.entries(stripNativeTelemetry(row)).filter(([key]) => !omit.has(key)).map(([key, value]) => [key, booleanFields.has(key) && [true, "true", "yes", false, "false", "no"].includes(value as string | boolean) ? trueValue(value) : value])) +} +function requireProof(condition: unknown, reason: string): asserts condition { if (!condition) throw Error(`VRF fixture proof refused: ${reason}`) } +function indexed(rows: readonly NativeRow[]): Map { + const result = new Map() + for (const row of rows) { + requireProof(row && typeof row === "object" && !Array.isArray(row) && idValue(row[".id"]), "missing native ID") + const id = row[".id"] + requireProof(!result.has(id), "duplicate native ID") + result.set(id, row) + } + return result +} +function conserved(old: NativeRow, current: NativeRow, omit = new Set()): boolean { return isDeepStrictEqual(comparable(old, omit), comparable(current, omit)) } +function requested(row: NativeRow, fields: NativeRow): boolean { + return Object.entries(fields).every(([key, value]) => key !== ".id" && !isNativeTelemetryField(key) && (!booleanFields.has(key) || trueValue(value) || falseValue(value)) && Object.hasOwn(row, key) && isDeepStrictEqual(comparable({ [key]: value }), comparable({ [key]: row[key] }))) +} +function assertAddressVrf(old: NativeRow, current: NativeRow, policy: NativeKernelPolicy): Set { + if (isDeepStrictEqual(old.vrf, current.vrf)) return new Set() + // The one permitted native companion is explicit and bounded. No arbitrary + // string, empty default, physical interface or missing replacement is inferred. + const allowed = policy.allowedAddressVrfs ?? [] + requireProof(typeof current.vrf === "string" && allowed.includes(current.vrf), "unapproved address VRF companion") + requireProof(old.vrf === undefined || typeof old.vrf === "string" && allowed.includes(old.vrf), "unapproved prior address VRF") + return new Set(["vrf"]) +} +function bridgeNames(bridges: readonly NativeOwnedObject[]): string[] { + const names: string[] = [], ids = new Set() + for (const bridge of bridges) { + requireProof(bridge.resource === "bridge" && idValue(bridge.id) && bridge.expected[".id"] === bridge.id && bridge.expected.comment === bridge.marker && typeof bridge.expected.name === "string" && bridge.expected.name !== "all" && bridge.expected.name !== "none", "invalid accepted bridge identity") + requireProof(!ids.has(bridge.id) && !names.includes(bridge.expected.name), "ambiguous accepted bridge") + ids.add(bridge.id); names.push(bridge.expected.name) + } + return names +} + +/** Register ONLY an exact ACK-assigned new ID and a conserved previous catalog. */ +export function acceptNativeCreate(resource: string, marker: string, fields: NativeRow, ack: unknown, before: readonly NativeRow[], after: readonly NativeRow[]): NativeOwnedObject { + const prior = indexed(before), current = indexed(after) + const id = ack && typeof ack === "object" && !Array.isArray(ack) ? (ack as NativeRow)[".id"] : undefined + requireProof(idValue(id) && !prior.has(id) && current.size === prior.size + 1, "CREATE identity/cardinality") + const row = current.get(id) + requireProof(row && row.comment === marker && fields.comment === marker && requested(row, fields), "CREATE ownership/requested values") + for (const [otherId, previous] of prior) requireProof(current.has(otherId) && conserved(previous, current.get(otherId)!), "CREATE collateral row change") + return { resource, marker, id, expected: structuredClone(row) } +} + +/** Full accepted-ID/config conservation, including opaque native fields. */ +export function assertAcceptedOwnership(catalog: NativeCatalog, accepted: readonly NativeOwnedObject[], policy: NativeKernelPolicy = {}): void { + const keys = new Set() + for (const item of accepted) { + const key = `${item.resource}/${item.id}` + requireProof(!keys.has(key) && idValue(item.id) && item.expected[".id"] === item.id && item.expected.comment === item.marker, "invalid accepted identity") + keys.add(key) + const rows = indexed(catalog[item.resource] ?? []), current = rows.get(item.id) + requireProof(current && current.comment === item.marker, "owned ID absent or ownership changed") + const omit = ["ipv4_address", "ipv6_address"].includes(item.resource) ? assertAddressVrf(item.expected, current, policy) : new Set() + requireProof(conserved(item.expected, current, omit), "accepted configuration drift") + } +} + +export function assertVrfMembersOwned(row: NativeRow, bridges: readonly NativeOwnedObject[]): void { + const names = bridgeNames(bridges) + requireProof(typeof row.interfaces === "string", "unknown VRF members") + const members = row.interfaces === "" ? [] : row.interfaces.split(",") + requireProof(new Set(members).size === members.length && members.every(name => names.includes(name)), "foreign/physical/ambiguous VRF member") +} + +/** No blanket adoption: all non-requested native config must stay conserved. */ +export function acceptNativePatch(owned: NativeOwnedObject, fields: NativeRow, before: readonly NativeRow[], after: readonly NativeRow[], bridges: readonly NativeOwnedObject[] = [], policy: NativeKernelPolicy = {}): NativeOwnedObject { + requireProof(Object.keys(fields).length > 0 && !Object.hasOwn(fields, ".id") && (!Object.hasOwn(fields, "comment") || fields.comment === owned.marker), "invalid PATCH fields") + assertAcceptedOwnership({ [owned.resource]: [...before] }, [owned], policy) + const prior = indexed(before), current = indexed(after), old = prior.get(owned.id), row = current.get(owned.id) + requireProof(old && row && current.size === prior.size && row.comment === owned.marker && requested(row, fields), "PATCH identity/cardinality/requested values") + requireProof(conserved(old, row, new Set(Object.keys(fields))), "PATCH unsupplied native configuration changed") + for (const [id, previous] of prior) if (id !== owned.id) requireProof(current.has(id) && conserved(previous, current.get(id)!), "PATCH collateral catalog row") + if (owned.resource === "vrf") { assertVrfMembersOwned(old, bridges); assertVrfMembersOwned(row, bridges) } + return { ...owned, expected: structuredClone(row) } +} + +export function assertNativeDelete(owned: NativeOwnedObject, before: readonly NativeRow[], after: readonly NativeRow[], policy: NativeKernelPolicy = {}): void { + assertAcceptedOwnership({ [owned.resource]: [...before] }, [owned], policy) + const prior = indexed(before), current = indexed(after) + requireProof(prior.has(owned.id) && !current.has(owned.id) && current.size === prior.size - 1, "DELETE exact target/cardinality") + for (const [id, previous] of prior) if (id !== owned.id) requireProof(current.has(id) && conserved(previous, current.get(id)!), "DELETE collateral catalog row") +} + +function ipv6Words(host: string): number[] | undefined { + if (isIP(host) !== 6 || host.includes(".")) return undefined + const parts = host.split("::") + const left = parts[0] ? parts[0].split(":").map(value => Number.parseInt(value, 16)) : [] + if (parts.length === 1) return left.length === 8 ? left : undefined + const right = parts[1] ? parts[1].split(":").map(value => Number.parseInt(value, 16)) : [] + return parts.length === 2 && left.length + right.length < 8 ? [...left, ...Array(8 - left.length - right.length).fill(0), ...right] : undefined +} +function prefixKey(prefix: string): string | undefined { + const [host, length, extra] = prefix.split("/") + if (extra !== undefined) return undefined + if (isIP(host) === 4 && length === "32") return `4:${host}/32` + const words = ipv6Words(host) + if (!words || !["64", "128"].includes(length) || length === "64" && words.slice(4).some(word => word !== 0)) return undefined + return `6:${words.map(word => word.toString(16)).join(":")}/${length}` +} +function linkLocalAddress(address: unknown): address is string { + if (typeof address !== "string") return false + const [host, length, extra] = address.split("/") + const words = ipv6Words(host) + return extra === undefined && length === "64" && words !== undefined && (words[0] & 0xffc0) === 0xfe80 +} + +/** Static ACK IDs and auto link-local companions are distinct. Companions are + * evidence only and must NEVER be passed to DELETE as accepted owned objects. */ +export function assertOwnedAddressClosure(catalog: NativeCatalog, accepted: readonly NativeOwnedObject[], bridges: readonly NativeOwnedObject[], marker: string, policy: NativeKernelPolicy = {}): AddressClosure { + const names = bridgeNames(bridges), staticAddresses = accepted.filter(item => ["ipv4_address", "ipv6_address"].includes(item.resource)) + requireProof(staticAddresses.every(item => item.marker === marker && names.includes(String(item.expected.interface))), "static address ownership/context") + assertAcceptedOwnership(catalog, staticAddresses, policy) + const linkLocalCompanions: LinkLocalCompanion[] = [] + for (const resource of ["ipv4_address", "ipv6_address"]) { + const rows = indexed(catalog[resource] ?? []), expected = new Set(staticAddresses.filter(item => item.resource === resource).map(item => item.id)) + const companionInterfaces = new Set() + for (const [id, row] of rows) { + if (expected.has(id)) { requireProof(row.dynamic === undefined || falseValue(row.dynamic), "ACK address dynamic flag unknown/true"); continue } + if (!names.includes(String(row.interface)) && row.comment !== marker) continue + requireProof(resource === "ipv6_address" && names.includes(String(row.interface)) && trueValue(row.dynamic) && linkLocalAddress(row.address) && (row.comment === undefined || row.comment === "" || row.comment === marker), "untracked address ID on fixture bridge") + requireProof(!companionInterfaces.has(String(row.interface)), "ambiguous link-local companion") + if (row["link-local"] !== undefined) requireProof(trueValue(row["link-local"]), "contradictory link-local flag") + if (row.vrf !== undefined) requireProof(typeof row.vrf === "string" && (policy.allowedAddressVrfs ?? []).includes(row.vrf), "unapproved link-local VRF") + companionInterfaces.add(String(row.interface)) + const words = ipv6Words(row.address.split("/")[0])! + const prefix = `${words.slice(0, 4).map(word => word.toString(16)).join(":")}::/64` + linkLocalCompanions.push({ kind: "kernel-link-local-address", id, interface: String(row.interface), address: row.address, prefix, row: structuredClone(row) }) + } + } + return { staticAddresses, linkLocalCompanions } +} + +/** Generic interface inventory is attributed by the ACK bridge ID AND native + * type/name/comment, never by a planned name that may have been reused. Native + * variants with a different inventory ID remain refused until separately proven. */ +export function deriveOwnedBridgeInventory(row: NativeRow, bridges: readonly NativeOwnedObject[]): BridgeInventoryIdentity | undefined { + bridgeNames(bridges) + const matched = bridges.filter(bridge => row[".id"] === bridge.id && row.name === bridge.expected.name && row.comment === bridge.marker && row.type === "bridge") + if (matched.length !== 1) return undefined + return { kind: "accepted-bridge-inventory", id: matched[0].id, name: String(matched[0].expected.name) } +} + +/** A dynamic table is a typed companion of the CURRENT ACK-owned VRF. Old + * planned names, static tables and unknown/contradictory vrf flags are foreign. */ +export function deriveVrfTableCompanion(row: NativeRow, currentVrf: NativeOwnedObject): VrfTableCompanion | undefined { + if (currentVrf.resource !== "vrf" || !idValue(currentVrf.id) || currentVrf.expected[".id"] !== currentVrf.id || currentVrf.expected.comment !== currentVrf.marker || typeof currentVrf.expected.name !== "string") return undefined + if (!idValue(row[".id"]) || !trueValue(row.dynamic) || row.name !== currentVrf.expected.name) return undefined + if (row.vrf === undefined) { + if (row[".id"] !== currentVrf.id || row.fib !== "") return undefined + } else if (row.vrf !== currentVrf.id && !(row[".id"] === currentVrf.id && (trueValue(row.vrf) || row.vrf === currentVrf.expected.name))) return undefined + return { kind: "kernel-vrf-table", id: row[".id"], vrfId: currentVrf.id, name: currentVrf.expected.name } +} + +/** Read-only schema admission before creating a bridge (which may itself emit + * LL routes). This is evidence of readable native flags, not ownership of the + * foreign baseline routes or proof of forwarding/native mutation semantics. */ +export function assessNativeConnectedCapabilities(catalog: NativeCatalog): ConnectedCapabilityAdmission { + const interfaces = (catalog.interface_inventory ?? []).filter(row => idValue(row[".id"]) && typeof row.name === "string").map(row => row.name) + const result: ConnectedCapabilityAdmission = { status: "unproven-zero-mutations", ipv4: [], ipv6: [] } + for (const [resource, family, target] of [["ipv4_route", 4, result.ipv4], ["ipv6_route", 6, result.ipv6]] as const) { + for (const row of catalog[resource] ?? []) { + if (!idValue(row[".id"]) || !trueValue(row.dynamic) || ![row.connect, row.connected].some(trueValue) && row.protocol !== "connect") continue + if (["connect", "connected"].some(key => row[key] !== undefined && !trueValue(row[key])) || row.protocol !== undefined && row.protocol !== "connect") continue + if (typeof row["routing-table"] !== "string" || !row["routing-table"] || typeof row["dst-address"] !== "string") continue + const [host, length, extra] = row["dst-address"].split("/") + if (extra !== undefined || isIP(host) !== family || !/^\d+$/.test(length ?? "") || Number(length) < 0 || Number(length) > (family === 4 ? 32 : 128)) continue + const contexts = [row.gateway, row["immediate-gw"], row.interface].filter(value => value !== undefined && value !== "") + if (!contexts.length || !contexts.every(value => value === contexts[0]) || !interfaces.includes(contexts[0])) continue + if (target.length < 20) target.push(row[".id"]) + } + } + if (result.ipv4.length && result.ipv6.length) result.status = "proven-baseline-schema" + return result +} + +/** Strict observed attribution, never a deletable ownership record. A route + * must have authoritative native connected flags, exact source prefix and + * exact bridge context. Scope/distance or a gateway substring prove nothing. */ +export function deriveOwnedConnectedRoute(row: NativeRow, context: { addresses: AddressClosure; bridges: readonly NativeOwnedObject[]; allowedTables: readonly string[]; vrfs?: readonly NativeOwnedObject[] }): ConnectedRouteAttribution | undefined { + const names = bridgeNames(context.bridges) + if (!idValue(row[".id"]) || !trueValue(row.dynamic) || ![row.connect, row.connected].some(trueValue) && row.protocol !== "connect") return undefined + if (["connect", "connected"].some(key => row[key] !== undefined && !trueValue(row[key])) || row.protocol !== undefined && row.protocol !== "connect") return undefined + const table = row["routing-table"] + if (typeof table !== "string" || !context.allowedTables.includes(table) || typeof row["dst-address"] !== "string") return undefined + const matches: Array<{ id: string; name: string; prefix: string; kind: ConnectedRouteAttribution["addressKind"] }> = [] + for (const address of context.addresses.staticAddresses) { + const prefix = address.expected.address, name = address.expected.interface + if (typeof prefix !== "string" || typeof name !== "string" || !names.includes(name)) continue + const [host, length, extra] = prefix.split("/") + if (extra === undefined && (isIP(host) === 4 && length === "32" || isIP(host) === 6 && length === "128")) matches.push({ id: address.id, name, prefix, kind: "acknowledged-static" }) + } + for (const address of context.addresses.linkLocalCompanions) matches.push({ id: address.id, name: address.interface, prefix: address.prefix, kind: "kernel-link-local" }) + const destination = prefixKey(row["dst-address"]) + if (!destination) return undefined + const matching = matches.filter(address => destination === prefixKey(address.prefix) && names.includes(address.name) && [row.gateway, row["immediate-gw"], row.interface].some(value => value === address.name)) + // Reject contradictory contexts too: an otherwise correct immediate-gw may + // not hide a second physical interface or an arbitrary gateway string. + if (matching.length !== 1) return undefined + const match = matching[0] + const vrf = context.vrfs?.find(item => item.resource === "vrf" && item.expected[".id"] === item.id && item.expected.comment === item.marker && item.expected.name === table && typeof item.expected.interfaces === "string" && item.expected.interfaces.split(",").includes(match.name)) + if (context.vrfs && table !== "main" && !vrf) return undefined + if (context.vrfs && table === "main" && context.vrfs.some(item => typeof item.expected.interfaces === "string" && item.expected.interfaces.split(",").includes(match.name))) return undefined + const qualified = vrf ? `${match.name}@${String(vrf.expected.name)}` : undefined + if (context.vrfs && vrf && row.gateway !== qualified) return undefined + if (row.gateway !== match.name && row.gateway !== qualified || row.gateway === undefined) return undefined + if (row["immediate-gw"] !== undefined && row["immediate-gw"] !== match.name || row.interface !== undefined && row.interface !== "" && row.interface !== match.name) return undefined + if (row.gateway === qualified && row["immediate-gw"] !== match.name) return undefined + // Lifecycle callers supply ACK VRFs and validate the original wire row. + // Legacy cleanup normalizes gateway itself and separately validates its + // original local-address; do not reinterpret that normalized adapter here. + if (context.vrfs !== undefined && row["local-address"] !== undefined) { + const source = match.kind === "kernel-link-local" ? context.addresses.linkLocalCompanions.find(item => item.id === match.id)?.address : context.addresses.staticAddresses.find(item => item.id === match.id && item.expected.interface === match.name && prefixKey(String(item.expected.address)) === destination)?.expected.address + if (typeof source !== "string" || row["local-address"] !== `${source.split("/")[0]}%${match.name}${row.gateway === qualified ? `@${String(vrf!.expected.name)}` : ""}`) return undefined + } + return { kind: "kernel-connected-route", id: row[".id"], addressId: match.id, interface: match.name, prefix: match.prefix, table, addressKind: match.kind } +} + +function assertEui64(row: NativeRow, bridge: NativeRow): void { + requireProof(trueValue(row.dynamic) && trueValue(row["auto-link-local"]) && trueValue(row["link-local"]) && row.vrf === "main", "link-local native flags/context") + const mac = bridge["mac-address"] + requireProof(typeof mac === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(mac), "missing bridge MAC proof") + const octets = mac.split(":").map(value => Number.parseInt(value, 16)) + const eui = [octets[0] ^ 2, octets[1], octets[2], 255, 254, octets[3], octets[4], octets[5]] + const words = typeof row.address === "string" ? ipv6Words(row.address.split("/")[0]) : undefined + requireProof(words && words.slice(0, 4).every((value, index) => value === (index === 0 ? 0xfe80 : 0)) && words.slice(4).every((value, index) => value === eui[index * 2] * 256 + eui[index * 2 + 1]), "link-local MAC/address binding") +} + +/** Classification is not adoption. Only advanceNativeKernelEvidence, after an + * exact primary ACK, may pin these full native rows for future checkpoints. */ +function kernelCandidates(catalog: NativeCatalog, accepted: readonly NativeOwnedObject[], marker: string, policy: NativeKernelPolicy, absences: readonly NativeKernelEvidence[] = []): NativeKernelEvidence[] { + requireProof(accepted.every(item => item.marker === marker), "foreign accepted fixture marker") + assertAcceptedOwnership(catalog, accepted, policy) + const bridges = accepted.filter(item => item.resource === "bridge"), ownedVrfs = accepted.filter(item => item.resource === "vrf") + requireProof(ownedVrfs.every(vrf => trueValue(vrf.expected.disabled) || falseValue(vrf.expected.disabled)), "unknown VRF enabled state") + const vrfs = ownedVrfs.filter(vrf => falseValue(vrf.expected.disabled)) + const addresses = assertOwnedAddressClosure(catalog, accepted, bridges, marker, policy) + for (const vrf of ownedVrfs) assertVrfMembersOwned(vrf.expected, bridges) + for (const address of addresses.staticAddresses) { + const prefix = prefixKey(String(address.expected.address)) + requireProof(prefix && (address.resource === "ipv4_address" ? prefix.startsWith("4:") && prefix.endsWith("/32") : prefix.startsWith("6:") && prefix.endsWith("/128")), "static address family/prefix shape") + const members = vrfs.filter(vrf => String(vrf.expected.interfaces).split(",").includes(String(address.expected.interface))) + requireProof(members.length <= 1, "ambiguous current static address VRF") + const row = indexed(catalog[address.resource] ?? []).get(address.id)! + // RouterOS 7.24.5 records the exact current VRF NAME, not any historical + // name/ID from the legacy bounded address policy. No shape inference. + requireProof(row.vrf === (members.length ? members[0].expected.name : "main"), "static address current member VRF binding") + } + const result: NativeKernelEvidence[] = [] + const add = (resource: string, row: NativeRow, kind: NativeKernelEvidence["kind"], parentId: string, key: string) => { + requireProof(idValue(row[".id"]), "kernel ID missing") + result.push({ resource, id: row[".id"], kind, parentId, key, expected: structuredClone(row) }) + } + for (const bridge of bridges) { + const rows = (catalog.interface_inventory ?? []).filter(row => deriveOwnedBridgeInventory(row, [bridge])) + requireProof(rows.length === 1, "bridge inventory cardinality") + add("interface_inventory", rows[0], "bridge", bridge.id, `bridge/${bridge.id}`) + } + for (const address of addresses.linkLocalCompanions) { + const bridge = bridges.find(item => item.expected.name === address.interface)! + const inventory = result.find(item => item.kind === "bridge" && item.parentId === bridge.id)!.expected + assertEui64(address.row, inventory) + add("ipv6_address", address.row, "link-local", bridge.id, `link-local/${bridge.id}`) + } + for (const vrf of ownedVrfs) { + assertVrfMembersOwned(vrf.expected, bridges) + const tables = (catalog.routing_table ?? []).filter(row => deriveVrfTableCompanion(row, vrf)) + if (falseValue(vrf.expected.disabled)) { + requireProof(tables.length === 1 && tables[0][".id"] === vrf.id, "VRF table cardinality/parent identity") + add("routing_table", tables[0], "table", vrf.id, `table/${vrf.id}`) + } else requireProof(tables.length === 0 && !(catalog.routing_table ?? []).some(row => row[".id"] === vrf.id), "disabled VRF table must be absent") + const virtual = (catalog.interface_inventory ?? []).filter(row => row.type === "vrf" && row.name === vrf.expected.name && row.vrf === "main" && (row.comment === undefined || row.comment === "") && !bridges.some(bridge => bridge.id === row[".id"])) + requireProof(virtual.length === 1, "VRF virtual inventory cardinality") + add("interface_inventory", virtual[0], "virtual", vrf.id, `virtual/${vrf.id}`) + } + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of catalog[resource] ?? []) { + const route = deriveOwnedConnectedRoute(row, { addresses, bridges, vrfs, allowedTables: ["main", ...vrfs.map(vrf => String(vrf.expected.name))] }) + if (!route) continue + requireProof(prefixKey(route.prefix)?.startsWith(resource === "ipv4_route" ? "4:" : "6:"), "connected route catalog family") + const bridge = bridges.find(item => item.expected.name === route.interface)! + add(resource, row, "route", bridge.id, `${resource}/${bridge.id}/${prefixKey(route.prefix)}`) + } + const missingLl = absences.filter(entry => entry.kind === "link-local") + requireProof(missingLl.every(entry => entry.present === false && entry.absenceReason === "transfer-ack" && idValue(entry.absenceVrfId) && entry.resource === "ipv6_address" && bridges.some(bridge => bridge.id === entry.parentId) && !addresses.linkLocalCompanions.some(row => row.interface === bridges.find(bridge => bridge.id === entry.parentId)!.expected.name)), "LL absence requires exact transfer provenance") + requireProof(new Set(missingLl.map(entry => entry.parentId)).size === missingLl.length && addresses.linkLocalCompanions.length === bridges.length - missingLl.length && result.filter(entry => entry.kind === "route").length === addresses.staticAddresses.length + addresses.linkLocalCompanions.length, "expected LL/connected route cardinality") + const keys = new Set(), ids = new Set() + for (const entry of result) { + requireProof(!keys.has(entry.key) && !ids.has(`${entry.resource}/${entry.id}`), "ambiguous kernel companion") + keys.add(entry.key); ids.add(`${entry.resource}/${entry.id}`) + } + return result +} + +/** Read-only checks never refresh/adopt native configuration, IDs or fields. */ +export function assertNativeKernelEvidence(catalog: NativeCatalog, accepted: readonly NativeOwnedObject[], marker: string, policy: NativeKernelPolicy, evidence: readonly NativeKernelEvidence[]): void { + const candidates = kernelCandidates(catalog, accepted, marker, policy, evidence.filter(entry => entry.present === false)) + requireProof(new Set(evidence.map(entry => entry.key)).size === evidence.length, "duplicate kernel checkpoint key") + const present = evidence.filter(entry => entry.present !== false) + requireProof(new Set(present.map(entry => `${entry.resource}/${entry.id}`)).size === present.length, "duplicate kernel checkpoint ID") + const fixed = evidence.filter(entry => !(entry.kind === "route" && entry.present === false)) + requireProof(new Set(fixed.map(entry => `${entry.resource}/${entry.id}`)).size === fixed.length, "duplicate fixed kernel checkpoint ID") + requireProof(candidates.length === present.length, "kernel checkpoint cardinality") + for (const entry of evidence.filter(entry => entry.present === false)) { + if (entry.kind === "table") { + const parent = accepted.find(vrf => vrf.resource === "vrf" && vrf.id === entry.parentId) + requireProof(entry.resource === "routing_table" && entry.id === entry.parentId && entry.expected[".id"] === entry.id && parent && trueValue(parent.expected.disabled) && deriveVrfTableCompanion(entry.expected, parent)?.id === entry.id && !indexed(catalog.routing_table ?? []).has(entry.id), "dormant table parent/absence binding") + continue + } + const bridge = accepted.find(item => item.resource === "bridge" && item.id === entry.parentId) + requireProof(bridge && entry.absenceReason === "transfer-ack" && idValue(entry.absenceVrfId) && entry.expected[".id"] === entry.id, "missing transfer absence provenance") + if (entry.kind === "link-local") { + requireProof(entry.resource === "ipv6_address" && entry.key === `link-local/${bridge.id}` && !indexed(catalog.ipv6_address ?? []).has(entry.id) && entry.expected.interface === bridge.expected.name, "LL tombstone identity") + assertEui64(entry.expected, candidates.find(item => item.kind === "bridge" && item.parentId === bridge.id)!.expected) + requireProof(evidence.some(item => item.present === false && item.kind === "route" && item.parentId === bridge.id && item.absenceVrfId === entry.absenceVrfId), "LL tombstone missing matching route") + } else { + requireProof(entry.kind === "route" && entry.resource === "ipv6_route" && entry.key === `ipv6_route/${bridge.id}/${prefixKey("fe80::/64")}` && prefixKey(String(entry.expected["dst-address"])) === prefixKey("fe80::/64") && entry.expected["immediate-gw"] === bridge.expected.name && trueValue(entry.expected.dynamic) && trueValue(entry.expected.connect) && evidence.some(item => item.present === false && item.kind === "link-local" && item.parentId === bridge.id && item.absenceVrfId === entry.absenceVrfId) && !candidates.some(item => item.key === entry.key), "route absence semantic binding") + requireProof(!indexed(catalog[entry.resource] ?? []).has(entry.id) || candidates.some(item => item.kind === "route" && item.resource === entry.resource && item.id === entry.id && item.key !== entry.key), "absent route ID acquired by foreign row") + } + } + for (const parent of accepted.filter(vrf => vrf.resource === "vrf" && trueValue(vrf.expected.disabled))) requireProof(evidence.some(entry => entry.kind === "table" && entry.parentId === parent.id && entry.present === false), "missing dormant table provenance") + for (const current of candidates) { + const old = present.find(entry => entry.key === current.key) + requireProof(old && old.resource === current.resource && old.id === current.id && old.kind === current.kind && old.parentId === current.parentId && conserved(old.expected, current.expected), "kernel checkpoint configuration/identity drift") + } +} + +function nativeTimestamp(value: unknown): value is string { + if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(value)) return false + const parts = value.match(/\d+/g)!.map(Number) + const date = new Date(Date.UTC(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5])) + return Number.isFinite(date.getTime()) && date.toISOString().slice(0, 19).replace("T", " ") === value +} + +function assertObservedVirtualDisable(old: NativeRow, current: NativeRow): void { + requireProof(old[".id"] === current[".id"] && old.type === "vrf" && current.type === "vrf" && old.name === current.name && old.vrf === "main" && current.vrf === "main", "virtual disable identity") + requireProof(falseValue(old.disabled) && trueValue(current.disabled) && old.status === "--LERU--O---------------" && current.status === "------------------------", "virtual disable status") + requireProof(typeof old["mac-address"] === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(old["mac-address"]) && old["mac-address"] !== "00:00:00:00:00:00" && current["mac-address"] === "00:00:00:00:00:00", "virtual disable MAC") + requireProof(old["link-downs"] === "0" && current["link-downs"] === "1" && old["last-link-down-time"] === undefined && nativeTimestamp(current["last-link-down-time"]) && nativeTimestamp(old["last-link-up-time"]) && current["last-link-down-time"] >= old["last-link-up-time"], "virtual disable chronology") + requireProof(typeof old.ifname === "string" && /^vrf\d+$/.test(old.ifname) && typeof old.ifindex === "string" && /^[1-9]\d*$/.test(old.ifindex) && old.flags === " UP RUNNING NOARP MASTER LOWER_UP (0x104c1)" && old["actual-mtu"] === "65536" && old.running === "true" && current.running === "false", "virtual disable on-shape") + requireProof(["ifname", "ifindex", "flags", "actual-mtu"].every(field => !Object.hasOwn(current, field)), "virtual disable removed fields") + requireProof(conserved(old, current, new Set(["disabled", "status", "mac-address", "link-downs", "last-link-down-time", "ifname", "ifindex", "flags"])), "virtual disable unknown configuration drift") +} + +/** Proposed admission only. A successful future native observation is still + * required before claiming RouterOS re-enable capability. */ +function assertProposedVirtualReenable(old: NativeKernelEvidence, current: NativeRow, after: NativeCatalog): void { + const active = old.activeExpected, disabled = old.expected + requireProof(active && old.disabledExpected && conserved(old.disabledExpected, disabled), "reenable missing on/off provenance") + requireProof(current[".id"] === old.id && current.type === "vrf" && current.name === disabled.name && current.vrf === "main" && falseValue(current.disabled) && current.status === active.status && current.flags === active.flags && current["actual-mtu"] === "65536" && current.running === "true", "reenable proposed on-shape") + requireProof(current.ifname === active.ifname && typeof current.ifindex === "string" && /^[1-9]\d*$/.test(current.ifindex) && (after.interface_inventory ?? []).filter(row => row.ifindex === current.ifindex).length === 1, "reenable generated interface index/name") + const mac = current["mac-address"] + requireProof(typeof mac === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(mac) && (Number.parseInt(mac.slice(0, 2), 16) & 3) === 2 && (after.interface_inventory ?? []).filter(row => typeof row["mac-address"] === "string" && String(row["mac-address"]).toLowerCase() === mac.toLowerCase()).length === 1, "reenable generated MAC") + requireProof(nativeTimestamp(current["last-link-up-time"]) && nativeTimestamp(disabled["last-link-down-time"]) && current["last-link-up-time"] >= disabled["last-link-down-time"], "reenable generated link-up chronology") + requireProof(conserved(disabled, current, new Set(["disabled", "status", "mac-address", "ifname", "ifindex", "flags", "last-link-up-time"])), "reenable unknown configuration drift") +} + +/** A bounded transition following a proven ACK. Unknown fields are conserved; + * explicit kernel identity/context transforms are not a blanket dynamic skip. */ +export function advanceNativeKernelEvidence(input: { before: NativeCatalog; after: NativeCatalog; beforeAccepted: readonly NativeOwnedObject[]; afterAccepted: readonly NativeOwnedObject[]; marker: string; policy: NativeKernelPolicy; previous: readonly NativeKernelEvidence[]; transition: NativeKernelTransition }): NativeKernelEvidence[] { + const { before, after, beforeAccepted, afterAccepted, marker, policy, previous, transition } = input + assertNativeKernelEvidence(before, beforeAccepted, marker, policy, previous) + const isVrf = transition.resource === "vrf" + const oldVrf = beforeAccepted.find(item => item.resource === "vrf" && item.id === transition.id) + const newVrf = afterAccepted.find(item => item.resource === "vrf" && item.id === transition.id) + const disable = isVrf && transition.method === "PATCH" && oldVrf && newVrf && falseValue(oldVrf.expected.disabled) && trueValue(newVrf.expected.disabled) + const reenable = isVrf && transition.method === "PATCH" && oldVrf && newVrf && trueValue(oldVrf.expected.disabled) && falseValue(newVrf.expected.disabled) + const vrfDelete = isVrf && transition.method === "DELETE" && oldVrf && !newVrf + const transfer = isVrf && transition.method === "PATCH" && oldVrf && newVrf && falseValue(oldVrf.expected.disabled) && falseValue(newVrf.expected.disabled) && Object.keys(transition.fields).length === 1 && Object.hasOwn(transition.fields, "interfaces") && requested(newVrf.expected, transition.fields) && typeof oldVrf.expected.interfaces === "string" && typeof newVrf.expected.interfaces === "string" && oldVrf.expected.interfaces !== newVrf.expected.interfaces && !oldVrf.expected.interfaces.includes(",") && !newVrf.expected.interfaces.includes(",") + if (disable || reenable) requireProof(Object.keys(transition.fields).length === 1 && Object.hasOwn(transition.fields, "disabled") && requested(newVrf!.expected, transition.fields), "VRF enabled transition must be exact ACK-specific PATCH") + const hints = previous.filter(entry => entry.present === false && ["link-local", "route"].includes(entry.kind) && afterAccepted.some(bridge => bridge.resource === "bridge" && bridge.id === entry.parentId)).filter(entry => { + const bridge = afterAccepted.find(item => item.resource === "bridge" && item.id === entry.parentId)! + return !(vrfDelete && entry.absenceVrfId === transition.id && (after.ipv6_address ?? []).some(row => row.interface === bridge.expected.name && trueValue(row.dynamic))) + }).map(entry => structuredClone(entry)) + if (transfer) { + const bridge = afterAccepted.find(item => item.resource === "bridge" && item.expected.name === newVrf!.expected.interfaces) + const priorBridge = beforeAccepted.find(item => item.resource === "bridge" && item.expected.name === oldVrf!.expected.interfaces) + requireProof(bridge && priorBridge && bridge.id !== priorBridge.id, "transfer must bind two distinct ACK bridges") + if (!(after.ipv6_address ?? []).some(row => row.interface === bridge.expected.name && trueValue(row.dynamic))) { + const ll = previous.filter(entry => entry.kind === "link-local" && entry.parentId === bridge.id && entry.present !== false) + const route = previous.filter(entry => entry.kind === "route" && entry.resource === "ipv6_route" && entry.parentId === bridge.id && entry.present !== false && prefixKey(String(entry.expected["dst-address"])) === prefixKey("fe80::/64")) + requireProof(ll.length === 1 && route.length === 1 && !indexed(after.ipv6_address ?? []).has(ll[0].id), "transfer LL absence requires exact live source pair") + for (const entry of [...ll, ...route]) hints.push({ ...structuredClone(entry), present: false, absenceReason: "transfer-ack", absenceVrfId: transition.id }) + } + } + const next = kernelCandidates(after, afterAccepted, marker, policy, hints) + next.push(...hints) + for (const vrf of afterAccepted.filter(item => item.resource === "vrf" && trueValue(item.expected.disabled))) { + const old = previous.find(entry => entry.kind === "table" && entry.parentId === vrf.id) + requireProof(old && (old.present === false || disable && vrf.id === transition.id), "disabled table lacks proven active predecessor") + next.push({ ...structuredClone(old), present: false }) + } + for (const current of next) { + const old = previous.find(entry => entry.key === current.key) + if (current.present === false) { + requireProof(old && old.id === current.id && old.parentId === current.parentId && conserved(old.expected, current.expected), "absent companion transition drift") + continue + } + if (!old) { + requireProof(!indexed(before[current.resource] ?? []).has(current.id), "new kernel companion reused preexisting ID") + const newBridge = afterAccepted.find(item => item.resource === "bridge" && item.id === current.parentId && !beforeAccepted.some(old => old.resource === "bridge" && old.id === item.id)) + const newAddress = afterAccepted.find(item => item.resource === transition.resource && ["ipv4_address", "ipv6_address"].includes(item.resource) && item.id === transition.id && current.key.endsWith(`/${prefixKey(String(item.expected.address))}`) && !beforeAccepted.some(old => old.resource === item.resource && old.id === item.id)) + requireProof(current.kind === "bridge" && Boolean(newBridge) || current.kind === "link-local" && Boolean(newBridge) || current.kind === "route" && Boolean(newBridge || newAddress) || ["table", "virtual"].includes(current.kind) && isVrf && transition.method === "PUT" && current.parentId === transition.id, "kernel companion unrelated to ACK") + if (current.kind === "virtual") current.activeExpected = structuredClone(current.expected) + continue + } + if (current.kind === "virtual" && current.parentId === transition.id && disable) { + requireProof(old.id === current.id, "virtual disable ID changed") + assertObservedVirtualDisable(old.expected, current.expected) + current.activeExpected = structuredClone(old.expected) + current.disabledExpected = structuredClone(current.expected) + continue + } + if (current.kind === "virtual" && current.parentId === transition.id && reenable) { + assertProposedVirtualReenable(old, current.expected, after) + current.activeExpected = structuredClone(current.expected) + current.disabledExpected = structuredClone(old.expected) + continue + } + if (old.present === false) requireProof(current.kind === "table" && current.parentId === transition.id && reenable && old.id === current.id && !indexed(before[current.resource] ?? []).has(current.id) || ["link-local", "route"].includes(current.kind) && vrfDelete && old.absenceVrfId === transition.id, "absent companion revival outside exact ACK") + if (old.activeExpected) current.activeExpected = structuredClone(old.activeExpected) + if (old.disabledExpected) current.disabledExpected = structuredClone(old.disabledExpected) + const omit = new Set() + const bridge = afterAccepted.find(item => item.resource === "bridge" && item.id === current.parentId) + const oldContext = bridge && oldVrf && falseValue(oldVrf.expected.disabled) && typeof oldVrf.expected.interfaces === "string" && oldVrf.expected.interfaces.split(",").includes(String(bridge.expected.name)) ? oldVrf.expected.name : "main" + const newContext = bridge && newVrf && falseValue(newVrf.expected.disabled) && typeof newVrf.expected.interfaces === "string" && newVrf.expected.interfaces.split(",").includes(String(bridge.expected.name)) ? newVrf.expected.name : "main" + const memberTransition = isVrf && bridge && oldContext !== newContext + const routeIdTransition = current.kind === "route" && (memberTransition || transfer || vrfDelete) + const llRevival = current.kind === "link-local" && old.present === false && vrfDelete && old.absenceVrfId === transition.id + if (memberTransition && ["route", "link-local"].includes(current.kind) || routeIdTransition || llRevival) { + omit.add(".id") + if (current.kind === "route" && memberTransition) for (const field of ["gateway", "immediate-gw", "routing-table", "local-address"]) omit.add(field) + else if (current.kind === "link-local") omit.add("vrf") + if (old.id !== current.id) { + const ownRouteIdBefore = previous.some(entry => entry.present !== false && entry.kind === "route" && entry.resource === current.resource && entry.id === current.id) + const ownRouteIdAfter = next.some(entry => entry.present !== false && entry.kind === "route" && entry.resource === old.resource && entry.id === old.id) + requireProof((!indexed(after[old.resource] ?? []).has(old.id) || current.kind === "route" && ownRouteIdAfter) && (!indexed(before[current.resource] ?? []).has(current.id) || current.kind === "route" && ownRouteIdBefore), "kernel successor acquired foreign ID") + } + } else requireProof(old.id === current.id, "kernel ID changed outside VRF transition") + if (memberTransition && current.kind === "bridge") { + const oldRow = old.expected, row = current.expected + const oldMember = oldContext !== "main", newMember = newContext !== "main" + requireProof(oldRow.vrf === (oldMember ? oldVrf!.expected.name : "main") && row.vrf === (newMember ? newVrf!.expected.name : "main"), "bridge VRF transition") + omit.add("vrf") + if (oldRow.status !== row.status) { + requireProof(typeof oldRow.status === "string" && typeof row.status === "string" && oldRow.status.length === 24 && row.status.length === 24 && oldRow.status.slice(0, 20) === row.status.slice(0, 20) && oldRow.status.slice(21) === row.status.slice(21) && oldRow.status[20] === (oldMember ? "V" : "-") && row.status[20] === (newMember ? "V" : "-"), "unproven bridge status transition") + omit.add("status") + } + } + if (current.kind === "bridge" && transition.resource === "ipv4_address" && ["PUT", "DELETE"].includes(transition.method)) { + const adding = transition.method === "PUT" + const address = (adding ? afterAccepted : beforeAccepted).find(item => item.resource === "ipv4_address" && item.id === transition.id) + const boundary = address?.expected.interface === bridge?.expected.name && !(adding ? beforeAccepted : afterAccepted).some(item => item.resource === "ipv4_address" && item.expected.interface === bridge?.expected.name) + if (boundary && old.expected.status !== current.expected.status) { + // Exact observed first-address CREATE and final-address DELETE forms, + // not a general interpretation/ignore rule for native status flags. + requireProof(old.expected.status === (adding ? "--LERU--O---------------" : "--LERUI-O---------------") && current.expected.status === (adding ? "--LERUI-O---------------" : "--LERU--O---------------") && current.expected.vrf === "main", "unproven IPv4 inventory status transition") + omit.add("status") + } + } + if (isVrf && current.parentId === transition.id && ["table", "virtual"].includes(current.kind) && transition.method === "PATCH") { + if (Object.hasOwn(transition.fields, "name")) omit.add("name") + // Other native fields, including flags/status/ifname, remain conserved. + } + requireProof(conserved(old.expected, current.expected, omit), "kernel companion opaque configuration drift") + } + for (const old of previous) if (!next.some(current => current.key === old.key)) { + const parentGone = !afterAccepted.some(item => item.resource === "bridge" && item.id === old.parentId) + const vrfGone = isVrf && transition.method === "DELETE" && old.parentId === transition.id && ["table", "virtual"].includes(old.kind) + const addressGone = old.kind === "route" && ["ipv4_address", "ipv6_address"].includes(transition.resource) && transition.method === "DELETE" && beforeAccepted.some(item => item.resource === transition.resource && item.id === transition.id && old.key.endsWith(`/${prefixKey(String(item.expected.address))}`)) + requireProof(parentGone && ["bridge", "route", "link-local"].includes(old.kind) || vrfGone || addressGone, "kernel companion disappeared without parent DELETE") + requireProof(!indexed(after[old.resource] ?? []).has(old.id) || old.kind === "route" && next.some(entry => entry.present !== false && entry.kind === "route" && entry.resource === old.resource && entry.id === old.id), "removed kernel ID retained/reused") + } + assertNativeKernelEvidence(after, afterAccepted, marker, policy, next) + return next +} diff --git a/backend/src/test/chr-vrf-companion-telemetry.test.ts b/backend/src/test/chr-vrf-companion-telemetry.test.ts new file mode 100644 index 00000000..bce393d0 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-telemetry.test.ts @@ -0,0 +1,32 @@ +import assert from "node:assert/strict" +import { NATIVE_TELEMETRY_FIELDS, isNativeTelemetryField, stripNativeTelemetry } from "./chr-vrf-companion-telemetry.js" + +const base = { ".id": "*A", name: "owned", comment: "fixture", disabled: "false", opaque: { mode: "preserve", nested: [1, 2] } } +assert.ok(Object.isFrozen(NATIVE_TELEMETRY_FIELDS)) +assert.equal(new Set(NATIVE_TELEMETRY_FIELDS).size, NATIVE_TELEMETRY_FIELDS.length) +for (const key of NATIVE_TELEMETRY_FIELDS) { + assert.equal(isNativeTelemetryField(key), true) + assert.deepEqual(stripNativeTelemetry({ ...base, [key]: "before" }), base) + assert.deepEqual(stripNativeTelemetry({ ...base, [key]: "after" }), base) +} +const unknown = [ + "rx-unknown-setting", "tx-unknown-setting", "rx-mode", "tx-queue-limit", "rx-bytes", "tx-bytes", + "max-bytes", "custom-errors", "config-packets", "maximum-drops", "opaque-config", "opaque", + "Rx-byte", "RX-byte", "rx-byte-limit", "tx-error-policy", "ipv4-fast-path-bytes-limit", +] +for (const key of unknown) { + assert.equal(isNativeTelemetryField(key), false, key) + const old = { ...base, [key]: "before" }, changed = { ...base, [key]: "after" } + assert.deepEqual(stripNativeTelemetry(old), old, `${key}: unknown value retained`) + assert.notDeepEqual(stripNativeTelemetry(old), stripNativeTelemetry(changed), `${key}: drift detectable`) + assert.notDeepEqual(stripNativeTelemetry(old), stripNativeTelemetry(base), `${key}: removal detectable`) +} +const allCounters = Object.fromEntries(NATIVE_TELEMETRY_FIELDS.map(key => [key, "123"])) +const observed = { ...base, ...allCounters, "rx-unknown-setting": false, "max-bytes": 2048, "custom-errors": "reject" } +const expected = { ...base, "rx-unknown-setting": false, "max-bytes": 2048, "custom-errors": "reject" } +assert.deepEqual(stripNativeTelemetry(observed), expected) +assert.equal(stripNativeTelemetry(observed).opaque, base.opaque, "opaque nested config stays intact") +assert.ok(Object.hasOwn(observed, "rx-byte"), "input is never mutated") +assert.notEqual(stripNativeTelemetry(observed), observed, "projection is a fresh object") +assert.deepEqual(stripNativeTelemetry({ setting: undefined }), { setting: undefined }) +console.log(`VRF exact telemetry: ${NATIVE_TELEMETRY_FIELDS.length} explicit fields tolerated; ${unknown.length} unknown/config drift names retained; no SQL/network`) diff --git a/backend/src/test/chr-vrf-companion-telemetry.ts b/backend/src/test/chr-vrf-companion-telemetry.ts new file mode 100644 index 00000000..74a45202 --- /dev/null +++ b/backend/src/test/chr-vrf-companion-telemetry.ts @@ -0,0 +1,26 @@ +/** Exact reviewed fields for the native VRF evidence fixture. Unknown fields + * remain configuration, including unknown counter-looking names. Never infer + * telemetry from prefixes, suffixes, value types or case-insensitive matches. + * The interface counter spellings match traffic-collector and guarded live-map + * readers; plural rx-bytes/tx-bytes are deliberately NOT aliases. */ +export const NATIVE_TELEMETRY_FIELDS = Object.freeze([ + "running", "actual-mtu", "current-mac-address", "active", "inactive", "invalid", + "ipv4-fast-path-active", "ipv4-fasttrack-active", "ipv6-fast-path-active", + "rx-byte", "tx-byte", "rx-packet", "tx-packet", + "rx-drop", "tx-drop", "rx-error", "tx-error", "tx-queue-drop", + "fp-rx-byte", "fp-tx-byte", "fp-rx-packet", "fp-tx-packet", + "ipv4-fast-path-packets", "ipv4-fast-path-bytes", + "ipv4-fasttrack-packets", "ipv4-fasttrack-bytes", + "ipv6-fast-path-packets", "ipv6-fast-path-bytes", + "packets", "bytes", +] as const) + +const telemetry: ReadonlySet = new Set(NATIVE_TELEMETRY_FIELDS) + +export function isNativeTelemetryField(key: string): boolean { return telemetry.has(key) } + +/** A fresh shallow projection. Unknown native configuration is preserved + * verbatim so conservation checks can detect its addition/removal/change. */ +export function stripNativeTelemetry(row: Readonly>): Record { + return Object.fromEntries(Object.entries(row).filter(([key]) => !isNativeTelemetryField(key))) +} diff --git a/backend/src/test/chr-vrf-companion-transfer-cleanup-proof.test.ts b/backend/src/test/chr-vrf-companion-transfer-cleanup-proof.test.ts new file mode 100644 index 00000000..f9ca4a8c --- /dev/null +++ b/backend/src/test/chr-vrf-companion-transfer-cleanup-proof.test.ts @@ -0,0 +1,127 @@ +import assert from "node:assert/strict" +import { readFile } from "node:fs/promises" +import { prepareTransferredVrfCleanupEvidence, assertTransferredVrfCleanupCheckpoint, advanceTransferredVrfCleanupCheckpoint, type TransferredVrfCleanupInput } from "./chr-vrf-companion-transfer-cleanup-proof.js" +import { VRF_CLEANUP_RESOURCES, vrfCleanupForeignHashes } from "./chr-vrf-companion-cleanup-proof.js" +import type { NativeCatalog, NativeRow } from "./chr-vrf-companion-proof.js" + +const original = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-native-v4.json", import.meta.url), "utf8")) +const diagnostic = JSON.parse(await readFile(new URL("../../../docs/mcp-chr-vrf-companion-readonly-diagnostic-v4.json", import.meta.url), "utf8")) +const baseline = Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, []])) as NativeCatalog +baseline.bridge = [{ ".id": "*F0", name: "foreign-bridge", comment: "preserve", "tx-opaque-config": "unchanged" }] +baseline.vrf = [{ ".id": "*0", name: "main", interfaces: "all", disabled: "false" }] +baseline.routing_table = [{ ".id": "*0", name: "main", dynamic: "false", fib: "true" }] +baseline.ethernet = [{ ".id": "*1", name: "ether1", "rx-opaque-config": "unchanged" }] +baseline.ipv4_route = [{ ".id": "*F1", "dst-address": "203.0.113.0/24", gateway: "ether1", "routing-table": "main", dynamic: "false" }] +baseline.ipv6_route = [{ ".id": "*F2", "dst-address": "2001:db8::/64", gateway: "ether1", "routing-table": "main", dynamic: "false" }] +baseline.interface_inventory = [{ ".id": "*1", name: "ether1", type: "ether", "default-name": "ether1", "rx-opaque-config": "unchanged" }] +baseline.ip_settings = [{ "rp-filter": "no", "opaque-bytes": "preserve" }] +baseline.ipv6_settings = [{ "multipath-hash-policy": "l3" }] +// Actual v4 ACK/reenable/transfer shapes; synthetic complete foreign baseline +// because the read-only diagnostic intentionally does not export foreign rows. +for (const phase of original.phases) if (phase.proofStatus === "passed") phase.foreignHashes = vrfCleanupForeignHashes(baseline) +const current = Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, [...structuredClone(baseline[resource]), ...structuredClone(diagnostic.candidates[resource])]])) as NativeCatalog +const input: TransferredVrfCleanupInput = { original, diagnostic, currentCatalog: current, sourceReportSha256: diagnostic.sourceReportSha256, cleaned: [] } +const initial = prepareTransferredVrfCleanupEvidence(input) +assert.equal(initial.deletions.length, 7) +assert.deepEqual(initial.deletions.map(item => item.resource), ["vrf", "ipv6_address", "ipv6_address", "ipv4_address", "ipv4_address", "bridge", "bridge"]) +assert.equal(initial.next?.id, "*3") +assert.equal(initial.next?.expected.interfaces, `${original.marker}-b`) +assert.deepEqual(initial.foreignHashes, vrfCleanupForeignHashes(baseline)) +assert.equal(initial.kernelExclusions.some(item => item.resource === "ipv6_address" && item.id === "*11"), false) +let refusals = 0 +function reject(change: (copy: TransferredVrfCleanupInput) => void) { + const copy = structuredClone(input); change(copy) + assert.throws(() => prepareTransferredVrfCleanupEvidence(copy)); refusals++ +} +reject(copy => { copy.sourceReportSha256 = "f".repeat(64) }) +reject(copy => { (copy.diagnostic as NativeRow).sourceReportSha256 = "f".repeat(64) }) +reject(copy => { (copy.original as typeof original).journal[10].outcome = "unknown" }) +reject(copy => { (copy.original as typeof original).journal[9].readbackVerified = false }) +reject(copy => { (copy.original as typeof original).journal[10].fields.interfaces = "ether1" }) +reject(copy => { (copy.original as typeof original).journal[10].requestedChange = undefined }) +reject(copy => { (copy.original as typeof original).journal[0].assignedId = "*FF" }) +reject(copy => { (copy.original as typeof original).journal.push(structuredClone((copy.original as typeof original).journal[10])) }) +reject(copy => { copy.cleaned = [{ resource: "vrf", id: "*3" }] }) +reject(copy => { copy.currentCatalog.ethernet[0]["rx-opaque-config"] = "changed" }) +reject(copy => { copy.currentCatalog.ip_settings[0]["opaque-bytes"] = "changed" }) +reject(copy => { copy.currentCatalog.bridge_port.push({ ".id": "*FF", bridge: `${original.marker}-a`, interface: "ether1" }) }) +reject(copy => { copy.currentCatalog.bridge_vlan.push({ ".id": "*FF", bridge: "foreign", tagged: `${original.marker}-b` }) }) +reject(copy => { copy.currentCatalog.interface_list_members.push({ ".id": "*FF", interface: `${original.marker}-b`, list: "WAN" }) }) +reject(copy => { copy.currentCatalog.vrf.push({ ".id": "*FF", name: "external", interfaces: `${original.marker}-a` }) }) +for (const resource of ["bridge", "vrf", "ipv4_address", "ipv6_address", "ipv4_route", "ipv6_route", "routing_table", "interface_inventory"]) { + for (const field of ["unknown-config", "rx-opaque-config", "tx-opaque-config"]) reject(copy => { copy.currentCatalog[resource].find(row => row.comment === original.marker || String(row.name ?? "").includes(original.marker) || String(row.gateway ?? "").includes(original.marker))![field] = "changed" }) +} +for (const resource of ["ipv4_route", "ipv6_route"]) reject(copy => { copy.currentCatalog[resource] = copy.currentCatalog[resource].filter(row => !String(row.gateway ?? "").includes(`${original.marker}-b`)) }) +reject(copy => { copy.currentCatalog.ipv6_address = copy.currentCatalog.ipv6_address.filter(row => row[".id"] !== "*17") }) +const active = original.phases.find((phase: { phase: string }) => phase.phase === "vrf-reenable") +const llB: NativeRow = active.owned.ipv6_address.raw.find((row: NativeRow) => row[".id"] === "*11") +const routeB: NativeRow = active.owned.ipv6_route.raw.find((row: NativeRow) => row["dst-address"] === "fe80::/64" && row["immediate-gw"] === `${original.marker}-b`) +reject(copy => { copy.currentCatalog.ipv6_address.push({ ...llB, ".id": "*18" }) }) +reject(copy => { copy.currentCatalog.ipv6_route.push({ ...routeB, ".id": "*E03", gateway: `${original.marker}-b@${original.marker}-r`, "routing-table": `${original.marker}-r` }) }) +const checked = assertTransferredVrfCleanupCheckpoint({ ...input, kernelCheckpoint: initial.kernelCheckpoint }) +assert.deepEqual(checked.foreignHashes, initial.foreignHashes) +assert.equal(checked.kernelCheckpoint, initial.kernelCheckpoint) +assert.throws(() => assertTransferredVrfCleanupCheckpoint({ ...input, kernelCheckpoint: structuredClone(initial.kernelCheckpoint) })); refusals++ +assert.throws(() => advanceTransferredVrfCleanupCheckpoint({ ...input, cleaned: initial.deletions.slice(0, 2).map(({ resource, id }) => ({ resource, id })), previousEvidence: initial, ackDeleted: initial.deletions[1] })); refusals++ + +for (const regenerateB of [false, true]) { + const evolving = structuredClone(input) + let previous = prepareTransferredVrfCleanupEvidence(evolving) + for (const deletion of previous.deletions) { + const stale = previous + const beforeInput = { ...evolving, currentCatalog: structuredClone(evolving.currentCatalog), cleaned: [...evolving.cleaned] } + const alias = assertTransferredVrfCleanupCheckpoint({ ...evolving, kernelCheckpoint: previous.kernelCheckpoint }) + assert.equal(alias.kernelCheckpoint, previous.kernelCheckpoint) + evolving.currentCatalog[deletion.resource] = evolving.currentCatalog[deletion.resource].filter(row => row[".id"] !== deletion.id) + if (deletion.resource === "vrf") { + evolving.currentCatalog.routing_table = evolving.currentCatalog.routing_table.filter(row => row[".id"] !== deletion.id) + evolving.currentCatalog.interface_inventory = evolving.currentCatalog.interface_inventory.filter(row => row.type !== "vrf") + for (const resource of ["ipv4_address", "ipv6_address"]) for (const row of evolving.currentCatalog[resource]) if (row.comment === original.marker) row.vrf = "main" + const bridgeB = evolving.currentCatalog.interface_inventory.find(row => row.name === `${original.marker}-b`)! + bridgeB.vrf = "main"; bridgeB.status = "--LERUI-O---------------" + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of evolving.currentCatalog[resource]) if (row["routing-table"] === `${original.marker}-r`) { + row["routing-table"] = "main"; row.gateway = `${original.marker}-b` + if (typeof row["local-address"] === "string") row["local-address"] = row["local-address"].replace(`@${original.marker}-r`, "") + } + if (regenerateB) { + evolving.currentCatalog.ipv6_address.push({ ...llB, ".id": "*18" }) + evolving.currentCatalog.ipv6_route.push({ ...routeB, ".id": "*E03" }) + } + } else if (deletion.resource.endsWith("address")) { + const resource = deletion.resource === "ipv4_address" ? "ipv4_route" : "ipv6_route" + evolving.currentCatalog[resource] = evolving.currentCatalog[resource].filter(row => row["dst-address"] !== deletion.expected.address) + if (deletion.resource === "ipv4_address") evolving.currentCatalog.interface_inventory.find(row => row.name === deletion.expected.interface)!.status = "--LERU--O---------------" + } else { + evolving.currentCatalog.interface_inventory = evolving.currentCatalog.interface_inventory.filter(row => row[".id"] !== deletion.id) + evolving.currentCatalog.ipv6_address = evolving.currentCatalog.ipv6_address.filter(row => row.interface !== deletion.expected.name) + evolving.currentCatalog.ipv6_route = evolving.currentCatalog.ipv6_route.filter(row => row.gateway !== deletion.expected.name) + } + evolving.cleaned = [...evolving.cleaned, { resource: deletion.resource, id: deletion.id }] + // Read-only validation cannot advance cleaned or adopt an ACK-time branch. + assert.throws(() => assertTransferredVrfCleanupCheckpoint({ ...evolving, kernelCheckpoint: previous.kernelCheckpoint })); refusals++ + previous = advanceTransferredVrfCleanupCheckpoint({ ...evolving, previousEvidence: previous, ackDeleted: deletion }) + assert.deepEqual(previous.foreignHashes, vrfCleanupForeignHashes(baseline)) + assert.throws(() => advanceTransferredVrfCleanupCheckpoint({ ...evolving, previousEvidence: stale, ackDeleted: deletion })); refusals++ + assert.throws(() => assertTransferredVrfCleanupCheckpoint({ ...beforeInput, kernelCheckpoint: alias.kernelCheckpoint })); refusals++ + previous = assertTransferredVrfCleanupCheckpoint({ ...evolving, kernelCheckpoint: previous.kernelCheckpoint }) + if (evolving.cleaned.length === 1) { + const changed = structuredClone(evolving) + if (regenerateB) changed.currentCatalog.ipv6_address.find(row => row[".id"] === "*18")![".id"] = "*19" + else changed.currentCatalog.ipv6_address.push({ ...llB, ".id": "*18" }) + assert.throws(() => assertTransferredVrfCleanupCheckpoint({ ...changed, kernelCheckpoint: previous.kernelCheckpoint })); refusals++ + const removedA = structuredClone(evolving) + removedA.currentCatalog.ipv6_address = removedA.currentCatalog.ipv6_address.filter(row => row[".id"] !== "*17") + assert.throws(() => assertTransferredVrfCleanupCheckpoint({ ...removedA, kernelCheckpoint: previous.kernelCheckpoint })); refusals++ + } + } + assert.equal(previous.next, null) + assert.deepEqual(vrfCleanupForeignHashes(evolving.currentCatalog), vrfCleanupForeignHashes(baseline)) +} +const failedAck = prepareTransferredVrfCleanupEvidence(input) +const incomplete = structuredClone(input) +incomplete.currentCatalog.vrf = incomplete.currentCatalog.vrf.filter(row => row[".id"] !== failedAck.next!.id) +incomplete.cleaned = [{ resource: "vrf", id: failedAck.next!.id }] +// Valid declared next ACK consumes the checkpoint even if post-ACK proof fails. +assert.throws(() => advanceTransferredVrfCleanupCheckpoint({ ...incomplete, previousEvidence: failedAck, ackDeleted: incomplete.cleaned[0] })); refusals++ +assert.throws(() => assertTransferredVrfCleanupCheckpoint({ ...input, kernelCheckpoint: failedAck.kernelCheckpoint })); refusals++ +console.log(`Transferred VRF cleanup: actual v4 source/diagnostic, pinned absent B LL, two ACK-only branch models x seven checkpoints, ${refusals} refusals; no network/SQL/executor`) diff --git a/backend/src/test/chr-vrf-companion-transfer-cleanup-proof.ts b/backend/src/test/chr-vrf-companion-transfer-cleanup-proof.ts new file mode 100644 index 00000000..263d4dee --- /dev/null +++ b/backend/src/test/chr-vrf-companion-transfer-cleanup-proof.ts @@ -0,0 +1,236 @@ +import { isDeepStrictEqual } from "node:util" +import { assertNativeKernelEvidence, assertOwnedAddressClosure, assertVrfMembersOwned, deriveOwnedConnectedRoute, type NativeCatalog, type NativeKernelEvidence, type NativeOwnedObject, type NativeRow } from "./chr-vrf-companion-proof.js" +import { VRF_CLEANUP_RESOURCES, vrfCleanupForeignHashes, type CleanupIdentity } from "./chr-vrf-companion-cleanup-proof.js" +import { stripNativeTelemetry } from "./chr-vrf-companion-telemetry.js" +import { parseChrVrfPrefix } from "./chr-vrf-companion-address-space.js" + +export interface TransferredVrfCleanupInput { + original: unknown; diagnostic: unknown; currentCatalog: NativeCatalog; + sourceReportSha256: string; cleaned: readonly CleanupIdentity[]; +} +export interface TransferredKernelCheckpoint { readonly kind: "transferred-vrf-cleanup-checkpoint" } +export interface TransferredVrfCleanupEvidence { + schema: "chr-vrf-companion-transfer-cleanup-proof-v1"; marker: string; sourceReportSha256: string; + deletions: NativeOwnedObject[]; remaining: NativeOwnedObject[]; next: NativeOwnedObject | null; + cleaned: CleanupIdentity[]; foreignHashes: Record; kernelExclusions: CleanupIdentity[]; + kernelCheckpoint: TransferredKernelCheckpoint; +} +interface KernelRow { resource: string; id: string; kind: "bridge" | "virtual" | "table" | "ll" | "route"; bridgeId?: string; semantic: string; expected: NativeRow } +interface State { + marker: string; sha: string; deletions: NativeOwnedObject[]; remaining: NativeOwnedObject[]; cleaned: CleanupIdentity[]; + hashes: Record; kernel: KernelRow[]; absent: CleanupIdentity[]; + bridges: NativeOwnedObject[]; llSources: KernelRow[]; routeSources: KernelRow[]; +} +const checkpoints = new WeakMap() +function proof(value: unknown): asserts value { if (!value) throw Error("Transferred VRF cleanup proof refused; no DELETE authorized") } +const id = (value: unknown): value is string => typeof value === "string" && /^\*[0-9a-f]+$/i.test(value) +const key = (item: CleanupIdentity) => `${item.resource}/${item.id}` +function obj(value: unknown): NativeRow { proof(value && typeof value === "object" && !Array.isArray(value)); return value as NativeRow } +function rows(value: unknown): NativeRow[] { proof(Array.isArray(value) && value.length <= 50000); return value.map(obj) } +function one(value: unknown, nativeId: string): NativeRow { const found = rows(value).filter(row => row[".id"] === nativeId); proof(found.length === 1); return found[0] } +function same(old: NativeRow, current: NativeRow, omit: readonly string[] = []) { + const project = (row: NativeRow) => Object.fromEntries(Object.entries(stripNativeTelemetry(row)).filter(([field]) => !omit.includes(field))) + proof(isDeepStrictEqual(project(old), project(current))) +} +function catalog(value: NativeRow): NativeCatalog { return Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, rows(value[resource])])) } +function phaseCatalog(phase: NativeRow): NativeCatalog { const owned = obj(phase.owned); return Object.fromEntries(VRF_CLEANUP_RESOURCES.map(resource => [resource, rows(obj(owned[resource]).raw)])) } +function validateCatalog(current: NativeCatalog) { + for (const resource of VRF_CLEANUP_RESOURCES) { + const seen = new Set() + for (const row of rows(current[resource])) { + if (["ip_settings", "ipv6_settings"].includes(resource) && row[".id"] === undefined) continue + proof(id(row[".id"]) && !seen.has(row[".id"])); seen.add(row[".id"]) + } + } +} +function llSuccessor(old: NativeRow, current: NativeRow, bridge: NativeOwnedObject) { + const before = parseChrVrfPrefix(old.address, 6), after = parseChrVrfPrefix(current.address, 6) + proof(before && after && before.prefixLength === 64 && after.prefixLength === 64 && before.host === after.host && after.host >> 64n === 0xfe80000000000000n) + proof([old, current].every(row => row.dynamic === "true" && row["auto-link-local"] === "true" && row["link-local"] === "true" && row.interface === bridge.expected.name && row.vrf === "main")) + const mac = bridge.expected["mac-address"] + proof(typeof mac === "string" && /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(mac)) + const bytes = mac.split(":").map(value => Number.parseInt(value, 16)) + const eui = [bytes[0] ^ 2, bytes[1], bytes[2], 255, 254, bytes[3], bytes[4], bytes[5]].reduce((value, byte) => value << 8n | BigInt(byte), 0n) + proof((after.host & ((1n << 64n) - 1n)) === eui) + same({ ...old, address: before.hostKey }, { ...current, address: after.hostKey }, [".id"]) +} +function routeRows(current: NativeCatalog, primary: readonly NativeOwnedObject[], bridges: readonly NativeOwnedObject[], marker: string, vrfActive: boolean): KernelRow[] { + const vrfs = primary.filter(item => item.resource === "vrf"), closure = assertOwnedAddressClosure(current, primary, bridges, marker, { allowedAddressVrfs: ["main", ...vrfs.map(item => String(item.expected.name))] }) + const result: KernelRow[] = [] + for (const resource of ["ipv4_route", "ipv6_route"]) for (const row of current[resource]) { + const route = deriveOwnedConnectedRoute(row, { addresses: closure, bridges, vrfs: vrfActive ? vrfs : [], allowedTables: ["main", ...(vrfActive ? vrfs.map(item => String(item.expected.name)) : [])] }) + if (!route) continue + const prefix = parseChrVrfPrefix(route.prefix, resource === "ipv4_route" ? 4 : 6) + proof(prefix && row["immediate-gw"] === route.interface) + const bridge = bridges.find(item => item.expected.name === route.interface)! + result.push({ resource, id: route.id, kind: "route", bridgeId: bridge.id, semantic: `${resource}/${bridge.id}/${prefix.hostKey}/${prefix.prefixLength}`, expected: structuredClone(row) }) + } + proof(new Set(result.map(item => item.semantic)).size === result.length) + return result +} +function verifyState(state: State, current: NativeCatalog): Record { + validateCatalog(current) + const expected = [...state.remaining, ...state.kernel], expectedKeys = new Set(expected.map(key)) + proof(expectedKeys.size === expected.length) + for (const item of expected) same(item.expected, one(current[item.resource], item.id)) + for (const item of state.absent) if (!expectedKeys.has(key(item))) proof(!current[item.resource].some(row => row[".id"] === item.id)) + const names = state.bridges.map(item => String(item.expected.name)), vrf = state.remaining.find(item => item.resource === "vrf") + proof([...current.bridge_port, ...current.bridge_vlan, ...current.interface_list_members].every(row => !names.includes(String(row.bridge)) && !names.includes(String(row.interface)) && ![row.tagged, row.untagged].some(value => typeof value === "string" && value.split(",").some(name => names.includes(name))))) + proof(current.vrf.every(row => vrf && row[".id"] === vrf.id || typeof row.interfaces === "string" && !row.interfaces.split(",").some(name => names.includes(name)) && (row.name === "main" || row.interfaces !== "all"))) + const hashes = vrfCleanupForeignHashes(current, expected) + proof(isDeepStrictEqual(hashes, state.hashes)) + return hashes +} +function evidence(state: State, current: NativeCatalog, existing?: TransferredKernelCheckpoint): TransferredVrfCleanupEvidence { + const hashes = verifyState(state, current) + const token: TransferredKernelCheckpoint = existing ?? Object.freeze({ kind: "transferred-vrf-cleanup-checkpoint" }) + if (!existing) checkpoints.set(token, structuredClone(state)) + return { schema: "chr-vrf-companion-transfer-cleanup-proof-v1", marker: state.marker, sourceReportSha256: state.sha, deletions: structuredClone(state.deletions), remaining: structuredClone(state.remaining), next: structuredClone(state.remaining[0] ?? null), cleaned: structuredClone(state.cleaned), foreignHashes: hashes, kernelExclusions: state.kernel.map(({ resource, id }) => ({ resource, id })), kernelCheckpoint: token } +} + +/** Initial admission only. Missing bridge-B LL is a specific transfer-ACK + * tombstone, never an optional LL rule. This cannot resume a serialized run. */ +export function prepareTransferredVrfCleanupEvidence(input: TransferredVrfCleanupInput): TransferredVrfCleanupEvidence { + proof(Array.isArray(input.cleaned) && input.cleaned.length === 0) + const original = obj(input.original), diagnostic = obj(input.diagnostic) + proof(/^[a-f0-9]{64}$/.test(input.sourceReportSha256) && diagnostic.sourceReportSha256 === input.sourceReportSha256) + proof(original.schema === "chr-vrf-companion-native-v4" && original.proofVersion === 4 && original.nativeOnly === true && original.complete === false && original.cleanupComplete === false && original.unknownWrite === true && typeof original.finishedAt === "string") + proof(diagnostic.readOnly === true && diagnostic.nativeMutations === 0 && diagnostic.candidateAttributionIsNotOwnership === true && typeof original.marker === "string" && /^mcp-test-vrf-companion-[0-9a-f-]{36}$/.test(original.marker)) + const marker = original.marker, phases = rows(original.phases), journal = rows(original.journal) + const phase = (name: string) => { const found = phases.filter(item => item.phase === name); proof(found.length === 1); return found[0] } + const baseline = phase("baseline"), pre = phase("addresses-before-vrf"), active = phase("vrf-reenable"), final = phase("finally-readonly-classification"), hashes = obj(baseline.foreignHashes) + proof(Object.keys(hashes).length === 34 && VRF_CLEANUP_RESOURCES.every(resource => [resource, `${resource}:native-configuration`].every(field => typeof hashes[field] === "string" && /^[a-f0-9]{64}$/.test(hashes[field] as string)))) + for (const p of [baseline, pre, phase("vrf-create"), phase("vrf-rename"), phase("vrf-disable"), active]) proof(p.proofStatus === "passed" && p.foreignStable === true && isDeepStrictEqual(p.foreignHashes, hashes)) + proof(journal.length === 11 && final.proofStatus === "refused-unproven-native-state") + const source = phaseCatalog(active), diag = catalog(obj(diagnostic.candidates)), preCatalog = phaseCatalog(pre) + validateCatalog(source); validateCatalog(diag) + const owned: NativeOwnedObject[] = [], identities = new Set() + for (const [index, command] of journal.slice(0, 7).entries()) { + proof(command.method === "PUT" && command.outcome === "acknowledged" && command.ownedIdentityConfirmed === true && command.readbackVerified === true && command.requestedChange === true && id(command.assignedId)) + proof(command.resource === (index < 2 ? "bridge" : index === 6 ? "vrf" : index % 2 === 0 ? "ipv4_address" : "ipv6_address")) + const resource = String(command.resource), created = one((index === 6 ? phaseCatalog(phase("vrf-create")) : preCatalog)[resource], command.assignedId), expected = one(source[resource], command.assignedId), fields = obj(command.fields) + proof(created.comment === marker && expected.comment === marker && fields.comment === marker && Object.entries(fields).every(([field, value]) => field !== ".id" && isDeepStrictEqual(created[field], value))) + same(created, expected, resource === "vrf" ? ["name"] : resource.endsWith("address") ? ["vrf"] : []) + const item = { resource, id: command.assignedId, marker, expected: structuredClone(expected) } + proof(!identities.has(key(item))); identities.add(key(item)); owned.push(item) + } + const bridges = owned.filter(item => item.resource === "bridge"), vrf = owned[6] + for (const [index, name, fields] of [[7, "vrf-rename", { name: vrf.expected.name }], [8, "vrf-disable", { disabled: "true" }], [9, "vrf-reenable", { disabled: "false" }]] as const) { + const command = journal[index] + proof(command.phase === name && command.resource === "vrf" && command.method === "PATCH" && command.id === vrf.id && command.outcome === "acknowledged" && command.ownedIdentityConfirmed === true && command.readbackVerified === true && command.requestedChange === true && isDeepStrictEqual(command.fields, fields)) + } + const transfer = journal[10] + proof(transfer.phase === "vrf-membership-transfer-a-to-b" && transfer.resource === "vrf" && transfer.method === "PATCH" && transfer.id === vrf.id && transfer.outcome === "acknowledged" && transfer.requestedChange === true && transfer.readbackVerified === undefined && transfer.ownedIdentityConfirmed === undefined && isDeepStrictEqual(transfer.fields, { interfaces: bridges[1].expected.name }) && vrf.expected.interfaces === bridges[0].expected.name && vrf.expected.disabled === "false") + const known = rows(original.knownRemaining) + proof(known.length === 7 && new Set(known.map(item => `${String(item.resource)}/${String(item.id)}`)).size === 7 && known.every(item => identities.has(`${String(item.resource)}/${String(item.id)}`))) + const sourceKernel = rows(active.attributions).map(item => obj(item.evidence)).filter(item => item.resource !== undefined) as unknown as NativeKernelEvidence[] + assertNativeKernelEvidence(source, owned, marker, { allowedAddressVrfs: ["main", String(vrf.expected.name), vrf.id] }, sourceKernel) + for (const item of owned) { + const current = one(diag[item.resource], item.id) + same(one(phaseCatalog(final)[item.resource], item.id), current) + if (item.resource === "vrf") { same(item.expected, current, ["interfaces"]); proof(current.interfaces === bridges[1].expected.name) } + else if (item.resource.endsWith("address")) { same(item.expected, current, ["vrf"]); proof(current.vrf === (current.interface === bridges[1].expected.name ? vrf.expected.name : "main")) } + else same(item.expected, current) + item.expected = structuredClone(current) + } + assertVrfMembersOwned(vrf.expected, bridges) + const kernel: KernelRow[] = [], absent: CleanupIdentity[] = [] + for (const bridge of bridges) { + const expected = structuredClone(one(preCatalog.interface_inventory, bridge.id)), current = one(diag.interface_inventory, bridge.id) + proof(expected.type === "bridge" && expected.name === bridge.expected.name && expected.comment === marker && expected.vrf === "main" && expected.status === "--LERUI-O---------------") + if (bridge.id === bridges[1].id) { expected.vrf = vrf.expected.name; expected.status = "--LERUI-O-----------V---" } + same(expected, current); kernel.push({ resource: "interface_inventory", id: bridge.id, kind: "bridge", bridgeId: bridge.id, semantic: `bridge/${bridge.id}`, expected: structuredClone(current) }) + } + for (const kind of ["virtual", "table"] as const) { + const old = sourceKernel.filter(item => item.kind === kind && item.parentId === vrf.id) + proof(old.length === 1 && old[0].present !== false) + const current = one(diag[old[0].resource], old[0].id); same(old[0].expected, current) + kernel.push({ resource: old[0].resource, id: old[0].id, kind, semantic: `${kind}/${vrf.id}`, expected: structuredClone(current) }) + } + const sourceLl = sourceKernel.filter(item => item.kind === "link-local").map(item => ({ resource: item.resource, id: item.id, kind: "ll" as const, bridgeId: item.parentId, semantic: `ll/${item.parentId}`, expected: structuredClone(item.expected) })) + proof(sourceLl.length === 2) + for (const old of sourceLl) { + const bridge = bridges.find(item => item.id === old.bridgeId)!, current = diag.ipv6_address.filter(row => row.interface === bridge.expected.name && row.dynamic === "true") + if (bridge.id === bridges[1].id) { proof(current.length === 0 && !diag.ipv6_address.some(row => row[".id"] === old.id)); absent.push({ resource: old.resource, id: old.id }); continue } + proof(current.length === 1); llSuccessor(old.expected, current[0], bridge) + proof(id(current[0][".id"]) && !diag.ipv6_address.some(row => row[".id"] === old.id)); absent.push({ resource: old.resource, id: old.id }) + kernel.push({ ...old, id: current[0][".id"], expected: structuredClone(current[0]) }) + } + const sourceOwned = owned.map(item => ({ ...item, expected: one(source[item.resource], item.id) })), oldRoutes = routeRows(source, sourceOwned, bridges, marker, true), currentRoutes = routeRows(diag, owned, bridges, marker, true) + proof(oldRoutes.length === 6 && currentRoutes.length === 5) + for (const old of oldRoutes) { + const current = currentRoutes.filter(item => item.semantic === old.semantic), ll = old.expected["dst-address"] === "fe80::/64" + if (ll && old.bridgeId === bridges[1].id) { proof(current.length === 0 && !diag[old.resource].some(row => row[".id"] === old.id)); absent.push({ resource: old.resource, id: old.id }); continue } + proof(current.length === 1); same(old.expected, current[0].expected, [".id", "gateway", "routing-table", "local-address"]) + kernel.push(current[0]) + if (!currentRoutes.some(item => item.resource === old.resource && item.id === old.id)) absent.push({ resource: old.resource, id: old.id }) + } + const deletions = [vrf, ...owned.filter(item => item.resource === "ipv6_address"), ...owned.filter(item => item.resource === "ipv4_address"), ...bridges] + const state: State = { marker, sha: input.sourceReportSha256, deletions, remaining: structuredClone(deletions), cleaned: [], hashes: hashes as Record, kernel, absent, bridges, llSources: sourceLl, routeSources: oldRoutes } + // Diagnostic rows are candidates only; the actual full catalog must also + // restore every independent foreign baseline hash after these exact refs. + return evidence(state, input.currentCatalog) +} + +function stateOf(input: TransferredVrfCleanupInput, token: TransferredKernelCheckpoint): State { + const state = checkpoints.get(token) + proof(state && input.sourceReportSha256 === state.sha && obj(input.diagnostic).sourceReportSha256 === state.sha) + return structuredClone(state) +} +export function assertTransferredVrfCleanupCheckpoint(input: TransferredVrfCleanupInput & { kernelCheckpoint: TransferredKernelCheckpoint }): TransferredVrfCleanupEvidence { + const state = stateOf(input, input.kernelCheckpoint) + proof(isDeepStrictEqual(input.cleaned, state.cleaned)) + return evidence(state, input.currentCatalog, input.kernelCheckpoint) +} + +/** Caller must have the single DELETE ACK before invoking this boundary. The + * opaque previous checkpoint is process-local and cannot be deserialized. */ +export function advanceTransferredVrfCleanupCheckpoint(input: TransferredVrfCleanupInput & { previousEvidence: TransferredVrfCleanupEvidence; ackDeleted: CleanupIdentity }): TransferredVrfCleanupEvidence { + const state = stateOf(input, input.previousEvidence.kernelCheckpoint), next = state.remaining[0] + proof(next && key(next) === key(input.ackDeleted) && isDeepStrictEqual(input.previousEvidence.cleaned, state.cleaned) && isDeepStrictEqual(input.cleaned, [...state.cleaned, { resource: next.resource, id: next.id }])) + // A declared valid next ACK opens exactly one proof attempt. Even a failed + // post-ACK proof cannot be retried with another branch or a stale checkpoint. + checkpoints.delete(input.previousEvidence.kernelCheckpoint) + const current = input.currentCatalog + validateCatalog(current) + proof(!current[next.resource].some(row => row[".id"] === next.id)) + const oldKernel = state.kernel, vrfDelete = next.resource === "vrf" + state.cleaned = input.cleaned.map(item => ({ resource: item.resource, id: item.id })) + state.remaining = state.remaining.slice(1) + state.absent.push({ resource: next.resource, id: next.id }) + for (const item of state.remaining) { + const now = one(current[item.resource], item.id) + if (vrfDelete && item.resource.endsWith("address")) { proof(now.vrf === "main"); same(item.expected, now, ["vrf"]); item.expected = structuredClone(now) } + else same(item.expected, now) + } + const bridges = state.remaining.filter(item => item.resource === "bridge"), vrf = state.remaining.find(item => item.resource === "vrf"), kernel: KernelRow[] = [] + for (const old of oldKernel.filter(item => ["bridge", "virtual", "table"].includes(item.kind))) { + if (vrfDelete && ["virtual", "table"].includes(old.kind) || next.resource === "bridge" && old.bridgeId === next.id) { proof(!current[old.resource].some(row => row[".id"] === old.id)); state.absent.push({ resource: old.resource, id: old.id }); continue } + const now = one(current[old.resource], old.id), expected = structuredClone(old.expected) + if (vrfDelete && old.kind === "bridge" && expected.vrf !== "main") { proof(expected.status === "--LERUI-O-----------V---"); expected.vrf = "main"; expected.status = "--LERUI-O---------------" } + if (next.resource === "ipv4_address" && old.kind === "bridge" && expected.name === next.expected.interface) { proof(expected.status === "--LERUI-O---------------"); expected.status = "--LERU--O---------------" } + same(expected, now); kernel.push({ ...old, expected: structuredClone(now) }) + } + const closure = assertOwnedAddressClosure(current, state.remaining, bridges, state.marker, { allowedAddressVrfs: ["main", ...(vrf ? [String(vrf.expected.name)] : [])] }) + for (const source of state.llSources) { + const bridge = state.bridges.find(item => item.id === source.bridgeId)!, old = oldKernel.find(item => item.kind === "ll" && item.bridgeId === source.bridgeId), now = closure.linkLocalCompanions.filter(item => item.interface === bridge.expected.name) + if (!bridges.some(item => item.id === source.bridgeId)) { proof(now.length === 0); if (old) state.absent.push({ resource: old.resource, id: old.id }); continue } + if (!vrfDelete) { proof(old ? now.length === 1 && now[0].id === old.id : now.length === 0); if (old) { same(old.expected, now[0].row); kernel.push(old) }; continue } + proof(old ? now.length === 1 : now.length <= 1) + if (now.length) { llSuccessor(source.expected, now[0].row, bridge); kernel.push({ ...source, id: now[0].id, expected: structuredClone(now[0].row) }) } + if (old && (!now.length || now[0].id !== old.id)) state.absent.push({ resource: old.resource, id: old.id }) + } + const routes = routeRows(current, state.remaining, bridges, state.marker, Boolean(vrf)) + const needed = state.remaining.filter(item => item.resource.endsWith("address")).length + kernel.filter(item => item.kind === "ll").length + proof(routes.length === needed) + for (const now of routes) { + const old = (vrfDelete ? state.routeSources : oldKernel.filter(item => item.kind === "route")).filter(item => item.semantic === now.semantic) + proof(old.length === 1) + same(old[0].expected, now.expected, vrfDelete ? [".id", "gateway", "routing-table", "local-address"] : []) + kernel.push(now) + } + for (const old of oldKernel.filter(item => item.kind === "route")) if (!routes.some(item => item.resource === old.resource && item.id === old.id)) state.absent.push({ resource: old.resource, id: old.id }) + state.kernel = kernel + const result = evidence(state, current) + return result +} diff --git a/backend/src/test/committed-scoped-checkpoint-observer.test.ts b/backend/src/test/committed-scoped-checkpoint-observer.test.ts new file mode 100644 index 00000000..c96fcf0f --- /dev/null +++ b/backend/src/test/committed-scoped-checkpoint-observer.test.ts @@ -0,0 +1,95 @@ +import assert from "node:assert/strict" +import { AsyncLocalStorage } from "node:async_hooks" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { test } from "node:test" +import type { Pool } from "pg" +import { observeCommittedScopedCheckpoint } from "./committed-scoped-checkpoint-observer.js" +process.env.NODE_ENV = "test" +const identity = "f82a90d9-a5df-4fda-bfe3-2be3bf4f7bd2" +const update = "UPDATE mcp_operation_checkpoints SET configuration_sealed=$3 WHERE operation_id=$1" +const owned = { databaseUrl: "postgresql://mcp_test:synthetic@127.0.0.1:54321/postgres", stateDir: join(tmpdir(), "mm-mcp-test-observer-unit", "state") } +const gate = () => { let resolve!: () => void; const promise = new Promise(done => { resolve = done }); return { promise, resolve } } +function fixture() { + const calls: unknown[][] = [], released: unknown[][] = [], callbackHandle = {} + let commit: () => Promise = async () => ({ command: "COMMIT", rowCount: null, rows: [] }) + const client = { + query: (...args: unknown[]) => { + calls.push(args) + if (typeof args.at(-1) === "function") { (args.at(-1) as (error: null, result: unknown) => void)(null, callbackHandle); return callbackHandle } + return args[0] === "COMMIT" ? commit() : Promise.resolve({ command: "UPDATE", rowCount: 1, rows: [] }) + }, + release: (...args: unknown[]) => { released.push(args) }, + } + const pool = { connect: (...args: unknown[]) => { + if (args.length) { (args[0] as (error: null, value: typeof client) => void)(null, client); return undefined } + return Promise.resolve(client) + } } as unknown as Pick + return { pool, client, calls, released, callbackHandle, setCommit: (work: typeof commit) => { commit = work } } +} +test("real COMMIT ACK precedes detached observer; continuation waits, ACK identity and release remain exact", async () => { + const f = fixture(), ack = gate(), observation = gate(), entered = gate(), parent = new AsyncLocalStorage() + const receipt = { command: "COMMIT", rowCount: null, rows: [] }, rawQuery = f.client.query, rawRelease = f.client.release, rawConnect = f.pool.connect + f.setCommit(async () => { await ack.promise; return receipt }) + const observer = observeCommittedScopedCheckpoint(f.pool, async id => { assert.equal(id, identity); assert.equal(parent.getStore(), undefined); entered.resolve(); await observation.promise }, owned) + try { + const client = await f.pool.connect(); await client.query(update, [identity]) + let returned = false + const pending = parent.run("configuration-origin", () => client.query("COMMIT")).then(result => { returned = true; return result }) + await Promise.resolve(); assert.equal(returned, false); assert.throws(observer.assertCompleted) + ack.resolve(); await entered.promise; assert.equal(returned, false) + observation.resolve(); assert.equal(await pending, receipt); observer.assertCompleted() + client.release(true); assert.equal(f.client.query, rawQuery); assert.equal(f.client.release, rawRelease); assert.deepEqual(f.released, [[true]]) + } finally { observer.close() } + assert.equal(f.pool.connect, rawConnect) +}) +test("ROLLBACK and aborted COMMIT cannot promote a former checkpoint on a later empty COMMIT", async () => { + for (const aborted of [false, true]) { + const f = fixture(); let observed = 0 + const observer = observeCommittedScopedCheckpoint(f.pool, async () => { observed++ }, owned) + try { + const client = await f.pool.connect(); await client.query(update, [identity]) + if (aborted) { f.setCommit(async () => ({ command: "ROLLBACK", rows: [] })); await client.query("COMMIT") } + else await client.query("ROLLBACK") + f.setCommit(async () => ({ command: "COMMIT", rows: [] })); await client.query("COMMIT") + assert.equal(observed, 0); assert.throws(observer.assertCompleted); client.release() + } finally { observer.close() } + } +}) +test("lost COMMIT ACK propagates unchanged and cannot be retried into an observed checkpoint", async () => { + const f = fixture(), lost = Error("synthetic lost ACK"); let observed = 0 + const observer = observeCommittedScopedCheckpoint(f.pool, async () => { observed++ }, owned) + try { + const client = await f.pool.connect(); await client.query(update, [identity]); f.setCommit(async () => { throw lost }) + await assert.rejects(client.query("COMMIT"), error => error === lost) + f.setCommit(async () => ({ command: "COMMIT", rows: [] })); await client.query("COMMIT") + assert.equal(observed, 0); assert.throws(observer.assertCompleted); client.release(true) + } finally { observer.close() } +}) +test("observer assertion is reported separately and never falsifies the genuine driver ACK", async () => { + const f = fixture(), failure = Error("synthetic observer assertion"), receipt = { command: "COMMIT", rows: [] } + f.setCommit(async () => receipt) + const observer = observeCommittedScopedCheckpoint(f.pool, async () => { throw failure }, owned) + try { + const client = await f.pool.connect(); await client.query(update, [identity]) + assert.equal(await client.query("COMMIT"), receipt); assert.throws(observer.assertCompleted, error => error === failure); client.release() + } finally { observer.close() } +}) +test("observer is single-use; callback connect/query overloads and unrelated COMMIT remain untouched", async () => { + const f = fixture(); let observed = 0, callbackCalled = false + const observer = observeCommittedScopedCheckpoint(f.pool, async () => { observed++ }, owned) + try { + const callbackReturn = f.pool.connect((error, client) => { assert.equal(error, null); assert.equal(client, f.client); callbackCalled = true }) + assert.equal(callbackReturn, undefined); assert(callbackCalled) + const client = await f.pool.connect(); await client.query("COMMIT"); assert.equal(observed, 0) + const handle = client.query("SELECT 1", () => {}); assert.equal(handle, f.callbackHandle) + await client.query(update, [identity]); await client.query("COMMIT"); await client.query(update, [identity]); await client.query("COMMIT") + assert.equal(observed, 1); observer.assertCompleted(); client.release() + } finally { observer.close(); observer.close() } +}) +test("non-owned database or state targets refuse before any checkout", () => { + for (const context of [{ ...owned, databaseUrl: owned.databaseUrl.replace("127.0.0.1", "192.0.2.1") }, { ...owned, stateDir: join(tmpdir(), "unrelated", "state") }]) { + const f = fixture(), original = f.pool.connect + assert.throws(() => observeCommittedScopedCheckpoint(f.pool, async () => {}, context)); assert.equal(f.pool.connect, original); assert.equal(f.calls.length, 0) + } +}) diff --git a/backend/src/test/committed-scoped-checkpoint-observer.ts b/backend/src/test/committed-scoped-checkpoint-observer.ts new file mode 100644 index 00000000..45b7dc72 --- /dev/null +++ b/backend/src/test/committed-scoped-checkpoint-observer.ts @@ -0,0 +1,67 @@ +/** Owned acceptance observer only. Forwards every real driver call and result. + * Its out-of-band checks run after the genuine checkpoint COMMIT, before the + * original continuation starts a timed native probe. It grants no authority. */ +import assert from "node:assert/strict" +import { AsyncResource } from "node:async_hooks" +import { basename, dirname, resolve } from "node:path" +import type { Pool, PoolClient } from "pg" + +export function observeCommittedScopedCheckpoint(pool: Pick, observer: (operationId: string) => Promise, + fixture: { databaseUrl: string; stateDir: string }) { + assert.equal(process.env.NODE_ENV, "test") + const target = new URL(fixture.databaseUrl) + assert(["postgres:", "postgresql:"].includes(target.protocol)) + assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") + assert.equal(target.pathname, "/postgres"); assert(target.password) + assert.equal(target.search, ""); assert.equal(target.hash, "") + assert.equal(basename(resolve(fixture.stateDir)), "state") + assert.match(basename(dirname(resolve(fixture.stateDir))), /^mm-mcp-test-[A-Za-z0-9-]+$/) + const originalConnect = pool.connect, scope = new AsyncResource("owned-committed-scoped-checkpoint") + let armed = true, completed = false, failure: { error: unknown } | undefined, closed = false + const connect = ((...connectArgs: unknown[]) => { + const connected = Reflect.apply(originalConnect, pool, connectArgs) + // Keep pg's callback overload and all unrelated checkouts unchanged. + if (connectArgs.length || !armed || closed) return connected + return Promise.resolve(connected).then((client: PoolClient) => { + const originalQuery = client.query, originalRelease = client.release + let operationId: string | undefined + client.query = ((...parameters: unknown[]) => { + const answer = Reflect.apply(originalQuery, client, parameters) + if (typeof parameters.at(-1) === "function") return answer + return Promise.resolve(answer).then(async result => { + const sql = typeof parameters[0] === "string" ? parameters[0] : undefined + const values = Array.isArray(parameters[1]) ? parameters[1] : [] + if (sql?.startsWith("UPDATE mcp_operation_checkpoints SET configuration_sealed=") && result.rowCount === 1) { + operationId = typeof values[0] === "string" && /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(values[0]) ? values[0] : undefined + } + if (sql?.trim().toUpperCase() === "ROLLBACK") operationId = undefined + if (sql?.trim().toUpperCase() === "COMMIT") { + const committedOperationId = operationId; operationId = undefined + if (!closed && armed && result.command === "COMMIT" && committedOperationId) { + armed = false + try { await scope.runInAsyncScope(observer, undefined, committedOperationId) } + catch (error) { failure = { error } } + finally { completed = true } + } + } + // An assertion in the observer must never falsify the real COMMIT ACK. + // The awaited acceptance assertion below reports its original failure. + return result + }, error => { + if (typeof parameters[0] === "string" && parameters[0].trim().toUpperCase() === "COMMIT") { operationId = undefined; armed = false } + throw error + }) + }) as typeof client.query + client.release = ((...args: unknown[]) => { + client.query = originalQuery; client.release = originalRelease + return Reflect.apply(originalRelease, client, args) + }) as typeof client.release + return client + }) + }) as typeof pool.connect + pool.connect = connect + return { + assertCompleted() { if (failure) throw failure.error; assert(completed, "No genuine scoped checkpoint COMMIT was observed") }, + close() { if (closed) return; closed = true; if (pool.connect === connect) pool.connect = originalConnect; scope.emitDestroy() }, + } +} diff --git a/backend/src/test/database-rest-replica.ts b/backend/src/test/database-rest-replica.ts new file mode 100644 index 00000000..aac9f386 --- /dev/null +++ b/backend/src/test/database-rest-replica.ts @@ -0,0 +1,278 @@ +import assert from "node:assert/strict" +import { fork } from "node:child_process" +import { once } from "node:events" +import { setTimeout as delay } from "node:timers/promises" +import { fileURLToPath } from "node:url" +import Fastify from "fastify" +import type { PoolClient } from "pg" +import { closePool, pool } from "../db/index.js" +import { SYSTEM_DATABASE_REQUEST_LOCK, withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { randomUUID } from "node:crypto" +import { withDetachedDatabaseApiRequest } from "../modules/mcp/database-request-lease.js" +import { + DATABASE_RESTORE_QUARANTINE_DDL, beginDatabaseRestoreQuarantine, completeDatabaseRestoreQuarantine, +} from "../db/database-restore-quarantine.js" +import { installDatabaseRequestGate } from "../plugins/database-request-gate.js" +import { withConfigMutationLock } from "../services/config-mutation-lock.js" +import { databaseBackgroundWork } from "../services/database-background-work.js" +import { + admitPartitionWriter, PARTITION_WRITER_ADVISORY_KEY, PartitionWriteError, partitionWriteFailureDiagnostic, withPartitionWrite, +} from "../services/partition-write-transaction.js" +import { deleteTrafficFlowData } from "../services/traffic-flow-purge-execution.js" +import { enqueueRipeMisses, lookupRipeCached, resetRipeCacheForTests, setRipeFetchForTests } from "../services/traffic-flow-ripe.js" +import { commitFlowWrite } from "../services/traffic-flow-write-transaction.js" + +const endpoint = "/api/owned-replica" +const detachedEndpoint = "/api/owned-replica/detached" +const detachedAdmissionEndpoint = "/owned-replica/detached-admission" +const readSql = "SELECT audit_retention_days FROM mcp_settings WHERE id=1" +const lockedRead = readSql + " FOR UPDATE" +const finishDetachedSql = "UPDATE mcp_settings SET audit_retention_days=audit_retention_days+1 WHERE id=1" +const pendingExclusiveLock = "SELECT 1 FROM pg_locks WHERE locktype='advisory' AND classid=0 AND objid=$1::oid AND objsubid=1 AND mode='ExclusiveLock' AND NOT granted" +const quarantineCode = "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" +const readQuarantineId = "SELECT operation_id FROM mmapp_control.database_restore_quarantine WHERE id=1" + +if (process.argv.includes("--replica-child")) { + assert(process.send && process.env.MMAPP_OWN_REST_REPLICA_ONLY === "true") + let finishDetached!: () => void + const detachedLatch = new Promise(resolve => { finishDetached = resolve }) + let detached: Promise | undefined + const app = Fastify() + installDatabaseRequestGate(app) + app.get("/health", async () => ({ ok: true })) + app.post(endpoint, async () => { + await pool.query(lockedRead) + return withConfigMutationLock([721], async () => { + const { rows } = await pool.query("UPDATE mcp_settings SET audit_retention_days=audit_retention_days+1 WHERE id=1 RETURNING audit_retention_days") + return rows[0] + }) + }) + app.post(detachedEndpoint, async (_request, reply) => { + detached = withDetachedDatabaseApiRequest(async () => { + process.send?.("detached-started") + await detachedLatch + await pool.query(finishDetachedSql) + }) + void detached.catch(error => console.error("Detached replica work failed", error)) + reply.code(202) + return { accepted: true } + }) + // Bypass the HTTP gate to exercise the detached operation's own quarantine check. + app.post(detachedAdmissionEndpoint, () => withDetachedDatabaseApiRequest(async () => { + assert.fail("Quarantined detached work entered its callback") + })) + const address = await app.listen({ host: "127.0.0.1", port: 0 }) + process.on("message", async message => { + if (message === "finish-detached") { finishDetached(); return } + if (message !== "stop") return + await app.close() + await detached?.catch(() => {}) + await closePool() + process.disconnect?.() + }) + process.send(address) +} + +async function verifyRipeCacheWrites(phase: "partition lock held" | "quarantined" | "reopened") { + const ip = "8.8.8.8", prefix = `${ip}/32`, asn = 64512, holder = "Owned RIPE fixture" + const canPersist = phase === "reopened" + setRipeFetchForTests(async input => { + const { pathname } = new URL(String(input)) + if (pathname === "/data/network-info/data.json") return Response.json({ data: { prefix, asns: [asn] } }) + if (pathname === "/data/maxmind-geo-lite/data.json") return Response.json({ data: {} }) + assert.equal(pathname, "/data/as-overview/data.json") + return Response.json({ data: { holder } }) + }) + try { + enqueueRipeMisses([ip]) + await databaseBackgroundWork.drain(4000) + const entry = lookupRipeCached(ip) + assert.equal(entry?.holder, holder, `RIPE resolution with admission ${phase}`) + assert.equal(entry?.asn, asn) + assert.deepEqual((await pool.query("SELECT asn, holder FROM flow_ip_meta WHERE prefix=$1", [prefix])).rows, + canPersist ? [{ asn, holder }] : [], `RIPE prefix persistence with admission ${phase}`) + assert.deepEqual((await pool.query("SELECT holder FROM flow_asn_meta WHERE asn=$1", [asn])).rows, + canPersist ? [{ holder }] : [], `RIPE ASN persistence with admission ${phase}`) + } finally { + resetRipeCacheForTests() + } +} + +export async function verifyDatabaseRestReplica() { + await pool.query("CREATE SCHEMA IF NOT EXISTS mmapp_control") + await pool.query(DATABASE_RESTORE_QUARANTINE_DDL) + const before = (await pool.query(readSql)).rows[0].audit_retention_days + const child = fork(fileURLToPath(import.meta.url), ["--replica-child"], { + execArgv: ["--import", "tsx"], stdio: ["ignore", "ignore", "inherit", "ipc"], + env: { ...process.env, DATABASE_RESTORE_QUARANTINE: "true" }, + }) + const exited = once(child, "exit") + const signal = AbortSignal.timeout(15000) + const blocker = await pool.connect() + const previousQuarantine = process.env.DATABASE_RESTORE_QUARANTINE + let snapshot: PoolClient | undefined + let first: Promise | undefined, maintenance: Promise | undefined + let partitionWriter: Promise | undefined, quarantine: Promise | undefined + try { + const [address] = await Promise.race([ + once(child, "message", { signal }), + exited.then(() => assert.fail("Replica exited before readiness")), + ]) + assert.equal(typeof address, "string") + const post = (path = endpoint) => fetch(address + path, { method: "POST", signal }) + const waitFor = async (sql: string, args: unknown[]) => { + while (!(await pool.query(sql, args)).rowCount) await delay(15, undefined, { signal }) + } + const refused = async (code = "PROTOCOL_STORAGE_BUSY", path = endpoint) => { + const response = await post(path) + assert.equal(response.status, 503) + const body = await response.json() + assert.ok(body && typeof body === "object" && "code" in body) + assert.equal(body.code, code) + } + await blocker.query("BEGIN") + await blocker.query(lockedRead) + first = post() + void first.catch(() => {}) + await waitFor("SELECT 1 FROM pg_stat_activity WHERE query=$1 AND wait_event_type='Lock'", [lockedRead]) + maintenance = withSystemDatabaseOperationLock(() => pool.connect(), async () => { + assert.equal((await pool.query(readSql)).rows[0].audit_retention_days, before + 1) + await refused() + assert.equal((await fetch(address + "/health", { signal })).status, 200) + }) + void maintenance.catch(() => {}) + await waitFor(pendingExclusiveLock, [SYSTEM_DATABASE_REQUEST_LOCK]) + await refused() + await blocker.query("ROLLBACK") + const response = await first + assert.equal(response.status, 200) + assert.deepEqual(await response.json(), { audit_retention_days: before + 1 }) + await maintenance + const resumed = await post() + assert.equal(resumed.status, 200) + assert.deepEqual(await resumed.json(), { audit_retention_days: before + 2 }) + console.log("Independent PostgreSQL replica: queued maintenance drained the active REST request and its nested mutation, denied new requests, then reopened admission") + + await blocker.query("BEGIN") + await blocker.query(lockedRead) + const [accepted, [started]] = await Promise.all([ + post(detachedEndpoint), once(child, "message", { signal }), + ]) + assert.equal(accepted.status, 202) + assert.deepEqual(await accepted.json(), { accepted: true }) + assert.equal(started, "detached-started") + const afterDetached = before + 3 + maintenance = withSystemDatabaseOperationLock(() => pool.connect(), async () => { + assert.equal((await pool.query(readSql)).rows[0].audit_retention_days, afterDetached) + }) + void maintenance.catch(() => {}) + await waitFor(pendingExclusiveLock, [SYSTEM_DATABASE_REQUEST_LOCK]) + assert.equal((await pool.query(readSql)).rows[0].audit_retention_days, before + 2) + child.send("finish-detached") + await waitFor("SELECT 1 FROM pg_stat_activity WHERE query=$1 AND wait_event_type='Lock'", [finishDetachedSql]) + assert.ok((await pool.query(pendingExclusiveLock, [SYSTEM_DATABASE_REQUEST_LOCK])).rowCount) + await blocker.query("ROLLBACK") + await maintenance + console.log("Independent PostgreSQL replica: detached work held admission after HTTP 202 through its final committed UPDATE") + + process.env.DATABASE_RESTORE_QUARANTINE = "true" + await blocker.query("BEGIN") + await blocker.query("SELECT pg_catalog.pg_advisory_xact_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + await verifyRipeCacheWrites("partition lock held") + await blocker.query("ROLLBACK") + + snapshot = await pool.connect() + await snapshot.query("BEGIN ISOLATION LEVEL REPEATABLE READ") + assert.equal((await snapshot.query(readQuarantineId)).rows[0].operation_id, null) + + let writerCalls = 0 + const writePartition = () => withPartitionWrite(async ({ client }) => { + writerCalls++ + return (await client.query<{ audit_retention_days: number }>(lockedRead)).rows[0].audit_retention_days + }, { signal }) + await blocker.query("BEGIN") + await blocker.query(lockedRead) + partitionWriter = writePartition() + void partitionWriter.catch(() => {}) + await waitFor("SELECT 1 FROM pg_stat_activity WHERE query=$1 AND wait_event_type='Lock'", [lockedRead]) + + const operationId = randomUUID(), otherId = randomUUID() + let restoreTargetReached = false + quarantine = withSystemDatabaseOperationLock(() => pool.connect(), async owner => { + await owner.query("BEGIN", []) + await owner.query("SET LOCAL synchronous_commit=on", []) + await beginDatabaseRestoreQuarantine(owner, operationId) + await owner.query("COMMIT", []) + await owner.query("BEGIN", []) + try { + await owner.query("SET LOCAL lock_timeout='5s'", []) + await owner.query("SELECT pg_catalog.pg_advisory_xact_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + await owner.query(readSql, []) + restoreTargetReached = true + } finally { + await owner.query("ROLLBACK", []) + } + }) + void quarantine.catch(() => {}) + await waitFor(pendingExclusiveLock + " AND EXISTS (SELECT 1 FROM pg_stat_activity AS writer WHERE writer.pid=ANY(pg_blocking_pids(pg_locks.pid)) AND writer.query=$2)", [PARTITION_WRITER_ADVISORY_KEY, lockedRead]) + assert.equal(restoreTargetReached, false) + await blocker.query("ROLLBACK") + assert.equal(await partitionWriter, afterDetached) + await quarantine + assert.equal(restoreTargetReached, true) + + await refused(quarantineCode) + await refused(quarantineCode, detachedAdmissionEndpoint) + await assert.rejects(beginDatabaseRestoreQuarantine(pool, otherId), { code: quarantineCode }) + await assert.rejects(completeDatabaseRestoreQuarantine(pool, otherId), { code: quarantineCode }) + await refused(quarantineCode) + assert.equal((await pool.query(readQuarantineId)).rows[0].operation_id, operationId) + assert.equal((await snapshot.query(readQuarantineId)).rows[0].operation_id, null) + await assert.rejects(admitPartitionWriter(snapshot), { code: quarantineCode }) + await assert.rejects(writePartition(), error => { + assert(error instanceof PartitionWriteError) + assert.equal(error.code, "PARTITION_WRITE_UNAVAILABLE") + assert.equal(partitionWriteFailureDiagnostic(error)?.stage, "admission") + return true + }) + assert.equal(writerCalls, 1) + const flowBatchId = randomUUID() + await assert.rejects(commitFlowWrite( + { id: flowBatchId, kind: "ingest", sourceProof: "a".repeat(64) }, + async () => assert.fail("Quarantined NetFlow batch entered its write callback"), + ), { code: quarantineCode }) + assert.equal((await pool.query("SELECT 1 FROM flow_write_receipts WHERE batch_id=$1", [flowBatchId])).rowCount, 0) + await assert.rejects(deleteTrafficFlowData( + () => pool.connect(), async () => assert.fail("Quarantined NetFlow purge entered its delete callback"), + ), { code: quarantineCode }) + await verifyRipeCacheWrites("quarantined") + assert.equal((await pool.query(readSql)).rows[0].audit_retention_days, afterDetached) + + await withSystemDatabaseOperationLock(() => pool.connect(), async owner => { + await owner.query("BEGIN", []) + await owner.query("SET LOCAL synchronous_commit=on", []) + await completeDatabaseRestoreQuarantine(owner, operationId) + await owner.query("COMMIT", []) + }) + await verifyRipeCacheWrites("reopened") + console.log("Independent PostgreSQL replica: RIPE prefix and ASN writes resumed only after exclusive partition locking and durable quarantine were cleared") + assert.equal(await writePartition(), afterDetached) + assert.equal(writerCalls, 2) + const recovered = await post() + assert.equal(recovered.status, 200) + assert.deepEqual(await recovered.json(), { audit_retention_days: afterDetached + 1 }) + console.log("Independent PostgreSQL replica: the exclusive restore lock drained an admitted partition writer before target SQL; durable quarantine refused requests, detached work and partition writes after lock release; exact resolution reopened admission") + } finally { + if (child.connected) child.send("finish-detached") + try { await blocker.query("ROLLBACK") } finally { blocker.release() } + try { await snapshot?.query("ROLLBACK") } finally { snapshot?.release() } + await Promise.allSettled([first, maintenance, partitionWriter, quarantine]) + if (previousQuarantine === undefined) delete process.env.DATABASE_RESTORE_QUARANTINE + else process.env.DATABASE_RESTORE_QUARANTINE = previousQuarantine + if (child.connected) child.send("stop") + try { + const [code] = await Promise.race([exited, delay(2500, undefined, { ref: false }).then(() => assert.fail("Replica did not shut down"))]) + assert.equal(code, 0) + } catch (error) { child.kill(); throw error } + } +} diff --git a/backend/src/test/database-restore-quarantine-owned.ts b/backend/src/test/database-restore-quarantine-owned.ts new file mode 100644 index 00000000..861f607c --- /dev/null +++ b/backend/src/test/database-restore-quarantine-owned.ts @@ -0,0 +1,490 @@ +import assert from "node:assert/strict" +import { spawn } from "node:child_process" +import { createHash, randomBytes, randomUUID } from "node:crypto" +import { link, mkdir, readFile, realpath, unlink, writeFile } from "node:fs/promises" +import type { Server } from "node:net" +import { tmpdir } from "node:os" +import path from "node:path" +import { fileURLToPath } from "node:url" +import pg from "pg" +import { env } from "../config.js" +import { initDatabase } from "../db/bootstrap.js" +import { closePool, pool } from "../db/index.js" +import { captureRuntimeRestoreData } from "../db/database-restore-data.js" +import { + DATABASE_RESTORE_QUARANTINE_DDL, assertDatabaseRestoreQuarantineOpen, completeDatabaseRestoreQuarantine, + readDatabaseRestoreJournalHead, +} from "../db/database-restore-quarantine.js" +import { applySqlMigrations } from "../db/migrate.js" +import { withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { mcpStateDirectory } from "../modules/mcp/security.js" +import { runtimeDatabaseTargetSha256 } from "../services/database-maintenance-resolution.js" +import { assertDatabaseRestoreJournalCurrent, databaseRestoreJournalHead, readMaintenanceRecoveryFacts } from "../services/database-maintenance-startup.js" +import { listenForRestoreCheckpointClaims } from "../services/database-restore-checkpoint-claim.js" +import { exportSystemDatabaseBackup, restoreSystemDatabaseBackup } from "../services/system-database-backup.js" +import { completePendingSystemDatabaseRestore, sealPendingSystemDatabaseRestore } from "../services/system-database-restore-recovery.js" +import { stageSystemDatabaseRestoreCheckpoint } from "../services/system-database-restore-staging.js" +import { inspectSystemDatabaseRestoreCheckpoint } from "../services/system-database-restore-inspection.js" +import { stopScheduler } from "../services/scheduler.js" +import { databaseSchemaReadiness } from "../services/shared-database-gate.js" +import { stopTrafficFlowListener } from "../services/traffic-flow-ingest.js" + +export async function verifyRestoreQuarantineWithArchive() { + assert.equal(process.env.MMAPP_OWN_RESTORE_QUARANTINE_ONLY, "true") + const url = new URL(env.DATABASE_URL) + assert.equal(url.hostname, "127.0.0.1") + assert.equal(url.username, "mcp_test") + assert.equal(url.pathname, "/postgres") + + const ownerPassword = randomBytes(24).toString("hex") + const runtimePassword = randomBytes(24).toString("hex") + await pool.query(` + CREATE ROLE quarantine_restore LOGIN PASSWORD '${ownerPassword}'; + CREATE ROLE quarantine_runtime LOGIN PASSWORD '${runtimePassword}'; + `) + await pool.query("CREATE DATABASE owned_restore_quarantine OWNER quarantine_restore") + url.pathname = "/owned_restore_quarantine" + const admin = new pg.Client({ connectionString: url.toString() }) + try { + await admin.connect() + await admin.query("ALTER SCHEMA public OWNER TO quarantine_restore; CREATE SCHEMA mmapp_control") + await admin.query(DATABASE_RESTORE_QUARANTINE_DDL) + await admin.query(` + GRANT USAGE ON SCHEMA mmapp_control TO quarantine_runtime, quarantine_restore; + GRANT SELECT ON mmapp_control.database_restore_quarantine TO quarantine_runtime, quarantine_restore; + GRANT UPDATE (operation_id, journal_prefix_bytes, journal_prefix_sha256) + ON mmapp_control.database_restore_quarantine TO quarantine_restore; + `) + } finally { await admin.end() } + + url.username = "quarantine_restore" + url.password = ownerPassword + const ownerUrl = url.toString() + const owner = new pg.Pool({ connectionString: ownerUrl }) + try { + await applySqlMigrations(owner) + await owner.query(` + GRANT SELECT ON ALL TABLES IN SCHEMA public TO quarantine_runtime; + GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO quarantine_runtime; + `) + } finally { await owner.end() } + + const directory = mcpStateDirectory() + const ownerFile = path.join(directory, "owned-restore-connection") + await mkdir(directory, { recursive: true, mode: 0o700 }) + await writeFile(ownerFile, ownerUrl, { flag: "wx", mode: 0o600 }) + url.username = "quarantine_runtime" + url.password = runtimePassword + await new Promise((resolve, reject) => { + const child = spawn(process.execPath, ["--import", "tsx", fileURLToPath(import.meta.url)], { + stdio: "inherit", + env: { + ...process.env, DATABASE_URL: url.toString(), DATABASE_RESTORE_URL_FILE: ownerFile, + DATABASE_RESTORE_QUARANTINE: "true", + }, + }) + child.once("error", reject) + child.once("exit", (code, signal) => { + if (code === 0) resolve() + else reject(new Error(`Restricted restore test exited with ${code ?? signal}`)) + }) + }) +} + +async function verifyRestrictedRestore() { + assert.equal(process.env.MMAPP_OWN_RESTORE_QUARANTINE_ONLY, "true") + const url = new URL(env.DATABASE_URL) + assert.equal(url.hostname, "127.0.0.1") + assert.equal(url.username, "quarantine_runtime") + assert.equal(url.pathname, "/owned_restore_quarantine") + const ownerFile = env.DATABASE_RESTORE_URL_FILE + assert.ok(ownerFile) + assert.equal(ownerFile, path.join(mcpStateDirectory(), "owned-restore-connection")) + const owner = new pg.Client({ connectionString: await readFile(ownerFile, "utf8") }) + const captureDataPin = () => withSystemDatabaseOperationLock(() => pool.connect(), + async (client, discard) => (await captureRuntimeRestoreData(client, discard)).sha256) + const directory = mcpStateDirectory() + const journalFile = path.join(directory, "database-maintenance.jsonl") + const readJournal = async () => (await readFile(journalFile, "utf8")) + .trim().split("\n").map(line => JSON.parse(line)) + const readQuarantine = async () => (await pool.query<{ operation_id: string | null }>( + "SELECT operation_id FROM mmapp_control.database_restore_quarantine WHERE id=1", + )).rows[0].operation_id + try { + assert.deepEqual((await pool.query("SELECT session_user, current_user")).rows, [ + { session_user: "quarantine_runtime", current_user: "quarantine_runtime" }, + ]) + await assert.rejects(pool.query("UPDATE mmapp_control.database_restore_quarantine SET operation_id = NULL"), { code: "42501" }) + await assert.rejects(pool.query("UPDATE mmapp_control.database_restore_quarantine SET journal_prefix_bytes = NULL"), { code: "42501" }) + await assert.rejects(pool.query("UPDATE mmapp_control.database_restore_quarantine SET journal_prefix_sha256 = NULL"), { code: "42501" }) + await assert.rejects(pool.query("ALTER TABLE public.servers ADD COLUMN forbidden int"), { code: "42501" }) + await owner.connect() + await owner.query("INSERT INTO public.servers(id,name,host,port,enabled) VALUES(1,'in archive','127.0.0.1',9,false)") + const archiveDataPin = await captureDataPin() + await owner.query("UPDATE public.servers SET name='live value' WHERE id=1") + const otherDataPin = await captureDataPin() + assert.notEqual(otherDataPin, archiveDataPin) + await owner.query("UPDATE public.servers SET name='in archive' WHERE id=1") + const dump = await exportSystemDatabaseBackup() + const archivePin = createHash("sha256").update(dump.buffer).digest("hex") + await owner.query("UPDATE public.servers SET name='live value' WHERE id=1") + process.env.DATABASE_RESTORE_ARCHIVE_SHA256 = createHash("sha256").update("wrong archive").digest("hex") + process.env.DATABASE_RESTORE_DATA_SHA256 = archiveDataPin + await assert.rejects(restoreSystemDatabaseBackup(dump.buffer), { code: "DATABASE_RESTORE_DATA_REFUSED" }) + await assertDatabaseRestoreQuarantineOpen(pool) + assert.deepEqual((await pool.query("SELECT name FROM public.servers WHERE id=1")).rows, [{ name: "live value" }]) + + process.env.DATABASE_RESTORE_ARCHIVE_SHA256 = archivePin + const otherJournal = path.join(directory, "journal-hardlink-test") + await writeFile(otherJournal, "", { flag: "wx" }) + await link(otherJournal, journalFile) + try { + await assert.rejects(restoreSystemDatabaseBackup(dump.buffer), /External maintenance state requires inspection/) + assert.equal((await readFile(otherJournal)).length, 0) + await assertDatabaseRestoreQuarantineOpen(pool) + assert.deepEqual((await pool.query("SELECT name FROM public.servers WHERE id=1")).rows, [{ name: "live value" }]) + } finally { + await unlink(journalFile) + await unlink(otherJournal) + } + const result = await restoreSystemDatabaseBackup(dump.buffer) + assert.equal(result.maintenanceError, undefined) + assert.equal(result.migrationError, undefined) + await assertDatabaseRestoreQuarantineOpen(pool) + assert.deepEqual((await pool.query("SELECT name FROM public.servers WHERE id=1")).rows, [{ name: "in archive" }]) + + const expectedBinding = { + archiveSha256: archivePin, + dataSha256: archiveDataPin, + runtimeTargetSha256: runtimeDatabaseTargetSha256(env.DATABASE_URL), + activationSha256: null, + } + const appliedEvents = await readJournal() + assert.deepEqual(appliedEvents.map(event => event.state), ["intent", "applied"]) + for (const event of appliedEvents) { + assert.equal(event.format, "database-restore-data-journal-v1") + assert.equal(event.operationId, appliedEvents[0].operationId) + assert.deepEqual(event.binding, expectedBinding) + assert.match(event.mac, /^[a-f0-9]{64}$/) + } + const appliedOperation = { operationId: appliedEvents[0].operationId, state: "applied" } + assert.deepEqual((await readMaintenanceRecoveryFacts(directory)).operations, [appliedOperation]) + + const originalJournal = await readFile(journalFile) + const expectedHead = databaseRestoreJournalHead(originalJournal) + const appliedIntent = originalJournal.subarray(0, originalJournal.indexOf(0x0a) + 1) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), expectedHead) + assert.deepEqual(await assertDatabaseRestoreJournalCurrent(pool, directory), expectedHead) + try { + for (const event of appliedEvents) { + const tampered = Buffer.from(originalJournal) + const tag = '"mac":"' + const offset = tampered.indexOf(tag + event.mac) + assert.ok(offset >= 0) + const byte = offset + tag.length + tampered[byte] = tampered[byte] === 0x30 ? 0x31 : 0x30 + await writeFile(journalFile, tampered) + await assert.rejects(readMaintenanceRecoveryFacts(directory)) + } + + const legacyPair = appliedEvents.map(({ operationId, state, at }) => ({ operationId, state, at })) + await writeFile(journalFile, legacyPair.map(event => JSON.stringify(event) + "\n").join("")) + assert.deepEqual((await readMaintenanceRecoveryFacts(directory)).operations, [appliedOperation]) + await assert.rejects(assertDatabaseRestoreJournalCurrent(pool, directory), /External maintenance state requires inspection/) + await assert.rejects(initDatabase(), /External maintenance state requires inspection/) + + await unlink(journalFile) + await assert.rejects(assertDatabaseRestoreJournalCurrent(pool, directory), /External maintenance state requires inspection/) + + const appendedOperationId = randomUUID() + const appendedPair = legacyPair.map(event => JSON.stringify({ ...event, operationId: appendedOperationId }) + "\n").join("") + await writeFile(journalFile, Buffer.concat([originalJournal, Buffer.from(appendedPair)]), { mode: 0o600 }) + assert.deepEqual((await readMaintenanceRecoveryFacts(directory)).operations, [ + appliedOperation, { operationId: appendedOperationId, state: "applied" }, + ]) + await assert.rejects(assertDatabaseRestoreJournalCurrent(pool, directory), /External maintenance state requires inspection/) + + await writeFile(journalFile, appliedIntent) + assert.deepEqual((await readMaintenanceRecoveryFacts(directory)).operations, [ + { operationId: appliedOperation.operationId, state: "intent" }, + ]) + await assert.rejects(assertDatabaseRestoreJournalCurrent(pool, directory), /External maintenance state requires inspection/) + } finally { + await writeFile(journalFile, originalJournal, { mode: 0o600 }) + } + assert.deepEqual(await assertDatabaseRestoreJournalCurrent(pool, directory), expectedHead) + assert.deepEqual((await readMaintenanceRecoveryFacts(directory)).operations, [appliedOperation]) + + // Reproduce the durable state after the applied append and before quarantine completion. + const pendingHead = databaseRestoreJournalHead(appliedIntent) + const appliedId = appliedOperation.operationId + assert.equal((await owner.query(`UPDATE mmapp_control.database_restore_quarantine + SET operation_id=$1, journal_prefix_bytes=$2, journal_prefix_sha256=$3 WHERE id=1`, + [appliedId, pendingHead.bytes, pendingHead.sha256])).rowCount, 1) + const assertPending = async () => { + assert.equal(await readQuarantine(), appliedId) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), pendingHead) + assert.deepEqual(await readFile(journalFile), originalJournal) + } + await assertPending() + await assert.rejects(initDatabase(), { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" }) + await assert.rejects(exportSystemDatabaseBackup(), { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" }) + await assert.rejects(completePendingSystemDatabaseRestore(appliedId, pendingHead.sha256)) + await assertPending() + + await owner.query("UPDATE public.servers SET name='live value' WHERE id=1") + await assert.rejects(completePendingSystemDatabaseRestore(appliedId, expectedHead.sha256)) + await assertPending() + assert.deepEqual((await pool.query("SELECT name FROM public.servers WHERE id=1")).rows, [{ name: "live value" }]) + await owner.query("UPDATE public.servers SET name='in archive' WHERE id=1") + assert.equal(await captureDataPin(), archiveDataPin) + await completePendingSystemDatabaseRestore(appliedId, expectedHead.sha256) + await assertDatabaseRestoreQuarantineOpen(pool) + assert.deepEqual(await assertDatabaseRestoreJournalCurrent(pool, directory), expectedHead) + await assert.rejects(completePendingSystemDatabaseRestore(appliedId, expectedHead.sha256)) + assert.equal(await readQuarantine(), null) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), expectedHead) + assert.deepEqual(await readFile(journalFile), originalJournal) + assert.equal(await captureDataPin(), archiveDataPin) + + const checkpointFile = path.join(await realpath(path.dirname(directory)), `restore-checkpoint-${randomUUID()}.json`) + const checkpointBinding = { + format: "database-restore-checkpoint-v1", + runtimeTargetSha256: expectedBinding.runtimeTargetSha256, + activationSha256: expectedBinding.activationSha256, + } + const sealedCheckpoint = { ...checkpointBinding, status: "sealed", head: expectedHead } + const socketName = `mmapp-restore-${randomUUID()}` + const claimAddress = process.platform === "win32" ? `\\\\.\\pipe\\${socketName}` : path.join(tmpdir(), socketName) + const previousCheckpointFile = process.env.DATABASE_RESTORE_CHECKPOINT_FILE + const previousClaimAddress = process.env.DATABASE_RESTORE_CHECKPOINT_CLAIM_SOCKET + const sealedFixtureBytes = Buffer.from(JSON.stringify(sealedCheckpoint)) + const sealedFixtureSha256 = createHash("sha256").update(sealedFixtureBytes).digest("hex") + await writeFile(checkpointFile, sealedFixtureBytes, { flag: "wx", mode: 0o600 }) + process.env.DATABASE_RESTORE_CHECKPOINT_FILE = checkpointFile + process.env.DATABASE_RESTORE_CHECKPOINT_CLAIM_SOCKET = claimAddress + let claimServer: Server | undefined + const setFixtureHead = async (head: typeof expectedHead | null, operationId: string | null = null) => { + const updated = await owner.query(`UPDATE mmapp_control.database_restore_quarantine + SET operation_id=$1, journal_prefix_bytes=$2, journal_prefix_sha256=$3 WHERE id=1`, + [operationId, head?.bytes ?? null, head?.sha256 ?? null]) + assert.equal(updated.rowCount, 1) + } + try { + claimServer = await listenForRestoreCheckpointClaims(checkpointFile, claimAddress) + assert.deepEqual(await assertDatabaseRestoreJournalCurrent(pool, directory), expectedHead) + const initialInspection = await inspectSystemDatabaseRestoreCheckpoint() + assert.equal(initialInspection.state, "matches-sealed") + assert.deepEqual(initialInspection.journalHead, expectedHead) + assert.equal(initialInspection.checkpointSha256, sealedFixtureSha256) + assert.ok((await exportSystemDatabaseBackup()).buffer.length > 0) + await setFixtureHead(null) + await unlink(journalFile) + assert.equal((await inspectSystemDatabaseRestoreCheckpoint()).state, "inspection-required") + await assert.rejects(initDatabase(), { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" }) + await assert.rejects(exportSystemDatabaseBackup(), { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" }) + await assert.rejects(stageSystemDatabaseRestoreCheckpoint(sealedFixtureSha256), { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" }) + assert.deepEqual(await readFile(checkpointFile), sealedFixtureBytes) + await setFixtureHead(expectedHead) + await writeFile(journalFile, originalJournal, { mode: 0o600 }) + + await owner.query("UPDATE public.servers SET name='live value' WHERE id=1") + const assertRestoreNotStarted = async () => { + assert.equal(await readQuarantine(), null) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), expectedHead) + assert.deepEqual(await readFile(journalFile), originalJournal) + assert.equal(await captureDataPin(), otherDataPin) + } + + const reformattedBytes = Buffer.concat([sealedFixtureBytes, Buffer.from("\n")]) + await writeFile(checkpointFile, reformattedBytes) + await assert.rejects(stageSystemDatabaseRestoreCheckpoint(sealedFixtureSha256), { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" }) + assert.deepEqual(await readFile(checkpointFile), reformattedBytes) + await assertRestoreNotStarted() + + const checkpointOperationId = await stageSystemDatabaseRestoreCheckpoint( + createHash("sha256").update(reformattedBytes).digest("hex"), + ) + assert.match(checkpointOperationId, /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/) + assert.notEqual(checkpointOperationId, appliedId) + const staged = { ...sealedCheckpoint, status: "pending", operationId: checkpointOperationId, + archiveSha256: archivePin, dataSha256: archiveDataPin } + const stagedBytes = await readFile(checkpointFile) + assert.deepEqual(JSON.parse(stagedBytes.toString("utf8")), staged) + const pendingInspection = await inspectSystemDatabaseRestoreCheckpoint() + assert.equal(pendingInspection.state, "matches-pending") + assert.equal(pendingInspection.operationId, checkpointOperationId) + await assertRestoreNotStarted() + const stagedSha256 = createHash("sha256").update(stagedBytes).digest("hex") + await assert.rejects(stageSystemDatabaseRestoreCheckpoint(stagedSha256), { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" }) + assert.deepEqual(await readFile(checkpointFile), stagedBytes) + await assertRestoreNotStarted() + + await writeFile(checkpointFile, JSON.stringify({ ...staged, dataSha256: otherDataPin })) + await assert.rejects(restoreSystemDatabaseBackup(dump.buffer), { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" }) + await assertRestoreNotStarted() + + await writeFile(checkpointFile, stagedBytes) + const stagedResult = await restoreSystemDatabaseBackup(dump.buffer) + assert.equal(stagedResult.dataVerified, true) + assert.equal(stagedResult.migrationError, undefined) + assert.match(stagedResult.maintenanceError ?? "", /публикации внешней контрольной записи/) + assert.throws(databaseSchemaReadiness.assertReady) + assert.equal(await readQuarantine(), checkpointOperationId) + assert.equal((await inspectSystemDatabaseRestoreCheckpoint()).state, "applied-consumed") + const consumedBytes = await readFile(checkpointFile) + const consumed = JSON.parse(consumedBytes.toString("utf8")) + assert.match(consumed.claimId, /^[0-9a-f]{64}$/) + assert.deepEqual(consumed, { ...staged, status: "consumed", claimId: consumed.claimId }) + const stagedJournal = await readFile(journalFile) + assert.deepEqual(stagedJournal.subarray(0, originalJournal.length), originalJournal) + const stagedHead = databaseRestoreJournalHead(stagedJournal) + const stagedIntentHead = databaseRestoreJournalHead(stagedJournal.subarray(0, stagedJournal.indexOf(0x0a, originalJournal.length) + 1)) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), stagedIntentHead) + assert.deepEqual((await readMaintenanceRecoveryFacts(directory)).operations, [ + appliedOperation, { operationId: checkpointOperationId, state: "applied" }, + ]) + const checkpointEvents = (await readJournal()).slice(2) + assert.deepEqual(checkpointEvents.map(event => event.state), ["intent", "applied"]) + for (const event of checkpointEvents) { + assert.equal(event.operationId, checkpointOperationId) + assert.deepEqual(event.binding, { ...expectedBinding, claimId: consumed.claimId }) + } + + await setFixtureHead(expectedHead) + await writeFile(journalFile, originalJournal) + assert.equal((await inspectSystemDatabaseRestoreCheckpoint()).state, "inspection-required") + await owner.query("UPDATE public.servers SET name='live value' WHERE id=1") + await assertDatabaseRestoreQuarantineOpen(pool) + assert.deepEqual(await assertDatabaseRestoreJournalCurrent(pool, directory), expectedHead) + assert.equal(await captureDataPin(), otherDataPin) + await assert.rejects(restoreSystemDatabaseBackup(dump.buffer), { code: "DATABASE_RESTORE_CHECKPOINT_REFUSED" }) + assert.equal(await readQuarantine(), null) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), expectedHead) + assert.deepEqual(await readFile(journalFile), originalJournal) + assert.deepEqual(await readFile(checkpointFile), consumedBytes) + assert.equal(await captureDataPin(), otherDataPin) + + await setFixtureHead(stagedIntentHead, checkpointOperationId) + await writeFile(journalFile, stagedJournal) + const assertStaged = async (checkpointBytes: Buffer) => { + assert.equal(await readQuarantine(), checkpointOperationId) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), stagedIntentHead) + assert.deepEqual(await readFile(journalFile), stagedJournal) + assert.deepEqual(await readFile(checkpointFile), checkpointBytes) + } + await assert.rejects(sealPendingSystemDatabaseRestore(checkpointOperationId, stagedHead.sha256)) + await assertStaged(consumedBytes) + assert.equal(await captureDataPin(), otherDataPin) + + await owner.query("UPDATE public.servers SET name='in archive' WHERE id=1") + await assert.rejects(completePendingSystemDatabaseRestore(checkpointOperationId, stagedHead.sha256)) + await writeFile(checkpointFile, JSON.stringify(sealedCheckpoint)) + await assert.rejects(completePendingSystemDatabaseRestore(checkpointOperationId, stagedHead.sha256)) + + const wrongClaimId = (consumed.claimId[0] === "0" ? "1" : "0") + consumed.claimId.slice(1) + const wrongClaimBytes = Buffer.from(JSON.stringify({ ...consumed, claimId: wrongClaimId })) + await writeFile(checkpointFile, wrongClaimBytes) + await assert.rejects(sealPendingSystemDatabaseRestore(checkpointOperationId, stagedHead.sha256)) + await assertStaged(wrongClaimBytes) + await writeFile(checkpointFile, consumedBytes) + await assert.rejects(sealPendingSystemDatabaseRestore(checkpointOperationId, stagedIntentHead.sha256)) + await assertStaged(consumedBytes) + + await sealPendingSystemDatabaseRestore(checkpointOperationId, stagedHead.sha256) + const sealedInspection = await inspectSystemDatabaseRestoreCheckpoint() + assert.equal(sealedInspection.state, "applied-sealed") + assert.deepEqual(sealedInspection.journalHead, stagedHead) + const sealedBytes = await readFile(checkpointFile) + assert.deepEqual(JSON.parse(sealedBytes.toString("utf8")), { ...sealedCheckpoint, head: stagedHead }) + await assertStaged(sealedBytes) + await assert.rejects(assertDatabaseRestoreQuarantineOpen(pool), { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" }) + assert.throws(databaseSchemaReadiness.assertReady) + await assert.rejects(sealPendingSystemDatabaseRestore(checkpointOperationId, stagedHead.sha256)) + await assertStaged(sealedBytes) + + await completePendingSystemDatabaseRestore(checkpointOperationId, stagedHead.sha256) + assert.equal((await inspectSystemDatabaseRestoreCheckpoint()).state, "matches-sealed") + await assertDatabaseRestoreQuarantineOpen(pool) + assert.deepEqual(await assertDatabaseRestoreJournalCurrent(pool, directory), stagedHead) + assert.deepEqual(await readFile(journalFile), stagedJournal) + assert.deepEqual(await readFile(checkpointFile), sealedBytes) + assert.equal(await captureDataPin(), archiveDataPin) + } finally { + if (claimServer) { + const server = claimServer + await new Promise(resolve => server.close(() => resolve())) + } + if (previousCheckpointFile === undefined) delete process.env.DATABASE_RESTORE_CHECKPOINT_FILE + else process.env.DATABASE_RESTORE_CHECKPOINT_FILE = previousCheckpointFile + if (previousClaimAddress === undefined) delete process.env.DATABASE_RESTORE_CHECKPOINT_CLAIM_SOCKET + else process.env.DATABASE_RESTORE_CHECKPOINT_CLAIM_SOCKET = previousClaimAddress + await unlink(checkpointFile) + await setFixtureHead(expectedHead) + await writeFile(journalFile, originalJournal, { mode: 0o600 }) + databaseSchemaReadiness.confirmSchema() + } + console.log("Restore checkpoint: paired loss blocked startup, backup and staging; approved staging preserved SQL and journal state; consumed claim blocked replay; verified seal preserved quarantine until completion") + + await owner.query("UPDATE public.servers SET name='live value' WHERE id=1") + process.env.DATABASE_RESTORE_DATA_SHA256 = otherDataPin + await assert.rejects(restoreSystemDatabaseBackup(dump.buffer), { code: "DATABASE_RESTORE_DATA_REFUSED" }) + assert.deepEqual((await pool.query("SELECT name FROM public.servers WHERE id=1")).rows, [{ name: "in archive" }]) + assert.throws(databaseSchemaReadiness.assertReady) + await assert.rejects(assertDatabaseRestoreQuarantineOpen(pool), { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" }) + await assert.rejects(exportSystemDatabaseBackup(), { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" }) + const mismatchEvents = await readJournal() + assert.deepEqual(mismatchEvents.map(event => event.state), ["intent", "applied", "intent", "unconfirmed"]) + const mismatchId = await readQuarantine() + assert.ok(mismatchId) + for (const event of mismatchEvents.slice(2)) { + assert.equal(event.format, "database-restore-data-journal-v1") + assert.equal(event.operationId, mismatchId) + assert.deepEqual(event.binding, { ...expectedBinding, dataSha256: otherDataPin }) + assert.match(event.mac, /^[a-f0-9]{64}$/) + } + assert.deepEqual((await readMaintenanceRecoveryFacts(directory)).operations, [ + appliedOperation, { operationId: mismatchId, state: "unconfirmed" }, + ]) + + const intentHead = await readDatabaseRestoreJournalHead(owner) + const unconfirmedJournal = await readFile(journalFile) + assert.ok(intentHead) + const unconfirmedHead = databaseRestoreJournalHead(unconfirmedJournal) + await owner.query("UPDATE public.servers SET name='live value' WHERE id=1") + assert.equal(await captureDataPin(), otherDataPin) + await assert.rejects(completePendingSystemDatabaseRestore(mismatchId, unconfirmedHead.sha256)) + assert.equal(await readQuarantine(), mismatchId) + assert.deepEqual(await readDatabaseRestoreJournalHead(pool), intentHead) + assert.deepEqual(await readFile(journalFile), unconfirmedJournal) + await owner.query("UPDATE public.servers SET name='in archive' WHERE id=1") + + // Reset the disposable fixture after checking refusal so it can exercise the separate SQL failure path. + await completeDatabaseRestoreQuarantine(owner, mismatchId, { + expected: intentHead, + next: unconfirmedHead, + }) + databaseSchemaReadiness.confirmSchema() + process.env.DATABASE_RESTORE_DATA_SHA256 = archiveDataPin + await owner.query(` + CREATE SCHEMA owned_quarantine_fixture; + CREATE VIEW owned_quarantine_fixture.restore_blocker AS SELECT id FROM public.servers; + UPDATE public.servers SET name='before failed restore' WHERE id=1; + `) + await assert.rejects(restoreSystemDatabaseBackup(dump.buffer), /psql не выполнена/) + await assert.rejects(assertDatabaseRestoreQuarantineOpen(pool), { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" }) + const events = await readJournal() + assert.deepEqual(events.map(event => event.state), ["intent", "applied", "intent", "unconfirmed", "intent", "unconfirmed"]) + assert.equal(await readQuarantine(), events[5].operationId) + assert.deepEqual((await pool.query("SELECT name FROM public.servers WHERE id=1")).rows, [{ name: "before failed restore" }]) + await assert.rejects(exportSystemDatabaseBackup(), { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE" }) + console.log("Restore quarantine: restricted runtime denied UPDATE and DDL; matched pins cleared quarantine and anchored the authenticated journal; pending completion preserved the journal and refused wrong approval, changed data, replay and unconfirmed outcome; modified MACs, replacement, removal, append and truncation were refused; wrong archive refused before replacement; wrong data and failed DDL retained quarantine and refused backup") + } finally { + stopScheduler() + await stopTrafficFlowListener() + await owner.end() + await closePool() + } +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) await verifyRestrictedRestore() diff --git a/backend/src/test/fixtures/bandwidth-control-identity.ts b/backend/src/test/fixtures/bandwidth-control-identity.ts new file mode 100644 index 00000000..afe96501 --- /dev/null +++ b/backend/src/test/fixtures/bandwidth-control-identity.ts @@ -0,0 +1,65 @@ +/** Complete synthetic identity/catalog fixture derived from the standalone validator corpus. */ +import { randomUUID } from "node:crypto" +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "../../services/bandwidth-admission-schema.js" +import { BANDWIDTH_CONTROL_SOURCE_DDL } from "../../services/bandwidth-control-source-schema.js" +import { validateBandwidthControlCatalogRows } from "../../services/bandwidth-control-catalog.js" +import { type BandwidthControlIdentityExpected } from "../../services/bandwidth-control-identity.js" +type Row=Record +type Item={section:string;raw_json:string|null} +// Same complete inert catalog corpus as the standalone validator tests. +function fixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_CONTROL_SOURCE_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_attempts:[["p",["attempt_id"]],["u",["caller_kind","operation_id"]]],bandwidth_node_occupancy:[["p",["node_id"]],["i",["attempt_id"]]],bandwidth_attempt_events:[["p",["ack_id"]],["u",["attempt_id","transition"]]],bandwidth_source_identity:[["p",["id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const table of["bandwidth_node_occupancy","bandwidth_attempt_events"]){ + const name=`${table}_attempt_id_fkey` + addConstraint(table,"f",["attempt_id"],name,{foreign_namespace:"mmapp_control",foreign_table:"bandwidth_attempts",foreign_keys:["attempt_id"],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],["bandwidth_attempts","RI_FKey_restrict_del",9],["bandwidth_attempts","RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +const expected:BandwidthControlIdentityExpected={databaseName:"owned_database",runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",applicationRole:"restricted_application", + databaseOid:123,installationId:randomUUID(),controlGeneration:randomUUID(),sourceRecordSha256:"a".repeat(64),eventKeySha256:"b".repeat(64), + catalogSha256:validateBandwidthControlCatalogRows(fixture())} +function rows(){ + const observation={databaseName:expected.databaseName,sessionRole:expected.runtimeRole,currentRole:expected.runtimeRole,runtimeRoleExists:true,ownerRoleExists:true, + runtimeFlagsSafe:true,ownerMembership:false,dangerousMembership:false,schemaExists:true,schemaOwner:expected.ownerRole,schemaUsage:true,schemaCreate:false,publicSchemaUsage:false,publicSchemaCreate:false, + tables:["bandwidth_attempts","bandwidth_node_occupancy","bandwidth_attempt_events","bandwidth_source_identity"].map(name=>({name,exists:true,ownerRole:expected.ownerRole,ownedByRuntime:false,privilegesExact:true}))} + const entries:[string,unknown][]=[["role",{observation}],["database",{oid:expected.databaseOid,owner:expected.ownerRole}],["isolation",{unsafe:false}], + ["source",{installation_id:expected.installationId,control_generation:expected.controlGeneration,attestation_sha256:expected.sourceRecordSha256,event_key_sha256:expected.eventKeySha256,status:"active"}], + ...fixture().map(item=>["catalog",item] as [string,unknown])] + return entries.map(([kind,value],index)=>({kind,row_order:String(index+1),raw_json:JSON.stringify(value)})) +} + +export {expected,rows}; diff --git a/backend/src/test/fixtures/bandwidth-node-identity.ts b/backend/src/test/fixtures/bandwidth-node-identity.ts new file mode 100644 index 00000000..ef1ffaaf --- /dev/null +++ b/backend/src/test/fixtures/bandwidth-node-identity.ts @@ -0,0 +1,64 @@ +/** Complete synthetic identity/catalog fixture derived from the standalone validator corpus. */ +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "../../services/bandwidth-admission-schema.js" +import { BANDWIDTH_NODE_REGISTRY_DDL } from "../../services/bandwidth-node-registry-schema.js" +import { validateBandwidthNodeRegistryCatalogRows } from "../../services/bandwidth-node-registry-catalog.js" +import { type BandwidthNodeIdentityExpected } from "../../services/bandwidth-node-identity.js" +type Row=Record +type Item={section:string;raw_json:string|null} +type Batch={kind:string;row_order:string;raw_json:string|null} +function catalogFixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_node_authority","bandwidth_nodes","bandwidth_node_aliases","bandwidth_node_events"] + const functions=["bandwidth_node_authority_guard","bandwidth_node_immutable_guard","bandwidth_node_alias_guard","bandwidth_node_event_guard","bandwidth_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_node_authority:[["p",["id"]]],bandwidth_nodes:[["p",["node_id"]],["u",["claim_proof"]]],bandwidth_node_aliases:[["p",["server_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_node_events:[["p",["event_id"]],["u",["server_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_node_aliases","node_id","bandwidth_nodes"],["bandwidth_node_events","server_id","bandwidth_node_aliases"],["bandwidth_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +function fixture(){ + const catalog=catalogFixture(),expected:BandwidthNodeIdentityExpected={databaseName:"owned_database",runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",installationId:"11111111-1111-4111-8111-111111111111",issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64),registryCatalogSha256:validateBandwidthNodeRegistryCatalogRows(catalog)} + const role={observation:{databaseName:expected.databaseName,sessionRole:expected.runtimeRole,currentRole:expected.runtimeRole,runtimeRoleExists:true,ownerRoleExists:true,runtimeFlagsSafe:true,ownerMembership:false,dangerousMembership:false,schemaExists:true,schemaOwner:expected.ownerRole,schemaUsage:true,schemaCreate:false,publicSchemaUsage:false,publicSchemaCreate:false,tables:["bandwidth_node_aliases","bandwidth_nodes","bandwidth_node_events","bandwidth_node_authority"].map(name=>({name,exists:true,ownerRole:expected.ownerRole,ownedByRuntime:false,privilegesExact:true}))}} + const authority={installation_id:expected.installationId,issuer_key_sha256:expected.issuerKeySha256,quiescence_proof:expected.quiescenceProof,status:"active"} + const groups:Record={role:[role],catalog,authority:[authority]},rows:Batch[]=Object.entries(groups).flatMap(([kind,values])=>values.map(value=>({kind,row_order:"",raw_json:JSON.stringify(value)}))) + ordinal(rows);return{rows,expected,catalog} +} +function ordinal(rows:Batch[]){rows.forEach((row,index)=>{row.row_order=String(index+1)});return rows} + +export {fixture}; diff --git a/backend/src/test/fixtures/bandwidth-peer-identity.ts b/backend/src/test/fixtures/bandwidth-peer-identity.ts new file mode 100644 index 00000000..e56085c1 --- /dev/null +++ b/backend/src/test/fixtures/bandwidth-peer-identity.ts @@ -0,0 +1,117 @@ +/** Complete synthetic identity/catalog fixture derived from the standalone validator corpus. */ +import { BANDWIDTH_ADMISSION_PROPOSAL_DDL } from "../../services/bandwidth-admission-schema.js" +import { BANDWIDTH_NODE_REGISTRY_DDL } from "../../services/bandwidth-node-registry-schema.js" +import { BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL } from "../../services/bandwidth-external-node-registry-schema.js" +import { type BandwidthPeerIdentityExpected } from "../../services/bandwidth-peer-identity.js" +import { validateBandwidthPeerRegistryCatalogRows } from "../../services/bandwidth-node-registry-catalog.js" +type Row=Record +type Item={section:string;raw_json:string|null} +type Batch={kind:string;row_order:string;raw_json:string|null} +// Inert DDL-derived catalog facts, matching the independent component tests. +// No database callbacks or observer replays implement the batch validator. +function managedFixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_node_authority","bandwidth_nodes","bandwidth_node_aliases","bandwidth_node_events"] + const functions=["bandwidth_node_authority_guard","bandwidth_node_immutable_guard","bandwidth_node_alias_guard","bandwidth_node_event_guard","bandwidth_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_node_authority:[["p",["id"]]],bandwidth_nodes:[["p",["node_id"]],["u",["claim_proof"]]],bandwidth_node_aliases:[["p",["server_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_node_events:[["p",["event_id"]],["u",["server_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_node_aliases","node_id","bandwidth_nodes"],["bandwidth_node_events","server_id","bandwidth_node_aliases"],["bandwidth_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +function externalFixture():Item[]{ + const found:Record={namespace:[{name:"mmapp_control",owner:"fixed_owner"}],tables:[],columns:[],functions:[],triggers:[],constraints:[],indexes:[],rules:[],policies:[]} + const tableBodies=new Map() + const tables=["bandwidth_external_node_aliases","bandwidth_external_node_events"] + const functions=["bandwidth_external_node_alias_guard","bandwidth_external_node_event_guard","bandwidth_external_node_alias_consistency_guard","bandwidth_admission_reject_truncate"] + for(const ddl of [BANDWIDTH_ADMISSION_PROPOSAL_DDL,BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL]){ + for(const match of ddl.matchAll(/CREATE TABLE mmapp_control\.([a-z_]+) \(([\s\S]*?)\n\);/g)){ + const table=match[1];if(!tables.includes(table))continue;tableBodies.set(table,match[2]) + found.tables.push({name:table,owner:"fixed_owner",kind:"r",persistence:"p",row_security:false,force_row_security:false,is_partition:false,replica_identity:"d",options:null,has_parent:false,has_children:false,typed_table:false,access_method:"heap"}) + let position=0 + for(const column of match[2].matchAll(/^\s+([a-z_][a-z_0-9]*) (uuid|text|integer|bigint|jsonb|timestamptz)\b([^\n]*)/gm)){ + const type=column[2]==="integer"?"int4":column[2]==="bigint"?"int8":column[2],isText=type==="text" + found.columns.push({table_name:table,position:++position,name:column[1],type_namespace:"pg_catalog",type_name:type,not_null:/NOT NULL|PRIMARY KEY/.test(column[3]),identity:"",generated:"",collation_namespace:isText?"pg_catalog":null,collation_name:isText?(/COLLATE "C"/.test(column[3])?"C":"default"):null,default_expr:/DEFAULT clock_timestamp\(\)/.test(column[3])?"clock_timestamp()":/DEFAULT 'active'/.test(column[3])?"'active'::text":null}) + } + } + for(const match of ddl.matchAll(/CREATE FUNCTION mmapp_control\.([a-z_]+)\([\s\S]*?\)\s+RETURNS\s+(?:boolean|trigger)[\s\S]*?\bAS\s+\$body\$([\s\S]*?)\$body\$;/g)){ + if(!functions.includes(match[1]))continue + const validator=match[1]==="bandwidth_admission_binding_valid",args=validator?["jsonb","uuid","text","text","text","text","text","text"]:[] + found.functions.push({name:match[1],owner:"fixed_owner",language:"plpgsql",body:match[2],security_definer:false,strict:validator,volatility:validator?"i":"v",leakproof:false,kind:"f",parallel:"u",argument_defaults:0,variadic:false,returns_set:false,has_support:false,input_only:true,argument_names:validator?["b","attempt","caller","operation","source","destination","proof","generation"]:null,config:["search_path=pg_catalog, mmapp_control"],return_type:validator?"bool":"trigger",return_namespace:"pg_catalog",argument_types:args,argument_namespaces:args.map(()=>"pg_catalog")}) + } + for(const match of ddl.matchAll(/CREATE (CONSTRAINT )?TRIGGER ([a-z_]+) (BEFORE|AFTER) ([\s\S]*?) ON mmapp_control\.([a-z_]+) ([\s\S]*?)EXECUTE FUNCTION mmapp_control\.([a-z_]+)\(\);/g)){ + if(!tables.includes(match[5]))continue + const deferred=!!match[1],events=match[4],row=match[6].includes("FOR EACH ROW"),type=(row?1:0)+(match[3]==="BEFORE"?2:0)+(events.includes("INSERT")?4:0)+(events.includes("DELETE")?8:0)+(events.includes("UPDATE")?16:0)+(events.includes("TRUNCATE")?32:0) + found.triggers.push({table_name:match[5],name:match[2],internal:false,type,enabled:"A",deferrable:deferred,initially_deferred:deferred,is_constraint:deferred,constraint_name:deferred?match[2]:null,function_namespace:"mmapp_control",function_name:match[7],argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + } + const addConstraint=(table:string,type:string,keys:string[],name:string,extra:Row={})=>found.constraints.push({table_name:table,name,type,validated:true,deferrable:type==="t",initially_deferred:type==="t",keys,foreign_namespace:null,foreign_table:null,foreign_keys:[],update_action:" ",delete_action:" ",match_type:" ",definition:type+":"+keys.join(","),...extra}) + for(const [table,body]of tableBodies){ + let index=0 + for(const _ of body.matchAll(/\bCHECK\s*\(/g))addConstraint(table,"c",[],`${table}_check_${index++}`) + for(const column of found.columns.filter(row=>row.table_name===table&&row.not_null))addConstraint(table,"n",[String(column.name)],`${table}_${column.name}_not_null`) + } + for(const row of found.triggers.filter(row=>row.is_constraint))addConstraint(String(row.table_name),"t",[],String(row.name)) + const specifications:Record>={bandwidth_external_node_aliases:[["p",["enrollment_id"]],["u",["incarnation"]],["i",["node_id"]]],bandwidth_external_node_events:[["p",["event_id"]],["u",["enrollment_id","kind"]],["i",["node_id"]]]} + for(const[table,specs]of Object.entries(specifications))for(const[kind,keys]of specs){ + if(kind!=="i")addConstraint(table,kind,keys,`${table}_${kind}`) + const fields=keys.map(name=>found.columns.find(row=>row.table_name===table&&row.name===name)!) + found.indexes.push({table_name:table,name:`${table}_${kind}`,unique:kind!=="i",primary:kind==="p",valid:true,ready:true,live:true,key_count:keys.length,attribute_count:keys.length,method:"btree",keys,collations:fields.map(row=>row.collation_name),collation_namespaces:fields.map(row=>row.collation_namespace),operator_classes:fields.map(row=>row.type_name==="uuid"?"uuid_ops":row.type_name==="int4"?"int4_ops":row.type_name==="int8"?"int8_ops":"text_ops"),operator_class_namespaces:fields.map(()=>"pg_catalog"),options:keys.map(()=>"0").join(" "),expressions:null,predicate:null,definition:`index ${table} (${keys.join(",")})`}) + } + for(const[table,key,parent]of[["bandwidth_external_node_aliases","node_id","bandwidth_nodes"],["bandwidth_external_node_events","enrollment_id","bandwidth_external_node_aliases"],["bandwidth_external_node_events","node_id","bandwidth_nodes"]]){ + const name=`${table}_${key}_fkey` + addConstraint(table,"f",[key],name,{foreign_namespace:"mmapp_control",foreign_table:parent,foreign_keys:[key],update_action:"r",delete_action:"r",match_type:"s"}) + for(const[relation,fn,type]of[[table,"RI_FKey_check_ins",5],[table,"RI_FKey_check_upd",17],[parent,"RI_FKey_restrict_del",9],[parent,"RI_FKey_restrict_upd",17]]as const)found.triggers.push({table_name:relation,name:null,internal:true,type,enabled:"O",deferrable:false,initially_deferred:false,is_constraint:true,constraint_name:name,function_namespace:"pg_catalog",function_name:fn,argument_count:0,arguments:"",columns:"",condition:null,old_table:null,new_table:null}) + } + return Object.entries(found).flatMap(([section,rows])=>rows.map(row=>({section,raw_json:JSON.stringify(row)}))) +} +const role=(external:boolean)=>({observation:{databaseName:"owned_database",sessionRole:"bandwidth_runtime",currentRole:"bandwidth_runtime",runtimeRoleExists:true,ownerRoleExists:true,runtimeFlagsSafe:true,ownerMembership:false,dangerousMembership:false,schemaExists:true,schemaOwner:"fixed_owner",schemaUsage:true,schemaCreate:false,publicSchemaUsage:false,publicSchemaCreate:false,tables:[external?"bandwidth_external_node_aliases":"bandwidth_node_aliases","bandwidth_nodes",external?"bandwidth_external_node_events":"bandwidth_node_events","bandwidth_node_authority"].map(name=>({name,exists:true,ownerRole:"fixed_owner",ownedByRuntime:false,privilegesExact:true}))}}) +function fixture(){ + const external=externalFixture(),managed=managedFixture() + managed.push(...external.filter(item=>item.section==="triggers"&&JSON.parse(item.raw_json!).table_name==="bandwidth_nodes")) + const incoming=external.filter(item=>item.section==="constraints").map(item=>JSON.parse(item.raw_json!) as Row).filter(row=>row.type==="f"&&row.foreign_table==="bandwidth_nodes").map(row=>({table_name:row.table_name,constraint_name:row.name})) + const expected:BandwidthPeerIdentityExpected={databaseName:"owned_database",runtimeRole:"bandwidth_runtime",ownerRole:"fixed_owner",installationId:"11111111-1111-4111-8111-111111111111",issuerKeySha256:"a".repeat(64),quiescenceProof:"b".repeat(64),registryCatalogSha256:validateBandwidthPeerRegistryCatalogRows(managed,external,incoming)} + const authority={installation_id:expected.installationId,issuer_key_sha256:expected.issuerKeySha256,quiescence_proof:expected.quiescenceProof,status:"active"} + const groups:Record={"managed-role":[role(false)],"external-role":[role(true)],"managed-catalog":managed,"external-catalog":external,incoming,authority:[authority]} + const rows:Batch[]=Object.entries(groups).flatMap(([kind,items])=>items.map(value=>({kind,row_order:"",raw_json:JSON.stringify(value)}))) + ordinal(rows);return{expected,rows,managed,external,incoming} +} +function ordinal(rows:Batch[]){rows.forEach((row,index)=>{row.row_order=String(index+1)});return rows} + +export {fixture}; diff --git a/backend/src/test/fixtures/configuration-audit-commit.ts b/backend/src/test/fixtures/configuration-audit-commit.ts new file mode 100644 index 00000000..836e3f81 --- /dev/null +++ b/backend/src/test/fixtures/configuration-audit-commit.ts @@ -0,0 +1,26 @@ +import { pool } from "../../db/index.js" +import type { PoolClient } from "pg" + +/** Trigger fixture drift after the intent is durable and before the native call. */ +export function afterConfigurationIntentCommit(kind: string, action: () => Promise) { + const connect = pool.connect + let fired = false + pool.connect = ((...connectArgs: unknown[]) => { + if (connectArgs.length) return Reflect.apply(connect, pool, connectArgs) + return (Reflect.apply(connect, pool, []) as Promise).then(client => { + let intent = false + return new Proxy(client, { get(target, key) { + if (key === "query") return async (...args: unknown[]) => { + const result = await Reflect.apply(target.query, target, args) + const sql = args[0], values = args[1] as unknown[] | undefined + if (typeof sql === "string" && sql.startsWith("INSERT INTO mcp_audit") && values?.[1] === "configuration.intent" && JSON.parse(String(values[3])).object?.kind === kind) intent = true + if (sql === "COMMIT" && intent && !fired) { fired = true; await action() } + return result + } + const value = Reflect.get(target, key) + return typeof value === "function" ? value.bind(target) : value + } }) + }) + }) as typeof pool.connect + return { fired: () => fired, restore() { pool.connect = connect } } +} diff --git a/backend/src/test/fixtures/managed-mutation-sql.ts b/backend/src/test/fixtures/managed-mutation-sql.ts new file mode 100644 index 00000000..26d977ee --- /dev/null +++ b/backend/src/test/fixtures/managed-mutation-sql.ts @@ -0,0 +1,66 @@ +/** Synthetic SQL only, for real localhost HTTP tests of the ORIGINAL managed + * mutation lock/policy. No arbitrary query or native guard is bypassed. */ +import assert from "node:assert/strict" +import { pool } from "../../db/index.js" +import { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK } from "../../db/operation-lock.js" + +// A fixture with no active controller must still execute the real writer guard. +// Match only its complete bounded query and the exact destination binding; +// other SQL continues to fail in the caller's strict fixture. +const activeRoutingProfilesSql = `SELECT id,version,connection_id,grant_expires_at, + CASE WHEN octet_length(config::text)<=262144 THEN config END AS config, + CASE WHEN octet_length(state::text)<=131072 THEN state END AS state, + (config IS NULL OR state IS NULL OR octet_length(config::text)>262144 OR octet_length(state::text)>131072) AS source_bounds_unavailable + FROM mcp_routing_profiles WHERE enabled AND (config->>'serverId'=$1 OR EXISTS( + SELECT 1 FROM jsonb_array_elements(config->'candidates') AS candidate + WHERE candidate->>'jumpHostId'=$1 OR candidate->>'exitNodeId'=$1)) ORDER BY id LIMIT 2` +export function emptyActiveRoutingProfilesSqlFixture(text: string, values: unknown[] | undefined, serverId: number) { + if (text !== activeRoutingProfilesSql) return undefined + assert.deepEqual(values, [String(serverId)]) + return { rows: [] } +} + +export function configurationAuditTransactionFixture(text: string, values?: unknown[]) { + if (text === "BEGIN" || text === "COMMIT" || text === "ROLLBACK") return { rows: [] } + if (text === "SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired") { + assert.deepEqual(values, [SYSTEM_DATABASE_REQUEST_LOCK]) + return { rows: [{ acquired: true }] } + } + if (text === "SELECT pg_catalog.pg_advisory_unlock_shared($1::bigint) AS released") { + assert.deepEqual(values, [SYSTEM_DATABASE_REQUEST_LOCK]) + return { rows: [{ released: true }] } + } + if (text !== "SELECT pg_catalog.pg_try_advisory_xact_lock_shared($1::bigint) AS acquired") return undefined + assert.deepEqual(values, [SYSTEM_DATABASE_OPERATION_LOCK]) + return { rows: [{ acquired: true }] } +} + +export function installManagedMutationSqlFixture(serverId: number) { + assert(Number.isSafeInteger(serverId) && serverId > 0) + const query = pool.query, connect = pool.connect + const actions: string[] = [] + pool.connect = (async () => ({ + async query(text: string, values?: unknown[]) { + const admission = configurationAuditTransactionFixture(text, values) + if (admission) return admission + if (text.startsWith("INSERT INTO mcp_audit(")) return pool.query(text, values) + assert(["SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired", "SELECT pg_advisory_unlock(hashtextextended($1,0))"].includes(text)) + assert.deepEqual(values, ["mm-config:" + serverId]) + return { rows: [{ acquired: true }] } + }, release() {}, + })) as typeof pool.connect + pool.query = (async (text: string, values?: unknown[]) => { + const routing = emptyActiveRoutingProfilesSqlFixture(text, values, serverId) + if (routing) return routing + if (text.startsWith("SELECT object_ref FROM mcp_protections WHERE")) { + assert.deepEqual(values, [String(serverId)]) + return { rows: [] } + } + assert.equal(text, "INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)") + assert.equal(JSON.parse(String(values?.[3])).serverId, serverId) + assert(["configuration.intent", "configuration.acknowledged", "configuration.unconfirmed"].includes(String(values?.[1]))) + actions.push(String(values?.[1])) + return { rows: [] } + }) as typeof pool.query + return { actions, restore() { pool.query = query; pool.connect = connect } } +} diff --git a/backend/src/test/flow-facts-rebuild-sql.ts b/backend/src/test/flow-facts-rebuild-sql.ts new file mode 100644 index 00000000..cad3b6c1 --- /dev/null +++ b/backend/src/test/flow-facts-rebuild-sql.ts @@ -0,0 +1,11 @@ +import assert from "node:assert/strict" + +// Bootstrap only the explicitly owned disposable database, never backend/.env. +const endpoint = new URL(process.env.DATABASE_URL ?? "postgresql://invalid") +assert.equal(process.env.MMAPP_OWN_FLOW_REBUILD, "true") +assert.equal(endpoint.hostname, "127.0.0.1") +assert.equal(endpoint.pathname, "/postgres") +assert.match(process.env.MCP_STATE_DIR ?? "", /mm-mcp-test-/) +const { pool } = await import("../db/index.js") +await (await import("../db/migrate.js")).applySqlMigrations(pool) +await import("../services/traffic-flow-facts-rebuild.pg.test.js") diff --git a/backend/src/test/legacy-flow-rebuild-sql.ts b/backend/src/test/legacy-flow-rebuild-sql.ts new file mode 100644 index 00000000..ac2bf0ae --- /dev/null +++ b/backend/src/test/legacy-flow-rebuild-sql.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict" +import { basename, dirname, resolve } from "node:path" + +// Existing PostgreSQL business regression, restricted to a fresh owned DB. +// Do not import its configured application pool until all guards pass. +assert.equal(process.env.MMAPP_OWN_LEGACY_FLOW_REBUILD, "true") +const endpoint = new URL(process.env.DATABASE_URL ?? "") +assert.equal(endpoint.hostname, "127.0.0.1") +assert.equal(endpoint.pathname, "/postgres") +assert(["postgres:", "postgresql:"].includes(endpoint.protocol)) +assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) +await import("../services/traffic-flow-facts-rebuild.test.js") diff --git a/backend/src/test/loopback-http-agent.test.ts b/backend/src/test/loopback-http-agent.test.ts new file mode 100644 index 00000000..9067b89c --- /dev/null +++ b/backend/src/test/loopback-http-agent.test.ts @@ -0,0 +1,37 @@ +import assert from "node:assert/strict" +import http from "node:http" +import { installLoopbackFixtureAgent } from "./loopback-http-agent.js" + +const original = http.request, ordinary = new http.Agent({ keepAlive: false }) +let ownConnections = 0, otherConnections = 0, drops = 0 +const own = http.createServer((req, res) => { if (req.url === "/drop") { drops++; req.socket.destroy(); return }; res.end("own") }) +const other = http.createServer((_req, res) => res.end("other")) +own.on("connection", () => ownConnections++); other.on("connection", () => otherConnections++) +assert.throws(() => installLoopbackFixtureAgent(own), /bound disposable/) +await new Promise(resolve => own.listen(0, "127.0.0.1", resolve)) +await new Promise(resolve => other.listen(0, "127.0.0.1", resolve)) +const get = (server: http.Server, path = "/") => new Promise((resolve, reject) => { + const req = http.request({ hostname: "127.0.0.1", port: (server.address() as { port: number }).port, method: "GET", path, agent: ordinary }, res => { + let body = ""; res.on("data", data => { body += data }); res.on("end", () => resolve(body)); res.on("error", reject) + }) + req.on("error", reject); req.end() +}) +const closeAgent = installLoopbackFixtureAgent(own) +try { + assert.equal(await get(own), "own"); assert.equal(await get(own), "own") + assert.equal(ownConnections, 1, "Only the exact fixture reuses its socket") + assert.equal(await get(other), "other"); assert.equal(await get(other), "other") + assert.equal(otherConnections, 2, "Other endpoints retain their supplied transport") + await assert.rejects(get(own, "/drop")) + assert.equal(drops, 1, "A lost response is never retried by the fixture adapter") + assert.equal(await get(own), "own"); assert.equal(ownConnections, 2) + closeAgent(); closeAgent() + assert.equal(http.request, original) + const before = ownConnections + await get(own); await get(own) + assert.equal(ownConnections, before + 2, "Production-style no-keepalive restored") +} finally { + closeAgent(); ordinary.destroy(); own.closeAllConnections(); other.closeAllConnections() + await Promise.all([new Promise(resolve => own.close(() => resolve())), new Promise(resolve => other.close(() => resolve()))]) +} +console.log("Loopback fixture agent: exact endpoint, bounded reuse, no lost-response retry, other transports and teardown preserved") diff --git a/backend/src/test/loopback-http-agent.ts b/backend/src/test/loopback-http-agent.ts new file mode 100644 index 00000000..6eafed4e --- /dev/null +++ b/backend/src/test/loopback-http-agent.ts @@ -0,0 +1,28 @@ +import http from "node:http" + +/** Disposable HTTP fixtures perform thousands of full catalog reads. Reuse at + * most two sockets to this exact bound fixture, avoiding Windows TIME_WAIT port + * exhaustion. The real request, authentication, parser and guards still run. + * Production agents and requests to every other endpoint are untouched. */ +export function installLoopbackFixtureAgent(server: http.Server): () => void { + const address = server.address() + if (!address || typeof address === "string" || address.address !== "127.0.0.1" || !Number.isSafeInteger(address.port) || address.port < 1) throw Error("Expected a bound disposable IPv4 loopback fixture") + const original = http.request, agent = new http.Agent({ keepAlive: true, maxSockets: 2, maxTotalSockets: 2, maxFreeSockets: 1 }) + const request = ((...args: unknown[]) => { + const options = args[0] + if (options && typeof options === "object" && !(options instanceof URL)) { + const value = options as http.RequestOptions + if (value.hostname === "127.0.0.1" && Number(value.port) === address.port) args[0] = { ...value, agent } + } + return Reflect.apply(original, http, args) as http.ClientRequest + }) as typeof http.request + http.request = request + let active = true + return () => { + if (!active) return + active = false + const nested = http.request !== request + http.request = original; agent.destroy() + if (nested) throw Error("Loopback fixture agent scopes must close in reverse order") + } +} diff --git a/backend/src/test/mcp-acme-settings.ts b/backend/src/test/mcp-acme-settings.ts new file mode 100644 index 00000000..2355d984 --- /dev/null +++ b/backend/src/test/mcp-acme-settings.ts @@ -0,0 +1,142 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { decryptSecret, encryptSecret } from "../modules/mcp/security.js" +import { findMcpService } from "../modules/mcp/service-registry.js" +import { ACME_CLOUDFLARE_SECRET_PURPOSE } from "../modules/mcp/acme-settings-actions.js" +import certificatesRoutes from "../routes/certificates.js" + +/** Isolated SQL/HTTP fixture only. No ACME account, Cloudflare, DNS or RouterOS requests. */ +export async function verifyMcpAcmeSettings(actor: AuthUser) { + const owner = { ...actor, id: "acme-settings-owner", permissions: [...new Set([...actor.permissions, "mm:certificates:write"])] } + const saved = (await pool.query("SELECT * FROM acme_settings WHERE id=1")).rows[0] + const ids = (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id")).rows.map(row => row.id) + assert.ok(ids.length > 0, "ACME acceptance needs scoped servers") + const issue = (name: string, serverIds = ids, permissions = ["mm:certificates:write"], allowGlobal = true) => createMcpConnection(mcpConnectionCreateSchema.parse({ name, mode: "approval", expiresAt: new Date(Date.now()+1800000).toISOString(), permissions, scope: { allowGlobal, serverIds, operationIds: ["certificates.acme.update"] }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner) + const issued = await issue("ACME settings"), principal = await authenticateMcpToken(issued.token) + const secretRef = randomUUID(), wrongRef = randomUUID(), otherRef = randomUUID(), serverRef = randomUUID(), protection = randomUUID(), busyJob = randomUUID() + const initial = "NEVER_ACME_SETTINGS_INITIAL", replacement = "NEVER_ACME_SETTINGS_REPLACEMENT", account = "NEVER_ACME_SETTINGS_ACCOUNT_KEY" + const originalQuery = pool.query.bind(pool), originalFetch = globalThis.fetch + let network = 0 + globalThis.fetch = (async () => { network++; throw Error("ACME settings must not dispatch HTTP") }) as typeof fetch + const rest = Fastify(); await rest.register(certificatesRoutes, { prefix: "/api" }) + const read = async () => (await originalQuery("SELECT * FROM acme_settings WHERE id=1")).rows[0] + const service = findMcpService("certificates.acme.update")! + assert.ok(service?.reconcile) + const internal = async (planId: string) => JSON.parse(await decryptSecret((await originalQuery("SELECT evidence FROM mcp_plans WHERE id=$1",[planId])).rows[0].evidence.sealed)).steps[0].input as Record + const prepare = (input: Record) => invokeMcpTool(principal, "mm_acme_settings_update_plan", input) as Promise<{ planId: string; state: string }> + const wait = async (id: string) => { + for(let i=0;i<600;i++) { + const row = (await originalQuery("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0] + if(!["pending","running"].includes(row.state)) return row + await delay(25) + } + throw Error("ACME settings still live; never replay") + } + const apply = async (input: Record) => { + const plan = await prepare(input); assert.equal(plan.state, "awaiting-approval") + assert.ok(!JSON.stringify(plan).includes("NEVER_ACME")); await approvePlan(plan.planId, owner, true) + const key = randomUUID(), op = await applyPlan(principal, plan.planId, key) + return { plan, key, op, ...await wait(op.id) } + } + try { + await originalQuery("INSERT INTO acme_settings(id,directory_url,default_zone_id,cloudflare_api_token,account_private_key) VALUES(1,'https://old.example/directory','old-zone',$1,$2) ON CONFLICT(id) DO UPDATE SET directory_url=EXCLUDED.directory_url,default_zone_id=EXCLUDED.default_zone_id,cloudflare_api_token=EXCLUDED.cloudflare_api_token,account_private_key=EXCLUDED.account_private_key", [initial,account]) + for (const [id,purpose,creator,serverId] of [[secretRef,ACME_CLOUDFLARE_SECRET_PURPOSE,owner.id,null],[wrongRef,"wrong-purpose",owner.id,null],[otherRef,ACME_CLOUDFLARE_SECRET_PURPOSE,"other-owner",null],[serverRef,ACME_CLOUDFLARE_SECRET_PURPOSE,owner.id,ids[0]]] as const) await originalQuery("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,$3,$4,$5)", [id,purpose,serverId,await encryptSecret(replacement),creator]) + for (const input of [{cloudflareApiToken:replacement},{settings:{cloudflareApiToken:replacement}},{secretRef:wrongRef},{secretRef:otherRef},{secretRef:serverRef}]) await assert.rejects(prepare(input)) + for (const [name,scope,permissions,global] of [["partial",ids.slice(1),["mm:certificates:write"],true],["wrong-permission",ids,["mm:settings:write"],true],["no-global",ids,["mm:certificates:write"],false]] as const) { + const denied = await issue(name,[...scope],[...permissions],global), deniedPrincipal = await authenticateMcpToken(denied.token) + let secretReads = 0 + pool.query = ((...args: unknown[]) => { + const sql = typeof args[0] === "string" ? args[0] : (args[0] as {text?: string})?.text + if (sql?.includes("FROM acme_settings") || sql?.includes("FROM mcp_secrets")) secretReads++ + return (originalQuery as (...a: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { await assert.rejects(invokeMcpTool(deniedPrincipal,"mm_acme_settings_update_plan",{secretRef})); assert.equal(secretReads,0) } + finally { pool.query = originalQuery as typeof pool.query; await setConnectionStatus(denied.connection.id,"paused",owner) } + } + await originalQuery("INSERT INTO certificate_issue_jobs(id,server_id,cert_name,domain_names) VALUES($1,$2,'acceptance-busy','[]'::jsonb)",[busyJob,ids[0]]) + await assert.rejects(prepare({secretRef})); await originalQuery("DELETE FROM certificate_issue_jobs WHERE id=$1",[busyJob]) + await originalQuery("UPDATE acme_settings SET cloudflare_api_token=$1 WHERE id=1",["x".repeat(4097)]) + await assert.rejects(prepare({})); await originalQuery("UPDATE acme_settings SET cloudflare_api_token=$1 WHERE id=1",[initial]) + const unapproved = await prepare({settings:{defaultZoneId:"new"}}) + const originalInput = await internal(unapproved.planId) + assert.deepEqual(await service.reconcile!(originalInput,async()=>{}),{matchesDesiredState:false,matchesOriginalState:true}) + await assert.rejects(service.reconcile!({...originalInput,proofRevision:undefined},async()=>{})) + await assert.rejects(applyPlan(principal,unapproved.planId,randomUUID())); assert.equal((await read()).default_zone_id,"old-zone") + let result = await apply({settings:{directoryUrl:"https://new.example/directory",defaultZoneId:" zone "}}) + assert.equal(result.state,"applied"); assert.equal((await read()).cloudflare_api_token,initial); assert.equal((await read()).default_zone_id,"zone") + const epoch = (await read()).updated_at + await applyPlan(principal,result.plan.planId,result.key); assert.deepEqual((await read()).updated_at,epoch) + const verifiedInput = await internal(result.plan.planId) + assert.deepEqual(await service.reconcile!(verifiedInput,async()=>{}),{matchesDesiredState:true,matchesOriginalState:false}) + assert.equal((await service.reconcile!({...verifiedInput,receiptId:randomUUID()},async()=>{})).matchesDesiredState,false) + assert.equal((await service.reconcile!({...verifiedInput,settings:{defaultZoneId:"foreign-intent"}},async()=>{})).matchesDesiredState,false) + const checkpoint = (await originalQuery("SELECT intended_state FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0",[result.op.id])).rows[0].intended_state + await originalQuery("UPDATE mcp_operation_steps SET intended_state=jsonb_set(intended_state,'{service}','\"foreign.service\"'::jsonb) WHERE operation_id=$1",[result.op.id]) + assert.equal((await service.reconcile!(verifiedInput,async()=>{})).matchesDesiredState,false) + await originalQuery("UPDATE mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1",[result.op.id,JSON.stringify(checkpoint)]) + await originalQuery("UPDATE mcp_operation_steps SET state='dispatched' WHERE operation_id=$1",[result.op.id]) + assert.equal((await service.reconcile!(verifiedInput,async()=>{})).matchesDesiredState,false) + await originalQuery("UPDATE mcp_operation_steps SET state='verified' WHERE operation_id=$1",[result.op.id]) + result = await apply({secretRef,settings:{defaultZoneId:null}}); assert.equal(result.state,"applied"); assert.equal((await read()).cloudflare_api_token,replacement); assert.equal((await read()).default_zone_id,"") + result = await apply({secretRef:null}); assert.equal(result.state,"applied",JSON.stringify({state:result.state,errorCode:result.error_code})); assert.equal((await read()).cloudflare_api_token,"") + for (const token of [initial,"",null]) { + const response = await rest.inject({method:"PUT",url:"/api/certificates/acme-settings",payload:{directoryUrl:"https://rest.example/directory",defaultZoneId:" rest-zone ",cloudflareApiToken:token}}) + assert.equal(response.statusCode,200,response.body); assert.equal(response.json().tokenConfigured,Boolean(token)); assert.ok(!response.body.includes("NEVER_ACME")); assert.equal((await read()).cloudflare_api_token,token??"") + } + assert.equal((await read()).account_private_key,account) + const rotated = await encryptSecret("NEVER_ACME_SETTINGS_ROTATED") + for (const mutate of [() => originalQuery("UPDATE acme_settings SET updated_at=updated_at+interval '1 second' WHERE id=1"), () => originalQuery("UPDATE acme_settings SET default_zone_id=default_zone_id WHERE id=1"), () => originalQuery("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1",[secretRef,rotated])]) { + // The reference ciphertext is generated outside the SQL callback. + const plan = await prepare({secretRef}); await approvePlan(plan.planId,owner,true); await mutate() + assert.equal((await wait((await applyPlan(principal,plan.planId,randomUUID())).id)).state,"rejected") + } + await originalQuery("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1",[secretRef,await encryptSecret(replacement)]) + await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"acme-settings",id:"1",protection:"deny"}),owner.id]) + await assert.rejects(prepare({secretRef})); assert.equal((await rest.inject({method:"PUT",url:"/api/certificates/acme-settings",payload:{defaultZoneId:"blocked"}})).statusCode,409) + await originalQuery("DELETE FROM mcp_protections WHERE id=$1",[protection]) + for(const mode of ["late-intent","ignored","partial","lost"]) { + const plan = await prepare({secretRef,settings:{defaultZoneId:"desired-"+mode}}); await approvePlan(plan.planId,owner,true) + let fired = false, writes = 0 + pool.query = ((...args: unknown[]) => { + const sql = typeof args[0] === "string" ? args[0] : (args[0] as {text?: string})?.text, params = args[1] as unknown[] | undefined + if (!fired && mode === "late-intent" && sql?.startsWith("INSERT INTO mcp_audit") && params?.[1] === "configuration.intent" && String(params?.[3]).includes('"kind":"acme-settings"')) { + fired = true; return (originalQuery as (...a: unknown[]) => Promise)(...args).then(async value => {await originalQuery("UPDATE acme_settings SET updated_at=updated_at+interval '1 second' WHERE id=1");return value}) + } + if(sql?.startsWith("UPDATE acme_settings SET directory_url=$1") && sql.includes("RETURNING id")) { + writes++; fired = true + if(mode === "ignored") return Promise.resolve({rows:[{id:1}],rowCount:1}) + if(mode === "partial") return originalQuery("UPDATE acme_settings SET default_zone_id=$1 WHERE id=1 RETURNING id",[params?.[1]]) + if(mode === "lost") return (originalQuery as (...a: unknown[]) => Promise)(...args).then(()=>{throw Error("lost SQL acknowledgement")}) + } + return (originalQuery as (...a: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { + const key = randomUUID(), op = await applyPlan(principal,plan.planId,key), outcome = await wait(op.id) + assert.equal(outcome.state,mode === "ignored" || mode === "partial" ? "partial" : "unknown"); assert.equal(fired,true); assert.equal(writes,mode === "late-intent"?0:1) + if(mode === "lost") { + assert.equal((await read()).default_zone_id,"desired-lost"); assert.equal((await read()).cloudflare_api_token,replacement) + assert.deepEqual(await service.reconcile!(await internal(plan.planId),async()=>{}),{matchesDesiredState:false,matchesOriginalState:false}) + } + await applyPlan(principal,plan.planId,key); assert.equal(writes,mode === "late-intent"?0:1) + } finally { pool.query = originalQuery as typeof pool.query } + } + const cancelled = new AbortController(); cancelled.abort(); await assert.rejects(invokeMcpTool(principal,"mm_acme_settings_update_plan",{},cancelled.signal)) + const paused = await prepare({settings:{defaultZoneId:"revoked"}}); await approvePlan(paused.planId,owner,true) + await setConnectionStatus(issued.connection.id,"paused",owner); await assert.rejects(applyPlan(principal,paused.planId,randomUUID())); await assert.rejects(prepare({})) + const persisted = await originalQuery("SELECT payload FROM mcp_audit WHERE connection_id=$1 UNION ALL SELECT payload FROM mcp_plans WHERE connection_id=$1",[issued.connection.id]) + assert.ok(!JSON.stringify(persisted.rows).includes("NEVER_ACME")); assert.equal(network,0) + console.log("✓ ACME settings: shared REST/MCP fields and retain/replace/clear, full-scope-before-secrets, owner/purpose refs and rotation, approval/protection, epoch drift, late intent, ignored/partial/lost SQL, revocation, no replay or secret leak; no provider I/O") + } finally { + pool.query = originalQuery as typeof pool.query; globalThis.fetch = originalFetch; await rest.close() + await setConnectionStatus(issued.connection.id,"paused",owner); await originalQuery("DELETE FROM certificate_issue_jobs WHERE id=$1",[busyJob]); await originalQuery("DELETE FROM mcp_protections WHERE id=$1",[protection]); await originalQuery("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])",[[secretRef,wrongRef,otherRef,serverRef]]) + if(saved) await originalQuery("UPDATE acme_settings SET directory_url=$1,cloudflare_api_token=$2,default_zone_id=$3,account_private_key=$4,updated_at=$5 WHERE id=1",[saved.directory_url,saved.cloudflare_api_token,saved.default_zone_id,saved.account_private_key,saved.updated_at]) + else await originalQuery("DELETE FROM acme_settings WHERE id=1") + } +} diff --git a/backend/src/test/mcp-acme-test.ts b/backend/src/test/mcp-acme-test.ts new file mode 100644 index 00000000..d67723c2 --- /dev/null +++ b/backend/src/test/mcp-acme-test.ts @@ -0,0 +1,103 @@ +import assert from "node:assert/strict" +import { afterConfigurationIntentCommit } from "./fixtures/configuration-audit-commit.js" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import certificatesRoutes from "../routes/certificates.js" + +/** Run only inside the isolated MCP PostgreSQL fixture, after action registration. */ +export async function verifyMcpAcmeTest(actor: AuthUser) { + const owner = { ...actor, id: "acme-test-owner", permissions: [...new Set([...actor.permissions, "mm:certificates:write"])] } + const saved = (await pool.query("SELECT * FROM acme_settings WHERE id=1")).rows[0] + const ids = (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id")).rows.map(row => row.id) + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "ACME test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:certificates:write"], scope: { allowGlobal: true, serverIds: ids, operationIds: ["certificates.acme.test"] }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner) + const principal = await authenticateMcpToken(issued.token), protection = randomUUID() + const originalFetch = globalThis.fetch, token = "NEVER_ACME_TEST_TOKEN" + let calls = 0, mode = "accepted" + let beforeResponse: (() => Promise) | undefined + const server = createServer(async (req, res) => { + calls++; assert.equal(req.method, "GET"); assert.equal(req.headers.authorization, `Bearer ${token}`) + if (mode === "lost") { req.socket.destroy(); return } + if (beforeResponse) try { await beforeResponse() } catch { res.destroy(); return } + res.writeHead(mode === "rejected" ? 403 : 200, { "content-type": "application/json" }) + res.end(mode === "rejected" ? JSON.stringify({ success: false, errors: [{ message: token }] }) : JSON.stringify({ success: true, result: [{ name: "PRIVATE_ZONE" }] })) + }) + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const local = `http://127.0.0.1:${(server.address() as { port: number }).port}/zones` + globalThis.fetch = ((url, init) => { assert.equal(String(url), "https://api.cloudflare.com/client/v4/zones?per_page=5"); assert.equal(init?.redirect, "error"); return originalFetch(local, init) }) as typeof fetch + const rest = Fastify(); await rest.register(certificatesRoutes, { prefix: "/api" }) + const prepare = () => invokeMcpTool(principal, "mm_acme_settings_test_plan", {}) as Promise<{ planId: string; state: string }> + const wait = async (id: string) => { + for (let i = 0; i < 600; i++) { + const row = (await pool.query("SELECT state,error_code,verification FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (!["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw Error("ACME operation still live; do not replay") + } + try { + await pool.query("INSERT INTO acme_settings(id,cloudflare_api_token) VALUES(1,$1) ON CONFLICT(id) DO UPDATE SET cloudflare_api_token=$1", [token]) + for(const scenario of ["observer","no-global","missing-server","wrong-permission"]){ + assert.ok(ids.length>0,"Acceptance requires scoped source servers") + const denied=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"ACME denied "+scenario,mode:scenario==="observer"?"observer":"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:[scenario==="wrong-permission"?"mm:certificates:read":"mm:certificates:write"],scope:{allowGlobal:scenario!=="no-global",serverIds:scenario==="missing-server"?ids.slice(1):ids,operationIds:["certificates.acme.test"]},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + const deniedPrincipal=await authenticateMcpToken(denied.token),beforeDenied=calls + await assert.rejects(invokeMcpTool(deniedPrincipal,"mm_acme_settings_test_plan",{})) + assert.equal(calls,beforeDenied,"Denied policy must issue no provider request") + const deniedAudit=await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[denied.connection.id]) + assert.ok(!JSON.stringify(deniedAudit.rows).includes(token)) + await setConnectionStatus(denied.connection.id,"paused",owner) + } + const unapproved = await prepare(); assert.equal(unapproved.state, "awaiting-approval") + assert.ok(!JSON.stringify(unapproved).includes(token)); assert.equal(calls, 0) + await assert.rejects(applyPlan(principal, unapproved.planId, randomUUID())); assert.equal(calls, 0) + for (const scenario of ["accepted", "rejected", "lost"]) { + mode = scenario + const plan = await prepare(); await approvePlan(plan.planId, owner, true) + const key = randomUUID(), before: number = calls, op = await applyPlan(principal, plan.planId, key) + assert.equal((await wait(op.id)).state, scenario === "accepted" ? "applied" : scenario === "rejected" ? "partial" : "unknown") + assert.equal(calls, before + 1) + await applyPlan(principal, plan.planId, key); assert.equal(calls, before + 1) + } + mode = "accepted" + const response = await rest.inject({ method: "POST", url: "/api/certificates/acme-settings/test", payload: {} }) + assert.equal(response.statusCode, 200, response.body); assert.equal(response.json().outcome, "accepted") + const stale = await prepare(); await approvePlan(stale.planId, owner, true) + await pool.query("UPDATE acme_settings SET cloudflare_api_token='CHANGED' WHERE id=1") + const before = calls + assert.equal((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "rejected"); assert.equal(calls, before) + await pool.query("UPDATE acme_settings SET cloudflare_api_token=$1 WHERE id=1", [token]) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "acme-settings", id: "1", protection: "deny" }), owner.id]) + await assert.rejects(prepare()) + assert.equal((await rest.inject({ method: "POST", url: "/api/certificates/acme-settings/test", payload: {} })).statusCode, 409); assert.equal(calls, before) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + const cancelled = new AbortController(); cancelled.abort() + await assert.rejects(invokeMcpTool(principal, "mm_acme_settings_test_plan", {}, cancelled.signal)) + await assert.rejects(invokeMcpTool(principal, "mm_acme_settings_test_plan", { cloudflareApiToken: token })) + const late = await prepare(); await approvePlan(late.planId, owner, true) + const drift = afterConfigurationIntentCommit("acme-settings-test", () => pool.query("UPDATE acme_settings SET cloudflare_api_token='LATE_CHANGED' WHERE id=1")) + try { assert.equal((await wait((await applyPlan(principal, late.planId, randomUUID())).id)).state, "unknown"); assert(drift.fired()); assert.equal(calls, before) } + finally { drift.restore() } + const audit = await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1", [issued.connection.id]) + assert.ok(!JSON.stringify(audit.rows).includes(token)); assert.ok(!JSON.stringify(audit.rows).includes("PRIVATE_ZONE")) + await pool.query("UPDATE acme_settings SET cloudflare_api_token=$1 WHERE id=1", [token]) + const revoked = await prepare(); await approvePlan(revoked.planId, owner, true) + beforeResponse = () => setConnectionStatus(issued.connection.id, "paused", owner).then(() => undefined) + const beforeRevoked: number = calls + const revokedState = await wait((await applyPlan(principal, revoked.planId, randomUUID())).id) + assert.notEqual(revokedState.state, "applied"); assert.equal(calls, beforeRevoked + 1) + await assert.rejects(prepare()) + console.log("✓ ACME token test: shared REST/MCP local HTTP, approval, saved secret, stale/late intent fences, protection, cancellation, pause and no replay; no external ACME") + } finally { + globalThis.fetch = originalFetch + await rest.close(); server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())) + await setConnectionStatus(issued.connection.id, "paused", owner); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + if (saved) await pool.query("UPDATE acme_settings SET directory_url=$1,cloudflare_api_token=$2,default_zone_id=$3,account_private_key=$4,updated_at=$5 WHERE id=1", [saved.directory_url, saved.cloudflare_api_token, saved.default_zone_id, saved.account_private_key, saved.updated_at]) + else await pool.query("DELETE FROM acme_settings WHERE id=1") + } +} diff --git a/backend/src/test/mcp-alerts-outbox.ts b/backend/src/test/mcp-alerts-outbox.ts new file mode 100644 index 00000000..1c517686 --- /dev/null +++ b/backend/src/test/mcp-alerts-outbox.ts @@ -0,0 +1,142 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import type { PoolClient } from "pg" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { createMcpConnection, authenticateMcpToken } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { retryUnknownOutbox } from "../services/alert-engine/outbox.js" +import alertsRoutes from "../routes/alerts.js" + +export async function verifyMcpAlertsOutbox(actor: AuthUser) { + const owner = { ...actor, id: "alerts-retry-" + randomUUID(), permissions: [...actor.permissions, "mm:alerts:write"] } + const id = "mcp-retry-" + randomUUID(), protection = randomUUID(), connections: string[] = [] + const secret = "NEVER_OUTBOX_RETRY_MESSAGE", chatId = "NEVER_OUTBOX_RETRY_RECIPIENT" + const payload = { text: secret, chatId, history: { id: randomUUID(), ruleId: null, groupId: null, ruleName: secret, severity: "warning", message: secret, firedAt: new Date().toISOString() } } + const ids = (await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id)) + const create = async (serverIds = ids, allowGlobal = true, mode = "approval") => { + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: id, mode, permissions: ["mm:alerts:write"], expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds, allowGlobal, operationIds: ["alerts.outbox.retry"] }, ...(mode === "autonomous" ? { dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["alerts.outbox.retry"] } : {}), limits: { callsPerMinute: 120, mutationsPerHour: 200 } }), owner) + connections.push(issued.connection.id) + return { issued, principal: await authenticateMcpToken(issued.token) } + } + const { principal } = await create() + const input = { id, expectedVersion: 0, acknowledgedDuplicateRisk: true } + const plan = (p = principal) => invokeMcpTool(p, "mm_alerts_outbox_retry_plan", input) as Promise<{ planId: string; changes: { warnings: string[]; requiresHuman: boolean; verificationScope: string } }> + const wait = async (op: string) => { + for (let n = 0; n < 400; n++) { + const row = (await pool.query("SELECT state,verification FROM mcp_operations WHERE id=$1", [op])).rows[0] + if (row && !["pending", "running"].includes(row.state)) return row + await delay(25) + } + throw new Error("Outbox operation did not finish; do not replay") + } + const reset = () => pool.query("UPDATE alert_outbox SET status='unknown',resolution_version=0,payload_json=$2::jsonb WHERE id=$1", [id, JSON.stringify(payload)]) + const row = async () => (await pool.query("SELECT status,resolution_version FROM alert_outbox WHERE id=$1", [id])).rows[0] + const http = Fastify(); await http.register(alertsRoutes, { prefix: "/api" }) + const originalFetch = globalThis.fetch; let senderCalls = 0 + globalThis.fetch = async (request, options) => { + const url = new URL(typeof request === "string" ? request : request instanceof URL ? request.href : request.url) + if (url.hostname === "api.telegram.org") { senderCalls++; throw new Error("External sender forbidden in queue admission test") } + return originalFetch(request, options) + } + try { + await pool.query("INSERT INTO alert_outbox(id,dedupe_key,status,next_attempt_at,payload_json) VALUES($1,$1,'unknown',now(),$2::jsonb)", [id, JSON.stringify(payload)]) + for (const bad of [{ id, expectedVersion: 0 }, { ...input, acknowledgedDuplicateRisk: false }, { ...input, sourceProof: "invented" }]) await assert.rejects(invokeMcpTool(principal, "mm_alerts_outbox_retry_plan", bad)) + for (const status of ["pending", "processing", "sent", "failed"]) { + await pool.query("UPDATE alert_outbox SET status=$2 WHERE id=$1", [id, status]); await assert.rejects(plan()) + } + await reset() + await assert.rejects(invokeMcpTool(principal, "mm_alerts_outbox_retry_plan", { ...input, expectedVersion: 1 })) + await assert.rejects(plan((await create(ids, false)).principal)) + await assert.rejects(plan((await create(ids.slice(1))).principal)) + await assert.rejects(plan((await create(ids, true, "observer")).principal)) + const autonomous = await create(ids, true, "autonomous"), manualOnly = await plan(autonomous.principal) + await assert.rejects(applyPlan(autonomous.principal, manualOnly.planId, randomUUID())) + assert.deepEqual(await row(), { status: "unknown", resolution_version: 0 }) + const first = await plan() + assert.equal(first.changes.requiresHuman, true); assert.equal(first.changes.verificationScope, "acknowledgement") + assert.ok(first.changes.warnings.some(value => value.includes("дубликат"))) + assert.ok(!JSON.stringify(first).includes("NEVER_OUTBOX")) + await assert.rejects(applyPlan(principal, first.planId, randomUUID())) + await approvePlan(first.planId, owner, true) + const key = randomUUID(), operation = await applyPlan(principal, first.planId, key) + assert.deepEqual(await wait(operation.id), { state: "applied", verification: "inconclusive" }) + assert.deepEqual(await row(), { status: "pending", resolution_version: 1 }) + assert.equal((await applyPlan(principal, first.planId, key)).id, operation.id) + assert.equal((await row()).resolution_version, 1) + assert.equal((await pool.query("SELECT state FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0].state, "acknowledged") + await reset() + const twin = await Promise.all([plan(), plan()]); for (const candidate of twin) await approvePlan(candidate.planId, owner, true) + const twins = await Promise.all(twin.map(candidate => applyPlan(principal, candidate.planId, randomUUID()))) + assert.deepEqual((await Promise.all(twins.map(op => wait(op.id)))).map(op => op.state).sort(), ["applied", "rejected"]) + assert.deepEqual(await row(), { status: "pending", resolution_version: 1 }) + await reset() + const stale = await plan(); await approvePlan(stale.planId, owner, true) + await pool.query("UPDATE alert_outbox SET payload_json=jsonb_set(payload_json,'{chatId}','\"changed-recipient\"') WHERE id=$1", [id]) + assert.equal((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "rejected") + assert.deepEqual(await row(), { status: "unknown", resolution_version: 0 }); await reset() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'fixture')", [protection, JSON.stringify({ kind: "alerts-outbox", id, protection: "deny" })]) + await assert.rejects(plan()) + await assert.rejects(retryUnknownOutbox(id, { expectedVersion: 0, acknowledgedDuplicateRisk: true }), (error: { code?: string }) => error.code === "PROTECTED_OBJECT") + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + const rest = await http.inject({ method: "POST", url: "/api/alerts/outbox/" + id + "/retry", payload: { expectedVersion: 0, acknowledgedDuplicateRisk: true } }) + assert.equal(rest.statusCode, 200, rest.body); assert.equal(rest.json().resolutionVersion, 1); await reset() + await pool.query("UPDATE alert_outbox SET payload_json=jsonb_build_object('text',repeat('x',1048577)) WHERE id=$1", [id]) + await assert.rejects(plan(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE"); await reset() + for (const triggerBody of ["NEW.payload_json := jsonb_set(NEW.payload_json,'{chatId}','\"changed-by-trigger\"');", "NEW.status := OLD.status; NEW.resolution_version := OLD.resolution_version;"]) { + await pool.query("CREATE FUNCTION mcp_retry_write_fault() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.id LIKE 'mcp-retry-%' THEN " + triggerBody + " END IF; RETURN NEW; END $$") + await pool.query("CREATE TRIGGER mcp_retry_write_fault BEFORE UPDATE ON alert_outbox FOR EACH ROW EXECUTE FUNCTION mcp_retry_write_fault()") + try { + const candidate = await plan(); await approvePlan(candidate.planId, owner, true) + const op = await applyPlan(principal, candidate.planId, randomUUID()) + assert.equal((await wait(op.id)).state, "unknown") + assert.deepEqual(await row(), { status: "unknown", resolution_version: 0 }) + assert.deepEqual((await pool.query("SELECT payload_json FROM alert_outbox WHERE id=$1", [id])).rows[0].payload_json, payload, "Changed recipient or ignored write must roll back before commit") + } finally { await pool.query("DROP TRIGGER mcp_retry_write_fault ON alert_outbox"); await pool.query("DROP FUNCTION mcp_retry_write_fault()") } + } + // Block the transactional source lock after ordinary source/admission checks. + const race = async (change: (blocker: PoolClient, connectionId: string, credentialId: string) => Promise, accessRevoked = false) => { + const access = await create(), candidate = await plan(access.principal); await approvePlan(candidate.planId, owner, true) + const blocker = await pool.connect(); let operationId: string | undefined + try { + await blocker.query("BEGIN"); await blocker.query("SELECT id FROM alert_outbox WHERE id=$1 FOR UPDATE", [id]) + const key = randomUUID(), op = await applyPlan(access.principal, candidate.planId, key); operationId = op.id + let reached = false + for (let n = 0; n < 160; n++) { + reached = (await pool.query("SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE '%to_jsonb(o)%FOR UPDATE OF o%') AS reached")).rows[0].reached + if (reached) break + await delay(25) + } + assert.equal(reached, true, "Race must reach transactional outbox source lock") + await change(blocker, access.issued.connection.id, access.principal.credentialId); await blocker.query("COMMIT") + assert.equal((await wait(op.id)).state, "unknown") + assert.deepEqual(await row(), { status: "unknown", resolution_version: 0 }) + if (accessRevoked) await assert.rejects(applyPlan(access.principal, candidate.planId, key)) + else assert.equal((await applyPlan(access.principal, candidate.planId, key)).id, op.id) + } finally { await blocker.query("ROLLBACK"); blocker.release(); if (operationId) await wait(operationId) } + } + await race(blocker => blocker.query("UPDATE alert_outbox SET payload_json=jsonb_set(payload_json,'{text}','\"changed-message\"') WHERE id=$1", [id])); await reset() + await race((_blocker, connectionId) => pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [connectionId])) + await race((_blocker, connectionId) => pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1", [connectionId]), true) + await race((_blocker, connectionId, credentialId) => { + const grantId = randomUUID() + return pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grantId, connectionId, JSON.stringify({ id: grantId, credentialId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(), scope: { allowGlobal: false, serverIds: [], operationIds: [] } })]) + }) + await race(() => pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'fixture')", [protection, JSON.stringify({ kind: "alerts-outbox", id, protection: "deny" })])) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + await race(() => pool.query("INSERT INTO servers(id,name,host) VALUES(99991,'outbox-late-catalog','192.0.2.91')")); await pool.query("DELETE FROM servers WHERE id=99991") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM alert_history WHERE message LIKE '%NEVER_OUTBOX%' ")).rows[0].n, 0) + assert.equal(senderCalls, 0, "Queue admission must not invoke the external sender") + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=ANY($1::uuid[])", [connections])).rows).includes("NEVER_OUTBOX")) + assert.ok(!JSON.stringify((await pool.query("SELECT result,intended_state FROM mcp_operation_steps WHERE operation_id IN(SELECT id FROM mcp_operations WHERE connection_id=ANY($1::uuid[]))", [connections])).rows).includes("NEVER_OUTBOX")) + console.log("✓ Outbox MCP retry: shared REST/CAS, mandatory human duplicate acknowledgement, bounded private source, concurrent/idempotent queue admission, stale/protected/grant/owner/pause/catalog row-lock races; no delivery or external sender") + } finally { + globalThis.fetch = originalFetch + await http.close(); await pool.query("DELETE FROM servers WHERE id=99991") + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]); await pool.query("DELETE FROM alert_outbox WHERE id=$1", [id]) + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=ANY($1::uuid[])", [connections]) + } +} diff --git a/backend/src/test/mcp-backup-download.ts b/backend/src/test/mcp-backup-download.ts new file mode 100644 index 00000000..1b3c5951 --- /dev/null +++ b/backend/src/test/mcp-backup-download.ts @@ -0,0 +1,119 @@ +import assert from "node:assert/strict" +import type { FastifyInstance } from "fastify" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { mkdir, writeFile, rm } from "node:fs/promises" +import path from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { env } from "../config.js" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { loadLogExportArtifactMetadata } from "../modules/mcp/log-artifacts.js" +import { readHumanBackupDownloadArtifact, readBackupDownloadSource } from "../modules/mcp/backup-download-artifact.js" + +/** Real SQL/HTTP helper; run only inside the isolated MCP integration database. */ +export async function verifyMcpBackupDownload(actor:AuthUser,app:FastifyInstance) { + const owner={...actor,id:"backup-download-owner",permissions:[...actor.permissions,"mm:backups:write"]} + const serverId=Number((await pool.query("SELECT id FROM servers ORDER BY id LIMIT 1")).rows[0].id) + const id=randomUUID(),filename=`mcp-download-${id}.rsc`,file=path.resolve(process.cwd(),"storage","backups",filename) + const content=Buffer.concat([Buffer.from("# synthetic backup\npassword=NEVER_MCP_BACKUP_SECRET\n"),Buffer.from([0,255,128,1])]) + const saved=(await pool.query("SELECT * FROM backup_storage_settings WHERE id=1")).rows[0] + assert.ok(saved,"Fixture expects initialized backup settings") + let remote=false,httpReads=0 + const http=createServer((_req,res)=>{httpReads++;if(!remote){res.statusCode=404;res.end("NoSuchKey");return}res.end(content)}) + await new Promise(resolve=>http.listen(0,"127.0.0.1",resolve)) + const address=http.address();assert.ok(address&&typeof address!=="string") + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Backup reveal fixture",mode:"autonomous",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:backups:write"],scope:{serverIds:[serverId],operationIds:["backups.download"],allowGlobal:false},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + const principal=await authenticateMcpToken(issued.token) + const jwt=await new SignJWT({sub:owner.id,apps:["mm"],permissions:owner.permissions}).setProtectedHeader({alg:"HS256"}).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + const headers={authorization:"Bearer "+jwt},prepare=()=>invokeMcpTool(principal,"mm_backup_download_plan",{backupId:id}) as Promise<{planId:string;state:string}> + const wait=async(operationId:string)=>{for(let n=0;n<600;n++){const row=(await pool.query("SELECT state,result FROM mcp_operations WHERE id=$1",[operationId])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("Backup export still active; do not replay")} + const originalQuery=pool.query.bind(pool) + try { + await mkdir(path.dirname(file),{recursive:true});await writeFile(file,content) + await pool.query("INSERT INTO backup_entries(id,server_id,server_name,filename,size_bytes,kind,storage,s3_key,created_at) VALUES($1,$2,'fixture',$3,$4,'manual','both','fixture.rsc',now())",[id,serverId,filename,content.length]) + await pool.query("UPDATE backup_storage_settings SET provider='s3',s3_endpoint=$1,s3_region='us-east-1',s3_bucket='fixture',s3_access_key_id='fixture',s3_secret_access_key='fixture',s3_force_path_style=true WHERE id=1",[`http://127.0.0.1:${address.port}`]) + const narrow=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Denied whole backup scope",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:backups:write"],scope:{serverIds:[],operationIds:["backups.download"],allowGlobal:false}}),owner) + const beforeDenied=httpReads + try{await assert.rejects(invokeMcpTool(await authenticateMcpToken(narrow.token),"mm_backup_download_plan",{backupId:id}));assert.equal(httpReads,beforeDenied)}finally{await setConnectionStatus(narrow.connection.id,"paused",owner)} + await pool.query("UPDATE backup_entries SET notes=repeat('x',32769) WHERE id=$1",[id]);await assert.rejects(prepare());await pool.query("UPDATE backup_entries SET notes=NULL WHERE id=$1",[id]) + const plan=await prepare();assert.equal(plan.state,"awaiting-approval");assert.ok(!JSON.stringify(plan).includes("NEVER_MCP_BACKUP_SECRET")) + await assert.rejects(applyPlan(principal,plan.planId,randomUUID())) + await approvePlan(plan.planId,owner,true) + const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key),done=await wait(operation.id) + assert.equal(done.state,"applied",JSON.stringify(done));assert.ok(!JSON.stringify(done).includes("NEVER_MCP_BACKUP_SECRET")) + const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0",[operation.id])).rows[0].result + assert.equal(receipt.humanDownloadOnly,true);assert.equal(receipt.backupContentReturned,false) + const listUrl=`/api/mcp/operations?connectionId=${issued.connection.id}` + const listed=async(auth=headers)=>{const response=await app.inject({url:listUrl,headers:auth});assert.equal(response.statusCode,200,response.body);return response.json().items.find((item:{id:string})=>item.id===operation.id)} + const readsBeforeList=httpReads + assert.deepEqual((await listed()).artifactReceipts,[{artifactId:receipt.artifactId,bytes:receipt.bytes,sha256:receipt.sha256,humanDownloadOnly:true,backupContentReturned:false,downloadPath:receipt.downloadPath}]) + assert.equal(httpReads,readsBeforeList,"Listing receipts must never read S3 content") + assert.ok(!JSON.stringify(await listed()).includes("NEVER_MCP_BACKUP_SECRET")) + const foreignJwt=await new SignJWT({sub:"foreign-admin",apps:["mm"],permissions:[...owner.permissions,"mm:mcp:admin"]}).setProtectedHeader({alg:"HS256"}).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + assert.deepEqual((await listed({authorization:"Bearer "+foreignJwt})).artifactReceipts,[]) + const noBackupJwt=await new SignJWT({sub:owner.id,apps:["mm"],permissions:["mm:mcp:read"]}).setProtectedHeader({alg:"HS256"}).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + assert.deepEqual((await listed({authorization:"Bearer "+noBackupJwt})).artifactReceipts,[]) + await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1",[operation.id]);assert.deepEqual((await listed()).artifactReceipts,[]) + await pool.query("UPDATE mcp_operation_steps SET state='verified',result=jsonb_set(result,'{artifactId}',to_jsonb($2::text)) WHERE operation_id=$1",[operation.id,randomUUID()]);assert.deepEqual((await listed()).artifactReceipts,[]) + await pool.query("UPDATE mcp_operation_steps SET result=$2::jsonb WHERE operation_id=$1",[operation.id,JSON.stringify({...receipt,downloadPath:"https://unsafe.example/private",secret:"NEVER_PROJECT_RAW_FIELD"})]) + const safeProjection=(await listed()).artifactReceipts + assert.equal(safeProjection[0].downloadPath,receipt.downloadPath);assert.ok(!JSON.stringify(safeProjection).includes("NEVER_PROJECT_RAW_FIELD"));assert.ok(!JSON.stringify(safeProjection).includes("unsafe.example")) + await pool.query("UPDATE mcp_operation_steps SET intended_state=jsonb_set(intended_state,'{service}',to_jsonb($2::text)) WHERE operation_id=$1",[operation.id,"unknown"]);assert.deepEqual((await listed()).artifactReceipts,[]) + await pool.query("UPDATE mcp_operation_steps SET intended_state=jsonb_set(intended_state,'{service}',to_jsonb($3::text)),result=$2::jsonb WHERE operation_id=$1",[operation.id,JSON.stringify(receipt),"backups.download"]) + const stored=(await pool.query("SELECT * FROM mcp_artifacts WHERE id=$1",[receipt.artifactId])).rows[0] + await pool.query("UPDATE mcp_artifacts SET expires_at=now()-interval '1 second' WHERE id=$1",[receipt.artifactId]);assert.deepEqual((await listed()).artifactReceipts,[]) + await pool.query("UPDATE mcp_artifacts SET expires_at=$2 WHERE id=$1",[receipt.artifactId,stored.expires_at]) + const downloadProtection=randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[downloadProtection,JSON.stringify({kind:"backup",id,protection:"deny"}),owner.id]) + try{assert.deepEqual((await listed()).artifactReceipts,[])}finally{await pool.query("DELETE FROM mcp_protections WHERE id=$1",[downloadProtection])} + assert.ok(!stored.ciphertext.includes("NEVER_MCP_BACKUP_SECRET"));assert.ok(!JSON.stringify(stored.access_scope).includes("NEVER_MCP_BACKUP_SECRET")) + assert.deepEqual((await readHumanBackupDownloadArtifact(receipt.artifactId,owner)).content,content) + const response=await app.inject({url:receipt.downloadPath,headers});assert.equal(response.statusCode,200);assert.deepEqual(response.rawPayload,content) + const rest=await app.inject({url:`/api/backups/${id}/download`,headers});assert.equal(rest.statusCode,200);assert.deepEqual(rest.rawPayload,content);assert.ok(httpReads>0) + assert.equal((await app.inject({url:receipt.downloadPath})).statusCode,401) + assert.equal((await app.inject({url:receipt.downloadPath,headers:{authorization:"Bearer "+issued.token}})).statusCode,401) + await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,{...owner,id:"other"})) + await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,{...owner,permissions:[]})) + await assert.rejects(loadLogExportArtifactMetadata(receipt.artifactId,issued.connection.id)) + await applyPlan(principal,plan.planId,key) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_artifacts WHERE connection_id=$1",[issued.connection.id])).rows[0].n,1) + // Identical bytes from newly available S3 still change the sealed source origin. + remote=true;await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,owner));remote=false + await writeFile(file,Buffer.from("changed source"));await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,owner));await writeFile(file,content) + const cancelled=new AbortController();cancelled.abort();await assert.rejects(readBackupDownloadSource(stored.access_scope.authority,async()=>{},cancelled.signal));await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,owner,undefined,cancelled.signal)) + for(const [field,value] of [["filename","tampered.rsc"],["ciphertext",stored.ciphertext+"x"],["access_scope",{...stored.access_scope,kind:"logs"}]] as const){ + if(field==="access_scope")await pool.query("UPDATE mcp_artifacts SET access_scope=$2::jsonb WHERE id=$1",[receipt.artifactId,JSON.stringify(value)]) + else await pool.query(`UPDATE mcp_artifacts SET ${field}=$2 WHERE id=$1`,[receipt.artifactId,value]) + await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,owner)) + const denied=await app.inject({url:receipt.downloadPath,headers});assert.equal(denied.statusCode,409);assert.ok(!denied.body.includes("NEVER_MCP_BACKUP_SECRET")) + await pool.query("UPDATE mcp_artifacts SET filename=$2,ciphertext=$3,access_scope=$4::jsonb WHERE id=$1",[receipt.artifactId,stored.filename,stored.ciphertext,JSON.stringify(stored.access_scope)]) + } + // The owner remap A→NULL→A must invalidate the old source epoch permanently. + await pool.query("UPDATE backup_entries SET server_id=NULL WHERE id=$1",[id]);await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,owner));await pool.query("UPDATE backup_entries SET server_id=$2 WHERE id=$1",[id,serverId]) + await assert.rejects(readHumanBackupDownloadArtifact(receipt.artifactId,owner)) + const freshPlan=await prepare();await approvePlan(freshPlan.planId,owner,true) + const freshOperation=await applyPlan(principal,freshPlan.planId,randomUUID());assert.equal((await wait(freshOperation.id)).state,"applied") + const freshReceipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0",[freshOperation.id])).rows[0].result + let fired=false + pool.query=((...args:unknown[])=>{const sql=typeof args[0]==="string"?args[0]:(args[0] as {text?:string})?.text;const pending=(originalQuery as (...args:unknown[])=>Promise)(...args);if(!fired&&sql?.includes("MCP backup artifact payload")){fired=true;return pending.then(async row=>{await setConnectionStatus(issued.connection.id,"paused",owner);return row})}return pending}) as typeof pool.query + try{await assert.rejects(readHumanBackupDownloadArtifact(freshReceipt.artifactId,owner));assert.equal(fired,true)}finally{pool.query=originalQuery as typeof pool.query} + assert.ok(!JSON.stringify((await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[issued.connection.id])).rows).includes("NEVER_MCP_BACKUP_SECRET")) + // Resume changes the policy epoch; old artifacts cannot revive through ABA. + assert.deepEqual((await listed()).artifactReceipts,[]) + await setConnectionStatus(issued.connection.id,"active",owner);await assert.rejects(readHumanBackupDownloadArtifact(freshReceipt.artifactId,owner)) + assert.deepEqual((await listed()).artifactReceipts,[]) + console.log("✓ Backup download: human approval in autonomous mode, SQL and REST/S3/local HTTP binary parity, source-origin drift, private artifact, JWT ownership, no model body, tamper, cancellation, orphan refusal, idempotency and late pause/ABA") + } finally { + pool.query=originalQuery as typeof pool.query + await setConnectionStatus(issued.connection.id,"paused",owner) + await pool.query("DELETE FROM mcp_artifacts WHERE connection_id=$1",[issued.connection.id]) + await pool.query("DELETE FROM backup_entries WHERE id=$1",[id]);await rm(file,{force:true}) + await pool.query("UPDATE backup_storage_settings SET provider=$1,s3_endpoint=$2,s3_region=$3,s3_bucket=$4,s3_access_key_id=$5,s3_secret_access_key=$6,s3_force_path_style=$7 WHERE id=1",[saved.provider,saved.s3_endpoint,saved.s3_region,saved.s3_bucket,saved.s3_access_key_id,saved.s3_secret_access_key,saved.s3_force_path_style]) + await new Promise((resolve,reject)=>http.close(error=>error?reject(error):resolve())) + } +} diff --git a/backend/src/test/mcp-bgp-community-policy.ts b/backend/src/test/mcp-bgp-community-policy.ts new file mode 100644 index 00000000..a4b79a63 --- /dev/null +++ b/backend/src/test/mcp-bgp-community-policy.ts @@ -0,0 +1,99 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { bgpCommunityPolicyChain, bgpCommunityPolicyComment, type BgpCommunityPolicy } from "../services/bgp-community-policy-model.js" +import { createMcpConnection, authenticateMcpToken } from "../modules/mcp/store.js" +import { approvePlan, applyPlan, invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpBgpCommunityPolicy(actor: AuthUser) { + const policyId = "fixture", chain = bgpCommunityPolicyChain(policyId), comment = bgpCommunityPolicyComment(policyId) + let reads = 0, writes = 0, ignored = false, corrupt = false, lost = false, missingId = false, bound = false, templateBound = false, late = false, lateReached = false, intentBaseline = 0 + const foreign = { ".id": "*foreign", chain: "other", rule: "NEVER_POLICY_FOREIGN_SCRIPT", comment: "NEVER_POLICY_NOTE", disabled: "false" } + let filters: Record[] = [{ ...foreign }] + const contexts: Record[]> = { "/routing/bgp/connection": [{ ".id": "*peer", name: "peer", "tcp-md5-key": "NEVER_POLICY_PEER_KEY" }], "/routing/bgp/template": [{ ".id": "*tpl", name: "default", "tcp-md5-key": "NEVER_POLICY_TEMPLATE_KEY" }] } + const intentCount = async () => Number((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent' AND payload->>'domain'='application' AND payload->'object'->>'kind'='bgp-community-policy'")).rows[0].n) + const mock = createServer(async (req, res) => { + const path = (req.url ?? "").replace(/^\/rest/, ""); res.setHeader("content-type", "application/json") + if (req.method === "GET") { + reads++ + if (late && path === "/routing/filter/rule" && await intentCount() > intentBaseline) { lateReached = true; late = false; contexts["/routing/bgp/template"][0]["tcp-md5-key"] += " changed" } + res.end(JSON.stringify(path === "/routing/filter/rule" ? filters : contexts[path] ?? [])); return + } + let raw = ""; for await (const part of req) raw += part + const body = raw ? JSON.parse(raw) as Record : {} + if (path.endsWith("/print")) { + assert.deepEqual(body[".query"], ["input.filter=" + chain, "output.filter-chain=" + chain, "#|"]) + res.end(JSON.stringify(path.startsWith("/routing/bgp/connection") && bound ? [{ ".id": "*peer", "output.filter-chain": chain }] : path.startsWith("/routing/bgp/template") && templateBound ? [{ ".id": "*tpl", "input.filter": chain }] : [])); return + } + if (path === "/routing/filter/rule/get") { res.end(JSON.stringify({ ret: "false" })); return } + writes++ + if (!ignored) { + if (path === "/routing/filter/rule/add") { + if (filters.length) filters.at(-1)![".nextid"] = "*owned" + filters.push({ ...body as Record, ".id": "*owned", disabled: "false" }) + } + else if (req.method === "PATCH") Object.assign(filters.find(row => row[".id"] === "*owned")!, body, { disabled: "false" }) + else if (req.method === "DELETE") { + filters = filters.filter(row => row[".id"] !== "*owned") + for (const row of filters) if (row[".nextid"] === "*owned") delete row[".nextid"] + } + } + if (corrupt) filters[0].comment += " changed" + if (lost) { req.socket.destroy(); return } + res.end(JSON.stringify(path.endsWith("/add") ? missingId ? {} : { ret: "*owned" } : {})) + }) + await new Promise(resolve => mock.listen(0, "127.0.0.1", resolve)); const address = mock.address(); assert(address && typeof address !== "string") + const permissions = ["mm:filters:write", "mm:bgp:write", "mm:network:read"] + const create = (granted = permissions, mode = "approval") => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Community policy fixture", mode, permissions: granted, + ...(mode === "autonomous" ? { dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["routing.bgp.community.policy"] } : {}), expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: [80], operationIds: ["routing.bgp.community.policy"] }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES(80,'policy-fixture','127.0.0.1',$1,false,'fixture','NEVER_POLICY_LOGIN')", [address.port]) + const issued = await create(), principal = await authenticateMcpToken(issued.token) + const rules = [{ prefix: "192.0.2.17/24", communityMode: "set", communities: ["64512:001"] }] + const prepare = (extra = {}) => invokeMcpTool(principal, "mm_bgp_community_policy_plan", { serverId: 80, policyId, rules, ...extra }) as Promise<{ planId: string; state: string; changes: { before: { nativeReferenceQueryMatches: number; previousPolicyRecognized: boolean; previousPolicy: BgpCommunityPolicy | null } } }> + const wait = async (id: string) => { for (let n = 0; n < 400; n++) { const row = (await pool.query("SELECT id,state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (row && !["pending", "running"].includes(row.state)) return row; await delay(25) } assert.fail("Community policy did not terminate") } + const apply = async (planId: string) => { await approvePlan(planId, actor, true); return wait((await applyPlan(principal, planId, randomUUID())).id) } + for (const permission of permissions) { const denied = await create(permissions.filter(value => value !== permission)), before = reads; await assert.rejects(invokeMcpTool(await authenticateMcpToken(denied.token), "mm_bgp_community_policy_plan", { serverId: 80, policyId, rules })); assert.equal(reads, before) } + const observer = await create(permissions, "observer"); await assert.rejects(invokeMcpTool(await authenticateMcpToken(observer.token), "mm_bgp_community_policy_plan", { serverId: 80, policyId, rules })) + const autonomous = await create(permissions, "autonomous"), human = await invokeMcpTool(await authenticateMcpToken(autonomous.token), "mm_bgp_community_policy_plan", { serverId: 80, policyId, rules }) as { state: string } + assert.equal(human.state, "awaiting-approval") + const beforeBad = reads; await assert.rejects(prepare({ rules: [{ prefix: "192.0.2.1/24); accept;" }] })); await assert.rejects(prepare({ rule: "accept;" })); await assert.rejects(prepare({ policyId: "x;" })); assert.equal(reads, beforeBad) + await assert.rejects(prepare({ serverId: 1 })) + const first = await prepare(); await assert.rejects(applyPlan(principal, first.planId, randomUUID())); assert.equal((await apply(first.planId)).state, "applied"); assert.equal(writes, 1) + assert.ok(filters[1].rule.includes("dst == 192.0.2.0/24")); assert.ok(filters[1].rule.includes("set bgp-communities 64512:1;")); assert.equal(filters[1].chain, chain); assert.equal(filters[1].comment, comment) + assert.equal((await apply((await prepare()).planId)).state, "applied"); assert.equal(writes, 1) + const current = (await prepare()).changes.before + assert.equal(current.previousPolicyRecognized, true); assert.equal(current.previousPolicy?.rules[0].prefix, "192.0.2.0/24"); assert.deepEqual(current.previousPolicy?.rules[0].communities, ["64512:1"]) + const originalBody = filters[1].rule; filters[1].rule = "NEVER_POLICY_OWNED_SCRIPT" + const opaque = await prepare(); assert.equal(opaque.changes.before.previousPolicyRecognized, false); assert.equal(opaque.changes.before.previousPolicy, null); assert.ok(!JSON.stringify(opaque).includes("NEVER_POLICY_OWNED_SCRIPT")); filters[1].rule = originalBody + assert.equal(filters[0][".nextid"], "*owned") + const linkPlan = await prepare(); filters[0][".nextid"] = "*foreignChangedLink" + assert.notEqual((await apply(linkPlan.planId)).state, "applied"); assert.equal(writes, 1); filters[0][".nextid"] = "*owned" + bound = true; assert.equal((await prepare()).changes.before.nativeReferenceQueryMatches, 1); await assert.rejects(prepare({ action: "delete", rules: [] })); bound = false + templateBound = true; await assert.rejects(prepare({ action: "delete", rules: [] })); templateBound = false + filters[0].rule = "JUMP " + chain; await assert.rejects(prepare({ action: "delete", rules: [] })); filters[0].rule = 'jump ("opaque" . "target")'; await assert.rejects(prepare({ action: "delete", rules: [] })); filters[0].rule = foreign.rule + const source = await prepare(); await approvePlan(source.planId, actor, true); await pool.query("UPDATE servers SET password='NEVER_POLICY_ROTATED' WHERE id=80"); const sourceReads = reads + assert.notEqual((await wait((await applyPlan(principal, source.planId, randomUUID())).id)).state, "applied"); assert.equal(reads, sourceReads); await pool.query("UPDATE servers SET password='NEVER_POLICY_LOGIN' WHERE id=80") + const latePlan = await prepare({ defaultAction: "accept" }); intentBaseline = await intentCount(); late = true; const lateWrites = writes + assert.notEqual((await apply(latePlan.planId)).state, "applied"); assert.equal(lateReached, true); assert.equal(writes, lateWrites); contexts["/routing/bgp/template"][0]["tcp-md5-key"] = "NEVER_POLICY_TEMPLATE_KEY" + ignored = true; assert.equal((await apply((await prepare({ defaultAction: "accept" })).planId)).state, "partial"); ignored = false + corrupt = true; assert.equal((await apply((await prepare({ defaultAction: "accept" })).planId)).state, "partial"); corrupt = false; filters[0].comment = foreign.comment + lost = true; const unknownPlan = await prepare({ defaultAction: "reject" }), unknown = await apply(unknownPlan.planId); lost = false; assert.equal(unknown.state, "unknown") + const replayWrites = writes; assert.equal((await applyPlan(principal, unknownPlan.planId, randomUUID())).id, unknown.id); assert.equal(writes, replayWrites) + assert.equal((await apply((await prepare()).planId)).state, "applied"); assert.equal(writes, replayWrites) + const protectedPlan = await prepare(); await approvePlan(protectedPlan.planId, actor, true); const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'fixture')", [protection, JSON.stringify({ kind: "bgp_connection", id: "*peer", serverId: 80, protection: "deny" })]) + await assert.rejects(applyPlan(principal, protectedPlan.planId, randomUUID()), (error: { code?: string }) => error.code === "PROTECTED_OBJECT"); assert.equal(writes, replayWrites); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + assert.equal((await apply((await prepare({ action: "delete", rules: [] })).planId)).state, "applied"); assert.equal(filters.length, 1) + missingId = true; assert.equal((await apply((await prepare()).planId)).state, "unknown"); missingId = false + const missingWrites = writes; assert.equal((await apply((await prepare()).planId)).state, "applied"); assert.equal(writes, missingWrites) + await apply((await prepare({ action: "delete", rules: [] })).planId) + filters.push({ ".id": "*collision", chain, comment: "foreign", rule: "accept;", disabled: "false" }); await assert.rejects(prepare()); filters.pop() + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [issued.connection.id])).rows).includes("NEVER_")) + console.log("✓ Community policy: typed compiler/ownership, conjunctive scope/human rights, canonical CIDR, no-op, hidden/effective BGP/template query references, jump guards, credential/no-poll, real late intent fence, protections, ignored/partial/unknown/missing-ID without replay, cleanup and private source proofs") + } finally { mock.closeAllConnections(); await new Promise(resolve => mock.close(() => resolve())); await pool.query("DELETE FROM servers WHERE id=80") } +} diff --git a/backend/src/test/mcp-bgp-export.ts b/backend/src/test/mcp-bgp-export.ts new file mode 100644 index 00000000..eb05130d --- /dev/null +++ b/backend/src/test/mcp-bgp-export.ts @@ -0,0 +1,157 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken } from "../modules/mcp/store.js" +import { approvePlan, applyPlan, invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpBgpExport(actor: AuthUser) { + let mode = "", templateMode = "", writes = 0, reads = 0, ignored = false, unexpected = false, lost = false + let staticQueryReply: unknown = [{ ".id": "*peer" }] + const connection: Record = { ".id": "*peer", name: "export-peer", disabled: "false", instance: "lab", vrf: "lab", "routing-table": "lab", templates: "default", "remote.address": "192.0.2.2", "remote.as": "64513", "tcp-md5-key": "NEVER_BGP_EXPORT_MD5" } + const routes: Record = { + "/ip/route": [{ ".id": "*c4", "routing-table": "lab", "dst-address": "192.0.2.0/30", connect: "true", dynamic: "true", active: "true", comment: "NEVER_BGP_EXPORT_ROUTE_NOTE" }, + { ".id": "*s4", "routing-table": "lab", "dst-address": "198.18.0.1/32", static: "true", dynamic: "false", active: "true" }, + { ".id": "*b4", "routing-table": "lab", "dst-address": "198.18.1.1/32", bgp: "true", dynamic: "true", active: "true" }], + "/ipv6/route": [{ ".id": "*c6", "routing-table": "lab", "dst-address": "fd00::/64", connect: "true", dynamic: "true", active: "true" }, + { ".id": "*s6", "routing-table": "lab", "dst-address": "fd00:1::1/128", static: "true", dynamic: "false", active: "true" }], + } + const collections: Record[]> = { + "/routing/bgp/template": [{ ".id": "*tpl", name: "default", "tcp-md5-key": "NEVER_BGP_EXPORT_TEMPLATE_KEY" }], + "/routing/bgp/instance": [{ ".id": "*i", name: "lab", disabled: "false", as: "64512", "router-id": "192.0.2.1", vrf: "lab", "routing-table": "lab" }], + "/ip/vrf": [{ ".id": "*v", name: "lab", interfaces: "export-gre" }], + "/routing/table": [{ ".id": "*t", name: "lab", fib: "true" }], + "/ip/address": [{ ".id": "*a4", address: "192.0.2.1/30", interface: "export-gre", disabled: "false" }], + "/ipv6/address": [{ ".id": "*a6", address: "fd00::1/64", interface: "export-gre", disabled: "false", dynamic: "false" }], + "/routing/filter/rule": [{ ".id": "*f", chain: "export-out", rule: "NEVER_BGP_EXPORT_FILTER_SECRET", disabled: "false" }], + } + const mock = createServer(async (req, res) => { + const path = (req.url ?? "").replace(/^\/rest/, "") + if (req.method === "GET") { + reads++; res.setHeader("content-type", "application/json") + res.end(JSON.stringify(path === "/routing/bgp/connection" ? [connection] : collections[path] ?? routes[path] ?? [])); return + } + let raw = ""; for await (const data of req) raw += data + const body = raw ? JSON.parse(raw) as Record : {} + res.setHeader("content-type", "application/json") + if (path === "/routing/bgp/connection/get") { + const property = body["value-name"] + res.end(JSON.stringify(property === "dynamic" ? [] : property === "output.redistribute" ? mode ? { ret: mode } : [] : { ret: property === "templates" ? connection.templates ?? "" : "false" })); return + } + if (path === "/routing/bgp/connection/print") { + assert.deepEqual(body, { ".proplist": ".id,dynamic", ".query": [".id=*peer", "-dynamic", "#&"] }) + res.end(JSON.stringify(staticQueryReply)); return + } + if (path === "/routing/bgp/template/get") { + const property = body["value-name"] + res.end(JSON.stringify(property === "output.redistribute" ? templateMode ? { ret: templateMode } : [] : property === "templates" ? { ret: collections["/routing/bgp/template"][0].templates ?? "" } : { ret: "false" })); return + } + if (req.method === "PATCH" && path === "/routing/bgp/connection/*peer") { + writes++ + assert.deepEqual(Object.keys(body), ["output.redistribute"]) + if (!ignored) mode = body["output.redistribute"] + if (unexpected) connection["tcp-md5-key"] = "NEVER_BGP_EXPORT_UNEXPECTED_MD5" + if (lost) { req.socket.destroy(); return } + res.end(JSON.stringify(connection)); return + } + res.writeHead(404); res.end("{}") + }) + await new Promise(resolve => mock.listen(0, "127.0.0.1", resolve)) + const address = mock.address(); assert(address && typeof address !== "string") + const permissions = ["mm:bgp:write", "mm:network:read", "mm:filters:read"] + const create = (granted = permissions, observer = false) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP export acceptance", mode: observer ? "observer" : "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: granted, + scope: { serverIds: [72], operationIds: ["routing.bgp.export"], allowGlobal: false }, limits: { callsPerMinute: 120, mutationsPerHour: 50 } }), actor) + const connections: string[] = [] + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES(72,'export-fixture','127.0.0.1',$1,false,'fixture','NEVER_BGP_EXPORT_LOGIN')", [address.port]) + const access = await create(), principal = await authenticateMcpToken(access.token); connections.push(access.connection.id) + const plan = async (next: string) => await invokeMcpTool(principal, "mm_bgp_export_plan", { serverId: 72, id: "*peer", mode: next }) as { planId: string; state: string; changes: { before: { mode: string; sources: unknown[] }; objects: { kind: string; id: string }[] } } + const wait = async (id: string) => { + for (let attempt = 0; attempt < 400; attempt++) { + const row = (await pool.query<{ id: string; state: string }>("SELECT id,state FROM mcp_operations WHERE id=$1", [id])).rows[0] + if (row && !["pending", "running"].includes(row.state)) return row + await delay(25) + } + assert.fail("BGP export fixture did not finish") + } + const apply = async (id: string) => { await approvePlan(id, actor, true); return wait((await applyPlan(principal, id, randomUUID())).id) } + for (const granted of [permissions.slice(0, 1), permissions.slice(0, 2), permissions.filter(permission => permission !== "mm:network:read")]) { + const denied = await create(granted), token = await authenticateMcpToken(denied.token); connections.push(denied.connection.id) + const before = reads; await assert.rejects(invokeMcpTool(token, "mm_bgp_export_plan", { serverId: 72, id: "*peer", mode: "connected" })); assert.equal(reads, before) + } + const observer = await create(permissions, true); connections.push(observer.connection.id) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(observer.token), "mm_bgp_export_plan", { serverId: 72, id: "*peer", mode: "connected" })) + const autonomous = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Explicit autonomous export", mode: "autonomous", dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["routing.bgp.export"], expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions, scope: { serverIds: [72], operationIds: ["routing.bgp.export"], allowGlobal: false } }), actor) + connections.push(autonomous.connection.id) + const autonomousPrincipal = await authenticateMcpToken(autonomous.token) + const autonomousPlan = await invokeMcpTool(autonomousPrincipal, "mm_bgp_export_plan", { serverId: 72, id: "*peer", mode: "connected" }) as { planId: string; state: string } + assert.equal(autonomousPlan.state, "awaiting-approval"); await assert.rejects(applyPlan(autonomousPrincipal, autonomousPlan.planId, randomUUID())); assert.equal(writes, 0) + const ownerDenied = await create(); connections.push(ownerDenied.connection.id) + const ownerDeniedPrincipal = await authenticateMcpToken(ownerDenied.token) + await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb WHERE id=$1", [ownerDenied.connection.id, JSON.stringify(["mm:bgp:write", "mm:network:read"])]) + const beforeOwnerDenied = reads; await assert.rejects(invokeMcpTool(ownerDeniedPrincipal, "mm_bgp_export_plan", { serverId: 72, id: "*peer", mode: "connected" })); assert.equal(reads, beforeOwnerDenied) + await assert.rejects(invokeMcpTool(principal, "mm_bgp_export_plan", { serverId: 1, id: "*peer", mode: "connected" })) + await assert.rejects(invokeMcpTool(principal, "mm_bgp_export_plan", { serverId: 72, id: "*peer", mode: "ospf" })) + await assert.rejects(invokeMcpTool(principal, "mm_bgp_export_plan", { serverId: 72, id: "*peer", mode: "static", sourceModes: ["disabled"] })) + for (const invalid of [[], [{ ".id": "*other" }], [{ ".id": "*peer", dynamic: "true" }], [{ ".id": "*peer" }, { ".id": "*peer" }], {}]) { + staticQueryReply = invalid; await assert.rejects(plan("connected")); assert.equal(writes, 0) + } + staticQueryReply = [{ ".id": "*peer" }] + let prepared = await plan("connected") + assert.equal(prepared.state, "awaiting-approval"); assert.equal(prepared.changes.before.mode, "disabled"); assert.equal(prepared.changes.before.sources.length, 2) + assert(prepared.changes.objects.some(row => row.kind === "ipv4_route" && row.id === "*c4")); assert(!prepared.changes.objects.some(row => row.kind === "ipv4_route" && row.id === "*b4")) + assert.equal(JSON.stringify(prepared).includes("NEVER_BGP_EXPORT_"), false) + await assert.rejects(applyPlan(principal, prepared.planId, randomUUID())); assert.equal(writes, 0) + await approvePlan(prepared.planId, actor, true); const key = randomUUID(), operation = await wait((await applyPlan(principal, prepared.planId, key)).id) + assert.equal(operation.state, "applied"); assert.equal(mode, "connected"); assert.equal(writes, 1) + assert.equal((await applyPlan(principal, prepared.planId, key)).id, operation.id); assert.equal(writes, 1) + prepared = await plan("disabled"); (routes["/ip/route"][2] as Record)["dst-address"] = "198.18.1.2/32"; connection.inactive = "true" + assert.equal((await apply(prepared.planId)).state, "applied"); assert.equal(mode, "") + connection.disabled = "true" + assert.equal((await apply((await plan("static")).planId)).state, "applied"); assert.equal(mode, "static"); assert.equal(connection.disabled, "true") + connection.disabled = "false" + const beforeNoop = writes; assert.equal((await apply((await plan("static")).planId)).state, "applied"); assert.equal(writes, beforeNoop) + prepared = await plan("disabled"); (routes["/ip/route"][1] as Record).gateway = "192.0.2.2" + const beforeDrift = writes; assert.equal((await apply(prepared.planId)).state, "rejected"); assert.equal(writes, beforeDrift) + delete (routes["/ip/route"][1] as Record).gateway + prepared = await plan("disabled"); collections["/routing/filter/rule"][0].rule += " changed" + assert.equal((await apply(prepared.planId)).state, "rejected"); assert.equal(writes, beforeDrift) + collections["/routing/filter/rule"][0].rule = "NEVER_BGP_EXPORT_FILTER_SECRET" + prepared = await plan("disabled") + await pool.query("UPDATE servers SET password='NEVER_BGP_EXPORT_NEW_LOGIN' WHERE id=72") + const beforeCredentialReads = reads + assert.equal((await apply(prepared.planId)).state, "rejected"); assert.equal(reads, beforeCredentialReads); assert.equal(writes, beforeDrift) + await pool.query("UPDATE servers SET password='NEVER_BGP_EXPORT_LOGIN' WHERE id=72") + prepared = await plan("disabled"); templateMode = "connected" + assert.equal((await apply(prepared.planId)).state, "rejected"); assert.equal(writes, beforeDrift) + await assert.rejects(plan("disabled"), /шаблона/) + templateMode = ""; collections["/routing/bgp/template"][0].templates = "default" + await assert.rejects(plan("static")); delete collections["/routing/bgp/template"][0].templates + const protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ipv4_route", id: "*s4", serverId: 72, protection: "deny" }), actor.id]) + try { await assert.rejects(plan("disabled")); assert.equal(writes, beforeDrift) } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + mode = "connected,static"; await assert.rejects(plan("disabled")); mode = "static" + delete connection.vrf; await assert.rejects(plan("disabled")); connection.vrf = "lab" + mode = ""; templateMode = "connected" + const inheritedBefore = writes + assert.equal((await apply((await plan("connected")).planId)).state, "applied"); assert.equal(writes, inheritedBefore) + templateMode = ""; mode = "static" + ignored = true; const ignoredBefore = writes + assert.equal((await apply((await plan("disabled")).planId)).state, "partial"); assert.equal(writes, ignoredBefore + 1); ignored = false + unexpected = true; assert.equal((await apply((await plan("disabled")).planId)).state, "partial"); unexpected = false; connection["tcp-md5-key"] = "NEVER_BGP_EXPORT_MD5" + mode = "static"; lost = true; const beforeLost = writes + const lostPlan = await plan("disabled"), lostOperation = await apply(lostPlan.planId) + assert.equal(lostOperation.state, "unknown"); assert.equal(writes, beforeLost + 1); lost = false + assert.equal((await applyPlan(principal, lostPlan.planId, randomUUID())).id, lostOperation.id); assert.equal(writes, beforeLost + 1) + const requests = (await pool.query("SELECT input,result FROM mcp_requests WHERE connection_id=ANY($1::uuid[])", [connections])).rows + const audit = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=ANY($1::uuid[])", [connections])).rows + assert.equal(JSON.stringify({ requests, audit }).includes("NEVER_BGP_EXPORT_"), false) + } finally { + const closed = new Promise(resolve => mock.close(() => resolve())); mock.closeAllConnections(); await closed + await pool.query("DELETE FROM servers WHERE id=72") + } + console.log("✓ BGP export: conjunctive owner/connection permissions, human approval, effective mode, explicit VRF/table, source/filter proof, route protections, no-op/idempotency, late drift, ignored/unexpected/lost write and private secret proof") +} diff --git a/backend/src/test/mcp-bgp-filter-binding.ts b/backend/src/test/mcp-bgp-filter-binding.ts new file mode 100644 index 00000000..f9b9903c --- /dev/null +++ b/backend/src/test/mcp-bgp-filter-binding.ts @@ -0,0 +1,94 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { createMcpConnection, authenticateMcpToken } from "../modules/mcp/store.js" +import { approvePlan, applyPlan, invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpBgpFilterBinding(actor: AuthUser) { + let reads = 0, writes = 0, ignored = false, corrupt = false, lost = false, lateDrift = false, lateFenceReached = false, intentBaseline = 0 + const peer: Record = { ".id": "*peer", name: "binding-peer", disabled: "false", instance: "lab", vrf: "lab", "routing-table": "lab", templates: "default", "tcp-md5-key": "NEVER_BIND_PEER_SECRET", "output.redistribute": "connected,static,ospf" } + const local: Record = { "input.filter": null, "output.filter-chain": null }, inherited: Record = { "input.filter": null, "output.filter-chain": null } + const context: Record = { instance: "lab", vrf: "lab", "routing-table": "lab" } + const filters: Record[] = [{ ".id": "*in", chain: "in", rule: "NEVER_BIND_INPUT_SCRIPT", disabled: "false" }, { ".id": "*out", chain: "out", rule: "NEVER_BIND_OUTPUT_SCRIPT", disabled: "false" }] + const collections: Record[]> = { "/routing/bgp/template": [{ ".id": "*tpl", name: "default", "tcp-md5-key": "NEVER_BIND_TEMPLATE_SECRET" }], + "/routing/bgp/instance": [{ ".id": "*instance", name: "lab", disabled: "false", vrf: "lab", "routing-table": "lab" }], "/ip/vrf": [{ ".id": "*vrf", name: "lab", interfaces: "gre" }], "/routing/table": [{ ".id": "*table", name: "lab", fib: "true" }] } + const intentCount = async () => Number((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='configuration.intent' AND payload->>'domain'='application' AND payload->'object'->>'kind'='bgp-policy'")).rows[0].n) + const mock = createServer(async (req, res) => { + const path = (req.url ?? "").replace(/^\/rest/, ""); res.setHeader("content-type", "application/json") + if (req.method === "GET") { + reads++ + if (lateDrift && path === "/routing/bgp/connection" && await intentCount() > intentBaseline) { lateFenceReached = true; lateDrift = false; filters[0].rule += " changed" } + res.end(JSON.stringify(path === "/routing/bgp/connection" ? [peer] : path === "/routing/filter/rule" ? filters : collections[path] ?? [])); return + } + let raw = ""; for await (const part of req) raw += part + const body = raw ? JSON.parse(raw) as Record : {}, property = body["value-name"] + if (path === "/routing/bgp/connection/get") { const value = property === "dynamic" ? null : property === "templates" ? peer.templates : property in local ? local[property] : property in peer ? peer[property] : context[property] ?? "false"; res.end(JSON.stringify(value === null ? [] : { ret: value })); return } + if (path === "/routing/bgp/connection/print") { assert.deepEqual(body[".query"], [".id=*peer", "-dynamic", "#&"]); res.end(JSON.stringify([{ ".id": "*peer" }])); return } + if (path === "/routing/bgp/template/get") { const value = property in inherited ? inherited[property] : property === "templates" ? collections["/routing/bgp/template"][0].templates ?? "" : "false"; res.end(JSON.stringify(value === null ? [] : { ret: value })); return } + if (path === "/routing/bgp/instance/get") { res.end(JSON.stringify({ ret: context[property] ?? "false" })); return } + if (path === "/routing/filter/rule/get") { res.end(JSON.stringify({ ret: "false" })); return } + if (req.method === "PATCH" && path === "/routing/bgp/connection/*peer") { + writes++; assert.equal(Object.keys(body).length, 1); const key = Object.keys(body)[0]; assert.ok(key in local) + if (!ignored) { local[key] = body[key] || null; if (body[key]) peer[key] = body[key]; else delete peer[key] } + if (corrupt) peer["tcp-md5-key"] = "NEVER_BIND_CHANGED_SECRET" + if (lost) { req.socket.destroy(); return } + res.end(JSON.stringify(peer)); return + } + res.writeHead(404); res.end("{}") + }) + await new Promise(resolve => mock.listen(0, "127.0.0.1", resolve)); const address = mock.address(); assert(address && typeof address !== "string") + const permissions = ["mm:bgp:write", "mm:network:read", "mm:filters:read"], ids: string[] = [] + const create = async (granted = permissions, mode = "approval") => { + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP binding fixture", mode, permissions: granted, expiresAt: new Date(Date.now() + 1800000).toISOString(), + ...(mode === "autonomous" ? { dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: ["routing.bgp.filter.bind"] } : {}), + scope: { serverIds: [79], operationIds: ["routing.bgp.filter.bind"] }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + ids.push(issued.connection.id); return issued + } + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES(79,'binding-fixture','127.0.0.1',$1,false,'fixture','NEVER_BIND_LOGIN')", [address.port]) + const issued = await create(), principal = await authenticateMcpToken(issued.token) + const prepare = (chain = "in", direction = "input") => invokeMcpTool(principal, "mm_bgp_filter_binding_plan", { serverId: 79, id: "*peer", chain, direction }) as Promise<{ planId: string; state: string }> + const wait = async (id: string) => { for (let n = 0; n < 400; n++) { const row = (await pool.query("SELECT id,state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (row && !["pending", "running"].includes(row.state)) return row; await delay(25) } assert.fail("Binding operation did not terminate") } + const apply = async (planId: string) => { await approvePlan(planId, actor, true); return wait((await applyPlan(principal, planId, randomUUID())).id) } + for (const permission of permissions) { const denied = await create(permissions.filter(value => value !== permission)), before = reads; await assert.rejects(invokeMcpTool(await authenticateMcpToken(denied.token), "mm_bgp_filter_binding_plan", { serverId: 79, id: "*peer", chain: "in", direction: "input" })); assert.equal(reads, before) } + const observer = await create(permissions, "observer"); await assert.rejects(invokeMcpTool(await authenticateMcpToken(observer.token), "mm_bgp_filter_binding_plan", { serverId: 79, id: "*peer", chain: "in", direction: "input" })) + const autonomous = await create(permissions, "autonomous"), human = await invokeMcpTool(await authenticateMcpToken(autonomous.token), "mm_bgp_filter_binding_plan", { serverId: 79, id: "*peer", chain: "in", direction: "input" }) as { planId: string; state: string } + assert.equal(human.state, "awaiting-approval"); await assert.rejects(applyPlan(await authenticateMcpToken(autonomous.token), human.planId, randomUUID())) + const first = await prepare(); await assert.rejects(applyPlan(principal, first.planId, randomUUID())); assert.equal((await apply(first.planId)).state, "applied"); assert.equal(writes, 1) + assert.equal(peer["output.redistribute"], "connected,static,ospf") + assert.equal((await apply((await prepare()).planId)).state, "applied"); assert.equal(writes, 1) + assert.equal((await apply((await prepare("out", "output")).planId)).state, "applied"); assert.equal(writes, 2); assert.equal(local["input.filter"], "in") + assert.equal((await apply((await prepare("")).planId)).state, "applied"); assert.equal(local["input.filter"], null) + inherited["input.filter"] = "in"; const override = await prepare("in"); assert.equal((await apply(override.planId)).state, "applied"); assert.equal(local["input.filter"], "in") + await assert.rejects(prepare("")); inherited["input.filter"] = null + delete peer.vrf; delete peer["routing-table"]; delete collections["/routing/bgp/instance"][0].vrf; delete collections["/routing/bgp/instance"][0]["routing-table"] + assert.equal((await apply((await prepare()).planId)).state, "applied", "Effective default context comes from native /get, not guessed main") + Object.assign(peer, context); Object.assign(collections["/routing/bgp/instance"][0], { vrf: "lab", "routing-table": "lab" }) + Object.assign(peer, { instance: "*instance", vrf: "*vrf", "routing-table": "*table" }); assert.equal((await apply((await prepare()).planId)).state, "applied"); Object.assign(peer, context) + collections["/routing/bgp/template"][0].templates = "default"; await assert.rejects(prepare()); delete collections["/routing/bgp/template"][0].templates + await assert.rejects(prepare("In")); await assert.rejects(prepare("missing")) + const beforeInvalid = reads; await assert.rejects(invokeMcpTool(principal, "mm_bgp_filter_binding_plan", { serverId: 79, id: "all", chain: "in", direction: "input" })); assert.equal(reads, beforeInvalid) + await assert.rejects(invokeMcpTool(principal, "mm_bgp_filter_binding_plan", { serverId: 79, id: "*peer", chain: "in", direction: "input", rule: "accept;" })); assert.equal(reads, beforeInvalid) + await assert.rejects(invokeMcpTool(principal, "mm_bgp_filter_binding_plan", { serverId: 1, id: "*peer", chain: "in", direction: "input" })) + filters[0].invalid = "true"; await assert.rejects(prepare()); delete filters[0].invalid + const source = await prepare(); await approvePlan(source.planId, actor, true); await pool.query("UPDATE servers SET password='NEVER_BIND_ROTATED' WHERE id=79"); const beforeSource = reads + assert.notEqual((await wait((await applyPlan(principal, source.planId, randomUUID())).id)).state, "applied"); assert.equal(reads, beforeSource); await pool.query("UPDATE servers SET password='NEVER_BIND_LOGIN' WHERE id=79") + const late = await prepare("out"); intentBaseline = await intentCount(); lateDrift = true; const beforeLate = writes + assert.notEqual((await apply(late.planId)).state, "applied"); assert.equal(lateFenceReached, true); assert.equal(writes, beforeLate); filters[0].rule = "NEVER_BIND_INPUT_SCRIPT" + const protection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'fixture')", [protection, JSON.stringify({ kind: "routing_filter", id: "*in", serverId: 79, protection: "deny" })]); await assert.rejects(prepare()); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + const noop = await prepare(); await approvePlan(noop.planId, actor, true) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'fixture')", [protection, JSON.stringify({ kind: "qos", id: "unrelated", serverId: 79, protection: "deny" })]) + const beforeNoop = writes; assert.notEqual((await wait((await applyPlan(principal, noop.planId, randomUUID())).id)).state, "applied"); assert.equal(writes, beforeNoop); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + ignored = true; const ignoredResult = await apply((await prepare("out")).planId); ignored = false; assert.equal(ignoredResult.state, "partial") + lost = true; const lostPlan = await prepare("out"), unknown = await apply(lostPlan.planId); lost = false; assert.equal(unknown.state, "unknown") + const beforeReplay = writes; const repeated = await applyPlan(principal, lostPlan.planId, randomUUID()); assert.equal(repeated.id, unknown.id); assert.equal((await wait(repeated.id)).state, "unknown"); assert.equal(writes, beforeReplay) + assert.equal((await apply((await prepare("out")).planId)).state, "applied"); assert.equal(writes, beforeReplay) + corrupt = true; const partial = await apply((await prepare("in")).planId); corrupt = false; assert.equal(partial.state, "partial"); peer["tcp-md5-key"] = "NEVER_BIND_PEER_SECRET" + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [issued.connection.id])).rows).includes("NEVER_")) + console.log("✓ BGP filter binding: conjunctive scope, human even autonomous, input/output exact single-property PATCH, local/inherited/nil clear, no-op, case/invalid guards, credential no-poll, proven application-intent late fence, protection, ignored/partial/unknown/no replay and opaque proofs") + } finally { mock.closeAllConnections(); await new Promise(resolve => mock.close(() => resolve())); await pool.query("DELETE FROM servers WHERE id=79") } +} diff --git a/backend/src/test/mcp-bgp-prefixes.ts b/backend/src/test/mcp-bgp-prefixes.ts new file mode 100644 index 00000000..66c1e6d0 --- /dev/null +++ b/backend/src/test/mcp-bgp-prefixes.ts @@ -0,0 +1,57 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpBgpPrefixes(actor: AuthUser) { + let reads = 0, changeSource = false, pause = false, removeOwnerPermission = false, tableDrift = false, tableReads = 0 + let payload: unknown = [{ ".id": "*b", "routing-table": "lab", "dst-address": "192.0.2.0/24", bgp: "true", dynamic: "true", active: "true", distance: "20", comment: "NEVER_PREFIX_PRIVATE", "tcp-md5-key": "NEVER_PREFIX_KEY" }, + { ".id": "*c", "routing-table": "lab", "dst-address": "192.0.3.0/24", bgp: "true", dynamic: "true" }] + let connectionId = "" + const mock = createServer(async (req, res) => { + reads++; res.setHeader("content-type", "application/json") + if (req.url === "/rest/routing/table") { tableReads++; res.end(JSON.stringify([{ ".id": "*t", name: "lab", fib: tableDrift && tableReads % 2 === 0 ? "false" : "true" }])); return } + assert.equal(req.url, "/rest/ip/route?routing-table=lab"); assert.equal(req.method, "GET") + if (changeSource) await pool.query("UPDATE servers SET password='NEVER_PREFIX_ROTATED' WHERE id=73") + if (removeOwnerPermission) await pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [connectionId]) + if (pause) await setConnectionStatus(connectionId, "paused", actor) + res.end(JSON.stringify(payload)) + }) + await new Promise(resolve => mock.listen(0, "127.0.0.1", resolve)) + const address = mock.address(); assert(address && typeof address !== "string") + const permissions = ["mm:bgp:read", "mm:network:read"] + const create = (granted = permissions) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP prefix observer", mode: "observer", permissions: granted, expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: [73], operationIds: ["routing.bgp.prefixes.read"] }, limits: { callsPerMinute: 120 } }), actor) + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES(73,'prefix-fixture','127.0.0.1',$1,false,'fixture','NEVER_PREFIX_LOGIN')", [address.port]) + const issued = await create(), principal = await authenticateMcpToken(issued.token); connectionId = issued.connection.id + const ownerPermissions = (await pool.query("SELECT owner_permissions FROM mcp_connections WHERE id=$1", [connectionId])).rows[0].owner_permissions + const read = (extra = {}) => invokeMcpTool(principal, "mm_bgp_prefixes_get", { serverId: 73, table: "lab", ...extra }) as Promise<{ items: { active: boolean | null }[]; total: number; nextOffset: number | null; forwardingVerified: boolean }> + for (const permission of permissions) { + const denied = await create([permission]), before = reads + await assert.rejects(invokeMcpTool(await authenticateMcpToken(denied.token), "mm_bgp_prefixes_get", { serverId: 73, table: "lab" })); assert.equal(reads, before) + } + let result = await read({ limit: 1 }); assert.equal(result.total, 2); assert.equal(result.items[0].active, true); assert.equal(result.nextOffset, 1); assert.equal(result.forwardingVerified, false) + result = await read({ offset: 1 }); assert.equal(result.items[0].active, null); assert.equal(JSON.stringify(result).includes("NEVER_PREFIX"), false) + const beforeDenied = reads; await assert.rejects(read({ serverId: 1 })); await assert.rejects(read({ family: "ip" })); assert.equal(reads, beforeDenied) + const aborted = new AbortController(); aborted.abort() + await assert.rejects(invokeMcpTool(principal, "mm_bgp_prefixes_get", { serverId: 73, table: "lab" }, aborted.signal)); assert.equal(reads, beforeDenied) + const original = payload; payload = []; assert.equal((await read()).total, 0) + payload = [{ ".id": "*x", "routing-table": "lab", "dst-address": "192.0.2.0/24" }]; await assert.rejects(read()) + payload = original; changeSource = true; await assert.rejects(read()); changeSource = false + await pool.query("UPDATE servers SET password='NEVER_PREFIX_LOGIN' WHERE id=73") + tableDrift = true; tableReads = 0; await assert.rejects(read()); tableDrift = false + const beforeOwnerRace = reads + removeOwnerPermission = true; await assert.rejects(read()); removeOwnerPermission = false + assert.ok(reads > beforeOwnerRace, "Owner race must reach the native read") + await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb WHERE id=$1", [connectionId, JSON.stringify(ownerPermissions)]) + pause = true; await assert.rejects(read()); pause = false + assert.equal((await pool.query("SELECT policy->>'status' AS status FROM mcp_connections WHERE id=$1", [connectionId])).rows[0].status, "paused", "Pause must occur during the native read") + const beforePause = reads; await assert.rejects(read()); assert.equal(reads, beforePause) + const rows = await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [connectionId]); assert.equal(JSON.stringify(rows.rows).includes("NEVER_PREFIX"), false) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n, 0) + console.log("✓ BGP prefix observer: conjunctive grants, scope before polling, pages, active unknown, bounded provenance, source/table/pause races and no secret echo/plans") + } finally { mock.closeAllConnections(); await new Promise(resolve => mock.close(() => resolve())); await pool.query("DELETE FROM servers WHERE id=73") } +} diff --git a/backend/src/test/mcp-bridge-vlan-configuration.ts b/backend/src/test/mcp-bridge-vlan-configuration.ts new file mode 100644 index 00000000..f0c74d2d --- /dev/null +++ b/backend/src/test/mcp-bridge-vlan-configuration.ts @@ -0,0 +1,351 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import type { FastifyInstance } from "fastify" +import type { BridgeVlanDesired, BridgeVlanState } from "@mmapp/contracts/bridge-vlan" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { env } from "../config.js" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { accessGeneration, decryptSecret, encryptSecret, fingerprint } from "../modules/mcp/security.js" +import { bridgeVlanCurrentDesired } from "../services/bridge-vlan-configuration.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan } from "../modules/mcp/runtime.js" + +/** Synthetic SQL and two HTTP loopback endpoints only. The sealed channel is + * seeded: this does not claim channel registration, TLS or network independence. */ +export async function verifyMcpBridgeVlanConfiguration(actor: AuthUser, app: FastifyInstance) { + const serverId = 91801, channelId = randomUUID(), connections: string[] = [], pending = new Set() + type Row = Record + type Rows = Record + const initial: Rows = { + "/interface/ethernet": [ + { ".id": "*2", name: "ether2", disabled: "false" }, { ".id": "*3", name: "ether3", disabled: "false" }, + { ".id": "*4", name: "ether-mgmt", disabled: "false" }, { ".id": "*9", name: "ether9", disabled: "false" }, + ], + "/interface/bridge": [{ ".id": "*1", name: "br-users", pvid: "1", "vlan-filtering": "false", "frame-types": "admit-all", "ingress-filtering": "true", mvrp: "false", disabled: "false", opaque: "preserve-bridge" }, { ".id": "*8", name: "br-foreign", pvid: "1", "vlan-filtering": "false", "frame-types": "admit-all", "ingress-filtering": "true", mvrp: "false", disabled: "false" }], + "/interface/bridge/port": [{ ".id": "*10", bridge: "br-users", interface: "ether2", pvid: "10", "ingress-filtering": "true", "frame-types": "admit-all", disabled: "false", opaque: "preserve-port" }, { ".id": "*98", bridge: "br-foreign", interface: "ether9", pvid: "99", "ingress-filtering": "true", "frame-types": "admit-all", disabled: "false" }], + "/interface/bridge/vlan": [{ ".id": "*11", bridge: "br-users", "vlan-ids": "10", tagged: "br-users", untagged: "ether2", disabled: "false", dynamic: "false", comment: "existing-membership", opaque: "preserve-static" }, { ".id": "*99", bridge: "br-foreign", "vlan-ids": "99", tagged: "br-foreign", untagged: "ether9", dynamic: "false", disabled: "false" }], + "/interface/vlan": [], "/interface/gre": [], "/interface/wireguard": [], "/interface/vxlan": [], "/ip/vrf": [], + "/ip/address": [{ ".id": "*20", interface: "ether-mgmt", address: "127.0.0.1/8", disabled: "false", dynamic: "false" }], "/ipv6/address": [], + } + initial["/interface/bridge/vlan"].push({ ".id": "*30", bridge: "br-users", "vlan-ids": "10", dynamic: "true", "current-untagged": "ether2", disabled: "false" }) + let rows = structuredClone(initial), commands: string[] = [], nextId = 160, lostAck = false, managementReads = 0, primaryIdentityReads = 0 + let beforePrimaryIdentity: (() => void) | undefined + const router = createServer(async (req, res) => { + res.setHeader("content-type", "application/json") + const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, "")) + if (req.method === "GET") { + if (path === "/system/license") { + primaryIdentityReads++ + if (beforePrimaryIdentity) { const run = beforePrimaryIdentity; beforePrimaryIdentity = undefined; run() } + res.end(JSON.stringify({ "system-id": "synthetic-bridge-device" })); return + } + if (path === "/interface") { res.end(JSON.stringify(Object.entries(rows).filter(([key]) => ["/interface/ethernet", "/interface/bridge"].includes(key)).flatMap(([key, values]) => values.map(row => ({ ...row, type: key.endsWith("ethernet") ? "ether" : "bridge" }))))); return } + if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return } + res.end(JSON.stringify(rows[path])); return + } + const base = Object.keys(rows).sort((a, b) => b.length - a.length).find(key => path === key || path.startsWith(key + "/")) + assert.ok(base, "Only declared fixture collections may be mutated") + let raw = ""; for await (const chunk of req) raw += chunk + const input = raw ? JSON.parse(raw) : {} + const fields = Object.fromEntries(Object.entries(input).map(([key, value]) => [key, value === "yes" ? "true" : value === "no" ? "false" : String(value)])) + commands.push(req.method + " " + path) + if (req.method === "PUT") { + const row = { ".id": "*" + (++nextId).toString(16), ...fields, dynamic: "false" }; rows[base].push(row) + if (lostAck) { lostAck = false; req.socket.destroy(); return } + res.end(JSON.stringify(row)); return + } + if (req.method === "PATCH") { const row = rows[base].find(row => row[".id"] === path.slice(base.length + 1)); assert.ok(row); Object.assign(row, fields); res.end(JSON.stringify(row)); return } + if (req.method === "DELETE") { const id = path.slice(base.length + 1); assert.ok(rows[base].some(row => row[".id"] === id)); rows[base] = rows[base].filter(row => row[".id"] !== id); res.end("{}"); return } + res.statusCode = 405; res.end("{}") + }) + const management = createServer((req, res) => { + managementReads++; assert.equal(req.method, "GET"); assert.equal(req.url, "/rest/system/license") + res.setHeader("content-type", "application/json"); res.end(JSON.stringify({ "system-id": "synthetic-bridge-device" })) + }) + await Promise.all([new Promise(resolve => router.listen(0, "127.0.0.1", resolve)), new Promise(resolve => management.listen(0, "127.0.0.1", resolve))]) + const cpu = { resource: "bridge", id: "*1" }, access = { resource: "ethernet", id: "*2" }, trunk = { resource: "ethernet", id: "*3" } + const desired: BridgeVlanDesired = { + filtering: true, pvid: 1, + ports: [{ id: "*10", interface: access, pvid: 10, ingressFiltering: true, frameTypes: "admit-all", disabled: false }, { interface: trunk, pvid: 1, ingressFiltering: true, frameTypes: "admit-only-vlan-tagged", disabled: false }], + vlans: [{ id: "*11", vlanIds: "10", tagged: [cpu], untagged: [access], disabled: false }, { vlanIds: "20", tagged: [cpu, trunk], untagged: [], disabled: false }], removePortIds: [], removeVlanIds: [], + } + const input = { serverId, bridgeId: "*1", desired } + const issue = async (name: string, serverIds = [serverId], permissions = ["mm:network:write"], operationIds = ["network.bridge-vlan.read", "network.bridge-vlan.configure", "changes.apply", "operations.read", "operations.recover"], mode: "approval" | "autonomous" = "approval") => { + const result = await createMcpConnection(mcpConnectionCreateSchema.parse({ name, mode, expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions, scope: { allowGlobal: false, serverIds, operationIds }, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), actor) + connections.push(result.connection.id); return { ...result, principal: await authenticateMcpToken(result.token) } + } + const prepare = (principal: Awaited>) => invokeMcpTool(principal, "mm_bridge_vlan_configuration_plan", input) as Promise<{ planId: string; state: string }> + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,enabled,password) VALUES($1,'bridge-owned','127.0.0.1',$2,false,true,'synthetic-password')", [serverId, (router.address() as { port: number }).port]) + await pool.query("INSERT INTO mcp_management_channels(id,server_id,config_sealed,identity_hash,attestation,created_by,generation,expires_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8)", [channelId, serverId, await encryptSecret(JSON.stringify({ host: "127.0.0.1", port: (management.address() as { port: number }).port, useSsl: false, verifySsl: true, username: "fixture", password: "synthetic-password" })), fingerprint("synthetic-bridge-device"), "Synthetic fixture: no independent network or TLS registration claim", actor.id, await accessGeneration(), new Date(Date.now() + 1800000)]) + const access = await issue("Bridge linked acceptance"), principal = access.principal + const token = await new SignJWT({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + const headers = { authorization: "Bearer " + token } + const ownerRead = await app.inject({ method: "GET", url: `/api/mcp/devices/${serverId}/bridge-vlan/configuration?connectionId=${access.connection.id}`, headers }) + assert.equal(ownerRead.statusCode, 200, ownerRead.body) + assert.deepEqual(ownerRead.json(), await invokeMcpTool(principal, "mm_bridge_vlan_configuration_get", { serverId })) + const denied = await issue("Bridge excluded server", []), observer = await issue("Bridge read only", [serverId], ["mm:network:read"]) + await assert.rejects(prepare(denied.principal), (error: unknown) => (error as { code?: string }).code === "SERVER_DENIED") + await assert.rejects(prepare(observer.principal), (error: unknown) => (error as { code?: string }).code === "CONNECTION_PERMISSION_MISSING") + assert.deepEqual(rows, initial); assert.equal(commands.length, 0) + const ownerPlan = await app.inject({ method: "POST", url: `/api/mcp/devices/${serverId}/bridge-vlan/configuration/plan`, headers, payload: { connectionId: access.connection.id, bridgeId: input.bridgeId, desired } }) + assert.equal(ownerPlan.statusCode, 200, ownerPlan.body) + const plan = ownerPlan.json() as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval"); assert.deepEqual(rows, initial); assert.equal(commands.length, 0) + const key = randomUUID() + const apply = (planId: string, idempotencyKey = key) => invokeMcpTool(principal, "mm_change_apply", { planId, idempotencyKey }) as Promise<{ id: string }> + const wait = async (id: string, operationPrincipal = principal) => { + pending.add(id) + for (let n = 0; n < 1200; n++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [id])).rows[0]?.state as string + if (!["pending", "running"].includes(state)) { + pending.delete(id) + return await invokeMcpTool(operationPrincipal, "mm_operation_get", { operationId: id }) as { state: string; verification: string; error_code: string; steps: { state: string }[] } + } + await delay(25) + } + throw Error("Bridge VLAN fixture operation unresolved; do not replay or clean live state") + } + await assert.rejects(apply(plan.planId)); assert.equal(commands.length, 0) + const approval = await app.inject({ method: "POST", url: `/api/mcp/plans/${plan.planId}/decision`, headers, payload: { approve: true } }) + assert.equal(approval.statusCode, 200, approval.body) + const operation = await apply(plan.planId), result = await wait(operation.id) + assert.equal(result.state, "applied", JSON.stringify(result)); assert.equal(result.verification, "passed") + assert.equal(result.steps.length, 3); assert.ok(result.steps.every(step => step.state === "verified")) + assert.deepEqual(commands, ["PUT /interface/bridge/vlan", "PUT /interface/bridge/port", "PATCH /interface/bridge/*1"]) + assert.equal(rows["/interface/bridge"][0]["vlan-filtering"], "true") + for (const path of ["/interface/bridge/port", "/interface/bridge/vlan"]) for (const row of initial[path]) assert.deepEqual(rows[path].find(value => value[".id"] === row[".id"]), row, "Conserve static and foreign configuration") + assert.deepEqual(rows["/interface/bridge"][1], initial["/interface/bridge"][1]); assert.equal(rows["/interface/bridge"][0].opaque, "preserve-bridge") + const readback = await invokeMcpTool(principal, "mm_bridge_vlan_configuration_get", { serverId }) as { state: { vlans: Row[]; ports: Row[]; bridges: Row[] } } + assert.equal(readback.state.vlans.length, 3); assert.equal(readback.state.ports.length, 3); assert.equal(readback.state.bridges[0]["vlan-filtering"], "true") + assert.equal((await apply(plan.planId)).id, operation.id); assert.equal(commands.length, 3) + const recover = (operationId: string, sourcePrincipal = principal) => invokeMcpTool(sourcePrincipal, "mm_operation_recovery_plan", { operationId }) as Promise<{ planId: string; state: string }> + const completedForward = structuredClone(rows) + rows["/interface/bridge/vlan"][1].comment = "external-recovery-drift" + await assert.rejects(recover(operation.id), (error: unknown) => (error as { code?: string }).code === "STATE_CONFLICT") + assert.equal(commands.length, 3); rows = completedForward + const createReceipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0].result + await pool.query("UPDATE mcp_operation_steps SET result=$2::jsonb WHERE operation_id=$1 AND step_index=0", [operation.id, JSON.stringify({ ...createReceipt, ".id": "*beef" })]) + try { await assert.rejects(recover(operation.id), (error: unknown) => (error as { code?: string }).code === "STATE_CONFLICT") } + finally { await pool.query("UPDATE mcp_operation_steps SET result=$2::jsonb WHERE operation_id=$1 AND step_index=0", [operation.id, JSON.stringify(createReceipt)]) } + assert.equal(commands.length, 3, "A checkpoint delta cannot replace the actual create ACK") + const savedEvidence = (await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [plan.planId])).rows[0].evidence + const obsolete = JSON.parse(await decryptSecret(savedEvidence.sealed)) + obsolete.snapshots.find((snapshot: { resource: string }) => snapshot.resource === "bridge_port").projectionVersion = 3 + await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [plan.planId, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(obsolete)) })]) + try { await assert.rejects(recover(operation.id), (error: unknown) => (error as { code?: string }).code === "STATE_CONFLICT") } + finally { await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [plan.planId, JSON.stringify(savedEvidence)]) } + const newChannelId = randomUUID() + await pool.query("UPDATE mcp_management_channels SET id=$2 WHERE id=$1", [channelId, newChannelId]) + const inverse = await recover(operation.id) + assert.equal(inverse.state, "awaiting-approval"); assert.equal(commands.length, 3) + const inverseEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [inverse.planId])).rows[0].evidence.sealed)) + assert.equal(inverseEvidence.managementChannels[0].channelId, newChannelId, "Inverse freshly verifies current management, never copies the expired source receipt") + await assert.rejects(apply(inverse.planId, randomUUID())); assert.equal(commands.length, 3) + await approvePlan(inverse.planId, actor, true) + const inverseOperation = await apply(inverse.planId, randomUUID()), inverseResult = await wait(inverseOperation.id) + assert.equal(inverseResult.state, "applied", JSON.stringify(inverseResult)); assert.equal(inverseResult.verification, "passed") + assert.ok(inverseResult.steps.every(step => step.state === "verified")) + assert.deepEqual(commands.slice(3).map(command => command.replace(/\*[^/]+$/, "*id")), ["PATCH /interface/bridge/*id", "DELETE /interface/bridge/vlan/*id", "DELETE /interface/bridge/port/*id"]) + assert.deepEqual(rows, initial, "Approved typed recovery restores the exact baseline and preserves foreign rows") + rows = structuredClone(initial); commands = [] + // Legacy tools get their original operation grants only. They cannot gain + // linked configuration authority or emit extra native resource changes. + const generic = await issue("Bridge legacy safety", [serverId], ["mm:network:write"], ["ros.bridge.update", "ros.bridge_port.update", "ros.bridge_vlan.update", "interfaces.update", "changes.apply", "operations.read", "operations.recover"]) + const genericPlan = (tool: string, body: Record) => invokeMcpTool(generic.principal, tool, { serverId, ...body }) as Promise<{ planId: string; state: string }> + const plansBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n + rows["/interface/bridge/vlan"][0].untagged = "" + rows["/interface/bridge/vlan"][0].tagged = "br-users,ether2" + const invalidFiltering = structuredClone(rows) + for (const [tool, body] of [ + ["mm_bridge_update_plan", { id: "*1", fields: { "vlan-filtering": true } }], + ["mm_interfaces_update_plan", { resource: "bridge", id: "*1", fields: { "vlan-filtering": true } }], + ] as const) { + await assert.rejects(genericPlan(tool, body), (error: unknown) => (error as { code?: string }).code === "INVALID_INPUT") + assert.deepEqual(rows, invalidFiltering); assert.equal(commands.length, 0) + } + rows = structuredClone(initial) + await assert.rejects(genericPlan("mm_bridge_vlan_update_plan", { id: "*11", fields: { bridge: "br-foreign" } }), (error: unknown) => (error as { code?: string }).code === "STATE_CONFLICT") + await assert.rejects(genericPlan("mm_bridge_vlan_update_plan", { id: "*11", fields: { tagged: "br-users,ether2" } }), (error: unknown) => (error as { code?: string }).code === "INVALID_INPUT") + assert.deepEqual(rows, initial); assert.equal(commands.length, 0) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n, plansBefore, "Invalid legacy requests produce neither plans nor writes") + let raced = false + beforePrimaryIdentity = () => { raced = true; rows["/interface/bridge/port"][0].pvid = "11" } + await assert.rejects(genericPlan("mm_bridge_port_update_plan", { id: "*10", fields: { "ingress-filtering": false } }), (error: unknown) => { + const value = error as { code?: string; message?: string } + return value.code === "STATE_CONFLICT" && !!value.message?.includes("незапрошенные поля") + }) + assert.equal(raced, true); assert.equal(commands.length, 0) + assert.equal(rows["/interface/bridge/port"][0].pvid, "11", "Concurrent PVID is not silently reverted by a different requested field") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n, plansBefore) + rows = structuredClone(initial) + const single = await genericPlan("mm_bridge_port_update_plan", { id: "*10", fields: { "ingress-filtering": false } }) + assert.equal(single.state, "awaiting-approval"); assert.deepEqual(rows, initial) + await approvePlan(single.planId, actor, true) + const singleOperation = await invokeMcpTool(generic.principal, "mm_change_apply", { planId: single.planId, idempotencyKey: randomUUID() }) as { id: string } + const singleResult = await wait(singleOperation.id, generic.principal) + assert.equal(singleResult.state, "applied", JSON.stringify(singleResult)); assert.equal(singleResult.verification, "passed") + assert.deepEqual(singleResult.steps.map(step => step.state), ["verified"]) + assert.deepEqual(commands, ["PATCH /interface/bridge/port/*10"]) + const expected = structuredClone(initial); expected["/interface/bridge/port"][0]["ingress-filtering"] = "false" + assert.deepEqual(rows, expected, "Valid single-field legacy change emits exactly its original command") + const singleInverse = await recover(singleOperation.id, generic.principal) + await approvePlan(singleInverse.planId, actor, true) + const singleRecovered = await invokeMcpTool(generic.principal, "mm_change_apply", { planId: singleInverse.planId, idempotencyKey: randomUUID() }) as { id: string } + const singleRecoveryResult = await wait(singleRecovered.id, generic.principal) + assert.equal(singleRecoveryResult.state, "applied", JSON.stringify(singleRecoveryResult)); assert.equal(singleRecoveryResult.verification, "passed") + assert.deepEqual(rows, initial, "Sensitive generic source uses typed inverse without linked configure grant") + rows = structuredClone(initial); commands = [] + const deletionForward = await prepare(principal); await approvePlan(deletionForward.planId, actor, true) + assert.equal((await wait((await apply(deletionForward.planId, randomUUID())).id)).state, "applied") + const beforeDeletion = (await invokeMcpTool(principal, "mm_bridge_vlan_configuration_get", { serverId }) as { state: BridgeVlanState }).state + const removedPortId = String(beforeDeletion.ports.find(row => row.interface === "ether3")![".id"]) + const removedVlanId = String(beforeDeletion.vlans.find(row => row["vlan-ids"] === "20")![".id"]) + const deletionDesired = bridgeVlanCurrentDesired(beforeDeletion, "*1") + deletionDesired.ports = deletionDesired.ports.filter(row => row.id !== removedPortId); deletionDesired.removePortIds = [removedPortId] + deletionDesired.vlans = deletionDesired.vlans.filter(row => row.id !== removedVlanId); deletionDesired.removeVlanIds = [removedVlanId] + const deletion = await invokeMcpTool(principal, "mm_bridge_vlan_configuration_plan", { serverId, bridgeId: "*1", desired: deletionDesired }) as { planId: string } + await approvePlan(deletion.planId, actor, true) + const deletedOperation = await apply(deletion.planId, randomUUID()), deletedResult = await wait(deletedOperation.id) + assert.equal(deletedResult.state, "applied", JSON.stringify(deletedResult)); assert.ok(deletedResult.steps.every(step => step.state === "verified")) + const recreate = await recover(deletedOperation.id); assert.equal(recreate.state, "awaiting-approval") + await approvePlan(recreate.planId, actor, true) + const recreated = await wait((await apply(recreate.planId, randomUUID())).id) + assert.equal(recreated.state, "applied", JSON.stringify(recreated)); assert.equal(recreated.verification, "passed"); assert.ok(recreated.steps.every(step => step.state === "verified")) + const afterRecreation = (await invokeMcpTool(principal, "mm_bridge_vlan_configuration_get", { serverId }) as { state: BridgeVlanState }).state + assert.notEqual(afterRecreation.ports.find(row => row.interface === "ether3")![".id"], removedPortId) + assert.notEqual(afterRecreation.vlans.find(row => row["vlan-ids"] === "20")![".id"], removedVlanId) + const topology = (state: BridgeVlanState) => ({ ...state, + ports: state.ports.map(({ ".id": _id, ...row }) => row).sort((a, b) => String(a.bridge + ":" + a.interface).localeCompare(String(b.bridge + ":" + b.interface))), + vlans: state.vlans.map(({ ".id": _id, ...row }) => row).sort((a, b) => String(a.bridge + ":" + a["vlan-ids"]).localeCompare(String(b.bridge + ":" + b["vlan-ids"]))), + }) + assert.deepEqual(topology(afterRecreation), topology(beforeDeletion), "Deleted memberships and ports restore configuration with newly acknowledged IDs") + for (const path of ["/interface/bridge/port", "/interface/bridge/vlan"]) for (const row of initial[path]) assert.deepEqual(rows[path].find(value => value[".id"] === row[".id"]), row) + rows = structuredClone(initial); commands = [] + const stale = await prepare(principal); await approvePlan(stale.planId, actor, true) + rows["/interface/bridge/vlan"][1].comment = "external-drift" + const drift = await wait((await apply(stale.planId, randomUUID())).id) + assert.equal(drift.state, "rejected", JSON.stringify(drift)); assert.equal(commands.length, 0, "Drift prevents every native write") + rows = structuredClone(initial); commands = [] + const lost = await prepare(principal); await approvePlan(lost.planId, actor, true); lostAck = true + const lostKey = randomUUID(), uncertain = await apply(lost.planId, lostKey), unknown = await wait(uncertain.id) + assert.equal(unknown.state, "unknown", JSON.stringify(unknown)); assert.equal(commands.length, 1) + assert.equal(rows["/interface/bridge/port"].length, initial["/interface/bridge/port"].length) + assert.equal(rows["/interface/bridge"][0]["vlan-filtering"], "false") + assert.equal((await apply(lost.planId, lostKey)).id, uncertain.id); assert.equal(commands.length, 1, "Unknown ACK never retries or continues") + await assert.rejects(recover(uncertain.id), (error: unknown) => (error as { code?: string }).code === "STATE_CONFLICT") + assert.equal(commands.length, 1, "Unknown source has no inverse authority") + assert.ok(managementReads > 1 && primaryIdentityReads > 1, "Fresh same-identity verification on both synthetic endpoints") + rows = structuredClone(initial); commands = [] + await pool.query("UPDATE mcp_management_channels SET identity_hash=$2 WHERE server_id=$1", [serverId, fingerprint("wrong-device")]) + await assert.rejects(prepare(principal), (error: unknown) => (error as { code?: string }).code === "STATE_CONFLICT") + assert.equal(commands.length, 0, "A registered channel identity mismatch cannot fall back to primary") + await pool.query("DELETE FROM mcp_management_channels WHERE server_id=$1", [serverId]) + rows["/ip/address"][0].address = "192.0.2.1/24" + await assert.rejects(prepare(principal), (error: unknown) => (error as { code?: string }).code === "MANAGEMENT_UNSAFE") + rows = structuredClone(initial) + const stolenManagement = structuredClone(desired) + stolenManagement.ports.push({ interface: { resource: "ethernet", id: "*4" }, pvid: 1, ingressFiltering: true, frameTypes: "admit-only-vlan-tagged", disabled: false }) + stolenManagement.vlans.push({ vlanIds: "30", tagged: [cpu, { resource: "ethernet", id: "*4" }], untagged: [], disabled: false }) + await assert.rejects(invokeMcpTool(principal, "mm_bridge_vlan_configuration_plan", { ...input, desired: stolenManagement }), (error: unknown) => (error as { code?: string }).code === "MANAGEMENT_UNSAFE") + rows["/ip/address"][0].interface = "br-users" + await assert.rejects(prepare(principal), (error: unknown) => (error as { code?: string }).code === "MANAGEMENT_UNSAFE") + assert.equal(commands.length, 0, "Primary admission refuses unconfirmed address, slave stealing and CPU delivery loss") + rows = structuredClone(initial) + const primaryPlan = await prepare(principal) + assert.equal(primaryPlan.state, "awaiting-approval"); assert.equal(commands.length, 0) + assert.equal((primaryPlan as unknown as { changes: { managementChannels?: unknown } }).changes.managementChannels, undefined, "Public primary plan never exposes a fake independent management receipt") + const primaryEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [primaryPlan.planId])).rows[0].evidence.sealed)) + assert.equal(primaryEvidence.requiresHuman, true) + assert.equal(primaryEvidence.managementChannels, undefined, "Primary management is never represented as a registered independent receipt") + assert.ok(primaryEvidence.warnings.some((warning: string) => warning.includes("основной адрес") && warning.includes("независимость канала не подтверждена"))) + assert.ok(Date.parse(primaryEvidence.validUntil) <= Date.parse(principal.credentialExpiresAt) && Date.parse(primaryEvidence.validUntil) <= actor.expiresAt!) + await assert.rejects(apply(primaryPlan.planId, randomUUID())); assert.equal(commands.length, 0) + await approvePlan(primaryPlan.planId, actor, true) + const primaryOperation = await apply(primaryPlan.planId, randomUUID()), primaryResult = await wait(primaryOperation.id) + assert.equal(primaryResult.state, "applied", JSON.stringify(primaryResult)); assert.equal(primaryResult.verification, "passed"); assert.ok(primaryResult.steps.every(step => step.state === "verified")) + const primaryInverse = await recover(primaryOperation.id) + const primaryInverseEvidence = JSON.parse(await decryptSecret((await pool.query("SELECT evidence FROM mcp_plans WHERE id=$1", [primaryInverse.planId])).rows[0].evidence.sealed)) + assert.equal(primaryInverseEvidence.managementChannels, undefined) + await approvePlan(primaryInverse.planId, actor, true) + const primaryRecovered = await wait((await apply(primaryInverse.planId, randomUUID())).id) + assert.equal(primaryRecovered.state, "applied", JSON.stringify(primaryRecovered)); assert.equal(primaryRecovered.verification, "passed") + assert.deepEqual(rows, initial) + // A normal filtering-enabled CPU/access management VLAN may consist only + // of native dynamic PVID membership, without any static VLAN table row. + rows["/interface/bridge"][0]["vlan-filtering"] = "true"; rows["/interface/bridge"][0].pvid = "10" + rows["/ip/address"][0].interface = "br-users" + rows["/interface/bridge/vlan"] = rows["/interface/bridge/vlan"].filter(row => row[".id"] !== "*11") + rows["/interface/bridge/vlan"].find(row => row.dynamic === "true")!["current-untagged"] = "br-users,ether2" + const implicitBaseline = structuredClone(rows) + const implicitState = (await invokeMcpTool(principal, "mm_bridge_vlan_configuration_get", { serverId }) as { state: BridgeVlanState }).state + const implicitDesired = bridgeVlanCurrentDesired(implicitState, "*1") + assert.deepEqual(implicitDesired.vlans, []) + const implicitPrepare = (next: BridgeVlanDesired) => invokeMcpTool(principal, "mm_bridge_vlan_configuration_plan", { serverId, bridgeId: "*1", desired: next }) as Promise<{ planId: string; state: string }> + for (const mutate of [ + (next: BridgeVlanDesired) => { next.pvid = 11 }, + (next: BridgeVlanDesired) => { next.ports[0].pvid = 11 }, + (next: BridgeVlanDesired) => { next.frameTypes = "admit-only-vlan-tagged" }, + ]) { + const unsafe = structuredClone(implicitDesired); mutate(unsafe) + await assert.rejects(implicitPrepare(unsafe), (error: unknown) => (error as { code?: string }).code === "MANAGEMENT_UNSAFE") + } + for (const [tool, body] of [ + ["mm_bridge_update_plan", { id: "*1", fields: { pvid: 11 } }], + ["mm_bridge_port_update_plan", { id: "*10", fields: { pvid: 11 } }], + ["mm_interfaces_update_plan", { resource: "bridge", id: "*1", fields: { "frame-types": "admit-only-vlan-tagged" } }], + ] as const) await assert.rejects(genericPlan(tool, body), (error: unknown) => (error as { code?: string }).code === "MANAGEMENT_UNSAFE") + const writesBeforeImplicit = commands.length + rows["/interface/bridge"][0].mvrp = "true" + await assert.rejects(implicitPrepare(implicitDesired), (error: unknown) => (error as { code?: string }).code === "UNSUPPORTED_CAPABILITY") + rows = structuredClone(implicitBaseline) + for (const field of ["frame-types", "ingress-filtering"]) { + delete rows["/interface/bridge"][0][field] + await assert.rejects(implicitPrepare(implicitDesired)) + rows = structuredClone(implicitBaseline) + } + assert.equal(commands.length, writesBeforeImplicit) + implicitDesired.ports[0].ingressFiltering = false + const implicitPlan = await implicitPrepare(implicitDesired) + assert.equal(implicitPlan.state, "awaiting-approval"); assert.equal(commands.length, writesBeforeImplicit) + await approvePlan(implicitPlan.planId, actor, true) + const implicitOperation = await apply(implicitPlan.planId, randomUUID()), implicitResult = await wait(implicitOperation.id) + assert.equal(implicitResult.state, "applied", JSON.stringify(implicitResult)); assert.equal(implicitResult.verification, "passed") + assert.deepEqual(implicitResult.steps.map(step => step.state), ["verified"]) + assert.deepEqual(commands.slice(writesBeforeImplicit), ["PATCH /interface/bridge/port/*10"], "Derived dynamic PVID rows never become native commands") + const implicitInverse = await recover(implicitOperation.id) + await approvePlan(implicitInverse.planId, actor, true) + const implicitRecovered = await wait((await apply(implicitInverse.planId, randomUUID())).id) + assert.equal(implicitRecovered.state, "applied", JSON.stringify(implicitRecovered)); assert.equal(implicitRecovered.verification, "passed") + assert.deepEqual(rows, implicitBaseline, "Implicit-PVID CPU management survives approved forward and typed inverse") + const beforeCpuEdit = commands.length + const cpuOnly = await genericPlan("mm_interfaces_update_plan", { resource: "bridge", id: "*1", fields: { "ingress-filtering": false } }) + await approvePlan(cpuOnly.planId, actor, true) + const cpuOperation = await invokeMcpTool(generic.principal, "mm_change_apply", { planId: cpuOnly.planId, idempotencyKey: randomUUID() }) as { id: string } + const cpuResult = await wait(cpuOperation.id, generic.principal) + assert.equal(cpuResult.state, "applied", JSON.stringify(cpuResult)); assert.equal(cpuResult.verification, "passed") + assert.deepEqual(commands.slice(beforeCpuEdit), ["PATCH /interface/bridge/*1"]) + const cpuInverse = await recover(cpuOperation.id, generic.principal); await approvePlan(cpuInverse.planId, actor, true) + const cpuRecovered = await invokeMcpTool(generic.principal, "mm_change_apply", { planId: cpuInverse.planId, idempotencyKey: randomUUID() }) as { id: string } + assert.equal((await wait(cpuRecovered.id, generic.principal)).state, "applied") + assert.deepEqual(rows, implicitBaseline, "CPU ingress alias edit and typed inverse preserve implicit PVID management without extra authority") + rows = structuredClone(initial) + const autonomous = await issue("Primary still human approved", [serverId], ["mm:network:write"], undefined, "autonomous") + const humanOnly = await prepare(autonomous.principal) + assert.equal(humanOnly.state, "awaiting-approval") + const beforeAutonomousApply = commands.length + await assert.rejects(invokeMcpTool(autonomous.principal, "mm_change_apply", { planId: humanOnly.planId, idempotencyKey: randomUUID() }), (error: unknown) => (error as { code?: string }).code === "APPROVAL_REQUIRED") + assert.equal(commands.length, beforeAutonomousApply); assert.deepEqual(rows, initial) + console.log("✓ Linked bridge VLAN SQL/HTTP: approved forward/recovery and delete recreation, fresh registered or human-only primary management, unsafe primary/identity and autonomous bypass refusals, terminal drift/ACK/projection guards, REST/MCP and legacy safety; HTTP fixture only, no TLS/independence claim") + } finally { + if (!pending.size) { + for (const id of connections) await setConnectionStatus(id, "paused", actor) + await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + } + await Promise.all([new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())), new Promise((resolve, reject) => management.close(error => error ? reject(error) : resolve()))]) + } +} diff --git a/backend/src/test/mcp-configuration-audit-admission.ts b/backend/src/test/mcp-configuration-audit-admission.ts new file mode 100644 index 00000000..fe0f89a7 --- /dev/null +++ b/backend/src/test/mcp-configuration-audit-admission.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { pool } from "../db/index.js" +import { SYSTEM_DATABASE_OPERATION_LOCK, withOperationLock } from "../db/operation-lock.js" +import { assertDatabaseRestoreAllowed, executeGuardedApplicationWrite, executeGuardedConfigurationWrite, withConfigurationActor } from "../services/config-mutation-policy.js" + +export async function verifyMcpConfigurationAuditAdmission() { + const exclusive = (work: () => Promise) => withOperationLock(() => pool.connect(), SYSTEM_DATABASE_OPERATION_LOCK, work) + const snapshot = (actorId: string) => pool.query("SELECT action,payload->>'writeId' AS id FROM mcp_audit a WHERE actor_id=$1 ORDER BY a.id", [actorId]).then(result => result.rows) + for (const [kind, guarded] of [ + ["router", (work: () => Promise) => executeGuardedConfigurationWrite(712, "PATCH", "/interface/*1", work)], + ["application", (work: () => Promise) => executeGuardedApplicationWrite({ kind: "fixture", id: "1" }, [], work)], + ] as const) { + const actorId = `owned-${kind}-audit`, run = (work: () => Promise) => withConfigurationActor(actorId, () => guarded(work)) + let calls = 0 + const dispatched = async () => { calls++; return "fixture ack" } + await exclusive(async () => assert.rejects(run(dispatched), { code: "PROTOCOL_STORAGE_BUSY" })) + assert.equal(calls, 0) + assert.deepEqual(await snapshot(actorId), []) + assert.equal(await run(async () => { + assert.equal((await snapshot(actorId))[0]?.action, "configuration.intent") + return dispatched() + }), "fixture ack") + const normal = await snapshot(actorId) + assert.deepEqual(normal.map(row => row.action), ["configuration.intent", "configuration.acknowledged"]) + assert.equal(normal[0].id, normal[1].id) + for (const acknowledged of [true, false]) { + const lock = await pool.connect() + try { + await lock.query("BEGIN") + await assert.rejects(run(async () => { + await dispatched() + await lock.query("SELECT pg_advisory_xact_lock($1::bigint)", [SYSTEM_DATABASE_OPERATION_LOCK]) + if (!acknowledged) throw Error("fixture transport acknowledgement lost") + return "fixture ack" + }), { code: "MUTATION_OUTCOME_UNKNOWN" }) + } finally { try { await lock.query("ROLLBACK") } finally { lock.release() } } + } + assert.equal(calls, 3) + assert.deepEqual((await snapshot(actorId)).map(row => row.action), ["configuration.intent", "configuration.acknowledged", "configuration.intent", "configuration.intent"]) + } + const protection = randomUUID(), actorId = "owned-restore-audit" + try { + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "server", id: "712", protection: "deny" }), actorId]) + await exclusive(() => withConfigurationActor(actorId, () => assert.rejects(assertDatabaseRestoreAllowed(), { code: "PROTECTED_OBJECT" }))) + assert.deepEqual((await snapshot(actorId)).map(row => row.action), ["configuration.denied"]) + } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) } + console.log("Configuration audit admission: no dispatch before intent, unknown on lost terminal audit, restore self-lock exception passed") +} diff --git a/backend/src/test/mcp-database-backup.ts b/backend/src/test/mcp-database-backup.ts new file mode 100644 index 00000000..ec8d4dd7 --- /dev/null +++ b/backend/src/test/mcp-database-backup.ts @@ -0,0 +1,158 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import type { FastifyInstance } from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { env } from "../config.js" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { accessGeneration, decryptBinary } from "../modules/mcp/security.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { readHumanDatabaseBackupArtifact, humanDatabaseBackupMetadata } from "../modules/mcp/database-backup-artifact.js" +import { sharedDatabaseGate } from "../services/shared-database-gate.js" +import { findMcpService } from "../modules/mcp/service-registry.js" + +/** Owned disposable SQL/HTTP acceptance helper; requires real PostgreSQL client tools on PATH. */ +export async function verifyMcpDatabaseBackup(actor: AuthUser, app: FastifyInstance) { + const owner = { ...actor, id: "database-backup-owner", permissions: [...new Set([...actor.permissions, "mm:settings:admin"])] } + const serverIds = (await pool.query<{ id: number }>("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id)) + const generation = await accessGeneration(), originalQuery = pool.query.bind(pool) + const issue = (scope = { serverIds, operationIds: ["database.backup"], allowGlobal: true }) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Database export fixture", mode: "autonomous", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:settings:admin"], scope, limits: { callsPerMinute: 120, mutationsPerHour: 100 } }), owner) + const issued = await issue(), principal = await authenticateMcpToken(issued.token) + const jwt = await new SignJWT({ sub: owner.id, apps: ["mm"], permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + const headers = { authorization: "Bearer " + jwt }, prepare = () => invokeMcpTool(principal, "mm_database_backup_plan", {}) as Promise<{ planId: string; state: string }> + const wait = async (id: string) => { for (let i = 0; i < 8000; i++) { const row = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) } throw Error("Database export still active; never replay") } + let blocker: (() => void) | undefined + let protection: string | undefined + let terminalRevoked = false + try { + const narrow = await issue({ serverIds: [], operationIds: ["database.backup"], allowGlobal: false }) + try { await assert.rejects(invokeMcpTool(await authenticateMcpToken(narrow.token), "mm_database_backup_plan", {})) } finally { await setConnectionStatus(narrow.connection.id, "paused", owner) } + protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "user", id: "unrelated-private-user", protection: "deny" }), owner.id]) + await assert.rejects(prepare()) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]); protection = undefined + const originalDatabaseUrl = env.DATABASE_URL + const unsupportedConnection = new URL(originalDatabaseUrl) + unsupportedConnection.searchParams.set("service", "unsupported_export_fixture") + const plansBeforeUnsupported = Number((await pool.query("SELECT count(*) AS n FROM mcp_plans WHERE connection_id=$1", [issued.connection.id])).rows[0].n) + try { + env.DATABASE_URL = unsupportedConnection.toString() + await assert.rejects(prepare(), error => !!error && typeof error === "object" && "code" in error && error.code === "DATABASE_DUMP_CONNECTION_UNSUPPORTED") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_plans WHERE connection_id=$1", [issued.connection.id])).rows[0].n), plansBeforeUnsupported, "Unsupported URI is refused before an approval plan or intent exists") + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_artifacts WHERE connection_id=$1", [issued.connection.id])).rows[0].n), 0) + } finally { env.DATABASE_URL = originalDatabaseUrl } + const plan = await prepare() + assert.equal(plan.state, "awaiting-approval", "Autonomous mode still needs human disclosure approval") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_artifacts WHERE connection_id=$1", [issued.connection.id])).rows[0].n, 0, "Planning must not create a dump") + await assert.rejects(applyPlan(principal, plan.planId, randomUUID())) + await approvePlan(plan.planId, owner, true) + const waiting = sharedDatabaseGate.request(() => new Promise(resolve => { blocker = resolve })) + const key = randomUUID() + const operation = await sharedDatabaseGate.request(() => applyPlan(principal, plan.planId, key)) + await delay(50) + assert.equal((await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0].state, "pending", "Exclusive export drains admitted requests before worker/config locks") + blocker!(); blocker = undefined; await waiting + const done = await wait(operation.id) + assert.equal(done.state, "applied", JSON.stringify(done)) + assert.equal(await accessGeneration(), generation, "Backup must not invalidate MCP access") + const receipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0].result + assert.equal(receipt.humanDownloadOnly, true); assert.equal(receipt.databaseContentReturned, false) + assert.ok(!JSON.stringify(receipt).includes("PGDMP")); assert.ok(!JSON.stringify(plan).includes(env.DATABASE_URL)) + const stored = (await pool.query("SELECT * FROM mcp_artifacts WHERE id=$1", [receipt.artifactId])).rows[0] + const checkpoint = (await pool.query("SELECT intended_state FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [operation.id])).rows[0].intended_state + const reconcile = () => findMcpService("database.backup")!.reconcile!(checkpoint.input, async () => {}) + assert.deepEqual(await reconcile(), { matchesDesiredState: true, matchesOriginalState: false }) + await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1", [operation.id]) + assert.deepEqual(await reconcile(), { matchesDesiredState: false, matchesOriginalState: false }, "An unverified acknowledgement does not prove export") + await pool.query("UPDATE mcp_operation_steps SET state='verified',intended_state=jsonb_set(intended_state,'{input,attemptRef}',to_jsonb($2::text)) WHERE operation_id=$1", [operation.id, randomUUID()]) + assert.deepEqual(await reconcile(), { matchesDesiredState: false, matchesOriginalState: false }, "No exact attempt checkpoint remains unresolved") + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb,result=jsonb_set(result,'{artifactId}',to_jsonb($3::text)) WHERE operation_id=$1", [operation.id, JSON.stringify(checkpoint), randomUUID()]) + assert.deepEqual(await reconcile(), { matchesDesiredState: false, matchesOriginalState: false }, "A foreign or missing artifact cannot be adopted") + await pool.query("UPDATE mcp_operation_steps SET result=$2::jsonb WHERE operation_id=$1", [operation.id, JSON.stringify(receipt)]) + assert.deepEqual(await reconcile(), { matchesDesiredState: true, matchesOriginalState: false }) + const content = await decryptBinary(Buffer.from(stored.ciphertext, "base64")) + assert.equal(content.subarray(0, 5).toString(), "PGDMP") + assert.equal(content.length, receipt.bytes); assert.equal(createHash("sha256").update(content).digest("hex"), receipt.sha256) + assert.deepEqual((await readHumanDatabaseBackupArtifact(receipt.artifactId, owner)).content, content) + const response = await app.inject({ url: receipt.downloadPath, headers }) + assert.equal(response.statusCode, 200, response.body.slice(0, 200)); assert.deepEqual(response.rawPayload, content) + const listUrl = `/api/mcp/operations?connectionId=${issued.connection.id}` + const listed = async () => { const response = await app.inject({ url: listUrl, headers }); assert.equal(response.statusCode, 200, response.body); return response.json().items.find((item: { id: string }) => item.id === operation.id) } + let payloadReads = 0 + pool.query = ((...args: unknown[]) => { const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text; if (/SELECT ciphertext|pg_dump/i.test(sql ?? "")) payloadReads++; return (originalQuery as (...args: unknown[]) => unknown)(...args) }) as typeof pool.query + try { + assert.deepEqual((await listed()).artifactReceipts, [{ artifactId: receipt.artifactId, bytes: receipt.bytes, sha256: receipt.sha256, humanDownloadOnly: true, databaseContentReturned: false, downloadPath: receipt.downloadPath }]) + assert.equal(payloadReads, 0, "Receipt listing is metadata only") + } finally { pool.query = originalQuery as typeof pool.query } + const metadata = (await pool.query("SELECT updated_at,routing_endpoint_epoch FROM servers WHERE id=$1", [serverIds[0]])).rows[0] + assert.ok(metadata, "Owned fixture needs a server for non-security metadata/endpoint epoch acceptance") + try { + await pool.query("UPDATE servers SET updated_at=clock_timestamp() WHERE id=$1", [serverIds[0]]) + assert.equal((await pool.query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1", [serverIds[0]])).rows[0].routing_endpoint_epoch, metadata.routing_endpoint_epoch) + assert.deepEqual((await readHumanDatabaseBackupArtifact(receipt.artifactId, owner)).content, content) + assert.equal((await listed()).artifactReceipts[0].artifactId, receipt.artifactId) + } finally { await pool.query("UPDATE servers SET updated_at=$2 WHERE id=$1", [serverIds[0], metadata.updated_at]) } + assert.equal((await app.inject({ url: receipt.downloadPath, headers: { authorization: "Bearer " + issued.token } })).statusCode, 401) + assert.equal((await app.inject({ url: receipt.downloadPath })).statusCode, 401) + await assert.rejects(readHumanDatabaseBackupArtifact(receipt.artifactId, { ...owner, id: "foreign-admin" })) + await assert.rejects(readHumanDatabaseBackupArtifact(receipt.artifactId, { ...owner, permissions: [] })) + await applyPlan(principal, plan.planId, key) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_artifacts WHERE connection_id=$1", [issued.connection.id])).rows[0].n, 1, "Idempotency cannot dump again") + for (const [field, value] of [["filename", "tampered.dump"], ["ciphertext", stored.ciphertext + "x"], ["access_scope", { ...stored.access_scope, kind: "logs" }]] as const) { + if (field === "access_scope") await pool.query("UPDATE mcp_artifacts SET access_scope=$2::jsonb WHERE id=$1", [receipt.artifactId, JSON.stringify(value)]) + else await pool.query(`UPDATE mcp_artifacts SET ${field}=$2 WHERE id=$1`, [receipt.artifactId, value]) + await assert.rejects(readHumanDatabaseBackupArtifact(receipt.artifactId, owner)) + await pool.query("UPDATE mcp_artifacts SET filename=$2,ciphertext=$3,access_scope=$4::jsonb WHERE id=$1", [receipt.artifactId, stored.filename, stored.ciphertext, JSON.stringify(stored.access_scope)]) + } + await pool.query("UPDATE mcp_artifacts SET expires_at=now()-interval '1 second' WHERE id=$1", [receipt.artifactId]); await assert.rejects(humanDatabaseBackupMetadata(receipt.artifactId, owner)); assert.deepEqual((await listed()).artifactReceipts, []) + await pool.query("UPDATE mcp_artifacts SET expires_at=$2 WHERE id=$1", [receipt.artifactId, stored.expires_at]) + protection = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "user", id: "late-private-user", protection: "deny" }), owner.id]) + await assert.rejects(readHumanDatabaseBackupArtifact(receipt.artifactId, owner)); assert.deepEqual((await listed()).artifactReceipts, []) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]); protection = undefined + const cancelled = new AbortController(); cancelled.abort(); await assert.rejects(readHumanDatabaseBackupArtifact(receipt.artifactId, owner, undefined, cancelled.signal)) + let fired = false + pool.query = ((...args: unknown[]) => { const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text; const pending = (originalQuery as (...args: unknown[]) => Promise)(...args); if (!fired && sql?.includes("MCP database artifact payload")) { fired = true; return pending.then(async result => { await setConnectionStatus(issued.connection.id, "paused", owner); return result }) } return pending }) as typeof pool.query + try { await assert.rejects(readHumanDatabaseBackupArtifact(receipt.artifactId, owner)); assert.equal(fired, true) } finally { pool.query = originalQuery as typeof pool.query } + await setConnectionStatus(issued.connection.id, "active", owner) + await assert.rejects(readHumanDatabaseBackupArtifact(receipt.artifactId, owner), "Pause/resume ABA cannot revive an old archive") + assert.deepEqual((await listed()).artifactReceipts, []) + const securityPlan = await prepare(); await approvePlan(securityPlan.planId, owner, true) + const securityOperation = await sharedDatabaseGate.request(() => applyPlan(principal, securityPlan.planId, randomUUID())) + assert.equal((await wait(securityOperation.id)).state, "applied") + const securityReceipt = (await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0", [securityOperation.id])).rows[0].result + assert.equal((await humanDatabaseBackupMetadata(securityReceipt.artifactId, owner)).kind, "database-backup") + const securityEndpoint = (await pool.query("SELECT password,routing_endpoint_epoch FROM servers WHERE id=$1", [serverIds[0]])).rows[0] + try { + await pool.query("UPDATE servers SET password=$2 WHERE id=$1", [serverIds[0], "database-backup-credential-fixture"]) + await assert.rejects(readHumanDatabaseBackupArtifact(securityReceipt.artifactId, owner)) + } finally { await pool.query("UPDATE servers SET password=$2 WHERE id=$1", [serverIds[0], securityEndpoint.password]) } + assert.notEqual((await pool.query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1", [serverIds[0]])).rows[0].routing_endpoint_epoch, securityEndpoint.routing_endpoint_epoch) + await assert.rejects(readHumanDatabaseBackupArtifact(securityReceipt.artifactId, owner), "Credential A→B→A permanently invalidates the archive") + const afterSecurity = await app.inject({ url: listUrl, headers }); assert.equal(afterSecurity.statusCode, 200) + assert.deepEqual(afterSecurity.json().items.find((item: { id: string }) => item.id === securityOperation.id).artifactReceipts, []) + assert.equal(await accessGeneration(), generation) + // Status boundary only: the endpoint ABA already invalidated this archive. + // A separate mounted journey proves revocation of a still-valid file. + await setConnectionStatus(issued.connection.id, "revoked", owner); terminalRevoked = true + payloadReads = 0 + pool.query = ((...args: unknown[]) => { const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text; if (/SELECT ciphertext|pg_dump/i.test(sql ?? "")) payloadReads++; return (originalQuery as (...args: unknown[]) => unknown)(...args) }) as typeof pool.query + try { + const revoked = await app.inject({ url: securityReceipt.downloadPath, headers }) + assert.equal(revoked.statusCode, 403) + assert.deepEqual(revoked.json(), { error: "Доступ к файлу отозван", code: "PERMISSION_DENIED" }) + assert.equal((await app.inject({ url: "/api/mcp/connections", headers })).statusCode, 200, "The same original owner JWT remains authenticated after delegated revocation") + assert.equal((await app.inject({ url: securityReceipt.downloadPath })).statusCode, 401, "Missing owner JWT still requires login") + assert.equal(payloadReads, 0, "Revoked delegation stops before artifact ciphertext queries") + } finally { pool.query = originalQuery as typeof pool.query } + console.log("✓ Database backup: real pg_dump, human approval, exclusive drain/no deadlock, binary SQL/HTTP parity, metadata-only receipt, model credential refusal, integrity/expiry/protection/revocation/ABA and no replay") + } finally { + blocker?.(); pool.query = originalQuery as typeof pool.query + if (protection) await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + if (!terminalRevoked) await setConnectionStatus(issued.connection.id, "paused", owner) + await pool.query("DELETE FROM mcp_artifacts WHERE connection_id=$1", [issued.connection.id]) + } +} diff --git a/backend/src/test/mcp-events-batch.ts b/backend/src/test/mcp-events-batch.ts new file mode 100644 index 00000000..452c4f82 --- /dev/null +++ b/backend/src/test/mcp-events-batch.ts @@ -0,0 +1,152 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { SignJWT } from "jose" +import type { FastifyInstance } from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import { env } from "../config.js" +import type { AuthUser } from "../lib/permissions.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan, loadPlan } from "../modules/mcp/runtime.js" +import { decryptSecret } from "../modules/mcp/security.js" +import { appendClientEventRows, appendEvent, appendEvents, prepareEventRows } from "../modules/events/service/events-service.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "../services/partition-write-transaction.js" +export async function verifyMcpEventsBatch(actor: AuthUser, app: FastifyInstance) { + const owner = { ...actor, id: "events-batch-owner", permissions: [...new Set([...actor.permissions, "mm:dashboard:read"])] } + const ids = (await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id)) + const issue = (serverIds = ids) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Event journal fixture", mode: "autonomous", permissions: ["mm:dashboard:read"], scope: { serverIds, operationIds: ["events.batch.append", "operations.global.reconcile"], allowGlobal: true }, expiresAt: new Date(Date.now() + 1800000).toISOString(), limits: { callsPerMinute: 240, mutationsPerHour: 100, serversPerPlan: 1000, objectsPerPlan: 10000 } }), owner) + const issued = await issue(), principal = await authenticateMcpToken(issued.token), allIds: string[] = [], protections: string[] = [] + const originalQuery = pool.query.bind(pool) + let afterIntent: (() => Promise) | undefined + let loseAcknowledgement = false + pool.query = (async (...args: unknown[]) => { + const result = await (originalQuery as (...args: unknown[]) => Promise)(...args) + if (typeof args[0] === "string" && args[0].includes("INSERT INTO mcp_audit") && (args[1] as unknown[] | undefined)?.[1] === "configuration.intent" && afterIntent) { const hook = afterIntent; afterIntent = undefined; await hook() } + if (typeof args[0] === "string" && args[0].includes("INSERT INTO mcp_audit") && (args[1] as unknown[] | undefined)?.[1] === "configuration.acknowledged" && loseAcknowledgement) { loseAcknowledgement = false; throw Error("Owned acknowledgement loss") } + return result + }) as typeof pool.query + const event = { level: "warning" as const, eventType: " fixture." + randomUUID() + " ", sourceModule: "system" as const, title: " review title ", message: " review message ", entityType: " server ", entityId: " 1 ", payload: { zero: 0, enabled: false, token: "NEVER_EVENT_PAYLOAD_TOKEN", nested: { text: "fixture" } }, createdAt: "2026-10-24T03:00:00+03:00" } + const internal = { ...event, eventType: "owned-internal-" + randomUUID() } + const prepare = async () => { + const result = await invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [event, { ...event, level: "info", entityType: undefined, entityId: undefined, payload: undefined, createdAt: undefined }] }) as { planId: string; state: string } + assert.equal(result.state, "awaiting-approval", "Autonomous policy still requires human approval") + const plan = await loadPlan(result.planId), evidence = JSON.parse(await decryptSecret(plan.evidence.sealed)), input = evidence.steps[0].input + const rows = JSON.parse(await decryptSecret(input.batchCiphertext)); allIds.push(...rows.map((row: { id: string }) => row.id)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=ANY($1::text[])", [rows.map((row: { id: string }) => row.id)])).rows[0].n, 0) + await approvePlan(result.planId, owner, true) + return { ...result, input, rows } + } + const execute = async (planId: string) => { + const operation = await applyPlan(principal, planId, randomUUID()) + for (let attempt = 0; attempt < 500; attempt++) { const row = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0]; if (!["pending", "running"].includes(row.state)) return { ...operation, state: row.state }; await delay(20) } + throw Error("Owned event batch operation did not settle") + } + const reconcile = async (operationId: string) => (await invokeMcpTool(principal, "mm_global_operation_reconcile", { operationId }) as { results: Array<{ matchesDesiredState: boolean; configurationMatchesDesired: boolean }> }).results[0] + try { + const [designed] = prepareEventRows([{ ...event, eventType: "owned-admission-fixture" }]); allIds.push(designed.id) + const blocker = await pool.connect() + let write: Promise | undefined + try { + await blocker.query("SELECT pg_advisory_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + let checked = false + write = Promise.allSettled([ + appendClientEventRows([designed], async () => { checked = true }), + appendEvent(internal), + appendEvents([internal, internal]), + ]).then(results => { + for (const result of results) if (result.status === "rejected") throw result.reason + }) + void write.catch(() => {}) + let waiting = false + for (let attempt = 0; attempt < 150 && !waiting; attempt++) { + waiting = (await pool.query(`SELECT 1 FROM pg_locks WHERE locktype='advisory' AND NOT granted + AND mode='ShareLock' AND classid=0 AND objid=$1::oid`, [PARTITION_WRITER_ADVISORY_KEY])).rowCount === 3 + if (!waiting) await delay(20) + } + const prematureCallback = checked + const storedWhileLocked = (await pool.query("SELECT 1 FROM events WHERE id=$1", [designed.id])).rowCount + const internalWhileLocked = (await pool.query("SELECT 1 FROM events WHERE event_type=$1", [internal.eventType])).rowCount + await blocker.query("SELECT pg_advisory_unlock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + await write + assert(waiting, "Client, internal single and internal batch writers must wait for common SQL admission") + assert.equal(prematureCallback, false) + assert.equal(storedWhileLocked, 0) + assert.equal(internalWhileLocked, 0) + assert.equal(checked, true) + assert.equal((await pool.query("SELECT 1 FROM events WHERE id=$1", [designed.id])).rowCount, 1) + assert.equal((await pool.query("SELECT 1 FROM events WHERE event_type=$1", [internal.eventType])).rowCount, 3) + const [refused] = prepareEventRows([event]); allIds.push(refused.id) + const refusal = Error("Owned post-admission callback refusal") + await assert.rejects(appendClientEventRows([refused], async () => { throw refusal }), error => error === refusal) + assert.equal((await pool.query("SELECT 1 FROM events WHERE id=$1", [refused.id])).rowCount, 0) + } finally { + blocker.release(true) + await write?.catch(() => {}) + } + const jwt = await new SignJWT({ sub: owner.id, apps: ["mm"], permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + const rest = await app.inject({ method: "POST", url: "/api/events/batch", headers: { authorization: "Bearer " + jwt }, payload: { events: [event] } }) + assert.equal(rest.statusCode, 201, rest.body); assert.deepEqual(rest.json(), { ok: true, inserted: 1 }) + const baseline = (await pool.query("SELECT * FROM events WHERE event_type=$1", [event.eventType.trim()])).rows[0]; allIds.push(baseline.id) + const prepared = await prepare(), applied = await execute(prepared.planId) + assert.equal(applied.state, "applied") + const saved = (await pool.query("SELECT * FROM events WHERE id=ANY($1::text[])", [prepared.rows.map((row: { id: string }) => row.id)])).rows + const first = saved.find(row => row.id === prepared.rows[0].id) + assert.deepEqual({ ...first, id: baseline.id }, baseline, "Shared REST/MCP preserves all supplied fields and timestamp semantics") + assert.equal((await reconcile(applied.id)).matchesDesiredState, true) + const portal = await app.inject({ method: "POST", url: `/api/mcp/operations/${applied.id}/reconcile`, headers: { authorization: "Bearer " + jwt }, payload: {} }) + assert.equal(portal.statusCode, 200, portal.body); assert.equal(portal.json().results[0].matchesDesiredState, true) + await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1", [applied.id]) + const unverified = await reconcile(applied.id); assert.equal(unverified.configurationMatchesDesired, true); assert.equal(unverified.matchesDesiredState, false) + await pool.query("UPDATE mcp_operation_steps SET state='verified' WHERE operation_id=$1", [applied.id]) + assert.equal((await reconcile(applied.id)).matchesDesiredState, true) + await pool.query("UPDATE events SET title='collateral change' WHERE id=$1", [first.id]); assert.equal((await reconcile(applied.id)).matchesDesiredState, false) + assert.equal((await applyPlan(principal, prepared.planId, randomUUID())).id, applied.id) + const lost = await prepare(); loseAcknowledgement = true + const unknown = await execute(lost.planId); assert.equal(unknown.state, "unknown"); assert.equal(loseAcknowledgement, false) + const lostComparison = await reconcile(unknown.id); assert.equal(lostComparison.configurationMatchesDesired, true); assert.equal(lostComparison.matchesDesiredState, false) + assert.equal((await applyPlan(principal, lost.planId, randomUUID())).id, unknown.id) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=ANY($1::text[])", [lost.rows.map((row: { id: string }) => row.id)])).rows[0].n, 2) + await assert.rejects(invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [{ ...event, id: randomUUID() }] })) + await assert.rejects(invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [{ ...event, message: "я".repeat(40000) }] })) + if (ids.length) { + const narrow = await issue(ids.slice(1)) + try { + const limited = await authenticateMcpToken(narrow.token) + await assert.rejects(invokeMcpTool(limited, "mm_events_batch_append_plan", { events: [event] })) + await assert.rejects(invokeMcpTool(limited, "mm_global_operation_reconcile", { operationId: applied.id })) + } + finally { await setConnectionStatus(narrow.connection.id, "paused", owner) } + } + const foreign = await issue() + try { await assert.rejects(invokeMcpTool(await authenticateMcpToken(foreign.token), "mm_global_operation_reconcile", { operationId: applied.id })) } + finally { await setConnectionStatus(foreign.connection.id, "paused", owner) } + const savedPolicy = (await pool.query("SELECT policy FROM mcp_connections WHERE id=$1", [issued.connection.id])).rows[0].policy + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{limits,objectsPerPlan}',$2::jsonb) WHERE id=$1", [issued.connection.id, JSON.stringify(ids.length + 2)]) + try { await assert.rejects(invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [event, event] }), (error: { code?: string }) => error.code === "PLAN_LIMIT_EXCEEDED") } + finally { await pool.query("UPDATE mcp_connections SET policy=$2::jsonb WHERE id=$1", [issued.connection.id, JSON.stringify(savedPolicy)]) } + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,operationIds}',$2::jsonb) WHERE id=$1", [issued.connection.id, JSON.stringify(savedPolicy.scope.operationIds.filter((id: string) => id !== "events.batch.append"))]) + try { await assert.rejects(invokeMcpTool(principal, "mm_global_operation_reconcile", { operationId: applied.id }), (error: { code?: string }) => error.code === "PERMISSION_DENIED") } + finally { await pool.query("UPDATE mcp_connections SET policy=$2::jsonb WHERE id=$1", [issued.connection.id, JSON.stringify(savedPolicy)]) } + for (const scenario of ["protection", "event-protection", "pause"] as const) { + const staged = await prepare() + afterIntent = async () => { + if (scenario === "pause") await setConnectionStatus(issued.connection.id, "paused", owner) + else { const id = randomUUID(); protections.push(id); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [id, JSON.stringify(scenario === "event-protection" ? { kind: "event", id: staged.rows[0].id, protection: "deny" } : { kind: "events-journal", id: "global", protection: "deny" }), owner.id]) } + } + assert.notEqual((await execute(staged.planId)).state, "applied") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=ANY($1::text[])", [staged.rows.map((row: { id: string }) => row.id)])).rows[0].n, 0) + assert.equal(afterIntent, undefined) + await pool.query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [protections]) + await setConnectionStatus(issued.connection.id, "active", owner) + } + const checkpoints = (await pool.query("SELECT intended_state,result FROM mcp_operation_steps WHERE operation_id IN(SELECT id FROM mcp_operations WHERE connection_id=$1)", [issued.connection.id])).rows + assert.ok(!JSON.stringify(checkpoints).includes("NEVER_EVENT_PAYLOAD_TOKEN")); assert.ok(!JSON.stringify(checkpoints).includes("review message")) + console.log("✓ Event batch SQL+REST: full fields/trim/time/payload parity, atomic stable UUID batch, human approval/full global scope, per-event object budget, JSON byte/depth bounds, exact VERIFIED read-only global/portal reconcile, cross-connection/narrow/original-action denial, lost acknowledgement unknown, tamper/no replay and after-intent pause/protection fences; journal entries only, no notifications") + } finally { + pool.query = originalQuery; await setConnectionStatus(issued.connection.id, "paused", owner) + await pool.query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [protections]) + await pool.query("DELETE FROM events WHERE id=ANY($1::text[])", [allIds]) + await pool.query("DELETE FROM events WHERE event_type=$1", [internal.eventType]) + } +} diff --git a/backend/src/test/mcp-filter-shared.ts b/backend/src/test/mcp-filter-shared.ts new file mode 100644 index 00000000..0b5b9d23 --- /dev/null +++ b/backend/src/test/mcp-filter-shared.ts @@ -0,0 +1,65 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import Fastify from "fastify" +import { validatorCompiler, serializerCompiler } from "@fastify/type-provider-zod" +import { pool } from "../db/index.js" +import filtersRoutes from "../routes/filters.js" + +/** Actual REST handler + shared service + native HTTP simulator on own SQL rows. + * Authentication is tested by the app's separate auth suite, not this handler driver. */ +export async function verifySharedFilterWrites() { + let rows: Record[] = [{ ".id": "*foreign", chain: "BGP-IN", rule: "accept;", comment: "NEVER_FOREIGN_FILTER" }] + let writes = 0, reads = 0, ignored = false, lost = false + const mock = createServer(async (req, res) => { + const path = req.url?.replace(/^\/rest/, "") + res.setHeader("content-type", "application/json") + if (req.method === "GET") { reads++; res.end(JSON.stringify(path === "/routing/filter/rule" ? rows : path === "/interface/gre" ? [{ ".id": "*gre", name: 'GRE test', disabled: "false", running: "true" }] : [])); return } + let raw = ""; for await (const chunk of req) raw += chunk + const body = raw ? JSON.parse(raw) as Record : {} + if (path === "/routing/filter/rule/get") { res.end(JSON.stringify({ ret: "false" })); return } + writes++ + if (!ignored) { + if (path === "/routing/filter/rule/add") rows.push({ ...body, ".id": "*own", disabled: "false" }) + else if (req.method === "PATCH") Object.assign(rows.find(row => row[".id"] === "*own")!, body, { disabled: "false" }) + else if (req.method === "DELETE") rows = rows.filter(row => row[".id"] !== "*own") + } + if (lost) { req.socket.destroy(); return } + res.end(JSON.stringify(path === "/routing/filter/rule/add" ? { ret: "*own" } : {})) + }) + await new Promise(resolve => mock.listen(0, "127.0.0.1", resolve)) + const address = mock.address(); assert(address && typeof address !== "string") + const app = Fastify(); app.setValidatorCompiler(validatorCompiler); app.setSerializerCompiler(serializerCompiler) + await app.register(filtersRoutes, { prefix: "/api" }) + const rules = [{ id: "route", community: "64512:11", communityName: "Fixture name", action: "route", gateway: "192.0.2.2", gatewayTunnelId: 'GRE test', description: "first }\rsecond" }, + { id: "drop", community: "64512:12", action: "blackhole", gateway: "", gatewayTunnelId: "", description: "" }] + const put = (value: unknown = rules) => app.inject({ method: "PUT", url: "/api/filters/rules", payload: { serverId: 74, rules: value } }) + const metadata = async () => (await pool.query("SELECT community,community_name,action,gateway,gateway_tunnel_id,description FROM filter_rules WHERE server_id=74 ORDER BY sort_order")).rows + const revisions = async () => (await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE server_id=74 AND section='filters'")).rows[0].n + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES(74,'filter-shared-fixture','127.0.0.1',$1,false,'fixture','NEVER_FILTER_LOGIN')", [address.port]) + const beforeInvalid = reads; assert.equal((await put([{ ...rules[0], gatewayTunnelId: 'GRE } "$name' }])).statusCode, 400); assert.equal(reads, beforeInvalid); assert.equal((await put([{ ...rules[0], gateway: "192.0.2.2; reject;" }])).statusCode, 400); assert.equal(reads, beforeInvalid); assert.equal(writes, 0) + assert.equal((await put()).statusCode, 200); assert.equal(writes, 1) + assert.ok(rows[1].rule.includes("set blackhole yes;")); assert.ok(rows[1].rule.includes('set gw-interface "GRE test";')) + const applied = await metadata(), revisionCount = await revisions() + assert.equal(applied.length, 2); assert.equal(applied[0].description, "first }\nsecond"); assert.equal(applied[0].community_name, "Fixture name") + const observed = await app.inject({ method: "GET", url: "/api/filters/rules?serverId=74" }); assert.equal(observed.statusCode, 200) + const live = observed.json().rulesets[0].rules as typeof rules + assert.equal(live[0].gatewayTunnelId, rules[0].gatewayTunnelId); assert.equal(live[0].description, "first }\nsecond"); assert.equal(live[0].communityName, "Fixture name"); assert.equal(live[1].description, "") + assert.deepEqual(await metadata(), applied); assert.equal(await revisions(), revisionCount) + ignored = true; const failed = await put([{ ...rules[0], gateway: "192.0.2.3" }]); ignored = false + assert.equal(failed.statusCode, 409); assert.deepEqual(await metadata(), applied); assert.equal(await revisions(), revisionCount); assert.ok(!failed.body.includes("NEVER_")) + const protection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'fixture')", [protection, JSON.stringify({ kind: "server", id: "74", protection: "deny" })]) + const beforeProtection = writes; assert.equal((await put()).statusCode, 409); assert.equal(writes, beforeProtection) + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + assert.equal((await put([])).statusCode, 200); assert.equal((await metadata()).length, 0); assert.equal(rows.length, 1) + const beforeLost = writes, beforeLostRevision = await revisions(); lost = true; const unknown = await put(); lost = false + assert.equal(unknown.statusCode, 503); assert.equal(writes, beforeLost + 1); assert.equal((await metadata()).length, 0); assert.equal(await revisions(), beforeLostRevision); assert.ok(!unknown.body.includes("NEVER_")) + await pool.query("UPDATE servers SET enabled=false WHERE id=74"); const beforeDisabled = reads + assert.equal((await put()).statusCode, 409); assert.equal(reads, beforeDisabled) + console.log("✓ Shared filters REST/SQL: modern compiler, quoted/braced names, multiline/empty descriptions and names round-trip, read-back before metadata, foreign-chain/protection guards, ignored/lost writes and atomic metadata") + } finally { + await app.close(); mock.closeAllConnections(); await new Promise(resolve => mock.close(() => resolve())) + await pool.query("DELETE FROM filter_rules WHERE server_id=74"); await pool.query("DELETE FROM config_revisions WHERE server_id=74"); await pool.query("DELETE FROM servers WHERE id=74") + } +} diff --git a/backend/src/test/mcp-filter-write.ts b/backend/src/test/mcp-filter-write.ts new file mode 100644 index 00000000..e44fe810 --- /dev/null +++ b/backend/src/test/mcp-filter-write.ts @@ -0,0 +1,130 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken } from "../modules/mcp/store.js" +import { approvePlan, applyPlan, invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpFilterWrites(actor: AuthUser) { + let reads = 0, writes = 0, ignored = false, lost = false, corrupt = false, filterReads = 0, driftAt = 0 + const foreign = { ".id": "*other", chain: "other", rule: "NEVER_FILTER_OTHER_SCRIPT", comment: "NEVER_FILTER_OTHER_NOTE" } + let filters: Record[] = [{ ...foreign }] + const collections: Record[]> = { + "/interface/gre": [{ ".id": "*gre", name: "fixture-gre", disabled: "false", "ipsec-secret": "NEVER_FILTER_GRE_SECRET" }], + "/ip/address": [{ ".id": "*addr", address: "192.0.2.1/30", interface: "fixture-gre", disabled: "false" }], "/ipv6/address": [], + "/ip/route?static=true": [], "/ipv6/route?static=true": [], + "/routing/bgp/connection": [{ ".id": "*peer", name: "fixture-peer", "input.filter": "bgp-in", "tcp-md5-key": "NEVER_FILTER_BGP_KEY" }], + "/routing/bgp/template": [{ ".id": "*tpl", name: "default" }], "/routing/table": [{ ".id": "*main", name: "main", fib: "true" }], "/ip/vrf": [{ ".id": "*vrf", name: "main", interfaces: "all" }], + } + const mock = createServer(async (req, res) => { + const path = (req.url ?? "").replace(/^\/rest/, ""); res.setHeader("content-type", "application/json") + if (req.method === "GET") { + reads++ + if (path === "/routing/filter/rule") { filterReads++; if (filterReads === driftAt) filters[0].comment += " changed" } + res.end(JSON.stringify(path === "/routing/filter/rule" ? filters : collections[path] ?? [])); return + } + let raw = ""; for await (const part of req) raw += part + const body = raw ? JSON.parse(raw) as Record : {} + if (path.endsWith("/get")) { res.end(JSON.stringify({ ret: "false" })); return } + writes++ + if (!ignored) { + if (typeof body.rule === "string") body.rule = body.rule.replace('gw-interface "fixture-gre"', 'gw-interface fixture-gre') + if (path.endsWith("/add")) filters.push({ ...body, ".id": "*own", disabled: "false" }) + else if (req.method === "PATCH") Object.assign(filters.find(row => row[".id"] === "*own")!, body, { disabled: "false" }) + else if (req.method === "DELETE") filters = filters.filter(row => row[".id"] !== "*own") + } + if (corrupt) filters[0].rule += " changed" + if (lost) { req.socket.destroy(); return } + res.end(JSON.stringify(path.endsWith("/add") ? { ret: "*own" } : {})) + }) + await new Promise(resolve => mock.listen(0, "127.0.0.1", resolve)); const address = mock.address(); assert(address && typeof address !== "string") + const permissions = ["mm:filters:write", "mm:network:read", "mm:bgp:read"], operations = ["filters.rules.replace", "filters.revision.restore"] + const create = (granted = permissions, observer = false) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Filter write fixture", mode: observer ? "observer" : "approval", permissions: granted, expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: [75], operationIds: operations }, limits: { callsPerMinute: 120, mutationsPerHour: 200 } }), actor) + const rules = [{ community: "64512:1", communityName: "fixture", action: "route", gateway: "192.0.2.2", gatewayTunnelId: "fixture-gre", description: "line }\nsecond" }, { community: "64512:2", action: "blackhole", gateway: "", gatewayTunnelId: "", description: "" }] + const connectionIds: string[] = [] + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES(75,'filter-write-fixture','127.0.0.1',$1,false,'fixture','NEVER_FILTER_PASSWORD')", [address.port]) + await pool.query("INSERT INTO filter_rules(server_id,sort_order,community,action,gateway,gateway_tunnel_id,description) VALUES(75,0,'64512:9','route','192.0.2.2','','NEVER_FILTER_CACHED_DESCRIPTION')") + const issued = await create(), principal = await authenticateMcpToken(issued.token); connectionIds.push(issued.connection.id) + const prepare = (desired: unknown = rules) => invokeMcpTool(principal, "mm_filter_rules_replace_plan", { serverId: 75, rules: desired }) as Promise<{ planId: string; state: string; changes: { mutationCost: number } }> + const wait = async (id: string) => { + for (let attempt = 0; attempt < 400; attempt++) { const row = (await pool.query("SELECT id,state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (row && !["pending", "running"].includes(row.state)) return row; await delay(25) } + assert.fail("Filter write operation did not finish") + } + const apply = async (id: string) => { await approvePlan(id, actor, true); return wait((await applyPlan(principal, id, randomUUID())).id) } + for (const missing of permissions) { + const denied = await create(permissions.filter(value => value !== missing)); connectionIds.push(denied.connection.id); const before = reads + await assert.rejects(invokeMcpTool(await authenticateMcpToken(denied.token), "mm_filter_rules_replace_plan", { serverId: 75, rules })); assert.equal(reads, before) + } + const observer = await create(permissions, true); connectionIds.push(observer.connection.id) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(observer.token), "mm_filter_rules_replace_plan", { serverId: 75, rules })) + const beforeBadInput = reads + await assert.rejects(prepare([{ ...rules[0], gatewayTunnelId: 'GRE } "$name' }])) + await assert.rejects(prepare([{ ...rules[0], rule: "reject;" }])); await assert.rejects(prepare([{ ...rules[0], community: "65536:1" }])); assert.equal(reads, beforeBadInput) + await assert.rejects(invokeMcpTool(principal, "mm_filter_rules_replace_plan", { serverId: 1, rules })) + await assert.rejects(invokeMcpTool(principal, "mm_filter_rules_replace_plan", { serverId: 75, rules, requireUnbound: true })) + const autonomous = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Explicit autonomous filter fixture", mode: "autonomous", dangerousPolicy: "explicit-allow", autonomousDangerousOperationIds: operations, permissions, expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: [75], operationIds: operations } }), actor) + connectionIds.push(autonomous.connection.id) + const automaticPlan = await invokeMcpTool(await authenticateMcpToken(autonomous.token), "mm_filter_rules_replace_plan", { serverId: 75, rules }) as { state: string } + assert.equal(automaticPlan.state, "awaiting-approval"); assert.equal(writes, 0) + let plan = await prepare(); assert.equal(plan.state, "awaiting-approval"); assert.equal(plan.changes.mutationCost, 2); assert.equal(JSON.stringify(plan).includes("NEVER_FILTER_"), false) + await assert.rejects(approvePlan(plan.planId, { ...actor, permissions: ["mm:mcp:approve", "mm:filters:write", "mm:network:read"] }, true)) + await assert.rejects(applyPlan(principal, plan.planId, randomUUID())); assert.equal(writes, 0) + const first = await apply(plan.planId); assert.equal(first.state, "applied"); assert.equal(writes, 1) + const originalRevision = (await pool.query("SELECT id FROM config_revisions WHERE server_id=75 AND section='filters' ORDER BY created_at DESC LIMIT 1")).rows[0].id + plan = await prepare(); assert.equal(plan.changes.mutationCost, 1); const beforeNoop = writes; assert.equal((await apply(plan.planId)).state, "applied"); assert.equal(writes, beforeNoop) + const annotations = [{ ...rules[0], communityName: "metadata-only", description: "metadata note" }, rules[1]] + plan = await prepare(annotations); assert.equal(plan.changes.mutationCost, 1) + await approvePlan(plan.planId, actor, true) + const lateProtection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [lateProtection, JSON.stringify({ kind: "late-unrelated-domain", id: "new", serverId: 75, protection: "deny" }), actor.id]) + assert.notEqual((await wait((await applyPlan(principal, plan.planId, randomUUID())).id)).state, "applied"); assert.equal(writes, beforeNoop) + assert.equal((await pool.query("SELECT community_name FROM filter_rules WHERE server_id=75 AND community='64512:1'")).rows[0].community_name, "fixture") + await pool.query("DELETE FROM mcp_protections WHERE id=$1", [lateProtection]) + assert.equal((await apply((await prepare(annotations)).planId)).state, "applied"); assert.equal(writes, beforeNoop) + assert.equal((await pool.query("SELECT community_name FROM filter_rules WHERE server_id=75 AND community='64512:1'")).rows[0].community_name, "metadata-only") + const changed = [{ ...rules[0], gateway: "192.0.2.3" }] + plan = await prepare(changed); collections["/interface/gre"][0]["ipsec-secret"] += " changed" + assert.equal((await apply(plan.planId)).state, "rejected"); assert.equal(writes, beforeNoop); collections["/interface/gre"][0]["ipsec-secret"] = "NEVER_FILTER_GRE_SECRET" + plan = await prepare(changed); await pool.query("UPDATE servers SET password='NEVER_FILTER_ROTATED' WHERE id=75"); const beforeCredentials = reads + assert.equal((await apply(plan.planId)).state, "rejected"); assert.equal(reads, beforeCredentials); await pool.query("UPDATE servers SET password='NEVER_FILTER_PASSWORD' WHERE id=75") + plan = await prepare(changed); collections["/routing/rip/instance"] = [{ ".id": "*rip", name: "late-context", "in-filter-chain": "bgp-in" }] + assert.equal((await apply(plan.planId)).state, "rejected"); assert.equal(writes, beforeNoop); collections["/routing/rip/instance"] = [] + plan = await prepare(changed); driftAt = filterReads + 4 + const late = await apply(plan.planId); assert.notEqual(late.state, "applied"); assert.equal(writes, beforeNoop); driftAt = 0; filters[0] = { ...foreign } + const protection = randomUUID(); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "bgp_connection", id: "*peer", serverId: 75, protection: "deny" }), actor.id]) + await assert.rejects(prepare(changed)); assert.equal(writes, beforeNoop); await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection]) + ignored = true; assert.equal((await apply((await prepare(changed)).planId)).state, "partial"); ignored = false + assert.equal((await pool.query("SELECT count(*)::int AS n FROM filter_rules WHERE server_id=75")).rows[0].n, 2) + corrupt = true; assert.equal((await apply((await prepare(changed)).planId)).state, "partial"); corrupt = false; filters[0] = { ...foreign } + // Reconcile the observed native state with a fresh approved plan; no replay of a failed plan. + assert.equal((await apply((await prepare()).planId)).state, "applied") + lost = true; plan = await prepare(changed); const beforeLost = writes; const unknown = await apply(plan.planId); lost = false + assert.equal(unknown.state, "unknown"); assert.equal(writes, beforeLost + 1) + assert.equal((await applyPlan(principal, plan.planId, randomUUID())).id, unknown.id); assert.equal(writes, beforeLost + 1) + plan = await prepare(changed); const beforeReconcile = writes; assert.equal((await apply(plan.planId)).state, "applied"); assert.equal(writes, beforeReconcile) + assert.equal((await apply((await prepare([])).planId)).state, "applied"); assert.equal(filters.length, 1) + const restoration = await invokeMcpTool(principal, "mm_filter_revision_restore_plan", { serverId: 75, revisionId: originalRevision }) as { planId: string } + assert.equal((await apply(restoration.planId)).state, "applied"); assert.equal((await pool.query("SELECT count(*)::int AS n FROM filter_rules WHERE server_id=75")).rows[0].n, 2) + const staleRevision = await invokeMcpTool(principal, "mm_filter_revision_restore_plan", { serverId: 75, revisionId: originalRevision }) as { planId: string } + await pool.query("UPDATE config_revisions SET fingerprint='tampered' WHERE id=$1", [originalRevision]); const beforeRevision = writes + assert.equal((await apply(staleRevision.planId)).state, "rejected"); assert.equal(writes, beforeRevision) + const vrf = { ".id": "*ownedvrf", name: "fixture-vrf", interfaces: "fixture-gre" } + collections["/ip/vrf"].unshift(vrf) + const beforeVrfNoop = writes + assert.equal((await apply((await prepare()).planId)).state, "applied"); assert.equal(writes, beforeVrfNoop) + const staleVrf = await prepare() + collections["/ip/vrf"].reverse() // main's all now wins; identical entries in a different effective order. + assert.equal((await apply(staleVrf.planId)).state, "rejected"); assert.equal(writes, beforeVrfNoop) + collections["/ip/vrf"] = [{ ".id": "*vrf", name: "main", interfaces: "all" }] + const requests = (await pool.query("SELECT input,result FROM mcp_requests WHERE connection_id=ANY($1::uuid[])", [connectionIds])).rows + const audit = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=ANY($1::uuid[])", [connectionIds])).rows + assert.equal(JSON.stringify({ requests, audit }).includes("NEVER_FILTER_"), false) + console.log("✓ Managed filter MCP: conjunctive scope/permissions, human plans, private proofs, source/native/credential/revision drift, late callback fencing, peer protections, no-op, partial/unknown/no replay, full replace/clear and revision restore") + } finally { + mock.closeAllConnections(); await new Promise(resolve => mock.close(() => resolve())) + await pool.query("DELETE FROM filter_rules WHERE server_id=75"); await pool.query("DELETE FROM config_revisions WHERE server_id=75"); await pool.query("DELETE FROM servers WHERE id=75") + } +} diff --git a/backend/src/test/mcp-fixture-expiry.ts b/backend/src/test/mcp-fixture-expiry.ts new file mode 100644 index 00000000..7a13b17c --- /dev/null +++ b/backend/src/test/mcp-fixture-expiry.ts @@ -0,0 +1,10 @@ +import assert from "node:assert/strict" + +/** Test-only remaining lifetime; never renew an original actor/credential grant. */ +export function fixtureGrantExpiry(now: number, ...originalExpiries: number[]): string { + assert(Number.isSafeInteger(now) && Number.isFinite(new Date(now).getTime()), "Invalid fixture clock") + assert(originalExpiries.length > 0 && originalExpiries.every(value => Number.isSafeInteger(value) && Number.isFinite(new Date(value).getTime())), "Missing or invalid original fixture expiry") + const expiry = Math.min(now + 1800000, ...originalExpiries) - 1000 + assert(expiry > now + 60000, "Insufficient original fixture lifetime; do not extend authority") + return new Date(expiry).toISOString() +} diff --git a/backend/src/test/mcp-flow-analytics.ts b/backend/src/test/mcp-flow-analytics.ts new file mode 100644 index 00000000..d441ed36 --- /dev/null +++ b/backend/src/test/mcp-flow-analytics.ts @@ -0,0 +1,75 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { FlowAnalyticsDto } from "@mmapp/contracts/traffic-flow" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +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 { 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" +import { duringFlowSourceQuery } from "./mcp-flow-query-race.js" + +export async function verifyMcpFlowAnalytics(actor: AuthUser) { + const owner = { ...actor, id: "flow-analytics-owner", permissions: [...actor.permissions, "mm:traffic:read"] } + const users = ["analytics-local", "analytics-mixed", "analytics-peer"] + await pool.query("INSERT INTO servers(id,name,host,password) VALUES(110,'analytics-visible','192.0.2.110','NEVER_ANALYTICS_PASSWORD'),(111,'NEVER_ANALYTICS_HIDDEN_SERVER','192.0.2.111','NEVER_ANALYTICS_PASSWORD')") + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped analytics", mode: "observer", expiresAt: new Date(Date.now()+1800000).toISOString(), + permissions: ["mm:traffic:read"], scope: { serverIds: [110], operationIds: ["traffic.flow.analytics.read"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), owner) + const principal = await authenticateMcpToken(access.token) + const read = (extra: Record = {}) => invokeMcpTool(principal, "mm_netflow_analytics_report_get", { serverId: 110, minutes: 5, ...extra }) as Promise<{ analytics: FlowAnalyticsDto; pages: Record; live: boolean; forwardingVerified: boolean }> + const bucket = new Date(Date.now()-60000).toISOString() + const put = (sid: number, name: string, bytes: string | number, src: string, at = bucket) => pool.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets,in_iface) VALUES($1,$2,$3,'8.8.8.8',6,12345,443,$4,1,$5)", [sid,at,src,bytes,name]) + try { + for (const at of [new Date(bucket), new Date(Date.now()+3600000)]) await ensurePartitionFor(pool,"flow_buckets","day",at) + disableRipeEnqueueForTests() + for (const id of users) await pool.query("INSERT INTO app_users(id,login,name) VALUES($1,$1,$2)", [id,id === "analytics-mixed" ? "NEVER_ANALYTICS_MIXED_USER" : id]) + for (const [userId,serverId,interfaceName,peer] of [[users[0],110,"analytics-in",""],[users[1],110,"analytics-mixed-in",""],[users[1],111,"NEVER_ANALYTICS_HIDDEN_IFACE",""],[users[2],110,"analytics-peer-in","NEVER_ANALYTICS_PEER_KEY"]] as const) + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,$2,$3,$4,$5)",[randomUUID(),userId,serverId,interfaceName,peer]) + clearPendingFlows(); invalidateFlowCatalogCache(); invalidateFlowAnalyticsCache() + await put(110,"analytics-in",100,"198.18.241.1"); await put(110,"analytics-mixed-in",200,"198.18.241.2") + await put(111,"NEVER_ANALYTICS_HIDDEN_IFACE","9007199254740993","198.18.241.3") + await put(110,"NEVER_ANALYTICS_FUTURE_IFACE",999,"198.18.241.4",new Date(Date.now()+3600000).toISOString()) + const topology = await loadFlowTopology([110]) + assert.deepEqual([...topology.clientByIface.values()].map(row=>row.userId),[users[0]]) + let result = await read() + assert.equal(result.analytics.bytes,300); assert.equal(result.analytics.packets,2); assert.equal(result.live,false); assert.equal(result.forwardingVerified,false) + assert.ok(!JSON.stringify(result).includes("NEVER_")); assert.equal(result.pages.conversationsList.total,2) + const restEngine = await buildFlowAnalytics({serverId:110,minutes:5}) + assert.equal(result.analytics.bytes,restEngine.bytes); assert.deepEqual(result.analytics.protocols,restEngine.protocols) + // An unrestricted REST cache contains mixed-user metadata; scoped reads must bypass it. + assert.ok(JSON.stringify(restEngine).includes("NEVER_ANALYTICS_MIXED_USER")) + assert.ok(!JSON.stringify(await read()).includes("NEVER_")) + result = await read({limit:1}); assert.equal(result.analytics.conversationsList.length,1); assert.equal(result.pages.conversationsList.nextOffset,1) + assert.equal((await read({offset:1,limit:1})).analytics.conversationsList.length,1) + assert.equal((await read({interface:"analytics-in"})).analytics.bytes,100) + assert.equal((await read({userId:users[0]})).analytics.bytes,100) + await put(110,"analytics-duplicate-in",50,"198.18.241.1") + assert.equal((await read()).analytics.bytes,300) + assert.equal((await read({dedup:false,excludeMesh:false,excludeOverlay:false})).analytics.bytes,350) + 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()=>{}}),/предел/) + 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) => + duringFlowSourceQuery(pool, match, action, () => assert.rejects(read())) + await duringQuery("FROM flow_buckets",async()=>{await pool.query("UPDATE servers SET host='192.0.2.210' WHERE id=110");await pool.query("UPDATE servers SET host='192.0.2.110' WHERE id=110")}) + await duringQuery("FROM server_snapshots",async()=>{await pool.query("UPDATE user_interface_bindings SET interface_name='changed-during-read' WHERE user_id=$1",[users[0]])}) + await pool.query("UPDATE user_interface_bindings SET interface_name='analytics-in' WHERE user_id=$1",[users[0]]) + await duringQuery("FROM flow_buckets",()=>setConnectionStatus(access.connection.id,"paused",owner)) + await setConnectionStatus(access.connection.id,"active",owner) + assert.equal((await read()).analytics.bytes,300) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1",[access.connection.id])).rows[0].n,0) + console.log("✓ Scoped NetFlow analytics: REST engine parity, hidden server/user/peer exclusion, cache isolation, bounds/paging/future rows, source ABA/topology drift, cancel/pause and no plans") + } finally { + await setConnectionStatus(access.connection.id,"paused",owner) + await pool.query("DELETE FROM user_interface_bindings WHERE user_id=ANY($1::text[])",[users]); await pool.query("DELETE FROM app_users WHERE id=ANY($1::text[])",[users]) + await pool.query("DELETE FROM flow_buckets WHERE server_id IN (110,111)"); await pool.query("DELETE FROM servers WHERE id IN (110,111)") + clearPendingFlows(); invalidateFlowCatalogCache(); invalidateFlowAnalyticsCache(); resetRipeCacheForTests() + } +} + diff --git a/backend/src/test/mcp-flow-live.ts b/backend/src/test/mcp-flow-live.ts new file mode 100644 index 00000000..8f024818 --- /dev/null +++ b/backend/src/test/mcp-flow-live.ts @@ -0,0 +1,112 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { FlowAnalyticsDto } from "@mmapp/contracts/traffic-flow" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { ensurePartitionFor } from "../db/partitions.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" +import { invalidateFlowAnalyticsCache } from "../services/traffic-flow-analytics.js" +import trafficFlowRoutes, { flowSseDiagnostics } from "../routes/traffic-flow.js" +import { invalidateFlowCatalogCache } from "../services/traffic-flow-topology.js" +import { resetFlowRingsForTests } from "../services/traffic-flow-ingest.js" +import { tryAcquireFlowLiveSlot, releaseFlowLiveSlot } from "../services/traffic-flow-live-slots.js" +import { disableRipeEnqueueForTests, resetRipeCacheForTests } from "../services/traffic-flow-ripe.js" +import { duringFlowSourceQuery } from "./mcp-flow-query-race.js" + +type Observation = { events: Array<{ event: string; data: FlowAnalyticsDto; pages: Record }>; observationComplete: boolean; forwardingVerified: boolean } +export async function verifyMcpFlowLive(actor: AuthUser) { + const owner = { ...actor, id: "flow-live-owner", permissions: [...actor.permissions, "mm:traffic:read"] } + const user = "flow-live-mixed", local = "flow-live-local", bucket = new Date(Date.now()-60000) + await pool.query("INSERT INTO servers(id,name,host,password) VALUES(114,'live-visible','192.0.2.114','NEVER_LIVE_PASSWORD'),(115,'NEVER_LIVE_SERVER','192.0.2.115','NEVER_LIVE_PASSWORD')") + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped live", mode: "observer", expiresAt: new Date(Date.now()+1800000).toISOString(), permissions: ["mm:traffic:read"], scope: { serverIds: [114], operationIds: ["traffic.flow.live.read"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), owner) + const principal = await authenticateMcpToken(access.token) + const http = Fastify(); http.setValidatorCompiler(validatorCompiler); http.setSerializerCompiler(serializerCompiler) + await http.register(trafficFlowRoutes,{prefix:"/api"}) + const address = await http.listen({host:"127.0.0.1",port:0}) + const read = (input: Record = {}, signal?: AbortSignal) => invokeMcpTool(principal, "mm_netflow_live_observe", { serverId: 114, ...input }, signal) as Promise + const put = (sid: number, iface: string, bytes: number, src: string) => pool.query("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,proto,src_port,dst_port,bytes,packets,in_iface) VALUES($1,$2,$3,'8.8.8.8',6,12345,443,$4,1,$5)",[sid,bucket,src,bytes,iface]) + try { + await ensurePartitionFor(pool,"flow_buckets","day",bucket) + for (const id of [user,local]) await pool.query("INSERT INTO app_users(id,login,name) VALUES($1,$1,$2)",[id,id === user ? "NEVER_LIVE_MIXED_USER" : id]) + for (const [uid,sid,iface] of [[user,114,"live-mixed"],[user,115,"NEVER_LIVE_INTERFACE"],[local,114,"live-in"]] as const) + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name) VALUES($1,$2,$3,$4)",[randomUUID(),uid,sid,iface]) + resetFlowRingsForTests(); invalidateFlowCatalogCache(); invalidateFlowAnalyticsCache() + disableRipeEnqueueForTests() + await put(114,"live-in",100,"198.18.245.1"); await put(114,"live-mixed",200,"198.18.245.2"); await put(115,"NEVER_LIVE_INTERFACE",999,"198.18.245.3") + // Read the real SSE endpoint, including its event envelope and live engine defaults. + const httpAbort = new AbortController(), timeout = setTimeout(()=>httpAbort.abort(),10000) + let rest: { event: string; data: FlowAnalyticsDto } + const stages: unknown[] = [] + const captureStage = (event: unknown) => stages.push(event) + flowSseDiagnostics.subscribe(captureStage) + try { + const response = await fetch(`${address}/api/traffic/flow/live?serverId=114`,{signal:httpAbort.signal}) + assert.equal(response.status,200) + const diagnosticId = response.headers.get("x-mm-netflow-stream") + assert.match(diagnosticId ?? "", /^[a-f0-9-]{36}$/) + const reader = response.body!.getReader(), decoder = new TextDecoder(); let body = "" + for (;;) { + const chunk = await reader.read(); assert.equal(chunk.done,false) + body += decoder.decode(chunk.value,{stream:true}) + const match = /event: (sample|error)\ndata: ([^\n]+)\n\n/.exec(body) + if (match) { rest={event:match[1],data:JSON.parse(match[2])}; break } + } + const events = stages as {streamId: string; stage: string; atUnixMs: number; monotonicMs: number}[] + assert.deepEqual(events.map(event => event.stage), ["open", "build-start", "build-ready", "write-start", "write-complete"]) + for (const event of events) { + assert.deepEqual(Object.keys(event).sort(), ["atUnixMs", "monotonicMs", "stage", "streamId"]) + assert.equal(event.streamId, diagnosticId) + assert.ok(Number.isSafeInteger(event.atUnixMs) && Number.isFinite(event.monotonicMs)) + } + } finally { + flowSseDiagnostics.unsubscribe(captureStage) + clearTimeout(timeout) + httpAbort.abort() + } + assert.equal(rest!.event,"sample"); assert.ok(JSON.stringify(rest!).includes("NEVER_LIVE_MIXED_USER")) + let result = await read() + assert.equal(result.events[0].data.bytes,300); assert.equal(result.events[0].event,"sample") + assert.equal(result.observationComplete,true); assert.equal(result.forwardingVerified,false) + assert.ok(!JSON.stringify(result).includes("NEVER_")) + assert.deepEqual(result.events[0].data.protocols,rest!.data.protocols) + result = await read({limit:1}); assert.equal(result.events[0].data.conversationsList.length,1); assert.equal(result.events[0].pages.conversationsList.nextOffset,1) + assert.equal((await read({offset:1,limit:1})).events[0].data.conversationsList.length,1) + assert.equal((await read({userId:local})).events[0].data.bytes,100) + assert.equal((await read({interface:"live-in"})).events[0].data.bytes,100) + await assert.rejects(read({userId:user})); await assert.rejects(read({serverId:115})); await assert.rejects(read({samples:4})); await assert.rejects(read({minutes:15})); await assert.rejects(read({skipHeavy:true})) + // Actual source changes between live ticks must appear in the next event, bypassing REST cache. + // Inject after the bounded data projection. The scalar preflight checks its size. + result = await duringFlowSourceQuery(pool, ["FROM flow_buckets", 'AS "__flowPresent"'], + () => put(114, "live-in", 50, "198.18.245.4"), () => read({ samples: 2 })) + assert.equal(result.events.length, 2) + assert.equal(result.events[0].data.bytes, 300) + assert.equal(result.events[1].data.bytes, 350) + // A finished sample cannot be published after authorization is revoked during its SQL read. + await duringFlowSourceQuery(pool, "FROM flow_buckets", + () => setConnectionStatus(access.connection.id, "paused", owner), () => assert.rejects(read())) + await setConnectionStatus(access.connection.id,"active",owner) + // Abort after a real source read started: a completed SQL result is still unpublished. + const duringReadAbort = new AbortController() + await duringFlowSourceQuery(pool, "FROM flow_buckets", async () => duringReadAbort.abort(), + () => assert.rejects(read({}, duringReadAbort.signal))) + const abort = new AbortController(); abort.abort(); await assert.rejects(read({},abort.signal)) + let acquired = 0 + try { while (tryAcquireFlowLiveSlot()) acquired++; assert.equal(acquired,4); await assert.rejects(read()) } + finally { while (acquired-- > 0) releaseFlowLiveSlot() } + assert.equal((await read()).events[0].data.bytes,350) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1",[access.connection.id])).rows[0].n,0) + console.log("✓ NetFlow live: actual shared REST engine, uncached changing ticks, complete DTO paging, SQL scope/mixed user exclusion, pause/cancel publication fences, shared slots and no plans") + } finally { + await http.close() + await setConnectionStatus(access.connection.id,"paused",owner) + await pool.query("DELETE FROM user_interface_bindings WHERE user_id=ANY($1::text[])",[[user,local]]) + await pool.query("DELETE FROM app_users WHERE id=ANY($1::text[])",[[user,local]]) + await pool.query("DELETE FROM flow_buckets WHERE server_id IN (114,115)"); await pool.query("DELETE FROM servers WHERE id IN (114,115)") + resetFlowRingsForTests(); invalidateFlowCatalogCache(); invalidateFlowAnalyticsCache() + resetRipeCacheForTests() + } +} diff --git a/backend/src/test/mcp-flow-map.ts b/backend/src/test/mcp-flow-map.ts new file mode 100644 index 00000000..6b35f80f --- /dev/null +++ b/backend/src/test/mcp-flow-map.ts @@ -0,0 +1,80 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { FlowMapHopsDto } from "@mmapp/contracts/traffic-flow" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +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 { 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" +import trafficFlowRoutes from "../routes/traffic-flow.js" +import { duringFlowSourceQuery } from "./mcp-flow-query-race.js" + +export async function verifyMcpFlowMap(actor: AuthUser) { + const owner = { ...actor, id: "flow-map-owner", permissions: [...actor.permissions, "mm:traffic:read"] } + const users = ["map-local-a", "map-local-b", "map-mixed", "map-peer"] + await pool.query("INSERT INTO servers(id,name,host,password,type) VALUES(112,'map-visible','192.0.2.112','NEVER_MAP_PASSWORD','exit-node'),(113,'NEVER_MAP_HIDDEN_SERVER','192.0.2.113','NEVER_MAP_PASSWORD','exit-node')") + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped map", mode: "observer", expiresAt: new Date(Date.now()+1800000).toISOString(), + permissions: ["mm:traffic:read"], scope: { serverIds: [112], operationIds: ["traffic.flow.map.read"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), owner) + const principal = await authenticateMcpToken(access.token) + const rest = Fastify(); rest.setValidatorCompiler(validatorCompiler); rest.setSerializerCompiler(serializerCompiler) + await rest.register(trafficFlowRoutes,{prefix:"/api"}) + type Report = { map: FlowMapHopsDto; pages: Record; live: boolean; forwardingVerified: boolean; tracerouteExecuted: boolean } + const read = (extra: Record = {}) => invokeMcpTool(principal,"mm_netflow_map_hops_get",{serverId:112,minutes:5,...extra}) as Promise + const bucket = new Date(Date.now()-60000).toISOString(), future = new Date(Date.now()+3600000).toISOString() + const put = (sid: number, name: string, bytes: string | number, src: string, at=bucket) => 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,$2,$3,'8.8.8.8',6,12345,443,$4,1,$5,'wan-map')",[sid,at,src,bytes,name]) + try { + for (const at of [bucket,future]) await ensurePartitionFor(pool,"flow_buckets","day",new Date(at)) + disableRipeEnqueueForTests(); resetFlowRingsForTests(); invalidateFlowCatalogCache(); invalidateFlowMapHopsCache() + for (const id of users) await pool.query("INSERT INTO app_users(id,login,name) VALUES($1,$1,$2)",[id,id===users[2]?"NEVER_MAP_MIXED_USER":id]) + for (const [user,sid,iface,peer] of [[users[0],112,"map-in-a",""],[users[1],112,"map-in-b",""],[users[2],112,"map-in-mixed",""],[users[2],113,"NEVER_MAP_HIDDEN_IFACE",""],[users[3],112,"map-peer-in","NEVER_MAP_PEER_KEY"]] as const) + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,peer_public_key) VALUES($1,$2,$3,$4,$5)",[randomUUID(),user,sid,iface,peer]) + await put(112,"map-in-a",100,"198.18.240.1");await put(112,"map-in-b",200,"198.18.240.2");await put(112,"map-in-mixed",300,"198.18.240.3") + await put(113,"NEVER_MAP_HIDDEN_IFACE","9007199254740993","198.18.240.4");await put(112,"NEVER_MAP_FUTURE_IFACE",999,"198.18.240.5",future) + let result=await read() + assert.equal(result.map.totalBytes,600);assert.equal(result.live,false);assert.equal(result.forwardingVerified,false);assert.equal(result.tracerouteExecuted,false) + assert.ok(!JSON.stringify(result).includes("NEVER_"));assert.ok(result.map.hops.length>=3);assert.ok(result.map.countryServiceGroups!.length>0) + // Actual REST HTTP warms the unrestricted cache with hidden mixed-user metadata. + const response=await rest.inject({method:"GET",url:"/api/traffic/flow/map-hops?serverId=112&range=5m"}) + assert.equal(response.statusCode,200);const legacy=response.json() + assert.equal(legacy.totalBytes,600);assert.deepEqual(result.map.services,legacy.services);assert.deepEqual(result.map.hops,legacy.hops) + assert.ok(JSON.stringify(legacy).includes("NEVER_MAP_MIXED_USER"));assert.ok(!JSON.stringify(await read()).includes("NEVER_")) + result=await read({limit:1,nestedLimit:1}) + assert.equal(result.map.hops.length,1);assert.ok(result.pages.hops.nextOffset!==null) + assert.ok(Object.entries(result.pages).some(([key,page])=>key.endsWith(".clients")&&page.total===2&&page.nextOffset===1)) + assert.ok(Object.entries(result.pages).some(([key,page])=>key.startsWith("countryServiceGroups[")&&key.endsWith(".paths")&&page.total>=2&&page.nextOffset===1)) + assert.equal((await read({offset:1,limit:1,nestedOffset:1,nestedLimit:1})).map.hops.length,1) + assert.equal((await read({interface:"map-in-a"})).map.totalBytes,100);assert.equal((await read({userId:users[0]})).map.totalBytes,100) + await put(112,"map-duplicate",50,"198.18.240.1") + assert.equal((await read()).map.totalBytes,600);assert.equal((await read({dedup:false,excludeMesh:false,excludeOverlay:false})).map.totalBytes,650) + await pool.query("DELETE FROM flow_buckets WHERE server_id=112 AND in_iface='map-duplicate'") + 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()=>{}}),/предел/) + 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) => + duringFlowSourceQuery(pool, match, action, () => assert.rejects(read())) + await duringQuery("FROM flow_buckets",async()=>{await pool.query("UPDATE servers SET host='192.0.2.212' WHERE id=112");await pool.query("UPDATE servers SET host='192.0.2.112' WHERE id=112")}) + await duringQuery("FROM server_snapshots",async()=>{await pool.query("UPDATE user_interface_bindings SET interface_name='map-drift' WHERE user_id=$1",[users[0]])}) + await pool.query("UPDATE user_interface_bindings SET interface_name='map-in-a' WHERE user_id=$1",[users[0]]) + const settings=(await pool.query("SELECT map_service_min_share_pct FROM traffic_flow_settings WHERE id=1")).rows[0] + try {await duringQuery("FROM flow_buckets",()=>pool.query("UPDATE traffic_flow_settings SET map_service_min_share_pct=$1 WHERE id=1",[settings.map_service_min_share_pct===5?6:5]))} + finally {await pool.query("UPDATE traffic_flow_settings SET map_service_min_share_pct=$1 WHERE id=1",[settings.map_service_min_share_pct])} + await duringQuery("FROM flow_buckets",()=>setConnectionStatus(access.connection.id,"paused",owner));await setConnectionStatus(access.connection.id,"active",owner) + assert.equal((await read()).map.totalBytes,600) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1",[access.connection.id])).rows[0].n,0) + console.log("✓ Scoped NetFlow map: actual REST HTTP parity, hidden/mixed/peer/pending exclusion, cache isolation, nested pages, filters/dedup/future/limits, endpoint ABA/topology/threshold drift, cancel/pause and no plans or traceroute") + } finally { + await setConnectionStatus(access.connection.id,"paused",owner);await rest.close() + await pool.query("DELETE FROM user_interface_bindings WHERE user_id=ANY($1::text[])",[users]);await pool.query("DELETE FROM app_users WHERE id=ANY($1::text[])",[users]) + await pool.query("DELETE FROM flow_buckets WHERE server_id IN (112,113)");await pool.query("DELETE FROM servers WHERE id IN (112,113)") + resetFlowRingsForTests();invalidateFlowCatalogCache();invalidateFlowMapHopsCache();resetRipeCacheForTests() + } +} diff --git a/backend/src/test/mcp-flow-overlay.ts b/backend/src/test/mcp-flow-overlay.ts new file mode 100644 index 00000000..1d4e872b --- /dev/null +++ b/backend/src/test/mcp-flow-overlay.ts @@ -0,0 +1,189 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { createSocket } from "node:dgram" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan, loadPlan } from "../modules/mcp/runtime.js" +import { decryptSecret } from "../modules/mcp/security.js" +import { findMcpService } from "../modules/mcp/service-registry.js" +import { generateWireGuardKeyPair } from "../services/wg-keys.js" +import { invalidateTrafficFlowSettingsCache } from "../services/traffic-flow-settings.js" +import { stopTrafficFlowListener, startTrafficFlowListener, getFlowListenerState } from "../services/traffic-flow-ingest.js" +import { readOverlayState } from "../services/traffic-flow-overlay-state.js" +import { overlayMatchesDesired } from "../services/traffic-flow-overlay-reconcile.js" +import { getFlowHostStatus } from "../services/traffic-flow-host-agent.js" +import trafficFlowRoutes from "../routes/traffic-flow.js" + +/** Disposable SQL + actual REST/native HTTP fixture. No real RouterOS or Linux-agent requests. */ +export async function verifyMcpFlowOverlay(actor:AuthUser) { + const owner={...actor,id:"flow-overlay-owner",permissions:[...new Set([...actor.permissions,"mm:traffic:write","mm:network:write","mm:settings:admin"])]} + const saved=(await pool.query("SELECT enabled,host_private_key,host_public_key,peers_json,collector_ip,prefix,flow_listen_port,wg_listen_port FROM traffic_flow_settings WHERE id=1")).rows[0] + const oldAgent=process.env.FLOW_AGENT_SOCKET,oldListen=process.env.FLOW_LISTEN_HOST + delete process.env.FLOW_AGENT_SOCKET;process.env.FLOW_LISTEN_HOST="127.0.0.1" + const hostKeys=generateWireGuardKeyPair(),routerKeys=generateWireGuardKeyPair(),protection=randomUUID() + let collections:Record[]>={},reads=0,writes=0,nextId=0,mode="normal",afterWrite:(()=>Promise)|undefined,afterRead:(()=>Promise)|undefined + const native=createServer(async(req,res)=>{ + const path=decodeURIComponent((req.url??"").replace(/^\/rest/,""));res.setHeader("content-type","application/json") + if(req.method==="GET") { + reads++;await afterRead?.() + const rows=path==="/interface"?[...(collections[path]??[]),...(collections["/interface/wireguard"]??[]).map(row=>({".id":row[".id"],name:row.name,type:"wg",disabled:row.disabled??"false"}))]:collections[path]??[] + res.end(JSON.stringify(rows));return + } + let text="";for await(const chunk of req)text+=chunk + const body=text?JSON.parse(text):{},fields=Object.fromEntries(Object.entries(body).map(([key,value])=>[key,value==="yes"?"true":value==="no"?"false":String(value)])) + const paths=Object.keys(collections).sort((a,b)=>b.length-a.length),base=paths.find(base=>path===base||path.startsWith(base+"/")) + assert.ok(base,"Only typed native overlay paths allowed");writes++ + if(mode!=="ignored") { + if(req.method==="PUT")collections[base].push({".id":"*N"+(++nextId),disabled:"false",...(base==="/interface/wireguard"?{"public-key":routerKeys.publicKey,"private-key":routerKeys.privateKey}:{}),...fields}) + else if(req.method==="PATCH")Object.assign(collections[base].find(row=>row[".id"]===path.slice(base.length+1))!,fields) + else if(req.method==="POST"&&path.endsWith("/set")){if(!collections[base].length)collections[base].push({".id":"*N"+(++nextId)});Object.assign(collections[base][0],fields)} + else if(req.method==="DELETE")collections[base]=collections[base].filter(row=>row[".id"]!==path.slice(base.length+1)) + else assert.fail("Unexpected overlay command") + } + await afterWrite?.() + if(mode==="lost"){req.socket.destroy();return} + res.end("{}") + }) + await new Promise(resolve=>native.listen(0,"127.0.0.1",resolve)) + let hostSnapshot:unknown=null,hostMode="normal",hostWrites=0 + const host=createServer(async(req,res)=>{ + let body="";for await(const chunk of req)body+=chunk + const payload=body?JSON.parse(body):null + if(req.url==="/apply"){hostWrites++;hostSnapshot=payload;if(hostMode==="lost"){req.socket.destroy();return}} + res.setHeader("content-type","application/json") + res.end(JSON.stringify({configured:true,reachable:true,interfaceUp:true,configMatches:req.url==="/status"?null:JSON.stringify(payload)===JSON.stringify(hostSnapshot),publicKey:hostKeys.publicKey,listenPort:51821,addresses:["10.255.254.1/24"],peers:[],error:null})) + }) + const hostSocket=process.platform==="win32"?`\\\\.\\pipe\\mm-overlay-${randomUUID()}`:`/tmp/mm-overlay-${randomUUID()}.sock` + await new Promise(resolve=>host.listen(hostSocket,resolve)) + const udp=createSocket("udp4");await new Promise(resolve=>udp.bind(0,"127.0.0.1",resolve));const listenPort=(udp.address()as{port:number}).port;await new Promise(resolve=>udp.close(()=>resolve())) + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(126,'overlay-visible','127.0.0.1',$1,false,'NEVER_OVERLAY_PASSWORD'),(127,'overlay-host-dependency','192.0.2.127',443,true,'NEVER_OVERLAY_OTHER_PASSWORD')",[(native.address()as{port:number}).port]) + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id")).rows.map(row=>row.id) + const make=async(serverIds:number[])=>createMcpConnection(mcpConnectionCreateSchema.parse({name:"Overlay fixture",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:traffic:write","mm:network:write","mm:settings:admin"],scope:{allowGlobal:true,serverIds,operationIds:["traffic.flow.overlay.apply"]},limits:{callsPerMinute:120,mutationsPerHour:1000}}),owner) + const issued=await make(ids),principal=await authenticateMcpToken(issued.token) + const rest=Fastify();rest.setValidatorCompiler(validatorCompiler);rest.setSerializerCompiler(serializerCompiler);await rest.register(trafficFlowRoutes,{prefix:"/api"}) + const reset=async()=>{ + stopTrafficFlowListener();afterWrite=undefined;afterRead=undefined;mode="normal" + collections=Object.fromEntries(["/interface","/interface/wireguard","/interface/wireguard/peers","/ip/address","/ip/route","/ip/firewall/filter","/ip/traffic-flow","/ip/traffic-flow/ipfix","/ip/traffic-flow/target","/interface/gre"].map(path=>[path,[]])) + collections["/interface"]=[{".id":"*E",name:"ether1",type:"ether"}] + collections["/ip/traffic-flow"]=[{".id":"*F",enabled:"false",interfaces:"ether1"}] + collections["/ip/traffic-flow/ipfix"]=[{".id":"*I",bytes:"false",packets:"false"}] + collections["/interface/gre"]=[{".id":"*G",name:"gre-fixture","allow-fast-path":"true",dynamic:"false"}] + await pool.query("UPDATE servers SET mgmt_tunnel_ip='',updated_at=now() WHERE id=126") + await pool.query("UPDATE traffic_flow_settings SET enabled=false,host_private_key=$1,host_public_key=$2,peers_json='[]',collector_ip='10.255.254.1',prefix='10.255.254.0/24',flow_listen_port=$3,wg_listen_port=51821 WHERE id=1",[hostKeys.privateKey,hostKeys.publicKey,listenPort]);invalidateTrafficFlowSettingsCache() + await delay(100) + } + const prepare=(extra:Record={})=>invokeMcpTool(principal,"mm_netflow_overlay_apply_plan",{serverId:126,publicEndpoint:"router.example.test",...extra}) as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{for(let n=0;n<1200;n++){const row=(await pool.query("SELECT state,error_code,verification,result FROM mcp_operations WHERE id=$1",[id])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("Overlay fixture timed out; never replay")} + const apply=async(extra:Record={})=>{const plan=await prepare(extra);await approvePlan(plan.planId,owner,true);const key=randomUUID(),op=await applyPlan(principal,plan.planId,key);return {plan,key,op,...await wait(op.id)}} + const stepInput=async(planId:string)=>JSON.parse(await decryptSecret((await loadPlan(planId)).evidence.sealed)).steps[0].input + try { + await reset() + const bounded=await readOverlayState(126,{disableGreFastPath:true}) + assert.ok(!JSON.stringify(bounded).includes(hostKeys.privateKey));assert.ok(!JSON.stringify(bounded).includes(hostKeys.publicKey));assert.ok(!JSON.stringify(bounded).includes("NEVER_OVERLAY_PASSWORD")) + const narrow=await make([126]),narrowPrincipal=await authenticateMcpToken(narrow.token),beforeScopeReads=reads + await assert.rejects(invokeMcpTool(narrowPrincipal,"mm_netflow_overlay_apply_plan",{serverId:126}));assert.equal(reads,beforeScopeReads);await setConnectionStatus(narrow.connection.id,"paused",owner) + const unapproved=await prepare();assert.equal(unapproved.state,"awaiting-approval");await assert.rejects(applyPlan(principal,unapproved.planId,randomUUID()));assert.equal(writes,0) + let result=await apply({disableGreFastPath:true});assert.equal(result.state,"applied",JSON.stringify(result));assert.equal(result.verification,"passed") + assert.equal(collections["/interface/gre"][0]["allow-fast-path"],"false");assert.equal(collections["/ip/traffic-flow"][0].enabled,"true") + assert.equal(collections["/ip/traffic-flow/target"][0]["dst-address"],"10.255.254.1");assert.equal(collections["/ip/traffic-flow/target"][0]["src-address"],"0.0.0.0") + const successful=structuredClone(collections),savedWrites=writes + const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.op.id])).rows[0].result + assert.equal(receipt.hostInstallationRequired,true);assert.equal(receipt.stage,"manual");assert.equal(receipt.keyMaterialReturned,false);assert.equal(receipt.forwardingVerified,false) + for(const key of [hostKeys.privateKey,hostKeys.publicKey,routerKeys.privateKey,routerKeys.publicKey])assert.ok(!JSON.stringify(receipt).includes(key)) + await applyPlan(principal,result.plan.planId,result.key);assert.equal(writes,savedWrites) + const input=await stepInput(result.plan.planId),service=findMcpService("traffic.flow.overlay.apply")! + assert.deepEqual(await service.reconcile!(input,async()=>{}),{matchesDesiredState:true,matchesOriginalState:false});assert.equal(writes,savedWrites) + // A matching graph is evidence of configuration, not attribution to this + // exact operation. Only the verified private receipt may establish success. + const checkpoint=(await pool.query("SELECT state,intended_state,result FROM mcp_operation_steps WHERE operation_id=$1",[result.op.id])).rows[0] + const restoreCheckpoint=()=>pool.query("UPDATE mcp_operation_steps SET state=$2,intended_state=$3::jsonb,result=$4::jsonb WHERE operation_id=$1",[result.op.id,checkpoint.state,JSON.stringify(checkpoint.intended_state),JSON.stringify(checkpoint.result)]) + try { + await pool.query("UPDATE mcp_operation_steps SET result=NULL WHERE operation_id=$1",[result.op.id]) + assert.equal((await service.reconcile!(input,async()=>{})).matchesDesiredState,false) + await restoreCheckpoint();await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1",[result.op.id]) + assert.equal((await service.reconcile!(input,async()=>{})).matchesDesiredState,false) + await restoreCheckpoint() + for(const replacement of [ + {...checkpoint.intended_state,service:"foreign.overlay.operation"}, + {...checkpoint.intended_state,input:{...checkpoint.intended_state.input,receiptId:randomUUID()}}, + {...checkpoint.intended_state,input:{...checkpoint.intended_state.input,authority:{...checkpoint.intended_state.input.authority,principal:{...checkpoint.intended_state.input.authority.principal,credentialId:randomUUID()}}}}, + ]) { + await pool.query("UPDATE mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1",[result.op.id,JSON.stringify(replacement)]) + assert.equal((await service.reconcile!(input,async()=>{})).matchesDesiredState,false) + } + await restoreCheckpoint();await pool.query("UPDATE mcp_operations SET connection_id=$2 WHERE id=$1",[result.op.id,narrow.connection.id]) + assert.equal((await service.reconcile!(input,async()=>{})).matchesDesiredState,false) + } finally { + await pool.query("UPDATE mcp_operations SET connection_id=$2 WHERE id=$1",[result.op.id,issued.connection.id]);await restoreCheckpoint() + } + assert.equal((await service.reconcile!(input,async()=>{})).matchesDesiredState,true);assert.equal(writes,savedWrites) + await reset() + const originalPlan=await prepare(),originalInput=await stepInput(originalPlan.planId) + assert.deepEqual(await service.reconcile!(originalInput,async()=>{}),{matchesDesiredState:false,matchesOriginalState:true}) + const response=await rest.inject({method:"POST",url:"/api/traffic/flow-overlay",payload:{serverId:126,publicEndpoint:"router.example.test",disableGreFastPath:true}}) + assert.equal(response.statusCode,200,response.body);assert.equal(response.json().stage,"manual") + const stripIds=(rows:Record[]>)=>Object.fromEntries(Object.entries(rows).map(([path,list])=>[path,list.map(({[".id"]:_id,...row})=>row)])) + assert.deepEqual(stripIds(collections),stripIds(successful)) + await reset();const stale=await prepare();await approvePlan(stale.planId,owner,true);await pool.query("UPDATE servers SET comment='source-drift',updated_at=now() WHERE id=127") + const beforeStale=writes;assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected");assert.equal(writes,beforeStale) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"server",id:"127",serverId:127,protection:"deny"}),owner.id]) + await assert.rejects(prepare());assert.equal((await rest.inject({method:"POST",url:"/api/traffic/flow/overlay",payload:{serverId:126}})).statusCode,409);assert.equal(writes,beforeStale) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + for(const fault of ["ignored","lost"]) { + await reset();mode=fault;const before:number=writes;result=await apply();assert.equal(result.state,"unknown");assert.equal(writes,before+1) + const faultInput=await stepInput(result.plan.planId);assert.equal((await service.reconcile!(faultInput,async()=>{})).matchesDesiredState,false);assert.equal(writes,before+1) + await applyPlan(principal,result.plan.planId,result.key);assert.equal(writes,before+1) + if(fault==="lost") { + mode="normal" + const completed=await rest.inject({method:"POST",url:"/api/traffic/flow-overlay",payload:{serverId:126,publicEndpoint:"router.example.test"}}) + assert.equal(completed.statusCode,200,completed.body) + const original=JSON.parse(await decryptSecret(String(faultInput.originalSealed))) + const options={publicEndpoint:String(faultInput.publicEndpoint),disableGreFastPath:faultInput.disableGreFastPath===true} + assert.equal(overlayMatchesDesired(original,await readOverlayState(126,options),options),true) + const externalWrites:number=writes + assert.equal((await service.reconcile!(faultInput,async()=>{})).matchesDesiredState,false);assert.equal(writes,externalWrites) + } + } + await reset();let fired=false + afterWrite=async()=>{if(!fired){fired=true;await setConnectionStatus(issued.connection.id,"paused",owner)}} + const beforePause=writes;result=await apply();assert.equal(result.state,"unknown");assert.equal(writes,beforePause+1);await setConnectionStatus(issued.connection.id,"active",owner) + await reset();const cancelled=new AbortController();cancelled.abort();await assert.rejects(invokeMcpTool(principal,"mm_netflow_overlay_apply_plan",{serverId:126},cancelled.signal)) + collections["/ip/route"].push({".id":"*FOREIGN","dst-address":"10.255.254.1/32",gateway:"ether1"});await assert.rejects(prepare()) + await reset();process.env.FLOW_AGENT_SOCKET=hostSocket + result=await apply();assert.equal(result.state,"applied",JSON.stringify(result));assert.equal(hostWrites,1) + const autoReceipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.op.id])).rows[0].result + assert.equal(autoReceipt.linuxHostApplied,true);assert.equal(autoReceipt.collectorListening,true);assert.equal(autoReceipt.hostInstallationRequired,false) + assert.equal((await service.reconcile!(await stepInput(result.plan.planId),async()=>{})).matchesDesiredState,true);assert.equal(hostWrites,1) + await reset();hostMode="lost";result=await apply();assert.equal(result.state,"unknown");assert.equal(hostWrites,2) + await applyPlan(principal,result.plan.planId,result.key);assert.equal(hostWrites,2) + const lostHostInput=await stepInput(result.plan.planId),lostHostOriginal=JSON.parse(await decryptSecret(String(lostHostInput.originalSealed))) + const lostHostOptions={publicEndpoint:String(lostHostInput.publicEndpoint),disableGreFastPath:lostHostInput.disableGreFastPath===true} + assert.equal(overlayMatchesDesired(lostHostOriginal,await readOverlayState(126,lostHostOptions),lostHostOptions),true) + // Model a collector already running when the host's apply response was lost. + await startTrafficFlowListener() + const listenerDeadline=Date.now()+10000 + while(!getFlowListenerState().bound&&Date.now(){}),{matchesDesiredState:false,matchesOriginalState:false}) + assert.equal(writes,lostHostWrites);assert.equal(hostWrites,2) + const audit=(await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[issued.connection.id])).rows + for(const key of [hostKeys.privateKey,hostKeys.publicKey,routerKeys.privateKey,routerKeys.publicKey])assert.ok(!JSON.stringify(audit).includes(key)) + console.log("✓ NetFlow overlay: actual shared REST/native HTTP+SQL stages, all-server/global scope, human approval, IPFIX/GRE/DB host peer parity, manual state without host claim, per-command readback, secret proofs/no leaks, stale/protected/cancel/revoke/lost/ignored/no replay and read-only reconciliation") + } finally { + afterWrite=undefined;afterRead=undefined;stopTrafficFlowListener();await rest.close();await new Promise(resolve=>native.close(()=>resolve()));await new Promise(resolve=>host.close(()=>resolve())) + await setConnectionStatus(issued.connection.id,"paused",owner) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await pool.query("DELETE FROM servers WHERE id IN (126,127)") + await pool.query("UPDATE traffic_flow_settings SET enabled=$1,host_private_key=$2,host_public_key=$3,peers_json=$4::jsonb,collector_ip=$5,prefix=$6,flow_listen_port=$7,wg_listen_port=$8 WHERE id=1",[saved.enabled,saved.host_private_key,saved.host_public_key,JSON.stringify(saved.peers_json),saved.collector_ip,saved.prefix,saved.flow_listen_port,saved.wg_listen_port]);invalidateTrafficFlowSettingsCache() + if(oldAgent===undefined)delete process.env.FLOW_AGENT_SOCKET;else process.env.FLOW_AGENT_SOCKET=oldAgent + if(oldListen===undefined)delete process.env.FLOW_LISTEN_HOST;else process.env.FLOW_LISTEN_HOST=oldListen + } +} diff --git a/backend/src/test/mcp-flow-purge.ts b/backend/src/test/mcp-flow-purge.ts new file mode 100644 index 00000000..347512b3 --- /dev/null +++ b/backend/src/test/mcp-flow-purge.ts @@ -0,0 +1,141 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import { PARTITION_SPECS, ensurePartitionFor } from "../db/partitions.js" +import type { AuthUser } from "../lib/permissions.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { flowPurgeTables } from "../services/traffic-flow-purge-execution.js" +import { purgeTrafficFlowStore } from "../services/traffic-flow-ingest.js" +import { recordTrafficFlowPurge } from "../services/flow-purge-service.js" +import { invalidateTrafficFlowSettingsCache } from "../services/traffic-flow-settings.js" +import { createTrafficFlowPurgeRoutes } from "../routes/traffic-flow-purge.js" + +/** Disposable SQL/Fastify.inject only. The listener is disabled; no worker, + * UDP bind, RouterOS/agent/RIPE or notification dispatch is requested. Originals + * of all partition-maintenance parents are held in temporary SQL tables. */ +export async function verifyMcpFlowPurge(actor:AuthUser){ + const backup=await pool.connect(),originalConnect=pool.connect.bind(pool),originalQuery=pool.query.bind(pool) + const owner={...actor,id:"flow-purge-owner",permissions:[...new Set([...actor.permissions,"mm:traffic:write"])]} + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id")).rows.map(row=>row.id) + const ownedServerId=ids.length?null:195 + if(ownedServerId!==null){ + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password,enabled) VALUES($1,'owned-purge-fixture','127.0.0.1',1,false,'owned-fixture',false)",[ownedServerId]) + ids.push(ownedServerId) + } + const sid=ids[0],stamp=new Date().toISOString(),day=stamp.slice(0,10) + const secret="NEVER_FLOW_PURGE_PRIVATE",protection=randomUUID(),grantId=randomUUID() + const settings=(await pool.query("SELECT * FROM traffic_flow_settings WHERE id=1")).rows[0] + let mode="normal",injected=false,deletions=0,faultBaseline=0 + for(const spec of PARTITION_SPECS){ + const count=Number((await backup.query(`SELECT count(*)::text AS n FROM ${spec.parent}`)).rows[0].n) + assert.ok(count<=100000,"Bounded owned fixture snapshot exceeded") + await backup.query(`CREATE TEMP TABLE saved_purge_${spec.parent} AS TABLE ${spec.parent}`) + } + const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Purge fixture",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:traffic:write"],scope:{allowGlobal:true,serverIds:ids,operationIds:["traffic.flow.purge"]},limits:{callsPerMinute:120,mutationsPerHour:10000,serversPerPlan:1000,objectsPerPlan:1000}}),owner) + const principal=await authenticateMcpToken(access.token) + const rest=Fastify();await rest.register(createTrafficFlowPurgeRoutes({purge:()=>purgeTrafficFlowStore(),record:recordTrafficFlowPurge}),{prefix:"/api"}) + const prepare=()=>invokeMcpTool(principal,"mm_netflow_purge_plan",{})as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{for(let i=0;i<800;i++){const row=(await originalQuery("SELECT state,error_code FROM mcp_operations WHERE id=$1",[id])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("Purge still live; never replay")} + const apply=async()=>{const plan=await prepare();assert.equal(plan.state,"awaiting-approval");assert.ok(!JSON.stringify(plan).includes(secret));await approvePlan(plan.planId,owner,true);const key=randomUUID(),op=await applyPlan(principal,plan.planId,key);return{plan,key,op,...await wait(op.id)}} + const seed=async()=>{ + for(const table of Object.values(flowPurgeTables))await originalQuery(`DELETE FROM ${table}`) + await originalQuery("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,bytes) VALUES($1,$2,'192.0.2.1','198.51.100.1',100)",[sid,stamp]) + await originalQuery("INSERT INTO flow_minute_stats(server_id,bucket_at,bytes) VALUES($1,$2,100)",[sid,stamp]) + await originalQuery("INSERT INTO flow_minute_dims(server_id,bucket_at,dim,key,bytes) VALUES($1,$2,'country','XX',100)",[sid,stamp]) + await originalQuery("INSERT INTO flow_daily_dims(server_id,day,dim,key,bytes) VALUES($1,$2,'country','XX',100)",[sid,day]) + await originalQuery("INSERT INTO flow_hour_facts(server_id,bucket_at,iface,country,service,asn,bytes) VALUES($1,$2,'fixture','XX','other',0,100)",[sid,stamp]) + await originalQuery("INSERT INTO flow_daily_facts(server_id,day,iface,country,service,asn,bytes) VALUES($1,$2,'fixture','XX','other',0,100)",[sid,day]) + await originalQuery("UPDATE traffic_flow_settings SET packets_received=42,last_exporter_ip='192.0.2.1',last_error='fixture' WHERE id=1") + } + // Faults intercept only the exact owned deletion transaction. A lost COMMIT + // response is simulated after the real COMMIT, proving unknown without replay. + pool.connect=(async(...connectionArgs:unknown[])=>{ + if(typeof connectionArgs[0]==="function")return(originalConnect as(...args:unknown[])=>unknown)(...connectionArgs) + const client=await originalConnect(),query=client.query.bind(client),release=client.release.bind(client) + let deleting=false + client.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text??"" + if(sql.startsWith("LOCK TABLE flow_buckets"))deleting=true + if(deleting&&sql==="DELETE FROM flow_buckets")deletions++ + if(deleting&&!injected&&mode==="rollback"&&sql==="DELETE FROM flow_minute_dims"){injected=true;return Promise.reject(Error("owned delete failure"))} + if(deleting&&!injected&&mode==="late-source"&&sql.startsWith("SELECT id FROM traffic_flow_settings")){injected=true;return(query as(...args:unknown[])=>Promise)(...args).then(async value=>{await query("UPDATE traffic_flow_settings SET top_n=top_n+1 WHERE id=1");return value})} + if(deleting&&!injected&&mode==="lost-commit"&&sql==="COMMIT"){injected=true;return(query as(...args:unknown[])=>Promise)(...args).then(()=>{throw Error("lost COMMIT response")})} + return(query as(...args:unknown[])=>unknown)(...args) + })as typeof client.query + client.release=((...args:unknown[])=>{client.query=query as typeof client.query;client.release=release;return(release as(...args:unknown[])=>unknown)(...args)})as typeof client.release + return client + })as typeof pool.connect + pool.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text??"" + if(!injected&&mode==="journal-failure"&&/^insert into "events"/i.test(sql)){injected=true;return Promise.reject(Error("NEVER_EVENT_PRIVATE"))} + if(!injected&&mode==="maintenance-failure"&&deletions>faultBaseline&&sql.includes("FROM pg_inherits")){injected=true;return Promise.reject(Error("NEVER_PARTITION_PRIVATE"))} + const pending=(originalQuery as(...args:unknown[])=>Promise)(...args) + if(!injected&&mode==="new-flow"&&sql.startsWith("UPDATE mcp_operation_steps SET state='applied'")){injected=true;return pending.then(async value=>{await originalQuery("INSERT INTO flow_buckets(server_id,bucket_at,src,dst,bytes) VALUES($1,$2,'192.0.2.2','198.51.100.2',200)",[sid,stamp]);return value})} + return pending + })as typeof pool.query + try{ + await originalQuery("INSERT INTO traffic_flow_settings(id,enabled,host_private_key,host_public_key,peers_json) VALUES(1,false,$1,'fixture-public','[]') ON CONFLICT(id) DO UPDATE SET enabled=false,host_private_key=$1,host_public_key='fixture-public',peers_json='[]'",[secret]);invalidateTrafficFlowSettingsCache() + for(const spec of PARTITION_SPECS.filter(spec=>Object.values(flowPurgeTables).includes(spec.parent as never)))await ensurePartitionFor(pool,spec.parent,spec.kind,new Date(stamp)) + await seed() + const unapproved=await prepare();await assert.rejects(applyPlan(principal,unapproved.planId,randomUUID()));assert.equal(deletions,0) + await originalQuery("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)",[grantId,access.connection.id,JSON.stringify({id:grantId,credentialId:principal.credentialId,startsAt:new Date(Date.now()-1000).toISOString(),expiresAt:new Date(Date.now()+60000).toISOString(),scope:{serverIds:ids,operationIds:["traffic.flow.purge"],allowGlobal:false}})]) + await assert.rejects(prepare());await originalQuery("DELETE FROM mcp_grants WHERE id=$1",[grantId]);assert.equal(deletions,0) + let result=await apply();assert.equal(result.state,"applied");assert.equal(deletions,1) + const receipt=(await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.op.id])).rows[0].result + for(const count of Object.values(receipt.deleted))assert.equal(count,1) + const preserved=(await originalQuery("SELECT enabled,host_private_key,host_public_key,peers_json,packets_received,last_exporter_ip,last_error FROM traffic_flow_settings WHERE id=1")).rows[0] + assert.deepEqual(preserved,{enabled:false,host_private_key:secret,host_public_key:"fixture-public",peers_json:[],packets_received:0,last_exporter_ip:null,last_error:""}) + await applyPlan(principal,result.plan.planId,result.key);assert.equal(deletions,1) + await seed();const http=await rest.inject({method:"POST",url:"/api/traffic/flow/purge"});assert.equal(http.statusCode,200,http.body);assert.deepEqual(http.json().deleted,receipt.deleted) + for(const fault of["rollback","late-source","lost-commit"]){ + mode=fault;injected=false;await seed();const before:number=deletions;result=await apply();assert.equal(result.state,"unknown");assert.equal(injected,true) + const count=Number((await originalQuery("SELECT count(*)::text AS n FROM flow_buckets")).rows[0].n) + assert.equal(count,fault==="lost-commit"?0:1);const after:number=deletions;await applyPlan(principal,result.plan.planId,result.key);assert.equal(deletions,after);assert.ok(after<=before+1) + } + for(const fault of["journal-failure","maintenance-failure","new-flow"]){ + mode=fault;injected=false;faultBaseline=deletions;await seed();result=await apply();assert.equal(result.state,"applied");assert.equal(injected,true) + const receipt=(await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.op.id])).rows[0].result + assert.ok(!JSON.stringify(receipt).includes("NEVER_EVENT_PRIVATE"));assert.ok(!JSON.stringify(receipt).includes("NEVER_PARTITION_PRIVATE")) + if(fault==="journal-failure")assert.ok(receipt.eventError) + if(fault==="maintenance-failure")assert.ok(receipt.partitionError) + if(fault==="new-flow")assert.equal(Number((await originalQuery("SELECT count(*)::text AS n FROM flow_buckets")).rows[0].n),1) + const before:number=deletions;await applyPlan(principal,result.plan.planId,result.key);assert.equal(deletions,before) + } + mode="normal";await seed() + const inventory=await prepare();await approvePlan(inventory.planId,owner,true) + assert.equal((await originalQuery("SELECT to_regclass('public.flow_buckets_d_19000101') AS relation")).rows[0].relation,null) + await ensurePartitionFor(pool,"flow_buckets","day",new Date("1900-01-01T00:00:00Z")) + const inventoryOperation=await applyPlan(principal,inventory.planId,randomUUID()) + assert.equal((await wait(inventoryOperation.id)).state,"applied") + assert.ok((await originalQuery("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[inventoryOperation.id])).rows[0].result.partitionError) + assert.ok((await originalQuery("SELECT to_regclass('public.flow_buckets_d_19000101') AS relation")).rows[0].relation) + await originalQuery('DROP TABLE "public"."flow_buckets_d_19000101"') + await seed() + const stale=await prepare();await approvePlan(stale.planId,owner,true);await originalQuery("UPDATE traffic_flow_settings SET host_private_key=$1 WHERE id=1",[secret+"-rotated"]) + const beforeStale:number=deletions;assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected");assert.equal(deletions,beforeStale) + await originalQuery("UPDATE traffic_flow_settings SET host_private_key=$1 WHERE id=1",[secret]) + const lock=await originalConnect();try{await lock.query("SELECT pg_advisory_lock(8723104)");result=await apply();assert.equal(result.state,"unknown");assert.equal(deletions,beforeStale)}finally{await lock.query("SELECT pg_advisory_unlock(8723104)");lock.release()} + await originalQuery("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"netflow-purge",id:"global",protection:"deny"}),owner.id]);await assert.rejects(prepare());assert.equal((await rest.inject({method:"POST",url:"/api/traffic/flow/purge"})).statusCode,409);assert.equal(deletions,beforeStale) + await originalQuery("DELETE FROM mcp_protections WHERE id=$1",[protection]);await setConnectionStatus(access.connection.id,"paused",owner);await assert.rejects(prepare()) + const audit=await originalQuery("SELECT payload FROM mcp_audit WHERE connection_id=$1",[access.connection.id]);assert.ok(!JSON.stringify(audit.rows).includes(secret)) + console.log("✓ Purge SQL/HTTP: full transaction/counters, preserved keys/settings, REST parity, human/global/grant/protection, stale+locked source fence, rollback/lost-COMMIT unknown, rebuild exclusion and no replay; disabled worker and no native/external dispatch") + }finally{ + pool.connect=originalConnect as typeof pool.connect;pool.query=originalQuery as typeof pool.query + await setConnectionStatus(access.connection.id,"paused",owner);await rest.close();await originalQuery("DELETE FROM mcp_protections WHERE id=$1",[protection]);await originalQuery("DELETE FROM mcp_grants WHERE id=$1",[grantId]) + for(const spec of PARTITION_SPECS){ + const column=spec.parent.endsWith("daily_dims")||spec.parent.endsWith("daily_facts")?"day":spec.parent==="server_snapshots"?"polled_at":spec.parent==="traffic_samples"||spec.parent==="servers_rest_ping_samples"||spec.parent.startsWith("uptime_")||spec.parent==="internet_path_snapshots"?"sampled_at":"bucket_at" + const dateExpression=column==="day"?"day":`(${column} AT TIME ZONE 'UTC')::date` + const dates=(await backup.query(`SELECT DISTINCT (${dateExpression})::text AS day FROM saved_purge_${spec.parent} LIMIT 1001`)).rows + assert.ok(dates.length<=1000,"Owned snapshot restore exceeds day cap") + for(const row of dates)await ensurePartitionFor(pool,spec.parent,spec.kind,new Date(`${row.day}T00:00:00Z`)) + await backup.query(`DELETE FROM ${spec.parent}`);await backup.query(`INSERT INTO ${spec.parent} OVERRIDING SYSTEM VALUE SELECT * FROM saved_purge_${spec.parent}`);await backup.query(`DROP TABLE saved_purge_${spec.parent}`) + } + if(settings){const columns=Object.keys(settings).filter(key=>key!=="id");await originalQuery(`UPDATE traffic_flow_settings SET ${columns.map((key,index)=>`${key}=$${index+1}`).join(",")} WHERE id=1`,columns.map(key=>key==="peers_json"?JSON.stringify(settings[key]):settings[key]))}else await originalQuery("DELETE FROM traffic_flow_settings WHERE id=1") + invalidateTrafficFlowSettingsCache();backup.release() + if(ownedServerId!==null)await originalQuery("DELETE FROM servers WHERE id=$1",[ownedServerId]) + } +} diff --git a/backend/src/test/mcp-flow-query-race.ts b/backend/src/test/mcp-flow-query-race.ts new file mode 100644 index 00000000..407e0a18 --- /dev/null +++ b/backend/src/test/mcp-flow-query-race.ts @@ -0,0 +1,45 @@ +import assert from "node:assert/strict" +import type { Pool, PoolClient } from "pg" + +export async function duringFlowSourceQuery( + pool: Pool, + match: string | readonly string[], + afterSourceRead: () => Promise, + exercise: () => Promise, +): Promise { + const originalQuery = pool.query, originalConnect = pool.connect + const fragments = typeof match === "string" ? [match] : match + let scheduled = false, injected = false + const afterRead = (args: unknown[], pending: Promise) => { + const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string } | undefined)?.text + if (scheduled || !sql || !fragments.every(fragment => sql.includes(fragment))) return pending + scheduled = true + return pending.then(async result => { + await afterSourceRead() + injected = true + return result + }) + } + pool.query = ((...args: unknown[]) => afterRead(args, Reflect.apply(originalQuery, pool, args))) as typeof originalQuery + pool.connect = (function (this: Pool, ...args: unknown[]) { + if (args.length) return Reflect.apply(originalConnect, this, args) + return (Reflect.apply(originalConnect, this, []) as Promise).then(client => { + const query = client.query, release = client.release + client.query = ((...queryArgs: unknown[]) => afterRead(queryArgs, Reflect.apply(query, client, queryArgs))) as typeof query + client.release = (...releaseArgs) => { + client.query = query + client.release = release + release.apply(client, releaseArgs) + } + return client + }) + }) as typeof originalConnect + try { + const result = await exercise() + assert.ok(injected, "Race fixture must inject the change after the real NetFlow source query") + return result + } finally { + pool.connect = originalConnect + pool.query = originalQuery + } +} diff --git a/backend/src/test/mcp-geoip-update.ts b/backend/src/test/mcp-geoip-update.ts new file mode 100644 index 00000000..555d610e --- /dev/null +++ b/backend/src/test/mcp-geoip-update.ts @@ -0,0 +1,115 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import path from "node:path" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan, loadPlan } from "../modules/mcp/runtime.js" +import { decryptSecret } from "../modules/mcp/security.js" +import { findMcpService } from "../modules/mcp/service-registry.js" +import { setGeoipFetchForTests, setGeoipValidateForTests, resetGeoipUpdateForTests } from "../services/geoip-update-collector.js" +import { resetGeoipForTests, GEOIP_COUNTRY_FILE, GEOIP_ASN_FILE } from "../services/traffic-flow-geoip.js" +import { updateGeoipManually } from "../services/geoip-manual-update.js" +import geoipRoutes from "../routes/geoip.js" + +/** Isolated real HTTP/file/SQL acceptance. No real mirror or RouterOS traffic. */ +export async function verifyMcpGeoipUpdate(actor:AuthUser){ + const owner={...actor,id:"geoip-update-owner",permissions:[...actor.permissions,"mm:traffic:write"]} + const saved=(await pool.query("SELECT * FROM geoip_settings WHERE id=1")).rows[0] + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id")).rows.map(row=>Number(row.id)) + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GeoIP update fixture",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:traffic:write"],scope:{allowGlobal:true,serverIds:ids,operationIds:["geoip.update"]},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + const principal=await authenticateMcpToken(issued.token),protection=randomUUID(),dir=await mkdtemp(path.join(tmpdir(),"mcp-geoip-update-")),oldDir=process.env.GEOIP_DIR,originalFetch=globalThis.fetch,originalQuery=pool.query.bind(pool) + let mode="normal",calls=0 + const requests:{url:string;etag:string|undefined}[]=[] + const upstream=createServer((req,res)=>{ + calls++;requests.push({url:req.url!,etag:req.headers["if-none-match"] as string|undefined}) + if(mode==="failure"){res.statusCode=503;res.end("NEVER_EXPOSE_UPSTREAM_BODY");return} + if(req.headers["if-none-match"]==='"fixture-v1"'){res.statusCode=304;res.end();return} + res.setHeader("etag",'"fixture-v1"');res.end("fixture "+req.url) + }) + await new Promise(resolve=>upstream.listen(0,"127.0.0.1",resolve)) + const local=`http://127.0.0.1:${(upstream.address()as{port:number}).port}` + process.env.GEOIP_DIR=dir;resetGeoipForTests() + setGeoipFetchForTests((async(url,options)=>{ + const parsed=new URL(String(url));assert.equal(parsed.origin,"https://github.com");assert.ok([GEOIP_COUNTRY_FILE,GEOIP_ASN_FILE].some(file=>parsed.pathname===`/P3TERX/GeoLite.mmdb/raw/download/${file}`)) + const response=await originalFetch(local+"/"+path.posix.basename(parsed.pathname),options) + if(mode==="etag-drift")await pool.query("UPDATE geoip_settings SET etags_json=$1::jsonb WHERE id=1",[JSON.stringify({[GEOIP_COUNTRY_FILE]:"changed-during-download"})]) + if(mode==="build-drift")await pool.query("UPDATE geoip_settings SET country_build_at='2000-01-01T00:00:00Z' WHERE id=1") + return response + })as typeof fetch) + const validate=async(file:string)=>{assert.match(await readFile(file,"utf8"),/^fixture /);return "2026-01-01T00:00:00.000Z"} + setGeoipValidateForTests({country:validate,asn:validate}) + const rest=Fastify();await rest.register(geoipRoutes,{prefix:"/api"}) + const prepare=()=>invokeMcpTool(principal,"mm_geoip_update_plan",{})as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{for(let i=0;i<600;i++){const row=(await pool.query("SELECT state,error_code,verification FROM mcp_operations WHERE id=$1",[id])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("GeoIP fixture remains active; do not replay")} + const apply=async()=>{const plan=await prepare();assert.equal(plan.state,"awaiting-approval");await approvePlan(plan.planId,owner,true);const key=randomUUID(),op=await applyPlan(principal,plan.planId,key);return {plan,key,op,...await wait(op.id)}} + try{ + await pool.query("INSERT INTO geoip_settings(id,enabled,update_interval_sec,etags_json) VALUES(1,false,604800,'{}'::jsonb) ON CONFLICT(id) DO UPDATE SET enabled=false,update_interval_sec=604800,etags_json='{}'::jsonb,last_check_at=NULL,last_success_at=NULL,last_error=NULL,country_build_at=NULL,asn_build_at=NULL") + const unapproved=await prepare();await assert.rejects(applyPlan(principal,unapproved.planId,randomUUID()));assert.equal(calls,0) + const cancelled=new AbortController();cancelled.abort(Error("cancelled before dispatch"));await assert.rejects(updateGeoipManually({signal:cancelled.signal}));await assert.rejects(invokeMcpTool(principal,"mm_geoip_update_plan",{},cancelled.signal));assert.equal(calls,0) + const denied=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GeoIP scope fixture",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:traffic:write"],scope:{allowGlobal:true,serverIds:[],operationIds:["geoip.update"]}}),owner) + if(ids.length)await assert.rejects(invokeMcpTool(await authenticateMcpToken(denied.token),"mm_geoip_update_plan",{})) + await setConnectionStatus(denied.connection.id,"paused",owner) + let result=await apply();assert.equal(result.state,"applied");assert.equal(calls,2) + const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.op.id])).rows[0].result + assert.equal(receipt.downloaded,2);assert.equal(receipt.ok,true);assert.ok(!JSON.stringify(receipt).includes(dir));assert.ok(!JSON.stringify(receipt).includes("NEVER_EXPOSE")) + await applyPlan(principal,result.plan.planId,result.key);assert.equal(calls,2) + const country=await readFile(path.join(dir,GEOIP_COUNTRY_FILE),"utf8") + const response=await rest.inject({method:"POST",url:"/api/geoip/update"});assert.equal(response.statusCode,200,response.body);assert.equal(response.json().snapshot.skippedUnchanged,2);assert.equal(calls,4);assert.deepEqual(requests.slice(2).map(request=>request.etag),['"fixture-v1"','"fixture-v1"']);assert.equal(await readFile(path.join(dir,GEOIP_COUNTRY_FILE),"utf8"),country) + const stale=await prepare();await approvePlan(stale.planId,owner,true);await writeFile(path.join(dir,GEOIP_COUNTRY_FILE),"fixture changed outside approved plan") + const beforeStale=calls;assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected");assert.equal(calls,beforeStale) + for(const kind of ["geoip-settings","geoip-update"]){ + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind,id:"1",protection:"deny"}),owner.id]) + await assert.rejects(prepare());assert.equal((await rest.inject({method:"POST",url:"/api/geoip/update"})).statusCode,409);assert.equal(calls,beforeStale) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + } + mode="failure";result=await apply();assert.equal(result.state,"partial");assert.equal(calls,beforeStale+2);await applyPlan(principal,result.plan.planId,result.key);assert.equal(calls,beforeStale+2) + mode="normal" + const beforeDrift=(await pool.query("SELECT etags_json,country_build_at FROM geoip_settings WHERE id=1")).rows[0] + for(const drift of ["etag-drift","build-drift"]){ + mode=drift;const before:number=calls,result=await apply();assert.equal(result.state,"unknown");assert.equal(calls,before+1,"Late metadata/ETag drift must forbid the second network source") + const atReturn:number=calls;await applyPlan(principal,result.plan.planId,result.key);assert.equal(calls,atReturn) + await pool.query("UPDATE geoip_settings SET etags_json=$1::jsonb,country_build_at=$2 WHERE id=1",[JSON.stringify(beforeDrift.etags_json),beforeDrift.country_build_at]) + } + mode="normal" + const late=await prepare();await approvePlan(late.planId,owner,true);let revoked=false + pool.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text,params=args[1]as unknown[]|undefined + const work=(originalQuery as(...args:unknown[])=>Promise)(...args) + if(!revoked&&sql?.startsWith("INSERT INTO mcp_audit")&¶ms?.[1]==="configuration.intent"&&String(params?.[3]).includes('"kind":"geoip-update"')){revoked=true;return work.then(async value=>{await setConnectionStatus(issued.connection.id,"paused",owner);return value})} + return work + })as typeof pool.query + const beforeLate=calls + try{await wait((await applyPlan(principal,late.planId,randomUUID())).id);assert.equal(revoked,true);assert.equal(calls,beforeLate)}finally{pool.query=originalQuery as typeof pool.query;await setConnectionStatus(issued.connection.id,"active",owner)} + const unknown=await prepare();await approvePlan(unknown.planId,owner,true);let dropped=false,targetWriteId:string|undefined + pool.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text,params=args[1]as unknown[]|undefined + if(sql?.startsWith("INSERT INTO mcp_audit")){ + const payload=JSON.parse(String(params?.[3]??"{}")) + if(params?.[1]==="configuration.intent"&&payload.object?.kind==="geoip-update")targetWriteId=payload.writeId + if(!dropped&&targetWriteId&¶ms?.[1]==="configuration.acknowledged"&&payload.writeId===targetWriteId){dropped=true;return Promise.reject(Error("fixture lost acknowledgement"))} + } + return (originalQuery as(...args:unknown[])=>Promise)(...args) + })as typeof pool.query + const key=randomUUID();let operationId="" + try{const op=await applyPlan(principal,unknown.planId,key);operationId=op.id;assert.equal((await wait(op.id)).state,"unknown");assert.equal(dropped,true)}finally{pool.query=originalQuery as typeof pool.query} + const beforeReplay=calls;await applyPlan(principal,unknown.planId,key);assert.equal(calls,beforeReplay) + const sealed=JSON.parse(await decryptSecret((await loadPlan(unknown.planId)).evidence.sealed)),service=findMcpService("geoip.update")! + assert.deepEqual(await service.reconcile!(sealed.steps[0].input,async()=>{}),{matchesDesiredState:false,matchesOriginalState:false});assert.equal(calls,beforeReplay) + const persisted=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[operationId])).rows + assert.ok(!JSON.stringify(persisted).includes("NEVER_EXPOSE_UPSTREAM_BODY")) + console.log("✓ GeoIP manual update: human approval, shared REST HTTP/file/SQL semantics, disabled force, ETag/304, bounded safe receipts, scope/protections/cancel/stale/late revoke, partial errors, lost acknowledgement unknown, no replay and read-only unresolved reconciliation; isolated upstream only") + }finally{ + pool.query=originalQuery as typeof pool.query;resetGeoipUpdateForTests();resetGeoipForTests();await rest.close();upstream.closeAllConnections();await new Promise(resolve=>upstream.close(()=>resolve())) + if(oldDir===undefined)delete process.env.GEOIP_DIR;else process.env.GEOIP_DIR=oldDir + await setConnectionStatus(issued.connection.id,"paused",owner);await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await pool.query("DELETE FROM geoip_settings WHERE id=1") + if(saved)await pool.query("INSERT INTO geoip_settings SELECT * FROM json_populate_record(NULL::geoip_settings,$1::json)",[JSON.stringify(saved)]) + assert.ok(path.resolve(dir).startsWith(path.resolve(tmpdir())+path.sep));await rm(dir,{recursive:true,force:true}) + } +} diff --git a/backend/src/test/mcp-grant-expiry-bounds.ts b/backend/src/test/mcp-grant-expiry-bounds.ts new file mode 100644 index 00000000..12c531ab --- /dev/null +++ b/backend/src/test/mcp-grant-expiry-bounds.ts @@ -0,0 +1,39 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { FastifyInstance } from "fastify" +import type { AuthUser } from "../lib/permissions.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import { createMcpConnection, getConnection } from "../modules/mcp/store.js" + +/** Real HTTP expiry denials, independent of how long the primary suite takes. + * Each bound is supplied through normal connection creation, never SQL aging. */ +export async function verifyMcpGrantExpiryBounds(app: FastifyInstance, actor: AuthUser, headers: { authorization: string }) { + const now = Date.now(), grantExpiry = now + 600000, shortExpiry = now + 480000 + assert(actor.expiresAt && actor.expiresAt > grantExpiry, "Normal fixture authority must outlive a ten-minute grant") + for (const bound of ["base", "owner"] as const) { + const connectionExpiry: number = bound === "base" ? shortExpiry : actor.expiresAt + const ownerExpiry: number = bound === "owner" ? shortExpiry : actor.expiresAt + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ + name: `Explicit grant ${bound} expiry boundary`, mode: "observer", expiresAt: new Date(connectionExpiry).toISOString(), + permissions: ["mm:servers:read"], scope: { serverIds: [], operationIds: ["servers.list"], allowGlobal: true }, + }), { ...actor, expiresAt: ownerExpiry }) + const row = await getConnection(access.connection.id) + assert.equal(Date.parse(row.policy.expiresAt), connectionExpiry) + assert.equal(row.owner_valid_until.getTime(), ownerExpiry) + assert(grantExpiry > (bound === "base" ? connectionExpiry : ownerExpiry)) + assert(grantExpiry < (bound === "base" ? ownerExpiry : connectionExpiry), "Only the selected expiry bound is exceeded") + const counts = async () => (await pool.query(`SELECT + (SELECT count(*)::int FROM mcp_credentials WHERE connection_id=$1) AS credentials, + (SELECT count(*)::int FROM mcp_grants WHERE connection_id=$1) AS grants`, [access.connection.id])).rows[0] + const before = await counts() + const denied = await app.inject({ method: "POST", url: `/api/mcp/connections/${access.connection.id}/grants`, headers, payload: { + id: randomUUID(), startsAt: new Date(now - 1000).toISOString(), expiresAt: new Date(grantExpiry).toISOString(), + scope: { serverIds: [], operationIds: ["servers.list"], allowGlobal: true }, + } }) + assert.equal(denied.statusCode, 403, `${bound} expiry must deny the grant`) + assert.equal(denied.json().code, "PERMISSION_DENIED") + assert.deepEqual(await counts(), before, "Denied grant must issue neither credentials nor a grant") + } + console.log("✓ Explicit HTTP grant expiry bounds: over-base and over-owner denied without issued credentials") +} diff --git a/backend/src/test/mcp-gre-read-cancellation.ts b/backend/src/test/mcp-gre-read-cancellation.ts new file mode 100644 index 00000000..b4bf2ee4 --- /dev/null +++ b/backend/src/test/mcp-gre-read-cancellation.ts @@ -0,0 +1,120 @@ +import assert from "node:assert/strict" +import httpTransport,{createServer,type ClientRequest,type IncomingMessage} from "node:http" +import {setTimeout as delay} from "node:timers/promises" +import {MikrotikClient} from "../services/mikrotik.js" +import {readGreMutationState} from "../services/gre-mutation-state.js" +import {getServerRowById} from "../modules/servers/repository/servers-repository.js" + +/** Actual HTTP cancellation/settlement with the same SQL reader as production. */ +export async function acceptGreReadCancellation(serverId:number){ + const originalFactory=MikrotikClient.fromServer,originalRequest=httpTransport.request,server=await getServerRowById(serverId);assert.ok(server) + let mode="http",reads=0,writes=0,inFlight=0,closed=0,started:()=>void=()=>{} + const http=createServer((req,res)=>{ + if(req.method!=="GET"){writes++;res.statusCode=405;res.end();return} + reads++;started() + res.on("close",()=>{closed++}) + if(req.url==="/rest/interface/gre"&&mode!=="cancel"){ + const timer=setTimeout(()=>{res.setHeader("content-type","application/json");res.statusCode=mode==="http"?401:200;res.end(mode==="malformed"?'{}':mode==="oversized"?JSON.stringify([{'.id':'*G',name:'gre',opaque:'x'.repeat(1048576)}]):'{"error":401}')},30) + res.on("close",()=>clearTimeout(timer)) + } + // Other responses remain open until the shared cancellation closes them. + }) + await new Promise(resolve=>http.listen(0,"127.0.0.1",resolve)) + const address=http.address();assert.ok(address&&typeof address!=="string") + const port=address.port,requests=new Set() + // Server arrival can lag an already-sent packet. Inspect actual client + // requests to distinguish that from a still-runnable socket queue. + httpTransport.request=((...args:unknown[])=>{ + const request=Reflect.apply(originalRequest,httpTransport,args)as ClientRequest + const options=args[0]as{hostname?:string;port?:number|string} + if(options?.hostname==="127.0.0.1"&&Number(options.port)===port)requests.add(request) + return request + })as typeof originalRequest + const source={...server,id:99945,host:"127.0.0.1",port:address.port,useSsl:false,username:"fixture",password:"fixture"} + MikrotikClient.fromServer=row=>{ + if(row.id!==source.id)return originalFactory(row) + const client=originalFactory(row),get=client.get.bind(client) + client.get=async(path:string,timeout?:number,signal?:AbortSignal)=>{inFlight++;try{return await get(path,timeout,signal)}finally{inFlight--}} + return client + } + try{ + for(const fault of ["http","malformed","oversized","cancel","access"]){ + mode=fault;const beforeReads=reads,beforeClosed=closed,controller=new AbortController() + let access=0,firstStarted:Promise|undefined + if(fault==="cancel")firstStarted=new Promise(resolve=>{started=resolve}) + const work=readGreMutationState(source,null,{allTargets:true,signal:controller.signal,deadlineAt:Date.now()+5000,beforeRead:async()=>{access++;if(fault==="access"&&access>1)throw Error("READ_ACCESS_REVOKED")}}) + const result=assert.rejects(work,error=>fault==="http"?!!error&&typeof error==="object"&&"statusCode"in error&&error.statusCode===401:fault==="malformed"?error instanceof Error&&/каталог/.test(error.message):fault==="oversized"?error instanceof Error&&/размер/.test(error.message):fault==="access"?error instanceof Error&&error.message==="READ_ACCESS_REVOKED":error instanceof Error) + if(firstStarted){await firstStarted;controller.abort(Error("READ_CANCELLED"));started=()=>{}} + await result + assert.equal(inFlight,0,"Reader must settle all native siblings before rejecting") + assert.ok([...requests].every(request=>request.destroyed||request.writableFinished&&(request as ClientRequest&{res?:IncomingMessage}).res?.complete),"Every native request must be destroyed or fully completed before rejection") + const atReturn=requests.size;await delay(100) + assert.equal(requests.size,atReturn,"No new native request may be created after rejection") + assert.equal(closed-beforeClosed,reads-beforeReads,"All started response sockets must close") + if(fault==="access")assert.equal(reads,beforeReads,"Revoked access refuses all wire requests") + assert.equal(writes,0) + } + console.log("✓ GRE source batch: actual HTTP 401/malformed/byte overflow/access revoke/cancel abort siblings, settle every request before rejection, close sockets and start no queued reads afterwards; zero writes") + }finally{MikrotikClient.fromServer=originalFactory;httpTransport.request=originalRequest;http.closeAllConnections();await new Promise(resolve=>http.close(()=>resolve()))} + await acceptGrePskFallback(serverId) +} + +/** Optional unsupported PSK reads may fall back, fatal read failures may not. */ +async function acceptGrePskFallback(serverId:number){ + const originalFactory=MikrotikClient.fromServer,server=await getServerRowById(serverId);assert.ok(server) + const gre={".id":"*G",name:"gre-fallback","local-address":"192.0.2.1","remote-address":"192.0.2.2",mtu:"1476",keepalive:"10s,10",dscp:"inherit",disabled:"false",running:"true","clamp-tcp-mss":"true","allow-fast-path":"true",dynamic:"false"} + let mode="",controller=new AbortController(),revoked=false,wire:string[]=[],printBodies:unknown[]=[],mutations=0,expireBudget:()=>void=()=>{} + const http=createServer((req,res)=>{ + const path=new URL(req.url!,"http://localhost").pathname + wire.push(`${req.method} ${path}`) + const send=(status:number,value:unknown)=>{res.statusCode=status;res.setHeader("content-type","application/json");res.end(JSON.stringify(value))} + if(req.method==="GET"){ + if(path==="/rest/interface/gre")send(200,[gre]) + else if(path==="/rest/interface")send(200,[{".id":"*G",name:gre.name,type:"gre",dynamic:"false"}]) + else if(path==="/rest/user"){ + if(mode==="capability-401")send(401,{error:401}) + else if(mode.startsWith("capability-unsupported"))send(Number(mode.endsWith("400")?400:404),{error:"unsupported"}) + else send(200,[{".id":"*U",name:"fixture",group:"full"}]) + }else if(path==="/rest/user/group"){ + if(mode==="access")revoked=true + if(mode==="budget")expireBudget() + send(200,[{".id":"*P",name:"full",policy:"read,sensitive"}]) + }else send(200,[]) + return + } + if(req.method!=="POST"||!["/rest/interface/gre/get","/rest/interface/gre/print"].includes(path)){mutations++;send(405,{});return} + let body="";req.setEncoding("utf8");req.on("data",chunk=>{body+=chunk});req.on("end",()=>{ + if(path.endsWith("/get")){ + assert.deepEqual(JSON.parse(body),{".id":"*G","value-name":"ipsec-secret"}) + if(mode==="get-401")send(401,{error:401}) + else if(mode==="cancel")controller.abort(Error("PSK_READ_CANCELLED")) + else send(mode==="get-unsupported-400"?400:404,{error:"unsupported"}) + }else{printBodies.push(JSON.parse(body));send(200,[{".id":"*G","ipsec-secret":"explicit-fallback-psk"}])} + }) + }) + await new Promise(resolve=>http.listen(0,"127.0.0.1",resolve)) + const address=http.address();assert.ok(address&&typeof address!=="string") + const source={...server,id:99946,host:"127.0.0.1",port:address.port,useSsl:false,username:"fixture",password:"fixture"} + MikrotikClient.fromServer=row=>originalFactory(row.id===source.id?source:row) + try{ + for(const fault of ["get-unsupported-400","get-unsupported-404","capability-unsupported-400","capability-unsupported-404","capability-401","get-401","cancel","budget","access"]){ + mode=fault;controller=new AbortController();revoked=false;wire=[];printBodies=[] + const options={allTargets:true,signal:controller.signal,deadlineAt:Date.now()+5000,beforeRead:async()=>{if(revoked)throw Error("PSK_ACCESS_REVOKED")}} + expireBudget=()=>{options.deadlineAt=Date.now()-1} + const work=readGreMutationState(source,null,options) + if(fault.includes("unsupported")){ + const state=await work + assert.equal(state.gre[0]["ipsec-secret"],"explicit-fallback-psk") + assert.deepEqual(printBodies,[{"show-sensitive":"",".proplist":".id,ipsec-secret",".query":[".id=*G"]}]) + assert.equal(wire.filter(path=>path==="POST /rest/interface/gre/get").length,1) + }else{ + await assert.rejects(work) + assert.equal(printBodies.length,0,`${fault} must forbid sensitive print fallback`) + if(["capability-401","access","budget"].includes(fault))assert.equal(wire.includes("POST /rest/interface/gre/get"),false,`${fault} must forbid the next getter`) + } + const atReturn=wire.length;await delay(50);assert.equal(wire.length,atReturn,"Sequential PSK reads must not start after completion") + assert.equal(mutations,0,"Only explicit read-only get/print POST commands are permitted") + } + console.log("✓ GRE PSK actual HTTP: unsupported optional 400/404 reads use explicit sensitive print; 401/cancel/budget/access revoke forbid further wire reads; zero mutations") + }finally{MikrotikClient.fromServer=originalFactory;http.closeAllConnections();await new Promise(resolve=>http.close(()=>resolve()))} +} diff --git a/backend/src/test/mcp-gre-revision.ts b/backend/src/test/mcp-gre-revision.ts new file mode 100644 index 00000000..c4b443b8 --- /dev/null +++ b/backend/src/test/mcp-gre-revision.ts @@ -0,0 +1,156 @@ +import assert from "node:assert/strict" +import {randomUUID} from "node:crypto" +import {setTimeout as delay} from "node:timers/promises" +import Fastify from "fastify" +import {validatorCompiler,serializerCompiler} from "@fastify/type-provider-zod" +import {mcpConnectionCreateSchema} from "@mmapp/contracts/mcp" +import type {AuthUser} from "../lib/permissions.js" +import {pool} from "../db/index.js" +import {createMcpConnection,authenticateMcpToken} from "../modules/mcp/store.js" +import {invokeMcpTool,approvePlan,applyPlan} from "../modules/mcp/runtime.js" +import {canonicalGreSnapshot} from "../services/entity-snapshots.js" +import {fingerprintPayload} from "../services/config-revisions.js" +import greRoutes from "../routes/gre.js" +import {acceptGreReadCancellation} from "./mcp-gre-read-cancellation.js" + +type Collections=Record[]> +interface Fixture{get:()=>Collections;set:(value:Collections)=>void;writes:()=>number;reads:()=>number;ignored:(v:boolean)=>void;lost:(v:boolean)=>void;afterCommand:(hook:(()=>Promise)|undefined)=>void;dependencies:(value:Collections)=>void} +export async function acceptGreRevisionRestore(actor:AuthUser,fixture:Fixture){ + const wait=async(id:string)=>{ + for(let attempt=0;attempt<2400;attempt++){ + const state=(await pool.query("SELECT state FROM mcp_operations WHERE id=$1",[id])).rows[0]?.state + if(state&&!["pending","running"].includes(state))return String(state) + await delay(25) + } + throw Error("GRE revision batch did not reach a terminal state") + } + const service="network.gre.revision.restore",serverId=45,ids:string[]=[],protection=randomUUID(),user="gre-revision-bound" + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GRE revision restore",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write","mm:network:read"],scope:{serverIds:[serverId],operationIds:[service,"operations.reconcile"],allowGlobal:false},limits:{callsPerMinute:240,mutationsPerHour:1000}}),actor) + const principal=await authenticateMcpToken(issued.token) + const seed=():Collections=>({"/interface/gre":[{".id":"*RESTORE",name:"restore-old","local-address":"192.0.2.1","remote-address":"192.0.2.2",mtu:"1476",keepalive:"1m,3",dscp:"inherit",disabled:"true",dynamic:"false",running:"false","clamp-tcp-mss":"true","allow-fast-path":"false","ipsec-secret":" NEVER_GRE_RESTORE_PSK ",comment:"old-comment","dont-fragment":"yes"}],"/ip/address":[{".id":"*R4",interface:"restore-old",address:"198.18.45.1/30",disabled:"false",dynamic:"false",invalid:"false",comment:"old-address"}],"/ipv6/address":[]}) + const desired=()=>canonicalGreSnapshot({tunnels:[{name:"restore-old",localAddress:"",remoteAddress:"192.0.2.3",localInnerIp:"fd00:45::1/64",innerAddresses:[{address:"fd00:45::1/64",disabled:false,advertise:false,noDad:true,comment:"address-note"}],remoteInnerIp:"",comment:"",disabled:false,mtu:1400,keepalive:"0",dscp:"inherit",clampTcpMss:true,allowFastPath:false,ipsecSecret:"*****"},{name:"restore-new",localAddress:"",remoteAddress:"192.0.2.4",localInnerIp:"198.18.46.1/30",innerAddresses:[{address:"198.18.46.1/30",disabled:false,comment:""}],remoteInnerIp:"",comment:"",disabled:false,mtu:1476,keepalive:"10s,10",dscp:"inherit",clampTcpMss:true,allowFastPath:true,ipsecSecret:""}]}) + const revision=async(payload:unknown,section="gre",sid=serverId,createdAt?:string)=>{ + const id=randomUUID();ids.push(id) + await pool.query("INSERT INTO config_revisions(id,server_id,section,source,fingerprint,payload,created_at) VALUES($1,$2,$3,'manual',$4,$5::jsonb,COALESCE($6::timestamptz,now()))",[id,sid,section,fingerprintPayload(payload),JSON.stringify(payload),createdAt??null]);return id + } + const plan=(revisionId:string)=>invokeMcpTool(principal,"mm_gre_revision_restore_plan",{serverId,revisionId})as Promise<{planId:string}> + const run=async(planId:string)=>{await approvePlan(planId,actor,true);const key=randomUUID(),op=await applyPlan(principal,planId,key);return{op,key,state:await wait(op.id)}} + const query=pool.query.bind(pool) + const http=Fastify();http.setValidatorCompiler(validatorCompiler);http.setSerializerCompiler(serializerCompiler);await http.register(greRoutes,{prefix:"/api"}) + try{ + fixture.set(seed());fixture.dependencies({}) + const selected=await revision(desired()) + const before=fixture.writes();let reads=fixture.reads() + await assert.rejects(invokeMcpTool(principal,"mm_gre_revision_restore_plan",{serverId:1,revisionId:selected}));assert.equal(fixture.reads(),reads) + await assert.rejects(invokeMcpTool(principal,"mm_gre_revision_restore_plan",{serverId,revisionId:selected,desired:{tunnels:[]}}));assert.equal(fixture.reads(),reads) + for(const mode of ["observer","autonomous"]as const){ + const access=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GRE restore "+mode,mode,expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[serverId],operationIds:[service],allowGlobal:false},limits:{mutationsPerHour:100}}),actor) + const scoped=await authenticateMcpToken(access.token) + if(mode==="observer"){await assert.rejects(invokeMcpTool(scoped,"mm_gre_revision_restore_plan",{serverId,revisionId:selected}));assert.equal(fixture.reads(),reads)} + else{const reviewed=await invokeMcpTool(scoped,"mm_gre_revision_restore_plan",{serverId,revisionId:selected})as{planId:string};await assert.rejects(applyPlan(scoped,reviewed.planId,randomUUID()));assert.equal(fixture.writes(),before)} + } + reads=fixture.reads() + for(const payload of [{},{tunnels:null},{tunnels:[{}]}]){ + const malformed=await revision(payload) + await assert.rejects(plan(malformed));assert.equal(fixture.reads(),reads) + const response=await http.inject({method:"POST",url:`/api/gre/revisions/${malformed}/restore`,payload:{serverId}}) + assert.equal(response.statusCode,409,response.body);assert.equal(fixture.writes(),before) + } + const wrongSection=await revision({tunnels:[]},"wireguard"),wrongServer=await revision({tunnels:[]},"gre",1) + await assert.rejects(plan(wrongSection));await assert.rejects(plan(wrongServer));assert.equal(fixture.reads(),reads) + await pool.query("UPDATE servers SET enabled=false WHERE id=$1",[serverId]);await assert.rejects(plan(selected));assert.equal(fixture.reads(),reads);await pool.query("UPDATE servers SET enabled=true WHERE id=$1",[serverId]) + const limited=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"GRE restore too small",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[serverId],operationIds:[service],allowGlobal:false},limits:{mutationsPerHour:1}}),actor) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(limited.token),"mm_gre_revision_restore_plan",{serverId,revisionId:selected}));assert.equal(fixture.writes(),before) + const stale=await plan(selected);fixture.get()["/interface/gre"][0]["dont-fragment"]="no" + assert.equal((await run(stale.planId)).state,"rejected");assert.equal(fixture.writes(),before);fixture.set(seed()) + const changedRevision=await plan(selected);await pool.query("UPDATE config_revisions SET payload='{}'::jsonb WHERE id=$1",[selected]) + assert.equal((await run(changedRevision.planId)).state,"rejected");assert.equal(fixture.writes(),before) + await pool.query("UPDATE config_revisions SET payload=$2::jsonb WHERE id=$1",[selected,JSON.stringify(desired())]) + const protectedPlan=await plan(selected) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"vlan",id:"*foreign",serverId,protection:"deny"}),actor.id]) + await approvePlan(protectedPlan.planId,actor,true);const protectedOperation=await applyPlan(principal,protectedPlan.planId,randomUUID());assert.equal(await wait(protectedOperation.id),"rejected");assert.equal(fixture.writes(),before) + assert.equal((await http.inject({method:"POST",url:`/api/gre/revisions/${selected}/restore`,payload:{serverId}})).statusCode,409);assert.equal(fixture.writes(),before) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + const parent={".id":"*RESTORE",name:"restore-old",type:"gre",dynamic:"false"} + for(const row of [{...parent,".id":"*OTHER"},{...parent,name:"wrong-name"},{...parent,type:"vlan"},{...parent,dynamic:"true"},{...parent,dynamic:"unknown"}]){ + fixture.dependencies({"/interface":[row]});await assert.rejects(plan(selected));assert.equal(fixture.writes(),before) + } + const noFlag={".id":parent[".id"],name:parent.name,type:parent.type} + fixture.dependencies({"/interface":[noFlag]});await assert.rejects(plan(selected));assert.equal(fixture.writes(),before) + fixture.dependencies({"/interface":[noFlag],"/interface/get":[{".id":parent[".id"],ret:"false"}]});await plan(selected);assert.equal(fixture.writes(),before) + fixture.dependencies({"/interface":[noFlag],"/interface/get":[{".id":parent[".id"],ret:""}]});await assert.rejects(plan(selected));assert.equal(fixture.writes(),before) + const queryBase={"/interface":[noFlag],"/interface/get":[{".id":parent[".id"],ret:"empty-array"}]} + fixture.dependencies(queryBase);await assert.rejects(plan(selected)) + fixture.dependencies({...queryBase,"/interface/static-print":[noFlag]});await plan(selected) + const queryFailures:Collections[]=[{"/interface/dynamic-print":[noFlag]},{"/interface/static-print":[{...noFlag,".id":"*OTHER"}]},{"/interface/static-print":[{...noFlag,type:"vlan"}]},{"/interface/static-print":[{...noFlag,dynamic:"true"}]}] + for(const extra of queryFailures){ + fixture.dependencies({...queryBase,"/interface/static-print":[noFlag],...extra});await assert.rejects(plan(selected));assert.equal(fixture.writes(),before) + } + fixture.dependencies({}) + const accepted=await run((await plan(selected)).planId) + assert.equal(accepted.state,"applied",JSON.stringify((await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[accepted.op.id])).rows[0])) + assert.equal(fixture.get()["/interface/gre"].length,2);assert.ok(fixture.get()["/interface/gre"].every(r=>r.disabled==="false")) + const old=fixture.get()["/interface/gre"].find(r=>r.name==="restore-old")! + assert.equal(old["ipsec-secret"]," NEVER_GRE_RESTORE_PSK ");assert.equal(old["dont-fragment"],"yes");assert.equal(old["local-address"],"0.0.0.0");assert.equal(old.comment,"") + assert.equal(fixture.get()["/ipv6/address"][0]["no-dad"],"true");assert.equal(fixture.get()["/ip/address"][0].interface,"restore-new") + const acceptedWrites=fixture.writes() + const reconciled=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:accepted.op.id}) + assert.ok(JSON.stringify(reconciled).includes('"matchesDesiredState":true'));assert.equal(fixture.writes(),acceptedWrites) + assert.equal((await applyPlan(principal,(await pool.query("SELECT plan_id FROM mcp_operations WHERE id=$1",[accepted.op.id])).rows[0].plan_id,accepted.key)).id,accepted.op.id);assert.equal(fixture.writes(),acceptedWrites) + const noop=await run((await plan(selected)).planId);assert.equal(noop.state,"applied");assert.equal(fixture.writes(),acceptedWrites) + fixture.set(seed()) + const rest=await http.inject({method:"POST",url:`/api/gre/revisions/${selected}/restore`,payload:{serverId}}) + assert.equal(rest.statusCode,200,rest.body);assert.equal(rest.json().mutation.verified,true);assert.equal(rest.json().tunnels.length,2) + const deletion=await revision({tunnels:[]});fixture.set(seed()) + await pool.query("INSERT INTO app_users(id,login,name) VALUES($1,$1,'GRE revision binding')",[user]) + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name) VALUES($1,$2,$3,'restore-old')",[randomUUID(),user,serverId]) + const boundWrites=fixture.writes();await assert.rejects(plan(deletion));assert.equal(fixture.writes(),boundWrites) + await pool.query("DELETE FROM app_users WHERE id=$1",[user]) + fixture.dependencies({"/routing/ospf/interface-template":[{".id":"*REF",interfaces:"restore-old"}]});await assert.rejects(plan(deletion));assert.equal(fixture.writes(),boundWrites);fixture.dependencies({}) + assert.equal((await run((await plan(deletion)).planId)).state,"applied");assert.equal(fixture.get()["/interface/gre"].length,0) + for(const mode of ["PUT","PATCH","DELETE","PAUSE","REVISION"]){ + const method=["PAUSE","REVISION"].includes(mode)?"PATCH":mode + fixture.set(method==="PUT"?{"/interface/gre":[],"/ip/address":[],"/ipv6/address":[]}:seed()) + const payload=method==="PUT"?canonicalGreSnapshot({tunnels:[{...desired().tunnels.find(r=>r.name==="restore-new")!,innerAddresses:[],localInnerIp:""}]}):method==="DELETE"?{tunnels:[]}:canonicalGreSnapshot({tunnels:desired().tunnels.filter(r=>r.name==="restore-old")}) + const id=await revision(payload),prepared=await plan(id);await approvePlan(prepared.planId,actor,true) + let fired=false + pool.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text,params=args[1]as unknown[]|undefined + const pending=(query as(...args:unknown[])=>Promise)(...args) + if(!fired&&sql?.startsWith("INSERT INTO mcp_audit")&¶ms?.[1]==="configuration.intent"&&String(params?.[3]).includes('"method":"'+method+'"')){ + fired=true;return pending.then(async value=>{ + if(mode==="PAUSE")await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1",[issued.connection.id]) + else if(mode==="REVISION")await query("UPDATE config_revisions SET payload='{}'::jsonb WHERE id=$1",[id]) + else await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"vlan",id:"*late",serverId,protection:"deny"}),actor.id]) + return value + }) + } + return pending + })as typeof pool.query + try{const count=fixture.writes(),op=await applyPlan(principal,prepared.planId,randomUUID());assert.equal(await wait(op.id),"unknown");assert.equal(fired,true);assert.equal(fixture.writes(),count)}finally{pool.query=query as typeof pool.query;await query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"active\"'::jsonb) WHERE id=$1",[issued.connection.id])} + } + for(const fault of ["ignored","lost","collateral","late-binding","wrong-default-key"]){ + fixture.set(seed());const prepared=await plan(selected),count=fixture.writes() + if(fault==="ignored")fixture.ignored(true) + if(fault==="lost")fixture.lost(true) + if(fault==="collateral")fixture.afterCommand(async()=>{fixture.get()["/interface/gre"][0]["dont-fragment"]="changed";fixture.afterCommand(undefined)}) + if(fault==="wrong-default-key")fixture.afterCommand(async()=>{fixture.get()["/interface/gre"].find(row=>row.name==="restore-new")!["ipsec-secret"]="WRONG_DEFAULT_GRE_PSK";fixture.afterCommand(undefined)}) + if(fault==="late-binding")fixture.afterCommand(async()=>{await pool.query("INSERT INTO app_users(id,login,name) VALUES($1,$1,'late GRE binding')",[user]);await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name) VALUES($1,$2,$3,'restore-old')",[randomUUID(),user,serverId]);fixture.afterCommand(undefined)}) + const result=await run(prepared.planId);assert.equal(result.state,fault==="lost"?"unknown":"partial");assert.equal(fixture.writes(),count+1) + await applyPlan(principal,prepared.planId,result.key);assert.equal(fixture.writes(),count+1) + fixture.ignored(false);fixture.lost(false);fixture.afterCommand(undefined);await pool.query("DELETE FROM app_users WHERE id=$1",[user]) + } + // The successful append must be allowed to prune the oldest source row. + fixture.set(seed());await pool.query("DELETE FROM config_revisions WHERE server_id=$1 AND section='gre'",[serverId]) + const oldest=await revision(desired(),"gre",serverId,new Date(Date.now()-3600000).toISOString()) + for(let i=0;i<49;i++)await revision({tunnels:[]},"gre",serverId,new Date(Date.now()-1800000+i*1000).toISOString()) + assert.equal((await run((await plan(oldest)).planId)).state,"applied");assert.equal((await pool.query("SELECT count(*)::int AS n FROM config_revisions WHERE id=$1",[oldest])).rows[0].n,0) + const publicRows=(await pool.query("SELECT result::text AS text FROM mcp_requests WHERE connection_id=$1 UNION ALL SELECT intended_state::text||COALESCE(s.result::text,'') FROM mcp_operation_steps s JOIN mcp_operations o ON o.id=s.operation_id WHERE o.connection_id=$1 UNION ALL SELECT payload::text FROM mcp_audit WHERE connection_id=$1",[issued.connection.id])).rows + assert.ok(publicRows.every(row=>!String(row.text).includes("NEVER_GRE_RESTORE_PSK"))) + await acceptGreReadCancellation(serverId) + console.log("✓ GRE revision shared REST/MCP: strict scoped history, whole-section multi-GRE/v4/v6, exact opaque/PSK preservation, staged writes/no-op/empty, budget/protections/dependencies/drift, PUT/PATCH/DELETE after-intent fences, ignored/lost/collateral/late-binding stop, no replay, read-only reconcile, pruning and privacy") + }finally{ + pool.query=query as typeof pool.query;fixture.ignored(false);fixture.lost(false);fixture.afterCommand(undefined);fixture.dependencies({});fixture.set({"/interface/gre":[],"/ip/address":[],"/ipv6/address":[]}) + await http.close();await pool.query("DELETE FROM app_users WHERE id=$1",[user]);await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await pool.query("DELETE FROM config_revisions WHERE id=ANY($1::text[])",[ids]) + } +} diff --git a/backend/src/test/mcp-host-files.ts b/backend/src/test/mcp-host-files.ts new file mode 100644 index 00000000..9fd887fe --- /dev/null +++ b/backend/src/test/mcp-host-files.ts @@ -0,0 +1,103 @@ +import assert from "node:assert/strict" +import type { FastifyInstance } from "fastify" +import { SignJWT } from "jose" +import { env } from "../config.js" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus, effectiveMcpConnection } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { readHumanHostFilesArtifact, verifyHostFilesArtifact } from "../modules/mcp/host-files-artifact.js" +import { loadLogExportArtifactMetadata, createLogExportArtifact } from "../modules/mcp/log-artifacts.js" +import { invalidateTrafficFlowSettingsCache } from "../services/traffic-flow-settings.js" +import { listTrafficFlowHostFiles } from "../services/traffic-flow-host-files.js" + +export async function verifyMcpHostFiles(actor: AuthUser, app: FastifyInstance) { + const owner={...actor,id:"host-files-owner",permissions:[...actor.permissions,"mm:traffic:write"]} + const secret=Buffer.alloc(32,41).toString("base64"), publicKey=Buffer.alloc(32,42).toString("base64") + const saved=(await pool.query("SELECT host_private_key,peers_json,collector_ip,prefix,flow_listen_port,wg_listen_port FROM traffic_flow_settings WHERE id=1")).rows[0] + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id")).rows.map(row=>row.id) + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Host files human export",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:traffic:write","mm:servers:read"],scope:{allowGlobal:true,serverIds:ids,operationIds:["traffic.flow.hostFiles.export","logs.export.read"]},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + const principal=await authenticateMcpToken(issued.token), originalQuery=pool.query.bind(pool) + const jwt=await new SignJWT({sub:owner.id,apps:["mm"],permissions:owner.permissions}).setProtectedHeader({alg:"HS256"}).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + const headers={authorization:"Bearer "+jwt} + const prepare=()=>invokeMcpTool(principal,"mm_netflow_host_files_export_plan",{}) as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{ + for(let n=0;n<600;n++){ + const row=(await pool.query("SELECT state,result FROM mcp_operations WHERE id=$1",[id])).rows[0] + if(!["pending","running"].includes(row.state))return row + await delay(25) + } + throw Error("Host export still live; do not replay") + } + try{ + await pool.query("UPDATE traffic_flow_settings SET host_private_key=$1,peers_json=$2::jsonb,collector_ip='10.255.254.1',prefix='10.255.254.0/24',flow_listen_port=4739,wg_listen_port=51821 WHERE id=1",[secret,JSON.stringify([{serverId:ids[0],name:"fixture-peer",publicKey,allowedIps:["10.255.254.2/32"],address:"10.255.254.2"}])]);invalidateTrafficFlowSettingsCache() + const partial=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Partial export",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:traffic:write"],scope:{allowGlobal:true,serverIds:ids.slice(0,-1),operationIds:["traffic.flow.hostFiles.export"]}}),owner) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(partial.token),"mm_netflow_host_files_export_plan",{}));await setConnectionStatus(partial.connection.id,"paused",owner) + const plan=await prepare();assert.equal(plan.state,"awaiting-approval");assert.ok(!JSON.stringify(plan).includes(secret)) + await assert.rejects(applyPlan(principal,plan.planId,randomUUID())) + await approvePlan(plan.planId,owner,true) + const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key),done=await wait(operation.id) + assert.equal(done.state,"applied",JSON.stringify(done)) + const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1 AND step_index=0",[operation.id])).rows[0].result as {artifactId:string;humanDownloadOnly:boolean;keyMaterialReturned:boolean} + assert.ok(receipt.artifactId);assert.equal(receipt.humanDownloadOnly,true);assert.equal(receipt.keyMaterialReturned,false) + assert.ok(!JSON.stringify(done).includes(secret)) + const stored=(await pool.query("SELECT * FROM mcp_artifacts WHERE id=$1",[receipt.artifactId])).rows[0] + assert.ok(!stored.ciphertext.includes(secret)) + const human=await readHumanHostFilesArtifact(receipt.artifactId,owner) + assert.ok(human.content.includes(secret));assert.deepEqual(JSON.parse(human.content).files,await listTrafficFlowHostFiles()) + const http=await app.inject({url:`/api/mcp/artifacts/${receipt.artifactId}`,headers}) + assert.equal(http.statusCode,200,http.body);assert.equal(http.body,human.content) + const rest=await app.inject({url:"/api/traffic/flow/host-files",headers}) + assert.equal(rest.statusCode,200,rest.body);assert.deepEqual(rest.json().files,JSON.parse(human.content).files) + assert.equal((await app.inject({url:`/api/mcp/artifacts/${receipt.artifactId}`})).statusCode,401) + assert.equal((await app.inject({url:`/api/mcp/artifacts/${receipt.artifactId}`,headers:{authorization:"Bearer "+issued.token}})).statusCode,401) + await assert.rejects(readHumanHostFilesArtifact(receipt.artifactId,{...owner,id:"other-owner"})) + await assert.rejects(readHumanHostFilesArtifact(receipt.artifactId,{...owner,permissions:[]})) + await applyPlan(principal,plan.planId,key) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_artifacts WHERE connection_id=$1",[issued.connection.id])).rows[0].n,1) + await assert.rejects(invokeMcpTool(principal,"mm_logs_export_read",{artifactId:receipt.artifactId})) + const logFile=await createLogExportArtifact('{"message":"safe-fixture"}',"json",ids,{principal,connection:await effectiveMcpConnection(principal)}) + const logResponse=await app.inject({url:logFile.downloadPath,headers});assert.equal(logResponse.statusCode,200,logResponse.body);assert.equal(logResponse.json().message,"safe-fixture") + await pool.query("UPDATE mcp_artifacts SET ciphertext=$2 WHERE id=$1",[logFile.artifactId,stored.ciphertext]) + const crossPurpose=await app.inject({url:logFile.downloadPath,headers});assert.equal(crossPurpose.statusCode,409,crossPurpose.body);assert.ok(!crossPurpose.body.includes(secret)) + for(const [field,value] of [["filename","changed.json"],["ciphertext",stored.ciphertext+"x"],["access_scope",{...stored.access_scope,kind:"logs"}]] as const){ + if(field==="access_scope")await pool.query("UPDATE mcp_artifacts SET access_scope=$2::jsonb WHERE id=$1",[receipt.artifactId,JSON.stringify(value)]) + else await pool.query(`UPDATE mcp_artifacts SET ${field}=$2 WHERE id=$1`,[receipt.artifactId,value]) + await assert.rejects(readHumanHostFilesArtifact(receipt.artifactId,owner)) + if(field==="ciphertext")assert.equal(await verifyHostFilesArtifact(receipt.artifactId,stored.access_scope.authority,stored.access_scope.sourceProof),false) + const rejected=await app.inject({url:`/api/mcp/artifacts/${receipt.artifactId}`,headers});assert.equal(rejected.statusCode,409,rejected.body);assert.ok(!rejected.body.includes(secret)) + if(field==="access_scope")await assert.rejects(loadLogExportArtifactMetadata(receipt.artifactId,issued.connection.id)) + await pool.query("UPDATE mcp_artifacts SET filename=$2,ciphertext=$3,access_scope=$4::jsonb WHERE id=$1",[receipt.artifactId,stored.filename,stored.ciphertext,JSON.stringify(stored.access_scope)]) + } + await pool.query("UPDATE traffic_flow_settings SET wg_listen_port=51822 WHERE id=1") + await assert.rejects(readHumanHostFilesArtifact(receipt.artifactId,owner)) + await pool.query("UPDATE traffic_flow_settings SET wg_listen_port=51821 WHERE id=1") + const stale=await prepare();await approvePlan(stale.planId,owner,true) + await pool.query("UPDATE traffic_flow_settings SET host_private_key=$1 WHERE id=1",[Buffer.alloc(32,43).toString("base64")]) + assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected") + await pool.query("UPDATE traffic_flow_settings SET host_private_key=$1 WHERE id=1",[secret]) + // Revocation after ciphertext SELECT must still prevent plaintext publication. + let fired=false + pool.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0] as {text?:string})?.text + const pending=(originalQuery as (...args:unknown[])=>Promise)(...args) + if(!fired&&sql?.includes("MCP host artifact payload")){fired=true;return pending.then(async row=>{await setConnectionStatus(issued.connection.id,"paused",owner);return row})} + return pending + }) as typeof pool.query + try{await assert.rejects(readHumanHostFilesArtifact(receipt.artifactId,owner));assert.equal(fired,true)}finally{pool.query=originalQuery as typeof pool.query} + const audit=await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[issued.connection.id]);assert.ok(!JSON.stringify(audit.rows).includes(secret)) + const pausedRead=await app.inject({url:`/api/mcp/artifacts/${receipt.artifactId}`,headers}) + assert.equal(pausedRead.statusCode,403,pausedRead.body) + assert.equal(pausedRead.json().code,"PERMISSION_DENIED") + assert.ok(!pausedRead.body.includes(secret)) + assert.equal((await app.inject({url:"/api/mcp/connections",headers})).statusCode,200,"MCP pause must not invalidate the owner's human JWT") + console.log("✓ NetFlow host files: human approval, full global scope, encrypted owner-only bundle, actual JWT HTTP and REST parity, opaque MCP token/no-auth rejection, no key in MCP/audit, signature/kind/cipher tamper and cross-purpose log HTTP refusal, stale source, idempotency and late pause before plaintext publication") + }finally{ + pool.query=originalQuery as typeof pool.query;await setConnectionStatus(issued.connection.id,"paused",owner) + await pool.query("DELETE FROM mcp_artifacts WHERE connection_id=$1",[issued.connection.id]) + await pool.query("UPDATE traffic_flow_settings SET host_private_key=$1,peers_json=$2::jsonb,collector_ip=$3,prefix=$4,flow_listen_port=$5,wg_listen_port=$6 WHERE id=1",[saved.host_private_key,JSON.stringify(saved.peers_json),saved.collector_ip,saved.prefix,saved.flow_listen_port,saved.wg_listen_port]);invalidateTrafficFlowSettingsCache() + } +} diff --git a/backend/src/test/mcp-log-artifacts.ts b/backend/src/test/mcp-log-artifacts.ts new file mode 100644 index 00000000..dee1d657 --- /dev/null +++ b/backend/src/test/mcp-log-artifacts.ts @@ -0,0 +1,210 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { logFiltersSchema } from "@mmapp/contracts" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { authenticateMcpToken, createMcpConnection, effectiveMcpConnection } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" +import { findAction } from "../modules/mcp/actions.js" +import { encryptSecret, redact } from "../modules/mcp/security.js" +import { MAX_LOG_ARTIFACT_CHUNK_BYTES, createLogExportArtifact, MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION } from "../modules/mcp/log-artifacts.js" +import { MAX_LOG_EXPORT_BYTES, readBoundedLogExportRows, LogExportLimitError } from "../services/router-logs-export.js" +import { PARTITION_WRITER_ADVISORY_KEY } from "../services/partition-write-transaction.js" +import { logWhere } from "../services/router-logs-model.js" + +export async function verifyMcpLogArtifacts(actor: AuthUser) { + const ids = [93, 94], marker = "artifact-" + randomUUID() + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=ANY($1::int[])", [ids])).rows[0].n, 0) + const operationIds = ["logs.export", "logs.export.read"] + const issue = async (serverIds = ids, permissions = ["mm:servers:read"]) => { + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "observer", permissions, expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds, operationIds, allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor) + return { issued, principal: await authenticateMcpToken(issued.token) } + } + type Issued = Awaited> + const exportFile = (source: Issued, serverId?: number, format = "json") => invokeMcpTool(source.principal, "mm_logs_export", { ...(serverId ? { serverId } : {}), q: marker, format }) as Promise<{ artifactId: string; bytes: number; sha256: string; rows: number }> + const read = (source: Issued, artifactId: string, options = {}, signal?: AbortSignal) => invokeMcpTool(source.principal, "mm_logs_export_read", { artifactId, ...options }, signal) as Promise<{ dataBase64: string; nextOffset: number | null; offset: number; complete: boolean; sha256: string; bytes: number }> + const blockedPayloadRace = async (source: Issued, file: { artifactId: string }, mutate: () => Promise, expectedCode: string, signal?: AbortSignal) => { + const stored = (await pool.query("SELECT ciphertext FROM mcp_artifacts WHERE id=$1", [file.artifactId])).rows[0].ciphertext + const blocker = await pool.connect() + let pending: Promise<{ error?: unknown }> | undefined + try { + await blocker.query("BEGIN"); await blocker.query("SELECT id FROM mcp_artifacts WHERE id=$1 FOR UPDATE", [file.artifactId]) + pending = read(source, file.artifactId, {}, signal).then(() => ({}), error => ({ error })) + let blocked = false + for (let n = 0; n < 300; n++) { + blocked = (await pool.query("SELECT 1 FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE '%MCP log artifact payload%' AND query LIKE '%FOR SHARE%' LIMIT 1")).rowCount === 1 + if (blocked) break + await delay(10) + } + assert.equal(blocked, true, "Must prove the actual scoped payload SELECT is blocked, not merely observe a request intent") + await mutate() + await blocker.query("UPDATE mcp_artifacts SET ciphertext='opaque corrupted late payload' WHERE id=$1", [file.artifactId]) + await blocker.query("COMMIT") + const outcome = await pending + assert.ok(outcome.error && typeof outcome.error === "object" && "code" in outcome.error) + assert.equal(outcome.error.code, expectedCode, "Lost authority/cancellation must win before payload hashing or decryption") + } finally { + await blocker.query("ROLLBACK").catch(() => {}); blocker.release() + if (pending) await pending + await pool.query("UPDATE mcp_artifacts SET ciphertext=$2 WHERE id=$1", [file.artifactId, stored]) + } + } + try { + await pool.query("INSERT INTO servers(id,name,host,enabled,username,password) SELECT n,$1,'127.0.0.1',false,'fixture','NEVER_ARTIFACT_LOGIN' FROM unnest($2::int[]) AS n", [marker, ids]) + const message = marker + " password=NEVER_ARTIFACT_PASSWORD " + "😀".repeat(3500) + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + VALUES(93,$1,'192.0.2.93','visible','00:00:00',now(),now(),ARRAY['system'],'info',$2,0), + (94,$1,'192.0.2.94','hidden','00:00:00',now(),now(),ARRAY['system'],'info',$3,0)`, [marker, message, marker + " NEVER_HIDDEN_ARTIFACT"]) + const narrow = await issue([93]), wide = await issue() + const context = { principal: narrow.principal, connection: await effectiveMcpConnection(narrow.principal) } + const expired = randomUUID() + await pool.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,expires_at) VALUES($1,$2,'expired.json','application/json',$3,now()-interval '1 second')", + [expired, context.connection.id, await encryptSecret("[]")]) + const writerBlocker = await pool.connect() + try { + await writerBlocker.query("BEGIN") + await writerBlocker.query("SELECT pg_advisory_xact_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) + await assert.rejects(createLogExportArtifact("[]", "json", [93], context), + (error: { code?: string }) => error.code === "55P03" || error.code === "57014") + const { rows } = await writerBlocker.query("SELECT id FROM mcp_artifacts WHERE connection_id=$1", [context.connection.id]) + assert.deepEqual(rows, [{ id: expired }]) + } finally { + try { await writerBlocker.query("ROLLBACK") } finally { writerBlocker.release() } + } + const file = await exportFile(narrow) + assert.equal((await pool.query("SELECT id FROM mcp_artifacts WHERE id=$1", [expired])).rowCount, 0) + assert.equal(file.rows, 1); assert.ok(file.bytes > MAX_LOG_ARTIFACT_CHUNK_BYTES) + const chunks: Buffer[] = []; let offset = 0 + do { + const part = await read(narrow, file.artifactId, offset === 0 ? { offset, limit: 1 } : { offset }) + assert.equal(part.offset, offset); assert.equal(part.sha256, file.sha256); assert.equal(part.bytes, file.bytes) + assert.ok(part.dataBase64.length <= 16000); chunks.push(Buffer.from(part.dataBase64, "base64")) + if (part.nextOffset === null) { assert.equal(part.complete, true); break } + assert.equal(part.complete, false); assert.ok(part.nextOffset > offset); offset = part.nextOffset + } while (true) + const body = Buffer.concat(chunks) + assert.equal(body.length, file.bytes); assert.equal(createHash("sha256").update(body).digest("hex"), file.sha256) + assert.ok(body.toString("utf8").includes("😀")); assert.ok(!body.toString("utf8").includes("NEVER_")); assert.equal(JSON.parse(body.toString("utf8")).length, 1) + const rest = Fastify() + await rest.register((await import("../routes/logs.js")).default, { prefix: "/api" }) + try { + const response = await rest.inject({ method: "GET", url: `/api/logs/export?serverId=93&q=${marker}&format=json` }) + assert.equal(response.statusCode, 200); assert.deepEqual(JSON.parse(body.toString("utf8")), redact(response.json())) + const csv = await rest.inject({ method: "GET", url: `/api/logs/export?serverId=93&q=${marker}&format=csv` }) + assert.equal(csv.statusCode, 200); assert.ok(csv.body.split("\r\n")[0].includes('"host"')) + } finally { await rest.close() } + assert.equal((await read(narrow, file.artifactId, { offset: file.bytes })).dataBase64, "") + await assert.rejects(read(narrow, file.artifactId, { offset: file.bytes + 1 }), (error: { code?: string }) => error.code === "INVALID_INPUT") + await assert.rejects(read(narrow, file.artifactId, { limit: MAX_LOG_ARTIFACT_CHUNK_BYTES + 1 })) + const denied = await issue([93], []) + await assert.rejects(read(denied, file.artifactId)) + const mixed = await exportFile(wide), stored = (await pool.query("SELECT * FROM mcp_artifacts WHERE id=$1", [mixed.artifactId])).rows[0] + assert.equal(mixed.rows, 2) + const singleWide = await exportFile(wide, 93), originalQuery = pool.query.bind(pool) + let metadataPoolReads = 0 + pool.query = ((...args: unknown[]) => { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string } | undefined)?.text + if (text?.includes("MCP log artifact metadata")) metadataPoolReads++ + return (originalQuery as (...values: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { await read(wide, singleWide.artifactId) } finally { pool.query = originalQuery } + assert.ok(metadataPoolReads <= 1, "Target lookup uses the reservation client, not a second pool connection while holding the first") + assert.deepEqual((await pool.query("SELECT server_ids FROM mcp_requests WHERE connection_id=$1 AND tool='mm_logs_export_read' AND input->>'artifactId'=$2 ORDER BY created_at DESC LIMIT 1", [wide.issued.connection.id, singleWide.artifactId])).rows[0].server_ids.map(Number), [93]) + await assert.rejects(read(narrow, mixed.artifactId), (error: { code?: string }) => error.code === "NOT_FOUND") + const grant = randomUUID() + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grant, wide.issued.connection.id, JSON.stringify({ id: grant, credentialId: wide.principal.credentialId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(), scope: { serverIds: [93], operationIds, allowGlobal: false } })]) + await pool.query("UPDATE mcp_artifacts SET ciphertext='corrupt opaque payload' WHERE id=$1", [mixed.artifactId]) + await assert.rejects(read(wide, mixed.artifactId), (error: { code?: string }) => error.code === "GRANT_SCOPE_DENIED") + await pool.query("DELETE FROM mcp_grants WHERE id=$1", [grant]) + await pool.query("UPDATE mcp_artifacts SET ciphertext=$2,access_scope=jsonb_set(access_scope,'{serverIds}','[93]') WHERE id=$1", [mixed.artifactId, stored.ciphertext]) + await assert.rejects(read(wide, mixed.artifactId), (error: { code?: string }) => error.code === "ARTIFACT_INVALID") + await pool.query("UPDATE mcp_artifacts SET access_scope=$2::jsonb,ciphertext='corrupt opaque payload' WHERE id=$1", [mixed.artifactId, JSON.stringify(stored.access_scope)]) + await assert.rejects(read(wide, mixed.artifactId), (error: { code?: string }) => error.code === "ARTIFACT_INVALID") + await pool.query("UPDATE mcp_artifacts SET ciphertext=$2 WHERE id=$1", [mixed.artifactId, stored.ciphertext]) + const legacy = randomUUID() + await pool.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,expires_at) VALUES($1,$2,'legacy.json','application/json',$3,now()+interval '15 minutes')", [legacy, narrow.issued.connection.id, await encryptSecret("NEVER_LEGACY_PAYLOAD")]) + await assert.rejects(read(narrow, legacy), (error: { code?: string }) => error.code === "ARTIFACT_SCOPE_UNKNOWN") + await pool.query("UPDATE mcp_artifacts SET expires_at=now()-interval '1 second' WHERE id=$1", [legacy]) + await assert.rejects(read(narrow, legacy), (error: { code?: string }) => error.code === "NOT_FOUND") + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{version}',to_jsonb((policy->>'version')::int+1)) WHERE id=$1", [wide.issued.connection.id]) + await assert.rejects(read(wide, mixed.artifactId), (error: { code?: string }) => error.code === "RESET_REQUIRED") + const late = await issue(), lateFile = await exportFile(late, 93), lateGrant = randomUUID() + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [lateGrant, late.issued.connection.id, JSON.stringify({ id: lateGrant, credentialId: late.principal.credentialId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(), scope: { serverIds: ids, operationIds, allowGlobal: false } })]) + await blockedPayloadRace(late, lateFile, async () => { await pool.query("UPDATE mcp_grants SET payload=jsonb_set(payload,'{scope,serverIds}','[94]') WHERE id=$1", [lateGrant]) }, "RESET_REQUIRED") + await pool.query("DELETE FROM mcp_grants WHERE id=$1", [lateGrant]) + const revoked = await issue([93]), revokedFile = await exportFile(revoked) + await blockedPayloadRace(revoked, revokedFile, async () => { await pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [revoked.issued.connection.id]) }, "PERMISSION_DENIED") + const paused = await issue([93]), pausedFile = await exportFile(paused) + await blockedPayloadRace(paused, pausedFile, async () => { await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"') WHERE id=$1", [paused.issued.connection.id]) }, "UNAUTHORIZED") + const cancelled = await issue([93]), cancelledFile = await exportFile(cancelled), abort = new AbortController() + await blockedPayloadRace(cancelled, cancelledFile, async () => { abort.abort() }, "READ_CANCELLED", abort.signal) + const budgetRevoked = await issue([93]), blocker = await pool.connect() + let blockedExport: Promise<{ error?: unknown }> | undefined + try { + await blocker.query("BEGIN"); await blocker.query("LOCK TABLE router_log_entries IN ACCESS EXCLUSIVE MODE") + blockedExport = exportFile(budgetRevoked).then(() => ({}), error => ({ error })) + let waiting = false + for (let n = 0; n < 300; n++) { + waiting = (await pool.query("SELECT 1 FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE 'WITH chosen AS MATERIALIZED%' LIMIT 1")).rowCount === 1 + if (waiting) break + await delay(10) + } + assert.equal(waiting, true) + await pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [budgetRevoked.issued.connection.id]) + await blocker.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + VALUES(93,$1,'192.0.2.93','late-budget','00:00:00',now(),now(),ARRAY['system'],'info',$1||repeat('x',$2::int),0)`, [marker, MAX_LOG_EXPORT_BYTES + 1]) + await blocker.query("COMMIT") + const outcome = await blockedExport + assert.ok(outcome.error && typeof outcome.error === "object" && "code" in outcome.error) + assert.equal(outcome.error.code, "PERMISSION_DENIED", "Loss of rights wins even when the source reports a byte limit") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_artifacts WHERE connection_id=$1", [budgetRevoked.issued.connection.id])).rows[0].n, 0) + } finally { await blocker.query("ROLLBACK").catch(() => {}); blocker.release(); if (blockedExport) await blockedExport; await pool.query("DELETE FROM router_log_entries WHERE server_id=93 AND server_name=$1 AND router_id='late-budget'", [marker]) } + const quota = await issue([93]), quotaContext = { principal: quota.principal, connection: await effectiveMcpConnection(quota.principal) } + for (let n = 0; n < 7; n++) await createLogExportArtifact("[]", "json", [93], quotaContext) + const concurrentQuota = await Promise.allSettled([createLogExportArtifact("[]", "json", [93], quotaContext), createLogExportArtifact("[]", "json", [93], quotaContext)]) + assert.equal(concurrentQuota.filter(result => result.status === "fulfilled").length, 1) + assert.ok(concurrentQuota.some(result => result.status === "rejected" && result.reason.code === "EXPORT_QUOTA_EXCEEDED")) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_artifacts WHERE connection_id=$1", [quota.issued.connection.id])).rows[0].n, 8) + const byteQuota = await issue([93]), byteContext = { principal: byteQuota.principal, connection: await effectiveMcpConnection(byteQuota.principal) }, large = " ".repeat(MAX_LOG_EXPORT_BYTES - 2) + "[]" + await createLogExportArtifact(large, "json", [93], byteContext); await createLogExportArtifact(large, "json", [93], byteContext) + await assert.rejects(createLogExportArtifact(large, "json", [93], byteContext), (error: { code?: string }) => error.code === "EXPORT_QUOTA_EXCEEDED") + const byteUsage = (await pool.query("SELECT count(*)::int AS n,sum(octet_length(ciphertext))::text AS bytes FROM mcp_artifacts WHERE connection_id=$1", [byteQuota.issued.connection.id])).rows[0] + assert.equal(byteUsage.n, 2); assert.ok(BigInt(byteUsage.bytes) <= BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION)) + const capabilityAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: marker, mode: "observer", permissions: ["mm:servers:read"], expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: ids, operationIds: ["capabilities.read"], allowGlobal: false } }), actor) + const capabilityPrincipal = await authenticateMcpToken(capabilityAccess.token), capabilityGrant = randomUUID() + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [capabilityGrant, capabilityAccess.connection.id, JSON.stringify({ id: capabilityGrant, credentialId: capabilityPrincipal.credentialId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(), scope: { serverIds: ids, operationIds: ["capabilities.read"], allowGlobal: false } })]) + const heldRead = async (mutate: () => Promise) => { + const action = findAction("mm_capabilities_get"), original = action.read! + let release!: () => void, entered!: () => void + const gate = new Promise(resolve => { release = resolve }), started = new Promise(resolve => { entered = resolve }) + action.read = async (input, context) => { assert.equal(context.transactionClient, undefined, "A released reservation client must never reach the read handler"); entered(); await gate; return original(input, context) } + const pending = invokeMcpTool(capabilityPrincipal, "mm_capabilities_get", {}).then(() => ({} as { error?: unknown }), error => ({ error })) + try { + await Promise.race([started, pending.then(() => { throw Error("Read did not reach its source") })]) + await mutate(); release() + const outcome = await pending + assert.ok(outcome.error && typeof outcome.error === "object" && "code" in outcome.error); assert.equal(outcome.error.code, "STATE_CONFLICT") + } finally { release(); action.read = original; await pending } + } + await heldRead(async () => { await pool.query("UPDATE mcp_grants SET payload=jsonb_set(payload,'{scope,serverIds}','[93]') WHERE id=$1", [capabilityGrant]) }) + await pool.query("DELETE FROM mcp_grants WHERE id=$1", [capabilityGrant]) + await heldRead(async () => { await pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [capabilityAccess.connection.id]) }) + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + VALUES(94,$1,'192.0.2.94','oversize-bytes','00:00:00',now(),now(),ARRAY['system'],'info',$1||repeat('x',$2::int),0)`, [marker, MAX_LOG_EXPORT_BYTES + 1]) + await assert.rejects(readBoundedLogExportRows(logWhere(logFiltersSchema.parse({ serverId: 94, q: marker }))), (error: unknown) => error instanceof LogExportLimitError && error.kind === "bytes") + assert.equal((await exportFile(narrow)).rows, 1, "Hidden oversized rows are excluded before the source budget") + await pool.query("DELETE FROM router_log_entries WHERE server_id=94 AND server_name=$1 AND router_id='oversize-bytes'", [marker]) + await pool.query(`INSERT INTO router_log_entries(server_id,server_name,host,router_id,raw_time,received_at,effective_at,topics,level,message,generation) + SELECT 93,$1,'192.0.2.93','row-cap','00:00:00',now(),now(),ARRAY['system'],'info',$1,0 FROM generate_series(1,50001)`, [marker]) + await assert.rejects(readBoundedLogExportRows(logWhere(logFiltersSchema.parse({ serverId: 93, q: marker }))), (error: unknown) => error instanceof LogExportLimitError && error.kind === "rows") + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=ANY($1::uuid[])", [[narrow.issued.connection.id, wide.issued.connection.id]])).rows).includes("NEVER_")) + console.log("✓ Log artifacts: scoped REST/MCP source and byte/row budgets, Unicode/chunk/hash, signed scope and tamper/expiry/legacy guards, real pre-decrypt grant/owner/pause/abort and source-budget races, atomic count/byte quotas, reservation client reuse and generic late-effective-access fence") + } finally { + await pool.query("DELETE FROM mcp_artifacts WHERE connection_id IN (SELECT id FROM mcp_connections WHERE name=$1)", [marker]) + await pool.query("DELETE FROM router_log_entries WHERE server_id=ANY($1::int[]) AND server_name=$2", [ids, marker]) + await pool.query("DELETE FROM servers WHERE id=ANY($1::int[]) AND name=$2", [ids, marker]) + } +} diff --git a/backend/src/test/mcp-network-hosts.ts b/backend/src/test/mcp-network-hosts.ts new file mode 100644 index 00000000..576a3bb3 --- /dev/null +++ b/backend/src/test/mcp-network-hosts.ts @@ -0,0 +1,31 @@ +import assert from "node:assert/strict" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpNetworkHosts(actor: AuthUser) { + const created: string[] = [] + const create = async (allowGlobal = true, permissions = ["mm:network:write"]) => { + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Resolver scope fixture", mode: "observer", permissions, expiresAt: new Date(Date.now() + 1800000).toISOString(), + scope: { serverIds: [], operationIds: ["network.hosts.resolve"], allowGlobal }, limits: { callsPerMinute: 120 } }), actor) + created.push(issued.connection.id); return issued + } + try { + const issued = await create(), principal = await authenticateMcpToken(issued.token) + const read = (input = { hosts: ["127.0.0.1", "192.0.2.1", "127.0.0.1"] }, signal?: AbortSignal) => invokeMcpTool(principal, "mm_network_hosts_resolve", input, signal) as Promise<{ results: Record; resolverLocation: string; routerResolverVerified: boolean }> + const result = await read(); assert.deepEqual(result.results, { "127.0.0.1": "127.0.0.1", "192.0.2.1": "192.0.2.1" }) + assert.equal(result.resolverLocation, "manager-host"); assert.equal(result.routerResolverVerified, false) + const noGlobal = await create(false), noPermission = await create(true, []), readOnly = await create(true, ["mm:network:read"]) + for (const denied of [noGlobal, noPermission, readOnly]) await assert.rejects(invokeMcpTool(await authenticateMcpToken(denied.token), "mm_network_hosts_resolve", { hosts: ["127.0.0.1"] })) + await assert.rejects(read({ hosts: ["http://127.0.0.1"] })); await assert.rejects(read({ hosts: Array(33).fill("127.0.0.1") })) + const aborted = new AbortController(); aborted.abort(); await assert.rejects(read(undefined, aborted.signal)) + const owner = (await pool.query("SELECT owner_permissions FROM mcp_connections WHERE id=$1", [issued.connection.id])).rows[0].owner_permissions + await pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [issued.connection.id]); await assert.rejects(read()) + await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb WHERE id=$1", [issued.connection.id, JSON.stringify(owner)]) + await setConnectionStatus(issued.connection.id, "paused", actor); await assert.rejects(read()) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [issued.connection.id])).rows[0].n, 0) + console.log("✓ Manager host resolution: explicit global+REST network-write grant, observer, literal/dedup result, strict input, cancellation, owner revoke/pause and no plans; no DNS/native fixture calls") + } finally { for (const id of created) await pool.query("DELETE FROM mcp_connections WHERE id=$1", [id]) } +} diff --git a/backend/src/test/mcp-ospf-legacy.ts b/backend/src/test/mcp-ospf-legacy.ts new file mode 100644 index 00000000..0e0719e8 --- /dev/null +++ b/backend/src/test/mcp-ospf-legacy.ts @@ -0,0 +1,123 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import { pool } from "../db/index.js" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { createMcpConnection, authenticateMcpToken } from "../modules/mcp/store.js" +import { approvePlan, applyPlan, invokeMcpTool } from "../modules/mcp/runtime.js" +import type { AuthUser } from "../lib/permissions.js" + +interface Fixture {get:()=>Record[];set:(rows:Record[])=>void;reads:()=>number;writes:()=>number;resetWrites:(value:number)=>void;fault:(value:string)=>void} +export async function acceptLegacyOspfPlans(actor:AuthUser,fixture:Fixture){ + const original=structuredClone(fixture.get()),originalWrites=fixture.writes(),probe=randomUUID(),protection=randomUUID() + const query=pool.query.bind(pool) + const operations=["routing.ospf.application.optimize","routing.ospf.application.restore","operations.reconcile"] + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Legacy OSPF human plans",mode:"autonomous",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write","mm:network:read"],scope:{serverIds:[1,2],operationIds:operations,allowGlobal:false},limits:{callsPerMinute:240,mutationsPerHour:100}}),actor) + const principal=await authenticateMcpToken(issued.token) + const narrowed=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Legacy OSPF denied catalog",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[1],operationIds:operations.slice(0,2),allowGlobal:false}}),actor) + const narrow=await authenticateMcpToken(narrowed.token) + const observer=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Legacy OSPF observer denied",mode:"observer",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:network:write"],scope:{serverIds:[1,2],operationIds:operations.slice(0,2),allowGlobal:false}}),actor) + const observerPrincipal=await authenticateMcpToken(observer.token) + const prepare=async()=>await invokeMcpTool(principal,"mm_ospf_application_optimize_plan",{serverId:1,pingWeight:60}) as {planId:string;changes:{before:{changes:{id:string;from:number;to:number}[]}}} + const wait=async(id:string)=>{ + for(let i=0;i<2400;i++){ + const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[id])).rows[0] + if(!["pending","running"].includes(row.state))return row + await delay(50) + } + throw Error("Legacy OSPF operation did not finish") + } + const run=async(planId:string,expected:string,key=randomUUID())=>{await approvePlan(planId,actor,true);const operation=await applyPlan(principal,planId,key);const row=await wait(operation.id);assert.equal(row.state,expected,JSON.stringify(row));return operation.id} + const restore=async(operationId:string,firstId?:string)=>{const plan=await invokeMcpTool(principal,"mm_ospf_application_restore_plan",{operationId}) as{planId:string;changes:{before:{changes:{id:string}[]}}};if(firstId)assert.equal(plan.changes.before.changes[0].id,firstId);return run(plan.planId,"applied")} + const reset=()=>{fixture.set([...structuredClone(original),{...original[0],".id":"*secondary",interfaces:"fixture-other",cost:"40","auth-key":"NEVER_LEGACY_OSPF_KEY"}]);fixture.fault("none")} + try{ + await pool.query("INSERT INTO uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,enabled,last_ping_at,last_ping_rtt_ms,last_tx_avg_mbps,last_rx_avg_mbps) VALUES($1,1,2,'fixture-gre',true,now(),10,50,60)",[probe]) + reset() + const deniedWrites=fixture.writes() + const deniedReads=fixture.reads() + await assert.rejects(invokeMcpTool(narrow,"mm_ospf_application_optimize_plan",{serverId:1}),/каталог|област|сервер/i) + await assert.rejects(invokeMcpTool(principal,"mm_ospf_application_optimize_plan",{serverId:1,targets:{"*template":1}})) + await assert.rejects(invokeMcpTool(principal,"mm_ospf_application_optimize_plan",{serverId:1},AbortSignal.abort())) + await assert.rejects(invokeMcpTool(observerPrincipal,"mm_ospf_application_optimize_plan",{serverId:1})) + assert.equal(fixture.reads(),deniedReads) + assert.equal(fixture.writes(),deniedWrites) + const first=await prepare() + assert.equal((await pool.query("SELECT state FROM mcp_plans WHERE id=$1",[first.planId])).rows[0].state,"awaiting-approval") + await assert.rejects(applyPlan(principal,first.planId,randomUUID())) + assert.equal(fixture.writes(),deniedWrites) + assert.equal(first.changes.before.changes.length,2) + assert.deepEqual(first.changes.before.changes.map(row=>row.to),[10,20]) + assert.ok(!JSON.stringify(first).includes("NEVER_LEGACY_OSPF_KEY")) + const key=randomUUID(),applied=await run(first.planId,"applied",key) + assert.equal(fixture.writes(),deniedWrites+2) + assert.equal((await applyPlan(principal,first.planId,key)).id,applied);assert.equal(fixture.writes(),deniedWrites+2) + const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[applied])).rows[0].result + assert.equal(receipt.nativeWrites,2);assert.equal(receipt.outcome,"applied") + const reconciled=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId:applied}) as {results:{matchesDesiredState:boolean}[]} + assert.equal(reconciled.results[0].matchesDesiredState,true);assert.equal(fixture.writes(),deniedWrites+2) + const noop=await prepare();await run(noop.planId,"applied");assert.equal(fixture.writes(),deniedWrites+2) + await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=999 WHERE id=$1",[probe]) + await restore(applied,"*secondary") + for(const row of fixture.get())assert.equal(row.cost,row[".id"]==="*secondary"?"40":original[0].cost) + assert.equal(fixture.writes(),deniedWrites+4,"A fresh human restore must write original costs, independent of new model scores") + await assert.rejects(invokeMcpTool(narrow,"mm_ospf_application_restore_plan",{operationId:applied})) + reset() + const stale=await prepare(),beforeStale=fixture.writes() + await pool.query("UPDATE uptime_speed_probes SET last_ping_rtt_ms=12 WHERE id=$1",[probe]) + await run(stale.planId,"rejected");assert.equal(fixture.writes(),beforeStale) + const protectedPlan=await prepare() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"ospf_template",id:original[0][".id"],serverId:1,protection:"deny"}),actor.id]) + await approvePlan(protectedPlan.planId,actor,true) + await assert.rejects(applyPlan(principal,protectedPlan.planId,randomUUID()),/Защищённый|запрещает/) + assert.equal(fixture.writes(),beforeStale) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + for(const mode of ["protection","pause"]){ + const plan=await prepare();await approvePlan(plan.planId,actor,true) + let fired=false + pool.query=((...args:unknown[])=>{ + const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text,params=args[1]as unknown[]|undefined + const pending=(query as(...args:unknown[])=>Promise)(...args) + if(!fired&&sql?.startsWith("INSERT INTO mcp_audit")&¶ms?.[1]==="configuration.intent"&&String(params?.[3]).includes("/routing/ospf/interface-template/")){ + fired=true;return pending.then(async value=>{ + if(mode==="pause")await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1",[issued.connection.id]) + else await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"ospf_template",id:original[0][".id"],serverId:1,protection:"deny"}),actor.id]) + return value + }) + } + return pending + })as typeof pool.query + try{ + const before=fixture.writes(),operation=await applyPlan(principal,plan.planId,randomUUID()) + assert.equal((await wait(operation.id)).state,"partial");assert.equal(fired,true);assert.equal(fixture.writes(),before) + assert.equal((await query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[operation.id])).rows[0].result.nativeWrites,0) + }finally{ + pool.query=query as typeof pool.query + await query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"active\"'::jsonb) WHERE id=$1",[issued.connection.id]) + } + } + for(const fault of ["ignored","lost-response","changed-field"]){ + reset();const plan=await prepare(),before=fixture.writes();fixture.fault(fault) + const operationId=await run(plan.planId,"unknown");assert.equal(fixture.writes(),before+1) + fixture.fault("none") + const result=await invokeMcpTool(principal,"mm_operation_reconcile",{operationId}) as {results:{matchesOriginalState:boolean;matchesDesiredState:boolean;confirmedPrefix:number|null}[]} + assert.equal(result.results[0].matchesDesiredState,false);assert.equal(fixture.writes(),before+1) + if(fault==="lost-response"){ + assert.equal(result.results[0].confirmedPrefix,1) + await restore(operationId);assert.equal(fixture.writes(),before+2) + assert.ok(fixture.get().every(row=>row.cost===(row[".id"]==="*secondary"?"40":original[0].cost))) + }else{ + if(fault==="ignored")assert.equal(result.results[0].matchesOriginalState,true) + await assert.rejects(invokeMcpTool(principal,"mm_ospf_application_restore_plan",{operationId})) + assert.equal(fixture.writes(),before+1) + } + } + const leak=(await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1",[issued.connection.id])).rows + assert.ok(!JSON.stringify(leak).includes("NEVER_LEGACY_OSPF_KEY")) + console.log("✓ Legacy OSPF public MCP plans: mandatory human approval, full catalog scope, exact cost targets, successful two-cost apply/idempotency, fresh reverse costs after metric drift, pre/post-intent protection/pause, stale zero writes, ignored/lost/collateral no replay, readonly prefix and separate human recovery; local SQL/HTTP only") + }finally{ + pool.query=query as typeof pool.query;fixture.set(original);fixture.resetWrites(originalWrites);fixture.fault("none") + await query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await query("DELETE FROM uptime_speed_probes WHERE id=$1",[probe]) + await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=ANY($1::uuid[])",[[issued.connection.id,narrowed.connection.id,observer.connection.id]]) + } +} diff --git a/backend/src/test/mcp-path-completion.ts b/backend/src/test/mcp-path-completion.ts new file mode 100644 index 00000000..270c0d47 --- /dev/null +++ b/backend/src/test/mcp-path-completion.ts @@ -0,0 +1,68 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { authenticateMcpToken, createMcpConnection } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" +import { requireRecentPath } from "../modules/mcp/path-verification.js" +import { managedComment } from "../managed-markers.js" +import { compileManagedFilterRules } from "../services/filter-rules-write-model.js" + +export async function verifyMcpPathCompletion(actor: AuthUser) { + let mode: "complete" | "incomplete" | "partial" | "failed" | "trace" | "oversized" = "complete", traces = 0 + const collections: Record[]> = { + "/routing/table": [{ ".id": "*main", name: "main", fib: "true", dynamic: "true" }], + "/ip/vrf": [{ ".id": "*vrf", name: "main", interfaces: "all", disabled: "false" }], + "/routing/bgp/connection": [{ ".id": "*bgp", name: "path-peer", instance: "default", vrf: "main", "routing-table": "main", afi: "ip", "input.filter": "bgp-in", "output.filter-chain": "", templates: "", disabled: "false" }], + "/routing/bgp/instance": [{ ".id": "*instance", name: "default", vrf: "main", "routing-table": "main", disabled: "false" }], + "/routing/bgp/template": [], + "/routing/filter/rule": [{ ".id": "*filter", chain: "bgp-in", disabled: "false", comment: managedComment("path-completion"), rule: compileManagedFilterRules([{ community: "64512:1", gateway: "192.0.2.2", outIface: "fixture-gre", action: "route", description: "" }]) }], + "/interface": [{ ".id": "*gre", name: "fixture-gre", disabled: "false", running: "true" }], + "/interface/gre": [{ ".id": "*gre", name: "fixture-gre", disabled: "false", running: "true" }], + "/ip/address": [1, 2].map(n => ({ ".id": "*" + n, address: "192.0.2." + n + "/30", interface: "fixture-gre", disabled: "false", invalid: "false" })), + "/routing/ospf/instance": [], "/routing/rip/instance": [], "/routing/isis/instance": [], "/routing/filter/select-rule": [], "/routing/bgp/vpn": [], "/routing/rule": [], "/ip/firewall/mangle": [], "/ipv6/firewall/mangle": [], + } + const native = createServer(async (req, res) => { + res.setHeader("content-type", "application/json") + const path = (req.url ?? "").replace(/^\/rest/, "") + if (req.method === "GET") { res.end(JSON.stringify(collections[path] ?? [])); return } + let raw = ""; for await (const part of req) raw += part + const body = raw ? JSON.parse(raw) : {} + if (req.method === "POST" && path.endsWith("/get")) { + const row = collections[path.slice(0, -4)]?.find(row => row[".id"] === body[".id"]), value = row?.[body["value-name"]] + res.end(JSON.stringify(value === undefined ? [] : { ret: value })); return + } + assert.equal(body.address, "192.0.2.2") + if (req.url === "/rest/tool/ping") { + assert.equal(body.count, "3") + if (mode === "oversized") res.end(JSON.stringify(Array.from({ length: 257 }, () => ({ status: "NEVER_PROBE_BODY" })))) + else res.end(JSON.stringify([{ sent: mode === "incomplete" ? "2" : "3", received: mode === "failed" ? "0" : ["incomplete", "partial"].includes(mode) ? "2" : "3", "packet-loss": mode === "failed" ? "100" : mode === "partial" ? "33" : "0", ...(mode !== "failed" ? { "avg-rtt": "1ms" } : {}) }])) + } else if (req.url === "/rest/tool/traceroute") { + traces++; res.end(JSON.stringify([{ address: mode === "trace" ? "192.0.2.99" : "192.0.2.2", sent: "1", loss: "0" }])) + } else { res.statusCode = 404; res.end("{}") } + }) + await new Promise(resolve => native.listen(0, "127.0.0.1", resolve)); const address = native.address(); assert(address && typeof address !== "string") + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=92")).rows[0].n, 0) + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,username,password) VALUES(92,'path-completion-fixture','127.0.0.1',$1,false,'fixture','NEVER_PROBE_LOGIN')", [address.port]) + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Path completion fixture", mode: "observer", permissions: ["mm:network:read"], expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: [92], operationIds: ["routing.path.verify"], allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(issued.token) + const profile = routeAiProfileSchema.parse({ name: "path fixture", serverId: 92, family: "ipv4", table: "main", community: "64512:1", candidates: [{ id: "candidate", jumpHostId: 92, exitNodeId: 92, wanInterface: "fixture-gre", downstreamInterface: "fixture-gre", gateway: "192.0.2.2", gatewayRef: { resource: "gre", id: "*gre" }, controlProbe: { targetInterface: "fixture-gre", targetAddress: "192.0.2.2", sourceAddress: "192.0.2.1", expectedHops: [{ serverId: 92, interface: "fixture-gre", address: "192.0.2.2" }] } }] }) + const check = () => invokeMcpTool(principal, "mm_route_path_verify", { profile, candidateId: "candidate" }) as Promise<{ id: string; status: string; reason?: string; packetCounts: { sent: number; received: number } | null; pingObservation?: { terminalSent: number | null } }> + const operationsBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_operations")).rows[0].n + const passed = await check(); assert.equal(passed.status, "passed"); assert.equal(traces, 1) + assert.equal(await requireRecentPath(profile, "candidate", principal.connectionId), passed.id) + mode = "incomplete"; const incomplete = await check() + assert.equal(incomplete.status, "unknown"); assert.equal(incomplete.reason, "PING_COUNTERS_UNCONFIRMED"); assert.equal(incomplete.packetCounts, null); assert.equal(incomplete.pingObservation?.terminalSent, 2); assert.equal(traces, 1) + await assert.rejects(requireRecentPath(profile, "candidate", principal.connectionId), (error: { code?: string }) => error.code === "MEASUREMENT_UNKNOWN") + mode = "partial"; const partial = await check(); assert.equal(partial.status, "unknown"); assert.equal(partial.reason, "PING_PARTIAL_LOSS"); assert.equal(traces, 1) + mode = "trace"; const trace = await check(); assert.equal(trace.status, "unknown"); assert.equal(trace.reason, "TRACE_UNCONFIRMED"); assert.equal(traces, 2) + mode = "oversized"; const oversized = await check(); assert.equal(oversized.status, "unknown"); assert.equal(oversized.reason, "PROBE_UNAVAILABLE"); assert.equal(traces, 2); assert.ok(!JSON.stringify(oversized).includes("NEVER_")) + mode = "failed"; const failed = await check(); assert.equal(failed.status, "failed"); assert.deepEqual(failed.packetCounts, { sent: 3, received: 0 }); assert.equal(traces, 2) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations")).rows[0].n, operationsBefore) + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [principal.connectionId])).rows).includes("NEVER_")) + console.log("✓ Path admission: real SQL plus HTTP; only complete 3/3 and verified trace grants a recent path, 2/2/partial/oversized/trace unknown cannot, 3/0 is confirmed loss, bounded safe observations and no operations") + } finally { native.closeAllConnections(); await new Promise(resolve => native.close(() => resolve())); await pool.query("DELETE FROM servers WHERE id=92 AND name='path-completion-fixture'") } +} diff --git a/backend/src/test/mcp-principal-source-rows.ts b/backend/src/test/mcp-principal-source-rows.ts new file mode 100644 index 00000000..7f8bee28 --- /dev/null +++ b/backend/src/test/mcp-principal-source-rows.ts @@ -0,0 +1,24 @@ +/** Synthetic SQL rows for tests of callers of the actual principal reader. + * Digests below model stable JS fixtures, not PostgreSQL JSONB text encoding. + * Actual SQL digest/precision and race evidence belongs to the owned PG tests. */ +import { createHash } from "node:crypto" +import type { McpPrincipal } from "../modules/mcp/store.js" + +export function principalSourceRowsForTests(connection: Record, principal: Pick, + options: { includeGrants?: boolean; credentialActive?: boolean; enabled?: boolean; cutoff?: Date | null; grants?: { id?: string; payload: unknown }[] } = {}) { + const utc = (value: Date) => value.toISOString().replace(/Z$/, "000Z") + const digest = (value: unknown) => createHash("sha256").update(JSON.stringify(value) ?? "null").digest("hex") + const active = options.credentialActive !== false, cutoff = options.cutoff ?? null + const core = { ...structuredClone(connection), __ps_exists: true, __ps_oversize: false, __ps_bytes: 4096, __ps_guard: true, + __ps_isolation: "read committed", __ps_policy_sha256: digest(connection.policy), + __ps_credential: active ? principal.credentialId : null, __ps_credential_current: active, + __ps_credential_expires: active ? utc(new Date(principal.credentialExpiresAt)) : null, __ps_credential_revoked: null, + __ps_owner_until: utc(connection.owner_valid_until as Date), __ps_owner_issued: utc(connection.owner_claims_issued_at as Date), + __ps_cutoff_owner: cutoff ? connection.owner_id : null, __ps_cutoff: cutoff, __ps_cutoff_text: cutoff ? utc(cutoff) : null, + __ps_settings: 1, __ps_enabled: options.enabled !== false } + const grants = options.includeGrants ? options.grants?.slice(0, 2) ?? [] : [] + return grants.length ? grants.map((grant, index) => ({ ...core, + __ps_grant_id: grant.id ?? "00000000-0000-4000-8000-" + String(index + 1).padStart(12, "0"), + __ps_grant_oversize: false, __ps_grant_payload: structuredClone(grant.payload), __ps_grant_sha256: digest(grant.payload) })) + : [{ ...core, __ps_grant_id: null, __ps_grant_oversize: null, __ps_grant_payload: null, __ps_grant_sha256: null }] +} diff --git a/backend/src/test/mcp-router-request-drain.ts b/backend/src/test/mcp-router-request-drain.ts new file mode 100644 index 00000000..a99c3cb8 --- /dev/null +++ b/backend/src/test/mcp-router-request-drain.ts @@ -0,0 +1,145 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { z } from "zod" +import { pool } from "../db/index.js" +import { SYSTEM_DATABASE_REQUEST_LOCK, withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { withConfigurationActor } from "../services/config-mutation-policy.js" +import { configurationMutationSignal, withConfigMutationLock } from "../services/config-mutation-lock.js" +import { MikrotikClient } from "../services/mikrotik.js" +import { add } from "../modules/mcp/actions.js" +import { applyPlan, approvePlan, invokeMcpTool } from "../modules/mcp/runtime.js" +import { registerMcpService } from "../modules/mcp/service-registry.js" +import { authenticateMcpToken, createMcpConnection } from "../modules/mcp/store.js" + +export async function verifyRouterRequestDrain() { + let arrived!: () => void, release!: () => void + const arrival = new Promise(resolve => { arrived = resolve }) + let responseAllowed = new Promise(resolve => { release = resolve }) + let closed = () => {} + let nativeCalls = 0 + const router = createServer(async (request, response) => { + request.resume() + nativeCalls++ + response.once("close", closed) + arrived() + await responseAllowed + if (!response.destroyed) response.writeHead(200, { "content-type": "application/json" }).end("{}") + }) + await new Promise((resolve, reject) => router.once("error", reject).listen(0, "127.0.0.1", resolve)) + const address = router.address() + assert(address && typeof address !== "string") + const params = { host: "127.0.0.1", port: address.port, username: "fixture", password: "fixture", useSsl: false, verifySsl: true } + const actorId = "owned-router-request-drain" + const write = (serverId: number) => withConfigurationActor(actorId, () => new MikrotikClient(params, serverId).patch("/interface/*1", { disabled: "yes" })) + let maintenance: Promise | undefined, inFlight: Promise | undefined, nested: Promise | undefined + const terminate = async (id: number) => { + const { rows } = await pool.query(`SELECT pid FROM pg_locks WHERE locktype='advisory' AND objsubid=1 AND mode='ExclusiveLock' AND granted + AND ((classid::bigint << 32) | objid::bigint)=hashtextextended($1,0)`, [`mm-config:${id}`]) + assert.equal(rows.length, 1, "Expected the exact owned RouterOS lease") + assert.equal((await pool.query("SELECT pg_terminate_backend($1) AS stopped", [rows[0].pid])).rows[0].stopped, true) + } + const wait = (promise: Promise, message: string) => Promise.race([promise, delay(2500, undefined, { ref: false }).then(() => assert.fail(message))]) + try { + inFlight = withConfigMutationLock([712], async () => { nested = write(712) }) + void inFlight.catch(() => {}) + await Promise.race([arrival, inFlight.then(() => assert.fail("Expected held native response")), delay(2000, undefined, { ref: false }).then(() => assert.fail("Native request did not arrive"))]) + maintenance = withSystemDatabaseOperationLock(() => pool.connect(), async () => { + const { rows } = await pool.query("SELECT action FROM mcp_audit WHERE actor_id=$1 ORDER BY id", [actorId]) + assert.deepEqual(rows.map(row => row.action), ["configuration.intent", "configuration.acknowledged"]) + await assert.rejects(write(713), { code: "PROTOCOL_STORAGE_BUSY" }) + }) + void maintenance.catch(() => {}) + let waiting = false + for (let i = 0; i < 100 && !waiting; i++) { + const { rowCount } = await pool.query("SELECT 1 FROM pg_locks WHERE locktype='advisory' AND classid=0 AND objid=$1::oid AND objsubid=1 AND mode='ExclusiveLock' AND NOT granted", [SYSTEM_DATABASE_REQUEST_LOCK]) + waiting = !!rowCount + if (!waiting) await delay(15) + } + assert(waiting, "Database maintenance must wait for the pending RouterOS response") + await assert.rejects(write(713), { code: "PROTOCOL_STORAGE_BUSY" }) + assert.equal(nativeCalls, 1) + release() + await inFlight + await nested + await maintenance + assert.equal(nativeCalls, 1) + await write(713) + assert.equal(nativeCalls, 2) + console.log("RouterOS request drain: pending response and committed audit precede maintenance; competing native writes refused") + for (const afterDispatch of [false, true]) { + const id = afterDispatch ? 715 : 714, actor = `owned-router-session-loss-${id}` + const point = new Promise(resolve => { arrived = resolve }) + const socketClosed = new Promise(resolve => { closed = resolve }) + responseAllowed = new Promise(resolve => { release = resolve }) + let scope: AbortSignal | undefined + inFlight = withConfigurationActor(actor, () => new MikrotikClient(params, id).patch("/interface/*1", { disabled: "yes" }, 10000, async () => { + scope = configurationMutationSignal() + if (!afterDispatch) { arrived(); await responseAllowed } + })) + void inFlight.catch(() => {}) + await wait(point, "RouterOS test did not reach the termination point") + assert(scope && !scope.aborted) + const lost = new Promise(resolve => scope!.addEventListener("abort", () => resolve(), { once: true })) + await terminate(id) + await wait(lost, "SQL session loss did not reach the native scope") + if (afterDispatch) await wait(socketClosed, "SQL session loss left the RouterOS socket open") + release() + await assert.rejects(inFlight, { code: afterDispatch ? "MUTATION_OUTCOME_UNKNOWN" : "PROTOCOL_STORAGE_BUSY" }) + assert.equal(nativeCalls, afterDispatch ? 3 : 2) + const { rows } = await pool.query("SELECT action FROM mcp_audit WHERE actor_id=$1 ORDER BY id", [actor]) + assert.deepEqual(rows.map(row => row.action), ["configuration.intent", "configuration.unconfirmed"]) + } + console.log("RouterOS SQL session loss: zero pre-dispatch requests, one in-flight request aborted, no acknowledged audit") + + const serverId = 716, operationName = "fixture.router-request-lease", tool = "mm_owned_router_request_lease_plan" + const actor = { id: "owned-router-mcp-request-lease", name: "Owned fixture", email: "owned@example.test", apps: ["mm"], + permissions: ["mm:servers:write", "mm:mcp:approve"], expiresAt: Date.now() + 120000 } + await pool.query("INSERT INTO servers(id,name,host,port,enabled) VALUES($1,'owned-lease','127.0.0.1',$2,false)", [serverId, address.port]) + const native = new MikrotikClient(params, serverId) + registerMcpService(operationName, { + current: async () => ({}), verify: async () => true, + execute: async () => { await native.patch("/interface/*1", { disabled: "yes" }); return native.patch("/interface/*1", { disabled: "no" }) }, + }) + const target = () => ({ scope: "servers" as const, serverIds: [serverId] }) + add(operationName, tool, "Owned RouterOS request lease fixture", "servers", "mm:servers:write", "write", "servers", z.object({}).strict(), { + target, prepare: async () => ({ target: target(), before: {}, snapshots: [], objects: [], warnings: [], steps: [{ kind: "service", service: operationName, input: {} }] }), + }) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned request lease", mode: "approval", permissions: ["mm:servers:write"], + scope: { allowGlobal: false, serverIds: [serverId], operationIds: [operationName] }, expiresAt: new Date(actor.expiresAt).toISOString() }), actor) + const principal = await authenticateMcpToken(access.token) + const plan = await invokeMcpTool(principal, tool, {}) as { planId: string } + await approvePlan(plan.planId, actor, true) + const requestArrived = new Promise(resolve => { arrived = resolve }) + const requestClosed = new Promise(resolve => { closed = resolve }) + responseAllowed = new Promise(resolve => { release = resolve }) + const idempotencyKey = randomUUID(), operation = await applyPlan(principal, plan.planId, idempotencyKey) + await wait(requestArrived, "MCP runner did not reach RouterOS") + const { rows: holders } = await pool.query(`SELECT l.pid FROM pg_locks l JOIN pg_stat_activity a USING(pid) + WHERE l.locktype='advisory' AND l.classid=0 AND l.objid=$1::oid AND l.objsubid=1 AND l.mode='ShareLock' AND l.granted + AND a.datname=current_database() AND a.state='idle' AND a.query='SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired'`, [SYSTEM_DATABASE_REQUEST_LOCK]) + assert.equal(holders.length, 1, "Expected only the dedicated runner request lease") + assert.equal((await pool.query("SELECT pg_terminate_backend($1) AS stopped", [holders[0].pid])).rows[0].stopped, true) + await wait(requestClosed, "Loss of the dedicated request lease did not close the native socket") + release() + let terminal + for (let i = 0; i < 100; i++) { + terminal = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + if (terminal && !["pending", "running"].includes(terminal.state)) break + await delay(20) + } + assert.deepEqual(terminal, { state: "unknown", error_code: "MUTATION_OUTCOME_UNKNOWN" }) + assert.equal((await applyPlan(principal, plan.planId, idempotencyKey)).id, operation.id) + assert.equal(nativeCalls, 4, "The second PATCH and replay must never dispatch") + const { rows: audit } = await pool.query("SELECT action FROM mcp_audit WHERE actor_id=$1 AND action LIKE 'configuration.%' ORDER BY id", [actor.id]) + assert.deepEqual(audit.map(row => row.action), ["configuration.intent", "configuration.unconfirmed"]) + console.log("MCP runner request lease lost: socket closed, durable unknown, second PATCH and replay refused") + } finally { + release() + await Promise.allSettled([inFlight, nested, maintenance]) + router.closeAllConnections() + await new Promise(resolve => router.close(() => resolve())) + } +} diff --git a/backend/src/test/mcp-routing-fixture.ts b/backend/src/test/mcp-routing-fixture.ts new file mode 100644 index 00000000..82ad83ce --- /dev/null +++ b/backend/src/test/mcp-routing-fixture.ts @@ -0,0 +1,13 @@ +import { pool } from "../db/index.js" +import { createRoutingSourceContext, readRoutingEndpoints, type RoutingSourceRecord } from "../modules/mcp/routing-observation-source.js" +import { routingObservationColumns } from "../modules/mcp/routing-observations.js" + +/** Synthetic integration fixtures only. Production never backfills provenance. */ +export async function attestRoutingFixture(id: string) { + if (process.env.NODE_ENV !== "test") throw new Error("Synthetic observation attestation is test-only") + const row = (await pool.query(`SELECT ${routingObservationColumns} FROM mcp_routing_observations o WHERE o.id=$1`, [id])).rows[0] + if (!row || row.source !== "fixture") throw new Error("Only an owned synthetic fixture can be attested") + const context = await createRoutingSourceContext(await readRoutingEndpoints([Number(row.srcServerId), Number(row.dstServerId)])) + const record = { ...row, srcServerId: Number(row.srcServerId), dstServerId: Number(row.dstServerId) } as RoutingSourceRecord + await pool.query("UPDATE mcp_routing_observations SET source_proof=$2 WHERE id=$1", [id, context.signRecord(record)]) +} diff --git a/backend/src/test/mcp-routing-sources.ts b/backend/src/test/mcp-routing-sources.ts new file mode 100644 index 00000000..2e0945de --- /dev/null +++ b/backend/src/test/mcp-routing-sources.ts @@ -0,0 +1,138 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { readRoutingObservations } from "../modules/mcp/routing-observations.js" + +export async function verifyRoutingSources(actor: AuthUser) { + let incomplete = false, pingCalls = 0, atReply: (() => Promise) | undefined + const http = createServer(async (req, res) => { + res.setHeader("content-type", "application/json") + if (req.url === "/rest/ipv6/address") { res.end(JSON.stringify(["fe80::2/64", "fd00::2/64"].map(address => ({ address, interface: "gre", disabled: "false" })))); return } + if (req.url === "/rest/tool/ping") { + pingCalls++ + if (atReply) { const work = atReply; atReply = undefined; await work() } + res.end(JSON.stringify([{ sent: incomplete ? "1" : "3", received: incomplete ? "1" : "3", "packet-loss": "0", "avg-rtt": "1ms", "min-rtt": "1ms", "max-rtt": "1ms" }])); return + } + res.statusCode = 404; res.end("{}") + }) + await new Promise(resolve => http.listen(0, "127.0.0.1", resolve)) + const port = (http.address() as { port: number }).port + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES(101,'source-fixture','127.0.0.1',$1,false,'fixture'),(102,'peer-fixture','127.0.0.1',$1,false,'fixture')", [port]) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Measurement sources", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [101, 102], operationIds: ["diagnostics.bandwidth"], allowGlobal: false } }), actor) + const principal = await authenticateMcpToken(access.token) + const history = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Measurement history", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [101, 102], operationIds: ["routing.measurements.read"], allowGlobal: false } }), actor) + const historyPrincipal = await authenticateMcpToken(history.token) + const plan = async () => { + const prepared = await invokeMcpTool(principal, "mm_routing_measurement_plan", { measurement: { srcServerId: 101, dstServerId: 102, srcInterface: "gre", dstInterface: "gre", family: "ipv6" } }) as { planId: string } + await approvePlan(prepared.planId, actor, true) + return prepared.planId + } + const apply = async (id = "") => { + const operation = await applyPlan(principal, id || await plan(), randomUUID()) + for (let n = 0; n < 240; n++) { + const state = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0].state + if (!["pending", "running"].includes(state)) return state as string + await delay(25) + } + throw new Error("Measurement operation still live; do not replay") + } + const read = () => readRoutingObservations([101, 102]) + const count = async () => Number((await pool.query("SELECT count(*) AS n FROM mcp_routing_observations WHERE src_server_id=101")).rows[0].n) + const epoch = async () => (await pool.query("SELECT routing_endpoint_epoch FROM servers WHERE id=102")).rows[0].routing_endpoint_epoch + try { + assert.equal(await apply(), "applied") + let latest = (await read())[0] + assert.equal(latest.sourceVerified, true); assert.equal(latest.contextVerified, true); assert.equal(latest.pingMs, 1) + assert.ok(!JSON.stringify(latest).includes("sourceProof")) + const visible = await invokeMcpTool(historyPrincipal, "mm_routing_measurements_query", { serverId: 102 }) as { observations: { sourceVerified: boolean; recordedAt: string }[] } + assert.equal(visible.observations.length, 1); assert.equal(visible.observations[0].sourceVerified, true) + assert.ok(visible.observations[0].recordedAt) + await assert.rejects(invokeMcpTool(historyPrincipal, "mm_routing_measurements_query", { serverId: 1 })) + const historyFilter = { srcServerId: 101, dstServerId: 102, srcInterface: "gre", dstInterface: "gre", family: "ipv6", table: "main", limit: 1 } + type HistoryPage = { observations: Awaited>; nextCursor: { recordedAt: string; id: string } | null } + const historyPage = (filters: Record = {}) => invokeMcpTool(historyPrincipal, "mm_routing_measurements_query", { ...historyFilter, ...filters }) as Promise + const firstId = latest.id + const tiedId = randomUUID(), olderId = randomUUID(), differentInterfaceId = randomUUID() + await pool.query(`INSERT INTO mcp_routing_observations(id,src_server_id,dst_server_id,src_interface,dst_interface,family,routing_table,context_verified,source,created_at) + SELECT fixture.id,src_server_id,dst_server_id,src_interface,fixture.iface,family,routing_table,false,'fixture',created_at-fixture.lag*interval '1 microsecond' + FROM mcp_routing_observations CROSS JOIN (VALUES ($2::uuid,'gre',0),($3::uuid,'gre',1),($4::uuid,'other',0)) AS fixture(id,iface,lag) + WHERE mcp_routing_observations.id=$1`, [firstId, tiedId, olderId, differentInterfaceId]) + try { + const firstPage = await historyPage() + assert.ok(firstPage.nextCursor) + const secondPage = await historyPage({ cursor: firstPage.nextCursor }) + assert.ok(secondPage.nextCursor) + const thirdPage = await historyPage({ cursor: secondPage.nextCursor }) + assert.equal(firstPage.nextCursor.recordedAt, secondPage.nextCursor.recordedAt) + assert.deepEqual([firstPage.observations[0].id, secondPage.observations[0].id], [firstId, tiedId].sort().reverse()) + assert.equal(thirdPage.observations[0].id, olderId) + assert.equal(thirdPage.nextCursor, null) + assert.equal((await historyPage({ dstInterface: "other" })).observations[0].id, differentInterfaceId) + assert.deepEqual((await historyPage({ srcServerId: 102, dstServerId: 101 })).observations, []) + assert.deepEqual((await historyPage({ table: "other" })).observations, []) + await assert.rejects(historyPage({ dstServerId: 1 })) + await assert.rejects(historyPage({ limit: 101 })) + console.log("✓ Routing measurement history: exact directional filters, scoped endpoints, stable microsecond/UUID pagination and bounded result") + } finally { + await pool.query("DELETE FROM mcp_routing_observations WHERE id=ANY($1::uuid[])", [[tiedId, olderId, differentInterfaceId]]) + } + const firstEpoch = await epoch() + await pool.query("UPDATE servers SET name='renamed',routing_endpoint_epoch=$1 WHERE id=102", [randomUUID()]) + assert.equal(await epoch(), firstEpoch); assert.equal((await read())[0].sourceVerified, true) + for (const [field, value] of [["host", "192.0.2.102"], ["port", port + 1], ["username", "changed"], ["use_ssl", true], ["verify_ssl", false], ["enabled", false]] as const) { + const saved = (await pool.query(`SELECT ${field} AS value FROM servers WHERE id=102`)).rows[0].value + const beforeEpoch = await epoch() + await pool.query(`UPDATE servers SET ${field}=$1 WHERE id=102`, [typeof saved === "boolean" ? !saved : value]) + assert.notEqual(await epoch(), beforeEpoch) + await pool.query(`UPDATE servers SET ${field}=$1 WHERE id=102`, [saved]) + assert.notEqual(await epoch(), beforeEpoch) + } + await pool.query("UPDATE servers SET password='changed' WHERE id=102") + assert.notEqual(await epoch(), firstEpoch); assert.equal((await read())[0].contextVerified, false) + await pool.query("UPDATE servers SET password='fixture' WHERE id=102") + assert.notEqual(await epoch(), firstEpoch); assert.equal((await read())[0].sourceVerified, false, "A→B→A cannot revive an old measurement") + assert.equal(await apply(), "applied") + latest = (await read())[0] + await pool.query("UPDATE mcp_routing_observations SET created_at=created_at+interval '1 microsecond' WHERE id=$1", [latest.id]) + assert.equal((await read())[0].sourceVerified, false) + assert.equal(await apply(), "applied") + latest = (await read())[0] + await pool.query("UPDATE mcp_routing_observations SET ping_ms=0 WHERE id=$1", [latest.id]) + assert.equal((await read())[0].sourceVerified, false) + assert.equal(await apply(), "applied") + incomplete = true + assert.equal(await apply(), "unknown") + latest = (await read())[0] + assert.equal(latest.sourceVerified, true); assert.equal(latest.contextVerified, false); assert.equal(latest.pingMs, null) + incomplete = false + const stale = await plan(), before = pingCalls + await pool.query("UPDATE servers SET password='late-change' WHERE id=102") + assert.equal(await apply(stale), "rejected"); assert.equal(pingCalls, before) + await pool.query("UPDATE servers SET password='fixture' WHERE id=102") + const rowsBefore = await count() + atReply = () => pool.query("UPDATE servers SET password='during-ping' WHERE id=102") + assert.equal(await apply(), "unknown"); assert.equal(await count(), rowsBefore) + await pool.query("UPDATE servers SET password='fixture' WHERE id=102") + for (const tamper of [false, true]) { + await pool.query(`CREATE OR REPLACE FUNCTION mcp_fixture_measurement_write() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.src_server_id=101 THEN ${tamper ? "NEW.ping_ms:=99;" : "RETURN NULL;"} END IF; RETURN NEW; END $$; CREATE TRIGGER mcp_fixture_measurement_write BEFORE INSERT ON mcp_routing_observations FOR EACH ROW EXECUTE FUNCTION mcp_fixture_measurement_write()`) + try { assert.equal(await apply(), "partial"); assert.equal(await count(), rowsBefore) } + finally { await pool.query("DROP TRIGGER mcp_fixture_measurement_write ON mcp_routing_observations; DROP FUNCTION mcp_fixture_measurement_write()") } + } + atReply = () => setConnectionStatus(access.connection.id, "paused", actor) + assert.equal(await apply(), "unknown"); assert.equal(await count(), rowsBefore) + console.log("✓ Measurement sources SQL/MCP/HTTP: both endpoints, ABA epoch, metadata, tampered values/microseconds, incomplete latest, stale plan, mid-ping drift/revocation, ignored/tampered publication rollback") + } finally { + atReply = undefined + await setConnectionStatus(access.connection.id, "paused", actor) + await setConnectionStatus(history.connection.id, "paused", actor) + await pool.query("DELETE FROM mcp_routing_observations WHERE src_server_id=101") + await pool.query("DELETE FROM servers WHERE id=ANY($1::bigint[])", [[101, 102]]) + await new Promise(resolve => http.close(() => resolve())) + } +} diff --git a/backend/src/test/mcp-server-connection.ts b/backend/src/test/mcp-server-connection.ts new file mode 100644 index 00000000..cc98f47f --- /dev/null +++ b/backend/src/test/mcp-server-connection.ts @@ -0,0 +1,85 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { encryptSecret } from "../modules/mcp/security.js" +import { serverConnectionSecretPurpose } from "../modules/mcp/server-connection-actions.js" +import serversRoutes from "../routes/servers.js" + +/** Root integration must registerServerConnectionActions first. Disposable SQL + * and local HTTP only; never connect to a saved server or external destination. */ +export async function verifyMcpServerConnection(actor: AuthUser) { + const owner={...actor,id:"server-connection-test-owner",permissions:[...actor.permissions,"mm:servers:write"]} + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id")).rows.map(row=>row.id) + const secret="NEVER_SERVER_CONNECTION_PASSWORD",reference=randomUUID(),wrong=randomUUID(),other=randomUUID(),scoped=randomUUID(),protection=randomUUID(),grantId=randomUUID() + let calls=0,mode="ok",late: (()=>Promise)|undefined + const paths:string[]=[],hosts:string[]=[],authorizations:string[]=[] + const server=createServer(async(request,response)=>{ + calls++;paths.push(request.url??"");hosts.push(request.headers.host??"");authorizations.push(request.headers.authorization??"") + assert.equal(request.method,"GET");response.setHeader("content-type","application/json") + if(late){const hook=late;late=undefined;await hook()} + if(mode==="lost"){request.socket.destroy();return} + if(mode==="redirect"){response.statusCode=302;response.setHeader("location","http://169.254.169.254/");response.end("{}");return} + if(mode==="denied"){response.statusCode=401;response.end(secret);return} + if(mode==="malformed"){response.end(`{${secret}`);return} + if(mode==="oversized"){response.end(" ".repeat(65537));return} + response.end(JSON.stringify(request.url==="/rest/system/identity"?{name:"fixture-"+secret}:{version:"7.22","board-name":"fixture",uptime:"1h"})) + }) + await new Promise(resolve=>server.listen(0,"127.0.0.1",resolve)) + const endpoint={host:"localhost",port:(server.address()as{port:number}).port,useSsl:false,verifySsl:false,apiPath:"/rest",username:"admin"} + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Server connection test",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:servers:write"],scope:{allowGlobal:true,serverIds:ids,operationIds:["servers.test-connection"]},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + const principal=await authenticateMcpToken(issued.token) + const rest=Fastify();rest.setValidatorCompiler(validatorCompiler);rest.setSerializerCompiler(serializerCompiler);await rest.register(serversRoutes,{prefix:"/api/servers"}) + const prepare=(extra:Record={})=>invokeMcpTool(principal,"mm_server_test_connection_plan",{...endpoint,passwordRef:reference,...extra})as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{for(let i=0;i<600;i++){const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[id])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("Connection test still live; do not replay")} + const apply=async()=>{const plan=await prepare();assert.equal(plan.state,"awaiting-approval");assert.ok(!JSON.stringify(plan).includes(secret));await approvePlan(plan.planId,owner,true);const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key);return{plan,key,operation,...await wait(operation.id)}} + try{ + const purpose=serverConnectionSecretPurpose(endpoint) + for(const[id,p,creator,serverId]of[[reference,purpose,owner.id,null],[wrong,"server-login:localhost:"+endpoint.port,owner.id,null],[other,purpose,"different-owner",null],[scoped,purpose,owner.id,ids[0]??null]]as const)await pool.query("INSERT INTO mcp_secrets(id,purpose,created_by,server_id,ciphertext) VALUES($1,$2,$3,$4,$5)",[id,p,creator,serverId,await encryptSecret(secret)]) + for(const extra of[{password:secret},{passwordRef:wrong},{passwordRef:other},{host:"169.254.169.254"},{apiPath:"/else"},{username:"other"},{verifySsl:true}])await assert.rejects(prepare(extra)) + if(ids.length)await assert.rejects(prepare({passwordRef:scoped})) + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)",[grantId,issued.connection.id,JSON.stringify({id:grantId,credentialId:principal.credentialId,startsAt:new Date(Date.now()-1000).toISOString(),expiresAt:new Date(Date.now()+60000).toISOString(),scope:{serverIds:ids,operationIds:["servers.test-connection"],allowGlobal:false}})]) + await assert.rejects(prepare());assert.equal(calls,0) + await pool.query("DELETE FROM mcp_grants WHERE id=$1",[grantId]) + const cancelled=new AbortController();cancelled.abort();await assert.rejects(invokeMcpTool(principal,"mm_server_test_connection_plan",{...endpoint,passwordRef:reference},cancelled.signal)) + assert.equal(calls,0) + const unapproved=await prepare();await assert.rejects(applyPlan(principal,unapproved.planId,randomUUID()));assert.equal(calls,0) + let result=await apply();assert.equal(result.state,"applied");assert.equal(calls,2) + assert.deepEqual(paths.slice(0,2).sort(),["/rest/system/identity","/rest/system/resource"]) + assert.ok(hosts.every(value=>value===`localhost:${endpoint.port}`));assert.ok(authorizations.every(value=>value==="Basic "+Buffer.from(`admin:${secret}`).toString("base64"))) + await applyPlan(principal,result.plan.planId,result.key);assert.equal(calls,2) + const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.operation.id])).rows[0].result + assert.equal(receipt.identity,"fixture-[redacted]");assert.equal(receipt.configurationChanged,false);assert.equal(receipt.forwardingVerified,false) + const restResponse=await rest.inject({method:"POST",url:"/api/servers/test-connection",payload:{...endpoint,password:secret}}) + assert.equal(restResponse.statusCode,200);const restBody=restResponse.json();for(const key of["success","identity","version","boardName","uptime","message"])assert.equal(receipt[key],restBody[key]) + assert.equal(calls,4) + for(const fault of["denied","redirect","lost","malformed","oversized"]){mode=fault;const before: number=calls;result=await apply();assert.equal(result.state,["denied","redirect"].includes(fault)?"partial":"unknown");assert.equal(calls,before+2);await applyPlan(principal,result.plan.planId,result.key);assert.equal(calls,before+2)} + mode="ok" + const stale=await prepare();await approvePlan(stale.planId,owner,true);await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1",[reference,await encryptSecret(secret)]) + const beforeStale: number=calls;assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected");assert.equal(calls,beforeStale) + const pending=await prepare();await approvePlan(pending.planId,owner,true) + late=async()=>{await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1",[reference,await encryptSecret("ROTATED_NEVER_PASSWORD")])} + assert.equal((await wait((await applyPlan(principal,pending.planId,randomUUID())).id)).state,"unknown") + await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1",[reference,await encryptSecret(secret)]) + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"server-connection-test",id:"global",protection:"deny"}),owner.id]) + const beforeProtection: number=calls;await assert.rejects(prepare());assert.equal(calls,beforeProtection) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + const paused=await prepare();await approvePlan(paused.planId,owner,true);await setConnectionStatus(issued.connection.id,"paused",owner) + await assert.rejects(prepare());await assert.rejects(applyPlan(principal,paused.planId,randomUUID()));assert.equal(calls,beforeProtection) + const persisted=await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[issued.connection.id]);assert.ok(!JSON.stringify(persisted.rows).includes(secret));assert.ok(!JSON.stringify(persisted.rows).includes("ROTATED_NEVER_PASSWORD")) + console.log("✓ Server connection SQL/HTTP: human approval, full destination-bound owner secret reference, original Host with DNS pinning, shared REST parity, 2 bounded GETs, reflected secret redacted, HTTP auth/redirect rejection, transport/malformed/oversized unknown, stale/late source fences, protection/revocation and no replay; no real RouterOS") + }finally{ + await setConnectionStatus(issued.connection.id,"paused",owner);await rest.close();await new Promise(resolve=>server.close(()=>resolve())) + await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])",[[reference,wrong,other,scoped]]);await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + await pool.query("DELETE FROM mcp_grants WHERE id=$1",[grantId]) + } +} + + diff --git a/backend/src/test/mcp-sidebar-counts.ts b/backend/src/test/mcp-sidebar-counts.ts new file mode 100644 index 00000000..3ee6e10d --- /dev/null +++ b/backend/src/test/mcp-sidebar-counts.ts @@ -0,0 +1,46 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import { hasPermission, type AuthUser } from "../lib/permissions.js" +import { defaultSidebarCountsDependencies } from "../services/sidebar-counts.js" +import { sidebarCountsPermissions } from "../modules/mcp/sidebar-counts-actions.js" +import { authenticateMcpToken, createMcpConnection } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpSidebarCounts(actor: AuthUser, nativeReads: () => number) { + const owner = { ...actor, permissions: [...new Set([...actor.permissions, ...sidebarCountsPermissions])] } + const issue = async (global = true, permissions: readonly string[] = sidebarCountsPermissions) => { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Sidebar counts fixture", mode: "observer", permissions, expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: [], operationIds: ["system.sidebar.counts.read"], allowGlobal: global } }), owner) + return authenticateMcpToken(access.token) + } + const principal = await issue() + const writesBefore = Number((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action LIKE 'configuration.%'")).rows[0].n) + const result = await invokeMcpTool(principal, "mm_sidebar_counts_get", {}) as { counts: Record; unknownCounters: string[]; incomplete: boolean; atomicSnapshot: boolean; scope: string } + const core = await defaultSidebarCountsDependencies.core() as Record + for (const [key, value] of Object.entries(core)) assert.equal(result.counts[key], value) + for (const [key, value] of Object.entries(result.counts)) { + assert.ok(value === null || Number.isSafeInteger(value) && value >= 0) + assert.equal(result.unknownCounters.includes(key), value === null) + } + assert.equal(result.incomplete, result.unknownCounters.length > 0); assert.equal(result.atomicSnapshot, false); assert.equal(result.scope, "global-all-servers") + const deniedReads = nativeReads() + await assert.rejects(invokeMcpTool(await issue(false), "mm_sidebar_counts_get", {})); assert.equal(nativeReads(), deniedReads) + for (const permission of sidebarCountsPermissions) { + const granted = sidebarCountsPermissions.filter(item => item !== permission), scoped = await issue(true, granted) + if (hasPermission(granted, permission)) assert.ok(await invokeMcpTool(scoped, "mm_sidebar_counts_get", {}), "Existing section-admin implications remain valid") + else { const before = nativeReads(); await assert.rejects(invokeMcpTool(scoped, "mm_sidebar_counts_get", {})); assert.equal(nativeReads(), before) } + } + await assert.rejects(invokeMcpTool(principal, "mm_sidebar_counts_get", { serverId: 1 })) + const ids = Array.from({ length: 101 }, (_, index) => 30000 + index), marker = "sidebar-limit-" + randomUUID() + assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=ANY($1::int[])", [ids])).rows[0].n, 0) + try { + await pool.query("INSERT INTO servers(id,name,host,enabled,username,password) SELECT n,$1,'127.0.0.1',false,'fixture','NEVER_SIDEBAR_LOGIN' FROM unnest($2::int[]) AS n", [marker, ids]) + const before = nativeReads() + await assert.rejects(invokeMcpTool(principal, "mm_sidebar_counts_get", {}), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED") + assert.equal(nativeReads(), before) + } finally { await pool.query("DELETE FROM servers WHERE id=ANY($1::int[]) AND name=$2", [ids, marker]) } + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action LIKE 'configuration.%'")).rows[0].n), writesBefore) + assert.ok(!JSON.stringify(result).includes("NEVER_")) + console.log("✓ Sidebar counts: real SQL core parity without user/binding hydration, all-source permission conjunction and global scope, null versus zero, bounded global catalog, strict args and no configuration writes") +} diff --git a/backend/src/test/mcp-telegram-settings.ts b/backend/src/test/mcp-telegram-settings.ts new file mode 100644 index 00000000..434f568b --- /dev/null +++ b/backend/src/test/mcp-telegram-settings.ts @@ -0,0 +1,56 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan } from "../modules/mcp/runtime.js" +import { encryptSecret } from "../modules/mcp/security.js" +import { TELEGRAM_SECRET_PURPOSE } from "../modules/mcp/telegram-settings-actions.js" +import alertsRoutes from "../routes/alerts.js" + +export async function verifyMcpTelegramSettings(actor:AuthUser){ + const owner={...actor,id:"telegram-settings-owner",permissions:[...actor.permissions,"mm:alerts:write"]} + const saved=(await pool.query("SELECT * FROM alert_telegram_settings WHERE id=1")).rows[0] + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id")).rows.map(r=>r.id) + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Telegram settings",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:alerts:write"],scope:{allowGlobal:true,serverIds:ids,operationIds:["alerts.telegram.update"]},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + const principal=await authenticateMcpToken(issued.token),secretRef=randomUUID(),wrongRef=randomUUID(),otherRef=randomUUID(),protection=randomUUID() + const secret="123456:NEVER_TELEGRAM_SETTINGS_SECRET",initial="654321:NEVER_TELEGRAM_INITIAL" + const rest=Fastify();await rest.register(alertsRoutes,{prefix:"/api"}) + const read=async()=>(await pool.query("SELECT bot_token,chat_id,message_thread_id FROM alert_telegram_settings WHERE id=1")).rows[0] + const prepare=(input:Record)=>invokeMcpTool(principal,"mm_alerts_telegram_update_plan",input) as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{for(let i=0;i<600;i++){const row=(await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1",[id])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("Telegram update still live; never replay")} + const apply=async(input:Record)=>{const plan=await prepare(input);assert.equal(plan.state,"awaiting-approval");assert.ok(!JSON.stringify(plan).includes(secret));await approvePlan(plan.planId,owner,true);const key=randomUUID(),operation=await applyPlan(principal,plan.planId,key);return{plan,key,operation,...await wait(operation.id)}} + try{ + await pool.query("INSERT INTO alert_telegram_settings(id,bot_token,chat_id,message_thread_id) VALUES(1,$1,'old-chat',42) ON CONFLICT(id) DO UPDATE SET bot_token=$1,chat_id='old-chat',message_thread_id=42",[initial]) + for(const [id,purpose,creator] of [[secretRef,TELEGRAM_SECRET_PURPOSE,owner.id],[wrongRef,"wrong-purpose",owner.id],[otherRef,TELEGRAM_SECRET_PURPOSE,"other-owner"]])await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,NULL,$3,$4)",[id,purpose,await encryptSecret(secret),creator]) + for(const input of [{},{settings:{}},{settings:{token:secret}},{token:secret},{secretRef:wrongRef},{secretRef:otherRef},{settings:{messageThreadId:0}}])await assert.rejects(prepare(input)) + const partial=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Partial Telegram",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:alerts:write"],scope:{allowGlobal:true,serverIds:ids.slice(0,-1),operationIds:["alerts.telegram.update"]}}),owner) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(partial.token),"mm_alerts_telegram_update_plan",{settings:{chatId:"new"}}));await setConnectionStatus(partial.connection.id,"paused",owner) + let result=await apply({settings:{chatId:" new-chat "}});assert.equal(result.state,"applied");assert.deepEqual(await read(),{bot_token:initial,chat_id:"new-chat",message_thread_id:42}) + await applyPlan(principal,result.plan.planId,result.key);assert.deepEqual(await read(),{bot_token:initial,chat_id:"new-chat",message_thread_id:42}) + result=await apply({secretRef,settings:{messageThreadId:null}});assert.equal(result.state,"applied");assert.deepEqual(await read(),{bot_token:secret,chat_id:"new-chat",message_thread_id:null}) + result=await apply({secretRef:null});assert.equal(result.state,"applied");assert.equal((await read()).bot_token,"") + const response=await rest.inject({method:"PUT",url:"/api/alerts/telegram",payload:{chatId:"rest-chat",messageThreadId:7}});assert.equal(response.statusCode,200,response.body);assert.equal((await read()).chat_id,"rest-chat") + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"alerts-telegram",id:"1",protection:"deny"}),owner.id]) + await assert.rejects(prepare({settings:{chatId:"blocked"}})) + assert.equal((await rest.inject({method:"PUT",url:"/api/alerts/telegram",payload:{chatId:"blocked"}})).statusCode,409) + assert.equal((await read()).chat_id,"rest-chat");await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + const stale=await prepare({secretRef});await approvePlan(stale.planId,owner,true) + await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1",[secretRef,await encryptSecret("NEVER_CHANGED_TOKEN")]) + assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected") + await pool.query("UPDATE mcp_secrets SET ciphertext=$2 WHERE id=$1",[secretRef,await encryptSecret(secret)]) + const configStale=await prepare({settings:{chatId:"stale"}});await approvePlan(configStale.planId,owner,true) + await pool.query("UPDATE alert_telegram_settings SET updated_at=updated_at+interval '1 second' WHERE id=1") + assert.equal((await wait((await applyPlan(principal,configStale.planId,randomUUID())).id)).state,"rejected") + const cancelled=new AbortController();cancelled.abort();await assert.rejects(invokeMcpTool(principal,"mm_alerts_telegram_update_plan",{settings:{chatId:"cancelled"}},cancelled.signal)) + const audit=await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[issued.connection.id]);assert.ok(!JSON.stringify(audit.rows).includes(secret));assert.ok(!JSON.stringify(audit.rows).includes(initial)) + await setConnectionStatus(issued.connection.id,"paused",owner);await assert.rejects(prepare({settings:{chatId:"paused"}})) + console.log("✓ Telegram settings: shared REST/MCP selective update, human approval and global scope, owner/purpose secret references, clear/preserve/thread, stale token/source, REST protections, cancellation/pause, idempotency and no token in preview/audit; no messages sent") + }finally{ + await setConnectionStatus(issued.connection.id,"paused",owner);await rest.close();await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await pool.query("DELETE FROM mcp_secrets WHERE id=ANY($1::uuid[])",[[secretRef,wrongRef,otherRef]]) + if(saved)await pool.query("UPDATE alert_telegram_settings SET bot_token=$1,chat_id=$2,message_thread_id=$3,updated_at=$4 WHERE id=1",[saved.bot_token,saved.chat_id,saved.message_thread_id,saved.updated_at]);else await pool.query("DELETE FROM alert_telegram_settings WHERE id=1") + } +} diff --git a/backend/src/test/mcp-telegram-test.ts b/backend/src/test/mcp-telegram-test.ts new file mode 100644 index 00000000..42636ac8 --- /dev/null +++ b/backend/src/test/mcp-telegram-test.ts @@ -0,0 +1,90 @@ +import assert from "node:assert/strict" +import { afterConfigurationIntentCommit } from "./fixtures/configuration-audit-commit.js" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan, applyPlan, loadPlan } from "../modules/mcp/runtime.js" +import { encryptSecret, decryptSecret } from "../modules/mcp/security.js" +import { findMcpService } from "../modules/mcp/service-registry.js" +import { TELEGRAM_SECRET_PURPOSE } from "../modules/mcp/telegram-settings-actions.js" +import alertsRoutes from "../routes/alerts.js" + +export async function verifyMcpTelegramTest(actor:AuthUser){ + const owner={...actor,id:"telegram-test-owner",permissions:[...actor.permissions,"mm:alerts:write"]} + const saved=(await pool.query("SELECT * FROM alert_telegram_settings WHERE id=1")).rows[0] + const ids=(await pool.query<{id:number}>("SELECT id FROM servers ORDER BY id")).rows.map(r=>r.id) + const issued=await createMcpConnection(mcpConnectionCreateSchema.parse({name:"Telegram test",mode:"approval",expiresAt:new Date(Date.now()+1800000).toISOString(),permissions:["mm:alerts:write"],scope:{allowGlobal:true,serverIds:ids,operationIds:["alerts.telegram.test"]},limits:{callsPerMinute:120,mutationsPerHour:100}}),owner) + const principal=await authenticateMcpToken(issued.token),secretRef=randomUUID(),protection=randomUUID() + const token="123456:NEVER_TELEGRAM_TEST_TOKEN",override="654321:NEVER_TELEGRAM_OVERRIDE" + const originalFetch=globalThis.fetch + let mode="normal",calls=0 + const payloads:Record[]=[],urls:string[]=[] + const server=createServer((req,res)=>{ + let body="";req.on("data",chunk=>{body+=chunk});req.on("end",()=>{ + calls++;payloads.push(JSON.parse(body));res.setHeader("content-type","application/json") + if(mode==="lost"){req.socket.destroy();return} + if(mode==="rejected"){res.statusCode=429;res.end(JSON.stringify({ok:false,error_code:429,description:token}));return} + if(mode==="malformed"){res.end("{}");return} + if(mode==="oversized"){res.end('{'+" ".repeat(65537)+'"ok":true}');return} + res.end('{"ok":true}') + }) + }) + await new Promise(resolve=>server.listen(0,"127.0.0.1",resolve)) + const local=`http://127.0.0.1:${(server.address()as{port:number}).port}/send` + globalThis.fetch=(async(url,init)=>{ + const target=String(url);if(!target.startsWith("https://api.telegram.org/bot")||!target.endsWith("/sendMessage"))throw Error("Unexpected network request in isolated Telegram fixture") + urls.push(target);assert.equal(init?.redirect,"error");return originalFetch(local,init) + }) as typeof fetch + const rest=Fastify();await rest.register(alertsRoutes,{prefix:"/api"}) + const prepare=(input:Record={})=>invokeMcpTool(principal,"mm_alerts_telegram_test_plan",input) as Promise<{planId:string;state:string}> + const wait=async(id:string)=>{for(let i=0;i<600;i++){const row=(await pool.query("SELECT state,error_code,verification FROM mcp_operations WHERE id=$1",[id])).rows[0];if(!["pending","running"].includes(row.state))return row;await delay(25)}throw Error("Telegram test still live; do not replay")} + const apply=async(input:Record={})=>{const plan=await prepare(input);assert.equal(plan.state,"awaiting-approval");assert.ok(!JSON.stringify(plan).includes(token));assert.ok(!JSON.stringify(plan).includes(override));await approvePlan(plan.planId,owner,true);const key=randomUUID(),op=await applyPlan(principal,plan.planId,key);return {plan,key,op,...await wait(op.id)}} + try{ + await pool.query("INSERT INTO alert_telegram_settings(id,bot_token,chat_id,message_thread_id) VALUES(1,$1,'fixture-chat',42) ON CONFLICT(id) DO UPDATE SET bot_token=$1,chat_id='fixture-chat',message_thread_id=42",[token]) + await pool.query("INSERT INTO mcp_secrets(id,purpose,server_id,ciphertext,created_by) VALUES($1,$2,NULL,$3,$4)",[secretRef,TELEGRAM_SECRET_PURPOSE,await encryptSecret(override),owner.id]) + const unapproved=await prepare();await assert.rejects(applyPlan(principal,unapproved.planId,randomUUID()));assert.equal(calls,0) + const sealed=(await loadPlan(unapproved.planId)).evidence.sealed + const input=JSON.parse(await decryptSecret(sealed)).steps[0].input + await assert.rejects(findMcpService("alerts.telegram.test")!.execute({...input,messagePreview:"different text"}),/preview/) + await assert.rejects(findMcpService("alerts.telegram.test")!.execute({...input,destination:{chatId:"different",messageThreadId:42}}),/preview/) + assert.equal(calls,0) + console.log("✓ Telegram preview fence: text/recipient mismatch refuses before HTTP dispatch") + const preview={name:"Fixture alert",targets:["fixture-router"],conditionLine:"loss > 0",severity:"warning",cooldown:"5м"} + let result=await apply({secretRef,chatId:"override-chat",messageThreadId:9,rulePreview:preview}) + assert.equal(result.state,"applied");assert.equal(result.verification,"inconclusive");assert.equal(calls,1);assert.ok(urls[0].includes(encodeURIComponent(override))) + const sent=payloads[0];assert.equal(sent.chat_id,"override-chat");assert.equal(sent.message_thread_id,9) + await applyPlan(principal,result.plan.planId,result.key);assert.equal(calls,1) + const response=await rest.inject({method:"POST",url:"/api/alerts/telegram/test",payload:{token:override,chatId:"override-chat",messageThreadId:9,rulePreview:preview}}) + assert.equal(response.statusCode,200,response.body);assert.deepEqual(payloads[1],sent) + assert.equal((await pool.query("SELECT bot_token FROM alert_telegram_settings WHERE id=1")).rows[0].bot_token,token) + for(const fault of ["rejected","lost","malformed","oversized"]){ + mode=fault;const before:number=calls;result=await apply();assert.equal(calls,before+1);assert.equal(result.state,fault==="rejected"?"partial":"unknown") + if(fault==="rejected"){const receipt=(await pool.query("SELECT result FROM mcp_operation_steps WHERE operation_id=$1",[result.op.id])).rows[0].result;assert.equal(receipt.outcome,"rejected");assert.equal(receipt.providerAccepted,false)} + await applyPlan(principal,result.plan.planId,result.key);assert.equal(calls,before+1) + } + mode="normal" + const stale=await prepare();await approvePlan(stale.planId,owner,true);await pool.query("UPDATE alert_telegram_settings SET chat_id='changed' WHERE id=1") + const beforeStale=calls;assert.equal((await wait((await applyPlan(principal,stale.planId,randomUUID())).id)).state,"rejected");assert.equal(calls,beforeStale) + await pool.query("UPDATE alert_telegram_settings SET chat_id='fixture-chat' WHERE id=1") + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"alerts-telegram-test",id:"1",protection:"deny"}),owner.id]) + await assert.rejects(prepare());assert.equal((await rest.inject({method:"POST",url:"/api/alerts/telegram/test",payload:{}})).statusCode,409);assert.equal(calls,beforeStale) + await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + for(const input of [{token:token},{settings:{}},{messageThreadId:0},{rulePreview:{...preview,name:"x".repeat(201)}}])await assert.rejects(prepare(input)) + const cancelled=new AbortController();cancelled.abort();await assert.rejects(invokeMcpTool(principal,"mm_alerts_telegram_test_plan",{},cancelled.signal)) + const late=await prepare();await approvePlan(late.planId,owner,true) + const drift=afterConfigurationIntentCommit("alerts-telegram-test",()=>pool.query("UPDATE alert_telegram_settings SET chat_id='late-drift' WHERE id=1")) + try{const before=calls;assert.equal((await wait((await applyPlan(principal,late.planId,randomUUID())).id)).state,"unknown");assert(drift.fired());assert.equal(calls,before)}finally{drift.restore()} + const audit=await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1",[issued.connection.id]);assert.ok(!JSON.stringify(audit.rows).includes(token));assert.ok(!JSON.stringify(audit.rows).includes(override)) + await setConnectionStatus(issued.connection.id,"paused",owner);await assert.rejects(prepare()) + console.log("✓ Telegram test plan: human-only, shared REST payload, exact one local HTTP POST, stored/reference overrides, accepted acknowledgement not delivery, explicit provider rejection partial, transport/malformed/oversized unknown, no replay, stale/late source and REST protections; no real Telegram network") + }finally{ + globalThis.fetch=originalFetch;await rest.close();await new Promise(resolve=>server.close(()=>resolve())) + await setConnectionStatus(issued.connection.id,"paused",owner);await pool.query("DELETE FROM mcp_secrets WHERE id=$1",[secretRef]);await pool.query("DELETE FROM mcp_protections WHERE id=$1",[protection]) + if(saved)await pool.query("UPDATE alert_telegram_settings SET bot_token=$1,chat_id=$2,message_thread_id=$3,updated_at=$4 WHERE id=1",[saved.bot_token,saved.chat_id,saved.message_thread_id,saved.updated_at]);else await pool.query("DELETE FROM alert_telegram_settings WHERE id=1") + } +} diff --git a/backend/src/test/mcp-terminal-diagnostics.ts b/backend/src/test/mcp-terminal-diagnostics.ts new file mode 100644 index 00000000..1b74c3ff --- /dev/null +++ b/backend/src/test/mcp-terminal-diagnostics.ts @@ -0,0 +1,116 @@ +import assert from "node:assert/strict" +import type { FastifyInstance } from "fastify" +import { SignJWT } from "jose" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { env } from "../config.js" +import { pool } from "../db/index.js" +import type { AuthUser } from "../lib/permissions.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" +import { findActionByOperation } from "../modules/mcp/actions.js" +import { parseTerminalCommand, terminalDiagnosticSchema } from "../services/terminal-diagnostics.js" +import { TERMINAL_DIAGNOSTIC_MAX_OUTPUT_BYTES } from "../modules/mcp/terminal-diagnostic-actions.js" +import { createTerminalDiagnosticFixture, terminalDiagnosticCases } from "./terminal-diagnostic-fixture.js" + +/** Real SQL + owned loopback RouterOS HTTP only; root's disposable PG runner invokes this. */ +export async function verifyMcpTerminalDiagnostics(actor: AuthUser, app: FastifyInstance) { + const fixture = await createTerminalDiagnosticFixture(), owner = { ...actor, id: "terminal-diagnostics-owner", permissions: [...new Set([...actor.permissions, "mm:servers:write"])] } + // Base acceptance fixtures intentionally insert id=1 without advancing the + // sequence. Use a reserved, checked ID instead of consuming that sequence. + const password = "NEVER_MCP_TERMINAL_PASSWORD", sourceId = 135 + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM servers WHERE id=$1", [sourceId])).rows[0].n), 0, "Terminal fixture ID must be unused") + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES($1,'terminal-fixture','127.0.0.1',$2,false,false,true,'terminal-fixture',$3)", [sourceId, fixture.port, password]) + const issue = (ids = [sourceId], permissions = ["mm:servers:write"]) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Typed REST terminal", mode: "autonomous", permissions: [...new Set(permissions)], scope: { serverIds: [...new Set(ids)], operationIds: ["servers.terminal.read"], allowGlobal: false }, expiresAt: new Date(Date.now() + 1800000).toISOString(), limits: { callsPerMinute: 240, mutationsPerHour: 100 } }), { ...owner, permissions: [...new Set([...owner.permissions, ...permissions])] }) + const issued = await issue(), principal = await authenticateMcpToken(issued.token) + const jwt = await new SignJWT({ sub: owner.id, apps: ["mm"], permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret)) + const headers = { authorization: "Bearer " + jwt }, originalQuery = pool.query.bind(pool) + const invoke = (args: Record) => invokeMcpTool(principal, "mm_server_terminal_diagnostic", { serverId: sourceId, ...args }) as Promise<{ output: string; truncated: boolean; redactionTruncated: boolean; returnedBytes: number; activity: Record }> + const restoreQuery = () => { pool.query = originalQuery as typeof pool.query } + try { + const planCount = Number((await pool.query("SELECT count(*) AS n FROM mcp_plans WHERE connection_id=$1", [issued.connection.id])).rows[0].n) + const action = findActionByOperation("servers.terminal.read") + assert.equal(action.operation.effect, "read"); assert.deepEqual(action.operation.requiredPermissions, ["mm:servers:write"]) + let secretReads = 0 + pool.query = ((...args: unknown[]) => { const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text; if (sql?.includes("AS endpoint")) secretReads++; return (originalQuery as (...args: unknown[]) => unknown)(...args) }) as typeof pool.query + try { + const before = fixture.requests.length + assert.ok((await invoke({ diagnostic: "help" })).output.includes("Доступные команды")) + assert.equal(fixture.requests.length, before); assert.equal(secretReads, 0, "Help resolves existing enabled server but never reads credentials/native endpoint") + } finally { restoreQuery() } + for (const [raw] of terminalDiagnosticCases) { + const parsed = parseTerminalCommand(raw), result = await invoke(parsed) + const rest = await app.inject({ method: "POST", url: `/api/servers/${sourceId}/exec`, headers, payload: { command: raw } }) + assert.equal(rest.statusCode, 200, rest.body) + assert.equal(result.output, rest.json().output, raw); assert.equal(result.truncated, false) + assert.ok(result.returnedBytes <= TERMINAL_DIAGNOSTIC_MAX_OUTPUT_BYTES) + assert.equal(result.activity.kind, parsed.diagnostic === "ping" ? "ping" : "read") + if (parsed.diagnostic === "ping") assert.equal(result.activity.count, 20) + } + assert.equal(Number((await pool.query("SELECT count(*) AS n FROM mcp_plans WHERE connection_id=$1", [issued.connection.id])).rows[0].n), planCount, "Readonly diagnostics must never generate plans") + const beforeDenied = fixture.requests.length + for (const bad of [{ diagnostic: "execute", command: "/system reboot" }, { diagnostic: "interfaces", command: "interface print" }, { diagnostic: "ping", target: "192.0.2.1", count: 21 }, { diagnostic: "logs", limit: 201 }]) await assert.rejects(invoke(bad)) + assert.equal(fixture.requests.length, beforeDenied) + const narrowed = await issue([]), readOnly = await issue([sourceId], ["mm:servers:read"]) + try { + for (const token of [narrowed.token, readOnly.token]) await assert.rejects(invokeMcpTool(await authenticateMcpToken(token), "mm_server_terminal_diagnostic", { serverId: sourceId, diagnostic: "interfaces" })) + assert.equal(fixture.requests.length, beforeDenied) + } finally { await setConnectionStatus(narrowed.connection.id, "paused", owner); await setConnectionStatus(readOnly.connection.id, "paused", owner) } + await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [sourceId]) + for (const diagnostic of ["help", "interfaces"]) await assert.rejects(invoke({ diagnostic })) + assert.equal(fixture.requests.length, beforeDenied) + await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [sourceId]) + const interfaces = fixture.payloads["/interface"], logs = fixture.payloads["/log"], resource = fixture.payloads["/system/resource"] + fixture.payloads["/interface"] = [{ ".id": "*1", name: "private-fixture", comment: `password=${password} token=NEVER_LOG_TOKEN`, type: "ether" }] + const redacted = await invoke({ diagnostic: "interfaces" }) + assert.ok(!redacted.output.includes(password)); assert.ok(!redacted.output.includes("NEVER_LOG_TOKEN")); assert.ok(redacted.output.includes("[redacted]")) + fixture.payloads["/log"] = [{ ".id": "*1", time: "0", topics: "system,info", message: `${password} password=NEVER_PRINT_LOG_PASSWORD token=NEVER_PRINT_LOG_TOKEN` }] + const logResult = await invoke({ diagnostic: "logs" }) + for (const secret of [password, "NEVER_PRINT_LOG_PASSWORD", "NEVER_PRINT_LOG_TOKEN"]) assert.ok(!logResult.output.includes(secret)) + fixture.payloads["/interface"] = [{ ".id": "*1", name: "name", type: "ether", comment: "я".repeat(20000) }] + const bounded = await invoke({ diagnostic: "interfaces" }); assert.equal(bounded.truncated, true); assert.ok(Buffer.byteLength(bounded.output) <= TERMINAL_DIAGNOSTIC_MAX_OUTPUT_BYTES); assert.ok(!bounded.output.includes("�")) + fixture.payloads["/interface"] = [{ ".id": "*1", name: "x".repeat(1048577) }]; await assert.rejects(invoke({ diagnostic: "interfaces" })) + fixture.payloads["/interface"] = interfaces; fixture.payloads["/log"] = logs + fixture.payloads["/system/resource"] = [] + await assert.rejects(invoke({ diagnostic: "resources" })); fixture.payloads["/system/resource"] = resource + fixture.setBeforeResponse(async (_request, response, path) => { if (path === "/interface") { response.writeHead(401); response.end(JSON.stringify({ message: `password=${password}` })); return true } }) + await assert.rejects(invoke({ diagnostic: "interfaces" })) + fixture.setBeforeResponse(async (_request, _response, path) => { if (path === "/routing/ospf/interface-template") { await pool.query("UPDATE servers SET password='transient-terminal-fixture' WHERE id=$1", [sourceId]); await pool.query("UPDATE servers SET password=$2 WHERE id=$1", [sourceId, password]) } }) + const beforeAba = fixture.requests.length + await assert.rejects(invoke({ diagnostic: "ospf_interfaces" })) + assert.equal(fixture.requests.length, beforeAba + 1, "Endpoint ABA after response prevents any flag follow-up") + fixture.setBeforeResponse(undefined) + assert.ok((await invoke({ diagnostic: "identity" })).output.includes("owned-router"), "Next fresh diagnostic can use the new endpoint epoch") + const cancelled = new AbortController(); cancelled.abort() + const beforeCancelled = fixture.requests.length + await assert.rejects(invokeMcpTool(principal, "mm_server_terminal_diagnostic", { serverId: sourceId, diagnostic: "interfaces" }, cancelled.signal)) + assert.equal(fixture.requests.length, beforeCancelled) + let streamStarted!: () => void, streamClosed!: () => void + const started = new Promise(resolve => { streamStarted = resolve }), closed = new Promise(resolve => { streamClosed = resolve }) + fixture.setBeforeResponse(async (_request, response) => { response.writeHead(200); response.write("["); response.once("close", streamClosed); streamStarted(); return true }) + const streaming = new AbortController() + const stopped = assert.rejects(invokeMcpTool(principal, "mm_server_terminal_diagnostic", { serverId: sourceId, diagnostic: "interfaces" }, streaming.signal)) + try { await Promise.race([started, delay(5000).then(() => { throw Error("Terminal owned stream did not start") })]) } + finally { streaming.abort() } + await stopped; await Promise.race([closed, delay(5000).then(() => { throw Error("Terminal owned stream did not close") })]) + fixture.setBeforeResponse(undefined) + let late = false + pool.query = ((...args: unknown[]) => { const sql = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string })?.text; const pending = (originalQuery as (...args: unknown[]) => Promise)(...args); if (!late && sql?.includes("AS endpoint")) { late = true; return pending.then(async rows => { await setConnectionStatus(issued.connection.id, "paused", owner); return rows }) } return pending }) as typeof pool.query + const beforeSecretRevoke = fixture.requests.length + try { await assert.rejects(invoke({ diagnostic: "interfaces" })); assert.equal(late, true); assert.equal(fixture.requests.length, beforeSecretRevoke) } finally { restoreQuery() } + await setConnectionStatus(issued.connection.id, "active", owner) + fixture.setBeforeResponse(async () => { await setConnectionStatus(issued.connection.id, "paused", owner) }) + await assert.rejects(invoke({ diagnostic: "interfaces" })) + const audit = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1", [issued.connection.id])).rows + const requests = (await pool.query("SELECT result,error_code FROM mcp_requests WHERE connection_id=$1", [issued.connection.id])).rows + for (const secret of [password, "NEVER_LOG_TOKEN", "NEVER_PRINT_LOG_PASSWORD", "NEVER_PRINT_LOG_TOKEN"]) { assert.ok(!JSON.stringify(audit).includes(secret)); assert.ok(!JSON.stringify(requests).includes(secret)) } + assert.equal(terminalDiagnosticSchema.safeParse({ diagnostic: "help", raw: "/system reboot" }).success, false) + assert.ok(fixture.requests.every(request => request.method === "GET" || request.method === "POST" && (request.path === "/tool/ping" || /\/routing\/ospf\/(?:area|instance|interface-template)\/(?:get|print)$/.test(request.path))), "The full scenario never dispatches configuration writes or universal execute") + console.log("✓ Typed terminal SQL+REST HTTP: every allowlist branch/alias exact parity, existing permission, help no credentials/I/O, native OSPF flags and logs clamps, no CLI/plans, bytes/UTF-8 limits, private logs/errors/audit, full-scope-before-secret, endpoint ABA and late grant fences") + } finally { + restoreQuery(); fixture.setBeforeResponse(undefined) + await setConnectionStatus(issued.connection.id, "paused", owner) + await fixture.close() + await pool.query("DELETE FROM servers WHERE id=$1 AND host='127.0.0.1' AND port=$2", [sourceId, fixture.port]) + } +} diff --git a/backend/src/test/mcp-traffic-history.ts b/backend/src/test/mcp-traffic-history.ts new file mode 100644 index 00000000..4e419c9f --- /dev/null +++ b/backend/src/test/mcp-traffic-history.ts @@ -0,0 +1,133 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { ensurePartitionFor } from "../db/partitions.js" +import { readServerSamplesInRange } from "../services/traffic-collector.js" +import { readTrafficCollectionContext } from "../services/traffic-collection-state.js" +import trafficRoutes from "../routes/traffic.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpTrafficHistory(actor: AuthUser) { + const savedSettings = (await pool.query("SELECT * FROM traffic_settings WHERE id=1")).rows[0] + const sourceAt = new Date(Math.floor(Date.now() / 1000) * 1000 - 15000), previous = new Date(sourceAt.getTime() - 30000), future = new Date(Date.now() + 3600000) + for (const time of [previous, sourceAt, future]) await ensurePartitionFor(pool, "traffic_samples", "week", time) + const owner = { ...actor, id: "traffic-history-owner", permissions: [...actor.permissions, "mm:traffic:read"] } + const operationIds = ["traffic.servers.history.read", "traffic.server.history.read", "traffic.server.interfaces.read"] + const makeAccess = async (ids: number[]) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Traffic stored history", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:traffic:read"], scope: { serverIds: ids, operationIds, allowGlobal: false }, limits: { callsPerMinute: 120 } }), owner) + const http = Fastify(); await http.register(trafficRoutes, { prefix: "/api" }) + type History = { serverId: number; rxNowMbps: number | null; txNowMbps: number | null; accountedRxGiB: number | null; currentState: string; rxSeriesMbps: (number | null)[]; collectionError: boolean; live: boolean } + type InterfacePage = { interfaces: { name: string; running: boolean | null; disabled: boolean | null; rxNowMbps: number | null }[]; total: number; nextOffset: number | null } + const plansBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n + try { + await pool.query("INSERT INTO servers(id,name,host,password,enabled,updated_at) VALUES(52,'NEVER_TRAFFIC_LABEL','192.0.2.52','NEVER_TRAFFIC_PASSWORD',true,$1),(53,'NEVER_TRAFFIC_HIDDEN','192.0.2.53','NEVER_TRAFFIC_PASSWORD',true,$1),(54,'disabled-history','192.0.2.54','',false,$1)", [previous.toISOString()]) + await pool.query("INSERT INTO traffic_settings(id,enabled,interval_sec,last_error) VALUES(1,true,30,'NEVER_TRAFFIC_SETTINGS_ERROR') ON CONFLICT(id) DO UPDATE SET enabled=true,interval_sec=30,last_error='NEVER_TRAFFIC_SETTINGS_ERROR'") + await pool.query("INSERT INTO traffic_collection_state(server_id,server_updated_at,last_attempt_at,last_succeeded_at,status,error) VALUES(52,$1,$2,$2,'ok',NULL),(53,$1,$2,$2,'error','NEVER_TRAFFIC_HIDDEN_ERROR')", [previous.toISOString(), sourceAt.toISOString()]) + const put = (sid: number, iface: string, time: Date, flags: number, peer = "") => pool.query("INSERT INTO traffic_samples(server_id,interface_name,peer_public_key,sampled_at,rx_bytes,tx_bytes,rx_bps,tx_bps,flags,native_counter_identity) VALUES($1,$2,$3,$4,1000,2000,0,0,$5,'counter-one')", [sid, iface, peer, time.toISOString(), flags]) + for (const time of [previous, sourceAt]) { + await put(52, "fixture-traffic-zero", time, 61) + await put(52, "fixture-traffic-zero", time, 61, "NEVER_TRAFFIC_PEER_KEY") + await put(52, "lo", time, 61) + } + await put(52, "NEVER_TRAFFIC_FUTURE_IFACE", future, 61) + await pool.query("INSERT INTO traffic_samples(server_id,interface_name,sampled_at,rx_bytes,flags) SELECT 53,'NEVER_TRAFFIC_HIDDEN_'||n,$1,9007199254740993,0 FROM generate_series(1,20001) n", [sourceAt.toISOString()]) + assert.deepEqual([...((await readTrafficCollectionContext([52])).states.keys())], [52]) + 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 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) + const rest = (await http.inject({ method: "GET", url: "/api/traffic/servers/52?range=1h" })).json().server + assert.equal(result.rxNowMbps, rest.rxNow); assert.equal(result.accountedRxGiB, rest.rxTotal) + const list = await invokeMcpTool(principal, "mm_traffic_servers_history_get", {}) as { servers: History[]; totalServers: number } + assert.deepEqual(list.servers.map(row => row.serverId), [52]); assert.equal(list.totalServers, 1) + assert.deepEqual((await invokeMcpTool(principal, "mm_traffic_servers_history_get", { serverOffset: 1000 }) as { servers: unknown[] }).servers, []) + let interfaces = await invokeMcpTool(principal, "mm_traffic_server_interfaces_get", { serverId: 52 }) as InterfacePage + assert.deepEqual(interfaces.interfaces.map(row => row.name), ["fixture-traffic-zero"]); assert.equal(interfaces.interfaces[0].running, true); assert.equal(interfaces.interfaces[0].rxNowMbps, 0) + const restInterfaces = (await http.inject({ method: "GET", url: "/api/traffic/servers/52/interfaces" })).json().interfaces + assert.equal(restInterfaces.find((row: { name: string }) => row.name === "fixture-traffic-zero").rxNow, 0) + for (const tool of ["mm_traffic_server_history_get", "mm_traffic_server_interfaces_get", "mm_traffic_servers_history_get"]) await assert.rejects(invokeMcpTool(principal, tool, { serverId: 53 })) + await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52, range: "7d" })) + await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52, interface: "__all__" })) + const aborted = new AbortController(); aborted.abort() + await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }, aborted.signal)) + await put(52, "fixture-traffic-unknown", sourceAt, 0) + interfaces = await invokeMcpTool(principal, "mm_traffic_server_interfaces_get", { serverId: 52, limit: 1 }) as InterfacePage + assert.equal(interfaces.total, 2); assert.equal(interfaces.nextOffset, 1) + const unknown = (await invokeMcpTool(principal, "mm_traffic_server_interfaces_get", { serverId: 52, offset: 1, limit: 1 }) as InterfacePage).interfaces[0] + assert.equal(unknown.name, "fixture-traffic-unknown"); assert.equal(unknown.running, null); assert.equal(unknown.disabled, null); assert.equal(unknown.rxNowMbps, null) + assert.equal((await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }) as History).rxNowMbps, null) + await pool.query("UPDATE traffic_collection_state SET status='error',error='NEVER_TRAFFIC_VISIBLE_ERROR' WHERE server_id=52") + const failed = await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52, interface: "fixture-traffic-zero" }) as History + assert.equal(failed.currentState, "error"); assert.equal(failed.rxNowMbps, null); assert.equal(failed.collectionError, true); assert(failed.rxSeriesMbps.some(value => value === 0)) + await pool.query("UPDATE traffic_collection_state SET status='pending',error=NULL WHERE server_id=52") + assert.equal((await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }) as History).currentState, "pending") + await pool.query("UPDATE traffic_collection_state SET status='ok',last_attempt_at=$1,last_succeeded_at=$1 WHERE server_id=52", [new Date(Date.now() - 180000).toISOString()]) + assert.equal((await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }) as History).currentState, "stale") + assert.equal((await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 54 }) as History).currentState, "disabled") + const wide = await makeAccess([52, 53, 54]), widePrincipal = await authenticateMcpToken(wide.token), grant = randomUUID() + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grant, wide.connection.id, JSON.stringify({ id: grant, credentialId: widePrincipal.credentialId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(), scope: { allowGlobal: false, serverIds: [52], operationIds } })]) + assert.deepEqual((await invokeMcpTool(widePrincipal, "mm_traffic_servers_history_get", {}) as { servers: History[] }).servers.map(row => row.serverId), [52]) + const originalQuery = pool.query.bind(pool) + let sourceChanged = false + pool.query = ((...args: unknown[]) => { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string } | undefined)?.text + if (!sourceChanged && text?.includes('from "traffic_samples"')) { + sourceChanged = true + return (async () => { + await originalQuery("UPDATE servers SET host='192.0.2.152' WHERE id=52") + return (originalQuery as (...values: unknown[]) => unknown)(...args) + })() + } + return (originalQuery as (...values: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }), /источника/); assert.equal(sourceChanged, true) } + 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/) + await pool.query("DELETE FROM traffic_samples WHERE server_id=52 AND interface_name LIKE 'limit-history-%'") + let ownerRevokedDuringRead = false + pool.query = ((...args: unknown[]) => { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string } | undefined)?.text + if (!ownerRevokedDuringRead && text?.includes('from "traffic_samples"')) { + ownerRevokedDuringRead = true + return (async () => { + await originalQuery("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [access.connection.id]) + return (originalQuery as (...values: unknown[]) => unknown)(...args) + })() + } + return (originalQuery as (...values: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }), /отозвано/); assert.equal(ownerRevokedDuringRead, true) } + finally { pool.query = originalQuery; await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb WHERE id=$1", [access.connection.id, JSON.stringify(owner.permissions)]) } + let pausedDuringRead = false + pool.query = ((...args: unknown[]) => { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string } | undefined)?.text + if (!pausedDuringRead && text?.includes('from "traffic_samples"')) { + pausedDuringRead = true + return (async () => { + await setConnectionStatus(access.connection.id, "paused", owner) + return (originalQuery as (...values: unknown[]) => unknown)(...args) + })() + } + return (originalQuery as (...values: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 })); assert.equal(pausedDuringRead, true) } + finally { pool.query = originalQuery } + await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 })) + const requests = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=ANY($1::uuid[])", [[access.connection.id, wide.connection.id]])).rows + assert.equal(JSON.stringify(requests).includes("NEVER_TRAFFIC_"), false) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n, plansBefore) + } finally { + await http.close() + await pool.query("DELETE FROM traffic_samples WHERE server_id IN (52,53,54)") + await pool.query("DELETE FROM servers WHERE id IN (52,53,54)") + if (savedSettings) await pool.query("UPDATE traffic_settings SET enabled=$1,interval_sec=$2,last_error=$3 WHERE id=1", [savedSettings.enabled, savedSettings.interval_sec, savedSettings.last_error]) + else await pool.query("DELETE FROM traffic_settings WHERE id=1") + } + console.log("✓ Traffic server history shares REST readers: pre-SQL scope/grants, zero/unknown masks, freshness/disabled/errors, aggregate versus WG peer, future exclusion, pages, row cap, late source drift, safe requests and no observer plans") +} diff --git a/backend/src/test/mcp-uptime-overview.ts b/backend/src/test/mcp-uptime-overview.ts new file mode 100644 index 00000000..edf41809 --- /dev/null +++ b/backend/src/test/mcp-uptime-overview.ts @@ -0,0 +1,112 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { setTimeout as delay } from "node:timers/promises" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { pool } from "../db/index.js" +import { ensurePartitionFor } from "../db/partitions.js" +import type { AuthUser } from "../lib/permissions.js" +import uptimeRoutes from "../routes/uptime.js" +import { projectUptimeOverview, readUptimeOverview } from "../services/uptime-overview-read.js" +import { readProbeRows, readProbeSamplesSince, readResourceSamplesSince } from "../services/uptime-collector.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpUptimeOverview(actor: AuthUser) { + const fixtureOwner = { ...actor, permissions: [...actor.permissions, "mm:uptime:read"] } + const prefix = "uptime-overview-" + randomUUID(), probe = prefix + "-a", hidden = prefix + "-hidden" + const at = new Date(Date.now() - 5000), before = new Date(at.getTime() - 1000), future = new Date(Date.now() + 3600000) + for (const table of ["uptime_probe_samples", "uptime_resource_samples"] as const) for (const time of [before, at, future]) await ensurePartitionFor(pool, table, "week", time) + const created: string[] = [] + const create = async (permissions = ["mm:uptime:read"]) => { + const issued = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: prefix, mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions, + scope: { serverIds: [76, 78], operationIds: ["uptime.overview.read"] }, limits: { callsPerMinute: 120 } }), fixtureOwner) + created.push(issued.connection.id); return issued + } + const app = Fastify(); await app.register(uptimeRoutes, { prefix: "/api" }) + try { + await pool.query("INSERT INTO servers(id,name,host,password,enabled) VALUES(76,'overview','192.0.2.76','NEVER_OVERVIEW_PASSWORD',true),(77,'NEVER_OVERVIEW_HIDDEN','192.0.2.77','NEVER_OVERVIEW_HIDDEN_PASSWORD',true),(78,'overview-disabled','192.0.2.78','',false)") + await pool.query("INSERT INTO uptime_probes(id,src_server_id,name,target) VALUES($1,76,'visible','192.0.2.1'),($2,77,'NEVER_OVERVIEW_HIDDEN','192.0.2.77')", [probe, hidden]) + await pool.query("INSERT INTO uptime_probe_samples(probe_id,sampled_at,rtt_ms,loss_pct,status,measurement_known,observation_error) VALUES($1,$2,0,0,'up',true,NULL),($1,$3,NULL,100,'down',false,'NEVER_OVERVIEW_PROVIDER_SECRET'),($1,$4,999,99,'down',true,NULL)", [probe, before.toISOString(), at.toISOString(), future.toISOString()]) + await pool.query("INSERT INTO uptime_resource_samples(server_id,sampled_at,status,cpu_load,total_memory,free_memory) VALUES(76,$1,'online',0,1048576,1048576),(76,$2,'offline',0,0,0),(76,$3,'online',99,999999,999999)", [before.toISOString(), at.toISOString(), future.toISOString()]) + // A hidden server exceeds the public reader's cap. Its rows cannot affect a scoped read. + await pool.query("INSERT INTO uptime_probe_samples(probe_id,sampled_at,rtt_ms,loss_pct,status) SELECT $1,$2::timestamptz - n * interval '1 millisecond',1,0,'up' FROM generate_series(1,20001) n", [hidden, before.toISOString()]) + const issued = await create(), principal = await authenticateMcpToken(issued.token) + type Result = ReturnType & { totalServers: number; incomplete: boolean; nextOffset: number | null } + const read = (input = {}, signal?: AbortSignal) => invokeMcpTool(principal, "mm_uptime_overview_get", { range: "5m", ...input }, signal) as Promise + const result = await read({ sampleLimit: 1 }) + assert.equal(result.totalServers, 2); assert.equal(result.probes.length, 1); assert.equal(result.probes[0].totalSamples, 2) + assert.equal(result.probes[0].rttMs, null); assert.equal(result.probes[0].lossPct, null); assert.equal(result.probes[0].measurementKnown, false) + assert.equal(result.probes[0].samplesTruncated, true); assert.equal(result.incomplete, true) + assert.equal(result.resources[0].cpuLoadPct, 0); assert.equal(result.resources[0].usingHistoricalFallback, true); assert.equal(result.resources[0].latestAttemptStatus, "offline") + assert.equal(result.resources[1].cpuLoadPct, null); assert.equal(result.resources[1].uptimeSeconds, null) + assert.equal(result.live, false); assert.equal(result.forwardingVerified, false); assert.ok(!JSON.stringify(result).includes("NEVER_")) + const expiredAt = new Date(Date.now() - 10800000) + await ensurePartitionFor(pool, "uptime_resource_samples", "week", expiredAt) + await pool.query("INSERT INTO uptime_resource_samples(server_id,sampled_at,status,cpu_load,total_memory) VALUES(78,$1,'online',42,1048576),(78,$2,'offline',0,0)", [expiredAt.toISOString(), at.toISOString()]) + const expired = (await read({ range: "4h" })).resources.find(row => row.serverId === 78)! + assert.equal(expired.cpuLoadPct, null); assert.equal(expired.usingHistoricalFallback, false); assert.equal(expired.latestAttemptStatus, "offline") + await assert.rejects(read({ serverId: 77 })); await assert.rejects(read({ range: "all" })) + const denied = await create([]); await assert.rejects(invokeMcpTool(await authenticateMcpToken(denied.token), "mm_uptime_overview_get", {})) + const aborted = new AbortController(); aborted.abort(); await assert.rejects(read({}, aborted.signal)) + assert.equal((await read({ offset: 1 })).probes.length, 0) + const bounds = { startAt: new Date(Date.now() - 300000).toISOString(), endAt: new Date().toISOString(), maxRows: 2, maxProbes: 1 } + assert.equal((await readUptimeOverview([76, 78], bounds)).probes.length, 1) + await assert.rejects(readProbeSamplesSince(bounds.startAt, { probeIds: [probe], untilIso: bounds.endAt, maxRows: 1 })) + await assert.rejects(readResourceSamplesSince(bounds.startAt, 76, { untilIso: bounds.endAt, maxRows: 1 })) + assert.deepEqual(await readProbeRows([], 1), []); assert.deepEqual(await readProbeSamplesSince(bounds.startAt, { probeIds: [] }), []) + await pool.query("DELETE FROM uptime_probe_samples WHERE probe_id=$1 AND sampled_at=$2", [probe, at.toISOString()]) + const zero = (await read()).probes[0]; assert.equal(zero.rttMs, 0); assert.equal(zero.lossPct, 0); assert.equal(zero.measurementKnown, true) + const schedulerId = prefix + "-run" + await pool.query("INSERT INTO scheduler_runs(id,job_key,started_at,finished_at,status,result_json) VALUES($1,'uptime_ping',$2,$2,'error',$3::jsonb)", [schedulerId, before.toISOString(), JSON.stringify({ v: 1, job: "uptime_ping", sampledAt: before.toISOString(), probes: [{ probeId: probe, ok: false, error: "NEVER_OVERVIEW_LEGACY_ERROR" }] })]) + const legacyFailure = (await read()).probes[0]; assert.equal(legacyFailure.rttMs, null); assert.equal(legacyFailure.status, "unknown") + await pool.query("DELETE FROM scheduler_runs WHERE id=$1", [schedulerId]) + await pool.query("DELETE FROM uptime_probe_samples WHERE probe_id=$1", [hidden]) + const http = await app.inject({ method: "GET", url: "/api/uptime/overview?range=5m" }); assert.equal(http.statusCode, 200) + const legacy = http.json(); assert.equal(legacy.resources.find((row: { serverId: string }) => row.serverId === "76").cpu, 0) + assert.equal(legacy.probes.find((row: { id: string }) => row.id === probe).rtt, 0) + assert.ok(!legacy.probes.find((row: { id: string }) => row.id === probe).series.includes(999), "REST also excludes future samples") + await pool.query("INSERT INTO uptime_probe_samples(probe_id,sampled_at,rtt_ms,loss_pct,status) SELECT $1,$2::timestamptz - n * interval '1 millisecond',1,0,'up' FROM generate_series(1,20001) n", [probe, before.toISOString()]) + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + await pool.query("DELETE FROM uptime_probe_samples WHERE probe_id=$1 AND sampled_at<$2", [probe, before.toISOString()]) + await pool.query("INSERT INTO uptime_resource_samples(server_id,sampled_at,status,total_memory) SELECT 76,$1::timestamptz - n * interval '1 millisecond','online',1048576 FROM generate_series(1,20001) n", [before.toISOString()]) + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + await pool.query("DELETE FROM uptime_resource_samples WHERE server_id=76 AND sampled_at<$1", [before.toISOString()]) + await pool.query("INSERT INTO uptime_probes(id,src_server_id,name,target) SELECT $1||n::text,76,'cap','192.0.2.1' FROM generate_series(1,1000) n", [prefix + "-cap-"]) + await assert.rejects(read(), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE") + await pool.query("DELETE FROM uptime_probes WHERE id LIKE $1", [prefix + "-cap-%"]) + const owner = (await pool.query("SELECT owner_permissions FROM mcp_connections WHERE id=$1", [issued.connection.id])).rows[0].owner_permissions + const race = async (change: () => Promise, signal?: AbortSignal) => { + const blocker = await pool.connect() + try { + await blocker.query("BEGIN"); await blocker.query("LOCK TABLE uptime_resource_samples IN ACCESS EXCLUSIVE MODE") + const pending = read({}, signal).then(value => ({ value, error: null }), error => ({ value: null, error })) + let reached = false + for (let n = 0; n < 100; n++) { + reached = (await pool.query("SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query ILIKE '%from%uptime_resource_samples%') AS reached")).rows[0].reached + if (reached) break + await delay(20) + } + assert.equal(reached, true, "Race must reach the real stored-resource read") + await change(); await blocker.query("COMMIT") + assert.ok((await pending).error, "A late source/grant change must reject the read") + } finally { await blocker.query("ROLLBACK"); blocker.release() } + } + await race(() => pool.query("UPDATE servers SET password='NEVER_OVERVIEW_CHANGED' WHERE id=76")) + await pool.query("UPDATE servers SET password='NEVER_OVERVIEW_PASSWORD' WHERE id=76") + await race(() => pool.query("UPDATE uptime_probes SET src_server_id=77 WHERE id=$1", [probe])) + await pool.query("UPDATE uptime_probes SET src_server_id=76 WHERE id=$1", [probe]) + await race(() => pool.query("UPDATE mcp_connections SET owner_permissions='[]'::jsonb WHERE id=$1", [issued.connection.id])) + await pool.query("UPDATE mcp_connections SET owner_permissions=$2::jsonb WHERE id=$1", [issued.connection.id, JSON.stringify(owner)]) + const cancel = new AbortController() + await race(async () => cancel.abort(), cancel.signal) + await race(() => setConnectionStatus(issued.connection.id, "paused", actor)) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [issued.connection.id])).rows[0].n, 0) + assert.ok(!JSON.stringify((await pool.query("SELECT result FROM mcp_requests WHERE connection_id=$1", [issued.connection.id])).rows).includes("NEVER_")) + console.log("✓ Uptime overview: shared REST/SQL, scope before 20001 hidden samples, caps/pages/future bounds, known zero vs unknown, historical fallback, real blocked-read source/probe/owner/pause races and no network/secrets/plans") + } finally { + await app.close(); await pool.query("DELETE FROM scheduler_runs WHERE id LIKE $1", [prefix + "%"]) + for (const id of created) await pool.query("DELETE FROM mcp_connections WHERE id=$1", [id]) + await pool.query("DELETE FROM servers WHERE id IN(76,77,78)") + } +} diff --git a/backend/src/test/mcp-user-traffic-history.ts b/backend/src/test/mcp-user-traffic-history.ts new file mode 100644 index 00000000..a0faa8da --- /dev/null +++ b/backend/src/test/mcp-user-traffic-history.ts @@ -0,0 +1,121 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import Fastify from "fastify" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { ensurePartitionFor } from "../db/partitions.js" +import trafficRoutes from "../routes/traffic.js" +import { readScopedTrafficUsers } from "../services/traffic-user-history-read.js" +import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool } from "../modules/mcp/runtime.js" + +export async function verifyMcpUserTrafficHistory(actor: AuthUser) { + const saved = (await pool.query("SELECT enabled,interval_sec FROM traffic_settings WHERE id=1")).rows[0] + const time = new Date(Math.floor(Date.now() / 1000) * 1000 - 10000), previous = new Date(time.getTime() - 30000), future = new Date(Date.now() + 3600000) + for (const at of [previous, time, future]) await ensurePartitionFor(pool, "traffic_samples", "week", at) + const names = ["a", "b", "first", "mixed", "off", "split", "unbound", "cap"].map(name => "uth-" + name) + const owner = { ...actor, id: "user-traffic-owner", permissions: [...actor.permissions, "mm:traffic:read"] } + const operations = ["traffic.users.history.read", "traffic.user.history.read", "traffic.bindings.history.read"] + const makeAccess = (ids: number[], allowGlobal = false) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Scoped user traffic", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:traffic:read"], scope: { serverIds: ids, operationIds: operations, allowGlobal }, limits: { callsPerMinute: 120 } }), owner) + const http = Fastify(); await http.register(trafficRoutes, { prefix: "/api" }) + type User = { id: string; rxNowMbps: number | null; accountedRxGiB: number | null; accountEnabled: boolean; completeSourceSet: boolean; bindingCount: number; selectedBindingCount: number; nextBindingOffset: number | null; currentState: string; bindings: { bindingId: string; serverId: number; peerSpecific: boolean; rxNowMbps: number | null; status: string }[] } + type UserPage = { users: User[]; total: number; nextOffset: number | null; incomplete: boolean } + type BindingPage = { interfaces: User["bindings"]; totalBindingsInUserPage: number; totalUsers: number; nextOffset: number | null; nextUserOffset: number | null; incomplete: boolean } + const plansBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n + let aBinding = "" + try { + await pool.query("INSERT INTO servers(id,name,host,password,enabled,updated_at) VALUES(55,'traffic-users','192.0.2.55','NEVER_UTH_SERVER_PASSWORD',true,$1),(56,'NEVER_UTH_HIDDEN_SERVER','192.0.2.56','NEVER_UTH_SERVER_PASSWORD',true,$1),(57,'traffic-disabled','192.0.2.57','',false,$1)", [previous.toISOString()]) + await pool.query("INSERT INTO traffic_settings(id,enabled,interval_sec) VALUES(1,true,30) ON CONFLICT(id) DO UPDATE SET enabled=true,interval_sec=30") + await pool.query("INSERT INTO traffic_collection_state(server_id,server_updated_at,last_attempt_at,last_succeeded_at,status,error) VALUES(55,$1,$2,$2,'ok',NULL),(56,$1,$2,$2,'error','NEVER_UTH_HIDDEN_ERROR')", [previous.toISOString(), time.toISOString()]) + for (const id of names.filter(id => id !== "uth-cap")) await pool.query("INSERT INTO app_users(id,login,name,active,sections_json,servers_json) VALUES($1,$1,$1,$2,$3::jsonb,$3::jsonb)", [id, id !== "uth-b", JSON.stringify({ malformed: "NEVER_UTH_PRIVATE_PERMISSIONS" })]) + const bind = async (user: string, sid: number, iface: string, peer = "") => { + const id = randomUUID() + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name,interface_type,peer_public_key,peer_name,comment) VALUES($1,$2,$3,$4,'ether',$5,'NEVER_UTH_PEER_NAME','NEVER_UTH_COMMENT')", [id, user, sid, iface, peer]) + return id + } + aBinding = await bind("uth-a", 55, "uth-a-if") + await bind("uth-b", 55, "uth-b-wg", "NEVER_UTH_PEER_KEY") + await bind("uth-first", 55, "uth-first-if") + await bind("uth-mixed", 55, "uth-mixed-if"); await bind("uth-mixed", 56, "NEVER_UTH_HIDDEN_IFACE") + await bind("uth-off", 57, "uth-off-if") + await bind("uth-split", 55, "uth-split-if"); await bind("uth-split", 57, "uth-split-off") + const sample = (sid: number, iface: string, at: Date, rx: number, peer = "", flags = 61) => pool.query("INSERT INTO traffic_samples(server_id,interface_name,peer_public_key,sampled_at,rx_bytes,tx_bytes,flags,native_counter_identity) VALUES($1,$2,$3,$4,$5,0,$6,'uth-counter')", [sid, iface, peer, at.toISOString(), rx, flags]) + for (const [index, at] of [previous, time].entries()) { + await sample(55, "uth-a-if", at, 1000 + index * 3750000) + await sample(55, "uth-b-wg", at, 0, "NEVER_UTH_PEER_KEY") + await sample(55, "uth-split-if", at, 0); await sample(55, "uth-mixed-if", at, 0) + await sample(57, "uth-off-if", at, 0); await sample(57, "uth-split-off", at, 0) + } + await sample(55, "uth-first-if", time, 0, "", 0) + const access = await makeAccess([55, 57]), principal = await authenticateMcpToken(access.token) + const page = await invokeMcpTool(principal, "mm_traffic_users_history_get", { serverLimit: 10 }) as UserPage + assert.deepEqual(page.users.map(user => user.id), ["uth-a", "uth-b", "uth-first", "uth-off", "uth-split"]) + const a = page.users.find(user => user.id === "uth-a")!, b = page.users.find(user => user.id === "uth-b")! + assert.equal(a.rxNowMbps, 1); assert.equal(a.accountedRxGiB, 3750000 / 1024 ** 3); assert.equal(a.completeSourceSet, true) + assert.equal(b.rxNowMbps, 0); assert.equal(b.bindings[0].peerSpecific, true); assert.equal(b.bindings[0].status, "unknown"); assert.equal(b.accountEnabled, false) + assert.equal(page.users.find(user => user.id === "uth-first")!.rxNowMbps, null) + assert.equal(page.users.find(user => user.id === "uth-off")!.currentState, "disabled") + const rest = (await http.inject({ method: "GET", url: "/api/traffic/users/uth-a?range=1h" })).json().user + assert.equal(a.rxNowMbps, rest.rxNow); assert.equal(a.accountedRxGiB, rest.rxTotal) + assert.equal((await http.inject({ method: "GET", url: "/api/traffic/bound-interfaces?range=1h" })).json().interfaces.find((row: { bindingId: string }) => row.bindingId === aBinding).rxNow, a.rxNowMbps) + await sample(55, "uth-a-if", future, 99999999) + assert.equal((await invokeMcpTool(principal, "mm_traffic_user_history_get", { userId: "uth-a" }) as { user: User }).user.rxNowMbps, 1) + assert.equal((await http.inject({ method: "GET", url: "/api/traffic/users/uth-a?range=1h" })).json().user.rxNow, 1) + const partial = (await invokeMcpTool(principal, "mm_traffic_user_history_get", { userId: "uth-split", serverId: 55 }) as { user: User; incomplete: boolean }) + assert.equal(partial.user.bindingCount, 2); assert.equal(partial.user.selectedBindingCount, 1); assert.equal(partial.user.completeSourceSet, false); assert.equal(partial.incomplete, true) + const nested = (await invokeMcpTool(principal, "mm_traffic_user_history_get", { userId: "uth-split", serverLimit: 10, bindingLimit: 1 }) as { user: User }).user + assert.equal(nested.bindings.length, 1); assert.equal(nested.nextBindingOffset, 1) + assert.equal((await invokeMcpTool(principal, "mm_traffic_users_history_get", { offset: 1, limit: 1 }) as UserPage).nextOffset, 2) + assert.equal((await invokeMcpTool(principal, "mm_traffic_users_history_get", { offset: 100000 }) as UserPage).users.length, 0) + let bindings = await invokeMcpTool(principal, "mm_traffic_bound_interfaces_get", { userId: "uth-split", serverLimit: 10, offset: 1, limit: 1 }) as BindingPage + assert.equal(bindings.interfaces.length, 1); assert.equal(bindings.totalBindingsInUserPage, 2); assert.equal(bindings.incomplete, true) + bindings = await invokeMcpTool(principal, "mm_traffic_bound_interfaces_get", { userLimit: 1, limit: 1 }) as BindingPage + assert.equal(bindings.totalUsers, 5); assert.equal(bindings.nextUserOffset, 1) + await assert.rejects(invokeMcpTool(principal, "mm_traffic_user_history_get", { userId: "uth-mixed" })) + await assert.rejects(invokeMcpTool(principal, "mm_traffic_users_history_get", { serverId: 56 })) + await assert.rejects(invokeMcpTool(principal, "mm_traffic_users_history_get", { includeUnbound: true }), /глобального/) + const globalSubset = await makeAccess([55], true) + await assert.rejects(invokeMcpTool(await authenticateMcpToken(globalSubset.token), "mm_traffic_users_history_get", { includeUnbound: true }), /весь каталог/) + await assert.rejects(invokeMcpTool(principal, "mm_traffic_users_history_get", { limit: 51 })) + const allIds = (await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id)) + const broad = await makeAccess(allIds, true), broadPrincipal = await authenticateMcpToken(broad.token) + assert.equal((await invokeMcpTool(broadPrincipal, "mm_traffic_user_history_get", { userId: "uth-unbound", serverId: 55, includeUnbound: true }) as { user: User }).user.bindingCount, 0) + await assert.rejects(invokeMcpTool(broadPrincipal, "mm_traffic_user_history_get", { userId: "uth-unbound", serverId: 55 })) + const grant = randomUUID() + await pool.query("INSERT INTO mcp_grants(id,connection_id,payload) VALUES($1,$2,$3::jsonb)", [grant, broad.connection.id, JSON.stringify({ id: grant, credentialId: broadPrincipal.credentialId, startsAt: new Date(Date.now() - 1000).toISOString(), expiresAt: new Date(Date.now() + 60000).toISOString(), scope: { allowGlobal: false, serverIds: [55], operationIds: operations } })]) + assert.deepEqual((await invokeMcpTool(broadPrincipal, "mm_traffic_users_history_get", {}) as UserPage).users.map(user => user.id), ["uth-a", "uth-b", "uth-first"]) + const aborted = new AbortController(); aborted.abort() + await assert.rejects(invokeMcpTool(principal, "mm_traffic_users_history_get", {}, aborted.signal)) + await assert.rejects(readScopedTrafficUsers({ allowedServerIds: [55], selectedServerIds: [56], offset: 0, limit: 20 })) + await pool.query("INSERT INTO app_users(id,login,name) VALUES('uth-cap','uth-cap','uth-cap')") + await pool.query("INSERT INTO user_interface_bindings(id,user_id,server_id,interface_name) SELECT 'uth-cap-'||n,'uth-cap',55,'uth-cap-if-'||n FROM generate_series(1,201) n") + await assert.rejects(invokeMcpTool(principal, "mm_traffic_user_history_get", { userId: "uth-cap" }), /200/) + await pool.query("DELETE FROM app_users WHERE id='uth-cap'") + const originalQuery = pool.query.bind(pool) + let changed = false + pool.query = ((...args: unknown[]) => { + const text = typeof args[0] === "string" ? args[0] : (args[0] as { text?: string } | undefined)?.text + if (!changed && text?.includes('from "traffic_samples"')) { + changed = true + return (async () => { await originalQuery("UPDATE user_interface_bindings SET server_id=56 WHERE id=$1", [aBinding]); return (originalQuery as (...values: unknown[]) => unknown)(...args) })() + } + return (originalQuery as (...values: unknown[]) => unknown)(...args) + }) as typeof pool.query + try { await assert.rejects(invokeMcpTool(principal, "mm_traffic_user_history_get", { userId: "uth-a" }), /привязки/); assert.equal(changed, true) } + finally { pool.query = originalQuery; await pool.query("UPDATE user_interface_bindings SET server_id=55 WHERE id=$1", [aBinding]) } + await setConnectionStatus(access.connection.id, "paused", owner) + await assert.rejects(invokeMcpTool(principal, "mm_traffic_users_history_get", {})) + const persisted = (await pool.query("SELECT result FROM mcp_requests WHERE connection_id=ANY($1::uuid[])", [[access.connection.id, broad.connection.id]])).rows + assert.equal(JSON.stringify(persisted).includes("NEVER_UTH_"), false) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n, plansBefore) + } finally { + await http.close() + await pool.query("DELETE FROM app_users WHERE id=ANY($1::text[])", [names]) + await pool.query("DELETE FROM traffic_samples WHERE server_id IN (55,56,57)") + await pool.query("DELETE FROM servers WHERE id IN (55,56,57)") + if (saved) await pool.query("UPDATE traffic_settings SET enabled=$1,interval_sec=$2 WHERE id=1", [saved.enabled, saved.interval_sec]) + else await pool.query("DELETE FROM traffic_settings WHERE id=1") + } + console.log("✓ User/binding traffic shares REST formulas: SQL eligibility before JSON, mixed-user exclusion, selected-source completeness, whole-interface and WG peer metrics, zero/unknown, disabled/account states, nested pages, global unbound guard, grants, caps, late binding drift and no observer plans") +} diff --git a/backend/src/test/mcp-users-bindings.ts b/backend/src/test/mcp-users-bindings.ts new file mode 100644 index 00000000..0e25caa3 --- /dev/null +++ b/backend/src/test/mcp-users-bindings.ts @@ -0,0 +1,274 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import { createServer } from "node:http" +import { setTimeout as delay } from "node:timers/promises" +import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp" +import { interfaceCatalogStateSchema, userBindingCreateSchema } from "@mmapp/contracts/users" +import type { AuthUser } from "../lib/permissions.js" +import { pool } from "../db/index.js" +import { ensurePartitionFor } from "../db/partitions.js" +import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js" +import { invokeMcpTool, approvePlan } from "../modules/mcp/runtime.js" +import { MAX_MCP_PUBLIC_ARRAY_ITEMS } from "../modules/mcp/security.js" +import { addBinding, getInterfaceCatalogState, listInterfaceCatalog } from "../modules/users/service/users-service.js" + +/** Owned PostgreSQL and loopback RouterOS fixture; never writes to a real router. */ +export async function verifyMcpUserBindings(actor: AuthUser) { + const serverId = 91731, login = "mcp-binding-" + randomUUID(), connections: string[] = [] + let userId: string | undefined, otherUserId: string | undefined, nativeReads = 0, nativeWrites = 0 + const otherLogin = login + "-other", wgKey = Buffer.alloc(32, 7).toString("base64") + const fixtures = [ + { name: "ether-owned-user", type: "ether", raw: "ether" }, + { name: "bridge-owned-user", type: "bridge", raw: "bridge" }, + { name: "vlan-owned-user", type: "vlan", raw: "vlan" }, + { name: "vxlan-owned-user", type: "vxlan", raw: "vxlan" }, + { name: "gre-owned-user", type: "gre", raw: "gre-tunnel" }, + { name: "other-owned-user", type: "other", raw: "dummy" }, + { name: "wg-owned-user", type: "wg", raw: "wg", peerPublicKey: wgKey }, + { name: "ipsec-vpn", type: "ipsec", raw: "ipsec", peerPublicKey: "owned-client-cn" }, + { name: "ipsec-vpn", type: "ipsec", raw: "ipsec", peerPublicKey: "owned-psk" }, + ] + const pendingOperations = new Set() + const providerCanary = "internal RouterOS diagnostic " + randomUUID() + const wgPeer = { ".id": "*A", interface: "wg-owned-user", "public-key": wgKey, name: "Owned WG peer", "allowed-address": "192.0.2.7/32", disabled: "false" } + let catalogMode: "normal" | "upstream-error" | "duplicate" | "many-peers" = "normal" + const router = createServer((req, res) => { + res.setHeader("content-type", "application/json") + if (req.method !== "GET") { nativeWrites++; res.statusCode = 405; res.end("{}"); return } + nativeReads++ + const path = new URL(req.url ?? "/", "http://127.0.0.1").pathname.replace(/^\/rest/, "") + if (catalogMode === "upstream-error" && (path === "/interface/wireguard/peers" || path === "/certificate")) { + res.statusCode = 500 + res.end(JSON.stringify({ detail: providerCanary })) + return + } + const wgRows = catalogMode === "duplicate" ? [wgPeer, { ...wgPeer, ".id": "*D" }] + : catalogMode === "many-peers" ? Array.from({ length: MAX_MCP_PUBLIC_ARRAY_ITEMS + 1 }, (_, i) => { + const key = Buffer.alloc(32) + key.writeUInt16BE(i) + return { ...wgPeer, ".id": "*" + (i + 10).toString(16), "public-key": key.toString("base64") } + }) : [wgPeer] + const rows = path === "/interface" ? fixtures.filter(iface => iface.type !== "ipsec").map((iface, index) => ({ ".id": "*" + (index + 1).toString(16), name: iface.name, type: iface.raw, running: "true", disabled: "false" })) + : path === "/interface/wireguard/peers" ? wgRows + : path === "/certificate" ? [{ ".id": "*B", name: "ipsec-user-owned-client", "common-name": "owned-client-cn", ca: "owned-ca", "key-usage": "tls-client" }] + : path === "/ip/ipsec/identity" ? [{ ".id": "*C", peer: "ike-owned", "auth-method": "pre-shared-key", comment: "MikrotikManager:ipsec user=owned-psk" }] : [] + res.end(JSON.stringify(rows)) + }) + await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) + const operations = ["users.create", "users.get", "users.interfaces.read", "users.interfaces.state", "users.binding_add", "users.binding_remove", "users.delete", "changes.apply", "operations.read"] + const issue = async (name: string, serverIds: number[], permissions = ["mm:users:write"]) => { + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name, mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions, + scope: { allowGlobal: true, serverIds, operationIds: operations }, limits: { callsPerMinute: 2000, mutationsPerHour: 100 } }), actor) + connections.push(access.connection.id) + return authenticateMcpToken(access.token) + } + const state = async () => ({ + users: (await pool.query("SELECT * FROM app_users WHERE login=ANY($1::text[]) ORDER BY login", [[login, otherLogin]])).rows, + bindings: (await pool.query("SELECT * FROM user_interface_bindings WHERE server_id=$1 ORDER BY id", [serverId])).rows, + }) + const matches = (code: string) => (error: unknown) => (error as { code?: string }).code === code + try { + await pool.query("INSERT INTO servers(id,name,host,port,use_ssl,enabled) VALUES($1,'owned-user-binding','127.0.0.1',$2,false,true)", [serverId, (router.address() as { port: number }).port]) + const sampledAt = new Date() + await ensurePartitionFor(pool, "server_snapshots", "week", sampledAt) + const snapshot = fixtures.filter(iface => iface.type !== "ipsec").map(iface => ({ name: iface.name, type: iface.raw, running: true, disabled: false })) + await pool.query("INSERT INTO server_snapshots(server_id,polled_at,status,raw_interfaces) VALUES($1,$2,'online',$3::jsonb)", [serverId, sampledAt.toISOString(), JSON.stringify(snapshot)]) + const replaceSnapshot = (rows: typeof snapshot) => pool.query("UPDATE server_snapshots SET raw_interfaces=$3::jsonb WHERE server_id=$1 AND polled_at=$2", [serverId, sampledAt.toISOString(), JSON.stringify(rows)]) + const principal = await issue("User binding journey", [serverId]) + const narrow = await issue("User binding excluded server", []) + const readOnly = await issue("User binding read only", [serverId], ["mm:users:read"]) + const getUser = async () => (await invokeMcpTool(principal, "mm_users_get", { id: userId }) as { user: { id: string; login: string; bindings: { id: string; serverId: number; interfaceName: string; interfaceType: string; peerPublicKey: string }[] } }).user + const apply = async (tool: string, input: unknown, expected: "applied" | "stale" | "race" = "applied", prepared?: { planId: string; state: string }) => { + const before = await state() + const plan = prepared ?? await invokeMcpTool(principal, tool, input) as { planId: string; state: string } + assert.equal(plan.state, "awaiting-approval") + assert.deepEqual(await state(), before, tool + " preparation changes no users or bindings") + const key = randomUUID() + await assert.rejects(invokeMcpTool(principal, "mm_change_apply", { planId: plan.planId, idempotencyKey: key }), matches("APPROVAL_REQUIRED")) + assert.deepEqual(await state(), before, tool + " unapproved apply changes no users or bindings") + await approvePlan(plan.planId, actor, true) + const operation = await invokeMcpTool(principal, "mm_change_apply", { planId: plan.planId, idempotencyKey: key }) as { id: string } + pendingOperations.add(operation.id) + for (let attempt = 0; attempt < 600; attempt++) { + const result = await invokeMcpTool(principal, "mm_operation_get", { operationId: operation.id }) as { state: string; verification: string; steps: { state: string }[] } + if (!["pending", "running"].includes(result.state)) { + pendingOperations.delete(operation.id) + if (expected === "applied") { + assert.equal(result.state, "applied", JSON.stringify(result)) + assert.equal(result.verification, "passed") + assert.equal(result.steps.length, 1) + assert.ok(result.steps.every(step => step.state === "verified")) + } else { + assert.equal(result.state, expected === "stale" ? "rejected" : "unknown", JSON.stringify(result)) + assert.deepEqual(await state(), before, "Refused race/stale plan leaves SQL ownership unchanged") + const stopped = (await pool.query("SELECT error_code,result FROM mcp_operations WHERE id=$1", [operation.id])).rows[0] + assert.equal(stopped.result.confirmedWrites, 0) + assert.equal(stopped.error_code, expected === "stale" ? "STATE_CONFLICT" : "DEVICE_UNAVAILABLE") + } + assert.equal((await invokeMcpTool(principal, "mm_change_apply", { planId: plan.planId, idempotencyKey: key }) as { id: string }).id, operation.id) + return + } + await delay(25) + } + throw Error("User binding operation remains live; never replay") + } + await apply("mm_users_create_plan", { user: { login, name: "Owned user binding", email: "binding@example.invalid" } }) + userId = (await state()).users[0]?.id + assert.ok(userId) + assert.equal((await getUser()).login, login) + assert.deepEqual((await getUser()).bindings, []) + await apply("mm_users_create_plan", { user: { login: otherLogin, name: "Other owned user", email: "other@example.invalid" } }) + otherUserId = (await state()).users.find(row => row.login === otherLogin)?.id + assert.ok(otherUserId) + const invalidSelectors = [ + { interfaceName: "missing-native-interface" }, + { interfaceName: "wg-owned-user", interfaceType: "ether" }, + { interfaceName: "wg-owned-user", peerPublicKey: Buffer.alloc(32, 9).toString("base64") }, + { interfaceName: "ipsec-vpn", interfaceType: "ipsec", peerPublicKey: "missing-native-cn" }, + ] + for (const [index, binding] of invalidSelectors.entries()) { + const before = await state(), plansBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n + await assert.rejects(invokeMcpTool(principal, "mm_users_binding_add_plan", { id: userId, binding: { serverId, ...binding } }), matches(index === 1 ? "STATE_CONFLICT" : "NOT_FOUND")) + assert.deepEqual(await state(), before) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n, plansBefore) + } + type CatalogEntry = { name: string; type: string; boundUserId: string | null; boundUserLogin: string | null; peers?: { publicKey: string; boundUserId: string | null; boundUserLogin: string | null }[] } + const catalog = async () => (await invokeMcpTool(principal, "mm_user_interfaces_list", { serverId }) as { interfaces: CatalogEntry[] }).interfaces + const owner = async (fixture: typeof fixtures[number]) => { + const iface = (await catalog()).find(entry => entry.name === fixture.name) + assert.ok(iface, fixture.name + " appears in catalog") + assert.equal(iface.type, fixture.type) + const entry = fixture.peerPublicKey ? iface.peers?.find(peer => peer.publicKey === fixture.peerPublicKey) : iface + assert.ok(entry, fixture.name + " peer appears in catalog") + return { id: entry.boundUserId, login: entry.boundUserLogin } + } + const catalogState = await invokeMcpTool(principal, "mm_user_interfaces_state_get", { serverId }) as { source: string; inventoryComplete: boolean; failures: unknown[] } + assert.equal(catalogState.source, "snapshot") + assert.equal(catalogState.inventoryComplete, true) + assert.deepEqual(catalogState.failures, []) + const catalogTools = ["mm_user_interfaces_list", "mm_user_interfaces_state_get"] + for (const tool of catalogTools) { + await assert.rejects(invokeMcpTool(narrow, tool, { serverId }), matches("SERVER_DENIED")) + } + try { + catalogMode = "upstream-error" + const publicState = interfaceCatalogStateSchema.parse(await getInterfaceCatalogState(serverId)) + assert.deepEqual(publicState.failures.map(failure => failure.resource), ["wireguard", "ipsec"]) + assert.equal(publicState.inventoryComplete, true) + assert.ok(publicState.interfaces.find(iface => iface.name === "wg-owned-user")?.peersError) + assert.ok(publicState.interfaces.find(iface => iface.name === "ipsec-vpn")?.peersError) + assert.ok(!JSON.stringify(publicState).includes(providerCanary)) + const legacy = await listInterfaceCatalog(serverId) + assert.ok(legacy.find(iface => iface.name === "wg-owned-user")?.peersError) + assert.ok(!JSON.stringify(legacy).includes(providerCanary)) + for (const tool of catalogTools) { + const result = await invokeMcpTool(readOnly, tool, { serverId }) + assert.ok(!JSON.stringify(result).includes(providerCanary)) + } + + catalogMode = "duplicate" + assert.equal(interfaceCatalogStateSchema.safeParse(await getInterfaceCatalogState(serverId)).success, false) + for (const tool of catalogTools) { + await assert.rejects(invokeMcpTool(readOnly, tool, { serverId }), matches("STATE_UNAVAILABLE")) + } + + catalogMode = "many-peers" + assert.equal((await listInterfaceCatalog(serverId)).find(iface => iface.name === "wg-owned-user")?.peers?.length, MAX_MCP_PUBLIC_ARRAY_ITEMS + 1) + for (const tool of catalogTools) { + await assert.rejects(invokeMcpTool(readOnly, tool, { serverId }), matches("BUDGET_EXCEEDED")) + } + + catalogMode = "normal" + await replaceSnapshot(Array.from({ length: MAX_MCP_PUBLIC_ARRAY_ITEMS + 1 }, (_, i) => ({ ...snapshot[0], name: `ether-owned-catalog-${i}` }))) + assert.equal(interfaceCatalogStateSchema.parse(await getInterfaceCatalogState(serverId)).inventoryComplete, true) + for (const tool of catalogTools) { + await assert.rejects(invokeMcpTool(readOnly, tool, { serverId }), matches("BUDGET_EXCEEDED")) + } + } finally { + catalogMode = "normal" + await replaceSnapshot(snapshot) + } + for (const fixture of fixtures) { + // Infer ordinary/WG types from native rows; verify the service's canonical + // name/key, including ordinary-interface peer clearing, after real INSERT. + const binding = { serverId, interfaceName: ` ${fixture.name} `, peerPublicKey: fixture.peerPublicKey ? ` ${fixture.peerPublicKey} ` : " ignored-non-peer ", ...(fixture.type === "ipsec" ? { interfaceType: "ipsec" } : {}) } + assert.deepEqual(await owner(fixture), { id: null, login: null }) + const beforeDenied = await state() + const plansBefore = (await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n + await assert.rejects(invokeMcpTool(narrow, "mm_users_binding_add_plan", { id: userId, binding }), matches("SERVER_DENIED")) + await assert.rejects(invokeMcpTool(readOnly, "mm_users_binding_add_plan", { id: userId, binding }), matches("CONNECTION_PERMISSION_MISSING")) + assert.deepEqual(await state(), beforeDenied) + assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans")).rows[0].n, plansBefore, "Denied requests create no plans") + // Preparation cannot reserve the tuple: another owner can bind it before apply. + const racedInput = { id: otherUserId, binding } + const racedPlan = await invokeMcpTool(principal, "mm_users_binding_add_plan", racedInput) as { planId: string; state: string } + await apply("mm_users_binding_add_plan", { id: userId, binding }) + const bound = (await getUser()).bindings + assert.equal(bound.length, 1) + assert.equal(bound[0].serverId, serverId) + assert.equal(bound[0].interfaceName, fixture.name) + assert.equal(bound[0].interfaceType, fixture.type) + assert.equal(bound[0].peerPublicKey, fixture.peerPublicKey ?? "") + assert.deepEqual(await owner(fixture), { id: userId, login }) + const hidden: { incomplete: boolean; bindings: unknown[] } = (await invokeMcpTool(narrow, "mm_users_get", { id: userId }) as { user: { incomplete: boolean; bindings: unknown[] } }).user + assert.equal(hidden.incomplete, true) + assert.deepEqual(hidden.bindings, []) + const beforeConflict = await state() + const counts = async () => (await pool.query("SELECT (SELECT count(*)::int FROM mcp_plans) AS plans,(SELECT count(*)::int FROM mcp_operations) AS operations")).rows[0] + const beforeCounts = await counts() + if (fixture === fixtures[0]) { + const readsBefore = nativeReads + await assert.rejects(invokeMcpTool(principal, "mm_users_binding_remove_plan", { id: userId, bindingId: randomUUID() }), matches("NOT_FOUND")) + await assert.rejects(invokeMcpTool(principal, "mm_users_binding_remove_plan", { id: otherUserId, bindingId: bound[0].id }), matches("NOT_FOUND")) + assert.equal(nativeReads, readsBefore, "Invalid removal is refused from the current user snapshot") + assert.deepEqual(await counts(), beforeCounts, "Invalid removal creates no plan or operation") + assert.deepEqual(await state(), beforeConflict) + } + for (const id of [userId, otherUserId]) { + await assert.rejects(invokeMcpTool(principal, "mm_users_binding_add_plan", { id, binding }), matches("STATE_CONFLICT")) + } + await assert.rejects(invokeMcpTool(narrow, "mm_users_binding_add_plan", { id: userId, binding }), matches("SERVER_DENIED")) + await assert.rejects(invokeMcpTool(readOnly, "mm_users_binding_add_plan", { id: userId, binding }), matches("CONNECTION_PERMISSION_MISSING")) + if (fixture.peerPublicKey) { + await assert.rejects(invokeMcpTool(principal, "mm_users_binding_add_plan", { id: userId, binding: { ...binding, peerPublicKey: "" } }), matches("INVALID_INPUT")) + } + assert.deepEqual(await counts(), beforeCounts, "Deterministic duplicate/takeover creates no plan or operation") + assert.deepEqual(await state(), beforeConflict) + // REST's shared SQL service retains its 409 contract. + for (const id of [userId, otherUserId]) { + await assert.rejects(addBinding(id, userBindingCreateSchema.parse(binding)), (error: unknown) => (error as { status?: number }).status === 409) + } + assert.deepEqual(await state(), beforeConflict) + await apply("mm_users_binding_add_plan", racedInput, "race", racedPlan) + assert.deepEqual(await owner(fixture), { id: userId, login }) + const beforeRemoveDenied = await state() + for (const tool of ["mm_users_binding_remove_plan", "mm_users_delete_plan"]) { + await assert.rejects(invokeMcpTool(narrow, tool, tool.includes("binding") ? { id: userId, bindingId: bound[0].id } : { id: userId }), matches("SERVER_DENIED")) + } + assert.deepEqual(await state(), beforeRemoveDenied) + const removeInput = { id: userId, bindingId: bound[0].id } + const staleRemoval = await invokeMcpTool(principal, "mm_users_binding_remove_plan", removeInput) as { planId: string; state: string } + await apply("mm_users_binding_remove_plan", removeInput) + assert.deepEqual((await getUser()).bindings, []) + assert.deepEqual(await owner(fixture), { id: null, login: null }) + await apply("mm_users_binding_remove_plan", removeInput, "stale", staleRemoval) + assert.deepEqual(await owner(fixture), { id: null, login: null }) + } + await apply("mm_users_delete_plan", { id: otherUserId }) + await apply("mm_users_delete_plan", { id: userId }) + await assert.rejects(invokeMcpTool(principal, "mm_users_get", { id: userId }), matches("NOT_FOUND")) + assert.deepEqual(await state(), { users: [], bindings: [] }) + assert.ok(nativeReads > 0, "Catalog consulted loopback RouterOS fixture") + assert.equal(nativeWrites, 0, "User binding workflow never mutates RouterOS") + console.log("✓ MCP users: eight native binding types plus IPsec PSK, missing/type-spoof/peer/CN refusal, add/read/remove, catalog owner, duplicate/takeover refusal, approval/replay and scope; owned PostgreSQL/RouterOS stub") + } finally { + // Never race cleanup against an unresolved operation. This helper runs only + // inside the disposable owned profile, whose runner tears down its database. + if (pendingOperations.size === 0) { + for (const id of connections) await setConnectionStatus(id, "paused", actor) + await pool.query("DELETE FROM app_users WHERE login=ANY($1::text[])", [[login, otherLogin]]) + await pool.query("DELETE FROM servers WHERE id=$1", [serverId]) + } + await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve())) + } +} diff --git a/backend/src/test/partition-timezones.ts b/backend/src/test/partition-timezones.ts new file mode 100644 index 00000000..eca7d2ab --- /dev/null +++ b/backend/src/test/partition-timezones.ts @@ -0,0 +1,50 @@ +import assert from "node:assert/strict" +import type { Pool } from "pg" +import { ensurePartitionFor, type PartitionKind } from "../db/partitions.js" +import { withPartitionWrite, PartitionWriteError } from "../services/partition-write-transaction.js" + +/** Transactional SQL acceptance: leaves no test tables or session settings. */ +export async function verifyPartitionTimezones(pool: Pool): Promise { + let completed=false,failure:unknown + try { + await withPartitionWrite(async({client})=>{ + try{ + const cases: { kind: PartitionKind; start: string; end: string; middle: string }[] = [ + { kind: "day", start: "2026-10-19T00:00:00Z", end: "2026-10-20T00:00:00Z", middle: "2026-10-19T17:55:00Z" }, + { kind: "week", start: "2026-10-19T00:00:00Z", end: "2026-10-26T00:00:00Z", middle: "2026-10-25T23:55:00Z" }, + { kind: "month", start: "2028-02-01T00:00:00Z", end: "2028-03-01T00:00:00Z", middle: "2028-02-29T23:55:00Z" }, + ] + for (const fixture of cases) { + const parent = `mcp_partition_tz_${fixture.kind}` + await client.query(`CREATE TABLE ${parent} (sampled_at timestamptz NOT NULL) PARTITION BY RANGE (sampled_at)`) + // Create in UTC+7, then reuse in other zones, including a DST zone. + for (const zone of ["Asia/Novosibirsk", "UTC", "America/New_York"]) { + await client.query("SELECT set_config('TimeZone', $1, true)", [zone]) + const partition = await ensurePartitionFor(client, parent, fixture.kind, new Date(fixture.middle)) + const next = await ensurePartitionFor(client, parent, fixture.kind, new Date(fixture.end)) + const boundary = new Date(Date.parse(fixture.end) - 1).toISOString() + for (const instant of [fixture.start, fixture.middle, boundary, fixture.end]) { + const { rows } = await client.query<{ leaf: string; epoch: number }>( + `INSERT INTO ${parent} VALUES ($1) RETURNING tableoid::regclass::text AS leaf, extract(epoch FROM sampled_at)::double precision AS epoch`, [instant], + ) + assert.equal(rows[0]?.leaf, instant === fixture.end ? next : partition, `${fixture.kind} ${zone} ${instant}`) + assert.equal(rows[0]?.epoch, Date.parse(instant) / 1000) + } + } + } + // DATE-keyed monthly aggregates retain date semantics with ISO UTC bounds. + await client.query("CREATE TABLE mcp_partition_tz_date (day date NOT NULL) PARTITION BY RANGE (day)") + await ensurePartitionFor(client, "mcp_partition_tz_date", "month", new Date("2028-02-29T23:55:00Z")) + await client.query("INSERT INTO mcp_partition_tz_date VALUES ('2028-02-01'), ('2028-02-29')") + }catch(error){failure=error;throw error} + completed=true + // This acceptance is rollback-only. Destroying the owned backend aborts its + // uncommitted fixture DDL, retaining the original zero-artifact contract. + throw Error("Owned timezone fixture intentionally rolls back") + },{connect:()=>pool.connect(),deadlineAt:Date.now()+120000}) + }catch(error){ + if(failure)throw failure + assert(completed&&error instanceof PartitionWriteError&&!error.outcomeUnknown) + } + console.log("✓ UTC partition boundaries: day/week/leap month, next boundary, DATE keys and PostgreSQL UTC+7/UTC/DST zones") +} diff --git a/backend/src/test/routing-controller-baseline-acceptance-main.ts b/backend/src/test/routing-controller-baseline-acceptance-main.ts new file mode 100644 index 00000000..288906d9 --- /dev/null +++ b/backend/src/test/routing-controller-baseline-acceptance-main.ts @@ -0,0 +1,140 @@ +import assert from "node:assert/strict" +import { fork } from "node:child_process" +import { readFile, unlink, writeFile } from "node:fs/promises" +import { resolve } from "node:path" +import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client" +import fastify from "fastify" +import mcpTransport from "../routes/mcp-transport.js" +import { pool, closePool } from "../db/index.js" +import { applySqlMigrations } from "../db/migrate.js" +import { updateMcpSettings } from "../modules/mcp/store.js" +import { acceptMainBaselineRuntime } from "../modules/mcp/routing-controller-baseline-acceptance.js" +import { acceptScopedRouteCommunitySwitch } from "../modules/mcp/routing-community-scoped-acceptance.js" +import type { AuthUser } from "../lib/permissions.js" + +// This file is an owned test entry, never a model tool or application startup. +const url = new URL(process.env.DATABASE_URL ?? "http://invalid") +if (process.env.MMAPP_ROUTING_BASELINE_OWN_SQL !== "true" || url.hostname !== "127.0.0.1" || url.username !== "mcp_test" || + url.pathname !== "/postgres" || !url.port || url.port === "5432" || process.env.NODE_ENV !== "test" || + process.env.MCP_STATE_DIR !== resolve(process.cwd(), "state")) throw new Error("Requires its own disposable routing baseline SQL runner") +const mode = process.env.MMAPP_ROUTING_BASELINE_MODE +if (!["main-and-forward-rounds", "main-only", "control-path-context", "default-scoped", "scoped-bare-readback", "scoped-resume-forward", "scoped-inverse-expiry", + "scoped-inverse-post-probe-expiry", "scoped-inverse-checkpoint-drift", "scoped-inverse-route-effects", "scoped-inverse-effects-checkpoint-drift", "scoped-inverse-lost-ack", "manual-forward-expiry", "manual-forward-post-intent-expiry"].includes(mode ?? "")) throw new Error("Unknown owned acceptance mode") +const actor: AuthUser = { id: "targeted-baseline-owner", email: "test@example.invalid", name: "Targeted", apps: ["mm"], + permissions: ["mm:settings:admin", "mm:certificates:write", "mm:network:admin", "mm:bgp:admin", "mm:servers:admin", "mm:filters:admin"], expiresAt: Date.now() + 3600000 } + +async function resumeForward({ token, planId, connectionId }: { token: string; planId: string; connectionId: string }) { + const app = fastify() + try { + await app.register(mcpTransport) + await app.listen({ host: "127.0.0.1", port: 0 }) + const address = app.server.address() + assert(address && typeof address === "object") + const run = (phase: "dispatch" | "resume") => new Promise((resolveRun, reject) => { + const child = fork(resolve("mcp-routing-forward-observer.mjs"), [phase, String(address.port), planId, connectionId], { + execArgv: [], stdio: ["ignore", "inherit", "inherit", "ipc"], + env: { SystemRoot: process.env.SystemRoot, WINDIR: process.env.WINDIR, MCP_FIXTURE_TOKEN: token }, + }) + let receipt: string | undefined + let terminatedAfterReceipt = false + let timedOut = false + const timer = setTimeout(() => { timedOut = true; child.kill() }, 75_000) + child.on("message", message => { + if (receipt || typeof message !== "string" || !/^[0-9a-f-]{36}$/.test(message)) return + receipt = message + if (phase === "dispatch") terminatedAfterReceipt = child.kill() + }) + child.once("error", error => { clearTimeout(timer); reject(error) }) + child.once("exit", code => { + clearTimeout(timer) + if (!timedOut && receipt && (phase === "dispatch" ? terminatedAfterReceipt && code !== 0 : code === 0)) resolveRun(receipt) + else reject(new Error("Owned routing observer did not complete phase " + phase)) + }) + }) + const operationId = await run("dispatch") + const journal = resolve("routing-forward.jsonl") + const receipts = async () => (await readFile(journal, "utf8")).trimEnd().split("\n").map(line => JSON.parse(line)) + const before = await receipts() + assert.deepEqual(before.map(row => row.phase), ["fixture", "started", "accepted"]) + assert.equal(before[0].fixture, `routing-scoped-forward:${connectionId}:${planId}`) + assert.equal(before[2].evidence, operationId) + // Only the exited child owned this lock in the disposable fixture directory. + await unlink(journal + ".lock") + assert.equal(await run("resume"), operationId) + const after = await receipts() + assert.deepEqual(after.slice(0, -1), before) + assert.deepEqual(after.at(-1), { id: "forward-apply", attempt: 1, phase: "passed", evidence: operationId }) + const { rows: [counts] } = await pool.query(`SELECT count(*) FILTER (WHERE tool='mm_change_apply')::int AS applies, + count(*) FILTER (WHERE tool='mm_operation_get')::int AS reads FROM mcp_requests WHERE connection_id=$1`, [connectionId]) + assert.equal(counts.applies, 1) + assert(counts.reads > 0) + return operationId + } finally { + await app.close() + } +} + +async function callInverseOverHttp(token: string, name: string, input: Record) { + const app = fastify() + const client = new Client({ name: "owned-routing-inverse", version: "1.0.0" }) + try { + await app.register(mcpTransport) + const address = await app.listen({ host: "127.0.0.1", port: 0 }) + await client.connect(new StreamableHTTPClientTransport(new URL("/mcp", address), { + requestInit: { headers: { Authorization: `Bearer ${token}` } }, + })) + if (name === "mm_operation_recovery_plan") { + const { tools } = await client.listTools() + const tool = tools.find(tool => tool.name === name) + assert(tool) + assert.match(JSON.stringify(tool.inputSchema.properties?.observation), /"route-effects-v1"/) + assert.ok(!tool.inputSchema.required?.includes("observation")) + } + const reply = await client.callTool({ name, arguments: input }) + assert.ok(!reply.isError, `MCP fixture call failed: ${name}`) + assert.ok(reply.structuredContent && typeof reply.structuredContent === "object" && "result" in reply.structuredContent) + return reply.structuredContent.result + } finally { + try { + await client.close() + } finally { + await app.close() + } + } +} + +let failed = false +try { + await applySqlMigrations(pool) + await pool.query("INSERT INTO geoip_settings(id,enabled) VALUES(1,false) ON CONFLICT(id) DO UPDATE SET enabled=false") + await updateMcpSettings({ enabled: true }, actor) + const main = mode === "main-only" || mode === "main-and-forward-rounds" + const controlPath = mode === "control-path-context" + const scopedSkipped = mode === "main-only" || controlPath + if (main) await acceptMainBaselineRuntime(actor) + if (controlPath) await (await import("../modules/mcp/control-path-context-acceptance.js")).verifyMcpControlPathContext(actor) + const scoped = scopedSkipped ? undefined : await acceptScopedRouteCommunitySwitch(actor, + mode === "main-and-forward-rounds" ? { forwardRoundsOnly: true } : + mode === "manual-forward-expiry" ? { manualForwardExpiry: "before-intent" } : + mode === "manual-forward-post-intent-expiry" ? { manualForwardExpiry: "after-intent" } : + mode === "scoped-resume-forward" ? { bareReadbackOnly: true, forwardCheckpoint: resumeForward } : + mode === "scoped-inverse-route-effects" ? { bareReadbackOnly: true, inverseRouteEffects: true, inverseToolCall: callInverseOverHttp } : + mode === "scoped-inverse-effects-checkpoint-drift" ? { bareReadbackOnly: true, inverseRouteEffects: true, inverseEffectsCheckpointDrift: true } : + mode === "scoped-bare-readback" || mode === "scoped-inverse-expiry" || mode === "scoped-inverse-post-probe-expiry" || mode === "scoped-inverse-checkpoint-drift" || mode === "scoped-inverse-lost-ack" + ? { bareReadbackOnly: true, expireDuringInverseProbe: mode === "scoped-inverse-expiry", + expireDuringInversePostProbe: mode === "scoped-inverse-post-probe-expiry", + inverseCheckpointDrift: mode === "scoped-inverse-checkpoint-drift", + loseInverseAck: mode === "scoped-inverse-lost-ack" } : {}) + await writeFile(resolve(process.cwd(), "runtime-results.json"), JSON.stringify({ passed: true, mode, productionApplied: false, + mainPublicActivation: main, privateDriftNativeWrites: main ? 0 : undefined, publicOverlapTOCTOU: main ? true : undefined, scopedSkipped, + controlPathContext: controlPath ? true : undefined, + scopedResult: scoped, defaultScopedInverseSuite: mode === "default-scoped" }, null, 2)) +} catch (error) { + failed = true; console.error(error) + await writeFile(resolve(process.cwd(), "runtime-failure.json"), JSON.stringify({ error: String(error), stack: error instanceof Error ? error.stack : undefined }, null, 2)) +} finally { + await (await import("../services/scheduler.js")).stopSchedulerForDatabaseOperation() + await (await import("../services/traffic-flow-ingest.js")).stopTrafficFlowListenerForDatabaseOperation() + await closePool() +} +if (failed) process.exitCode = 1 diff --git a/backend/src/test/sqlite-import-cli-admission.ts b/backend/src/test/sqlite-import-cli-admission.ts new file mode 100644 index 00000000..1bb3d535 --- /dev/null +++ b/backend/src/test/sqlite-import-cli-admission.ts @@ -0,0 +1,66 @@ +import assert from "node:assert/strict" +import { execFile } from "node:child_process" +import { mkdir } from "node:fs/promises" +import { join } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { fileURLToPath } from "node:url" +import { promisify } from "node:util" +import Database from "better-sqlite3" +import { pool } from "../db/index.js" +import { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK, withSystemDatabaseOperationLock } from "../db/operation-lock.js" +import { mcpStateDirectory } from "../modules/mcp/security.js" + +export async function verifySqliteImportCliAdmission() { + assert.equal(process.env.MMAPP_OWN_SQLITE_IMPORT_CLI_ONLY, "true") + const directory = mcpStateDirectory() + await mkdir(directory, { recursive: true }) + const sqlitePath = join(directory, "owned-import.sqlite") + const sqlite = new Database(sqlitePath) + try { + sqlite.exec(`CREATE TABLE traffic_settings(id INT, enabled INT, interval_sec INT, retention_days INT, created_at TEXT, updated_at TEXT); + INSERT INTO traffic_settings VALUES(1,0,31,12,'2026-01-01','2026-01-01');`) + } finally { sqlite.close() } + + const cli = fileURLToPath(new URL("../scripts/migrate-sqlite-to-pg.ts", import.meta.url)) + const runImport = promisify(execFile) + const args = ["--import", "tsx", cli, `--sqlite=${sqlitePath}`] + await assert.rejects(runImport(process.execPath, [...args, "--dry-run"], { + timeout: 20000, + windowsHide: true, + env: { ...process.env, DATABASE_RUNTIME_REQUEST_FILE: join(directory, "unread-runtime-request.json") }, + }), /Импорт SQLite требует отдельного процесса оператора PostgreSQL/) + + const blocker = await pool.connect() + let child: Promise<{ stdout: string; stderr: string }> | undefined + try { + await blocker.query("BEGIN") + const { rows: [before] } = await blocker.query("SELECT interval_sec FROM traffic_settings WHERE id=1 FOR UPDATE") + assert(before && before.interval_sec !== 31) + child = runImport(process.execPath, args, { timeout: 20000, windowsHide: true }) + void child.catch(() => {}) + let copying = false + for (let i = 0; i < 240 && !copying; i++) { + const { rowCount } = await pool.query("SELECT 1 FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE 'INSERT INTO traffic_settings (%'") + copying = !!rowCount + if (!copying) await Promise.race([delay(15), child.then(() => assert.fail("CLI exited before the held SQLite import write"))]) + } + assert(copying, "Expected the real SQLite importer to reach its held PostgreSQL write") + const { rows } = await pool.query(`SELECT objid::int AS key FROM pg_locks + WHERE locktype='advisory' AND classid=0 AND objsubid=1 AND mode='ExclusiveLock' AND granted + AND objid=ANY($1::oid[]) ORDER BY objid`, [[SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK]]) + assert.deepEqual(rows.map(row => row.key), [SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK]) + assert.deepEqual((await blocker.query("SELECT interval_sec FROM traffic_settings WHERE id=1")).rows, [before]) + await blocker.query("ROLLBACK") + } finally { + try { await blocker.query("ROLLBACK") } finally { blocker.release() } + await child?.catch(() => {}) + } + const output = await child + assert.match(output?.stdout ?? "", /"copied": 1/) + await withSystemDatabaseOperationLock(() => pool.connect(), async () => { + const { rows } = await pool.query("SELECT interval_sec,enabled FROM traffic_settings WHERE id=1") + assert.deepEqual(rows, [{ interval_sec: 31, enabled: false }]) + assert.equal((await pool.query("SELECT 1 FROM data_migration WHERE id=1 AND sqlite_imported_at IS NOT NULL")).rowCount, 1) + }) + console.log("SQLite CLI: request drain and DB operation lock retained through actual import; rows and import marker verified") +} diff --git a/backend/src/test/terminal-diagnostic-fixture.ts b/backend/src/test/terminal-diagnostic-fixture.ts new file mode 100644 index 00000000..bc170f0a --- /dev/null +++ b/backend/src/test/terminal-diagnostic-fixture.ts @@ -0,0 +1,45 @@ +import { createServer, type IncomingMessage, type ServerResponse } from "node:http" +import type { Socket } from "node:net" + +export const terminalDiagnosticCases = [ + ["help", "help"], ["?", "help"], ["/IP ADDRESS PRINT", "addresses"], ["interface print", "interfaces"], ["system resource print", "resources"], ["system identity print", "identity"], ["ip route print", "routes"], ["ip firewall filter print", "firewall_filters"], + ["routing bgp session print", "bgp_sessions"], ["bgp session print", "bgp_sessions"], ["bgp peer print", "bgp_sessions"], ["routing ospf neighbor print", "ospf_neighbors"], ["ospf neighbor print", "ospf_neighbors"], ["routing ospf area print", "ospf_areas"], ["ospf area print", "ospf_areas"], ["routing ospf instance print", "ospf_instances"], ["ospf instance print", "ospf_instances"], ["routing ospf interface print", "ospf_interfaces"], ["routing ospf interface-template print", "ospf_interfaces"], ["routing bfd session print", "bfd_sessions"], ["bfd session print", "bfd_sessions"], ["log print limit=500", "logs"], ["ping 192.0.2.1 count=99", "ping"], +] as const +export async function createTerminalDiagnosticFixture() { + const requests: { path: string; method: string; body?: unknown }[] = [], sockets = new Set() + const payloads: Record = { + "/system/identity": { name: "owned-router" }, "/system/resource": [{ "total-memory": "0", uptime: "bad-child" }, { "total-memory": "1048576", "free-memory": "524288", version: "7.20", uptime: "1h", "board-name": "fixture" }], + "/ip/address": [{ ".id": "*1", address: "192.0.2.1/24", network: "192.0.2.0", interface: "ether1", disabled: "false" }], + "/interface": [{ ".id": "*1", name: "ether1", type: "ether", running: "true", disabled: "false" }], + "/ip/route": [{ ".id": "*1", "dst-address": "192.0.2.0/24", gateway: "ether1", active: "true", distance: "0" }], + "/ip/firewall/filter": [{ ".id": "*1", chain: "input", action: "accept", packets: "0" }], + "/routing/bgp/session": [{ ".id": "*1", name: "owned-bgp", "remote.as": "64512", "remote.address": "192.0.2.2", established: "true", uptime: "1h" }], + "/routing/ospf/neighbor": [{ ".id": "*1", "router-id": "192.0.2.2", state: "Full", interface: "ether1" }], + "/routing/ospf/area": [{ ".id": "*1", name: "owned-area", "area-id": "0.0.0.0", instance: "owned-instance" }], + "/routing/ospf/instance": [{ ".id": "*1", name: "owned-instance", "router-id": "192.0.2.1", version: "2" }], + "/routing/ospf/interface-template": [{ ".id": "*1", interfaces: "ether1", area: "owned-area", cost: "10", type: "broadcast" }], + "/routing/bfd/session": [{ ".id": "*1", "local-address": "192.0.2.1", "remote-address": "192.0.2.2", state: "up", uptime: "1h" }], + "/log": Array.from({ length: 201 }, (_, i) => ({ ".id": `*${i.toString(16)}`, time: String(i), topics: "system,info", message: `log-${i}` })), + "/tool/ping": [{ seq: "0", time: "0ms", ttl: "64" }, { sent: "20", received: "20", "packet-loss": "0", "avg-rtt": "0ms" }], + } + let beforeResponse: ((request: IncomingMessage, response: ServerResponse, path: string) => Promise) | undefined + const server = createServer(async (request, response) => { + const url = new URL(request.url!, "http://127.0.0.1"), route = url.pathname.replace(/^\/rest/, "") + let body: unknown + if (request.method === "POST") { const chunks: Buffer[] = []; for await (const chunk of request) chunks.push(Buffer.from(chunk)); body = JSON.parse(Buffer.concat(chunks).toString()) } + requests.push({ path: route, method: request.method!, ...(body ? { body } : {}) }) + try { + if (await beforeResponse?.(request, response, route)) return + response.setHeader("Content-Type", "application/json") + if (route.endsWith("/get")) { response.end(JSON.stringify({ ret: "false" })); return } + if (route.endsWith("/print")) { response.end(JSON.stringify((body as { ".query": string[] })[".query"].length === 1 ? [] : [{ ".id": "*1" }])); return } + if (!(route in payloads)) { response.statusCode = 404; response.end("{}"); return } + response.end(JSON.stringify(payloads[route])) + } catch { response.destroy() } + }) + server.on("connection", socket => { sockets.add(socket); socket.once("close", () => sockets.delete(socket)) }) + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)) + const address = server.address(); if (!address || typeof address === "string") throw Error("Missing fixture port") + return { payloads, requests, port: address.port, setBeforeResponse(value: typeof beforeResponse) { beforeResponse = value }, + async close() { for (const socket of sockets) socket.destroy(); await new Promise(resolve => server.close(() => resolve())) } } +} diff --git a/backend/src/test/traffic-flow-chr-capture-check.test.ts b/backend/src/test/traffic-flow-chr-capture-check.test.ts new file mode 100644 index 00000000..9e8dcbd3 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-capture-check.test.ts @@ -0,0 +1,119 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { checkChrCapture } from "./traffic-flow-chr-capture-check.js" + +function fixture() { + const exporters = [ + { address: "192.0.2.10", port: 2055, version: 9, domain: 7, templateId: 256 }, + { address: "192.0.2.20", port: 2055, version: 10, domain: 7, templateId: 256 }, + ] + const expected = { src: "192.0.2.1", dst: "198.51.100.1", bytes: 1200, packets: 3, inIface: "11", outIface: "12" } + const fields = [8, 12, 1, 2, 10, 14] + const data = Buffer.alloc(24) + ;[0xc0000201, 0xc6336401, 1200, 3, 11, 12].forEach((value, index) => data.writeUInt32BE(value, index * 4)) + const frames = (["template", "data", "refresh", "data"] as const).flatMap((kind, phase) => exporters.map((source, exporter) => { + const isTemplate = kind === "template" || kind === "refresh" + const reversed = kind === "refresh" || (kind === "template" && exporter === 1) + const body = isTemplate ? Buffer.alloc(28) : data + if (isTemplate) { + body.writeUInt16BE(source.templateId) + body.writeUInt16BE(fields.length, 2) + const ordered = reversed ? [12, 8, ...fields.slice(2)] : fields + ordered.forEach((id, index) => { body.writeUInt16BE(id, 4 + index * 4); body.writeUInt16BE(4, 6 + index * 4) }) + } + const header = source.version === 9 ? 20 : 16 + const packet = Buffer.alloc(header + 4 + body.length) + packet.writeUInt16BE(source.version) + packet.writeUInt16BE(source.version === 9 ? 1 : packet.length, 2) + packet.writeUInt32BE(source.domain, header - 4) + packet.writeUInt16BE(isTemplate ? source.version === 9 ? 0 : 2 : source.templateId, header) + packet.writeUInt16BE(body.length + 4, header + 2) + body.copy(packet, header + 4) + return { + exporter, kind: kind as string, ordinal: phase * 2 + exporter + 1, receivedMs: phase + 1, payloadHex: packet.toString("hex"), + expected: isTemplate ? [] : [phase === 3 || exporter === 1 ? { ...expected, src: expected.dst, dst: expected.src } : { ...expected }], + } + })) + return { destination: "192.0.2.100", exporters, frames } +} + +test("interleaved v9 and IPFIX replay matches exact counters and ifIndex after same-port template reuse", () => { + const result = checkChrCapture(fixture()) + assert.equal(result.captureReplayMatched, true) + assert.deepEqual(result.exporters.map(row => [row.baselineRecords, row.refreshedRecords]), [[1, 1], [1, 1]]) + assert.equal(result.captureProvenanceVerified, false) + assert.equal(result.restartIsolationAccepted, false) +}) + +test("refuses swapped exporter identity, altered ifIndex, unordered frames and missing refreshed traffic", () => { + const cases = [ + (input: ReturnType) => { input.frames[2].exporter = 1 }, + (input: ReturnType) => { input.frames[2].expected[0].inIface = "99" }, + (input: ReturnType) => { input.frames[2].ordinal = input.frames[1].ordinal }, + (input: ReturnType) => { input.frames[7] = { ...input.frames[5], ordinal: 8, receivedMs: 8 } }, + ] + for (const mutate of cases) { + const input = fixture() + mutate(input) + assert.throws(() => checkChrCapture(input)) + } +}) + +test("same-tuple pre-refresh packet still decodes with previous template and refuses restart evidence", () => { + const input = fixture() + input.frames.splice(4, 0, { ...input.frames[2], kind: "pre-refresh-probe", expected: [] }) + input.frames.forEach((frame, index) => { frame.ordinal = index + 1; frame.receivedMs = index + 1 }) + assert.throws(() => checkChrCapture(input), /reused the previous template/) + assert.equal(checkChrCapture(fixture()).captureReplayMatched, true) +}) + +function concatenateSets(version: number, first: string, second: string, reversed = false) { + const header = version === 9 ? 20 : 16 + const left = Buffer.from(first, "hex"), right = Buffer.from(second, "hex") + const values = [left.subarray(header), right.subarray(header)] + if (reversed) values.reverse() + const packet = Buffer.concat([left.subarray(0, header), ...values]) + packet.writeUInt16BE(version === 9 ? 2 : packet.length, 2) + return packet +} + +function bundledFixture() { + const input = fixture() + input.frames = [0, 1, 4, 5].map((index, ordinal) => { + const first = input.frames[index], second = input.frames[index + 2] + return { ...first, ordinal: ordinal + 1, expected: second.expected, + payloadHex: concatenateSets(input.exporters[first.exporter].version, first.payloadHex, second.payloadHex).toString("hex") } + }) + return input +} + +test("v9 and IPFIX template plus data datagrams prove both phases with the current product parser", () => { + const result = checkChrCapture(bundledFixture()) + assert.equal(result.frames, 4) + assert.deepEqual(result.exporters.map(row => [row.baselineRecords, row.refreshedRecords]), [[1, 1], [1, 1]]) + const multipleData = fixture() + const row = multipleData.frames[3] + row.payloadHex = concatenateSets(10, row.payloadHex, row.payloadHex).toString("hex") + row.expected = [...row.expected, ...row.expected] + assert.equal(checkChrCapture(multipleData).exporters[1].baselineRecords, 2) +}) + +test("rejects stale data before a refresh, foreign sets, trailing bytes and v9 counts that hide a set", () => { + const original = fixture() + const reversed = bundledFixture() + reversed.frames[2].payloadHex = concatenateSets(9, original.frames[4].payloadHex, original.frames[6].payloadHex, true).toString("hex") + assert.throws(() => checkChrCapture(reversed), /template order/) + const foreign = bundledFixture() + const foreignPacket = Buffer.from(foreign.frames[1].payloadHex, "hex") + foreignPacket.writeUInt16BE(257, 16 + foreignPacket.readUInt16BE(18)) + foreign.frames[1].payloadHex = foreignPacket.toString("hex") + assert.throws(() => checkChrCapture(foreign), /data set ID/) + const trailing = bundledFixture() + trailing.frames[0].payloadHex += "00" + assert.throws(() => checkChrCapture(trailing), /set count or header/) + const hidden = bundledFixture() + const hiddenPacket = Buffer.from(hidden.frames[0].payloadHex, "hex") + hiddenPacket.writeUInt16BE(1, 2) + hidden.frames[0].payloadHex = hiddenPacket.toString("hex") + assert.throws(() => checkChrCapture(hidden), /v9 set coverage/) +}) diff --git a/backend/src/test/traffic-flow-chr-capture-check.ts b/backend/src/test/traffic-flow-chr-capture-check.ts new file mode 100644 index 00000000..540b8a10 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-capture-check.ts @@ -0,0 +1,170 @@ +/** Offline replay of a reviewed UDP capture projection through the current parser. */ +import { createHash } from "node:crypto" +import { open } from "node:fs/promises" +import { isIP } from "node:net" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { clearFlowTemplateCache, FLOW_TEMPLATE_LIMITS, FlowTemplateRefused, parseFlowPacket } from "../services/traffic-flow-parse.js" + +const maxInput = 512 * 1024 +const ip = z.string().max(45).refine(value => isIP(value) !== 0) +const ipv4 = ip.refine(value => isIP(value) === 4) +const counter = z.number().int().nonnegative().safe() +const iface = z.string().regex(/^(?:0|[1-9][0-9]{0,9})$/) +const flow = z.object({ src: ip, dst: ip, bytes: counter, packets: counter, inIface: iface, outIface: iface }).strict() +const exporter = z.object({ + address: ipv4, port: z.number().int().min(1).max(65535), version: z.union([z.literal(9), z.literal(10)]), + domain: counter.max(0xffffffff), templateId: z.number().int().min(256).max(65535), +}).strict() +const capture = z.object({ + destination: ipv4, + exporters: z.tuple([exporter, exporter]), + frames: z.array(z.object({ + exporter: z.union([z.literal(0), z.literal(1)]), + ordinal: counter, + receivedMs: counter, + payloadHex: z.string().min(40).max(8192).regex(/^(?:[0-9a-f]{2})+$/), + kind: z.enum(["template", "data", "refresh", "pre-refresh-probe"]), + expected: z.array(flow).max(64), + }).strict()).min(4).max(64), +}).strict() + +function requireEvidence(condition: unknown, reason: string): asserts condition { + if (!condition) throw Error(reason) +} + +type Exporter = z.infer +const zeroPadding = (bytes: Buffer) => bytes.length <= 3 && bytes.every(value => value === 0) + +/** Check the entire datagram envelope without decoding record values. */ +function inspectSets(packet: Buffer, source: Exporter, changesTemplate: boolean, previousWidth: number) { + const header = source.version === 9 ? 20 : 16 + requireEvidence(packet.length >= header + 4 && packet.readUInt16BE(0) === source.version && + packet.readUInt32BE(header - 4) === source.domain && + (source.version === 9 || packet.readUInt16BE(2) === packet.length), "Packet does not match the declared exporter") + let width = previousWidth, templateSeen = false, records = 0, sets = 0 + for (let offset = header; offset < packet.length;) { + requireEvidence(packet.length - offset >= 4 && ++sets <= 32, "Invalid set count or header") + const id = packet.readUInt16BE(offset), length = packet.readUInt16BE(offset + 2) + requireEvidence(length >= 4 && offset + length <= packet.length, "Invalid set length") + const body = packet.subarray(offset + 4, offset + length) + offset += length + if (id === (source.version === 9 ? 0 : 2)) { + requireEvidence(changesTemplate && !templateSeen && records === 0 && body.length >= 4 && + body.readUInt16BE(0) === source.templateId, "Unexpected template identity or order") + const fieldCount = body.readUInt16BE(2) + requireEvidence(fieldCount > 0 && fieldCount <= FLOW_TEMPLATE_LIMITS.perTemplateFields, "Invalid template field count") + let at = 4 + width = 0 + for (let field = 0; field < fieldCount; field++) { + requireEvidence(body.length - at >= 4, "Truncated template field") + const type = body.readUInt16BE(at), size = body.readUInt16BE(at + 2) + requireEvidence(size > 0 && width + size <= 4096, "Variable or oversized replay template") + width += size + at += source.version === 10 && (type & 0x8000) !== 0 ? 8 : 4 + requireEvidence(at <= body.length, "Truncated enterprise template field") + } + requireEvidence(zeroPadding(body.subarray(at)), "Unexpected additional template or padding") + templateSeen = true + } else { + requireEvidence(id === source.templateId && (!changesTemplate || templateSeen), "Unexpected data set ID or template order") + requireEvidence(width > 0 && body.length >= width && zeroPadding(body.subarray(Math.floor(body.length / width) * width)), "Invalid data set records or padding") + records += Math.floor(body.length / width) + requireEvidence(records <= 64, "Replay record budget exceeded") + } + } + requireEvidence(templateSeen === changesTemplate && (source.version !== 9 || packet.readUInt16BE(2) >= sets), + "Template transition or v9 set coverage differs") + return { width, records } +} + +/** Synchronous, isolated replay. Call in an offline process with no live parser. */ +export function checkChrCapture(input: unknown) { + const parsed = capture.safeParse(input) + requireEvidence(parsed.success, "Invalid capture projection") + const { destination, exporters, frames } = parsed.data + requireEvidence(exporters[0].address !== exporters[1].address && exporters.every(row => row.address !== destination), "Expected two distinct exporter addresses and one destination") + const state = exporters.map(() => ({ phase: 0, width: 0, baselineRecords: 0, refreshedRecords: 0, missingTemplateObserved: false })) + let lastOrdinal = -1, lastReceivedMs = -1 + clearFlowTemplateCache() + try { + for (const frame of frames) { + requireEvidence(frame.ordinal > lastOrdinal && frame.receivedMs >= lastReceivedMs, "Frames must have strict capture order") + lastOrdinal = frame.ordinal + lastReceivedMs = frame.receivedMs + const source = exporters[frame.exporter], current = state[frame.exporter] + const packet = Buffer.from(frame.payloadHex, "hex") + const isTemplate = frame.kind === "template" || frame.kind === "refresh" + if (isTemplate) { + requireEvidence(frame.kind === "template" ? current.phase === 0 : current.phase === 1 && current.baselineRecords > 0, "Unexpected template transition") + } else { + requireEvidence(current.phase > 0, "Data preceding baseline template") + } + const envelope = inspectSets(packet, source, isTemplate, current.width) + const transport = { sourcePort: source.port, collectorSocketId: "offline_capture_socket" } + if (frame.kind === "pre-refresh-probe") { + requireEvidence(current.phase === 1 && current.baselineRecords > 0 && !current.missingTemplateObserved && frame.expected.length === 0, "Unexpected pre-refresh probe") + try { + parseFlowPacket(packet, source.address, transport) + } catch (error) { + if (!(error instanceof FlowTemplateRefused) || error.reason !== "missing-template") throw Error("Probe did not yield a missing-template refusal") + current.missingTemplateObserved = true + } + requireEvidence(current.missingTemplateObserved, "Pre-refresh data reused the previous template") + continue + } + const actual = parseFlowPacket(packet, source.address, transport).map(({ src, dst, bytes, packets, inIface, outIface }) => ({ src, dst, bytes, packets, inIface, outIface })) + requireEvidence(actual.length === envelope.records && isDeepStrictEqual(actual, frame.expected), "Decoded flow records differ from the expected capture projection") + if (isTemplate) { + current.phase++ + current.width = envelope.width + } + if (actual.length) { + requireEvidence(actual.every(row => row.bytes > 0 && row.packets > 0), "Expected positive traffic after the template") + if (current.phase === 1) current.baselineRecords += actual.length + else current.refreshedRecords += actual.length + } + } + requireEvidence(state.every(row => row.baselineRecords > 0 && row.refreshedRecords > 0), "Both exporters need positive baseline and refreshed traffic") + return { + captureReplayMatched: true, + exporterCount: state.length, + frames: frames.length, + exporters: state.map(({ baselineRecords, refreshedRecords, missingTemplateObserved }) => ({ baselineRecords, refreshedRecords, missingTemplateObserved })), + captureProvenanceVerified: false, + liveCollectorAccepted: false, + restartIsolationAccepted: false, + } + } finally { + clearFlowTemplateCache() + } +} + +async function main() { + const [path, expectedSha, ...extra] = process.argv.slice(2) + requireEvidence(path && /^[a-f0-9]{64}$/.test(expectedSha ?? "") && !extra.length, "Pass a capture projection file and its SHA-256") + const handle = await open(path, "r") + try { + const info = await handle.stat() + requireEvidence(info.isFile() && info.size > 0 && info.size <= maxInput, "Capture projection file is outside its size bound") + const bytes = Buffer.alloc(maxInput + 1) + let count = 0 + while (count < bytes.length) { + const { bytesRead } = await handle.read(bytes, count, bytes.length - count, count) + if (!bytesRead) break + count += bytesRead + } + requireEvidence(count === info.size, "Capture projection changed while reading") + const body = bytes.subarray(0, count) + requireEvidence(createHash("sha256").update(body).digest("hex") === expectedSha, "Capture projection SHA-256 differs") + console.log(JSON.stringify({ projectionSha256: expectedSha, ...checkChrCapture(JSON.parse(body.toString("utf8"))) })) + } finally { + await handle.close() + } +} + +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch(() => { console.error("CHR capture projection refused"); process.exitCode = 1 }) +} diff --git a/backend/src/test/traffic-flow-chr-capture-process.test.ts b/backend/src/test/traffic-flow-chr-capture-process.test.ts new file mode 100644 index 00000000..ae45c28c --- /dev/null +++ b/backend/src/test/traffic-flow-chr-capture-process.test.ts @@ -0,0 +1,111 @@ +import assert from "node:assert/strict" +import { EventEmitter } from "node:events" +import { PassThrough } from "node:stream" +import { test } from "node:test" +import { captureOwnedChrPacketsForTests } from "./traffic-flow-chr-capture-process.js" + +const scope = { interfaceName: "owned0", sources: ["192.0.2.1", "192.0.2.2"] as const, destination: "192.0.2.100", port: 4739 } +function fixturePcap(linkType: "RAW" | "EN10MB" = "RAW") { + const link = linkType === "EN10MB" ? 14 : 0, frame = 60 + link + const bytes = Buffer.alloc(24 + 2 * frame) + bytes.writeUInt32LE(0xa1b2c3d4) + bytes.writeUInt16LE(2, 4); bytes.writeUInt16LE(4, 6) + bytes.writeUInt32LE(4124 + link, 16); bytes.writeUInt32LE(link ? 1 : 101, 20) + for (const [index, at] of [24, 24 + frame].entries()) { + bytes.writeUInt32LE(1700000000, at); bytes.writeUInt32LE(44 + link, at + 8); bytes.writeUInt32LE(44 + link, at + 12) + if (link) bytes.writeUInt16BE(0x0800, at + 16 + 12) + const ip = bytes.subarray(at + 16 + link, at + frame) + ip[0] = 0x45; ip.writeUInt16BE(44, 2); ip[9] = 17 + Buffer.from([192, 0, 2, index + 1, 192, 0, 2, 100]).copy(ip, 12) + ip.writeUInt16BE(2055, 20); ip.writeUInt16BE(scope.port, 22); ip.writeUInt16BE(24, 24) + ip.writeUInt16BE(10, 28); ip.writeUInt16BE(16, 30) + } + return bytes +} +const ready = "tcpdump: listening on owned0, link-type RAW (Raw IP), snapshot length 4124 bytes\n" +type Link = "RAW" | "EN10MB" +function fixture({ mode = "normal", drop = 0, linkType = "RAW", reportedLink = linkType, fileLink = linkType }: + { mode?: "normal" | "early" | "wrong-interface"; drop?: number; linkType?: Link; reportedLink?: Link; fileLink?: Link } = {}) { + const child = Object.assign(new EventEmitter(), { stdout: new PassThrough(), stderr: new PassThrough(), + kill(signal: "SIGINT" | "SIGKILL") { + events.push(signal) + if (signal === "SIGINT") { + child.stdout.end(fixturePcap(fileLink)) + child.stderr.end("2 packets captured\n2 packets received by filter\n" + drop + " packets dropped by kernel\n") + } + queueMicrotask(() => child.emit("close", signal === "SIGINT" ? 0 : null, signal === "SIGINT" ? null : signal)) + return true + }, + }) + const events: string[] = [] + const launch = (args: string[]) => { + events.push("launch") + assert.deepEqual(args, ["-n", "-p", "-U", "-i", "owned0", "-y", linkType, "-s", linkType === "RAW" ? "4124" : "4138", "-c", "64", "-w", "-", + "ip and udp and dst host 192.0.2.100 and dst port 4739 and (src host 192.0.2.1 or src host 192.0.2.2)"]) + queueMicrotask(() => { + if (mode === "early") child.emit("close", 0, null) + else { + const line = reportedLink === "RAW" ? ready : ready.replace("RAW (Raw IP)", "EN10MB (Ethernet)").replace("4124", "4138") + child.stderr.write(mode === "normal" ? line : line.replace("owned0", "foreign0")) + } + }) + return child + } + return { child, launch, events } +} + +test("confined capture waits for exact readiness, then checks complete acknowledged two-source pcap", async () => { + const f = fixture() + const result = await captureOwnedChrPacketsForTests(scope, { + deadlineAt: Date.now() + 1000, assertCurrent: async () => { f.events.push("current") }, + exercise: async signal => { assert.equal(signal.aborted, false); f.events.push("exercise") }, + }, f.launch) + assert.deepEqual(f.events, ["current", "launch", "current", "exercise", "current", "SIGINT"]) + assert.deepEqual(result.counts, { captured: 2, received: 2, dropped: 0 }) + assert.deepEqual(result.bytes, fixturePcap()) +}) + +test("wrong-interface readiness and early exit never dispatch exercise", async () => { + for (const mode of ["wrong-interface", "early"] as const) { + const f = fixture({ mode }) + await assert.rejects(captureOwnedChrPacketsForTests(scope, { + deadlineAt: Date.now() + 100, assertCurrent: async () => {}, + exercise: async () => { f.events.push("exercise") }, + }, f.launch)) + assert.deepEqual(f.events, mode === "early" ? ["launch"] : ["launch", "SIGKILL"]) + } +}) + +test("capture drops refuse evidence and scope revocation prevents exercise", async () => { + const dropped = fixture({ drop: 1 }) + await assert.rejects(captureOwnedChrPacketsForTests(scope, { + deadlineAt: Date.now() + 1000, assertCurrent: async () => {}, exercise: async () => {}, + }, dropped.launch), /completeness/) + const revoked = fixture() + let checks = 0 + await assert.rejects(captureOwnedChrPacketsForTests(scope, { + deadlineAt: Date.now() + 1000, assertCurrent: async () => { if (++checks === 2) throw Error("revoked") }, + exercise: async () => { revoked.events.push("exercise") }, + }, revoked.launch), /revoked/) + assert.deepEqual(revoked.events, ["launch", "SIGKILL"]) +}) + +test("EN10MB launch, readiness, and file must agree independently", async () => { + const ethScope = { ...scope, linkType: "EN10MB" } as const + const f = fixture({ linkType: "EN10MB" }) + const result = await captureOwnedChrPacketsForTests(ethScope, { + deadlineAt: Date.now() + 1000, assertCurrent: async () => {}, exercise: async () => {}, + }, f.launch) + assert.deepEqual(result.bytes, fixturePcap("EN10MB")) + + const wrongReady = fixture({ linkType: "EN10MB", reportedLink: "RAW" }) + await assert.rejects(captureOwnedChrPacketsForTests(ethScope, { + deadlineAt: Date.now() + 100, assertCurrent: async () => {}, exercise: async () => { wrongReady.events.push("exercise") }, + }, wrongReady.launch)) + assert.deepEqual(wrongReady.events, ["launch", "SIGKILL"]) + + const wrongFile = fixture({ linkType: "EN10MB", fileLink: "RAW" }) + await assert.rejects(captureOwnedChrPacketsForTests(ethScope, { + deadlineAt: Date.now() + 1000, assertCurrent: async () => {}, exercise: async () => {}, + }, wrongFile.launch), /pinned link type EN10MB/) +}) diff --git a/backend/src/test/traffic-flow-chr-capture-process.ts b/backend/src/test/traffic-flow-chr-capture-process.ts new file mode 100644 index 00000000..09bc009a --- /dev/null +++ b/backend/src/test/traffic-flow-chr-capture-process.ts @@ -0,0 +1,135 @@ +/** Owns a single confined tcpdump child. Used only by an admitted native runner. */ +import { spawn } from "node:child_process" +import type { Readable } from "node:stream" +import { isIPv4 } from "node:net" +import { chrCaptureLayout, projectChrWire } from "./traffic-flow-chr-wire.js" +import { prepareChrReplay } from "./traffic-flow-chr-offline-replay.js" + +type Scope = Parameters[1] & { interfaceName: string } +type Child = { + stdout: Pick + stderr: Pick + once(event: "error", listener: (error: Error) => void): unknown + once(event: "close", listener: (code: number | null, signal: NodeJS.Signals | null) => void): unknown + kill(signal: "SIGINT" | "SIGKILL"): boolean +} +type Launch = (args: string[]) => Child +type Options = { + deadlineAt: number + assertCurrent: () => Promise + exercise: (signal: AbortSignal) => Promise +} +const maxCapture = 512 * 1024 +const maxDiagnostics = 8192 +const packetCap = 64 +function must(value: unknown, reason: string): asserts value { + if (!value) throw Error(reason) +} +function argsFor(scope: Scope, layout: ReturnType) { + must(/^[A-Za-z0-9][A-Za-z0-9_.-]{0,14}$/.test(scope.interfaceName) && scope.interfaceName !== "any" && scope.sources.length === 2 && + [...scope.sources, scope.destination].every(isIPv4) && new Set([...scope.sources, scope.destination]).size === 3 && + Number.isInteger(scope.port) && scope.port >= 1 && scope.port <= 65535, "Invalid confined capture scope") + const [first, second] = scope.sources + return ["-n", "-p", "-U", "-i", scope.interfaceName, "-y", layout.linkType, "-s", String(layout.snapLength), "-c", String(packetCap), "-w", "-", + "ip and udp and dst host " + scope.destination + " and dst port " + scope.port + " and (src host " + first + " or src host " + second + ")"] +} +function counters(stderr: string) { + const exact = (label: string) => { + const matches = stderr.split(/\r?\n/).map(line => new RegExp("^(\\d+) packets? " + label + "$").exec(line)).filter(match => match !== null) + must(matches.length === 1, "Capture counters are missing or ambiguous") + const value = Number(matches[0][1]) + must(Number.isSafeInteger(value) && value >= 0, "Capture counter exceeds integer range") + return value + } + return { captured: exact("captured"), received: exact("received by filter"), dropped: exact("dropped by kernel") } +} + +async function run(scope: Scope, options: Options, launch: Launch) { + const pinned = structuredClone(scope), layout = chrCaptureLayout(pinned.linkType) + const args = argsFor(pinned, layout), deadline = options.deadlineAt + must(Number.isSafeInteger(deadline) && deadline > Date.now() && deadline - Date.now() <= 90000, "Invalid capture deadline") + const controller = new AbortController() + const live = () => must(!controller.signal.aborted && Date.now() < deadline, "Capture deadline or ownership ended") + await options.assertCurrent() + live() + const child = launch(args), chunks: Buffer[] = [] + let bytes = 0, diagnosticBytes = 0, stderr = "", requestedStop = false, closed = false, failed = false + let ready!: () => void, terminate!: () => void, rejectWork!: (error: Error) => void + const readiness = new Promise(resolve => { ready = resolve }) + const workFailed = new Promise((_resolve, reject) => { rejectWork = reject }) + // The promise can reject during cleanup after an earlier error was observed. + void workFailed.catch(() => {}) + const stopped = new Promise(resolve => { terminate = resolve }) + const fail = (reason: string) => { + if (failed) return + failed = true + controller.abort() + rejectWork(Error(reason)) + } + child.stdout.on("data", (part: Buffer) => { + bytes += part.length + if (bytes > maxCapture) { fail("Capture exceeded byte limit"); return } + chunks.push(Buffer.from(part)) + }) + child.stderr.on("data", (part: Buffer) => { + diagnosticBytes += part.length + if (diagnosticBytes > maxDiagnostics) { fail("Capture diagnostics exceeded byte limit"); return } + stderr += part.toString("utf8") + const expected = "tcpdump: listening on " + pinned.interfaceName + ", link-type " + layout.linkType + if (stderr.split(/\r?\n/).some(line => line.startsWith(expected + " (") && line.endsWith("snapshot length " + layout.snapLength + " bytes"))) ready() + }) + child.once("error", () => fail("Capture child failed to start or signal")) + child.once("close", (code, signal) => { + closed = true + if (!requestedStop || code !== 0 || signal !== null) fail("Capture closed outside its acknowledged stop") + terminate() + }) + const timer = setTimeout(() => fail("Capture deadline expired"), Math.max(1, deadline - Date.now())) + let cleanupTimer: ReturnType | undefined + try { + await Promise.race([readiness, workFailed]) + live() + await Promise.race([options.assertCurrent(), workFailed]) + live() + await Promise.race([options.exercise(controller.signal), workFailed]) + live() + await Promise.race([options.assertCurrent(), workFailed]) + live() + requestedStop = true + must(child.kill("SIGINT"), "Capture stop signal was not delivered") + await Promise.race([stopped, workFailed]) + live() + const counts = counters(stderr), capture = Buffer.concat(chunks, bytes) + const wire = projectChrWire(capture, { sources: pinned.sources, destination: pinned.destination, port: pinned.port, ...(pinned.linkType === undefined ? {} : { linkType: pinned.linkType }) }) + must(counts.dropped === 0 && counts.captured === counts.received && counts.captured === wire.frames.length && counts.captured < packetCap, + "Capture completeness was not established") + return { bytes: capture, sha256: wire.captureSha256, dropped: counts.dropped, complete: true as const, counts, wire } + } finally { + clearTimeout(timer) + controller.abort() + if (!closed) { + child.kill("SIGKILL") + const exited = await Promise.race([stopped.then(() => true), new Promise(resolve => { cleanupTimer = setTimeout(() => resolve(false), 2000) })]) + if (cleanupTimer) clearTimeout(cleanupTimer) + must(exited, "Capture child exit could not be confirmed") + } + } +} + +export const captureOwnedChrPackets = (scope: Scope, options: Options) => { + must(process.platform === "linux", "Confined tcpdump capture requires Linux") + return run(scope, options, args => + spawn("tcpdump", args, { shell: false, stdio: ["ignore", "pipe", "pipe"], env: { ...process.env, LC_ALL: "C" } })) +} + +/** Adapter for the capture callback of executeOwnedChrCapture. */ +export function createConfinedChrCapture(scope: Scope, options: Omit) { + const pinned = structuredClone(scope) + return async (exercise: (signal?: AbortSignal) => Promise) => { + const capture = await captureOwnedChrPackets(pinned, { ...options, exercise }) + return { ...capture, ...prepareChrReplay(capture.wire) } + } +} + +/** Explicit inert transport seam. */ +export const captureOwnedChrPacketsForTests = run diff --git a/backend/src/test/traffic-flow-chr-capture-timing.test.ts b/backend/src/test/traffic-flow-chr-capture-timing.test.ts new file mode 100644 index 00000000..7fc18ecc --- /dev/null +++ b/backend/src/test/traffic-flow-chr-capture-timing.test.ts @@ -0,0 +1,65 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { assessChrCaptureTiming, planChrMeasuredCaptureWindow } from "./traffic-flow-chr-capture-timing.js" +import { CHR_TOPOLOGY_CATALOG_PAIR_GETS, CHR_TOPOLOGY_CATALOG_READ_LIMIT } from "./traffic-flow-chr-topology-inventory.js" +import { CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS } from "./traffic-flow-chr-target-harness.js" + +const current = { pairGets: CHR_TOPOLOGY_CATALOG_PAIR_GETS, catalogueReadLimit: CHR_TOPOLOGY_CATALOG_READ_LIMIT, + captureMs: 90000, targetMinimumRemainingMs: CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS } + +test("reduced exact catalogue count fits the existing cap but does not authorize writes without measurement", () => { + assert.equal(current.pairGets, 86) + const result = assessChrCaptureTiming(current) + assert.equal(result.status, "measurement-required") + assert.equal(result.minimumNodeCatalogueGets, 9460) + const max = assessChrCaptureTiming({ ...current, forwardWrites: 6 }) + assert.equal(max.minimumNodeCatalogueGets, 10492) + assert.equal(max.pairsBeforeGenerator, 22) + assert.equal(result.pairsBeforeFirstTargetWindow, 2) + assert.equal(result.setupBudgetMs, 26000) + assert.equal(assessChrCaptureTiming({ ...current, catalogueReadLimit: 8000 }).status, "catalogue-read-limit") +}) + +const measured = { + childPairMs: 12023, parentPairMs: 12023, directSetupMs: 4000, + collectorBeforeCaptureMs: 45000, collectorAfterCaptureMs: 55000, + beforeCaptureMs: 1400000, afterCaptureMs: 1500000, counterSpanMs: 120000, remainingAuthorityMs: 3600000, +} + +test("proposes a finite measured capture window and reserves authority through inverse", () => { + assert.deepEqual(planChrMeasuredCaptureWindow(measured), { + status: "candidate-window", captureMs: 205000, requiredAtCaptureStartMs: 1810000, requiredBeforeFirstWriteMs: 3315000, + requiredCollectorMs: 515000, + }) + assert.equal(planChrMeasuredCaptureWindow({ ...measured, remainingAuthorityMs: 3315000 }).status, "authority-window-exceeded") + assert.deepEqual(planChrMeasuredCaptureWindow({ ...measured, childPairMs: 100, parentPairMs: 100 }), { + status: "candidate-window", captureMs: 90000, requiredAtCaptureStartMs: 1695000, requiredBeforeFirstWriteMs: 3200000, + requiredCollectorMs: 400000, + }) +}) + +test("refuses missing or excessive measurements and preserves the counter evidence horizon", () => { + assert.equal(planChrMeasuredCaptureWindow({ ...measured, parentPairMs: undefined }).status, "measurement-required") + assert.equal(planChrMeasuredCaptureWindow({ ...measured, directSetupMs: undefined }).status, "measurement-required") + assert.equal(planChrMeasuredCaptureWindow({ ...measured, childPairMs: 30000 }).status, "capture-window-exceeded") + assert.equal(planChrMeasuredCaptureWindow({ ...measured, counterSpanMs: 180001 }).status, "capture-window-exceeded") + assert.throws(() => planChrMeasuredCaptureWindow({ ...measured, childPairMs: Infinity }), /Invalid CHR measured capture inputs/) + assert.throws(() => planChrMeasuredCaptureWindow({ ...measured, remainingAuthorityMs: 3600001 }), /Invalid CHR measured capture inputs/) +}) + +test("a long fixture lease still requires measured work within the short MCP credential", () => { + assert.equal(planChrMeasuredCaptureWindow({ ...measured, collectorAfterCaptureMs: undefined }).status, "collector-measurement-required") + assert.deepEqual(planChrMeasuredCaptureWindow({ ...measured, collectorAfterCaptureMs: 140000 }), { + status: "collector-window-exceeded", requiredCollectorMs: 600000, + }) +}) + +test("a larger catalogue budget still requires real representative measurement and refuses observed latency", () => { + const options = { ...current, catalogueReadLimit: 20000 } + assert.equal(assessChrCaptureTiming(options).status, "measurement-required") + // Minimum recorded GET on CHR A/B times 43 each. This is a projection, + // not an assertion about the next live relay transaction. + assert.equal(assessChrCaptureTiming({ ...options, pairedReadMs: 43 * (108 + 263) }).status, "capture-setup-window") + assert.equal(assessChrCaptureTiming({ ...options, pairedReadMs: 100 }).status, "parent-measurement-required") + assert.equal(assessChrCaptureTiming({ ...options, pairedReadMs: 100, parentPairedReadMs: 100 }).status, "budget-check-passed") +}) diff --git a/backend/src/test/traffic-flow-chr-capture-timing.ts b/backend/src/test/traffic-flow-chr-capture-timing.ts new file mode 100644 index 00000000..d0a85ded --- /dev/null +++ b/backend/src/test/traffic-flow-chr-capture-timing.ts @@ -0,0 +1,90 @@ +import { CHR_COLLECTOR_RESULT_POLL_MS, CHR_STAGED_COLLECTOR_APPROVAL_MS, CHR_STAGED_TARGET_CAPTURE_MAX_MS, + CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS } from "./traffic-flow-chr-target-timing-profile.js" + +export function measuredChrCaptureMs(childPairMs: number, parentPairMs: number, directSetupMs: number) { + // Two enables with pre-dispatch and post-ACK reads by both owners, plus the child's pre-probe read. + const setupMs = (5 * childPairMs + 4 * parentPairMs + directSetupMs) * 1.25 + return Math.max(90000, (Math.floor((CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS + setupMs) / 1000) + 1) * 1000) +} + +/** This model is a lower bound for the current serial read schedule. It can + * refuse admission. Its success never substitutes for live scope or authority. */ +export function assessChrCaptureTiming(options: { + pairGets: number + catalogueReadLimit: number + captureMs: number + targetMinimumRemainingMs: number + forwardWrites?: number + pairedReadMs?: number + parentPairedReadMs?: number +}) { + const { pairGets, catalogueReadLimit, captureMs, targetMinimumRemainingMs, forwardWrites = 4, pairedReadMs, parentPairedReadMs } = options + if (![pairGets, catalogueReadLimit, captureMs, targetMinimumRemainingMs].every(value => Number.isSafeInteger(value) && value > 0) || + ![4, 5, 6].includes(forwardWrites) || [pairedReadMs, parentPairedReadMs].some(value => + value !== undefined && (!Number.isFinite(value) || value <= 0))) { + throw Error("Invalid CHR timing inputs") + } + // Each flow setting/target write takes three fresh reads and a fresh + // topology proof after each. Eight outer pairs include the timing sample. + const pairsBeforeFirstTargetWindow = 2 + const pairsBeforeGenerator = pairsBeforeFirstTargetWindow + 3 * forwardWrites + 2 + const topologyAcquireAndRestorePairs = 2 * (1 + 2 * 18) + const minimumNodePairs = topologyAcquireAndRestorePairs + pairsBeforeGenerator + 3 * forwardWrites + 8 + const minimumNodeCatalogueGets = minimumNodePairs * pairGets + const minimumParentPairsBeforeGenerator = 2 * forwardWrites + const minimumParentCatalogueGetsBeforeGenerator = minimumParentPairsBeforeGenerator * pairGets + const setupBudgetMs = captureMs - targetMinimumRemainingMs + // A measured pair omits parent identity/catalogue extras. Use it only to + // veto an already underbudget capture, with margin for local scheduling. + const projectedSetupMs = pairedReadMs === undefined ? undefined : + Math.ceil((pairsBeforeGenerator * pairedReadMs + minimumParentPairsBeforeGenerator * (parentPairedReadMs ?? 0)) * 1.25) + const status = minimumNodeCatalogueGets > catalogueReadLimit ? "catalogue-read-limit" : + setupBudgetMs <= 0 || (projectedSetupMs !== undefined && projectedSetupMs >= setupBudgetMs) ? "capture-setup-window" : + pairedReadMs === undefined ? "measurement-required" : + parentPairedReadMs === undefined ? "parent-measurement-required" : "budget-check-passed" + return { status, minimumNodeCatalogueGets, minimumParentCatalogueGetsBeforeGenerator, + pairsBeforeFirstTargetWindow, pairsBeforeGenerator, setupBudgetMs, projectedSetupMs } as const +} + +/** Offline proposal for the staged disabled-baseline path. Before/after phase + * budgets cover the complete reviewed schedule outside capture, with one + * separate bounded human approval in each phase. */ +export function planChrMeasuredCaptureWindow(options: { + childPairMs?: number + parentPairMs?: number + directSetupMs?: number + collectorBeforeCaptureMs?: number + collectorAfterCaptureMs?: number + beforeCaptureMs: number + afterCaptureMs: number + counterSpanMs: number + remainingAuthorityMs: number +}) { + const { childPairMs, parentPairMs, directSetupMs, collectorBeforeCaptureMs, collectorAfterCaptureMs, beforeCaptureMs, afterCaptureMs, + counterSpanMs, remainingAuthorityMs } = options + const valid = (value: number) => Number.isSafeInteger(value) && value > 0 && value <= 3600000 + if (![beforeCaptureMs, afterCaptureMs, counterSpanMs, remainingAuthorityMs].every(valid) || + [childPairMs, parentPairMs, directSetupMs, collectorBeforeCaptureMs, collectorAfterCaptureMs] + .some(value => value !== undefined && !valid(value))) { + throw Error("Invalid CHR measured capture inputs") + } + if (childPairMs === undefined || parentPairMs === undefined || directSetupMs === undefined) { + return { status: "measurement-required" } as const + } + const captureMs = measuredChrCaptureMs(childPairMs, parentPairMs, directSetupMs) + if (captureMs > CHR_STAGED_TARGET_CAPTURE_MAX_MS || counterSpanMs > 180000) { + return { status: "capture-window-exceeded", captureMs } as const + } + const operationMs = CHR_STAGED_COLLECTOR_APPROVAL_MS + CHR_COLLECTOR_RESULT_POLL_MS + if (collectorBeforeCaptureMs === undefined || collectorAfterCaptureMs === undefined) { + return { status: "collector-measurement-required" } as const + } + const requiredCollectorMs = captureMs + 2 * operationMs + collectorBeforeCaptureMs + collectorAfterCaptureMs + if (requiredCollectorMs >= 600000) return { status: "collector-window-exceeded", requiredCollectorMs } as const + const requiredAtCaptureStartMs = captureMs + afterCaptureMs + operationMs + const requiredBeforeFirstWriteMs = beforeCaptureMs + operationMs + requiredAtCaptureStartMs + if (requiredBeforeFirstWriteMs >= remainingAuthorityMs) { + return { status: "authority-window-exceeded", requiredBeforeFirstWriteMs } as const + } + return { status: "candidate-window", captureMs, requiredAtCaptureStartMs, requiredBeforeFirstWriteMs, requiredCollectorMs } as const +} diff --git a/backend/src/test/traffic-flow-chr-catalogue-timing.test.ts b/backend/src/test/traffic-flow-chr-catalogue-timing.test.ts new file mode 100644 index 00000000..7e37dea9 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-catalogue-timing.test.ts @@ -0,0 +1,124 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import { Readable } from "node:stream" +import { test } from "node:test" +import { chrCatalogueTimingFailure, compareChrCatalogueTimingReceipts, measureChrCatalogueTiming, parseChrCatalogueTimingPins, readChrCatalogueTimingStartup } from "./traffic-flow-chr-catalogue-timing.js" + +const routers = { + A: { deviceId: "A", identity: "a", version: "7.24.5" }, + B: { deviceId: "B", identity: "b", version: "7.24.5" }, +} +const runId = "a012bcde" + +test("timing pins reject unsealed, expired, aliased or extra inputs", () => { + const namespace = { device: "1", inode: "1" }, now = 1000 + const input = { role: "child", runId, deadlineAt: now + 120000, startupSocket: namespace, hostNamespace: { device: "1", inode: "2" }, + readSockets: { A: { device: "2", inode: "3" }, B: { device: "2", inode: "4" } }, routers, readPassword: "synthetic" } + function parse(value: unknown) { + const raw = Buffer.from(JSON.stringify(value)) + return parseChrCatalogueTimingPins(raw, createHash("sha256").update(raw).digest("hex"), now) + } + assert.equal(parse(input).role, "child") + assert.throws(() => parse({ ...input, startupSocket: input.readSockets.A })) + assert.throws(() => parse({ ...input, deadlineAt: now + 180001 })) + assert.throws(() => parse({ ...input, writeAllowed: true })) + const equal = { ...input, routers: { A: routers.A, B: { ...routers.B, identity: routers.A.identity } } } + assert.throws(() => parse(equal)) + const identityProvenance = { + A: { endpoint: "194.226.121.150", sshHostKey: "SHA256:zTLVXLYTylDWKgY6KIxJJwckbec9MzEiGZnk/HDOTTM" }, + B: { endpoint: "104.165.244.7", sshHostKey: "SHA256:X6oEE81lb/we7YFUHMs2+nHvMbMp6bTvGKiL8pesCJc" }, + } + assert.equal(parse({ ...equal, identityProvenance }).routers.B.identity, routers.A.identity) + assert.throws(() => parse({ ...equal, identityProvenance: { ...identityProvenance, B: identityProvenance.A } })) + assert.throws(() => parse({ ...equal, identityProvenance, routers: { ...equal.routers, B: { ...equal.routers.B, deviceId: "foreign" } } })) + assert.throws(() => parseChrCatalogueTimingPins(Buffer.from(JSON.stringify(input)), "0".repeat(64), now)) +}) + +for (const role of ["parent", "child"] as const) test("timing samples real catalogue factory with exact GET count: " + role, async () => { + const gets = { A: 0, B: 0 } + const client = (side: "A" | "B") => ({ + async get(path: string, _timeout?: number, _signal?: AbortSignal, _max?: number, before?: () => Promise) { + gets[side]++ + await before?.() + const row = routers[side] + return (path === "/system/identity" ? { name: row.identity } : path === "/system/resource" ? [{ version: row.version }] : []) as T + }, + }) + const clients = { A: client("A"), B: client("B") } + const result = await measureChrCatalogueTiming({ role, runId, deadlineAt: Date.now() + 100000, routers, clients, + authorize: async () => {}, + ...(role === "parent" ? { async parentIdentity(side: "A" | "B", deviceId: string) { + assert.equal(side, deviceId) + await clients[side].get("/system/identity") + await clients[side].get("/system/resource") + } } : {}), + }) + assert.deepEqual(gets, role === "parent" ? { A: 141, B: 141 } : { A: 133, B: 133 }) + assert.equal(result.pairs.length, 3) + assert(result.pairs.every(sample => Number.isSafeInteger(sample) && sample > 0)) + assert.equal(result.catalogueGetsPerPair, 86) + assert.equal(result.nativeWrites, 0) + assert.equal(result.postgresqlStarted, false) + assert.equal(result.directSetupMeasured, false) +}) + +test("paired timing receipts use both maximum samples and never grant a write lease", () => { + const now = 2_000_000 + const common = { runId, scopeSha256: "a".repeat(64), startedAt: now - 100000, finishedAt: now - 1000, + pairs: [10000_000, 12023_000, 9000_000], pairMaxMs: 12023, catalogueGetsPerPair: 86, + nativeWrites: 0, postgresqlStarted: false, directSetupMeasured: false, collectorPhasesMeasured: false } + const parent = { ...common, role: "parent", childOuterIdentityGets: 0, parentIdentityGetsPerPair: 8 } + const child = { ...common, role: "child", childOuterIdentityGets: 8, parentIdentityGetsPerPair: 0 } + const result = compareChrCatalogueTimingReceipts(parent, child, now) + assert.equal(result.minimumCaptureMs, 200000) + assert.equal(result.status, "remaining-measurements-required") + assert.equal(result.nativeWritesAuthorized, false) + assert.equal(compareChrCatalogueTimingReceipts(parent, { ...child, pairs: [30000_000, 12023_000, 9000_000], pairMaxMs: 30000 }, now).status, + "capture-window-exceeded") + for (const changed of [{ runId: "fedcba01" }, { scopeSha256: "b".repeat(64) }, { role: "parent" }, + { pairMaxMs: 10000 }, { parallelRouters: true }, { nativeWrites: 1 }, { finishedAt: now + 1 }]) { + assert.throws(() => compareChrCatalogueTimingReceipts(parent, { ...child, ...changed }, now)) + } +}) + + +test("startup requires the exact fresh local namespace on the complete private stream", async () => { + const expected = { device: "3", inode: "4" }, host = { device: "3", inode: "5" } + const stream = (chunks: string[]) => Readable.from(chunks.map(chunk => Buffer.from(chunk))) + await readChrCatalogueTimingStartup(stream(["MM-NETFLOW-", "TIMING-READY 3 4\n"]), expected, host) + for (const raw of ["MM-NETFLOW-TIMING-READY 3 5\n", "MM-NETFLOW-TIMING-READY 3 4\ntrailing", + "MM-NETFLOW-TIMING-READY 3 4", "X".repeat(73)]) { + await assert.rejects(readChrCatalogueTimingStartup(stream([raw]), expected, host)) + } + await assert.rejects(readChrCatalogueTimingStartup(stream(["MM-NETFLOW-TIMING-READY 3 4\n"]), expected, expected)) +}) + + +test("same expected names still require each CHR to match its own name and exact version", async () => { + const paired = { A: routers.A, B: { ...routers.B, identity: routers.A.identity } } + for (const mismatch of ["none", "name", "version"] as const) { + const reader = (side: "A" | "B") => ({ + async get(path: string) { + return (path === "/system/identity" + ? { name: side === "B" && mismatch === "name" ? "wrong" : paired[side].identity } + : path === "/system/resource" + ? { version: side === "B" && mismatch === "version" ? "wrong" : paired[side].version } + : []) as T + }, + }) + const result = measureChrCatalogueTiming({ role: "child", runId, deadlineAt: Date.now() + 100000, routers: paired, + clients: { A: reader("A"), B: reader("B") }, authorize: async () => {} }) + if (mismatch === "none") assert.equal((await result).pairs.length, 3) + else await assert.rejects(result, /catalogue timing refused/) + } +}) + + +test("refusal projection exposes only finite role, stage, code and catalogue count", () => { + assert.deepEqual(chrCatalogueTimingFailure(Error("Foreign native catalogue drift"), "parent", "catalogue", 172), + { completed: false, automaticReplay: false, role: "parent", stage: "catalogue", code: "foreign-drift", catalogueGets: 172 }) + const secret = Error("never serialize raw private data") + Object.defineProperty(secret, "code", { get() { throw Error("never execute a private error getter") } }) + assert.deepEqual(chrCatalogueTimingFailure(secret, "child", "startup", 0), + { completed: false, automaticReplay: false, role: "child", stage: "startup", code: "refused", catalogueGets: 0 }) +}) diff --git a/backend/src/test/traffic-flow-chr-catalogue-timing.ts b/backend/src/test/traffic-flow-chr-catalogue-timing.ts new file mode 100644 index 00000000..06625c37 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-catalogue-timing.ts @@ -0,0 +1,297 @@ +import { createHash } from "node:crypto" +import { constants } from "node:fs" +import { lstat, open, realpath } from "node:fs/promises" +import { createConnection } from "node:net" +import type { Readable } from "node:stream" +import { fileURLToPath } from "node:url" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import type { MikrotikClient } from "../services/mikrotik.js" +import { openChrParentCatalogueReaders } from "./traffic-flow-chr-parent-launch.js" +import { readChrLinuxNamespaceIdentity } from "./traffic-flow-chr-linux-udp-owner.js" +import { CHR_TOPOLOGY_CATALOG_PAIR_GETS, createChrTransitTopologyInventory } from "./traffic-flow-chr-topology-inventory.js" +import { measuredChrCaptureMs } from "./traffic-flow-chr-capture-timing.js" +import { CHR_STAGED_TARGET_CAPTURE_MAX_MS } from "./traffic-flow-chr-target-timing-profile.js" + +type Side = "A" | "B" +type Reader = Pick +const sides = ["A", "B"] as const +const pinValue = z.string().regex(/^[1-9][0-9]{0,19}$/) +const pin = z.object({ device: pinValue, inode: pinValue }).strict() +const label = z.string().regex(/^[^\u0000-\u001f\u007f]{1,128}$/u) +const device = z.object({ deviceId: label, identity: label, version: label }).strict() +const identityProvenance = z.object({ + A: z.object({ endpoint: z.literal("194.226.121.150"), + sshHostKey: z.literal("SHA256:zTLVXLYTylDWKgY6KIxJJwckbec9MzEiGZnk/HDOTTM") }).strict(), + B: z.object({ endpoint: z.literal("104.165.244.7"), + sshHostKey: z.literal("SHA256:X6oEE81lb/we7YFUHMs2+nHvMbMp6bTvGKiL8pesCJc") }).strict(), +}).strict() +const fixture = z.object({ + role: z.enum(["parent", "child"]), runId: z.string().regex(/^[a-f0-9]{8}$/), deadlineAt: z.number().int().positive().safe(), + hostNamespace: pin, startupSocket: pin, readSockets: z.object({ A: pin, B: pin }).strict(), + routers: z.object({ A: device, B: device }).strict(), + identityProvenance: identityProvenance.optional(), + parallelRouters: z.literal(true).optional(), + readPassword: z.string().regex(/^[^\u0000-\u001f\u007f]{1,512}$/u), +}).strict() +type Fixture = z.infer +const maximumMs = 180000 +const samplesPerProfile = 3 +const fail = (): never => { throw Error("Read-only CHR catalogue timing refused") } +const digestPattern = /^[a-f0-9]{64}$/ +const digestOf = (raw: Buffer | string) => createHash("sha256").update(raw).digest("hex") + +export function parseChrCatalogueTimingPins(raw: Buffer, digest: string, now = Date.now()) { + if (!digestPattern.test(digest) || raw.length < 1 || raw.length > 16384 || digestOf(raw) !== digest) return fail() + const pins = fixture.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(raw))) + if (pins.deadlineAt <= now || pins.deadlineAt - now > maximumMs || + new Set([pins.startupSocket, ...Object.values(pins.readSockets)].map(item => item.device + ":" + item.inode)).size !== 3 || pins.routers.A.deviceId === pins.routers.B.deviceId || + (pins.identityProvenance && (pins.routers.A.deviceId !== "A" || pins.routers.B.deviceId !== "B")) || + (!pins.identityProvenance && pins.routers.A.identity === pins.routers.B.identity)) return fail() + return pins +} + +async function readPins(role: Fixture["role"]) { + const file = await open("/run/mm-netflow-catalogue-timing-pins.json", constants.O_RDONLY | constants.O_NOFOLLOW) + try { + const row = await file.stat() + if (!row.isFile() || row.uid !== 0 || row.gid !== process.getegid?.() || row.nlink !== 1 || + (row.mode & 0o777) !== (role === "parent" ? 0o400 : 0o440) || row.size < 1 || row.size > 16384) return fail() + const buffer = Buffer.alloc(row.size + 1) + const result = await file.read(buffer, 0, buffer.length, 0) + if (result.bytesRead !== row.size) return fail() + const pins = parseChrCatalogueTimingPins(buffer.subarray(0, row.size), process.env.MMAPP_NETFLOW_TIMING_PINS_SHA256 ?? "") + if (pins.role !== role) return fail() + return pins + } finally { await file.close() } +} + +export async function readChrCatalogueTimingStartup(stream: Readable, expected: Fixture["hostNamespace"], host: Fixture["hostNamespace"]) { + let raw = Buffer.alloc(0) + for await (const data of stream) { + const bytes = Buffer.isBuffer(data) ? data : Buffer.from(data) + if (raw.length + bytes.length > 72) return fail() + raw = Buffer.concat([raw, bytes]) + } + const match = /^MM-NETFLOW-TIMING-READY ([1-9][0-9]{0,19}) ([1-9][0-9]{0,19})\n$/.exec(raw.toString("latin1")) + if (!match || isDeepStrictEqual(expected, host) || + !isDeepStrictEqual({ device: match[1], inode: match[2] }, expected)) return fail() +} + +async function awaitStartup(pins: Fixture, observed: Fixture["hostNamespace"], authorize: () => Promise) { + const directory = "/run/mm-netflow-catalogue-timing" + const path = directory + "/startup.sock" + const gid = BigInt(process.getegid?.() ?? -1) + const fence = async () => { + await authorize() + const [dir, sock] = await Promise.all([lstat(directory, { bigint: true }), lstat(path, { bigint: true })]) + if (!dir.isDirectory() || dir.uid !== 0n || dir.gid !== gid || await realpath(directory) !== directory || + (dir.mode & 0o777n) !== (pins.role === "parent" ? 0o700n : 0o710n) || + !sock.isSocket() || sock.uid !== 0n || sock.gid !== gid || + (sock.mode & 0o777n) !== (pins.role === "parent" ? 0o600n : 0o660n) || + String(sock.dev) !== pins.startupSocket.device || String(sock.ino) !== pins.startupSocket.inode) return fail() + } + await fence() + const socket = createConnection({ path }) + const timer = setTimeout(() => socket.destroy(Error("Catalogue timing startup timed out")), Math.max(1, Math.min(10000, pins.deadlineAt - Date.now()))) + try { + await readChrCatalogueTimingStartup(socket, observed, pins.hostNamespace) + await fence() + } finally { clearTimeout(timer); socket.destroy() } +} +function assertSingleton(raw: unknown, field: string, value: string) { + const row = Array.isArray(raw) ? raw.length === 1 ? raw[0] : undefined : raw + if (!row || typeof row !== "object" || Array.isArray(row) || (row as Record)[field] !== value) return fail() +} + +/** Only actual catalogue GET batches belong to the timed interval. Parent + * identity bracketing is part of that profile; child identity is an outer fence. */ +export async function measureChrCatalogueTiming(options: Pick & { + clients: Record + authorize(): Promise + parentIdentity?(side: Side, deviceId: string): Promise + parallelRouters?: true + note?(stage: TimingStage, catalogueGets: number): void +}) { + const { role, runId, deadlineAt, authorize, parentIdentity, note } = options + const startedAt = Date.now() + let stage: TimingStage = "catalogue", catalogueGets = 0 + const advance = (next: TimingStage) => { stage = next; note?.(stage, catalogueGets) } + if ((role === "parent") !== (typeof parentIdentity === "function")) return fail() + let gets = 0 + const routers = Object.fromEntries(sides.map(side => [side, { + deviceId: options.routers[side].deviceId, client: { get(...args: Parameters) { + gets++ + if (stage === "catalogue") { catalogueGets++; note?.(stage, catalogueGets) } + return options.clients[side].get(...args) + } }, + }])) as Record + const assertPair = async () => { + advance("parent-identity") + for (const side of sides) { + await authorize() + await parentIdentity!(side, routers[side].deviceId) + } + advance("catalogue") + } + const childIdentity = async () => { + for (const side of sides) { + await authorize() + const expected = options.routers[side] + const [name, version] = await Promise.all([ + routers[side].client.get("/system/identity", 1800, undefined, 65536, authorize), + routers[side].client.get("/system/resource", 1800, undefined, 65536, authorize), + ]) + assertSingleton(name, "name", expected.identity) + assertSingleton(version, "version", expected.version) + await authorize() + } + } + if (role === "child") { advance("child-identity-before"); await childIdentity() } + const inventory = createChrTransitTopologyInventory({ + runId, deadlineAt, authorize: role === "parent" ? assertPair : authorize, + ...(role === "parent" ? { authorizeRequest: authorize } : {}), parallelism: 2, parallelRouters: options.parallelRouters, routers, + }) + const pairs = [] + for (let sample = 0; sample < samplesPerProfile; sample++) { + advance("catalogue") + const previous = gets + const started = performance.now() + await inventory.assertOutsideScope([]) + await inventory.read("A") + await inventory.read("B") + if (gets - previous !== CHR_TOPOLOGY_CATALOG_PAIR_GETS) return fail() + pairs.push(Math.max(1, Math.ceil((performance.now() - started) * 1000))) + } + if (role === "child") { advance("child-identity-after"); await childIdentity() } + return Object.freeze({ + role, runId, startedAt, finishedAt: Date.now(), + scopeSha256: digestOf(JSON.stringify({ routers: options.routers, identityProvenance: options.identityProvenance })), + pairs, pairMaxMs: Math.ceil(Math.max(...pairs) / 1000), catalogueGetsPerPair: CHR_TOPOLOGY_CATALOG_PAIR_GETS, + ...(options.parallelRouters ? { parallelRouters: true as const } : {}), + childOuterIdentityGets: role === "child" ? 8 : 0, parentIdentityGetsPerPair: role === "parent" ? 8 : 0, + nativeWrites: 0, postgresqlStarted: false, directSetupMeasured: false, collectorPhasesMeasured: false, + }) +} + +const receipt = z.object({ + role: fixture.shape.role, runId: fixture.shape.runId, scopeSha256: z.string().regex(digestPattern), + startedAt: z.number().int().positive().safe(), finishedAt: z.number().int().positive().safe(), + pairs: z.array(z.number().int().min(1).max(maximumMs * 1000)).length(samplesPerProfile), + pairMaxMs: z.number().int().min(1).max(maximumMs), catalogueGetsPerPair: z.literal(CHR_TOPOLOGY_CATALOG_PAIR_GETS), + parallelRouters: fixture.shape.parallelRouters, + childOuterIdentityGets: z.union([z.literal(0), z.literal(8)]), parentIdentityGetsPerPair: z.union([z.literal(0), z.literal(8)]), + nativeWrites: z.literal(0), postgresqlStarted: z.literal(false), directSetupMeasured: z.literal(false), collectorPhasesMeasured: z.literal(false), +}).strict() + +export function compareChrCatalogueTimingReceipts(parent: unknown, child: unknown, now = Date.now()) { + const parentTiming = receipt.parse(parent), childTiming = receipt.parse(child) + if (parentTiming.role !== "parent" || childTiming.role !== "child" || parentTiming.runId !== childTiming.runId || + parentTiming.scopeSha256 !== childTiming.scopeSha256 || parentTiming.parallelRouters !== childTiming.parallelRouters || + parentTiming.childOuterIdentityGets !== 0 || parentTiming.parentIdentityGetsPerPair !== 8 || + childTiming.childOuterIdentityGets !== 8 || childTiming.parentIdentityGetsPerPair !== 0 || [parentTiming, childTiming].some(row => + row.finishedAt < row.startedAt || row.finishedAt > now || row.finishedAt - row.startedAt > maximumMs || + row.pairMaxMs !== Math.ceil(Math.max(...row.pairs) / 1000))) return fail() + const minimumCaptureMs = measuredChrCaptureMs(childTiming.pairMaxMs, parentTiming.pairMaxMs, 0) + return { runId: parentTiming.runId, scopeSha256: parentTiming.scopeSha256, parentPairMs: parentTiming.pairMaxMs, childPairMs: childTiming.pairMaxMs, + minimumCaptureMs, status: minimumCaptureMs > CHR_STAGED_TARGET_CAPTURE_MAX_MS ? "capture-window-exceeded" : "remaining-measurements-required", + directSetupMeasured: false, collectorPhasesMeasured: false, fullLeaseMeasured: false, nativeWritesAuthorized: false } as const +} + +async function readTimingReceipt(path: string, digest: string) { + const file = await open(path, "r") + try { + const stat = await file.stat() + if (!stat.isFile() || stat.size < 1 || stat.size > 16384 || !digestPattern.test(digest)) return fail() + const buffer = Buffer.alloc(stat.size + 1) + const { bytesRead } = await file.read(buffer, 0, buffer.length, 0) + const raw = buffer.subarray(0, bytesRead) + if (bytesRead !== stat.size || digestOf(raw) !== digest) return fail() + return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(raw)) as unknown + } finally { await file.close() } +} + +const stages = ["entry", "pins", "namespace", "startup", "parent-reader", "child-import", "child-relay", + "child-identity-before", "parent-identity", "catalogue", "child-identity-after", "child-close"] as const +type TimingStage = typeof stages[number] + +export function chrCatalogueTimingFailure(error: unknown, role: "parent" | "child", stage: TimingStage, catalogueGets: number) { + const value = error instanceof Error ? error : undefined + const messages: Readonly> = { + "Foreign native catalogue drift": "foreign-drift", + "Incomplete native topology catalogue": "catalogue-shape", + "Ambiguous native topology catalogue": "catalogue-id", + "Paired topology catalogue exceeds budget": "catalogue-bytes", + "Topology catalogue deadline elapsed": "deadline", + "Owned CHR namespace relay refused": "relay-refused", + "Catalogue timing startup timed out": "startup-timeout", + "Pinned NetFlow parent refused. Automatic replay forbidden.": "parent-read-refused", + } + const code = Object.getOwnPropertyDescriptor(value ?? {}, "code")?.value + const system = ["ENOENT", "EACCES", "EPERM", "EADDRINUSE", "ECONNREFUSED", "ECONNRESET", "ETIMEDOUT", "ABORT_ERR"] + const message = Object.getOwnPropertyDescriptor(value ?? {}, "message")?.value + return { + completed: false as const, automaticReplay: false as const, role, + stage: stages.includes(stage) ? stage : "entry", + code: typeof code === "string" && system.includes(code) ? code : + typeof message === "string" && Object.hasOwn(messages, message) ? messages[message] : "refused", + catalogueGets: Number.isSafeInteger(catalogueGets) && catalogueGets >= 0 && catalogueGets <= 258 ? catalogueGets : 0, + } +} + +const progress = { role: (process.argv[3] === "child" ? "child" : "parent") as "parent" | "child", + stage: "entry" as TimingStage, catalogueGets: 0 } +async function main() { + if (process.argv[2] === "--compare-readonly-chr-catalogues") { + const [parentPath, parentDigest, childPath, childDigest, extra] = process.argv.slice(3) + if (!parentPath || !parentDigest || !childPath || !childDigest || extra !== undefined) return fail() + const [parent, child] = await Promise.all([readTimingReceipt(parentPath, parentDigest), readTimingReceipt(childPath, childDigest)]) + return compareChrCatalogueTimingReceipts(parent, child) + } + const [mode, role, extra] = process.argv.slice(2) + if (process.platform !== "linux" || process.pid !== 1 || mode !== "--measure-readonly-chr-catalogues" || + role !== "parent" && role !== "child" || extra !== undefined || + (role === "parent" ? process.geteuid?.() !== 0 || process.getegid?.() !== 0 : !process.geteuid?.() || !process.getegid?.())) return fail() + progress.stage = "pins" + const pins = await readPins(role) + progress.stage = "namespace" + const namespace = await readChrLinuxNamespaceIdentity() + if (isDeepStrictEqual(namespace, pins.hostNamespace)) return fail() + const authorize = async () => { + if (Date.now() >= pins.deadlineAt || !isDeepStrictEqual(await readChrLinuxNamespaceIdentity(), namespace)) return fail() + } + progress.stage = "startup" + await awaitStartup(pins, namespace, authorize) + const note = (stage: TimingStage, catalogueGets: number) => { progress.stage = stage; progress.catalogueGets = catalogueGets } + if (role === "parent") { + progress.stage = "parent-reader" + const { readers, assertRouterIdentity } = await openChrParentCatalogueReaders({ ...pins, authorize }) + return measureChrCatalogueTiming({ ...pins, clients: readers, authorize, parentIdentity: assertRouterIdentity, note }) + } + progress.stage = "child-import" + const { openChrNamespaceClients } = await import("./traffic-flow-chr-namespace-relay.js") + progress.stage = "child-relay" + const bridge = await openChrNamespaceClients({ + runId: pins.runId, deadlineAt: pins.deadlineAt, pins: pins.readSockets, password: pins.readPassword, + namespace, hostNamespace: pins.hostNamespace, assertCurrent: authorize, + }) + try { + const result = await measureChrCatalogueTiming({ ...pins, clients: bridge.clients, authorize, note }) + progress.stage = "child-close" + return result + } finally { await bridge.close() } +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) { + void main().then(result => { + process.stdout.write(JSON.stringify(result) + "\n") + }).catch(error => { + process.stdout.write(JSON.stringify(chrCatalogueTimingFailure(error, progress.role, progress.stage, progress.catalogueGets)) + "\n") + process.exitCode = 1 + }) +} + + + + + diff --git a/backend/src/test/traffic-flow-chr-child-catalogue-working-set.test.ts b/backend/src/test/traffic-flow-chr-child-catalogue-working-set.test.ts new file mode 100644 index 00000000..61a0cf03 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-child-catalogue-working-set.test.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { runChrChildCatalogueWorkingSet } from "./traffic-flow-chr-child-catalogue-working-set.js" + +test("synthetic child exercises all three paired catalogue reads without native access", async () => { + const result = await runChrChildCatalogueWorkingSet("dense") + assert.deepEqual(result.calls, { A: 133, B: 133 }) + assert.equal(result.catalogueFixtureBytesPerSide, 7021528) + assert.equal(result.pairs.length, 3) + assert.equal(result.nativeWrites, 0) + assert.equal(result.postgresqlStarted, false) +}) diff --git a/backend/src/test/traffic-flow-chr-child-catalogue-working-set.ts b/backend/src/test/traffic-flow-chr-child-catalogue-working-set.ts new file mode 100644 index 00000000..ba4e3ee4 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-child-catalogue-working-set.ts @@ -0,0 +1,90 @@ +import { readFile } from "node:fs/promises" +import { fileURLToPath } from "node:url" +import { measureChrCatalogueTiming } from "./traffic-flow-chr-catalogue-timing.js" +// Load the actual child path without opening its loopback or Unix sockets. +import "./traffic-flow-chr-namespace-relay.js" + +const stressPaths = new Set([ + "/ip/firewall/filter", "/ip/firewall/nat", "/ip/firewall/raw", "/ip/firewall/mangle", + "/ipv6/firewall/filter", "/ipv6/firewall/nat", "/ipv6/firewall/raw", "/ipv6/firewall/mangle", +]) +const routers = { + A: { deviceId: "A", identity: "synthetic-A", version: "7.24.5" }, + B: { deviceId: "B", identity: "synthetic-B", version: "7.24.5" }, +} +const sides = ["A", "B"] as const + +export async function runChrChildCatalogueWorkingSet(profile: "empty" | "dense") { + const denseRows = profile === "dense" ? Buffer.from(JSON.stringify(Array.from({ length: 700 }, (_, i) => ({ + ".id": "*" + (4096 + i).toString(16), chain: "forward", action: "accept", disabled: "true", + comment: "synthetic-stress-" + i + "-" + "x".repeat(1150), + })))) : undefined + const empty = Buffer.from("[]") + const version = Buffer.from(JSON.stringify([{ version: routers.A.version }])) + const calls = { A: 0, B: 0 } + const client = (side: (typeof sides)[number]) => { + const name = Buffer.from(JSON.stringify({ name: routers[side].identity })) + return { + async get(path: string, _timeout?: number, signal?: AbortSignal, max = 1048576, before?: () => Promise) { + signal?.throwIfAborted() + await before?.() + signal?.throwIfAborted() + const raw = path === "/system/identity" ? name : path === "/system/resource" ? version + : stressPaths.has(path) && denseRows ? denseRows : empty + if (raw.length > max) throw Error("Synthetic native response exceeds actual request budget") + calls[side]++ + // Model received chunks and the client's combined JSON decode buffer. + const chunks: Buffer[] = [] + for (let offset = 0; offset < raw.length; offset += 64 * 1024) { + chunks.push(Buffer.from(raw.subarray(offset, offset + 64 * 1024))) + } + return JSON.parse(Buffer.concat(chunks, raw.length).toString("utf8")) as T + }, + } + } + const deadlineAt = Date.now() + 90000 + const result = await measureChrCatalogueTiming({ + role: "child", runId: "a012bcde", deadlineAt, routers, + clients: { A: client("A"), B: client("B") }, + authorize: async () => { if (Date.now() >= deadlineAt) throw Error("Synthetic catalogue deadline ended") }, + }) + if (calls.A !== 133 || calls.B !== 133) throw Error("Synthetic child catalogue count differs") + return { ...result, calls, catalogueFixtureBytesPerSide: (denseRows?.length ?? 0) * stressPaths.size } +} + +async function cgroupMemory() { + if (process.platform !== "linux") return undefined + const get = async (key: "memory.max" | "memory.current" | "memory.peak") => { + try { + const raw = (await readFile("/sys/fs/cgroup/" + key, "ascii")).trim() + if (raw === "max") return raw + const number = Number(raw) + if (/^[0-9]{1,19}$/.test(raw) && Number.isSafeInteger(number)) return number + } catch { return undefined } + } + const [max, current, peak] = await Promise.all([get("memory.max"), get("memory.current"), get("memory.peak")]) + return { max, current, peak } +} + +async function main() { + const [mode, profile, extra] = process.argv.slice(2) + if (mode !== "--run-inert-netflow-child-catalogue-working-set" || profile !== "empty" && profile !== "dense" || + extra !== undefined) throw Error("Invalid synthetic child catalogue profile") + const initialRssKiB = Math.ceil(process.memoryUsage.rss() / 1024) + const started = performance.now() + const result = await runChrChildCatalogueWorkingSet(profile) + const maxRssKiB = process.resourceUsage().maxRSS + if (maxRssKiB <= 0) throw Error("Synthetic child RSS unavailable") + process.stdout.write(JSON.stringify({ + synthetic: true, profile, platform: process.platform, nodeVersion: process.version, + ...result, initialRssKiB, maxRssKiB, elapsedMs: Math.ceil(performance.now() - started), + within192MiB: maxRssKiB <= 192 * 1024, cgroupMemory: await cgroupMemory(), + }) + "\n") +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) { + void main().catch(() => { + process.stderr.write('{"synthetic":true,"complete":false}\n') + process.exitCode = 1 + }) +} diff --git a/backend/src/test/traffic-flow-chr-collector-bootstrap.test.ts b/backend/src/test/traffic-flow-chr-collector-bootstrap.test.ts new file mode 100644 index 00000000..f2bd5043 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-collector-bootstrap.test.ts @@ -0,0 +1,128 @@ +import assert from "node:assert/strict" +import { spawnSync } from "node:child_process" +import { readFile } from "node:fs/promises" +import { fileURLToPath } from "node:url" +import { test } from "node:test" +import { chrCollectorDependencyPin, createChrCollectorActivation, createChrCollectorBootstrapDiagnostics, matchesChrCollectorPostgresSockets, + verifyChrCollectorDependencyMetadata } from "./traffic-flow-chr-collector-bootstrap.js" + +test("collector startup reports only fixed stage and allowlisted OS reason", () => { + const diagnostic = createChrCollectorBootstrapDiagnostics() + diagnostic.enter("runtime-files") + diagnostic.enter("secret data should never be a stage" as never) + const io = Object.assign(Error("raw secret and path"), { code: "EACCES", path: "private" }) + assert.deepEqual(diagnostic.refusal(io), { stage: "runtime-files", reason: "EACCES", automaticReplay: false }) + const unknown = Object.assign(Error("raw private value"), { code: "private-code" }) + assert.deepEqual(diagnostic.refusal(unknown), { stage: "runtime-files", reason: "refused", automaticReplay: false }) + const unexpected = new Error("another secret") + Object.defineProperty(unexpected, "code", { get() { throw Error("private getter") } }) + assert.equal(diagnostic.refusal(unexpected).reason, "refused") +}) + +test("external PG accepts only the exact two private socket directories", () => { + const owned = "/tmp/mcp-netflow-a012bcde/pg-socket" + const dual = "/var/run/postgresql," + owned + assert.equal(matchesChrCollectorPostgresSockets(dual, owned, true), true) + assert.equal(matchesChrCollectorPostgresSockets(owned, owned, false), true) + for (const invalid of [owned, owned + ",/var/run/postgresql", dual + ",/tmp", dual + " ", null]) { + assert.equal(matchesChrCollectorPostgresSockets(invalid, owned, true), false) + } + assert.equal(matchesChrCollectorPostgresSockets(dual, owned, false), false) +}) + +test("actual lockfile drizzle package fits bounded runtime metadata verification", async () => { + const lock = JSON.parse(await readFile(new URL("../../../package-lock.json", import.meta.url), "utf8")) + const dependency = "node_modules/drizzle-orm" + const pin = chrCollectorDependencyPin({ name: "drizzle-orm", path: dependency + "/package.json", + version: lock.packages[dependency].version }) + const raw = await readFile(new URL("../../../" + pin.path, import.meta.url)) + assert.ok(raw.length > 128 * 1024 && raw.length <= 256 * 1024) + verifyChrCollectorDependencyMetadata(pin, raw) + assert.throws(() => verifyChrCollectorDependencyMetadata(pin, Buffer.alloc(256 * 1024 + 1)), /size invalid/) +}) + +test("dependency diagnostic identifies only the ordinal and exact failed comparison", () => { + const pin = chrCollectorDependencyPin({ name: "@fastify/jwt", path: "node_modules/@fastify/jwt/package.json", version: "10.2.2" }) + for (const invalid of [ + { name: "..", path: "node_modules/../package.json", version: "1" }, + { ...pin, path: "node_modules/../@fastify/jwt/package.json" }, + ]) assert.throws(() => chrCollectorDependencyPin(invalid), /pin invalid/) + const diagnostic = createChrCollectorBootstrapDiagnostics() + const checks = (check: Parameters[2]>>[0]) => + diagnostic.enter("runtime-dependencies", { ordinal: 3, check }) + for (const [body, expected] of [ + ["not-json", "metadata-json"], + ['{"name":"some-private-value","version":"10.2.2"}', "package-name"], + ['{"name":"@fastify/jwt","version":"different-private-value"}', "package-version"], + ] as const) { + assert.throws(() => verifyChrCollectorDependencyMetadata(pin, Buffer.from(body), checks), error => { + assert.deepEqual(diagnostic.refusal(error), { stage: "runtime-dependencies", reason: "refused", automaticReplay: false, + dependencyOrdinal: 3, dependencyCheck: expected }) + return true + }) + } + diagnostic.enter("state-directory") + assert.equal(Object.hasOwn(diagnostic.refusal(Error()), "dependencyOrdinal"), false) +}) + +test("CLI refusal exposes only the arguments-stage marker and never raw invalid input", () => { + const entry = fileURLToPath(new URL("./traffic-flow-chr-collector-bootstrap.ts", import.meta.url)) + const result = spawnSync(process.execPath, ["--import", "tsx", entry, "--check-only", "sensitive-untrusted-value"], + { encoding: "utf8", timeout: 10000 }) + assert.equal(result.status, 1) + assert.equal(result.stdout, "") + assert.equal(result.stderr.trim(), + 'MM_NETFLOW_COLLECTOR_BOOTSTRAP_REFUSED {"stage":"arguments","reason":"refused","automaticReplay":false}') +}) + + +test("staged collector authority is lazy, capped at ten minutes and spent once", async () => { + let now = 1000 + const events: string[] = [] + const owner = createChrCollectorActivation({ + runDeadlineAt: now + 3600000, now: () => now, + authorizeFixture: async () => { events.push("authorize"); now += 200 }, + create: async deadline => { events.push("create"); return deadline }, + }) + assert.deepEqual(events, []) + const activated = owner.activateCollector(100) + await assert.rejects(owner.activateCollector(100), /already spent/) + assert.equal(await activated, 601200) + assert.deepEqual(events, ["authorize", "create"]) + await owner.settle() + await assert.rejects(owner.activateCollector(100), /already spent/) + const short = createChrCollectorActivation({ runDeadlineAt: 2000, now: () => now, + authorizeFixture: async () => {}, create: async deadline => deadline }) + assert.equal(await short.activateCollector(100), 2000) +}) + +test("staged activation refuses expired or failed authority and seals an abandoned request", async () => { + let creates = 0 + const create = async () => { creates++ } + const stale = createChrCollectorActivation({ runDeadlineAt: 1000, now: () => 1000, + authorizeFixture: async () => {}, create }) + await assert.rejects(stale.activateCollector(100), /window unavailable/) + const unauthorized = createChrCollectorActivation({ runDeadlineAt: 1000, now: () => 900, + authorizeFixture: async () => { throw Error("scope expired") }, create }) + await assert.rejects(unauthorized.activateCollector(100), /scope expired/) + await assert.rejects(unauthorized.activateCollector(100), /already spent/) + let release!: () => void + const wait = new Promise(resolve => { release = resolve }) + const abandoned = createChrCollectorActivation({ runDeadlineAt: 1000, now: () => 900, + authorizeFixture: () => wait, create }) + const pending = abandoned.activateCollector(100) + const settled = abandoned.settle() + release() + await assert.rejects(pending, /window unavailable/) + await settled + assert.equal(creates, 0) +}) + + +test("staged activation rejects a measured window that does not fit the remaining authority", async () => { + const owner = createChrCollectorActivation({ + runDeadlineAt: 1000000, now: () => 900000, authorizeFixture: async () => {}, + create: async () => assert.fail("Issued a credential without the measured window"), + }) + await assert.rejects(owner.activateCollector(100000), /measured window unavailable/) +}) diff --git a/backend/src/test/traffic-flow-chr-collector-bootstrap.ts b/backend/src/test/traffic-flow-chr-collector-bootstrap.ts new file mode 100644 index 00000000..489eb4c8 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-collector-bootstrap.ts @@ -0,0 +1,516 @@ +import { createHash, randomBytes } from "node:crypto" +import { lstat, mkdir, open, readFile, realpath } from "node:fs/promises" +import { constants } from "node:fs" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import { createInterface } from "node:readline/promises" +import { setTimeout as delay } from "node:timers/promises" +import type { createChrDisposableCollector } from "./traffic-flow-chr-disposable-collector.js" +import { readChrLinuxNamespaceIdentity } from "./traffic-flow-chr-linux-udp-owner.js" +import { CHR_STAGED_COLLECTOR_APPROVAL_MS } from "./traffic-flow-chr-target-timing-profile.js" +import type { reconcileOwnedChrFlow } from "./traffic-flow-chr-reconcile.js" +import type { AuthUser } from "../lib/permissions.js" + +type Namespace = Awaited> +type Pins = { + runId: string + deadlineAt: number + namespace: Namespace + hostNamespace: Namespace + collectorPort: number +} +function must(value: unknown, reason: string): asserts value { if (!value) throw Error(reason) } + +const bootstrapStages = ["arguments", "process-boundary", "process-scope", "process-workdir", "fixture-root", "fixture-namespace", + "runtime-node", "runtime-manifest", "runtime-files", "runtime-dependencies", "state-directory", + "pg-secret", "pg-socket-directory", "pg-start", "pg-connect", "pg-identity", "backend-import", + "db-fresh", "db-migrate", "fixture-seed", "app-ready", "mcp-principal", "collector-owner", + "collector-work", "approval-enable", "approval-disable", "enable-operation", "listener-bind", + "disable-operation", "listener-drain", "terminal-retirement"] as const +type BootstrapStage = (typeof bootstrapStages)[number] +const dependencyChecks = ["pin", "metadata-read", "metadata-size", "metadata-json", "package-name", "package-version"] as const +type DependencyCheck = (typeof dependencyChecks)[number] +type DependencyDiagnostic = { ordinal: number; check: DependencyCheck } +type EnterStage = (stage: BootstrapStage, dependency?: DependencyDiagnostic) => void +const safeIoCodes = new Set(["EACCES", "EEXIST", "EISDIR", "ELOOP", "EMFILE", "ENOENT", "ENOTDIR", "EPERM", "EROFS"]) +const maxDependencyMetadataBytes = 256 * 1024 + +export function createChrCollectorBootstrapDiagnostics() { + let current: BootstrapStage = "arguments" + let dependency: DependencyDiagnostic | undefined + return { + enter(stage: BootstrapStage, detail?: DependencyDiagnostic) { + if (!bootstrapStages.includes(stage)) return + current = stage + dependency = stage === "runtime-dependencies" && detail && + Number.isInteger(detail.ordinal) && detail.ordinal >= 1 && detail.ordinal <= 128 && dependencyChecks.includes(detail.check) + ? { ordinal: detail.ordinal, check: detail.check } : undefined + }, + refusal(error: unknown) { + const code = error instanceof Error && Object.getOwnPropertyDescriptor(error, "code")?.value + return { stage: current, reason: typeof code === "string" && safeIoCodes.has(code) ? code : "refused", + automaticReplay: false as const, ...(dependency && { dependencyOrdinal: dependency.ordinal, dependencyCheck: dependency.check }) } + }, + } +} + +export function matchesChrCollectorPostgresSockets(configured: unknown, ownedSocket: string, external: boolean) { + return configured === (external ? "/var/run/postgresql," + ownedSocket : ownedSocket) +} + +export function chrCollectorDependencyPin(value: unknown) { + must(value && typeof value === "object" && !Array.isArray(value), "Runtime dependency pin invalid") + const { name, path, version } = value as Record + must(typeof name === "string" && name.length <= 214 && + /^(?:@[a-z0-9][a-z0-9_.-]*\/)?[a-z0-9][a-z0-9_.-]*$/.test(name) && + typeof version === "string" && version.length > 0 && version.length <= 64 && typeof path === "string" && + ["node_modules/", "backend/node_modules/", "packages/contracts/node_modules/"] + .some(prefix => path === prefix + name + "/package.json"), "Runtime dependency pin invalid") + return { name, path, version } +} + +export function verifyChrCollectorDependencyMetadata(pin: ReturnType, raw: Buffer, + check: (stage: DependencyCheck) => void = () => {}) { + check("metadata-size") + must(raw.length > 0 && raw.length <= maxDependencyMetadataBytes, "Cached dependency package size invalid") + check("metadata-json") + const metadata: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(raw)) + must(metadata && typeof metadata === "object" && !Array.isArray(metadata), "Cached dependency package invalid") + const { name, version } = metadata as Record + check("package-name") + must(name === pin.name, "Cached dependency package name differs") + check("package-version") + must(version === pin.version, "Cached dependency package version differs") +} + +async function verifyCachedDependency(item: unknown, ordinal: number, enter: EnterStage) { + const check = (stage: DependencyCheck) => enter("runtime-dependencies", { ordinal, check: stage }) + check("pin") + const pin = chrCollectorDependencyPin(item) + check("metadata-read") + const file = await open("/app/" + pin.path, constants.O_RDONLY | constants.O_NOFOLLOW) + try { + const info = await file.stat() + check("metadata-size") + must(info.isFile() && info.size > 0 && info.size <= maxDependencyMetadataBytes, "Cached dependency package size invalid") + const data = Buffer.alloc(info.size + 1) + let offset = 0 + check("metadata-read") + while (offset < data.length) { + const result = await file.read(data, offset, data.length - offset, null) + if (!result.bytesRead) break + offset += result.bytesRead + } + check("metadata-size") + must(offset === info.size, "Cached dependency package changed during read") + verifyChrCollectorDependencyMetadata(pin, data.subarray(0, offset), check) + } finally { + await file.close() + } +} + +export function chrCollectorBootstrapPaths(runId: string) { + must(/^[a-f0-9]{8}$/.test(runId), "Invalid collector run ID") + const root = "/tmp/mcp-netflow-" + runId + const database = "mcp_netflow_" + runId + const socket = root + "/pg-socket" + return { root, socket, database, state: root + "/state", pg: root + "/pg", + url: "postgresql://mcp_test@localhost/" + database + "?host=" + encodeURIComponent(socket) } +} + +async function verifyCachedCollectorRuntime(enter: EnterStage) { + enter("runtime-node") + const file = "/run/mm-netflow-packet.json" + const expected = process.env.MMAPP_NETFLOW_PACKET_SHA256 + must(process.platform === "linux" && process.arch === "x64" && /^22\./.test(process.versions.node) && + /^[a-f0-9]{64}$/.test(expected ?? ""), "Pinned Node 22 runtime unavailable") + enter("runtime-manifest") + const stat = await lstat(file) + must(stat.isFile() && stat.size > 0 && stat.size <= 1048576, "Pinned runtime packet unavailable") + const raw = await readFile(file) + must(createHash("sha256").update(raw).digest("hex") === expected, "Runtime packet pin differs") + const manifest = JSON.parse(raw.toString("utf8")) + must(manifest.format === "mmapp-netflow-collector-smoke-v1" && + manifest.cachedSourceRevision === "bf3d50e1ff2ee3b5c9268991a255c3b49ce6378e" && + Array.isArray(manifest.dependencies) && manifest.dependencies.length > 0 && manifest.dependencies.length <= 128, + "Runtime dependency manifest invalid") + must(Array.isArray(manifest.files) && manifest.files.length > 0 && manifest.files.length <= 4096, + "Runtime file manifest invalid") + enter("runtime-files") + const roots = { dist: "/app/backend/dist", drizzle: "/app/backend/drizzle", "contracts-dist": "/app/packages/contracts/dist" } + const observed = new Set() + let total = 0 + for (const item of manifest.files) { + must(item && typeof item.path === "string" && item.path.length <= 256 && !observed.has(item.path) && + Number.isInteger(item.size) && item.size > 0 && item.size <= 2097152 && /^[a-f0-9]{64}$/.test(item.sha256), + "Runtime file pin invalid") + const relative: string = item.path + const [scope, ...parts] = relative.split("/") + must(Object.hasOwn(roots, scope) && parts.length && parts.every(part => /^[A-Za-z0-9_.-]+$/.test(part) && + part !== "." && part !== ".."), "Runtime file scope invalid") + total += item.size + must(total <= 128 * 1024 * 1024, "Runtime packet exceeds budget") + const path = roots[scope as keyof typeof roots] + "/" + parts.join("/") + const info = await lstat(path) + must(info.isFile() && info.size === item.size && + createHash("sha256").update(await readFile(path)).digest("hex") === item.sha256, "Runtime packet file differs") + observed.add(item.path) + } + must(observed.has("dist/test/traffic-flow-chr-collector-bootstrap.js"), "Collector entry pin absent") + for (const [index, item] of manifest.dependencies.entries()) { + await verifyCachedDependency(item, index + 1, enter) + } +} + +type Context = Awaited> & { + serverIds: readonly [1, 2] + reconcile: (options: Pick[0], "wire" | "from" | "until" | "minutes" | "interfaceLabel">) => Promise +} + +type StagedContext = { + authorizeFixture(): Promise + activateCollector(requiredWindowMs: number): Promise }> +} + +const credentialWindowMs = 600000 + +/** A staged caller spends this capability after its topology preflight. + * Capture and cleanup share one nonrenewable connection. */ +export function createChrCollectorActivation(options: { + runDeadlineAt: number + authorizeFixture(): Promise + create(credentialDeadlineAt: number): Promise + now?: () => number +}) { + const { runDeadlineAt, authorizeFixture, create, now = Date.now } = options + let activation: Promise | undefined + let closed = false + return Object.freeze({ + async activateCollector(requiredWindowMs: number) { + must(!closed && !activation, "Staged collector activation already spent") + activation = (async () => { + await authorizeFixture() + const issuedAt = now() + must(!closed && Number.isSafeInteger(issuedAt) && Number.isSafeInteger(runDeadlineAt) && + issuedAt < runDeadlineAt, "Staged collector credential window unavailable") + const credentialDeadlineAt = Math.min(runDeadlineAt, issuedAt + credentialWindowMs) + must(Number.isSafeInteger(requiredWindowMs) && requiredWindowMs > 0 && + credentialDeadlineAt - issuedAt > requiredWindowMs, "Staged collector measured window unavailable") + return create(credentialDeadlineAt) + })() + return activation + }, + async settle() { + closed = true + await activation?.catch(() => {}) + }, + }) +} +/** Supervise one private cluster and app inside the already owned C process. + * The supplied transport/generator remain trusted, reviewed caller code. */ +export async function runOwnedChrCollectorProcess(options: { + pins: Pins + postgresSource?: "pinned-container" + enterStage?: EnterStage + assertCurrent: () => Promise + reviewApproval: (plan: { planId: string; enabled: boolean; deadlineAt: number }) => Promise +} & ({ work: (context: Context) => Promise; stagedWork?: never } | + { postgresSource: "pinned-container"; work?: never; stagedWork: (context: StagedContext) => Promise })) { + const { assertCurrent, reviewApproval, work, stagedWork, enterStage: enter = () => {} } = options + const staged = typeof stagedWork === "function" + must(staged ? !work && options.postgresSource === "pinned-container" : typeof work === "function", "Invalid collector work profile") + enter("process-boundary") + const { runId, deadlineAt, collectorPort, namespace, hostNamespace } = structuredClone(options.pins) + const paths = chrCollectorBootstrapPaths(runId) + const uid = process.geteuid?.() + must(process.platform === "linux" && uid && process.env.MMAPP_OWN_CHR_NETFLOW_COLLECTOR === "true", + "Owned Linux collector process boundary unavailable") + enter("process-scope") + must(Number.isSafeInteger(deadlineAt) && Date.now() < deadlineAt && deadlineAt - Date.now() <= (staged ? 3600000 : credentialWindowMs) && + Number.isSafeInteger(collectorPort) && collectorPort >= 49152 && collectorPort <= 65535, "Owned collector scope differs") + enter("process-workdir") + must(process.cwd() === paths.root, "Owned collector working directory differs") + enter("fixture-root") + const root = await lstat(paths.root) + must(root.isDirectory() && root.uid === uid && (root.mode & 0o777) === 0o700 && await realpath(paths.root) === paths.root, + "Owned collector root differs") + enter("fixture-namespace") + const current = await readChrLinuxNamespaceIdentity() + must(current.device === namespace.device && current.inode === namespace.inode && + (current.device !== hostNamespace.device || current.inode !== hostNamespace.inode), + "Owned collector namespace differs") + await assertCurrent() + const authorizeFixture = async () => { + must(Date.now() < deadlineAt, "Owned collector fixture deadline ended") + await assertCurrent() + const observed = await readChrLinuxNamespaceIdentity() + must(Date.now() < deadlineAt && observed.device === namespace.device && observed.inode === namespace.inode, + "Owned collector fixture namespace differs") + } + const external = options.postgresSource === "pinned-container" + if (external) await verifyCachedCollectorRuntime(enter) + // The state directory is exclusive. A retained fixture is never reused. + process.umask(0o077) + enter("state-directory") + await mkdir(paths.state, { mode: 0o700 }) + let password = randomBytes(32).toString("hex") + if (external) { + enter("pg-secret") + const secret = await open("/run/mm-netflow-pg-secret", constants.O_RDONLY | constants.O_NOFOLLOW) + try { + const stat = await secret.stat() + must(stat.isFile() && stat.uid === uid && stat.size === 65 && (stat.mode & 0o777) === 0o400, + "Pinned PostgreSQL secret unavailable") + password = (await secret.readFile("utf8")).trimEnd() + must(/^[a-f0-9]{64}$/.test(password), "Pinned PostgreSQL secret invalid") + } finally { await secret.close() } + enter("pg-socket-directory") + const socket = await lstat(paths.socket) + must(socket.isDirectory() && socket.uid === uid && (socket.mode & 0o777) === 0o700 && + await realpath(paths.socket) === paths.socket, "Pinned PostgreSQL socket directory differs") + } else { + enter("pg-socket-directory") + await mkdir(paths.socket, { mode: 0o700 }) + await mkdir(paths.pg, { mode: 0o700 }) + } + const endpoint = new URL(paths.url) + endpoint.password = password + Object.assign(process.env, { NODE_ENV: "test", DATABASE_URL: endpoint.href, MCP_STATE_DIR: paths.state, + TMPDIR: paths.root, AUTH_REQUIRED: "true", AUTH_JWT_SECRET: randomBytes(32).toString("hex"), + AUTH_ISSUER: "urn:mmapp:owned-netflow:" + runId, FLOW_LISTEN_HOST: "198.18.242.3", FLOW_AGENT_SOCKET: "" }) + let stopPostgres: (() => Promise) | undefined + let closeApp: (() => Promise) | undefined, closePool: (() => Promise) | undefined + let terminalIdle: (() => Promise) | undefined + let revoke: (() => Promise) | undefined + let settleActivation: (() => Promise) | undefined + let retired = false + let retirementAttempted = false + try { + if (!external) { + enter("pg-start") + const { default: EmbeddedPostgres } = await import("embedded-postgres") + const postgres = new EmbeddedPostgres({ databaseDir: paths.pg, port: 5432, user: "mcp_test", + password, authMethod: "scram-sha-256", persistent: true, createPostgresUser: false, + initdbFlags: ["--encoding=UTF8"], postgresFlags: ["-h", "", "-k", paths.socket, "-c", "unix_socket_permissions=0700"], + onLog: () => {}, onError: () => {} }) + await postgres.initialise() + await postgres.start() + stopPostgres = () => postgres.stop() + } + enter("pg-connect") + const { Client } = await import("pg") + const pg = new Client({ host: paths.socket, database: "postgres", user: "mcp_test", password, + port: 5432, connectionTimeoutMillis: 3000, statement_timeout: 3000 }) + await pg.connect() + let oid: string + try { + enter("pg-identity") + const [server] = (await pg.query("SELECT current_user AS role,inet_server_addr() IS NULL AS unix_socket, " + + "current_setting('listen_addresses') AS listen, current_setting('unix_socket_directories') AS socket, " + + "current_setting('server_version_num')::int AS version")).rows + must(server?.role === "mcp_test" && server.unix_socket === true && server.listen === "" && + matchesChrCollectorPostgresSockets(server.socket, paths.socket, external) && + (!external || server.version >= 180000 && server.version < 190000), + "Private PostgreSQL bind or major version not proven") + if (!external) await pg.query('CREATE DATABASE "' + paths.database + '"') + const result = await pg.query("SELECT oid::text AS oid FROM pg_database WHERE datname=$1", [paths.database]) + must(result.rows.length === 1 && /^[1-9][0-9]*$/.test(result.rows[0].oid), "Disposable database OID unavailable") + oid = result.rows[0].oid + } finally { + await pg.end() + } + enter("backend-import") + const [{ pool }, { applySqlMigrations }, { MIGRATION_MANIFEST }, { buildApp }, store, { invokeMcpTool }, + { mcpConnectionCreateSchema }, { createChrDisposableCollector }, { decodeOwnedIpfix }, { reconcileOwnedChrFlow }] = await Promise.all([ + import("../db/index.js"), import("../db/migrate.js"), import("../db/migration-manifest.js"), + import("../index.js"), import("../modules/mcp/store.js"), import("../modules/mcp/runtime.js"), + import("@mmapp/contracts/mcp"), import("./traffic-flow-chr-disposable-collector.js"), + import("./traffic-flow-chr-independent-ipfix.js"), import("./traffic-flow-chr-reconcile.js"), + ]) + closePool = () => pool.end() + enter("db-fresh") + const baseline = (await pool.query("SELECT count(*)::int AS n FROM pg_tables WHERE schemaname='public'")).rows + must(baseline.length === 1 && baseline[0].n === 0, "Disposable database is not fresh") + enter("db-migrate") + await applySqlMigrations(pool) + const applied = (await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n + must(applied === MIGRATION_MANIFEST.length, "Migration manifest differs") + enter("fixture-seed") + await pool.query("INSERT INTO servers(id,name,host,mgmt_tunnel_ip) VALUES " + + "(1,'owned CHR A','194.226.121.150','198.18.241.2'),(2,'owned CHR B','104.165.244.7','198.18.241.9')") + await pool.query("INSERT INTO traffic_flow_settings(id,enabled,collector_ip,flow_listen_port) VALUES(1,false,$1,$2) " + + "ON CONFLICT(id) DO UPDATE SET enabled=false,collector_ip=EXCLUDED.collector_ip,flow_listen_port=EXCLUDED.flow_listen_port", + ["198.18.242.3", collectorPort]) + enter("app-ready") + const app = await buildApp({ startScheduler: false, logger: false }) + closeApp = () => app.close() + await app.ready() + const create = async (credentialDeadlineAt: number): Promise => { + const actor: AuthUser = { id: "netflow-operator-" + runId, name: "Owned NetFlow operator", email: "operator@example.invalid", + apps: ["mm"], permissions: ["mm:settings:admin", "mm:traffic:admin"], expiresAt: credentialDeadlineAt } + enter("mcp-principal") + await store.updateMcpSettings({ enabled: true }, actor) + const access = await store.createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned NetFlow " + runId, mode: "autonomous", + permissions: ["mm:traffic:read", "mm:traffic:write"], expiresAt: new Date(credentialDeadlineAt).toISOString(), + scope: { allowGlobal: true, serverIds: [1, 2], operationIds: ["netflow.settings.update", "netflow.settings.read", "traffic.flow.talkers.read"] }, + limits: { callsPerMinute: 240, mutationsPerHour: 8, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 64 } }), actor) + revoke = async () => { + revoke = undefined + await store.setConnectionStatus(access.connection.id, "revoked", actor) + } + const principal = await store.authenticateMcpToken(access.token) + enter("collector-owner") + const collector = await createChrDisposableCollector({ + runId, deadlineAt: credentialDeadlineAt, namespace, hostNamespace, port: collectorPort, + serverIds: [1, 2], database: { name: paths.database, role: "mcp_test", oid }, principal, + assertCurrent: staged ? authorizeFixture : assertCurrent, + awaitHumanApproval: async (planId, enabled, approvalDeadline) => { + enter(enabled ? "approval-enable" : "approval-disable") + const deadlineAt = staged ? Math.min(approvalDeadline, Date.now() + CHR_STAGED_COLLECTOR_APPROVAL_MS) : approvalDeadline + const command = await reviewApproval({ planId, enabled, deadlineAt }) + must(command === "approve " + planId && Date.now() < deadlineAt, "Exact plan approval not given") + const token = app.jwt.sign({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }, + { iss: process.env.AUTH_ISSUER, expiresIn: "1m" }) + const response = await app.inject({ method: "POST", url: "/api/mcp/plans/" + planId + "/decision", + headers: { authorization: "Bearer " + token }, payload: { approve: true } }) + must(response.statusCode === 200 && response.json().ok === true, "Ordinary MCP approval refused") + enter(enabled ? "enable-operation" : "disable-operation") + }, + }) + terminalIdle = async () => { + const observed = await collector.collector.read() + return observed.enabled === false && !observed.workerAlive && observed.boundAddress === null && observed.udpOwners.length === 0 + } + enter("collector-work") + return { ...collector, serverIds: [1, 2], reconcile: evidence => + reconcileOwnedChrFlow({ ...evidence, decoded: decodeOwnedIpfix(evidence.wire), serverIds: [1, 2], + assertCurrent: collector.collector.authorize, + callTool: (tool, input) => invokeMcpTool(principal, tool, input), + readOnlySnapshot: async read => { + const connection = await pool.connect() + try { + await connection.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + await connection.query("SET LOCAL statement_timeout='2000ms'") + const result = await read(async (text, parameters) => (await connection.query(text, [...parameters])).rows) + await connection.query("COMMIT") + connection.release() + return result + } catch (error) { + connection.release(true) + throw error + } + }, + }), + } + } + const retireCollector = async () => { + must(!retirementAttempted, "Collector retirement already attempted") + retirementAttempted = true + enter("terminal-retirement") + must(terminalIdle && revoke && await terminalIdle(), "Collector did not retire") + await revoke() + terminalIdle = undefined + retired = true + } + let value: T + if (stagedWork) { + const activation = createChrCollectorActivation({ + runDeadlineAt: deadlineAt, authorizeFixture, + create: async credentialDeadlineAt => Object.freeze({ ...await create(credentialDeadlineAt), credentialDeadlineAt, retireCollector }), + }) + settleActivation = activation.settle + value = await stagedWork({ authorizeFixture, activateCollector: activation.activateCollector }) + } else { + value = await work(await create(deadlineAt)) + } + enter("terminal-retirement") + must(terminalIdle && await terminalIdle(), "Collector did not retire") + await revoke?.() + terminalIdle = undefined + return value + } finally { + try { + await settleActivation?.() + if (terminalIdle && !await terminalIdle().catch(() => false)) { + // Retire only the owned Worker. Preserve the DB, receipts and unresolved + // settings state; no second enable/disable plan is dispatched here. + const ingest = await import("../services/traffic-flow-ingest.js") + await ingest.stopTrafficFlowListenerForDatabaseOperation() + } + } finally { + try { + if (staged) await revoke?.() + } finally { + try { await closeApp?.() } finally { + try { await closePool?.() } finally { await stopPostgres?.() } + } + } + } + } +} + +async function main(enter: EnterStage) { + const [mode, runId, ...extra] = process.argv.slice(2) + const paths = chrCollectorBootstrapPaths(runId) + if (mode === "--check-only" && !extra.length) { + console.log(JSON.stringify({ root: paths.root, database: paths.database, socket: paths.socket, + networkUsed: false, collectorStarted: false, requiresTrustedCaller: true })) + return + } + must(["--collector-cycle", "--container-collector-cycle"].includes(mode) && extra.length === 5 && process.stdin.isTTY && process.stdout.isTTY, + "Collector-only cycle requires exact namespace and port pins and an interactive operator") + const [port, namespaceDevice, namespaceInode, hostDevice, hostInode] = extra + must(/^\d{5}$/.test(port) && extra.slice(1).every(value => /^[1-9][0-9]{0,19}$/.test(value)), "Collector pins malformed") + const deadlineAt = Date.now() + 300000 + const tty = createInterface({ input: process.stdin, output: process.stdout }) + const interrupted = new AbortController() + const interrupt = () => interrupted.abort() + process.once("SIGINT", interrupt) + process.once("SIGTERM", interrupt) + try { + const result = await runOwnedChrCollectorProcess({ + enterStage: enter, + postgresSource: mode === "--container-collector-cycle" ? "pinned-container" : undefined, + pins: { runId, deadlineAt, collectorPort: Number(port), namespace: { device: namespaceDevice, inode: namespaceInode }, + hostNamespace: { device: hostDevice, inode: hostInode } }, + assertCurrent: async () => { must(!interrupted.signal.aborted && Date.now() < deadlineAt, "Collector cycle expired or interrupted") }, + reviewApproval: async ({ planId, enabled, deadlineAt: approvalDeadline }) => tty.question( + "Collector " + (enabled ? "enable" : "disable") + " plan " + planId + ". Enter approve followed by the exact ID: ", + { signal: AbortSignal.any([interrupted.signal, AbortSignal.timeout(Math.max(1, approvalDeadline - Date.now()))]) }), + work: async ({ collector }) => { + enter("enable-operation") + await collector.applyEnabled(true, collector.authorize) + enter("listener-bind") + const bindDeadline = Math.min(deadlineAt, Date.now() + 5000) + for (;;) { + const value = await collector.read() + if (value.boundAddress === "198.18.242.3:" + port && value.workerAlive && value.udpOwners.length === 1) break + must(Date.now() < bindDeadline, "Collector socket readiness unconfirmed") + await delay(50) + } + enter("disable-operation") + await collector.applyEnabled(false, collector.authorize) + enter("listener-drain") + await collector.assertDrained() + return { collectorCycleVerified: true, nativeAcceptance: false } + }, + }) + console.log(JSON.stringify(result)) + } finally { + process.off("SIGINT", interrupt) + process.off("SIGTERM", interrupt) + tty.close() + } +} + +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const diagnostics = createChrCollectorBootstrapDiagnostics() + main(diagnostics.enter).catch(error => { + console.error("MM_NETFLOW_COLLECTOR_BOOTSTRAP_REFUSED " + JSON.stringify(diagnostics.refusal(error))) + process.exitCode = 1 + }) +} + + + + diff --git a/backend/src/test/traffic-flow-chr-disabled-baseline.test.ts b/backend/src/test/traffic-flow-chr-disabled-baseline.test.ts new file mode 100644 index 00000000..06f49af3 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-disabled-baseline.test.ts @@ -0,0 +1,74 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createDisabledChrFlowHarness } from "./traffic-flow-chr-disabled-baseline.js" + +function fixture(lose = false, idless = false) { + const baseline = { + deviceId: "fixture", flow: { ...(idless ? {} : { ".id": "*F" }), enabled: "false", interfaces: "all", untouched: "1m" }, + ipfix: { ...(idless ? {} : { ".id": "*A" }), bytes: "false", packets: "true", "src-address": "true", "dst-address": "true", + protocol: "true", "src-port": "true", "dst-port": "true", "in-interface": "true", "out-interface": "true" }, + targets: [] as Record[], + } + const state = structuredClone(baseline) + const writes: Record[] = [] + const journal: string[] = [] + const dispatches: string[] = [] + async function change(method: string, path: string, body: Record, + fence?: () => Promise): Promise { + await fence?.() + dispatches.push(method + " " + path) + assert.ok(Object.values(body).every(value => typeof value === "string")) + writes.push(body as Record) + const row = path.includes("/ipfix/") ? state.ipfix : state.flow + Object.assign(row, Object.fromEntries(Object.entries(body).map(([key, value]) => + [key, key === "interfaces" ? value : value === "yes" ? "true" : "false"]))) + if (lose) throw Error("lost ACK") + return undefined as T + } + const harness = createDisabledChrFlowHarness({ + baseline, transitInterface: "transit-test", read: async () => state, authorize: async () => {}, + checkpoint: async entry => { journal.push(entry.step + "." + entry.phase) }, + native: { + patch: (path, body, _ms, fence) => change("PATCH", path, body, fence), + post: (path, body, _ms, _signal, fence) => change("POST", path, body, fence), + }, + }) + return { baseline, state, writes, journal, harness, dispatches } +} + +test("changes only required fields and one transit interface, then restores the exact disabled inventory", async () => { + const { baseline, state, writes, journal, harness } = fixture() + assert.equal((await harness.enable()).flow.enabled, "true") + assert.deepEqual(await harness.restore(), { restored: true, automaticReplay: false }) + assert.deepEqual(state, baseline) + assert.deepEqual(writes, [{ bytes: "yes" }, { enabled: "yes", interfaces: "transit-test" }, + { enabled: "no", interfaces: "all" }, { bytes: "no" }]) + assert.deepEqual(journal.filter(value => value.endsWith(".verified")), + ["ipfix.enable.verified", "flow.enable.verified", "flow.restore.verified", "ipfix.restore.verified"]) +}) + +test("missing singleton IDs use only fixed POST /set and require ACK before inverse", async () => { + const { baseline, state, dispatches, harness } = fixture(false, true) + await harness.enable() + await harness.restore() + assert.deepEqual(state, baseline) + assert.deepEqual(dispatches, ["POST /ip/traffic-flow/ipfix/set", "POST /ip/traffic-flow/set", + "POST /ip/traffic-flow/set", "POST /ip/traffic-flow/ipfix/set"]) + const lost = fixture(true, true) + await assert.rejects(lost.harness.enable(), /requires reconciliation/) + await assert.rejects(lost.harness.restore(), /No verified/) + assert.deepEqual(lost.dispatches, ["POST /ip/traffic-flow/ipfix/set"]) +}) + +test("refuses restore while a target remains and refuses replay after unknown acknowledgement", async () => { + const known = fixture() + await known.harness.enable() + known.state.targets.push({ ".id": "*1" }) + await assert.rejects(known.harness.restore(), /baseline changed/) + assert.equal(known.writes.length, 2) + const unknown = fixture(true) + await assert.rejects(unknown.harness.enable(), /requires reconciliation/) + await assert.rejects(unknown.harness.enable(), /already attempted/) + await assert.rejects(unknown.harness.restore(), /No verified/) + assert.equal(unknown.writes.length, 1) +}) diff --git a/backend/src/test/traffic-flow-chr-disabled-baseline.ts b/backend/src/test/traffic-flow-chr-disabled-baseline.ts new file mode 100644 index 00000000..c6aa6615 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-disabled-baseline.ts @@ -0,0 +1,116 @@ +import { runChrWrite, type ChrWriteAdmission } from "./traffic-flow-chr-write-admission.js" +import { isDeepStrictEqual } from "node:util" +import type { MikrotikClient } from "../services/mikrotik.js" +import { encodeRosId } from "../services/routeros-native-paths.js" + +type Row = Record +type Inventory = { deviceId: string; flow: Row; ipfix: Row; targets: Row[] } +type Step = "ipfix.enable" | "flow.enable" | "flow.restore" | "ipfix.restore" +type Options = { + baseline: Inventory + relayWrite?: { side: "A" | "B"; admission: ChrWriteAdmission } + transitInterface: string + read: () => Promise + authorize: () => Promise + verifyContext?: () => Promise + native: Pick + checkpoint: (entry: { step: Step; phase: "intent" | "acknowledged" | "verified"; + method: "PATCH" | "POST"; path: string; id?: string }) => Promise +} +const fields = ["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", "dst-port", "in-interface", "out-interface"] +const yes = (value: unknown) => value === true || value === "true" || value === "yes" +const no = (value: unknown) => value === false || value === "false" || value === "no" +const base = "/ip/traffic-flow" +function must(value: unknown, message: string): asserts value { + if (!value) throw Error(message) +} +function idOf(row: Row) { + if (!Object.hasOwn(row, ".id")) return undefined + const id = row[".id"] + must(typeof id === "string" && /^\*[a-f0-9]{1,16}$/i.test(id), "Exact Traffic Flow singleton ID required") + return id +} + +export function chrDisabledFlowPatchCount(ipfix: Row) { + must(fields.every(key => yes(ipfix[key]) || no(ipfix[key])), "Exact restorable IPFIX fields required") + return fields.some(key => no(ipfix[key])) ? 2 : 1 +} + +/** Scope is an already proven isolated transit interface. This component owns + * singleton changes only. The separate target owner must remove its ACKed row + * and conserve both singletons before restore is admitted. */ +export function createDisabledChrFlowHarness(options: Options) { + const baseline = structuredClone(options.baseline) + const { transitInterface, authorize, verifyContext, read, native, checkpoint, relayWrite } = options + must(baseline.deviceId && baseline.targets.length === 0 && no(baseline.flow.enabled), "Disabled target-free baseline required") + must(/^[a-zA-Z0-9_.-]{1,64}$/.test(transitInterface) && transitInterface !== "all", "Exact transit interface required") + const flowId = idOf(baseline.flow), ipfixId = idOf(baseline.ipfix) + must(typeof baseline.flow.interfaces === "string" && baseline.flow.interfaces.length > 0, "Exact restorable settings required") + chrDisabledFlowPatchCount(baseline.ipfix) + const missing = fields.filter(key => no(baseline.ipfix[key])) + const enableFields = Object.fromEntries(missing.map(key => [key, "yes"])) + const restoreFields = Object.fromEntries(missing.map(key => [key, "no"])) + let phase: "new" | "blocked" | "enabled" | "restored" = "new" + let active: Inventory | undefined + async function current() { + await authorize() + const result = structuredClone(await read()) + await verifyContext?.() + await authorize() + return result + } + async function change(step: Step, kind: "flow" | "ipfix", before: Inventory, body: Record) { + const id = kind === "flow" ? flowId : ipfixId + const path = (kind === "flow" ? base : base + "/ipfix") + "/" + (id ? encodeRosId(id) : "set") + const method = id ? "PATCH" : "POST" + const record = { step, path, method, ...(id && { id }) } as const + must(isDeepStrictEqual(await current(), before), "Traffic Flow baseline changed") + await checkpoint({ ...record, phase: "intent" }) + let dispatched = false + try { + await runChrWrite(relayWrite?.admission, relayWrite && { step: "flow:" + relayWrite.side + ":" + step, + side: relayWrite.side, deviceId: baseline.deviceId, method, path, body }, async arm => { + const beforeDispatch = async () => { + must(!dispatched && isDeepStrictEqual(await current(), before) && !dispatched, "Traffic Flow dispatch refused") + dispatched = true + await arm() + } + if (id) await native.patch(path, body, 15000, beforeDispatch) + else await native.post(path, body, 15000, undefined, beforeDispatch) + }) + must(dispatched, "Traffic Flow dispatch fence omitted") + await checkpoint({ ...record, phase: "acknowledged" }) + const after = await current() + const changed = after[kind] + must(Object.entries(body).every(([key, value]) => key === "interfaces" ? changed[key] === value : + value === "yes" ? yes(changed[key]) : no(changed[key])), "Traffic Flow settings differ") + const conserved = structuredClone(after) + for (const key of Object.keys(body)) conserved[kind][key] = before[kind][key] + must(isDeepStrictEqual(conserved, before), "Unrelated Traffic Flow settings changed") + await checkpoint({ ...record, phase: "verified" }) + return after + } catch (error) { + if (dispatched) throw Error("Traffic Flow write requires reconciliation. Automatic replay forbidden.") + throw error + } + } + return { + async enable() { + must(phase === "new", "Traffic Flow enable already attempted") + phase = "blocked" + const before = missing.length ? await change("ipfix.enable", "ipfix", baseline, enableFields) : baseline + active = await change("flow.enable", "flow", before, { enabled: "yes", interfaces: transitInterface }) + phase = "enabled" + return structuredClone(active) + }, + async restore() { + must(phase === "enabled" && active, "No verified enabled state to restore") + phase = "blocked" + const after = await change("flow.restore", "flow", active, { enabled: "no", interfaces: String(baseline.flow.interfaces) }) + const restored = missing.length ? await change("ipfix.restore", "ipfix", after, restoreFields) : after + must(isDeepStrictEqual(restored, baseline), "Original Traffic Flow baseline not restored") + phase = "restored" + return { restored: true as const, automaticReplay: false as const } + }, + } +} diff --git a/backend/src/test/traffic-flow-chr-disabled-runner.test.ts b/backend/src/test/traffic-flow-chr-disabled-runner.test.ts new file mode 100644 index 00000000..fa525a45 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-disabled-runner.test.ts @@ -0,0 +1,116 @@ +import assert from "node:assert/strict" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { test } from "node:test" +import { chrExportDrainMs, executeDisabledChrCapture, type DisabledChrCaptureOptions } from "./traffic-flow-chr-disabled-runner.js" + +test("export drain requires a bounded inactive-flow timeout plus two seconds", () => { + assert.equal(chrExportDrainMs("15s"), 17000) + assert.equal(chrExportDrainMs("00:00:30"), 32000) + for (const value of ["", "0s", "1m1s", "300", undefined]) { + assert.throws(() => chrExportDrainMs(value), /Inactive flow timeout/) + } +}) + +const cases = { + "create ACK loss": /Target creation outcome unknown/, + "unsealed direct capture": /capture producer stopped/, + "staged capture ACK missing": /capture acknowledgement unavailable/, + "staged enable ACK loss": /Target enable outcome unknown/, + "unsealed staged capture": /capture producer stopped/, +} as const + +for (const mode of Object.keys(cases) as (keyof typeof cases)[]) test(mode, async () => { + const staged = mode.includes("staged") + const dir = await mkdtemp(join(tmpdir(), "mm-chr-disabled-")) + const actions: string[] = [] + const cleanup: boolean[] = [] + let topologyChecks = 0, topologyChecksAtCapture = 0 + function router(index: number) { + const name = "nfa012bcde" + (index === 0 ? "ea" : "eb") + const baseline = { deviceId: String(index), flow: { ".id": "*F", enabled: "false", interfaces: "all", "inactive-flow-timeout": "15s" }, + ipfix: { ".id": "*A", ...Object.fromEntries(["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", + "dst-port", "in-interface", "out-interface"].map(key => [key, "true"])) }, targets: [] as Record[] } + const state = structuredClone(baseline) + return { baseline, transitInterface: name, interfaces: [{ id: "*1", name, ifIndex: "1" }, + { id: "*2", name: "nfa012bcde" + (index === 0 ? "ma" : "mb"), ifIndex: "2" }] as const, + read: async () => state, counters: async () => { + if (staged) actions.push("counters:" + index) + return { startedAt: 1, observedAt: 1, rows: [] } + }, + native: { async post() { throw Error("unexpected POST") }, + async patch(_path: string, body: Record, _timeout?: number, fence?: () => Promise) { + await fence?.() + actions.push("flow:" + index) + Object.assign(state.flow, body, { enabled: "true" }) + return undefined as T + } }, + targetNative: { async put(_path: string, body: Record, _timeout?: number, fence?: () => Promise): Promise { + await fence?.() + actions.push("target:" + index) + if (mode === "create ACK loss") throw Error("lost ACK") + assert.equal(body.disabled, staged ? "yes" : undefined) + state.targets.push({ ...body, ".id": "*C", disabled: body.disabled ?? "false" }) + return { ".id": "*C" } as T + }, async patch(path: string, body: Record, _timeout?: number, fence?: () => Promise) { + await fence?.() + assert.equal(path, "/ip/traffic-flow/target/*C") + assert.deepEqual(body, { disabled: "no" }) + assert.equal(state.targets[0]?.disabled, "yes") + state.targets[0].disabled = "false" + actions.push("enable:" + index) + if (mode === "staged enable ACK loss") throw Error("lost ACK") + return { ".id": "*C" } as T + }, async delete() { actions.push("unexpected inverse") } } } + } + const journalPath = join(dir, "owned.jsonl") + try { + const options: DisabledChrCaptureOptions = { + journalPath, deadlineAt: Date.now() + 180000, ...(staged ? { stagedTarget: true as const } : {}), + scope: { sources: ["192.0.2.1", "192.0.2.2"], destination: "192.0.2.3", port: 50000 }, + expected: [], serverIds: [1, 2], routers: [router(0), router(1)], + assertTopology: async () => { topologyChecks++ }, authorize: async () => {}, + capture: async exercise => { + if (staged) actions.push("capture") + topologyChecksAtCapture = topologyChecks + await exercise(new AbortController().signal, staged && mode !== "staged capture ACK missing" + ? { kind: "staged-ipfix-timeout-20s", captureDeadlineAt: Date.now() + 120000 } : undefined) + throw Error("unexpected capture") + }, + generate: async () => { + if (staged) { + assert.equal(topologyChecks - topologyChecksAtCapture, 5) // Two per enable, one before generation. + actions.push("generate") + } + throw Error("capture producer stopped before seal") + }, + drainCollector: async () => { actions.push("unexpected drain") }, + reconcile: async () => {}, reportRouterCleanup: clean => cleanup.push(clean), + } + await assert.rejects(executeDisabledChrCapture(options), cases[mode]) + await assert.rejects(executeDisabledChrCapture(options), { code: "EEXIST" }) + const prefix = ["flow:0", "target:0", "flow:1", "target:1"] + assert.deepEqual(actions, { + "create ACK loss": prefix.slice(0, 2), + "unsealed direct capture": prefix, + "staged capture ACK missing": [...prefix, "capture"], + "staged enable ACK loss": [...prefix, "capture", "enable:0"], + "unsealed staged capture": [...prefix, "capture", "enable:0", "enable:1", "counters:0", "counters:1", "generate"], + }[mode]) + assert.deepEqual(cleanup, [false]) + const journal = (await readFile(journalPath, "utf8")).trim().split("\n").map(line => JSON.parse(line)) + if (staged && mode !== "staged capture ACK missing") { + assert(journal.find(entry => entry.phase === "capture-ready")) + } + assert.deepEqual(journal.at(-1), { + "create ACK loss": { device: 0, action: "create", phase: "intent" }, + "unsealed direct capture": { device: 1, action: "create", phase: "verified", id: "*C" }, + "staged capture ACK missing": { phase: "targets-staged-disabled" }, + "staged enable ACK loss": { device: 0, action: "enable", phase: "intent", id: "*C" }, + "unsealed staged capture": { device: 1, action: "enable", phase: "verified", id: "*C" }, + }[mode]) + } finally { + await rm(dir, { recursive: true, force: true }) + } +}) diff --git a/backend/src/test/traffic-flow-chr-disabled-runner.ts b/backend/src/test/traffic-flow-chr-disabled-runner.ts new file mode 100644 index 00000000..73cecc4e --- /dev/null +++ b/backend/src/test/traffic-flow-chr-disabled-runner.ts @@ -0,0 +1,224 @@ +import { open } from "node:fs/promises" +import { dirname } from "node:path" +import { setTimeout as delay } from "node:timers/promises" +import { createDisabledChrFlowHarness } from "./traffic-flow-chr-disabled-baseline.js" +import { assertChrTargetTimerWindow, createChrTargetHarness, type ChrStagedTargetTimerProfile, type ChrTargetTimerProfile } from "./traffic-flow-chr-target-harness.js" +import { verifyChrOwnedWgEvidence } from "./traffic-flow-chr-owned-wg-evidence.js" +import { projectChrWire } from "./traffic-flow-chr-wire.js" + +import type { ChrWriteAdmission } from "./traffic-flow-chr-write-admission.js" + +type Flow = Parameters[0] +type Target = Parameters[0] +type Evidence = Parameters[0] +type Counters = Evidence["exporters"][0]["before"] +type InterfacePins = Evidence["exporters"][0]["interfaces"] +type Router = Pick & { + targetNative: Target["native"] + interfaces: InterfacePins + counters: () => Promise +} +type Capture = Pick +export type DisabledChrCaptureOptions = { + journalPath: string + deadlineAt: number + writeAdmission?: ChrWriteAdmission + targetTimerProfile?: Omit + stagedTarget?: true + scope: Evidence["scope"] + expected: Evidence["expected"] + serverIds: readonly [number, number] + routers: readonly [Router, Router] + assertTopology: () => Promise + authorize: () => Promise + capture: (exercise: (signal: AbortSignal, timer?: Omit) => Promise) => Promise + generate: (signal: AbortSignal) => Promise<{ lastDatagramAt: number }> + drainCollector: () => Promise + reconcile: (wire: ReturnType, serverIds: readonly [number, number]) => Promise + reportRouterCleanup: (clean: boolean) => void +} +function must(value: unknown, reason: string): asserts value { if (!value) throw Error(reason) } + +export function chrExportDrainMs(value: unknown) { + must(typeof value === "string", "Inactive flow timeout unavailable") + const compact = /^(?:(\d{1,2})m)?(?:(\d{1,2})s)?$/.exec(value) + const clock = /^(\d{2}):(\d{2}):(\d{2})$/.exec(value) + const seconds = compact ? Number(compact[1] ?? 0) * 60 + Number(compact[2] ?? 0) : + clock ? Number(clock[1]) * 3600 + Number(clock[2]) * 60 + Number(clock[3]) : 0 + must(seconds >= 1 && seconds <= 60, "Inactive flow timeout outside owned capture budget") + return seconds * 1000 + 2000 +} + +/** Trusted local runner composition. The caller owns topology and collector. + * A post-dispatch ambiguity is never converted into a synthetic cleanup ACK. */ +export async function executeDisabledChrCapture(options: DisabledChrCaptureOptions) { + const scope = structuredClone(options.scope), expected = structuredClone(options.expected) + const serverIds = structuredClone(options.serverIds), deadline = options.deadlineAt + const writeAdmission = options.writeAdmission + const targetTimerProfile = options.targetTimerProfile && { ...options.targetTimerProfile } + const staged = options.stagedTarget === true + const routers = options.routers.map(router => ({ ...router, baseline: structuredClone(router.baseline), + interfaces: structuredClone(router.interfaces) })) + const live = () => must(Number.isSafeInteger(deadline) && Date.now() < deadline, "Disabled capture authority expired") + const authorize = async () => { + live() + await options.authorize() + live() + } + const verifyTopology = async () => { + await authorize() + await options.assertTopology() + await authorize() + } + live() + must(deadline - Date.now() <= 600000 && routers.length === 2 && routers[0].baseline.deviceId !== routers[1].baseline.deviceId && + serverIds.length === 2 && new Set(serverIds).size === 2 && serverIds.every(id => Number.isSafeInteger(id) && id > 0) && + routers.every(router => router.interfaces.some(pin => pin.name === router.transitInterface && /e[ab]$/.test(pin.name))), + "Invalid disabled capture fixture") + must(!staged || (!targetTimerProfile && routers.every(router => "patch" in router.targetNative && typeof router.targetNative.patch === "function" && + router.baseline.targets.length === 0 && ["15s", "00:00:15"].some(value => value === router.baseline.flow["inactive-flow-timeout"]))), + "Invalid staged target fixture") + const drainMs = Math.max(...routers.map(router => chrExportDrainMs(router.baseline.flow["inactive-flow-timeout"]))) + const assertTargetTimersFit = () => { + if (!targetTimerProfile) return + must(targetTimerProfile.captureDeadlineAt <= deadline, "IPFIX capture timer exceeds the owner deadline") + for (const router of routers) assertChrTargetTimerWindow(targetTimerProfile, router.baseline.flow["inactive-flow-timeout"]) + } + assertTargetTimersFit() + const journal = await open(options.journalPath, "wx", 0o600) + const append = async (record: object) => { + await journal.writeFile(JSON.stringify(record) + "\n") + await journal.sync() + } + const states = routers.map(() => ({ clean: true, inverse: undefined as (() => Promise) | undefined })) + let work: Promise | undefined, closed = false + let before: Counters[] | undefined, finalExport: { lastDatagramAt: number; completedAt: number } | undefined + let captureSealed = false + let captureSignal: AbortSignal | undefined + const routerAuthority = async () => { + must(captureSealed || (!closed && !captureSignal?.aborted), "Disabled capture cancelled") + await authorize() + must(captureSealed || (!closed && !captureSignal?.aborted), "Disabled capture cancelled") + } + const routerContext = async () => { + await routerAuthority() + await options.assertTopology() + await routerAuthority() + } + const stageRouters = async () => { + const targets: ReturnType[] = [] + for (const [index, router] of routers.entries()) { + const state = states[index] + const checkpoint = (entry: object) => append({ device: index, ...entry }) + const relayWrite = writeAdmission && { side: index === 0 ? "A" as const : "B" as const, admission: writeAdmission } + const flow = createDisabledChrFlowHarness({ ...router, authorize: routerAuthority, verifyContext: routerContext, checkpoint, relayWrite }) + await routerAuthority() + state.clean = false + const active = await flow.enable() + state.inverse = async () => { await flow.restore(); state.clean = true } + const targetOptions = { baseline: active, read: router.read, + source: scope.sources[index], destination: scope.destination, port: scope.port, authorize: routerAuthority, + verifyContext: routerContext, checkpoint, relayWrite } + const native = router.targetNative + const target = staged ? (() => { + must("patch" in native && typeof native.patch === "function", "Staged target enable transport unavailable") + return createChrTargetHarness({ ...targetOptions, stagedDisabled: true, native }) + })() : createChrTargetHarness({ ...targetOptions, native, timerProfile: targetTimerProfile }) + await routerAuthority() + state.inverse = undefined + await target.create() + state.inverse = async () => { await target.remove(); await flow.restore(); state.clean = true } + targets.push(target) + } + return targets + } + try { + if (process.platform !== "win32") { + const dir = await open(dirname(options.journalPath), "r") + try { await dir.sync() } finally { await dir.close() } + } + await verifyTopology() + await append({ phase: "admitted", scope, serverIds }) + const stagedTargets = staged ? await stageRouters() : undefined + if (stagedTargets) await append({ phase: "targets-staged-disabled" }) + const capture = await options.capture((signal, captureTimer) => { + must(!work && !closed && !signal.aborted, "Disabled capture exercise already dispatched or cancelled") + work = (async () => { + captureSignal = signal + const timer = captureTimer && { ...captureTimer } + must(staged ? timer?.kind === "staged-ipfix-timeout-20s" && timer.captureDeadlineAt <= deadline : !timer, + "Staged capture acknowledgement unavailable") + if (timer) for (const router of routers) assertChrTargetTimerWindow(timer, router.baseline.flow["inactive-flow-timeout"]) + if (stagedTargets) await routerAuthority() + else await routerContext() + if (!stagedTargets) before = await Promise.all(routers.map(router => router.counters())) + assertTargetTimersFit() + const targets = stagedTargets ?? await stageRouters() + if (timer) { + await append({ phase: "capture-ready", captureDeadlineAt: timer.captureDeadlineAt }) + for (const target of targets) await target.enable(timer) + } + await routerAuthority() + if (stagedTargets) { + await routerContext() + for (const target of stagedTargets) await target.assertStagedTimerReady() + before = await Promise.all(routers.map(router => router.counters())) + } else if (targetTimerProfile) { + for (const target of targets) await target.assertTimerReady() + } + await routerAuthority() + const generatedAfter = Date.now() + const { lastDatagramAt } = await options.generate(signal) + must(Number.isSafeInteger(lastDatagramAt) && lastDatagramAt >= generatedAfter && lastDatagramAt <= Date.now() && + lastDatagramAt + drainMs < Math.min(deadline, timer?.captureDeadlineAt ?? targetTimerProfile?.captureDeadlineAt ?? deadline), + "Final export drain cannot fit the owned window") + await append({ phase: "final-export-drain", lastDatagramAt, drainMs }) + await delay(Math.max(0, lastDatagramAt + drainMs - Date.now()), undefined, { signal }) + // The staged path takes its complete post-traffic snapshot after the + // host seals capture, with no native write between the two points. + if (stagedTargets) await routerAuthority() + else await routerContext() + const completedAt = Date.now() + must(completedAt >= lastDatagramAt + drainMs && (!timer || completedAt < timer.captureDeadlineAt), "Final export inactivity unconfirmed") + await append({ phase: "final-export-drain-complete", lastDatagramAt, drainMs, completedAt }) + finalExport = { lastDatagramAt, completedAt } + })() + return work + }) + must(work, "Capture omitted the disabled exercise") + await work + if (!staged) await verifyTopology() + await authorize() + const after = await Promise.all(routers.map(router => router.counters())) + if (staged) await verifyTopology() + must(before?.length === 2 && after.length === 2 && finalExport && states.every(state => state.inverse), "Disabled capture evidence incomplete") + const evidence = verifyChrOwnedWgEvidence({ ...capture, scope, expected, requireRefresh: true, + ...(staged ? { counterWindow: "traffic" as const } : {}), exporters: [ + { interfaces: routers[0].interfaces, before: before[0], after: after[0] }, + { interfaces: routers[1].interfaces, before: before[1], after: after[1] }, + ] }) + await append({ phase: "wire-bound", captureSha256: evidence.captureSha256, ...finalExport }) + captureSealed = true + // The parent independently proves export inactivity and sealed capture + // before authorizing these exact one-shot inverses. + for (const state of [...states].reverse()) { + const inverse = state.inverse + state.inverse = undefined + must(inverse, "Verified router inverse unavailable") + await inverse() + } + must(states.every(state => state.clean), "Disabled capture router cleanup requires reconciliation") + await verifyTopology() + await options.drainCollector() + await verifyTopology() + await options.reconcile(projectChrWire(capture.bytes, scope), serverIds) + await append({ phase: "reconciled" }) + return { ...evidence, componentSequenceComplete: true as const } + } finally { + closed = true + // Capture may report child failure before its exercise settles. + if (work) await work.catch(() => undefined) + try { options.reportRouterCleanup(states.every(state => state.clean)) } + finally { await journal.close() } + } +} diff --git a/backend/src/test/traffic-flow-chr-disposable-collector.ts b/backend/src/test/traffic-flow-chr-disposable-collector.ts new file mode 100644 index 00000000..93b207c4 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-disposable-collector.ts @@ -0,0 +1,153 @@ +import { randomUUID } from "node:crypto" +import { networkInterfaces } from "node:os" +import { setTimeout as delay } from "node:timers/promises" +import type { McpPrincipal } from "../modules/mcp/store.js" +import { readChrLinuxNamespaceIdentity, readChrLinuxUdpOwner } from "./traffic-flow-chr-linux-udp-owner.js" +import { CHR_COLLECTOR_RESULT_POLL_MS } from "./traffic-flow-chr-target-timing-profile.js" + +type Namespace = Awaited> +type Options = { + runId: string + deadlineAt: number + namespace: Namespace + hostNamespace: Namespace + port: number + serverIds: readonly [number, number] + principal: McpPrincipal + database: { name: string; oid: string; role: string } + assertCurrent: () => Promise + /** Waits for a separate operator's ordinary approval of this exact new plan. */ + awaitHumanApproval: (planId: string, enabled: boolean, deadlineAt: number) => Promise +} +const address = "198.18.242.3" +const uuid = (value: unknown): value is string => + typeof value === "string" && /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(value) +function must(value: unknown, reason: string): asserts value { if (!value) throw Error(reason) } + +/** Runs inside the disposable backend process. The reviewed parent owns its + * launch, admission, process exit and namespace removal. */ +export async function createChrDisposableCollector(options: Options) { + const { runId, deadlineAt, port, assertCurrent, awaitHumanApproval } = options + const { namespace, hostNamespace, serverIds, database, principal } = structuredClone({ + namespace: options.namespace, hostNamespace: options.hostNamespace, serverIds: options.serverIds, + database: options.database, principal: options.principal, + }) + const instance = "netflow-" + runId + const interfaceName = "nf" + runId + "cv" + must(process.platform === "linux" && process.env.MMAPP_OWN_CHR_NETFLOW_COLLECTOR === "true" && + /^[a-f0-9]{8}$/.test(runId) && Number.isSafeInteger(deadlineAt) && + Date.now() < deadlineAt && deadlineAt - Date.now() <= 600000 && + Number.isSafeInteger(port) && port >= 49152 && port <= 65535 && + [namespace.device, namespace.inode, hostNamespace.device, hostNamespace.inode, database.oid].every(value => /^[1-9][0-9]{0,19}$/.test(value)) && + (namespace.device !== hostNamespace.device || namespace.inode !== hostNamespace.inode) && + database.name === "mcp_netflow_" + runId && database.role === "mcp_test" && + serverIds.length === 2 && new Set(serverIds).size === 2 && serverIds.every(value => Number.isSafeInteger(value) && value > 0), + "Invalid disposable collector boundary") + const fixtureRoot = "/tmp/mcp-netflow-" + runId + const dbUrl = new URL(process.env.DATABASE_URL ?? "about:blank") + must(["postgres:", "postgresql:"].includes(dbUrl.protocol) && dbUrl.hostname === "localhost" && + dbUrl.username === database.role && /^[a-f0-9]{64}$/.test(dbUrl.password) && !dbUrl.port && !dbUrl.hash && + dbUrl.pathname === "/" + database.name && [...dbUrl.searchParams.keys()].join() === "host" && + dbUrl.searchParams.get("host") === fixtureRoot + "/pg-socket" && + process.env.MCP_STATE_DIR === fixtureRoot + "/state", "Disposable database or MCP state endpoint is unpinned") + const local = async () => { + must(Date.now() < deadlineAt, "Disposable collector deadline ended") + await assertCurrent() + const actual = await readChrLinuxNamespaceIdentity() + must(Date.now() < deadlineAt && actual.device === namespace.device && actual.inode === namespace.inode && + (networkInterfaces()[interfaceName] ?? []).filter(row => row.family === "IPv4" && row.address === address).length === 1 && + (!process.env.FLOW_LISTEN_HOST?.trim() || process.env.FLOW_LISTEN_HOST.trim() === address) && + !process.env.FLOW_AGENT_SOCKET?.trim(), "Disposable namespace or listener source changed") + } + await local() + const [{ pool }, { invokeMcpTool, applyPlan, loadPlan }, { validatePrincipal }, ingest, { createFlowSettingsStore }, + { flowSettingsPrivateProof }] = await Promise.all([ + import("../db/index.js"), import("../modules/mcp/runtime.js"), import("../modules/mcp/store.js"), + import("../services/traffic-flow-ingest.js"), import("../services/traffic-flow-settings-store.js"), + import("../services/traffic-flow-host-keys-service.js"), + ]) + const authorize = async () => { + await local() + await validatePrincipal(principal) + await local() + } + await authorize() + const db = (await pool.query("SELECT current_database() AS name,current_user AS role,oid::text AS oid," + + "inet_server_addr() IS NULL AS unix_socket FROM pg_database WHERE datname=current_database()")).rows + const servers = (await pool.query("SELECT id FROM servers ORDER BY id LIMIT 3")).rows.map(row => Number(row.id)) + must(db.length === 1 && db[0].name === database.name && db[0].role === database.role && db[0].oid === database.oid && + db[0].unix_socket === true && servers.length === 2 && serverIds.every(id => servers.includes(id)), "Disposable database identity differs") + const configStore = createFlowSettingsStore(pool, authorize, () => {}) + const config = await configStore.read() + must(config.enabled === false && config.collectorIp === address && config.flowListenPort === port, + "Expected disabled exact collector configuration") + const proof = (value: typeof config) => flowSettingsPrivateProof({ ...value, enabled: false }) + const invariantProof = await proof(config) + const observedUdp = () => readChrLinuxUdpOwner({ namespace, address, port }) + const read = async () => { + await authorize() + const configuration = await configStore.read() + const udpOwners = await observedUdp() + const listener = ingest.getFlowListenerState(), worker = ingest.getFlowWorkerHealth() + const currentProof = await proof(configuration) + must(currentProof === invariantProof && configuration.collectorIp === address && + configuration.flowListenPort === port && typeof configuration.enabled === "boolean", "Disposable collector settings changed") + await authorize() + return { instance, pid: process.pid, invariantProof: currentProof, listenAddress: configuration.collectorIp, + listenPort: configuration.flowListenPort, enabled: configuration.enabled, workerAlive: worker.alive, + boundAddress: listener.bound ? listener.address : null, udpOwners } + } + const initial = await read() + must(initial.udpOwners.length === 0 && !initial.workerAlive && initial.boundAddress === null, "Disposable collector not idle") + let enableAttempted = false, disableAttempted = false, enabledVerified = false, disableVerified = false + const applyEnabled = async (enabled: boolean, beforeDispatch: () => Promise) => { + must(enabled ? !enableAttempted : enabledVerified && !disableAttempted, "Collector mutation already attempted or unknown") + if (enabled) enableAttempted = true + else disableAttempted = true + await authorize() + const current = await read() + must(current.enabled !== enabled, "Unexpected collector enable state") + const prepared = await invokeMcpTool(principal, "mm_netflow_settings_update_plan", { settings: { enabled } }) as { + state?: string; planId?: string + } + must(prepared.state === "awaiting-approval" && uuid(prepared.planId), "Collector plan did not await human approval") + await awaitHumanApproval(prepared.planId, enabled, deadlineAt) + await authorize() + const approved = await loadPlan(prepared.planId) + must(approved.connection_id === principal.connectionId && approved.operation_id === "netflow.settings.update" && + approved.state === "approved" && approved.approved_by && approved.expires_at.getTime() > Date.now(), "Collector approval unconfirmed") + await beforeDispatch() + await authorize() + const operation = await applyPlan(principal, prepared.planId, randomUUID()) + must(uuid(operation.id), "Collector operation acknowledgement missing") + const pollUntil = Math.min(deadlineAt, Date.now() + CHR_COLLECTOR_RESULT_POLL_MS) + while (Date.now() < pollUntil) { + await authorize() + const rows = (await pool.query("SELECT o.state,o.verification,s.state AS step_state,s.result " + + "FROM mcp_operations o LEFT JOIN mcp_operation_steps s ON s.operation_id=o.id " + + "WHERE o.id=$1 AND o.connection_id=$2 AND o.plan_id=$3 LIMIT 2", + [operation.id, principal.connectionId, prepared.planId])).rows + const [row] = rows + if (rows.length === 1 && ["pending", "running"].includes(row.state)) { await delay(100); continue } + must(rows.length === 1 && row.state === "applied" && row.verification === "passed" && row.step_state === "verified" && + row.result?.configurationConfirmed === true && row.result.receiptConfirmed === true && + row.result.listener === "request-acknowledged" && row.result.automaticReplay === false && + uuid(row.result.receiptId), "Collector operation result requires reconciliation") + const receipt = (await pool.query("SELECT phase,state FROM flow_settings_intents " + + "WHERE receipt_id=$1 ORDER BY phase", [row.result.receiptId])).rows + must(receipt.length === 2 && receipt[0].phase === "configuration" && receipt[0].state === "completed" && + receipt[1].phase === "listener" && receipt[1].state === "completed", "Collector durable listener acknowledgement missing") + await authorize() + if (enabled) enabledVerified = true + else disableVerified = true + return { operationId: operation.id, acknowledged: true } + } + throw Error("Collector operation outcome unknown. Automatic replay forbidden.") + } + const assertDrained = async () => { + const observed = await read() + must(disableVerified && !observed.enabled && !observed.workerAlive && + observed.boundAddress === null && observed.udpOwners.length === 0, "Collector retirement unconfirmed") + } + return { interfaceName, pins: { instance, pid: process.pid, invariantProof }, collector: { authorize, read, applyEnabled, assertDrained } } +} diff --git a/backend/src/test/traffic-flow-chr-egress-replay.ts b/backend/src/test/traffic-flow-chr-egress-replay.ts new file mode 100644 index 00000000..ffd68c26 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-egress-replay.ts @@ -0,0 +1,23 @@ +import { isDeepStrictEqual } from "node:util" +import { clearFlowTemplateCache, parseFlowPacket } from "../services/traffic-flow-parse.js" +import { decodeChrEgressIpfix } from "./traffic-flow-chr-independent-ipfix.js" +import type { projectChrEgress } from "./traffic-flow-chr-wire.js" + +/** Offline only. Run in the wire projector CLI, which owns its parser cache. */ +export function replayChrEgress(wire: ReturnType) { + const decoded = decodeChrEgressIpfix(wire) + clearFlowTemplateCache() + try { + for (const [index, frame] of wire.frames.entries()) { + const actual = parseFlowPacket(Buffer.from(frame.payloadHex, "hex"), wire.scope.source, { + sourcePort: frame.sourcePort, collectorSocketId: "offline-egress", + }).map(({ src, dst, proto, srcPort, dstPort, inIface, outIface, bytes, packets }) => { + if (!Number.isSafeInteger(bytes) || !Number.isSafeInteger(packets)) + throw Error("Product replay requires exact JavaScript integer counters") + return { src, dst, proto, srcPort, dstPort, inIface, outIface, bytes: String(bytes), packets: String(packets) } + }) + if (!isDeepStrictEqual(actual, decoded.frames[index].flows)) throw Error("Egress flow records disagree with independent decoder") + } + return { ...decoded, captureReplayMatched: true, liveCollectorAccepted: false } + } finally { clearFlowTemplateCache() } +} diff --git a/backend/src/test/traffic-flow-chr-firewall-preflight.test.ts b/backend/src/test/traffic-flow-chr-firewall-preflight.test.ts new file mode 100644 index 00000000..9785d1bf --- /dev/null +++ b/backend/src/test/traffic-flow-chr-firewall-preflight.test.ts @@ -0,0 +1,88 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { verifyChrFirewallPreflight } from "./traffic-flow-chr-firewall-preflight.js" + +type Row = Record +const rule = { ".id": "*A", chain: "forward", action: "accept", disabled: "false", protocol: "udp", + "src-address": "198.18.240.0/24", "dst-address": "198.18.240.2/32", + "in-interface": "edge-a", "out-interface": "middle-a", "dst-port": "50003,50101-50103", + "connection-state": "new,established", log: "false", "log-prefix": "" } + +test("owned WG policy admits complete empty snapshots only and requires matching path profile", async () => { + const { config, rules } = fixture() + rules.filter.length = 0 + const profile = "owned-wg-empty-firewall" as const + const owned = { ...config, profile, readPathEvidence: async () => ({ + ...await config.readPathEvidence(), profile, ownedInterfaces: ["edge-a", "middle-a"], + ownedRoutes: [{ destination: "198.18.240.1", interfaceName: "edge-a" }, { destination: "198.18.240.2", interfaceName: "middle-a" }], + udpPairs: ["198.18.240.1:50100/198.18.240.2:50101"], + }) } + assert.equal((await verifyChrFirewallPreflight(owned)).policyVerified, true) + await assert.rejects(verifyChrFirewallPreflight({ ...owned, packets: [{ ...config.packets[0], destinationPort: 50102 }] }), /only owned WireGuard/) + await assert.rejects(verifyChrFirewallPreflight({ ...owned, readPathEvidence: config.readPathEvidence }), /reachability/) + rules.raw.push({ ".id": "*A", chain: "output", action: "accept", disabled: "true" }) + await assert.rejects(verifyChrFirewallPreflight(owned), /requires empty firewall/) +}) +function fixture() { + const calls: string[] = [] + const rules = { raw: [] as Row[], mangle: [] as Row[], nat: [] as Row[], filter: [rule] as Row[] } + const config = { + identity: "router-a", version: "7.24.5 (stable)", + packets: [{ direction: "forward" as const, source: "198.18.240.1", destination: "198.18.240.2", + sourcePort: 50100, destinationPort: 50101, ingress: "edge-a", egress: "middle-a" }], + assertCurrent: async () => {}, + readPathEvidence: async () => ({ routesVerified: true, freshConntrackTuples: true, fingerprint: "a".repeat(64) }), + client: { async get(path: string, timeout?: number, _signal?: AbortSignal, bytes?: number, fence?: () => Promise) { + assert.ok(timeout && timeout <= 2000) + assert.equal(bytes, 128 * 1024) + await fence?.() + calls.push(path) + if (path === "/system/identity") return [{ name: "router-a" }] as T + if (path === "/system/resource") return [{ version: "7.24.5 (stable)" }] as T + const table = path.replace("/ip/firewall/", "") as keyof typeof rules + return rules[table].map(row => ({ ...row, packets: String(calls.length), bytes: "100" })) as T + } }, + } + return { config, rules, calls } +} + +test("reads only fixed bounded native inventories twice and accepts exact simple UDP policy", async () => { + const { config, calls, rules } = fixture() + rules.filter.unshift({ ".id": "*B", chain: "forward", action: "drop", protocol: "tcp", "in-interface-list": "any" }) + const paths = ["/system/identity", "/system/resource", "/ip/firewall/raw", "/ip/firewall/mangle", + "/ip/firewall/nat", "/ip/firewall/filter"] + assert.deepEqual(await verifyChrFirewallPreflight(config), { policyVerified: true, packetScopes: 1, actualIngressVerified: false }) + assert.deepEqual(calls, [...paths, ...paths]) +}) + +test("rejects matching FastTrack, NAT, jumps, unsupported matchers and incomplete filter admission", async () => { + for (const [table, inserted] of [ + ["filter", { chain: "forward", action: "fasttrack-connection", "connection-state": "established" }], + ["nat", { chain: "srcnat", action: "masquerade" }], + ["raw", { chain: "prerouting", action: "jump", "jump-target": "policy" }], + ["filter", { chain: "forward", action: "accept", "src-address-list": "unknown" }], + ] as const) { + const { config, rules } = fixture() + rules[table].unshift({ ".id": "*B", ...inserted }) + await assert.rejects(verifyChrFirewallPreflight(config), /matching|unsupported/) + } + const { config, rules } = fixture() + rules.filter.length = 0 + await assert.rejects(verifyChrFirewallPreflight(config), /no explicit filter allow/) +}) + +test("rejects inventory drift, unavailable route or conntrack proof, and lost authorization", async () => { + const stale = fixture() + stale.config.readPathEvidence = async () => { + if (stale.calls.length === 6) stale.rules.filter[0] = { ...rule, comment: "changed" } + return { routesVerified: true, freshConntrackTuples: true, fingerprint: "a".repeat(64) } + } + await assert.rejects(verifyChrFirewallPreflight(stale.config), /stale inventory/) + const unknown = fixture() + unknown.config.readPathEvidence = async () => ({ routesVerified: true, freshConntrackTuples: false, fingerprint: "a".repeat(64) }) + await assert.rejects(verifyChrFirewallPreflight(unknown.config), /unknown active reachability/) + const expired = fixture() + expired.config.assertCurrent = async () => { throw Error("ownership expired") } + await assert.rejects(verifyChrFirewallPreflight(expired.config), /ownership expired/) + assert.equal(expired.calls.length, 0) +}) diff --git a/backend/src/test/traffic-flow-chr-firewall-preflight.ts b/backend/src/test/traffic-flow-chr-firewall-preflight.ts new file mode 100644 index 00000000..af01245d --- /dev/null +++ b/backend/src/test/traffic-flow-chr-firewall-preflight.ts @@ -0,0 +1,179 @@ +import { isIPv4 } from "node:net" +import { performance } from "node:perf_hooks" +import { isDeepStrictEqual } from "node:util" +import type { MikrotikClient } from "../services/mikrotik.js" + +type Row = Record +const tables = ["raw", "mangle", "nat", "filter"] as const +type Table = (typeof tables)[number] +type Packet = { + direction: "input" | "forward" | "output" + source: string + destination: string + sourcePort: number + destinationPort: number + ingress?: string + egress?: string +} +type Options = { + client: Pick + identity: string + version: string + packets: readonly Packet[] + assertCurrent: () => Promise + /** Trusted reader of exact active routes and address/interface bindings. */ + profile?: "owned-wg-empty-firewall" + readPathEvidence: () => Promise<{ routesVerified: boolean; freshConntrackTuples: boolean; fingerprint: string; + profile?: "owned-wg-empty-firewall"; ownedInterfaces?: readonly string[]; + ownedRoutes?: readonly { destination: string; interfaceName: string }[]; udpPairs?: readonly string[] }> +} +const paths: Record> = { + raw: { input: ["prerouting"], forward: ["prerouting"], output: ["output"] }, + mangle: { input: ["prerouting", "input"], forward: ["prerouting", "forward", "postrouting"], output: ["output", "postrouting"] }, + nat: { input: ["dstnat", "input"], forward: ["dstnat", "srcnat"], output: ["output", "srcnat"] }, + filter: { input: ["input"], forward: ["forward"], output: ["output"] }, +} +const meta = new Set([".id", "action", "chain", "comment", "disabled", "dynamic", "invalid"]) +const matchKeys = new Set(["protocol", "src-address", "dst-address", "src-port", "dst-port", "in-interface", "out-interface", "connection-state"]) +const states = ["new", "established", "related", "invalid", "untracked"] as const +const deny = (reason: string): never => { throw Error("RouterOS fixture preflight refused: " + reason) } +const validPort = (value: number) => Number.isInteger(value) && value > 0 && value <= 65535 +const flag = (value: unknown) => { + if (value === undefined || value === false || value === "false" || value === "no") return false + if (value === true || value === "true" || value === "yes") return true + return deny("unsupported rule flag") +} +const ipv4Number = (address: string) => address.split(".").reduce((sum, octet) => sum * 256 + Number(octet), 0) +function addressMatches(value: string, ip: string) { + const [base, bits = "32", ...extra] = value.split("/") + if (!isIPv4(base) || !/^(0|[1-9]|[12][0-9]|3[0-2])$/.test(bits) || extra.length) return undefined + const size = 2 ** (32 - Number(bits)) + return Math.floor(ipv4Number(base) / size) === Math.floor(ipv4Number(ip) / size) +} +function portMatches(value: string, port: number) { + const ranges = value.split(",").map(range => /^([1-9][0-9]{0,4})(?:-([1-9][0-9]{0,4}))?$/.exec(range)) + if (ranges.some(range => !range || !validPort(Number(range[1])) || !validPort(Number(range[2] ?? range[1])) || + Number(range[1]) > Number(range[2] ?? range[1]))) return undefined + return ranges.some(range => range && Number(range[1]) <= port && port <= Number(range[2] ?? range[1])) +} +function matcher(key: string, value: unknown, packet: Packet, state: "new" | "established", chain: string) { + if (typeof value !== "string" || !value) return undefined + switch (key) { + case "protocol": + return /^(udp|tcp|icmp|gre|ipsec-esp|17|6|1|47|50)$/.test(value) ? value === "udp" || value === "17" : undefined + case "src-address": return addressMatches(value, packet.source) + case "dst-address": return addressMatches(value, packet.destination) + case "src-port": return portMatches(value, packet.sourcePort) + case "dst-port": return portMatches(value, packet.destinationPort) + case "in-interface": + return !["postrouting", "srcnat"].includes(chain) && packet.ingress && /^[\w.-]{1,64}$/.test(value) + ? value === packet.ingress : undefined + case "out-interface": + return !["prerouting", "dstnat"].includes(chain) && packet.egress && /^[\w.-]{1,64}$/.test(value) + ? value === packet.egress : undefined + case "connection-state": { + const values = value.split(",") + return values.every(item => (states as readonly string[]).includes(item)) ? values.includes(state) : undefined + } + default: return undefined + } +} +function matches(rule: Row, packet: Packet, state: "new" | "established", chain: string, table: Table) { + const results = Object.entries(rule).filter(([key]) => !meta.has(key)) + .filter(([key, value]) => !(key === "log" && (value === false || value === "false" || value === "no") || + key === "log-prefix" && value === "")) + .map(([key, value]) => matchKeys.has(key) && !(table === "raw" && key === "connection-state") + ? matcher(key, value, packet, state, chain) : undefined) + if (results.includes(false)) return false + if (results.includes(undefined)) deny("unsupported potentially matching rule") + if (flag(rule.dynamic) || flag(rule.invalid)) deny("dynamic or invalid matching rule") + return true +} +function verifyRules(inventory: Record, packet: Packet) { + for (const state of ["new", "established"] as const) { + for (const table of tables) { + for (const chain of paths[table][packet.direction]) { + let accepted = false + for (const rule of inventory[table]) { + if (rule.chain !== chain || flag(rule.disabled) || !matches(rule, packet, state, chain, table)) continue + if (rule.action !== "accept") deny("matching non-accept action") + accepted = true + break + } + // Require an explicit filter allow even though RouterOS may implicitly accept. + if (table === "filter" && !accepted) deny("no explicit filter allow") + } + } + } +} +function rows(value: unknown, limit: number): Row[] { + const result = Array.isArray(value) ? value : [value] + if (result.length > limit || result.some(row => !row || typeof row !== "object" || Array.isArray(row))) return deny("invalid inventory") + return result +} + +/** Bounded double read via the existing RouterOS client. This proves a narrow + * policy subset at observation time. It does not prove packet delivery. */ +export async function verifyChrFirewallPreflight(options: Options) { + const { client, assertCurrent, readPathEvidence, profile } = options + const { identity, version, packets } = structuredClone({ identity: options.identity, version: options.version, packets: options.packets }) + if (!identity || !version || !packets.length || packets.length > 32 || + packets.some(packet => !Object.hasOwn(paths.filter, packet.direction) || + !isIPv4(packet.source) || !isIPv4(packet.destination) || !validPort(packet.sourcePort) || !validPort(packet.destinationPort) || + (packet.direction !== "output" && !packet.ingress) || (packet.direction !== "input" && !packet.egress) || + [packet.ingress, packet.egress].some(name => name !== undefined && !/^[\w.-]{1,64}$/.test(name)))) deny("invalid packet scope") + const deadline = performance.now() + 15000 + async function fence() { + if (performance.now() >= deadline) deny("observation expired") + await assertCurrent() + if (performance.now() >= deadline) deny("observation expired") + } + async function get(path: string, limit: number) { + await fence() + const result = rows(await client.get(path, Math.min(2000, Math.ceil(deadline - performance.now())), + undefined, 128 * 1024, fence), limit) + await fence() + return result + } + async function snapshot() { + const [device] = await get("/system/identity", 1) + const [resource] = await get("/system/resource", 1) + if (device?.name !== identity || resource?.version !== version) deny("device identity changed") + const inventory = {} as Record + for (const table of tables) { + inventory[table] = (await get("/ip/firewall/" + table, 256)).map(({ bytes: _bytes, packets: _packets, ...rule }) => { + if (typeof rule.chain !== "string" || !rule.chain || typeof rule.action !== "string" || + typeof rule[".id"] !== "string" || !/^\*[a-f0-9]{1,16}$/i.test(rule[".id"])) deny("invalid rule identity") + return structuredClone(rule) + }) + if (new Set(inventory[table].map(rule => rule[".id"])).size !== inventory[table].length) deny("duplicate rule ID") + } + await fence() + const evidence = await readPathEvidence() + await fence() + if (evidence.routesVerified !== true || evidence.freshConntrackTuples !== true || evidence.profile !== profile || + !/^[a-f0-9]{64}$/.test(evidence.fingerprint)) deny("unknown active reachability or existing connection") + if (profile && (evidence.ownedInterfaces?.length !== 2 || + !packets.some(packet => packet.direction === "forward") || + packets.some(packet => packet.direction === "forward" && + (packet.ingress === packet.egress || !evidence.ownedInterfaces?.includes(packet.ingress!) || + !evidence.ownedInterfaces?.includes(packet.egress!) || + !evidence.ownedRoutes?.some(route => route.destination === packet.destination && route.interfaceName === packet.egress) || + !evidence.ownedRoutes?.some(route => route.destination === packet.source && route.interfaceName === packet.ingress) || + !evidence.udpPairs?.includes([packet.source + ":" + packet.sourcePort, packet.destination + ":" + packet.destinationPort].sort().join("/")))))) { + deny("forwarding does not use only owned WireGuard") + } + return { inventory, fingerprint: evidence.fingerprint, ownedInterfaces: evidence.ownedInterfaces, + ownedRoutes: evidence.ownedRoutes, udpPairs: evidence.udpPairs } + } + const before = await snapshot() + const after = await snapshot() + if (!isDeepStrictEqual(before, after)) deny("stale inventory or path") + if (profile === "owned-wg-empty-firewall") { + if (tables.some(table => after.inventory[table].length !== 0)) deny("owned WireGuard profile requires empty firewall") + } else { + for (const packet of packets) verifyRules(after.inventory, packet) + } + await fence() + return { policyVerified: true as const, packetScopes: packets.length, actualIngressVerified: false as const } +} diff --git a/backend/src/test/traffic-flow-chr-full-entry.test.ts b/backend/src/test/traffic-flow-chr-full-entry.test.ts new file mode 100644 index 00000000..4b08b05b --- /dev/null +++ b/backend/src/test/traffic-flow-chr-full-entry.test.ts @@ -0,0 +1,74 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { parseChrFullPins } from "./traffic-flow-chr-full-entry.js" +import { bindChrStagedHostCaptureTimer, decodeChrHostReply } from "./traffic-flow-chr-host-ipc.js" +import { chrTransitNames } from "./traffic-flow-chr-transit-fixture.js" + +const runId = "a012bcde" +const ports = Object.fromEntries(["linuxA", "edgeA", "middleA", "middleB", "edgeB", "linuxB", "collector", + "calibrationSource", "calibrationDestination", "phase1Source", "phase1Destination", "phase2Source", "phase2Destination"] + .map((role, index) => [role, 50100 + index])) +const directions = ["A-to-B", "B-to-A", "export-A", "export-B", "underlay-linuxA-to-A", "underlay-A-to-linuxA", + "underlay-A-to-B", "underlay-B-to-A", "underlay-B-to-linuxB", "underlay-linuxB-to-B"] +const file = (inode: string) => ({ device: "3", inode }) +const pins = { + runId, nonce: runId + "a".repeat(24), deadlineAt: 10001, namespace: file("8"), hostNamespace: file("9"), + relaySockets: { A: file("11"), B: file("12") }, controlSocket: file("13"), admissionSocket: file("14"), ports, + isolation: { checkedPorts: Object.values(ports), checkedNames: Object.values(chrTransitNames(runId)), + checkedCidrs: ["198.18.240.1/32", "198.18.240.2/32", "198.18.241.0/30", "198.18.241.4/30", + "198.18.241.8/30", "198.18.242.0/29"], occupiedPorts: [], overlappingCidrs: [], occupiedNames: [], + policies: directions.map(direction => ({ direction, verdict: "allow", nat: "none", fastTrack: "none", ambiguousRules: 0 })) }, + routers: { A: { deviceId: "reviewed-A", identity: "CHR-A", version: "7.24.5" }, + B: { deviceId: "reviewed-B", identity: "CHR-B", version: "7.24.5" } }, + linux: { endpoint: "161.104.46.222", publicKeys: { linuxA: "A".repeat(43) + "=", linuxB: "B".repeat(43) + "=" } }, +} + +test("full entry admits only exact reviewed isolated scope and no credential or path overrides", () => { + const { plan } = parseChrFullPins(pins, 1) + assert.equal(plan.ports.collector, 50106) + assert.equal(plan.address.collector, "198.18.242.3") + assert.throws(() => parseChrFullPins({ ...pins, password: "must-not-be-in-manifest" }, 1)) + assert.throws(() => parseChrFullPins({ ...pins, namespace: pins.hostNamespace }, 1), /fixture pins differ/) + assert.throws(() => parseChrFullPins(pins, pins.deadlineAt), /fixture pins differ/) +}) + +test("host ACK frames bind sequence, operation, complete capture pin and no caller path", () => { + const bytes = (value: object) => Buffer.from(JSON.stringify(value) + "\n") + const stopped = { sequence: 6, op: "capture-stop", ok: true, sha256: "a".repeat(64), complete: true, dropped: 0 } + assert.equal(decodeChrHostReply(bytes(stopped), 6, "capture-stop").sha256, stopped.sha256) + assert.throws(() => decodeChrHostReply(bytes(stopped), 5, "capture-stop"), /acknowledgement/) + assert.throws(() => decodeChrHostReply(bytes({ ...stopped, path: "capture.pcap" }), 6, "capture-stop"), /reply shape/) + assert.throws(() => decodeChrHostReply(bytes({ ...stopped, dropped: 1 }), 6, "capture-stop"), /capture unconfirmed/) + assert.throws(() => decodeChrHostReply(Buffer.concat([bytes(stopped), bytes(stopped)]), 6, "capture-stop"), /frame/) +}) + +test("only a separately sealed staged profile selects the staged ACK shape and capture timer", () => { + const captureProfile = { kind: "staged-disabled-target", captureMs: 205000, requiredCollectorWindowMs: 450000 } + const startupBarrier = { device: "5", inode: "123" } + assert.deepEqual(parseChrFullPins({ ...pins, captureProfile, startupBarrier }, 1).pins.captureProfile, captureProfile) + assert.equal(parseChrFullPins(pins, 1).pins.captureProfile, undefined) + assert.throws(() => parseChrFullPins({ ...pins, captureProfile }, 1), /fixture pins differ/) + assert.throws(() => parseChrFullPins({ ...pins, startupBarrier }, 1), /fixture pins differ/) + for (const candidate of [{ ...captureProfile, captureMs: 240001 }, { ...captureProfile, timingAccepted: true }, + { ...captureProfile, kind: "direct-target" }, { kind: captureProfile.kind, captureMs: captureProfile.captureMs }, + { ...captureProfile, requiredCollectorWindowMs: 415000 }, { ...captureProfile, requiredCollectorWindowMs: 600000 }]) { + assert.throws(() => parseChrFullPins({ ...pins, captureProfile: candidate, startupBarrier }, 1)) + } + assert.throws(() => parseChrFullPins({ ...pins, deadlineAt: 600002 }, 1), /fixture pins differ/) + assert.equal(parseChrFullPins({ ...pins, captureProfile, startupBarrier, deadlineAt: 3600001 }, 1).pins.deadlineAt, 3600001) + assert.throws(() => parseChrFullPins({ ...pins, captureProfile, startupBarrier, deadlineAt: 3600002 }, 1), /fixture pins differ/) + + const frame = (extra: object = {}) => Buffer.from(JSON.stringify({ sequence: 3, op: "capture-start", ok: true, ...extra }) + "\n") + assert.equal(decodeChrHostReply(frame(), 3, "capture-start").captureDeadlineAt, undefined) + assert.throws(() => decodeChrHostReply(frame({ captureDeadlineAt: 206500 }), 3, "capture-start"), /reply shape/) + assert.throws(() => decodeChrHostReply(frame(), 3, "capture-start", true), /reply shape/) + assert.throws(() => decodeChrHostReply(frame({ captureDeadlineAt: "206500" }), 3, "capture-start", true), /deadline unconfirmed/) + assert.throws(() => decodeChrHostReply(frame({ captureDeadlineAt: 206500, path: "x" }), 3, "capture-start", true), /reply shape/) + const receipt = decodeChrHostReply(frame({ captureDeadlineAt: 206500 }), 3, "capture-start", true) + assert.deepEqual(bindChrStagedHostCaptureTimer(receipt.captureDeadlineAt, 205000, 220000, 1000, 2000), + { kind: "staged-ipfix-timeout-20s", captureDeadlineAt: 206500 }) + for (const candidate of [undefined, 1000, 205999, 207001]) { + assert.throws(() => bindChrStagedHostCaptureTimer(candidate, 205000, 220000, 1000, 2000), /reviewed window/) + } + assert.throws(() => bindChrStagedHostCaptureTimer(206500, 205000, 206499, 1000, 2000), /reviewed window/) +}) diff --git a/backend/src/test/traffic-flow-chr-full-entry.ts b/backend/src/test/traffic-flow-chr-full-entry.ts new file mode 100644 index 00000000..668eda88 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-full-entry.ts @@ -0,0 +1,398 @@ +import { createHash } from "node:crypto" +import { constants, type BigIntStats } from "node:fs" +import { lstat, mkdir, open } from "node:fs/promises" +import { createConnection } from "node:net" +import { resolve } from "node:path" +import { createInterface } from "node:readline/promises" +import { setTimeout as delay } from "node:timers/promises" +import { fileURLToPath } from "node:url" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { createChrCollectorBootstrapDiagnostics, runOwnedChrCollectorProcess } from "./traffic-flow-chr-collector-bootstrap.js" +import type { DisabledChrCaptureOptions } from "./traffic-flow-chr-disabled-runner.js" +import { createChrHostControl } from "./traffic-flow-chr-host-ipc.js" +import { assessChrCaptureTiming } from "./traffic-flow-chr-capture-timing.js" +import { readChrLinuxNamespaceIdentity } from "./traffic-flow-chr-linux-udp-owner.js" +import { isChrWireGuardInterfaceType, planChrTransitFixture } from "./traffic-flow-chr-transit-fixture.js" +import type { ChrWriteAdmission } from "./traffic-flow-chr-write-admission.js" +import { createChrChildAdmissionPipe } from "./traffic-flow-chr-write-admission-ipc.js" +import { chrFixtureDeadlineMaxMs } from "./traffic-flow-chr-target-timing-profile.js" +import { chrStagedCaptureProfileSchema } from "./traffic-flow-chr-staged-profile.js" + +const sides = ["A", "B"] as const +type Side = (typeof sides)[number] +type Row = Record +type Fixture = Parameters +const ns = z.object({ device: z.string().regex(/^[1-9][0-9]{0,19}$/), inode: z.string().regex(/^[1-9][0-9]{0,19}$/) }).strict() +const text = z.string().min(1).max(128) +const router = z.object({ deviceId: text, identity: text, version: text }).strict() +const key = z.string().regex(/^[A-Za-z0-9+/]{43}=$/) +const boundedStrings = z.array(text).max(64) +const boundedPorts = z.array(z.number().int().min(49152).max(65535)).max(13) +const fixtureIsolation = z.object({ checkedPorts: boundedPorts, checkedCidrs: boundedStrings, checkedNames: boundedStrings, + occupiedPorts: boundedPorts, overlappingCidrs: boundedStrings, occupiedNames: boundedStrings, + policies: z.array(z.object({ direction: text, verdict: z.literal("allow"), nat: z.literal("none"), + fastTrack: z.literal("none"), ambiguousRules: z.literal(0) }).strict()).length(10) }).strict() +const fullPins = z.object({ + runId: z.string().regex(/^[a-f0-9]{8}$/), nonce: z.string().regex(/^[a-f0-9]{32}$/), + deadlineAt: z.number().int().positive().safe(), namespace: ns, hostNamespace: ns, + captureProfile: chrStagedCaptureProfileSchema.optional(), + startupBarrier: ns.optional(), + relaySockets: z.object({ A: ns, B: ns }).strict(), controlSocket: ns, admissionSocket: ns, + ports: z.record(z.string(), z.number().int().min(49152).max(65535)), isolation: fixtureIsolation, + routers: z.object({ A: router, B: router }).strict(), + linux: z.object({ endpoint: z.literal("161.104.46.222"), publicKeys: z.object({ linuxA: key, linuxB: key }).strict() }).strict(), +}).strict() +type Pins = z.infer +type CollectorProcessOptions = Parameters[0] +type CollectorContext = Parameters>[0] +type StagedCollectorContext = Parameters>[0] +const endpoints = { A: "194.226.121.150", B: "104.165.244.7" } as const +const routerRoles = { A: ["edgeA", "middleA"], B: ["edgeB", "middleB"] } as const +const roleNames = { edgeA: "ea", middleA: "ma", edgeB: "eb", middleB: "mb" } as const +const nativeId = (value: unknown): value is string => typeof value === "string" && /^\*[a-f0-9]{1,16}$/i.test(value) +const maxRead = 512 * 1024 +function must(value: unknown, reason: string): asserts value { if (!value) throw Error(reason) } + +export function parseChrFullPins(value: unknown, now = Date.now()) { + const pins = fullPins.parse(value) + must(pins.nonce.startsWith(pins.runId) && pins.deadlineAt > now && + pins.deadlineAt - now <= chrFixtureDeadlineMaxMs(pins.captureProfile?.kind) && + pins.routers.A.deviceId !== pins.routers.B.deviceId && !isDeepStrictEqual(pins.namespace, pins.hostNamespace) && + Boolean(pins.captureProfile) === Boolean(pins.startupBarrier), + "Full CHR fixture pins differ") + const scope: Fixture = [pins.runId, pins.ports as Fixture[1], pins.isolation as Fixture[2]] + return { pins, scope, plan: planChrTransitFixture(...scope) } +} + + +async function awaitCollectorStartup(runId: string, pin: NonNullable, deadlineAt: number, + authorize: () => Promise) { + const path = `/tmp/mcp-netflow-${runId}/relay/startup.ready` + const end = Math.min(deadlineAt, Date.now() + 12_000) + const ready = Buffer.from(`ready:${runId}\n`, "ascii") + const pinned = (info: BigIntStats) => + info.isFile() && info.uid === 0n && info.gid === BigInt(process.getegid?.() ?? -1) && info.nlink === 1n && + (info.mode & 0o777n) === 0o440n && String(info.dev) === pin.device && String(info.ino) === pin.inode + while (Date.now() < end) { + await authorize() + const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK) + try { + must(pinned(await file.stat({ bigint: true })), "Collector startup barrier identity changed") + const raw = Buffer.alloc(ready.length + 1) + const { bytesRead } = await file.read(raw, 0, raw.length, 0) + must(pinned(await lstat(path, { bigint: true })) && Date.now() < end, "Collector startup barrier expired") + if (bytesRead) { + must(bytesRead === ready.length && raw.subarray(0, bytesRead).equals(ready), "Collector startup barrier refused") + await authorize() + must(Date.now() < end, "Collector startup barrier expired") + return + } + } finally { await file.close() } + await delay(50) + } + throw Error("Collector startup barrier expired") +} + +function nativeRows(value: unknown, limit: number, singleton = false): Row[] { + const rows = Array.isArray(value) ? value : singleton ? [value] : null + must(rows && rows.length <= limit && (!singleton || rows.length === 1) && + rows.every(row => row && typeof row === "object" && !Array.isArray(row)), "Exact native catalogue unavailable") + return rows +} + +export async function runFullChrNetflow(options: { + reviewedPins: unknown; password: string; writeAdmission: ChrWriteAdmission + assertCurrent: () => Promise + reviewApproval: Parameters[0]["reviewApproval"] + enterCollectorStage?: Parameters[0]["enterStage"] +}) { + const { pins, scope, plan } = parseChrFullPins(options.reviewedPins) + const { runId, deadlineAt, namespace, hostNamespace, captureProfile } = pins + const authorizeRun = async () => { + must(Date.now() < deadlineAt, "Full CHR fixture deadline ended") + await options.assertCurrent() + must(Date.now() < deadlineAt, "Full CHR fixture deadline ended") + } + const host = await createChrHostControl({ runId, nonce: pins.nonce, deadlineAt, socket: pins.controlSocket, assertCurrent: authorizeRun }) + const work = async (context: CollectorContext | StagedCollectorContext) => { + const staged = "activateCollector" in context + must(staged === Boolean(captureProfile), "Collector work profile differs from sealed capture profile") + if (pins.startupBarrier) await awaitCollectorStartup(runId, pins.startupBarrier, deadlineAt, authorizeRun) + // Native readers reach backend config and the process-global pool. + // Load them only after the bootstrap pins the disposable database. + const [{ encodeRosId }, { chrDisabledFlowPatchCount }, { verifyChrFirewallPreflight }, { createChrInterfaceCounterReader }, + { openChrNamespaceClients }, { createChrRouteConntrackReader }, + { CHR_TARGET_TEMPLATE_TIMEOUT_MS, CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS }, + { createChrTransitTopologyInventory, CHR_TOPOLOGY_CATALOG_PAIR_GETS, CHR_TOPOLOGY_CATALOG_READ_LIMIT }, + { createChrTransitTopologyOwner }, { executeDisabledChrVpsOperator }] = + await Promise.all([import("../services/mikrotik.js"), import("./traffic-flow-chr-disabled-baseline.js"), + import("./traffic-flow-chr-firewall-preflight.js"), + import("./traffic-flow-chr-interface-counters.js"), import("./traffic-flow-chr-namespace-relay.js"), + import("./traffic-flow-chr-route-conntrack.js"), import("./traffic-flow-chr-target-harness.js"), + import("./traffic-flow-chr-topology-inventory.js"), import("./traffic-flow-chr-topology-owner.js"), + import("./traffic-flow-chr-vps-operator.js")]) + const bridge = await openChrNamespaceClients({ runId, deadlineAt, leaseProfile: captureProfile?.kind, + pins: pins.relaySockets, password: options.password, + namespace, hostNamespace, assertCurrent: authorizeRun }) + try { + const authorizeFixture = staged ? context.authorizeFixture : context.collector.authorize + const auth = async () => { await authorizeRun(); await authorizeFixture() } + const clients = bridge.clients + async function get(side: Side, path: string, limit: number, singleton = false) { + await auth() + const result = await clients[side].get(path, 1800, undefined, maxRead, auth) + await auth() + return nativeRows(result, limit, singleton) + } + async function identity(side: Side) { + const [[device], [version]] = await Promise.all([get(side, "/system/identity", 1, true), get(side, "/system/resource", 1, true)]) + must(device.name === pins.routers[side].identity && version.version === pins.routers[side].version, "CHR identity changed") + } + async function flow(side: Side) { + await identity(side) + const [[flow], [ipfix], targets] = await Promise.all([ + get(side, "/ip/traffic-flow", 1, true), get(side, "/ip/traffic-flow/ipfix", 1, true), + get(side, "/ip/traffic-flow/target", 64), + ]) + return { deviceId: pins.routers[side].deviceId, flow, ipfix, targets } + } + const [a, b] = await Promise.all(sides.map(flow)) + const baseline = { A: a, B: b } + must(sides.every(side => [false, "false", "no"].includes(baseline[side].flow.enabled as string | boolean) && + baseline[side].targets.length === 0), "Disabled CHR baseline differs") + const assertFlowRestored = async () => { + for (const side of sides) must(isDeepStrictEqual(await flow(side), baseline[side]), "CHR flow baseline not restored") + } + const inventory = createChrTransitTopologyInventory({ runId, deadlineAt, authorize: auth, parallelism: 2, routers: { + A: { deviceId: pins.routers.A.deviceId, client: clients.A }, B: { deviceId: pins.routers.B.deviceId, client: clients.B }, + } }) + const exact = (side: Side) => ({ deviceId: pins.routers[side].deviceId, endpoint: endpoints[side], + read: () => inventory.read(side), native: clients[side] }) + const root = "/tmp/mcp-netflow-" + runId + const topologyPath = root + "/chr-topology" + await mkdir(topologyPath, { mode: 0o700 }) + const journal = await open(topologyPath + "/owned.jsonl", "wx", 0o600) + const owned = new Map() + try { + const dir = await open(topologyPath, "r") + try { await dir.sync() } finally { await dir.close() } + const topology = createChrTransitTopologyOwner({ scope, routers: { A: exact("A"), B: exact("B") }, + linux: pins.linux, deadlineAt, writeAdmission: options.writeAdmission, authorize: auth, + assertOutsideScope: inventory.assertOutsideScope, assertFlowRestored, + checkpoint: async entry => { + await journal.writeFile(JSON.stringify(entry) + "\n"); await journal.sync() + if (entry.phase === "verified" && entry.id && entry.action === "create") owned.set(entry.step, entry.id) + } }) + const tuple = (phase: "initial" | "refreshed") => ({ + source: { address: plan.address.endpointA, port: phase === "initial" ? plan.ports.phase1Source : plan.ports.phase2Source }, + destination: { address: plan.address.endpointB, port: phase === "initial" ? plan.ports.phase1Destination : plan.ports.phase2Destination }, + }) + const phases = ["initial", "refreshed"] as const + async function prepareRouter(side: Side) { + const roles: readonly (keyof typeof roleNames)[] = routerRoles[side] + const interfaces = await Promise.all(roles.map(async role => { + const id = owned.get(role), name = plan.names[roleNames[role]] + must(nativeId(id), "CHR interface ACK pin absent") + const [row] = await get(side, "/interface/" + encodeRosId(id), 1, true) + must(row[".id"] === id && row.name === name && isChrWireGuardInterfaceType(row.type) && typeof row.ifindex === "string" && + /^[1-9][0-9]{0,9}$/.test(row.ifindex), "CHR interface ifIndex pin absent") + return { id, name, ifIndex: row.ifindex } + })) + const [edge, middle] = interfaces + const rib = await get(side, "/routing/route", 2048) + const routePins = plan.routes.filter(route => route.node === side).map(route => { + const role = roles.find(item => item === route.device) + must(role, "CHR route role differs") + const iface = interfaces[roles.indexOf(role)] + const matches = rib.filter(row => row["dst-address"] === route.destination && row["routing-table"] === "main") + const routeId = matches[0]?.[".id"], addressId = owned.get("address:" + role) + must(matches.length === 1 && nativeId(routeId) && nativeId(addressId), "CHR route or address pin absent") + return { routeId, destination: route.destination.slice(0, -3), nextHop: route.nextHop, + interfaceName: iface.name, interfaceId: iface.id, addressId, interfaceCidr: plan.addresses[role] } + }) + const udp = phases.map(tuple) + const reader = createChrRouteConntrackReader({ client: clients[side], ...pins.routers[side], routes: routePins, + udp, assertCurrent: auth, profile: "owned-wg-empty-firewall" }) + const packets = udp.flatMap(({ source, destination }) => { + const [ingress, egress] = side === "A" ? [edge, middle] : [middle, edge] + return [{ direction: "forward" as const, source: source.address, destination: destination.address, + sourcePort: source.port, destinationPort: destination.port, ingress: ingress.name, egress: egress.name }, + { direction: "forward" as const, source: destination.address, destination: source.address, + sourcePort: destination.port, destinationPort: source.port, ingress: egress.name, egress: ingress.name }] + }) + await verifyChrFirewallPreflight({ client: clients[side], ...pins.routers[side], packets, + assertCurrent: auth, profile: "owned-wg-empty-firewall", readPathEvidence: reader }) + const counterPins = [edge, middle] as const + return { baseline: baseline[side], read: () => flow(side), native: clients[side], targetNative: clients[side], + transitInterface: edge.name, interfaces: counterPins, + counters: createChrInterfaceCounterReader({ client: clients[side], ...pins.routers[side], interfaces: counterPins, assertCurrent: auth }) } + } + type OperatorOptions = Parameters[0] + const operator: Pick = { + directory: root + "/capture-evidence", deadlineAt, + scope: { interfaceName: plan.names.cv, linkType: "EN10MB", sources: [plan.address.exportA, plan.address.exportB], + destination: plan.address.collector, port: plan.ports.collector }, + serverIds: staged ? [1, 2] : context.serverIds, writeAdmission: options.writeAdmission, capture: host.capture, + admitBeforeHardwareWrites: async () => { + // Choosing the sealed protocol is not evidence that the full + // staged route fits its authority and cleanup reserve. + must(!captureProfile, "Staged full CHR timing and authority have not been admitted") + const input = { pairGets: CHR_TOPOLOGY_CATALOG_PAIR_GETS, catalogueReadLimit: CHR_TOPOLOGY_CATALOG_READ_LIMIT, + captureMs: 90000, targetMinimumRemainingMs: CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS, + forwardWrites: 2 + chrDisabledFlowPatchCount(baseline.A.ipfix) + chrDisabledFlowPatchCount(baseline.B.ipfix) } + let assessment = assessChrCaptureTiming(input) + if (assessment.status === "measurement-required") { + const start = performance.now() + await inventory.assertOutsideScope([]) + await inventory.read("A") + await inventory.read("B") + assessment = assessChrCaptureTiming({ ...input, pairedReadMs: performance.now() - start }) + } + const file = await open(topologyPath + "/timing.json", "wx", 0o600) + try { await file.writeFile(JSON.stringify(assessment) + "\n"); await file.sync() } + finally { await file.close() } + must(assessment.status === "budget-check-passed", "Full CHR write schedule exceeds the reviewed timing budget") + }, + topology: { + acquire: async () => { await topology.acquire() }, + assertCurrent: topology.assertCurrent, + preflight: async () => { + await host.topologyReady() + await host.generate("calibration") + const [first, second] = await Promise.all(sides.map(prepareRouter)) + const routers = [first, second] as const + const expected: DisabledChrCaptureOptions["expected"] = phases.flatMap(phase => { + const { source, destination } = tuple(phase) + return ([0, 1] as const).flatMap(exporter => { + const [edge, middle] = routers[exporter].interfaces + const [ingress, egress] = exporter === 0 ? [edge, middle] : [middle, edge] + return [{ exporter, phase, src: source.address, dst: destination.address, srcPort: source.port, + dstPort: destination.port, inIface: ingress.ifIndex, outIface: egress.ifIndex, packets: 3 }, + { exporter, phase, src: destination.address, dst: source.address, srcPort: destination.port, + dstPort: source.port, inIface: egress.ifIndex, outIface: ingress.ifIndex, packets: 3 }] + }) + }) + return { routers, expected } + }, + restore: async () => { await topology.restore() }, + }, + generate: async signal => { + const first = await host.generate("initial", signal) + await delay(Math.max(0, first.lastDatagramAt + CHR_TARGET_TEMPLATE_TIMEOUT_MS + 2000 - Date.now()), undefined, { signal }) + return host.generate("refresh", signal) + }, + } + const reconcile = (collectorContext: CollectorContext): DisabledChrCaptureOptions["reconcile"] => async wire => { + const minute = (value: number) => Math.floor(value / 60000) * 60000 + await collectorContext.reconcile({ wire, from: new Date(minute(wire.frames[0].receivedMs)).toISOString(), + until: new Date(minute(wire.frames.at(-1)!.receivedMs) + 60000).toISOString(), minutes: 5, + interfaceLabel: (_server, index) => "#" + index }) + } + if (staged) { + must(captureProfile, "Staged capture profile unavailable") + return await executeDisabledChrVpsOperator({ + ...operator, authorizeFixture, requiredCollectorWindowMs: captureProfile.requiredCollectorWindowMs, + stagedCapture: exercise => host.captureStaged(captureProfile.captureMs, exercise), + activateCollector: async requiredWindowMs => { + const activation = await context.activateCollector(requiredWindowMs) + must(activation.interfaceName === operator.scope.interfaceName && isDeepStrictEqual(activation.serverIds, operator.serverIds), + "Activated collector scope differs") + return { ...activation, reconcile: reconcile(activation) } + }, + }) + } + return await executeDisabledChrVpsOperator({ ...operator, pins: context.pins, collector: context.collector, + scope: { ...operator.scope, interfaceName: context.interfaceName }, reconcile: reconcile(context) }) + } finally { await journal.close() } + } finally { await bridge.close() } + } + const bootstrap = { + pins: { runId, deadlineAt, namespace, hostNamespace, collectorPort: plan.ports.collector }, + postgresSource: "pinned-container" as const, assertCurrent: authorizeRun, reviewApproval: options.reviewApproval, + enterStage: options.enterCollectorStage, + } + return captureProfile + ? runOwnedChrCollectorProcess({ ...bootstrap, stagedWork: work }) + : runOwnedChrCollectorProcess({ ...bootstrap, work }) +} + +async function readPrivateMount(path: "/run/mm-netflow-full-pins.json" | "/run/mm-netflow-chr-secret", max: number) { + const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW) + try { + const row = await file.stat() + must(row.isFile() && row.uid === 0 && row.gid === process.getegid?.() && (row.mode & 0o777) === 0o440 && + row.size > 0 && row.size <= max, "Full-run private mount refused") + const bytes = Buffer.alloc(row.size + 1) + const { bytesRead } = await file.read(bytes, 0, bytes.length, 0) + must(bytesRead === row.size, "Full-run private mount changed") + return bytes.subarray(0, bytesRead) + } finally { await file.close() } +} + +async function openChrFullAdmission(pins: Pins) { + const directory = "/tmp/mcp-netflow-" + pins.runId + "/ipc" + const path = directory + "/admission.sock" + const gid = process.getegid?.() + must(gid && isDeepStrictEqual(await readChrLinuxNamespaceIdentity(), pins.namespace), "Admission namespace differs") + const verify = async () => { + const [dir, row] = await Promise.all([lstat(directory, { bigint: true }), lstat(path, { bigint: true })]) + must(Date.now() < pins.deadlineAt && dir.isDirectory() && dir.uid === 0n && dir.gid === BigInt(gid) && + (dir.mode & 0o777n) === 0o710n && row.isSocket() && row.uid === 0n && row.gid === BigInt(gid) && + (row.mode & 0o777n) === 0o660n && String(row.dev) === pins.admissionSocket.device && + String(row.ino) === pins.admissionSocket.inode, "Admission socket pin differs") + } + await verify() + const socket = createConnection(path) + try { + await new Promise((resolve, reject) => { + const timer = setTimeout(() => socket.destroy(Error("Admission connection deadline ended")), 2000) + socket.once("error", error => { clearTimeout(timer); reject(error) }) + socket.once("connect", () => { clearTimeout(timer); resolve() }) + }) + await verify() + const expiry = setTimeout(() => socket.destroy(), Math.max(1, pins.deadlineAt - Date.now())) + socket.once("close", () => clearTimeout(expiry)) + return createChrChildAdmissionPipe({ input: socket, output: socket }, pins.captureProfile?.kind ?? "direct-target") + } catch (error) { socket.destroy(); throw error } +} + +export async function runFullChrNetflowCli(diagnostics = createChrCollectorBootstrapDiagnostics()) { + must(process.platform === "linux" && process.stdin.isTTY && process.stdout.isTTY && + process.env.MMAPP_OWN_CHR_NETFLOW_COLLECTOR === "true" && process.argv.slice(2).join() === "--run-full-chr-netflow", + "Full-run operator admission refused") + const raw = await readPrivateMount("/run/mm-netflow-full-pins.json", 16384) + must(createHash("sha256").update(raw).digest("hex") === process.env.MMAPP_NETFLOW_FULL_PINS_SHA256, "Full-run pins digest differs") + const parsed = parseChrFullPins(JSON.parse(raw.toString("utf8"))) + const password = (await readPrivateMount("/run/mm-netflow-chr-secret", 2049)).toString("utf8").replace(/\n$/, "") + const admission = await openChrFullAdmission(parsed.pins) + const tty = createInterface({ input: process.stdin, output: process.stdout }) + const abort = new AbortController() + const interrupt = () => abort.abort() + process.once("SIGINT", interrupt) + process.once("SIGTERM", interrupt) + try { + return await runFullChrNetflow({ reviewedPins: parsed.pins, password, writeAdmission: admission, + enterCollectorStage: diagnostics.enter, + assertCurrent: async () => { abort.signal.throwIfAborted() }, + reviewApproval: async ({ planId, enabled, deadlineAt }) => tty.question( + "Collector " + (enabled ? "enable" : "disable") + " plan " + planId + ". Enter approve followed by the exact ID: ", + { signal: AbortSignal.any([abort.signal, AbortSignal.timeout(Math.max(1, deadlineAt - Date.now()))]) }) }) + } finally { + process.off("SIGINT", interrupt) + process.off("SIGTERM", interrupt) + tty.close() + admission.close() + } +} + +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const diagnostics = createChrCollectorBootstrapDiagnostics() + runFullChrNetflowCli(diagnostics) + .then(result => console.log(JSON.stringify(result))) + .catch(error => { + console.error("MM_NETFLOW_FULL_ENTRY_REFUSED " + JSON.stringify(diagnostics.refusal(error))) + process.exitCode = 1 + }) +} diff --git a/backend/src/test/traffic-flow-chr-host-ipc.ts b/backend/src/test/traffic-flow-chr-host-ipc.ts new file mode 100644 index 00000000..84b3ef98 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-host-ipc.ts @@ -0,0 +1,158 @@ +import { createHash } from "node:crypto" +import { constants } from "node:fs" +import { lstat, open } from "node:fs/promises" +import { createConnection } from "node:net" +import { CHR_STAGED_TARGET_CAPTURE_MAX_MS } from "./traffic-flow-chr-target-timing-profile.js" +import type { ChrStagedTargetTimerProfile } from "./traffic-flow-chr-target-harness.js" + +type Operation = "topology-ready" | "generate" | "capture-start" | "capture-stop" | "cancel" +type Phase = "calibration" | "initial" | "refresh" +type Identity = { device: string; inode: string } +const limit = 4096 +const captureLimit = 512 * 1024 +function must(value: unknown, reason: string): asserts value { if (!value) throw Error(reason) } + +export function decodeChrHostReply(raw: Buffer, sequence: number, op: Operation, stagedStart = false) { + must(raw.length > 0 && raw.length <= limit && raw.at(-1) === 10 && raw.indexOf(10) === raw.length - 1, + "Invalid host IPC frame") + const value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(raw)) + must(value && value.sequence === sequence && value.op === op && value.ok === true, "Host IPC acknowledgement unavailable") + const extra = op === "generate" ? ["lastDatagramAt"] : op === "capture-stop" ? ["sha256", "complete", "dropped"] : + op === "capture-start" && stagedStart ? ["captureDeadlineAt"] : [] + must(Object.keys(value).sort().join() === ["sequence", "op", "ok", ...extra].sort().join(), "Invalid host IPC reply shape") + if (op === "generate") must(Number.isSafeInteger(value.lastDatagramAt) && value.lastDatagramAt > 0, "Host UDP time unconfirmed") + if (op === "capture-stop") must(value.complete === true && value.dropped === 0 && typeof value.sha256 === "string" && + /^[a-f0-9]{64}$/.test(value.sha256), "Host capture unconfirmed") + if (op === "capture-start" && stagedStart) must(Number.isSafeInteger(value.captureDeadlineAt) && value.captureDeadlineAt > 0, + "Host capture deadline unconfirmed") + return value as { lastDatagramAt?: number; sha256?: string; captureDeadlineAt?: number } +} + +export function bindChrStagedHostCaptureTimer(captureDeadlineAt: number | undefined, captureMs: number, + ownerDeadline: number, requestedAt: number, acknowledgedAt: number): Omit { + must(Number.isSafeInteger(captureMs) && captureMs >= 90000 && captureMs <= CHR_STAGED_TARGET_CAPTURE_MAX_MS && + [ownerDeadline, requestedAt, acknowledgedAt].every(value => Number.isSafeInteger(value) && value > 0) && + captureDeadlineAt !== undefined && Number.isSafeInteger(captureDeadlineAt) && requestedAt <= acknowledgedAt && + captureDeadlineAt > acknowledgedAt && captureDeadlineAt <= ownerDeadline && + captureDeadlineAt >= requestedAt + captureMs && captureDeadlineAt <= acknowledgedAt + captureMs, + "Host capture deadline outside reviewed window") + return { kind: "staged-ipfix-timeout-20s", captureDeadlineAt } +} + +export async function createChrHostControl(options: { + runId: string; nonce: string; deadlineAt: number; socket: Identity; assertCurrent: () => Promise +}) { + const { runId, nonce, deadlineAt, assertCurrent } = options + const pin = structuredClone(options.socket) + const gid = process.getegid?.() + must(process.platform === "linux" && gid && /^[a-f0-9]{8}$/.test(runId) && + /^[a-f0-9]{32}$/.test(nonce) && nonce.startsWith(runId), "Host IPC ownership unavailable") + const directory = "/tmp/mcp-netflow-" + runId + "/ipc" + const socketPath = directory + "/control.sock" + const fence = async () => { + await assertCurrent() + const [dir, socket] = await Promise.all([lstat(directory, { bigint: true }), lstat(socketPath, { bigint: true })]) + must(Date.now() < deadlineAt && dir.isDirectory() && dir.uid === 0n && dir.gid === BigInt(gid) && + (dir.mode & 0o777n) === 0o710n && socket.isSocket() && socket.uid === 0n && socket.gid === BigInt(gid) && + (socket.mode & 0o777n) === 0o660n && String(socket.dev) === pin.device && String(socket.ino) === pin.inode, + "Host IPC ownership changed") + } + await fence() + let sequence = 0, busy = false, failed = false, phase = "new" + const reached = (expected: string) => phase === expected + async function exchange(op: Operation, signal?: AbortSignal, generation?: Phase, stagedStart = false) { + must(!failed && !busy && !signal?.aborted && sequence < 8, "Host IPC request refused") + busy = true + try { + await fence() + const current = ++sequence + const started = Date.now() + const raw = await new Promise((resolve, reject) => { + const sock = createConnection(socketPath) + let reply = Buffer.alloc(0) + const fail = () => { sock.destroy(); reject(Error("Host IPC outcome unknown")) } + const timer = setTimeout(fail, Math.max(1, Math.min(15000, deadlineAt - Date.now()))) + const cleanup = () => { clearTimeout(timer); signal?.removeEventListener("abort", fail) } + signal?.addEventListener("abort", fail, { once: true }) + if (signal?.aborted) fail() + sock.once("connect", () => sock.write(JSON.stringify({ runId, nonce, sequence: current, op, + ...(generation && { phase: generation }) }) + "\n")) + sock.on("data", chunk => { + if (reply.length + chunk.length > limit) return fail() + reply = Buffer.concat([reply, chunk]) + if (reply.includes(10)) { cleanup(); resolve(reply); sock.destroy() } + }) + sock.once("error", fail) + sock.once("close", () => { cleanup(); reject(Error("Host IPC closed before acknowledgement")) }) + }) + const value = decodeChrHostReply(raw, current, op, stagedStart) + await fence() + if (op === "generate") must(value.lastDatagramAt! >= started && value.lastDatagramAt! <= Date.now(), + "Host UDP receipt is outside request window") + return value + } catch (error) { failed = true; throw error } + finally { busy = false } + } + async function capture(deadline: number, exercise: (signal: AbortSignal, timer?: Omit) => Promise, + stagedMs?: number) { + const requestedAt = Date.now() + must(phase === "calibration" && requestedAt < deadline && deadline <= deadlineAt, "Host capture phase differs") + const startSignal = AbortSignal.timeout(deadline - requestedAt) + const started = await exchange("capture-start", startSignal, undefined, stagedMs !== undefined) + phase = "capturing" + try { + const acknowledgedAt = Date.now() + const timer = stagedMs === undefined ? undefined : + bindChrStagedHostCaptureTimer(started.captureDeadlineAt, stagedMs, deadline, requestedAt, acknowledgedAt) + const signal = timer ? AbortSignal.timeout(timer.captureDeadlineAt - acknowledgedAt) : startSignal + if (timer) await exercise(signal, timer) + else await exercise(signal) + must(reached("refresh") && !signal.aborted, "Host capture exercise incomplete") + const { sha256 } = await exchange("capture-stop", signal) + phase = "finished" + const file = await open(directory + "/capture.pcap", constants.O_RDONLY | constants.O_NOFOLLOW) + try { + const row = await file.stat() + must(row.isFile() && row.uid === 0 && row.gid === gid && (row.mode & 0o777) === 0o640 && + row.nlink === 1 && row.size > 0 && row.size <= captureLimit, "Host capture ownership unavailable") + const bytes = Buffer.alloc(row.size + 1) + let offset = 0 + while (offset < bytes.length) { + const result = await file.read(bytes, offset, bytes.length - offset, null) + if (!result.bytesRead) break + offset += result.bytesRead + } + const capture = bytes.subarray(0, offset) + must(offset === row.size && createHash("sha256").update(capture).digest("hex") === sha256, "Host capture bytes differ") + return { bytes: capture, sha256: sha256!, complete: true, dropped: 0 } + } finally { await file.close() } + } finally { + if (!failed && phase !== "finished") { + phase = "cancelled" + // Parent cancellation intentionally refuses the session. The + // original capture failure remains the terminal cause. + await exchange("cancel").catch(() => {}) + } + } + } + return { + async topologyReady() { + must(phase === "new", "Host topology acknowledgement already attempted") + await exchange("topology-ready") + phase = "topology" + }, + async generate(generation: Phase, signal?: AbortSignal) { + const expected = generation === "calibration" ? "topology" : generation === "initial" ? "capturing" : "initial" + must(phase === expected, "Host UDP phase order differs") + const value = await exchange("generate", signal, generation) + phase = generation + return { lastDatagramAt: value.lastDatagramAt! } + }, + capture: (deadline: number, exercise: (signal: AbortSignal) => Promise) => capture(deadline, exercise), + captureStaged: (captureMs: number, exercise: Parameters[1]) => { + must(Number.isSafeInteger(captureMs) && captureMs >= 90000 && captureMs <= CHR_STAGED_TARGET_CAPTURE_MAX_MS && + Date.now() + captureMs < deadlineAt, "Staged host capture window unavailable") + return capture(deadlineAt, exercise, captureMs) + }, + } +} diff --git a/backend/src/test/traffic-flow-chr-independent-ipfix.test.ts b/backend/src/test/traffic-flow-chr-independent-ipfix.test.ts new file mode 100644 index 00000000..4100b69d --- /dev/null +++ b/backend/src/test/traffic-flow-chr-independent-ipfix.test.ts @@ -0,0 +1,86 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { decodeChrEgressIpfix, decodeOwnedIpfix } from "./traffic-flow-chr-independent-ipfix.js" +import { replayChrEgress } from "./traffic-flow-chr-egress-replay.js" + +const fields = [[1, 8], [2, 8], [4, 1], [7, 2], [8, 4], [10, 4], [11, 2], [12, 4], [14, 4]] +function set(id: number, body: Buffer) { + const header = Buffer.alloc(4) + header.writeUInt16BE(id); header.writeUInt16BE(body.length + 4, 2) + return Buffer.concat([header, body]) +} +function packet(sets: Buffer[]) { + const header = Buffer.alloc(16) + header.writeUInt16BE(10) + header.writeUInt16BE(16 + sets.reduce((total, value) => total + value.length, 0), 2) + header.writeUInt32BE(7, 12) + return Buffer.concat([header, ...sets]).toString("hex") +} +function fixture() { + const template = Buffer.alloc(4 + fields.length * 4) + template.writeUInt16BE(256); template.writeUInt16BE(fields.length, 2) + fields.forEach(([field, size], index) => { template.writeUInt16BE(field, 4 + index * 4); template.writeUInt16BE(size, 6 + index * 4) }) + const data = Buffer.alloc(37) + data.writeBigUInt64BE(9007199254740993n); data.writeBigUInt64BE(3n, 8); data[16] = 17 + data.writeUInt16BE(9000, 17); Buffer.from([192, 0, 2, 1]).copy(data, 19); data.writeUInt32BE(11, 23) + data.writeUInt16BE(9001, 27); Buffer.from([198, 51, 100, 1]).copy(data, 29); data.writeUInt32BE(12, 33) + const frames = [0, 1].map((exporter, index) => ({ + ordinal: index + 1, receivedMs: 1700000000000, receivedNs: "1700000000000000000", exporter, sourcePort: 2055, + version: 10, domain: 7, payloadHex: packet([set(2, template), set(256, data)]), + })) + return { template, data, input: { captureSha256: "a".repeat(64), scope: { sources: ["192.0.2.10", "192.0.2.20"], destination: "192.0.2.100", port: 4739 }, frames } } +} + +test("independent IPv4 IPFIX decoder preserves exact counters beyond JavaScript safe integer range", () => { + const { input } = fixture() + const result = decodeOwnedIpfix(input) + assert.equal(result.captureSha256, input.captureSha256) + assert.equal(result.nativeAcceptance, false) + for (const row of result.frames) { + assert.deepEqual(row.setIds, [2, 256]) + assert.deepEqual(row.flows, [{ src: "192.0.2.1", dst: "198.51.100.1", proto: 17, srcPort: 9000, dstPort: 9001, + inIface: "11", outIface: "12", bytes: "9007199254740993", packets: "3" }]) + } +}) + +test("compares single-router egress without admitting it as dual-source ingress", () => { + const { input, data, template } = fixture() + data.writeBigUInt64BE(1240n) + const frame = { ...input.frames[0], payloadHex: packet([set(2, template), set(256, data)]) } + const wire = { + capturePoint: "router-egress" as const, captureSha256: input.captureSha256, + scope: { source: input.scope.sources[0], destination: input.scope.destination, port: input.scope.port }, + frames: [frame], + } + const result = replayChrEgress(wire) + assert.equal(result.captureReplayMatched, true) + assert.equal(result.captureSha256, input.captureSha256) + assert.equal(result.frames[0].flows[0].bytes, "1240") + assert.equal(result.liveCollectorAccepted, false) + assert.throws(() => decodeOwnedIpfix(wire)) + assert.throws(() => decodeChrEgressIpfix({ ...wire, frames: [{ ...frame, exporter: 1 }] })) + assert.throws(() => replayChrEgress({ ...wire, frames: [input.frames[0]] }), /exact JavaScript integer/) +}) + +test("refuses template borrowing across exporters and UDP source ports", () => { + const { input, data } = fixture() + input.frames[1].payloadHex = packet([set(256, data)]) + assert.throws(() => decodeOwnedIpfix(input), /Missing IPFIX template/) + input.frames[1].exporter = 0 + input.frames[1].sourcePort = 2056 + assert.throws(() => decodeOwnedIpfix(input), /Missing IPFIX template/) +}) + +test("refuses duplicate required fields, ambiguous padding and unsupported variable fields", () => { + const duplicate = fixture() + duplicate.template.writeUInt16BE(1, 8) + duplicate.input.frames[0].payloadHex = packet([set(2, duplicate.template), set(256, duplicate.data)]) + assert.throws(() => decodeOwnedIpfix(duplicate.input), /Ambiguous/) + const variable = fixture() + variable.template.writeUInt16BE(65535, 6) + variable.input.frames[0].payloadHex = packet([set(2, variable.template), set(256, variable.data)]) + assert.throws(() => decodeOwnedIpfix(variable.input), /Variable/) + const padded = fixture() + padded.input.frames[0].payloadHex = packet([set(2, padded.template), set(256, Buffer.concat([padded.data, Buffer.from([1])]))]) + assert.throws(() => decodeOwnedIpfix(padded.input), /padding/) +}) diff --git a/backend/src/test/traffic-flow-chr-independent-ipfix.ts b/backend/src/test/traffic-flow-chr-independent-ipfix.ts new file mode 100644 index 00000000..a7039626 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-independent-ipfix.ts @@ -0,0 +1,154 @@ +/** Narrow independent decoder for the reviewed IPv4 RouterOS IPFIX fixture. + * Deliberately imports no product flow parser or SQL aggregation. */ +import { createHash } from "node:crypto" +import { open } from "node:fs/promises" +import { isIPv4 } from "node:net" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import { z } from "zod" + +const sha = z.string().regex(/^[a-f0-9]{64}$/) +const ip = z.string().max(15).refine(isIPv4) +const port = z.number().int().min(1).max(65535) +const wireFrame = z.object({ + ordinal: z.number().int().positive().safe(), receivedMs: z.number().int().nonnegative().safe(), + receivedNs: z.string().regex(/^\d{1,20}$/), exporter: z.union([z.literal(0), z.literal(1)]), + sourcePort: port, version: z.literal(10), domain: z.number().int().min(0).max(0xffffffff), + payloadHex: z.string().min(40).max(8192).regex(/^(?:[0-9a-f]{2})+$/), +}).strict() +const receiver = { destination: ip, port, linkType: z.enum(["RAW", "EN10MB"]).optional() } +const projection = z.object({ + captureSha256: sha, + scope: z.object({ sources: z.tuple([ip, ip]), ...receiver }).strict(), + frames: z.array(wireFrame).min(2).max(64), +}).strict() +const egressProjection = z.object({ + capturePoint: z.literal("router-egress"), + captureSha256: sha, + scope: z.object({ source: ip, ...receiver }).strict(), + frames: z.array(wireFrame.extend({ exporter: z.literal(0) })).min(1).max(64), +}).strict() +const required = new Map([[1, [1, 2, 4, 8]], [2, [1, 2, 4, 8]], [4, [1]], [7, [2]], [8, [4]], [10, [1, 2, 4]], [11, [2]], [12, [4]], [14, [1, 2, 4]]]) +type Field = { offset: number; size: number } +type Template = { fields: Map; width: number } +function must(value: unknown, error: string): asserts value { + if (!value) throw Error(error) +} +function padding(bytes: Buffer) { + return bytes.length <= 3 && bytes.every(value => value === 0) +} + +export function decodeOwnedIpfix(raw: unknown) { + return decode(projection.parse(raw)) +} + +export function decodeChrEgressIpfix(raw: unknown) { + const input = egressProjection.parse(raw) + return { ...decode({ ...input, scope: { sources: [input.scope.source], destination: input.scope.destination } }), + capturePoint: input.capturePoint } +} + +function decode(input: { captureSha256: string; scope: { sources: readonly string[]; destination: string }; frames: z.infer[] }) { + const sourceCount = input.scope.sources.length + must(new Set([...input.scope.sources, input.scope.destination]).size === sourceCount + 1, "Invalid exporter scope") + const templates = new Map() + let last = 0, records = 0, lastTimestamp = -1n + const positive = new Set() + const frames = input.frames.map(frame => { + const timestamp = BigInt(frame.receivedNs) + must(frame.ordinal === last + 1 && timestamp >= lastTimestamp && timestamp / 1000000n === BigInt(frame.receivedMs), "Invalid wire ordinal or timestamp") + last = frame.ordinal + lastTimestamp = timestamp + const data = Buffer.from(frame.payloadHex, "hex") + must(data.readUInt16BE(0) === 10 && data.readUInt16BE(2) === data.length && data.readUInt32BE(12) === frame.domain, "IPFIX header disagrees with wire metadata") + const session = [frame.exporter, frame.sourcePort, frame.domain].join(":") + const flows = [] + const setIds: number[] = [] + const templateIds: number[] = [] + for (let offset = 16; offset < data.length;) { + must(data.length - offset >= 4 && setIds.length < 32, "Invalid IPFIX set count or header") + const id = data.readUInt16BE(offset), length = data.readUInt16BE(offset + 2) + must(length >= 4 && offset + length <= data.length, "Invalid IPFIX set length") + const body = data.subarray(offset + 4, offset + length) + offset += length + setIds.push(id) + if (id === 2) { + for (let at = 0; at < body.length && !padding(body.subarray(at));) { + must(body.length - at >= 4, "Truncated IPFIX template") + const templateId = body.readUInt16BE(at), fieldCount = body.readUInt16BE(at + 2) + must(templateId >= 256 && fieldCount >= required.size && fieldCount <= 64, "Unsupported template or withdrawal") + at += 4 + const fields = new Map() + let width = 0 + for (let index = 0; index < fieldCount; index++) { + must(body.length - at >= 4, "Truncated IPFIX field") + const field = body.readUInt16BE(at), size = body.readUInt16BE(at + 2) + at += 4 + must(size > 0 && width + size <= 4096, "Variable or oversized IPFIX field") + if (field & 0x8000) { + must(body.length - at >= 4, "Truncated enterprise field") + at += 4 + } else if (required.has(field)) { + must(!fields.has(field) && required.get(field)!.includes(size), "Ambiguous or unsupported required field") + fields.set(field, { offset: width, size }) + } + width += size + } + must(fields.size === required.size, "Required IPv4 flow fields are missing") + templates.set(session + ":" + templateId, { fields, width }) + must(templates.size <= 32, "Template budget exceeded") + templateIds.push(templateId) + } + continue + } + must(id >= 256, "Unsupported IPFIX control set") + const template = templates.get(session + ":" + id) + must(template && body.length >= template.width, "Missing IPFIX template or data") + let at = 0 + for (; at + template.width <= body.length; at += template.width) { + must(++records <= 200 && flows.length < 64, "Independent record budget exceeded") + const part = (field: number) => { + const item = template.fields.get(field)! + return body.subarray(at + item.offset, at + item.offset + item.size) + } + const unsigned = (field: number) => part(field).reduce((value, byte) => value * 256n + BigInt(byte), 0n) + const bytes = unsigned(1), packets = unsigned(2) + must(bytes > 0n && packets > 0n && bytes <= 9223372036854775807n && packets <= 9223372036854775807n, "Counter outside positive SQL bigint range") + flows.push({ src: [...part(8)].join("."), dst: [...part(12)].join("."), proto: Number(unsigned(4)), + srcPort: Number(unsigned(7)), dstPort: Number(unsigned(11)), inIface: String(unsigned(10)), outIface: String(unsigned(14)), + bytes: String(bytes), packets: String(packets) }) + } + must(padding(body.subarray(at)), "Ambiguous IPFIX data padding") + } + if (flows.length) positive.add(frame.exporter) + return { ordinal: frame.ordinal, flows, setIds, templateIds } + }) + must(positive.size === sourceCount, sourceCount === 2 ? "Both exporters need independently decoded positive traffic" : + "Exporter needs independently decoded positive traffic") + return { captureSha256: input.captureSha256, frames, source: "independent-fixed-ipv4-ipfix-v1", nativeAcceptance: false } +} + +async function main() { + const [path, expectedSha, ...extra] = process.argv.slice(2) + must(path && !extra.length && sha.safeParse(expectedSha).success, "Expected pinned wire projection") + const handle = await open(path, "r") + try { + const info = await handle.stat() + must(info.isFile() && info.size > 0 && info.size <= 512 * 1024, "Wire projection outside file bound") + const buffer = Buffer.alloc(info.size + 1) + let size = 0 + while (size < buffer.length) { + const { bytesRead } = await handle.read(buffer, size, buffer.length - size, size) + if (!bytesRead) break + size += bytesRead + } + const body = buffer.subarray(0, size) + must(size === info.size && createHash("sha256").update(body).digest("hex") === expectedSha, "Wire projection changed or digest differs") + console.log(JSON.stringify({ wireProjectionSha256: expectedSha, ...decodeOwnedIpfix(JSON.parse(body.toString("utf8"))) })) + } finally { + await handle.close() + } +} +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch(() => { console.error("Independent IPFIX projection refused"); process.exitCode = 1 }) +} diff --git a/backend/src/test/traffic-flow-chr-ingress-discovery.test.ts b/backend/src/test/traffic-flow-chr-ingress-discovery.test.ts new file mode 100644 index 00000000..db3781a8 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-ingress-discovery.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { discoverChrIngressEndpoint } from "./traffic-flow-chr-ingress-discovery.js" + +type Options = Parameters[0] +function fixture() { + const reads: string[] = [], toolReads: string[] = [] + const collectors = { enabled: true, collectorIp: "192.0.2.100", flowListenPort: 4740, + listenerBound: true, listenerAddress: "192.0.2.100:4740" } + let revoked = false + const mappings = new Map([["192.0.2.10", 1], ["192.0.2.20", 2]]) + const native = (serverId: number) => { + const identity = "owned-" + serverId, version = "7.24.5 (stable)", source = "192.0.2." + serverId * 10 + const table: Record = { + "/system/identity": [{ name: identity }], + "/system/resource": { version }, + "/ip/address": [{ address: source + "/32", disabled: "false" }], + "/ip/traffic-flow": [{ enabled: "true" }], + "/ip/traffic-flow/target": [{ ".id": "*A", disabled: "false", "dst-address": "192.0.2.100", + "src-address": serverId === 1 ? source : "0.0.0.0", port: "4740", version: "ipfix" }], + } + const device: Options["devices"][0] = { + identity, version, source, serverId, + assertPinnedTransport: async () => { if (revoked) throw Error("transport pin lost") }, + client: { async get(path: string, timeout?: number, _signal?: AbortSignal, max?: number, before?: () => Promise) { + assert(timeout! > 0 && timeout! <= 5000) + assert.equal(max, 65536) + assert(before) + await before() + reads.push(serverId + ":" + path) + assert(Object.hasOwn(table, path)) + return structuredClone(table[path]) as T + } }, + } + return { table, device } + } + const a = native(1), b = native(2) + const options: Options = { destination: collectors.collectorIp, baselinePort: 4740, testPort: 4739, + deadlineAt: Date.now() + 5000, devices: [a.device, b.device], assertCurrent: async () => {}, + callTool: async (name, input) => { assert.deepEqual(input, {}); toolReads.push(name); return collectors }, + resolveExporter: async address => mappings.get(address) ?? null, + } + return { options, reads, toolReads, a, b, mappings, collectors, revoke: () => { revoked = true } } +} + +test("discovers exact configured two-CHR baseline endpoint without claiming observed ingress", async () => { + const f = fixture() + const result = await discoverChrIngressEndpoint(f.options) + assert.deepEqual(result.endpoint, { sources: ["192.0.2.10", "192.0.2.20"], destination: "192.0.2.100", port: 4740 }) + assert.deepEqual(result.observations.map(row => row.sourceConfiguredExplicitly), [true, false]) + assert.equal(result.ingressVerified, false) + assert.equal(f.reads.length, 10) + assert.deepEqual(f.toolReads, ["mm_netflow_settings_get", "mm_netflow_settings_get"]) +}) + +test("refuses absent exact target, competing test port, source drift and attribution mismatch", async () => { + for (const [mutate, reason] of [ + [(f: ReturnType) => { f.b.table["/ip/traffic-flow/target"] = [] }, /baseline IPFIX target/], + [(f: ReturnType) => { (f.a.table["/ip/traffic-flow/target"] as object[]).push({ ".id": "*B", "dst-address": "192.0.2.100", port: "4739" }) }, /test endpoint/], + [(f: ReturnType) => { f.b.table["/ip/address"] = [{ address: "192.0.2.99/32", disabled: false }] }, /source is not assigned/], + [(f: ReturnType) => { f.mappings.set("192.0.2.20", 1) }, /attribution differs/], + ] as const) { + const f = fixture() + mutate(f) + await assert.rejects(discoverChrIngressEndpoint(f.options), reason) + } +}) + +test("stops on wrong collector bind, lost transport pin and changed CHR identity", async () => { + const wrong = fixture(); wrong.collectors.listenerAddress = "0.0.0.0:4740" + await assert.rejects(discoverChrIngressEndpoint(wrong.options), /collector endpoint/) + assert.deepEqual(wrong.reads, []) + const revoked = fixture(); revoked.revoke() + await assert.rejects(discoverChrIngressEndpoint(revoked.options), /transport pin lost/) + assert.deepEqual(revoked.reads, []) + const changed = fixture(); changed.a.table["/system/identity"] = [{ name: "foreign" }] + await assert.rejects(discoverChrIngressEndpoint(changed.options), /identity or version/) + assert.deepEqual(changed.reads, ["1:/system/identity", "1:/system/resource"]) +}) diff --git a/backend/src/test/traffic-flow-chr-ingress-discovery.ts b/backend/src/test/traffic-flow-chr-ingress-discovery.ts new file mode 100644 index 00000000..77764485 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-ingress-discovery.ts @@ -0,0 +1,123 @@ +/** Exact read-only prerequisite for a passive two-CHR ingress capture. */ +import { isIPv4 } from "node:net" +import { setTimeout as delay } from "node:timers/promises" +import type { MikrotikClient } from "../services/mikrotik.js" +import { captureOwnedChrPackets } from "./traffic-flow-chr-capture-process.js" + +type Row = Record +type Device = { + client: Pick + identity: string; version: string; source: string; serverId: number + assertPinnedTransport: () => Promise +} +type Options = { + destination: string; baselinePort: number; testPort: number + devices: readonly [Device, Device] + callTool: (name: "mm_netflow_settings_get", input: Record) => Promise + resolveExporter: (address: string) => Promise + assertCurrent: () => Promise + deadlineAt: number + signal?: AbortSignal +} +const targetPath = "/ip/traffic-flow/target" +const trueValue = (value: unknown) => value === true || value === "true" || value === "yes" +const falseValue = (value: unknown) => value === false || value === "false" || value === "no" +function must(value: unknown, reason: string): asserts value { + if (!value) throw Error(reason) +} +function rows(value: unknown, limit: number): Row[] { + const result = Array.isArray(value) ? value : [value] + must(result.length <= limit && result.every(row => row && typeof row === "object" && !Array.isArray(row)), "Invalid bounded native observation") + return result as Row[] +} +function singleton(value: unknown) { + const result = rows(value, 1) + must(result.length === 1, "Missing native singleton observation") + return result[0] +} + +export async function discoverChrIngressEndpoint(options: Options) { + const { destination, baselinePort, testPort, deadlineAt, signal } = options + const devices = options.devices.map(device => ({ ...device })) + const port = (value: number) => Number.isInteger(value) && value >= 1 && value <= 65535 + must(devices.length === 2 && new Set([...devices.map(device => device.source), destination]).size === 3 && + [...devices.map(device => device.source), destination].every(isIPv4) && new Set(devices.map(device => device.serverId)).size === 2 && + new Set(devices.map(device => device.identity)).size === 2 && + devices.every(device => Number.isSafeInteger(device.serverId) && device.serverId > 0 && device.identity.length > 0 && + device.identity.length <= 128 && device.version.length > 0 && device.version.length <= 128) && + port(baselinePort) && port(testPort) && baselinePort !== testPort && + Number.isSafeInteger(deadlineAt) && deadlineAt > Date.now() && deadlineAt - Date.now() <= 90000, "Invalid pinned ingress scope") + const live = () => { + signal?.throwIfAborted() + must(Date.now() < deadlineAt, "Ingress discovery deadline ended") + } + const check = async () => { live(); await options.assertCurrent(); live() } + const collector = async () => { + await check() + const value = singleton(await options.callTool("mm_netflow_settings_get", {})) + await check() + must(value.enabled === true && value.collectorIp === destination && value.flowListenPort === baselinePort && + value.listenerBound === true && value.listenerAddress === destination + ":" + baselinePort, "Current baseline collector endpoint is not confirmed") + } + await collector() + const observations = [] + for (const device of devices) { + const fence = async () => { await check(); await device.assertPinnedTransport(); await check() } + const get = async (path: string) => { + await fence() + const value = await device.client.get(path, Math.min(5000, deadlineAt - Date.now()), signal, 64 * 1024, fence) + await fence() + return value + } + const identity = singleton(await get("/system/identity")), resource = singleton(await get("/system/resource")) + must(identity.name === device.identity && resource.version === device.version, "Pinned CHR identity or version differs") + const addresses = rows(await get("/ip/address"), 256) + must(addresses.filter(row => typeof row.address === "string" && row.address.split("/")[0] === device.source && falseValue(row.disabled)).length === 1, + "Pinned exporter source is not assigned exactly once") + await fence() + must(await options.resolveExporter(device.source) === device.serverId, "Collector exporter attribution differs") + await fence() + const flow = singleton(await get("/ip/traffic-flow")) + must(trueValue(flow.enabled), "Traffic Flow is disabled or unconfirmed") + const targets = rows(await get(targetPath), 64) + must(targets.every(row => typeof row[".id"] === "string" && /^\*[a-f0-9]{1,16}$/i.test(row[".id"])) && + new Set(targets.map(row => row[".id"])).size === targets.length, "Target identity is malformed or ambiguous") + const matches = targets.filter(row => row["dst-address"] === destination && String(row.port) === String(baselinePort)) + must(matches.length === 1 && falseValue(matches[0].disabled) && matches[0].version === "ipfix", + "Exact enabled baseline IPFIX target is missing or ambiguous") + must(!targets.some(row => row["dst-address"] === destination && String(row.port) === String(testPort)), + "Proposed test endpoint is already configured") + const source = matches[0]["src-address"] + must(source === device.source || source === "0.0.0.0", "Baseline target source conflicts with the pinned address") + observations.push({ serverId: device.serverId, identity: device.identity, targetId: matches[0][".id"], + sourceAddress: device.source, sourceConfiguredExplicitly: source === device.source }) + } + await collector() + return { + endpoint: { sources: [devices[0].source, devices[1].source] as const, destination, port: baselinePort }, + testPort, observations, passiveCaptureCandidate: true, + ingressVerified: false, actualNativeAcceptance: false, + } +} + +/** Passive capture entry for the pinned Linux operator. The exercise only waits. */ +export async function observeChrBaselineIngress(options: Options & { interfaceName: string; sampleMs: number }) { + must(process.platform === "linux" && Number.isInteger(options.sampleMs) && options.sampleMs >= 1000 && options.sampleMs <= 30000, + "Invalid passive capture platform or window") + const discovered = await discoverChrIngressEndpoint(options) + must(Date.now() + options.sampleMs < options.deadlineAt, "Passive capture window exceeds the reviewed deadline") + const capture = await captureOwnedChrPackets({ ...discovered.endpoint, interfaceName: options.interfaceName }, { + deadlineAt: options.deadlineAt, + assertCurrent: async () => { + options.signal?.throwIfAborted() + await options.assertCurrent() + for (const device of options.devices) await device.assertPinnedTransport() + options.signal?.throwIfAborted() + }, + exercise: async signal => { + await delay(options.sampleMs, undefined, { signal: options.signal ? AbortSignal.any([signal, options.signal]) : signal }) + }, + }) + return { discovery: discovered, ingress: { bytes: capture.bytes, sha256: capture.sha256, existingPort: discovered.endpoint.port }, + counts: capture.counts, nativeAcceptance: false } +} diff --git a/backend/src/test/traffic-flow-chr-interface-counters.test.ts b/backend/src/test/traffic-flow-chr-interface-counters.test.ts new file mode 100644 index 00000000..109ea896 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-interface-counters.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createChrInterfaceCounterReader } from "./traffic-flow-chr-interface-counters.js" + +function fixture() { + const interfaces = [{ id: "*A", name: "nfa012bcdeea", ifIndex: "12" }, + { id: "*B", name: "nfa012bcdema", ifIndex: "13" }] as const + const calls: string[] = [] + const catalog: Record> = { + "/system/identity": { name: "chr-a" }, + "/system/resource": { version: "7.24.5 (stable)" }, + } + for (const pin of interfaces) catalog["/interface/" + pin.id] = { + ".id": pin.id, name: pin.name, type: "wg", ifindex: pin.ifIndex, running: "true", disabled: "false", + "rx-packet": "10", "tx-packet": "11", "fp-rx-packet": "0", "fp-tx-packet": "0", + "fp-rx-byte": "0", "fp-tx-byte": "0", opaque: "must stay private", + } + const config = { identity: "chr-a", version: "7.24.5 (stable)", interfaces, assertCurrent: async () => {}, + client: { async get(path: string, timeout?: number, _signal?: AbortSignal, bytes?: number, fence?: () => Promise) { + assert.ok(timeout && timeout <= 1500) + assert.equal(bytes, 16384) + await fence?.() + calls.push(path) + assert.ok(Object.hasOwn(catalog, path)) + return structuredClone(catalog[path]) as T + } }, + } + return { config, catalog, calls } +} + +test("reads only the exact native IDs with bounded identity checks and emits selected counters", async () => { + const { config, calls } = fixture() + const read = createChrInterfaceCounterReader(config) + const result = await read() + assert.deepEqual(calls, ["/system/identity", "/system/resource", "/interface/*A", "/interface/*B", "/system/identity", "/system/resource"]) + assert.ok(result.startedAt <= result.observedAt) + assert.deepEqual(result.rows.map(row => row.ifindex), ["12", "13"]) + assert.ok(result.rows.every(row => row["fp-rx-packet"] === "0" && !Object.hasOwn(row, "opaque"))) +}) + +test("refuses missing and overflowing counters, changed identity and index guesses", async () => { + for (const change of [{ "fp-rx-packet": undefined }, { "fp-rx-byte": "18446744073709551616" }, + { ifindex: undefined }, { ifindex: "10" }, { ".id": "*F" }]) { + const { config, catalog } = fixture() + Object.assign(catalog["/interface/*A"], change) + await assert.rejects(createChrInterfaceCounterReader(config)(), /counter unavailable|identity or state differs/) + } + const stale = fixture() + stale.config.assertCurrent = async () => { if (stale.calls.length >= 4) throw Error("expired") } + await assert.rejects(createChrInterfaceCounterReader(stale.config)(), /expired/) +}) diff --git a/backend/src/test/traffic-flow-chr-interface-counters.ts b/backend/src/test/traffic-flow-chr-interface-counters.ts new file mode 100644 index 00000000..8f5ab5b6 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-interface-counters.ts @@ -0,0 +1,70 @@ +import { performance } from "node:perf_hooks" +import { encodeRosId, type MikrotikClient } from "../services/mikrotik.js" +import { isChrWireGuardInterfaceType } from "./traffic-flow-chr-transit-fixture.js" + +type Row = Record +type InterfacePin = { id: string; name: string; ifIndex: string } +type Options = { + client: Pick + identity: string + version: string + interfaces: readonly [InterfacePin, InterfacePin] + assertCurrent: () => Promise +} + +export const ownedWgFastPathFields = ["fp-rx-byte", "fp-tx-byte", "fp-rx-packet", "fp-tx-packet"] as const +export const ownedWgPacketFields = ["rx-packet", "tx-packet"] as const +const fields = [...ownedWgFastPathFields, ...ownedWgPacketFields] +const validId = (id: string) => /^\*[a-f0-9]{1,16}$/i.test(id) +const index = (value: unknown) => typeof value === "string" && /^[1-9][0-9]{0,9}$/.test(value) && Number(value) <= 0xffffffff +const observedUint = (value: unknown) => typeof value === "string" && /^(0|[1-9][0-9]{0,19})$/.test(value) && + BigInt(value) <= 0xffffffffffffffffn +const deny = (reason: string): never => { throw Error("RouterOS owned counter read refused: " + reason) } + +/** Uses only fixed read operations. Missing counter/index fields refuse the + * profile rather than substituting zero or guessing the wire ifIndex. */ +export function createChrInterfaceCounterReader(options: Options) { + const { client, assertCurrent, identity, version } = options + const interfaces = structuredClone(options.interfaces) + if (!identity || !version || interfaces.length !== 2 || interfaces.some(pin => + !validId(pin.id) || !/^nf[a-f0-9]{8}(ea|ma|mb|eb)$/.test(pin.name) || !index(pin.ifIndex)) || + interfaces[0].id === interfaces[1].id || interfaces[0].ifIndex === interfaces[1].ifIndex || + interfaces[0].name.slice(0, -2) !== interfaces[1].name.slice(0, -2) || + !["ea,ma", "eb,mb"].includes(interfaces.map(pin => pin.name.slice(-2)).sort().join(","))) deny("invalid interface pins") + return async () => { + const startedAt = Date.now(), deadline = performance.now() + 10000 + const fence = async () => { + if (performance.now() >= deadline) deny("observation expired") + await assertCurrent() + if (performance.now() >= deadline) deny("observation expired") + } + const read = async (path: string) => { + await fence() + const result = await client.get(path, Math.min(1500, Math.ceil(deadline - performance.now())), undefined, 16384, fence) + await fence() + const rows = Array.isArray(result) ? result : [result] + if (rows.length !== 1 || !rows[0] || typeof rows[0] !== "object" || Array.isArray(rows[0])) deny("incomplete response") + return rows[0] as Row + } + const verifyIdentity = async () => { + const device = await read("/system/identity") + const resource = await read("/system/resource") + if (device.name !== identity || resource.version !== version) deny("device identity changed") + } + await verifyIdentity() + const rows: Row[] = [] + for (const pin of interfaces) { + const row = await read("/interface/" + encodeRosId(pin.id)) + if (row[".id"] !== pin.id || row.name !== pin.name || !isChrWireGuardInterfaceType(row.type) || row.ifindex !== pin.ifIndex || + ![true, "true", "yes"].includes(row.running as string | boolean) || + ![false, "false", "no"].includes(row.disabled as string | boolean)) deny("interface identity or state differs") + if (!fields.every(field => observedUint(row[field]))) deny("required counter unavailable") + rows.push({ ".id": pin.id, name: pin.name, type: "wg", ifindex: pin.ifIndex, + ...Object.fromEntries(fields.map(field => [field, row[field]])) }) + } + await verifyIdentity() + const observedAt = Date.now() + if (observedAt < startedAt || observedAt - startedAt > 10000) deny("observation wall clock changed") + return { startedAt, observedAt, rows } + } +} diff --git a/backend/src/test/traffic-flow-chr-linux-udp-owner.test.ts b/backend/src/test/traffic-flow-chr-linux-udp-owner.test.ts new file mode 100644 index 00000000..403269cd --- /dev/null +++ b/backend/src/test/traffic-flow-chr-linux-udp-owner.test.ts @@ -0,0 +1,20 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { parseChrLinuxUdpTable } from "./traffic-flow-chr-linux-udp-owner.js" + +const header = "sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode" +const row = (address: string, port: string, inode: string) => + ` 14: ${address}:${port} 00000000:0000 07 00000000:00000000 00:00000000 00000000 1000 0 ${inode} 2 0000000000000000 0` + +test("parses exact kernel IPv4 socket inode and leaves unrelated UDP ports out", () => { + const text = [header, row("03F212C6", "C355", "42"), row("00000000", "C356", "43")].join("\n") + assert.deepEqual(parseChrLinuxUdpTable(text, 4, 50005), [{ address: "198.18.242.3", inode: "42" }]) + assert.deepEqual(parseChrLinuxUdpTable(text, 4, 50007), []) +}) + +test("fails closed for malformed rows, ownerless sockets and IPv6 conflicts", () => { + assert.throws(() => parseChrLinuxUdpTable("header", 4, 50005), /kernel table/) + assert.throws(() => parseChrLinuxUdpTable(header + "\n14: malformed", 4, 50005), /kernel socket row/) + assert.throws(() => parseChrLinuxUdpTable(header + "\n" + row("00000000", "C355", "0"), 4, 50005), /unattributed/) + assert.throws(() => parseChrLinuxUdpTable(header + "\n" + row("0".repeat(32), "C355", "42"), 6, 50005), /ambiguous IPv6/) +}) diff --git a/backend/src/test/traffic-flow-chr-linux-udp-owner.ts b/backend/src/test/traffic-flow-chr-linux-udp-owner.ts new file mode 100644 index 00000000..5ac4c16d --- /dev/null +++ b/backend/src/test/traffic-flow-chr-linux-udp-owner.ts @@ -0,0 +1,86 @@ +import { open, opendir, readlink, stat } from "node:fs/promises" + +const MAX_TABLE_BYTES = 256 * 1024 +const MAX_ROWS = 2048 +const deny = (reason: string): never => { throw Error("Collector UDP ownership refused: " + reason) } +type UdpOwner = { pid: number; address: string; port: number } + +export function parseChrLinuxUdpTable(text: string, family: 4 | 6, port: number) { + const lines = text.trim().split("\n") + if (Buffer.byteLength(text) > MAX_TABLE_BYTES || lines.length > MAX_ROWS || + !/^sl\s+local_address\s+(?:rem_address|remote_address)\s+st\s/.test(lines[0])) deny("unrecognized or oversized kernel table") + const width = family === 4 ? 8 : 32 + const entries: { address: string; inode: string }[] = [] + for (const line of lines.slice(1)) { + const values = line.trim().split(/\s+/) + const socket = values[1]?.split(":") + if (!/^\d+:$/.test(values[0]) || socket?.length !== 2 || socket[0].length !== width || + !/^[0-9A-F]+$/.test(socket[0]) || !/^[0-9A-F]{4}$/.test(socket[1]) || + !/^(0|[1-9][0-9]{0,19})$/.test(values[9] ?? "")) deny("incomplete kernel socket row") + if (Number.parseInt(socket[1], 16) !== port) continue + if (family !== 4 || values[9] === "0") deny("ambiguous IPv6 or unattributed socket") + const address = socket[0].match(/../g)!.reverse().map(value => Number.parseInt(value, 16)).join(".") + entries.push({ address, inode: values[9] }) + } + return entries +} + +async function boundedRead(path: "/proc/self/net/udp" | "/proc/self/net/udp6") { + const file = await open(path, "r") + try { + const bytes = Buffer.alloc(MAX_TABLE_BYTES + 1) + let length = 0 + while (length < bytes.length) { + const { bytesRead } = await file.read(bytes, length, bytes.length - length, null) + if (bytesRead === 0) break + length += bytesRead + } + if (length > MAX_TABLE_BYTES) deny("kernel table exceeds budget") + return new TextDecoder("utf-8", { fatal: true }).decode(bytes.subarray(0, length)) + } finally { + await file.close() + } +} + +export async function readChrLinuxNamespaceIdentity() { + if (process.platform !== "linux") deny("Linux required") + const identity = await stat("/proc/self/ns/net", { bigint: true }) + return { device: String(identity.dev), inode: String(identity.ino) } +} + +/** Self-process observation in the collector namespace, with no process + * execution, pathname overrides or inference from a missing socket owner. */ +export async function readChrLinuxUdpOwner(options: { + namespace: { device: string; inode: string }; port: number; address: string +}): Promise { + const { namespace, port, address } = options + const current = async () => { + const identity = await readChrLinuxNamespaceIdentity() + if (identity.device !== namespace.device || identity.inode !== namespace.inode) deny("network namespace changed") + } + await current() + const scan = async () => { + const ipv4 = parseChrLinuxUdpTable(await boundedRead("/proc/self/net/udp"), 4, port) + parseChrLinuxUdpTable(await boundedRead("/proc/self/net/udp6"), 6, port) + if (ipv4.length > 1 || ipv4.some(row => row.address !== address)) deny("competing or wildcard UDP bind") + return ipv4[0] + } + const candidate = await scan() + if (candidate) { + let matches = 0, visited = 0 + const directory = await opendir("/proc/self/fd") + for await (const entry of directory) { + if (++visited > 2048 || !/^\d+$/.test(entry.name)) deny("process descriptor inventory exceeds budget") + try { + if (await readlink("/proc/self/fd/" + entry.name) === `socket:[${candidate.inode}]`) matches++ + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error + } + } + if (matches !== 1) deny("UDP socket is not exclusively identified in the collector process") + } + const recheck = await scan() + if (candidate?.inode !== recheck?.inode) deny("UDP socket changed during observation") + await current() + return candidate ? [{ pid: process.pid, address, port }] : [] +} diff --git a/backend/src/test/traffic-flow-chr-namespace-relay-policy.ts b/backend/src/test/traffic-flow-chr-namespace-relay-policy.ts new file mode 100644 index 00000000..ca22cfb6 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-namespace-relay-policy.ts @@ -0,0 +1,26 @@ +import { wireguardInterfaceDependencyPaths } from "../services/routeros-native-paths.js" + +const readPaths = new Set(["/system/identity", "/system/resource", "/interface", "/interface/wireguard", + "/interface/wireguard/peers", "/interface/bridge", "/interface/list/member", "/ip/address", "/ip/route", + "/ipv6/address", "/ipv6/route", "/routing/route", "/routing/rule", "/routing/table", "/ip/vrf", "/ip/settings", + "/ip/firewall/filter", "/ip/firewall/raw", "/ip/firewall/mangle", "/ip/firewall/nat", "/ipv6/firewall/filter", + "/ip/firewall/connection/tracking", "/ip/firewall/connection", "/ip/traffic-flow", "/ip/traffic-flow/ipfix", + "/ip/traffic-flow/target", ...wireguardInterfaceDependencyPaths]) +const rowMenus = ["/interface/wireguard", "/interface/wireguard/peers", "/ip/address", "/ip/route", "/ip/traffic-flow/target"] +export const targetMenu = "/ip/traffic-flow/target" +const singletonMenus = ["/ip/traffic-flow", "/ip/traffic-flow/ipfix"] +export const nativeId = /^\*[0-9a-f]{1,16}$/i +const deny = () => { throw Error("Owned CHR namespace relay refused") } + +export function chrNamespaceRelayOperation(method: string, rest: string) { + if (!rest.startsWith("/rest/")) return deny() + const path = rest.slice(5) + const last = path.lastIndexOf("/") + const menu = path.slice(0, last), token = path.slice(last + 1) + if (method === "GET" && (readPaths.has(path) || menu === "/interface" && nativeId.test(token))) return { path, write: false } + if (method === "PUT" && rowMenus.includes(path) || + method === "DELETE" && rowMenus.includes(menu) && nativeId.test(token) || + method === "PATCH" && (singletonMenus.includes(menu) || menu === targetMenu) && nativeId.test(token) || + method === "POST" && singletonMenus.includes(menu) && token === "set") return { path, write: true } + return deny() +} diff --git a/backend/src/test/traffic-flow-chr-namespace-relay.test.ts b/backend/src/test/traffic-flow-chr-namespace-relay.test.ts new file mode 100644 index 00000000..c750ec21 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-namespace-relay.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { chrNamespaceRelayOperation, normalizeChrNamespaceRelayWrite } from "./traffic-flow-chr-namespace-relay.js" + +test("admits exact finite catalogues, owned singleton and row operation shapes", () => { + assert.deepEqual(chrNamespaceRelayOperation("GET", "/rest/interface/*A"), { path: "/interface/*A", write: false }) + assert.deepEqual(chrNamespaceRelayOperation("GET", "/rest/interface/bridge/port"), { path: "/interface/bridge/port", write: false }) + assert.deepEqual(chrNamespaceRelayOperation("POST", "/rest/ip/traffic-flow/set"), { path: "/ip/traffic-flow/set", write: true }) + assert.deepEqual(chrNamespaceRelayOperation("DELETE", "/rest/ip/route/*42"), { path: "/ip/route/*42", write: true }) + assert.deepEqual(chrNamespaceRelayOperation("PUT", "/rest/ip/traffic-flow/target"), { path: "/ip/traffic-flow/target", write: true }) +}) + +test("refuses arbitrary destinations, paths, mutations and unsafe payloads", () => { + for (const [method, path] of [ + ["GET", "http://194.226.121.150/rest/ip/address"], ["GET", "/rest/ip/address?limit=2"], + ["POST", "/rest/interface/print"], ["POST", "/rest/system/reboot"], ["PATCH", "/rest/ip/settings/*1"], + ["DELETE", "/rest/ip/route/%2A42"], ["PUT", "/rest/ip/firewall/filter"], + ]) assert.throws(() => chrNamespaceRelayOperation(method, path), /refused/) + for (const body of ["[]", '{"enabled":true}', '{"Enabled":"yes"}', '{"enabled":"no\\u0000"}']) { + assert.throws(() => normalizeChrNamespaceRelayWrite(Buffer.from(body), "POST"), /refused/) + } + assert.deepEqual(normalizeChrNamespaceRelayWrite(Buffer.from('{"enabled":"no","enabled":"yes"}'), "POST"), { enabled: "yes" }) + assert.throws(() => normalizeChrNamespaceRelayWrite(Buffer.from("{}"), "DELETE"), /refused/) + assert.deepEqual(normalizeChrNamespaceRelayWrite(Buffer.alloc(0), "DELETE"), {}) +}) diff --git a/backend/src/test/traffic-flow-chr-namespace-relay.ts b/backend/src/test/traffic-flow-chr-namespace-relay.ts new file mode 100644 index 00000000..b118da39 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-namespace-relay.ts @@ -0,0 +1,252 @@ +import { chmod, chown, lstat, mkdir, realpath, rmdir } from "node:fs/promises" +import { createServer as createHttpServer, request, type IncomingMessage, type ServerResponse } from "node:http" +import { createConnection, createServer as createTcpServer, type Server, type Socket } from "node:net" +import { MikrotikClient } from "../services/mikrotik.js" +import { chrNamespaceRelayOperation, nativeId, targetMenu } from "./traffic-flow-chr-namespace-relay-policy.js" +import { readChrLinuxNamespaceIdentity } from "./traffic-flow-chr-linux-udp-owner.js" +import { chrFixtureDeadlineMaxMs, type ChrStagedFixtureLease } from "./traffic-flow-chr-target-timing-profile.js" + +import type { ChrParentNativeAck } from "./traffic-flow-chr-parent-stages.js" +import type { ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" + +export { chrNamespaceRelayOperation } from "./traffic-flow-chr-namespace-relay-policy.js" + +const sides = ["A", "B"] as const +type Side = (typeof sides)[number] +const mapping = { + A: { host: "194.226.121.150", port: 19081, file: "chr-a.sock" }, + B: { host: "104.165.244.7", port: 19082, file: "chr-b.sock" }, +} as const +const maxBody = 16 * 1024, maxResponse = 1024 * 1024, maxSessions = 16 +const deny = () => { throw Error("Owned CHR namespace relay refused") } +type FileIdentity = { device: string; inode: string } +type RelayWrite = { side: Side; method: string; path: string; body: Readonly> } +type ParentOptions = { runId: string; uid: number; gid: number; deadlineAt: number; hostNamespace: FileIdentity; + assertPinnedTunnel: (side: Side) => Promise; authorizeWrite?: (input: RelayWrite) => Promise; + recordWriteAcknowledgement?: (ack: ChrParentNativeAck) => void } + +function relayRoot(runId: string) { + if (!/^[a-f0-9]{8}$/.test(runId)) return deny() + return "/tmp/mcp-netflow-" + runId + "/relay" +} +async function identity(path: string, uid: number) { + const row = await lstat(path, { bigint: true }) + if (!row.isSocket() || row.uid !== BigInt(uid) || (row.mode & 0o777n) !== 0o600n) return deny() + return { device: String(row.dev), inode: String(row.ino) } +} +function same(a: FileIdentity, b: FileIdentity) { return a.device === b.device && a.inode === b.inode } +function bounded(deadlineAt: number, leaseProfile?: ChrStagedFixtureLease) { + if (!Number.isSafeInteger(deadlineAt) || Date.now() >= deadlineAt || + deadlineAt - Date.now() > chrFixtureDeadlineMaxMs(leaseProfile)) deny() +} +function listen(server: Server, path: string | number) { + return new Promise((resolve, reject) => { + server.once("error", reject) + const ready = () => { server.off("error", reject); resolve() } + if (typeof path === "string") server.listen(path, ready) + else server.listen(path, "127.0.0.1", ready) + }) +} +function ownServer(server: Server, deadline: number) { + const sockets = new Set() + server.on("connection", (socket: Socket) => { + if (sockets.size >= maxSessions || Date.now() >= deadline) { socket.destroy(); return } + sockets.add(socket) + const timer = setTimeout(() => socket.destroy(), Math.min(25000, Math.max(1, deadline - Date.now()))) + socket.on("error", () => {}) + socket.once("close", () => { clearTimeout(timer); sockets.delete(socket) }) + }) + return async () => { + for (const socket of sockets) socket.destroy() + if (server.listening) await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())) + } +} +async function bodyOf(req: IncomingMessage) { + const chunks = [] + let bytes = 0 + for await (const item of req) { + const chunk = Buffer.from(item) + bytes += chunk.length + if (bytes > maxBody) return deny() + chunks.push(chunk) + } + return Buffer.concat(chunks) +} +export function normalizeChrNamespaceRelayWrite(bytes: Buffer, method: string) { + if (method === "DELETE") { + if (bytes.length) return deny() + return {} + } + if (!bytes.length || bytes.length > maxBody) return deny() + const value = JSON.parse(bytes.toString("utf8")) as Record + if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).length > 24 || + Object.entries(value).some(([key, item]) => !/^[a-z][a-z0-9-]{0,47}$/.test(key) || + typeof item !== "string" || item.length > 4096 || /[\u0000-\u001f]/.test(item))) return deny() + return Object.freeze(value as Record) +} + +/** Owns Unix sockets in the outer namespace. The upstream can only be the + * already pinned dual relay. Mutations require separate per-request admission. */ +export async function openChrParentUnixRelay(options: ParentOptions) { + const { runId, uid, gid, deadlineAt, assertPinnedTunnel, authorizeWrite, recordWriteAcknowledgement } = options + const hostNamespace = structuredClone(options.hostNamespace) + const directory = relayRoot(runId) + bounded(deadlineAt) + if (process.platform !== "linux" || process.geteuid?.() !== 0 || + !Number.isSafeInteger(uid) || uid <= 0 || !Number.isSafeInteger(gid) || gid <= 0) return deny() + if (!same(hostNamespace, await readChrLinuxNamespaceIdentity())) return deny() + const parent = directory.slice(0, -"/relay".length) + const root = await lstat(parent) + if (!root.isDirectory() || root.uid !== uid || (root.mode & 0o777) !== 0o700 || await realpath(parent) !== parent) return deny() + for (const side of sides) await assertPinnedTunnel(side) + await mkdir(directory, { mode: 0o700 }) + const owned: { path: string; pin?: FileIdentity; close: () => Promise }[] = [] + const pins = {} as Record + const close = async () => { + for (const item of owned) { + // Node may unlink its own Unix path on close. Refuse a changed pathname + // before closing so a foreign path is never selected for deletion. + if (!item.pin || !same(item.pin, await identity(item.path, uid))) deny() + await item.close() + } + await rmdir(directory) + } + try { + for (const side of sides) { + const target = mapping[side], path = directory + "/" + target.file + const handler = async (req: IncomingMessage, res: ServerResponse) => { + bounded(deadlineAt) + const method = req.method ?? "", rest = req.url ?? "" + const { path: native, write } = chrNamespaceRelayOperation(method, rest) + const auth = req.headers.authorization + if (!auth || !/^Basic [A-Za-z0-9+/=]{1,8192}$/.test(auth) || req.headers.expect || req.headers.upgrade) deny() + let data = await bodyOf(req) + let accepted: ChrWriteIntent | void = undefined + if (write) { + if (!authorizeWrite) return deny() + const body = normalizeChrNamespaceRelayWrite(data, method) + accepted = await authorizeWrite({ side, method, path: native, body }) + if (recordWriteAcknowledgement && !accepted) return deny() + if (method === "PATCH" && native.startsWith(targetMenu + "/") && + (!recordWriteAcknowledgement || accepted?.step !== "target:" + side + ":enable" || + Object.keys(body).length !== 1 || body.disabled !== "no")) return deny() + if (method !== "DELETE") data = Buffer.from(JSON.stringify(body)) + } else if (data.length) return deny() + await assertPinnedTunnel(side) + if (!same(hostNamespace, await readChrLinuxNamespaceIdentity())) return deny() + bounded(deadlineAt) + if (req.destroyed && !req.complete || res.destroyed) return deny() + const ownedWrite = accepted + const upstream = request({ hostname: "127.0.0.1", port: target.port, method, path: rest, agent: false, + headers: { Host: target.host, Authorization: auth, "Content-Type": "application/json", + "Content-Length": data.length, Connection: "close" } }, response => { + let bytes = 0 + const chunks: Buffer[] = [] + const wantsIdentity = ownedWrite?.method === "PUT" || + ownedWrite?.method === "PATCH" && ownedWrite.path.startsWith(targetMenu + "/") + res.writeHead(response.statusCode ?? 502, { "Content-Type": "application/json", Connection: "close" }) + response.on("data", chunk => { + bytes += chunk.length + if (bytes > maxResponse) { response.destroy(); res.destroy(); return } + if (recordWriteAcknowledgement && wantsIdentity) { + if (bytes > maxBody) { response.destroy(); res.destroy(); return } + chunks.push(Buffer.from(chunk)) + } + if (!res.write(chunk)) { response.pause(); res.once("drain", () => response.resume()) } + }) + response.once("end", () => { + try { + if (recordWriteAcknowledgement && ownedWrite && response.complete) { + const status = response.statusCode ?? 502 + let body: unknown + if (wantsIdentity && (status === 200 || status === 201)) { + const row = JSON.parse(Buffer.concat(chunks).toString("utf8")) + if (!row || Array.isArray(row) || !Object.hasOwn(row, ".id") || !nativeId.test(row[".id"]) || + typeof row[".id"] !== "string") return res.destroy() + body = { ".id": row[".id"] } + } + recordWriteAcknowledgement({ intent: ownedWrite, status, body }) + } + res.end() + } catch { res.destroy() } + }) + response.once("aborted", () => res.destroy()) + response.once("error", () => res.destroy()) + }) + const timer = setTimeout(() => upstream.destroy(), Math.min(24000, Math.max(1, deadlineAt - Date.now()))) + upstream.once("error", () => res.destroy()) + upstream.once("close", () => clearTimeout(timer)) + res.once("close", () => upstream.destroy()) + upstream.end(data) + } + const server = createHttpServer({ maxHeaderSize: 16384, requestTimeout: 20000 }, + (req, res) => { void handler(req, res).catch(() => res.destroy()) }) + server.maxRequestsPerSocket = 1 + const item = { path, close: ownServer(server, deadlineAt), pin: undefined as FileIdentity | undefined } + await listen(server, path) + owned.push(item) + await chown(path, uid, gid) + await chmod(path, 0o600) + item.pin = await identity(path, uid) + pins[side] = item.pin + } + await chown(directory, uid, gid) + return { pins, close } + } catch (error) { + for (const item of owned) { + if (item.pin && same(item.pin, await identity(item.path, uid))) await item.close() + } + throw error + } +} + +/** C-side adapter. Production MikrotikClient is unchanged and sees only two + * exact namespace-local loopback ports. There is no TCP/DNS fallback. */ +export async function openChrNamespaceClients(options: { + runId: string; deadlineAt: number; pins: Readonly>; password: string; + leaseProfile?: ChrStagedFixtureLease + namespace: FileIdentity; hostNamespace: FileIdentity; assertCurrent: () => Promise +}) { + const { runId, deadlineAt, leaseProfile, password, assertCurrent } = options + const directory = relayRoot(runId), pins = structuredClone(options.pins), uid = process.geteuid?.() + bounded(deadlineAt, leaseProfile) + if (process.platform !== "linux" || !uid || !/^[^\u0000-\u001f\u007f]{1,512}$/u.test(password)) return deny() + const { namespace, hostNamespace } = structuredClone({ namespace: options.namespace, hostNamespace: options.hostNamespace }) + const fence = async () => { + await assertCurrent() + bounded(deadlineAt, leaseProfile) + if (same(namespace, hostNamespace) || !same(namespace, await readChrLinuxNamespaceIdentity())) return deny() + } + await fence() + const owned: (() => Promise)[] = [] + const clients = {} as Record + const close = async () => { for (const stop of [...owned].reverse()) await stop() } + try { + for (const side of sides) { + const { port, file } = mapping[side], path = directory + "/" + file + if (!same(pins[side], await identity(path, uid))) return deny() + const server = createTcpServer(socket => { + socket.pause() + void (async () => { + await fence() + if (socket.destroyed || !same(pins[side], await identity(path, uid))) return deny() + const peer = createConnection(path) + peer.once("error", () => socket.destroy()) + socket.once("close", () => peer.destroy()) + let bytes = 0 + const chunks: Buffer[] = [] + socket.on("data", chunk => { bytes += chunk.length; if (bytes > maxBody + 16384) socket.destroy() }) + socket.pipe(peer).pipe(socket) + })().catch(() => socket.destroy()) + }) + const stop = ownServer(server, deadlineAt) + await listen(server, port) + owned.push(stop) + clients[side] = new MikrotikClient({ host: "127.0.0.1", port, useSsl: false, verifySsl: true, username: "admin", password }) + } + return { clients, close } + } catch (error) { await close(); throw error } +} + + + diff --git a/backend/src/test/traffic-flow-chr-offline-replay.test.ts b/backend/src/test/traffic-flow-chr-offline-replay.test.ts new file mode 100644 index 00000000..6efabd7a --- /dev/null +++ b/backend/src/test/traffic-flow-chr-offline-replay.test.ts @@ -0,0 +1,149 @@ +import assert from "node:assert/strict" +import { execFile } from "node:child_process" +import { createHash } from "node:crypto" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { join } from "node:path" +import { fileURLToPath } from "node:url" +import { promisify } from "node:util" +import { test } from "node:test" +import { replayRecordedChrEvidence } from "./traffic-flow-chr-offline-replay.js" +import { verifyChrOwnedWgEvidence } from "./traffic-flow-chr-owned-wg-evidence.js" + +const digest = (value: Buffer | string) => createHash("sha256").update(value).digest("hex") +function fixture(ethernet = false, earlyTemplates = false) { + const link = ethernet ? 14 : 0 + const seconds = 1700000010 + const bucket = Math.floor(seconds / 60) * 60000 + const from = new Date(bucket).toISOString(), until = new Date(bucket + 60000).toISOString() + const scope = { sources: ["192.0.2.10", "192.0.2.20"], destination: "192.0.2.100", port: 4739, ...(ethernet ? { linkType: "EN10MB" as const } : {}) } + const fields = [[1, 4], [2, 4], [4, 1], [7, 2], [8, 4], [10, 4], [11, 2], [12, 4], [14, 4]] + const template = Buffer.alloc(8 + fields.length * 4) + template.writeUInt16BE(2); template.writeUInt16BE(template.length, 2) + template.writeUInt16BE(256, 4); template.writeUInt16BE(fields.length, 6) + fields.forEach(([id, width], index) => { template.writeUInt16BE(id, 8 + index * 4); template.writeUInt16BE(width, 10 + index * 4) }) + const data = Buffer.alloc(33) + data.writeUInt16BE(256); data.writeUInt16BE(data.length, 2) + data.writeUInt32BE(1200, 4); data.writeUInt32BE(3, 8); data[12] = 17 + data.writeUInt16BE(9000, 13); Buffer.from([192, 0, 2, 1]).copy(data, 15); data.writeUInt32BE(11, 19) + data.writeUInt16BE(9001, 23); Buffer.from([198, 51, 100, 1]).copy(data, 25); data.writeUInt32BE(12, 29) + const ipfix = Buffer.alloc(16) + ipfix.writeUInt16BE(10); ipfix.writeUInt16BE(ipfix.length + template.length + data.length, 2); ipfix.writeUInt32BE(7, 12) + const header = Buffer.alloc(24) + header.writeUInt32LE(0xa1b2c3d4); header.writeUInt16LE(2, 4); header.writeUInt16LE(4, 6) + header.writeUInt32LE(4124 + link, 16); header.writeUInt32LE(ethernet ? 1 : 101, 20) + const frames = [10, 20, 10, 20].map((octet, index) => ({ + octet, at: seconds, micros: index * 1000, sequence: Math.floor(index / 2), + sets: earlyTemplates && index < 2 ? [data] : [template, data], + })) + if (earlyTemplates) frames.unshift(...[10, 20].map(octet => + ({ octet, at: seconds - 1, micros: 0, sequence: 0, sets: [template] }))) + const packets = frames.map(({ octet, at, micros, sequence, sets }) => { + const payload = Buffer.concat([ipfix, ...sets]) + payload.writeUInt16BE(payload.length, 2) + const packet = Buffer.alloc(44 + link + payload.length) + packet.writeUInt32LE(at, 0); packet.writeUInt32LE(micros, 4) + packet.writeUInt32LE(packet.length - 16, 8); packet.writeUInt32LE(packet.length - 16, 12) + if (ethernet) packet.writeUInt16BE(0x0800, 28) + const ip = packet.subarray(16 + link) + ip[0] = 0x45; ip.writeUInt16BE(ip.length, 2); ip[9] = 17 + Buffer.from([192, 0, 2, octet, 192, 0, 2, 100]).copy(ip, 12) + ip.writeUInt16BE(2055, 20); ip.writeUInt16BE(scope.port, 22); ip.writeUInt16BE(ip.length - 20, 24) + payload.copy(ip, 28) + ip.writeUInt32BE(seconds, 32) + ip.writeUInt32BE(sequence, 36) + return packet + }) + const pcap = Buffer.concat([header, ...packets]) + const raw = [1, 2].map(serverId => ({ serverId, bucketAt: from, src: "192.0.2.1", dst: "198.51.100.1", + proto: 17, srcPort: 9000, dstPort: 9001, inIface: "11", outIface: "12", bytes: "2400", packets: "6" })) + const minute = raw.map(({ serverId, bucketAt, bytes, packets }) => ({ serverId, bucketAt, bytes, packets })) + const evidence = { + captureSha256: digest(pcap), complete: true, dropped: 0, scope, serverIds: [1, 2], from, until, minutes: 2, + interfaceLabels: [1, 2].map(serverId => ({ serverId, index: "11", label: "ether-" + serverId })), + before: { raw, minute }, after: structuredClone({ raw, minute }), + talkers: raw.map(row => ({ serverId: row.serverId, result: { + serverIds: [row.serverId], from: new Date(bucket - 60000).toISOString(), to: until, + topConversationsLimit: 20, total: 1, totalConversations: 1, observedExporters: 1, retainedRowsBytes: 2400, nextOffset: null, + coverageVerified: false, rowSumIsNetworkTotal: false, deduplicatedAcrossInterfaces: false, forwardingVerified: false, + items: [{ ...row, serverId: String(row.serverId), inIface: "ether-" + row.serverId, inIfaceIndex: "11", bytes: 2400, packets: 6, bps: 160 }], + } })), + } + return { pcap, evidence } +} + +test("owned WG evidence requires exact IPFIX tuples and unchanged FastPath counters", () => { + const { pcap, evidence } = fixture() + const makeExporter = (roles: [string, string]) => { + const interfaces = roles.map((role, index) => ({ id: "*" + (index + 1), name: "nfa012bcde" + role, + ifIndex: String(index + 11) })) as [{ id: string; name: string; ifIndex: string }, { id: string; name: string; ifIndex: string }] + const snapshot = (observedAt: number, packetCount: string) => ({ startedAt: observedAt, observedAt, rows: interfaces.map(item => ({ + ".id": item.id, name: item.name, type: "wg", ifindex: item.ifIndex, "rx-packet": packetCount, "tx-packet": packetCount, + "fp-rx-byte": "0", "fp-tx-byte": "0", "fp-rx-packet": "0", "fp-tx-packet": "0", + })) }) + return { interfaces, before: snapshot(1700000010000, "0"), after: snapshot(1700000011000, "6") } + } + const input = { bytes: pcap, sha256: evidence.captureSha256, complete: true, dropped: 0, + scope: { ...evidence.scope, sources: [evidence.scope.sources[0], evidence.scope.sources[1]] as [string, string] }, + exporters: [makeExporter(["ea", "ma"]), makeExporter(["mb", "eb"])] as const, + expected: ([0, 1] as const).map(exporter => ({ exporter, src: "192.0.2.1", dst: "198.51.100.1", + srcPort: 9000, dstPort: 9001, inIface: "11", outIface: "12", packets: 6 })), + } + assert.equal(verifyChrOwnedWgEvidence(input).nativeAcceptance, false) + const initialAndRefreshed = input.expected.flatMap(row => (["initial", "refreshed"] as const) + .map(phase => ({ ...row, phase, packets: 3 }))) + assert.equal(verifyChrOwnedWgEvidence({ ...input, expected: initialAndRefreshed, requireRefresh: true }).exactIpfixMatched, true) + const staged = fixture(false, true) + const stagedInput = { ...input, bytes: staged.pcap, sha256: staged.evidence.captureSha256, + expected: initialAndRefreshed, requireRefresh: true } + assert.throws(() => verifyChrOwnedWgEvidence(stagedInput), /counter window/) + assert.equal(verifyChrOwnedWgEvidence({ ...stagedInput, counterWindow: "traffic" }).exactIpfixMatched, true) + input.exporters[0].before.observedAt++ + assert.throws(() => verifyChrOwnedWgEvidence({ ...stagedInput, counterWindow: "traffic" }), /counter window/) + input.exporters[0].before.observedAt-- + assert.throws(() => verifyChrOwnedWgEvidence({ ...input, requireRefresh: true }), /initial and refreshed/) + assert.throws(() => verifyChrOwnedWgEvidence({ ...input, expected: input.expected.map(row => ({ ...row, packets: 5 })) }), /totals/) + const after = input.exporters[0].after.rows + for (const [row, field] of [[after[0], "rx-packet"], [after[1], "tx-packet"]] as const) { + row[field] = "5" + assert.throws(() => verifyChrOwnedWgEvidence(input), /packet delta.*IPFIX/) + row[field] = "7" + } + assert.equal(verifyChrOwnedWgEvidence(input).exactIpfixMatched, true) + input.exporters[0].after.rows[0]["fp-rx-packet"] = "1" + assert.throws(() => verifyChrOwnedWgEvidence(input), /FastPath/) +}) + +test("offline CLI joins original multi-set IPFIX pcap, product parser and recorded SQL/MCP evidence", async () => { + const { pcap, evidence } = fixture() + const dir = await mkdtemp(join(fileURLToPath(new URL("../../../tmp/", import.meta.url)), "mm-chr-offline-")) + try { + const packetPath = join(dir, "owned.pcap"), evidencePath = join(dir, "owned.json"), body = JSON.stringify(evidence) + await Promise.all([writeFile(packetPath, pcap), writeFile(evidencePath, body)]) + const { stdout } = await promisify(execFile)(process.execPath, ["--import", "tsx", + fileURLToPath(new URL("./traffic-flow-chr-offline-replay.ts", import.meta.url)), packetPath, digest(pcap), evidencePath, digest(body)], + { timeout: 15000, windowsHide: true }) + const result = JSON.parse(stdout) + assert.equal(result.recordedEvidenceMatched, true) + assert.equal(result.replay.frames, 4) + assert.equal(result.reconciliation.rawRows, 2) + assert.equal(result.actualNativeAcceptance, false) + } finally { + await rm(dir, { recursive: true, force: true }) + } +}) + +test("refuses a mixed capture pin or drift in post-MCP SQL evidence", async () => { + const { pcap, evidence } = fixture() + await assert.rejects(replayRecordedChrEvidence(pcap, "0".repeat(64), evidence), /Capture SHA/) + evidence.after.minute[0].packets = "5" + await assert.rejects(replayRecordedChrEvidence(pcap, digest(pcap), evidence), /persisted SQL/) +}) + +test("offline replay preserves an explicit Ethernet capture scope across both decoders", async () => { + const { pcap, evidence } = fixture(true) + const result = await replayRecordedChrEvidence(pcap, digest(pcap), evidence) + assert.equal(result.recordedEvidenceMatched, true) + assert.equal(result.replay.frames, 4) + await assert.rejects(replayRecordedChrEvidence(pcap, digest(pcap), { ...evidence, + scope: { ...evidence.scope, linkType: "RAW" } }), /pinned link type RAW/) +}) diff --git a/backend/src/test/traffic-flow-chr-offline-replay.ts b/backend/src/test/traffic-flow-chr-offline-replay.ts new file mode 100644 index 00000000..6c7c1736 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-offline-replay.ts @@ -0,0 +1,129 @@ +/** Offline comparison of one pcap with recorded SQL and MCP evidence. */ +import { createHash } from "node:crypto" +import { open } from "node:fs/promises" +import { isIPv4 } from "node:net" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import { z } from "zod" +import { checkChrCapture } from "./traffic-flow-chr-capture-check.js" +import { decodeOwnedIpfix } from "./traffic-flow-chr-independent-ipfix.js" +import { reconcileOwnedChrFlow } from "./traffic-flow-chr-reconcile.js" +import { projectChrWire } from "./traffic-flow-chr-wire.js" + +const sha = z.string().regex(/^[a-f0-9]{64}$/) +const ip = z.string().max(15).refine(isIPv4) +const serverId = z.number().int().positive().safe() +const snapshot = z.object({ raw: z.array(z.unknown()).max(200), minute: z.array(z.unknown()).max(20) }).strict() +const evidenceSchema = z.object({ + captureSha256: sha, complete: z.literal(true), dropped: z.literal(0), + scope: z.object({ sources: z.tuple([ip, ip]), destination: ip, port: z.number().int().min(1).max(65535), linkType: z.enum(["RAW", "EN10MB"]).optional() }).strict(), + serverIds: z.tuple([serverId, serverId]), + from: z.string().max(24), until: z.string().max(24), minutes: z.number().int().min(1).max(15), + interfaceLabels: z.array(z.object({ serverId, index: z.string().regex(/^(?:0|[1-9]\d{0,9})$/), label: z.string().min(1).max(200) }).strict()).max(200), + before: snapshot, after: snapshot, + talkers: z.tuple([z.object({ serverId, result: z.unknown() }).strict(), z.object({ serverId, result: z.unknown() }).strict()]), +}).strict() +function must(value: unknown, reason: string): asserts value { + if (!value) throw Error(reason) +} +function replayCounter(value: string) { + const integer = BigInt(value) + must(integer <= BigInt(Number.MAX_SAFE_INTEGER), "Product replay requires exact JavaScript integer counters") + return Number(integer) +} + +export function prepareChrReplay(wire: ReturnType) { + const decoded = decodeOwnedIpfix(wire) + const exporters = wire.scope.sources.map((address, index) => { + const frames = wire.frames.filter(frame => frame.exporter === index) + const first = frames[0] + must(first && frames.every(frame => frame.sourcePort === first.sourcePort && frame.version === first.version && frame.domain === first.domain), "Exporter session changed") + const ids = new Set(decoded.frames.flatMap((frame, ordinal) => wire.frames[ordinal].exporter === index ? frame.templateIds : [])) + must(ids.size === 1, "Replay requires one pinned template ID per exporter") + return { address, port: first.sourcePort, version: first.version, domain: first.domain, templateId: [...ids][0] } + }) + const phase = [0, 0] + const frames = wire.frames.map((frame, index) => { + const { flows, templateIds } = decoded.frames[index] + let kind = "data" + if (templateIds.length) kind = phase[frame.exporter]++ === 0 ? "template" : "refresh" + const expected = flows.map(({ src, dst, bytes, packets, inIface, outIface }) => + ({ src, dst, bytes: replayCounter(bytes), packets: replayCounter(packets), inIface, outIface })) + return { exporter: frame.exporter, ordinal: frame.ordinal, receivedMs: frame.receivedMs, payloadHex: frame.payloadHex, kind, expected } + }) + return { decoded, projection: { destination: wire.scope.destination, exporters, frames } } +} + +export async function replayRecordedChrEvidence(bytes: Buffer, expectedSha: string, rawEvidence: unknown) { + const evidence = evidenceSchema.parse(rawEvidence) + const wire = projectChrWire(bytes, evidence.scope) + must(wire.captureSha256 === expectedSha && evidence.captureSha256 === expectedSha, "Capture SHA-256 does not match recorded evidence") + const { decoded, projection } = prepareChrReplay(wire) + const replay = checkChrCapture(projection) + const labels = new Map() + for (const row of evidence.interfaceLabels) { + const key = row.serverId + ":" + row.index + must(evidence.serverIds.includes(row.serverId) && !labels.has(key), "Interface evidence identity differs") + labels.set(key, row.label) + } + must(evidence.talkers.every((row, index) => row.serverId === evidence.serverIds[index]), "Public evidence order or scope differs") + let snapshots = 0, publicReads = 0 + const reconciliation = await reconcileOwnedChrFlow({ + wire, decoded, serverIds: evidence.serverIds, from: evidence.from, until: evidence.until, minutes: evidence.minutes, + interfaceLabel: (id, index) => { + const label = labels.get(id + ":" + index) + must(label, "Missing independently observed interface label") + return label + }, + readOnlySnapshot: async work => { + const recorded = [evidence.before, evidence.after][snapshots++] + must(recorded, "Unexpected SQL evidence read") + return work(async sql => { + if (sql.includes("FROM flow_buckets")) return recorded.raw + must(sql.includes("FROM flow_minute_stats"), "Unexpected SQL evidence query") + return recorded.minute + }) + }, + callTool: async (tool, request) => { + const recorded = evidence.talkers[publicReads++] + must(tool === "mm_netflow_talkers_get" && recorded?.serverId === request.serverId, "Unexpected recorded MCP scope") + return recorded.result + }, + assertCurrent: async () => { /* Offline comparison of immutable recorded evidence. */ }, + }) + return { captureSha256: wire.captureSha256, replay, reconciliation, recordedEvidenceMatched: true, actualNativeAcceptance: false } +} + +async function readPinned(path: string, digest: string) { + sha.parse(digest) + const file = await open(path, "r") + try { + const info = await file.stat() + must(info.isFile() && info.size > 0 && info.size <= 512 * 1024, "Offline evidence file outside size bound") + const bytes = Buffer.alloc(info.size + 1) + let read = 0 + while (read < bytes.length) { + const { bytesRead } = await file.read(bytes, read, bytes.length - read, read) + if (!bytesRead) break + read += bytesRead + } + const body = bytes.subarray(0, read) + must(read === info.size && createHash("sha256").update(body).digest("hex") === digest, "Offline evidence hash differs") + return body + } finally { + await file.close() + } +} +async function main() { + const args = process.argv.slice(2) + must(args.length === 4, "Expected pcap and recorded evidence pins") + const [capture, captureSha, evidence, evidenceSha] = args + const [bytes, raw] = await Promise.all([readPinned(capture, captureSha), readPinned(evidence, evidenceSha)]) + console.log(JSON.stringify({ evidenceSha256: evidenceSha, ...await replayRecordedChrEvidence(bytes, captureSha, JSON.parse(raw.toString("utf8"))) })) +} +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch(() => { + console.error("Recorded CHR evidence refused") + process.exitCode = 1 + }) +} diff --git a/backend/src/test/traffic-flow-chr-owned-sequence.test.ts b/backend/src/test/traffic-flow-chr-owned-sequence.test.ts new file mode 100644 index 00000000..aef94da2 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-owned-sequence.test.ts @@ -0,0 +1,106 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { test } from "node:test" +import { bindChrCapture, executeOwnedChrCapture } from "./traffic-flow-chr-owned-sequence.js" + +type Options = Parameters[0] +const scope = { sources: ["192.0.2.1", "192.0.2.2"] as const, destination: "192.0.2.100", port: 4739 } +const sha = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex") +function fixtures(at = 1700000000000, port = scope.port) { + const pcap = Buffer.alloc(24) + pcap.writeUInt32LE(0xa1b2c3d4); pcap.writeUInt16LE(2, 4); pcap.writeUInt16LE(4, 6) + pcap.writeUInt32LE(4124, 16); pcap.writeUInt32LE(101, 20) + const sourceRows = scope.sources.map(address => ({ address, port: 2055, version: 10, domain: 7, templateId: 256 })) + const records: Buffer[] = [] + const frames = (["template", "data", "refresh", "data"] as const).flatMap(kind => sourceRows.map((_source, exporter) => { + const template = kind !== "data" + const payload = Buffer.alloc(template ? 48 : 44) + payload.writeUInt16BE(10); payload.writeUInt16BE(payload.length, 2); payload.writeUInt32BE(7, 12) + payload.writeUInt16BE(template ? 2 : 256, 16); payload.writeUInt16BE(payload.length - 16, 18) + if (template) { + payload.writeUInt16BE(256, 20); payload.writeUInt16BE(6, 22) + ;[8, 12, 1, 2, 10, 14].forEach((field, index) => { payload.writeUInt16BE(field, 24 + index * 4); payload.writeUInt16BE(4, 26 + index * 4) }) + } else { + ;[0xc0000201, 0xc6336401, 1200, 3, 11, 12].forEach((value, index) => payload.writeUInt32BE(value, 20 + index * 4)) + } + const record = Buffer.alloc(44 + payload.length), ordinal = records.length + 1 + record.writeUInt32LE(Math.floor(at / 1000)); record.writeUInt32LE(at % 1000 * 1000, 4) + record.writeUInt32LE(record.length - 16, 8); record.writeUInt32LE(record.length - 16, 12) + const ip = record.subarray(16) + ip[0] = 0x45; ip.writeUInt16BE(ip.length, 2); ip[9] = 17 + Buffer.from([192, 0, 2, exporter + 1, 192, 0, 2, 100]).copy(ip, 12) + ip.writeUInt16BE(2055, 20); ip.writeUInt16BE(port, 22); ip.writeUInt16BE(ip.length - 20, 24) + payload.copy(ip, 28) + records.push(record) + return { kind, ordinal, receivedMs: at, exporter, payloadHex: payload.toString("hex"), + expected: template ? [] : [{ src: "192.0.2.1", dst: "198.51.100.1", bytes: 1200, packets: 3, inIface: "11", outIface: "12" }] } + })) + const bytes = Buffer.concat([pcap, ...records]) + return { bytes, sha256: sha(bytes), dropped: 0, complete: true, projection: { destination: scope.destination, exporters: sourceRows, frames } } +} + +test("all reviewed datagrams are bound to the original ordered pcap bytes before replay", () => { + const capture = fixtures() + assert.equal(bindChrCapture(capture, scope).wire.frames.length, 8) + assert.throws(() => bindChrCapture({ ...capture, dropped: 1 }, scope), /completeness/) + assert.throws(() => bindChrCapture({ ...capture, sha256: "0".repeat(64) }, scope), /pin/) + const changed = structuredClone(capture.projection) + changed.frames[2].payloadHex = "ff" + changed.frames[2].payloadHex.slice(2) + assert.throws(() => bindChrCapture({ ...capture, projection: changed }, scope), /differs from wire/) +}) + +test("owned journal is synchronized before target dispatch and captures target cleanup before reconciliation", async () => { + const directory = await mkdtemp(join(tmpdir(), "mm-chr-sequence-")) + const journalPath = join(directory, "owned.jsonl") + try { + const capture = fixtures(Date.now() - 1000, 4740), events: string[] = [] + let bound = true + const device = (deviceId: string): Options["targets"][0] => { + const baseline = { deviceId, flow: { enabled: "true" }, targets: [], + ipfix: Object.fromEntries(["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", "dst-port", "in-interface", "out-interface"].map(key => [key, true])) } + let rows: Record[] = [] + const intentSaved = async (action: string) => { + const records = (await readFile(journalPath, "utf8")).trim().split("\n").map(line => JSON.parse(line)) + assert(records.some(row => row.action === action && row.phase === "intent" && row.deviceId === deviceId)) + } + return { baseline, read: async () => ({ ...baseline, targets: rows }), native: { + async put(_path: string, body: Record, _timeout?: number, fence?: () => Promise) { + await fence?.(); await intentSaved("create"); events.push("create:" + deviceId) + rows = [{ ...body, ".id": "*A", disabled: false }] + return { ".id": "*A" } as T + }, + async delete(_path, _timeout, fence) { await fence?.(); await intentSaved("delete"); rows = []; events.push("delete:" + deviceId) }, + } } + } + const options: Options = { + journalPath, deadlineAt: Date.now() + 5000, scope, ingress: { bytes: capture.bytes, sha256: capture.sha256, existingPort: 4740 }, + serverIds: [1, 2], targets: [device("A"), device("B")], + collector: { authorize: async () => {}, assertBound: async () => { assert(bound) }, restore: async () => { bound = false; events.push("restore") } }, + capture: async run => { events.push("start"); await run(); events.push("stop"); return fixtures(Date.now()) }, + exercise: async () => { events.push("exercise") }, + reconcile: async (wire, ids) => { assert.equal(bound, false); assert.equal(wire.frames.length, 8); assert.deepEqual(ids, [1, 2]); events.push("reconcile") }, + } + await assert.rejects(executeOwnedChrCapture({ ...options, ingress: { ...options.ingress, sha256: "0".repeat(64) } }), /ingress SHA/) + assert.deepEqual(events, []) + assert.equal((await executeOwnedChrCapture(options)).nativeAcceptance, false) + assert.deepEqual(events, ["start", "create:A", "create:B", "exercise", "delete:B", "delete:A", "stop", "restore", "reconcile"]) + await assert.rejects(executeOwnedChrCapture(options), { code: "EEXIST" }) + assert.equal(events.length, 9) + bound = true + await assert.rejects(executeOwnedChrCapture({ + ...options, journalPath: join(directory, "aborted.jsonl"), + capture: async run => { + const owner = new AbortController() + owner.abort() + await run(owner.signal) + return fixtures(Date.now()) + }, + }), /ownership ended/) + assert.equal(events.length, 9) + } finally { + await rm(directory, { recursive: true, force: true }) + } +}) diff --git a/backend/src/test/traffic-flow-chr-owned-sequence.ts b/backend/src/test/traffic-flow-chr-owned-sequence.ts new file mode 100644 index 00000000..c47da919 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-owned-sequence.ts @@ -0,0 +1,135 @@ +/** Local composition for a future guarded runner. Callbacks are trusted runner + * boundaries, never populated from a request or evidence JSON. */ +import { open } from "node:fs/promises" +import { dirname } from "node:path" +import { createChrTargetHarness } from "./traffic-flow-chr-target-harness.js" +import { projectChrWire } from "./traffic-flow-chr-wire.js" +import { checkChrCapture } from "./traffic-flow-chr-capture-check.js" + +type Target = Extract[0], { stagedDisabled?: false }> +type Scope = Parameters[1] +type Wire = ReturnType +type Projection = { + destination: string + exporters: readonly { address: string; port: number; version: number; domain: number }[] + frames: readonly { exporter: number; ordinal: number; receivedMs: number; payloadHex: string }[] +} +type Capture = { bytes: Buffer; sha256: string; dropped: number; complete: boolean; projection: Projection } +type Ingress = { bytes: Buffer; sha256: string; existingPort: number } +function must(value: unknown, reason: string): asserts value { + if (!value) throw Error(reason) +} + +export function checkChrBaselineIngress(input: Ingress, scope: Scope, now = Date.now()) { + const wire = projectChrWire(input.bytes, { sources: scope.sources, destination: scope.destination, port: input.existingPort }) + must(wire.captureSha256 === input.sha256, "Baseline ingress SHA-256 differs") + must(wire.frames.every(frame => frame.receivedMs >= now - 10 * 60_000 && frame.receivedMs <= now), + "Baseline ingress is outside the current evidence window") + return wire +} + +/** Bind every reviewed row to its original wire record and require complete + * capture coverage for conservation. Sample-only replay uses the separate CLI. */ +export function bindChrCapture(capture: Capture, scope: Scope) { + const wire = projectChrWire(capture.bytes, scope) + const { projection } = capture + must(wire.captureSha256 === capture.sha256 && capture.dropped === 0 && capture.complete === true, + "Capture provenance pin or completeness differs") + must(projection.destination === scope.destination && projection.exporters.length === 2 && + projection.frames.length === wire.frames.length, "Projection scope or coverage differs") + for (const [index, frame] of wire.frames.entries()) { + const row = projection.frames[index], exporter = projection.exporters[frame.exporter] + must(row && row.ordinal === frame.ordinal && row.receivedMs === frame.receivedMs && + row.exporter === frame.exporter && row.payloadHex === frame.payloadHex && + exporter?.address === scope.sources[frame.exporter] && exporter.port === frame.sourcePort && + exporter.version === frame.version && exporter.domain === frame.domain, "Projection differs from wire record") + } + return { wire, replay: checkChrCapture(projection) } +} + +type Options = { + journalPath: string + deadlineAt: number + scope: Scope + ingress: Ingress + serverIds: readonly [number, number] + targets: readonly [Omit, Omit] + collector: { authorize: () => Promise; assertBound: () => Promise; restore: () => Promise } + capture: (exercise: (signal?: AbortSignal) => Promise) => Promise + exercise: () => Promise + reconcile: (wire: Wire, serverIds: readonly [number, number]) => Promise +} + +export async function executeOwnedChrCapture(options: Options) { + const scope = structuredClone(options.scope), serverIds = Object.freeze([...options.serverIds]) as readonly [number, number] + const deadlineAt = options.deadlineAt + let active = true, captureSignal: AbortSignal | undefined + const live = () => must(active && !captureSignal?.aborted && Number.isSafeInteger(deadlineAt) && Date.now() < deadlineAt, "Owned capture deadline or ownership ended") + live() + must(deadlineAt - Date.now() <= 10 * 60_000 && serverIds.length === 2 && new Set(serverIds).size === 2 && + serverIds.every(id => Number.isSafeInteger(id) && id > 0) && options.targets.length === 2 && + options.targets[0].baseline.deviceId !== options.targets[1].baseline.deviceId, "Invalid owned capture identity") + const ingress = checkChrBaselineIngress(options.ingress, scope) + const file = await open(options.journalPath, "wx", 0o600) + const append = async (record: object) => { + live() + await file.writeFile(JSON.stringify(record) + "\n") + await file.sync() + live() + } + const authorize = async () => { + live() + await options.collector.authorize() + await options.collector.assertBound() + await options.collector.authorize() + live() + } + try { + await authorize() + await append({ phase: "admitted", scope, serverIds, ingressSha256: ingress.captureSha256, devices: options.targets.map(target => target.baseline.deviceId) }) + if (process.platform !== "win32") { + const directory = await open(dirname(options.journalPath), "r") + try { await directory.sync() } finally { await directory.close() } + } + const targets = options.targets.map((target, index) => createChrTargetHarness({ + ...target, source: scope.sources[index], destination: scope.destination, port: scope.port, authorize, + checkpoint: entry => append({ ...entry, serverId: serverIds[index], deviceId: target.baseline.deviceId }), + })) + await authorize() + let started = false, exercised = false + const captureStartedAt = Date.now() + const capture = await options.capture(async signal => { + must(!started, "Capture exercise was already attempted") + started = true + captureSignal = signal + await authorize() + for (const target of targets) await target.create() + await authorize() + await options.exercise() + await authorize() + for (const target of [...targets].reverse()) await target.remove() + exercised = true + }) + const captureEndedAt = Date.now() + must(exercised, "Capture omitted or did not complete its exercise") + captureSignal = undefined + await authorize() + await append({ phase: "collector-restore-intent" }) + await options.collector.authorize() + live() + await options.collector.restore() + await append({ phase: "collector-restored" }) + const { wire, replay } = bindChrCapture(capture, scope) + must(wire.captureSha256 !== ingress.captureSha256 && wire.frames.every(frame => frame.receivedMs >= captureStartedAt && frame.receivedMs <= captureEndedAt), + "Capture does not belong to the owned exercise window") + await append({ phase: "wire-bound", captureSha256: wire.captureSha256, frames: wire.frames.length }) + await options.collector.authorize() + live() + await options.reconcile(wire, serverIds) + await append({ phase: "reconciled" }) + return { replay, captureSha256: wire.captureSha256, componentSequenceComplete: true, nativeAcceptance: false } + } finally { + active = false + await file.close() + } +} diff --git a/backend/src/test/traffic-flow-chr-owned-wg-evidence.ts b/backend/src/test/traffic-flow-chr-owned-wg-evidence.ts new file mode 100644 index 00000000..fd9c2aa2 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-owned-wg-evidence.ts @@ -0,0 +1,97 @@ +import { decodeOwnedIpfix } from "./traffic-flow-chr-independent-ipfix.js" +import { projectChrWire } from "./traffic-flow-chr-wire.js" +import { ownedWgFastPathFields as fp, ownedWgPacketFields as packets } from "./traffic-flow-chr-interface-counters.js" +import { isChrWireGuardInterfaceType } from "./traffic-flow-chr-transit-fixture.js" + +type Scope = Parameters[1] +type Counters = { startedAt: number; observedAt: number; rows: readonly Record[] } +type Interface = { id: string; name: string; ifIndex: string } +type Expected = { exporter: 0 | 1; src: string; dst: string; srcPort: number; dstPort: number; + inIface: string; outIface: string; packets: number; phase?: "initial" | "refreshed" } +function must(value: unknown, reason: string): asserts value { if (!value) throw Error(reason) } +function counter(row: Record, name: string) { + const value = row[name] + must(typeof value === "string" && /^(0|[1-9][0-9]{0,19})$/.test(value), "Owned interface counter unavailable") + return BigInt(value) +} + +/** Requires real before/after counters from the runner's pinned native reader + * and original pcap bytes. Artifact agreement alone confers no live authority. */ +export function verifyChrOwnedWgEvidence(options: { + bytes: Buffer; sha256: string; complete: boolean; dropped: number; scope: Scope + exporters: readonly [{ interfaces: readonly [Interface, Interface]; before: Counters; after: Counters }, + { interfaces: readonly [Interface, Interface]; before: Counters; after: Counters }] + expected: readonly Expected[] + requireRefresh?: boolean + counterWindow?: "traffic" +}) { + const { scope, expected, exporters } = options + const wire = projectChrWire(options.bytes, scope) + must(options.complete === true && options.dropped === 0 && options.sha256 === wire.captureSha256, "Owned capture provenance differs") + const decoded = decodeOwnedIpfix(wire) + must(options.counterWindow !== "traffic" || options.requireRefresh, "Staged traffic counters require refreshed data") + const matches = expected.map(() => 0n) + must(expected.length >= 2 && expected.length <= 16 && [0, 1].every(exporter => expected.some(flow => flow.exporter === exporter)) && + expected.every(flow => Number.isSafeInteger(flow.packets) && flow.packets > 0), "Expected owned flow scope is incomplete") + const sessions = new Map() + if (options.requireRefresh) must([0, 1].every(exporter => + ["initial", "refreshed"].every(phase => expected.some(pin => pin.exporter === exporter && pin.phase === phase))), + "Both exporters need initial and refreshed flow pins") + for (const [index, { flows, templateIds }] of decoded.frames.entries()) { + const { exporter, sourcePort, domain } = wire.frames[index] + if (options.requireRefresh) { + const key = sourcePort + "/" + domain + const session = sessions.get(exporter) + must(!session || session.key === key, "Owned exporter session changed") + if (templateIds.length) { + must(templateIds.length === 1 && (!session || session.templateId === templateIds[0]), "Owned template identity changed") + sessions.set(exporter, { key, templateId: templateIds[0], phase: session ? "refreshed" : "initial" }) + } + } + for (const flow of flows) { + const matching = expected.map((pin, offset) => ({ pin, offset })).filter(({ pin }) => + pin.exporter === exporter && pin.src === flow.src && pin.dst === flow.dst && pin.srcPort === flow.srcPort && + pin.dstPort === flow.dstPort && pin.inIface === flow.inIface && pin.outIface === flow.outIface && flow.proto === 17 && + (!options.requireRefresh || sessions.get(exporter)?.phase === pin.phase)) + must(matching.length === 1, "Unscoped or ambiguous owned IPFIX flow") + matches[matching[0].offset] += BigInt(flow.packets) + } + } + must(expected.every((pin, offset) => matches[offset] === BigInt(pin.packets)), "Owned IPFIX packet totals differ") + for (const [index, { before, after, interfaces }] of exporters.entries()) { + const exporterFlows = expected.filter(flow => flow.exporter === index) + // A staged target can emit a template as soon as it is enabled. Only + // record-bearing frames are relevant to the forwarding counter interval. + const timestamps = wire.frames.filter((frame, offset) => frame.exporter === index && + (options.counterWindow !== "traffic" || decoded.frames[offset].flows.length > 0)).map(frame => frame.receivedMs) + must([before.startedAt, before.observedAt, after.startedAt, after.observedAt].every(Number.isSafeInteger) && + before.startedAt <= before.observedAt && after.startedAt <= after.observedAt && + timestamps.length > 0 && before.observedAt <= Math.min(...timestamps) && after.startedAt >= Math.max(...timestamps) && + after.observedAt - before.startedAt <= 180000 && + interfaces[0].id !== interfaces[1].id && interfaces[0].name !== interfaces[1].name && + interfaces[0].ifIndex !== interfaces[1].ifIndex && + interfaces.every(pin => /^\*[a-f0-9]{1,16}$/i.test(pin.id) && /^nf[a-f0-9]{8}(ea|ma|mb|eb)$/.test(pin.name) && + /^[1-9][0-9]{0,9}$/.test(pin.ifIndex)) && + exporterFlows.every(flow => flow.inIface !== flow.outIface && + interfaces.some(pin => pin.ifIndex === flow.inIface) && interfaces.some(pin => pin.ifIndex === flow.outIface)), + "Owned counter window or interface scope differs") + for (const pin of interfaces) { + const read = (sample: Counters) => { + const rows = sample.rows.filter(row => row[".id"] === pin.id || row.name === pin.name) + must(rows.length === 1 && rows[0][".id"] === pin.id && rows[0].name === pin.name && isChrWireGuardInterfaceType(rows[0].type) && + rows[0].ifindex === pin.ifIndex, + "Owned interface counter identity differs") + return rows[0] + } + const old = read(before), current = read(after) + must(fp.every(name => counter(old, name) === counter(current, name)), "Owned interface FastPath counter changed") + for (const [ifaceField, counterField] of [["inIface", packets[0]], ["outIface", packets[1]]] as const) { + const exported = exporterFlows.reduce((sum, flow) => flow[ifaceField] === pin.ifIndex ? sum + BigInt(flow.packets) : sum, 0n) + const delta = counter(current, counterField) - counter(old, counterField) + must(delta > 0n && delta >= exported, "Owned interface packet delta does not corroborate matched IPFIX packets") + } + } + } + return { captureSha256: wire.captureSha256, exactIpfixMatched: true as const, + ownedFastPathDeltasZero: true as const, nativeAcceptance: false as const } +} diff --git a/backend/src/test/traffic-flow-chr-parent-admission-socket.ts b/backend/src/test/traffic-flow-chr-parent-admission-socket.ts new file mode 100644 index 00000000..e78c3bbd --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-admission-socket.ts @@ -0,0 +1,127 @@ +import { fstatSync } from "node:fs" +import { Socket } from "node:net" +import type { Duplex } from "node:stream" +import { createChrParentOrchestration } from "./traffic-flow-chr-parent-orchestration.js" + +type Parent = Awaited> + +/** The root launcher inherits one private socketpair end from its credential + * verifying AF_UNIX acceptor. The collector's pathname never reaches Node. */ +function inheritedSocket(descriptor: number) { + if (process.platform !== "linux" || process.geteuid?.() !== 0 || + !Number.isSafeInteger(descriptor) || descriptor < 3 || descriptor > 1024 || + !fstatSync(descriptor).isSocket()) { + throw Error("Inherited parent CHR admission socket refused") + } + return new Socket({ fd: descriptor, readable: true, writable: true, allowHalfOpen: true }) +} + +export async function serveChrParentInheritedAdmission(parent: Parent, descriptor: number) { + let socket: Socket | undefined + try { + socket = inheritedSocket(descriptor) + return await parent.serve({ input: socket, output: socket }) + } finally { + socket?.destroy() + parent.retire() + } +} + +/** Ask the host owner to recheck actual bridge milestones. This channel is + * separate from the collector connection and has no mutation operations. */ +export function openChrParentHostPhaseChecks(descriptor: number, deadlineAt: number, staged = false) { + return openChrParentConnectedPhaseChecks(inheritedSocket(descriptor), deadlineAt, staged) +} + +export function openChrParentConnectedPhaseChecks(socket: Duplex, deadlineAt: number, staged = false) { + socket.on("error", () => {}) + const responses = socket[Symbol.asyncIterator]() + let sequence = 0, pending = false + const check = async (phase: "preflight" | "stage" | "ready" | "drain" | "inverse" | "timer") => { + if (pending || socket.destroyed || ++sequence > (staged ? 99 : 91) || Date.now() >= deadlineAt) { + socket.destroy() + throw Error("Host phase query refused") + } + pending = true + const timer = setTimeout(() => socket.destroy(), Math.min(8000, deadlineAt - Date.now())) + try { + const expected = sequence + ":ok" + await new Promise((resolve, reject) => { + socket.write(sequence + ":" + phase + "\n", error => error ? reject(error) : resolve()) + }) + let received = Buffer.alloc(0) + while (received.at(-1) !== 10) { + const { value, done } = await responses.next() + if (done || !Buffer.isBuffer(value) || received.length + value.length > 64) throw Error("Host phase query refused") + received = Buffer.concat([received, value]) + } + const text = received.toString("ascii") + if (phase !== "timer") { + if (text !== expected + "\n") throw Error("Host phase query refused") + return + } + const deadline = text.startsWith(expected + ":") ? text.slice(expected.length + 1) : "" + const value = Number(deadline.trim()) + if (!/^[1-9][0-9]{12}\n$/.test(deadline) || !Number.isSafeInteger(value) || value <= Date.now() || + value > Math.min(deadlineAt, Date.now() + 240000)) throw Error("Host capture deadline refused") + return value + } catch (error) { socket.destroy(); throw error } + finally { clearTimeout(timer); pending = false } + } + const fence = async (phase: Exclude[0], "timer">) => { await check(phase) } + return Object.freeze({ + assertTimingPreflight: () => fence("preflight"), + ...(staged ? { assertTransitAndCollectorStagingReady: () => fence("stage") } : {}), + assertTransitAndCollectorReady: () => fence("ready"), + assertCaptureAndDrainComplete: () => fence("drain"), + assertLinuxInverseReady: () => fence("inverse"), + async captureDeadline() { + if (!staged) throw Error("Staged host capture deadline unavailable") + const result = await check("timer") + if (result === undefined) throw Error("Staged host capture deadline unavailable") + return result + }, + close: () => socket.destroy(), + }) +} + + + + +type HostCheck = keyof Omit, "close" | "captureDeadline"> + +/** The host launches one root Node with two private descriptors. Call serve + * after opening the CHR relay with the returned hooks and sealing its pins. */ +export async function openChrParentAdmissionTransport( + options: Omit[0], HostCheck>, + descriptors: { admission: number; phase: number }, +) { + if (descriptors.admission === descriptors.phase) throw Error("Parent admission descriptors overlap") + const socket = inheritedSocket(descriptors.admission) + socket.on("error", () => {}) + let phases: ReturnType | undefined + let parent: Parent | undefined + try { + phases = openChrParentHostPhaseChecks(descriptors.phase, options.deadlineAt, !!options.stagedTarget) + parent = await createChrParentOrchestration({ ...options, ...phases }) + const owner = parent + const close = () => { owner.retire(); socket.destroy(); phases?.close() } + return Object.freeze({ + authorizeWrite: owner.authorizeWrite, + recordWriteAcknowledgement: owner.recordWriteAcknowledgement, + async serve() { + try { return await owner.serve({ input: socket, output: socket }) } + finally { close() } + }, + close, + }) + } catch (error) { + parent?.retire() + socket.destroy() + phases?.close() + throw error + } +} + + + diff --git a/backend/src/test/traffic-flow-chr-parent-launch.test.ts b/backend/src/test/traffic-flow-chr-parent-launch.test.ts new file mode 100644 index 00000000..d1aa2ea0 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-launch.test.ts @@ -0,0 +1,151 @@ +import assert from "node:assert/strict" +import { spawnSync } from "node:child_process" +import { createHash } from "node:crypto" +import type { Socket } from "node:net" +import { Duplex, PassThrough } from "node:stream" +import { test } from "node:test" +import { fileURLToPath } from "node:url" +import { chrTransitNames } from "./traffic-flow-chr-transit-fixture.js" +import { openChrParentConnectedPhaseChecks } from "./traffic-flow-chr-parent-admission-socket.js" +import { awaitChrParentStart, parseChrParentLaunchPins } from "./traffic-flow-chr-parent-launch.js" +import { serveChrParentNativeBoundary } from "./traffic-flow-chr-parent-native-boundary.js" +import type { ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" + +const runId = "abcdef98", now = 1793865000000 + +test("parent waits for exact fragmented startup barrier without consuming following protocol bytes", async () => { + const input = new PassThrough() + const pending = awaitChrParentStart(input as unknown as Socket, Date.now() + 1000) + input.write("MM-NETFLOW-") + input.write("PARENT-READY\nnext") + await pending + assert.equal(input.read().toString(), "next") + input.destroy() + + const bad = new PassThrough() + const refused = awaitChrParentStart(bad as unknown as Socket, Date.now() + 1000) + bad.end("MM-NETFLOW-PARENT-WRONG\n") + await assert.rejects(refused, /startup refused/) +}) + +test("the actual parent entry imports without loading the application database or its drivers", () => { + const entry = new URL("./traffic-flow-chr-parent-launch.ts", import.meta.url) + const child = spawnSync(process.execPath, ["--import", "tsx", "--input-type=module", "-e", ` + import { registerHooks } from "node:module" + registerHooks({ load(url, context, next) { + if (/(?:\\/src\\/db\\/|node_modules\\/(?:pg|drizzle-orm)\\/)/.test(url)) { + throw Error("Unexpected application database import") + } + return next(url, context) + } }) + await import(${JSON.stringify(entry.href)}) + `], { cwd: fileURLToPath(new URL("../../", import.meta.url)), timeout: 10000, encoding: "utf8" }) + assert.equal(child.error, undefined) + assert.equal(child.status, 0, child.stderr) +}) +const ports = Object.fromEntries(["linuxA", "edgeA", "middleA", "middleB", "edgeB", "linuxB", "collector", "calibrationSource", + "calibrationDestination", "phase1Source", "phase1Destination", "phase2Source", "phase2Destination"].map((name, index) => [name, 50100 + index])) +const pin = (i: number) => ({ device: "8", inode: String(i) }) +const directions = ["A-to-B", "B-to-A", "export-A", "export-B", "underlay-linuxA-to-A", "underlay-A-to-linuxA", + "underlay-A-to-B", "underlay-B-to-A", "underlay-B-to-linuxB", "underlay-linuxB-to-B"] +const pins = { + runId, deadlineAt: now + 500000, hostNamespace: pin(1), ports, + isolation: { checkedPorts: Object.values(ports), checkedNames: Object.values(chrTransitNames(runId)), + checkedCidrs: ["198.18.240.1/32", "198.18.240.2/32", "198.18.241.0/30", "198.18.241.4/30", "198.18.241.8/30", "198.18.242.0/29"], + occupiedPorts: [], overlappingCidrs: [], occupiedNames: [], + policies: directions.map(direction => ({ direction, verdict: "allow", nat: "none", fastTrack: "none", ambiguousRules: 0 })) }, + controlSockets: { admission: pin(2), phase: pin(3), native: pin(4) }, readSockets: { A: pin(5), B: pin(6) }, + routers: { A: { deviceId: "A", identity: "a", version: "7.24.5" }, B: { deviceId: "B", identity: "b", version: "7.24.5" } }, + linux: { endpoint: "161.104.46.222", publicKeys: { linuxA: "A".repeat(43) + "=", linuxB: "B".repeat(43) + "=" } }, + readPassword: "synthetic-fixture-only", +} +function parse(value: unknown) { + const data = Buffer.from(JSON.stringify(value)) + return parseChrParentLaunchPins(data, createHash("sha256").update(data).digest("hex"), now) +} + +test("parent manifest caps the longer lease to the explicit sealed staged profile", () => { + const captureProfile = { kind: "staged-disabled-target", captureMs: 205000, requiredCollectorWindowMs: 450000 } + assert.throws(() => parse({ ...pins, deadlineAt: now + 600001 })) + assert.equal(parse({ ...pins, deadlineAt: now + 3600000, captureProfile }).pins.deadlineAt, now + 3600000) + assert.throws(() => parse({ ...pins, deadlineAt: now + 3600001, captureProfile })) + assert.throws(() => parse({ ...pins, deadlineAt: now + 1200000, captureProfile: { ...captureProfile, timingAccepted: true } })) + for (const requiredCollectorWindowMs of [undefined, 415000, 600000]) { + assert.throws(() => parse({ ...pins, deadlineAt: now + 1200000, captureProfile: { ...captureProfile, requiredCollectorWindowMs } })) + } +}) + +test("root parent pins require exact fixture, authority duration and distinct private socket identities", () => { + assert.equal(parse(pins).scope[0], runId) + assert.throws(() => parse({ ...pins, extra: "unknown" })) + assert.throws(() => parse({ ...pins, deadlineAt: now + 600001 })) + assert.throws(() => parse({ ...pins, readSockets: { A: pin(5), B: pin(4) } })) + assert.throws(() => parse({ ...pins, ports: { ...ports, linuxA: ports.linuxB } })) + assert.throws(() => parseChrParentLaunchPins(Buffer.from(JSON.stringify(pins)), "0".repeat(64), now)) +}) + +function boundary() { + const input = new PassThrough(), output = new PassThrough() + const stream = Duplex.from({ readable: input, writable: output }) + const intent: ChrWriteIntent = { step: "target:A:enable", side: "A", deviceId: "fixture-A", method: "PATCH", + path: "/ip/traffic-flow/target/*5", body: { disabled: "no" } } + const native = { side: intent.side, method: intent.method, path: intent.path } + const frames: unknown[] = [], acks: unknown[] = [] + output.on("data", chunk => frames.push(JSON.parse(chunk.toString()))) + const done = serveChrParentNativeBoundary(stream, { + async authorizeWrite(input) { assert.deepEqual(input, { ...native, body: intent.body }); return intent }, + recordWriteAcknowledgement(ack) { acks.push(ack) }, + }, async () => {}) + void done.catch(() => {}) + const write = (value: object) => input.write(JSON.stringify(value) + "\n") + return { intent, native, frames, acks, done, write, close: () => input.end() } +} + +test("native boundary uses one prior gate grant and only a bounded ACK projection", async () => { + const f = boundary() + f.write({ sequence: 1, op: "consume", ...f.native, body: f.intent.body }) + f.write({ sequence: 2, op: "ack", ...f.native, status: 200, id: "*5" }) + f.close() + await assert.rejects(f.done, /native boundary refused/) + assert.deepEqual(f.frames, [{ sequence: 1, ok: true }, { sequence: 2, ok: true }]) + assert.deepEqual(f.acks, [{ intent: f.intent, status: 200, body: { ".id": "*5" } }]) +}) + +test("native boundary refuses ACK without grant, wrong path, omitted ID or child-provided intent", async () => { + for (const variant of ["early", "path", "id", "intent"]) { + const f = boundary() + if (variant !== "early") f.write({ sequence: 1, op: "consume", ...f.native, body: f.intent.body }) + f.write({ sequence: variant === "early" ? 1 : 2, op: "ack", ...f.native, status: 200, + ...(variant === "path" ? { path: "/ip/traffic-flow/target/*6" } : {}), + ...(variant === "id" ? {} : { id: "*5" }), + ...(variant === "intent" ? { intent: f.intent } : {}), + }) + f.close() + await assert.rejects(f.done, /native boundary refused/) + assert.deepEqual(f.acks, []) + assert.equal(f.frames.length, variant === "early" ? 0 : 1) + } +}) + + +test("private host phase timer accepts only exact bounded deadline replies", async () => { + for (const offset of [100000, 240001]) { + const incoming = new PassThrough(), outgoing = new PassThrough() + const stream = Duplex.from({ readable: incoming, writable: outgoing }) + const deadlineAt = Date.now() + offset + outgoing.on("data", bytes => { + const text = bytes.toString() + if (text === "1:preflight\n") incoming.write("1:ok\n") + else if (text === "2:timer\n") { + incoming.write("2:ok:") + incoming.write(String(deadlineAt) + "\n") + } + }) + const phases = openChrParentConnectedPhaseChecks(stream, Date.now() + 230000, true) + try { + await phases.assertTimingPreflight() + if (offset === 100000) assert.equal(await phases.captureDeadline(), deadlineAt) + else await assert.rejects(phases.captureDeadline(), /capture deadline refused/) + } finally { phases.close() } + } +}) diff --git a/backend/src/test/traffic-flow-chr-parent-launch.ts b/backend/src/test/traffic-flow-chr-parent-launch.ts new file mode 100644 index 00000000..df6fd9c4 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-launch.ts @@ -0,0 +1,254 @@ +import { createHash } from "node:crypto" +import { constants } from "node:fs" +import { lstat, open, realpath } from "node:fs/promises" +import { request } from "node:http" +import { isDeepStrictEqual } from "node:util" +import { createConnection, type Socket } from "node:net" +import { fileURLToPath } from "node:url" +import { z } from "zod" +import type { MikrotikClient } from "../services/mikrotik.js" +import { openChrParentConnectedPhaseChecks } from "./traffic-flow-chr-parent-admission-socket.js" +import { serveChrParentNativeBoundary } from "./traffic-flow-chr-parent-native-boundary.js" +import { createChrParentOrchestration } from "./traffic-flow-chr-parent-orchestration.js" +import { readChrLinuxNamespaceIdentity } from "./traffic-flow-chr-linux-udp-owner.js" +import { chrNamespaceRelayOperation } from "./traffic-flow-chr-namespace-relay-policy.js" +import { planChrTransitFixture } from "./traffic-flow-chr-transit-fixture.js" +import { chrFixtureDeadlineMaxMs } from "./traffic-flow-chr-target-timing-profile.js" +import { chrStagedCaptureProfileSchema } from "./traffic-flow-chr-staged-profile.js" + +const parentDirectory = "/run/mm-netflow-parent" +const readDirectory = "/run/mm-netflow-parent-relay" +const pinsPath = "/run/mm-netflow-parent-pins.json" +const endpoints = { A: "194.226.121.150", B: "104.165.244.7" } as const +const token = z.string().regex(/^[1-9][0-9]{0,19}$/) +const pinSchema = z.object({ device: token, inode: token }).strict() +const name = z.string().min(1).max(128).regex(/^[^\u0000-\u001f\u007f]+$/) +const key = z.string().regex(/^[A-Za-z0-9+/]{43}=$/) +const router = z.object({ deviceId: name, identity: name, version: name }).strict() +const port = z.number().int().min(49152).max(65535) +const isolation = z.object({ + checkedPorts: z.array(port).length(13), checkedCidrs: z.array(name).length(6), checkedNames: z.array(name).max(64), + occupiedPorts: z.array(port).length(0), overlappingCidrs: z.array(name).length(0), occupiedNames: z.array(name).length(0), + policies: z.array(z.object({ direction: name, verdict: z.literal("allow"), nat: z.literal("none"), + fastTrack: z.literal("none"), ambiguousRules: z.literal(0) }).strict()).length(10), +}).strict() +const schema = z.object({ + runId: z.string().regex(/^[a-f0-9]{8}$/), deadlineAt: z.number().int().positive().safe(), hostNamespace: pinSchema, + captureProfile: chrStagedCaptureProfileSchema.optional(), + controlSockets: z.object({ admission: pinSchema, phase: pinSchema, native: pinSchema }).strict(), + readSockets: z.object({ A: pinSchema, B: pinSchema }).strict(), routers: z.object({ A: router, B: router }).strict(), + ports: z.record(z.string(), port), isolation, + linux: z.object({ endpoint: z.literal("161.104.46.222"), publicKeys: z.object({ linuxA: key, linuxB: key }).strict() }).strict(), + readPassword: z.string().regex(/^[^\u0000-\u001f\u007f]{1,512}$/u), +}).strict() +type Pin = z.infer +type Scope = Parameters +type Side = "A" | "B" +const fail = (): never => { throw Error("Pinned NetFlow parent refused. Automatic replay forbidden.") } + +export function parseChrParentLaunchPins(raw: Buffer, digest: string, now = Date.now()) { + if (!/^[a-f0-9]{64}$/.test(digest) || !raw.length || raw.length > 65536 || + createHash("sha256").update(raw).digest("hex") !== digest) return fail() + const pins = schema.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(raw))) + const sockets = [...Object.values(pins.controlSockets), ...Object.values(pins.readSockets)] + if (pins.deadlineAt <= now || pins.deadlineAt - now > chrFixtureDeadlineMaxMs(pins.captureProfile?.kind) || + pins.routers.A.deviceId === pins.routers.B.deviceId || + pins.routers.A.identity === pins.routers.B.identity || + new Set(sockets.map(pin => pin.device + ":" + pin.inode)).size !== sockets.length) return fail() + const scope: Scope = [pins.runId, pins.ports as Scope[1], pins.isolation as Scope[2]] + planChrTransitFixture(...scope) + return { pins, scope } +} + +async function rootDirectory(path: string) { + const row = await lstat(path, { bigint: true }) + if (!row.isDirectory() || row.uid !== 0n || row.gid !== 0n || + (row.mode & 0o777n) !== 0o700n || await realpath(path) !== path) return fail() + return { device: String(row.dev), inode: String(row.ino) } +} + +async function socketFence(directory: string, expectedDirectory: Pin, basename: string, expected: Pin) { + if (!isDeepStrictEqual(await rootDirectory(directory), expectedDirectory)) return fail() + const path = directory + "/" + basename + const row = await lstat(path, { bigint: true }) + if (!row.isSocket() || row.uid !== 0n || row.gid !== 0n || (row.mode & 0o777n) !== 0o600n || + String(row.dev) !== expected.device || String(row.ino) !== expected.inode) return fail() + return path +} + +async function pinnedFile() { + const handle = await open(pinsPath, constants.O_RDONLY | constants.O_NOFOLLOW) + try { + const row = await handle.stat() + if (!row.isFile() || row.uid !== 0 || row.gid !== 0 || (row.mode & 0o777) !== 0o400 || + row.nlink !== 1 || row.size < 1 || row.size > 65536) return fail() + const bytes = await handle.readFile() + if (bytes.length !== row.size) return fail() + return bytes + } finally { await handle.close() } +} + +async function connectPinned(path: string, current: () => Promise) { + await current() + const socket = createConnection({ path, allowHalfOpen: true }) + socket.on("error", () => {}) + try { + await new Promise((resolve, reject) => { + const timer = setTimeout(() => socket.destroy(Error("Parent connection timed out")), 2000) + socket.once("error", error => { clearTimeout(timer); reject(error) }) + socket.once("connect", () => { clearTimeout(timer); resolve() }) + }) + await current() + return socket + } catch (error) { socket.destroy(); throw error } +} + +export async function awaitChrParentStart(socket: Socket, deadlineAt: number) { + const expected = Buffer.from("MM-NETFLOW-PARENT-READY\n", "ascii") + const wait = Math.min(2000, deadlineAt - Date.now()) + if (wait <= 0) return fail() + await new Promise((resolve, reject) => { + const timer = setTimeout(refuse, wait) + function done(error?: Error) { + clearTimeout(timer) + socket.off("readable", read).off("error", refuse).off("close", refuse).off("end", refuse) + if (error) reject(error) + else resolve() + } + function refuse() { done(Error("Pinned NetFlow parent startup refused.")) } + function read() { + const raw: unknown = socket.read(expected.length) + if (raw === null) return + if (!Buffer.isBuffer(raw) || !raw.equals(expected)) return refuse() + done() + } + socket.on("readable", read).once("error", refuse).once("close", refuse).once("end", refuse) + read() + }) +} + +function makeReader(password: string, deadlineAt: number, socketPath: () => Promise): Pick { + const authorization = "Basic " + Buffer.from("admin:" + password).toString("base64") + return { + async get(path: string, timeoutMs = 8000, signal?: AbortSignal, cap = 1048576, before?: () => Promise) { + const route = chrNamespaceRelayOperation("GET", "/rest" + path) + if (route.write || route.path !== path || !Number.isSafeInteger(cap) || cap < 1 || cap > 1048576) return fail() + const pathname = await socketPath() + await before?.() + if (await socketPath() !== pathname) return fail() + const timeout = Math.min(8000, timeoutMs, deadlineAt - Date.now()) + if (!Number.isSafeInteger(timeout) || timeout <= 0) return fail() + const value = await new Promise((resolve, reject) => { + const req = request({ + socketPath: pathname, method: "GET", path: "/rest" + path, agent: false, + signal: AbortSignal.any([AbortSignal.timeout(timeout), ...(signal ? [signal] : [])]), + headers: { Authorization: authorization, Connection: "close" }, + }, res => { + let length = 0 + const chunks: Buffer[] = [] + res.once("error", reject) + res.once("aborted", () => reject(Error("Parent read incomplete"))) + res.on("data", (chunk: Buffer) => { + length += chunk.length + if (length > cap) { res.destroy(Error("Parent read exceeds bound")); return } + chunks.push(chunk) + }) + res.once("end", () => { + try { + if (!res.complete || res.statusCode !== 200 || length > cap) return fail() + resolve(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks, length)))) + } catch (error) { reject(error) } + }) + }) + req.once("error", reject) + req.end() + }) + if (await socketPath() !== pathname) return fail() + return value as T + }, + } +} + +export async function openChrParentCatalogueReaders(options: Pick< + z.infer, "readPassword" | "deadlineAt" | "readSockets" | "routers"> & { authorize(): Promise }) { + const directory = await rootDirectory(readDirectory) + const readers = Object.fromEntries((["A", "B"] as const).map(side => [side, makeReader(options.readPassword, options.deadlineAt, async () => { + await options.authorize() + return socketFence(readDirectory, directory, side === "A" ? "chr-a.sock" : "chr-b.sock", options.readSockets[side]) + })])) as Record> + const assertRouterIdentity = async (side: Side, deviceId: string) => { + const expected = options.routers[side] + if (deviceId !== expected.deviceId) return fail() + for (const [path, field, value] of [ + ["/system/identity", "name", expected.identity], ["/system/resource", "version", expected.version], + ] as const) { + const raw = await readers[side].get(path, 2000, undefined, 65536) + const row = Array.isArray(raw) ? raw.length === 1 ? raw[0] : null : raw + if (!row || typeof row !== "object" || (row as Record)[field] !== value) return fail() + } + } + return { readers, assertRouterIdentity } +} +async function run() { + if (process.platform !== "linux" || process.geteuid?.() !== 0 || process.getegid?.() !== 0 || + process.pid !== 1 || process.argv.slice(2).join() !== "--run-staged-netflow-parent") return fail() + const { pins, scope } = parseChrParentLaunchPins(await pinnedFile(), process.env.MMAPP_NETFLOW_PARENT_PINS_SHA256 ?? "") + const namespace = await readChrLinuxNamespaceIdentity() + if (isDeepStrictEqual(namespace, pins.hostNamespace)) return fail() + const authorize = async () => { + if (Date.now() >= pins.deadlineAt || !isDeepStrictEqual(namespace, await readChrLinuxNamespaceIdentity())) return fail() + } + const controls = await rootDirectory(parentDirectory) + const controlPath = async (role: keyof typeof pins.controlSockets) => { + await authorize() + return socketFence(parentDirectory, controls, role + ".sock", pins.controlSockets[role]) + } + const { readers, assertRouterIdentity } = await openChrParentCatalogueReaders({ ...pins, authorize }) + const sockets: Socket[] = [] + let parent: Awaited> | undefined + try { + const connect = async (role: keyof typeof pins.controlSockets) => { + const path = await controlPath(role) + const socket = await connectPinned(path, async () => { if (await controlPath(role) !== path) return fail() }) + sockets.push(socket) + return socket + } + const admission = await connect("admission") + const phase = await connect("phase") + const native = await connect("native") + await awaitChrParentStart(admission, pins.deadlineAt) + const phases = openChrParentConnectedPhaseChecks(phase, pins.deadlineAt, true) + let sealedDeadline: number | undefined + parent = await createChrParentOrchestration({ + scope, linux: pins.linux, deadlineAt: pins.deadlineAt, leaseProfile: pins.captureProfile?.kind, authorize, assertRouterIdentity, ...phases, + routers: { + A: { deviceId: pins.routers.A.deviceId, endpoint: endpoints.A, client: readers.A }, + B: { deviceId: pins.routers.B.deviceId, endpoint: endpoints.B, client: readers.B }, + }, + stagedTarget: { + async captureTimer() { + const captureDeadlineAt = await phases.captureDeadline() + if (sealedDeadline !== undefined && sealedDeadline !== captureDeadlineAt) return fail() + if (pins.captureProfile && captureDeadlineAt - Date.now() > pins.captureProfile.captureMs) return fail() + sealedDeadline = captureDeadlineAt + return { kind: "staged-ipfix-timeout-20s", captureDeadlineAt } + }, + }, + }) + const checked = serveChrParentNativeBoundary(native, parent, async () => { await controlPath("native") }) + const result = await Promise.race([parent.serve({ input: admission, output: admission }), checked]) + process.stdout.write(JSON.stringify({ parentComplete: true, completed: result.completed, automaticReplay: false }) + "\n") + } finally { + parent?.retire() + for (const socket of sockets) socket.destroy() + } +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) { + void run().catch(() => { + process.stderr.write('{"parentComplete":false,"automaticReplay":false}\n') + process.exitCode = 1 + }) +} + + diff --git a/backend/src/test/traffic-flow-chr-parent-native-boundary.ts b/backend/src/test/traffic-flow-chr-parent-native-boundary.ts new file mode 100644 index 00000000..7d3aa625 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-native-boundary.ts @@ -0,0 +1,68 @@ +import type { Duplex } from "node:stream" +import type { createChrParentOrchestration } from "./traffic-flow-chr-parent-orchestration.js" +import type { ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" + +type Owner = Awaited> +type Hooks = Pick +const frameLimit = 16384 +const nativeId = /^\*[a-f0-9]{1,16}$/i +const fail = () => { throw Error("Parent native boundary refused. Automatic replay forbidden.") } +const object = (raw: unknown): raw is Record => !!raw && typeof raw === "object" && !Array.isArray(raw) +const shape = (raw: Record, keys: string[]) => + Object.keys(raw).length === keys.length && keys.every(key => Object.hasOwn(raw, key)) + +async function* frames(socket: Duplex) { + let buffered = Buffer.alloc(0) + for await (const data of socket) { + const bytes = Buffer.isBuffer(data) ? data : Buffer.from(data) + if (bytes.length > frameLimit * 2) return fail() + for (let offset = 0; offset < bytes.length;) { + const end = bytes.indexOf(10, offset) + const length = (end < 0 ? bytes.length : end) - offset + if (buffered.length + length > frameLimit) return fail() + buffered = Buffer.concat([buffered, bytes.subarray(offset, offset + length)]) + if (end < 0) break + const value: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(buffered)) + if (!object(value)) return fail() + yield value + buffered = Buffer.alloc(0) + offset = end + 1 + } + } + if (buffered.length) return fail() +} + +/** Host obtains a consumed grant before sending native bytes. Only its + * authenticated peer can submit the subsequent complete response projection. */ +export async function serveChrParentNativeBoundary(socket: Duplex, hooks: Hooks, assertCurrent: () => Promise) { + let sequence = 0 + let pending: ChrWriteIntent | undefined + socket.on("error", () => {}) + for await (const value of frames(socket)) { + await assertCurrent() + const { side, method, path } = value + if (value.sequence !== ++sequence || sequence > 100 || side !== "A" && side !== "B" || + !["PUT", "PATCH", "POST", "DELETE"].includes(String(method)) || typeof path !== "string" || path.length > 128) return fail() + if (value.op === "consume") { + if (pending || !shape(value, ["sequence", "op", "side", "method", "path", "body"]) || !object(value.body) || + !Object.values(value.body).every(item => typeof item === "string")) return fail() + pending = await hooks.authorizeWrite({ side, method: String(method), path, body: value.body as Record }) + if (pending.side !== side || pending.method !== method || pending.path !== path) return fail() + } else if (value.op === "ack") { + const { status, id } = value + if (!pending || pending.side !== side || pending.method !== method || pending.path !== path || + !shape(value, ["sequence", "op", "side", "method", "path", "status", ...(Object.hasOwn(value, "id") ? ["id"] : [])]) || + typeof status !== "number" || !Number.isInteger(status) || status < 100 || status > 599) return fail() + const needsId = (method === "PUT" && (status === 200 || status === 201)) || + (pending.step === "target:" + side + ":enable" && status === 200) + if (needsId ? typeof id !== "string" || !nativeId.test(id) : Object.hasOwn(value, "id")) return fail() + hooks.recordWriteAcknowledgement({ intent: pending, status, body: needsId ? { ".id": id } : undefined }) + pending = undefined + } else return fail() + await assertCurrent() + await new Promise((resolve, reject) => { + socket.write(JSON.stringify({ sequence, ok: true }) + "\n", error => error ? reject(error) : resolve()) + }) + } + return fail() +} diff --git a/backend/src/test/traffic-flow-chr-parent-orchestration.test.ts b/backend/src/test/traffic-flow-chr-parent-orchestration.test.ts new file mode 100644 index 00000000..c87ec1e2 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-orchestration.test.ts @@ -0,0 +1,8 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { runChrParentSynthetic } from "./traffic-flow-chr-parent-synthetic.js" + +for (const mode of ["direct", "staged", "wrong-ack"] as const) test("paired catalogue and native ACK parent profile: " + mode, async () => { + const result = await runChrParentSynthetic(mode) + assert.equal(result.writes, mode === "direct" ? 48 : mode === "staged" ? 50 : 25) +}) diff --git a/backend/src/test/traffic-flow-chr-parent-orchestration.ts b/backend/src/test/traffic-flow-chr-parent-orchestration.ts new file mode 100644 index 00000000..a15f0a3e --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-orchestration.ts @@ -0,0 +1,131 @@ +import type { MikrotikClient } from "../services/mikrotik.js" +import { createChrParentAckMailbox, createChrParentTrustedStages } from "./traffic-flow-chr-parent-stages.js" +import { createChrTransitTopologyInventory } from "./traffic-flow-chr-topology-inventory.js" +import { createChrParentWriteAdmission, type ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" +import { serveChrParentAdmissionPipe } from "./traffic-flow-chr-write-admission-ipc.js" +import { chrFixtureDeadlineMaxMs, type ChrStagedFixtureLease } from "./traffic-flow-chr-target-timing-profile.js" + +type StagesOptions = Parameters[0] +type Side = "A" | "B" +type Row = Record +type Options = Pick & { + leaseProfile?: ChrStagedFixtureLease + routers: Record }> + assertTimingPreflight(): Promise + assertRouterIdentity(side: Side, deviceId: string): Promise + assertTransitAndCollectorReady(): Promise + assertTransitAndCollectorStagingReady?(): Promise + assertCaptureAndDrainComplete(): Promise + assertLinuxInverseReady(): Promise +} + +const fail = () => { throw Error("Parent CHR composition refused. Automatic replay forbidden.") } +const isRow = (value: unknown): value is Row => !!value && typeof value === "object" && !Array.isArray(value) + +export async function createChrParentOrchestration(options: Options) { + const { deadlineAt, leaseProfile, authorize, assertRouterIdentity } = options + const stagedTarget = options.stagedTarget && { ...options.stagedTarget } + const stageReady = options.assertTransitAndCollectorStagingReady + if ((stagedTarget && !stageReady) || (leaseProfile && !stagedTarget)) return fail() + const routers = { A: { ...options.routers.A }, B: { ...options.routers.B } } + const sides = ["A", "B"] as const + const check = () => { + const left = deadlineAt - Date.now() + if (!Number.isSafeInteger(deadlineAt) || left <= 0 || left > chrFixtureDeadlineMaxMs(leaseProfile)) return fail() + return Math.min(8000, left) + } + const assertCurrent = async (side: Side, deviceId: string) => { + check() + if (routers[side].deviceId !== deviceId) return fail() + await authorize() + await assertRouterIdentity(side, deviceId) + check() + } + const assertPair = async () => { + for (const side of sides) await assertCurrent(side, routers[side].deviceId) + } + const assertStageAuthority = async (intent: Readonly) => { + await assertPair() + if (intent.step.startsWith("topology:delete:")) { + await options.assertLinuxInverseReady() + } else if (intent.step.endsWith(".restore") || intent.step.startsWith("target:") && intent.method === "DELETE") { + await options.assertCaptureAndDrainComplete() + } else if (stageReady && stagedTarget && (intent.step.endsWith(".enable") || intent.step.startsWith("target:") && intent.method === "PUT")) { + await stageReady() + } else if (!intent.step.startsWith("topology:create:")) { + await options.assertTransitAndCollectorReady() + } + check() + } + const exactGet = async (side: Side, path: string, singleton = false) => { + const { client } = routers[side] + const fence = async () => { check(); await authorize(); check() } + await fence() + const timeout = check() + const raw = await client.get(path, timeout, AbortSignal.timeout(timeout), 65536, fence) + await fence() + const rows = Array.isArray(raw) ? raw : singleton && isRow(raw) ? [raw] : undefined + if (!rows || Buffer.byteLength(JSON.stringify(rows)) > 65536 || !rows.every(isRow)) return fail() + return rows + } + const readFlow = async (side: Side) => { + const deviceId = routers[side].deviceId + await assertCurrent(side, deviceId) + const [flow, ipfix, targets] = await Promise.all([ + exactGet(side, "/ip/traffic-flow", true), exactGet(side, "/ip/traffic-flow/ipfix", true), + exactGet(side, "/ip/traffic-flow/target"), + ]) + await assertCurrent(side, deviceId) + if (flow.length !== 1 || ipfix.length !== 1 || targets.length > 1) return fail() + return { deviceId, flow: flow[0], ipfix: ipfix[0], targets } + } + check() + await assertPair() + await options.assertTimingPreflight() + check() + const scope = structuredClone(options.scope) + const inventory = createChrTransitTopologyInventory({ + runId: scope[0], deadlineAt, authorize: assertPair, authorizeRequest: authorize, parallelism: 2, routers, + }) + const mailbox = createChrParentAckMailbox() + const trusted = await createChrParentTrustedStages({ + scope, linux: options.linux, deadlineAt, timerProfile: options.timerProfile, stagedTarget, authorize: assertPair, + routers: { + A: { ...routers.A, read: () => inventory.read("A"), readFlow: () => readFlow("A") }, + B: { ...routers.B, read: () => inventory.read("B"), readFlow: () => readFlow("B") }, + }, + assertOutsideScope: inventory.assertOutsideScope, assertStageAuthority, takeAcknowledgement: mailbox.take, + }) + const gate = createChrParentWriteAdmission({ + deadlineAt, profile: trusted.profile, deviceIds: { A: routers.A.deviceId, B: routers.B.deviceId }, assertCurrent, assertIntentCurrent: assertStageAuthority, + }) + let started = false, retired = false + let activeInput: Parameters[0]["input"] | undefined + const retire = () => { + retired = true + gate.retire() + mailbox.retire() + trusted.retire() + activeInput?.destroy() + } + return Object.freeze({ + authorizeWrite: gate.authorizeWrite, + recordWriteAcknowledgement: mailbox.record, + retire, + async serve(pipes: Parameters[0]) { + if (started || retired) return fail() + started = true + activeInput = pipes.input + try { + const result = await serveChrParentAdmissionPipe(pipes, gate, trusted.stages, deadlineAt, trusted.profile, leaseProfile) + if (result.completed !== trusted.stages.length || gate.consumed !== result.completed) return fail() + return result + } finally { retire() } + }, + }) +} + + + + + diff --git a/backend/src/test/traffic-flow-chr-parent-stages.test.ts b/backend/src/test/traffic-flow-chr-parent-stages.test.ts new file mode 100644 index 00000000..501c3336 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-stages.test.ts @@ -0,0 +1,121 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { chrTransitNames } from "./traffic-flow-chr-transit-fixture.js" + + +const runId = "a012bcde" +const ports = { linuxA: 50101, edgeA: 50102, middleA: 50103, middleB: 50104, edgeB: 50105, linuxB: 50106, + collector: 50107, calibrationSource: 50112, calibrationDestination: 50113, phase1Source: 50108, phase1Destination: 50109, phase2Source: 50110, phase2Destination: 50111 } +const directions = ["A-to-B", "B-to-A", "export-A", "export-B", "underlay-linuxA-to-A", "underlay-A-to-linuxA", + "underlay-A-to-B", "underlay-B-to-A", "underlay-B-to-linuxB", "underlay-linuxB-to-B"] as const +const isolation = { checkedPorts: Object.values(ports), checkedNames: Object.values(chrTransitNames(runId)), + checkedCidrs: ["198.18.240.1/32", "198.18.240.2/32", "198.18.241.0/30", "198.18.241.4/30", "198.18.241.8/30", "198.18.242.0/29"], + occupiedPorts: [], overlappingCidrs: [], occupiedNames: [], + policies: directions.map(direction => ({ direction, verdict: "allow", nat: "none", fastTrack: "none", ambiguousRules: 0 } as const)) } +import { createChrParentAckMailbox, createChrParentTrustedStages } from "./traffic-flow-chr-parent-stages.js" +import type { ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" + +const menus = { "/interface/wireguard": "wireguard", "/interface/wireguard/peers": "peer", "/ip/address": "address", "/ip/route": "route" } as const +async function fixture(fault?: "foreign" | "ack" | "singleton" | "target" | "authority") { + const makeRows = () => ({ wireguard: [] as Row[], peer: [] as Row[], address: [] as Row[], route: [] as Row[] }) + const ipfix = Object.fromEntries(["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", "dst-port", "in-interface", "out-interface"].map(key => [key, "no"])) + const rows = { A: makeRows(), B: makeRows() } + const makeFlow = (side: "A" | "B") => ({ + deviceId: side, flow: { ...(side === "B" ? { ".id": "*F" } : {}), enabled: "no", interfaces: "all", + "inactive-flow-timeout": "15s" }, ipfix: { ...ipfix }, targets: [] as Row[], + }) + const flow = { A: makeFlow("A"), B: makeFlow("B") } + function router(side: "A" | "B", endpoint: string) { + return { deviceId: side, endpoint, read: async () => ({ deviceId: side, complete: true as const, rows: structuredClone(rows[side]) }), + readFlow: async () => structuredClone(flow[side]) } + } + let count = 0 + const writes: ChrWriteIntent[] = [] + const result = await createChrParentTrustedStages({ + scope: [runId, ports, isolation], deadlineAt: 100000, now: () => 10000, + timerProfile: { kind: "bounded-ipfix-timeout-20s", captureDeadlineAt: 99000, now: () => 10000 }, + routers: { A: router("A", "192.0.2.1"), B: router("B", "192.0.2.2") }, + linux: { endpoint: "192.0.2.3", publicKeys: { linuxA: "X".repeat(43) + "=", linuxB: "Y".repeat(43) + "=" } }, + authorize: async () => {}, assertOutsideScope: async () => {}, + async assertStageAuthority(intent) { + if (fault === "authority" && intent.step === "flow:A:ipfix.enable") throw Error("Owner phase refused") + }, + async takeAcknowledgement(intent) { + const recorded = writes.at(-1)! + const id = "*" + (++count).toString(16) + const slash = recorded.path.lastIndexOf("/") + const menuPath = recorded.method === "PUT" ? recorded.path : recorded.path.slice(0, slash) + const menu = menus[menuPath as keyof typeof menus] + if (menu) { + if (recorded.method === "PUT") { + rows[recorded.side][menu].push({ ...recorded.body, ".id": id, ...(menu === "wireguard" ? { "public-key": String.fromCharCode(64 + count).repeat(43) + "=" } : {}) }) + if (fault === "foreign") rows.B.address.push({ ".id": "*FF" }) + } else rows[recorded.side][menu] = rows[recorded.side][menu].filter(row => row[".id"] !== decodeURIComponent(recorded.path.slice(slash + 1))) + } else if (menuPath === "/ip/traffic-flow/target") { + if (recorded.method === "PUT") { + flow[recorded.side].targets.push({ ".id": id, disabled: false, ...recorded.body, + ...(fault === "target" ? { "v9-template-timeout": "21s" } : {}) }) + } else flow[recorded.side].targets = [] + } else { + Object.assign(menuPath.endsWith("/ipfix") ? flow[recorded.side].ipfix : flow[recorded.side].flow, recorded.body) + if (fault === "singleton") flow[recorded.side].flow.interfaces = "foreign" + } + return { intent: { ...intent, ...(fault === "ack" ? { side: "B" } : {}) }, + status: recorded.method === "PUT" ? 201 : 204, body: recorded.method === "PUT" ? { ".id": id } : undefined } + }, + }) + return { ...result, rows, flow, writes, async step(index: number) { + const stage = result.stages[index] + const intent = await stage.intent() + writes.push(intent) + await stage.verify(intent) + } } +} + +test("parent resolves exact 48 stages from real proof owners and restores both baselines", async () => { + const f = await fixture() + assert.equal(f.stages.length, 48) + for (let i = 0; i < f.stages.length; i++) await f.step(i) + assert(f.writes.some(write => write.step === "flow:B:flow.enable" && write.method === "PATCH" && write.path === "/ip/traffic-flow/*F")) + assert(f.writes.some(write => write.step === "flow:A:flow.enable" && write.method === "POST" && write.path === "/ip/traffic-flow/set")) + assert(f.writes.some(write => write.step === "target:A:create" && write.body["v9-template-timeout"] === "20s")) + assert.deepEqual(Object.values(f.rows).flatMap(side => Object.values(side).flat()), []) + assert.equal(f.flow.B.flow.enabled, "no") +}) + +test("parent refuses ACK identity, peer drift, singleton drift and wrong target timer without releasing next stage", async () => { + for (const [fault, failedAt] of [["ack", 0], ["foreign", 0], ["singleton", 18], ["target", 20]] as const) { + const f = await fixture(fault) + for (let i = 0; i < failedAt; i++) await f.step(i) + await assert.rejects(f.step(failedAt)) + await assert.rejects(f.stages[failedAt + 1].intent()) + await assert.rejects(f.stages[failedAt].intent()) + assert.equal(f.writes.length, failedAt + 1) + } +}) + + +test("outer relay ACK mailbox allows one exact take and latches missing or duplicate data", async () => { + const f = await fixture() + const intent = await f.stages[0].intent() + const ack = { intent, status: 201, body: { ".id": "*A" } } + f.retire() + const mailbox = createChrParentAckMailbox() + mailbox.record(ack) + assert.deepEqual(await mailbox.take(intent), ack) + await assert.rejects(mailbox.take(intent)) + assert.throws(() => mailbox.record(ack)) + const duplicate = createChrParentAckMailbox() + duplicate.record(ack) + assert.throws(() => duplicate.record(ack)) + await assert.rejects(duplicate.take(intent)) +}) + + +test("parent readiness refusal blocks the export phase before any singleton or target grant", async () => { + const f = await fixture("authority") + for (let i = 0; i < 18; i++) await f.step(i) + await assert.rejects(f.stages[18].intent()) + await assert.rejects(f.stages[19].intent()) + assert.equal(f.writes.length, 18) +}) diff --git a/backend/src/test/traffic-flow-chr-parent-stages.ts b/backend/src/test/traffic-flow-chr-parent-stages.ts new file mode 100644 index 00000000..504a6c63 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-stages.ts @@ -0,0 +1,280 @@ +import { isDeepStrictEqual } from "node:util" +import { createDisabledChrFlowHarness } from "./traffic-flow-chr-disabled-baseline.js" +import { assertChrTargetTimerWindow, createChrTargetHarness, type ChrTargetTimerProfile, type ChrStagedTargetTimerProfile } from "./traffic-flow-chr-target-harness.js" +import { createChrTransitTopologyOwner } from "./traffic-flow-chr-topology-owner.js" +import { planChrTransitFixture } from "./traffic-flow-chr-transit-fixture.js" +import { chrWriteScheduleLimit, type ChrWriteAdmission, type ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" +import type { serveChrParentAdmissionPipe } from "./traffic-flow-chr-write-admission-ipc.js" + +type Topology = Parameters[0] +type Flow = Parameters[0] +type Side = "A" | "B" +type Stage = Parameters[2][number] +export type ChrParentNativeAck = { intent: ChrWriteIntent; status: number; body: unknown } +type Options = Pick & { + routers: Record & { readFlow: Flow["read"] }> + timerProfile?: ChrTargetTimerProfile + stagedTarget?: { captureTimer(): Promise } + /** Assert the parent-owned phase permits this stage (for example, isolated transit ready before export). */ + assertStageAuthority(intent: Readonly): Promise + takeAcknowledgement(intent: Readonly): Promise +} + +const roles = ["edgeA", "middleA", "middleB", "edgeB"] as const +const sides = ["A", "B"] as const +const exportFields = ["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", "dst-port", "in-interface", "out-interface"] +const disabled = (value: unknown) => value === false || value === "false" || value === "no" +const refuse = (): never => { throw Error("Parent native stage refused. Automatic replay forbidden.") } +const require = (value: unknown) => { if (!value) refuse() } + +function deferred() { + let resolve!: (value: T) => void, reject!: (error: unknown) => void + const promise = new Promise((yes, no) => { resolve = yes; reject = no }) + void promise.catch(() => {}) + return { promise, resolve, reject } +} + +/** The parent executes the same bounded proof owners using independent native reads. + * Its synthetic transport cannot send a write and can only consume the outer relay ACK. */ +export async function createChrParentTrustedStages(options: Options) { + const scope = structuredClone(options.scope) + const plan = planChrTransitFixture(...scope) + const routers = { A: { ...options.routers.A }, B: { ...options.routers.B } } + const { deadlineAt, now = Date.now, authorize, takeAcknowledgement, assertOutsideScope, assertStageAuthority } = options + const timerProfile = options.timerProfile && { ...options.timerProfile } + const stagedTarget = options.stagedTarget && { ...options.stagedTarget } + require(!stagedTarget || !timerProfile) + const profile = stagedTarget ? "staged-disabled-target" : "direct-target" + let failed = false, cursor = 0 + const live = () => require(!failed && Number.isSafeInteger(deadlineAt) && now() < deadlineAt) + const readFlow = async (side: Side) => { + live() + await authorize() + const value = structuredClone(await routers[side].readFlow()) + await authorize() + live() + require(value.deviceId === routers[side].deviceId && value.flow && value.ipfix && + Array.isArray(value.targets) && value.targets.length <= 1 && Buffer.byteLength(JSON.stringify(value)) <= 65536) + return value + } + const baseline = { A: await readFlow("A"), B: await readFlow("B") } + if (timerProfile) { + require(timerProfile.captureDeadlineAt <= deadlineAt) + for (const side of sides) assertChrTargetTimerWindow(timerProfile, baseline[side].flow["inactive-flow-timeout"]) + } + const expected = structuredClone(baseline) + const hasIpfixChange = (side: Side) => exportFields.some(field => disabled(baseline[side].ipfix[field])) + const keys = [...roles, ...roles.map(role => "address:" + role), ...roles.map(role => "peer:" + role), + ...plan.routes.filter(route => route.node === "A" || route.node === "B").map((route, i) => [route.node, "route:" + i].join(":"))] + const topologyStep = (action: string, key: string) => { + const side = key.startsWith("A:route:") || key.startsWith("B:route:") ? key[0] : key.at(-1) + return "topology:" + action + ":" + side + ":" + key.replace(/^[AB]:(?=route:)/, "") + } + const labels = keys.map(key => topologyStep("create", key)) + for (const side of sides) { + if (hasIpfixChange(side)) labels.push("flow:" + side + ":ipfix.enable") + labels.push("flow:" + side + ":flow.enable", "target:" + side + ":create") + } + if (stagedTarget) for (const side of sides) labels.push("target:" + side + ":enable") + for (const side of [...sides].reverse()) { + labels.push("target:" + side + ":delete", "flow:" + side + ":flow.restore") + if (hasIpfixChange(side)) labels.push("flow:" + side + ":ipfix.restore") + } + labels.push(...[...keys].reverse().map(key => topologyStep("delete", key))) + require(keys.length === 18 && labels.length <= chrWriteScheduleLimit(profile) && new Set(labels).size === labels.length) + type Slot = { + intent: ChrWriteIntent + acknowledgement: ReturnType> + verified: ReturnType> + requested: boolean + supplied: boolean + } + const ready = labels.map(() => deferred()) + const requested = labels.map(() => deferred()) + let slot: Slot | undefined + let timer: ReturnType | undefined + let launched = false + const abort = (error: unknown) => { + failed = true + clearTimeout(timer) + slot?.acknowledgement.reject(error) + slot?.verified.reject(error) + for (const entry of ready) entry.reject(error) + for (const entry of requested) entry.reject(error) + } + const expectFlow = async (side: Side) => require(isDeepStrictEqual(await readFlow(side), expected[side])) + const otherSide = (side: Side) => side === "A" ? "B" : "A" + const conserveFlow = async (intent: ChrWriteIntent) => { + await expectFlow(otherSide(intent.side)) + if (intent.step.startsWith("topology:")) await expectFlow(intent.side) + } + + const broker: ChrWriteAdmission = { + async run(intent: ChrWriteIntent, send: (arm: () => Promise) => Promise): Promise { + live() + require(!slot && labels[cursor] === intent.step) + await requested[cursor].promise + live() + slot = { intent: structuredClone(intent), acknowledgement: deferred(), verified: deferred(), requested: false, supplied: false } + return send(async () => { + live() + require(slot?.intent.step === intent.step) + await assertStageAuthority(structuredClone(intent)) + await conserveFlow(intent) + if (!intent.step.startsWith("topology:")) await topology.assertCurrent() + live() + ready[cursor].resolve(slot!) + }) + }, + } + const nativeFor = () => { + const simulate = async (method: ChrWriteIntent["method"], path: string, body: unknown, before?: () => Promise) => { + const entry = slot + require(entry && before && isDeepStrictEqual({ method, path, body }, { + method: entry?.intent.method, path: entry?.intent.path, body: entry?.intent.body, + })) + await before!() + return await entry!.acknowledgement.promise as T + } + return { + put: (path: string, body: unknown, _timeout?: number, before?: () => Promise) => simulate("PUT", path, body, before), + patch: (path: string, body: unknown, _timeout?: number, before?: () => Promise) => simulate("PATCH", path, body, before), + post: (path: string, body: unknown, _timeout?: number, _headers?: unknown, before?: () => Promise) => simulate("POST", path, body, before), + delete: async (path: string, _timeout?: number, before?: () => Promise) => { await simulate("DELETE", path, {}, before) }, + } + } + const checkpoint = async (entry: { phase: string }) => { + if (entry.phase !== "verified") return + const verified = slot + require(verified?.supplied) + const intent = verified!.intent + await conserveFlow(intent) + if (!intent.step.startsWith("topology:")) { + await topology.assertCurrent() + // The harness already performed exact local readback. See checkedRead below. + require(nextFlow && nextFlow.side === intent.side) + expected[intent.side] = nextFlow!.value + nextFlow = undefined + } + slot = undefined + cursor++ + verified!.verified.resolve() + } + let nextFlow: { side: Side; value: Flow["baseline"] } | undefined + const checkedRead = async (side: Side) => { + const value = await readFlow(side) + if (slot?.supplied) nextFlow = { side, value } + return value + } + const native = nativeFor() + const topology = createChrTransitTopologyOwner({ + scope, linux: options.linux, deadlineAt, now, authorize, assertOutsideScope, + routers: { A: { ...routers.A, native }, B: { ...routers.B, native } }, + writeAdmission: broker, checkpoint, + assertFlowRestored: async () => { await expectFlow("A"); await expectFlow("B"); require(isDeepStrictEqual(expected, baseline)) }, + }) + const flows = Object.fromEntries(sides.map(side => [side, createDisabledChrFlowHarness({ + baseline: baseline[side], transitInterface: "nf" + plan.runId + (side === "A" ? "ea" : "eb"), + authorize, read: () => checkedRead(side), native, checkpoint, relayWrite: { side, admission: broker }, + })])) as Record> + + const workflow = async () => { + await topology.acquire() + const targets: Partial>> = {} + for (const side of sides) { + const active = await flows[side].enable() + const targetOptions = { + baseline: active, source: side === "A" ? plan.address.exportA : plan.address.exportB, + destination: plan.address.collector, port: plan.ports.collector, + authorize, read: () => checkedRead(side), native, checkpoint, relayWrite: { side, admission: broker }, + } + const target = stagedTarget + ? createChrTargetHarness({ ...targetOptions, stagedDisabled: true }) + : createChrTargetHarness({ ...targetOptions, timerProfile }) + await target.create() + targets[side] = target + } + if (stagedTarget) for (const side of sides) { + await requested[cursor].promise + live() + const timer = await stagedTarget.captureTimer() + require(timer.kind === "staged-ipfix-timeout-20s" && timer.captureDeadlineAt <= deadlineAt) + await targets[side]!.enable(timer) + } + for (const side of [...sides].reverse()) { + await targets[side]!.remove() + await flows[side].restore() + } + await topology.restore() + await expectFlow("A") + await expectFlow("B") + require(cursor === labels.length && isDeepStrictEqual(expected, baseline)) + } + let completed: Promise | undefined + const retire = () => abort(Error("Parent native stage retired")) + const stages: Stage[] = labels.map((step, index) => Object.freeze({ + step, + async intent() { + try { + live() + require(cursor === index) + if (!launched) { + launched = true + timer = setTimeout(retire, Math.max(1, deadlineAt - now())) + completed = workflow() + void completed.then(() => clearTimeout(timer), abort) + } + requested[index].resolve() + const entry = await ready[index].promise + live() + require(slot === entry && !entry.requested) + entry.requested = true + return structuredClone(entry.intent) + } catch (error) { abort(error); throw error } + }, + async verify(intent: Readonly) { + try { + live() + const entry = slot + require(cursor === index && entry?.requested && !entry.supplied && isDeepStrictEqual(entry.intent, intent)) + const ack = await takeAcknowledgement(intent) + live() + require(ack && isDeepStrictEqual(ack.intent, intent) && + (intent.method === "PUT" ? [200, 201].includes(ack.status) : + intent.step === "target:" + intent.side + ":enable" ? ack.status === 200 : [200, 204].includes(ack.status))) + entry!.supplied = true + entry!.acknowledgement.resolve(structuredClone(ack.body)) + await entry!.verified.promise + if (index === labels.length - 1) await completed + live() + } catch (error) { abort(error); throw error } + }, + })) + return Object.freeze({ profile, stages: Object.freeze(stages), retire }) +} + + +/** Connect directly to the owned parent relay. Each response is disposable and + * usable once. Only exact consumed gate intent and projected ACK travel here. */ +export function createChrParentAckMailbox() { + let pending: ChrParentNativeAck | undefined, retired = false + return Object.freeze({ + record(ack: ChrParentNativeAck) { + if (retired || pending) { retired = true; pending = undefined; return refuse() } + pending = structuredClone(ack) + }, + async take(intent: Readonly) { + const ack = pending + pending = undefined + if (retired || !ack || !isDeepStrictEqual(ack.intent, intent)) { retired = true; return refuse() } + return ack + }, + retire() { retired = true; pending = undefined }, + }) +} + + + + + + diff --git a/backend/src/test/traffic-flow-chr-parent-synthetic.ts b/backend/src/test/traffic-flow-chr-parent-synthetic.ts new file mode 100644 index 00000000..07e0a324 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-synthetic.ts @@ -0,0 +1,237 @@ +import assert from "node:assert/strict" +import { Duplex, PassThrough } from "node:stream" +import { serveChrParentNativeBoundary } from "./traffic-flow-chr-parent-native-boundary.js" +import { chrTransitNames } from "./traffic-flow-chr-transit-fixture.js" + + +const runId = "a012bcde" +const ports = { linuxA: 50101, edgeA: 50102, middleA: 50103, middleB: 50104, edgeB: 50105, linuxB: 50106, + collector: 50107, calibrationSource: 50112, calibrationDestination: 50113, phase1Source: 50108, phase1Destination: 50109, phase2Source: 50110, phase2Destination: 50111 } +const directions = ["A-to-B", "B-to-A", "export-A", "export-B", "underlay-linuxA-to-A", "underlay-A-to-linuxA", + "underlay-A-to-B", "underlay-B-to-A", "underlay-B-to-linuxB", "underlay-linuxB-to-B"] as const +const isolation = { checkedPorts: Object.values(ports), checkedNames: Object.values(chrTransitNames(runId)), + checkedCidrs: ["198.18.240.1/32", "198.18.240.2/32", "198.18.241.0/30", "198.18.241.4/30", "198.18.241.8/30", "198.18.242.0/29"], + occupiedPorts: [], overlappingCidrs: [], occupiedNames: [], + policies: directions.map(direction => ({ direction, verdict: "allow", nat: "none", fastTrack: "none", ambiguousRules: 0 } as const)) } +import { createChrParentOrchestration } from "./traffic-flow-chr-parent-orchestration.js" +import { createChrChildAdmissionPipe } from "./traffic-flow-chr-write-admission-ipc.js" +import { parseChrVrfPrefix } from "./chr-vrf-companion-address-space.js" +import type { ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" + +const admittedMenus = ["/interface/wireguard", "/ip/address", "/interface/wireguard/peers", "/ip/route", "/ip/traffic-flow/target"] +const roles = ["edgeA", "middleA", "middleB", "edgeB"] +const roleSuffix: Record = { edgeA: "ea", middleA: "ma", middleB: "mb", edgeB: "eb" } +const names = Object.fromEntries(roles.map(role => [role, "nf" + runId + roleSuffix[role]])) +const marker = "netflow-" + runId +const exportFields = ["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", "dst-port", "in-interface", "out-interface"] +const singleton = "/ip/traffic-flow", ipfix = singleton + "/ipfix", target = singleton + "/target" +type Row = Record +type Side = "A" | "B" + +function state(side: Side) { + return { + [singleton]: [{ ...(side === "B" ? { ".id": "*F" } : {}), enabled: "no", interfaces: "all", "inactive-flow-timeout": "15s" }], + [ipfix]: [Object.fromEntries(exportFields.map(key => [key, "no"]))], + "/interface": [{ ".id": "*F0", name: "ether1", type: "ether" }], + } as Record +} + +export async function runChrParentSynthetic(mode: "direct" | "staged" | "wrong-ack", density: "empty" | "dense" = "empty") { + const staged = mode !== "direct" + const phaseCalls: string[] = [] + const rows = { A: state("A"), B: state("B") }, baseline = structuredClone(rows) + const stressPaths = new Set(["/ip/firewall/filter", "/ip/firewall/nat", "/ip/firewall/raw", "/ip/firewall/mangle", + "/ipv6/firewall/filter", "/ipv6/firewall/nat", "/ipv6/firewall/raw", "/ipv6/firewall/mangle"]) + const denseBody = density === "dense" ? JSON.stringify(Array.from({ length: 700 }, (_, i) => ({ + ".id": "*" + (i + 4096).toString(16), chain: "forward", action: "accept", disabled: "true", + comment: "synthetic-stress-" + i + "-" + "x".repeat(1150), + }))) : "" + const catalogueFixtureBytesPerSide = Buffer.byteLength(denseBody) * stressPaths.size + const now = Date.now() + let writes = 0, staging = false, ready = false, drained = false, inverse = false + const reads: Record = { A: 0, B: 0 } + const makeRouter = (side: Side, endpoint: string) => ({ + endpoint, deviceId: side, + client: { async get(path: string, _timeout?: number, _signal?: AbortSignal, _max?: number, before?: () => Promise) { + await before?.() + reads[side]++ + if (denseBody && stressPaths.has(path)) return JSON.parse(denseBody) as T + return JSON.parse(JSON.stringify(rows[side][path] ?? [])) as T + } }, + }) + const orchestration = await createChrParentOrchestration({ + scope: [runId, ports, isolation], deadlineAt: now + (density === "dense" ? 580000 : 100000), + linux: { endpoint: "192.0.2.3", publicKeys: { linuxA: "X".repeat(43) + "=", linuxB: "Y".repeat(43) + "=" } }, + routers: { A: makeRouter("A", "192.0.2.1"), B: makeRouter("B", "192.0.2.2") }, + ...(staged ? { stagedTarget: { async captureTimer() { + assert(ready) + return { kind: "staged-ipfix-timeout-20s" as const, captureDeadlineAt: Math.min(Date.now() + 180000, now + (density === "dense" ? 580000 : 100000)) } + } } } : { timerProfile: { kind: "bounded-ipfix-timeout-20s" as const, captureDeadlineAt: now + 89000 } }), + assertTransitAndCollectorStagingReady: async () => { phaseCalls.push("staging"); assert(staging && !ready) }, + authorize: async () => {}, assertTimingPreflight: async () => { phaseCalls.push("preflight") }, assertRouterIdentity: async (side, id) => assert.equal(side, id), + assertTransitAndCollectorReady: async () => { phaseCalls.push("ready"); assert(ready) }, + assertCaptureAndDrainComplete: async () => { phaseCalls.push("drained"); assert(drained) }, + assertLinuxInverseReady: async () => { phaseCalls.push("inverse"); assert(inverse) }, + }) + const toParent = new PassThrough(), toChild = new PassThrough() + const done = orchestration.serve({ input: toParent, output: toChild }) + void done.catch(() => {}) + const child = createChrChildAdmissionPipe({ input: toChild, output: toParent }, staged ? "staged-disabled-target" : "direct-target") + const nativeInput = new PassThrough(), nativeOutput = new PassThrough() + const nativeStream = Duplex.from({ readable: nativeInput, writable: nativeOutput }) + const physicalBoundary = serveChrParentNativeBoundary(nativeStream, orchestration, async () => {}) + void physicalBoundary.catch(() => { nativeStream.destroy(); nativeOutput.destroy() }) + let nativeSequence = 0 + const physicalExchange = async (data: object) => { + const sequence = ++nativeSequence + await new Promise((resolve, reject) => { + const receive = (reply: Buffer) => { + nativeOutput.off("close", closed) + try { assert.deepEqual(JSON.parse(reply.toString()), { sequence, ok: true }); resolve() } + catch (error) { reject(error) } + } + const closed = () => { nativeOutput.off("data", receive); reject(Error("Synthetic native boundary closed")) } + nativeOutput.once("data", receive) + nativeOutput.once("close", closed) + nativeInput.write(JSON.stringify({ sequence, ...data }) + "\n") + }) + } + const owned = new Map() + const execute = async (intent: ChrWriteIntent) => { + await child.run(intent, async arm => { + await arm() + const projection = { side: intent.side, method: intent.method, path: intent.path } + await physicalExchange({ op: "consume", ...projection, body: intent.body }) + const catalogue = rows[intent.side] + const id = "*" + (++writes).toString(16) + const slash = intent.path.lastIndexOf("/") + const menu = intent.method === "PUT" ? intent.path : intent.path.slice(0, slash) + const list = catalogue[menu] ??= [] + let body: unknown + if (intent.method === "PUT") { + assert(admittedMenus.includes(menu)) + const row: Row = { ...intent.body, ".id": id } + if (menu === "/interface/wireguard") { + row["public-key"] = String.fromCharCode(64 + writes).repeat(43) + "=" + catalogue["/interface"].push({ ".id": id, name: row.name, type: "wireguard", comment: marker, disabled: false }) + } + if (menu === "/ip/address") { + row.dynamic = false + row.vrf = "main" + const prefix = parseChrVrfPrefix(row.address, 4)! + const ip = [24n, 16n, 8n, 0n].map(shift => Number((prefix.first >> shift) & 255n)).join(".") + const route = { ".id": "*a" + id.slice(1), "dst-address": ip + "/" + prefix.prefixLength, gateway: row.interface, + "routing-table": "main", connect: true, distance: "0", scope: "10", "target-scope": "5" } + ;(catalogue["/ip/route"] ??= []).push({ ...route, dynamic: true }) + ;(catalogue["/routing/route"] ??= []).push({ ...route, afi: "ip4", "belongs-to": "connected", + "local-address": String(row.address).split("/")[0] + "%" + row.interface }) + } + if (menu === target) row.disabled = staged ? "yes" : false + list.push(row) + owned.set(intent.step, { intent, id }) + body = { ".id": id } + } else if (intent.method === "DELETE") { + const previous = decodeURIComponent(intent.path.slice(slash + 1)) + assert.equal(list.filter(row => row[".id"] === previous).length, 1) + catalogue[menu] = list.filter(row => row[".id"] !== previous) + if (menu === "/interface/wireguard") catalogue["/interface"] = catalogue["/interface"].filter(row => row[".id"] !== previous) + if (menu === "/ip/address") for (const path of ["/ip/route", "/routing/route"]) { + catalogue[path] = catalogue[path].filter(row => row[".id"] !== "*a" + previous.slice(1)) + } + } else if (menu === target) { + const previous = decodeURIComponent(intent.path.slice(slash + 1)) + const matching = list.filter(row => row[".id"] === previous) + assert.equal(matching.length, 1) + assert.equal(matching[0].disabled, "yes") + Object.assign(matching[0], intent.body) + body = { ".id": mode === "wrong-ack" ? "*FFFF" : previous } + } else Object.assign(list[0], intent.body) + await physicalExchange({ op: "ack", ...projection, status: intent.method === "PUT" ? 201 : body ? 200 : 204, + ...(body && typeof body === "object" ? { id: (body as Row)[".id"] } : {}) }) + }) + } + const send = (step: string, side: Side, method: ChrWriteIntent["method"], path: string, body: ChrWriteIntent["body"]) => + execute({ step, side, deviceId: side, method, path, body }) + const create = (key: string, side: Side, path: string, body: Record) => + send("topology:create:" + side + ":" + key, side, "PUT", path, { ...body, disabled: "false", comment: marker }) + const addr: Record = { edgeA: "198.18.241.2/30", middleA: "198.18.241.5/30", middleB: "198.18.241.6/30", edgeB: "198.18.241.9/30" } + const sideOf = (role: string): Side => role.endsWith("A") ? "A" : "B" + const portOf = (role: string) => String(ports[role as keyof typeof ports]) + try { + for (const role of roles) await create(role, sideOf(role), "/interface/wireguard", { name: names[role], "listen-port": portOf(role), mtu: "1420" }) + for (const role of roles) await create("address:" + role, sideOf(role), "/ip/address", { interface: names[role], address: addr[role] }) + const peerSpecs = [ + ["edgeA", "linuxA", "192.0.2.3", "X", "198.18.240.1/32,198.18.242.3/32"], + ["middleA", "middleB", "192.0.2.2", "C", "198.18.240.2/32"], + ["middleB", "middleA", "192.0.2.1", "B", "198.18.240.1/32"], + ["edgeB", "linuxB", "192.0.2.3", "Y", "198.18.240.2/32,198.18.242.3/32"], + ] + for (const [role, opposite, endpoint, key, allowed] of peerSpecs) await create("peer:" + role, sideOf(role), "/interface/wireguard/peers", { + interface: names[role], "public-key": key.repeat(43) + "=", "allowed-address": allowed, + "endpoint-address": endpoint, "endpoint-port": portOf(opposite), "persistent-keepalive": "10", + }) + const routes = [ + ["edgeA", "198.18.240.1", "198.18.241.1"], ["middleA", "198.18.240.2", "198.18.241.6"], ["edgeA", "198.18.242.3", "198.18.241.1"], + ["edgeB", "198.18.240.2", "198.18.241.10"], ["middleB", "198.18.240.1", "198.18.241.5"], ["edgeB", "198.18.242.3", "198.18.241.10"], + ] + for (const [i, [role, destination, gateway]] of routes.entries()) await create("route:" + i, sideOf(role), "/ip/route", { + "dst-address": destination + "/32", gateway: gateway + "%" + names[role], "routing-table": "main", + }) + staging = staged + ready = !staged + const changeIpfix = (side: Side, enable: boolean) => send("flow:" + side + ":ipfix." + (enable ? "enable" : "restore"), + side, "POST", ipfix + "/set", Object.fromEntries(exportFields.map(key => [key, enable ? "yes" : "no"]))) + const changeFlow = (side: Side, enable: boolean) => send("flow:" + side + ":flow." + (enable ? "enable" : "restore"), + side, side === "B" ? "PATCH" : "POST", singleton + (side === "B" ? "/*F" : "/set"), + { enabled: enable ? "yes" : "no", interfaces: enable ? names["edge" + side] : "all" }) + for (const side of ["A", "B"] as const) { + await changeIpfix(side, true) + await changeFlow(side, true) + await send("target:" + side + ":create", side, "PUT", target, { "dst-address": "198.18.242.3", + "src-address": side === "A" ? "198.18.241.2" : "198.18.241.9", port: portOf("collector"), version: "ipfix", "v9-template-timeout": "20s", ...(staged ? { disabled: "yes" } : {}) }) + } + if (staged) { + ready = true + for (const side of ["A", "B"] as const) { + const enabling = send("target:" + side + ":enable", side, "PATCH", target + "/" + owned.get("target:" + side + ":create")!.id, { disabled: "no" }) + if (mode === "wrong-ack") { + await assert.rejects(enabling) + await assert.rejects(done) + assert.equal(writes, 25) + assert.equal(rows.B[target][0].disabled, "yes") + return { completed: 0, writes, reads, catalogueFixtureBytesPerSide, nativeFrames: nativeSequence } + } + await enabling + } + } + drained = true + for (const side of ["B", "A"] as const) { + await send("target:" + side + ":delete", side, "DELETE", target + "/" + owned.get("target:" + side + ":create")!.id, {}) + await changeFlow(side, false) + await changeIpfix(side, false) + } + inverse = true + for (const { intent, id } of [...owned.values()].reverse().filter(value => value.intent.step.startsWith("topology:"))) { + await send(intent.step.replace(":create:", ":delete:"), intent.side, "DELETE", intent.path + "/" + id, {}) + } + toParent.end() + assert.deepEqual(await done, { completed: staged ? 50 : 48 }) + assert.equal(writes, staged ? 50 : 48) + assert(reads.A > 100 && reads.B > 100) + assert.deepEqual([...new Set(phaseCalls)], ["preflight", ...(staged ? ["staging"] : []), "ready", "drained", "inverse"]) + for (const side of ["A", "B"] as const) { + for (const [path, records] of Object.entries(rows[side])) assert.deepEqual(records, baseline[side][path] ?? []) + } + assert.equal(nativeSequence, writes * 2) + return { completed: writes, writes, reads, catalogueFixtureBytesPerSide, nativeFrames: nativeSequence } + } finally { + nativeStream.destroy() + nativeInput.destroy() + nativeOutput.destroy() + toParent.destroy() + toChild.destroy() + orchestration.retire() + } +} + + diff --git a/backend/src/test/traffic-flow-chr-parent-working-set.ts b/backend/src/test/traffic-flow-chr-parent-working-set.ts new file mode 100644 index 00000000..2930c47c --- /dev/null +++ b/backend/src/test/traffic-flow-chr-parent-working-set.ts @@ -0,0 +1,45 @@ +import { readFile } from "node:fs/promises" +import { fileURLToPath } from "node:url" +// Load the real launch graph before the synthetic exercise so the process +// includes its code and dependencies in the working-set measurement. +import "./traffic-flow-chr-parent-launch.js" +import { runChrParentSynthetic } from "./traffic-flow-chr-parent-synthetic.js" + +async function cgroupMemory() { + if (process.platform !== "linux") return undefined + const read = async (key: "memory.max" | "memory.current" | "memory.peak") => { + try { + const text = (await readFile("/sys/fs/cgroup/" + key, "ascii")).trim() + if (text === "max") return text + const value = Number(text) + return /^[0-9]{1,19}$/.test(text) && Number.isSafeInteger(value) ? value : undefined + } catch { return undefined } + } + const [max, current, peak] = await Promise.all([read("memory.max"), read("memory.current"), read("memory.peak")]) + return { max, current, peak } +} + +async function main() { + const [mode, density, extra] = process.argv.slice(2) + if (mode !== "--run-inert-netflow-parent-working-set" || density !== "empty" && density !== "dense" || extra !== undefined) { + throw Error("Invalid synthetic working-set profile") + } + const started = performance.now() + const initialRssKiB = Math.ceil(process.memoryUsage.rss() / 1024) + const result = await runChrParentSynthetic("staged", density) + const maxRssKiB = process.resourceUsage().maxRSS + if (result.completed !== 50 || result.nativeFrames !== 100 || maxRssKiB <= 0) throw Error("Synthetic working set incomplete") + process.stdout.write(JSON.stringify({ + synthetic: true, profile: density, platform: process.platform, nodeVersion: process.version, + ...result, initialRssKiB, maxRssKiB, elapsedMs: Math.ceil(performance.now() - started), + within128MiB: maxRssKiB <= 128 * 1024, cgroupMemory: await cgroupMemory(), + }) + "\n") +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) { + void main().catch(() => { + process.stderr.write('{"synthetic":true,"complete":false}\n') + process.exitCode = 1 + }) +} + diff --git a/backend/src/test/traffic-flow-chr-pcapng.ts b/backend/src/test/traffic-flow-chr-pcapng.ts new file mode 100644 index 00000000..ba77e188 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-pcapng.ts @@ -0,0 +1,70 @@ +/** Read a bounded, one-section PCAPNG capture with enhanced packet blocks. */ +export function readChrPcapng(bytes: Buffer, linkType: number, snapLength: number) { + if (bytes.length < 28) throw Error("Incomplete PCAPNG section") + const encoding = bytes.readUInt32BE(8) + if (encoding !== 0x1a2b3c4d && encoding !== 0x4d3c2b1a) throw Error("Invalid PCAPNG byte order") + const little = encoding === 0x4d3c2b1a + const u16 = (offset: number) => little ? bytes.readUInt16LE(offset) : bytes.readUInt16BE(offset) + const u32 = (offset: number) => little ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset) + const frames: { timestamp: bigint; frame: Buffer }[] = [] + const interfaces: { snap: number; units: bigint; offset: bigint }[] = [] + for (let start = 0; start < bytes.length;) { + if (bytes.length - start < 12) throw Error("Incomplete PCAPNG block") + const type = u32(start), size = u32(start + 4), end = start + size + if (size < 12 || size % 4 !== 0 || end > bytes.length || u32(end - 4) !== size) + throw Error("Invalid PCAPNG block length") + const options = (from: number, visit: (code: number, offset: number, length: number) => void) => { + for (let at = from; at < end - 4;) { + if (at + 4 > end - 4) throw Error("Incomplete PCAPNG option") + const code = u16(at), length = u16(at + 2), next = at + 4 + Math.ceil(length / 4) * 4 + if (next > end - 4) throw Error("Invalid PCAPNG option length") + if (code === 0) { + if (length || next !== end - 4) throw Error("Invalid PCAPNG option terminator") + return + } + visit(code, at + 4, length) + at = next + } + } + if (type === 0x0a0d0d0a) { + if (start !== 0 || size < 28 || u16(start + 12) !== 1 || u16(start + 14) !== 0) + throw Error("Expected one PCAPNG 1.0 section") + } else if (type === 1) { + if (size < 20 || interfaces.length >= 64 || u16(start + 8) !== linkType) + throw Error("Unexpected PCAPNG interface or link type") + const info = { snap: u32(start + 12), units: 1000000n, offset: 0n } + const seen = new Set() + options(start + 16, (code, at, length) => { + if (code !== 9 && code !== 14) return + if (seen.has(code)) throw Error("Duplicate PCAPNG timestamp option") + seen.add(code) + if (code === 9) { + if (length !== 1) throw Error("Invalid PCAPNG timestamp resolution") + const resolution = bytes[at] + info.units = (resolution & 128 ? 2n : 10n) ** BigInt(resolution & 127) + } else { + if (length !== 8) throw Error("Invalid PCAPNG timestamp offset") + info.offset = little ? bytes.readBigInt64LE(at) : bytes.readBigInt64BE(at) + } + }) + interfaces.push(info) + } else if (type === 6) { + if (size < 32 || frames.length >= 64) throw Error("Incomplete or oversized PCAPNG capture") + const info = interfaces[u32(start + 8)] + const length = u32(start + 20), data = start + 28, next = data + Math.ceil(length / 4) * 4 + if (!info || length === 0 || length !== u32(start + 24) || length > snapLength || + (info.snap !== 0 && length > info.snap) || next > end - 4) + throw Error("Incomplete or oversized PCAPNG frame") + const ticks = (BigInt(u32(start + 12)) << 32n) | BigInt(u32(start + 16)) + const timestamp = ticks * 1000000000n / info.units + info.offset * 1000000000n + if (timestamp < 0n || timestamp > 0xffffffffn * 1000000000n + 999999999n) + throw Error("PCAPNG timestamp outside permitted range") + options(next, () => {}) + frames.push({ timestamp, frame: bytes.subarray(data, data + length) }) + } else if (type !== 5) { + throw Error("Unsupported PCAPNG block") + } + start = end + } + return frames +} diff --git a/backend/src/test/traffic-flow-chr-readiness.test.ts b/backend/src/test/traffic-flow-chr-readiness.test.ts new file mode 100644 index 00000000..a7dae7d8 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-readiness.test.ts @@ -0,0 +1,72 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import { test } from "node:test" +import { inspectChrReadiness } from "./traffic-flow-chr-readiness.js" + +const now = 1700000005000 +function fixture() { + const devices = [1, 2].map(serverId => ({ + managementAddress: "198.51.100." + serverId, deviceId: "fixture-" + serverId, + version: "7.24.5 (stable)", exporterSource: "192.0.2." + serverId, serverId, + })) + const pins = { vpsHostKeySha256: "a".repeat(64), collectorInstance: "owned-backend", collectorAddress: "192.0.2.100", + interfaceName: "test0", testPort: 4739, baselinePort: 4740, devices } + const evidence = { observedAt: now - 1, vpsHostKeySha256: pins.vpsHostKeySha256, collectorInstance: pins.collectorInstance, + interfaceName: pins.interfaceName, interfaceAddresses: [pins.collectorAddress], collectorPid: 42, + udpOwners: [{ pid: 42, address: pins.collectorAddress, port: pins.testPort }], + worker: { alive: true, bound: true, address: "192.0.2.100:4739" }, + settings: { enabled: true, collectorIp: pins.collectorAddress, flowListenPort: pins.testPort, + listenerBound: true, listenerAddress: "192.0.2.100:4739" }, devices: structuredClone(devices) } + return { pins, evidence } +} +function capture() { + const bytes = Buffer.alloc(24 + 2 * 60) + bytes.writeUInt32LE(0xa1b2c3d4) + bytes.writeUInt16LE(2, 4); bytes.writeUInt16LE(4, 6) + bytes.writeUInt32LE(4124, 16); bytes.writeUInt32LE(101, 20) + for (const [index, at] of [24, 84].entries()) { + bytes.writeUInt32LE(1700000000, at); bytes.writeUInt32LE(44, at + 8); bytes.writeUInt32LE(44, at + 12) + const ip = bytes.subarray(at + 16, at + 60) + ip[0] = 0x45; ip.writeUInt16BE(44, 2); ip[9] = 17 + Buffer.from([192, 0, 2, index + 1, 192, 0, 2, 100]).copy(ip, 12) + ip.writeUInt16BE(2055, 20); ip.writeUInt16BE(4740, 22); ip.writeUInt16BE(24, 24) + ip.writeUInt16BE(10, 28); ip.writeUInt16BE(16, 30) + } + return { bytes, sha256: createHash("sha256").update(bytes).digest("hex") } +} + +test("pinned identity and owned bind do not fill a missing ingress observation", () => { + const { pins, evidence } = fixture() + const result = inspectChrReadiness(pins, evidence, undefined, now) + assert.equal(result.identityMatched, true) + assert.equal(result.collectorBindMatched, true) + assert.equal(result.ingressStatus, "missing") + assert.equal(result.readyForReview, false) + assert.deepEqual(result.targetScopes.map(row => [row.deviceId, row.fields["src-address"], row.fields.port]), [ + ["fixture-1", "192.0.2.1", "4739"], ["fixture-2", "192.0.2.2", "4739"], + ]) +}) + +test("requires fresh pinned observations, exact listener address and exclusive UDP owner", () => { + for (const mutate of [ + (f: ReturnType) => { f.evidence.devices[0].version = "different" }, + (f: ReturnType) => { f.evidence.observedAt = now - 120001 }, + (f: ReturnType) => { f.evidence.udpOwners[0].pid = 999 }, + (f: ReturnType) => { f.evidence.udpOwners[0].address = "0.0.0.0" }, + (f: ReturnType) => { f.evidence.udpOwners.push({ ...f.evidence.udpOwners[0] }) }, + (f: ReturnType) => { f.evidence.worker.alive = false }, + ]) { + const f = fixture(); mutate(f) + assert.equal(inspectChrReadiness(f.pins, f.evidence, capture(), now).readyForReview, false) + } +}) + +test("two-source scoped baseline pcap is checked separately from the owned test port", () => { + const { pins, evidence } = fixture(), input = capture() + const result = inspectChrReadiness(pins, evidence, input, now) + assert.equal(result.ingressStatus, "matched") + assert.equal(result.readyForReview, true) + assert.equal(result.actualNativeAcceptance, false) + assert.equal(inspectChrReadiness(pins, evidence, { ...input, sha256: "0".repeat(64) }, now).ingressStatus, "refused") + assert.equal(inspectChrReadiness(pins, { ...evidence, observedAt: now + 600001 }, input, now + 600001).ingressStatus, "refused") +}) diff --git a/backend/src/test/traffic-flow-chr-readiness.ts b/backend/src/test/traffic-flow-chr-readiness.ts new file mode 100644 index 00000000..e4a87923 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-readiness.ts @@ -0,0 +1,104 @@ +/** Local preflight report from reviewed VPS/CHR observations and optional pcap. */ +import { createHash } from "node:crypto" +import { open } from "node:fs/promises" +import { isIPv4 } from "node:net" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import { isDeepStrictEqual } from "node:util" +import { z } from "zod" +import { projectChrWire } from "./traffic-flow-chr-wire.js" + +const ip = z.string().max(15).refine(isIPv4) +const port = z.number().int().min(1).max(65535) +const id = z.string().min(1).max(128) +const sha = z.string().regex(/^[a-f0-9]{64}$/) +const device = z.object({ managementAddress: ip, deviceId: id, version: id, exporterSource: ip, serverId: z.number().int().positive().safe() }).strict() +const pinsSchema = z.object({ + vpsHostKeySha256: sha, collectorInstance: id, collectorAddress: ip, interfaceName: z.string().regex(/^[A-Za-z0-9_.-]{1,15}$/), + testPort: port, baselinePort: port, devices: z.tuple([device, device]), +}).strict() +const evidenceSchema = z.object({ + observedAt: z.number().int().nonnegative().safe(), + vpsHostKeySha256: sha, collectorInstance: id, interfaceName: z.string().max(15), + interfaceAddresses: z.array(ip).max(32), + collectorPid: z.number().int().positive().max(0x7fffffff), + udpOwners: z.array(z.object({ pid: z.number().int().positive().max(0x7fffffff), address: ip, port }).strict()).max(16), + worker: z.object({ alive: z.boolean(), bound: z.boolean(), address: z.string().max(32).nullable() }).strict(), + settings: z.object({ enabled: z.boolean(), collectorIp: ip, flowListenPort: port, listenerBound: z.boolean(), listenerAddress: z.string().max(32).nullable() }).strict(), + devices: z.tuple([device, device]), +}).strict() + +type Ingress = { bytes: Buffer; sha256: string } +export function inspectChrReadiness(rawPins: unknown, rawEvidence: unknown, ingress?: Ingress, now = Date.now()) { + const pins = pinsSchema.parse(rawPins), evidence = evidenceSchema.parse(rawEvidence) + const distinct = new Set([...pins.devices.map(row => row.exporterSource), pins.collectorAddress]).size === 3 && + new Set(pins.devices.map(row => row.deviceId)).size === 2 && new Set(pins.devices.map(row => row.managementAddress)).size === 2 && + new Set(pins.devices.map(row => row.serverId)).size === 2 && pins.testPort !== pins.baselinePort + const fresh = evidence.observedAt <= now && now - evidence.observedAt <= 120000 + const identityMatched = distinct && fresh && pins.vpsHostKeySha256 === evidence.vpsHostKeySha256 && + pins.collectorInstance === evidence.collectorInstance && isDeepStrictEqual(pins.devices, evidence.devices) + const endpoint = pins.collectorAddress + ":" + pins.testPort + const owners = evidence.udpOwners.filter(row => row.port === pins.testPort) + const collectorBindMatched = identityMatched && pins.interfaceName === evidence.interfaceName && + evidence.interfaceAddresses.includes(pins.collectorAddress) && owners.length === 1 && + owners[0].pid === evidence.collectorPid && owners[0].address === pins.collectorAddress && + evidence.worker.alive && evidence.worker.bound && evidence.worker.address === endpoint && + evidence.settings.enabled && evidence.settings.collectorIp === pins.collectorAddress && + evidence.settings.flowListenPort === pins.testPort && evidence.settings.listenerBound && evidence.settings.listenerAddress === endpoint + let ingressStatus: "missing" | "refused" | "matched" = ingress ? "refused" : "missing" + if (identityMatched && ingress) { + try { + const wire = projectChrWire(ingress.bytes, { + sources: [pins.devices[0].exporterSource, pins.devices[1].exporterSource], destination: pins.collectorAddress, port: pins.baselinePort, + }) + if (wire.captureSha256 === ingress.sha256 && + wire.frames.every(frame => frame.receivedMs <= now && frame.receivedMs >= now - 600000)) ingressStatus = "matched" + } catch { + // Keep the failed evidence state explicit without including packet data. + } + } + return { + identityMatched, collectorBindMatched, ingressStatus, + readyForReview: collectorBindMatched && ingressStatus === "matched", + targetScopes: pins.devices.map(row => ({ deviceId: row.deviceId, serverId: row.serverId, method: "PUT", path: "/ip/traffic-flow/target", + fields: { "dst-address": pins.collectorAddress, "src-address": row.exporterSource, port: String(pins.testPort), version: "ipfix" } })), + actualNativeAcceptance: false, + } +} + +async function pinnedFile(path: string, digest: string, max: number) { + sha.parse(digest) + const file = await open(path, "r") + try { + const info = await file.stat() + if (!info.isFile() || info.size < 1 || info.size > max) throw Error("Input outside file bound") + const bytes = Buffer.alloc(info.size + 1) + let size = 0 + while (size < bytes.length) { + const { bytesRead } = await file.read(bytes, size, bytes.length - size, size) + if (!bytesRead) break + size += bytesRead + } + const body = bytes.subarray(0, size) + if (size !== info.size || createHash("sha256").update(body).digest("hex") !== digest) throw Error("Input digest differs") + return body + } finally { + await file.close() + } +} + +async function main() { + const args = process.argv.slice(2) + if (args.length !== 4 && args.length !== 6) throw Error("Expected pinned inputs") + const [pins, pinsSha, evidence, evidenceSha, capture, captureSha] = args + const report = inspectChrReadiness( + JSON.parse((await pinnedFile(pins, pinsSha, 16384)).toString("utf8")), + JSON.parse((await pinnedFile(evidence, evidenceSha, 16384)).toString("utf8")), + capture ? { bytes: await pinnedFile(capture, captureSha, 512 * 1024), sha256: captureSha } : undefined, + ) + console.log(JSON.stringify(report)) + if (!report.readyForReview) process.exitCode = 2 +} +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch(() => { console.error("CHR readiness inputs refused"); process.exitCode = 1 }) +} diff --git a/backend/src/test/traffic-flow-chr-reconcile.test.ts b/backend/src/test/traffic-flow-chr-reconcile.test.ts new file mode 100644 index 00000000..cc34a3f9 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-reconcile.test.ts @@ -0,0 +1,75 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { reconcileOwnedChrFlow } from "./traffic-flow-chr-reconcile.js" + +type Options = Parameters[0] +function fixture() { + const bucketAt = "2026-11-04T00:01:00.000Z", until = "2026-11-04T00:02:00.000Z" + const flow = { src: "192.0.2.1", dst: "198.51.100.1", proto: 17, srcPort: 9000, dstPort: 9001, inIface: "11", outIface: "12", bytes: "1200", packets: "3" } + const frames = [0, 1, 0].map((exporter, index) => ({ + ordinal: index + 1, exporter, receivedMs: Date.parse(bucketAt) + 10000 + index, receivedNs: "0", + sourcePort: 2055, version: 10, domain: 7, payloadHex: "", + })) + const wire = { captureSha256: "a".repeat(64), scope: { sources: ["192.0.2.20", "192.0.2.30"] as const, destination: "192.0.2.100", port: 4739 }, frames } + const raw = [1, 2].map(serverId => ({ ...flow, serverId, bucketAt: new Date(bucketAt), bytes: serverId === 1 ? "2400" : "1200", packets: serverId === 1 ? "6" : "3" })) + const minutes = raw.map(({ serverId, bucketAt, bytes, packets }) => ({ serverId, bucketAt, bytes, packets })) + const calls: string[] = [] + let drift = false, publicDrift = false, active = true + const options: Options = { + wire, decoded: { captureSha256: wire.captureSha256, frames: frames.map(frame => ({ ordinal: frame.ordinal, flows: [flow] })) }, + serverIds: [1, 2], from: bucketAt, until, minutes: 2, + interfaceLabel: (id, index) => "ether-" + id + "-" + index, + assertCurrent: async () => { if (!active) throw Error("retired scope") }, + readOnlySnapshot: async work => work(async (sql, parameters) => { + assert.deepEqual(parameters, [[1, 2], bucketAt, until]) + assert.match(sql, /^SELECT /) + if (sql.includes("flow_buckets")) { calls.push("raw"); return structuredClone(raw) } + calls.push("minute"); return structuredClone(minutes) + }), + callTool: async (name, input) => { + assert.equal(name, "mm_netflow_talkers_get") + assert.deepEqual(input, { serverId: input.serverId, minutes: 2, serverOffset: 0, serverLimit: 1, offset: 0, limit: 200 }) + calls.push("public:" + input.serverId) + const row = raw[input.serverId - 1] + const bytes = Number(row.bytes), packets = Number(row.packets) + const result = { + serverIds: [input.serverId], from: "2026-11-04T00:00:00.000Z", to: until, topConversationsLimit: 20, + items: [{ ...row, serverId: String(row.serverId), inIface: publicDrift ? "wrong-iface" : options.interfaceLabel(row.serverId, row.inIface), + inIfaceIndex: row.inIface, bytes, packets, bps: bytes * 8 / 120 }], + total: 1, totalConversations: 1, observedExporters: 1, nextOffset: null, retainedRowsBytes: bytes, + coverageVerified: false, rowSumIsNetworkTotal: false, deduplicatedAcrossInterfaces: false, forwardingVerified: false, + } + if (drift) raw[0].bytes = "999" + return result + }, + } + return { options, raw, minutes, calls, drift: () => { drift = true }, publicDrift: () => { publicDrift = true }, revoke: () => { active = false } } +} + +test("reconciles wire records to exact raw/minute totals and independently projected scoped talkers", async () => { + const f = fixture() + assert.deepEqual(await reconcileOwnedChrFlow(f.options), + { rawRows: 2, minuteRows: 2, publicScopes: 2, localReconciliationMatched: true, nativeAcceptance: false }) + assert.deepEqual(f.calls, ["raw", "minute", "public:1", "public:2", "raw", "minute"]) +}) + +test("raw egress drift, minute count drift and foreign SQL rows refuse before MCP reads", async () => { + for (const mode of ["egress", "count", "foreign"]) { + const f = fixture() + if (mode === "egress") f.raw[0].outIface = "13" + else if (mode === "count") f.minutes[0].packets = "7" + else f.raw[0].serverId = 3 + await assert.rejects(reconcileOwnedChrFlow(f.options), /persisted SQL/) + assert.deepEqual(f.calls, ["raw", "minute"]) + } +}) + +test("rejects altered public label, drifting persisted window and revoked current scope", async () => { + const publicDrift = fixture(); publicDrift.publicDrift() + await assert.rejects(reconcileOwnedChrFlow(publicDrift.options), /Public retained/) + const drift = fixture(); drift.drift() + await assert.rejects(reconcileOwnedChrFlow(drift.options), /persisted SQL/) + const revoked = fixture(); revoked.revoke() + await assert.rejects(reconcileOwnedChrFlow(revoked.options), /retired scope/) + assert.deepEqual(revoked.calls, []) +}) diff --git a/backend/src/test/traffic-flow-chr-reconcile.ts b/backend/src/test/traffic-flow-chr-reconcile.ts new file mode 100644 index 00000000..84af55fb --- /dev/null +++ b/backend/src/test/traffic-flow-chr-reconcile.ts @@ -0,0 +1,147 @@ +/** Independent conservation check for the owned, drained collector database. + * SQL and MCP transports must be scoped by the enclosing runner. */ +import { isIP } from "node:net" +import { isDeepStrictEqual } from "node:util" +import type { projectChrWire } from "./traffic-flow-chr-wire.js" + +type Wire = ReturnType +type Counters = { bytes: string; packets: string } +type Key = { serverId: number; bucketAt: string; src: string; dst: string; proto: number; srcPort: number; dstPort: number; inIface: string; outIface: string } +type Flow = Omit & Counters +type Raw = Key & Counters +type Minute = Pick & Counters +type Query = (text: string, parameters: readonly unknown[]) => Promise +type Options = { + wire: Wire + serverIds: readonly [number, number] + decoded: { captureSha256: string; frames: readonly { ordinal: number; flows: readonly Flow[] }[] } + from: string + until: string + minutes: number + interfaceLabel: (serverId: number, ifIndex: string) => string + readOnlySnapshot: (work: (query: Query) => Promise) => Promise + callTool: (name: "mm_netflow_talkers_get", input: { serverId: number; minutes: number; serverOffset: 0; serverLimit: 1; offset: 0; limit: 200 }) => Promise + assertCurrent: () => Promise +} +const rawSql = `SELECT server_id AS "serverId", bucket_at AS "bucketAt", host(src) AS src, host(dst) AS dst, proto, + src_port AS "srcPort", dst_port AS "dstPort", in_iface AS "inIface", out_iface AS "outIface", bytes::text, packets::text + FROM flow_buckets WHERE server_id = ANY($1::bigint[]) AND bucket_at >= $2 AND bucket_at < $3 LIMIT 201` +const minuteSql = `SELECT server_id AS "serverId", bucket_at AS "bucketAt", bytes::text, packets::text + FROM flow_minute_stats WHERE server_id = ANY($1::bigint[]) AND bucket_at >= $2 AND bucket_at < $3 LIMIT 21` +const stable = (rows: unknown[]) => rows.map(row => JSON.stringify(row)).sort() +const conversation = (row: Key) => JSON.stringify([row.serverId, row.src, row.dst, row.proto, row.srcPort, row.dstPort, row.inIface]) +const minute = (row: Pick) => JSON.stringify([row.serverId, row.bucketAt]) +const rawKey = (row: Key) => JSON.stringify([row.bucketAt, conversation(row)]) +const rowFields = (row: Raw) => [row.serverId, row.bucketAt, row.src, row.dst, row.proto, row.srcPort, row.dstPort, row.inIface, row.outIface, row.bytes, row.packets] +const minuteFields = (row: Minute) => [row.serverId, row.bucketAt, row.bytes, row.packets] +function must(condition: unknown, reason: string): asserts condition { + if (!condition) throw Error(reason) +} +function count(value: unknown) { + must(typeof value === "string" && /^(?:0|[1-9]\d{0,18})$/.test(value) && BigInt(value) <= 9223372036854775807n, "Invalid decimal flow counter") + return BigInt(value) +} +function number(value: bigint) { + must(value <= BigInt(Number.MAX_SAFE_INTEGER), "Public flow counter exceeds exact integer range") + return Number(value) +} +function time(value: unknown) { + const text = value instanceof Date && Number.isFinite(value.getTime()) ? value.toISOString() : value + must(typeof text === "string" && Number.isFinite(Date.parse(text)) && new Date(text).toISOString() === text, "Invalid canonical flow timestamp") + return text +} +function add(rows: Map, key: string, row: T) { + const prior = rows.get(key) + rows.set(key, { ...row, bytes: String(count(row.bytes) + count(prior?.bytes ?? "0")), packets: String(count(row.packets) + count(prior?.packets ?? "0")) }) +} +function object(value: unknown): Record { + must(value !== null && typeof value === "object" && !Array.isArray(value), "Invalid flow row") + return value as Record +} +function storedIdentity(value: unknown) { + const row = object(value) + const serverId = typeof row.serverId === "string" && /^[1-9]\d{0,15}$/.test(row.serverId) ? Number(row.serverId) : row.serverId + must(typeof serverId === "number" && Number.isSafeInteger(serverId) && serverId > 0, "Invalid stored flow identity") + return { ...row, serverId, bucketAt: time(row.bucketAt) } +} + +export async function reconcileOwnedChrFlow(options: Options) { + const ids = [...options.serverIds] + const from = time(options.from), until = time(options.until) + must(ids.length === 2 && new Set(ids).size === 2 && ids.every(id => Number.isSafeInteger(id) && id > 0) && + Date.parse(until) > Date.parse(from) && Date.parse(until) - Date.parse(from) <= 10 * 60_000 && + Number.isInteger(options.minutes) && options.minutes >= 1 && options.minutes <= 15, "Invalid reconciliation scope") + const { wire, decoded } = options + must(decoded.captureSha256 === wire.captureSha256 && wire.frames.length >= 2 && wire.frames.length <= 64 && + wire.frames.length === decoded.frames.length, "Decoded evidence is not bound to all wire frames") + const raw = new Map(), rollup = new Map() + let records = 0 + for (const [index, frame] of wire.frames.entries()) { + const independent = decoded.frames[index] + must(independent.ordinal === frame.ordinal && independent.flows.length <= 64, "Decoded frame ordinal or size differs") + for (const flow of independent.flows) { + const serverId = ids[frame.exporter], bucketAt = new Date(Math.floor(frame.receivedMs / 60000) * 60000).toISOString() + must(serverId !== undefined && bucketAt >= from && bucketAt < until && ++records <= 200 && + isIP(flow.src) && isIP(flow.dst) && Number.isInteger(flow.proto) && flow.proto >= 0 && flow.proto <= 255 && + [flow.srcPort, flow.dstPort].every(port => Number.isInteger(port) && port >= 0 && port <= 65535) && + [flow.inIface, flow.outIface].every(iface => /^(?:0|[1-9]\d{0,9})$/.test(iface)) && + count(flow.bytes) > 0n && count(flow.packets) > 0n, "Decoded flow is outside the owned reconciliation scope") + const row = { ...flow, serverId, bucketAt } + const key = rawKey(row), previous = raw.get(key) + must(!previous || previous.outIface === row.outIface, "Egress interface changed within an aggregate") + add(raw, key, row) + add(rollup, minute(row), { serverId, bucketAt, bytes: row.bytes, packets: row.packets }) + } + } + const expected = [...raw.values()] + must(ids.every(id => expected.some(row => row.serverId === id)), "Both exporters need positive decoded traffic") + const read = async () => { + await options.assertCurrent() + return options.readOnlySnapshot(async query => { + const parameters = [ids, from, until] + await options.assertCurrent() + const stored = await query(rawSql, parameters) + await options.assertCurrent() + const minutes = await query(minuteSql, parameters) + await options.assertCurrent() + must(stored.length <= 200 && minutes.length <= 20, "SQL reconciliation exceeded the row budget") + const rows = stored.map(row => storedIdentity(row) as Raw) + const totals = minutes.map(row => storedIdentity(row) as Minute) + must(isDeepStrictEqual(stable(rows.map(rowFields)), stable(expected.map(rowFields))) && + isDeepStrictEqual(stable(totals.map(minuteFields)), stable([...rollup.values()].map(minuteFields))), "Wire flow counters or identities differ from persisted SQL") + return rows + }) + } + const stored = await read() + for (const serverId of ids) { + await options.assertCurrent() + const result = object(await options.callTool("mm_netflow_talkers_get", { serverId, minutes: options.minutes, serverOffset: 0, serverLimit: 1, offset: 0, limit: 200 })) + await options.assertCurrent() + const since = time(result.from), end = time(result.to) + const rows = stored.filter(row => row.serverId === serverId) + must(isDeepStrictEqual(result.serverIds, [serverId]) && Date.parse(end) - Date.parse(since) === options.minutes * 60000 && + rows.every(row => row.bucketAt >= since && row.bucketAt <= end) && result.coverageVerified === false && + result.rowSumIsNetworkTotal === false && result.deduplicatedAcrossInterfaces === false && result.forwardingVerified === false, + "Public scope, window or evidence semantics differ") + const aggregate = new Map() + for (const row of rows) add(aggregate, conversation(row), row) + const limit = result.topConversationsLimit + must(typeof limit === "number" && Number.isSafeInteger(limit) && limit >= aggregate.size && limit <= 1000 && + result.total === aggregate.size && result.totalConversations === aggregate.size && + result.observedExporters === 1 && result.nextOffset === null && Array.isArray(result.items) && result.items.length === aggregate.size, + "Public result is truncated or has different conversation totals") + const ordered = [...aggregate.entries()].sort(([keyA, a], [keyB, b]) => number(count(b.bytes)) - number(count(a.bytes)) || keyA.localeCompare(keyB)) + const expectedItems = ordered.map(([, row]) => [ + String(serverId), row.src, row.dst, row.proto, row.srcPort, row.dstPort, row.inIface, + options.interfaceLabel(serverId, row.inIface), number(count(row.bytes)), number(count(row.packets)), number(count(row.bytes)) * 8 / (options.minutes * 60), + ]) + const items = result.items.map(value => { + const row = object(value) + return [row.serverId, row.src, row.dst, row.proto, row.srcPort, row.dstPort, row.inIfaceIndex, row.inIface, row.bytes, row.packets, row.bps] + }) + must(isDeepStrictEqual(items, expectedItems) && result.retainedRowsBytes === number(rows.reduce((sum, row) => sum + count(row.bytes), 0n)), + "Public retained conversations differ from independently aggregated SQL") + } + await read() + return { rawRows: raw.size, minuteRows: rollup.size, publicScopes: ids.length, localReconciliationMatched: true, nativeAcceptance: false } +} diff --git a/backend/src/test/traffic-flow-chr-route-conntrack.test.ts b/backend/src/test/traffic-flow-chr-route-conntrack.test.ts new file mode 100644 index 00000000..4fced343 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-route-conntrack.test.ts @@ -0,0 +1,112 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createChrRouteConntrackReader } from "./traffic-flow-chr-route-conntrack.js" + +function fixture() { + const calls: string[] = [] + const catalog: Record[]> = { + "/system/identity": [{ name: "chr-a" }], + "/system/resource": [{ version: "7.24.5 (stable)" }], + "/ip/settings": [{ "ip-forward": "true", "allow-fast-path": "false" }], + "/routing/rule": [], + "/ip/vrf": [{ ".id": "*1", name: "main", interfaces: "all" }], + "/routing/table": [{ ".id": "*1", name: "main", fib: "true" }], + "/interface": [{ ".id": "*2", name: "nfedge", type: "wg", running: "true", disabled: "false" }], + "/ip/address": [{ ".id": "*3", address: "198.18.241.2/30", interface: "nfedge", disabled: "false" }], + "/routing/route": [{ ".id": "*4", "routing-table": "main", "dst-address": "198.18.240.2/32", + gateway: "198.18.241.1", "immediate-gw": "198.18.241.1%nfedge", active: "true", disabled: "false" }], + "/ip/firewall/connection/tracking": [{ enabled: "yes" }], + "/ip/firewall/connection": [], + } + const config = { + identity: "chr-a", version: "7.24.5 (stable)", + routes: [{ routeId: "*4", destination: "198.18.240.2", nextHop: "198.18.241.1", + interfaceName: "nfedge", interfaceId: "*2", addressId: "*3", interfaceCidr: "198.18.241.2/30" }], + udp: [{ source: { address: "198.18.240.1", port: 50001 }, destination: { address: "198.18.240.2", port: 50002 } }], + assertCurrent: async () => {}, + client: { async get(path: string, timeout?: number, _signal?: AbortSignal, bytes?: number, fence?: () => Promise) { + assert.ok(timeout && timeout <= 1800) + assert.equal(bytes, 512 * 1024) + await fence?.() + calls.push(path) + assert.ok(Object.hasOwn(catalog, path)) + return structuredClone(catalog[path]) as T + } }, + } + return { calls, catalog, config, read: createChrRouteConntrackReader(config) } +} +const foreignUdp = { ".id": "*1", protocol: "udp", "src-address": "192.0.2.1:50001", "dst-address": "192.0.2.2:50002", + "reply-src-address": "192.0.2.2:50002", "reply-dst-address": "192.0.2.1:50001" } + +test("owned two-interface profile permits observed FastPath flag and auto only with empty conntrack", async () => { + const { catalog, config } = fixture() + const [route] = config.routes + const edge = "nfa012bcdeea", middle = "nfa012bcdema" + const owned = createChrRouteConntrackReader({ ...config, profile: "owned-wg-empty-firewall", routes: [ + { ...route, interfaceName: edge }, + { ...route, interfaceName: middle, interfaceId: "*5", addressId: "*6", routeId: "*7", + interfaceCidr: "198.18.241.5/30", destination: "198.18.240.1", nextHop: "198.18.241.6" }, + ] }) + catalog["/ip/settings"][0]["allow-fast-path"] = "true" + catalog["/ip/firewall/connection/tracking"][0].enabled = "auto" + catalog["/interface"][0].name = edge + catalog["/interface"].push({ ...catalog["/interface"][0], ".id": "*5", name: middle }) + catalog["/ip/address"][0].interface = edge + catalog["/ip/address"].push({ ...catalog["/ip/address"][0], ".id": "*6", interface: middle, address: "198.18.241.5/30" }) + catalog["/routing/route"][0]["immediate-gw"] = "198.18.241.1%" + edge + catalog["/routing/route"].push({ ...catalog["/routing/route"][0], ".id": "*7", "dst-address": "198.18.240.1/32", + gateway: "198.18.241.6", "immediate-gw": "198.18.241.6%" + middle }) + assert.deepEqual((await owned()).ownedInterfaces, [edge, middle]) + catalog["/ip/firewall/connection"].push(foreignUdp) + await assert.rejects(owned(), /not observed empty/) +}) + +test("reads complete bounded native catalogs with identity fences and stable exact path fingerprint", async () => { + const { calls, catalog, read } = fixture() + const first = await read() + assert.equal(first.routesVerified, true) + assert.equal(first.freshConntrackTuples, true) + assert.match(first.fingerprint, /^[a-f0-9]{64}$/) + assert.deepEqual(calls, [...Object.keys(catalog).slice(0, 5), "/interface", "/ip/address", "/routing/table", + "/routing/route", "/ip/firewall/connection/tracking", "/ip/firewall/connection", "/system/identity", "/system/resource"]) + catalog["/ip/firewall/connection"].push(foreignUdp) + assert.deepEqual(await read(), first) + catalog["/routing/route"][0].distance = "2" + assert.notEqual((await read()).fingerprint, first.fingerprint) +}) + +test("accepts the owned scoped gateway and skips explicitly typed non-IP link routes", async () => { + const { catalog, read } = fixture() + catalog["/routing/route"][0].gateway = "198.18.241.1%nfedge" + catalog["/routing/route"].push({ ".id": "*5", "routing-table": "main", afi: "link", "dst-address": "nfedge" }) + assert.equal((await read()).routesVerified, true) + catalog["/routing/route"][1].afi = "ip" + await assert.rejects(read(), /invalid prefix/) +}) + +test("refuses original or reply tuple reuse and malformed nonmatching UDP rows", async () => { + for (const fields of [ + { "src-address": "198.18.240.1:50001", "dst-address": "198.18.240.2:50002" }, + { "reply-src-address": "198.18.240.2:50002", "reply-dst-address": "198.18.240.1:50001" }, + { "reply-src-address": undefined }, + ]) { + const { catalog, read } = fixture() + catalog["/ip/firewall/connection"].push({ ...foreignUdp, ...fields }) + await assert.rejects(read(), /tracked|incomplete UDP/) + } +}) + +test("refuses competing routes, routing policy, unproven conntrack and changed device identity", async () => { + const competitor = fixture() + competitor.catalog["/routing/route"].push({ ...competitor.catalog["/routing/route"][0], ".id": "*5" }) + await assert.rejects(competitor.read(), /exact active host route/) + const policy = fixture() + policy.catalog["/routing/rule"].push({ ".id": "*1", disabled: "false" }) + await assert.rejects(policy.read(), /active routing policy/) + const tracking = fixture() + tracking.catalog["/ip/firewall/connection/tracking"][0].enabled = "auto" + await assert.rejects(tracking.read(), /not explicitly enabled/) + const identity = fixture() + identity.catalog["/system/identity"][0].name = "foreign" + await assert.rejects(identity.read(), /device identity changed/) +}) diff --git a/backend/src/test/traffic-flow-chr-route-conntrack.ts b/backend/src/test/traffic-flow-chr-route-conntrack.ts new file mode 100644 index 00000000..8e0be358 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-route-conntrack.ts @@ -0,0 +1,160 @@ +import { createHash } from "node:crypto" +import { isIP, isIPv4 } from "node:net" +import { performance } from "node:perf_hooks" +import type { MikrotikClient } from "../services/mikrotik.js" +import { isChrWireGuardInterfaceType } from "./traffic-flow-chr-transit-fixture.js" + +type Row = Record +type Socket = { address: string; port: number } +type UdpTuple = { source: Socket; destination: Socket } +type RoutePin = { routeId: string; destination: string; nextHop: string; interfaceName: string; + interfaceId: string; addressId: string; interfaceCidr: string } +type Options = { + client: Pick + identity: string + version: string + routes: readonly RoutePin[] + udp: readonly UdpTuple[] + assertCurrent: () => Promise + profile?: "owned-wg-empty-firewall" +} + +const deny = (message: string): never => { throw Error("RouterOS route/conntrack proof refused: " + message) } +const id = (value: unknown) => typeof value === "string" && /^\*[a-f0-9]{1,16}$/i.test(value) +const yes = (value: unknown) => value === true || value === "true" || value === "yes" +const no = (value: unknown) => value === false || value === "false" || value === "no" +const port = (value: number) => Number.isInteger(value) && value >= 1 && value <= 65535 +const socketKey = (value: Socket) => value.address + ":" + value.port +const endpointsKey = (a: string, b: string) => [a, b].sort().join("/") +const canonicalRow = (row: Row) => Object.fromEntries(Object.keys(row).sort().map(key => [key, row[key]])) +const ipNumber = (address: string) => address.split(".").reduce((sum, octet) => sum * 256 + Number(octet), 0) + +function prefix(value: unknown) { + if (typeof value !== "string") return deny("invalid prefix") + const [address, length, ...rest] = value.split("/") + const family = isIP(address) + if (!family || rest.length || !/^(0|[1-9][0-9]{0,2})$/.test(length) || Number(length) > (family === 4 ? 32 : 128)) { + return deny("invalid prefix") + } + return { address, length: Number(length), family } +} +function endpoint(value: unknown) { + if (typeof value !== "string") return deny("incomplete UDP conntrack row") + const match = /^([0-9.]+):([1-9][0-9]{0,4})$/.exec(value) + if (!match || !isIPv4(match[1]) || !port(Number(match[2]))) return deny("unsupported UDP conntrack endpoint") + return socketKey({ address: match[1], port: Number(match[2]) }) +} +function sameSubnet(cidr: string, nextHop: string) { + const net = prefix(cidr) + const size = 2 ** (32 - net.length) + return net.family === 4 && net.length < 32 && net.address !== nextHop && + Math.floor(ipNumber(net.address) / size) === Math.floor(ipNumber(nextHop) / size) +} + +/** Narrow main-table reader for an already provisioned fixture. An authoritative + * caller brackets policy evaluation with independent fresh calls to this reader. */ +export function createChrRouteConntrackReader(options: Options) { + const { client, assertCurrent, profile } = options + const { identity, version, routes, udp } = structuredClone({ + identity: options.identity, version: options.version, routes: options.routes, udp: options.udp, + }) + if (!identity || !version || !routes.length || routes.length > 16 || !udp.length || udp.length > 32 || + new Set(routes.map(pin => pin.destination)).size !== routes.length || routes.some(pin => + !id(pin.routeId) || !id(pin.interfaceId) || !id(pin.addressId) || + !isIPv4(pin.destination) || !isIPv4(pin.nextHop) || !/^[\w.-]{1,64}$/.test(pin.interfaceName) || + !sameSubnet(pin.interfaceCidr, pin.nextHop)) || + udp.some(tuple => [tuple.source, tuple.destination].some(value => !isIPv4(value.address) || !port(value.port)))) deny("invalid fixture scope") + const pinnedTuples = new Set(udp.map(tuple => endpointsKey(socketKey(tuple.source), socketKey(tuple.destination)))) + const ownedWg = profile === "owned-wg-empty-firewall" + const ownedInterfaces = [...new Set(routes.map(pin => pin.interfaceName))].sort() + if (ownedWg && (routes.some(pin => !/^nf[a-f0-9]{8}(ea|ma|mb|eb)$/.test(pin.interfaceName)) || + new Set(routes.map(pin => pin.interfaceName.slice(0, -2))).size !== 1 || + !["ea,ma", "eb,mb"].includes(ownedInterfaces.map(name => name.slice(-2)).join(",")) || + new Set(routes.map(pin => pin.interfaceId)).size !== 2)) deny("owned WireGuard interface scope differs") + + return async function readPathEvidence() { + const deadline = performance.now() + 12000 + async function fence() { + if (performance.now() >= deadline) deny("expired evidence") + await assertCurrent() + if (performance.now() >= deadline) deny("expired evidence") + } + async function read(path: string, limit: number, single = false) { + await fence() + const response = await client.get(path, Math.min(1800, Math.ceil(deadline - performance.now())), + undefined, 512 * 1024, fence) + await fence() + const rows = Array.isArray(response) ? response : single ? [response] : null + if (!rows || rows.length > limit || single && rows.length !== 1 || + rows.some(row => !row || typeof row !== "object" || Array.isArray(row))) return deny("incomplete catalog") + return rows as Row[] + } + async function identityFence() { + const [device] = await read("/system/identity", 1, true) + const [resource] = await read("/system/resource", 1, true) + if (device.name !== identity || resource.version !== version) deny("device identity changed") + } + function requireCatalogIds(rows: Row[]) { + if (rows.some(row => !id(row[".id"])) || new Set(rows.map(row => row[".id"])).size !== rows.length) deny("invalid catalog IDs") + return rows + } + await identityFence() + const [ip] = await read("/ip/settings", 1, true) + if (!yes(ip["ip-forward"]) || (!no(ip["allow-fast-path"]) && !(ownedWg && yes(ip["allow-fast-path"])))) { + deny("forwarding or fast-path state unknown") + } + const policy = requireCatalogIds(await read("/routing/rule", 128)) + if (policy.some(row => !yes(row.disabled))) deny("active routing policy") + const vrfs = requireCatalogIds(await read("/ip/vrf", 64)) + if (vrfs.length !== 1 || vrfs[0].name !== "main" || vrfs[0].interfaces !== "all" || yes(vrfs[0].disabled)) deny("non-default VRF policy") + const interfaces = requireCatalogIds(await read("/interface", 256)) + const addresses = requireCatalogIds(await read("/ip/address", 256)) + const main = requireCatalogIds(await read("/routing/table", 64)).filter(row => row.name === "main") + if (main.length !== 1 || !yes(main[0].fib) || yes(main[0].disabled)) deny("main FIB unconfirmed") + const candidates = requireCatalogIds(await read("/routing/route", 2048)) + for (const row of candidates) { + if (typeof row["routing-table"] !== "string" || !row["routing-table"]) deny("incomplete route catalog") + if (row.afi === "link") { + if (typeof row["dst-address"] !== "string" || !/^[\w.-]{1,64}$/.test(row["dst-address"])) deny("invalid link route") + } else prefix(row["dst-address"]) + } + const bindings = routes.map(pin => { + const interfaceMatches = interfaces.filter(row => row.name === pin.interfaceName) + const iface = interfaceMatches[0] + const assigned = addresses.filter(row => row.address === pin.interfaceCidr) + const local = assigned[0] + if (interfaceMatches.length !== 1 || iface[".id"] !== pin.interfaceId || !isChrWireGuardInterfaceType(iface.type) || + !yes(iface.running) || !no(iface.disabled) || assigned.length !== 1 || local[".id"] !== pin.addressId || + local.interface !== pin.interfaceName || !no(local.disabled) || yes(local.invalid) || + (local["actual-interface"] !== undefined && local["actual-interface"] !== pin.interfaceName)) deny("interface address binding differs") + const exact = candidates.filter(row => row["routing-table"] === "main" && row["dst-address"] === pin.destination + "/32") + const row = exact[0] + if (exact.length !== 1 || row[".id"] !== pin.routeId || !yes(row.active) || !no(row.disabled) || + ![pin.nextHop, pin.nextHop + "%" + pin.interfaceName].includes(String(row.gateway)) || + row["immediate-gw"] !== pin.nextHop + "%" + pin.interfaceName || + yes(row.blackhole) || yes(row.unreachable) || yes(row.prohibit) || yes(row.ecmp)) deny("exact active host route unavailable") + return [pin, canonicalRow(row), canonicalRow(local), iface.type, iface.running, iface.disabled] + }) + const [tracking] = await read("/ip/firewall/connection/tracking", 1, true) + if (!yes(tracking.enabled) && !(ownedWg && tracking.enabled === "auto")) deny("conntrack is not explicitly enabled") + const connections = requireCatalogIds(await read("/ip/firewall/connection", 2048)) + if (ownedWg && tracking.enabled === "auto" && connections.length !== 0) deny("automatic conntrack is not observed empty") + for (const row of connections) { + const protocol = row.protocol + if (protocol === "udp" || protocol === "17") { + const original = endpointsKey(endpoint(row["src-address"]), endpoint(row["dst-address"])) + const reply = endpointsKey(endpoint(row["reply-src-address"]), endpoint(row["reply-dst-address"])) + if (pinnedTuples.has(original) || pinnedTuples.has(reply)) deny("UDP fixture tuple already tracked") + } else if (typeof protocol !== "string" || !/^(tcp|icmp|gre|ipsec-esp|ipsec-ah|sctp|udplite|ospf|ipv6-encap|0|[1-9][0-9]{0,2})$/.test(protocol)) { + deny("unsupported conntrack protocol") + } + } + await identityFence() + const fingerprint = createHash("sha256").update(JSON.stringify([identity, version, profile, ip["allow-fast-path"], tracking.enabled, bindings, + canonicalRow(main[0]), canonicalRow(vrfs[0]), [...pinnedTuples].sort()])).digest("hex") + return { routesVerified: true as const, freshConntrackTuples: true as const, fingerprint, + ...(profile && { profile, ownedInterfaces, + ownedRoutes: routes.map(pin => ({ destination: pin.destination, interfaceName: pin.interfaceName })), + udpPairs: [...pinnedTuples].sort() }) } + } +} diff --git a/backend/src/test/traffic-flow-chr-staged-profile.ts b/backend/src/test/traffic-flow-chr-staged-profile.ts new file mode 100644 index 00000000..409123df --- /dev/null +++ b/backend/src/test/traffic-flow-chr-staged-profile.ts @@ -0,0 +1,9 @@ +import { z } from "zod" +import { CHR_LEGACY_FIXTURE_DEADLINE_MAX_MS, CHR_STAGED_TARGET_CAPTURE_MAX_MS } from "./traffic-flow-chr-target-timing-profile.js" + +export const chrStagedCaptureProfileSchema = z.object({ + kind: z.literal("staged-disabled-target"), + captureMs: z.number().int().min(90000).max(CHR_STAGED_TARGET_CAPTURE_MAX_MS), + requiredCollectorWindowMs: z.number().int().positive().lt(CHR_LEGACY_FIXTURE_DEADLINE_MAX_MS), +}).strict().refine(profile => profile.requiredCollectorWindowMs > profile.captureMs + 210000, + "Staged collector window must include both measured phases and the cleanup reserve") diff --git a/backend/src/test/traffic-flow-chr-target-harness.test.ts b/backend/src/test/traffic-flow-chr-target-harness.test.ts new file mode 100644 index 00000000..7892493a --- /dev/null +++ b/backend/src/test/traffic-flow-chr-target-harness.test.ts @@ -0,0 +1,191 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createChrTargetHarness } from "./traffic-flow-chr-target-harness.js" + +type Options = Parameters[0] +function fixture() { + const baseline: Options["baseline"] = { deviceId: "chr-fixture-a", flow: { enabled: "true", interfaces: "ether1" }, + ipfix: Object.fromEntries(["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", "dst-port", "in-interface", "out-interface"].map(key => [key, "true"])), + targets: [{ ".id": "*3", "dst-address": "192.0.2.200", port: "4739", unknown: "retained" }] } + const live = structuredClone(baseline), effects: string[] = [] + let lostAck = false, wrongAck = false, extraChange = false, loseDeleteAck = false + let allowed = true, afterIntent: (() => void) | undefined + const options: Options = { + baseline, source: "192.0.2.1", destination: "192.0.2.100", port: 4739, + authorize: async () => { if (!allowed) throw Error("revoked") }, + read: async () => live, + checkpoint: async entry => { effects.push(entry.action + ":" + entry.phase + ":" + (entry.id ?? "")); if (entry.phase === "intent") afterIntent?.() }, + native: { + async put(path: string, body: Record, _timeout?: number, beforeDispatch?: () => Promise): Promise { + await beforeDispatch?.() + effects.push("PUT " + path) + live.targets.push({ ...body, ".id": "*A", disabled: body.disabled ?? "false", unknown: "owned" }) + if (extraChange) live.flow.interfaces = "all" + if (lostAck) throw Error("private native failure") + return { ".id": wrongAck ? "*B" : "*A" } as T + }, + async delete(path, _timeout, beforeDispatch) { + await beforeDispatch?.() + effects.push("DELETE " + path) + live.targets = live.targets.filter(row => row[".id"] !== "*A") + if (loseDeleteAck) throw Error("private native failure") + }, + }, + } + return { harness: createChrTargetHarness(options), options, live, effects, + loseAck: () => { lostAck = true }, wrongAck: () => { wrongAck = true }, extraChange: () => { extraChange = true }, + loseDeleteAck: () => { loseDeleteAck = true }, revoke: () => { allowed = false }, onIntent: (fn: () => void) => { afterIntent = fn } } +} + +function stagedFixture() { + const f = fixture() + f.options.baseline.flow["inactive-flow-timeout"] = f.live.flow["inactive-flow-timeout"] = "15s" + f.options.baseline.targets = [] + f.live.targets = [] + let loseEnableAck = false, mutateForeign = false + const harness = createChrTargetHarness({ + ...f.options, stagedDisabled: true, + verifyContext: async () => { f.effects.push("complete-context") }, + native: { ...f.options.native, + async patch(path: string, body: Record, _timeout?: number, beforeDispatch?: () => Promise): Promise { + await beforeDispatch?.() + assert.equal(path, "/ip/traffic-flow/target/*A") + assert.deepEqual(body, { disabled: "no" }) + f.effects.push("PATCH " + path) + f.live.targets.find(row => row[".id"] === "*A")!.disabled = "false" + if (mutateForeign) f.live.flow.interfaces = "drift" + if (loseEnableAck) throw Error("private acknowledgement failure") + return { ".id": "*A" } as T + }, + }, + }) + return { ...f, harness, loseEnableAck: () => { loseEnableAck = true }, mutateForeign: () => { mutateForeign = true } } +} + +const stagedTimer = { kind: "staged-ipfix-timeout-20s" as const, captureDeadlineAt: 240001, now: () => 1 } + +test("stages a disabled exact target and enables only the same ACK ID inside the separate capture window", async () => { + const f = stagedFixture() + await f.harness.create() + const row = f.live.targets.at(-1)! + assert.equal(row.disabled, "yes") + assert.equal(row["v9-template-timeout"], "20s") + await assert.rejects(f.harness.assertTimerReady(), /Verified owned/) + f.effects.length = 0 + assert.equal((await f.harness.enable(stagedTimer)).id, "*A") + assert.deepEqual(f.effects, [ + "enable:intent:*A", "complete-context", "PATCH /ip/traffic-flow/target/*A", + "enable:acknowledged:*A", "complete-context", "enable:verified:*A", + ]) + await f.harness.assertTimerReady() + await f.harness.remove() + assert.deepEqual(f.live, f.options.baseline) +}) + +test("staged enable refuses expiry, drift and lost acknowledgement without replay", async () => { + const expired = stagedFixture() + await expired.harness.create() + await assert.rejects(expired.harness.enable({ ...stagedTimer, captureDeadlineAt: 240002 }), /timing window/) + assert.equal(expired.effects.some(event => event.startsWith("PATCH ")), false) + const changed = stagedFixture() + await changed.harness.create() + changed.onIntent(() => { changed.live.flow.interfaces = "drift" }) + await assert.rejects(changed.harness.enable(stagedTimer), /inventory changed/) + assert.equal(changed.effects.some(event => event.startsWith("PATCH ")), false) + for (const fault of ["loseEnableAck", "mutateForeign"] as const) { + const f = stagedFixture() + await f.harness.create() + f[fault]() + await assert.rejects(f.harness.enable(stagedTimer), /outcome unknown/) + await assert.rejects(f.harness.enable(stagedTimer), /No verified disabled/) + await assert.rejects(f.harness.remove(), /No verified/) + assert.equal(f.effects.filter(event => event.startsWith("PATCH ")).length, 1) + } +}) + +test("creates one exact ACK-owned target and restores only its ID with durable checkpoints", async () => { + const f = fixture() + assert.deepEqual(await f.harness.create(), { id: "*A", verified: true, automaticReplay: false }) + assert.deepEqual(await f.harness.remove(), { id: "*A", restored: true, automaticReplay: false }) + assert.deepEqual(f.live, f.options.baseline) + assert.deepEqual(f.effects, ["create:intent:", "PUT /ip/traffic-flow/target", "create:acknowledged:*A", "create:verified:*A", + "delete:intent:*A", "DELETE /ip/traffic-flow/target/*A", "delete:acknowledged:*A", "delete:verified:*A"]) + await assert.rejects(f.harness.remove(), /No verified/) +}) + +test("lost or foreign ACK and unexpected changes refuse ownership without automatic deletion or replay", async () => { + for (const mode of ["loseAck", "wrongAck", "extraChange"] as const) { + const f = fixture(); f[mode]() + await assert.rejects(f.harness.create(), /outcome unknown/) + await assert.rejects(f.harness.create(), /already attempted/) + await assert.rejects(f.harness.remove(), /No verified/) + assert.equal(f.effects.filter(value => value.startsWith("PUT ")).length, 1) + assert.equal(f.effects.some(value => value.startsWith("DELETE ")), false) + } +}) + +test("dispatch rechecks authority, complete inventory and the exact owned row after the intent checkpoint", async () => { + const revoked = fixture(); revoked.onIntent(revoked.revoke) + await assert.rejects(revoked.harness.create(), /revoked/) + assert.deepEqual(revoked.effects, ["create:intent:"]) + const changed = fixture(); await changed.harness.create() + changed.onIntent(() => { changed.live.targets.find(row => row[".id"] === "*A")!.unknown = "replacement" }) + await assert.rejects(changed.harness.remove(), /inventory changed/) + assert.equal(changed.effects.some(value => value.startsWith("DELETE ")), false) +}) + +test("delete ACK loss remains unknown even when the fixture row was removed", async () => { + const f = fixture() + await f.harness.create(); f.loseDeleteAck() + await assert.rejects(f.harness.remove(), /outcome unknown/) + await assert.rejects(f.harness.remove(), /No verified/) + assert.equal(f.effects.filter(value => value.startsWith("DELETE ")).length, 1) + assert.equal(f.effects.includes("delete:verified:*A"), false) +}) + +test("optional timer writes one reviewed field and verifies its exact normalized readback", async () => { + const f = fixture() + f.options.baseline.flow["inactive-flow-timeout"] = f.live.flow["inactive-flow-timeout"] = "15s" + const originalPut = f.options.native.put + f.options.native.put = async (...args: Parameters) => { + assert.equal(args[1]["v9-template-timeout"], "20s") + assert.equal(Object.hasOwn(args[1], "v9-template-refresh"), false) + const ack = await originalPut(...args) + f.live.targets.at(-1)!["v9-template-timeout"] = "00:00:20" + return ack + } + const harness = createChrTargetHarness({ ...f.options, timerProfile: { kind: "bounded-ipfix-timeout-20s", captureDeadlineAt: 90001, now: () => 1 } }) + const receipt = await harness.create() + assert.equal(receipt.configuredTemplateTimeoutMs, 20000) + assert.equal(receipt.wireRefreshVerified, false) + await harness.assertTimerReady() + await harness.remove() +}) + +test("unsupported timer and elapsed window refuse without replay or guessed fallback", async () => { + for (const readback of [undefined, "20", "30s"]) { + const f = fixture() + f.options.baseline.flow["inactive-flow-timeout"] = f.live.flow["inactive-flow-timeout"] = "00:00:15" + const originalPut = f.options.native.put + f.options.native.put = async (...args: Parameters) => { + const ack = await originalPut(...args) + const row = f.live.targets.at(-1)! + if (readback === undefined) delete row["v9-template-timeout"] + else row["v9-template-timeout"] = readback + return ack + } + const harness = createChrTargetHarness({ ...f.options, timerProfile: { kind: "bounded-ipfix-timeout-20s", captureDeadlineAt: 90001, now: () => 1 } }) + await assert.rejects(harness.create(), /outcome unknown/) + await assert.rejects(harness.assertTimerReady()) + await assert.rejects(harness.remove(), /No verified/) + assert.equal(f.effects.filter(value => value.startsWith("PUT ")).length, 1) + } + const f = fixture() + f.options.baseline.flow["inactive-flow-timeout"] = f.live.flow["inactive-flow-timeout"] = "15s" + let now = 1 + const timerProfile = { kind: "bounded-ipfix-timeout-20s" as const, captureDeadlineAt: 90001, now: () => now } + const harness = createChrTargetHarness({ ...f.options, timerProfile }) + f.onIntent(() => { now = 26002 }) + await assert.rejects(harness.create(), /timing window/) + assert.equal(f.effects.filter(value => value.startsWith("PUT ")).length, 0) +}) diff --git a/backend/src/test/traffic-flow-chr-target-harness.ts b/backend/src/test/traffic-flow-chr-target-harness.ts new file mode 100644 index 00000000..3d52aab2 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-target-harness.ts @@ -0,0 +1,232 @@ +/** Target-only component for a reviewed two-CHR fixture. Transport and durable + * checkpoint ownership belong to the enclosing native runner. */ +import { isIPv4 } from "node:net" +import { isDeepStrictEqual } from "node:util" +import type { MikrotikClient } from "../services/mikrotik.js" +import { encodeRosId } from "../services/routeros-native-paths.js" + +import { runChrWrite, type ChrWriteAdmission } from "./traffic-flow-chr-write-admission.js" +import { CHR_STAGED_TARGET_CAPTURE_MAX_MS, CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS, CHR_TARGET_TEMPLATE_TIMEOUT_MS } from "./traffic-flow-chr-target-timing-profile.js" +export { CHR_STAGED_TARGET_CAPTURE_MAX_MS, CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS, CHR_TARGET_TEMPLATE_TIMEOUT_MS } from "./traffic-flow-chr-target-timing-profile.js" + +type Row = Record +type Inventory = { deviceId: string; flow: Row; ipfix: Row; targets: Row[] } +type Checkpoint = { action: "create" | "enable" | "delete"; phase: "intent" | "acknowledged" | "verified"; id?: string } +export type ChrTargetTimerProfile = { + kind: "bounded-ipfix-timeout-20s" + captureDeadlineAt: number + now?: () => number +} +export type ChrStagedTargetTimerProfile = Omit & { kind: "staged-ipfix-timeout-20s" } +const timerField = "v9-template-timeout" +const timeoutWire = "20s" +const captureMaxMs = 90000 +type Options = { + baseline: Inventory + relayWrite?: { side: "A" | "B"; admission: ChrWriteAdmission } + source: string + destination: string + port: number + read: () => Promise + authorize: () => Promise + verifyContext?: () => Promise + checkpoint: (entry: Checkpoint) => Promise +} & ({ + stagedDisabled?: false + native: Pick + timerProfile?: ChrTargetTimerProfile +} | { + stagedDisabled: true + native: Pick + timerProfile?: never +}) +const targetPath = "/ip/traffic-flow/target" +const isId = (value: unknown): value is string => typeof value === "string" && /^\*[a-f0-9]{1,16}$/i.test(value) +const yes = (value: unknown) => value === true || value === "true" || value === "yes" +const no = (value: unknown) => value === false || value === "false" || value === "no" +const ownedInactiveTimeout = (value: unknown) => value === "15s" || value === "00:00:15" +const requiredFields = ["bytes", "packets", "src-address", "dst-address", "protocol", "src-port", "dst-port", "in-interface", "out-interface"] +function must(value: unknown, message: string): asserts value { + if (!value) throw Error(message) +} +export function assertChrTargetTimerWindow(timer: ChrTargetTimerProfile | ChrStagedTargetTimerProfile, inactiveTimeout: unknown) { + const remaining = timer.captureDeadlineAt - (timer.now ?? Date.now)() + const max = timer.kind === "staged-ipfix-timeout-20s" ? CHR_STAGED_TARGET_CAPTURE_MAX_MS : captureMaxMs + must((timer.kind === "bounded-ipfix-timeout-20s" || timer.kind === "staged-ipfix-timeout-20s") && + ownedInactiveTimeout(inactiveTimeout) && + Number.isSafeInteger(timer.captureDeadlineAt) && Number.isSafeInteger(remaining) && + remaining >= CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS && remaining <= max, "Owned IPFIX timing window unavailable") +} +function snapshot(inventory: Inventory): Inventory { + must(typeof inventory.deviceId === "string" && inventory.deviceId.length > 0 && inventory.deviceId.length <= 128 && + inventory.flow && inventory.ipfix && Array.isArray(inventory.targets) && inventory.targets.length <= 64 && + Buffer.byteLength(JSON.stringify(inventory)) <= 64 * 1024, "Invalid target inventory") + must(inventory.targets.every(row => isId(row?.[".id"])) && + new Set(inventory.targets.map(row => row[".id"])).size === inventory.targets.length, "Invalid or duplicate target identity") + const copy = structuredClone(inventory) + copy.targets.sort((left, right) => String(left[".id"]).localeCompare(String(right[".id"]))) + return copy +} + +export function createChrTargetHarness(options: Options) { + const { read, authorize, checkpoint, native, source, destination, port, relayWrite } = options + const baseline = snapshot(options.baseline) + must(isIPv4(source) && isIPv4(destination) && source !== destination && + Number.isInteger(port) && port > 0 && port <= 65535, "Invalid owned target tuple") + must(yes(baseline.flow.enabled) && requiredFields.every(key => yes(baseline.ipfix[key])), "Traffic Flow or required IPFIX fields are unavailable") + must(!baseline.targets.some(row => row["dst-address"] === destination && String(row.port) === String(port)), "The target tuple already exists") + const staged = options.stagedDisabled === true + if (staged) { + must(options.timerProfile === undefined && baseline.targets.length === 0 && + ownedInactiveTimeout(baseline.flow["inactive-flow-timeout"]), "Disabled target staging prerequisites unavailable") + } + let timer: ChrTargetTimerProfile | ChrStagedTargetTimerProfile | undefined = options.timerProfile && { ...options.timerProfile } + const assertTimerWindow = () => { if (timer) assertChrTargetTimerWindow(timer, baseline.flow["inactive-flow-timeout"]) } + assertTimerWindow() + const fields = Object.freeze({ "dst-address": destination, "src-address": source, port: String(port), version: "ipfix", + ...(timer || staged ? { [timerField]: timeoutWire } : {}), ...(staged ? { disabled: "yes" } : {}) }) + const fieldsMatch = (row: Row) => Object.entries(fields).every(([key, value]) => key === timerField + ? Object.hasOwn(row, key) && (row[key] === timeoutWire || row[key] === "00:00:20") + : key === "disabled" ? yes(row.disabled) : String(row[key]) === value) + let phase: "new" | "creating" | "staged" | "enabling" | "owned" | "deleting" | "done" | "refused" = "new" + let owned: Inventory | undefined + let ownedId: string | undefined + const current = async (verifyContext = true) => { + await authorize() + const result = snapshot(await read()) + must(result.deviceId === baseline.deviceId, "Target device identity changed") + if (verifyContext) await options.verifyContext?.() + await authorize() + return result + } + const expect = async (value: Inventory, verifyContext = true) => { + must(isDeepStrictEqual(await current(verifyContext), value), "Target inventory changed") + } + return Object.freeze({ + async create() { + must(phase === "new", "Target creation already attempted") + phase = "creating" + let dispatched = false + try { + await expect(baseline) + await checkpoint({ action: "create", phase: "intent" }) + const ack = await runChrWrite(relayWrite?.admission, relayWrite && { step: "target:" + relayWrite.side + ":create", + side: relayWrite.side, deviceId: baseline.deviceId, method: "PUT", path: targetPath, body: fields }, + arm => native.put(targetPath, { ...fields }, 15000, async () => { + must(!dispatched, "Target creation already dispatched") + await expect(baseline) + must(!dispatched, "Target creation already dispatched") + assertTimerWindow() + dispatched = true + await arm() + })) + must(dispatched, "Target creation skipped dispatch fence") + const id = ack && typeof ack === "object" ? (ack as Row)[".id"] : undefined + must(isId(id) && !baseline.targets.some(row => row[".id"] === id), "Target creation has no exact new ACK ID") + await checkpoint({ action: "create", phase: "acknowledged", id }) + const result = await current() + const row = result.targets.find(item => item[".id"] === id) + must(row && (staged ? yes(row.disabled) : no(row.disabled)) && fieldsMatch(row), "ACK target differs on read-back") + must(isDeepStrictEqual({ ...result, targets: result.targets.filter(item => item[".id"] !== id) }, baseline), "Unrelated target inventory changed") + await checkpoint({ action: "create", phase: "verified", id }) + owned = result + ownedId = id + phase = staged ? "staged" : "owned" + return { id, verified: true as const, automaticReplay: false as const, + ...(timer ? { configuredTemplateTimeoutMs: CHR_TARGET_TEMPLATE_TIMEOUT_MS, wireRefreshVerified: false as const } : {}) } + } catch (error) { + phase = "refused" + if (dispatched) throw Error("Target creation outcome unknown. Automatic replay forbidden.") + throw error + } + }, + async enable(profile: ChrStagedTargetTimerProfile) { + must(options.stagedDisabled && phase === "staged" && owned && ownedId && + profile.kind === "staged-ipfix-timeout-20s", "No verified disabled target to enable") + const expected = owned, id = ownedId, native = options.native + const path = targetPath + "/" + encodeRosId(id) + const body = Object.freeze({ disabled: "no" }) + timer = { ...profile } + phase = "enabling" + let dispatched = false + try { + assertTimerWindow() + // The durable intent follows an exact local read. Complete context + // verification still runs immediately before dispatch and after ACK. + await expect(expected, false) + await checkpoint({ action: "enable", phase: "intent", id }) + const ack = await runChrWrite(relayWrite?.admission, relayWrite && { step: "target:" + relayWrite.side + ":enable", + side: relayWrite.side, deviceId: baseline.deviceId, method: "PATCH", path, body }, + arm => native.patch(path, { ...body }, 15000, async () => { + must(!dispatched, "Target enable already dispatched") + await expect(expected) + must(!dispatched, "Target enable already dispatched") + assertTimerWindow() + dispatched = true + await arm() + })) + must(dispatched && ack && typeof ack === "object" && (ack as Row)[".id"] === id, "Target enable has no exact ACK ID") + await checkpoint({ action: "enable", phase: "acknowledged", id }) + const result = await current() + const reduced = structuredClone(result) + const row = reduced.targets.find(item => item[".id"] === id) + must(row && no(row.disabled), "ACK target is not enabled") + row.disabled = expected.targets.find(item => item[".id"] === id)!.disabled + must(isDeepStrictEqual(reduced, expected), "Target enable changed unrelated inventory") + await checkpoint({ action: "enable", phase: "verified", id }) + owned = result + phase = "owned" + return { id, verified: true as const, automaticReplay: false as const, + configuredTemplateTimeoutMs: CHR_TARGET_TEMPLATE_TIMEOUT_MS, wireRefreshVerified: false as const } + } catch (error) { + phase = "refused" + if (dispatched) throw Error("Target enable outcome unknown. Automatic replay forbidden.") + throw error + } + }, + async assertTimerReady() { + must(timer && phase === "owned" && owned, "Verified owned IPFIX timer required") + assertTimerWindow() + await expect(owned) + assertTimerWindow() + }, + async assertStagedTimerReady() { + must(staged && timer && phase === "owned" && owned, "Verified staged IPFIX timer required") + assertTimerWindow() + // The caller supplies one complete pre-generation topology fence for + // both exact target reads. No native write occurs in this phase. + await expect(owned, false) + assertTimerWindow() + }, + async remove() { + must((phase === "owned" || phase === "staged") && owned && ownedId, "No verified owned target to delete") + const expected = owned, id = ownedId + phase = "deleting" + let dispatched = false + try { + await expect(expected) + await checkpoint({ action: "delete", phase: "intent", id }) + const path = targetPath + "/" + encodeRosId(id) + await runChrWrite(relayWrite?.admission, relayWrite && { step: "target:" + relayWrite.side + ":delete", + side: relayWrite.side, deviceId: baseline.deviceId, method: "DELETE", path, body: {} }, + arm => native.delete(path, 10000, async () => { + must(!dispatched, "Target deletion already dispatched") + await expect(expected) + must(!dispatched, "Target deletion already dispatched") + dispatched = true + await arm() + })) + must(dispatched, "Target deletion skipped dispatch fence") + await checkpoint({ action: "delete", phase: "acknowledged", id }) + await expect(baseline) + await checkpoint({ action: "delete", phase: "verified", id }) + phase = "done" + return { id, restored: true as const, automaticReplay: false as const } + } catch (error) { + phase = "refused" + if (dispatched) throw Error("Target deletion outcome unknown. Automatic replay forbidden.") + throw error + } + }, + }) +} diff --git a/backend/src/test/traffic-flow-chr-target-timing-profile.ts b/backend/src/test/traffic-flow-chr-target-timing-profile.ts new file mode 100644 index 00000000..7e143fac --- /dev/null +++ b/backend/src/test/traffic-flow-chr-target-timing-profile.ts @@ -0,0 +1,14 @@ +// Pure protocol limits shared before the disposable database is pinned. +export const CHR_TARGET_TEMPLATE_TIMEOUT_MS = 20000 +export const CHR_STAGED_TARGET_CAPTURE_MAX_MS = 240000 +export const CHR_STAGED_COLLECTOR_APPROVAL_MS = 60000 +export const CHR_COLLECTOR_RESULT_POLL_MS = 45000 +// Two 17-second drains, the template interval, and 10 seconds for exchanges. +export const CHR_TARGET_MINIMUM_CAPTURE_REMAINING_MS = 2 * 17000 + CHR_TARGET_TEMPLATE_TIMEOUT_MS + 10000 + +export const CHR_LEGACY_FIXTURE_DEADLINE_MAX_MS = 600000 +export const CHR_STAGED_FIXTURE_DEADLINE_MAX_MS = 3600000 +export type ChrStagedFixtureLease = "staged-disabled-target" +export function chrFixtureDeadlineMaxMs(profile?: ChrStagedFixtureLease) { + return profile === "staged-disabled-target" ? CHR_STAGED_FIXTURE_DEADLINE_MAX_MS : CHR_LEGACY_FIXTURE_DEADLINE_MAX_MS +} diff --git a/backend/src/test/traffic-flow-chr-topology-inventory.test.ts b/backend/src/test/traffic-flow-chr-topology-inventory.test.ts new file mode 100644 index 00000000..09cc59c0 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-topology-inventory.test.ts @@ -0,0 +1,181 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { setImmediate as wait } from "node:timers/promises" +import { createChrTransitTopologyInventory, CHR_TOPOLOGY_CATALOG_PAIR_GETS } from "./traffic-flow-chr-topology-inventory.js" + +const name = "nfa012bcdeea", marker = "netflow-a012bcde" +const wg = { side: "A", menu: "wireguard", step: "edgeA", id: "*10" } as const +const address = { side: "A", menu: "address", step: "address:edgeA", id: "*11" } as const +const ip = { ".id": "*11", address: "198.18.241.2/30", interface: name, comment: marker, dynamic: false, disabled: false, vrf: "main" } +const route = { ".id": "*12", "dst-address": "198.18.241.0/30", gateway: name, "routing-table": "main", dynamic: true, + connect: true, distance: "0", scope: "10", "target-scope": "5" } +const rib = { ...route, afi: "ip", "belongs-to": "connected", "local-address": "198.18.241.2%" + name } +function fixture() { + const tables = { A: {} as Record, B: {} as Record } + const client = (side: "A" | "B") => ({ deviceId: side, client: { async get(path: string, timeout?: number, signal?: AbortSignal, cap?: number) { + assert.equal(timeout, 99); assert(signal instanceof AbortSignal); assert.equal(cap, 1048576) + return structuredClone(tables[side][path] ?? []) as T + } } }) + const reader = createChrTransitTopologyInventory({ runId: "a012bcde", deadlineAt: 100, now: () => 1, authorize: async () => {}, + routers: { A: client("A"), B: client("B") } }) + const snapshot = async (owned: Parameters[0], transition?: Parameters[1]) => { + await reader.assertOutsideScope(owned, transition) + const a = await reader.read("A") + await reader.read("B") + return a.rows + } + async function prepare(interfaceType = "wireguard") { + tables.B["/ip/route"] = [{ ".id": "*22", "dst-address": "192.0.2.0/24", dynamic: true }] + await snapshot([]) + tables.A["/interface"] = [{ ".id": "*10", name, comment: marker, type: interfaceType, disabled: false }] + await snapshot([], { ...wg, action: "create" }) + } + return { tables, reader, snapshot, prepare } +} +test("the generic interface catalogue accepts either WireGuard spelling and refuses foreign types", async () => { + await fixture().prepare("wg") + await assert.rejects(fixture().prepare("ether")) +}) +test("IPv4 DHCP lease countdown is telemetry while gateway and IPv6 countdown remain conserved", async () => { + for (const [path, change, accepted] of [ + ["/ip/dhcp-client", { "expires-after": "4m" }, true], + ["/ip/dhcp-client", { "expires-after": "unexpected" }, false], + ["/ip/dhcp-client", { gateway: "192.0.2.2" }, false], + ["/ipv6/dhcp-client", { "expires-after": "4m" }, false], + ] as const) { + const f = fixture() + const row = { ".id": "*A", interface: "ether1", gateway: "192.0.2.1", "expires-after": "5m" } + f.tables.A[path] = [row] + await f.snapshot([]) + f.tables.A[path] = [{ ...row, ...change }] + if (accepted) await f.snapshot([]) + else await assert.rejects(f.snapshot([]), /Foreign native catalogue drift/) + } +}) +test("a foreign DHCP client added on the second CHR invalidates the entire paired snapshot", async () => { + const f = fixture() + const path = "/ip/dhcp-client" + const clientA = { ".id": "*A", interface: "ether1", "expires-after": "5m" } + const clientB = { ".id": "*B", interface: "ether1", "expires-after": "5m" } + f.tables.A[path] = [clientA] + f.tables.B[path] = [clientB] + await f.prepare() + + clientA["expires-after"] = "4m59s" + clientB["expires-after"] = "4m58s" + await f.reader.assertOutsideScope([wg]) + + f.tables.A["/interface"] = [] + clientA["expires-after"] = "4m58s" + clientB["expires-after"] = "4m57s" + f.tables.B[path].push({ ".id": "*C", interface: "ether2", "expires-after": "10m" }) + await assert.rejects(f.reader.assertOutsideScope([wg], { ...wg, action: "delete" }), /Foreign native catalogue drift/) + await assert.rejects(f.reader.read("A"), /snapshot required/) + await assert.rejects(f.reader.read("B"), /snapshot required/) +}) +test("single paired capture projects only exact ACK-address connected menu and RIB companions", async () => { + const f = fixture(); await f.prepare() + f.tables.A["/ip/address"] = [ip]; f.tables.A["/ip/route"] = [route]; f.tables.A["/routing/route"] = [rib] + const view = await f.snapshot([wg], { ...address, action: "create" }) + assert.deepEqual(view.address, [ip]); assert.deepEqual(view.route, []) + await assert.rejects(f.reader.read("A"), /snapshot required/) + await f.reader.assertOutsideScope([wg, address]) + await f.reader.read("A") + assert.deepEqual((await f.reader.read("B")).rows.route, f.tables.B["/ip/route"]) + f.tables.A["/ip/address"] = []; f.tables.A["/ip/route"] = []; f.tables.A["/routing/route"] = [] + await f.snapshot([wg, address], { ...address, action: "delete" }) + f.tables.A["/interface"] = [] + await f.snapshot([wg], { ...wg, action: "delete" }) +}) +test("wrong address ACK, conflicting RIB or unrelated peer drift refuses the next admission", async () => { + for (const fault of ["ack", "rib", "peer", "duplicate"]) { + const f = fixture(); await f.prepare() + f.tables.A["/ip/address"] = [{ ...ip, ".id": fault === "ack" ? "*33" : ip[".id"] }] + f.tables.A["/ip/route"] = fault === "duplicate" ? [route, { ...route, ".id": "*14" }] : [route] + f.tables.A["/routing/route"] = [{ ...rib, "local-address": fault === "rib" ? "192.0.2.1" : rib["local-address"] }] + if (fault === "peer") f.tables.B["/ip/firewall/filter"] = [{ ".id": "*22" }] + await assert.rejects(f.snapshot([wg], { ...address, action: "create" })) + await assert.rejects(f.snapshot([wg])) + } +}) +test("previously confirmed companion opaque config cannot change on a later capture", async () => { + const f = fixture(); await f.prepare() + f.tables.A["/ip/address"] = [ip]; f.tables.A["/ip/route"] = [route]; f.tables.A["/routing/route"] = [rib] + await f.snapshot([wg], { ...address, action: "create" }) + f.tables.A["/routing/route"] = [{ ...rib, "unreviewed-field": "different" }] + await assert.rejects(f.snapshot([wg, address]), /companion drift/) +}) + +test("fixed parallel batches keep both authorities fresh and never mix CHR sources", async () => { + let pairFences = 0, requestFences = 0, active = 0, peak = 0 + const requests: string[] = [] + const running = new Set<"A" | "B">() + const client = (side: "A" | "B") => ({ deviceId: side, client: { + async get(path: string, _timeout?: number, _signal?: AbortSignal, cap?: number, before?: () => Promise) { + assert.equal(cap, 1048576) + await before?.() + assert.ok(!running.size || running.has(side)) + running.add(side) + requests.push(side + path) + peak = Math.max(peak, ++active) + await wait() + if (--active === 0) running.clear() + return [] as T + }, + } }) + const reader = createChrTransitTopologyInventory({ runId: "a012bcde", deadlineAt: 100, now: () => 1, parallelism: 2, + authorize: async () => { pairFences++ }, authorizeRequest: async () => { requestFences++ }, + routers: { A: client("A"), B: client("B") } }) + await reader.assertOutsideScope([]) + assert.equal(requests.length, CHR_TOPOLOGY_CATALOG_PAIR_GETS) + assert.equal(new Set(requests).size, requests.length) + assert.equal(peak, 2) + assert.equal(pairFences, 2) + assert.equal(requestFences, 2 * requests.length) + await reader.read("A") + await reader.read("B") +}) + +test("failed concurrent catalogue settles its sibling and never exposes a partial pair", async () => { + let started = 0, completed = 0 + const reader = createChrTransitTopologyInventory({ + runId: "a012bcde", deadlineAt: 100, now: () => 1, parallelism: 2, authorize: async () => {}, + routers: { A: { deviceId: "A", client: { async get() { + if (++started === 1) throw Error("fixed path unavailable") + await wait() + completed++ + return [] as T + } } }, B: { deviceId: "B", client: { async get(): Promise { throw Error("foreign side reached") } } } }, + }) + await assert.rejects(reader.assertOutsideScope([]), /fixed path unavailable/) + assert.equal(started, 2) + assert.equal(completed, 1) + await assert.rejects(reader.read("A"), /snapshot required/) + await assert.rejects(reader.assertOutsideScope([])) + assert.equal(started, 2) +}) + +test("paired timing candidate overlaps the two routers under their individual request bounds", async () => { + let total = 0, peak = 0 + const counters = { A: 0, B: 0 } + const client = (side: "A" | "B") => ({ deviceId: side, client: { + async get(_path: string, _timeout?: number, _signal?: AbortSignal, _cap?: number, before?: () => Promise) { + await before?.() + ++total + assert(++counters[side] <= 2) + peak = Math.max(peak, counters.A + counters.B) + await wait() + --counters[side] + return [] as T + }, + } }) + const reader = createChrTransitTopologyInventory({ + runId: "a012bcde", deadlineAt: 100, now: () => 1, parallelism: 2, parallelRouters: true, + authorize: async () => {}, routers: { A: client("A"), B: client("B") }, + }) + await reader.assertOutsideScope([]) + assert.equal(total, CHR_TOPOLOGY_CATALOG_PAIR_GETS) + assert.equal(peak, 4) + assert.equal((await reader.read("A")).deviceId, "A") + assert.equal((await reader.read("B")).deviceId, "B") +}) diff --git a/backend/src/test/traffic-flow-chr-topology-inventory.ts b/backend/src/test/traffic-flow-chr-topology-inventory.ts new file mode 100644 index 00000000..54e448c6 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-topology-inventory.ts @@ -0,0 +1,274 @@ +import { isDeepStrictEqual } from "node:util" +import { parseChrVrfPrefix } from "./chr-vrf-companion-address-space.js" +import { stripNativeTelemetry } from "./chr-vrf-companion-telemetry.js" +import { greDependencyPaths, wireguardInterfaceDependencyPaths } from "../services/routeros-native-paths.js" +import type { MikrotikClient } from "../services/mikrotik.js" +import type { createChrTransitTopologyOwner } from "./traffic-flow-chr-topology-owner.js" +import { isChrWireGuardInterfaceType } from "./traffic-flow-chr-transit-fixture.js" + +type OwnerOptions = Parameters[0] +type Fence = OwnerOptions["assertOutsideScope"] +type Side = "A" | "B" +type Row = Record +type Catalogues = Record +const sides = ["A", "B"] as const +const writePaths = { wireguard: "/interface/wireguard", peer: "/interface/wireguard/peers", address: "/ip/address", route: "/ip/route" } as const +export const chrTopologyCataloguePaths: readonly string[] = Object.freeze([...new Set([...Object.values(writePaths), "/interface", "/ipv6/address", "/ipv6/route", "/routing/route", + "/ip/vrf", "/routing/table", "/interface/bridge", "/interface/list/member", "/ip/firewall/filter", "/ipv6/firewall/filter", + ...greDependencyPaths, ...wireguardInterfaceDependencyPaths])]) +const nativePaths = chrTopologyCataloguePaths +export const CHR_TOPOLOGY_CATALOG_PAIR_GETS = nativePaths.length * 2 +export const CHR_TOPOLOGY_CATALOG_READ_LIMIT = 12000 +const roles = { edgeA: "ea", middleA: "ma", middleB: "mb", edgeB: "eb" } as const +const addressRole = { edgeA: "198.18.241.2/30", middleA: "198.18.241.5/30", middleB: "198.18.241.6/30", edgeB: "198.18.241.9/30" } as const +const id = (row: Row) => row[".id"] +const yes = (value: unknown) => [true, "true", "yes"].includes(value as string | boolean) +const no = (value: unknown) => [false, "false", "no"].includes(value as string | boolean) +const native = (value: unknown) => typeof value === "string" && /^\*[a-f0-9]{1,16}$/i.test(value) +const nativeCanonical = (value: unknown) => String(value).replace(/^\*0+(?=.)/, "*").toLowerCase() +function must(value: unknown, message: string): asserts value { if (!value) throw Error(message) } +function exact(rows: Row[], test: (row: Row) => boolean) { + const matching = rows.filter(test) + must(matching.length <= 1, "Ambiguous topology companion") + return matching[0] +} +const points = (row: Row, name: string) => [row.gateway, row["immediate-gw"], row.interface, row["actual-interface"]].includes(name) || + String(row["local-address"] ?? "").endsWith("%" + name) || row["dst-address"] === "fe80::%" + name + "/64" + +function connected(row: Row, name: string, host: string, network: NonNullable>, rib: boolean) { + const destination = row["dst-address"] === "fe80::%" + name + "/64" ? "fe80::/64" : row["dst-address"] + const value = parseChrVrfPrefix(destination, network.family) + if (!points(row, name) || value?.host !== network.first || value?.prefixLength !== network.prefixLength) return false + if (rib && !(network.family === 4 ? ["ip", "ip4"] : ["ipv6", "ip6"]).includes(String(row.afi))) return false + must(native(id(row)) && row.gateway === name && row["routing-table"] === "main" && yes(row.connect) && + (!Object.hasOwn(row, "connected") || yes(row.connected)) && + (rib ? row["belongs-to"] === "connected" && (!Object.hasOwn(row, "dynamic") || yes(row.dynamic)) : yes(row.dynamic)) && + String(row.distance) === "0" && String(row.scope) === "10" && String(row["target-scope"]) === "5" && + ["static", "bgp", "blackhole", "invalid", "disabled", "ecmp"].every(key => !Object.hasOwn(row, key) || no(row[key])), + "Unproven connected route") + for (const field of ["interface", "immediate-gw"]) must(!Object.hasOwn(row, field) || row[field] === name, "Conflicting connected route interface") + must(!Object.hasOwn(row, "local-address") || row["local-address"] === host + "%" + name, "Conflicting connected route source") + if (rib) must(row["local-address"] === host + "%" + name, "Missing exact connected RIB source") + return true +} + +type Ownership = Parameters[0] +type Transition = Parameters[1] +export function createChrTransitTopologyInventory(options: { + runId: string + deadlineAt: number + authorize(): Promise + authorizeRequest?: () => Promise + parallelism?: 1 | 2 | 4 + parallelRouters?: true + routers: Record }> + now?: () => number +}) { + const { runId, deadlineAt, authorize, authorizeRequest = authorize, parallelism = 1, now = Date.now } = options + const routers = { A: { ...options.routers.A }, B: { ...options.routers.B } } + must(/^[a-f0-9]{8}$/.test(runId) && Number.isSafeInteger(deadlineAt) && [1, 2, 4].includes(parallelism) && + routers.A.deviceId && routers.B.deviceId && + routers.A.deviceId !== routers.B.deviceId, "Invalid paired topology reader scope") + const nameFor = (role: keyof typeof roles) => "nf" + runId + roles[role] + const marker = "netflow-" + runId + let baseline: Record | undefined + let cache: Partial> | undefined + let previousCompanions: Record> | undefined + let open = false, refused = false, reads = 0 + const live = () => { + const left = Math.min(8000, Math.floor(deadlineAt - now())) + must(left > 0, "Topology catalogue deadline elapsed") + return left + } + async function readSide(side: Side, signal?: AbortSignal): Promise { + const catalogues: Catalogues = {} + let bytes = 0 + for (let offset = 0; offset < nativePaths.length; offset += parallelism) { + signal?.throwIfAborted() + const batch = nativePaths.slice(offset, offset + parallelism) + reads += batch.length + must(reads <= CHR_TOPOLOGY_CATALOG_READ_LIMIT, "Topology catalogue read limit exhausted") + const byteLimit = Math.min(1048576, Math.floor((8 * 1048576 - bytes) / batch.length)) + must(byteLimit > 0, "Paired topology catalogue exceeds budget") + const captured = await Promise.allSettled(batch.map(async path => { + await authorizeRequest() + signal?.throwIfAborted() + const timeout = live() + const value = await routers[side].client.get(path, timeout, AbortSignal.timeout(timeout), byteLimit, + async () => { await authorizeRequest(); signal?.throwIfAborted(); live() }) + live() + must(Array.isArray(value) && value.length <= 1000 && value.every(row => row && typeof row === "object" && !Array.isArray(row)), + "Incomplete native topology catalogue") + const rows = value as Row[] + const size = Buffer.byteLength(JSON.stringify(rows)) + must(size <= byteLimit, "Paired topology catalogue exceeds budget") + must(rows.every(row => native(id(row))) && new Set(rows.map(row => nativeCanonical(id(row)))).size === rows.length, + "Ambiguous native topology catalogue") + return { path, rows: structuredClone(rows), size } + })) + for (const item of captured) { + if (item.status === "rejected") throw item.reason + const { path, rows, size } = item.value + bytes += size + catalogues[path] = rows + } + } + return catalogues + } + async function readPair(): Promise> { + if (options.authorizeRequest) { await authorize(); live() } + let a: Catalogues, b: Catalogues + if (options.parallelRouters) { + const stop = new AbortController() + const read = async (side: Side) => { + try { return await readSide(side, stop.signal) } + catch (error) { stop.abort(error); throw error } + } + const [first, second] = await Promise.allSettled([read("A"), read("B")]) + if (first.status === "rejected") throw first.reason + if (second.status === "rejected") throw second.reason + a = first.value + b = second.value + } else { + a = await readSide("A") + b = await readSide("B") + } + await authorize(); live() + return { A: a, B: b } + } + function projections(c: Catalogues, entries: Ownership, side: Side) { + const exclusions = new Map>() + const companions = new Map() + const exclude = (path: string, row: Row, step: string) => { + const current = exclusions.get(path) ?? new Set() + must(!current.has(row), "Topology companion has multiple owners") + current.add(row) + companions.set(path + "/" + id(row), { step, row: stripNativeTelemetry(row) }) + exclusions.set(path, current) + } + for (const [role, cidr] of Object.entries(addressRole) as [keyof typeof roles, string][]) { + if (!role.endsWith(side)) continue + const name = nameFor(role) + const wg = entries.find(entry => entry.side === side && entry.menu === "wireguard" && entry.step === role) + const address = entries.find(entry => entry.side === side && entry.menu === "address" && entry.step === "address:" + role) + must(!address || wg, "Owned address lost its WireGuard parent") + if (wg) { + const iface = exact(c["/interface"], row => id(row) === wg.id || row.name === name) + must(iface && id(iface) === wg.id && iface.name === name && isChrWireGuardInterfaceType(iface.type) && iface.comment === marker && + no(iface.disabled) && (!Object.hasOwn(iface, "vrf") || iface.vrf === "main"), "Missing exact WireGuard inventory") + exclude("/interface", iface, role) + const link = exact(c["/routing/route"], row => row.afi === "link" && row["dst-address"] === name) + if (link) { + must(link["belongs-to"] === "interface" && link["routing-table"] === "main" && String(link.distance) === "0", + "Unproven WireGuard link RIB row") + exclude("/routing/route", link, role) + } + const ll = exact(c["/ipv6/address"], row => points(row, name)) + if (ll) { + const range = parseChrVrfPrefix(ll.address, 6) + must(ll.interface === name && range?.prefixLength === 64 && range.first === 0xfe80n << 112n && + yes(ll.dynamic) && yes(ll["auto-link-local"]) && yes(ll["link-local"]) && + (!Object.hasOwn(ll, "actual-interface") || ll["actual-interface"] === name) && + (!Object.hasOwn(ll, "vrf") || ll.vrf === "main") && + (!Object.hasOwn(ll, "disabled") || no(ll.disabled)) && (!Object.hasOwn(ll, "invalid") || no(ll.invalid)) && + (!Object.hasOwn(ll, "comment") || ll.comment === "" || ll.comment === marker), "Unproven WireGuard IPv6 link-local") + exclude("/ipv6/address", ll, role) + const host = String(ll.address).split("/")[0] + const rib = exact(c["/routing/route"], row => connected(row, name, host, range, true)) + must(rib, "Missing exact IPv6 link-local RIB row") + exclude("/routing/route", rib, role) + const kernel = exact(c["/ipv6/route"], row => connected(row, name, host, range, false)) + if (kernel) exclude("/ipv6/route", kernel, role) + } + } + if (address) { + const raw = exact(c["/ip/address"], row => id(row) === address.id || row.address === cidr) + must(raw && id(raw) === address.id && raw.address === cidr && raw.interface === name && raw.comment === marker && + no(raw.dynamic) && no(raw.disabled) && raw.vrf === "main", "Missing exact ACK static address") + const range = parseChrVrfPrefix(cidr, 4)! + const host = cidr.split("/")[0] + for (const path of ["/ip/route", "/routing/route"]) { + const route = exact(c[path], row => connected(row, name, host, range, path === "/routing/route")) + must(route, "Missing exact static-address companion") + exclude(path, route, "address:" + role) + } + } + } + // A static /ip/route object may also appear in the RIB. Only associate a + // unique, native static RIB row with the exact ACK route descriptor. + for (const entry of entries.filter(row => row.side === side && row.menu === "route")) { + const raw = exact(c["/ip/route"], row => id(row) === entry.id) + must(raw && raw.comment === marker && raw["routing-table"] === "main" && no(raw.disabled), "Missing exact ACK host route") + const rib = exact(c["/routing/route"], row => row["dst-address"] === raw["dst-address"] && + row["routing-table"] === raw["routing-table"] && row.gateway === raw.gateway) + if (rib) { + must(rib["belongs-to"] === "static" && yes(rib.static) && ["ip", "ip4"].includes(String(rib.afi)) && + (!Object.hasOwn(rib, "dynamic") || no(rib.dynamic)), "Unproven static RIB route") + exclude("/routing/route", rib, entry.step) + } + } + const projected = Object.fromEntries(nativePaths.map(path => [path, c[path].filter(row => !exclusions.get(path)?.has(row))])) + const outside = Object.fromEntries(nativePaths.filter(path => !Object.values(writePaths).some(writable => writable === path)) + .map(path => { + const rows = projected[path].map(row => { + const stable = stripNativeTelemetry(row) + if (path === "/ip/dhcp-client" && typeof stable["expires-after"] === "string" && + stable["expires-after"].length <= 32 && /^(?=\d)(?:\d+w)?(?:\d+d)?(?:\d+h)?(?:\d+m)?(?:\d+s)?$/.test(stable["expires-after"])) { + delete stable["expires-after"] + } + return stable + }) + if (path === "/routing/route") rows.sort((a, b) => String(id(a)).localeCompare(String(id(b)))) + return [path, rows] + })) + // Keep unrelated dynamic /ip/route entries in the writable projection. + // They may not disappear under a blanket connected/dynamic exclusion. + return { projected, outside, companions } + } + const assertOutsideScope: Fence = async (owned: Ownership, transition?: Transition) => { + must(!open && !refused, "Topology catalogue capture refused") + open = true + cache = undefined + try { + const entries = [...owned] + if (transition) { + const index = entries.findIndex(entry => entry.side === transition.side && entry.menu === transition.menu && entry.step === transition.step) + if (transition.action === "create") { must(index < 0, "Duplicate topology ownership"); entries.push(transition) } + else { must(index >= 0 && entries[index].id === transition.id, "Unknown topology inverse"); entries.splice(index, 1) } + } + const raw = await readPair() + const a = projections(raw.A, entries, "A"), b = projections(raw.B, entries, "B") + const outside = { A: a.outside, B: b.outside } + if (!baseline) { must(!entries.length && !transition, "Topology baseline required before mutation"); baseline = outside } + must(isDeepStrictEqual(outside, baseline), "Foreign native catalogue drift") + const companions = { A: a.companions, B: b.companions } + if (previousCompanions) for (const side of sides) { + const before = previousCompanions[side], after = companions[side] + for (const key of new Set([...before.keys(), ...after.keys()])) { + const old = before.get(key), fresh = after.get(key) + if (isDeepStrictEqual(old, fresh)) continue + must(transition?.side === side && (transition.action === "create" + ? !old && fresh?.step === transition.step : !fresh && old?.step === transition.step), + "Owned topology companion drift") + } + } + previousCompanions = companions + cache = { A: a.projected, B: b.projected } + } catch (error) { refused = true; throw error } + finally { open = false } + } + return Object.freeze({ + assertOutsideScope, + async read(side: Side) { + live() + const captured = cache?.[side] + must(captured && cache, "Paired topology snapshot required") + const rows = { wireguard: captured[writePaths.wireguard], peer: captured[writePaths.peer], + address: captured[writePaths.address], route: captured[writePaths.route] } + // The owner consumes A then B and cannot reuse one capture. + if (side === "A") delete cache.A + else cache = undefined + return { complete: true as const, deviceId: routers[side].deviceId, rows: structuredClone(rows) } + }, + }) +} diff --git a/backend/src/test/traffic-flow-chr-topology-owner.test.ts b/backend/src/test/traffic-flow-chr-topology-owner.test.ts new file mode 100644 index 00000000..065d9258 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-topology-owner.test.ts @@ -0,0 +1,104 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { chrTransitNames } from "./traffic-flow-chr-transit-fixture.js" +import { createChrTransitTopologyOwner } from "./traffic-flow-chr-topology-owner.js" + +import { createChrParentWriteAdmission } from "./traffic-flow-chr-write-admission.js" + +const runId = "a012bcde" +const ports = { linuxA: 50101, edgeA: 50102, middleA: 50103, middleB: 50104, edgeB: 50105, linuxB: 50106, + collector: 50107, calibrationSource: 50112, calibrationDestination: 50113, phase1Source: 50108, phase1Destination: 50109, phase2Source: 50110, phase2Destination: 50111 } +const directions = ["A-to-B", "B-to-A", "export-A", "export-B", "underlay-linuxA-to-A", "underlay-A-to-linuxA", + "underlay-A-to-B", "underlay-B-to-A", "underlay-B-to-linuxB", "underlay-linuxB-to-B"] as const +const isolation = { checkedPorts: Object.values(ports), checkedNames: Object.values(chrTransitNames(runId)), + checkedCidrs: ["198.18.240.1/32", "198.18.240.2/32", "198.18.241.0/30", "198.18.241.4/30", "198.18.241.8/30", "198.18.242.0/29"], + occupiedPorts: [], overlappingCidrs: [], occupiedNames: [], + policies: directions.map(direction => ({ direction, verdict: "allow", nat: "none", fastTrack: "none", ambiguousRules: 0 } as const)) } +type Row = Record +const menus = { "/interface/wireguard": "wireguard", "/interface/wireguard/peers": "peer", "/ip/address": "address", "/ip/route": "route" } as const +function fixture(fault?: string, bridged = false) { + const state = () => ({ wireguard: [] as Row[], peer: [] as Row[], address: [] as Row[], route: [] as Row[] }) + const rows = { A: state(), B: state() }, writes: { method: string; path: string; side: string }[] = [], journal: object[] = [] + let flowRestored = false, tick = 1, pairReads = 0 + const gate = bridged ? createChrParentWriteAdmission({ deadlineAt: 100, now: () => tick, deviceIds: { A: "A", B: "B" }, + assertCurrent: async () => {} }) : undefined + function router(side: "A" | "B", endpoint: string) { + return { deviceId: side, endpoint, read: async () => ({ deviceId: side, complete: true as const, rows: structuredClone(rows[side]) }), + native: { + async put(path: string, fields: Record, _timeout?: number, fence?: () => Promise) { + if (fault !== "skip-fence") await fence?.() + await gate?.authorizeWrite({ side, method: "PUT", path, body: fields }) + writes.push({ method: "PUT", path, side }) + const id = "*" + writes.length.toString(16) + const menu = menus[path as keyof typeof menus] + rows[side][menu].push({ ...fields, ".id": id, ...(menu === "wireguard" ? { "public-key": String.fromCharCode(65 + writes.length).repeat(43) + "=" } : {}) }) + if (fault === "lost-ack") throw Error("private") + if (fault === "foreign") rows.B.address.push({ ".id": "*FF" }) + if (fault === "expire") tick = 100 + return { ".id": id } as T + }, + async delete(path: string, _timeout?: number, fence?: () => Promise) { + await fence?.() + await gate?.authorizeWrite({ side, method: "DELETE", path, body: {} }) + writes.push({ method: "DELETE", path, side }) + const slash = path.lastIndexOf("/"), menu = menus[path.slice(0, slash) as keyof typeof menus] + rows[side][menu] = rows[side][menu].filter(row => row[".id"] !== decodeURIComponent(path.slice(slash + 1))) + }, + } } + } + const owner = createChrTransitTopologyOwner({ scope: [runId, ports, isolation], deadlineAt: 100, now: () => tick, writeAdmission: gate, + routers: { A: router("A", "192.0.2.1"), B: router("B", "192.0.2.2") }, + linux: { endpoint: "192.0.2.3", publicKeys: { linuxA: "X".repeat(43) + "=", linuxB: "Y".repeat(43) + "=" } }, + authorize: async () => {}, checkpoint: async entry => { + journal.push(entry) + if (fault === "drift-after-intent" && entry.phase === "intent") rows.B.address.push({ ".id": "*FF" }) + }, assertOutsideScope: async () => { pairReads++ }, + assertFlowRestored: async () => { assert(flowRestored) } }) + return { owner, rows, writes, journal, gate, flowClean: () => { flowRestored = true }, pairReads: () => pairReads } +} + +test("paired exact ACKs own 18 creates and 18 dependency-ordered inverse writes", async () => { + const f = fixture() + assert.equal((await f.owner.acquire()).attempts, 18) + await f.owner.assertCurrent() + f.flowClean() + assert.equal((await f.owner.restore()).attempts, 36) + assert.deepEqual(f.writes.slice(18).map(row => row.path.slice(0, row.path.lastIndexOf("/"))), + ["/ip/route", "/interface/wireguard/peers", "/ip/address", "/interface/wireguard"].flatMap((path, i) => Array(i ? 4 : 6).fill(path))) + assert.equal(f.journal.length, 108) + assert.equal(f.pairReads(), 75) // 74 write-schedule pairs and the explicit assertCurrent above. + await assert.rejects(f.owner.restore()) +}) + +test("fresh paired comparison immediately before dispatch rejects drift after durable intent", async () => { + const f = fixture("drift-after-intent") + await assert.rejects(f.owner.acquire(), /dispatch refused/) + assert.equal(f.writes.length, 0) + assert.deepEqual(f.journal.map(entry => (entry as { phase: string }).phase), ["intent"]) + assert.equal(f.pairReads(), 2) +}) +test("unknown ACK, lost fence and foreign changes latch before subsequent writes", async () => { + for (const fault of ["lost-ack", "skip-fence", "foreign", "expire"]) { + const f = fixture(fault) + await assert.rejects(f.owner.acquire(), /Topology/) + await assert.rejects(f.owner.acquire()) + await assert.rejects(f.owner.restore()) + assert.equal(f.writes.length, 1) + } +}) +test("target or singleton restoration must precede all inverse attempts", async () => { + const f = fixture() + await f.owner.acquire() + await assert.rejects(f.owner.restore()) + f.flowClean() + await assert.rejects(f.owner.restore()) + assert.equal(f.writes.length, 18) +}) + +test("all topology writes consume the parent relay gate exactly once", async () => { + const f = fixture(undefined, true) + assert.equal((await f.owner.acquire()).attempts, 18) + f.flowClean() + assert.equal((await f.owner.restore()).attempts, 36) + assert.equal(f.gate?.consumed, 36) +}) diff --git a/backend/src/test/traffic-flow-chr-topology-owner.ts b/backend/src/test/traffic-flow-chr-topology-owner.ts new file mode 100644 index 00000000..439d34e0 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-topology-owner.ts @@ -0,0 +1,199 @@ +import { isIPv4 } from "node:net" +import { isDeepStrictEqual } from "node:util" +import type { MikrotikClient } from "../services/mikrotik.js" +import { encodeRosId } from "../services/routeros-native-paths.js" +import { runChrWrite, type ChrWriteAdmission } from "./traffic-flow-chr-write-admission.js" +import { planChrTransitFixture } from "./traffic-flow-chr-transit-fixture.js" + +type Scope = Parameters +type Row = Record +const paths = { wireguard: "/interface/wireguard", peer: "/interface/wireguard/peers", address: "/ip/address", route: "/ip/route" } as const +type Menu = keyof typeof paths +type Side = "A" | "B" +type Role = "edgeA" | "middleA" | "middleB" | "edgeB" +type Inventory = { deviceId: string; complete: true; rows: Record } +type Command = { key: string; side: Side; menu: Menu; fields: Record } +type Checkpoint = { step: string; side: Side; menu: Menu; action: "create" | "delete"; phase: "intent" | "acknowledged" | "verified"; id?: string } +type Options = { + scope: Scope + routers: Record; native: Pick }> + linux: { endpoint: string; publicKeys: { linuxA: string; linuxB: string } } + deadlineAt: number + writeAdmission?: ChrWriteAdmission + authorize(): Promise + checkpoint(entry: Checkpoint): Promise + assertFlowRestored(): Promise + /** Independently verify the complete configuration outside the four writable menus. */ + assertOutsideScope(owned: readonly { step: string; side: Side; menu: Menu; id: string }[], transition?: { step: string; side: Side; menu: Menu; action: Checkpoint["action"]; id: string }): Promise + now?: () => number +} +const roles = ["edgeA", "middleA", "middleB", "edgeB"] as const +const interfaces = { edgeA: "ea", middleA: "ma", middleB: "mb", edgeB: "eb" } as const +const menus = Object.keys(paths) as Menu[] +const roleSide = (role: Role): Side => role.endsWith("A") ? "A" : "B" +const id = (row: Row) => row[".id"] +const nativeId = (value: unknown): value is string => typeof value === "string" && /^\*[a-f0-9]{1,16}$/i.test(value) +const publicKey = (value: unknown): value is string => typeof value === "string" && /^[A-Za-z0-9+/]{43}=$/.test(value) +const off = (value: unknown) => [false, "false", "no"].includes(value as string | boolean) +function must(value: unknown, message: string): asserts value { if (!value) throw Error(message) } +const telemetry = new Set(["running", "active", "inactive", "rx", "tx", "last-handshake", "current-endpoint-address", "current-endpoint-port", "actual-mtu"]) + +/** The pinned reader must provide full configuration rows. Explicitly observed + * dynamic connected routes belong in the independent read-only closure, not + * in the static route projection accepted by this owner. */ +function snapshot(input: Inventory, expectedDevice: string) { + must(input.complete && input.deviceId === expectedDevice && Object.keys(input.rows).sort().join() === [...menus].sort().join() && + Buffer.byteLength(JSON.stringify(input.rows)) <= 2 * 1048576, "Incomplete topology catalogue") + return Object.fromEntries(menus.map(menu => { + const rows = input.rows[menu] + must(Array.isArray(rows) && rows.length <= 1000 && rows.every(row => row && nativeId(id(row))) && + new Set(rows.map(row => String(id(row)).replace(/^\*0+(?=.)/, "*").toLowerCase())).size === rows.length, + "Ambiguous topology catalogue identity") + return [menu, rows.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => !telemetry.has(key)))) + .sort((a, b) => String(id(a)).localeCompare(String(id(b))))] + })) as Record +} +function matches(row: Row, fields: Record) { + return Object.entries(fields).every(([key, value]) => key === "disabled" ? value === "false" && off(row[key]) : String(row[key]) === value) +} + +export function createChrTransitTopologyOwner(options: Options) { + const { authorize, checkpoint, assertFlowRestored, assertOutsideScope, deadlineAt, writeAdmission, now = Date.now } = options + const plan = planChrTransitFixture(...structuredClone(options.scope)), linux = structuredClone(options.linux) + const routers = { A: { ...options.routers.A }, B: { ...options.routers.B } } + must(/^[a-f0-9]{8}$/.test(plan.runId) && Number.isSafeInteger(deadlineAt) && + routers.A.deviceId && routers.B.deviceId && routers.A.deviceId !== routers.B.deviceId && + [routers.A.endpoint, routers.B.endpoint, linux.endpoint].every(isIPv4) && + new Set([routers.A.endpoint, routers.B.endpoint, linux.endpoint]).size === 3 && + publicKey(linux.publicKeys.linuxA) && publicKey(linux.publicKeys.linuxB) && linux.publicKeys.linuxA !== linux.publicKeys.linuxB, + "Invalid pinned transit identities") + const marker = "netflow-" + plan.runId + const name = (role: Role) => "nf" + plan.runId + interfaces[role] + const keys: Record = { ...linux.publicKeys } + const owned = new Map() + let phase: "new" | "creating" | "owned" | "deleting" | "done" | "refused" = "new" + let attempts = 0 + let baseline: Record> + let expected: Record> + const live = () => must(now() < deadlineAt, "Topology authorization expired") + const current = async (transition?: Parameters[1]) => { + live(); await authorize(); await assertOutsideScope([...owned.values()].map(({ command, id }) => ({ step: command.key, side: command.side, menu: command.menu, id })), transition); live() + const a = snapshot(await routers.A.read(), routers.A.deviceId), b = snapshot(await routers.B.read(), routers.B.deviceId) + await authorize(); live() + return { A: a, B: b } + } + const fence = async () => must(isDeepStrictEqual(await current(), expected), "Topology catalogue drift") + const run = async (action: Checkpoint["action"], command: Command, targetId?: string) => { + const { key, side, menu, fields } = command + const entry = { step: key, side, menu, action } + const router = routers[side] + const prior = expected + // The durable intent does not dispatch. Re-read the entire pair at the + // actual native dispatch fence and again on ACK, not before the intent too. + await authorize(); live() + if (action === "delete") { + must(nativeId(targetId) && prior[side][menu].some(row => id(row) === targetId && matches(row, fields)), + "Owned inverse target no longer matches") + } + await checkpoint({ ...entry, phase: "intent", ...(targetId ? { id: targetId } : {}) }) + let sent = false + const beforeSend = async () => { + must(!sent, "Repeated topology dispatch") + await fence() + if (action === "delete") await assertFlowRestored() + await authorize(); live() + must(!sent && attempts < 36, "Topology write budget exhausted") + sent = true; attempts++ + } + try { + let identity = targetId + const path = action === "create" ? paths[menu] : paths[menu] + "/" + encodeRosId(targetId!) + const commandBody = action === "create" ? { ...fields } : {} + const ack = await runChrWrite(writeAdmission, { step: "topology:" + action + ":" + side + ":" + key, + deviceId: router.deviceId, side, method: action === "create" ? "PUT" : "DELETE", path, body: commandBody }, async arm => { + const fence = async () => { await beforeSend(); await arm() } + if (action === "create") return router.native.put(path, commandBody, 15000, fence) + return router.native.delete(path, 15000, fence) + }) + if (action === "create") { + identity = ack && typeof ack === "object" && !Array.isArray(ack) ? (ack as Row)[".id"] as string : undefined + } + must(sent && nativeId(identity), "Native ACK missing exact identity") + await checkpoint({ ...entry, phase: "acknowledged", id: identity }) + const after = await current({ ...entry, id: identity }), reduced = structuredClone(action === "create" ? after : prior) + const row = reduced[side][menu].find(candidate => id(candidate) === identity) + must(row && matches(row, fields), "Native read-back differs from owned descriptor") + reduced[side][menu] = reduced[side][menu].filter(candidate => id(candidate) !== identity) + must(isDeepStrictEqual(reduced, action === "create" ? prior : after), "Topology write changed unrelated configuration") + if (action === "create" && menu === "wireguard") { + must(publicKey(row["public-key"]) && !Object.values(keys).includes(row["public-key"]), "Invalid or reused native WireGuard public key") + keys[key] = row["public-key"] + } + await checkpoint({ ...entry, phase: "verified", id: identity }) + expected = after + if (action === "create") owned.set(key, { command, id: identity }) + else owned.delete(key) + } catch { + throw Error(sent ? "Topology outcome unknown or unverified. Automatic replay forbidden." : "Topology dispatch refused.") + } + } + const interfaceCommands = () => roles.map(role => ({ key: role, side: roleSide(role), menu: "wireguard" as const, + fields: { name: name(role), "listen-port": String(plan.ports[role]), mtu: "1420", disabled: "false", comment: marker } })) + const peerCommands = () => roles.map(role => { + const peer = plan.peers[role], opposite = peer.opposite + const endpoint = opposite.startsWith("linux") ? linux.endpoint : routers[roleSide(opposite as Role)].endpoint + const key = keys[opposite] + must(publicKey(key), "Exact remote peer key unavailable") + return { key: "peer:" + role, side: roleSide(role), menu: "peer" as const, fields: { + interface: name(role), "public-key": key, "allowed-address": peer.allowed.join(","), "endpoint-address": endpoint, + "endpoint-port": String(plan.ports[opposite as keyof typeof plan.ports]), "persistent-keepalive": "10", disabled: "false", comment: marker, + } } + }) + const addressCommands = () => roles.map(role => ({ key: "address:" + role, side: roleSide(role), menu: "address" as const, + fields: { interface: name(role), address: plan.addresses[role], disabled: "false", comment: marker } })) + const routeCommands = () => plan.routes.filter(route => route.node === "A" || route.node === "B").map((route, index) => ({ + key: "route:" + index, side: route.node as Side, menu: "route" as const, fields: { + "dst-address": route.destination, gateway: route.nextHop + "%" + name(route.device as Role), "routing-table": "main", disabled: "false", comment: marker, + }, + })) + return Object.freeze({ + async acquire() { + must(phase === "new", "Topology acquisition already attempted") + phase = "creating" + try { + baseline = expected = await current() + const ifaces = interfaceCommands(), addresses = addressCommands(), routes = routeCommands() + must(ifaces.length === 4 && addresses.length === 4 && routes.length === 6 && + Object.values(baseline).every(rows => menus.every(menu => rows[menu].every(row => row.comment !== marker))) && + ifaces.every(cmd => !baseline[cmd.side].wireguard.some(row => row.name === cmd.fields.name || String(row["listen-port"]) === cmd.fields["listen-port"])) && + addresses.every(cmd => !baseline[cmd.side].address.some(row => row.address === cmd.fields.address)) && + routes.every(cmd => !baseline[cmd.side].route.some(row => row["dst-address"] === cmd.fields["dst-address"])), "Topology preflight collision") + for (const command of [...ifaces, ...addresses]) await run("create", command) + const peers = peerCommands() + must(peers.length === 4, "Topology peer schedule differs") + for (const command of [...peers, ...routes]) await run("create", command) + must(attempts === 18 && owned.size === 18, "Topology create schedule incomplete") + phase = "owned" + return { verified: true as const, attempts, chrPublicKeys: Object.fromEntries(roles.map(role => [role, keys[role]])) } + } catch (error) { phase = "refused"; throw error } + }, + async assertCurrent() { + must(phase === "owned", "Topology is not verified") + try { await fence() } catch (error) { phase = "refused"; throw error } + }, + async restore() { + must(phase === "owned", "Topology inverse has no verified source") + phase = "deleting" + try { + await assertFlowRestored() + for (const menu of ["route", "peer", "address", "wireguard"] as const) + for (const row of [...owned.values()].filter(row => row.command.menu === menu).reverse()) + await run("delete", row.command, row.id) + must(attempts === 36 && owned.size === 0 && isDeepStrictEqual(expected, baseline), "Topology inverse did not restore the baseline") + await fence() + phase = "done" + return { verified: true as const, attempts, automaticReplay: false as const } + } catch (error) { phase = "refused"; throw error } + }, + }) +} diff --git a/backend/src/test/traffic-flow-chr-transit-fixture.test.ts b/backend/src/test/traffic-flow-chr-transit-fixture.test.ts new file mode 100644 index 00000000..9a235741 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-transit-fixture.test.ts @@ -0,0 +1,48 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { chrTransitNames, planChrTransitFixture, verifyChrTransitRoutes } from "./traffic-flow-chr-transit-fixture.js" + +const owner = "a012bcde" +const ports = { linuxA: 50101, edgeA: 50102, middleA: 50103, middleB: 50104, edgeB: 50105, linuxB: 50106, + collector: 50107, calibrationSource: 50112, calibrationDestination: 50113, phase1Source: 50108, phase1Destination: 50109, phase2Source: 50110, phase2Destination: 50111 } +const isolation = { + checkedPorts: Object.values(ports), + checkedNames: Object.values(chrTransitNames(owner)), + checkedCidrs: ["198.18.240.1/32", "198.18.240.2/32", "198.18.241.0/30", "198.18.241.4/30", + "198.18.241.8/30", "198.18.242.0/29"], + occupiedPorts: [], overlappingCidrs: [], occupiedNames: [], + policies: (["A-to-B", "B-to-A", "export-A", "export-B", "underlay-linuxA-to-A", + "underlay-A-to-linuxA", "underlay-A-to-B", "underlay-B-to-A", + "underlay-B-to-linuxB", "underlay-linuxB-to-B"] as const).map(direction => ({ + direction, verdict: "allow", nat: "none", fastTrack: "none", ambiguousRules: 0, + } as const)), +} + +test("plans separate directions through both CHRs and two exporter return routes", () => { + const plan = planChrTransitFixture(owner, ports, isolation) + assert.equal(verifyChrTransitRoutes([...plan.routes].reverse()), true) + assert.deepEqual(plan.routes.filter(row => row.destination === "198.18.240.2/32").map(row => row.device), + ["middleA", "edgeB", "linuxA"]) + assert.deepEqual(plan.routes.filter(row => row.destination === "198.18.240.1/32").map(row => row.device), + ["edgeA", "middleB", "linuxB"]) + assert.equal(plan.routes.filter(row => row.node === "C").length, 2) + assert.ok(plan.peers.edgeA.allowed.includes("198.18.242.3/32")) + assert.ok(plan.peers.edgeB.allowed.includes("198.18.242.3/32")) + assert.deepEqual(plan.topologyWrites.chrA, { wireGuard: 2, peers: 2, addresses: 2, routes: 3 }) + assert.deepEqual(plan.topologyWrites.chrB, plan.topologyWrites.chrA) + assert.equal(plan.actualIngressVerified, false) + assert.throws(() => verifyChrTransitRoutes(plan.routes.map(row => ({ ...row, nextHop: "192.0.2.1" }))), /route selection/) +}) + +test("refuses collisions, incomplete inventory and missing or ambiguous directional policy", () => { + assert.throws(() => planChrTransitFixture(owner, { ...ports, edgeA: ports.linuxA }, isolation), /fixture port/) + assert.throws(() => planChrTransitFixture(owner, { ...ports, calibrationSource: ports.phase1Source }, isolation), /fixture port/) + assert.throws(() => planChrTransitFixture(owner, { ...ports, calibrationDestination: 46000 }, isolation), /fixture port/) + assert.throws(() => planChrTransitFixture(owner, ports, { ...isolation, occupiedPorts: [ports.edgeA] }), /collision/) + assert.throws(() => planChrTransitFixture(owner, ports, { ...isolation, overlappingCidrs: ["198.18.0.0/15"] }), /collision/) + assert.throws(() => planChrTransitFixture(owner, ports, { ...isolation, checkedNames: [] }), /incomplete/) + assert.throws(() => planChrTransitFixture(owner, ports, { ...isolation, policies: isolation.policies.slice(1) }), /FastTrack/) + assert.throws(() => planChrTransitFixture(owner, ports, { + ...isolation, policies: [isolation.policies[1], ...isolation.policies.slice(1)], + }), /FastTrack/) +}) diff --git a/backend/src/test/traffic-flow-chr-transit-fixture.ts b/backend/src/test/traffic-flow-chr-transit-fixture.ts new file mode 100644 index 00000000..c35e06e8 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-transit-fixture.ts @@ -0,0 +1,104 @@ +import { isDeepStrictEqual } from "node:util" + +const address = { + endpointA: "198.18.240.1", endpointB: "198.18.240.2", + linuxA: "198.18.241.1", exportA: "198.18.241.2", + middleA: "198.18.241.5", middleB: "198.18.241.6", + exportB: "198.18.241.9", linuxB: "198.18.241.10", + vethA: "198.18.242.1", vethB: "198.18.242.2", collector: "198.18.242.3", +} as const +const cidrs = ["198.18.240.1/32", "198.18.240.2/32", "198.18.241.0/30", + "198.18.241.4/30", "198.18.241.8/30", "198.18.242.0/29"] as const +const portRoles = ["linuxA", "edgeA", "middleA", "middleB", "edgeB", "linuxB", "collector", + "calibrationSource", "calibrationDestination", "phase1Source", "phase1Destination", "phase2Source", "phase2Destination"] as const +type Ports = Record<(typeof portRoles)[number], number> +type Route = { node: string; destination: string; nextHop: string; device: string } +const host = (ip: string) => ip + "/32" +const addresses = { + edgeA: address.exportA + "/30", middleA: address.middleA + "/30", + middleB: address.middleB + "/30", edgeB: address.exportB + "/30", +} +const peers = { + edgeA: { opposite: "linuxA", allowed: [host(address.endpointA), host(address.collector)] }, + middleA: { opposite: "middleB", allowed: [host(address.endpointB)] }, + middleB: { opposite: "middleA", allowed: [host(address.endpointA)] }, + edgeB: { opposite: "linuxB", allowed: [host(address.endpointB), host(address.collector)] }, + linuxA: { opposite: "edgeA", allowed: [host(address.endpointB), host(address.exportA)] }, + linuxB: { opposite: "edgeB", allowed: [host(address.endpointA), host(address.exportB)] }, +} +const routes: Route[] = [ + { node: "A", destination: host(address.endpointA), nextHop: address.linuxA, device: "edgeA" }, + { node: "A", destination: host(address.endpointB), nextHop: address.middleB, device: "middleA" }, + { node: "A", destination: host(address.collector), nextHop: address.linuxA, device: "edgeA" }, + { node: "B", destination: host(address.endpointB), nextHop: address.linuxB, device: "edgeB" }, + { node: "B", destination: host(address.endpointA), nextHop: address.middleA, device: "middleB" }, + { node: "B", destination: host(address.collector), nextHop: address.linuxB, device: "edgeB" }, + { node: "linuxA", destination: host(address.endpointB), nextHop: address.exportA, device: "linuxA" }, + { node: "linuxB", destination: host(address.endpointA), nextHop: address.exportB, device: "linuxB" }, + { node: "C", destination: host(address.exportA), nextHop: address.vethA, device: "collector-veth" }, + { node: "C", destination: host(address.exportB), nextHop: address.vethB, device: "collector-veth" }, +] +const directions = ["A-to-B", "B-to-A", "export-A", "export-B", + "underlay-linuxA-to-A", "underlay-A-to-linuxA", "underlay-A-to-B", "underlay-B-to-A", + "underlay-B-to-linuxB", "underlay-linuxB-to-B"] as const +type Direction = (typeof directions)[number] +type PolicyCheck = { direction: Direction; verdict: "allow"; nat: "none"; fastTrack: "none"; ambiguousRules: 0 } +type Isolation = { + checkedPorts: readonly number[] + checkedCidrs: readonly string[] + checkedNames: readonly string[] + occupiedPorts: readonly number[] + overlappingCidrs: readonly string[] + occupiedNames: readonly string[] + policies: readonly PolicyCheck[] +} +const must = (condition: unknown, message: string) => { if (!condition) throw Error(message) } +const sameSet = (first: readonly T[], second: readonly T[]) => + isDeepStrictEqual([...first].sort(), [...second].sort()) + +/** Inputs are normalized observations of fixed, independently reviewed scope. + * This planner does not interpret raw RouterOS firewall rules or acquire authority. */ +export function planChrTransitFixture(runId: string, ports: Ports, isolation: Isolation) { + must(/^[a-f0-9]{8}$/.test(runId), "Invalid fixture owner") + must(Object.keys(ports).length === portRoles.length && + portRoles.every(role => Number.isInteger(ports[role]) && ports[role] >= 49152 && ports[role] <= 65535) && + new Set(portRoles.map(role => ports[role])).size === portRoles.length, "Unreserved or duplicate fixture port") + const names = chrTransitNames(runId) + must(sameSet(isolation.checkedPorts, portRoles.map(role => ports[role])) && + sameSet(isolation.checkedCidrs, cidrs) && sameSet(isolation.checkedNames, Object.values(names)), + "Fixture isolation inventory incomplete") + must(isolation.occupiedPorts.length === 0 && isolation.overlappingCidrs.length === 0 && + isolation.occupiedNames.length === 0, "Fixture ownership collision") + must(isolation.policies.length === directions.length && + directions.every(direction => isolation.policies.filter(check => check.direction === direction && + check.verdict === "allow" && check.nat === "none" && check.fastTrack === "none" && + check.ambiguousRules === 0).length === 1), "Firewall, NAT or FastTrack path unavailable") + return structuredClone({ + runId, names, address, cidrs, ports, peers, addresses, routes, + requiredPolicyDirections: directions, + topologyWrites: { chrA: { wireGuard: 2, peers: 2, addresses: 2, routes: 3 }, + chrB: { wireGuard: 2, peers: 2, addresses: 2, routes: 3 }, + linux: { namespaces: 4, wireGuard: 2, vethPairs: 3, bridges: 1 } }, + rollbackOrder: ["netflow-targets", "netflow-singletons", "chr-host-routes", "chr-peers", + "chr-addresses", "chr-wireguard", "linux-owned-processes", "linux-owned-namespaces"], + automaticReplay: false, + actualIngressVerified: false, + }) +} + +export function chrTransitNames(runId: string) { + must(/^[a-f0-9]{8}$/.test(runId), "Invalid fixture owner") + return Object.fromEntries(["na", "nb", "nc", "ns", "la", "lb", "ea", "ma", "mb", "eb", + "av", "as", "bv", "bs", "cv", "cs", "br"].map(role => [role, "nf" + runId + role])) +} + +/** The reviewed route probe returns each selected route with no ambiguity. */ +export function verifyChrTransitRoutes(observed: readonly Route[]) { + must(observed.length === routes.length && + routes.every(route => observed.filter(candidate => isDeepStrictEqual(candidate, route)).length === 1), + "Fixture route selection differs") + return true +} +export function isChrWireGuardInterfaceType(value: unknown) { + return value === "wg" || value === "wireguard" +} diff --git a/backend/src/test/traffic-flow-chr-vps-operator.test.ts b/backend/src/test/traffic-flow-chr-vps-operator.test.ts new file mode 100644 index 00000000..96346e0a --- /dev/null +++ b/backend/src/test/traffic-flow-chr-vps-operator.test.ts @@ -0,0 +1,143 @@ +import assert from "node:assert/strict" +import { createHash, randomUUID } from "node:crypto" +import { mkdtemp, readFile, rm } from "node:fs/promises" +import { join } from "node:path" +import { fileURLToPath } from "node:url" +import { test } from "node:test" +import { executeDisabledChrVpsOperatorForTests, executeOwnedCollectorForTests } from "./traffic-flow-chr-vps-operator.js" + +const scope = { interfaceName: "owned0", sources: ["192.0.2.1", "192.0.2.2"] as const, destination: "192.0.2.100", port: 4739 } +type Options = Parameters[0] +function capture() { + const bytes = Buffer.alloc(144) + bytes.writeUInt32LE(0xa1b2c3d4); bytes.writeUInt16LE(2, 4); bytes.writeUInt16LE(4, 6) + bytes.writeUInt32LE(4124, 16); bytes.writeUInt32LE(101, 20) + for (const [index, at] of [24, 84].entries()) { + bytes.writeUInt32LE(1700000000, at); bytes.writeUInt32LE(44, at + 8); bytes.writeUInt32LE(44, at + 12) + const ip = bytes.subarray(at + 16, at + 60) + ip[0] = 0x45; ip.writeUInt16BE(44, 2); ip[9] = 17 + Buffer.from([192, 0, 2, index + 1, 192, 0, 2, 100]).copy(ip, 12) + ip.writeUInt16BE(2055, 20); ip.writeUInt16BE(scope.port, 22); ip.writeUInt16BE(24, 24) + ip.writeUInt16BE(10, 28); ip.writeUInt16BE(16, 30) + } + return { bytes, sha256: createHash("sha256").update(bytes).digest("hex"), complete: true, dropped: 0 } +} +function fixture(directory: string) { + const pins = { instance: "owned-instance", pid: 42, invariantProof: "a".repeat(64) } + let enabled = false, foreign = false, lostAck = false + const writes: boolean[] = [] + let drains = 0 + const options: Options = { directory, scope, pins, deadlineAt: Date.now() + 5000, collector: { + authorize: async () => {}, + read: async () => ({ ...pins, listenAddress: scope.destination, listenPort: scope.port, enabled, workerAlive: enabled, + boundAddress: enabled ? scope.destination + ":" + scope.port : null, + udpOwners: enabled || foreign ? [{ pid: foreign ? 999 : pins.pid, address: scope.destination, port: scope.port }] : [] }), + assertDrained: async () => { assert.equal(enabled, false); drains++ }, + applyEnabled: async (desired, fence) => { + await fence() + const contents = await readFile(join(directory, "collector.jsonl"), "utf8") + assert(contents.includes(JSON.stringify({ phase: "intent", enabled: desired }))) + writes.push(desired) + enabled = desired + return { operationId: randomUUID(), acknowledged: !lostAck } + }, + } } + return { options, writes, drains: () => drains, setForeign: () => { foreign = true }, loseAck: () => { lostAck = true } } +} +async function inDirectory(work: (directory: string) => Promise) { + const parent = await mkdtemp(join(fileURLToPath(new URL("../../../tmp/", import.meta.url)), "mm-chr-vps-")) + try { await work(join(parent, "owned")) } finally { await rm(parent, { recursive: true, force: true }) } +} + +test("preserves exact pcap, acknowledges bound owner and verifies clean disabled baseline", async () => { + await inDirectory(async directory => { + const f = fixture(directory), pcap = capture() + await executeOwnedCollectorForTests(f.options, async owner => { + await owner.assertBound() + await owner.retainCapture(pcap) + assert.deepEqual(await readFile(join(directory, "capture.pcap")), pcap.bytes) + await assert.rejects(owner.retainCapture(pcap), { code: "EEXIST" }) + await owner.restore() + }) + assert.deepEqual(f.writes, [true, false]) + assert.equal(f.drains(), 1) + await assert.rejects(executeOwnedCollectorForTests(f.options, async () => {}), { code: "EEXIST" }) + assert.deepEqual(f.writes, [true, false]) + }) +}) + +test("foreign socket and lost enable acknowledgement block ownership and cleanup writes", async () => { + await inDirectory(async directory => { + const f = fixture(directory); f.setForeign() + await assert.rejects(executeOwnedCollectorForTests(f.options, async () => assert.fail()), /UDP owner/) + assert.deepEqual(f.writes, []) + }) + await inDirectory(async directory => { + const f = fixture(directory); f.loseAck() + await assert.rejects(executeOwnedCollectorForTests(f.options, async () => assert.fail()), /lacks acknowledged/) + assert.deepEqual(f.writes, [true]) + }) +}) + +test("an unsupported downstream replay preserves pcap and retires the still-owned collector once", async () => { + await inDirectory(async directory => { + const f = fixture(directory), pcap = capture() + await assert.rejects(executeOwnedCollectorForTests(f.options, async owner => { + await owner.retainCapture(pcap) + throw Error("unsupported downstream replay") + }), /unsupported downstream replay/) + assert.deepEqual(f.writes, [true, false]) + assert.equal(f.drains(), 1) + assert.deepEqual(await readFile(join(directory, "capture.pcap")), pcap.bytes) + }) +}) + +test("cleanup refuses a changed socket owner after an acknowledged startup", async () => { + await inDirectory(async directory => { + const f = fixture(directory) + await assert.rejects(executeOwnedCollectorForTests(f.options, async () => { + f.setForeign() + throw Error("exercise stopped") + }), /UDP owner/) + assert.deepEqual(f.writes, [true]) + assert.equal(f.drains(), 0) + }) +}) + + +test("staged activation spends the credential window only after exact topology preflight", async () => { + for (const denied of ["admission", "preflight", "activation"] as const) { + const stages: string[] = [] + await assert.rejects(executeDisabledChrVpsOperatorForTests({ + directory: "unused", deadlineAt: Date.now() + 1800000, + scope: { ...scope, interfaceName: "nfa012bcdecv", linkType: "EN10MB" }, serverIds: [1, 2], + authorizeFixture: async () => { stages.push("authorize") }, + admitBeforeHardwareWrites: async () => { + stages.push("admit") + if (denied === "admission") throw Error("timing admission refused") + }, + topology: { + acquire: async () => { stages.push("acquire") }, + assertCurrent: async () => { stages.push("topology") }, + preflight: async () => { + stages.push("preflight") + if (denied === "preflight") throw Error("preflight refused") + return { routers: [] as never, expected: [] } + }, + restore: async () => assert.fail("Unadmitted topology inverse"), + }, + requiredCollectorWindowMs: 390000, + activateCollector: async window => { + assert.equal(window, 390000) + stages.push("activation") + throw Error("activation refused") + }, + capture: async () => assert.fail("Unadmitted capture"), + stagedCapture: async () => assert.fail("Unadmitted staged capture"), + generate: async () => assert.fail("Unadmitted generator"), + }), /refused/) + assert.deepEqual(stages, ["authorize", "admit", + ...(denied === "admission" ? [] : ["acquire", "topology", "preflight"]), + ...(denied === "activation" ? ["topology", "activation"] : [])]) + } +}) diff --git a/backend/src/test/traffic-flow-chr-vps-operator.ts b/backend/src/test/traffic-flow-chr-vps-operator.ts new file mode 100644 index 00000000..558f22b3 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-vps-operator.ts @@ -0,0 +1,267 @@ +/** Owning collector lifecycle for an admitted VPS runner. The supplied MCP + * adapter performs the existing settings plan, human approval and apply. */ +import { mkdir, open } from "node:fs/promises" +import { join } from "node:path" +import { isIPv4 } from "node:net" +import { setTimeout as delay } from "node:timers/promises" +import { captureOwnedChrPackets } from "./traffic-flow-chr-capture-process.js" +import { prepareChrReplay } from "./traffic-flow-chr-offline-replay.js" +import { checkChrBaselineIngress, executeOwnedChrCapture } from "./traffic-flow-chr-owned-sequence.js" +import { projectChrWire } from "./traffic-flow-chr-wire.js" +import { executeDisabledChrCapture, type DisabledChrCaptureOptions } from "./traffic-flow-chr-disabled-runner.js" + +type Sequence = Parameters[0] +type Scope = Sequence["scope"] & { interfaceName: string } +type Observation = { + instance: string; pid: number; invariantProof: string + listenAddress: string; listenPort: number; enabled: boolean; workerAlive: boolean; boundAddress: string | null + udpOwners: readonly { pid: number; address: string; port: number }[] +} +type Collector = { + authorize: () => Promise + read: () => Promise + assertDrained: () => Promise + applyEnabled: (enabled: boolean, beforeDispatch: () => Promise) => Promise<{ operationId: string; acknowledged: boolean }> +} +type OwnerOptions = { + directory: string; scope: Scope; deadlineAt: number + pins: { instance: string; pid: number; invariantProof: string } + collector: Collector + mayRestoreAfterFailure?: () => boolean +} +type Capture = { bytes: Buffer; sha256: string; complete: boolean; dropped: number } +function must(value: unknown, reason: string): asserts value { + if (!value) throw Error(reason) +} + +async function ownedCollector(options: OwnerOptions, work: (owner: Sequence["collector"] & { + retainCapture: (capture: Capture) => Promise +}) => Promise) { + const { directory, collector, deadlineAt } = options, scope = structuredClone(options.scope), pins = structuredClone(options.pins) + const live = () => must(Date.now() < deadlineAt, "Owned collector deadline ended") + must(Number.isSafeInteger(deadlineAt) && deadlineAt - Date.now() <= 600000 && deadlineAt > Date.now() && + /^[A-Za-z0-9][A-Za-z0-9_.-]{0,14}$/.test(scope.interfaceName) && scope.interfaceName !== "any" && + scope.sources.length === 2 && new Set([...scope.sources, scope.destination]).size === 3 && + [...scope.sources, scope.destination].every(isIPv4) && Number.isSafeInteger(scope.port) && scope.port > 0 && scope.port <= 65535 && + /^[a-f0-9]{64}$/.test(pins.invariantProof) && Number.isSafeInteger(pins.pid) && pins.pid > 0 && + typeof pins.instance === "string" && pins.instance.length > 0 && pins.instance.length <= 128, "Invalid collector ownership pins") + const authorize = async () => { live(); await collector.authorize(); live() } + const read = async () => { + await authorize() + const current = await collector.read() + await authorize() + must(current.instance === pins.instance && current.pid === pins.pid && current.invariantProof === pins.invariantProof && + current.listenAddress === scope.destination && current.listenPort === scope.port && + Array.isArray(current.udpOwners) && current.udpOwners.length <= 1 && + current.udpOwners.every(row => row.pid === pins.pid && row.address === scope.destination && row.port === scope.port), + "Collector instance, configuration or UDP owner changed") + return current + } + const idle = async () => { + const current = await read() + must(current.enabled === false && current.workerAlive === false && current.boundAddress === null && current.udpOwners.length === 0, + "Collector does not match the disabled baseline") + } + const endpoint = scope.destination + ":" + scope.port + const isBound = (current: Observation) => current.enabled === true && current.workerAlive === true && + current.boundAddress === endpoint && current.udpOwners.length === 1 + await idle() + await mkdir(directory, { mode: 0o700 }) + const syncDirectory = async (path: string) => { + if (process.platform === "win32") return + const handle = await open(path, "r") + try { await handle.sync() } finally { await handle.close() } + } + await syncDirectory(join(directory, "..")) + const journal = await open(join(directory, "collector.jsonl"), "wx", 0o600) + let enableOwned = false, restoreAttempted = false, restored = false + const append = async (record: object) => { + await journal.writeFile(JSON.stringify(record) + "\n") + await journal.sync() + } + const change = async (enabled: boolean, check: () => Promise) => { + await authorize() + await append({ phase: "intent", enabled }) + let dispatched = false + const receipt = await collector.applyEnabled(enabled, async () => { + must(!dispatched, "Collector write already dispatched") + await check() + must(!dispatched, "Collector write already dispatched") + dispatched = true + }) + must(dispatched && receipt.acknowledged === true && /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(receipt.operationId), "Collector write lacks acknowledged operation identity") + await append({ phase: "acknowledged", enabled, operationId: receipt.operationId }) + } + const restore = async () => { + must(enableOwned && !restoreAttempted, "Collector cleanup has no unspent acknowledged owner") + restoreAttempted = true + await change(false, async () => { + const current = await read() + must(current.enabled === true && (current.boundAddress === null || current.boundAddress === endpoint), "Owned collector changed before cleanup") + }) + await authorize() + await collector.assertDrained() + await idle() + await append({ phase: "restored" }) + restored = true + } + try { + await append({ phase: "baseline", pins, scope, enabled: false }) + await syncDirectory(directory) + await change(true, idle) + enableOwned = true + const startupDeadline = Math.min(deadlineAt, Date.now() + 5000) + for (;;) { + if (isBound(await read())) break + must(Date.now() < startupDeadline, "Owned collector did not bind") + await delay(50) + } + await append({ phase: "bound" }) + const result = await work({ + authorize, restore, + assertBound: async () => { must(isBound(await read()), "Owned collector bind is unavailable") }, + retainCapture: async capture => { + await authorize() + const wire = projectChrWire(capture.bytes, { sources: scope.sources, destination: scope.destination, + port: scope.port, ...(scope.linkType && { linkType: scope.linkType }) }) + must(capture.complete === true && capture.dropped === 0 && wire.captureSha256 === capture.sha256, "Capture pin or completeness differs") + const pcap = await open(join(directory, "capture.pcap"), "wx", 0o600) + try { await pcap.writeFile(capture.bytes); await pcap.sync() } finally { await pcap.close() } + await syncDirectory(directory) + await append({ phase: "capture-retained", sha256: capture.sha256, bytes: capture.bytes.length, frames: wire.frames.length }) + }, + }) + must(restored, "Owned exercise did not verify collector cleanup") + return result + } finally { + try { if (enableOwned && !restoreAttempted && (options.mayRestoreAfterFailure?.() ?? true)) await restore() } + finally { await journal.close() } + } +} + +export async function executeChrVpsOperator(options: OwnerOptions & Pick) { + must(process.platform === "linux", "Owned VPS capture requires Linux") + must(options.ingress.existingPort !== options.scope.port && options.serverIds.length === 2 && new Set(options.serverIds).size === 2 && + options.serverIds.every(id => Number.isSafeInteger(id) && id > 0) && options.targets.length === 2 && + options.targets[0].baseline.deviceId !== options.targets[1].baseline.deviceId, "Invalid dedicated two-CHR scope") + checkChrBaselineIngress(options.ingress, options.scope) + return ownedCollector(options, owner => executeOwnedChrCapture({ + ...options, journalPath: join(options.directory, "targets.jsonl"), collector: owner, + capture: async exercise => { + const captured = await captureOwnedChrPackets(options.scope, { + deadlineAt: Math.min(options.deadlineAt, Date.now() + 90000), assertCurrent: owner.assertBound, exercise, + }) + await owner.retainCapture(captured) + return { ...captured, ...prepareChrReplay(captured.wire) } + }, + })) +} + +type CollectorOwner = Pick & Pick +type DisabledOperatorOptions = Omit & + Pick & { + admitBeforeHardwareWrites: () => Promise + capture: (deadlineAt: number, exercise: (signal: AbortSignal) => Promise) => Promise + stagedCapture?: DisabledChrCaptureOptions["capture"] + topology: { + acquire: () => Promise + assertCurrent: () => Promise + preflight: () => Promise> + restore: () => Promise + } +} & ((CollectorOwner & { activateCollector?: never; authorizeFixture?: never }) | { + stagedCapture: DisabledChrCaptureOptions["capture"] + authorizeFixture: () => Promise + requiredCollectorWindowMs: number + activateCollector: (requiredWindowMs: number) => Promise }> +}) + +export async function executeDisabledChrVpsOperator(options: DisabledOperatorOptions) { + must(process.platform === "linux", "Owned VPS capture requires Linux") + return executeDisabledChrVpsOperatorCore(options) +} + +async function executeDisabledChrVpsOperatorCore(options: DisabledOperatorOptions) { + must(options.scope.linkType === "EN10MB" && /^nf[a-f0-9]{8}cv$/.test(options.scope.interfaceName), + "Disabled fixture requires exact Ethernet collector veth") + must(Number.isSafeInteger(options.deadlineAt) && Date.now() < options.deadlineAt && + options.deadlineAt - Date.now() <= (options.activateCollector ? 3600000 : 600000), "Invalid disabled VPS capture deadline") + const { topology } = options + const authorizeFixture = options.activateCollector ? options.authorizeFixture : options.collector.authorize + let acquired = false, collectorEntered = false, collectorRestored = false, collectorRetired = false, routersClean = true + const assertTopology = async () => { + must(Date.now() < options.deadlineAt, "Owned topology deadline ended") + await topology.assertCurrent() + must(Date.now() < options.deadlineAt, "Owned topology deadline ended") + } + try { + await authorizeFixture() + await options.admitBeforeHardwareWrites() + await topology.acquire() + acquired = true + await assertTopology() + const exercise = await topology.preflight() + await assertTopology() + const collectorOwner = options.activateCollector + ? await options.activateCollector(options.requiredCollectorWindowMs) + : options + const activation = "retireCollector" in collectorOwner ? collectorOwner : undefined + const collectorDeadlineAt = activation?.credentialDeadlineAt ?? options.deadlineAt + must(Number.isSafeInteger(collectorDeadlineAt) && Date.now() < collectorDeadlineAt && + collectorDeadlineAt <= options.deadlineAt && collectorDeadlineAt - Date.now() <= 600000 && + (!options.activateCollector || collectorDeadlineAt - Date.now() > options.requiredCollectorWindowMs), "Invalid collector activation deadline") + collectorEntered = true + const result = await ownedCollector({ ...options, ...collectorOwner, deadlineAt: collectorDeadlineAt, mayRestoreAfterFailure: () => routersClean }, async owner => { + let restoreAttempted = false + const restore = async () => { + restoreAttempted = true + await owner.restore() + collectorRestored = true + } + try { + const captureDeadlineAt = Math.min(collectorDeadlineAt, Date.now() + 90000) + const stagedCapture = options.stagedCapture + const { sources, destination, port, linkType } = options.scope + return await executeDisabledChrCapture({ + ...options, ...collectorOwner, ...exercise, deadlineAt: collectorDeadlineAt, scope: { sources, destination, port, linkType }, + ...(stagedCapture ? { stagedTarget: true as const } + : { targetTimerProfile: { kind: "bounded-ipfix-timeout-20s" as const, captureDeadlineAt } }), + journalPath: join(options.directory, "disabled-routers.jsonl"), assertTopology, + authorize: owner.authorize, drainCollector: restore, + reportRouterCleanup: clean => { routersClean = clean }, + capture: async work => { + await owner.assertBound() + const exercise: Parameters[0] = async (signal, timer) => { + await owner.assertBound() + await work(signal, timer) + } + const captured = stagedCapture + ? await stagedCapture(exercise) + : await options.capture(captureDeadlineAt, signal => exercise(signal)) + await owner.assertBound() + await owner.retainCapture(captured) + return captured + }, + }) + } finally { + if (!restoreAttempted && routersClean) await restore() + } + }) + if (activation) { + await activation.retireCollector() + collectorRetired = true + } + return result + } finally { + if (acquired && routersClean && (options.activateCollector ? collectorRetired : !collectorEntered || collectorRestored)) { + await authorizeFixture() + await assertTopology() + await topology.restore() + } + } +} + +export const executeOwnedCollectorForTests = ownedCollector +export const executeDisabledChrVpsOperatorForTests = executeDisabledChrVpsOperatorCore + + diff --git a/backend/src/test/traffic-flow-chr-wire.test.ts b/backend/src/test/traffic-flow-chr-wire.test.ts new file mode 100644 index 00000000..e057531d --- /dev/null +++ b/backend/src/test/traffic-flow-chr-wire.test.ts @@ -0,0 +1,132 @@ +import assert from "node:assert/strict" +import { createHash } from "node:crypto" +import { test } from "node:test" +import { projectChrEgress, projectChrWire } from "./traffic-flow-chr-wire.js" + +const scope = { sources: ["192.0.2.1", "192.0.2.2"] as const, destination: "192.0.2.100", port: 4739 } +function capture(ethernet = false, etherType = 0x0800) { + const header = Buffer.alloc(24) + header.writeUInt32LE(0xa1b2c3d4) + header.writeUInt16LE(2, 4) + header.writeUInt16LE(4, 6) + header.writeUInt32LE(ethernet ? 4138 : 4124, 16) + header.writeUInt32LE(ethernet ? 1 : 101, 20) + const frames = [1, 2].map(lastOctet => { + const link = ethernet ? 14 : 0 + const record = Buffer.alloc(16 + link + 20 + 8 + 16) + record.writeUInt32LE(1700000000) + record.writeUInt32LE(lastOctet, 4) + record.writeUInt32LE(44 + link, 8) + record.writeUInt32LE(44 + link, 12) + if (ethernet) record.writeUInt16BE(etherType, 16 + 12) + const ip = record.subarray(16 + link) + ip[0] = 0x45 + ip.writeUInt16BE(44, 2) + ip[9] = 17 + Buffer.from([192, 0, 2, lastOctet, 192, 0, 2, 100]).copy(ip, 12) + ip.writeUInt16BE(2055, 20) + ip.writeUInt16BE(4739, 22) + ip.writeUInt16BE(24, 24) + ip.writeUInt16BE(10, 28) + ip.writeUInt16BE(16, 30) + ip.writeUInt32BE(lastOctet, 40) + return record + }) + return Buffer.concat([header, ...frames]) +} + +test("projects exact wire payload, exporter source port, time and domain from a two-source pcap", () => { + const pcap = capture() + const projected = projectChrWire(pcap, scope) + assert.deepEqual(projected.frames.map(({ exporter, sourcePort, domain, receivedNs }) => [exporter, sourcePort, domain, receivedNs]), + [[0, 2055, 1, "1700000000000001000"], [1, 2055, 2, "1700000000000002000"]]) + assert.equal(projected.frames[0].payloadHex, pcap.subarray(68, 84).toString("hex")) + assert.match(projected.captureSha256, /^[a-f0-9]{64}$/) +}) + +test("one-router egress preserves the original digest and refuses another exporter", () => { + const bytes = capture().subarray(0, 84) + const egress = { source: scope.sources[0], destination: scope.destination, port: scope.port } + const result = projectChrEgress(bytes, egress) + assert.equal(result.capturePoint, "router-egress") + assert.equal(result.captureSha256, createHash("sha256").update(bytes).digest("hex")) + assert.equal(result.frames.length, 1) + assert.equal(result.frames[0].sourcePort, 2055) + assert.throws(() => projectChrEgress(capture(), egress), /outside admitted UDP tuple/) + assert.throws(() => projectChrEgress(bytes, { ...egress, source: scope.sources[1] }), /outside admitted UDP tuple/) + assert.throws(() => projectChrWire(bytes, scope), /Both exporters/) +}) + +test("refuses wrong link type, incomplete wire bytes, fragments, wrong destinations and single-exporter capture", () => { + assert.throws(() => projectChrWire(capture().subarray(0, -1), scope), /Incomplete/) + assert.throws(() => projectChrWire(capture().subarray(0, 84), scope), /Both exporters/) + for (const mutate of [ + (bytes: Buffer) => bytes.writeUInt32LE(1, 20), + (bytes: Buffer) => bytes.writeUInt16BE(0x2000, 46), + (bytes: Buffer) => { bytes[59] = 101 }, + (bytes: Buffer) => bytes.writeUInt16BE(9999, 62), + ]) { + const bytes = capture() + mutate(bytes) + assert.throws(() => projectChrWire(bytes, scope)) + } +}) + +test("pins Ethernet framing, preserves original hash, and never infers a link type", () => { + const ethernet = capture(true) + const original = Buffer.from(ethernet) + const ethScope = { ...scope, linkType: "EN10MB" } as const + const result = projectChrWire(ethernet, ethScope) + assert.deepEqual(result.frames, projectChrWire(capture(), { ...scope, linkType: "RAW" }).frames) + assert.equal(result.captureSha256, createHash("sha256").update(original).digest("hex")) + assert.deepEqual(ethernet, original) + assert.throws(() => projectChrWire(ethernet, scope), /pinned link type RAW/) + assert.throws(() => projectChrWire(capture(), ethScope), /pinned link type EN10MB/) + for (const etherType of [0x8100, 0x88a8, 0x86dd, 0x0806]) + assert.throws(() => projectChrWire(capture(true, etherType), ethScope), /untagged Ethernet IPv4/) +}) + +test("projects PCAPNG Ethernet packets with nanosecond timestamps and preserves the file hash", () => { + const block = (type: number, payload: Buffer) => { + const bytes = Buffer.alloc(payload.length + 12) + bytes.writeUInt32LE(type) + bytes.writeUInt32LE(bytes.length, 4) + payload.copy(bytes, 8) + bytes.writeUInt32LE(bytes.length, bytes.length - 4) + return bytes + } + const section = Buffer.alloc(16, 0xff) + section.writeUInt32LE(0x1a2b3c4d) + section.writeUInt16LE(1, 4) + section.writeUInt16LE(0, 6) + const iface = Buffer.alloc(20) + iface.writeUInt16LE(1) + iface.writeUInt32LE(4138, 4) + iface.writeUInt16LE(9, 8) + iface.writeUInt16LE(1, 10) + iface[12] = 9 + const classic = capture(true) + const frames = [40, 114].map((start, index) => { + const frame = classic.subarray(start, start + 58) + const body = Buffer.alloc(80) + const ticks = 1700000000000000000n + BigInt(index + 1) + body.writeUInt32LE(Number(ticks >> 32n), 4) + body.writeUInt32LE(Number(ticks & 0xffffffffn), 8) + body.writeUInt32LE(frame.length, 12) + body.writeUInt32LE(frame.length, 16) + frame.copy(body, 20) + return block(6, body) + }) + const bytes = Buffer.concat([block(0x0a0d0d0a, section), block(1, iface), ...frames]) + const ethScope = { ...scope, linkType: "EN10MB" } as const + const result = projectChrWire(bytes, ethScope) + assert.deepEqual(result.frames.map(({ receivedNs, ...frame }) => [receivedNs, frame]), + projectChrWire(classic, ethScope).frames.map(({ receivedNs, ...frame }, index) => + [(1700000000000000000n + BigInt(index + 1)).toString(), frame])) + assert.equal(result.captureSha256, createHash("sha256").update(bytes).digest("hex")) + assert.throws(() => projectChrWire(bytes.subarray(0, -1), ethScope), /PCAPNG block length/) + assert.throws(() => projectChrWire(bytes, scope), /link type/) + const unknownInterface = Buffer.from(bytes) + unknownInterface.writeUInt32LE(1, 68) + assert.throws(() => projectChrWire(unknownInterface, ethScope), /PCAPNG frame/) +}) diff --git a/backend/src/test/traffic-flow-chr-wire.ts b/backend/src/test/traffic-flow-chr-wire.ts new file mode 100644 index 00000000..932aca5d --- /dev/null +++ b/backend/src/test/traffic-flow-chr-wire.ts @@ -0,0 +1,137 @@ +/** Offline projection of bounded IPv4/UDP pcap and PCAPNG files. */ +import { createHash } from "node:crypto" +import { open } from "node:fs/promises" +import { isIPv4 } from "node:net" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import { readChrPcapng } from "./traffic-flow-chr-pcapng.js" + +const maxCaptureBytes = 512 * 1024 +export type ChrCaptureLinkType = "RAW" | "EN10MB" +type Scope = { sources: readonly [string, string]; destination: string; port: number; linkType?: ChrCaptureLinkType } +export function chrCaptureLayout(linkType: ChrCaptureLinkType = "RAW") { + switch (linkType) { + case "RAW": return { linkType, pcapType: 101, ipOffset: 0, snapLength: 4124 } + case "EN10MB": return { linkType, pcapType: 1, ipOffset: 14, snapLength: 4138 } + default: throw Error("Unsupported capture link type") + } +} +const magic = new Map([["d4c3b2a1", [true, false]], ["a1b2c3d4", [false, false]], ["4d3cb2a1", [true, true]], ["a1b23c4d", [false, true]]]) +function must(value: unknown, error: string): asserts value { + if (!value) throw Error(error) +} + +export function projectChrWire(bytes: Buffer, scope: Scope) { + must(scope.sources.length === 2, "Expected scope for two exporters") + const projection = projectWire(bytes, scope) + must(projection.frames.some(frame => frame.exporter === 0) && projection.frames.some(frame => frame.exporter === 1), "Both exporters must be present") + return projection +} + +/** Egress capture on one router, with its own clock and original file digest. */ +export function projectChrEgress(bytes: Buffer, scope: { source: string; destination: string; port: number; linkType?: ChrCaptureLinkType }) { + const projection = projectWire(bytes, { ...scope, sources: [scope.source] }) + must(projection.frames.length > 0, "Expected exporter packets") + return { ...projection, scope, capturePoint: "router-egress" as const } +} + +function projectWire(bytes: Buffer, scope: S) { + must(new Set([...scope.sources, scope.destination]).size === scope.sources.length + 1 && + [...scope.sources, scope.destination].every(isIPv4) && Number.isInteger(scope.port) && scope.port > 0 && scope.port <= 65535, "Invalid capture scope") + const layout = chrCaptureLayout(scope.linkType) + must(bytes.length >= 24 && bytes.length <= maxCaptureBytes, "Capture outside size bound") + const packets = bytes.readUInt32BE(0) === 0x0a0d0d0a + ? readChrPcapng(bytes, layout.pcapType, layout.snapLength) : classicPcap(bytes, layout) + const frames = [] + let lastTimestamp = -1n + for (const { timestamp, frame } of packets) { + must(timestamp >= lastTimestamp, "Capture timestamps went backwards") + lastTimestamp = timestamp + must(frame.length >= layout.ipOffset + 28, "Incomplete UDP capture") + if (layout.linkType === "EN10MB") must(frame.readUInt16BE(12) === 0x0800, "Expected untagged Ethernet IPv4") + const packet = frame.subarray(layout.ipOffset) + const header = (packet[0] & 15) * 4 + must(packet[0] >>> 4 === 4 && header >= 20 && packet.length >= header + 8 && + packet.readUInt16BE(2) === packet.length && (packet.readUInt16BE(6) & 0x3fff) === 0 && packet[9] === 17, "Expected complete unfragmented IPv4 UDP") + const source = [...packet.subarray(12, 16)].join(".") + const destination = [...packet.subarray(16, 20)].join(".") + const exporter = scope.sources.indexOf(source), sourcePort = packet.readUInt16BE(header) + const payload = packet.subarray(header + 8) + must(exporter >= 0 && destination === scope.destination && sourcePort > 0 && packet.readUInt16BE(header + 2) === scope.port && + packet.readUInt16BE(header + 4) === packet.length - header, "Packet outside admitted UDP tuple") + const version = payload.length >= 2 ? payload.readUInt16BE(0) : 0 + must(payload.length <= 4096 && (version === 9 && payload.length >= 20 || version === 10 && payload.length >= 16 && + payload.readUInt16BE(2) === payload.length), "Invalid or unsupported NetFlow payload") + frames.push({ + ordinal: frames.length + 1, receivedMs: Number(timestamp / 1000000n), receivedNs: timestamp.toString(), exporter, + sourcePort, version, domain: payload.readUInt32BE(version === 9 ? 16 : 12), payloadHex: payload.toString("hex"), + }) + } + return { captureSha256: createHash("sha256").update(bytes).digest("hex"), scope, frames } +} + +function classicPcap(bytes: Buffer, layout: ReturnType) { + const encoding = magic.get(bytes.subarray(0, 4).toString("hex")) + must(encoding, "Expected classic pcap") + const [little, nanos] = encoding + const uint = (at: number) => little ? bytes.readUInt32LE(at) : bytes.readUInt32BE(at) + const short = (at: number) => little ? bytes.readUInt16LE(at) : bytes.readUInt16BE(at) + must(short(4) === 2 && short(6) === 4 && uint(20) === layout.pcapType, "Expected pcap v2.4 with pinned link type " + layout.linkType) + const frames = [] + for (let at = 24; at < bytes.length;) { + must(frames.length < 64 && bytes.length - at >= 16, "Capture has too many frames or an incomplete frame header") + const seconds = uint(at), fraction = uint(at + 4), captured = uint(at + 8), original = uint(at + 12) + must(fraction < (nanos ? 1e9 : 1e6) && captured === original && captured >= layout.ipOffset + 28 && captured <= layout.snapLength && + captured <= uint(16) && bytes.length - at - 16 >= captured, "Incomplete or oversized UDP capture") + const timestamp = BigInt(seconds) * 1000000000n + BigInt(fraction) * (nanos ? 1n : 1000n) + const frame = bytes.subarray(at + 16, at + 16 + captured) + frames.push({ timestamp, frame }) + at += 16 + captured + } + return frames +} + +async function readPinnedCapture(path: string, sha: string) { + must(path && /^[a-f0-9]{64}$/.test(sha ?? ""), "Expected capture path and SHA-256") + const file = await open(path, "r") + try { + const stat = await file.stat() + must(stat.isFile() && stat.size > 0 && stat.size <= maxCaptureBytes, "Capture file outside size bound") + const bytes = Buffer.alloc(stat.size + 1) + let size = 0 + while (size < bytes.length) { + const { bytesRead } = await file.read(bytes, size, bytes.length - size, size) + if (!bytesRead) break + size += bytesRead + } + must(size === stat.size, "Capture changed while reading") + const capture = bytes.subarray(0, size) + must(createHash("sha256").update(capture).digest("hex") === sha, "Capture SHA-256 differs") + return capture + } finally { + await file.close() + } +} +async function main() { + const args = process.argv.slice(2) + const compare = args[0] === "--egress-compare" + const egress = compare || args[0] === "--egress" + if (egress) args.shift() + const [path, sha, first] = args + const [destination, port, linkType, ...rest] = args.slice(egress ? 3 : 4) + must(first && (egress || args[3]) && destination && /^\d{1,5}$/.test(port ?? "") && !rest.length && + (linkType === undefined || linkType === "RAW" || linkType === "EN10MB"), "Expected capture path, SHA-256 and exact scope") + const bytes = await readPinnedCapture(path, sha) + const receiver: Omit = { destination, port: Number(port), ...(linkType === undefined ? {} : { linkType }) } + const projected = egress ? projectChrEgress(bytes, { source: first, ...receiver }) : + projectChrWire(bytes, { sources: [first, args[3]], ...receiver }) + if (compare && "capturePoint" in projected) { + const { replayChrEgress } = await import("./traffic-flow-chr-egress-replay.js") + console.log(JSON.stringify(replayChrEgress(projected))) + return + } + console.log(JSON.stringify(projected)) +} +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch(() => { console.error("CHR wire capture refused"); process.exitCode = 1 }) +} diff --git a/backend/src/test/traffic-flow-chr-write-admission-ipc.test.ts b/backend/src/test/traffic-flow-chr-write-admission-ipc.test.ts new file mode 100644 index 00000000..5272c69a --- /dev/null +++ b/backend/src/test/traffic-flow-chr-write-admission-ipc.test.ts @@ -0,0 +1,130 @@ +import { spawn } from "node:child_process" +import { once } from "node:events" +import assert from "node:assert/strict" +import { PassThrough } from "node:stream" +import { test } from "node:test" +import { createChrChildAdmissionPipe, serveChrParentAdmissionPipe } from "./traffic-flow-chr-write-admission-ipc.js" +import { createChrParentWriteAdmission, type ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" +import type { ChrStagedFixtureLease } from "./traffic-flow-chr-target-timing-profile.js" + +const first: ChrWriteIntent = { step: "fixture:create", side: "A", deviceId: "a", method: "PUT", + path: "/interface/wireguard", body: { name: "owned" } } +const second: ChrWriteIntent = { step: "fixture:delete", side: "A", deviceId: "a", method: "DELETE", + path: "/interface/wireguard/*A", body: {} } +const wire = ({ side, method, path, body }: ChrWriteIntent) => ({ side, method, path, body }) +function fixture({ failProof = false, deadlineMs = 1000, staged = false, leaseProfile }: { + failProof?: boolean; deadlineMs?: number; staged?: boolean; leaseProfile?: ChrStagedFixtureLease +} = {}) { + const parentIn = new PassThrough(), childIn = new PassThrough(), verified: string[] = [], reserved: string[] = [] + const deadlineAt = Date.now() + deadlineMs + const profile = staged ? "staged-disabled-target" : "direct-target" + const gate = createChrParentWriteAdmission({ deadlineAt, profile, deviceIds: { A: "a", B: "b" }, assertCurrent: async () => {} }) + const stages = [first, second].map(intent => ({ + step: intent.step, + async intent() { + reserved.push(intent.step) + if (intent === second) assert.deepEqual(verified, [first.step]) + return intent + }, + async verify(command: ChrWriteIntent) { + assert.deepEqual(command, intent) + if (failProof) throw Error("Independent proof refused") + verified.push(command.step) + }, + })) + const parent = serveChrParentAdmissionPipe({ input: parentIn, output: childIn }, gate, stages, deadlineAt, profile, leaseProfile) + parent.catch(() => {}) + const child = createChrChildAdmissionPipe({ input: childIn, output: parentIn }, profile) + const send = (intent: ChrWriteIntent) => child.run(intent, async arm => { + await arm() + await gate.authorizeWrite(wire(intent)) + return "native-ack" + }) + return { child, parent, gate, send, verified, reserved, parentIn, childIn } +} +test("the staged write schedule alone cannot extend the admission pipe lease", async () => { + const leaseProfile = "staged-disabled-target" + assert.throws(() => fixture({ deadlineMs: 1200000, staged: true }), /pipe refused/) + assert.throws(() => fixture({ deadlineMs: 1200000, leaseProfile }), /pipe refused/) + assert.throws(() => fixture({ deadlineMs: 3700000, staged: true, leaseProfile }), /pipe refused/) + const f = fixture({ deadlineMs: 1200000, staged: true, leaseProfile }) + assert.equal(await f.send(first), "native-ack") + assert.equal(await f.send(second), "native-ack") + f.child.close() + assert.deepEqual(await f.parent, { completed: 2 }) +}) +test("only the trusted ordered intent can arm and consume the relay, with independent proof before next step", async () => { + const f = fixture() + assert.equal(await f.send(first), "native-ack") + assert.equal(await f.send(second), "native-ack") + f.child.close() + assert.deepEqual(await f.parent, { completed: 2 }) + assert.deepEqual(f.verified, [first.step, second.step]) + assert.equal(f.gate.consumed, 2) +}) +test("out-of-order step, child-chosen body and extra arbitrary intent fields refuse before socket consumption", async () => { + for (const altered of [second, { ...first, body: { name: "foreign" } }, { ...first, deviceId: "b" }]) { + const f = fixture() + await assert.rejects(f.send(altered)) + await assert.rejects(f.parent) + assert.equal(f.gate.consumed, 0) + assert.deepEqual(f.verified, []) + } + const f = fixture() + f.parentIn.end(JSON.stringify({ sequence: 1, op: "reserve", step: first.step, body: { name: "foreign" } }) + "\n") + await assert.rejects(f.parent) + assert.deepEqual(f.reserved, []) +}) +test("child completion cannot substitute for relay consumption or independent parent proof", async () => { + const unconsumed = fixture() + await assert.rejects(unconsumed.child.run(first, async arm => { await arm(); return "invented" })) + await assert.rejects(unconsumed.parent) + assert.equal(unconsumed.gate.consumed, 0) + const unproven = fixture({ failProof: true }) + await assert.rejects(unproven.send(first)) + await assert.rejects(unproven.parent) + assert.deepEqual(unproven.reserved, [first.step]) + assert.equal(unproven.gate.consumed, 1) +}) +test("EOF and deadline retire unused parent grant", async () => { + const eof = fixture() + eof.child.close() + await assert.rejects(eof.parent) + assert.equal(eof.gate.consumed, 0) + const expired = fixture({ deadlineMs: 20 }) + await assert.rejects(expired.parent) + expired.child.close() + assert.equal(expired.gate.consumed, 0) +}) + +test("actual inherited descriptor pair carries only control while parent consumes a separate mock relay event", async () => { + const deadlineAt = Date.now() + 10000 + const gate = createChrParentWriteAdmission({ deadlineAt, deviceIds: { A: "a", B: "b" }, assertCurrent: async () => {} }) + const file = new URL("./traffic-flow-chr-write-admission-ipc.ts", import.meta.url).href + const source = [ + "import { once } from 'node:events';", + "const {openInheritedChrChildAdmission}=await import(" + JSON.stringify(file) + ");", + "const child=openInheritedChrChildAdmission();", + "await child.run(" + JSON.stringify(first) + ",async arm=>{await arm();const ack=once(process.stdin,'data');", + "process.stdout.write('relay-event\\n');await ack;process.stdin.pause();return 'ack';});", + "child.close();", + ].join("") + const child = spawn(process.execPath, ["--import", "tsx", "--input-type=module", "-e", source], + { stdio: ["pipe", "pipe", "ignore", "pipe", "pipe"], windowsHide: true }) + const exited = once(child, "exit") + const event = once(child.stdout!, "data") + const [parentOut, parentIn] = [child.stdio[3], child.stdio[4]] + assert(parentOut?.writable && parentIn?.readable) + const parent = serveChrParentAdmissionPipe({ input: parentIn, output: parentOut }, gate, + [{ step: first.step, intent: async () => first, verify: async () => {} }], deadlineAt) + parent.catch(() => {}) + try { + const [chunk] = await Promise.race([event, exited.then(() => assert.fail("Child exited before mock relay"))]) + assert.match(String(chunk), /^relay-event/) + await gate.authorizeWrite(wire(first)) + child.stdin!.end("ack") + assert.deepEqual(await parent, { completed: 1 }) + assert.deepEqual(await exited, [0, null]) + assert.equal(gate.consumed, 1) + } finally { if (child.exitCode === null && child.signalCode === null) child.kill() } +}) diff --git a/backend/src/test/traffic-flow-chr-write-admission-ipc.ts b/backend/src/test/traffic-flow-chr-write-admission-ipc.ts new file mode 100644 index 00000000..35cad73a --- /dev/null +++ b/backend/src/test/traffic-flow-chr-write-admission-ipc.ts @@ -0,0 +1,180 @@ +import { createHash } from "node:crypto" +import { createReadStream, createWriteStream } from "node:fs" +import type { Readable, Writable } from "node:stream" +import { chrWriteScheduleLimit, type ChrWriteAdmission, type ChrWriteIntent, type ChrWriteScheduleProfile } from "./traffic-flow-chr-write-admission.js" +import { chrFixtureDeadlineMaxMs, type ChrStagedFixtureLease } from "./traffic-flow-chr-target-timing-profile.js" + +type Pipes = { input: Readable; output: Writable } +type Gate = ChrWriteAdmission & { retire(): void } +type Stage = { step: string; intent(): Promise; verify(intent: Readonly): Promise } +type Request = { sequence: number; op: "reserve"; step: string } | + { sequence: number; op: "arm" | "finish" } +type Response = { sequence: number; ok: true; digest?: string } | { sequence: number; ok: false } +const maxFrame = 512 +const safeStep = /^[a-zA-Z0-9:._-]{1,100}$/ +const digestPattern = /^[a-f0-9]{64}$/ +const refused = () => { throw Error("Owned CHR admission pipe refused") } + +function digest(intent: ChrWriteIntent) { + const { step, deviceId, side, method, path, body } = intent + return createHash("sha256").update(JSON.stringify([step, deviceId, side, method, path, + Object.entries(body).sort(([left], [right]) => left.localeCompare(right))])).digest("hex") +} + +async function* frames(input: Readable, maxFrames: number) { + let buffered = Buffer.alloc(0), count = 0 + for await (const incoming of input) { + const bytes = Buffer.isBuffer(incoming) ? incoming : Buffer.from(incoming) + if (bytes.length > maxFrame * maxFrames) refused() + for (let offset = 0; offset < bytes.length;) { + const newline = bytes.indexOf(10, offset) + const end = newline < 0 ? bytes.length : newline + if (buffered.length + end - offset > maxFrame) refused() + buffered = Buffer.concat([buffered, bytes.subarray(offset, end)]) + if (newline < 0) break + if (!buffered.length || ++count > maxFrames) refused() + const value: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(buffered)) + if (!value || typeof value !== "object" || Array.isArray(value)) refused() + yield value as Record + buffered = Buffer.alloc(0) + offset = newline + 1 + } + } + if (buffered.length) refused() +} + +function write(output: Writable, frame: Request | Response) { + const body = JSON.stringify(frame) + "\n" + if (Buffer.byteLength(body) > maxFrame || output.destroyed) return Promise.reject(Error("Owned CHR admission pipe closed")) + return new Promise((resolve, reject) => output.write(body, error => error ? reject(error) : resolve())) +} + +function defer() { + let resolve!: () => void, reject!: (error: Error) => void + const promise = new Promise((accept, deny) => { resolve = accept; reject = deny }) + promise.catch(() => {}) + return { promise, resolve, reject } +} + +function shape(frame: Record, keys: readonly string[]) { + return Object.keys(frame).length === keys.length && keys.every(key => Object.hasOwn(frame, key)) +} + +/** Parent input/output are the two ends created by the trusted launcher. + * Stage factories run only here and must derive changing IDs from independently + * confirmed state. The child can name the next step, never its native intent. */ +export function serveChrParentAdmissionPipe(pipes: Pipes, gate: Gate, trustedStages: readonly Stage[], deadlineAt: number, + profile: ChrWriteScheduleProfile = "direct-target", leaseProfile?: ChrStagedFixtureLease) { + const maxWrites = chrWriteScheduleLimit(profile) + const stages = trustedStages.map(stage => ({ ...stage })) + if (!stages.length || stages.length > maxWrites || new Set(stages.map(stage => stage.step)).size !== stages.length || + (leaseProfile !== undefined && leaseProfile !== profile) || !Number.isSafeInteger(deadlineAt) || + deadlineAt <= Date.now() || deadlineAt - Date.now() > chrFixtureDeadlineMaxMs(leaseProfile) || + !stages.every(stage => safeStep.test(stage.step) && typeof stage.intent === "function" && typeof stage.verify === "function")) return refused() + pipes.output.on("error", () => {}) + return (async () => { + const timer = setTimeout(() => { gate.retire(); pipes.input.destroy() }, Math.max(1, deadlineAt - Date.now())) + let sequence = 0, cursor = 0 + let slot: { intent: ChrWriteIntent; arm(): Promise; completion: ReturnType; running: Promise; armed: boolean } | undefined + try { + for await (const frame of frames(pipes.input, maxWrites * 3)) { + if (Date.now() >= deadlineAt || frame.sequence !== ++sequence) refused() + if (frame.op === "reserve") { + const stage = stages[cursor] + if (slot || !stage || !shape(frame, ["sequence", "op", "step"]) || frame.step !== stage.step) return refused() + const intent = structuredClone(await stage.intent()) + if (intent.step !== stage.step) refused() + const completion = defer() + let arm: (() => Promise) | undefined + const running = gate.run(intent, callback => { arm = callback; return completion.promise }) + running.catch(() => {}) + if (!arm) { await running; return refused() } + slot = { intent, arm, completion, running, armed: false } + await write(pipes.output, { sequence, ok: true, digest: digest(intent) }) + continue + } + if (!slot || !shape(frame, ["sequence", "op"])) return refused() + if (frame.op === "arm" && !slot.armed) { + await slot.arm() + slot.armed = true + } else if (frame.op === "finish" && slot.armed) { + slot.completion.resolve() + await slot.running + await stages[cursor].verify(structuredClone(slot.intent)) + if (Date.now() >= deadlineAt) refused() + slot = undefined + cursor++ + } else return refused() + await write(pipes.output, { sequence, ok: true }) + } + if (slot || cursor !== stages.length || Date.now() >= deadlineAt) refused() + return { completed: cursor } + } catch (error) { + slot?.completion.reject(Error("Owned CHR admission pipe interrupted")) + await slot?.running.catch(() => {}) + await write(pipes.output, { sequence, ok: false }).catch(() => {}) + throw error + } finally { + clearTimeout(timer) + gate.retire() + pipes.output.end() + if (!pipes.input.destroyed) pipes.input.destroy() + } + })() +} + +/** Child adapter for the existing owner callback. Wire command content is never + * transmitted in the admission pipe. Native HTTP remains a separate channel. */ +export function createChrChildAdmissionPipe(pipes: Pipes, profile: ChrWriteScheduleProfile = "direct-target") { + const maxFrames = chrWriteScheduleLimit(profile) * 3 + pipes.output.on("error", () => {}) + let sequence = 0, busy = false, closed = false + const replies = frames(pipes.input, maxFrames) + async function exchange(request: Omit & { step?: string }, expectDigest = false) { + const current = ++sequence + if (current > maxFrames) return refused() + await write(pipes.output, { ...request, sequence: current } as Request) + const response = await replies.next() + if (response.done || response.value.sequence !== current || response.value.ok !== true || + !shape(response.value, expectDigest ? ["sequence", "ok", "digest"] : ["sequence", "ok"])) return refused() + return response.value + } + const close = () => { + closed = true + pipes.output.end() + if (!pipes.input.destroyed) pipes.input.destroy() + } + return Object.freeze({ + close, + async run(intent: ChrWriteIntent, send: (arm: () => Promise) => Promise) { + if (closed || busy || !safeStep.test(intent.step)) return refused() + busy = true + try { + const expected = digest(structuredClone(intent)) + const response = await exchange({ op: "reserve", step: intent.step }, true) + if (typeof response.digest !== "string" || !digestPattern.test(response.digest) || response.digest !== expected) return refused() + let armed = false + const result = await send(async () => { + if (armed) return refused() + armed = true + await exchange({ op: "arm" }) + }) + if (!armed) return refused() + await exchange({ op: "finish" }) + return result + } catch (error) { + close() + throw error + } finally { busy = false } + }, + }) +} + +/** The launcher grants only these inherited descriptors to the child. */ +export function openInheritedChrChildAdmission() { + return createChrChildAdmissionPipe({ + input: createReadStream("", { fd: 3, autoClose: true }), + output: createWriteStream("", { fd: 4, autoClose: true }), + }) +} + diff --git a/backend/src/test/traffic-flow-chr-write-admission.test.ts b/backend/src/test/traffic-flow-chr-write-admission.test.ts new file mode 100644 index 00000000..529b2398 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-write-admission.test.ts @@ -0,0 +1,81 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { createChrParentWriteAdmission, type ChrWriteIntent } from "./traffic-flow-chr-write-admission.js" + +const first: ChrWriteIntent = { step: "topology:create:A:edgeA", side: "A", deviceId: "device-a", method: "PUT", + path: "/interface/wireguard", body: { name: "fixture", "listen-port": "50101" } } +const second: ChrWriteIntent = { step: "flow:B:flow.enable", side: "B", deviceId: "device-b", method: "POST", + path: "/ip/traffic-flow/set", body: { enabled: "yes", interfaces: "fixture" } } +const wire = ({ side, method, path, body }: ChrWriteIntent) => ({ side, method, path, body }) +function fixture() { + const identities: string[] = [] + let now = 1, revoked = false + const gate = createChrParentWriteAdmission({ deadlineAt: 100, now: () => now, deviceIds: { A: "device-a", B: "device-b" }, + assertCurrent: async (side, identity) => { identities.push(side + ":" + identity); if (revoked) throw Error("revoked") } }) + const send = (intent: ChrWriteIntent) => gate.run(intent, async arm => { await arm(); await gate.authorizeWrite(wire(intent)); return "ACK" }) + return { gate, identities, send, expire: () => { now = 100 }, revoke: () => { revoked = true } } +} +test("relay consumes one exact armed command and distinct ordered intent for both devices", async () => { + const f = fixture() + assert.equal(await f.send(first), "ACK") + assert.equal(await f.send(second), "ACK") + assert.equal(f.gate.consumed, 2) + assert.deepEqual(f.identities, ["A:device-a", "A:device-a", "B:device-b", "B:device-b"]) + await assert.rejects(f.send(first), /admission refused/) + assert.equal(f.gate.consumed, 2) +}) +test("wrong route, method, body, device, unarmed write and duplicate are refused without a new grant", async () => { + for (const altered of [ + { ...wire(first), side: "B" as const }, { ...wire(first), path: "/ip/address" }, + { ...wire(first), method: "PATCH" }, { ...wire(first), body: { ...first.body, "listen-port": "50102" } }, + ]) { + const f = fixture() + await assert.rejects(f.gate.run(first, async arm => { await arm(); await f.gate.authorizeWrite(altered) })) + await assert.rejects(f.send(first)) + assert.equal(f.gate.consumed, 0) + } + const invalid = fixture() + await assert.rejects(invalid.send({ ...first, deviceId: "device-b" })) + const unarmed = fixture() + await assert.rejects(unarmed.gate.run(first, async () => unarmed.gate.authorizeWrite(wire(first)))) + const duplicate = fixture() + await assert.rejects(duplicate.gate.run(first, async arm => { + await arm() + await duplicate.gate.authorizeWrite(wire(first)) + await duplicate.gate.authorizeWrite(wire(first)) + })) + assert.equal(duplicate.gate.consumed, 1) +}) +test("authority drift, deadline and absent socket consumption refuse after local arming", async () => { + for (const kind of ["authority", "deadline", "missing"] as const) { + const f = fixture() + await assert.rejects(f.gate.run(first, async arm => { + await arm() + if (kind === "authority") f.revoke() + if (kind === "deadline") f.expire() + if (kind !== "missing") await f.gate.authorizeWrite(wire(first)) + })) + assert.equal(f.gate.consumed, 0) + await assert.rejects(f.send(second)) + } +}) + +test("staged target PATCH requires explicit profile, exact step, ID path and disabled-only body", async () => { + const intent = { step: "target:A:enable", deviceId: "A", side: "A", method: "PATCH", + path: "/ip/traffic-flow/target/*A", body: { disabled: "no" } } as const + for (const profile of ["direct-target", "staged-disabled-target"] as const) { + const gate = createChrParentWriteAdmission({ profile, deadlineAt: Date.now() + 2000, deviceIds: { A: "A", B: "B" }, assertCurrent: async () => {} }) + const run = gate.run(intent, async arm => { await arm(); return await gate.authorizeWrite(intent) }) + if (profile === "direct-target") await assert.rejects(run) + else assert.deepEqual(await run, intent) + } + for (const bad of [ + { ...intent, step: "target:A:create" }, + { ...intent, path: "/ip/traffic-flow/target/set" }, + { ...intent, body: { disabled: "no", port: "1" } }, + ]) { + const gate = createChrParentWriteAdmission({ profile: "staged-disabled-target", deadlineAt: Date.now() + 2000, + deviceIds: { A: "A", B: "B" }, assertCurrent: async () => {} }) + await assert.rejects(gate.run(bad, async () => assert.fail("Native arm offered to invalid staged target"))) + } +}) diff --git a/backend/src/test/traffic-flow-chr-write-admission.ts b/backend/src/test/traffic-flow-chr-write-admission.ts new file mode 100644 index 00000000..b4c5be80 --- /dev/null +++ b/backend/src/test/traffic-flow-chr-write-admission.ts @@ -0,0 +1,134 @@ +import { isDeepStrictEqual } from "node:util" + +type Side = "A" | "B" +type Method = "PUT" | "PATCH" | "POST" | "DELETE" +type Body = Readonly> +type RelayWrite = { side: Side; method: string; path: string; body: Body } +export type ChrWriteIntent = { step: string; deviceId: string; side: Side; method: Method; path: string; body: Body } +export type ChrWriteAdmission = { + run(intent: ChrWriteIntent, send: (arm: () => Promise) => Promise): Promise +} +type Slot = { intent: ChrWriteIntent; sequence: number; phase: "pending" | "armed" | "authorizing" | "consumed" } + +export type ChrWriteScheduleProfile = "direct-target" | "staged-disabled-target" +export function chrWriteScheduleLimit(profile: ChrWriteScheduleProfile) { + if (profile === "direct-target") return 48 + if (profile === "staged-disabled-target") return 50 + return fail() +} +const fail = () => { throw Error("Exact CHR write admission refused") } +const ownMenus = new Set(["/interface/wireguard", "/interface/wireguard/peers", "/ip/address", "/ip/route", "/ip/traffic-flow/target"]) +const targetMenu = "/ip/traffic-flow/target" +const singletons = new Set(["/ip/traffic-flow", "/ip/traffic-flow/ipfix"]) +const id = /^\*[0-9a-f]{1,16}$/i + +function command(method: string, path: string, body: Body, stagedEnable: boolean) { + if (!body || Object.getPrototypeOf(body) !== Object.prototype || Object.keys(body).length > 24 || + Object.entries(body).some(([key, value]) => !/^[a-z][a-z0-9-]{0,47}$/.test(key) || typeof value !== "string" || value.length > 4096 || + /[\u0000-\u001f]/.test(value))) return fail() + const slash = path.lastIndexOf("/"), base = path.slice(0, slash), token = path.slice(slash + 1) + const count = Object.keys(body).length + if (method === "PUT" && ownMenus.has(path) && count || + method === "DELETE" && ownMenus.has(base) && id.test(token) && count === 0 || + method === "PATCH" && singletons.has(base) && id.test(token) && count || + stagedEnable && method === "PATCH" && base === targetMenu && id.test(token) && count === 1 && body.disabled === "no" || + method === "POST" && singletons.has(base) && token === "set" && count) return + return fail() +} + +/** Keep this owner in the pinned relay parent. Only the trusted workflow + * continuation can install an intent. The relay socket receives consume only. */ +export function createChrParentWriteAdmission(options: { + profile?: ChrWriteScheduleProfile + deadlineAt: number + deviceIds: Readonly> + assertCurrent(side: Side, deviceId: string): Promise + assertIntentCurrent?(intent: Readonly): Promise + now?: () => number +}) { + const { deadlineAt, assertCurrent, assertIntentCurrent, now = Date.now } = options + const profile = options.profile ?? "direct-target" + const maxWrites = chrWriteScheduleLimit(profile) + const isStagedEnable = (intent: ChrWriteIntent) => profile === "staged-disabled-target" && + intent.step === "target:" + intent.side + ":enable" + const devices = { ...options.deviceIds } + if (!devices.A || !devices.B || devices.A === devices.B || !Number.isSafeInteger(deadlineAt)) return fail() + let current: Slot | undefined, refused = false, consumed = 0 + const steps = new Set() + const live = () => { + if (refused || now() >= deadlineAt) return fail() + } + const authority = async (intent: ChrWriteIntent) => { + live() + await assertCurrent(intent.side, intent.deviceId) + await assertIntentCurrent?.(structuredClone(intent)) + live() + } + const same = (slot: Slot) => { + live() + if (current !== slot) return fail() + } + return Object.freeze({ + get consumed() { return consumed }, + retire() { refused = true }, + async run(input: ChrWriteIntent, send: (arm: () => Promise) => Promise): Promise { + let slot: Slot | undefined + try { + live() + if (current || !["A", "B"].includes(input.side) || input.deviceId !== devices[input.side] || + !/^[a-zA-Z0-9:._-]{1,100}$/.test(input.step) || steps.has(input.step) || consumed >= maxWrites) return fail() + command(input.method, input.path, input.body, isStagedEnable(input)) + if (input.step === "target:" + input.side + ":enable" && + (!isStagedEnable(input) || input.method !== "PATCH" || !input.path.startsWith(targetMenu + "/"))) return fail() + const intent = structuredClone(input) + slot = { intent, sequence: consumed + 1, phase: "pending" } + current = slot + const exact = slot + steps.add(intent.step) + const result = await send(async () => { + try { + same(exact) + if (exact.phase !== "pending") return fail() + await authority(intent) + same(exact) + if (exact.phase !== "pending") return fail() + exact.phase = "armed" + } catch (error) { refused = true; throw error } + }) + same(exact) + if (exact.phase !== "consumed" || consumed !== exact.sequence) return fail() + return result + } catch (error) { refused = true; throw error } + finally { if (current === slot) current = undefined } + }, + async authorizeWrite(input: RelayWrite) { + try { + live() + const slot = current + if (!slot || slot.phase !== "armed") return fail() + const intent = slot.intent + command(input.method, input.path, input.body, isStagedEnable(intent)) + if (input.side !== intent.side || input.method !== intent.method || input.path !== intent.path || + !isDeepStrictEqual(input.body, intent.body)) return fail() + slot.phase = "authorizing" + await authority(intent) + same(slot) + if (slot.phase !== "authorizing" || slot.sequence !== consumed + 1) return fail() + slot.phase = "consumed" + consumed++ + return structuredClone(intent) + } catch (error) { refused = true; throw error } + }, + }) +} + +export function runChrWrite(admission: ChrWriteAdmission | undefined, intent: ChrWriteIntent | undefined, + send: (arm: () => Promise) => Promise) { + if (!admission) return send(async () => {}) + if (!intent) return fail() + return admission.run(intent, send) +} + + + + diff --git a/backend/src/test/users-profile-permissions.ts b/backend/src/test/users-profile-permissions.ts new file mode 100644 index 00000000..8dce25f1 --- /dev/null +++ b/backend/src/test/users-profile-permissions.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict" +import { randomUUID } from "node:crypto" +import type { FastifyInstance } from "fastify" +import { SignJWT } from "jose" +import { env } from "../config.js" + +export async function verifyUsersProfilePermissions(app: FastifyInstance) { + const marker = randomUUID() + const token = (sub: string, permissions: string[]) => new SignJWT({ sub, apps: ["mm"], permissions }) + .setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("5m") + .sign(new TextEncoder().encode(env.jwtSecret)) + const editor = { authorization: `Bearer ${await token(`users-editor-${marker}`, ["mm:users:write"])}` } + const admin = { authorization: `Bearer ${await token(`users-admin-${marker}`, ["mm:users:write", "mm:settings:admin"])}` } + const profile = { name: "Test profile", login: `profile-${marker}`, email: "fixture@example.invalid" } + const sections = [{ section: "traffic", level: "read" }] + const servers = [{ serverId: "1", level: "read" }] + const post = (extra: object) => app.inject({ method: "POST", url: "/api/users", headers: editor, payload: { ...profile, ...extra } }) + for (const protectedField of [{ role: "admin" }, { sections }, { servers }]) { + assert.equal((await post(protectedField)).statusCode, 403) + } + const created = await post({}) + assert.equal(created.statusCode, 201) + const { user } = created.json() + const url = `/api/users/${user.id}` + const patch = (payload: object, headers = editor) => app.inject({ method: "PATCH", url, payload, headers }) + try { + assert.equal(user.role, "viewer") + assert.deepEqual([user.sections, user.servers], [[], []]) + const granted = await patch({ role: "operator", sections, servers }, admin) + assert.equal(granted.statusCode, 200) + for (const protectedField of [{ role: "admin" }, { sections: [] }, { servers: [] }]) { + assert.equal((await patch({ name: "Blocked update", ...protectedField })).statusCode, 403) + } + const unchanged = await app.inject({ method: "GET", url, headers: admin }) + assert.equal(unchanged.statusCode, 200) + assert.equal(unchanged.json().user.name, profile.name) + const edited = await patch({ name: "Edited profile", active: false, role: "operator", sections, servers }) + assert.equal(edited.statusCode, 200) + const result = edited.json().user + assert.equal(result.name, "Edited profile") + assert.equal(result.active, false) + assert.equal(result.role, "operator") + assert.deepEqual(result.sections, sections) + assert.deepEqual(result.servers, servers) + console.log("✓ REST users: ordinary profile edits and settings-admin-only role, section and server permissions") + } finally { + assert.equal((await app.inject({ method: "DELETE", url, headers: admin })).statusCode, 204) + } +} diff --git a/backend/src/test/vrf-production-execution-owned.ts b/backend/src/test/vrf-production-execution-owned.ts new file mode 100644 index 00000000..13f61d46 --- /dev/null +++ b/backend/src/test/vrf-production-execution-owned.ts @@ -0,0 +1,466 @@ +import assert from "node:assert/strict" +import { createServer } from "node:http" +import { readFile } from "node:fs/promises" +import { randomUUID } from "node:crypto" +import { createServer as createNetServer, createConnection } from "node:net" +import { setTimeout as delay } from "node:timers/promises" +import type { AuthUser } from "../lib/permissions.js" + +assert.equal(process.env.MMAPP_OWN_VRF_EXECUTION, "true") +assert.equal(process.env.NODE_ENV, "test") +const target = new URL(process.env.DATABASE_URL ?? "") +assert.equal(target.hostname, "127.0.0.1"); assert.equal(target.username, "mcp_test") +if (process.env.MMAPP_VRF_SHARED_OWNED_PG === "true") { + assert(/^mm_accept_166_\d+$/.test(process.env.MMAPP_OWN_SQL_DB ?? "")) + assert.equal(target.pathname, "/" + process.env.MMAPP_OWN_SQL_DB) +} else assert.equal(target.pathname, "/postgres") +assert(process.env.MCP_STATE_DIR && process.env.MMAPP_VRF_NATIVE_REPORT) +const slice = process.env.MMAPP_VRF_EXECUTION_SLICE ?? "baseline" +assert(["baseline", "physical", "vlan", "disabled", "mixed", "reconcile", "sql-journal", "guards", "coexistence", "alias", "gre"].includes(slice)) +// Fail closed on restricted loopback before importing DB, runtime, crypto or +// any other product module. This probe has no credentials and no remote target. +const probe = createNetServer(socket => socket.end()) +let probeClient: ReturnType | undefined +try { + await new Promise((resolve, reject) => { probe.once("error", reject); probe.listen(0, "127.0.0.1", resolve) }) + probeClient = createConnection({ host: "127.0.0.1", port: (probe.address() as { port: number }).port }) + await new Promise((resolve, reject) => { + const timeout = setTimeout(() => reject(Error("Owned loopback preflight deadline")), 5000) + probeClient!.once("connect", () => { clearTimeout(timeout); resolve() }) + probeClient!.once("error", error => { clearTimeout(timeout); reject(error) }) + }) +} finally { probeClient?.destroy(); await new Promise(resolve => probe.close(() => resolve())) } +const { pool, closePool } = await import("../db/index.js") +const { applySqlMigrations } = await import("../db/migrate.js") +await applySqlMigrations(pool) +const { installLoopbackFixtureAgent } = await import("./loopback-http-agent.js") +const { mcpConnectionCreateSchema } = await import("@mmapp/contracts/mcp") +const { updateMcpSettings, createMcpConnection, authenticateMcpToken, setConnectionStatus } = await import("../modules/mcp/store.js") +const { invokeMcpTool, approvePlan, applyPlan } = await import("../modules/mcp/runtime.js") +const { VRF_PRODUCTION_REQUIRED_CATALOGS } = await import("../services/vrf-production-admission.js") +const { VRF_PRODUCTION_RAW_PATHS } = await import("../services/vrf-production-read.js") +const { decryptSecretExisting, withVrfProductionSealingKey } = await import("../modules/mcp/security.js") +const { loadVerifiedVrfProductionHistory } = await import("../services/vrf-production-execution.js") +const { assertVrfProductionHistoryProvenance } = await import("../services/vrf-production-transition.js") +const { findMcpOperation, registerMcpOperation } = await import("../modules/mcp/registry.js") +type Row = Record +const report = JSON.parse(await readFile(process.env.MMAPP_VRF_NATIVE_REPORT, "utf8")) +const nativePhases = ["addresses-before-vrf", "vrf-create", "vrf-rename", "vrf-disable", "vrf-reenable", "vrf-membership-transfer-a-to-b", "vrf-delete-addresses-remain"] +const phases = slice === "disabled" ? [nativePhases[0], "disabled-create", "disabled-enable-first", ...nativePhases.slice(2)] : nativePhases +const createCommand = report.journal.find((value: { phase: string }) => value.phase === "vrf-create") +const selectedNames = report.phases.find((value: { phase: string }) => value.phase === nativePhases[0]).owned.bridge.raw.map((row: Row) => String(row.name)) as string[] +let coexistenceForeign = false +const deleteLoopName = "owned-gre-delete-loop" +function addSyntheticDeleteLoop(catalogs: Record, selected: boolean): void { + const target = catalogs.vrf.find(row => row.name !== "main"), table = selected ? String(target!.name) : "main" + if (selected) target!.interfaces = String(target!.interfaces) + "," + deleteLoopName + const mac = "02:00:00:00:00:30", id = "*ff30", gateway = deleteLoopName + (table === "main" ? "" : "@" + table) + catalogs.bridge.push({ ".id": id, name: deleteLoopName, disabled: "false", dynamic: "false", running: "true", "mac-address": mac, "protocol-mode": "none" }) + catalogs.interface_inventory.push({ ".id": id, name: deleteLoopName, type: "bridge", disabled: "false", dynamic: "false", running: "true", "mac-address": mac, vrf: table }) + catalogs.ipv4_address.push({ ".id": "*ff31", interface: deleteLoopName, "actual-interface": deleteLoopName, address: "198.18.246.17/32", dynamic: "false", disabled: "false", invalid: "false", vrf: table }) + catalogs.ipv4_route.push({ ".id": selected ? "*ff33" : "*ff35", "dst-address": "198.18.246.17/32", gateway, "immediate-gw": deleteLoopName, "local-address": "198.18.246.17%" + gateway, "routing-table": table, dynamic: "true", connect: "true", distance: "0", scope: "10", "target-scope": "5" }) + if (selected) { + catalogs.ipv6_address.push({ ".id": "*ff32", interface: deleteLoopName, "actual-interface": deleteLoopName, address: "fe80::ff:fe00:30/64", dynamic: "true", "auto-link-local": "true", "link-local": "true", disabled: "false", invalid: "false", vrf: "main" }) + catalogs.ipv6_route.push({ ".id": "*ff34", "dst-address": "fe80::/64", gateway, "immediate-gw": deleteLoopName, "routing-table": table, dynamic: "true", connect: "true", distance: "0", scope: "10", "target-scope": "5" }) + } +} +function nativeCatalog(phase: string): Record { + const record = report.phases.find((value: { phase: string }) => value.phase === phase) + assert(record) + const result: Record = Object.fromEntries(VRF_PRODUCTION_REQUIRED_CATALOGS.map(resource => [resource, structuredClone(record.owned[resource].raw)])) + result.vrf.push({ ".id": "*0", name: "main", interfaces: "all", disabled: "false", builtin: "true" }) + result.routing_table.unshift({ ".id": "*0", name: "main", fib: "true", dynamic: "true" }) + result.ip_settings = [{}]; result.ipv6_settings = [{}] + return result +} +function catalog(phase: string): Record { + const result = nativeCatalog(phase === "disabled-create" ? nativePhases[0] : phase === "disabled-enable-first" ? "vrf-create" : phase) + if (phase === "disabled-create") { + const created = nativeCatalog("vrf-create") + result.vrf.unshift({ ...created.vrf.find(row => row[".id"] === createCommand.assignedId), disabled: "true" }) + const inactive = structuredClone(created.interface_inventory.find(row => row.type === "vrf")!) + Object.assign(inactive, { disabled: "true", running: "false", status: "------------------------", "mac-address": "00:00:00:00:00:00", "link-downs": "0" }) + for (const key of ["ifname", "ifindex", "flags", "actual-mtu", "last-link-up-time"]) delete inactive[key] + result.interface_inventory.push(inactive) + } + if (["physical", "vlan", "mixed"].includes(slice)) { + result.bridge = result.bridge.filter(row => !selectedNames.includes(String(row.name))) + if (slice === "vlan" || slice === "mixed") { + result.interface_inventory.push({ ".id": "*ff01", name: "owned-parent", type: "ether", vrf: "main" }) + result.ethernet.push({ ".id": "*ff02", name: "owned-parent", mtu: "1500", "auto-negotiation": "yes" }) + result.vlan = [] + } + for (const [index, name] of selectedNames.entries()) { + const row = result.interface_inventory.find(row => row.name === name)! + const physical = slice === "physical" || slice === "mixed" && index === 0 + row.type = physical ? "ether" : "vlan" + if (physical) result.ethernet.push({ ".id": "*ff" + (16 + index).toString(16), name, mtu: "1500", "auto-negotiation": "yes" }) + else result.vlan.push({ ".id": "*ff" + (32 + index).toString(16), name, interface: "owned-parent", "vlan-id": String(101 + index) }) + } + } + if (slice === "coexistence" && coexistenceForeign) { + const target = result.vrf.find(row => row.name !== "main") + if (target) { + const oldName = String(target.name) + for (const rows of Object.values(result)) for (const row of rows) for (const [key, value] of Object.entries(row)) if (typeof value === "string") row[key] = value.replaceAll(oldName, "human-renamed-vrf") + target.comment = "human approved harmless metadata" + } + result.ipv4_route.push({ ".id": "*ffed", "dst-address": "203.0.113.0/24", gateway: "198.51.100.1", "routing-table": "main", dynamic: "false", disabled: "false" }) + result.ip_filter.push({ ".id": "*ffec", chain: "forward", action: "drop", comment: "legitimate foreign firewall" }) + } + if (slice === "gre") { + // Synthetic six-transition GRE lifecycle derived from the recorded bridge + // vectors. Only the remote-bound no-MAC auto-LL/endpoints match captured + // GRE observations; these lifecycle transitions are not native CHR proof. + result.gre = result.bridge.map((row, index) => ({ ".id": row[".id"], name: row.name, "local-address": `198.18.245.${index ? 13 : 9}`, "remote-address": `198.18.245.${index ? 14 : 10}` })) + for (const row of result.interface_inventory) if (selectedNames.includes(String(row.name))) { row.type = "gre-tunnel"; delete row["mac-address"] } + for (const row of result.ipv6_address) if (row.dynamic === "true" && selectedNames.includes(String(row.interface))) { + row.address = row.interface === selectedNames[0] ? "fe80::2:c612:f50a/64" : "fe80::2:c612:f50e/64" + // Synthetic context correction only; never assert native lifecycle proof. + row.vrf = result.interface_inventory.find(item => item.name === row.interface)!.vrf + } + for (const row of result.interface_inventory.filter(item => selectedNames.includes(String(item.name)))) { + // Same synthetic bounded-cycle model as the pure suite. It drives this + // existing real MCP/SQL/approval/REST profile, not a new test API or native proof. + let previous = "main", cycles = 0 + for (const step of nativePhases.slice(0, nativePhases.indexOf(phase) + 1)) { + const current = String(nativeCatalog(step).interface_inventory.find(item => item.name === row.name)!.vrf) + if (current !== previous) cycles++ + previous = current + } + const clock = (seconds: number) => new Date(Date.UTC(2026, 0, 1, 0, 0, seconds)).toISOString().slice(0, 19).replace("T", " ") + row["link-downs"] = String(cycles); row["last-link-up-time"] = clock(cycles * 2) + if (cycles) row["last-link-down-time"] = clock(cycles * 2 - 1) + else delete row["last-link-down-time"] + } + result.bridge = [] + result.wireguard = [{ ".id": "*ff11", name: "gre-source-wg", mtu: "1420", "listen-port": "55100", disabled: "false" }] + // Stable nonempty V30-shaped peer configuration, using a documentation IP. + // No keys, live underlay or native handshake are implied. The HTTP stub + // adds only synthetic changing peer telemetry to each GET response below. + result.wireguard_peer = [{ ".id": "*ff12", interface: "gre-source-wg", "endpoint-address": "198.51.100.1", "endpoint-port": "55101", "allowed-address": "198.18.245.10/32,198.18.245.14/32", "persistent-keepalive": "10s", disabled: "false" }] + result.interface_inventory.push({ ".id": "*ff11", name: "gre-source-wg", type: "wg", vrf: "main" }) + for (const [index, host] of [9, 13].entries()) result.ipv4_address.push({ ".id": `*ff2${index}`, interface: "gre-source-wg", address: `198.18.245.${host}/30`, dynamic: "false", disabled: "false", vrf: "main" }) + } + if (slice === "alias") { const target = result.vrf.find(row => row.name !== "main"); if (target) target.comment = "MikrotikManager: owned alias fixture" } + if (slice === "gre" && phase === nativePhases.at(-1)) addSyntheticDeleteLoop(result, false) + return result +} +let directDisabledDelete = false +function command(index: number) { + if (slice === "disabled" && index === 1) return { ...createCommand, label: "disabled-create", after: "disabled-create", fields: { ...createCommand.fields, disabled: "true" } } + if (slice === "disabled" && index === 2) return directDisabledDelete + ? { method: "DELETE", id: createCommand.assignedId, fields: {}, label: "disabled-delete", after: nativePhases[0] } + : { method: "PATCH", id: createCommand.assignedId, fields: { disabled: "false" }, label: "disabled-enable-first", after: "disabled-enable-first" } + return { ...report.journal.find((value: { phase: string }) => value.phase === phases[index]), label: phases[index], after: phases[index] } +} +let state = catalog(slice === "alias" ? phases[4] : phases[0]), next = slice === "alias" ? 5 : 1, writes = 0, reads = 0 +let wireguardPeerTelemetryTicks = 0 +let fault: "none" | "ignored" | "collateral" | "wrong-ack" | "lost-ack" | "post-intent-revoke" | "wrong-explicit-id" | "native-contract-drift" = "none" +let connectionId = "", revokePlanId = "", revokeAfter: Date | undefined, revoked = false +let lateRoute: Row | undefined +let aliasOspfRows: Row[] = [], nativeContractChanged = false +const pathToResource = new Map(Object.entries(VRF_PRODUCTION_RAW_PATHS).map(([resource, path]) => [path, resource])) +const password = "owned-vrf-loopback-password" +const authorization = "Basic " + Buffer.from("admin:" + password).toString("base64") +const router = createServer((request, response) => { void (async () => { + response.setHeader("content-type", "application/json") + if (request.headers.authorization !== authorization) { response.statusCode = 401; response.end("{}"); return } + const path = (request.url ?? "").replace(/^\/rest/, "") + if (request.method === "GET") { + reads++ + if (fault === "native-contract-drift" && !nativeContractChanged && revokePlanId) { + const exact = await pool.query(`SELECT o.id FROM mcp_operations o + JOIN mcp_operation_steps st ON st.operation_id=o.id AND st.step_index=0 AND st.state='intent' + JOIN mcp_audit a ON a.connection_id=o.connection_id AND a.action='configuration.intent' + AND a.created_at>=st.created_at AND a.payload->>'method'='PATCH' AND a.payload->>'path' LIKE '/ip/vrf/%' + WHERE o.connection_id=$1 AND o.plan_id=$2 AND o.state='running' LIMIT 1`, [connectionId, revokePlanId]) + if (exact.rows.length === 1) { const native = findMcpOperation("ros.vrf.update")!; registerMcpOperation({ ...native, version: native.version + 1 }); nativeContractChanged = true } + } + if (fault === "post-intent-revoke" && !revoked && revokePlanId && revokeAfter) { + const exact = await pool.query(`SELECT o.id FROM mcp_operations o + JOIN mcp_operation_steps st ON st.operation_id=o.id AND st.step_index=0 AND st.state='intent' + JOIN mcp_audit a ON a.connection_id=o.connection_id AND a.action='configuration.intent' + AND a.created_at>=st.created_at AND a.created_at>=$3 AND a.payload->>'method'='PUT' + AND a.payload->>'path'='/ip/vrf' AND a.payload->>'serverId'=st.server_id::text + WHERE o.connection_id=$1 AND o.plan_id=$2 AND o.state='running' LIMIT 1`, [connectionId, revokePlanId, revokeAfter]) + if (exact.rows.length === 1) { + await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1", [connectionId]); revoked = true + } + } + if (slice === "alias" && path === "/routing/ospf/interface-template") { response.end(JSON.stringify(aliasOspfRows)); return } + const resource = pathToResource.get(path) + if (slice === "gre" && resource === "wireguard_peer") { + wireguardPeerTelemetryTicks++ + assert(wireguardPeerTelemetryTicks <= 10000, "Synthetic peer telemetry tick budget") + response.end(JSON.stringify(state.wireguard_peer.map(row => ({ ...row, rx: String(wireguardPeerTelemetryTicks * 64), tx: String(wireguardPeerTelemetryTicks * 128), "last-handshake": `${wireguardPeerTelemetryTicks}s` })))); return + } + response.end(JSON.stringify(resource ? [...state[resource] ?? [], ...(resource === "ipv4_route" && lateRoute ? [lateRoute] : [])] : [])); return + } + assert(path === "/ip/vrf" || path.startsWith("/ip/vrf/"), "Only the exact VRF native command is permitted") + let body = ""; for await (const chunk of request) { body += chunk; assert(Buffer.byteLength(body) < 32768) } + const expected = command(next) + assert(expected); assert.equal(request.method, expected.method) + const fields = body ? JSON.parse(body) : {} + assert.deepEqual(fields, request.method === "PUT" ? { ...expected.fields, "place-before": "*0" } : expected.fields) + if (request.method !== "PUT") assert.equal(decodeURIComponent(path.slice("/ip/vrf/".length)), expected.id) + writes++ + if (fault !== "ignored") state = catalog(expected.after) + if (fault === "collateral") state.bridge[0].unexpected = "collateral fixture drift" + next++ + if (fault === "lost-ack") { response.destroy(); return } + if (request.method === "DELETE" && fault !== "wrong-explicit-id") { response.statusCode = 204; response.end(); return } + response.end(JSON.stringify(request.method === "PUT" ? { ".id": fault === "wrong-ack" ? "*0" : expected.assignedId } : fault === "wrong-explicit-id" ? { ".id": "*0" } : {})) +})().catch(() => { response.statusCode = 500; response.end("{}") }) }) +await new Promise(resolve => router.listen(0, "127.0.0.1", resolve)) +const closeAgent = installLoopbackFixtureAgent(router) +const actor: AuthUser = { id: "owned-vrf-executor", email: "vrf@example.invalid", name: "VRF owned acceptance", apps: ["mm"], permissions: ["mm:settings:admin", "mm:network:admin", "mm:servers:admin"], expiresAt: Date.now() + 1800000 } +const accepted: string[] = [] +const diagnosticStarted = performance.now() +// Disabled slice only: phase = positive ordinal * 100 + fixed stage below. +// At most 92 numeric stage records; no plan IDs, payloads or error text. +const disabledStage = (stage: number) => { + if (slice === "disabled") console.log(JSON.stringify({ phase: (accepted.length + 1) * 100 + stage, elapsedMs: Math.round(performance.now() - diagnosticStarted) })) +} +try { + await updateMcpSettings({ enabled: true }, actor) + const serverId = Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,username,password,enabled) VALUES('owned-vrf-runtime','127.0.0.1',$1,false,'admin',$2,true) RETURNING id", [(router.address() as { port: number }).port, password])).rows[0].id) + const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned VRF production", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write", "mm:network:read"], scope: { serverIds: [serverId], operationIds: slice === "alias" ? ["routing.vrf.update"] : ["ros.vrf.create", "ros.vrf.update", "ros.vrf.delete", "operations.reconcile", "operations.recover"], allowGlobal: false }, limits: { callsPerMinute: 120, objectsPerPlan: 100, mutationsPerHour: 100 } }), actor) + connectionId = access.connection.id + const principal = await authenticateMcpToken(access.token) + const wait = async (id: string) => { + const end = performance.now() + 60000 + while (performance.now() < end) { const row = (await pool.query("SELECT * FROM mcp_operations WHERE id=$1", [id])).rows[0]; if (!["pending", "running"].includes(row.state)) return row; await delay(25) } + throw Error("VRF owned operation terminal timeout") + } + const prepare = async () => { + const selected = command(next) + const action = selected.method === "PUT" ? "create" : selected.method === "DELETE" ? "delete" : "update" + const fields = Object.fromEntries(Object.entries(selected.fields).map(([key, value]) => [key, key === "disabled" ? value === "true" : value])) + disabledStage(10) // prepare tool: before/after + const result = await invokeMcpTool(principal, slice === "alias" ? "mm_vrf_plan" : `mm_vrf_${action}_plan`, slice === "alias" ? { serverId, id: selected.id, interfaces: String(fields.interfaces).split(",") } : { serverId, productionAdmission: true, ...(selected.id ? { id: selected.id } : {}), fields }) as { planId: string; vrfTicket: { ticketId: string } } + disabledStage(11) + assert(result.vrfTicket?.ticketId) + disabledStage(20) // human approval: before/after + await approvePlan(result.planId, actor, true) + disabledStage(21) + return result + } + let lastOperationId = "" + const positive = async (label: string) => { + const beforePeerTicks = wireguardPeerTelemetryTicks + const prepared = await prepare(), beforeWrites = writes + disabledStage(30) // apply call: before/after + const operation = await applyPlan(principal, prepared.planId, randomUUID()) + disabledStage(31) + disabledStage(40) // terminal wait: before/after + const terminal = await wait(operation.id) + disabledStage(41) + disabledStage(50) // SQL/crypto/replay postverification: before/after + assert.equal(terminal.state, "applied", JSON.stringify({ phase: label, error: terminal.error_code, result: terminal.result })) + assert.equal(terminal.verification, "passed"); assert.equal(writes, beforeWrites + 1) + const step = (await pool.query("SELECT * FROM mcp_operation_steps WHERE operation_id=$1", [operation.id])).rows[0] + assert.equal(step.state, "verified"); assert.equal(step.result.configurationVerified, true); assert.equal(step.result.forwardingVerified, false) + const checkpoint = (await pool.query("SELECT before_sealed,after_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1", [operation.id])).rows[0] + assert.equal(JSON.parse(await decryptSecretExisting(checkpoint.after_sealed)).kind, "vrf-production-private-checkpoint-v1") + if (slice === "gre") { + assert(wireguardPeerTelemetryTicks > beforePeerTicks, "Each GRE lifecycle phase must read changing peer telemetry") + const { before } = JSON.parse(await decryptSecretExisting(checkpoint.before_sealed)) + if (next === phases.length) { + // Synthetic external current-state addition before fresh DELETE approval; + // not a claimed earlier native action or historical checkpoint rewrite. + assert(before.catalogs.bridge.some((row: Row) => row.name === deleteLoopName)) + assert(before.catalogs.vrf.some((row: Row) => String(row.interfaces).split(",").includes(deleteLoopName))) + assert(before.catalogs.ipv6_address.some((row: Row) => row.interface === deleteLoopName)) + assert(!state.ipv6_address.some(row => row.interface === deleteLoopName)) + assert(state.ipv4_address.some(row => row.interface === deleteLoopName && row[".id"] === "*ff31")) + assert(state.ipv6_address.some(row => row.interface === selectedNames[1] && row.dynamic === "true"), "Prior GRE acquisition absence must still revive on DELETE") + } + await withVrfProductionSealingKey(async secret => { + const history = await loadVerifiedVrfProductionHistory({ sql: pool, ownerId: actor.id, serverId, sourceProof: before.sourceProof, nativeId: step.result.nativeId, secret }) + assert(history, "GRE verified SQL checkpoint must reopen") + assertVrfProductionHistoryProvenance(history, { ...before, catalogs: state }) + }) + } + assert.equal((await pool.query("SELECT state FROM vrf_production_tickets WHERE ticket_id=$1", [prepared.vrfTicket.ticketId])).rows[0].state, "claimed") + const replay = await applyPlan(principal, prepared.planId, randomUUID()); assert.equal(replay.id, operation.id); assert.equal(writes, beforeWrites + 1) + disabledStage(51) + accepted.push(label) + lastOperationId = operation.id + console.log(JSON.stringify({ slice, acceptedCase: label, nativeWrites: writes, ...(slice === "gre" ? { wireguardPeerTelemetryTicks } : {}), configurationVerified: true, forwardingVerified: false })) + } + for (let phase = 1; phase < (["sql-journal", "guards", "coexistence", "alias"].includes(slice) ? 1 : slice === "reconcile" ? 2 : phases.length); phase++) { + if (slice === "gre" && phase === phases.length - 1) { + // Model a separate administrator adding an empty loop bridge and selecting + // it after the first five actions. The sixth action gets a fresh approval + // and ticket for this complete current graph, with prior GRE debt intact. + addSyntheticDeleteLoop(state, true) + } + await positive(phases[phase]) + } + if (slice === "alias") { + await assert.rejects(invokeMcpTool(principal, "mm_vrf_update_plan", { serverId, id: command(next).id, productionAdmission: true, fields: { interfaces: selectedNames[1] } })) + const target = state.vrf.find(row => row.name !== "main")!, comment = target.comment + target.comment = "unmanaged" + await assert.rejects(prepare(), /существующего VRF приложения/); target.comment = comment + const protectionId = randomUUID(), parent = state.bridge.find(row => row.name === selectedNames[0])! + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protectionId, JSON.stringify({ kind: "bridge", id: parent[".id"], serverId, protection: "deny" }), actor.id]) + try { await assert.rejects(prepare()) } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) } + assert.equal(writes, 0); accepted.push("managed-prefix-and-original-parent-protection-preserved") + const stale = await prepare() + aliasOspfRows = [{ ".id": "*ffe9", interfaces: "foreign-ospf", area: "foreign" }] + const staleOperation = await applyPlan(principal, stale.planId, randomUUID()), staleTerminal = await wait(staleOperation.id) + assert.notEqual(staleTerminal.state, "applied"); assert.equal(writes, 0); aliasOspfRows = [] + accepted.push("alias-original-extra-dependency-drift-zero-writes") + const oldPublic = await prepare() + await pool.query("UPDATE mcp_plans SET operation_version=2 WHERE id=$1", [oldPublic.planId]) + await assert.rejects(applyPlan(principal, oldPublic.planId, randomUUID())); assert.equal(writes, 0) + const oldV3Public = await prepare() + await pool.query("UPDATE mcp_plans SET operation_version=3 WHERE id=$1", [oldV3Public.planId]) + await assert.rejects(applyPlan(principal, oldV3Public.planId, randomUUID())); assert.equal(writes, 0) + const originalNative = findMcpOperation("ros.vrf.update")!, nativeStale = await prepare() + revokePlanId = nativeStale.planId; fault = "native-contract-drift" + try { + const operation = await applyPlan(principal, nativeStale.planId, randomUUID()), terminal = await wait(operation.id) + assert(nativeContractChanged); assert.notEqual(terminal.state, "applied"); assert.equal(writes, 0) + assert.equal((await pool.query("SELECT state FROM vrf_production_tickets WHERE ticket_id=$1", [nativeStale.vrfTicket.ticketId])).rows[0].state, "claimed") + assert.equal((await applyPlan(principal, nativeStale.planId, randomUUID())).id, operation.id); assert.equal(writes, 0) + } finally { registerMcpOperation(originalNative); fault = "none" } + accepted.push("public-v2-and-fresh-native-version-fences-no-replay") + await positive("alias-only-grant-actual-apply-canonical-tool-denied") + const applied = (await pool.query("SELECT p.operation_id,p.operation_version,t.action_id,t.action_version,t.ciphertext FROM mcp_operations o JOIN mcp_plans p ON p.id=o.plan_id JOIN vrf_production_tickets t ON t.plan_id=p.id WHERE o.id=$1", [lastOperationId])).rows[0] + assert.equal(applied.operation_id, "routing.vrf.update"); assert.equal(applied.operation_version, 4); assert.equal(applied.action_id, "ros.vrf.update") + const privateContext = JSON.parse(await decryptSecretExisting(applied.ciphertext)).context + assert.deepEqual(privateContext.authorityAction, { id: "routing.vrf.update", version: 4 }); assert.equal(privateContext.actionVersion, originalNative.version) + assert.equal(writes, 1) + } + if (slice === "coexistence") { + for (let phase = 1; phase <= 3; phase++) await positive("setup-" + phases[phase]) + accepted.length = 0 + coexistenceForeign = true; state = catalog(phases[3]) + await positive("fresh-approved-foreign-route-firewall-primary-metadata-coexist") + const stale = await prepare(), beforeWrites = writes + lateRoute = { ".id": "*ffeb", "dst-address": "203.0.114.0/24", gateway: "198.51.100.2", "routing-table": "main", dynamic: "false" } + const operation = await applyPlan(principal, stale.planId, randomUUID()), terminal = await wait(operation.id) + assert.notEqual(terminal.state, "applied"); assert.equal(writes, beforeWrites); lateRoute = undefined + accepted.push("after-approval-unrelated-foreign-drift-zero-writes") + await positive("setup-missing-LL-transfer"); accepted.pop() + const member = state.interface_inventory.find(row => row.name === selectedNames[1])! + member["mac-address"] = "02:00:00:00:00:99" + const plans = Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n), causalBeforeWrites = writes + await assert.rejects(prepare(), /missing LL member causal/) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n), plans) + assert.equal(writes, causalBeforeWrites); accepted.push("causal-missing-LL-member-MAC-drift-refused-before-write") + console.log(JSON.stringify({ slice, acceptedCase: accepted, nativeWrites: writes, causalProvenance: true })) + } + if (slice === "guards") { + for (const action of ["create", "update", "delete"]) { + await assert.rejects(invokeMcpTool(principal, `mm_vrf_${action}_plan`, { serverId, ...(action === "create" ? {} : { id: createCommand.assignedId }), fields: action === "create" ? createCommand.fields : {} }), error => Boolean(error && typeof error === "object" && "code" in error && error.code === "UNSUPPORTED_CAPABILITY")) + accepted.push(`explicit-admission-required-${action}`) + } + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM vrf_production_tickets WHERE connection_id=$1", [connectionId])).rows[0].n), 0) + assert.equal(Number((await pool.query("SELECT coalesce(sum(used),0)::int AS n FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='mutations'", [connectionId])).rows[0].n), 0) + assert.equal(writes, 0) + await positive("guard-setup-create") + const stale = await prepare(), writesBeforeLateRoute = writes + lateRoute = { ".id": "*fffe", "dst-address": "192.0.2.99/32", "routing-table": "main", gateway: `${selectedNames[0]}@${state.vrf.find(row => row.name !== "main")!.name}`, dynamic: "false" } + const staleOperation = await applyPlan(principal, stale.planId, randomUUID()), staleTerminal = await wait(staleOperation.id) + assert.notEqual(staleTerminal.state, "applied"); assert.equal(writes, writesBeforeLateRoute); lateRoute = undefined + accepted.push("late-approved-external-gateway-zero-writes") + const wrongExplicitAck = async (label: string) => { + const prepared = await prepare(), beforeWrites = writes; fault = "wrong-explicit-id" + const operation = await applyPlan(principal, prepared.planId, randomUUID()), terminal = await wait(operation.id) + assert.equal(terminal.state, "partial"); assert.equal(terminal.result.confirmedWrites, 1); assert.equal(writes, beforeWrites + 1) + assert.equal((await pool.query("SELECT after_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1", [operation.id])).rows[0].after_sealed, null) + assert.equal((await applyPlan(principal, prepared.planId, randomUUID())).id, operation.id); assert.equal(writes, beforeWrites + 1) + accepted.push(label); fault = "none" + console.log(JSON.stringify({ slice, acceptedCase: label, nativeWrites: writes, confirmedWrites: 1, state: terminal.state })) + } + await wrongExplicitAck("contradictory-PATCH-ACK-no-replay") + state = catalog(phases[1]); next = 2 + for (let phase = 2; phase < phases.length - 1; phase++) await positive(phases[phase]) + await wrongExplicitAck("contradictory-DELETE-ACK-no-replay") + } + if (slice === "sql-journal") { + const prepared = await prepare(), beforeWrites = writes + await pool.query("CREATE FUNCTION owned_vrf_ack_journal_fail() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.state='applied' AND NEW.result->>'kind'='vrf-production-ack-v1' THEN RAISE EXCEPTION 'owned VRF ACK journal failure'; END IF; RETURN NEW; END $$") + await pool.query("CREATE TRIGGER owned_vrf_ack_journal_fail BEFORE UPDATE ON mcp_operation_steps FOR EACH ROW EXECUTE FUNCTION owned_vrf_ack_journal_fail()") + try { + const operation = await applyPlan(principal, prepared.planId, randomUUID()), terminal = await wait(operation.id) + assert.equal(terminal.state, "unknown"); assert.equal(terminal.result.confirmedWrites, 1); assert.equal(writes, beforeWrites + 1) + const checkpoint = (await pool.query("SELECT after_sealed FROM mcp_operation_checkpoints WHERE operation_id=$1", [operation.id])).rows[0] + assert.equal(checkpoint.after_sealed, null) + const replay = await applyPlan(principal, prepared.planId, randomUUID()); assert.equal(replay.id, operation.id); assert.equal(writes, beforeWrites + 1) + accepted.push("actual-ACK-journal-SQL-failure-known-write-no-replay") + console.log(JSON.stringify({ slice, acceptedCase: accepted[0], nativeWrites: writes, state: terminal.state, confirmedWrites: terminal.result.confirmedWrites })) + } finally { await pool.query("DROP TRIGGER owned_vrf_ack_journal_fail ON mcp_operation_steps"); await pool.query("DROP FUNCTION owned_vrf_ack_journal_fail()") } + } + if (slice === "reconcile") { + const beforeWrites = writes + const compare = async (expected: boolean | null, available: boolean) => { + const value = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: lastOperationId }) as { results: Array<{ matchesVerifiedCheckpoint: boolean | null; checkpointAvailable: boolean; comparisonScope: string; forwardingVerified: boolean }> } + assert.equal(value.results[0].matchesVerifiedCheckpoint, expected); assert.equal(value.results[0].checkpointAvailable, available) + assert.equal(value.results[0].comparisonScope, "whole-native-companion-configuration"); assert.equal(value.results[0].forwardingVerified, false); assert.equal(writes, beforeWrites) + } + await compare(true, true); accepted.push("whole-companion-readonly-match") + state.bridge[0].opaque = "late companion drift"; await compare(false, true); delete state.bridge[0].opaque; accepted.push("whole-companion-readonly-drift") + state.bridge[0]["rx-byte"] = "77"; await compare(true, true); delete state.bridge[0]["rx-byte"]; accepted.push("readonly-reviewed-telemetry") + await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: lastOperationId }), error => Boolean(error && typeof error === "object" && "code" in error && error.code === "UNSUPPORTED_CAPABILITY")); accepted.push("explicit-no-inverse-revision") + await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1", [lastOperationId]) + await compare(null, false); accepted.push("unverified-checkpoint-is-unknown") + console.log(JSON.stringify({ slice, acceptedCase: accepted.slice(1), nativeWrites: writes, readonly: true })) + } + if (slice === "disabled") { + disabledStage(0) // direct disabled-create/delete reset: before/after + state = catalog(phases[0]); next = 1; directDisabledDelete = true + disabledStage(1) + await positive("disabled-create-direct-delete"); await positive("disabled-delete") + } + if (["physical", "vlan", "mixed"].includes(slice)) { + state = catalog(phases[0]); next = 1 + const prepared = await prepare(), beforeWrites = writes + state.ethernet[0].mtu = "1400" + const operation = await applyPlan(principal, prepared.planId, randomUUID()), terminal = await wait(operation.id) + assert.notEqual(terminal.state, "applied"); assert.equal(writes, beforeWrites) + accepted.push("original-parent-drift-before-dispatch") + console.log(JSON.stringify({ slice, acceptedCase: "original-parent-drift-before-dispatch", nativeWrites: writes, operationState: terminal.state })) + } + if (slice === "vlan") { + state = catalog(phases[0]); next = 1 + const prepared = await prepare(), beforeWrites = writes, protectionId = randomUUID() + await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protectionId, JSON.stringify({ kind: "ethernet", id: "*ff02", serverId, protection: "deny" }), actor.id]) + try { + await assert.rejects(applyPlan(principal, prepared.planId, randomUUID()), error => Boolean(error && typeof error === "object" && "code" in error && error.code === "PROTECTED_OBJECT")) + assert.equal(writes, beforeWrites) + accepted.push("closed-physical-parent-protection-deny") + console.log(JSON.stringify({ slice, acceptedCase: "closed-physical-parent-protection-deny", nativeWrites: writes })) + } finally { await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protectionId]) } + const limited = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned closed VRF budget", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [serverId], operationIds: ["ros.vrf.create"], allowGlobal: false }, limits: { callsPerMinute: 120, objectsPerPlan: 3, mutationsPerHour: 100 } }), actor) + const limitedPrincipal = await authenticateMcpToken(limited.token) + await assert.rejects(invokeMcpTool(limitedPrincipal, "mm_vrf_create_plan", { serverId, productionAdmission: true, fields: createCommand.fields })) + assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE connection_id=$1", [limited.connection.id])).rows[0].n), 0) + assert.equal(writes, beforeWrites) + accepted.push("closed-parent-object-budget-atomic-refusal") + console.log(JSON.stringify({ slice, acceptedCase: "closed-parent-object-budget-atomic-refusal", nativeWrites: writes })) + } + for (const selected of (slice === "baseline" ? ["ignored", "collateral", "wrong-ack", "lost-ack", "post-intent-revoke"] : []) as Array<"ignored" | "collateral" | "wrong-ack" | "lost-ack" | "post-intent-revoke">) { + state = catalog(phases[0]); next = 1; fault = "none"; revoked = false + const prepared = await prepare(), beforeWrites = writes + revokePlanId = prepared.planId + revokeAfter = (await pool.query("SELECT clock_timestamp() AS instant")).rows[0].instant as Date + fault = selected + const operation = await applyPlan(principal, prepared.planId, randomUUID()), terminal = await wait(operation.id) + assert.notEqual(terminal.state, "applied", selected) + assert.equal(writes, beforeWrites + (selected === "post-intent-revoke" ? 0 : 1), selected + " exact no-retry dispatch count") + if (selected === "post-intent-revoke") { assert(revoked); await setConnectionStatus(connectionId, "active", actor) } + else { const replay = await applyPlan(principal, prepared.planId, randomUUID()); assert.equal(replay.id, operation.id); assert.equal(writes, beforeWrites + 1) } + accepted.push(selected) + console.log(JSON.stringify({ acceptedCase: selected, nativeWrites: writes, operationState: terminal.state, forwardingVerified: false })) + } + assert(reads > 0) + if (slice === "gre") assert(wireguardPeerTelemetryTicks > 6 && wireguardPeerTelemetryTicks <= 10000) + console.log(JSON.stringify({ slice, rawShape: slice === "baseline" ? "authoritative recorded v5 bridge vectors" : slice === "disabled" ? "synthetic disabled-CREATE derived from v5 inactive vector; never-enabled defaults not actual CHR" : slice === "gre" ? "synthetic no-MAC GRE lifecycle derived from v5 bridge vectors and captured remote-bound auto-LL; static WG source parent with native wg alias and synthetic ticking peer telemetry; not actual CHR lifecycle" : "synthetic physical/VLAN concrete-parent projection derived from v5 context vectors", accepted, writes, rawReads: reads, ...(slice === "gre" ? { wireguardPeerTelemetryTicks } : {}), actualSql: true, actualCrypto: true, actualAuthPolicyApprovalRuntime: true, fakeVerifier: false, actualRouter: false, forwardingVerified: false })) +} finally { + closeAgent(); await new Promise(resolve => router.close(() => resolve())); await closePool() +} diff --git a/backend/src/types/server.ts b/backend/src/types/server.ts index 8d4ecb16..482dde39 100644 --- a/backend/src/types/server.ts +++ b/backend/src/types/server.ts @@ -216,8 +216,11 @@ export interface RosOspfInterfaceTemplate { ".id": string "area": string // area NAME (not area-id) "interfaces"?: string // interface name or "*ID" ref + "networks"?: string // comma-separated prefix selectors + "prefix-list"?: string "cost"?: string "priority"?: string + "passive"?: string // normalized from the native switch presence receipts "type"?: string // "ptp" | "broadcast" | "nbma" | "ptmp" | "ptmp-broadcast" "disabled": string "inactive": string